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.
37 const char def[7 + 2*ENABLE_USE_BB_SHADOW];
38 const uint8_t off[7 + 2*ENABLE_USE_BB_SHADOW];
44 const char def[7 + 2*ENABLE_USE_BB_SHADOW];
45 const uint8_t off[7 + 2*ENABLE_USE_BB_SHADOW];
51 /* Note: for shadow db, def[] will not contain terminating NUL,
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.
57 /* S = string not empty, s = string maybe empty,
58 * I = uid,gid, l = long maybe empty, m = members,
61 #define PW_DEF "SsIIsSS"
63 #define SP_DEF "Ssllllllr"
65 static const struct const_passdb const_pw_db = {
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 */
76 sizeof(PW_DEF)-1, sizeof(struct passwd)
78 static const struct const_passdb const_gr_db = {
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 **) */
86 sizeof(GR_DEF)-1, sizeof(struct group)
88 #if ENABLE_USE_BB_SHADOW
89 static const struct const_passdb const_sp_db = {
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 */
102 sizeof(SP_DEF)-1, sizeof(struct spwd)
106 /* We avoid having big global data. */
108 /* It's ok to use same buffer (db[0].malloced) for getpwuid and getpwnam.
110 * "The return value may point to a static area, and may be overwritten
111 * by subsequent calls to getpwent(), getpwnam(), or getpwuid()."
113 struct passdb db[2 + ENABLE_USE_BB_SHADOW];
115 unsigned string_size;
118 static struct statics *ptr_to_statics;
119 #define S (*ptr_to_statics)
120 #define has_S (ptr_to_statics)
122 #if ENABLE_FEATURE_CLEAN_UP
123 static void free_static(void)
125 free(S.db[0].malloced);
126 free(S.db[1].malloced);
127 # if ENABLE_USE_BB_SHADOW
128 free(S.db[2].malloced);
130 free(ptr_to_statics);
134 static struct statics *get_S(void)
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));
143 #if ENABLE_FEATURE_CLEAN_UP
147 return ptr_to_statics;
150 /* Internal functions */
152 /* Divide the passwd/group/shadow record in fields
153 * by substituting the given delimeter
154 * e.g. ':' or ',' with '\0'.
155 * Returns the number of fields found.
156 * Strips leading and trailing whitespace in fields.
158 static int tokenize(char *buffer, int ch)
166 overlapping_strcpy(s, skip_whitespace(s));
168 if (*p == ch || *p == '\0') {
170 while (p != s && isblank(p[-1]))
173 overlapping_strcpy(p, end);
176 S.tokenize_end = p + 1;
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.
189 static char *parse_common(FILE *fp, const char *filename,
191 const char *key, int field_pos)
196 while ((buf = xmalloc_fgetline(fp)) != NULL) {
198 /* Skip empty lines, comment lines */
199 if (buf[0] == '\0' || buf[0] == '#')
201 if (tokenize(buf, ':') != n_fields) {
202 /* number of fields is wrong */
203 bb_error_msg("bad record at %s:%u", filename, count);
208 /* no key specified: sequential read, return a record */
211 if (strcmp(key, nth_string(buf, field_pos)) == 0) {
219 S.string_size = S.tokenize_end - buf;
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.
226 if (buf && n_fields == sizeof(GR_DEF)-1) { /* if we read group file... */
229 while (p < S.tokenize_end)
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);
240 static char *parse_file(const char *filename,
242 const char *key, int field_pos)
245 FILE *fp = fopen_for_read(filename);
248 buf = parse_common(fp, filename, n_fields, key, field_pos);
254 /* Convert passwd/group/shadow file record in buffer to a struct */
255 static void *convert_to_struct(struct passdb *db,
256 char *buffer, void *result)
258 const char *def = db->def;
259 const uint8_t *off = db->off;
261 /* For consistency, zero out all fields */
262 memset(result, 0, db->size_of);
265 void *member = (char*)result + (*off++);
267 if ((*def | 0x20) == 's') { /* s or S */
268 *(char **)member = (char*)buffer;
269 if (!buffer[0] && (*def == 'S')) {
274 *(int *)member = bb_strtou(buffer, NULL, 10);
276 #if ENABLE_USE_BB_SHADOW
280 n = bb_strtol(buffer, NULL, 10);
286 int i = tokenize(buffer, ',');
288 /* 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!
294 ( ((intptr_t)S.tokenize_end + sizeof(members[0]))
295 & -(intptr_t)sizeof(members[0])
298 ((struct group *)result)->gr_mem = members;
301 buffer += strlen(buffer) + 1;
305 /* def "r" does nothing */
308 if ((unsigned char)*def <= (unsigned char)' ')
310 buffer += strlen(buffer) + 1;
318 /****** getXXnam/id_r */
320 static int FAST_FUNC getXXnam_r(const char *name, uintptr_t db_and_field_pos,
321 char *buffer, size_t buflen,
324 void *struct_buf = *(void**)result;
328 db = &S.db[db_and_field_pos >> 2];
330 *(void**)result = NULL;
331 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 */
335 size_t size = S.tokenize_end - buf;
339 memcpy(buffer, buf, size);
340 *(void**)result = convert_to_struct(db, buffer, struct_buf);
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:
351 int FAST_FUNC getpwnam_r(const char *name, struct passwd *struct_buf,
352 char *buffer, size_t buflen,
353 struct passwd **result)
355 /* Why the "store buffer address in result" trick?
356 * This way, getXXnam_r has the same ABI signature as getpwnam_r,
357 * hopefully compiler can optimize tail call better in this case.
359 *result = struct_buf;
360 return getXXnam_r(name, (0 << 2) + 0, buffer, buflen, result);
362 #if ENABLE_USE_BB_SHADOW
363 int FAST_FUNC getspnam_r(const char *name, struct spwd *struct_buf, char *buffer, size_t buflen,
364 struct spwd **result)
366 *result = struct_buf;
367 return getXXnam_r(name, (2 << 2) + 0, buffer, buflen, result);
371 /****** getXXent_r */
373 static int FAST_FUNC getXXent_r(void *struct_buf, char *buffer, size_t buflen,
382 *(void**)result = NULL;
385 db->fp = fopen_for_read(db->filename);
389 close_on_exec_on(fileno(db->fp));
392 buf = parse_common(db->fp, db->filename, db->numfields, /*no search key:*/ NULL, 0);
394 size_t size = S.tokenize_end - buf;
398 memcpy(buffer, buf, size);
399 *(void**)result = convert_to_struct(db, buffer, struct_buf);
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:
410 int FAST_FUNC getpwent_r(struct passwd *struct_buf, char *buffer, size_t buflen, struct passwd **result)
412 return getXXent_r(struct_buf, buffer, buflen, result, 0);
415 /****** getXXnam/id */
417 static void* FAST_FUNC getXXnam(const char *name, unsigned db_and_field_pos)
423 db = &S.db[db_and_field_pos >> 2];
428 db->fp = fopen_for_read(db->filename);
432 close_on_exec_on(fileno(db->fp));
435 buf = parse_common(db->fp, db->filename, db->numfields, name, db_and_field_pos & 3);
438 /* 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.
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);
450 struct passwd* FAST_FUNC getpwnam(const char *name)
452 return getXXnam(name, (0 << 2) + 0);
454 struct group* FAST_FUNC getgrnam(const char *name)
456 return getXXnam(name, (1 << 2) + 0);
458 struct passwd* FAST_FUNC getpwuid(uid_t id)
460 return getXXnam(utoa(id), (0 << 2) + 2);
462 struct group* FAST_FUNC getgrgid(gid_t id)
464 return getXXnam(utoa(id), (1 << 2) + 2);
467 /****** end/setXXend */
469 void FAST_FUNC endpwent(void)
471 if (has_S && S.db[0].fp) {
476 void FAST_FUNC setpwent(void)
478 if (has_S && S.db[0].fp) {
482 void FAST_FUNC endgrent(void)
484 if (has_S && S.db[1].fp) {
490 /****** initgroups and getgrouplist */
492 static gid_t* FAST_FUNC getgrouplist_internal(int *ngroups_ptr,
493 const char *user, gid_t gid)
501 /* We alloc space for 8 gids at a time. */
502 group_list = xzalloc(8 * sizeof(group_list[0]));
506 fp = fopen_for_read(_PATH_GROUP);
509 while ((buf = parse_common(fp, _PATH_GROUP, sizeof(GR_DEF)-1, NULL, 0)) != NULL) {
512 if (!convert_to_struct(&S.db[1], buf, &group))
514 if (group.gr_gid == gid)
516 for (m = group.gr_mem; *m; m++) {
517 if (strcmp(*m, user) != 0)
519 group_list = xrealloc_vector(group_list, /*8=2^3:*/ 3, ngroups);
520 group_list[ngroups++] = group.gr_gid;
528 *ngroups_ptr = ngroups;
532 int FAST_FUNC initgroups(const char *user, gid_t gid)
535 gid_t *group_list = getgrouplist_internal(&ngroups, user, gid);
537 ngroups = setgroups(ngroups, group_list);
542 int FAST_FUNC getgrouplist(const char *user, gid_t gid, gid_t *groups, int *ngroups)
544 int ngroups_old = *ngroups;
545 gid_t *group_list = getgrouplist_internal(ngroups, user, gid);
547 if (*ngroups <= ngroups_old) {
548 ngroups_old = *ngroups;
549 memcpy(groups, group_list, ngroups_old * sizeof(groups[0]));