1 /* vi: set sw=4 ts=4: */
2 /* Copyright (C) 2003 Manuel Novoa III
4 * Licensed under GPL v2, or later. See file LICENSE in this tarball.
7 /* Nov 6, 2003 Initial version.
9 * NOTE: This implementation is quite strict about requiring all
10 * field seperators. It also does not allow leading whitespace
11 * except when processing the numeric fields. glibc is more
12 * lenient. See the various glibc difference comments below.
15 * Move to dynamic allocation of (currently statically allocated)
16 * buffers; especially for the group-related functions since
17 * large group member lists will cause error returns.
25 #define _PATH_SHADOW "/etc/shadow"
28 #define _PATH_PASSWD "/etc/passwd"
31 #define _PATH_GROUP "/etc/group"
34 /**********************************************************************/
35 /* Sizes for statically allocated buffers. */
37 /* If you change these values, also change _SC_GETPW_R_SIZE_MAX and
38 * _SC_GETGR_R_SIZE_MAX in libc/unistd/sysconf.c to match */
39 #define PWD_BUFFER_SIZE 256
40 #define GRP_BUFFER_SIZE 256
42 /**********************************************************************/
43 /* Prototypes for internal functions. */
45 static int bb__pgsreader(
46 int FAST_FUNC (*parserfunc)(void *d, char *line),
48 char *__restrict line_buff,
52 static int FAST_FUNC bb__parsepwent(void *pw, char *line);
53 static int FAST_FUNC bb__parsegrent(void *gr, char *line);
54 #if ENABLE_USE_BB_SHADOW
55 static int FAST_FUNC bb__parsespent(void *sp, char *line);
58 /**********************************************************************/
59 /* We avoid having big global data. */
62 /* Smaller things first */
63 struct passwd getpwuid_resultbuf;
64 struct group getgrgid_resultbuf;
65 struct passwd getpwnam_resultbuf;
66 struct group getgrnam_resultbuf;
68 char getpwuid_buffer[PWD_BUFFER_SIZE];
69 char getgrgid_buffer[GRP_BUFFER_SIZE];
70 char getpwnam_buffer[PWD_BUFFER_SIZE];
71 char getgrnam_buffer[GRP_BUFFER_SIZE];
73 struct passwd fgetpwent_resultbuf;
74 struct group fgetgrent_resultbuf;
75 struct spwd fgetspent_resultbuf;
76 char fgetpwent_buffer[PWD_BUFFER_SIZE];
77 char fgetgrent_buffer[GRP_BUFFER_SIZE];
78 char fgetspent_buffer[PWD_BUFFER_SIZE];
80 #if 0 //ENABLE_USE_BB_SHADOW
81 struct spwd getspuid_resultbuf;
82 struct spwd getspnam_resultbuf;
83 char getspuid_buffer[PWD_BUFFER_SIZE];
84 char getspnam_buffer[PWD_BUFFER_SIZE];
86 // Not converted - too small to bother
87 //pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER;
88 //FILE *pwf /*= NULL*/;
89 //FILE *grf /*= NULL*/;
90 //FILE *spf /*= NULL*/;
92 struct passwd getpwent_pwd;
93 struct group getgrent_gr;
94 char getpwent_line_buff[PWD_BUFFER_SIZE];
95 char getgrent_line_buff[GRP_BUFFER_SIZE];
97 #if 0 //ENABLE_USE_BB_SHADOW
98 struct spwd getspent_spwd;
99 struct spwd sgetspent_spwd;
100 char getspent_line_buff[PWD_BUFFER_SIZE];
101 char sgetspent_line_buff[PWD_BUFFER_SIZE];
105 static struct statics *ptr_to_statics;
107 static struct statics *get_S(void)
110 ptr_to_statics = xzalloc(sizeof(*ptr_to_statics));
111 return ptr_to_statics;
114 /* Always use in this order, get_S() must be called first */
115 #define RESULTBUF(name) &((S = get_S())->name##_resultbuf)
116 #define BUFFER(name) (S->name##_buffer)
118 /**********************************************************************/
119 /* For the various fget??ent_r funcs, return
122 * ENOENT: end-of-file encountered
123 * ERANGE: buflen too small
124 * other error values possible. See bb__pgsreader.
126 * Also, *result == resultbuf on success and NULL on failure.
128 * NOTE: glibc difference - For the ENOENT case, glibc also sets errno.
129 * We do not, as it really isn't an error if we reach the end-of-file.
130 * Doing so is analogous to having fgetc() set errno on EOF.
132 /**********************************************************************/
134 int fgetpwent_r(FILE *__restrict stream, struct passwd *__restrict resultbuf,
135 char *__restrict buffer, size_t buflen,
136 struct passwd **__restrict result)
142 rv = bb__pgsreader(bb__parsepwent, resultbuf, buffer, buflen, stream);
150 int fgetgrent_r(FILE *__restrict stream, struct group *__restrict resultbuf,
151 char *__restrict buffer, size_t buflen,
152 struct group **__restrict result)
158 rv = bb__pgsreader(bb__parsegrent, resultbuf, buffer, buflen, stream);
166 #if ENABLE_USE_BB_SHADOW
167 int fgetspent_r(FILE *__restrict stream, struct spwd *__restrict resultbuf,
168 char *__restrict buffer, size_t buflen,
169 struct spwd **__restrict result)
175 rv = bb__pgsreader(bb__parsespent, resultbuf, buffer, buflen, stream);
184 /**********************************************************************/
185 /* For the various fget??ent funcs, return NULL on failure and a
186 * pointer to the appropriate struct (statically allocated) on success.
187 * TODO: audit & stop using these in bbox, they pull in static buffers */
188 /**********************************************************************/
190 #ifdef UNUSED_SINCE_WE_AVOID_STATIC_BUFS
191 struct passwd *fgetpwent(FILE *stream)
194 struct passwd *resultbuf = RESULTBUF(fgetpwent);
195 char *buffer = BUFFER(fgetpwent);
196 struct passwd *result;
198 fgetpwent_r(stream, resultbuf, buffer, sizeof(BUFFER(fgetpwent)), &result);
202 struct group *fgetgrent(FILE *stream)
205 struct group *resultbuf = RESULTBUF(fgetgrent);
206 char *buffer = BUFFER(fgetgrent);
207 struct group *result;
209 fgetgrent_r(stream, resultbuf, buffer, sizeof(BUFFER(fgetgrent)), &result);
214 #if ENABLE_USE_BB_SHADOW
216 struct spwd *fgetspent(FILE *stream)
219 struct spwd *resultbuf = RESULTBUF(fgetspent);
220 char *buffer = BUFFER(fgetspent);
223 fgetspent_r(stream, resultbuf, buffer, sizeof(BUFFER(fgetspent)), &result);
228 int sgetspent_r(const char *string, struct spwd *result_buf,
229 char *buffer, size_t buflen, struct spwd **result)
235 if (buflen < PWD_BUFFER_SIZE) {
241 if (string != buffer) {
242 if (strlen(string) >= buflen) {
245 strcpy(buffer, string);
248 rv = bb__parsespent(result_buf, buffer);
250 *result = result_buf;
258 /**********************************************************************/
260 #define GETXXKEY_R_FUNC getpwnam_r
261 #define GETXXKEY_R_PARSER bb__parsepwent
262 #define GETXXKEY_R_ENTTYPE struct passwd
263 #define GETXXKEY_R_TEST(ENT) (!strcmp((ENT)->pw_name, key))
264 #define GETXXKEY_R_KEYTYPE const char *__restrict
265 #define GETXXKEY_R_PATHNAME _PATH_PASSWD
266 #include "pwd_grp_internal.c"
268 #define GETXXKEY_R_FUNC getgrnam_r
269 #define GETXXKEY_R_PARSER bb__parsegrent
270 #define GETXXKEY_R_ENTTYPE struct group
271 #define GETXXKEY_R_TEST(ENT) (!strcmp((ENT)->gr_name, key))
272 #define GETXXKEY_R_KEYTYPE const char *__restrict
273 #define GETXXKEY_R_PATHNAME _PATH_GROUP
274 #include "pwd_grp_internal.c"
276 #if ENABLE_USE_BB_SHADOW
277 #define GETXXKEY_R_FUNC getspnam_r
278 #define GETXXKEY_R_PARSER bb__parsespent
279 #define GETXXKEY_R_ENTTYPE struct spwd
280 #define GETXXKEY_R_TEST(ENT) (!strcmp((ENT)->sp_namp, key))
281 #define GETXXKEY_R_KEYTYPE const char *__restrict
282 #define GETXXKEY_R_PATHNAME _PATH_SHADOW
283 #include "pwd_grp_internal.c"
286 #define GETXXKEY_R_FUNC getpwuid_r
287 #define GETXXKEY_R_PARSER bb__parsepwent
288 #define GETXXKEY_R_ENTTYPE struct passwd
289 #define GETXXKEY_R_TEST(ENT) ((ENT)->pw_uid == key)
290 #define GETXXKEY_R_KEYTYPE uid_t
291 #define GETXXKEY_R_PATHNAME _PATH_PASSWD
292 #include "pwd_grp_internal.c"
294 #define GETXXKEY_R_FUNC getgrgid_r
295 #define GETXXKEY_R_PARSER bb__parsegrent
296 #define GETXXKEY_R_ENTTYPE struct group
297 #define GETXXKEY_R_TEST(ENT) ((ENT)->gr_gid == key)
298 #define GETXXKEY_R_KEYTYPE gid_t
299 #define GETXXKEY_R_PATHNAME _PATH_GROUP
300 #include "pwd_grp_internal.c"
302 /**********************************************************************/
303 /* TODO: audit & stop using these in bbox, they pull in static buffers */
305 /* This one has many users */
306 struct passwd *getpwuid(uid_t uid)
309 struct passwd *resultbuf = RESULTBUF(getpwuid);
310 char *buffer = BUFFER(getpwuid);
311 struct passwd *result;
313 getpwuid_r(uid, resultbuf, buffer, sizeof(BUFFER(getpwuid)), &result);
317 /* This one has many users */
318 struct group *getgrgid(gid_t gid)
321 struct group *resultbuf = RESULTBUF(getgrgid);
322 char *buffer = BUFFER(getgrgid);
323 struct group *result;
325 getgrgid_r(gid, resultbuf, buffer, sizeof(BUFFER(getgrgid)), &result);
329 #if 0 //ENABLE_USE_BB_SHADOW
330 /* This function is non-standard and is currently not built. It seems
331 * to have been created as a reentrant version of the non-standard
332 * functions getspuid. Why getspuid was added, I do not know. */
333 int getspuid_r(uid_t uid, struct spwd *__restrict resultbuf,
334 char *__restrict buffer, size_t buflen,
335 struct spwd **__restrict result)
339 struct passwd password;
340 char pwd_buff[PWD_BUFFER_SIZE];
343 rv = getpwuid_r(uid, &password, pwd_buff, sizeof(pwd_buff), &pp);
345 rv = getspnam_r(password.pw_name, resultbuf, buffer, buflen, result);
351 /* This function is non-standard and is currently not built.
352 * Why it was added, I do not know. */
353 struct spwd *getspuid(uid_t uid)
356 struct spwd *resultbuf = RESULTBUF(getspuid);
357 char *buffer = BUFFER(getspuid);
360 getspuid_r(uid, resultbuf, buffer, sizeof(BUFFER(getspuid)), &result);
365 /* This one has many users */
366 struct passwd *getpwnam(const char *name)
369 struct passwd *resultbuf = RESULTBUF(getpwnam);
370 char *buffer = BUFFER(getpwnam);
371 struct passwd *result;
373 getpwnam_r(name, resultbuf, buffer, sizeof(BUFFER(getpwnam)), &result);
377 /* This one has many users */
378 struct group *getgrnam(const char *name)
381 struct group *resultbuf = RESULTBUF(getgrnam);
382 char *buffer = BUFFER(getgrnam);
383 struct group *result;
385 getgrnam_r(name, resultbuf, buffer, sizeof(BUFFER(getgrnam)), &result);
389 #if 0 //ENABLE_USE_BB_SHADOW
390 struct spwd *getspnam(const char *name)
393 struct spwd *resultbuf = RESULTBUF(getspnam);
394 char *buffer = BUFFER(getspnam);
397 getspnam_r(name, resultbuf, buffer, sizeof(BUFFER(getspnam)), &result);
402 /**********************************************************************/
404 /* FIXME: we don't have such CONFIG_xx - ?! */
406 #if defined CONFIG_USE_BB_THREADSAFE_SHADOW && defined PTHREAD_MUTEX_INITIALIZER
407 static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER;
408 # define LOCK pthread_mutex_lock(&mylock)
409 # define UNLOCK pthread_mutex_unlock(&mylock);
411 # define LOCK ((void) 0)
412 # define UNLOCK ((void) 0)
415 static FILE *pwf /*= NULL*/;
436 int getpwent_r(struct passwd *__restrict resultbuf,
437 char *__restrict buffer, size_t buflen,
438 struct passwd **__restrict result)
443 *result = NULL; /* In case of error... */
446 pwf = fopen_for_read(_PATH_PASSWD);
453 rv = bb__pgsreader(bb__parsepwent, resultbuf, buffer, buflen, pwf);
463 static FILE *grf /*= NULL*/;
483 int getgrent_r(struct group *__restrict resultbuf,
484 char *__restrict buffer, size_t buflen,
485 struct group **__restrict result)
490 *result = NULL; /* In case of error... */
493 grf = fopen_for_read(_PATH_GROUP);
500 rv = bb__pgsreader(bb__parsegrent, resultbuf, buffer, buflen, grf);
510 #if ENABLE_USE_BB_SHADOW
511 static FILE *spf /*= NULL*/;
531 int getspent_r(struct spwd *resultbuf, char *buffer,
532 size_t buflen, struct spwd **result)
537 *result = NULL; /* In case of error... */
540 spf = fopen_for_read(_PATH_SHADOW);
547 rv = bb__pgsreader(bb__parsespent, resultbuf, buffer, buflen, spf);
558 #ifdef UNUSED_SINCE_WE_AVOID_STATIC_BUFS
559 struct passwd *getpwent(void)
561 static char line_buff[PWD_BUFFER_SIZE];
562 static struct passwd pwd;
563 struct passwd *result;
565 getpwent_r(&pwd, line_buff, sizeof(line_buff), &result);
569 struct group *getgrent(void)
571 static char line_buff[GRP_BUFFER_SIZE];
572 static struct group gr;
573 struct group *result;
575 getgrent_r(&gr, line_buff, sizeof(line_buff), &result);
580 #if 0 //ENABLE_USE_BB_SHADOW
581 struct spwd *getspent(void)
583 static char line_buff[PWD_BUFFER_SIZE];
584 static struct spwd spwd;
587 getspent_r(&spwd, line_buff, sizeof(line_buff), &result);
591 struct spwd *sgetspent(const char *string)
593 static char line_buff[PWD_BUFFER_SIZE];
594 static struct spwd spwd;
597 sgetspent_r(string, &spwd, line_buff, sizeof(line_buff), &result);
602 static gid_t *getgrouplist_internal(int *ngroups_ptr, const char *user, gid_t gid)
608 char buff[PWD_BUFFER_SIZE];
610 /* We alloc space for 8 gids at a time. */
611 group_list = xmalloc(8 * sizeof(group_list[0]));
615 grfile = fopen_for_read(_PATH_GROUP);
617 while (!bb__pgsreader(bb__parsegrent, &group, buff, sizeof(buff), grfile)) {
619 assert(group.gr_mem); /* Must have at least a NULL terminator. */
620 if (group.gr_gid == gid)
622 for (m = group.gr_mem; *m; m++) {
623 if (strcmp(*m, user) != 0)
625 group_list = xrealloc_vector(group_list, /*8=2^3:*/ 3, ngroups);
626 group_list[ngroups++] = group.gr_gid;
632 *ngroups_ptr = ngroups;
636 int initgroups(const char *user, gid_t gid)
639 gid_t *group_list = getgrouplist_internal(&ngroups, user, gid);
641 ngroups = setgroups(ngroups, group_list);
646 int getgrouplist(const char *user, gid_t gid, gid_t *groups, int *ngroups)
648 int ngroups_old = *ngroups;
649 gid_t *group_list = getgrouplist_internal(ngroups, user, gid);
651 if (*ngroups <= ngroups_old) {
652 ngroups_old = *ngroups;
653 memcpy(groups, group_list, ngroups_old * sizeof(groups[0]));
661 #ifdef UNUSED_SINCE_WE_AVOID_STATIC_BUFS
662 int putpwent(const struct passwd *__restrict p, FILE *__restrict f)
671 /* No extra thread locking is needed above what fprintf does. */
672 if (fprintf(f, "%s:%s:%lu:%lu:%s:%s:%s\n",
673 p->pw_name, p->pw_passwd,
674 (unsigned long)(p->pw_uid),
675 (unsigned long)(p->pw_gid),
676 p->pw_gecos, p->pw_dir, p->pw_shell) >= 0
684 int putgrent(const struct group *__restrict p, FILE *__restrict f)
688 if (!p || !f) { /* Sigh... glibc checks. */
693 if (fprintf(f, "%s:%s:%lu:",
694 p->gr_name, p->gr_passwd,
695 (unsigned long)(p->gr_gid)) >= 0
697 static const char format[] ALIGN1 = ",%s";
709 if (fputc('\n', f) >= 0) {
714 if (fprintf(f, fmt, *m) < 0) {
726 #if ENABLE_USE_BB_SHADOW
727 static const unsigned char put_sp_off[] ALIGN1 = {
728 offsetof(struct spwd, sp_lstchg), /* 2 - not a char ptr */
729 offsetof(struct spwd, sp_min), /* 3 - not a char ptr */
730 offsetof(struct spwd, sp_max), /* 4 - not a char ptr */
731 offsetof(struct spwd, sp_warn), /* 5 - not a char ptr */
732 offsetof(struct spwd, sp_inact), /* 6 - not a char ptr */
733 offsetof(struct spwd, sp_expire) /* 7 - not a char ptr */
736 int putspent(const struct spwd *p, FILE *stream)
743 /* Unlike putpwent and putgrent, glibc does not check the args. */
744 if (fprintf(stream, "%s:%s:", p->sp_namp,
745 (p->sp_pwdp ? p->sp_pwdp : "")) < 0
750 for (i = 0; i < sizeof(put_sp_off); i++) {
752 x = *(long *)((char *)p + put_sp_off[i]);
756 if (fprintf(stream, fmt, x) < 0) {
761 if ((p->sp_flag != ~0UL) && (fprintf(stream, "%lu", p->sp_flag) < 0)) {
765 if (fputc('\n', stream) > 0) {
774 /**********************************************************************/
775 /* Internal uClibc functions. */
776 /**********************************************************************/
778 static const unsigned char pw_off[] ALIGN1 = {
779 offsetof(struct passwd, pw_name), /* 0 */
780 offsetof(struct passwd, pw_passwd), /* 1 */
781 offsetof(struct passwd, pw_uid), /* 2 - not a char ptr */
782 offsetof(struct passwd, pw_gid), /* 3 - not a char ptr */
783 offsetof(struct passwd, pw_gecos), /* 4 */
784 offsetof(struct passwd, pw_dir), /* 5 */
785 offsetof(struct passwd, pw_shell) /* 6 */
788 static int FAST_FUNC bb__parsepwent(void *data, char *line)
796 p = (char *) data + pw_off[i];
798 if (i < 2 || i > 3) {
799 *((char **) p) = line;
803 /* NOTE: glibc difference - glibc allows omission of
804 * ':' seperators after the gid field if all remaining
805 * entries are empty. We require all separators. */
806 line = strchr(line, ':');
811 unsigned long t = strtoul(line, &endptr, 10);
812 /* Make sure we had at least one digit, and that the
813 * failing char is the next field seperator ':'. See
814 * glibc difference note above. */
815 /* TODO: Also check for leading whitespace? */
816 if ((endptr == line) || (*endptr != ':')) {
820 if (i & 1) { /* i == 3 -- gid */
822 } else { /* i == 2 -- uid */
834 /**********************************************************************/
836 static const unsigned char gr_off[] ALIGN1 = {
837 offsetof(struct group, gr_name), /* 0 */
838 offsetof(struct group, gr_passwd), /* 1 */
839 offsetof(struct group, gr_gid) /* 2 - not a char ptr */
842 static int FAST_FUNC bb__parsegrent(void *data, char *line)
850 end_of_buf = ((struct group *) data)->gr_name; /* Evil hack! */
853 p = (char *) data + gr_off[i];
856 *((char **) p) = line;
857 line = strchr(line, ':');
864 *((gid_t *) p) = strtoul(line, &endptr, 10);
866 /* NOTE: glibc difference - glibc allows omission of the
867 * trailing colon when there is no member list. We treat
868 * this as an error. */
870 /* Make sure we had at least one digit, and that the
871 * failing char is the next field seperator ':'. See
872 * glibc difference note above. */
873 if ((endptr == line) || (*endptr != ':')) {
877 i = 1; /* Count terminating NULL ptr. */
880 if (p[1]) { /* We have a member list to process. */
881 /* Overwrite the last ':' with a ',' before counting.
882 * This allows us to test for initial ',' and adds
883 * one ',' so that the ',' count equals the member
887 /* NOTE: glibc difference - glibc allows and trims leading
888 * (but not trailing) space. We treat this as an error. */
889 /* NOTE: glibc difference - glibc allows consecutive and
890 * trailing commas, and ignores "empty string" users. We
891 * treat this as an error. */
894 *p = 0; /* nul-terminate each member string. */
895 if (!*++p || (*p == ',') || isspace(*p)) {
902 /* Now align (p+1), rounding up. */
903 /* Assumes sizeof(char **) is a power of 2. */
904 members = (char **)( (((intptr_t) p) + sizeof(char **))
905 & ~((intptr_t)(sizeof(char **) - 1)) );
907 if (((char *)(members + i)) > end_of_buf) { /* No space. */
911 ((struct group *) data)->gr_mem = members;
914 p = endptr; /* Pointing to char prior to first member. */
933 /**********************************************************************/
935 #if ENABLE_USE_BB_SHADOW
936 static const unsigned char sp_off[] ALIGN1 = {
937 offsetof(struct spwd, sp_namp), /* 0: char* */
938 offsetof(struct spwd, sp_pwdp), /* 1: char* */
939 offsetof(struct spwd, sp_lstchg), /* 2: long */
940 offsetof(struct spwd, sp_min), /* 3: long */
941 offsetof(struct spwd, sp_max), /* 4: long */
942 offsetof(struct spwd, sp_warn), /* 5: long */
943 offsetof(struct spwd, sp_inact), /* 6: long */
944 offsetof(struct spwd, sp_expire), /* 7: long */
945 offsetof(struct spwd, sp_flag) /* 8: unsigned long */
948 static int FAST_FUNC bb__parsespent(void *data, char *line)
956 p = (char *) data + sp_off[i];
958 *((char **) p) = line;
959 line = strchr(line, ':');
964 *((long *) p) = strtoul(line, &endptr, 10);
965 if (endptr == line) {
973 return 0; /* all ok */
987 /**********************************************************************/
989 /* Reads until if EOF, or until if finds a line which fits in the buffer
990 * and for which the parser function succeeds.
992 * Returns 0 on success and ENOENT for end-of-file (glibc concession).
994 static int bb__pgsreader(
995 int FAST_FUNC (*parserfunc)(void *d, char *line),
997 char *__restrict line_buff,
1004 if (buflen < PWD_BUFFER_SIZE) {
1011 if (!fgets(line_buff, buflen, f)) {
1019 int line_len = strlen(line_buff) - 1;
1020 if (line_len >= 0 && line_buff[line_len] == '\n') {
1021 line_buff[line_len] = '\0';
1023 if (line_len + 2 == buflen) {
1024 /* A start (or continuation) of overlong line */
1027 } /* else: a last line in the file, and it has no '\n' */
1031 /* This "line" is a remainder of overlong line, ignore */
1036 /* NOTE: glibc difference - glibc strips leading whitespace from
1037 * records. We do not allow leading whitespace. */
1039 /* Skip empty lines, comment lines, and lines with leading
1041 if (line_buff[0] != '\0' && line_buff[0] != '#' && !isspace(line_buff[0])) {
1042 if (parserfunc == bb__parsegrent) {
1043 /* Do evil group hack:
1044 * The group entry parsing function needs to know where
1045 * the end of the buffer is so that it can construct the
1046 * group member ptr table. */
1047 ((struct group *) data)->gr_name = line_buff + buflen;
1049 if (parserfunc(data, line_buff) == 0) {