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
29 static const char rcsid[] =
36 #include <sys/param.h>
40 #include <sys/types.h>
42 #include <sys/resource.h>
45 #include <login_cap.h>
49 #if (MAXLOGNAME-1) > UT_NAMESIZE
50 #define LOGNAMESIZE UT_NAMESIZE
52 #define LOGNAMESIZE (MAXLOGNAME-1)
55 static char locked_str[] = "*LOCKED*";
57 static int print_user(struct passwd * pwd, int pretty, int v7);
58 static uid_t pw_uidpolicy(struct userconf * cnf, struct cargs * args);
59 static uid_t pw_gidpolicy(struct userconf * cnf, struct cargs * args, char *nam, gid_t prefer);
60 static time_t pw_pwdpolicy(struct userconf * cnf, struct cargs * args);
61 static time_t pw_exppolicy(struct userconf * cnf, struct cargs * args);
62 static char *pw_homepolicy(struct userconf * cnf, struct cargs * args, char const * user);
63 static char *pw_shellpolicy(struct userconf * cnf, struct cargs * args, char *newshell);
64 static char *pw_password(struct userconf * cnf, struct cargs * args, char const * user);
65 static char *shell_path(char const * path, char *shells[], char *sh);
66 static void rmat(uid_t uid);
67 static void rmopie(char const * name);
70 * -C config configuration file
74 * -c comment user name/comment
75 * -d directory home directory
76 * -e date account expiry date
77 * -p date password expiry date
78 * -g grp primary group
79 * -G grp1,grp2 additional groups
80 * -m [ -k dir ] create and set up home
81 * -s shell name of login shell
84 * -l name new login name
85 * -h fd password filehandle
86 * -H fd encrypted password filehandle
87 * -F force print or add
89 * -D set user defaults
90 * -b dir default home root dir
91 * -e period default expiry period
92 * -p period default password change period
93 * -g group default group
94 * -G grp1,grp2.. default additional groups
95 * -L class default login class
96 * -k dir default home skeleton
97 * -s shell default shell
98 * -w method default password method
102 pw_user(struct userconf * cnf, int mode, struct cargs * args)
110 struct passwd *pwd = NULL;
113 char line[_PASSWORD_LEN+1];
119 static struct passwd fakeuser =
131 #if defined(__FreeBSD__)
138 * With M_NEXT, we only need to return the
143 uid_t next = pw_uidpolicy(cnf, args);
144 if (getarg(args, 'q'))
146 printf("%ld:", (long)next);
147 pw_group(cnf, mode, args);
152 * We can do all of the common legwork here
155 if ((arg = getarg(args, 'b')) != NULL) {
156 cnf->home = arg->val;
159 if ((arg = getarg(args, 'M')) != NULL) {
161 if ((set = setmode(dmode_c)) == NULL)
162 errx(EX_DATAERR, "invalid directory creation mode '%s'",
164 dmode = getmode(set, S_IRWXU | S_IRWXG | S_IRWXO);
166 cnf->homemode = dmode;
170 * If we'll need to use it or we're updating it,
171 * then create the base home directory if necessary
173 if (arg != NULL || getarg(args, 'm') != NULL) {
174 int l = strlen(cnf->home);
176 if (l > 1 && cnf->home[l-1] == '/') /* Shave off any trailing path delimiter */
177 cnf->home[--l] = '\0';
179 if (l < 2 || *cnf->home != '/') /* Check for absolute path name */
180 errx(EX_DATAERR, "invalid base directory for home '%s'", cnf->home);
182 if (stat(cnf->home, &st) == -1) {
183 char dbuf[MAXPATHLEN];
186 * This is a kludge especially for Joerg :)
187 * If the home directory would be created in the root partition, then
188 * we really create it under /usr which is likely to have more space.
189 * But we create a symlink from cnf->home -> "/usr" -> cnf->home
191 if (strchr(cnf->home+1, '/') == NULL) {
192 strcpy(dbuf, "/usr");
193 strncat(dbuf, cnf->home, MAXPATHLEN-5);
194 if (mkdir(dbuf, cnf->homemode) != -1 || errno == EEXIST) {
197 * Skip first "/" and create symlink:
200 symlink(dbuf+1, cnf->home);
202 /* If this falls, fall back to old method */
204 strlcpy(dbuf, cnf->home, sizeof(dbuf));
206 if (stat(dbuf, &st) == -1) {
207 while ((p = strchr(++p, '/')) != NULL) {
209 if (stat(dbuf, &st) == -1) {
210 if (mkdir(dbuf, cnf->homemode) == -1)
213 } else if (!S_ISDIR(st.st_mode))
214 errx(EX_OSFILE, "'%s' (root home parent) is not a directory", dbuf);
218 if (stat(dbuf, &st) == -1) {
219 if (mkdir(dbuf, cnf->homemode) == -1) {
220 direrr: err(EX_OSFILE, "mkdir '%s'", dbuf);
224 } else if (!S_ISDIR(st.st_mode))
225 errx(EX_OSFILE, "root home `%s' is not a directory", cnf->home);
228 if ((arg = getarg(args, 'e')) != NULL)
229 cnf->expire_days = atoi(arg->val);
231 if ((arg = getarg(args, 'y')) != NULL)
232 cnf->nispasswd = arg->val;
234 if ((arg = getarg(args, 'p')) != NULL && arg->val)
235 cnf->password_days = atoi(arg->val);
237 if ((arg = getarg(args, 'g')) != NULL) {
238 if (!*(p = arg->val)) /* Handle empty group list specially */
239 cnf->default_group = "";
241 if ((grp = GETGRNAM(p)) == NULL) {
242 if (!isdigit((unsigned char)*p) || (grp = GETGRGID((gid_t) atoi(p))) == NULL)
243 errx(EX_NOUSER, "group `%s' does not exist", p);
245 cnf->default_group = newstr(grp->gr_name);
248 if ((arg = getarg(args, 'L')) != NULL)
249 cnf->default_class = pw_checkname((u_char *)arg->val, 0);
251 if ((arg = getarg(args, 'G')) != NULL && arg->val) {
254 for (p = strtok(arg->val, ", \t"); p != NULL; p = strtok(NULL, ", \t")) {
255 if ((grp = GETGRNAM(p)) == NULL) {
256 if (!isdigit((unsigned char)*p) || (grp = GETGRGID((gid_t) atoi(p))) == NULL)
257 errx(EX_NOUSER, "group `%s' does not exist", p);
259 if (extendarray(&cnf->groups, &cnf->numgroups, i + 2) != -1)
260 cnf->groups[i++] = newstr(grp->gr_name);
262 while (i < cnf->numgroups)
263 cnf->groups[i++] = NULL;
266 if ((arg = getarg(args, 'k')) != NULL) {
267 if (stat(cnf->dotdir = arg->val, &st) == -1 || !S_ISDIR(st.st_mode))
268 errx(EX_OSFILE, "skeleton `%s' is not a directory or does not exist", cnf->dotdir);
271 if ((arg = getarg(args, 's')) != NULL)
272 cnf->shell_default = arg->val;
274 if ((arg = getarg(args, 'w')) != NULL)
275 cnf->default_password = boolean_val(arg->val, cnf->default_password);
276 if (mode == M_ADD && getarg(args, 'D')) {
277 if (getarg(args, 'n') != NULL)
278 errx(EX_DATAERR, "can't combine `-D' with `-n name'");
279 if ((arg = getarg(args, 'u')) != NULL && (p = strtok(arg->val, ", \t")) != NULL) {
280 if ((cnf->min_uid = (uid_t) atoi(p)) == 0)
282 if ((p = strtok(NULL, " ,\t")) == NULL || (cnf->max_uid = (uid_t) atoi(p)) < cnf->min_uid)
283 cnf->max_uid = 32000;
285 if ((arg = getarg(args, 'i')) != NULL && (p = strtok(arg->val, ", \t")) != NULL) {
286 if ((cnf->min_gid = (gid_t) atoi(p)) == 0)
288 if ((p = strtok(NULL, " ,\t")) == NULL || (cnf->max_gid = (gid_t) atoi(p)) < cnf->min_gid)
289 cnf->max_gid = 32000;
292 arg = getarg(args, 'C');
293 if (write_userconfig(arg ? arg->val : NULL))
295 warn("config update");
299 if (mode == M_PRINT && getarg(args, 'a')) {
300 int pretty = getarg(args, 'P') != NULL;
301 int v7 = getarg(args, '7') != NULL;
304 while ((pwd = GETPWENT()) != NULL)
305 print_user(pwd, pretty, v7);
310 if ((a_name = getarg(args, 'n')) != NULL)
311 pwd = GETPWNAM(pw_checkname((u_char *)a_name->val, 0));
312 a_uid = getarg(args, 'u');
316 errx(EX_DATAERR, "user name or id required");
319 * Determine whether 'n' switch is name or uid - we don't
320 * really don't really care which we have, but we need to
323 if (mode != M_ADD && pwd == NULL
324 && strspn(a_name->val, "0123456789") == strlen(a_name->val)
325 && atoi(a_name->val) > 0) { /* Assume uid */
326 (a_uid = a_name)->ch = 'u';
332 * Update, delete & print require that the user exists
334 if (mode == M_UPDATE || mode == M_DELETE ||
335 mode == M_PRINT || mode == M_LOCK || mode == M_UNLOCK) {
337 if (a_name == NULL && pwd == NULL) /* Try harder */
338 pwd = GETPWUID(atoi(a_uid->val));
341 if (mode == M_PRINT && getarg(args, 'F')) {
342 fakeuser.pw_name = a_name ? a_name->val : "nouser";
343 fakeuser.pw_uid = a_uid ? (uid_t) atol(a_uid->val) : -1;
344 return print_user(&fakeuser,
345 getarg(args, 'P') != NULL,
346 getarg(args, '7') != NULL);
349 errx(EX_NOUSER, "no such uid `%s'", a_uid->val);
350 errx(EX_NOUSER, "no such user `%s'", a_name->val);
353 if (a_name == NULL) /* May be needed later */
354 a_name = addarg(args, 'n', newstr(pwd->pw_name));
357 * The M_LOCK and M_UNLOCK functions simply add or remove
358 * a "*LOCKED*" prefix from in front of the password to
359 * prevent it decoding correctly, and therefore prevents
360 * access. Of course, this only prevents access via
361 * password authentication (not ssh, kerberos or any
362 * other method that does not use the UNIX password) but
363 * that is a known limitation.
366 if (mode == M_LOCK) {
367 if (strncmp(pwd->pw_passwd, locked_str, sizeof(locked_str)-1) == 0)
368 errx(EX_DATAERR, "user '%s' is already locked", pwd->pw_name);
369 passtmp = malloc(strlen(pwd->pw_passwd) + sizeof(locked_str));
370 if (passtmp == NULL) /* disaster */
371 errx(EX_UNAVAILABLE, "out of memory");
372 strcpy(passtmp, locked_str);
373 strcat(passtmp, pwd->pw_passwd);
374 pwd->pw_passwd = passtmp;
376 } else if (mode == M_UNLOCK) {
377 if (strncmp(pwd->pw_passwd, locked_str, sizeof(locked_str)-1) != 0)
378 errx(EX_DATAERR, "user '%s' is not locked", pwd->pw_name);
379 pwd->pw_passwd += sizeof(locked_str)-1;
381 } else if (mode == M_DELETE) {
383 * Handle deletions now
385 char file[MAXPATHLEN];
386 char home[MAXPATHLEN];
387 uid_t uid = pwd->pw_uid;
389 if (strcmp(pwd->pw_name, "root") == 0)
390 errx(EX_DATAERR, "cannot remove user 'root'");
394 * Remove opie record from /etc/opiekeys
397 rmopie(pwd->pw_name);
402 sprintf(file, "/var/cron/tabs/%s", pwd->pw_name);
403 if (access(file, F_OK) == 0) {
404 sprintf(file, "crontab -u %s -r", pwd->pw_name);
409 * Save these for later, since contents of pwd may be
410 * invalidated by deletion
412 sprintf(file, "%s/%s", _PATH_MAILDIR, pwd->pw_name);
413 strlcpy(home, pwd->pw_dir, sizeof(home));
417 err(EX_IOERR, "user '%s' does not exist", pwd->pw_name);
419 warn("passwd update");
423 if (cnf->nispasswd && *cnf->nispasswd=='/') {
424 rc = delnispwent(cnf->nispasswd, a_name->val);
426 warnx("WARNING: user '%s' does not exist in NIS passwd", pwd->pw_name);
428 warn("WARNING: NIS passwd update");
432 editgroups(a_name->val, NULL);
434 pw_log(cnf, mode, W_USER, "%s(%ld) account removed", a_name->val, (long) uid);
445 if (getpwuid(uid) == NULL)
449 * Remove home directory and contents
451 if (getarg(args, 'r') != NULL && *home == '/' && getpwuid(uid) == NULL) {
452 if (stat(home, &st) != -1) {
454 pw_log(cnf, mode, W_USER, "%s(%ld) home '%s' %sremoved",
455 a_name->val, (long) uid, home,
456 stat(home, &st) == -1 ? "" : "not completely ");
461 } else if (mode == M_PRINT)
462 return print_user(pwd,
463 getarg(args, 'P') != NULL,
464 getarg(args, '7') != NULL);
467 * The rest is edit code
469 if ((arg = getarg(args, 'l')) != NULL) {
470 if (strcmp(pwd->pw_name, "root") == 0)
471 errx(EX_DATAERR, "can't rename `root' account");
472 pwd->pw_name = pw_checkname((u_char *)arg->val, 0);
476 if ((arg = getarg(args, 'u')) != NULL && isdigit((unsigned char)*arg->val)) {
477 pwd->pw_uid = (uid_t) atol(arg->val);
479 if (pwd->pw_uid != 0 && strcmp(pwd->pw_name, "root") == 0)
480 errx(EX_DATAERR, "can't change uid of `root' account");
481 if (pwd->pw_uid == 0 && strcmp(pwd->pw_name, "root") != 0)
482 warnx("WARNING: account `%s' will have a uid of 0 (superuser access!)", pwd->pw_name);
485 if ((arg = getarg(args, 'g')) != NULL && pwd->pw_uid != 0) { /* Already checked this */
486 gid_t newgid = (gid_t) GETGRNAM(cnf->default_group)->gr_gid;
487 if (newgid != pwd->pw_gid) {
489 pwd->pw_gid = newgid;
493 if ((arg = getarg(args, 'p')) != NULL) {
494 if (*arg->val == '\0' || strcmp(arg->val, "0") == 0) {
495 if (pwd->pw_change != 0) {
501 time_t now = time(NULL);
502 time_t expire = parse_date(now, arg->val);
505 errx(EX_DATAERR, "invalid password change date `%s'", arg->val);
506 if (pwd->pw_change != expire) {
507 pwd->pw_change = expire;
513 if ((arg = getarg(args, 'e')) != NULL) {
514 if (*arg->val == '\0' || strcmp(arg->val, "0") == 0) {
515 if (pwd->pw_expire != 0) {
521 time_t now = time(NULL);
522 time_t expire = parse_date(now, arg->val);
525 errx(EX_DATAERR, "invalid account expiry date `%s'", arg->val);
526 if (pwd->pw_expire != expire) {
527 pwd->pw_expire = expire;
533 if ((arg = getarg(args, 's')) != NULL) {
534 char *shell = shell_path(cnf->shelldir, cnf->shells, arg->val);
537 if (strcmp(shell, pwd->pw_shell) != 0) {
538 pwd->pw_shell = shell;
543 if (getarg(args, 'L')) {
544 if (cnf->default_class == NULL)
545 cnf->default_class = "";
546 if (strcmp(pwd->pw_class, cnf->default_class) != 0) {
547 pwd->pw_class = cnf->default_class;
552 if ((arg = getarg(args, 'd')) != NULL) {
553 if (strcmp(pwd->pw_dir, arg->val))
555 if (stat(pwd->pw_dir = arg->val, &st) == -1) {
556 if (getarg(args, 'm') == NULL && strcmp(pwd->pw_dir, "/nonexistent") != 0)
557 warnx("WARNING: home `%s' does not exist", pwd->pw_dir);
558 } else if (!S_ISDIR(st.st_mode))
559 warnx("WARNING: home `%s' is not a directory", pwd->pw_dir);
562 if ((arg = getarg(args, 'w')) != NULL &&
563 getarg(args, 'h') == NULL && getarg(args, 'H') == NULL) {
566 lc = login_getpwclass(pwd);
568 login_setcryptfmt(lc, "md5", NULL) == NULL)
569 warn("setting crypt(3) format");
571 pwd->pw_passwd = pw_password(cnf, args, pwd->pw_name);
582 if (a_name == NULL) /* Required */
583 errx(EX_DATAERR, "login name required");
584 else if ((pwd = GETPWNAM(a_name->val)) != NULL) /* Exists */
585 errx(EX_DATAERR, "login name `%s' already exists", a_name->val);
588 * Now, set up defaults for a new user
591 pwd->pw_name = a_name->val;
592 pwd->pw_class = cnf->default_class ? cnf->default_class : "";
593 pwd->pw_uid = pw_uidpolicy(cnf, args);
594 pwd->pw_gid = pw_gidpolicy(cnf, args, pwd->pw_name, (gid_t) pwd->pw_uid);
595 pwd->pw_change = pw_pwdpolicy(cnf, args);
596 pwd->pw_expire = pw_exppolicy(cnf, args);
597 pwd->pw_dir = pw_homepolicy(cnf, args, pwd->pw_name);
598 pwd->pw_shell = pw_shellpolicy(cnf, args, NULL);
599 lc = login_getpwclass(pwd);
600 if (lc == NULL || login_setcryptfmt(lc, "md5", NULL) == NULL)
601 warn("setting crypt(3) format");
603 pwd->pw_passwd = pw_password(cnf, args, pwd->pw_name);
606 if (pwd->pw_uid == 0 && strcmp(pwd->pw_name, "root") != 0)
607 warnx("WARNING: new account `%s' has a uid of 0 (superuser access!)", pwd->pw_name);
611 * Shared add/edit code
613 if ((arg = getarg(args, 'c')) != NULL) {
614 char *gecos = pw_checkname((u_char *)arg->val, 1);
615 if (strcmp(pwd->pw_gecos, gecos) != 0) {
616 pwd->pw_gecos = gecos;
621 if ((arg = getarg(args, 'h')) != NULL ||
622 (arg = getarg(args, 'H')) != NULL) {
623 if (strcmp(arg->val, "-") == 0) {
624 if (!pwd->pw_passwd || *pwd->pw_passwd != '*') {
625 pwd->pw_passwd = "*"; /* No access */
629 int fd = atoi(arg->val);
630 int precrypt = (arg->ch == 'H');
632 int istty = isatty(fd);
637 if (tcgetattr(fd, &t) == -1)
640 struct termios n = t;
643 n.c_lflag &= ~(ECHO);
644 tcsetattr(fd, TCSANOW, &n);
645 printf("%s%spassword for user %s:",
646 (mode == M_UPDATE) ? "new " : "",
647 precrypt ? "encrypted " : "",
652 b = read(fd, line, sizeof(line) - 1);
653 if (istty) { /* Restore state */
654 tcsetattr(fd, TCSANOW, &t);
659 warn("-%c file descriptor", precrypt ? 'H' :
664 if ((p = strpbrk(line, "\r\n")) != NULL)
667 errx(EX_DATAERR, "empty password read on file descriptor %d", fd);
669 if (strchr(line, ':') != NULL)
671 pwd->pw_passwd = line;
673 lc = login_getpwclass(pwd);
675 login_setcryptfmt(lc, "md5", NULL) == NULL)
676 warn("setting crypt(3) format");
678 pwd->pw_passwd = pw_pwcrypt(line);
685 * Special case: -N only displays & exits
687 if (getarg(args, 'N') != NULL)
688 return print_user(pwd,
689 getarg(args, 'P') != NULL,
690 getarg(args, '7') != NULL);
693 edited = 1; /* Always */
696 warnx("user '%s' already exists", pwd->pw_name);
698 } else if (rc != 0) {
699 warn("passwd file update");
702 if (cnf->nispasswd && *cnf->nispasswd=='/') {
703 rc = addnispwent(cnf->nispasswd, pwd);
705 warnx("User '%s' already exists in NIS passwd", pwd->pw_name);
707 warn("NIS passwd update");
708 /* NOTE: we treat NIS-only update errors as non-fatal */
710 } else if (mode == M_UPDATE || mode == M_LOCK || mode == M_UNLOCK) {
711 if (edited) { /* Only updated this if required */
712 rc = chgpwent(a_name->val, pwd);
714 warnx("user '%s' does not exist (NIS?)", pwd->pw_name);
716 } else if (rc != 0) {
717 warn("passwd file update");
720 if ( cnf->nispasswd && *cnf->nispasswd=='/') {
721 rc = chgnispwent(cnf->nispasswd, a_name->val, pwd);
723 warn("User '%s' not found in NIS passwd", pwd->pw_name);
725 warn("NIS passwd update");
726 /* NOTE: NIS-only update errors are not fatal */
732 * Ok, user is created or changed - now edit group file
735 if (mode == M_ADD || getarg(args, 'G') != NULL)
736 editgroups(pwd->pw_name, cnf->groups);
738 /* go get a current version of pwd */
739 pwd = GETPWNAM(a_name->val);
741 /* This will fail when we rename, so special case that */
742 if (mode == M_UPDATE && (arg = getarg(args, 'l')) != NULL) {
743 a_name->val = arg->val; /* update new name */
744 pwd = GETPWNAM(a_name->val); /* refetch renamed rec */
747 if (pwd == NULL) /* can't go on without this */
748 errx(EX_NOUSER, "user '%s' disappeared during update", a_name->val);
750 grp = GETGRGID(pwd->pw_gid);
751 pw_log(cnf, mode, W_USER, "%s(%ld):%s(%ld):%s:%s:%s",
752 pwd->pw_name, (long) pwd->pw_uid,
753 grp ? grp->gr_name : "unknown", (long) (grp ? grp->gr_gid : -1),
754 pwd->pw_gecos, pwd->pw_dir, pwd->pw_shell);
757 * If adding, let's touch and chown the user's mail file. This is not
758 * strictly necessary under BSD with a 0755 maildir but it also
759 * doesn't hurt anything to create the empty mailfile
763 sprintf(line, "%s/%s", _PATH_MAILDIR, pwd->pw_name);
764 close(open(line, O_RDWR | O_CREAT, 0600)); /* Preserve contents &
766 chown(line, pwd->pw_uid, pwd->pw_gid);
771 * Let's create and populate the user's home directory. Note
772 * that this also `works' for editing users if -m is used, but
773 * existing files will *not* be overwritten.
775 if (!PWALTDIR() && getarg(args, 'm') != NULL && pwd->pw_dir && *pwd->pw_dir == '/' && pwd->pw_dir[1]) {
776 copymkdir(pwd->pw_dir, cnf->dotdir, cnf->homemode, pwd->pw_uid, pwd->pw_gid);
777 pw_log(cnf, mode, W_USER, "%s(%ld) home %s made",
778 pwd->pw_name, (long) pwd->pw_uid, pwd->pw_dir);
783 * Finally, send mail to the new user as well, if we are asked to
785 if (mode == M_ADD && !PWALTDIR() && cnf->newmail && *cnf->newmail && (fp = fopen(cnf->newmail, "r")) != NULL) {
786 FILE *pfp = popen(_PATH_SENDMAIL " -t", "w");
791 fprintf(pfp, "From: root\n" "To: %s\n" "Subject: Welcome!\n\n", pwd->pw_name);
792 while (fgets(line, sizeof(line), fp) != NULL) {
793 /* Do substitutions? */
797 pw_log(cnf, mode, W_USER, "%s(%ld) new user mail sent",
798 pwd->pw_name, (long) pwd->pw_uid);
808 pw_uidpolicy(struct userconf * cnf, struct cargs * args)
811 uid_t uid = (uid_t) - 1;
812 struct carg *a_uid = getarg(args, 'u');
815 * Check the given uid, if any
818 uid = (uid_t) atol(a_uid->val);
820 if ((pwd = GETPWUID(uid)) != NULL && getarg(args, 'o') == NULL)
821 errx(EX_DATAERR, "uid `%ld' has already been allocated", (long) pwd->pw_uid);
826 * We need to allocate the next available uid under one of
827 * two policies a) Grab the first unused uid b) Grab the
828 * highest possible unused uid
830 if (cnf->min_uid >= cnf->max_uid) { /* Sanity
831 * claus^H^H^H^Hheck */
833 cnf->max_uid = 32000;
835 bm = bm_alloc(cnf->max_uid - cnf->min_uid + 1);
838 * Now, let's fill the bitmap from the password file
841 while ((pwd = GETPWENT()) != NULL)
842 if (pwd->pw_uid >= (uid_t) cnf->min_uid && pwd->pw_uid <= (uid_t) cnf->max_uid)
843 bm_setbit(&bm, pwd->pw_uid - cnf->min_uid);
847 * Then apply the policy, with fallback to reuse if necessary
849 if (cnf->reuse_uids || (uid = (uid_t) (bm_lastset(&bm) + cnf->min_uid + 1)) > cnf->max_uid)
850 uid = (uid_t) (bm_firstunset(&bm) + cnf->min_uid);
853 * Another sanity check
855 if (uid < cnf->min_uid || uid > cnf->max_uid)
856 errx(EX_SOFTWARE, "unable to allocate a new uid - range fully used");
864 pw_gidpolicy(struct userconf * cnf, struct cargs * args, char *nam, gid_t prefer)
867 gid_t gid = (uid_t) - 1;
868 struct carg *a_gid = getarg(args, 'g');
871 * If no arg given, see if default can help out
873 if (a_gid == NULL && cnf->default_group && *cnf->default_group)
874 a_gid = addarg(args, 'g', cnf->default_group);
877 * Check the given gid, if any
881 if ((grp = GETGRNAM(a_gid->val)) == NULL) {
882 gid = (gid_t) atol(a_gid->val);
883 if ((gid == 0 && !isdigit((unsigned char)*a_gid->val)) || (grp = GETGRGID(gid)) == NULL)
884 errx(EX_NOUSER, "group `%s' is not defined", a_gid->val);
887 } else if ((grp = GETGRNAM(nam)) != NULL && grp->gr_mem[0] == NULL) {
888 gid = grp->gr_gid; /* Already created? Use it anyway... */
890 struct cargs grpargs;
894 addarg(&grpargs, 'n', nam);
897 * We need to auto-create a group with the user's name. We
898 * can send all the appropriate output to our sister routine
899 * bit first see if we can create a group with gid==uid so we
900 * can keep the user and group ids in sync. We purposely do
901 * NOT check the gid range if we can force the sync. If the
902 * user's name dups an existing group, then the group add
903 * function will happily handle that case for us and exit.
905 if (GETGRGID(prefer) == NULL) {
906 sprintf(tmp, "%lu", (unsigned long) prefer);
907 addarg(&grpargs, 'g', tmp);
909 if (getarg(args, 'N'))
911 addarg(&grpargs, 'N', NULL);
912 addarg(&grpargs, 'q', NULL);
913 gid = pw_group(cnf, M_NEXT, &grpargs);
917 pw_group(cnf, M_ADD, &grpargs);
918 if ((grp = GETGRNAM(nam)) != NULL)
921 a_gid = LIST_FIRST(&grpargs);
922 while (a_gid != NULL) {
923 struct carg *t = LIST_NEXT(a_gid, list);
924 LIST_REMOVE(a_gid, list);
934 pw_pwdpolicy(struct userconf * cnf, struct cargs * args)
937 time_t now = time(NULL);
938 struct carg *arg = getarg(args, 'p');
941 if ((result = parse_date(now, arg->val)) == now)
942 errx(EX_DATAERR, "invalid date/time `%s'", arg->val);
943 } else if (cnf->password_days > 0)
944 result = now + ((long) cnf->password_days * 86400L);
950 pw_exppolicy(struct userconf * cnf, struct cargs * args)
953 time_t now = time(NULL);
954 struct carg *arg = getarg(args, 'e');
957 if ((result = parse_date(now, arg->val)) == now)
958 errx(EX_DATAERR, "invalid date/time `%s'", arg->val);
959 } else if (cnf->expire_days > 0)
960 result = now + ((long) cnf->expire_days * 86400L);
966 pw_homepolicy(struct userconf * cnf, struct cargs * args, char const * user)
968 struct carg *arg = getarg(args, 'd');
973 static char home[128];
975 if (cnf->home == NULL || *cnf->home == '\0')
976 errx(EX_CONFIG, "no base home directory set");
977 sprintf(home, "%s/%s", cnf->home, user);
983 shell_path(char const * path, char *shells[], char *sh)
985 if (sh != NULL && (*sh == '/' || *sh == '\0'))
986 return sh; /* specified full path or forced none */
989 char paths[_UC_MAXLINE];
992 * We need to search paths
994 strlcpy(paths, path, sizeof(paths));
995 for (p = strtok(paths, ": \t\r\n"); p != NULL; p = strtok(NULL, ": \t\r\n")) {
997 static char shellpath[256];
1000 sprintf(shellpath, "%s/%s", p, sh);
1001 if (access(shellpath, X_OK) == 0)
1004 for (i = 0; i < _UC_MAXSHELLS && shells[i] != NULL; i++) {
1005 sprintf(shellpath, "%s/%s", p, shells[i]);
1006 if (access(shellpath, X_OK) == 0)
1011 errx(EX_OSFILE, "can't find shell `%s' in shell paths", sh);
1012 errx(EX_CONFIG, "no default shell available or defined");
1019 pw_shellpolicy(struct userconf * cnf, struct cargs * args, char *newshell)
1021 char *sh = newshell;
1022 struct carg *arg = getarg(args, 's');
1024 if (newshell == NULL && arg != NULL)
1026 return shell_path(cnf->shelldir, cnf->shells, sh ? sh : cnf->shell_default);
1031 static char const chars[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ./";
1034 pw_pwcrypt(char *password)
1037 char salt[SALTSIZE + 1];
1039 static char buf[256];
1042 * Calculate a salt value
1044 for (i = 0; i < SALTSIZE; i++)
1045 salt[i] = chars[arc4random_uniform(sizeof(chars) - 1)];
1046 salt[SALTSIZE] = '\0';
1048 return strcpy(buf, crypt(password, salt));
1053 pw_password(struct userconf * cnf, struct cargs * args, char const * user)
1058 switch (cnf->default_password) {
1059 case -1: /* Random password */
1060 l = (arc4random() % 8 + 8); /* 8 - 16 chars */
1061 for (i = 0; i < l; i++)
1062 pwbuf[i] = chars[arc4random_uniform(sizeof(chars)-1)];
1066 * We give this information back to the user
1068 if (getarg(args, 'h') == NULL && getarg(args, 'H') == NULL &&
1069 getarg(args, 'N') == NULL) {
1070 if (isatty(STDOUT_FILENO))
1071 printf("Password for '%s' is: ", user);
1072 printf("%s\n", pwbuf);
1077 case -2: /* No password at all! */
1080 case 0: /* No login - default */
1084 case 1: /* user's name */
1085 strlcpy(pwbuf, user, sizeof(pwbuf));
1088 return pw_pwcrypt(pwbuf);
1093 print_user(struct passwd * pwd, int pretty, int v7)
1096 char buf[_UC_MAXLINE];
1098 fmtpwentry(buf, pwd, v7 ? PWF_PASSWD : PWF_STANDARD);
1103 struct group *grp = GETGRGID(pwd->pw_gid);
1104 char uname[60] = "User &", office[60] = "[None]",
1105 wphone[60] = "[None]", hphone[60] = "[None]";
1106 char acexpire[32] = "[None]", pwexpire[32] = "[None]";
1109 if ((p = strtok(pwd->pw_gecos, ",")) != NULL) {
1110 strlcpy(uname, p, sizeof(uname));
1111 if ((p = strtok(NULL, ",")) != NULL) {
1112 strlcpy(office, p, sizeof(office));
1113 if ((p = strtok(NULL, ",")) != NULL) {
1114 strlcpy(wphone, p, sizeof(wphone));
1115 if ((p = strtok(NULL, "")) != NULL) {
1123 * Handle '&' in gecos field
1125 if ((p = strchr(uname, '&')) != NULL) {
1126 int l = strlen(pwd->pw_name);
1129 memmove(p + l, p + 1, m);
1130 memmove(p, pwd->pw_name, l);
1131 *p = (char) toupper((unsigned char)*p);
1133 if (pwd->pw_expire > (time_t)0 && (tptr = localtime(&pwd->pw_expire)) != NULL)
1134 strftime(acexpire, sizeof acexpire, "%c", tptr);
1135 if (pwd->pw_change > (time_t)0 && (tptr = localtime(&pwd->pw_change)) != NULL)
1136 strftime(pwexpire, sizeof pwexpire, "%c", tptr);
1137 printf("Login Name: %-15s #%-12ld Group: %-15s #%ld\n"
1139 " Home: %-26.26s Class: %s\n"
1140 " Shell: %-26.26s Office: %s\n"
1141 "Work Phone: %-26.26s Home Phone: %s\n"
1142 "Acc Expire: %-26.26s Pwd Expire: %s\n",
1143 pwd->pw_name, (long) pwd->pw_uid,
1144 grp ? grp->gr_name : "(invalid)", (long) pwd->pw_gid,
1145 uname, pwd->pw_dir, pwd->pw_class,
1146 pwd->pw_shell, office, wphone, hphone,
1147 acexpire, pwexpire);
1150 while ((grp=GETGRENT()) != NULL)
1153 while (grp->gr_mem[i] != NULL)
1155 if (strcmp(grp->gr_mem[i], pwd->pw_name)==0)
1157 printf(j++ == 0 ? " Groups: %s" : ",%s", grp->gr_name);
1164 printf("%s", j ? "\n" : "");
1166 return EXIT_SUCCESS;
1170 pw_checkname(u_char *name, int gecos)
1173 u_char const *badchars, *ch, *showtype;
1179 /* See if the name is valid as a gecos (comment) field. */
1181 showtype = "gecos field";
1183 /* See if the name is valid as a userid or group. */
1184 badchars = " ,\t:+&#%$^()!@~*?<>=|\\/\"";
1185 showtype = "userid/group name";
1186 /* Userids and groups can not have a leading '-'. */
1192 if (strchr(badchars, *ch) != NULL || *ch < ' ' ||
1197 /* 8-bit characters are only allowed in GECOS fields */
1198 if (!gecos && (*ch & 0x80)) {
1206 * A `$' is allowed as the final character for userids and groups,
1207 * mainly for the benefit of samba.
1209 if (reject && !gecos) {
1210 if (*ch == '$' && *(ch + 1) == '\0') {
1216 snprintf(showch, sizeof(showch), (*ch >= ' ' && *ch < 127)
1217 ? "`%c'" : "0x%02x", *ch);
1218 errx(EX_DATAERR, "invalid character %s at position %d in %s",
1219 showch, (ch - name), showtype);
1221 if (!gecos && (ch - name) > LOGNAMESIZE)
1222 errx(EX_DATAERR, "name too long `%s' (max is %d)", name,
1224 return (char *)name;
1231 DIR *d = opendir("/var/at/jobs");
1236 while ((e = readdir(d)) != NULL) {
1239 if (strncmp(e->d_name, ".lock", 5) != 0 &&
1240 stat(e->d_name, &st) == 0 &&
1241 !S_ISDIR(st.st_mode) &&
1243 char tmp[MAXPATHLEN];
1245 sprintf(tmp, "/usr/bin/atrm %s", e->d_name);
1254 rmopie(char const * name)
1256 static const char etcopie[] = "/etc/opiekeys";
1257 FILE *fp = fopen(etcopie, "r+");
1262 int length = strlen(name);
1264 while (fgets(tmp, sizeof tmp, fp) != NULL) {
1265 if (strncmp(name, tmp, length) == 0 && tmp[length]==' ') {
1266 if (fseek(fp, atofs, SEEK_SET) == 0) {
1267 fwrite("#", 1, 1, fp); /* Comment username out */
1274 * If we got an error of any sort, don't update!