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 staticly allocated)
15 * buffers; especially for the group-related functions since
16 * 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 staticly 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 /**********************************************************************/
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 (staticly 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
230 #define GETXXKEY_R_FUNC getgrnam_r
231 #define GETXXKEY_R_PARSER __parsegrent
232 #define GETXXKEY_R_ENTTYPE struct group
233 #define GETXXKEY_R_TEST(ENT) (!strcmp((ENT)->gr_name, key))
234 #define DO_GETXXKEY_R_KEYTYPE const char *__restrict
235 #define DO_GETXXKEY_R_PATHNAME _PATH_GROUP
239 #define GETXXKEY_R_FUNC getspnam_r
240 #define GETXXKEY_R_PARSER __parsespent
241 #define GETXXKEY_R_ENTTYPE struct spwd
242 #define GETXXKEY_R_TEST(ENT) (!strcmp((ENT)->sp_namp, key))
243 #define DO_GETXXKEY_R_KEYTYPE const char *__restrict
244 #define DO_GETXXKEY_R_PATHNAME _PATH_SHADOW
248 #define GETXXKEY_R_FUNC getpwuid_r
249 #define GETXXKEY_R_PARSER __parsepwent
250 #define GETXXKEY_R_ENTTYPE struct passwd
251 #define GETXXKEY_R_TEST(ENT) ((ENT)->pw_uid == key)
252 #define DO_GETXXKEY_R_KEYTYPE uid_t
253 #define DO_GETXXKEY_R_PATHNAME _PATH_PASSWD
257 #define GETXXKEY_R_FUNC getgrgid_r
258 #define GETXXKEY_R_PARSER __parsegrent
259 #define GETXXKEY_R_ENTTYPE struct group
260 #define GETXXKEY_R_TEST(ENT) ((ENT)->gr_gid == key)
261 #define DO_GETXXKEY_R_KEYTYPE gid_t
262 #define DO_GETXXKEY_R_PATHNAME _PATH_GROUP
265 /**********************************************************************/
266 #ifdef GETXXKEY_R_FUNC
268 int GETXXKEY_R_FUNC(DO_GETXXKEY_R_KEYTYPE key,
269 GETXXKEY_R_ENTTYPE *__restrict resultbuf,
270 char *__restrict buffer, size_t buflen,
271 GETXXKEY_R_ENTTYPE **__restrict result)
278 if (!(stream = fopen(DO_GETXXKEY_R_PATHNAME, "r"))) {
282 if (!(rv = __pgsreader(GETXXKEY_R_PARSER, resultbuf,
283 buffer, buflen, stream))
285 if (GETXXKEY_R_TEST(resultbuf)) { /* Found key? */
290 if (rv == ENOENT) { /* end-of-file encountered. */
303 /**********************************************************************/
306 struct passwd *getpwuid(uid_t uid)
308 static char buffer[PWD_BUFFER_SIZE];
309 static struct passwd resultbuf;
310 struct passwd *result;
312 getpwuid_r(uid, &resultbuf, buffer, sizeof(buffer), &result);
317 /**********************************************************************/
320 struct group *getgrgid(gid_t gid)
322 static char buffer[GRP_BUFFER_SIZE];
323 static struct group resultbuf;
324 struct group *result;
326 getgrgid_r(gid, &resultbuf, buffer, sizeof(buffer), &result);
331 /**********************************************************************/
334 /* This function is non-standard and is currently not built. It seems
335 * to have been created as a reentrant version of the non-standard
336 * functions getspuid. Why getspuid was added, I do not know. */
338 int getspuid_r(uid_t uid, struct spwd *__restrict resultbuf,
339 char *__restrict buffer, size_t buflen,
340 struct spwd **__restrict result)
344 struct passwd password;
345 char pwd_buff[PWD_BUFFER_SIZE];
348 if (!(rv = getpwuid_r(uid, &password, pwd_buff, sizeof(pwd_buff), &pp))) {
349 rv = getspnam_r(password.pw_name, resultbuf, buffer, buflen, result);
356 /**********************************************************************/
359 /* This function is non-standard and is currently not built.
360 * Why it was added, I do not know. */
362 struct spwd *getspuid(uid_t uid)
364 static char buffer[PWD_BUFFER_SIZE];
365 static struct spwd resultbuf;
368 getspuid_r(uid, &resultbuf, buffer, sizeof(buffer), &result);
373 /**********************************************************************/
376 struct passwd *getpwnam(const char *name)
378 static char buffer[PWD_BUFFER_SIZE];
379 static struct passwd resultbuf;
380 struct passwd *result;
382 getpwnam_r(name, &resultbuf, buffer, sizeof(buffer), &result);
387 /**********************************************************************/
390 struct group *getgrnam(const char *name)
392 static char buffer[GRP_BUFFER_SIZE];
393 static struct group resultbuf;
394 struct group *result;
396 getgrnam_r(name, &resultbuf, buffer, sizeof(buffer), &result);
401 /**********************************************************************/
404 struct spwd *getspnam(const char *name)
406 static char buffer[PWD_BUFFER_SIZE];
407 static struct spwd resultbuf;
410 getspnam_r(name, &resultbuf, buffer, sizeof(buffer), &result);
415 /**********************************************************************/
418 int getpw(uid_t uid, char *buf)
420 struct passwd resultbuf;
421 struct passwd *result;
422 char buffer[PWD_BUFFER_SIZE];
426 } else if (!getpwuid_r(uid, &resultbuf, buffer, sizeof(buffer), &result)) {
427 if (sprintf(buf, "%s:%s:%lu:%lu:%s:%s:%s\n",
428 resultbuf.pw_name, resultbuf.pw_passwd,
429 (unsigned long)(resultbuf.pw_uid),
430 (unsigned long)(resultbuf.pw_gid),
431 resultbuf.pw_gecos, resultbuf.pw_dir,
432 resultbuf.pw_shell) >= 0
442 /**********************************************************************/
445 static FILE *pwf /*= NULL*/;
462 int getpwent_r(struct passwd *__restrict resultbuf,
463 char *__restrict buffer, size_t buflen,
464 struct passwd **__restrict result)
468 *result = NULL; /* In case of error... */
471 if (!(pwf = fopen(_PATH_PASSWD, "r"))) {
477 if (!(rv = __pgsreader(__parsepwent, resultbuf,
478 buffer, buflen, pwf))) {
487 /**********************************************************************/
490 static FILE *grf /*= NULL*/;
506 int getgrent_r(struct group *__restrict resultbuf,
507 char *__restrict buffer, size_t buflen,
508 struct group **__restrict result)
512 *result = NULL; /* In case of error... */
515 if (!(grf = fopen(_PATH_GROUP, "r"))) {
521 if (!(rv = __pgsreader(__parsegrent, resultbuf,
522 buffer, buflen, grf))) {
531 /**********************************************************************/
534 static FILE *spf /*= NULL*/;
550 int getspent_r(struct spwd *resultbuf, char *buffer,
551 size_t buflen, struct spwd **result)
555 *result = NULL; /* In case of error... */
558 if (!(spf = fopen(_PATH_SHADOW, "r"))) {
564 if (!(rv = __pgsreader(__parsespent, resultbuf,
565 buffer, buflen, spf))) {
574 /**********************************************************************/
577 struct passwd *getpwent(void)
579 static char line_buff[PWD_BUFFER_SIZE];
580 static struct passwd pwd;
581 struct passwd *result;
583 getpwent_r(&pwd, line_buff, sizeof(line_buff), &result);
588 /**********************************************************************/
591 struct group *getgrent(void)
593 static char line_buff[GRP_BUFFER_SIZE];
594 static struct group gr;
595 struct group *result;
597 getgrent_r(&gr, line_buff, sizeof(line_buff), &result);
602 /**********************************************************************/
605 struct spwd *getspent(void)
607 static char line_buff[PWD_BUFFER_SIZE];
608 static struct spwd spwd;
611 getspent_r(&spwd, line_buff, sizeof(line_buff), &result);
616 /**********************************************************************/
619 struct spwd *sgetspent(const char *string)
621 static char line_buff[PWD_BUFFER_SIZE];
622 static struct spwd spwd;
625 sgetspent_r(string, &spwd, line_buff, sizeof(line_buff), &result);
630 /**********************************************************************/
633 int initgroups(const char *user, gid_t gid)
640 char buff[PWD_BUFFER_SIZE];
644 /* We alloc space for 8 gids at a time. */
645 if (((group_list = (gid_t *) malloc(8*sizeof(gid_t *))) != NULL)
646 && ((grf = fopen(_PATH_GROUP, "r")) != NULL)
652 while (!__pgsreader(__parsegrent, &group, buff, sizeof(buff), grf)) {
653 assert(group.gr_mem); /* Must have at least a NULL terminator. */
654 if (group.gr_gid != gid) {
655 for (m=group.gr_mem ; *m ; m++) {
656 if (!strcmp(*m, user)) {
657 if (!(num_groups & 7)) {
658 gid_t *tmp = (gid_t *)
660 (num_groups+8) * sizeof(gid_t *));
667 group_list[num_groups++] = group.gr_gid;
674 rv = setgroups(num_groups, group_list);
679 /* group_list will be NULL if initial malloc failed, which may trigger
680 * warnings from various malloc debuggers. */
686 /**********************************************************************/
689 int putpwent(const struct passwd *__restrict p, FILE *__restrict f)
696 /* No extra thread locking is needed above what fprintf does. */
697 if (fprintf(f, "%s:%s:%lu:%lu:%s:%s:%s\n",
698 p->pw_name, p->pw_passwd,
699 (unsigned long)(p->pw_uid),
700 (unsigned long)(p->pw_gid),
701 p->pw_gecos, p->pw_dir, p->pw_shell) >= 0
711 /**********************************************************************/
714 int putgrent(const struct group *__restrict p, FILE *__restrict f)
716 static const char format[] = ",%s";
721 if (!p || !f) { /* Sigh... glibc checks. */
724 if (fprintf(f, "%s:%s:%lu:",
725 p->gr_name, p->gr_passwd,
726 (unsigned long)(p->gr_gid)) >= 0
736 if (fputc('\n', f) >= 0) {
741 if (fprintf(f, fmt, *m) < 0) {
756 /**********************************************************************/
759 static const unsigned char sp_off[] = {
760 offsetof(struct spwd, sp_lstchg), /* 2 - not a char ptr */
761 offsetof(struct spwd, sp_min), /* 3 - not a char ptr */
762 offsetof(struct spwd, sp_max), /* 4 - not a char ptr */
763 offsetof(struct spwd, sp_warn), /* 5 - not a char ptr */
764 offsetof(struct spwd, sp_inact), /* 6 - not a char ptr */
765 offsetof(struct spwd, sp_expire), /* 7 - not a char ptr */
768 int putspent(const struct spwd *p, FILE *stream)
770 static const char ld_format[] = "%ld:";
776 /* Unlike putpwent and putgrent, glibc does not check the args. */
777 if (fprintf(stream, "%s:%s:", p->sp_namp,
778 (p->sp_pwdp ? p->sp_pwdp : "")) < 0
783 for (i=0 ; i < sizeof(sp_off) ; i++) {
785 if ((x = *(const long int *)(((const char *) p) + sp_off[i])) == -1) {
788 if (fprintf(stream, f, x) < 0) {
793 if ((p->sp_flag != ~0UL) && (fprintf(stream, "%lu", p->sp_flag) < 0)) {
797 if (fputc('\n', stream) > 0) {
806 /**********************************************************************/
807 /* Internal uClibc functions. */
808 /**********************************************************************/
809 #ifdef L___parsepwent
811 static const unsigned char pw_off[] = {
812 offsetof(struct passwd, pw_name), /* 0 */
813 offsetof(struct passwd, pw_passwd), /* 1 */
814 offsetof(struct passwd, pw_uid), /* 2 - not a char ptr */
815 offsetof(struct passwd, pw_gid), /* 3 - not a char ptr */
816 offsetof(struct passwd, pw_gecos), /* 4 */
817 offsetof(struct passwd, pw_dir), /* 5 */
818 offsetof(struct passwd, pw_shell) /* 6 */
821 int __parsepwent(void *data, char *line)
829 p = ((char *) ((struct passwd *) data)) + pw_off[i];
831 if ((i & 6) ^ 2) { /* i!=2 and i!=3 */
832 *((char **) p) = line;
836 /* NOTE: glibc difference - glibc allows omission of
837 * ':' seperators after the gid field if all remaining
838 * entries are empty. We require all separators. */
839 if (!(line = strchr(line, ':'))) {
843 unsigned long t = strtoul(line, &endptr, 10);
844 /* Make sure we had at least one digit, and that the
845 * failing char is the next field seperator ':'. See
846 * glibc difference note above. */
847 /* TODO: Also check for leading whitespace? */
848 if ((endptr == line) || (*endptr != ':')) {
852 if (i & 1) { /* i == 3 -- gid */
854 } else { /* i == 2 -- uid */
867 /**********************************************************************/
868 #ifdef L___parsegrent
870 static const unsigned char gr_off[] = {
871 offsetof(struct group, gr_name), /* 0 */
872 offsetof(struct group, gr_passwd), /* 1 */
873 offsetof(struct group, gr_gid) /* 2 - not a char ptr */
876 int __parsegrent(void *data, char *line)
884 end_of_buf = ((struct group *) data)->gr_name; /* Evil hack! */
887 p = ((char *) ((struct group *) data)) + gr_off[i];
890 *((char **) p) = line;
891 if (!(line = strchr(line, ':'))) {
897 *((gid_t *) p) = strtoul(line, &endptr, 10);
899 /* NOTE: glibc difference - glibc allows omission of the
900 * trailing colon when there is no member list. We treat
901 * this as an error. */
903 /* Make sure we had at least one digit, and that the
904 * failing char is the next field seperator ':'. See
905 * glibc difference note above. */
906 if ((endptr == line) || (*endptr != ':')) {
910 i = 1; /* Count terminating NULL ptr. */
913 if (p[1]) { /* We have a member list to process. */
914 /* Overwrite the last ':' with a ',' before counting.
915 * This allows us to test for initial ',' and adds
916 * one ',' so that the ',' count equals the member
920 /* NOTE: glibc difference - glibc allows and trims leading
921 * (but not trailing) space. We treat this as an error. */
922 /* NOTE: glibc difference - glibc allows consecutive and
923 * trailing commas, and ignores "empty string" users. We
924 * treat this as an error. */
927 *p = 0; /* nul-terminate each member string. */
928 if (!*++p || (*p == ',') || isspace(*p)) {
935 /* Now align (p+1), rounding up. */
936 /* Assumes sizeof(char **) is a power of 2. */
937 members = (char **)( (((intptr_t) p) + sizeof(char **))
938 & ~((intptr_t)(sizeof(char **) - 1)) );
940 if (((char *)(members + i)) > end_of_buf) { /* No space. */
944 ((struct group *) data)->gr_mem = members;
947 p = endptr; /* Pointing to char prior to first member. */
965 /**********************************************************************/
966 #ifdef L___parsespent
968 static const unsigned char sp_off[] = {
969 offsetof(struct spwd, sp_namp), /* 0 */
970 offsetof(struct spwd, sp_pwdp), /* 1 */
971 offsetof(struct spwd, sp_lstchg), /* 2 - not a char ptr */
972 offsetof(struct spwd, sp_min), /* 3 - not a char ptr */
973 offsetof(struct spwd, sp_max), /* 4 - not a char ptr */
974 offsetof(struct spwd, sp_warn), /* 5 - not a char ptr */
975 offsetof(struct spwd, sp_inact), /* 6 - not a char ptr */
976 offsetof(struct spwd, sp_expire), /* 7 - not a char ptr */
977 offsetof(struct spwd, sp_flag) /* 8 - not a char ptr */
980 int __parsespent(void *data, char * line)
988 p = ((char *) ((struct spwd *) data)) + sp_off[i];
990 *((char **) p) = line;
991 if (!(line = strchr(line, ':'))) {
996 if (i==5) { /* Support for old format. */
997 while (isspace(*line)) ++line; /* glibc eats space here. */
999 ((struct spwd *) data)->sp_warn = -1;
1000 ((struct spwd *) data)->sp_inact = -1;
1001 ((struct spwd *) data)->sp_expire = -1;
1002 ((struct spwd *) data)->sp_flag = ~0UL;
1008 *((long *) p) = (long) strtoul(line, &endptr, 10);
1010 if (endptr == line) {
1011 *((long *) p) = ((i != 8) ? -1L : ((long)(~0UL)));
1023 if (*endptr != ':') {
1037 /**********************************************************************/
1038 #ifdef L___pgsreader
1040 /* Reads until if EOF, or until if finds a line which fits in the buffer
1041 * and for which the parser function succeeds.
1043 * Returns 0 on success and ENOENT for end-of-file (glibc concession).
1046 int __pgsreader(int (*__parserfunc)(void *d, char *line), void *data,
1047 char *__restrict line_buff, size_t buflen, FILE *f)
1053 if (buflen < PWD_BUFFER_SIZE) {
1058 if (!fgets(line_buff, buflen, f)) {
1065 line_len = strlen(line_buff) - 1; /* strlen() must be > 0. */
1066 if (line_buff[line_len] == '\n') {
1067 line_buff[line_len] = 0;
1068 } else if (line_len + 2 == buflen) { /* line too long */
1078 /* NOTE: glibc difference - glibc strips leading whitespace from
1079 * records. We do not allow leading whitespace. */
1081 /* Skip empty lines, comment lines, and lines with leading
1083 if (*line_buff && (*line_buff != '#') && !isspace(*line_buff)) {
1084 if (__parserfunc == __parsegrent) { /* Do evil group hack. */
1085 /* The group entry parsing function needs to know where
1086 * the end of the buffer is so that it can construct the
1087 * group member ptr table. */
1088 ((struct group *) data)->gr_name = line_buff + buflen;
1091 if (!__parserfunc(data, line_buff)) {
1104 /**********************************************************************/