[821] | 1 | /* vi: set sw=4 ts=4: */
|
---|
| 2 | /*
|
---|
| 3 | * password utility routines.
|
---|
| 4 | *
|
---|
| 5 | * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
|
---|
| 6 | *
|
---|
[1770] | 7 | * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
|
---|
[821] | 8 | */
|
---|
| 9 |
|
---|
[1770] | 10 | #include "libbb.h"
|
---|
[821] | 11 |
|
---|
[1770] | 12 | #define assert(x) ((void)0)
|
---|
[821] | 13 |
|
---|
[1770] | 14 | /* internal function for bb_getpwuid and bb_getgrgid */
|
---|
| 15 | /* Hacked by Tito Ragusa (c) 2004 <farmatito@tiscali.it> to make it more
|
---|
| 16 | * flexible:
|
---|
| 17 | *
|
---|
| 18 | * bufsize > 0: If idname is not NULL it is copied to buffer,
|
---|
| 19 | * and buffer is returned. Else id as string is written
|
---|
| 20 | * to buffer, and NULL is returned.
|
---|
| 21 | *
|
---|
| 22 | * bufsize == 0: idname is returned.
|
---|
| 23 | *
|
---|
| 24 | * bufsize < 0: If idname is not NULL it is returned.
|
---|
| 25 | * Else an error message is printed and the program exits.
|
---|
| 26 | */
|
---|
| 27 | static char* bb_getug(char *buffer, int bufsize, char *idname, long id, char prefix)
|
---|
| 28 | {
|
---|
| 29 | if (bufsize > 0) {
|
---|
| 30 | assert(buffer != NULL);
|
---|
| 31 | if (idname) {
|
---|
| 32 | return safe_strncpy(buffer, idname, bufsize);
|
---|
| 33 | }
|
---|
| 34 | snprintf(buffer, bufsize, "%ld", id);
|
---|
| 35 | } else if (bufsize < 0 && !idname) {
|
---|
| 36 | bb_error_msg_and_die("unknown %cid %ld", prefix, id);
|
---|
| 37 | }
|
---|
| 38 | return idname;
|
---|
| 39 | }
|
---|
[821] | 40 |
|
---|
[1770] | 41 | /* bb_getpwuid, bb_getgrgid:
|
---|
| 42 | * bb_getXXXid(buf, bufsz, id) - copy user/group name or id
|
---|
| 43 | * as a string to buf, return user/group name or NULL
|
---|
| 44 | * bb_getXXXid(NULL, 0, id) - return user/group name or NULL
|
---|
| 45 | * bb_getXXXid(NULL, -1, id) - return user/group name or exit
|
---|
| 46 | */
|
---|
| 47 | /* gets a username given a uid */
|
---|
| 48 | char* bb_getpwuid(char *name, int bufsize, long uid)
|
---|
| 49 | {
|
---|
| 50 | struct passwd *myuser = getpwuid(uid);
|
---|
| 51 |
|
---|
| 52 | return bb_getug(name, bufsize,
|
---|
| 53 | (myuser ? myuser->pw_name : (char*)myuser),
|
---|
| 54 | uid, 'u');
|
---|
| 55 | }
|
---|
[821] | 56 | /* gets a groupname given a gid */
|
---|
[1770] | 57 | char* bb_getgrgid(char *group, int bufsize, long gid)
|
---|
[821] | 58 | {
|
---|
| 59 | struct group *mygroup = getgrgid(gid);
|
---|
| 60 |
|
---|
[1770] | 61 | return bb_getug(group, bufsize,
|
---|
| 62 | (mygroup ? mygroup->gr_name : (char*)mygroup),
|
---|
| 63 | gid, 'g');
|
---|
[821] | 64 | }
|
---|
| 65 |
|
---|
| 66 | /* returns a gid given a group name */
|
---|
[1770] | 67 | long xgroup2gid(const char *name)
|
---|
[821] | 68 | {
|
---|
| 69 | struct group *mygroup;
|
---|
| 70 |
|
---|
[1770] | 71 | mygroup = getgrnam(name);
|
---|
| 72 | if (mygroup == NULL)
|
---|
[821] | 73 | bb_error_msg_and_die("unknown group name: %s", name);
|
---|
| 74 |
|
---|
[1770] | 75 | return mygroup->gr_gid;
|
---|
[821] | 76 | }
|
---|
| 77 |
|
---|
| 78 | /* returns a uid given a username */
|
---|
[1770] | 79 | long xuname2uid(const char *name)
|
---|
[821] | 80 | {
|
---|
| 81 | struct passwd *myuser;
|
---|
| 82 |
|
---|
[1770] | 83 | myuser = getpwnam(name);
|
---|
| 84 | if (myuser == NULL)
|
---|
[821] | 85 | bb_error_msg_and_die("unknown user name: %s", name);
|
---|
| 86 |
|
---|
| 87 | return myuser->pw_uid;
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | unsigned long get_ug_id(const char *s,
|
---|
[1770] | 91 | long (*xname2id)(const char *))
|
---|
[821] | 92 | {
|
---|
| 93 | unsigned long r;
|
---|
| 94 |
|
---|
[1770] | 95 | r = bb_strtoul(s, NULL, 10);
|
---|
| 96 | if (errno)
|
---|
| 97 | return xname2id(s);
|
---|
[821] | 98 | return r;
|
---|
| 99 | }
|
---|