blob: 2392e7d2259fcf806e8cc026cccdeb9f0e89a0a2 [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Denys Vlasenko31d67342015-01-03 15:15:47 +01002/* Copyright (C) 2014 Tito Ragusa <farmatito@tiscali.it>
Eric Andersen9615a082004-07-15 12:53:49 +00003 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02004 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Eric Andersen9615a082004-07-15 12:53:49 +00005 */
Tito Ragusa1da09cf2015-01-02 21:37:59 +01006/* This program is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY!!
Eric Andersen9615a082004-07-15 12:53:49 +00008 *
Tito Ragusa1da09cf2015-01-02 21:37:59 +01009 * Rewrite of some parts. Main differences are:
Eric Andersen9615a082004-07-15 12:53:49 +000010 *
Tito Ragusa1da09cf2015-01-02 21:37:59 +010011 * 1) the buffer for getpwuid, getgrgid, getpwnam, getgrnam is dynamically
Denys Vlasenko31d67342015-01-03 15:15:47 +010012 * allocated and reused by later calls.
Tito Ragusa1da09cf2015-01-02 21:37:59 +010013 * 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 Vlasenko31d67342015-01-03 15:15:47 +010018 * b) leading or trailing whitespace in fields is stripped.
Tito Ragusa1da09cf2015-01-02 21:37:59 +010019 * 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 Vlasenko31d67342015-01-03 15:15:47 +010025 * functions, or errno is set to EINVAL.
26 * e) leading or trailing whitespace in group member names are stripped.
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 Ragusa1da09cf2015-01-02 21:37:59 +010029 * implemented, if you need a particular missing function it should be
30 * easy to write it by using the internal common code.
Eric Andersen9615a082004-07-15 12:53:49 +000031 */
32
Rob Landleyea224be2006-06-18 20:20:07 +000033#include "libbb.h"
Bernhard Reutner-Fischerfa939aa2006-04-05 16:21:37 +000034
Denys Vlasenko8d547ac2015-01-03 15:54:04 +010035struct const_passdb {
36 const char *filename;
Denys Vlasenko134c5302015-01-03 20:47:47 +010037 const char def[7 + 2*ENABLE_USE_BB_SHADOW];
38 const uint8_t off[7 + 2*ENABLE_USE_BB_SHADOW];
Denys Vlasenko8d547ac2015-01-03 15:54:04 +010039 uint8_t numfields;
Denys Vlasenko134c5302015-01-03 20:47:47 +010040 uint8_t size_of;
Denys Vlasenko8d547ac2015-01-03 15:54:04 +010041};
42struct passdb {
43 const char *filename;
Denys Vlasenko134c5302015-01-03 20:47:47 +010044 const char def[7 + 2*ENABLE_USE_BB_SHADOW];
45 const uint8_t off[7 + 2*ENABLE_USE_BB_SHADOW];
Denys Vlasenko8d547ac2015-01-03 15:54:04 +010046 uint8_t numfields;
Denys Vlasenko134c5302015-01-03 20:47:47 +010047 uint8_t size_of;
Denys Vlasenko8d547ac2015-01-03 15:54:04 +010048 FILE *fp;
49 char *malloced;
Denys Vlasenko8d547ac2015-01-03 15:54:04 +010050};
Denys Vlasenko134c5302015-01-03 20:47:47 +010051/* Note: for shadow db, def[] will not contain terminating NUL,
Denys Vlasenko20c0a162015-01-03 19:12:49 +010052 * 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 Vlasenko8d547ac2015-01-03 15:54:04 +010056
Denys Vlasenko31d67342015-01-03 15:15:47 +010057/* S = string not empty, s = string maybe empty,
58 * I = uid,gid, l = long maybe empty, m = members,
59 * r = reserved
60 */
Tito Ragusa1da09cf2015-01-02 21:37:59 +010061#define PW_DEF "SsIIsSS"
62#define GR_DEF "SsIm"
63#define SP_DEF "Ssllllllr"
Eric Andersen9615a082004-07-15 12:53:49 +000064
Denys Vlasenko8d547ac2015-01-03 15:54:04 +010065static 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 Vlasenko134c5302015-01-03 20:47:47 +010076 sizeof(PW_DEF)-1, sizeof(struct passwd)
Tito Ragusa1da09cf2015-01-02 21:37:59 +010077};
Denys Vlasenko8d547ac2015-01-03 15:54:04 +010078static 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 Vlasenko134c5302015-01-03 20:47:47 +010086 sizeof(GR_DEF)-1, sizeof(struct group)
Tito Ragusa1da09cf2015-01-02 21:37:59 +010087};
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +000088#if ENABLE_USE_BB_SHADOW
Denys Vlasenko8d547ac2015-01-03 15:54:04 +010089static 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 Vlasenko134c5302015-01-03 20:47:47 +0100102 sizeof(SP_DEF)-1, sizeof(struct spwd)
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100103};
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000104#endif
105
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100106/* We avoid having big global data. */
Denis Vlasenko2c91efb2007-06-18 10:08:27 +0000107struct statics {
Denys Vlasenko134c5302015-01-03 20:47:47 +0100108 /* It's ok to use same buffer (db[0].malloced) for getpwuid and getpwnam.
Denys Vlasenko31d67342015-01-03 15:15:47 +0100109 * Manpage says:
Denys Vlasenkoacdb0042012-01-06 16:24:56 +0100110 * "The return value may point to a static area, and may be overwritten
111 * by subsequent calls to getpwent(), getpwnam(), or getpwuid()."
112 */
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100113 struct passdb db[2 + ENABLE_USE_BB_SHADOW];
114 char *tokenize_end;
Denys Vlasenko134c5302015-01-03 20:47:47 +0100115 unsigned string_size;
Denis Vlasenko2c91efb2007-06-18 10:08:27 +0000116};
117
118static struct statics *ptr_to_statics;
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100119#define S (*ptr_to_statics)
120#define has_S (ptr_to_statics)
Denis Vlasenko2c91efb2007-06-18 10:08:27 +0000121
Denys Vlasenko9dca6ac2015-01-03 16:09:05 +0100122#if ENABLE_FEATURE_CLEAN_UP
123static void free_static(void)
124{
125 free(S.db[0].malloced);
126 free(S.db[1].malloced);
127# if ENABLE_USE_BB_SHADOW
Denys Vlasenko20c0a162015-01-03 19:12:49 +0100128 free(S.db[2].malloced);
Denys Vlasenko9dca6ac2015-01-03 16:09:05 +0100129# endif
130 free(ptr_to_statics);
131}
132#endif
133
Denis Vlasenko2c91efb2007-06-18 10:08:27 +0000134static struct statics *get_S(void)
135{
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100136 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 Vlasenko9dca6ac2015-01-03 16:09:05 +0100143#if ENABLE_FEATURE_CLEAN_UP
144 atexit(free_static);
145#endif
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100146 }
Denis Vlasenko2c91efb2007-06-18 10:08:27 +0000147 return ptr_to_statics;
148}
149
Denys Vlasenko31d67342015-01-03 15:15:47 +0100150/* Internal functions */
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100151
152/* Divide the passwd/group/shadow record in fields
153 * by substituting the given delimeter
154 * e.g. ':' or ',' with '\0'.
Denys Vlasenko31d67342015-01-03 15:15:47 +0100155 * Returns the number of fields found.
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100156 * Strips leading and trailing whitespace in fields.
Eric Andersen9615a082004-07-15 12:53:49 +0000157 */
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100158static int tokenize(char *buffer, int ch)
Eric Andersen9615a082004-07-15 12:53:49 +0000159{
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100160 char *p = buffer;
161 char *s = p;
162 int num_fields = 0;
Eric Andersen9615a082004-07-15 12:53:49 +0000163
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100164 for (;;) {
165 if (isblank(*s)) {
166 overlapping_strcpy(s, skip_whitespace(s));
Eric Andersen9615a082004-07-15 12:53:49 +0000167 }
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100168 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 Andersen9615a082004-07-15 12:53:49 +0000183 }
Eric Andersen9615a082004-07-15 12:53:49 +0000184}
185
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100186/* 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 */
189static char *parse_common(FILE *fp, const char *filename,
190 int n_fields,
191 const char *key, int field_pos)
192{
193 int count = 0;
194 char *buf;
Eric Andersen9615a082004-07-15 12:53:49 +0000195
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100196 while ((buf = xmalloc_fgetline(fp)) != NULL) {
197 count++;
198 /* Skip empty lines, comment lines */
199 if (buf[0] == '\0' || buf[0] == '#')
200 goto free_and_next;
201 if (tokenize(buf, ':') != n_fields) {
202 /* number of fields is wrong */
203 bb_error_msg("bad record at %s:%u", filename, count);
204 goto free_and_next;
205 }
Eric Andersen9615a082004-07-15 12:53:49 +0000206
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100207 if (!key) {
208 /* no key specified: sequential read, return a record */
209 break;
210 }
211 if (strcmp(key, nth_string(buf, field_pos)) == 0) {
212 /* record found */
213 break;
214 }
215 free_and_next:
216 free(buf);
217 }
218
Denys Vlasenkof9936672015-01-03 21:03:39 +0100219 S.string_size = S.tokenize_end - buf;
220/*
221 * Ugly hack: group db requires additional buffer space
222 * for members[] array. If there is only one group, we need space
223 * for 3 pointers: alignment padding, group name, NULL.
224 * +1 for every additional group.
225 */
Denys Vlasenko402451a2015-01-03 21:11:27 +0100226 if (buf && n_fields == sizeof(GR_DEF)-1) { /* if we read group file... */
Denys Vlasenkof9936672015-01-03 21:03:39 +0100227 int cnt = 3;
228 char *p = buf;
229 while (p < S.tokenize_end)
230 if (*p++ == ',')
231 cnt++;
232 S.string_size += cnt * sizeof(char*);
233//bb_error_msg("+%d words = %u key:%s buf:'%s'", cnt, S.string_size, key, buf);
234 buf = xrealloc(buf, S.string_size);
235 }
236
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100237 return buf;
238}
239
240static char *parse_file(const char *filename,
241 int n_fields,
242 const char *key, int field_pos)
243{
244 char *buf = NULL;
245 FILE *fp = fopen_for_read(filename);
246
247 if (fp) {
248 buf = parse_common(fp, filename, n_fields, key, field_pos);
249 fclose(fp);
250 }
251 return buf;
252}
253
254/* Convert passwd/group/shadow file record in buffer to a struct */
Denys Vlasenko8d547ac2015-01-03 15:54:04 +0100255static void *convert_to_struct(struct passdb *db,
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100256 char *buffer, void *result)
257{
Denys Vlasenko8d547ac2015-01-03 15:54:04 +0100258 const char *def = db->def;
259 const uint8_t *off = db->off;
260
Denys Vlasenko134c5302015-01-03 20:47:47 +0100261 /* For consistency, zero out all fields */
262 memset(result, 0, db->size_of);
Denys Vlasenko8d547ac2015-01-03 15:54:04 +0100263
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100264 for (;;) {
265 void *member = (char*)result + (*off++);
266
267 if ((*def | 0x20) == 's') { /* s or S */
268 *(char **)member = (char*)buffer;
269 if (!buffer[0] && (*def == 'S')) {
270 errno = EINVAL;
271 }
272 }
273 if (*def == 'I') {
274 *(int *)member = bb_strtou(buffer, NULL, 10);
275 }
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000276#if ENABLE_USE_BB_SHADOW
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100277 if (*def == 'l') {
278 long n = -1;
279 if (buffer[0])
280 n = bb_strtol(buffer, NULL, 10);
281 *(long *)member = n;
282 }
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000283#endif
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100284 if (*def == 'm') {
285 char **members;
286 int i = tokenize(buffer, ',');
Eric Andersen9615a082004-07-15 12:53:49 +0000287
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100288 /* Store members[] after buffer's end.
289 * This is safe ONLY because there is a hack
290 * in parse_common() which allocates additional space
291 * at the end of malloced buffer!
292 */
293 members = (char **)
Denys Vlasenko20c0a162015-01-03 19:12:49 +0100294 ( ((intptr_t)S.tokenize_end + sizeof(members[0]))
295 & -(intptr_t)sizeof(members[0])
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100296 );
Eric Andersen9615a082004-07-15 12:53:49 +0000297
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100298 ((struct group *)result)->gr_mem = members;
299 while (--i >= 0) {
300 *members++ = buffer;
301 buffer += strlen(buffer) + 1;
302 }
303 *members = NULL;
304 }
305 /* def "r" does nothing */
Eric Andersen9615a082004-07-15 12:53:49 +0000306
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100307 def++;
Denys Vlasenko134c5302015-01-03 20:47:47 +0100308 if ((unsigned char)*def <= (unsigned char)' ')
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100309 break;
310 buffer += strlen(buffer) + 1;
Eric Andersen9615a082004-07-15 12:53:49 +0000311 }
312
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100313 if (errno)
314 result = NULL;
315 return result;
Eric Andersen9615a082004-07-15 12:53:49 +0000316}
317
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100318/****** getXXnam/id_r */
Eric Andersen9615a082004-07-15 12:53:49 +0000319
Denys Vlasenko8d547ac2015-01-03 15:54:04 +0100320static int FAST_FUNC getXXnam_r(const char *name, uintptr_t db_and_field_pos,
321 char *buffer, size_t buflen,
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100322 void *result)
323{
324 void *struct_buf = *(void**)result;
325 char *buf;
326 struct passdb *db;
327 get_S();
328 db = &S.db[db_and_field_pos >> 2];
329
330 *(void**)result = NULL;
Denys Vlasenko134c5302015-01-03 20:47:47 +0100331 buf = parse_file(db->filename, db->numfields, name, 0 /*db_and_field_pos & 3*/);
332 /* "db_and_field_pos & 3" is commented out since so far we don't implement
333 * getXXXid_r() functions which would use that to pass 2 here */
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100334 if (buf) {
335 size_t size = S.tokenize_end - buf;
336 if (size > buflen) {
337 errno = ERANGE;
338 } else {
339 memcpy(buffer, buf, size);
Denys Vlasenko8d547ac2015-01-03 15:54:04 +0100340 *(void**)result = convert_to_struct(db, buffer, struct_buf);
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100341 }
342 free(buf);
343 }
344 /* "The reentrant functions return zero on success.
345 * In case of error, an error number is returned."
346 * NB: not finding the record is also a "success" here:
347 */
348 return errno;
349}
350
Denys Vlasenko8d547ac2015-01-03 15:54:04 +0100351int FAST_FUNC getpwnam_r(const char *name, struct passwd *struct_buf,
352 char *buffer, size_t buflen,
353 struct passwd **result)
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100354{
355 /* Why the "store buffer address in result" trick?
356 * This way, getXXnam_r has the same ABI signature as getpwnam_r,
Denys Vlasenko908b6e52015-01-02 22:31:07 +0100357 * hopefully compiler can optimize tail call better in this case.
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100358 */
359 *result = struct_buf;
360 return getXXnam_r(name, (0 << 2) + 0, buffer, buflen, result);
361}
362#if ENABLE_USE_BB_SHADOW
Denys Vlasenko908b6e52015-01-02 22:31:07 +0100363int FAST_FUNC getspnam_r(const char *name, struct spwd *struct_buf, char *buffer, size_t buflen,
Denys Vlasenko31d67342015-01-03 15:15:47 +0100364 struct spwd **result)
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100365{
366 *result = struct_buf;
367 return getXXnam_r(name, (2 << 2) + 0, buffer, buflen, result);
Eric Andersen9615a082004-07-15 12:53:49 +0000368}
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000369#endif
Eric Andersen9615a082004-07-15 12:53:49 +0000370
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100371/****** getXXent_r */
372
Denys Vlasenko908b6e52015-01-02 22:31:07 +0100373static int FAST_FUNC getXXent_r(void *struct_buf, char *buffer, size_t buflen,
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100374 void *result,
375 unsigned db_idx)
376{
377 char *buf;
378 struct passdb *db;
379 get_S();
380 db = &S.db[db_idx];
381
382 *(void**)result = NULL;
383
384 if (!db->fp) {
385 db->fp = fopen_for_read(db->filename);
386 if (!db->fp) {
387 return errno;
388 }
389 close_on_exec_on(fileno(db->fp));
390 }
391
392 buf = parse_common(db->fp, db->filename, db->numfields, /*no search key:*/ NULL, 0);
393 if (buf) {
394 size_t size = S.tokenize_end - buf;
395 if (size > buflen) {
396 errno = ERANGE;
397 } else {
398 memcpy(buffer, buf, size);
Denys Vlasenko8d547ac2015-01-03 15:54:04 +0100399 *(void**)result = convert_to_struct(db, buffer, struct_buf);
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100400 }
401 free(buf);
402 }
403 /* "The reentrant functions return zero on success.
404 * In case of error, an error number is returned."
405 * NB: not finding the record is also a "success" here:
406 */
407 return errno;
408}
409
Denys Vlasenko908b6e52015-01-02 22:31:07 +0100410int FAST_FUNC getpwent_r(struct passwd *struct_buf, char *buffer, size_t buflen, struct passwd **result)
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100411{
412 return getXXent_r(struct_buf, buffer, buflen, result, 0);
413}
414
415/****** getXXnam/id */
416
Denys Vlasenko908b6e52015-01-02 22:31:07 +0100417static void* FAST_FUNC getXXnam(const char *name, unsigned db_and_field_pos)
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100418{
419 char *buf;
420 void *result;
421 struct passdb *db;
422 get_S();
423 db = &S.db[db_and_field_pos >> 2];
424
425 result = NULL;
426
427 if (!db->fp) {
428 db->fp = fopen_for_read(db->filename);
429 if (!db->fp) {
430 return NULL;
431 }
432 close_on_exec_on(fileno(db->fp));
433 }
434
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100435 buf = parse_common(db->fp, db->filename, db->numfields, name, db_and_field_pos & 3);
436 if (buf) {
Denys Vlasenko8d547ac2015-01-03 15:54:04 +0100437 free(db->malloced);
Denys Vlasenko134c5302015-01-03 20:47:47 +0100438 /* We enlarge buf and move string data up, freeing space
439 * for struct passwd/group/spwd at the beginning. This way,
440 * entire result of getXXnam is in a single malloced block.
441 * This enables easy creation of xmalloc_getpwnam() API.
442 */
443 db->malloced = buf = xrealloc(buf, db->size_of + S.string_size);
444 memmove(buf + db->size_of, buf, S.string_size);
445 result = convert_to_struct(db, buf + db->size_of, buf);
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100446 }
447 return result;
448}
449
Denys Vlasenko908b6e52015-01-02 22:31:07 +0100450struct passwd* FAST_FUNC getpwnam(const char *name)
Eric Andersen9615a082004-07-15 12:53:49 +0000451{
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100452 return getXXnam(name, (0 << 2) + 0);
Eric Andersen9615a082004-07-15 12:53:49 +0000453}
Denys Vlasenko908b6e52015-01-02 22:31:07 +0100454struct group* FAST_FUNC getgrnam(const char *name)
Eric Andersen9615a082004-07-15 12:53:49 +0000455{
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100456 return getXXnam(name, (1 << 2) + 0);
Eric Andersen9615a082004-07-15 12:53:49 +0000457}
Denys Vlasenko908b6e52015-01-02 22:31:07 +0100458struct passwd* FAST_FUNC getpwuid(uid_t id)
Eric Andersen9615a082004-07-15 12:53:49 +0000459{
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100460 return getXXnam(utoa(id), (0 << 2) + 2);
Eric Andersen9615a082004-07-15 12:53:49 +0000461}
Denys Vlasenko908b6e52015-01-02 22:31:07 +0100462struct group* FAST_FUNC getgrgid(gid_t id)
Eric Andersen9615a082004-07-15 12:53:49 +0000463{
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100464 return getXXnam(utoa(id), (1 << 2) + 2);
Eric Andersen9615a082004-07-15 12:53:49 +0000465}
466
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100467/****** end/setXXend */
468
Denys Vlasenko908b6e52015-01-02 22:31:07 +0100469void FAST_FUNC endpwent(void)
Eric Andersen9615a082004-07-15 12:53:49 +0000470{
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100471 if (has_S && S.db[0].fp) {
472 fclose(S.db[0].fp);
473 S.db[0].fp = NULL;
Eric Andersen9615a082004-07-15 12:53:49 +0000474 }
475}
Denys Vlasenko908b6e52015-01-02 22:31:07 +0100476void FAST_FUNC setpwent(void)
Eric Andersen9615a082004-07-15 12:53:49 +0000477{
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100478 if (has_S && S.db[0].fp) {
479 rewind(S.db[0].fp);
Eric Andersen9615a082004-07-15 12:53:49 +0000480 }
Eric Andersen9615a082004-07-15 12:53:49 +0000481}
Denys Vlasenko908b6e52015-01-02 22:31:07 +0100482void FAST_FUNC endgrent(void)
Eric Andersen9615a082004-07-15 12:53:49 +0000483{
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100484 if (has_S && S.db[1].fp) {
485 fclose(S.db[1].fp);
486 S.db[1].fp = NULL;
Eric Andersen9615a082004-07-15 12:53:49 +0000487 }
488}
489
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100490/****** initgroups and getgrouplist */
491
492static gid_t* FAST_FUNC getgrouplist_internal(int *ngroups_ptr,
493 const char *user, gid_t gid)
Eric Andersen9615a082004-07-15 12:53:49 +0000494{
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100495 FILE *fp;
Eric Andersen9615a082004-07-15 12:53:49 +0000496 gid_t *group_list;
Denis Vlasenko22284262008-09-18 00:56:24 +0000497 int ngroups;
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100498
499 get_S();
Eric Andersen9615a082004-07-15 12:53:49 +0000500
Denis Vlasenko22284262008-09-18 00:56:24 +0000501 /* We alloc space for 8 gids at a time. */
Denys Vlasenko31d67342015-01-03 15:15:47 +0100502 group_list = xzalloc(8 * sizeof(group_list[0]));
Denis Vlasenko22284262008-09-18 00:56:24 +0000503 group_list[0] = gid;
504 ngroups = 1;
505
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100506 fp = fopen_for_read(_PATH_GROUP);
507 if (fp) {
508 char *buf;
509 while ((buf = parse_common(fp, _PATH_GROUP, sizeof(GR_DEF)-1, NULL, 0)) != NULL) {
Denis Vlasenko22284262008-09-18 00:56:24 +0000510 char **m;
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100511 struct group group;
Denys Vlasenko8d547ac2015-01-03 15:54:04 +0100512 if (!convert_to_struct(&S.db[1], buf, &group))
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100513 goto next;
Denis Vlasenko22284262008-09-18 00:56:24 +0000514 if (group.gr_gid == gid)
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100515 goto next;
Denis Vlasenko22284262008-09-18 00:56:24 +0000516 for (m = group.gr_mem; *m; m++) {
517 if (strcmp(*m, user) != 0)
518 continue;
Denys Vlasenko17fcd722010-03-31 12:37:43 +0200519 group_list = xrealloc_vector(group_list, /*8=2^3:*/ 3, ngroups);
Denis Vlasenko22284262008-09-18 00:56:24 +0000520 group_list[ngroups++] = group.gr_gid;
521 break;
Eric Andersen9615a082004-07-15 12:53:49 +0000522 }
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100523 next:
524 free(buf);
Eric Andersen9615a082004-07-15 12:53:49 +0000525 }
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100526 fclose(fp);
Eric Andersen9615a082004-07-15 12:53:49 +0000527 }
Denis Vlasenko22284262008-09-18 00:56:24 +0000528 *ngroups_ptr = ngroups;
529 return group_list;
530}
Eric Andersen9615a082004-07-15 12:53:49 +0000531
Denys Vlasenko908b6e52015-01-02 22:31:07 +0100532int FAST_FUNC initgroups(const char *user, gid_t gid)
Denis Vlasenko22284262008-09-18 00:56:24 +0000533{
534 int ngroups;
535 gid_t *group_list = getgrouplist_internal(&ngroups, user, gid);
536
Denis Vlasenko22284262008-09-18 00:56:24 +0000537 ngroups = setgroups(ngroups, group_list);
538 free(group_list);
539 return ngroups;
540}
541
Denys Vlasenko908b6e52015-01-02 22:31:07 +0100542int FAST_FUNC getgrouplist(const char *user, gid_t gid, gid_t *groups, int *ngroups)
Denis Vlasenko22284262008-09-18 00:56:24 +0000543{
544 int ngroups_old = *ngroups;
545 gid_t *group_list = getgrouplist_internal(ngroups, user, gid);
546
547 if (*ngroups <= ngroups_old) {
548 ngroups_old = *ngroups;
549 memcpy(groups, group_list, ngroups_old * sizeof(groups[0]));
550 } else {
551 ngroups_old = -1;
552 }
553 free(group_list);
554 return ngroups_old;
Eric Andersen9615a082004-07-15 12:53:49 +0000555}