pw_user.c revision 284111
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 284111 2015-06-07 11:26:28Z bapt $";
3130259Scharnier#endif /* not lint */
3230259Scharnier
3330259Scharnier#include <ctype.h>
3430259Scharnier#include <err.h>
3520253Sjoerg#include <fcntl.h>
3620253Sjoerg#include <sys/param.h>
3720253Sjoerg#include <dirent.h>
3830259Scharnier#include <paths.h>
3920253Sjoerg#include <termios.h>
4020555Sdavidn#include <sys/types.h>
4120555Sdavidn#include <sys/time.h>
4220555Sdavidn#include <sys/resource.h>
4364918Sgreen#include <login_cap.h>
44242349Sbapt#include <pwd.h>
45242349Sbapt#include <grp.h>
46242349Sbapt#include <libutil.h>
4720253Sjoerg#include "pw.h"
4820253Sjoerg#include "bitmap.h"
4920253Sjoerg
5023318Sache#define LOGNAMESIZE (MAXLOGNAME-1)
5122394Sdavidn
5252512Sdavidnstatic		char locked_str[] = "*LOCKED*";
5324214Sache
54284111Sbaptstatic int	delete_user(struct userconf *cnf, struct passwd *pwd,
55284111Sbapt		    struct carg *a_name, int delete, int mode);
5644386Sdavidnstatic int      print_user(struct passwd * pwd, int pretty, int v7);
5720253Sjoergstatic uid_t    pw_uidpolicy(struct userconf * cnf, struct cargs * args);
5820253Sjoergstatic uid_t    pw_gidpolicy(struct userconf * cnf, struct cargs * args, char *nam, gid_t prefer);
5920253Sjoergstatic time_t   pw_pwdpolicy(struct userconf * cnf, struct cargs * args);
6020253Sjoergstatic time_t   pw_exppolicy(struct userconf * cnf, struct cargs * args);
6120253Sjoergstatic char    *pw_homepolicy(struct userconf * cnf, struct cargs * args, char const * user);
6220253Sjoergstatic char    *pw_shellpolicy(struct userconf * cnf, struct cargs * args, char *newshell);
6320253Sjoergstatic char    *pw_password(struct userconf * cnf, struct cargs * args, char const * user);
6420253Sjoergstatic char    *shell_path(char const * path, char *shells[], char *sh);
6520253Sjoergstatic void     rmat(uid_t uid);
6685145Sachestatic void     rmopie(char const * name);
6720253Sjoerg
68283961Sbaptstatic void
69283961Sbaptcreate_and_populate_homedir(int mode, struct cargs *args, struct passwd *pwd,
70283961Sbapt    struct userconf *cnf)
71283961Sbapt{
72283961Sbapt	char *homedir, *dotdir;
73283961Sbapt	struct carg	*arg;
74283961Sbapt
75283961Sbapt	homedir = dotdir = NULL;
76283961Sbapt
77283961Sbapt	if ((arg = getarg(args, 'R'))) {
78283961Sbapt		asprintf(&homedir, "%s/%s", arg->val, pwd->pw_dir);
79283961Sbapt		if (homedir == NULL)
80283961Sbapt			errx(EX_OSERR, "out of memory");
81283961Sbapt		asprintf(&dotdir, "%s/%s", arg->val, cnf->dotdir);
82283961Sbapt	}
83283961Sbapt
84283961Sbapt	copymkdir(homedir ? homedir : pwd->pw_dir, dotdir ? dotdir: cnf->dotdir,
85283961Sbapt	    cnf->homemode, pwd->pw_uid, pwd->pw_gid);
86283961Sbapt	pw_log(cnf, mode, W_USER, "%s(%u) home %s made", pwd->pw_name,
87283961Sbapt	    pwd->pw_uid, pwd->pw_dir);
88283961Sbapt}
89283961Sbapt
9020253Sjoerg/*-
9120253Sjoerg * -C config      configuration file
9220253Sjoerg * -q             quiet operation
9320253Sjoerg * -n name        login name
9420253Sjoerg * -u uid         user id
9520253Sjoerg * -c comment     user name/comment
9620253Sjoerg * -d directory   home directory
9720253Sjoerg * -e date        account expiry date
9820253Sjoerg * -p date        password expiry date
9920253Sjoerg * -g grp         primary group
10020253Sjoerg * -G grp1,grp2   additional groups
10120253Sjoerg * -m [ -k dir ]  create and set up home
10220253Sjoerg * -s shell       name of login shell
10320253Sjoerg * -o             duplicate uid ok
10420253Sjoerg * -L class       user class
10520253Sjoerg * -l name        new login name
10620253Sjoerg * -h fd          password filehandle
107124382Siedowse * -H fd          encrypted password filehandle
10820253Sjoerg * -F             force print or add
10920253Sjoerg *   Setting defaults:
11020253Sjoerg * -D             set user defaults
11120253Sjoerg * -b dir         default home root dir
11220253Sjoerg * -e period      default expiry period
11320253Sjoerg * -p period      default password change period
11420253Sjoerg * -g group       default group
11520253Sjoerg * -G             grp1,grp2.. default additional groups
11620253Sjoerg * -L class       default login class
11720253Sjoerg * -k dir         default home skeleton
11820253Sjoerg * -s shell       default shell
11920253Sjoerg * -w method      default password method
12020253Sjoerg */
12120253Sjoerg
12220253Sjoergint
12320253Sjoergpw_user(struct userconf * cnf, int mode, struct cargs * args)
12420253Sjoerg{
12552527Sdavidn	int	        rc, edited = 0;
12620253Sjoerg	char           *p = NULL;
12752512Sdavidn	char					 *passtmp;
12820253Sjoerg	struct carg    *a_name;
12920253Sjoerg	struct carg    *a_uid;
13020253Sjoerg	struct carg    *arg;
13120253Sjoerg	struct passwd  *pwd = NULL;
13220253Sjoerg	struct group   *grp;
13320253Sjoerg	struct stat     st;
13420747Sdavidn	char            line[_PASSWORD_LEN+1];
135283961Sbapt	char		path[MAXPATHLEN];
13682868Sdd	FILE	       *fp;
137167919Sle	char *dmode_c;
138167919Sle	void *set = NULL;
13920253Sjoerg
14020253Sjoerg	static struct passwd fakeuser =
14120253Sjoerg	{
14220253Sjoerg		NULL,
14320253Sjoerg		"*",
14420253Sjoerg		-1,
14520253Sjoerg		-1,
14620253Sjoerg		0,
14720253Sjoerg		"",
14820253Sjoerg		"User &",
14956000Sdavidn		"/nonexistent",
15020253Sjoerg		"/bin/sh",
15120253Sjoerg		0
15256000Sdavidn#if defined(__FreeBSD__)
15356000Sdavidn		,0
15456000Sdavidn#endif
15520253Sjoerg	};
15620253Sjoerg
15752512Sdavidn
15820253Sjoerg	/*
15920267Sjoerg	 * With M_NEXT, we only need to return the
16020267Sjoerg	 * next uid to stdout
16120267Sjoerg	 */
16220267Sjoerg	if (mode == M_NEXT)
16320267Sjoerg	{
16420267Sjoerg		uid_t next = pw_uidpolicy(cnf, args);
16520267Sjoerg		if (getarg(args, 'q'))
16620267Sjoerg			return next;
167283842Sbapt		printf("%u:", next);
16820267Sjoerg		pw_group(cnf, mode, args);
16920267Sjoerg		return EXIT_SUCCESS;
17020267Sjoerg	}
17120267Sjoerg
17220267Sjoerg	/*
17320253Sjoerg	 * We can do all of the common legwork here
17420253Sjoerg	 */
17520253Sjoerg
17620253Sjoerg	if ((arg = getarg(args, 'b')) != NULL) {
17720267Sjoerg		cnf->home = arg->val;
17820253Sjoerg	}
17921052Sdavidn
180167919Sle	if ((arg = getarg(args, 'M')) != NULL) {
181167919Sle		dmode_c = arg->val;
182167919Sle		if ((set = setmode(dmode_c)) == NULL)
183167919Sle			errx(EX_DATAERR, "invalid directory creation mode '%s'",
184167919Sle			    dmode_c);
185219408Sjkim		cnf->homemode = getmode(set, _DEF_DIRMODE);
186167919Sle		free(set);
187168044Sle	}
188167919Sle
18921052Sdavidn	/*
19021052Sdavidn	 * If we'll need to use it or we're updating it,
19121052Sdavidn	 * then create the base home directory if necessary
19221052Sdavidn	 */
193224535Sdelphij	if (arg != NULL || getarg(args, 'm') != NULL) {
19421052Sdavidn		int	l = strlen(cnf->home);
19521052Sdavidn
19621052Sdavidn		if (l > 1 && cnf->home[l-1] == '/')	/* Shave off any trailing path delimiter */
19721052Sdavidn			cnf->home[--l] = '\0';
19821052Sdavidn
19921052Sdavidn		if (l < 2 || *cnf->home != '/')		/* Check for absolute path name */
20030259Scharnier			errx(EX_DATAERR, "invalid base directory for home '%s'", cnf->home);
20121052Sdavidn
20221052Sdavidn		if (stat(cnf->home, &st) == -1) {
20321052Sdavidn			char	dbuf[MAXPATHLEN];
20421052Sdavidn
20521242Sdavidn			/*
20621242Sdavidn			 * This is a kludge especially for Joerg :)
20721242Sdavidn			 * If the home directory would be created in the root partition, then
20821242Sdavidn			 * we really create it under /usr which is likely to have more space.
20921242Sdavidn			 * But we create a symlink from cnf->home -> "/usr" -> cnf->home
21021242Sdavidn			 */
21121242Sdavidn			if (strchr(cnf->home+1, '/') == NULL) {
212282683Sbapt				snprintf(dbuf, MAXPATHLEN, "/usr%s", cnf->home);
213219408Sjkim				if (mkdir(dbuf, _DEF_DIRMODE) != -1 || errno == EEXIST) {
21421242Sdavidn					chown(dbuf, 0, 0);
215148584Spjd					/*
216148584Spjd					 * Skip first "/" and create symlink:
217148584Spjd					 * /home -> usr/home
218148584Spjd					 */
219148584Spjd					symlink(dbuf+1, cnf->home);
22021242Sdavidn				}
22121242Sdavidn				/* If this falls, fall back to old method */
22221242Sdavidn			}
223130633Srobert			strlcpy(dbuf, cnf->home, sizeof(dbuf));
224130633Srobert			p = dbuf;
22521242Sdavidn			if (stat(dbuf, &st) == -1) {
226252377Skientzle				while ((p = strchr(p + 1, '/')) != NULL) {
22721242Sdavidn					*p = '\0';
22821242Sdavidn					if (stat(dbuf, &st) == -1) {
229219408Sjkim						if (mkdir(dbuf, _DEF_DIRMODE) == -1)
23021242Sdavidn							goto direrr;
23121242Sdavidn						chown(dbuf, 0, 0);
23221242Sdavidn					} else if (!S_ISDIR(st.st_mode))
23330259Scharnier						errx(EX_OSFILE, "'%s' (root home parent) is not a directory", dbuf);
23421242Sdavidn					*p = '/';
23521242Sdavidn				}
23621052Sdavidn			}
23721242Sdavidn			if (stat(dbuf, &st) == -1) {
238219408Sjkim				if (mkdir(dbuf, _DEF_DIRMODE) == -1) {
23930259Scharnier				direrr:	err(EX_OSFILE, "mkdir '%s'", dbuf);
24021052Sdavidn				}
24121052Sdavidn				chown(dbuf, 0, 0);
24221052Sdavidn			}
24321052Sdavidn		} else if (!S_ISDIR(st.st_mode))
24430259Scharnier			errx(EX_OSFILE, "root home `%s' is not a directory", cnf->home);
24521052Sdavidn	}
24621052Sdavidn
24720253Sjoerg	if ((arg = getarg(args, 'e')) != NULL)
24820253Sjoerg		cnf->expire_days = atoi(arg->val);
24920253Sjoerg
25021330Sdavidn	if ((arg = getarg(args, 'y')) != NULL)
25121330Sdavidn		cnf->nispasswd = arg->val;
25221330Sdavidn
25320253Sjoerg	if ((arg = getarg(args, 'p')) != NULL && arg->val)
25420253Sjoerg		cnf->password_days = atoi(arg->val);
25520253Sjoerg
25620253Sjoerg	if ((arg = getarg(args, 'g')) != NULL) {
25763596Sdavidn		if (!*(p = arg->val))	/* Handle empty group list specially */
25863596Sdavidn			cnf->default_group = "";
25963596Sdavidn		else {
26063596Sdavidn			if ((grp = GETGRNAM(p)) == NULL) {
26163596Sdavidn				if (!isdigit((unsigned char)*p) || (grp = GETGRGID((gid_t) atoi(p))) == NULL)
26263596Sdavidn					errx(EX_NOUSER, "group `%s' does not exist", p);
26363596Sdavidn			}
26463596Sdavidn			cnf->default_group = newstr(grp->gr_name);
26520253Sjoerg		}
26620253Sjoerg	}
26720253Sjoerg	if ((arg = getarg(args, 'L')) != NULL)
268284110Sbapt		cnf->default_class = pw_checkname(arg->val, 0);
26920253Sjoerg
27020253Sjoerg	if ((arg = getarg(args, 'G')) != NULL && arg->val) {
27152527Sdavidn		int i = 0;
27220253Sjoerg
27320747Sdavidn		for (p = strtok(arg->val, ", \t"); p != NULL; p = strtok(NULL, ", \t")) {
27444229Sdavidn			if ((grp = GETGRNAM(p)) == NULL) {
27561957Sache				if (!isdigit((unsigned char)*p) || (grp = GETGRGID((gid_t) atoi(p))) == NULL)
27630259Scharnier					errx(EX_NOUSER, "group `%s' does not exist", p);
27720253Sjoerg			}
27820747Sdavidn			if (extendarray(&cnf->groups, &cnf->numgroups, i + 2) != -1)
27920747Sdavidn				cnf->groups[i++] = newstr(grp->gr_name);
28020253Sjoerg		}
28120747Sdavidn		while (i < cnf->numgroups)
28220253Sjoerg			cnf->groups[i++] = NULL;
28320253Sjoerg	}
28452527Sdavidn
28520253Sjoerg	if ((arg = getarg(args, 'k')) != NULL) {
28626088Sdavidn		if (stat(cnf->dotdir = arg->val, &st) == -1 || !S_ISDIR(st.st_mode))
28730259Scharnier			errx(EX_OSFILE, "skeleton `%s' is not a directory or does not exist", cnf->dotdir);
28820253Sjoerg	}
28952527Sdavidn
29020253Sjoerg	if ((arg = getarg(args, 's')) != NULL)
29120253Sjoerg		cnf->shell_default = arg->val;
29220253Sjoerg
29363600Sdavidn	if ((arg = getarg(args, 'w')) != NULL)
29463600Sdavidn		cnf->default_password = boolean_val(arg->val, cnf->default_password);
29520253Sjoerg	if (mode == M_ADD && getarg(args, 'D')) {
29620253Sjoerg		if (getarg(args, 'n') != NULL)
29730259Scharnier			errx(EX_DATAERR, "can't combine `-D' with `-n name'");
29820253Sjoerg		if ((arg = getarg(args, 'u')) != NULL && (p = strtok(arg->val, ", \t")) != NULL) {
29920253Sjoerg			if ((cnf->min_uid = (uid_t) atoi(p)) == 0)
30020253Sjoerg				cnf->min_uid = 1000;
30120253Sjoerg			if ((p = strtok(NULL, " ,\t")) == NULL || (cnf->max_uid = (uid_t) atoi(p)) < cnf->min_uid)
30220253Sjoerg				cnf->max_uid = 32000;
30320253Sjoerg		}
30420253Sjoerg		if ((arg = getarg(args, 'i')) != NULL && (p = strtok(arg->val, ", \t")) != NULL) {
30520253Sjoerg			if ((cnf->min_gid = (gid_t) atoi(p)) == 0)
30620253Sjoerg				cnf->min_gid = 1000;
30720253Sjoerg			if ((p = strtok(NULL, " ,\t")) == NULL || (cnf->max_gid = (gid_t) atoi(p)) < cnf->min_gid)
30820253Sjoerg				cnf->max_gid = 32000;
30920253Sjoerg		}
31020253Sjoerg
31120253Sjoerg		arg = getarg(args, 'C');
31220253Sjoerg		if (write_userconfig(arg ? arg->val : NULL))
31320267Sjoerg			return EXIT_SUCCESS;
314283814Sbapt		err(EX_IOERR, "config udpate");
31520253Sjoerg	}
31652527Sdavidn
31720253Sjoerg	if (mode == M_PRINT && getarg(args, 'a')) {
31820267Sjoerg		int             pretty = getarg(args, 'P') != NULL;
31944386Sdavidn		int		v7 = getarg(args, '7') != NULL;
32044229Sdavidn		SETPWENT();
32144229Sdavidn		while ((pwd = GETPWENT()) != NULL)
32244386Sdavidn			print_user(pwd, pretty, v7);
32344229Sdavidn		ENDPWENT();
32420267Sjoerg		return EXIT_SUCCESS;
32520253Sjoerg	}
32652527Sdavidn
32720253Sjoerg	if ((a_name = getarg(args, 'n')) != NULL)
328284110Sbapt		pwd = GETPWNAM(pw_checkname(a_name->val, 0));
32920253Sjoerg	a_uid = getarg(args, 'u');
33020253Sjoerg
33120253Sjoerg	if (a_uid == NULL) {
33220253Sjoerg		if (a_name == NULL)
33330259Scharnier			errx(EX_DATAERR, "user name or id required");
33420253Sjoerg
33520253Sjoerg		/*
33620253Sjoerg		 * Determine whether 'n' switch is name or uid - we don't
33720253Sjoerg		 * really don't really care which we have, but we need to
33820253Sjoerg		 * know.
33920253Sjoerg		 */
34043780Sdes		if (mode != M_ADD && pwd == NULL
341241108Sbapt		    && strspn(a_name->val, "0123456789") == strlen(a_name->val)
342241108Sbapt		    && *a_name->val) {
34320253Sjoerg			(a_uid = a_name)->ch = 'u';
34420253Sjoerg			a_name = NULL;
34520253Sjoerg		}
346273787Sbapt	} else {
347277764Sbapt		if (strspn(a_uid->val, "0123456789") != strlen(a_uid->val))
348273787Sbapt			errx(EX_USAGE, "-u expects a number");
34920253Sjoerg	}
35052527Sdavidn
35120253Sjoerg	/*
35220253Sjoerg	 * Update, delete & print require that the user exists
35320253Sjoerg	 */
35452512Sdavidn	if (mode == M_UPDATE || mode == M_DELETE ||
35552512Sdavidn	    mode == M_PRINT  || mode == M_LOCK   || mode == M_UNLOCK) {
35652527Sdavidn
35720253Sjoerg		if (a_name == NULL && pwd == NULL)	/* Try harder */
35844229Sdavidn			pwd = GETPWUID(atoi(a_uid->val));
35920253Sjoerg
36020253Sjoerg		if (pwd == NULL) {
36120253Sjoerg			if (mode == M_PRINT && getarg(args, 'F')) {
36220253Sjoerg				fakeuser.pw_name = a_name ? a_name->val : "nouser";
363283841Sbapt				fakeuser.pw_uid = a_uid ? (uid_t) atol(a_uid->val) : (uid_t) -1;
36444386Sdavidn				return print_user(&fakeuser,
36544386Sdavidn						  getarg(args, 'P') != NULL,
36644386Sdavidn						  getarg(args, '7') != NULL);
36720253Sjoerg			}
36820253Sjoerg			if (a_name == NULL)
36930259Scharnier				errx(EX_NOUSER, "no such uid `%s'", a_uid->val);
37030259Scharnier			errx(EX_NOUSER, "no such user `%s'", a_name->val);
37120253Sjoerg		}
37252527Sdavidn
37320253Sjoerg		if (a_name == NULL)	/* May be needed later */
37420253Sjoerg			a_name = addarg(args, 'n', newstr(pwd->pw_name));
37520253Sjoerg
37620253Sjoerg		/*
37752512Sdavidn		 * The M_LOCK and M_UNLOCK functions simply add or remove
37852512Sdavidn		 * a "*LOCKED*" prefix from in front of the password to
37952512Sdavidn		 * prevent it decoding correctly, and therefore prevents
38052512Sdavidn		 * access. Of course, this only prevents access via
38152512Sdavidn		 * password authentication (not ssh, kerberos or any
38252512Sdavidn		 * other method that does not use the UNIX password) but
38352512Sdavidn		 * that is a known limitation.
38452512Sdavidn		 */
38552512Sdavidn
38652512Sdavidn		if (mode == M_LOCK) {
38752512Sdavidn			if (strncmp(pwd->pw_passwd, locked_str, sizeof(locked_str)-1) == 0)
38852512Sdavidn				errx(EX_DATAERR, "user '%s' is already locked", pwd->pw_name);
389282685Sbapt			asprintf(&passtmp, "%s%s", locked_str, pwd->pw_passwd);
39052512Sdavidn			if (passtmp == NULL)	/* disaster */
39152512Sdavidn				errx(EX_UNAVAILABLE, "out of memory");
39252512Sdavidn			pwd->pw_passwd = passtmp;
39352527Sdavidn			edited = 1;
39452512Sdavidn		} else if (mode == M_UNLOCK) {
39552512Sdavidn			if (strncmp(pwd->pw_passwd, locked_str, sizeof(locked_str)-1) != 0)
39652512Sdavidn				errx(EX_DATAERR, "user '%s' is not locked", pwd->pw_name);
39752512Sdavidn			pwd->pw_passwd += sizeof(locked_str)-1;
39852527Sdavidn			edited = 1;
399284111Sbapt		} else if (mode == M_DELETE)
400284111Sbapt			return (delete_user(cnf, pwd, a_name,
401284111Sbapt				    getarg(args, 'r') != NULL, mode));
402284111Sbapt		else if (mode == M_PRINT)
40344386Sdavidn			return print_user(pwd,
40444386Sdavidn					  getarg(args, 'P') != NULL,
40544386Sdavidn					  getarg(args, '7') != NULL);
40620253Sjoerg
40720253Sjoerg		/*
40820253Sjoerg		 * The rest is edit code
40920253Sjoerg		 */
41020253Sjoerg		if ((arg = getarg(args, 'l')) != NULL) {
41120253Sjoerg			if (strcmp(pwd->pw_name, "root") == 0)
41230259Scharnier				errx(EX_DATAERR, "can't rename `root' account");
413284110Sbapt			pwd->pw_name = pw_checkname(arg->val, 0);
41452527Sdavidn			edited = 1;
41520253Sjoerg		}
41652527Sdavidn
41761957Sache		if ((arg = getarg(args, 'u')) != NULL && isdigit((unsigned char)*arg->val)) {
41820253Sjoerg			pwd->pw_uid = (uid_t) atol(arg->val);
41952527Sdavidn			edited = 1;
42020253Sjoerg			if (pwd->pw_uid != 0 && strcmp(pwd->pw_name, "root") == 0)
42130259Scharnier				errx(EX_DATAERR, "can't change uid of `root' account");
42220253Sjoerg			if (pwd->pw_uid == 0 && strcmp(pwd->pw_name, "root") != 0)
42330259Scharnier				warnx("WARNING: account `%s' will have a uid of 0 (superuser access!)", pwd->pw_name);
42420253Sjoerg		}
42520253Sjoerg
42652527Sdavidn		if ((arg = getarg(args, 'g')) != NULL && pwd->pw_uid != 0) {	/* Already checked this */
42752527Sdavidn			gid_t newgid = (gid_t) GETGRNAM(cnf->default_group)->gr_gid;
42852527Sdavidn			if (newgid != pwd->pw_gid) {
42952527Sdavidn				edited = 1;
43061762Sdavidn				pwd->pw_gid = newgid;
43152527Sdavidn			}
43252527Sdavidn		}
43352527Sdavidn
43420253Sjoerg		if ((arg = getarg(args, 'p')) != NULL) {
43552527Sdavidn			if (*arg->val == '\0' || strcmp(arg->val, "0") == 0) {
43652527Sdavidn				if (pwd->pw_change != 0) {
43752527Sdavidn					pwd->pw_change = 0;
43852527Sdavidn					edited = 1;
43952527Sdavidn				}
44052527Sdavidn			}
44120253Sjoerg			else {
44220253Sjoerg				time_t          now = time(NULL);
44320253Sjoerg				time_t          expire = parse_date(now, arg->val);
44420253Sjoerg
44552527Sdavidn				if (pwd->pw_change != expire) {
44652527Sdavidn					pwd->pw_change = expire;
44752527Sdavidn					edited = 1;
44852527Sdavidn				}
44920253Sjoerg			}
45020253Sjoerg		}
45152527Sdavidn
45220267Sjoerg		if ((arg = getarg(args, 'e')) != NULL) {
45352527Sdavidn			if (*arg->val == '\0' || strcmp(arg->val, "0") == 0) {
45452527Sdavidn				if (pwd->pw_expire != 0) {
45552527Sdavidn					pwd->pw_expire = 0;
45652527Sdavidn					edited = 1;
45752527Sdavidn				}
45852527Sdavidn			}
45920253Sjoerg			else {
46020253Sjoerg				time_t          now = time(NULL);
46120253Sjoerg				time_t          expire = parse_date(now, arg->val);
46220253Sjoerg
46352527Sdavidn				if (pwd->pw_expire != expire) {
46452527Sdavidn					pwd->pw_expire = expire;
46552527Sdavidn					edited = 1;
46652527Sdavidn				}
46720253Sjoerg			}
46820253Sjoerg		}
46920253Sjoerg
47052527Sdavidn		if ((arg = getarg(args, 's')) != NULL) {
47152527Sdavidn			char *shell = shell_path(cnf->shelldir, cnf->shells, arg->val);
47252527Sdavidn			if (shell == NULL)
47352527Sdavidn				shell = "";
47452527Sdavidn			if (strcmp(shell, pwd->pw_shell) != 0) {
47552527Sdavidn				pwd->pw_shell = shell;
47652527Sdavidn				edited = 1;
47752527Sdavidn			}
47852527Sdavidn		}
47920253Sjoerg
48052527Sdavidn		if (getarg(args, 'L')) {
48152527Sdavidn			if (cnf->default_class == NULL)
48252527Sdavidn				cnf->default_class = "";
48352527Sdavidn			if (strcmp(pwd->pw_class, cnf->default_class) != 0) {
48452527Sdavidn				pwd->pw_class = cnf->default_class;
48552527Sdavidn				edited = 1;
48652527Sdavidn			}
48752527Sdavidn		}
48852527Sdavidn
48920747Sdavidn		if ((arg  = getarg(args, 'd')) != NULL) {
490130629Srobert			if (strcmp(pwd->pw_dir, arg->val))
491130629Srobert				edited = 1;
49220747Sdavidn			if (stat(pwd->pw_dir = arg->val, &st) == -1) {
49320747Sdavidn				if (getarg(args, 'm') == NULL && strcmp(pwd->pw_dir, "/nonexistent") != 0)
49430259Scharnier				  warnx("WARNING: home `%s' does not exist", pwd->pw_dir);
49520747Sdavidn			} else if (!S_ISDIR(st.st_mode))
49630259Scharnier				warnx("WARNING: home `%s' is not a directory", pwd->pw_dir);
49720747Sdavidn		}
49820747Sdavidn
499124382Siedowse		if ((arg = getarg(args, 'w')) != NULL &&
500124382Siedowse		    getarg(args, 'h') == NULL && getarg(args, 'H') == NULL) {
50164918Sgreen			login_cap_t *lc;
50264918Sgreen
50364918Sgreen			lc = login_getpwclass(pwd);
50464918Sgreen			if (lc == NULL ||
505252688Sdes			    login_setcryptfmt(lc, "sha512", NULL) == NULL)
50664918Sgreen				warn("setting crypt(3) format");
50764918Sgreen			login_close(lc);
50820267Sjoerg			pwd->pw_passwd = pw_password(cnf, args, pwd->pw_name);
50952527Sdavidn			edited = 1;
51052527Sdavidn		}
51120267Sjoerg
51220253Sjoerg	} else {
51364918Sgreen		login_cap_t *lc;
51452527Sdavidn
51552527Sdavidn		/*
51652527Sdavidn		 * Add code
51752527Sdavidn		 */
51852527Sdavidn
51920253Sjoerg		if (a_name == NULL)	/* Required */
52030259Scharnier			errx(EX_DATAERR, "login name required");
52144229Sdavidn		else if ((pwd = GETPWNAM(a_name->val)) != NULL)	/* Exists */
52230259Scharnier			errx(EX_DATAERR, "login name `%s' already exists", a_name->val);
52320253Sjoerg
52420253Sjoerg		/*
52520253Sjoerg		 * Now, set up defaults for a new user
52620253Sjoerg		 */
52720253Sjoerg		pwd = &fakeuser;
52820253Sjoerg		pwd->pw_name = a_name->val;
52920253Sjoerg		pwd->pw_class = cnf->default_class ? cnf->default_class : "";
53020253Sjoerg		pwd->pw_uid = pw_uidpolicy(cnf, args);
53120253Sjoerg		pwd->pw_gid = pw_gidpolicy(cnf, args, pwd->pw_name, (gid_t) pwd->pw_uid);
53220253Sjoerg		pwd->pw_change = pw_pwdpolicy(cnf, args);
53320253Sjoerg		pwd->pw_expire = pw_exppolicy(cnf, args);
53420253Sjoerg		pwd->pw_dir = pw_homepolicy(cnf, args, pwd->pw_name);
53520253Sjoerg		pwd->pw_shell = pw_shellpolicy(cnf, args, NULL);
53664918Sgreen		lc = login_getpwclass(pwd);
537272833Sdes		if (lc == NULL || login_setcryptfmt(lc, "sha512", NULL) == NULL)
53864918Sgreen			warn("setting crypt(3) format");
53964918Sgreen		login_close(lc);
54064918Sgreen		pwd->pw_passwd = pw_password(cnf, args, pwd->pw_name);
54152527Sdavidn		edited = 1;
54220253Sjoerg
54320253Sjoerg		if (pwd->pw_uid == 0 && strcmp(pwd->pw_name, "root") != 0)
54430259Scharnier			warnx("WARNING: new account `%s' has a uid of 0 (superuser access!)", pwd->pw_name);
54520253Sjoerg	}
54620253Sjoerg
54720253Sjoerg	/*
54820253Sjoerg	 * Shared add/edit code
54920253Sjoerg	 */
55052527Sdavidn	if ((arg = getarg(args, 'c')) != NULL) {
551284110Sbapt		char	*gecos = pw_checkname(arg->val, 1);
55252527Sdavidn		if (strcmp(pwd->pw_gecos, gecos) != 0) {
55352527Sdavidn			pwd->pw_gecos = gecos;
55452527Sdavidn			edited = 1;
55552527Sdavidn		}
55652527Sdavidn	}
55720253Sjoerg
558124382Siedowse	if ((arg = getarg(args, 'h')) != NULL ||
559124382Siedowse	    (arg = getarg(args, 'H')) != NULL) {
56063572Sdavidn		if (strcmp(arg->val, "-") == 0) {
56163572Sdavidn			if (!pwd->pw_passwd || *pwd->pw_passwd != '*') {
56263572Sdavidn				pwd->pw_passwd = "*";	/* No access */
56363572Sdavidn				edited = 1;
56463572Sdavidn			}
56563572Sdavidn		} else {
56620253Sjoerg			int             fd = atoi(arg->val);
567124382Siedowse			int		precrypt = (arg->ch == 'H');
56820253Sjoerg			int             b;
56920253Sjoerg			int             istty = isatty(fd);
57020253Sjoerg			struct termios  t;
57164918Sgreen			login_cap_t	*lc;
57220253Sjoerg
57320253Sjoerg			if (istty) {
57420253Sjoerg				if (tcgetattr(fd, &t) == -1)
57520253Sjoerg					istty = 0;
57620253Sjoerg				else {
57720253Sjoerg					struct termios  n = t;
57820253Sjoerg
57920253Sjoerg					/* Disable echo */
58020253Sjoerg					n.c_lflag &= ~(ECHO);
58120253Sjoerg					tcsetattr(fd, TCSANOW, &n);
582124382Siedowse					printf("%s%spassword for user %s:",
583124382Siedowse					     (mode == M_UPDATE) ? "new " : "",
584124382Siedowse					     precrypt ? "encrypted " : "",
585124382Siedowse					     pwd->pw_name);
58620253Sjoerg					fflush(stdout);
58720253Sjoerg				}
58820253Sjoerg			}
58920253Sjoerg			b = read(fd, line, sizeof(line) - 1);
59020253Sjoerg			if (istty) {	/* Restore state */
59120253Sjoerg				tcsetattr(fd, TCSANOW, &t);
59220253Sjoerg				fputc('\n', stdout);
59320253Sjoerg				fflush(stdout);
59420253Sjoerg			}
595283814Sbapt			if (b < 0)
596283814Sbapt				err(EX_IOERR, "-%c file descriptor",
597283814Sbapt				    precrypt ? 'H' : 'h');
59820253Sjoerg			line[b] = '\0';
599168045Sle			if ((p = strpbrk(line, "\r\n")) != NULL)
60020253Sjoerg				*p = '\0';
60120253Sjoerg			if (!*line)
60230259Scharnier				errx(EX_DATAERR, "empty password read on file descriptor %d", fd);
603124382Siedowse			if (precrypt) {
604124382Siedowse				if (strchr(line, ':') != NULL)
605124382Siedowse					return EX_DATAERR;
606124382Siedowse				pwd->pw_passwd = line;
607124382Siedowse			} else {
608124382Siedowse				lc = login_getpwclass(pwd);
609124382Siedowse				if (lc == NULL ||
610272833Sdes				    login_setcryptfmt(lc, "sha512", NULL) == NULL)
611124382Siedowse					warn("setting crypt(3) format");
612124382Siedowse				login_close(lc);
613124382Siedowse				pwd->pw_passwd = pw_pwcrypt(line);
614124382Siedowse			}
61552527Sdavidn			edited = 1;
61620253Sjoerg		}
61720253Sjoerg	}
61820267Sjoerg
61920267Sjoerg	/*
62020267Sjoerg	 * Special case: -N only displays & exits
62120267Sjoerg	 */
62220267Sjoerg	if (getarg(args, 'N') != NULL)
62344386Sdavidn		return print_user(pwd,
62444386Sdavidn				  getarg(args, 'P') != NULL,
62544386Sdavidn				  getarg(args, '7') != NULL);
62620267Sjoerg
62721330Sdavidn	if (mode == M_ADD) {
62852527Sdavidn		edited = 1;	/* Always */
62952502Sdavidn		rc = addpwent(pwd);
630283814Sbapt		if (rc == -1)
631283814Sbapt			errx(EX_IOERR, "user '%s' already exists",
632283814Sbapt			    pwd->pw_name);
633283814Sbapt		else if (rc != 0)
634283814Sbapt			err(EX_IOERR, "passwd file update");
63552502Sdavidn		if (cnf->nispasswd && *cnf->nispasswd=='/') {
63652502Sdavidn			rc = addnispwent(cnf->nispasswd, pwd);
63752502Sdavidn			if (rc == -1)
63852502Sdavidn				warnx("User '%s' already exists in NIS passwd", pwd->pw_name);
63952502Sdavidn			else
64056000Sdavidn				warn("NIS passwd update");
64152502Sdavidn			/* NOTE: we treat NIS-only update errors as non-fatal */
64252502Sdavidn		}
64352512Sdavidn	} else if (mode == M_UPDATE || mode == M_LOCK || mode == M_UNLOCK) {
64452527Sdavidn		if (edited) {	/* Only updated this if required */
64552527Sdavidn			rc = chgpwent(a_name->val, pwd);
646283814Sbapt			if (rc == -1)
647283814Sbapt				errx(EX_IOERR, "user '%s' does not exist (NIS?)", pwd->pw_name);
648283814Sbapt			else if (rc != 0)
649283814Sbapt				err(EX_IOERR, "passwd file update");
65052527Sdavidn			if ( cnf->nispasswd && *cnf->nispasswd=='/') {
65152527Sdavidn				rc = chgnispwent(cnf->nispasswd, a_name->val, pwd);
65252527Sdavidn				if (rc == -1)
65352527Sdavidn					warn("User '%s' not found in NIS passwd", pwd->pw_name);
65452527Sdavidn				else
65556000Sdavidn					warn("NIS passwd update");
65652527Sdavidn				/* NOTE: NIS-only update errors are not fatal */
65752527Sdavidn			}
65852502Sdavidn		}
65921330Sdavidn	}
66021330Sdavidn
66120253Sjoerg	/*
66220253Sjoerg	 * Ok, user is created or changed - now edit group file
66320253Sjoerg	 */
66420253Sjoerg
665242349Sbapt	if (mode == M_ADD || getarg(args, 'G') != NULL) {
666273779Sbapt		int i, j;
667273779Sbapt		/* First remove the user from all group */
668273779Sbapt		SETGRENT();
669273779Sbapt		while ((grp = GETGRENT()) != NULL) {
670273779Sbapt			char group[MAXLOGNAME];
671273779Sbapt			if (grp->gr_mem == NULL)
672273779Sbapt				continue;
673273779Sbapt			for (i = 0; grp->gr_mem[i] != NULL; i++) {
674273779Sbapt				if (strcmp(grp->gr_mem[i] , pwd->pw_name) != 0)
675273779Sbapt					continue;
676273779Sbapt				for (j = i; grp->gr_mem[j] != NULL ; j++)
677273779Sbapt					grp->gr_mem[j] = grp->gr_mem[j+1];
678273779Sbapt				strlcpy(group, grp->gr_name, MAXLOGNAME);
679273779Sbapt				chggrent(group, grp);
680273779Sbapt			}
681273779Sbapt		}
682273779Sbapt		ENDGRENT();
683273779Sbapt
684273779Sbapt		/* now add to group where needed */
685242349Sbapt		for (i = 0; cnf->groups[i] != NULL; i++) {
686242349Sbapt			grp = GETGRNAM(cnf->groups[i]);
687244737Sbapt			grp = gr_add(grp, pwd->pw_name);
688244737Sbapt			/*
689244737Sbapt			 * grp can only be NULL in 2 cases:
690244737Sbapt			 * - the new member is already a member
691244737Sbapt			 * - a problem with memory occurs
692244737Sbapt			 * in both cases we want to skip now.
693244737Sbapt			 */
694244737Sbapt			if (grp == NULL)
695242349Sbapt				continue;
696242349Sbapt			chggrent(cnf->groups[i], grp);
697245114Smjg			free(grp);
698242349Sbapt		}
699242349Sbapt	}
700242349Sbapt
701242349Sbapt
70261759Sdavidn	/* go get a current version of pwd */
70361759Sdavidn	pwd = GETPWNAM(a_name->val);
70461759Sdavidn	if (pwd == NULL) {
70561759Sdavidn		/* This will fail when we rename, so special case that */
70661759Sdavidn		if (mode == M_UPDATE && (arg = getarg(args, 'l')) != NULL) {
70761759Sdavidn			a_name->val = arg->val;		/* update new name */
70861759Sdavidn			pwd = GETPWNAM(a_name->val);	/* refetch renamed rec */
70961759Sdavidn		}
71061759Sdavidn	}
71161759Sdavidn	if (pwd == NULL)	/* can't go on without this */
71230259Scharnier		errx(EX_NOUSER, "user '%s' disappeared during update", a_name->val);
71320253Sjoerg
71444229Sdavidn	grp = GETGRGID(pwd->pw_gid);
715283842Sbapt	pw_log(cnf, mode, W_USER, "%s(%u):%s(%u):%s:%s:%s",
716283842Sbapt	       pwd->pw_name, pwd->pw_uid,
717283842Sbapt	    grp ? grp->gr_name : "unknown", (grp ? grp->gr_gid : (uid_t)-1),
71820253Sjoerg	       pwd->pw_gecos, pwd->pw_dir, pwd->pw_shell);
71920253Sjoerg
72020253Sjoerg	/*
72120253Sjoerg	 * If adding, let's touch and chown the user's mail file. This is not
72220253Sjoerg	 * strictly necessary under BSD with a 0755 maildir but it also
72320253Sjoerg	 * doesn't hurt anything to create the empty mailfile
72420253Sjoerg	 */
72520253Sjoerg	if (mode == M_ADD) {
726283961Sbapt		if (PWALTDIR() != PWF_ALT) {
727283961Sbapt			arg = getarg(args, 'R');
728283961Sbapt			snprintf(path, sizeof(path), "%s%s/%s",
729283961Sbapt			    arg ? arg->val : "", _PATH_MAILDIR, pwd->pw_name);
730283961Sbapt			close(open(path, O_RDWR | O_CREAT, 0600));	/* Preserve contents &
73144229Sdavidn									 * mtime */
732283961Sbapt			chown(path, pwd->pw_uid, pwd->pw_gid);
73320253Sjoerg		}
73420253Sjoerg	}
73552527Sdavidn
73620253Sjoerg	/*
73782868Sdd	 * Let's create and populate the user's home directory. Note
73820253Sjoerg	 * that this also `works' for editing users if -m is used, but
73920253Sjoerg	 * existing files will *not* be overwritten.
74020253Sjoerg	 */
741283961Sbapt	if (PWALTDIR() != PWF_ALT && getarg(args, 'm') != NULL && pwd->pw_dir &&
742283961Sbapt	    *pwd->pw_dir == '/' && pwd->pw_dir[1])
743283961Sbapt		create_and_populate_homedir(mode, args, pwd, cnf);
74452527Sdavidn
74582868Sdd	/*
74682868Sdd	 * Finally, send mail to the new user as well, if we are asked to
74782868Sdd	 */
74882868Sdd	if (mode == M_ADD && !PWALTDIR() && cnf->newmail && *cnf->newmail && (fp = fopen(cnf->newmail, "r")) != NULL) {
74982868Sdd		FILE           *pfp = popen(_PATH_SENDMAIL " -t", "w");
75082868Sdd
75182868Sdd		if (pfp == NULL)
75282868Sdd			warn("sendmail");
75382868Sdd		else {
75482868Sdd			fprintf(pfp, "From: root\n" "To: %s\n" "Subject: Welcome!\n\n", pwd->pw_name);
75582868Sdd			while (fgets(line, sizeof(line), fp) != NULL) {
75682868Sdd				/* Do substitutions? */
75782868Sdd				fputs(line, pfp);
75882868Sdd			}
75982868Sdd			pclose(pfp);
760283842Sbapt			pw_log(cnf, mode, W_USER, "%s(%u) new user mail sent",
761283842Sbapt			    pwd->pw_name, pwd->pw_uid);
76282868Sdd		}
76382868Sdd		fclose(fp);
76482868Sdd	}
76582868Sdd
76620267Sjoerg	return EXIT_SUCCESS;
76720253Sjoerg}
76820253Sjoerg
76920253Sjoerg
77020253Sjoergstatic          uid_t
77120253Sjoergpw_uidpolicy(struct userconf * cnf, struct cargs * args)
77220253Sjoerg{
77320253Sjoerg	struct passwd  *pwd;
77420253Sjoerg	uid_t           uid = (uid_t) - 1;
77520253Sjoerg	struct carg    *a_uid = getarg(args, 'u');
77620253Sjoerg
77720253Sjoerg	/*
77820253Sjoerg	 * Check the given uid, if any
77920253Sjoerg	 */
78020253Sjoerg	if (a_uid != NULL) {
78120253Sjoerg		uid = (uid_t) atol(a_uid->val);
78220253Sjoerg
78344229Sdavidn		if ((pwd = GETPWUID(uid)) != NULL && getarg(args, 'o') == NULL)
784283842Sbapt			errx(EX_DATAERR, "uid `%u' has already been allocated", pwd->pw_uid);
78520253Sjoerg	} else {
78620253Sjoerg		struct bitmap   bm;
78720253Sjoerg
78820253Sjoerg		/*
78920253Sjoerg		 * We need to allocate the next available uid under one of
79020253Sjoerg		 * two policies a) Grab the first unused uid b) Grab the
79120253Sjoerg		 * highest possible unused uid
79220253Sjoerg		 */
79320253Sjoerg		if (cnf->min_uid >= cnf->max_uid) {	/* Sanity
79420253Sjoerg							 * claus^H^H^H^Hheck */
79520253Sjoerg			cnf->min_uid = 1000;
79620253Sjoerg			cnf->max_uid = 32000;
79720253Sjoerg		}
79820253Sjoerg		bm = bm_alloc(cnf->max_uid - cnf->min_uid + 1);
79920253Sjoerg
80020253Sjoerg		/*
80120253Sjoerg		 * Now, let's fill the bitmap from the password file
80220253Sjoerg		 */
80344229Sdavidn		SETPWENT();
80444229Sdavidn		while ((pwd = GETPWENT()) != NULL)
80544229Sdavidn			if (pwd->pw_uid >= (uid_t) cnf->min_uid && pwd->pw_uid <= (uid_t) cnf->max_uid)
80620253Sjoerg				bm_setbit(&bm, pwd->pw_uid - cnf->min_uid);
80744229Sdavidn		ENDPWENT();
80820253Sjoerg
80920253Sjoerg		/*
81020253Sjoerg		 * Then apply the policy, with fallback to reuse if necessary
81120253Sjoerg		 */
81220253Sjoerg		if (cnf->reuse_uids || (uid = (uid_t) (bm_lastset(&bm) + cnf->min_uid + 1)) > cnf->max_uid)
81320253Sjoerg			uid = (uid_t) (bm_firstunset(&bm) + cnf->min_uid);
81420253Sjoerg
81520253Sjoerg		/*
81620253Sjoerg		 * Another sanity check
81720253Sjoerg		 */
81820253Sjoerg		if (uid < cnf->min_uid || uid > cnf->max_uid)
81930259Scharnier			errx(EX_SOFTWARE, "unable to allocate a new uid - range fully used");
82020253Sjoerg		bm_dealloc(&bm);
82120253Sjoerg	}
82220253Sjoerg	return uid;
82320253Sjoerg}
82420253Sjoerg
82520253Sjoerg
82620253Sjoergstatic          uid_t
82720253Sjoergpw_gidpolicy(struct userconf * cnf, struct cargs * args, char *nam, gid_t prefer)
82820253Sjoerg{
82920253Sjoerg	struct group   *grp;
83020253Sjoerg	gid_t           gid = (uid_t) - 1;
83120253Sjoerg	struct carg    *a_gid = getarg(args, 'g');
83220253Sjoerg
83320253Sjoerg	/*
83420253Sjoerg	 * If no arg given, see if default can help out
83520253Sjoerg	 */
83620253Sjoerg	if (a_gid == NULL && cnf->default_group && *cnf->default_group)
83720253Sjoerg		a_gid = addarg(args, 'g', cnf->default_group);
83820253Sjoerg
83920253Sjoerg	/*
84020253Sjoerg	 * Check the given gid, if any
84120253Sjoerg	 */
84244229Sdavidn	SETGRENT();
84320253Sjoerg	if (a_gid != NULL) {
84444229Sdavidn		if ((grp = GETGRNAM(a_gid->val)) == NULL) {
84520253Sjoerg			gid = (gid_t) atol(a_gid->val);
84661957Sache			if ((gid == 0 && !isdigit((unsigned char)*a_gid->val)) || (grp = GETGRGID(gid)) == NULL)
84730259Scharnier				errx(EX_NOUSER, "group `%s' is not defined", a_gid->val);
84820253Sjoerg		}
84920253Sjoerg		gid = grp->gr_gid;
850262865Sjulian	} else if ((grp = GETGRNAM(nam)) != NULL &&
851262865Sjulian	    (grp->gr_mem == NULL || grp->gr_mem[0] == NULL)) {
85220267Sjoerg		gid = grp->gr_gid;  /* Already created? Use it anyway... */
85320253Sjoerg	} else {
85420253Sjoerg		struct cargs    grpargs;
85520253Sjoerg		char            tmp[32];
85620253Sjoerg
85720253Sjoerg		LIST_INIT(&grpargs);
85820253Sjoerg		addarg(&grpargs, 'n', nam);
85920253Sjoerg
86020253Sjoerg		/*
86120253Sjoerg		 * We need to auto-create a group with the user's name. We
86220253Sjoerg		 * can send all the appropriate output to our sister routine
86320253Sjoerg		 * bit first see if we can create a group with gid==uid so we
86420253Sjoerg		 * can keep the user and group ids in sync. We purposely do
86520253Sjoerg		 * NOT check the gid range if we can force the sync. If the
86620253Sjoerg		 * user's name dups an existing group, then the group add
86720253Sjoerg		 * function will happily handle that case for us and exit.
86820253Sjoerg		 */
86944229Sdavidn		if (GETGRGID(prefer) == NULL) {
870282700Sbapt			snprintf(tmp, sizeof(tmp), "%u", prefer);
87120253Sjoerg			addarg(&grpargs, 'g', tmp);
87220253Sjoerg		}
87320267Sjoerg		if (getarg(args, 'N'))
87420267Sjoerg		{
87520267Sjoerg			addarg(&grpargs, 'N', NULL);
87620267Sjoerg			addarg(&grpargs, 'q', NULL);
87720267Sjoerg			gid = pw_group(cnf, M_NEXT, &grpargs);
87820267Sjoerg		}
87920267Sjoerg		else
88020267Sjoerg		{
88120267Sjoerg			pw_group(cnf, M_ADD, &grpargs);
88244229Sdavidn			if ((grp = GETGRNAM(nam)) != NULL)
88320267Sjoerg				gid = grp->gr_gid;
88420267Sjoerg		}
88570486Sben		a_gid = LIST_FIRST(&grpargs);
88620253Sjoerg		while (a_gid != NULL) {
88770486Sben			struct carg    *t = LIST_NEXT(a_gid, list);
88820253Sjoerg			LIST_REMOVE(a_gid, list);
88920253Sjoerg			a_gid = t;
89020253Sjoerg		}
89120253Sjoerg	}
89244229Sdavidn	ENDGRENT();
89320253Sjoerg	return gid;
89420253Sjoerg}
89520253Sjoerg
89620253Sjoerg
89720253Sjoergstatic          time_t
89820253Sjoergpw_pwdpolicy(struct userconf * cnf, struct cargs * args)
89920253Sjoerg{
90020253Sjoerg	time_t          result = 0;
90120253Sjoerg	time_t          now = time(NULL);
90227831Sdavidn	struct carg    *arg = getarg(args, 'p');
90320253Sjoerg
90420253Sjoerg	if (arg != NULL) {
90520253Sjoerg		if ((result = parse_date(now, arg->val)) == now)
90630259Scharnier			errx(EX_DATAERR, "invalid date/time `%s'", arg->val);
90720253Sjoerg	} else if (cnf->password_days > 0)
90820253Sjoerg		result = now + ((long) cnf->password_days * 86400L);
90920253Sjoerg	return result;
91020253Sjoerg}
91120253Sjoerg
91220253Sjoerg
91320253Sjoergstatic          time_t
91420253Sjoergpw_exppolicy(struct userconf * cnf, struct cargs * args)
91520253Sjoerg{
91620253Sjoerg	time_t          result = 0;
91720253Sjoerg	time_t          now = time(NULL);
91820253Sjoerg	struct carg    *arg = getarg(args, 'e');
91920253Sjoerg
92020253Sjoerg	if (arg != NULL) {
92120253Sjoerg		if ((result = parse_date(now, arg->val)) == now)
92230259Scharnier			errx(EX_DATAERR, "invalid date/time `%s'", arg->val);
92320253Sjoerg	} else if (cnf->expire_days > 0)
92420253Sjoerg		result = now + ((long) cnf->expire_days * 86400L);
92520253Sjoerg	return result;
92620253Sjoerg}
92720253Sjoerg
92820253Sjoerg
92920253Sjoergstatic char    *
93020253Sjoergpw_homepolicy(struct userconf * cnf, struct cargs * args, char const * user)
93120253Sjoerg{
93220253Sjoerg	struct carg    *arg = getarg(args, 'd');
933282699Sbapt	static char     home[128];
93420253Sjoerg
93520253Sjoerg	if (arg)
936282699Sbapt		return (arg->val);
93720253Sjoerg
938282699Sbapt	if (cnf->home == NULL || *cnf->home == '\0')
939282699Sbapt		errx(EX_CONFIG, "no base home directory set");
940282699Sbapt	snprintf(home, sizeof(home), "%s/%s", cnf->home, user);
941282699Sbapt
942282699Sbapt	return (home);
94320253Sjoerg}
94420253Sjoerg
94520253Sjoergstatic char    *
94620253Sjoergshell_path(char const * path, char *shells[], char *sh)
94720253Sjoerg{
94820253Sjoerg	if (sh != NULL && (*sh == '/' || *sh == '\0'))
94920253Sjoerg		return sh;	/* specified full path or forced none */
95020253Sjoerg	else {
95120253Sjoerg		char           *p;
95220253Sjoerg		char            paths[_UC_MAXLINE];
95320253Sjoerg
95420253Sjoerg		/*
95520253Sjoerg		 * We need to search paths
95620253Sjoerg		 */
957130633Srobert		strlcpy(paths, path, sizeof(paths));
95820253Sjoerg		for (p = strtok(paths, ": \t\r\n"); p != NULL; p = strtok(NULL, ": \t\r\n")) {
95920253Sjoerg			int             i;
96020253Sjoerg			static char     shellpath[256];
96120253Sjoerg
96220253Sjoerg			if (sh != NULL) {
963282700Sbapt				snprintf(shellpath, sizeof(shellpath), "%s/%s", p, sh);
96420253Sjoerg				if (access(shellpath, X_OK) == 0)
96520253Sjoerg					return shellpath;
96620253Sjoerg			} else
96720253Sjoerg				for (i = 0; i < _UC_MAXSHELLS && shells[i] != NULL; i++) {
968282700Sbapt					snprintf(shellpath, sizeof(shellpath), "%s/%s", p, shells[i]);
96920253Sjoerg					if (access(shellpath, X_OK) == 0)
97020253Sjoerg						return shellpath;
97120253Sjoerg				}
97220253Sjoerg		}
97320253Sjoerg		if (sh == NULL)
97430259Scharnier			errx(EX_OSFILE, "can't find shell `%s' in shell paths", sh);
97530259Scharnier		errx(EX_CONFIG, "no default shell available or defined");
97620253Sjoerg		return NULL;
97720253Sjoerg	}
97820253Sjoerg}
97920253Sjoerg
98020253Sjoerg
98120253Sjoergstatic char    *
98220253Sjoergpw_shellpolicy(struct userconf * cnf, struct cargs * args, char *newshell)
98320253Sjoerg{
98420253Sjoerg	char           *sh = newshell;
98520253Sjoerg	struct carg    *arg = getarg(args, 's');
98620253Sjoerg
98720253Sjoerg	if (newshell == NULL && arg != NULL)
98820253Sjoerg		sh = arg->val;
98920253Sjoerg	return shell_path(cnf->shelldir, cnf->shells, sh ? sh : cnf->shell_default);
99020253Sjoerg}
99120253Sjoerg
992179365Santoine#define	SALTSIZE	32
99320253Sjoerg
994179365Santoinestatic char const chars[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ./";
995179365Santoine
99620253Sjoergchar           *
99720253Sjoergpw_pwcrypt(char *password)
99820253Sjoerg{
99920253Sjoerg	int             i;
1000179365Santoine	char            salt[SALTSIZE + 1];
1001231994Skevlo	char		*cryptpw;
100220253Sjoerg
100320253Sjoerg	static char     buf[256];
100420253Sjoerg
100520253Sjoerg	/*
100620253Sjoerg	 * Calculate a salt value
100720253Sjoerg	 */
1008179365Santoine	for (i = 0; i < SALTSIZE; i++)
1009181785Sache		salt[i] = chars[arc4random_uniform(sizeof(chars) - 1)];
1010179365Santoine	salt[SALTSIZE] = '\0';
101120253Sjoerg
1012231994Skevlo	cryptpw = crypt(password, salt);
1013231994Skevlo	if (cryptpw == NULL)
1014231994Skevlo		errx(EX_CONFIG, "crypt(3) failure");
1015231994Skevlo	return strcpy(buf, cryptpw);
101620253Sjoerg}
101720253Sjoerg
101820590Sdavidn
101920253Sjoergstatic char    *
102020253Sjoergpw_password(struct userconf * cnf, struct cargs * args, char const * user)
102120253Sjoerg{
102220253Sjoerg	int             i, l;
102320253Sjoerg	char            pwbuf[32];
102420253Sjoerg
102520253Sjoerg	switch (cnf->default_password) {
102620253Sjoerg	case -1:		/* Random password */
102773563Skris		l = (arc4random() % 8 + 8);	/* 8 - 16 chars */
102820253Sjoerg		for (i = 0; i < l; i++)
1029181785Sache			pwbuf[i] = chars[arc4random_uniform(sizeof(chars)-1)];
103020253Sjoerg		pwbuf[i] = '\0';
103120253Sjoerg
103220253Sjoerg		/*
103320253Sjoerg		 * We give this information back to the user
103420253Sjoerg		 */
1035124382Siedowse		if (getarg(args, 'h') == NULL && getarg(args, 'H') == NULL &&
1036124382Siedowse		    getarg(args, 'N') == NULL) {
103761957Sache			if (isatty(STDOUT_FILENO))
103820712Sdavidn				printf("Password for '%s' is: ", user);
103920253Sjoerg			printf("%s\n", pwbuf);
104020253Sjoerg			fflush(stdout);
104120253Sjoerg		}
104220253Sjoerg		break;
104320253Sjoerg
104420253Sjoerg	case -2:		/* No password at all! */
104520253Sjoerg		return "";
104620253Sjoerg
104720253Sjoerg	case 0:		/* No login - default */
104820253Sjoerg	default:
104920253Sjoerg		return "*";
105020253Sjoerg
105120253Sjoerg	case 1:		/* user's name */
1052130633Srobert		strlcpy(pwbuf, user, sizeof(pwbuf));
105320253Sjoerg		break;
105420253Sjoerg	}
105520253Sjoerg	return pw_pwcrypt(pwbuf);
105620253Sjoerg}
105720253Sjoerg
1058284111Sbaptstatic int
1059284111Sbaptdelete_user(struct userconf *cnf, struct passwd *pwd, struct carg *a_name,
1060284111Sbapt    int delete, int mode)
1061284111Sbapt{
1062284111Sbapt	char		 file[MAXPATHLEN];
1063284111Sbapt	char		 home[MAXPATHLEN];
1064284111Sbapt	uid_t		 uid = pwd->pw_uid;
1065284111Sbapt	struct group	*gr, *grp;
1066284111Sbapt	char		 grname[LOGNAMESIZE];
1067284111Sbapt	int		 rc;
1068284111Sbapt	struct stat	 st;
106920253Sjoerg
1070284111Sbapt	if (strcmp(pwd->pw_name, "root") == 0)
1071284111Sbapt		errx(EX_DATAERR, "cannot remove user 'root'");
1072284111Sbapt
1073284111Sbapt	if (!PWALTDIR()) {
1074284111Sbapt		/*
1075284111Sbapt		 * Remove opie record from /etc/opiekeys
1076284111Sbapt		*/
1077284111Sbapt
1078284111Sbapt		rmopie(pwd->pw_name);
1079284111Sbapt
1080284111Sbapt		/*
1081284111Sbapt		 * Remove crontabs
1082284111Sbapt		 */
1083284111Sbapt		snprintf(file, sizeof(file), "/var/cron/tabs/%s", pwd->pw_name);
1084284111Sbapt		if (access(file, F_OK) == 0) {
1085284111Sbapt			snprintf(file, sizeof(file), "crontab -u %s -r", pwd->pw_name);
1086284111Sbapt			system(file);
1087284111Sbapt		}
1088284111Sbapt	}
1089284111Sbapt	/*
1090284111Sbapt	 * Save these for later, since contents of pwd may be
1091284111Sbapt	 * invalidated by deletion
1092284111Sbapt	 */
1093284111Sbapt	snprintf(file, sizeof(file), "%s/%s", _PATH_MAILDIR, pwd->pw_name);
1094284111Sbapt	strlcpy(home, pwd->pw_dir, sizeof(home));
1095284111Sbapt	gr = GETGRGID(pwd->pw_gid);
1096284111Sbapt	if (gr != NULL)
1097284111Sbapt		strlcpy(grname, gr->gr_name, LOGNAMESIZE);
1098284111Sbapt	else
1099284111Sbapt		grname[0] = '\0';
1100284111Sbapt
1101284111Sbapt	rc = delpwent(pwd);
1102284111Sbapt	if (rc == -1)
1103284111Sbapt		err(EX_IOERR, "user '%s' does not exist", pwd->pw_name);
1104284111Sbapt	else if (rc != 0)
1105284111Sbapt		err(EX_IOERR, "passwd update");
1106284111Sbapt
1107284111Sbapt	if (cnf->nispasswd && *cnf->nispasswd=='/') {
1108284111Sbapt		rc = delnispwent(cnf->nispasswd, a_name->val);
1109284111Sbapt		if (rc == -1)
1110284111Sbapt			warnx("WARNING: user '%s' does not exist in NIS passwd", pwd->pw_name);
1111284111Sbapt		else if (rc != 0)
1112284111Sbapt			warn("WARNING: NIS passwd update");
1113284111Sbapt		/* non-fatal */
1114284111Sbapt	}
1115284111Sbapt
1116284111Sbapt	grp = GETGRNAM(a_name->val);
1117284111Sbapt	if (grp != NULL &&
1118284111Sbapt	    (grp->gr_mem == NULL || *grp->gr_mem == NULL) &&
1119284111Sbapt	    strcmp(a_name->val, grname) == 0)
1120284111Sbapt		delgrent(GETGRNAM(a_name->val));
1121284111Sbapt	SETGRENT();
1122284111Sbapt	while ((grp = GETGRENT()) != NULL) {
1123284111Sbapt		int i, j;
1124284111Sbapt		char group[MAXLOGNAME];
1125284111Sbapt		if (grp->gr_mem != NULL) {
1126284111Sbapt			for (i = 0; grp->gr_mem[i] != NULL; i++) {
1127284111Sbapt				if (!strcmp(grp->gr_mem[i], a_name->val)) {
1128284111Sbapt					for (j = i; grp->gr_mem[j] != NULL; j++)
1129284111Sbapt						grp->gr_mem[j] = grp->gr_mem[j+1];
1130284111Sbapt					strlcpy(group, grp->gr_name, MAXLOGNAME);
1131284111Sbapt					chggrent(group, grp);
1132284111Sbapt				}
1133284111Sbapt			}
1134284111Sbapt		}
1135284111Sbapt	}
1136284111Sbapt	ENDGRENT();
1137284111Sbapt
1138284111Sbapt	pw_log(cnf, mode, W_USER, "%s(%u) account removed", a_name->val, uid);
1139284111Sbapt
1140284111Sbapt	if (PWALTDIR()) {
1141284111Sbapt		/*
1142284111Sbapt		 * Remove mail file
1143284111Sbapt		 */
1144284111Sbapt		remove(file);
1145284111Sbapt
1146284111Sbapt		/*
1147284111Sbapt		 * Remove at jobs
1148284111Sbapt		 */
1149284111Sbapt		if (getpwuid(uid) == NULL)
1150284111Sbapt			rmat(uid);
1151284111Sbapt
1152284111Sbapt		/*
1153284111Sbapt		 * Remove home directory and contents
1154284111Sbapt		 */
1155284111Sbapt		if (delete && *home == '/' && getpwuid(uid) == NULL) {
1156284111Sbapt			if (stat(home, &st) != -1) {
1157284111Sbapt				rm_r(home, uid);
1158284111Sbapt				pw_log(cnf, mode, W_USER, "%s(%u) home '%s' %sremoved",
1159284111Sbapt				       a_name->val, uid, home,
1160284111Sbapt				       stat(home, &st) == -1 ? "" : "not completely ");
1161284111Sbapt			}
1162284111Sbapt		}
1163284111Sbapt	}
1164284111Sbapt
1165284111Sbapt	return (EXIT_SUCCESS);
1166284111Sbapt}
1167284111Sbapt
116820253Sjoergstatic int
116944386Sdavidnprint_user(struct passwd * pwd, int pretty, int v7)
117020253Sjoerg{
117120253Sjoerg	if (!pretty) {
1172242349Sbapt		char            *buf;
117320253Sjoerg
1174242349Sbapt		if (!v7)
1175242349Sbapt			pwd->pw_passwd = (pwd->pw_passwd == NULL) ? "" : "*";
1176242349Sbapt
1177242349Sbapt		buf = v7 ? pw_make_v7(pwd) : pw_make(pwd);
1178242349Sbapt		printf("%s\n", buf);
1179242349Sbapt		free(buf);
118020253Sjoerg	} else {
118120267Sjoerg		int		j;
118220253Sjoerg		char           *p;
118344229Sdavidn		struct group   *grp = GETGRGID(pwd->pw_gid);
118420253Sjoerg		char            uname[60] = "User &", office[60] = "[None]",
118520253Sjoerg		                wphone[60] = "[None]", hphone[60] = "[None]";
118620590Sdavidn		char		acexpire[32] = "[None]", pwexpire[32] = "[None]";
118720590Sdavidn		struct tm *    tptr;
118820253Sjoerg
118920253Sjoerg		if ((p = strtok(pwd->pw_gecos, ",")) != NULL) {
1190130633Srobert			strlcpy(uname, p, sizeof(uname));
119120253Sjoerg			if ((p = strtok(NULL, ",")) != NULL) {
1192130633Srobert				strlcpy(office, p, sizeof(office));
119320253Sjoerg				if ((p = strtok(NULL, ",")) != NULL) {
1194130633Srobert					strlcpy(wphone, p, sizeof(wphone));
119520253Sjoerg					if ((p = strtok(NULL, "")) != NULL) {
1196130633Srobert						strlcpy(hphone, p,
1197130633Srobert						    sizeof(hphone));
119820253Sjoerg					}
119920253Sjoerg				}
120020253Sjoerg			}
120120253Sjoerg		}
120220253Sjoerg		/*
120320253Sjoerg		 * Handle '&' in gecos field
120420253Sjoerg		 */
120520253Sjoerg		if ((p = strchr(uname, '&')) != NULL) {
120620253Sjoerg			int             l = strlen(pwd->pw_name);
120720253Sjoerg			int             m = strlen(p);
120820253Sjoerg
120920253Sjoerg			memmove(p + l, p + 1, m);
121020253Sjoerg			memmove(p, pwd->pw_name, l);
121161957Sache			*p = (char) toupper((unsigned char)*p);
121220253Sjoerg		}
121320590Sdavidn		if (pwd->pw_expire > (time_t)0 && (tptr = localtime(&pwd->pw_expire)) != NULL)
121474569Sache			strftime(acexpire, sizeof acexpire, "%c", tptr);
121561957Sache		if (pwd->pw_change > (time_t)0 && (tptr = localtime(&pwd->pw_change)) != NULL)
121674569Sache			strftime(pwexpire, sizeof pwexpire, "%c", tptr);
1217283842Sbapt		printf("Login Name: %-15s   #%-12u Group: %-15s   #%u\n"
121820747Sdavidn		       " Full Name: %s\n"
121920747Sdavidn		       "      Home: %-26.26s      Class: %s\n"
122020747Sdavidn		       "     Shell: %-26.26s     Office: %s\n"
122120747Sdavidn		       "Work Phone: %-26.26s Home Phone: %s\n"
122220747Sdavidn		       "Acc Expire: %-26.26s Pwd Expire: %s\n",
1223283842Sbapt		       pwd->pw_name, pwd->pw_uid,
1224283842Sbapt		       grp ? grp->gr_name : "(invalid)", pwd->pw_gid,
122520253Sjoerg		       uname, pwd->pw_dir, pwd->pw_class,
122620590Sdavidn		       pwd->pw_shell, office, wphone, hphone,
122720590Sdavidn		       acexpire, pwexpire);
122844229Sdavidn	        SETGRENT();
122920267Sjoerg		j = 0;
123044229Sdavidn		while ((grp=GETGRENT()) != NULL)
123120267Sjoerg		{
123220267Sjoerg			int     i = 0;
1233262865Sjulian			if (grp->gr_mem != NULL) {
1234262865Sjulian				while (grp->gr_mem[i] != NULL)
123520267Sjoerg				{
1236262865Sjulian					if (strcmp(grp->gr_mem[i], pwd->pw_name)==0)
1237262865Sjulian					{
1238262865Sjulian						printf(j++ == 0 ? "    Groups: %s" : ",%s", grp->gr_name);
1239262865Sjulian						break;
1240262865Sjulian					}
1241262865Sjulian					++i;
124220267Sjoerg				}
124320267Sjoerg			}
124420267Sjoerg		}
124544229Sdavidn		ENDGRENT();
124661957Sache		printf("%s", j ? "\n" : "");
124720253Sjoerg	}
124820267Sjoerg	return EXIT_SUCCESS;
124920253Sjoerg}
125020253Sjoerg
1251284110Sbaptchar *
1252284110Sbaptpw_checkname(char *name, int gecos)
125320253Sjoerg{
1254109961Sgad	char showch[8];
1255284110Sbapt	const char *badchars, *ch, *showtype;
1256109961Sgad	int reject;
125720253Sjoerg
1258109961Sgad	ch = name;
1259109961Sgad	reject = 0;
1260109961Sgad	if (gecos) {
1261109961Sgad		/* See if the name is valid as a gecos (comment) field. */
1262109961Sgad		badchars = ":!@";
1263109961Sgad		showtype = "gecos field";
1264109961Sgad	} else {
1265109961Sgad		/* See if the name is valid as a userid or group. */
1266109961Sgad		badchars = " ,\t:+&#%$^()!@~*?<>=|\\/\"";
1267109961Sgad		showtype = "userid/group name";
1268109961Sgad		/* Userids and groups can not have a leading '-'. */
1269109961Sgad		if (*ch == '-')
1270109961Sgad			reject = 1;
127120253Sjoerg	}
1272109961Sgad	if (!reject) {
1273109961Sgad		while (*ch) {
1274109961Sgad			if (strchr(badchars, *ch) != NULL || *ch < ' ' ||
1275109961Sgad			    *ch == 127) {
1276109961Sgad				reject = 1;
1277109961Sgad				break;
1278109961Sgad			}
1279109961Sgad			/* 8-bit characters are only allowed in GECOS fields */
1280109961Sgad			if (!gecos && (*ch & 0x80)) {
1281109961Sgad				reject = 1;
1282109961Sgad				break;
1283109961Sgad			}
1284109961Sgad			ch++;
1285109961Sgad		}
1286109961Sgad	}
1287109961Sgad	/*
1288109961Sgad	 * A `$' is allowed as the final character for userids and groups,
1289109961Sgad	 * mainly for the benefit of samba.
1290109961Sgad	 */
1291109961Sgad	if (reject && !gecos) {
1292109961Sgad		if (*ch == '$' && *(ch + 1) == '\0') {
1293109961Sgad			reject = 0;
1294109961Sgad			ch++;
1295109961Sgad		}
1296109961Sgad	}
1297109961Sgad	if (reject) {
1298109961Sgad		snprintf(showch, sizeof(showch), (*ch >= ' ' && *ch < 127)
1299109961Sgad		    ? "`%c'" : "0x%02x", *ch);
1300228673Sdim		errx(EX_DATAERR, "invalid character %s at position %td in %s",
1301109961Sgad		    showch, (ch - name), showtype);
1302109961Sgad	}
1303109961Sgad	if (!gecos && (ch - name) > LOGNAMESIZE)
1304109961Sgad		errx(EX_DATAERR, "name too long `%s' (max is %d)", name,
1305109961Sgad		    LOGNAMESIZE);
1306284110Sbapt
1307284110Sbapt	return (name);
130820253Sjoerg}
130920253Sjoerg
131020253Sjoerg
131120253Sjoergstatic void
131220253Sjoergrmat(uid_t uid)
131320253Sjoerg{
131420253Sjoerg	DIR            *d = opendir("/var/at/jobs");
131520253Sjoerg
131620253Sjoerg	if (d != NULL) {
131720253Sjoerg		struct dirent  *e;
131820253Sjoerg
131920253Sjoerg		while ((e = readdir(d)) != NULL) {
132020253Sjoerg			struct stat     st;
132120253Sjoerg
132220253Sjoerg			if (strncmp(e->d_name, ".lock", 5) != 0 &&
132320253Sjoerg			    stat(e->d_name, &st) == 0 &&
132420253Sjoerg			    !S_ISDIR(st.st_mode) &&
132520253Sjoerg			    st.st_uid == uid) {
132620253Sjoerg				char            tmp[MAXPATHLEN];
132720253Sjoerg
1328282700Sbapt				snprintf(tmp, sizeof(tmp), "/usr/bin/atrm %s", e->d_name);
132920253Sjoerg				system(tmp);
133020253Sjoerg			}
133120253Sjoerg		}
133220253Sjoerg		closedir(d);
133320253Sjoerg	}
133420253Sjoerg}
133520747Sdavidn
133620747Sdavidnstatic void
133785145Sachermopie(char const * name)
133820747Sdavidn{
133985145Sache	static const char etcopie[] = "/etc/opiekeys";
134085145Sache	FILE   *fp = fopen(etcopie, "r+");
134120747Sdavidn
134221052Sdavidn	if (fp != NULL) {
134321052Sdavidn		char	tmp[1024];
134421052Sdavidn		off_t	atofs = 0;
134521052Sdavidn		int	length = strlen(name);
134620747Sdavidn
134721052Sdavidn		while (fgets(tmp, sizeof tmp, fp) != NULL) {
134821052Sdavidn			if (strncmp(name, tmp, length) == 0 && tmp[length]==' ') {
134921052Sdavidn				if (fseek(fp, atofs, SEEK_SET) == 0) {
135021052Sdavidn					fwrite("#", 1, 1, fp);	/* Comment username out */
135121052Sdavidn				}
135221052Sdavidn				break;
135320747Sdavidn			}
135421052Sdavidn			atofs = ftell(fp);
135520747Sdavidn		}
135621052Sdavidn		/*
135721052Sdavidn		 * If we got an error of any sort, don't update!
135821052Sdavidn		 */
135921052Sdavidn		fclose(fp);
136020747Sdavidn	}
136120747Sdavidn}
136220747Sdavidn
1363