1 /* Copyright (C) 2003 Manuel Novoa III
3 * Licensed under GPL v2, or later. See file LICENSE in this tarball.
6 /* Nov 6, 2003 Initial version.
8 * NOTE: This implementation is quite strict about requiring all
9 * field seperators. It also does not allow leading whitespace
10 * except when processing the numeric fields. glibc is more
11 * lenient. See the various glibc difference comments below.
14 * Move to dynamic allocation of (currently statically allocated)
15 * buffers; especially for the group-related functions since
16 * large group member lists will cause error returns.
34 #define _PATH_SHADOW "/etc/shadow"
37 #define _PATH_PASSWD "/etc/passwd"
40 #define _PATH_GROUP "/etc/group"
43 /**********************************************************************/
44 /* Sizes for statically allocated buffers. */
46 /* If you change these values, also change _SC_GETPW_R_SIZE_MAX and
47 * _SC_GETGR_R_SIZE_MAX in libc/unistd/sysconf.c to match */
48 #define PWD_BUFFER_SIZE 256
49 #define GRP_BUFFER_SIZE 256
51 /**********************************************************************/
52 /* Prototypes for internal functions. */
54 extern int __parsepwent(void *pw, char *line);
55 extern int __parsegrent(void *gr, char *line);
56 extern int __parsespent(void *sp, char *line);
58 extern int __pgsreader(int (*__parserfunc)(void *d, char *line), void *data,
59 char *__restrict line_buff, size_t buflen, FILE *f);
61 /**********************************************************************/
62 /* For the various fget??ent_r funcs, return
65 * ENOENT: end-of-file encountered
66 * ERANGE: buflen too small
67 * other error values possible. See __pgsreader.
69 * Also, *result == resultbuf on success and NULL on failure.
71 * NOTE: glibc difference - For the ENOENT case, glibc also sets errno.
72 * We do not, as it really isn't an error if we reach the end-of-file.
73 * Doing so is analogous to having fgetc() set errno on EOF.
75 /**********************************************************************/
79 int fgetpwent_r(FILE *__restrict stream, struct passwd *__restrict resultbuf,
80 char *__restrict buffer, size_t buflen,
81 struct passwd **__restrict result)
87 if (!(rv = __pgsreader(__parsepwent, resultbuf, buffer, buflen, stream))) {
95 /**********************************************************************/
98 int fgetgrent_r(FILE *__restrict stream, struct group *__restrict resultbuf,
99 char *__restrict buffer, size_t buflen,
100 struct group **__restrict result)
106 if (!(rv = __pgsreader(__parsegrent, resultbuf, buffer, buflen, stream))) {
114 /**********************************************************************/
117 int fgetspent_r(FILE *__restrict stream, struct spwd *__restrict resultbuf,
118 char *__restrict buffer, size_t buflen,
119 struct spwd **__restrict result)
125 if (!(rv = __pgsreader(__parsespent, resultbuf, buffer, buflen, stream))) {
133 /**********************************************************************/
134 /* For the various fget??ent funcs, return NULL on failure and a
135 * pointer to the appropriate struct (statically allocated) on success.
137 /**********************************************************************/
140 struct passwd *fgetpwent(FILE *stream)
142 static char buffer[PWD_BUFFER_SIZE];
143 static struct passwd resultbuf;
144 struct passwd *result;
146 fgetpwent_r(stream, &resultbuf, buffer, sizeof(buffer), &result);
151 /**********************************************************************/
154 struct group *fgetgrent(FILE *stream)
156 static char buffer[GRP_BUFFER_SIZE];
157 static struct group resultbuf;
158 struct group *result;
160 fgetgrent_r(stream, &resultbuf, buffer, sizeof(buffer), &result);
165 /**********************************************************************/
168 extern int fgetspent_r(FILE *__restrict stream, struct spwd *__restrict resultbuf,
169 char *__restrict buffer, size_t buflen,
170 struct spwd **__restrict result);
171 struct spwd *fgetspent(FILE *stream)
173 static char buffer[PWD_BUFFER_SIZE];
174 static struct spwd resultbuf;
177 fgetspent_r(stream, &resultbuf, buffer, sizeof(buffer), &result);
182 /**********************************************************************/
185 int sgetspent_r(const char *string, struct spwd *result_buf,
186 char *buffer, size_t buflen, struct spwd **result)
192 if (buflen < PWD_BUFFER_SIZE) {
198 if (string != buffer) {
199 if (strlen(string) >= buflen) {
202 strcpy(buffer, string);
205 if (!(rv = __parsespent(result_buf, buffer))) {
206 *result = result_buf;
214 /**********************************************************************/
216 #ifdef GETXXKEY_R_FUNC
217 #error GETXXKEY_R_FUNC is already defined!
221 #define GETXXKEY_R_FUNC getpwnam_r
222 #define GETXXKEY_R_PARSER __parsepwent
223 #define GETXXKEY_R_ENTTYPE struct passwd
224 #define GETXXKEY_R_TEST(ENT) (!strcmp((ENT)->pw_name, key))
225 #define DO_GETXXKEY_R_KEYTYPE const char *__restrict
226 #define DO_GETXXKEY_R_PATHNAME _PATH_PASSWD
227 #include "pwd_grp_internal.c"
231 #define GETXXKEY_R_FUNC getgrnam_r
232 #define GETXXKEY_R_PARSER __parsegrent
233 #define GETXXKEY_R_ENTTYPE struct group
234 #define GETXXKEY_R_TEST(ENT) (!strcmp((ENT)->gr_name, key))
235 #define DO_GETXXKEY_R_KEYTYPE const char *__restrict
236 #define DO_GETXXKEY_R_PATHNAME _PATH_GROUP
237 #include "pwd_grp_internal.c"
241 #define GETXXKEY_R_FUNC getspnam_r
242 #define GETXXKEY_R_PARSER __parsespent
243 #define GETXXKEY_R_ENTTYPE struct spwd
244 #define GETXXKEY_R_TEST(ENT) (!strcmp((ENT)->sp_namp, key))
245 #define DO_GETXXKEY_R_KEYTYPE const char *__restrict
246 #define DO_GETXXKEY_R_PATHNAME _PATH_SHADOW
247 #include "pwd_grp_internal.c"
251 #define GETXXKEY_R_FUNC getpwuid_r
252 #define GETXXKEY_R_PARSER __parsepwent
253 #define GETXXKEY_R_ENTTYPE struct passwd
254 #define GETXXKEY_R_TEST(ENT) ((ENT)->pw_uid == key)
255 #define DO_GETXXKEY_R_KEYTYPE uid_t
256 #define DO_GETXXKEY_R_PATHNAME _PATH_PASSWD
257 #include "pwd_grp_internal.c"
261 #define GETXXKEY_R_FUNC getgrgid_r
262 #define GETXXKEY_R_PARSER __parsegrent
263 #define GETXXKEY_R_ENTTYPE struct group
264 #define GETXXKEY_R_TEST(ENT) ((ENT)->gr_gid == key)
265 #define DO_GETXXKEY_R_KEYTYPE gid_t
266 #define DO_GETXXKEY_R_PATHNAME _PATH_GROUP
267 #include "pwd_grp_internal.c"
270 /**********************************************************************/
273 struct passwd *getpwuid(uid_t uid)
275 static char buffer[PWD_BUFFER_SIZE];
276 static struct passwd resultbuf;
277 struct passwd *result;
279 getpwuid_r(uid, &resultbuf, buffer, sizeof(buffer), &result);
284 /**********************************************************************/
287 struct group *getgrgid(gid_t gid)
289 static char buffer[GRP_BUFFER_SIZE];
290 static struct group resultbuf;
291 struct group *result;
293 getgrgid_r(gid, &resultbuf, buffer, sizeof(buffer), &result);
298 /**********************************************************************/
301 /* This function is non-standard and is currently not built. It seems
302 * to have been created as a reentrant version of the non-standard
303 * functions getspuid. Why getspuid was added, I do not know. */
305 int getspuid_r(uid_t uid, struct spwd *__restrict resultbuf,
306 char *__restrict buffer, size_t buflen,
307 struct spwd **__restrict result)
311 struct passwd password;
312 char pwd_buff[PWD_BUFFER_SIZE];
315 if (!(rv = getpwuid_r(uid, &password, pwd_buff, sizeof(pwd_buff), &pp))) {
316 rv = getspnam_r(password.pw_name, resultbuf, buffer, buflen, result);
323 /**********************************************************************/
326 /* This function is non-standard and is currently not built.
327 * Why it was added, I do not know. */
329 struct spwd *getspuid(uid_t uid)
331 static char buffer[PWD_BUFFER_SIZE];
332 static struct spwd resultbuf;
335 getspuid_r(uid, &resultbuf, buffer, sizeof(buffer), &result);
340 /**********************************************************************/
343 struct passwd *getpwnam(const char *name)
345 static char buffer[PWD_BUFFER_SIZE];
346 static struct passwd resultbuf;
347 struct passwd *result;
349 getpwnam_r(name, &resultbuf, buffer, sizeof(buffer), &result);
354 /**********************************************************************/
357 struct group *getgrnam(const char *name)
359 static char buffer[GRP_BUFFER_SIZE];
360 static struct group resultbuf;
361 struct group *result;
363 getgrnam_r(name, &resultbuf, buffer, sizeof(buffer), &result);
368 /**********************************************************************/
371 struct spwd *getspnam(const char *name)
373 static char buffer[PWD_BUFFER_SIZE];
374 static struct spwd resultbuf;
377 getspnam_r(name, &resultbuf, buffer, sizeof(buffer), &result);
382 /**********************************************************************/
385 int getpw(uid_t uid, char *buf)
387 struct passwd resultbuf;
388 struct passwd *result;
389 char buffer[PWD_BUFFER_SIZE];
393 } else if (!getpwuid_r(uid, &resultbuf, buffer, sizeof(buffer), &result)) {
394 if (sprintf(buf, "%s:%s:%lu:%lu:%s:%s:%s\n",
395 resultbuf.pw_name, resultbuf.pw_passwd,
396 (unsigned long)(resultbuf.pw_uid),
397 (unsigned long)(resultbuf.pw_gid),
398 resultbuf.pw_gecos, resultbuf.pw_dir,
399 resultbuf.pw_shell) >= 0
409 /**********************************************************************/
411 #if defined(L_getpwent_r) || defined(L_getgrent_r) || defined(L_getspent_r)
412 #if defined CONFIG_USE_BB_THREADSAFE_SHADOW && defined PTHREAD_MUTEX_INITIALIZER
413 static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER;
414 # define LOCK pthread_mutex_lock(&mylock)
415 # define UNLOCK pthread_mutex_unlock(&mylock);
417 # define LOCK ((void) 0)
418 # define UNLOCK ((void) 0)
423 static FILE *pwf /*= NULL*/;
444 int getpwent_r(struct passwd *__restrict resultbuf,
445 char *__restrict buffer, size_t buflen,
446 struct passwd **__restrict result)
451 *result = NULL; /* In case of error... */
454 if (!(pwf = fopen(_PATH_PASSWD, "r"))) {
460 if (!(rv = __pgsreader(__parsepwent, resultbuf,
461 buffer, buflen, pwf))) {
471 /**********************************************************************/
474 static FILE *grf /*= NULL*/;
494 int getgrent_r(struct group *__restrict resultbuf,
495 char *__restrict buffer, size_t buflen,
496 struct group **__restrict result)
501 *result = NULL; /* In case of error... */
504 if (!(grf = fopen(_PATH_GROUP, "r"))) {
510 if (!(rv = __pgsreader(__parsegrent, resultbuf,
511 buffer, buflen, grf))) {
521 /**********************************************************************/
524 static FILE *spf /*= NULL*/;
544 int getspent_r(struct spwd *resultbuf, char *buffer,
545 size_t buflen, struct spwd **result)
550 *result = NULL; /* In case of error... */
553 if (!(spf = fopen(_PATH_SHADOW, "r"))) {
559 if (!(rv = __pgsreader(__parsespent, resultbuf,
560 buffer, buflen, spf))) {
570 /**********************************************************************/
573 struct passwd *getpwent(void)
575 static char line_buff[PWD_BUFFER_SIZE];
576 static struct passwd pwd;
577 struct passwd *result;
579 getpwent_r(&pwd, line_buff, sizeof(line_buff), &result);
584 /**********************************************************************/
587 struct group *getgrent(void)
589 static char line_buff[GRP_BUFFER_SIZE];
590 static struct group gr;
591 struct group *result;
593 getgrent_r(&gr, line_buff, sizeof(line_buff), &result);
598 /**********************************************************************/
601 struct spwd *getspent(void)
603 static char line_buff[PWD_BUFFER_SIZE];
604 static struct spwd spwd;
607 getspent_r(&spwd, line_buff, sizeof(line_buff), &result);
612 /**********************************************************************/
615 struct spwd *sgetspent(const char *string)
617 static char line_buff[PWD_BUFFER_SIZE];
618 static struct spwd spwd;
621 sgetspent_r(string, &spwd, line_buff, sizeof(line_buff), &result);
626 /**********************************************************************/
629 int initgroups(const char *user, gid_t gid)
636 char buff[PWD_BUFFER_SIZE];
640 /* We alloc space for 8 gids at a time. */
641 if (((group_list = (gid_t *) malloc(8*sizeof(gid_t *))) != NULL)
642 && ((grfile = fopen(_PATH_GROUP, "r")) != NULL)
648 while (!__pgsreader(__parsegrent, &group, buff, sizeof(buff), grfile)) {
649 assert(group.gr_mem); /* Must have at least a NULL terminator. */
650 if (group.gr_gid != gid) {
651 for (m=group.gr_mem ; *m ; m++) {
652 if (!strcmp(*m, user)) {
653 if (!(num_groups & 7)) {
654 gid_t *tmp = (gid_t *)
656 (num_groups+8) * sizeof(gid_t *));
663 group_list[num_groups++] = group.gr_gid;
670 rv = setgroups(num_groups, group_list);
675 /* group_list will be NULL if initial malloc failed, which may trigger
676 * warnings from various malloc debuggers. */
682 /**********************************************************************/
685 int putpwent(const struct passwd *__restrict p, FILE *__restrict f)
692 /* No extra thread locking is needed above what fprintf does. */
693 if (fprintf(f, "%s:%s:%lu:%lu:%s:%s:%s\n",
694 p->pw_name, p->pw_passwd,
695 (unsigned long)(p->pw_uid),
696 (unsigned long)(p->pw_gid),
697 p->pw_gecos, p->pw_dir, p->pw_shell) >= 0
707 /**********************************************************************/
710 int putgrent(const struct group *__restrict p, FILE *__restrict f)
712 static const char format[] = ",%s";
717 if (!p || !f) { /* Sigh... glibc checks. */
720 if (fprintf(f, "%s:%s:%lu:",
721 p->gr_name, p->gr_passwd,
722 (unsigned long)(p->gr_gid)) >= 0
732 if (fputc('\n', f) >= 0) {
737 if (fprintf(f, fmt, *m) < 0) {
752 /**********************************************************************/
755 static const unsigned char _sp_off[] = {
756 offsetof(struct spwd, sp_lstchg), /* 2 - not a char ptr */
757 offsetof(struct spwd, sp_min), /* 3 - not a char ptr */
758 offsetof(struct spwd, sp_max), /* 4 - not a char ptr */
759 offsetof(struct spwd, sp_warn), /* 5 - not a char ptr */
760 offsetof(struct spwd, sp_inact), /* 6 - not a char ptr */
761 offsetof(struct spwd, sp_expire), /* 7 - not a char ptr */
764 int putspent(const struct spwd *p, FILE *stream)
766 static const char ld_format[] = "%ld:";
772 /* Unlike putpwent and putgrent, glibc does not check the args. */
773 if (fprintf(stream, "%s:%s:", p->sp_namp,
774 (p->sp_pwdp ? p->sp_pwdp : "")) < 0
779 for (i=0 ; i < sizeof(_sp_off) ; i++) {
781 if ((x = *(const long int *)(((const char *) p) + _sp_off[i])) == -1) {
784 if (fprintf(stream, f, x) < 0) {
789 if ((p->sp_flag != ~0UL) && (fprintf(stream, "%lu", p->sp_flag) < 0)) {
793 if (fputc('\n', stream) > 0) {
802 /**********************************************************************/
803 /* Internal uClibc functions. */
804 /**********************************************************************/
805 #ifdef L___parsepwent
807 static const unsigned char pw_off[] = {
808 offsetof(struct passwd, pw_name), /* 0 */
809 offsetof(struct passwd, pw_passwd), /* 1 */
810 offsetof(struct passwd, pw_uid), /* 2 - not a char ptr */
811 offsetof(struct passwd, pw_gid), /* 3 - not a char ptr */
812 offsetof(struct passwd, pw_gecos), /* 4 */
813 offsetof(struct passwd, pw_dir), /* 5 */
814 offsetof(struct passwd, pw_shell) /* 6 */
817 int __parsepwent(void *data, char *line)
825 p = ((char *) ((struct passwd *) data)) + pw_off[i];
827 if ((i & 6) ^ 2) { /* i!=2 and i!=3 */
828 *((char **) p) = line;
832 /* NOTE: glibc difference - glibc allows omission of
833 * ':' seperators after the gid field if all remaining
834 * entries are empty. We require all separators. */
835 if (!(line = strchr(line, ':'))) {
839 unsigned long t = strtoul(line, &endptr, 10);
840 /* Make sure we had at least one digit, and that the
841 * failing char is the next field seperator ':'. See
842 * glibc difference note above. */
843 /* TODO: Also check for leading whitespace? */
844 if ((endptr == line) || (*endptr != ':')) {
848 if (i & 1) { /* i == 3 -- gid */
850 } else { /* i == 2 -- uid */
863 /**********************************************************************/
864 #ifdef L___parsegrent
866 static const unsigned char gr_off[] = {
867 offsetof(struct group, gr_name), /* 0 */
868 offsetof(struct group, gr_passwd), /* 1 */
869 offsetof(struct group, gr_gid) /* 2 - not a char ptr */
872 int __parsegrent(void *data, char *line)
880 end_of_buf = ((struct group *) data)->gr_name; /* Evil hack! */
883 p = ((char *) ((struct group *) data)) + gr_off[i];
886 *((char **) p) = line;
887 if (!(line = strchr(line, ':'))) {
893 *((gid_t *) p) = strtoul(line, &endptr, 10);
895 /* NOTE: glibc difference - glibc allows omission of the
896 * trailing colon when there is no member list. We treat
897 * this as an error. */
899 /* Make sure we had at least one digit, and that the
900 * failing char is the next field seperator ':'. See
901 * glibc difference note above. */
902 if ((endptr == line) || (*endptr != ':')) {
906 i = 1; /* Count terminating NULL ptr. */
909 if (p[1]) { /* We have a member list to process. */
910 /* Overwrite the last ':' with a ',' before counting.
911 * This allows us to test for initial ',' and adds
912 * one ',' so that the ',' count equals the member
916 /* NOTE: glibc difference - glibc allows and trims leading
917 * (but not trailing) space. We treat this as an error. */
918 /* NOTE: glibc difference - glibc allows consecutive and
919 * trailing commas, and ignores "empty string" users. We
920 * treat this as an error. */
923 *p = 0; /* nul-terminate each member string. */
924 if (!*++p || (*p == ',') || isspace(*p)) {
931 /* Now align (p+1), rounding up. */
932 /* Assumes sizeof(char **) is a power of 2. */
933 members = (char **)( (((intptr_t) p) + sizeof(char **))
934 & ~((intptr_t)(sizeof(char **) - 1)) );
936 if (((char *)(members + i)) > end_of_buf) { /* No space. */
940 ((struct group *) data)->gr_mem = members;
943 p = endptr; /* Pointing to char prior to first member. */
961 /**********************************************************************/
962 #ifdef L___parsespent
964 static const unsigned char sp_off[] = {
965 offsetof(struct spwd, sp_namp), /* 0 */
966 offsetof(struct spwd, sp_pwdp), /* 1 */
967 offsetof(struct spwd, sp_lstchg), /* 2 - not a char ptr */
968 offsetof(struct spwd, sp_min), /* 3 - not a char ptr */
969 offsetof(struct spwd, sp_max), /* 4 - not a char ptr */
970 offsetof(struct spwd, sp_warn), /* 5 - not a char ptr */
971 offsetof(struct spwd, sp_inact), /* 6 - not a char ptr */
972 offsetof(struct spwd, sp_expire), /* 7 - not a char ptr */
973 offsetof(struct spwd, sp_flag) /* 8 - not a char ptr */
976 int __parsespent(void *data, char * line)
984 p = ((char *) ((struct spwd *) data)) + sp_off[i];
986 *((char **) p) = line;
987 if (!(line = strchr(line, ':'))) {
991 *((long *) p) = (long) strtoul(line, &endptr, 10);
993 if (endptr == line) {
994 *((long *) p) = ((i != 8) ? -1L : ((long)(~0UL)));
1006 if (*endptr != ':') {
1020 /**********************************************************************/
1021 #ifdef L___pgsreader
1023 /* Reads until if EOF, or until if finds a line which fits in the buffer
1024 * and for which the parser function succeeds.
1026 * Returns 0 on success and ENOENT for end-of-file (glibc concession).
1029 int __pgsreader(int (*__parserfunc)(void *d, char *line), void *data,
1030 char *__restrict line_buff, size_t buflen, FILE *f)
1036 if (buflen < PWD_BUFFER_SIZE) {
1041 if (!fgets(line_buff, buflen, f)) {
1048 line_len = strlen(line_buff) - 1; /* strlen() must be > 0. */
1049 if (line_buff[line_len] == '\n') {
1050 line_buff[line_len] = 0;
1051 } else if (line_len + 2 == buflen) { /* line too long */
1061 /* NOTE: glibc difference - glibc strips leading whitespace from
1062 * records. We do not allow leading whitespace. */
1064 /* Skip empty lines, comment lines, and lines with leading
1066 if (*line_buff && (*line_buff != '#') && !isspace(*line_buff)) {
1067 if (__parserfunc == __parsegrent) { /* Do evil group hack. */
1068 /* The group entry parsing function needs to know where
1069 * the end of the buffer is so that it can construct the
1070 * group member ptr table. */
1071 ((struct group *) data)->gr_name = line_buff + buflen;
1074 if (!__parserfunc(data, line_buff)) {
1087 /**********************************************************************/