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> |
Denis Vlasenko | 0c68a87 | 2008-12-02 22:56:59 +0000 | [diff] [blame] | 6 | * Copyright (C) 2008 by Tito Ragusa <farmatito@tiscali.it> |
Bernhard Reutner-Fischer | c5280e8 | 2005-09-20 21:09:31 +0000 | [diff] [blame] | 7 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 8 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Bernhard Reutner-Fischer | c5280e8 | 2005-09-20 21:09:31 +0000 | [diff] [blame] | 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 | 0c68a87 | 2008-12-02 22:56:59 +0000 | [diff] [blame] | 12 | /* TODO: maybe change API to return malloced data? |
| 13 | * This will allow to stop using libc functions returning |
| 14 | * pointers to static data (getpwuid) |
Denis Vlasenko | 3734b94 | 2007-07-27 11:20:10 +0000 | [diff] [blame] | 15 | */ |
Denis Vlasenko | 0c68a87 | 2008-12-02 22:56:59 +0000 | [diff] [blame] | 16 | |
Denis Vlasenko | d7a805e | 2008-12-03 19:05:55 +0000 | [diff] [blame] | 17 | struct passwd* FAST_FUNC xgetpwnam(const char *name) |
| 18 | { |
| 19 | struct passwd *pw = getpwnam(name); |
| 20 | if (!pw) |
| 21 | bb_error_msg_and_die("unknown user %s", name); |
| 22 | return pw; |
| 23 | } |
| 24 | |
Denis Vlasenko | 15437e3 | 2008-12-05 16:23:06 +0000 | [diff] [blame] | 25 | struct group* FAST_FUNC xgetgrnam(const char *name) |
| 26 | { |
| 27 | struct group *gr = getgrnam(name); |
| 28 | if (!gr) |
| 29 | bb_error_msg_and_die("unknown group %s", name); |
| 30 | return gr; |
| 31 | } |
| 32 | |
Denis Vlasenko | 0c68a87 | 2008-12-02 22:56:59 +0000 | [diff] [blame] | 33 | struct passwd* FAST_FUNC xgetpwuid(uid_t uid) |
Denis Vlasenko | becd8c5 | 2006-12-01 21:34:20 +0000 | [diff] [blame] | 34 | { |
Denys Vlasenko | 39194f0 | 2017-08-03 19:00:01 +0200 | [diff] [blame] | 35 | /* Note: used in nofork applets (whoami), be careful not to leak anything */ |
Denis Vlasenko | 0c68a87 | 2008-12-02 22:56:59 +0000 | [diff] [blame] | 36 | struct passwd *pw = getpwuid(uid); |
| 37 | if (!pw) |
| 38 | bb_error_msg_and_die("unknown uid %u", (unsigned)uid); |
| 39 | return pw; |
Denis Vlasenko | becd8c5 | 2006-12-01 21:34:20 +0000 | [diff] [blame] | 40 | } |
| 41 | |
Denis Vlasenko | 0c68a87 | 2008-12-02 22:56:59 +0000 | [diff] [blame] | 42 | struct group* FAST_FUNC xgetgrgid(gid_t gid) |
Denis Vlasenko | 3734b94 | 2007-07-27 11:20:10 +0000 | [diff] [blame] | 43 | { |
Denis Vlasenko | 0c68a87 | 2008-12-02 22:56:59 +0000 | [diff] [blame] | 44 | struct group *gr = getgrgid(gid); |
| 45 | if (!gr) |
| 46 | bb_error_msg_and_die("unknown gid %u", (unsigned)gid); |
| 47 | return gr; |
Bernhard Reutner-Fischer | c5280e8 | 2005-09-20 21:09:31 +0000 | [diff] [blame] | 48 | } |
Bernhard Reutner-Fischer | c5280e8 | 2005-09-20 21:09:31 +0000 | [diff] [blame] | 49 | |
Denis Vlasenko | 0c68a87 | 2008-12-02 22:56:59 +0000 | [diff] [blame] | 50 | char* FAST_FUNC xuid2uname(uid_t uid) |
Bernhard Reutner-Fischer | c5280e8 | 2005-09-20 21:09:31 +0000 | [diff] [blame] | 51 | { |
Denys Vlasenko | 39194f0 | 2017-08-03 19:00:01 +0200 | [diff] [blame] | 52 | /* Note: used in nofork applets (whoami), be careful not to leak anything */ |
Denis Vlasenko | 0c68a87 | 2008-12-02 22:56:59 +0000 | [diff] [blame] | 53 | struct passwd *pw = xgetpwuid(uid); |
| 54 | return pw->pw_name; |
Bernhard Reutner-Fischer | c5280e8 | 2005-09-20 21:09:31 +0000 | [diff] [blame] | 55 | } |
Bernhard Reutner-Fischer | c5280e8 | 2005-09-20 21:09:31 +0000 | [diff] [blame] | 56 | |
Denis Vlasenko | 0c68a87 | 2008-12-02 22:56:59 +0000 | [diff] [blame] | 57 | char* FAST_FUNC xgid2group(gid_t gid) |
| 58 | { |
| 59 | struct group *gr = xgetgrgid(gid); |
| 60 | return gr->gr_name; |
| 61 | } |
| 62 | |
| 63 | char* FAST_FUNC uid2uname(uid_t uid) |
| 64 | { |
| 65 | struct passwd *pw = getpwuid(uid); |
| 66 | return (pw) ? pw->pw_name : NULL; |
| 67 | } |
| 68 | |
| 69 | char* FAST_FUNC gid2group(gid_t gid) |
| 70 | { |
| 71 | struct group *gr = getgrgid(gid); |
| 72 | return (gr) ? gr->gr_name : NULL; |
| 73 | } |
| 74 | |
Denys Vlasenko | 7d65abe | 2011-03-01 16:27:13 +0100 | [diff] [blame] | 75 | char* FAST_FUNC uid2uname_utoa(uid_t uid) |
Denis Vlasenko | 0c68a87 | 2008-12-02 22:56:59 +0000 | [diff] [blame] | 76 | { |
| 77 | char *name = uid2uname(uid); |
| 78 | return (name) ? name : utoa(uid); |
| 79 | } |
| 80 | |
Denys Vlasenko | 7d65abe | 2011-03-01 16:27:13 +0100 | [diff] [blame] | 81 | char* FAST_FUNC gid2group_utoa(gid_t gid) |
Denis Vlasenko | 0c68a87 | 2008-12-02 22:56:59 +0000 | [diff] [blame] | 82 | { |
| 83 | char *name = gid2group(gid); |
| 84 | return (name) ? name : utoa(gid); |
| 85 | } |
| 86 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 87 | long FAST_FUNC xuname2uid(const char *name) |
Bernhard Reutner-Fischer | c5280e8 | 2005-09-20 21:09:31 +0000 | [diff] [blame] | 88 | { |
| 89 | struct passwd *myuser; |
| 90 | |
Denis Vlasenko | d7a805e | 2008-12-03 19:05:55 +0000 | [diff] [blame] | 91 | myuser = xgetpwnam(name); |
Bernhard Reutner-Fischer | c5280e8 | 2005-09-20 21:09:31 +0000 | [diff] [blame] | 92 | return myuser->pw_uid; |
| 93 | } |
Bernhard Reutner-Fischer | c5280e8 | 2005-09-20 21:09:31 +0000 | [diff] [blame] | 94 | |
Denis Vlasenko | 0c68a87 | 2008-12-02 22:56:59 +0000 | [diff] [blame] | 95 | long FAST_FUNC xgroup2gid(const char *name) |
| 96 | { |
| 97 | struct group *mygroup; |
| 98 | |
Denis Vlasenko | 15437e3 | 2008-12-05 16:23:06 +0000 | [diff] [blame] | 99 | mygroup = xgetgrnam(name); |
Denis Vlasenko | 0c68a87 | 2008-12-02 22:56:59 +0000 | [diff] [blame] | 100 | return mygroup->gr_gid; |
| 101 | } |
| 102 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 103 | unsigned long FAST_FUNC get_ug_id(const char *s, |
| 104 | long FAST_FUNC (*xname2id)(const char *)) |
Bernhard Reutner-Fischer | c5280e8 | 2005-09-20 21:09:31 +0000 | [diff] [blame] | 105 | { |
| 106 | unsigned long r; |
Bernhard Reutner-Fischer | c5280e8 | 2005-09-20 21:09:31 +0000 | [diff] [blame] | 107 | |
Denis Vlasenko | becd8c5 | 2006-12-01 21:34:20 +0000 | [diff] [blame] | 108 | r = bb_strtoul(s, NULL, 10); |
| 109 | if (errno) |
Denis Vlasenko | 9a44c4f | 2006-12-28 05:44:47 +0000 | [diff] [blame] | 110 | return xname2id(s); |
Bernhard Reutner-Fischer | c5280e8 | 2005-09-20 21:09:31 +0000 | [diff] [blame] | 111 | return r; |
| 112 | } |