]>
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>
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 "/home", /* 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)
190 * Special cases for defaultpassword
192 if (strcmp(str
, "random") == 0)
194 if (strcmp(str
, "none") == 0)
208 return val
? booltrue
[0] : boolfalse
[0];
212 newstr(char const * p
)
216 if ((p
= unquote(p
)) == NULL
)
219 if ((q
= strdup(p
)) == NULL
)
226 read_userconfig(char const * file
)
236 config
.groups
= sl_init();
237 if (config
.groups
== NULL
)
240 file
= _PATH_PW_CONF
;
242 if ((fp
= fopen(file
, "r")) == NULL
)
245 while ((linelen
= getline(&buf
, &linecap
, fp
)) > 0) {
246 if (*buf
&& (p
= strtok(buf
, " \t\r\n=")) != NULL
&& *p
!= '#') {
247 static char const toks
[] = " \t\r\n,=";
248 char *q
= strtok(NULL
, toks
);
252 while (i
< _UC_FIELDS
&& strcmp(p
, kwds
[i
]) != 0)
256 printf("Got unknown kwd `%s' val=`%s'\n", p
, q
? q
: "");
258 printf("Got kwd[%s]=%s\n", p
, q
);
262 config
.default_password
= boolean_val(q
, 1);
265 config
.reuse_uids
= boolean_val(q
, 0);
268 config
.reuse_gids
= boolean_val(q
, 0);
271 config
.nispasswd
= (q
== NULL
|| !boolean_val(q
, 1))
275 config
.dotdir
= (q
== NULL
|| !boolean_val(q
, 1))
279 config
.newmail
= (q
== NULL
|| !boolean_val(q
, 1))
283 config
.logfile
= (q
== NULL
|| !boolean_val(q
, 1))
287 config
.home
= (q
== NULL
|| !boolean_val(q
, 1))
288 ? "/home" : newstr(q
);
291 modeset
= setmode(q
);
292 config
.homemode
= (q
== NULL
|| !boolean_val(q
, 1))
293 ? _DEF_DIRMODE
: getmode(modeset
, _DEF_DIRMODE
);
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 sl_add(config
.groups
, newstr(q
));
320 case _UC_DEFAULTCLASS
:
321 config
.default_class
= (q
== NULL
|| !boolean_val(q
, 1))
325 if ((q
= unquote(q
)) != NULL
&& isdigit(*q
))
326 config
.min_uid
= (uid_t
) atol(q
);
329 if ((q
= unquote(q
)) != NULL
&& isdigit(*q
))
330 config
.max_uid
= (uid_t
) atol(q
);
333 if ((q
= unquote(q
)) != NULL
&& isdigit(*q
))
334 config
.min_gid
= (gid_t
) atol(q
);
337 if ((q
= unquote(q
)) != NULL
&& isdigit(*q
))
338 config
.max_gid
= (gid_t
) atol(q
);
341 if ((q
= unquote(q
)) != NULL
&& isdigit(*q
))
342 config
.expire_days
= atoi(q
);
345 if ((q
= unquote(q
)) != NULL
&& isdigit(*q
))
346 config
.password_days
= atoi(q
);
362 write_userconfig(char const * file
)
370 file
= _PATH_PW_CONF
;
372 if ((fd
= open(file
, O_CREAT
|O_RDWR
|O_TRUNC
|O_EXLOCK
, 0644)) == -1)
375 if ((fp
= fdopen(fd
, "w")) == NULL
) {
380 buf
= sbuf_new_auto();
381 for (i
= _UC_NONE
; i
< _UC_FIELDS
; i
++) {
387 sbuf_cat(buf
, boolean_str(config
.default_password
));
390 sbuf_cat(buf
, boolean_str(config
.reuse_uids
));
393 sbuf_cat(buf
, boolean_str(config
.reuse_gids
));
396 sbuf_cat(buf
, config
.nispasswd
? config
.nispasswd
:
401 sbuf_cat(buf
, config
.dotdir
? config
.dotdir
:
405 sbuf_cat(buf
, config
.newmail
? config
.newmail
:
409 sbuf_cat(buf
, config
.logfile
? config
.logfile
:
413 sbuf_cat(buf
, config
.home
);
416 sbuf_printf(buf
, "%04o", config
.homemode
);
420 sbuf_cat(buf
, config
.shelldir
);
423 for (j
= 0; j
< _UC_MAXSHELLS
&&
424 system_shells
[j
] != NULL
; j
++)
425 sbuf_printf(buf
, "%s\"%s\"", j
?
426 "," : "", system_shells
[j
]);
429 case _UC_DEFAULTSHELL
:
430 sbuf_cat(buf
, config
.shell_default
?
431 config
.shell_default
: bourne_shell
);
433 case _UC_DEFAULTGROUP
:
434 sbuf_cat(buf
, config
.default_group
?
435 config
.default_group
: "");
437 case _UC_EXTRAGROUPS
:
438 for (j
= 0; config
.groups
!= NULL
&&
439 j
< (int)config
.groups
->sl_cur
; j
++)
440 sbuf_printf(buf
, "%s\"%s\"", j
?
441 "," : "", config
.groups
->sl_str
[j
]);
444 case _UC_DEFAULTCLASS
:
445 sbuf_cat(buf
, config
.default_class
?
446 config
.default_class
: "");
449 sbuf_printf(buf
, "%u", config
.min_uid
);
453 sbuf_printf(buf
, "%u", config
.max_uid
);
457 sbuf_printf(buf
, "%u", config
.min_gid
);
461 sbuf_printf(buf
, "%u", config
.max_gid
);
465 sbuf_printf(buf
, "%d", config
.expire_days
);
469 sbuf_printf(buf
, "%d", config
.password_days
);
478 fputs(comments
[i
], fp
);
482 fprintf(fp
, "%s = \"%s\"\n", kwds
[i
],
485 fprintf(fp
, "%s = %s\n", kwds
[i
], sbuf_data(buf
));
487 printf("WROTE: %s = %s\n", kwds
[i
], sbuf_data(buf
));
492 return (fclose(fp
) != EOF
);