Deleted Added
full compact
pw_user.c (20267) pw_user.c (20302)
1/*-
1/*-
2 * Copyright (c) 1996 by David L. Nugent <davidn@blaze.net.au>.
3 * All rights reserved.
2 * Copyright (C) 1996
3 * David L. Nugent. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer as
10 * the first lines of this file unmodified.
9 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
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 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by David L. Nugent.
17 * 4. The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
19 *
13 *
20 * THIS SOFTWARE IS PROVIDED BY THE DAVID L. NUGENT ``AS IS'' AND
14 * THIS SOFTWARE IS PROVIDED BY DAVID L. NUGENT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL DAVID L. NUGENT BE LIABLE
17 * ARE DISCLAIMED. IN NO EVENT SHALL DAVID L. NUGENT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 *
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
24 * SUCH DAMAGE.
25 *
32 * $Id$
26 * $Id: pw_user.c,v 1.1.1.2 1996/12/09 23:55:10 joerg Exp $
33 */
34
35#include <unistd.h>
36#include <fcntl.h>
37#include <ctype.h>
38#include <paths.h>
39#include <sys/param.h>
40#include <dirent.h>
41#include <termios.h>
42#include "pw.h"
43#include "bitmap.h"
44#include "pwupd.h"
45
46static int print_user(struct passwd * pwd, int pretty);
47static uid_t pw_uidpolicy(struct userconf * cnf, struct cargs * args);
48static uid_t pw_gidpolicy(struct userconf * cnf, struct cargs * args, char *nam, gid_t prefer);
49static time_t pw_pwdpolicy(struct userconf * cnf, struct cargs * args);
50static time_t pw_exppolicy(struct userconf * cnf, struct cargs * args);
51static char *pw_homepolicy(struct userconf * cnf, struct cargs * args, char const * user);
52static char *pw_shellpolicy(struct userconf * cnf, struct cargs * args, char *newshell);
53static char *pw_password(struct userconf * cnf, struct cargs * args, char const * user);
54static char *pw_checkname(char *name, int gecos);
55static char *shell_path(char const * path, char *shells[], char *sh);
56static void rmat(uid_t uid);
57
58/*-
59 * -C config configuration file
60 * -q quiet operation
61 * -n name login name
62 * -u uid user id
63 * -c comment user name/comment
64 * -d directory home directory
65 * -e date account expiry date
66 * -p date password expiry date
67 * -g grp primary group
68 * -G grp1,grp2 additional groups
69 * -m [ -k dir ] create and set up home
70 * -s shell name of login shell
71 * -o duplicate uid ok
72 * -L class user class
73 * -l name new login name
74 * -h fd password filehandle
75 * -F force print or add
76 * Setting defaults:
77 * -D set user defaults
78 * -b dir default home root dir
79 * -e period default expiry period
80 * -p period default password change period
81 * -g group default group
82 * -G grp1,grp2.. default additional groups
83 * -L class default login class
84 * -k dir default home skeleton
85 * -s shell default shell
86 * -w method default password method
87 */
88
89int
90pw_user(struct userconf * cnf, int mode, struct cargs * args)
91{
92 char *p = NULL;
93 struct carg *a_name;
94 struct carg *a_uid;
95 struct carg *arg;
96 struct passwd *pwd = NULL;
97 struct group *grp;
98 struct stat st;
99 char line[MAXPWLINE];
100
101 static struct passwd fakeuser =
102 {
103 NULL,
104 "*",
105 -1,
106 -1,
107 0,
108 "",
109 "User &",
110 "/bin/sh",
111 0,
112 0
113 };
114
115 /*
116 * With M_NEXT, we only need to return the
117 * next uid to stdout
118 */
119 if (mode == M_NEXT)
120 {
121 uid_t next = pw_uidpolicy(cnf, args);
122 if (getarg(args, 'q'))
123 return next;
124 printf("%ld:", (long)next);
125 pw_group(cnf, mode, args);
126 return EXIT_SUCCESS;
127 }
128
129 /*
130 * We can do all of the common legwork here
131 */
132
133 if ((arg = getarg(args, 'b')) != NULL) {
134 cnf->home = arg->val;
135 if (stat(cnf->home, &st) == -1 || !S_ISDIR(st.st_mode))
136 cmderr(EX_OSFILE, "root home `%s' is not a directory or does not exist\n", cnf->home);
137 }
138 if ((arg = getarg(args, 'e')) != NULL)
139 cnf->expire_days = atoi(arg->val);
140
141 if ((arg = getarg(args, 'p')) != NULL && arg->val)
142 cnf->password_days = atoi(arg->val);
143
144 if ((arg = getarg(args, 'g')) != NULL) {
145 p = arg->val;
146 if ((grp = getgrnam(p)) == NULL) {
147 if (!isdigit(*p) || (grp = getgrgid((gid_t) atoi(p))) == NULL)
148 cmderr(EX_NOUSER, "group `%s' does not exist\n", p);
149 }
150 cnf->default_group = newstr(grp->gr_name);
151 }
152 if ((arg = getarg(args, 'L')) != NULL)
153 cnf->default_class = pw_checkname(arg->val, 0);
154
155 if ((arg = getarg(args, 'G')) != NULL && arg->val) {
156 int i = 0;
157
158 for (p = strtok(arg->val, ", \t"); i < _UC_MAXGROUPS && p != NULL; p = strtok(NULL, ", \t")) {
159 if ((grp = getgrnam(p)) == NULL) {
160 if (!isdigit(*p) || (grp = getgrgid((gid_t) atoi(p))) == NULL)
161 cmderr(EX_NOUSER, "group `%s' does not exist\n", p);
162 }
163 cnf->groups[i++] = newstr(grp->gr_name);
164 }
165 while (i < _UC_MAXGROUPS)
166 cnf->groups[i++] = NULL;
167 }
168 if ((arg = getarg(args, 'k')) != NULL) {
169 if (stat(cnf->dotdir = arg->val, &st) == -1 || S_ISDIR(st.st_mode))
170 cmderr(EX_OSFILE, "skeleton `%s' is not a directory or does not exist\n", cnf->dotdir);
171 }
172 if ((arg = getarg(args, 's')) != NULL)
173 cnf->shell_default = arg->val;
174
175 if (mode == M_ADD && getarg(args, 'D')) {
176 if (getarg(args, 'n') != NULL)
177 cmderr(EX_DATAERR, "can't combine `-D' with `-n name'\n");
178 if ((arg = getarg(args, 'u')) != NULL && (p = strtok(arg->val, ", \t")) != NULL) {
179 if ((cnf->min_uid = (uid_t) atoi(p)) == 0)
180 cnf->min_uid = 1000;
181 if ((p = strtok(NULL, " ,\t")) == NULL || (cnf->max_uid = (uid_t) atoi(p)) < cnf->min_uid)
182 cnf->max_uid = 32000;
183 }
184 if ((arg = getarg(args, 'i')) != NULL && (p = strtok(arg->val, ", \t")) != NULL) {
185 if ((cnf->min_gid = (gid_t) atoi(p)) == 0)
186 cnf->min_gid = 1000;
187 if ((p = strtok(NULL, " ,\t")) == NULL || (cnf->max_gid = (gid_t) atoi(p)) < cnf->min_gid)
188 cnf->max_gid = 32000;
189 }
190 if ((arg = getarg(args, 'w')) != NULL)
191 cnf->default_password = boolean_val(arg->val, cnf->default_password);
192
193 arg = getarg(args, 'C');
194 if (write_userconfig(arg ? arg->val : NULL))
195 return EXIT_SUCCESS;
196 perror("config update");
197 return EX_IOERR;
198 }
199 if (mode == M_PRINT && getarg(args, 'a')) {
200 int pretty = getarg(args, 'P') != NULL;
201
202 setpwent();
203 while ((pwd = getpwent()) != NULL)
204 print_user(pwd, pretty);
205 endpwent();
206 return EXIT_SUCCESS;
207 }
208 if ((a_name = getarg(args, 'n')) != NULL)
209 pwd = getpwnam(pw_checkname(a_name->val, 0));
210 a_uid = getarg(args, 'u');
211
212 if (a_uid == NULL) {
213 if (a_name == NULL)
214 cmderr(EX_DATAERR, "user name or id required\n");
215
216 /*
217 * Determine whether 'n' switch is name or uid - we don't
218 * really don't really care which we have, but we need to
219 * know.
220 */
221 if (mode != M_ADD && pwd == NULL && isdigit(*a_name->val) && atoi(a_name->val) > 0) { /* Assume uid */
222 (a_uid = a_name)->ch = 'u';
223 a_name = NULL;
224 }
225 }
226 /*
227 * Update, delete & print require that the user exists
228 */
229 if (mode == M_UPDATE || mode == M_DELETE || mode == M_PRINT) {
230 if (a_name == NULL && pwd == NULL) /* Try harder */
231 pwd = getpwuid(atoi(a_uid->val));
232
233 if (pwd == NULL) {
234 if (mode == M_PRINT && getarg(args, 'F')) {
235 fakeuser.pw_name = a_name ? a_name->val : "nouser";
236 fakeuser.pw_uid = a_uid ? (uid_t) atol(a_uid->val) : -1;
237 return print_user(&fakeuser, getarg(args, 'P') != NULL);
238 }
239 if (a_name == NULL)
240 cmderr(EX_NOUSER, "no such uid `%s'\n", a_uid->val);
241 cmderr(EX_NOUSER, "no such user `%s'\n", a_name->val);
242 }
243 if (a_name == NULL) /* May be needed later */
244 a_name = addarg(args, 'n', newstr(pwd->pw_name));
245
246 /*
247 * Handle deletions now
248 */
249 if (mode == M_DELETE) {
250 char file[MAXPATHLEN];
251 char home[MAXPATHLEN];
252 uid_t uid = pwd->pw_uid;
253
254 if (strcmp(pwd->pw_name, "root") == 0)
255 cmderr(EX_DATAERR, "cannot remove user 'root'\n");
256
257 /*
258 * Remove crontabs
259 */
260 sprintf(file, "/var/cron/tabs/%s", pwd->pw_name);
261 if (access(file, F_OK) == 0) {
262 sprintf(file, "crontab -u %s -r", pwd->pw_name);
263 system(file);
264 }
265 /*
266 * Save these for later, since contents of pwd may be
267 * invalidated by deletion
268 */
269 sprintf(file, "%s/%s", _PATH_MAILDIR, pwd->pw_name);
270 strncpy(home, pwd->pw_dir, sizeof home);
271 home[sizeof home - 1] = '\0';
272
273 if (!delpwent(pwd))
274 cmderr(EX_IOERR, "Error updating passwd file: %s\n", strerror(errno));
275 editgroups(a_name->val, NULL);
276
277 pw_log(cnf, mode, W_USER, "%s(%ld) account removed", a_name->val, (long) uid);
278
279 /*
280 * Remove mail file
281 */
282 remove(file);
283
284 /*
285 * Remove at jobs
286 */
287 if (getpwuid(uid) == NULL)
288 rmat(uid);
289
290 /*
291 * Remove home directory and contents
292 */
293 if (getarg(args, 'r') != NULL && *home == '/' && getpwuid(uid) == NULL) {
294 if (stat(home, &st) != -1) {
295 rm_r(home, uid);
296 pw_log(cnf, mode, W_USER, "%s(%ld) home '%s' %sremoved",
297 a_name->val, (long) uid, home,
298 stat(home, &st) == -1 ? "" : "not completely ");
299 }
300 }
301 return EXIT_SUCCESS;
302 } else if (mode == M_PRINT)
303 return print_user(pwd, getarg(args, 'P') != NULL);
304
305 /*
306 * The rest is edit code
307 */
308 if ((arg = getarg(args, 'l')) != NULL) {
309 if (strcmp(pwd->pw_name, "root") == 0)
310 cmderr(EX_DATAERR, "can't rename `root' account\n");
311 pwd->pw_name = pw_checkname(arg->val, 0);
312 }
313 if ((arg = getarg(args, 'u')) != NULL && isdigit(*arg->val)) {
314 pwd->pw_uid = (uid_t) atol(arg->val);
315 if (pwd->pw_uid != 0 && strcmp(pwd->pw_name, "root") == 0)
316 cmderr(EX_DATAERR, "can't change uid of `root' account\n");
317 if (pwd->pw_uid == 0 && strcmp(pwd->pw_name, "root") != 0)
318 fprintf(stderr, "WARNING: account `%s' will have a uid of 0 (superuser access!)\n", pwd->pw_name);
319 }
320 if ((arg = getarg(args, 'g')) != NULL && pwd->pw_uid != 0) /* Already checked this */
321 pwd->pw_gid = (gid_t) getgrnam(cnf->default_group)->gr_gid;
322
323 if ((arg = getarg(args, 'p')) != NULL) {
324 if (*arg->val == '\0' || strcmp(arg->val, "0") == 0)
325 pwd->pw_change = 0;
326 else {
327 time_t now = time(NULL);
328 time_t expire = parse_date(now, arg->val);
329
330 if (now == expire)
331 cmderr(EX_DATAERR, "Invalid password change date `%s'\n", arg->val);
332 pwd->pw_change = expire;
333 }
334 }
335 if ((arg = getarg(args, 'e')) != NULL) {
336 if (*arg->val == '\0' || strcmp(arg->val, "0") == 0)
337 pwd->pw_expire = 0;
338 else {
339 time_t now = time(NULL);
340 time_t expire = parse_date(now, arg->val);
341
342 if (now == expire)
343 cmderr(EX_DATAERR, "Invalid account expiry date `%s'\n", arg->val);
344 pwd->pw_expire = expire;
345 }
346 }
347 if ((arg = getarg(args, 's')) != NULL)
348 pwd->pw_shell = shell_path(cnf->shelldir, cnf->shells, arg->val);
349
350 if (getarg(args, 'L'))
351 pwd->pw_class = cnf->default_class;
352
353 if ((arg = getarg(args, 'w')) != NULL && getarg(args, 'h') == NULL)
354 pwd->pw_passwd = pw_password(cnf, args, pwd->pw_name);
355
356 } else {
357 if (a_name == NULL) /* Required */
358 cmderr(EX_DATAERR, "login name required\n");
359 else if ((pwd = getpwnam(a_name->val)) != NULL) /* Exists */
360 cmderr(EX_DATAERR, "login name `%s' already exists\n", a_name->val);
361
362 /*
363 * Now, set up defaults for a new user
364 */
365 pwd = &fakeuser;
366 pwd->pw_name = a_name->val;
367 pwd->pw_class = cnf->default_class ? cnf->default_class : "";
368 pwd->pw_passwd = pw_password(cnf, args, pwd->pw_name);
369 pwd->pw_uid = pw_uidpolicy(cnf, args);
370 pwd->pw_gid = pw_gidpolicy(cnf, args, pwd->pw_name, (gid_t) pwd->pw_uid);
371 pwd->pw_change = pw_pwdpolicy(cnf, args);
372 pwd->pw_expire = pw_exppolicy(cnf, args);
373 pwd->pw_dir = pw_homepolicy(cnf, args, pwd->pw_name);
374 pwd->pw_shell = pw_shellpolicy(cnf, args, NULL);
375
376 if (pwd->pw_uid == 0 && strcmp(pwd->pw_name, "root") != 0)
377 fprintf(stderr, "WARNING: new account `%s' has a uid of 0 (superuser access!)\n", pwd->pw_name);
378 }
379
380 /*
381 * Shared add/edit code
382 */
383 if ((arg = getarg(args, 'c')) != NULL)
384 pwd->pw_gecos = pw_checkname(arg->val, 1);
385
386 if ((arg = getarg(args, 'h')) != NULL) {
387 if (strcmp(arg->val, "-") == 0)
388 pwd->pw_passwd = "*"; /* No access */
389 else {
390 int fd = atoi(arg->val);
391 int b;
392 int istty = isatty(fd);
393 struct termios t;
394
395 if (istty) {
396 if (tcgetattr(fd, &t) == -1)
397 istty = 0;
398 else {
399 struct termios n = t;
400
401 /* Disable echo */
402 n.c_lflag &= ~(ECHO);
403 tcsetattr(fd, TCSANOW, &n);
404 printf("%sassword for user %s:", (mode == M_UPDATE) ? "New p" : "P", pwd->pw_name);
405 fflush(stdout);
406 }
407 }
408 b = read(fd, line, sizeof(line) - 1);
409 if (istty) { /* Restore state */
410 tcsetattr(fd, TCSANOW, &t);
411 fputc('\n', stdout);
412 fflush(stdout);
413 }
414 if (b < 0) {
415 perror("-h file descriptor");
416 return EX_IOERR;
417 }
418 line[b] = '\0';
419 if ((p = strpbrk(line, " \t\r\n")) != NULL)
420 *p = '\0';
421 if (!*line)
422 cmderr(EX_DATAERR, "empty password read on file descriptor %d\n", fd);
423 pwd->pw_passwd = pw_pwcrypt(line);
424 }
425 }
426
427 /*
428 * Special case: -N only displays & exits
429 */
430 if (getarg(args, 'N') != NULL)
431 return print_user(pwd, getarg(args, 'P') != NULL);
432
433 if ((mode == M_ADD && !addpwent(pwd)) ||
434 (mode == M_UPDATE && !chgpwent(a_name->val, pwd))) {
435 perror("password update");
436 return EX_IOERR;
437 }
438 /*
439 * Ok, user is created or changed - now edit group file
440 */
441
442 if (mode == M_ADD || getarg(args, 'G') != NULL)
443 editgroups(pwd->pw_name, cnf->groups);
444
445 /* pwd may have been invalidated */
446 if ((pwd = getpwnam(a_name->val)) == NULL)
447 cmderr(EX_NOUSER, "user '%s' disappeared during update\n", a_name->val);
448
449 grp = getgrgid(pwd->pw_gid);
450 pw_log(cnf, mode, W_USER, "%s(%ld):%s(%d):%s:%s:%s",
451 pwd->pw_name, (long) pwd->pw_uid,
452 grp ? grp->gr_name : "unknown", (long) (grp ? grp->gr_gid : -1),
453 pwd->pw_gecos, pwd->pw_dir, pwd->pw_shell);
454
455 /*
456 * If adding, let's touch and chown the user's mail file. This is not
457 * strictly necessary under BSD with a 0755 maildir but it also
458 * doesn't hurt anything to create the empty mailfile
459 */
460 if (mode == M_ADD) {
461 FILE *fp;
462
463 sprintf(line, "%s/%s", _PATH_MAILDIR, pwd->pw_name);
464 close(open(line, O_RDWR | O_CREAT, 0600)); /* Preserve contents &
465 * mtime */
466 chown(line, pwd->pw_uid, pwd->pw_gid);
467
468 /*
469 * Send mail to the new user as well, if we are asked to
470 */
471 if (cnf->newmail && *cnf->newmail && (fp = fopen(cnf->newmail, "r")) != NULL) {
472 FILE *pfp = popen(_PATH_SENDMAIL " -t", "w");
473
474 if (pfp == NULL)
475 perror("sendmail");
476 else {
477 fprintf(pfp, "From: root\n" "To: %s\n" "Subject: Welcome!\n\n", pwd->pw_name);
478 while (fgets(line, sizeof(line), fp) != NULL) {
479 /* Do substitutions? */
480 fputs(line, pfp);
481 }
482 pclose(pfp);
483 pw_log(cnf, mode, W_USER, "%s(%ld) new user mail sent",
484 pwd->pw_name, (long) pwd->pw_uid);
485 }
486 fclose(fp);
487 }
488 }
489 /*
490 * Finally, let's create and populate the user's home directory. Note
491 * that this also `works' for editing users if -m is used, but
492 * existing files will *not* be overwritten.
493 */
494 if (getarg(args, 'm') != NULL && pwd->pw_dir && *pwd->pw_dir == '/' && pwd->pw_dir[1]) {
495 copymkdir(pwd->pw_dir, cnf->dotdir, 0755, pwd->pw_uid, pwd->pw_gid);
496 pw_log(cnf, mode, W_USER, "%s(%ld) home %s made",
497 pwd->pw_name, (long) pwd->pw_uid, pwd->pw_dir);
498 }
499 return EXIT_SUCCESS;
500}
501
502
503static uid_t
504pw_uidpolicy(struct userconf * cnf, struct cargs * args)
505{
506 struct passwd *pwd;
507 uid_t uid = (uid_t) - 1;
508 struct carg *a_uid = getarg(args, 'u');
509
510 /*
511 * Check the given uid, if any
512 */
513 if (a_uid != NULL) {
514 uid = (uid_t) atol(a_uid->val);
515
516 if ((pwd = getpwuid(uid)) != NULL && getarg(args, 'o') == NULL)
517 cmderr(EX_DATAERR, "uid `%ld' has already been allocated\n", (long) pwd->pw_uid);
518 } else {
519 struct bitmap bm;
520
521 /*
522 * We need to allocate the next available uid under one of
523 * two policies a) Grab the first unused uid b) Grab the
524 * highest possible unused uid
525 */
526 if (cnf->min_uid >= cnf->max_uid) { /* Sanity
527 * claus^H^H^H^Hheck */
528 cnf->min_uid = 1000;
529 cnf->max_uid = 32000;
530 }
531 bm = bm_alloc(cnf->max_uid - cnf->min_uid + 1);
532
533 /*
534 * Now, let's fill the bitmap from the password file
535 */
536 setpwent();
537 while ((pwd = getpwent()) != NULL)
538 if (pwd->pw_uid >= (int) cnf->min_uid && pwd->pw_uid <= (int) cnf->max_uid)
539 bm_setbit(&bm, pwd->pw_uid - cnf->min_uid);
540 endpwent();
541
542 /*
543 * Then apply the policy, with fallback to reuse if necessary
544 */
545 if (cnf->reuse_uids || (uid = (uid_t) (bm_lastset(&bm) + cnf->min_uid + 1)) > cnf->max_uid)
546 uid = (uid_t) (bm_firstunset(&bm) + cnf->min_uid);
547
548 /*
549 * Another sanity check
550 */
551 if (uid < cnf->min_uid || uid > cnf->max_uid)
552 cmderr(EX_SOFTWARE, "unable to allocate a new uid - range fully used\n");
553 bm_dealloc(&bm);
554 }
555 return uid;
556}
557
558
559static uid_t
560pw_gidpolicy(struct userconf * cnf, struct cargs * args, char *nam, gid_t prefer)
561{
562 struct group *grp;
563 gid_t gid = (uid_t) - 1;
564 struct carg *a_gid = getarg(args, 'g');
565
566 /*
567 * If no arg given, see if default can help out
568 */
569 if (a_gid == NULL && cnf->default_group && *cnf->default_group)
570 a_gid = addarg(args, 'g', cnf->default_group);
571
572 /*
573 * Check the given gid, if any
574 */
575 setgrent();
576 if (a_gid != NULL) {
577 if ((grp = getgrnam(a_gid->val)) == NULL) {
578 gid = (gid_t) atol(a_gid->val);
579 if ((gid == 0 && !isdigit(*a_gid->val)) || (grp = getgrgid(gid)) == NULL)
580 cmderr(EX_NOUSER, "group `%s' is not defined\n", a_gid->val);
581 }
582 gid = grp->gr_gid;
583 } else if ((grp = getgrnam(nam)) != NULL && grp->gr_mem[0] == NULL) {
584 gid = grp->gr_gid; /* Already created? Use it anyway... */
585 } else {
586 struct cargs grpargs;
587 char tmp[32];
588
589 LIST_INIT(&grpargs);
590 addarg(&grpargs, 'n', nam);
591
592 /*
593 * We need to auto-create a group with the user's name. We
594 * can send all the appropriate output to our sister routine
595 * bit first see if we can create a group with gid==uid so we
596 * can keep the user and group ids in sync. We purposely do
597 * NOT check the gid range if we can force the sync. If the
598 * user's name dups an existing group, then the group add
599 * function will happily handle that case for us and exit.
600 */
601 if (getgrgid(prefer) == NULL) {
602 sprintf(tmp, "%lu", (unsigned long) prefer);
603 addarg(&grpargs, 'g', tmp);
604 }
605 if (getarg(args, 'N'))
606 {
607 addarg(&grpargs, 'N', NULL);
608 addarg(&grpargs, 'q', NULL);
609 gid = pw_group(cnf, M_NEXT, &grpargs);
610 }
611 else
612 {
613 pw_group(cnf, M_ADD, &grpargs);
614 if ((grp = getgrnam(nam)) != NULL)
615 gid = grp->gr_gid;
616 }
617 a_gid = grpargs.lh_first;
618 while (a_gid != NULL) {
619 struct carg *t = a_gid->list.le_next;
620 LIST_REMOVE(a_gid, list);
621 a_gid = t;
622 }
623 }
624 endgrent();
625 return gid;
626}
627
628
629static time_t
630pw_pwdpolicy(struct userconf * cnf, struct cargs * args)
631{
632 time_t result = 0;
633 time_t now = time(NULL);
634 struct carg *arg = getarg(args, 'e');
635
636 if (arg != NULL) {
637 if ((result = parse_date(now, arg->val)) == now)
638 cmderr(EX_DATAERR, "invalid date/time `%s'\n", arg->val);
639 } else if (cnf->password_days > 0)
640 result = now + ((long) cnf->password_days * 86400L);
641 return result;
642}
643
644
645static time_t
646pw_exppolicy(struct userconf * cnf, struct cargs * args)
647{
648 time_t result = 0;
649 time_t now = time(NULL);
650 struct carg *arg = getarg(args, 'e');
651
652 if (arg != NULL) {
653 if ((result = parse_date(now, arg->val)) == now)
654 cmderr(EX_DATAERR, "invalid date/time `%s'\n", arg->val);
655 } else if (cnf->expire_days > 0)
656 result = now + ((long) cnf->expire_days * 86400L);
657 return result;
658}
659
660
661static char *
662pw_homepolicy(struct userconf * cnf, struct cargs * args, char const * user)
663{
664 struct carg *arg = getarg(args, 'd');
665
666 if (arg)
667 return arg->val;
668 else {
669 static char home[128];
670
671 if (cnf->home == NULL || *cnf->home == '\0')
672 cmderr(EX_CONFIG, "no base home directory set\n");
673 sprintf(home, "%s/%s", cnf->home, user);
674 return home;
675 }
676}
677
678static char *
679shell_path(char const * path, char *shells[], char *sh)
680{
681 if (sh != NULL && (*sh == '/' || *sh == '\0'))
682 return sh; /* specified full path or forced none */
683 else {
684 char *p;
685 char paths[_UC_MAXLINE];
686
687 /*
688 * We need to search paths
689 */
690 strncpy(paths, path, sizeof paths);
691 paths[sizeof paths - 1] = '\0';
692 for (p = strtok(paths, ": \t\r\n"); p != NULL; p = strtok(NULL, ": \t\r\n")) {
693 int i;
694 static char shellpath[256];
695
696 if (sh != NULL) {
697 sprintf(shellpath, "%s/%s", p, sh);
698 if (access(shellpath, X_OK) == 0)
699 return shellpath;
700 } else
701 for (i = 0; i < _UC_MAXSHELLS && shells[i] != NULL; i++) {
702 sprintf(shellpath, "%s/%s", p, shells[i]);
703 if (access(shellpath, X_OK) == 0)
704 return shellpath;
705 }
706 }
707 if (sh == NULL)
708 cmderr(EX_OSFILE, "can't find shell `%s' in shell paths\n", sh);
709 cmderr(EX_CONFIG, "no default shell available or defined\n");
710 return NULL;
711 }
712}
713
714
715static char *
716pw_shellpolicy(struct userconf * cnf, struct cargs * args, char *newshell)
717{
718 char *sh = newshell;
719 struct carg *arg = getarg(args, 's');
720
721 if (newshell == NULL && arg != NULL)
722 sh = arg->val;
723 return shell_path(cnf->shelldir, cnf->shells, sh ? sh : cnf->shell_default);
724}
725
726static char const chars[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.";
727
728char *
729pw_pwcrypt(char *password)
730{
731 int i;
732 char salt[12];
733
734 static char buf[256];
735
736 /*
737 * Calculate a salt value
738 */
739 srandom((unsigned) (time(NULL) | getpid()));
740 for (i = 0; i < 8; i++)
741 salt[i] = chars[random() % 63];
742 salt[i] = '\0';
743
744 return strcpy(buf, crypt(password, salt));
745}
746
747
748static char *
749pw_password(struct userconf * cnf, struct cargs * args, char const * user)
750{
751 int i, l;
752 char pwbuf[32];
753
754 switch (cnf->default_password) {
755 case -1: /* Random password */
756 srandom((unsigned) (time(NULL) | getpid()));
757 l = (random() % 8 + 8); /* 8 - 16 chars */
758 for (i = 0; i < l; i++)
759 pwbuf[i] = chars[random() % sizeof(chars)];
760 pwbuf[i] = '\0';
761
762 /*
763 * We give this information back to the user
764 */
765 if (getarg(args, 'h') == NULL && getarg(args, 'N') == NULL) {
766 if (isatty(0))
767 printf("Password is: ");
768 printf("%s\n", pwbuf);
769 fflush(stdout);
770 }
771 break;
772
773 case -2: /* No password at all! */
774 return "";
775
776 case 0: /* No login - default */
777 default:
778 return "*";
779
780 case 1: /* user's name */
781 strncpy(pwbuf, user, sizeof pwbuf);
782 pwbuf[sizeof pwbuf - 1] = '\0';
783 break;
784 }
785 return pw_pwcrypt(pwbuf);
786}
787
788
789static int
790print_user(struct passwd * pwd, int pretty)
791{
792 if (!pretty) {
793 char buf[_UC_MAXLINE];
794
795 fmtpwent(buf, pwd);
796 fputs(buf, stdout);
797 } else {
798 int j;
799 char *p;
800 struct group *grp = getgrgid(pwd->pw_gid);
801 char uname[60] = "User &", office[60] = "[None]",
802 wphone[60] = "[None]", hphone[60] = "[None]";
803
804 if ((p = strtok(pwd->pw_gecos, ",")) != NULL) {
805 strncpy(uname, p, sizeof uname);
806 uname[sizeof uname - 1] = '\0';
807 if ((p = strtok(NULL, ",")) != NULL) {
808 strncpy(office, p, sizeof office);
809 office[sizeof office - 1] = '\0';
810 if ((p = strtok(NULL, ",")) != NULL) {
811 strncpy(wphone, p, sizeof wphone);
812 wphone[sizeof wphone - 1] = '\0';
813 if ((p = strtok(NULL, "")) != NULL) {
814 strncpy(hphone, p, sizeof hphone);
815 hphone[sizeof hphone - 1] = '\0';
816 }
817 }
818 }
819 }
820 /*
821 * Handle '&' in gecos field
822 */
823 if ((p = strchr(uname, '&')) != NULL) {
824 int l = strlen(pwd->pw_name);
825 int m = strlen(p);
826
827 memmove(p + l, p + 1, m);
828 memmove(p, pwd->pw_name, l);
829 *p = (char) toupper(*p);
830 }
831 printf("Login Name : %-10s #%-22ld Group : %-10s #%ld\n"
832 " Full Name : %s\n"
833 " Home : %-32.32s Class : %s\n"
834 " Shell : %-32.32s Office : %s\n"
835 "Work Phone : %-32.32s Home Phone : %s\n",
836
837 pwd->pw_name, (long) pwd->pw_uid,
838 grp ? grp->gr_name : "(invalid)", (long) pwd->pw_gid,
839 uname, pwd->pw_dir, pwd->pw_class,
840 pwd->pw_shell, office, wphone, hphone);
841 setgrent();
842 j = 0;
843 while ((grp=getgrent()) != NULL)
844 {
845 int i = 0;
846 while (i < _UC_MAXGROUPS && grp->gr_mem[i] != NULL)
847 {
848 if (strcmp(grp->gr_mem[i], pwd->pw_name)==0)
849 {
850 printf(j++ == 0 ? " Groups : %s" : ",%s", grp->gr_name);
851 break;
852 }
853 ++i;
854 }
855 }
856 endgrent();
857 printf("%s\n", j ? "\n" : "");
858 }
859 return EXIT_SUCCESS;
860}
861
862static char *
863pw_checkname(char *name, int gecos)
864{
865 int l = 0;
866 char const *notch = gecos ? ":" : " :+-&#%$^()!@~*?<>=|\\/\"";
867
868 while (name[l]) {
869 if (strchr(notch, name[l]) != NULL || name[l] < ' ')
870 cmderr(EX_DATAERR, "invalid character `%c' in field\n", name[l]);
871 ++l;
872 }
873 if (!gecos && l > 8)
874 cmderr(EX_DATAERR, "name too long `%s'\n", name);
875 return name;
876}
877
878
879static void
880rmat(uid_t uid)
881{
882 DIR *d = opendir("/var/at/jobs");
883
884 if (d != NULL) {
885 struct dirent *e;
886
887 while ((e = readdir(d)) != NULL) {
888 struct stat st;
889
890 if (strncmp(e->d_name, ".lock", 5) != 0 &&
891 stat(e->d_name, &st) == 0 &&
892 !S_ISDIR(st.st_mode) &&
893 st.st_uid == uid) {
894 char tmp[MAXPATHLEN];
895
896 sprintf(tmp, "/usr/bin/atrm %s", e->d_name);
897 system(tmp);
898 }
899 }
900 closedir(d);
901 }
902}
27 */
28
29#include <unistd.h>
30#include <fcntl.h>
31#include <ctype.h>
32#include <paths.h>
33#include <sys/param.h>
34#include <dirent.h>
35#include <termios.h>
36#include "pw.h"
37#include "bitmap.h"
38#include "pwupd.h"
39
40static int print_user(struct passwd * pwd, int pretty);
41static uid_t pw_uidpolicy(struct userconf * cnf, struct cargs * args);
42static uid_t pw_gidpolicy(struct userconf * cnf, struct cargs * args, char *nam, gid_t prefer);
43static time_t pw_pwdpolicy(struct userconf * cnf, struct cargs * args);
44static time_t pw_exppolicy(struct userconf * cnf, struct cargs * args);
45static char *pw_homepolicy(struct userconf * cnf, struct cargs * args, char const * user);
46static char *pw_shellpolicy(struct userconf * cnf, struct cargs * args, char *newshell);
47static char *pw_password(struct userconf * cnf, struct cargs * args, char const * user);
48static char *pw_checkname(char *name, int gecos);
49static char *shell_path(char const * path, char *shells[], char *sh);
50static void rmat(uid_t uid);
51
52/*-
53 * -C config configuration file
54 * -q quiet operation
55 * -n name login name
56 * -u uid user id
57 * -c comment user name/comment
58 * -d directory home directory
59 * -e date account expiry date
60 * -p date password expiry date
61 * -g grp primary group
62 * -G grp1,grp2 additional groups
63 * -m [ -k dir ] create and set up home
64 * -s shell name of login shell
65 * -o duplicate uid ok
66 * -L class user class
67 * -l name new login name
68 * -h fd password filehandle
69 * -F force print or add
70 * Setting defaults:
71 * -D set user defaults
72 * -b dir default home root dir
73 * -e period default expiry period
74 * -p period default password change period
75 * -g group default group
76 * -G grp1,grp2.. default additional groups
77 * -L class default login class
78 * -k dir default home skeleton
79 * -s shell default shell
80 * -w method default password method
81 */
82
83int
84pw_user(struct userconf * cnf, int mode, struct cargs * args)
85{
86 char *p = NULL;
87 struct carg *a_name;
88 struct carg *a_uid;
89 struct carg *arg;
90 struct passwd *pwd = NULL;
91 struct group *grp;
92 struct stat st;
93 char line[MAXPWLINE];
94
95 static struct passwd fakeuser =
96 {
97 NULL,
98 "*",
99 -1,
100 -1,
101 0,
102 "",
103 "User &",
104 "/bin/sh",
105 0,
106 0
107 };
108
109 /*
110 * With M_NEXT, we only need to return the
111 * next uid to stdout
112 */
113 if (mode == M_NEXT)
114 {
115 uid_t next = pw_uidpolicy(cnf, args);
116 if (getarg(args, 'q'))
117 return next;
118 printf("%ld:", (long)next);
119 pw_group(cnf, mode, args);
120 return EXIT_SUCCESS;
121 }
122
123 /*
124 * We can do all of the common legwork here
125 */
126
127 if ((arg = getarg(args, 'b')) != NULL) {
128 cnf->home = arg->val;
129 if (stat(cnf->home, &st) == -1 || !S_ISDIR(st.st_mode))
130 cmderr(EX_OSFILE, "root home `%s' is not a directory or does not exist\n", cnf->home);
131 }
132 if ((arg = getarg(args, 'e')) != NULL)
133 cnf->expire_days = atoi(arg->val);
134
135 if ((arg = getarg(args, 'p')) != NULL && arg->val)
136 cnf->password_days = atoi(arg->val);
137
138 if ((arg = getarg(args, 'g')) != NULL) {
139 p = arg->val;
140 if ((grp = getgrnam(p)) == NULL) {
141 if (!isdigit(*p) || (grp = getgrgid((gid_t) atoi(p))) == NULL)
142 cmderr(EX_NOUSER, "group `%s' does not exist\n", p);
143 }
144 cnf->default_group = newstr(grp->gr_name);
145 }
146 if ((arg = getarg(args, 'L')) != NULL)
147 cnf->default_class = pw_checkname(arg->val, 0);
148
149 if ((arg = getarg(args, 'G')) != NULL && arg->val) {
150 int i = 0;
151
152 for (p = strtok(arg->val, ", \t"); i < _UC_MAXGROUPS && p != NULL; p = strtok(NULL, ", \t")) {
153 if ((grp = getgrnam(p)) == NULL) {
154 if (!isdigit(*p) || (grp = getgrgid((gid_t) atoi(p))) == NULL)
155 cmderr(EX_NOUSER, "group `%s' does not exist\n", p);
156 }
157 cnf->groups[i++] = newstr(grp->gr_name);
158 }
159 while (i < _UC_MAXGROUPS)
160 cnf->groups[i++] = NULL;
161 }
162 if ((arg = getarg(args, 'k')) != NULL) {
163 if (stat(cnf->dotdir = arg->val, &st) == -1 || S_ISDIR(st.st_mode))
164 cmderr(EX_OSFILE, "skeleton `%s' is not a directory or does not exist\n", cnf->dotdir);
165 }
166 if ((arg = getarg(args, 's')) != NULL)
167 cnf->shell_default = arg->val;
168
169 if (mode == M_ADD && getarg(args, 'D')) {
170 if (getarg(args, 'n') != NULL)
171 cmderr(EX_DATAERR, "can't combine `-D' with `-n name'\n");
172 if ((arg = getarg(args, 'u')) != NULL && (p = strtok(arg->val, ", \t")) != NULL) {
173 if ((cnf->min_uid = (uid_t) atoi(p)) == 0)
174 cnf->min_uid = 1000;
175 if ((p = strtok(NULL, " ,\t")) == NULL || (cnf->max_uid = (uid_t) atoi(p)) < cnf->min_uid)
176 cnf->max_uid = 32000;
177 }
178 if ((arg = getarg(args, 'i')) != NULL && (p = strtok(arg->val, ", \t")) != NULL) {
179 if ((cnf->min_gid = (gid_t) atoi(p)) == 0)
180 cnf->min_gid = 1000;
181 if ((p = strtok(NULL, " ,\t")) == NULL || (cnf->max_gid = (gid_t) atoi(p)) < cnf->min_gid)
182 cnf->max_gid = 32000;
183 }
184 if ((arg = getarg(args, 'w')) != NULL)
185 cnf->default_password = boolean_val(arg->val, cnf->default_password);
186
187 arg = getarg(args, 'C');
188 if (write_userconfig(arg ? arg->val : NULL))
189 return EXIT_SUCCESS;
190 perror("config update");
191 return EX_IOERR;
192 }
193 if (mode == M_PRINT && getarg(args, 'a')) {
194 int pretty = getarg(args, 'P') != NULL;
195
196 setpwent();
197 while ((pwd = getpwent()) != NULL)
198 print_user(pwd, pretty);
199 endpwent();
200 return EXIT_SUCCESS;
201 }
202 if ((a_name = getarg(args, 'n')) != NULL)
203 pwd = getpwnam(pw_checkname(a_name->val, 0));
204 a_uid = getarg(args, 'u');
205
206 if (a_uid == NULL) {
207 if (a_name == NULL)
208 cmderr(EX_DATAERR, "user name or id required\n");
209
210 /*
211 * Determine whether 'n' switch is name or uid - we don't
212 * really don't really care which we have, but we need to
213 * know.
214 */
215 if (mode != M_ADD && pwd == NULL && isdigit(*a_name->val) && atoi(a_name->val) > 0) { /* Assume uid */
216 (a_uid = a_name)->ch = 'u';
217 a_name = NULL;
218 }
219 }
220 /*
221 * Update, delete & print require that the user exists
222 */
223 if (mode == M_UPDATE || mode == M_DELETE || mode == M_PRINT) {
224 if (a_name == NULL && pwd == NULL) /* Try harder */
225 pwd = getpwuid(atoi(a_uid->val));
226
227 if (pwd == NULL) {
228 if (mode == M_PRINT && getarg(args, 'F')) {
229 fakeuser.pw_name = a_name ? a_name->val : "nouser";
230 fakeuser.pw_uid = a_uid ? (uid_t) atol(a_uid->val) : -1;
231 return print_user(&fakeuser, getarg(args, 'P') != NULL);
232 }
233 if (a_name == NULL)
234 cmderr(EX_NOUSER, "no such uid `%s'\n", a_uid->val);
235 cmderr(EX_NOUSER, "no such user `%s'\n", a_name->val);
236 }
237 if (a_name == NULL) /* May be needed later */
238 a_name = addarg(args, 'n', newstr(pwd->pw_name));
239
240 /*
241 * Handle deletions now
242 */
243 if (mode == M_DELETE) {
244 char file[MAXPATHLEN];
245 char home[MAXPATHLEN];
246 uid_t uid = pwd->pw_uid;
247
248 if (strcmp(pwd->pw_name, "root") == 0)
249 cmderr(EX_DATAERR, "cannot remove user 'root'\n");
250
251 /*
252 * Remove crontabs
253 */
254 sprintf(file, "/var/cron/tabs/%s", pwd->pw_name);
255 if (access(file, F_OK) == 0) {
256 sprintf(file, "crontab -u %s -r", pwd->pw_name);
257 system(file);
258 }
259 /*
260 * Save these for later, since contents of pwd may be
261 * invalidated by deletion
262 */
263 sprintf(file, "%s/%s", _PATH_MAILDIR, pwd->pw_name);
264 strncpy(home, pwd->pw_dir, sizeof home);
265 home[sizeof home - 1] = '\0';
266
267 if (!delpwent(pwd))
268 cmderr(EX_IOERR, "Error updating passwd file: %s\n", strerror(errno));
269 editgroups(a_name->val, NULL);
270
271 pw_log(cnf, mode, W_USER, "%s(%ld) account removed", a_name->val, (long) uid);
272
273 /*
274 * Remove mail file
275 */
276 remove(file);
277
278 /*
279 * Remove at jobs
280 */
281 if (getpwuid(uid) == NULL)
282 rmat(uid);
283
284 /*
285 * Remove home directory and contents
286 */
287 if (getarg(args, 'r') != NULL && *home == '/' && getpwuid(uid) == NULL) {
288 if (stat(home, &st) != -1) {
289 rm_r(home, uid);
290 pw_log(cnf, mode, W_USER, "%s(%ld) home '%s' %sremoved",
291 a_name->val, (long) uid, home,
292 stat(home, &st) == -1 ? "" : "not completely ");
293 }
294 }
295 return EXIT_SUCCESS;
296 } else if (mode == M_PRINT)
297 return print_user(pwd, getarg(args, 'P') != NULL);
298
299 /*
300 * The rest is edit code
301 */
302 if ((arg = getarg(args, 'l')) != NULL) {
303 if (strcmp(pwd->pw_name, "root") == 0)
304 cmderr(EX_DATAERR, "can't rename `root' account\n");
305 pwd->pw_name = pw_checkname(arg->val, 0);
306 }
307 if ((arg = getarg(args, 'u')) != NULL && isdigit(*arg->val)) {
308 pwd->pw_uid = (uid_t) atol(arg->val);
309 if (pwd->pw_uid != 0 && strcmp(pwd->pw_name, "root") == 0)
310 cmderr(EX_DATAERR, "can't change uid of `root' account\n");
311 if (pwd->pw_uid == 0 && strcmp(pwd->pw_name, "root") != 0)
312 fprintf(stderr, "WARNING: account `%s' will have a uid of 0 (superuser access!)\n", pwd->pw_name);
313 }
314 if ((arg = getarg(args, 'g')) != NULL && pwd->pw_uid != 0) /* Already checked this */
315 pwd->pw_gid = (gid_t) getgrnam(cnf->default_group)->gr_gid;
316
317 if ((arg = getarg(args, 'p')) != NULL) {
318 if (*arg->val == '\0' || strcmp(arg->val, "0") == 0)
319 pwd->pw_change = 0;
320 else {
321 time_t now = time(NULL);
322 time_t expire = parse_date(now, arg->val);
323
324 if (now == expire)
325 cmderr(EX_DATAERR, "Invalid password change date `%s'\n", arg->val);
326 pwd->pw_change = expire;
327 }
328 }
329 if ((arg = getarg(args, 'e')) != NULL) {
330 if (*arg->val == '\0' || strcmp(arg->val, "0") == 0)
331 pwd->pw_expire = 0;
332 else {
333 time_t now = time(NULL);
334 time_t expire = parse_date(now, arg->val);
335
336 if (now == expire)
337 cmderr(EX_DATAERR, "Invalid account expiry date `%s'\n", arg->val);
338 pwd->pw_expire = expire;
339 }
340 }
341 if ((arg = getarg(args, 's')) != NULL)
342 pwd->pw_shell = shell_path(cnf->shelldir, cnf->shells, arg->val);
343
344 if (getarg(args, 'L'))
345 pwd->pw_class = cnf->default_class;
346
347 if ((arg = getarg(args, 'w')) != NULL && getarg(args, 'h') == NULL)
348 pwd->pw_passwd = pw_password(cnf, args, pwd->pw_name);
349
350 } else {
351 if (a_name == NULL) /* Required */
352 cmderr(EX_DATAERR, "login name required\n");
353 else if ((pwd = getpwnam(a_name->val)) != NULL) /* Exists */
354 cmderr(EX_DATAERR, "login name `%s' already exists\n", a_name->val);
355
356 /*
357 * Now, set up defaults for a new user
358 */
359 pwd = &fakeuser;
360 pwd->pw_name = a_name->val;
361 pwd->pw_class = cnf->default_class ? cnf->default_class : "";
362 pwd->pw_passwd = pw_password(cnf, args, pwd->pw_name);
363 pwd->pw_uid = pw_uidpolicy(cnf, args);
364 pwd->pw_gid = pw_gidpolicy(cnf, args, pwd->pw_name, (gid_t) pwd->pw_uid);
365 pwd->pw_change = pw_pwdpolicy(cnf, args);
366 pwd->pw_expire = pw_exppolicy(cnf, args);
367 pwd->pw_dir = pw_homepolicy(cnf, args, pwd->pw_name);
368 pwd->pw_shell = pw_shellpolicy(cnf, args, NULL);
369
370 if (pwd->pw_uid == 0 && strcmp(pwd->pw_name, "root") != 0)
371 fprintf(stderr, "WARNING: new account `%s' has a uid of 0 (superuser access!)\n", pwd->pw_name);
372 }
373
374 /*
375 * Shared add/edit code
376 */
377 if ((arg = getarg(args, 'c')) != NULL)
378 pwd->pw_gecos = pw_checkname(arg->val, 1);
379
380 if ((arg = getarg(args, 'h')) != NULL) {
381 if (strcmp(arg->val, "-") == 0)
382 pwd->pw_passwd = "*"; /* No access */
383 else {
384 int fd = atoi(arg->val);
385 int b;
386 int istty = isatty(fd);
387 struct termios t;
388
389 if (istty) {
390 if (tcgetattr(fd, &t) == -1)
391 istty = 0;
392 else {
393 struct termios n = t;
394
395 /* Disable echo */
396 n.c_lflag &= ~(ECHO);
397 tcsetattr(fd, TCSANOW, &n);
398 printf("%sassword for user %s:", (mode == M_UPDATE) ? "New p" : "P", pwd->pw_name);
399 fflush(stdout);
400 }
401 }
402 b = read(fd, line, sizeof(line) - 1);
403 if (istty) { /* Restore state */
404 tcsetattr(fd, TCSANOW, &t);
405 fputc('\n', stdout);
406 fflush(stdout);
407 }
408 if (b < 0) {
409 perror("-h file descriptor");
410 return EX_IOERR;
411 }
412 line[b] = '\0';
413 if ((p = strpbrk(line, " \t\r\n")) != NULL)
414 *p = '\0';
415 if (!*line)
416 cmderr(EX_DATAERR, "empty password read on file descriptor %d\n", fd);
417 pwd->pw_passwd = pw_pwcrypt(line);
418 }
419 }
420
421 /*
422 * Special case: -N only displays & exits
423 */
424 if (getarg(args, 'N') != NULL)
425 return print_user(pwd, getarg(args, 'P') != NULL);
426
427 if ((mode == M_ADD && !addpwent(pwd)) ||
428 (mode == M_UPDATE && !chgpwent(a_name->val, pwd))) {
429 perror("password update");
430 return EX_IOERR;
431 }
432 /*
433 * Ok, user is created or changed - now edit group file
434 */
435
436 if (mode == M_ADD || getarg(args, 'G') != NULL)
437 editgroups(pwd->pw_name, cnf->groups);
438
439 /* pwd may have been invalidated */
440 if ((pwd = getpwnam(a_name->val)) == NULL)
441 cmderr(EX_NOUSER, "user '%s' disappeared during update\n", a_name->val);
442
443 grp = getgrgid(pwd->pw_gid);
444 pw_log(cnf, mode, W_USER, "%s(%ld):%s(%d):%s:%s:%s",
445 pwd->pw_name, (long) pwd->pw_uid,
446 grp ? grp->gr_name : "unknown", (long) (grp ? grp->gr_gid : -1),
447 pwd->pw_gecos, pwd->pw_dir, pwd->pw_shell);
448
449 /*
450 * If adding, let's touch and chown the user's mail file. This is not
451 * strictly necessary under BSD with a 0755 maildir but it also
452 * doesn't hurt anything to create the empty mailfile
453 */
454 if (mode == M_ADD) {
455 FILE *fp;
456
457 sprintf(line, "%s/%s", _PATH_MAILDIR, pwd->pw_name);
458 close(open(line, O_RDWR | O_CREAT, 0600)); /* Preserve contents &
459 * mtime */
460 chown(line, pwd->pw_uid, pwd->pw_gid);
461
462 /*
463 * Send mail to the new user as well, if we are asked to
464 */
465 if (cnf->newmail && *cnf->newmail && (fp = fopen(cnf->newmail, "r")) != NULL) {
466 FILE *pfp = popen(_PATH_SENDMAIL " -t", "w");
467
468 if (pfp == NULL)
469 perror("sendmail");
470 else {
471 fprintf(pfp, "From: root\n" "To: %s\n" "Subject: Welcome!\n\n", pwd->pw_name);
472 while (fgets(line, sizeof(line), fp) != NULL) {
473 /* Do substitutions? */
474 fputs(line, pfp);
475 }
476 pclose(pfp);
477 pw_log(cnf, mode, W_USER, "%s(%ld) new user mail sent",
478 pwd->pw_name, (long) pwd->pw_uid);
479 }
480 fclose(fp);
481 }
482 }
483 /*
484 * Finally, let's create and populate the user's home directory. Note
485 * that this also `works' for editing users if -m is used, but
486 * existing files will *not* be overwritten.
487 */
488 if (getarg(args, 'm') != NULL && pwd->pw_dir && *pwd->pw_dir == '/' && pwd->pw_dir[1]) {
489 copymkdir(pwd->pw_dir, cnf->dotdir, 0755, pwd->pw_uid, pwd->pw_gid);
490 pw_log(cnf, mode, W_USER, "%s(%ld) home %s made",
491 pwd->pw_name, (long) pwd->pw_uid, pwd->pw_dir);
492 }
493 return EXIT_SUCCESS;
494}
495
496
497static uid_t
498pw_uidpolicy(struct userconf * cnf, struct cargs * args)
499{
500 struct passwd *pwd;
501 uid_t uid = (uid_t) - 1;
502 struct carg *a_uid = getarg(args, 'u');
503
504 /*
505 * Check the given uid, if any
506 */
507 if (a_uid != NULL) {
508 uid = (uid_t) atol(a_uid->val);
509
510 if ((pwd = getpwuid(uid)) != NULL && getarg(args, 'o') == NULL)
511 cmderr(EX_DATAERR, "uid `%ld' has already been allocated\n", (long) pwd->pw_uid);
512 } else {
513 struct bitmap bm;
514
515 /*
516 * We need to allocate the next available uid under one of
517 * two policies a) Grab the first unused uid b) Grab the
518 * highest possible unused uid
519 */
520 if (cnf->min_uid >= cnf->max_uid) { /* Sanity
521 * claus^H^H^H^Hheck */
522 cnf->min_uid = 1000;
523 cnf->max_uid = 32000;
524 }
525 bm = bm_alloc(cnf->max_uid - cnf->min_uid + 1);
526
527 /*
528 * Now, let's fill the bitmap from the password file
529 */
530 setpwent();
531 while ((pwd = getpwent()) != NULL)
532 if (pwd->pw_uid >= (int) cnf->min_uid && pwd->pw_uid <= (int) cnf->max_uid)
533 bm_setbit(&bm, pwd->pw_uid - cnf->min_uid);
534 endpwent();
535
536 /*
537 * Then apply the policy, with fallback to reuse if necessary
538 */
539 if (cnf->reuse_uids || (uid = (uid_t) (bm_lastset(&bm) + cnf->min_uid + 1)) > cnf->max_uid)
540 uid = (uid_t) (bm_firstunset(&bm) + cnf->min_uid);
541
542 /*
543 * Another sanity check
544 */
545 if (uid < cnf->min_uid || uid > cnf->max_uid)
546 cmderr(EX_SOFTWARE, "unable to allocate a new uid - range fully used\n");
547 bm_dealloc(&bm);
548 }
549 return uid;
550}
551
552
553static uid_t
554pw_gidpolicy(struct userconf * cnf, struct cargs * args, char *nam, gid_t prefer)
555{
556 struct group *grp;
557 gid_t gid = (uid_t) - 1;
558 struct carg *a_gid = getarg(args, 'g');
559
560 /*
561 * If no arg given, see if default can help out
562 */
563 if (a_gid == NULL && cnf->default_group && *cnf->default_group)
564 a_gid = addarg(args, 'g', cnf->default_group);
565
566 /*
567 * Check the given gid, if any
568 */
569 setgrent();
570 if (a_gid != NULL) {
571 if ((grp = getgrnam(a_gid->val)) == NULL) {
572 gid = (gid_t) atol(a_gid->val);
573 if ((gid == 0 && !isdigit(*a_gid->val)) || (grp = getgrgid(gid)) == NULL)
574 cmderr(EX_NOUSER, "group `%s' is not defined\n", a_gid->val);
575 }
576 gid = grp->gr_gid;
577 } else if ((grp = getgrnam(nam)) != NULL && grp->gr_mem[0] == NULL) {
578 gid = grp->gr_gid; /* Already created? Use it anyway... */
579 } else {
580 struct cargs grpargs;
581 char tmp[32];
582
583 LIST_INIT(&grpargs);
584 addarg(&grpargs, 'n', nam);
585
586 /*
587 * We need to auto-create a group with the user's name. We
588 * can send all the appropriate output to our sister routine
589 * bit first see if we can create a group with gid==uid so we
590 * can keep the user and group ids in sync. We purposely do
591 * NOT check the gid range if we can force the sync. If the
592 * user's name dups an existing group, then the group add
593 * function will happily handle that case for us and exit.
594 */
595 if (getgrgid(prefer) == NULL) {
596 sprintf(tmp, "%lu", (unsigned long) prefer);
597 addarg(&grpargs, 'g', tmp);
598 }
599 if (getarg(args, 'N'))
600 {
601 addarg(&grpargs, 'N', NULL);
602 addarg(&grpargs, 'q', NULL);
603 gid = pw_group(cnf, M_NEXT, &grpargs);
604 }
605 else
606 {
607 pw_group(cnf, M_ADD, &grpargs);
608 if ((grp = getgrnam(nam)) != NULL)
609 gid = grp->gr_gid;
610 }
611 a_gid = grpargs.lh_first;
612 while (a_gid != NULL) {
613 struct carg *t = a_gid->list.le_next;
614 LIST_REMOVE(a_gid, list);
615 a_gid = t;
616 }
617 }
618 endgrent();
619 return gid;
620}
621
622
623static time_t
624pw_pwdpolicy(struct userconf * cnf, struct cargs * args)
625{
626 time_t result = 0;
627 time_t now = time(NULL);
628 struct carg *arg = getarg(args, 'e');
629
630 if (arg != NULL) {
631 if ((result = parse_date(now, arg->val)) == now)
632 cmderr(EX_DATAERR, "invalid date/time `%s'\n", arg->val);
633 } else if (cnf->password_days > 0)
634 result = now + ((long) cnf->password_days * 86400L);
635 return result;
636}
637
638
639static time_t
640pw_exppolicy(struct userconf * cnf, struct cargs * args)
641{
642 time_t result = 0;
643 time_t now = time(NULL);
644 struct carg *arg = getarg(args, 'e');
645
646 if (arg != NULL) {
647 if ((result = parse_date(now, arg->val)) == now)
648 cmderr(EX_DATAERR, "invalid date/time `%s'\n", arg->val);
649 } else if (cnf->expire_days > 0)
650 result = now + ((long) cnf->expire_days * 86400L);
651 return result;
652}
653
654
655static char *
656pw_homepolicy(struct userconf * cnf, struct cargs * args, char const * user)
657{
658 struct carg *arg = getarg(args, 'd');
659
660 if (arg)
661 return arg->val;
662 else {
663 static char home[128];
664
665 if (cnf->home == NULL || *cnf->home == '\0')
666 cmderr(EX_CONFIG, "no base home directory set\n");
667 sprintf(home, "%s/%s", cnf->home, user);
668 return home;
669 }
670}
671
672static char *
673shell_path(char const * path, char *shells[], char *sh)
674{
675 if (sh != NULL && (*sh == '/' || *sh == '\0'))
676 return sh; /* specified full path or forced none */
677 else {
678 char *p;
679 char paths[_UC_MAXLINE];
680
681 /*
682 * We need to search paths
683 */
684 strncpy(paths, path, sizeof paths);
685 paths[sizeof paths - 1] = '\0';
686 for (p = strtok(paths, ": \t\r\n"); p != NULL; p = strtok(NULL, ": \t\r\n")) {
687 int i;
688 static char shellpath[256];
689
690 if (sh != NULL) {
691 sprintf(shellpath, "%s/%s", p, sh);
692 if (access(shellpath, X_OK) == 0)
693 return shellpath;
694 } else
695 for (i = 0; i < _UC_MAXSHELLS && shells[i] != NULL; i++) {
696 sprintf(shellpath, "%s/%s", p, shells[i]);
697 if (access(shellpath, X_OK) == 0)
698 return shellpath;
699 }
700 }
701 if (sh == NULL)
702 cmderr(EX_OSFILE, "can't find shell `%s' in shell paths\n", sh);
703 cmderr(EX_CONFIG, "no default shell available or defined\n");
704 return NULL;
705 }
706}
707
708
709static char *
710pw_shellpolicy(struct userconf * cnf, struct cargs * args, char *newshell)
711{
712 char *sh = newshell;
713 struct carg *arg = getarg(args, 's');
714
715 if (newshell == NULL && arg != NULL)
716 sh = arg->val;
717 return shell_path(cnf->shelldir, cnf->shells, sh ? sh : cnf->shell_default);
718}
719
720static char const chars[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.";
721
722char *
723pw_pwcrypt(char *password)
724{
725 int i;
726 char salt[12];
727
728 static char buf[256];
729
730 /*
731 * Calculate a salt value
732 */
733 srandom((unsigned) (time(NULL) | getpid()));
734 for (i = 0; i < 8; i++)
735 salt[i] = chars[random() % 63];
736 salt[i] = '\0';
737
738 return strcpy(buf, crypt(password, salt));
739}
740
741
742static char *
743pw_password(struct userconf * cnf, struct cargs * args, char const * user)
744{
745 int i, l;
746 char pwbuf[32];
747
748 switch (cnf->default_password) {
749 case -1: /* Random password */
750 srandom((unsigned) (time(NULL) | getpid()));
751 l = (random() % 8 + 8); /* 8 - 16 chars */
752 for (i = 0; i < l; i++)
753 pwbuf[i] = chars[random() % sizeof(chars)];
754 pwbuf[i] = '\0';
755
756 /*
757 * We give this information back to the user
758 */
759 if (getarg(args, 'h') == NULL && getarg(args, 'N') == NULL) {
760 if (isatty(0))
761 printf("Password is: ");
762 printf("%s\n", pwbuf);
763 fflush(stdout);
764 }
765 break;
766
767 case -2: /* No password at all! */
768 return "";
769
770 case 0: /* No login - default */
771 default:
772 return "*";
773
774 case 1: /* user's name */
775 strncpy(pwbuf, user, sizeof pwbuf);
776 pwbuf[sizeof pwbuf - 1] = '\0';
777 break;
778 }
779 return pw_pwcrypt(pwbuf);
780}
781
782
783static int
784print_user(struct passwd * pwd, int pretty)
785{
786 if (!pretty) {
787 char buf[_UC_MAXLINE];
788
789 fmtpwent(buf, pwd);
790 fputs(buf, stdout);
791 } else {
792 int j;
793 char *p;
794 struct group *grp = getgrgid(pwd->pw_gid);
795 char uname[60] = "User &", office[60] = "[None]",
796 wphone[60] = "[None]", hphone[60] = "[None]";
797
798 if ((p = strtok(pwd->pw_gecos, ",")) != NULL) {
799 strncpy(uname, p, sizeof uname);
800 uname[sizeof uname - 1] = '\0';
801 if ((p = strtok(NULL, ",")) != NULL) {
802 strncpy(office, p, sizeof office);
803 office[sizeof office - 1] = '\0';
804 if ((p = strtok(NULL, ",")) != NULL) {
805 strncpy(wphone, p, sizeof wphone);
806 wphone[sizeof wphone - 1] = '\0';
807 if ((p = strtok(NULL, "")) != NULL) {
808 strncpy(hphone, p, sizeof hphone);
809 hphone[sizeof hphone - 1] = '\0';
810 }
811 }
812 }
813 }
814 /*
815 * Handle '&' in gecos field
816 */
817 if ((p = strchr(uname, '&')) != NULL) {
818 int l = strlen(pwd->pw_name);
819 int m = strlen(p);
820
821 memmove(p + l, p + 1, m);
822 memmove(p, pwd->pw_name, l);
823 *p = (char) toupper(*p);
824 }
825 printf("Login Name : %-10s #%-22ld Group : %-10s #%ld\n"
826 " Full Name : %s\n"
827 " Home : %-32.32s Class : %s\n"
828 " Shell : %-32.32s Office : %s\n"
829 "Work Phone : %-32.32s Home Phone : %s\n",
830
831 pwd->pw_name, (long) pwd->pw_uid,
832 grp ? grp->gr_name : "(invalid)", (long) pwd->pw_gid,
833 uname, pwd->pw_dir, pwd->pw_class,
834 pwd->pw_shell, office, wphone, hphone);
835 setgrent();
836 j = 0;
837 while ((grp=getgrent()) != NULL)
838 {
839 int i = 0;
840 while (i < _UC_MAXGROUPS && grp->gr_mem[i] != NULL)
841 {
842 if (strcmp(grp->gr_mem[i], pwd->pw_name)==0)
843 {
844 printf(j++ == 0 ? " Groups : %s" : ",%s", grp->gr_name);
845 break;
846 }
847 ++i;
848 }
849 }
850 endgrent();
851 printf("%s\n", j ? "\n" : "");
852 }
853 return EXIT_SUCCESS;
854}
855
856static char *
857pw_checkname(char *name, int gecos)
858{
859 int l = 0;
860 char const *notch = gecos ? ":" : " :+-&#%$^()!@~*?<>=|\\/\"";
861
862 while (name[l]) {
863 if (strchr(notch, name[l]) != NULL || name[l] < ' ')
864 cmderr(EX_DATAERR, "invalid character `%c' in field\n", name[l]);
865 ++l;
866 }
867 if (!gecos && l > 8)
868 cmderr(EX_DATAERR, "name too long `%s'\n", name);
869 return name;
870}
871
872
873static void
874rmat(uid_t uid)
875{
876 DIR *d = opendir("/var/at/jobs");
877
878 if (d != NULL) {
879 struct dirent *e;
880
881 while ((e = readdir(d)) != NULL) {
882 struct stat st;
883
884 if (strncmp(e->d_name, ".lock", 5) != 0 &&
885 stat(e->d_name, &st) == 0 &&
886 !S_ISDIR(st.st_mode) &&
887 st.st_uid == uid) {
888 char tmp[MAXPATHLEN];
889
890 sprintf(tmp, "/usr/bin/atrm %s", e->d_name);
891 system(tmp);
892 }
893 }
894 closedir(d);
895 }
896}