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