pw_user.c revision 44229
120253Sjoerg/*-
220302Sjoerg * Copyright (C) 1996
320302Sjoerg *	David L. Nugent.  All rights reserved.
420253Sjoerg *
520253Sjoerg * Redistribution and use in source and binary forms, with or without
620253Sjoerg * modification, are permitted provided that the following conditions
720253Sjoerg * are met:
820253Sjoerg * 1. Redistributions of source code must retain the above copyright
920302Sjoerg *    notice, this list of conditions and the following disclaimer.
1020253Sjoerg * 2. Redistributions in binary form must reproduce the above copyright
1120253Sjoerg *    notice, this list of conditions and the following disclaimer in the
1220253Sjoerg *    documentation and/or other materials provided with the distribution.
1320253Sjoerg *
1420302Sjoerg * THIS SOFTWARE IS PROVIDED BY DAVID L. NUGENT AND CONTRIBUTORS ``AS IS'' AND
1520253Sjoerg * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1620253Sjoerg * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1720302Sjoerg * ARE DISCLAIMED.  IN NO EVENT SHALL DAVID L. NUGENT OR CONTRIBUTORS BE LIABLE
1820253Sjoerg * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1920253Sjoerg * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2020253Sjoerg * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2120253Sjoerg * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2220253Sjoerg * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2320253Sjoerg * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2420253Sjoerg * SUCH DAMAGE.
2544229Sdavidn *
2620253Sjoerg */
2720253Sjoerg
2830259Scharnier#ifndef lint
2930259Scharnierstatic const char rcsid[] =
3044229Sdavidn	"$Id: pw_user.c,v 1.26 1999/02/08 21:26:44 des Exp $";
3130259Scharnier#endif /* not lint */
3230259Scharnier
3330259Scharnier#include <ctype.h>
3430259Scharnier#include <err.h>
3520253Sjoerg#include <fcntl.h>
3620253Sjoerg#include <sys/param.h>
3720253Sjoerg#include <dirent.h>
3830259Scharnier#include <paths.h>
3920253Sjoerg#include <termios.h>
4020555Sdavidn#include <sys/types.h>
4120555Sdavidn#include <sys/time.h>
4220555Sdavidn#include <sys/resource.h>
4330259Scharnier#include <unistd.h>
4422394Sdavidn#include <utmp.h>
4522394Sdavidn#if defined(USE_MD5RAND)
4620555Sdavidn#include <md5.h>
4722394Sdavidn#endif
4820253Sjoerg#include "pw.h"
4920253Sjoerg#include "bitmap.h"
5020253Sjoerg
5123318Sache#if (MAXLOGNAME-1) > UT_NAMESIZE
5222394Sdavidn#define LOGNAMESIZE UT_NAMESIZE
5322394Sdavidn#else
5423318Sache#define LOGNAMESIZE (MAXLOGNAME-1)
5522394Sdavidn#endif
5622394Sdavidn
5724214Sachestatic          randinit;
5824214Sache
5920253Sjoergstatic int      print_user(struct passwd * pwd, int pretty);
6020253Sjoergstatic uid_t    pw_uidpolicy(struct userconf * cnf, struct cargs * args);
6120253Sjoergstatic uid_t    pw_gidpolicy(struct userconf * cnf, struct cargs * args, char *nam, gid_t prefer);
6220253Sjoergstatic time_t   pw_pwdpolicy(struct userconf * cnf, struct cargs * args);
6320253Sjoergstatic time_t   pw_exppolicy(struct userconf * cnf, struct cargs * args);
6420253Sjoergstatic char    *pw_homepolicy(struct userconf * cnf, struct cargs * args, char const * user);
6520253Sjoergstatic char    *pw_shellpolicy(struct userconf * cnf, struct cargs * args, char *newshell);
6620253Sjoergstatic char    *pw_password(struct userconf * cnf, struct cargs * args, char const * user);
6720253Sjoergstatic char    *shell_path(char const * path, char *shells[], char *sh);
6820253Sjoergstatic void     rmat(uid_t uid);
6920747Sdavidnstatic void	rmskey(char const * name);
7020253Sjoerg
7120253Sjoerg/*-
7220253Sjoerg * -C config      configuration file
7320253Sjoerg * -q             quiet operation
7420253Sjoerg * -n name        login name
7520253Sjoerg * -u uid         user id
7620253Sjoerg * -c comment     user name/comment
7720253Sjoerg * -d directory   home directory
7820253Sjoerg * -e date        account expiry date
7920253Sjoerg * -p date        password expiry date
8020253Sjoerg * -g grp         primary group
8120253Sjoerg * -G grp1,grp2   additional groups
8220253Sjoerg * -m [ -k dir ]  create and set up home
8320253Sjoerg * -s shell       name of login shell
8420253Sjoerg * -o             duplicate uid ok
8520253Sjoerg * -L class       user class
8620253Sjoerg * -l name        new login name
8720253Sjoerg * -h fd          password filehandle
8820253Sjoerg * -F             force print or add
8920253Sjoerg *   Setting defaults:
9020253Sjoerg * -D             set user defaults
9120253Sjoerg * -b dir         default home root dir
9220253Sjoerg * -e period      default expiry period
9320253Sjoerg * -p period      default password change period
9420253Sjoerg * -g group       default group
9520253Sjoerg * -G             grp1,grp2.. default additional groups
9620253Sjoerg * -L class       default login class
9720253Sjoerg * -k dir         default home skeleton
9820253Sjoerg * -s shell       default shell
9920253Sjoerg * -w method      default password method
10020253Sjoerg */
10120253Sjoerg
10220253Sjoergint
10320253Sjoergpw_user(struct userconf * cnf, int mode, struct cargs * args)
10420253Sjoerg{
10521330Sdavidn	int	        r, r1;
10620253Sjoerg	char           *p = NULL;
10720253Sjoerg	struct carg    *a_name;
10820253Sjoerg	struct carg    *a_uid;
10920253Sjoerg	struct carg    *arg;
11020253Sjoerg	struct passwd  *pwd = NULL;
11120253Sjoerg	struct group   *grp;
11220253Sjoerg	struct stat     st;
11320747Sdavidn	char            line[_PASSWORD_LEN+1];
11420253Sjoerg
11520253Sjoerg	static struct passwd fakeuser =
11620253Sjoerg	{
11720253Sjoerg		NULL,
11820253Sjoerg		"*",
11920253Sjoerg		-1,
12020253Sjoerg		-1,
12120253Sjoerg		0,
12220253Sjoerg		"",
12320253Sjoerg		"User &",
12420253Sjoerg		"/bin/sh",
12520253Sjoerg		0,
12620253Sjoerg		0
12720253Sjoerg	};
12820253Sjoerg
12920253Sjoerg	/*
13020267Sjoerg	 * With M_NEXT, we only need to return the
13120267Sjoerg	 * next uid to stdout
13220267Sjoerg	 */
13320267Sjoerg	if (mode == M_NEXT)
13420267Sjoerg	{
13520267Sjoerg		uid_t next = pw_uidpolicy(cnf, args);
13620267Sjoerg		if (getarg(args, 'q'))
13720267Sjoerg			return next;
13820267Sjoerg		printf("%ld:", (long)next);
13920267Sjoerg		pw_group(cnf, mode, args);
14020267Sjoerg		return EXIT_SUCCESS;
14120267Sjoerg	}
14220267Sjoerg
14320267Sjoerg	/*
14420253Sjoerg	 * We can do all of the common legwork here
14520253Sjoerg	 */
14620253Sjoerg
14720253Sjoerg	if ((arg = getarg(args, 'b')) != NULL) {
14820267Sjoerg		cnf->home = arg->val;
14920253Sjoerg	}
15021052Sdavidn
15121052Sdavidn	/*
15221052Sdavidn	 * If we'll need to use it or we're updating it,
15321052Sdavidn	 * then create the base home directory if necessary
15421052Sdavidn	 */
15521052Sdavidn	if (arg != NULL || getarg(args, 'm') != NULL) {
15621052Sdavidn		int	l = strlen(cnf->home);
15721052Sdavidn
15821052Sdavidn		if (l > 1 && cnf->home[l-1] == '/')	/* Shave off any trailing path delimiter */
15921052Sdavidn			cnf->home[--l] = '\0';
16021052Sdavidn
16121052Sdavidn		if (l < 2 || *cnf->home != '/')		/* Check for absolute path name */
16230259Scharnier			errx(EX_DATAERR, "invalid base directory for home '%s'", cnf->home);
16321052Sdavidn
16421052Sdavidn		if (stat(cnf->home, &st) == -1) {
16521052Sdavidn			char	dbuf[MAXPATHLEN];
16621052Sdavidn
16721242Sdavidn			/*
16821242Sdavidn			 * This is a kludge especially for Joerg :)
16921242Sdavidn			 * If the home directory would be created in the root partition, then
17021242Sdavidn			 * we really create it under /usr which is likely to have more space.
17121242Sdavidn			 * But we create a symlink from cnf->home -> "/usr" -> cnf->home
17221242Sdavidn			 */
17321242Sdavidn			if (strchr(cnf->home+1, '/') == NULL) {
17421242Sdavidn				strcpy(dbuf, "/usr");
17521242Sdavidn				strncat(dbuf, cnf->home, MAXPATHLEN-5);
17621242Sdavidn				if (mkdir(dbuf, 0755) != -1 || errno == EEXIST) {
17721242Sdavidn					chown(dbuf, 0, 0);
17821242Sdavidn					symlink(dbuf, cnf->home);
17921242Sdavidn				}
18021242Sdavidn				/* If this falls, fall back to old method */
18121242Sdavidn			}
18221052Sdavidn			p = strncpy(dbuf, cnf->home, sizeof dbuf);
18321052Sdavidn			dbuf[MAXPATHLEN-1] = '\0';
18421242Sdavidn			if (stat(dbuf, &st) == -1) {
18521242Sdavidn				while ((p = strchr(++p, '/')) != NULL) {
18621242Sdavidn					*p = '\0';
18721242Sdavidn					if (stat(dbuf, &st) == -1) {
18821242Sdavidn						if (mkdir(dbuf, 0755) == -1)
18921242Sdavidn							goto direrr;
19021242Sdavidn						chown(dbuf, 0, 0);
19121242Sdavidn					} else if (!S_ISDIR(st.st_mode))
19230259Scharnier						errx(EX_OSFILE, "'%s' (root home parent) is not a directory", dbuf);
19321242Sdavidn					*p = '/';
19421242Sdavidn				}
19521052Sdavidn			}
19621242Sdavidn			if (stat(dbuf, &st) == -1) {
19721052Sdavidn				if (mkdir(dbuf, 0755) == -1) {
19830259Scharnier				direrr:	err(EX_OSFILE, "mkdir '%s'", dbuf);
19921052Sdavidn				}
20021052Sdavidn				chown(dbuf, 0, 0);
20121052Sdavidn			}
20221052Sdavidn		} else if (!S_ISDIR(st.st_mode))
20330259Scharnier			errx(EX_OSFILE, "root home `%s' is not a directory", cnf->home);
20421052Sdavidn	}
20521052Sdavidn
20621052Sdavidn
20720253Sjoerg	if ((arg = getarg(args, 'e')) != NULL)
20820253Sjoerg		cnf->expire_days = atoi(arg->val);
20920253Sjoerg
21021330Sdavidn	if ((arg = getarg(args, 'y')) != NULL)
21121330Sdavidn		cnf->nispasswd = arg->val;
21221330Sdavidn
21320253Sjoerg	if ((arg = getarg(args, 'p')) != NULL && arg->val)
21420253Sjoerg		cnf->password_days = atoi(arg->val);
21520253Sjoerg
21620253Sjoerg	if ((arg = getarg(args, 'g')) != NULL) {
21720253Sjoerg		p = arg->val;
21844229Sdavidn		if ((grp = GETGRNAM(p)) == NULL) {
21944229Sdavidn			if (!isdigit(*p) || (grp = GETGRGID((gid_t) atoi(p))) == NULL)
22030259Scharnier				errx(EX_NOUSER, "group `%s' does not exist", p);
22120253Sjoerg		}
22220253Sjoerg		cnf->default_group = newstr(grp->gr_name);
22320253Sjoerg	}
22420253Sjoerg	if ((arg = getarg(args, 'L')) != NULL)
22520679Sdavidn		cnf->default_class = pw_checkname((u_char *)arg->val, 0);
22620253Sjoerg
22720253Sjoerg	if ((arg = getarg(args, 'G')) != NULL && arg->val) {
22820253Sjoerg		int             i = 0;
22920253Sjoerg
23020747Sdavidn		for (p = strtok(arg->val, ", \t"); p != NULL; p = strtok(NULL, ", \t")) {
23144229Sdavidn			if ((grp = GETGRNAM(p)) == NULL) {
23244229Sdavidn				if (!isdigit(*p) || (grp = GETGRGID((gid_t) atoi(p))) == NULL)
23330259Scharnier					errx(EX_NOUSER, "group `%s' does not exist", p);
23420253Sjoerg			}
23520747Sdavidn			if (extendarray(&cnf->groups, &cnf->numgroups, i + 2) != -1)
23620747Sdavidn				cnf->groups[i++] = newstr(grp->gr_name);
23720253Sjoerg		}
23820747Sdavidn		while (i < cnf->numgroups)
23920253Sjoerg			cnf->groups[i++] = NULL;
24020253Sjoerg	}
24120253Sjoerg	if ((arg = getarg(args, 'k')) != NULL) {
24226088Sdavidn		if (stat(cnf->dotdir = arg->val, &st) == -1 || !S_ISDIR(st.st_mode))
24330259Scharnier			errx(EX_OSFILE, "skeleton `%s' is not a directory or does not exist", cnf->dotdir);
24420253Sjoerg	}
24520253Sjoerg	if ((arg = getarg(args, 's')) != NULL)
24620253Sjoerg		cnf->shell_default = arg->val;
24720253Sjoerg
24820253Sjoerg	if (mode == M_ADD && getarg(args, 'D')) {
24920253Sjoerg		if (getarg(args, 'n') != NULL)
25030259Scharnier			errx(EX_DATAERR, "can't combine `-D' with `-n name'");
25120253Sjoerg		if ((arg = getarg(args, 'u')) != NULL && (p = strtok(arg->val, ", \t")) != NULL) {
25220253Sjoerg			if ((cnf->min_uid = (uid_t) atoi(p)) == 0)
25320253Sjoerg				cnf->min_uid = 1000;
25420253Sjoerg			if ((p = strtok(NULL, " ,\t")) == NULL || (cnf->max_uid = (uid_t) atoi(p)) < cnf->min_uid)
25520253Sjoerg				cnf->max_uid = 32000;
25620253Sjoerg		}
25720253Sjoerg		if ((arg = getarg(args, 'i')) != NULL && (p = strtok(arg->val, ", \t")) != NULL) {
25820253Sjoerg			if ((cnf->min_gid = (gid_t) atoi(p)) == 0)
25920253Sjoerg				cnf->min_gid = 1000;
26020253Sjoerg			if ((p = strtok(NULL, " ,\t")) == NULL || (cnf->max_gid = (gid_t) atoi(p)) < cnf->min_gid)
26120253Sjoerg				cnf->max_gid = 32000;
26220253Sjoerg		}
26320253Sjoerg		if ((arg = getarg(args, 'w')) != NULL)
26420253Sjoerg			cnf->default_password = boolean_val(arg->val, cnf->default_password);
26520253Sjoerg
26620253Sjoerg		arg = getarg(args, 'C');
26720253Sjoerg		if (write_userconfig(arg ? arg->val : NULL))
26820267Sjoerg			return EXIT_SUCCESS;
26930259Scharnier		warn("config update");
27020267Sjoerg		return EX_IOERR;
27120253Sjoerg	}
27220253Sjoerg	if (mode == M_PRINT && getarg(args, 'a')) {
27320267Sjoerg		int             pretty = getarg(args, 'P') != NULL;
27420253Sjoerg
27544229Sdavidn		SETPWENT();
27644229Sdavidn		while ((pwd = GETPWENT()) != NULL)
27720253Sjoerg			print_user(pwd, pretty);
27844229Sdavidn		ENDPWENT();
27920267Sjoerg		return EXIT_SUCCESS;
28020253Sjoerg	}
28120253Sjoerg	if ((a_name = getarg(args, 'n')) != NULL)
28244229Sdavidn		pwd = GETPWNAM(pw_checkname((u_char *)a_name->val, 0));
28320253Sjoerg	a_uid = getarg(args, 'u');
28420253Sjoerg
28520253Sjoerg	if (a_uid == NULL) {
28620253Sjoerg		if (a_name == NULL)
28730259Scharnier			errx(EX_DATAERR, "user name or id required");
28820253Sjoerg
28920253Sjoerg		/*
29020253Sjoerg		 * Determine whether 'n' switch is name or uid - we don't
29120253Sjoerg		 * really don't really care which we have, but we need to
29220253Sjoerg		 * know.
29320253Sjoerg		 */
29443780Sdes		if (mode != M_ADD && pwd == NULL
29543780Sdes		    && strspn(a_name->val, "0123456789") == strlen(a_name->val)
29643780Sdes		    && atoi(a_name->val) > 0) {	/* Assume uid */
29720253Sjoerg			(a_uid = a_name)->ch = 'u';
29820253Sjoerg			a_name = NULL;
29920253Sjoerg		}
30020253Sjoerg	}
30120253Sjoerg	/*
30220253Sjoerg	 * Update, delete & print require that the user exists
30320253Sjoerg	 */
30420253Sjoerg	if (mode == M_UPDATE || mode == M_DELETE || mode == M_PRINT) {
30520253Sjoerg		if (a_name == NULL && pwd == NULL)	/* Try harder */
30644229Sdavidn			pwd = GETPWUID(atoi(a_uid->val));
30720253Sjoerg
30820253Sjoerg		if (pwd == NULL) {
30920253Sjoerg			if (mode == M_PRINT && getarg(args, 'F')) {
31020253Sjoerg				fakeuser.pw_name = a_name ? a_name->val : "nouser";
31120253Sjoerg				fakeuser.pw_uid = a_uid ? (uid_t) atol(a_uid->val) : -1;
31220267Sjoerg				return print_user(&fakeuser, getarg(args, 'P') != NULL);
31320253Sjoerg			}
31420253Sjoerg			if (a_name == NULL)
31530259Scharnier				errx(EX_NOUSER, "no such uid `%s'", a_uid->val);
31630259Scharnier			errx(EX_NOUSER, "no such user `%s'", a_name->val);
31720253Sjoerg		}
31820253Sjoerg		if (a_name == NULL)	/* May be needed later */
31920253Sjoerg			a_name = addarg(args, 'n', newstr(pwd->pw_name));
32020253Sjoerg
32120253Sjoerg		/*
32220253Sjoerg		 * Handle deletions now
32320253Sjoerg		 */
32420253Sjoerg		if (mode == M_DELETE) {
32520253Sjoerg			char            file[MAXPATHLEN];
32620253Sjoerg			char            home[MAXPATHLEN];
32720253Sjoerg			uid_t           uid = pwd->pw_uid;
32820253Sjoerg
32920253Sjoerg			if (strcmp(pwd->pw_name, "root") == 0)
33030259Scharnier				errx(EX_DATAERR, "cannot remove user 'root'");
33120253Sjoerg
33244229Sdavidn			if (!PWALTDIR()) {
33344229Sdavidn				/*
33444229Sdavidn				 * Remove skey record from /etc/skeykeys
33544229Sdavidn		        	 */
33620747Sdavidn
33744229Sdavidn				rmskey(pwd->pw_name);
33820747Sdavidn
33944229Sdavidn				/*
34044229Sdavidn				 * Remove crontabs
34144229Sdavidn				 */
34244229Sdavidn				sprintf(file, "/var/cron/tabs/%s", pwd->pw_name);
34344229Sdavidn				if (access(file, F_OK) == 0) {
34444229Sdavidn					sprintf(file, "crontab -u %s -r", pwd->pw_name);
34544229Sdavidn					system(file);
34644229Sdavidn				}
34720253Sjoerg			}
34820253Sjoerg			/*
34920253Sjoerg			 * Save these for later, since contents of pwd may be
35020253Sjoerg			 * invalidated by deletion
35120253Sjoerg			 */
35220253Sjoerg			sprintf(file, "%s/%s", _PATH_MAILDIR, pwd->pw_name);
35320253Sjoerg			strncpy(home, pwd->pw_dir, sizeof home);
35420253Sjoerg			home[sizeof home - 1] = '\0';
35520253Sjoerg
35620253Sjoerg			if (!delpwent(pwd))
35730259Scharnier				err(EX_IOERR, "error updating passwd file");
35821330Sdavidn
35921330Sdavidn			if (cnf->nispasswd && *cnf->nispasswd=='/' && !delnispwent(cnf->nispasswd, a_name->val))
36030259Scharnier				warn("WARNING: NIS passwd update");
36121330Sdavidn
36220253Sjoerg			editgroups(a_name->val, NULL);
36320253Sjoerg
36420253Sjoerg			pw_log(cnf, mode, W_USER, "%s(%ld) account removed", a_name->val, (long) uid);
36520253Sjoerg
36644229Sdavidn			if (!PWALTDIR()) {
36744229Sdavidn				/*
36844229Sdavidn				 * Remove mail file
36944229Sdavidn				 */
37044229Sdavidn				remove(file);
37120253Sjoerg
37244229Sdavidn				/*
37344229Sdavidn				 * Remove at jobs
37444229Sdavidn				 */
37544229Sdavidn				if (getpwuid(uid) == NULL)
37644229Sdavidn					rmat(uid);
37720253Sjoerg
37844229Sdavidn				/*
37944229Sdavidn				 * Remove home directory and contents
38044229Sdavidn				 */
38144229Sdavidn				if (getarg(args, 'r') != NULL && *home == '/' && getpwuid(uid) == NULL) {
38244229Sdavidn					if (stat(home, &st) != -1) {
38344229Sdavidn						rm_r(home, uid);
38444229Sdavidn						pw_log(cnf, mode, W_USER, "%s(%ld) home '%s' %sremoved",
38544229Sdavidn						       a_name->val, (long) uid, home,
38644229Sdavidn						       stat(home, &st) == -1 ? "" : "not completely ");
38744229Sdavidn					}
38820253Sjoerg				}
38920253Sjoerg			}
39020267Sjoerg			return EXIT_SUCCESS;
39120253Sjoerg		} else if (mode == M_PRINT)
39220267Sjoerg			return print_user(pwd, getarg(args, 'P') != NULL);
39320253Sjoerg
39420253Sjoerg		/*
39520253Sjoerg		 * The rest is edit code
39620253Sjoerg		 */
39720253Sjoerg		if ((arg = getarg(args, 'l')) != NULL) {
39820253Sjoerg			if (strcmp(pwd->pw_name, "root") == 0)
39930259Scharnier				errx(EX_DATAERR, "can't rename `root' account");
40020679Sdavidn			pwd->pw_name = pw_checkname((u_char *)arg->val, 0);
40120253Sjoerg		}
40220253Sjoerg		if ((arg = getarg(args, 'u')) != NULL && isdigit(*arg->val)) {
40320253Sjoerg			pwd->pw_uid = (uid_t) atol(arg->val);
40420253Sjoerg			if (pwd->pw_uid != 0 && strcmp(pwd->pw_name, "root") == 0)
40530259Scharnier				errx(EX_DATAERR, "can't change uid of `root' account");
40620253Sjoerg			if (pwd->pw_uid == 0 && strcmp(pwd->pw_name, "root") != 0)
40730259Scharnier				warnx("WARNING: account `%s' will have a uid of 0 (superuser access!)", pwd->pw_name);
40820253Sjoerg		}
40920253Sjoerg		if ((arg = getarg(args, 'g')) != NULL && pwd->pw_uid != 0)	/* Already checked this */
41044229Sdavidn			pwd->pw_gid = (gid_t) GETGRNAM(cnf->default_group)->gr_gid;
41120253Sjoerg
41220253Sjoerg		if ((arg = getarg(args, 'p')) != NULL) {
41320253Sjoerg			if (*arg->val == '\0' || strcmp(arg->val, "0") == 0)
41420253Sjoerg				pwd->pw_change = 0;
41520253Sjoerg			else {
41620253Sjoerg				time_t          now = time(NULL);
41720253Sjoerg				time_t          expire = parse_date(now, arg->val);
41820253Sjoerg
41920253Sjoerg				if (now == expire)
42030259Scharnier					errx(EX_DATAERR, "invalid password change date `%s'", arg->val);
42120253Sjoerg				pwd->pw_change = expire;
42220253Sjoerg			}
42320253Sjoerg		}
42420267Sjoerg		if ((arg = getarg(args, 'e')) != NULL) {
42520253Sjoerg			if (*arg->val == '\0' || strcmp(arg->val, "0") == 0)
42620253Sjoerg				pwd->pw_expire = 0;
42720253Sjoerg			else {
42820253Sjoerg				time_t          now = time(NULL);
42920253Sjoerg				time_t          expire = parse_date(now, arg->val);
43020253Sjoerg
43120253Sjoerg				if (now == expire)
43230259Scharnier					errx(EX_DATAERR, "invalid account expiry date `%s'", arg->val);
43320253Sjoerg				pwd->pw_expire = expire;
43420253Sjoerg			}
43520253Sjoerg		}
43620253Sjoerg		if ((arg = getarg(args, 's')) != NULL)
43720253Sjoerg			pwd->pw_shell = shell_path(cnf->shelldir, cnf->shells, arg->val);
43820253Sjoerg
43920253Sjoerg		if (getarg(args, 'L'))
44020253Sjoerg			pwd->pw_class = cnf->default_class;
44120253Sjoerg
44220747Sdavidn		if ((arg  = getarg(args, 'd')) != NULL) {
44320747Sdavidn			if (stat(pwd->pw_dir = arg->val, &st) == -1) {
44420747Sdavidn				if (getarg(args, 'm') == NULL && strcmp(pwd->pw_dir, "/nonexistent") != 0)
44530259Scharnier				  warnx("WARNING: home `%s' does not exist", pwd->pw_dir);
44620747Sdavidn			} else if (!S_ISDIR(st.st_mode))
44730259Scharnier				warnx("WARNING: home `%s' is not a directory", pwd->pw_dir);
44820747Sdavidn		}
44920747Sdavidn
45020267Sjoerg		if ((arg = getarg(args, 'w')) != NULL && getarg(args, 'h') == NULL)
45120267Sjoerg			pwd->pw_passwd = pw_password(cnf, args, pwd->pw_name);
45220267Sjoerg
45320253Sjoerg	} else {
45420253Sjoerg		if (a_name == NULL)	/* Required */
45530259Scharnier			errx(EX_DATAERR, "login name required");
45644229Sdavidn		else if ((pwd = GETPWNAM(a_name->val)) != NULL)	/* Exists */
45730259Scharnier			errx(EX_DATAERR, "login name `%s' already exists", a_name->val);
45820253Sjoerg
45920253Sjoerg		/*
46020253Sjoerg		 * Now, set up defaults for a new user
46120253Sjoerg		 */
46220253Sjoerg		pwd = &fakeuser;
46320253Sjoerg		pwd->pw_name = a_name->val;
46420253Sjoerg		pwd->pw_class = cnf->default_class ? cnf->default_class : "";
46520253Sjoerg		pwd->pw_passwd = pw_password(cnf, args, pwd->pw_name);
46620253Sjoerg		pwd->pw_uid = pw_uidpolicy(cnf, args);
46720253Sjoerg		pwd->pw_gid = pw_gidpolicy(cnf, args, pwd->pw_name, (gid_t) pwd->pw_uid);
46820253Sjoerg		pwd->pw_change = pw_pwdpolicy(cnf, args);
46920253Sjoerg		pwd->pw_expire = pw_exppolicy(cnf, args);
47020253Sjoerg		pwd->pw_dir = pw_homepolicy(cnf, args, pwd->pw_name);
47120253Sjoerg		pwd->pw_shell = pw_shellpolicy(cnf, args, NULL);
47220253Sjoerg
47320253Sjoerg		if (pwd->pw_uid == 0 && strcmp(pwd->pw_name, "root") != 0)
47430259Scharnier			warnx("WARNING: new account `%s' has a uid of 0 (superuser access!)", pwd->pw_name);
47520253Sjoerg	}
47620253Sjoerg
47720253Sjoerg	/*
47820253Sjoerg	 * Shared add/edit code
47920253Sjoerg	 */
48020253Sjoerg	if ((arg = getarg(args, 'c')) != NULL)
48120679Sdavidn		pwd->pw_gecos = pw_checkname((u_char *)arg->val, 1);
48220253Sjoerg
48320253Sjoerg	if ((arg = getarg(args, 'h')) != NULL) {
48420253Sjoerg		if (strcmp(arg->val, "-") == 0)
48520253Sjoerg			pwd->pw_passwd = "*";	/* No access */
48620253Sjoerg		else {
48720253Sjoerg			int             fd = atoi(arg->val);
48820253Sjoerg			int             b;
48920253Sjoerg			int             istty = isatty(fd);
49020253Sjoerg			struct termios  t;
49120253Sjoerg
49220253Sjoerg			if (istty) {
49320253Sjoerg				if (tcgetattr(fd, &t) == -1)
49420253Sjoerg					istty = 0;
49520253Sjoerg				else {
49620253Sjoerg					struct termios  n = t;
49720253Sjoerg
49820253Sjoerg					/* Disable echo */
49920253Sjoerg					n.c_lflag &= ~(ECHO);
50020253Sjoerg					tcsetattr(fd, TCSANOW, &n);
50120253Sjoerg					printf("%sassword for user %s:", (mode == M_UPDATE) ? "New p" : "P", pwd->pw_name);
50220253Sjoerg					fflush(stdout);
50320253Sjoerg				}
50420253Sjoerg			}
50520253Sjoerg			b = read(fd, line, sizeof(line) - 1);
50620253Sjoerg			if (istty) {	/* Restore state */
50720253Sjoerg				tcsetattr(fd, TCSANOW, &t);
50820253Sjoerg				fputc('\n', stdout);
50920253Sjoerg				fflush(stdout);
51020253Sjoerg			}
51120253Sjoerg			if (b < 0) {
51230259Scharnier				warn("-h file descriptor");
51320267Sjoerg				return EX_IOERR;
51420253Sjoerg			}
51520253Sjoerg			line[b] = '\0';
51620253Sjoerg			if ((p = strpbrk(line, " \t\r\n")) != NULL)
51720253Sjoerg				*p = '\0';
51820253Sjoerg			if (!*line)
51930259Scharnier				errx(EX_DATAERR, "empty password read on file descriptor %d", fd);
52020253Sjoerg			pwd->pw_passwd = pw_pwcrypt(line);
52120253Sjoerg		}
52220253Sjoerg	}
52320267Sjoerg
52420267Sjoerg	/*
52520267Sjoerg	 * Special case: -N only displays & exits
52620267Sjoerg	 */
52720267Sjoerg	if (getarg(args, 'N') != NULL)
52820267Sjoerg		return print_user(pwd, getarg(args, 'P') != NULL);
52920267Sjoerg
53021330Sdavidn	r = r1 = 1;
53121330Sdavidn	if (mode == M_ADD) {
53221330Sdavidn		r = addpwent(pwd);
53321330Sdavidn		if (r && cnf->nispasswd && *cnf->nispasswd=='/')
53421330Sdavidn			r1 = addnispwent(cnf->nispasswd, pwd);
53521330Sdavidn	} else if (mode == M_UPDATE) {
53621330Sdavidn		r = chgpwent(a_name->val, pwd);
53721330Sdavidn		if (r && cnf->nispasswd && *cnf->nispasswd=='/')
53821330Sdavidn			r1 = chgnispwent(cnf->nispasswd, a_name->val, pwd);
53921330Sdavidn	}
54021330Sdavidn
54121330Sdavidn	if (!r) {
54230259Scharnier		warn("password update");
54320267Sjoerg		return EX_IOERR;
54421330Sdavidn	} else if (!r1) {
54530259Scharnier		warn("WARNING: NIS password update");
54621330Sdavidn		/* Keep on trucking */
54720253Sjoerg	}
54821330Sdavidn
54920253Sjoerg	/*
55020253Sjoerg	 * Ok, user is created or changed - now edit group file
55120253Sjoerg	 */
55220253Sjoerg
55320253Sjoerg	if (mode == M_ADD || getarg(args, 'G') != NULL)
55420253Sjoerg		editgroups(pwd->pw_name, cnf->groups);
55520253Sjoerg
55620253Sjoerg	/* pwd may have been invalidated */
55744229Sdavidn	if ((pwd = GETPWNAM(a_name->val)) == NULL)
55830259Scharnier		errx(EX_NOUSER, "user '%s' disappeared during update", a_name->val);
55920253Sjoerg
56044229Sdavidn	grp = GETGRGID(pwd->pw_gid);
56120253Sjoerg	pw_log(cnf, mode, W_USER, "%s(%ld):%s(%d):%s:%s:%s",
56220253Sjoerg	       pwd->pw_name, (long) pwd->pw_uid,
56320253Sjoerg	    grp ? grp->gr_name : "unknown", (long) (grp ? grp->gr_gid : -1),
56420253Sjoerg	       pwd->pw_gecos, pwd->pw_dir, pwd->pw_shell);
56520253Sjoerg
56620253Sjoerg	/*
56720253Sjoerg	 * If adding, let's touch and chown the user's mail file. This is not
56820253Sjoerg	 * strictly necessary under BSD with a 0755 maildir but it also
56920253Sjoerg	 * doesn't hurt anything to create the empty mailfile
57020253Sjoerg	 */
57120253Sjoerg	if (mode == M_ADD) {
57220253Sjoerg		FILE           *fp;
57320253Sjoerg
57444229Sdavidn		if (!PWALTDIR()) {
57544229Sdavidn			sprintf(line, "%s/%s", _PATH_MAILDIR, pwd->pw_name);
57644229Sdavidn			close(open(line, O_RDWR | O_CREAT, 0600));	/* Preserve contents &
57744229Sdavidn									 * mtime */
57844229Sdavidn			chown(line, pwd->pw_uid, pwd->pw_gid);
57920253Sjoerg
58044229Sdavidn			/*
58144229Sdavidn			 * Send mail to the new user as well, if we are asked to
58244229Sdavidn			 */
58344229Sdavidn			if (cnf->newmail && *cnf->newmail && (fp = fopen(cnf->newmail, "r")) != NULL) {
58444229Sdavidn				FILE           *pfp = popen(_PATH_SENDMAIL " -t", "w");
58520253Sjoerg
58644229Sdavidn				if (pfp == NULL)
58744229Sdavidn					warn("sendmail");
58844229Sdavidn				else {
58944229Sdavidn					fprintf(pfp, "From: root\n" "To: %s\n" "Subject: Welcome!\n\n", pwd->pw_name);
59044229Sdavidn					while (fgets(line, sizeof(line), fp) != NULL) {
59144229Sdavidn						/* Do substitutions? */
59244229Sdavidn						fputs(line, pfp);
59344229Sdavidn					}
59444229Sdavidn					pclose(pfp);
59544229Sdavidn					pw_log(cnf, mode, W_USER, "%s(%ld) new user mail sent",
59644229Sdavidn					       pwd->pw_name, (long) pwd->pw_uid);
59720253Sjoerg				}
59844229Sdavidn				fclose(fp);
59920253Sjoerg			}
60020253Sjoerg		}
60120253Sjoerg	}
60220253Sjoerg	/*
60320253Sjoerg	 * Finally, let's create and populate the user's home directory. Note
60420253Sjoerg	 * that this also `works' for editing users if -m is used, but
60520253Sjoerg	 * existing files will *not* be overwritten.
60620253Sjoerg	 */
60744229Sdavidn	if (!PWALTDIR() && getarg(args, 'm') != NULL && pwd->pw_dir && *pwd->pw_dir == '/' && pwd->pw_dir[1]) {
60820253Sjoerg		copymkdir(pwd->pw_dir, cnf->dotdir, 0755, pwd->pw_uid, pwd->pw_gid);
60920253Sjoerg		pw_log(cnf, mode, W_USER, "%s(%ld) home %s made",
61020253Sjoerg		       pwd->pw_name, (long) pwd->pw_uid, pwd->pw_dir);
61120253Sjoerg	}
61220267Sjoerg	return EXIT_SUCCESS;
61320253Sjoerg}
61420253Sjoerg
61520253Sjoerg
61620253Sjoergstatic          uid_t
61720253Sjoergpw_uidpolicy(struct userconf * cnf, struct cargs * args)
61820253Sjoerg{
61920253Sjoerg	struct passwd  *pwd;
62020253Sjoerg	uid_t           uid = (uid_t) - 1;
62120253Sjoerg	struct carg    *a_uid = getarg(args, 'u');
62220253Sjoerg
62320253Sjoerg	/*
62420253Sjoerg	 * Check the given uid, if any
62520253Sjoerg	 */
62620253Sjoerg	if (a_uid != NULL) {
62720253Sjoerg		uid = (uid_t) atol(a_uid->val);
62820253Sjoerg
62944229Sdavidn		if ((pwd = GETPWUID(uid)) != NULL && getarg(args, 'o') == NULL)
63030259Scharnier			errx(EX_DATAERR, "uid `%ld' has already been allocated", (long) pwd->pw_uid);
63120253Sjoerg	} else {
63220253Sjoerg		struct bitmap   bm;
63320253Sjoerg
63420253Sjoerg		/*
63520253Sjoerg		 * We need to allocate the next available uid under one of
63620253Sjoerg		 * two policies a) Grab the first unused uid b) Grab the
63720253Sjoerg		 * highest possible unused uid
63820253Sjoerg		 */
63920253Sjoerg		if (cnf->min_uid >= cnf->max_uid) {	/* Sanity
64020253Sjoerg							 * claus^H^H^H^Hheck */
64120253Sjoerg			cnf->min_uid = 1000;
64220253Sjoerg			cnf->max_uid = 32000;
64320253Sjoerg		}
64420253Sjoerg		bm = bm_alloc(cnf->max_uid - cnf->min_uid + 1);
64520253Sjoerg
64620253Sjoerg		/*
64720253Sjoerg		 * Now, let's fill the bitmap from the password file
64820253Sjoerg		 */
64944229Sdavidn		SETPWENT();
65044229Sdavidn		while ((pwd = GETPWENT()) != NULL)
65144229Sdavidn			if (pwd->pw_uid >= (uid_t) cnf->min_uid && pwd->pw_uid <= (uid_t) cnf->max_uid)
65220253Sjoerg				bm_setbit(&bm, pwd->pw_uid - cnf->min_uid);
65344229Sdavidn		ENDPWENT();
65420253Sjoerg
65520253Sjoerg		/*
65620253Sjoerg		 * Then apply the policy, with fallback to reuse if necessary
65720253Sjoerg		 */
65820253Sjoerg		if (cnf->reuse_uids || (uid = (uid_t) (bm_lastset(&bm) + cnf->min_uid + 1)) > cnf->max_uid)
65920253Sjoerg			uid = (uid_t) (bm_firstunset(&bm) + cnf->min_uid);
66020253Sjoerg
66120253Sjoerg		/*
66220253Sjoerg		 * Another sanity check
66320253Sjoerg		 */
66420253Sjoerg		if (uid < cnf->min_uid || uid > cnf->max_uid)
66530259Scharnier			errx(EX_SOFTWARE, "unable to allocate a new uid - range fully used");
66620253Sjoerg		bm_dealloc(&bm);
66720253Sjoerg	}
66820253Sjoerg	return uid;
66920253Sjoerg}
67020253Sjoerg
67120253Sjoerg
67220253Sjoergstatic          uid_t
67320253Sjoergpw_gidpolicy(struct userconf * cnf, struct cargs * args, char *nam, gid_t prefer)
67420253Sjoerg{
67520253Sjoerg	struct group   *grp;
67620253Sjoerg	gid_t           gid = (uid_t) - 1;
67720253Sjoerg	struct carg    *a_gid = getarg(args, 'g');
67820253Sjoerg
67920253Sjoerg	/*
68020253Sjoerg	 * If no arg given, see if default can help out
68120253Sjoerg	 */
68220253Sjoerg	if (a_gid == NULL && cnf->default_group && *cnf->default_group)
68320253Sjoerg		a_gid = addarg(args, 'g', cnf->default_group);
68420253Sjoerg
68520253Sjoerg	/*
68620253Sjoerg	 * Check the given gid, if any
68720253Sjoerg	 */
68844229Sdavidn	SETGRENT();
68920253Sjoerg	if (a_gid != NULL) {
69044229Sdavidn		if ((grp = GETGRNAM(a_gid->val)) == NULL) {
69120253Sjoerg			gid = (gid_t) atol(a_gid->val);
69244229Sdavidn			if ((gid == 0 && !isdigit(*a_gid->val)) || (grp = GETGRGID(gid)) == NULL)
69330259Scharnier				errx(EX_NOUSER, "group `%s' is not defined", a_gid->val);
69420253Sjoerg		}
69520253Sjoerg		gid = grp->gr_gid;
69644229Sdavidn	} else if ((grp = GETGRNAM(nam)) != NULL && grp->gr_mem[0] == NULL) {
69720267Sjoerg		gid = grp->gr_gid;  /* Already created? Use it anyway... */
69820253Sjoerg	} else {
69920253Sjoerg		struct cargs    grpargs;
70020253Sjoerg		char            tmp[32];
70120253Sjoerg
70220253Sjoerg		LIST_INIT(&grpargs);
70320253Sjoerg		addarg(&grpargs, 'n', nam);
70420253Sjoerg
70520253Sjoerg		/*
70620253Sjoerg		 * We need to auto-create a group with the user's name. We
70720253Sjoerg		 * can send all the appropriate output to our sister routine
70820253Sjoerg		 * bit first see if we can create a group with gid==uid so we
70920253Sjoerg		 * can keep the user and group ids in sync. We purposely do
71020253Sjoerg		 * NOT check the gid range if we can force the sync. If the
71120253Sjoerg		 * user's name dups an existing group, then the group add
71220253Sjoerg		 * function will happily handle that case for us and exit.
71320253Sjoerg		 */
71444229Sdavidn		if (GETGRGID(prefer) == NULL) {
71520253Sjoerg			sprintf(tmp, "%lu", (unsigned long) prefer);
71620253Sjoerg			addarg(&grpargs, 'g', tmp);
71720253Sjoerg		}
71820267Sjoerg		if (getarg(args, 'N'))
71920267Sjoerg		{
72020267Sjoerg			addarg(&grpargs, 'N', NULL);
72120267Sjoerg			addarg(&grpargs, 'q', NULL);
72220267Sjoerg			gid = pw_group(cnf, M_NEXT, &grpargs);
72320267Sjoerg		}
72420267Sjoerg		else
72520267Sjoerg		{
72620267Sjoerg			pw_group(cnf, M_ADD, &grpargs);
72744229Sdavidn			if ((grp = GETGRNAM(nam)) != NULL)
72820267Sjoerg				gid = grp->gr_gid;
72920267Sjoerg		}
73020253Sjoerg		a_gid = grpargs.lh_first;
73120253Sjoerg		while (a_gid != NULL) {
73220253Sjoerg			struct carg    *t = a_gid->list.le_next;
73320253Sjoerg			LIST_REMOVE(a_gid, list);
73420253Sjoerg			a_gid = t;
73520253Sjoerg		}
73620253Sjoerg	}
73744229Sdavidn	ENDGRENT();
73820253Sjoerg	return gid;
73920253Sjoerg}
74020253Sjoerg
74120253Sjoerg
74220253Sjoergstatic          time_t
74320253Sjoergpw_pwdpolicy(struct userconf * cnf, struct cargs * args)
74420253Sjoerg{
74520253Sjoerg	time_t          result = 0;
74620253Sjoerg	time_t          now = time(NULL);
74727831Sdavidn	struct carg    *arg = getarg(args, 'p');
74820253Sjoerg
74920253Sjoerg	if (arg != NULL) {
75020253Sjoerg		if ((result = parse_date(now, arg->val)) == now)
75130259Scharnier			errx(EX_DATAERR, "invalid date/time `%s'", arg->val);
75220253Sjoerg	} else if (cnf->password_days > 0)
75320253Sjoerg		result = now + ((long) cnf->password_days * 86400L);
75420253Sjoerg	return result;
75520253Sjoerg}
75620253Sjoerg
75720253Sjoerg
75820253Sjoergstatic          time_t
75920253Sjoergpw_exppolicy(struct userconf * cnf, struct cargs * args)
76020253Sjoerg{
76120253Sjoerg	time_t          result = 0;
76220253Sjoerg	time_t          now = time(NULL);
76320253Sjoerg	struct carg    *arg = getarg(args, 'e');
76420253Sjoerg
76520253Sjoerg	if (arg != NULL) {
76620253Sjoerg		if ((result = parse_date(now, arg->val)) == now)
76730259Scharnier			errx(EX_DATAERR, "invalid date/time `%s'", arg->val);
76820253Sjoerg	} else if (cnf->expire_days > 0)
76920253Sjoerg		result = now + ((long) cnf->expire_days * 86400L);
77020253Sjoerg	return result;
77120253Sjoerg}
77220253Sjoerg
77320253Sjoerg
77420253Sjoergstatic char    *
77520253Sjoergpw_homepolicy(struct userconf * cnf, struct cargs * args, char const * user)
77620253Sjoerg{
77720253Sjoerg	struct carg    *arg = getarg(args, 'd');
77820253Sjoerg
77920253Sjoerg	if (arg)
78020253Sjoerg		return arg->val;
78120253Sjoerg	else {
78220253Sjoerg		static char     home[128];
78320253Sjoerg
78420253Sjoerg		if (cnf->home == NULL || *cnf->home == '\0')
78530259Scharnier			errx(EX_CONFIG, "no base home directory set");
78620253Sjoerg		sprintf(home, "%s/%s", cnf->home, user);
78720253Sjoerg		return home;
78820253Sjoerg	}
78920253Sjoerg}
79020253Sjoerg
79120253Sjoergstatic char    *
79220253Sjoergshell_path(char const * path, char *shells[], char *sh)
79320253Sjoerg{
79420253Sjoerg	if (sh != NULL && (*sh == '/' || *sh == '\0'))
79520253Sjoerg		return sh;	/* specified full path or forced none */
79620253Sjoerg	else {
79720253Sjoerg		char           *p;
79820253Sjoerg		char            paths[_UC_MAXLINE];
79920253Sjoerg
80020253Sjoerg		/*
80120253Sjoerg		 * We need to search paths
80220253Sjoerg		 */
80320253Sjoerg		strncpy(paths, path, sizeof paths);
80420253Sjoerg		paths[sizeof paths - 1] = '\0';
80520253Sjoerg		for (p = strtok(paths, ": \t\r\n"); p != NULL; p = strtok(NULL, ": \t\r\n")) {
80620253Sjoerg			int             i;
80720253Sjoerg			static char     shellpath[256];
80820253Sjoerg
80920253Sjoerg			if (sh != NULL) {
81020253Sjoerg				sprintf(shellpath, "%s/%s", p, sh);
81120253Sjoerg				if (access(shellpath, X_OK) == 0)
81220253Sjoerg					return shellpath;
81320253Sjoerg			} else
81420253Sjoerg				for (i = 0; i < _UC_MAXSHELLS && shells[i] != NULL; i++) {
81520253Sjoerg					sprintf(shellpath, "%s/%s", p, shells[i]);
81620253Sjoerg					if (access(shellpath, X_OK) == 0)
81720253Sjoerg						return shellpath;
81820253Sjoerg				}
81920253Sjoerg		}
82020253Sjoerg		if (sh == NULL)
82130259Scharnier			errx(EX_OSFILE, "can't find shell `%s' in shell paths", sh);
82230259Scharnier		errx(EX_CONFIG, "no default shell available or defined");
82320253Sjoerg		return NULL;
82420253Sjoerg	}
82520253Sjoerg}
82620253Sjoerg
82720253Sjoerg
82820253Sjoergstatic char    *
82920253Sjoergpw_shellpolicy(struct userconf * cnf, struct cargs * args, char *newshell)
83020253Sjoerg{
83120253Sjoerg	char           *sh = newshell;
83220253Sjoerg	struct carg    *arg = getarg(args, 's');
83320253Sjoerg
83420253Sjoerg	if (newshell == NULL && arg != NULL)
83520253Sjoerg		sh = arg->val;
83620253Sjoerg	return shell_path(cnf->shelldir, cnf->shells, sh ? sh : cnf->shell_default);
83720253Sjoerg}
83820253Sjoerg
83920253Sjoergstatic char const chars[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.";
84020253Sjoerg
84120253Sjoergchar           *
84220253Sjoergpw_pwcrypt(char *password)
84320253Sjoerg{
84420253Sjoerg	int             i;
84520253Sjoerg	char            salt[12];
84620253Sjoerg
84720253Sjoerg	static char     buf[256];
84820253Sjoerg
84920253Sjoerg	/*
85020253Sjoerg	 * Calculate a salt value
85120253Sjoerg	 */
85224214Sache	if (!randinit) {
85324214Sache		randinit = 1;
85424214Sache#ifdef __FreeBSD__
85526626Sache		srandomdev();
85626626Sache#else
85726626Sache		srandom((unsigned long) (time(NULL) ^ getpid()));
85824214Sache#endif
85924214Sache	}
86020253Sjoerg	for (i = 0; i < 8; i++)
86120253Sjoerg		salt[i] = chars[random() % 63];
86220253Sjoerg	salt[i] = '\0';
86320253Sjoerg
86420253Sjoerg	return strcpy(buf, crypt(password, salt));
86520253Sjoerg}
86620253Sjoerg
86720590Sdavidn#if defined(USE_MD5RAND)
86820555Sdavidnu_char *
86920590Sdavidnpw_getrand(u_char *buf, int len)	/* cryptographically secure rng */
87020555Sdavidn{
87120590Sdavidn	int i;
87220590Sdavidn	for (i=0;i<len;i+=16) {
87320590Sdavidn		u_char ubuf[16];
87420590Sdavidn
87520590Sdavidn		MD5_CTX md5_ctx;
87620590Sdavidn		struct timeval tv, tvo;
87720590Sdavidn		struct rusage ru;
87820590Sdavidn		int n=0;
87920590Sdavidn		int t;
88020590Sdavidn
88120590Sdavidn		MD5Init (&md5_ctx);
88220590Sdavidn		t=getpid();
88320590Sdavidn		MD5Update (&md5_ctx, (u_char*)&t, sizeof t);
88420590Sdavidn		t=getppid();
88520590Sdavidn		MD5Update (&md5_ctx, (u_char*)&t, sizeof t);
88620590Sdavidn		gettimeofday (&tvo, NULL);
88720590Sdavidn		do {
88820590Sdavidn			getrusage (RUSAGE_SELF, &ru);
88920590Sdavidn			MD5Update (&md5_ctx, (u_char*)&ru, sizeof ru);
89020590Sdavidn			gettimeofday (&tv, NULL);
89120590Sdavidn			MD5Update (&md5_ctx, (u_char*)&tv, sizeof tv);
89220590Sdavidn		} while (n++<20 || tv.tv_usec-tvo.tv_usec<100*1000);
89320590Sdavidn		MD5Final (ubuf, &md5_ctx);
89420590Sdavidn		memcpy(buf+i, ubuf, MIN(16, len-n));
89520590Sdavidn	}
89620590Sdavidn	return buf;
89720555Sdavidn}
89820253Sjoerg
89920590Sdavidn#else	/* Portable version */
90020590Sdavidn
90120590Sdavidnstatic u_char *
90220590Sdavidnpw_getrand(u_char *buf, int len)
90320590Sdavidn{
90420590Sdavidn	int i;
90520590Sdavidn
90620590Sdavidn	for (i = 0; i < len; i++) {
90724214Sache		unsigned long val = random();
90820590Sdavidn		/* Use all bits in the random value */
90920590Sdavidn		buf[i]=(u_char)((val >> 24) ^ (val >> 16) ^ (val >> 8) ^ val);
91020590Sdavidn	}
91120590Sdavidn	return buf;
91220590Sdavidn}
91320590Sdavidn
91420590Sdavidn#endif
91520590Sdavidn
91620253Sjoergstatic char    *
91720253Sjoergpw_password(struct userconf * cnf, struct cargs * args, char const * user)
91820253Sjoerg{
91920253Sjoerg	int             i, l;
92020253Sjoerg	char            pwbuf[32];
92120555Sdavidn	u_char		rndbuf[sizeof pwbuf];
92220253Sjoerg
92320253Sjoerg	switch (cnf->default_password) {
92420253Sjoerg	case -1:		/* Random password */
92524214Sache		if (!randinit) {
92624214Sache			randinit = 1;
92724214Sache#ifdef __FreeBSD__
92826626Sache			srandomdev();
92926626Sache#else
93026626Sache			srandom((unsigned long) (time(NULL) ^ getpid()));
93124214Sache#endif
93224214Sache		}
93320253Sjoerg		l = (random() % 8 + 8);	/* 8 - 16 chars */
93420555Sdavidn		pw_getrand(rndbuf, l);
93520253Sjoerg		for (i = 0; i < l; i++)
93642245Sbillf			pwbuf[i] = chars[rndbuf[i] % (sizeof(chars)-1)];
93720253Sjoerg		pwbuf[i] = '\0';
93820253Sjoerg
93920253Sjoerg		/*
94020253Sjoerg		 * We give this information back to the user
94120253Sjoerg		 */
94220267Sjoerg		if (getarg(args, 'h') == NULL && getarg(args, 'N') == NULL) {
94320712Sdavidn			if (isatty(1))
94420712Sdavidn				printf("Password for '%s' is: ", user);
94520253Sjoerg			printf("%s\n", pwbuf);
94620253Sjoerg			fflush(stdout);
94720253Sjoerg		}
94820253Sjoerg		break;
94920253Sjoerg
95020253Sjoerg	case -2:		/* No password at all! */
95120253Sjoerg		return "";
95220253Sjoerg
95320253Sjoerg	case 0:		/* No login - default */
95420253Sjoerg	default:
95520253Sjoerg		return "*";
95620253Sjoerg
95720253Sjoerg	case 1:		/* user's name */
95820253Sjoerg		strncpy(pwbuf, user, sizeof pwbuf);
95920253Sjoerg		pwbuf[sizeof pwbuf - 1] = '\0';
96020253Sjoerg		break;
96120253Sjoerg	}
96220253Sjoerg	return pw_pwcrypt(pwbuf);
96320253Sjoerg}
96420253Sjoerg
96520253Sjoerg
96620253Sjoergstatic int
96720253Sjoergprint_user(struct passwd * pwd, int pretty)
96820253Sjoerg{
96920253Sjoerg	if (!pretty) {
97020253Sjoerg		char            buf[_UC_MAXLINE];
97120253Sjoerg
97220253Sjoerg		fmtpwent(buf, pwd);
97320253Sjoerg		fputs(buf, stdout);
97420253Sjoerg	} else {
97520267Sjoerg		int		j;
97620253Sjoerg		char           *p;
97744229Sdavidn		struct group   *grp = GETGRGID(pwd->pw_gid);
97820253Sjoerg		char            uname[60] = "User &", office[60] = "[None]",
97920253Sjoerg		                wphone[60] = "[None]", hphone[60] = "[None]";
98020590Sdavidn		char		acexpire[32] = "[None]", pwexpire[32] = "[None]";
98120590Sdavidn		struct tm *    tptr;
98220253Sjoerg
98320253Sjoerg		if ((p = strtok(pwd->pw_gecos, ",")) != NULL) {
98420253Sjoerg			strncpy(uname, p, sizeof uname);
98520253Sjoerg			uname[sizeof uname - 1] = '\0';
98620253Sjoerg			if ((p = strtok(NULL, ",")) != NULL) {
98720253Sjoerg				strncpy(office, p, sizeof office);
98820253Sjoerg				office[sizeof office - 1] = '\0';
98920253Sjoerg				if ((p = strtok(NULL, ",")) != NULL) {
99020253Sjoerg					strncpy(wphone, p, sizeof wphone);
99120253Sjoerg					wphone[sizeof wphone - 1] = '\0';
99220253Sjoerg					if ((p = strtok(NULL, "")) != NULL) {
99320253Sjoerg						strncpy(hphone, p, sizeof hphone);
99420253Sjoerg						hphone[sizeof hphone - 1] = '\0';
99520253Sjoerg					}
99620253Sjoerg				}
99720253Sjoerg			}
99820253Sjoerg		}
99920253Sjoerg		/*
100020253Sjoerg		 * Handle '&' in gecos field
100120253Sjoerg		 */
100220253Sjoerg		if ((p = strchr(uname, '&')) != NULL) {
100320253Sjoerg			int             l = strlen(pwd->pw_name);
100420253Sjoerg			int             m = strlen(p);
100520253Sjoerg
100620253Sjoerg			memmove(p + l, p + 1, m);
100720253Sjoerg			memmove(p, pwd->pw_name, l);
100820253Sjoerg			*p = (char) toupper(*p);
100920253Sjoerg		}
101020590Sdavidn		if (pwd->pw_expire > (time_t)0 && (tptr = localtime(&pwd->pw_expire)) != NULL)
101120590Sdavidn		  strftime(acexpire, sizeof acexpire, "%c", tptr);
101220590Sdavidn		if (pwd->pw_change > (time_t)9 && (tptr = localtime(&pwd->pw_change)) != NULL)
101320590Sdavidn		  strftime(pwexpire, sizeof pwexpire, "%c", tptr);
101422394Sdavidn		printf("Login Name: %-15s   #%-12ld Group: %-15s   #%ld\n"
101520747Sdavidn		       " Full Name: %s\n"
101620747Sdavidn		       "      Home: %-26.26s      Class: %s\n"
101720747Sdavidn		       "     Shell: %-26.26s     Office: %s\n"
101820747Sdavidn		       "Work Phone: %-26.26s Home Phone: %s\n"
101920747Sdavidn		       "Acc Expire: %-26.26s Pwd Expire: %s\n",
102020253Sjoerg		       pwd->pw_name, (long) pwd->pw_uid,
102120253Sjoerg		       grp ? grp->gr_name : "(invalid)", (long) pwd->pw_gid,
102220253Sjoerg		       uname, pwd->pw_dir, pwd->pw_class,
102320590Sdavidn		       pwd->pw_shell, office, wphone, hphone,
102420590Sdavidn		       acexpire, pwexpire);
102544229Sdavidn	        SETGRENT();
102620267Sjoerg		j = 0;
102744229Sdavidn		while ((grp=GETGRENT()) != NULL)
102820267Sjoerg		{
102920267Sjoerg			int     i = 0;
103020747Sdavidn			while (grp->gr_mem[i] != NULL)
103120267Sjoerg			{
103220267Sjoerg				if (strcmp(grp->gr_mem[i], pwd->pw_name)==0)
103320267Sjoerg				{
103420747Sdavidn					printf(j++ == 0 ? "    Groups: %s" : ",%s", grp->gr_name);
103520267Sjoerg					break;
103620267Sjoerg				}
103720267Sjoerg				++i;
103820267Sjoerg			}
103920267Sjoerg		}
104044229Sdavidn		ENDGRENT();
104120267Sjoerg		printf("%s\n", j ? "\n" : "");
104220253Sjoerg	}
104320267Sjoerg	return EXIT_SUCCESS;
104420253Sjoerg}
104520253Sjoerg
104620679Sdavidnchar    *
104720679Sdavidnpw_checkname(u_char *name, int gecos)
104820253Sjoerg{
104920253Sjoerg	int             l = 0;
105021052Sdavidn	char const     *notch = gecos ? ":!@" : " ,\t:+&#%$^()!@~*?<>=|\\/\"";
105120253Sjoerg
105220253Sjoerg	while (name[l]) {
105321052Sdavidn		if (strchr(notch, name[l]) != NULL || name[l] < ' ' || name[l] == 127 ||
105421052Sdavidn			(!gecos && l==0 && name[l] == '-') ||	/* leading '-' */
105521052Sdavidn			(!gecos && name[l] & 0x80))	/* 8-bit */
105630259Scharnier			errx(EX_DATAERR, (name[l] >= ' ' && name[l] < 127)
105730259Scharnier					    ? "invalid character `%c' in field"
105830259Scharnier					    : "invalid character 0x%02x in field",
105920325Sjoerg					    name[l]);
106020253Sjoerg		++l;
106120253Sjoerg	}
106222394Sdavidn	if (!gecos && l > LOGNAMESIZE)
106330259Scharnier		errx(EX_DATAERR, "name too long `%s'", name);
106420679Sdavidn	return (char *)name;
106520253Sjoerg}
106620253Sjoerg
106720253Sjoerg
106820253Sjoergstatic void
106920253Sjoergrmat(uid_t uid)
107020253Sjoerg{
107120253Sjoerg	DIR            *d = opendir("/var/at/jobs");
107220253Sjoerg
107320253Sjoerg	if (d != NULL) {
107420253Sjoerg		struct dirent  *e;
107520253Sjoerg
107620253Sjoerg		while ((e = readdir(d)) != NULL) {
107720253Sjoerg			struct stat     st;
107820253Sjoerg
107920253Sjoerg			if (strncmp(e->d_name, ".lock", 5) != 0 &&
108020253Sjoerg			    stat(e->d_name, &st) == 0 &&
108120253Sjoerg			    !S_ISDIR(st.st_mode) &&
108220253Sjoerg			    st.st_uid == uid) {
108320253Sjoerg				char            tmp[MAXPATHLEN];
108420253Sjoerg
108520253Sjoerg				sprintf(tmp, "/usr/bin/atrm %s", e->d_name);
108620253Sjoerg				system(tmp);
108720253Sjoerg			}
108820253Sjoerg		}
108920253Sjoerg		closedir(d);
109020253Sjoerg	}
109120253Sjoerg}
109220747Sdavidn
109320747Sdavidnstatic void
109420747Sdavidnrmskey(char const * name)
109520747Sdavidn{
109620747Sdavidn	static const char etcskey[] = "/etc/skeykeys";
109721052Sdavidn	FILE   *fp = fopen(etcskey, "r+");
109820747Sdavidn
109921052Sdavidn	if (fp != NULL) {
110021052Sdavidn		char	tmp[1024];
110121052Sdavidn		off_t	atofs = 0;
110221052Sdavidn		int	length = strlen(name);
110320747Sdavidn
110421052Sdavidn		while (fgets(tmp, sizeof tmp, fp) != NULL) {
110521052Sdavidn			if (strncmp(name, tmp, length) == 0 && tmp[length]==' ') {
110621052Sdavidn				if (fseek(fp, atofs, SEEK_SET) == 0) {
110721052Sdavidn					fwrite("#", 1, 1, fp);	/* Comment username out */
110821052Sdavidn				}
110921052Sdavidn				break;
111020747Sdavidn			}
111121052Sdavidn			atofs = ftell(fp);
111220747Sdavidn		}
111321052Sdavidn		/*
111421052Sdavidn		 * If we got an error of any sort, don't update!
111521052Sdavidn		 */
111621052Sdavidn		fclose(fp);
111720747Sdavidn	}
111820747Sdavidn}
111920747Sdavidn
1120