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.
26 #define _PATH_SHADOW "/etc/shadow"
29 #define _PATH_PASSWD "/etc/passwd"
32 #define _PATH_GROUP "/etc/group"
35 /**********************************************************************/
36 /* Sizes for statically allocated buffers. */
38 /* If you change these values, also change _SC_GETPW_R_SIZE_MAX and
39 * _SC_GETGR_R_SIZE_MAX in libc/unistd/sysconf.c to match */
40 #define PWD_BUFFER_SIZE 256
41 #define GRP_BUFFER_SIZE 256
43 /**********************************************************************/
44 /* Prototypes for internal functions. */
46 static int bb__pgsreader(int (*parserfunc)(void *d, char *line), void *data,
47 char *__restrict line_buff, size_t buflen, FILE *f);
49 static int bb__parsepwent(void *pw, char *line);
50 static int bb__parsegrent(void *gr, char *line);
51 #if ENABLE_USE_BB_SHADOW
52 static int bb__parsespent(void *sp, char *line);
55 /**********************************************************************/
56 /* We avoid having big global data. */
59 /* Smaller things first */
60 struct passwd getpwuid_resultbuf;
61 struct group getgrgid_resultbuf;
62 struct passwd getpwnam_resultbuf;
63 struct group getgrnam_resultbuf;
65 char getpwuid_buffer[PWD_BUFFER_SIZE];
66 char getgrgid_buffer[GRP_BUFFER_SIZE];
67 char getpwnam_buffer[PWD_BUFFER_SIZE];
68 char getgrnam_buffer[GRP_BUFFER_SIZE];
70 struct passwd fgetpwent_resultbuf;
71 struct group fgetgrent_resultbuf;
72 struct spwd fgetspent_resultbuf;
73 char fgetpwent_buffer[PWD_BUFFER_SIZE];
74 char fgetgrent_buffer[GRP_BUFFER_SIZE];
75 char fgetspent_buffer[PWD_BUFFER_SIZE];
77 #if 0 //ENABLE_USE_BB_SHADOW
78 struct spwd getspuid_resultbuf;
79 struct spwd getspnam_resultbuf;
80 char getspuid_buffer[PWD_BUFFER_SIZE];
81 char getspnam_buffer[PWD_BUFFER_SIZE];
83 // Not converted - too small to bother
84 //pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER;
85 //FILE *pwf /*= NULL*/;
86 //FILE *grf /*= NULL*/;
87 //FILE *spf /*= NULL*/;
89 struct passwd getpwent_pwd;
90 struct group getgrent_gr;
91 char getpwent_line_buff[PWD_BUFFER_SIZE];
92 char getgrent_line_buff[GRP_BUFFER_SIZE];
94 #if 0 //ENABLE_USE_BB_SHADOW
95 struct spwd getspent_spwd;
96 struct spwd sgetspent_spwd;
97 char getspent_line_buff[PWD_BUFFER_SIZE];
98 char sgetspent_line_buff[PWD_BUFFER_SIZE];
102 static struct statics *ptr_to_statics;
104 static struct statics *get_S(void)
107 ptr_to_statics = xzalloc(sizeof(*ptr_to_statics));
108 return ptr_to_statics;
111 /* Always use in this order, get_S() must be called first */
112 #define RESULTBUF(name) &((S = get_S())->name##_resultbuf)
113 #define BUFFER(name) (S->name##_buffer)
115 /**********************************************************************/
116 /* For the various fget??ent_r funcs, return
119 * ENOENT: end-of-file encountered
120 * ERANGE: buflen too small
121 * other error values possible. See bb__pgsreader.
123 * Also, *result == resultbuf on success and NULL on failure.
125 * NOTE: glibc difference - For the ENOENT case, glibc also sets errno.
126 * We do not, as it really isn't an error if we reach the end-of-file.
127 * Doing so is analogous to having fgetc() set errno on EOF.
129 /**********************************************************************/
131 int fgetpwent_r(FILE *__restrict stream, struct passwd *__restrict resultbuf,
132 char *__restrict buffer, size_t buflen,
133 struct passwd **__restrict result)
139 rv = bb__pgsreader(bb__parsepwent, resultbuf, buffer, buflen, stream);
147 int fgetgrent_r(FILE *__restrict stream, struct group *__restrict resultbuf,
148 char *__restrict buffer, size_t buflen,
149 struct group **__restrict result)
155 rv = bb__pgsreader(bb__parsegrent, resultbuf, buffer, buflen, stream);
163 #if ENABLE_USE_BB_SHADOW
164 int fgetspent_r(FILE *__restrict stream, struct spwd *__restrict resultbuf,
165 char *__restrict buffer, size_t buflen,
166 struct spwd **__restrict result)
172 rv = bb__pgsreader(bb__parsespent, resultbuf, buffer, buflen, stream);
181 /**********************************************************************/
182 /* For the various fget??ent funcs, return NULL on failure and a
183 * pointer to the appropriate struct (statically allocated) on success.
184 * TODO: audit & stop using these in bbox, they pull in static buffers */
185 /**********************************************************************/
188 struct passwd *fgetpwent(FILE *stream)
191 struct passwd *resultbuf = RESULTBUF(fgetpwent);
192 char *buffer = BUFFER(fgetpwent);
193 struct passwd *result;
195 fgetpwent_r(stream, resultbuf, buffer, sizeof(BUFFER(fgetpwent)), &result);
199 struct group *fgetgrent(FILE *stream)
202 struct group *resultbuf = RESULTBUF(fgetgrent);
203 char *buffer = BUFFER(fgetgrent);
204 struct group *result;
206 fgetgrent_r(stream, resultbuf, buffer, sizeof(BUFFER(fgetgrent)), &result);
211 #if ENABLE_USE_BB_SHADOW
213 struct spwd *fgetspent(FILE *stream)
216 struct spwd *resultbuf = RESULTBUF(fgetspent);
217 char *buffer = BUFFER(fgetspent);
220 fgetspent_r(stream, resultbuf, buffer, sizeof(BUFFER(fgetspent)), &result);
225 int sgetspent_r(const char *string, struct spwd *result_buf,
226 char *buffer, size_t buflen, struct spwd **result)
232 if (buflen < PWD_BUFFER_SIZE) {
238 if (string != buffer) {
239 if (strlen(string) >= buflen) {
242 strcpy(buffer, string);
245 rv = bb__parsespent(result_buf, buffer);
247 *result = result_buf;
255 /**********************************************************************/
257 #define GETXXKEY_R_FUNC getpwnam_r
258 #define GETXXKEY_R_PARSER bb__parsepwent
259 #define GETXXKEY_R_ENTTYPE struct passwd
260 #define GETXXKEY_R_TEST(ENT) (!strcmp((ENT)->pw_name, key))
261 #define GETXXKEY_R_KEYTYPE const char *__restrict
262 #define GETXXKEY_R_PATHNAME _PATH_PASSWD
263 #include "pwd_grp_internal.c"
265 #define GETXXKEY_R_FUNC getgrnam_r
266 #define GETXXKEY_R_PARSER bb__parsegrent
267 #define GETXXKEY_R_ENTTYPE struct group
268 #define GETXXKEY_R_TEST(ENT) (!strcmp((ENT)->gr_name, key))
269 #define GETXXKEY_R_KEYTYPE const char *__restrict
270 #define GETXXKEY_R_PATHNAME _PATH_GROUP
271 #include "pwd_grp_internal.c"
273 #if ENABLE_USE_BB_SHADOW
274 #define GETXXKEY_R_FUNC getspnam_r
275 #define GETXXKEY_R_PARSER bb__parsespent
276 #define GETXXKEY_R_ENTTYPE struct spwd
277 #define GETXXKEY_R_TEST(ENT) (!strcmp((ENT)->sp_namp, key))
278 #define GETXXKEY_R_KEYTYPE const char *__restrict
279 #define GETXXKEY_R_PATHNAME _PATH_SHADOW
280 #include "pwd_grp_internal.c"
283 #define GETXXKEY_R_FUNC getpwuid_r
284 #define GETXXKEY_R_PARSER bb__parsepwent
285 #define GETXXKEY_R_ENTTYPE struct passwd
286 #define GETXXKEY_R_TEST(ENT) ((ENT)->pw_uid == key)
287 #define GETXXKEY_R_KEYTYPE uid_t
288 #define GETXXKEY_R_PATHNAME _PATH_PASSWD
289 #include "pwd_grp_internal.c"
291 #define GETXXKEY_R_FUNC getgrgid_r
292 #define GETXXKEY_R_PARSER bb__parsegrent
293 #define GETXXKEY_R_ENTTYPE struct group
294 #define GETXXKEY_R_TEST(ENT) ((ENT)->gr_gid == key)
295 #define GETXXKEY_R_KEYTYPE gid_t
296 #define GETXXKEY_R_PATHNAME _PATH_GROUP
297 #include "pwd_grp_internal.c"
299 /**********************************************************************/
300 /* TODO: audit & stop using these in bbox, they pull in static buffers */
302 /* This one has many users */
303 struct passwd *getpwuid(uid_t uid)
306 struct passwd *resultbuf = RESULTBUF(getpwuid);
307 char *buffer = BUFFER(getpwuid);
308 struct passwd *result;
310 getpwuid_r(uid, resultbuf, buffer, sizeof(BUFFER(getpwuid)), &result);
314 /* This one has many users */
315 struct group *getgrgid(gid_t gid)
318 struct group *resultbuf = RESULTBUF(getgrgid);
319 char *buffer = BUFFER(getgrgid);
320 struct group *result;
322 getgrgid_r(gid, resultbuf, buffer, sizeof(BUFFER(getgrgid)), &result);
326 #if 0 //ENABLE_USE_BB_SHADOW
327 /* This function is non-standard and is currently not built. It seems
328 * to have been created as a reentrant version of the non-standard
329 * functions getspuid. Why getspuid was added, I do not know. */
330 int getspuid_r(uid_t uid, struct spwd *__restrict resultbuf,
331 char *__restrict buffer, size_t buflen,
332 struct spwd **__restrict result)
336 struct passwd password;
337 char pwd_buff[PWD_BUFFER_SIZE];
340 rv = getpwuid_r(uid, &password, pwd_buff, sizeof(pwd_buff), &pp);
342 rv = getspnam_r(password.pw_name, resultbuf, buffer, buflen, result);
348 /* This function is non-standard and is currently not built.
349 * Why it was added, I do not know. */
350 struct spwd *getspuid(uid_t uid)
353 struct spwd *resultbuf = RESULTBUF(getspuid);
354 char *buffer = BUFFER(getspuid);
357 getspuid_r(uid, resultbuf, buffer, sizeof(BUFFER(getspuid)), &result);
362 /* This one has many users */
363 struct passwd *getpwnam(const char *name)
366 struct passwd *resultbuf = RESULTBUF(getpwnam);
367 char *buffer = BUFFER(getpwnam);
368 struct passwd *result;
370 getpwnam_r(name, resultbuf, buffer, sizeof(BUFFER(getpwnam)), &result);
374 /* This one has many users */
375 struct group *getgrnam(const char *name)
378 struct group *resultbuf = RESULTBUF(getgrnam);
379 char *buffer = BUFFER(getgrnam);
380 struct group *result;
382 getgrnam_r(name, resultbuf, buffer, sizeof(BUFFER(getgrnam)), &result);
386 #if 0 //ENABLE_USE_BB_SHADOW
387 struct spwd *getspnam(const char *name)
390 struct spwd *resultbuf = RESULTBUF(getspnam);
391 char *buffer = BUFFER(getspnam);
394 getspnam_r(name, resultbuf, buffer, sizeof(BUFFER(getspnam)), &result);
399 /* This one doesn't use static buffers */
400 int getpw(uid_t uid, char *buf)
402 struct passwd resultbuf;
403 struct passwd *result;
404 char buffer[PWD_BUFFER_SIZE];
408 } else if (!getpwuid_r(uid, &resultbuf, buffer, sizeof(buffer), &result)) {
409 if (sprintf(buf, "%s:%s:%lu:%lu:%s:%s:%s\n",
410 resultbuf.pw_name, resultbuf.pw_passwd,
411 (unsigned long)(resultbuf.pw_uid),
412 (unsigned long)(resultbuf.pw_gid),
413 resultbuf.pw_gecos, resultbuf.pw_dir,
414 resultbuf.pw_shell) >= 0
423 /**********************************************************************/
425 /* FIXME: we don't have such CONFIG_xx - ?! */
427 #if defined CONFIG_USE_BB_THREADSAFE_SHADOW && defined PTHREAD_MUTEX_INITIALIZER
428 static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER;
429 # define LOCK pthread_mutex_lock(&mylock)
430 # define UNLOCK pthread_mutex_unlock(&mylock);
432 # define LOCK ((void) 0)
433 # define UNLOCK ((void) 0)
436 static FILE *pwf /*= NULL*/;
457 int getpwent_r(struct passwd *__restrict resultbuf,
458 char *__restrict buffer, size_t buflen,
459 struct passwd **__restrict result)
464 *result = NULL; /* In case of error... */
467 pwf = fopen(_PATH_PASSWD, "r");
474 rv = bb__pgsreader(bb__parsepwent, resultbuf, buffer, buflen, pwf);
484 static FILE *grf /*= NULL*/;
504 int getgrent_r(struct group *__restrict resultbuf,
505 char *__restrict buffer, size_t buflen,
506 struct group **__restrict result)
511 *result = NULL; /* In case of error... */
514 grf = fopen(_PATH_GROUP, "r");
521 rv = bb__pgsreader(bb__parsegrent, resultbuf, buffer, buflen, grf);
531 #if ENABLE_USE_BB_SHADOW
532 static FILE *spf /*= NULL*/;
552 int getspent_r(struct spwd *resultbuf, char *buffer,
553 size_t buflen, struct spwd **result)
558 *result = NULL; /* In case of error... */
561 spf = fopen(_PATH_SHADOW, "r");
568 rv = bb__pgsreader(bb__parsespent, resultbuf, buffer, buflen, spf);
580 struct passwd *getpwent(void)
582 static char line_buff[PWD_BUFFER_SIZE];
583 static struct passwd pwd;
584 struct passwd *result;
586 getpwent_r(&pwd, line_buff, sizeof(line_buff), &result);
590 struct group *getgrent(void)
592 static char line_buff[GRP_BUFFER_SIZE];
593 static struct group gr;
594 struct group *result;
596 getgrent_r(&gr, line_buff, sizeof(line_buff), &result);
601 #if 0 //ENABLE_USE_BB_SHADOW
602 struct spwd *getspent(void)
604 static char line_buff[PWD_BUFFER_SIZE];
605 static struct spwd spwd;
608 getspent_r(&spwd, line_buff, sizeof(line_buff), &result);
612 struct spwd *sgetspent(const char *string)
614 static char line_buff[PWD_BUFFER_SIZE];
615 static struct spwd spwd;
618 sgetspent_r(string, &spwd, line_buff, sizeof(line_buff), &result);
623 int initgroups(const char *user, gid_t gid)
630 char buff[PWD_BUFFER_SIZE];
634 /* We alloc space for 8 gids at a time. */
635 group_list = (gid_t *) malloc(8*sizeof(gid_t *));
637 && ((grfile = fopen(_PATH_GROUP, "r")) != NULL)
642 while (!bb__pgsreader(bb__parsegrent, &group, buff, sizeof(buff), grfile)) {
643 assert(group.gr_mem); /* Must have at least a NULL terminator. */
644 if (group.gr_gid != gid) {
645 for (m = group.gr_mem; *m; m++) {
646 if (!strcmp(*m, user)) {
647 if (!(num_groups & 7)) {
648 gid_t *tmp = (gid_t *)
650 (num_groups+8) * sizeof(gid_t *));
657 group_list[num_groups++] = group.gr_gid;
664 rv = setgroups(num_groups, group_list);
669 /* group_list will be NULL if initial malloc failed, which may trigger
670 * warnings from various malloc debuggers. */
675 int putpwent(const struct passwd *__restrict p, FILE *__restrict f)
682 /* No extra thread locking is needed above what fprintf does. */
683 if (fprintf(f, "%s:%s:%lu:%lu:%s:%s:%s\n",
684 p->pw_name, p->pw_passwd,
685 (unsigned long)(p->pw_uid),
686 (unsigned long)(p->pw_gid),
687 p->pw_gecos, p->pw_dir, p->pw_shell) >= 0
696 int putgrent(const struct group *__restrict p, FILE *__restrict f)
698 static const char format[] ALIGN1 = ",%s";
704 if (!p || !f) { /* Sigh... glibc checks. */
707 if (fprintf(f, "%s:%s:%lu:",
708 p->gr_name, p->gr_passwd,
709 (unsigned long)(p->gr_gid)) >= 0
719 if (fputc('\n', f) >= 0) {
724 if (fprintf(f, fmt, *m) < 0) {
738 #if ENABLE_USE_BB_SHADOW
739 static const unsigned char _sp_off[] ALIGN1 = {
740 offsetof(struct spwd, sp_lstchg), /* 2 - not a char ptr */
741 offsetof(struct spwd, sp_min), /* 3 - not a char ptr */
742 offsetof(struct spwd, sp_max), /* 4 - not a char ptr */
743 offsetof(struct spwd, sp_warn), /* 5 - not a char ptr */
744 offsetof(struct spwd, sp_inact), /* 6 - not a char ptr */
745 offsetof(struct spwd, sp_expire) /* 7 - not a char ptr */
748 int putspent(const struct spwd *p, FILE *stream)
750 static const char ld_format[] ALIGN1 = "%ld:";
757 /* Unlike putpwent and putgrent, glibc does not check the args. */
758 if (fprintf(stream, "%s:%s:", p->sp_namp,
759 (p->sp_pwdp ? p->sp_pwdp : "")) < 0
764 for (i = 0; i < sizeof(_sp_off); i++) {
766 x = *(const long *)(((const char *) p) + _sp_off[i]);
770 if (fprintf(stream, f, x) < 0) {
775 if ((p->sp_flag != ~0UL) && (fprintf(stream, "%lu", p->sp_flag) < 0)) {
779 if (fputc('\n', stream) > 0) {
788 /**********************************************************************/
789 /* Internal uClibc functions. */
790 /**********************************************************************/
792 static const unsigned char pw_off[] ALIGN1 = {
793 offsetof(struct passwd, pw_name), /* 0 */
794 offsetof(struct passwd, pw_passwd), /* 1 */
795 offsetof(struct passwd, pw_uid), /* 2 - not a char ptr */
796 offsetof(struct passwd, pw_gid), /* 3 - not a char ptr */
797 offsetof(struct passwd, pw_gecos), /* 4 */
798 offsetof(struct passwd, pw_dir), /* 5 */
799 offsetof(struct passwd, pw_shell) /* 6 */
802 static int bb__parsepwent(void *data, char *line)
810 p = ((char *) ((struct passwd *) data)) + pw_off[i];
812 if ((i & 6) ^ 2) { /* i!=2 and i!=3 */
813 *((char **) p) = line;
817 /* NOTE: glibc difference - glibc allows omission of
818 * ':' seperators after the gid field if all remaining
819 * entries are empty. We require all separators. */
820 line = strchr(line, ':');
825 unsigned long t = strtoul(line, &endptr, 10);
826 /* Make sure we had at least one digit, and that the
827 * failing char is the next field seperator ':'. See
828 * glibc difference note above. */
829 /* TODO: Also check for leading whitespace? */
830 if ((endptr == line) || (*endptr != ':')) {
834 if (i & 1) { /* i == 3 -- gid */
836 } else { /* i == 2 -- uid */
848 /**********************************************************************/
850 static const unsigned char gr_off[] ALIGN1 = {
851 offsetof(struct group, gr_name), /* 0 */
852 offsetof(struct group, gr_passwd), /* 1 */
853 offsetof(struct group, gr_gid) /* 2 - not a char ptr */
856 static int bb__parsegrent(void *data, char *line)
864 end_of_buf = ((struct group *) data)->gr_name; /* Evil hack! */
867 p = ((char *) ((struct group *) data)) + gr_off[i];
870 *((char **) p) = line;
871 line = strchr(line, ':');
878 *((gid_t *) p) = strtoul(line, &endptr, 10);
880 /* NOTE: glibc difference - glibc allows omission of the
881 * trailing colon when there is no member list. We treat
882 * this as an error. */
884 /* Make sure we had at least one digit, and that the
885 * failing char is the next field seperator ':'. See
886 * glibc difference note above. */
887 if ((endptr == line) || (*endptr != ':')) {
891 i = 1; /* Count terminating NULL ptr. */
894 if (p[1]) { /* We have a member list to process. */
895 /* Overwrite the last ':' with a ',' before counting.
896 * This allows us to test for initial ',' and adds
897 * one ',' so that the ',' count equals the member
901 /* NOTE: glibc difference - glibc allows and trims leading
902 * (but not trailing) space. We treat this as an error. */
903 /* NOTE: glibc difference - glibc allows consecutive and
904 * trailing commas, and ignores "empty string" users. We
905 * treat this as an error. */
908 *p = 0; /* nul-terminate each member string. */
909 if (!*++p || (*p == ',') || isspace(*p)) {
916 /* Now align (p+1), rounding up. */
917 /* Assumes sizeof(char **) is a power of 2. */
918 members = (char **)( (((intptr_t) p) + sizeof(char **))
919 & ~((intptr_t)(sizeof(char **) - 1)) );
921 if (((char *)(members + i)) > end_of_buf) { /* No space. */
925 ((struct group *) data)->gr_mem = members;
928 p = endptr; /* Pointing to char prior to first member. */
945 /**********************************************************************/
947 #if ENABLE_USE_BB_SHADOW
948 static const unsigned char sp_off[] ALIGN1 = {
949 offsetof(struct spwd, sp_namp), /* 0 */
950 offsetof(struct spwd, sp_pwdp), /* 1 */
951 offsetof(struct spwd, sp_lstchg), /* 2 - not a char ptr */
952 offsetof(struct spwd, sp_min), /* 3 - not a char ptr */
953 offsetof(struct spwd, sp_max), /* 4 - not a char ptr */
954 offsetof(struct spwd, sp_warn), /* 5 - not a char ptr */
955 offsetof(struct spwd, sp_inact), /* 6 - not a char ptr */
956 offsetof(struct spwd, sp_expire), /* 7 - not a char ptr */
957 offsetof(struct spwd, sp_flag) /* 8 - not a char ptr */
960 static int bb__parsespent(void *data, char * line)
968 p = ((char *) ((struct spwd *) data)) + sp_off[i];
970 *((char **) p) = line;
971 line = strchr(line, ':');
976 *((long *) p) = (long) strtoul(line, &endptr, 10);
978 if (endptr == line) {
979 *((long *) p) = ((i != 8) ? -1L : ((long)(~0UL)));
991 if (*endptr != ':') {
1005 /**********************************************************************/
1007 /* Reads until if EOF, or until if finds a line which fits in the buffer
1008 * and for which the parser function succeeds.
1010 * Returns 0 on success and ENOENT for end-of-file (glibc concession).
1013 static int bb__pgsreader(int (*parserfunc)(void *d, char *line), void *data,
1014 char *__restrict line_buff, size_t buflen, FILE *f)
1020 if (buflen < PWD_BUFFER_SIZE) {
1025 if (!fgets(line_buff, buflen, f)) {
1032 line_len = strlen(line_buff) - 1; /* strlen() must be > 0. */
1033 if (line_buff[line_len] == '\n') {
1034 line_buff[line_len] = 0;
1035 } else if (line_len + 2 == buflen) { /* line too long */
1045 /* NOTE: glibc difference - glibc strips leading whitespace from
1046 * records. We do not allow leading whitespace. */
1048 /* Skip empty lines, comment lines, and lines with leading
1050 if (*line_buff && (*line_buff != '#') && !isspace(*line_buff)) {
1051 if (parserfunc == bb__parsegrent) { /* Do evil group hack. */
1052 /* The group entry parsing function needs to know where
1053 * the end of the buffer is so that it can construct the
1054 * group member ptr table. */
1055 ((struct group *) data)->gr_name = line_buff + buflen;
1058 if (!parserfunc(data, line_buff)) {