pw_user.c revision 284128
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 284128 2015-06-07 19:03:41Z 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,
55284128Sbapt		    char *name, int delete, int mode);
56284124Sbaptstatic int	print_user(struct passwd * pwd);
5720253Sjoergstatic uid_t    pw_uidpolicy(struct userconf * cnf, struct cargs * args);
58284118Sbaptstatic uid_t    pw_gidpolicy(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
69284118Sbaptcreate_and_populate_homedir(int mode, struct passwd *pwd)
70283961Sbapt{
71283961Sbapt	char *homedir, *dotdir;
72284118Sbapt	struct userconf *cnf = conf.userconf;
73283961Sbapt
74283961Sbapt	homedir = dotdir = NULL;
75283961Sbapt
76284118Sbapt	if (conf.rootdir[0] != '\0') {
77284118Sbapt		asprintf(&homedir, "%s/%s", conf.rootdir, pwd->pw_dir);
78283961Sbapt		if (homedir == NULL)
79283961Sbapt			errx(EX_OSERR, "out of memory");
80284118Sbapt		asprintf(&dotdir, "%s/%s", conf.rootdir, cnf->dotdir);
81283961Sbapt	}
82283961Sbapt
83283961Sbapt	copymkdir(homedir ? homedir : pwd->pw_dir, dotdir ? dotdir: cnf->dotdir,
84283961Sbapt	    cnf->homemode, pwd->pw_uid, pwd->pw_gid);
85283961Sbapt	pw_log(cnf, mode, W_USER, "%s(%u) home %s made", pwd->pw_name,
86283961Sbapt	    pwd->pw_uid, pwd->pw_dir);
87283961Sbapt}
88283961Sbapt
8920253Sjoerg/*-
9020253Sjoerg * -C config      configuration file
9120253Sjoerg * -q             quiet operation
9220253Sjoerg * -n name        login name
9320253Sjoerg * -u uid         user id
9420253Sjoerg * -c comment     user name/comment
9520253Sjoerg * -d directory   home directory
9620253Sjoerg * -e date        account expiry date
9720253Sjoerg * -p date        password expiry date
9820253Sjoerg * -g grp         primary group
9920253Sjoerg * -G grp1,grp2   additional groups
10020253Sjoerg * -m [ -k dir ]  create and set up home
10120253Sjoerg * -s shell       name of login shell
10220253Sjoerg * -o             duplicate uid ok
10320253Sjoerg * -L class       user class
10420253Sjoerg * -l name        new login name
10520253Sjoerg * -h fd          password filehandle
106124382Siedowse * -H fd          encrypted password filehandle
10720253Sjoerg * -F             force print or add
10820253Sjoerg *   Setting defaults:
10920253Sjoerg * -D             set user defaults
11020253Sjoerg * -b dir         default home root dir
11120253Sjoerg * -e period      default expiry period
11220253Sjoerg * -p period      default password change period
11320253Sjoerg * -g group       default group
11420253Sjoerg * -G             grp1,grp2.. default additional groups
11520253Sjoerg * -L class       default login class
11620253Sjoerg * -k dir         default home skeleton
11720253Sjoerg * -s shell       default shell
11820253Sjoerg * -w method      default password method
11920253Sjoerg */
12020253Sjoerg
12120253Sjoergint
122284128Sbaptpw_user(int mode, char *name, long id, struct cargs * args)
12320253Sjoerg{
12452527Sdavidn	int	        rc, edited = 0;
12520253Sjoerg	char           *p = NULL;
12652512Sdavidn	char					 *passtmp;
12720253Sjoerg	struct carg    *arg;
12820253Sjoerg	struct passwd  *pwd = NULL;
12920253Sjoerg	struct group   *grp;
13020253Sjoerg	struct stat     st;
131284118Sbapt	struct userconf	*cnf;
13220747Sdavidn	char            line[_PASSWORD_LEN+1];
133283961Sbapt	char		path[MAXPATHLEN];
13482868Sdd	FILE	       *fp;
135167919Sle	char *dmode_c;
136167919Sle	void *set = NULL;
13720253Sjoerg
13820253Sjoerg	static struct passwd fakeuser =
13920253Sjoerg	{
14020253Sjoerg		NULL,
14120253Sjoerg		"*",
14220253Sjoerg		-1,
14320253Sjoerg		-1,
14420253Sjoerg		0,
14520253Sjoerg		"",
14620253Sjoerg		"User &",
14756000Sdavidn		"/nonexistent",
14820253Sjoerg		"/bin/sh",
14920253Sjoerg		0
15056000Sdavidn#if defined(__FreeBSD__)
15156000Sdavidn		,0
15256000Sdavidn#endif
15320253Sjoerg	};
15420253Sjoerg
155284118Sbapt	cnf = conf.userconf;
15652512Sdavidn
15720253Sjoerg	/*
15820267Sjoerg	 * With M_NEXT, we only need to return the
15920267Sjoerg	 * next uid to stdout
16020267Sjoerg	 */
16120267Sjoerg	if (mode == M_NEXT)
16220267Sjoerg	{
16320267Sjoerg		uid_t next = pw_uidpolicy(cnf, args);
16420267Sjoerg		if (getarg(args, 'q'))
16520267Sjoerg			return next;
166283842Sbapt		printf("%u:", next);
167284128Sbapt		pw_group(mode, name, -1, args);
16820267Sjoerg		return EXIT_SUCCESS;
16920267Sjoerg	}
17020267Sjoerg
17120267Sjoerg	/*
17220253Sjoerg	 * We can do all of the common legwork here
17320253Sjoerg	 */
17420253Sjoerg
17520253Sjoerg	if ((arg = getarg(args, 'b')) != NULL) {
17620267Sjoerg		cnf->home = arg->val;
17720253Sjoerg	}
17821052Sdavidn
179167919Sle	if ((arg = getarg(args, 'M')) != NULL) {
180167919Sle		dmode_c = arg->val;
181167919Sle		if ((set = setmode(dmode_c)) == NULL)
182167919Sle			errx(EX_DATAERR, "invalid directory creation mode '%s'",
183167919Sle			    dmode_c);
184219408Sjkim		cnf->homemode = getmode(set, _DEF_DIRMODE);
185167919Sle		free(set);
186168044Sle	}
187167919Sle
18821052Sdavidn	/*
18921052Sdavidn	 * If we'll need to use it or we're updating it,
19021052Sdavidn	 * then create the base home directory if necessary
19121052Sdavidn	 */
192224535Sdelphij	if (arg != NULL || getarg(args, 'm') != NULL) {
19321052Sdavidn		int	l = strlen(cnf->home);
19421052Sdavidn
19521052Sdavidn		if (l > 1 && cnf->home[l-1] == '/')	/* Shave off any trailing path delimiter */
19621052Sdavidn			cnf->home[--l] = '\0';
19721052Sdavidn
19821052Sdavidn		if (l < 2 || *cnf->home != '/')		/* Check for absolute path name */
19930259Scharnier			errx(EX_DATAERR, "invalid base directory for home '%s'", cnf->home);
20021052Sdavidn
20121052Sdavidn		if (stat(cnf->home, &st) == -1) {
20221052Sdavidn			char	dbuf[MAXPATHLEN];
20321052Sdavidn
20421242Sdavidn			/*
20521242Sdavidn			 * This is a kludge especially for Joerg :)
20621242Sdavidn			 * If the home directory would be created in the root partition, then
20721242Sdavidn			 * we really create it under /usr which is likely to have more space.
20821242Sdavidn			 * But we create a symlink from cnf->home -> "/usr" -> cnf->home
20921242Sdavidn			 */
21021242Sdavidn			if (strchr(cnf->home+1, '/') == NULL) {
211282683Sbapt				snprintf(dbuf, MAXPATHLEN, "/usr%s", cnf->home);
212219408Sjkim				if (mkdir(dbuf, _DEF_DIRMODE) != -1 || errno == EEXIST) {
21321242Sdavidn					chown(dbuf, 0, 0);
214148584Spjd					/*
215148584Spjd					 * Skip first "/" and create symlink:
216148584Spjd					 * /home -> usr/home
217148584Spjd					 */
218148584Spjd					symlink(dbuf+1, cnf->home);
21921242Sdavidn				}
22021242Sdavidn				/* If this falls, fall back to old method */
22121242Sdavidn			}
222130633Srobert			strlcpy(dbuf, cnf->home, sizeof(dbuf));
223130633Srobert			p = dbuf;
22421242Sdavidn			if (stat(dbuf, &st) == -1) {
225252377Skientzle				while ((p = strchr(p + 1, '/')) != NULL) {
22621242Sdavidn					*p = '\0';
22721242Sdavidn					if (stat(dbuf, &st) == -1) {
228219408Sjkim						if (mkdir(dbuf, _DEF_DIRMODE) == -1)
22921242Sdavidn							goto direrr;
23021242Sdavidn						chown(dbuf, 0, 0);
23121242Sdavidn					} else if (!S_ISDIR(st.st_mode))
23230259Scharnier						errx(EX_OSFILE, "'%s' (root home parent) is not a directory", dbuf);
23321242Sdavidn					*p = '/';
23421242Sdavidn				}
23521052Sdavidn			}
23621242Sdavidn			if (stat(dbuf, &st) == -1) {
237219408Sjkim				if (mkdir(dbuf, _DEF_DIRMODE) == -1) {
23830259Scharnier				direrr:	err(EX_OSFILE, "mkdir '%s'", dbuf);
23921052Sdavidn				}
24021052Sdavidn				chown(dbuf, 0, 0);
24121052Sdavidn			}
24221052Sdavidn		} else if (!S_ISDIR(st.st_mode))
24330259Scharnier			errx(EX_OSFILE, "root home `%s' is not a directory", cnf->home);
24421052Sdavidn	}
24521052Sdavidn
24620253Sjoerg	if ((arg = getarg(args, 'e')) != NULL)
24720253Sjoerg		cnf->expire_days = atoi(arg->val);
24820253Sjoerg
24921330Sdavidn	if ((arg = getarg(args, 'y')) != NULL)
25021330Sdavidn		cnf->nispasswd = arg->val;
25121330Sdavidn
25220253Sjoerg	if ((arg = getarg(args, 'p')) != NULL && arg->val)
25320253Sjoerg		cnf->password_days = atoi(arg->val);
25420253Sjoerg
25520253Sjoerg	if ((arg = getarg(args, 'g')) != NULL) {
25663596Sdavidn		if (!*(p = arg->val))	/* Handle empty group list specially */
25763596Sdavidn			cnf->default_group = "";
25863596Sdavidn		else {
25963596Sdavidn			if ((grp = GETGRNAM(p)) == NULL) {
26063596Sdavidn				if (!isdigit((unsigned char)*p) || (grp = GETGRGID((gid_t) atoi(p))) == NULL)
26163596Sdavidn					errx(EX_NOUSER, "group `%s' does not exist", p);
26263596Sdavidn			}
26363596Sdavidn			cnf->default_group = newstr(grp->gr_name);
26420253Sjoerg		}
26520253Sjoerg	}
26620253Sjoerg	if ((arg = getarg(args, 'L')) != NULL)
267284110Sbapt		cnf->default_class = pw_checkname(arg->val, 0);
26820253Sjoerg
26920253Sjoerg	if ((arg = getarg(args, 'G')) != NULL && arg->val) {
27052527Sdavidn		int i = 0;
27120253Sjoerg
27220747Sdavidn		for (p = strtok(arg->val, ", \t"); p != NULL; p = strtok(NULL, ", \t")) {
27344229Sdavidn			if ((grp = GETGRNAM(p)) == NULL) {
27461957Sache				if (!isdigit((unsigned char)*p) || (grp = GETGRGID((gid_t) atoi(p))) == NULL)
27530259Scharnier					errx(EX_NOUSER, "group `%s' does not exist", p);
27620253Sjoerg			}
27720747Sdavidn			if (extendarray(&cnf->groups, &cnf->numgroups, i + 2) != -1)
27820747Sdavidn				cnf->groups[i++] = newstr(grp->gr_name);
27920253Sjoerg		}
28020747Sdavidn		while (i < cnf->numgroups)
28120253Sjoerg			cnf->groups[i++] = NULL;
28220253Sjoerg	}
28352527Sdavidn
28420253Sjoerg	if ((arg = getarg(args, 'k')) != NULL) {
28526088Sdavidn		if (stat(cnf->dotdir = arg->val, &st) == -1 || !S_ISDIR(st.st_mode))
28630259Scharnier			errx(EX_OSFILE, "skeleton `%s' is not a directory or does not exist", cnf->dotdir);
28720253Sjoerg	}
28852527Sdavidn
28920253Sjoerg	if ((arg = getarg(args, 's')) != NULL)
29020253Sjoerg		cnf->shell_default = arg->val;
29120253Sjoerg
29263600Sdavidn	if ((arg = getarg(args, 'w')) != NULL)
29363600Sdavidn		cnf->default_password = boolean_val(arg->val, cnf->default_password);
29420253Sjoerg	if (mode == M_ADD && getarg(args, 'D')) {
29520253Sjoerg		if (getarg(args, 'n') != NULL)
29630259Scharnier			errx(EX_DATAERR, "can't combine `-D' with `-n name'");
29720253Sjoerg		if ((arg = getarg(args, 'u')) != NULL && (p = strtok(arg->val, ", \t")) != NULL) {
29820253Sjoerg			if ((cnf->min_uid = (uid_t) atoi(p)) == 0)
29920253Sjoerg				cnf->min_uid = 1000;
30020253Sjoerg			if ((p = strtok(NULL, " ,\t")) == NULL || (cnf->max_uid = (uid_t) atoi(p)) < cnf->min_uid)
30120253Sjoerg				cnf->max_uid = 32000;
30220253Sjoerg		}
30320253Sjoerg		if ((arg = getarg(args, 'i')) != NULL && (p = strtok(arg->val, ", \t")) != NULL) {
30420253Sjoerg			if ((cnf->min_gid = (gid_t) atoi(p)) == 0)
30520253Sjoerg				cnf->min_gid = 1000;
30620253Sjoerg			if ((p = strtok(NULL, " ,\t")) == NULL || (cnf->max_gid = (gid_t) atoi(p)) < cnf->min_gid)
30720253Sjoerg				cnf->max_gid = 32000;
30820253Sjoerg		}
30920253Sjoerg
31020253Sjoerg		arg = getarg(args, 'C');
31120253Sjoerg		if (write_userconfig(arg ? arg->val : NULL))
31220267Sjoerg			return EXIT_SUCCESS;
313283814Sbapt		err(EX_IOERR, "config udpate");
31420253Sjoerg	}
31552527Sdavidn
31620253Sjoerg	if (mode == M_PRINT && getarg(args, 'a')) {
31744229Sdavidn		SETPWENT();
31844229Sdavidn		while ((pwd = GETPWENT()) != NULL)
319284124Sbapt			print_user(pwd);
32044229Sdavidn		ENDPWENT();
32120267Sjoerg		return EXIT_SUCCESS;
32220253Sjoerg	}
32352527Sdavidn
324284128Sbapt	if (name != NULL)
325284128Sbapt		pwd = GETPWNAM(pw_checkname(name, 0));
32620253Sjoerg
327284128Sbapt	if (id < 0 && name == NULL)
328284128Sbapt		errx(EX_DATAERR, "user name or id required");
32920253Sjoerg
33020253Sjoerg	/*
33120253Sjoerg	 * Update, delete & print require that the user exists
33220253Sjoerg	 */
33352512Sdavidn	if (mode == M_UPDATE || mode == M_DELETE ||
33452512Sdavidn	    mode == M_PRINT  || mode == M_LOCK   || mode == M_UNLOCK) {
33552527Sdavidn
336284128Sbapt		if (name == NULL && pwd == NULL)	/* Try harder */
337284128Sbapt			pwd = GETPWUID(id);
33820253Sjoerg
33920253Sjoerg		if (pwd == NULL) {
34020253Sjoerg			if (mode == M_PRINT && getarg(args, 'F')) {
341284128Sbapt				fakeuser.pw_name = name ? name : "nouser";
342284128Sbapt				fakeuser.pw_uid = (uid_t) id;
343284126Sbapt				return print_user(&fakeuser);
34420253Sjoerg			}
345284128Sbapt			if (name == NULL)
346284128Sbapt				errx(EX_NOUSER, "no such uid `%ld'", id);
347284128Sbapt			errx(EX_NOUSER, "no such user `%s'", name);
34820253Sjoerg		}
34952527Sdavidn
350284128Sbapt		if (name == NULL)
351284128Sbapt			name = pwd->pw_name;
35220253Sjoerg
35320253Sjoerg		/*
35452512Sdavidn		 * The M_LOCK and M_UNLOCK functions simply add or remove
35552512Sdavidn		 * a "*LOCKED*" prefix from in front of the password to
35652512Sdavidn		 * prevent it decoding correctly, and therefore prevents
35752512Sdavidn		 * access. Of course, this only prevents access via
35852512Sdavidn		 * password authentication (not ssh, kerberos or any
35952512Sdavidn		 * other method that does not use the UNIX password) but
36052512Sdavidn		 * that is a known limitation.
36152512Sdavidn		 */
36252512Sdavidn
36352512Sdavidn		if (mode == M_LOCK) {
36452512Sdavidn			if (strncmp(pwd->pw_passwd, locked_str, sizeof(locked_str)-1) == 0)
36552512Sdavidn				errx(EX_DATAERR, "user '%s' is already locked", pwd->pw_name);
366282685Sbapt			asprintf(&passtmp, "%s%s", locked_str, pwd->pw_passwd);
36752512Sdavidn			if (passtmp == NULL)	/* disaster */
36852512Sdavidn				errx(EX_UNAVAILABLE, "out of memory");
36952512Sdavidn			pwd->pw_passwd = passtmp;
37052527Sdavidn			edited = 1;
37152512Sdavidn		} else if (mode == M_UNLOCK) {
37252512Sdavidn			if (strncmp(pwd->pw_passwd, locked_str, sizeof(locked_str)-1) != 0)
37352512Sdavidn				errx(EX_DATAERR, "user '%s' is not locked", pwd->pw_name);
37452512Sdavidn			pwd->pw_passwd += sizeof(locked_str)-1;
37552527Sdavidn			edited = 1;
376284111Sbapt		} else if (mode == M_DELETE)
377284128Sbapt			return (delete_user(cnf, pwd, name,
378284111Sbapt				    getarg(args, 'r') != NULL, mode));
379284111Sbapt		else if (mode == M_PRINT)
380284126Sbapt			return print_user(pwd);
38120253Sjoerg
38220253Sjoerg		/*
38320253Sjoerg		 * The rest is edit code
38420253Sjoerg		 */
38520253Sjoerg		if ((arg = getarg(args, 'l')) != NULL) {
38620253Sjoerg			if (strcmp(pwd->pw_name, "root") == 0)
38730259Scharnier				errx(EX_DATAERR, "can't rename `root' account");
388284110Sbapt			pwd->pw_name = pw_checkname(arg->val, 0);
38952527Sdavidn			edited = 1;
39020253Sjoerg		}
39152527Sdavidn
39261957Sache		if ((arg = getarg(args, 'u')) != NULL && isdigit((unsigned char)*arg->val)) {
39320253Sjoerg			pwd->pw_uid = (uid_t) atol(arg->val);
39452527Sdavidn			edited = 1;
39520253Sjoerg			if (pwd->pw_uid != 0 && strcmp(pwd->pw_name, "root") == 0)
39630259Scharnier				errx(EX_DATAERR, "can't change uid of `root' account");
39720253Sjoerg			if (pwd->pw_uid == 0 && strcmp(pwd->pw_name, "root") != 0)
39830259Scharnier				warnx("WARNING: account `%s' will have a uid of 0 (superuser access!)", pwd->pw_name);
39920253Sjoerg		}
40020253Sjoerg
40152527Sdavidn		if ((arg = getarg(args, 'g')) != NULL && pwd->pw_uid != 0) {	/* Already checked this */
40252527Sdavidn			gid_t newgid = (gid_t) GETGRNAM(cnf->default_group)->gr_gid;
40352527Sdavidn			if (newgid != pwd->pw_gid) {
40452527Sdavidn				edited = 1;
40561762Sdavidn				pwd->pw_gid = newgid;
40652527Sdavidn			}
40752527Sdavidn		}
40852527Sdavidn
40920253Sjoerg		if ((arg = getarg(args, 'p')) != NULL) {
41052527Sdavidn			if (*arg->val == '\0' || strcmp(arg->val, "0") == 0) {
41152527Sdavidn				if (pwd->pw_change != 0) {
41252527Sdavidn					pwd->pw_change = 0;
41352527Sdavidn					edited = 1;
41452527Sdavidn				}
41552527Sdavidn			}
41620253Sjoerg			else {
41720253Sjoerg				time_t          now = time(NULL);
41820253Sjoerg				time_t          expire = parse_date(now, arg->val);
41920253Sjoerg
42052527Sdavidn				if (pwd->pw_change != expire) {
42152527Sdavidn					pwd->pw_change = expire;
42252527Sdavidn					edited = 1;
42352527Sdavidn				}
42420253Sjoerg			}
42520253Sjoerg		}
42652527Sdavidn
42720267Sjoerg		if ((arg = getarg(args, 'e')) != NULL) {
42852527Sdavidn			if (*arg->val == '\0' || strcmp(arg->val, "0") == 0) {
42952527Sdavidn				if (pwd->pw_expire != 0) {
43052527Sdavidn					pwd->pw_expire = 0;
43152527Sdavidn					edited = 1;
43252527Sdavidn				}
43352527Sdavidn			}
43420253Sjoerg			else {
43520253Sjoerg				time_t          now = time(NULL);
43620253Sjoerg				time_t          expire = parse_date(now, arg->val);
43720253Sjoerg
43852527Sdavidn				if (pwd->pw_expire != expire) {
43952527Sdavidn					pwd->pw_expire = expire;
44052527Sdavidn					edited = 1;
44152527Sdavidn				}
44220253Sjoerg			}
44320253Sjoerg		}
44420253Sjoerg
44552527Sdavidn		if ((arg = getarg(args, 's')) != NULL) {
44652527Sdavidn			char *shell = shell_path(cnf->shelldir, cnf->shells, arg->val);
44752527Sdavidn			if (shell == NULL)
44852527Sdavidn				shell = "";
44952527Sdavidn			if (strcmp(shell, pwd->pw_shell) != 0) {
45052527Sdavidn				pwd->pw_shell = shell;
45152527Sdavidn				edited = 1;
45252527Sdavidn			}
45352527Sdavidn		}
45420253Sjoerg
45552527Sdavidn		if (getarg(args, 'L')) {
45652527Sdavidn			if (cnf->default_class == NULL)
45752527Sdavidn				cnf->default_class = "";
45852527Sdavidn			if (strcmp(pwd->pw_class, cnf->default_class) != 0) {
45952527Sdavidn				pwd->pw_class = cnf->default_class;
46052527Sdavidn				edited = 1;
46152527Sdavidn			}
46252527Sdavidn		}
46352527Sdavidn
46420747Sdavidn		if ((arg  = getarg(args, 'd')) != NULL) {
465130629Srobert			if (strcmp(pwd->pw_dir, arg->val))
466130629Srobert				edited = 1;
46720747Sdavidn			if (stat(pwd->pw_dir = arg->val, &st) == -1) {
46820747Sdavidn				if (getarg(args, 'm') == NULL && strcmp(pwd->pw_dir, "/nonexistent") != 0)
46930259Scharnier				  warnx("WARNING: home `%s' does not exist", pwd->pw_dir);
47020747Sdavidn			} else if (!S_ISDIR(st.st_mode))
47130259Scharnier				warnx("WARNING: home `%s' is not a directory", pwd->pw_dir);
47220747Sdavidn		}
47320747Sdavidn
474124382Siedowse		if ((arg = getarg(args, 'w')) != NULL &&
475124382Siedowse		    getarg(args, 'h') == NULL && getarg(args, 'H') == NULL) {
47664918Sgreen			login_cap_t *lc;
47764918Sgreen
47864918Sgreen			lc = login_getpwclass(pwd);
47964918Sgreen			if (lc == NULL ||
480252688Sdes			    login_setcryptfmt(lc, "sha512", NULL) == NULL)
48164918Sgreen				warn("setting crypt(3) format");
48264918Sgreen			login_close(lc);
48320267Sjoerg			pwd->pw_passwd = pw_password(cnf, args, pwd->pw_name);
48452527Sdavidn			edited = 1;
48552527Sdavidn		}
48620267Sjoerg
48720253Sjoerg	} else {
48864918Sgreen		login_cap_t *lc;
48952527Sdavidn
49052527Sdavidn		/*
49152527Sdavidn		 * Add code
49252527Sdavidn		 */
49352527Sdavidn
494284128Sbapt		if (name == NULL)	/* Required */
49530259Scharnier			errx(EX_DATAERR, "login name required");
496284128Sbapt		else if ((pwd = GETPWNAM(name)) != NULL)	/* Exists */
497284128Sbapt			errx(EX_DATAERR, "login name `%s' already exists", name);
49820253Sjoerg
49920253Sjoerg		/*
50020253Sjoerg		 * Now, set up defaults for a new user
50120253Sjoerg		 */
50220253Sjoerg		pwd = &fakeuser;
503284128Sbapt		pwd->pw_name = name;
50420253Sjoerg		pwd->pw_class = cnf->default_class ? cnf->default_class : "";
50520253Sjoerg		pwd->pw_uid = pw_uidpolicy(cnf, args);
506284118Sbapt		pwd->pw_gid = pw_gidpolicy(args, pwd->pw_name, (gid_t) pwd->pw_uid);
50720253Sjoerg		pwd->pw_change = pw_pwdpolicy(cnf, args);
50820253Sjoerg		pwd->pw_expire = pw_exppolicy(cnf, args);
50920253Sjoerg		pwd->pw_dir = pw_homepolicy(cnf, args, pwd->pw_name);
51020253Sjoerg		pwd->pw_shell = pw_shellpolicy(cnf, args, NULL);
51164918Sgreen		lc = login_getpwclass(pwd);
512272833Sdes		if (lc == NULL || login_setcryptfmt(lc, "sha512", NULL) == NULL)
51364918Sgreen			warn("setting crypt(3) format");
51464918Sgreen		login_close(lc);
51564918Sgreen		pwd->pw_passwd = pw_password(cnf, args, pwd->pw_name);
51652527Sdavidn		edited = 1;
51720253Sjoerg
51820253Sjoerg		if (pwd->pw_uid == 0 && strcmp(pwd->pw_name, "root") != 0)
51930259Scharnier			warnx("WARNING: new account `%s' has a uid of 0 (superuser access!)", pwd->pw_name);
52020253Sjoerg	}
52120253Sjoerg
52220253Sjoerg	/*
52320253Sjoerg	 * Shared add/edit code
52420253Sjoerg	 */
52552527Sdavidn	if ((arg = getarg(args, 'c')) != NULL) {
526284110Sbapt		char	*gecos = pw_checkname(arg->val, 1);
52752527Sdavidn		if (strcmp(pwd->pw_gecos, gecos) != 0) {
52852527Sdavidn			pwd->pw_gecos = gecos;
52952527Sdavidn			edited = 1;
53052527Sdavidn		}
53152527Sdavidn	}
53220253Sjoerg
533124382Siedowse	if ((arg = getarg(args, 'h')) != NULL ||
534124382Siedowse	    (arg = getarg(args, 'H')) != NULL) {
53563572Sdavidn		if (strcmp(arg->val, "-") == 0) {
53663572Sdavidn			if (!pwd->pw_passwd || *pwd->pw_passwd != '*') {
53763572Sdavidn				pwd->pw_passwd = "*";	/* No access */
53863572Sdavidn				edited = 1;
53963572Sdavidn			}
54063572Sdavidn		} else {
54120253Sjoerg			int             fd = atoi(arg->val);
542124382Siedowse			int		precrypt = (arg->ch == 'H');
54320253Sjoerg			int             b;
54420253Sjoerg			int             istty = isatty(fd);
54520253Sjoerg			struct termios  t;
54664918Sgreen			login_cap_t	*lc;
54720253Sjoerg
54820253Sjoerg			if (istty) {
54920253Sjoerg				if (tcgetattr(fd, &t) == -1)
55020253Sjoerg					istty = 0;
55120253Sjoerg				else {
55220253Sjoerg					struct termios  n = t;
55320253Sjoerg
55420253Sjoerg					/* Disable echo */
55520253Sjoerg					n.c_lflag &= ~(ECHO);
55620253Sjoerg					tcsetattr(fd, TCSANOW, &n);
557124382Siedowse					printf("%s%spassword for user %s:",
558124382Siedowse					     (mode == M_UPDATE) ? "new " : "",
559124382Siedowse					     precrypt ? "encrypted " : "",
560124382Siedowse					     pwd->pw_name);
56120253Sjoerg					fflush(stdout);
56220253Sjoerg				}
56320253Sjoerg			}
56420253Sjoerg			b = read(fd, line, sizeof(line) - 1);
56520253Sjoerg			if (istty) {	/* Restore state */
56620253Sjoerg				tcsetattr(fd, TCSANOW, &t);
56720253Sjoerg				fputc('\n', stdout);
56820253Sjoerg				fflush(stdout);
56920253Sjoerg			}
570283814Sbapt			if (b < 0)
571283814Sbapt				err(EX_IOERR, "-%c file descriptor",
572283814Sbapt				    precrypt ? 'H' : 'h');
57320253Sjoerg			line[b] = '\0';
574168045Sle			if ((p = strpbrk(line, "\r\n")) != NULL)
57520253Sjoerg				*p = '\0';
57620253Sjoerg			if (!*line)
57730259Scharnier				errx(EX_DATAERR, "empty password read on file descriptor %d", fd);
578124382Siedowse			if (precrypt) {
579124382Siedowse				if (strchr(line, ':') != NULL)
580124382Siedowse					return EX_DATAERR;
581124382Siedowse				pwd->pw_passwd = line;
582124382Siedowse			} else {
583124382Siedowse				lc = login_getpwclass(pwd);
584124382Siedowse				if (lc == NULL ||
585272833Sdes				    login_setcryptfmt(lc, "sha512", NULL) == NULL)
586124382Siedowse					warn("setting crypt(3) format");
587124382Siedowse				login_close(lc);
588124382Siedowse				pwd->pw_passwd = pw_pwcrypt(line);
589124382Siedowse			}
59052527Sdavidn			edited = 1;
59120253Sjoerg		}
59220253Sjoerg	}
59320267Sjoerg
59420267Sjoerg	/*
59520267Sjoerg	 * Special case: -N only displays & exits
59620267Sjoerg	 */
597284121Sbapt	if (conf.dryrun)
598284126Sbapt		return print_user(pwd);
59920267Sjoerg
60021330Sdavidn	if (mode == M_ADD) {
60152527Sdavidn		edited = 1;	/* Always */
60252502Sdavidn		rc = addpwent(pwd);
603283814Sbapt		if (rc == -1)
604283814Sbapt			errx(EX_IOERR, "user '%s' already exists",
605283814Sbapt			    pwd->pw_name);
606283814Sbapt		else if (rc != 0)
607283814Sbapt			err(EX_IOERR, "passwd file update");
60852502Sdavidn		if (cnf->nispasswd && *cnf->nispasswd=='/') {
60952502Sdavidn			rc = addnispwent(cnf->nispasswd, pwd);
61052502Sdavidn			if (rc == -1)
61152502Sdavidn				warnx("User '%s' already exists in NIS passwd", pwd->pw_name);
61252502Sdavidn			else
61356000Sdavidn				warn("NIS passwd update");
61452502Sdavidn			/* NOTE: we treat NIS-only update errors as non-fatal */
61552502Sdavidn		}
61652512Sdavidn	} else if (mode == M_UPDATE || mode == M_LOCK || mode == M_UNLOCK) {
61752527Sdavidn		if (edited) {	/* Only updated this if required */
618284128Sbapt			rc = chgpwent(name, pwd);
619283814Sbapt			if (rc == -1)
620283814Sbapt				errx(EX_IOERR, "user '%s' does not exist (NIS?)", pwd->pw_name);
621283814Sbapt			else if (rc != 0)
622283814Sbapt				err(EX_IOERR, "passwd file update");
62352527Sdavidn			if ( cnf->nispasswd && *cnf->nispasswd=='/') {
624284128Sbapt				rc = chgnispwent(cnf->nispasswd, name, pwd);
62552527Sdavidn				if (rc == -1)
62652527Sdavidn					warn("User '%s' not found in NIS passwd", pwd->pw_name);
62752527Sdavidn				else
62856000Sdavidn					warn("NIS passwd update");
62952527Sdavidn				/* NOTE: NIS-only update errors are not fatal */
63052527Sdavidn			}
63152502Sdavidn		}
63221330Sdavidn	}
63321330Sdavidn
63420253Sjoerg	/*
63520253Sjoerg	 * Ok, user is created or changed - now edit group file
63620253Sjoerg	 */
63720253Sjoerg
638242349Sbapt	if (mode == M_ADD || getarg(args, 'G') != NULL) {
639273779Sbapt		int i, j;
640273779Sbapt		/* First remove the user from all group */
641273779Sbapt		SETGRENT();
642273779Sbapt		while ((grp = GETGRENT()) != NULL) {
643273779Sbapt			char group[MAXLOGNAME];
644273779Sbapt			if (grp->gr_mem == NULL)
645273779Sbapt				continue;
646273779Sbapt			for (i = 0; grp->gr_mem[i] != NULL; i++) {
647273779Sbapt				if (strcmp(grp->gr_mem[i] , pwd->pw_name) != 0)
648273779Sbapt					continue;
649273779Sbapt				for (j = i; grp->gr_mem[j] != NULL ; j++)
650273779Sbapt					grp->gr_mem[j] = grp->gr_mem[j+1];
651273779Sbapt				strlcpy(group, grp->gr_name, MAXLOGNAME);
652273779Sbapt				chggrent(group, grp);
653273779Sbapt			}
654273779Sbapt		}
655273779Sbapt		ENDGRENT();
656273779Sbapt
657273779Sbapt		/* now add to group where needed */
658242349Sbapt		for (i = 0; cnf->groups[i] != NULL; i++) {
659242349Sbapt			grp = GETGRNAM(cnf->groups[i]);
660244737Sbapt			grp = gr_add(grp, pwd->pw_name);
661244737Sbapt			/*
662244737Sbapt			 * grp can only be NULL in 2 cases:
663244737Sbapt			 * - the new member is already a member
664244737Sbapt			 * - a problem with memory occurs
665244737Sbapt			 * in both cases we want to skip now.
666244737Sbapt			 */
667244737Sbapt			if (grp == NULL)
668242349Sbapt				continue;
669242349Sbapt			chggrent(cnf->groups[i], grp);
670245114Smjg			free(grp);
671242349Sbapt		}
672242349Sbapt	}
673242349Sbapt
674242349Sbapt
67561759Sdavidn	/* go get a current version of pwd */
676284128Sbapt	pwd = GETPWNAM(name);
67761759Sdavidn	if (pwd == NULL) {
67861759Sdavidn		/* This will fail when we rename, so special case that */
67961759Sdavidn		if (mode == M_UPDATE && (arg = getarg(args, 'l')) != NULL) {
680284128Sbapt			name = arg->val;		/* update new name */
681284128Sbapt			pwd = GETPWNAM(name);	/* refetch renamed rec */
68261759Sdavidn		}
68361759Sdavidn	}
68461759Sdavidn	if (pwd == NULL)	/* can't go on without this */
685284128Sbapt		errx(EX_NOUSER, "user '%s' disappeared during update", name);
68620253Sjoerg
68744229Sdavidn	grp = GETGRGID(pwd->pw_gid);
688283842Sbapt	pw_log(cnf, mode, W_USER, "%s(%u):%s(%u):%s:%s:%s",
689283842Sbapt	       pwd->pw_name, pwd->pw_uid,
690283842Sbapt	    grp ? grp->gr_name : "unknown", (grp ? grp->gr_gid : (uid_t)-1),
69120253Sjoerg	       pwd->pw_gecos, pwd->pw_dir, pwd->pw_shell);
69220253Sjoerg
69320253Sjoerg	/*
69420253Sjoerg	 * If adding, let's touch and chown the user's mail file. This is not
69520253Sjoerg	 * strictly necessary under BSD with a 0755 maildir but it also
69620253Sjoerg	 * doesn't hurt anything to create the empty mailfile
69720253Sjoerg	 */
69820253Sjoerg	if (mode == M_ADD) {
699283961Sbapt		if (PWALTDIR() != PWF_ALT) {
700283961Sbapt			arg = getarg(args, 'R');
701283961Sbapt			snprintf(path, sizeof(path), "%s%s/%s",
702283961Sbapt			    arg ? arg->val : "", _PATH_MAILDIR, pwd->pw_name);
703283961Sbapt			close(open(path, O_RDWR | O_CREAT, 0600));	/* Preserve contents &
70444229Sdavidn									 * mtime */
705283961Sbapt			chown(path, pwd->pw_uid, pwd->pw_gid);
70620253Sjoerg		}
70720253Sjoerg	}
70852527Sdavidn
70920253Sjoerg	/*
71082868Sdd	 * Let's create and populate the user's home directory. Note
71120253Sjoerg	 * that this also `works' for editing users if -m is used, but
71220253Sjoerg	 * existing files will *not* be overwritten.
71320253Sjoerg	 */
714283961Sbapt	if (PWALTDIR() != PWF_ALT && getarg(args, 'm') != NULL && pwd->pw_dir &&
715283961Sbapt	    *pwd->pw_dir == '/' && pwd->pw_dir[1])
716284118Sbapt		create_and_populate_homedir(mode, pwd);
71752527Sdavidn
71882868Sdd	/*
71982868Sdd	 * Finally, send mail to the new user as well, if we are asked to
72082868Sdd	 */
72182868Sdd	if (mode == M_ADD && !PWALTDIR() && cnf->newmail && *cnf->newmail && (fp = fopen(cnf->newmail, "r")) != NULL) {
72282868Sdd		FILE           *pfp = popen(_PATH_SENDMAIL " -t", "w");
72382868Sdd
72482868Sdd		if (pfp == NULL)
72582868Sdd			warn("sendmail");
72682868Sdd		else {
72782868Sdd			fprintf(pfp, "From: root\n" "To: %s\n" "Subject: Welcome!\n\n", pwd->pw_name);
72882868Sdd			while (fgets(line, sizeof(line), fp) != NULL) {
72982868Sdd				/* Do substitutions? */
73082868Sdd				fputs(line, pfp);
73182868Sdd			}
73282868Sdd			pclose(pfp);
733283842Sbapt			pw_log(cnf, mode, W_USER, "%s(%u) new user mail sent",
734283842Sbapt			    pwd->pw_name, pwd->pw_uid);
73582868Sdd		}
73682868Sdd		fclose(fp);
73782868Sdd	}
73882868Sdd
73920267Sjoerg	return EXIT_SUCCESS;
74020253Sjoerg}
74120253Sjoerg
74220253Sjoerg
74320253Sjoergstatic          uid_t
74420253Sjoergpw_uidpolicy(struct userconf * cnf, struct cargs * args)
74520253Sjoerg{
74620253Sjoerg	struct passwd  *pwd;
74720253Sjoerg	uid_t           uid = (uid_t) - 1;
74820253Sjoerg	struct carg    *a_uid = getarg(args, 'u');
74920253Sjoerg
75020253Sjoerg	/*
75120253Sjoerg	 * Check the given uid, if any
75220253Sjoerg	 */
75320253Sjoerg	if (a_uid != NULL) {
75420253Sjoerg		uid = (uid_t) atol(a_uid->val);
75520253Sjoerg
75644229Sdavidn		if ((pwd = GETPWUID(uid)) != NULL && getarg(args, 'o') == NULL)
757283842Sbapt			errx(EX_DATAERR, "uid `%u' has already been allocated", pwd->pw_uid);
75820253Sjoerg	} else {
75920253Sjoerg		struct bitmap   bm;
76020253Sjoerg
76120253Sjoerg		/*
76220253Sjoerg		 * We need to allocate the next available uid under one of
76320253Sjoerg		 * two policies a) Grab the first unused uid b) Grab the
76420253Sjoerg		 * highest possible unused uid
76520253Sjoerg		 */
76620253Sjoerg		if (cnf->min_uid >= cnf->max_uid) {	/* Sanity
76720253Sjoerg							 * claus^H^H^H^Hheck */
76820253Sjoerg			cnf->min_uid = 1000;
76920253Sjoerg			cnf->max_uid = 32000;
77020253Sjoerg		}
77120253Sjoerg		bm = bm_alloc(cnf->max_uid - cnf->min_uid + 1);
77220253Sjoerg
77320253Sjoerg		/*
77420253Sjoerg		 * Now, let's fill the bitmap from the password file
77520253Sjoerg		 */
77644229Sdavidn		SETPWENT();
77744229Sdavidn		while ((pwd = GETPWENT()) != NULL)
77844229Sdavidn			if (pwd->pw_uid >= (uid_t) cnf->min_uid && pwd->pw_uid <= (uid_t) cnf->max_uid)
77920253Sjoerg				bm_setbit(&bm, pwd->pw_uid - cnf->min_uid);
78044229Sdavidn		ENDPWENT();
78120253Sjoerg
78220253Sjoerg		/*
78320253Sjoerg		 * Then apply the policy, with fallback to reuse if necessary
78420253Sjoerg		 */
78520253Sjoerg		if (cnf->reuse_uids || (uid = (uid_t) (bm_lastset(&bm) + cnf->min_uid + 1)) > cnf->max_uid)
78620253Sjoerg			uid = (uid_t) (bm_firstunset(&bm) + cnf->min_uid);
78720253Sjoerg
78820253Sjoerg		/*
78920253Sjoerg		 * Another sanity check
79020253Sjoerg		 */
79120253Sjoerg		if (uid < cnf->min_uid || uid > cnf->max_uid)
79230259Scharnier			errx(EX_SOFTWARE, "unable to allocate a new uid - range fully used");
79320253Sjoerg		bm_dealloc(&bm);
79420253Sjoerg	}
79520253Sjoerg	return uid;
79620253Sjoerg}
79720253Sjoerg
79820253Sjoerg
79920253Sjoergstatic          uid_t
800284118Sbaptpw_gidpolicy(struct cargs * args, char *nam, gid_t prefer)
80120253Sjoerg{
80220253Sjoerg	struct group   *grp;
80320253Sjoerg	gid_t           gid = (uid_t) - 1;
80420253Sjoerg	struct carg    *a_gid = getarg(args, 'g');
805284118Sbapt	struct userconf	*cnf = conf.userconf;
80620253Sjoerg
80720253Sjoerg	/*
80820253Sjoerg	 * If no arg given, see if default can help out
80920253Sjoerg	 */
81020253Sjoerg	if (a_gid == NULL && cnf->default_group && *cnf->default_group)
81120253Sjoerg		a_gid = addarg(args, 'g', cnf->default_group);
81220253Sjoerg
81320253Sjoerg	/*
81420253Sjoerg	 * Check the given gid, if any
81520253Sjoerg	 */
81644229Sdavidn	SETGRENT();
81720253Sjoerg	if (a_gid != NULL) {
81844229Sdavidn		if ((grp = GETGRNAM(a_gid->val)) == NULL) {
81920253Sjoerg			gid = (gid_t) atol(a_gid->val);
82061957Sache			if ((gid == 0 && !isdigit((unsigned char)*a_gid->val)) || (grp = GETGRGID(gid)) == NULL)
82130259Scharnier				errx(EX_NOUSER, "group `%s' is not defined", a_gid->val);
82220253Sjoerg		}
82320253Sjoerg		gid = grp->gr_gid;
824262865Sjulian	} else if ((grp = GETGRNAM(nam)) != NULL &&
825262865Sjulian	    (grp->gr_mem == NULL || grp->gr_mem[0] == NULL)) {
82620267Sjoerg		gid = grp->gr_gid;  /* Already created? Use it anyway... */
82720253Sjoerg	} else {
82820253Sjoerg		struct cargs    grpargs;
82920253Sjoerg		char            tmp[32];
83020253Sjoerg
83120253Sjoerg		LIST_INIT(&grpargs);
83220253Sjoerg
83320253Sjoerg		/*
83420253Sjoerg		 * We need to auto-create a group with the user's name. We
83520253Sjoerg		 * can send all the appropriate output to our sister routine
83620253Sjoerg		 * bit first see if we can create a group with gid==uid so we
83720253Sjoerg		 * can keep the user and group ids in sync. We purposely do
83820253Sjoerg		 * NOT check the gid range if we can force the sync. If the
83920253Sjoerg		 * user's name dups an existing group, then the group add
84020253Sjoerg		 * function will happily handle that case for us and exit.
84120253Sjoerg		 */
84244229Sdavidn		if (GETGRGID(prefer) == NULL) {
843282700Sbapt			snprintf(tmp, sizeof(tmp), "%u", prefer);
84420253Sjoerg			addarg(&grpargs, 'g', tmp);
84520253Sjoerg		}
846284121Sbapt		if (conf.dryrun) {
84720267Sjoerg			addarg(&grpargs, 'q', NULL);
848284128Sbapt			gid = pw_group(M_NEXT, nam, -1, &grpargs);
84920267Sjoerg		}
85020267Sjoerg		else
85120267Sjoerg		{
852284128Sbapt			pw_group(M_ADD, nam, -1, &grpargs);
85344229Sdavidn			if ((grp = GETGRNAM(nam)) != NULL)
85420267Sjoerg				gid = grp->gr_gid;
85520267Sjoerg		}
85670486Sben		a_gid = LIST_FIRST(&grpargs);
85720253Sjoerg		while (a_gid != NULL) {
85870486Sben			struct carg    *t = LIST_NEXT(a_gid, list);
85920253Sjoerg			LIST_REMOVE(a_gid, list);
86020253Sjoerg			a_gid = t;
86120253Sjoerg		}
86220253Sjoerg	}
86344229Sdavidn	ENDGRENT();
86420253Sjoerg	return gid;
86520253Sjoerg}
86620253Sjoerg
86720253Sjoerg
86820253Sjoergstatic          time_t
86920253Sjoergpw_pwdpolicy(struct userconf * cnf, struct cargs * args)
87020253Sjoerg{
87120253Sjoerg	time_t          result = 0;
87220253Sjoerg	time_t          now = time(NULL);
87327831Sdavidn	struct carg    *arg = getarg(args, 'p');
87420253Sjoerg
87520253Sjoerg	if (arg != NULL) {
87620253Sjoerg		if ((result = parse_date(now, arg->val)) == now)
87730259Scharnier			errx(EX_DATAERR, "invalid date/time `%s'", arg->val);
87820253Sjoerg	} else if (cnf->password_days > 0)
87920253Sjoerg		result = now + ((long) cnf->password_days * 86400L);
88020253Sjoerg	return result;
88120253Sjoerg}
88220253Sjoerg
88320253Sjoerg
88420253Sjoergstatic          time_t
88520253Sjoergpw_exppolicy(struct userconf * cnf, struct cargs * args)
88620253Sjoerg{
88720253Sjoerg	time_t          result = 0;
88820253Sjoerg	time_t          now = time(NULL);
88920253Sjoerg	struct carg    *arg = getarg(args, 'e');
89020253Sjoerg
89120253Sjoerg	if (arg != NULL) {
89220253Sjoerg		if ((result = parse_date(now, arg->val)) == now)
89330259Scharnier			errx(EX_DATAERR, "invalid date/time `%s'", arg->val);
89420253Sjoerg	} else if (cnf->expire_days > 0)
89520253Sjoerg		result = now + ((long) cnf->expire_days * 86400L);
89620253Sjoerg	return result;
89720253Sjoerg}
89820253Sjoerg
89920253Sjoerg
90020253Sjoergstatic char    *
90120253Sjoergpw_homepolicy(struct userconf * cnf, struct cargs * args, char const * user)
90220253Sjoerg{
90320253Sjoerg	struct carg    *arg = getarg(args, 'd');
904282699Sbapt	static char     home[128];
90520253Sjoerg
90620253Sjoerg	if (arg)
907282699Sbapt		return (arg->val);
90820253Sjoerg
909282699Sbapt	if (cnf->home == NULL || *cnf->home == '\0')
910282699Sbapt		errx(EX_CONFIG, "no base home directory set");
911282699Sbapt	snprintf(home, sizeof(home), "%s/%s", cnf->home, user);
912282699Sbapt
913282699Sbapt	return (home);
91420253Sjoerg}
91520253Sjoerg
91620253Sjoergstatic char    *
91720253Sjoergshell_path(char const * path, char *shells[], char *sh)
91820253Sjoerg{
91920253Sjoerg	if (sh != NULL && (*sh == '/' || *sh == '\0'))
92020253Sjoerg		return sh;	/* specified full path or forced none */
92120253Sjoerg	else {
92220253Sjoerg		char           *p;
92320253Sjoerg		char            paths[_UC_MAXLINE];
92420253Sjoerg
92520253Sjoerg		/*
92620253Sjoerg		 * We need to search paths
92720253Sjoerg		 */
928130633Srobert		strlcpy(paths, path, sizeof(paths));
92920253Sjoerg		for (p = strtok(paths, ": \t\r\n"); p != NULL; p = strtok(NULL, ": \t\r\n")) {
93020253Sjoerg			int             i;
93120253Sjoerg			static char     shellpath[256];
93220253Sjoerg
93320253Sjoerg			if (sh != NULL) {
934282700Sbapt				snprintf(shellpath, sizeof(shellpath), "%s/%s", p, sh);
93520253Sjoerg				if (access(shellpath, X_OK) == 0)
93620253Sjoerg					return shellpath;
93720253Sjoerg			} else
93820253Sjoerg				for (i = 0; i < _UC_MAXSHELLS && shells[i] != NULL; i++) {
939282700Sbapt					snprintf(shellpath, sizeof(shellpath), "%s/%s", p, shells[i]);
94020253Sjoerg					if (access(shellpath, X_OK) == 0)
94120253Sjoerg						return shellpath;
94220253Sjoerg				}
94320253Sjoerg		}
94420253Sjoerg		if (sh == NULL)
94530259Scharnier			errx(EX_OSFILE, "can't find shell `%s' in shell paths", sh);
94630259Scharnier		errx(EX_CONFIG, "no default shell available or defined");
94720253Sjoerg		return NULL;
94820253Sjoerg	}
94920253Sjoerg}
95020253Sjoerg
95120253Sjoerg
95220253Sjoergstatic char    *
95320253Sjoergpw_shellpolicy(struct userconf * cnf, struct cargs * args, char *newshell)
95420253Sjoerg{
95520253Sjoerg	char           *sh = newshell;
95620253Sjoerg	struct carg    *arg = getarg(args, 's');
95720253Sjoerg
95820253Sjoerg	if (newshell == NULL && arg != NULL)
95920253Sjoerg		sh = arg->val;
96020253Sjoerg	return shell_path(cnf->shelldir, cnf->shells, sh ? sh : cnf->shell_default);
96120253Sjoerg}
96220253Sjoerg
963179365Santoine#define	SALTSIZE	32
96420253Sjoerg
965179365Santoinestatic char const chars[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ./";
966179365Santoine
96720253Sjoergchar           *
96820253Sjoergpw_pwcrypt(char *password)
96920253Sjoerg{
97020253Sjoerg	int             i;
971179365Santoine	char            salt[SALTSIZE + 1];
972231994Skevlo	char		*cryptpw;
97320253Sjoerg
97420253Sjoerg	static char     buf[256];
97520253Sjoerg
97620253Sjoerg	/*
97720253Sjoerg	 * Calculate a salt value
97820253Sjoerg	 */
979179365Santoine	for (i = 0; i < SALTSIZE; i++)
980181785Sache		salt[i] = chars[arc4random_uniform(sizeof(chars) - 1)];
981179365Santoine	salt[SALTSIZE] = '\0';
98220253Sjoerg
983231994Skevlo	cryptpw = crypt(password, salt);
984231994Skevlo	if (cryptpw == NULL)
985231994Skevlo		errx(EX_CONFIG, "crypt(3) failure");
986231994Skevlo	return strcpy(buf, cryptpw);
98720253Sjoerg}
98820253Sjoerg
98920590Sdavidn
99020253Sjoergstatic char    *
99120253Sjoergpw_password(struct userconf * cnf, struct cargs * args, char const * user)
99220253Sjoerg{
99320253Sjoerg	int             i, l;
99420253Sjoerg	char            pwbuf[32];
99520253Sjoerg
99620253Sjoerg	switch (cnf->default_password) {
99720253Sjoerg	case -1:		/* Random password */
99873563Skris		l = (arc4random() % 8 + 8);	/* 8 - 16 chars */
99920253Sjoerg		for (i = 0; i < l; i++)
1000181785Sache			pwbuf[i] = chars[arc4random_uniform(sizeof(chars)-1)];
100120253Sjoerg		pwbuf[i] = '\0';
100220253Sjoerg
100320253Sjoerg		/*
100420253Sjoerg		 * We give this information back to the user
100520253Sjoerg		 */
1006124382Siedowse		if (getarg(args, 'h') == NULL && getarg(args, 'H') == NULL &&
1007284121Sbapt		    !conf.dryrun) {
100861957Sache			if (isatty(STDOUT_FILENO))
100920712Sdavidn				printf("Password for '%s' is: ", user);
101020253Sjoerg			printf("%s\n", pwbuf);
101120253Sjoerg			fflush(stdout);
101220253Sjoerg		}
101320253Sjoerg		break;
101420253Sjoerg
101520253Sjoerg	case -2:		/* No password at all! */
101620253Sjoerg		return "";
101720253Sjoerg
101820253Sjoerg	case 0:		/* No login - default */
101920253Sjoerg	default:
102020253Sjoerg		return "*";
102120253Sjoerg
102220253Sjoerg	case 1:		/* user's name */
1023130633Srobert		strlcpy(pwbuf, user, sizeof(pwbuf));
102420253Sjoerg		break;
102520253Sjoerg	}
102620253Sjoerg	return pw_pwcrypt(pwbuf);
102720253Sjoerg}
102820253Sjoerg
1029284111Sbaptstatic int
1030284128Sbaptdelete_user(struct userconf *cnf, struct passwd *pwd, char *name,
1031284111Sbapt    int delete, int mode)
1032284111Sbapt{
1033284111Sbapt	char		 file[MAXPATHLEN];
1034284111Sbapt	char		 home[MAXPATHLEN];
1035284111Sbapt	uid_t		 uid = pwd->pw_uid;
1036284111Sbapt	struct group	*gr, *grp;
1037284111Sbapt	char		 grname[LOGNAMESIZE];
1038284111Sbapt	int		 rc;
1039284111Sbapt	struct stat	 st;
104020253Sjoerg
1041284111Sbapt	if (strcmp(pwd->pw_name, "root") == 0)
1042284111Sbapt		errx(EX_DATAERR, "cannot remove user 'root'");
1043284111Sbapt
1044284111Sbapt	if (!PWALTDIR()) {
1045284111Sbapt		/*
1046284111Sbapt		 * Remove opie record from /etc/opiekeys
1047284111Sbapt		*/
1048284111Sbapt
1049284111Sbapt		rmopie(pwd->pw_name);
1050284111Sbapt
1051284111Sbapt		/*
1052284111Sbapt		 * Remove crontabs
1053284111Sbapt		 */
1054284111Sbapt		snprintf(file, sizeof(file), "/var/cron/tabs/%s", pwd->pw_name);
1055284111Sbapt		if (access(file, F_OK) == 0) {
1056284111Sbapt			snprintf(file, sizeof(file), "crontab -u %s -r", pwd->pw_name);
1057284111Sbapt			system(file);
1058284111Sbapt		}
1059284111Sbapt	}
1060284111Sbapt	/*
1061284111Sbapt	 * Save these for later, since contents of pwd may be
1062284111Sbapt	 * invalidated by deletion
1063284111Sbapt	 */
1064284111Sbapt	snprintf(file, sizeof(file), "%s/%s", _PATH_MAILDIR, pwd->pw_name);
1065284111Sbapt	strlcpy(home, pwd->pw_dir, sizeof(home));
1066284111Sbapt	gr = GETGRGID(pwd->pw_gid);
1067284111Sbapt	if (gr != NULL)
1068284111Sbapt		strlcpy(grname, gr->gr_name, LOGNAMESIZE);
1069284111Sbapt	else
1070284111Sbapt		grname[0] = '\0';
1071284111Sbapt
1072284111Sbapt	rc = delpwent(pwd);
1073284111Sbapt	if (rc == -1)
1074284111Sbapt		err(EX_IOERR, "user '%s' does not exist", pwd->pw_name);
1075284111Sbapt	else if (rc != 0)
1076284111Sbapt		err(EX_IOERR, "passwd update");
1077284111Sbapt
1078284111Sbapt	if (cnf->nispasswd && *cnf->nispasswd=='/') {
1079284128Sbapt		rc = delnispwent(cnf->nispasswd, name);
1080284111Sbapt		if (rc == -1)
1081284111Sbapt			warnx("WARNING: user '%s' does not exist in NIS passwd", pwd->pw_name);
1082284111Sbapt		else if (rc != 0)
1083284111Sbapt			warn("WARNING: NIS passwd update");
1084284111Sbapt		/* non-fatal */
1085284111Sbapt	}
1086284111Sbapt
1087284128Sbapt	grp = GETGRNAM(name);
1088284111Sbapt	if (grp != NULL &&
1089284111Sbapt	    (grp->gr_mem == NULL || *grp->gr_mem == NULL) &&
1090284128Sbapt	    strcmp(name, grname) == 0)
1091284128Sbapt		delgrent(GETGRNAM(name));
1092284111Sbapt	SETGRENT();
1093284111Sbapt	while ((grp = GETGRENT()) != NULL) {
1094284111Sbapt		int i, j;
1095284111Sbapt		char group[MAXLOGNAME];
1096284113Sbapt		if (grp->gr_mem == NULL)
1097284113Sbapt			continue;
1098284113Sbapt
1099284113Sbapt		for (i = 0; grp->gr_mem[i] != NULL; i++) {
1100284128Sbapt			if (strcmp(grp->gr_mem[i], name) != 0)
1101284113Sbapt				continue;
1102284113Sbapt
1103284113Sbapt			for (j = i; grp->gr_mem[j] != NULL; j++)
1104284113Sbapt				grp->gr_mem[j] = grp->gr_mem[j+1];
1105284113Sbapt			strlcpy(group, grp->gr_name, MAXLOGNAME);
1106284113Sbapt			chggrent(group, grp);
1107284111Sbapt		}
1108284111Sbapt	}
1109284111Sbapt	ENDGRENT();
1110284111Sbapt
1111284128Sbapt	pw_log(cnf, mode, W_USER, "%s(%u) account removed", name, uid);
1112284111Sbapt
1113284117Sbapt	if (!PWALTDIR()) {
1114284111Sbapt		/*
1115284111Sbapt		 * Remove mail file
1116284111Sbapt		 */
1117284111Sbapt		remove(file);
1118284111Sbapt
1119284111Sbapt		/*
1120284111Sbapt		 * Remove at jobs
1121284111Sbapt		 */
1122284111Sbapt		if (getpwuid(uid) == NULL)
1123284111Sbapt			rmat(uid);
1124284111Sbapt
1125284111Sbapt		/*
1126284111Sbapt		 * Remove home directory and contents
1127284111Sbapt		 */
1128284112Sbapt		if (delete && *home == '/' && getpwuid(uid) == NULL &&
1129284112Sbapt		    stat(home, &st) != -1) {
1130284112Sbapt			rm_r(home, uid);
1131284112Sbapt			pw_log(cnf, mode, W_USER, "%s(%u) home '%s' %sremoved",
1132284128Sbapt			       name, uid, home,
1133284112Sbapt			       stat(home, &st) == -1 ? "" : "not completely ");
1134284111Sbapt		}
1135284111Sbapt	}
1136284111Sbapt
1137284111Sbapt	return (EXIT_SUCCESS);
1138284111Sbapt}
1139284111Sbapt
114020253Sjoergstatic int
1141284124Sbaptprint_user(struct passwd * pwd)
114220253Sjoerg{
1143284122Sbapt	if (!conf.pretty) {
1144242349Sbapt		char            *buf;
114520253Sjoerg
1146284124Sbapt		if (!conf.v7)
1147242349Sbapt			pwd->pw_passwd = (pwd->pw_passwd == NULL) ? "" : "*";
1148242349Sbapt
1149284124Sbapt		buf = conf.v7 ? pw_make_v7(pwd) : pw_make(pwd);
1150242349Sbapt		printf("%s\n", buf);
1151242349Sbapt		free(buf);
115220253Sjoerg	} else {
115320267Sjoerg		int		j;
115420253Sjoerg		char           *p;
115544229Sdavidn		struct group   *grp = GETGRGID(pwd->pw_gid);
115620253Sjoerg		char            uname[60] = "User &", office[60] = "[None]",
115720253Sjoerg		                wphone[60] = "[None]", hphone[60] = "[None]";
115820590Sdavidn		char		acexpire[32] = "[None]", pwexpire[32] = "[None]";
115920590Sdavidn		struct tm *    tptr;
116020253Sjoerg
116120253Sjoerg		if ((p = strtok(pwd->pw_gecos, ",")) != NULL) {
1162130633Srobert			strlcpy(uname, p, sizeof(uname));
116320253Sjoerg			if ((p = strtok(NULL, ",")) != NULL) {
1164130633Srobert				strlcpy(office, p, sizeof(office));
116520253Sjoerg				if ((p = strtok(NULL, ",")) != NULL) {
1166130633Srobert					strlcpy(wphone, p, sizeof(wphone));
116720253Sjoerg					if ((p = strtok(NULL, "")) != NULL) {
1168130633Srobert						strlcpy(hphone, p,
1169130633Srobert						    sizeof(hphone));
117020253Sjoerg					}
117120253Sjoerg				}
117220253Sjoerg			}
117320253Sjoerg		}
117420253Sjoerg		/*
117520253Sjoerg		 * Handle '&' in gecos field
117620253Sjoerg		 */
117720253Sjoerg		if ((p = strchr(uname, '&')) != NULL) {
117820253Sjoerg			int             l = strlen(pwd->pw_name);
117920253Sjoerg			int             m = strlen(p);
118020253Sjoerg
118120253Sjoerg			memmove(p + l, p + 1, m);
118220253Sjoerg			memmove(p, pwd->pw_name, l);
118361957Sache			*p = (char) toupper((unsigned char)*p);
118420253Sjoerg		}
118520590Sdavidn		if (pwd->pw_expire > (time_t)0 && (tptr = localtime(&pwd->pw_expire)) != NULL)
118674569Sache			strftime(acexpire, sizeof acexpire, "%c", tptr);
118761957Sache		if (pwd->pw_change > (time_t)0 && (tptr = localtime(&pwd->pw_change)) != NULL)
118874569Sache			strftime(pwexpire, sizeof pwexpire, "%c", tptr);
1189283842Sbapt		printf("Login Name: %-15s   #%-12u Group: %-15s   #%u\n"
119020747Sdavidn		       " Full Name: %s\n"
119120747Sdavidn		       "      Home: %-26.26s      Class: %s\n"
119220747Sdavidn		       "     Shell: %-26.26s     Office: %s\n"
119320747Sdavidn		       "Work Phone: %-26.26s Home Phone: %s\n"
119420747Sdavidn		       "Acc Expire: %-26.26s Pwd Expire: %s\n",
1195283842Sbapt		       pwd->pw_name, pwd->pw_uid,
1196283842Sbapt		       grp ? grp->gr_name : "(invalid)", pwd->pw_gid,
119720253Sjoerg		       uname, pwd->pw_dir, pwd->pw_class,
119820590Sdavidn		       pwd->pw_shell, office, wphone, hphone,
119920590Sdavidn		       acexpire, pwexpire);
120044229Sdavidn	        SETGRENT();
120120267Sjoerg		j = 0;
120244229Sdavidn		while ((grp=GETGRENT()) != NULL)
120320267Sjoerg		{
120420267Sjoerg			int     i = 0;
1205262865Sjulian			if (grp->gr_mem != NULL) {
1206262865Sjulian				while (grp->gr_mem[i] != NULL)
120720267Sjoerg				{
1208262865Sjulian					if (strcmp(grp->gr_mem[i], pwd->pw_name)==0)
1209262865Sjulian					{
1210262865Sjulian						printf(j++ == 0 ? "    Groups: %s" : ",%s", grp->gr_name);
1211262865Sjulian						break;
1212262865Sjulian					}
1213262865Sjulian					++i;
121420267Sjoerg				}
121520267Sjoerg			}
121620267Sjoerg		}
121744229Sdavidn		ENDGRENT();
121861957Sache		printf("%s", j ? "\n" : "");
121920253Sjoerg	}
122020267Sjoerg	return EXIT_SUCCESS;
122120253Sjoerg}
122220253Sjoerg
1223284110Sbaptchar *
1224284110Sbaptpw_checkname(char *name, int gecos)
122520253Sjoerg{
1226109961Sgad	char showch[8];
1227284110Sbapt	const char *badchars, *ch, *showtype;
1228109961Sgad	int reject;
122920253Sjoerg
1230109961Sgad	ch = name;
1231109961Sgad	reject = 0;
1232109961Sgad	if (gecos) {
1233109961Sgad		/* See if the name is valid as a gecos (comment) field. */
1234109961Sgad		badchars = ":!@";
1235109961Sgad		showtype = "gecos field";
1236109961Sgad	} else {
1237109961Sgad		/* See if the name is valid as a userid or group. */
1238109961Sgad		badchars = " ,\t:+&#%$^()!@~*?<>=|\\/\"";
1239109961Sgad		showtype = "userid/group name";
1240109961Sgad		/* Userids and groups can not have a leading '-'. */
1241109961Sgad		if (*ch == '-')
1242109961Sgad			reject = 1;
124320253Sjoerg	}
1244109961Sgad	if (!reject) {
1245109961Sgad		while (*ch) {
1246109961Sgad			if (strchr(badchars, *ch) != NULL || *ch < ' ' ||
1247109961Sgad			    *ch == 127) {
1248109961Sgad				reject = 1;
1249109961Sgad				break;
1250109961Sgad			}
1251109961Sgad			/* 8-bit characters are only allowed in GECOS fields */
1252109961Sgad			if (!gecos && (*ch & 0x80)) {
1253109961Sgad				reject = 1;
1254109961Sgad				break;
1255109961Sgad			}
1256109961Sgad			ch++;
1257109961Sgad		}
1258109961Sgad	}
1259109961Sgad	/*
1260109961Sgad	 * A `$' is allowed as the final character for userids and groups,
1261109961Sgad	 * mainly for the benefit of samba.
1262109961Sgad	 */
1263109961Sgad	if (reject && !gecos) {
1264109961Sgad		if (*ch == '$' && *(ch + 1) == '\0') {
1265109961Sgad			reject = 0;
1266109961Sgad			ch++;
1267109961Sgad		}
1268109961Sgad	}
1269109961Sgad	if (reject) {
1270109961Sgad		snprintf(showch, sizeof(showch), (*ch >= ' ' && *ch < 127)
1271109961Sgad		    ? "`%c'" : "0x%02x", *ch);
1272228673Sdim		errx(EX_DATAERR, "invalid character %s at position %td in %s",
1273109961Sgad		    showch, (ch - name), showtype);
1274109961Sgad	}
1275109961Sgad	if (!gecos && (ch - name) > LOGNAMESIZE)
1276109961Sgad		errx(EX_DATAERR, "name too long `%s' (max is %d)", name,
1277109961Sgad		    LOGNAMESIZE);
1278284110Sbapt
1279284110Sbapt	return (name);
128020253Sjoerg}
128120253Sjoerg
128220253Sjoerg
128320253Sjoergstatic void
128420253Sjoergrmat(uid_t uid)
128520253Sjoerg{
128620253Sjoerg	DIR            *d = opendir("/var/at/jobs");
128720253Sjoerg
128820253Sjoerg	if (d != NULL) {
128920253Sjoerg		struct dirent  *e;
129020253Sjoerg
129120253Sjoerg		while ((e = readdir(d)) != NULL) {
129220253Sjoerg			struct stat     st;
129320253Sjoerg
129420253Sjoerg			if (strncmp(e->d_name, ".lock", 5) != 0 &&
129520253Sjoerg			    stat(e->d_name, &st) == 0 &&
129620253Sjoerg			    !S_ISDIR(st.st_mode) &&
129720253Sjoerg			    st.st_uid == uid) {
129820253Sjoerg				char            tmp[MAXPATHLEN];
129920253Sjoerg
1300282700Sbapt				snprintf(tmp, sizeof(tmp), "/usr/bin/atrm %s", e->d_name);
130120253Sjoerg				system(tmp);
130220253Sjoerg			}
130320253Sjoerg		}
130420253Sjoerg		closedir(d);
130520253Sjoerg	}
130620253Sjoerg}
130720747Sdavidn
130820747Sdavidnstatic void
130985145Sachermopie(char const * name)
131020747Sdavidn{
131185145Sache	static const char etcopie[] = "/etc/opiekeys";
131285145Sache	FILE   *fp = fopen(etcopie, "r+");
131320747Sdavidn
131421052Sdavidn	if (fp != NULL) {
131521052Sdavidn		char	tmp[1024];
131621052Sdavidn		off_t	atofs = 0;
131721052Sdavidn		int	length = strlen(name);
131820747Sdavidn
131921052Sdavidn		while (fgets(tmp, sizeof tmp, fp) != NULL) {
132021052Sdavidn			if (strncmp(name, tmp, length) == 0 && tmp[length]==' ') {
132121052Sdavidn				if (fseek(fp, atofs, SEEK_SET) == 0) {
132221052Sdavidn					fwrite("#", 1, 1, fp);	/* Comment username out */
132321052Sdavidn				}
132421052Sdavidn				break;
132520747Sdavidn			}
132621052Sdavidn			atofs = ftell(fp);
132720747Sdavidn		}
132821052Sdavidn		/*
132921052Sdavidn		 * If we got an error of any sort, don't update!
133021052Sdavidn		 */
133121052Sdavidn		fclose(fp);
133220747Sdavidn	}
133320747Sdavidn}
133420747Sdavidn
1335