pw_user.c revision 52527
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 52527 1999-10-26 15:07:20Z 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{
10652527Sdavidn	int	        rc, edited = 0;
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
21020253Sjoerg	if ((arg = getarg(args, 'e')) != NULL)
21120253Sjoerg		cnf->expire_days = atoi(arg->val);
21220253Sjoerg
21321330Sdavidn	if ((arg = getarg(args, 'y')) != NULL)
21421330Sdavidn		cnf->nispasswd = arg->val;
21521330Sdavidn
21620253Sjoerg	if ((arg = getarg(args, 'p')) != NULL && arg->val)
21720253Sjoerg		cnf->password_days = atoi(arg->val);
21820253Sjoerg
21920253Sjoerg	if ((arg = getarg(args, 'g')) != NULL) {
22020253Sjoerg		p = arg->val;
22144229Sdavidn		if ((grp = GETGRNAM(p)) == NULL) {
22244229Sdavidn			if (!isdigit(*p) || (grp = GETGRGID((gid_t) atoi(p))) == NULL)
22330259Scharnier				errx(EX_NOUSER, "group `%s' does not exist", p);
22420253Sjoerg		}
22520253Sjoerg		cnf->default_group = newstr(grp->gr_name);
22620253Sjoerg	}
22720253Sjoerg	if ((arg = getarg(args, 'L')) != NULL)
22820679Sdavidn		cnf->default_class = pw_checkname((u_char *)arg->val, 0);
22920253Sjoerg
23020253Sjoerg	if ((arg = getarg(args, 'G')) != NULL && arg->val) {
23152527Sdavidn		int i = 0;
23220253Sjoerg
23320747Sdavidn		for (p = strtok(arg->val, ", \t"); p != NULL; p = strtok(NULL, ", \t")) {
23444229Sdavidn			if ((grp = GETGRNAM(p)) == NULL) {
23544229Sdavidn				if (!isdigit(*p) || (grp = GETGRGID((gid_t) atoi(p))) == NULL)
23630259Scharnier					errx(EX_NOUSER, "group `%s' does not exist", p);
23720253Sjoerg			}
23820747Sdavidn			if (extendarray(&cnf->groups, &cnf->numgroups, i + 2) != -1)
23920747Sdavidn				cnf->groups[i++] = newstr(grp->gr_name);
24020253Sjoerg		}
24120747Sdavidn		while (i < cnf->numgroups)
24220253Sjoerg			cnf->groups[i++] = NULL;
24320253Sjoerg	}
24452527Sdavidn
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	}
24952527Sdavidn
25020253Sjoerg	if ((arg = getarg(args, 's')) != NULL)
25120253Sjoerg		cnf->shell_default = arg->val;
25220253Sjoerg
25320253Sjoerg	if (mode == M_ADD && getarg(args, 'D')) {
25420253Sjoerg		if (getarg(args, 'n') != NULL)
25530259Scharnier			errx(EX_DATAERR, "can't combine `-D' with `-n name'");
25620253Sjoerg		if ((arg = getarg(args, 'u')) != NULL && (p = strtok(arg->val, ", \t")) != NULL) {
25720253Sjoerg			if ((cnf->min_uid = (uid_t) atoi(p)) == 0)
25820253Sjoerg				cnf->min_uid = 1000;
25920253Sjoerg			if ((p = strtok(NULL, " ,\t")) == NULL || (cnf->max_uid = (uid_t) atoi(p)) < cnf->min_uid)
26020253Sjoerg				cnf->max_uid = 32000;
26120253Sjoerg		}
26220253Sjoerg		if ((arg = getarg(args, 'i')) != NULL && (p = strtok(arg->val, ", \t")) != NULL) {
26320253Sjoerg			if ((cnf->min_gid = (gid_t) atoi(p)) == 0)
26420253Sjoerg				cnf->min_gid = 1000;
26520253Sjoerg			if ((p = strtok(NULL, " ,\t")) == NULL || (cnf->max_gid = (gid_t) atoi(p)) < cnf->min_gid)
26620253Sjoerg				cnf->max_gid = 32000;
26720253Sjoerg		}
26820253Sjoerg		if ((arg = getarg(args, 'w')) != NULL)
26920253Sjoerg			cnf->default_password = boolean_val(arg->val, cnf->default_password);
27020253Sjoerg
27120253Sjoerg		arg = getarg(args, 'C');
27220253Sjoerg		if (write_userconfig(arg ? arg->val : NULL))
27320267Sjoerg			return EXIT_SUCCESS;
27430259Scharnier		warn("config update");
27520267Sjoerg		return EX_IOERR;
27620253Sjoerg	}
27752527Sdavidn
27820253Sjoerg	if (mode == M_PRINT && getarg(args, 'a')) {
27920267Sjoerg		int             pretty = getarg(args, 'P') != NULL;
28044386Sdavidn		int		v7 = getarg(args, '7') != NULL;
28120253Sjoerg
28244229Sdavidn		SETPWENT();
28344229Sdavidn		while ((pwd = GETPWENT()) != NULL)
28444386Sdavidn			print_user(pwd, pretty, v7);
28544229Sdavidn		ENDPWENT();
28620267Sjoerg		return EXIT_SUCCESS;
28720253Sjoerg	}
28852527Sdavidn
28920253Sjoerg	if ((a_name = getarg(args, 'n')) != NULL)
29044229Sdavidn		pwd = GETPWNAM(pw_checkname((u_char *)a_name->val, 0));
29120253Sjoerg	a_uid = getarg(args, 'u');
29220253Sjoerg
29320253Sjoerg	if (a_uid == NULL) {
29420253Sjoerg		if (a_name == NULL)
29530259Scharnier			errx(EX_DATAERR, "user name or id required");
29620253Sjoerg
29720253Sjoerg		/*
29820253Sjoerg		 * Determine whether 'n' switch is name or uid - we don't
29920253Sjoerg		 * really don't really care which we have, but we need to
30020253Sjoerg		 * know.
30120253Sjoerg		 */
30243780Sdes		if (mode != M_ADD && pwd == NULL
30343780Sdes		    && strspn(a_name->val, "0123456789") == strlen(a_name->val)
30443780Sdes		    && atoi(a_name->val) > 0) {	/* Assume uid */
30520253Sjoerg			(a_uid = a_name)->ch = 'u';
30620253Sjoerg			a_name = NULL;
30720253Sjoerg		}
30820253Sjoerg	}
30952527Sdavidn
31020253Sjoerg	/*
31120253Sjoerg	 * Update, delete & print require that the user exists
31220253Sjoerg	 */
31352512Sdavidn	if (mode == M_UPDATE || mode == M_DELETE ||
31452512Sdavidn	    mode == M_PRINT  || mode == M_LOCK   || mode == M_UNLOCK) {
31552527Sdavidn
31620253Sjoerg		if (a_name == NULL && pwd == NULL)	/* Try harder */
31744229Sdavidn			pwd = GETPWUID(atoi(a_uid->val));
31820253Sjoerg
31920253Sjoerg		if (pwd == NULL) {
32020253Sjoerg			if (mode == M_PRINT && getarg(args, 'F')) {
32120253Sjoerg				fakeuser.pw_name = a_name ? a_name->val : "nouser";
32220253Sjoerg				fakeuser.pw_uid = a_uid ? (uid_t) atol(a_uid->val) : -1;
32344386Sdavidn				return print_user(&fakeuser,
32444386Sdavidn						  getarg(args, 'P') != NULL,
32544386Sdavidn						  getarg(args, '7') != NULL);
32620253Sjoerg			}
32720253Sjoerg			if (a_name == NULL)
32830259Scharnier				errx(EX_NOUSER, "no such uid `%s'", a_uid->val);
32930259Scharnier			errx(EX_NOUSER, "no such user `%s'", a_name->val);
33020253Sjoerg		}
33152527Sdavidn
33220253Sjoerg		if (a_name == NULL)	/* May be needed later */
33320253Sjoerg			a_name = addarg(args, 'n', newstr(pwd->pw_name));
33420253Sjoerg
33520253Sjoerg		/*
33652512Sdavidn		 * The M_LOCK and M_UNLOCK functions simply add or remove
33752512Sdavidn		 * a "*LOCKED*" prefix from in front of the password to
33852512Sdavidn		 * prevent it decoding correctly, and therefore prevents
33952512Sdavidn		 * access. Of course, this only prevents access via
34052512Sdavidn		 * password authentication (not ssh, kerberos or any
34152512Sdavidn		 * other method that does not use the UNIX password) but
34252512Sdavidn		 * that is a known limitation.
34352512Sdavidn		 */
34452512Sdavidn
34552512Sdavidn		if (mode == M_LOCK) {
34652512Sdavidn			if (strncmp(pwd->pw_passwd, locked_str, sizeof(locked_str)-1) == 0)
34752512Sdavidn				errx(EX_DATAERR, "user '%s' is already locked", pwd->pw_name);
34852512Sdavidn			passtmp = malloc(strlen(pwd->pw_passwd) + sizeof(locked_str));
34952512Sdavidn			if (passtmp == NULL)	/* disaster */
35052512Sdavidn				errx(EX_UNAVAILABLE, "out of memory");
35152512Sdavidn			strcpy(passtmp, locked_str);
35252512Sdavidn			strcat(passtmp, pwd->pw_passwd);
35352512Sdavidn			pwd->pw_passwd = passtmp;
35452527Sdavidn			edited = 1;
35552512Sdavidn		} else if (mode == M_UNLOCK) {
35652512Sdavidn			if (strncmp(pwd->pw_passwd, locked_str, sizeof(locked_str)-1) != 0)
35752512Sdavidn				errx(EX_DATAERR, "user '%s' is not locked", pwd->pw_name);
35852512Sdavidn			pwd->pw_passwd += sizeof(locked_str)-1;
35952527Sdavidn			edited = 1;
36052527Sdavidn		} else if (mode == M_DELETE) {
36152527Sdavidn			/*
36252527Sdavidn			 * Handle deletions now
36352527Sdavidn			 */
36420253Sjoerg			char            file[MAXPATHLEN];
36520253Sjoerg			char            home[MAXPATHLEN];
36620253Sjoerg			uid_t           uid = pwd->pw_uid;
36720253Sjoerg
36820253Sjoerg			if (strcmp(pwd->pw_name, "root") == 0)
36930259Scharnier				errx(EX_DATAERR, "cannot remove user 'root'");
37020253Sjoerg
37144229Sdavidn			if (!PWALTDIR()) {
37244229Sdavidn				/*
37344229Sdavidn				 * Remove skey record from /etc/skeykeys
37444229Sdavidn		        	 */
37520747Sdavidn
37644229Sdavidn				rmskey(pwd->pw_name);
37720747Sdavidn
37844229Sdavidn				/*
37944229Sdavidn				 * Remove crontabs
38044229Sdavidn				 */
38144229Sdavidn				sprintf(file, "/var/cron/tabs/%s", pwd->pw_name);
38244229Sdavidn				if (access(file, F_OK) == 0) {
38344229Sdavidn					sprintf(file, "crontab -u %s -r", pwd->pw_name);
38444229Sdavidn					system(file);
38544229Sdavidn				}
38620253Sjoerg			}
38720253Sjoerg			/*
38820253Sjoerg			 * Save these for later, since contents of pwd may be
38920253Sjoerg			 * invalidated by deletion
39020253Sjoerg			 */
39120253Sjoerg			sprintf(file, "%s/%s", _PATH_MAILDIR, pwd->pw_name);
39220253Sjoerg			strncpy(home, pwd->pw_dir, sizeof home);
39320253Sjoerg			home[sizeof home - 1] = '\0';
39420253Sjoerg
39552502Sdavidn			rc = delpwent(pwd);
39652502Sdavidn			if (rc == -1)
39752502Sdavidn				err(EX_IOERR, "user '%s' does not exist", pwd->pw_name);
39852502Sdavidn			else if (rc != 0) {
39952502Sdavidn				warnc(rc, "passwd update");
40052502Sdavidn				return EX_IOERR;
40152502Sdavidn			}
40221330Sdavidn
40352502Sdavidn			if (cnf->nispasswd && *cnf->nispasswd=='/') {
40452502Sdavidn				rc = delnispwent(cnf->nispasswd, a_name->val);
40552502Sdavidn				if (rc == -1)
40652502Sdavidn					warnx("WARNING: user '%s' does not exist in NIS passwd", pwd->pw_name);
40752502Sdavidn				else if (rc != 0)
40852502Sdavidn					warnc(rc, "WARNING: NIS passwd update");
40952502Sdavidn				/* non-fatal */
41052502Sdavidn			}
41152502Sdavidn
41220253Sjoerg			editgroups(a_name->val, NULL);
41320253Sjoerg
41420253Sjoerg			pw_log(cnf, mode, W_USER, "%s(%ld) account removed", a_name->val, (long) uid);
41520253Sjoerg
41644229Sdavidn			if (!PWALTDIR()) {
41744229Sdavidn				/*
41844229Sdavidn				 * Remove mail file
41944229Sdavidn				 */
42044229Sdavidn				remove(file);
42120253Sjoerg
42244229Sdavidn				/*
42344229Sdavidn				 * Remove at jobs
42444229Sdavidn				 */
42544229Sdavidn				if (getpwuid(uid) == NULL)
42644229Sdavidn					rmat(uid);
42720253Sjoerg
42844229Sdavidn				/*
42944229Sdavidn				 * Remove home directory and contents
43044229Sdavidn				 */
43144229Sdavidn				if (getarg(args, 'r') != NULL && *home == '/' && getpwuid(uid) == NULL) {
43244229Sdavidn					if (stat(home, &st) != -1) {
43344229Sdavidn						rm_r(home, uid);
43444229Sdavidn						pw_log(cnf, mode, W_USER, "%s(%ld) home '%s' %sremoved",
43544229Sdavidn						       a_name->val, (long) uid, home,
43644229Sdavidn						       stat(home, &st) == -1 ? "" : "not completely ");
43744229Sdavidn					}
43820253Sjoerg				}
43920253Sjoerg			}
44020267Sjoerg			return EXIT_SUCCESS;
44120253Sjoerg		} else if (mode == M_PRINT)
44244386Sdavidn			return print_user(pwd,
44344386Sdavidn					  getarg(args, 'P') != NULL,
44444386Sdavidn					  getarg(args, '7') != NULL);
44520253Sjoerg
44620253Sjoerg		/*
44720253Sjoerg		 * The rest is edit code
44820253Sjoerg		 */
44920253Sjoerg		if ((arg = getarg(args, 'l')) != NULL) {
45020253Sjoerg			if (strcmp(pwd->pw_name, "root") == 0)
45130259Scharnier				errx(EX_DATAERR, "can't rename `root' account");
45220679Sdavidn			pwd->pw_name = pw_checkname((u_char *)arg->val, 0);
45352527Sdavidn			edited = 1;
45420253Sjoerg		}
45552527Sdavidn
45620253Sjoerg		if ((arg = getarg(args, 'u')) != NULL && isdigit(*arg->val)) {
45720253Sjoerg			pwd->pw_uid = (uid_t) atol(arg->val);
45852527Sdavidn			edited = 1;
45920253Sjoerg			if (pwd->pw_uid != 0 && strcmp(pwd->pw_name, "root") == 0)
46030259Scharnier				errx(EX_DATAERR, "can't change uid of `root' account");
46120253Sjoerg			if (pwd->pw_uid == 0 && strcmp(pwd->pw_name, "root") != 0)
46230259Scharnier				warnx("WARNING: account `%s' will have a uid of 0 (superuser access!)", pwd->pw_name);
46320253Sjoerg		}
46420253Sjoerg
46552527Sdavidn		if ((arg = getarg(args, 'g')) != NULL && pwd->pw_uid != 0) {	/* Already checked this */
46652527Sdavidn			gid_t newgid = (gid_t) GETGRNAM(cnf->default_group)->gr_gid;
46752527Sdavidn			if (newgid != pwd->pw_gid) {
46852527Sdavidn				edited = 1;
46952527Sdavidn				pwd->pw_gid = pwd->pw_gid;
47052527Sdavidn			}
47152527Sdavidn		}
47252527Sdavidn
47320253Sjoerg		if ((arg = getarg(args, 'p')) != NULL) {
47452527Sdavidn			if (*arg->val == '\0' || strcmp(arg->val, "0") == 0) {
47552527Sdavidn				if (pwd->pw_change != 0) {
47652527Sdavidn					pwd->pw_change = 0;
47752527Sdavidn					edited = 1;
47852527Sdavidn				}
47952527Sdavidn			}
48020253Sjoerg			else {
48120253Sjoerg				time_t          now = time(NULL);
48220253Sjoerg				time_t          expire = parse_date(now, arg->val);
48320253Sjoerg
48420253Sjoerg				if (now == expire)
48530259Scharnier					errx(EX_DATAERR, "invalid password change date `%s'", arg->val);
48652527Sdavidn				if (pwd->pw_change != expire) {
48752527Sdavidn					pwd->pw_change = expire;
48852527Sdavidn					edited = 1;
48952527Sdavidn				}
49020253Sjoerg			}
49120253Sjoerg		}
49252527Sdavidn
49320267Sjoerg		if ((arg = getarg(args, 'e')) != NULL) {
49452527Sdavidn			if (*arg->val == '\0' || strcmp(arg->val, "0") == 0) {
49552527Sdavidn				if (pwd->pw_expire != 0) {
49652527Sdavidn					pwd->pw_expire = 0;
49752527Sdavidn					edited = 1;
49852527Sdavidn				}
49952527Sdavidn			}
50020253Sjoerg			else {
50120253Sjoerg				time_t          now = time(NULL);
50220253Sjoerg				time_t          expire = parse_date(now, arg->val);
50320253Sjoerg
50420253Sjoerg				if (now == expire)
50530259Scharnier					errx(EX_DATAERR, "invalid account expiry date `%s'", arg->val);
50652527Sdavidn				if (pwd->pw_expire != expire) {
50752527Sdavidn					pwd->pw_expire = expire;
50852527Sdavidn					edited = 1;
50952527Sdavidn				}
51020253Sjoerg			}
51120253Sjoerg		}
51220253Sjoerg
51352527Sdavidn		if ((arg = getarg(args, 's')) != NULL) {
51452527Sdavidn			char *shell = shell_path(cnf->shelldir, cnf->shells, arg->val);
51552527Sdavidn			if (shell == NULL)
51652527Sdavidn				shell = "";
51752527Sdavidn			if (strcmp(shell, pwd->pw_shell) != 0) {
51852527Sdavidn				pwd->pw_shell = shell;
51952527Sdavidn				edited = 1;
52052527Sdavidn			}
52152527Sdavidn		}
52220253Sjoerg
52352527Sdavidn		if (getarg(args, 'L')) {
52452527Sdavidn			if (cnf->default_class == NULL)
52552527Sdavidn				cnf->default_class = "";
52652527Sdavidn			if (strcmp(pwd->pw_class, cnf->default_class) != 0) {
52752527Sdavidn				pwd->pw_class = cnf->default_class;
52852527Sdavidn				edited = 1;
52952527Sdavidn			}
53052527Sdavidn		}
53152527Sdavidn
53220747Sdavidn		if ((arg  = getarg(args, 'd')) != NULL) {
53320747Sdavidn			if (stat(pwd->pw_dir = arg->val, &st) == -1) {
53420747Sdavidn				if (getarg(args, 'm') == NULL && strcmp(pwd->pw_dir, "/nonexistent") != 0)
53530259Scharnier				  warnx("WARNING: home `%s' does not exist", pwd->pw_dir);
53620747Sdavidn			} else if (!S_ISDIR(st.st_mode))
53730259Scharnier				warnx("WARNING: home `%s' is not a directory", pwd->pw_dir);
53820747Sdavidn		}
53920747Sdavidn
54052527Sdavidn		if ((arg = getarg(args, 'w')) != NULL && getarg(args, 'h') == NULL) {
54120267Sjoerg			pwd->pw_passwd = pw_password(cnf, args, pwd->pw_name);
54252527Sdavidn			edited = 1;
54352527Sdavidn		}
54420267Sjoerg
54520253Sjoerg	} else {
54652527Sdavidn
54752527Sdavidn		/*
54852527Sdavidn		 * Add code
54952527Sdavidn		 */
55052527Sdavidn
55120253Sjoerg		if (a_name == NULL)	/* Required */
55230259Scharnier			errx(EX_DATAERR, "login name required");
55344229Sdavidn		else if ((pwd = GETPWNAM(a_name->val)) != NULL)	/* Exists */
55430259Scharnier			errx(EX_DATAERR, "login name `%s' already exists", a_name->val);
55520253Sjoerg
55620253Sjoerg		/*
55720253Sjoerg		 * Now, set up defaults for a new user
55820253Sjoerg		 */
55920253Sjoerg		pwd = &fakeuser;
56020253Sjoerg		pwd->pw_name = a_name->val;
56120253Sjoerg		pwd->pw_class = cnf->default_class ? cnf->default_class : "";
56220253Sjoerg		pwd->pw_passwd = pw_password(cnf, args, pwd->pw_name);
56320253Sjoerg		pwd->pw_uid = pw_uidpolicy(cnf, args);
56420253Sjoerg		pwd->pw_gid = pw_gidpolicy(cnf, args, pwd->pw_name, (gid_t) pwd->pw_uid);
56520253Sjoerg		pwd->pw_change = pw_pwdpolicy(cnf, args);
56620253Sjoerg		pwd->pw_expire = pw_exppolicy(cnf, args);
56720253Sjoerg		pwd->pw_dir = pw_homepolicy(cnf, args, pwd->pw_name);
56820253Sjoerg		pwd->pw_shell = pw_shellpolicy(cnf, args, NULL);
56952527Sdavidn		edited = 1;
57020253Sjoerg
57120253Sjoerg		if (pwd->pw_uid == 0 && strcmp(pwd->pw_name, "root") != 0)
57230259Scharnier			warnx("WARNING: new account `%s' has a uid of 0 (superuser access!)", pwd->pw_name);
57320253Sjoerg	}
57420253Sjoerg
57520253Sjoerg	/*
57620253Sjoerg	 * Shared add/edit code
57720253Sjoerg	 */
57852527Sdavidn	if ((arg = getarg(args, 'c')) != NULL) {
57952527Sdavidn		char	*gecos = pw_checkname((u_char *)arg->val, 1);
58052527Sdavidn		if (strcmp(pwd->pw_gecos, gecos) != 0) {
58152527Sdavidn			pwd->pw_gecos = gecos;
58252527Sdavidn			edited = 1;
58352527Sdavidn		}
58452527Sdavidn	}
58520253Sjoerg
58620253Sjoerg	if ((arg = getarg(args, 'h')) != NULL) {
58720253Sjoerg		if (strcmp(arg->val, "-") == 0)
58820253Sjoerg			pwd->pw_passwd = "*";	/* No access */
58920253Sjoerg		else {
59020253Sjoerg			int             fd = atoi(arg->val);
59120253Sjoerg			int             b;
59220253Sjoerg			int             istty = isatty(fd);
59320253Sjoerg			struct termios  t;
59420253Sjoerg
59520253Sjoerg			if (istty) {
59620253Sjoerg				if (tcgetattr(fd, &t) == -1)
59720253Sjoerg					istty = 0;
59820253Sjoerg				else {
59920253Sjoerg					struct termios  n = t;
60020253Sjoerg
60120253Sjoerg					/* Disable echo */
60220253Sjoerg					n.c_lflag &= ~(ECHO);
60320253Sjoerg					tcsetattr(fd, TCSANOW, &n);
60420253Sjoerg					printf("%sassword for user %s:", (mode == M_UPDATE) ? "New p" : "P", pwd->pw_name);
60520253Sjoerg					fflush(stdout);
60620253Sjoerg				}
60720253Sjoerg			}
60820253Sjoerg			b = read(fd, line, sizeof(line) - 1);
60920253Sjoerg			if (istty) {	/* Restore state */
61020253Sjoerg				tcsetattr(fd, TCSANOW, &t);
61120253Sjoerg				fputc('\n', stdout);
61220253Sjoerg				fflush(stdout);
61320253Sjoerg			}
61420253Sjoerg			if (b < 0) {
61530259Scharnier				warn("-h file descriptor");
61620267Sjoerg				return EX_IOERR;
61720253Sjoerg			}
61820253Sjoerg			line[b] = '\0';
61920253Sjoerg			if ((p = strpbrk(line, " \t\r\n")) != NULL)
62020253Sjoerg				*p = '\0';
62120253Sjoerg			if (!*line)
62230259Scharnier				errx(EX_DATAERR, "empty password read on file descriptor %d", fd);
62320253Sjoerg			pwd->pw_passwd = pw_pwcrypt(line);
62452527Sdavidn			edited = 1;
62520253Sjoerg		}
62620253Sjoerg	}
62720267Sjoerg
62820267Sjoerg	/*
62920267Sjoerg	 * Special case: -N only displays & exits
63020267Sjoerg	 */
63120267Sjoerg	if (getarg(args, 'N') != NULL)
63244386Sdavidn		return print_user(pwd,
63344386Sdavidn				  getarg(args, 'P') != NULL,
63444386Sdavidn				  getarg(args, '7') != NULL);
63520267Sjoerg
63621330Sdavidn	if (mode == M_ADD) {
63752527Sdavidn		edited = 1;	/* Always */
63852502Sdavidn		rc = addpwent(pwd);
63952502Sdavidn		if (rc == -1) {
64052502Sdavidn			warnx("user '%s' already exists", pwd->pw_name);
64152502Sdavidn			return EX_IOERR;
64252502Sdavidn		} else if (rc != 0) {
64352502Sdavidn			warnc(rc, "passwd file update");
64452502Sdavidn			return EX_IOERR;
64552502Sdavidn		}
64652502Sdavidn		if (cnf->nispasswd && *cnf->nispasswd=='/') {
64752502Sdavidn			rc = addnispwent(cnf->nispasswd, pwd);
64852502Sdavidn			if (rc == -1)
64952502Sdavidn				warnx("User '%s' already exists in NIS passwd", pwd->pw_name);
65052502Sdavidn			else
65152502Sdavidn				warnc(rc, "NIS passwd update");
65252502Sdavidn			/* NOTE: we treat NIS-only update errors as non-fatal */
65352502Sdavidn		}
65452512Sdavidn	} else if (mode == M_UPDATE || mode == M_LOCK || mode == M_UNLOCK) {
65552527Sdavidn		if (edited) {	/* Only updated this if required */
65652527Sdavidn			rc = chgpwent(a_name->val, pwd);
65752527Sdavidn			if (rc == -1) {
65852527Sdavidn				warnx("user '%s' does not exist (NIS?)", pwd->pw_name);
65952527Sdavidn				return EX_IOERR;
66052527Sdavidn			} else if (rc != 0) {
66152527Sdavidn				warnc(rc, "passwd file update");
66252527Sdavidn				return EX_IOERR;
66352527Sdavidn			}
66452527Sdavidn			if ( cnf->nispasswd && *cnf->nispasswd=='/') {
66552527Sdavidn				rc = chgnispwent(cnf->nispasswd, a_name->val, pwd);
66652527Sdavidn				if (rc == -1)
66752527Sdavidn					warn("User '%s' not found in NIS passwd", pwd->pw_name);
66852527Sdavidn				else
66952527Sdavidn					warnc(rc, "NIS passwd update");
67052527Sdavidn				/* NOTE: NIS-only update errors are not fatal */
67152527Sdavidn			}
67252502Sdavidn		}
67321330Sdavidn	}
67421330Sdavidn
67520253Sjoerg	/*
67620253Sjoerg	 * Ok, user is created or changed - now edit group file
67720253Sjoerg	 */
67820253Sjoerg
67920253Sjoerg	if (mode == M_ADD || getarg(args, 'G') != NULL)
68020253Sjoerg		editgroups(pwd->pw_name, cnf->groups);
68120253Sjoerg
68220253Sjoerg	/* pwd may have been invalidated */
68344229Sdavidn	if ((pwd = GETPWNAM(a_name->val)) == NULL)
68430259Scharnier		errx(EX_NOUSER, "user '%s' disappeared during update", a_name->val);
68520253Sjoerg
68644229Sdavidn	grp = GETGRGID(pwd->pw_gid);
68720253Sjoerg	pw_log(cnf, mode, W_USER, "%s(%ld):%s(%d):%s:%s:%s",
68820253Sjoerg	       pwd->pw_name, (long) pwd->pw_uid,
68920253Sjoerg	    grp ? grp->gr_name : "unknown", (long) (grp ? grp->gr_gid : -1),
69020253Sjoerg	       pwd->pw_gecos, pwd->pw_dir, pwd->pw_shell);
69120253Sjoerg
69220253Sjoerg	/*
69320253Sjoerg	 * If adding, let's touch and chown the user's mail file. This is not
69420253Sjoerg	 * strictly necessary under BSD with a 0755 maildir but it also
69520253Sjoerg	 * doesn't hurt anything to create the empty mailfile
69620253Sjoerg	 */
69720253Sjoerg	if (mode == M_ADD) {
69820253Sjoerg		FILE           *fp;
69920253Sjoerg
70044229Sdavidn		if (!PWALTDIR()) {
70144229Sdavidn			sprintf(line, "%s/%s", _PATH_MAILDIR, pwd->pw_name);
70244229Sdavidn			close(open(line, O_RDWR | O_CREAT, 0600));	/* Preserve contents &
70344229Sdavidn									 * mtime */
70444229Sdavidn			chown(line, pwd->pw_uid, pwd->pw_gid);
70520253Sjoerg
70644229Sdavidn			/*
70744229Sdavidn			 * Send mail to the new user as well, if we are asked to
70844229Sdavidn			 */
70944229Sdavidn			if (cnf->newmail && *cnf->newmail && (fp = fopen(cnf->newmail, "r")) != NULL) {
71044229Sdavidn				FILE           *pfp = popen(_PATH_SENDMAIL " -t", "w");
71120253Sjoerg
71244229Sdavidn				if (pfp == NULL)
71344229Sdavidn					warn("sendmail");
71444229Sdavidn				else {
71544229Sdavidn					fprintf(pfp, "From: root\n" "To: %s\n" "Subject: Welcome!\n\n", pwd->pw_name);
71644229Sdavidn					while (fgets(line, sizeof(line), fp) != NULL) {
71744229Sdavidn						/* Do substitutions? */
71844229Sdavidn						fputs(line, pfp);
71944229Sdavidn					}
72044229Sdavidn					pclose(pfp);
72144229Sdavidn					pw_log(cnf, mode, W_USER, "%s(%ld) new user mail sent",
72244229Sdavidn					       pwd->pw_name, (long) pwd->pw_uid);
72320253Sjoerg				}
72444229Sdavidn				fclose(fp);
72520253Sjoerg			}
72620253Sjoerg		}
72720253Sjoerg	}
72852527Sdavidn
72920253Sjoerg	/*
73020253Sjoerg	 * Finally, let's create and populate the user's home directory. Note
73120253Sjoerg	 * that this also `works' for editing users if -m is used, but
73220253Sjoerg	 * existing files will *not* be overwritten.
73320253Sjoerg	 */
73444229Sdavidn	if (!PWALTDIR() && getarg(args, 'm') != NULL && pwd->pw_dir && *pwd->pw_dir == '/' && pwd->pw_dir[1]) {
73520253Sjoerg		copymkdir(pwd->pw_dir, cnf->dotdir, 0755, pwd->pw_uid, pwd->pw_gid);
73620253Sjoerg		pw_log(cnf, mode, W_USER, "%s(%ld) home %s made",
73720253Sjoerg		       pwd->pw_name, (long) pwd->pw_uid, pwd->pw_dir);
73820253Sjoerg	}
73952527Sdavidn
74020267Sjoerg	return EXIT_SUCCESS;
74120253Sjoerg}
74220253Sjoerg
74320253Sjoerg
74420253Sjoergstatic          uid_t
74520253Sjoergpw_uidpolicy(struct userconf * cnf, struct cargs * args)
74620253Sjoerg{
74720253Sjoerg	struct passwd  *pwd;
74820253Sjoerg	uid_t           uid = (uid_t) - 1;
74920253Sjoerg	struct carg    *a_uid = getarg(args, 'u');
75020253Sjoerg
75120253Sjoerg	/*
75220253Sjoerg	 * Check the given uid, if any
75320253Sjoerg	 */
75420253Sjoerg	if (a_uid != NULL) {
75520253Sjoerg		uid = (uid_t) atol(a_uid->val);
75620253Sjoerg
75744229Sdavidn		if ((pwd = GETPWUID(uid)) != NULL && getarg(args, 'o') == NULL)
75830259Scharnier			errx(EX_DATAERR, "uid `%ld' has already been allocated", (long) pwd->pw_uid);
75920253Sjoerg	} else {
76020253Sjoerg		struct bitmap   bm;
76120253Sjoerg
76220253Sjoerg		/*
76320253Sjoerg		 * We need to allocate the next available uid under one of
76420253Sjoerg		 * two policies a) Grab the first unused uid b) Grab the
76520253Sjoerg		 * highest possible unused uid
76620253Sjoerg		 */
76720253Sjoerg		if (cnf->min_uid >= cnf->max_uid) {	/* Sanity
76820253Sjoerg							 * claus^H^H^H^Hheck */
76920253Sjoerg			cnf->min_uid = 1000;
77020253Sjoerg			cnf->max_uid = 32000;
77120253Sjoerg		}
77220253Sjoerg		bm = bm_alloc(cnf->max_uid - cnf->min_uid + 1);
77320253Sjoerg
77420253Sjoerg		/*
77520253Sjoerg		 * Now, let's fill the bitmap from the password file
77620253Sjoerg		 */
77744229Sdavidn		SETPWENT();
77844229Sdavidn		while ((pwd = GETPWENT()) != NULL)
77944229Sdavidn			if (pwd->pw_uid >= (uid_t) cnf->min_uid && pwd->pw_uid <= (uid_t) cnf->max_uid)
78020253Sjoerg				bm_setbit(&bm, pwd->pw_uid - cnf->min_uid);
78144229Sdavidn		ENDPWENT();
78220253Sjoerg
78320253Sjoerg		/*
78420253Sjoerg		 * Then apply the policy, with fallback to reuse if necessary
78520253Sjoerg		 */
78620253Sjoerg		if (cnf->reuse_uids || (uid = (uid_t) (bm_lastset(&bm) + cnf->min_uid + 1)) > cnf->max_uid)
78720253Sjoerg			uid = (uid_t) (bm_firstunset(&bm) + cnf->min_uid);
78820253Sjoerg
78920253Sjoerg		/*
79020253Sjoerg		 * Another sanity check
79120253Sjoerg		 */
79220253Sjoerg		if (uid < cnf->min_uid || uid > cnf->max_uid)
79330259Scharnier			errx(EX_SOFTWARE, "unable to allocate a new uid - range fully used");
79420253Sjoerg		bm_dealloc(&bm);
79520253Sjoerg	}
79620253Sjoerg	return uid;
79720253Sjoerg}
79820253Sjoerg
79920253Sjoerg
80020253Sjoergstatic          uid_t
80120253Sjoergpw_gidpolicy(struct userconf * cnf, struct cargs * args, char *nam, gid_t prefer)
80220253Sjoerg{
80320253Sjoerg	struct group   *grp;
80420253Sjoerg	gid_t           gid = (uid_t) - 1;
80520253Sjoerg	struct carg    *a_gid = getarg(args, 'g');
80620253Sjoerg
80720253Sjoerg	/*
80820253Sjoerg	 * If no arg given, see if default can help out
80920253Sjoerg	 */
81020253Sjoerg	if (a_gid == NULL && cnf->default_group && *cnf->default_group)
81120253Sjoerg		a_gid = addarg(args, 'g', cnf->default_group);
81220253Sjoerg
81320253Sjoerg	/*
81420253Sjoerg	 * Check the given gid, if any
81520253Sjoerg	 */
81644229Sdavidn	SETGRENT();
81720253Sjoerg	if (a_gid != NULL) {
81844229Sdavidn		if ((grp = GETGRNAM(a_gid->val)) == NULL) {
81920253Sjoerg			gid = (gid_t) atol(a_gid->val);
82044229Sdavidn			if ((gid == 0 && !isdigit(*a_gid->val)) || (grp = GETGRGID(gid)) == NULL)
82130259Scharnier				errx(EX_NOUSER, "group `%s' is not defined", a_gid->val);
82220253Sjoerg		}
82320253Sjoerg		gid = grp->gr_gid;
82444229Sdavidn	} else if ((grp = GETGRNAM(nam)) != NULL && grp->gr_mem[0] == NULL) {
82520267Sjoerg		gid = grp->gr_gid;  /* Already created? Use it anyway... */
82620253Sjoerg	} else {
82720253Sjoerg		struct cargs    grpargs;
82820253Sjoerg		char            tmp[32];
82920253Sjoerg
83020253Sjoerg		LIST_INIT(&grpargs);
83120253Sjoerg		addarg(&grpargs, 'n', nam);
83220253Sjoerg
83320253Sjoerg		/*
83420253Sjoerg		 * We need to auto-create a group with the user's name. We
83520253Sjoerg		 * can send all the appropriate output to our sister routine
83620253Sjoerg		 * bit first see if we can create a group with gid==uid so we
83720253Sjoerg		 * can keep the user and group ids in sync. We purposely do
83820253Sjoerg		 * NOT check the gid range if we can force the sync. If the
83920253Sjoerg		 * user's name dups an existing group, then the group add
84020253Sjoerg		 * function will happily handle that case for us and exit.
84120253Sjoerg		 */
84244229Sdavidn		if (GETGRGID(prefer) == NULL) {
84320253Sjoerg			sprintf(tmp, "%lu", (unsigned long) prefer);
84420253Sjoerg			addarg(&grpargs, 'g', tmp);
84520253Sjoerg		}
84620267Sjoerg		if (getarg(args, 'N'))
84720267Sjoerg		{
84820267Sjoerg			addarg(&grpargs, 'N', NULL);
84920267Sjoerg			addarg(&grpargs, 'q', NULL);
85020267Sjoerg			gid = pw_group(cnf, M_NEXT, &grpargs);
85120267Sjoerg		}
85220267Sjoerg		else
85320267Sjoerg		{
85420267Sjoerg			pw_group(cnf, M_ADD, &grpargs);
85544229Sdavidn			if ((grp = GETGRNAM(nam)) != NULL)
85620267Sjoerg				gid = grp->gr_gid;
85720267Sjoerg		}
85820253Sjoerg		a_gid = grpargs.lh_first;
85920253Sjoerg		while (a_gid != NULL) {
86020253Sjoerg			struct carg    *t = a_gid->list.le_next;
86120253Sjoerg			LIST_REMOVE(a_gid, list);
86220253Sjoerg			a_gid = t;
86320253Sjoerg		}
86420253Sjoerg	}
86544229Sdavidn	ENDGRENT();
86620253Sjoerg	return gid;
86720253Sjoerg}
86820253Sjoerg
86920253Sjoerg
87020253Sjoergstatic          time_t
87120253Sjoergpw_pwdpolicy(struct userconf * cnf, struct cargs * args)
87220253Sjoerg{
87320253Sjoerg	time_t          result = 0;
87420253Sjoerg	time_t          now = time(NULL);
87527831Sdavidn	struct carg    *arg = getarg(args, 'p');
87620253Sjoerg
87720253Sjoerg	if (arg != NULL) {
87820253Sjoerg		if ((result = parse_date(now, arg->val)) == now)
87930259Scharnier			errx(EX_DATAERR, "invalid date/time `%s'", arg->val);
88020253Sjoerg	} else if (cnf->password_days > 0)
88120253Sjoerg		result = now + ((long) cnf->password_days * 86400L);
88220253Sjoerg	return result;
88320253Sjoerg}
88420253Sjoerg
88520253Sjoerg
88620253Sjoergstatic          time_t
88720253Sjoergpw_exppolicy(struct userconf * cnf, struct cargs * args)
88820253Sjoerg{
88920253Sjoerg	time_t          result = 0;
89020253Sjoerg	time_t          now = time(NULL);
89120253Sjoerg	struct carg    *arg = getarg(args, 'e');
89220253Sjoerg
89320253Sjoerg	if (arg != NULL) {
89420253Sjoerg		if ((result = parse_date(now, arg->val)) == now)
89530259Scharnier			errx(EX_DATAERR, "invalid date/time `%s'", arg->val);
89620253Sjoerg	} else if (cnf->expire_days > 0)
89720253Sjoerg		result = now + ((long) cnf->expire_days * 86400L);
89820253Sjoerg	return result;
89920253Sjoerg}
90020253Sjoerg
90120253Sjoerg
90220253Sjoergstatic char    *
90320253Sjoergpw_homepolicy(struct userconf * cnf, struct cargs * args, char const * user)
90420253Sjoerg{
90520253Sjoerg	struct carg    *arg = getarg(args, 'd');
90620253Sjoerg
90720253Sjoerg	if (arg)
90820253Sjoerg		return arg->val;
90920253Sjoerg	else {
91020253Sjoerg		static char     home[128];
91120253Sjoerg
91220253Sjoerg		if (cnf->home == NULL || *cnf->home == '\0')
91330259Scharnier			errx(EX_CONFIG, "no base home directory set");
91420253Sjoerg		sprintf(home, "%s/%s", cnf->home, user);
91520253Sjoerg		return home;
91620253Sjoerg	}
91720253Sjoerg}
91820253Sjoerg
91920253Sjoergstatic char    *
92020253Sjoergshell_path(char const * path, char *shells[], char *sh)
92120253Sjoerg{
92220253Sjoerg	if (sh != NULL && (*sh == '/' || *sh == '\0'))
92320253Sjoerg		return sh;	/* specified full path or forced none */
92420253Sjoerg	else {
92520253Sjoerg		char           *p;
92620253Sjoerg		char            paths[_UC_MAXLINE];
92720253Sjoerg
92820253Sjoerg		/*
92920253Sjoerg		 * We need to search paths
93020253Sjoerg		 */
93120253Sjoerg		strncpy(paths, path, sizeof paths);
93220253Sjoerg		paths[sizeof paths - 1] = '\0';
93320253Sjoerg		for (p = strtok(paths, ": \t\r\n"); p != NULL; p = strtok(NULL, ": \t\r\n")) {
93420253Sjoerg			int             i;
93520253Sjoerg			static char     shellpath[256];
93620253Sjoerg
93720253Sjoerg			if (sh != NULL) {
93820253Sjoerg				sprintf(shellpath, "%s/%s", p, sh);
93920253Sjoerg				if (access(shellpath, X_OK) == 0)
94020253Sjoerg					return shellpath;
94120253Sjoerg			} else
94220253Sjoerg				for (i = 0; i < _UC_MAXSHELLS && shells[i] != NULL; i++) {
94320253Sjoerg					sprintf(shellpath, "%s/%s", p, shells[i]);
94420253Sjoerg					if (access(shellpath, X_OK) == 0)
94520253Sjoerg						return shellpath;
94620253Sjoerg				}
94720253Sjoerg		}
94820253Sjoerg		if (sh == NULL)
94930259Scharnier			errx(EX_OSFILE, "can't find shell `%s' in shell paths", sh);
95030259Scharnier		errx(EX_CONFIG, "no default shell available or defined");
95120253Sjoerg		return NULL;
95220253Sjoerg	}
95320253Sjoerg}
95420253Sjoerg
95520253Sjoerg
95620253Sjoergstatic char    *
95720253Sjoergpw_shellpolicy(struct userconf * cnf, struct cargs * args, char *newshell)
95820253Sjoerg{
95920253Sjoerg	char           *sh = newshell;
96020253Sjoerg	struct carg    *arg = getarg(args, 's');
96120253Sjoerg
96220253Sjoerg	if (newshell == NULL && arg != NULL)
96320253Sjoerg		sh = arg->val;
96420253Sjoerg	return shell_path(cnf->shelldir, cnf->shells, sh ? sh : cnf->shell_default);
96520253Sjoerg}
96620253Sjoerg
96720253Sjoergstatic char const chars[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.";
96820253Sjoerg
96920253Sjoergchar           *
97020253Sjoergpw_pwcrypt(char *password)
97120253Sjoerg{
97220253Sjoerg	int             i;
97320253Sjoerg	char            salt[12];
97420253Sjoerg
97520253Sjoerg	static char     buf[256];
97620253Sjoerg
97720253Sjoerg	/*
97820253Sjoerg	 * Calculate a salt value
97920253Sjoerg	 */
98024214Sache	if (!randinit) {
98124214Sache		randinit = 1;
98224214Sache#ifdef __FreeBSD__
98326626Sache		srandomdev();
98426626Sache#else
98526626Sache		srandom((unsigned long) (time(NULL) ^ getpid()));
98624214Sache#endif
98724214Sache	}
98820253Sjoerg	for (i = 0; i < 8; i++)
98920253Sjoerg		salt[i] = chars[random() % 63];
99020253Sjoerg	salt[i] = '\0';
99120253Sjoerg
99220253Sjoerg	return strcpy(buf, crypt(password, salt));
99320253Sjoerg}
99420253Sjoerg
99520590Sdavidn#if defined(USE_MD5RAND)
99620555Sdavidnu_char *
99720590Sdavidnpw_getrand(u_char *buf, int len)	/* cryptographically secure rng */
99820555Sdavidn{
99920590Sdavidn	int i;
100020590Sdavidn	for (i=0;i<len;i+=16) {
100120590Sdavidn		u_char ubuf[16];
100220590Sdavidn
100320590Sdavidn		MD5_CTX md5_ctx;
100420590Sdavidn		struct timeval tv, tvo;
100520590Sdavidn		struct rusage ru;
100620590Sdavidn		int n=0;
100720590Sdavidn		int t;
100820590Sdavidn
100920590Sdavidn		MD5Init (&md5_ctx);
101020590Sdavidn		t=getpid();
101120590Sdavidn		MD5Update (&md5_ctx, (u_char*)&t, sizeof t);
101220590Sdavidn		t=getppid();
101320590Sdavidn		MD5Update (&md5_ctx, (u_char*)&t, sizeof t);
101420590Sdavidn		gettimeofday (&tvo, NULL);
101520590Sdavidn		do {
101620590Sdavidn			getrusage (RUSAGE_SELF, &ru);
101720590Sdavidn			MD5Update (&md5_ctx, (u_char*)&ru, sizeof ru);
101820590Sdavidn			gettimeofday (&tv, NULL);
101920590Sdavidn			MD5Update (&md5_ctx, (u_char*)&tv, sizeof tv);
102020590Sdavidn		} while (n++<20 || tv.tv_usec-tvo.tv_usec<100*1000);
102120590Sdavidn		MD5Final (ubuf, &md5_ctx);
102220590Sdavidn		memcpy(buf+i, ubuf, MIN(16, len-n));
102320590Sdavidn	}
102420590Sdavidn	return buf;
102520555Sdavidn}
102620253Sjoerg
102720590Sdavidn#else	/* Portable version */
102820590Sdavidn
102920590Sdavidnstatic u_char *
103020590Sdavidnpw_getrand(u_char *buf, int len)
103120590Sdavidn{
103220590Sdavidn	int i;
103320590Sdavidn
103420590Sdavidn	for (i = 0; i < len; i++) {
103524214Sache		unsigned long val = random();
103620590Sdavidn		/* Use all bits in the random value */
103720590Sdavidn		buf[i]=(u_char)((val >> 24) ^ (val >> 16) ^ (val >> 8) ^ val);
103820590Sdavidn	}
103920590Sdavidn	return buf;
104020590Sdavidn}
104120590Sdavidn
104220590Sdavidn#endif
104320590Sdavidn
104420253Sjoergstatic char    *
104520253Sjoergpw_password(struct userconf * cnf, struct cargs * args, char const * user)
104620253Sjoerg{
104720253Sjoerg	int             i, l;
104820253Sjoerg	char            pwbuf[32];
104920555Sdavidn	u_char		rndbuf[sizeof pwbuf];
105020253Sjoerg
105120253Sjoerg	switch (cnf->default_password) {
105220253Sjoerg	case -1:		/* Random password */
105324214Sache		if (!randinit) {
105424214Sache			randinit = 1;
105524214Sache#ifdef __FreeBSD__
105626626Sache			srandomdev();
105726626Sache#else
105826626Sache			srandom((unsigned long) (time(NULL) ^ getpid()));
105924214Sache#endif
106024214Sache		}
106120253Sjoerg		l = (random() % 8 + 8);	/* 8 - 16 chars */
106220555Sdavidn		pw_getrand(rndbuf, l);
106320253Sjoerg		for (i = 0; i < l; i++)
106442245Sbillf			pwbuf[i] = chars[rndbuf[i] % (sizeof(chars)-1)];
106520253Sjoerg		pwbuf[i] = '\0';
106620253Sjoerg
106720253Sjoerg		/*
106820253Sjoerg		 * We give this information back to the user
106920253Sjoerg		 */
107020267Sjoerg		if (getarg(args, 'h') == NULL && getarg(args, 'N') == NULL) {
107120712Sdavidn			if (isatty(1))
107220712Sdavidn				printf("Password for '%s' is: ", user);
107320253Sjoerg			printf("%s\n", pwbuf);
107420253Sjoerg			fflush(stdout);
107520253Sjoerg		}
107620253Sjoerg		break;
107720253Sjoerg
107820253Sjoerg	case -2:		/* No password at all! */
107920253Sjoerg		return "";
108020253Sjoerg
108120253Sjoerg	case 0:		/* No login - default */
108220253Sjoerg	default:
108320253Sjoerg		return "*";
108420253Sjoerg
108520253Sjoerg	case 1:		/* user's name */
108620253Sjoerg		strncpy(pwbuf, user, sizeof pwbuf);
108720253Sjoerg		pwbuf[sizeof pwbuf - 1] = '\0';
108820253Sjoerg		break;
108920253Sjoerg	}
109020253Sjoerg	return pw_pwcrypt(pwbuf);
109120253Sjoerg}
109220253Sjoerg
109320253Sjoerg
109420253Sjoergstatic int
109544386Sdavidnprint_user(struct passwd * pwd, int pretty, int v7)
109620253Sjoerg{
109720253Sjoerg	if (!pretty) {
109820253Sjoerg		char            buf[_UC_MAXLINE];
109920253Sjoerg
110044386Sdavidn		fmtpwentry(buf, pwd, v7 ? PWF_PASSWD : PWF_STANDARD);
110120253Sjoerg		fputs(buf, stdout);
110220253Sjoerg	} else {
110320267Sjoerg		int		j;
110420253Sjoerg		char           *p;
110544229Sdavidn		struct group   *grp = GETGRGID(pwd->pw_gid);
110620253Sjoerg		char            uname[60] = "User &", office[60] = "[None]",
110720253Sjoerg		                wphone[60] = "[None]", hphone[60] = "[None]";
110820590Sdavidn		char		acexpire[32] = "[None]", pwexpire[32] = "[None]";
110920590Sdavidn		struct tm *    tptr;
111020253Sjoerg
111120253Sjoerg		if ((p = strtok(pwd->pw_gecos, ",")) != NULL) {
111220253Sjoerg			strncpy(uname, p, sizeof uname);
111320253Sjoerg			uname[sizeof uname - 1] = '\0';
111420253Sjoerg			if ((p = strtok(NULL, ",")) != NULL) {
111520253Sjoerg				strncpy(office, p, sizeof office);
111620253Sjoerg				office[sizeof office - 1] = '\0';
111720253Sjoerg				if ((p = strtok(NULL, ",")) != NULL) {
111820253Sjoerg					strncpy(wphone, p, sizeof wphone);
111920253Sjoerg					wphone[sizeof wphone - 1] = '\0';
112020253Sjoerg					if ((p = strtok(NULL, "")) != NULL) {
112120253Sjoerg						strncpy(hphone, p, sizeof hphone);
112220253Sjoerg						hphone[sizeof hphone - 1] = '\0';
112320253Sjoerg					}
112420253Sjoerg				}
112520253Sjoerg			}
112620253Sjoerg		}
112720253Sjoerg		/*
112820253Sjoerg		 * Handle '&' in gecos field
112920253Sjoerg		 */
113020253Sjoerg		if ((p = strchr(uname, '&')) != NULL) {
113120253Sjoerg			int             l = strlen(pwd->pw_name);
113220253Sjoerg			int             m = strlen(p);
113320253Sjoerg
113420253Sjoerg			memmove(p + l, p + 1, m);
113520253Sjoerg			memmove(p, pwd->pw_name, l);
113620253Sjoerg			*p = (char) toupper(*p);
113720253Sjoerg		}
113820590Sdavidn		if (pwd->pw_expire > (time_t)0 && (tptr = localtime(&pwd->pw_expire)) != NULL)
113920590Sdavidn		  strftime(acexpire, sizeof acexpire, "%c", tptr);
114020590Sdavidn		if (pwd->pw_change > (time_t)9 && (tptr = localtime(&pwd->pw_change)) != NULL)
114120590Sdavidn		  strftime(pwexpire, sizeof pwexpire, "%c", tptr);
114222394Sdavidn		printf("Login Name: %-15s   #%-12ld Group: %-15s   #%ld\n"
114320747Sdavidn		       " Full Name: %s\n"
114420747Sdavidn		       "      Home: %-26.26s      Class: %s\n"
114520747Sdavidn		       "     Shell: %-26.26s     Office: %s\n"
114620747Sdavidn		       "Work Phone: %-26.26s Home Phone: %s\n"
114720747Sdavidn		       "Acc Expire: %-26.26s Pwd Expire: %s\n",
114820253Sjoerg		       pwd->pw_name, (long) pwd->pw_uid,
114920253Sjoerg		       grp ? grp->gr_name : "(invalid)", (long) pwd->pw_gid,
115020253Sjoerg		       uname, pwd->pw_dir, pwd->pw_class,
115120590Sdavidn		       pwd->pw_shell, office, wphone, hphone,
115220590Sdavidn		       acexpire, pwexpire);
115344229Sdavidn	        SETGRENT();
115420267Sjoerg		j = 0;
115544229Sdavidn		while ((grp=GETGRENT()) != NULL)
115620267Sjoerg		{
115720267Sjoerg			int     i = 0;
115820747Sdavidn			while (grp->gr_mem[i] != NULL)
115920267Sjoerg			{
116020267Sjoerg				if (strcmp(grp->gr_mem[i], pwd->pw_name)==0)
116120267Sjoerg				{
116220747Sdavidn					printf(j++ == 0 ? "    Groups: %s" : ",%s", grp->gr_name);
116320267Sjoerg					break;
116420267Sjoerg				}
116520267Sjoerg				++i;
116620267Sjoerg			}
116720267Sjoerg		}
116844229Sdavidn		ENDGRENT();
116920267Sjoerg		printf("%s\n", j ? "\n" : "");
117020253Sjoerg	}
117120267Sjoerg	return EXIT_SUCCESS;
117220253Sjoerg}
117320253Sjoerg
117420679Sdavidnchar    *
117520679Sdavidnpw_checkname(u_char *name, int gecos)
117620253Sjoerg{
117720253Sjoerg	int             l = 0;
117821052Sdavidn	char const     *notch = gecos ? ":!@" : " ,\t:+&#%$^()!@~*?<>=|\\/\"";
117920253Sjoerg
118020253Sjoerg	while (name[l]) {
118121052Sdavidn		if (strchr(notch, name[l]) != NULL || name[l] < ' ' || name[l] == 127 ||
118221052Sdavidn			(!gecos && l==0 && name[l] == '-') ||	/* leading '-' */
118321052Sdavidn			(!gecos && name[l] & 0x80))	/* 8-bit */
118430259Scharnier			errx(EX_DATAERR, (name[l] >= ' ' && name[l] < 127)
118530259Scharnier					    ? "invalid character `%c' in field"
118630259Scharnier					    : "invalid character 0x%02x in field",
118720325Sjoerg					    name[l]);
118820253Sjoerg		++l;
118920253Sjoerg	}
119022394Sdavidn	if (!gecos && l > LOGNAMESIZE)
119130259Scharnier		errx(EX_DATAERR, "name too long `%s'", name);
119220679Sdavidn	return (char *)name;
119320253Sjoerg}
119420253Sjoerg
119520253Sjoerg
119620253Sjoergstatic void
119720253Sjoergrmat(uid_t uid)
119820253Sjoerg{
119920253Sjoerg	DIR            *d = opendir("/var/at/jobs");
120020253Sjoerg
120120253Sjoerg	if (d != NULL) {
120220253Sjoerg		struct dirent  *e;
120320253Sjoerg
120420253Sjoerg		while ((e = readdir(d)) != NULL) {
120520253Sjoerg			struct stat     st;
120620253Sjoerg
120720253Sjoerg			if (strncmp(e->d_name, ".lock", 5) != 0 &&
120820253Sjoerg			    stat(e->d_name, &st) == 0 &&
120920253Sjoerg			    !S_ISDIR(st.st_mode) &&
121020253Sjoerg			    st.st_uid == uid) {
121120253Sjoerg				char            tmp[MAXPATHLEN];
121220253Sjoerg
121320253Sjoerg				sprintf(tmp, "/usr/bin/atrm %s", e->d_name);
121420253Sjoerg				system(tmp);
121520253Sjoerg			}
121620253Sjoerg		}
121720253Sjoerg		closedir(d);
121820253Sjoerg	}
121920253Sjoerg}
122020747Sdavidn
122120747Sdavidnstatic void
122220747Sdavidnrmskey(char const * name)
122320747Sdavidn{
122420747Sdavidn	static const char etcskey[] = "/etc/skeykeys";
122521052Sdavidn	FILE   *fp = fopen(etcskey, "r+");
122620747Sdavidn
122721052Sdavidn	if (fp != NULL) {
122821052Sdavidn		char	tmp[1024];
122921052Sdavidn		off_t	atofs = 0;
123021052Sdavidn		int	length = strlen(name);
123120747Sdavidn
123221052Sdavidn		while (fgets(tmp, sizeof tmp, fp) != NULL) {
123321052Sdavidn			if (strncmp(name, tmp, length) == 0 && tmp[length]==' ') {
123421052Sdavidn				if (fseek(fp, atofs, SEEK_SET) == 0) {
123521052Sdavidn					fwrite("#", 1, 1, fp);	/* Comment username out */
123621052Sdavidn				}
123721052Sdavidn				break;
123820747Sdavidn			}
123921052Sdavidn			atofs = ftell(fp);
124020747Sdavidn		}
124121052Sdavidn		/*
124221052Sdavidn		 * If we got an error of any sort, don't update!
124321052Sdavidn		 */
124421052Sdavidn		fclose(fp);
124520747Sdavidn	}
124620747Sdavidn}
124720747Sdavidn
1248