1 /* vi: set sw=4 ts=4: */
2 /* Copyright (C) 2003 Manuel Novoa III
4 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
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.
23 /**********************************************************************/
24 /* Sizes for statically allocated buffers. */
26 #define PWD_BUFFER_SIZE 256
27 #define GRP_BUFFER_SIZE 256
29 /**********************************************************************/
30 /* Prototypes for internal functions. */
32 static int bb__pgsreader(
33 int FAST_FUNC (*parserfunc)(void *d, char *line),
35 char *__restrict line_buff,
39 static int FAST_FUNC bb__parsepwent(void *pw, char *line);
40 static int FAST_FUNC bb__parsegrent(void *gr, char *line);
41 #if ENABLE_USE_BB_SHADOW
42 static int FAST_FUNC bb__parsespent(void *sp, char *line);
45 /**********************************************************************/
46 /* We avoid having big global data. */
49 /* Smaller things first */
50 /* It's ok to use one buffer for getpwuid and getpwnam. Manpage says:
51 * "The return value may point to a static area, and may be overwritten
52 * by subsequent calls to getpwent(), getpwnam(), or getpwuid()."
54 struct passwd getpw_resultbuf;
55 struct group getgr_resultbuf;
57 char getpw_buffer[PWD_BUFFER_SIZE];
58 char getgr_buffer[GRP_BUFFER_SIZE];
59 #if 0 //ENABLE_USE_BB_SHADOW
60 struct spwd getsp_resultbuf;
61 char getsp_buffer[PWD_BUFFER_SIZE];
63 // Not converted - too small to bother
64 //pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER;
65 //FILE *pwf /*= NULL*/;
66 //FILE *grf /*= NULL*/;
67 //FILE *spf /*= NULL*/;
70 static struct statics *ptr_to_statics;
72 static struct statics *get_S(void)
75 ptr_to_statics = xzalloc(sizeof(*ptr_to_statics));
76 return ptr_to_statics;
79 /* Always use in this order, get_S() must be called first */
80 #define RESULTBUF(name) &((S = get_S())->name##_resultbuf)
81 #define BUFFER(name) (S->name##_buffer)
83 /**********************************************************************/
84 /* For the various fget??ent_r funcs, return
87 * ENOENT: end-of-file encountered
88 * ERANGE: buflen too small
89 * other error values possible. See bb__pgsreader.
91 * Also, *result == resultbuf on success and NULL on failure.
93 * NOTE: glibc difference - For the ENOENT case, glibc also sets errno.
94 * We do not, as it really isn't an error if we reach the end-of-file.
95 * Doing so is analogous to having fgetc() set errno on EOF.
97 /**********************************************************************/
99 int fgetpwent_r(FILE *__restrict stream, struct passwd *__restrict resultbuf,
100 char *__restrict buffer, size_t buflen,
101 struct passwd **__restrict result)
107 rv = bb__pgsreader(bb__parsepwent, resultbuf, buffer, buflen, stream);
115 int fgetgrent_r(FILE *__restrict stream, struct group *__restrict resultbuf,
116 char *__restrict buffer, size_t buflen,
117 struct group **__restrict result)
123 rv = bb__pgsreader(bb__parsegrent, resultbuf, buffer, buflen, stream);
131 #if ENABLE_USE_BB_SHADOW
132 #ifdef UNUSED_FOR_NOW
133 int fgetspent_r(FILE *__restrict stream, struct spwd *__restrict resultbuf,
134 char *__restrict buffer, size_t buflen,
135 struct spwd **__restrict result)
141 rv = bb__pgsreader(bb__parsespent, resultbuf, buffer, buflen, stream);
151 /**********************************************************************/
152 /* For the various fget??ent funcs, return NULL on failure and a
153 * pointer to the appropriate struct (statically allocated) on success.
154 * TODO: audit & stop using these in bbox, they pull in static buffers */
155 /**********************************************************************/
157 #ifdef UNUSED_SINCE_WE_AVOID_STATIC_BUFS
158 struct passwd *fgetpwent(FILE *stream)
161 struct passwd *resultbuf = RESULTBUF(getpw);
162 char *buffer = BUFFER(getpw);
163 struct passwd *result;
165 fgetpwent_r(stream, resultbuf, buffer, sizeof(BUFFER(getpw)), &result);
169 struct group *fgetgrent(FILE *stream)
172 struct group *resultbuf = RESULTBUF(getgr);
173 char *buffer = BUFFER(getgr);
174 struct group *result;
176 fgetgrent_r(stream, resultbuf, buffer, sizeof(BUFFER(getgr)), &result);
181 #if ENABLE_USE_BB_SHADOW
182 #ifdef UNUSED_SINCE_WE_AVOID_STATIC_BUFS
183 struct spwd *fgetspent(FILE *stream)
186 struct spwd *resultbuf = RESULTBUF(getsp);
187 char *buffer = BUFFER(getsp);
190 fgetspent_r(stream, resultbuf, buffer, sizeof(BUFFER(getsp)), &result);
195 #ifdef UNUSED_FOR_NOW
196 int sgetspent_r(const char *string, struct spwd *result_buf,
197 char *buffer, size_t buflen, struct spwd **result)
203 if (buflen < PWD_BUFFER_SIZE) {
209 if (string != buffer) {
210 if (strlen(string) >= buflen) {
213 strcpy(buffer, string);
216 rv = bb__parsespent(result_buf, buffer);
218 *result = result_buf;
225 #endif /* ENABLE_USE_BB_SHADOW */
227 /**********************************************************************/
229 #define GETXXKEY_R_FUNC getpwnam_r
230 #define GETXXKEY_R_PARSER bb__parsepwent
231 #define GETXXKEY_R_ENTTYPE struct passwd
232 #define GETXXKEY_R_TEST(ENT) (!strcmp((ENT)->pw_name, key))
233 #define GETXXKEY_R_KEYTYPE const char *__restrict
234 #define GETXXKEY_R_PATHNAME _PATH_PASSWD
235 #include "pwd_grp_internal.c"
237 #define GETXXKEY_R_FUNC getgrnam_r
238 #define GETXXKEY_R_PARSER bb__parsegrent
239 #define GETXXKEY_R_ENTTYPE struct group
240 #define GETXXKEY_R_TEST(ENT) (!strcmp((ENT)->gr_name, key))
241 #define GETXXKEY_R_KEYTYPE const char *__restrict
242 #define GETXXKEY_R_PATHNAME _PATH_GROUP
243 #include "pwd_grp_internal.c"
245 #if ENABLE_USE_BB_SHADOW
246 #define GETXXKEY_R_FUNC getspnam_r
247 #define GETXXKEY_R_PARSER bb__parsespent
248 #define GETXXKEY_R_ENTTYPE struct spwd
249 #define GETXXKEY_R_TEST(ENT) (!strcmp((ENT)->sp_namp, key))
250 #define GETXXKEY_R_KEYTYPE const char *__restrict
251 #define GETXXKEY_R_PATHNAME _PATH_SHADOW
252 #include "pwd_grp_internal.c"
255 #define GETXXKEY_R_FUNC getpwuid_r
256 #define GETXXKEY_R_PARSER bb__parsepwent
257 #define GETXXKEY_R_ENTTYPE struct passwd
258 #define GETXXKEY_R_TEST(ENT) ((ENT)->pw_uid == key)
259 #define GETXXKEY_R_KEYTYPE uid_t
260 #define GETXXKEY_R_PATHNAME _PATH_PASSWD
261 #include "pwd_grp_internal.c"
263 #define GETXXKEY_R_FUNC getgrgid_r
264 #define GETXXKEY_R_PARSER bb__parsegrent
265 #define GETXXKEY_R_ENTTYPE struct group
266 #define GETXXKEY_R_TEST(ENT) ((ENT)->gr_gid == key)
267 #define GETXXKEY_R_KEYTYPE gid_t
268 #define GETXXKEY_R_PATHNAME _PATH_GROUP
269 #include "pwd_grp_internal.c"
271 /**********************************************************************/
272 /* TODO: audit & stop using these in bbox, they pull in static buffers */
274 /* This one has many users */
275 struct passwd *getpwuid(uid_t uid)
278 struct passwd *resultbuf = RESULTBUF(getpw);
279 char *buffer = BUFFER(getpw);
280 struct passwd *result;
282 getpwuid_r(uid, resultbuf, buffer, sizeof(BUFFER(getpw)), &result);
286 /* This one has many users */
287 struct group *getgrgid(gid_t gid)
290 struct group *resultbuf = RESULTBUF(getgr);
291 char *buffer = BUFFER(getgr);
292 struct group *result;
294 getgrgid_r(gid, resultbuf, buffer, sizeof(BUFFER(getgr)), &result);
298 #if 0 //ENABLE_USE_BB_SHADOW
299 /* This function is non-standard and is currently not built. It seems
300 * to have been created as a reentrant version of the non-standard
301 * functions getspuid. Why getspuid was added, I do not know. */
302 int getspuid_r(uid_t uid, struct spwd *__restrict resultbuf,
303 char *__restrict buffer, size_t buflen,
304 struct spwd **__restrict result)
308 struct passwd password;
309 char pwd_buff[PWD_BUFFER_SIZE];
312 rv = getpwuid_r(uid, &password, pwd_buff, sizeof(pwd_buff), &pp);
314 rv = getspnam_r(password.pw_name, resultbuf, buffer, buflen, result);
320 /* This function is non-standard and is currently not built.
321 * Why it was added, I do not know. */
322 struct spwd *getspuid(uid_t uid)
325 struct spwd *resultbuf = RESULTBUF(getsp);
326 char *buffer = BUFFER(getsp);
329 getspuid_r(uid, resultbuf, buffer, sizeof(BUFFER(getsp)), &result);
334 /* This one has many users */
335 struct passwd *getpwnam(const char *name)
338 struct passwd *resultbuf = RESULTBUF(getpw);
339 char *buffer = BUFFER(getpw);
340 struct passwd *result;
342 getpwnam_r(name, resultbuf, buffer, sizeof(BUFFER(getpw)), &result);
346 /* This one has many users */
347 struct group *getgrnam(const char *name)
350 struct group *resultbuf = RESULTBUF(getgr);
351 char *buffer = BUFFER(getgr);
352 struct group *result;
354 getgrnam_r(name, resultbuf, buffer, sizeof(BUFFER(getgr)), &result);
358 #if 0 //ENABLE_USE_BB_SHADOW
359 struct spwd *getspnam(const char *name)
362 struct spwd *resultbuf = RESULTBUF(getsp);
363 char *buffer = BUFFER(getsp);
366 getspnam_r(name, resultbuf, buffer, sizeof(BUFFER(getsp)), &result);
371 /**********************************************************************/
373 /* FIXME: we don't have such CONFIG_xx - ?! */
375 #if defined CONFIG_USE_BB_THREADSAFE_SHADOW && defined PTHREAD_MUTEX_INITIALIZER
376 static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER;
377 # define LOCK pthread_mutex_lock(&mylock)
378 # define UNLOCK pthread_mutex_unlock(&mylock);
380 # define LOCK ((void) 0)
381 # define UNLOCK ((void) 0)
384 static FILE *pwf /*= NULL*/;
405 int getpwent_r(struct passwd *__restrict resultbuf,
406 char *__restrict buffer, size_t buflen,
407 struct passwd **__restrict result)
412 *result = NULL; /* In case of error... */
415 pwf = fopen_for_read(_PATH_PASSWD);
420 close_on_exec_on(fileno(pwf));
423 rv = bb__pgsreader(bb__parsepwent, resultbuf, buffer, buflen, pwf);
433 static FILE *grf /*= NULL*/;
453 int getgrent_r(struct group *__restrict resultbuf,
454 char *__restrict buffer, size_t buflen,
455 struct group **__restrict result)
460 *result = NULL; /* In case of error... */
463 grf = fopen_for_read(_PATH_GROUP);
468 close_on_exec_on(fileno(grf));
471 rv = bb__pgsreader(bb__parsegrent, resultbuf, buffer, buflen, grf);
481 #ifdef UNUSED_FOR_NOW
482 #if ENABLE_USE_BB_SHADOW
483 static FILE *spf /*= NULL*/;
503 int getspent_r(struct spwd *resultbuf, char *buffer,
504 size_t buflen, struct spwd **result)
509 *result = NULL; /* In case of error... */
512 spf = fopen_for_read(_PATH_SHADOW);
517 close_on_exec_on(fileno(spf));
520 rv = bb__pgsreader(bb__parsespent, resultbuf, buffer, buflen, spf);
530 #endif /* UNUSED_FOR_NOW */
532 #ifdef UNUSED_SINCE_WE_AVOID_STATIC_BUFS
533 struct passwd *getpwent(void)
535 static char line_buff[PWD_BUFFER_SIZE];
536 static struct passwd pwd;
537 struct passwd *result;
539 getpwent_r(&pwd, line_buff, sizeof(line_buff), &result);
543 struct group *getgrent(void)
545 static char line_buff[GRP_BUFFER_SIZE];
546 static struct group gr;
547 struct group *result;
549 getgrent_r(&gr, line_buff, sizeof(line_buff), &result);
553 #if ENABLE_USE_BB_SHADOW
554 struct spwd *getspent(void)
556 static char line_buff[PWD_BUFFER_SIZE];
557 static struct spwd spwd;
560 getspent_r(&spwd, line_buff, sizeof(line_buff), &result);
564 struct spwd *sgetspent(const char *string)
566 static char line_buff[PWD_BUFFER_SIZE];
567 static struct spwd spwd;
570 sgetspent_r(string, &spwd, line_buff, sizeof(line_buff), &result);
574 #endif /* UNUSED_SINCE_WE_AVOID_STATIC_BUFS */
576 static gid_t *getgrouplist_internal(int *ngroups_ptr, const char *user, gid_t gid)
582 char buff[PWD_BUFFER_SIZE];
584 /* We alloc space for 8 gids at a time. */
585 group_list = xmalloc(8 * sizeof(group_list[0]));
589 grfile = fopen_for_read(_PATH_GROUP);
591 while (!bb__pgsreader(bb__parsegrent, &group, buff, sizeof(buff), grfile)) {
593 assert(group.gr_mem); /* Must have at least a NULL terminator. */
594 if (group.gr_gid == gid)
596 for (m = group.gr_mem; *m; m++) {
597 if (strcmp(*m, user) != 0)
599 group_list = xrealloc_vector(group_list, /*8=2^3:*/ 3, ngroups);
600 group_list[ngroups++] = group.gr_gid;
606 *ngroups_ptr = ngroups;
610 int initgroups(const char *user, gid_t gid)
613 gid_t *group_list = getgrouplist_internal(&ngroups, user, gid);
615 ngroups = setgroups(ngroups, group_list);
620 int getgrouplist(const char *user, gid_t gid, gid_t *groups, int *ngroups)
622 int ngroups_old = *ngroups;
623 gid_t *group_list = getgrouplist_internal(ngroups, user, gid);
625 if (*ngroups <= ngroups_old) {
626 ngroups_old = *ngroups;
627 memcpy(groups, group_list, ngroups_old * sizeof(groups[0]));
635 #ifdef UNUSED_SINCE_WE_AVOID_STATIC_BUFS
636 int putpwent(const struct passwd *__restrict p, FILE *__restrict f)
641 /* glibc does this check */
648 /* No extra thread locking is needed above what fprintf does. */
649 if (fprintf(f, "%s:%s:%lu:%lu:%s:%s:%s\n",
650 p->pw_name, p->pw_passwd,
651 (unsigned long)(p->pw_uid),
652 (unsigned long)(p->pw_gid),
653 p->pw_gecos, p->pw_dir, p->pw_shell) >= 0
661 int putgrent(const struct group *__restrict p, FILE *__restrict f)
666 /* glibc does this check */
673 if (fprintf(f, "%s:%s:%lu:",
674 p->gr_name, p->gr_passwd,
675 (unsigned long)(p->gr_gid)) >= 0
677 static const char format[] ALIGN1 = ",%s";
689 if (fputc('\n', f) >= 0) {
694 if (fprintf(f, fmt, *m) < 0) {
706 #if ENABLE_USE_BB_SHADOW
707 #ifdef UNUSED_FOR_NOW
708 static const unsigned char put_sp_off[] ALIGN1 = {
709 offsetof(struct spwd, sp_lstchg), /* 2 - not a char ptr */
710 offsetof(struct spwd, sp_min), /* 3 - not a char ptr */
711 offsetof(struct spwd, sp_max), /* 4 - not a char ptr */
712 offsetof(struct spwd, sp_warn), /* 5 - not a char ptr */
713 offsetof(struct spwd, sp_inact), /* 6 - not a char ptr */
714 offsetof(struct spwd, sp_expire) /* 7 - not a char ptr */
717 int putspent(const struct spwd *p, FILE *stream)
724 /* Unlike putpwent and putgrent, glibc does not check the args. */
725 if (fprintf(stream, "%s:%s:", p->sp_namp,
726 (p->sp_pwdp ? p->sp_pwdp : "")) < 0
731 for (i = 0; i < sizeof(put_sp_off); i++) {
733 x = *(long *)((char *)p + put_sp_off[i]);
737 if (fprintf(stream, fmt, x) < 0) {
742 if ((p->sp_flag != ~0UL) && (fprintf(stream, "%lu", p->sp_flag) < 0)) {
746 if (fputc('\n', stream) > 0) {
754 #endif /* USE_BB_SHADOW */
756 /**********************************************************************/
757 /* Internal functions */
758 /**********************************************************************/
760 static const unsigned char pw_off[] ALIGN1 = {
761 offsetof(struct passwd, pw_name), /* 0 */
762 offsetof(struct passwd, pw_passwd), /* 1 */
763 offsetof(struct passwd, pw_uid), /* 2 - not a char ptr */
764 offsetof(struct passwd, pw_gid), /* 3 - not a char ptr */
765 offsetof(struct passwd, pw_gecos), /* 4 */
766 offsetof(struct passwd, pw_dir), /* 5 */
767 offsetof(struct passwd, pw_shell) /* 6 */
770 static int FAST_FUNC bb__parsepwent(void *data, char *line)
778 p = (char *) data + pw_off[i];
780 if (i < 2 || i > 3) {
781 *((char **) p) = line;
785 /* NOTE: glibc difference - glibc allows omission of
786 * ':' seperators after the gid field if all remaining
787 * entries are empty. We require all separators. */
788 line = strchr(line, ':');
793 unsigned long t = strtoul(line, &endptr, 10);
794 /* Make sure we had at least one digit, and that the
795 * failing char is the next field seperator ':'. See
796 * glibc difference note above. */
797 /* TODO: Also check for leading whitespace? */
798 if ((endptr == line) || (*endptr != ':')) {
802 if (i & 1) { /* i == 3 -- gid */
804 } else { /* i == 2 -- uid */
816 /**********************************************************************/
818 static const unsigned char gr_off[] ALIGN1 = {
819 offsetof(struct group, gr_name), /* 0 */
820 offsetof(struct group, gr_passwd), /* 1 */
821 offsetof(struct group, gr_gid) /* 2 - not a char ptr */
824 static int FAST_FUNC bb__parsegrent(void *data, char *line)
832 end_of_buf = ((struct group *) data)->gr_name; /* Evil hack! */
835 p = (char *) data + gr_off[i];
838 *((char **) p) = line;
839 line = strchr(line, ':');
846 *((gid_t *) p) = strtoul(line, &endptr, 10);
848 /* NOTE: glibc difference - glibc allows omission of the
849 * trailing colon when there is no member list. We treat
850 * this as an error. */
852 /* Make sure we had at least one digit, and that the
853 * failing char is the next field seperator ':'. See
854 * glibc difference note above. */
855 if ((endptr == line) || (*endptr != ':')) {
859 i = 1; /* Count terminating NULL ptr. */
862 if (p[1]) { /* We have a member list to process. */
863 /* Overwrite the last ':' with a ',' before counting.
864 * This allows us to (1) test for initial ','
865 * and (2) adds one ',' so that the number of commas
866 * equals the member count. */
869 /* NOTE: glibc difference - glibc allows and trims leading
870 * (but not trailing) space. We treat this as an error. */
871 /* NOTE: glibc difference - glibc allows consecutive and
872 * trailing commas, and ignores "empty string" users. We
873 * treat this as an error. */
876 *p = 0; /* nul-terminate each member string. */
877 if (!*++p || (*p == ',') || isspace(*p)) {
884 /* Now align (p+1), rounding up. */
885 /* Assumes sizeof(char **) is a power of 2. */
886 members = (char **)( (((intptr_t) p) + sizeof(char **))
887 & ~((intptr_t)(sizeof(char **) - 1)) );
889 if (((char *)(members + i)) > end_of_buf) { /* No space. */
893 ((struct group *) data)->gr_mem = members;
896 p = endptr; /* Pointing to char prior to first member. */
915 /**********************************************************************/
917 #if ENABLE_USE_BB_SHADOW
918 static const unsigned char sp_off[] ALIGN1 = {
919 offsetof(struct spwd, sp_namp), /* 0: char* */
920 offsetof(struct spwd, sp_pwdp), /* 1: char* */
921 offsetof(struct spwd, sp_lstchg), /* 2: long */
922 offsetof(struct spwd, sp_min), /* 3: long */
923 offsetof(struct spwd, sp_max), /* 4: long */
924 offsetof(struct spwd, sp_warn), /* 5: long */
925 offsetof(struct spwd, sp_inact), /* 6: long */
926 offsetof(struct spwd, sp_expire), /* 7: long */
927 offsetof(struct spwd, sp_flag) /* 8: unsigned long */
930 static int FAST_FUNC bb__parsespent(void *data, char *line)
938 p = (char *) data + sp_off[i];
940 *((char **) p) = line;
941 line = strchr(line, ':');
946 *((long *) p) = strtoul(line, &endptr, 10);
947 if (endptr == line) {
955 return 0; /* all ok */
969 /**********************************************************************/
971 /* Reads until EOF, or until it finds a line which fits in the buffer
972 * and for which the parser function succeeds.
974 * Returns 0 on success and ENOENT for end-of-file (glibc convention).
976 static int bb__pgsreader(
977 int FAST_FUNC (*parserfunc)(void *d, char *line),
979 char *__restrict line_buff,
986 if (buflen < PWD_BUFFER_SIZE) {
993 if (!fgets(line_buff, buflen, f)) {
1001 int line_len = strlen(line_buff) - 1;
1002 if (line_len >= 0 && line_buff[line_len] == '\n') {
1003 line_buff[line_len] = '\0';
1005 if (line_len + 2 == buflen) {
1006 /* A start (or continuation) of overlong line */
1009 } /* else: a last line in the file, and it has no '\n' */
1013 /* This "line" is a remainder of overlong line, ignore */
1018 /* NOTE: glibc difference - glibc strips leading whitespace from
1019 * records. We do not allow leading whitespace. */
1021 /* Skip empty lines, comment lines, and lines with leading
1023 if (line_buff[0] != '\0' && line_buff[0] != '#' && !isspace(line_buff[0])) {
1024 if (parserfunc == bb__parsegrent) {
1025 /* Do evil group hack:
1026 * The group entry parsing function needs to know where
1027 * the end of the buffer is so that it can construct the
1028 * group member ptr table. */
1029 ((struct group *) data)->gr_name = line_buff + buflen;
1031 if (parserfunc(data, line_buff) == 0) {