pw_user.c revision 285414
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 285414 2015-07-11 23:56:55Z 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;
878285414Sbapt		gid_t		grid = -1;
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		 */
891285414Sbapt		if (GETGRGID(prefer) == NULL)
892285414Sbapt			grid = prefer;
893284121Sbapt		if (conf.dryrun) {
894285395Sbapt			gid = pw_groupnext(cnf, true);
895285395Sbapt		} else {
896285414Sbapt			pw_group(M_ADD, nam, grid, &grpargs);
89744229Sdavidn			if ((grp = GETGRNAM(nam)) != NULL)
89820267Sjoerg				gid = grp->gr_gid;
89920267Sjoerg		}
90020253Sjoerg	}
90144229Sdavidn	ENDGRENT();
90220253Sjoerg	return gid;
90320253Sjoerg}
90420253Sjoerg
90520253Sjoerg
90620253Sjoergstatic          time_t
90720253Sjoergpw_pwdpolicy(struct userconf * cnf, struct cargs * args)
90820253Sjoerg{
90920253Sjoerg	time_t          result = 0;
91020253Sjoerg	time_t          now = time(NULL);
91127831Sdavidn	struct carg    *arg = getarg(args, 'p');
91220253Sjoerg
91320253Sjoerg	if (arg != NULL) {
91420253Sjoerg		if ((result = parse_date(now, arg->val)) == now)
91530259Scharnier			errx(EX_DATAERR, "invalid date/time `%s'", arg->val);
91620253Sjoerg	} else if (cnf->password_days > 0)
91720253Sjoerg		result = now + ((long) cnf->password_days * 86400L);
91820253Sjoerg	return result;
91920253Sjoerg}
92020253Sjoerg
92120253Sjoerg
92220253Sjoergstatic          time_t
92320253Sjoergpw_exppolicy(struct userconf * cnf, struct cargs * args)
92420253Sjoerg{
92520253Sjoerg	time_t          result = 0;
92620253Sjoerg	time_t          now = time(NULL);
92720253Sjoerg	struct carg    *arg = getarg(args, 'e');
92820253Sjoerg
92920253Sjoerg	if (arg != NULL) {
93020253Sjoerg		if ((result = parse_date(now, arg->val)) == now)
93130259Scharnier			errx(EX_DATAERR, "invalid date/time `%s'", arg->val);
93220253Sjoerg	} else if (cnf->expire_days > 0)
93320253Sjoerg		result = now + ((long) cnf->expire_days * 86400L);
93420253Sjoerg	return result;
93520253Sjoerg}
93620253Sjoerg
93720253Sjoerg
93820253Sjoergstatic char    *
93920253Sjoergpw_homepolicy(struct userconf * cnf, struct cargs * args, char const * user)
94020253Sjoerg{
94120253Sjoerg	struct carg    *arg = getarg(args, 'd');
942282699Sbapt	static char     home[128];
94320253Sjoerg
94420253Sjoerg	if (arg)
945282699Sbapt		return (arg->val);
94620253Sjoerg
947282699Sbapt	if (cnf->home == NULL || *cnf->home == '\0')
948282699Sbapt		errx(EX_CONFIG, "no base home directory set");
949282699Sbapt	snprintf(home, sizeof(home), "%s/%s", cnf->home, user);
950282699Sbapt
951282699Sbapt	return (home);
95220253Sjoerg}
95320253Sjoerg
95420253Sjoergstatic char    *
95520253Sjoergshell_path(char const * path, char *shells[], char *sh)
95620253Sjoerg{
95720253Sjoerg	if (sh != NULL && (*sh == '/' || *sh == '\0'))
95820253Sjoerg		return sh;	/* specified full path or forced none */
95920253Sjoerg	else {
96020253Sjoerg		char           *p;
96120253Sjoerg		char            paths[_UC_MAXLINE];
96220253Sjoerg
96320253Sjoerg		/*
96420253Sjoerg		 * We need to search paths
96520253Sjoerg		 */
966130633Srobert		strlcpy(paths, path, sizeof(paths));
96720253Sjoerg		for (p = strtok(paths, ": \t\r\n"); p != NULL; p = strtok(NULL, ": \t\r\n")) {
96820253Sjoerg			int             i;
96920253Sjoerg			static char     shellpath[256];
97020253Sjoerg
97120253Sjoerg			if (sh != NULL) {
972282700Sbapt				snprintf(shellpath, sizeof(shellpath), "%s/%s", p, sh);
97320253Sjoerg				if (access(shellpath, X_OK) == 0)
97420253Sjoerg					return shellpath;
97520253Sjoerg			} else
97620253Sjoerg				for (i = 0; i < _UC_MAXSHELLS && shells[i] != NULL; i++) {
977282700Sbapt					snprintf(shellpath, sizeof(shellpath), "%s/%s", p, shells[i]);
97820253Sjoerg					if (access(shellpath, X_OK) == 0)
97920253Sjoerg						return shellpath;
98020253Sjoerg				}
98120253Sjoerg		}
98220253Sjoerg		if (sh == NULL)
98330259Scharnier			errx(EX_OSFILE, "can't find shell `%s' in shell paths", sh);
98430259Scharnier		errx(EX_CONFIG, "no default shell available or defined");
98520253Sjoerg		return NULL;
98620253Sjoerg	}
98720253Sjoerg}
98820253Sjoerg
98920253Sjoerg
99020253Sjoergstatic char    *
99120253Sjoergpw_shellpolicy(struct userconf * cnf, struct cargs * args, char *newshell)
99220253Sjoerg{
99320253Sjoerg	char           *sh = newshell;
99420253Sjoerg	struct carg    *arg = getarg(args, 's');
99520253Sjoerg
99620253Sjoerg	if (newshell == NULL && arg != NULL)
99720253Sjoerg		sh = arg->val;
99820253Sjoerg	return shell_path(cnf->shelldir, cnf->shells, sh ? sh : cnf->shell_default);
99920253Sjoerg}
100020253Sjoerg
1001179365Santoine#define	SALTSIZE	32
100220253Sjoerg
1003179365Santoinestatic char const chars[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ./";
1004179365Santoine
100520253Sjoergchar           *
100620253Sjoergpw_pwcrypt(char *password)
100720253Sjoerg{
100820253Sjoerg	int             i;
1009179365Santoine	char            salt[SALTSIZE + 1];
1010231994Skevlo	char		*cryptpw;
101120253Sjoerg
101220253Sjoerg	static char     buf[256];
101320253Sjoerg
101420253Sjoerg	/*
101520253Sjoerg	 * Calculate a salt value
101620253Sjoerg	 */
1017179365Santoine	for (i = 0; i < SALTSIZE; i++)
1018181785Sache		salt[i] = chars[arc4random_uniform(sizeof(chars) - 1)];
1019179365Santoine	salt[SALTSIZE] = '\0';
102020253Sjoerg
1021231994Skevlo	cryptpw = crypt(password, salt);
1022231994Skevlo	if (cryptpw == NULL)
1023231994Skevlo		errx(EX_CONFIG, "crypt(3) failure");
1024231994Skevlo	return strcpy(buf, cryptpw);
102520253Sjoerg}
102620253Sjoerg
102720590Sdavidn
102820253Sjoergstatic char    *
1029285409Sbaptpw_password(struct userconf * cnf, char const * user)
103020253Sjoerg{
103120253Sjoerg	int             i, l;
103220253Sjoerg	char            pwbuf[32];
103320253Sjoerg
103420253Sjoerg	switch (cnf->default_password) {
103520253Sjoerg	case -1:		/* Random password */
103673563Skris		l = (arc4random() % 8 + 8);	/* 8 - 16 chars */
103720253Sjoerg		for (i = 0; i < l; i++)
1038181785Sache			pwbuf[i] = chars[arc4random_uniform(sizeof(chars)-1)];
103920253Sjoerg		pwbuf[i] = '\0';
104020253Sjoerg
104120253Sjoerg		/*
104220253Sjoerg		 * We give this information back to the user
104320253Sjoerg		 */
1044285137Sbapt		if (conf.fd == -1 && !conf.dryrun) {
104561957Sache			if (isatty(STDOUT_FILENO))
104620712Sdavidn				printf("Password for '%s' is: ", user);
104720253Sjoerg			printf("%s\n", pwbuf);
104820253Sjoerg			fflush(stdout);
104920253Sjoerg		}
105020253Sjoerg		break;
105120253Sjoerg
105220253Sjoerg	case -2:		/* No password at all! */
105320253Sjoerg		return "";
105420253Sjoerg
105520253Sjoerg	case 0:		/* No login - default */
105620253Sjoerg	default:
105720253Sjoerg		return "*";
105820253Sjoerg
105920253Sjoerg	case 1:		/* user's name */
1060130633Srobert		strlcpy(pwbuf, user, sizeof(pwbuf));
106120253Sjoerg		break;
106220253Sjoerg	}
106320253Sjoerg	return pw_pwcrypt(pwbuf);
106420253Sjoerg}
106520253Sjoerg
1066284111Sbaptstatic int
1067285401Sbaptpw_userdel(char *name, long id)
1068284111Sbapt{
1069285401Sbapt	struct passwd *pwd = NULL;
1070284111Sbapt	char		 file[MAXPATHLEN];
1071284111Sbapt	char		 home[MAXPATHLEN];
1072285401Sbapt	uid_t		 uid;
1073284111Sbapt	struct group	*gr, *grp;
1074284111Sbapt	char		 grname[LOGNAMESIZE];
1075284111Sbapt	int		 rc;
1076284111Sbapt	struct stat	 st;
107720253Sjoerg
1078285401Sbapt	if (id < 0 && name == NULL)
1079285401Sbapt		errx(EX_DATAERR, "username or id required");
1080285401Sbapt
1081285401Sbapt	pwd = (name != NULL) ? GETPWNAM(pw_checkname(name, 0)) : GETPWUID(id);
1082285401Sbapt	if (pwd == NULL) {
1083285401Sbapt		if (name == NULL)
1084285401Sbapt			errx(EX_NOUSER, "no such uid `%ld'", id);
1085285401Sbapt		errx(EX_NOUSER, "no such user `%s'", name);
1086285401Sbapt	}
1087285401Sbapt	uid = pwd->pw_uid;
1088285401Sbapt	if (name == NULL)
1089285401Sbapt		name = pwd->pw_name;
1090285401Sbapt
1091284111Sbapt	if (strcmp(pwd->pw_name, "root") == 0)
1092284111Sbapt		errx(EX_DATAERR, "cannot remove user 'root'");
1093284111Sbapt
1094284111Sbapt	if (!PWALTDIR()) {
1095284111Sbapt		/*
1096284111Sbapt		 * Remove opie record from /etc/opiekeys
1097284111Sbapt		*/
1098284111Sbapt
1099284111Sbapt		rmopie(pwd->pw_name);
1100284111Sbapt
1101284111Sbapt		/*
1102284111Sbapt		 * Remove crontabs
1103284111Sbapt		 */
1104284111Sbapt		snprintf(file, sizeof(file), "/var/cron/tabs/%s", pwd->pw_name);
1105284111Sbapt		if (access(file, F_OK) == 0) {
1106284111Sbapt			snprintf(file, sizeof(file), "crontab -u %s -r", pwd->pw_name);
1107284111Sbapt			system(file);
1108284111Sbapt		}
1109284111Sbapt	}
1110284111Sbapt	/*
1111284111Sbapt	 * Save these for later, since contents of pwd may be
1112284111Sbapt	 * invalidated by deletion
1113284111Sbapt	 */
1114284111Sbapt	snprintf(file, sizeof(file), "%s/%s", _PATH_MAILDIR, pwd->pw_name);
1115284111Sbapt	strlcpy(home, pwd->pw_dir, sizeof(home));
1116284111Sbapt	gr = GETGRGID(pwd->pw_gid);
1117284111Sbapt	if (gr != NULL)
1118284111Sbapt		strlcpy(grname, gr->gr_name, LOGNAMESIZE);
1119284111Sbapt	else
1120284111Sbapt		grname[0] = '\0';
1121284111Sbapt
1122284111Sbapt	rc = delpwent(pwd);
1123284111Sbapt	if (rc == -1)
1124284111Sbapt		err(EX_IOERR, "user '%s' does not exist", pwd->pw_name);
1125284111Sbapt	else if (rc != 0)
1126284111Sbapt		err(EX_IOERR, "passwd update");
1127284111Sbapt
1128285401Sbapt	if (conf.userconf->nispasswd && *conf.userconf->nispasswd=='/') {
1129285401Sbapt		rc = delnispwent(conf.userconf->nispasswd, name);
1130284111Sbapt		if (rc == -1)
1131285401Sbapt			warnx("WARNING: user '%s' does not exist in NIS passwd",
1132285401Sbapt			    pwd->pw_name);
1133284111Sbapt		else if (rc != 0)
1134284111Sbapt			warn("WARNING: NIS passwd update");
1135284111Sbapt		/* non-fatal */
1136284111Sbapt	}
1137284111Sbapt
1138284128Sbapt	grp = GETGRNAM(name);
1139284111Sbapt	if (grp != NULL &&
1140284111Sbapt	    (grp->gr_mem == NULL || *grp->gr_mem == NULL) &&
1141284128Sbapt	    strcmp(name, grname) == 0)
1142284128Sbapt		delgrent(GETGRNAM(name));
1143284111Sbapt	SETGRENT();
1144284111Sbapt	while ((grp = GETGRENT()) != NULL) {
1145284111Sbapt		int i, j;
1146284111Sbapt		char group[MAXLOGNAME];
1147284113Sbapt		if (grp->gr_mem == NULL)
1148284113Sbapt			continue;
1149284113Sbapt
1150284113Sbapt		for (i = 0; grp->gr_mem[i] != NULL; i++) {
1151284128Sbapt			if (strcmp(grp->gr_mem[i], name) != 0)
1152284113Sbapt				continue;
1153284113Sbapt
1154284113Sbapt			for (j = i; grp->gr_mem[j] != NULL; j++)
1155284113Sbapt				grp->gr_mem[j] = grp->gr_mem[j+1];
1156284113Sbapt			strlcpy(group, grp->gr_name, MAXLOGNAME);
1157284113Sbapt			chggrent(group, grp);
1158284111Sbapt		}
1159284111Sbapt	}
1160284111Sbapt	ENDGRENT();
1161284111Sbapt
1162285401Sbapt	pw_log(conf.userconf, M_DELETE, W_USER, "%s(%u) account removed", name,
1163285401Sbapt	    uid);
1164284111Sbapt
1165284117Sbapt	if (!PWALTDIR()) {
1166284111Sbapt		/*
1167284111Sbapt		 * Remove mail file
1168284111Sbapt		 */
1169284111Sbapt		remove(file);
1170284111Sbapt
1171284111Sbapt		/*
1172284111Sbapt		 * Remove at jobs
1173284111Sbapt		 */
1174284111Sbapt		if (getpwuid(uid) == NULL)
1175284111Sbapt			rmat(uid);
1176284111Sbapt
1177284111Sbapt		/*
1178284111Sbapt		 * Remove home directory and contents
1179284111Sbapt		 */
1180285401Sbapt		if (conf.deletehome && *home == '/' && getpwuid(uid) == NULL &&
1181284112Sbapt		    stat(home, &st) != -1) {
1182284112Sbapt			rm_r(home, uid);
1183285401Sbapt			pw_log(conf.userconf, M_DELETE, W_USER, "%s(%u) home '%s' %sremoved",
1184284128Sbapt			       name, uid, home,
1185284112Sbapt			       stat(home, &st) == -1 ? "" : "not completely ");
1186284111Sbapt		}
1187284111Sbapt	}
1188284111Sbapt
1189284111Sbapt	return (EXIT_SUCCESS);
1190284111Sbapt}
1191284111Sbapt
119220253Sjoergstatic int
1193284124Sbaptprint_user(struct passwd * pwd)
119420253Sjoerg{
1195284122Sbapt	if (!conf.pretty) {
1196242349Sbapt		char            *buf;
119720253Sjoerg
1198284124Sbapt		buf = conf.v7 ? pw_make_v7(pwd) : pw_make(pwd);
1199242349Sbapt		printf("%s\n", buf);
1200242349Sbapt		free(buf);
120120253Sjoerg	} else {
120220267Sjoerg		int		j;
120320253Sjoerg		char           *p;
120444229Sdavidn		struct group   *grp = GETGRGID(pwd->pw_gid);
120520253Sjoerg		char            uname[60] = "User &", office[60] = "[None]",
120620253Sjoerg		                wphone[60] = "[None]", hphone[60] = "[None]";
120720590Sdavidn		char		acexpire[32] = "[None]", pwexpire[32] = "[None]";
120820590Sdavidn		struct tm *    tptr;
120920253Sjoerg
121020253Sjoerg		if ((p = strtok(pwd->pw_gecos, ",")) != NULL) {
1211130633Srobert			strlcpy(uname, p, sizeof(uname));
121220253Sjoerg			if ((p = strtok(NULL, ",")) != NULL) {
1213130633Srobert				strlcpy(office, p, sizeof(office));
121420253Sjoerg				if ((p = strtok(NULL, ",")) != NULL) {
1215130633Srobert					strlcpy(wphone, p, sizeof(wphone));
121620253Sjoerg					if ((p = strtok(NULL, "")) != NULL) {
1217130633Srobert						strlcpy(hphone, p,
1218130633Srobert						    sizeof(hphone));
121920253Sjoerg					}
122020253Sjoerg				}
122120253Sjoerg			}
122220253Sjoerg		}
122320253Sjoerg		/*
122420253Sjoerg		 * Handle '&' in gecos field
122520253Sjoerg		 */
122620253Sjoerg		if ((p = strchr(uname, '&')) != NULL) {
122720253Sjoerg			int             l = strlen(pwd->pw_name);
122820253Sjoerg			int             m = strlen(p);
122920253Sjoerg
123020253Sjoerg			memmove(p + l, p + 1, m);
123120253Sjoerg			memmove(p, pwd->pw_name, l);
123261957Sache			*p = (char) toupper((unsigned char)*p);
123320253Sjoerg		}
123420590Sdavidn		if (pwd->pw_expire > (time_t)0 && (tptr = localtime(&pwd->pw_expire)) != NULL)
123574569Sache			strftime(acexpire, sizeof acexpire, "%c", tptr);
123661957Sache		if (pwd->pw_change > (time_t)0 && (tptr = localtime(&pwd->pw_change)) != NULL)
123774569Sache			strftime(pwexpire, sizeof pwexpire, "%c", tptr);
1238283842Sbapt		printf("Login Name: %-15s   #%-12u Group: %-15s   #%u\n"
123920747Sdavidn		       " Full Name: %s\n"
124020747Sdavidn		       "      Home: %-26.26s      Class: %s\n"
124120747Sdavidn		       "     Shell: %-26.26s     Office: %s\n"
124220747Sdavidn		       "Work Phone: %-26.26s Home Phone: %s\n"
124320747Sdavidn		       "Acc Expire: %-26.26s Pwd Expire: %s\n",
1244283842Sbapt		       pwd->pw_name, pwd->pw_uid,
1245283842Sbapt		       grp ? grp->gr_name : "(invalid)", pwd->pw_gid,
124620253Sjoerg		       uname, pwd->pw_dir, pwd->pw_class,
124720590Sdavidn		       pwd->pw_shell, office, wphone, hphone,
124820590Sdavidn		       acexpire, pwexpire);
124944229Sdavidn	        SETGRENT();
125020267Sjoerg		j = 0;
125144229Sdavidn		while ((grp=GETGRENT()) != NULL)
125220267Sjoerg		{
125320267Sjoerg			int     i = 0;
1254262865Sjulian			if (grp->gr_mem != NULL) {
1255262865Sjulian				while (grp->gr_mem[i] != NULL)
125620267Sjoerg				{
1257262865Sjulian					if (strcmp(grp->gr_mem[i], pwd->pw_name)==0)
1258262865Sjulian					{
1259262865Sjulian						printf(j++ == 0 ? "    Groups: %s" : ",%s", grp->gr_name);
1260262865Sjulian						break;
1261262865Sjulian					}
1262262865Sjulian					++i;
126320267Sjoerg				}
126420267Sjoerg			}
126520267Sjoerg		}
126644229Sdavidn		ENDGRENT();
126761957Sache		printf("%s", j ? "\n" : "");
126820253Sjoerg	}
126920267Sjoerg	return EXIT_SUCCESS;
127020253Sjoerg}
127120253Sjoerg
1272284110Sbaptchar *
1273284110Sbaptpw_checkname(char *name, int gecos)
127420253Sjoerg{
1275109961Sgad	char showch[8];
1276284110Sbapt	const char *badchars, *ch, *showtype;
1277109961Sgad	int reject;
127820253Sjoerg
1279109961Sgad	ch = name;
1280109961Sgad	reject = 0;
1281109961Sgad	if (gecos) {
1282109961Sgad		/* See if the name is valid as a gecos (comment) field. */
1283109961Sgad		badchars = ":!@";
1284109961Sgad		showtype = "gecos field";
1285109961Sgad	} else {
1286109961Sgad		/* See if the name is valid as a userid or group. */
1287109961Sgad		badchars = " ,\t:+&#%$^()!@~*?<>=|\\/\"";
1288109961Sgad		showtype = "userid/group name";
1289109961Sgad		/* Userids and groups can not have a leading '-'. */
1290109961Sgad		if (*ch == '-')
1291109961Sgad			reject = 1;
129220253Sjoerg	}
1293109961Sgad	if (!reject) {
1294109961Sgad		while (*ch) {
1295109961Sgad			if (strchr(badchars, *ch) != NULL || *ch < ' ' ||
1296109961Sgad			    *ch == 127) {
1297109961Sgad				reject = 1;
1298109961Sgad				break;
1299109961Sgad			}
1300109961Sgad			/* 8-bit characters are only allowed in GECOS fields */
1301109961Sgad			if (!gecos && (*ch & 0x80)) {
1302109961Sgad				reject = 1;
1303109961Sgad				break;
1304109961Sgad			}
1305109961Sgad			ch++;
1306109961Sgad		}
1307109961Sgad	}
1308109961Sgad	/*
1309109961Sgad	 * A `$' is allowed as the final character for userids and groups,
1310109961Sgad	 * mainly for the benefit of samba.
1311109961Sgad	 */
1312109961Sgad	if (reject && !gecos) {
1313109961Sgad		if (*ch == '$' && *(ch + 1) == '\0') {
1314109961Sgad			reject = 0;
1315109961Sgad			ch++;
1316109961Sgad		}
1317109961Sgad	}
1318109961Sgad	if (reject) {
1319109961Sgad		snprintf(showch, sizeof(showch), (*ch >= ' ' && *ch < 127)
1320109961Sgad		    ? "`%c'" : "0x%02x", *ch);
1321228673Sdim		errx(EX_DATAERR, "invalid character %s at position %td in %s",
1322109961Sgad		    showch, (ch - name), showtype);
1323109961Sgad	}
1324109961Sgad	if (!gecos && (ch - name) > LOGNAMESIZE)
1325109961Sgad		errx(EX_DATAERR, "name too long `%s' (max is %d)", name,
1326109961Sgad		    LOGNAMESIZE);
1327284110Sbapt
1328284110Sbapt	return (name);
132920253Sjoerg}
133020253Sjoerg
133120253Sjoerg
133220253Sjoergstatic void
133320253Sjoergrmat(uid_t uid)
133420253Sjoerg{
133520253Sjoerg	DIR            *d = opendir("/var/at/jobs");
133620253Sjoerg
133720253Sjoerg	if (d != NULL) {
133820253Sjoerg		struct dirent  *e;
133920253Sjoerg
134020253Sjoerg		while ((e = readdir(d)) != NULL) {
134120253Sjoerg			struct stat     st;
134220253Sjoerg
134320253Sjoerg			if (strncmp(e->d_name, ".lock", 5) != 0 &&
134420253Sjoerg			    stat(e->d_name, &st) == 0 &&
134520253Sjoerg			    !S_ISDIR(st.st_mode) &&
134620253Sjoerg			    st.st_uid == uid) {
134720253Sjoerg				char            tmp[MAXPATHLEN];
134820253Sjoerg
1349282700Sbapt				snprintf(tmp, sizeof(tmp), "/usr/bin/atrm %s", e->d_name);
135020253Sjoerg				system(tmp);
135120253Sjoerg			}
135220253Sjoerg		}
135320253Sjoerg		closedir(d);
135420253Sjoerg	}
135520253Sjoerg}
135620747Sdavidn
135720747Sdavidnstatic void
135885145Sachermopie(char const * name)
135920747Sdavidn{
136085145Sache	static const char etcopie[] = "/etc/opiekeys";
136185145Sache	FILE   *fp = fopen(etcopie, "r+");
136220747Sdavidn
136321052Sdavidn	if (fp != NULL) {
136421052Sdavidn		char	tmp[1024];
136521052Sdavidn		off_t	atofs = 0;
136621052Sdavidn		int	length = strlen(name);
136720747Sdavidn
136821052Sdavidn		while (fgets(tmp, sizeof tmp, fp) != NULL) {
136921052Sdavidn			if (strncmp(name, tmp, length) == 0 && tmp[length]==' ') {
137021052Sdavidn				if (fseek(fp, atofs, SEEK_SET) == 0) {
137121052Sdavidn					fwrite("#", 1, 1, fp);	/* Comment username out */
137221052Sdavidn				}
137321052Sdavidn				break;
137420747Sdavidn			}
137521052Sdavidn			atofs = ftell(fp);
137620747Sdavidn		}
137721052Sdavidn		/*
137821052Sdavidn		 * If we got an error of any sort, don't update!
137921052Sdavidn		 */
138021052Sdavidn		fclose(fp);
138120747Sdavidn	}
138220747Sdavidn}
138320747Sdavidn
1384