]> git.cameronkatri.com Git - pw-darwin.git/blobdiff - pw/pw_conf.c
pw: respect path specified using -V when writing pw.conf, and -C is not
[pw-darwin.git] / pw / pw_conf.c
index 087f48e3fdf7a47b9034f89fc5c3d52e2f1b109f..f7b98f188f7767a5cf6632e5b2e82782a6e7ca03 100644 (file)
@@ -1,4 +1,6 @@
 /*-
+ * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+ *
  * Copyright (C) 1996
  *     David L. Nugent.  All rights reserved.
  *
@@ -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;
 }
@@ -258,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);
@@ -313,7 +333,7 @@ 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));
@@ -397,9 +417,13 @@ write_userconfig(struct userconf *cnf, const char *file)
        int             i, j;
        struct sbuf     *buf;
        FILE           *fp;
+       char            cfgfile[MAXPATHLEN];
 
-       if (file == NULL)
-               file = _PATH_PW_CONF;
+       if (file == NULL) {
+               snprintf(cfgfile, sizeof(cfgfile), "%s/" _PW_CONF,
+                   conf.etcpath);
+               file = cfgfile;
+       }
 
        if ((fd = open(file, O_CREAT|O_RDWR|O_TRUNC|O_EXLOCK, 0644)) == -1)
                return (0);
@@ -493,11 +517,11 @@ write_userconfig(struct userconf *cnf, const char *file)
                        quote = 0;
                        break;
                case _UC_EXPIRE:
-                       sbuf_printf(buf, "%ld", cnf->expire_days);
+                       sbuf_printf(buf, "%jd", (intmax_t)cnf->expire_days);
                        quote = 0;
                        break;
                case _UC_PASSWORD:
-                       sbuf_printf(buf, "%ld", cnf->password_days);
+                       sbuf_printf(buf, "%jd", (intmax_t)cnf->password_days);
                        quote = 0;
                        break;
                case _UC_NONE: