pw_user.c revision 301367
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 301367 2016-06-04 16:31:33Z bapt $";
3130259Scharnier#endif /* not lint */
3230259Scharnier
33286201Sbapt#include <sys/param.h>
34286201Sbapt#include <sys/types.h>
35286201Sbapt
3630259Scharnier#include <ctype.h>
37286201Sbapt#include <dirent.h>
3830259Scharnier#include <err.h>
39286982Sbapt#include <errno.h>
4020253Sjoerg#include <fcntl.h>
41286201Sbapt#include <grp.h>
42286201Sbapt#include <pwd.h>
43286201Sbapt#include <libutil.h>
44286201Sbapt#include <login_cap.h>
4530259Scharnier#include <paths.h>
46286201Sbapt#include <string.h>
47286201Sbapt#include <sysexits.h>
4820253Sjoerg#include <termios.h>
49286201Sbapt#include <unistd.h>
50286201Sbapt
5120253Sjoerg#include "pw.h"
5220253Sjoerg#include "bitmap.h"
53286201Sbapt#include "psdate.h"
5420253Sjoerg
5523318Sache#define LOGNAMESIZE (MAXLOGNAME-1)
5622394Sdavidn
5752512Sdavidnstatic		char locked_str[] = "*LOCKED*";
5824214Sache
59286196Sbaptstatic struct passwd fakeuser = {
60286196Sbapt	"nouser",
61286196Sbapt	"*",
62286196Sbapt	-1,
63286196Sbapt	-1,
64286196Sbapt	0,
65286196Sbapt	"",
66286196Sbapt	"User &",
67286196Sbapt	"/nonexistent",
68286196Sbapt	"/bin/sh",
69286196Sbapt	0,
70286196Sbapt	0
71286196Sbapt};
7220253Sjoerg
73286196Sbaptstatic int	 print_user(struct passwd *pwd, bool pretty, bool v7);
74286196Sbaptstatic uid_t	 pw_uidpolicy(struct userconf *cnf, intmax_t id);
75286196Sbaptstatic uid_t	 pw_gidpolicy(struct userconf *cnf, char *grname, char *nam,
76286196Sbapt    gid_t prefer, bool dryrun);
77286196Sbaptstatic char	*pw_homepolicy(struct userconf * cnf, char *homedir,
78286196Sbapt    const char *user);
79286196Sbaptstatic char	*pw_shellpolicy(struct userconf * cnf);
80286196Sbaptstatic char	*pw_password(struct userconf * cnf, char const * user,
81286196Sbapt    bool dryrun);
82286196Sbaptstatic char	*shell_path(char const * path, char *shells[], char *sh);
83286196Sbaptstatic void	rmat(uid_t uid);
84286196Sbaptstatic void	rmopie(char const * name);
85286196Sbapt
86283961Sbaptstatic void
87286982Sbaptmkdir_home_parents(int dfd, const char *dir)
88286982Sbapt{
89286982Sbapt	struct stat st;
90286982Sbapt	char *dirs, *tmp;
91286982Sbapt
92286982Sbapt	if (*dir != '/')
93286982Sbapt		errx(EX_DATAERR, "invalid base directory for home '%s'", dir);
94286982Sbapt
95286982Sbapt	dir++;
96286982Sbapt
97286982Sbapt	if (fstatat(dfd, dir, &st, 0) != -1) {
98286982Sbapt		if (S_ISDIR(st.st_mode))
99286982Sbapt			return;
100286982Sbapt		errx(EX_OSFILE, "root home `/%s' is not a directory", dir);
101286982Sbapt	}
102286982Sbapt
103286982Sbapt	dirs = strdup(dir);
104286982Sbapt	if (dirs == NULL)
105286982Sbapt		errx(EX_UNAVAILABLE, "out of memory");
106286982Sbapt
107286982Sbapt	tmp = strrchr(dirs, '/');
108290153Sbdrewery	if (tmp == NULL) {
109290153Sbdrewery		free(dirs);
110286982Sbapt		return;
111290153Sbdrewery	}
112286982Sbapt	tmp[0] = '\0';
113286982Sbapt
114286982Sbapt	/*
115286982Sbapt	 * This is a kludge especially for Joerg :)
116286982Sbapt	 * If the home directory would be created in the root partition, then
117286982Sbapt	 * we really create it under /usr which is likely to have more space.
118286982Sbapt	 * But we create a symlink from cnf->home -> "/usr" -> cnf->home
119286982Sbapt	 */
120286982Sbapt	if (strchr(dirs, '/') == NULL) {
121286982Sbapt		asprintf(&tmp, "usr/%s", dirs);
122286982Sbapt		if (tmp == NULL)
123286982Sbapt			errx(EX_UNAVAILABLE, "out of memory");
124286982Sbapt		if (mkdirat(dfd, tmp, _DEF_DIRMODE) != -1 || errno == EEXIST) {
125286982Sbapt			fchownat(dfd, tmp, 0, 0, 0);
126286986Sbapt			symlinkat(tmp, dfd, dirs);
127286982Sbapt		}
128286982Sbapt		free(tmp);
129286982Sbapt	}
130286982Sbapt	tmp = dirs;
131286982Sbapt	if (fstatat(dfd, dirs, &st, 0) == -1) {
132286982Sbapt		while ((tmp = strchr(tmp + 1, '/')) != NULL) {
133286982Sbapt			*tmp = '\0';
134286982Sbapt			if (fstatat(dfd, dirs, &st, 0) == -1) {
135286982Sbapt				if (mkdirat(dfd, dirs, _DEF_DIRMODE) == -1)
136286982Sbapt					err(EX_OSFILE,  "'%s' (root home parent) is not a directory", dirs);
137286982Sbapt			}
138286982Sbapt			*tmp = '/';
139286982Sbapt		}
140286982Sbapt	}
141286982Sbapt	if (fstatat(dfd, dirs, &st, 0) == -1) {
142286982Sbapt		if (mkdirat(dfd, dirs, _DEF_DIRMODE) == -1)
143286982Sbapt			err(EX_OSFILE,  "'%s' (root home parent) is not a directory", dirs);
144286982Sbapt		fchownat(dfd, dirs, 0, 0, 0);
145286982Sbapt	}
146286982Sbapt
147286982Sbapt	free(dirs);
148286982Sbapt}
149286982Sbapt
150286982Sbaptstatic void
151286196Sbaptcreate_and_populate_homedir(struct userconf *cnf, struct passwd *pwd,
152286196Sbapt    const char *skeldir, mode_t homemode, bool update)
153283961Sbapt{
154285430Sbapt	int skelfd = -1;
155283961Sbapt
156286982Sbapt	/* Create home parents directories */
157286982Sbapt	mkdir_home_parents(conf.rootfd, pwd->pw_dir);
158286982Sbapt
159285430Sbapt	if (skeldir != NULL && *skeldir != '\0') {
160285434Sbapt		if (*skeldir == '/')
161285434Sbapt			skeldir++;
162285434Sbapt		skelfd = openat(conf.rootfd, skeldir, O_DIRECTORY|O_CLOEXEC);
163283961Sbapt	}
164283961Sbapt
165286196Sbapt	copymkdir(conf.rootfd, pwd->pw_dir, skelfd, homemode, pwd->pw_uid,
166285430Sbapt	    pwd->pw_gid, 0);
167286196Sbapt	pw_log(cnf, update ? M_UPDATE : M_ADD, W_USER, "%s(%ju) home %s made",
168286196Sbapt	    pwd->pw_name, (uintmax_t)pwd->pw_uid, pwd->pw_dir);
169283961Sbapt}
170283961Sbapt
171285133Sbaptstatic int
172286196Sbaptpw_set_passwd(struct passwd *pwd, int fd, bool precrypted, bool update)
173285133Sbapt{
174285133Sbapt	int		 b, istty;
175285133Sbapt	struct termios	 t, n;
176285133Sbapt	login_cap_t	*lc;
177285133Sbapt	char		line[_PASSWORD_LEN+1];
178285133Sbapt	char		*p;
179285133Sbapt
180286196Sbapt	if (fd == '-') {
181285133Sbapt		if (!pwd->pw_passwd || *pwd->pw_passwd != '*') {
182285133Sbapt			pwd->pw_passwd = "*";	/* No access */
183285133Sbapt			return (1);
184285133Sbapt		}
185285133Sbapt		return (0);
186285133Sbapt	}
187285133Sbapt
188286196Sbapt	if ((istty = isatty(fd))) {
189286196Sbapt		if (tcgetattr(fd, &t) == -1)
190285133Sbapt			istty = 0;
191285133Sbapt		else {
192285137Sbapt			n = t;
193285133Sbapt			n.c_lflag &= ~(ECHO);
194286196Sbapt			tcsetattr(fd, TCSANOW, &n);
195285133Sbapt			printf("%s%spassword for user %s:",
196285133Sbapt			    update ? "new " : "",
197286196Sbapt			    precrypted ? "encrypted " : "",
198285133Sbapt			    pwd->pw_name);
199285133Sbapt			fflush(stdout);
200285133Sbapt		}
201285133Sbapt	}
202286196Sbapt	b = read(fd, line, sizeof(line) - 1);
203285133Sbapt	if (istty) {	/* Restore state */
204286196Sbapt		tcsetattr(fd, TCSANOW, &t);
205285133Sbapt		fputc('\n', stdout);
206285133Sbapt		fflush(stdout);
207285133Sbapt	}
208285133Sbapt
209285133Sbapt	if (b < 0)
210285133Sbapt		err(EX_IOERR, "-%c file descriptor",
211286196Sbapt		    precrypted ? 'H' : 'h');
212285133Sbapt	line[b] = '\0';
213285133Sbapt	if ((p = strpbrk(line, "\r\n")) != NULL)
214285133Sbapt		*p = '\0';
215285133Sbapt	if (!*line)
216285133Sbapt		errx(EX_DATAERR, "empty password read on file descriptor %d",
217286196Sbapt		    fd);
218286196Sbapt	if (precrypted) {
219285133Sbapt		if (strchr(line, ':') != NULL)
220285137Sbapt			errx(EX_DATAERR, "bad encrypted password");
221286196Sbapt		pwd->pw_passwd = strdup(line);
222285133Sbapt	} else {
223285133Sbapt		lc = login_getpwclass(pwd);
224285133Sbapt		if (lc == NULL ||
225285133Sbapt				login_setcryptfmt(lc, "sha512", NULL) == NULL)
226285133Sbapt			warn("setting crypt(3) format");
227285133Sbapt		login_close(lc);
228285133Sbapt		pwd->pw_passwd = pw_pwcrypt(line);
229285133Sbapt	}
230285133Sbapt	return (1);
231285133Sbapt}
232285133Sbapt
233285405Sbaptstatic void
234286196Sbaptperform_chgpwent(const char *name, struct passwd *pwd, char *nispasswd)
235285405Sbapt{
236285405Sbapt	int rc;
237286196Sbapt	struct passwd *nispwd;
238285405Sbapt
239286196Sbapt	/* duplicate for nis so that chgpwent is not modifying before NIS */
240286196Sbapt	if (nispasswd && *nispasswd == '/')
241286196Sbapt		nispwd = pw_dup(pwd);
242286196Sbapt
243285405Sbapt	rc = chgpwent(name, pwd);
244285405Sbapt	if (rc == -1)
245285405Sbapt		errx(EX_IOERR, "user '%s' does not exist (NIS?)", pwd->pw_name);
246285405Sbapt	else if (rc != 0)
247285405Sbapt		err(EX_IOERR, "passwd file update");
248285405Sbapt
249286196Sbapt	if (nispasswd && *nispasswd == '/') {
250286196Sbapt		rc = chgnispwent(nispasswd, name, nispwd);
251285405Sbapt		if (rc == -1)
252285405Sbapt			warn("User '%s' not found in NIS passwd", pwd->pw_name);
253285984Sbapt		else if (rc != 0)
254285405Sbapt			warn("NIS passwd update");
255285405Sbapt		/* NOTE: NIS-only update errors are not fatal */
256285405Sbapt	}
257285405Sbapt}
258285405Sbapt
259285405Sbapt/*
260285405Sbapt * The M_LOCK and M_UNLOCK functions simply add or remove
261285405Sbapt * a "*LOCKED*" prefix from in front of the password to
262285405Sbapt * prevent it decoding correctly, and therefore prevents
263285405Sbapt * access. Of course, this only prevents access via
264285405Sbapt * password authentication (not ssh, kerberos or any
265285405Sbapt * other method that does not use the UNIX password) but
266285405Sbapt * that is a known limitation.
267285405Sbapt */
268285405Sbaptstatic int
269286196Sbaptpw_userlock(char *arg1, int mode)
270285405Sbapt{
271285405Sbapt	struct passwd *pwd = NULL;
272285405Sbapt	char *passtmp = NULL;
273286196Sbapt	char *name;
274285405Sbapt	bool locked = false;
275291657Sbapt	uid_t id = (uid_t)-1;
276285405Sbapt
277286196Sbapt	if (geteuid() != 0)
278286196Sbapt		errx(EX_NOPERM, "you must be root");
279286196Sbapt
280286196Sbapt	if (arg1 == NULL)
281285405Sbapt		errx(EX_DATAERR, "username or id required");
282285405Sbapt
283291657Sbapt	name = arg1;
284291657Sbapt	if (arg1[strspn(name, "0123456789")] == '\0')
285291657Sbapt		id = pw_checkid(name, UID_MAX);
286286196Sbapt
287291657Sbapt	pwd = GETPWNAM(pw_checkname(name, 0));
288291657Sbapt	if (pwd == NULL && id != (uid_t)-1) {
289291657Sbapt		pwd = GETPWUID(id);
290291657Sbapt		if (pwd != NULL)
291291657Sbapt			name = pwd->pw_name;
292291657Sbapt	}
293285405Sbapt	if (pwd == NULL) {
294291657Sbapt		if (id == (uid_t)-1)
295291657Sbapt			errx(EX_NOUSER, "no such name or uid `%ju'", (uintmax_t) id);
296285405Sbapt		errx(EX_NOUSER, "no such user `%s'", name);
297285405Sbapt	}
298285405Sbapt
299285405Sbapt	if (name == NULL)
300285405Sbapt		name = pwd->pw_name;
301285405Sbapt
302285405Sbapt	if (strncmp(pwd->pw_passwd, locked_str, sizeof(locked_str) -1) == 0)
303285405Sbapt		locked = true;
304285405Sbapt	if (mode == M_LOCK && locked)
305285405Sbapt		errx(EX_DATAERR, "user '%s' is already locked", pwd->pw_name);
306285405Sbapt	if (mode == M_UNLOCK && !locked)
307285405Sbapt		errx(EX_DATAERR, "user '%s' is not locked", pwd->pw_name);
308285405Sbapt
309285405Sbapt	if (mode == M_LOCK) {
310285405Sbapt		asprintf(&passtmp, "%s%s", locked_str, pwd->pw_passwd);
311285405Sbapt		if (passtmp == NULL)	/* disaster */
312285405Sbapt			errx(EX_UNAVAILABLE, "out of memory");
313285405Sbapt		pwd->pw_passwd = passtmp;
314285405Sbapt	} else {
315285405Sbapt		pwd->pw_passwd += sizeof(locked_str)-1;
316285405Sbapt	}
317285405Sbapt
318286196Sbapt	perform_chgpwent(name, pwd, NULL);
319285405Sbapt	free(passtmp);
320285405Sbapt
321285405Sbapt	return (EXIT_SUCCESS);
322285405Sbapt}
323285405Sbapt
324286196Sbaptstatic uid_t
325286196Sbaptpw_uidpolicy(struct userconf * cnf, intmax_t id)
32620253Sjoerg{
327286196Sbapt	struct passwd  *pwd;
328286196Sbapt	struct bitmap   bm;
329286196Sbapt	uid_t           uid = (uid_t) - 1;
33020253Sjoerg
33120267Sjoerg	/*
332286196Sbapt	 * Check the given uid, if any
33320253Sjoerg	 */
334286196Sbapt	if (id >= 0) {
335286196Sbapt		uid = (uid_t) id;
33620253Sjoerg
337286196Sbapt		if ((pwd = GETPWUID(uid)) != NULL && conf.checkduplicate)
338286196Sbapt			errx(EX_DATAERR, "uid `%ju' has already been allocated",
339286196Sbapt			    (uintmax_t)pwd->pw_uid);
340286196Sbapt		return (uid);
34120253Sjoerg	}
34221052Sdavidn	/*
343286196Sbapt	 * We need to allocate the next available uid under one of
344286196Sbapt	 * two policies a) Grab the first unused uid b) Grab the
345286196Sbapt	 * highest possible unused uid
34621052Sdavidn	 */
347286196Sbapt	if (cnf->min_uid >= cnf->max_uid) {	/* Sanity
348286196Sbapt						 * claus^H^H^H^Hheck */
349286196Sbapt		cnf->min_uid = 1000;
350286196Sbapt		cnf->max_uid = 32000;
35121052Sdavidn	}
352286196Sbapt	bm = bm_alloc(cnf->max_uid - cnf->min_uid + 1);
35321052Sdavidn
35420253Sjoerg	/*
355286196Sbapt	 * Now, let's fill the bitmap from the password file
35620253Sjoerg	 */
357286196Sbapt	SETPWENT();
358286196Sbapt	while ((pwd = GETPWENT()) != NULL)
359286196Sbapt		if (pwd->pw_uid >= (uid_t) cnf->min_uid && pwd->pw_uid <= (uid_t) cnf->max_uid)
360286196Sbapt			bm_setbit(&bm, pwd->pw_uid - cnf->min_uid);
361286196Sbapt	ENDPWENT();
36252527Sdavidn
36320253Sjoerg	/*
364286196Sbapt	 * Then apply the policy, with fallback to reuse if necessary
36520253Sjoerg	 */
366286196Sbapt	if (cnf->reuse_uids || (uid = (uid_t) (bm_lastset(&bm) + cnf->min_uid + 1)) > cnf->max_uid)
367286196Sbapt		uid = (uid_t) (bm_firstunset(&bm) + cnf->min_uid);
36820253Sjoerg
36920267Sjoerg	/*
370286196Sbapt	 * Another sanity check
37120267Sjoerg	 */
372286196Sbapt	if (uid < cnf->min_uid || uid > cnf->max_uid)
373286196Sbapt		errx(EX_SOFTWARE, "unable to allocate a new uid - range fully used");
374286196Sbapt	bm_dealloc(&bm);
375286196Sbapt	return (uid);
37620253Sjoerg}
37720253Sjoerg
378286196Sbaptstatic uid_t
379286196Sbaptpw_gidpolicy(struct userconf *cnf, char *grname, char *nam, gid_t prefer, bool dryrun)
38020253Sjoerg{
38120253Sjoerg	struct group   *grp;
38220253Sjoerg	gid_t           gid = (uid_t) - 1;
38320253Sjoerg
38420253Sjoerg	/*
38520253Sjoerg	 * Check the given gid, if any
38620253Sjoerg	 */
38744229Sdavidn	SETGRENT();
388286196Sbapt	if (grname) {
389286196Sbapt		if ((grp = GETGRNAM(grname)) == NULL) {
390286196Sbapt			gid = pw_checkid(grname, GID_MAX);
391286196Sbapt			grp = GETGRGID(gid);
39220253Sjoerg		}
39320253Sjoerg		gid = grp->gr_gid;
394262865Sjulian	} else if ((grp = GETGRNAM(nam)) != NULL &&
395262865Sjulian	    (grp->gr_mem == NULL || grp->gr_mem[0] == NULL)) {
39620267Sjoerg		gid = grp->gr_gid;  /* Already created? Use it anyway... */
39720253Sjoerg	} else {
398286196Sbapt		intmax_t		grid = -1;
39920253Sjoerg
40020253Sjoerg		/*
40120253Sjoerg		 * We need to auto-create a group with the user's name. We
40220253Sjoerg		 * can send all the appropriate output to our sister routine
40320253Sjoerg		 * bit first see if we can create a group with gid==uid so we
40420253Sjoerg		 * can keep the user and group ids in sync. We purposely do
40520253Sjoerg		 * NOT check the gid range if we can force the sync. If the
40620253Sjoerg		 * user's name dups an existing group, then the group add
40720253Sjoerg		 * function will happily handle that case for us and exit.
40820253Sjoerg		 */
409285414Sbapt		if (GETGRGID(prefer) == NULL)
410285414Sbapt			grid = prefer;
411286196Sbapt		if (dryrun) {
412285395Sbapt			gid = pw_groupnext(cnf, true);
413285395Sbapt		} else {
414286196Sbapt			if (grid == -1)
415286196Sbapt				grid =  pw_groupnext(cnf, true);
416286196Sbapt			groupadd(cnf, nam, grid, NULL, -1, false, false, false);
41744229Sdavidn			if ((grp = GETGRNAM(nam)) != NULL)
41820267Sjoerg				gid = grp->gr_gid;
41920267Sjoerg		}
42020253Sjoerg	}
42144229Sdavidn	ENDGRENT();
422286196Sbapt	return (gid);
42320253Sjoerg}
42420253Sjoerg
425286196Sbaptstatic char *
426286196Sbaptpw_homepolicy(struct userconf * cnf, char *homedir, const char *user)
42720253Sjoerg{
428282699Sbapt	static char     home[128];
42920253Sjoerg
430286196Sbapt	if (homedir)
431286196Sbapt		return (homedir);
43220253Sjoerg
433282699Sbapt	if (cnf->home == NULL || *cnf->home == '\0')
434282699Sbapt		errx(EX_CONFIG, "no base home directory set");
435282699Sbapt	snprintf(home, sizeof(home), "%s/%s", cnf->home, user);
436282699Sbapt
437282699Sbapt	return (home);
43820253Sjoerg}
43920253Sjoerg
440286196Sbaptstatic char *
44120253Sjoergshell_path(char const * path, char *shells[], char *sh)
44220253Sjoerg{
44320253Sjoerg	if (sh != NULL && (*sh == '/' || *sh == '\0'))
44420253Sjoerg		return sh;	/* specified full path or forced none */
44520253Sjoerg	else {
44620253Sjoerg		char           *p;
44720253Sjoerg		char            paths[_UC_MAXLINE];
44820253Sjoerg
44920253Sjoerg		/*
45020253Sjoerg		 * We need to search paths
45120253Sjoerg		 */
452130633Srobert		strlcpy(paths, path, sizeof(paths));
45320253Sjoerg		for (p = strtok(paths, ": \t\r\n"); p != NULL; p = strtok(NULL, ": \t\r\n")) {
45420253Sjoerg			int             i;
45520253Sjoerg			static char     shellpath[256];
45620253Sjoerg
45720253Sjoerg			if (sh != NULL) {
458282700Sbapt				snprintf(shellpath, sizeof(shellpath), "%s/%s", p, sh);
45920253Sjoerg				if (access(shellpath, X_OK) == 0)
46020253Sjoerg					return shellpath;
46120253Sjoerg			} else
46220253Sjoerg				for (i = 0; i < _UC_MAXSHELLS && shells[i] != NULL; i++) {
463282700Sbapt					snprintf(shellpath, sizeof(shellpath), "%s/%s", p, shells[i]);
46420253Sjoerg					if (access(shellpath, X_OK) == 0)
46520253Sjoerg						return shellpath;
46620253Sjoerg				}
46720253Sjoerg		}
46820253Sjoerg		if (sh == NULL)
46930259Scharnier			errx(EX_OSFILE, "can't find shell `%s' in shell paths", sh);
47030259Scharnier		errx(EX_CONFIG, "no default shell available or defined");
47120253Sjoerg		return NULL;
47220253Sjoerg	}
47320253Sjoerg}
47420253Sjoerg
475286196Sbaptstatic char *
476286196Sbaptpw_shellpolicy(struct userconf * cnf)
47720253Sjoerg{
47820253Sjoerg
479286196Sbapt	return shell_path(cnf->shelldir, cnf->shells, cnf->shell_default);
48020253Sjoerg}
48120253Sjoerg
482179365Santoine#define	SALTSIZE	32
48320253Sjoerg
484179365Santoinestatic char const chars[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ./";
485179365Santoine
486286196Sbaptchar *
48720253Sjoergpw_pwcrypt(char *password)
48820253Sjoerg{
48920253Sjoerg	int             i;
490179365Santoine	char            salt[SALTSIZE + 1];
491231994Skevlo	char		*cryptpw;
49220253Sjoerg	static char     buf[256];
49320253Sjoerg
49420253Sjoerg	/*
49520253Sjoerg	 * Calculate a salt value
49620253Sjoerg	 */
497179365Santoine	for (i = 0; i < SALTSIZE; i++)
498181785Sache		salt[i] = chars[arc4random_uniform(sizeof(chars) - 1)];
499179365Santoine	salt[SALTSIZE] = '\0';
50020253Sjoerg
501231994Skevlo	cryptpw = crypt(password, salt);
502231994Skevlo	if (cryptpw == NULL)
503231994Skevlo		errx(EX_CONFIG, "crypt(3) failure");
504231994Skevlo	return strcpy(buf, cryptpw);
50520253Sjoerg}
50620253Sjoerg
507286196Sbaptstatic char *
508286196Sbaptpw_password(struct userconf * cnf, char const * user, bool dryrun)
50920253Sjoerg{
51020253Sjoerg	int             i, l;
51120253Sjoerg	char            pwbuf[32];
51220253Sjoerg
51320253Sjoerg	switch (cnf->default_password) {
51420253Sjoerg	case -1:		/* Random password */
51573563Skris		l = (arc4random() % 8 + 8);	/* 8 - 16 chars */
51620253Sjoerg		for (i = 0; i < l; i++)
517181785Sache			pwbuf[i] = chars[arc4random_uniform(sizeof(chars)-1)];
51820253Sjoerg		pwbuf[i] = '\0';
51920253Sjoerg
52020253Sjoerg		/*
52120253Sjoerg		 * We give this information back to the user
52220253Sjoerg		 */
523286196Sbapt		if (conf.fd == -1 && !dryrun) {
52461957Sache			if (isatty(STDOUT_FILENO))
52520712Sdavidn				printf("Password for '%s' is: ", user);
52620253Sjoerg			printf("%s\n", pwbuf);
52720253Sjoerg			fflush(stdout);
52820253Sjoerg		}
52920253Sjoerg		break;
53020253Sjoerg
53120253Sjoerg	case -2:		/* No password at all! */
53220253Sjoerg		return "";
53320253Sjoerg
53420253Sjoerg	case 0:		/* No login - default */
53520253Sjoerg	default:
53620253Sjoerg		return "*";
53720253Sjoerg
53820253Sjoerg	case 1:		/* user's name */
539130633Srobert		strlcpy(pwbuf, user, sizeof(pwbuf));
54020253Sjoerg		break;
54120253Sjoerg	}
54220253Sjoerg	return pw_pwcrypt(pwbuf);
54320253Sjoerg}
54420253Sjoerg
545284111Sbaptstatic int
546286196Sbaptprint_user(struct passwd * pwd, bool pretty, bool v7)
547284111Sbapt{
548286196Sbapt	int		j;
549286196Sbapt	char           *p;
550286196Sbapt	struct group   *grp = GETGRGID(pwd->pw_gid);
551286196Sbapt	char            uname[60] = "User &", office[60] = "[None]",
552286196Sbapt			wphone[60] = "[None]", hphone[60] = "[None]";
553286196Sbapt	char		acexpire[32] = "[None]", pwexpire[32] = "[None]";
554286196Sbapt	struct tm *    tptr;
555286196Sbapt
556286196Sbapt	if (!pretty) {
557286196Sbapt		p = v7 ? pw_make_v7(pwd) : pw_make(pwd);
558286196Sbapt		printf("%s\n", p);
559286196Sbapt		free(p);
560286196Sbapt		return (EXIT_SUCCESS);
561286196Sbapt	}
562286196Sbapt
563286196Sbapt	if ((p = strtok(pwd->pw_gecos, ",")) != NULL) {
564286196Sbapt		strlcpy(uname, p, sizeof(uname));
565286196Sbapt		if ((p = strtok(NULL, ",")) != NULL) {
566286196Sbapt			strlcpy(office, p, sizeof(office));
567286196Sbapt			if ((p = strtok(NULL, ",")) != NULL) {
568286196Sbapt				strlcpy(wphone, p, sizeof(wphone));
569286196Sbapt				if ((p = strtok(NULL, "")) != NULL) {
570286196Sbapt					strlcpy(hphone, p, sizeof(hphone));
571286196Sbapt				}
572286196Sbapt			}
573286196Sbapt		}
574286196Sbapt	}
575286196Sbapt	/*
576286196Sbapt	 * Handle '&' in gecos field
577286196Sbapt	 */
578286196Sbapt	if ((p = strchr(uname, '&')) != NULL) {
579286196Sbapt		int             l = strlen(pwd->pw_name);
580286196Sbapt		int             m = strlen(p);
581286196Sbapt
582286196Sbapt		memmove(p + l, p + 1, m);
583286196Sbapt		memmove(p, pwd->pw_name, l);
584286196Sbapt		*p = (char) toupper((unsigned char)*p);
585286196Sbapt	}
586286196Sbapt	if (pwd->pw_expire > (time_t)0 && (tptr = localtime(&pwd->pw_expire)) != NULL)
587286196Sbapt		strftime(acexpire, sizeof acexpire, "%c", tptr);
588286196Sbapt		if (pwd->pw_change > (time_t)0 && (tptr = localtime(&pwd->pw_change)) != NULL)
589286196Sbapt		strftime(pwexpire, sizeof pwexpire, "%c", tptr);
590286196Sbapt	printf("Login Name: %-15s   #%-12ju Group: %-15s   #%ju\n"
591286196Sbapt	       " Full Name: %s\n"
592286196Sbapt	       "      Home: %-26.26s      Class: %s\n"
593286196Sbapt	       "     Shell: %-26.26s     Office: %s\n"
594286196Sbapt	       "Work Phone: %-26.26s Home Phone: %s\n"
595286196Sbapt	       "Acc Expire: %-26.26s Pwd Expire: %s\n",
596286196Sbapt	       pwd->pw_name, (uintmax_t)pwd->pw_uid,
597286196Sbapt	       grp ? grp->gr_name : "(invalid)", (uintmax_t)pwd->pw_gid,
598286196Sbapt	       uname, pwd->pw_dir, pwd->pw_class,
599286196Sbapt	       pwd->pw_shell, office, wphone, hphone,
600286196Sbapt	       acexpire, pwexpire);
601286196Sbapt        SETGRENT();
602286196Sbapt	j = 0;
603286196Sbapt	while ((grp=GETGRENT()) != NULL) {
604286196Sbapt		int     i = 0;
605286196Sbapt		if (grp->gr_mem != NULL) {
606286196Sbapt			while (grp->gr_mem[i] != NULL) {
607286196Sbapt				if (strcmp(grp->gr_mem[i], pwd->pw_name)==0) {
608286196Sbapt					printf(j++ == 0 ? "    Groups: %s" : ",%s", grp->gr_name);
609286196Sbapt					break;
610286196Sbapt				}
611286196Sbapt				++i;
612286196Sbapt			}
613286196Sbapt		}
614286196Sbapt	}
615286196Sbapt	ENDGRENT();
616286196Sbapt	printf("%s", j ? "\n" : "");
617286196Sbapt	return (EXIT_SUCCESS);
618286196Sbapt}
619286196Sbapt
620286196Sbaptchar *
621286196Sbaptpw_checkname(char *name, int gecos)
622286196Sbapt{
623286196Sbapt	char showch[8];
624286196Sbapt	const char *badchars, *ch, *showtype;
625286196Sbapt	int reject;
626286196Sbapt
627286196Sbapt	ch = name;
628286196Sbapt	reject = 0;
629286196Sbapt	if (gecos) {
630286196Sbapt		/* See if the name is valid as a gecos (comment) field. */
631286196Sbapt		badchars = ":!@";
632286196Sbapt		showtype = "gecos field";
633286196Sbapt	} else {
634286196Sbapt		/* See if the name is valid as a userid or group. */
635286196Sbapt		badchars = " ,\t:+&#%$^()!@~*?<>=|\\/\"";
636286196Sbapt		showtype = "userid/group name";
637286196Sbapt		/* Userids and groups can not have a leading '-'. */
638286196Sbapt		if (*ch == '-')
639286196Sbapt			reject = 1;
640286196Sbapt	}
641286196Sbapt	if (!reject) {
642286196Sbapt		while (*ch) {
643291658Sbapt			if (strchr(badchars, *ch) != NULL ||
644291658Sbapt			    (!gecos && *ch < ' ') ||
645286196Sbapt			    *ch == 127) {
646286196Sbapt				reject = 1;
647286196Sbapt				break;
648286196Sbapt			}
649286196Sbapt			/* 8-bit characters are only allowed in GECOS fields */
650286196Sbapt			if (!gecos && (*ch & 0x80)) {
651286196Sbapt				reject = 1;
652286196Sbapt				break;
653286196Sbapt			}
654286196Sbapt			ch++;
655286196Sbapt		}
656286196Sbapt	}
657286196Sbapt	/*
658286196Sbapt	 * A `$' is allowed as the final character for userids and groups,
659286196Sbapt	 * mainly for the benefit of samba.
660286196Sbapt	 */
661286196Sbapt	if (reject && !gecos) {
662286196Sbapt		if (*ch == '$' && *(ch + 1) == '\0') {
663286196Sbapt			reject = 0;
664286196Sbapt			ch++;
665286196Sbapt		}
666286196Sbapt	}
667286196Sbapt	if (reject) {
668286196Sbapt		snprintf(showch, sizeof(showch), (*ch >= ' ' && *ch < 127)
669286196Sbapt		    ? "`%c'" : "0x%02x", *ch);
670286196Sbapt		errx(EX_DATAERR, "invalid character %s at position %td in %s",
671286196Sbapt		    showch, (ch - name), showtype);
672286196Sbapt	}
673286196Sbapt	if (!gecos && (ch - name) > LOGNAMESIZE)
674286196Sbapt		errx(EX_USAGE, "name too long `%s' (max is %d)", name,
675286196Sbapt		    LOGNAMESIZE);
676286196Sbapt
677286196Sbapt	return (name);
678286196Sbapt}
679286196Sbapt
680286196Sbaptstatic void
681286196Sbaptrmat(uid_t uid)
682286196Sbapt{
683286196Sbapt	DIR            *d = opendir("/var/at/jobs");
684286196Sbapt
685286196Sbapt	if (d != NULL) {
686286196Sbapt		struct dirent  *e;
687286196Sbapt
688286196Sbapt		while ((e = readdir(d)) != NULL) {
689286196Sbapt			struct stat     st;
690286196Sbapt
691286196Sbapt			if (strncmp(e->d_name, ".lock", 5) != 0 &&
692286196Sbapt			    stat(e->d_name, &st) == 0 &&
693286196Sbapt			    !S_ISDIR(st.st_mode) &&
694286196Sbapt			    st.st_uid == uid) {
695286196Sbapt				char            tmp[MAXPATHLEN];
696286196Sbapt
697286202Sbapt				snprintf(tmp, sizeof(tmp), "/usr/bin/atrm %s",
698286202Sbapt				    e->d_name);
699286196Sbapt				system(tmp);
700286196Sbapt			}
701286196Sbapt		}
702286196Sbapt		closedir(d);
703286196Sbapt	}
704286196Sbapt}
705286196Sbapt
706286196Sbaptstatic void
707286196Sbaptrmopie(char const * name)
708286196Sbapt{
709286196Sbapt	char tmp[1014];
710286196Sbapt	FILE *fp;
711286196Sbapt	int fd;
712286196Sbapt	size_t len;
713286196Sbapt	off_t	atofs = 0;
714286196Sbapt
715286196Sbapt	if ((fd = openat(conf.rootfd, "etc/opiekeys", O_RDWR)) == -1)
716286196Sbapt		return;
717286196Sbapt
718286196Sbapt	fp = fdopen(fd, "r+");
719286196Sbapt	len = strlen(name);
720286196Sbapt
721286196Sbapt	while (fgets(tmp, sizeof(tmp), fp) != NULL) {
722286196Sbapt		if (strncmp(name, tmp, len) == 0 && tmp[len]==' ') {
723286196Sbapt			/* Comment username out */
724286196Sbapt			if (fseek(fp, atofs, SEEK_SET) == 0)
725286196Sbapt				fwrite("#", 1, 1, fp);
726286196Sbapt			break;
727286196Sbapt		}
728286196Sbapt		atofs = ftell(fp);
729286196Sbapt	}
730286196Sbapt	/*
731286196Sbapt	 * If we got an error of any sort, don't update!
732286196Sbapt	 */
733286196Sbapt	fclose(fp);
734286196Sbapt}
735286196Sbapt
736286196Sbaptint
737286196Sbaptpw_user_next(int argc, char **argv, char *name __unused)
738286196Sbapt{
739286196Sbapt	struct userconf *cnf = NULL;
740286196Sbapt	const char *cfg = NULL;
741286196Sbapt	int ch;
742286196Sbapt	bool quiet = false;
743286196Sbapt	uid_t next;
744286196Sbapt
745301367Sbapt	while ((ch = getopt(argc, argv, "C:q")) != -1) {
746286196Sbapt		switch (ch) {
747286196Sbapt		case 'C':
748286196Sbapt			cfg = optarg;
749286196Sbapt			break;
750286196Sbapt		case 'q':
751286217Sadrian			quiet = true;
752286196Sbapt			break;
753286196Sbapt		}
754286196Sbapt	}
755286196Sbapt
756286196Sbapt	if (quiet)
757286196Sbapt		freopen(_PATH_DEVNULL, "w", stderr);
758286196Sbapt
759286196Sbapt	cnf = get_userconfig(cfg);
760286196Sbapt
761286196Sbapt	next = pw_uidpolicy(cnf, -1);
762286196Sbapt
763286196Sbapt	printf("%ju:", (uintmax_t)next);
764286196Sbapt	pw_groupnext(cnf, quiet);
765286196Sbapt
766286196Sbapt	return (EXIT_SUCCESS);
767286196Sbapt}
768286196Sbapt
769286196Sbaptint
770286196Sbaptpw_user_show(int argc, char **argv, char *arg1)
771286196Sbapt{
772285401Sbapt	struct passwd *pwd = NULL;
773286196Sbapt	char *name = NULL;
774286218Sbapt	intmax_t id = -1;
775286196Sbapt	int ch;
776286196Sbapt	bool all = false;
777286196Sbapt	bool pretty = false;
778286196Sbapt	bool force = false;
779286196Sbapt	bool v7 = false;
780286196Sbapt	bool quiet = false;
78120253Sjoerg
782286196Sbapt	if (arg1 != NULL) {
783286259Sed		if (arg1[strspn(arg1, "0123456789")] == '\0')
784286196Sbapt			id = pw_checkid(arg1, UID_MAX);
785286196Sbapt		else
786286196Sbapt			name = arg1;
787286196Sbapt	}
788286196Sbapt
789286196Sbapt	while ((ch = getopt(argc, argv, "C:qn:u:FPa7")) != -1) {
790286196Sbapt		switch (ch) {
791286196Sbapt		case 'C':
792286196Sbapt			/* ignore compatibility */
793286196Sbapt			break;
794286196Sbapt		case 'q':
795286196Sbapt			quiet = true;
796286196Sbapt			break;
797286196Sbapt		case 'n':
798286196Sbapt			name = optarg;
799286196Sbapt			break;
800286196Sbapt		case 'u':
801286196Sbapt			id = pw_checkid(optarg, UID_MAX);
802286196Sbapt			break;
803286196Sbapt		case 'F':
804286196Sbapt			force = true;
805286196Sbapt			break;
806286196Sbapt		case 'P':
807286196Sbapt			pretty = true;
808286196Sbapt			break;
809286196Sbapt		case 'a':
810286196Sbapt			all = true;
811286196Sbapt			break;
812287799Sbapt		case '7':
813286196Sbapt			v7 = true;
814286196Sbapt			break;
815286196Sbapt		}
816286196Sbapt	}
817286196Sbapt
818286196Sbapt	if (quiet)
819286196Sbapt		freopen(_PATH_DEVNULL, "w", stderr);
820286196Sbapt
821286196Sbapt	if (all) {
822286196Sbapt		SETPWENT();
823286196Sbapt		while ((pwd = GETPWENT()) != NULL)
824286196Sbapt			print_user(pwd, pretty, v7);
825286196Sbapt		ENDPWENT();
826286196Sbapt		return (EXIT_SUCCESS);
827286196Sbapt	}
828286196Sbapt
829285401Sbapt	if (id < 0 && name == NULL)
830285401Sbapt		errx(EX_DATAERR, "username or id required");
831285401Sbapt
832285401Sbapt	pwd = (name != NULL) ? GETPWNAM(pw_checkname(name, 0)) : GETPWUID(id);
833285401Sbapt	if (pwd == NULL) {
834286196Sbapt		if (force) {
835286196Sbapt			pwd = &fakeuser;
836286196Sbapt		} else {
837286196Sbapt			if (name == NULL)
838286196Sbapt				errx(EX_NOUSER, "no such uid `%ju'",
839286196Sbapt				    (uintmax_t) id);
840286196Sbapt			errx(EX_NOUSER, "no such user `%s'", name);
841286196Sbapt		}
842286196Sbapt	}
843286196Sbapt
844286196Sbapt	return (print_user(pwd, pretty, v7));
845286196Sbapt}
846286196Sbapt
847286196Sbaptint
848286196Sbaptpw_user_del(int argc, char **argv, char *arg1)
849286196Sbapt{
850286196Sbapt	struct userconf *cnf = NULL;
851286196Sbapt	struct passwd *pwd = NULL;
852286196Sbapt	struct group *gr, *grp;
853286196Sbapt	char *name = NULL;
854286196Sbapt	char grname[MAXLOGNAME];
855286196Sbapt	char *nispasswd = NULL;
856286196Sbapt	char file[MAXPATHLEN];
857286196Sbapt	char home[MAXPATHLEN];
858286196Sbapt	const char *cfg = NULL;
859286196Sbapt	struct stat st;
860286218Sbapt	intmax_t id = -1;
861286196Sbapt	int ch, rc;
862286196Sbapt	bool nis = false;
863286196Sbapt	bool deletehome = false;
864286196Sbapt	bool quiet = false;
865286196Sbapt
866286196Sbapt	if (arg1 != NULL) {
867286259Sed		if (arg1[strspn(arg1, "0123456789")] == '\0')
868286196Sbapt			id = pw_checkid(arg1, UID_MAX);
869286196Sbapt		else
870286196Sbapt			name = arg1;
871286196Sbapt	}
872286196Sbapt
873286196Sbapt	while ((ch = getopt(argc, argv, "C:qn:u:rYy:")) != -1) {
874286196Sbapt		switch (ch) {
875286196Sbapt		case 'C':
876286196Sbapt			cfg = optarg;
877286196Sbapt			break;
878286196Sbapt		case 'q':
879286196Sbapt			quiet = true;
880286196Sbapt			break;
881286196Sbapt		case 'n':
882286196Sbapt			name = optarg;
883286196Sbapt			break;
884286196Sbapt		case 'u':
885286196Sbapt			id = pw_checkid(optarg, UID_MAX);
886286196Sbapt			break;
887286196Sbapt		case 'r':
888286196Sbapt			deletehome = true;
889286196Sbapt			break;
890286196Sbapt		case 'y':
891286196Sbapt			nispasswd = optarg;
892286196Sbapt			break;
893286196Sbapt		case 'Y':
894286196Sbapt			nis = true;
895286196Sbapt			break;
896286196Sbapt		}
897286196Sbapt	}
898286196Sbapt
899286196Sbapt	if (quiet)
900286196Sbapt		freopen(_PATH_DEVNULL, "w", stderr);
901286196Sbapt
902286196Sbapt	if (id < 0 && name == NULL)
903286196Sbapt		errx(EX_DATAERR, "username or id required");
904286196Sbapt
905286196Sbapt	cnf = get_userconfig(cfg);
906286196Sbapt
907286196Sbapt	if (nispasswd == NULL)
908286196Sbapt		nispasswd = cnf->nispasswd;
909286196Sbapt
910286196Sbapt	pwd = (name != NULL) ? GETPWNAM(pw_checkname(name, 0)) : GETPWUID(id);
911286196Sbapt	if (pwd == NULL) {
912285401Sbapt		if (name == NULL)
913286196Sbapt			errx(EX_NOUSER, "no such uid `%ju'", (uintmax_t) id);
914285401Sbapt		errx(EX_NOUSER, "no such user `%s'", name);
915285401Sbapt	}
916285989Sbapt
917285989Sbapt	if (PWF._altdir == PWF_REGULAR &&
918286196Sbapt	    ((pwd->pw_fields & _PWF_SOURCE) != _PWF_FILES)) {
919286196Sbapt		if ((pwd->pw_fields & _PWF_SOURCE) == _PWF_NIS) {
920286196Sbapt			if (!nis && nispasswd && *nispasswd != '/')
921286196Sbapt				errx(EX_NOUSER, "Cannot remove NIS user `%s'",
922286196Sbapt				    name);
923286196Sbapt		} else {
924286196Sbapt			errx(EX_NOUSER, "Cannot remove non local user `%s'",
925286196Sbapt			    name);
926286196Sbapt		}
927286196Sbapt	}
928285989Sbapt
929286196Sbapt	id = pwd->pw_uid;
930285401Sbapt	if (name == NULL)
931285401Sbapt		name = pwd->pw_name;
932285401Sbapt
933284111Sbapt	if (strcmp(pwd->pw_name, "root") == 0)
934284111Sbapt		errx(EX_DATAERR, "cannot remove user 'root'");
935284111Sbapt
936286196Sbapt	/* Remove opie record from /etc/opiekeys */
937285433Sbapt	if (PWALTDIR() != PWF_ALT)
938284111Sbapt		rmopie(pwd->pw_name);
939284111Sbapt
940285433Sbapt	if (!PWALTDIR()) {
941285433Sbapt		/* Remove crontabs */
942284111Sbapt		snprintf(file, sizeof(file), "/var/cron/tabs/%s", pwd->pw_name);
943284111Sbapt		if (access(file, F_OK) == 0) {
944286202Sbapt			snprintf(file, sizeof(file), "crontab -u %s -r",
945286202Sbapt			    pwd->pw_name);
946284111Sbapt			system(file);
947284111Sbapt		}
948284111Sbapt	}
949286196Sbapt
950284111Sbapt	/*
951284111Sbapt	 * Save these for later, since contents of pwd may be
952284111Sbapt	 * invalidated by deletion
953284111Sbapt	 */
954284111Sbapt	snprintf(file, sizeof(file), "%s/%s", _PATH_MAILDIR, pwd->pw_name);
955284111Sbapt	strlcpy(home, pwd->pw_dir, sizeof(home));
956284111Sbapt	gr = GETGRGID(pwd->pw_gid);
957284111Sbapt	if (gr != NULL)
958284111Sbapt		strlcpy(grname, gr->gr_name, LOGNAMESIZE);
959284111Sbapt	else
960284111Sbapt		grname[0] = '\0';
961284111Sbapt
962284111Sbapt	rc = delpwent(pwd);
963284111Sbapt	if (rc == -1)
964284111Sbapt		err(EX_IOERR, "user '%s' does not exist", pwd->pw_name);
965284111Sbapt	else if (rc != 0)
966284111Sbapt		err(EX_IOERR, "passwd update");
967284111Sbapt
968286196Sbapt	if (nis && nispasswd && *nispasswd=='/') {
969286196Sbapt		rc = delnispwent(nispasswd, name);
970284111Sbapt		if (rc == -1)
971285401Sbapt			warnx("WARNING: user '%s' does not exist in NIS passwd",
972285401Sbapt			    pwd->pw_name);
973284111Sbapt		else if (rc != 0)
974284111Sbapt			warn("WARNING: NIS passwd update");
975284111Sbapt	}
976284111Sbapt
977284128Sbapt	grp = GETGRNAM(name);
978284111Sbapt	if (grp != NULL &&
979284111Sbapt	    (grp->gr_mem == NULL || *grp->gr_mem == NULL) &&
980284128Sbapt	    strcmp(name, grname) == 0)
981284128Sbapt		delgrent(GETGRNAM(name));
982284111Sbapt	SETGRENT();
983284111Sbapt	while ((grp = GETGRENT()) != NULL) {
984284111Sbapt		int i, j;
985284111Sbapt		char group[MAXLOGNAME];
986284113Sbapt		if (grp->gr_mem == NULL)
987284113Sbapt			continue;
988284113Sbapt
989284113Sbapt		for (i = 0; grp->gr_mem[i] != NULL; i++) {
990284128Sbapt			if (strcmp(grp->gr_mem[i], name) != 0)
991284113Sbapt				continue;
992284113Sbapt
993284113Sbapt			for (j = i; grp->gr_mem[j] != NULL; j++)
994284113Sbapt				grp->gr_mem[j] = grp->gr_mem[j+1];
995284113Sbapt			strlcpy(group, grp->gr_name, MAXLOGNAME);
996284113Sbapt			chggrent(group, grp);
997284111Sbapt		}
998284111Sbapt	}
999284111Sbapt	ENDGRENT();
1000284111Sbapt
1001286196Sbapt	pw_log(cnf, M_DELETE, W_USER, "%s(%ju) account removed", name,
1002286196Sbapt	    (uintmax_t)id);
1003284111Sbapt
1004285433Sbapt	/* Remove mail file */
1005285433Sbapt	if (PWALTDIR() != PWF_ALT)
1006285433Sbapt		unlinkat(conf.rootfd, file + 1, 0);
1007284111Sbapt
1008286196Sbapt	/* Remove at jobs */
1009286196Sbapt	if (!PWALTDIR() && getpwuid(id) == NULL)
1010286196Sbapt		rmat(id);
1011284111Sbapt
1012285433Sbapt	/* Remove home directory and contents */
1013286196Sbapt	if (PWALTDIR() != PWF_ALT && deletehome && *home == '/' &&
1014286196Sbapt	    GETPWUID(id) == NULL &&
1015285433Sbapt	    fstatat(conf.rootfd, home + 1, &st, 0) != -1) {
1016286196Sbapt		rm_r(conf.rootfd, home, id);
1017286196Sbapt		pw_log(cnf, M_DELETE, W_USER, "%s(%ju) home '%s' %s"
1018286196Sbapt		    "removed", name, (uintmax_t)id, home,
1019285433Sbapt		     fstatat(conf.rootfd, home + 1, &st, 0) == -1 ? "" : "not "
1020285433Sbapt		     "completely ");
1021284111Sbapt	}
1022284111Sbapt
1023284111Sbapt	return (EXIT_SUCCESS);
1024284111Sbapt}
1025284111Sbapt
1026286196Sbaptint
1027286196Sbaptpw_user_lock(int argc, char **argv, char *arg1)
102820253Sjoerg{
1029286196Sbapt	int ch;
103020253Sjoerg
1031286196Sbapt	while ((ch = getopt(argc, argv, "Cq")) != -1) {
1032286196Sbapt		switch (ch) {
1033286196Sbapt		case 'C':
1034286196Sbapt		case 'q':
1035286196Sbapt			/* compatibility */
1036286196Sbapt			break;
103720253Sjoerg		}
1038286196Sbapt	}
103920253Sjoerg
1040286196Sbapt	return (pw_userlock(arg1, M_LOCK));
1041286196Sbapt}
1042286196Sbapt
1043286196Sbaptint
1044286196Sbaptpw_user_unlock(int argc, char **argv, char *arg1)
1045286196Sbapt{
1046286196Sbapt	int ch;
1047286196Sbapt
1048286196Sbapt	while ((ch = getopt(argc, argv, "Cq")) != -1) {
1049286196Sbapt		switch (ch) {
1050286196Sbapt		case 'C':
1051286196Sbapt		case 'q':
1052286196Sbapt			/* compatibility */
1053286196Sbapt			break;
105420253Sjoerg		}
105520253Sjoerg	}
1056286196Sbapt
1057286196Sbapt	return (pw_userlock(arg1, M_UNLOCK));
105820253Sjoerg}
105920253Sjoerg
1060286196Sbaptstatic struct group *
1061286196Sbaptgroup_from_name_or_id(char *name)
106220253Sjoerg{
1063286196Sbapt	const char *errstr = NULL;
1064286196Sbapt	struct group *grp;
1065286196Sbapt	uintmax_t id;
106620253Sjoerg
1067286196Sbapt	if ((grp = GETGRNAM(name)) == NULL) {
1068286196Sbapt		id = strtounum(name, 0, GID_MAX, &errstr);
1069286196Sbapt		if (errstr)
1070286196Sbapt			errx(EX_NOUSER, "group `%s' does not exist", name);
1071286196Sbapt		grp = GETGRGID(id);
1072286196Sbapt		if (grp == NULL)
1073286196Sbapt			errx(EX_NOUSER, "group `%s' does not exist", name);
107420253Sjoerg	}
1075286196Sbapt
1076286196Sbapt	return (grp);
1077286196Sbapt}
1078286196Sbapt
1079286196Sbaptstatic void
1080286196Sbaptsplit_groups(StringList **groups, char *groupsstr)
1081286196Sbapt{
1082286196Sbapt	struct group *grp;
1083286196Sbapt	char *p;
1084286196Sbapt	char tok[] = ", \t";
1085286196Sbapt
1086286196Sbapt	for (p = strtok(groupsstr, tok); p != NULL; p = strtok(NULL, tok)) {
1087286196Sbapt		grp = group_from_name_or_id(p);
1088286196Sbapt		if (*groups == NULL)
1089286196Sbapt			*groups = sl_init();
1090286196Sbapt		sl_add(*groups, newstr(grp->gr_name));
1091286196Sbapt	}
1092286196Sbapt}
1093286196Sbapt
1094286196Sbaptstatic void
1095286196Sbaptvalidate_grname(struct userconf *cnf, char *group)
1096286196Sbapt{
1097286196Sbapt	struct group *grp;
1098286196Sbapt
1099286196Sbapt	if (group == NULL || *group == '\0') {
1100286196Sbapt		cnf->default_group = "";
1101286196Sbapt		return;
1102286196Sbapt	}
1103286196Sbapt	grp = group_from_name_or_id(group);
1104286196Sbapt	cnf->default_group = newstr(grp->gr_name);
1105286196Sbapt}
1106286196Sbapt
1107286196Sbaptstatic mode_t
1108286196Sbaptvalidate_mode(char *mode)
1109286196Sbapt{
1110286196Sbapt	mode_t m;
1111286196Sbapt	void *set;
1112286196Sbapt
1113286196Sbapt	if ((set = setmode(mode)) == NULL)
1114286196Sbapt		errx(EX_DATAERR, "invalid directory creation mode '%s'", mode);
1115286196Sbapt
1116286196Sbapt	m = getmode(set, _DEF_DIRMODE);
1117286196Sbapt	free(set);
1118286196Sbapt	return (m);
1119286196Sbapt}
1120286196Sbapt
1121286196Sbaptstatic void
1122286196Sbaptmix_config(struct userconf *cmdcnf, struct userconf *cfg)
1123286196Sbapt{
1124286196Sbapt
1125286196Sbapt	if (cmdcnf->default_password == 0)
1126286196Sbapt		cmdcnf->default_password = cfg->default_password;
1127286196Sbapt	if (cmdcnf->reuse_uids == 0)
1128286196Sbapt		cmdcnf->reuse_uids = cfg->reuse_uids;
1129286196Sbapt	if (cmdcnf->reuse_gids == 0)
1130286196Sbapt		cmdcnf->reuse_gids = cfg->reuse_gids;
1131286196Sbapt	if (cmdcnf->nispasswd == NULL)
1132286196Sbapt		cmdcnf->nispasswd = cfg->nispasswd;
1133286196Sbapt	if (cmdcnf->dotdir == NULL)
1134286196Sbapt		cmdcnf->dotdir = cfg->dotdir;
1135286196Sbapt	if (cmdcnf->newmail == NULL)
1136286196Sbapt		cmdcnf->newmail = cfg->newmail;
1137286196Sbapt	if (cmdcnf->logfile == NULL)
1138286196Sbapt		cmdcnf->logfile = cfg->logfile;
1139286196Sbapt	if (cmdcnf->home == NULL)
1140286196Sbapt		cmdcnf->home = cfg->home;
1141286196Sbapt	if (cmdcnf->homemode == 0)
1142286196Sbapt		cmdcnf->homemode = cfg->homemode;
1143286196Sbapt	if (cmdcnf->shelldir == NULL)
1144286196Sbapt		cmdcnf->shelldir = cfg->shelldir;
1145286196Sbapt	if (cmdcnf->shells == NULL)
1146286196Sbapt		cmdcnf->shells = cfg->shells;
1147286196Sbapt	if (cmdcnf->shell_default == NULL)
1148286196Sbapt		cmdcnf->shell_default = cfg->shell_default;
1149286196Sbapt	if (cmdcnf->default_group == NULL)
1150286196Sbapt		cmdcnf->default_group = cfg->default_group;
1151286196Sbapt	if (cmdcnf->groups == NULL)
1152286196Sbapt		cmdcnf->groups = cfg->groups;
1153286196Sbapt	if (cmdcnf->default_class == NULL)
1154286196Sbapt		cmdcnf->default_class = cfg->default_class;
1155286196Sbapt	if (cmdcnf->min_uid == 0)
1156286196Sbapt		cmdcnf->min_uid = cfg->min_uid;
1157286196Sbapt	if (cmdcnf->max_uid == 0)
1158286196Sbapt		cmdcnf->max_uid = cfg->max_uid;
1159286196Sbapt	if (cmdcnf->min_gid == 0)
1160286196Sbapt		cmdcnf->min_gid = cfg->min_gid;
1161286196Sbapt	if (cmdcnf->max_gid == 0)
1162286196Sbapt		cmdcnf->max_gid = cfg->max_gid;
1163286196Sbapt	if (cmdcnf->expire_days == 0)
1164286196Sbapt		cmdcnf->expire_days = cfg->expire_days;
1165286196Sbapt	if (cmdcnf->password_days == 0)
1166286196Sbapt		cmdcnf->password_days = cfg->password_days;
1167286196Sbapt}
1168286196Sbapt
1169286196Sbaptint
1170286196Sbaptpw_user_add(int argc, char **argv, char *arg1)
1171286196Sbapt{
1172286196Sbapt	struct userconf *cnf, *cmdcnf;
1173286196Sbapt	struct passwd *pwd;
1174286196Sbapt	struct group *grp;
1175286196Sbapt	struct stat st;
1176286196Sbapt	char args[] = "C:qn:u:c:d:e:p:g:G:mM:k:s:oL:i:w:h:H:Db:NPy:Y";
1177286196Sbapt	char line[_PASSWORD_LEN+1], path[MAXPATHLEN];
1178286196Sbapt	char *gecos, *homedir, *skel, *walk, *userid, *groupid, *grname;
1179286196Sbapt	char *default_passwd, *name, *p;
1180286196Sbapt	const char *cfg;
1181286196Sbapt	login_cap_t *lc;
1182286196Sbapt	FILE *pfp, *fp;
1183286196Sbapt	intmax_t id = -1;
1184286196Sbapt	time_t now;
1185286196Sbapt	int rc, ch, fd = -1;
1186286196Sbapt	size_t i;
1187286196Sbapt	bool dryrun, nis, pretty, quiet, createhome, precrypted, genconf;
1188286196Sbapt
1189286196Sbapt	dryrun = nis = pretty = quiet = createhome = precrypted = false;
1190286196Sbapt	genconf = false;
1191286196Sbapt	gecos = homedir = skel = userid = groupid = default_passwd = NULL;
1192286196Sbapt	grname = name = NULL;
1193286196Sbapt
1194286196Sbapt	if ((cmdcnf = calloc(1, sizeof(struct userconf))) == NULL)
1195286196Sbapt		err(EXIT_FAILURE, "calloc()");
1196286196Sbapt
1197286196Sbapt	if (arg1 != NULL) {
1198286259Sed		if (arg1[strspn(arg1, "0123456789")] == '\0')
1199286196Sbapt			id = pw_checkid(arg1, UID_MAX);
1200286196Sbapt		else
1201286196Sbapt			name = arg1;
1202286196Sbapt	}
1203286196Sbapt
1204286196Sbapt	while ((ch = getopt(argc, argv, args)) != -1) {
1205286196Sbapt		switch (ch) {
1206286196Sbapt		case 'C':
1207286196Sbapt			cfg = optarg;
1208286196Sbapt			break;
1209286196Sbapt		case 'q':
1210286196Sbapt			quiet = true;
1211286196Sbapt			break;
1212286196Sbapt		case 'n':
1213286196Sbapt			name = optarg;
1214286196Sbapt			break;
1215286196Sbapt		case 'u':
1216286196Sbapt			userid = optarg;
1217286196Sbapt			break;
1218286196Sbapt		case 'c':
1219286196Sbapt			gecos = pw_checkname(optarg, 1);
1220286196Sbapt			break;
1221286196Sbapt		case 'd':
1222286196Sbapt			homedir = optarg;
1223286196Sbapt			break;
1224286196Sbapt		case 'e':
1225286196Sbapt			now = time(NULL);
1226286196Sbapt			cmdcnf->expire_days = parse_date(now, optarg);
1227286196Sbapt			break;
1228286196Sbapt		case 'p':
1229286196Sbapt			now = time(NULL);
1230286196Sbapt			cmdcnf->password_days = parse_date(now, optarg);
1231286196Sbapt			break;
1232286196Sbapt		case 'g':
1233286196Sbapt			validate_grname(cmdcnf, optarg);
1234286196Sbapt			grname = optarg;
1235286196Sbapt			break;
1236286196Sbapt		case 'G':
1237286196Sbapt			split_groups(&cmdcnf->groups, optarg);
1238286196Sbapt			break;
1239286196Sbapt		case 'm':
1240286196Sbapt			createhome = true;
1241286196Sbapt			break;
1242286196Sbapt		case 'M':
1243286196Sbapt			cmdcnf->homemode = validate_mode(optarg);
1244286196Sbapt			break;
1245286196Sbapt		case 'k':
1246286196Sbapt			walk = skel = optarg;
1247286196Sbapt			if (*walk == '/')
1248286196Sbapt				walk++;
1249286196Sbapt			if (fstatat(conf.rootfd, walk, &st, 0) == -1)
1250286196Sbapt				errx(EX_OSFILE, "skeleton `%s' does not "
1251286196Sbapt				    "exists", skel);
1252286196Sbapt			if (!S_ISDIR(st.st_mode))
1253286196Sbapt				errx(EX_OSFILE, "skeleton `%s' is not a "
1254286196Sbapt				    "directory", skel);
1255286196Sbapt			cmdcnf->dotdir = skel;
1256286196Sbapt			break;
1257286196Sbapt		case 's':
1258286196Sbapt			cmdcnf->shell_default = optarg;
1259286196Sbapt			break;
1260286196Sbapt		case 'o':
1261286196Sbapt			conf.checkduplicate = false;
1262286196Sbapt			break;
1263286196Sbapt		case 'L':
1264286196Sbapt			cmdcnf->default_class = pw_checkname(optarg, 0);
1265286196Sbapt			break;
1266286196Sbapt		case 'i':
1267286196Sbapt			groupid = optarg;
1268286196Sbapt			break;
1269286196Sbapt		case 'w':
1270286196Sbapt			default_passwd = optarg;
1271286196Sbapt			break;
1272286196Sbapt		case 'H':
1273286196Sbapt			if (fd != -1)
1274286196Sbapt				errx(EX_USAGE, "'-h' and '-H' are mutually "
1275286196Sbapt				    "exclusive options");
1276286196Sbapt			fd = pw_checkfd(optarg);
1277286196Sbapt			precrypted = true;
1278286196Sbapt			if (fd == '-')
1279286196Sbapt				errx(EX_USAGE, "-H expects a file descriptor");
1280286196Sbapt			break;
1281286196Sbapt		case 'h':
1282286196Sbapt			if (fd != -1)
1283286196Sbapt				errx(EX_USAGE, "'-h' and '-H' are mutually "
1284286196Sbapt				    "exclusive options");
1285286196Sbapt			fd = pw_checkfd(optarg);
1286286196Sbapt			break;
1287286196Sbapt		case 'D':
1288286196Sbapt			genconf = true;
1289286196Sbapt			break;
1290286196Sbapt		case 'b':
1291286196Sbapt			cmdcnf->home = optarg;
1292286196Sbapt			break;
1293286196Sbapt		case 'N':
1294286196Sbapt			dryrun = true;
1295286196Sbapt			break;
1296286196Sbapt		case 'P':
1297286196Sbapt			pretty = true;
1298286196Sbapt			break;
1299286196Sbapt		case 'y':
1300286196Sbapt			cmdcnf->nispasswd = optarg;
1301286196Sbapt			break;
1302286196Sbapt		case 'Y':
1303286196Sbapt			nis = true;
1304286196Sbapt			break;
1305109961Sgad		}
1306109961Sgad	}
1307286196Sbapt
1308286196Sbapt	if (geteuid() != 0 && ! dryrun)
1309286196Sbapt		errx(EX_NOPERM, "you must be root");
1310286196Sbapt
1311286196Sbapt	if (quiet)
1312286196Sbapt		freopen(_PATH_DEVNULL, "w", stderr);
1313286196Sbapt
1314286196Sbapt	cnf = get_userconfig(cfg);
1315286196Sbapt
1316286196Sbapt	mix_config(cmdcnf, cnf);
1317286196Sbapt	if (default_passwd)
1318286196Sbapt		cmdcnf->default_password = boolean_val(default_passwd,
1319286196Sbapt		    cnf->default_password);
1320286196Sbapt	if (genconf) {
1321286196Sbapt		if (name != NULL)
1322286196Sbapt			errx(EX_DATAERR, "can't combine `-D' with `-n name'");
1323286196Sbapt		if (userid != NULL) {
1324286196Sbapt			if ((p = strtok(userid, ", \t")) != NULL)
1325286196Sbapt				cmdcnf->min_uid = pw_checkid(p, UID_MAX);
1326286196Sbapt			if (cmdcnf->min_uid == 0)
1327286196Sbapt				cmdcnf->min_uid = 1000;
1328286196Sbapt			if ((p = strtok(NULL, " ,\t")) != NULL)
1329286196Sbapt				cmdcnf->max_uid = pw_checkid(p, UID_MAX);
1330286196Sbapt			if (cmdcnf->max_uid == 0)
1331286196Sbapt				cmdcnf->max_uid = 32000;
1332109961Sgad		}
1333286196Sbapt		if (groupid != NULL) {
1334286196Sbapt			if ((p = strtok(groupid, ", \t")) != NULL)
1335286196Sbapt				cmdcnf->min_gid = pw_checkid(p, GID_MAX);
1336286196Sbapt			if (cmdcnf->min_gid == 0)
1337286196Sbapt				cmdcnf->min_gid = 1000;
1338286196Sbapt			if ((p = strtok(NULL, " ,\t")) != NULL)
1339286196Sbapt				cmdcnf->max_gid = pw_checkid(p, GID_MAX);
1340286196Sbapt			if (cmdcnf->max_gid == 0)
1341286196Sbapt				cmdcnf->max_gid = 32000;
1342286196Sbapt		}
1343286196Sbapt		if (write_userconfig(cmdcnf, cfg))
1344286196Sbapt			return (EXIT_SUCCESS);
1345286196Sbapt		err(EX_IOERR, "config update");
1346109961Sgad	}
1347286196Sbapt
1348286196Sbapt	if (userid)
1349286196Sbapt		id = pw_checkid(userid, UID_MAX);
1350286196Sbapt	if (id < 0 && name == NULL)
1351286196Sbapt		errx(EX_DATAERR, "user name or id required");
1352286196Sbapt
1353286196Sbapt	if (name == NULL)
1354286196Sbapt		errx(EX_DATAERR, "login name required");
1355286196Sbapt
1356286198Sbapt	if (GETPWNAM(name) != NULL)
1357286198Sbapt		errx(EX_DATAERR, "login name `%s' already exists", name);
1358286198Sbapt
1359286196Sbapt	pwd = &fakeuser;
1360286196Sbapt	pwd->pw_name = name;
1361286196Sbapt	pwd->pw_class = cmdcnf->default_class ? cmdcnf->default_class : "";
1362286196Sbapt	pwd->pw_uid = pw_uidpolicy(cmdcnf, id);
1363286196Sbapt	pwd->pw_gid = pw_gidpolicy(cnf, grname, pwd->pw_name,
1364286196Sbapt	    (gid_t) pwd->pw_uid, dryrun);
1365286196Sbapt	pwd->pw_change = cmdcnf->password_days;
1366286196Sbapt	pwd->pw_expire = cmdcnf->expire_days;
1367286196Sbapt	pwd->pw_dir = pw_homepolicy(cmdcnf, homedir, pwd->pw_name);
1368286196Sbapt	pwd->pw_shell = pw_shellpolicy(cmdcnf);
1369286196Sbapt	lc = login_getpwclass(pwd);
1370286196Sbapt	if (lc == NULL || login_setcryptfmt(lc, "sha512", NULL) == NULL)
1371286196Sbapt		warn("setting crypt(3) format");
1372286196Sbapt	login_close(lc);
1373286196Sbapt	pwd->pw_passwd = pw_password(cmdcnf, pwd->pw_name, dryrun);
1374286196Sbapt	if (pwd->pw_uid == 0 && strcmp(pwd->pw_name, "root") != 0)
1375286196Sbapt		warnx("WARNING: new account `%s' has a uid of 0 "
1376286196Sbapt		    "(superuser access!)", pwd->pw_name);
1377286196Sbapt	if (gecos)
1378286196Sbapt		pwd->pw_gecos = gecos;
1379286196Sbapt
1380286196Sbapt	if (fd != -1)
1381286196Sbapt		pw_set_passwd(pwd, fd, precrypted, false);
1382286196Sbapt
1383286196Sbapt	if (dryrun)
1384286196Sbapt		return (print_user(pwd, pretty, false));
1385286196Sbapt
1386286196Sbapt	if ((rc = addpwent(pwd)) != 0) {
1387286196Sbapt		if (rc == -1)
1388286196Sbapt			errx(EX_IOERR, "user '%s' already exists",
1389286196Sbapt			    pwd->pw_name);
1390286196Sbapt		else if (rc != 0)
1391286196Sbapt			err(EX_IOERR, "passwd file update");
1392109961Sgad	}
1393286196Sbapt	if (nis && cmdcnf->nispasswd && *cmdcnf->nispasswd == '/') {
1394286196Sbapt		printf("%s\n", cmdcnf->nispasswd);
1395286196Sbapt		rc = addnispwent(cmdcnf->nispasswd, pwd);
1396286196Sbapt		if (rc == -1)
1397286202Sbapt			warnx("User '%s' already exists in NIS passwd",
1398286202Sbapt			    pwd->pw_name);
1399286196Sbapt		else if (rc != 0)
1400286196Sbapt			warn("NIS passwd update");
1401286196Sbapt		/* NOTE: we treat NIS-only update errors as non-fatal */
1402286196Sbapt	}
1403284110Sbapt
1404286196Sbapt	if (cmdcnf->groups != NULL) {
1405286196Sbapt		for (i = 0; i < cmdcnf->groups->sl_cur; i++) {
1406286196Sbapt			grp = GETGRNAM(cmdcnf->groups->sl_str[i]);
1407286196Sbapt			grp = gr_add(grp, pwd->pw_name);
1408286196Sbapt			/*
1409286196Sbapt			 * grp can only be NULL in 2 cases:
1410286196Sbapt			 * - the new member is already a member
1411286196Sbapt			 * - a problem with memory occurs
1412286196Sbapt			 * in both cases we want to skip now.
1413286196Sbapt			 */
1414286196Sbapt			if (grp == NULL)
1415286196Sbapt				continue;
1416286196Sbapt			chggrent(grp->gr_name, grp);
1417286196Sbapt			free(grp);
1418286196Sbapt		}
1419286196Sbapt	}
142020253Sjoerg
1421286196Sbapt	pwd = GETPWNAM(name);
1422286196Sbapt	if (pwd == NULL)
1423286196Sbapt		errx(EX_NOUSER, "user '%s' disappeared during update", name);
142420253Sjoerg
1425286196Sbapt	grp = GETGRGID(pwd->pw_gid);
1426286196Sbapt	pw_log(cnf, M_ADD, W_USER, "%s(%ju):%s(%ju):%s:%s:%s",
1427286196Sbapt	       pwd->pw_name, (uintmax_t)pwd->pw_uid,
1428286202Sbapt	    grp ? grp->gr_name : "unknown",
1429286202Sbapt	       (uintmax_t)(grp ? grp->gr_gid : (uid_t)-1),
1430286196Sbapt	       pwd->pw_gecos, pwd->pw_dir, pwd->pw_shell);
143120253Sjoerg
1432286196Sbapt	/*
1433286196Sbapt	 * let's touch and chown the user's mail file. This is not
1434286196Sbapt	 * strictly necessary under BSD with a 0755 maildir but it also
1435286196Sbapt	 * doesn't hurt anything to create the empty mailfile
1436286196Sbapt	 */
1437286196Sbapt	if (PWALTDIR() != PWF_ALT) {
1438286196Sbapt		snprintf(path, sizeof(path), "%s/%s", _PATH_MAILDIR,
1439286196Sbapt		    pwd->pw_name);
1440286196Sbapt		/* Preserve contents & mtime */
1441286196Sbapt		close(openat(conf.rootfd, path +1, O_RDWR | O_CREAT, 0600));
1442286196Sbapt		fchownat(conf.rootfd, path + 1, pwd->pw_uid, pwd->pw_gid,
1443286196Sbapt		    AT_SYMLINK_NOFOLLOW);
1444286196Sbapt	}
144520253Sjoerg
1446286196Sbapt	/*
1447286196Sbapt	 * Let's create and populate the user's home directory. Note
1448286196Sbapt	 * that this also `works' for editing users if -m is used, but
1449286196Sbapt	 * existing files will *not* be overwritten.
1450286196Sbapt	 */
1451286196Sbapt	if (PWALTDIR() != PWF_ALT && createhome && pwd->pw_dir &&
1452286196Sbapt	    *pwd->pw_dir == '/' && pwd->pw_dir[1])
1453286196Sbapt		create_and_populate_homedir(cmdcnf, pwd, cmdcnf->dotdir,
1454286196Sbapt		    cmdcnf->homemode, false);
145520253Sjoerg
1456286196Sbapt	if (!PWALTDIR() && cmdcnf->newmail && *cmdcnf->newmail &&
1457286196Sbapt	    (fp = fopen(cnf->newmail, "r")) != NULL) {
1458286196Sbapt		if ((pfp = popen(_PATH_SENDMAIL " -t", "w")) == NULL)
1459286196Sbapt			warn("sendmail");
1460286196Sbapt		else {
1461286196Sbapt			fprintf(pfp, "From: root\n" "To: %s\n"
1462286196Sbapt			    "Subject: Welcome!\n\n", pwd->pw_name);
1463286196Sbapt			while (fgets(line, sizeof(line), fp) != NULL) {
1464286196Sbapt				/* Do substitutions? */
1465286196Sbapt				fputs(line, pfp);
146620253Sjoerg			}
1467286196Sbapt			pclose(pfp);
1468286196Sbapt			pw_log(cnf, M_ADD, W_USER, "%s(%ju) new user mail sent",
1469286196Sbapt			    pwd->pw_name, (uintmax_t)pwd->pw_uid);
147020253Sjoerg		}
1471286196Sbapt		fclose(fp);
147220253Sjoerg	}
1473286196Sbapt
1474286196Sbapt	if (nis && nis_update() == 0)
1475286196Sbapt		pw_log(cnf, M_ADD, W_USER, "NIS maps updated");
1476286196Sbapt
1477286196Sbapt	return (EXIT_SUCCESS);
147820253Sjoerg}
147920747Sdavidn
1480286196Sbaptint
1481286196Sbaptpw_user_mod(int argc, char **argv, char *arg1)
148220747Sdavidn{
1483286196Sbapt	struct userconf *cnf;
1484286196Sbapt	struct passwd *pwd;
1485286196Sbapt	struct group *grp;
1486286196Sbapt	StringList *groups = NULL;
1487286196Sbapt	char args[] = "C:qn:u:c:d:e:p:g:G:mM:l:k:s:w:L:h:H:NPYy:";
1488286196Sbapt	const char *cfg;
1489286196Sbapt	char *gecos, *homedir, *grname, *name, *newname, *walk, *skel, *shell;
1490286196Sbapt	char *passwd, *class, *nispasswd;
1491286196Sbapt	login_cap_t *lc;
1492286196Sbapt	struct stat st;
1493286196Sbapt	intmax_t id = -1;
1494286196Sbapt	int ch, fd = -1;
1495286196Sbapt	size_t i, j;
1496286196Sbapt	bool quiet, createhome, pretty, dryrun, nis, edited, docreatehome;
1497286218Sbapt	bool precrypted;
1498286196Sbapt	mode_t homemode = 0;
1499286218Sbapt	time_t expire_days, password_days, now;
150020747Sdavidn
1501286196Sbapt	expire_days = password_days = -1;
1502286196Sbapt	gecos = homedir = grname = name = newname = skel = shell =NULL;
1503286196Sbapt	passwd = NULL;
1504286196Sbapt	class = nispasswd = NULL;
1505286196Sbapt	quiet = createhome = pretty = dryrun = nis = precrypted = false;
1506286196Sbapt	edited = docreatehome = false;
150720747Sdavidn
1508286196Sbapt	if (arg1 != NULL) {
1509286259Sed		if (arg1[strspn(arg1, "0123456789")] == '\0')
1510286196Sbapt			id = pw_checkid(arg1, UID_MAX);
1511286196Sbapt		else
1512286196Sbapt			name = arg1;
1513286196Sbapt	}
1514286196Sbapt
1515286196Sbapt	while ((ch = getopt(argc, argv, args)) != -1) {
1516286196Sbapt		switch (ch) {
1517286196Sbapt		case 'C':
1518286196Sbapt			cfg = optarg;
1519285433Sbapt			break;
1520286196Sbapt		case 'q':
1521286196Sbapt			quiet = true;
1522286196Sbapt			break;
1523286196Sbapt		case 'n':
1524286196Sbapt			name = optarg;
1525286196Sbapt			break;
1526286196Sbapt		case 'u':
1527286196Sbapt			id = pw_checkid(optarg, UID_MAX);
1528286196Sbapt			break;
1529286196Sbapt		case 'c':
1530286196Sbapt			gecos = pw_checkname(optarg, 1);
1531286196Sbapt			break;
1532286196Sbapt		case 'd':
1533286196Sbapt			homedir = optarg;
1534286196Sbapt			break;
1535286196Sbapt		case 'e':
1536286196Sbapt			now = time(NULL);
1537286196Sbapt			expire_days = parse_date(now, optarg);
1538286196Sbapt			break;
1539286196Sbapt		case 'p':
1540286196Sbapt			now = time(NULL);
1541286196Sbapt			password_days = parse_date(now, optarg);
1542286196Sbapt			break;
1543286196Sbapt		case 'g':
1544286196Sbapt			group_from_name_or_id(optarg);
1545286196Sbapt			grname = optarg;
1546286196Sbapt			break;
1547286196Sbapt		case 'G':
1548286196Sbapt			split_groups(&groups, optarg);
1549286196Sbapt			break;
1550286196Sbapt		case 'm':
1551286196Sbapt			createhome = true;
1552286196Sbapt			break;
1553286196Sbapt		case 'M':
1554286196Sbapt			homemode = validate_mode(optarg);
1555286196Sbapt			break;
1556286196Sbapt		case 'l':
1557286196Sbapt			newname = optarg;
1558286196Sbapt			break;
1559286196Sbapt		case 'k':
1560286196Sbapt			walk = skel = optarg;
1561286196Sbapt			if (*walk == '/')
1562286196Sbapt				walk++;
1563286196Sbapt			if (fstatat(conf.rootfd, walk, &st, 0) == -1)
1564286196Sbapt				errx(EX_OSFILE, "skeleton `%s' does not "
1565286196Sbapt				    "exists", skel);
1566286196Sbapt			if (!S_ISDIR(st.st_mode))
1567286196Sbapt				errx(EX_OSFILE, "skeleton `%s' is not a "
1568286196Sbapt				    "directory", skel);
1569286196Sbapt			break;
1570286196Sbapt		case 's':
1571286196Sbapt			shell = optarg;
1572286196Sbapt			break;
1573286196Sbapt		case 'w':
1574286196Sbapt			passwd = optarg;
1575286196Sbapt			break;
1576286196Sbapt		case 'L':
1577286196Sbapt			class = pw_checkname(optarg, 0);
1578286196Sbapt			break;
1579286196Sbapt		case 'H':
1580286196Sbapt			if (fd != -1)
1581286196Sbapt				errx(EX_USAGE, "'-h' and '-H' are mutually "
1582286196Sbapt				    "exclusive options");
1583286196Sbapt			fd = pw_checkfd(optarg);
1584286196Sbapt			precrypted = true;
1585286196Sbapt			if (fd == '-')
1586286196Sbapt				errx(EX_USAGE, "-H expects a file descriptor");
1587286196Sbapt			break;
1588286196Sbapt		case 'h':
1589286196Sbapt			if (fd != -1)
1590286196Sbapt				errx(EX_USAGE, "'-h' and '-H' are mutually "
1591286196Sbapt				    "exclusive options");
1592286196Sbapt			fd = pw_checkfd(optarg);
1593286196Sbapt			break;
1594286196Sbapt		case 'N':
1595286196Sbapt			dryrun = true;
1596286196Sbapt			break;
1597286196Sbapt		case 'P':
1598286196Sbapt			pretty = true;
1599286196Sbapt			break;
1600286196Sbapt		case 'y':
1601286196Sbapt			nispasswd = optarg;
1602286196Sbapt			break;
1603286196Sbapt		case 'Y':
1604286196Sbapt			nis = true;
1605286196Sbapt			break;
160620747Sdavidn		}
160720747Sdavidn	}
1608286196Sbapt
1609286196Sbapt	if (geteuid() != 0 && ! dryrun)
1610286196Sbapt		errx(EX_NOPERM, "you must be root");
1611286196Sbapt
1612286196Sbapt	if (quiet)
1613286196Sbapt		freopen(_PATH_DEVNULL, "w", stderr);
1614286196Sbapt
1615286196Sbapt	cnf = get_userconfig(cfg);
1616286196Sbapt
1617286196Sbapt	if (id < 0 && name == NULL)
1618286196Sbapt		errx(EX_DATAERR, "username or id required");
1619286196Sbapt
1620286196Sbapt	pwd = (name != NULL) ? GETPWNAM(pw_checkname(name, 0)) : GETPWUID(id);
1621286196Sbapt	if (pwd == NULL) {
1622286196Sbapt		if (name == NULL)
1623286196Sbapt			errx(EX_NOUSER, "no such uid `%ju'",
1624286196Sbapt			    (uintmax_t) id);
1625286196Sbapt		errx(EX_NOUSER, "no such user `%s'", name);
1626286196Sbapt	}
1627286196Sbapt
1628286196Sbapt	if (name == NULL)
1629286196Sbapt		name = pwd->pw_name;
1630286196Sbapt
1631286196Sbapt	if (nis && nispasswd == NULL)
1632286196Sbapt		nispasswd = cnf->nispasswd;
1633286196Sbapt
1634286196Sbapt	if (PWF._altdir == PWF_REGULAR &&
1635286196Sbapt	    ((pwd->pw_fields & _PWF_SOURCE) != _PWF_FILES)) {
1636286196Sbapt		if ((pwd->pw_fields & _PWF_SOURCE) == _PWF_NIS) {
1637286196Sbapt			if (!nis && nispasswd && *nispasswd != '/')
1638286196Sbapt				errx(EX_NOUSER, "Cannot modify NIS user `%s'",
1639286196Sbapt				    name);
1640286196Sbapt		} else {
1641286196Sbapt			errx(EX_NOUSER, "Cannot modify non local user `%s'",
1642286196Sbapt			    name);
1643286196Sbapt		}
1644286196Sbapt	}
1645286196Sbapt
1646286196Sbapt	if (newname) {
1647286196Sbapt		if (strcmp(pwd->pw_name, "root") == 0)
1648286196Sbapt			errx(EX_DATAERR, "can't rename `root' account");
1649286196Sbapt		if (strcmp(pwd->pw_name, newname) != 0) {
1650286196Sbapt			pwd->pw_name = pw_checkname(newname, 0);
1651286196Sbapt			edited = true;
1652286196Sbapt		}
1653286196Sbapt	}
1654286196Sbapt
1655296300Smarkj	if (id >= 0 && pwd->pw_uid != id) {
1656286196Sbapt		pwd->pw_uid = id;
1657286196Sbapt		edited = true;
1658286196Sbapt		if (pwd->pw_uid != 0 && strcmp(pwd->pw_name, "root") == 0)
1659286196Sbapt			errx(EX_DATAERR, "can't change uid of `root' account");
1660286196Sbapt		if (pwd->pw_uid == 0 && strcmp(pwd->pw_name, "root") != 0)
1661286202Sbapt			warnx("WARNING: account `%s' will have a uid of 0 "
1662286202Sbapt			    "(superuser access!)", pwd->pw_name);
1663286196Sbapt	}
1664286196Sbapt
1665286196Sbapt	if (grname && pwd->pw_uid != 0) {
1666286196Sbapt		grp = GETGRNAM(grname);
1667286196Sbapt		if (grp == NULL)
1668286196Sbapt			grp = GETGRGID(pw_checkid(grname, GID_MAX));
1669286196Sbapt		if (grp->gr_gid != pwd->pw_gid) {
1670286196Sbapt			pwd->pw_gid = grp->gr_gid;
1671286196Sbapt			edited = true;
1672286196Sbapt		}
1673286196Sbapt	}
1674286196Sbapt
1675286196Sbapt	if (password_days >= 0 && pwd->pw_change != password_days) {
1676286196Sbapt		pwd->pw_change = password_days;
1677286196Sbapt		edited = true;
1678286196Sbapt	}
1679286196Sbapt
1680286196Sbapt	if (expire_days >= 0 && pwd->pw_expire != expire_days) {
1681286196Sbapt		pwd->pw_expire = expire_days;
1682286196Sbapt		edited = true;
1683286196Sbapt	}
1684286196Sbapt
1685286196Sbapt	if (shell) {
1686286196Sbapt		shell = shell_path(cnf->shelldir, cnf->shells, shell);
1687286196Sbapt		if (shell == NULL)
1688286196Sbapt			shell = "";
1689286196Sbapt		if (strcmp(shell, pwd->pw_shell) != 0) {
1690286196Sbapt			pwd->pw_shell = shell;
1691286196Sbapt			edited = true;
1692286196Sbapt		}
1693286196Sbapt	}
1694286196Sbapt
1695286196Sbapt	if (class && strcmp(pwd->pw_class, class) != 0) {
1696286196Sbapt		pwd->pw_class = class;
1697286196Sbapt		edited = true;
1698286196Sbapt	}
1699286196Sbapt
1700286196Sbapt	if (homedir && strcmp(pwd->pw_dir, homedir) != 0) {
1701286196Sbapt		pwd->pw_dir = homedir;
1702287701Sbapt		edited = true;
1703286196Sbapt		if (fstatat(conf.rootfd, pwd->pw_dir, &st, 0) == -1) {
1704286196Sbapt			if (!createhome)
1705286196Sbapt				warnx("WARNING: home `%s' does not exist",
1706286196Sbapt				    pwd->pw_dir);
1707286196Sbapt			else
1708286196Sbapt				docreatehome = true;
1709286196Sbapt		} else if (!S_ISDIR(st.st_mode)) {
1710286196Sbapt			warnx("WARNING: home `%s' is not a directory",
1711286196Sbapt			    pwd->pw_dir);
1712286196Sbapt		}
1713286196Sbapt	}
1714286196Sbapt
1715286196Sbapt	if (passwd && conf.fd == -1) {
1716286196Sbapt		lc = login_getpwclass(pwd);
1717286196Sbapt		if (lc == NULL || login_setcryptfmt(lc, "sha512", NULL) == NULL)
1718286196Sbapt			warn("setting crypt(3) format");
1719286196Sbapt		login_close(lc);
1720286775Sbapt		cnf->default_password = boolean_val(passwd,
1721286775Sbapt		    cnf->default_password);
1722286196Sbapt		pwd->pw_passwd = pw_password(cnf, pwd->pw_name, dryrun);
1723286196Sbapt		edited = true;
1724286196Sbapt	}
1725286196Sbapt
1726286196Sbapt	if (gecos && strcmp(pwd->pw_gecos, gecos) != 0) {
1727286196Sbapt		pwd->pw_gecos = gecos;
1728286196Sbapt		edited = true;
1729286196Sbapt	}
1730286196Sbapt
1731286196Sbapt	if (fd != -1)
1732286196Sbapt		edited = pw_set_passwd(pwd, fd, precrypted, true);
1733286196Sbapt
1734286196Sbapt	if (dryrun)
1735286196Sbapt		return (print_user(pwd, pretty, false));
1736286196Sbapt
1737286196Sbapt	if (edited) /* Only updated this if required */
1738286196Sbapt		perform_chgpwent(name, pwd, nis ? nispasswd : NULL);
1739286196Sbapt	/* Now perform the needed changes concern groups */
1740286196Sbapt	if (groups != NULL) {
1741286196Sbapt		/* Delete User from groups using old name */
1742286196Sbapt		SETGRENT();
1743286196Sbapt		while ((grp = GETGRENT()) != NULL) {
1744286196Sbapt			if (grp->gr_mem == NULL)
1745286196Sbapt				continue;
1746286196Sbapt			for (i = 0; grp->gr_mem[i] != NULL; i++) {
1747286196Sbapt				if (strcmp(grp->gr_mem[i] , name) != 0)
1748286196Sbapt					continue;
1749286196Sbapt				for (j = i; grp->gr_mem[j] != NULL ; j++)
1750286196Sbapt					grp->gr_mem[j] = grp->gr_mem[j+1];
1751286196Sbapt				chggrent(grp->gr_name, grp);
1752286196Sbapt				break;
1753286196Sbapt			}
1754286196Sbapt		}
1755286196Sbapt		ENDGRENT();
1756286196Sbapt		/* Add the user to the needed groups */
1757286196Sbapt		for (i = 0; i < groups->sl_cur; i++) {
1758286196Sbapt			grp = GETGRNAM(groups->sl_str[i]);
1759286196Sbapt			grp = gr_add(grp, pwd->pw_name);
1760286196Sbapt			if (grp == NULL)
1761286196Sbapt				continue;
1762286196Sbapt			chggrent(grp->gr_name, grp);
1763286196Sbapt			free(grp);
1764286196Sbapt		}
1765286196Sbapt	}
1766286196Sbapt	/* In case of rename we need to walk over the different groups */
1767286196Sbapt	if (newname) {
1768286196Sbapt		SETGRENT();
1769286196Sbapt		while ((grp = GETGRENT()) != NULL) {
1770286196Sbapt			if (grp->gr_mem == NULL)
1771286196Sbapt				continue;
1772286196Sbapt			for (i = 0; grp->gr_mem[i] != NULL; i++) {
1773286196Sbapt				if (strcmp(grp->gr_mem[i], name) != 0)
1774286196Sbapt					continue;
1775286196Sbapt				grp->gr_mem[i] = newname;
1776286196Sbapt				chggrent(grp->gr_name, grp);
1777286196Sbapt				break;
1778286196Sbapt			}
1779286196Sbapt		}
1780286196Sbapt	}
1781286196Sbapt
1782286196Sbapt	/* go get a current version of pwd */
1783286196Sbapt	if (newname)
1784286196Sbapt		name = newname;
1785286196Sbapt	pwd = GETPWNAM(name);
1786286196Sbapt	if (pwd == NULL)
1787286196Sbapt		errx(EX_NOUSER, "user '%s' disappeared during update", name);
1788286196Sbapt	grp = GETGRGID(pwd->pw_gid);
1789286196Sbapt	pw_log(cnf, M_UPDATE, W_USER, "%s(%ju):%s(%ju):%s:%s:%s",
1790286196Sbapt	    pwd->pw_name, (uintmax_t)pwd->pw_uid,
1791286196Sbapt	    grp ? grp->gr_name : "unknown",
1792286196Sbapt	    (uintmax_t)(grp ? grp->gr_gid : (uid_t)-1),
1793286196Sbapt	    pwd->pw_gecos, pwd->pw_dir, pwd->pw_shell);
1794286196Sbapt
1795285433Sbapt	/*
1796286196Sbapt	 * Let's create and populate the user's home directory. Note
1797286196Sbapt	 * that this also `works' for editing users if -m is used, but
1798286196Sbapt	 * existing files will *not* be overwritten.
1799285433Sbapt	 */
1800286196Sbapt	if (PWALTDIR() != PWF_ALT && docreatehome && pwd->pw_dir &&
1801286196Sbapt	    *pwd->pw_dir == '/' && pwd->pw_dir[1]) {
1802286196Sbapt		if (!skel)
1803286196Sbapt			skel = cnf->dotdir;
1804286196Sbapt		if (homemode == 0)
1805286196Sbapt			homemode = cnf->homemode;
1806286196Sbapt		create_and_populate_homedir(cnf, pwd, skel, homemode, true);
1807286196Sbapt	}
1808286196Sbapt
1809286196Sbapt	if (nis && nis_update() == 0)
1810286196Sbapt		pw_log(cnf, M_UPDATE, W_USER, "NIS maps updated");
1811286196Sbapt
1812286196Sbapt	return (EXIT_SUCCESS);
181320747Sdavidn}
1814