]> git.cameronkatri.com Git - pw-darwin.git/blobdiff - pw/pw_conf.c
PREFIX stuff
[pw-darwin.git] / pw / pw_conf.c
index b723c31a39dd72bbbd7c0a3967e774db4901fac9..be051648e214186abf5fba2755de996c3311ba7b 100644 (file)
@@ -1,4 +1,6 @@
 /*-
+ * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+ *
  * Copyright (C) 1996
  *     David L. Nugent.  All rights reserved.
  *
@@ -29,13 +31,10 @@ static const char rcsid[] =
   "$FreeBSD$";
 #endif /* not lint */
 
-#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"
 
@@ -94,7 +93,7 @@ static struct userconf config =
        "/usr/share/skel",      /* Where to obtain skeleton files */
        NULL,                   /* Mail to send to new accounts */
        "/var/log/userlog",     /* Where to log changes */
-       "/home",                /* Where to create home directory */
+       "/var",         /* Where to create home directory */
        _DEF_DIRMODE,           /* Home directory perms, modified by umask */
        "/bin",                 /* Where shells are located */
        system_shells,          /* List of shells (first is default) */
@@ -186,14 +185,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;
 }
@@ -201,12 +218,14 @@ boolean_val(char const * str, int dflt)
 char const     *
 boolean_str(int val)
 {
-       if (val == -1)
-               return "random";
-       else if (val == -2)
-               return "none";
+       if (val == P_NO)
+               return (boolfalse[0]);
+       else if (val == P_RANDOM)
+               return ("random");
+       else if (val == P_NONE)
+               return ("none");
        else
-               return val ? booltrue[0] : boolfalse[0];
+               return (booltrue[0]);
 }
 
 char           *
@@ -235,12 +254,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;
-
        if ((fp = fopen(file, "r")) == NULL)
                return (&config);
 
@@ -261,7 +274,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);
@@ -287,7 +300,7 @@ read_userconfig(char const * file)
                                break;
                        case _UC_HOMEROOT:
                                config.home = (q == NULL || !boolean_val(q, 1))
-                                       ? "/home" : newstr(q);
+                                       ? "/var" : newstr(q);
                                break;
                        case _UC_HOMEMODE:
                                modeset = setmode(q);
@@ -316,8 +329,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))
@@ -391,15 +407,21 @@ read_userconfig(char const * file)
 
 
 int
-write_userconfig(char const * file)
+write_userconfig(struct userconf *cnf, const char *file)
 {
        int             fd;
        int             i, j;
-       struct sbuf     *buf;
+       FILE           *buffp;
        FILE           *fp;
-
-       if (file == NULL)
-               file = _PATH_PW_CONF;
+       char            cfgfile[MAXPATHLEN];
+       char           *buf;
+       size_t          sz;
+
+       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);
@@ -408,118 +430,124 @@ write_userconfig(char const * file)
                close(fd);
                return (0);
        }
-                       
-       buf = sbuf_new_auto();
+
+       sz = 0;
+       buf = NULL;
+       buffp = open_memstream(&buf, &sz);
+       if (buffp == NULL)
+               err(EXIT_FAILURE, "open_memstream()");
+
        for (i = _UC_NONE; i < _UC_FIELDS; i++) {
                int             quote = 1;
 
-               sbuf_clear(buf);
+               if (buf != NULL)
+                       memset(buf, 0, sz);
+               rewind(buffp);
                switch (i) {
                case _UC_DEFAULTPWD:
-                       sbuf_cat(buf, boolean_str(config.default_password));
+                       fputs(boolean_str(cnf->default_password), buffp);
                        break;
                case _UC_REUSEUID:
-                       sbuf_cat(buf, boolean_str(config.reuse_uids));
+                       fputs(boolean_str(cnf->reuse_uids), buffp);
                        break;
                case _UC_REUSEGID:
-                       sbuf_cat(buf, boolean_str(config.reuse_gids));
+                       fputs(boolean_str(cnf->reuse_gids), buffp);
                        break;
                case _UC_NISPASSWD:
-                       sbuf_cat(buf, config.nispasswd ?  config.nispasswd :
-                           "");
+                       fputs(cnf->nispasswd ?  cnf->nispasswd : "", buffp);
                        quote = 0;
                        break;
                case _UC_DOTDIR:
-                       sbuf_cat(buf, config.dotdir ?  config.dotdir :
-                           boolean_str(0));
+                       fputs(cnf->dotdir ?  cnf->dotdir : boolean_str(0),
+                           buffp);
                        break;
                case _UC_NEWMAIL:
-                       sbuf_cat(buf, config.newmail ?  config.newmail :
-                           boolean_str(0));
+                       fputs(cnf->newmail ?  cnf->newmail : boolean_str(0),
+                           buffp);
                        break;
                case _UC_LOGFILE:
-                       sbuf_cat(buf, config.logfile ?  config.logfile :
-                           boolean_str(0));
+                       fputs(cnf->logfile ?  cnf->logfile : boolean_str(0),
+                           buffp);
                        break;
                case _UC_HOMEROOT:
-                       sbuf_cat(buf, config.home);
+                       fputs(cnf->home, buffp);
                        break;
                case _UC_HOMEMODE:
-                       sbuf_printf(buf, "%04o", config.homemode);
+                       fprintf(buffp, "%04o", cnf->homemode);
                        quote = 0;
                        break;
                case _UC_SHELLPATH:
-                       sbuf_cat(buf, config.shelldir);
+                       fputs(cnf->shelldir, buffp);
                        break;
                case _UC_SHELLS:
                        for (j = 0; j < _UC_MAXSHELLS &&
                            system_shells[j] != NULL; j++)
-                               sbuf_printf(buf, "%s\"%s\"", j ?
+                               fprintf(buffp, "%s\"%s\"", j ?
                                    "," : "", system_shells[j]);
                        quote = 0;
                        break;
                case _UC_DEFAULTSHELL:
-                       sbuf_cat(buf, config.shell_default ?
-                           config.shell_default : bourne_shell);
+                       fputs(cnf->shell_default ?  cnf->shell_default :
+                           bourne_shell, buffp);
                        break;
                case _UC_DEFAULTGROUP:
-                       sbuf_cat(buf, config.default_group ?
-                           config.default_group : "");
+                       fputs(cnf->default_group ?  cnf->default_group : "",
+                           buffp);
                        break;
                case _UC_EXTRAGROUPS:
-                       for (j = 0; config.groups != NULL &&
-                           j < (int)config.groups->sl_cur; j++)
-                               sbuf_printf(buf, "%s\"%s\"", j ?
-                                   "," : "", config.groups->sl_str[j]);
+                       for (j = 0; cnf->groups != NULL &&
+                           j < (int)cnf->groups->sl_cur; j++)
+                               fprintf(buffp, "%s\"%s\"", j ?
+                                   "," : "", cnf->groups->sl_str[j]);
                        quote = 0;
                        break;
                case _UC_DEFAULTCLASS:
-                       sbuf_cat(buf, config.default_class ?
-                           config.default_class : "");
+                       fputs(cnf->default_class ?  cnf->default_class : "",
+                           buffp);
                        break;
                case _UC_MINUID:
-                       sbuf_printf(buf, "%ju", (uintmax_t)config.min_uid);
+                       fprintf(buffp, "%ju", (uintmax_t)cnf->min_uid);
                        quote = 0;
                        break;
                case _UC_MAXUID:
-                       sbuf_printf(buf, "%ju", (uintmax_t)config.max_uid);
+                       fprintf(buffp, "%ju", (uintmax_t)cnf->max_uid);
                        quote = 0;
                        break;
                case _UC_MINGID:
-                       sbuf_printf(buf, "%ju", (uintmax_t)config.min_gid);
+                       fprintf(buffp, "%ju", (uintmax_t)cnf->min_gid);
                        quote = 0;
                        break;
                case _UC_MAXGID:
-                       sbuf_printf(buf, "%ju", (uintmax_t)config.max_gid);
+                       fprintf(buffp, "%ju", (uintmax_t)cnf->max_gid);
                        quote = 0;
                        break;
                case _UC_EXPIRE:
-                       sbuf_printf(buf, "%d", config.expire_days);
+                       fprintf(buffp, "%jd", (intmax_t)cnf->expire_days);
                        quote = 0;
                        break;
                case _UC_PASSWORD:
-                       sbuf_printf(buf, "%d", config.password_days);
+                       fprintf(buffp, "%jd", (intmax_t)cnf->password_days);
                        quote = 0;
                        break;
                case _UC_NONE:
                        break;
                }
-               sbuf_finish(buf);
+               fflush(buffp);
 
                if (comments[i])
                        fputs(comments[i], fp);
 
                if (*kwds[i]) {
                        if (quote)
-                               fprintf(fp, "%s = \"%s\"\n", kwds[i],
-                                   sbuf_data(buf));
+                               fprintf(fp, "%s = \"%s\"\n", kwds[i], buf);
                        else
-                               fprintf(fp, "%s = %s\n", kwds[i], sbuf_data(buf));
+                               fprintf(fp, "%s = %s\n", kwds[i], buf);
 #if debugging
-                       printf("WROTE: %s = %s\n", kwds[i], sbuf_data(buf));
+                       printf("WROTE: %s = %s\n", kwds[i], buf);
 #endif
                }
        }
-       sbuf_delete(buf);
+       fclose(buffp);
+       free(buf);
        return (fclose(fp) != EOF);
 }