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 static struct statics *get_S(void)
122 if (!ptr_to_statics) {
123 ptr_to_statics = xzalloc(sizeof(S));
124 memcpy(&S.db[0], &const_pw_db, sizeof(const_pw_db));
125 memcpy(&S.db[1], &const_gr_db, sizeof(const_gr_db));
126 #if ENABLE_USE_BB_SHADOW
127 memcpy(&S.db[2], &const_sp_db, sizeof(const_sp_db));
130 return ptr_to_statics;
133 /* Internal functions */
135 /* Divide the passwd/group/shadow record in fields
136 * by substituting the given delimeter
137 * e.g. ':' or ',' with '\0'.
138 * Returns the number of fields found.
139 * Strips leading and trailing whitespace in fields.
141 static int tokenize(char *buffer, int ch)
149 overlapping_strcpy(s, skip_whitespace(s));
151 if (*p == ch || *p == '\0') {
153 while (p != s && isblank(p[-1]))
156 overlapping_strcpy(p, end);
159 S.tokenize_end = p + 1;
169 /* Returns !NULL on success and matching line broken up in fields by '\0' in buf.
170 * We require the expected number of fields to be found.
172 static char *parse_common(FILE *fp, const char *filename,
174 const char *key, int field_pos)
179 while ((buf = xmalloc_fgetline(fp)) != NULL) {
181 /* Skip empty lines, comment lines */
182 if (buf[0] == '\0' || buf[0] == '#')
184 if (tokenize(buf, ':') != n_fields) {
185 /* number of fields is wrong */
186 bb_error_msg("bad record at %s:%u", filename, count);
190 /* Ugly hack: group db requires aqdditional buffer space
191 * for members[] array. If there is only one group, we need space
192 * for 3 pointers: alignment padding, group name, NULL.
193 * +1 for every additional group.
195 if (n_fields == sizeof(GR_DEF)-1) { /* if we read group file */
201 resize *= sizeof(char**);
202 resize += S.tokenize_end - buf;
203 buf = xrealloc(buf, resize);
207 /* no key specified: sequential read, return a record */
210 if (strcmp(key, nth_string(buf, field_pos)) == 0) {
221 static char *parse_file(const char *filename,
223 const char *key, int field_pos)
226 FILE *fp = fopen_for_read(filename);
229 buf = parse_common(fp, filename, n_fields, key, field_pos);
235 /* Convert passwd/group/shadow file record in buffer to a struct */
236 static void *convert_to_struct(struct passdb *db,
237 char *buffer, void *result)
239 const char *def = db->def;
240 const uint8_t *off = db->off;
242 /* TODO? for consistency, zero out all fields */
243 /* memset(result, 0, size_of_result);*/
246 void *member = (char*)result + (*off++);
248 if ((*def | 0x20) == 's') { /* s or S */
249 *(char **)member = (char*)buffer;
250 if (!buffer[0] && (*def == 'S')) {
255 *(int *)member = bb_strtou(buffer, NULL, 10);
257 #if ENABLE_USE_BB_SHADOW
261 n = bb_strtol(buffer, NULL, 10);
267 int i = tokenize(buffer, ',');
269 /* Store members[] after buffer's end.
270 * This is safe ONLY because there is a hack
271 * in parse_common() which allocates additional space
272 * at the end of malloced buffer!
275 ( ((intptr_t)S.tokenize_end + sizeof(char**))
276 & -(intptr_t)sizeof(char**)
279 ((struct group *)result)->gr_mem = members;
282 buffer += strlen(buffer) + 1;
286 /* def "r" does nothing */
291 buffer += strlen(buffer) + 1;
299 /****** getXXnam/id_r */
301 static int FAST_FUNC getXXnam_r(const char *name, uintptr_t db_and_field_pos,
302 char *buffer, size_t buflen,
305 void *struct_buf = *(void**)result;
309 db = &S.db[db_and_field_pos >> 2];
311 *(void**)result = NULL;
312 buf = parse_file(db->filename, db->numfields, name, db_and_field_pos & 3);
314 size_t size = S.tokenize_end - buf;
318 memcpy(buffer, buf, size);
319 *(void**)result = convert_to_struct(db, buffer, struct_buf);
323 /* "The reentrant functions return zero on success.
324 * In case of error, an error number is returned."
325 * NB: not finding the record is also a "success" here:
330 int FAST_FUNC getpwnam_r(const char *name, struct passwd *struct_buf,
331 char *buffer, size_t buflen,
332 struct passwd **result)
334 /* Why the "store buffer address in result" trick?
335 * This way, getXXnam_r has the same ABI signature as getpwnam_r,
336 * hopefully compiler can optimize tail call better in this case.
338 *result = struct_buf;
339 return getXXnam_r(name, (0 << 2) + 0, buffer, buflen, result);
341 #if ENABLE_USE_BB_SHADOW
342 int FAST_FUNC getspnam_r(const char *name, struct spwd *struct_buf, char *buffer, size_t buflen,
343 struct spwd **result)
345 *result = struct_buf;
346 return getXXnam_r(name, (2 << 2) + 0, buffer, buflen, result);
350 /****** getXXent_r */
352 static int FAST_FUNC getXXent_r(void *struct_buf, char *buffer, size_t buflen,
361 *(void**)result = NULL;
364 db->fp = fopen_for_read(db->filename);
368 close_on_exec_on(fileno(db->fp));
371 buf = parse_common(db->fp, db->filename, db->numfields, /*no search key:*/ NULL, 0);
373 size_t size = S.tokenize_end - buf;
377 memcpy(buffer, buf, size);
378 *(void**)result = convert_to_struct(db, buffer, struct_buf);
382 /* "The reentrant functions return zero on success.
383 * In case of error, an error number is returned."
384 * NB: not finding the record is also a "success" here:
389 int FAST_FUNC getpwent_r(struct passwd *struct_buf, char *buffer, size_t buflen, struct passwd **result)
391 return getXXent_r(struct_buf, buffer, buflen, result, 0);
394 /****** getXXnam/id */
396 static void* FAST_FUNC getXXnam(const char *name, unsigned db_and_field_pos)
402 db = &S.db[db_and_field_pos >> 2];
407 db->fp = fopen_for_read(db->filename);
411 close_on_exec_on(fileno(db->fp));
414 buf = parse_common(db->fp, db->filename, db->numfields, name, db_and_field_pos & 3);
418 result = convert_to_struct(db, buf, db->struct_result);
423 struct passwd* FAST_FUNC getpwnam(const char *name)
425 return getXXnam(name, (0 << 2) + 0);
427 struct group* FAST_FUNC getgrnam(const char *name)
429 return getXXnam(name, (1 << 2) + 0);
431 struct passwd* FAST_FUNC getpwuid(uid_t id)
433 return getXXnam(utoa(id), (0 << 2) + 2);
435 struct group* FAST_FUNC getgrgid(gid_t id)
437 return getXXnam(utoa(id), (1 << 2) + 2);
440 /****** end/setXXend */
442 void FAST_FUNC endpwent(void)
444 if (has_S && S.db[0].fp) {
449 void FAST_FUNC setpwent(void)
451 if (has_S && S.db[0].fp) {
455 void FAST_FUNC endgrent(void)
457 if (has_S && S.db[1].fp) {
463 /****** initgroups and getgrouplist */
465 static gid_t* FAST_FUNC getgrouplist_internal(int *ngroups_ptr,
466 const char *user, gid_t gid)
474 /* We alloc space for 8 gids at a time. */
475 group_list = xzalloc(8 * sizeof(group_list[0]));
479 fp = fopen_for_read(_PATH_GROUP);
482 while ((buf = parse_common(fp, _PATH_GROUP, sizeof(GR_DEF)-1, NULL, 0)) != NULL) {
485 if (!convert_to_struct(&S.db[1], buf, &group))
487 if (group.gr_gid == gid)
489 for (m = group.gr_mem; *m; m++) {
490 if (strcmp(*m, user) != 0)
492 group_list = xrealloc_vector(group_list, /*8=2^3:*/ 3, ngroups);
493 group_list[ngroups++] = group.gr_gid;
501 *ngroups_ptr = ngroups;
505 int FAST_FUNC initgroups(const char *user, gid_t gid)
508 gid_t *group_list = getgrouplist_internal(&ngroups, user, gid);
510 ngroups = setgroups(ngroups, group_list);
515 int FAST_FUNC getgrouplist(const char *user, gid_t gid, gid_t *groups, int *ngroups)
517 int ngroups_old = *ngroups;
518 gid_t *group_list = getgrouplist_internal(ngroups, user, gid);
520 if (*ngroups <= ngroups_old) {
521 ngroups_old = *ngroups;
522 memcpy(groups, group_list, ngroups_old * sizeof(groups[0]));