]> git.cameronkatri.com Git - pw-darwin.git/blobdiff - pw/pw_conf.c
Split some extra long lines
[pw-darwin.git] / pw / pw_conf.c
index 46474a121c061202b42b950055934630a745ceb1..087f48e3fdf7a47b9034f89fc5c3d52e2f1b109f 100644 (file)
@@ -31,10 +31,11 @@ static const char rcsid[] =
 
 #include <sys/types.h>
 #include <sys/sbuf.h>
-#include <string.h>
-#include <ctype.h>
-#include <fcntl.h>
+
 #include <err.h>
+#include <fcntl.h>
+#include <string.h>
+#include <unistd.h>
 
 #include "pw.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] =
@@ -213,15 +213,12 @@ char           *
 newstr(char const * p)
 {
        char    *q;
-       size_t   l;
 
        if ((p = unquote(p)) == NULL)
                return (NULL);
 
-       l = strlen(p) + 1;
-
-       if ((q = strndup(p, l)) == NULL)
-               err(1, "strndup()");
+       if ((q = strdup(p)) == NULL)
+               err(1, "strdup()");
 
        return (q);
 }
@@ -231,142 +228,170 @@ read_userconfig(char const * file)
 {
        FILE    *fp;
        char    *buf, *p;
+       const char *errstr;
        size_t  linecap;
        ssize_t linelen;
 
        buf = NULL;
        linecap = 0;
 
-       extendarray(&config.groups, &config.numgroups, 200);
-       memset(config.groups, 0, config.numgroups * sizeof(char *));
        if (file == NULL)
                file = _PATH_PW_CONF;
 
-       if ((fp = fopen(file, "r")) != NULL) {
-               while ((linelen = getline(&buf, &linecap, fp)) > 0) {
-                       if (*buf && (p = strtok(buf, " \t\r\n=")) != NULL && *p != '#') {
-                               static char const toks[] = " \t\r\n,=";
-                               char           *q = strtok(NULL, toks);
-                               int             i = 0;
-                               mode_t          *modeset;
+       if ((fp = fopen(file, "r")) == NULL)
+               return (&config);
 
-                               while (i < _UC_FIELDS && strcmp(p, kwds[i]) != 0)
-                                       ++i;
+       while ((linelen = getline(&buf, &linecap, fp)) > 0) {
+               if (*buf && (p = strtok(buf, " \t\r\n=")) != NULL && *p != '#') {
+                       static char const toks[] = " \t\r\n,=";
+                       char           *q = strtok(NULL, toks);
+                       int             i = 0;
+                       mode_t          *modeset;
+
+                       while (i < _UC_FIELDS && strcmp(p, kwds[i]) != 0)
+                               ++i;
 #if debugging
-                               if (i == _UC_FIELDS)
-                                       printf("Got unknown kwd `%s' val=`%s'\n", p, q ? q : "");
-                               else
-                                       printf("Got kwd[%s]=%s\n", p, q);
+                       if (i == _UC_FIELDS)
+                               printf("Got unknown kwd `%s' val=`%s'\n", p, q ? q : "");
+                       else
+                               printf("Got kwd[%s]=%s\n", p, q);
 #endif
-                               switch (i) {
-                               case _UC_DEFAULTPWD:
-                                       config.default_password = boolean_val(q, 1);
-                                       break;
-                               case _UC_REUSEUID:
-                                       config.reuse_uids = boolean_val(q, 0);
-                                       break;
-                               case _UC_REUSEGID:
-                                       config.reuse_gids = boolean_val(q, 0);
-                                       break;
-                               case _UC_NISPASSWD:
-                                       config.nispasswd = (q == NULL || !boolean_val(q, 1))
-                                               ? NULL : newstr(q);
-                                       break;
-                               case _UC_DOTDIR:
-                                       config.dotdir = (q == NULL || !boolean_val(q, 1))
-                                               ? NULL : newstr(q);
-                                       break;
+                       switch (i) {
+                       case _UC_DEFAULTPWD:
+                               config.default_password = boolean_val(q, 1);
+                               break;
+                       case _UC_REUSEUID:
+                               config.reuse_uids = boolean_val(q, 0);
+                               break;
+                       case _UC_REUSEGID:
+                               config.reuse_gids = boolean_val(q, 0);
+                               break;
+                       case _UC_NISPASSWD:
+                               config.nispasswd = (q == NULL || !boolean_val(q, 1))
+                                       ? NULL : newstr(q);
+                               break;
+                       case _UC_DOTDIR:
+                               config.dotdir = (q == NULL || !boolean_val(q, 1))
+                                       ? NULL : newstr(q);
+                               break;
                                case _UC_NEWMAIL:
-                                       config.newmail = (q == NULL || !boolean_val(q, 1))
-                                               ? NULL : newstr(q);
-                                       break;
-                               case _UC_LOGFILE:
-                                       config.logfile = (q == NULL || !boolean_val(q, 1))
-                                               ? NULL : newstr(q);
-                                       break;
-                               case _UC_HOMEROOT:
-                                       config.home = (q == NULL || !boolean_val(q, 1))
-                                               ? "/home" : newstr(q);
-                                       break;
-                               case _UC_HOMEMODE:
-                                       modeset = setmode(q);
-                                       config.homemode = (q == NULL || !boolean_val(q, 1))
-                                               ? _DEF_DIRMODE : getmode(modeset, _DEF_DIRMODE);
-                                       free(modeset);
-                                       break;
-                               case _UC_SHELLPATH:
-                                       config.shelldir = (q == NULL || !boolean_val(q, 1))
-                                               ? "/bin" : newstr(q);
-                                       break;
-                               case _UC_SHELLS:
-                                       for (i = 0; i < _UC_MAXSHELLS && q != NULL; i++, q = strtok(NULL, toks))
-                                               system_shells[i] = newstr(q);
-                                       if (i > 0)
-                                               while (i < _UC_MAXSHELLS)
-                                                       system_shells[i++] = NULL;
-                                       break;
-                               case _UC_DEFAULTSHELL:
-                                       config.shell_default = (q == NULL || !boolean_val(q, 1))
-                                               ? (char *) bourne_shell : newstr(q);
-                                       break;
-                               case _UC_DEFAULTGROUP:
-                                       q = unquote(q);
-                                       config.default_group = (q == NULL || !boolean_val(q, 1) || GETGRNAM(q) == NULL)
-                                               ? 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;
-                                       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);
-                                       break;
-                               case _UC_MAXUID:
-                                       if ((q = unquote(q)) != NULL && isdigit(*q))
-                                               config.max_uid = (uid_t) atol(q);
-                                       break;
-                               case _UC_MINGID:
-                                       if ((q = unquote(q)) != NULL && isdigit(*q))
-                                               config.min_gid = (gid_t) atol(q);
-                                       break;
-                               case _UC_MAXGID:
-                                       if ((q = unquote(q)) != NULL && isdigit(*q))
-                                               config.max_gid = (gid_t) atol(q);
-                                       break;
-                               case _UC_EXPIRE:
-                                       if ((q = unquote(q)) != NULL && isdigit(*q))
-                                               config.expire_days = atoi(q);
-                                       break;
-                               case _UC_PASSWORD:
-                                       if ((q = unquote(q)) != NULL && isdigit(*q))
-                                               config.password_days = atoi(q);
-                                       break;
-                               case _UC_FIELDS:
-                               case _UC_NONE:
-                                       break;
+                               config.newmail = (q == NULL || !boolean_val(q, 1))
+                                       ? NULL : newstr(q);
+                               break;
+                       case _UC_LOGFILE:
+                               config.logfile = (q == NULL || !boolean_val(q, 1))
+                                       ? NULL : newstr(q);
+                               break;
+                       case _UC_HOMEROOT:
+                               config.home = (q == NULL || !boolean_val(q, 1))
+                                       ? "/home" : newstr(q);
+                               break;
+                       case _UC_HOMEMODE:
+                               modeset = setmode(q);
+                               config.homemode = (q == NULL || !boolean_val(q, 1))
+                                       ? _DEF_DIRMODE : getmode(modeset, _DEF_DIRMODE);
+                               free(modeset);
+                               break;
+                       case _UC_SHELLPATH:
+                               config.shelldir = (q == NULL || !boolean_val(q, 1))
+                                       ? "/bin" : newstr(q);
+                               break;
+                       case _UC_SHELLS:
+                               for (i = 0; i < _UC_MAXSHELLS && q != NULL; i++, q = strtok(NULL, toks))
+                                       system_shells[i] = newstr(q);
+                               if (i > 0)
+                                       while (i < _UC_MAXSHELLS)
+                                               system_shells[i++] = NULL;
+                               break;
+                       case _UC_DEFAULTSHELL:
+                               config.shell_default = (q == NULL || !boolean_val(q, 1))
+                                       ? (char *) bourne_shell : newstr(q);
+                               break;
+                       case _UC_DEFAULTGROUP:
+                               q = unquote(q);
+                               config.default_group = (q == NULL || !boolean_val(q, 1) || GETGRNAM(q) == NULL)
+                                       ? NULL : newstr(q);
+                               break;
+                       case _UC_EXTRAGROUPS:
+                               for (i = 0; q != NULL; q = strtok(NULL, toks)) {
+                                       if (config.groups == NULL)
+                                               config.groups = sl_init();
+                                       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) {
+                                       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) {
+                                       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) {
+                                       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) {
+                                       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) {
+                                       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) {
+                                       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:
+                               break;
                        }
                }
-               if (linecap > 0)
-                       free(buf);
-               fclose(fp);
        }
-       return &config;
+       free(buf);
+       fclose(fp);
+
+       return (&config);
 }
 
 
 int
-write_userconfig(char const * file)
+write_userconfig(struct userconf *cnf, const char *file)
 {
        int             fd;
        int             i, j;
@@ -391,40 +416,39 @@ write_userconfig(char const * file)
                sbuf_clear(buf);
                switch (i) {
                case _UC_DEFAULTPWD:
-                       sbuf_cat(buf, boolean_str(config.default_password));
+                       sbuf_cat(buf, boolean_str(cnf->default_password));
                        break;
                case _UC_REUSEUID:
-                       sbuf_cat(buf, boolean_str(config.reuse_uids));
+                       sbuf_cat(buf, boolean_str(cnf->reuse_uids));
                        break;
                case _UC_REUSEGID:
-                       sbuf_cat(buf, boolean_str(config.reuse_gids));
+                       sbuf_cat(buf, boolean_str(cnf->reuse_gids));
                        break;
                case _UC_NISPASSWD:
-                       sbuf_cat(buf, config.nispasswd ?  config.nispasswd :
-                           "");
+                       sbuf_cat(buf, cnf->nispasswd ?  cnf->nispasswd : "");
                        quote = 0;
                        break;
                case _UC_DOTDIR:
-                       sbuf_cat(buf, config.dotdir ?  config.dotdir :
+                       sbuf_cat(buf, cnf->dotdir ?  cnf->dotdir :
                            boolean_str(0));
                        break;
                case _UC_NEWMAIL:
-                       sbuf_cat(buf, config.newmail ?  config.newmail :
+                       sbuf_cat(buf, cnf->newmail ?  cnf->newmail :
                            boolean_str(0));
                        break;
                case _UC_LOGFILE:
-                       sbuf_cat(buf, config.logfile ?  config.logfile :
+                       sbuf_cat(buf, cnf->logfile ?  cnf->logfile :
                            boolean_str(0));
                        break;
                case _UC_HOMEROOT:
-                       sbuf_cat(buf, config.home);
+                       sbuf_cat(buf, cnf->home);
                        break;
                case _UC_HOMEMODE:
-                       sbuf_printf(buf, "%04o", config.homemode);
+                       sbuf_printf(buf, "%04o", cnf->homemode);
                        quote = 0;
                        break;
                case _UC_SHELLPATH:
-                       sbuf_cat(buf, config.shelldir);
+                       sbuf_cat(buf, cnf->shelldir);
                        break;
                case _UC_SHELLS:
                        for (j = 0; j < _UC_MAXSHELLS &&
@@ -434,46 +458,46 @@ write_userconfig(char const * file)
                        quote = 0;
                        break;
                case _UC_DEFAULTSHELL:
-                       sbuf_cat(buf, config.shell_default ?
-                           config.shell_default : bourne_shell);
+                       sbuf_cat(buf, cnf->shell_default ?
+                           cnf->shell_default : bourne_shell);
                        break;
                case _UC_DEFAULTGROUP:
-                       sbuf_cat(buf, config.default_group ?
-                           config.default_group : "");
+                       sbuf_cat(buf, cnf->default_group ?
+                           cnf->default_group : "");
                        break;
                case _UC_EXTRAGROUPS:
-                       for (j = 0; j < config.numgroups &&
-                           config.groups[j] != NULL; j++)
+                       for (j = 0; cnf->groups != NULL &&
+                           j < (int)cnf->groups->sl_cur; j++)
                                sbuf_printf(buf, "%s\"%s\"", j ?
-                                   "," : "", config.groups[j]);
+                                   "," : "", cnf->groups->sl_str[j]);
                        quote = 0;
                        break;
                case _UC_DEFAULTCLASS:
-                       sbuf_cat(buf, config.default_class ?
-                           config.default_class : "");
+                       sbuf_cat(buf, cnf->default_class ?
+                           cnf->default_class : "");
                        break;
                case _UC_MINUID:
-                       sbuf_printf(buf, "%lu", (unsigned long) config.min_uid);
+                       sbuf_printf(buf, "%ju", (uintmax_t)cnf->min_uid);
                        quote = 0;
                        break;
                case _UC_MAXUID:
-                       sbuf_printf(buf, "%lu", (unsigned long) config.max_uid);
+                       sbuf_printf(buf, "%ju", (uintmax_t)cnf->max_uid);
                        quote = 0;
                        break;
                case _UC_MINGID:
-                       sbuf_printf(buf, "%lu", (unsigned long) config.min_gid);
+                       sbuf_printf(buf, "%ju", (uintmax_t)cnf->min_gid);
                        quote = 0;
                        break;
                case _UC_MAXGID:
-                       sbuf_printf(buf, "%lu", (unsigned long) config.max_gid);
+                       sbuf_printf(buf, "%ju", (uintmax_t)cnf->max_gid);
                        quote = 0;
                        break;
                case _UC_EXPIRE:
-                       sbuf_printf(buf, "%d", config.expire_days);
+                       sbuf_printf(buf, "%ld", cnf->expire_days);
                        quote = 0;
                        break;
                case _UC_PASSWORD:
-                       sbuf_printf(buf, "%d", config.password_days);
+                       sbuf_printf(buf, "%ld", cnf->password_days);
                        quote = 0;
                        break;
                case _UC_NONE: