]> git.cameronkatri.com Git - pw-darwin.git/blobdiff - pw/pw_conf.c
Remove things that crept in after badly checked revert
[pw-darwin.git] / pw / pw_conf.c
index 24c06508aba4537bd1be1b517d762c24a821687a..b723c31a39dd72bbbd7c0a3967e774db4901fac9 100644 (file)
@@ -31,6 +31,7 @@ static const char rcsid[] =
 
 #include <sys/types.h>
 #include <sys/sbuf.h>
+#include <inttypes.h>
 #include <string.h>
 #include <ctype.h>
 #include <fcntl.h>
@@ -104,8 +105,7 @@ static struct userconf config =
        1000, 32000,            /* Allowed range of uids */
        1000, 32000,            /* Allowed range of gids */
        0,                      /* Days until account expires */
-       0,                      /* Days until password expires */
-       0                       /* size of default_group array */
+       0                       /* Days until password expires */
 };
 
 static char const *comments[_UC_FIELDS] =
@@ -228,16 +228,16 @@ read_userconfig(char const * file)
 {
        FILE    *fp;
        char    *buf, *p;
+       const char *errstr;
        size_t  linecap;
        ssize_t linelen;
 
        buf = NULL;
        linecap = 0;
 
-       config.numgroups = 200;
-       config.groups = calloc(config.numgroups, sizeof(char *));
+       config.groups = sl_init();
        if (config.groups == NULL)
-               err(1, "calloc()");
+               err(1, "sl_init()");
        if (file == NULL)
                file = _PATH_PW_CONF;
 
@@ -316,41 +316,66 @@ read_userconfig(char const * file)
                                        ? NULL : newstr(q);
                                break;
                        case _UC_EXTRAGROUPS:
-                               for (i = 0; q != NULL; q = strtok(NULL, toks)) {
-                                       if (extendarray(&config.groups, &config.numgroups, i + 2) != -1)
-                                               config.groups[i++] = newstr(q);
-                               }
-                               if (i > 0)
-                                       while (i < config.numgroups)
-                                               config.groups[i++] = NULL;
+                               for (i = 0; q != NULL; q = strtok(NULL, toks))
+                                       sl_add(config.groups, newstr(q));
                                break;
                        case _UC_DEFAULTCLASS:
                                config.default_class = (q == NULL || !boolean_val(q, 1))
                                        ? NULL : newstr(q);
                                break;
                        case _UC_MINUID:
-                               if ((q = unquote(q)) != NULL && isdigit(*q))
-                                       config.min_uid = (uid_t) atol(q);
+                               if ((q = unquote(q)) != NULL) {
+                                       config.min_uid = strtounum(q, 0,
+                                           UID_MAX, &errstr);
+                                       if (errstr)
+                                               warnx("Invalid min_uid: '%s';"
+                                                   " ignoring", q);
+                               }
                                break;
                        case _UC_MAXUID:
-                               if ((q = unquote(q)) != NULL && isdigit(*q))
-                                       config.max_uid = (uid_t) atol(q);
+                               if ((q = unquote(q)) != NULL) {
+                                       config.max_uid = strtounum(q, 0,
+                                           UID_MAX, &errstr);
+                                       if (errstr)
+                                               warnx("Invalid max_uid: '%s';"
+                                                   " ignoring", q);
+                               }
                                break;
                        case _UC_MINGID:
-                               if ((q = unquote(q)) != NULL && isdigit(*q))
-                                       config.min_gid = (gid_t) atol(q);
+                               if ((q = unquote(q)) != NULL) {
+                                       config.min_gid = strtounum(q, 0,
+                                           GID_MAX, &errstr);
+                                       if (errstr)
+                                               warnx("Invalid min_gid: '%s';"
+                                                   " ignoring", q);
+                               }
                                break;
                        case _UC_MAXGID:
-                               if ((q = unquote(q)) != NULL && isdigit(*q))
-                                       config.max_gid = (gid_t) atol(q);
+                               if ((q = unquote(q)) != NULL) {
+                                       config.max_gid = strtounum(q, 0,
+                                           GID_MAX, &errstr);
+                                       if (errstr)
+                                               warnx("Invalid max_gid: '%s';"
+                                                   " ignoring", q);
+                               }
                                break;
                        case _UC_EXPIRE:
-                               if ((q = unquote(q)) != NULL && isdigit(*q))
-                                       config.expire_days = atoi(q);
+                               if ((q = unquote(q)) != NULL) {
+                                       config.expire_days = strtonum(q, 0,
+                                           INT_MAX, &errstr);
+                                       if (errstr)
+                                               warnx("Invalid expire days:"
+                                                   " '%s'; ignoring", q);
+                               }
                                break;
                        case _UC_PASSWORD:
-                               if ((q = unquote(q)) != NULL && isdigit(*q))
-                                       config.password_days = atoi(q);
+                               if ((q = unquote(q)) != NULL) {
+                                       config.password_days = strtonum(q, 0,
+                                           INT_MAX, &errstr);
+                                       if (errstr)
+                                               warnx("Invalid password days:"
+                                                   " '%s'; ignoring", q);
+                               }
                                break;
                        case _UC_FIELDS:
                        case _UC_NONE:
@@ -442,10 +467,10 @@ write_userconfig(char const * file)
                            config.default_group : "");
                        break;
                case _UC_EXTRAGROUPS:
-                       for (j = 0; j < config.numgroups &&
-                           config.groups[j] != NULL; j++)
+                       for (j = 0; config.groups != NULL &&
+                           j < (int)config.groups->sl_cur; j++)
                                sbuf_printf(buf, "%s\"%s\"", j ?
-                                   "," : "", config.groups[j]);
+                                   "," : "", config.groups->sl_str[j]);
                        quote = 0;
                        break;
                case _UC_DEFAULTCLASS:
@@ -453,19 +478,19 @@ write_userconfig(char const * file)
                            config.default_class : "");
                        break;
                case _UC_MINUID:
-                       sbuf_printf(buf, "%u", config.min_uid);
+                       sbuf_printf(buf, "%ju", (uintmax_t)config.min_uid);
                        quote = 0;
                        break;
                case _UC_MAXUID:
-                       sbuf_printf(buf, "%u", config.max_uid);
+                       sbuf_printf(buf, "%ju", (uintmax_t)config.max_uid);
                        quote = 0;
                        break;
                case _UC_MINGID:
-                       sbuf_printf(buf, "%u", config.min_gid);
+                       sbuf_printf(buf, "%ju", (uintmax_t)config.min_gid);
                        quote = 0;
                        break;
                case _UC_MAXGID:
-                       sbuf_printf(buf, "%u", config.max_gid);
+                       sbuf_printf(buf, "%ju", (uintmax_t)config.max_gid);
                        quote = 0;
                        break;
                case _UC_EXPIRE: