blob: b44ada4323ecd041e36cf7590f24f99679790e9a [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Denys Vlasenko2ab94032017-10-05 15:33:28 +02002/*
3 * Copyright (C) 2014 Tito Ragusa <farmatito@tiscali.it>
Eric Andersen9615a082004-07-15 12:53:49 +00004 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02005 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Eric Andersen9615a082004-07-15 12:53:49 +00006 */
Tito Ragusa1da09cf2015-01-02 21:37:59 +01007/* This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY!!
Eric Andersen9615a082004-07-15 12:53:49 +00009 *
Tito Ragusa1da09cf2015-01-02 21:37:59 +010010 * Rewrite of some parts. Main differences are:
Eric Andersen9615a082004-07-15 12:53:49 +000011 *
Tito Ragusa1da09cf2015-01-02 21:37:59 +010012 * 1) the buffer for getpwuid, getgrgid, getpwnam, getgrnam is dynamically
Denys Vlasenkoc5d4a042015-01-05 15:09:04 +010013 * allocated.
Tito Ragusa1da09cf2015-01-02 21:37:59 +010014 * If ENABLE_FEATURE_CLEAN_UP is set the buffers are freed at program
15 * exit using the atexit function to make valgrind happy.
16 * 2) the passwd/group files:
17 * a) must contain the expected number of fields (as per count of field
Denys Vlasenko10ad6222017-04-17 16:13:32 +020018 * delimiters ":") or we will complain with a error message.
Denys Vlasenkoc5d4a042015-01-05 15:09:04 +010019 * b) leading and trailing whitespace in fields is stripped.
Denys Vlasenkob5dabd92015-10-13 00:31:02 +020020 * c) some fields are not allowed to be empty (e.g. username, uid/gid),
21 * and in this case NULL is returned and errno is set to EINVAL.
22 * This behaviour could be easily changed by modifying PW_DEF, GR_DEF,
23 * SP_DEF strings (uppercase makes a field mandatory).
Tito Ragusa1da09cf2015-01-02 21:37:59 +010024 * d) the string representing uid/gid must be convertible by strtoXX
Denys Vlasenko31d67342015-01-03 15:15:47 +010025 * functions, or errno is set to EINVAL.
Denys Vlasenkoc5d4a042015-01-05 15:09:04 +010026 * e) leading and trailing whitespace in group member names is stripped.
Denys Vlasenko31d67342015-01-03 15:15:47 +010027 * 3) the internal function for getgrouplist uses dynamically allocated buffer.
28 * 4) at the moment only the functions really used by busybox code are
Tito Ragusa1da09cf2015-01-02 21:37:59 +010029 * implemented, if you need a particular missing function it should be
30 * easy to write it by using the internal common code.
Eric Andersen9615a082004-07-15 12:53:49 +000031 */
Rob Landleyea224be2006-06-18 20:20:07 +000032#include "libbb.h"
Bernhard Reutner-Fischerfa939aa2006-04-05 16:21:37 +000033
Denys Vlasenko8d547ac2015-01-03 15:54:04 +010034struct const_passdb {
35 const char *filename;
Denys Vlasenkoc5d4a042015-01-05 15:09:04 +010036 char def[7 + 2*ENABLE_USE_BB_SHADOW];
37 uint8_t off[7 + 2*ENABLE_USE_BB_SHADOW];
Denys Vlasenko8d547ac2015-01-03 15:54:04 +010038 uint8_t numfields;
Denys Vlasenko134c5302015-01-03 20:47:47 +010039 uint8_t size_of;
Denys Vlasenko8d547ac2015-01-03 15:54:04 +010040};
41struct passdb {
42 const char *filename;
Denys Vlasenkoc5d4a042015-01-05 15:09:04 +010043 char def[7 + 2*ENABLE_USE_BB_SHADOW];
44 uint8_t off[7 + 2*ENABLE_USE_BB_SHADOW];
Denys Vlasenko8d547ac2015-01-03 15:54:04 +010045 uint8_t numfields;
Denys Vlasenko134c5302015-01-03 20:47:47 +010046 uint8_t size_of;
Denys Vlasenko8d547ac2015-01-03 15:54:04 +010047 FILE *fp;
48 char *malloced;
Denys Vlasenko8d547ac2015-01-03 15:54:04 +010049};
Denys Vlasenko134c5302015-01-03 20:47:47 +010050/* Note: for shadow db, def[] will not contain terminating NUL,
Denys Vlasenko20c0a162015-01-03 19:12:49 +010051 * 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.
54 */
Denys Vlasenko8d547ac2015-01-03 15:54:04 +010055
Denys Vlasenko31d67342015-01-03 15:15:47 +010056/* S = string not empty, s = string maybe empty,
57 * I = uid,gid, l = long maybe empty, m = members,
58 * r = reserved
59 */
Denys Vlasenko5de45022015-10-12 04:06:18 +020060#define PW_DEF "SsIIsss"
Tito Ragusa1da09cf2015-01-02 21:37:59 +010061#define GR_DEF "SsIm"
62#define SP_DEF "Ssllllllr"
Eric Andersen9615a082004-07-15 12:53:49 +000063
Denys Vlasenko8d547ac2015-01-03 15:54:04 +010064static const struct const_passdb const_pw_db = {
65 _PATH_PASSWD, PW_DEF,
66 {
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 */
Denys Vlasenkob5dabd92015-10-13 00:31:02 +020072 offsetof(struct passwd, pw_dir), /* 5 s */
73 offsetof(struct passwd, pw_shell) /* 6 s */
Denys Vlasenko8d547ac2015-01-03 15:54:04 +010074 },
Denys Vlasenko134c5302015-01-03 20:47:47 +010075 sizeof(PW_DEF)-1, sizeof(struct passwd)
Tito Ragusa1da09cf2015-01-02 21:37:59 +010076};
Denys Vlasenko8d547ac2015-01-03 15:54:04 +010077static const struct const_passdb const_gr_db = {
78 _PATH_GROUP, GR_DEF,
79 {
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 **) */
84 },
Denys Vlasenko134c5302015-01-03 20:47:47 +010085 sizeof(GR_DEF)-1, sizeof(struct group)
Tito Ragusa1da09cf2015-01-02 21:37:59 +010086};
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +000087#if ENABLE_USE_BB_SHADOW
Denys Vlasenko8d547ac2015-01-03 15:54:04 +010088static const struct const_passdb const_sp_db = {
89 _PATH_SHADOW, SP_DEF,
90 {
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 */
100 },
Denys Vlasenko134c5302015-01-03 20:47:47 +0100101 sizeof(SP_DEF)-1, sizeof(struct spwd)
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100102};
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000103#endif
104
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100105/* We avoid having big global data. */
Denis Vlasenko2c91efb2007-06-18 10:08:27 +0000106struct statics {
Denys Vlasenkoc5d4a042015-01-05 15:09:04 +0100107 /* We use same buffer (db[0].malloced) for getpwuid and getpwnam.
Denys Vlasenko31d67342015-01-03 15:15:47 +0100108 * Manpage says:
Denys Vlasenkoacdb0042012-01-06 16:24:56 +0100109 * "The return value may point to a static area, and may be overwritten
110 * by subsequent calls to getpwent(), getpwnam(), or getpwuid()."
111 */
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100112 struct passdb db[2 + ENABLE_USE_BB_SHADOW];
113 char *tokenize_end;
Denys Vlasenko134c5302015-01-03 20:47:47 +0100114 unsigned string_size;
Denis Vlasenko2c91efb2007-06-18 10:08:27 +0000115};
116
117static struct statics *ptr_to_statics;
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100118#define S (*ptr_to_statics)
119#define has_S (ptr_to_statics)
Denis Vlasenko2c91efb2007-06-18 10:08:27 +0000120
Denys Vlasenko9dca6ac2015-01-03 16:09:05 +0100121#if ENABLE_FEATURE_CLEAN_UP
122static void free_static(void)
123{
Denys Vlasenko6390a3a2015-10-13 01:51:37 +0200124 free(S.db[0].malloced);
Denys Vlasenko9dca6ac2015-01-03 16:09:05 +0100125 free(S.db[1].malloced);
126# if ENABLE_USE_BB_SHADOW
Denys Vlasenko20c0a162015-01-03 19:12:49 +0100127 free(S.db[2].malloced);
Denys Vlasenko9dca6ac2015-01-03 16:09:05 +0100128# endif
129 free(ptr_to_statics);
130}
131#endif
132
Denis Vlasenko2c91efb2007-06-18 10:08:27 +0000133static struct statics *get_S(void)
134{
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100135 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));
141#endif
Denys Vlasenko9dca6ac2015-01-03 16:09:05 +0100142#if ENABLE_FEATURE_CLEAN_UP
143 atexit(free_static);
144#endif
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100145 }
Denis Vlasenko2c91efb2007-06-18 10:08:27 +0000146 return ptr_to_statics;
147}
148
Denys Vlasenko31d67342015-01-03 15:15:47 +0100149/* Internal functions */
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100150
151/* Divide the passwd/group/shadow record in fields
Denys Vlasenko10ad6222017-04-17 16:13:32 +0200152 * by substituting the given delimiter
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100153 * e.g. ':' or ',' with '\0'.
Denys Vlasenko31d67342015-01-03 15:15:47 +0100154 * Returns the number of fields found.
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100155 * Strips leading and trailing whitespace in fields.
Eric Andersen9615a082004-07-15 12:53:49 +0000156 */
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100157static int tokenize(char *buffer, int ch)
Eric Andersen9615a082004-07-15 12:53:49 +0000158{
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100159 char *p = buffer;
160 char *s = p;
161 int num_fields = 0;
Eric Andersen9615a082004-07-15 12:53:49 +0000162
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100163 for (;;) {
164 if (isblank(*s)) {
165 overlapping_strcpy(s, skip_whitespace(s));
Eric Andersen9615a082004-07-15 12:53:49 +0000166 }
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100167 if (*p == ch || *p == '\0') {
168 char *end = p;
169 while (p != s && isblank(p[-1]))
170 p--;
171 if (p != end)
172 overlapping_strcpy(p, end);
173 num_fields++;
174 if (*end == '\0') {
175 S.tokenize_end = p + 1;
176 return num_fields;
177 }
178 *p = '\0';
179 s = p + 1;
180 }
181 p++;
Eric Andersen9615a082004-07-15 12:53:49 +0000182 }
Eric Andersen9615a082004-07-15 12:53:49 +0000183}
184
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100185/* 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.
187 */
Denys Vlasenko5acf1342015-01-04 02:02:39 +0100188static char *parse_common(FILE *fp, struct passdb *db,
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100189 const char *key, int field_pos)
190{
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100191 char *buf;
Eric Andersen9615a082004-07-15 12:53:49 +0000192
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100193 while ((buf = xmalloc_fgetline(fp)) != NULL) {
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100194 /* Skip empty lines, comment lines */
195 if (buf[0] == '\0' || buf[0] == '#')
196 goto free_and_next;
Denys Vlasenko5acf1342015-01-04 02:02:39 +0100197 if (tokenize(buf, ':') != db->numfields) {
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100198 /* number of fields is wrong */
Tito Ragusae5cae082015-01-06 01:22:36 +0100199 bb_error_msg("%s: bad record", db->filename);
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100200 goto free_and_next;
201 }
Eric Andersen9615a082004-07-15 12:53:49 +0000202
Denys Vlasenkoc5d4a042015-01-05 15:09:04 +0100203 if (field_pos == -1) {
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100204 /* no key specified: sequential read, return a record */
205 break;
206 }
207 if (strcmp(key, nth_string(buf, field_pos)) == 0) {
208 /* record found */
209 break;
210 }
211 free_and_next:
212 free(buf);
213 }
214
Denys Vlasenkof9936672015-01-03 21:03:39 +0100215 S.string_size = S.tokenize_end - buf;
216/*
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.
221 */
Denys Vlasenko5acf1342015-01-04 02:02:39 +0100222 if (buf && db->numfields == sizeof(GR_DEF)-1) { /* if we read group file... */
Denys Vlasenkof9936672015-01-03 21:03:39 +0100223 int cnt = 3;
224 char *p = buf;
225 while (p < S.tokenize_end)
226 if (*p++ == ',')
227 cnt++;
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);
231 }
232
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100233 return buf;
234}
235
Denys Vlasenko5acf1342015-01-04 02:02:39 +0100236static char *parse_file(struct passdb *db,
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100237 const char *key, int field_pos)
238{
239 char *buf = NULL;
Denys Vlasenko5acf1342015-01-04 02:02:39 +0100240 FILE *fp = fopen_for_read(db->filename);
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100241
242 if (fp) {
Denys Vlasenko5acf1342015-01-04 02:02:39 +0100243 buf = parse_common(fp, db, key, field_pos);
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100244 fclose(fp);
245 }
246 return buf;
247}
248
249/* Convert passwd/group/shadow file record in buffer to a struct */
Denys Vlasenko8d547ac2015-01-03 15:54:04 +0100250static void *convert_to_struct(struct passdb *db,
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100251 char *buffer, void *result)
252{
Denys Vlasenko8d547ac2015-01-03 15:54:04 +0100253 const char *def = db->def;
254 const uint8_t *off = db->off;
255
Denys Vlasenko134c5302015-01-03 20:47:47 +0100256 /* For consistency, zero out all fields */
257 memset(result, 0, db->size_of);
Denys Vlasenko8d547ac2015-01-03 15:54:04 +0100258
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100259 for (;;) {
260 void *member = (char*)result + (*off++);
261
262 if ((*def | 0x20) == 's') { /* s or S */
263 *(char **)member = (char*)buffer;
264 if (!buffer[0] && (*def == 'S')) {
265 errno = EINVAL;
266 }
267 }
268 if (*def == 'I') {
269 *(int *)member = bb_strtou(buffer, NULL, 10);
270 }
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000271#if ENABLE_USE_BB_SHADOW
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100272 if (*def == 'l') {
273 long n = -1;
274 if (buffer[0])
275 n = bb_strtol(buffer, NULL, 10);
276 *(long *)member = n;
277 }
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000278#endif
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100279 if (*def == 'm') {
280 char **members;
281 int i = tokenize(buffer, ',');
Eric Andersen9615a082004-07-15 12:53:49 +0000282
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100283 /* 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!
287 */
288 members = (char **)
Denys Vlasenko20c0a162015-01-03 19:12:49 +0100289 ( ((intptr_t)S.tokenize_end + sizeof(members[0]))
290 & -(intptr_t)sizeof(members[0])
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100291 );
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100292 ((struct group *)result)->gr_mem = members;
293 while (--i >= 0) {
Denys Vlasenko12fc8692015-01-03 21:16:18 +0100294 if (buffer[0]) {
295 *members++ = buffer;
296 // bb_error_msg("member[]='%s'", buffer);
297 }
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100298 buffer += strlen(buffer) + 1;
299 }
300 *members = NULL;
301 }
302 /* def "r" does nothing */
Eric Andersen9615a082004-07-15 12:53:49 +0000303
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100304 def++;
Denys Vlasenko134c5302015-01-03 20:47:47 +0100305 if ((unsigned char)*def <= (unsigned char)' ')
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100306 break;
307 buffer += strlen(buffer) + 1;
Eric Andersen9615a082004-07-15 12:53:49 +0000308 }
309
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100310 if (errno)
311 result = NULL;
312 return result;
Eric Andersen9615a082004-07-15 12:53:49 +0000313}
314
Denys Vlasenkodb4d1052015-01-04 02:34:52 +0100315static int massage_data_for_r_func(struct passdb *db,
Denys Vlasenko8d547ac2015-01-03 15:54:04 +0100316 char *buffer, size_t buflen,
Denys Vlasenkodb4d1052015-01-04 02:34:52 +0100317 void **result,
318 char *buf)
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100319{
Denys Vlasenkodb4d1052015-01-04 02:34:52 +0100320 void *result_buf = *result;
321 *result = NULL;
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100322 if (buf) {
Denys Vlasenkodb4d1052015-01-04 02:34:52 +0100323 if (S.string_size > buflen) {
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100324 errno = ERANGE;
325 } else {
Denys Vlasenkodb4d1052015-01-04 02:34:52 +0100326 memcpy(buffer, buf, S.string_size);
327 *result = convert_to_struct(db, buffer, result_buf);
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100328 }
329 free(buf);
330 }
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:
334 */
335 return errno;
336}
337
Denys Vlasenko23cfaab2015-02-07 21:21:02 +0100338static void* massage_data_for_non_r_func(struct passdb *db, char *buf)
339{
340 if (!buf)
341 return NULL;
342
343 free(db->malloced);
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.
348 */
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);
352}
353
Denys Vlasenkodb4d1052015-01-04 02:34:52 +0100354/****** getXXnam/id_r */
355
356static int FAST_FUNC getXXnam_r(const char *name, uintptr_t db_and_field_pos,
357 char *buffer, size_t buflen,
358 void *result)
359{
360 char *buf;
361 struct passdb *db = &get_S()->db[db_and_field_pos >> 2];
362
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 */
366
367 return massage_data_for_r_func(db, buffer, buflen, result, buf);
368}
369
Denys Vlasenko8d547ac2015-01-03 15:54:04 +0100370int FAST_FUNC getpwnam_r(const char *name, struct passwd *struct_buf,
371 char *buffer, size_t buflen,
372 struct passwd **result)
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100373{
374 /* Why the "store buffer address in result" trick?
375 * This way, getXXnam_r has the same ABI signature as getpwnam_r,
Denys Vlasenko908b6e52015-01-02 22:31:07 +0100376 * hopefully compiler can optimize tail call better in this case.
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100377 */
378 *result = struct_buf;
379 return getXXnam_r(name, (0 << 2) + 0, buffer, buflen, result);
380}
381#if ENABLE_USE_BB_SHADOW
Denys Vlasenko908b6e52015-01-02 22:31:07 +0100382int FAST_FUNC getspnam_r(const char *name, struct spwd *struct_buf, char *buffer, size_t buflen,
Denys Vlasenko31d67342015-01-03 15:15:47 +0100383 struct spwd **result)
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100384{
385 *result = struct_buf;
386 return getXXnam_r(name, (2 << 2) + 0, buffer, buflen, result);
Eric Andersen9615a082004-07-15 12:53:49 +0000387}
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000388#endif
Eric Andersen9615a082004-07-15 12:53:49 +0000389
Denys Vlasenko23cfaab2015-02-07 21:21:02 +0100390#ifdef UNUSED
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100391/****** getXXent_r */
392
Denys Vlasenkodb4d1052015-01-04 02:34:52 +0100393static int FAST_FUNC getXXent_r(uintptr_t db_idx, char *buffer, size_t buflen,
394 void *result)
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100395{
396 char *buf;
Denys Vlasenko5acf1342015-01-04 02:02:39 +0100397 struct passdb *db = &get_S()->db[db_idx];
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100398
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100399 if (!db->fp) {
400 db->fp = fopen_for_read(db->filename);
401 if (!db->fp) {
402 return errno;
403 }
404 close_on_exec_on(fileno(db->fp));
405 }
406
Denys Vlasenkoc5d4a042015-01-05 15:09:04 +0100407 buf = parse_common(db->fp, db, /*no search key:*/ NULL, -1);
408 if (!buf && !errno)
409 errno = ENOENT;
Denys Vlasenkodb4d1052015-01-04 02:34:52 +0100410 return massage_data_for_r_func(db, buffer, buflen, result, buf);
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100411}
412
Denys Vlasenkodb4d1052015-01-04 02:34:52 +0100413int FAST_FUNC getpwent_r(struct passwd *struct_buf, char *buffer, size_t buflen,
414 struct passwd **result)
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100415{
Denys Vlasenkodb4d1052015-01-04 02:34:52 +0100416 *result = struct_buf;
417 return getXXent_r(0, buffer, buflen, result);
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100418}
Denys Vlasenko23cfaab2015-02-07 21:21:02 +0100419#endif
420
421/****** getXXent */
422
423static void* FAST_FUNC getXXent(uintptr_t db_idx)
424{
425 char *buf;
426 struct passdb *db = &get_S()->db[db_idx];
427
428 if (!db->fp) {
429 db->fp = fopen_for_read(db->filename);
430 if (!db->fp) {
431 return NULL;
432 }
433 close_on_exec_on(fileno(db->fp));
434 }
435
436 buf = parse_common(db->fp, db, /*no search key:*/ NULL, -1);
437 return massage_data_for_non_r_func(db, buf);
438}
439
440struct passwd* FAST_FUNC getpwent(void)
441{
442 return getXXent(0);
443}
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100444
445/****** getXXnam/id */
446
Denys Vlasenko908b6e52015-01-02 22:31:07 +0100447static void* FAST_FUNC getXXnam(const char *name, unsigned db_and_field_pos)
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100448{
449 char *buf;
Denys Vlasenko5acf1342015-01-04 02:02:39 +0100450 struct passdb *db = &get_S()->db[db_and_field_pos >> 2];
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100451
Tito Ragusacb6a1122015-02-19 22:02:59 +0100452 buf = parse_file(db, name, db_and_field_pos & 3);
Denys Vlasenko23cfaab2015-02-07 21:21:02 +0100453 return massage_data_for_non_r_func(db, buf);
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100454}
455
Denys Vlasenko908b6e52015-01-02 22:31:07 +0100456struct passwd* FAST_FUNC getpwnam(const char *name)
Eric Andersen9615a082004-07-15 12:53:49 +0000457{
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100458 return getXXnam(name, (0 << 2) + 0);
Eric Andersen9615a082004-07-15 12:53:49 +0000459}
Denys Vlasenko908b6e52015-01-02 22:31:07 +0100460struct group* FAST_FUNC getgrnam(const char *name)
Eric Andersen9615a082004-07-15 12:53:49 +0000461{
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100462 return getXXnam(name, (1 << 2) + 0);
Eric Andersen9615a082004-07-15 12:53:49 +0000463}
Denys Vlasenko908b6e52015-01-02 22:31:07 +0100464struct passwd* FAST_FUNC getpwuid(uid_t id)
Eric Andersen9615a082004-07-15 12:53:49 +0000465{
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100466 return getXXnam(utoa(id), (0 << 2) + 2);
Eric Andersen9615a082004-07-15 12:53:49 +0000467}
Denys Vlasenko908b6e52015-01-02 22:31:07 +0100468struct group* FAST_FUNC getgrgid(gid_t id)
Eric Andersen9615a082004-07-15 12:53:49 +0000469{
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100470 return getXXnam(utoa(id), (1 << 2) + 2);
Eric Andersen9615a082004-07-15 12:53:49 +0000471}
472
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100473/****** end/setXXend */
474
Denys Vlasenko908b6e52015-01-02 22:31:07 +0100475void FAST_FUNC endpwent(void)
Eric Andersen9615a082004-07-15 12:53:49 +0000476{
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100477 if (has_S && S.db[0].fp) {
478 fclose(S.db[0].fp);
479 S.db[0].fp = NULL;
Eric Andersen9615a082004-07-15 12:53:49 +0000480 }
481}
Denys Vlasenko908b6e52015-01-02 22:31:07 +0100482void FAST_FUNC setpwent(void)
Eric Andersen9615a082004-07-15 12:53:49 +0000483{
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100484 if (has_S && S.db[0].fp) {
485 rewind(S.db[0].fp);
Eric Andersen9615a082004-07-15 12:53:49 +0000486 }
Eric Andersen9615a082004-07-15 12:53:49 +0000487}
Denys Vlasenko908b6e52015-01-02 22:31:07 +0100488void FAST_FUNC endgrent(void)
Eric Andersen9615a082004-07-15 12:53:49 +0000489{
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100490 if (has_S && S.db[1].fp) {
491 fclose(S.db[1].fp);
492 S.db[1].fp = NULL;
Eric Andersen9615a082004-07-15 12:53:49 +0000493 }
494}
495
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100496/****** initgroups and getgrouplist */
497
498static gid_t* FAST_FUNC getgrouplist_internal(int *ngroups_ptr,
499 const char *user, gid_t gid)
Eric Andersen9615a082004-07-15 12:53:49 +0000500{
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100501 FILE *fp;
Eric Andersen9615a082004-07-15 12:53:49 +0000502 gid_t *group_list;
Denis Vlasenko22284262008-09-18 00:56:24 +0000503 int ngroups;
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100504
Denis Vlasenko22284262008-09-18 00:56:24 +0000505 /* We alloc space for 8 gids at a time. */
Denys Vlasenko31d67342015-01-03 15:15:47 +0100506 group_list = xzalloc(8 * sizeof(group_list[0]));
Denis Vlasenko22284262008-09-18 00:56:24 +0000507 group_list[0] = gid;
508 ngroups = 1;
509
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100510 fp = fopen_for_read(_PATH_GROUP);
511 if (fp) {
Denys Vlasenko5acf1342015-01-04 02:02:39 +0100512 struct passdb *db = &get_S()->db[1];
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100513 char *buf;
Denys Vlasenkoc5d4a042015-01-05 15:09:04 +0100514 while ((buf = parse_common(fp, db, NULL, -1)) != NULL) {
Denis Vlasenko22284262008-09-18 00:56:24 +0000515 char **m;
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100516 struct group group;
Denys Vlasenko5acf1342015-01-04 02:02:39 +0100517 if (!convert_to_struct(db, buf, &group))
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100518 goto next;
Denis Vlasenko22284262008-09-18 00:56:24 +0000519 if (group.gr_gid == gid)
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100520 goto next;
Denis Vlasenko22284262008-09-18 00:56:24 +0000521 for (m = group.gr_mem; *m; m++) {
522 if (strcmp(*m, user) != 0)
523 continue;
Denys Vlasenko17fcd722010-03-31 12:37:43 +0200524 group_list = xrealloc_vector(group_list, /*8=2^3:*/ 3, ngroups);
Denis Vlasenko22284262008-09-18 00:56:24 +0000525 group_list[ngroups++] = group.gr_gid;
Denys Vlasenkoc5d4a042015-01-05 15:09:04 +0100526 goto next;
Eric Andersen9615a082004-07-15 12:53:49 +0000527 }
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100528 next:
529 free(buf);
Eric Andersen9615a082004-07-15 12:53:49 +0000530 }
Tito Ragusa1da09cf2015-01-02 21:37:59 +0100531 fclose(fp);
Eric Andersen9615a082004-07-15 12:53:49 +0000532 }
Denis Vlasenko22284262008-09-18 00:56:24 +0000533 *ngroups_ptr = ngroups;
534 return group_list;
535}
Eric Andersen9615a082004-07-15 12:53:49 +0000536
Denys Vlasenko908b6e52015-01-02 22:31:07 +0100537int FAST_FUNC initgroups(const char *user, gid_t gid)
Denis Vlasenko22284262008-09-18 00:56:24 +0000538{
539 int ngroups;
540 gid_t *group_list = getgrouplist_internal(&ngroups, user, gid);
541
Denis Vlasenko22284262008-09-18 00:56:24 +0000542 ngroups = setgroups(ngroups, group_list);
543 free(group_list);
544 return ngroups;
545}
546
Denys Vlasenko908b6e52015-01-02 22:31:07 +0100547int FAST_FUNC getgrouplist(const char *user, gid_t gid, gid_t *groups, int *ngroups)
Denis Vlasenko22284262008-09-18 00:56:24 +0000548{
549 int ngroups_old = *ngroups;
550 gid_t *group_list = getgrouplist_internal(ngroups, user, gid);
551
552 if (*ngroups <= ngroups_old) {
553 ngroups_old = *ngroups;
554 memcpy(groups, group_list, ngroups_old * sizeof(groups[0]));
555 } else {
556 ngroups_old = -1;
557 }
558 free(group_list);
559 return ngroups_old;
Eric Andersen9615a082004-07-15 12:53:49 +0000560}