pw_user.c revision 20302
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.
2520253Sjoerg *
2620302Sjoerg *	$Id: pw_user.c,v 1.1.1.2 1996/12/09 23:55:10 joerg Exp $
2720253Sjoerg */
2820253Sjoerg
2920253Sjoerg#include <unistd.h>
3020253Sjoerg#include <fcntl.h>
3120253Sjoerg#include <ctype.h>
3220253Sjoerg#include <paths.h>
3320253Sjoerg#include <sys/param.h>
3420253Sjoerg#include <dirent.h>
3520253Sjoerg#include <termios.h>
3620253Sjoerg#include "pw.h"
3720253Sjoerg#include "bitmap.h"
3820253Sjoerg#include "pwupd.h"
3920253Sjoerg
4020253Sjoergstatic int      print_user(struct passwd * pwd, int pretty);
4120253Sjoergstatic uid_t    pw_uidpolicy(struct userconf * cnf, struct cargs * args);
4220253Sjoergstatic uid_t    pw_gidpolicy(struct userconf * cnf, struct cargs * args, char *nam, gid_t prefer);
4320253Sjoergstatic time_t   pw_pwdpolicy(struct userconf * cnf, struct cargs * args);
4420253Sjoergstatic time_t   pw_exppolicy(struct userconf * cnf, struct cargs * args);
4520253Sjoergstatic char    *pw_homepolicy(struct userconf * cnf, struct cargs * args, char const * user);
4620253Sjoergstatic char    *pw_shellpolicy(struct userconf * cnf, struct cargs * args, char *newshell);
4720253Sjoergstatic char    *pw_password(struct userconf * cnf, struct cargs * args, char const * user);
4820253Sjoergstatic char    *pw_checkname(char *name, int gecos);
4920253Sjoergstatic char    *shell_path(char const * path, char *shells[], char *sh);
5020253Sjoergstatic void     rmat(uid_t uid);
5120253Sjoerg
5220253Sjoerg/*-
5320253Sjoerg * -C config      configuration file
5420253Sjoerg * -q             quiet operation
5520253Sjoerg * -n name        login name
5620253Sjoerg * -u uid         user id
5720253Sjoerg * -c comment     user name/comment
5820253Sjoerg * -d directory   home directory
5920253Sjoerg * -e date        account expiry date
6020253Sjoerg * -p date        password expiry date
6120253Sjoerg * -g grp         primary group
6220253Sjoerg * -G grp1,grp2   additional groups
6320253Sjoerg * -m [ -k dir ]  create and set up home
6420253Sjoerg * -s shell       name of login shell
6520253Sjoerg * -o             duplicate uid ok
6620253Sjoerg * -L class       user class
6720253Sjoerg * -l name        new login name
6820253Sjoerg * -h fd          password filehandle
6920253Sjoerg * -F             force print or add
7020253Sjoerg *   Setting defaults:
7120253Sjoerg * -D             set user defaults
7220253Sjoerg * -b dir         default home root dir
7320253Sjoerg * -e period      default expiry period
7420253Sjoerg * -p period      default password change period
7520253Sjoerg * -g group       default group
7620253Sjoerg * -G             grp1,grp2.. default additional groups
7720253Sjoerg * -L class       default login class
7820253Sjoerg * -k dir         default home skeleton
7920253Sjoerg * -s shell       default shell
8020253Sjoerg * -w method      default password method
8120253Sjoerg */
8220253Sjoerg
8320253Sjoergint
8420253Sjoergpw_user(struct userconf * cnf, int mode, struct cargs * args)
8520253Sjoerg{
8620253Sjoerg	char           *p = NULL;
8720253Sjoerg	struct carg    *a_name;
8820253Sjoerg	struct carg    *a_uid;
8920253Sjoerg	struct carg    *arg;
9020253Sjoerg	struct passwd  *pwd = NULL;
9120253Sjoerg	struct group   *grp;
9220253Sjoerg	struct stat     st;
9320253Sjoerg	char            line[MAXPWLINE];
9420253Sjoerg
9520253Sjoerg	static struct passwd fakeuser =
9620253Sjoerg	{
9720253Sjoerg		NULL,
9820253Sjoerg		"*",
9920253Sjoerg		-1,
10020253Sjoerg		-1,
10120253Sjoerg		0,
10220253Sjoerg		"",
10320253Sjoerg		"User &",
10420253Sjoerg		"/bin/sh",
10520253Sjoerg		0,
10620253Sjoerg		0
10720253Sjoerg	};
10820253Sjoerg
10920253Sjoerg	/*
11020267Sjoerg	 * With M_NEXT, we only need to return the
11120267Sjoerg	 * next uid to stdout
11220267Sjoerg	 */
11320267Sjoerg	if (mode == M_NEXT)
11420267Sjoerg	{
11520267Sjoerg		uid_t next = pw_uidpolicy(cnf, args);
11620267Sjoerg		if (getarg(args, 'q'))
11720267Sjoerg			return next;
11820267Sjoerg		printf("%ld:", (long)next);
11920267Sjoerg		pw_group(cnf, mode, args);
12020267Sjoerg		return EXIT_SUCCESS;
12120267Sjoerg	}
12220267Sjoerg
12320267Sjoerg	/*
12420253Sjoerg	 * We can do all of the common legwork here
12520253Sjoerg	 */
12620253Sjoerg
12720253Sjoerg	if ((arg = getarg(args, 'b')) != NULL) {
12820267Sjoerg		cnf->home = arg->val;
12920267Sjoerg		if (stat(cnf->home, &st) == -1 || !S_ISDIR(st.st_mode))
13020267Sjoerg			cmderr(EX_OSFILE, "root home `%s' is not a directory or does not exist\n", cnf->home);
13120253Sjoerg	}
13220253Sjoerg	if ((arg = getarg(args, 'e')) != NULL)
13320253Sjoerg		cnf->expire_days = atoi(arg->val);
13420253Sjoerg
13520253Sjoerg	if ((arg = getarg(args, 'p')) != NULL && arg->val)
13620253Sjoerg		cnf->password_days = atoi(arg->val);
13720253Sjoerg
13820253Sjoerg	if ((arg = getarg(args, 'g')) != NULL) {
13920253Sjoerg		p = arg->val;
14020253Sjoerg		if ((grp = getgrnam(p)) == NULL) {
14120253Sjoerg			if (!isdigit(*p) || (grp = getgrgid((gid_t) atoi(p))) == NULL)
14220267Sjoerg				cmderr(EX_NOUSER, "group `%s' does not exist\n", p);
14320253Sjoerg		}
14420253Sjoerg		cnf->default_group = newstr(grp->gr_name);
14520253Sjoerg	}
14620253Sjoerg	if ((arg = getarg(args, 'L')) != NULL)
14720253Sjoerg		cnf->default_class = pw_checkname(arg->val, 0);
14820253Sjoerg
14920253Sjoerg	if ((arg = getarg(args, 'G')) != NULL && arg->val) {
15020253Sjoerg		int             i = 0;
15120253Sjoerg
15220253Sjoerg		for (p = strtok(arg->val, ", \t"); i < _UC_MAXGROUPS && p != NULL; p = strtok(NULL, ", \t")) {
15320253Sjoerg			if ((grp = getgrnam(p)) == NULL) {
15420253Sjoerg				if (!isdigit(*p) || (grp = getgrgid((gid_t) atoi(p))) == NULL)
15520267Sjoerg					cmderr(EX_NOUSER, "group `%s' does not exist\n", p);
15620253Sjoerg			}
15720253Sjoerg			cnf->groups[i++] = newstr(grp->gr_name);
15820253Sjoerg		}
15920253Sjoerg		while (i < _UC_MAXGROUPS)
16020253Sjoerg			cnf->groups[i++] = NULL;
16120253Sjoerg	}
16220253Sjoerg	if ((arg = getarg(args, 'k')) != NULL) {
16320253Sjoerg		if (stat(cnf->dotdir = arg->val, &st) == -1 || S_ISDIR(st.st_mode))
16420267Sjoerg			cmderr(EX_OSFILE, "skeleton `%s' is not a directory or does not exist\n", cnf->dotdir);
16520253Sjoerg	}
16620253Sjoerg	if ((arg = getarg(args, 's')) != NULL)
16720253Sjoerg		cnf->shell_default = arg->val;
16820253Sjoerg
16920253Sjoerg	if (mode == M_ADD && getarg(args, 'D')) {
17020253Sjoerg		if (getarg(args, 'n') != NULL)
17120267Sjoerg			cmderr(EX_DATAERR, "can't combine `-D' with `-n name'\n");
17220253Sjoerg		if ((arg = getarg(args, 'u')) != NULL && (p = strtok(arg->val, ", \t")) != NULL) {
17320253Sjoerg			if ((cnf->min_uid = (uid_t) atoi(p)) == 0)
17420253Sjoerg				cnf->min_uid = 1000;
17520253Sjoerg			if ((p = strtok(NULL, " ,\t")) == NULL || (cnf->max_uid = (uid_t) atoi(p)) < cnf->min_uid)
17620253Sjoerg				cnf->max_uid = 32000;
17720253Sjoerg		}
17820253Sjoerg		if ((arg = getarg(args, 'i')) != NULL && (p = strtok(arg->val, ", \t")) != NULL) {
17920253Sjoerg			if ((cnf->min_gid = (gid_t) atoi(p)) == 0)
18020253Sjoerg				cnf->min_gid = 1000;
18120253Sjoerg			if ((p = strtok(NULL, " ,\t")) == NULL || (cnf->max_gid = (gid_t) atoi(p)) < cnf->min_gid)
18220253Sjoerg				cnf->max_gid = 32000;
18320253Sjoerg		}
18420253Sjoerg		if ((arg = getarg(args, 'w')) != NULL)
18520253Sjoerg			cnf->default_password = boolean_val(arg->val, cnf->default_password);
18620253Sjoerg
18720253Sjoerg		arg = getarg(args, 'C');
18820253Sjoerg		if (write_userconfig(arg ? arg->val : NULL))
18920267Sjoerg			return EXIT_SUCCESS;
19020253Sjoerg		perror("config update");
19120267Sjoerg		return EX_IOERR;
19220253Sjoerg	}
19320253Sjoerg	if (mode == M_PRINT && getarg(args, 'a')) {
19420267Sjoerg		int             pretty = getarg(args, 'P') != NULL;
19520253Sjoerg
19620253Sjoerg		setpwent();
19720253Sjoerg		while ((pwd = getpwent()) != NULL)
19820253Sjoerg			print_user(pwd, pretty);
19920253Sjoerg		endpwent();
20020267Sjoerg		return EXIT_SUCCESS;
20120253Sjoerg	}
20220253Sjoerg	if ((a_name = getarg(args, 'n')) != NULL)
20320253Sjoerg		pwd = getpwnam(pw_checkname(a_name->val, 0));
20420253Sjoerg	a_uid = getarg(args, 'u');
20520253Sjoerg
20620253Sjoerg	if (a_uid == NULL) {
20720253Sjoerg		if (a_name == NULL)
20820267Sjoerg			cmderr(EX_DATAERR, "user name or id required\n");
20920253Sjoerg
21020253Sjoerg		/*
21120253Sjoerg		 * Determine whether 'n' switch is name or uid - we don't
21220253Sjoerg		 * really don't really care which we have, but we need to
21320253Sjoerg		 * know.
21420253Sjoerg		 */
21520253Sjoerg		if (mode != M_ADD && pwd == NULL && isdigit(*a_name->val) && atoi(a_name->val) > 0) {	/* Assume uid */
21620253Sjoerg			(a_uid = a_name)->ch = 'u';
21720253Sjoerg			a_name = NULL;
21820253Sjoerg		}
21920253Sjoerg	}
22020253Sjoerg	/*
22120253Sjoerg	 * Update, delete & print require that the user exists
22220253Sjoerg	 */
22320253Sjoerg	if (mode == M_UPDATE || mode == M_DELETE || mode == M_PRINT) {
22420253Sjoerg		if (a_name == NULL && pwd == NULL)	/* Try harder */
22520253Sjoerg			pwd = getpwuid(atoi(a_uid->val));
22620253Sjoerg
22720253Sjoerg		if (pwd == NULL) {
22820253Sjoerg			if (mode == M_PRINT && getarg(args, 'F')) {
22920253Sjoerg				fakeuser.pw_name = a_name ? a_name->val : "nouser";
23020253Sjoerg				fakeuser.pw_uid = a_uid ? (uid_t) atol(a_uid->val) : -1;
23120267Sjoerg				return print_user(&fakeuser, getarg(args, 'P') != NULL);
23220253Sjoerg			}
23320253Sjoerg			if (a_name == NULL)
23420267Sjoerg				cmderr(EX_NOUSER, "no such uid `%s'\n", a_uid->val);
23520267Sjoerg			cmderr(EX_NOUSER, "no such user `%s'\n", a_name->val);
23620253Sjoerg		}
23720253Sjoerg		if (a_name == NULL)	/* May be needed later */
23820253Sjoerg			a_name = addarg(args, 'n', newstr(pwd->pw_name));
23920253Sjoerg
24020253Sjoerg		/*
24120253Sjoerg		 * Handle deletions now
24220253Sjoerg		 */
24320253Sjoerg		if (mode == M_DELETE) {
24420253Sjoerg			char            file[MAXPATHLEN];
24520253Sjoerg			char            home[MAXPATHLEN];
24620253Sjoerg			uid_t           uid = pwd->pw_uid;
24720253Sjoerg
24820253Sjoerg			if (strcmp(pwd->pw_name, "root") == 0)
24920267Sjoerg				cmderr(EX_DATAERR, "cannot remove user 'root'\n");
25020253Sjoerg
25120253Sjoerg			/*
25220253Sjoerg			 * Remove crontabs
25320253Sjoerg			 */
25420253Sjoerg			sprintf(file, "/var/cron/tabs/%s", pwd->pw_name);
25520253Sjoerg			if (access(file, F_OK) == 0) {
25620253Sjoerg				sprintf(file, "crontab -u %s -r", pwd->pw_name);
25720253Sjoerg				system(file);
25820253Sjoerg			}
25920253Sjoerg			/*
26020253Sjoerg			 * Save these for later, since contents of pwd may be
26120253Sjoerg			 * invalidated by deletion
26220253Sjoerg			 */
26320253Sjoerg			sprintf(file, "%s/%s", _PATH_MAILDIR, pwd->pw_name);
26420253Sjoerg			strncpy(home, pwd->pw_dir, sizeof home);
26520253Sjoerg			home[sizeof home - 1] = '\0';
26620253Sjoerg
26720253Sjoerg			if (!delpwent(pwd))
26820267Sjoerg				cmderr(EX_IOERR, "Error updating passwd file: %s\n", strerror(errno));
26920253Sjoerg			editgroups(a_name->val, NULL);
27020253Sjoerg
27120253Sjoerg			pw_log(cnf, mode, W_USER, "%s(%ld) account removed", a_name->val, (long) uid);
27220253Sjoerg
27320253Sjoerg			/*
27420253Sjoerg			 * Remove mail file
27520253Sjoerg			 */
27620253Sjoerg			remove(file);
27720253Sjoerg
27820253Sjoerg			/*
27920253Sjoerg			 * Remove at jobs
28020253Sjoerg			 */
28120253Sjoerg			if (getpwuid(uid) == NULL)
28220253Sjoerg				rmat(uid);
28320253Sjoerg
28420253Sjoerg			/*
28520253Sjoerg			 * Remove home directory and contents
28620253Sjoerg			 */
28720253Sjoerg			if (getarg(args, 'r') != NULL && *home == '/' && getpwuid(uid) == NULL) {
28820253Sjoerg				if (stat(home, &st) != -1) {
28920253Sjoerg					rm_r(home, uid);
29020253Sjoerg					pw_log(cnf, mode, W_USER, "%s(%ld) home '%s' %sremoved",
29120253Sjoerg					       a_name->val, (long) uid, home,
29220253Sjoerg					       stat(home, &st) == -1 ? "" : "not completely ");
29320253Sjoerg				}
29420253Sjoerg			}
29520267Sjoerg			return EXIT_SUCCESS;
29620253Sjoerg		} else if (mode == M_PRINT)
29720267Sjoerg			return print_user(pwd, getarg(args, 'P') != NULL);
29820253Sjoerg
29920253Sjoerg		/*
30020253Sjoerg		 * The rest is edit code
30120253Sjoerg		 */
30220253Sjoerg		if ((arg = getarg(args, 'l')) != NULL) {
30320253Sjoerg			if (strcmp(pwd->pw_name, "root") == 0)
30420267Sjoerg				cmderr(EX_DATAERR, "can't rename `root' account\n");
30520253Sjoerg			pwd->pw_name = pw_checkname(arg->val, 0);
30620253Sjoerg		}
30720253Sjoerg		if ((arg = getarg(args, 'u')) != NULL && isdigit(*arg->val)) {
30820253Sjoerg			pwd->pw_uid = (uid_t) atol(arg->val);
30920253Sjoerg			if (pwd->pw_uid != 0 && strcmp(pwd->pw_name, "root") == 0)
31020267Sjoerg				cmderr(EX_DATAERR, "can't change uid of `root' account\n");
31120253Sjoerg			if (pwd->pw_uid == 0 && strcmp(pwd->pw_name, "root") != 0)
31220253Sjoerg				fprintf(stderr, "WARNING: account `%s' will have a uid of 0 (superuser access!)\n", pwd->pw_name);
31320253Sjoerg		}
31420253Sjoerg		if ((arg = getarg(args, 'g')) != NULL && pwd->pw_uid != 0)	/* Already checked this */
31520253Sjoerg			pwd->pw_gid = (gid_t) getgrnam(cnf->default_group)->gr_gid;
31620253Sjoerg
31720253Sjoerg		if ((arg = getarg(args, 'p')) != NULL) {
31820253Sjoerg			if (*arg->val == '\0' || strcmp(arg->val, "0") == 0)
31920253Sjoerg				pwd->pw_change = 0;
32020253Sjoerg			else {
32120253Sjoerg				time_t          now = time(NULL);
32220253Sjoerg				time_t          expire = parse_date(now, arg->val);
32320253Sjoerg
32420253Sjoerg				if (now == expire)
32520267Sjoerg					cmderr(EX_DATAERR, "Invalid password change date `%s'\n", arg->val);
32620253Sjoerg				pwd->pw_change = expire;
32720253Sjoerg			}
32820253Sjoerg		}
32920267Sjoerg		if ((arg = getarg(args, 'e')) != NULL) {
33020253Sjoerg			if (*arg->val == '\0' || strcmp(arg->val, "0") == 0)
33120253Sjoerg				pwd->pw_expire = 0;
33220253Sjoerg			else {
33320253Sjoerg				time_t          now = time(NULL);
33420253Sjoerg				time_t          expire = parse_date(now, arg->val);
33520253Sjoerg
33620253Sjoerg				if (now == expire)
33720267Sjoerg					cmderr(EX_DATAERR, "Invalid account expiry date `%s'\n", arg->val);
33820253Sjoerg				pwd->pw_expire = expire;
33920253Sjoerg			}
34020253Sjoerg		}
34120253Sjoerg		if ((arg = getarg(args, 's')) != NULL)
34220253Sjoerg			pwd->pw_shell = shell_path(cnf->shelldir, cnf->shells, arg->val);
34320253Sjoerg
34420253Sjoerg		if (getarg(args, 'L'))
34520253Sjoerg			pwd->pw_class = cnf->default_class;
34620253Sjoerg
34720267Sjoerg		if ((arg = getarg(args, 'w')) != NULL && getarg(args, 'h') == NULL)
34820267Sjoerg			pwd->pw_passwd = pw_password(cnf, args, pwd->pw_name);
34920267Sjoerg
35020253Sjoerg	} else {
35120253Sjoerg		if (a_name == NULL)	/* Required */
35220267Sjoerg			cmderr(EX_DATAERR, "login name required\n");
35320253Sjoerg		else if ((pwd = getpwnam(a_name->val)) != NULL)	/* Exists */
35420267Sjoerg			cmderr(EX_DATAERR, "login name `%s' already exists\n", a_name->val);
35520253Sjoerg
35620253Sjoerg		/*
35720253Sjoerg		 * Now, set up defaults for a new user
35820253Sjoerg		 */
35920253Sjoerg		pwd = &fakeuser;
36020253Sjoerg		pwd->pw_name = a_name->val;
36120253Sjoerg		pwd->pw_class = cnf->default_class ? cnf->default_class : "";
36220253Sjoerg		pwd->pw_passwd = pw_password(cnf, args, pwd->pw_name);
36320253Sjoerg		pwd->pw_uid = pw_uidpolicy(cnf, args);
36420253Sjoerg		pwd->pw_gid = pw_gidpolicy(cnf, args, pwd->pw_name, (gid_t) pwd->pw_uid);
36520253Sjoerg		pwd->pw_change = pw_pwdpolicy(cnf, args);
36620253Sjoerg		pwd->pw_expire = pw_exppolicy(cnf, args);
36720253Sjoerg		pwd->pw_dir = pw_homepolicy(cnf, args, pwd->pw_name);
36820253Sjoerg		pwd->pw_shell = pw_shellpolicy(cnf, args, NULL);
36920253Sjoerg
37020253Sjoerg		if (pwd->pw_uid == 0 && strcmp(pwd->pw_name, "root") != 0)
37120253Sjoerg			fprintf(stderr, "WARNING: new account `%s' has a uid of 0 (superuser access!)\n", pwd->pw_name);
37220253Sjoerg	}
37320253Sjoerg
37420253Sjoerg	/*
37520253Sjoerg	 * Shared add/edit code
37620253Sjoerg	 */
37720253Sjoerg	if ((arg = getarg(args, 'c')) != NULL)
37820253Sjoerg		pwd->pw_gecos = pw_checkname(arg->val, 1);
37920253Sjoerg
38020253Sjoerg	if ((arg = getarg(args, 'h')) != NULL) {
38120253Sjoerg		if (strcmp(arg->val, "-") == 0)
38220253Sjoerg			pwd->pw_passwd = "*";	/* No access */
38320253Sjoerg		else {
38420253Sjoerg			int             fd = atoi(arg->val);
38520253Sjoerg			int             b;
38620253Sjoerg			int             istty = isatty(fd);
38720253Sjoerg			struct termios  t;
38820253Sjoerg
38920253Sjoerg			if (istty) {
39020253Sjoerg				if (tcgetattr(fd, &t) == -1)
39120253Sjoerg					istty = 0;
39220253Sjoerg				else {
39320253Sjoerg					struct termios  n = t;
39420253Sjoerg
39520253Sjoerg					/* Disable echo */
39620253Sjoerg					n.c_lflag &= ~(ECHO);
39720253Sjoerg					tcsetattr(fd, TCSANOW, &n);
39820253Sjoerg					printf("%sassword for user %s:", (mode == M_UPDATE) ? "New p" : "P", pwd->pw_name);
39920253Sjoerg					fflush(stdout);
40020253Sjoerg				}
40120253Sjoerg			}
40220253Sjoerg			b = read(fd, line, sizeof(line) - 1);
40320253Sjoerg			if (istty) {	/* Restore state */
40420253Sjoerg				tcsetattr(fd, TCSANOW, &t);
40520253Sjoerg				fputc('\n', stdout);
40620253Sjoerg				fflush(stdout);
40720253Sjoerg			}
40820253Sjoerg			if (b < 0) {
40920253Sjoerg				perror("-h file descriptor");
41020267Sjoerg				return EX_IOERR;
41120253Sjoerg			}
41220253Sjoerg			line[b] = '\0';
41320253Sjoerg			if ((p = strpbrk(line, " \t\r\n")) != NULL)
41420253Sjoerg				*p = '\0';
41520253Sjoerg			if (!*line)
41620267Sjoerg				cmderr(EX_DATAERR, "empty password read on file descriptor %d\n", fd);
41720253Sjoerg			pwd->pw_passwd = pw_pwcrypt(line);
41820253Sjoerg		}
41920253Sjoerg	}
42020267Sjoerg
42120267Sjoerg	/*
42220267Sjoerg	 * Special case: -N only displays & exits
42320267Sjoerg	 */
42420267Sjoerg	if (getarg(args, 'N') != NULL)
42520267Sjoerg		return print_user(pwd, getarg(args, 'P') != NULL);
42620267Sjoerg
42720253Sjoerg	if ((mode == M_ADD && !addpwent(pwd)) ||
42820253Sjoerg	    (mode == M_UPDATE && !chgpwent(a_name->val, pwd))) {
42920253Sjoerg		perror("password update");
43020267Sjoerg		return EX_IOERR;
43120253Sjoerg	}
43220253Sjoerg	/*
43320253Sjoerg	 * Ok, user is created or changed - now edit group file
43420253Sjoerg	 */
43520253Sjoerg
43620253Sjoerg	if (mode == M_ADD || getarg(args, 'G') != NULL)
43720253Sjoerg		editgroups(pwd->pw_name, cnf->groups);
43820253Sjoerg
43920253Sjoerg	/* pwd may have been invalidated */
44020253Sjoerg	if ((pwd = getpwnam(a_name->val)) == NULL)
44120267Sjoerg		cmderr(EX_NOUSER, "user '%s' disappeared during update\n", a_name->val);
44220253Sjoerg
44320253Sjoerg	grp = getgrgid(pwd->pw_gid);
44420253Sjoerg	pw_log(cnf, mode, W_USER, "%s(%ld):%s(%d):%s:%s:%s",
44520253Sjoerg	       pwd->pw_name, (long) pwd->pw_uid,
44620253Sjoerg	    grp ? grp->gr_name : "unknown", (long) (grp ? grp->gr_gid : -1),
44720253Sjoerg	       pwd->pw_gecos, pwd->pw_dir, pwd->pw_shell);
44820253Sjoerg
44920253Sjoerg	/*
45020253Sjoerg	 * If adding, let's touch and chown the user's mail file. This is not
45120253Sjoerg	 * strictly necessary under BSD with a 0755 maildir but it also
45220253Sjoerg	 * doesn't hurt anything to create the empty mailfile
45320253Sjoerg	 */
45420253Sjoerg	if (mode == M_ADD) {
45520253Sjoerg		FILE           *fp;
45620253Sjoerg
45720253Sjoerg		sprintf(line, "%s/%s", _PATH_MAILDIR, pwd->pw_name);
45820253Sjoerg		close(open(line, O_RDWR | O_CREAT, 0600));	/* Preserve contents &
45920253Sjoerg								 * mtime */
46020253Sjoerg		chown(line, pwd->pw_uid, pwd->pw_gid);
46120253Sjoerg
46220253Sjoerg		/*
46320253Sjoerg		 * Send mail to the new user as well, if we are asked to
46420253Sjoerg		 */
46520253Sjoerg		if (cnf->newmail && *cnf->newmail && (fp = fopen(cnf->newmail, "r")) != NULL) {
46620253Sjoerg			FILE           *pfp = popen(_PATH_SENDMAIL " -t", "w");
46720253Sjoerg
46820253Sjoerg			if (pfp == NULL)
46920253Sjoerg				perror("sendmail");
47020253Sjoerg			else {
47120253Sjoerg				fprintf(pfp, "From: root\n" "To: %s\n" "Subject: Welcome!\n\n", pwd->pw_name);
47220253Sjoerg				while (fgets(line, sizeof(line), fp) != NULL) {
47320253Sjoerg					/* Do substitutions? */
47420253Sjoerg					fputs(line, pfp);
47520253Sjoerg				}
47620253Sjoerg				pclose(pfp);
47720253Sjoerg				pw_log(cnf, mode, W_USER, "%s(%ld) new user mail sent",
47820253Sjoerg				       pwd->pw_name, (long) pwd->pw_uid);
47920253Sjoerg			}
48020253Sjoerg			fclose(fp);
48120253Sjoerg		}
48220253Sjoerg	}
48320253Sjoerg	/*
48420253Sjoerg	 * Finally, let's create and populate the user's home directory. Note
48520253Sjoerg	 * that this also `works' for editing users if -m is used, but
48620253Sjoerg	 * existing files will *not* be overwritten.
48720253Sjoerg	 */
48820253Sjoerg	if (getarg(args, 'm') != NULL && pwd->pw_dir && *pwd->pw_dir == '/' && pwd->pw_dir[1]) {
48920253Sjoerg		copymkdir(pwd->pw_dir, cnf->dotdir, 0755, pwd->pw_uid, pwd->pw_gid);
49020253Sjoerg		pw_log(cnf, mode, W_USER, "%s(%ld) home %s made",
49120253Sjoerg		       pwd->pw_name, (long) pwd->pw_uid, pwd->pw_dir);
49220253Sjoerg	}
49320267Sjoerg	return EXIT_SUCCESS;
49420253Sjoerg}
49520253Sjoerg
49620253Sjoerg
49720253Sjoergstatic          uid_t
49820253Sjoergpw_uidpolicy(struct userconf * cnf, struct cargs * args)
49920253Sjoerg{
50020253Sjoerg	struct passwd  *pwd;
50120253Sjoerg	uid_t           uid = (uid_t) - 1;
50220253Sjoerg	struct carg    *a_uid = getarg(args, 'u');
50320253Sjoerg
50420253Sjoerg	/*
50520253Sjoerg	 * Check the given uid, if any
50620253Sjoerg	 */
50720253Sjoerg	if (a_uid != NULL) {
50820253Sjoerg		uid = (uid_t) atol(a_uid->val);
50920253Sjoerg
51020253Sjoerg		if ((pwd = getpwuid(uid)) != NULL && getarg(args, 'o') == NULL)
51120267Sjoerg			cmderr(EX_DATAERR, "uid `%ld' has already been allocated\n", (long) pwd->pw_uid);
51220253Sjoerg	} else {
51320253Sjoerg		struct bitmap   bm;
51420253Sjoerg
51520253Sjoerg		/*
51620253Sjoerg		 * We need to allocate the next available uid under one of
51720253Sjoerg		 * two policies a) Grab the first unused uid b) Grab the
51820253Sjoerg		 * highest possible unused uid
51920253Sjoerg		 */
52020253Sjoerg		if (cnf->min_uid >= cnf->max_uid) {	/* Sanity
52120253Sjoerg							 * claus^H^H^H^Hheck */
52220253Sjoerg			cnf->min_uid = 1000;
52320253Sjoerg			cnf->max_uid = 32000;
52420253Sjoerg		}
52520253Sjoerg		bm = bm_alloc(cnf->max_uid - cnf->min_uid + 1);
52620253Sjoerg
52720253Sjoerg		/*
52820253Sjoerg		 * Now, let's fill the bitmap from the password file
52920253Sjoerg		 */
53020253Sjoerg		setpwent();
53120253Sjoerg		while ((pwd = getpwent()) != NULL)
53220253Sjoerg			if (pwd->pw_uid >= (int) cnf->min_uid && pwd->pw_uid <= (int) cnf->max_uid)
53320253Sjoerg				bm_setbit(&bm, pwd->pw_uid - cnf->min_uid);
53420253Sjoerg		endpwent();
53520253Sjoerg
53620253Sjoerg		/*
53720253Sjoerg		 * Then apply the policy, with fallback to reuse if necessary
53820253Sjoerg		 */
53920253Sjoerg		if (cnf->reuse_uids || (uid = (uid_t) (bm_lastset(&bm) + cnf->min_uid + 1)) > cnf->max_uid)
54020253Sjoerg			uid = (uid_t) (bm_firstunset(&bm) + cnf->min_uid);
54120253Sjoerg
54220253Sjoerg		/*
54320253Sjoerg		 * Another sanity check
54420253Sjoerg		 */
54520253Sjoerg		if (uid < cnf->min_uid || uid > cnf->max_uid)
54620267Sjoerg			cmderr(EX_SOFTWARE, "unable to allocate a new uid - range fully used\n");
54720253Sjoerg		bm_dealloc(&bm);
54820253Sjoerg	}
54920253Sjoerg	return uid;
55020253Sjoerg}
55120253Sjoerg
55220253Sjoerg
55320253Sjoergstatic          uid_t
55420253Sjoergpw_gidpolicy(struct userconf * cnf, struct cargs * args, char *nam, gid_t prefer)
55520253Sjoerg{
55620253Sjoerg	struct group   *grp;
55720253Sjoerg	gid_t           gid = (uid_t) - 1;
55820253Sjoerg	struct carg    *a_gid = getarg(args, 'g');
55920253Sjoerg
56020253Sjoerg	/*
56120253Sjoerg	 * If no arg given, see if default can help out
56220253Sjoerg	 */
56320253Sjoerg	if (a_gid == NULL && cnf->default_group && *cnf->default_group)
56420253Sjoerg		a_gid = addarg(args, 'g', cnf->default_group);
56520253Sjoerg
56620253Sjoerg	/*
56720253Sjoerg	 * Check the given gid, if any
56820253Sjoerg	 */
56920267Sjoerg	setgrent();
57020253Sjoerg	if (a_gid != NULL) {
57120253Sjoerg		if ((grp = getgrnam(a_gid->val)) == NULL) {
57220253Sjoerg			gid = (gid_t) atol(a_gid->val);
57320253Sjoerg			if ((gid == 0 && !isdigit(*a_gid->val)) || (grp = getgrgid(gid)) == NULL)
57420267Sjoerg				cmderr(EX_NOUSER, "group `%s' is not defined\n", a_gid->val);
57520253Sjoerg		}
57620253Sjoerg		gid = grp->gr_gid;
57720267Sjoerg	} else if ((grp = getgrnam(nam)) != NULL && grp->gr_mem[0] == NULL) {
57820267Sjoerg		gid = grp->gr_gid;  /* Already created? Use it anyway... */
57920253Sjoerg	} else {
58020253Sjoerg		struct cargs    grpargs;
58120253Sjoerg		char            tmp[32];
58220253Sjoerg
58320253Sjoerg		LIST_INIT(&grpargs);
58420253Sjoerg		addarg(&grpargs, 'n', nam);
58520253Sjoerg
58620253Sjoerg		/*
58720253Sjoerg		 * We need to auto-create a group with the user's name. We
58820253Sjoerg		 * can send all the appropriate output to our sister routine
58920253Sjoerg		 * bit first see if we can create a group with gid==uid so we
59020253Sjoerg		 * can keep the user and group ids in sync. We purposely do
59120253Sjoerg		 * NOT check the gid range if we can force the sync. If the
59220253Sjoerg		 * user's name dups an existing group, then the group add
59320253Sjoerg		 * function will happily handle that case for us and exit.
59420253Sjoerg		 */
59520253Sjoerg		if (getgrgid(prefer) == NULL) {
59620253Sjoerg			sprintf(tmp, "%lu", (unsigned long) prefer);
59720253Sjoerg			addarg(&grpargs, 'g', tmp);
59820253Sjoerg		}
59920267Sjoerg		if (getarg(args, 'N'))
60020267Sjoerg		{
60120267Sjoerg			addarg(&grpargs, 'N', NULL);
60220267Sjoerg			addarg(&grpargs, 'q', NULL);
60320267Sjoerg			gid = pw_group(cnf, M_NEXT, &grpargs);
60420267Sjoerg		}
60520267Sjoerg		else
60620267Sjoerg		{
60720267Sjoerg			pw_group(cnf, M_ADD, &grpargs);
60820267Sjoerg			if ((grp = getgrnam(nam)) != NULL)
60920267Sjoerg				gid = grp->gr_gid;
61020267Sjoerg		}
61120253Sjoerg		a_gid = grpargs.lh_first;
61220253Sjoerg		while (a_gid != NULL) {
61320253Sjoerg			struct carg    *t = a_gid->list.le_next;
61420253Sjoerg			LIST_REMOVE(a_gid, list);
61520253Sjoerg			a_gid = t;
61620253Sjoerg		}
61720253Sjoerg	}
61820267Sjoerg	endgrent();
61920253Sjoerg	return gid;
62020253Sjoerg}
62120253Sjoerg
62220253Sjoerg
62320253Sjoergstatic          time_t
62420253Sjoergpw_pwdpolicy(struct userconf * cnf, struct cargs * args)
62520253Sjoerg{
62620253Sjoerg	time_t          result = 0;
62720253Sjoerg	time_t          now = time(NULL);
62820253Sjoerg	struct carg    *arg = getarg(args, 'e');
62920253Sjoerg
63020253Sjoerg	if (arg != NULL) {
63120253Sjoerg		if ((result = parse_date(now, arg->val)) == now)
63220267Sjoerg			cmderr(EX_DATAERR, "invalid date/time `%s'\n", arg->val);
63320253Sjoerg	} else if (cnf->password_days > 0)
63420253Sjoerg		result = now + ((long) cnf->password_days * 86400L);
63520253Sjoerg	return result;
63620253Sjoerg}
63720253Sjoerg
63820253Sjoerg
63920253Sjoergstatic          time_t
64020253Sjoergpw_exppolicy(struct userconf * cnf, struct cargs * args)
64120253Sjoerg{
64220253Sjoerg	time_t          result = 0;
64320253Sjoerg	time_t          now = time(NULL);
64420253Sjoerg	struct carg    *arg = getarg(args, 'e');
64520253Sjoerg
64620253Sjoerg	if (arg != NULL) {
64720253Sjoerg		if ((result = parse_date(now, arg->val)) == now)
64820267Sjoerg			cmderr(EX_DATAERR, "invalid date/time `%s'\n", arg->val);
64920253Sjoerg	} else if (cnf->expire_days > 0)
65020253Sjoerg		result = now + ((long) cnf->expire_days * 86400L);
65120253Sjoerg	return result;
65220253Sjoerg}
65320253Sjoerg
65420253Sjoerg
65520253Sjoergstatic char    *
65620253Sjoergpw_homepolicy(struct userconf * cnf, struct cargs * args, char const * user)
65720253Sjoerg{
65820253Sjoerg	struct carg    *arg = getarg(args, 'd');
65920253Sjoerg
66020253Sjoerg	if (arg)
66120253Sjoerg		return arg->val;
66220253Sjoerg	else {
66320253Sjoerg		static char     home[128];
66420253Sjoerg
66520253Sjoerg		if (cnf->home == NULL || *cnf->home == '\0')
66620267Sjoerg			cmderr(EX_CONFIG, "no base home directory set\n");
66720253Sjoerg		sprintf(home, "%s/%s", cnf->home, user);
66820253Sjoerg		return home;
66920253Sjoerg	}
67020253Sjoerg}
67120253Sjoerg
67220253Sjoergstatic char    *
67320253Sjoergshell_path(char const * path, char *shells[], char *sh)
67420253Sjoerg{
67520253Sjoerg	if (sh != NULL && (*sh == '/' || *sh == '\0'))
67620253Sjoerg		return sh;	/* specified full path or forced none */
67720253Sjoerg	else {
67820253Sjoerg		char           *p;
67920253Sjoerg		char            paths[_UC_MAXLINE];
68020253Sjoerg
68120253Sjoerg		/*
68220253Sjoerg		 * We need to search paths
68320253Sjoerg		 */
68420253Sjoerg		strncpy(paths, path, sizeof paths);
68520253Sjoerg		paths[sizeof paths - 1] = '\0';
68620253Sjoerg		for (p = strtok(paths, ": \t\r\n"); p != NULL; p = strtok(NULL, ": \t\r\n")) {
68720253Sjoerg			int             i;
68820253Sjoerg			static char     shellpath[256];
68920253Sjoerg
69020253Sjoerg			if (sh != NULL) {
69120253Sjoerg				sprintf(shellpath, "%s/%s", p, sh);
69220253Sjoerg				if (access(shellpath, X_OK) == 0)
69320253Sjoerg					return shellpath;
69420253Sjoerg			} else
69520253Sjoerg				for (i = 0; i < _UC_MAXSHELLS && shells[i] != NULL; i++) {
69620253Sjoerg					sprintf(shellpath, "%s/%s", p, shells[i]);
69720253Sjoerg					if (access(shellpath, X_OK) == 0)
69820253Sjoerg						return shellpath;
69920253Sjoerg				}
70020253Sjoerg		}
70120253Sjoerg		if (sh == NULL)
70220267Sjoerg			cmderr(EX_OSFILE, "can't find shell `%s' in shell paths\n", sh);
70320267Sjoerg		cmderr(EX_CONFIG, "no default shell available or defined\n");
70420253Sjoerg		return NULL;
70520253Sjoerg	}
70620253Sjoerg}
70720253Sjoerg
70820253Sjoerg
70920253Sjoergstatic char    *
71020253Sjoergpw_shellpolicy(struct userconf * cnf, struct cargs * args, char *newshell)
71120253Sjoerg{
71220253Sjoerg	char           *sh = newshell;
71320253Sjoerg	struct carg    *arg = getarg(args, 's');
71420253Sjoerg
71520253Sjoerg	if (newshell == NULL && arg != NULL)
71620253Sjoerg		sh = arg->val;
71720253Sjoerg	return shell_path(cnf->shelldir, cnf->shells, sh ? sh : cnf->shell_default);
71820253Sjoerg}
71920253Sjoerg
72020253Sjoergstatic char const chars[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.";
72120253Sjoerg
72220253Sjoergchar           *
72320253Sjoergpw_pwcrypt(char *password)
72420253Sjoerg{
72520253Sjoerg	int             i;
72620253Sjoerg	char            salt[12];
72720253Sjoerg
72820253Sjoerg	static char     buf[256];
72920253Sjoerg
73020253Sjoerg	/*
73120253Sjoerg	 * Calculate a salt value
73220253Sjoerg	 */
73320253Sjoerg	srandom((unsigned) (time(NULL) | getpid()));
73420253Sjoerg	for (i = 0; i < 8; i++)
73520253Sjoerg		salt[i] = chars[random() % 63];
73620253Sjoerg	salt[i] = '\0';
73720253Sjoerg
73820253Sjoerg	return strcpy(buf, crypt(password, salt));
73920253Sjoerg}
74020253Sjoerg
74120253Sjoerg
74220253Sjoergstatic char    *
74320253Sjoergpw_password(struct userconf * cnf, struct cargs * args, char const * user)
74420253Sjoerg{
74520253Sjoerg	int             i, l;
74620253Sjoerg	char            pwbuf[32];
74720253Sjoerg
74820253Sjoerg	switch (cnf->default_password) {
74920253Sjoerg	case -1:		/* Random password */
75020253Sjoerg		srandom((unsigned) (time(NULL) | getpid()));
75120253Sjoerg		l = (random() % 8 + 8);	/* 8 - 16 chars */
75220253Sjoerg		for (i = 0; i < l; i++)
75320253Sjoerg			pwbuf[i] = chars[random() % sizeof(chars)];
75420253Sjoerg		pwbuf[i] = '\0';
75520253Sjoerg
75620253Sjoerg		/*
75720253Sjoerg		 * We give this information back to the user
75820253Sjoerg		 */
75920267Sjoerg		if (getarg(args, 'h') == NULL && getarg(args, 'N') == NULL) {
76020253Sjoerg			if (isatty(0))
76120253Sjoerg				printf("Password is: ");
76220253Sjoerg			printf("%s\n", pwbuf);
76320253Sjoerg			fflush(stdout);
76420253Sjoerg		}
76520253Sjoerg		break;
76620253Sjoerg
76720253Sjoerg	case -2:		/* No password at all! */
76820253Sjoerg		return "";
76920253Sjoerg
77020253Sjoerg	case 0:		/* No login - default */
77120253Sjoerg	default:
77220253Sjoerg		return "*";
77320253Sjoerg
77420253Sjoerg	case 1:		/* user's name */
77520253Sjoerg		strncpy(pwbuf, user, sizeof pwbuf);
77620253Sjoerg		pwbuf[sizeof pwbuf - 1] = '\0';
77720253Sjoerg		break;
77820253Sjoerg	}
77920253Sjoerg	return pw_pwcrypt(pwbuf);
78020253Sjoerg}
78120253Sjoerg
78220253Sjoerg
78320253Sjoergstatic int
78420253Sjoergprint_user(struct passwd * pwd, int pretty)
78520253Sjoerg{
78620253Sjoerg	if (!pretty) {
78720253Sjoerg		char            buf[_UC_MAXLINE];
78820253Sjoerg
78920253Sjoerg		fmtpwent(buf, pwd);
79020253Sjoerg		fputs(buf, stdout);
79120253Sjoerg	} else {
79220267Sjoerg		int		j;
79320253Sjoerg		char           *p;
79420253Sjoerg		struct group   *grp = getgrgid(pwd->pw_gid);
79520253Sjoerg		char            uname[60] = "User &", office[60] = "[None]",
79620253Sjoerg		                wphone[60] = "[None]", hphone[60] = "[None]";
79720253Sjoerg
79820253Sjoerg		if ((p = strtok(pwd->pw_gecos, ",")) != NULL) {
79920253Sjoerg			strncpy(uname, p, sizeof uname);
80020253Sjoerg			uname[sizeof uname - 1] = '\0';
80120253Sjoerg			if ((p = strtok(NULL, ",")) != NULL) {
80220253Sjoerg				strncpy(office, p, sizeof office);
80320253Sjoerg				office[sizeof office - 1] = '\0';
80420253Sjoerg				if ((p = strtok(NULL, ",")) != NULL) {
80520253Sjoerg					strncpy(wphone, p, sizeof wphone);
80620253Sjoerg					wphone[sizeof wphone - 1] = '\0';
80720253Sjoerg					if ((p = strtok(NULL, "")) != NULL) {
80820253Sjoerg						strncpy(hphone, p, sizeof hphone);
80920253Sjoerg						hphone[sizeof hphone - 1] = '\0';
81020253Sjoerg					}
81120253Sjoerg				}
81220253Sjoerg			}
81320253Sjoerg		}
81420253Sjoerg		/*
81520253Sjoerg		 * Handle '&' in gecos field
81620253Sjoerg		 */
81720253Sjoerg		if ((p = strchr(uname, '&')) != NULL) {
81820253Sjoerg			int             l = strlen(pwd->pw_name);
81920253Sjoerg			int             m = strlen(p);
82020253Sjoerg
82120253Sjoerg			memmove(p + l, p + 1, m);
82220253Sjoerg			memmove(p, pwd->pw_name, l);
82320253Sjoerg			*p = (char) toupper(*p);
82420253Sjoerg		}
82520253Sjoerg		printf("Login Name : %-10s   #%-22ld  Group : %-10s   #%ld\n"
82620253Sjoerg		       " Full Name : %s\n"
82720253Sjoerg		       "      Home : %-32.32s      Class : %s\n"
82820253Sjoerg		       "     Shell : %-32.32s     Office : %s\n"
82920267Sjoerg		       "Work Phone : %-32.32s Home Phone : %s\n",
83020267Sjoerg
83120253Sjoerg		       pwd->pw_name, (long) pwd->pw_uid,
83220253Sjoerg		       grp ? grp->gr_name : "(invalid)", (long) pwd->pw_gid,
83320253Sjoerg		       uname, pwd->pw_dir, pwd->pw_class,
83420253Sjoerg		       pwd->pw_shell, office, wphone, hphone);
83520267Sjoerg	        setgrent();
83620267Sjoerg		j = 0;
83720267Sjoerg		while ((grp=getgrent()) != NULL)
83820267Sjoerg		{
83920267Sjoerg			int     i = 0;
84020267Sjoerg			while (i < _UC_MAXGROUPS && grp->gr_mem[i] != NULL)
84120267Sjoerg			{
84220267Sjoerg				if (strcmp(grp->gr_mem[i], pwd->pw_name)==0)
84320267Sjoerg				{
84420267Sjoerg					printf(j++ == 0 ? "    Groups : %s" : ",%s", grp->gr_name);
84520267Sjoerg					break;
84620267Sjoerg				}
84720267Sjoerg				++i;
84820267Sjoerg			}
84920267Sjoerg		}
85020267Sjoerg		endgrent();
85120267Sjoerg		printf("%s\n", j ? "\n" : "");
85220253Sjoerg	}
85320267Sjoerg	return EXIT_SUCCESS;
85420253Sjoerg}
85520253Sjoerg
85620253Sjoergstatic char    *
85720253Sjoergpw_checkname(char *name, int gecos)
85820253Sjoerg{
85920253Sjoerg	int             l = 0;
86020253Sjoerg	char const     *notch = gecos ? ":" : " :+-&#%$^()!@~*?<>=|\\/\"";
86120253Sjoerg
86220253Sjoerg	while (name[l]) {
86320253Sjoerg		if (strchr(notch, name[l]) != NULL || name[l] < ' ')
86420267Sjoerg			cmderr(EX_DATAERR, "invalid character `%c' in field\n", name[l]);
86520253Sjoerg		++l;
86620253Sjoerg	}
86720253Sjoerg	if (!gecos && l > 8)
86820267Sjoerg		cmderr(EX_DATAERR, "name too long `%s'\n", name);
86920253Sjoerg	return name;
87020253Sjoerg}
87120253Sjoerg
87220253Sjoerg
87320253Sjoergstatic void
87420253Sjoergrmat(uid_t uid)
87520253Sjoerg{
87620253Sjoerg	DIR            *d = opendir("/var/at/jobs");
87720253Sjoerg
87820253Sjoerg	if (d != NULL) {
87920253Sjoerg		struct dirent  *e;
88020253Sjoerg
88120253Sjoerg		while ((e = readdir(d)) != NULL) {
88220253Sjoerg			struct stat     st;
88320253Sjoerg
88420253Sjoerg			if (strncmp(e->d_name, ".lock", 5) != 0 &&
88520253Sjoerg			    stat(e->d_name, &st) == 0 &&
88620253Sjoerg			    !S_ISDIR(st.st_mode) &&
88720253Sjoerg			    st.st_uid == uid) {
88820253Sjoerg				char            tmp[MAXPATHLEN];
88920253Sjoerg
89020253Sjoerg				sprintf(tmp, "/usr/bin/atrm %s", e->d_name);
89120253Sjoerg				system(tmp);
89220253Sjoerg			}
89320253Sjoerg		}
89420253Sjoerg		closedir(d);
89520253Sjoerg	}
89620253Sjoerg}
897