Bernhard Reutner-Fischer | c5280e8 | 2005-09-20 21:09:31 +0000 | [diff] [blame] | 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 | * |
Bernhard Reutner-Fischer | 7fee0c4 | 2006-09-13 16:39:19 +0000 | [diff] [blame] | 7 | * Licensed under the GPL v2 or later, see the file LICENSE in this tarball. |
Bernhard Reutner-Fischer | c5280e8 | 2005-09-20 21:09:31 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
Denis Vlasenko | 7d219aa | 2006-10-05 10:17:08 +0000 | [diff] [blame] | 10 | #include "libbb.h" |
Bernhard Reutner-Fischer | c5280e8 | 2005-09-20 21:09:31 +0000 | [diff] [blame] | 11 | |
Denis Vlasenko | 9a44c4f | 2006-12-28 05:44:47 +0000 | [diff] [blame] | 12 | #define assert(x) ((void)0) |
| 13 | |
Denis Vlasenko | becd8c5 | 2006-12-01 21:34:20 +0000 | [diff] [blame] | 14 | /* internal function for bb_getpwuid and bb_getgrgid */ |
Denis Vlasenko | 3734b94 | 2007-07-27 11:20:10 +0000 | [diff] [blame] | 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) |
Denis Vlasenko | becd8c5 | 2006-12-01 21:34:20 +0000 | [diff] [blame] | 28 | { |
Denis Vlasenko | 219d14d | 2007-03-24 15:40:16 +0000 | [diff] [blame] | 29 | if (bufsize > 0) { |
Denis Vlasenko | 9a44c4f | 2006-12-28 05:44:47 +0000 | [diff] [blame] | 30 | assert(buffer != NULL); |
| 31 | if (idname) { |
Denis Vlasenko | becd8c5 | 2006-12-01 21:34:20 +0000 | [diff] [blame] | 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 | } |
| 40 | |
Denis Vlasenko | 3734b94 | 2007-07-27 11:20:10 +0000 | [diff] [blame] | 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 |
Denis Vlasenko | 9a44c4f | 2006-12-28 05:44:47 +0000 | [diff] [blame] | 46 | */ |
Denis Vlasenko | 3734b94 | 2007-07-27 11:20:10 +0000 | [diff] [blame] | 47 | /* gets a username given a uid */ |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame^] | 48 | char* FAST_FUNC bb_getpwuid(char *name, int bufsize, long uid) |
Denis Vlasenko | 3734b94 | 2007-07-27 11:20:10 +0000 | [diff] [blame] | 49 | { |
| 50 | struct passwd *myuser = getpwuid(uid); |
Bernhard Reutner-Fischer | c5280e8 | 2005-09-20 21:09:31 +0000 | [diff] [blame] | 51 | |
Denis Vlasenko | 3734b94 | 2007-07-27 11:20:10 +0000 | [diff] [blame] | 52 | return bb_getug(name, bufsize, |
| 53 | (myuser ? myuser->pw_name : (char*)myuser), |
| 54 | uid, 'u'); |
| 55 | } |
Bernhard Reutner-Fischer | c5280e8 | 2005-09-20 21:09:31 +0000 | [diff] [blame] | 56 | /* gets a groupname given a gid */ |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame^] | 57 | char* FAST_FUNC bb_getgrgid(char *group, int bufsize, long gid) |
Bernhard Reutner-Fischer | c5280e8 | 2005-09-20 21:09:31 +0000 | [diff] [blame] | 58 | { |
| 59 | struct group *mygroup = getgrgid(gid); |
| 60 | |
Denis Vlasenko | 3734b94 | 2007-07-27 11:20:10 +0000 | [diff] [blame] | 61 | return bb_getug(group, bufsize, |
| 62 | (mygroup ? mygroup->gr_name : (char*)mygroup), |
| 63 | gid, 'g'); |
Bernhard Reutner-Fischer | c5280e8 | 2005-09-20 21:09:31 +0000 | [diff] [blame] | 64 | } |
Bernhard Reutner-Fischer | c5280e8 | 2005-09-20 21:09:31 +0000 | [diff] [blame] | 65 | |
| 66 | /* returns a gid given a group name */ |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame^] | 67 | long FAST_FUNC xgroup2gid(const char *name) |
Bernhard Reutner-Fischer | c5280e8 | 2005-09-20 21:09:31 +0000 | [diff] [blame] | 68 | { |
| 69 | struct group *mygroup; |
| 70 | |
Denis Vlasenko | 9a44c4f | 2006-12-28 05:44:47 +0000 | [diff] [blame] | 71 | mygroup = getgrnam(name); |
| 72 | if (mygroup == NULL) |
Bernhard Reutner-Fischer | c5280e8 | 2005-09-20 21:09:31 +0000 | [diff] [blame] | 73 | bb_error_msg_and_die("unknown group name: %s", name); |
| 74 | |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 75 | return mygroup->gr_gid; |
Bernhard Reutner-Fischer | c5280e8 | 2005-09-20 21:09:31 +0000 | [diff] [blame] | 76 | } |
Bernhard Reutner-Fischer | c5280e8 | 2005-09-20 21:09:31 +0000 | [diff] [blame] | 77 | |
| 78 | /* returns a uid given a username */ |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame^] | 79 | long FAST_FUNC xuname2uid(const char *name) |
Bernhard Reutner-Fischer | c5280e8 | 2005-09-20 21:09:31 +0000 | [diff] [blame] | 80 | { |
| 81 | struct passwd *myuser; |
| 82 | |
Denis Vlasenko | 9a44c4f | 2006-12-28 05:44:47 +0000 | [diff] [blame] | 83 | myuser = getpwnam(name); |
| 84 | if (myuser == NULL) |
Bernhard Reutner-Fischer | c5280e8 | 2005-09-20 21:09:31 +0000 | [diff] [blame] | 85 | bb_error_msg_and_die("unknown user name: %s", name); |
| 86 | |
| 87 | return myuser->pw_uid; |
| 88 | } |
Bernhard Reutner-Fischer | c5280e8 | 2005-09-20 21:09:31 +0000 | [diff] [blame] | 89 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame^] | 90 | unsigned long FAST_FUNC get_ug_id(const char *s, |
| 91 | long FAST_FUNC (*xname2id)(const char *)) |
Bernhard Reutner-Fischer | c5280e8 | 2005-09-20 21:09:31 +0000 | [diff] [blame] | 92 | { |
| 93 | unsigned long r; |
Bernhard Reutner-Fischer | c5280e8 | 2005-09-20 21:09:31 +0000 | [diff] [blame] | 94 | |
Denis Vlasenko | becd8c5 | 2006-12-01 21:34:20 +0000 | [diff] [blame] | 95 | r = bb_strtoul(s, NULL, 10); |
| 96 | if (errno) |
Denis Vlasenko | 9a44c4f | 2006-12-28 05:44:47 +0000 | [diff] [blame] | 97 | return xname2id(s); |
Bernhard Reutner-Fischer | c5280e8 | 2005-09-20 21:09:31 +0000 | [diff] [blame] | 98 | return r; |
| 99 | } |