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 /* For the various fget??ent_r funcs, return
59 * ENOENT: end-of-file encountered
60 * ERANGE: buflen too small
61 * other error values possible. See bb__pgsreader.
63 * Also, *result == resultbuf on success and NULL on failure.
65 * NOTE: glibc difference - For the ENOENT case, glibc also sets errno.
66 * We do not, as it really isn't an error if we reach the end-of-file.
67 * Doing so is analogous to having fgetc() set errno on EOF.
69 /**********************************************************************/
71 int fgetpwent_r(FILE *__restrict stream, struct passwd *__restrict resultbuf,
72 char *__restrict buffer, size_t buflen,
73 struct passwd **__restrict result)
79 rv = bb__pgsreader(bb__parsepwent, resultbuf, buffer, buflen, stream);
87 int fgetgrent_r(FILE *__restrict stream, struct group *__restrict resultbuf,
88 char *__restrict buffer, size_t buflen,
89 struct group **__restrict result)
95 rv = bb__pgsreader(bb__parsegrent, resultbuf, buffer, buflen, stream);
103 #if ENABLE_USE_BB_SHADOW
104 int fgetspent_r(FILE *__restrict stream, struct spwd *__restrict resultbuf,
105 char *__restrict buffer, size_t buflen,
106 struct spwd **__restrict result)
112 rv = bb__pgsreader(bb__parsespent, resultbuf, buffer, buflen, stream);
121 /**********************************************************************/
122 /* For the various fget??ent funcs, return NULL on failure and a
123 * pointer to the appropriate struct (statically allocated) on success.
124 * TODO: audit & stop using these in bbox, they pull in static buffers */
125 /**********************************************************************/
128 struct passwd *fgetpwent(FILE *stream)
130 static char buffer[PWD_BUFFER_SIZE];
131 static struct passwd resultbuf;
132 struct passwd *result;
134 fgetpwent_r(stream, &resultbuf, buffer, sizeof(buffer), &result);
138 struct group *fgetgrent(FILE *stream)
140 static char buffer[GRP_BUFFER_SIZE];
141 static struct group resultbuf;
142 struct group *result;
144 fgetgrent_r(stream, &resultbuf, buffer, sizeof(buffer), &result);
149 #if ENABLE_USE_BB_SHADOW
151 struct spwd *fgetspent(FILE *stream)
153 static char buffer[PWD_BUFFER_SIZE];
154 static struct spwd resultbuf;
157 fgetspent_r(stream, &resultbuf, buffer, sizeof(buffer), &result);
162 int sgetspent_r(const char *string, struct spwd *result_buf,
163 char *buffer, size_t buflen, struct spwd **result)
169 if (buflen < PWD_BUFFER_SIZE) {
175 if (string != buffer) {
176 if (strlen(string) >= buflen) {
179 strcpy(buffer, string);
182 rv = bb__parsespent(result_buf, buffer);
184 *result = result_buf;
192 /**********************************************************************/
194 #define GETXXKEY_R_FUNC getpwnam_r
195 #define GETXXKEY_R_PARSER bb__parsepwent
196 #define GETXXKEY_R_ENTTYPE struct passwd
197 #define GETXXKEY_R_TEST(ENT) (!strcmp((ENT)->pw_name, key))
198 #define GETXXKEY_R_KEYTYPE const char *__restrict
199 #define GETXXKEY_R_PATHNAME _PATH_PASSWD
200 #include "pwd_grp_internal.c"
202 #define GETXXKEY_R_FUNC getgrnam_r
203 #define GETXXKEY_R_PARSER bb__parsegrent
204 #define GETXXKEY_R_ENTTYPE struct group
205 #define GETXXKEY_R_TEST(ENT) (!strcmp((ENT)->gr_name, key))
206 #define GETXXKEY_R_KEYTYPE const char *__restrict
207 #define GETXXKEY_R_PATHNAME _PATH_GROUP
208 #include "pwd_grp_internal.c"
210 #if ENABLE_USE_BB_SHADOW
211 #define GETXXKEY_R_FUNC getspnam_r
212 #define GETXXKEY_R_PARSER bb__parsespent
213 #define GETXXKEY_R_ENTTYPE struct spwd
214 #define GETXXKEY_R_TEST(ENT) (!strcmp((ENT)->sp_namp, key))
215 #define GETXXKEY_R_KEYTYPE const char *__restrict
216 #define GETXXKEY_R_PATHNAME _PATH_SHADOW
217 #include "pwd_grp_internal.c"
220 #define GETXXKEY_R_FUNC getpwuid_r
221 #define GETXXKEY_R_PARSER bb__parsepwent
222 #define GETXXKEY_R_ENTTYPE struct passwd
223 #define GETXXKEY_R_TEST(ENT) ((ENT)->pw_uid == key)
224 #define GETXXKEY_R_KEYTYPE uid_t
225 #define GETXXKEY_R_PATHNAME _PATH_PASSWD
226 #include "pwd_grp_internal.c"
228 #define GETXXKEY_R_FUNC getgrgid_r
229 #define GETXXKEY_R_PARSER bb__parsegrent
230 #define GETXXKEY_R_ENTTYPE struct group
231 #define GETXXKEY_R_TEST(ENT) ((ENT)->gr_gid == key)
232 #define GETXXKEY_R_KEYTYPE gid_t
233 #define GETXXKEY_R_PATHNAME _PATH_GROUP
234 #include "pwd_grp_internal.c"
236 /**********************************************************************/
237 /* TODO: audit & stop using these in bbox, they pull in static buffers */
239 /* This one has many users */
240 struct passwd *getpwuid(uid_t uid)
242 static char buffer[PWD_BUFFER_SIZE];
243 static struct passwd resultbuf;
244 struct passwd *result;
246 getpwuid_r(uid, &resultbuf, buffer, sizeof(buffer), &result);
250 /* This one has many users */
251 struct group *getgrgid(gid_t gid)
253 static char buffer[GRP_BUFFER_SIZE];
254 static struct group resultbuf;
255 struct group *result;
257 getgrgid_r(gid, &resultbuf, buffer, sizeof(buffer), &result);
261 #if 0 //ENABLE_USE_BB_SHADOW
262 /* This function is non-standard and is currently not built. It seems
263 * to have been created as a reentrant version of the non-standard
264 * functions getspuid. Why getspuid was added, I do not know. */
265 int getspuid_r(uid_t uid, struct spwd *__restrict resultbuf,
266 char *__restrict buffer, size_t buflen,
267 struct spwd **__restrict result)
271 struct passwd password;
272 char pwd_buff[PWD_BUFFER_SIZE];
275 rv = getpwuid_r(uid, &password, pwd_buff, sizeof(pwd_buff), &pp);
277 rv = getspnam_r(password.pw_name, resultbuf, buffer, buflen, result);
283 /* This function is non-standard and is currently not built.
284 * Why it was added, I do not know. */
285 struct spwd *getspuid(uid_t uid)
287 static char buffer[PWD_BUFFER_SIZE];
288 static struct spwd resultbuf;
291 getspuid_r(uid, &resultbuf, buffer, sizeof(buffer), &result);
296 /* This one has many users */
297 struct passwd *getpwnam(const char *name)
299 static char buffer[PWD_BUFFER_SIZE];
300 static struct passwd resultbuf;
301 struct passwd *result;
303 getpwnam_r(name, &resultbuf, buffer, sizeof(buffer), &result);
307 /* This one has many users */
308 struct group *getgrnam(const char *name)
310 static char buffer[GRP_BUFFER_SIZE];
311 static struct group resultbuf;
312 struct group *result;
314 getgrnam_r(name, &resultbuf, buffer, sizeof(buffer), &result);
318 #if 0 //ENABLE_USE_BB_SHADOW
319 struct spwd *getspnam(const char *name)
321 static char buffer[PWD_BUFFER_SIZE];
322 static struct spwd resultbuf;
325 getspnam_r(name, &resultbuf, buffer, sizeof(buffer), &result);
330 /* This one doesn't use static buffers */
331 int getpw(uid_t uid, char *buf)
333 struct passwd resultbuf;
334 struct passwd *result;
335 char buffer[PWD_BUFFER_SIZE];
339 } else if (!getpwuid_r(uid, &resultbuf, buffer, sizeof(buffer), &result)) {
340 if (sprintf(buf, "%s:%s:%lu:%lu:%s:%s:%s\n",
341 resultbuf.pw_name, resultbuf.pw_passwd,
342 (unsigned long)(resultbuf.pw_uid),
343 (unsigned long)(resultbuf.pw_gid),
344 resultbuf.pw_gecos, resultbuf.pw_dir,
345 resultbuf.pw_shell) >= 0
354 /**********************************************************************/
356 /* FIXME: we don't have such CONFIG_xx - ?! */
358 #if defined CONFIG_USE_BB_THREADSAFE_SHADOW && defined PTHREAD_MUTEX_INITIALIZER
359 static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER;
360 # define LOCK pthread_mutex_lock(&mylock)
361 # define UNLOCK pthread_mutex_unlock(&mylock);
363 # define LOCK ((void) 0)
364 # define UNLOCK ((void) 0)
367 static FILE *pwf /*= NULL*/;
388 int getpwent_r(struct passwd *__restrict resultbuf,
389 char *__restrict buffer, size_t buflen,
390 struct passwd **__restrict result)
395 *result = NULL; /* In case of error... */
398 pwf = fopen(_PATH_PASSWD, "r");
405 rv = bb__pgsreader(bb__parsepwent, resultbuf, buffer, buflen, pwf);
415 static FILE *grf /*= NULL*/;
435 int getgrent_r(struct group *__restrict resultbuf,
436 char *__restrict buffer, size_t buflen,
437 struct group **__restrict result)
442 *result = NULL; /* In case of error... */
445 grf = fopen(_PATH_GROUP, "r");
452 rv = bb__pgsreader(bb__parsegrent, resultbuf, buffer, buflen, grf);
462 #if ENABLE_USE_BB_SHADOW
463 static FILE *spf /*= NULL*/;
483 int getspent_r(struct spwd *resultbuf, char *buffer,
484 size_t buflen, struct spwd **result)
489 *result = NULL; /* In case of error... */
492 spf = fopen(_PATH_SHADOW, "r");
499 rv = bb__pgsreader(bb__parsespent, resultbuf, buffer, buflen, spf);
511 struct passwd *getpwent(void)
513 static char line_buff[PWD_BUFFER_SIZE];
514 static struct passwd pwd;
515 struct passwd *result;
517 getpwent_r(&pwd, line_buff, sizeof(line_buff), &result);
521 struct group *getgrent(void)
523 static char line_buff[GRP_BUFFER_SIZE];
524 static struct group gr;
525 struct group *result;
527 getgrent_r(&gr, line_buff, sizeof(line_buff), &result);
532 #if 0 //ENABLE_USE_BB_SHADOW
533 struct spwd *getspent(void)
535 static char line_buff[PWD_BUFFER_SIZE];
536 static struct spwd spwd;
539 getspent_r(&spwd, line_buff, sizeof(line_buff), &result);
543 struct spwd *sgetspent(const char *string)
545 static char line_buff[PWD_BUFFER_SIZE];
546 static struct spwd spwd;
549 sgetspent_r(string, &spwd, line_buff, sizeof(line_buff), &result);
554 int initgroups(const char *user, gid_t gid)
561 char buff[PWD_BUFFER_SIZE];
565 /* We alloc space for 8 gids at a time. */
566 group_list = (gid_t *) malloc(8*sizeof(gid_t *));
568 && ((grfile = fopen(_PATH_GROUP, "r")) != NULL)
573 while (!bb__pgsreader(bb__parsegrent, &group, buff, sizeof(buff), grfile)) {
574 assert(group.gr_mem); /* Must have at least a NULL terminator. */
575 if (group.gr_gid != gid) {
576 for (m=group.gr_mem ; *m ; m++) {
577 if (!strcmp(*m, user)) {
578 if (!(num_groups & 7)) {
579 gid_t *tmp = (gid_t *)
581 (num_groups+8) * sizeof(gid_t *));
588 group_list[num_groups++] = group.gr_gid;
595 rv = setgroups(num_groups, group_list);
600 /* group_list will be NULL if initial malloc failed, which may trigger
601 * warnings from various malloc debuggers. */
606 int putpwent(const struct passwd *__restrict p, FILE *__restrict f)
613 /* No extra thread locking is needed above what fprintf does. */
614 if (fprintf(f, "%s:%s:%lu:%lu:%s:%s:%s\n",
615 p->pw_name, p->pw_passwd,
616 (unsigned long)(p->pw_uid),
617 (unsigned long)(p->pw_gid),
618 p->pw_gecos, p->pw_dir, p->pw_shell) >= 0
627 int putgrent(const struct group *__restrict p, FILE *__restrict f)
629 static const char format[] = ",%s";
634 if (!p || !f) { /* Sigh... glibc checks. */
637 if (fprintf(f, "%s:%s:%lu:",
638 p->gr_name, p->gr_passwd,
639 (unsigned long)(p->gr_gid)) >= 0
649 if (fputc('\n', f) >= 0) {
654 if (fprintf(f, fmt, *m) < 0) {
668 #if ENABLE_USE_BB_SHADOW
669 static const unsigned char _sp_off[] = {
670 offsetof(struct spwd, sp_lstchg), /* 2 - not a char ptr */
671 offsetof(struct spwd, sp_min), /* 3 - not a char ptr */
672 offsetof(struct spwd, sp_max), /* 4 - not a char ptr */
673 offsetof(struct spwd, sp_warn), /* 5 - not a char ptr */
674 offsetof(struct spwd, sp_inact), /* 6 - not a char ptr */
675 offsetof(struct spwd, sp_expire), /* 7 - not a char ptr */
678 int putspent(const struct spwd *p, FILE *stream)
680 static const char ld_format[] = "%ld:";
686 /* Unlike putpwent and putgrent, glibc does not check the args. */
687 if (fprintf(stream, "%s:%s:", p->sp_namp,
688 (p->sp_pwdp ? p->sp_pwdp : "")) < 0
693 for (i=0 ; i < sizeof(_sp_off) ; i++) {
695 x = *(const long int *)(((const char *) p) + _sp_off[i]);
699 if (fprintf(stream, f, x) < 0) {
704 if ((p->sp_flag != ~0UL) && (fprintf(stream, "%lu", p->sp_flag) < 0)) {
708 if (fputc('\n', stream) > 0) {
717 /**********************************************************************/
718 /* Internal uClibc functions. */
719 /**********************************************************************/
721 static const unsigned char pw_off[] = {
722 offsetof(struct passwd, pw_name), /* 0 */
723 offsetof(struct passwd, pw_passwd), /* 1 */
724 offsetof(struct passwd, pw_uid), /* 2 - not a char ptr */
725 offsetof(struct passwd, pw_gid), /* 3 - not a char ptr */
726 offsetof(struct passwd, pw_gecos), /* 4 */
727 offsetof(struct passwd, pw_dir), /* 5 */
728 offsetof(struct passwd, pw_shell) /* 6 */
731 static int bb__parsepwent(void *data, char *line)
739 p = ((char *) ((struct passwd *) data)) + pw_off[i];
741 if ((i & 6) ^ 2) { /* i!=2 and i!=3 */
742 *((char **) p) = line;
746 /* NOTE: glibc difference - glibc allows omission of
747 * ':' seperators after the gid field if all remaining
748 * entries are empty. We require all separators. */
749 line = strchr(line, ':');
754 unsigned long t = strtoul(line, &endptr, 10);
755 /* Make sure we had at least one digit, and that the
756 * failing char is the next field seperator ':'. See
757 * glibc difference note above. */
758 /* TODO: Also check for leading whitespace? */
759 if ((endptr == line) || (*endptr != ':')) {
763 if (i & 1) { /* i == 3 -- gid */
765 } else { /* i == 2 -- uid */
777 /**********************************************************************/
779 static const unsigned char gr_off[] = {
780 offsetof(struct group, gr_name), /* 0 */
781 offsetof(struct group, gr_passwd), /* 1 */
782 offsetof(struct group, gr_gid) /* 2 - not a char ptr */
785 static int bb__parsegrent(void *data, char *line)
793 end_of_buf = ((struct group *) data)->gr_name; /* Evil hack! */
796 p = ((char *) ((struct group *) data)) + gr_off[i];
799 *((char **) p) = line;
800 line = strchr(line, ':');
807 *((gid_t *) p) = strtoul(line, &endptr, 10);
809 /* NOTE: glibc difference - glibc allows omission of the
810 * trailing colon when there is no member list. We treat
811 * this as an error. */
813 /* Make sure we had at least one digit, and that the
814 * failing char is the next field seperator ':'. See
815 * glibc difference note above. */
816 if ((endptr == line) || (*endptr != ':')) {
820 i = 1; /* Count terminating NULL ptr. */
823 if (p[1]) { /* We have a member list to process. */
824 /* Overwrite the last ':' with a ',' before counting.
825 * This allows us to test for initial ',' and adds
826 * one ',' so that the ',' count equals the member
830 /* NOTE: glibc difference - glibc allows and trims leading
831 * (but not trailing) space. We treat this as an error. */
832 /* NOTE: glibc difference - glibc allows consecutive and
833 * trailing commas, and ignores "empty string" users. We
834 * treat this as an error. */
837 *p = 0; /* nul-terminate each member string. */
838 if (!*++p || (*p == ',') || isspace(*p)) {
845 /* Now align (p+1), rounding up. */
846 /* Assumes sizeof(char **) is a power of 2. */
847 members = (char **)( (((intptr_t) p) + sizeof(char **))
848 & ~((intptr_t)(sizeof(char **) - 1)) );
850 if (((char *)(members + i)) > end_of_buf) { /* No space. */
854 ((struct group *) data)->gr_mem = members;
857 p = endptr; /* Pointing to char prior to first member. */
874 /**********************************************************************/
876 #if ENABLE_USE_BB_SHADOW
877 static const unsigned char sp_off[] = {
878 offsetof(struct spwd, sp_namp), /* 0 */
879 offsetof(struct spwd, sp_pwdp), /* 1 */
880 offsetof(struct spwd, sp_lstchg), /* 2 - not a char ptr */
881 offsetof(struct spwd, sp_min), /* 3 - not a char ptr */
882 offsetof(struct spwd, sp_max), /* 4 - not a char ptr */
883 offsetof(struct spwd, sp_warn), /* 5 - not a char ptr */
884 offsetof(struct spwd, sp_inact), /* 6 - not a char ptr */
885 offsetof(struct spwd, sp_expire), /* 7 - not a char ptr */
886 offsetof(struct spwd, sp_flag) /* 8 - not a char ptr */
889 static int bb__parsespent(void *data, char * line)
897 p = ((char *) ((struct spwd *) data)) + sp_off[i];
899 *((char **) p) = line;
900 line = strchr(line, ':');
905 *((long *) p) = (long) strtoul(line, &endptr, 10);
907 if (endptr == line) {
908 *((long *) p) = ((i != 8) ? -1L : ((long)(~0UL)));
920 if (*endptr != ':') {
934 /**********************************************************************/
936 /* Reads until if EOF, or until if finds a line which fits in the buffer
937 * and for which the parser function succeeds.
939 * Returns 0 on success and ENOENT for end-of-file (glibc concession).
942 static int bb__pgsreader(int (*parserfunc)(void *d, char *line), void *data,
943 char *__restrict line_buff, size_t buflen, FILE *f)
949 if (buflen < PWD_BUFFER_SIZE) {
954 if (!fgets(line_buff, buflen, f)) {
961 line_len = strlen(line_buff) - 1; /* strlen() must be > 0. */
962 if (line_buff[line_len] == '\n') {
963 line_buff[line_len] = 0;
964 } else if (line_len + 2 == buflen) { /* line too long */
974 /* NOTE: glibc difference - glibc strips leading whitespace from
975 * records. We do not allow leading whitespace. */
977 /* Skip empty lines, comment lines, and lines with leading
979 if (*line_buff && (*line_buff != '#') && !isspace(*line_buff)) {
980 if (parserfunc == bb__parsegrent) { /* Do evil group hack. */
981 /* The group entry parsing function needs to know where
982 * the end of the buffer is so that it can construct the
983 * group member ptr table. */
984 ((struct group *) data)->gr_name = line_buff + buflen;
987 if (!parserfunc(data, line_buff)) {