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.
125 /**********************************************************************/
127 struct passwd *fgetpwent(FILE *stream)
129 static char buffer[PWD_BUFFER_SIZE];
130 static struct passwd resultbuf;
131 struct passwd *result;
133 fgetpwent_r(stream, &resultbuf, buffer, sizeof(buffer), &result);
137 struct group *fgetgrent(FILE *stream)
139 static char buffer[GRP_BUFFER_SIZE];
140 static struct group resultbuf;
141 struct group *result;
143 fgetgrent_r(stream, &resultbuf, buffer, sizeof(buffer), &result);
147 #if ENABLE_USE_BB_SHADOW
148 struct spwd *fgetspent(FILE *stream)
150 static char buffer[PWD_BUFFER_SIZE];
151 static struct spwd resultbuf;
154 fgetspent_r(stream, &resultbuf, buffer, sizeof(buffer), &result);
158 int sgetspent_r(const char *string, struct spwd *result_buf,
159 char *buffer, size_t buflen, struct spwd **result)
165 if (buflen < PWD_BUFFER_SIZE) {
171 if (string != buffer) {
172 if (strlen(string) >= buflen) {
175 strcpy(buffer, string);
178 rv = bb__parsespent(result_buf, buffer);
180 *result = result_buf;
188 /**********************************************************************/
190 #define GETXXKEY_R_FUNC getpwnam_r
191 #define GETXXKEY_R_PARSER bb__parsepwent
192 #define GETXXKEY_R_ENTTYPE struct passwd
193 #define GETXXKEY_R_TEST(ENT) (!strcmp((ENT)->pw_name, key))
194 #define GETXXKEY_R_KEYTYPE const char *__restrict
195 #define GETXXKEY_R_PATHNAME _PATH_PASSWD
196 #include "pwd_grp_internal.c"
198 #define GETXXKEY_R_FUNC getgrnam_r
199 #define GETXXKEY_R_PARSER bb__parsegrent
200 #define GETXXKEY_R_ENTTYPE struct group
201 #define GETXXKEY_R_TEST(ENT) (!strcmp((ENT)->gr_name, key))
202 #define GETXXKEY_R_KEYTYPE const char *__restrict
203 #define GETXXKEY_R_PATHNAME _PATH_GROUP
204 #include "pwd_grp_internal.c"
206 #if ENABLE_USE_BB_SHADOW
207 #define GETXXKEY_R_FUNC getspnam_r
208 #define GETXXKEY_R_PARSER bb__parsespent
209 #define GETXXKEY_R_ENTTYPE struct spwd
210 #define GETXXKEY_R_TEST(ENT) (!strcmp((ENT)->sp_namp, key))
211 #define GETXXKEY_R_KEYTYPE const char *__restrict
212 #define GETXXKEY_R_PATHNAME _PATH_SHADOW
213 #include "pwd_grp_internal.c"
216 #define GETXXKEY_R_FUNC getpwuid_r
217 #define GETXXKEY_R_PARSER bb__parsepwent
218 #define GETXXKEY_R_ENTTYPE struct passwd
219 #define GETXXKEY_R_TEST(ENT) ((ENT)->pw_uid == key)
220 #define GETXXKEY_R_KEYTYPE uid_t
221 #define GETXXKEY_R_PATHNAME _PATH_PASSWD
222 #include "pwd_grp_internal.c"
224 #define GETXXKEY_R_FUNC getgrgid_r
225 #define GETXXKEY_R_PARSER bb__parsegrent
226 #define GETXXKEY_R_ENTTYPE struct group
227 #define GETXXKEY_R_TEST(ENT) ((ENT)->gr_gid == key)
228 #define GETXXKEY_R_KEYTYPE gid_t
229 #define GETXXKEY_R_PATHNAME _PATH_GROUP
230 #include "pwd_grp_internal.c"
232 /**********************************************************************/
234 struct passwd *getpwuid(uid_t uid)
236 static char buffer[PWD_BUFFER_SIZE];
237 static struct passwd resultbuf;
238 struct passwd *result;
240 getpwuid_r(uid, &resultbuf, buffer, sizeof(buffer), &result);
244 struct group *getgrgid(gid_t gid)
246 static char buffer[GRP_BUFFER_SIZE];
247 static struct group resultbuf;
248 struct group *result;
250 getgrgid_r(gid, &resultbuf, buffer, sizeof(buffer), &result);
254 #if 0 //ENABLE_USE_BB_SHADOW
255 /* This function is non-standard and is currently not built. It seems
256 * to have been created as a reentrant version of the non-standard
257 * functions getspuid. Why getspuid was added, I do not know. */
258 int getspuid_r(uid_t uid, struct spwd *__restrict resultbuf,
259 char *__restrict buffer, size_t buflen,
260 struct spwd **__restrict result)
264 struct passwd password;
265 char pwd_buff[PWD_BUFFER_SIZE];
268 rv = getpwuid_r(uid, &password, pwd_buff, sizeof(pwd_buff), &pp);
270 rv = getspnam_r(password.pw_name, resultbuf, buffer, buflen, result);
276 /* This function is non-standard and is currently not built.
277 * Why it was added, I do not know. */
278 struct spwd *getspuid(uid_t uid)
280 static char buffer[PWD_BUFFER_SIZE];
281 static struct spwd resultbuf;
284 getspuid_r(uid, &resultbuf, buffer, sizeof(buffer), &result);
289 struct passwd *getpwnam(const char *name)
291 static char buffer[PWD_BUFFER_SIZE];
292 static struct passwd resultbuf;
293 struct passwd *result;
295 getpwnam_r(name, &resultbuf, buffer, sizeof(buffer), &result);
299 struct group *getgrnam(const char *name)
301 static char buffer[GRP_BUFFER_SIZE];
302 static struct group resultbuf;
303 struct group *result;
305 getgrnam_r(name, &resultbuf, buffer, sizeof(buffer), &result);
309 #if ENABLE_USE_BB_SHADOW
310 struct spwd *getspnam(const char *name)
312 static char buffer[PWD_BUFFER_SIZE];
313 static struct spwd resultbuf;
316 getspnam_r(name, &resultbuf, buffer, sizeof(buffer), &result);
321 int getpw(uid_t uid, char *buf)
323 struct passwd resultbuf;
324 struct passwd *result;
325 char buffer[PWD_BUFFER_SIZE];
329 } else if (!getpwuid_r(uid, &resultbuf, buffer, sizeof(buffer), &result)) {
330 if (sprintf(buf, "%s:%s:%lu:%lu:%s:%s:%s\n",
331 resultbuf.pw_name, resultbuf.pw_passwd,
332 (unsigned long)(resultbuf.pw_uid),
333 (unsigned long)(resultbuf.pw_gid),
334 resultbuf.pw_gecos, resultbuf.pw_dir,
335 resultbuf.pw_shell) >= 0
344 /**********************************************************************/
346 /* FIXME: we don't have such CONFIG_xx - ?! */
348 #if defined CONFIG_USE_BB_THREADSAFE_SHADOW && defined PTHREAD_MUTEX_INITIALIZER
349 static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER;
350 # define LOCK pthread_mutex_lock(&mylock)
351 # define UNLOCK pthread_mutex_unlock(&mylock);
353 # define LOCK ((void) 0)
354 # define UNLOCK ((void) 0)
357 static FILE *pwf /*= NULL*/;
378 int getpwent_r(struct passwd *__restrict resultbuf,
379 char *__restrict buffer, size_t buflen,
380 struct passwd **__restrict result)
385 *result = NULL; /* In case of error... */
388 pwf = fopen(_PATH_PASSWD, "r");
395 rv = bb__pgsreader(bb__parsepwent, resultbuf, buffer, buflen, pwf);
405 static FILE *grf /*= NULL*/;
425 int getgrent_r(struct group *__restrict resultbuf,
426 char *__restrict buffer, size_t buflen,
427 struct group **__restrict result)
432 *result = NULL; /* In case of error... */
435 grf = fopen(_PATH_GROUP, "r");
442 rv = bb__pgsreader(bb__parsegrent, resultbuf, buffer, buflen, grf);
452 #if ENABLE_USE_BB_SHADOW
453 static FILE *spf /*= NULL*/;
473 int getspent_r(struct spwd *resultbuf, char *buffer,
474 size_t buflen, struct spwd **result)
479 *result = NULL; /* In case of error... */
482 spf = fopen(_PATH_SHADOW, "r");
489 rv = bb__pgsreader(bb__parsespent, resultbuf, buffer, buflen, spf);
500 struct passwd *getpwent(void)
502 static char line_buff[PWD_BUFFER_SIZE];
503 static struct passwd pwd;
504 struct passwd *result;
506 getpwent_r(&pwd, line_buff, sizeof(line_buff), &result);
510 struct group *getgrent(void)
512 static char line_buff[GRP_BUFFER_SIZE];
513 static struct group gr;
514 struct group *result;
516 getgrent_r(&gr, line_buff, sizeof(line_buff), &result);
520 #if ENABLE_USE_BB_SHADOW
521 struct spwd *getspent(void)
523 static char line_buff[PWD_BUFFER_SIZE];
524 static struct spwd spwd;
527 getspent_r(&spwd, line_buff, sizeof(line_buff), &result);
531 struct spwd *sgetspent(const char *string)
533 static char line_buff[PWD_BUFFER_SIZE];
534 static struct spwd spwd;
537 sgetspent_r(string, &spwd, line_buff, sizeof(line_buff), &result);
542 int initgroups(const char *user, gid_t gid)
549 char buff[PWD_BUFFER_SIZE];
553 /* We alloc space for 8 gids at a time. */
554 group_list = (gid_t *) malloc(8*sizeof(gid_t *));
556 && ((grfile = fopen(_PATH_GROUP, "r")) != NULL)
561 while (!bb__pgsreader(bb__parsegrent, &group, buff, sizeof(buff), grfile)) {
562 assert(group.gr_mem); /* Must have at least a NULL terminator. */
563 if (group.gr_gid != gid) {
564 for (m=group.gr_mem ; *m ; m++) {
565 if (!strcmp(*m, user)) {
566 if (!(num_groups & 7)) {
567 gid_t *tmp = (gid_t *)
569 (num_groups+8) * sizeof(gid_t *));
576 group_list[num_groups++] = group.gr_gid;
583 rv = setgroups(num_groups, group_list);
588 /* group_list will be NULL if initial malloc failed, which may trigger
589 * warnings from various malloc debuggers. */
594 int putpwent(const struct passwd *__restrict p, FILE *__restrict f)
601 /* No extra thread locking is needed above what fprintf does. */
602 if (fprintf(f, "%s:%s:%lu:%lu:%s:%s:%s\n",
603 p->pw_name, p->pw_passwd,
604 (unsigned long)(p->pw_uid),
605 (unsigned long)(p->pw_gid),
606 p->pw_gecos, p->pw_dir, p->pw_shell) >= 0
615 int putgrent(const struct group *__restrict p, FILE *__restrict f)
617 static const char format[] = ",%s";
622 if (!p || !f) { /* Sigh... glibc checks. */
625 if (fprintf(f, "%s:%s:%lu:",
626 p->gr_name, p->gr_passwd,
627 (unsigned long)(p->gr_gid)) >= 0
637 if (fputc('\n', f) >= 0) {
642 if (fprintf(f, fmt, *m) < 0) {
656 #if ENABLE_USE_BB_SHADOW
657 static const unsigned char _sp_off[] = {
658 offsetof(struct spwd, sp_lstchg), /* 2 - not a char ptr */
659 offsetof(struct spwd, sp_min), /* 3 - not a char ptr */
660 offsetof(struct spwd, sp_max), /* 4 - not a char ptr */
661 offsetof(struct spwd, sp_warn), /* 5 - not a char ptr */
662 offsetof(struct spwd, sp_inact), /* 6 - not a char ptr */
663 offsetof(struct spwd, sp_expire), /* 7 - not a char ptr */
666 int putspent(const struct spwd *p, FILE *stream)
668 static const char ld_format[] = "%ld:";
674 /* Unlike putpwent and putgrent, glibc does not check the args. */
675 if (fprintf(stream, "%s:%s:", p->sp_namp,
676 (p->sp_pwdp ? p->sp_pwdp : "")) < 0
681 for (i=0 ; i < sizeof(_sp_off) ; i++) {
683 x = *(const long int *)(((const char *) p) + _sp_off[i]);
687 if (fprintf(stream, f, x) < 0) {
692 if ((p->sp_flag != ~0UL) && (fprintf(stream, "%lu", p->sp_flag) < 0)) {
696 if (fputc('\n', stream) > 0) {
705 /**********************************************************************/
706 /* Internal uClibc functions. */
707 /**********************************************************************/
709 static const unsigned char pw_off[] = {
710 offsetof(struct passwd, pw_name), /* 0 */
711 offsetof(struct passwd, pw_passwd), /* 1 */
712 offsetof(struct passwd, pw_uid), /* 2 - not a char ptr */
713 offsetof(struct passwd, pw_gid), /* 3 - not a char ptr */
714 offsetof(struct passwd, pw_gecos), /* 4 */
715 offsetof(struct passwd, pw_dir), /* 5 */
716 offsetof(struct passwd, pw_shell) /* 6 */
719 static int bb__parsepwent(void *data, char *line)
727 p = ((char *) ((struct passwd *) data)) + pw_off[i];
729 if ((i & 6) ^ 2) { /* i!=2 and i!=3 */
730 *((char **) p) = line;
734 /* NOTE: glibc difference - glibc allows omission of
735 * ':' seperators after the gid field if all remaining
736 * entries are empty. We require all separators. */
737 line = strchr(line, ':');
742 unsigned long t = strtoul(line, &endptr, 10);
743 /* Make sure we had at least one digit, and that the
744 * failing char is the next field seperator ':'. See
745 * glibc difference note above. */
746 /* TODO: Also check for leading whitespace? */
747 if ((endptr == line) || (*endptr != ':')) {
751 if (i & 1) { /* i == 3 -- gid */
753 } else { /* i == 2 -- uid */
765 /**********************************************************************/
767 static const unsigned char gr_off[] = {
768 offsetof(struct group, gr_name), /* 0 */
769 offsetof(struct group, gr_passwd), /* 1 */
770 offsetof(struct group, gr_gid) /* 2 - not a char ptr */
773 static int bb__parsegrent(void *data, char *line)
781 end_of_buf = ((struct group *) data)->gr_name; /* Evil hack! */
784 p = ((char *) ((struct group *) data)) + gr_off[i];
787 *((char **) p) = line;
788 line = strchr(line, ':');
795 *((gid_t *) p) = strtoul(line, &endptr, 10);
797 /* NOTE: glibc difference - glibc allows omission of the
798 * trailing colon when there is no member list. We treat
799 * this as an error. */
801 /* Make sure we had at least one digit, and that the
802 * failing char is the next field seperator ':'. See
803 * glibc difference note above. */
804 if ((endptr == line) || (*endptr != ':')) {
808 i = 1; /* Count terminating NULL ptr. */
811 if (p[1]) { /* We have a member list to process. */
812 /* Overwrite the last ':' with a ',' before counting.
813 * This allows us to test for initial ',' and adds
814 * one ',' so that the ',' count equals the member
818 /* NOTE: glibc difference - glibc allows and trims leading
819 * (but not trailing) space. We treat this as an error. */
820 /* NOTE: glibc difference - glibc allows consecutive and
821 * trailing commas, and ignores "empty string" users. We
822 * treat this as an error. */
825 *p = 0; /* nul-terminate each member string. */
826 if (!*++p || (*p == ',') || isspace(*p)) {
833 /* Now align (p+1), rounding up. */
834 /* Assumes sizeof(char **) is a power of 2. */
835 members = (char **)( (((intptr_t) p) + sizeof(char **))
836 & ~((intptr_t)(sizeof(char **) - 1)) );
838 if (((char *)(members + i)) > end_of_buf) { /* No space. */
842 ((struct group *) data)->gr_mem = members;
845 p = endptr; /* Pointing to char prior to first member. */
862 /**********************************************************************/
864 #if ENABLE_USE_BB_SHADOW
865 static const unsigned char sp_off[] = {
866 offsetof(struct spwd, sp_namp), /* 0 */
867 offsetof(struct spwd, sp_pwdp), /* 1 */
868 offsetof(struct spwd, sp_lstchg), /* 2 - not a char ptr */
869 offsetof(struct spwd, sp_min), /* 3 - not a char ptr */
870 offsetof(struct spwd, sp_max), /* 4 - not a char ptr */
871 offsetof(struct spwd, sp_warn), /* 5 - not a char ptr */
872 offsetof(struct spwd, sp_inact), /* 6 - not a char ptr */
873 offsetof(struct spwd, sp_expire), /* 7 - not a char ptr */
874 offsetof(struct spwd, sp_flag) /* 8 - not a char ptr */
877 static int bb__parsespent(void *data, char * line)
885 p = ((char *) ((struct spwd *) data)) + sp_off[i];
887 *((char **) p) = line;
888 line = strchr(line, ':');
893 *((long *) p) = (long) strtoul(line, &endptr, 10);
895 if (endptr == line) {
896 *((long *) p) = ((i != 8) ? -1L : ((long)(~0UL)));
908 if (*endptr != ':') {
922 /**********************************************************************/
924 /* Reads until if EOF, or until if finds a line which fits in the buffer
925 * and for which the parser function succeeds.
927 * Returns 0 on success and ENOENT for end-of-file (glibc concession).
930 static int bb__pgsreader(int (*parserfunc)(void *d, char *line), void *data,
931 char *__restrict line_buff, size_t buflen, FILE *f)
937 if (buflen < PWD_BUFFER_SIZE) {
942 if (!fgets(line_buff, buflen, f)) {
949 line_len = strlen(line_buff) - 1; /* strlen() must be > 0. */
950 if (line_buff[line_len] == '\n') {
951 line_buff[line_len] = 0;
952 } else if (line_len + 2 == buflen) { /* line too long */
962 /* NOTE: glibc difference - glibc strips leading whitespace from
963 * records. We do not allow leading whitespace. */
965 /* Skip empty lines, comment lines, and lines with leading
967 if (*line_buff && (*line_buff != '#') && !isspace(*line_buff)) {
968 if (parserfunc == bb__parsegrent) { /* Do evil group hack. */
969 /* The group entry parsing function needs to know where
970 * the end of the buffer is so that it can construct the
971 * group member ptr table. */
972 ((struct group *) data)->gr_name = line_buff + buflen;
975 if (!parserfunc(data, line_buff)) {