1 /* vi: set sw=4 ts=4: */
3 * password utility routines.
5 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
7 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
15 /* Hacked by Tito Ragusa (c) 2004 <farmatito@tiscali.it> to make it more
18 * if bufsize is > 0 char *group cannot be set to NULL.
19 * On success groupname is written on static allocated buffer
20 * group (and a pointer to it is returned).
21 * On failure gid as string is written to static allocated
22 * buffer group and NULL is returned.
23 * if bufsize is = 0 char *group can be set to NULL.
24 * On success groupname is returned.
25 * On failure NULL is returned.
26 * if bufsize is < 0 char *group can be set to NULL.
27 * On success groupname is returned.
28 * On failure an error message is printed and
32 /* gets a groupname given a gid */
33 char * bb_getgrgid(char *group, long gid, int bufsize)
35 struct group *mygroup = getgrgid(gid);
37 return bb_getug(group, (mygroup) ?
38 mygroup->gr_name : (char *)mygroup, gid, bufsize, 'g');
41 /* returns a gid given a group name */
42 long bb_xgetgrnam(const char *name)
44 struct group *mygroup;
46 mygroup = getgrnam(name);
48 bb_error_msg_and_die("unknown group name: %s", name);
50 return (mygroup->gr_gid);
53 /* returns a uid given a username */
54 long bb_xgetpwnam(const char *name)
56 struct passwd *myuser;
58 myuser = getpwnam(name);
60 bb_error_msg_and_die("unknown user name: %s", name);
62 return myuser->pw_uid;
65 /* Hacked by Tito Ragusa (c) 2004 <farmatito@tiscali.it> to make it more
68 * if bufsize is > 0 char *name cannot be set to NULL.
69 * On success username is written on the static allocated
70 * buffer name (and a pointer to it is returned).
71 * On failure uid as string is written to the static
72 * allocated buffer name and NULL is returned.
73 * if bufsize is = 0 char *name can be set to NULL.
74 * On success username is returned.
75 * On failure NULL is returned.
76 * if bufsize is < 0 char *name can be set to NULL
77 * On success username is returned.
78 * On failure an error message is printed and
82 /* gets a username given a uid */
83 char * bb_getpwuid(char *name, long uid, int bufsize)
85 struct passwd *myuser = getpwuid(uid);
87 return bb_getug(name, (myuser) ?
88 myuser->pw_name : (char *)myuser , uid, bufsize, 'u');
92 * if bufsize is > 0 char *buffer cannot be set to NULL.
93 * If idname is not NULL it is written on the static
94 * allocated buffer (and a pointer to it is returned).
95 * if idname is NULL, id as string is written to the static
96 * allocated buffer and NULL is returned.
97 * if bufsize is = 0 char *buffer can be set to NULL.
98 * If idname exists a pointer to it is returned,
99 * else NULL is returned.
100 * if bufsize is < 0 char *buffer can be set to NULL.
101 * If idname exists a pointer to it is returned,
102 * else an error message is printed and the program exits.
105 /* internal function for bb_getpwuid and bb_getgrgid */
106 char * bb_getug(char *buffer, char *idname, long id, int bufsize, char prefix)
109 assert(buffer!=NULL);
111 return safe_strncpy(buffer, idname, bufsize);
113 snprintf(buffer, bufsize, "%ld", id);
114 } else if(bufsize < 0 && !idname) {
115 bb_error_msg_and_die("unknown %cid %ld", prefix, id);
120 unsigned long get_ug_id(const char *s,
121 long (*__bb_getxxnam)(const char *))
126 r = strtoul(s, &p, 10);
127 if (*p || (s == p)) {
128 r = __bb_getxxnam(s);