pw_user.c revision 242349
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 242349 2012-10-30 08:00:53Z 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>
4330259Scharnier#include <unistd.h>
4464918Sgreen#include <login_cap.h>
45242349Sbapt#include <pwd.h>
46242349Sbapt#include <grp.h>
47242349Sbapt#include <libutil.h>
4820253Sjoerg#include "pw.h"
4920253Sjoerg#include "bitmap.h"
5020253Sjoerg
5123318Sache#define LOGNAMESIZE (MAXLOGNAME-1)
5222394Sdavidn
5352512Sdavidnstatic		char locked_str[] = "*LOCKED*";
5424214Sache
5544386Sdavidnstatic int      print_user(struct passwd * pwd, int pretty, int v7);
5620253Sjoergstatic uid_t    pw_uidpolicy(struct userconf * cnf, struct cargs * args);
5720253Sjoergstatic uid_t    pw_gidpolicy(struct userconf * cnf, struct cargs * args, char *nam, gid_t prefer);
5820253Sjoergstatic time_t   pw_pwdpolicy(struct userconf * cnf, struct cargs * args);
5920253Sjoergstatic time_t   pw_exppolicy(struct userconf * cnf, struct cargs * args);
6020253Sjoergstatic char    *pw_homepolicy(struct userconf * cnf, struct cargs * args, char const * user);
6120253Sjoergstatic char    *pw_shellpolicy(struct userconf * cnf, struct cargs * args, char *newshell);
6220253Sjoergstatic char    *pw_password(struct userconf * cnf, struct cargs * args, char const * user);
6320253Sjoergstatic char    *shell_path(char const * path, char *shells[], char *sh);
6420253Sjoergstatic void     rmat(uid_t uid);
6585145Sachestatic void     rmopie(char const * name);
6620253Sjoerg
6720253Sjoerg/*-
6820253Sjoerg * -C config      configuration file
6920253Sjoerg * -q             quiet operation
7020253Sjoerg * -n name        login name
7120253Sjoerg * -u uid         user id
7220253Sjoerg * -c comment     user name/comment
7320253Sjoerg * -d directory   home directory
7420253Sjoerg * -e date        account expiry date
7520253Sjoerg * -p date        password expiry date
7620253Sjoerg * -g grp         primary group
7720253Sjoerg * -G grp1,grp2   additional groups
7820253Sjoerg * -m [ -k dir ]  create and set up home
7920253Sjoerg * -s shell       name of login shell
8020253Sjoerg * -o             duplicate uid ok
8120253Sjoerg * -L class       user class
8220253Sjoerg * -l name        new login name
8320253Sjoerg * -h fd          password filehandle
84124382Siedowse * -H fd          encrypted password filehandle
8520253Sjoerg * -F             force print or add
8620253Sjoerg *   Setting defaults:
8720253Sjoerg * -D             set user defaults
8820253Sjoerg * -b dir         default home root dir
8920253Sjoerg * -e period      default expiry period
9020253Sjoerg * -p period      default password change period
9120253Sjoerg * -g group       default group
9220253Sjoerg * -G             grp1,grp2.. default additional groups
9320253Sjoerg * -L class       default login class
9420253Sjoerg * -k dir         default home skeleton
9520253Sjoerg * -s shell       default shell
9620253Sjoerg * -w method      default password method
9720253Sjoerg */
9820253Sjoerg
9920253Sjoergint
10020253Sjoergpw_user(struct userconf * cnf, int mode, struct cargs * args)
10120253Sjoerg{
10252527Sdavidn	int	        rc, edited = 0;
10320253Sjoerg	char           *p = NULL;
10452512Sdavidn	char					 *passtmp;
10520253Sjoerg	struct carg    *a_name;
10620253Sjoerg	struct carg    *a_uid;
10720253Sjoerg	struct carg    *arg;
10820253Sjoerg	struct passwd  *pwd = NULL;
10920253Sjoerg	struct group   *grp;
11020253Sjoerg	struct stat     st;
11120747Sdavidn	char            line[_PASSWORD_LEN+1];
11282868Sdd	FILE	       *fp;
113167919Sle	char *dmode_c;
114167919Sle	void *set = NULL;
11520253Sjoerg
11620253Sjoerg	static struct passwd fakeuser =
11720253Sjoerg	{
11820253Sjoerg		NULL,
11920253Sjoerg		"*",
12020253Sjoerg		-1,
12120253Sjoerg		-1,
12220253Sjoerg		0,
12320253Sjoerg		"",
12420253Sjoerg		"User &",
12556000Sdavidn		"/nonexistent",
12620253Sjoerg		"/bin/sh",
12720253Sjoerg		0
12856000Sdavidn#if defined(__FreeBSD__)
12956000Sdavidn		,0
13056000Sdavidn#endif
13120253Sjoerg	};
13220253Sjoerg
13352512Sdavidn
13420253Sjoerg	/*
13520267Sjoerg	 * With M_NEXT, we only need to return the
13620267Sjoerg	 * next uid to stdout
13720267Sjoerg	 */
13820267Sjoerg	if (mode == M_NEXT)
13920267Sjoerg	{
14020267Sjoerg		uid_t next = pw_uidpolicy(cnf, args);
14120267Sjoerg		if (getarg(args, 'q'))
14220267Sjoerg			return next;
14320267Sjoerg		printf("%ld:", (long)next);
14420267Sjoerg		pw_group(cnf, mode, args);
14520267Sjoerg		return EXIT_SUCCESS;
14620267Sjoerg	}
14720267Sjoerg
14820267Sjoerg	/*
14920253Sjoerg	 * We can do all of the common legwork here
15020253Sjoerg	 */
15120253Sjoerg
15220253Sjoerg	if ((arg = getarg(args, 'b')) != NULL) {
15320267Sjoerg		cnf->home = arg->val;
15420253Sjoerg	}
15521052Sdavidn
156167919Sle	if ((arg = getarg(args, 'M')) != NULL) {
157167919Sle		dmode_c = arg->val;
158167919Sle		if ((set = setmode(dmode_c)) == NULL)
159167919Sle			errx(EX_DATAERR, "invalid directory creation mode '%s'",
160167919Sle			    dmode_c);
161219408Sjkim		cnf->homemode = getmode(set, _DEF_DIRMODE);
162167919Sle		free(set);
163168044Sle	}
164167919Sle
16521052Sdavidn	/*
16621052Sdavidn	 * If we'll need to use it or we're updating it,
16721052Sdavidn	 * then create the base home directory if necessary
16821052Sdavidn	 */
169224535Sdelphij	if (arg != NULL || getarg(args, 'm') != NULL) {
17021052Sdavidn		int	l = strlen(cnf->home);
17121052Sdavidn
17221052Sdavidn		if (l > 1 && cnf->home[l-1] == '/')	/* Shave off any trailing path delimiter */
17321052Sdavidn			cnf->home[--l] = '\0';
17421052Sdavidn
17521052Sdavidn		if (l < 2 || *cnf->home != '/')		/* Check for absolute path name */
17630259Scharnier			errx(EX_DATAERR, "invalid base directory for home '%s'", cnf->home);
17721052Sdavidn
17821052Sdavidn		if (stat(cnf->home, &st) == -1) {
17921052Sdavidn			char	dbuf[MAXPATHLEN];
18021052Sdavidn
18121242Sdavidn			/*
18221242Sdavidn			 * This is a kludge especially for Joerg :)
18321242Sdavidn			 * If the home directory would be created in the root partition, then
18421242Sdavidn			 * we really create it under /usr which is likely to have more space.
18521242Sdavidn			 * But we create a symlink from cnf->home -> "/usr" -> cnf->home
18621242Sdavidn			 */
18721242Sdavidn			if (strchr(cnf->home+1, '/') == NULL) {
18821242Sdavidn				strcpy(dbuf, "/usr");
18921242Sdavidn				strncat(dbuf, cnf->home, MAXPATHLEN-5);
190219408Sjkim				if (mkdir(dbuf, _DEF_DIRMODE) != -1 || errno == EEXIST) {
19121242Sdavidn					chown(dbuf, 0, 0);
192148584Spjd					/*
193148584Spjd					 * Skip first "/" and create symlink:
194148584Spjd					 * /home -> usr/home
195148584Spjd					 */
196148584Spjd					symlink(dbuf+1, cnf->home);
19721242Sdavidn				}
19821242Sdavidn				/* If this falls, fall back to old method */
19921242Sdavidn			}
200130633Srobert			strlcpy(dbuf, cnf->home, sizeof(dbuf));
201130633Srobert			p = dbuf;
20221242Sdavidn			if (stat(dbuf, &st) == -1) {
20321242Sdavidn				while ((p = strchr(++p, '/')) != NULL) {
20421242Sdavidn					*p = '\0';
20521242Sdavidn					if (stat(dbuf, &st) == -1) {
206219408Sjkim						if (mkdir(dbuf, _DEF_DIRMODE) == -1)
20721242Sdavidn							goto direrr;
20821242Sdavidn						chown(dbuf, 0, 0);
20921242Sdavidn					} else if (!S_ISDIR(st.st_mode))
21030259Scharnier						errx(EX_OSFILE, "'%s' (root home parent) is not a directory", dbuf);
21121242Sdavidn					*p = '/';
21221242Sdavidn				}
21321052Sdavidn			}
21421242Sdavidn			if (stat(dbuf, &st) == -1) {
215219408Sjkim				if (mkdir(dbuf, _DEF_DIRMODE) == -1) {
21630259Scharnier				direrr:	err(EX_OSFILE, "mkdir '%s'", dbuf);
21721052Sdavidn				}
21821052Sdavidn				chown(dbuf, 0, 0);
21921052Sdavidn			}
22021052Sdavidn		} else if (!S_ISDIR(st.st_mode))
22130259Scharnier			errx(EX_OSFILE, "root home `%s' is not a directory", cnf->home);
22221052Sdavidn	}
22321052Sdavidn
22420253Sjoerg	if ((arg = getarg(args, 'e')) != NULL)
22520253Sjoerg		cnf->expire_days = atoi(arg->val);
22620253Sjoerg
22721330Sdavidn	if ((arg = getarg(args, 'y')) != NULL)
22821330Sdavidn		cnf->nispasswd = arg->val;
22921330Sdavidn
23020253Sjoerg	if ((arg = getarg(args, 'p')) != NULL && arg->val)
23120253Sjoerg		cnf->password_days = atoi(arg->val);
23220253Sjoerg
23320253Sjoerg	if ((arg = getarg(args, 'g')) != NULL) {
23463596Sdavidn		if (!*(p = arg->val))	/* Handle empty group list specially */
23563596Sdavidn			cnf->default_group = "";
23663596Sdavidn		else {
23763596Sdavidn			if ((grp = GETGRNAM(p)) == NULL) {
23863596Sdavidn				if (!isdigit((unsigned char)*p) || (grp = GETGRGID((gid_t) atoi(p))) == NULL)
23963596Sdavidn					errx(EX_NOUSER, "group `%s' does not exist", p);
24063596Sdavidn			}
24163596Sdavidn			cnf->default_group = newstr(grp->gr_name);
24220253Sjoerg		}
24320253Sjoerg	}
24420253Sjoerg	if ((arg = getarg(args, 'L')) != NULL)
24520679Sdavidn		cnf->default_class = pw_checkname((u_char *)arg->val, 0);
24620253Sjoerg
24720253Sjoerg	if ((arg = getarg(args, 'G')) != NULL && arg->val) {
24852527Sdavidn		int i = 0;
24920253Sjoerg
25020747Sdavidn		for (p = strtok(arg->val, ", \t"); p != NULL; p = strtok(NULL, ", \t")) {
25144229Sdavidn			if ((grp = GETGRNAM(p)) == NULL) {
25261957Sache				if (!isdigit((unsigned char)*p) || (grp = GETGRGID((gid_t) atoi(p))) == NULL)
25330259Scharnier					errx(EX_NOUSER, "group `%s' does not exist", p);
25420253Sjoerg			}
25520747Sdavidn			if (extendarray(&cnf->groups, &cnf->numgroups, i + 2) != -1)
25620747Sdavidn				cnf->groups[i++] = newstr(grp->gr_name);
25720253Sjoerg		}
25820747Sdavidn		while (i < cnf->numgroups)
25920253Sjoerg			cnf->groups[i++] = NULL;
26020253Sjoerg	}
26152527Sdavidn
26220253Sjoerg	if ((arg = getarg(args, 'k')) != NULL) {
26326088Sdavidn		if (stat(cnf->dotdir = arg->val, &st) == -1 || !S_ISDIR(st.st_mode))
26430259Scharnier			errx(EX_OSFILE, "skeleton `%s' is not a directory or does not exist", cnf->dotdir);
26520253Sjoerg	}
26652527Sdavidn
26720253Sjoerg	if ((arg = getarg(args, 's')) != NULL)
26820253Sjoerg		cnf->shell_default = arg->val;
26920253Sjoerg
27063600Sdavidn	if ((arg = getarg(args, 'w')) != NULL)
27163600Sdavidn		cnf->default_password = boolean_val(arg->val, cnf->default_password);
27220253Sjoerg	if (mode == M_ADD && getarg(args, 'D')) {
27320253Sjoerg		if (getarg(args, 'n') != NULL)
27430259Scharnier			errx(EX_DATAERR, "can't combine `-D' with `-n name'");
27520253Sjoerg		if ((arg = getarg(args, 'u')) != NULL && (p = strtok(arg->val, ", \t")) != NULL) {
27620253Sjoerg			if ((cnf->min_uid = (uid_t) atoi(p)) == 0)
27720253Sjoerg				cnf->min_uid = 1000;
27820253Sjoerg			if ((p = strtok(NULL, " ,\t")) == NULL || (cnf->max_uid = (uid_t) atoi(p)) < cnf->min_uid)
27920253Sjoerg				cnf->max_uid = 32000;
28020253Sjoerg		}
28120253Sjoerg		if ((arg = getarg(args, 'i')) != NULL && (p = strtok(arg->val, ", \t")) != NULL) {
28220253Sjoerg			if ((cnf->min_gid = (gid_t) atoi(p)) == 0)
28320253Sjoerg				cnf->min_gid = 1000;
28420253Sjoerg			if ((p = strtok(NULL, " ,\t")) == NULL || (cnf->max_gid = (gid_t) atoi(p)) < cnf->min_gid)
28520253Sjoerg				cnf->max_gid = 32000;
28620253Sjoerg		}
28720253Sjoerg
28820253Sjoerg		arg = getarg(args, 'C');
28920253Sjoerg		if (write_userconfig(arg ? arg->val : NULL))
29020267Sjoerg			return EXIT_SUCCESS;
29130259Scharnier		warn("config update");
29220267Sjoerg		return EX_IOERR;
29320253Sjoerg	}
29452527Sdavidn
29520253Sjoerg	if (mode == M_PRINT && getarg(args, 'a')) {
29620267Sjoerg		int             pretty = getarg(args, 'P') != NULL;
29744386Sdavidn		int		v7 = getarg(args, '7') != NULL;
29844229Sdavidn		SETPWENT();
29944229Sdavidn		while ((pwd = GETPWENT()) != NULL)
30044386Sdavidn			print_user(pwd, pretty, v7);
30144229Sdavidn		ENDPWENT();
30220267Sjoerg		return EXIT_SUCCESS;
30320253Sjoerg	}
30452527Sdavidn
30520253Sjoerg	if ((a_name = getarg(args, 'n')) != NULL)
30644229Sdavidn		pwd = GETPWNAM(pw_checkname((u_char *)a_name->val, 0));
30720253Sjoerg	a_uid = getarg(args, 'u');
30820253Sjoerg
30920253Sjoerg	if (a_uid == NULL) {
31020253Sjoerg		if (a_name == NULL)
31130259Scharnier			errx(EX_DATAERR, "user name or id required");
31220253Sjoerg
31320253Sjoerg		/*
31420253Sjoerg		 * Determine whether 'n' switch is name or uid - we don't
31520253Sjoerg		 * really don't really care which we have, but we need to
31620253Sjoerg		 * know.
31720253Sjoerg		 */
31843780Sdes		if (mode != M_ADD && pwd == NULL
319241108Sbapt		    && strspn(a_name->val, "0123456789") == strlen(a_name->val)
320241108Sbapt		    && *a_name->val) {
32120253Sjoerg			(a_uid = a_name)->ch = 'u';
32220253Sjoerg			a_name = NULL;
32320253Sjoerg		}
32420253Sjoerg	}
32552527Sdavidn
32620253Sjoerg	/*
32720253Sjoerg	 * Update, delete & print require that the user exists
32820253Sjoerg	 */
32952512Sdavidn	if (mode == M_UPDATE || mode == M_DELETE ||
33052512Sdavidn	    mode == M_PRINT  || mode == M_LOCK   || mode == M_UNLOCK) {
33152527Sdavidn
33220253Sjoerg		if (a_name == NULL && pwd == NULL)	/* Try harder */
33344229Sdavidn			pwd = GETPWUID(atoi(a_uid->val));
33420253Sjoerg
33520253Sjoerg		if (pwd == NULL) {
33620253Sjoerg			if (mode == M_PRINT && getarg(args, 'F')) {
33720253Sjoerg				fakeuser.pw_name = a_name ? a_name->val : "nouser";
33820253Sjoerg				fakeuser.pw_uid = a_uid ? (uid_t) atol(a_uid->val) : -1;
33944386Sdavidn				return print_user(&fakeuser,
34044386Sdavidn						  getarg(args, 'P') != NULL,
34144386Sdavidn						  getarg(args, '7') != NULL);
34220253Sjoerg			}
34320253Sjoerg			if (a_name == NULL)
34430259Scharnier				errx(EX_NOUSER, "no such uid `%s'", a_uid->val);
34530259Scharnier			errx(EX_NOUSER, "no such user `%s'", a_name->val);
34620253Sjoerg		}
34752527Sdavidn
34820253Sjoerg		if (a_name == NULL)	/* May be needed later */
34920253Sjoerg			a_name = addarg(args, 'n', newstr(pwd->pw_name));
35020253Sjoerg
35120253Sjoerg		/*
35252512Sdavidn		 * The M_LOCK and M_UNLOCK functions simply add or remove
35352512Sdavidn		 * a "*LOCKED*" prefix from in front of the password to
35452512Sdavidn		 * prevent it decoding correctly, and therefore prevents
35552512Sdavidn		 * access. Of course, this only prevents access via
35652512Sdavidn		 * password authentication (not ssh, kerberos or any
35752512Sdavidn		 * other method that does not use the UNIX password) but
35852512Sdavidn		 * that is a known limitation.
35952512Sdavidn		 */
36052512Sdavidn
36152512Sdavidn		if (mode == M_LOCK) {
36252512Sdavidn			if (strncmp(pwd->pw_passwd, locked_str, sizeof(locked_str)-1) == 0)
36352512Sdavidn				errx(EX_DATAERR, "user '%s' is already locked", pwd->pw_name);
36452512Sdavidn			passtmp = malloc(strlen(pwd->pw_passwd) + sizeof(locked_str));
36552512Sdavidn			if (passtmp == NULL)	/* disaster */
36652512Sdavidn				errx(EX_UNAVAILABLE, "out of memory");
36752512Sdavidn			strcpy(passtmp, locked_str);
36852512Sdavidn			strcat(passtmp, pwd->pw_passwd);
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;
37652527Sdavidn		} else if (mode == M_DELETE) {
37752527Sdavidn			/*
37852527Sdavidn			 * Handle deletions now
37952527Sdavidn			 */
38020253Sjoerg			char            file[MAXPATHLEN];
38120253Sjoerg			char            home[MAXPATHLEN];
38220253Sjoerg			uid_t           uid = pwd->pw_uid;
38320253Sjoerg
38420253Sjoerg			if (strcmp(pwd->pw_name, "root") == 0)
38530259Scharnier				errx(EX_DATAERR, "cannot remove user 'root'");
38620253Sjoerg
38744229Sdavidn			if (!PWALTDIR()) {
38844229Sdavidn				/*
38985145Sache				 * Remove opie record from /etc/opiekeys
39044229Sdavidn		        	 */
39120747Sdavidn
39285145Sache				rmopie(pwd->pw_name);
39320747Sdavidn
39444229Sdavidn				/*
39544229Sdavidn				 * Remove crontabs
39644229Sdavidn				 */
39744229Sdavidn				sprintf(file, "/var/cron/tabs/%s", pwd->pw_name);
39844229Sdavidn				if (access(file, F_OK) == 0) {
39944229Sdavidn					sprintf(file, "crontab -u %s -r", pwd->pw_name);
40044229Sdavidn					system(file);
40144229Sdavidn				}
40220253Sjoerg			}
40320253Sjoerg			/*
40420253Sjoerg			 * Save these for later, since contents of pwd may be
40520253Sjoerg			 * invalidated by deletion
40620253Sjoerg			 */
40720253Sjoerg			sprintf(file, "%s/%s", _PATH_MAILDIR, pwd->pw_name);
408130633Srobert			strlcpy(home, pwd->pw_dir, sizeof(home));
40920253Sjoerg
41052502Sdavidn			rc = delpwent(pwd);
41152502Sdavidn			if (rc == -1)
41252502Sdavidn				err(EX_IOERR, "user '%s' does not exist", pwd->pw_name);
41352502Sdavidn			else if (rc != 0) {
41456000Sdavidn				warn("passwd update");
41552502Sdavidn				return EX_IOERR;
41652502Sdavidn			}
41721330Sdavidn
41852502Sdavidn			if (cnf->nispasswd && *cnf->nispasswd=='/') {
41952502Sdavidn				rc = delnispwent(cnf->nispasswd, a_name->val);
42052502Sdavidn				if (rc == -1)
42152502Sdavidn					warnx("WARNING: user '%s' does not exist in NIS passwd", pwd->pw_name);
42252502Sdavidn				else if (rc != 0)
42356000Sdavidn					warn("WARNING: NIS passwd update");
42452502Sdavidn				/* non-fatal */
42552502Sdavidn			}
42652502Sdavidn
427242349Sbapt			grp = GETGRNAM(a_name->val);
428242349Sbapt			if (*grp->gr_mem == NULL)
429242349Sbapt				delgrent(GETGRNAM(a_name->val));
430242349Sbapt			SETGRENT();
431242349Sbapt			while ((grp = GETGRENT()) != NULL) {
432242349Sbapt				int i;
433242349Sbapt				char group[MAXLOGNAME];
434242349Sbapt				for (i = 0; grp->gr_mem[i] != NULL; i++) {
435242349Sbapt					if (!strcmp(grp->gr_mem[i], a_name->val)) {
436242349Sbapt						while (grp->gr_mem[i] != NULL) {
437242349Sbapt							grp->gr_mem[i] = grp->gr_mem[i+1];
438242349Sbapt						}
439242349Sbapt						strlcpy(group, grp->gr_name, MAXLOGNAME);
440242349Sbapt						chggrent(group, grp);
441242349Sbapt					}
442242349Sbapt				}
443242349Sbapt			}
444242349Sbapt			ENDGRENT();
44520253Sjoerg
44620253Sjoerg			pw_log(cnf, mode, W_USER, "%s(%ld) account removed", a_name->val, (long) uid);
44720253Sjoerg
44844229Sdavidn			if (!PWALTDIR()) {
44944229Sdavidn				/*
45044229Sdavidn				 * Remove mail file
45144229Sdavidn				 */
45244229Sdavidn				remove(file);
45320253Sjoerg
45444229Sdavidn				/*
45544229Sdavidn				 * Remove at jobs
45644229Sdavidn				 */
45744229Sdavidn				if (getpwuid(uid) == NULL)
45844229Sdavidn					rmat(uid);
45920253Sjoerg
46044229Sdavidn				/*
46144229Sdavidn				 * Remove home directory and contents
46244229Sdavidn				 */
46344229Sdavidn				if (getarg(args, 'r') != NULL && *home == '/' && getpwuid(uid) == NULL) {
46444229Sdavidn					if (stat(home, &st) != -1) {
46544229Sdavidn						rm_r(home, uid);
46644229Sdavidn						pw_log(cnf, mode, W_USER, "%s(%ld) home '%s' %sremoved",
46744229Sdavidn						       a_name->val, (long) uid, home,
46844229Sdavidn						       stat(home, &st) == -1 ? "" : "not completely ");
46944229Sdavidn					}
47020253Sjoerg				}
47120253Sjoerg			}
47220267Sjoerg			return EXIT_SUCCESS;
47320253Sjoerg		} else if (mode == M_PRINT)
47444386Sdavidn			return print_user(pwd,
47544386Sdavidn					  getarg(args, 'P') != NULL,
47644386Sdavidn					  getarg(args, '7') != NULL);
47720253Sjoerg
47820253Sjoerg		/*
47920253Sjoerg		 * The rest is edit code
48020253Sjoerg		 */
48120253Sjoerg		if ((arg = getarg(args, 'l')) != NULL) {
48220253Sjoerg			if (strcmp(pwd->pw_name, "root") == 0)
48330259Scharnier				errx(EX_DATAERR, "can't rename `root' account");
48420679Sdavidn			pwd->pw_name = pw_checkname((u_char *)arg->val, 0);
48552527Sdavidn			edited = 1;
48620253Sjoerg		}
48752527Sdavidn
48861957Sache		if ((arg = getarg(args, 'u')) != NULL && isdigit((unsigned char)*arg->val)) {
48920253Sjoerg			pwd->pw_uid = (uid_t) atol(arg->val);
49052527Sdavidn			edited = 1;
49120253Sjoerg			if (pwd->pw_uid != 0 && strcmp(pwd->pw_name, "root") == 0)
49230259Scharnier				errx(EX_DATAERR, "can't change uid of `root' account");
49320253Sjoerg			if (pwd->pw_uid == 0 && strcmp(pwd->pw_name, "root") != 0)
49430259Scharnier				warnx("WARNING: account `%s' will have a uid of 0 (superuser access!)", pwd->pw_name);
49520253Sjoerg		}
49620253Sjoerg
49752527Sdavidn		if ((arg = getarg(args, 'g')) != NULL && pwd->pw_uid != 0) {	/* Already checked this */
49852527Sdavidn			gid_t newgid = (gid_t) GETGRNAM(cnf->default_group)->gr_gid;
49952527Sdavidn			if (newgid != pwd->pw_gid) {
50052527Sdavidn				edited = 1;
50161762Sdavidn				pwd->pw_gid = newgid;
50252527Sdavidn			}
50352527Sdavidn		}
50452527Sdavidn
50520253Sjoerg		if ((arg = getarg(args, 'p')) != NULL) {
50652527Sdavidn			if (*arg->val == '\0' || strcmp(arg->val, "0") == 0) {
50752527Sdavidn				if (pwd->pw_change != 0) {
50852527Sdavidn					pwd->pw_change = 0;
50952527Sdavidn					edited = 1;
51052527Sdavidn				}
51152527Sdavidn			}
51220253Sjoerg			else {
51320253Sjoerg				time_t          now = time(NULL);
51420253Sjoerg				time_t          expire = parse_date(now, arg->val);
51520253Sjoerg
51620253Sjoerg				if (now == expire)
51730259Scharnier					errx(EX_DATAERR, "invalid password change date `%s'", arg->val);
51852527Sdavidn				if (pwd->pw_change != expire) {
51952527Sdavidn					pwd->pw_change = expire;
52052527Sdavidn					edited = 1;
52152527Sdavidn				}
52220253Sjoerg			}
52320253Sjoerg		}
52452527Sdavidn
52520267Sjoerg		if ((arg = getarg(args, 'e')) != NULL) {
52652527Sdavidn			if (*arg->val == '\0' || strcmp(arg->val, "0") == 0) {
52752527Sdavidn				if (pwd->pw_expire != 0) {
52852527Sdavidn					pwd->pw_expire = 0;
52952527Sdavidn					edited = 1;
53052527Sdavidn				}
53152527Sdavidn			}
53220253Sjoerg			else {
53320253Sjoerg				time_t          now = time(NULL);
53420253Sjoerg				time_t          expire = parse_date(now, arg->val);
53520253Sjoerg
53620253Sjoerg				if (now == expire)
53730259Scharnier					errx(EX_DATAERR, "invalid account expiry date `%s'", arg->val);
53852527Sdavidn				if (pwd->pw_expire != expire) {
53952527Sdavidn					pwd->pw_expire = expire;
54052527Sdavidn					edited = 1;
54152527Sdavidn				}
54220253Sjoerg			}
54320253Sjoerg		}
54420253Sjoerg
54552527Sdavidn		if ((arg = getarg(args, 's')) != NULL) {
54652527Sdavidn			char *shell = shell_path(cnf->shelldir, cnf->shells, arg->val);
54752527Sdavidn			if (shell == NULL)
54852527Sdavidn				shell = "";
54952527Sdavidn			if (strcmp(shell, pwd->pw_shell) != 0) {
55052527Sdavidn				pwd->pw_shell = shell;
55152527Sdavidn				edited = 1;
55252527Sdavidn			}
55352527Sdavidn		}
55420253Sjoerg
55552527Sdavidn		if (getarg(args, 'L')) {
55652527Sdavidn			if (cnf->default_class == NULL)
55752527Sdavidn				cnf->default_class = "";
55852527Sdavidn			if (strcmp(pwd->pw_class, cnf->default_class) != 0) {
55952527Sdavidn				pwd->pw_class = cnf->default_class;
56052527Sdavidn				edited = 1;
56152527Sdavidn			}
56252527Sdavidn		}
56352527Sdavidn
56420747Sdavidn		if ((arg  = getarg(args, 'd')) != NULL) {
565130629Srobert			if (strcmp(pwd->pw_dir, arg->val))
566130629Srobert				edited = 1;
56720747Sdavidn			if (stat(pwd->pw_dir = arg->val, &st) == -1) {
56820747Sdavidn				if (getarg(args, 'm') == NULL && strcmp(pwd->pw_dir, "/nonexistent") != 0)
56930259Scharnier				  warnx("WARNING: home `%s' does not exist", pwd->pw_dir);
57020747Sdavidn			} else if (!S_ISDIR(st.st_mode))
57130259Scharnier				warnx("WARNING: home `%s' is not a directory", pwd->pw_dir);
57220747Sdavidn		}
57320747Sdavidn
574124382Siedowse		if ((arg = getarg(args, 'w')) != NULL &&
575124382Siedowse		    getarg(args, 'h') == NULL && getarg(args, 'H') == NULL) {
57664918Sgreen			login_cap_t *lc;
57764918Sgreen
57864918Sgreen			lc = login_getpwclass(pwd);
57964918Sgreen			if (lc == NULL ||
58064918Sgreen			    login_setcryptfmt(lc, "md5", NULL) == NULL)
58164918Sgreen				warn("setting crypt(3) format");
58264918Sgreen			login_close(lc);
58320267Sjoerg			pwd->pw_passwd = pw_password(cnf, args, pwd->pw_name);
58452527Sdavidn			edited = 1;
58552527Sdavidn		}
58620267Sjoerg
58720253Sjoerg	} else {
58864918Sgreen		login_cap_t *lc;
58952527Sdavidn
59052527Sdavidn		/*
59152527Sdavidn		 * Add code
59252527Sdavidn		 */
59352527Sdavidn
59420253Sjoerg		if (a_name == NULL)	/* Required */
59530259Scharnier			errx(EX_DATAERR, "login name required");
59644229Sdavidn		else if ((pwd = GETPWNAM(a_name->val)) != NULL)	/* Exists */
59730259Scharnier			errx(EX_DATAERR, "login name `%s' already exists", a_name->val);
59820253Sjoerg
59920253Sjoerg		/*
60020253Sjoerg		 * Now, set up defaults for a new user
60120253Sjoerg		 */
60220253Sjoerg		pwd = &fakeuser;
60320253Sjoerg		pwd->pw_name = a_name->val;
60420253Sjoerg		pwd->pw_class = cnf->default_class ? cnf->default_class : "";
60520253Sjoerg		pwd->pw_uid = pw_uidpolicy(cnf, args);
60620253Sjoerg		pwd->pw_gid = pw_gidpolicy(cnf, args, pwd->pw_name, (gid_t) pwd->pw_uid);
60720253Sjoerg		pwd->pw_change = pw_pwdpolicy(cnf, args);
60820253Sjoerg		pwd->pw_expire = pw_exppolicy(cnf, args);
60920253Sjoerg		pwd->pw_dir = pw_homepolicy(cnf, args, pwd->pw_name);
61020253Sjoerg		pwd->pw_shell = pw_shellpolicy(cnf, args, NULL);
61164918Sgreen		lc = login_getpwclass(pwd);
61264918Sgreen		if (lc == NULL || login_setcryptfmt(lc, "md5", NULL) == NULL)
61364918Sgreen			warn("setting crypt(3) format");
61464918Sgreen		login_close(lc);
61564918Sgreen		pwd->pw_passwd = pw_password(cnf, args, pwd->pw_name);
61652527Sdavidn		edited = 1;
61720253Sjoerg
61820253Sjoerg		if (pwd->pw_uid == 0 && strcmp(pwd->pw_name, "root") != 0)
61930259Scharnier			warnx("WARNING: new account `%s' has a uid of 0 (superuser access!)", pwd->pw_name);
62020253Sjoerg	}
62120253Sjoerg
62220253Sjoerg	/*
62320253Sjoerg	 * Shared add/edit code
62420253Sjoerg	 */
62552527Sdavidn	if ((arg = getarg(args, 'c')) != NULL) {
62652527Sdavidn		char	*gecos = pw_checkname((u_char *)arg->val, 1);
62752527Sdavidn		if (strcmp(pwd->pw_gecos, gecos) != 0) {
62852527Sdavidn			pwd->pw_gecos = gecos;
62952527Sdavidn			edited = 1;
63052527Sdavidn		}
63152527Sdavidn	}
63220253Sjoerg
633124382Siedowse	if ((arg = getarg(args, 'h')) != NULL ||
634124382Siedowse	    (arg = getarg(args, 'H')) != NULL) {
63563572Sdavidn		if (strcmp(arg->val, "-") == 0) {
63663572Sdavidn			if (!pwd->pw_passwd || *pwd->pw_passwd != '*') {
63763572Sdavidn				pwd->pw_passwd = "*";	/* No access */
63863572Sdavidn				edited = 1;
63963572Sdavidn			}
64063572Sdavidn		} else {
64120253Sjoerg			int             fd = atoi(arg->val);
642124382Siedowse			int		precrypt = (arg->ch == 'H');
64320253Sjoerg			int             b;
64420253Sjoerg			int             istty = isatty(fd);
64520253Sjoerg			struct termios  t;
64664918Sgreen			login_cap_t	*lc;
64720253Sjoerg
64820253Sjoerg			if (istty) {
64920253Sjoerg				if (tcgetattr(fd, &t) == -1)
65020253Sjoerg					istty = 0;
65120253Sjoerg				else {
65220253Sjoerg					struct termios  n = t;
65320253Sjoerg
65420253Sjoerg					/* Disable echo */
65520253Sjoerg					n.c_lflag &= ~(ECHO);
65620253Sjoerg					tcsetattr(fd, TCSANOW, &n);
657124382Siedowse					printf("%s%spassword for user %s:",
658124382Siedowse					     (mode == M_UPDATE) ? "new " : "",
659124382Siedowse					     precrypt ? "encrypted " : "",
660124382Siedowse					     pwd->pw_name);
66120253Sjoerg					fflush(stdout);
66220253Sjoerg				}
66320253Sjoerg			}
66420253Sjoerg			b = read(fd, line, sizeof(line) - 1);
66520253Sjoerg			if (istty) {	/* Restore state */
66620253Sjoerg				tcsetattr(fd, TCSANOW, &t);
66720253Sjoerg				fputc('\n', stdout);
66820253Sjoerg				fflush(stdout);
66920253Sjoerg			}
67020253Sjoerg			if (b < 0) {
671124382Siedowse				warn("-%c file descriptor", precrypt ? 'H' :
672124382Siedowse				    'h');
67320267Sjoerg				return EX_IOERR;
67420253Sjoerg			}
67520253Sjoerg			line[b] = '\0';
676168045Sle			if ((p = strpbrk(line, "\r\n")) != NULL)
67720253Sjoerg				*p = '\0';
67820253Sjoerg			if (!*line)
67930259Scharnier				errx(EX_DATAERR, "empty password read on file descriptor %d", fd);
680124382Siedowse			if (precrypt) {
681124382Siedowse				if (strchr(line, ':') != NULL)
682124382Siedowse					return EX_DATAERR;
683124382Siedowse				pwd->pw_passwd = line;
684124382Siedowse			} else {
685124382Siedowse				lc = login_getpwclass(pwd);
686124382Siedowse				if (lc == NULL ||
687124382Siedowse				    login_setcryptfmt(lc, "md5", NULL) == NULL)
688124382Siedowse					warn("setting crypt(3) format");
689124382Siedowse				login_close(lc);
690124382Siedowse				pwd->pw_passwd = pw_pwcrypt(line);
691124382Siedowse			}
69252527Sdavidn			edited = 1;
69320253Sjoerg		}
69420253Sjoerg	}
69520267Sjoerg
69620267Sjoerg	/*
69720267Sjoerg	 * Special case: -N only displays & exits
69820267Sjoerg	 */
69920267Sjoerg	if (getarg(args, 'N') != NULL)
70044386Sdavidn		return print_user(pwd,
70144386Sdavidn				  getarg(args, 'P') != NULL,
70244386Sdavidn				  getarg(args, '7') != NULL);
70320267Sjoerg
70421330Sdavidn	if (mode == M_ADD) {
70552527Sdavidn		edited = 1;	/* Always */
70652502Sdavidn		rc = addpwent(pwd);
70752502Sdavidn		if (rc == -1) {
70852502Sdavidn			warnx("user '%s' already exists", pwd->pw_name);
70952502Sdavidn			return EX_IOERR;
71052502Sdavidn		} else if (rc != 0) {
71156000Sdavidn			warn("passwd file update");
71252502Sdavidn			return EX_IOERR;
71352502Sdavidn		}
71452502Sdavidn		if (cnf->nispasswd && *cnf->nispasswd=='/') {
71552502Sdavidn			rc = addnispwent(cnf->nispasswd, pwd);
71652502Sdavidn			if (rc == -1)
71752502Sdavidn				warnx("User '%s' already exists in NIS passwd", pwd->pw_name);
71852502Sdavidn			else
71956000Sdavidn				warn("NIS passwd update");
72052502Sdavidn			/* NOTE: we treat NIS-only update errors as non-fatal */
72152502Sdavidn		}
72252512Sdavidn	} else if (mode == M_UPDATE || mode == M_LOCK || mode == M_UNLOCK) {
72352527Sdavidn		if (edited) {	/* Only updated this if required */
72452527Sdavidn			rc = chgpwent(a_name->val, pwd);
72552527Sdavidn			if (rc == -1) {
72652527Sdavidn				warnx("user '%s' does not exist (NIS?)", pwd->pw_name);
72752527Sdavidn				return EX_IOERR;
72852527Sdavidn			} else if (rc != 0) {
72956000Sdavidn				warn("passwd file update");
73052527Sdavidn				return EX_IOERR;
73152527Sdavidn			}
73252527Sdavidn			if ( cnf->nispasswd && *cnf->nispasswd=='/') {
73352527Sdavidn				rc = chgnispwent(cnf->nispasswd, a_name->val, pwd);
73452527Sdavidn				if (rc == -1)
73552527Sdavidn					warn("User '%s' not found in NIS passwd", pwd->pw_name);
73652527Sdavidn				else
73756000Sdavidn					warn("NIS passwd update");
73852527Sdavidn				/* NOTE: NIS-only update errors are not fatal */
73952527Sdavidn			}
74052502Sdavidn		}
74121330Sdavidn	}
74221330Sdavidn
74320253Sjoerg	/*
74420253Sjoerg	 * Ok, user is created or changed - now edit group file
74520253Sjoerg	 */
74620253Sjoerg
747242349Sbapt	if (mode == M_ADD || getarg(args, 'G') != NULL) {
748242349Sbapt		int i, j;
749242349Sbapt		for (i = 0; cnf->groups[i] != NULL; i++) {
750242349Sbapt			grp = GETGRNAM(cnf->groups[i]);
751242349Sbapt			for (j = 0; grp->gr_mem[j] != NULL; j++) {
752242349Sbapt				if (!strcmp(grp->gr_mem[j], pwd->pw_name))
753242349Sbapt					break;
754242349Sbapt			}
755242349Sbapt			if (grp->gr_mem[j] != NULL) /* user already member of group */
756242349Sbapt				continue;
75720253Sjoerg
758242349Sbapt			if (j == 0)
759242349Sbapt				grp->gr_mem = NULL;
760242349Sbapt
761242349Sbapt			grp->gr_mem = reallocf(grp->gr_mem, sizeof(*grp->gr_mem) *
762242349Sbapt					                    (j + 2));
763242349Sbapt
764242349Sbapt			grp->gr_mem[j] = pwd->pw_name;
765242349Sbapt			grp->gr_mem[j+1] = NULL;
766242349Sbapt			chggrent(cnf->groups[i], grp);
767242349Sbapt		}
768242349Sbapt	}
769242349Sbapt
770242349Sbapt
77161759Sdavidn	/* go get a current version of pwd */
77261759Sdavidn	pwd = GETPWNAM(a_name->val);
77361759Sdavidn	if (pwd == NULL) {
77461759Sdavidn		/* This will fail when we rename, so special case that */
77561759Sdavidn		if (mode == M_UPDATE && (arg = getarg(args, 'l')) != NULL) {
77661759Sdavidn			a_name->val = arg->val;		/* update new name */
77761759Sdavidn			pwd = GETPWNAM(a_name->val);	/* refetch renamed rec */
77861759Sdavidn		}
77961759Sdavidn	}
78061759Sdavidn	if (pwd == NULL)	/* can't go on without this */
78130259Scharnier		errx(EX_NOUSER, "user '%s' disappeared during update", a_name->val);
78220253Sjoerg
78344229Sdavidn	grp = GETGRGID(pwd->pw_gid);
78498744Sdwmalone	pw_log(cnf, mode, W_USER, "%s(%ld):%s(%ld):%s:%s:%s",
78520253Sjoerg	       pwd->pw_name, (long) pwd->pw_uid,
78620253Sjoerg	    grp ? grp->gr_name : "unknown", (long) (grp ? grp->gr_gid : -1),
78720253Sjoerg	       pwd->pw_gecos, pwd->pw_dir, pwd->pw_shell);
78820253Sjoerg
78920253Sjoerg	/*
79020253Sjoerg	 * If adding, let's touch and chown the user's mail file. This is not
79120253Sjoerg	 * strictly necessary under BSD with a 0755 maildir but it also
79220253Sjoerg	 * doesn't hurt anything to create the empty mailfile
79320253Sjoerg	 */
79420253Sjoerg	if (mode == M_ADD) {
79544229Sdavidn		if (!PWALTDIR()) {
79644229Sdavidn			sprintf(line, "%s/%s", _PATH_MAILDIR, pwd->pw_name);
79744229Sdavidn			close(open(line, O_RDWR | O_CREAT, 0600));	/* Preserve contents &
79844229Sdavidn									 * mtime */
79944229Sdavidn			chown(line, pwd->pw_uid, pwd->pw_gid);
80020253Sjoerg		}
80120253Sjoerg	}
80252527Sdavidn
80320253Sjoerg	/*
80482868Sdd	 * Let's create and populate the user's home directory. Note
80520253Sjoerg	 * that this also `works' for editing users if -m is used, but
80620253Sjoerg	 * existing files will *not* be overwritten.
80720253Sjoerg	 */
80844229Sdavidn	if (!PWALTDIR() && getarg(args, 'm') != NULL && pwd->pw_dir && *pwd->pw_dir == '/' && pwd->pw_dir[1]) {
809168044Sle		copymkdir(pwd->pw_dir, cnf->dotdir, cnf->homemode, pwd->pw_uid, pwd->pw_gid);
81020253Sjoerg		pw_log(cnf, mode, W_USER, "%s(%ld) home %s made",
81120253Sjoerg		       pwd->pw_name, (long) pwd->pw_uid, pwd->pw_dir);
81220253Sjoerg	}
81352527Sdavidn
81482868Sdd
81582868Sdd	/*
81682868Sdd	 * Finally, send mail to the new user as well, if we are asked to
81782868Sdd	 */
81882868Sdd	if (mode == M_ADD && !PWALTDIR() && cnf->newmail && *cnf->newmail && (fp = fopen(cnf->newmail, "r")) != NULL) {
81982868Sdd		FILE           *pfp = popen(_PATH_SENDMAIL " -t", "w");
82082868Sdd
82182868Sdd		if (pfp == NULL)
82282868Sdd			warn("sendmail");
82382868Sdd		else {
82482868Sdd			fprintf(pfp, "From: root\n" "To: %s\n" "Subject: Welcome!\n\n", pwd->pw_name);
82582868Sdd			while (fgets(line, sizeof(line), fp) != NULL) {
82682868Sdd				/* Do substitutions? */
82782868Sdd				fputs(line, pfp);
82882868Sdd			}
82982868Sdd			pclose(pfp);
83082868Sdd			pw_log(cnf, mode, W_USER, "%s(%ld) new user mail sent",
83182868Sdd			    pwd->pw_name, (long) pwd->pw_uid);
83282868Sdd		}
83382868Sdd		fclose(fp);
83482868Sdd	}
83582868Sdd
83620267Sjoerg	return EXIT_SUCCESS;
83720253Sjoerg}
83820253Sjoerg
83920253Sjoerg
84020253Sjoergstatic          uid_t
84120253Sjoergpw_uidpolicy(struct userconf * cnf, struct cargs * args)
84220253Sjoerg{
84320253Sjoerg	struct passwd  *pwd;
84420253Sjoerg	uid_t           uid = (uid_t) - 1;
84520253Sjoerg	struct carg    *a_uid = getarg(args, 'u');
84620253Sjoerg
84720253Sjoerg	/*
84820253Sjoerg	 * Check the given uid, if any
84920253Sjoerg	 */
85020253Sjoerg	if (a_uid != NULL) {
85120253Sjoerg		uid = (uid_t) atol(a_uid->val);
85220253Sjoerg
85344229Sdavidn		if ((pwd = GETPWUID(uid)) != NULL && getarg(args, 'o') == NULL)
85430259Scharnier			errx(EX_DATAERR, "uid `%ld' has already been allocated", (long) pwd->pw_uid);
85520253Sjoerg	} else {
85620253Sjoerg		struct bitmap   bm;
85720253Sjoerg
85820253Sjoerg		/*
85920253Sjoerg		 * We need to allocate the next available uid under one of
86020253Sjoerg		 * two policies a) Grab the first unused uid b) Grab the
86120253Sjoerg		 * highest possible unused uid
86220253Sjoerg		 */
86320253Sjoerg		if (cnf->min_uid >= cnf->max_uid) {	/* Sanity
86420253Sjoerg							 * claus^H^H^H^Hheck */
86520253Sjoerg			cnf->min_uid = 1000;
86620253Sjoerg			cnf->max_uid = 32000;
86720253Sjoerg		}
86820253Sjoerg		bm = bm_alloc(cnf->max_uid - cnf->min_uid + 1);
86920253Sjoerg
87020253Sjoerg		/*
87120253Sjoerg		 * Now, let's fill the bitmap from the password file
87220253Sjoerg		 */
87344229Sdavidn		SETPWENT();
87444229Sdavidn		while ((pwd = GETPWENT()) != NULL)
87544229Sdavidn			if (pwd->pw_uid >= (uid_t) cnf->min_uid && pwd->pw_uid <= (uid_t) cnf->max_uid)
87620253Sjoerg				bm_setbit(&bm, pwd->pw_uid - cnf->min_uid);
87744229Sdavidn		ENDPWENT();
87820253Sjoerg
87920253Sjoerg		/*
88020253Sjoerg		 * Then apply the policy, with fallback to reuse if necessary
88120253Sjoerg		 */
88220253Sjoerg		if (cnf->reuse_uids || (uid = (uid_t) (bm_lastset(&bm) + cnf->min_uid + 1)) > cnf->max_uid)
88320253Sjoerg			uid = (uid_t) (bm_firstunset(&bm) + cnf->min_uid);
88420253Sjoerg
88520253Sjoerg		/*
88620253Sjoerg		 * Another sanity check
88720253Sjoerg		 */
88820253Sjoerg		if (uid < cnf->min_uid || uid > cnf->max_uid)
88930259Scharnier			errx(EX_SOFTWARE, "unable to allocate a new uid - range fully used");
89020253Sjoerg		bm_dealloc(&bm);
89120253Sjoerg	}
89220253Sjoerg	return uid;
89320253Sjoerg}
89420253Sjoerg
89520253Sjoerg
89620253Sjoergstatic          uid_t
89720253Sjoergpw_gidpolicy(struct userconf * cnf, struct cargs * args, char *nam, gid_t prefer)
89820253Sjoerg{
89920253Sjoerg	struct group   *grp;
90020253Sjoerg	gid_t           gid = (uid_t) - 1;
90120253Sjoerg	struct carg    *a_gid = getarg(args, 'g');
90220253Sjoerg
90320253Sjoerg	/*
90420253Sjoerg	 * If no arg given, see if default can help out
90520253Sjoerg	 */
90620253Sjoerg	if (a_gid == NULL && cnf->default_group && *cnf->default_group)
90720253Sjoerg		a_gid = addarg(args, 'g', cnf->default_group);
90820253Sjoerg
90920253Sjoerg	/*
91020253Sjoerg	 * Check the given gid, if any
91120253Sjoerg	 */
91244229Sdavidn	SETGRENT();
91320253Sjoerg	if (a_gid != NULL) {
91444229Sdavidn		if ((grp = GETGRNAM(a_gid->val)) == NULL) {
91520253Sjoerg			gid = (gid_t) atol(a_gid->val);
91661957Sache			if ((gid == 0 && !isdigit((unsigned char)*a_gid->val)) || (grp = GETGRGID(gid)) == NULL)
91730259Scharnier				errx(EX_NOUSER, "group `%s' is not defined", a_gid->val);
91820253Sjoerg		}
91920253Sjoerg		gid = grp->gr_gid;
92044229Sdavidn	} else if ((grp = GETGRNAM(nam)) != NULL && grp->gr_mem[0] == NULL) {
92120267Sjoerg		gid = grp->gr_gid;  /* Already created? Use it anyway... */
92220253Sjoerg	} else {
92320253Sjoerg		struct cargs    grpargs;
92420253Sjoerg		char            tmp[32];
92520253Sjoerg
92620253Sjoerg		LIST_INIT(&grpargs);
92720253Sjoerg		addarg(&grpargs, 'n', nam);
92820253Sjoerg
92920253Sjoerg		/*
93020253Sjoerg		 * We need to auto-create a group with the user's name. We
93120253Sjoerg		 * can send all the appropriate output to our sister routine
93220253Sjoerg		 * bit first see if we can create a group with gid==uid so we
93320253Sjoerg		 * can keep the user and group ids in sync. We purposely do
93420253Sjoerg		 * NOT check the gid range if we can force the sync. If the
93520253Sjoerg		 * user's name dups an existing group, then the group add
93620253Sjoerg		 * function will happily handle that case for us and exit.
93720253Sjoerg		 */
93844229Sdavidn		if (GETGRGID(prefer) == NULL) {
93920253Sjoerg			sprintf(tmp, "%lu", (unsigned long) prefer);
94020253Sjoerg			addarg(&grpargs, 'g', tmp);
94120253Sjoerg		}
94220267Sjoerg		if (getarg(args, 'N'))
94320267Sjoerg		{
94420267Sjoerg			addarg(&grpargs, 'N', NULL);
94520267Sjoerg			addarg(&grpargs, 'q', NULL);
94620267Sjoerg			gid = pw_group(cnf, M_NEXT, &grpargs);
94720267Sjoerg		}
94820267Sjoerg		else
94920267Sjoerg		{
95020267Sjoerg			pw_group(cnf, M_ADD, &grpargs);
95144229Sdavidn			if ((grp = GETGRNAM(nam)) != NULL)
95220267Sjoerg				gid = grp->gr_gid;
95320267Sjoerg		}
95470486Sben		a_gid = LIST_FIRST(&grpargs);
95520253Sjoerg		while (a_gid != NULL) {
95670486Sben			struct carg    *t = LIST_NEXT(a_gid, list);
95720253Sjoerg			LIST_REMOVE(a_gid, list);
95820253Sjoerg			a_gid = t;
95920253Sjoerg		}
96020253Sjoerg	}
96144229Sdavidn	ENDGRENT();
96220253Sjoerg	return gid;
96320253Sjoerg}
96420253Sjoerg
96520253Sjoerg
96620253Sjoergstatic          time_t
96720253Sjoergpw_pwdpolicy(struct userconf * cnf, struct cargs * args)
96820253Sjoerg{
96920253Sjoerg	time_t          result = 0;
97020253Sjoerg	time_t          now = time(NULL);
97127831Sdavidn	struct carg    *arg = getarg(args, 'p');
97220253Sjoerg
97320253Sjoerg	if (arg != NULL) {
97420253Sjoerg		if ((result = parse_date(now, arg->val)) == now)
97530259Scharnier			errx(EX_DATAERR, "invalid date/time `%s'", arg->val);
97620253Sjoerg	} else if (cnf->password_days > 0)
97720253Sjoerg		result = now + ((long) cnf->password_days * 86400L);
97820253Sjoerg	return result;
97920253Sjoerg}
98020253Sjoerg
98120253Sjoerg
98220253Sjoergstatic          time_t
98320253Sjoergpw_exppolicy(struct userconf * cnf, struct cargs * args)
98420253Sjoerg{
98520253Sjoerg	time_t          result = 0;
98620253Sjoerg	time_t          now = time(NULL);
98720253Sjoerg	struct carg    *arg = getarg(args, 'e');
98820253Sjoerg
98920253Sjoerg	if (arg != NULL) {
99020253Sjoerg		if ((result = parse_date(now, arg->val)) == now)
99130259Scharnier			errx(EX_DATAERR, "invalid date/time `%s'", arg->val);
99220253Sjoerg	} else if (cnf->expire_days > 0)
99320253Sjoerg		result = now + ((long) cnf->expire_days * 86400L);
99420253Sjoerg	return result;
99520253Sjoerg}
99620253Sjoerg
99720253Sjoerg
99820253Sjoergstatic char    *
99920253Sjoergpw_homepolicy(struct userconf * cnf, struct cargs * args, char const * user)
100020253Sjoerg{
100120253Sjoerg	struct carg    *arg = getarg(args, 'd');
100220253Sjoerg
100320253Sjoerg	if (arg)
100420253Sjoerg		return arg->val;
100520253Sjoerg	else {
100620253Sjoerg		static char     home[128];
100720253Sjoerg
100820253Sjoerg		if (cnf->home == NULL || *cnf->home == '\0')
100930259Scharnier			errx(EX_CONFIG, "no base home directory set");
101020253Sjoerg		sprintf(home, "%s/%s", cnf->home, user);
101120253Sjoerg		return home;
101220253Sjoerg	}
101320253Sjoerg}
101420253Sjoerg
101520253Sjoergstatic char    *
101620253Sjoergshell_path(char const * path, char *shells[], char *sh)
101720253Sjoerg{
101820253Sjoerg	if (sh != NULL && (*sh == '/' || *sh == '\0'))
101920253Sjoerg		return sh;	/* specified full path or forced none */
102020253Sjoerg	else {
102120253Sjoerg		char           *p;
102220253Sjoerg		char            paths[_UC_MAXLINE];
102320253Sjoerg
102420253Sjoerg		/*
102520253Sjoerg		 * We need to search paths
102620253Sjoerg		 */
1027130633Srobert		strlcpy(paths, path, sizeof(paths));
102820253Sjoerg		for (p = strtok(paths, ": \t\r\n"); p != NULL; p = strtok(NULL, ": \t\r\n")) {
102920253Sjoerg			int             i;
103020253Sjoerg			static char     shellpath[256];
103120253Sjoerg
103220253Sjoerg			if (sh != NULL) {
103320253Sjoerg				sprintf(shellpath, "%s/%s", p, sh);
103420253Sjoerg				if (access(shellpath, X_OK) == 0)
103520253Sjoerg					return shellpath;
103620253Sjoerg			} else
103720253Sjoerg				for (i = 0; i < _UC_MAXSHELLS && shells[i] != NULL; i++) {
103820253Sjoerg					sprintf(shellpath, "%s/%s", p, shells[i]);
103920253Sjoerg					if (access(shellpath, X_OK) == 0)
104020253Sjoerg						return shellpath;
104120253Sjoerg				}
104220253Sjoerg		}
104320253Sjoerg		if (sh == NULL)
104430259Scharnier			errx(EX_OSFILE, "can't find shell `%s' in shell paths", sh);
104530259Scharnier		errx(EX_CONFIG, "no default shell available or defined");
104620253Sjoerg		return NULL;
104720253Sjoerg	}
104820253Sjoerg}
104920253Sjoerg
105020253Sjoerg
105120253Sjoergstatic char    *
105220253Sjoergpw_shellpolicy(struct userconf * cnf, struct cargs * args, char *newshell)
105320253Sjoerg{
105420253Sjoerg	char           *sh = newshell;
105520253Sjoerg	struct carg    *arg = getarg(args, 's');
105620253Sjoerg
105720253Sjoerg	if (newshell == NULL && arg != NULL)
105820253Sjoerg		sh = arg->val;
105920253Sjoerg	return shell_path(cnf->shelldir, cnf->shells, sh ? sh : cnf->shell_default);
106020253Sjoerg}
106120253Sjoerg
1062179365Santoine#define	SALTSIZE	32
106320253Sjoerg
1064179365Santoinestatic char const chars[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ./";
1065179365Santoine
106620253Sjoergchar           *
106720253Sjoergpw_pwcrypt(char *password)
106820253Sjoerg{
106920253Sjoerg	int             i;
1070179365Santoine	char            salt[SALTSIZE + 1];
1071231994Skevlo	char		*cryptpw;
107220253Sjoerg
107320253Sjoerg	static char     buf[256];
107420253Sjoerg
107520253Sjoerg	/*
107620253Sjoerg	 * Calculate a salt value
107720253Sjoerg	 */
1078179365Santoine	for (i = 0; i < SALTSIZE; i++)
1079181785Sache		salt[i] = chars[arc4random_uniform(sizeof(chars) - 1)];
1080179365Santoine	salt[SALTSIZE] = '\0';
108120253Sjoerg
1082231994Skevlo	cryptpw = crypt(password, salt);
1083231994Skevlo	if (cryptpw == NULL)
1084231994Skevlo		errx(EX_CONFIG, "crypt(3) failure");
1085231994Skevlo	return strcpy(buf, cryptpw);
108620253Sjoerg}
108720253Sjoerg
108820590Sdavidn
108920253Sjoergstatic char    *
109020253Sjoergpw_password(struct userconf * cnf, struct cargs * args, char const * user)
109120253Sjoerg{
109220253Sjoerg	int             i, l;
109320253Sjoerg	char            pwbuf[32];
109420253Sjoerg
109520253Sjoerg	switch (cnf->default_password) {
109620253Sjoerg	case -1:		/* Random password */
109773563Skris		l = (arc4random() % 8 + 8);	/* 8 - 16 chars */
109820253Sjoerg		for (i = 0; i < l; i++)
1099181785Sache			pwbuf[i] = chars[arc4random_uniform(sizeof(chars)-1)];
110020253Sjoerg		pwbuf[i] = '\0';
110120253Sjoerg
110220253Sjoerg		/*
110320253Sjoerg		 * We give this information back to the user
110420253Sjoerg		 */
1105124382Siedowse		if (getarg(args, 'h') == NULL && getarg(args, 'H') == NULL &&
1106124382Siedowse		    getarg(args, 'N') == NULL) {
110761957Sache			if (isatty(STDOUT_FILENO))
110820712Sdavidn				printf("Password for '%s' is: ", user);
110920253Sjoerg			printf("%s\n", pwbuf);
111020253Sjoerg			fflush(stdout);
111120253Sjoerg		}
111220253Sjoerg		break;
111320253Sjoerg
111420253Sjoerg	case -2:		/* No password at all! */
111520253Sjoerg		return "";
111620253Sjoerg
111720253Sjoerg	case 0:		/* No login - default */
111820253Sjoerg	default:
111920253Sjoerg		return "*";
112020253Sjoerg
112120253Sjoerg	case 1:		/* user's name */
1122130633Srobert		strlcpy(pwbuf, user, sizeof(pwbuf));
112320253Sjoerg		break;
112420253Sjoerg	}
112520253Sjoerg	return pw_pwcrypt(pwbuf);
112620253Sjoerg}
112720253Sjoerg
112820253Sjoerg
112920253Sjoergstatic int
113044386Sdavidnprint_user(struct passwd * pwd, int pretty, int v7)
113120253Sjoerg{
113220253Sjoerg	if (!pretty) {
1133242349Sbapt		char            *buf;
113420253Sjoerg
1135242349Sbapt		if (!v7)
1136242349Sbapt			pwd->pw_passwd = (pwd->pw_passwd == NULL) ? "" : "*";
1137242349Sbapt
1138242349Sbapt		buf = v7 ? pw_make_v7(pwd) : pw_make(pwd);
1139242349Sbapt		printf("%s\n", buf);
1140242349Sbapt		free(buf);
114120253Sjoerg	} else {
114220267Sjoerg		int		j;
114320253Sjoerg		char           *p;
114444229Sdavidn		struct group   *grp = GETGRGID(pwd->pw_gid);
114520253Sjoerg		char            uname[60] = "User &", office[60] = "[None]",
114620253Sjoerg		                wphone[60] = "[None]", hphone[60] = "[None]";
114720590Sdavidn		char		acexpire[32] = "[None]", pwexpire[32] = "[None]";
114820590Sdavidn		struct tm *    tptr;
114920253Sjoerg
115020253Sjoerg		if ((p = strtok(pwd->pw_gecos, ",")) != NULL) {
1151130633Srobert			strlcpy(uname, p, sizeof(uname));
115220253Sjoerg			if ((p = strtok(NULL, ",")) != NULL) {
1153130633Srobert				strlcpy(office, p, sizeof(office));
115420253Sjoerg				if ((p = strtok(NULL, ",")) != NULL) {
1155130633Srobert					strlcpy(wphone, p, sizeof(wphone));
115620253Sjoerg					if ((p = strtok(NULL, "")) != NULL) {
1157130633Srobert						strlcpy(hphone, p,
1158130633Srobert						    sizeof(hphone));
115920253Sjoerg					}
116020253Sjoerg				}
116120253Sjoerg			}
116220253Sjoerg		}
116320253Sjoerg		/*
116420253Sjoerg		 * Handle '&' in gecos field
116520253Sjoerg		 */
116620253Sjoerg		if ((p = strchr(uname, '&')) != NULL) {
116720253Sjoerg			int             l = strlen(pwd->pw_name);
116820253Sjoerg			int             m = strlen(p);
116920253Sjoerg
117020253Sjoerg			memmove(p + l, p + 1, m);
117120253Sjoerg			memmove(p, pwd->pw_name, l);
117261957Sache			*p = (char) toupper((unsigned char)*p);
117320253Sjoerg		}
117420590Sdavidn		if (pwd->pw_expire > (time_t)0 && (tptr = localtime(&pwd->pw_expire)) != NULL)
117574569Sache			strftime(acexpire, sizeof acexpire, "%c", tptr);
117661957Sache		if (pwd->pw_change > (time_t)0 && (tptr = localtime(&pwd->pw_change)) != NULL)
117774569Sache			strftime(pwexpire, sizeof pwexpire, "%c", tptr);
117822394Sdavidn		printf("Login Name: %-15s   #%-12ld Group: %-15s   #%ld\n"
117920747Sdavidn		       " Full Name: %s\n"
118020747Sdavidn		       "      Home: %-26.26s      Class: %s\n"
118120747Sdavidn		       "     Shell: %-26.26s     Office: %s\n"
118220747Sdavidn		       "Work Phone: %-26.26s Home Phone: %s\n"
118320747Sdavidn		       "Acc Expire: %-26.26s Pwd Expire: %s\n",
118420253Sjoerg		       pwd->pw_name, (long) pwd->pw_uid,
118520253Sjoerg		       grp ? grp->gr_name : "(invalid)", (long) pwd->pw_gid,
118620253Sjoerg		       uname, pwd->pw_dir, pwd->pw_class,
118720590Sdavidn		       pwd->pw_shell, office, wphone, hphone,
118820590Sdavidn		       acexpire, pwexpire);
118944229Sdavidn	        SETGRENT();
119020267Sjoerg		j = 0;
119144229Sdavidn		while ((grp=GETGRENT()) != NULL)
119220267Sjoerg		{
119320267Sjoerg			int     i = 0;
119420747Sdavidn			while (grp->gr_mem[i] != NULL)
119520267Sjoerg			{
119620267Sjoerg				if (strcmp(grp->gr_mem[i], pwd->pw_name)==0)
119720267Sjoerg				{
119820747Sdavidn					printf(j++ == 0 ? "    Groups: %s" : ",%s", grp->gr_name);
119920267Sjoerg					break;
120020267Sjoerg				}
120120267Sjoerg				++i;
120220267Sjoerg			}
120320267Sjoerg		}
120444229Sdavidn		ENDGRENT();
120561957Sache		printf("%s", j ? "\n" : "");
120620253Sjoerg	}
120720267Sjoerg	return EXIT_SUCCESS;
120820253Sjoerg}
120920253Sjoerg
121020679Sdavidnchar    *
121120679Sdavidnpw_checkname(u_char *name, int gecos)
121220253Sjoerg{
1213109961Sgad	char showch[8];
1214109961Sgad	u_char const *badchars, *ch, *showtype;
1215109961Sgad	int reject;
121620253Sjoerg
1217109961Sgad	ch = name;
1218109961Sgad	reject = 0;
1219109961Sgad	if (gecos) {
1220109961Sgad		/* See if the name is valid as a gecos (comment) field. */
1221109961Sgad		badchars = ":!@";
1222109961Sgad		showtype = "gecos field";
1223109961Sgad	} else {
1224109961Sgad		/* See if the name is valid as a userid or group. */
1225109961Sgad		badchars = " ,\t:+&#%$^()!@~*?<>=|\\/\"";
1226109961Sgad		showtype = "userid/group name";
1227109961Sgad		/* Userids and groups can not have a leading '-'. */
1228109961Sgad		if (*ch == '-')
1229109961Sgad			reject = 1;
123020253Sjoerg	}
1231109961Sgad	if (!reject) {
1232109961Sgad		while (*ch) {
1233109961Sgad			if (strchr(badchars, *ch) != NULL || *ch < ' ' ||
1234109961Sgad			    *ch == 127) {
1235109961Sgad				reject = 1;
1236109961Sgad				break;
1237109961Sgad			}
1238109961Sgad			/* 8-bit characters are only allowed in GECOS fields */
1239109961Sgad			if (!gecos && (*ch & 0x80)) {
1240109961Sgad				reject = 1;
1241109961Sgad				break;
1242109961Sgad			}
1243109961Sgad			ch++;
1244109961Sgad		}
1245109961Sgad	}
1246109961Sgad	/*
1247109961Sgad	 * A `$' is allowed as the final character for userids and groups,
1248109961Sgad	 * mainly for the benefit of samba.
1249109961Sgad	 */
1250109961Sgad	if (reject && !gecos) {
1251109961Sgad		if (*ch == '$' && *(ch + 1) == '\0') {
1252109961Sgad			reject = 0;
1253109961Sgad			ch++;
1254109961Sgad		}
1255109961Sgad	}
1256109961Sgad	if (reject) {
1257109961Sgad		snprintf(showch, sizeof(showch), (*ch >= ' ' && *ch < 127)
1258109961Sgad		    ? "`%c'" : "0x%02x", *ch);
1259228673Sdim		errx(EX_DATAERR, "invalid character %s at position %td in %s",
1260109961Sgad		    showch, (ch - name), showtype);
1261109961Sgad	}
1262109961Sgad	if (!gecos && (ch - name) > LOGNAMESIZE)
1263109961Sgad		errx(EX_DATAERR, "name too long `%s' (max is %d)", name,
1264109961Sgad		    LOGNAMESIZE);
126520679Sdavidn	return (char *)name;
126620253Sjoerg}
126720253Sjoerg
126820253Sjoerg
126920253Sjoergstatic void
127020253Sjoergrmat(uid_t uid)
127120253Sjoerg{
127220253Sjoerg	DIR            *d = opendir("/var/at/jobs");
127320253Sjoerg
127420253Sjoerg	if (d != NULL) {
127520253Sjoerg		struct dirent  *e;
127620253Sjoerg
127720253Sjoerg		while ((e = readdir(d)) != NULL) {
127820253Sjoerg			struct stat     st;
127920253Sjoerg
128020253Sjoerg			if (strncmp(e->d_name, ".lock", 5) != 0 &&
128120253Sjoerg			    stat(e->d_name, &st) == 0 &&
128220253Sjoerg			    !S_ISDIR(st.st_mode) &&
128320253Sjoerg			    st.st_uid == uid) {
128420253Sjoerg				char            tmp[MAXPATHLEN];
128520253Sjoerg
128620253Sjoerg				sprintf(tmp, "/usr/bin/atrm %s", e->d_name);
128720253Sjoerg				system(tmp);
128820253Sjoerg			}
128920253Sjoerg		}
129020253Sjoerg		closedir(d);
129120253Sjoerg	}
129220253Sjoerg}
129320747Sdavidn
129420747Sdavidnstatic void
129585145Sachermopie(char const * name)
129620747Sdavidn{
129785145Sache	static const char etcopie[] = "/etc/opiekeys";
129885145Sache	FILE   *fp = fopen(etcopie, "r+");
129920747Sdavidn
130021052Sdavidn	if (fp != NULL) {
130121052Sdavidn		char	tmp[1024];
130221052Sdavidn		off_t	atofs = 0;
130321052Sdavidn		int	length = strlen(name);
130420747Sdavidn
130521052Sdavidn		while (fgets(tmp, sizeof tmp, fp) != NULL) {
130621052Sdavidn			if (strncmp(name, tmp, length) == 0 && tmp[length]==' ') {
130721052Sdavidn				if (fseek(fp, atofs, SEEK_SET) == 0) {
130821052Sdavidn					fwrite("#", 1, 1, fp);	/* Comment username out */
130921052Sdavidn				}
131021052Sdavidn				break;
131120747Sdavidn			}
131221052Sdavidn			atofs = ftell(fp);
131320747Sdavidn		}
131421052Sdavidn		/*
131521052Sdavidn		 * If we got an error of any sort, don't update!
131621052Sdavidn		 */
131721052Sdavidn		fclose(fp);
131820747Sdavidn	}
131920747Sdavidn}
132020747Sdavidn
1321