]> git.cameronkatri.com Git - pw-darwin.git/blobdiff - pw/pw_conf.c
Make pw_scan(3) more compatible with getpwent(3) et. al. when processing
[pw-darwin.git] / pw / pw_conf.c
index 10ded5cfe1075341335aa4cb87f15a1196840a9d..36bb8b61e9d14b0e7b63d2b3034c9e72578db2b5 100644 (file)
@@ -1,4 +1,6 @@
 /*-
+ * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+ *
  * Copyright (C) 1996
  *     David L. Nugent.  All rights reserved.
  *
@@ -31,11 +33,11 @@ static const char rcsid[] =
 
 #include <sys/types.h>
 #include <sys/sbuf.h>
-#include <inttypes.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"
 
@@ -186,14 +188,32 @@ boolean_val(char const * str, int dflt)
                for (i = 0; boolfalse[i]; i++)
                        if (strcmp(str, boolfalse[i]) == 0)
                                return 0;
+       }
+       return dflt;
+}
+
+int
+passwd_val(char const * str, int dflt)
+{
+       if ((str = unquote(str)) != NULL) {
+               int             i;
+
+               for (i = 0; booltrue[i]; i++)
+                       if (strcmp(str, booltrue[i]) == 0)
+                               return P_YES;
+               for (i = 0; boolfalse[i]; i++)
+                       if (strcmp(str, boolfalse[i]) == 0)
+                               return P_NO;
 
                /*
                 * Special cases for defaultpassword
                 */
                if (strcmp(str, "random") == 0)
-                       return -1;
+                       return P_RANDOM;
                if (strcmp(str, "none") == 0)
-                       return -2;
+                       return P_NONE;
+
+               errx(1, "Invalid value for default password");
        }
        return dflt;
 }
@@ -235,9 +255,6 @@ read_userconfig(char const * file)
        buf = NULL;
        linecap = 0;
 
-       config.groups = sl_init();
-       if (config.groups == NULL)
-               err(1, "sl_init()");
        if (file == NULL)
                file = _PATH_PW_CONF;
 
@@ -261,7 +278,7 @@ read_userconfig(char const * file)
 #endif
                        switch (i) {
                        case _UC_DEFAULTPWD:
-                               config.default_password = boolean_val(q, 1);
+                               config.default_password = passwd_val(q, 1);
                                break;
                        case _UC_REUSEUID:
                                config.reuse_uids = boolean_val(q, 0);
@@ -316,8 +333,11 @@ read_userconfig(char const * file)
                                        ? NULL : newstr(q);
                                break;
                        case _UC_EXTRAGROUPS:
-                               for (i = 0; q != NULL; q = strtok(NULL, toks))
+                               while ((q = strtok(NULL, toks)) != NULL) {
+                                       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))
@@ -348,6 +368,7 @@ read_userconfig(char const * file)
                                        if (errstr)
                                                warnx("Invalid min_gid: '%s';"
                                                    " ignoring", q);
+                               }
                                break;
                        case _UC_MAXGID:
                                if ((q = unquote(q)) != NULL) {
@@ -390,7 +411,7 @@ read_userconfig(char const * file)
 
 
 int
-write_userconfig(char const * file)
+write_userconfig(struct userconf *cnf, const char *file)
 {
        int             fd;
        int             i, j;
@@ -415,40 +436,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 &&
@@ -458,46 +478,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; config.groups != NULL &&
-                           j < (int)config.groups->sl_cur; j++)
+                       for (j = 0; cnf->groups != NULL &&
+                           j < (int)cnf->groups->sl_cur; j++)
                                sbuf_printf(buf, "%s\"%s\"", j ?
-                                   "," : "", config.groups->sl_str[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, "%ju", (uintmax_t)config.min_uid);
+                       sbuf_printf(buf, "%ju", (uintmax_t)cnf->min_uid);
                        quote = 0;
                        break;
                case _UC_MAXUID:
-                       sbuf_printf(buf, "%ju", (uintmax_t)config.max_uid);
+                       sbuf_printf(buf, "%ju", (uintmax_t)cnf->max_uid);
                        quote = 0;
                        break;
                case _UC_MINGID:
-                       sbuf_printf(buf, "%ju", (uintmax_t)config.min_gid);
+                       sbuf_printf(buf, "%ju", (uintmax_t)cnf->min_gid);
                        quote = 0;
                        break;
                case _UC_MAXGID:
-                       sbuf_printf(buf, "%ju", (uintmax_t)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, "%jd", (intmax_t)cnf->expire_days);
                        quote = 0;
                        break;
                case _UC_PASSWORD:
-                       sbuf_printf(buf, "%d", config.password_days);
+                       sbuf_printf(buf, "%jd", (intmax_t)cnf->password_days);
                        quote = 0;
                        break;
                case _UC_NONE: