pw_user.c revision 70486
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 70486 2000-12-29 18:04:54Z ben $";
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>
4564918Sgreen#include <login_cap.h>
4622394Sdavidn#if defined(USE_MD5RAND)
4720555Sdavidn#include <md5.h>
4822394Sdavidn#endif
4920253Sjoerg#include "pw.h"
5020253Sjoerg#include "bitmap.h"
5120253Sjoerg
5223318Sache#if (MAXLOGNAME-1) > UT_NAMESIZE
5322394Sdavidn#define LOGNAMESIZE UT_NAMESIZE
5422394Sdavidn#else
5523318Sache#define LOGNAMESIZE (MAXLOGNAME-1)
5622394Sdavidn#endif
5722394Sdavidn
5849171Sdavidnstatic          int randinit;
5952512Sdavidnstatic		char locked_str[] = "*LOCKED*";
6024214Sache
6144386Sdavidnstatic int      print_user(struct passwd * pwd, int pretty, int v7);
6220253Sjoergstatic uid_t    pw_uidpolicy(struct userconf * cnf, struct cargs * args);
6320253Sjoergstatic uid_t    pw_gidpolicy(struct userconf * cnf, struct cargs * args, char *nam, gid_t prefer);
6420253Sjoergstatic time_t   pw_pwdpolicy(struct userconf * cnf, struct cargs * args);
6520253Sjoergstatic time_t   pw_exppolicy(struct userconf * cnf, struct cargs * args);
6620253Sjoergstatic char    *pw_homepolicy(struct userconf * cnf, struct cargs * args, char const * user);
6720253Sjoergstatic char    *pw_shellpolicy(struct userconf * cnf, struct cargs * args, char *newshell);
6820253Sjoergstatic char    *pw_password(struct userconf * cnf, struct cargs * args, char const * user);
6920253Sjoergstatic char    *shell_path(char const * path, char *shells[], char *sh);
7020253Sjoergstatic void     rmat(uid_t uid);
7120747Sdavidnstatic void	rmskey(char const * name);
7220253Sjoerg
7320253Sjoerg/*-
7420253Sjoerg * -C config      configuration file
7520253Sjoerg * -q             quiet operation
7620253Sjoerg * -n name        login name
7720253Sjoerg * -u uid         user id
7820253Sjoerg * -c comment     user name/comment
7920253Sjoerg * -d directory   home directory
8020253Sjoerg * -e date        account expiry date
8120253Sjoerg * -p date        password expiry date
8220253Sjoerg * -g grp         primary group
8320253Sjoerg * -G grp1,grp2   additional groups
8420253Sjoerg * -m [ -k dir ]  create and set up home
8520253Sjoerg * -s shell       name of login shell
8620253Sjoerg * -o             duplicate uid ok
8720253Sjoerg * -L class       user class
8820253Sjoerg * -l name        new login name
8920253Sjoerg * -h fd          password filehandle
9020253Sjoerg * -F             force print or add
9120253Sjoerg *   Setting defaults:
9220253Sjoerg * -D             set user defaults
9320253Sjoerg * -b dir         default home root dir
9420253Sjoerg * -e period      default expiry period
9520253Sjoerg * -p period      default password change period
9620253Sjoerg * -g group       default group
9720253Sjoerg * -G             grp1,grp2.. default additional groups
9820253Sjoerg * -L class       default login class
9920253Sjoerg * -k dir         default home skeleton
10020253Sjoerg * -s shell       default shell
10120253Sjoerg * -w method      default password method
10220253Sjoerg */
10320253Sjoerg
10420253Sjoergint
10520253Sjoergpw_user(struct userconf * cnf, int mode, struct cargs * args)
10620253Sjoerg{
10752527Sdavidn	int	        rc, edited = 0;
10820253Sjoerg	char           *p = NULL;
10952512Sdavidn	char					 *passtmp;
11020253Sjoerg	struct carg    *a_name;
11120253Sjoerg	struct carg    *a_uid;
11220253Sjoerg	struct carg    *arg;
11320253Sjoerg	struct passwd  *pwd = NULL;
11420253Sjoerg	struct group   *grp;
11520253Sjoerg	struct stat     st;
11620747Sdavidn	char            line[_PASSWORD_LEN+1];
11720253Sjoerg
11820253Sjoerg	static struct passwd fakeuser =
11920253Sjoerg	{
12020253Sjoerg		NULL,
12120253Sjoerg		"*",
12220253Sjoerg		-1,
12320253Sjoerg		-1,
12420253Sjoerg		0,
12520253Sjoerg		"",
12620253Sjoerg		"User &",
12756000Sdavidn		"/nonexistent",
12820253Sjoerg		"/bin/sh",
12920253Sjoerg		0
13056000Sdavidn#if defined(__FreeBSD__)
13156000Sdavidn		,0
13256000Sdavidn#endif
13320253Sjoerg	};
13420253Sjoerg
13552512Sdavidn
13620253Sjoerg	/*
13720267Sjoerg	 * With M_NEXT, we only need to return the
13820267Sjoerg	 * next uid to stdout
13920267Sjoerg	 */
14020267Sjoerg	if (mode == M_NEXT)
14120267Sjoerg	{
14220267Sjoerg		uid_t next = pw_uidpolicy(cnf, args);
14320267Sjoerg		if (getarg(args, 'q'))
14420267Sjoerg			return next;
14520267Sjoerg		printf("%ld:", (long)next);
14620267Sjoerg		pw_group(cnf, mode, args);
14720267Sjoerg		return EXIT_SUCCESS;
14820267Sjoerg	}
14920267Sjoerg
15020267Sjoerg	/*
15120253Sjoerg	 * We can do all of the common legwork here
15220253Sjoerg	 */
15320253Sjoerg
15420253Sjoerg	if ((arg = getarg(args, 'b')) != NULL) {
15520267Sjoerg		cnf->home = arg->val;
15620253Sjoerg	}
15721052Sdavidn
15821052Sdavidn	/*
15921052Sdavidn	 * If we'll need to use it or we're updating it,
16021052Sdavidn	 * then create the base home directory if necessary
16121052Sdavidn	 */
16221052Sdavidn	if (arg != NULL || getarg(args, 'm') != NULL) {
16321052Sdavidn		int	l = strlen(cnf->home);
16421052Sdavidn
16521052Sdavidn		if (l > 1 && cnf->home[l-1] == '/')	/* Shave off any trailing path delimiter */
16621052Sdavidn			cnf->home[--l] = '\0';
16721052Sdavidn
16821052Sdavidn		if (l < 2 || *cnf->home != '/')		/* Check for absolute path name */
16930259Scharnier			errx(EX_DATAERR, "invalid base directory for home '%s'", cnf->home);
17021052Sdavidn
17121052Sdavidn		if (stat(cnf->home, &st) == -1) {
17221052Sdavidn			char	dbuf[MAXPATHLEN];
17321052Sdavidn
17421242Sdavidn			/*
17521242Sdavidn			 * This is a kludge especially for Joerg :)
17621242Sdavidn			 * If the home directory would be created in the root partition, then
17721242Sdavidn			 * we really create it under /usr which is likely to have more space.
17821242Sdavidn			 * But we create a symlink from cnf->home -> "/usr" -> cnf->home
17921242Sdavidn			 */
18021242Sdavidn			if (strchr(cnf->home+1, '/') == NULL) {
18121242Sdavidn				strcpy(dbuf, "/usr");
18221242Sdavidn				strncat(dbuf, cnf->home, MAXPATHLEN-5);
18321242Sdavidn				if (mkdir(dbuf, 0755) != -1 || errno == EEXIST) {
18421242Sdavidn					chown(dbuf, 0, 0);
18521242Sdavidn					symlink(dbuf, cnf->home);
18621242Sdavidn				}
18721242Sdavidn				/* If this falls, fall back to old method */
18821242Sdavidn			}
18921052Sdavidn			p = strncpy(dbuf, cnf->home, sizeof dbuf);
19021052Sdavidn			dbuf[MAXPATHLEN-1] = '\0';
19121242Sdavidn			if (stat(dbuf, &st) == -1) {
19221242Sdavidn				while ((p = strchr(++p, '/')) != NULL) {
19321242Sdavidn					*p = '\0';
19421242Sdavidn					if (stat(dbuf, &st) == -1) {
19521242Sdavidn						if (mkdir(dbuf, 0755) == -1)
19621242Sdavidn							goto direrr;
19721242Sdavidn						chown(dbuf, 0, 0);
19821242Sdavidn					} else if (!S_ISDIR(st.st_mode))
19930259Scharnier						errx(EX_OSFILE, "'%s' (root home parent) is not a directory", dbuf);
20021242Sdavidn					*p = '/';
20121242Sdavidn				}
20221052Sdavidn			}
20321242Sdavidn			if (stat(dbuf, &st) == -1) {
20421052Sdavidn				if (mkdir(dbuf, 0755) == -1) {
20530259Scharnier				direrr:	err(EX_OSFILE, "mkdir '%s'", dbuf);
20621052Sdavidn				}
20721052Sdavidn				chown(dbuf, 0, 0);
20821052Sdavidn			}
20921052Sdavidn		} else if (!S_ISDIR(st.st_mode))
21030259Scharnier			errx(EX_OSFILE, "root home `%s' is not a directory", cnf->home);
21121052Sdavidn	}
21221052Sdavidn
21320253Sjoerg	if ((arg = getarg(args, 'e')) != NULL)
21420253Sjoerg		cnf->expire_days = atoi(arg->val);
21520253Sjoerg
21621330Sdavidn	if ((arg = getarg(args, 'y')) != NULL)
21721330Sdavidn		cnf->nispasswd = arg->val;
21821330Sdavidn
21920253Sjoerg	if ((arg = getarg(args, 'p')) != NULL && arg->val)
22020253Sjoerg		cnf->password_days = atoi(arg->val);
22120253Sjoerg
22220253Sjoerg	if ((arg = getarg(args, 'g')) != NULL) {
22363596Sdavidn		if (!*(p = arg->val))	/* Handle empty group list specially */
22463596Sdavidn			cnf->default_group = "";
22563596Sdavidn		else {
22663596Sdavidn			if ((grp = GETGRNAM(p)) == NULL) {
22763596Sdavidn				if (!isdigit((unsigned char)*p) || (grp = GETGRGID((gid_t) atoi(p))) == NULL)
22863596Sdavidn					errx(EX_NOUSER, "group `%s' does not exist", p);
22963596Sdavidn			}
23063596Sdavidn			cnf->default_group = newstr(grp->gr_name);
23120253Sjoerg		}
23220253Sjoerg	}
23320253Sjoerg	if ((arg = getarg(args, 'L')) != NULL)
23420679Sdavidn		cnf->default_class = pw_checkname((u_char *)arg->val, 0);
23520253Sjoerg
23620253Sjoerg	if ((arg = getarg(args, 'G')) != NULL && arg->val) {
23752527Sdavidn		int i = 0;
23820253Sjoerg
23920747Sdavidn		for (p = strtok(arg->val, ", \t"); p != NULL; p = strtok(NULL, ", \t")) {
24044229Sdavidn			if ((grp = GETGRNAM(p)) == NULL) {
24161957Sache				if (!isdigit((unsigned char)*p) || (grp = GETGRGID((gid_t) atoi(p))) == NULL)
24230259Scharnier					errx(EX_NOUSER, "group `%s' does not exist", p);
24320253Sjoerg			}
24420747Sdavidn			if (extendarray(&cnf->groups, &cnf->numgroups, i + 2) != -1)
24520747Sdavidn				cnf->groups[i++] = newstr(grp->gr_name);
24620253Sjoerg		}
24720747Sdavidn		while (i < cnf->numgroups)
24820253Sjoerg			cnf->groups[i++] = NULL;
24920253Sjoerg	}
25052527Sdavidn
25120253Sjoerg	if ((arg = getarg(args, 'k')) != NULL) {
25226088Sdavidn		if (stat(cnf->dotdir = arg->val, &st) == -1 || !S_ISDIR(st.st_mode))
25330259Scharnier			errx(EX_OSFILE, "skeleton `%s' is not a directory or does not exist", cnf->dotdir);
25420253Sjoerg	}
25552527Sdavidn
25620253Sjoerg	if ((arg = getarg(args, 's')) != NULL)
25720253Sjoerg		cnf->shell_default = arg->val;
25820253Sjoerg
25963600Sdavidn	if ((arg = getarg(args, 'w')) != NULL)
26063600Sdavidn		cnf->default_password = boolean_val(arg->val, cnf->default_password);
26120253Sjoerg	if (mode == M_ADD && getarg(args, 'D')) {
26220253Sjoerg		if (getarg(args, 'n') != NULL)
26330259Scharnier			errx(EX_DATAERR, "can't combine `-D' with `-n name'");
26420253Sjoerg		if ((arg = getarg(args, 'u')) != NULL && (p = strtok(arg->val, ", \t")) != NULL) {
26520253Sjoerg			if ((cnf->min_uid = (uid_t) atoi(p)) == 0)
26620253Sjoerg				cnf->min_uid = 1000;
26720253Sjoerg			if ((p = strtok(NULL, " ,\t")) == NULL || (cnf->max_uid = (uid_t) atoi(p)) < cnf->min_uid)
26820253Sjoerg				cnf->max_uid = 32000;
26920253Sjoerg		}
27020253Sjoerg		if ((arg = getarg(args, 'i')) != NULL && (p = strtok(arg->val, ", \t")) != NULL) {
27120253Sjoerg			if ((cnf->min_gid = (gid_t) atoi(p)) == 0)
27220253Sjoerg				cnf->min_gid = 1000;
27320253Sjoerg			if ((p = strtok(NULL, " ,\t")) == NULL || (cnf->max_gid = (gid_t) atoi(p)) < cnf->min_gid)
27420253Sjoerg				cnf->max_gid = 32000;
27520253Sjoerg		}
27620253Sjoerg
27720253Sjoerg		arg = getarg(args, 'C');
27820253Sjoerg		if (write_userconfig(arg ? arg->val : NULL))
27920267Sjoerg			return EXIT_SUCCESS;
28030259Scharnier		warn("config update");
28120267Sjoerg		return EX_IOERR;
28220253Sjoerg	}
28352527Sdavidn
28420253Sjoerg	if (mode == M_PRINT && getarg(args, 'a')) {
28520267Sjoerg		int             pretty = getarg(args, 'P') != NULL;
28644386Sdavidn		int		v7 = getarg(args, '7') != NULL;
28720253Sjoerg
28844229Sdavidn		SETPWENT();
28944229Sdavidn		while ((pwd = GETPWENT()) != NULL)
29044386Sdavidn			print_user(pwd, pretty, v7);
29144229Sdavidn		ENDPWENT();
29220267Sjoerg		return EXIT_SUCCESS;
29320253Sjoerg	}
29452527Sdavidn
29520253Sjoerg	if ((a_name = getarg(args, 'n')) != NULL)
29644229Sdavidn		pwd = GETPWNAM(pw_checkname((u_char *)a_name->val, 0));
29720253Sjoerg	a_uid = getarg(args, 'u');
29820253Sjoerg
29920253Sjoerg	if (a_uid == NULL) {
30020253Sjoerg		if (a_name == NULL)
30130259Scharnier			errx(EX_DATAERR, "user name or id required");
30220253Sjoerg
30320253Sjoerg		/*
30420253Sjoerg		 * Determine whether 'n' switch is name or uid - we don't
30520253Sjoerg		 * really don't really care which we have, but we need to
30620253Sjoerg		 * know.
30720253Sjoerg		 */
30843780Sdes		if (mode != M_ADD && pwd == NULL
30943780Sdes		    && strspn(a_name->val, "0123456789") == strlen(a_name->val)
31043780Sdes		    && atoi(a_name->val) > 0) {	/* Assume uid */
31120253Sjoerg			(a_uid = a_name)->ch = 'u';
31220253Sjoerg			a_name = NULL;
31320253Sjoerg		}
31420253Sjoerg	}
31552527Sdavidn
31620253Sjoerg	/*
31720253Sjoerg	 * Update, delete & print require that the user exists
31820253Sjoerg	 */
31952512Sdavidn	if (mode == M_UPDATE || mode == M_DELETE ||
32052512Sdavidn	    mode == M_PRINT  || mode == M_LOCK   || mode == M_UNLOCK) {
32152527Sdavidn
32220253Sjoerg		if (a_name == NULL && pwd == NULL)	/* Try harder */
32344229Sdavidn			pwd = GETPWUID(atoi(a_uid->val));
32420253Sjoerg
32520253Sjoerg		if (pwd == NULL) {
32620253Sjoerg			if (mode == M_PRINT && getarg(args, 'F')) {
32720253Sjoerg				fakeuser.pw_name = a_name ? a_name->val : "nouser";
32820253Sjoerg				fakeuser.pw_uid = a_uid ? (uid_t) atol(a_uid->val) : -1;
32944386Sdavidn				return print_user(&fakeuser,
33044386Sdavidn						  getarg(args, 'P') != NULL,
33144386Sdavidn						  getarg(args, '7') != NULL);
33220253Sjoerg			}
33320253Sjoerg			if (a_name == NULL)
33430259Scharnier				errx(EX_NOUSER, "no such uid `%s'", a_uid->val);
33530259Scharnier			errx(EX_NOUSER, "no such user `%s'", a_name->val);
33620253Sjoerg		}
33752527Sdavidn
33820253Sjoerg		if (a_name == NULL)	/* May be needed later */
33920253Sjoerg			a_name = addarg(args, 'n', newstr(pwd->pw_name));
34020253Sjoerg
34120253Sjoerg		/*
34252512Sdavidn		 * The M_LOCK and M_UNLOCK functions simply add or remove
34352512Sdavidn		 * a "*LOCKED*" prefix from in front of the password to
34452512Sdavidn		 * prevent it decoding correctly, and therefore prevents
34552512Sdavidn		 * access. Of course, this only prevents access via
34652512Sdavidn		 * password authentication (not ssh, kerberos or any
34752512Sdavidn		 * other method that does not use the UNIX password) but
34852512Sdavidn		 * that is a known limitation.
34952512Sdavidn		 */
35052512Sdavidn
35152512Sdavidn		if (mode == M_LOCK) {
35252512Sdavidn			if (strncmp(pwd->pw_passwd, locked_str, sizeof(locked_str)-1) == 0)
35352512Sdavidn				errx(EX_DATAERR, "user '%s' is already locked", pwd->pw_name);
35452512Sdavidn			passtmp = malloc(strlen(pwd->pw_passwd) + sizeof(locked_str));
35552512Sdavidn			if (passtmp == NULL)	/* disaster */
35652512Sdavidn				errx(EX_UNAVAILABLE, "out of memory");
35752512Sdavidn			strcpy(passtmp, locked_str);
35852512Sdavidn			strcat(passtmp, pwd->pw_passwd);
35952512Sdavidn			pwd->pw_passwd = passtmp;
36052527Sdavidn			edited = 1;
36152512Sdavidn		} else if (mode == M_UNLOCK) {
36252512Sdavidn			if (strncmp(pwd->pw_passwd, locked_str, sizeof(locked_str)-1) != 0)
36352512Sdavidn				errx(EX_DATAERR, "user '%s' is not locked", pwd->pw_name);
36452512Sdavidn			pwd->pw_passwd += sizeof(locked_str)-1;
36552527Sdavidn			edited = 1;
36652527Sdavidn		} else if (mode == M_DELETE) {
36752527Sdavidn			/*
36852527Sdavidn			 * Handle deletions now
36952527Sdavidn			 */
37020253Sjoerg			char            file[MAXPATHLEN];
37120253Sjoerg			char            home[MAXPATHLEN];
37220253Sjoerg			uid_t           uid = pwd->pw_uid;
37320253Sjoerg
37420253Sjoerg			if (strcmp(pwd->pw_name, "root") == 0)
37530259Scharnier				errx(EX_DATAERR, "cannot remove user 'root'");
37620253Sjoerg
37744229Sdavidn			if (!PWALTDIR()) {
37844229Sdavidn				/*
37944229Sdavidn				 * Remove skey record from /etc/skeykeys
38044229Sdavidn		        	 */
38120747Sdavidn
38244229Sdavidn				rmskey(pwd->pw_name);
38320747Sdavidn
38444229Sdavidn				/*
38544229Sdavidn				 * Remove crontabs
38644229Sdavidn				 */
38744229Sdavidn				sprintf(file, "/var/cron/tabs/%s", pwd->pw_name);
38844229Sdavidn				if (access(file, F_OK) == 0) {
38944229Sdavidn					sprintf(file, "crontab -u %s -r", pwd->pw_name);
39044229Sdavidn					system(file);
39144229Sdavidn				}
39220253Sjoerg			}
39320253Sjoerg			/*
39420253Sjoerg			 * Save these for later, since contents of pwd may be
39520253Sjoerg			 * invalidated by deletion
39620253Sjoerg			 */
39720253Sjoerg			sprintf(file, "%s/%s", _PATH_MAILDIR, pwd->pw_name);
39820253Sjoerg			strncpy(home, pwd->pw_dir, sizeof home);
39920253Sjoerg			home[sizeof home - 1] = '\0';
40020253Sjoerg
40152502Sdavidn			rc = delpwent(pwd);
40252502Sdavidn			if (rc == -1)
40352502Sdavidn				err(EX_IOERR, "user '%s' does not exist", pwd->pw_name);
40452502Sdavidn			else if (rc != 0) {
40556000Sdavidn				warn("passwd update");
40652502Sdavidn				return EX_IOERR;
40752502Sdavidn			}
40821330Sdavidn
40952502Sdavidn			if (cnf->nispasswd && *cnf->nispasswd=='/') {
41052502Sdavidn				rc = delnispwent(cnf->nispasswd, a_name->val);
41152502Sdavidn				if (rc == -1)
41252502Sdavidn					warnx("WARNING: user '%s' does not exist in NIS passwd", pwd->pw_name);
41352502Sdavidn				else if (rc != 0)
41456000Sdavidn					warn("WARNING: NIS passwd update");
41552502Sdavidn				/* non-fatal */
41652502Sdavidn			}
41752502Sdavidn
41820253Sjoerg			editgroups(a_name->val, NULL);
41920253Sjoerg
42020253Sjoerg			pw_log(cnf, mode, W_USER, "%s(%ld) account removed", a_name->val, (long) uid);
42120253Sjoerg
42244229Sdavidn			if (!PWALTDIR()) {
42344229Sdavidn				/*
42444229Sdavidn				 * Remove mail file
42544229Sdavidn				 */
42644229Sdavidn				remove(file);
42720253Sjoerg
42844229Sdavidn				/*
42944229Sdavidn				 * Remove at jobs
43044229Sdavidn				 */
43144229Sdavidn				if (getpwuid(uid) == NULL)
43244229Sdavidn					rmat(uid);
43320253Sjoerg
43444229Sdavidn				/*
43544229Sdavidn				 * Remove home directory and contents
43644229Sdavidn				 */
43744229Sdavidn				if (getarg(args, 'r') != NULL && *home == '/' && getpwuid(uid) == NULL) {
43844229Sdavidn					if (stat(home, &st) != -1) {
43944229Sdavidn						rm_r(home, uid);
44044229Sdavidn						pw_log(cnf, mode, W_USER, "%s(%ld) home '%s' %sremoved",
44144229Sdavidn						       a_name->val, (long) uid, home,
44244229Sdavidn						       stat(home, &st) == -1 ? "" : "not completely ");
44344229Sdavidn					}
44420253Sjoerg				}
44520253Sjoerg			}
44620267Sjoerg			return EXIT_SUCCESS;
44720253Sjoerg		} else if (mode == M_PRINT)
44844386Sdavidn			return print_user(pwd,
44944386Sdavidn					  getarg(args, 'P') != NULL,
45044386Sdavidn					  getarg(args, '7') != NULL);
45120253Sjoerg
45220253Sjoerg		/*
45320253Sjoerg		 * The rest is edit code
45420253Sjoerg		 */
45520253Sjoerg		if ((arg = getarg(args, 'l')) != NULL) {
45620253Sjoerg			if (strcmp(pwd->pw_name, "root") == 0)
45730259Scharnier				errx(EX_DATAERR, "can't rename `root' account");
45820679Sdavidn			pwd->pw_name = pw_checkname((u_char *)arg->val, 0);
45952527Sdavidn			edited = 1;
46020253Sjoerg		}
46152527Sdavidn
46261957Sache		if ((arg = getarg(args, 'u')) != NULL && isdigit((unsigned char)*arg->val)) {
46320253Sjoerg			pwd->pw_uid = (uid_t) atol(arg->val);
46452527Sdavidn			edited = 1;
46520253Sjoerg			if (pwd->pw_uid != 0 && strcmp(pwd->pw_name, "root") == 0)
46630259Scharnier				errx(EX_DATAERR, "can't change uid of `root' account");
46720253Sjoerg			if (pwd->pw_uid == 0 && strcmp(pwd->pw_name, "root") != 0)
46830259Scharnier				warnx("WARNING: account `%s' will have a uid of 0 (superuser access!)", pwd->pw_name);
46920253Sjoerg		}
47020253Sjoerg
47152527Sdavidn		if ((arg = getarg(args, 'g')) != NULL && pwd->pw_uid != 0) {	/* Already checked this */
47252527Sdavidn			gid_t newgid = (gid_t) GETGRNAM(cnf->default_group)->gr_gid;
47352527Sdavidn			if (newgid != pwd->pw_gid) {
47452527Sdavidn				edited = 1;
47561762Sdavidn				pwd->pw_gid = newgid;
47652527Sdavidn			}
47752527Sdavidn		}
47852527Sdavidn
47920253Sjoerg		if ((arg = getarg(args, 'p')) != NULL) {
48052527Sdavidn			if (*arg->val == '\0' || strcmp(arg->val, "0") == 0) {
48152527Sdavidn				if (pwd->pw_change != 0) {
48252527Sdavidn					pwd->pw_change = 0;
48352527Sdavidn					edited = 1;
48452527Sdavidn				}
48552527Sdavidn			}
48620253Sjoerg			else {
48720253Sjoerg				time_t          now = time(NULL);
48820253Sjoerg				time_t          expire = parse_date(now, arg->val);
48920253Sjoerg
49020253Sjoerg				if (now == expire)
49130259Scharnier					errx(EX_DATAERR, "invalid password change date `%s'", arg->val);
49252527Sdavidn				if (pwd->pw_change != expire) {
49352527Sdavidn					pwd->pw_change = expire;
49452527Sdavidn					edited = 1;
49552527Sdavidn				}
49620253Sjoerg			}
49720253Sjoerg		}
49852527Sdavidn
49920267Sjoerg		if ((arg = getarg(args, 'e')) != NULL) {
50052527Sdavidn			if (*arg->val == '\0' || strcmp(arg->val, "0") == 0) {
50152527Sdavidn				if (pwd->pw_expire != 0) {
50252527Sdavidn					pwd->pw_expire = 0;
50352527Sdavidn					edited = 1;
50452527Sdavidn				}
50552527Sdavidn			}
50620253Sjoerg			else {
50720253Sjoerg				time_t          now = time(NULL);
50820253Sjoerg				time_t          expire = parse_date(now, arg->val);
50920253Sjoerg
51020253Sjoerg				if (now == expire)
51130259Scharnier					errx(EX_DATAERR, "invalid account expiry date `%s'", arg->val);
51252527Sdavidn				if (pwd->pw_expire != expire) {
51352527Sdavidn					pwd->pw_expire = expire;
51452527Sdavidn					edited = 1;
51552527Sdavidn				}
51620253Sjoerg			}
51720253Sjoerg		}
51820253Sjoerg
51952527Sdavidn		if ((arg = getarg(args, 's')) != NULL) {
52052527Sdavidn			char *shell = shell_path(cnf->shelldir, cnf->shells, arg->val);
52152527Sdavidn			if (shell == NULL)
52252527Sdavidn				shell = "";
52352527Sdavidn			if (strcmp(shell, pwd->pw_shell) != 0) {
52452527Sdavidn				pwd->pw_shell = shell;
52552527Sdavidn				edited = 1;
52652527Sdavidn			}
52752527Sdavidn		}
52820253Sjoerg
52952527Sdavidn		if (getarg(args, 'L')) {
53052527Sdavidn			if (cnf->default_class == NULL)
53152527Sdavidn				cnf->default_class = "";
53252527Sdavidn			if (strcmp(pwd->pw_class, cnf->default_class) != 0) {
53352527Sdavidn				pwd->pw_class = cnf->default_class;
53452527Sdavidn				edited = 1;
53552527Sdavidn			}
53652527Sdavidn		}
53752527Sdavidn
53820747Sdavidn		if ((arg  = getarg(args, 'd')) != NULL) {
53962461Sdavidn			edited = strcmp(pwd->pw_dir, arg->val) != 0;
54020747Sdavidn			if (stat(pwd->pw_dir = arg->val, &st) == -1) {
54120747Sdavidn				if (getarg(args, 'm') == NULL && strcmp(pwd->pw_dir, "/nonexistent") != 0)
54230259Scharnier				  warnx("WARNING: home `%s' does not exist", pwd->pw_dir);
54320747Sdavidn			} else if (!S_ISDIR(st.st_mode))
54430259Scharnier				warnx("WARNING: home `%s' is not a directory", pwd->pw_dir);
54520747Sdavidn		}
54620747Sdavidn
54752527Sdavidn		if ((arg = getarg(args, 'w')) != NULL && getarg(args, 'h') == NULL) {
54864918Sgreen			login_cap_t *lc;
54964918Sgreen
55064918Sgreen			lc = login_getpwclass(pwd);
55164918Sgreen			if (lc == NULL ||
55264918Sgreen			    login_setcryptfmt(lc, "md5", NULL) == NULL)
55364918Sgreen				warn("setting crypt(3) format");
55464918Sgreen			login_close(lc);
55520267Sjoerg			pwd->pw_passwd = pw_password(cnf, args, pwd->pw_name);
55652527Sdavidn			edited = 1;
55752527Sdavidn		}
55820267Sjoerg
55920253Sjoerg	} else {
56064918Sgreen		login_cap_t *lc;
56152527Sdavidn
56252527Sdavidn		/*
56352527Sdavidn		 * Add code
56452527Sdavidn		 */
56552527Sdavidn
56620253Sjoerg		if (a_name == NULL)	/* Required */
56730259Scharnier			errx(EX_DATAERR, "login name required");
56844229Sdavidn		else if ((pwd = GETPWNAM(a_name->val)) != NULL)	/* Exists */
56930259Scharnier			errx(EX_DATAERR, "login name `%s' already exists", a_name->val);
57020253Sjoerg
57120253Sjoerg		/*
57220253Sjoerg		 * Now, set up defaults for a new user
57320253Sjoerg		 */
57420253Sjoerg		pwd = &fakeuser;
57520253Sjoerg		pwd->pw_name = a_name->val;
57620253Sjoerg		pwd->pw_class = cnf->default_class ? cnf->default_class : "";
57720253Sjoerg		pwd->pw_uid = pw_uidpolicy(cnf, args);
57820253Sjoerg		pwd->pw_gid = pw_gidpolicy(cnf, args, pwd->pw_name, (gid_t) pwd->pw_uid);
57920253Sjoerg		pwd->pw_change = pw_pwdpolicy(cnf, args);
58020253Sjoerg		pwd->pw_expire = pw_exppolicy(cnf, args);
58120253Sjoerg		pwd->pw_dir = pw_homepolicy(cnf, args, pwd->pw_name);
58220253Sjoerg		pwd->pw_shell = pw_shellpolicy(cnf, args, NULL);
58364918Sgreen		lc = login_getpwclass(pwd);
58464918Sgreen		if (lc == NULL || login_setcryptfmt(lc, "md5", NULL) == NULL)
58564918Sgreen			warn("setting crypt(3) format");
58664918Sgreen		login_close(lc);
58764918Sgreen		pwd->pw_passwd = pw_password(cnf, args, pwd->pw_name);
58852527Sdavidn		edited = 1;
58920253Sjoerg
59020253Sjoerg		if (pwd->pw_uid == 0 && strcmp(pwd->pw_name, "root") != 0)
59130259Scharnier			warnx("WARNING: new account `%s' has a uid of 0 (superuser access!)", pwd->pw_name);
59220253Sjoerg	}
59320253Sjoerg
59420253Sjoerg	/*
59520253Sjoerg	 * Shared add/edit code
59620253Sjoerg	 */
59752527Sdavidn	if ((arg = getarg(args, 'c')) != NULL) {
59852527Sdavidn		char	*gecos = pw_checkname((u_char *)arg->val, 1);
59952527Sdavidn		if (strcmp(pwd->pw_gecos, gecos) != 0) {
60052527Sdavidn			pwd->pw_gecos = gecos;
60152527Sdavidn			edited = 1;
60252527Sdavidn		}
60352527Sdavidn	}
60420253Sjoerg
60520253Sjoerg	if ((arg = getarg(args, 'h')) != NULL) {
60663572Sdavidn		if (strcmp(arg->val, "-") == 0) {
60763572Sdavidn			if (!pwd->pw_passwd || *pwd->pw_passwd != '*') {
60863572Sdavidn				pwd->pw_passwd = "*";	/* No access */
60963572Sdavidn				edited = 1;
61063572Sdavidn			}
61163572Sdavidn		} else {
61220253Sjoerg			int             fd = atoi(arg->val);
61320253Sjoerg			int             b;
61420253Sjoerg			int             istty = isatty(fd);
61520253Sjoerg			struct termios  t;
61664918Sgreen			login_cap_t	*lc;
61720253Sjoerg
61820253Sjoerg			if (istty) {
61920253Sjoerg				if (tcgetattr(fd, &t) == -1)
62020253Sjoerg					istty = 0;
62120253Sjoerg				else {
62220253Sjoerg					struct termios  n = t;
62320253Sjoerg
62420253Sjoerg					/* Disable echo */
62520253Sjoerg					n.c_lflag &= ~(ECHO);
62620253Sjoerg					tcsetattr(fd, TCSANOW, &n);
62720253Sjoerg					printf("%sassword for user %s:", (mode == M_UPDATE) ? "New p" : "P", pwd->pw_name);
62820253Sjoerg					fflush(stdout);
62920253Sjoerg				}
63020253Sjoerg			}
63120253Sjoerg			b = read(fd, line, sizeof(line) - 1);
63220253Sjoerg			if (istty) {	/* Restore state */
63320253Sjoerg				tcsetattr(fd, TCSANOW, &t);
63420253Sjoerg				fputc('\n', stdout);
63520253Sjoerg				fflush(stdout);
63620253Sjoerg			}
63720253Sjoerg			if (b < 0) {
63830259Scharnier				warn("-h file descriptor");
63920267Sjoerg				return EX_IOERR;
64020253Sjoerg			}
64120253Sjoerg			line[b] = '\0';
64220253Sjoerg			if ((p = strpbrk(line, " \t\r\n")) != NULL)
64320253Sjoerg				*p = '\0';
64420253Sjoerg			if (!*line)
64530259Scharnier				errx(EX_DATAERR, "empty password read on file descriptor %d", fd);
64664918Sgreen			lc = login_getpwclass(pwd);
64764918Sgreen			if (lc == NULL ||
64864918Sgreen			    login_setcryptfmt(lc, "md5", NULL) == NULL)
64964918Sgreen				warn("setting crypt(3) format");
65064918Sgreen			login_close(lc);
65120253Sjoerg			pwd->pw_passwd = pw_pwcrypt(line);
65252527Sdavidn			edited = 1;
65320253Sjoerg		}
65420253Sjoerg	}
65520267Sjoerg
65620267Sjoerg	/*
65720267Sjoerg	 * Special case: -N only displays & exits
65820267Sjoerg	 */
65920267Sjoerg	if (getarg(args, 'N') != NULL)
66044386Sdavidn		return print_user(pwd,
66144386Sdavidn				  getarg(args, 'P') != NULL,
66244386Sdavidn				  getarg(args, '7') != NULL);
66320267Sjoerg
66421330Sdavidn	if (mode == M_ADD) {
66552527Sdavidn		edited = 1;	/* Always */
66652502Sdavidn		rc = addpwent(pwd);
66752502Sdavidn		if (rc == -1) {
66852502Sdavidn			warnx("user '%s' already exists", pwd->pw_name);
66952502Sdavidn			return EX_IOERR;
67052502Sdavidn		} else if (rc != 0) {
67156000Sdavidn			warn("passwd file update");
67252502Sdavidn			return EX_IOERR;
67352502Sdavidn		}
67452502Sdavidn		if (cnf->nispasswd && *cnf->nispasswd=='/') {
67552502Sdavidn			rc = addnispwent(cnf->nispasswd, pwd);
67652502Sdavidn			if (rc == -1)
67752502Sdavidn				warnx("User '%s' already exists in NIS passwd", pwd->pw_name);
67852502Sdavidn			else
67956000Sdavidn				warn("NIS passwd update");
68052502Sdavidn			/* NOTE: we treat NIS-only update errors as non-fatal */
68152502Sdavidn		}
68252512Sdavidn	} else if (mode == M_UPDATE || mode == M_LOCK || mode == M_UNLOCK) {
68352527Sdavidn		if (edited) {	/* Only updated this if required */
68452527Sdavidn			rc = chgpwent(a_name->val, pwd);
68552527Sdavidn			if (rc == -1) {
68652527Sdavidn				warnx("user '%s' does not exist (NIS?)", pwd->pw_name);
68752527Sdavidn				return EX_IOERR;
68852527Sdavidn			} else if (rc != 0) {
68956000Sdavidn				warn("passwd file update");
69052527Sdavidn				return EX_IOERR;
69152527Sdavidn			}
69252527Sdavidn			if ( cnf->nispasswd && *cnf->nispasswd=='/') {
69352527Sdavidn				rc = chgnispwent(cnf->nispasswd, a_name->val, pwd);
69452527Sdavidn				if (rc == -1)
69552527Sdavidn					warn("User '%s' not found in NIS passwd", pwd->pw_name);
69652527Sdavidn				else
69756000Sdavidn					warn("NIS passwd update");
69852527Sdavidn				/* NOTE: NIS-only update errors are not fatal */
69952527Sdavidn			}
70052502Sdavidn		}
70121330Sdavidn	}
70221330Sdavidn
70320253Sjoerg	/*
70420253Sjoerg	 * Ok, user is created or changed - now edit group file
70520253Sjoerg	 */
70620253Sjoerg
70720253Sjoerg	if (mode == M_ADD || getarg(args, 'G') != NULL)
70820253Sjoerg		editgroups(pwd->pw_name, cnf->groups);
70920253Sjoerg
71061759Sdavidn	/* go get a current version of pwd */
71161759Sdavidn	pwd = GETPWNAM(a_name->val);
71261759Sdavidn	if (pwd == NULL) {
71361759Sdavidn		/* This will fail when we rename, so special case that */
71461759Sdavidn		if (mode == M_UPDATE && (arg = getarg(args, 'l')) != NULL) {
71561759Sdavidn			a_name->val = arg->val;		/* update new name */
71661759Sdavidn			pwd = GETPWNAM(a_name->val);	/* refetch renamed rec */
71761759Sdavidn		}
71861759Sdavidn	}
71961759Sdavidn	if (pwd == NULL)	/* can't go on without this */
72030259Scharnier		errx(EX_NOUSER, "user '%s' disappeared during update", a_name->val);
72120253Sjoerg
72244229Sdavidn	grp = GETGRGID(pwd->pw_gid);
72320253Sjoerg	pw_log(cnf, mode, W_USER, "%s(%ld):%s(%d):%s:%s:%s",
72420253Sjoerg	       pwd->pw_name, (long) pwd->pw_uid,
72520253Sjoerg	    grp ? grp->gr_name : "unknown", (long) (grp ? grp->gr_gid : -1),
72620253Sjoerg	       pwd->pw_gecos, pwd->pw_dir, pwd->pw_shell);
72720253Sjoerg
72820253Sjoerg	/*
72920253Sjoerg	 * If adding, let's touch and chown the user's mail file. This is not
73020253Sjoerg	 * strictly necessary under BSD with a 0755 maildir but it also
73120253Sjoerg	 * doesn't hurt anything to create the empty mailfile
73220253Sjoerg	 */
73320253Sjoerg	if (mode == M_ADD) {
73420253Sjoerg		FILE           *fp;
73520253Sjoerg
73644229Sdavidn		if (!PWALTDIR()) {
73744229Sdavidn			sprintf(line, "%s/%s", _PATH_MAILDIR, pwd->pw_name);
73844229Sdavidn			close(open(line, O_RDWR | O_CREAT, 0600));	/* Preserve contents &
73944229Sdavidn									 * mtime */
74044229Sdavidn			chown(line, pwd->pw_uid, pwd->pw_gid);
74120253Sjoerg
74244229Sdavidn			/*
74344229Sdavidn			 * Send mail to the new user as well, if we are asked to
74444229Sdavidn			 */
74544229Sdavidn			if (cnf->newmail && *cnf->newmail && (fp = fopen(cnf->newmail, "r")) != NULL) {
74644229Sdavidn				FILE           *pfp = popen(_PATH_SENDMAIL " -t", "w");
74720253Sjoerg
74844229Sdavidn				if (pfp == NULL)
74944229Sdavidn					warn("sendmail");
75044229Sdavidn				else {
75144229Sdavidn					fprintf(pfp, "From: root\n" "To: %s\n" "Subject: Welcome!\n\n", pwd->pw_name);
75244229Sdavidn					while (fgets(line, sizeof(line), fp) != NULL) {
75344229Sdavidn						/* Do substitutions? */
75444229Sdavidn						fputs(line, pfp);
75544229Sdavidn					}
75644229Sdavidn					pclose(pfp);
75744229Sdavidn					pw_log(cnf, mode, W_USER, "%s(%ld) new user mail sent",
75844229Sdavidn					       pwd->pw_name, (long) pwd->pw_uid);
75920253Sjoerg				}
76044229Sdavidn				fclose(fp);
76120253Sjoerg			}
76220253Sjoerg		}
76320253Sjoerg	}
76452527Sdavidn
76520253Sjoerg	/*
76620253Sjoerg	 * Finally, let's create and populate the user's home directory. Note
76720253Sjoerg	 * that this also `works' for editing users if -m is used, but
76820253Sjoerg	 * existing files will *not* be overwritten.
76920253Sjoerg	 */
77044229Sdavidn	if (!PWALTDIR() && getarg(args, 'm') != NULL && pwd->pw_dir && *pwd->pw_dir == '/' && pwd->pw_dir[1]) {
77120253Sjoerg		copymkdir(pwd->pw_dir, cnf->dotdir, 0755, pwd->pw_uid, pwd->pw_gid);
77220253Sjoerg		pw_log(cnf, mode, W_USER, "%s(%ld) home %s made",
77320253Sjoerg		       pwd->pw_name, (long) pwd->pw_uid, pwd->pw_dir);
77420253Sjoerg	}
77552527Sdavidn
77620267Sjoerg	return EXIT_SUCCESS;
77720253Sjoerg}
77820253Sjoerg
77920253Sjoerg
78020253Sjoergstatic          uid_t
78120253Sjoergpw_uidpolicy(struct userconf * cnf, struct cargs * args)
78220253Sjoerg{
78320253Sjoerg	struct passwd  *pwd;
78420253Sjoerg	uid_t           uid = (uid_t) - 1;
78520253Sjoerg	struct carg    *a_uid = getarg(args, 'u');
78620253Sjoerg
78720253Sjoerg	/*
78820253Sjoerg	 * Check the given uid, if any
78920253Sjoerg	 */
79020253Sjoerg	if (a_uid != NULL) {
79120253Sjoerg		uid = (uid_t) atol(a_uid->val);
79220253Sjoerg
79344229Sdavidn		if ((pwd = GETPWUID(uid)) != NULL && getarg(args, 'o') == NULL)
79430259Scharnier			errx(EX_DATAERR, "uid `%ld' has already been allocated", (long) pwd->pw_uid);
79520253Sjoerg	} else {
79620253Sjoerg		struct bitmap   bm;
79720253Sjoerg
79820253Sjoerg		/*
79920253Sjoerg		 * We need to allocate the next available uid under one of
80020253Sjoerg		 * two policies a) Grab the first unused uid b) Grab the
80120253Sjoerg		 * highest possible unused uid
80220253Sjoerg		 */
80320253Sjoerg		if (cnf->min_uid >= cnf->max_uid) {	/* Sanity
80420253Sjoerg							 * claus^H^H^H^Hheck */
80520253Sjoerg			cnf->min_uid = 1000;
80620253Sjoerg			cnf->max_uid = 32000;
80720253Sjoerg		}
80820253Sjoerg		bm = bm_alloc(cnf->max_uid - cnf->min_uid + 1);
80920253Sjoerg
81020253Sjoerg		/*
81120253Sjoerg		 * Now, let's fill the bitmap from the password file
81220253Sjoerg		 */
81344229Sdavidn		SETPWENT();
81444229Sdavidn		while ((pwd = GETPWENT()) != NULL)
81544229Sdavidn			if (pwd->pw_uid >= (uid_t) cnf->min_uid && pwd->pw_uid <= (uid_t) cnf->max_uid)
81620253Sjoerg				bm_setbit(&bm, pwd->pw_uid - cnf->min_uid);
81744229Sdavidn		ENDPWENT();
81820253Sjoerg
81920253Sjoerg		/*
82020253Sjoerg		 * Then apply the policy, with fallback to reuse if necessary
82120253Sjoerg		 */
82220253Sjoerg		if (cnf->reuse_uids || (uid = (uid_t) (bm_lastset(&bm) + cnf->min_uid + 1)) > cnf->max_uid)
82320253Sjoerg			uid = (uid_t) (bm_firstunset(&bm) + cnf->min_uid);
82420253Sjoerg
82520253Sjoerg		/*
82620253Sjoerg		 * Another sanity check
82720253Sjoerg		 */
82820253Sjoerg		if (uid < cnf->min_uid || uid > cnf->max_uid)
82930259Scharnier			errx(EX_SOFTWARE, "unable to allocate a new uid - range fully used");
83020253Sjoerg		bm_dealloc(&bm);
83120253Sjoerg	}
83220253Sjoerg	return uid;
83320253Sjoerg}
83420253Sjoerg
83520253Sjoerg
83620253Sjoergstatic          uid_t
83720253Sjoergpw_gidpolicy(struct userconf * cnf, struct cargs * args, char *nam, gid_t prefer)
83820253Sjoerg{
83920253Sjoerg	struct group   *grp;
84020253Sjoerg	gid_t           gid = (uid_t) - 1;
84120253Sjoerg	struct carg    *a_gid = getarg(args, 'g');
84220253Sjoerg
84320253Sjoerg	/*
84420253Sjoerg	 * If no arg given, see if default can help out
84520253Sjoerg	 */
84620253Sjoerg	if (a_gid == NULL && cnf->default_group && *cnf->default_group)
84720253Sjoerg		a_gid = addarg(args, 'g', cnf->default_group);
84820253Sjoerg
84920253Sjoerg	/*
85020253Sjoerg	 * Check the given gid, if any
85120253Sjoerg	 */
85244229Sdavidn	SETGRENT();
85320253Sjoerg	if (a_gid != NULL) {
85444229Sdavidn		if ((grp = GETGRNAM(a_gid->val)) == NULL) {
85520253Sjoerg			gid = (gid_t) atol(a_gid->val);
85661957Sache			if ((gid == 0 && !isdigit((unsigned char)*a_gid->val)) || (grp = GETGRGID(gid)) == NULL)
85730259Scharnier				errx(EX_NOUSER, "group `%s' is not defined", a_gid->val);
85820253Sjoerg		}
85920253Sjoerg		gid = grp->gr_gid;
86044229Sdavidn	} else if ((grp = GETGRNAM(nam)) != NULL && grp->gr_mem[0] == NULL) {
86120267Sjoerg		gid = grp->gr_gid;  /* Already created? Use it anyway... */
86220253Sjoerg	} else {
86320253Sjoerg		struct cargs    grpargs;
86420253Sjoerg		char            tmp[32];
86520253Sjoerg
86620253Sjoerg		LIST_INIT(&grpargs);
86720253Sjoerg		addarg(&grpargs, 'n', nam);
86820253Sjoerg
86920253Sjoerg		/*
87020253Sjoerg		 * We need to auto-create a group with the user's name. We
87120253Sjoerg		 * can send all the appropriate output to our sister routine
87220253Sjoerg		 * bit first see if we can create a group with gid==uid so we
87320253Sjoerg		 * can keep the user and group ids in sync. We purposely do
87420253Sjoerg		 * NOT check the gid range if we can force the sync. If the
87520253Sjoerg		 * user's name dups an existing group, then the group add
87620253Sjoerg		 * function will happily handle that case for us and exit.
87720253Sjoerg		 */
87844229Sdavidn		if (GETGRGID(prefer) == NULL) {
87920253Sjoerg			sprintf(tmp, "%lu", (unsigned long) prefer);
88020253Sjoerg			addarg(&grpargs, 'g', tmp);
88120253Sjoerg		}
88220267Sjoerg		if (getarg(args, 'N'))
88320267Sjoerg		{
88420267Sjoerg			addarg(&grpargs, 'N', NULL);
88520267Sjoerg			addarg(&grpargs, 'q', NULL);
88620267Sjoerg			gid = pw_group(cnf, M_NEXT, &grpargs);
88720267Sjoerg		}
88820267Sjoerg		else
88920267Sjoerg		{
89020267Sjoerg			pw_group(cnf, M_ADD, &grpargs);
89144229Sdavidn			if ((grp = GETGRNAM(nam)) != NULL)
89220267Sjoerg				gid = grp->gr_gid;
89320267Sjoerg		}
89470486Sben		a_gid = LIST_FIRST(&grpargs);
89520253Sjoerg		while (a_gid != NULL) {
89670486Sben			struct carg    *t = LIST_NEXT(a_gid, list);
89720253Sjoerg			LIST_REMOVE(a_gid, list);
89820253Sjoerg			a_gid = t;
89920253Sjoerg		}
90020253Sjoerg	}
90144229Sdavidn	ENDGRENT();
90220253Sjoerg	return gid;
90320253Sjoerg}
90420253Sjoerg
90520253Sjoerg
90620253Sjoergstatic          time_t
90720253Sjoergpw_pwdpolicy(struct userconf * cnf, struct cargs * args)
90820253Sjoerg{
90920253Sjoerg	time_t          result = 0;
91020253Sjoerg	time_t          now = time(NULL);
91127831Sdavidn	struct carg    *arg = getarg(args, 'p');
91220253Sjoerg
91320253Sjoerg	if (arg != NULL) {
91420253Sjoerg		if ((result = parse_date(now, arg->val)) == now)
91530259Scharnier			errx(EX_DATAERR, "invalid date/time `%s'", arg->val);
91620253Sjoerg	} else if (cnf->password_days > 0)
91720253Sjoerg		result = now + ((long) cnf->password_days * 86400L);
91820253Sjoerg	return result;
91920253Sjoerg}
92020253Sjoerg
92120253Sjoerg
92220253Sjoergstatic          time_t
92320253Sjoergpw_exppolicy(struct userconf * cnf, struct cargs * args)
92420253Sjoerg{
92520253Sjoerg	time_t          result = 0;
92620253Sjoerg	time_t          now = time(NULL);
92720253Sjoerg	struct carg    *arg = getarg(args, 'e');
92820253Sjoerg
92920253Sjoerg	if (arg != NULL) {
93020253Sjoerg		if ((result = parse_date(now, arg->val)) == now)
93130259Scharnier			errx(EX_DATAERR, "invalid date/time `%s'", arg->val);
93220253Sjoerg	} else if (cnf->expire_days > 0)
93320253Sjoerg		result = now + ((long) cnf->expire_days * 86400L);
93420253Sjoerg	return result;
93520253Sjoerg}
93620253Sjoerg
93720253Sjoerg
93820253Sjoergstatic char    *
93920253Sjoergpw_homepolicy(struct userconf * cnf, struct cargs * args, char const * user)
94020253Sjoerg{
94120253Sjoerg	struct carg    *arg = getarg(args, 'd');
94220253Sjoerg
94320253Sjoerg	if (arg)
94420253Sjoerg		return arg->val;
94520253Sjoerg	else {
94620253Sjoerg		static char     home[128];
94720253Sjoerg
94820253Sjoerg		if (cnf->home == NULL || *cnf->home == '\0')
94930259Scharnier			errx(EX_CONFIG, "no base home directory set");
95020253Sjoerg		sprintf(home, "%s/%s", cnf->home, user);
95120253Sjoerg		return home;
95220253Sjoerg	}
95320253Sjoerg}
95420253Sjoerg
95520253Sjoergstatic char    *
95620253Sjoergshell_path(char const * path, char *shells[], char *sh)
95720253Sjoerg{
95820253Sjoerg	if (sh != NULL && (*sh == '/' || *sh == '\0'))
95920253Sjoerg		return sh;	/* specified full path or forced none */
96020253Sjoerg	else {
96120253Sjoerg		char           *p;
96220253Sjoerg		char            paths[_UC_MAXLINE];
96320253Sjoerg
96420253Sjoerg		/*
96520253Sjoerg		 * We need to search paths
96620253Sjoerg		 */
96720253Sjoerg		strncpy(paths, path, sizeof paths);
96820253Sjoerg		paths[sizeof paths - 1] = '\0';
96920253Sjoerg		for (p = strtok(paths, ": \t\r\n"); p != NULL; p = strtok(NULL, ": \t\r\n")) {
97020253Sjoerg			int             i;
97120253Sjoerg			static char     shellpath[256];
97220253Sjoerg
97320253Sjoerg			if (sh != NULL) {
97420253Sjoerg				sprintf(shellpath, "%s/%s", p, sh);
97520253Sjoerg				if (access(shellpath, X_OK) == 0)
97620253Sjoerg					return shellpath;
97720253Sjoerg			} else
97820253Sjoerg				for (i = 0; i < _UC_MAXSHELLS && shells[i] != NULL; i++) {
97920253Sjoerg					sprintf(shellpath, "%s/%s", p, shells[i]);
98020253Sjoerg					if (access(shellpath, X_OK) == 0)
98120253Sjoerg						return shellpath;
98220253Sjoerg				}
98320253Sjoerg		}
98420253Sjoerg		if (sh == NULL)
98530259Scharnier			errx(EX_OSFILE, "can't find shell `%s' in shell paths", sh);
98630259Scharnier		errx(EX_CONFIG, "no default shell available or defined");
98720253Sjoerg		return NULL;
98820253Sjoerg	}
98920253Sjoerg}
99020253Sjoerg
99120253Sjoerg
99220253Sjoergstatic char    *
99320253Sjoergpw_shellpolicy(struct userconf * cnf, struct cargs * args, char *newshell)
99420253Sjoerg{
99520253Sjoerg	char           *sh = newshell;
99620253Sjoerg	struct carg    *arg = getarg(args, 's');
99720253Sjoerg
99820253Sjoerg	if (newshell == NULL && arg != NULL)
99920253Sjoerg		sh = arg->val;
100020253Sjoerg	return shell_path(cnf->shelldir, cnf->shells, sh ? sh : cnf->shell_default);
100120253Sjoerg}
100220253Sjoerg
100320253Sjoergstatic char const chars[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.";
100420253Sjoerg
100520253Sjoergchar           *
100620253Sjoergpw_pwcrypt(char *password)
100720253Sjoerg{
100820253Sjoerg	int             i;
100920253Sjoerg	char            salt[12];
101020253Sjoerg
101120253Sjoerg	static char     buf[256];
101220253Sjoerg
101320253Sjoerg	/*
101420253Sjoerg	 * Calculate a salt value
101520253Sjoerg	 */
101624214Sache	if (!randinit) {
101724214Sache		randinit = 1;
101824214Sache#ifdef __FreeBSD__
101926626Sache		srandomdev();
102026626Sache#else
102126626Sache		srandom((unsigned long) (time(NULL) ^ getpid()));
102224214Sache#endif
102324214Sache	}
102420253Sjoerg	for (i = 0; i < 8; i++)
102520253Sjoerg		salt[i] = chars[random() % 63];
102620253Sjoerg	salt[i] = '\0';
102720253Sjoerg
102820253Sjoerg	return strcpy(buf, crypt(password, salt));
102920253Sjoerg}
103020253Sjoerg
103120590Sdavidn#if defined(USE_MD5RAND)
103220555Sdavidnu_char *
103320590Sdavidnpw_getrand(u_char *buf, int len)	/* cryptographically secure rng */
103420555Sdavidn{
103520590Sdavidn	int i;
103620590Sdavidn	for (i=0;i<len;i+=16) {
103720590Sdavidn		u_char ubuf[16];
103820590Sdavidn
103920590Sdavidn		MD5_CTX md5_ctx;
104020590Sdavidn		struct timeval tv, tvo;
104120590Sdavidn		struct rusage ru;
104220590Sdavidn		int n=0;
104320590Sdavidn		int t;
104420590Sdavidn
104520590Sdavidn		MD5Init (&md5_ctx);
104620590Sdavidn		t=getpid();
104720590Sdavidn		MD5Update (&md5_ctx, (u_char*)&t, sizeof t);
104820590Sdavidn		t=getppid();
104920590Sdavidn		MD5Update (&md5_ctx, (u_char*)&t, sizeof t);
105020590Sdavidn		gettimeofday (&tvo, NULL);
105120590Sdavidn		do {
105220590Sdavidn			getrusage (RUSAGE_SELF, &ru);
105320590Sdavidn			MD5Update (&md5_ctx, (u_char*)&ru, sizeof ru);
105420590Sdavidn			gettimeofday (&tv, NULL);
105520590Sdavidn			MD5Update (&md5_ctx, (u_char*)&tv, sizeof tv);
105620590Sdavidn		} while (n++<20 || tv.tv_usec-tvo.tv_usec<100*1000);
105720590Sdavidn		MD5Final (ubuf, &md5_ctx);
105820590Sdavidn		memcpy(buf+i, ubuf, MIN(16, len-n));
105920590Sdavidn	}
106020590Sdavidn	return buf;
106120555Sdavidn}
106220253Sjoerg
106320590Sdavidn#else	/* Portable version */
106420590Sdavidn
106520590Sdavidnstatic u_char *
106620590Sdavidnpw_getrand(u_char *buf, int len)
106720590Sdavidn{
106820590Sdavidn	int i;
106920590Sdavidn
107020590Sdavidn	for (i = 0; i < len; i++) {
107124214Sache		unsigned long val = random();
107220590Sdavidn		/* Use all bits in the random value */
107320590Sdavidn		buf[i]=(u_char)((val >> 24) ^ (val >> 16) ^ (val >> 8) ^ val);
107420590Sdavidn	}
107520590Sdavidn	return buf;
107620590Sdavidn}
107720590Sdavidn
107820590Sdavidn#endif
107920590Sdavidn
108020253Sjoergstatic char    *
108120253Sjoergpw_password(struct userconf * cnf, struct cargs * args, char const * user)
108220253Sjoerg{
108320253Sjoerg	int             i, l;
108420253Sjoerg	char            pwbuf[32];
108520555Sdavidn	u_char		rndbuf[sizeof pwbuf];
108620253Sjoerg
108720253Sjoerg	switch (cnf->default_password) {
108820253Sjoerg	case -1:		/* Random password */
108924214Sache		if (!randinit) {
109024214Sache			randinit = 1;
109124214Sache#ifdef __FreeBSD__
109226626Sache			srandomdev();
109326626Sache#else
109426626Sache			srandom((unsigned long) (time(NULL) ^ getpid()));
109524214Sache#endif
109624214Sache		}
109720253Sjoerg		l = (random() % 8 + 8);	/* 8 - 16 chars */
109820555Sdavidn		pw_getrand(rndbuf, l);
109920253Sjoerg		for (i = 0; i < l; i++)
110042245Sbillf			pwbuf[i] = chars[rndbuf[i] % (sizeof(chars)-1)];
110120253Sjoerg		pwbuf[i] = '\0';
110220253Sjoerg
110320253Sjoerg		/*
110420253Sjoerg		 * We give this information back to the user
110520253Sjoerg		 */
110620267Sjoerg		if (getarg(args, 'h') == NULL && getarg(args, 'N') == NULL) {
110761957Sache			if (isatty(STDOUT_FILENO))
110820712Sdavidn				printf("Password for '%s' is: ", user);
110920253Sjoerg			printf("%s\n", pwbuf);
111020253Sjoerg			fflush(stdout);
111120253Sjoerg		}
111220253Sjoerg		break;
111320253Sjoerg
111420253Sjoerg	case -2:		/* No password at all! */
111520253Sjoerg		return "";
111620253Sjoerg
111720253Sjoerg	case 0:		/* No login - default */
111820253Sjoerg	default:
111920253Sjoerg		return "*";
112020253Sjoerg
112120253Sjoerg	case 1:		/* user's name */
112220253Sjoerg		strncpy(pwbuf, user, sizeof pwbuf);
112320253Sjoerg		pwbuf[sizeof pwbuf - 1] = '\0';
112420253Sjoerg		break;
112520253Sjoerg	}
112620253Sjoerg	return pw_pwcrypt(pwbuf);
112720253Sjoerg}
112820253Sjoerg
112920253Sjoerg
113020253Sjoergstatic int
113144386Sdavidnprint_user(struct passwd * pwd, int pretty, int v7)
113220253Sjoerg{
113320253Sjoerg	if (!pretty) {
113420253Sjoerg		char            buf[_UC_MAXLINE];
113520253Sjoerg
113644386Sdavidn		fmtpwentry(buf, pwd, v7 ? PWF_PASSWD : PWF_STANDARD);
113720253Sjoerg		fputs(buf, stdout);
113820253Sjoerg	} else {
113920267Sjoerg		int		j;
114020253Sjoerg		char           *p;
114144229Sdavidn		struct group   *grp = GETGRGID(pwd->pw_gid);
114220253Sjoerg		char            uname[60] = "User &", office[60] = "[None]",
114320253Sjoerg		                wphone[60] = "[None]", hphone[60] = "[None]";
114420590Sdavidn		char		acexpire[32] = "[None]", pwexpire[32] = "[None]";
114520590Sdavidn		struct tm *    tptr;
114620253Sjoerg
114720253Sjoerg		if ((p = strtok(pwd->pw_gecos, ",")) != NULL) {
114820253Sjoerg			strncpy(uname, p, sizeof uname);
114920253Sjoerg			uname[sizeof uname - 1] = '\0';
115020253Sjoerg			if ((p = strtok(NULL, ",")) != NULL) {
115120253Sjoerg				strncpy(office, p, sizeof office);
115220253Sjoerg				office[sizeof office - 1] = '\0';
115320253Sjoerg				if ((p = strtok(NULL, ",")) != NULL) {
115420253Sjoerg					strncpy(wphone, p, sizeof wphone);
115520253Sjoerg					wphone[sizeof wphone - 1] = '\0';
115620253Sjoerg					if ((p = strtok(NULL, "")) != NULL) {
115720253Sjoerg						strncpy(hphone, p, sizeof hphone);
115820253Sjoerg						hphone[sizeof hphone - 1] = '\0';
115920253Sjoerg					}
116020253Sjoerg				}
116120253Sjoerg			}
116220253Sjoerg		}
116320253Sjoerg		/*
116420253Sjoerg		 * Handle '&' in gecos field
116520253Sjoerg		 */
116620253Sjoerg		if ((p = strchr(uname, '&')) != NULL) {
116720253Sjoerg			int             l = strlen(pwd->pw_name);
116820253Sjoerg			int             m = strlen(p);
116920253Sjoerg
117020253Sjoerg			memmove(p + l, p + 1, m);
117120253Sjoerg			memmove(p, pwd->pw_name, l);
117261957Sache			*p = (char) toupper((unsigned char)*p);
117320253Sjoerg		}
117420590Sdavidn		if (pwd->pw_expire > (time_t)0 && (tptr = localtime(&pwd->pw_expire)) != NULL)
117562034Sache		  strftime(acexpire, sizeof acexpire, "%a %Ef %Y %X", tptr);
117661957Sache		if (pwd->pw_change > (time_t)0 && (tptr = localtime(&pwd->pw_change)) != NULL)
117762034Sache		  strftime(pwexpire, sizeof pwexpire, "%a %Ef %Y %X", tptr);
117822394Sdavidn		printf("Login Name: %-15s   #%-12ld Group: %-15s   #%ld\n"
117920747Sdavidn		       " Full Name: %s\n"
118020747Sdavidn		       "      Home: %-26.26s      Class: %s\n"
118120747Sdavidn		       "     Shell: %-26.26s     Office: %s\n"
118220747Sdavidn		       "Work Phone: %-26.26s Home Phone: %s\n"
118320747Sdavidn		       "Acc Expire: %-26.26s Pwd Expire: %s\n",
118420253Sjoerg		       pwd->pw_name, (long) pwd->pw_uid,
118520253Sjoerg		       grp ? grp->gr_name : "(invalid)", (long) pwd->pw_gid,
118620253Sjoerg		       uname, pwd->pw_dir, pwd->pw_class,
118720590Sdavidn		       pwd->pw_shell, office, wphone, hphone,
118820590Sdavidn		       acexpire, pwexpire);
118944229Sdavidn	        SETGRENT();
119020267Sjoerg		j = 0;
119144229Sdavidn		while ((grp=GETGRENT()) != NULL)
119220267Sjoerg		{
119320267Sjoerg			int     i = 0;
119420747Sdavidn			while (grp->gr_mem[i] != NULL)
119520267Sjoerg			{
119620267Sjoerg				if (strcmp(grp->gr_mem[i], pwd->pw_name)==0)
119720267Sjoerg				{
119820747Sdavidn					printf(j++ == 0 ? "    Groups: %s" : ",%s", grp->gr_name);
119920267Sjoerg					break;
120020267Sjoerg				}
120120267Sjoerg				++i;
120220267Sjoerg			}
120320267Sjoerg		}
120444229Sdavidn		ENDGRENT();
120561957Sache		printf("%s", j ? "\n" : "");
120620253Sjoerg	}
120720267Sjoerg	return EXIT_SUCCESS;
120820253Sjoerg}
120920253Sjoerg
121020679Sdavidnchar    *
121120679Sdavidnpw_checkname(u_char *name, int gecos)
121220253Sjoerg{
121320253Sjoerg	int             l = 0;
121421052Sdavidn	char const     *notch = gecos ? ":!@" : " ,\t:+&#%$^()!@~*?<>=|\\/\"";
121520253Sjoerg
121620253Sjoerg	while (name[l]) {
121721052Sdavidn		if (strchr(notch, name[l]) != NULL || name[l] < ' ' || name[l] == 127 ||
121821052Sdavidn			(!gecos && l==0 && name[l] == '-') ||	/* leading '-' */
121921052Sdavidn			(!gecos && name[l] & 0x80))	/* 8-bit */
122030259Scharnier			errx(EX_DATAERR, (name[l] >= ' ' && name[l] < 127)
122130259Scharnier					    ? "invalid character `%c' in field"
122230259Scharnier					    : "invalid character 0x%02x in field",
122320325Sjoerg					    name[l]);
122420253Sjoerg		++l;
122520253Sjoerg	}
122622394Sdavidn	if (!gecos && l > LOGNAMESIZE)
122730259Scharnier		errx(EX_DATAERR, "name too long `%s'", name);
122820679Sdavidn	return (char *)name;
122920253Sjoerg}
123020253Sjoerg
123120253Sjoerg
123220253Sjoergstatic void
123320253Sjoergrmat(uid_t uid)
123420253Sjoerg{
123520253Sjoerg	DIR            *d = opendir("/var/at/jobs");
123620253Sjoerg
123720253Sjoerg	if (d != NULL) {
123820253Sjoerg		struct dirent  *e;
123920253Sjoerg
124020253Sjoerg		while ((e = readdir(d)) != NULL) {
124120253Sjoerg			struct stat     st;
124220253Sjoerg
124320253Sjoerg			if (strncmp(e->d_name, ".lock", 5) != 0 &&
124420253Sjoerg			    stat(e->d_name, &st) == 0 &&
124520253Sjoerg			    !S_ISDIR(st.st_mode) &&
124620253Sjoerg			    st.st_uid == uid) {
124720253Sjoerg				char            tmp[MAXPATHLEN];
124820253Sjoerg
124920253Sjoerg				sprintf(tmp, "/usr/bin/atrm %s", e->d_name);
125020253Sjoerg				system(tmp);
125120253Sjoerg			}
125220253Sjoerg		}
125320253Sjoerg		closedir(d);
125420253Sjoerg	}
125520253Sjoerg}
125620747Sdavidn
125720747Sdavidnstatic void
125820747Sdavidnrmskey(char const * name)
125920747Sdavidn{
126020747Sdavidn	static const char etcskey[] = "/etc/skeykeys";
126121052Sdavidn	FILE   *fp = fopen(etcskey, "r+");
126220747Sdavidn
126321052Sdavidn	if (fp != NULL) {
126421052Sdavidn		char	tmp[1024];
126521052Sdavidn		off_t	atofs = 0;
126621052Sdavidn		int	length = strlen(name);
126720747Sdavidn
126821052Sdavidn		while (fgets(tmp, sizeof tmp, fp) != NULL) {
126921052Sdavidn			if (strncmp(name, tmp, length) == 0 && tmp[length]==' ') {
127021052Sdavidn				if (fseek(fp, atofs, SEEK_SET) == 0) {
127121052Sdavidn					fwrite("#", 1, 1, fp);	/* Comment username out */
127221052Sdavidn				}
127321052Sdavidn				break;
127420747Sdavidn			}
127521052Sdavidn			atofs = ftell(fp);
127620747Sdavidn		}
127721052Sdavidn		/*
127821052Sdavidn		 * If we got an error of any sort, don't update!
127921052Sdavidn		 */
128021052Sdavidn		fclose(fp);
128120747Sdavidn	}
128220747Sdavidn}
128320747Sdavidn
1284