pw_user.c revision 21052
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.
2520253Sjoerg *
2621052Sdavidn *	$Id: pw_user.c,v 1.9 1996/12/23 02:27:29 davidn Exp $
2720253Sjoerg */
2820253Sjoerg
2920253Sjoerg#include <unistd.h>
3020253Sjoerg#include <fcntl.h>
3120253Sjoerg#include <ctype.h>
3220253Sjoerg#include <paths.h>
3320253Sjoerg#include <sys/param.h>
3420253Sjoerg#include <dirent.h>
3520253Sjoerg#include <termios.h>
3620555Sdavidn#include <sys/types.h>
3720555Sdavidn#include <sys/time.h>
3820555Sdavidn#include <sys/resource.h>
3920555Sdavidn#include <md5.h>
4020253Sjoerg#include "pw.h"
4120253Sjoerg#include "bitmap.h"
4220253Sjoerg#include "pwupd.h"
4320253Sjoerg
4420253Sjoergstatic int      print_user(struct passwd * pwd, int pretty);
4520253Sjoergstatic uid_t    pw_uidpolicy(struct userconf * cnf, struct cargs * args);
4620253Sjoergstatic uid_t    pw_gidpolicy(struct userconf * cnf, struct cargs * args, char *nam, gid_t prefer);
4720253Sjoergstatic time_t   pw_pwdpolicy(struct userconf * cnf, struct cargs * args);
4820253Sjoergstatic time_t   pw_exppolicy(struct userconf * cnf, struct cargs * args);
4920253Sjoergstatic char    *pw_homepolicy(struct userconf * cnf, struct cargs * args, char const * user);
5020253Sjoergstatic char    *pw_shellpolicy(struct userconf * cnf, struct cargs * args, char *newshell);
5120253Sjoergstatic char    *pw_password(struct userconf * cnf, struct cargs * args, char const * user);
5220253Sjoergstatic char    *shell_path(char const * path, char *shells[], char *sh);
5320253Sjoergstatic void     rmat(uid_t uid);
5420747Sdavidnstatic void	rmskey(char const * name);
5520253Sjoerg
5620253Sjoerg/*-
5720253Sjoerg * -C config      configuration file
5820253Sjoerg * -q             quiet operation
5920253Sjoerg * -n name        login name
6020253Sjoerg * -u uid         user id
6120253Sjoerg * -c comment     user name/comment
6220253Sjoerg * -d directory   home directory
6320253Sjoerg * -e date        account expiry date
6420253Sjoerg * -p date        password expiry date
6520253Sjoerg * -g grp         primary group
6620253Sjoerg * -G grp1,grp2   additional groups
6720253Sjoerg * -m [ -k dir ]  create and set up home
6820253Sjoerg * -s shell       name of login shell
6920253Sjoerg * -o             duplicate uid ok
7020253Sjoerg * -L class       user class
7120253Sjoerg * -l name        new login name
7220253Sjoerg * -h fd          password filehandle
7320253Sjoerg * -F             force print or add
7420253Sjoerg *   Setting defaults:
7520253Sjoerg * -D             set user defaults
7620253Sjoerg * -b dir         default home root dir
7720253Sjoerg * -e period      default expiry period
7820253Sjoerg * -p period      default password change period
7920253Sjoerg * -g group       default group
8020253Sjoerg * -G             grp1,grp2.. default additional groups
8120253Sjoerg * -L class       default login class
8220253Sjoerg * -k dir         default home skeleton
8320253Sjoerg * -s shell       default shell
8420253Sjoerg * -w method      default password method
8520253Sjoerg */
8620253Sjoerg
8720253Sjoergint
8820253Sjoergpw_user(struct userconf * cnf, int mode, struct cargs * args)
8920253Sjoerg{
9020253Sjoerg	char           *p = NULL;
9120253Sjoerg	struct carg    *a_name;
9220253Sjoerg	struct carg    *a_uid;
9320253Sjoerg	struct carg    *arg;
9420253Sjoerg	struct passwd  *pwd = NULL;
9520253Sjoerg	struct group   *grp;
9620253Sjoerg	struct stat     st;
9720747Sdavidn	char            line[_PASSWORD_LEN+1];
9820253Sjoerg
9920253Sjoerg	static struct passwd fakeuser =
10020253Sjoerg	{
10120253Sjoerg		NULL,
10220253Sjoerg		"*",
10320253Sjoerg		-1,
10420253Sjoerg		-1,
10520253Sjoerg		0,
10620253Sjoerg		"",
10720253Sjoerg		"User &",
10820253Sjoerg		"/bin/sh",
10920253Sjoerg		0,
11020253Sjoerg		0
11120253Sjoerg	};
11220253Sjoerg
11320253Sjoerg	/*
11420267Sjoerg	 * With M_NEXT, we only need to return the
11520267Sjoerg	 * next uid to stdout
11620267Sjoerg	 */
11720267Sjoerg	if (mode == M_NEXT)
11820267Sjoerg	{
11920267Sjoerg		uid_t next = pw_uidpolicy(cnf, args);
12020267Sjoerg		if (getarg(args, 'q'))
12120267Sjoerg			return next;
12220267Sjoerg		printf("%ld:", (long)next);
12320267Sjoerg		pw_group(cnf, mode, args);
12420267Sjoerg		return EXIT_SUCCESS;
12520267Sjoerg	}
12620267Sjoerg
12720267Sjoerg	/*
12820253Sjoerg	 * We can do all of the common legwork here
12920253Sjoerg	 */
13020253Sjoerg
13120253Sjoerg	if ((arg = getarg(args, 'b')) != NULL) {
13220267Sjoerg		cnf->home = arg->val;
13320253Sjoerg	}
13421052Sdavidn
13521052Sdavidn	/*
13621052Sdavidn	 * If we'll need to use it or we're updating it,
13721052Sdavidn	 * then create the base home directory if necessary
13821052Sdavidn	 */
13921052Sdavidn	if (arg != NULL || getarg(args, 'm') != NULL) {
14021052Sdavidn		int	l = strlen(cnf->home);
14121052Sdavidn
14221052Sdavidn		if (l > 1 && cnf->home[l-1] == '/')	/* Shave off any trailing path delimiter */
14321052Sdavidn			cnf->home[--l] = '\0';
14421052Sdavidn
14521052Sdavidn		if (l < 2 || *cnf->home != '/')		/* Check for absolute path name */
14621052Sdavidn			cmderr(EX_DATAERR, "invalid base directory for home '%s'\n", cnf->home);
14721052Sdavidn
14821052Sdavidn		if (stat(cnf->home, &st) == -1) {
14921052Sdavidn			char	dbuf[MAXPATHLEN];
15021052Sdavidn
15121052Sdavidn			p = strncpy(dbuf, cnf->home, sizeof dbuf);
15221052Sdavidn			dbuf[MAXPATHLEN-1] = '\0';
15321052Sdavidn			while ((p = strchr(++p, '/')) != NULL) {
15421052Sdavidn				*p = '\0';
15521052Sdavidn				if (stat(dbuf, &st) == -1) {
15621052Sdavidn					if (mkdir(dbuf, 0755) == -1)
15721052Sdavidn						goto direrr;
15821052Sdavidn					chown(dbuf, 0, 0);
15921052Sdavidn				} else if (!S_ISDIR(st.st_mode))
16021052Sdavidn					cmderr(EX_OSFILE, "'%s' (root home parent) is not a directory\n", dbuf);
16121052Sdavidn				*p = '/';
16221052Sdavidn			}
16321052Sdavidn			if (stat(dbuf, &st) == -1) {	/* Should not be strictly necessary */
16421052Sdavidn				if (mkdir(dbuf, 0755) == -1) {
16521052Sdavidn				direrr:	cmderr(EX_OSFILE, "mkdir '%s': %s\n", dbuf, strerror(errno));
16621052Sdavidn				}
16721052Sdavidn				chown(dbuf, 0, 0);
16821052Sdavidn			}
16921052Sdavidn		} else if (!S_ISDIR(st.st_mode))
17021052Sdavidn			cmderr(EX_OSFILE, "root home `%s' is not a directory\n", cnf->home);
17121052Sdavidn	}
17221052Sdavidn
17321052Sdavidn
17420253Sjoerg	if ((arg = getarg(args, 'e')) != NULL)
17520253Sjoerg		cnf->expire_days = atoi(arg->val);
17620253Sjoerg
17720253Sjoerg	if ((arg = getarg(args, 'p')) != NULL && arg->val)
17820253Sjoerg		cnf->password_days = atoi(arg->val);
17920253Sjoerg
18020253Sjoerg	if ((arg = getarg(args, 'g')) != NULL) {
18120253Sjoerg		p = arg->val;
18220253Sjoerg		if ((grp = getgrnam(p)) == NULL) {
18320253Sjoerg			if (!isdigit(*p) || (grp = getgrgid((gid_t) atoi(p))) == NULL)
18420267Sjoerg				cmderr(EX_NOUSER, "group `%s' does not exist\n", p);
18520253Sjoerg		}
18620253Sjoerg		cnf->default_group = newstr(grp->gr_name);
18720253Sjoerg	}
18820253Sjoerg	if ((arg = getarg(args, 'L')) != NULL)
18920679Sdavidn		cnf->default_class = pw_checkname((u_char *)arg->val, 0);
19020253Sjoerg
19120253Sjoerg	if ((arg = getarg(args, 'G')) != NULL && arg->val) {
19220253Sjoerg		int             i = 0;
19320253Sjoerg
19420747Sdavidn		for (p = strtok(arg->val, ", \t"); p != NULL; p = strtok(NULL, ", \t")) {
19520253Sjoerg			if ((grp = getgrnam(p)) == NULL) {
19620253Sjoerg				if (!isdigit(*p) || (grp = getgrgid((gid_t) atoi(p))) == NULL)
19720267Sjoerg					cmderr(EX_NOUSER, "group `%s' does not exist\n", p);
19820253Sjoerg			}
19920747Sdavidn			if (extendarray(&cnf->groups, &cnf->numgroups, i + 2) != -1)
20020747Sdavidn				cnf->groups[i++] = newstr(grp->gr_name);
20120253Sjoerg		}
20220747Sdavidn		while (i < cnf->numgroups)
20320253Sjoerg			cnf->groups[i++] = NULL;
20420253Sjoerg	}
20520253Sjoerg	if ((arg = getarg(args, 'k')) != NULL) {
20620253Sjoerg		if (stat(cnf->dotdir = arg->val, &st) == -1 || S_ISDIR(st.st_mode))
20720267Sjoerg			cmderr(EX_OSFILE, "skeleton `%s' is not a directory or does not exist\n", cnf->dotdir);
20820253Sjoerg	}
20920253Sjoerg	if ((arg = getarg(args, 's')) != NULL)
21020253Sjoerg		cnf->shell_default = arg->val;
21120253Sjoerg
21220253Sjoerg	if (mode == M_ADD && getarg(args, 'D')) {
21320253Sjoerg		if (getarg(args, 'n') != NULL)
21420267Sjoerg			cmderr(EX_DATAERR, "can't combine `-D' with `-n name'\n");
21520253Sjoerg		if ((arg = getarg(args, 'u')) != NULL && (p = strtok(arg->val, ", \t")) != NULL) {
21620253Sjoerg			if ((cnf->min_uid = (uid_t) atoi(p)) == 0)
21720253Sjoerg				cnf->min_uid = 1000;
21820253Sjoerg			if ((p = strtok(NULL, " ,\t")) == NULL || (cnf->max_uid = (uid_t) atoi(p)) < cnf->min_uid)
21920253Sjoerg				cnf->max_uid = 32000;
22020253Sjoerg		}
22120253Sjoerg		if ((arg = getarg(args, 'i')) != NULL && (p = strtok(arg->val, ", \t")) != NULL) {
22220253Sjoerg			if ((cnf->min_gid = (gid_t) atoi(p)) == 0)
22320253Sjoerg				cnf->min_gid = 1000;
22420253Sjoerg			if ((p = strtok(NULL, " ,\t")) == NULL || (cnf->max_gid = (gid_t) atoi(p)) < cnf->min_gid)
22520253Sjoerg				cnf->max_gid = 32000;
22620253Sjoerg		}
22720253Sjoerg		if ((arg = getarg(args, 'w')) != NULL)
22820253Sjoerg			cnf->default_password = boolean_val(arg->val, cnf->default_password);
22920253Sjoerg
23020253Sjoerg		arg = getarg(args, 'C');
23120253Sjoerg		if (write_userconfig(arg ? arg->val : NULL))
23220267Sjoerg			return EXIT_SUCCESS;
23320253Sjoerg		perror("config update");
23420267Sjoerg		return EX_IOERR;
23520253Sjoerg	}
23620253Sjoerg	if (mode == M_PRINT && getarg(args, 'a')) {
23720267Sjoerg		int             pretty = getarg(args, 'P') != NULL;
23820253Sjoerg
23920253Sjoerg		setpwent();
24020253Sjoerg		while ((pwd = getpwent()) != NULL)
24120253Sjoerg			print_user(pwd, pretty);
24220253Sjoerg		endpwent();
24320267Sjoerg		return EXIT_SUCCESS;
24420253Sjoerg	}
24520253Sjoerg	if ((a_name = getarg(args, 'n')) != NULL)
24620679Sdavidn		pwd = getpwnam(pw_checkname((u_char *)a_name->val, 0));
24720253Sjoerg	a_uid = getarg(args, 'u');
24820253Sjoerg
24920253Sjoerg	if (a_uid == NULL) {
25020253Sjoerg		if (a_name == NULL)
25120267Sjoerg			cmderr(EX_DATAERR, "user name or id required\n");
25220253Sjoerg
25320253Sjoerg		/*
25420253Sjoerg		 * Determine whether 'n' switch is name or uid - we don't
25520253Sjoerg		 * really don't really care which we have, but we need to
25620253Sjoerg		 * know.
25720253Sjoerg		 */
25820253Sjoerg		if (mode != M_ADD && pwd == NULL && isdigit(*a_name->val) && atoi(a_name->val) > 0) {	/* Assume uid */
25920253Sjoerg			(a_uid = a_name)->ch = 'u';
26020253Sjoerg			a_name = NULL;
26120253Sjoerg		}
26220253Sjoerg	}
26320253Sjoerg	/*
26420253Sjoerg	 * Update, delete & print require that the user exists
26520253Sjoerg	 */
26620253Sjoerg	if (mode == M_UPDATE || mode == M_DELETE || mode == M_PRINT) {
26720253Sjoerg		if (a_name == NULL && pwd == NULL)	/* Try harder */
26820253Sjoerg			pwd = getpwuid(atoi(a_uid->val));
26920253Sjoerg
27020253Sjoerg		if (pwd == NULL) {
27120253Sjoerg			if (mode == M_PRINT && getarg(args, 'F')) {
27220253Sjoerg				fakeuser.pw_name = a_name ? a_name->val : "nouser";
27320253Sjoerg				fakeuser.pw_uid = a_uid ? (uid_t) atol(a_uid->val) : -1;
27420267Sjoerg				return print_user(&fakeuser, getarg(args, 'P') != NULL);
27520253Sjoerg			}
27620253Sjoerg			if (a_name == NULL)
27720267Sjoerg				cmderr(EX_NOUSER, "no such uid `%s'\n", a_uid->val);
27820267Sjoerg			cmderr(EX_NOUSER, "no such user `%s'\n", a_name->val);
27920253Sjoerg		}
28020253Sjoerg		if (a_name == NULL)	/* May be needed later */
28120253Sjoerg			a_name = addarg(args, 'n', newstr(pwd->pw_name));
28220253Sjoerg
28320253Sjoerg		/*
28420253Sjoerg		 * Handle deletions now
28520253Sjoerg		 */
28620253Sjoerg		if (mode == M_DELETE) {
28720253Sjoerg			char            file[MAXPATHLEN];
28820253Sjoerg			char            home[MAXPATHLEN];
28920253Sjoerg			uid_t           uid = pwd->pw_uid;
29020253Sjoerg
29120253Sjoerg			if (strcmp(pwd->pw_name, "root") == 0)
29220267Sjoerg				cmderr(EX_DATAERR, "cannot remove user 'root'\n");
29320253Sjoerg
29420253Sjoerg			/*
29520747Sdavidn			 * Remove skey record from /etc/skeykeys
29620747Sdavidn			 */
29720747Sdavidn
29820747Sdavidn			rmskey(pwd->pw_name);
29920747Sdavidn
30020747Sdavidn			/*
30120253Sjoerg			 * Remove crontabs
30220253Sjoerg			 */
30320253Sjoerg			sprintf(file, "/var/cron/tabs/%s", pwd->pw_name);
30420253Sjoerg			if (access(file, F_OK) == 0) {
30520253Sjoerg				sprintf(file, "crontab -u %s -r", pwd->pw_name);
30620253Sjoerg				system(file);
30720253Sjoerg			}
30820253Sjoerg			/*
30920253Sjoerg			 * Save these for later, since contents of pwd may be
31020253Sjoerg			 * invalidated by deletion
31120253Sjoerg			 */
31220253Sjoerg			sprintf(file, "%s/%s", _PATH_MAILDIR, pwd->pw_name);
31320253Sjoerg			strncpy(home, pwd->pw_dir, sizeof home);
31420253Sjoerg			home[sizeof home - 1] = '\0';
31520253Sjoerg
31620253Sjoerg			if (!delpwent(pwd))
31720267Sjoerg				cmderr(EX_IOERR, "Error updating passwd file: %s\n", strerror(errno));
31820253Sjoerg			editgroups(a_name->val, NULL);
31920253Sjoerg
32020253Sjoerg			pw_log(cnf, mode, W_USER, "%s(%ld) account removed", a_name->val, (long) uid);
32120253Sjoerg
32220253Sjoerg			/*
32320253Sjoerg			 * Remove mail file
32420253Sjoerg			 */
32520253Sjoerg			remove(file);
32620253Sjoerg
32720253Sjoerg			/*
32820253Sjoerg			 * Remove at jobs
32920253Sjoerg			 */
33020253Sjoerg			if (getpwuid(uid) == NULL)
33120253Sjoerg				rmat(uid);
33220253Sjoerg
33320253Sjoerg			/*
33420253Sjoerg			 * Remove home directory and contents
33520253Sjoerg			 */
33620253Sjoerg			if (getarg(args, 'r') != NULL && *home == '/' && getpwuid(uid) == NULL) {
33720253Sjoerg				if (stat(home, &st) != -1) {
33820253Sjoerg					rm_r(home, uid);
33920253Sjoerg					pw_log(cnf, mode, W_USER, "%s(%ld) home '%s' %sremoved",
34020253Sjoerg					       a_name->val, (long) uid, home,
34120253Sjoerg					       stat(home, &st) == -1 ? "" : "not completely ");
34220253Sjoerg				}
34320253Sjoerg			}
34420267Sjoerg			return EXIT_SUCCESS;
34520253Sjoerg		} else if (mode == M_PRINT)
34620267Sjoerg			return print_user(pwd, getarg(args, 'P') != NULL);
34720253Sjoerg
34820253Sjoerg		/*
34920253Sjoerg		 * The rest is edit code
35020253Sjoerg		 */
35120253Sjoerg		if ((arg = getarg(args, 'l')) != NULL) {
35220253Sjoerg			if (strcmp(pwd->pw_name, "root") == 0)
35320267Sjoerg				cmderr(EX_DATAERR, "can't rename `root' account\n");
35420679Sdavidn			pwd->pw_name = pw_checkname((u_char *)arg->val, 0);
35520253Sjoerg		}
35620253Sjoerg		if ((arg = getarg(args, 'u')) != NULL && isdigit(*arg->val)) {
35720253Sjoerg			pwd->pw_uid = (uid_t) atol(arg->val);
35820253Sjoerg			if (pwd->pw_uid != 0 && strcmp(pwd->pw_name, "root") == 0)
35920267Sjoerg				cmderr(EX_DATAERR, "can't change uid of `root' account\n");
36020253Sjoerg			if (pwd->pw_uid == 0 && strcmp(pwd->pw_name, "root") != 0)
36120253Sjoerg				fprintf(stderr, "WARNING: account `%s' will have a uid of 0 (superuser access!)\n", pwd->pw_name);
36220253Sjoerg		}
36320253Sjoerg		if ((arg = getarg(args, 'g')) != NULL && pwd->pw_uid != 0)	/* Already checked this */
36420253Sjoerg			pwd->pw_gid = (gid_t) getgrnam(cnf->default_group)->gr_gid;
36520253Sjoerg
36620253Sjoerg		if ((arg = getarg(args, 'p')) != NULL) {
36720253Sjoerg			if (*arg->val == '\0' || strcmp(arg->val, "0") == 0)
36820253Sjoerg				pwd->pw_change = 0;
36920253Sjoerg			else {
37020253Sjoerg				time_t          now = time(NULL);
37120253Sjoerg				time_t          expire = parse_date(now, arg->val);
37220253Sjoerg
37320253Sjoerg				if (now == expire)
37420267Sjoerg					cmderr(EX_DATAERR, "Invalid password change date `%s'\n", arg->val);
37520253Sjoerg				pwd->pw_change = expire;
37620253Sjoerg			}
37720253Sjoerg		}
37820267Sjoerg		if ((arg = getarg(args, 'e')) != NULL) {
37920253Sjoerg			if (*arg->val == '\0' || strcmp(arg->val, "0") == 0)
38020253Sjoerg				pwd->pw_expire = 0;
38120253Sjoerg			else {
38220253Sjoerg				time_t          now = time(NULL);
38320253Sjoerg				time_t          expire = parse_date(now, arg->val);
38420253Sjoerg
38520253Sjoerg				if (now == expire)
38620267Sjoerg					cmderr(EX_DATAERR, "Invalid account expiry date `%s'\n", arg->val);
38720253Sjoerg				pwd->pw_expire = expire;
38820253Sjoerg			}
38920253Sjoerg		}
39020253Sjoerg		if ((arg = getarg(args, 's')) != NULL)
39120253Sjoerg			pwd->pw_shell = shell_path(cnf->shelldir, cnf->shells, arg->val);
39220253Sjoerg
39320253Sjoerg		if (getarg(args, 'L'))
39420253Sjoerg			pwd->pw_class = cnf->default_class;
39520253Sjoerg
39620747Sdavidn		if ((arg  = getarg(args, 'd')) != NULL) {
39720747Sdavidn			if (stat(pwd->pw_dir = arg->val, &st) == -1) {
39820747Sdavidn				if (getarg(args, 'm') == NULL && strcmp(pwd->pw_dir, "/nonexistent") != 0)
39920747Sdavidn				  fprintf(stderr, "WARNING: home `%s' does not exist\n", pwd->pw_dir);
40020747Sdavidn			} else if (!S_ISDIR(st.st_mode))
40120747Sdavidn				fprintf(stderr, "WARNING: home `%s' is not a directory\n", pwd->pw_dir);
40220747Sdavidn		}
40320747Sdavidn
40420267Sjoerg		if ((arg = getarg(args, 'w')) != NULL && getarg(args, 'h') == NULL)
40520267Sjoerg			pwd->pw_passwd = pw_password(cnf, args, pwd->pw_name);
40620267Sjoerg
40720253Sjoerg	} else {
40820253Sjoerg		if (a_name == NULL)	/* Required */
40920267Sjoerg			cmderr(EX_DATAERR, "login name required\n");
41020253Sjoerg		else if ((pwd = getpwnam(a_name->val)) != NULL)	/* Exists */
41120267Sjoerg			cmderr(EX_DATAERR, "login name `%s' already exists\n", a_name->val);
41220253Sjoerg
41320253Sjoerg		/*
41420253Sjoerg		 * Now, set up defaults for a new user
41520253Sjoerg		 */
41620253Sjoerg		pwd = &fakeuser;
41720253Sjoerg		pwd->pw_name = a_name->val;
41820253Sjoerg		pwd->pw_class = cnf->default_class ? cnf->default_class : "";
41920253Sjoerg		pwd->pw_passwd = pw_password(cnf, args, pwd->pw_name);
42020253Sjoerg		pwd->pw_uid = pw_uidpolicy(cnf, args);
42120253Sjoerg		pwd->pw_gid = pw_gidpolicy(cnf, args, pwd->pw_name, (gid_t) pwd->pw_uid);
42220253Sjoerg		pwd->pw_change = pw_pwdpolicy(cnf, args);
42320253Sjoerg		pwd->pw_expire = pw_exppolicy(cnf, args);
42420253Sjoerg		pwd->pw_dir = pw_homepolicy(cnf, args, pwd->pw_name);
42520253Sjoerg		pwd->pw_shell = pw_shellpolicy(cnf, args, NULL);
42620253Sjoerg
42720253Sjoerg		if (pwd->pw_uid == 0 && strcmp(pwd->pw_name, "root") != 0)
42820253Sjoerg			fprintf(stderr, "WARNING: new account `%s' has a uid of 0 (superuser access!)\n", pwd->pw_name);
42920253Sjoerg	}
43020253Sjoerg
43120253Sjoerg	/*
43220253Sjoerg	 * Shared add/edit code
43320253Sjoerg	 */
43420253Sjoerg	if ((arg = getarg(args, 'c')) != NULL)
43520679Sdavidn		pwd->pw_gecos = pw_checkname((u_char *)arg->val, 1);
43620253Sjoerg
43720253Sjoerg	if ((arg = getarg(args, 'h')) != NULL) {
43820253Sjoerg		if (strcmp(arg->val, "-") == 0)
43920253Sjoerg			pwd->pw_passwd = "*";	/* No access */
44020253Sjoerg		else {
44120253Sjoerg			int             fd = atoi(arg->val);
44220253Sjoerg			int             b;
44320253Sjoerg			int             istty = isatty(fd);
44420253Sjoerg			struct termios  t;
44520253Sjoerg
44620253Sjoerg			if (istty) {
44720253Sjoerg				if (tcgetattr(fd, &t) == -1)
44820253Sjoerg					istty = 0;
44920253Sjoerg				else {
45020253Sjoerg					struct termios  n = t;
45120253Sjoerg
45220253Sjoerg					/* Disable echo */
45320253Sjoerg					n.c_lflag &= ~(ECHO);
45420253Sjoerg					tcsetattr(fd, TCSANOW, &n);
45520253Sjoerg					printf("%sassword for user %s:", (mode == M_UPDATE) ? "New p" : "P", pwd->pw_name);
45620253Sjoerg					fflush(stdout);
45720253Sjoerg				}
45820253Sjoerg			}
45920253Sjoerg			b = read(fd, line, sizeof(line) - 1);
46020253Sjoerg			if (istty) {	/* Restore state */
46120253Sjoerg				tcsetattr(fd, TCSANOW, &t);
46220253Sjoerg				fputc('\n', stdout);
46320253Sjoerg				fflush(stdout);
46420253Sjoerg			}
46520253Sjoerg			if (b < 0) {
46620253Sjoerg				perror("-h file descriptor");
46720267Sjoerg				return EX_IOERR;
46820253Sjoerg			}
46920253Sjoerg			line[b] = '\0';
47020253Sjoerg			if ((p = strpbrk(line, " \t\r\n")) != NULL)
47120253Sjoerg				*p = '\0';
47220253Sjoerg			if (!*line)
47320267Sjoerg				cmderr(EX_DATAERR, "empty password read on file descriptor %d\n", fd);
47420253Sjoerg			pwd->pw_passwd = pw_pwcrypt(line);
47520253Sjoerg		}
47620253Sjoerg	}
47720267Sjoerg
47820267Sjoerg	/*
47920267Sjoerg	 * Special case: -N only displays & exits
48020267Sjoerg	 */
48120267Sjoerg	if (getarg(args, 'N') != NULL)
48220267Sjoerg		return print_user(pwd, getarg(args, 'P') != NULL);
48320267Sjoerg
48420253Sjoerg	if ((mode == M_ADD && !addpwent(pwd)) ||
48520253Sjoerg	    (mode == M_UPDATE && !chgpwent(a_name->val, pwd))) {
48620253Sjoerg		perror("password update");
48720267Sjoerg		return EX_IOERR;
48820253Sjoerg	}
48920253Sjoerg	/*
49020253Sjoerg	 * Ok, user is created or changed - now edit group file
49120253Sjoerg	 */
49220253Sjoerg
49320253Sjoerg	if (mode == M_ADD || getarg(args, 'G') != NULL)
49420253Sjoerg		editgroups(pwd->pw_name, cnf->groups);
49520253Sjoerg
49620253Sjoerg	/* pwd may have been invalidated */
49720253Sjoerg	if ((pwd = getpwnam(a_name->val)) == NULL)
49820267Sjoerg		cmderr(EX_NOUSER, "user '%s' disappeared during update\n", a_name->val);
49920253Sjoerg
50020253Sjoerg	grp = getgrgid(pwd->pw_gid);
50120253Sjoerg	pw_log(cnf, mode, W_USER, "%s(%ld):%s(%d):%s:%s:%s",
50220253Sjoerg	       pwd->pw_name, (long) pwd->pw_uid,
50320253Sjoerg	    grp ? grp->gr_name : "unknown", (long) (grp ? grp->gr_gid : -1),
50420253Sjoerg	       pwd->pw_gecos, pwd->pw_dir, pwd->pw_shell);
50520253Sjoerg
50620253Sjoerg	/*
50720253Sjoerg	 * If adding, let's touch and chown the user's mail file. This is not
50820253Sjoerg	 * strictly necessary under BSD with a 0755 maildir but it also
50920253Sjoerg	 * doesn't hurt anything to create the empty mailfile
51020253Sjoerg	 */
51120253Sjoerg	if (mode == M_ADD) {
51220253Sjoerg		FILE           *fp;
51320253Sjoerg
51420253Sjoerg		sprintf(line, "%s/%s", _PATH_MAILDIR, pwd->pw_name);
51520253Sjoerg		close(open(line, O_RDWR | O_CREAT, 0600));	/* Preserve contents &
51620253Sjoerg								 * mtime */
51720253Sjoerg		chown(line, pwd->pw_uid, pwd->pw_gid);
51820253Sjoerg
51920253Sjoerg		/*
52020253Sjoerg		 * Send mail to the new user as well, if we are asked to
52120253Sjoerg		 */
52220253Sjoerg		if (cnf->newmail && *cnf->newmail && (fp = fopen(cnf->newmail, "r")) != NULL) {
52320253Sjoerg			FILE           *pfp = popen(_PATH_SENDMAIL " -t", "w");
52420253Sjoerg
52520253Sjoerg			if (pfp == NULL)
52620253Sjoerg				perror("sendmail");
52720253Sjoerg			else {
52820253Sjoerg				fprintf(pfp, "From: root\n" "To: %s\n" "Subject: Welcome!\n\n", pwd->pw_name);
52920253Sjoerg				while (fgets(line, sizeof(line), fp) != NULL) {
53020253Sjoerg					/* Do substitutions? */
53120253Sjoerg					fputs(line, pfp);
53220253Sjoerg				}
53320253Sjoerg				pclose(pfp);
53420253Sjoerg				pw_log(cnf, mode, W_USER, "%s(%ld) new user mail sent",
53520253Sjoerg				       pwd->pw_name, (long) pwd->pw_uid);
53620253Sjoerg			}
53720253Sjoerg			fclose(fp);
53820253Sjoerg		}
53920253Sjoerg	}
54020253Sjoerg	/*
54120253Sjoerg	 * Finally, let's create and populate the user's home directory. Note
54220253Sjoerg	 * that this also `works' for editing users if -m is used, but
54320253Sjoerg	 * existing files will *not* be overwritten.
54420253Sjoerg	 */
54520253Sjoerg	if (getarg(args, 'm') != NULL && pwd->pw_dir && *pwd->pw_dir == '/' && pwd->pw_dir[1]) {
54620253Sjoerg		copymkdir(pwd->pw_dir, cnf->dotdir, 0755, pwd->pw_uid, pwd->pw_gid);
54720253Sjoerg		pw_log(cnf, mode, W_USER, "%s(%ld) home %s made",
54820253Sjoerg		       pwd->pw_name, (long) pwd->pw_uid, pwd->pw_dir);
54920253Sjoerg	}
55020267Sjoerg	return EXIT_SUCCESS;
55120253Sjoerg}
55220253Sjoerg
55320253Sjoerg
55420253Sjoergstatic          uid_t
55520253Sjoergpw_uidpolicy(struct userconf * cnf, struct cargs * args)
55620253Sjoerg{
55720253Sjoerg	struct passwd  *pwd;
55820253Sjoerg	uid_t           uid = (uid_t) - 1;
55920253Sjoerg	struct carg    *a_uid = getarg(args, 'u');
56020253Sjoerg
56120253Sjoerg	/*
56220253Sjoerg	 * Check the given uid, if any
56320253Sjoerg	 */
56420253Sjoerg	if (a_uid != NULL) {
56520253Sjoerg		uid = (uid_t) atol(a_uid->val);
56620253Sjoerg
56720253Sjoerg		if ((pwd = getpwuid(uid)) != NULL && getarg(args, 'o') == NULL)
56820267Sjoerg			cmderr(EX_DATAERR, "uid `%ld' has already been allocated\n", (long) pwd->pw_uid);
56920253Sjoerg	} else {
57020253Sjoerg		struct bitmap   bm;
57120253Sjoerg
57220253Sjoerg		/*
57320253Sjoerg		 * We need to allocate the next available uid under one of
57420253Sjoerg		 * two policies a) Grab the first unused uid b) Grab the
57520253Sjoerg		 * highest possible unused uid
57620253Sjoerg		 */
57720253Sjoerg		if (cnf->min_uid >= cnf->max_uid) {	/* Sanity
57820253Sjoerg							 * claus^H^H^H^Hheck */
57920253Sjoerg			cnf->min_uid = 1000;
58020253Sjoerg			cnf->max_uid = 32000;
58120253Sjoerg		}
58220253Sjoerg		bm = bm_alloc(cnf->max_uid - cnf->min_uid + 1);
58320253Sjoerg
58420253Sjoerg		/*
58520253Sjoerg		 * Now, let's fill the bitmap from the password file
58620253Sjoerg		 */
58720253Sjoerg		setpwent();
58820253Sjoerg		while ((pwd = getpwent()) != NULL)
58920253Sjoerg			if (pwd->pw_uid >= (int) cnf->min_uid && pwd->pw_uid <= (int) cnf->max_uid)
59020253Sjoerg				bm_setbit(&bm, pwd->pw_uid - cnf->min_uid);
59120253Sjoerg		endpwent();
59220253Sjoerg
59320253Sjoerg		/*
59420253Sjoerg		 * Then apply the policy, with fallback to reuse if necessary
59520253Sjoerg		 */
59620253Sjoerg		if (cnf->reuse_uids || (uid = (uid_t) (bm_lastset(&bm) + cnf->min_uid + 1)) > cnf->max_uid)
59720253Sjoerg			uid = (uid_t) (bm_firstunset(&bm) + cnf->min_uid);
59820253Sjoerg
59920253Sjoerg		/*
60020253Sjoerg		 * Another sanity check
60120253Sjoerg		 */
60220253Sjoerg		if (uid < cnf->min_uid || uid > cnf->max_uid)
60320267Sjoerg			cmderr(EX_SOFTWARE, "unable to allocate a new uid - range fully used\n");
60420253Sjoerg		bm_dealloc(&bm);
60520253Sjoerg	}
60620253Sjoerg	return uid;
60720253Sjoerg}
60820253Sjoerg
60920253Sjoerg
61020253Sjoergstatic          uid_t
61120253Sjoergpw_gidpolicy(struct userconf * cnf, struct cargs * args, char *nam, gid_t prefer)
61220253Sjoerg{
61320253Sjoerg	struct group   *grp;
61420253Sjoerg	gid_t           gid = (uid_t) - 1;
61520253Sjoerg	struct carg    *a_gid = getarg(args, 'g');
61620253Sjoerg
61720253Sjoerg	/*
61820253Sjoerg	 * If no arg given, see if default can help out
61920253Sjoerg	 */
62020253Sjoerg	if (a_gid == NULL && cnf->default_group && *cnf->default_group)
62120253Sjoerg		a_gid = addarg(args, 'g', cnf->default_group);
62220253Sjoerg
62320253Sjoerg	/*
62420253Sjoerg	 * Check the given gid, if any
62520253Sjoerg	 */
62620267Sjoerg	setgrent();
62720253Sjoerg	if (a_gid != NULL) {
62820253Sjoerg		if ((grp = getgrnam(a_gid->val)) == NULL) {
62920253Sjoerg			gid = (gid_t) atol(a_gid->val);
63020253Sjoerg			if ((gid == 0 && !isdigit(*a_gid->val)) || (grp = getgrgid(gid)) == NULL)
63120267Sjoerg				cmderr(EX_NOUSER, "group `%s' is not defined\n", a_gid->val);
63220253Sjoerg		}
63320253Sjoerg		gid = grp->gr_gid;
63420267Sjoerg	} else if ((grp = getgrnam(nam)) != NULL && grp->gr_mem[0] == NULL) {
63520267Sjoerg		gid = grp->gr_gid;  /* Already created? Use it anyway... */
63620253Sjoerg	} else {
63720253Sjoerg		struct cargs    grpargs;
63820253Sjoerg		char            tmp[32];
63920253Sjoerg
64020253Sjoerg		LIST_INIT(&grpargs);
64120253Sjoerg		addarg(&grpargs, 'n', nam);
64220253Sjoerg
64320253Sjoerg		/*
64420253Sjoerg		 * We need to auto-create a group with the user's name. We
64520253Sjoerg		 * can send all the appropriate output to our sister routine
64620253Sjoerg		 * bit first see if we can create a group with gid==uid so we
64720253Sjoerg		 * can keep the user and group ids in sync. We purposely do
64820253Sjoerg		 * NOT check the gid range if we can force the sync. If the
64920253Sjoerg		 * user's name dups an existing group, then the group add
65020253Sjoerg		 * function will happily handle that case for us and exit.
65120253Sjoerg		 */
65220253Sjoerg		if (getgrgid(prefer) == NULL) {
65320253Sjoerg			sprintf(tmp, "%lu", (unsigned long) prefer);
65420253Sjoerg			addarg(&grpargs, 'g', tmp);
65520253Sjoerg		}
65620267Sjoerg		if (getarg(args, 'N'))
65720267Sjoerg		{
65820267Sjoerg			addarg(&grpargs, 'N', NULL);
65920267Sjoerg			addarg(&grpargs, 'q', NULL);
66020267Sjoerg			gid = pw_group(cnf, M_NEXT, &grpargs);
66120267Sjoerg		}
66220267Sjoerg		else
66320267Sjoerg		{
66420267Sjoerg			pw_group(cnf, M_ADD, &grpargs);
66520267Sjoerg			if ((grp = getgrnam(nam)) != NULL)
66620267Sjoerg				gid = grp->gr_gid;
66720267Sjoerg		}
66820253Sjoerg		a_gid = grpargs.lh_first;
66920253Sjoerg		while (a_gid != NULL) {
67020253Sjoerg			struct carg    *t = a_gid->list.le_next;
67120253Sjoerg			LIST_REMOVE(a_gid, list);
67220253Sjoerg			a_gid = t;
67320253Sjoerg		}
67420253Sjoerg	}
67520267Sjoerg	endgrent();
67620253Sjoerg	return gid;
67720253Sjoerg}
67820253Sjoerg
67920253Sjoerg
68020253Sjoergstatic          time_t
68120253Sjoergpw_pwdpolicy(struct userconf * cnf, struct cargs * args)
68220253Sjoerg{
68320253Sjoerg	time_t          result = 0;
68420253Sjoerg	time_t          now = time(NULL);
68520253Sjoerg	struct carg    *arg = getarg(args, 'e');
68620253Sjoerg
68720253Sjoerg	if (arg != NULL) {
68820253Sjoerg		if ((result = parse_date(now, arg->val)) == now)
68920267Sjoerg			cmderr(EX_DATAERR, "invalid date/time `%s'\n", arg->val);
69020253Sjoerg	} else if (cnf->password_days > 0)
69120253Sjoerg		result = now + ((long) cnf->password_days * 86400L);
69220253Sjoerg	return result;
69320253Sjoerg}
69420253Sjoerg
69520253Sjoerg
69620253Sjoergstatic          time_t
69720253Sjoergpw_exppolicy(struct userconf * cnf, struct cargs * args)
69820253Sjoerg{
69920253Sjoerg	time_t          result = 0;
70020253Sjoerg	time_t          now = time(NULL);
70120253Sjoerg	struct carg    *arg = getarg(args, 'e');
70220253Sjoerg
70320253Sjoerg	if (arg != NULL) {
70420253Sjoerg		if ((result = parse_date(now, arg->val)) == now)
70520267Sjoerg			cmderr(EX_DATAERR, "invalid date/time `%s'\n", arg->val);
70620253Sjoerg	} else if (cnf->expire_days > 0)
70720253Sjoerg		result = now + ((long) cnf->expire_days * 86400L);
70820253Sjoerg	return result;
70920253Sjoerg}
71020253Sjoerg
71120253Sjoerg
71220253Sjoergstatic char    *
71320253Sjoergpw_homepolicy(struct userconf * cnf, struct cargs * args, char const * user)
71420253Sjoerg{
71520253Sjoerg	struct carg    *arg = getarg(args, 'd');
71620253Sjoerg
71720253Sjoerg	if (arg)
71820253Sjoerg		return arg->val;
71920253Sjoerg	else {
72020253Sjoerg		static char     home[128];
72120253Sjoerg
72220253Sjoerg		if (cnf->home == NULL || *cnf->home == '\0')
72320267Sjoerg			cmderr(EX_CONFIG, "no base home directory set\n");
72420253Sjoerg		sprintf(home, "%s/%s", cnf->home, user);
72520253Sjoerg		return home;
72620253Sjoerg	}
72720253Sjoerg}
72820253Sjoerg
72920253Sjoergstatic char    *
73020253Sjoergshell_path(char const * path, char *shells[], char *sh)
73120253Sjoerg{
73220253Sjoerg	if (sh != NULL && (*sh == '/' || *sh == '\0'))
73320253Sjoerg		return sh;	/* specified full path or forced none */
73420253Sjoerg	else {
73520253Sjoerg		char           *p;
73620253Sjoerg		char            paths[_UC_MAXLINE];
73720253Sjoerg
73820253Sjoerg		/*
73920253Sjoerg		 * We need to search paths
74020253Sjoerg		 */
74120253Sjoerg		strncpy(paths, path, sizeof paths);
74220253Sjoerg		paths[sizeof paths - 1] = '\0';
74320253Sjoerg		for (p = strtok(paths, ": \t\r\n"); p != NULL; p = strtok(NULL, ": \t\r\n")) {
74420253Sjoerg			int             i;
74520253Sjoerg			static char     shellpath[256];
74620253Sjoerg
74720253Sjoerg			if (sh != NULL) {
74820253Sjoerg				sprintf(shellpath, "%s/%s", p, sh);
74920253Sjoerg				if (access(shellpath, X_OK) == 0)
75020253Sjoerg					return shellpath;
75120253Sjoerg			} else
75220253Sjoerg				for (i = 0; i < _UC_MAXSHELLS && shells[i] != NULL; i++) {
75320253Sjoerg					sprintf(shellpath, "%s/%s", p, shells[i]);
75420253Sjoerg					if (access(shellpath, X_OK) == 0)
75520253Sjoerg						return shellpath;
75620253Sjoerg				}
75720253Sjoerg		}
75820253Sjoerg		if (sh == NULL)
75920267Sjoerg			cmderr(EX_OSFILE, "can't find shell `%s' in shell paths\n", sh);
76020267Sjoerg		cmderr(EX_CONFIG, "no default shell available or defined\n");
76120253Sjoerg		return NULL;
76220253Sjoerg	}
76320253Sjoerg}
76420253Sjoerg
76520253Sjoerg
76620253Sjoergstatic char    *
76720253Sjoergpw_shellpolicy(struct userconf * cnf, struct cargs * args, char *newshell)
76820253Sjoerg{
76920253Sjoerg	char           *sh = newshell;
77020253Sjoerg	struct carg    *arg = getarg(args, 's');
77120253Sjoerg
77220253Sjoerg	if (newshell == NULL && arg != NULL)
77320253Sjoerg		sh = arg->val;
77420253Sjoerg	return shell_path(cnf->shelldir, cnf->shells, sh ? sh : cnf->shell_default);
77520253Sjoerg}
77620253Sjoerg
77720253Sjoergstatic char const chars[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.";
77820253Sjoerg
77920253Sjoergchar           *
78020253Sjoergpw_pwcrypt(char *password)
78120253Sjoerg{
78220253Sjoerg	int             i;
78320253Sjoerg	char            salt[12];
78420253Sjoerg
78520253Sjoerg	static char     buf[256];
78620253Sjoerg
78720253Sjoerg	/*
78820253Sjoerg	 * Calculate a salt value
78920253Sjoerg	 */
79020555Sdavidn	srandom((unsigned) (time(NULL) ^ getpid()));
79120253Sjoerg	for (i = 0; i < 8; i++)
79220253Sjoerg		salt[i] = chars[random() % 63];
79320253Sjoerg	salt[i] = '\0';
79420253Sjoerg
79520253Sjoerg	return strcpy(buf, crypt(password, salt));
79620253Sjoerg}
79720253Sjoerg
79820590Sdavidn#if defined(__FreeBSD__)
79920590Sdavidn
80020590Sdavidn#if defined(USE_MD5RAND)
80120555Sdavidnu_char *
80220590Sdavidnpw_getrand(u_char *buf, int len)	/* cryptographically secure rng */
80320555Sdavidn{
80420590Sdavidn	int i;
80520590Sdavidn	for (i=0;i<len;i+=16) {
80620590Sdavidn		u_char ubuf[16];
80720590Sdavidn
80820590Sdavidn		MD5_CTX md5_ctx;
80920590Sdavidn		struct timeval tv, tvo;
81020590Sdavidn		struct rusage ru;
81120590Sdavidn		int n=0;
81220590Sdavidn		int t;
81320590Sdavidn
81420590Sdavidn		MD5Init (&md5_ctx);
81520590Sdavidn		t=getpid();
81620590Sdavidn		MD5Update (&md5_ctx, (u_char*)&t, sizeof t);
81720590Sdavidn		t=getppid();
81820590Sdavidn		MD5Update (&md5_ctx, (u_char*)&t, sizeof t);
81920590Sdavidn		gettimeofday (&tvo, NULL);
82020590Sdavidn		do {
82120590Sdavidn			getrusage (RUSAGE_SELF, &ru);
82220590Sdavidn			MD5Update (&md5_ctx, (u_char*)&ru, sizeof ru);
82320590Sdavidn			gettimeofday (&tv, NULL);
82420590Sdavidn			MD5Update (&md5_ctx, (u_char*)&tv, sizeof tv);
82520590Sdavidn		} while (n++<20 || tv.tv_usec-tvo.tv_usec<100*1000);
82620590Sdavidn		MD5Final (ubuf, &md5_ctx);
82720590Sdavidn		memcpy(buf+i, ubuf, MIN(16, len-n));
82820590Sdavidn	}
82920590Sdavidn	return buf;
83020555Sdavidn}
83120253Sjoerg
83220590Sdavidn#else	/* Use random device (preferred) */
83320590Sdavidn
83420555Sdavidnstatic u_char *
83520555Sdavidnpw_getrand(u_char *buf, int len)
83620555Sdavidn{
83720555Sdavidn	int		fd;
83820555Sdavidn	fd = open("/dev/urandom", O_RDONLY);
83920590Sdavidn	if (fd==-1)
84020590Sdavidn		cmderr(EX_OSFILE, "can't open /dev/urandom: %s\n", strerror(errno));
84120590Sdavidn	else if (read(fd, buf, len)!=len)
84220590Sdavidn		cmderr(EX_IOERR, "read error on /dev/urandom\n");
84320555Sdavidn	close(fd);
84420555Sdavidn	return buf;
84520555Sdavidn}
84620555Sdavidn
84720590Sdavidn#endif
84820590Sdavidn
84920590Sdavidn#else	/* Portable version */
85020590Sdavidn
85120590Sdavidnstatic u_char *
85220590Sdavidnpw_getrand(u_char *buf, int len)
85320590Sdavidn{
85420590Sdavidn	int i;
85520590Sdavidn
85620590Sdavidn	for (i = 0; i < len; i++) {
85720590Sdavidn		unsigned val = random();
85820590Sdavidn		/* Use all bits in the random value */
85920590Sdavidn		buf[i]=(u_char)((val >> 24) ^ (val >> 16) ^ (val >> 8) ^ val);
86020590Sdavidn	}
86120590Sdavidn	return buf;
86220590Sdavidn}
86320590Sdavidn
86420590Sdavidn#endif
86520590Sdavidn
86620253Sjoergstatic char    *
86720253Sjoergpw_password(struct userconf * cnf, struct cargs * args, char const * user)
86820253Sjoerg{
86920253Sjoerg	int             i, l;
87020253Sjoerg	char            pwbuf[32];
87120555Sdavidn	u_char		rndbuf[sizeof pwbuf];
87220253Sjoerg
87320253Sjoerg	switch (cnf->default_password) {
87420253Sjoerg	case -1:		/* Random password */
87520555Sdavidn		srandom((unsigned) (time(NULL) ^ getpid()));
87620253Sjoerg		l = (random() % 8 + 8);	/* 8 - 16 chars */
87720555Sdavidn		pw_getrand(rndbuf, l);
87820253Sjoerg		for (i = 0; i < l; i++)
87920555Sdavidn			pwbuf[i] = chars[rndbuf[i] % sizeof(chars)];
88020253Sjoerg		pwbuf[i] = '\0';
88120253Sjoerg
88220253Sjoerg		/*
88320253Sjoerg		 * We give this information back to the user
88420253Sjoerg		 */
88520267Sjoerg		if (getarg(args, 'h') == NULL && getarg(args, 'N') == NULL) {
88620712Sdavidn			if (isatty(1))
88720712Sdavidn				printf("Password for '%s' is: ", user);
88820253Sjoerg			printf("%s\n", pwbuf);
88920253Sjoerg			fflush(stdout);
89020253Sjoerg		}
89120253Sjoerg		break;
89220253Sjoerg
89320253Sjoerg	case -2:		/* No password at all! */
89420253Sjoerg		return "";
89520253Sjoerg
89620253Sjoerg	case 0:		/* No login - default */
89720253Sjoerg	default:
89820253Sjoerg		return "*";
89920253Sjoerg
90020253Sjoerg	case 1:		/* user's name */
90120253Sjoerg		strncpy(pwbuf, user, sizeof pwbuf);
90220253Sjoerg		pwbuf[sizeof pwbuf - 1] = '\0';
90320253Sjoerg		break;
90420253Sjoerg	}
90520253Sjoerg	return pw_pwcrypt(pwbuf);
90620253Sjoerg}
90720253Sjoerg
90820253Sjoerg
90920253Sjoergstatic int
91020253Sjoergprint_user(struct passwd * pwd, int pretty)
91120253Sjoerg{
91220253Sjoerg	if (!pretty) {
91320253Sjoerg		char            buf[_UC_MAXLINE];
91420253Sjoerg
91520253Sjoerg		fmtpwent(buf, pwd);
91620253Sjoerg		fputs(buf, stdout);
91720253Sjoerg	} else {
91820267Sjoerg		int		j;
91920253Sjoerg		char           *p;
92020253Sjoerg		struct group   *grp = getgrgid(pwd->pw_gid);
92120253Sjoerg		char            uname[60] = "User &", office[60] = "[None]",
92220253Sjoerg		                wphone[60] = "[None]", hphone[60] = "[None]";
92320590Sdavidn		char		acexpire[32] = "[None]", pwexpire[32] = "[None]";
92420590Sdavidn		struct tm *    tptr;
92520253Sjoerg
92620253Sjoerg		if ((p = strtok(pwd->pw_gecos, ",")) != NULL) {
92720253Sjoerg			strncpy(uname, p, sizeof uname);
92820253Sjoerg			uname[sizeof uname - 1] = '\0';
92920253Sjoerg			if ((p = strtok(NULL, ",")) != NULL) {
93020253Sjoerg				strncpy(office, p, sizeof office);
93120253Sjoerg				office[sizeof office - 1] = '\0';
93220253Sjoerg				if ((p = strtok(NULL, ",")) != NULL) {
93320253Sjoerg					strncpy(wphone, p, sizeof wphone);
93420253Sjoerg					wphone[sizeof wphone - 1] = '\0';
93520253Sjoerg					if ((p = strtok(NULL, "")) != NULL) {
93620253Sjoerg						strncpy(hphone, p, sizeof hphone);
93720253Sjoerg						hphone[sizeof hphone - 1] = '\0';
93820253Sjoerg					}
93920253Sjoerg				}
94020253Sjoerg			}
94120253Sjoerg		}
94220253Sjoerg		/*
94320253Sjoerg		 * Handle '&' in gecos field
94420253Sjoerg		 */
94520253Sjoerg		if ((p = strchr(uname, '&')) != NULL) {
94620253Sjoerg			int             l = strlen(pwd->pw_name);
94720253Sjoerg			int             m = strlen(p);
94820253Sjoerg
94920253Sjoerg			memmove(p + l, p + 1, m);
95020253Sjoerg			memmove(p, pwd->pw_name, l);
95120253Sjoerg			*p = (char) toupper(*p);
95220253Sjoerg		}
95320590Sdavidn		if (pwd->pw_expire > (time_t)0 && (tptr = localtime(&pwd->pw_expire)) != NULL)
95420590Sdavidn		  strftime(acexpire, sizeof acexpire, "%c", tptr);
95520590Sdavidn		if (pwd->pw_change > (time_t)9 && (tptr = localtime(&pwd->pw_change)) != NULL)
95620590Sdavidn		  strftime(pwexpire, sizeof pwexpire, "%c", tptr);
95720747Sdavidn		printf("Login Name: %-10s   #%-16ld  Group: %-10s   #%ld\n"
95820747Sdavidn		       " Full Name: %s\n"
95920747Sdavidn		       "      Home: %-26.26s      Class: %s\n"
96020747Sdavidn		       "     Shell: %-26.26s     Office: %s\n"
96120747Sdavidn		       "Work Phone: %-26.26s Home Phone: %s\n"
96220747Sdavidn		       "Acc Expire: %-26.26s Pwd Expire: %s\n",
96320253Sjoerg		       pwd->pw_name, (long) pwd->pw_uid,
96420253Sjoerg		       grp ? grp->gr_name : "(invalid)", (long) pwd->pw_gid,
96520253Sjoerg		       uname, pwd->pw_dir, pwd->pw_class,
96620590Sdavidn		       pwd->pw_shell, office, wphone, hphone,
96720590Sdavidn		       acexpire, pwexpire);
96820267Sjoerg	        setgrent();
96920267Sjoerg		j = 0;
97020267Sjoerg		while ((grp=getgrent()) != NULL)
97120267Sjoerg		{
97220267Sjoerg			int     i = 0;
97320747Sdavidn			while (grp->gr_mem[i] != NULL)
97420267Sjoerg			{
97520267Sjoerg				if (strcmp(grp->gr_mem[i], pwd->pw_name)==0)
97620267Sjoerg				{
97720747Sdavidn					printf(j++ == 0 ? "    Groups: %s" : ",%s", grp->gr_name);
97820267Sjoerg					break;
97920267Sjoerg				}
98020267Sjoerg				++i;
98120267Sjoerg			}
98220267Sjoerg		}
98320267Sjoerg		endgrent();
98420267Sjoerg		printf("%s\n", j ? "\n" : "");
98520253Sjoerg	}
98620267Sjoerg	return EXIT_SUCCESS;
98720253Sjoerg}
98820253Sjoerg
98920679Sdavidnchar    *
99020679Sdavidnpw_checkname(u_char *name, int gecos)
99120253Sjoerg{
99220253Sjoerg	int             l = 0;
99321052Sdavidn	char const     *notch = gecos ? ":!@" : " ,\t:+&#%$^()!@~*?<>=|\\/\"";
99420253Sjoerg
99520253Sjoerg	while (name[l]) {
99621052Sdavidn		if (strchr(notch, name[l]) != NULL || name[l] < ' ' || name[l] == 127 ||
99721052Sdavidn			(!gecos && l==0 && name[l] == '-') ||	/* leading '-' */
99821052Sdavidn			(!gecos && name[l] & 0x80))	/* 8-bit */
99921052Sdavidn			cmderr(EX_DATAERR, (name[l] >= ' ' && name[l] < 127)
100020325Sjoerg					    ? "invalid character `%c' in field\n"
100121052Sdavidn					    : "invalid character 0x%02x in field\n",
100220325Sjoerg					    name[l]);
100320253Sjoerg		++l;
100420253Sjoerg	}
100520325Sjoerg	if (!gecos && l > MAXLOGNAME)
100620267Sjoerg		cmderr(EX_DATAERR, "name too long `%s'\n", name);
100720679Sdavidn	return (char *)name;
100820253Sjoerg}
100920253Sjoerg
101020253Sjoerg
101120253Sjoergstatic void
101220253Sjoergrmat(uid_t uid)
101320253Sjoerg{
101420253Sjoerg	DIR            *d = opendir("/var/at/jobs");
101520253Sjoerg
101620253Sjoerg	if (d != NULL) {
101720253Sjoerg		struct dirent  *e;
101820253Sjoerg
101920253Sjoerg		while ((e = readdir(d)) != NULL) {
102020253Sjoerg			struct stat     st;
102120253Sjoerg
102220253Sjoerg			if (strncmp(e->d_name, ".lock", 5) != 0 &&
102320253Sjoerg			    stat(e->d_name, &st) == 0 &&
102420253Sjoerg			    !S_ISDIR(st.st_mode) &&
102520253Sjoerg			    st.st_uid == uid) {
102620253Sjoerg				char            tmp[MAXPATHLEN];
102720253Sjoerg
102820253Sjoerg				sprintf(tmp, "/usr/bin/atrm %s", e->d_name);
102920253Sjoerg				system(tmp);
103020253Sjoerg			}
103120253Sjoerg		}
103220253Sjoerg		closedir(d);
103320253Sjoerg	}
103420253Sjoerg}
103520747Sdavidn
103620747Sdavidnstatic void
103720747Sdavidnrmskey(char const * name)
103820747Sdavidn{
103920747Sdavidn	static const char etcskey[] = "/etc/skeykeys";
104021052Sdavidn	FILE   *fp = fopen(etcskey, "r+");
104120747Sdavidn
104221052Sdavidn	if (fp != NULL) {
104321052Sdavidn		char	tmp[1024];
104421052Sdavidn		off_t	atofs = 0;
104521052Sdavidn		int	length = strlen(name);
104620747Sdavidn
104721052Sdavidn		while (fgets(tmp, sizeof tmp, fp) != NULL) {
104821052Sdavidn			if (strncmp(name, tmp, length) == 0 && tmp[length]==' ') {
104921052Sdavidn				if (fseek(fp, atofs, SEEK_SET) == 0) {
105021052Sdavidn					fwrite("#", 1, 1, fp);	/* Comment username out */
105121052Sdavidn				}
105221052Sdavidn				break;
105320747Sdavidn			}
105421052Sdavidn			atofs = ftell(fp);
105520747Sdavidn		}
105621052Sdavidn		/*
105721052Sdavidn		 * If we got an error of any sort, don't update!
105821052Sdavidn		 */
105921052Sdavidn		fclose(fp);
106020747Sdavidn	}
106120747Sdavidn}
106220747Sdavidn
1063