1 /* vi: set sw=4 ts=4: */
2 /* Copyright (C) 2014 Tito Ragusa <farmatito@tiscali.it>
4 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
6 /* This program is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY!!
9 * Rewrite of some parts. Main differences are:
11 * 1) the buffer for getpwuid, getgrgid, getpwnam, getgrnam is dynamically
12 * allocated and reused by later calls.
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.
18 * b) leading or trailing whitespace in fields is stripped.
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
25 * 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
29 * implemented, if you need a particular missing function it should be
30 * easy to write it by using the internal common code.
49 | sizeof(struct passwd)
50 | sizeof(struct group)
51 IF_USE_BB_SHADOW( | sizeof(struct spwd) )
52 /* bitwise OR above is poor man's max(a,b,c) */
56 /* S = string not empty, s = string maybe empty,
57 * I = uid,gid, l = long maybe empty, m = members,
60 #define PW_DEF "SsIIsSS"
62 #define SP_DEF "Ssllllllr"
64 static const struct const_passdb const_pw_db = {
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 */
72 offsetof(struct passwd, pw_dir), /* 5 S */
73 offsetof(struct passwd, pw_shell) /* 6 S */
77 static const struct const_passdb const_gr_db = {
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 **) */
87 #if ENABLE_USE_BB_SHADOW
88 static const struct const_passdb const_sp_db = {
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 */
105 /* We avoid having big global data. */
107 /* It's ok to use same buffer (db[0].struct_result) for getpwuid and getpwnam.
109 * "The return value may point to a static area, and may be overwritten
110 * by subsequent calls to getpwent(), getpwnam(), or getpwuid()."
112 struct passdb db[2 + ENABLE_USE_BB_SHADOW];
116 static struct statics *ptr_to_statics;
117 #define S (*ptr_to_statics)
118 #define has_S (ptr_to_statics)
120 #if ENABLE_FEATURE_CLEAN_UP
121 static void free_static(void)
123 free(S.db[0].malloced);
124 free(S.db[1].malloced);
125 # if ENABLE_USE_BB_SHADOW
128 free(ptr_to_statics);
132 static struct statics *get_S(void)
134 if (!ptr_to_statics) {
135 ptr_to_statics = xzalloc(sizeof(S));
136 memcpy(&S.db[0], &const_pw_db, sizeof(const_pw_db));
137 memcpy(&S.db[1], &const_gr_db, sizeof(const_gr_db));
138 #if ENABLE_USE_BB_SHADOW
139 memcpy(&S.db[2], &const_sp_db, sizeof(const_sp_db));
141 #if ENABLE_FEATURE_CLEAN_UP
145 return ptr_to_statics;
148 /* Internal functions */
150 /* Divide the passwd/group/shadow record in fields
151 * by substituting the given delimeter
152 * e.g. ':' or ',' with '\0'.
153 * Returns the number of fields found.
154 * Strips leading and trailing whitespace in fields.
156 static int tokenize(char *buffer, int ch)
164 overlapping_strcpy(s, skip_whitespace(s));
166 if (*p == ch || *p == '\0') {
168 while (p != s && isblank(p[-1]))
171 overlapping_strcpy(p, end);
174 S.tokenize_end = p + 1;
184 /* Returns !NULL on success and matching line broken up in fields by '\0' in buf.
185 * We require the expected number of fields to be found.
187 static char *parse_common(FILE *fp, const char *filename,
189 const char *key, int field_pos)
194 while ((buf = xmalloc_fgetline(fp)) != NULL) {
196 /* Skip empty lines, comment lines */
197 if (buf[0] == '\0' || buf[0] == '#')
199 if (tokenize(buf, ':') != n_fields) {
200 /* number of fields is wrong */
201 bb_error_msg("bad record at %s:%u", filename, count);
205 /* Ugly hack: group db requires aqdditional buffer space
206 * for members[] array. If there is only one group, we need space
207 * for 3 pointers: alignment padding, group name, NULL.
208 * +1 for every additional group.
210 if (n_fields == sizeof(GR_DEF)-1) { /* if we read group file */
216 resize *= sizeof(char**);
217 resize += S.tokenize_end - buf;
218 buf = xrealloc(buf, resize);
222 /* no key specified: sequential read, return a record */
225 if (strcmp(key, nth_string(buf, field_pos)) == 0) {
236 static char *parse_file(const char *filename,
238 const char *key, int field_pos)
241 FILE *fp = fopen_for_read(filename);
244 buf = parse_common(fp, filename, n_fields, key, field_pos);
250 /* Convert passwd/group/shadow file record in buffer to a struct */
251 static void *convert_to_struct(struct passdb *db,
252 char *buffer, void *result)
254 const char *def = db->def;
255 const uint8_t *off = db->off;
257 /* TODO? for consistency, zero out all fields */
258 /* memset(result, 0, size_of_result);*/
261 void *member = (char*)result + (*off++);
263 if ((*def | 0x20) == 's') { /* s or S */
264 *(char **)member = (char*)buffer;
265 if (!buffer[0] && (*def == 'S')) {
270 *(int *)member = bb_strtou(buffer, NULL, 10);
272 #if ENABLE_USE_BB_SHADOW
276 n = bb_strtol(buffer, NULL, 10);
282 int i = tokenize(buffer, ',');
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!
290 ( ((intptr_t)S.tokenize_end + sizeof(char**))
291 & -(intptr_t)sizeof(char**)
294 ((struct group *)result)->gr_mem = members;
297 buffer += strlen(buffer) + 1;
301 /* def "r" does nothing */
306 buffer += strlen(buffer) + 1;
314 /****** getXXnam/id_r */
316 static int FAST_FUNC getXXnam_r(const char *name, uintptr_t db_and_field_pos,
317 char *buffer, size_t buflen,
320 void *struct_buf = *(void**)result;
324 db = &S.db[db_and_field_pos >> 2];
326 *(void**)result = NULL;
327 buf = parse_file(db->filename, db->numfields, name, db_and_field_pos & 3);
329 size_t size = S.tokenize_end - buf;
333 memcpy(buffer, buf, size);
334 *(void**)result = convert_to_struct(db, buffer, struct_buf);
338 /* "The reentrant functions return zero on success.
339 * In case of error, an error number is returned."
340 * NB: not finding the record is also a "success" here:
345 int FAST_FUNC getpwnam_r(const char *name, struct passwd *struct_buf,
346 char *buffer, size_t buflen,
347 struct passwd **result)
349 /* Why the "store buffer address in result" trick?
350 * This way, getXXnam_r has the same ABI signature as getpwnam_r,
351 * hopefully compiler can optimize tail call better in this case.
353 *result = struct_buf;
354 return getXXnam_r(name, (0 << 2) + 0, buffer, buflen, result);
356 #if ENABLE_USE_BB_SHADOW
357 int FAST_FUNC getspnam_r(const char *name, struct spwd *struct_buf, char *buffer, size_t buflen,
358 struct spwd **result)
360 *result = struct_buf;
361 return getXXnam_r(name, (2 << 2) + 0, buffer, buflen, result);
365 /****** getXXent_r */
367 static int FAST_FUNC getXXent_r(void *struct_buf, char *buffer, size_t buflen,
376 *(void**)result = NULL;
379 db->fp = fopen_for_read(db->filename);
383 close_on_exec_on(fileno(db->fp));
386 buf = parse_common(db->fp, db->filename, db->numfields, /*no search key:*/ NULL, 0);
388 size_t size = S.tokenize_end - buf;
392 memcpy(buffer, buf, size);
393 *(void**)result = convert_to_struct(db, buffer, struct_buf);
397 /* "The reentrant functions return zero on success.
398 * In case of error, an error number is returned."
399 * NB: not finding the record is also a "success" here:
404 int FAST_FUNC getpwent_r(struct passwd *struct_buf, char *buffer, size_t buflen, struct passwd **result)
406 return getXXent_r(struct_buf, buffer, buflen, result, 0);
409 /****** getXXnam/id */
411 static void* FAST_FUNC getXXnam(const char *name, unsigned db_and_field_pos)
417 db = &S.db[db_and_field_pos >> 2];
422 db->fp = fopen_for_read(db->filename);
426 close_on_exec_on(fileno(db->fp));
429 buf = parse_common(db->fp, db->filename, db->numfields, name, db_and_field_pos & 3);
433 result = convert_to_struct(db, buf, db->struct_result);
438 struct passwd* FAST_FUNC getpwnam(const char *name)
440 return getXXnam(name, (0 << 2) + 0);
442 struct group* FAST_FUNC getgrnam(const char *name)
444 return getXXnam(name, (1 << 2) + 0);
446 struct passwd* FAST_FUNC getpwuid(uid_t id)
448 return getXXnam(utoa(id), (0 << 2) + 2);
450 struct group* FAST_FUNC getgrgid(gid_t id)
452 return getXXnam(utoa(id), (1 << 2) + 2);
455 /****** end/setXXend */
457 void FAST_FUNC endpwent(void)
459 if (has_S && S.db[0].fp) {
464 void FAST_FUNC setpwent(void)
466 if (has_S && S.db[0].fp) {
470 void FAST_FUNC endgrent(void)
472 if (has_S && S.db[1].fp) {
478 /****** initgroups and getgrouplist */
480 static gid_t* FAST_FUNC getgrouplist_internal(int *ngroups_ptr,
481 const char *user, gid_t gid)
489 /* We alloc space for 8 gids at a time. */
490 group_list = xzalloc(8 * sizeof(group_list[0]));
494 fp = fopen_for_read(_PATH_GROUP);
497 while ((buf = parse_common(fp, _PATH_GROUP, sizeof(GR_DEF)-1, NULL, 0)) != NULL) {
500 if (!convert_to_struct(&S.db[1], buf, &group))
502 if (group.gr_gid == gid)
504 for (m = group.gr_mem; *m; m++) {
505 if (strcmp(*m, user) != 0)
507 group_list = xrealloc_vector(group_list, /*8=2^3:*/ 3, ngroups);
508 group_list[ngroups++] = group.gr_gid;
516 *ngroups_ptr = ngroups;
520 int FAST_FUNC initgroups(const char *user, gid_t gid)
523 gid_t *group_list = getgrouplist_internal(&ngroups, user, gid);
525 ngroups = setgroups(ngroups, group_list);
530 int FAST_FUNC getgrouplist(const char *user, gid_t gid, gid_t *groups, int *ngroups)
532 int ngroups_old = *ngroups;
533 gid_t *group_list = getgrouplist_internal(ngroups, user, gid);
535 if (*ngroups <= ngroups_old) {
536 ngroups_old = *ngroups;
537 memcpy(groups, group_list, ngroups_old * sizeof(groups[0]));