]>
git.cameronkatri.com Git - pw-darwin.git/blob - pw/pw_conf.c
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
5 * David L. Nugent. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY DAVID L. NUGENT AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL DAVID L. NUGENT OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 static const char rcsid
[] =
69 static char bourne_shell
[] = "sh";
71 static char *system_shells
[_UC_MAXSHELLS
] =
78 static char const *booltrue
[] =
80 "yes", "true", "1", "on", NULL
82 static char const *boolfalse
[] =
84 "no", "false", "0", "off", NULL
87 static struct userconf config
=
89 0, /* Default password for new users? (nologin) */
92 NULL
, /* NIS version of the passwd file */
93 "/usr/share/skel", /* Where to obtain skeleton files */
94 NULL
, /* Mail to send to new accounts */
95 "/var/log/userlog", /* Where to log changes */
96 "/var", /* Where to create home directory */
97 _DEF_DIRMODE
, /* Home directory perms, modified by umask */
98 "/bin", /* Where shells are located */
99 system_shells
, /* List of shells (first is default) */
100 bourne_shell
, /* Default shell */
101 NULL
, /* Default group name */
102 NULL
, /* Default (additional) groups */
103 NULL
, /* Default login class */
104 1000, 32000, /* Allowed range of uids */
105 1000, 32000, /* Allowed range of gids */
106 0, /* Days until account expires */
107 0 /* Days until password expires */
110 static char const *comments
[_UC_FIELDS
] =
112 "#\n# pw.conf - user/group configuration defaults\n#\n",
113 "\n# Password for new users? no=nologin yes=loginid none=blank random=random\n",
114 "\n# Reuse gaps in uid sequence? (yes or no)\n",
115 "\n# Reuse gaps in gid sequence? (yes or no)\n",
116 "\n# Path to the NIS passwd file (blank or 'no' for none)\n",
117 "\n# Obtain default dotfiles from this directory\n",
118 "\n# Mail this file to new user (/etc/newuser.msg or no)\n",
119 "\n# Log add/change/remove information in this file\n",
120 "\n# Root directory in which $HOME directory is created\n",
121 "\n# Mode for the new $HOME directory, will be modified by umask\n",
122 "\n# Colon separated list of directories containing valid shells\n",
123 "\n# Comma separated list of available shells (without paths)\n",
124 "\n# Default shell (without path)\n",
125 "\n# Default group (leave blank for new group per user)\n",
126 "\n# Extra groups for new users\n",
127 "\n# Default login class for new users\n",
128 "\n# Range of valid default user ids\n",
130 "\n# Range of valid default group ids\n",
132 "\n# Days after which account expires (0=disabled)\n",
133 "\n# Days after which password expires (0=disabled)\n"
136 static char const *kwds
[] =
164 unquote(char const * str
)
166 if (str
&& (*str
== '"' || *str
== '\'')) {
167 char *p
= strchr(str
+ 1, *str
);
171 return (char *) (*++str
? str
: NULL
);
177 boolean_val(char const * str
, int dflt
)
179 if ((str
= unquote(str
)) != NULL
) {
182 for (i
= 0; booltrue
[i
]; i
++)
183 if (strcmp(str
, booltrue
[i
]) == 0)
185 for (i
= 0; boolfalse
[i
]; i
++)
186 if (strcmp(str
, boolfalse
[i
]) == 0)
193 passwd_val(char const * str
, int dflt
)
195 if ((str
= unquote(str
)) != NULL
) {
198 for (i
= 0; booltrue
[i
]; i
++)
199 if (strcmp(str
, booltrue
[i
]) == 0)
201 for (i
= 0; boolfalse
[i
]; i
++)
202 if (strcmp(str
, boolfalse
[i
]) == 0)
206 * Special cases for defaultpassword
208 if (strcmp(str
, "random") == 0)
210 if (strcmp(str
, "none") == 0)
213 errx(1, "Invalid value for default password");
222 return (boolfalse
[0]);
223 else if (val
== P_RANDOM
)
225 else if (val
== P_NONE
)
228 return (booltrue
[0]);
232 newstr(char const * p
)
236 if ((p
= unquote(p
)) == NULL
)
239 if ((q
= strdup(p
)) == NULL
)
246 read_userconfig(char const * file
)
257 if ((fp
= fopen(file
, "r")) == NULL
)
260 while ((linelen
= getline(&buf
, &linecap
, fp
)) > 0) {
261 if (*buf
&& (p
= strtok(buf
, " \t\r\n=")) != NULL
&& *p
!= '#') {
262 static char const toks
[] = " \t\r\n,=";
263 char *q
= strtok(NULL
, toks
);
267 while (i
< _UC_FIELDS
&& strcmp(p
, kwds
[i
]) != 0)
271 printf("Got unknown kwd `%s' val=`%s'\n", p
, q
? q
: "");
273 printf("Got kwd[%s]=%s\n", p
, q
);
277 config
.default_password
= passwd_val(q
, 1);
280 config
.reuse_uids
= boolean_val(q
, 0);
283 config
.reuse_gids
= boolean_val(q
, 0);
286 config
.nispasswd
= (q
== NULL
|| !boolean_val(q
, 1))
290 config
.dotdir
= (q
== NULL
|| !boolean_val(q
, 1))
294 config
.newmail
= (q
== NULL
|| !boolean_val(q
, 1))
298 config
.logfile
= (q
== NULL
|| !boolean_val(q
, 1))
302 config
.home
= (q
== NULL
|| !boolean_val(q
, 1))
303 ? "/var" : newstr(q
);
306 modeset
= setmode(q
);
307 config
.homemode
= (q
== NULL
|| !boolean_val(q
, 1))
308 ? _DEF_DIRMODE
: getmode(modeset
, _DEF_DIRMODE
);
312 config
.shelldir
= (q
== NULL
|| !boolean_val(q
, 1))
313 ? "/bin" : newstr(q
);
316 for (i
= 0; i
< _UC_MAXSHELLS
&& q
!= NULL
; i
++, q
= strtok(NULL
, toks
))
317 system_shells
[i
] = newstr(q
);
319 while (i
< _UC_MAXSHELLS
)
320 system_shells
[i
++] = NULL
;
322 case _UC_DEFAULTSHELL
:
323 config
.shell_default
= (q
== NULL
|| !boolean_val(q
, 1))
324 ? (char *) bourne_shell
: newstr(q
);
326 case _UC_DEFAULTGROUP
:
328 config
.default_group
= (q
== NULL
|| !boolean_val(q
, 1) || GETGRNAM(q
) == NULL
)
331 case _UC_EXTRAGROUPS
:
332 while ((q
= strtok(NULL
, toks
)) != NULL
) {
333 if (config
.groups
== NULL
)
334 config
.groups
= sl_init();
335 sl_add(config
.groups
, newstr(q
));
338 case _UC_DEFAULTCLASS
:
339 config
.default_class
= (q
== NULL
|| !boolean_val(q
, 1))
343 if ((q
= unquote(q
)) != NULL
) {
344 config
.min_uid
= strtounum(q
, 0,
347 warnx("Invalid min_uid: '%s';"
352 if ((q
= unquote(q
)) != NULL
) {
353 config
.max_uid
= strtounum(q
, 0,
356 warnx("Invalid max_uid: '%s';"
361 if ((q
= unquote(q
)) != NULL
) {
362 config
.min_gid
= strtounum(q
, 0,
365 warnx("Invalid min_gid: '%s';"
370 if ((q
= unquote(q
)) != NULL
) {
371 config
.max_gid
= strtounum(q
, 0,
374 warnx("Invalid max_gid: '%s';"
379 if ((q
= unquote(q
)) != NULL
) {
380 config
.expire_days
= strtonum(q
, 0,
383 warnx("Invalid expire days:"
384 " '%s'; ignoring", q
);
388 if ((q
= unquote(q
)) != NULL
) {
389 config
.password_days
= strtonum(q
, 0,
392 warnx("Invalid password days:"
393 " '%s'; ignoring", q
);
410 write_userconfig(struct userconf
*cnf
, const char *file
)
416 char cfgfile
[MAXPATHLEN
];
421 snprintf(cfgfile
, sizeof(cfgfile
), "%s/" _PW_CONF
,
426 if ((fd
= open(file
, O_CREAT
|O_RDWR
|O_TRUNC
|O_EXLOCK
, 0644)) == -1)
429 if ((fp
= fdopen(fd
, "w")) == NULL
) {
436 buffp
= open_memstream(&buf
, &sz
);
438 err(EXIT_FAILURE
, "open_memstream()");
440 for (i
= _UC_NONE
; i
< _UC_FIELDS
; i
++) {
448 fputs(boolean_str(cnf
->default_password
), buffp
);
451 fputs(boolean_str(cnf
->reuse_uids
), buffp
);
454 fputs(boolean_str(cnf
->reuse_gids
), buffp
);
457 fputs(cnf
->nispasswd
? cnf
->nispasswd
: "", buffp
);
461 fputs(cnf
->dotdir
? cnf
->dotdir
: boolean_str(0),
465 fputs(cnf
->newmail
? cnf
->newmail
: boolean_str(0),
469 fputs(cnf
->logfile
? cnf
->logfile
: boolean_str(0),
473 fputs(cnf
->home
, buffp
);
476 fprintf(buffp
, "%04o", cnf
->homemode
);
480 fputs(cnf
->shelldir
, buffp
);
483 for (j
= 0; j
< _UC_MAXSHELLS
&&
484 system_shells
[j
] != NULL
; j
++)
485 fprintf(buffp
, "%s\"%s\"", j
?
486 "," : "", system_shells
[j
]);
489 case _UC_DEFAULTSHELL
:
490 fputs(cnf
->shell_default
? cnf
->shell_default
:
491 bourne_shell
, buffp
);
493 case _UC_DEFAULTGROUP
:
494 fputs(cnf
->default_group
? cnf
->default_group
: "",
497 case _UC_EXTRAGROUPS
:
498 for (j
= 0; cnf
->groups
!= NULL
&&
499 j
< (int)cnf
->groups
->sl_cur
; j
++)
500 fprintf(buffp
, "%s\"%s\"", j
?
501 "," : "", cnf
->groups
->sl_str
[j
]);
504 case _UC_DEFAULTCLASS
:
505 fputs(cnf
->default_class
? cnf
->default_class
: "",
509 fprintf(buffp
, "%ju", (uintmax_t)cnf
->min_uid
);
513 fprintf(buffp
, "%ju", (uintmax_t)cnf
->max_uid
);
517 fprintf(buffp
, "%ju", (uintmax_t)cnf
->min_gid
);
521 fprintf(buffp
, "%ju", (uintmax_t)cnf
->max_gid
);
525 fprintf(buffp
, "%jd", (intmax_t)cnf
->expire_days
);
529 fprintf(buffp
, "%jd", (intmax_t)cnf
->password_days
);
538 fputs(comments
[i
], fp
);
542 fprintf(fp
, "%s = \"%s\"\n", kwds
[i
], buf
);
544 fprintf(fp
, "%s = %s\n", kwds
[i
], buf
);
546 printf("WROTE: %s = %s\n", kwds
[i
], buf
);
552 return (fclose(fp
) != EOF
);