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 | */ |
| 10 | |
Denis Vlasenko | 7d219aa | 2006-10-05 10:17:08 +0000 | [diff] [blame] | 11 | #include "libbb.h" |
Bernhard Reutner-Fischer | c5280e8 | 2005-09-20 21:09:31 +0000 | [diff] [blame] | 12 | |
Denis Vlasenko | 0c68a87 | 2008-12-02 22:56:59 +0000 | [diff] [blame] | 13 | /* TODO: maybe change API to return malloced data? |
| 14 | * This will allow to stop using libc functions returning |
| 15 | * pointers to static data (getpwuid) |
Denis Vlasenko | 3734b94 | 2007-07-27 11:20:10 +0000 | [diff] [blame] | 16 | */ |
Denis Vlasenko | 0c68a87 | 2008-12-02 22:56:59 +0000 | [diff] [blame] | 17 | |
Denis Vlasenko | d7a805e | 2008-12-03 19:05:55 +0000 | [diff] [blame] | 18 | struct passwd* FAST_FUNC xgetpwnam(const char *name) |
| 19 | { |
| 20 | struct passwd *pw = getpwnam(name); |
| 21 | if (!pw) |
| 22 | bb_error_msg_and_die("unknown user %s", name); |
| 23 | return pw; |
| 24 | } |
| 25 | |
Denis Vlasenko | 15437e3 | 2008-12-05 16:23:06 +0000 | [diff] [blame] | 26 | struct group* FAST_FUNC xgetgrnam(const char *name) |
| 27 | { |
| 28 | struct group *gr = getgrnam(name); |
| 29 | if (!gr) |
| 30 | bb_error_msg_and_die("unknown group %s", name); |
| 31 | return gr; |
| 32 | } |
| 33 | |
Denis Vlasenko | 0c68a87 | 2008-12-02 22:56:59 +0000 | [diff] [blame] | 34 | |
| 35 | struct passwd* FAST_FUNC xgetpwuid(uid_t uid) |
Denis Vlasenko | becd8c5 | 2006-12-01 21:34:20 +0000 | [diff] [blame] | 36 | { |
Denis Vlasenko | 0c68a87 | 2008-12-02 22:56:59 +0000 | [diff] [blame] | 37 | struct passwd *pw = getpwuid(uid); |
| 38 | if (!pw) |
| 39 | bb_error_msg_and_die("unknown uid %u", (unsigned)uid); |
| 40 | return pw; |
Denis Vlasenko | becd8c5 | 2006-12-01 21:34:20 +0000 | [diff] [blame] | 41 | } |
| 42 | |
Denis Vlasenko | 0c68a87 | 2008-12-02 22:56:59 +0000 | [diff] [blame] | 43 | struct group* FAST_FUNC xgetgrgid(gid_t gid) |
Denis Vlasenko | 3734b94 | 2007-07-27 11:20:10 +0000 | [diff] [blame] | 44 | { |
Denis Vlasenko | 0c68a87 | 2008-12-02 22:56:59 +0000 | [diff] [blame] | 45 | struct group *gr = getgrgid(gid); |
| 46 | if (!gr) |
| 47 | bb_error_msg_and_die("unknown gid %u", (unsigned)gid); |
| 48 | return gr; |
Bernhard Reutner-Fischer | c5280e8 | 2005-09-20 21:09:31 +0000 | [diff] [blame] | 49 | } |
Bernhard Reutner-Fischer | c5280e8 | 2005-09-20 21:09:31 +0000 | [diff] [blame] | 50 | |
Denis Vlasenko | 0c68a87 | 2008-12-02 22:56:59 +0000 | [diff] [blame] | 51 | char* FAST_FUNC xuid2uname(uid_t uid) |
Bernhard Reutner-Fischer | c5280e8 | 2005-09-20 21:09:31 +0000 | [diff] [blame] | 52 | { |
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 | } |
Denys Vlasenko | cffe28e | 2013-11-26 13:46:18 +0100 | [diff] [blame^] | 113 | |
| 114 | /* Experimental "mallocing" API. |
| 115 | * The goal is nice: "we want to support a case when "guests" group is very large" |
| 116 | * but the code is butt-ugly. |
| 117 | */ |
| 118 | #if 0 |
| 119 | static char *find_latest(char last, char *cp) |
| 120 | { |
| 121 | if (!cp) |
| 122 | return last; |
| 123 | cp += strlen(cp) + 1; |
| 124 | if (last < cp) |
| 125 | last = cp; |
| 126 | return last; |
| 127 | } |
| 128 | |
| 129 | struct group* FAST_FUNC xmalloc_getgrnam(const char *name) |
| 130 | { |
| 131 | struct { |
| 132 | struct group gr; |
| 133 | // May still be not enough! |
| 134 | char buf[64*1024 - sizeof(struct group) - 16]; |
| 135 | } *s; |
| 136 | struct group *grp; |
| 137 | int r; |
| 138 | char *last; |
| 139 | char **gr_mem; |
| 140 | |
| 141 | s = xmalloc(sizeof(*s)); |
| 142 | r = getgrnam_r(name, &s->gr, s->buf, sizeof(s->buf), &grp); |
| 143 | if (!grp) { |
| 144 | free(s); |
| 145 | return grp; |
| 146 | } |
| 147 | last = find_latest(s->buf, grp->gr_name); |
| 148 | last = find_latest(last, grp->gr_passwd); |
| 149 | gr_mem = grp->gr_mem; |
| 150 | while (*gr_mem) |
| 151 | last = find_latest(last, *gr_mem++); |
| 152 | gr_mem++; /* points past NULL */ |
| 153 | if (last < (char*)gr_mem) |
| 154 | last = (char*)gr_mem; |
| 155 | //FIXME: what if we get not only truncated, but also moved here? |
| 156 | // grp->gr_name pointer and friends are invalid now!!! |
| 157 | s = xrealloc(s, last - (char*)s); |
| 158 | return grp; |
| 159 | } |
| 160 | #endif |