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[] =
29 "$FreeBSD: src/usr.sbin/pw/pw_conf.c,v 1.10.2.2 2001/01/14 08:41:19 dougb Exp $";
65 static char bourne_shell[] = "sh";
67 static char *system_shells[_UC_MAXSHELLS] =
74 static char const *booltrue[] =
76 "yes", "true", "1", "on", NULL
78 static char const *boolfalse[] =
80 "no", "false", "0", "off", NULL
83 static struct userconf config =
85 0, /* Default password for new users? (nologin) */
88 NULL, /* NIS version of the passwd file */
89 "/usr/share/skel", /* Where to obtain skeleton files */
90 NULL, /* Mail to send to new accounts */
91 "/var/log/userlog", /* Where to log changes */
92 "/home", /* Where to create home directory */
93 "/bin", /* Where shells are located */
94 system_shells, /* List of shells (first is default) */
95 bourne_shell, /* Default shell */
96 NULL, /* Default group name */
97 NULL, /* Default (additional) groups */
98 NULL, /* Default login class */
99 1000, 32000, /* Allowed range of uids */
100 1000, 32000, /* Allowed range of gids */
101 0, /* Days until account expires */
102 0, /* Days until password expires */
103 0 /* size of default_group array */
106 static char const *comments[_UC_FIELDS] =
108 "#\n# pw.conf - user/group configuration defaults\n#\n",
109 "\n# Password for new users? no=nologin yes=loginid none=blank random=random\n",
110 "\n# Reuse gaps in uid sequence? (yes or no)\n",
111 "\n# Reuse gaps in gid sequence? (yes or no)\n",
112 "\n# Path to the NIS passwd file (blank or 'no' for none)\n",
113 "\n# Obtain default dotfiles from this directory\n",
114 "\n# Mail this file to new user (/etc/newuser.msg or no)\n",
115 "\n# Log add/change/remove information in this file\n",
116 "\n# Root directory in which $HOME directory is created\n",
117 "\n# Colon separated list of directories containing valid shells\n",
118 "\n# Comma separated list of available shells (without paths)\n",
119 "\n# Default shell (without path)\n",
120 "\n# Default group (leave blank for new group per user)\n",
121 "\n# Extra groups for new users\n",
122 "\n# Default login class for new users\n",
123 "\n# Range of valid default user ids\n",
125 "\n# Range of valid default group ids\n",
127 "\n# Days after which account expires (0=disabled)\n",
128 "\n# Days after which password expires (0=disabled)\n"
131 static char const *kwds[] =
158 unquote(char const * str)
160 if (str && (*str == '"' || *str == '\'')) {
161 char *p = strchr(str + 1, *str);
165 return (char *) (*++str ? str : NULL);
171 boolean_val(char const * str, int dflt)
173 if ((str = unquote(str)) != NULL) {
176 for (i = 0; booltrue[i]; i++)
177 if (strcmp(str, booltrue[i]) == 0)
179 for (i = 0; boolfalse[i]; i++)
180 if (strcmp(str, boolfalse[i]) == 0)
184 * Special cases for defaultpassword
186 if (strcmp(str, "random") == 0)
188 if (strcmp(str, "none") == 0)
202 return val ? booltrue[0] : boolfalse[0];
206 newstr(char const * p)
210 if ((p = unquote(p)) != NULL) {
211 int l = strlen(p) + 1;
213 if ((q = malloc(l)) != NULL)
223 read_userconfig(char const * file)
227 extendarray(&config.groups, &config.numgroups, 200);
228 memset(config.groups, 0, config.numgroups * sizeof(char *));
230 file = _PATH_PW_CONF;
231 if ((fp = fopen(file, "r")) != NULL) {
232 int buflen = LNBUFSZ;
233 char *buf = malloc(buflen);
236 while (fgets(buf, buflen, fp) != NULL) {
239 while ((p = strchr(buf, '\n')) == NULL) {
241 if (extendline(&buf, &buflen, buflen + LNBUFSZ) == -1) {
243 while ((ch = fgetc(fp)) != '\n' && ch != EOF);
244 goto nextline; /* Ignore it */
247 if (fgets(buf + l, buflen - l, fp) == NULL)
248 break; /* Unterminated last line */
254 if (*buf && (p = strtok(buf, " \t\r\n=")) != NULL && *p != '#') {
255 static char const toks[] = " \t\r\n,=";
256 char *q = strtok(NULL, toks);
259 while (i < _UC_FIELDS && strcmp(p, kwds[i]) != 0)
263 printf("Got unknown kwd `%s' val=`%s'\n", p, q ? q : "");
265 printf("Got kwd[%s]=%s\n", p, q);
269 config.default_password = boolean_val(q, 1);
272 config.reuse_uids = boolean_val(q, 0);
275 config.reuse_gids = boolean_val(q, 0);
278 config.nispasswd = (q == NULL || !boolean_val(q, 1))
282 config.dotdir = (q == NULL || !boolean_val(q, 1))
286 config.newmail = (q == NULL || !boolean_val(q, 1))
290 config.logfile = (q == NULL || !boolean_val(q, 1))
294 config.home = (q == NULL || !boolean_val(q, 1))
295 ? "/home" : newstr(q);
298 config.shelldir = (q == NULL || !boolean_val(q, 1))
299 ? "/bin" : newstr(q);
302 for (i = 0; i < _UC_MAXSHELLS && q != NULL; i++, q = strtok(NULL, toks))
303 system_shells[i] = newstr(q);
305 while (i < _UC_MAXSHELLS)
306 system_shells[i++] = NULL;
308 case _UC_DEFAULTSHELL:
309 config.shell_default = (q == NULL || !boolean_val(q, 1))
310 ? (char *) bourne_shell : newstr(q);
312 case _UC_DEFAULTGROUP:
314 config.default_group = (q == NULL || !boolean_val(q, 1) || GETGRNAM(q) == NULL)
317 case _UC_EXTRAGROUPS:
318 for (i = 0; q != NULL; q = strtok(NULL, toks)) {
319 if (extendarray(&config.groups, &config.numgroups, i + 2) != -1)
320 config.groups[i++] = newstr(q);
323 while (i < config.numgroups)
324 config.groups[i++] = NULL;
326 case _UC_DEFAULTCLASS:
327 config.default_class = (q == NULL || !boolean_val(q, 1))
331 if ((q = unquote(q)) != NULL && isdigit(*q))
332 config.min_uid = (uid_t) atol(q);
335 if ((q = unquote(q)) != NULL && isdigit(*q))
336 config.max_uid = (uid_t) atol(q);
339 if ((q = unquote(q)) != NULL && isdigit(*q))
340 config.min_gid = (gid_t) atol(q);
343 if ((q = unquote(q)) != NULL && isdigit(*q))
344 config.max_gid = (gid_t) atol(q);
347 if ((q = unquote(q)) != NULL && isdigit(*q))
348 config.expire_days = atoi(q);
351 if ((q = unquote(q)) != NULL && isdigit(*q))
352 config.password_days = atoi(q);
368 write_userconfig(char const * file)
373 file = _PATH_PW_CONF;
375 if ((fd = open(file, O_CREAT | O_RDWR | O_TRUNC | O_EXLOCK, 0644)) != -1) {
378 if ((fp = fdopen(fd, "w")) == NULL)
383 char *buf = malloc(len);
385 for (i = _UC_NONE; i < _UC_FIELDS; i++) {
387 char const *val = buf;
392 val = boolean_str(config.default_password);
395 val = boolean_str(config.reuse_uids);
398 val = boolean_str(config.reuse_gids);
401 val = config.nispasswd ? config.nispasswd : "";
405 val = config.dotdir ? config.dotdir : boolean_str(0);
408 val = config.newmail ? config.newmail : boolean_str(0);
411 val = config.logfile ? config.logfile : boolean_str(0);
417 val = config.shelldir;
420 for (j = k = 0; j < _UC_MAXSHELLS && system_shells[j] != NULL; j++) {
422 int l = snprintf(lbuf, sizeof lbuf, "%s\"%s\"", k ? "," : "", system_shells[j]);
423 if (l + k + 1 < len || extendline(&buf, &len, len + LNBUFSZ) != -1) {
424 strcpy(buf + k, lbuf);
430 case _UC_DEFAULTSHELL:
431 val = config.shell_default ? config.shell_default : bourne_shell;
433 case _UC_DEFAULTGROUP:
434 val = config.default_group ? config.default_group : "";
436 case _UC_EXTRAGROUPS:
437 extendarray(&config.groups, &config.numgroups, 200);
438 for (j = k = 0; j < config.numgroups && config.groups[j] != NULL; j++) {
440 int l = snprintf(lbuf, sizeof lbuf, "%s\"%s\"", k ? "," : "", config.groups[j]);
441 if (l + k + 1 < len || extendline(&buf, &len, len + 1024) != -1) {
442 strcpy(buf + k, lbuf);
448 case _UC_DEFAULTCLASS:
449 val = config.default_class ? config.default_class : "";
452 sprintf(buf, "%lu", (unsigned long) config.min_uid);
456 sprintf(buf, "%lu", (unsigned long) config.max_uid);
460 sprintf(buf, "%lu", (unsigned long) config.min_gid);
464 sprintf(buf, "%lu", (unsigned long) config.max_gid);
468 sprintf(buf, "%d", config.expire_days);
472 sprintf(buf, "%d", config.password_days);
480 fputs(comments[i], fp);
484 fprintf(fp, "%s = \"%s\"\n", kwds[i], val);
486 fprintf(fp, "%s = %s\n", kwds[i], val);
488 printf("WROTE: %s = %s\n", kwds[i], val);
493 return fclose(fp) != EOF;