pw_user.c revision 24214
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 *
2624214Sache *	$Id: pw_user.c,v 1.18 1997/03/11 14:11:43 ache 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>
3922394Sdavidn#include <utmp.h>
4022394Sdavidn#if defined(USE_MD5RAND)
4120555Sdavidn#include <md5.h>
4222394Sdavidn#endif
4320253Sjoerg#include "pw.h"
4420253Sjoerg#include "bitmap.h"
4520253Sjoerg#include "pwupd.h"
4620253Sjoerg
4723318Sache#if (MAXLOGNAME-1) > UT_NAMESIZE
4822394Sdavidn#define LOGNAMESIZE UT_NAMESIZE
4922394Sdavidn#else
5023318Sache#define LOGNAMESIZE (MAXLOGNAME-1)
5122394Sdavidn#endif
5222394Sdavidn
5324214Sachestatic          randinit;
5424214Sache
5520253Sjoergstatic int      print_user(struct passwd * pwd, int pretty);
5620253Sjoergstatic uid_t    pw_uidpolicy(struct userconf * cnf, struct cargs * args);
5720253Sjoergstatic uid_t    pw_gidpolicy(struct userconf * cnf, struct cargs * args, char *nam, gid_t prefer);
5820253Sjoergstatic time_t   pw_pwdpolicy(struct userconf * cnf, struct cargs * args);
5920253Sjoergstatic time_t   pw_exppolicy(struct userconf * cnf, struct cargs * args);
6020253Sjoergstatic char    *pw_homepolicy(struct userconf * cnf, struct cargs * args, char const * user);
6120253Sjoergstatic char    *pw_shellpolicy(struct userconf * cnf, struct cargs * args, char *newshell);
6220253Sjoergstatic char    *pw_password(struct userconf * cnf, struct cargs * args, char const * user);
6320253Sjoergstatic char    *shell_path(char const * path, char *shells[], char *sh);
6420253Sjoergstatic void     rmat(uid_t uid);
6520747Sdavidnstatic void	rmskey(char const * name);
6620253Sjoerg
6720253Sjoerg/*-
6820253Sjoerg * -C config      configuration file
6920253Sjoerg * -q             quiet operation
7020253Sjoerg * -n name        login name
7120253Sjoerg * -u uid         user id
7220253Sjoerg * -c comment     user name/comment
7320253Sjoerg * -d directory   home directory
7420253Sjoerg * -e date        account expiry date
7520253Sjoerg * -p date        password expiry date
7620253Sjoerg * -g grp         primary group
7720253Sjoerg * -G grp1,grp2   additional groups
7820253Sjoerg * -m [ -k dir ]  create and set up home
7920253Sjoerg * -s shell       name of login shell
8020253Sjoerg * -o             duplicate uid ok
8120253Sjoerg * -L class       user class
8220253Sjoerg * -l name        new login name
8320253Sjoerg * -h fd          password filehandle
8420253Sjoerg * -F             force print or add
8520253Sjoerg *   Setting defaults:
8620253Sjoerg * -D             set user defaults
8720253Sjoerg * -b dir         default home root dir
8820253Sjoerg * -e period      default expiry period
8920253Sjoerg * -p period      default password change period
9020253Sjoerg * -g group       default group
9120253Sjoerg * -G             grp1,grp2.. default additional groups
9220253Sjoerg * -L class       default login class
9320253Sjoerg * -k dir         default home skeleton
9420253Sjoerg * -s shell       default shell
9520253Sjoerg * -w method      default password method
9620253Sjoerg */
9720253Sjoerg
9820253Sjoergint
9920253Sjoergpw_user(struct userconf * cnf, int mode, struct cargs * args)
10020253Sjoerg{
10121330Sdavidn	int	        r, r1;
10220253Sjoerg	char           *p = NULL;
10320253Sjoerg	struct carg    *a_name;
10420253Sjoerg	struct carg    *a_uid;
10520253Sjoerg	struct carg    *arg;
10620253Sjoerg	struct passwd  *pwd = NULL;
10720253Sjoerg	struct group   *grp;
10820253Sjoerg	struct stat     st;
10920747Sdavidn	char            line[_PASSWORD_LEN+1];
11020253Sjoerg
11120253Sjoerg	static struct passwd fakeuser =
11220253Sjoerg	{
11320253Sjoerg		NULL,
11420253Sjoerg		"*",
11520253Sjoerg		-1,
11620253Sjoerg		-1,
11720253Sjoerg		0,
11820253Sjoerg		"",
11920253Sjoerg		"User &",
12020253Sjoerg		"/bin/sh",
12120253Sjoerg		0,
12220253Sjoerg		0
12320253Sjoerg	};
12420253Sjoerg
12520253Sjoerg	/*
12620267Sjoerg	 * With M_NEXT, we only need to return the
12720267Sjoerg	 * next uid to stdout
12820267Sjoerg	 */
12920267Sjoerg	if (mode == M_NEXT)
13020267Sjoerg	{
13120267Sjoerg		uid_t next = pw_uidpolicy(cnf, args);
13220267Sjoerg		if (getarg(args, 'q'))
13320267Sjoerg			return next;
13420267Sjoerg		printf("%ld:", (long)next);
13520267Sjoerg		pw_group(cnf, mode, args);
13620267Sjoerg		return EXIT_SUCCESS;
13720267Sjoerg	}
13820267Sjoerg
13920267Sjoerg	/*
14020253Sjoerg	 * We can do all of the common legwork here
14120253Sjoerg	 */
14220253Sjoerg
14320253Sjoerg	if ((arg = getarg(args, 'b')) != NULL) {
14420267Sjoerg		cnf->home = arg->val;
14520253Sjoerg	}
14621052Sdavidn
14721052Sdavidn	/*
14821052Sdavidn	 * If we'll need to use it or we're updating it,
14921052Sdavidn	 * then create the base home directory if necessary
15021052Sdavidn	 */
15121052Sdavidn	if (arg != NULL || getarg(args, 'm') != NULL) {
15221052Sdavidn		int	l = strlen(cnf->home);
15321052Sdavidn
15421052Sdavidn		if (l > 1 && cnf->home[l-1] == '/')	/* Shave off any trailing path delimiter */
15521052Sdavidn			cnf->home[--l] = '\0';
15621052Sdavidn
15721052Sdavidn		if (l < 2 || *cnf->home != '/')		/* Check for absolute path name */
15821052Sdavidn			cmderr(EX_DATAERR, "invalid base directory for home '%s'\n", cnf->home);
15921052Sdavidn
16021052Sdavidn		if (stat(cnf->home, &st) == -1) {
16121052Sdavidn			char	dbuf[MAXPATHLEN];
16221052Sdavidn
16321242Sdavidn			/*
16421242Sdavidn			 * This is a kludge especially for Joerg :)
16521242Sdavidn			 * If the home directory would be created in the root partition, then
16621242Sdavidn			 * we really create it under /usr which is likely to have more space.
16721242Sdavidn			 * But we create a symlink from cnf->home -> "/usr" -> cnf->home
16821242Sdavidn			 */
16921242Sdavidn			if (strchr(cnf->home+1, '/') == NULL) {
17021242Sdavidn				strcpy(dbuf, "/usr");
17121242Sdavidn				strncat(dbuf, cnf->home, MAXPATHLEN-5);
17221242Sdavidn				if (mkdir(dbuf, 0755) != -1 || errno == EEXIST) {
17321242Sdavidn					chown(dbuf, 0, 0);
17421242Sdavidn					symlink(dbuf, cnf->home);
17521242Sdavidn				}
17621242Sdavidn				/* If this falls, fall back to old method */
17721242Sdavidn			}
17821052Sdavidn			p = strncpy(dbuf, cnf->home, sizeof dbuf);
17921052Sdavidn			dbuf[MAXPATHLEN-1] = '\0';
18021242Sdavidn			if (stat(dbuf, &st) == -1) {
18121242Sdavidn				while ((p = strchr(++p, '/')) != NULL) {
18221242Sdavidn					*p = '\0';
18321242Sdavidn					if (stat(dbuf, &st) == -1) {
18421242Sdavidn						if (mkdir(dbuf, 0755) == -1)
18521242Sdavidn							goto direrr;
18621242Sdavidn						chown(dbuf, 0, 0);
18721242Sdavidn					} else if (!S_ISDIR(st.st_mode))
18821242Sdavidn						cmderr(EX_OSFILE, "'%s' (root home parent) is not a directory\n", dbuf);
18921242Sdavidn					*p = '/';
19021242Sdavidn				}
19121052Sdavidn			}
19221242Sdavidn			if (stat(dbuf, &st) == -1) {
19321052Sdavidn				if (mkdir(dbuf, 0755) == -1) {
19421052Sdavidn				direrr:	cmderr(EX_OSFILE, "mkdir '%s': %s\n", dbuf, strerror(errno));
19521052Sdavidn				}
19621052Sdavidn				chown(dbuf, 0, 0);
19721052Sdavidn			}
19821052Sdavidn		} else if (!S_ISDIR(st.st_mode))
19921052Sdavidn			cmderr(EX_OSFILE, "root home `%s' is not a directory\n", cnf->home);
20021052Sdavidn	}
20121052Sdavidn
20221052Sdavidn
20320253Sjoerg	if ((arg = getarg(args, 'e')) != NULL)
20420253Sjoerg		cnf->expire_days = atoi(arg->val);
20520253Sjoerg
20621330Sdavidn	if ((arg = getarg(args, 'y')) != NULL)
20721330Sdavidn		cnf->nispasswd = arg->val;
20821330Sdavidn
20920253Sjoerg	if ((arg = getarg(args, 'p')) != NULL && arg->val)
21020253Sjoerg		cnf->password_days = atoi(arg->val);
21120253Sjoerg
21220253Sjoerg	if ((arg = getarg(args, 'g')) != NULL) {
21320253Sjoerg		p = arg->val;
21420253Sjoerg		if ((grp = getgrnam(p)) == NULL) {
21520253Sjoerg			if (!isdigit(*p) || (grp = getgrgid((gid_t) atoi(p))) == NULL)
21620267Sjoerg				cmderr(EX_NOUSER, "group `%s' does not exist\n", p);
21720253Sjoerg		}
21820253Sjoerg		cnf->default_group = newstr(grp->gr_name);
21920253Sjoerg	}
22020253Sjoerg	if ((arg = getarg(args, 'L')) != NULL)
22120679Sdavidn		cnf->default_class = pw_checkname((u_char *)arg->val, 0);
22220253Sjoerg
22320253Sjoerg	if ((arg = getarg(args, 'G')) != NULL && arg->val) {
22420253Sjoerg		int             i = 0;
22520253Sjoerg
22620747Sdavidn		for (p = strtok(arg->val, ", \t"); p != NULL; p = strtok(NULL, ", \t")) {
22720253Sjoerg			if ((grp = getgrnam(p)) == NULL) {
22820253Sjoerg				if (!isdigit(*p) || (grp = getgrgid((gid_t) atoi(p))) == NULL)
22920267Sjoerg					cmderr(EX_NOUSER, "group `%s' does not exist\n", p);
23020253Sjoerg			}
23120747Sdavidn			if (extendarray(&cnf->groups, &cnf->numgroups, i + 2) != -1)
23220747Sdavidn				cnf->groups[i++] = newstr(grp->gr_name);
23320253Sjoerg		}
23420747Sdavidn		while (i < cnf->numgroups)
23520253Sjoerg			cnf->groups[i++] = NULL;
23620253Sjoerg	}
23720253Sjoerg	if ((arg = getarg(args, 'k')) != NULL) {
23820253Sjoerg		if (stat(cnf->dotdir = arg->val, &st) == -1 || S_ISDIR(st.st_mode))
23920267Sjoerg			cmderr(EX_OSFILE, "skeleton `%s' is not a directory or does not exist\n", cnf->dotdir);
24020253Sjoerg	}
24120253Sjoerg	if ((arg = getarg(args, 's')) != NULL)
24220253Sjoerg		cnf->shell_default = arg->val;
24320253Sjoerg
24420253Sjoerg	if (mode == M_ADD && getarg(args, 'D')) {
24520253Sjoerg		if (getarg(args, 'n') != NULL)
24620267Sjoerg			cmderr(EX_DATAERR, "can't combine `-D' with `-n name'\n");
24720253Sjoerg		if ((arg = getarg(args, 'u')) != NULL && (p = strtok(arg->val, ", \t")) != NULL) {
24820253Sjoerg			if ((cnf->min_uid = (uid_t) atoi(p)) == 0)
24920253Sjoerg				cnf->min_uid = 1000;
25020253Sjoerg			if ((p = strtok(NULL, " ,\t")) == NULL || (cnf->max_uid = (uid_t) atoi(p)) < cnf->min_uid)
25120253Sjoerg				cnf->max_uid = 32000;
25220253Sjoerg		}
25320253Sjoerg		if ((arg = getarg(args, 'i')) != NULL && (p = strtok(arg->val, ", \t")) != NULL) {
25420253Sjoerg			if ((cnf->min_gid = (gid_t) atoi(p)) == 0)
25520253Sjoerg				cnf->min_gid = 1000;
25620253Sjoerg			if ((p = strtok(NULL, " ,\t")) == NULL || (cnf->max_gid = (gid_t) atoi(p)) < cnf->min_gid)
25720253Sjoerg				cnf->max_gid = 32000;
25820253Sjoerg		}
25920253Sjoerg		if ((arg = getarg(args, 'w')) != NULL)
26020253Sjoerg			cnf->default_password = boolean_val(arg->val, cnf->default_password);
26120253Sjoerg
26220253Sjoerg		arg = getarg(args, 'C');
26320253Sjoerg		if (write_userconfig(arg ? arg->val : NULL))
26420267Sjoerg			return EXIT_SUCCESS;
26520253Sjoerg		perror("config update");
26620267Sjoerg		return EX_IOERR;
26720253Sjoerg	}
26820253Sjoerg	if (mode == M_PRINT && getarg(args, 'a')) {
26920267Sjoerg		int             pretty = getarg(args, 'P') != NULL;
27020253Sjoerg
27120253Sjoerg		setpwent();
27220253Sjoerg		while ((pwd = getpwent()) != NULL)
27320253Sjoerg			print_user(pwd, pretty);
27420253Sjoerg		endpwent();
27520267Sjoerg		return EXIT_SUCCESS;
27620253Sjoerg	}
27720253Sjoerg	if ((a_name = getarg(args, 'n')) != NULL)
27820679Sdavidn		pwd = getpwnam(pw_checkname((u_char *)a_name->val, 0));
27920253Sjoerg	a_uid = getarg(args, 'u');
28020253Sjoerg
28120253Sjoerg	if (a_uid == NULL) {
28220253Sjoerg		if (a_name == NULL)
28320267Sjoerg			cmderr(EX_DATAERR, "user name or id required\n");
28420253Sjoerg
28520253Sjoerg		/*
28620253Sjoerg		 * Determine whether 'n' switch is name or uid - we don't
28720253Sjoerg		 * really don't really care which we have, but we need to
28820253Sjoerg		 * know.
28920253Sjoerg		 */
29020253Sjoerg		if (mode != M_ADD && pwd == NULL && isdigit(*a_name->val) && atoi(a_name->val) > 0) {	/* Assume uid */
29120253Sjoerg			(a_uid = a_name)->ch = 'u';
29220253Sjoerg			a_name = NULL;
29320253Sjoerg		}
29420253Sjoerg	}
29520253Sjoerg	/*
29620253Sjoerg	 * Update, delete & print require that the user exists
29720253Sjoerg	 */
29820253Sjoerg	if (mode == M_UPDATE || mode == M_DELETE || mode == M_PRINT) {
29920253Sjoerg		if (a_name == NULL && pwd == NULL)	/* Try harder */
30020253Sjoerg			pwd = getpwuid(atoi(a_uid->val));
30120253Sjoerg
30220253Sjoerg		if (pwd == NULL) {
30320253Sjoerg			if (mode == M_PRINT && getarg(args, 'F')) {
30420253Sjoerg				fakeuser.pw_name = a_name ? a_name->val : "nouser";
30520253Sjoerg				fakeuser.pw_uid = a_uid ? (uid_t) atol(a_uid->val) : -1;
30620267Sjoerg				return print_user(&fakeuser, getarg(args, 'P') != NULL);
30720253Sjoerg			}
30820253Sjoerg			if (a_name == NULL)
30920267Sjoerg				cmderr(EX_NOUSER, "no such uid `%s'\n", a_uid->val);
31020267Sjoerg			cmderr(EX_NOUSER, "no such user `%s'\n", a_name->val);
31120253Sjoerg		}
31220253Sjoerg		if (a_name == NULL)	/* May be needed later */
31320253Sjoerg			a_name = addarg(args, 'n', newstr(pwd->pw_name));
31420253Sjoerg
31520253Sjoerg		/*
31620253Sjoerg		 * Handle deletions now
31720253Sjoerg		 */
31820253Sjoerg		if (mode == M_DELETE) {
31920253Sjoerg			char            file[MAXPATHLEN];
32020253Sjoerg			char            home[MAXPATHLEN];
32120253Sjoerg			uid_t           uid = pwd->pw_uid;
32220253Sjoerg
32320253Sjoerg			if (strcmp(pwd->pw_name, "root") == 0)
32420267Sjoerg				cmderr(EX_DATAERR, "cannot remove user 'root'\n");
32520253Sjoerg
32620253Sjoerg			/*
32720747Sdavidn			 * Remove skey record from /etc/skeykeys
32820747Sdavidn			 */
32920747Sdavidn
33020747Sdavidn			rmskey(pwd->pw_name);
33120747Sdavidn
33220747Sdavidn			/*
33320253Sjoerg			 * Remove crontabs
33420253Sjoerg			 */
33520253Sjoerg			sprintf(file, "/var/cron/tabs/%s", pwd->pw_name);
33620253Sjoerg			if (access(file, F_OK) == 0) {
33720253Sjoerg				sprintf(file, "crontab -u %s -r", pwd->pw_name);
33820253Sjoerg				system(file);
33920253Sjoerg			}
34020253Sjoerg			/*
34120253Sjoerg			 * Save these for later, since contents of pwd may be
34220253Sjoerg			 * invalidated by deletion
34320253Sjoerg			 */
34420253Sjoerg			sprintf(file, "%s/%s", _PATH_MAILDIR, pwd->pw_name);
34520253Sjoerg			strncpy(home, pwd->pw_dir, sizeof home);
34620253Sjoerg			home[sizeof home - 1] = '\0';
34720253Sjoerg
34820253Sjoerg			if (!delpwent(pwd))
34920267Sjoerg				cmderr(EX_IOERR, "Error updating passwd file: %s\n", strerror(errno));
35021330Sdavidn
35121330Sdavidn			if (cnf->nispasswd && *cnf->nispasswd=='/' && !delnispwent(cnf->nispasswd, a_name->val))
35221330Sdavidn				perror("WARNING: NIS passwd update");
35321330Sdavidn
35420253Sjoerg			editgroups(a_name->val, NULL);
35520253Sjoerg
35620253Sjoerg			pw_log(cnf, mode, W_USER, "%s(%ld) account removed", a_name->val, (long) uid);
35720253Sjoerg
35820253Sjoerg			/*
35920253Sjoerg			 * Remove mail file
36020253Sjoerg			 */
36120253Sjoerg			remove(file);
36220253Sjoerg
36320253Sjoerg			/*
36420253Sjoerg			 * Remove at jobs
36520253Sjoerg			 */
36620253Sjoerg			if (getpwuid(uid) == NULL)
36720253Sjoerg				rmat(uid);
36820253Sjoerg
36920253Sjoerg			/*
37020253Sjoerg			 * Remove home directory and contents
37120253Sjoerg			 */
37220253Sjoerg			if (getarg(args, 'r') != NULL && *home == '/' && getpwuid(uid) == NULL) {
37320253Sjoerg				if (stat(home, &st) != -1) {
37420253Sjoerg					rm_r(home, uid);
37520253Sjoerg					pw_log(cnf, mode, W_USER, "%s(%ld) home '%s' %sremoved",
37620253Sjoerg					       a_name->val, (long) uid, home,
37720253Sjoerg					       stat(home, &st) == -1 ? "" : "not completely ");
37820253Sjoerg				}
37920253Sjoerg			}
38020267Sjoerg			return EXIT_SUCCESS;
38120253Sjoerg		} else if (mode == M_PRINT)
38220267Sjoerg			return print_user(pwd, getarg(args, 'P') != NULL);
38320253Sjoerg
38420253Sjoerg		/*
38520253Sjoerg		 * The rest is edit code
38620253Sjoerg		 */
38720253Sjoerg		if ((arg = getarg(args, 'l')) != NULL) {
38820253Sjoerg			if (strcmp(pwd->pw_name, "root") == 0)
38920267Sjoerg				cmderr(EX_DATAERR, "can't rename `root' account\n");
39020679Sdavidn			pwd->pw_name = pw_checkname((u_char *)arg->val, 0);
39120253Sjoerg		}
39220253Sjoerg		if ((arg = getarg(args, 'u')) != NULL && isdigit(*arg->val)) {
39320253Sjoerg			pwd->pw_uid = (uid_t) atol(arg->val);
39420253Sjoerg			if (pwd->pw_uid != 0 && strcmp(pwd->pw_name, "root") == 0)
39520267Sjoerg				cmderr(EX_DATAERR, "can't change uid of `root' account\n");
39620253Sjoerg			if (pwd->pw_uid == 0 && strcmp(pwd->pw_name, "root") != 0)
39720253Sjoerg				fprintf(stderr, "WARNING: account `%s' will have a uid of 0 (superuser access!)\n", pwd->pw_name);
39820253Sjoerg		}
39920253Sjoerg		if ((arg = getarg(args, 'g')) != NULL && pwd->pw_uid != 0)	/* Already checked this */
40020253Sjoerg			pwd->pw_gid = (gid_t) getgrnam(cnf->default_group)->gr_gid;
40120253Sjoerg
40220253Sjoerg		if ((arg = getarg(args, 'p')) != NULL) {
40320253Sjoerg			if (*arg->val == '\0' || strcmp(arg->val, "0") == 0)
40420253Sjoerg				pwd->pw_change = 0;
40520253Sjoerg			else {
40620253Sjoerg				time_t          now = time(NULL);
40720253Sjoerg				time_t          expire = parse_date(now, arg->val);
40820253Sjoerg
40920253Sjoerg				if (now == expire)
41020267Sjoerg					cmderr(EX_DATAERR, "Invalid password change date `%s'\n", arg->val);
41120253Sjoerg				pwd->pw_change = expire;
41220253Sjoerg			}
41320253Sjoerg		}
41420267Sjoerg		if ((arg = getarg(args, 'e')) != NULL) {
41520253Sjoerg			if (*arg->val == '\0' || strcmp(arg->val, "0") == 0)
41620253Sjoerg				pwd->pw_expire = 0;
41720253Sjoerg			else {
41820253Sjoerg				time_t          now = time(NULL);
41920253Sjoerg				time_t          expire = parse_date(now, arg->val);
42020253Sjoerg
42120253Sjoerg				if (now == expire)
42220267Sjoerg					cmderr(EX_DATAERR, "Invalid account expiry date `%s'\n", arg->val);
42320253Sjoerg				pwd->pw_expire = expire;
42420253Sjoerg			}
42520253Sjoerg		}
42620253Sjoerg		if ((arg = getarg(args, 's')) != NULL)
42720253Sjoerg			pwd->pw_shell = shell_path(cnf->shelldir, cnf->shells, arg->val);
42820253Sjoerg
42920253Sjoerg		if (getarg(args, 'L'))
43020253Sjoerg			pwd->pw_class = cnf->default_class;
43120253Sjoerg
43220747Sdavidn		if ((arg  = getarg(args, 'd')) != NULL) {
43320747Sdavidn			if (stat(pwd->pw_dir = arg->val, &st) == -1) {
43420747Sdavidn				if (getarg(args, 'm') == NULL && strcmp(pwd->pw_dir, "/nonexistent") != 0)
43520747Sdavidn				  fprintf(stderr, "WARNING: home `%s' does not exist\n", pwd->pw_dir);
43620747Sdavidn			} else if (!S_ISDIR(st.st_mode))
43720747Sdavidn				fprintf(stderr, "WARNING: home `%s' is not a directory\n", pwd->pw_dir);
43820747Sdavidn		}
43920747Sdavidn
44020267Sjoerg		if ((arg = getarg(args, 'w')) != NULL && getarg(args, 'h') == NULL)
44120267Sjoerg			pwd->pw_passwd = pw_password(cnf, args, pwd->pw_name);
44220267Sjoerg
44320253Sjoerg	} else {
44420253Sjoerg		if (a_name == NULL)	/* Required */
44520267Sjoerg			cmderr(EX_DATAERR, "login name required\n");
44620253Sjoerg		else if ((pwd = getpwnam(a_name->val)) != NULL)	/* Exists */
44720267Sjoerg			cmderr(EX_DATAERR, "login name `%s' already exists\n", a_name->val);
44820253Sjoerg
44920253Sjoerg		/*
45020253Sjoerg		 * Now, set up defaults for a new user
45120253Sjoerg		 */
45220253Sjoerg		pwd = &fakeuser;
45320253Sjoerg		pwd->pw_name = a_name->val;
45420253Sjoerg		pwd->pw_class = cnf->default_class ? cnf->default_class : "";
45520253Sjoerg		pwd->pw_passwd = pw_password(cnf, args, pwd->pw_name);
45620253Sjoerg		pwd->pw_uid = pw_uidpolicy(cnf, args);
45720253Sjoerg		pwd->pw_gid = pw_gidpolicy(cnf, args, pwd->pw_name, (gid_t) pwd->pw_uid);
45820253Sjoerg		pwd->pw_change = pw_pwdpolicy(cnf, args);
45920253Sjoerg		pwd->pw_expire = pw_exppolicy(cnf, args);
46020253Sjoerg		pwd->pw_dir = pw_homepolicy(cnf, args, pwd->pw_name);
46120253Sjoerg		pwd->pw_shell = pw_shellpolicy(cnf, args, NULL);
46220253Sjoerg
46320253Sjoerg		if (pwd->pw_uid == 0 && strcmp(pwd->pw_name, "root") != 0)
46420253Sjoerg			fprintf(stderr, "WARNING: new account `%s' has a uid of 0 (superuser access!)\n", pwd->pw_name);
46520253Sjoerg	}
46620253Sjoerg
46720253Sjoerg	/*
46820253Sjoerg	 * Shared add/edit code
46920253Sjoerg	 */
47020253Sjoerg	if ((arg = getarg(args, 'c')) != NULL)
47120679Sdavidn		pwd->pw_gecos = pw_checkname((u_char *)arg->val, 1);
47220253Sjoerg
47320253Sjoerg	if ((arg = getarg(args, 'h')) != NULL) {
47420253Sjoerg		if (strcmp(arg->val, "-") == 0)
47520253Sjoerg			pwd->pw_passwd = "*";	/* No access */
47620253Sjoerg		else {
47720253Sjoerg			int             fd = atoi(arg->val);
47820253Sjoerg			int             b;
47920253Sjoerg			int             istty = isatty(fd);
48020253Sjoerg			struct termios  t;
48120253Sjoerg
48220253Sjoerg			if (istty) {
48320253Sjoerg				if (tcgetattr(fd, &t) == -1)
48420253Sjoerg					istty = 0;
48520253Sjoerg				else {
48620253Sjoerg					struct termios  n = t;
48720253Sjoerg
48820253Sjoerg					/* Disable echo */
48920253Sjoerg					n.c_lflag &= ~(ECHO);
49020253Sjoerg					tcsetattr(fd, TCSANOW, &n);
49120253Sjoerg					printf("%sassword for user %s:", (mode == M_UPDATE) ? "New p" : "P", pwd->pw_name);
49220253Sjoerg					fflush(stdout);
49320253Sjoerg				}
49420253Sjoerg			}
49520253Sjoerg			b = read(fd, line, sizeof(line) - 1);
49620253Sjoerg			if (istty) {	/* Restore state */
49720253Sjoerg				tcsetattr(fd, TCSANOW, &t);
49820253Sjoerg				fputc('\n', stdout);
49920253Sjoerg				fflush(stdout);
50020253Sjoerg			}
50120253Sjoerg			if (b < 0) {
50220253Sjoerg				perror("-h file descriptor");
50320267Sjoerg				return EX_IOERR;
50420253Sjoerg			}
50520253Sjoerg			line[b] = '\0';
50620253Sjoerg			if ((p = strpbrk(line, " \t\r\n")) != NULL)
50720253Sjoerg				*p = '\0';
50820253Sjoerg			if (!*line)
50920267Sjoerg				cmderr(EX_DATAERR, "empty password read on file descriptor %d\n", fd);
51020253Sjoerg			pwd->pw_passwd = pw_pwcrypt(line);
51120253Sjoerg		}
51220253Sjoerg	}
51320267Sjoerg
51420267Sjoerg	/*
51520267Sjoerg	 * Special case: -N only displays & exits
51620267Sjoerg	 */
51720267Sjoerg	if (getarg(args, 'N') != NULL)
51820267Sjoerg		return print_user(pwd, getarg(args, 'P') != NULL);
51920267Sjoerg
52021330Sdavidn	r = r1 = 1;
52121330Sdavidn	if (mode == M_ADD) {
52221330Sdavidn		r = addpwent(pwd);
52321330Sdavidn		if (r && cnf->nispasswd && *cnf->nispasswd=='/')
52421330Sdavidn			r1 = addnispwent(cnf->nispasswd, pwd);
52521330Sdavidn	} else if (mode == M_UPDATE) {
52621330Sdavidn		r = chgpwent(a_name->val, pwd);
52721330Sdavidn		if (r && cnf->nispasswd && *cnf->nispasswd=='/')
52821330Sdavidn			r1 = chgnispwent(cnf->nispasswd, a_name->val, pwd);
52921330Sdavidn	}
53021330Sdavidn
53121330Sdavidn	if (!r) {
53220253Sjoerg		perror("password update");
53320267Sjoerg		return EX_IOERR;
53421330Sdavidn	} else if (!r1) {
53521330Sdavidn		perror("WARNING: NIS password update");
53621330Sdavidn		/* Keep on trucking */
53720253Sjoerg	}
53821330Sdavidn
53920253Sjoerg	/*
54020253Sjoerg	 * Ok, user is created or changed - now edit group file
54120253Sjoerg	 */
54220253Sjoerg
54320253Sjoerg	if (mode == M_ADD || getarg(args, 'G') != NULL)
54420253Sjoerg		editgroups(pwd->pw_name, cnf->groups);
54520253Sjoerg
54620253Sjoerg	/* pwd may have been invalidated */
54720253Sjoerg	if ((pwd = getpwnam(a_name->val)) == NULL)
54820267Sjoerg		cmderr(EX_NOUSER, "user '%s' disappeared during update\n", a_name->val);
54920253Sjoerg
55020253Sjoerg	grp = getgrgid(pwd->pw_gid);
55120253Sjoerg	pw_log(cnf, mode, W_USER, "%s(%ld):%s(%d):%s:%s:%s",
55220253Sjoerg	       pwd->pw_name, (long) pwd->pw_uid,
55320253Sjoerg	    grp ? grp->gr_name : "unknown", (long) (grp ? grp->gr_gid : -1),
55420253Sjoerg	       pwd->pw_gecos, pwd->pw_dir, pwd->pw_shell);
55520253Sjoerg
55620253Sjoerg	/*
55720253Sjoerg	 * If adding, let's touch and chown the user's mail file. This is not
55820253Sjoerg	 * strictly necessary under BSD with a 0755 maildir but it also
55920253Sjoerg	 * doesn't hurt anything to create the empty mailfile
56020253Sjoerg	 */
56120253Sjoerg	if (mode == M_ADD) {
56220253Sjoerg		FILE           *fp;
56320253Sjoerg
56420253Sjoerg		sprintf(line, "%s/%s", _PATH_MAILDIR, pwd->pw_name);
56520253Sjoerg		close(open(line, O_RDWR | O_CREAT, 0600));	/* Preserve contents &
56620253Sjoerg								 * mtime */
56720253Sjoerg		chown(line, pwd->pw_uid, pwd->pw_gid);
56820253Sjoerg
56920253Sjoerg		/*
57020253Sjoerg		 * Send mail to the new user as well, if we are asked to
57120253Sjoerg		 */
57220253Sjoerg		if (cnf->newmail && *cnf->newmail && (fp = fopen(cnf->newmail, "r")) != NULL) {
57320253Sjoerg			FILE           *pfp = popen(_PATH_SENDMAIL " -t", "w");
57420253Sjoerg
57520253Sjoerg			if (pfp == NULL)
57620253Sjoerg				perror("sendmail");
57720253Sjoerg			else {
57820253Sjoerg				fprintf(pfp, "From: root\n" "To: %s\n" "Subject: Welcome!\n\n", pwd->pw_name);
57920253Sjoerg				while (fgets(line, sizeof(line), fp) != NULL) {
58020253Sjoerg					/* Do substitutions? */
58120253Sjoerg					fputs(line, pfp);
58220253Sjoerg				}
58320253Sjoerg				pclose(pfp);
58420253Sjoerg				pw_log(cnf, mode, W_USER, "%s(%ld) new user mail sent",
58520253Sjoerg				       pwd->pw_name, (long) pwd->pw_uid);
58620253Sjoerg			}
58720253Sjoerg			fclose(fp);
58820253Sjoerg		}
58920253Sjoerg	}
59020253Sjoerg	/*
59120253Sjoerg	 * Finally, let's create and populate the user's home directory. Note
59220253Sjoerg	 * that this also `works' for editing users if -m is used, but
59320253Sjoerg	 * existing files will *not* be overwritten.
59420253Sjoerg	 */
59520253Sjoerg	if (getarg(args, 'm') != NULL && pwd->pw_dir && *pwd->pw_dir == '/' && pwd->pw_dir[1]) {
59620253Sjoerg		copymkdir(pwd->pw_dir, cnf->dotdir, 0755, pwd->pw_uid, pwd->pw_gid);
59720253Sjoerg		pw_log(cnf, mode, W_USER, "%s(%ld) home %s made",
59820253Sjoerg		       pwd->pw_name, (long) pwd->pw_uid, pwd->pw_dir);
59920253Sjoerg	}
60020267Sjoerg	return EXIT_SUCCESS;
60120253Sjoerg}
60220253Sjoerg
60320253Sjoerg
60420253Sjoergstatic          uid_t
60520253Sjoergpw_uidpolicy(struct userconf * cnf, struct cargs * args)
60620253Sjoerg{
60720253Sjoerg	struct passwd  *pwd;
60820253Sjoerg	uid_t           uid = (uid_t) - 1;
60920253Sjoerg	struct carg    *a_uid = getarg(args, 'u');
61020253Sjoerg
61120253Sjoerg	/*
61220253Sjoerg	 * Check the given uid, if any
61320253Sjoerg	 */
61420253Sjoerg	if (a_uid != NULL) {
61520253Sjoerg		uid = (uid_t) atol(a_uid->val);
61620253Sjoerg
61720253Sjoerg		if ((pwd = getpwuid(uid)) != NULL && getarg(args, 'o') == NULL)
61820267Sjoerg			cmderr(EX_DATAERR, "uid `%ld' has already been allocated\n", (long) pwd->pw_uid);
61920253Sjoerg	} else {
62020253Sjoerg		struct bitmap   bm;
62120253Sjoerg
62220253Sjoerg		/*
62320253Sjoerg		 * We need to allocate the next available uid under one of
62420253Sjoerg		 * two policies a) Grab the first unused uid b) Grab the
62520253Sjoerg		 * highest possible unused uid
62620253Sjoerg		 */
62720253Sjoerg		if (cnf->min_uid >= cnf->max_uid) {	/* Sanity
62820253Sjoerg							 * claus^H^H^H^Hheck */
62920253Sjoerg			cnf->min_uid = 1000;
63020253Sjoerg			cnf->max_uid = 32000;
63120253Sjoerg		}
63220253Sjoerg		bm = bm_alloc(cnf->max_uid - cnf->min_uid + 1);
63320253Sjoerg
63420253Sjoerg		/*
63520253Sjoerg		 * Now, let's fill the bitmap from the password file
63620253Sjoerg		 */
63720253Sjoerg		setpwent();
63820253Sjoerg		while ((pwd = getpwent()) != NULL)
63920253Sjoerg			if (pwd->pw_uid >= (int) cnf->min_uid && pwd->pw_uid <= (int) cnf->max_uid)
64020253Sjoerg				bm_setbit(&bm, pwd->pw_uid - cnf->min_uid);
64120253Sjoerg		endpwent();
64220253Sjoerg
64320253Sjoerg		/*
64420253Sjoerg		 * Then apply the policy, with fallback to reuse if necessary
64520253Sjoerg		 */
64620253Sjoerg		if (cnf->reuse_uids || (uid = (uid_t) (bm_lastset(&bm) + cnf->min_uid + 1)) > cnf->max_uid)
64720253Sjoerg			uid = (uid_t) (bm_firstunset(&bm) + cnf->min_uid);
64820253Sjoerg
64920253Sjoerg		/*
65020253Sjoerg		 * Another sanity check
65120253Sjoerg		 */
65220253Sjoerg		if (uid < cnf->min_uid || uid > cnf->max_uid)
65320267Sjoerg			cmderr(EX_SOFTWARE, "unable to allocate a new uid - range fully used\n");
65420253Sjoerg		bm_dealloc(&bm);
65520253Sjoerg	}
65620253Sjoerg	return uid;
65720253Sjoerg}
65820253Sjoerg
65920253Sjoerg
66020253Sjoergstatic          uid_t
66120253Sjoergpw_gidpolicy(struct userconf * cnf, struct cargs * args, char *nam, gid_t prefer)
66220253Sjoerg{
66320253Sjoerg	struct group   *grp;
66420253Sjoerg	gid_t           gid = (uid_t) - 1;
66520253Sjoerg	struct carg    *a_gid = getarg(args, 'g');
66620253Sjoerg
66720253Sjoerg	/*
66820253Sjoerg	 * If no arg given, see if default can help out
66920253Sjoerg	 */
67020253Sjoerg	if (a_gid == NULL && cnf->default_group && *cnf->default_group)
67120253Sjoerg		a_gid = addarg(args, 'g', cnf->default_group);
67220253Sjoerg
67320253Sjoerg	/*
67420253Sjoerg	 * Check the given gid, if any
67520253Sjoerg	 */
67620267Sjoerg	setgrent();
67720253Sjoerg	if (a_gid != NULL) {
67820253Sjoerg		if ((grp = getgrnam(a_gid->val)) == NULL) {
67920253Sjoerg			gid = (gid_t) atol(a_gid->val);
68020253Sjoerg			if ((gid == 0 && !isdigit(*a_gid->val)) || (grp = getgrgid(gid)) == NULL)
68120267Sjoerg				cmderr(EX_NOUSER, "group `%s' is not defined\n", a_gid->val);
68220253Sjoerg		}
68320253Sjoerg		gid = grp->gr_gid;
68420267Sjoerg	} else if ((grp = getgrnam(nam)) != NULL && grp->gr_mem[0] == NULL) {
68520267Sjoerg		gid = grp->gr_gid;  /* Already created? Use it anyway... */
68620253Sjoerg	} else {
68720253Sjoerg		struct cargs    grpargs;
68820253Sjoerg		char            tmp[32];
68920253Sjoerg
69020253Sjoerg		LIST_INIT(&grpargs);
69120253Sjoerg		addarg(&grpargs, 'n', nam);
69220253Sjoerg
69320253Sjoerg		/*
69420253Sjoerg		 * We need to auto-create a group with the user's name. We
69520253Sjoerg		 * can send all the appropriate output to our sister routine
69620253Sjoerg		 * bit first see if we can create a group with gid==uid so we
69720253Sjoerg		 * can keep the user and group ids in sync. We purposely do
69820253Sjoerg		 * NOT check the gid range if we can force the sync. If the
69920253Sjoerg		 * user's name dups an existing group, then the group add
70020253Sjoerg		 * function will happily handle that case for us and exit.
70120253Sjoerg		 */
70220253Sjoerg		if (getgrgid(prefer) == NULL) {
70320253Sjoerg			sprintf(tmp, "%lu", (unsigned long) prefer);
70420253Sjoerg			addarg(&grpargs, 'g', tmp);
70520253Sjoerg		}
70620267Sjoerg		if (getarg(args, 'N'))
70720267Sjoerg		{
70820267Sjoerg			addarg(&grpargs, 'N', NULL);
70920267Sjoerg			addarg(&grpargs, 'q', NULL);
71020267Sjoerg			gid = pw_group(cnf, M_NEXT, &grpargs);
71120267Sjoerg		}
71220267Sjoerg		else
71320267Sjoerg		{
71420267Sjoerg			pw_group(cnf, M_ADD, &grpargs);
71520267Sjoerg			if ((grp = getgrnam(nam)) != NULL)
71620267Sjoerg				gid = grp->gr_gid;
71720267Sjoerg		}
71820253Sjoerg		a_gid = grpargs.lh_first;
71920253Sjoerg		while (a_gid != NULL) {
72020253Sjoerg			struct carg    *t = a_gid->list.le_next;
72120253Sjoerg			LIST_REMOVE(a_gid, list);
72220253Sjoerg			a_gid = t;
72320253Sjoerg		}
72420253Sjoerg	}
72520267Sjoerg	endgrent();
72620253Sjoerg	return gid;
72720253Sjoerg}
72820253Sjoerg
72920253Sjoerg
73020253Sjoergstatic          time_t
73120253Sjoergpw_pwdpolicy(struct userconf * cnf, struct cargs * args)
73220253Sjoerg{
73320253Sjoerg	time_t          result = 0;
73420253Sjoerg	time_t          now = time(NULL);
73520253Sjoerg	struct carg    *arg = getarg(args, 'e');
73620253Sjoerg
73720253Sjoerg	if (arg != NULL) {
73820253Sjoerg		if ((result = parse_date(now, arg->val)) == now)
73920267Sjoerg			cmderr(EX_DATAERR, "invalid date/time `%s'\n", arg->val);
74020253Sjoerg	} else if (cnf->password_days > 0)
74120253Sjoerg		result = now + ((long) cnf->password_days * 86400L);
74220253Sjoerg	return result;
74320253Sjoerg}
74420253Sjoerg
74520253Sjoerg
74620253Sjoergstatic          time_t
74720253Sjoergpw_exppolicy(struct userconf * cnf, struct cargs * args)
74820253Sjoerg{
74920253Sjoerg	time_t          result = 0;
75020253Sjoerg	time_t          now = time(NULL);
75120253Sjoerg	struct carg    *arg = getarg(args, 'e');
75220253Sjoerg
75320253Sjoerg	if (arg != NULL) {
75420253Sjoerg		if ((result = parse_date(now, arg->val)) == now)
75520267Sjoerg			cmderr(EX_DATAERR, "invalid date/time `%s'\n", arg->val);
75620253Sjoerg	} else if (cnf->expire_days > 0)
75720253Sjoerg		result = now + ((long) cnf->expire_days * 86400L);
75820253Sjoerg	return result;
75920253Sjoerg}
76020253Sjoerg
76120253Sjoerg
76220253Sjoergstatic char    *
76320253Sjoergpw_homepolicy(struct userconf * cnf, struct cargs * args, char const * user)
76420253Sjoerg{
76520253Sjoerg	struct carg    *arg = getarg(args, 'd');
76620253Sjoerg
76720253Sjoerg	if (arg)
76820253Sjoerg		return arg->val;
76920253Sjoerg	else {
77020253Sjoerg		static char     home[128];
77120253Sjoerg
77220253Sjoerg		if (cnf->home == NULL || *cnf->home == '\0')
77320267Sjoerg			cmderr(EX_CONFIG, "no base home directory set\n");
77420253Sjoerg		sprintf(home, "%s/%s", cnf->home, user);
77520253Sjoerg		return home;
77620253Sjoerg	}
77720253Sjoerg}
77820253Sjoerg
77920253Sjoergstatic char    *
78020253Sjoergshell_path(char const * path, char *shells[], char *sh)
78120253Sjoerg{
78220253Sjoerg	if (sh != NULL && (*sh == '/' || *sh == '\0'))
78320253Sjoerg		return sh;	/* specified full path or forced none */
78420253Sjoerg	else {
78520253Sjoerg		char           *p;
78620253Sjoerg		char            paths[_UC_MAXLINE];
78720253Sjoerg
78820253Sjoerg		/*
78920253Sjoerg		 * We need to search paths
79020253Sjoerg		 */
79120253Sjoerg		strncpy(paths, path, sizeof paths);
79220253Sjoerg		paths[sizeof paths - 1] = '\0';
79320253Sjoerg		for (p = strtok(paths, ": \t\r\n"); p != NULL; p = strtok(NULL, ": \t\r\n")) {
79420253Sjoerg			int             i;
79520253Sjoerg			static char     shellpath[256];
79620253Sjoerg
79720253Sjoerg			if (sh != NULL) {
79820253Sjoerg				sprintf(shellpath, "%s/%s", p, sh);
79920253Sjoerg				if (access(shellpath, X_OK) == 0)
80020253Sjoerg					return shellpath;
80120253Sjoerg			} else
80220253Sjoerg				for (i = 0; i < _UC_MAXSHELLS && shells[i] != NULL; i++) {
80320253Sjoerg					sprintf(shellpath, "%s/%s", p, shells[i]);
80420253Sjoerg					if (access(shellpath, X_OK) == 0)
80520253Sjoerg						return shellpath;
80620253Sjoerg				}
80720253Sjoerg		}
80820253Sjoerg		if (sh == NULL)
80920267Sjoerg			cmderr(EX_OSFILE, "can't find shell `%s' in shell paths\n", sh);
81020267Sjoerg		cmderr(EX_CONFIG, "no default shell available or defined\n");
81120253Sjoerg		return NULL;
81220253Sjoerg	}
81320253Sjoerg}
81420253Sjoerg
81520253Sjoerg
81620253Sjoergstatic char    *
81720253Sjoergpw_shellpolicy(struct userconf * cnf, struct cargs * args, char *newshell)
81820253Sjoerg{
81920253Sjoerg	char           *sh = newshell;
82020253Sjoerg	struct carg    *arg = getarg(args, 's');
82120253Sjoerg
82220253Sjoerg	if (newshell == NULL && arg != NULL)
82320253Sjoerg		sh = arg->val;
82420253Sjoerg	return shell_path(cnf->shelldir, cnf->shells, sh ? sh : cnf->shell_default);
82520253Sjoerg}
82620253Sjoerg
82720253Sjoergstatic char const chars[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.";
82820253Sjoerg
82920253Sjoergchar           *
83020253Sjoergpw_pwcrypt(char *password)
83120253Sjoerg{
83220253Sjoerg	int             i;
83320253Sjoerg	char            salt[12];
83420253Sjoerg
83520253Sjoerg	static char     buf[256];
83620253Sjoerg
83720253Sjoerg	/*
83820253Sjoerg	 * Calculate a salt value
83920253Sjoerg	 */
84024214Sache	if (!randinit) {
84124214Sache		randinit = 1;
84224214Sache#ifdef __FreeBSD__
84324214Sache		if (srandomdev() < 0)
84424214Sache#endif
84524214Sache			srandom((unsigned long) (time(NULL) ^ getpid()));
84624214Sache	}
84720253Sjoerg	for (i = 0; i < 8; i++)
84820253Sjoerg		salt[i] = chars[random() % 63];
84920253Sjoerg	salt[i] = '\0';
85020253Sjoerg
85120253Sjoerg	return strcpy(buf, crypt(password, salt));
85220253Sjoerg}
85320253Sjoerg
85420590Sdavidn#if defined(USE_MD5RAND)
85520555Sdavidnu_char *
85620590Sdavidnpw_getrand(u_char *buf, int len)	/* cryptographically secure rng */
85720555Sdavidn{
85820590Sdavidn	int i;
85920590Sdavidn	for (i=0;i<len;i+=16) {
86020590Sdavidn		u_char ubuf[16];
86120590Sdavidn
86220590Sdavidn		MD5_CTX md5_ctx;
86320590Sdavidn		struct timeval tv, tvo;
86420590Sdavidn		struct rusage ru;
86520590Sdavidn		int n=0;
86620590Sdavidn		int t;
86720590Sdavidn
86820590Sdavidn		MD5Init (&md5_ctx);
86920590Sdavidn		t=getpid();
87020590Sdavidn		MD5Update (&md5_ctx, (u_char*)&t, sizeof t);
87120590Sdavidn		t=getppid();
87220590Sdavidn		MD5Update (&md5_ctx, (u_char*)&t, sizeof t);
87320590Sdavidn		gettimeofday (&tvo, NULL);
87420590Sdavidn		do {
87520590Sdavidn			getrusage (RUSAGE_SELF, &ru);
87620590Sdavidn			MD5Update (&md5_ctx, (u_char*)&ru, sizeof ru);
87720590Sdavidn			gettimeofday (&tv, NULL);
87820590Sdavidn			MD5Update (&md5_ctx, (u_char*)&tv, sizeof tv);
87920590Sdavidn		} while (n++<20 || tv.tv_usec-tvo.tv_usec<100*1000);
88020590Sdavidn		MD5Final (ubuf, &md5_ctx);
88120590Sdavidn		memcpy(buf+i, ubuf, MIN(16, len-n));
88220590Sdavidn	}
88320590Sdavidn	return buf;
88420555Sdavidn}
88520253Sjoerg
88620590Sdavidn#else	/* Portable version */
88720590Sdavidn
88820590Sdavidnstatic u_char *
88920590Sdavidnpw_getrand(u_char *buf, int len)
89020590Sdavidn{
89120590Sdavidn	int i;
89220590Sdavidn
89320590Sdavidn	for (i = 0; i < len; i++) {
89424214Sache		unsigned long val = random();
89520590Sdavidn		/* Use all bits in the random value */
89620590Sdavidn		buf[i]=(u_char)((val >> 24) ^ (val >> 16) ^ (val >> 8) ^ val);
89720590Sdavidn	}
89820590Sdavidn	return buf;
89920590Sdavidn}
90020590Sdavidn
90120590Sdavidn#endif
90220590Sdavidn
90320253Sjoergstatic char    *
90420253Sjoergpw_password(struct userconf * cnf, struct cargs * args, char const * user)
90520253Sjoerg{
90620253Sjoerg	int             i, l;
90720253Sjoerg	char            pwbuf[32];
90820555Sdavidn	u_char		rndbuf[sizeof pwbuf];
90920253Sjoerg
91020253Sjoerg	switch (cnf->default_password) {
91120253Sjoerg	case -1:		/* Random password */
91224214Sache		if (!randinit) {
91324214Sache			randinit = 1;
91424214Sache#ifdef __FreeBSD__
91524214Sache			if (srandomdev() < 0)
91624214Sache#endif
91724214Sache				srandom((unsigned long) (time(NULL) ^ getpid()));
91824214Sache		}
91920253Sjoerg		l = (random() % 8 + 8);	/* 8 - 16 chars */
92020555Sdavidn		pw_getrand(rndbuf, l);
92120253Sjoerg		for (i = 0; i < l; i++)
92220555Sdavidn			pwbuf[i] = chars[rndbuf[i] % sizeof(chars)];
92320253Sjoerg		pwbuf[i] = '\0';
92420253Sjoerg
92520253Sjoerg		/*
92620253Sjoerg		 * We give this information back to the user
92720253Sjoerg		 */
92820267Sjoerg		if (getarg(args, 'h') == NULL && getarg(args, 'N') == NULL) {
92920712Sdavidn			if (isatty(1))
93020712Sdavidn				printf("Password for '%s' is: ", user);
93120253Sjoerg			printf("%s\n", pwbuf);
93220253Sjoerg			fflush(stdout);
93320253Sjoerg		}
93420253Sjoerg		break;
93520253Sjoerg
93620253Sjoerg	case -2:		/* No password at all! */
93720253Sjoerg		return "";
93820253Sjoerg
93920253Sjoerg	case 0:		/* No login - default */
94020253Sjoerg	default:
94120253Sjoerg		return "*";
94220253Sjoerg
94320253Sjoerg	case 1:		/* user's name */
94420253Sjoerg		strncpy(pwbuf, user, sizeof pwbuf);
94520253Sjoerg		pwbuf[sizeof pwbuf - 1] = '\0';
94620253Sjoerg		break;
94720253Sjoerg	}
94820253Sjoerg	return pw_pwcrypt(pwbuf);
94920253Sjoerg}
95020253Sjoerg
95120253Sjoerg
95220253Sjoergstatic int
95320253Sjoergprint_user(struct passwd * pwd, int pretty)
95420253Sjoerg{
95520253Sjoerg	if (!pretty) {
95620253Sjoerg		char            buf[_UC_MAXLINE];
95720253Sjoerg
95820253Sjoerg		fmtpwent(buf, pwd);
95920253Sjoerg		fputs(buf, stdout);
96020253Sjoerg	} else {
96120267Sjoerg		int		j;
96220253Sjoerg		char           *p;
96320253Sjoerg		struct group   *grp = getgrgid(pwd->pw_gid);
96420253Sjoerg		char            uname[60] = "User &", office[60] = "[None]",
96520253Sjoerg		                wphone[60] = "[None]", hphone[60] = "[None]";
96620590Sdavidn		char		acexpire[32] = "[None]", pwexpire[32] = "[None]";
96720590Sdavidn		struct tm *    tptr;
96820253Sjoerg
96920253Sjoerg		if ((p = strtok(pwd->pw_gecos, ",")) != NULL) {
97020253Sjoerg			strncpy(uname, p, sizeof uname);
97120253Sjoerg			uname[sizeof uname - 1] = '\0';
97220253Sjoerg			if ((p = strtok(NULL, ",")) != NULL) {
97320253Sjoerg				strncpy(office, p, sizeof office);
97420253Sjoerg				office[sizeof office - 1] = '\0';
97520253Sjoerg				if ((p = strtok(NULL, ",")) != NULL) {
97620253Sjoerg					strncpy(wphone, p, sizeof wphone);
97720253Sjoerg					wphone[sizeof wphone - 1] = '\0';
97820253Sjoerg					if ((p = strtok(NULL, "")) != NULL) {
97920253Sjoerg						strncpy(hphone, p, sizeof hphone);
98020253Sjoerg						hphone[sizeof hphone - 1] = '\0';
98120253Sjoerg					}
98220253Sjoerg				}
98320253Sjoerg			}
98420253Sjoerg		}
98520253Sjoerg		/*
98620253Sjoerg		 * Handle '&' in gecos field
98720253Sjoerg		 */
98820253Sjoerg		if ((p = strchr(uname, '&')) != NULL) {
98920253Sjoerg			int             l = strlen(pwd->pw_name);
99020253Sjoerg			int             m = strlen(p);
99120253Sjoerg
99220253Sjoerg			memmove(p + l, p + 1, m);
99320253Sjoerg			memmove(p, pwd->pw_name, l);
99420253Sjoerg			*p = (char) toupper(*p);
99520253Sjoerg		}
99620590Sdavidn		if (pwd->pw_expire > (time_t)0 && (tptr = localtime(&pwd->pw_expire)) != NULL)
99720590Sdavidn		  strftime(acexpire, sizeof acexpire, "%c", tptr);
99820590Sdavidn		if (pwd->pw_change > (time_t)9 && (tptr = localtime(&pwd->pw_change)) != NULL)
99920590Sdavidn		  strftime(pwexpire, sizeof pwexpire, "%c", tptr);
100022394Sdavidn		printf("Login Name: %-15s   #%-12ld Group: %-15s   #%ld\n"
100120747Sdavidn		       " Full Name: %s\n"
100220747Sdavidn		       "      Home: %-26.26s      Class: %s\n"
100320747Sdavidn		       "     Shell: %-26.26s     Office: %s\n"
100420747Sdavidn		       "Work Phone: %-26.26s Home Phone: %s\n"
100520747Sdavidn		       "Acc Expire: %-26.26s Pwd Expire: %s\n",
100620253Sjoerg		       pwd->pw_name, (long) pwd->pw_uid,
100720253Sjoerg		       grp ? grp->gr_name : "(invalid)", (long) pwd->pw_gid,
100820253Sjoerg		       uname, pwd->pw_dir, pwd->pw_class,
100920590Sdavidn		       pwd->pw_shell, office, wphone, hphone,
101020590Sdavidn		       acexpire, pwexpire);
101120267Sjoerg	        setgrent();
101220267Sjoerg		j = 0;
101320267Sjoerg		while ((grp=getgrent()) != NULL)
101420267Sjoerg		{
101520267Sjoerg			int     i = 0;
101620747Sdavidn			while (grp->gr_mem[i] != NULL)
101720267Sjoerg			{
101820267Sjoerg				if (strcmp(grp->gr_mem[i], pwd->pw_name)==0)
101920267Sjoerg				{
102020747Sdavidn					printf(j++ == 0 ? "    Groups: %s" : ",%s", grp->gr_name);
102120267Sjoerg					break;
102220267Sjoerg				}
102320267Sjoerg				++i;
102420267Sjoerg			}
102520267Sjoerg		}
102620267Sjoerg		endgrent();
102720267Sjoerg		printf("%s\n", j ? "\n" : "");
102820253Sjoerg	}
102920267Sjoerg	return EXIT_SUCCESS;
103020253Sjoerg}
103120253Sjoerg
103220679Sdavidnchar    *
103320679Sdavidnpw_checkname(u_char *name, int gecos)
103420253Sjoerg{
103520253Sjoerg	int             l = 0;
103621052Sdavidn	char const     *notch = gecos ? ":!@" : " ,\t:+&#%$^()!@~*?<>=|\\/\"";
103720253Sjoerg
103820253Sjoerg	while (name[l]) {
103921052Sdavidn		if (strchr(notch, name[l]) != NULL || name[l] < ' ' || name[l] == 127 ||
104021052Sdavidn			(!gecos && l==0 && name[l] == '-') ||	/* leading '-' */
104121052Sdavidn			(!gecos && name[l] & 0x80))	/* 8-bit */
104221052Sdavidn			cmderr(EX_DATAERR, (name[l] >= ' ' && name[l] < 127)
104320325Sjoerg					    ? "invalid character `%c' in field\n"
104421052Sdavidn					    : "invalid character 0x%02x in field\n",
104520325Sjoerg					    name[l]);
104620253Sjoerg		++l;
104720253Sjoerg	}
104822394Sdavidn	if (!gecos && l > LOGNAMESIZE)
104920267Sjoerg		cmderr(EX_DATAERR, "name too long `%s'\n", name);
105020679Sdavidn	return (char *)name;
105120253Sjoerg}
105220253Sjoerg
105320253Sjoerg
105420253Sjoergstatic void
105520253Sjoergrmat(uid_t uid)
105620253Sjoerg{
105720253Sjoerg	DIR            *d = opendir("/var/at/jobs");
105820253Sjoerg
105920253Sjoerg	if (d != NULL) {
106020253Sjoerg		struct dirent  *e;
106120253Sjoerg
106220253Sjoerg		while ((e = readdir(d)) != NULL) {
106320253Sjoerg			struct stat     st;
106420253Sjoerg
106520253Sjoerg			if (strncmp(e->d_name, ".lock", 5) != 0 &&
106620253Sjoerg			    stat(e->d_name, &st) == 0 &&
106720253Sjoerg			    !S_ISDIR(st.st_mode) &&
106820253Sjoerg			    st.st_uid == uid) {
106920253Sjoerg				char            tmp[MAXPATHLEN];
107020253Sjoerg
107120253Sjoerg				sprintf(tmp, "/usr/bin/atrm %s", e->d_name);
107220253Sjoerg				system(tmp);
107320253Sjoerg			}
107420253Sjoerg		}
107520253Sjoerg		closedir(d);
107620253Sjoerg	}
107720253Sjoerg}
107820747Sdavidn
107920747Sdavidnstatic void
108020747Sdavidnrmskey(char const * name)
108120747Sdavidn{
108220747Sdavidn	static const char etcskey[] = "/etc/skeykeys";
108321052Sdavidn	FILE   *fp = fopen(etcskey, "r+");
108420747Sdavidn
108521052Sdavidn	if (fp != NULL) {
108621052Sdavidn		char	tmp[1024];
108721052Sdavidn		off_t	atofs = 0;
108821052Sdavidn		int	length = strlen(name);
108920747Sdavidn
109021052Sdavidn		while (fgets(tmp, sizeof tmp, fp) != NULL) {
109121052Sdavidn			if (strncmp(name, tmp, length) == 0 && tmp[length]==' ') {
109221052Sdavidn				if (fseek(fp, atofs, SEEK_SET) == 0) {
109321052Sdavidn					fwrite("#", 1, 1, fp);	/* Comment username out */
109421052Sdavidn				}
109521052Sdavidn				break;
109620747Sdavidn			}
109721052Sdavidn			atofs = ftell(fp);
109820747Sdavidn		}
109921052Sdavidn		/*
110021052Sdavidn		 * If we got an error of any sort, don't update!
110121052Sdavidn		 */
110221052Sdavidn		fclose(fp);
110320747Sdavidn	}
110420747Sdavidn}
110520747Sdavidn
1106