skip_whitespace() shouldn't claim its return value is const, it doesn't know
[oweals/busybox.git] / libpwdgrp / pwd_grp.c
index 9412faeb458e88da3d2e183b3c6ae41662e7528d..98ecf40a366cf52bbbb8aab5c0bda33ea81360d8 100644 (file)
@@ -1,18 +1,6 @@
 /*  Copyright (C) 2003     Manuel Novoa III
  *
- *  This library is free software; you can redistribute it and/or
- *  modify it under the terms of the GNU Library General Public
- *  License as published by the Free Software Foundation; either
- *  version 2 of the License, or (at your option) any later version.
- *
- *  This library is distributed in the hope that it will be useful,
- *  but WITHOUT ANY WARRANTY; without even the implied warranty of
- *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- *  Library General Public License for more details.
- *
- *  You should have received a copy of the GNU Library General Public
- *  License along with this library; if not, write to the Free
- *  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *  Licensed under GPL v2, or later.  See file LICENSE in this tarball.
  */
 
 /*  Nov 6, 2003  Initial version.
  *    lenient.  See the various glibc difference comments below.
  *
  *  TODO:
- *    Move to dynamic allocation of (currently staticly allocated)
+ *    Move to dynamic allocation of (currently statically allocated)
  *      buffers; especially for the group-related functions since
  *      large group member lists will cause error returns.
  *
  */
 
+#include "libbb.h"
 #include <features.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <errno.h>
 #include <assert.h>
 #include <ctype.h>
-#include "busybox.h"
-#include "pwd_.h"
-#include "grp_.h"
-#include "shadow_.h"
+
+//#include "pwd_.h"
+//#include "grp_.h"
+//#include "shadow_.h"
 
 #ifndef _PATH_SHADOW
 #define        _PATH_SHADOW    "/etc/shadow"
@@ -54,7 +43,7 @@
 #endif
 
 /**********************************************************************/
-/* Sizes for staticly allocated buffers. */
+/* Sizes for statically allocated buffers. */
 
 /* If you change these values, also change _SC_GETPW_R_SIZE_MAX and
  * _SC_GETGR_R_SIZE_MAX in libc/unistd/sysconf.c to match */
@@ -86,6 +75,7 @@ extern int __pgsreader(int (*__parserfunc)(void *d, char *line), void *data,
  *   Doing so is analogous to having fgetc() set errno on EOF.
  */
 /**********************************************************************/
+
 #ifdef L_fgetpwent_r
 
 int fgetpwent_r(FILE *__restrict stream, struct passwd *__restrict resultbuf,
@@ -144,7 +134,7 @@ int fgetspent_r(FILE *__restrict stream, struct spwd *__restrict resultbuf,
 #endif
 /**********************************************************************/
 /* For the various fget??ent funcs, return NULL on failure and a
- * pointer to the appropriate struct (staticly allocated) on success.
+ * pointer to the appropriate struct (statically allocated) on success.
  */
 /**********************************************************************/
 #ifdef L_fgetpwent
@@ -231,87 +221,54 @@ int sgetspent_r(const char *string, struct spwd *result_buf,
 
 #ifdef L_getpwnam_r
 #define GETXXKEY_R_FUNC                        getpwnam_r
-#define GETXXKEY_R_PARSER      __parsepwent
+#define GETXXKEY_R_PARSER              __parsepwent
 #define GETXXKEY_R_ENTTYPE             struct passwd
 #define GETXXKEY_R_TEST(ENT)   (!strcmp((ENT)->pw_name, key))
 #define DO_GETXXKEY_R_KEYTYPE  const char *__restrict
 #define DO_GETXXKEY_R_PATHNAME  _PATH_PASSWD
+#include "pwd_grp_internal.c"
 #endif
 
 #ifdef L_getgrnam_r
 #define GETXXKEY_R_FUNC                        getgrnam_r
-#define GETXXKEY_R_PARSER      __parsegrent
+#define GETXXKEY_R_PARSER              __parsegrent
 #define GETXXKEY_R_ENTTYPE             struct group
 #define GETXXKEY_R_TEST(ENT)   (!strcmp((ENT)->gr_name, key))
 #define DO_GETXXKEY_R_KEYTYPE  const char *__restrict
 #define DO_GETXXKEY_R_PATHNAME  _PATH_GROUP
+#include "pwd_grp_internal.c"
 #endif
 
 #ifdef L_getspnam_r
 #define GETXXKEY_R_FUNC                        getspnam_r
-#define GETXXKEY_R_PARSER      __parsespent
+#define GETXXKEY_R_PARSER              __parsespent
 #define GETXXKEY_R_ENTTYPE             struct spwd
 #define GETXXKEY_R_TEST(ENT)   (!strcmp((ENT)->sp_namp, key))
 #define DO_GETXXKEY_R_KEYTYPE  const char *__restrict
 #define DO_GETXXKEY_R_PATHNAME  _PATH_SHADOW
+#include "pwd_grp_internal.c"
 #endif
 
 #ifdef L_getpwuid_r
 #define GETXXKEY_R_FUNC                        getpwuid_r
-#define GETXXKEY_R_PARSER      __parsepwent
+#define GETXXKEY_R_PARSER              __parsepwent
 #define GETXXKEY_R_ENTTYPE             struct passwd
 #define GETXXKEY_R_TEST(ENT)   ((ENT)->pw_uid == key)
 #define DO_GETXXKEY_R_KEYTYPE  uid_t
 #define DO_GETXXKEY_R_PATHNAME  _PATH_PASSWD
+#include "pwd_grp_internal.c"
 #endif
 
 #ifdef L_getgrgid_r
 #define GETXXKEY_R_FUNC                        getgrgid_r
-#define GETXXKEY_R_PARSER      __parsegrent
+#define GETXXKEY_R_PARSER              __parsegrent
 #define GETXXKEY_R_ENTTYPE             struct group
 #define GETXXKEY_R_TEST(ENT)   ((ENT)->gr_gid == key)
 #define DO_GETXXKEY_R_KEYTYPE  gid_t
 #define DO_GETXXKEY_R_PATHNAME  _PATH_GROUP
+#include "pwd_grp_internal.c"
 #endif
 
-/**********************************************************************/
-#ifdef GETXXKEY_R_FUNC
-
-int GETXXKEY_R_FUNC(DO_GETXXKEY_R_KEYTYPE key,
-                                       GETXXKEY_R_ENTTYPE *__restrict resultbuf,
-                                       char *__restrict buffer, size_t buflen,
-                                       GETXXKEY_R_ENTTYPE **__restrict result)
-{
-       FILE *stream;
-       int rv;
-
-       *result = NULL;
-
-       if (!(stream = fopen(DO_GETXXKEY_R_PATHNAME, "r"))) {
-               rv = errno;
-       } else {
-               do {
-                       if (!(rv = __pgsreader(GETXXKEY_R_PARSER, resultbuf,
-                                                                  buffer, buflen, stream))
-                               ) {
-                               if (GETXXKEY_R_TEST(resultbuf)) { /* Found key? */
-                                       *result = resultbuf;
-                                       break;
-                               }
-                       } else {
-                               if (rv == ENOENT) {     /* end-of-file encountered. */
-                                       rv = 0;
-                               }
-                               break;
-                       }
-               } while (1);
-               fclose(stream);
-       }
-
-       return rv;
-}
-
-#endif
 /**********************************************************************/
 #ifdef L_getpwuid
 
@@ -452,31 +409,47 @@ int getpw(uid_t uid, char *buf)
 
 #endif
 /**********************************************************************/
-#ifdef L_getpwent_r
 
+#if defined(L_getpwent_r) || defined(L_getgrent_r) || defined(L_getspent_r)
+#if defined CONFIG_USE_BB_THREADSAFE_SHADOW && defined PTHREAD_MUTEX_INITIALIZER
+static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER;
+# define LOCK          pthread_mutex_lock(&mylock)
+# define UNLOCK                pthread_mutex_unlock(&mylock);
+#else
+# define LOCK          ((void) 0)
+# define UNLOCK                ((void) 0)
+#endif
+#endif
+
+#ifdef L_getpwent_r
 static FILE *pwf /*= NULL*/;
 void setpwent(void)
 {
+       LOCK;
        if (pwf) {
                rewind(pwf);
        }
+       UNLOCK;
 }
 
 void endpwent(void)
 {
+       LOCK;
        if (pwf) {
                fclose(pwf);
                pwf = NULL;
        }
+       UNLOCK;
 }
 
 
-int getpwent_r(struct passwd *__restrict resultbuf, 
+int getpwent_r(struct passwd *__restrict resultbuf,
                           char *__restrict buffer, size_t buflen,
                           struct passwd **__restrict result)
 {
        int rv;
 
+       LOCK;
        *result = NULL;                         /* In case of error... */
 
        if (!pwf) {
@@ -492,6 +465,7 @@ int getpwent_r(struct passwd *__restrict resultbuf,
        }
 
  ERR:
+       UNLOCK;
        return rv;
 }
 
@@ -502,17 +476,21 @@ int getpwent_r(struct passwd *__restrict resultbuf,
 static FILE *grf /*= NULL*/;
 void setgrent(void)
 {
+       LOCK;
        if (grf) {
                rewind(grf);
        }
+       UNLOCK;
 }
 
 void endgrent(void)
 {
+       LOCK;
        if (grf) {
                fclose(grf);
                grf = NULL;
        }
+       UNLOCK;
 }
 
 int getgrent_r(struct group *__restrict resultbuf,
@@ -521,6 +499,7 @@ int getgrent_r(struct group *__restrict resultbuf,
 {
        int rv;
 
+       LOCK;
        *result = NULL;                         /* In case of error... */
 
        if (!grf) {
@@ -536,6 +515,7 @@ int getgrent_r(struct group *__restrict resultbuf,
        }
 
  ERR:
+       UNLOCK;
        return rv;
 }
 
@@ -546,24 +526,29 @@ int getgrent_r(struct group *__restrict resultbuf,
 static FILE *spf /*= NULL*/;
 void setspent(void)
 {
+       LOCK;
        if (spf) {
                rewind(spf);
        }
+       UNLOCK;
 }
 
 void endspent(void)
 {
+       LOCK;
        if (spf) {
                fclose(spf);
                spf = NULL;
        }
+       UNLOCK;
 }
 
-int getspent_r(struct spwd *resultbuf, char *buffer, 
+int getspent_r(struct spwd *resultbuf, char *buffer,
                           size_t buflen, struct spwd **result)
 {
        int rv;
 
+       LOCK;
        *result = NULL;                         /* In case of error... */
 
        if (!spf) {
@@ -579,6 +564,7 @@ int getspent_r(struct spwd *resultbuf, char *buffer,
        }
 
  ERR:
+       UNLOCK;
        return rv;
 }
 
@@ -644,7 +630,7 @@ struct spwd *sgetspent(const char *string)
 
 int initgroups(const char *user, gid_t gid)
 {
-       FILE *grf;
+       FILE *grfile;
        gid_t *group_list;
        int num_groups, rv;
        char **m;
@@ -655,13 +641,13 @@ int initgroups(const char *user, gid_t gid)
 
        /* We alloc space for 8 gids at a time. */
        if (((group_list = (gid_t *) malloc(8*sizeof(gid_t *))) != NULL)
-               && ((grf = fopen(_PATH_GROUP, "r")) != NULL)
+               && ((grfile = fopen(_PATH_GROUP, "r")) != NULL)
                ) {
 
                *group_list = gid;
                num_groups = 1;
 
-               while (!__pgsreader(__parsegrent, &group, buff, sizeof(buff), grf)) {
+               while (!__pgsreader(__parsegrent, &group, buff, sizeof(buff), grfile)) {
                        assert(group.gr_mem); /* Must have at least a NULL terminator. */
                        if (group.gr_gid != gid) {
                                for (m=group.gr_mem ; *m ; m++) {
@@ -685,7 +671,7 @@ int initgroups(const char *user, gid_t gid)
 
                rv = setgroups(num_groups, group_list);
        DO_CLOSE:
-               fclose(grf);
+               fclose(grfile);
        }
 
        /* group_list will be NULL if initial malloc failed, which may trigger
@@ -745,7 +731,7 @@ int putgrent(const struct group *__restrict p, FILE *__restrict f)
 
                        do {
                                if (!*m) {
-                                       if (fputc_unlocked('\n', f) >= 0) {
+                                       if (fputc('\n', f) >= 0) {
                                                rv = 0;
                                        }
                                        break;
@@ -768,13 +754,13 @@ int putgrent(const struct group *__restrict p, FILE *__restrict f)
 /**********************************************************************/
 #ifdef L_putspent
 
-static const unsigned char sp_off[] = {
+static const unsigned char _sp_off[] = {
        offsetof(struct spwd, sp_lstchg),       /* 2 - not a char ptr */
-       offsetof(struct spwd, sp_min),          /* 3 - not a char ptr */
+       offsetof(struct spwd, sp_min),          /* 3 - not a char ptr */
        offsetof(struct spwd, sp_max),          /* 4 - not a char ptr */
-       offsetof(struct spwd, sp_warn),         /* 5 - not a char ptr */
-       offsetof(struct spwd, sp_inact),        /* 6 - not a char ptr */
-       offsetof(struct spwd, sp_expire),       /* 7 - not a char ptr */
+       offsetof(struct spwd, sp_warn),         /* 5 - not a char ptr */
+       offsetof(struct spwd, sp_inact),        /* 6 - not a char ptr */
+       offsetof(struct spwd, sp_expire),       /* 7 - not a char ptr */
 };
 
 int putspent(const struct spwd *p, FILE *stream)
@@ -792,9 +778,9 @@ int putspent(const struct spwd *p, FILE *stream)
                goto DO_UNLOCK;
        }
 
-       for (i=0 ; i < sizeof(sp_off) ; i++) {
+       for (i=0 ; i < sizeof(_sp_off) ; i++) {
                f = ld_format;
-               if ((x = *(const long int *)(((const char *) p) + sp_off[i])) == -1) {
+               if ((x = *(const long int *)(((const char *) p) + _sp_off[i])) == -1) {
                        f += 3;
                }
                if (fprintf(stream, f, x) < 0) {
@@ -806,7 +792,7 @@ int putspent(const struct spwd *p, FILE *stream)
                goto DO_UNLOCK;
        }
 
-       if (fputc_unlocked('\n', stream) > 0) {
+       if (fputc('\n', stream) > 0) {
                rv = 0;
        }
 
@@ -816,18 +802,18 @@ DO_UNLOCK:
 
 #endif
 /**********************************************************************/
-/* Internal uClibc functions.                                         */
+/* Internal uClibc functions.                                   */
 /**********************************************************************/
 #ifdef L___parsepwent
 
 static const unsigned char pw_off[] = {
-       offsetof(struct passwd, pw_name),       /* 0 */
+       offsetof(struct passwd, pw_name),       /* 0 */
        offsetof(struct passwd, pw_passwd),     /* 1 */
        offsetof(struct passwd, pw_uid),        /* 2 - not a char ptr */
-       offsetof(struct passwd, pw_gid),        /* 3 - not a char ptr */
+       offsetof(struct passwd, pw_gid),        /* 3 - not a char ptr */
        offsetof(struct passwd, pw_gecos),      /* 4 */
-       offsetof(struct passwd, pw_dir),        /* 5 */
-       offsetof(struct passwd, pw_shell)       /* 6 */
+       offsetof(struct passwd, pw_dir),        /* 5 */
+       offsetof(struct passwd, pw_shell)       /* 6 */
 };
 
 int __parsepwent(void *data, char *line)
@@ -840,7 +826,7 @@ int __parsepwent(void *data, char *line)
        do {
                p = ((char *) ((struct passwd *) data)) + pw_off[i];
 
-               if ((i & 6) ^ 2) {      /* i!=2 and i!=3 */
+               if ((i & 6) ^ 2) {      /* i!=2 and i!=3 */
                        *((char **) p) = line;
                        if (i==6) {
                                return 0;
@@ -880,7 +866,7 @@ int __parsepwent(void *data, char *line)
 #ifdef L___parsegrent
 
 static const unsigned char gr_off[] = {
-       offsetof(struct group, gr_name),        /* 0 */
+       offsetof(struct group, gr_name),        /* 0 */
        offsetof(struct group, gr_passwd),      /* 1 */
        offsetof(struct group, gr_gid)          /* 2 - not a char ptr */
 };
@@ -962,7 +948,7 @@ int __parsegrent(void *data, char *line)
                                        if (!--i) break;
                                        while (*++p) {}
                                } while (1);
-                       }                               
+                       }
                        *members = NULL;
 
                        return 0;
@@ -981,12 +967,12 @@ static const unsigned char sp_off[] = {
        offsetof(struct spwd, sp_namp),         /* 0 */
        offsetof(struct spwd, sp_pwdp),         /* 1 */
        offsetof(struct spwd, sp_lstchg),       /* 2 - not a char ptr */
-       offsetof(struct spwd, sp_min),          /* 3 - not a char ptr */
+       offsetof(struct spwd, sp_min),          /* 3 - not a char ptr */
        offsetof(struct spwd, sp_max),          /* 4 - not a char ptr */
-       offsetof(struct spwd, sp_warn),         /* 5 - not a char ptr */
-       offsetof(struct spwd, sp_inact),        /* 6 - not a char ptr */
-       offsetof(struct spwd, sp_expire),       /* 7 - not a char ptr */
-       offsetof(struct spwd, sp_flag)          /* 8 - not a char ptr */
+       offsetof(struct spwd, sp_warn),         /* 5 - not a char ptr */
+       offsetof(struct spwd, sp_inact),        /* 6 - not a char ptr */
+       offsetof(struct spwd, sp_expire),       /* 7 - not a char ptr */
+       offsetof(struct spwd, sp_flag)          /* 8 - not a char ptr */
 };
 
 int __parsespent(void *data, char * line)
@@ -1067,8 +1053,8 @@ int __pgsreader(int (*__parserfunc)(void *d, char *line), void *data,
        } else {
                skip = 0;
                do {
-                       if (!fgets_unlocked(line_buff, buflen, f)) {
-                               if (feof_unlocked(f)) {
+                       if (!fgets(line_buff, buflen, f)) {
+                               if (feof(f)) {
                                        rv = ENOENT;
                                }
                                break;