1 /* Copyright (C) 2003 Manuel Novoa III
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Library General Public
5 * License as published by the Free Software Foundation; either
6 * version 2 of the License, or (at your option) any later version.
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Library General Public License for more details.
13 * You should have received a copy of the GNU Library General Public
14 * License along with this library; if not, write to the Free
15 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 /* Nov 6, 2003 Initial version.
20 * NOTE: This implementation is quite strict about requiring all
21 * field seperators. It also does not allow leading whitespace
22 * except when processing the numeric fields. glibc is more
23 * lenient. See the various glibc difference comments below.
26 * Move to dynamic allocation of (currently staticly allocated)
27 * buffers; especially for the group-related functions since
28 * large group member lists will cause error returns.
47 #define _PATH_SHADOW "/etc/shadow"
50 #define _PATH_PASSWD "/etc/passwd"
53 #define _PATH_GROUP "/etc/group"
56 /**********************************************************************/
57 /* Sizes for staticly allocated buffers. */
59 /* If you change these values, also change _SC_GETPW_R_SIZE_MAX and
60 * _SC_GETGR_R_SIZE_MAX in libc/unistd/sysconf.c to match */
61 #define PWD_BUFFER_SIZE 256
62 #define GRP_BUFFER_SIZE 256
64 /**********************************************************************/
65 /* Prototypes for internal functions. */
67 extern int __parsepwent(void *pw, char *line);
68 extern int __parsegrent(void *gr, char *line);
69 extern int __parsespent(void *sp, char *line);
71 extern int __pgsreader(int (*__parserfunc)(void *d, char *line), void *data,
72 char *__restrict line_buff, size_t buflen, FILE *f);
74 /**********************************************************************/
75 /* For the various fget??ent_r funcs, return
78 * ENOENT: end-of-file encountered
79 * ERANGE: buflen too small
80 * other error values possible. See __pgsreader.
82 * Also, *result == resultbuf on success and NULL on failure.
84 * NOTE: glibc difference - For the ENOENT case, glibc also sets errno.
85 * We do not, as it really isn't an error if we reach the end-of-file.
86 * Doing so is analogous to having fgetc() set errno on EOF.
88 /**********************************************************************/
91 int fgetpwent_r(FILE *__restrict stream, struct passwd *__restrict resultbuf,
92 char *__restrict buffer, size_t buflen,
93 struct passwd **__restrict result)
99 if (!(rv = __pgsreader(__parsepwent, resultbuf, buffer, buflen, stream))) {
107 /**********************************************************************/
110 int fgetgrent_r(FILE *__restrict stream, struct group *__restrict resultbuf,
111 char *__restrict buffer, size_t buflen,
112 struct group **__restrict result)
118 if (!(rv = __pgsreader(__parsegrent, resultbuf, buffer, buflen, stream))) {
126 /**********************************************************************/
129 int fgetspent_r(FILE *__restrict stream, struct spwd *__restrict resultbuf,
130 char *__restrict buffer, size_t buflen,
131 struct spwd **__restrict result)
137 if (!(rv = __pgsreader(__parsespent, resultbuf, buffer, buflen, stream))) {
145 /**********************************************************************/
146 /* For the various fget??ent funcs, return NULL on failure and a
147 * pointer to the appropriate struct (staticly allocated) on success.
149 /**********************************************************************/
152 struct passwd *fgetpwent(FILE *stream)
154 static char buffer[PWD_BUFFER_SIZE];
155 static struct passwd resultbuf;
156 struct passwd *result;
158 fgetpwent_r(stream, &resultbuf, buffer, sizeof(buffer), &result);
163 /**********************************************************************/
166 struct group *fgetgrent(FILE *stream)
168 static char buffer[GRP_BUFFER_SIZE];
169 static struct group resultbuf;
170 struct group *result;
172 fgetgrent_r(stream, &resultbuf, buffer, sizeof(buffer), &result);
177 /**********************************************************************/
180 extern int fgetspent_r(FILE *__restrict stream, struct spwd *__restrict resultbuf,
181 char *__restrict buffer, size_t buflen,
182 struct spwd **__restrict result);
183 struct spwd *fgetspent(FILE *stream)
185 static char buffer[PWD_BUFFER_SIZE];
186 static struct spwd resultbuf;
189 fgetspent_r(stream, &resultbuf, buffer, sizeof(buffer), &result);
194 /**********************************************************************/
197 int sgetspent_r(const char *string, struct spwd *result_buf,
198 char *buffer, size_t buflen, struct spwd **result)
204 if (buflen < PWD_BUFFER_SIZE) {
210 if (string != buffer) {
211 if (strlen(string) >= buflen) {
214 strcpy(buffer, string);
217 if (!(rv = __parsespent(result_buf, buffer))) {
218 *result = result_buf;
226 /**********************************************************************/
228 #ifdef GETXXKEY_R_FUNC
229 #error GETXXKEY_R_FUNC is already defined!
233 #define GETXXKEY_R_FUNC getpwnam_r
234 #define GETXXKEY_R_PARSER __parsepwent
235 #define GETXXKEY_R_ENTTYPE struct passwd
236 #define GETXXKEY_R_TEST(ENT) (!strcmp((ENT)->pw_name, key))
237 #define DO_GETXXKEY_R_KEYTYPE const char *__restrict
238 #define DO_GETXXKEY_R_PATHNAME _PATH_PASSWD
242 #define GETXXKEY_R_FUNC getgrnam_r
243 #define GETXXKEY_R_PARSER __parsegrent
244 #define GETXXKEY_R_ENTTYPE struct group
245 #define GETXXKEY_R_TEST(ENT) (!strcmp((ENT)->gr_name, key))
246 #define DO_GETXXKEY_R_KEYTYPE const char *__restrict
247 #define DO_GETXXKEY_R_PATHNAME _PATH_GROUP
251 #define GETXXKEY_R_FUNC getspnam_r
252 #define GETXXKEY_R_PARSER __parsespent
253 #define GETXXKEY_R_ENTTYPE struct spwd
254 #define GETXXKEY_R_TEST(ENT) (!strcmp((ENT)->sp_namp, key))
255 #define DO_GETXXKEY_R_KEYTYPE const char *__restrict
256 #define DO_GETXXKEY_R_PATHNAME _PATH_SHADOW
260 #define GETXXKEY_R_FUNC getpwuid_r
261 #define GETXXKEY_R_PARSER __parsepwent
262 #define GETXXKEY_R_ENTTYPE struct passwd
263 #define GETXXKEY_R_TEST(ENT) ((ENT)->pw_uid == key)
264 #define DO_GETXXKEY_R_KEYTYPE uid_t
265 #define DO_GETXXKEY_R_PATHNAME _PATH_PASSWD
269 #define GETXXKEY_R_FUNC getgrgid_r
270 #define GETXXKEY_R_PARSER __parsegrent
271 #define GETXXKEY_R_ENTTYPE struct group
272 #define GETXXKEY_R_TEST(ENT) ((ENT)->gr_gid == key)
273 #define DO_GETXXKEY_R_KEYTYPE gid_t
274 #define DO_GETXXKEY_R_PATHNAME _PATH_GROUP
277 /**********************************************************************/
278 #ifdef GETXXKEY_R_FUNC
280 int GETXXKEY_R_FUNC(DO_GETXXKEY_R_KEYTYPE key,
281 GETXXKEY_R_ENTTYPE *__restrict resultbuf,
282 char *__restrict buffer, size_t buflen,
283 GETXXKEY_R_ENTTYPE **__restrict result)
290 if (!(stream = fopen(DO_GETXXKEY_R_PATHNAME, "r"))) {
294 if (!(rv = __pgsreader(GETXXKEY_R_PARSER, resultbuf,
295 buffer, buflen, stream))
297 if (GETXXKEY_R_TEST(resultbuf)) { /* Found key? */
302 if (rv == ENOENT) { /* end-of-file encountered. */
315 /**********************************************************************/
318 struct passwd *getpwuid(uid_t uid)
320 static char buffer[PWD_BUFFER_SIZE];
321 static struct passwd resultbuf;
322 struct passwd *result;
324 getpwuid_r(uid, &resultbuf, buffer, sizeof(buffer), &result);
329 /**********************************************************************/
332 struct group *getgrgid(gid_t gid)
334 static char buffer[GRP_BUFFER_SIZE];
335 static struct group resultbuf;
336 struct group *result;
338 getgrgid_r(gid, &resultbuf, buffer, sizeof(buffer), &result);
343 /**********************************************************************/
346 /* This function is non-standard and is currently not built. It seems
347 * to have been created as a reentrant version of the non-standard
348 * functions getspuid. Why getspuid was added, I do not know. */
350 int getspuid_r(uid_t uid, struct spwd *__restrict resultbuf,
351 char *__restrict buffer, size_t buflen,
352 struct spwd **__restrict result)
356 struct passwd password;
357 char pwd_buff[PWD_BUFFER_SIZE];
360 if (!(rv = getpwuid_r(uid, &password, pwd_buff, sizeof(pwd_buff), &pp))) {
361 rv = getspnam_r(password.pw_name, resultbuf, buffer, buflen, result);
368 /**********************************************************************/
371 /* This function is non-standard and is currently not built.
372 * Why it was added, I do not know. */
374 struct spwd *getspuid(uid_t uid)
376 static char buffer[PWD_BUFFER_SIZE];
377 static struct spwd resultbuf;
380 getspuid_r(uid, &resultbuf, buffer, sizeof(buffer), &result);
385 /**********************************************************************/
388 struct passwd *getpwnam(const char *name)
390 static char buffer[PWD_BUFFER_SIZE];
391 static struct passwd resultbuf;
392 struct passwd *result;
394 getpwnam_r(name, &resultbuf, buffer, sizeof(buffer), &result);
399 /**********************************************************************/
402 struct group *getgrnam(const char *name)
404 static char buffer[GRP_BUFFER_SIZE];
405 static struct group resultbuf;
406 struct group *result;
408 getgrnam_r(name, &resultbuf, buffer, sizeof(buffer), &result);
413 /**********************************************************************/
416 struct spwd *getspnam(const char *name)
418 static char buffer[PWD_BUFFER_SIZE];
419 static struct spwd resultbuf;
422 getspnam_r(name, &resultbuf, buffer, sizeof(buffer), &result);
427 /**********************************************************************/
430 int getpw(uid_t uid, char *buf)
432 struct passwd resultbuf;
433 struct passwd *result;
434 char buffer[PWD_BUFFER_SIZE];
438 } else if (!getpwuid_r(uid, &resultbuf, buffer, sizeof(buffer), &result)) {
439 if (sprintf(buf, "%s:%s:%lu:%lu:%s:%s:%s\n",
440 resultbuf.pw_name, resultbuf.pw_passwd,
441 (unsigned long)(resultbuf.pw_uid),
442 (unsigned long)(resultbuf.pw_gid),
443 resultbuf.pw_gecos, resultbuf.pw_dir,
444 resultbuf.pw_shell) >= 0
454 /**********************************************************************/
457 static FILE *pwf /*= NULL*/;
474 int getpwent_r(struct passwd *__restrict resultbuf,
475 char *__restrict buffer, size_t buflen,
476 struct passwd **__restrict result)
480 *result = NULL; /* In case of error... */
483 if (!(pwf = fopen(_PATH_PASSWD, "r"))) {
489 if (!(rv = __pgsreader(__parsepwent, resultbuf,
490 buffer, buflen, pwf))) {
499 /**********************************************************************/
502 static FILE *grf /*= NULL*/;
518 int getgrent_r(struct group *__restrict resultbuf,
519 char *__restrict buffer, size_t buflen,
520 struct group **__restrict result)
524 *result = NULL; /* In case of error... */
527 if (!(grf = fopen(_PATH_GROUP, "r"))) {
533 if (!(rv = __pgsreader(__parsegrent, resultbuf,
534 buffer, buflen, grf))) {
543 /**********************************************************************/
546 static FILE *spf /*= NULL*/;
562 int getspent_r(struct spwd *resultbuf, char *buffer,
563 size_t buflen, struct spwd **result)
567 *result = NULL; /* In case of error... */
570 if (!(spf = fopen(_PATH_SHADOW, "r"))) {
576 if (!(rv = __pgsreader(__parsespent, resultbuf,
577 buffer, buflen, spf))) {
586 /**********************************************************************/
589 struct passwd *getpwent(void)
591 static char line_buff[PWD_BUFFER_SIZE];
592 static struct passwd pwd;
593 struct passwd *result;
595 getpwent_r(&pwd, line_buff, sizeof(line_buff), &result);
600 /**********************************************************************/
603 struct group *getgrent(void)
605 static char line_buff[GRP_BUFFER_SIZE];
606 static struct group gr;
607 struct group *result;
609 getgrent_r(&gr, line_buff, sizeof(line_buff), &result);
614 /**********************************************************************/
617 struct spwd *getspent(void)
619 static char line_buff[PWD_BUFFER_SIZE];
620 static struct spwd spwd;
623 getspent_r(&spwd, line_buff, sizeof(line_buff), &result);
628 /**********************************************************************/
631 struct spwd *sgetspent(const char *string)
633 static char line_buff[PWD_BUFFER_SIZE];
634 static struct spwd spwd;
637 sgetspent_r(string, &spwd, line_buff, sizeof(line_buff), &result);
642 /**********************************************************************/
645 int initgroups(const char *user, gid_t gid)
652 char buff[PWD_BUFFER_SIZE];
656 /* We alloc space for 8 gids at a time. */
657 if (((group_list = (gid_t *) malloc(8*sizeof(gid_t *))) != NULL)
658 && ((grf = fopen(_PATH_GROUP, "r")) != NULL)
664 while (!__pgsreader(__parsegrent, &group, buff, sizeof(buff), grf)) {
665 assert(group.gr_mem); /* Must have at least a NULL terminator. */
666 if (group.gr_gid != gid) {
667 for (m=group.gr_mem ; *m ; m++) {
668 if (!strcmp(*m, user)) {
669 if (!(num_groups & 7)) {
670 gid_t *tmp = (gid_t *)
672 (num_groups+8) * sizeof(gid_t *));
679 group_list[num_groups++] = group.gr_gid;
686 rv = setgroups(num_groups, group_list);
691 /* group_list will be NULL if initial malloc failed, which may trigger
692 * warnings from various malloc debuggers. */
698 /**********************************************************************/
701 int putpwent(const struct passwd *__restrict p, FILE *__restrict f)
708 /* No extra thread locking is needed above what fprintf does. */
709 if (fprintf(f, "%s:%s:%lu:%lu:%s:%s:%s\n",
710 p->pw_name, p->pw_passwd,
711 (unsigned long)(p->pw_uid),
712 (unsigned long)(p->pw_gid),
713 p->pw_gecos, p->pw_dir, p->pw_shell) >= 0
723 /**********************************************************************/
726 int putgrent(const struct group *__restrict p, FILE *__restrict f)
728 static const char format[] = ",%s";
733 if (!p || !f) { /* Sigh... glibc checks. */
736 if (fprintf(f, "%s:%s:%lu:",
737 p->gr_name, p->gr_passwd,
738 (unsigned long)(p->gr_gid)) >= 0
748 if (fputc_unlocked('\n', f) >= 0) {
753 if (fprintf(f, fmt, *m) < 0) {
768 /**********************************************************************/
771 static const unsigned char sp_off[] = {
772 offsetof(struct spwd, sp_lstchg), /* 2 - not a char ptr */
773 offsetof(struct spwd, sp_min), /* 3 - not a char ptr */
774 offsetof(struct spwd, sp_max), /* 4 - not a char ptr */
775 offsetof(struct spwd, sp_warn), /* 5 - not a char ptr */
776 offsetof(struct spwd, sp_inact), /* 6 - not a char ptr */
777 offsetof(struct spwd, sp_expire), /* 7 - not a char ptr */
780 int putspent(const struct spwd *p, FILE *stream)
782 static const char ld_format[] = "%ld:";
788 /* Unlike putpwent and putgrent, glibc does not check the args. */
789 if (fprintf(stream, "%s:%s:", p->sp_namp,
790 (p->sp_pwdp ? p->sp_pwdp : "")) < 0
795 for (i=0 ; i < sizeof(sp_off) ; i++) {
797 if ((x = *(const long int *)(((const char *) p) + sp_off[i])) == -1) {
800 if (fprintf(stream, f, x) < 0) {
805 if ((p->sp_flag != ~0UL) && (fprintf(stream, "%lu", p->sp_flag) < 0)) {
809 if (fputc_unlocked('\n', stream) > 0) {
818 /**********************************************************************/
819 /* Internal uClibc functions. */
820 /**********************************************************************/
821 #ifdef L___parsepwent
823 static const unsigned char pw_off[] = {
824 offsetof(struct passwd, pw_name), /* 0 */
825 offsetof(struct passwd, pw_passwd), /* 1 */
826 offsetof(struct passwd, pw_uid), /* 2 - not a char ptr */
827 offsetof(struct passwd, pw_gid), /* 3 - not a char ptr */
828 offsetof(struct passwd, pw_gecos), /* 4 */
829 offsetof(struct passwd, pw_dir), /* 5 */
830 offsetof(struct passwd, pw_shell) /* 6 */
833 int __parsepwent(void *data, char *line)
841 p = ((char *) ((struct passwd *) data)) + pw_off[i];
843 if ((i & 6) ^ 2) { /* i!=2 and i!=3 */
844 *((char **) p) = line;
848 /* NOTE: glibc difference - glibc allows omission of
849 * ':' seperators after the gid field if all remaining
850 * entries are empty. We require all separators. */
851 if (!(line = strchr(line, ':'))) {
855 unsigned long t = strtoul(line, &endptr, 10);
856 /* Make sure we had at least one digit, and that the
857 * failing char is the next field seperator ':'. See
858 * glibc difference note above. */
859 /* TODO: Also check for leading whitespace? */
860 if ((endptr == line) || (*endptr != ':')) {
864 if (i & 1) { /* i == 3 -- gid */
866 } else { /* i == 2 -- uid */
879 /**********************************************************************/
880 #ifdef L___parsegrent
882 static const unsigned char gr_off[] = {
883 offsetof(struct group, gr_name), /* 0 */
884 offsetof(struct group, gr_passwd), /* 1 */
885 offsetof(struct group, gr_gid) /* 2 - not a char ptr */
888 int __parsegrent(void *data, char *line)
896 end_of_buf = ((struct group *) data)->gr_name; /* Evil hack! */
899 p = ((char *) ((struct group *) data)) + gr_off[i];
902 *((char **) p) = line;
903 if (!(line = strchr(line, ':'))) {
909 *((gid_t *) p) = strtoul(line, &endptr, 10);
911 /* NOTE: glibc difference - glibc allows omission of the
912 * trailing colon when there is no member list. We treat
913 * this as an error. */
915 /* Make sure we had at least one digit, and that the
916 * failing char is the next field seperator ':'. See
917 * glibc difference note above. */
918 if ((endptr == line) || (*endptr != ':')) {
922 i = 1; /* Count terminating NULL ptr. */
925 if (p[1]) { /* We have a member list to process. */
926 /* Overwrite the last ':' with a ',' before counting.
927 * This allows us to test for initial ',' and adds
928 * one ',' so that the ',' count equals the member
932 /* NOTE: glibc difference - glibc allows and trims leading
933 * (but not trailing) space. We treat this as an error. */
934 /* NOTE: glibc difference - glibc allows consecutive and
935 * trailing commas, and ignores "empty string" users. We
936 * treat this as an error. */
939 *p = 0; /* nul-terminate each member string. */
940 if (!*++p || (*p == ',') || isspace(*p)) {
947 /* Now align (p+1), rounding up. */
948 /* Assumes sizeof(char **) is a power of 2. */
949 members = (char **)( (((intptr_t) p) + sizeof(char **))
950 & ~((intptr_t)(sizeof(char **) - 1)) );
952 if (((char *)(members + i)) > end_of_buf) { /* No space. */
956 ((struct group *) data)->gr_mem = members;
959 p = endptr; /* Pointing to char prior to first member. */
977 /**********************************************************************/
978 #ifdef L___parsespent
980 static const unsigned char sp_off[] = {
981 offsetof(struct spwd, sp_namp), /* 0 */
982 offsetof(struct spwd, sp_pwdp), /* 1 */
983 offsetof(struct spwd, sp_lstchg), /* 2 - not a char ptr */
984 offsetof(struct spwd, sp_min), /* 3 - not a char ptr */
985 offsetof(struct spwd, sp_max), /* 4 - not a char ptr */
986 offsetof(struct spwd, sp_warn), /* 5 - not a char ptr */
987 offsetof(struct spwd, sp_inact), /* 6 - not a char ptr */
988 offsetof(struct spwd, sp_expire), /* 7 - not a char ptr */
989 offsetof(struct spwd, sp_flag) /* 8 - not a char ptr */
992 int __parsespent(void *data, char * line)
1000 p = ((char *) ((struct spwd *) data)) + sp_off[i];
1002 *((char **) p) = line;
1003 if (!(line = strchr(line, ':'))) {
1008 if (i==5) { /* Support for old format. */
1009 while (isspace(*line)) ++line; /* glibc eats space here. */
1011 ((struct spwd *) data)->sp_warn = -1;
1012 ((struct spwd *) data)->sp_inact = -1;
1013 ((struct spwd *) data)->sp_expire = -1;
1014 ((struct spwd *) data)->sp_flag = ~0UL;
1020 *((long *) p) = (long) strtoul(line, &endptr, 10);
1022 if (endptr == line) {
1023 *((long *) p) = ((i != 8) ? -1L : ((long)(~0UL)));
1035 if (*endptr != ':') {
1049 /**********************************************************************/
1050 #ifdef L___pgsreader
1052 /* Reads until if EOF, or until if finds a line which fits in the buffer
1053 * and for which the parser function succeeds.
1055 * Returns 0 on success and ENOENT for end-of-file (glibc concession).
1058 int __pgsreader(int (*__parserfunc)(void *d, char *line), void *data,
1059 char *__restrict line_buff, size_t buflen, FILE *f)
1065 if (buflen < PWD_BUFFER_SIZE) {
1070 if (!fgets_unlocked(line_buff, buflen, f)) {
1071 if (feof_unlocked(f)) {
1077 line_len = strlen(line_buff) - 1; /* strlen() must be > 0. */
1078 if (line_buff[line_len] == '\n') {
1079 line_buff[line_len] = 0;
1080 } else if (line_len + 2 == buflen) { /* line too long */
1090 /* NOTE: glibc difference - glibc strips leading whitespace from
1091 * records. We do not allow leading whitespace. */
1093 /* Skip empty lines, comment lines, and lines with leading
1095 if (*line_buff && (*line_buff != '#') && !isspace(*line_buff)) {
1096 if (__parserfunc == __parsegrent) { /* Do evil group hack. */
1097 /* The group entry parsing function needs to know where
1098 * the end of the buffer is so that it can construct the
1099 * group member ptr table. */
1100 ((struct group *) data)->gr_name = line_buff + buflen;
1103 if (!__parserfunc(data, line_buff)) {
1116 /**********************************************************************/