Annotation of /trunk/mkinitrd-magellan/busybox/libbb/bb_pwd.c
Parent Directory | Revision Log
Revision 532 -
(hide annotations)
(download)
Sat Sep 1 22:45:15 2007 UTC (16 years, 9 months ago) by niro
File MIME type: text/plain
File size: 4066 byte(s)
Sat Sep 1 22:45:15 2007 UTC (16 years, 9 months ago) by niro
File MIME type: text/plain
File size: 4066 byte(s)
-import if magellan mkinitrd; it is a fork of redhats mkinitrd-5.0.8 with all magellan patches and features; deprecates magellan-src/mkinitrd
1 | niro | 532 | /* vi: set sw=4 ts=4: */ |
2 | /* | ||
3 | * password utility routines. | ||
4 | * | ||
5 | * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> | ||
6 | * | ||
7 | * Licensed under the GPL v2 or later, see the file LICENSE in this tarball. | ||
8 | */ | ||
9 | |||
10 | #include "libbb.h" | ||
11 | |||
12 | #define assert(x) ((void)0) | ||
13 | |||
14 | /* | ||
15 | * if bufsize is > 0 char *buffer cannot be set to NULL. | ||
16 | * If idname is not NULL it is written on the static | ||
17 | * allocated buffer (and a pointer to it is returned). | ||
18 | * if idname is NULL, id as string is written to the static | ||
19 | * allocated buffer and NULL is returned. | ||
20 | * if bufsize is = 0 char *buffer can be set to NULL. | ||
21 | * If idname exists a pointer to it is returned, | ||
22 | * else NULL is returned. | ||
23 | * if bufsize is < 0 char *buffer can be set to NULL. | ||
24 | * If idname exists a pointer to it is returned, | ||
25 | * else an error message is printed and the program exits. | ||
26 | */ | ||
27 | |||
28 | /* internal function for bb_getpwuid and bb_getgrgid */ | ||
29 | static char* bb_getug(char *buffer, char *idname, long id, int bufsize, char prefix) | ||
30 | { | ||
31 | if (bufsize > 0 ) { | ||
32 | assert(buffer != NULL); | ||
33 | if (idname) { | ||
34 | return safe_strncpy(buffer, idname, bufsize); | ||
35 | } | ||
36 | snprintf(buffer, bufsize, "%ld", id); | ||
37 | } else if (bufsize < 0 && !idname) { | ||
38 | bb_error_msg_and_die("unknown %cid %ld", prefix, id); | ||
39 | } | ||
40 | return idname; | ||
41 | } | ||
42 | |||
43 | /* Hacked by Tito Ragusa (c) 2004 <farmatito@tiscali.it> to make it more | ||
44 | * flexible : | ||
45 | * | ||
46 | * if bufsize is > 0 char *group cannot be set to NULL. | ||
47 | * On success groupname is written on static allocated buffer | ||
48 | * group (and a pointer to it is returned). | ||
49 | * On failure gid as string is written to static allocated | ||
50 | * buffer group and NULL is returned. | ||
51 | * if bufsize is = 0 char *group can be set to NULL. | ||
52 | * On success groupname is returned. | ||
53 | * On failure NULL is returned. | ||
54 | * if bufsize is < 0 char *group can be set to NULL. | ||
55 | * On success groupname is returned. | ||
56 | * On failure an error message is printed and | ||
57 | * the program exits. | ||
58 | */ | ||
59 | |||
60 | /* gets a groupname given a gid */ | ||
61 | char* bb_getgrgid(char *group, long gid, int bufsize) | ||
62 | { | ||
63 | struct group *mygroup = getgrgid(gid); | ||
64 | |||
65 | return bb_getug(group, | ||
66 | mygroup ? mygroup->gr_name : (char *)mygroup, | ||
67 | gid, bufsize, 'g'); | ||
68 | } | ||
69 | |||
70 | /* returns a gid given a group name */ | ||
71 | long xgroup2gid(const char *name) | ||
72 | { | ||
73 | struct group *mygroup; | ||
74 | |||
75 | mygroup = getgrnam(name); | ||
76 | if (mygroup == NULL) | ||
77 | bb_error_msg_and_die("unknown group name: %s", name); | ||
78 | |||
79 | return mygroup->gr_gid; | ||
80 | } | ||
81 | |||
82 | /* returns a uid given a username */ | ||
83 | long xuname2uid(const char *name) | ||
84 | { | ||
85 | struct passwd *myuser; | ||
86 | |||
87 | myuser = getpwnam(name); | ||
88 | if (myuser == NULL) | ||
89 | bb_error_msg_and_die("unknown user name: %s", name); | ||
90 | |||
91 | return myuser->pw_uid; | ||
92 | } | ||
93 | |||
94 | /* Hacked by Tito Ragusa (c) 2004 <farmatito@tiscali.it> to make it more | ||
95 | * flexible : | ||
96 | * | ||
97 | * if bufsize is > 0 char *name cannot be set to NULL. | ||
98 | * On success username is written on the static allocated | ||
99 | * buffer name (and a pointer to it is returned). | ||
100 | * On failure uid as string is written to the static | ||
101 | * allocated buffer name and NULL is returned. | ||
102 | * if bufsize is = 0 char *name can be set to NULL. | ||
103 | * On success username is returned. | ||
104 | * On failure NULL is returned. | ||
105 | * if bufsize is < 0 char *name can be set to NULL | ||
106 | * On success username is returned. | ||
107 | * On failure an error message is printed and | ||
108 | * the program exits. | ||
109 | */ | ||
110 | |||
111 | /* gets a username given a uid */ | ||
112 | char* bb_getpwuid(char *name, long uid, int bufsize) | ||
113 | { | ||
114 | struct passwd *myuser = getpwuid(uid); | ||
115 | |||
116 | return bb_getug(name, myuser ? myuser->pw_name : (char *)myuser, | ||
117 | uid, bufsize, 'u'); | ||
118 | } | ||
119 | |||
120 | unsigned long get_ug_id(const char *s, | ||
121 | long (*xname2id)(const char *)) | ||
122 | { | ||
123 | unsigned long r; | ||
124 | |||
125 | r = bb_strtoul(s, NULL, 10); | ||
126 | if (errno) | ||
127 | return xname2id(s); | ||
128 | return r; | ||
129 | } |