pw_user.c revision 52512
120253Sjoerg/*-
220302Sjoerg * Copyright (C) 1996
320302Sjoerg *	David L. Nugent.  All rights reserved.
420253Sjoerg *
520253Sjoerg * Redistribution and use in source and binary forms, with or without
620253Sjoerg * modification, are permitted provided that the following conditions
720253Sjoerg * are met:
820253Sjoerg * 1. Redistributions of source code must retain the above copyright
920302Sjoerg *    notice, this list of conditions and the following disclaimer.
1020253Sjoerg * 2. Redistributions in binary form must reproduce the above copyright
1120253Sjoerg *    notice, this list of conditions and the following disclaimer in the
1220253Sjoerg *    documentation and/or other materials provided with the distribution.
1320253Sjoerg *
1420302Sjoerg * THIS SOFTWARE IS PROVIDED BY DAVID L. NUGENT AND CONTRIBUTORS ``AS IS'' AND
1520253Sjoerg * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1620253Sjoerg * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1720302Sjoerg * ARE DISCLAIMED.  IN NO EVENT SHALL DAVID L. NUGENT OR CONTRIBUTORS BE LIABLE
1820253Sjoerg * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1920253Sjoerg * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2020253Sjoerg * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2120253Sjoerg * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2220253Sjoerg * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2320253Sjoerg * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2420253Sjoerg * SUCH DAMAGE.
2544229Sdavidn *
2620253Sjoerg */
2720253Sjoerg
2830259Scharnier#ifndef lint
2930259Scharnierstatic const char rcsid[] =
3050479Speter  "$FreeBSD: head/usr.sbin/pw/pw_user.c 52512 1999-10-26 08:34:09Z davidn $";
3130259Scharnier#endif /* not lint */
3230259Scharnier
3330259Scharnier#include <ctype.h>
3430259Scharnier#include <err.h>
3520253Sjoerg#include <fcntl.h>
3620253Sjoerg#include <sys/param.h>
3720253Sjoerg#include <dirent.h>
3830259Scharnier#include <paths.h>
3920253Sjoerg#include <termios.h>
4020555Sdavidn#include <sys/types.h>
4120555Sdavidn#include <sys/time.h>
4220555Sdavidn#include <sys/resource.h>
4330259Scharnier#include <unistd.h>
4422394Sdavidn#include <utmp.h>
4522394Sdavidn#if defined(USE_MD5RAND)
4620555Sdavidn#include <md5.h>
4722394Sdavidn#endif
4820253Sjoerg#include "pw.h"
4920253Sjoerg#include "bitmap.h"
5020253Sjoerg
5123318Sache#if (MAXLOGNAME-1) > UT_NAMESIZE
5222394Sdavidn#define LOGNAMESIZE UT_NAMESIZE
5322394Sdavidn#else
5423318Sache#define LOGNAMESIZE (MAXLOGNAME-1)
5522394Sdavidn#endif
5622394Sdavidn
5749171Sdavidnstatic          int randinit;
5852512Sdavidnstatic		char locked_str[] = "*LOCKED*";
5924214Sache
6044386Sdavidnstatic int      print_user(struct passwd * pwd, int pretty, int v7);
6120253Sjoergstatic uid_t    pw_uidpolicy(struct userconf * cnf, struct cargs * args);
6220253Sjoergstatic uid_t    pw_gidpolicy(struct userconf * cnf, struct cargs * args, char *nam, gid_t prefer);
6320253Sjoergstatic time_t   pw_pwdpolicy(struct userconf * cnf, struct cargs * args);
6420253Sjoergstatic time_t   pw_exppolicy(struct userconf * cnf, struct cargs * args);
6520253Sjoergstatic char    *pw_homepolicy(struct userconf * cnf, struct cargs * args, char const * user);
6620253Sjoergstatic char    *pw_shellpolicy(struct userconf * cnf, struct cargs * args, char *newshell);
6720253Sjoergstatic char    *pw_password(struct userconf * cnf, struct cargs * args, char const * user);
6820253Sjoergstatic char    *shell_path(char const * path, char *shells[], char *sh);
6920253Sjoergstatic void     rmat(uid_t uid);
7020747Sdavidnstatic void	rmskey(char const * name);
7120253Sjoerg
7220253Sjoerg/*-
7320253Sjoerg * -C config      configuration file
7420253Sjoerg * -q             quiet operation
7520253Sjoerg * -n name        login name
7620253Sjoerg * -u uid         user id
7720253Sjoerg * -c comment     user name/comment
7820253Sjoerg * -d directory   home directory
7920253Sjoerg * -e date        account expiry date
8020253Sjoerg * -p date        password expiry date
8120253Sjoerg * -g grp         primary group
8220253Sjoerg * -G grp1,grp2   additional groups
8320253Sjoerg * -m [ -k dir ]  create and set up home
8420253Sjoerg * -s shell       name of login shell
8520253Sjoerg * -o             duplicate uid ok
8620253Sjoerg * -L class       user class
8720253Sjoerg * -l name        new login name
8820253Sjoerg * -h fd          password filehandle
8920253Sjoerg * -F             force print or add
9020253Sjoerg *   Setting defaults:
9120253Sjoerg * -D             set user defaults
9220253Sjoerg * -b dir         default home root dir
9320253Sjoerg * -e period      default expiry period
9420253Sjoerg * -p period      default password change period
9520253Sjoerg * -g group       default group
9620253Sjoerg * -G             grp1,grp2.. default additional groups
9720253Sjoerg * -L class       default login class
9820253Sjoerg * -k dir         default home skeleton
9920253Sjoerg * -s shell       default shell
10020253Sjoerg * -w method      default password method
10120253Sjoerg */
10220253Sjoerg
10320253Sjoergint
10420253Sjoergpw_user(struct userconf * cnf, int mode, struct cargs * args)
10520253Sjoerg{
10652502Sdavidn	int	        rc;
10720253Sjoerg	char           *p = NULL;
10852512Sdavidn	char					 *passtmp;
10920253Sjoerg	struct carg    *a_name;
11020253Sjoerg	struct carg    *a_uid;
11120253Sjoerg	struct carg    *arg;
11220253Sjoerg	struct passwd  *pwd = NULL;
11320253Sjoerg	struct group   *grp;
11420253Sjoerg	struct stat     st;
11520747Sdavidn	char            line[_PASSWORD_LEN+1];
11620253Sjoerg
11720253Sjoerg	static struct passwd fakeuser =
11820253Sjoerg	{
11920253Sjoerg		NULL,
12020253Sjoerg		"*",
12120253Sjoerg		-1,
12220253Sjoerg		-1,
12320253Sjoerg		0,
12420253Sjoerg		"",
12520253Sjoerg		"User &",
12620253Sjoerg		"/bin/sh",
12720253Sjoerg		0,
12849171Sdavidn		0,
12920253Sjoerg		0
13020253Sjoerg	};
13120253Sjoerg
13252512Sdavidn
13320253Sjoerg	/*
13420267Sjoerg	 * With M_NEXT, we only need to return the
13520267Sjoerg	 * next uid to stdout
13620267Sjoerg	 */
13720267Sjoerg	if (mode == M_NEXT)
13820267Sjoerg	{
13920267Sjoerg		uid_t next = pw_uidpolicy(cnf, args);
14020267Sjoerg		if (getarg(args, 'q'))
14120267Sjoerg			return next;
14220267Sjoerg		printf("%ld:", (long)next);
14320267Sjoerg		pw_group(cnf, mode, args);
14420267Sjoerg		return EXIT_SUCCESS;
14520267Sjoerg	}
14620267Sjoerg
14720267Sjoerg	/*
14820253Sjoerg	 * We can do all of the common legwork here
14920253Sjoerg	 */
15020253Sjoerg
15120253Sjoerg	if ((arg = getarg(args, 'b')) != NULL) {
15220267Sjoerg		cnf->home = arg->val;
15320253Sjoerg	}
15421052Sdavidn
15521052Sdavidn	/*
15621052Sdavidn	 * If we'll need to use it or we're updating it,
15721052Sdavidn	 * then create the base home directory if necessary
15821052Sdavidn	 */
15921052Sdavidn	if (arg != NULL || getarg(args, 'm') != NULL) {
16021052Sdavidn		int	l = strlen(cnf->home);
16121052Sdavidn
16221052Sdavidn		if (l > 1 && cnf->home[l-1] == '/')	/* Shave off any trailing path delimiter */
16321052Sdavidn			cnf->home[--l] = '\0';
16421052Sdavidn
16521052Sdavidn		if (l < 2 || *cnf->home != '/')		/* Check for absolute path name */
16630259Scharnier			errx(EX_DATAERR, "invalid base directory for home '%s'", cnf->home);
16721052Sdavidn
16821052Sdavidn		if (stat(cnf->home, &st) == -1) {
16921052Sdavidn			char	dbuf[MAXPATHLEN];
17021052Sdavidn
17121242Sdavidn			/*
17221242Sdavidn			 * This is a kludge especially for Joerg :)
17321242Sdavidn			 * If the home directory would be created in the root partition, then
17421242Sdavidn			 * we really create it under /usr which is likely to have more space.
17521242Sdavidn			 * But we create a symlink from cnf->home -> "/usr" -> cnf->home
17621242Sdavidn			 */
17721242Sdavidn			if (strchr(cnf->home+1, '/') == NULL) {
17821242Sdavidn				strcpy(dbuf, "/usr");
17921242Sdavidn				strncat(dbuf, cnf->home, MAXPATHLEN-5);
18021242Sdavidn				if (mkdir(dbuf, 0755) != -1 || errno == EEXIST) {
18121242Sdavidn					chown(dbuf, 0, 0);
18221242Sdavidn					symlink(dbuf, cnf->home);
18321242Sdavidn				}
18421242Sdavidn				/* If this falls, fall back to old method */
18521242Sdavidn			}
18621052Sdavidn			p = strncpy(dbuf, cnf->home, sizeof dbuf);
18721052Sdavidn			dbuf[MAXPATHLEN-1] = '\0';
18821242Sdavidn			if (stat(dbuf, &st) == -1) {
18921242Sdavidn				while ((p = strchr(++p, '/')) != NULL) {
19021242Sdavidn					*p = '\0';
19121242Sdavidn					if (stat(dbuf, &st) == -1) {
19221242Sdavidn						if (mkdir(dbuf, 0755) == -1)
19321242Sdavidn							goto direrr;
19421242Sdavidn						chown(dbuf, 0, 0);
19521242Sdavidn					} else if (!S_ISDIR(st.st_mode))
19630259Scharnier						errx(EX_OSFILE, "'%s' (root home parent) is not a directory", dbuf);
19721242Sdavidn					*p = '/';
19821242Sdavidn				}
19921052Sdavidn			}
20021242Sdavidn			if (stat(dbuf, &st) == -1) {
20121052Sdavidn				if (mkdir(dbuf, 0755) == -1) {
20230259Scharnier				direrr:	err(EX_OSFILE, "mkdir '%s'", dbuf);
20321052Sdavidn				}
20421052Sdavidn				chown(dbuf, 0, 0);
20521052Sdavidn			}
20621052Sdavidn		} else if (!S_ISDIR(st.st_mode))
20730259Scharnier			errx(EX_OSFILE, "root home `%s' is not a directory", cnf->home);
20821052Sdavidn	}
20921052Sdavidn
21021052Sdavidn
21120253Sjoerg	if ((arg = getarg(args, 'e')) != NULL)
21220253Sjoerg		cnf->expire_days = atoi(arg->val);
21320253Sjoerg
21421330Sdavidn	if ((arg = getarg(args, 'y')) != NULL)
21521330Sdavidn		cnf->nispasswd = arg->val;
21621330Sdavidn
21720253Sjoerg	if ((arg = getarg(args, 'p')) != NULL && arg->val)
21820253Sjoerg		cnf->password_days = atoi(arg->val);
21920253Sjoerg
22020253Sjoerg	if ((arg = getarg(args, 'g')) != NULL) {
22120253Sjoerg		p = arg->val;
22244229Sdavidn		if ((grp = GETGRNAM(p)) == NULL) {
22344229Sdavidn			if (!isdigit(*p) || (grp = GETGRGID((gid_t) atoi(p))) == NULL)
22430259Scharnier				errx(EX_NOUSER, "group `%s' does not exist", p);
22520253Sjoerg		}
22620253Sjoerg		cnf->default_group = newstr(grp->gr_name);
22720253Sjoerg	}
22820253Sjoerg	if ((arg = getarg(args, 'L')) != NULL)
22920679Sdavidn		cnf->default_class = pw_checkname((u_char *)arg->val, 0);
23020253Sjoerg
23120253Sjoerg	if ((arg = getarg(args, 'G')) != NULL && arg->val) {
23220253Sjoerg		int             i = 0;
23320253Sjoerg
23420747Sdavidn		for (p = strtok(arg->val, ", \t"); p != NULL; p = strtok(NULL, ", \t")) {
23544229Sdavidn			if ((grp = GETGRNAM(p)) == NULL) {
23644229Sdavidn				if (!isdigit(*p) || (grp = GETGRGID((gid_t) atoi(p))) == NULL)
23730259Scharnier					errx(EX_NOUSER, "group `%s' does not exist", p);
23820253Sjoerg			}
23920747Sdavidn			if (extendarray(&cnf->groups, &cnf->numgroups, i + 2) != -1)
24020747Sdavidn				cnf->groups[i++] = newstr(grp->gr_name);
24120253Sjoerg		}
24220747Sdavidn		while (i < cnf->numgroups)
24320253Sjoerg			cnf->groups[i++] = NULL;
24420253Sjoerg	}
24520253Sjoerg	if ((arg = getarg(args, 'k')) != NULL) {
24626088Sdavidn		if (stat(cnf->dotdir = arg->val, &st) == -1 || !S_ISDIR(st.st_mode))
24730259Scharnier			errx(EX_OSFILE, "skeleton `%s' is not a directory or does not exist", cnf->dotdir);
24820253Sjoerg	}
24920253Sjoerg	if ((arg = getarg(args, 's')) != NULL)
25020253Sjoerg		cnf->shell_default = arg->val;
25120253Sjoerg
25220253Sjoerg	if (mode == M_ADD && getarg(args, 'D')) {
25320253Sjoerg		if (getarg(args, 'n') != NULL)
25430259Scharnier			errx(EX_DATAERR, "can't combine `-D' with `-n name'");
25520253Sjoerg		if ((arg = getarg(args, 'u')) != NULL && (p = strtok(arg->val, ", \t")) != NULL) {
25620253Sjoerg			if ((cnf->min_uid = (uid_t) atoi(p)) == 0)
25720253Sjoerg				cnf->min_uid = 1000;
25820253Sjoerg			if ((p = strtok(NULL, " ,\t")) == NULL || (cnf->max_uid = (uid_t) atoi(p)) < cnf->min_uid)
25920253Sjoerg				cnf->max_uid = 32000;
26020253Sjoerg		}
26120253Sjoerg		if ((arg = getarg(args, 'i')) != NULL && (p = strtok(arg->val, ", \t")) != NULL) {
26220253Sjoerg			if ((cnf->min_gid = (gid_t) atoi(p)) == 0)
26320253Sjoerg				cnf->min_gid = 1000;
26420253Sjoerg			if ((p = strtok(NULL, " ,\t")) == NULL || (cnf->max_gid = (gid_t) atoi(p)) < cnf->min_gid)
26520253Sjoerg				cnf->max_gid = 32000;
26620253Sjoerg		}
26720253Sjoerg		if ((arg = getarg(args, 'w')) != NULL)
26820253Sjoerg			cnf->default_password = boolean_val(arg->val, cnf->default_password);
26920253Sjoerg
27020253Sjoerg		arg = getarg(args, 'C');
27120253Sjoerg		if (write_userconfig(arg ? arg->val : NULL))
27220267Sjoerg			return EXIT_SUCCESS;
27330259Scharnier		warn("config update");
27420267Sjoerg		return EX_IOERR;
27520253Sjoerg	}
27620253Sjoerg	if (mode == M_PRINT && getarg(args, 'a')) {
27720267Sjoerg		int             pretty = getarg(args, 'P') != NULL;
27844386Sdavidn		int		v7 = getarg(args, '7') != NULL;
27920253Sjoerg
28044229Sdavidn		SETPWENT();
28144229Sdavidn		while ((pwd = GETPWENT()) != NULL)
28244386Sdavidn			print_user(pwd, pretty, v7);
28344229Sdavidn		ENDPWENT();
28420267Sjoerg		return EXIT_SUCCESS;
28520253Sjoerg	}
28620253Sjoerg	if ((a_name = getarg(args, 'n')) != NULL)
28744229Sdavidn		pwd = GETPWNAM(pw_checkname((u_char *)a_name->val, 0));
28820253Sjoerg	a_uid = getarg(args, 'u');
28920253Sjoerg
29020253Sjoerg	if (a_uid == NULL) {
29120253Sjoerg		if (a_name == NULL)
29230259Scharnier			errx(EX_DATAERR, "user name or id required");
29320253Sjoerg
29420253Sjoerg		/*
29520253Sjoerg		 * Determine whether 'n' switch is name or uid - we don't
29620253Sjoerg		 * really don't really care which we have, but we need to
29720253Sjoerg		 * know.
29820253Sjoerg		 */
29943780Sdes		if (mode != M_ADD && pwd == NULL
30043780Sdes		    && strspn(a_name->val, "0123456789") == strlen(a_name->val)
30143780Sdes		    && atoi(a_name->val) > 0) {	/* Assume uid */
30220253Sjoerg			(a_uid = a_name)->ch = 'u';
30320253Sjoerg			a_name = NULL;
30420253Sjoerg		}
30520253Sjoerg	}
30620253Sjoerg	/*
30720253Sjoerg	 * Update, delete & print require that the user exists
30820253Sjoerg	 */
30952512Sdavidn	if (mode == M_UPDATE || mode == M_DELETE ||
31052512Sdavidn	    mode == M_PRINT  || mode == M_LOCK   || mode == M_UNLOCK) {
31120253Sjoerg		if (a_name == NULL && pwd == NULL)	/* Try harder */
31244229Sdavidn			pwd = GETPWUID(atoi(a_uid->val));
31320253Sjoerg
31420253Sjoerg		if (pwd == NULL) {
31520253Sjoerg			if (mode == M_PRINT && getarg(args, 'F')) {
31620253Sjoerg				fakeuser.pw_name = a_name ? a_name->val : "nouser";
31720253Sjoerg				fakeuser.pw_uid = a_uid ? (uid_t) atol(a_uid->val) : -1;
31844386Sdavidn				return print_user(&fakeuser,
31944386Sdavidn						  getarg(args, 'P') != NULL,
32044386Sdavidn						  getarg(args, '7') != NULL);
32120253Sjoerg			}
32220253Sjoerg			if (a_name == NULL)
32330259Scharnier				errx(EX_NOUSER, "no such uid `%s'", a_uid->val);
32430259Scharnier			errx(EX_NOUSER, "no such user `%s'", a_name->val);
32520253Sjoerg		}
32620253Sjoerg		if (a_name == NULL)	/* May be needed later */
32720253Sjoerg			a_name = addarg(args, 'n', newstr(pwd->pw_name));
32820253Sjoerg
32920253Sjoerg		/*
33052512Sdavidn		 * The M_LOCK and M_UNLOCK functions simply add or remove
33152512Sdavidn		 * a "*LOCKED*" prefix from in front of the password to
33252512Sdavidn		 * prevent it decoding correctly, and therefore prevents
33352512Sdavidn		 * access. Of course, this only prevents access via
33452512Sdavidn		 * password authentication (not ssh, kerberos or any
33552512Sdavidn		 * other method that does not use the UNIX password) but
33652512Sdavidn		 * that is a known limitation.
33752512Sdavidn		 */
33852512Sdavidn
33952512Sdavidn		if (mode == M_LOCK) {
34052512Sdavidn			if (strncmp(pwd->pw_passwd, locked_str, sizeof(locked_str)-1) == 0)
34152512Sdavidn				errx(EX_DATAERR, "user '%s' is already locked", pwd->pw_name);
34252512Sdavidn			passtmp = malloc(strlen(pwd->pw_passwd) + sizeof(locked_str));
34352512Sdavidn			if (passtmp == NULL)	/* disaster */
34452512Sdavidn				errx(EX_UNAVAILABLE, "out of memory");
34552512Sdavidn			strcpy(passtmp, locked_str);
34652512Sdavidn			strcat(passtmp, pwd->pw_passwd);
34752512Sdavidn			pwd->pw_passwd = passtmp;
34852512Sdavidn		} else if (mode == M_UNLOCK) {
34952512Sdavidn			if (strncmp(pwd->pw_passwd, locked_str, sizeof(locked_str)-1) != 0)
35052512Sdavidn				errx(EX_DATAERR, "user '%s' is not locked", pwd->pw_name);
35152512Sdavidn			pwd->pw_passwd += sizeof(locked_str)-1;
35252512Sdavidn		}
35352512Sdavidn
35452512Sdavidn		/*
35520253Sjoerg		 * Handle deletions now
35620253Sjoerg		 */
35720253Sjoerg		if (mode == M_DELETE) {
35820253Sjoerg			char            file[MAXPATHLEN];
35920253Sjoerg			char            home[MAXPATHLEN];
36020253Sjoerg			uid_t           uid = pwd->pw_uid;
36120253Sjoerg
36220253Sjoerg			if (strcmp(pwd->pw_name, "root") == 0)
36330259Scharnier				errx(EX_DATAERR, "cannot remove user 'root'");
36420253Sjoerg
36544229Sdavidn			if (!PWALTDIR()) {
36644229Sdavidn				/*
36744229Sdavidn				 * Remove skey record from /etc/skeykeys
36844229Sdavidn		        	 */
36920747Sdavidn
37044229Sdavidn				rmskey(pwd->pw_name);
37120747Sdavidn
37244229Sdavidn				/*
37344229Sdavidn				 * Remove crontabs
37444229Sdavidn				 */
37544229Sdavidn				sprintf(file, "/var/cron/tabs/%s", pwd->pw_name);
37644229Sdavidn				if (access(file, F_OK) == 0) {
37744229Sdavidn					sprintf(file, "crontab -u %s -r", pwd->pw_name);
37844229Sdavidn					system(file);
37944229Sdavidn				}
38020253Sjoerg			}
38120253Sjoerg			/*
38220253Sjoerg			 * Save these for later, since contents of pwd may be
38320253Sjoerg			 * invalidated by deletion
38420253Sjoerg			 */
38520253Sjoerg			sprintf(file, "%s/%s", _PATH_MAILDIR, pwd->pw_name);
38620253Sjoerg			strncpy(home, pwd->pw_dir, sizeof home);
38720253Sjoerg			home[sizeof home - 1] = '\0';
38820253Sjoerg
38952502Sdavidn			rc = delpwent(pwd);
39052502Sdavidn			if (rc == -1)
39152502Sdavidn				err(EX_IOERR, "user '%s' does not exist", pwd->pw_name);
39252502Sdavidn			else if (rc != 0) {
39352502Sdavidn				warnc(rc, "passwd update");
39452502Sdavidn				return EX_IOERR;
39552502Sdavidn			}
39621330Sdavidn
39752502Sdavidn			if (cnf->nispasswd && *cnf->nispasswd=='/') {
39852502Sdavidn				rc = delnispwent(cnf->nispasswd, a_name->val);
39952502Sdavidn				if (rc == -1)
40052502Sdavidn					warnx("WARNING: user '%s' does not exist in NIS passwd", pwd->pw_name);
40152502Sdavidn				else if (rc != 0)
40252502Sdavidn					warnc(rc, "WARNING: NIS passwd update");
40352502Sdavidn				/* non-fatal */
40452502Sdavidn			}
40552502Sdavidn
40620253Sjoerg			editgroups(a_name->val, NULL);
40720253Sjoerg
40820253Sjoerg			pw_log(cnf, mode, W_USER, "%s(%ld) account removed", a_name->val, (long) uid);
40920253Sjoerg
41044229Sdavidn			if (!PWALTDIR()) {
41144229Sdavidn				/*
41244229Sdavidn				 * Remove mail file
41344229Sdavidn				 */
41444229Sdavidn				remove(file);
41520253Sjoerg
41644229Sdavidn				/*
41744229Sdavidn				 * Remove at jobs
41844229Sdavidn				 */
41944229Sdavidn				if (getpwuid(uid) == NULL)
42044229Sdavidn					rmat(uid);
42120253Sjoerg
42244229Sdavidn				/*
42344229Sdavidn				 * Remove home directory and contents
42444229Sdavidn				 */
42544229Sdavidn				if (getarg(args, 'r') != NULL && *home == '/' && getpwuid(uid) == NULL) {
42644229Sdavidn					if (stat(home, &st) != -1) {
42744229Sdavidn						rm_r(home, uid);
42844229Sdavidn						pw_log(cnf, mode, W_USER, "%s(%ld) home '%s' %sremoved",
42944229Sdavidn						       a_name->val, (long) uid, home,
43044229Sdavidn						       stat(home, &st) == -1 ? "" : "not completely ");
43144229Sdavidn					}
43220253Sjoerg				}
43320253Sjoerg			}
43420267Sjoerg			return EXIT_SUCCESS;
43520253Sjoerg		} else if (mode == M_PRINT)
43644386Sdavidn			return print_user(pwd,
43744386Sdavidn					  getarg(args, 'P') != NULL,
43844386Sdavidn					  getarg(args, '7') != NULL);
43920253Sjoerg
44020253Sjoerg		/*
44120253Sjoerg		 * The rest is edit code
44220253Sjoerg		 */
44320253Sjoerg		if ((arg = getarg(args, 'l')) != NULL) {
44420253Sjoerg			if (strcmp(pwd->pw_name, "root") == 0)
44530259Scharnier				errx(EX_DATAERR, "can't rename `root' account");
44620679Sdavidn			pwd->pw_name = pw_checkname((u_char *)arg->val, 0);
44720253Sjoerg		}
44820253Sjoerg		if ((arg = getarg(args, 'u')) != NULL && isdigit(*arg->val)) {
44920253Sjoerg			pwd->pw_uid = (uid_t) atol(arg->val);
45020253Sjoerg			if (pwd->pw_uid != 0 && strcmp(pwd->pw_name, "root") == 0)
45130259Scharnier				errx(EX_DATAERR, "can't change uid of `root' account");
45220253Sjoerg			if (pwd->pw_uid == 0 && strcmp(pwd->pw_name, "root") != 0)
45330259Scharnier				warnx("WARNING: account `%s' will have a uid of 0 (superuser access!)", pwd->pw_name);
45420253Sjoerg		}
45520253Sjoerg		if ((arg = getarg(args, 'g')) != NULL && pwd->pw_uid != 0)	/* Already checked this */
45644229Sdavidn			pwd->pw_gid = (gid_t) GETGRNAM(cnf->default_group)->gr_gid;
45720253Sjoerg
45820253Sjoerg		if ((arg = getarg(args, 'p')) != NULL) {
45920253Sjoerg			if (*arg->val == '\0' || strcmp(arg->val, "0") == 0)
46020253Sjoerg				pwd->pw_change = 0;
46120253Sjoerg			else {
46220253Sjoerg				time_t          now = time(NULL);
46320253Sjoerg				time_t          expire = parse_date(now, arg->val);
46420253Sjoerg
46520253Sjoerg				if (now == expire)
46630259Scharnier					errx(EX_DATAERR, "invalid password change date `%s'", arg->val);
46720253Sjoerg				pwd->pw_change = expire;
46820253Sjoerg			}
46920253Sjoerg		}
47020267Sjoerg		if ((arg = getarg(args, 'e')) != NULL) {
47120253Sjoerg			if (*arg->val == '\0' || strcmp(arg->val, "0") == 0)
47220253Sjoerg				pwd->pw_expire = 0;
47320253Sjoerg			else {
47420253Sjoerg				time_t          now = time(NULL);
47520253Sjoerg				time_t          expire = parse_date(now, arg->val);
47620253Sjoerg
47720253Sjoerg				if (now == expire)
47830259Scharnier					errx(EX_DATAERR, "invalid account expiry date `%s'", arg->val);
47920253Sjoerg				pwd->pw_expire = expire;
48020253Sjoerg			}
48120253Sjoerg		}
48220253Sjoerg		if ((arg = getarg(args, 's')) != NULL)
48320253Sjoerg			pwd->pw_shell = shell_path(cnf->shelldir, cnf->shells, arg->val);
48420253Sjoerg
48520253Sjoerg		if (getarg(args, 'L'))
48620253Sjoerg			pwd->pw_class = cnf->default_class;
48720253Sjoerg
48820747Sdavidn		if ((arg  = getarg(args, 'd')) != NULL) {
48920747Sdavidn			if (stat(pwd->pw_dir = arg->val, &st) == -1) {
49020747Sdavidn				if (getarg(args, 'm') == NULL && strcmp(pwd->pw_dir, "/nonexistent") != 0)
49130259Scharnier				  warnx("WARNING: home `%s' does not exist", pwd->pw_dir);
49220747Sdavidn			} else if (!S_ISDIR(st.st_mode))
49330259Scharnier				warnx("WARNING: home `%s' is not a directory", pwd->pw_dir);
49420747Sdavidn		}
49520747Sdavidn
49620267Sjoerg		if ((arg = getarg(args, 'w')) != NULL && getarg(args, 'h') == NULL)
49720267Sjoerg			pwd->pw_passwd = pw_password(cnf, args, pwd->pw_name);
49820267Sjoerg
49920253Sjoerg	} else {
50020253Sjoerg		if (a_name == NULL)	/* Required */
50130259Scharnier			errx(EX_DATAERR, "login name required");
50244229Sdavidn		else if ((pwd = GETPWNAM(a_name->val)) != NULL)	/* Exists */
50330259Scharnier			errx(EX_DATAERR, "login name `%s' already exists", a_name->val);
50420253Sjoerg
50520253Sjoerg		/*
50620253Sjoerg		 * Now, set up defaults for a new user
50720253Sjoerg		 */
50820253Sjoerg		pwd = &fakeuser;
50920253Sjoerg		pwd->pw_name = a_name->val;
51020253Sjoerg		pwd->pw_class = cnf->default_class ? cnf->default_class : "";
51120253Sjoerg		pwd->pw_passwd = pw_password(cnf, args, pwd->pw_name);
51220253Sjoerg		pwd->pw_uid = pw_uidpolicy(cnf, args);
51320253Sjoerg		pwd->pw_gid = pw_gidpolicy(cnf, args, pwd->pw_name, (gid_t) pwd->pw_uid);
51420253Sjoerg		pwd->pw_change = pw_pwdpolicy(cnf, args);
51520253Sjoerg		pwd->pw_expire = pw_exppolicy(cnf, args);
51620253Sjoerg		pwd->pw_dir = pw_homepolicy(cnf, args, pwd->pw_name);
51720253Sjoerg		pwd->pw_shell = pw_shellpolicy(cnf, args, NULL);
51820253Sjoerg
51920253Sjoerg		if (pwd->pw_uid == 0 && strcmp(pwd->pw_name, "root") != 0)
52030259Scharnier			warnx("WARNING: new account `%s' has a uid of 0 (superuser access!)", pwd->pw_name);
52120253Sjoerg	}
52220253Sjoerg
52320253Sjoerg	/*
52420253Sjoerg	 * Shared add/edit code
52520253Sjoerg	 */
52620253Sjoerg	if ((arg = getarg(args, 'c')) != NULL)
52720679Sdavidn		pwd->pw_gecos = pw_checkname((u_char *)arg->val, 1);
52820253Sjoerg
52920253Sjoerg	if ((arg = getarg(args, 'h')) != NULL) {
53020253Sjoerg		if (strcmp(arg->val, "-") == 0)
53120253Sjoerg			pwd->pw_passwd = "*";	/* No access */
53220253Sjoerg		else {
53320253Sjoerg			int             fd = atoi(arg->val);
53420253Sjoerg			int             b;
53520253Sjoerg			int             istty = isatty(fd);
53620253Sjoerg			struct termios  t;
53720253Sjoerg
53820253Sjoerg			if (istty) {
53920253Sjoerg				if (tcgetattr(fd, &t) == -1)
54020253Sjoerg					istty = 0;
54120253Sjoerg				else {
54220253Sjoerg					struct termios  n = t;
54320253Sjoerg
54420253Sjoerg					/* Disable echo */
54520253Sjoerg					n.c_lflag &= ~(ECHO);
54620253Sjoerg					tcsetattr(fd, TCSANOW, &n);
54720253Sjoerg					printf("%sassword for user %s:", (mode == M_UPDATE) ? "New p" : "P", pwd->pw_name);
54820253Sjoerg					fflush(stdout);
54920253Sjoerg				}
55020253Sjoerg			}
55120253Sjoerg			b = read(fd, line, sizeof(line) - 1);
55220253Sjoerg			if (istty) {	/* Restore state */
55320253Sjoerg				tcsetattr(fd, TCSANOW, &t);
55420253Sjoerg				fputc('\n', stdout);
55520253Sjoerg				fflush(stdout);
55620253Sjoerg			}
55720253Sjoerg			if (b < 0) {
55830259Scharnier				warn("-h file descriptor");
55920267Sjoerg				return EX_IOERR;
56020253Sjoerg			}
56120253Sjoerg			line[b] = '\0';
56220253Sjoerg			if ((p = strpbrk(line, " \t\r\n")) != NULL)
56320253Sjoerg				*p = '\0';
56420253Sjoerg			if (!*line)
56530259Scharnier				errx(EX_DATAERR, "empty password read on file descriptor %d", fd);
56620253Sjoerg			pwd->pw_passwd = pw_pwcrypt(line);
56720253Sjoerg		}
56820253Sjoerg	}
56920267Sjoerg
57020267Sjoerg	/*
57120267Sjoerg	 * Special case: -N only displays & exits
57220267Sjoerg	 */
57320267Sjoerg	if (getarg(args, 'N') != NULL)
57444386Sdavidn		return print_user(pwd,
57544386Sdavidn				  getarg(args, 'P') != NULL,
57644386Sdavidn				  getarg(args, '7') != NULL);
57720267Sjoerg
57821330Sdavidn	if (mode == M_ADD) {
57952502Sdavidn		rc = addpwent(pwd);
58052502Sdavidn		if (rc == -1) {
58152502Sdavidn			warnx("user '%s' already exists", pwd->pw_name);
58252502Sdavidn			return EX_IOERR;
58352502Sdavidn		} else if (rc != 0) {
58452502Sdavidn			warnc(rc, "passwd file update");
58552502Sdavidn			return EX_IOERR;
58652502Sdavidn		}
58752502Sdavidn		if (cnf->nispasswd && *cnf->nispasswd=='/') {
58852502Sdavidn			rc = addnispwent(cnf->nispasswd, pwd);
58952502Sdavidn			if (rc == -1)
59052502Sdavidn				warnx("User '%s' already exists in NIS passwd", pwd->pw_name);
59152502Sdavidn			else
59252502Sdavidn				warnc(rc, "NIS passwd update");
59352502Sdavidn			/* NOTE: we treat NIS-only update errors as non-fatal */
59452502Sdavidn		}
59552512Sdavidn	} else if (mode == M_UPDATE || mode == M_LOCK || mode == M_UNLOCK) {
59652502Sdavidn		rc = chgpwent(a_name->val, pwd);
59752502Sdavidn		if (rc == -1) {
59852502Sdavidn			warnx("user '%s' does not exist (NIS?)", pwd->pw_name);
59952502Sdavidn			return EX_IOERR;
60052502Sdavidn		} else if (rc != 0) {
60152502Sdavidn			warnc(rc, "passwd file update");
60252502Sdavidn			return EX_IOERR;
60352502Sdavidn		}
60452502Sdavidn		if ( cnf->nispasswd && *cnf->nispasswd=='/') {
60552502Sdavidn			rc = chgnispwent(cnf->nispasswd, a_name->val, pwd);
60652502Sdavidn			if (rc == -1)
60752502Sdavidn				warn("User '%s' not found in NIS passwd", pwd->pw_name);
60852502Sdavidn			else
60952502Sdavidn				warnc(rc, "NIS passwd update");
61052502Sdavidn			/* NOTE: NIS-only update errors are not fatal */
61152502Sdavidn		}
61221330Sdavidn	}
61321330Sdavidn
61420253Sjoerg	/*
61520253Sjoerg	 * Ok, user is created or changed - now edit group file
61620253Sjoerg	 */
61720253Sjoerg
61820253Sjoerg	if (mode == M_ADD || getarg(args, 'G') != NULL)
61920253Sjoerg		editgroups(pwd->pw_name, cnf->groups);
62020253Sjoerg
62120253Sjoerg	/* pwd may have been invalidated */
62244229Sdavidn	if ((pwd = GETPWNAM(a_name->val)) == NULL)
62330259Scharnier		errx(EX_NOUSER, "user '%s' disappeared during update", a_name->val);
62420253Sjoerg
62544229Sdavidn	grp = GETGRGID(pwd->pw_gid);
62620253Sjoerg	pw_log(cnf, mode, W_USER, "%s(%ld):%s(%d):%s:%s:%s",
62720253Sjoerg	       pwd->pw_name, (long) pwd->pw_uid,
62820253Sjoerg	    grp ? grp->gr_name : "unknown", (long) (grp ? grp->gr_gid : -1),
62920253Sjoerg	       pwd->pw_gecos, pwd->pw_dir, pwd->pw_shell);
63020253Sjoerg
63120253Sjoerg	/*
63220253Sjoerg	 * If adding, let's touch and chown the user's mail file. This is not
63320253Sjoerg	 * strictly necessary under BSD with a 0755 maildir but it also
63420253Sjoerg	 * doesn't hurt anything to create the empty mailfile
63520253Sjoerg	 */
63620253Sjoerg	if (mode == M_ADD) {
63720253Sjoerg		FILE           *fp;
63820253Sjoerg
63944229Sdavidn		if (!PWALTDIR()) {
64044229Sdavidn			sprintf(line, "%s/%s", _PATH_MAILDIR, pwd->pw_name);
64144229Sdavidn			close(open(line, O_RDWR | O_CREAT, 0600));	/* Preserve contents &
64244229Sdavidn									 * mtime */
64344229Sdavidn			chown(line, pwd->pw_uid, pwd->pw_gid);
64420253Sjoerg
64544229Sdavidn			/*
64644229Sdavidn			 * Send mail to the new user as well, if we are asked to
64744229Sdavidn			 */
64844229Sdavidn			if (cnf->newmail && *cnf->newmail && (fp = fopen(cnf->newmail, "r")) != NULL) {
64944229Sdavidn				FILE           *pfp = popen(_PATH_SENDMAIL " -t", "w");
65020253Sjoerg
65144229Sdavidn				if (pfp == NULL)
65244229Sdavidn					warn("sendmail");
65344229Sdavidn				else {
65444229Sdavidn					fprintf(pfp, "From: root\n" "To: %s\n" "Subject: Welcome!\n\n", pwd->pw_name);
65544229Sdavidn					while (fgets(line, sizeof(line), fp) != NULL) {
65644229Sdavidn						/* Do substitutions? */
65744229Sdavidn						fputs(line, pfp);
65844229Sdavidn					}
65944229Sdavidn					pclose(pfp);
66044229Sdavidn					pw_log(cnf, mode, W_USER, "%s(%ld) new user mail sent",
66144229Sdavidn					       pwd->pw_name, (long) pwd->pw_uid);
66220253Sjoerg				}
66344229Sdavidn				fclose(fp);
66420253Sjoerg			}
66520253Sjoerg		}
66620253Sjoerg	}
66720253Sjoerg	/*
66820253Sjoerg	 * Finally, let's create and populate the user's home directory. Note
66920253Sjoerg	 * that this also `works' for editing users if -m is used, but
67020253Sjoerg	 * existing files will *not* be overwritten.
67120253Sjoerg	 */
67244229Sdavidn	if (!PWALTDIR() && getarg(args, 'm') != NULL && pwd->pw_dir && *pwd->pw_dir == '/' && pwd->pw_dir[1]) {
67320253Sjoerg		copymkdir(pwd->pw_dir, cnf->dotdir, 0755, pwd->pw_uid, pwd->pw_gid);
67420253Sjoerg		pw_log(cnf, mode, W_USER, "%s(%ld) home %s made",
67520253Sjoerg		       pwd->pw_name, (long) pwd->pw_uid, pwd->pw_dir);
67620253Sjoerg	}
67720267Sjoerg	return EXIT_SUCCESS;
67820253Sjoerg}
67920253Sjoerg
68020253Sjoerg
68120253Sjoergstatic          uid_t
68220253Sjoergpw_uidpolicy(struct userconf * cnf, struct cargs * args)
68320253Sjoerg{
68420253Sjoerg	struct passwd  *pwd;
68520253Sjoerg	uid_t           uid = (uid_t) - 1;
68620253Sjoerg	struct carg    *a_uid = getarg(args, 'u');
68720253Sjoerg
68820253Sjoerg	/*
68920253Sjoerg	 * Check the given uid, if any
69020253Sjoerg	 */
69120253Sjoerg	if (a_uid != NULL) {
69220253Sjoerg		uid = (uid_t) atol(a_uid->val);
69320253Sjoerg
69444229Sdavidn		if ((pwd = GETPWUID(uid)) != NULL && getarg(args, 'o') == NULL)
69530259Scharnier			errx(EX_DATAERR, "uid `%ld' has already been allocated", (long) pwd->pw_uid);
69620253Sjoerg	} else {
69720253Sjoerg		struct bitmap   bm;
69820253Sjoerg
69920253Sjoerg		/*
70020253Sjoerg		 * We need to allocate the next available uid under one of
70120253Sjoerg		 * two policies a) Grab the first unused uid b) Grab the
70220253Sjoerg		 * highest possible unused uid
70320253Sjoerg		 */
70420253Sjoerg		if (cnf->min_uid >= cnf->max_uid) {	/* Sanity
70520253Sjoerg							 * claus^H^H^H^Hheck */
70620253Sjoerg			cnf->min_uid = 1000;
70720253Sjoerg			cnf->max_uid = 32000;
70820253Sjoerg		}
70920253Sjoerg		bm = bm_alloc(cnf->max_uid - cnf->min_uid + 1);
71020253Sjoerg
71120253Sjoerg		/*
71220253Sjoerg		 * Now, let's fill the bitmap from the password file
71320253Sjoerg		 */
71444229Sdavidn		SETPWENT();
71544229Sdavidn		while ((pwd = GETPWENT()) != NULL)
71644229Sdavidn			if (pwd->pw_uid >= (uid_t) cnf->min_uid && pwd->pw_uid <= (uid_t) cnf->max_uid)
71720253Sjoerg				bm_setbit(&bm, pwd->pw_uid - cnf->min_uid);
71844229Sdavidn		ENDPWENT();
71920253Sjoerg
72020253Sjoerg		/*
72120253Sjoerg		 * Then apply the policy, with fallback to reuse if necessary
72220253Sjoerg		 */
72320253Sjoerg		if (cnf->reuse_uids || (uid = (uid_t) (bm_lastset(&bm) + cnf->min_uid + 1)) > cnf->max_uid)
72420253Sjoerg			uid = (uid_t) (bm_firstunset(&bm) + cnf->min_uid);
72520253Sjoerg
72620253Sjoerg		/*
72720253Sjoerg		 * Another sanity check
72820253Sjoerg		 */
72920253Sjoerg		if (uid < cnf->min_uid || uid > cnf->max_uid)
73030259Scharnier			errx(EX_SOFTWARE, "unable to allocate a new uid - range fully used");
73120253Sjoerg		bm_dealloc(&bm);
73220253Sjoerg	}
73320253Sjoerg	return uid;
73420253Sjoerg}
73520253Sjoerg
73620253Sjoerg
73720253Sjoergstatic          uid_t
73820253Sjoergpw_gidpolicy(struct userconf * cnf, struct cargs * args, char *nam, gid_t prefer)
73920253Sjoerg{
74020253Sjoerg	struct group   *grp;
74120253Sjoerg	gid_t           gid = (uid_t) - 1;
74220253Sjoerg	struct carg    *a_gid = getarg(args, 'g');
74320253Sjoerg
74420253Sjoerg	/*
74520253Sjoerg	 * If no arg given, see if default can help out
74620253Sjoerg	 */
74720253Sjoerg	if (a_gid == NULL && cnf->default_group && *cnf->default_group)
74820253Sjoerg		a_gid = addarg(args, 'g', cnf->default_group);
74920253Sjoerg
75020253Sjoerg	/*
75120253Sjoerg	 * Check the given gid, if any
75220253Sjoerg	 */
75344229Sdavidn	SETGRENT();
75420253Sjoerg	if (a_gid != NULL) {
75544229Sdavidn		if ((grp = GETGRNAM(a_gid->val)) == NULL) {
75620253Sjoerg			gid = (gid_t) atol(a_gid->val);
75744229Sdavidn			if ((gid == 0 && !isdigit(*a_gid->val)) || (grp = GETGRGID(gid)) == NULL)
75830259Scharnier				errx(EX_NOUSER, "group `%s' is not defined", a_gid->val);
75920253Sjoerg		}
76020253Sjoerg		gid = grp->gr_gid;
76144229Sdavidn	} else if ((grp = GETGRNAM(nam)) != NULL && grp->gr_mem[0] == NULL) {
76220267Sjoerg		gid = grp->gr_gid;  /* Already created? Use it anyway... */
76320253Sjoerg	} else {
76420253Sjoerg		struct cargs    grpargs;
76520253Sjoerg		char            tmp[32];
76620253Sjoerg
76720253Sjoerg		LIST_INIT(&grpargs);
76820253Sjoerg		addarg(&grpargs, 'n', nam);
76920253Sjoerg
77020253Sjoerg		/*
77120253Sjoerg		 * We need to auto-create a group with the user's name. We
77220253Sjoerg		 * can send all the appropriate output to our sister routine
77320253Sjoerg		 * bit first see if we can create a group with gid==uid so we
77420253Sjoerg		 * can keep the user and group ids in sync. We purposely do
77520253Sjoerg		 * NOT check the gid range if we can force the sync. If the
77620253Sjoerg		 * user's name dups an existing group, then the group add
77720253Sjoerg		 * function will happily handle that case for us and exit.
77820253Sjoerg		 */
77944229Sdavidn		if (GETGRGID(prefer) == NULL) {
78020253Sjoerg			sprintf(tmp, "%lu", (unsigned long) prefer);
78120253Sjoerg			addarg(&grpargs, 'g', tmp);
78220253Sjoerg		}
78320267Sjoerg		if (getarg(args, 'N'))
78420267Sjoerg		{
78520267Sjoerg			addarg(&grpargs, 'N', NULL);
78620267Sjoerg			addarg(&grpargs, 'q', NULL);
78720267Sjoerg			gid = pw_group(cnf, M_NEXT, &grpargs);
78820267Sjoerg		}
78920267Sjoerg		else
79020267Sjoerg		{
79120267Sjoerg			pw_group(cnf, M_ADD, &grpargs);
79244229Sdavidn			if ((grp = GETGRNAM(nam)) != NULL)
79320267Sjoerg				gid = grp->gr_gid;
79420267Sjoerg		}
79520253Sjoerg		a_gid = grpargs.lh_first;
79620253Sjoerg		while (a_gid != NULL) {
79720253Sjoerg			struct carg    *t = a_gid->list.le_next;
79820253Sjoerg			LIST_REMOVE(a_gid, list);
79920253Sjoerg			a_gid = t;
80020253Sjoerg		}
80120253Sjoerg	}
80244229Sdavidn	ENDGRENT();
80320253Sjoerg	return gid;
80420253Sjoerg}
80520253Sjoerg
80620253Sjoerg
80720253Sjoergstatic          time_t
80820253Sjoergpw_pwdpolicy(struct userconf * cnf, struct cargs * args)
80920253Sjoerg{
81020253Sjoerg	time_t          result = 0;
81120253Sjoerg	time_t          now = time(NULL);
81227831Sdavidn	struct carg    *arg = getarg(args, 'p');
81320253Sjoerg
81420253Sjoerg	if (arg != NULL) {
81520253Sjoerg		if ((result = parse_date(now, arg->val)) == now)
81630259Scharnier			errx(EX_DATAERR, "invalid date/time `%s'", arg->val);
81720253Sjoerg	} else if (cnf->password_days > 0)
81820253Sjoerg		result = now + ((long) cnf->password_days * 86400L);
81920253Sjoerg	return result;
82020253Sjoerg}
82120253Sjoerg
82220253Sjoerg
82320253Sjoergstatic          time_t
82420253Sjoergpw_exppolicy(struct userconf * cnf, struct cargs * args)
82520253Sjoerg{
82620253Sjoerg	time_t          result = 0;
82720253Sjoerg	time_t          now = time(NULL);
82820253Sjoerg	struct carg    *arg = getarg(args, 'e');
82920253Sjoerg
83020253Sjoerg	if (arg != NULL) {
83120253Sjoerg		if ((result = parse_date(now, arg->val)) == now)
83230259Scharnier			errx(EX_DATAERR, "invalid date/time `%s'", arg->val);
83320253Sjoerg	} else if (cnf->expire_days > 0)
83420253Sjoerg		result = now + ((long) cnf->expire_days * 86400L);
83520253Sjoerg	return result;
83620253Sjoerg}
83720253Sjoerg
83820253Sjoerg
83920253Sjoergstatic char    *
84020253Sjoergpw_homepolicy(struct userconf * cnf, struct cargs * args, char const * user)
84120253Sjoerg{
84220253Sjoerg	struct carg    *arg = getarg(args, 'd');
84320253Sjoerg
84420253Sjoerg	if (arg)
84520253Sjoerg		return arg->val;
84620253Sjoerg	else {
84720253Sjoerg		static char     home[128];
84820253Sjoerg
84920253Sjoerg		if (cnf->home == NULL || *cnf->home == '\0')
85030259Scharnier			errx(EX_CONFIG, "no base home directory set");
85120253Sjoerg		sprintf(home, "%s/%s", cnf->home, user);
85220253Sjoerg		return home;
85320253Sjoerg	}
85420253Sjoerg}
85520253Sjoerg
85620253Sjoergstatic char    *
85720253Sjoergshell_path(char const * path, char *shells[], char *sh)
85820253Sjoerg{
85920253Sjoerg	if (sh != NULL && (*sh == '/' || *sh == '\0'))
86020253Sjoerg		return sh;	/* specified full path or forced none */
86120253Sjoerg	else {
86220253Sjoerg		char           *p;
86320253Sjoerg		char            paths[_UC_MAXLINE];
86420253Sjoerg
86520253Sjoerg		/*
86620253Sjoerg		 * We need to search paths
86720253Sjoerg		 */
86820253Sjoerg		strncpy(paths, path, sizeof paths);
86920253Sjoerg		paths[sizeof paths - 1] = '\0';
87020253Sjoerg		for (p = strtok(paths, ": \t\r\n"); p != NULL; p = strtok(NULL, ": \t\r\n")) {
87120253Sjoerg			int             i;
87220253Sjoerg			static char     shellpath[256];
87320253Sjoerg
87420253Sjoerg			if (sh != NULL) {
87520253Sjoerg				sprintf(shellpath, "%s/%s", p, sh);
87620253Sjoerg				if (access(shellpath, X_OK) == 0)
87720253Sjoerg					return shellpath;
87820253Sjoerg			} else
87920253Sjoerg				for (i = 0; i < _UC_MAXSHELLS && shells[i] != NULL; i++) {
88020253Sjoerg					sprintf(shellpath, "%s/%s", p, shells[i]);
88120253Sjoerg					if (access(shellpath, X_OK) == 0)
88220253Sjoerg						return shellpath;
88320253Sjoerg				}
88420253Sjoerg		}
88520253Sjoerg		if (sh == NULL)
88630259Scharnier			errx(EX_OSFILE, "can't find shell `%s' in shell paths", sh);
88730259Scharnier		errx(EX_CONFIG, "no default shell available or defined");
88820253Sjoerg		return NULL;
88920253Sjoerg	}
89020253Sjoerg}
89120253Sjoerg
89220253Sjoerg
89320253Sjoergstatic char    *
89420253Sjoergpw_shellpolicy(struct userconf * cnf, struct cargs * args, char *newshell)
89520253Sjoerg{
89620253Sjoerg	char           *sh = newshell;
89720253Sjoerg	struct carg    *arg = getarg(args, 's');
89820253Sjoerg
89920253Sjoerg	if (newshell == NULL && arg != NULL)
90020253Sjoerg		sh = arg->val;
90120253Sjoerg	return shell_path(cnf->shelldir, cnf->shells, sh ? sh : cnf->shell_default);
90220253Sjoerg}
90320253Sjoerg
90420253Sjoergstatic char const chars[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.";
90520253Sjoerg
90620253Sjoergchar           *
90720253Sjoergpw_pwcrypt(char *password)
90820253Sjoerg{
90920253Sjoerg	int             i;
91020253Sjoerg	char            salt[12];
91120253Sjoerg
91220253Sjoerg	static char     buf[256];
91320253Sjoerg
91420253Sjoerg	/*
91520253Sjoerg	 * Calculate a salt value
91620253Sjoerg	 */
91724214Sache	if (!randinit) {
91824214Sache		randinit = 1;
91924214Sache#ifdef __FreeBSD__
92026626Sache		srandomdev();
92126626Sache#else
92226626Sache		srandom((unsigned long) (time(NULL) ^ getpid()));
92324214Sache#endif
92424214Sache	}
92520253Sjoerg	for (i = 0; i < 8; i++)
92620253Sjoerg		salt[i] = chars[random() % 63];
92720253Sjoerg	salt[i] = '\0';
92820253Sjoerg
92920253Sjoerg	return strcpy(buf, crypt(password, salt));
93020253Sjoerg}
93120253Sjoerg
93220590Sdavidn#if defined(USE_MD5RAND)
93320555Sdavidnu_char *
93420590Sdavidnpw_getrand(u_char *buf, int len)	/* cryptographically secure rng */
93520555Sdavidn{
93620590Sdavidn	int i;
93720590Sdavidn	for (i=0;i<len;i+=16) {
93820590Sdavidn		u_char ubuf[16];
93920590Sdavidn
94020590Sdavidn		MD5_CTX md5_ctx;
94120590Sdavidn		struct timeval tv, tvo;
94220590Sdavidn		struct rusage ru;
94320590Sdavidn		int n=0;
94420590Sdavidn		int t;
94520590Sdavidn
94620590Sdavidn		MD5Init (&md5_ctx);
94720590Sdavidn		t=getpid();
94820590Sdavidn		MD5Update (&md5_ctx, (u_char*)&t, sizeof t);
94920590Sdavidn		t=getppid();
95020590Sdavidn		MD5Update (&md5_ctx, (u_char*)&t, sizeof t);
95120590Sdavidn		gettimeofday (&tvo, NULL);
95220590Sdavidn		do {
95320590Sdavidn			getrusage (RUSAGE_SELF, &ru);
95420590Sdavidn			MD5Update (&md5_ctx, (u_char*)&ru, sizeof ru);
95520590Sdavidn			gettimeofday (&tv, NULL);
95620590Sdavidn			MD5Update (&md5_ctx, (u_char*)&tv, sizeof tv);
95720590Sdavidn		} while (n++<20 || tv.tv_usec-tvo.tv_usec<100*1000);
95820590Sdavidn		MD5Final (ubuf, &md5_ctx);
95920590Sdavidn		memcpy(buf+i, ubuf, MIN(16, len-n));
96020590Sdavidn	}
96120590Sdavidn	return buf;
96220555Sdavidn}
96320253Sjoerg
96420590Sdavidn#else	/* Portable version */
96520590Sdavidn
96620590Sdavidnstatic u_char *
96720590Sdavidnpw_getrand(u_char *buf, int len)
96820590Sdavidn{
96920590Sdavidn	int i;
97020590Sdavidn
97120590Sdavidn	for (i = 0; i < len; i++) {
97224214Sache		unsigned long val = random();
97320590Sdavidn		/* Use all bits in the random value */
97420590Sdavidn		buf[i]=(u_char)((val >> 24) ^ (val >> 16) ^ (val >> 8) ^ val);
97520590Sdavidn	}
97620590Sdavidn	return buf;
97720590Sdavidn}
97820590Sdavidn
97920590Sdavidn#endif
98020590Sdavidn
98120253Sjoergstatic char    *
98220253Sjoergpw_password(struct userconf * cnf, struct cargs * args, char const * user)
98320253Sjoerg{
98420253Sjoerg	int             i, l;
98520253Sjoerg	char            pwbuf[32];
98620555Sdavidn	u_char		rndbuf[sizeof pwbuf];
98720253Sjoerg
98820253Sjoerg	switch (cnf->default_password) {
98920253Sjoerg	case -1:		/* Random password */
99024214Sache		if (!randinit) {
99124214Sache			randinit = 1;
99224214Sache#ifdef __FreeBSD__
99326626Sache			srandomdev();
99426626Sache#else
99526626Sache			srandom((unsigned long) (time(NULL) ^ getpid()));
99624214Sache#endif
99724214Sache		}
99820253Sjoerg		l = (random() % 8 + 8);	/* 8 - 16 chars */
99920555Sdavidn		pw_getrand(rndbuf, l);
100020253Sjoerg		for (i = 0; i < l; i++)
100142245Sbillf			pwbuf[i] = chars[rndbuf[i] % (sizeof(chars)-1)];
100220253Sjoerg		pwbuf[i] = '\0';
100320253Sjoerg
100420253Sjoerg		/*
100520253Sjoerg		 * We give this information back to the user
100620253Sjoerg		 */
100720267Sjoerg		if (getarg(args, 'h') == NULL && getarg(args, 'N') == NULL) {
100820712Sdavidn			if (isatty(1))
100920712Sdavidn				printf("Password for '%s' is: ", user);
101020253Sjoerg			printf("%s\n", pwbuf);
101120253Sjoerg			fflush(stdout);
101220253Sjoerg		}
101320253Sjoerg		break;
101420253Sjoerg
101520253Sjoerg	case -2:		/* No password at all! */
101620253Sjoerg		return "";
101720253Sjoerg
101820253Sjoerg	case 0:		/* No login - default */
101920253Sjoerg	default:
102020253Sjoerg		return "*";
102120253Sjoerg
102220253Sjoerg	case 1:		/* user's name */
102320253Sjoerg		strncpy(pwbuf, user, sizeof pwbuf);
102420253Sjoerg		pwbuf[sizeof pwbuf - 1] = '\0';
102520253Sjoerg		break;
102620253Sjoerg	}
102720253Sjoerg	return pw_pwcrypt(pwbuf);
102820253Sjoerg}
102920253Sjoerg
103020253Sjoerg
103120253Sjoergstatic int
103244386Sdavidnprint_user(struct passwd * pwd, int pretty, int v7)
103320253Sjoerg{
103420253Sjoerg	if (!pretty) {
103520253Sjoerg		char            buf[_UC_MAXLINE];
103620253Sjoerg
103744386Sdavidn		fmtpwentry(buf, pwd, v7 ? PWF_PASSWD : PWF_STANDARD);
103820253Sjoerg		fputs(buf, stdout);
103920253Sjoerg	} else {
104020267Sjoerg		int		j;
104120253Sjoerg		char           *p;
104244229Sdavidn		struct group   *grp = GETGRGID(pwd->pw_gid);
104320253Sjoerg		char            uname[60] = "User &", office[60] = "[None]",
104420253Sjoerg		                wphone[60] = "[None]", hphone[60] = "[None]";
104520590Sdavidn		char		acexpire[32] = "[None]", pwexpire[32] = "[None]";
104620590Sdavidn		struct tm *    tptr;
104720253Sjoerg
104820253Sjoerg		if ((p = strtok(pwd->pw_gecos, ",")) != NULL) {
104920253Sjoerg			strncpy(uname, p, sizeof uname);
105020253Sjoerg			uname[sizeof uname - 1] = '\0';
105120253Sjoerg			if ((p = strtok(NULL, ",")) != NULL) {
105220253Sjoerg				strncpy(office, p, sizeof office);
105320253Sjoerg				office[sizeof office - 1] = '\0';
105420253Sjoerg				if ((p = strtok(NULL, ",")) != NULL) {
105520253Sjoerg					strncpy(wphone, p, sizeof wphone);
105620253Sjoerg					wphone[sizeof wphone - 1] = '\0';
105720253Sjoerg					if ((p = strtok(NULL, "")) != NULL) {
105820253Sjoerg						strncpy(hphone, p, sizeof hphone);
105920253Sjoerg						hphone[sizeof hphone - 1] = '\0';
106020253Sjoerg					}
106120253Sjoerg				}
106220253Sjoerg			}
106320253Sjoerg		}
106420253Sjoerg		/*
106520253Sjoerg		 * Handle '&' in gecos field
106620253Sjoerg		 */
106720253Sjoerg		if ((p = strchr(uname, '&')) != NULL) {
106820253Sjoerg			int             l = strlen(pwd->pw_name);
106920253Sjoerg			int             m = strlen(p);
107020253Sjoerg
107120253Sjoerg			memmove(p + l, p + 1, m);
107220253Sjoerg			memmove(p, pwd->pw_name, l);
107320253Sjoerg			*p = (char) toupper(*p);
107420253Sjoerg		}
107520590Sdavidn		if (pwd->pw_expire > (time_t)0 && (tptr = localtime(&pwd->pw_expire)) != NULL)
107620590Sdavidn		  strftime(acexpire, sizeof acexpire, "%c", tptr);
107720590Sdavidn		if (pwd->pw_change > (time_t)9 && (tptr = localtime(&pwd->pw_change)) != NULL)
107820590Sdavidn		  strftime(pwexpire, sizeof pwexpire, "%c", tptr);
107922394Sdavidn		printf("Login Name: %-15s   #%-12ld Group: %-15s   #%ld\n"
108020747Sdavidn		       " Full Name: %s\n"
108120747Sdavidn		       "      Home: %-26.26s      Class: %s\n"
108220747Sdavidn		       "     Shell: %-26.26s     Office: %s\n"
108320747Sdavidn		       "Work Phone: %-26.26s Home Phone: %s\n"
108420747Sdavidn		       "Acc Expire: %-26.26s Pwd Expire: %s\n",
108520253Sjoerg		       pwd->pw_name, (long) pwd->pw_uid,
108620253Sjoerg		       grp ? grp->gr_name : "(invalid)", (long) pwd->pw_gid,
108720253Sjoerg		       uname, pwd->pw_dir, pwd->pw_class,
108820590Sdavidn		       pwd->pw_shell, office, wphone, hphone,
108920590Sdavidn		       acexpire, pwexpire);
109044229Sdavidn	        SETGRENT();
109120267Sjoerg		j = 0;
109244229Sdavidn		while ((grp=GETGRENT()) != NULL)
109320267Sjoerg		{
109420267Sjoerg			int     i = 0;
109520747Sdavidn			while (grp->gr_mem[i] != NULL)
109620267Sjoerg			{
109720267Sjoerg				if (strcmp(grp->gr_mem[i], pwd->pw_name)==0)
109820267Sjoerg				{
109920747Sdavidn					printf(j++ == 0 ? "    Groups: %s" : ",%s", grp->gr_name);
110020267Sjoerg					break;
110120267Sjoerg				}
110220267Sjoerg				++i;
110320267Sjoerg			}
110420267Sjoerg		}
110544229Sdavidn		ENDGRENT();
110620267Sjoerg		printf("%s\n", j ? "\n" : "");
110720253Sjoerg	}
110820267Sjoerg	return EXIT_SUCCESS;
110920253Sjoerg}
111020253Sjoerg
111120679Sdavidnchar    *
111220679Sdavidnpw_checkname(u_char *name, int gecos)
111320253Sjoerg{
111420253Sjoerg	int             l = 0;
111521052Sdavidn	char const     *notch = gecos ? ":!@" : " ,\t:+&#%$^()!@~*?<>=|\\/\"";
111620253Sjoerg
111720253Sjoerg	while (name[l]) {
111821052Sdavidn		if (strchr(notch, name[l]) != NULL || name[l] < ' ' || name[l] == 127 ||
111921052Sdavidn			(!gecos && l==0 && name[l] == '-') ||	/* leading '-' */
112021052Sdavidn			(!gecos && name[l] & 0x80))	/* 8-bit */
112130259Scharnier			errx(EX_DATAERR, (name[l] >= ' ' && name[l] < 127)
112230259Scharnier					    ? "invalid character `%c' in field"
112330259Scharnier					    : "invalid character 0x%02x in field",
112420325Sjoerg					    name[l]);
112520253Sjoerg		++l;
112620253Sjoerg	}
112722394Sdavidn	if (!gecos && l > LOGNAMESIZE)
112830259Scharnier		errx(EX_DATAERR, "name too long `%s'", name);
112920679Sdavidn	return (char *)name;
113020253Sjoerg}
113120253Sjoerg
113220253Sjoerg
113320253Sjoergstatic void
113420253Sjoergrmat(uid_t uid)
113520253Sjoerg{
113620253Sjoerg	DIR            *d = opendir("/var/at/jobs");
113720253Sjoerg
113820253Sjoerg	if (d != NULL) {
113920253Sjoerg		struct dirent  *e;
114020253Sjoerg
114120253Sjoerg		while ((e = readdir(d)) != NULL) {
114220253Sjoerg			struct stat     st;
114320253Sjoerg
114420253Sjoerg			if (strncmp(e->d_name, ".lock", 5) != 0 &&
114520253Sjoerg			    stat(e->d_name, &st) == 0 &&
114620253Sjoerg			    !S_ISDIR(st.st_mode) &&
114720253Sjoerg			    st.st_uid == uid) {
114820253Sjoerg				char            tmp[MAXPATHLEN];
114920253Sjoerg
115020253Sjoerg				sprintf(tmp, "/usr/bin/atrm %s", e->d_name);
115120253Sjoerg				system(tmp);
115220253Sjoerg			}
115320253Sjoerg		}
115420253Sjoerg		closedir(d);
115520253Sjoerg	}
115620253Sjoerg}
115720747Sdavidn
115820747Sdavidnstatic void
115920747Sdavidnrmskey(char const * name)
116020747Sdavidn{
116120747Sdavidn	static const char etcskey[] = "/etc/skeykeys";
116221052Sdavidn	FILE   *fp = fopen(etcskey, "r+");
116320747Sdavidn
116421052Sdavidn	if (fp != NULL) {
116521052Sdavidn		char	tmp[1024];
116621052Sdavidn		off_t	atofs = 0;
116721052Sdavidn		int	length = strlen(name);
116820747Sdavidn
116921052Sdavidn		while (fgets(tmp, sizeof tmp, fp) != NULL) {
117021052Sdavidn			if (strncmp(name, tmp, length) == 0 && tmp[length]==' ') {
117121052Sdavidn				if (fseek(fp, atofs, SEEK_SET) == 0) {
117221052Sdavidn					fwrite("#", 1, 1, fp);	/* Comment username out */
117321052Sdavidn				}
117421052Sdavidn				break;
117520747Sdavidn			}
117621052Sdavidn			atofs = ftell(fp);
117720747Sdavidn		}
117821052Sdavidn		/*
117921052Sdavidn		 * If we got an error of any sort, don't update!
118021052Sdavidn		 */
118121052Sdavidn		fclose(fp);
118220747Sdavidn	}
118320747Sdavidn}
118420747Sdavidn
1185