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