]>
git.cameronkatri.com Git - pw-darwin.git/blob - pw/pw_conf.c
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
[] =
32 #include <sys/types.h>
70 static char bourne_shell
[] = "sh";
72 static char *system_shells
[_UC_MAXSHELLS
] =
79 static char const *booltrue
[] =
81 "yes", "true", "1", "on", NULL
83 static char const *boolfalse
[] =
85 "no", "false", "0", "off", NULL
88 static struct userconf config
=
90 0, /* Default password for new users? (nologin) */
93 NULL
, /* NIS version of the passwd file */
94 "/usr/share/skel", /* Where to obtain skeleton files */
95 NULL
, /* Mail to send to new accounts */
96 "/var/log/userlog", /* Where to log changes */
97 "/home", /* Where to create home directory */
98 _DEF_DIRMODE
, /* Home directory perms, modified by umask */
99 "/bin", /* Where shells are located */
100 system_shells
, /* List of shells (first is default) */
101 bourne_shell
, /* Default shell */
102 NULL
, /* Default group name */
103 NULL
, /* Default (additional) groups */
104 NULL
, /* Default login class */
105 1000, 32000, /* Allowed range of uids */
106 1000, 32000, /* Allowed range of gids */
107 0, /* Days until account expires */
108 0 /* Days until password expires */
111 static char const *comments
[_UC_FIELDS
] =
113 "#\n# pw.conf - user/group configuration defaults\n#\n",
114 "\n# Password for new users? no=nologin yes=loginid none=blank random=random\n",
115 "\n# Reuse gaps in uid sequence? (yes or no)\n",
116 "\n# Reuse gaps in gid sequence? (yes or no)\n",
117 "\n# Path to the NIS passwd file (blank or 'no' for none)\n",
118 "\n# Obtain default dotfiles from this directory\n",
119 "\n# Mail this file to new user (/etc/newuser.msg or no)\n",
120 "\n# Log add/change/remove information in this file\n",
121 "\n# Root directory in which $HOME directory is created\n",
122 "\n# Mode for the new $HOME directory, will be modified by umask\n",
123 "\n# Colon separated list of directories containing valid shells\n",
124 "\n# Comma separated list of available shells (without paths)\n",
125 "\n# Default shell (without path)\n",
126 "\n# Default group (leave blank for new group per user)\n",
127 "\n# Extra groups for new users\n",
128 "\n# Default login class for new users\n",
129 "\n# Range of valid default user ids\n",
131 "\n# Range of valid default group ids\n",
133 "\n# Days after which account expires (0=disabled)\n",
134 "\n# Days after which password expires (0=disabled)\n"
137 static char const *kwds
[] =
165 unquote(char const * str
)
167 if (str
&& (*str
== '"' || *str
== '\'')) {
168 char *p
= strchr(str
+ 1, *str
);
172 return (char *) (*++str
? str
: NULL
);
178 boolean_val(char const * str
, int dflt
)
180 if ((str
= unquote(str
)) != NULL
) {
183 for (i
= 0; booltrue
[i
]; i
++)
184 if (strcmp(str
, booltrue
[i
]) == 0)
186 for (i
= 0; boolfalse
[i
]; i
++)
187 if (strcmp(str
, boolfalse
[i
]) == 0)
191 * Special cases for defaultpassword
193 if (strcmp(str
, "random") == 0)
195 if (strcmp(str
, "none") == 0)
209 return val
? booltrue
[0] : boolfalse
[0];
213 newstr(char const * p
)
217 if ((p
= unquote(p
)) == NULL
)
220 if ((q
= strdup(p
)) == NULL
)
227 read_userconfig(char const * file
)
238 config
.groups
= sl_init();
239 if (config
.groups
== NULL
)
242 file
= _PATH_PW_CONF
;
244 if ((fp
= fopen(file
, "r")) == NULL
)
247 while ((linelen
= getline(&buf
, &linecap
, fp
)) > 0) {
248 if (*buf
&& (p
= strtok(buf
, " \t\r\n=")) != NULL
&& *p
!= '#') {
249 static char const toks
[] = " \t\r\n,=";
250 char *q
= strtok(NULL
, toks
);
254 while (i
< _UC_FIELDS
&& strcmp(p
, kwds
[i
]) != 0)
258 printf("Got unknown kwd `%s' val=`%s'\n", p
, q
? q
: "");
260 printf("Got kwd[%s]=%s\n", p
, q
);
264 config
.default_password
= boolean_val(q
, 1);
267 config
.reuse_uids
= boolean_val(q
, 0);
270 config
.reuse_gids
= boolean_val(q
, 0);
273 config
.nispasswd
= (q
== NULL
|| !boolean_val(q
, 1))
277 config
.dotdir
= (q
== NULL
|| !boolean_val(q
, 1))
281 config
.newmail
= (q
== NULL
|| !boolean_val(q
, 1))
285 config
.logfile
= (q
== NULL
|| !boolean_val(q
, 1))
289 config
.home
= (q
== NULL
|| !boolean_val(q
, 1))
290 ? "/home" : newstr(q
);
293 modeset
= setmode(q
);
294 config
.homemode
= (q
== NULL
|| !boolean_val(q
, 1))
295 ? _DEF_DIRMODE
: getmode(modeset
, _DEF_DIRMODE
);
299 config
.shelldir
= (q
== NULL
|| !boolean_val(q
, 1))
300 ? "/bin" : newstr(q
);
303 for (i
= 0; i
< _UC_MAXSHELLS
&& q
!= NULL
; i
++, q
= strtok(NULL
, toks
))
304 system_shells
[i
] = newstr(q
);
306 while (i
< _UC_MAXSHELLS
)
307 system_shells
[i
++] = NULL
;
309 case _UC_DEFAULTSHELL
:
310 config
.shell_default
= (q
== NULL
|| !boolean_val(q
, 1))
311 ? (char *) bourne_shell
: newstr(q
);
313 case _UC_DEFAULTGROUP
:
315 config
.default_group
= (q
== NULL
|| !boolean_val(q
, 1) || GETGRNAM(q
) == NULL
)
318 case _UC_EXTRAGROUPS
:
319 for (i
= 0; q
!= NULL
; q
= strtok(NULL
, toks
))
320 sl_add(config
.groups
, newstr(q
));
322 case _UC_DEFAULTCLASS
:
323 config
.default_class
= (q
== NULL
|| !boolean_val(q
, 1))
327 if ((q
= unquote(q
)) != NULL
) {
328 config
.min_uid
= strtounum(q
, 0,
331 warnx("Invalid min_uid: '%s';"
336 if ((q
= unquote(q
)) != NULL
) {
337 config
.max_uid
= strtounum(q
, 0,
340 warnx("Invalid max_uid: '%s';"
345 if ((q
= unquote(q
)) != NULL
) {
346 config
.min_gid
= strtounum(q
, 0,
349 warnx("Invalid min_gid: '%s';"
353 if ((q
= unquote(q
)) != NULL
) {
354 config
.max_gid
= strtounum(q
, 0,
357 warnx("Invalid max_gid: '%s';"
362 if ((q
= unquote(q
)) != NULL
) {
363 config
.expire_days
= strtonum(q
, 0,
366 warnx("Invalid expire days:"
367 " '%s'; ignoring", q
);
371 if ((q
= unquote(q
)) != NULL
) {
372 config
.password_days
= strtonum(q
, 0,
375 warnx("Invalid password days:"
376 " '%s'; ignoring", q
);
393 write_userconfig(char const * file
)
401 file
= _PATH_PW_CONF
;
403 if ((fd
= open(file
, O_CREAT
|O_RDWR
|O_TRUNC
|O_EXLOCK
, 0644)) == -1)
406 if ((fp
= fdopen(fd
, "w")) == NULL
) {
411 buf
= sbuf_new_auto();
412 for (i
= _UC_NONE
; i
< _UC_FIELDS
; i
++) {
418 sbuf_cat(buf
, boolean_str(config
.default_password
));
421 sbuf_cat(buf
, boolean_str(config
.reuse_uids
));
424 sbuf_cat(buf
, boolean_str(config
.reuse_gids
));
427 sbuf_cat(buf
, config
.nispasswd
? config
.nispasswd
:
432 sbuf_cat(buf
, config
.dotdir
? config
.dotdir
:
436 sbuf_cat(buf
, config
.newmail
? config
.newmail
:
440 sbuf_cat(buf
, config
.logfile
? config
.logfile
:
444 sbuf_cat(buf
, config
.home
);
447 sbuf_printf(buf
, "%04o", config
.homemode
);
451 sbuf_cat(buf
, config
.shelldir
);
454 for (j
= 0; j
< _UC_MAXSHELLS
&&
455 system_shells
[j
] != NULL
; j
++)
456 sbuf_printf(buf
, "%s\"%s\"", j
?
457 "," : "", system_shells
[j
]);
460 case _UC_DEFAULTSHELL
:
461 sbuf_cat(buf
, config
.shell_default
?
462 config
.shell_default
: bourne_shell
);
464 case _UC_DEFAULTGROUP
:
465 sbuf_cat(buf
, config
.default_group
?
466 config
.default_group
: "");
468 case _UC_EXTRAGROUPS
:
469 for (j
= 0; config
.groups
!= NULL
&&
470 j
< (int)config
.groups
->sl_cur
; j
++)
471 sbuf_printf(buf
, "%s\"%s\"", j
?
472 "," : "", config
.groups
->sl_str
[j
]);
475 case _UC_DEFAULTCLASS
:
476 sbuf_cat(buf
, config
.default_class
?
477 config
.default_class
: "");
480 sbuf_printf(buf
, "%ju", (uintmax_t)config
.min_uid
);
484 sbuf_printf(buf
, "%ju", (uintmax_t)config
.max_uid
);
488 sbuf_printf(buf
, "%ju", (uintmax_t)config
.min_gid
);
492 sbuf_printf(buf
, "%ju", (uintmax_t)config
.max_gid
);
496 sbuf_printf(buf
, "%d", config
.expire_days
);
500 sbuf_printf(buf
, "%d", config
.password_days
);
509 fputs(comments
[i
], fp
);
513 fprintf(fp
, "%s = \"%s\"\n", kwds
[i
],
516 fprintf(fp
, "%s = %s\n", kwds
[i
], sbuf_data(buf
));
518 printf("WROTE: %s = %s\n", kwds
[i
], sbuf_data(buf
));
523 return (fclose(fp
) != EOF
);