watchdog: stop watchdog first on startup
[oweals/busybox.git] / libpwdgrp / pwd_grp.c
index 2392e7d2259fcf806e8cc026cccdeb9f0e89a0a2..c9bbc8bdadce5ce754407d0b3e4128f40ec62fde 100644 (file)
@@ -9,21 +9,20 @@
  * Rewrite of some parts. Main differences are:
  *
  * 1) the buffer for getpwuid, getgrgid, getpwnam, getgrnam is dynamically
- *    allocated and reused by later calls.
+ *    allocated.
  *    If ENABLE_FEATURE_CLEAN_UP is set the buffers are freed at program
  *    exit using the atexit function to make valgrind happy.
  * 2) the passwd/group files:
  *      a) must contain the expected number of fields (as per count of field
- *         delimeters ":") or we will complain with a error message.
- *      b) leading or trailing whitespace in fields is stripped.
- *      c) some fields are not allowed to be empty (e.g. username, uid/gid,
- *         homedir, shell) and in this case NULL is returned and errno is
- *         set to EINVAL. This behaviour could be easily changed by
- *         modifying PW_DEF, GR_DEF, SP_DEF strings (uppercase
- *         makes a field mandatory).
+ *         delimiters ":") or we will complain with a error message.
+ *      b) leading and trailing whitespace in fields is stripped.
+ *      c) some fields are not allowed to be empty (e.g. username, uid/gid),
+ *         and in this case NULL is returned and errno is set to EINVAL.
+ *         This behaviour could be easily changed by modifying PW_DEF, GR_DEF,
+ *         SP_DEF strings (uppercase makes a field mandatory).
  *      d) the string representing uid/gid must be convertible by strtoXX
  *         functions, or errno is set to EINVAL.
- *      e) leading or trailing whitespace in group member names are stripped.
+ *      e) leading and trailing whitespace in group member names is stripped.
  * 3) the internal function for getgrouplist uses dynamically allocated buffer.
  * 4) at the moment only the functions really used by busybox code are
  *    implemented, if you need a particular missing function it should be
 
 struct const_passdb {
        const char *filename;
-       const char def[7 + 2*ENABLE_USE_BB_SHADOW];
-       const uint8_t off[7 + 2*ENABLE_USE_BB_SHADOW];
+       char def[7 + 2*ENABLE_USE_BB_SHADOW];
+       uint8_t off[7 + 2*ENABLE_USE_BB_SHADOW];
        uint8_t numfields;
        uint8_t size_of;
 };
 struct passdb {
        const char *filename;
-       const char def[7 + 2*ENABLE_USE_BB_SHADOW];
-       const uint8_t off[7 + 2*ENABLE_USE_BB_SHADOW];
+       char def[7 + 2*ENABLE_USE_BB_SHADOW];
+       uint8_t off[7 + 2*ENABLE_USE_BB_SHADOW];
        uint8_t numfields;
        uint8_t size_of;
        FILE *fp;
@@ -58,7 +57,7 @@ struct passdb {
  * I = uid,gid, l = long maybe empty, m = members,
  * r = reserved
  */
-#define PW_DEF "SsIIsSS"
+#define PW_DEF "SsIIsss"
 #define GR_DEF "SsIm"
 #define SP_DEF "Ssllllllr"
 
@@ -70,8 +69,8 @@ static const struct const_passdb const_pw_db = {
                offsetof(struct passwd, pw_uid),        /* 2 I */
                offsetof(struct passwd, pw_gid),        /* 3 I */
                offsetof(struct passwd, pw_gecos),      /* 4 s */
-               offsetof(struct passwd, pw_dir),        /* 5 S */
-               offsetof(struct passwd, pw_shell)       /* 6 S */
+               offsetof(struct passwd, pw_dir),        /* 5 s */
+               offsetof(struct passwd, pw_shell)       /* 6 s */
        },
        sizeof(PW_DEF)-1, sizeof(struct passwd)
 };
@@ -105,7 +104,7 @@ static const struct const_passdb const_sp_db = {
 
 /* We avoid having big global data. */
 struct statics {
-       /* It's ok to use same buffer (db[0].malloced) for getpwuid and getpwnam.
+       /* We use same buffer (db[0].malloced) for getpwuid and getpwnam.
         * Manpage says:
         * "The return value may point to a static area, and may be overwritten
         * by subsequent calls to getpwent(), getpwnam(), or getpwuid()."
@@ -122,7 +121,7 @@ static struct statics *ptr_to_statics;
 #if ENABLE_FEATURE_CLEAN_UP
 static void free_static(void)
 {
-       free(S.db[0].malloced);
+       free(S.db[0].malloced);
        free(S.db[1].malloced);
 # if ENABLE_USE_BB_SHADOW
        free(S.db[2].malloced);
@@ -150,7 +149,7 @@ static struct statics *get_S(void)
 /* Internal functions */
 
 /* Divide the passwd/group/shadow record in fields
- * by substituting the given delimeter
+ * by substituting the given delimiter
  * e.g. ':' or ',' with '\0'.
  * Returns the number of fields found.
  * Strips leading and trailing whitespace in fields.
@@ -186,25 +185,22 @@ static int tokenize(char *buffer, int ch)
 /* Returns !NULL on success and matching line broken up in fields by '\0' in buf.
  * We require the expected number of fields to be found.
  */
-static char *parse_common(FILE *fp, const char *filename,
-               int n_fields,
+static char *parse_common(FILE *fp, struct passdb *db,
                const char *key, int field_pos)
 {
-       int count = 0;
        char *buf;
 
        while ((buf = xmalloc_fgetline(fp)) != NULL) {
-               count++;
                /* Skip empty lines, comment lines */
                if (buf[0] == '\0' || buf[0] == '#')
                        goto free_and_next;
-               if (tokenize(buf, ':') != n_fields) {
+               if (tokenize(buf, ':') != db->numfields) {
                        /* number of fields is wrong */
-                       bb_error_msg("bad record at %s:%u", filename, count);
+                       bb_error_msg("%s: bad record", db->filename);
                        goto free_and_next;
                }
 
-               if (!key) {
+               if (field_pos == -1) {
                        /* no key specified: sequential read, return a record */
                        break;
                }
@@ -223,7 +219,7 @@ static char *parse_common(FILE *fp, const char *filename,
  * for 3 pointers: alignment padding, group name, NULL.
  * +1 for every additional group.
  */
-       if (buf && n_fields == sizeof(GR_DEF)-1) { /* if we read group file... */
+       if (buf && db->numfields == sizeof(GR_DEF)-1) { /* if we read group file... */
                int cnt = 3;
                char *p = buf;
                while (p < S.tokenize_end)
@@ -237,15 +233,14 @@ static char *parse_common(FILE *fp, const char *filename,
        return buf;
 }
 
-static char *parse_file(const char *filename,
-               int n_fields,
+static char *parse_file(struct passdb *db,
                const char *key, int field_pos)
 {
        char *buf = NULL;
-       FILE *fp = fopen_for_read(filename);
+       FILE *fp = fopen_for_read(db->filename);
 
        if (fp) {
-               buf = parse_common(fp, filename, n_fields, key, field_pos);
+               buf = parse_common(fp, db, key, field_pos);
                fclose(fp);
        }
        return buf;
@@ -294,10 +289,12 @@ static void *convert_to_struct(struct passdb *db,
                                ( ((intptr_t)S.tokenize_end + sizeof(members[0]))
                                & -(intptr_t)sizeof(members[0])
                                );
-
                        ((struct group *)result)->gr_mem = members;
                        while (--i >= 0) {
-                               *members++ = buffer;
+                               if (buffer[0]) {
+                                       *members++ = buffer;
+                                       // bb_error_msg("member[]='%s'", buffer);
+                               }
                                buffer += strlen(buffer) + 1;
                        }
                        *members = NULL;
@@ -315,29 +312,19 @@ static void *convert_to_struct(struct passdb *db,
        return result;
 }
 
-/****** getXXnam/id_r */
-
-static int FAST_FUNC getXXnam_r(const char *name, uintptr_t db_and_field_pos,
+static int massage_data_for_r_func(struct passdb *db,
                char *buffer, size_t buflen,
-               void *result)
+               void **result,
+               char *buf)
 {
-       void *struct_buf = *(void**)result;
-       char *buf;
-       struct passdb *db;
-       get_S();
-       db = &S.db[db_and_field_pos >> 2];
-
-       *(void**)result = NULL;
-       buf = parse_file(db->filename, db->numfields, name, 0 /*db_and_field_pos & 3*/);
-       /* "db_and_field_pos & 3" is commented out since so far we don't implement
-        * getXXXid_r() functions which would use that to pass 2 here */
+       void *result_buf = *result;
+       *result = NULL;
        if (buf) {
-               size_t size = S.tokenize_end - buf;
-               if (size > buflen) {
+               if (S.string_size > buflen) {
                        errno = ERANGE;
                } else {
-                       memcpy(buffer, buf, size);
-                       *(void**)result = convert_to_struct(db, buffer, struct_buf);
+                       memcpy(buffer, buf, S.string_size);
+                       *result = convert_to_struct(db, buffer, result_buf);
                }
                free(buf);
        }
@@ -348,6 +335,38 @@ static int FAST_FUNC getXXnam_r(const char *name, uintptr_t db_and_field_pos,
        return errno;
 }
 
+static void* massage_data_for_non_r_func(struct passdb *db, char *buf)
+{
+       if (!buf)
+               return NULL;
+
+       free(db->malloced);
+       /* We enlarge buf and move string data up, freeing space
+        * for struct passwd/group/spwd at the beginning. This way,
+        * entire result of getXXnam is in a single malloced block.
+        * This enables easy creation of xmalloc_getpwnam() API.
+        */
+       db->malloced = buf = xrealloc(buf, db->size_of + S.string_size);
+       memmove(buf + db->size_of, buf, S.string_size);
+       return convert_to_struct(db, buf + db->size_of, buf);
+}
+
+/****** getXXnam/id_r */
+
+static int FAST_FUNC getXXnam_r(const char *name, uintptr_t db_and_field_pos,
+               char *buffer, size_t buflen,
+               void *result)
+{
+       char *buf;
+       struct passdb *db = &get_S()->db[db_and_field_pos >> 2];
+
+       buf = parse_file(db, name, 0 /*db_and_field_pos & 3*/);
+       /* "db_and_field_pos & 3" is commented out since so far we don't implement
+        * getXXXid_r() functions which would use that to pass 2 here */
+
+       return massage_data_for_r_func(db, buffer, buflen, result, buf);
+}
+
 int FAST_FUNC getpwnam_r(const char *name, struct passwd *struct_buf,
                char *buffer, size_t buflen,
                struct passwd **result)
@@ -368,18 +387,14 @@ int FAST_FUNC getspnam_r(const char *name, struct spwd *struct_buf, char *buffer
 }
 #endif
 
+#ifdef UNUSED
 /****** getXXent_r */
 
-static int FAST_FUNC getXXent_r(void *struct_buf, char *buffer, size_t buflen,
-               void *result,
-               unsigned db_idx)
+static int FAST_FUNC getXXent_r(uintptr_t db_idx, char *buffer, size_t buflen,
+               void *result)
 {
        char *buf;
-       struct passdb *db;
-       get_S();
-       db = &S.db[db_idx];
-
-       *(void**)result = NULL;
+       struct passdb *db = &get_S()->db[db_idx];
 
        if (!db->fp) {
                db->fp = fopen_for_read(db->filename);
@@ -389,40 +404,26 @@ static int FAST_FUNC getXXent_r(void *struct_buf, char *buffer, size_t buflen,
                close_on_exec_on(fileno(db->fp));
        }
 
-       buf = parse_common(db->fp, db->filename, db->numfields, /*no search key:*/ NULL, 0);
-       if (buf) {
-               size_t size = S.tokenize_end - buf;
-               if (size > buflen) {
-                       errno = ERANGE;
-               } else {
-                       memcpy(buffer, buf, size);
-                       *(void**)result = convert_to_struct(db, buffer, struct_buf);
-               }
-               free(buf);
-       }
-       /* "The reentrant functions return zero on success.
-        * In case of error, an error number is returned."
-        * NB: not finding the record is also a "success" here:
-        */
-       return errno;
+       buf = parse_common(db->fp, db, /*no search key:*/ NULL, -1);
+       if (!buf && !errno)
+               errno = ENOENT;
+       return massage_data_for_r_func(db, buffer, buflen, result, buf);
 }
 
-int FAST_FUNC getpwent_r(struct passwd *struct_buf, char *buffer, size_t buflen, struct passwd **result)
+int FAST_FUNC getpwent_r(struct passwd *struct_buf, char *buffer, size_t buflen,
+               struct passwd **result)
 {
-       return getXXent_r(struct_buf, buffer, buflen, result, 0);
+       *result = struct_buf;
+       return getXXent_r(0, buffer, buflen, result);
 }
+#endif
 
-/****** getXXnam/id */
+/****** getXXent */
 
-static void* FAST_FUNC getXXnam(const char *name, unsigned db_and_field_pos)
+static void* FAST_FUNC getXXent(uintptr_t db_idx)
 {
        char *buf;
-       void *result;
-       struct passdb *db;
-       get_S();
-       db = &S.db[db_and_field_pos >> 2];
-
-       result = NULL;
+       struct passdb *db = &get_S()->db[db_idx];
 
        if (!db->fp) {
                db->fp = fopen_for_read(db->filename);
@@ -432,19 +433,24 @@ static void* FAST_FUNC getXXnam(const char *name, unsigned db_and_field_pos)
                close_on_exec_on(fileno(db->fp));
        }
 
-       buf = parse_common(db->fp, db->filename, db->numfields, name, db_and_field_pos & 3);
-       if (buf) {
-               free(db->malloced);
-               /* We enlarge buf and move string data up, freeing space
-                * for struct passwd/group/spwd at the beginning. This way,
-                * entire result of getXXnam is in a single malloced block.
-                * This enables easy creation of xmalloc_getpwnam() API.
-                */
-               db->malloced = buf = xrealloc(buf, db->size_of + S.string_size);
-               memmove(buf + db->size_of, buf, S.string_size);
-               result = convert_to_struct(db, buf + db->size_of, buf);
-       }
-       return result;
+       buf = parse_common(db->fp, db, /*no search key:*/ NULL, -1);
+       return massage_data_for_non_r_func(db, buf);
+}
+
+struct passwd* FAST_FUNC getpwent(void)
+{
+       return getXXent(0);
+}
+
+/****** getXXnam/id */
+
+static void* FAST_FUNC getXXnam(const char *name, unsigned db_and_field_pos)
+{
+       char *buf;
+       struct passdb *db = &get_S()->db[db_and_field_pos >> 2];
+
+       buf = parse_file(db, name, db_and_field_pos & 3);
+       return massage_data_for_non_r_func(db, buf);
 }
 
 struct passwd* FAST_FUNC getpwnam(const char *name)
@@ -496,8 +502,6 @@ static gid_t* FAST_FUNC getgrouplist_internal(int *ngroups_ptr,
        gid_t *group_list;
        int ngroups;
 
-       get_S();
-
        /* We alloc space for 8 gids at a time. */
        group_list = xzalloc(8 * sizeof(group_list[0]));
        group_list[0] = gid;
@@ -505,11 +509,12 @@ static gid_t* FAST_FUNC getgrouplist_internal(int *ngroups_ptr,
 
        fp = fopen_for_read(_PATH_GROUP);
        if (fp) {
+               struct passdb *db = &get_S()->db[1];
                char *buf;
-               while ((buf = parse_common(fp, _PATH_GROUP, sizeof(GR_DEF)-1, NULL, 0)) != NULL) {
+               while ((buf = parse_common(fp, db, NULL, -1)) != NULL) {
                        char **m;
                        struct group group;
-                       if (!convert_to_struct(&S.db[1], buf, &group))
+                       if (!convert_to_struct(db, buf, &group))
                                goto next;
                        if (group.gr_gid == gid)
                                goto next;
@@ -518,7 +523,7 @@ static gid_t* FAST_FUNC getgrouplist_internal(int *ngroups_ptr,
                                        continue;
                                group_list = xrealloc_vector(group_list, /*8=2^3:*/ 3, ngroups);
                                group_list[ngroups++] = group.gr_gid;
-                               break;
+                               goto next;
                        }
  next:
                        free(buf);