"Robert P. J. Day" | 63fc1a9 | 2006-07-02 19:47:05 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Denys Vlasenko | 2ab9403 | 2017-10-05 15:33:28 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2014 Tito Ragusa <farmatito@tiscali.it> |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 4 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 5 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 6 | */ |
Tito Ragusa | 1da09cf | 2015-01-02 21:37:59 +0100 | [diff] [blame] | 7 | /* This program is distributed in the hope that it will be useful, |
| 8 | * but WITHOUT ANY WARRANTY!! |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 9 | * |
Tito Ragusa | 1da09cf | 2015-01-02 21:37:59 +0100 | [diff] [blame] | 10 | * Rewrite of some parts. Main differences are: |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 11 | * |
Tito Ragusa | 1da09cf | 2015-01-02 21:37:59 +0100 | [diff] [blame] | 12 | * 1) the buffer for getpwuid, getgrgid, getpwnam, getgrnam is dynamically |
Denys Vlasenko | c5d4a04 | 2015-01-05 15:09:04 +0100 | [diff] [blame] | 13 | * allocated. |
Tito Ragusa | 1da09cf | 2015-01-02 21:37:59 +0100 | [diff] [blame] | 14 | * If ENABLE_FEATURE_CLEAN_UP is set the buffers are freed at program |
| 15 | * exit using the atexit function to make valgrind happy. |
| 16 | * 2) the passwd/group files: |
| 17 | * a) must contain the expected number of fields (as per count of field |
Denys Vlasenko | 10ad622 | 2017-04-17 16:13:32 +0200 | [diff] [blame] | 18 | * delimiters ":") or we will complain with a error message. |
Denys Vlasenko | c5d4a04 | 2015-01-05 15:09:04 +0100 | [diff] [blame] | 19 | * b) leading and trailing whitespace in fields is stripped. |
Denys Vlasenko | b5dabd9 | 2015-10-13 00:31:02 +0200 | [diff] [blame] | 20 | * c) some fields are not allowed to be empty (e.g. username, uid/gid), |
| 21 | * and in this case NULL is returned and errno is set to EINVAL. |
| 22 | * This behaviour could be easily changed by modifying PW_DEF, GR_DEF, |
| 23 | * SP_DEF strings (uppercase makes a field mandatory). |
Tito Ragusa | 1da09cf | 2015-01-02 21:37:59 +0100 | [diff] [blame] | 24 | * d) the string representing uid/gid must be convertible by strtoXX |
Denys Vlasenko | 31d6734 | 2015-01-03 15:15:47 +0100 | [diff] [blame] | 25 | * functions, or errno is set to EINVAL. |
Denys Vlasenko | c5d4a04 | 2015-01-05 15:09:04 +0100 | [diff] [blame] | 26 | * e) leading and trailing whitespace in group member names is stripped. |
Denys Vlasenko | 31d6734 | 2015-01-03 15:15:47 +0100 | [diff] [blame] | 27 | * 3) the internal function for getgrouplist uses dynamically allocated buffer. |
| 28 | * 4) at the moment only the functions really used by busybox code are |
Tito Ragusa | 1da09cf | 2015-01-02 21:37:59 +0100 | [diff] [blame] | 29 | * implemented, if you need a particular missing function it should be |
| 30 | * easy to write it by using the internal common code. |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 31 | */ |
Rob Landley | ea224be | 2006-06-18 20:20:07 +0000 | [diff] [blame] | 32 | #include "libbb.h" |
Bernhard Reutner-Fischer | fa939aa | 2006-04-05 16:21:37 +0000 | [diff] [blame] | 33 | |
Denys Vlasenko | 8d547ac | 2015-01-03 15:54:04 +0100 | [diff] [blame] | 34 | struct const_passdb { |
| 35 | const char *filename; |
Denys Vlasenko | c5d4a04 | 2015-01-05 15:09:04 +0100 | [diff] [blame] | 36 | char def[7 + 2*ENABLE_USE_BB_SHADOW]; |
| 37 | uint8_t off[7 + 2*ENABLE_USE_BB_SHADOW]; |
Denys Vlasenko | 8d547ac | 2015-01-03 15:54:04 +0100 | [diff] [blame] | 38 | uint8_t numfields; |
Denys Vlasenko | 134c530 | 2015-01-03 20:47:47 +0100 | [diff] [blame] | 39 | uint8_t size_of; |
Denys Vlasenko | 8d547ac | 2015-01-03 15:54:04 +0100 | [diff] [blame] | 40 | }; |
| 41 | struct passdb { |
| 42 | const char *filename; |
Denys Vlasenko | c5d4a04 | 2015-01-05 15:09:04 +0100 | [diff] [blame] | 43 | char def[7 + 2*ENABLE_USE_BB_SHADOW]; |
| 44 | uint8_t off[7 + 2*ENABLE_USE_BB_SHADOW]; |
Denys Vlasenko | 8d547ac | 2015-01-03 15:54:04 +0100 | [diff] [blame] | 45 | uint8_t numfields; |
Denys Vlasenko | 134c530 | 2015-01-03 20:47:47 +0100 | [diff] [blame] | 46 | uint8_t size_of; |
Denys Vlasenko | 8d547ac | 2015-01-03 15:54:04 +0100 | [diff] [blame] | 47 | FILE *fp; |
| 48 | char *malloced; |
Denys Vlasenko | 8d547ac | 2015-01-03 15:54:04 +0100 | [diff] [blame] | 49 | }; |
Denys Vlasenko | 134c530 | 2015-01-03 20:47:47 +0100 | [diff] [blame] | 50 | /* Note: for shadow db, def[] will not contain terminating NUL, |
Denys Vlasenko | 20c0a16 | 2015-01-03 19:12:49 +0100 | [diff] [blame] | 51 | * but convert_to_struct() logic detects def[] end by "less than SP?", |
| 52 | * not by "is it NUL?" condition; and off[0] happens to be zero |
| 53 | * for every db anyway, so there _is_ in fact a terminating NUL there. |
| 54 | */ |
Denys Vlasenko | 8d547ac | 2015-01-03 15:54:04 +0100 | [diff] [blame] | 55 | |
Denys Vlasenko | 31d6734 | 2015-01-03 15:15:47 +0100 | [diff] [blame] | 56 | /* S = string not empty, s = string maybe empty, |
| 57 | * I = uid,gid, l = long maybe empty, m = members, |
| 58 | * r = reserved |
| 59 | */ |
Denys Vlasenko | 5de4502 | 2015-10-12 04:06:18 +0200 | [diff] [blame] | 60 | #define PW_DEF "SsIIsss" |
Tito Ragusa | 1da09cf | 2015-01-02 21:37:59 +0100 | [diff] [blame] | 61 | #define GR_DEF "SsIm" |
| 62 | #define SP_DEF "Ssllllllr" |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 63 | |
Denys Vlasenko | 8d547ac | 2015-01-03 15:54:04 +0100 | [diff] [blame] | 64 | static const struct const_passdb const_pw_db = { |
| 65 | _PATH_PASSWD, PW_DEF, |
| 66 | { |
| 67 | offsetof(struct passwd, pw_name), /* 0 S */ |
| 68 | offsetof(struct passwd, pw_passwd), /* 1 s */ |
| 69 | offsetof(struct passwd, pw_uid), /* 2 I */ |
| 70 | offsetof(struct passwd, pw_gid), /* 3 I */ |
| 71 | offsetof(struct passwd, pw_gecos), /* 4 s */ |
Denys Vlasenko | b5dabd9 | 2015-10-13 00:31:02 +0200 | [diff] [blame] | 72 | offsetof(struct passwd, pw_dir), /* 5 s */ |
| 73 | offsetof(struct passwd, pw_shell) /* 6 s */ |
Denys Vlasenko | 8d547ac | 2015-01-03 15:54:04 +0100 | [diff] [blame] | 74 | }, |
Denys Vlasenko | 134c530 | 2015-01-03 20:47:47 +0100 | [diff] [blame] | 75 | sizeof(PW_DEF)-1, sizeof(struct passwd) |
Tito Ragusa | 1da09cf | 2015-01-02 21:37:59 +0100 | [diff] [blame] | 76 | }; |
Denys Vlasenko | 8d547ac | 2015-01-03 15:54:04 +0100 | [diff] [blame] | 77 | static const struct const_passdb const_gr_db = { |
| 78 | _PATH_GROUP, GR_DEF, |
| 79 | { |
| 80 | offsetof(struct group, gr_name), /* 0 S */ |
| 81 | offsetof(struct group, gr_passwd), /* 1 s */ |
| 82 | offsetof(struct group, gr_gid), /* 2 I */ |
| 83 | offsetof(struct group, gr_mem) /* 3 m (char **) */ |
| 84 | }, |
Denys Vlasenko | 134c530 | 2015-01-03 20:47:47 +0100 | [diff] [blame] | 85 | sizeof(GR_DEF)-1, sizeof(struct group) |
Tito Ragusa | 1da09cf | 2015-01-02 21:37:59 +0100 | [diff] [blame] | 86 | }; |
Denis Vlasenko | 7fa0fca | 2006-12-28 21:33:30 +0000 | [diff] [blame] | 87 | #if ENABLE_USE_BB_SHADOW |
Denys Vlasenko | 8d547ac | 2015-01-03 15:54:04 +0100 | [diff] [blame] | 88 | static const struct const_passdb const_sp_db = { |
| 89 | _PATH_SHADOW, SP_DEF, |
| 90 | { |
| 91 | offsetof(struct spwd, sp_namp), /* 0 S Login name */ |
| 92 | offsetof(struct spwd, sp_pwdp), /* 1 s Encrypted password */ |
| 93 | offsetof(struct spwd, sp_lstchg), /* 2 l */ |
| 94 | offsetof(struct spwd, sp_min), /* 3 l */ |
| 95 | offsetof(struct spwd, sp_max), /* 4 l */ |
| 96 | offsetof(struct spwd, sp_warn), /* 5 l */ |
| 97 | offsetof(struct spwd, sp_inact), /* 6 l */ |
| 98 | offsetof(struct spwd, sp_expire), /* 7 l */ |
| 99 | offsetof(struct spwd, sp_flag) /* 8 r Reserved */ |
| 100 | }, |
Denys Vlasenko | 134c530 | 2015-01-03 20:47:47 +0100 | [diff] [blame] | 101 | sizeof(SP_DEF)-1, sizeof(struct spwd) |
Tito Ragusa | 1da09cf | 2015-01-02 21:37:59 +0100 | [diff] [blame] | 102 | }; |
Denis Vlasenko | 7fa0fca | 2006-12-28 21:33:30 +0000 | [diff] [blame] | 103 | #endif |
| 104 | |
Tito Ragusa | 1da09cf | 2015-01-02 21:37:59 +0100 | [diff] [blame] | 105 | /* We avoid having big global data. */ |
Denis Vlasenko | 2c91efb | 2007-06-18 10:08:27 +0000 | [diff] [blame] | 106 | struct statics { |
Denys Vlasenko | c5d4a04 | 2015-01-05 15:09:04 +0100 | [diff] [blame] | 107 | /* We use same buffer (db[0].malloced) for getpwuid and getpwnam. |
Denys Vlasenko | 31d6734 | 2015-01-03 15:15:47 +0100 | [diff] [blame] | 108 | * Manpage says: |
Denys Vlasenko | acdb004 | 2012-01-06 16:24:56 +0100 | [diff] [blame] | 109 | * "The return value may point to a static area, and may be overwritten |
| 110 | * by subsequent calls to getpwent(), getpwnam(), or getpwuid()." |
| 111 | */ |
Tito Ragusa | 1da09cf | 2015-01-02 21:37:59 +0100 | [diff] [blame] | 112 | struct passdb db[2 + ENABLE_USE_BB_SHADOW]; |
| 113 | char *tokenize_end; |
Denys Vlasenko | 134c530 | 2015-01-03 20:47:47 +0100 | [diff] [blame] | 114 | unsigned string_size; |
Denis Vlasenko | 2c91efb | 2007-06-18 10:08:27 +0000 | [diff] [blame] | 115 | }; |
| 116 | |
| 117 | static struct statics *ptr_to_statics; |
Tito Ragusa | 1da09cf | 2015-01-02 21:37:59 +0100 | [diff] [blame] | 118 | #define S (*ptr_to_statics) |
| 119 | #define has_S (ptr_to_statics) |
Denis Vlasenko | 2c91efb | 2007-06-18 10:08:27 +0000 | [diff] [blame] | 120 | |
Denys Vlasenko | 9dca6ac | 2015-01-03 16:09:05 +0100 | [diff] [blame] | 121 | #if ENABLE_FEATURE_CLEAN_UP |
| 122 | static void free_static(void) |
| 123 | { |
Denys Vlasenko | 6390a3a | 2015-10-13 01:51:37 +0200 | [diff] [blame] | 124 | free(S.db[0].malloced); |
Denys Vlasenko | 9dca6ac | 2015-01-03 16:09:05 +0100 | [diff] [blame] | 125 | free(S.db[1].malloced); |
| 126 | # if ENABLE_USE_BB_SHADOW |
Denys Vlasenko | 20c0a16 | 2015-01-03 19:12:49 +0100 | [diff] [blame] | 127 | free(S.db[2].malloced); |
Denys Vlasenko | 9dca6ac | 2015-01-03 16:09:05 +0100 | [diff] [blame] | 128 | # endif |
| 129 | free(ptr_to_statics); |
| 130 | } |
| 131 | #endif |
| 132 | |
Denis Vlasenko | 2c91efb | 2007-06-18 10:08:27 +0000 | [diff] [blame] | 133 | static struct statics *get_S(void) |
| 134 | { |
Tito Ragusa | 1da09cf | 2015-01-02 21:37:59 +0100 | [diff] [blame] | 135 | if (!ptr_to_statics) { |
| 136 | ptr_to_statics = xzalloc(sizeof(S)); |
| 137 | memcpy(&S.db[0], &const_pw_db, sizeof(const_pw_db)); |
| 138 | memcpy(&S.db[1], &const_gr_db, sizeof(const_gr_db)); |
| 139 | #if ENABLE_USE_BB_SHADOW |
| 140 | memcpy(&S.db[2], &const_sp_db, sizeof(const_sp_db)); |
| 141 | #endif |
Denys Vlasenko | 9dca6ac | 2015-01-03 16:09:05 +0100 | [diff] [blame] | 142 | #if ENABLE_FEATURE_CLEAN_UP |
| 143 | atexit(free_static); |
| 144 | #endif |
Tito Ragusa | 1da09cf | 2015-01-02 21:37:59 +0100 | [diff] [blame] | 145 | } |
Denis Vlasenko | 2c91efb | 2007-06-18 10:08:27 +0000 | [diff] [blame] | 146 | return ptr_to_statics; |
| 147 | } |
| 148 | |
Denys Vlasenko | 31d6734 | 2015-01-03 15:15:47 +0100 | [diff] [blame] | 149 | /* Internal functions */ |
Tito Ragusa | 1da09cf | 2015-01-02 21:37:59 +0100 | [diff] [blame] | 150 | |
| 151 | /* Divide the passwd/group/shadow record in fields |
Denys Vlasenko | 10ad622 | 2017-04-17 16:13:32 +0200 | [diff] [blame] | 152 | * by substituting the given delimiter |
Tito Ragusa | 1da09cf | 2015-01-02 21:37:59 +0100 | [diff] [blame] | 153 | * e.g. ':' or ',' with '\0'. |
Denys Vlasenko | 31d6734 | 2015-01-03 15:15:47 +0100 | [diff] [blame] | 154 | * Returns the number of fields found. |
Tito Ragusa | 1da09cf | 2015-01-02 21:37:59 +0100 | [diff] [blame] | 155 | * Strips leading and trailing whitespace in fields. |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 156 | */ |
Tito Ragusa | 1da09cf | 2015-01-02 21:37:59 +0100 | [diff] [blame] | 157 | static int tokenize(char *buffer, int ch) |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 158 | { |
Tito Ragusa | 1da09cf | 2015-01-02 21:37:59 +0100 | [diff] [blame] | 159 | char *p = buffer; |
| 160 | char *s = p; |
| 161 | int num_fields = 0; |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 162 | |
Tito Ragusa | 1da09cf | 2015-01-02 21:37:59 +0100 | [diff] [blame] | 163 | for (;;) { |
| 164 | if (isblank(*s)) { |
| 165 | overlapping_strcpy(s, skip_whitespace(s)); |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 166 | } |
Tito Ragusa | 1da09cf | 2015-01-02 21:37:59 +0100 | [diff] [blame] | 167 | if (*p == ch || *p == '\0') { |
| 168 | char *end = p; |
| 169 | while (p != s && isblank(p[-1])) |
| 170 | p--; |
| 171 | if (p != end) |
| 172 | overlapping_strcpy(p, end); |
| 173 | num_fields++; |
| 174 | if (*end == '\0') { |
| 175 | S.tokenize_end = p + 1; |
| 176 | return num_fields; |
| 177 | } |
| 178 | *p = '\0'; |
| 179 | s = p + 1; |
| 180 | } |
| 181 | p++; |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 182 | } |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 183 | } |
| 184 | |
Tito Ragusa | 1da09cf | 2015-01-02 21:37:59 +0100 | [diff] [blame] | 185 | /* Returns !NULL on success and matching line broken up in fields by '\0' in buf. |
| 186 | * We require the expected number of fields to be found. |
| 187 | */ |
Denys Vlasenko | 5acf134 | 2015-01-04 02:02:39 +0100 | [diff] [blame] | 188 | static char *parse_common(FILE *fp, struct passdb *db, |
Tito Ragusa | 1da09cf | 2015-01-02 21:37:59 +0100 | [diff] [blame] | 189 | const char *key, int field_pos) |
| 190 | { |
Tito Ragusa | 1da09cf | 2015-01-02 21:37:59 +0100 | [diff] [blame] | 191 | char *buf; |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 192 | |
Tito Ragusa | 1da09cf | 2015-01-02 21:37:59 +0100 | [diff] [blame] | 193 | while ((buf = xmalloc_fgetline(fp)) != NULL) { |
Denys Vlasenko | 0cdd6f5 | 2022-04-27 15:29:57 +0200 | [diff] [blame] | 194 | int n; |
| 195 | char *field; |
| 196 | |
Tito Ragusa | 1da09cf | 2015-01-02 21:37:59 +0100 | [diff] [blame] | 197 | /* Skip empty lines, comment lines */ |
| 198 | if (buf[0] == '\0' || buf[0] == '#') |
| 199 | goto free_and_next; |
Denys Vlasenko | 5acf134 | 2015-01-04 02:02:39 +0100 | [diff] [blame] | 200 | if (tokenize(buf, ':') != db->numfields) { |
Tito Ragusa | 1da09cf | 2015-01-02 21:37:59 +0100 | [diff] [blame] | 201 | /* number of fields is wrong */ |
Tito Ragusa | e5cae08 | 2015-01-06 01:22:36 +0100 | [diff] [blame] | 202 | bb_error_msg("%s: bad record", db->filename); |
Tito Ragusa | 1da09cf | 2015-01-02 21:37:59 +0100 | [diff] [blame] | 203 | goto free_and_next; |
| 204 | } |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 205 | |
Denys Vlasenko | c5d4a04 | 2015-01-05 15:09:04 +0100 | [diff] [blame] | 206 | if (field_pos == -1) { |
Tito Ragusa | 1da09cf | 2015-01-02 21:37:59 +0100 | [diff] [blame] | 207 | /* no key specified: sequential read, return a record */ |
| 208 | break; |
| 209 | } |
Denys Vlasenko | 0cdd6f5 | 2022-04-27 15:29:57 +0200 | [diff] [blame] | 210 | /* Can't use nth_string() here, it does not allow empty strings |
| 211 | * ("\0\0" terminates the list), and a valid passwd entry |
| 212 | * "user::UID:GID..." would be mishandled */ |
| 213 | n = field_pos; |
| 214 | field = buf; |
| 215 | while (n) { |
| 216 | n--; |
| 217 | field += strlen(field) + 1; |
| 218 | } |
| 219 | if (strcmp(key, field) == 0) { |
Tito Ragusa | 1da09cf | 2015-01-02 21:37:59 +0100 | [diff] [blame] | 220 | /* record found */ |
| 221 | break; |
| 222 | } |
| 223 | free_and_next: |
| 224 | free(buf); |
| 225 | } |
| 226 | |
Denys Vlasenko | f993667 | 2015-01-03 21:03:39 +0100 | [diff] [blame] | 227 | S.string_size = S.tokenize_end - buf; |
| 228 | /* |
| 229 | * Ugly hack: group db requires additional buffer space |
| 230 | * for members[] array. If there is only one group, we need space |
| 231 | * for 3 pointers: alignment padding, group name, NULL. |
| 232 | * +1 for every additional group. |
| 233 | */ |
Denys Vlasenko | 5acf134 | 2015-01-04 02:02:39 +0100 | [diff] [blame] | 234 | if (buf && db->numfields == sizeof(GR_DEF)-1) { /* if we read group file... */ |
Denys Vlasenko | f993667 | 2015-01-03 21:03:39 +0100 | [diff] [blame] | 235 | int cnt = 3; |
| 236 | char *p = buf; |
| 237 | while (p < S.tokenize_end) |
| 238 | if (*p++ == ',') |
| 239 | cnt++; |
| 240 | S.string_size += cnt * sizeof(char*); |
| 241 | //bb_error_msg("+%d words = %u key:%s buf:'%s'", cnt, S.string_size, key, buf); |
| 242 | buf = xrealloc(buf, S.string_size); |
| 243 | } |
| 244 | |
Tito Ragusa | 1da09cf | 2015-01-02 21:37:59 +0100 | [diff] [blame] | 245 | return buf; |
| 246 | } |
| 247 | |
Denys Vlasenko | 5acf134 | 2015-01-04 02:02:39 +0100 | [diff] [blame] | 248 | static char *parse_file(struct passdb *db, |
Tito Ragusa | 1da09cf | 2015-01-02 21:37:59 +0100 | [diff] [blame] | 249 | const char *key, int field_pos) |
| 250 | { |
| 251 | char *buf = NULL; |
Denys Vlasenko | 5acf134 | 2015-01-04 02:02:39 +0100 | [diff] [blame] | 252 | FILE *fp = fopen_for_read(db->filename); |
Tito Ragusa | 1da09cf | 2015-01-02 21:37:59 +0100 | [diff] [blame] | 253 | |
| 254 | if (fp) { |
Denys Vlasenko | 5acf134 | 2015-01-04 02:02:39 +0100 | [diff] [blame] | 255 | buf = parse_common(fp, db, key, field_pos); |
Tito Ragusa | 1da09cf | 2015-01-02 21:37:59 +0100 | [diff] [blame] | 256 | fclose(fp); |
| 257 | } |
| 258 | return buf; |
| 259 | } |
| 260 | |
| 261 | /* Convert passwd/group/shadow file record in buffer to a struct */ |
Denys Vlasenko | 8d547ac | 2015-01-03 15:54:04 +0100 | [diff] [blame] | 262 | static void *convert_to_struct(struct passdb *db, |
Tito Ragusa | 1da09cf | 2015-01-02 21:37:59 +0100 | [diff] [blame] | 263 | char *buffer, void *result) |
| 264 | { |
Denys Vlasenko | 8d547ac | 2015-01-03 15:54:04 +0100 | [diff] [blame] | 265 | const char *def = db->def; |
| 266 | const uint8_t *off = db->off; |
| 267 | |
Denys Vlasenko | 134c530 | 2015-01-03 20:47:47 +0100 | [diff] [blame] | 268 | /* For consistency, zero out all fields */ |
| 269 | memset(result, 0, db->size_of); |
Denys Vlasenko | 8d547ac | 2015-01-03 15:54:04 +0100 | [diff] [blame] | 270 | |
Tito Ragusa | 1da09cf | 2015-01-02 21:37:59 +0100 | [diff] [blame] | 271 | for (;;) { |
| 272 | void *member = (char*)result + (*off++); |
| 273 | |
| 274 | if ((*def | 0x20) == 's') { /* s or S */ |
| 275 | *(char **)member = (char*)buffer; |
| 276 | if (!buffer[0] && (*def == 'S')) { |
| 277 | errno = EINVAL; |
| 278 | } |
| 279 | } |
| 280 | if (*def == 'I') { |
| 281 | *(int *)member = bb_strtou(buffer, NULL, 10); |
| 282 | } |
Denis Vlasenko | 7fa0fca | 2006-12-28 21:33:30 +0000 | [diff] [blame] | 283 | #if ENABLE_USE_BB_SHADOW |
Tito Ragusa | 1da09cf | 2015-01-02 21:37:59 +0100 | [diff] [blame] | 284 | if (*def == 'l') { |
| 285 | long n = -1; |
| 286 | if (buffer[0]) |
| 287 | n = bb_strtol(buffer, NULL, 10); |
| 288 | *(long *)member = n; |
| 289 | } |
Denis Vlasenko | 7fa0fca | 2006-12-28 21:33:30 +0000 | [diff] [blame] | 290 | #endif |
Tito Ragusa | 1da09cf | 2015-01-02 21:37:59 +0100 | [diff] [blame] | 291 | if (*def == 'm') { |
| 292 | char **members; |
| 293 | int i = tokenize(buffer, ','); |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 294 | |
Tito Ragusa | 1da09cf | 2015-01-02 21:37:59 +0100 | [diff] [blame] | 295 | /* Store members[] after buffer's end. |
| 296 | * This is safe ONLY because there is a hack |
| 297 | * in parse_common() which allocates additional space |
| 298 | * at the end of malloced buffer! |
| 299 | */ |
| 300 | members = (char **) |
Denys Vlasenko | 20c0a16 | 2015-01-03 19:12:49 +0100 | [diff] [blame] | 301 | ( ((intptr_t)S.tokenize_end + sizeof(members[0])) |
| 302 | & -(intptr_t)sizeof(members[0]) |
Tito Ragusa | 1da09cf | 2015-01-02 21:37:59 +0100 | [diff] [blame] | 303 | ); |
Tito Ragusa | 1da09cf | 2015-01-02 21:37:59 +0100 | [diff] [blame] | 304 | ((struct group *)result)->gr_mem = members; |
| 305 | while (--i >= 0) { |
Denys Vlasenko | 12fc869 | 2015-01-03 21:16:18 +0100 | [diff] [blame] | 306 | if (buffer[0]) { |
| 307 | *members++ = buffer; |
| 308 | // bb_error_msg("member[]='%s'", buffer); |
| 309 | } |
Tito Ragusa | 1da09cf | 2015-01-02 21:37:59 +0100 | [diff] [blame] | 310 | buffer += strlen(buffer) + 1; |
| 311 | } |
| 312 | *members = NULL; |
| 313 | } |
| 314 | /* def "r" does nothing */ |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 315 | |
Tito Ragusa | 1da09cf | 2015-01-02 21:37:59 +0100 | [diff] [blame] | 316 | def++; |
Denys Vlasenko | 134c530 | 2015-01-03 20:47:47 +0100 | [diff] [blame] | 317 | if ((unsigned char)*def <= (unsigned char)' ') |
Tito Ragusa | 1da09cf | 2015-01-02 21:37:59 +0100 | [diff] [blame] | 318 | break; |
| 319 | buffer += strlen(buffer) + 1; |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 320 | } |
| 321 | |
Tito Ragusa | 1da09cf | 2015-01-02 21:37:59 +0100 | [diff] [blame] | 322 | if (errno) |
| 323 | result = NULL; |
| 324 | return result; |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 325 | } |
| 326 | |
Denys Vlasenko | db4d105 | 2015-01-04 02:34:52 +0100 | [diff] [blame] | 327 | static int massage_data_for_r_func(struct passdb *db, |
Denys Vlasenko | 8d547ac | 2015-01-03 15:54:04 +0100 | [diff] [blame] | 328 | char *buffer, size_t buflen, |
Denys Vlasenko | db4d105 | 2015-01-04 02:34:52 +0100 | [diff] [blame] | 329 | void **result, |
| 330 | char *buf) |
Tito Ragusa | 1da09cf | 2015-01-02 21:37:59 +0100 | [diff] [blame] | 331 | { |
Denys Vlasenko | db4d105 | 2015-01-04 02:34:52 +0100 | [diff] [blame] | 332 | void *result_buf = *result; |
| 333 | *result = NULL; |
Tito Ragusa | 1da09cf | 2015-01-02 21:37:59 +0100 | [diff] [blame] | 334 | if (buf) { |
Denys Vlasenko | db4d105 | 2015-01-04 02:34:52 +0100 | [diff] [blame] | 335 | if (S.string_size > buflen) { |
Tito Ragusa | 1da09cf | 2015-01-02 21:37:59 +0100 | [diff] [blame] | 336 | errno = ERANGE; |
| 337 | } else { |
Denys Vlasenko | db4d105 | 2015-01-04 02:34:52 +0100 | [diff] [blame] | 338 | memcpy(buffer, buf, S.string_size); |
| 339 | *result = convert_to_struct(db, buffer, result_buf); |
Tito Ragusa | 1da09cf | 2015-01-02 21:37:59 +0100 | [diff] [blame] | 340 | } |
| 341 | free(buf); |
| 342 | } |
| 343 | /* "The reentrant functions return zero on success. |
| 344 | * In case of error, an error number is returned." |
| 345 | * NB: not finding the record is also a "success" here: |
| 346 | */ |
| 347 | return errno; |
| 348 | } |
| 349 | |
Denys Vlasenko | 23cfaab | 2015-02-07 21:21:02 +0100 | [diff] [blame] | 350 | static void* massage_data_for_non_r_func(struct passdb *db, char *buf) |
| 351 | { |
| 352 | if (!buf) |
| 353 | return NULL; |
| 354 | |
| 355 | free(db->malloced); |
| 356 | /* We enlarge buf and move string data up, freeing space |
| 357 | * for struct passwd/group/spwd at the beginning. This way, |
| 358 | * entire result of getXXnam is in a single malloced block. |
| 359 | * This enables easy creation of xmalloc_getpwnam() API. |
| 360 | */ |
| 361 | db->malloced = buf = xrealloc(buf, db->size_of + S.string_size); |
| 362 | memmove(buf + db->size_of, buf, S.string_size); |
| 363 | return convert_to_struct(db, buf + db->size_of, buf); |
| 364 | } |
| 365 | |
Denys Vlasenko | db4d105 | 2015-01-04 02:34:52 +0100 | [diff] [blame] | 366 | /****** getXXnam/id_r */ |
| 367 | |
| 368 | static int FAST_FUNC getXXnam_r(const char *name, uintptr_t db_and_field_pos, |
| 369 | char *buffer, size_t buflen, |
| 370 | void *result) |
| 371 | { |
| 372 | char *buf; |
| 373 | struct passdb *db = &get_S()->db[db_and_field_pos >> 2]; |
| 374 | |
| 375 | buf = parse_file(db, name, 0 /*db_and_field_pos & 3*/); |
| 376 | /* "db_and_field_pos & 3" is commented out since so far we don't implement |
| 377 | * getXXXid_r() functions which would use that to pass 2 here */ |
| 378 | |
| 379 | return massage_data_for_r_func(db, buffer, buflen, result, buf); |
| 380 | } |
| 381 | |
Denys Vlasenko | 8d547ac | 2015-01-03 15:54:04 +0100 | [diff] [blame] | 382 | int FAST_FUNC getpwnam_r(const char *name, struct passwd *struct_buf, |
| 383 | char *buffer, size_t buflen, |
| 384 | struct passwd **result) |
Tito Ragusa | 1da09cf | 2015-01-02 21:37:59 +0100 | [diff] [blame] | 385 | { |
| 386 | /* Why the "store buffer address in result" trick? |
| 387 | * This way, getXXnam_r has the same ABI signature as getpwnam_r, |
Denys Vlasenko | 908b6e5 | 2015-01-02 22:31:07 +0100 | [diff] [blame] | 388 | * hopefully compiler can optimize tail call better in this case. |
Tito Ragusa | 1da09cf | 2015-01-02 21:37:59 +0100 | [diff] [blame] | 389 | */ |
| 390 | *result = struct_buf; |
| 391 | return getXXnam_r(name, (0 << 2) + 0, buffer, buflen, result); |
| 392 | } |
| 393 | #if ENABLE_USE_BB_SHADOW |
Denys Vlasenko | 908b6e5 | 2015-01-02 22:31:07 +0100 | [diff] [blame] | 394 | int FAST_FUNC getspnam_r(const char *name, struct spwd *struct_buf, char *buffer, size_t buflen, |
Denys Vlasenko | 31d6734 | 2015-01-03 15:15:47 +0100 | [diff] [blame] | 395 | struct spwd **result) |
Tito Ragusa | 1da09cf | 2015-01-02 21:37:59 +0100 | [diff] [blame] | 396 | { |
| 397 | *result = struct_buf; |
| 398 | return getXXnam_r(name, (2 << 2) + 0, buffer, buflen, result); |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 399 | } |
Denis Vlasenko | 7fa0fca | 2006-12-28 21:33:30 +0000 | [diff] [blame] | 400 | #endif |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 401 | |
Denys Vlasenko | 23cfaab | 2015-02-07 21:21:02 +0100 | [diff] [blame] | 402 | #ifdef UNUSED |
Tito Ragusa | 1da09cf | 2015-01-02 21:37:59 +0100 | [diff] [blame] | 403 | /****** getXXent_r */ |
| 404 | |
Denys Vlasenko | db4d105 | 2015-01-04 02:34:52 +0100 | [diff] [blame] | 405 | static int FAST_FUNC getXXent_r(uintptr_t db_idx, char *buffer, size_t buflen, |
| 406 | void *result) |
Tito Ragusa | 1da09cf | 2015-01-02 21:37:59 +0100 | [diff] [blame] | 407 | { |
| 408 | char *buf; |
Denys Vlasenko | 5acf134 | 2015-01-04 02:02:39 +0100 | [diff] [blame] | 409 | struct passdb *db = &get_S()->db[db_idx]; |
Tito Ragusa | 1da09cf | 2015-01-02 21:37:59 +0100 | [diff] [blame] | 410 | |
Tito Ragusa | 1da09cf | 2015-01-02 21:37:59 +0100 | [diff] [blame] | 411 | if (!db->fp) { |
| 412 | db->fp = fopen_for_read(db->filename); |
| 413 | if (!db->fp) { |
| 414 | return errno; |
| 415 | } |
| 416 | close_on_exec_on(fileno(db->fp)); |
| 417 | } |
| 418 | |
Denys Vlasenko | c5d4a04 | 2015-01-05 15:09:04 +0100 | [diff] [blame] | 419 | buf = parse_common(db->fp, db, /*no search key:*/ NULL, -1); |
| 420 | if (!buf && !errno) |
| 421 | errno = ENOENT; |
Denys Vlasenko | db4d105 | 2015-01-04 02:34:52 +0100 | [diff] [blame] | 422 | return massage_data_for_r_func(db, buffer, buflen, result, buf); |
Tito Ragusa | 1da09cf | 2015-01-02 21:37:59 +0100 | [diff] [blame] | 423 | } |
| 424 | |
Denys Vlasenko | db4d105 | 2015-01-04 02:34:52 +0100 | [diff] [blame] | 425 | int FAST_FUNC getpwent_r(struct passwd *struct_buf, char *buffer, size_t buflen, |
| 426 | struct passwd **result) |
Tito Ragusa | 1da09cf | 2015-01-02 21:37:59 +0100 | [diff] [blame] | 427 | { |
Denys Vlasenko | db4d105 | 2015-01-04 02:34:52 +0100 | [diff] [blame] | 428 | *result = struct_buf; |
| 429 | return getXXent_r(0, buffer, buflen, result); |
Tito Ragusa | 1da09cf | 2015-01-02 21:37:59 +0100 | [diff] [blame] | 430 | } |
Denys Vlasenko | 23cfaab | 2015-02-07 21:21:02 +0100 | [diff] [blame] | 431 | #endif |
| 432 | |
| 433 | /****** getXXent */ |
| 434 | |
| 435 | static void* FAST_FUNC getXXent(uintptr_t db_idx) |
| 436 | { |
| 437 | char *buf; |
| 438 | struct passdb *db = &get_S()->db[db_idx]; |
| 439 | |
| 440 | if (!db->fp) { |
| 441 | db->fp = fopen_for_read(db->filename); |
| 442 | if (!db->fp) { |
| 443 | return NULL; |
| 444 | } |
| 445 | close_on_exec_on(fileno(db->fp)); |
| 446 | } |
| 447 | |
| 448 | buf = parse_common(db->fp, db, /*no search key:*/ NULL, -1); |
| 449 | return massage_data_for_non_r_func(db, buf); |
| 450 | } |
| 451 | |
| 452 | struct passwd* FAST_FUNC getpwent(void) |
| 453 | { |
| 454 | return getXXent(0); |
| 455 | } |
Tito Ragusa | 1da09cf | 2015-01-02 21:37:59 +0100 | [diff] [blame] | 456 | |
| 457 | /****** getXXnam/id */ |
| 458 | |
Denys Vlasenko | 908b6e5 | 2015-01-02 22:31:07 +0100 | [diff] [blame] | 459 | static void* FAST_FUNC getXXnam(const char *name, unsigned db_and_field_pos) |
Tito Ragusa | 1da09cf | 2015-01-02 21:37:59 +0100 | [diff] [blame] | 460 | { |
| 461 | char *buf; |
Denys Vlasenko | 5acf134 | 2015-01-04 02:02:39 +0100 | [diff] [blame] | 462 | struct passdb *db = &get_S()->db[db_and_field_pos >> 2]; |
Tito Ragusa | 1da09cf | 2015-01-02 21:37:59 +0100 | [diff] [blame] | 463 | |
Tito Ragusa | cb6a112 | 2015-02-19 22:02:59 +0100 | [diff] [blame] | 464 | buf = parse_file(db, name, db_and_field_pos & 3); |
Denys Vlasenko | 23cfaab | 2015-02-07 21:21:02 +0100 | [diff] [blame] | 465 | return massage_data_for_non_r_func(db, buf); |
Tito Ragusa | 1da09cf | 2015-01-02 21:37:59 +0100 | [diff] [blame] | 466 | } |
| 467 | |
Denys Vlasenko | 908b6e5 | 2015-01-02 22:31:07 +0100 | [diff] [blame] | 468 | struct passwd* FAST_FUNC getpwnam(const char *name) |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 469 | { |
Tito Ragusa | 1da09cf | 2015-01-02 21:37:59 +0100 | [diff] [blame] | 470 | return getXXnam(name, (0 << 2) + 0); |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 471 | } |
Denys Vlasenko | 908b6e5 | 2015-01-02 22:31:07 +0100 | [diff] [blame] | 472 | struct group* FAST_FUNC getgrnam(const char *name) |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 473 | { |
Tito Ragusa | 1da09cf | 2015-01-02 21:37:59 +0100 | [diff] [blame] | 474 | return getXXnam(name, (1 << 2) + 0); |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 475 | } |
Denys Vlasenko | 908b6e5 | 2015-01-02 22:31:07 +0100 | [diff] [blame] | 476 | struct passwd* FAST_FUNC getpwuid(uid_t id) |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 477 | { |
Tito Ragusa | 1da09cf | 2015-01-02 21:37:59 +0100 | [diff] [blame] | 478 | return getXXnam(utoa(id), (0 << 2) + 2); |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 479 | } |
Denys Vlasenko | 908b6e5 | 2015-01-02 22:31:07 +0100 | [diff] [blame] | 480 | struct group* FAST_FUNC getgrgid(gid_t id) |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 481 | { |
Tito Ragusa | 1da09cf | 2015-01-02 21:37:59 +0100 | [diff] [blame] | 482 | return getXXnam(utoa(id), (1 << 2) + 2); |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 483 | } |
| 484 | |
Tito Ragusa | 1da09cf | 2015-01-02 21:37:59 +0100 | [diff] [blame] | 485 | /****** end/setXXend */ |
| 486 | |
Denys Vlasenko | 908b6e5 | 2015-01-02 22:31:07 +0100 | [diff] [blame] | 487 | void FAST_FUNC endpwent(void) |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 488 | { |
Tito Ragusa | 1da09cf | 2015-01-02 21:37:59 +0100 | [diff] [blame] | 489 | if (has_S && S.db[0].fp) { |
| 490 | fclose(S.db[0].fp); |
| 491 | S.db[0].fp = NULL; |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 492 | } |
| 493 | } |
Denys Vlasenko | 908b6e5 | 2015-01-02 22:31:07 +0100 | [diff] [blame] | 494 | void FAST_FUNC setpwent(void) |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 495 | { |
Tito Ragusa | 1da09cf | 2015-01-02 21:37:59 +0100 | [diff] [blame] | 496 | if (has_S && S.db[0].fp) { |
| 497 | rewind(S.db[0].fp); |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 498 | } |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 499 | } |
Denys Vlasenko | 908b6e5 | 2015-01-02 22:31:07 +0100 | [diff] [blame] | 500 | void FAST_FUNC endgrent(void) |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 501 | { |
Tito Ragusa | 1da09cf | 2015-01-02 21:37:59 +0100 | [diff] [blame] | 502 | if (has_S && S.db[1].fp) { |
| 503 | fclose(S.db[1].fp); |
| 504 | S.db[1].fp = NULL; |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 505 | } |
| 506 | } |
| 507 | |
Tito Ragusa | 1da09cf | 2015-01-02 21:37:59 +0100 | [diff] [blame] | 508 | /****** initgroups and getgrouplist */ |
| 509 | |
| 510 | static gid_t* FAST_FUNC getgrouplist_internal(int *ngroups_ptr, |
| 511 | const char *user, gid_t gid) |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 512 | { |
Tito Ragusa | 1da09cf | 2015-01-02 21:37:59 +0100 | [diff] [blame] | 513 | FILE *fp; |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 514 | gid_t *group_list; |
Denis Vlasenko | 2228426 | 2008-09-18 00:56:24 +0000 | [diff] [blame] | 515 | int ngroups; |
Tito Ragusa | 1da09cf | 2015-01-02 21:37:59 +0100 | [diff] [blame] | 516 | |
Denis Vlasenko | 2228426 | 2008-09-18 00:56:24 +0000 | [diff] [blame] | 517 | /* We alloc space for 8 gids at a time. */ |
Denys Vlasenko | 31d6734 | 2015-01-03 15:15:47 +0100 | [diff] [blame] | 518 | group_list = xzalloc(8 * sizeof(group_list[0])); |
Denis Vlasenko | 2228426 | 2008-09-18 00:56:24 +0000 | [diff] [blame] | 519 | group_list[0] = gid; |
| 520 | ngroups = 1; |
| 521 | |
Tito Ragusa | 1da09cf | 2015-01-02 21:37:59 +0100 | [diff] [blame] | 522 | fp = fopen_for_read(_PATH_GROUP); |
| 523 | if (fp) { |
Denys Vlasenko | 5acf134 | 2015-01-04 02:02:39 +0100 | [diff] [blame] | 524 | struct passdb *db = &get_S()->db[1]; |
Tito Ragusa | 1da09cf | 2015-01-02 21:37:59 +0100 | [diff] [blame] | 525 | char *buf; |
Denys Vlasenko | c5d4a04 | 2015-01-05 15:09:04 +0100 | [diff] [blame] | 526 | while ((buf = parse_common(fp, db, NULL, -1)) != NULL) { |
Denis Vlasenko | 2228426 | 2008-09-18 00:56:24 +0000 | [diff] [blame] | 527 | char **m; |
Tito Ragusa | 1da09cf | 2015-01-02 21:37:59 +0100 | [diff] [blame] | 528 | struct group group; |
Denys Vlasenko | 5acf134 | 2015-01-04 02:02:39 +0100 | [diff] [blame] | 529 | if (!convert_to_struct(db, buf, &group)) |
Tito Ragusa | 1da09cf | 2015-01-02 21:37:59 +0100 | [diff] [blame] | 530 | goto next; |
Denis Vlasenko | 2228426 | 2008-09-18 00:56:24 +0000 | [diff] [blame] | 531 | if (group.gr_gid == gid) |
Tito Ragusa | 1da09cf | 2015-01-02 21:37:59 +0100 | [diff] [blame] | 532 | goto next; |
Denis Vlasenko | 2228426 | 2008-09-18 00:56:24 +0000 | [diff] [blame] | 533 | for (m = group.gr_mem; *m; m++) { |
| 534 | if (strcmp(*m, user) != 0) |
| 535 | continue; |
Denys Vlasenko | 17fcd72 | 2010-03-31 12:37:43 +0200 | [diff] [blame] | 536 | group_list = xrealloc_vector(group_list, /*8=2^3:*/ 3, ngroups); |
Denis Vlasenko | 2228426 | 2008-09-18 00:56:24 +0000 | [diff] [blame] | 537 | group_list[ngroups++] = group.gr_gid; |
Denys Vlasenko | c5d4a04 | 2015-01-05 15:09:04 +0100 | [diff] [blame] | 538 | goto next; |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 539 | } |
Tito Ragusa | 1da09cf | 2015-01-02 21:37:59 +0100 | [diff] [blame] | 540 | next: |
| 541 | free(buf); |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 542 | } |
Tito Ragusa | 1da09cf | 2015-01-02 21:37:59 +0100 | [diff] [blame] | 543 | fclose(fp); |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 544 | } |
Denis Vlasenko | 2228426 | 2008-09-18 00:56:24 +0000 | [diff] [blame] | 545 | *ngroups_ptr = ngroups; |
| 546 | return group_list; |
| 547 | } |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 548 | |
Denys Vlasenko | 908b6e5 | 2015-01-02 22:31:07 +0100 | [diff] [blame] | 549 | int FAST_FUNC initgroups(const char *user, gid_t gid) |
Denis Vlasenko | 2228426 | 2008-09-18 00:56:24 +0000 | [diff] [blame] | 550 | { |
| 551 | int ngroups; |
| 552 | gid_t *group_list = getgrouplist_internal(&ngroups, user, gid); |
| 553 | |
Denis Vlasenko | 2228426 | 2008-09-18 00:56:24 +0000 | [diff] [blame] | 554 | ngroups = setgroups(ngroups, group_list); |
| 555 | free(group_list); |
| 556 | return ngroups; |
| 557 | } |
| 558 | |
Denys Vlasenko | 908b6e5 | 2015-01-02 22:31:07 +0100 | [diff] [blame] | 559 | int FAST_FUNC getgrouplist(const char *user, gid_t gid, gid_t *groups, int *ngroups) |
Denis Vlasenko | 2228426 | 2008-09-18 00:56:24 +0000 | [diff] [blame] | 560 | { |
| 561 | int ngroups_old = *ngroups; |
| 562 | gid_t *group_list = getgrouplist_internal(ngroups, user, gid); |
| 563 | |
| 564 | if (*ngroups <= ngroups_old) { |
| 565 | ngroups_old = *ngroups; |
| 566 | memcpy(groups, group_list, ngroups_old * sizeof(groups[0])); |
| 567 | } else { |
| 568 | ngroups_old = -1; |
| 569 | } |
| 570 | free(group_list); |
| 571 | return ngroups_old; |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 572 | } |