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
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 and trailing whitespace in fields is stripped.
19 * c) some fields are not allowed to be empty (e.g. username, uid/gid),
20 * and in this case NULL is returned and errno is set to EINVAL.
21 * This behaviour could be easily changed by modifying PW_DEF, GR_DEF,
22 * SP_DEF strings (uppercase makes a field mandatory).
23 * d) the string representing uid/gid must be convertible by strtoXX
24 * functions, or errno is set to EINVAL.
25 * e) leading and trailing whitespace in group member names is stripped.
26 * 3) the internal function for getgrouplist uses dynamically allocated buffer.
27 * 4) at the moment only the functions really used by busybox code are
28 * implemented, if you need a particular missing function it should be
29 * easy to write it by using the internal common code.
36 char def[7 + 2*ENABLE_USE_BB_SHADOW];
37 uint8_t off[7 + 2*ENABLE_USE_BB_SHADOW];
43 char def[7 + 2*ENABLE_USE_BB_SHADOW];
44 uint8_t off[7 + 2*ENABLE_USE_BB_SHADOW];
50 /* Note: for shadow db, def[] will not contain terminating NUL,
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.
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 */
75 sizeof(PW_DEF)-1, sizeof(struct passwd)
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 **) */
85 sizeof(GR_DEF)-1, sizeof(struct group)
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 */
101 sizeof(SP_DEF)-1, sizeof(struct spwd)
105 /* We avoid having big global data. */
107 /* We use same buffer (db[0].malloced) 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];
114 unsigned string_size;
117 static struct statics *ptr_to_statics;
118 #define S (*ptr_to_statics)
119 #define has_S (ptr_to_statics)
121 #if ENABLE_FEATURE_CLEAN_UP
122 static void free_static(void)
124 free(S.db[0].malloced);
125 free(S.db[1].malloced);
126 # if ENABLE_USE_BB_SHADOW
127 free(S.db[2].malloced);
129 free(ptr_to_statics);
133 static struct statics *get_S(void)
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));
142 #if ENABLE_FEATURE_CLEAN_UP
146 return ptr_to_statics;
149 /* Internal functions */
151 /* Divide the passwd/group/shadow record in fields
152 * by substituting the given delimeter
153 * e.g. ':' or ',' with '\0'.
154 * Returns the number of fields found.
155 * Strips leading and trailing whitespace in fields.
157 static int tokenize(char *buffer, int ch)
165 overlapping_strcpy(s, skip_whitespace(s));
167 if (*p == ch || *p == '\0') {
169 while (p != s && isblank(p[-1]))
172 overlapping_strcpy(p, end);
175 S.tokenize_end = p + 1;
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.
188 static char *parse_common(FILE *fp, struct passdb *db,
189 const char *key, int field_pos)
193 while ((buf = xmalloc_fgetline(fp)) != NULL) {
194 /* Skip empty lines, comment lines */
195 if (buf[0] == '\0' || buf[0] == '#')
197 if (tokenize(buf, ':') != db->numfields) {
198 /* number of fields is wrong */
199 bb_error_msg("%s: bad record", db->filename);
203 if (field_pos == -1) {
204 /* no key specified: sequential read, return a record */
207 if (strcmp(key, nth_string(buf, field_pos)) == 0) {
215 S.string_size = S.tokenize_end - buf;
217 * Ugly hack: group db requires additional buffer space
218 * for members[] array. If there is only one group, we need space
219 * for 3 pointers: alignment padding, group name, NULL.
220 * +1 for every additional group.
222 if (buf && db->numfields == sizeof(GR_DEF)-1) { /* if we read group file... */
225 while (p < S.tokenize_end)
228 S.string_size += cnt * sizeof(char*);
229 //bb_error_msg("+%d words = %u key:%s buf:'%s'", cnt, S.string_size, key, buf);
230 buf = xrealloc(buf, S.string_size);
236 static char *parse_file(struct passdb *db,
237 const char *key, int field_pos)
240 FILE *fp = fopen_for_read(db->filename);
243 buf = parse_common(fp, db, key, field_pos);
249 /* Convert passwd/group/shadow file record in buffer to a struct */
250 static void *convert_to_struct(struct passdb *db,
251 char *buffer, void *result)
253 const char *def = db->def;
254 const uint8_t *off = db->off;
256 /* For consistency, zero out all fields */
257 memset(result, 0, db->size_of);
260 void *member = (char*)result + (*off++);
262 if ((*def | 0x20) == 's') { /* s or S */
263 *(char **)member = (char*)buffer;
264 if (!buffer[0] && (*def == 'S')) {
269 *(int *)member = bb_strtou(buffer, NULL, 10);
271 #if ENABLE_USE_BB_SHADOW
275 n = bb_strtol(buffer, NULL, 10);
281 int i = tokenize(buffer, ',');
283 /* Store members[] after buffer's end.
284 * This is safe ONLY because there is a hack
285 * in parse_common() which allocates additional space
286 * at the end of malloced buffer!
289 ( ((intptr_t)S.tokenize_end + sizeof(members[0]))
290 & -(intptr_t)sizeof(members[0])
292 ((struct group *)result)->gr_mem = members;
296 // bb_error_msg("member[]='%s'", buffer);
298 buffer += strlen(buffer) + 1;
302 /* def "r" does nothing */
305 if ((unsigned char)*def <= (unsigned char)' ')
307 buffer += strlen(buffer) + 1;
315 static int massage_data_for_r_func(struct passdb *db,
316 char *buffer, size_t buflen,
320 void *result_buf = *result;
323 if (S.string_size > buflen) {
326 memcpy(buffer, buf, S.string_size);
327 *result = convert_to_struct(db, buffer, result_buf);
331 /* "The reentrant functions return zero on success.
332 * In case of error, an error number is returned."
333 * NB: not finding the record is also a "success" here:
338 static void* massage_data_for_non_r_func(struct passdb *db, char *buf)
344 /* We enlarge buf and move string data up, freeing space
345 * for struct passwd/group/spwd at the beginning. This way,
346 * entire result of getXXnam is in a single malloced block.
347 * This enables easy creation of xmalloc_getpwnam() API.
349 db->malloced = buf = xrealloc(buf, db->size_of + S.string_size);
350 memmove(buf + db->size_of, buf, S.string_size);
351 return convert_to_struct(db, buf + db->size_of, buf);
354 /****** getXXnam/id_r */
356 static int FAST_FUNC getXXnam_r(const char *name, uintptr_t db_and_field_pos,
357 char *buffer, size_t buflen,
361 struct passdb *db = &get_S()->db[db_and_field_pos >> 2];
363 buf = parse_file(db, name, 0 /*db_and_field_pos & 3*/);
364 /* "db_and_field_pos & 3" is commented out since so far we don't implement
365 * getXXXid_r() functions which would use that to pass 2 here */
367 return massage_data_for_r_func(db, buffer, buflen, result, buf);
370 int FAST_FUNC getpwnam_r(const char *name, struct passwd *struct_buf,
371 char *buffer, size_t buflen,
372 struct passwd **result)
374 /* Why the "store buffer address in result" trick?
375 * This way, getXXnam_r has the same ABI signature as getpwnam_r,
376 * hopefully compiler can optimize tail call better in this case.
378 *result = struct_buf;
379 return getXXnam_r(name, (0 << 2) + 0, buffer, buflen, result);
381 #if ENABLE_USE_BB_SHADOW
382 int FAST_FUNC getspnam_r(const char *name, struct spwd *struct_buf, char *buffer, size_t buflen,
383 struct spwd **result)
385 *result = struct_buf;
386 return getXXnam_r(name, (2 << 2) + 0, buffer, buflen, result);
391 /****** getXXent_r */
393 static int FAST_FUNC getXXent_r(uintptr_t db_idx, char *buffer, size_t buflen,
397 struct passdb *db = &get_S()->db[db_idx];
400 db->fp = fopen_for_read(db->filename);
404 close_on_exec_on(fileno(db->fp));
407 buf = parse_common(db->fp, db, /*no search key:*/ NULL, -1);
410 return massage_data_for_r_func(db, buffer, buflen, result, buf);
413 int FAST_FUNC getpwent_r(struct passwd *struct_buf, char *buffer, size_t buflen,
414 struct passwd **result)
416 *result = struct_buf;
417 return getXXent_r(0, buffer, buflen, result);
423 static void* FAST_FUNC getXXent(uintptr_t db_idx)
426 struct passdb *db = &get_S()->db[db_idx];
429 db->fp = fopen_for_read(db->filename);
433 close_on_exec_on(fileno(db->fp));
436 buf = parse_common(db->fp, db, /*no search key:*/ NULL, -1);
437 return massage_data_for_non_r_func(db, buf);
440 struct passwd* FAST_FUNC getpwent(void)
445 /****** getXXnam/id */
447 static void* FAST_FUNC getXXnam(const char *name, unsigned db_and_field_pos)
450 struct passdb *db = &get_S()->db[db_and_field_pos >> 2];
452 buf = parse_file(db, name, db_and_field_pos & 3);
453 return massage_data_for_non_r_func(db, buf);
456 struct passwd* FAST_FUNC getpwnam(const char *name)
458 return getXXnam(name, (0 << 2) + 0);
460 struct group* FAST_FUNC getgrnam(const char *name)
462 return getXXnam(name, (1 << 2) + 0);
464 struct passwd* FAST_FUNC getpwuid(uid_t id)
466 return getXXnam(utoa(id), (0 << 2) + 2);
468 struct group* FAST_FUNC getgrgid(gid_t id)
470 return getXXnam(utoa(id), (1 << 2) + 2);
473 /****** end/setXXend */
475 void FAST_FUNC endpwent(void)
477 if (has_S && S.db[0].fp) {
482 void FAST_FUNC setpwent(void)
484 if (has_S && S.db[0].fp) {
488 void FAST_FUNC endgrent(void)
490 if (has_S && S.db[1].fp) {
496 /****** initgroups and getgrouplist */
498 static gid_t* FAST_FUNC getgrouplist_internal(int *ngroups_ptr,
499 const char *user, gid_t gid)
505 /* We alloc space for 8 gids at a time. */
506 group_list = xzalloc(8 * sizeof(group_list[0]));
510 fp = fopen_for_read(_PATH_GROUP);
512 struct passdb *db = &get_S()->db[1];
514 while ((buf = parse_common(fp, db, NULL, -1)) != NULL) {
517 if (!convert_to_struct(db, buf, &group))
519 if (group.gr_gid == gid)
521 for (m = group.gr_mem; *m; m++) {
522 if (strcmp(*m, user) != 0)
524 group_list = xrealloc_vector(group_list, /*8=2^3:*/ 3, ngroups);
525 group_list[ngroups++] = group.gr_gid;
533 *ngroups_ptr = ngroups;
537 int FAST_FUNC initgroups(const char *user, gid_t gid)
540 gid_t *group_list = getgrouplist_internal(&ngroups, user, gid);
542 ngroups = setgroups(ngroups, group_list);
547 int FAST_FUNC getgrouplist(const char *user, gid_t gid, gid_t *groups, int *ngroups)
549 int ngroups_old = *ngroups;
550 gid_t *group_list = getgrouplist_internal(ngroups, user, gid);
552 if (*ngroups <= ngroups_old) {
553 ngroups_old = *ngroups;
554 memcpy(groups, group_list, ngroups_old * sizeof(groups[0]));