3 * David L. Nugent. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY DAVID L. NUGENT AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL DAVID L. NUGENT OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 static const char rcsid[] =
65 static char bourne_shell[] = "sh";
67 static char *system_shells[_UC_MAXSHELLS] =
73 static char const *booltrue[] =
75 "yes", "true", "1", "on", NULL
77 static char const *boolfalse[] =
79 "no", "false", "0", "off", NULL
82 static struct userconf config =
84 0, /* Default password for new users? (nologin) */
87 NULL, /* NIS version of the passwd file */
88 "/usr/share/skel", /* Where to obtain skeleton files */
89 NULL, /* Mail to send to new accounts */
90 "/var/log/userlog", /* Where to log changes */
91 "/home", /* Where to create home directory */
92 "/bin", /* Where shells are located */
93 system_shells, /* List of shells (first is default) */
94 bourne_shell, /* Default shell */
95 NULL, /* Default group name */
96 NULL, /* Default (additional) groups */
97 NULL, /* Default login class */
98 1000, 32000, /* Allowed range of uids */
99 1000, 32000, /* Allowed range of gids */
100 0, /* Days until account expires */
101 0, /* Days until password expires */
102 0 /* size of default_group array */
105 static char const *comments[_UC_FIELDS] =
107 "#\n# pw.conf - user/group configuration defaults\n#\n",
108 "\n# Password for new users? no=nologin yes=loginid none=blank random=random\n",
109 "\n# Reuse gaps in uid sequence? (yes or no)\n",
110 "\n# Reuse gaps in gid sequence? (yes or no)\n",
111 "\n# Path to the NIS passwd file (blank or 'no' for none)\n",
112 "\n# Obtain default dotfiles from this directory\n",
113 "\n# Mail this file to new user (/etc/newuser.msg or no)\n",
114 "\n# Log add/change/remove information in this file\n",
115 "\n# Root directory in which $HOME directory is created\n",
116 "\n# Colon separated list of directories containing valid shells\n",
117 "\n# Space separated list of available shells (without paths)\n",
118 "\n# Default shell (without path)\n",
119 "\n# Default group (leave blank for new group per user)\n",
120 "\n# Extra groups for new users\n",
121 "\n# Default login class for new users\n",
122 "\n# Range of valid default user ids\n",
124 "\n# Range of valid default group ids\n",
126 "\n# Days after which account expires (0=disabled)\n",
127 "\n# Days after which password expires (0=disabled)\n"
130 static char const *kwds[] =
157 unquote(char const * str)
159 if (str && (*str == '"' || *str == '\'')) {
160 char *p = strchr(str + 1, *str);
164 return (char *) (*++str ? str : NULL);
170 boolean_val(char const * str, int dflt)
172 if ((str = unquote(str)) != NULL) {
175 for (i = 0; booltrue[i]; i++)
176 if (strcmp(str, booltrue[i]) == 0)
178 for (i = 0; boolfalse[i]; i++)
179 if (strcmp(str, boolfalse[i]) == 0)
183 * Special cases for defaultpassword
185 if (strcmp(str, "random") == 0)
187 if (strcmp(str, "none") == 0)
201 return val ? booltrue[0] : boolfalse[0];
205 newstr(char const * p)
209 if ((p = unquote(p)) != NULL) {
210 int l = strlen(p) + 1;
212 if ((q = malloc(l)) != NULL)
222 read_userconfig(char const * file)
226 extendarray(&config.groups, &config.numgroups, 200);
227 memset(config.groups, 0, config.numgroups * sizeof(char *));
229 file = _PATH_PW_CONF;
230 if ((fp = fopen(file, "r")) != NULL) {
231 int buflen = LNBUFSZ;
232 char *buf = malloc(buflen);
235 while (fgets(buf, buflen, fp) != NULL) {
238 while ((p = strchr(buf, '\n')) == NULL) {
240 if (extendline(&buf, &buflen, buflen + LNBUFSZ) == -1) {
242 while ((ch = fgetc(fp)) != '\n' && ch != EOF);
243 goto nextline; /* Ignore it */
246 if (fgets(buf + l, buflen - l, fp) == NULL)
247 break; /* Unterminated last line */
253 if (*buf && (p = strtok(buf, " \t\r\n=")) != NULL && *p != '#') {
254 static char const toks[] = " \t\r\n,=";
255 char *q = strtok(NULL, toks);
258 while (i < _UC_FIELDS && strcmp(p, kwds[i]) != 0)
262 printf("Got unknown kwd `%s' val=`%s'\n", p, q ? q : "");
264 printf("Got kwd[%s]=%s\n", p, q);
268 config.default_password = boolean_val(q, 1);
271 config.reuse_uids = boolean_val(q, 0);
274 config.reuse_gids = boolean_val(q, 0);
277 config.nispasswd = (q == NULL || !boolean_val(q, 1))
281 config.dotdir = (q == NULL || !boolean_val(q, 1))
285 config.newmail = (q == NULL || !boolean_val(q, 1))
289 config.logfile = (q == NULL || !boolean_val(q, 1))
293 config.home = (q == NULL || !boolean_val(q, 1))
294 ? "/home" : newstr(q);
297 config.shelldir = (q == NULL || !boolean_val(q, 1))
298 ? "/bin" : newstr(q);
301 for (i = 0; i < _UC_MAXSHELLS && q != NULL; i++, q = strtok(NULL, toks))
302 system_shells[i] = newstr(q);
304 while (i < _UC_MAXSHELLS)
305 system_shells[i++] = NULL;
307 case _UC_DEFAULTSHELL:
308 config.shell_default = (q == NULL || !boolean_val(q, 1))
309 ? (char *) bourne_shell : newstr(q);
311 case _UC_DEFAULTGROUP:
313 config.default_group = (q == NULL || !boolean_val(q, 1) || GETGRNAM(q) == NULL)
316 case _UC_EXTRAGROUPS:
317 for (i = 0; q != NULL; q = strtok(NULL, toks)) {
318 if (extendarray(&config.groups, &config.numgroups, i + 2) != -1)
319 config.groups[i++] = newstr(q);
322 while (i < config.numgroups)
323 config.groups[i++] = NULL;
325 case _UC_DEFAULTCLASS:
326 config.default_class = (q == NULL || !boolean_val(q, 1))
330 if ((q = unquote(q)) != NULL && isdigit(*q))
331 config.min_uid = (uid_t) atol(q);
334 if ((q = unquote(q)) != NULL && isdigit(*q))
335 config.max_uid = (uid_t) atol(q);
338 if ((q = unquote(q)) != NULL && isdigit(*q))
339 config.min_gid = (gid_t) atol(q);
342 if ((q = unquote(q)) != NULL && isdigit(*q))
343 config.max_gid = (gid_t) atol(q);
346 if ((q = unquote(q)) != NULL && isdigit(*q))
347 config.expire_days = atoi(q);
350 if ((q = unquote(q)) != NULL && isdigit(*q))
351 config.password_days = atoi(q);
367 write_userconfig(char const * file)
372 file = _PATH_PW_CONF;
374 if ((fd = open(file, O_CREAT | O_RDWR | O_TRUNC | O_EXLOCK, 0644)) != -1) {
377 if ((fp = fdopen(fd, "w")) == NULL)
382 char *buf = malloc(len);
384 for (i = _UC_NONE; i < _UC_FIELDS; i++) {
386 char const *val = buf;
391 val = boolean_str(config.default_password);
394 val = boolean_str(config.reuse_uids);
397 val = boolean_str(config.reuse_gids);
400 val = config.nispasswd ? config.nispasswd : "";
404 val = config.dotdir ? config.dotdir : boolean_str(0);
407 val = config.newmail ? config.newmail : boolean_str(0);
410 val = config.logfile ? config.logfile : boolean_str(0);
416 val = config.shelldir;
419 for (j = k = 0; j < _UC_MAXSHELLS && system_shells[j] != NULL; j++) {
421 int l = snprintf(lbuf, sizeof lbuf, "%s\"%s\"", k ? "," : "", system_shells[j]);
422 if (l + k + 1 < len || extendline(&buf, &len, len + LNBUFSZ) != -1) {
423 strcpy(buf + k, lbuf);
429 case _UC_DEFAULTSHELL:
430 val = config.shell_default ? config.shell_default : bourne_shell;
432 case _UC_DEFAULTGROUP:
433 val = config.default_group ? config.default_group : "";
435 case _UC_EXTRAGROUPS:
436 extendarray(&config.groups, &config.numgroups, 200);
437 for (j = k = 0; j < config.numgroups && config.groups[j] != NULL; j++) {
439 int l = snprintf(lbuf, sizeof lbuf, "%s\"%s\"", k ? "," : "", config.groups[j]);
440 if (l + k + 1 < len || extendline(&buf, &len, len + 1024) != -1) {
441 strcpy(buf + k, lbuf);
447 case _UC_DEFAULTCLASS:
448 val = config.default_class ? config.default_class : "";
451 sprintf(buf, "%lu", (unsigned long) config.min_uid);
455 sprintf(buf, "%lu", (unsigned long) config.max_uid);
459 sprintf(buf, "%lu", (unsigned long) config.min_gid);
463 sprintf(buf, "%lu", (unsigned long) config.max_gid);
467 sprintf(buf, "%d", config.expire_days);
471 sprintf(buf, "%d", config.password_days);
479 fputs(comments[i], fp);
483 fprintf(fp, "%s = \"%s\"\n", kwds[i], val);
485 fprintf(fp, "%s = %s\n", kwds[i], val);
487 printf("WROTE: %s = %s\n", kwds[i], val);
492 return fclose(fp) != EOF;