pw_user.c revision 285405
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 285405 2015-07-11 20:10:12Z 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);
6220253Sjoergstatic char    *pw_password(struct userconf * cnf, struct cargs * args, char const * user);
6320253Sjoergstatic char    *shell_path(char const * path, char *shells[], char *sh);
6420253Sjoergstatic void     rmat(uid_t uid);
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)
40321242Sdavidn							goto direrr;
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) {
411219408Sjkim				if (mkdir(dbuf, _DEF_DIRMODE) == -1) {
41230259Scharnier				direrr:	err(EX_OSFILE, "mkdir '%s'", dbuf);
41321052Sdavidn				}
41421052Sdavidn				chown(dbuf, 0, 0);
41521052Sdavidn			}
41621052Sdavidn		} else if (!S_ISDIR(st.st_mode))
41730259Scharnier			errx(EX_OSFILE, "root home `%s' is not a directory", cnf->home);
41821052Sdavidn	}
41921052Sdavidn
42020253Sjoerg	if ((arg = getarg(args, 'e')) != NULL)
42120253Sjoerg		cnf->expire_days = atoi(arg->val);
42220253Sjoerg
42321330Sdavidn	if ((arg = getarg(args, 'y')) != NULL)
42421330Sdavidn		cnf->nispasswd = arg->val;
42521330Sdavidn
42620253Sjoerg	if ((arg = getarg(args, 'p')) != NULL && arg->val)
42720253Sjoerg		cnf->password_days = atoi(arg->val);
42820253Sjoerg
42920253Sjoerg	if ((arg = getarg(args, 'g')) != NULL) {
43063596Sdavidn		if (!*(p = arg->val))	/* Handle empty group list specially */
43163596Sdavidn			cnf->default_group = "";
43263596Sdavidn		else {
43363596Sdavidn			if ((grp = GETGRNAM(p)) == NULL) {
43463596Sdavidn				if (!isdigit((unsigned char)*p) || (grp = GETGRGID((gid_t) atoi(p))) == NULL)
43563596Sdavidn					errx(EX_NOUSER, "group `%s' does not exist", p);
43663596Sdavidn			}
43763596Sdavidn			cnf->default_group = newstr(grp->gr_name);
43820253Sjoerg		}
43920253Sjoerg	}
44020253Sjoerg	if ((arg = getarg(args, 'L')) != NULL)
441284110Sbapt		cnf->default_class = pw_checkname(arg->val, 0);
44220253Sjoerg
44320253Sjoerg	if ((arg = getarg(args, 'G')) != NULL && arg->val) {
44452527Sdavidn		int i = 0;
44520253Sjoerg
44620747Sdavidn		for (p = strtok(arg->val, ", \t"); p != NULL; p = strtok(NULL, ", \t")) {
44744229Sdavidn			if ((grp = GETGRNAM(p)) == NULL) {
44861957Sache				if (!isdigit((unsigned char)*p) || (grp = GETGRGID((gid_t) atoi(p))) == NULL)
44930259Scharnier					errx(EX_NOUSER, "group `%s' does not exist", p);
45020253Sjoerg			}
45120747Sdavidn			if (extendarray(&cnf->groups, &cnf->numgroups, i + 2) != -1)
45220747Sdavidn				cnf->groups[i++] = newstr(grp->gr_name);
45320253Sjoerg		}
45420747Sdavidn		while (i < cnf->numgroups)
45520253Sjoerg			cnf->groups[i++] = NULL;
45620253Sjoerg	}
45752527Sdavidn
45820253Sjoerg	if ((arg = getarg(args, 'k')) != NULL) {
45926088Sdavidn		if (stat(cnf->dotdir = arg->val, &st) == -1 || !S_ISDIR(st.st_mode))
46030259Scharnier			errx(EX_OSFILE, "skeleton `%s' is not a directory or does not exist", cnf->dotdir);
46120253Sjoerg	}
46252527Sdavidn
46320253Sjoerg	if ((arg = getarg(args, 's')) != NULL)
46420253Sjoerg		cnf->shell_default = arg->val;
46520253Sjoerg
46663600Sdavidn	if ((arg = getarg(args, 'w')) != NULL)
46763600Sdavidn		cnf->default_password = boolean_val(arg->val, cnf->default_password);
46820253Sjoerg	if (mode == M_ADD && getarg(args, 'D')) {
469284135Sbapt		if (name != NULL)
47030259Scharnier			errx(EX_DATAERR, "can't combine `-D' with `-n name'");
47120253Sjoerg		if ((arg = getarg(args, 'u')) != NULL && (p = strtok(arg->val, ", \t")) != NULL) {
47220253Sjoerg			if ((cnf->min_uid = (uid_t) atoi(p)) == 0)
47320253Sjoerg				cnf->min_uid = 1000;
47420253Sjoerg			if ((p = strtok(NULL, " ,\t")) == NULL || (cnf->max_uid = (uid_t) atoi(p)) < cnf->min_uid)
47520253Sjoerg				cnf->max_uid = 32000;
47620253Sjoerg		}
47720253Sjoerg		if ((arg = getarg(args, 'i')) != NULL && (p = strtok(arg->val, ", \t")) != NULL) {
47820253Sjoerg			if ((cnf->min_gid = (gid_t) atoi(p)) == 0)
47920253Sjoerg				cnf->min_gid = 1000;
48020253Sjoerg			if ((p = strtok(NULL, " ,\t")) == NULL || (cnf->max_gid = (gid_t) atoi(p)) < cnf->min_gid)
48120253Sjoerg				cnf->max_gid = 32000;
48220253Sjoerg		}
48320253Sjoerg
484284135Sbapt		if (write_userconfig(conf.config))
485284135Sbapt			return (EXIT_SUCCESS);
486283814Sbapt		err(EX_IOERR, "config udpate");
48720253Sjoerg	}
48852527Sdavidn
489284128Sbapt	if (name != NULL)
490284128Sbapt		pwd = GETPWNAM(pw_checkname(name, 0));
49120253Sjoerg
492284128Sbapt	if (id < 0 && name == NULL)
493284128Sbapt		errx(EX_DATAERR, "user name or id required");
49420253Sjoerg
49520253Sjoerg	/*
496285401Sbapt	 * Update require that the user exists
49720253Sjoerg	 */
498285405Sbapt	if (mode == M_UPDATE) {
49952527Sdavidn
500284128Sbapt		if (name == NULL && pwd == NULL)	/* Try harder */
501284128Sbapt			pwd = GETPWUID(id);
50220253Sjoerg
50320253Sjoerg		if (pwd == NULL) {
504284128Sbapt			if (name == NULL)
505284128Sbapt				errx(EX_NOUSER, "no such uid `%ld'", id);
506284128Sbapt			errx(EX_NOUSER, "no such user `%s'", name);
50720253Sjoerg		}
50852527Sdavidn
509284128Sbapt		if (name == NULL)
510284128Sbapt			name = pwd->pw_name;
51120253Sjoerg
51220253Sjoerg		/*
51320253Sjoerg		 * The rest is edit code
51420253Sjoerg		 */
515284129Sbapt		if (conf.newname != NULL) {
51620253Sjoerg			if (strcmp(pwd->pw_name, "root") == 0)
51730259Scharnier				errx(EX_DATAERR, "can't rename `root' account");
518284129Sbapt			pwd->pw_name = pw_checkname(conf.newname, 0);
51952527Sdavidn			edited = 1;
52020253Sjoerg		}
52152527Sdavidn
522284133Sbapt		if (id > 0 && isdigit((unsigned char)*arg->val)) {
523284133Sbapt			pwd->pw_uid = (uid_t)id;
52452527Sdavidn			edited = 1;
52520253Sjoerg			if (pwd->pw_uid != 0 && strcmp(pwd->pw_name, "root") == 0)
52630259Scharnier				errx(EX_DATAERR, "can't change uid of `root' account");
52720253Sjoerg			if (pwd->pw_uid == 0 && strcmp(pwd->pw_name, "root") != 0)
52830259Scharnier				warnx("WARNING: account `%s' will have a uid of 0 (superuser access!)", pwd->pw_name);
52920253Sjoerg		}
53020253Sjoerg
53152527Sdavidn		if ((arg = getarg(args, 'g')) != NULL && pwd->pw_uid != 0) {	/* Already checked this */
53252527Sdavidn			gid_t newgid = (gid_t) GETGRNAM(cnf->default_group)->gr_gid;
53352527Sdavidn			if (newgid != pwd->pw_gid) {
53452527Sdavidn				edited = 1;
53561762Sdavidn				pwd->pw_gid = newgid;
53652527Sdavidn			}
53752527Sdavidn		}
53852527Sdavidn
53920253Sjoerg		if ((arg = getarg(args, 'p')) != NULL) {
54052527Sdavidn			if (*arg->val == '\0' || strcmp(arg->val, "0") == 0) {
54152527Sdavidn				if (pwd->pw_change != 0) {
54252527Sdavidn					pwd->pw_change = 0;
54352527Sdavidn					edited = 1;
54452527Sdavidn				}
54552527Sdavidn			}
54620253Sjoerg			else {
54720253Sjoerg				time_t          now = time(NULL);
54820253Sjoerg				time_t          expire = parse_date(now, arg->val);
54920253Sjoerg
55052527Sdavidn				if (pwd->pw_change != expire) {
55152527Sdavidn					pwd->pw_change = expire;
55252527Sdavidn					edited = 1;
55352527Sdavidn				}
55420253Sjoerg			}
55520253Sjoerg		}
55652527Sdavidn
55720267Sjoerg		if ((arg = getarg(args, 'e')) != NULL) {
55852527Sdavidn			if (*arg->val == '\0' || strcmp(arg->val, "0") == 0) {
55952527Sdavidn				if (pwd->pw_expire != 0) {
56052527Sdavidn					pwd->pw_expire = 0;
56152527Sdavidn					edited = 1;
56252527Sdavidn				}
56352527Sdavidn			}
56420253Sjoerg			else {
56520253Sjoerg				time_t          now = time(NULL);
56620253Sjoerg				time_t          expire = parse_date(now, arg->val);
56720253Sjoerg
56852527Sdavidn				if (pwd->pw_expire != expire) {
56952527Sdavidn					pwd->pw_expire = expire;
57052527Sdavidn					edited = 1;
57152527Sdavidn				}
57220253Sjoerg			}
57320253Sjoerg		}
57420253Sjoerg
57552527Sdavidn		if ((arg = getarg(args, 's')) != NULL) {
57652527Sdavidn			char *shell = shell_path(cnf->shelldir, cnf->shells, arg->val);
57752527Sdavidn			if (shell == NULL)
57852527Sdavidn				shell = "";
57952527Sdavidn			if (strcmp(shell, pwd->pw_shell) != 0) {
58052527Sdavidn				pwd->pw_shell = shell;
58152527Sdavidn				edited = 1;
58252527Sdavidn			}
58352527Sdavidn		}
58420253Sjoerg
58552527Sdavidn		if (getarg(args, 'L')) {
58652527Sdavidn			if (cnf->default_class == NULL)
58752527Sdavidn				cnf->default_class = "";
58852527Sdavidn			if (strcmp(pwd->pw_class, cnf->default_class) != 0) {
58952527Sdavidn				pwd->pw_class = cnf->default_class;
59052527Sdavidn				edited = 1;
59152527Sdavidn			}
59252527Sdavidn		}
59352527Sdavidn
59420747Sdavidn		if ((arg  = getarg(args, 'd')) != NULL) {
595130629Srobert			if (strcmp(pwd->pw_dir, arg->val))
596130629Srobert				edited = 1;
59720747Sdavidn			if (stat(pwd->pw_dir = arg->val, &st) == -1) {
59820747Sdavidn				if (getarg(args, 'm') == NULL && strcmp(pwd->pw_dir, "/nonexistent") != 0)
59930259Scharnier				  warnx("WARNING: home `%s' does not exist", pwd->pw_dir);
60020747Sdavidn			} else if (!S_ISDIR(st.st_mode))
60130259Scharnier				warnx("WARNING: home `%s' is not a directory", pwd->pw_dir);
60220747Sdavidn		}
60320747Sdavidn
604285137Sbapt		if ((arg = getarg(args, 'w')) != NULL && conf.fd == -1) {
60564918Sgreen			login_cap_t *lc;
60664918Sgreen
60764918Sgreen			lc = login_getpwclass(pwd);
60864918Sgreen			if (lc == NULL ||
609252688Sdes			    login_setcryptfmt(lc, "sha512", NULL) == NULL)
61064918Sgreen				warn("setting crypt(3) format");
61164918Sgreen			login_close(lc);
61220267Sjoerg			pwd->pw_passwd = pw_password(cnf, args, pwd->pw_name);
61352527Sdavidn			edited = 1;
61452527Sdavidn		}
61520267Sjoerg
61620253Sjoerg	} else {
61764918Sgreen		login_cap_t *lc;
61852527Sdavidn
61952527Sdavidn		/*
62052527Sdavidn		 * Add code
62152527Sdavidn		 */
62252527Sdavidn
623284128Sbapt		if (name == NULL)	/* Required */
62430259Scharnier			errx(EX_DATAERR, "login name required");
625284128Sbapt		else if ((pwd = GETPWNAM(name)) != NULL)	/* Exists */
626284128Sbapt			errx(EX_DATAERR, "login name `%s' already exists", name);
62720253Sjoerg
62820253Sjoerg		/*
62920253Sjoerg		 * Now, set up defaults for a new user
63020253Sjoerg		 */
63120253Sjoerg		pwd = &fakeuser;
632284128Sbapt		pwd->pw_name = name;
63320253Sjoerg		pwd->pw_class = cnf->default_class ? cnf->default_class : "";
634284133Sbapt		pwd->pw_uid = pw_uidpolicy(cnf, id);
635284118Sbapt		pwd->pw_gid = pw_gidpolicy(args, pwd->pw_name, (gid_t) pwd->pw_uid);
63620253Sjoerg		pwd->pw_change = pw_pwdpolicy(cnf, args);
63720253Sjoerg		pwd->pw_expire = pw_exppolicy(cnf, args);
63820253Sjoerg		pwd->pw_dir = pw_homepolicy(cnf, args, pwd->pw_name);
63920253Sjoerg		pwd->pw_shell = pw_shellpolicy(cnf, args, NULL);
64064918Sgreen		lc = login_getpwclass(pwd);
641272833Sdes		if (lc == NULL || login_setcryptfmt(lc, "sha512", NULL) == NULL)
64264918Sgreen			warn("setting crypt(3) format");
64364918Sgreen		login_close(lc);
64464918Sgreen		pwd->pw_passwd = pw_password(cnf, args, pwd->pw_name);
64552527Sdavidn		edited = 1;
64620253Sjoerg
64720253Sjoerg		if (pwd->pw_uid == 0 && strcmp(pwd->pw_name, "root") != 0)
64830259Scharnier			warnx("WARNING: new account `%s' has a uid of 0 (superuser access!)", pwd->pw_name);
64920253Sjoerg	}
65020253Sjoerg
65120253Sjoerg	/*
65220253Sjoerg	 * Shared add/edit code
65320253Sjoerg	 */
65452527Sdavidn	if ((arg = getarg(args, 'c')) != NULL) {
655284110Sbapt		char	*gecos = pw_checkname(arg->val, 1);
65652527Sdavidn		if (strcmp(pwd->pw_gecos, gecos) != 0) {
65752527Sdavidn			pwd->pw_gecos = gecos;
65852527Sdavidn			edited = 1;
65952527Sdavidn		}
66052527Sdavidn	}
66120253Sjoerg
662285133Sbapt	if (conf.fd != -1)
663285137Sbapt		edited = set_passwd(pwd, mode == M_UPDATE);
66420253Sjoerg
66520267Sjoerg	/*
66620267Sjoerg	 * Special case: -N only displays & exits
66720267Sjoerg	 */
668284121Sbapt	if (conf.dryrun)
669284126Sbapt		return print_user(pwd);
67020267Sjoerg
67121330Sdavidn	if (mode == M_ADD) {
67252527Sdavidn		edited = 1;	/* Always */
67352502Sdavidn		rc = addpwent(pwd);
674283814Sbapt		if (rc == -1)
675283814Sbapt			errx(EX_IOERR, "user '%s' already exists",
676283814Sbapt			    pwd->pw_name);
677283814Sbapt		else if (rc != 0)
678283814Sbapt			err(EX_IOERR, "passwd file update");
67952502Sdavidn		if (cnf->nispasswd && *cnf->nispasswd=='/') {
68052502Sdavidn			rc = addnispwent(cnf->nispasswd, pwd);
68152502Sdavidn			if (rc == -1)
68252502Sdavidn				warnx("User '%s' already exists in NIS passwd", pwd->pw_name);
68352502Sdavidn			else
68456000Sdavidn				warn("NIS passwd update");
68552502Sdavidn			/* NOTE: we treat NIS-only update errors as non-fatal */
68652502Sdavidn		}
687285405Sbapt	} else if (mode == M_UPDATE && edited) /* Only updated this if required */
688285405Sbapt		perform_chgpwent(name, pwd);
68921330Sdavidn
69020253Sjoerg	/*
69120253Sjoerg	 * Ok, user is created or changed - now edit group file
69220253Sjoerg	 */
69320253Sjoerg
694242349Sbapt	if (mode == M_ADD || getarg(args, 'G') != NULL) {
695273779Sbapt		int i, j;
696273779Sbapt		/* First remove the user from all group */
697273779Sbapt		SETGRENT();
698273779Sbapt		while ((grp = GETGRENT()) != NULL) {
699273779Sbapt			char group[MAXLOGNAME];
700273779Sbapt			if (grp->gr_mem == NULL)
701273779Sbapt				continue;
702273779Sbapt			for (i = 0; grp->gr_mem[i] != NULL; i++) {
703273779Sbapt				if (strcmp(grp->gr_mem[i] , pwd->pw_name) != 0)
704273779Sbapt					continue;
705273779Sbapt				for (j = i; grp->gr_mem[j] != NULL ; j++)
706273779Sbapt					grp->gr_mem[j] = grp->gr_mem[j+1];
707273779Sbapt				strlcpy(group, grp->gr_name, MAXLOGNAME);
708273779Sbapt				chggrent(group, grp);
709273779Sbapt			}
710273779Sbapt		}
711273779Sbapt		ENDGRENT();
712273779Sbapt
713273779Sbapt		/* now add to group where needed */
714242349Sbapt		for (i = 0; cnf->groups[i] != NULL; i++) {
715242349Sbapt			grp = GETGRNAM(cnf->groups[i]);
716244737Sbapt			grp = gr_add(grp, pwd->pw_name);
717244737Sbapt			/*
718244737Sbapt			 * grp can only be NULL in 2 cases:
719244737Sbapt			 * - the new member is already a member
720244737Sbapt			 * - a problem with memory occurs
721244737Sbapt			 * in both cases we want to skip now.
722244737Sbapt			 */
723244737Sbapt			if (grp == NULL)
724242349Sbapt				continue;
725242349Sbapt			chggrent(cnf->groups[i], grp);
726245114Smjg			free(grp);
727242349Sbapt		}
728242349Sbapt	}
729242349Sbapt
730242349Sbapt
73161759Sdavidn	/* go get a current version of pwd */
732284128Sbapt	pwd = GETPWNAM(name);
73361759Sdavidn	if (pwd == NULL) {
73461759Sdavidn		/* This will fail when we rename, so special case that */
735284129Sbapt		if (mode == M_UPDATE && conf.newname != NULL) {
736284129Sbapt			name = conf.newname;		/* update new name */
737284128Sbapt			pwd = GETPWNAM(name);	/* refetch renamed rec */
73861759Sdavidn		}
73961759Sdavidn	}
74061759Sdavidn	if (pwd == NULL)	/* can't go on without this */
741284128Sbapt		errx(EX_NOUSER, "user '%s' disappeared during update", name);
74220253Sjoerg
74344229Sdavidn	grp = GETGRGID(pwd->pw_gid);
744283842Sbapt	pw_log(cnf, mode, W_USER, "%s(%u):%s(%u):%s:%s:%s",
745283842Sbapt	       pwd->pw_name, pwd->pw_uid,
746283842Sbapt	    grp ? grp->gr_name : "unknown", (grp ? grp->gr_gid : (uid_t)-1),
74720253Sjoerg	       pwd->pw_gecos, pwd->pw_dir, pwd->pw_shell);
74820253Sjoerg
74920253Sjoerg	/*
75020253Sjoerg	 * If adding, let's touch and chown the user's mail file. This is not
75120253Sjoerg	 * strictly necessary under BSD with a 0755 maildir but it also
75220253Sjoerg	 * doesn't hurt anything to create the empty mailfile
75320253Sjoerg	 */
75420253Sjoerg	if (mode == M_ADD) {
755283961Sbapt		if (PWALTDIR() != PWF_ALT) {
756283961Sbapt			arg = getarg(args, 'R');
757283961Sbapt			snprintf(path, sizeof(path), "%s%s/%s",
758283961Sbapt			    arg ? arg->val : "", _PATH_MAILDIR, pwd->pw_name);
759283961Sbapt			close(open(path, O_RDWR | O_CREAT, 0600));	/* Preserve contents &
76044229Sdavidn									 * mtime */
761283961Sbapt			chown(path, pwd->pw_uid, pwd->pw_gid);
76220253Sjoerg		}
76320253Sjoerg	}
76452527Sdavidn
76520253Sjoerg	/*
76682868Sdd	 * Let's create and populate the user's home directory. Note
76720253Sjoerg	 * that this also `works' for editing users if -m is used, but
76820253Sjoerg	 * existing files will *not* be overwritten.
76920253Sjoerg	 */
770283961Sbapt	if (PWALTDIR() != PWF_ALT && getarg(args, 'm') != NULL && pwd->pw_dir &&
771283961Sbapt	    *pwd->pw_dir == '/' && pwd->pw_dir[1])
772285403Sbapt		create_and_populate_homedir(pwd);
77352527Sdavidn
77482868Sdd	/*
77582868Sdd	 * Finally, send mail to the new user as well, if we are asked to
77682868Sdd	 */
77782868Sdd	if (mode == M_ADD && !PWALTDIR() && cnf->newmail && *cnf->newmail && (fp = fopen(cnf->newmail, "r")) != NULL) {
77882868Sdd		FILE           *pfp = popen(_PATH_SENDMAIL " -t", "w");
77982868Sdd
78082868Sdd		if (pfp == NULL)
78182868Sdd			warn("sendmail");
78282868Sdd		else {
78382868Sdd			fprintf(pfp, "From: root\n" "To: %s\n" "Subject: Welcome!\n\n", pwd->pw_name);
78482868Sdd			while (fgets(line, sizeof(line), fp) != NULL) {
78582868Sdd				/* Do substitutions? */
78682868Sdd				fputs(line, pfp);
78782868Sdd			}
78882868Sdd			pclose(pfp);
789283842Sbapt			pw_log(cnf, mode, W_USER, "%s(%u) new user mail sent",
790283842Sbapt			    pwd->pw_name, pwd->pw_uid);
79182868Sdd		}
79282868Sdd		fclose(fp);
79382868Sdd	}
79482868Sdd
79520267Sjoerg	return EXIT_SUCCESS;
79620253Sjoerg}
79720253Sjoerg
79820253Sjoerg
79920253Sjoergstatic          uid_t
800284133Sbaptpw_uidpolicy(struct userconf * cnf, long id)
80120253Sjoerg{
80220253Sjoerg	struct passwd  *pwd;
80320253Sjoerg	uid_t           uid = (uid_t) - 1;
80420253Sjoerg
80520253Sjoerg	/*
80620253Sjoerg	 * Check the given uid, if any
80720253Sjoerg	 */
808284133Sbapt	if (id > 0) {
809284133Sbapt		uid = (uid_t) id;
81020253Sjoerg
811284133Sbapt		if ((pwd = GETPWUID(uid)) != NULL && conf.checkduplicate)
812283842Sbapt			errx(EX_DATAERR, "uid `%u' has already been allocated", pwd->pw_uid);
81320253Sjoerg	} else {
81420253Sjoerg		struct bitmap   bm;
81520253Sjoerg
81620253Sjoerg		/*
81720253Sjoerg		 * We need to allocate the next available uid under one of
81820253Sjoerg		 * two policies a) Grab the first unused uid b) Grab the
81920253Sjoerg		 * highest possible unused uid
82020253Sjoerg		 */
82120253Sjoerg		if (cnf->min_uid >= cnf->max_uid) {	/* Sanity
82220253Sjoerg							 * claus^H^H^H^Hheck */
82320253Sjoerg			cnf->min_uid = 1000;
82420253Sjoerg			cnf->max_uid = 32000;
82520253Sjoerg		}
82620253Sjoerg		bm = bm_alloc(cnf->max_uid - cnf->min_uid + 1);
82720253Sjoerg
82820253Sjoerg		/*
82920253Sjoerg		 * Now, let's fill the bitmap from the password file
83020253Sjoerg		 */
83144229Sdavidn		SETPWENT();
83244229Sdavidn		while ((pwd = GETPWENT()) != NULL)
83344229Sdavidn			if (pwd->pw_uid >= (uid_t) cnf->min_uid && pwd->pw_uid <= (uid_t) cnf->max_uid)
83420253Sjoerg				bm_setbit(&bm, pwd->pw_uid - cnf->min_uid);
83544229Sdavidn		ENDPWENT();
83620253Sjoerg
83720253Sjoerg		/*
83820253Sjoerg		 * Then apply the policy, with fallback to reuse if necessary
83920253Sjoerg		 */
84020253Sjoerg		if (cnf->reuse_uids || (uid = (uid_t) (bm_lastset(&bm) + cnf->min_uid + 1)) > cnf->max_uid)
84120253Sjoerg			uid = (uid_t) (bm_firstunset(&bm) + cnf->min_uid);
84220253Sjoerg
84320253Sjoerg		/*
84420253Sjoerg		 * Another sanity check
84520253Sjoerg		 */
84620253Sjoerg		if (uid < cnf->min_uid || uid > cnf->max_uid)
84730259Scharnier			errx(EX_SOFTWARE, "unable to allocate a new uid - range fully used");
84820253Sjoerg		bm_dealloc(&bm);
84920253Sjoerg	}
85020253Sjoerg	return uid;
85120253Sjoerg}
85220253Sjoerg
85320253Sjoerg
85420253Sjoergstatic          uid_t
855284118Sbaptpw_gidpolicy(struct cargs * args, char *nam, gid_t prefer)
85620253Sjoerg{
85720253Sjoerg	struct group   *grp;
85820253Sjoerg	gid_t           gid = (uid_t) - 1;
85920253Sjoerg	struct carg    *a_gid = getarg(args, 'g');
860284118Sbapt	struct userconf	*cnf = conf.userconf;
86120253Sjoerg
86220253Sjoerg	/*
86320253Sjoerg	 * If no arg given, see if default can help out
86420253Sjoerg	 */
86520253Sjoerg	if (a_gid == NULL && cnf->default_group && *cnf->default_group)
86620253Sjoerg		a_gid = addarg(args, 'g', cnf->default_group);
86720253Sjoerg
86820253Sjoerg	/*
86920253Sjoerg	 * Check the given gid, if any
87020253Sjoerg	 */
87144229Sdavidn	SETGRENT();
87220253Sjoerg	if (a_gid != NULL) {
87344229Sdavidn		if ((grp = GETGRNAM(a_gid->val)) == NULL) {
87420253Sjoerg			gid = (gid_t) atol(a_gid->val);
87561957Sache			if ((gid == 0 && !isdigit((unsigned char)*a_gid->val)) || (grp = GETGRGID(gid)) == NULL)
87630259Scharnier				errx(EX_NOUSER, "group `%s' is not defined", a_gid->val);
87720253Sjoerg		}
87820253Sjoerg		gid = grp->gr_gid;
879262865Sjulian	} else if ((grp = GETGRNAM(nam)) != NULL &&
880262865Sjulian	    (grp->gr_mem == NULL || grp->gr_mem[0] == NULL)) {
88120267Sjoerg		gid = grp->gr_gid;  /* Already created? Use it anyway... */
88220253Sjoerg	} else {
88320253Sjoerg		struct cargs    grpargs;
88420253Sjoerg		char            tmp[32];
88520253Sjoerg
88620253Sjoerg		LIST_INIT(&grpargs);
88720253Sjoerg
88820253Sjoerg		/*
88920253Sjoerg		 * We need to auto-create a group with the user's name. We
89020253Sjoerg		 * can send all the appropriate output to our sister routine
89120253Sjoerg		 * bit first see if we can create a group with gid==uid so we
89220253Sjoerg		 * can keep the user and group ids in sync. We purposely do
89320253Sjoerg		 * NOT check the gid range if we can force the sync. If the
89420253Sjoerg		 * user's name dups an existing group, then the group add
89520253Sjoerg		 * function will happily handle that case for us and exit.
89620253Sjoerg		 */
89744229Sdavidn		if (GETGRGID(prefer) == NULL) {
898282700Sbapt			snprintf(tmp, sizeof(tmp), "%u", prefer);
89920253Sjoerg			addarg(&grpargs, 'g', tmp);
90020253Sjoerg		}
901284121Sbapt		if (conf.dryrun) {
902285395Sbapt			gid = pw_groupnext(cnf, true);
903285395Sbapt		} else {
904284128Sbapt			pw_group(M_ADD, nam, -1, &grpargs);
90544229Sdavidn			if ((grp = GETGRNAM(nam)) != NULL)
90620267Sjoerg				gid = grp->gr_gid;
90720267Sjoerg		}
90870486Sben		a_gid = LIST_FIRST(&grpargs);
90920253Sjoerg		while (a_gid != NULL) {
91070486Sben			struct carg    *t = LIST_NEXT(a_gid, list);
91120253Sjoerg			LIST_REMOVE(a_gid, list);
91220253Sjoerg			a_gid = t;
91320253Sjoerg		}
91420253Sjoerg	}
91544229Sdavidn	ENDGRENT();
91620253Sjoerg	return gid;
91720253Sjoerg}
91820253Sjoerg
91920253Sjoerg
92020253Sjoergstatic          time_t
92120253Sjoergpw_pwdpolicy(struct userconf * cnf, struct cargs * args)
92220253Sjoerg{
92320253Sjoerg	time_t          result = 0;
92420253Sjoerg	time_t          now = time(NULL);
92527831Sdavidn	struct carg    *arg = getarg(args, 'p');
92620253Sjoerg
92720253Sjoerg	if (arg != NULL) {
92820253Sjoerg		if ((result = parse_date(now, arg->val)) == now)
92930259Scharnier			errx(EX_DATAERR, "invalid date/time `%s'", arg->val);
93020253Sjoerg	} else if (cnf->password_days > 0)
93120253Sjoerg		result = now + ((long) cnf->password_days * 86400L);
93220253Sjoerg	return result;
93320253Sjoerg}
93420253Sjoerg
93520253Sjoerg
93620253Sjoergstatic          time_t
93720253Sjoergpw_exppolicy(struct userconf * cnf, struct cargs * args)
93820253Sjoerg{
93920253Sjoerg	time_t          result = 0;
94020253Sjoerg	time_t          now = time(NULL);
94120253Sjoerg	struct carg    *arg = getarg(args, 'e');
94220253Sjoerg
94320253Sjoerg	if (arg != NULL) {
94420253Sjoerg		if ((result = parse_date(now, arg->val)) == now)
94530259Scharnier			errx(EX_DATAERR, "invalid date/time `%s'", arg->val);
94620253Sjoerg	} else if (cnf->expire_days > 0)
94720253Sjoerg		result = now + ((long) cnf->expire_days * 86400L);
94820253Sjoerg	return result;
94920253Sjoerg}
95020253Sjoerg
95120253Sjoerg
95220253Sjoergstatic char    *
95320253Sjoergpw_homepolicy(struct userconf * cnf, struct cargs * args, char const * user)
95420253Sjoerg{
95520253Sjoerg	struct carg    *arg = getarg(args, 'd');
956282699Sbapt	static char     home[128];
95720253Sjoerg
95820253Sjoerg	if (arg)
959282699Sbapt		return (arg->val);
96020253Sjoerg
961282699Sbapt	if (cnf->home == NULL || *cnf->home == '\0')
962282699Sbapt		errx(EX_CONFIG, "no base home directory set");
963282699Sbapt	snprintf(home, sizeof(home), "%s/%s", cnf->home, user);
964282699Sbapt
965282699Sbapt	return (home);
96620253Sjoerg}
96720253Sjoerg
96820253Sjoergstatic char    *
96920253Sjoergshell_path(char const * path, char *shells[], char *sh)
97020253Sjoerg{
97120253Sjoerg	if (sh != NULL && (*sh == '/' || *sh == '\0'))
97220253Sjoerg		return sh;	/* specified full path or forced none */
97320253Sjoerg	else {
97420253Sjoerg		char           *p;
97520253Sjoerg		char            paths[_UC_MAXLINE];
97620253Sjoerg
97720253Sjoerg		/*
97820253Sjoerg		 * We need to search paths
97920253Sjoerg		 */
980130633Srobert		strlcpy(paths, path, sizeof(paths));
98120253Sjoerg		for (p = strtok(paths, ": \t\r\n"); p != NULL; p = strtok(NULL, ": \t\r\n")) {
98220253Sjoerg			int             i;
98320253Sjoerg			static char     shellpath[256];
98420253Sjoerg
98520253Sjoerg			if (sh != NULL) {
986282700Sbapt				snprintf(shellpath, sizeof(shellpath), "%s/%s", p, sh);
98720253Sjoerg				if (access(shellpath, X_OK) == 0)
98820253Sjoerg					return shellpath;
98920253Sjoerg			} else
99020253Sjoerg				for (i = 0; i < _UC_MAXSHELLS && shells[i] != NULL; i++) {
991282700Sbapt					snprintf(shellpath, sizeof(shellpath), "%s/%s", p, shells[i]);
99220253Sjoerg					if (access(shellpath, X_OK) == 0)
99320253Sjoerg						return shellpath;
99420253Sjoerg				}
99520253Sjoerg		}
99620253Sjoerg		if (sh == NULL)
99730259Scharnier			errx(EX_OSFILE, "can't find shell `%s' in shell paths", sh);
99830259Scharnier		errx(EX_CONFIG, "no default shell available or defined");
99920253Sjoerg		return NULL;
100020253Sjoerg	}
100120253Sjoerg}
100220253Sjoerg
100320253Sjoerg
100420253Sjoergstatic char    *
100520253Sjoergpw_shellpolicy(struct userconf * cnf, struct cargs * args, char *newshell)
100620253Sjoerg{
100720253Sjoerg	char           *sh = newshell;
100820253Sjoerg	struct carg    *arg = getarg(args, 's');
100920253Sjoerg
101020253Sjoerg	if (newshell == NULL && arg != NULL)
101120253Sjoerg		sh = arg->val;
101220253Sjoerg	return shell_path(cnf->shelldir, cnf->shells, sh ? sh : cnf->shell_default);
101320253Sjoerg}
101420253Sjoerg
1015179365Santoine#define	SALTSIZE	32
101620253Sjoerg
1017179365Santoinestatic char const chars[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ./";
1018179365Santoine
101920253Sjoergchar           *
102020253Sjoergpw_pwcrypt(char *password)
102120253Sjoerg{
102220253Sjoerg	int             i;
1023179365Santoine	char            salt[SALTSIZE + 1];
1024231994Skevlo	char		*cryptpw;
102520253Sjoerg
102620253Sjoerg	static char     buf[256];
102720253Sjoerg
102820253Sjoerg	/*
102920253Sjoerg	 * Calculate a salt value
103020253Sjoerg	 */
1031179365Santoine	for (i = 0; i < SALTSIZE; i++)
1032181785Sache		salt[i] = chars[arc4random_uniform(sizeof(chars) - 1)];
1033179365Santoine	salt[SALTSIZE] = '\0';
103420253Sjoerg
1035231994Skevlo	cryptpw = crypt(password, salt);
1036231994Skevlo	if (cryptpw == NULL)
1037231994Skevlo		errx(EX_CONFIG, "crypt(3) failure");
1038231994Skevlo	return strcpy(buf, cryptpw);
103920253Sjoerg}
104020253Sjoerg
104120590Sdavidn
104220253Sjoergstatic char    *
104320253Sjoergpw_password(struct userconf * cnf, struct cargs * args, char const * user)
104420253Sjoerg{
104520253Sjoerg	int             i, l;
104620253Sjoerg	char            pwbuf[32];
104720253Sjoerg
104820253Sjoerg	switch (cnf->default_password) {
104920253Sjoerg	case -1:		/* Random password */
105073563Skris		l = (arc4random() % 8 + 8);	/* 8 - 16 chars */
105120253Sjoerg		for (i = 0; i < l; i++)
1052181785Sache			pwbuf[i] = chars[arc4random_uniform(sizeof(chars)-1)];
105320253Sjoerg		pwbuf[i] = '\0';
105420253Sjoerg
105520253Sjoerg		/*
105620253Sjoerg		 * We give this information back to the user
105720253Sjoerg		 */
1058285137Sbapt		if (conf.fd == -1 && !conf.dryrun) {
105961957Sache			if (isatty(STDOUT_FILENO))
106020712Sdavidn				printf("Password for '%s' is: ", user);
106120253Sjoerg			printf("%s\n", pwbuf);
106220253Sjoerg			fflush(stdout);
106320253Sjoerg		}
106420253Sjoerg		break;
106520253Sjoerg
106620253Sjoerg	case -2:		/* No password at all! */
106720253Sjoerg		return "";
106820253Sjoerg
106920253Sjoerg	case 0:		/* No login - default */
107020253Sjoerg	default:
107120253Sjoerg		return "*";
107220253Sjoerg
107320253Sjoerg	case 1:		/* user's name */
1074130633Srobert		strlcpy(pwbuf, user, sizeof(pwbuf));
107520253Sjoerg		break;
107620253Sjoerg	}
107720253Sjoerg	return pw_pwcrypt(pwbuf);
107820253Sjoerg}
107920253Sjoerg
1080284111Sbaptstatic int
1081285401Sbaptpw_userdel(char *name, long id)
1082284111Sbapt{
1083285401Sbapt	struct passwd *pwd = NULL;
1084284111Sbapt	char		 file[MAXPATHLEN];
1085284111Sbapt	char		 home[MAXPATHLEN];
1086285401Sbapt	uid_t		 uid;
1087284111Sbapt	struct group	*gr, *grp;
1088284111Sbapt	char		 grname[LOGNAMESIZE];
1089284111Sbapt	int		 rc;
1090284111Sbapt	struct stat	 st;
109120253Sjoerg
1092285401Sbapt	if (id < 0 && name == NULL)
1093285401Sbapt		errx(EX_DATAERR, "username or id required");
1094285401Sbapt
1095285401Sbapt	pwd = (name != NULL) ? GETPWNAM(pw_checkname(name, 0)) : GETPWUID(id);
1096285401Sbapt	if (pwd == NULL) {
1097285401Sbapt		if (name == NULL)
1098285401Sbapt			errx(EX_NOUSER, "no such uid `%ld'", id);
1099285401Sbapt		errx(EX_NOUSER, "no such user `%s'", name);
1100285401Sbapt	}
1101285401Sbapt	uid = pwd->pw_uid;
1102285401Sbapt	if (name == NULL)
1103285401Sbapt		name = pwd->pw_name;
1104285401Sbapt
1105284111Sbapt	if (strcmp(pwd->pw_name, "root") == 0)
1106284111Sbapt		errx(EX_DATAERR, "cannot remove user 'root'");
1107284111Sbapt
1108284111Sbapt	if (!PWALTDIR()) {
1109284111Sbapt		/*
1110284111Sbapt		 * Remove opie record from /etc/opiekeys
1111284111Sbapt		*/
1112284111Sbapt
1113284111Sbapt		rmopie(pwd->pw_name);
1114284111Sbapt
1115284111Sbapt		/*
1116284111Sbapt		 * Remove crontabs
1117284111Sbapt		 */
1118284111Sbapt		snprintf(file, sizeof(file), "/var/cron/tabs/%s", pwd->pw_name);
1119284111Sbapt		if (access(file, F_OK) == 0) {
1120284111Sbapt			snprintf(file, sizeof(file), "crontab -u %s -r", pwd->pw_name);
1121284111Sbapt			system(file);
1122284111Sbapt		}
1123284111Sbapt	}
1124284111Sbapt	/*
1125284111Sbapt	 * Save these for later, since contents of pwd may be
1126284111Sbapt	 * invalidated by deletion
1127284111Sbapt	 */
1128284111Sbapt	snprintf(file, sizeof(file), "%s/%s", _PATH_MAILDIR, pwd->pw_name);
1129284111Sbapt	strlcpy(home, pwd->pw_dir, sizeof(home));
1130284111Sbapt	gr = GETGRGID(pwd->pw_gid);
1131284111Sbapt	if (gr != NULL)
1132284111Sbapt		strlcpy(grname, gr->gr_name, LOGNAMESIZE);
1133284111Sbapt	else
1134284111Sbapt		grname[0] = '\0';
1135284111Sbapt
1136284111Sbapt	rc = delpwent(pwd);
1137284111Sbapt	if (rc == -1)
1138284111Sbapt		err(EX_IOERR, "user '%s' does not exist", pwd->pw_name);
1139284111Sbapt	else if (rc != 0)
1140284111Sbapt		err(EX_IOERR, "passwd update");
1141284111Sbapt
1142285401Sbapt	if (conf.userconf->nispasswd && *conf.userconf->nispasswd=='/') {
1143285401Sbapt		rc = delnispwent(conf.userconf->nispasswd, name);
1144284111Sbapt		if (rc == -1)
1145285401Sbapt			warnx("WARNING: user '%s' does not exist in NIS passwd",
1146285401Sbapt			    pwd->pw_name);
1147284111Sbapt		else if (rc != 0)
1148284111Sbapt			warn("WARNING: NIS passwd update");
1149284111Sbapt		/* non-fatal */
1150284111Sbapt	}
1151284111Sbapt
1152284128Sbapt	grp = GETGRNAM(name);
1153284111Sbapt	if (grp != NULL &&
1154284111Sbapt	    (grp->gr_mem == NULL || *grp->gr_mem == NULL) &&
1155284128Sbapt	    strcmp(name, grname) == 0)
1156284128Sbapt		delgrent(GETGRNAM(name));
1157284111Sbapt	SETGRENT();
1158284111Sbapt	while ((grp = GETGRENT()) != NULL) {
1159284111Sbapt		int i, j;
1160284111Sbapt		char group[MAXLOGNAME];
1161284113Sbapt		if (grp->gr_mem == NULL)
1162284113Sbapt			continue;
1163284113Sbapt
1164284113Sbapt		for (i = 0; grp->gr_mem[i] != NULL; i++) {
1165284128Sbapt			if (strcmp(grp->gr_mem[i], name) != 0)
1166284113Sbapt				continue;
1167284113Sbapt
1168284113Sbapt			for (j = i; grp->gr_mem[j] != NULL; j++)
1169284113Sbapt				grp->gr_mem[j] = grp->gr_mem[j+1];
1170284113Sbapt			strlcpy(group, grp->gr_name, MAXLOGNAME);
1171284113Sbapt			chggrent(group, grp);
1172284111Sbapt		}
1173284111Sbapt	}
1174284111Sbapt	ENDGRENT();
1175284111Sbapt
1176285401Sbapt	pw_log(conf.userconf, M_DELETE, W_USER, "%s(%u) account removed", name,
1177285401Sbapt	    uid);
1178284111Sbapt
1179284117Sbapt	if (!PWALTDIR()) {
1180284111Sbapt		/*
1181284111Sbapt		 * Remove mail file
1182284111Sbapt		 */
1183284111Sbapt		remove(file);
1184284111Sbapt
1185284111Sbapt		/*
1186284111Sbapt		 * Remove at jobs
1187284111Sbapt		 */
1188284111Sbapt		if (getpwuid(uid) == NULL)
1189284111Sbapt			rmat(uid);
1190284111Sbapt
1191284111Sbapt		/*
1192284111Sbapt		 * Remove home directory and contents
1193284111Sbapt		 */
1194285401Sbapt		if (conf.deletehome && *home == '/' && getpwuid(uid) == NULL &&
1195284112Sbapt		    stat(home, &st) != -1) {
1196284112Sbapt			rm_r(home, uid);
1197285401Sbapt			pw_log(conf.userconf, M_DELETE, W_USER, "%s(%u) home '%s' %sremoved",
1198284128Sbapt			       name, uid, home,
1199284112Sbapt			       stat(home, &st) == -1 ? "" : "not completely ");
1200284111Sbapt		}
1201284111Sbapt	}
1202284111Sbapt
1203284111Sbapt	return (EXIT_SUCCESS);
1204284111Sbapt}
1205284111Sbapt
120620253Sjoergstatic int
1207284124Sbaptprint_user(struct passwd * pwd)
120820253Sjoerg{
1209284122Sbapt	if (!conf.pretty) {
1210242349Sbapt		char            *buf;
121120253Sjoerg
1212284124Sbapt		buf = conf.v7 ? pw_make_v7(pwd) : pw_make(pwd);
1213242349Sbapt		printf("%s\n", buf);
1214242349Sbapt		free(buf);
121520253Sjoerg	} else {
121620267Sjoerg		int		j;
121720253Sjoerg		char           *p;
121844229Sdavidn		struct group   *grp = GETGRGID(pwd->pw_gid);
121920253Sjoerg		char            uname[60] = "User &", office[60] = "[None]",
122020253Sjoerg		                wphone[60] = "[None]", hphone[60] = "[None]";
122120590Sdavidn		char		acexpire[32] = "[None]", pwexpire[32] = "[None]";
122220590Sdavidn		struct tm *    tptr;
122320253Sjoerg
122420253Sjoerg		if ((p = strtok(pwd->pw_gecos, ",")) != NULL) {
1225130633Srobert			strlcpy(uname, p, sizeof(uname));
122620253Sjoerg			if ((p = strtok(NULL, ",")) != NULL) {
1227130633Srobert				strlcpy(office, p, sizeof(office));
122820253Sjoerg				if ((p = strtok(NULL, ",")) != NULL) {
1229130633Srobert					strlcpy(wphone, p, sizeof(wphone));
123020253Sjoerg					if ((p = strtok(NULL, "")) != NULL) {
1231130633Srobert						strlcpy(hphone, p,
1232130633Srobert						    sizeof(hphone));
123320253Sjoerg					}
123420253Sjoerg				}
123520253Sjoerg			}
123620253Sjoerg		}
123720253Sjoerg		/*
123820253Sjoerg		 * Handle '&' in gecos field
123920253Sjoerg		 */
124020253Sjoerg		if ((p = strchr(uname, '&')) != NULL) {
124120253Sjoerg			int             l = strlen(pwd->pw_name);
124220253Sjoerg			int             m = strlen(p);
124320253Sjoerg
124420253Sjoerg			memmove(p + l, p + 1, m);
124520253Sjoerg			memmove(p, pwd->pw_name, l);
124661957Sache			*p = (char) toupper((unsigned char)*p);
124720253Sjoerg		}
124820590Sdavidn		if (pwd->pw_expire > (time_t)0 && (tptr = localtime(&pwd->pw_expire)) != NULL)
124974569Sache			strftime(acexpire, sizeof acexpire, "%c", tptr);
125061957Sache		if (pwd->pw_change > (time_t)0 && (tptr = localtime(&pwd->pw_change)) != NULL)
125174569Sache			strftime(pwexpire, sizeof pwexpire, "%c", tptr);
1252283842Sbapt		printf("Login Name: %-15s   #%-12u Group: %-15s   #%u\n"
125320747Sdavidn		       " Full Name: %s\n"
125420747Sdavidn		       "      Home: %-26.26s      Class: %s\n"
125520747Sdavidn		       "     Shell: %-26.26s     Office: %s\n"
125620747Sdavidn		       "Work Phone: %-26.26s Home Phone: %s\n"
125720747Sdavidn		       "Acc Expire: %-26.26s Pwd Expire: %s\n",
1258283842Sbapt		       pwd->pw_name, pwd->pw_uid,
1259283842Sbapt		       grp ? grp->gr_name : "(invalid)", pwd->pw_gid,
126020253Sjoerg		       uname, pwd->pw_dir, pwd->pw_class,
126120590Sdavidn		       pwd->pw_shell, office, wphone, hphone,
126220590Sdavidn		       acexpire, pwexpire);
126344229Sdavidn	        SETGRENT();
126420267Sjoerg		j = 0;
126544229Sdavidn		while ((grp=GETGRENT()) != NULL)
126620267Sjoerg		{
126720267Sjoerg			int     i = 0;
1268262865Sjulian			if (grp->gr_mem != NULL) {
1269262865Sjulian				while (grp->gr_mem[i] != NULL)
127020267Sjoerg				{
1271262865Sjulian					if (strcmp(grp->gr_mem[i], pwd->pw_name)==0)
1272262865Sjulian					{
1273262865Sjulian						printf(j++ == 0 ? "    Groups: %s" : ",%s", grp->gr_name);
1274262865Sjulian						break;
1275262865Sjulian					}
1276262865Sjulian					++i;
127720267Sjoerg				}
127820267Sjoerg			}
127920267Sjoerg		}
128044229Sdavidn		ENDGRENT();
128161957Sache		printf("%s", j ? "\n" : "");
128220253Sjoerg	}
128320267Sjoerg	return EXIT_SUCCESS;
128420253Sjoerg}
128520253Sjoerg
1286284110Sbaptchar *
1287284110Sbaptpw_checkname(char *name, int gecos)
128820253Sjoerg{
1289109961Sgad	char showch[8];
1290284110Sbapt	const char *badchars, *ch, *showtype;
1291109961Sgad	int reject;
129220253Sjoerg
1293109961Sgad	ch = name;
1294109961Sgad	reject = 0;
1295109961Sgad	if (gecos) {
1296109961Sgad		/* See if the name is valid as a gecos (comment) field. */
1297109961Sgad		badchars = ":!@";
1298109961Sgad		showtype = "gecos field";
1299109961Sgad	} else {
1300109961Sgad		/* See if the name is valid as a userid or group. */
1301109961Sgad		badchars = " ,\t:+&#%$^()!@~*?<>=|\\/\"";
1302109961Sgad		showtype = "userid/group name";
1303109961Sgad		/* Userids and groups can not have a leading '-'. */
1304109961Sgad		if (*ch == '-')
1305109961Sgad			reject = 1;
130620253Sjoerg	}
1307109961Sgad	if (!reject) {
1308109961Sgad		while (*ch) {
1309109961Sgad			if (strchr(badchars, *ch) != NULL || *ch < ' ' ||
1310109961Sgad			    *ch == 127) {
1311109961Sgad				reject = 1;
1312109961Sgad				break;
1313109961Sgad			}
1314109961Sgad			/* 8-bit characters are only allowed in GECOS fields */
1315109961Sgad			if (!gecos && (*ch & 0x80)) {
1316109961Sgad				reject = 1;
1317109961Sgad				break;
1318109961Sgad			}
1319109961Sgad			ch++;
1320109961Sgad		}
1321109961Sgad	}
1322109961Sgad	/*
1323109961Sgad	 * A `$' is allowed as the final character for userids and groups,
1324109961Sgad	 * mainly for the benefit of samba.
1325109961Sgad	 */
1326109961Sgad	if (reject && !gecos) {
1327109961Sgad		if (*ch == '$' && *(ch + 1) == '\0') {
1328109961Sgad			reject = 0;
1329109961Sgad			ch++;
1330109961Sgad		}
1331109961Sgad	}
1332109961Sgad	if (reject) {
1333109961Sgad		snprintf(showch, sizeof(showch), (*ch >= ' ' && *ch < 127)
1334109961Sgad		    ? "`%c'" : "0x%02x", *ch);
1335228673Sdim		errx(EX_DATAERR, "invalid character %s at position %td in %s",
1336109961Sgad		    showch, (ch - name), showtype);
1337109961Sgad	}
1338109961Sgad	if (!gecos && (ch - name) > LOGNAMESIZE)
1339109961Sgad		errx(EX_DATAERR, "name too long `%s' (max is %d)", name,
1340109961Sgad		    LOGNAMESIZE);
1341284110Sbapt
1342284110Sbapt	return (name);
134320253Sjoerg}
134420253Sjoerg
134520253Sjoerg
134620253Sjoergstatic void
134720253Sjoergrmat(uid_t uid)
134820253Sjoerg{
134920253Sjoerg	DIR            *d = opendir("/var/at/jobs");
135020253Sjoerg
135120253Sjoerg	if (d != NULL) {
135220253Sjoerg		struct dirent  *e;
135320253Sjoerg
135420253Sjoerg		while ((e = readdir(d)) != NULL) {
135520253Sjoerg			struct stat     st;
135620253Sjoerg
135720253Sjoerg			if (strncmp(e->d_name, ".lock", 5) != 0 &&
135820253Sjoerg			    stat(e->d_name, &st) == 0 &&
135920253Sjoerg			    !S_ISDIR(st.st_mode) &&
136020253Sjoerg			    st.st_uid == uid) {
136120253Sjoerg				char            tmp[MAXPATHLEN];
136220253Sjoerg
1363282700Sbapt				snprintf(tmp, sizeof(tmp), "/usr/bin/atrm %s", e->d_name);
136420253Sjoerg				system(tmp);
136520253Sjoerg			}
136620253Sjoerg		}
136720253Sjoerg		closedir(d);
136820253Sjoerg	}
136920253Sjoerg}
137020747Sdavidn
137120747Sdavidnstatic void
137285145Sachermopie(char const * name)
137320747Sdavidn{
137485145Sache	static const char etcopie[] = "/etc/opiekeys";
137585145Sache	FILE   *fp = fopen(etcopie, "r+");
137620747Sdavidn
137721052Sdavidn	if (fp != NULL) {
137821052Sdavidn		char	tmp[1024];
137921052Sdavidn		off_t	atofs = 0;
138021052Sdavidn		int	length = strlen(name);
138120747Sdavidn
138221052Sdavidn		while (fgets(tmp, sizeof tmp, fp) != NULL) {
138321052Sdavidn			if (strncmp(name, tmp, length) == 0 && tmp[length]==' ') {
138421052Sdavidn				if (fseek(fp, atofs, SEEK_SET) == 0) {
138521052Sdavidn					fwrite("#", 1, 1, fp);	/* Comment username out */
138621052Sdavidn				}
138721052Sdavidn				break;
138820747Sdavidn			}
138921052Sdavidn			atofs = ftell(fp);
139020747Sdavidn		}
139121052Sdavidn		/*
139221052Sdavidn		 * If we got an error of any sort, don't update!
139321052Sdavidn		 */
139421052Sdavidn		fclose(fp);
139520747Sdavidn	}
139620747Sdavidn}
139720747Sdavidn
1398