]> git.cameronkatri.com Git - pw-darwin.git/blobdiff - pw/pw_conf.c
Use arc4random_uniform() to avoid "modulo bias"
[pw-darwin.git] / pw / pw_conf.c
index 63742a74349e538a90d2c1f6159adda2696bcb95..51672b9ae4932d01faca483a085a773209936ef1 100644 (file)
@@ -26,7 +26,7 @@
 
 #ifndef lint
 static const char rcsid[] =
-       "$Id$";
+  "$FreeBSD$";
 #endif /* not lint */
 
 #include <string.h>
@@ -34,7 +34,6 @@ static const char rcsid[] =
 #include <fcntl.h>
 
 #include "pw.h"
-#include "pwupd.h"
 
 #define debugging 0
 
@@ -48,6 +47,7 @@ enum {
        _UC_NEWMAIL,
        _UC_LOGFILE,
        _UC_HOMEROOT,
+       _UC_HOMEMODE,
        _UC_SHELLPATH,
        _UC_SHELLS,
        _UC_DEFAULTSHELL,
@@ -68,7 +68,8 @@ static char     bourne_shell[] = "sh";
 static char    *system_shells[_UC_MAXSHELLS] =
 {
        bourne_shell,
-       "csh"
+       "csh",
+       "tcsh"
 };
 
 static char const *booltrue[] =
@@ -90,6 +91,7 @@ static struct userconf config =
        NULL,                   /* Mail to send to new accounts */
        "/var/log/userlog",     /* Where to log changes */
        "/home",                /* Where to create home directory */
+       0777,                   /* Home directory perms, modified by umask */
        "/bin",                 /* Where shells are located */
        system_shells,          /* List of shells (first is default) */
        bourne_shell,           /* Default shell */
@@ -99,7 +101,8 @@ 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,                      /* Days until password expires */
+       0                       /* size of default_group array */
 };
 
 static char const *comments[_UC_FIELDS] =
@@ -113,8 +116,9 @@ static char const *comments[_UC_FIELDS] =
        "\n# Mail this file to new user (/etc/newuser.msg or no)\n",
        "\n# Log add/change/remove information in this file\n",
        "\n# Root directory in which $HOME directory is created\n",
+       "\n# Mode for the new $HOME directory, will be modified by umask\n",
        "\n# Colon separated list of directories containing valid shells\n",
-       "\n# Space separated list of available shells (without paths)\n",
+       "\n# Comma separated list of available shells (without paths)\n",
        "\n# Default shell (without path)\n",
        "\n# Default group (leave blank for new group per user)\n",
        "\n# Extra groups for new users\n",
@@ -138,6 +142,7 @@ static char const *kwds[] =
        "newmail",
        "logfile",
        "home",
+       "homemode",
        "shellpath",
        "shells",
        "defaultshell",
@@ -254,6 +259,7 @@ read_userconfig(char const * file)
                                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;
@@ -293,6 +299,12 @@ read_userconfig(char const * file)
                                        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))
+                                               ? 0777 : getmode(modeset, 0777);
+                                       free(modeset);
+                                       break;
                                case _UC_SHELLPATH:
                                        config.shelldir = (q == NULL || !boolean_val(q, 1))
                                                ? "/bin" : newstr(q);
@@ -310,7 +322,7 @@ read_userconfig(char const * file)
                                        break;
                                case _UC_DEFAULTGROUP:
                                        q = unquote(q);
-                                       config.default_group = (q == NULL || !boolean_val(q, 1) || getgrnam(q) == NULL)
+                                       config.default_group = (q == NULL || !boolean_val(q, 1) || GETGRNAM(q) == NULL)
                                                ? NULL : newstr(q);
                                        break;
                                case _UC_EXTRAGROUPS:
@@ -412,6 +424,10 @@ write_userconfig(char const * file)
                                case _UC_HOMEROOT:
                                        val = config.home;
                                        break;
+                               case _UC_HOMEMODE:
+                                       sprintf(buf, "%04o", config.homemode);
+                                       quote = 0;
+                                       break;
                                case _UC_SHELLPATH:
                                        val = config.shelldir;
                                        break;
@@ -419,6 +435,8 @@ write_userconfig(char const * file)
                                        for (j = k = 0; j < _UC_MAXSHELLS && system_shells[j] != NULL; j++) {
                                                char    lbuf[64];
                                                int     l = snprintf(lbuf, sizeof lbuf, "%s\"%s\"", k ? "," : "", system_shells[j]);
+                                               if (l < 0)
+                                                       l = 0;
                                                if (l + k + 1 < len || extendline(&buf, &len, len + LNBUFSZ) != -1) {
                                                        strcpy(buf + k, lbuf);
                                                        k += l;
@@ -437,6 +455,8 @@ write_userconfig(char const * file)
                                        for (j = k = 0; j < config.numgroups && config.groups[j] != NULL; j++) {
                                                char    lbuf[64];
                                                int     l = snprintf(lbuf, sizeof lbuf, "%s\"%s\"", k ? "," : "", config.groups[j]);
+                                               if (l < 0)
+                                                       l = 0;
                                                if (l + k + 1 < len || extendline(&buf, &len, len + 1024) != -1) {
                                                        strcpy(buf + k, lbuf);
                                                        k +=  l;