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 /* Should be max(sizeof passwd,group,spwd), but this will do: */
50 IF_NOT_USE_BB_SHADOW(sizeof(struct passwd))
51 IF_USE_BB_SHADOW(sizeof(struct spwd))
54 /* Note: for shadow db, def[9] will not contain terminating NUL,
55 * but convert_to_struct() logic detects def[] end by "less than SP?",
56 * not by "is it NUL?" condition; and off[0] happens to be zero
57 * for every db anyway, so there _is_ in fact a terminating NUL there.
60 /* S = string not empty, s = string maybe empty,
61 * I = uid,gid, l = long maybe empty, m = members,
64 #define PW_DEF "SsIIsSS"
66 #define SP_DEF "Ssllllllr"
68 static const struct const_passdb const_pw_db = {
71 offsetof(struct passwd, pw_name), /* 0 S */
72 offsetof(struct passwd, pw_passwd), /* 1 s */
73 offsetof(struct passwd, pw_uid), /* 2 I */
74 offsetof(struct passwd, pw_gid), /* 3 I */
75 offsetof(struct passwd, pw_gecos), /* 4 s */
76 offsetof(struct passwd, pw_dir), /* 5 S */
77 offsetof(struct passwd, pw_shell) /* 6 S */
81 static const struct const_passdb const_gr_db = {
84 offsetof(struct group, gr_name), /* 0 S */
85 offsetof(struct group, gr_passwd), /* 1 s */
86 offsetof(struct group, gr_gid), /* 2 I */
87 offsetof(struct group, gr_mem) /* 3 m (char **) */
91 #if ENABLE_USE_BB_SHADOW
92 static const struct const_passdb const_sp_db = {
95 offsetof(struct spwd, sp_namp), /* 0 S Login name */
96 offsetof(struct spwd, sp_pwdp), /* 1 s Encrypted password */
97 offsetof(struct spwd, sp_lstchg), /* 2 l */
98 offsetof(struct spwd, sp_min), /* 3 l */
99 offsetof(struct spwd, sp_max), /* 4 l */
100 offsetof(struct spwd, sp_warn), /* 5 l */
101 offsetof(struct spwd, sp_inact), /* 6 l */
102 offsetof(struct spwd, sp_expire), /* 7 l */
103 offsetof(struct spwd, sp_flag) /* 8 r Reserved */
109 /* We avoid having big global data. */
111 /* It's ok to use same buffer (db[0].struct_result) for getpwuid and getpwnam.
113 * "The return value may point to a static area, and may be overwritten
114 * by subsequent calls to getpwent(), getpwnam(), or getpwuid()."
116 struct passdb db[2 + ENABLE_USE_BB_SHADOW];
120 static struct statics *ptr_to_statics;
121 #define S (*ptr_to_statics)
122 #define has_S (ptr_to_statics)
124 #if ENABLE_FEATURE_CLEAN_UP
125 static void free_static(void)
127 free(S.db[0].malloced);
128 free(S.db[1].malloced);
129 # if ENABLE_USE_BB_SHADOW
130 free(S.db[2].malloced);
132 free(ptr_to_statics);
136 static struct statics *get_S(void)
138 if (!ptr_to_statics) {
139 ptr_to_statics = xzalloc(sizeof(S));
140 memcpy(&S.db[0], &const_pw_db, sizeof(const_pw_db));
141 memcpy(&S.db[1], &const_gr_db, sizeof(const_gr_db));
142 #if ENABLE_USE_BB_SHADOW
143 memcpy(&S.db[2], &const_sp_db, sizeof(const_sp_db));
145 #if ENABLE_FEATURE_CLEAN_UP
149 return ptr_to_statics;
152 /* Internal functions */
154 /* Divide the passwd/group/shadow record in fields
155 * by substituting the given delimeter
156 * e.g. ':' or ',' with '\0'.
157 * Returns the number of fields found.
158 * Strips leading and trailing whitespace in fields.
160 static int tokenize(char *buffer, int ch)
168 overlapping_strcpy(s, skip_whitespace(s));
170 if (*p == ch || *p == '\0') {
172 while (p != s && isblank(p[-1]))
175 overlapping_strcpy(p, end);
178 S.tokenize_end = p + 1;
188 /* Returns !NULL on success and matching line broken up in fields by '\0' in buf.
189 * We require the expected number of fields to be found.
191 static char *parse_common(FILE *fp, const char *filename,
193 const char *key, int field_pos)
198 while ((buf = xmalloc_fgetline(fp)) != NULL) {
200 /* Skip empty lines, comment lines */
201 if (buf[0] == '\0' || buf[0] == '#')
203 if (tokenize(buf, ':') != n_fields) {
204 /* number of fields is wrong */
205 bb_error_msg("bad record at %s:%u", filename, count);
209 /* Ugly hack: group db requires aqdditional buffer space
210 * for members[] array. If there is only one group, we need space
211 * for 3 pointers: alignment padding, group name, NULL.
212 * +1 for every additional group.
214 if (n_fields == sizeof(GR_DEF)-1) { /* if we read group file */
220 resize *= sizeof(char**);
221 resize += S.tokenize_end - buf;
222 buf = xrealloc(buf, resize);
226 /* no key specified: sequential read, return a record */
229 if (strcmp(key, nth_string(buf, field_pos)) == 0) {
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 /* TODO? for consistency, zero out all fields */
262 /* memset(result, 0, size_of_result);*/
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, db_and_field_pos & 3);
333 size_t size = S.tokenize_end - buf;
337 memcpy(buffer, buf, size);
338 *(void**)result = convert_to_struct(db, buffer, struct_buf);
342 /* "The reentrant functions return zero on success.
343 * In case of error, an error number is returned."
344 * NB: not finding the record is also a "success" here:
349 int FAST_FUNC getpwnam_r(const char *name, struct passwd *struct_buf,
350 char *buffer, size_t buflen,
351 struct passwd **result)
353 /* Why the "store buffer address in result" trick?
354 * This way, getXXnam_r has the same ABI signature as getpwnam_r,
355 * hopefully compiler can optimize tail call better in this case.
357 *result = struct_buf;
358 return getXXnam_r(name, (0 << 2) + 0, buffer, buflen, result);
360 #if ENABLE_USE_BB_SHADOW
361 int FAST_FUNC getspnam_r(const char *name, struct spwd *struct_buf, char *buffer, size_t buflen,
362 struct spwd **result)
364 *result = struct_buf;
365 return getXXnam_r(name, (2 << 2) + 0, buffer, buflen, result);
369 /****** getXXent_r */
371 static int FAST_FUNC getXXent_r(void *struct_buf, char *buffer, size_t buflen,
380 *(void**)result = NULL;
383 db->fp = fopen_for_read(db->filename);
387 close_on_exec_on(fileno(db->fp));
390 buf = parse_common(db->fp, db->filename, db->numfields, /*no search key:*/ NULL, 0);
392 size_t size = S.tokenize_end - buf;
396 memcpy(buffer, buf, size);
397 *(void**)result = convert_to_struct(db, buffer, struct_buf);
401 /* "The reentrant functions return zero on success.
402 * In case of error, an error number is returned."
403 * NB: not finding the record is also a "success" here:
408 int FAST_FUNC getpwent_r(struct passwd *struct_buf, char *buffer, size_t buflen, struct passwd **result)
410 return getXXent_r(struct_buf, buffer, buflen, result, 0);
413 /****** getXXnam/id */
415 static void* FAST_FUNC getXXnam(const char *name, unsigned db_and_field_pos)
421 db = &S.db[db_and_field_pos >> 2];
426 db->fp = fopen_for_read(db->filename);
430 close_on_exec_on(fileno(db->fp));
433 buf = parse_common(db->fp, db->filename, db->numfields, name, db_and_field_pos & 3);
437 result = convert_to_struct(db, buf, db->struct_result);
442 struct passwd* FAST_FUNC getpwnam(const char *name)
444 return getXXnam(name, (0 << 2) + 0);
446 struct group* FAST_FUNC getgrnam(const char *name)
448 return getXXnam(name, (1 << 2) + 0);
450 struct passwd* FAST_FUNC getpwuid(uid_t id)
452 return getXXnam(utoa(id), (0 << 2) + 2);
454 struct group* FAST_FUNC getgrgid(gid_t id)
456 return getXXnam(utoa(id), (1 << 2) + 2);
459 /****** end/setXXend */
461 void FAST_FUNC endpwent(void)
463 if (has_S && S.db[0].fp) {
468 void FAST_FUNC setpwent(void)
470 if (has_S && S.db[0].fp) {
474 void FAST_FUNC endgrent(void)
476 if (has_S && S.db[1].fp) {
482 /****** initgroups and getgrouplist */
484 static gid_t* FAST_FUNC getgrouplist_internal(int *ngroups_ptr,
485 const char *user, gid_t gid)
493 /* We alloc space for 8 gids at a time. */
494 group_list = xzalloc(8 * sizeof(group_list[0]));
498 fp = fopen_for_read(_PATH_GROUP);
501 while ((buf = parse_common(fp, _PATH_GROUP, sizeof(GR_DEF)-1, NULL, 0)) != NULL) {
504 if (!convert_to_struct(&S.db[1], buf, &group))
506 if (group.gr_gid == gid)
508 for (m = group.gr_mem; *m; m++) {
509 if (strcmp(*m, user) != 0)
511 group_list = xrealloc_vector(group_list, /*8=2^3:*/ 3, ngroups);
512 group_list[ngroups++] = group.gr_gid;
520 *ngroups_ptr = ngroups;
524 int FAST_FUNC initgroups(const char *user, gid_t gid)
527 gid_t *group_list = getgrouplist_internal(&ngroups, user, gid);
529 ngroups = setgroups(ngroups, group_list);
534 int FAST_FUNC getgrouplist(const char *user, gid_t gid, gid_t *groups, int *ngroups)
536 int ngroups_old = *ngroups;
537 gid_t *group_list = getgrouplist_internal(ngroups, user, gid);
539 if (*ngroups <= ngroups_old) {
540 ngroups_old = *ngroups;
541 memcpy(groups, group_list, ngroups_old * sizeof(groups[0]));