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.
35 #define _PATH_SHADOW "/etc/shadow"
38 #define _PATH_PASSWD "/etc/passwd"
41 #define _PATH_GROUP "/etc/group"
44 /**********************************************************************/
45 /* Sizes for statically allocated buffers. */
47 /* If you change these values, also change _SC_GETPW_R_SIZE_MAX and
48 * _SC_GETGR_R_SIZE_MAX in libc/unistd/sysconf.c to match */
49 #define PWD_BUFFER_SIZE 256
50 #define GRP_BUFFER_SIZE 256
52 /**********************************************************************/
53 /* Prototypes for internal functions. */
55 extern int __parsepwent(void *pw, char *line);
56 extern int __parsegrent(void *gr, char *line);
57 extern int __parsespent(void *sp, char *line);
59 extern int __pgsreader(int (*__parserfunc)(void *d, char *line), void *data,
60 char *__restrict line_buff, size_t buflen, FILE *f);
62 /**********************************************************************/
63 /* For the various fget??ent_r funcs, return
66 * ENOENT: end-of-file encountered
67 * ERANGE: buflen too small
68 * other error values possible. See __pgsreader.
70 * Also, *result == resultbuf on success and NULL on failure.
72 * NOTE: glibc difference - For the ENOENT case, glibc also sets errno.
73 * We do not, as it really isn't an error if we reach the end-of-file.
74 * Doing so is analogous to having fgetc() set errno on EOF.
76 /**********************************************************************/
80 int fgetpwent_r(FILE *__restrict stream, struct passwd *__restrict resultbuf,
81 char *__restrict buffer, size_t buflen,
82 struct passwd **__restrict result)
88 if (!(rv = __pgsreader(__parsepwent, resultbuf, buffer, buflen, stream))) {
96 /**********************************************************************/
99 int fgetgrent_r(FILE *__restrict stream, struct group *__restrict resultbuf,
100 char *__restrict buffer, size_t buflen,
101 struct group **__restrict result)
107 if (!(rv = __pgsreader(__parsegrent, resultbuf, buffer, buflen, stream))) {
115 /**********************************************************************/
118 int fgetspent_r(FILE *__restrict stream, struct spwd *__restrict resultbuf,
119 char *__restrict buffer, size_t buflen,
120 struct spwd **__restrict result)
126 if (!(rv = __pgsreader(__parsespent, resultbuf, buffer, buflen, stream))) {
134 /**********************************************************************/
135 /* For the various fget??ent funcs, return NULL on failure and a
136 * pointer to the appropriate struct (statically allocated) on success.
138 /**********************************************************************/
141 struct passwd *fgetpwent(FILE *stream)
143 static char buffer[PWD_BUFFER_SIZE];
144 static struct passwd resultbuf;
145 struct passwd *result;
147 fgetpwent_r(stream, &resultbuf, buffer, sizeof(buffer), &result);
152 /**********************************************************************/
155 struct group *fgetgrent(FILE *stream)
157 static char buffer[GRP_BUFFER_SIZE];
158 static struct group resultbuf;
159 struct group *result;
161 fgetgrent_r(stream, &resultbuf, buffer, sizeof(buffer), &result);
166 /**********************************************************************/
169 extern int fgetspent_r(FILE *__restrict stream, struct spwd *__restrict resultbuf,
170 char *__restrict buffer, size_t buflen,
171 struct spwd **__restrict result);
172 struct spwd *fgetspent(FILE *stream)
174 static char buffer[PWD_BUFFER_SIZE];
175 static struct spwd resultbuf;
178 fgetspent_r(stream, &resultbuf, buffer, sizeof(buffer), &result);
183 /**********************************************************************/
186 int sgetspent_r(const char *string, struct spwd *result_buf,
187 char *buffer, size_t buflen, struct spwd **result)
193 if (buflen < PWD_BUFFER_SIZE) {
199 if (string != buffer) {
200 if (strlen(string) >= buflen) {
203 strcpy(buffer, string);
206 if (!(rv = __parsespent(result_buf, buffer))) {
207 *result = result_buf;
215 /**********************************************************************/
217 #ifdef GETXXKEY_R_FUNC
218 #error GETXXKEY_R_FUNC is already defined!
222 #define GETXXKEY_R_FUNC getpwnam_r
223 #define GETXXKEY_R_PARSER __parsepwent
224 #define GETXXKEY_R_ENTTYPE struct passwd
225 #define GETXXKEY_R_TEST(ENT) (!strcmp((ENT)->pw_name, key))
226 #define DO_GETXXKEY_R_KEYTYPE const char *__restrict
227 #define DO_GETXXKEY_R_PATHNAME _PATH_PASSWD
228 #include "pwd_grp_internal.c"
232 #define GETXXKEY_R_FUNC getgrnam_r
233 #define GETXXKEY_R_PARSER __parsegrent
234 #define GETXXKEY_R_ENTTYPE struct group
235 #define GETXXKEY_R_TEST(ENT) (!strcmp((ENT)->gr_name, key))
236 #define DO_GETXXKEY_R_KEYTYPE const char *__restrict
237 #define DO_GETXXKEY_R_PATHNAME _PATH_GROUP
238 #include "pwd_grp_internal.c"
242 #define GETXXKEY_R_FUNC getspnam_r
243 #define GETXXKEY_R_PARSER __parsespent
244 #define GETXXKEY_R_ENTTYPE struct spwd
245 #define GETXXKEY_R_TEST(ENT) (!strcmp((ENT)->sp_namp, key))
246 #define DO_GETXXKEY_R_KEYTYPE const char *__restrict
247 #define DO_GETXXKEY_R_PATHNAME _PATH_SHADOW
248 #include "pwd_grp_internal.c"
252 #define GETXXKEY_R_FUNC getpwuid_r
253 #define GETXXKEY_R_PARSER __parsepwent
254 #define GETXXKEY_R_ENTTYPE struct passwd
255 #define GETXXKEY_R_TEST(ENT) ((ENT)->pw_uid == key)
256 #define DO_GETXXKEY_R_KEYTYPE uid_t
257 #define DO_GETXXKEY_R_PATHNAME _PATH_PASSWD
258 #include "pwd_grp_internal.c"
262 #define GETXXKEY_R_FUNC getgrgid_r
263 #define GETXXKEY_R_PARSER __parsegrent
264 #define GETXXKEY_R_ENTTYPE struct group
265 #define GETXXKEY_R_TEST(ENT) ((ENT)->gr_gid == key)
266 #define DO_GETXXKEY_R_KEYTYPE gid_t
267 #define DO_GETXXKEY_R_PATHNAME _PATH_GROUP
268 #include "pwd_grp_internal.c"
271 /**********************************************************************/
274 struct passwd *getpwuid(uid_t uid)
276 static char buffer[PWD_BUFFER_SIZE];
277 static struct passwd resultbuf;
278 struct passwd *result;
280 getpwuid_r(uid, &resultbuf, buffer, sizeof(buffer), &result);
285 /**********************************************************************/
288 struct group *getgrgid(gid_t gid)
290 static char buffer[GRP_BUFFER_SIZE];
291 static struct group resultbuf;
292 struct group *result;
294 getgrgid_r(gid, &resultbuf, buffer, sizeof(buffer), &result);
299 /**********************************************************************/
302 /* This function is non-standard and is currently not built. It seems
303 * to have been created as a reentrant version of the non-standard
304 * functions getspuid. Why getspuid was added, I do not know. */
306 int getspuid_r(uid_t uid, struct spwd *__restrict resultbuf,
307 char *__restrict buffer, size_t buflen,
308 struct spwd **__restrict result)
312 struct passwd password;
313 char pwd_buff[PWD_BUFFER_SIZE];
316 if (!(rv = getpwuid_r(uid, &password, pwd_buff, sizeof(pwd_buff), &pp))) {
317 rv = getspnam_r(password.pw_name, resultbuf, buffer, buflen, result);
324 /**********************************************************************/
327 /* This function is non-standard and is currently not built.
328 * Why it was added, I do not know. */
330 struct spwd *getspuid(uid_t uid)
332 static char buffer[PWD_BUFFER_SIZE];
333 static struct spwd resultbuf;
336 getspuid_r(uid, &resultbuf, buffer, sizeof(buffer), &result);
341 /**********************************************************************/
344 struct passwd *getpwnam(const char *name)
346 static char buffer[PWD_BUFFER_SIZE];
347 static struct passwd resultbuf;
348 struct passwd *result;
350 getpwnam_r(name, &resultbuf, buffer, sizeof(buffer), &result);
355 /**********************************************************************/
358 struct group *getgrnam(const char *name)
360 static char buffer[GRP_BUFFER_SIZE];
361 static struct group resultbuf;
362 struct group *result;
364 getgrnam_r(name, &resultbuf, buffer, sizeof(buffer), &result);
369 /**********************************************************************/
372 struct spwd *getspnam(const char *name)
374 static char buffer[PWD_BUFFER_SIZE];
375 static struct spwd resultbuf;
378 getspnam_r(name, &resultbuf, buffer, sizeof(buffer), &result);
383 /**********************************************************************/
386 int getpw(uid_t uid, char *buf)
388 struct passwd resultbuf;
389 struct passwd *result;
390 char buffer[PWD_BUFFER_SIZE];
394 } else if (!getpwuid_r(uid, &resultbuf, buffer, sizeof(buffer), &result)) {
395 if (sprintf(buf, "%s:%s:%lu:%lu:%s:%s:%s\n",
396 resultbuf.pw_name, resultbuf.pw_passwd,
397 (unsigned long)(resultbuf.pw_uid),
398 (unsigned long)(resultbuf.pw_gid),
399 resultbuf.pw_gecos, resultbuf.pw_dir,
400 resultbuf.pw_shell) >= 0
410 /**********************************************************************/
412 #if defined(L_getpwent_r) || defined(L_getgrent_r) || defined(L_getspent_r)
413 #if defined CONFIG_USE_BB_THREADSAFE_SHADOW && defined PTHREAD_MUTEX_INITIALIZER
414 static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER;
415 # define LOCK pthread_mutex_lock(&mylock)
416 # define UNLOCK pthread_mutex_unlock(&mylock);
418 # define LOCK ((void) 0)
419 # define UNLOCK ((void) 0)
424 static FILE *pwf /*= NULL*/;
445 int getpwent_r(struct passwd *__restrict resultbuf,
446 char *__restrict buffer, size_t buflen,
447 struct passwd **__restrict result)
452 *result = NULL; /* In case of error... */
455 if (!(pwf = fopen(_PATH_PASSWD, "r"))) {
461 if (!(rv = __pgsreader(__parsepwent, resultbuf,
462 buffer, buflen, pwf))) {
472 /**********************************************************************/
475 static FILE *grf /*= NULL*/;
495 int getgrent_r(struct group *__restrict resultbuf,
496 char *__restrict buffer, size_t buflen,
497 struct group **__restrict result)
502 *result = NULL; /* In case of error... */
505 if (!(grf = fopen(_PATH_GROUP, "r"))) {
511 if (!(rv = __pgsreader(__parsegrent, resultbuf,
512 buffer, buflen, grf))) {
522 /**********************************************************************/
525 static FILE *spf /*= NULL*/;
545 int getspent_r(struct spwd *resultbuf, char *buffer,
546 size_t buflen, struct spwd **result)
551 *result = NULL; /* In case of error... */
554 if (!(spf = fopen(_PATH_SHADOW, "r"))) {
560 if (!(rv = __pgsreader(__parsespent, resultbuf,
561 buffer, buflen, spf))) {
571 /**********************************************************************/
574 struct passwd *getpwent(void)
576 static char line_buff[PWD_BUFFER_SIZE];
577 static struct passwd pwd;
578 struct passwd *result;
580 getpwent_r(&pwd, line_buff, sizeof(line_buff), &result);
585 /**********************************************************************/
588 struct group *getgrent(void)
590 static char line_buff[GRP_BUFFER_SIZE];
591 static struct group gr;
592 struct group *result;
594 getgrent_r(&gr, line_buff, sizeof(line_buff), &result);
599 /**********************************************************************/
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);
613 /**********************************************************************/
616 struct spwd *sgetspent(const char *string)
618 static char line_buff[PWD_BUFFER_SIZE];
619 static struct spwd spwd;
622 sgetspent_r(string, &spwd, line_buff, sizeof(line_buff), &result);
627 /**********************************************************************/
630 int initgroups(const char *user, gid_t gid)
637 char buff[PWD_BUFFER_SIZE];
641 /* We alloc space for 8 gids at a time. */
642 if (((group_list = (gid_t *) malloc(8*sizeof(gid_t *))) != NULL)
643 && ((grfile = fopen(_PATH_GROUP, "r")) != NULL)
649 while (!__pgsreader(__parsegrent, &group, buff, sizeof(buff), grfile)) {
650 assert(group.gr_mem); /* Must have at least a NULL terminator. */
651 if (group.gr_gid != gid) {
652 for (m=group.gr_mem ; *m ; m++) {
653 if (!strcmp(*m, user)) {
654 if (!(num_groups & 7)) {
655 gid_t *tmp = (gid_t *)
657 (num_groups+8) * sizeof(gid_t *));
664 group_list[num_groups++] = group.gr_gid;
671 rv = setgroups(num_groups, group_list);
676 /* group_list will be NULL if initial malloc failed, which may trigger
677 * warnings from various malloc debuggers. */
683 /**********************************************************************/
686 int putpwent(const struct passwd *__restrict p, FILE *__restrict f)
693 /* No extra thread locking is needed above what fprintf does. */
694 if (fprintf(f, "%s:%s:%lu:%lu:%s:%s:%s\n",
695 p->pw_name, p->pw_passwd,
696 (unsigned long)(p->pw_uid),
697 (unsigned long)(p->pw_gid),
698 p->pw_gecos, p->pw_dir, p->pw_shell) >= 0
708 /**********************************************************************/
711 int putgrent(const struct group *__restrict p, FILE *__restrict f)
713 static const char format[] = ",%s";
718 if (!p || !f) { /* Sigh... glibc checks. */
721 if (fprintf(f, "%s:%s:%lu:",
722 p->gr_name, p->gr_passwd,
723 (unsigned long)(p->gr_gid)) >= 0
733 if (fputc('\n', f) >= 0) {
738 if (fprintf(f, fmt, *m) < 0) {
753 /**********************************************************************/
756 static const unsigned char _sp_off[] = {
757 offsetof(struct spwd, sp_lstchg), /* 2 - not a char ptr */
758 offsetof(struct spwd, sp_min), /* 3 - not a char ptr */
759 offsetof(struct spwd, sp_max), /* 4 - not a char ptr */
760 offsetof(struct spwd, sp_warn), /* 5 - not a char ptr */
761 offsetof(struct spwd, sp_inact), /* 6 - not a char ptr */
762 offsetof(struct spwd, sp_expire), /* 7 - not a char ptr */
765 int putspent(const struct spwd *p, FILE *stream)
767 static const char ld_format[] = "%ld:";
773 /* Unlike putpwent and putgrent, glibc does not check the args. */
774 if (fprintf(stream, "%s:%s:", p->sp_namp,
775 (p->sp_pwdp ? p->sp_pwdp : "")) < 0
780 for (i=0 ; i < sizeof(_sp_off) ; i++) {
782 if ((x = *(const long int *)(((const char *) p) + _sp_off[i])) == -1) {
785 if (fprintf(stream, f, x) < 0) {
790 if ((p->sp_flag != ~0UL) && (fprintf(stream, "%lu", p->sp_flag) < 0)) {
794 if (fputc('\n', stream) > 0) {
803 /**********************************************************************/
804 /* Internal uClibc functions. */
805 /**********************************************************************/
806 #ifdef L___parsepwent
808 static const unsigned char pw_off[] = {
809 offsetof(struct passwd, pw_name), /* 0 */
810 offsetof(struct passwd, pw_passwd), /* 1 */
811 offsetof(struct passwd, pw_uid), /* 2 - not a char ptr */
812 offsetof(struct passwd, pw_gid), /* 3 - not a char ptr */
813 offsetof(struct passwd, pw_gecos), /* 4 */
814 offsetof(struct passwd, pw_dir), /* 5 */
815 offsetof(struct passwd, pw_shell) /* 6 */
818 int __parsepwent(void *data, char *line)
826 p = ((char *) ((struct passwd *) data)) + pw_off[i];
828 if ((i & 6) ^ 2) { /* i!=2 and i!=3 */
829 *((char **) p) = line;
833 /* NOTE: glibc difference - glibc allows omission of
834 * ':' seperators after the gid field if all remaining
835 * entries are empty. We require all separators. */
836 if (!(line = strchr(line, ':'))) {
840 unsigned long t = strtoul(line, &endptr, 10);
841 /* Make sure we had at least one digit, and that the
842 * failing char is the next field seperator ':'. See
843 * glibc difference note above. */
844 /* TODO: Also check for leading whitespace? */
845 if ((endptr == line) || (*endptr != ':')) {
849 if (i & 1) { /* i == 3 -- gid */
851 } else { /* i == 2 -- uid */
864 /**********************************************************************/
865 #ifdef L___parsegrent
867 static const unsigned char gr_off[] = {
868 offsetof(struct group, gr_name), /* 0 */
869 offsetof(struct group, gr_passwd), /* 1 */
870 offsetof(struct group, gr_gid) /* 2 - not a char ptr */
873 int __parsegrent(void *data, char *line)
881 end_of_buf = ((struct group *) data)->gr_name; /* Evil hack! */
884 p = ((char *) ((struct group *) data)) + gr_off[i];
887 *((char **) p) = line;
888 if (!(line = strchr(line, ':'))) {
894 *((gid_t *) p) = strtoul(line, &endptr, 10);
896 /* NOTE: glibc difference - glibc allows omission of the
897 * trailing colon when there is no member list. We treat
898 * this as an error. */
900 /* Make sure we had at least one digit, and that the
901 * failing char is the next field seperator ':'. See
902 * glibc difference note above. */
903 if ((endptr == line) || (*endptr != ':')) {
907 i = 1; /* Count terminating NULL ptr. */
910 if (p[1]) { /* We have a member list to process. */
911 /* Overwrite the last ':' with a ',' before counting.
912 * This allows us to test for initial ',' and adds
913 * one ',' so that the ',' count equals the member
917 /* NOTE: glibc difference - glibc allows and trims leading
918 * (but not trailing) space. We treat this as an error. */
919 /* NOTE: glibc difference - glibc allows consecutive and
920 * trailing commas, and ignores "empty string" users. We
921 * treat this as an error. */
924 *p = 0; /* nul-terminate each member string. */
925 if (!*++p || (*p == ',') || isspace(*p)) {
932 /* Now align (p+1), rounding up. */
933 /* Assumes sizeof(char **) is a power of 2. */
934 members = (char **)( (((intptr_t) p) + sizeof(char **))
935 & ~((intptr_t)(sizeof(char **) - 1)) );
937 if (((char *)(members + i)) > end_of_buf) { /* No space. */
941 ((struct group *) data)->gr_mem = members;
944 p = endptr; /* Pointing to char prior to first member. */
962 /**********************************************************************/
963 #ifdef L___parsespent
965 static const unsigned char sp_off[] = {
966 offsetof(struct spwd, sp_namp), /* 0 */
967 offsetof(struct spwd, sp_pwdp), /* 1 */
968 offsetof(struct spwd, sp_lstchg), /* 2 - not a char ptr */
969 offsetof(struct spwd, sp_min), /* 3 - not a char ptr */
970 offsetof(struct spwd, sp_max), /* 4 - not a char ptr */
971 offsetof(struct spwd, sp_warn), /* 5 - not a char ptr */
972 offsetof(struct spwd, sp_inact), /* 6 - not a char ptr */
973 offsetof(struct spwd, sp_expire), /* 7 - not a char ptr */
974 offsetof(struct spwd, sp_flag) /* 8 - not a char ptr */
977 int __parsespent(void *data, char * line)
985 p = ((char *) ((struct spwd *) data)) + sp_off[i];
987 *((char **) p) = line;
988 if (!(line = strchr(line, ':'))) {
992 *((long *) p) = (long) strtoul(line, &endptr, 10);
994 if (endptr == line) {
995 *((long *) p) = ((i != 8) ? -1L : ((long)(~0UL)));
1007 if (*endptr != ':') {
1021 /**********************************************************************/
1022 #ifdef L___pgsreader
1024 /* Reads until if EOF, or until if finds a line which fits in the buffer
1025 * and for which the parser function succeeds.
1027 * Returns 0 on success and ENOENT for end-of-file (glibc concession).
1030 int __pgsreader(int (*__parserfunc)(void *d, char *line), void *data,
1031 char *__restrict line_buff, size_t buflen, FILE *f)
1037 if (buflen < PWD_BUFFER_SIZE) {
1042 if (!fgets(line_buff, buflen, f)) {
1049 line_len = strlen(line_buff) - 1; /* strlen() must be > 0. */
1050 if (line_buff[line_len] == '\n') {
1051 line_buff[line_len] = 0;
1052 } else if (line_len + 2 == buflen) { /* line too long */
1062 /* NOTE: glibc difference - glibc strips leading whitespace from
1063 * records. We do not allow leading whitespace. */
1065 /* Skip empty lines, comment lines, and lines with leading
1067 if (*line_buff && (*line_buff != '#') && !isspace(*line_buff)) {
1068 if (__parserfunc == __parsegrent) { /* Do evil group hack. */
1069 /* The group entry parsing function needs to know where
1070 * the end of the buffer is so that it can construct the
1071 * group member ptr table. */
1072 ((struct group *) data)->gr_name = line_buff + buflen;
1075 if (!__parserfunc(data, line_buff)) {
1088 /**********************************************************************/