pw_user.c revision 285412
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 285412 2015-07-11 23:07:17Z 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
54285401Sbaptstatic int	pw_userdel(char *name, long id);
55284124Sbaptstatic int	print_user(struct passwd * pwd);
56284133Sbaptstatic uid_t    pw_uidpolicy(struct userconf * cnf, long id);
57284118Sbaptstatic uid_t    pw_gidpolicy(struct cargs * args, char *nam, gid_t prefer);
5820253Sjoergstatic time_t   pw_pwdpolicy(struct userconf * cnf, struct cargs * args);
5920253Sjoergstatic time_t   pw_exppolicy(struct userconf * cnf, struct cargs * args);
6020253Sjoergstatic char    *pw_homepolicy(struct userconf * cnf, struct cargs * args, char const * user);
6120253Sjoergstatic char    *pw_shellpolicy(struct userconf * cnf, struct cargs * args, char *newshell);
62285409Sbaptstatic char    *pw_password(struct userconf * cnf, char const * user);
6320253Sjoergstatic char    *shell_path(char const * path, char *shells[], char *sh);
6420253Sjoergstatic void     rmat(uid_t uid);
6585145Sachestatic void     rmopie(char const * name);
6620253Sjoerg
67283961Sbaptstatic void
68285403Sbaptcreate_and_populate_homedir(struct passwd *pwd)
69283961Sbapt{
70283961Sbapt	char *homedir, *dotdir;
71284118Sbapt	struct userconf *cnf = conf.userconf;
72283961Sbapt
73283961Sbapt	homedir = dotdir = NULL;
74283961Sbapt
75284118Sbapt	if (conf.rootdir[0] != '\0') {
76284118Sbapt		asprintf(&homedir, "%s/%s", conf.rootdir, pwd->pw_dir);
77283961Sbapt		if (homedir == NULL)
78283961Sbapt			errx(EX_OSERR, "out of memory");
79284118Sbapt		asprintf(&dotdir, "%s/%s", conf.rootdir, cnf->dotdir);
80283961Sbapt	}
81283961Sbapt
82283961Sbapt	copymkdir(homedir ? homedir : pwd->pw_dir, dotdir ? dotdir: cnf->dotdir,
83283961Sbapt	    cnf->homemode, pwd->pw_uid, pwd->pw_gid);
84285403Sbapt	pw_log(cnf, M_ADD, W_USER, "%s(%u) home %s made", pwd->pw_name,
85283961Sbapt	    pwd->pw_uid, pwd->pw_dir);
86283961Sbapt}
87283961Sbapt
88285133Sbaptstatic int
89285137Sbaptset_passwd(struct passwd *pwd, bool update)
90285133Sbapt{
91285133Sbapt	int		 b, istty;
92285133Sbapt	struct termios	 t, n;
93285133Sbapt	login_cap_t	*lc;
94285133Sbapt	char		line[_PASSWORD_LEN+1];
95285133Sbapt	char		*p;
96285133Sbapt
97285133Sbapt	if (conf.fd == '-') {
98285133Sbapt		if (!pwd->pw_passwd || *pwd->pw_passwd != '*') {
99285133Sbapt			pwd->pw_passwd = "*";	/* No access */
100285133Sbapt			return (1);
101285133Sbapt		}
102285133Sbapt		return (0);
103285133Sbapt	}
104285133Sbapt
105285133Sbapt	if ((istty = isatty(conf.fd))) {
106285133Sbapt		if (tcgetattr(conf.fd, &t) == -1)
107285133Sbapt			istty = 0;
108285133Sbapt		else {
109285137Sbapt			n = t;
110285133Sbapt			n.c_lflag &= ~(ECHO);
111285133Sbapt			tcsetattr(conf.fd, TCSANOW, &n);
112285133Sbapt			printf("%s%spassword for user %s:",
113285133Sbapt			    update ? "new " : "",
114285133Sbapt			    conf.precrypted ? "encrypted " : "",
115285133Sbapt			    pwd->pw_name);
116285133Sbapt			fflush(stdout);
117285133Sbapt		}
118285133Sbapt	}
119285133Sbapt	b = read(conf.fd, line, sizeof(line) - 1);
120285133Sbapt	if (istty) {	/* Restore state */
121285133Sbapt		tcsetattr(conf.fd, TCSANOW, &t);
122285133Sbapt		fputc('\n', stdout);
123285133Sbapt		fflush(stdout);
124285133Sbapt	}
125285133Sbapt
126285133Sbapt	if (b < 0)
127285133Sbapt		err(EX_IOERR, "-%c file descriptor",
128285133Sbapt		    conf.precrypted ? 'H' : 'h');
129285133Sbapt	line[b] = '\0';
130285133Sbapt	if ((p = strpbrk(line, "\r\n")) != NULL)
131285133Sbapt		*p = '\0';
132285133Sbapt	if (!*line)
133285133Sbapt		errx(EX_DATAERR, "empty password read on file descriptor %d",
134285133Sbapt		    conf.fd);
135285133Sbapt	if (conf.precrypted) {
136285133Sbapt		if (strchr(line, ':') != NULL)
137285137Sbapt			errx(EX_DATAERR, "bad encrypted password");
138285133Sbapt		pwd->pw_passwd = line;
139285133Sbapt	} else {
140285133Sbapt		lc = login_getpwclass(pwd);
141285133Sbapt		if (lc == NULL ||
142285133Sbapt				login_setcryptfmt(lc, "sha512", NULL) == NULL)
143285133Sbapt			warn("setting crypt(3) format");
144285133Sbapt		login_close(lc);
145285133Sbapt		pwd->pw_passwd = pw_pwcrypt(line);
146285133Sbapt	}
147285133Sbapt	return (1);
148285133Sbapt}
149285133Sbapt
150285395Sbaptint
151285395Sbaptpw_usernext(struct userconf *cnf, bool quiet)
152285395Sbapt{
153285395Sbapt	uid_t next = pw_uidpolicy(cnf, -1);
154285395Sbapt
155285395Sbapt	if (quiet)
156285395Sbapt		return (next);
157285395Sbapt
158285395Sbapt	printf("%u:", next);
159285395Sbapt	pw_groupnext(cnf, quiet);
160285395Sbapt
161285395Sbapt	return (EXIT_SUCCESS);
162285395Sbapt}
163285395Sbapt
164285398Sbaptstatic int
165285398Sbaptpw_usershow(char *name, long id, struct passwd *fakeuser)
166285398Sbapt{
167285398Sbapt	struct passwd *pwd = NULL;
168285398Sbapt
169285398Sbapt	if (id < 0 && name == NULL && !conf.all)
170285398Sbapt		errx(EX_DATAERR, "username or id or '-a' required");
171285398Sbapt
172285398Sbapt	if (conf.all) {
173285398Sbapt		SETPWENT();
174285398Sbapt		while ((pwd = GETPWENT()) != NULL)
175285398Sbapt			print_user(pwd);
176285398Sbapt		ENDPWENT();
177285398Sbapt		return (EXIT_SUCCESS);
178285398Sbapt	}
179285398Sbapt
180285398Sbapt	pwd = (name != NULL) ? GETPWNAM(pw_checkname(name, 0)) : GETPWUID(id);
181285398Sbapt	if (pwd == NULL) {
182285398Sbapt		if (conf.force) {
183285398Sbapt			pwd = fakeuser;
184285398Sbapt		} else {
185285398Sbapt			if (name == NULL)
186285398Sbapt				errx(EX_NOUSER, "no such uid `%ld'", id);
187285398Sbapt			errx(EX_NOUSER, "no such user `%s'", name);
188285398Sbapt		}
189285398Sbapt	}
190285398Sbapt
191285398Sbapt	return (print_user(pwd));
192285398Sbapt}
193285398Sbapt
194285405Sbaptstatic void
195285405Sbaptperform_chgpwent(const char *name, struct passwd *pwd)
196285405Sbapt{
197285405Sbapt	int rc;
198285405Sbapt
199285405Sbapt	rc = chgpwent(name, pwd);
200285405Sbapt	if (rc == -1)
201285405Sbapt		errx(EX_IOERR, "user '%s' does not exist (NIS?)", pwd->pw_name);
202285405Sbapt	else if (rc != 0)
203285405Sbapt		err(EX_IOERR, "passwd file update");
204285405Sbapt
205285405Sbapt	if (conf.userconf->nispasswd && *conf.userconf->nispasswd == '/') {
206285405Sbapt		rc = chgnispwent(conf.userconf->nispasswd, name, pwd);
207285405Sbapt		if (rc == -1)
208285405Sbapt			warn("User '%s' not found in NIS passwd", pwd->pw_name);
209285405Sbapt		else
210285405Sbapt			warn("NIS passwd update");
211285405Sbapt		/* NOTE: NIS-only update errors are not fatal */
212285405Sbapt	}
213285405Sbapt}
214285405Sbapt
215285405Sbapt/*
216285405Sbapt * The M_LOCK and M_UNLOCK functions simply add or remove
217285405Sbapt * a "*LOCKED*" prefix from in front of the password to
218285405Sbapt * prevent it decoding correctly, and therefore prevents
219285405Sbapt * access. Of course, this only prevents access via
220285405Sbapt * password authentication (not ssh, kerberos or any
221285405Sbapt * other method that does not use the UNIX password) but
222285405Sbapt * that is a known limitation.
223285405Sbapt */
224285405Sbaptstatic int
225285405Sbaptpw_userlock(char *name, long id, int mode)
226285405Sbapt{
227285405Sbapt	struct passwd *pwd = NULL;
228285405Sbapt	char *passtmp = NULL;
229285405Sbapt	bool locked = false;
230285405Sbapt
231285405Sbapt	if (id < 0 && name == NULL)
232285405Sbapt		errx(EX_DATAERR, "username or id required");
233285405Sbapt
234285405Sbapt	pwd = (name != NULL) ? GETPWNAM(pw_checkname(name, 0)) : GETPWUID(id);
235285405Sbapt	if (pwd == NULL) {
236285405Sbapt		if (name == NULL)
237285405Sbapt			errx(EX_NOUSER, "no such uid `%ld'", id);
238285405Sbapt		errx(EX_NOUSER, "no such user `%s'", name);
239285405Sbapt	}
240285405Sbapt
241285405Sbapt	if (name == NULL)
242285405Sbapt		name = pwd->pw_name;
243285405Sbapt
244285405Sbapt	if (strncmp(pwd->pw_passwd, locked_str, sizeof(locked_str) -1) == 0)
245285405Sbapt		locked = true;
246285405Sbapt	if (mode == M_LOCK && locked)
247285405Sbapt		errx(EX_DATAERR, "user '%s' is already locked", pwd->pw_name);
248285405Sbapt	if (mode == M_UNLOCK && !locked)
249285405Sbapt		errx(EX_DATAERR, "user '%s' is not locked", pwd->pw_name);
250285405Sbapt
251285405Sbapt	if (mode == M_LOCK) {
252285405Sbapt		asprintf(&passtmp, "%s%s", locked_str, pwd->pw_passwd);
253285405Sbapt		if (passtmp == NULL)	/* disaster */
254285405Sbapt			errx(EX_UNAVAILABLE, "out of memory");
255285405Sbapt		pwd->pw_passwd = passtmp;
256285405Sbapt	} else {
257285405Sbapt		pwd->pw_passwd += sizeof(locked_str)-1;
258285405Sbapt	}
259285405Sbapt
260285405Sbapt	perform_chgpwent(name, pwd);
261285405Sbapt	free(passtmp);
262285405Sbapt
263285405Sbapt	return (EXIT_SUCCESS);
264285405Sbapt}
265285405Sbapt
26620253Sjoerg/*-
26720253Sjoerg * -C config      configuration file
26820253Sjoerg * -q             quiet operation
26920253Sjoerg * -n name        login name
27020253Sjoerg * -u uid         user id
27120253Sjoerg * -c comment     user name/comment
27220253Sjoerg * -d directory   home directory
27320253Sjoerg * -e date        account expiry date
27420253Sjoerg * -p date        password expiry date
27520253Sjoerg * -g grp         primary group
27620253Sjoerg * -G grp1,grp2   additional groups
27720253Sjoerg * -m [ -k dir ]  create and set up home
27820253Sjoerg * -s shell       name of login shell
27920253Sjoerg * -o             duplicate uid ok
28020253Sjoerg * -L class       user class
28120253Sjoerg * -l name        new login name
28220253Sjoerg * -h fd          password filehandle
283124382Siedowse * -H fd          encrypted password filehandle
28420253Sjoerg * -F             force print or add
28520253Sjoerg *   Setting defaults:
28620253Sjoerg * -D             set user defaults
28720253Sjoerg * -b dir         default home root dir
28820253Sjoerg * -e period      default expiry period
28920253Sjoerg * -p period      default password change period
29020253Sjoerg * -g group       default group
29120253Sjoerg * -G             grp1,grp2.. default additional groups
29220253Sjoerg * -L class       default login class
29320253Sjoerg * -k dir         default home skeleton
29420253Sjoerg * -s shell       default shell
29520253Sjoerg * -w method      default password method
29620253Sjoerg */
29720253Sjoerg
29820253Sjoergint
299284128Sbaptpw_user(int mode, char *name, long id, struct cargs * args)
30020253Sjoerg{
30152527Sdavidn	int	        rc, edited = 0;
30220253Sjoerg	char           *p = NULL;
30320253Sjoerg	struct carg    *arg;
30420253Sjoerg	struct passwd  *pwd = NULL;
30520253Sjoerg	struct group   *grp;
30620253Sjoerg	struct stat     st;
307284118Sbapt	struct userconf	*cnf;
30820747Sdavidn	char            line[_PASSWORD_LEN+1];
309283961Sbapt	char		path[MAXPATHLEN];
31082868Sdd	FILE	       *fp;
311167919Sle	char *dmode_c;
312167919Sle	void *set = NULL;
31320253Sjoerg
31420253Sjoerg	static struct passwd fakeuser =
31520253Sjoerg	{
316285398Sbapt		"nouser",
31720253Sjoerg		"*",
31820253Sjoerg		-1,
31920253Sjoerg		-1,
32020253Sjoerg		0,
32120253Sjoerg		"",
32220253Sjoerg		"User &",
32356000Sdavidn		"/nonexistent",
32420253Sjoerg		"/bin/sh",
32520253Sjoerg		0
32656000Sdavidn#if defined(__FreeBSD__)
32756000Sdavidn		,0
32856000Sdavidn#endif
32920253Sjoerg	};
33020253Sjoerg
331284118Sbapt	cnf = conf.userconf;
33252512Sdavidn
333284149Sbapt	if (mode == M_NEXT)
334285396Sbapt		return (pw_usernext(cnf, conf.quiet));
33520267Sjoerg
336285398Sbapt	if (mode == M_PRINT)
337285398Sbapt		return (pw_usershow(name, id, &fakeuser));
338285398Sbapt
339285401Sbapt	if (mode == M_DELETE)
340285401Sbapt		return (pw_userdel(name, id));
341285401Sbapt
342285405Sbapt	if (mode == M_LOCK || mode == M_UNLOCK)
343285405Sbapt		return (pw_userlock(name, id, mode));
344285405Sbapt
34520267Sjoerg	/*
34620253Sjoerg	 * We can do all of the common legwork here
34720253Sjoerg	 */
34820253Sjoerg
34920253Sjoerg	if ((arg = getarg(args, 'b')) != NULL) {
35020267Sjoerg		cnf->home = arg->val;
35120253Sjoerg	}
35221052Sdavidn
353167919Sle	if ((arg = getarg(args, 'M')) != NULL) {
354167919Sle		dmode_c = arg->val;
355167919Sle		if ((set = setmode(dmode_c)) == NULL)
356167919Sle			errx(EX_DATAERR, "invalid directory creation mode '%s'",
357167919Sle			    dmode_c);
358219408Sjkim		cnf->homemode = getmode(set, _DEF_DIRMODE);
359167919Sle		free(set);
360168044Sle	}
361167919Sle
36221052Sdavidn	/*
36321052Sdavidn	 * If we'll need to use it or we're updating it,
36421052Sdavidn	 * then create the base home directory if necessary
36521052Sdavidn	 */
366224535Sdelphij	if (arg != NULL || getarg(args, 'm') != NULL) {
36721052Sdavidn		int	l = strlen(cnf->home);
36821052Sdavidn
36921052Sdavidn		if (l > 1 && cnf->home[l-1] == '/')	/* Shave off any trailing path delimiter */
37021052Sdavidn			cnf->home[--l] = '\0';
37121052Sdavidn
37221052Sdavidn		if (l < 2 || *cnf->home != '/')		/* Check for absolute path name */
37330259Scharnier			errx(EX_DATAERR, "invalid base directory for home '%s'", cnf->home);
37421052Sdavidn
37521052Sdavidn		if (stat(cnf->home, &st) == -1) {
37621052Sdavidn			char	dbuf[MAXPATHLEN];
37721052Sdavidn
37821242Sdavidn			/*
37921242Sdavidn			 * This is a kludge especially for Joerg :)
38021242Sdavidn			 * If the home directory would be created in the root partition, then
38121242Sdavidn			 * we really create it under /usr which is likely to have more space.
38221242Sdavidn			 * But we create a symlink from cnf->home -> "/usr" -> cnf->home
38321242Sdavidn			 */
38421242Sdavidn			if (strchr(cnf->home+1, '/') == NULL) {
385282683Sbapt				snprintf(dbuf, MAXPATHLEN, "/usr%s", cnf->home);
386219408Sjkim				if (mkdir(dbuf, _DEF_DIRMODE) != -1 || errno == EEXIST) {
38721242Sdavidn					chown(dbuf, 0, 0);
388148584Spjd					/*
389148584Spjd					 * Skip first "/" and create symlink:
390148584Spjd					 * /home -> usr/home
391148584Spjd					 */
392148584Spjd					symlink(dbuf+1, cnf->home);
39321242Sdavidn				}
39421242Sdavidn				/* If this falls, fall back to old method */
39521242Sdavidn			}
396130633Srobert			strlcpy(dbuf, cnf->home, sizeof(dbuf));
397130633Srobert			p = dbuf;
39821242Sdavidn			if (stat(dbuf, &st) == -1) {
399252377Skientzle				while ((p = strchr(p + 1, '/')) != NULL) {
40021242Sdavidn					*p = '\0';
40121242Sdavidn					if (stat(dbuf, &st) == -1) {
402219408Sjkim						if (mkdir(dbuf, _DEF_DIRMODE) == -1)
403285406Sbapt							err(EX_OSFILE, "mkdir '%s'", dbuf);
40421242Sdavidn						chown(dbuf, 0, 0);
40521242Sdavidn					} else if (!S_ISDIR(st.st_mode))
40630259Scharnier						errx(EX_OSFILE, "'%s' (root home parent) is not a directory", dbuf);
40721242Sdavidn					*p = '/';
40821242Sdavidn				}
40921052Sdavidn			}
41021242Sdavidn			if (stat(dbuf, &st) == -1) {
411285406Sbapt				if (mkdir(dbuf, _DEF_DIRMODE) == -1)
412285406Sbapt					err(EX_OSFILE, "mkdir '%s'", dbuf);
41321052Sdavidn				chown(dbuf, 0, 0);
41421052Sdavidn			}
41521052Sdavidn		} else if (!S_ISDIR(st.st_mode))
41630259Scharnier			errx(EX_OSFILE, "root home `%s' is not a directory", cnf->home);
41721052Sdavidn	}
41821052Sdavidn
41920253Sjoerg	if ((arg = getarg(args, 'e')) != NULL)
42020253Sjoerg		cnf->expire_days = atoi(arg->val);
42120253Sjoerg
42221330Sdavidn	if ((arg = getarg(args, 'y')) != NULL)
42321330Sdavidn		cnf->nispasswd = arg->val;
42421330Sdavidn
42520253Sjoerg	if ((arg = getarg(args, 'p')) != NULL && arg->val)
42620253Sjoerg		cnf->password_days = atoi(arg->val);
42720253Sjoerg
42820253Sjoerg	if ((arg = getarg(args, 'g')) != NULL) {
42963596Sdavidn		if (!*(p = arg->val))	/* Handle empty group list specially */
43063596Sdavidn			cnf->default_group = "";
43163596Sdavidn		else {
43263596Sdavidn			if ((grp = GETGRNAM(p)) == NULL) {
43363596Sdavidn				if (!isdigit((unsigned char)*p) || (grp = GETGRGID((gid_t) atoi(p))) == NULL)
43463596Sdavidn					errx(EX_NOUSER, "group `%s' does not exist", p);
43563596Sdavidn			}
43663596Sdavidn			cnf->default_group = newstr(grp->gr_name);
43720253Sjoerg		}
43820253Sjoerg	}
43920253Sjoerg	if ((arg = getarg(args, 'L')) != NULL)
440284110Sbapt		cnf->default_class = pw_checkname(arg->val, 0);
44120253Sjoerg
44220253Sjoerg	if ((arg = getarg(args, 'G')) != NULL && arg->val) {
44320747Sdavidn		for (p = strtok(arg->val, ", \t"); p != NULL; p = strtok(NULL, ", \t")) {
44444229Sdavidn			if ((grp = GETGRNAM(p)) == NULL) {
44561957Sache				if (!isdigit((unsigned char)*p) || (grp = GETGRGID((gid_t) atoi(p))) == NULL)
44630259Scharnier					errx(EX_NOUSER, "group `%s' does not exist", p);
44720253Sjoerg			}
448285412Sbapt			sl_add(cnf->groups, newstr(grp->gr_name));
44920253Sjoerg		}
45020253Sjoerg	}
45152527Sdavidn
45220253Sjoerg	if ((arg = getarg(args, 'k')) != NULL) {
45326088Sdavidn		if (stat(cnf->dotdir = arg->val, &st) == -1 || !S_ISDIR(st.st_mode))
45430259Scharnier			errx(EX_OSFILE, "skeleton `%s' is not a directory or does not exist", cnf->dotdir);
45520253Sjoerg	}
45652527Sdavidn
45720253Sjoerg	if ((arg = getarg(args, 's')) != NULL)
45820253Sjoerg		cnf->shell_default = arg->val;
45920253Sjoerg
46063600Sdavidn	if ((arg = getarg(args, 'w')) != NULL)
46163600Sdavidn		cnf->default_password = boolean_val(arg->val, cnf->default_password);
46220253Sjoerg	if (mode == M_ADD && getarg(args, 'D')) {
463284135Sbapt		if (name != NULL)
46430259Scharnier			errx(EX_DATAERR, "can't combine `-D' with `-n name'");
46520253Sjoerg		if ((arg = getarg(args, 'u')) != NULL && (p = strtok(arg->val, ", \t")) != NULL) {
46620253Sjoerg			if ((cnf->min_uid = (uid_t) atoi(p)) == 0)
46720253Sjoerg				cnf->min_uid = 1000;
46820253Sjoerg			if ((p = strtok(NULL, " ,\t")) == NULL || (cnf->max_uid = (uid_t) atoi(p)) < cnf->min_uid)
46920253Sjoerg				cnf->max_uid = 32000;
47020253Sjoerg		}
47120253Sjoerg		if ((arg = getarg(args, 'i')) != NULL && (p = strtok(arg->val, ", \t")) != NULL) {
47220253Sjoerg			if ((cnf->min_gid = (gid_t) atoi(p)) == 0)
47320253Sjoerg				cnf->min_gid = 1000;
47420253Sjoerg			if ((p = strtok(NULL, " ,\t")) == NULL || (cnf->max_gid = (gid_t) atoi(p)) < cnf->min_gid)
47520253Sjoerg				cnf->max_gid = 32000;
47620253Sjoerg		}
47720253Sjoerg
478284135Sbapt		if (write_userconfig(conf.config))
479284135Sbapt			return (EXIT_SUCCESS);
480283814Sbapt		err(EX_IOERR, "config udpate");
48120253Sjoerg	}
48252527Sdavidn
483284128Sbapt	if (name != NULL)
484284128Sbapt		pwd = GETPWNAM(pw_checkname(name, 0));
48520253Sjoerg
486284128Sbapt	if (id < 0 && name == NULL)
487284128Sbapt		errx(EX_DATAERR, "user name or id required");
48820253Sjoerg
48920253Sjoerg	/*
490285401Sbapt	 * Update require that the user exists
49120253Sjoerg	 */
492285405Sbapt	if (mode == M_UPDATE) {
49352527Sdavidn
494284128Sbapt		if (name == NULL && pwd == NULL)	/* Try harder */
495284128Sbapt			pwd = GETPWUID(id);
49620253Sjoerg
49720253Sjoerg		if (pwd == NULL) {
498284128Sbapt			if (name == NULL)
499284128Sbapt				errx(EX_NOUSER, "no such uid `%ld'", id);
500284128Sbapt			errx(EX_NOUSER, "no such user `%s'", name);
50120253Sjoerg		}
50252527Sdavidn
503284128Sbapt		if (name == NULL)
504284128Sbapt			name = pwd->pw_name;
50520253Sjoerg
50620253Sjoerg		/*
50720253Sjoerg		 * The rest is edit code
50820253Sjoerg		 */
509284129Sbapt		if (conf.newname != NULL) {
51020253Sjoerg			if (strcmp(pwd->pw_name, "root") == 0)
51130259Scharnier				errx(EX_DATAERR, "can't rename `root' account");
512284129Sbapt			pwd->pw_name = pw_checkname(conf.newname, 0);
51352527Sdavidn			edited = 1;
51420253Sjoerg		}
51552527Sdavidn
516284133Sbapt		if (id > 0 && isdigit((unsigned char)*arg->val)) {
517284133Sbapt			pwd->pw_uid = (uid_t)id;
51852527Sdavidn			edited = 1;
51920253Sjoerg			if (pwd->pw_uid != 0 && strcmp(pwd->pw_name, "root") == 0)
52030259Scharnier				errx(EX_DATAERR, "can't change uid of `root' account");
52120253Sjoerg			if (pwd->pw_uid == 0 && strcmp(pwd->pw_name, "root") != 0)
52230259Scharnier				warnx("WARNING: account `%s' will have a uid of 0 (superuser access!)", pwd->pw_name);
52320253Sjoerg		}
52420253Sjoerg
52552527Sdavidn		if ((arg = getarg(args, 'g')) != NULL && pwd->pw_uid != 0) {	/* Already checked this */
52652527Sdavidn			gid_t newgid = (gid_t) GETGRNAM(cnf->default_group)->gr_gid;
52752527Sdavidn			if (newgid != pwd->pw_gid) {
52852527Sdavidn				edited = 1;
52961762Sdavidn				pwd->pw_gid = newgid;
53052527Sdavidn			}
53152527Sdavidn		}
53252527Sdavidn
53320253Sjoerg		if ((arg = getarg(args, 'p')) != NULL) {
53452527Sdavidn			if (*arg->val == '\0' || strcmp(arg->val, "0") == 0) {
53552527Sdavidn				if (pwd->pw_change != 0) {
53652527Sdavidn					pwd->pw_change = 0;
53752527Sdavidn					edited = 1;
53852527Sdavidn				}
53952527Sdavidn			}
54020253Sjoerg			else {
54120253Sjoerg				time_t          now = time(NULL);
54220253Sjoerg				time_t          expire = parse_date(now, arg->val);
54320253Sjoerg
54452527Sdavidn				if (pwd->pw_change != expire) {
54552527Sdavidn					pwd->pw_change = expire;
54652527Sdavidn					edited = 1;
54752527Sdavidn				}
54820253Sjoerg			}
54920253Sjoerg		}
55052527Sdavidn
55120267Sjoerg		if ((arg = getarg(args, 'e')) != NULL) {
55252527Sdavidn			if (*arg->val == '\0' || strcmp(arg->val, "0") == 0) {
55352527Sdavidn				if (pwd->pw_expire != 0) {
55452527Sdavidn					pwd->pw_expire = 0;
55552527Sdavidn					edited = 1;
55652527Sdavidn				}
55752527Sdavidn			}
55820253Sjoerg			else {
55920253Sjoerg				time_t          now = time(NULL);
56020253Sjoerg				time_t          expire = parse_date(now, arg->val);
56120253Sjoerg
56252527Sdavidn				if (pwd->pw_expire != expire) {
56352527Sdavidn					pwd->pw_expire = expire;
56452527Sdavidn					edited = 1;
56552527Sdavidn				}
56620253Sjoerg			}
56720253Sjoerg		}
56820253Sjoerg
56952527Sdavidn		if ((arg = getarg(args, 's')) != NULL) {
57052527Sdavidn			char *shell = shell_path(cnf->shelldir, cnf->shells, arg->val);
57152527Sdavidn			if (shell == NULL)
57252527Sdavidn				shell = "";
57352527Sdavidn			if (strcmp(shell, pwd->pw_shell) != 0) {
57452527Sdavidn				pwd->pw_shell = shell;
57552527Sdavidn				edited = 1;
57652527Sdavidn			}
57752527Sdavidn		}
57820253Sjoerg
57952527Sdavidn		if (getarg(args, 'L')) {
58052527Sdavidn			if (cnf->default_class == NULL)
58152527Sdavidn				cnf->default_class = "";
58252527Sdavidn			if (strcmp(pwd->pw_class, cnf->default_class) != 0) {
58352527Sdavidn				pwd->pw_class = cnf->default_class;
58452527Sdavidn				edited = 1;
58552527Sdavidn			}
58652527Sdavidn		}
58752527Sdavidn
58820747Sdavidn		if ((arg  = getarg(args, 'd')) != NULL) {
589130629Srobert			if (strcmp(pwd->pw_dir, arg->val))
590130629Srobert				edited = 1;
59120747Sdavidn			if (stat(pwd->pw_dir = arg->val, &st) == -1) {
59220747Sdavidn				if (getarg(args, 'm') == NULL && strcmp(pwd->pw_dir, "/nonexistent") != 0)
59330259Scharnier				  warnx("WARNING: home `%s' does not exist", pwd->pw_dir);
59420747Sdavidn			} else if (!S_ISDIR(st.st_mode))
59530259Scharnier				warnx("WARNING: home `%s' is not a directory", pwd->pw_dir);
59620747Sdavidn		}
59720747Sdavidn
598285137Sbapt		if ((arg = getarg(args, 'w')) != NULL && conf.fd == -1) {
59964918Sgreen			login_cap_t *lc;
60064918Sgreen
60164918Sgreen			lc = login_getpwclass(pwd);
60264918Sgreen			if (lc == NULL ||
603252688Sdes			    login_setcryptfmt(lc, "sha512", NULL) == NULL)
60464918Sgreen				warn("setting crypt(3) format");
60564918Sgreen			login_close(lc);
606285409Sbapt			pwd->pw_passwd = pw_password(cnf, pwd->pw_name);
60752527Sdavidn			edited = 1;
60852527Sdavidn		}
60920267Sjoerg
61020253Sjoerg	} else {
61164918Sgreen		login_cap_t *lc;
61252527Sdavidn
61352527Sdavidn		/*
61452527Sdavidn		 * Add code
61552527Sdavidn		 */
61652527Sdavidn
617284128Sbapt		if (name == NULL)	/* Required */
61830259Scharnier			errx(EX_DATAERR, "login name required");
619284128Sbapt		else if ((pwd = GETPWNAM(name)) != NULL)	/* Exists */
620284128Sbapt			errx(EX_DATAERR, "login name `%s' already exists", name);
62120253Sjoerg
62220253Sjoerg		/*
62320253Sjoerg		 * Now, set up defaults for a new user
62420253Sjoerg		 */
62520253Sjoerg		pwd = &fakeuser;
626284128Sbapt		pwd->pw_name = name;
62720253Sjoerg		pwd->pw_class = cnf->default_class ? cnf->default_class : "";
628284133Sbapt		pwd->pw_uid = pw_uidpolicy(cnf, id);
629284118Sbapt		pwd->pw_gid = pw_gidpolicy(args, pwd->pw_name, (gid_t) pwd->pw_uid);
63020253Sjoerg		pwd->pw_change = pw_pwdpolicy(cnf, args);
63120253Sjoerg		pwd->pw_expire = pw_exppolicy(cnf, args);
63220253Sjoerg		pwd->pw_dir = pw_homepolicy(cnf, args, pwd->pw_name);
63320253Sjoerg		pwd->pw_shell = pw_shellpolicy(cnf, args, NULL);
63464918Sgreen		lc = login_getpwclass(pwd);
635272833Sdes		if (lc == NULL || login_setcryptfmt(lc, "sha512", NULL) == NULL)
63664918Sgreen			warn("setting crypt(3) format");
63764918Sgreen		login_close(lc);
638285409Sbapt		pwd->pw_passwd = pw_password(cnf, pwd->pw_name);
63952527Sdavidn		edited = 1;
64020253Sjoerg
64120253Sjoerg		if (pwd->pw_uid == 0 && strcmp(pwd->pw_name, "root") != 0)
64230259Scharnier			warnx("WARNING: new account `%s' has a uid of 0 (superuser access!)", pwd->pw_name);
64320253Sjoerg	}
64420253Sjoerg
64520253Sjoerg	/*
64620253Sjoerg	 * Shared add/edit code
64720253Sjoerg	 */
648285408Sbapt	if (conf.gecos != NULL) {
649285408Sbapt		if (strcmp(pwd->pw_gecos, conf.gecos) != 0) {
650285408Sbapt			pwd->pw_gecos = conf.gecos;
65152527Sdavidn			edited = 1;
65252527Sdavidn		}
65352527Sdavidn	}
65420253Sjoerg
655285133Sbapt	if (conf.fd != -1)
656285137Sbapt		edited = set_passwd(pwd, mode == M_UPDATE);
65720253Sjoerg
65820267Sjoerg	/*
65920267Sjoerg	 * Special case: -N only displays & exits
66020267Sjoerg	 */
661284121Sbapt	if (conf.dryrun)
662284126Sbapt		return print_user(pwd);
66320267Sjoerg
66421330Sdavidn	if (mode == M_ADD) {
66552527Sdavidn		edited = 1;	/* Always */
66652502Sdavidn		rc = addpwent(pwd);
667283814Sbapt		if (rc == -1)
668283814Sbapt			errx(EX_IOERR, "user '%s' already exists",
669283814Sbapt			    pwd->pw_name);
670283814Sbapt		else if (rc != 0)
671283814Sbapt			err(EX_IOERR, "passwd file update");
67252502Sdavidn		if (cnf->nispasswd && *cnf->nispasswd=='/') {
67352502Sdavidn			rc = addnispwent(cnf->nispasswd, pwd);
67452502Sdavidn			if (rc == -1)
67552502Sdavidn				warnx("User '%s' already exists in NIS passwd", pwd->pw_name);
67652502Sdavidn			else
67756000Sdavidn				warn("NIS passwd update");
67852502Sdavidn			/* NOTE: we treat NIS-only update errors as non-fatal */
67952502Sdavidn		}
680285405Sbapt	} else if (mode == M_UPDATE && edited) /* Only updated this if required */
681285405Sbapt		perform_chgpwent(name, pwd);
68221330Sdavidn
68320253Sjoerg	/*
68420253Sjoerg	 * Ok, user is created or changed - now edit group file
68520253Sjoerg	 */
68620253Sjoerg
687242349Sbapt	if (mode == M_ADD || getarg(args, 'G') != NULL) {
688285412Sbapt		int j;
689285412Sbapt		size_t i;
690273779Sbapt		/* First remove the user from all group */
691273779Sbapt		SETGRENT();
692273779Sbapt		while ((grp = GETGRENT()) != NULL) {
693273779Sbapt			char group[MAXLOGNAME];
694273779Sbapt			if (grp->gr_mem == NULL)
695273779Sbapt				continue;
696273779Sbapt			for (i = 0; grp->gr_mem[i] != NULL; i++) {
697273779Sbapt				if (strcmp(grp->gr_mem[i] , pwd->pw_name) != 0)
698273779Sbapt					continue;
699273779Sbapt				for (j = i; grp->gr_mem[j] != NULL ; j++)
700273779Sbapt					grp->gr_mem[j] = grp->gr_mem[j+1];
701273779Sbapt				strlcpy(group, grp->gr_name, MAXLOGNAME);
702273779Sbapt				chggrent(group, grp);
703273779Sbapt			}
704273779Sbapt		}
705273779Sbapt		ENDGRENT();
706273779Sbapt
707273779Sbapt		/* now add to group where needed */
708285412Sbapt		for (i = 0; i < cnf->groups->sl_cur; i++) {
709285412Sbapt			grp = GETGRNAM(cnf->groups->sl_str[i]);
710244737Sbapt			grp = gr_add(grp, pwd->pw_name);
711244737Sbapt			/*
712244737Sbapt			 * grp can only be NULL in 2 cases:
713244737Sbapt			 * - the new member is already a member
714244737Sbapt			 * - a problem with memory occurs
715244737Sbapt			 * in both cases we want to skip now.
716244737Sbapt			 */
717244737Sbapt			if (grp == NULL)
718242349Sbapt				continue;
719285412Sbapt			chggrent(grp->gr_name, grp);
720245114Smjg			free(grp);
721242349Sbapt		}
722242349Sbapt	}
723242349Sbapt
724242349Sbapt
72561759Sdavidn	/* go get a current version of pwd */
726284128Sbapt	pwd = GETPWNAM(name);
72761759Sdavidn	if (pwd == NULL) {
72861759Sdavidn		/* This will fail when we rename, so special case that */
729284129Sbapt		if (mode == M_UPDATE && conf.newname != NULL) {
730284129Sbapt			name = conf.newname;		/* update new name */
731284128Sbapt			pwd = GETPWNAM(name);	/* refetch renamed rec */
73261759Sdavidn		}
73361759Sdavidn	}
73461759Sdavidn	if (pwd == NULL)	/* can't go on without this */
735284128Sbapt		errx(EX_NOUSER, "user '%s' disappeared during update", name);
73620253Sjoerg
73744229Sdavidn	grp = GETGRGID(pwd->pw_gid);
738283842Sbapt	pw_log(cnf, mode, W_USER, "%s(%u):%s(%u):%s:%s:%s",
739283842Sbapt	       pwd->pw_name, pwd->pw_uid,
740283842Sbapt	    grp ? grp->gr_name : "unknown", (grp ? grp->gr_gid : (uid_t)-1),
74120253Sjoerg	       pwd->pw_gecos, pwd->pw_dir, pwd->pw_shell);
74220253Sjoerg
74320253Sjoerg	/*
74420253Sjoerg	 * If adding, let's touch and chown the user's mail file. This is not
74520253Sjoerg	 * strictly necessary under BSD with a 0755 maildir but it also
74620253Sjoerg	 * doesn't hurt anything to create the empty mailfile
74720253Sjoerg	 */
74820253Sjoerg	if (mode == M_ADD) {
749283961Sbapt		if (PWALTDIR() != PWF_ALT) {
750283961Sbapt			arg = getarg(args, 'R');
751283961Sbapt			snprintf(path, sizeof(path), "%s%s/%s",
752283961Sbapt			    arg ? arg->val : "", _PATH_MAILDIR, pwd->pw_name);
753283961Sbapt			close(open(path, O_RDWR | O_CREAT, 0600));	/* Preserve contents &
75444229Sdavidn									 * mtime */
755283961Sbapt			chown(path, pwd->pw_uid, pwd->pw_gid);
75620253Sjoerg		}
75720253Sjoerg	}
75852527Sdavidn
75920253Sjoerg	/*
76082868Sdd	 * Let's create and populate the user's home directory. Note
76120253Sjoerg	 * that this also `works' for editing users if -m is used, but
76220253Sjoerg	 * existing files will *not* be overwritten.
76320253Sjoerg	 */
764283961Sbapt	if (PWALTDIR() != PWF_ALT && getarg(args, 'm') != NULL && pwd->pw_dir &&
765283961Sbapt	    *pwd->pw_dir == '/' && pwd->pw_dir[1])
766285403Sbapt		create_and_populate_homedir(pwd);
76752527Sdavidn
76882868Sdd	/*
76982868Sdd	 * Finally, send mail to the new user as well, if we are asked to
77082868Sdd	 */
77182868Sdd	if (mode == M_ADD && !PWALTDIR() && cnf->newmail && *cnf->newmail && (fp = fopen(cnf->newmail, "r")) != NULL) {
77282868Sdd		FILE           *pfp = popen(_PATH_SENDMAIL " -t", "w");
77382868Sdd
77482868Sdd		if (pfp == NULL)
77582868Sdd			warn("sendmail");
77682868Sdd		else {
77782868Sdd			fprintf(pfp, "From: root\n" "To: %s\n" "Subject: Welcome!\n\n", pwd->pw_name);
77882868Sdd			while (fgets(line, sizeof(line), fp) != NULL) {
77982868Sdd				/* Do substitutions? */
78082868Sdd				fputs(line, pfp);
78182868Sdd			}
78282868Sdd			pclose(pfp);
783283842Sbapt			pw_log(cnf, mode, W_USER, "%s(%u) new user mail sent",
784283842Sbapt			    pwd->pw_name, pwd->pw_uid);
78582868Sdd		}
78682868Sdd		fclose(fp);
78782868Sdd	}
78882868Sdd
78920267Sjoerg	return EXIT_SUCCESS;
79020253Sjoerg}
79120253Sjoerg
79220253Sjoerg
79320253Sjoergstatic          uid_t
794284133Sbaptpw_uidpolicy(struct userconf * cnf, long id)
79520253Sjoerg{
79620253Sjoerg	struct passwd  *pwd;
79720253Sjoerg	uid_t           uid = (uid_t) - 1;
79820253Sjoerg
79920253Sjoerg	/*
80020253Sjoerg	 * Check the given uid, if any
80120253Sjoerg	 */
802284133Sbapt	if (id > 0) {
803284133Sbapt		uid = (uid_t) id;
80420253Sjoerg
805284133Sbapt		if ((pwd = GETPWUID(uid)) != NULL && conf.checkduplicate)
806283842Sbapt			errx(EX_DATAERR, "uid `%u' has already been allocated", pwd->pw_uid);
80720253Sjoerg	} else {
80820253Sjoerg		struct bitmap   bm;
80920253Sjoerg
81020253Sjoerg		/*
81120253Sjoerg		 * We need to allocate the next available uid under one of
81220253Sjoerg		 * two policies a) Grab the first unused uid b) Grab the
81320253Sjoerg		 * highest possible unused uid
81420253Sjoerg		 */
81520253Sjoerg		if (cnf->min_uid >= cnf->max_uid) {	/* Sanity
81620253Sjoerg							 * claus^H^H^H^Hheck */
81720253Sjoerg			cnf->min_uid = 1000;
81820253Sjoerg			cnf->max_uid = 32000;
81920253Sjoerg		}
82020253Sjoerg		bm = bm_alloc(cnf->max_uid - cnf->min_uid + 1);
82120253Sjoerg
82220253Sjoerg		/*
82320253Sjoerg		 * Now, let's fill the bitmap from the password file
82420253Sjoerg		 */
82544229Sdavidn		SETPWENT();
82644229Sdavidn		while ((pwd = GETPWENT()) != NULL)
82744229Sdavidn			if (pwd->pw_uid >= (uid_t) cnf->min_uid && pwd->pw_uid <= (uid_t) cnf->max_uid)
82820253Sjoerg				bm_setbit(&bm, pwd->pw_uid - cnf->min_uid);
82944229Sdavidn		ENDPWENT();
83020253Sjoerg
83120253Sjoerg		/*
83220253Sjoerg		 * Then apply the policy, with fallback to reuse if necessary
83320253Sjoerg		 */
83420253Sjoerg		if (cnf->reuse_uids || (uid = (uid_t) (bm_lastset(&bm) + cnf->min_uid + 1)) > cnf->max_uid)
83520253Sjoerg			uid = (uid_t) (bm_firstunset(&bm) + cnf->min_uid);
83620253Sjoerg
83720253Sjoerg		/*
83820253Sjoerg		 * Another sanity check
83920253Sjoerg		 */
84020253Sjoerg		if (uid < cnf->min_uid || uid > cnf->max_uid)
84130259Scharnier			errx(EX_SOFTWARE, "unable to allocate a new uid - range fully used");
84220253Sjoerg		bm_dealloc(&bm);
84320253Sjoerg	}
84420253Sjoerg	return uid;
84520253Sjoerg}
84620253Sjoerg
84720253Sjoerg
84820253Sjoergstatic          uid_t
849284118Sbaptpw_gidpolicy(struct cargs * args, char *nam, gid_t prefer)
85020253Sjoerg{
85120253Sjoerg	struct group   *grp;
85220253Sjoerg	gid_t           gid = (uid_t) - 1;
85320253Sjoerg	struct carg    *a_gid = getarg(args, 'g');
854284118Sbapt	struct userconf	*cnf = conf.userconf;
85520253Sjoerg
85620253Sjoerg	/*
85720253Sjoerg	 * If no arg given, see if default can help out
85820253Sjoerg	 */
85920253Sjoerg	if (a_gid == NULL && cnf->default_group && *cnf->default_group)
86020253Sjoerg		a_gid = addarg(args, 'g', cnf->default_group);
86120253Sjoerg
86220253Sjoerg	/*
86320253Sjoerg	 * Check the given gid, if any
86420253Sjoerg	 */
86544229Sdavidn	SETGRENT();
86620253Sjoerg	if (a_gid != NULL) {
86744229Sdavidn		if ((grp = GETGRNAM(a_gid->val)) == NULL) {
86820253Sjoerg			gid = (gid_t) atol(a_gid->val);
86961957Sache			if ((gid == 0 && !isdigit((unsigned char)*a_gid->val)) || (grp = GETGRGID(gid)) == NULL)
87030259Scharnier				errx(EX_NOUSER, "group `%s' is not defined", a_gid->val);
87120253Sjoerg		}
87220253Sjoerg		gid = grp->gr_gid;
873262865Sjulian	} else if ((grp = GETGRNAM(nam)) != NULL &&
874262865Sjulian	    (grp->gr_mem == NULL || grp->gr_mem[0] == NULL)) {
87520267Sjoerg		gid = grp->gr_gid;  /* Already created? Use it anyway... */
87620253Sjoerg	} else {
87720253Sjoerg		struct cargs    grpargs;
87820253Sjoerg		char            tmp[32];
87920253Sjoerg
88020253Sjoerg		LIST_INIT(&grpargs);
88120253Sjoerg
88220253Sjoerg		/*
88320253Sjoerg		 * We need to auto-create a group with the user's name. We
88420253Sjoerg		 * can send all the appropriate output to our sister routine
88520253Sjoerg		 * bit first see if we can create a group with gid==uid so we
88620253Sjoerg		 * can keep the user and group ids in sync. We purposely do
88720253Sjoerg		 * NOT check the gid range if we can force the sync. If the
88820253Sjoerg		 * user's name dups an existing group, then the group add
88920253Sjoerg		 * function will happily handle that case for us and exit.
89020253Sjoerg		 */
89144229Sdavidn		if (GETGRGID(prefer) == NULL) {
892282700Sbapt			snprintf(tmp, sizeof(tmp), "%u", prefer);
89320253Sjoerg			addarg(&grpargs, 'g', tmp);
89420253Sjoerg		}
895284121Sbapt		if (conf.dryrun) {
896285395Sbapt			gid = pw_groupnext(cnf, true);
897285395Sbapt		} else {
898284128Sbapt			pw_group(M_ADD, nam, -1, &grpargs);
89944229Sdavidn			if ((grp = GETGRNAM(nam)) != NULL)
90020267Sjoerg				gid = grp->gr_gid;
90120267Sjoerg		}
90270486Sben		a_gid = LIST_FIRST(&grpargs);
90320253Sjoerg		while (a_gid != NULL) {
90470486Sben			struct carg    *t = LIST_NEXT(a_gid, list);
90520253Sjoerg			LIST_REMOVE(a_gid, list);
90620253Sjoerg			a_gid = t;
90720253Sjoerg		}
90820253Sjoerg	}
90944229Sdavidn	ENDGRENT();
91020253Sjoerg	return gid;
91120253Sjoerg}
91220253Sjoerg
91320253Sjoerg
91420253Sjoergstatic          time_t
91520253Sjoergpw_pwdpolicy(struct userconf * cnf, struct cargs * args)
91620253Sjoerg{
91720253Sjoerg	time_t          result = 0;
91820253Sjoerg	time_t          now = time(NULL);
91927831Sdavidn	struct carg    *arg = getarg(args, 'p');
92020253Sjoerg
92120253Sjoerg	if (arg != NULL) {
92220253Sjoerg		if ((result = parse_date(now, arg->val)) == now)
92330259Scharnier			errx(EX_DATAERR, "invalid date/time `%s'", arg->val);
92420253Sjoerg	} else if (cnf->password_days > 0)
92520253Sjoerg		result = now + ((long) cnf->password_days * 86400L);
92620253Sjoerg	return result;
92720253Sjoerg}
92820253Sjoerg
92920253Sjoerg
93020253Sjoergstatic          time_t
93120253Sjoergpw_exppolicy(struct userconf * cnf, struct cargs * args)
93220253Sjoerg{
93320253Sjoerg	time_t          result = 0;
93420253Sjoerg	time_t          now = time(NULL);
93520253Sjoerg	struct carg    *arg = getarg(args, 'e');
93620253Sjoerg
93720253Sjoerg	if (arg != NULL) {
93820253Sjoerg		if ((result = parse_date(now, arg->val)) == now)
93930259Scharnier			errx(EX_DATAERR, "invalid date/time `%s'", arg->val);
94020253Sjoerg	} else if (cnf->expire_days > 0)
94120253Sjoerg		result = now + ((long) cnf->expire_days * 86400L);
94220253Sjoerg	return result;
94320253Sjoerg}
94420253Sjoerg
94520253Sjoerg
94620253Sjoergstatic char    *
94720253Sjoergpw_homepolicy(struct userconf * cnf, struct cargs * args, char const * user)
94820253Sjoerg{
94920253Sjoerg	struct carg    *arg = getarg(args, 'd');
950282699Sbapt	static char     home[128];
95120253Sjoerg
95220253Sjoerg	if (arg)
953282699Sbapt		return (arg->val);
95420253Sjoerg
955282699Sbapt	if (cnf->home == NULL || *cnf->home == '\0')
956282699Sbapt		errx(EX_CONFIG, "no base home directory set");
957282699Sbapt	snprintf(home, sizeof(home), "%s/%s", cnf->home, user);
958282699Sbapt
959282699Sbapt	return (home);
96020253Sjoerg}
96120253Sjoerg
96220253Sjoergstatic char    *
96320253Sjoergshell_path(char const * path, char *shells[], char *sh)
96420253Sjoerg{
96520253Sjoerg	if (sh != NULL && (*sh == '/' || *sh == '\0'))
96620253Sjoerg		return sh;	/* specified full path or forced none */
96720253Sjoerg	else {
96820253Sjoerg		char           *p;
96920253Sjoerg		char            paths[_UC_MAXLINE];
97020253Sjoerg
97120253Sjoerg		/*
97220253Sjoerg		 * We need to search paths
97320253Sjoerg		 */
974130633Srobert		strlcpy(paths, path, sizeof(paths));
97520253Sjoerg		for (p = strtok(paths, ": \t\r\n"); p != NULL; p = strtok(NULL, ": \t\r\n")) {
97620253Sjoerg			int             i;
97720253Sjoerg			static char     shellpath[256];
97820253Sjoerg
97920253Sjoerg			if (sh != NULL) {
980282700Sbapt				snprintf(shellpath, sizeof(shellpath), "%s/%s", p, sh);
98120253Sjoerg				if (access(shellpath, X_OK) == 0)
98220253Sjoerg					return shellpath;
98320253Sjoerg			} else
98420253Sjoerg				for (i = 0; i < _UC_MAXSHELLS && shells[i] != NULL; i++) {
985282700Sbapt					snprintf(shellpath, sizeof(shellpath), "%s/%s", p, shells[i]);
98620253Sjoerg					if (access(shellpath, X_OK) == 0)
98720253Sjoerg						return shellpath;
98820253Sjoerg				}
98920253Sjoerg		}
99020253Sjoerg		if (sh == NULL)
99130259Scharnier			errx(EX_OSFILE, "can't find shell `%s' in shell paths", sh);
99230259Scharnier		errx(EX_CONFIG, "no default shell available or defined");
99320253Sjoerg		return NULL;
99420253Sjoerg	}
99520253Sjoerg}
99620253Sjoerg
99720253Sjoerg
99820253Sjoergstatic char    *
99920253Sjoergpw_shellpolicy(struct userconf * cnf, struct cargs * args, char *newshell)
100020253Sjoerg{
100120253Sjoerg	char           *sh = newshell;
100220253Sjoerg	struct carg    *arg = getarg(args, 's');
100320253Sjoerg
100420253Sjoerg	if (newshell == NULL && arg != NULL)
100520253Sjoerg		sh = arg->val;
100620253Sjoerg	return shell_path(cnf->shelldir, cnf->shells, sh ? sh : cnf->shell_default);
100720253Sjoerg}
100820253Sjoerg
1009179365Santoine#define	SALTSIZE	32
101020253Sjoerg
1011179365Santoinestatic char const chars[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ./";
1012179365Santoine
101320253Sjoergchar           *
101420253Sjoergpw_pwcrypt(char *password)
101520253Sjoerg{
101620253Sjoerg	int             i;
1017179365Santoine	char            salt[SALTSIZE + 1];
1018231994Skevlo	char		*cryptpw;
101920253Sjoerg
102020253Sjoerg	static char     buf[256];
102120253Sjoerg
102220253Sjoerg	/*
102320253Sjoerg	 * Calculate a salt value
102420253Sjoerg	 */
1025179365Santoine	for (i = 0; i < SALTSIZE; i++)
1026181785Sache		salt[i] = chars[arc4random_uniform(sizeof(chars) - 1)];
1027179365Santoine	salt[SALTSIZE] = '\0';
102820253Sjoerg
1029231994Skevlo	cryptpw = crypt(password, salt);
1030231994Skevlo	if (cryptpw == NULL)
1031231994Skevlo		errx(EX_CONFIG, "crypt(3) failure");
1032231994Skevlo	return strcpy(buf, cryptpw);
103320253Sjoerg}
103420253Sjoerg
103520590Sdavidn
103620253Sjoergstatic char    *
1037285409Sbaptpw_password(struct userconf * cnf, char const * user)
103820253Sjoerg{
103920253Sjoerg	int             i, l;
104020253Sjoerg	char            pwbuf[32];
104120253Sjoerg
104220253Sjoerg	switch (cnf->default_password) {
104320253Sjoerg	case -1:		/* Random password */
104473563Skris		l = (arc4random() % 8 + 8);	/* 8 - 16 chars */
104520253Sjoerg		for (i = 0; i < l; i++)
1046181785Sache			pwbuf[i] = chars[arc4random_uniform(sizeof(chars)-1)];
104720253Sjoerg		pwbuf[i] = '\0';
104820253Sjoerg
104920253Sjoerg		/*
105020253Sjoerg		 * We give this information back to the user
105120253Sjoerg		 */
1052285137Sbapt		if (conf.fd == -1 && !conf.dryrun) {
105361957Sache			if (isatty(STDOUT_FILENO))
105420712Sdavidn				printf("Password for '%s' is: ", user);
105520253Sjoerg			printf("%s\n", pwbuf);
105620253Sjoerg			fflush(stdout);
105720253Sjoerg		}
105820253Sjoerg		break;
105920253Sjoerg
106020253Sjoerg	case -2:		/* No password at all! */
106120253Sjoerg		return "";
106220253Sjoerg
106320253Sjoerg	case 0:		/* No login - default */
106420253Sjoerg	default:
106520253Sjoerg		return "*";
106620253Sjoerg
106720253Sjoerg	case 1:		/* user's name */
1068130633Srobert		strlcpy(pwbuf, user, sizeof(pwbuf));
106920253Sjoerg		break;
107020253Sjoerg	}
107120253Sjoerg	return pw_pwcrypt(pwbuf);
107220253Sjoerg}
107320253Sjoerg
1074284111Sbaptstatic int
1075285401Sbaptpw_userdel(char *name, long id)
1076284111Sbapt{
1077285401Sbapt	struct passwd *pwd = NULL;
1078284111Sbapt	char		 file[MAXPATHLEN];
1079284111Sbapt	char		 home[MAXPATHLEN];
1080285401Sbapt	uid_t		 uid;
1081284111Sbapt	struct group	*gr, *grp;
1082284111Sbapt	char		 grname[LOGNAMESIZE];
1083284111Sbapt	int		 rc;
1084284111Sbapt	struct stat	 st;
108520253Sjoerg
1086285401Sbapt	if (id < 0 && name == NULL)
1087285401Sbapt		errx(EX_DATAERR, "username or id required");
1088285401Sbapt
1089285401Sbapt	pwd = (name != NULL) ? GETPWNAM(pw_checkname(name, 0)) : GETPWUID(id);
1090285401Sbapt	if (pwd == NULL) {
1091285401Sbapt		if (name == NULL)
1092285401Sbapt			errx(EX_NOUSER, "no such uid `%ld'", id);
1093285401Sbapt		errx(EX_NOUSER, "no such user `%s'", name);
1094285401Sbapt	}
1095285401Sbapt	uid = pwd->pw_uid;
1096285401Sbapt	if (name == NULL)
1097285401Sbapt		name = pwd->pw_name;
1098285401Sbapt
1099284111Sbapt	if (strcmp(pwd->pw_name, "root") == 0)
1100284111Sbapt		errx(EX_DATAERR, "cannot remove user 'root'");
1101284111Sbapt
1102284111Sbapt	if (!PWALTDIR()) {
1103284111Sbapt		/*
1104284111Sbapt		 * Remove opie record from /etc/opiekeys
1105284111Sbapt		*/
1106284111Sbapt
1107284111Sbapt		rmopie(pwd->pw_name);
1108284111Sbapt
1109284111Sbapt		/*
1110284111Sbapt		 * Remove crontabs
1111284111Sbapt		 */
1112284111Sbapt		snprintf(file, sizeof(file), "/var/cron/tabs/%s", pwd->pw_name);
1113284111Sbapt		if (access(file, F_OK) == 0) {
1114284111Sbapt			snprintf(file, sizeof(file), "crontab -u %s -r", pwd->pw_name);
1115284111Sbapt			system(file);
1116284111Sbapt		}
1117284111Sbapt	}
1118284111Sbapt	/*
1119284111Sbapt	 * Save these for later, since contents of pwd may be
1120284111Sbapt	 * invalidated by deletion
1121284111Sbapt	 */
1122284111Sbapt	snprintf(file, sizeof(file), "%s/%s", _PATH_MAILDIR, pwd->pw_name);
1123284111Sbapt	strlcpy(home, pwd->pw_dir, sizeof(home));
1124284111Sbapt	gr = GETGRGID(pwd->pw_gid);
1125284111Sbapt	if (gr != NULL)
1126284111Sbapt		strlcpy(grname, gr->gr_name, LOGNAMESIZE);
1127284111Sbapt	else
1128284111Sbapt		grname[0] = '\0';
1129284111Sbapt
1130284111Sbapt	rc = delpwent(pwd);
1131284111Sbapt	if (rc == -1)
1132284111Sbapt		err(EX_IOERR, "user '%s' does not exist", pwd->pw_name);
1133284111Sbapt	else if (rc != 0)
1134284111Sbapt		err(EX_IOERR, "passwd update");
1135284111Sbapt
1136285401Sbapt	if (conf.userconf->nispasswd && *conf.userconf->nispasswd=='/') {
1137285401Sbapt		rc = delnispwent(conf.userconf->nispasswd, name);
1138284111Sbapt		if (rc == -1)
1139285401Sbapt			warnx("WARNING: user '%s' does not exist in NIS passwd",
1140285401Sbapt			    pwd->pw_name);
1141284111Sbapt		else if (rc != 0)
1142284111Sbapt			warn("WARNING: NIS passwd update");
1143284111Sbapt		/* non-fatal */
1144284111Sbapt	}
1145284111Sbapt
1146284128Sbapt	grp = GETGRNAM(name);
1147284111Sbapt	if (grp != NULL &&
1148284111Sbapt	    (grp->gr_mem == NULL || *grp->gr_mem == NULL) &&
1149284128Sbapt	    strcmp(name, grname) == 0)
1150284128Sbapt		delgrent(GETGRNAM(name));
1151284111Sbapt	SETGRENT();
1152284111Sbapt	while ((grp = GETGRENT()) != NULL) {
1153284111Sbapt		int i, j;
1154284111Sbapt		char group[MAXLOGNAME];
1155284113Sbapt		if (grp->gr_mem == NULL)
1156284113Sbapt			continue;
1157284113Sbapt
1158284113Sbapt		for (i = 0; grp->gr_mem[i] != NULL; i++) {
1159284128Sbapt			if (strcmp(grp->gr_mem[i], name) != 0)
1160284113Sbapt				continue;
1161284113Sbapt
1162284113Sbapt			for (j = i; grp->gr_mem[j] != NULL; j++)
1163284113Sbapt				grp->gr_mem[j] = grp->gr_mem[j+1];
1164284113Sbapt			strlcpy(group, grp->gr_name, MAXLOGNAME);
1165284113Sbapt			chggrent(group, grp);
1166284111Sbapt		}
1167284111Sbapt	}
1168284111Sbapt	ENDGRENT();
1169284111Sbapt
1170285401Sbapt	pw_log(conf.userconf, M_DELETE, W_USER, "%s(%u) account removed", name,
1171285401Sbapt	    uid);
1172284111Sbapt
1173284117Sbapt	if (!PWALTDIR()) {
1174284111Sbapt		/*
1175284111Sbapt		 * Remove mail file
1176284111Sbapt		 */
1177284111Sbapt		remove(file);
1178284111Sbapt
1179284111Sbapt		/*
1180284111Sbapt		 * Remove at jobs
1181284111Sbapt		 */
1182284111Sbapt		if (getpwuid(uid) == NULL)
1183284111Sbapt			rmat(uid);
1184284111Sbapt
1185284111Sbapt		/*
1186284111Sbapt		 * Remove home directory and contents
1187284111Sbapt		 */
1188285401Sbapt		if (conf.deletehome && *home == '/' && getpwuid(uid) == NULL &&
1189284112Sbapt		    stat(home, &st) != -1) {
1190284112Sbapt			rm_r(home, uid);
1191285401Sbapt			pw_log(conf.userconf, M_DELETE, W_USER, "%s(%u) home '%s' %sremoved",
1192284128Sbapt			       name, uid, home,
1193284112Sbapt			       stat(home, &st) == -1 ? "" : "not completely ");
1194284111Sbapt		}
1195284111Sbapt	}
1196284111Sbapt
1197284111Sbapt	return (EXIT_SUCCESS);
1198284111Sbapt}
1199284111Sbapt
120020253Sjoergstatic int
1201284124Sbaptprint_user(struct passwd * pwd)
120220253Sjoerg{
1203284122Sbapt	if (!conf.pretty) {
1204242349Sbapt		char            *buf;
120520253Sjoerg
1206284124Sbapt		buf = conf.v7 ? pw_make_v7(pwd) : pw_make(pwd);
1207242349Sbapt		printf("%s\n", buf);
1208242349Sbapt		free(buf);
120920253Sjoerg	} else {
121020267Sjoerg		int		j;
121120253Sjoerg		char           *p;
121244229Sdavidn		struct group   *grp = GETGRGID(pwd->pw_gid);
121320253Sjoerg		char            uname[60] = "User &", office[60] = "[None]",
121420253Sjoerg		                wphone[60] = "[None]", hphone[60] = "[None]";
121520590Sdavidn		char		acexpire[32] = "[None]", pwexpire[32] = "[None]";
121620590Sdavidn		struct tm *    tptr;
121720253Sjoerg
121820253Sjoerg		if ((p = strtok(pwd->pw_gecos, ",")) != NULL) {
1219130633Srobert			strlcpy(uname, p, sizeof(uname));
122020253Sjoerg			if ((p = strtok(NULL, ",")) != NULL) {
1221130633Srobert				strlcpy(office, p, sizeof(office));
122220253Sjoerg				if ((p = strtok(NULL, ",")) != NULL) {
1223130633Srobert					strlcpy(wphone, p, sizeof(wphone));
122420253Sjoerg					if ((p = strtok(NULL, "")) != NULL) {
1225130633Srobert						strlcpy(hphone, p,
1226130633Srobert						    sizeof(hphone));
122720253Sjoerg					}
122820253Sjoerg				}
122920253Sjoerg			}
123020253Sjoerg		}
123120253Sjoerg		/*
123220253Sjoerg		 * Handle '&' in gecos field
123320253Sjoerg		 */
123420253Sjoerg		if ((p = strchr(uname, '&')) != NULL) {
123520253Sjoerg			int             l = strlen(pwd->pw_name);
123620253Sjoerg			int             m = strlen(p);
123720253Sjoerg
123820253Sjoerg			memmove(p + l, p + 1, m);
123920253Sjoerg			memmove(p, pwd->pw_name, l);
124061957Sache			*p = (char) toupper((unsigned char)*p);
124120253Sjoerg		}
124220590Sdavidn		if (pwd->pw_expire > (time_t)0 && (tptr = localtime(&pwd->pw_expire)) != NULL)
124374569Sache			strftime(acexpire, sizeof acexpire, "%c", tptr);
124461957Sache		if (pwd->pw_change > (time_t)0 && (tptr = localtime(&pwd->pw_change)) != NULL)
124574569Sache			strftime(pwexpire, sizeof pwexpire, "%c", tptr);
1246283842Sbapt		printf("Login Name: %-15s   #%-12u Group: %-15s   #%u\n"
124720747Sdavidn		       " Full Name: %s\n"
124820747Sdavidn		       "      Home: %-26.26s      Class: %s\n"
124920747Sdavidn		       "     Shell: %-26.26s     Office: %s\n"
125020747Sdavidn		       "Work Phone: %-26.26s Home Phone: %s\n"
125120747Sdavidn		       "Acc Expire: %-26.26s Pwd Expire: %s\n",
1252283842Sbapt		       pwd->pw_name, pwd->pw_uid,
1253283842Sbapt		       grp ? grp->gr_name : "(invalid)", pwd->pw_gid,
125420253Sjoerg		       uname, pwd->pw_dir, pwd->pw_class,
125520590Sdavidn		       pwd->pw_shell, office, wphone, hphone,
125620590Sdavidn		       acexpire, pwexpire);
125744229Sdavidn	        SETGRENT();
125820267Sjoerg		j = 0;
125944229Sdavidn		while ((grp=GETGRENT()) != NULL)
126020267Sjoerg		{
126120267Sjoerg			int     i = 0;
1262262865Sjulian			if (grp->gr_mem != NULL) {
1263262865Sjulian				while (grp->gr_mem[i] != NULL)
126420267Sjoerg				{
1265262865Sjulian					if (strcmp(grp->gr_mem[i], pwd->pw_name)==0)
1266262865Sjulian					{
1267262865Sjulian						printf(j++ == 0 ? "    Groups: %s" : ",%s", grp->gr_name);
1268262865Sjulian						break;
1269262865Sjulian					}
1270262865Sjulian					++i;
127120267Sjoerg				}
127220267Sjoerg			}
127320267Sjoerg		}
127444229Sdavidn		ENDGRENT();
127561957Sache		printf("%s", j ? "\n" : "");
127620253Sjoerg	}
127720267Sjoerg	return EXIT_SUCCESS;
127820253Sjoerg}
127920253Sjoerg
1280284110Sbaptchar *
1281284110Sbaptpw_checkname(char *name, int gecos)
128220253Sjoerg{
1283109961Sgad	char showch[8];
1284284110Sbapt	const char *badchars, *ch, *showtype;
1285109961Sgad	int reject;
128620253Sjoerg
1287109961Sgad	ch = name;
1288109961Sgad	reject = 0;
1289109961Sgad	if (gecos) {
1290109961Sgad		/* See if the name is valid as a gecos (comment) field. */
1291109961Sgad		badchars = ":!@";
1292109961Sgad		showtype = "gecos field";
1293109961Sgad	} else {
1294109961Sgad		/* See if the name is valid as a userid or group. */
1295109961Sgad		badchars = " ,\t:+&#%$^()!@~*?<>=|\\/\"";
1296109961Sgad		showtype = "userid/group name";
1297109961Sgad		/* Userids and groups can not have a leading '-'. */
1298109961Sgad		if (*ch == '-')
1299109961Sgad			reject = 1;
130020253Sjoerg	}
1301109961Sgad	if (!reject) {
1302109961Sgad		while (*ch) {
1303109961Sgad			if (strchr(badchars, *ch) != NULL || *ch < ' ' ||
1304109961Sgad			    *ch == 127) {
1305109961Sgad				reject = 1;
1306109961Sgad				break;
1307109961Sgad			}
1308109961Sgad			/* 8-bit characters are only allowed in GECOS fields */
1309109961Sgad			if (!gecos && (*ch & 0x80)) {
1310109961Sgad				reject = 1;
1311109961Sgad				break;
1312109961Sgad			}
1313109961Sgad			ch++;
1314109961Sgad		}
1315109961Sgad	}
1316109961Sgad	/*
1317109961Sgad	 * A `$' is allowed as the final character for userids and groups,
1318109961Sgad	 * mainly for the benefit of samba.
1319109961Sgad	 */
1320109961Sgad	if (reject && !gecos) {
1321109961Sgad		if (*ch == '$' && *(ch + 1) == '\0') {
1322109961Sgad			reject = 0;
1323109961Sgad			ch++;
1324109961Sgad		}
1325109961Sgad	}
1326109961Sgad	if (reject) {
1327109961Sgad		snprintf(showch, sizeof(showch), (*ch >= ' ' && *ch < 127)
1328109961Sgad		    ? "`%c'" : "0x%02x", *ch);
1329228673Sdim		errx(EX_DATAERR, "invalid character %s at position %td in %s",
1330109961Sgad		    showch, (ch - name), showtype);
1331109961Sgad	}
1332109961Sgad	if (!gecos && (ch - name) > LOGNAMESIZE)
1333109961Sgad		errx(EX_DATAERR, "name too long `%s' (max is %d)", name,
1334109961Sgad		    LOGNAMESIZE);
1335284110Sbapt
1336284110Sbapt	return (name);
133720253Sjoerg}
133820253Sjoerg
133920253Sjoerg
134020253Sjoergstatic void
134120253Sjoergrmat(uid_t uid)
134220253Sjoerg{
134320253Sjoerg	DIR            *d = opendir("/var/at/jobs");
134420253Sjoerg
134520253Sjoerg	if (d != NULL) {
134620253Sjoerg		struct dirent  *e;
134720253Sjoerg
134820253Sjoerg		while ((e = readdir(d)) != NULL) {
134920253Sjoerg			struct stat     st;
135020253Sjoerg
135120253Sjoerg			if (strncmp(e->d_name, ".lock", 5) != 0 &&
135220253Sjoerg			    stat(e->d_name, &st) == 0 &&
135320253Sjoerg			    !S_ISDIR(st.st_mode) &&
135420253Sjoerg			    st.st_uid == uid) {
135520253Sjoerg				char            tmp[MAXPATHLEN];
135620253Sjoerg
1357282700Sbapt				snprintf(tmp, sizeof(tmp), "/usr/bin/atrm %s", e->d_name);
135820253Sjoerg				system(tmp);
135920253Sjoerg			}
136020253Sjoerg		}
136120253Sjoerg		closedir(d);
136220253Sjoerg	}
136320253Sjoerg}
136420747Sdavidn
136520747Sdavidnstatic void
136685145Sachermopie(char const * name)
136720747Sdavidn{
136885145Sache	static const char etcopie[] = "/etc/opiekeys";
136985145Sache	FILE   *fp = fopen(etcopie, "r+");
137020747Sdavidn
137121052Sdavidn	if (fp != NULL) {
137221052Sdavidn		char	tmp[1024];
137321052Sdavidn		off_t	atofs = 0;
137421052Sdavidn		int	length = strlen(name);
137520747Sdavidn
137621052Sdavidn		while (fgets(tmp, sizeof tmp, fp) != NULL) {
137721052Sdavidn			if (strncmp(name, tmp, length) == 0 && tmp[length]==' ') {
137821052Sdavidn				if (fseek(fp, atofs, SEEK_SET) == 0) {
137921052Sdavidn					fwrite("#", 1, 1, fp);	/* Comment username out */
138021052Sdavidn				}
138121052Sdavidn				break;
138220747Sdavidn			}
138321052Sdavidn			atofs = ftell(fp);
138420747Sdavidn		}
138521052Sdavidn		/*
138621052Sdavidn		 * If we got an error of any sort, don't update!
138721052Sdavidn		 */
138821052Sdavidn		fclose(fp);
138920747Sdavidn	}
139020747Sdavidn}
139120747Sdavidn
1392