pw_user.c revision 284124
120253Sjoerg/*-
220302Sjoerg * Copyright (C) 1996
320302Sjoerg *	David L. Nugent.  All rights reserved.
420253Sjoerg *
520253Sjoerg * Redistribution and use in source and binary forms, with or without
620253Sjoerg * modification, are permitted provided that the following conditions
720253Sjoerg * are met:
820253Sjoerg * 1. Redistributions of source code must retain the above copyright
920302Sjoerg *    notice, this list of conditions and the following disclaimer.
1020253Sjoerg * 2. Redistributions in binary form must reproduce the above copyright
1120253Sjoerg *    notice, this list of conditions and the following disclaimer in the
1220253Sjoerg *    documentation and/or other materials provided with the distribution.
1320253Sjoerg *
1420302Sjoerg * THIS SOFTWARE IS PROVIDED BY DAVID L. NUGENT AND CONTRIBUTORS ``AS IS'' AND
1520253Sjoerg * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1620253Sjoerg * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1720302Sjoerg * ARE DISCLAIMED.  IN NO EVENT SHALL DAVID L. NUGENT OR CONTRIBUTORS BE LIABLE
1820253Sjoerg * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1920253Sjoerg * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2020253Sjoerg * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2120253Sjoerg * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2220253Sjoerg * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2320253Sjoerg * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2420253Sjoerg * SUCH DAMAGE.
2544229Sdavidn *
2620253Sjoerg */
2720253Sjoerg
2830259Scharnier#ifndef lint
2930259Scharnierstatic const char rcsid[] =
3050479Speter  "$FreeBSD: head/usr.sbin/pw/pw_user.c 284124 2015-06-07 15:33:08Z bapt $";
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>
4364918Sgreen#include <login_cap.h>
44242349Sbapt#include <pwd.h>
45242349Sbapt#include <grp.h>
46242349Sbapt#include <libutil.h>
4720253Sjoerg#include "pw.h"
4820253Sjoerg#include "bitmap.h"
4920253Sjoerg
5023318Sache#define LOGNAMESIZE (MAXLOGNAME-1)
5122394Sdavidn
5252512Sdavidnstatic		char locked_str[] = "*LOCKED*";
5324214Sache
54284111Sbaptstatic int	delete_user(struct userconf *cnf, struct passwd *pwd,
55284111Sbapt		    struct carg *a_name, int delete, int mode);
56284124Sbaptstatic int	print_user(struct passwd * pwd);
5720253Sjoergstatic uid_t    pw_uidpolicy(struct userconf * cnf, struct cargs * args);
58284118Sbaptstatic uid_t    pw_gidpolicy(struct cargs * args, char *nam, gid_t prefer);
5920253Sjoergstatic time_t   pw_pwdpolicy(struct userconf * cnf, struct cargs * args);
6020253Sjoergstatic time_t   pw_exppolicy(struct userconf * cnf, struct cargs * args);
6120253Sjoergstatic char    *pw_homepolicy(struct userconf * cnf, struct cargs * args, char const * user);
6220253Sjoergstatic char    *pw_shellpolicy(struct userconf * cnf, struct cargs * args, char *newshell);
6320253Sjoergstatic char    *pw_password(struct userconf * cnf, struct cargs * args, char const * user);
6420253Sjoergstatic char    *shell_path(char const * path, char *shells[], char *sh);
6520253Sjoergstatic void     rmat(uid_t uid);
6685145Sachestatic void     rmopie(char const * name);
6720253Sjoerg
68283961Sbaptstatic void
69284118Sbaptcreate_and_populate_homedir(int mode, struct passwd *pwd)
70283961Sbapt{
71283961Sbapt	char *homedir, *dotdir;
72284118Sbapt	struct userconf *cnf = conf.userconf;
73283961Sbapt
74283961Sbapt	homedir = dotdir = NULL;
75283961Sbapt
76284118Sbapt	if (conf.rootdir[0] != '\0') {
77284118Sbapt		asprintf(&homedir, "%s/%s", conf.rootdir, pwd->pw_dir);
78283961Sbapt		if (homedir == NULL)
79283961Sbapt			errx(EX_OSERR, "out of memory");
80284118Sbapt		asprintf(&dotdir, "%s/%s", conf.rootdir, cnf->dotdir);
81283961Sbapt	}
82283961Sbapt
83283961Sbapt	copymkdir(homedir ? homedir : pwd->pw_dir, dotdir ? dotdir: cnf->dotdir,
84283961Sbapt	    cnf->homemode, pwd->pw_uid, pwd->pw_gid);
85283961Sbapt	pw_log(cnf, mode, W_USER, "%s(%u) home %s made", pwd->pw_name,
86283961Sbapt	    pwd->pw_uid, pwd->pw_dir);
87283961Sbapt}
88283961Sbapt
8920253Sjoerg/*-
9020253Sjoerg * -C config      configuration file
9120253Sjoerg * -q             quiet operation
9220253Sjoerg * -n name        login name
9320253Sjoerg * -u uid         user id
9420253Sjoerg * -c comment     user name/comment
9520253Sjoerg * -d directory   home directory
9620253Sjoerg * -e date        account expiry date
9720253Sjoerg * -p date        password expiry date
9820253Sjoerg * -g grp         primary group
9920253Sjoerg * -G grp1,grp2   additional groups
10020253Sjoerg * -m [ -k dir ]  create and set up home
10120253Sjoerg * -s shell       name of login shell
10220253Sjoerg * -o             duplicate uid ok
10320253Sjoerg * -L class       user class
10420253Sjoerg * -l name        new login name
10520253Sjoerg * -h fd          password filehandle
106124382Siedowse * -H fd          encrypted password filehandle
10720253Sjoerg * -F             force print or add
10820253Sjoerg *   Setting defaults:
10920253Sjoerg * -D             set user defaults
11020253Sjoerg * -b dir         default home root dir
11120253Sjoerg * -e period      default expiry period
11220253Sjoerg * -p period      default password change period
11320253Sjoerg * -g group       default group
11420253Sjoerg * -G             grp1,grp2.. default additional groups
11520253Sjoerg * -L class       default login class
11620253Sjoerg * -k dir         default home skeleton
11720253Sjoerg * -s shell       default shell
11820253Sjoerg * -w method      default password method
11920253Sjoerg */
12020253Sjoerg
12120253Sjoergint
122284118Sbaptpw_user(int mode, struct cargs * args)
12320253Sjoerg{
12452527Sdavidn	int	        rc, edited = 0;
12520253Sjoerg	char           *p = NULL;
12652512Sdavidn	char					 *passtmp;
12720253Sjoerg	struct carg    *a_name;
12820253Sjoerg	struct carg    *a_uid;
12920253Sjoerg	struct carg    *arg;
13020253Sjoerg	struct passwd  *pwd = NULL;
13120253Sjoerg	struct group   *grp;
13220253Sjoerg	struct stat     st;
133284118Sbapt	struct userconf	*cnf;
13420747Sdavidn	char            line[_PASSWORD_LEN+1];
135283961Sbapt	char		path[MAXPATHLEN];
13682868Sdd	FILE	       *fp;
137167919Sle	char *dmode_c;
138167919Sle	void *set = NULL;
13920253Sjoerg
14020253Sjoerg	static struct passwd fakeuser =
14120253Sjoerg	{
14220253Sjoerg		NULL,
14320253Sjoerg		"*",
14420253Sjoerg		-1,
14520253Sjoerg		-1,
14620253Sjoerg		0,
14720253Sjoerg		"",
14820253Sjoerg		"User &",
14956000Sdavidn		"/nonexistent",
15020253Sjoerg		"/bin/sh",
15120253Sjoerg		0
15256000Sdavidn#if defined(__FreeBSD__)
15356000Sdavidn		,0
15456000Sdavidn#endif
15520253Sjoerg	};
15620253Sjoerg
157284118Sbapt	cnf = conf.userconf;
15852512Sdavidn
15920253Sjoerg	/*
16020267Sjoerg	 * With M_NEXT, we only need to return the
16120267Sjoerg	 * next uid to stdout
16220267Sjoerg	 */
16320267Sjoerg	if (mode == M_NEXT)
16420267Sjoerg	{
16520267Sjoerg		uid_t next = pw_uidpolicy(cnf, args);
16620267Sjoerg		if (getarg(args, 'q'))
16720267Sjoerg			return next;
168283842Sbapt		printf("%u:", next);
169284118Sbapt		pw_group(mode, args);
17020267Sjoerg		return EXIT_SUCCESS;
17120267Sjoerg	}
17220267Sjoerg
17320267Sjoerg	/*
17420253Sjoerg	 * We can do all of the common legwork here
17520253Sjoerg	 */
17620253Sjoerg
17720253Sjoerg	if ((arg = getarg(args, 'b')) != NULL) {
17820267Sjoerg		cnf->home = arg->val;
17920253Sjoerg	}
18021052Sdavidn
181167919Sle	if ((arg = getarg(args, 'M')) != NULL) {
182167919Sle		dmode_c = arg->val;
183167919Sle		if ((set = setmode(dmode_c)) == NULL)
184167919Sle			errx(EX_DATAERR, "invalid directory creation mode '%s'",
185167919Sle			    dmode_c);
186219408Sjkim		cnf->homemode = getmode(set, _DEF_DIRMODE);
187167919Sle		free(set);
188168044Sle	}
189167919Sle
19021052Sdavidn	/*
19121052Sdavidn	 * If we'll need to use it or we're updating it,
19221052Sdavidn	 * then create the base home directory if necessary
19321052Sdavidn	 */
194224535Sdelphij	if (arg != NULL || getarg(args, 'm') != NULL) {
19521052Sdavidn		int	l = strlen(cnf->home);
19621052Sdavidn
19721052Sdavidn		if (l > 1 && cnf->home[l-1] == '/')	/* Shave off any trailing path delimiter */
19821052Sdavidn			cnf->home[--l] = '\0';
19921052Sdavidn
20021052Sdavidn		if (l < 2 || *cnf->home != '/')		/* Check for absolute path name */
20130259Scharnier			errx(EX_DATAERR, "invalid base directory for home '%s'", cnf->home);
20221052Sdavidn
20321052Sdavidn		if (stat(cnf->home, &st) == -1) {
20421052Sdavidn			char	dbuf[MAXPATHLEN];
20521052Sdavidn
20621242Sdavidn			/*
20721242Sdavidn			 * This is a kludge especially for Joerg :)
20821242Sdavidn			 * If the home directory would be created in the root partition, then
20921242Sdavidn			 * we really create it under /usr which is likely to have more space.
21021242Sdavidn			 * But we create a symlink from cnf->home -> "/usr" -> cnf->home
21121242Sdavidn			 */
21221242Sdavidn			if (strchr(cnf->home+1, '/') == NULL) {
213282683Sbapt				snprintf(dbuf, MAXPATHLEN, "/usr%s", cnf->home);
214219408Sjkim				if (mkdir(dbuf, _DEF_DIRMODE) != -1 || errno == EEXIST) {
21521242Sdavidn					chown(dbuf, 0, 0);
216148584Spjd					/*
217148584Spjd					 * Skip first "/" and create symlink:
218148584Spjd					 * /home -> usr/home
219148584Spjd					 */
220148584Spjd					symlink(dbuf+1, cnf->home);
22121242Sdavidn				}
22221242Sdavidn				/* If this falls, fall back to old method */
22321242Sdavidn			}
224130633Srobert			strlcpy(dbuf, cnf->home, sizeof(dbuf));
225130633Srobert			p = dbuf;
22621242Sdavidn			if (stat(dbuf, &st) == -1) {
227252377Skientzle				while ((p = strchr(p + 1, '/')) != NULL) {
22821242Sdavidn					*p = '\0';
22921242Sdavidn					if (stat(dbuf, &st) == -1) {
230219408Sjkim						if (mkdir(dbuf, _DEF_DIRMODE) == -1)
23121242Sdavidn							goto direrr;
23221242Sdavidn						chown(dbuf, 0, 0);
23321242Sdavidn					} else if (!S_ISDIR(st.st_mode))
23430259Scharnier						errx(EX_OSFILE, "'%s' (root home parent) is not a directory", dbuf);
23521242Sdavidn					*p = '/';
23621242Sdavidn				}
23721052Sdavidn			}
23821242Sdavidn			if (stat(dbuf, &st) == -1) {
239219408Sjkim				if (mkdir(dbuf, _DEF_DIRMODE) == -1) {
24030259Scharnier				direrr:	err(EX_OSFILE, "mkdir '%s'", dbuf);
24121052Sdavidn				}
24221052Sdavidn				chown(dbuf, 0, 0);
24321052Sdavidn			}
24421052Sdavidn		} else if (!S_ISDIR(st.st_mode))
24530259Scharnier			errx(EX_OSFILE, "root home `%s' is not a directory", cnf->home);
24621052Sdavidn	}
24721052Sdavidn
24820253Sjoerg	if ((arg = getarg(args, 'e')) != NULL)
24920253Sjoerg		cnf->expire_days = atoi(arg->val);
25020253Sjoerg
25121330Sdavidn	if ((arg = getarg(args, 'y')) != NULL)
25221330Sdavidn		cnf->nispasswd = arg->val;
25321330Sdavidn
25420253Sjoerg	if ((arg = getarg(args, 'p')) != NULL && arg->val)
25520253Sjoerg		cnf->password_days = atoi(arg->val);
25620253Sjoerg
25720253Sjoerg	if ((arg = getarg(args, 'g')) != NULL) {
25863596Sdavidn		if (!*(p = arg->val))	/* Handle empty group list specially */
25963596Sdavidn			cnf->default_group = "";
26063596Sdavidn		else {
26163596Sdavidn			if ((grp = GETGRNAM(p)) == NULL) {
26263596Sdavidn				if (!isdigit((unsigned char)*p) || (grp = GETGRGID((gid_t) atoi(p))) == NULL)
26363596Sdavidn					errx(EX_NOUSER, "group `%s' does not exist", p);
26463596Sdavidn			}
26563596Sdavidn			cnf->default_group = newstr(grp->gr_name);
26620253Sjoerg		}
26720253Sjoerg	}
26820253Sjoerg	if ((arg = getarg(args, 'L')) != NULL)
269284110Sbapt		cnf->default_class = pw_checkname(arg->val, 0);
27020253Sjoerg
27120253Sjoerg	if ((arg = getarg(args, 'G')) != NULL && arg->val) {
27252527Sdavidn		int i = 0;
27320253Sjoerg
27420747Sdavidn		for (p = strtok(arg->val, ", \t"); p != NULL; p = strtok(NULL, ", \t")) {
27544229Sdavidn			if ((grp = GETGRNAM(p)) == NULL) {
27661957Sache				if (!isdigit((unsigned char)*p) || (grp = GETGRGID((gid_t) atoi(p))) == NULL)
27730259Scharnier					errx(EX_NOUSER, "group `%s' does not exist", p);
27820253Sjoerg			}
27920747Sdavidn			if (extendarray(&cnf->groups, &cnf->numgroups, i + 2) != -1)
28020747Sdavidn				cnf->groups[i++] = newstr(grp->gr_name);
28120253Sjoerg		}
28220747Sdavidn		while (i < cnf->numgroups)
28320253Sjoerg			cnf->groups[i++] = NULL;
28420253Sjoerg	}
28552527Sdavidn
28620253Sjoerg	if ((arg = getarg(args, 'k')) != NULL) {
28726088Sdavidn		if (stat(cnf->dotdir = arg->val, &st) == -1 || !S_ISDIR(st.st_mode))
28830259Scharnier			errx(EX_OSFILE, "skeleton `%s' is not a directory or does not exist", cnf->dotdir);
28920253Sjoerg	}
29052527Sdavidn
29120253Sjoerg	if ((arg = getarg(args, 's')) != NULL)
29220253Sjoerg		cnf->shell_default = arg->val;
29320253Sjoerg
29463600Sdavidn	if ((arg = getarg(args, 'w')) != NULL)
29563600Sdavidn		cnf->default_password = boolean_val(arg->val, cnf->default_password);
29620253Sjoerg	if (mode == M_ADD && getarg(args, 'D')) {
29720253Sjoerg		if (getarg(args, 'n') != NULL)
29830259Scharnier			errx(EX_DATAERR, "can't combine `-D' with `-n name'");
29920253Sjoerg		if ((arg = getarg(args, 'u')) != NULL && (p = strtok(arg->val, ", \t")) != NULL) {
30020253Sjoerg			if ((cnf->min_uid = (uid_t) atoi(p)) == 0)
30120253Sjoerg				cnf->min_uid = 1000;
30220253Sjoerg			if ((p = strtok(NULL, " ,\t")) == NULL || (cnf->max_uid = (uid_t) atoi(p)) < cnf->min_uid)
30320253Sjoerg				cnf->max_uid = 32000;
30420253Sjoerg		}
30520253Sjoerg		if ((arg = getarg(args, 'i')) != NULL && (p = strtok(arg->val, ", \t")) != NULL) {
30620253Sjoerg			if ((cnf->min_gid = (gid_t) atoi(p)) == 0)
30720253Sjoerg				cnf->min_gid = 1000;
30820253Sjoerg			if ((p = strtok(NULL, " ,\t")) == NULL || (cnf->max_gid = (gid_t) atoi(p)) < cnf->min_gid)
30920253Sjoerg				cnf->max_gid = 32000;
31020253Sjoerg		}
31120253Sjoerg
31220253Sjoerg		arg = getarg(args, 'C');
31320253Sjoerg		if (write_userconfig(arg ? arg->val : NULL))
31420267Sjoerg			return EXIT_SUCCESS;
315283814Sbapt		err(EX_IOERR, "config udpate");
31620253Sjoerg	}
31752527Sdavidn
31820253Sjoerg	if (mode == M_PRINT && getarg(args, 'a')) {
31944229Sdavidn		SETPWENT();
32044229Sdavidn		while ((pwd = GETPWENT()) != NULL)
321284124Sbapt			print_user(pwd);
32244229Sdavidn		ENDPWENT();
32320267Sjoerg		return EXIT_SUCCESS;
32420253Sjoerg	}
32552527Sdavidn
32620253Sjoerg	if ((a_name = getarg(args, 'n')) != NULL)
327284110Sbapt		pwd = GETPWNAM(pw_checkname(a_name->val, 0));
32820253Sjoerg	a_uid = getarg(args, 'u');
32920253Sjoerg
33020253Sjoerg	if (a_uid == NULL) {
33120253Sjoerg		if (a_name == NULL)
33230259Scharnier			errx(EX_DATAERR, "user name or id required");
33320253Sjoerg
33420253Sjoerg		/*
33520253Sjoerg		 * Determine whether 'n' switch is name or uid - we don't
33620253Sjoerg		 * really don't really care which we have, but we need to
33720253Sjoerg		 * know.
33820253Sjoerg		 */
33943780Sdes		if (mode != M_ADD && pwd == NULL
340241108Sbapt		    && strspn(a_name->val, "0123456789") == strlen(a_name->val)
341241108Sbapt		    && *a_name->val) {
34220253Sjoerg			(a_uid = a_name)->ch = 'u';
34320253Sjoerg			a_name = NULL;
34420253Sjoerg		}
345273787Sbapt	} else {
346277764Sbapt		if (strspn(a_uid->val, "0123456789") != strlen(a_uid->val))
347273787Sbapt			errx(EX_USAGE, "-u expects a number");
34820253Sjoerg	}
34952527Sdavidn
35020253Sjoerg	/*
35120253Sjoerg	 * Update, delete & print require that the user exists
35220253Sjoerg	 */
35352512Sdavidn	if (mode == M_UPDATE || mode == M_DELETE ||
35452512Sdavidn	    mode == M_PRINT  || mode == M_LOCK   || mode == M_UNLOCK) {
35552527Sdavidn
35620253Sjoerg		if (a_name == NULL && pwd == NULL)	/* Try harder */
35744229Sdavidn			pwd = GETPWUID(atoi(a_uid->val));
35820253Sjoerg
35920253Sjoerg		if (pwd == NULL) {
36020253Sjoerg			if (mode == M_PRINT && getarg(args, 'F')) {
36120253Sjoerg				fakeuser.pw_name = a_name ? a_name->val : "nouser";
362283841Sbapt				fakeuser.pw_uid = a_uid ? (uid_t) atol(a_uid->val) : (uid_t) -1;
36344386Sdavidn				return print_user(&fakeuser,
36444386Sdavidn						  getarg(args, '7') != NULL);
36520253Sjoerg			}
36620253Sjoerg			if (a_name == NULL)
36730259Scharnier				errx(EX_NOUSER, "no such uid `%s'", a_uid->val);
36830259Scharnier			errx(EX_NOUSER, "no such user `%s'", a_name->val);
36920253Sjoerg		}
37052527Sdavidn
37120253Sjoerg		if (a_name == NULL)	/* May be needed later */
37220253Sjoerg			a_name = addarg(args, 'n', newstr(pwd->pw_name));
37320253Sjoerg
37420253Sjoerg		/*
37552512Sdavidn		 * The M_LOCK and M_UNLOCK functions simply add or remove
37652512Sdavidn		 * a "*LOCKED*" prefix from in front of the password to
37752512Sdavidn		 * prevent it decoding correctly, and therefore prevents
37852512Sdavidn		 * access. Of course, this only prevents access via
37952512Sdavidn		 * password authentication (not ssh, kerberos or any
38052512Sdavidn		 * other method that does not use the UNIX password) but
38152512Sdavidn		 * that is a known limitation.
38252512Sdavidn		 */
38352512Sdavidn
38452512Sdavidn		if (mode == M_LOCK) {
38552512Sdavidn			if (strncmp(pwd->pw_passwd, locked_str, sizeof(locked_str)-1) == 0)
38652512Sdavidn				errx(EX_DATAERR, "user '%s' is already locked", pwd->pw_name);
387282685Sbapt			asprintf(&passtmp, "%s%s", locked_str, pwd->pw_passwd);
38852512Sdavidn			if (passtmp == NULL)	/* disaster */
38952512Sdavidn				errx(EX_UNAVAILABLE, "out of memory");
39052512Sdavidn			pwd->pw_passwd = passtmp;
39152527Sdavidn			edited = 1;
39252512Sdavidn		} else if (mode == M_UNLOCK) {
39352512Sdavidn			if (strncmp(pwd->pw_passwd, locked_str, sizeof(locked_str)-1) != 0)
39452512Sdavidn				errx(EX_DATAERR, "user '%s' is not locked", pwd->pw_name);
39552512Sdavidn			pwd->pw_passwd += sizeof(locked_str)-1;
39652527Sdavidn			edited = 1;
397284111Sbapt		} else if (mode == M_DELETE)
398284111Sbapt			return (delete_user(cnf, pwd, a_name,
399284111Sbapt				    getarg(args, 'r') != NULL, mode));
400284111Sbapt		else if (mode == M_PRINT)
401284122Sbapt			return print_user(pwd, getarg(args, '7') != NULL);
40220253Sjoerg
40320253Sjoerg		/*
40420253Sjoerg		 * The rest is edit code
40520253Sjoerg		 */
40620253Sjoerg		if ((arg = getarg(args, 'l')) != NULL) {
40720253Sjoerg			if (strcmp(pwd->pw_name, "root") == 0)
40830259Scharnier				errx(EX_DATAERR, "can't rename `root' account");
409284110Sbapt			pwd->pw_name = pw_checkname(arg->val, 0);
41052527Sdavidn			edited = 1;
41120253Sjoerg		}
41252527Sdavidn
41361957Sache		if ((arg = getarg(args, 'u')) != NULL && isdigit((unsigned char)*arg->val)) {
41420253Sjoerg			pwd->pw_uid = (uid_t) atol(arg->val);
41552527Sdavidn			edited = 1;
41620253Sjoerg			if (pwd->pw_uid != 0 && strcmp(pwd->pw_name, "root") == 0)
41730259Scharnier				errx(EX_DATAERR, "can't change uid of `root' account");
41820253Sjoerg			if (pwd->pw_uid == 0 && strcmp(pwd->pw_name, "root") != 0)
41930259Scharnier				warnx("WARNING: account `%s' will have a uid of 0 (superuser access!)", pwd->pw_name);
42020253Sjoerg		}
42120253Sjoerg
42252527Sdavidn		if ((arg = getarg(args, 'g')) != NULL && pwd->pw_uid != 0) {	/* Already checked this */
42352527Sdavidn			gid_t newgid = (gid_t) GETGRNAM(cnf->default_group)->gr_gid;
42452527Sdavidn			if (newgid != pwd->pw_gid) {
42552527Sdavidn				edited = 1;
42661762Sdavidn				pwd->pw_gid = newgid;
42752527Sdavidn			}
42852527Sdavidn		}
42952527Sdavidn
43020253Sjoerg		if ((arg = getarg(args, 'p')) != NULL) {
43152527Sdavidn			if (*arg->val == '\0' || strcmp(arg->val, "0") == 0) {
43252527Sdavidn				if (pwd->pw_change != 0) {
43352527Sdavidn					pwd->pw_change = 0;
43452527Sdavidn					edited = 1;
43552527Sdavidn				}
43652527Sdavidn			}
43720253Sjoerg			else {
43820253Sjoerg				time_t          now = time(NULL);
43920253Sjoerg				time_t          expire = parse_date(now, arg->val);
44020253Sjoerg
44152527Sdavidn				if (pwd->pw_change != expire) {
44252527Sdavidn					pwd->pw_change = expire;
44352527Sdavidn					edited = 1;
44452527Sdavidn				}
44520253Sjoerg			}
44620253Sjoerg		}
44752527Sdavidn
44820267Sjoerg		if ((arg = getarg(args, 'e')) != NULL) {
44952527Sdavidn			if (*arg->val == '\0' || strcmp(arg->val, "0") == 0) {
45052527Sdavidn				if (pwd->pw_expire != 0) {
45152527Sdavidn					pwd->pw_expire = 0;
45252527Sdavidn					edited = 1;
45352527Sdavidn				}
45452527Sdavidn			}
45520253Sjoerg			else {
45620253Sjoerg				time_t          now = time(NULL);
45720253Sjoerg				time_t          expire = parse_date(now, arg->val);
45820253Sjoerg
45952527Sdavidn				if (pwd->pw_expire != expire) {
46052527Sdavidn					pwd->pw_expire = expire;
46152527Sdavidn					edited = 1;
46252527Sdavidn				}
46320253Sjoerg			}
46420253Sjoerg		}
46520253Sjoerg
46652527Sdavidn		if ((arg = getarg(args, 's')) != NULL) {
46752527Sdavidn			char *shell = shell_path(cnf->shelldir, cnf->shells, arg->val);
46852527Sdavidn			if (shell == NULL)
46952527Sdavidn				shell = "";
47052527Sdavidn			if (strcmp(shell, pwd->pw_shell) != 0) {
47152527Sdavidn				pwd->pw_shell = shell;
47252527Sdavidn				edited = 1;
47352527Sdavidn			}
47452527Sdavidn		}
47520253Sjoerg
47652527Sdavidn		if (getarg(args, 'L')) {
47752527Sdavidn			if (cnf->default_class == NULL)
47852527Sdavidn				cnf->default_class = "";
47952527Sdavidn			if (strcmp(pwd->pw_class, cnf->default_class) != 0) {
48052527Sdavidn				pwd->pw_class = cnf->default_class;
48152527Sdavidn				edited = 1;
48252527Sdavidn			}
48352527Sdavidn		}
48452527Sdavidn
48520747Sdavidn		if ((arg  = getarg(args, 'd')) != NULL) {
486130629Srobert			if (strcmp(pwd->pw_dir, arg->val))
487130629Srobert				edited = 1;
48820747Sdavidn			if (stat(pwd->pw_dir = arg->val, &st) == -1) {
48920747Sdavidn				if (getarg(args, 'm') == NULL && strcmp(pwd->pw_dir, "/nonexistent") != 0)
49030259Scharnier				  warnx("WARNING: home `%s' does not exist", pwd->pw_dir);
49120747Sdavidn			} else if (!S_ISDIR(st.st_mode))
49230259Scharnier				warnx("WARNING: home `%s' is not a directory", pwd->pw_dir);
49320747Sdavidn		}
49420747Sdavidn
495124382Siedowse		if ((arg = getarg(args, 'w')) != NULL &&
496124382Siedowse		    getarg(args, 'h') == NULL && getarg(args, 'H') == NULL) {
49764918Sgreen			login_cap_t *lc;
49864918Sgreen
49964918Sgreen			lc = login_getpwclass(pwd);
50064918Sgreen			if (lc == NULL ||
501252688Sdes			    login_setcryptfmt(lc, "sha512", NULL) == NULL)
50264918Sgreen				warn("setting crypt(3) format");
50364918Sgreen			login_close(lc);
50420267Sjoerg			pwd->pw_passwd = pw_password(cnf, args, pwd->pw_name);
50552527Sdavidn			edited = 1;
50652527Sdavidn		}
50720267Sjoerg
50820253Sjoerg	} else {
50964918Sgreen		login_cap_t *lc;
51052527Sdavidn
51152527Sdavidn		/*
51252527Sdavidn		 * Add code
51352527Sdavidn		 */
51452527Sdavidn
51520253Sjoerg		if (a_name == NULL)	/* Required */
51630259Scharnier			errx(EX_DATAERR, "login name required");
51744229Sdavidn		else if ((pwd = GETPWNAM(a_name->val)) != NULL)	/* Exists */
51830259Scharnier			errx(EX_DATAERR, "login name `%s' already exists", a_name->val);
51920253Sjoerg
52020253Sjoerg		/*
52120253Sjoerg		 * Now, set up defaults for a new user
52220253Sjoerg		 */
52320253Sjoerg		pwd = &fakeuser;
52420253Sjoerg		pwd->pw_name = a_name->val;
52520253Sjoerg		pwd->pw_class = cnf->default_class ? cnf->default_class : "";
52620253Sjoerg		pwd->pw_uid = pw_uidpolicy(cnf, args);
527284118Sbapt		pwd->pw_gid = pw_gidpolicy(args, pwd->pw_name, (gid_t) pwd->pw_uid);
52820253Sjoerg		pwd->pw_change = pw_pwdpolicy(cnf, args);
52920253Sjoerg		pwd->pw_expire = pw_exppolicy(cnf, args);
53020253Sjoerg		pwd->pw_dir = pw_homepolicy(cnf, args, pwd->pw_name);
53120253Sjoerg		pwd->pw_shell = pw_shellpolicy(cnf, args, NULL);
53264918Sgreen		lc = login_getpwclass(pwd);
533272833Sdes		if (lc == NULL || login_setcryptfmt(lc, "sha512", NULL) == NULL)
53464918Sgreen			warn("setting crypt(3) format");
53564918Sgreen		login_close(lc);
53664918Sgreen		pwd->pw_passwd = pw_password(cnf, args, pwd->pw_name);
53752527Sdavidn		edited = 1;
53820253Sjoerg
53920253Sjoerg		if (pwd->pw_uid == 0 && strcmp(pwd->pw_name, "root") != 0)
54030259Scharnier			warnx("WARNING: new account `%s' has a uid of 0 (superuser access!)", pwd->pw_name);
54120253Sjoerg	}
54220253Sjoerg
54320253Sjoerg	/*
54420253Sjoerg	 * Shared add/edit code
54520253Sjoerg	 */
54652527Sdavidn	if ((arg = getarg(args, 'c')) != NULL) {
547284110Sbapt		char	*gecos = pw_checkname(arg->val, 1);
54852527Sdavidn		if (strcmp(pwd->pw_gecos, gecos) != 0) {
54952527Sdavidn			pwd->pw_gecos = gecos;
55052527Sdavidn			edited = 1;
55152527Sdavidn		}
55252527Sdavidn	}
55320253Sjoerg
554124382Siedowse	if ((arg = getarg(args, 'h')) != NULL ||
555124382Siedowse	    (arg = getarg(args, 'H')) != NULL) {
55663572Sdavidn		if (strcmp(arg->val, "-") == 0) {
55763572Sdavidn			if (!pwd->pw_passwd || *pwd->pw_passwd != '*') {
55863572Sdavidn				pwd->pw_passwd = "*";	/* No access */
55963572Sdavidn				edited = 1;
56063572Sdavidn			}
56163572Sdavidn		} else {
56220253Sjoerg			int             fd = atoi(arg->val);
563124382Siedowse			int		precrypt = (arg->ch == 'H');
56420253Sjoerg			int             b;
56520253Sjoerg			int             istty = isatty(fd);
56620253Sjoerg			struct termios  t;
56764918Sgreen			login_cap_t	*lc;
56820253Sjoerg
56920253Sjoerg			if (istty) {
57020253Sjoerg				if (tcgetattr(fd, &t) == -1)
57120253Sjoerg					istty = 0;
57220253Sjoerg				else {
57320253Sjoerg					struct termios  n = t;
57420253Sjoerg
57520253Sjoerg					/* Disable echo */
57620253Sjoerg					n.c_lflag &= ~(ECHO);
57720253Sjoerg					tcsetattr(fd, TCSANOW, &n);
578124382Siedowse					printf("%s%spassword for user %s:",
579124382Siedowse					     (mode == M_UPDATE) ? "new " : "",
580124382Siedowse					     precrypt ? "encrypted " : "",
581124382Siedowse					     pwd->pw_name);
58220253Sjoerg					fflush(stdout);
58320253Sjoerg				}
58420253Sjoerg			}
58520253Sjoerg			b = read(fd, line, sizeof(line) - 1);
58620253Sjoerg			if (istty) {	/* Restore state */
58720253Sjoerg				tcsetattr(fd, TCSANOW, &t);
58820253Sjoerg				fputc('\n', stdout);
58920253Sjoerg				fflush(stdout);
59020253Sjoerg			}
591283814Sbapt			if (b < 0)
592283814Sbapt				err(EX_IOERR, "-%c file descriptor",
593283814Sbapt				    precrypt ? 'H' : 'h');
59420253Sjoerg			line[b] = '\0';
595168045Sle			if ((p = strpbrk(line, "\r\n")) != NULL)
59620253Sjoerg				*p = '\0';
59720253Sjoerg			if (!*line)
59830259Scharnier				errx(EX_DATAERR, "empty password read on file descriptor %d", fd);
599124382Siedowse			if (precrypt) {
600124382Siedowse				if (strchr(line, ':') != NULL)
601124382Siedowse					return EX_DATAERR;
602124382Siedowse				pwd->pw_passwd = line;
603124382Siedowse			} else {
604124382Siedowse				lc = login_getpwclass(pwd);
605124382Siedowse				if (lc == NULL ||
606272833Sdes				    login_setcryptfmt(lc, "sha512", NULL) == NULL)
607124382Siedowse					warn("setting crypt(3) format");
608124382Siedowse				login_close(lc);
609124382Siedowse				pwd->pw_passwd = pw_pwcrypt(line);
610124382Siedowse			}
61152527Sdavidn			edited = 1;
61220253Sjoerg		}
61320253Sjoerg	}
61420267Sjoerg
61520267Sjoerg	/*
61620267Sjoerg	 * Special case: -N only displays & exits
61720267Sjoerg	 */
618284121Sbapt	if (conf.dryrun)
619284122Sbapt		return print_user(pwd, getarg(args, '7') != NULL);
62020267Sjoerg
62121330Sdavidn	if (mode == M_ADD) {
62252527Sdavidn		edited = 1;	/* Always */
62352502Sdavidn		rc = addpwent(pwd);
624283814Sbapt		if (rc == -1)
625283814Sbapt			errx(EX_IOERR, "user '%s' already exists",
626283814Sbapt			    pwd->pw_name);
627283814Sbapt		else if (rc != 0)
628283814Sbapt			err(EX_IOERR, "passwd file update");
62952502Sdavidn		if (cnf->nispasswd && *cnf->nispasswd=='/') {
63052502Sdavidn			rc = addnispwent(cnf->nispasswd, pwd);
63152502Sdavidn			if (rc == -1)
63252502Sdavidn				warnx("User '%s' already exists in NIS passwd", pwd->pw_name);
63352502Sdavidn			else
63456000Sdavidn				warn("NIS passwd update");
63552502Sdavidn			/* NOTE: we treat NIS-only update errors as non-fatal */
63652502Sdavidn		}
63752512Sdavidn	} else if (mode == M_UPDATE || mode == M_LOCK || mode == M_UNLOCK) {
63852527Sdavidn		if (edited) {	/* Only updated this if required */
63952527Sdavidn			rc = chgpwent(a_name->val, pwd);
640283814Sbapt			if (rc == -1)
641283814Sbapt				errx(EX_IOERR, "user '%s' does not exist (NIS?)", pwd->pw_name);
642283814Sbapt			else if (rc != 0)
643283814Sbapt				err(EX_IOERR, "passwd file update");
64452527Sdavidn			if ( cnf->nispasswd && *cnf->nispasswd=='/') {
64552527Sdavidn				rc = chgnispwent(cnf->nispasswd, a_name->val, pwd);
64652527Sdavidn				if (rc == -1)
64752527Sdavidn					warn("User '%s' not found in NIS passwd", pwd->pw_name);
64852527Sdavidn				else
64956000Sdavidn					warn("NIS passwd update");
65052527Sdavidn				/* NOTE: NIS-only update errors are not fatal */
65152527Sdavidn			}
65252502Sdavidn		}
65321330Sdavidn	}
65421330Sdavidn
65520253Sjoerg	/*
65620253Sjoerg	 * Ok, user is created or changed - now edit group file
65720253Sjoerg	 */
65820253Sjoerg
659242349Sbapt	if (mode == M_ADD || getarg(args, 'G') != NULL) {
660273779Sbapt		int i, j;
661273779Sbapt		/* First remove the user from all group */
662273779Sbapt		SETGRENT();
663273779Sbapt		while ((grp = GETGRENT()) != NULL) {
664273779Sbapt			char group[MAXLOGNAME];
665273779Sbapt			if (grp->gr_mem == NULL)
666273779Sbapt				continue;
667273779Sbapt			for (i = 0; grp->gr_mem[i] != NULL; i++) {
668273779Sbapt				if (strcmp(grp->gr_mem[i] , pwd->pw_name) != 0)
669273779Sbapt					continue;
670273779Sbapt				for (j = i; grp->gr_mem[j] != NULL ; j++)
671273779Sbapt					grp->gr_mem[j] = grp->gr_mem[j+1];
672273779Sbapt				strlcpy(group, grp->gr_name, MAXLOGNAME);
673273779Sbapt				chggrent(group, grp);
674273779Sbapt			}
675273779Sbapt		}
676273779Sbapt		ENDGRENT();
677273779Sbapt
678273779Sbapt		/* now add to group where needed */
679242349Sbapt		for (i = 0; cnf->groups[i] != NULL; i++) {
680242349Sbapt			grp = GETGRNAM(cnf->groups[i]);
681244737Sbapt			grp = gr_add(grp, pwd->pw_name);
682244737Sbapt			/*
683244737Sbapt			 * grp can only be NULL in 2 cases:
684244737Sbapt			 * - the new member is already a member
685244737Sbapt			 * - a problem with memory occurs
686244737Sbapt			 * in both cases we want to skip now.
687244737Sbapt			 */
688244737Sbapt			if (grp == NULL)
689242349Sbapt				continue;
690242349Sbapt			chggrent(cnf->groups[i], grp);
691245114Smjg			free(grp);
692242349Sbapt		}
693242349Sbapt	}
694242349Sbapt
695242349Sbapt
69661759Sdavidn	/* go get a current version of pwd */
69761759Sdavidn	pwd = GETPWNAM(a_name->val);
69861759Sdavidn	if (pwd == NULL) {
69961759Sdavidn		/* This will fail when we rename, so special case that */
70061759Sdavidn		if (mode == M_UPDATE && (arg = getarg(args, 'l')) != NULL) {
70161759Sdavidn			a_name->val = arg->val;		/* update new name */
70261759Sdavidn			pwd = GETPWNAM(a_name->val);	/* refetch renamed rec */
70361759Sdavidn		}
70461759Sdavidn	}
70561759Sdavidn	if (pwd == NULL)	/* can't go on without this */
70630259Scharnier		errx(EX_NOUSER, "user '%s' disappeared during update", a_name->val);
70720253Sjoerg
70844229Sdavidn	grp = GETGRGID(pwd->pw_gid);
709283842Sbapt	pw_log(cnf, mode, W_USER, "%s(%u):%s(%u):%s:%s:%s",
710283842Sbapt	       pwd->pw_name, pwd->pw_uid,
711283842Sbapt	    grp ? grp->gr_name : "unknown", (grp ? grp->gr_gid : (uid_t)-1),
71220253Sjoerg	       pwd->pw_gecos, pwd->pw_dir, pwd->pw_shell);
71320253Sjoerg
71420253Sjoerg	/*
71520253Sjoerg	 * If adding, let's touch and chown the user's mail file. This is not
71620253Sjoerg	 * strictly necessary under BSD with a 0755 maildir but it also
71720253Sjoerg	 * doesn't hurt anything to create the empty mailfile
71820253Sjoerg	 */
71920253Sjoerg	if (mode == M_ADD) {
720283961Sbapt		if (PWALTDIR() != PWF_ALT) {
721283961Sbapt			arg = getarg(args, 'R');
722283961Sbapt			snprintf(path, sizeof(path), "%s%s/%s",
723283961Sbapt			    arg ? arg->val : "", _PATH_MAILDIR, pwd->pw_name);
724283961Sbapt			close(open(path, O_RDWR | O_CREAT, 0600));	/* Preserve contents &
72544229Sdavidn									 * mtime */
726283961Sbapt			chown(path, pwd->pw_uid, pwd->pw_gid);
72720253Sjoerg		}
72820253Sjoerg	}
72952527Sdavidn
73020253Sjoerg	/*
73182868Sdd	 * Let's create and populate the user's home directory. Note
73220253Sjoerg	 * that this also `works' for editing users if -m is used, but
73320253Sjoerg	 * existing files will *not* be overwritten.
73420253Sjoerg	 */
735283961Sbapt	if (PWALTDIR() != PWF_ALT && getarg(args, 'm') != NULL && pwd->pw_dir &&
736283961Sbapt	    *pwd->pw_dir == '/' && pwd->pw_dir[1])
737284118Sbapt		create_and_populate_homedir(mode, pwd);
73852527Sdavidn
73982868Sdd	/*
74082868Sdd	 * Finally, send mail to the new user as well, if we are asked to
74182868Sdd	 */
74282868Sdd	if (mode == M_ADD && !PWALTDIR() && cnf->newmail && *cnf->newmail && (fp = fopen(cnf->newmail, "r")) != NULL) {
74382868Sdd		FILE           *pfp = popen(_PATH_SENDMAIL " -t", "w");
74482868Sdd
74582868Sdd		if (pfp == NULL)
74682868Sdd			warn("sendmail");
74782868Sdd		else {
74882868Sdd			fprintf(pfp, "From: root\n" "To: %s\n" "Subject: Welcome!\n\n", pwd->pw_name);
74982868Sdd			while (fgets(line, sizeof(line), fp) != NULL) {
75082868Sdd				/* Do substitutions? */
75182868Sdd				fputs(line, pfp);
75282868Sdd			}
75382868Sdd			pclose(pfp);
754283842Sbapt			pw_log(cnf, mode, W_USER, "%s(%u) new user mail sent",
755283842Sbapt			    pwd->pw_name, pwd->pw_uid);
75682868Sdd		}
75782868Sdd		fclose(fp);
75882868Sdd	}
75982868Sdd
76020267Sjoerg	return EXIT_SUCCESS;
76120253Sjoerg}
76220253Sjoerg
76320253Sjoerg
76420253Sjoergstatic          uid_t
76520253Sjoergpw_uidpolicy(struct userconf * cnf, struct cargs * args)
76620253Sjoerg{
76720253Sjoerg	struct passwd  *pwd;
76820253Sjoerg	uid_t           uid = (uid_t) - 1;
76920253Sjoerg	struct carg    *a_uid = getarg(args, 'u');
77020253Sjoerg
77120253Sjoerg	/*
77220253Sjoerg	 * Check the given uid, if any
77320253Sjoerg	 */
77420253Sjoerg	if (a_uid != NULL) {
77520253Sjoerg		uid = (uid_t) atol(a_uid->val);
77620253Sjoerg
77744229Sdavidn		if ((pwd = GETPWUID(uid)) != NULL && getarg(args, 'o') == NULL)
778283842Sbapt			errx(EX_DATAERR, "uid `%u' has already been allocated", pwd->pw_uid);
77920253Sjoerg	} else {
78020253Sjoerg		struct bitmap   bm;
78120253Sjoerg
78220253Sjoerg		/*
78320253Sjoerg		 * We need to allocate the next available uid under one of
78420253Sjoerg		 * two policies a) Grab the first unused uid b) Grab the
78520253Sjoerg		 * highest possible unused uid
78620253Sjoerg		 */
78720253Sjoerg		if (cnf->min_uid >= cnf->max_uid) {	/* Sanity
78820253Sjoerg							 * claus^H^H^H^Hheck */
78920253Sjoerg			cnf->min_uid = 1000;
79020253Sjoerg			cnf->max_uid = 32000;
79120253Sjoerg		}
79220253Sjoerg		bm = bm_alloc(cnf->max_uid - cnf->min_uid + 1);
79320253Sjoerg
79420253Sjoerg		/*
79520253Sjoerg		 * Now, let's fill the bitmap from the password file
79620253Sjoerg		 */
79744229Sdavidn		SETPWENT();
79844229Sdavidn		while ((pwd = GETPWENT()) != NULL)
79944229Sdavidn			if (pwd->pw_uid >= (uid_t) cnf->min_uid && pwd->pw_uid <= (uid_t) cnf->max_uid)
80020253Sjoerg				bm_setbit(&bm, pwd->pw_uid - cnf->min_uid);
80144229Sdavidn		ENDPWENT();
80220253Sjoerg
80320253Sjoerg		/*
80420253Sjoerg		 * Then apply the policy, with fallback to reuse if necessary
80520253Sjoerg		 */
80620253Sjoerg		if (cnf->reuse_uids || (uid = (uid_t) (bm_lastset(&bm) + cnf->min_uid + 1)) > cnf->max_uid)
80720253Sjoerg			uid = (uid_t) (bm_firstunset(&bm) + cnf->min_uid);
80820253Sjoerg
80920253Sjoerg		/*
81020253Sjoerg		 * Another sanity check
81120253Sjoerg		 */
81220253Sjoerg		if (uid < cnf->min_uid || uid > cnf->max_uid)
81330259Scharnier			errx(EX_SOFTWARE, "unable to allocate a new uid - range fully used");
81420253Sjoerg		bm_dealloc(&bm);
81520253Sjoerg	}
81620253Sjoerg	return uid;
81720253Sjoerg}
81820253Sjoerg
81920253Sjoerg
82020253Sjoergstatic          uid_t
821284118Sbaptpw_gidpolicy(struct cargs * args, char *nam, gid_t prefer)
82220253Sjoerg{
82320253Sjoerg	struct group   *grp;
82420253Sjoerg	gid_t           gid = (uid_t) - 1;
82520253Sjoerg	struct carg    *a_gid = getarg(args, 'g');
826284118Sbapt	struct userconf	*cnf = conf.userconf;
82720253Sjoerg
82820253Sjoerg	/*
82920253Sjoerg	 * If no arg given, see if default can help out
83020253Sjoerg	 */
83120253Sjoerg	if (a_gid == NULL && cnf->default_group && *cnf->default_group)
83220253Sjoerg		a_gid = addarg(args, 'g', cnf->default_group);
83320253Sjoerg
83420253Sjoerg	/*
83520253Sjoerg	 * Check the given gid, if any
83620253Sjoerg	 */
83744229Sdavidn	SETGRENT();
83820253Sjoerg	if (a_gid != NULL) {
83944229Sdavidn		if ((grp = GETGRNAM(a_gid->val)) == NULL) {
84020253Sjoerg			gid = (gid_t) atol(a_gid->val);
84161957Sache			if ((gid == 0 && !isdigit((unsigned char)*a_gid->val)) || (grp = GETGRGID(gid)) == NULL)
84230259Scharnier				errx(EX_NOUSER, "group `%s' is not defined", a_gid->val);
84320253Sjoerg		}
84420253Sjoerg		gid = grp->gr_gid;
845262865Sjulian	} else if ((grp = GETGRNAM(nam)) != NULL &&
846262865Sjulian	    (grp->gr_mem == NULL || grp->gr_mem[0] == NULL)) {
84720267Sjoerg		gid = grp->gr_gid;  /* Already created? Use it anyway... */
84820253Sjoerg	} else {
84920253Sjoerg		struct cargs    grpargs;
85020253Sjoerg		char            tmp[32];
85120253Sjoerg
85220253Sjoerg		LIST_INIT(&grpargs);
85320253Sjoerg		addarg(&grpargs, 'n', nam);
85420253Sjoerg
85520253Sjoerg		/*
85620253Sjoerg		 * We need to auto-create a group with the user's name. We
85720253Sjoerg		 * can send all the appropriate output to our sister routine
85820253Sjoerg		 * bit first see if we can create a group with gid==uid so we
85920253Sjoerg		 * can keep the user and group ids in sync. We purposely do
86020253Sjoerg		 * NOT check the gid range if we can force the sync. If the
86120253Sjoerg		 * user's name dups an existing group, then the group add
86220253Sjoerg		 * function will happily handle that case for us and exit.
86320253Sjoerg		 */
86444229Sdavidn		if (GETGRGID(prefer) == NULL) {
865282700Sbapt			snprintf(tmp, sizeof(tmp), "%u", prefer);
86620253Sjoerg			addarg(&grpargs, 'g', tmp);
86720253Sjoerg		}
868284121Sbapt		if (conf.dryrun) {
86920267Sjoerg			addarg(&grpargs, 'q', NULL);
870284118Sbapt			gid = pw_group(M_NEXT, &grpargs);
87120267Sjoerg		}
87220267Sjoerg		else
87320267Sjoerg		{
874284118Sbapt			pw_group(M_ADD, &grpargs);
87544229Sdavidn			if ((grp = GETGRNAM(nam)) != NULL)
87620267Sjoerg				gid = grp->gr_gid;
87720267Sjoerg		}
87870486Sben		a_gid = LIST_FIRST(&grpargs);
87920253Sjoerg		while (a_gid != NULL) {
88070486Sben			struct carg    *t = LIST_NEXT(a_gid, list);
88120253Sjoerg			LIST_REMOVE(a_gid, list);
88220253Sjoerg			a_gid = t;
88320253Sjoerg		}
88420253Sjoerg	}
88544229Sdavidn	ENDGRENT();
88620253Sjoerg	return gid;
88720253Sjoerg}
88820253Sjoerg
88920253Sjoerg
89020253Sjoergstatic          time_t
89120253Sjoergpw_pwdpolicy(struct userconf * cnf, struct cargs * args)
89220253Sjoerg{
89320253Sjoerg	time_t          result = 0;
89420253Sjoerg	time_t          now = time(NULL);
89527831Sdavidn	struct carg    *arg = getarg(args, 'p');
89620253Sjoerg
89720253Sjoerg	if (arg != NULL) {
89820253Sjoerg		if ((result = parse_date(now, arg->val)) == now)
89930259Scharnier			errx(EX_DATAERR, "invalid date/time `%s'", arg->val);
90020253Sjoerg	} else if (cnf->password_days > 0)
90120253Sjoerg		result = now + ((long) cnf->password_days * 86400L);
90220253Sjoerg	return result;
90320253Sjoerg}
90420253Sjoerg
90520253Sjoerg
90620253Sjoergstatic          time_t
90720253Sjoergpw_exppolicy(struct userconf * cnf, struct cargs * args)
90820253Sjoerg{
90920253Sjoerg	time_t          result = 0;
91020253Sjoerg	time_t          now = time(NULL);
91120253Sjoerg	struct carg    *arg = getarg(args, 'e');
91220253Sjoerg
91320253Sjoerg	if (arg != NULL) {
91420253Sjoerg		if ((result = parse_date(now, arg->val)) == now)
91530259Scharnier			errx(EX_DATAERR, "invalid date/time `%s'", arg->val);
91620253Sjoerg	} else if (cnf->expire_days > 0)
91720253Sjoerg		result = now + ((long) cnf->expire_days * 86400L);
91820253Sjoerg	return result;
91920253Sjoerg}
92020253Sjoerg
92120253Sjoerg
92220253Sjoergstatic char    *
92320253Sjoergpw_homepolicy(struct userconf * cnf, struct cargs * args, char const * user)
92420253Sjoerg{
92520253Sjoerg	struct carg    *arg = getarg(args, 'd');
926282699Sbapt	static char     home[128];
92720253Sjoerg
92820253Sjoerg	if (arg)
929282699Sbapt		return (arg->val);
93020253Sjoerg
931282699Sbapt	if (cnf->home == NULL || *cnf->home == '\0')
932282699Sbapt		errx(EX_CONFIG, "no base home directory set");
933282699Sbapt	snprintf(home, sizeof(home), "%s/%s", cnf->home, user);
934282699Sbapt
935282699Sbapt	return (home);
93620253Sjoerg}
93720253Sjoerg
93820253Sjoergstatic char    *
93920253Sjoergshell_path(char const * path, char *shells[], char *sh)
94020253Sjoerg{
94120253Sjoerg	if (sh != NULL && (*sh == '/' || *sh == '\0'))
94220253Sjoerg		return sh;	/* specified full path or forced none */
94320253Sjoerg	else {
94420253Sjoerg		char           *p;
94520253Sjoerg		char            paths[_UC_MAXLINE];
94620253Sjoerg
94720253Sjoerg		/*
94820253Sjoerg		 * We need to search paths
94920253Sjoerg		 */
950130633Srobert		strlcpy(paths, path, sizeof(paths));
95120253Sjoerg		for (p = strtok(paths, ": \t\r\n"); p != NULL; p = strtok(NULL, ": \t\r\n")) {
95220253Sjoerg			int             i;
95320253Sjoerg			static char     shellpath[256];
95420253Sjoerg
95520253Sjoerg			if (sh != NULL) {
956282700Sbapt				snprintf(shellpath, sizeof(shellpath), "%s/%s", p, sh);
95720253Sjoerg				if (access(shellpath, X_OK) == 0)
95820253Sjoerg					return shellpath;
95920253Sjoerg			} else
96020253Sjoerg				for (i = 0; i < _UC_MAXSHELLS && shells[i] != NULL; i++) {
961282700Sbapt					snprintf(shellpath, sizeof(shellpath), "%s/%s", p, shells[i]);
96220253Sjoerg					if (access(shellpath, X_OK) == 0)
96320253Sjoerg						return shellpath;
96420253Sjoerg				}
96520253Sjoerg		}
96620253Sjoerg		if (sh == NULL)
96730259Scharnier			errx(EX_OSFILE, "can't find shell `%s' in shell paths", sh);
96830259Scharnier		errx(EX_CONFIG, "no default shell available or defined");
96920253Sjoerg		return NULL;
97020253Sjoerg	}
97120253Sjoerg}
97220253Sjoerg
97320253Sjoerg
97420253Sjoergstatic char    *
97520253Sjoergpw_shellpolicy(struct userconf * cnf, struct cargs * args, char *newshell)
97620253Sjoerg{
97720253Sjoerg	char           *sh = newshell;
97820253Sjoerg	struct carg    *arg = getarg(args, 's');
97920253Sjoerg
98020253Sjoerg	if (newshell == NULL && arg != NULL)
98120253Sjoerg		sh = arg->val;
98220253Sjoerg	return shell_path(cnf->shelldir, cnf->shells, sh ? sh : cnf->shell_default);
98320253Sjoerg}
98420253Sjoerg
985179365Santoine#define	SALTSIZE	32
98620253Sjoerg
987179365Santoinestatic char const chars[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ./";
988179365Santoine
98920253Sjoergchar           *
99020253Sjoergpw_pwcrypt(char *password)
99120253Sjoerg{
99220253Sjoerg	int             i;
993179365Santoine	char            salt[SALTSIZE + 1];
994231994Skevlo	char		*cryptpw;
99520253Sjoerg
99620253Sjoerg	static char     buf[256];
99720253Sjoerg
99820253Sjoerg	/*
99920253Sjoerg	 * Calculate a salt value
100020253Sjoerg	 */
1001179365Santoine	for (i = 0; i < SALTSIZE; i++)
1002181785Sache		salt[i] = chars[arc4random_uniform(sizeof(chars) - 1)];
1003179365Santoine	salt[SALTSIZE] = '\0';
100420253Sjoerg
1005231994Skevlo	cryptpw = crypt(password, salt);
1006231994Skevlo	if (cryptpw == NULL)
1007231994Skevlo		errx(EX_CONFIG, "crypt(3) failure");
1008231994Skevlo	return strcpy(buf, cryptpw);
100920253Sjoerg}
101020253Sjoerg
101120590Sdavidn
101220253Sjoergstatic char    *
101320253Sjoergpw_password(struct userconf * cnf, struct cargs * args, char const * user)
101420253Sjoerg{
101520253Sjoerg	int             i, l;
101620253Sjoerg	char            pwbuf[32];
101720253Sjoerg
101820253Sjoerg	switch (cnf->default_password) {
101920253Sjoerg	case -1:		/* Random password */
102073563Skris		l = (arc4random() % 8 + 8);	/* 8 - 16 chars */
102120253Sjoerg		for (i = 0; i < l; i++)
1022181785Sache			pwbuf[i] = chars[arc4random_uniform(sizeof(chars)-1)];
102320253Sjoerg		pwbuf[i] = '\0';
102420253Sjoerg
102520253Sjoerg		/*
102620253Sjoerg		 * We give this information back to the user
102720253Sjoerg		 */
1028124382Siedowse		if (getarg(args, 'h') == NULL && getarg(args, 'H') == NULL &&
1029284121Sbapt		    !conf.dryrun) {
103061957Sache			if (isatty(STDOUT_FILENO))
103120712Sdavidn				printf("Password for '%s' is: ", user);
103220253Sjoerg			printf("%s\n", pwbuf);
103320253Sjoerg			fflush(stdout);
103420253Sjoerg		}
103520253Sjoerg		break;
103620253Sjoerg
103720253Sjoerg	case -2:		/* No password at all! */
103820253Sjoerg		return "";
103920253Sjoerg
104020253Sjoerg	case 0:		/* No login - default */
104120253Sjoerg	default:
104220253Sjoerg		return "*";
104320253Sjoerg
104420253Sjoerg	case 1:		/* user's name */
1045130633Srobert		strlcpy(pwbuf, user, sizeof(pwbuf));
104620253Sjoerg		break;
104720253Sjoerg	}
104820253Sjoerg	return pw_pwcrypt(pwbuf);
104920253Sjoerg}
105020253Sjoerg
1051284111Sbaptstatic int
1052284111Sbaptdelete_user(struct userconf *cnf, struct passwd *pwd, struct carg *a_name,
1053284111Sbapt    int delete, int mode)
1054284111Sbapt{
1055284111Sbapt	char		 file[MAXPATHLEN];
1056284111Sbapt	char		 home[MAXPATHLEN];
1057284111Sbapt	uid_t		 uid = pwd->pw_uid;
1058284111Sbapt	struct group	*gr, *grp;
1059284111Sbapt	char		 grname[LOGNAMESIZE];
1060284111Sbapt	int		 rc;
1061284111Sbapt	struct stat	 st;
106220253Sjoerg
1063284111Sbapt	if (strcmp(pwd->pw_name, "root") == 0)
1064284111Sbapt		errx(EX_DATAERR, "cannot remove user 'root'");
1065284111Sbapt
1066284111Sbapt	if (!PWALTDIR()) {
1067284111Sbapt		/*
1068284111Sbapt		 * Remove opie record from /etc/opiekeys
1069284111Sbapt		*/
1070284111Sbapt
1071284111Sbapt		rmopie(pwd->pw_name);
1072284111Sbapt
1073284111Sbapt		/*
1074284111Sbapt		 * Remove crontabs
1075284111Sbapt		 */
1076284111Sbapt		snprintf(file, sizeof(file), "/var/cron/tabs/%s", pwd->pw_name);
1077284111Sbapt		if (access(file, F_OK) == 0) {
1078284111Sbapt			snprintf(file, sizeof(file), "crontab -u %s -r", pwd->pw_name);
1079284111Sbapt			system(file);
1080284111Sbapt		}
1081284111Sbapt	}
1082284111Sbapt	/*
1083284111Sbapt	 * Save these for later, since contents of pwd may be
1084284111Sbapt	 * invalidated by deletion
1085284111Sbapt	 */
1086284111Sbapt	snprintf(file, sizeof(file), "%s/%s", _PATH_MAILDIR, pwd->pw_name);
1087284111Sbapt	strlcpy(home, pwd->pw_dir, sizeof(home));
1088284111Sbapt	gr = GETGRGID(pwd->pw_gid);
1089284111Sbapt	if (gr != NULL)
1090284111Sbapt		strlcpy(grname, gr->gr_name, LOGNAMESIZE);
1091284111Sbapt	else
1092284111Sbapt		grname[0] = '\0';
1093284111Sbapt
1094284111Sbapt	rc = delpwent(pwd);
1095284111Sbapt	if (rc == -1)
1096284111Sbapt		err(EX_IOERR, "user '%s' does not exist", pwd->pw_name);
1097284111Sbapt	else if (rc != 0)
1098284111Sbapt		err(EX_IOERR, "passwd update");
1099284111Sbapt
1100284111Sbapt	if (cnf->nispasswd && *cnf->nispasswd=='/') {
1101284111Sbapt		rc = delnispwent(cnf->nispasswd, a_name->val);
1102284111Sbapt		if (rc == -1)
1103284111Sbapt			warnx("WARNING: user '%s' does not exist in NIS passwd", pwd->pw_name);
1104284111Sbapt		else if (rc != 0)
1105284111Sbapt			warn("WARNING: NIS passwd update");
1106284111Sbapt		/* non-fatal */
1107284111Sbapt	}
1108284111Sbapt
1109284111Sbapt	grp = GETGRNAM(a_name->val);
1110284111Sbapt	if (grp != NULL &&
1111284111Sbapt	    (grp->gr_mem == NULL || *grp->gr_mem == NULL) &&
1112284111Sbapt	    strcmp(a_name->val, grname) == 0)
1113284111Sbapt		delgrent(GETGRNAM(a_name->val));
1114284111Sbapt	SETGRENT();
1115284111Sbapt	while ((grp = GETGRENT()) != NULL) {
1116284111Sbapt		int i, j;
1117284111Sbapt		char group[MAXLOGNAME];
1118284113Sbapt		if (grp->gr_mem == NULL)
1119284113Sbapt			continue;
1120284113Sbapt
1121284113Sbapt		for (i = 0; grp->gr_mem[i] != NULL; i++) {
1122284114Sbapt			if (strcmp(grp->gr_mem[i], a_name->val) != 0)
1123284113Sbapt				continue;
1124284113Sbapt
1125284113Sbapt			for (j = i; grp->gr_mem[j] != NULL; j++)
1126284113Sbapt				grp->gr_mem[j] = grp->gr_mem[j+1];
1127284113Sbapt			strlcpy(group, grp->gr_name, MAXLOGNAME);
1128284113Sbapt			chggrent(group, grp);
1129284111Sbapt		}
1130284111Sbapt	}
1131284111Sbapt	ENDGRENT();
1132284111Sbapt
1133284111Sbapt	pw_log(cnf, mode, W_USER, "%s(%u) account removed", a_name->val, uid);
1134284111Sbapt
1135284117Sbapt	if (!PWALTDIR()) {
1136284111Sbapt		/*
1137284111Sbapt		 * Remove mail file
1138284111Sbapt		 */
1139284111Sbapt		remove(file);
1140284111Sbapt
1141284111Sbapt		/*
1142284111Sbapt		 * Remove at jobs
1143284111Sbapt		 */
1144284111Sbapt		if (getpwuid(uid) == NULL)
1145284111Sbapt			rmat(uid);
1146284111Sbapt
1147284111Sbapt		/*
1148284111Sbapt		 * Remove home directory and contents
1149284111Sbapt		 */
1150284112Sbapt		if (delete && *home == '/' && getpwuid(uid) == NULL &&
1151284112Sbapt		    stat(home, &st) != -1) {
1152284112Sbapt			rm_r(home, uid);
1153284112Sbapt			pw_log(cnf, mode, W_USER, "%s(%u) home '%s' %sremoved",
1154284112Sbapt			       a_name->val, uid, home,
1155284112Sbapt			       stat(home, &st) == -1 ? "" : "not completely ");
1156284111Sbapt		}
1157284111Sbapt	}
1158284111Sbapt
1159284111Sbapt	return (EXIT_SUCCESS);
1160284111Sbapt}
1161284111Sbapt
116220253Sjoergstatic int
1163284124Sbaptprint_user(struct passwd * pwd)
116420253Sjoerg{
1165284122Sbapt	if (!conf.pretty) {
1166242349Sbapt		char            *buf;
116720253Sjoerg
1168284124Sbapt		if (!conf.v7)
1169242349Sbapt			pwd->pw_passwd = (pwd->pw_passwd == NULL) ? "" : "*";
1170242349Sbapt
1171284124Sbapt		buf = conf.v7 ? pw_make_v7(pwd) : pw_make(pwd);
1172242349Sbapt		printf("%s\n", buf);
1173242349Sbapt		free(buf);
117420253Sjoerg	} else {
117520267Sjoerg		int		j;
117620253Sjoerg		char           *p;
117744229Sdavidn		struct group   *grp = GETGRGID(pwd->pw_gid);
117820253Sjoerg		char            uname[60] = "User &", office[60] = "[None]",
117920253Sjoerg		                wphone[60] = "[None]", hphone[60] = "[None]";
118020590Sdavidn		char		acexpire[32] = "[None]", pwexpire[32] = "[None]";
118120590Sdavidn		struct tm *    tptr;
118220253Sjoerg
118320253Sjoerg		if ((p = strtok(pwd->pw_gecos, ",")) != NULL) {
1184130633Srobert			strlcpy(uname, p, sizeof(uname));
118520253Sjoerg			if ((p = strtok(NULL, ",")) != NULL) {
1186130633Srobert				strlcpy(office, p, sizeof(office));
118720253Sjoerg				if ((p = strtok(NULL, ",")) != NULL) {
1188130633Srobert					strlcpy(wphone, p, sizeof(wphone));
118920253Sjoerg					if ((p = strtok(NULL, "")) != NULL) {
1190130633Srobert						strlcpy(hphone, p,
1191130633Srobert						    sizeof(hphone));
119220253Sjoerg					}
119320253Sjoerg				}
119420253Sjoerg			}
119520253Sjoerg		}
119620253Sjoerg		/*
119720253Sjoerg		 * Handle '&' in gecos field
119820253Sjoerg		 */
119920253Sjoerg		if ((p = strchr(uname, '&')) != NULL) {
120020253Sjoerg			int             l = strlen(pwd->pw_name);
120120253Sjoerg			int             m = strlen(p);
120220253Sjoerg
120320253Sjoerg			memmove(p + l, p + 1, m);
120420253Sjoerg			memmove(p, pwd->pw_name, l);
120561957Sache			*p = (char) toupper((unsigned char)*p);
120620253Sjoerg		}
120720590Sdavidn		if (pwd->pw_expire > (time_t)0 && (tptr = localtime(&pwd->pw_expire)) != NULL)
120874569Sache			strftime(acexpire, sizeof acexpire, "%c", tptr);
120961957Sache		if (pwd->pw_change > (time_t)0 && (tptr = localtime(&pwd->pw_change)) != NULL)
121074569Sache			strftime(pwexpire, sizeof pwexpire, "%c", tptr);
1211283842Sbapt		printf("Login Name: %-15s   #%-12u Group: %-15s   #%u\n"
121220747Sdavidn		       " Full Name: %s\n"
121320747Sdavidn		       "      Home: %-26.26s      Class: %s\n"
121420747Sdavidn		       "     Shell: %-26.26s     Office: %s\n"
121520747Sdavidn		       "Work Phone: %-26.26s Home Phone: %s\n"
121620747Sdavidn		       "Acc Expire: %-26.26s Pwd Expire: %s\n",
1217283842Sbapt		       pwd->pw_name, pwd->pw_uid,
1218283842Sbapt		       grp ? grp->gr_name : "(invalid)", pwd->pw_gid,
121920253Sjoerg		       uname, pwd->pw_dir, pwd->pw_class,
122020590Sdavidn		       pwd->pw_shell, office, wphone, hphone,
122120590Sdavidn		       acexpire, pwexpire);
122244229Sdavidn	        SETGRENT();
122320267Sjoerg		j = 0;
122444229Sdavidn		while ((grp=GETGRENT()) != NULL)
122520267Sjoerg		{
122620267Sjoerg			int     i = 0;
1227262865Sjulian			if (grp->gr_mem != NULL) {
1228262865Sjulian				while (grp->gr_mem[i] != NULL)
122920267Sjoerg				{
1230262865Sjulian					if (strcmp(grp->gr_mem[i], pwd->pw_name)==0)
1231262865Sjulian					{
1232262865Sjulian						printf(j++ == 0 ? "    Groups: %s" : ",%s", grp->gr_name);
1233262865Sjulian						break;
1234262865Sjulian					}
1235262865Sjulian					++i;
123620267Sjoerg				}
123720267Sjoerg			}
123820267Sjoerg		}
123944229Sdavidn		ENDGRENT();
124061957Sache		printf("%s", j ? "\n" : "");
124120253Sjoerg	}
124220267Sjoerg	return EXIT_SUCCESS;
124320253Sjoerg}
124420253Sjoerg
1245284110Sbaptchar *
1246284110Sbaptpw_checkname(char *name, int gecos)
124720253Sjoerg{
1248109961Sgad	char showch[8];
1249284110Sbapt	const char *badchars, *ch, *showtype;
1250109961Sgad	int reject;
125120253Sjoerg
1252109961Sgad	ch = name;
1253109961Sgad	reject = 0;
1254109961Sgad	if (gecos) {
1255109961Sgad		/* See if the name is valid as a gecos (comment) field. */
1256109961Sgad		badchars = ":!@";
1257109961Sgad		showtype = "gecos field";
1258109961Sgad	} else {
1259109961Sgad		/* See if the name is valid as a userid or group. */
1260109961Sgad		badchars = " ,\t:+&#%$^()!@~*?<>=|\\/\"";
1261109961Sgad		showtype = "userid/group name";
1262109961Sgad		/* Userids and groups can not have a leading '-'. */
1263109961Sgad		if (*ch == '-')
1264109961Sgad			reject = 1;
126520253Sjoerg	}
1266109961Sgad	if (!reject) {
1267109961Sgad		while (*ch) {
1268109961Sgad			if (strchr(badchars, *ch) != NULL || *ch < ' ' ||
1269109961Sgad			    *ch == 127) {
1270109961Sgad				reject = 1;
1271109961Sgad				break;
1272109961Sgad			}
1273109961Sgad			/* 8-bit characters are only allowed in GECOS fields */
1274109961Sgad			if (!gecos && (*ch & 0x80)) {
1275109961Sgad				reject = 1;
1276109961Sgad				break;
1277109961Sgad			}
1278109961Sgad			ch++;
1279109961Sgad		}
1280109961Sgad	}
1281109961Sgad	/*
1282109961Sgad	 * A `$' is allowed as the final character for userids and groups,
1283109961Sgad	 * mainly for the benefit of samba.
1284109961Sgad	 */
1285109961Sgad	if (reject && !gecos) {
1286109961Sgad		if (*ch == '$' && *(ch + 1) == '\0') {
1287109961Sgad			reject = 0;
1288109961Sgad			ch++;
1289109961Sgad		}
1290109961Sgad	}
1291109961Sgad	if (reject) {
1292109961Sgad		snprintf(showch, sizeof(showch), (*ch >= ' ' && *ch < 127)
1293109961Sgad		    ? "`%c'" : "0x%02x", *ch);
1294228673Sdim		errx(EX_DATAERR, "invalid character %s at position %td in %s",
1295109961Sgad		    showch, (ch - name), showtype);
1296109961Sgad	}
1297109961Sgad	if (!gecos && (ch - name) > LOGNAMESIZE)
1298109961Sgad		errx(EX_DATAERR, "name too long `%s' (max is %d)", name,
1299109961Sgad		    LOGNAMESIZE);
1300284110Sbapt
1301284110Sbapt	return (name);
130220253Sjoerg}
130320253Sjoerg
130420253Sjoerg
130520253Sjoergstatic void
130620253Sjoergrmat(uid_t uid)
130720253Sjoerg{
130820253Sjoerg	DIR            *d = opendir("/var/at/jobs");
130920253Sjoerg
131020253Sjoerg	if (d != NULL) {
131120253Sjoerg		struct dirent  *e;
131220253Sjoerg
131320253Sjoerg		while ((e = readdir(d)) != NULL) {
131420253Sjoerg			struct stat     st;
131520253Sjoerg
131620253Sjoerg			if (strncmp(e->d_name, ".lock", 5) != 0 &&
131720253Sjoerg			    stat(e->d_name, &st) == 0 &&
131820253Sjoerg			    !S_ISDIR(st.st_mode) &&
131920253Sjoerg			    st.st_uid == uid) {
132020253Sjoerg				char            tmp[MAXPATHLEN];
132120253Sjoerg
1322282700Sbapt				snprintf(tmp, sizeof(tmp), "/usr/bin/atrm %s", e->d_name);
132320253Sjoerg				system(tmp);
132420253Sjoerg			}
132520253Sjoerg		}
132620253Sjoerg		closedir(d);
132720253Sjoerg	}
132820253Sjoerg}
132920747Sdavidn
133020747Sdavidnstatic void
133185145Sachermopie(char const * name)
133220747Sdavidn{
133385145Sache	static const char etcopie[] = "/etc/opiekeys";
133485145Sache	FILE   *fp = fopen(etcopie, "r+");
133520747Sdavidn
133621052Sdavidn	if (fp != NULL) {
133721052Sdavidn		char	tmp[1024];
133821052Sdavidn		off_t	atofs = 0;
133921052Sdavidn		int	length = strlen(name);
134020747Sdavidn
134121052Sdavidn		while (fgets(tmp, sizeof tmp, fp) != NULL) {
134221052Sdavidn			if (strncmp(name, tmp, length) == 0 && tmp[length]==' ') {
134321052Sdavidn				if (fseek(fp, atofs, SEEK_SET) == 0) {
134421052Sdavidn					fwrite("#", 1, 1, fp);	/* Comment username out */
134521052Sdavidn				}
134621052Sdavidn				break;
134720747Sdavidn			}
134821052Sdavidn			atofs = ftell(fp);
134920747Sdavidn		}
135021052Sdavidn		/*
135121052Sdavidn		 * If we got an error of any sort, don't update!
135221052Sdavidn		 */
135321052Sdavidn		fclose(fp);
135420747Sdavidn	}
135520747Sdavidn}
135620747Sdavidn
1357