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.
33 #define _PATH_SHADOW "/etc/shadow"
36 #define _PATH_PASSWD "/etc/passwd"
39 #define _PATH_GROUP "/etc/group"
42 /**********************************************************************/
43 /* Sizes for statically allocated buffers. */
45 /* If you change these values, also change _SC_GETPW_R_SIZE_MAX and
46 * _SC_GETGR_R_SIZE_MAX in libc/unistd/sysconf.c to match */
47 #define PWD_BUFFER_SIZE 256
48 #define GRP_BUFFER_SIZE 256
50 /**********************************************************************/
51 /* Prototypes for internal functions. */
53 extern int __parsepwent(void *pw, char *line);
54 extern int __parsegrent(void *gr, char *line);
55 extern int __parsespent(void *sp, char *line);
57 extern int __pgsreader(int (*__parserfunc)(void *d, char *line), void *data,
58 char *__restrict line_buff, size_t buflen, FILE *f);
60 /**********************************************************************/
61 /* For the various fget??ent_r funcs, return
64 * ENOENT: end-of-file encountered
65 * ERANGE: buflen too small
66 * other error values possible. See __pgsreader.
68 * Also, *result == resultbuf on success and NULL on failure.
70 * NOTE: glibc difference - For the ENOENT case, glibc also sets errno.
71 * We do not, as it really isn't an error if we reach the end-of-file.
72 * Doing so is analogous to having fgetc() set errno on EOF.
74 /**********************************************************************/
76 int fgetpwent_r(FILE *__restrict stream, struct passwd *__restrict resultbuf,
77 char *__restrict buffer, size_t buflen,
78 struct passwd **__restrict result)
84 if (!(rv = __pgsreader(__parsepwent, resultbuf, buffer, buflen, stream))) {
91 int fgetgrent_r(FILE *__restrict stream, struct group *__restrict resultbuf,
92 char *__restrict buffer, size_t buflen,
93 struct group **__restrict result)
99 if (!(rv = __pgsreader(__parsegrent, resultbuf, buffer, buflen, stream))) {
106 int fgetspent_r(FILE *__restrict stream, struct spwd *__restrict resultbuf,
107 char *__restrict buffer, size_t buflen,
108 struct spwd **__restrict result)
114 if (!(rv = __pgsreader(__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 extern int fgetspent_r(FILE *__restrict stream, struct spwd *__restrict resultbuf,
148 char *__restrict buffer, size_t buflen,
149 struct spwd **__restrict result);
150 struct spwd *fgetspent(FILE *stream)
152 static char buffer[PWD_BUFFER_SIZE];
153 static struct spwd resultbuf;
156 fgetspent_r(stream, &resultbuf, buffer, sizeof(buffer), &result);
160 int sgetspent_r(const char *string, struct spwd *result_buf,
161 char *buffer, size_t buflen, struct spwd **result)
167 if (buflen < PWD_BUFFER_SIZE) {
173 if (string != buffer) {
174 if (strlen(string) >= buflen) {
177 strcpy(buffer, string);
180 if (!(rv = __parsespent(result_buf, buffer))) {
181 *result = result_buf;
188 /**********************************************************************/
190 #ifdef GETXXKEY_R_FUNC
191 #error GETXXKEY_R_FUNC is already defined!
194 #define GETXXKEY_R_FUNC getpwnam_R
195 #define GETXXKEY_R_PARSER __parsepwent
196 #define GETXXKEY_R_ENTTYPE struct passwd
197 #define GETXXKEY_R_TEST(ENT) (!strcmp((ENT)->pw_name, key))
198 #define DO_GETXXKEY_R_KEYTYPE const char *__restrict
199 #define DO_GETXXKEY_R_PATHNAME _PATH_PASSWD
200 #include "pwd_grp_internal.c"
202 #define GETXXKEY_R_FUNC getgrnam_R
203 #define GETXXKEY_R_PARSER __parsegrent
204 #define GETXXKEY_R_ENTTYPE struct group
205 #define GETXXKEY_R_TEST(ENT) (!strcmp((ENT)->gr_name, key))
206 #define DO_GETXXKEY_R_KEYTYPE const char *__restrict
207 #define DO_GETXXKEY_R_PATHNAME _PATH_GROUP
208 #include "pwd_grp_internal.c"
210 #define GETXXKEY_R_FUNC getspnam_R
211 #define GETXXKEY_R_PARSER __parsespent
212 #define GETXXKEY_R_ENTTYPE struct spwd
213 #define GETXXKEY_R_TEST(ENT) (!strcmp((ENT)->sp_namp, key))
214 #define DO_GETXXKEY_R_KEYTYPE const char *__restrict
215 #define DO_GETXXKEY_R_PATHNAME _PATH_SHADOW
216 #include "pwd_grp_internal.c"
218 #define GETXXKEY_R_FUNC getpwuid_R
219 #define GETXXKEY_R_PARSER __parsepwent
220 #define GETXXKEY_R_ENTTYPE struct passwd
221 #define GETXXKEY_R_TEST(ENT) ((ENT)->pw_uid == key)
222 #define DO_GETXXKEY_R_KEYTYPE uid_t
223 #define DO_GETXXKEY_R_PATHNAME _PATH_PASSWD
224 #include "pwd_grp_internal.c"
226 #define GETXXKEY_R_FUNC getgrgid_R
227 #define GETXXKEY_R_PARSER __parsegrent
228 #define GETXXKEY_R_ENTTYPE struct group
229 #define GETXXKEY_R_TEST(ENT) ((ENT)->gr_gid == key)
230 #define DO_GETXXKEY_R_KEYTYPE gid_t
231 #define DO_GETXXKEY_R_PATHNAME _PATH_GROUP
232 #include "pwd_grp_internal.c"
234 /**********************************************************************/
236 struct passwd *getpwuid(uid_t uid)
238 static char buffer[PWD_BUFFER_SIZE];
239 static struct passwd resultbuf;
240 struct passwd *result;
242 getpwuid_r(uid, &resultbuf, buffer, sizeof(buffer), &result);
246 struct group *getgrgid(gid_t gid)
248 static char buffer[GRP_BUFFER_SIZE];
249 static struct group resultbuf;
250 struct group *result;
252 getgrgid_r(gid, &resultbuf, buffer, sizeof(buffer), &result);
256 /* This function is non-standard and is currently not built. It seems
257 * to have been created as a reentrant version of the non-standard
258 * functions getspuid. Why getspuid was added, I do not know. */
260 int getspuid_r(uid_t uid, struct spwd *__restrict resultbuf,
261 char *__restrict buffer, size_t buflen,
262 struct spwd **__restrict result)
266 struct passwd password;
267 char pwd_buff[PWD_BUFFER_SIZE];
270 if (!(rv = getpwuid_r(uid, &password, pwd_buff, sizeof(pwd_buff), &pp))) {
271 rv = getspnam_r(password.pw_name, resultbuf, buffer, buflen, result);
277 /* This function is non-standard and is currently not built.
278 * Why it was added, I do not know. */
280 struct spwd *getspuid(uid_t uid)
282 static char buffer[PWD_BUFFER_SIZE];
283 static struct spwd resultbuf;
286 getspuid_r(uid, &resultbuf, buffer, sizeof(buffer), &result);
290 struct passwd *getpwnam(const char *name)
292 static char buffer[PWD_BUFFER_SIZE];
293 static struct passwd resultbuf;
294 struct passwd *result;
296 getpwnam_r(name, &resultbuf, buffer, sizeof(buffer), &result);
300 struct group *getgrnam(const char *name)
302 static char buffer[GRP_BUFFER_SIZE];
303 static struct group resultbuf;
304 struct group *result;
306 getgrnam_r(name, &resultbuf, buffer, sizeof(buffer), &result);
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);
320 int getpw(uid_t uid, char *buf)
322 struct passwd resultbuf;
323 struct passwd *result;
324 char buffer[PWD_BUFFER_SIZE];
328 } else if (!getpwuid_r(uid, &resultbuf, buffer, sizeof(buffer), &result)) {
329 if (sprintf(buf, "%s:%s:%lu:%lu:%s:%s:%s\n",
330 resultbuf.pw_name, resultbuf.pw_passwd,
331 (unsigned long)(resultbuf.pw_uid),
332 (unsigned long)(resultbuf.pw_gid),
333 resultbuf.pw_gecos, resultbuf.pw_dir,
334 resultbuf.pw_shell) >= 0
343 /**********************************************************************/
345 #if defined CONFIG_USE_BB_THREADSAFE_SHADOW && defined PTHREAD_MUTEX_INITIALIZER
346 static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER;
347 # define LOCK pthread_mutex_lock(&mylock)
348 # define UNLOCK pthread_mutex_unlock(&mylock);
350 # define LOCK ((void) 0)
351 # define UNLOCK ((void) 0)
354 static FILE *pwf /*= NULL*/;
375 int getpwent_r(struct passwd *__restrict resultbuf,
376 char *__restrict buffer, size_t buflen,
377 struct passwd **__restrict result)
382 *result = NULL; /* In case of error... */
385 if (!(pwf = fopen(_PATH_PASSWD, "r"))) {
391 if (!(rv = __pgsreader(__parsepwent, resultbuf,
392 buffer, buflen, pwf))) {
401 static FILE *grf /*= NULL*/;
421 int getgrent_r(struct group *__restrict resultbuf,
422 char *__restrict buffer, size_t buflen,
423 struct group **__restrict result)
428 *result = NULL; /* In case of error... */
431 if (!(grf = fopen(_PATH_GROUP, "r"))) {
437 if (!(rv = __pgsreader(__parsegrent, resultbuf,
438 buffer, buflen, grf))) {
447 static FILE *spf /*= NULL*/;
467 int getspent_r(struct spwd *resultbuf, char *buffer,
468 size_t buflen, struct spwd **result)
473 *result = NULL; /* In case of error... */
476 if (!(spf = fopen(_PATH_SHADOW, "r"))) {
482 if (!(rv = __pgsreader(__parsespent, resultbuf,
483 buffer, buflen, spf))) {
492 struct passwd *getpwent(void)
494 static char line_buff[PWD_BUFFER_SIZE];
495 static struct passwd pwd;
496 struct passwd *result;
498 getpwent_r(&pwd, line_buff, sizeof(line_buff), &result);
502 struct group *getgrent(void)
504 static char line_buff[GRP_BUFFER_SIZE];
505 static struct group gr;
506 struct group *result;
508 getgrent_r(&gr, line_buff, sizeof(line_buff), &result);
512 struct spwd *getspent(void)
514 static char line_buff[PWD_BUFFER_SIZE];
515 static struct spwd spwd;
518 getspent_r(&spwd, line_buff, sizeof(line_buff), &result);
522 struct spwd *sgetspent(const char *string)
524 static char line_buff[PWD_BUFFER_SIZE];
525 static struct spwd spwd;
528 sgetspent_r(string, &spwd, line_buff, sizeof(line_buff), &result);
532 int initgroups(const char *user, gid_t gid)
539 char buff[PWD_BUFFER_SIZE];
543 /* We alloc space for 8 gids at a time. */
544 if (((group_list = (gid_t *) malloc(8*sizeof(gid_t *))) != NULL)
545 && ((grfile = fopen(_PATH_GROUP, "r")) != NULL)
551 while (!__pgsreader(__parsegrent, &group, buff, sizeof(buff), grfile)) {
552 assert(group.gr_mem); /* Must have at least a NULL terminator. */
553 if (group.gr_gid != gid) {
554 for (m=group.gr_mem ; *m ; m++) {
555 if (!strcmp(*m, user)) {
556 if (!(num_groups & 7)) {
557 gid_t *tmp = (gid_t *)
559 (num_groups+8) * sizeof(gid_t *));
566 group_list[num_groups++] = group.gr_gid;
573 rv = setgroups(num_groups, group_list);
578 /* group_list will be NULL if initial malloc failed, which may trigger
579 * warnings from various malloc debuggers. */
584 int putpwent(const struct passwd *__restrict p, FILE *__restrict f)
591 /* No extra thread locking is needed above what fprintf does. */
592 if (fprintf(f, "%s:%s:%lu:%lu:%s:%s:%s\n",
593 p->pw_name, p->pw_passwd,
594 (unsigned long)(p->pw_uid),
595 (unsigned long)(p->pw_gid),
596 p->pw_gecos, p->pw_dir, p->pw_shell) >= 0
605 int putgrent(const struct group *__restrict p, FILE *__restrict f)
607 static const char format[] = ",%s";
612 if (!p || !f) { /* Sigh... glibc checks. */
615 if (fprintf(f, "%s:%s:%lu:",
616 p->gr_name, p->gr_passwd,
617 (unsigned long)(p->gr_gid)) >= 0
627 if (fputc('\n', f) >= 0) {
632 if (fprintf(f, fmt, *m) < 0) {
646 static const unsigned char _sp_off[] = {
647 offsetof(struct spwd, sp_lstchg), /* 2 - not a char ptr */
648 offsetof(struct spwd, sp_min), /* 3 - not a char ptr */
649 offsetof(struct spwd, sp_max), /* 4 - not a char ptr */
650 offsetof(struct spwd, sp_warn), /* 5 - not a char ptr */
651 offsetof(struct spwd, sp_inact), /* 6 - not a char ptr */
652 offsetof(struct spwd, sp_expire), /* 7 - not a char ptr */
655 int putspent(const struct spwd *p, FILE *stream)
657 static const char ld_format[] = "%ld:";
663 /* Unlike putpwent and putgrent, glibc does not check the args. */
664 if (fprintf(stream, "%s:%s:", p->sp_namp,
665 (p->sp_pwdp ? p->sp_pwdp : "")) < 0
670 for (i=0 ; i < sizeof(_sp_off) ; i++) {
672 if ((x = *(const long int *)(((const char *) p) + _sp_off[i])) == -1) {
675 if (fprintf(stream, f, x) < 0) {
680 if ((p->sp_flag != ~0UL) && (fprintf(stream, "%lu", p->sp_flag) < 0)) {
684 if (fputc('\n', stream) > 0) {
692 /**********************************************************************/
693 /* Internal uClibc functions. */
694 /**********************************************************************/
696 static const unsigned char pw_off[] = {
697 offsetof(struct passwd, pw_name), /* 0 */
698 offsetof(struct passwd, pw_passwd), /* 1 */
699 offsetof(struct passwd, pw_uid), /* 2 - not a char ptr */
700 offsetof(struct passwd, pw_gid), /* 3 - not a char ptr */
701 offsetof(struct passwd, pw_gecos), /* 4 */
702 offsetof(struct passwd, pw_dir), /* 5 */
703 offsetof(struct passwd, pw_shell) /* 6 */
706 int __parsepwent(void *data, char *line)
714 p = ((char *) ((struct passwd *) data)) + pw_off[i];
716 if ((i & 6) ^ 2) { /* i!=2 and i!=3 */
717 *((char **) p) = line;
721 /* NOTE: glibc difference - glibc allows omission of
722 * ':' seperators after the gid field if all remaining
723 * entries are empty. We require all separators. */
724 if (!(line = strchr(line, ':'))) {
728 unsigned long t = strtoul(line, &endptr, 10);
729 /* Make sure we had at least one digit, and that the
730 * failing char is the next field seperator ':'. See
731 * glibc difference note above. */
732 /* TODO: Also check for leading whitespace? */
733 if ((endptr == line) || (*endptr != ':')) {
737 if (i & 1) { /* i == 3 -- gid */
739 } else { /* i == 2 -- uid */
751 /**********************************************************************/
753 static const unsigned char gr_off[] = {
754 offsetof(struct group, gr_name), /* 0 */
755 offsetof(struct group, gr_passwd), /* 1 */
756 offsetof(struct group, gr_gid) /* 2 - not a char ptr */
759 int __parsegrent(void *data, char *line)
767 end_of_buf = ((struct group *) data)->gr_name; /* Evil hack! */
770 p = ((char *) ((struct group *) data)) + gr_off[i];
773 *((char **) p) = line;
774 if (!(line = strchr(line, ':'))) {
780 *((gid_t *) p) = strtoul(line, &endptr, 10);
782 /* NOTE: glibc difference - glibc allows omission of the
783 * trailing colon when there is no member list. We treat
784 * this as an error. */
786 /* Make sure we had at least one digit, and that the
787 * failing char is the next field seperator ':'. See
788 * glibc difference note above. */
789 if ((endptr == line) || (*endptr != ':')) {
793 i = 1; /* Count terminating NULL ptr. */
796 if (p[1]) { /* We have a member list to process. */
797 /* Overwrite the last ':' with a ',' before counting.
798 * This allows us to test for initial ',' and adds
799 * one ',' so that the ',' count equals the member
803 /* NOTE: glibc difference - glibc allows and trims leading
804 * (but not trailing) space. We treat this as an error. */
805 /* NOTE: glibc difference - glibc allows consecutive and
806 * trailing commas, and ignores "empty string" users. We
807 * treat this as an error. */
810 *p = 0; /* nul-terminate each member string. */
811 if (!*++p || (*p == ',') || isspace(*p)) {
818 /* Now align (p+1), rounding up. */
819 /* Assumes sizeof(char **) is a power of 2. */
820 members = (char **)( (((intptr_t) p) + sizeof(char **))
821 & ~((intptr_t)(sizeof(char **) - 1)) );
823 if (((char *)(members + i)) > end_of_buf) { /* No space. */
827 ((struct group *) data)->gr_mem = members;
830 p = endptr; /* Pointing to char prior to first member. */
847 /**********************************************************************/
849 static const unsigned char sp_off[] = {
850 offsetof(struct spwd, sp_namp), /* 0 */
851 offsetof(struct spwd, sp_pwdp), /* 1 */
852 offsetof(struct spwd, sp_lstchg), /* 2 - not a char ptr */
853 offsetof(struct spwd, sp_min), /* 3 - not a char ptr */
854 offsetof(struct spwd, sp_max), /* 4 - not a char ptr */
855 offsetof(struct spwd, sp_warn), /* 5 - not a char ptr */
856 offsetof(struct spwd, sp_inact), /* 6 - not a char ptr */
857 offsetof(struct spwd, sp_expire), /* 7 - not a char ptr */
858 offsetof(struct spwd, sp_flag) /* 8 - not a char ptr */
861 int __parsespent(void *data, char * line)
869 p = ((char *) ((struct spwd *) data)) + sp_off[i];
871 *((char **) p) = line;
872 if (!(line = strchr(line, ':'))) {
876 *((long *) p) = (long) strtoul(line, &endptr, 10);
878 if (endptr == line) {
879 *((long *) p) = ((i != 8) ? -1L : ((long)(~0UL)));
891 if (*endptr != ':') {
904 /**********************************************************************/
906 /* Reads until if EOF, or until if finds a line which fits in the buffer
907 * and for which the parser function succeeds.
909 * Returns 0 on success and ENOENT for end-of-file (glibc concession).
912 int __pgsreader(int (*__parserfunc)(void *d, char *line), void *data,
913 char *__restrict line_buff, size_t buflen, FILE *f)
919 if (buflen < PWD_BUFFER_SIZE) {
924 if (!fgets(line_buff, buflen, f)) {
931 line_len = strlen(line_buff) - 1; /* strlen() must be > 0. */
932 if (line_buff[line_len] == '\n') {
933 line_buff[line_len] = 0;
934 } else if (line_len + 2 == buflen) { /* line too long */
944 /* NOTE: glibc difference - glibc strips leading whitespace from
945 * records. We do not allow leading whitespace. */
947 /* Skip empty lines, comment lines, and lines with leading
949 if (*line_buff && (*line_buff != '#') && !isspace(*line_buff)) {
950 if (__parserfunc == __parsegrent) { /* Do evil group hack. */
951 /* The group entry parsing function needs to know where
952 * the end of the buffer is so that it can construct the
953 * group member ptr table. */
954 ((struct group *) data)->gr_name = line_buff + buflen;
957 if (!__parserfunc(data, line_buff)) {
969 /**********************************************************************/