pw_user.c revision 252688
1227569Sphilip/*-
2283514Sarybchik * Copyright (C) 1996
3283514Sarybchik *	David L. Nugent.  All rights reserved.
4227569Sphilip *
5227569Sphilip * Redistribution and use in source and binary forms, with or without
6283514Sarybchik * modification, are permitted provided that the following conditions
7227569Sphilip * are met:
8283514Sarybchik * 1. Redistributions of source code must retain the above copyright
9283514Sarybchik *    notice, this list of conditions and the following disclaimer.
10283514Sarybchik * 2. Redistributions in binary form must reproduce the above copyright
11283514Sarybchik *    notice, this list of conditions and the following disclaimer in the
12283514Sarybchik *    documentation and/or other materials provided with the distribution.
13283514Sarybchik *
14283514Sarybchik * THIS SOFTWARE IS PROVIDED BY DAVID L. NUGENT AND CONTRIBUTORS ``AS IS'' AND
15283514Sarybchik * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16283514Sarybchik * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17283514Sarybchik * ARE DISCLAIMED.  IN NO EVENT SHALL DAVID L. NUGENT OR CONTRIBUTORS BE LIABLE
18283514Sarybchik * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19283514Sarybchik * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20283514Sarybchik * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21283514Sarybchik * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22283514Sarybchik * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23283514Sarybchik * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24283514Sarybchik * SUCH DAMAGE.
25283514Sarybchik *
26283514Sarybchik */
27283514Sarybchik
28283514Sarybchik#ifndef lint
29227569Sphilipstatic const char rcsid[] =
30227569Sphilip  "$FreeBSD: head/usr.sbin/pw/pw_user.c 252688 2013-07-04 07:59:11Z des $";
31228078Sphilip#endif /* not lint */
32228078Sphilip
33228078Sphilip#include <ctype.h>
34227569Sphilip#include <err.h>
35227569Sphilip#include <fcntl.h>
36293814Sarybchik#include <sys/param.h>
37283514Sarybchik#include <dirent.h>
38293814Sarybchik#include <paths.h>
39227569Sphilip#include <termios.h>
40227569Sphilip#include <sys/types.h>
41227569Sphilip#include <sys/time.h>
42227569Sphilip#include <sys/resource.h>
43227569Sphilip#include <unistd.h>
44227569Sphilip#include <login_cap.h>
45227569Sphilip#include <pwd.h>
46227569Sphilip#include <grp.h>
47227569Sphilip#include <libutil.h>
48227569Sphilip#include "pw.h"
49227569Sphilip#include "bitmap.h"
50283514Sarybchik
51283514Sarybchik#define LOGNAMESIZE (MAXLOGNAME-1)
52283514Sarybchik
53283514Sarybchikstatic		char locked_str[] = "*LOCKED*";
54283514Sarybchik
55283514Sarybchikstatic int      print_user(struct passwd * pwd, int pretty, int v7);
56283514Sarybchikstatic uid_t    pw_uidpolicy(struct userconf * cnf, struct cargs * args);
57283514Sarybchikstatic uid_t    pw_gidpolicy(struct userconf * cnf, struct cargs * args, char *nam, gid_t prefer);
58291436Sarybchikstatic time_t   pw_pwdpolicy(struct userconf * cnf, struct cargs * args);
59283514Sarybchikstatic time_t   pw_exppolicy(struct userconf * cnf, struct cargs * args);
60283514Sarybchikstatic char    *pw_homepolicy(struct userconf * cnf, struct cargs * args, char const * user);
61283514Sarybchikstatic char    *pw_shellpolicy(struct userconf * cnf, struct cargs * args, char *newshell);
62283514Sarybchikstatic char    *pw_password(struct userconf * cnf, struct cargs * args, char const * user);
63283514Sarybchikstatic char    *shell_path(char const * path, char *shells[], char *sh);
64283514Sarybchikstatic void     rmat(uid_t uid);
65283514Sarybchikstatic void     rmopie(char const * name);
66291436Sarybchik
67283514Sarybchik/*-
68283514Sarybchik * -C config      configuration file
69283514Sarybchik * -q             quiet operation
70283514Sarybchik * -n name        login name
71283514Sarybchik * -u uid         user id
72283514Sarybchik * -c comment     user name/comment
73283514Sarybchik * -d directory   home directory
74283514Sarybchik * -e date        account expiry date
75283514Sarybchik * -p date        password expiry date
76283514Sarybchik * -g grp         primary group
77283514Sarybchik * -G grp1,grp2   additional groups
78283514Sarybchik * -m [ -k dir ]  create and set up home
79291436Sarybchik * -s shell       name of login shell
80283514Sarybchik * -o             duplicate uid ok
81283514Sarybchik * -L class       user class
82283514Sarybchik * -l name        new login name
83283514Sarybchik * -h fd          password filehandle
84283514Sarybchik * -H fd          encrypted password filehandle
85283514Sarybchik * -F             force print or add
86283514Sarybchik *   Setting defaults:
87283514Sarybchik * -D             set user defaults
88283514Sarybchik * -b dir         default home root dir
89283514Sarybchik * -e period      default expiry period
90283514Sarybchik * -p period      default password change period
91283514Sarybchik * -g group       default group
92283514Sarybchik * -G             grp1,grp2.. default additional groups
93283514Sarybchik * -L class       default login class
94283514Sarybchik * -k dir         default home skeleton
95283514Sarybchik * -s shell       default shell
96291436Sarybchik * -w method      default password method
97283514Sarybchik */
98283514Sarybchik
99283514Sarybchikint
100283514Sarybchikpw_user(struct userconf * cnf, int mode, struct cargs * args)
101283514Sarybchik{
102283514Sarybchik	int	        rc, edited = 0;
103283514Sarybchik	char           *p = NULL;
104283514Sarybchik	char					 *passtmp;
105283514Sarybchik	struct carg    *a_name;
106283514Sarybchik	struct carg    *a_uid;
107283514Sarybchik	struct carg    *arg;
108283514Sarybchik	struct passwd  *pwd = NULL;
109283514Sarybchik	struct group   *grp;
110283514Sarybchik	struct stat     st;
111283514Sarybchik	char            line[_PASSWORD_LEN+1];
112283514Sarybchik	FILE	       *fp;
113283514Sarybchik	char *dmode_c;
114283514Sarybchik	void *set = NULL;
115283514Sarybchik
116283514Sarybchik	static struct passwd fakeuser =
117283514Sarybchik	{
118283514Sarybchik		NULL,
119283514Sarybchik		"*",
120283514Sarybchik		-1,
121283514Sarybchik		-1,
122283514Sarybchik		0,
123283514Sarybchik		"",
124283514Sarybchik		"User &",
125283514Sarybchik		"/nonexistent",
126283514Sarybchik		"/bin/sh",
127283514Sarybchik		0
128283514Sarybchik#if defined(__FreeBSD__)
129283514Sarybchik		,0
130283514Sarybchik#endif
131283514Sarybchik	};
132283514Sarybchik
133283514Sarybchik
134283514Sarybchik	/*
135283514Sarybchik	 * With M_NEXT, we only need to return the
136283514Sarybchik	 * next uid to stdout
137283514Sarybchik	 */
138283514Sarybchik	if (mode == M_NEXT)
139283514Sarybchik	{
140283514Sarybchik		uid_t next = pw_uidpolicy(cnf, args);
141293752Sarybchik		if (getarg(args, 'q'))
142293752Sarybchik			return next;
143293752Sarybchik		printf("%ld:", (long)next);
144293752Sarybchik		pw_group(cnf, mode, args);
145293752Sarybchik		return EXIT_SUCCESS;
146293752Sarybchik	}
147293752Sarybchik
148293752Sarybchik	/*
149293752Sarybchik	 * We can do all of the common legwork here
150283514Sarybchik	 */
151293752Sarybchik
152283514Sarybchik	if ((arg = getarg(args, 'b')) != NULL) {
153283514Sarybchik		cnf->home = arg->val;
154293752Sarybchik	}
155283514Sarybchik
156283514Sarybchik	if ((arg = getarg(args, 'M')) != NULL) {
157291436Sarybchik		dmode_c = arg->val;
158227569Sphilip		if ((set = setmode(dmode_c)) == NULL)
159227569Sphilip			errx(EX_DATAERR, "invalid directory creation mode '%s'",
160227569Sphilip			    dmode_c);
161283514Sarybchik		cnf->homemode = getmode(set, _DEF_DIRMODE);
162291436Sarybchik		free(set);
163227569Sphilip	}
164227569Sphilip
165227569Sphilip	/*
166227569Sphilip	 * If we'll need to use it or we're updating it,
167227569Sphilip	 * then create the base home directory if necessary
168227569Sphilip	 */
169227569Sphilip	if (arg != NULL || getarg(args, 'm') != NULL) {
170227569Sphilip		int	l = strlen(cnf->home);
171227569Sphilip
172283514Sarybchik		if (l > 1 && cnf->home[l-1] == '/')	/* Shave off any trailing path delimiter */
173283514Sarybchik			cnf->home[--l] = '\0';
174283514Sarybchik
175283514Sarybchik		if (l < 2 || *cnf->home != '/')		/* Check for absolute path name */
176283514Sarybchik			errx(EX_DATAERR, "invalid base directory for home '%s'", cnf->home);
177283514Sarybchik
178283514Sarybchik		if (stat(cnf->home, &st) == -1) {
179283514Sarybchik			char	dbuf[MAXPATHLEN];
180283514Sarybchik
181283514Sarybchik			/*
182283514Sarybchik			 * This is a kludge especially for Joerg :)
183283514Sarybchik			 * If the home directory would be created in the root partition, then
184283514Sarybchik			 * we really create it under /usr which is likely to have more space.
185283514Sarybchik			 * But we create a symlink from cnf->home -> "/usr" -> cnf->home
186283514Sarybchik			 */
187293752Sarybchik			if (strchr(cnf->home+1, '/') == NULL) {
188283514Sarybchik				strcpy(dbuf, "/usr");
189283514Sarybchik				strncat(dbuf, cnf->home, MAXPATHLEN-5);
190283514Sarybchik				if (mkdir(dbuf, _DEF_DIRMODE) != -1 || errno == EEXIST) {
191293752Sarybchik					chown(dbuf, 0, 0);
192293752Sarybchik					/*
193293752Sarybchik					 * Skip first "/" and create symlink:
194293752Sarybchik					 * /home -> usr/home
195293752Sarybchik					 */
196293752Sarybchik					symlink(dbuf+1, cnf->home);
197283514Sarybchik				}
198283514Sarybchik				/* If this falls, fall back to old method */
199283514Sarybchik			}
200283514Sarybchik			strlcpy(dbuf, cnf->home, sizeof(dbuf));
201283514Sarybchik			p = dbuf;
202283514Sarybchik			if (stat(dbuf, &st) == -1) {
203227569Sphilip				while ((p = strchr(p + 1, '/')) != NULL) {
204227569Sphilip					*p = '\0';
205283514Sarybchik					if (stat(dbuf, &st) == -1) {
206283514Sarybchik						if (mkdir(dbuf, _DEF_DIRMODE) == -1)
207283514Sarybchik							goto direrr;
208283514Sarybchik						chown(dbuf, 0, 0);
209283514Sarybchik					} else if (!S_ISDIR(st.st_mode))
210283514Sarybchik						errx(EX_OSFILE, "'%s' (root home parent) is not a directory", dbuf);
211283514Sarybchik					*p = '/';
212283514Sarybchik				}
213283514Sarybchik			}
214283514Sarybchik			if (stat(dbuf, &st) == -1) {
215283514Sarybchik				if (mkdir(dbuf, _DEF_DIRMODE) == -1) {
216291436Sarybchik				direrr:	err(EX_OSFILE, "mkdir '%s'", dbuf);
217283514Sarybchik				}
218283514Sarybchik				chown(dbuf, 0, 0);
219283514Sarybchik			}
220283514Sarybchik		} else if (!S_ISDIR(st.st_mode))
221283514Sarybchik			errx(EX_OSFILE, "root home `%s' is not a directory", cnf->home);
222283514Sarybchik	}
223283514Sarybchik
224283514Sarybchik	if ((arg = getarg(args, 'e')) != NULL)
225283514Sarybchik		cnf->expire_days = atoi(arg->val);
226283514Sarybchik
227283514Sarybchik	if ((arg = getarg(args, 'y')) != NULL)
228283514Sarybchik		cnf->nispasswd = arg->val;
229283514Sarybchik
230283514Sarybchik	if ((arg = getarg(args, 'p')) != NULL && arg->val)
231283514Sarybchik		cnf->password_days = atoi(arg->val);
232283514Sarybchik
233283514Sarybchik	if ((arg = getarg(args, 'g')) != NULL) {
234283514Sarybchik		if (!*(p = arg->val))	/* Handle empty group list specially */
235283514Sarybchik			cnf->default_group = "";
236283514Sarybchik		else {
237283514Sarybchik			if ((grp = GETGRNAM(p)) == NULL) {
238283514Sarybchik				if (!isdigit((unsigned char)*p) || (grp = GETGRGID((gid_t) atoi(p))) == NULL)
239283514Sarybchik					errx(EX_NOUSER, "group `%s' does not exist", p);
240283514Sarybchik			}
241283514Sarybchik			cnf->default_group = newstr(grp->gr_name);
242283514Sarybchik		}
243291436Sarybchik	}
244283514Sarybchik	if ((arg = getarg(args, 'L')) != NULL)
245283514Sarybchik		cnf->default_class = pw_checkname((u_char *)arg->val, 0);
246283514Sarybchik
247283514Sarybchik	if ((arg = getarg(args, 'G')) != NULL && arg->val) {
248283514Sarybchik		int i = 0;
249283514Sarybchik
250283514Sarybchik		for (p = strtok(arg->val, ", \t"); p != NULL; p = strtok(NULL, ", \t")) {
251283514Sarybchik			if ((grp = GETGRNAM(p)) == NULL) {
252283514Sarybchik				if (!isdigit((unsigned char)*p) || (grp = GETGRGID((gid_t) atoi(p))) == NULL)
253283514Sarybchik					errx(EX_NOUSER, "group `%s' does not exist", p);
254283514Sarybchik			}
255291436Sarybchik			if (extendarray(&cnf->groups, &cnf->numgroups, i + 2) != -1)
256283514Sarybchik				cnf->groups[i++] = newstr(grp->gr_name);
257283514Sarybchik		}
258283514Sarybchik		while (i < cnf->numgroups)
259283514Sarybchik			cnf->groups[i++] = NULL;
260283514Sarybchik	}
261283514Sarybchik
262283514Sarybchik	if ((arg = getarg(args, 'k')) != NULL) {
263283514Sarybchik		if (stat(cnf->dotdir = arg->val, &st) == -1 || !S_ISDIR(st.st_mode))
264283514Sarybchik			errx(EX_OSFILE, "skeleton `%s' is not a directory or does not exist", cnf->dotdir);
265283514Sarybchik	}
266283514Sarybchik
267283514Sarybchik	if ((arg = getarg(args, 's')) != NULL)
268283514Sarybchik		cnf->shell_default = arg->val;
269283514Sarybchik
270283514Sarybchik	if ((arg = getarg(args, 'w')) != NULL)
271283514Sarybchik		cnf->default_password = boolean_val(arg->val, cnf->default_password);
272283514Sarybchik	if (mode == M_ADD && getarg(args, 'D')) {
273283514Sarybchik		if (getarg(args, 'n') != NULL)
274283514Sarybchik			errx(EX_DATAERR, "can't combine `-D' with `-n name'");
275283514Sarybchik		if ((arg = getarg(args, 'u')) != NULL && (p = strtok(arg->val, ", \t")) != NULL) {
276283514Sarybchik			if ((cnf->min_uid = (uid_t) atoi(p)) == 0)
277283514Sarybchik				cnf->min_uid = 1000;
278283514Sarybchik			if ((p = strtok(NULL, " ,\t")) == NULL || (cnf->max_uid = (uid_t) atoi(p)) < cnf->min_uid)
279283514Sarybchik				cnf->max_uid = 32000;
280283514Sarybchik		}
281283514Sarybchik		if ((arg = getarg(args, 'i')) != NULL && (p = strtok(arg->val, ", \t")) != NULL) {
282283514Sarybchik			if ((cnf->min_gid = (gid_t) atoi(p)) == 0)
283283514Sarybchik				cnf->min_gid = 1000;
284283514Sarybchik			if ((p = strtok(NULL, " ,\t")) == NULL || (cnf->max_gid = (gid_t) atoi(p)) < cnf->min_gid)
285283514Sarybchik				cnf->max_gid = 32000;
286283514Sarybchik		}
287291436Sarybchik
288283514Sarybchik		arg = getarg(args, 'C');
289283514Sarybchik		if (write_userconfig(arg ? arg->val : NULL))
290283514Sarybchik			return EXIT_SUCCESS;
291283514Sarybchik		warn("config update");
292283514Sarybchik		return EX_IOERR;
293283514Sarybchik	}
294283514Sarybchik
295283514Sarybchik	if (mode == M_PRINT && getarg(args, 'a')) {
296283514Sarybchik		int             pretty = getarg(args, 'P') != NULL;
297283514Sarybchik		int		v7 = getarg(args, '7') != NULL;
298283514Sarybchik		SETPWENT();
299283514Sarybchik		while ((pwd = GETPWENT()) != NULL)
300283514Sarybchik			print_user(pwd, pretty, v7);
301283514Sarybchik		ENDPWENT();
302283514Sarybchik		return EXIT_SUCCESS;
303283514Sarybchik	}
304283514Sarybchik
305283514Sarybchik	if ((a_name = getarg(args, 'n')) != NULL)
306283514Sarybchik		pwd = GETPWNAM(pw_checkname((u_char *)a_name->val, 0));
307283514Sarybchik	a_uid = getarg(args, 'u');
308283514Sarybchik
309291436Sarybchik	if (a_uid == NULL) {
310283514Sarybchik		if (a_name == NULL)
311283514Sarybchik			errx(EX_DATAERR, "user name or id required");
312283514Sarybchik
313283514Sarybchik		/*
314283514Sarybchik		 * Determine whether 'n' switch is name or uid - we don't
315283514Sarybchik		 * really don't really care which we have, but we need to
316291436Sarybchik		 * know.
317283514Sarybchik		 */
318283514Sarybchik		if (mode != M_ADD && pwd == NULL
319283514Sarybchik		    && strspn(a_name->val, "0123456789") == strlen(a_name->val)
320283514Sarybchik		    && *a_name->val) {
321283514Sarybchik			(a_uid = a_name)->ch = 'u';
322283514Sarybchik			a_name = NULL;
323283514Sarybchik		}
324283514Sarybchik	}
325283514Sarybchik
326283514Sarybchik	/*
327283514Sarybchik	 * Update, delete & print require that the user exists
328283514Sarybchik	 */
329283514Sarybchik	if (mode == M_UPDATE || mode == M_DELETE ||
330283514Sarybchik	    mode == M_PRINT  || mode == M_LOCK   || mode == M_UNLOCK) {
331283514Sarybchik
332283514Sarybchik		if (a_name == NULL && pwd == NULL)	/* Try harder */
333291436Sarybchik			pwd = GETPWUID(atoi(a_uid->val));
334283514Sarybchik
335283514Sarybchik		if (pwd == NULL) {
336283514Sarybchik			if (mode == M_PRINT && getarg(args, 'F')) {
337283514Sarybchik				fakeuser.pw_name = a_name ? a_name->val : "nouser";
338283514Sarybchik				fakeuser.pw_uid = a_uid ? (uid_t) atol(a_uid->val) : -1;
339283514Sarybchik				return print_user(&fakeuser,
340283514Sarybchik						  getarg(args, 'P') != NULL,
341283514Sarybchik						  getarg(args, '7') != NULL);
342283514Sarybchik			}
343283514Sarybchik			if (a_name == NULL)
344283514Sarybchik				errx(EX_NOUSER, "no such uid `%s'", a_uid->val);
345283514Sarybchik			errx(EX_NOUSER, "no such user `%s'", a_name->val);
346283514Sarybchik		}
347283514Sarybchik
348283514Sarybchik		if (a_name == NULL)	/* May be needed later */
349283514Sarybchik			a_name = addarg(args, 'n', newstr(pwd->pw_name));
350283514Sarybchik
351283514Sarybchik		/*
352283514Sarybchik		 * The M_LOCK and M_UNLOCK functions simply add or remove
353283514Sarybchik		 * a "*LOCKED*" prefix from in front of the password to
354283514Sarybchik		 * prevent it decoding correctly, and therefore prevents
355283514Sarybchik		 * access. Of course, this only prevents access via
356283514Sarybchik		 * password authentication (not ssh, kerberos or any
357283514Sarybchik		 * other method that does not use the UNIX password) but
358283514Sarybchik		 * that is a known limitation.
359283514Sarybchik		 */
360283514Sarybchik
361283514Sarybchik		if (mode == M_LOCK) {
362283514Sarybchik			if (strncmp(pwd->pw_passwd, locked_str, sizeof(locked_str)-1) == 0)
363283514Sarybchik				errx(EX_DATAERR, "user '%s' is already locked", pwd->pw_name);
364283514Sarybchik			passtmp = malloc(strlen(pwd->pw_passwd) + sizeof(locked_str));
365283514Sarybchik			if (passtmp == NULL)	/* disaster */
366283514Sarybchik				errx(EX_UNAVAILABLE, "out of memory");
367283514Sarybchik			strcpy(passtmp, locked_str);
368283514Sarybchik			strcat(passtmp, pwd->pw_passwd);
369283514Sarybchik			pwd->pw_passwd = passtmp;
370283514Sarybchik			edited = 1;
371283514Sarybchik		} else if (mode == M_UNLOCK) {
372283514Sarybchik			if (strncmp(pwd->pw_passwd, locked_str, sizeof(locked_str)-1) != 0)
373283514Sarybchik				errx(EX_DATAERR, "user '%s' is not locked", pwd->pw_name);
374283514Sarybchik			pwd->pw_passwd += sizeof(locked_str)-1;
375283514Sarybchik			edited = 1;
376283514Sarybchik		} else if (mode == M_DELETE) {
377283514Sarybchik			/*
378283514Sarybchik			 * Handle deletions now
379283514Sarybchik			 */
380227569Sphilip			char            file[MAXPATHLEN];
381283514Sarybchik			char            home[MAXPATHLEN];
382283514Sarybchik			uid_t           uid = pwd->pw_uid;
383283514Sarybchik
384283514Sarybchik			if (strcmp(pwd->pw_name, "root") == 0)
385283514Sarybchik				errx(EX_DATAERR, "cannot remove user 'root'");
386283514Sarybchik
387283514Sarybchik			if (!PWALTDIR()) {
388283514Sarybchik				/*
389283514Sarybchik				 * Remove opie record from /etc/opiekeys
390283514Sarybchik		        	 */
391283514Sarybchik
392283514Sarybchik				rmopie(pwd->pw_name);
393283514Sarybchik
394283514Sarybchik				/*
395283514Sarybchik				 * Remove crontabs
396283514Sarybchik				 */
397283514Sarybchik				snprintf(file, sizeof(file), "/var/cron/tabs/%s", pwd->pw_name);
398283514Sarybchik				if (access(file, F_OK) == 0) {
399283514Sarybchik					sprintf(file, "crontab -u %s -r", pwd->pw_name);
400283514Sarybchik					system(file);
401283514Sarybchik				}
402283514Sarybchik			}
403283514Sarybchik			/*
404283514Sarybchik			 * Save these for later, since contents of pwd may be
405283514Sarybchik			 * invalidated by deletion
406283514Sarybchik			 */
407283514Sarybchik			sprintf(file, "%s/%s", _PATH_MAILDIR, pwd->pw_name);
408283514Sarybchik			strlcpy(home, pwd->pw_dir, sizeof(home));
409283514Sarybchik
410283514Sarybchik			rc = delpwent(pwd);
411283514Sarybchik			if (rc == -1)
412283514Sarybchik				err(EX_IOERR, "user '%s' does not exist", pwd->pw_name);
413283514Sarybchik			else if (rc != 0) {
414283514Sarybchik				warn("passwd update");
415283514Sarybchik				return EX_IOERR;
416283514Sarybchik			}
417291436Sarybchik
418283514Sarybchik			if (cnf->nispasswd && *cnf->nispasswd=='/') {
419283514Sarybchik				rc = delnispwent(cnf->nispasswd, a_name->val);
420283514Sarybchik				if (rc == -1)
421283514Sarybchik					warnx("WARNING: user '%s' does not exist in NIS passwd", pwd->pw_name);
422283514Sarybchik				else if (rc != 0)
423283514Sarybchik					warn("WARNING: NIS passwd update");
424291436Sarybchik				/* non-fatal */
425283514Sarybchik			}
426283514Sarybchik
427283514Sarybchik			grp = GETGRNAM(a_name->val);
428283514Sarybchik			if (grp != NULL && *grp->gr_mem == NULL)
429283514Sarybchik				delgrent(GETGRNAM(a_name->val));
430283514Sarybchik			SETGRENT();
431283514Sarybchik			while ((grp = GETGRENT()) != NULL) {
432283514Sarybchik				int i;
433283514Sarybchik				char group[MAXLOGNAME];
434291436Sarybchik				for (i = 0; grp->gr_mem[i] != NULL; i++) {
435283514Sarybchik					if (!strcmp(grp->gr_mem[i], a_name->val)) {
436283514Sarybchik						while (grp->gr_mem[i] != NULL) {
437283514Sarybchik							grp->gr_mem[i] = grp->gr_mem[i+1];
438283514Sarybchik						}
439283514Sarybchik						strlcpy(group, grp->gr_name, MAXLOGNAME);
440283514Sarybchik						chggrent(group, grp);
441283514Sarybchik					}
442283514Sarybchik				}
443283514Sarybchik			}
444283514Sarybchik			ENDGRENT();
445283514Sarybchik
446283514Sarybchik			pw_log(cnf, mode, W_USER, "%s(%ld) account removed", a_name->val, (long) uid);
447283514Sarybchik
448283514Sarybchik			if (!PWALTDIR()) {
449283514Sarybchik				/*
450283514Sarybchik				 * Remove mail file
451283514Sarybchik				 */
452283514Sarybchik				remove(file);
453283514Sarybchik
454283514Sarybchik				/*
455283514Sarybchik				 * Remove at jobs
456291436Sarybchik				 */
457283514Sarybchik				if (getpwuid(uid) == NULL)
458283514Sarybchik					rmat(uid);
459283514Sarybchik
460283514Sarybchik				/*
461283514Sarybchik				 * Remove home directory and contents
462283514Sarybchik				 */
463227569Sphilip				if (getarg(args, 'r') != NULL && *home == '/' && getpwuid(uid) == NULL) {
464227569Sphilip					if (stat(home, &st) != -1) {
465227569Sphilip						rm_r(home, uid);
466227569Sphilip						pw_log(cnf, mode, W_USER, "%s(%ld) home '%s' %sremoved",
467227569Sphilip						       a_name->val, (long) uid, home,
468227569Sphilip						       stat(home, &st) == -1 ? "" : "not completely ");
469227569Sphilip					}
470227569Sphilip				}
471227569Sphilip			}
472227569Sphilip			return EXIT_SUCCESS;
473227569Sphilip		} else if (mode == M_PRINT)
474227569Sphilip			return print_user(pwd,
475283514Sarybchik					  getarg(args, 'P') != NULL,
476227569Sphilip					  getarg(args, '7') != NULL);
477227569Sphilip
478227569Sphilip		/*
479227569Sphilip		 * The rest is edit code
480227569Sphilip		 */
481227569Sphilip		if ((arg = getarg(args, 'l')) != NULL) {
482227569Sphilip			if (strcmp(pwd->pw_name, "root") == 0)
483227569Sphilip				errx(EX_DATAERR, "can't rename `root' account");
484227569Sphilip			pwd->pw_name = pw_checkname((u_char *)arg->val, 0);
485227569Sphilip			edited = 1;
486227569Sphilip		}
487278839Sarybchik
488278839Sarybchik		if ((arg = getarg(args, 'u')) != NULL && isdigit((unsigned char)*arg->val)) {
489227569Sphilip			pwd->pw_uid = (uid_t) atol(arg->val);
490227569Sphilip			edited = 1;
491227569Sphilip			if (pwd->pw_uid != 0 && strcmp(pwd->pw_name, "root") == 0)
492227569Sphilip				errx(EX_DATAERR, "can't change uid of `root' account");
493227569Sphilip			if (pwd->pw_uid == 0 && strcmp(pwd->pw_name, "root") != 0)
494227569Sphilip				warnx("WARNING: account `%s' will have a uid of 0 (superuser access!)", pwd->pw_name);
495227569Sphilip		}
496227569Sphilip
497227569Sphilip		if ((arg = getarg(args, 'g')) != NULL && pwd->pw_uid != 0) {	/* Already checked this */
498227569Sphilip			gid_t newgid = (gid_t) GETGRNAM(cnf->default_group)->gr_gid;
499227569Sphilip			if (newgid != pwd->pw_gid) {
500293806Sarybchik				edited = 1;
501278839Sarybchik				pwd->pw_gid = newgid;
502278839Sarybchik			}
503227569Sphilip		}
504227569Sphilip
505227569Sphilip		if ((arg = getarg(args, 'p')) != NULL) {
506227569Sphilip			if (*arg->val == '\0' || strcmp(arg->val, "0") == 0) {
507227569Sphilip				if (pwd->pw_change != 0) {
508227569Sphilip					pwd->pw_change = 0;
509227569Sphilip					edited = 1;
510227569Sphilip				}
511227569Sphilip			}
512227569Sphilip			else {
513227569Sphilip				time_t          now = time(NULL);
514227569Sphilip				time_t          expire = parse_date(now, arg->val);
515293806Sarybchik
516227569Sphilip				if (pwd->pw_change != expire) {
517227569Sphilip					pwd->pw_change = expire;
518227569Sphilip					edited = 1;
519227569Sphilip				}
520227569Sphilip			}
521227569Sphilip		}
522227569Sphilip
523227569Sphilip		if ((arg = getarg(args, 'e')) != NULL) {
524227569Sphilip			if (*arg->val == '\0' || strcmp(arg->val, "0") == 0) {
525227569Sphilip				if (pwd->pw_expire != 0) {
526227569Sphilip					pwd->pw_expire = 0;
527227569Sphilip					edited = 1;
528227569Sphilip				}
529227569Sphilip			}
530227569Sphilip			else {
531227569Sphilip				time_t          now = time(NULL);
532227569Sphilip				time_t          expire = parse_date(now, arg->val);
533227569Sphilip
534227569Sphilip				if (pwd->pw_expire != expire) {
535227569Sphilip					pwd->pw_expire = expire;
536227569Sphilip					edited = 1;
537227569Sphilip				}
538227569Sphilip			}
539227569Sphilip		}
540227569Sphilip
541227569Sphilip		if ((arg = getarg(args, 's')) != NULL) {
542227569Sphilip			char *shell = shell_path(cnf->shelldir, cnf->shells, arg->val);
543227569Sphilip			if (shell == NULL)
544227569Sphilip				shell = "";
545227569Sphilip			if (strcmp(shell, pwd->pw_shell) != 0) {
546227569Sphilip				pwd->pw_shell = shell;
547227569Sphilip				edited = 1;
548227569Sphilip			}
549227569Sphilip		}
550227569Sphilip
551227569Sphilip		if (getarg(args, 'L')) {
552227569Sphilip			if (cnf->default_class == NULL)
553227569Sphilip				cnf->default_class = "";
554227569Sphilip			if (strcmp(pwd->pw_class, cnf->default_class) != 0) {
555227569Sphilip				pwd->pw_class = cnf->default_class;
556227569Sphilip				edited = 1;
557227569Sphilip			}
558227569Sphilip		}
559227569Sphilip
560227569Sphilip		if ((arg  = getarg(args, 'd')) != NULL) {
561227569Sphilip			if (strcmp(pwd->pw_dir, arg->val))
562227569Sphilip				edited = 1;
563227569Sphilip			if (stat(pwd->pw_dir = arg->val, &st) == -1) {
564227569Sphilip				if (getarg(args, 'm') == NULL && strcmp(pwd->pw_dir, "/nonexistent") != 0)
565283514Sarybchik				  warnx("WARNING: home `%s' does not exist", pwd->pw_dir);
566227569Sphilip			} else if (!S_ISDIR(st.st_mode))
567227569Sphilip				warnx("WARNING: home `%s' is not a directory", pwd->pw_dir);
568227569Sphilip		}
569227569Sphilip
570227569Sphilip		if ((arg = getarg(args, 'w')) != NULL &&
571227569Sphilip		    getarg(args, 'h') == NULL && getarg(args, 'H') == NULL) {
572227569Sphilip			login_cap_t *lc;
573227569Sphilip
574227569Sphilip			lc = login_getpwclass(pwd);
575227569Sphilip			if (lc == NULL ||
576293806Sarybchik			    login_setcryptfmt(lc, "sha512", NULL) == NULL)
577227569Sphilip				warn("setting crypt(3) format");
578227569Sphilip			login_close(lc);
579293806Sarybchik			pwd->pw_passwd = pw_password(cnf, args, pwd->pw_name);
580227569Sphilip			edited = 1;
581227569Sphilip		}
582227569Sphilip
583227569Sphilip	} else {
584227569Sphilip		login_cap_t *lc;
585227569Sphilip
586227569Sphilip		/*
587227569Sphilip		 * Add code
588227569Sphilip		 */
589227569Sphilip
590227569Sphilip		if (a_name == NULL)	/* Required */
591227569Sphilip			errx(EX_DATAERR, "login name required");
592227569Sphilip		else if ((pwd = GETPWNAM(a_name->val)) != NULL)	/* Exists */
593227569Sphilip			errx(EX_DATAERR, "login name `%s' already exists", a_name->val);
594293806Sarybchik
595227569Sphilip		/*
596227569Sphilip		 * Now, set up defaults for a new user
597293806Sarybchik		 */
598227569Sphilip		pwd = &fakeuser;
599227569Sphilip		pwd->pw_name = a_name->val;
600227569Sphilip		pwd->pw_class = cnf->default_class ? cnf->default_class : "";
601227569Sphilip		pwd->pw_uid = pw_uidpolicy(cnf, args);
602227569Sphilip		pwd->pw_gid = pw_gidpolicy(cnf, args, pwd->pw_name, (gid_t) pwd->pw_uid);
603227569Sphilip		pwd->pw_change = pw_pwdpolicy(cnf, args);
604227569Sphilip		pwd->pw_expire = pw_exppolicy(cnf, args);
605227569Sphilip		pwd->pw_dir = pw_homepolicy(cnf, args, pwd->pw_name);
606227569Sphilip		pwd->pw_shell = pw_shellpolicy(cnf, args, NULL);
607227569Sphilip		lc = login_getpwclass(pwd);
608227569Sphilip		if (lc == NULL || login_setcryptfmt(lc, "md5", NULL) == NULL)
609227569Sphilip			warn("setting crypt(3) format");
610227569Sphilip		login_close(lc);
611227569Sphilip		pwd->pw_passwd = pw_password(cnf, args, pwd->pw_name);
612227569Sphilip		edited = 1;
613227569Sphilip
614227569Sphilip		if (pwd->pw_uid == 0 && strcmp(pwd->pw_name, "root") != 0)
615227569Sphilip			warnx("WARNING: new account `%s' has a uid of 0 (superuser access!)", pwd->pw_name);
616227569Sphilip	}
617227569Sphilip
618227569Sphilip	/*
619227569Sphilip	 * Shared add/edit code
620227569Sphilip	 */
621227569Sphilip	if ((arg = getarg(args, 'c')) != NULL) {
622227569Sphilip		char	*gecos = pw_checkname((u_char *)arg->val, 1);
623227569Sphilip		if (strcmp(pwd->pw_gecos, gecos) != 0) {
624227569Sphilip			pwd->pw_gecos = gecos;
625227569Sphilip			edited = 1;
626227569Sphilip		}
627227569Sphilip	}
628227569Sphilip
629227569Sphilip	if ((arg = getarg(args, 'h')) != NULL ||
630227569Sphilip	    (arg = getarg(args, 'H')) != NULL) {
631227569Sphilip		if (strcmp(arg->val, "-") == 0) {
632227569Sphilip			if (!pwd->pw_passwd || *pwd->pw_passwd != '*') {
633227569Sphilip				pwd->pw_passwd = "*";	/* No access */
634227569Sphilip				edited = 1;
635227569Sphilip			}
636227569Sphilip		} else {
637227569Sphilip			int             fd = atoi(arg->val);
638227569Sphilip			int		precrypt = (arg->ch == 'H');
639227569Sphilip			int             b;
640227569Sphilip			int             istty = isatty(fd);
641227569Sphilip			struct termios  t;
642227569Sphilip			login_cap_t	*lc;
643227569Sphilip
644227569Sphilip			if (istty) {
645227569Sphilip				if (tcgetattr(fd, &t) == -1)
646227569Sphilip					istty = 0;
647227569Sphilip				else {
648227569Sphilip					struct termios  n = t;
649227569Sphilip
650227569Sphilip					/* Disable echo */
651227569Sphilip					n.c_lflag &= ~(ECHO);
652293806Sarybchik					tcsetattr(fd, TCSANOW, &n);
653227569Sphilip					printf("%s%spassword for user %s:",
654227569Sphilip					     (mode == M_UPDATE) ? "new " : "",
655227569Sphilip					     precrypt ? "encrypted " : "",
656227569Sphilip					     pwd->pw_name);
657227569Sphilip					fflush(stdout);
658293806Sarybchik				}
659227569Sphilip			}
660227569Sphilip			b = read(fd, line, sizeof(line) - 1);
661227569Sphilip			if (istty) {	/* Restore state */
662283514Sarybchik				tcsetattr(fd, TCSANOW, &t);
663227569Sphilip				fputc('\n', stdout);
664227569Sphilip				fflush(stdout);
665227569Sphilip			}
666227569Sphilip			if (b < 0) {
667227569Sphilip				warn("-%c file descriptor", precrypt ? 'H' :
668227569Sphilip				    'h');
669227569Sphilip				return EX_IOERR;
670227569Sphilip			}
671227569Sphilip			line[b] = '\0';
672227569Sphilip			if ((p = strpbrk(line, "\r\n")) != NULL)
673227569Sphilip				*p = '\0';
674227569Sphilip			if (!*line)
675227569Sphilip				errx(EX_DATAERR, "empty password read on file descriptor %d", fd);
676227569Sphilip			if (precrypt) {
677227569Sphilip				if (strchr(line, ':') != NULL)
678227569Sphilip					return EX_DATAERR;
679227569Sphilip				pwd->pw_passwd = line;
680227569Sphilip			} else {
681227569Sphilip				lc = login_getpwclass(pwd);
682227569Sphilip				if (lc == NULL ||
683227569Sphilip				    login_setcryptfmt(lc, "md5", NULL) == NULL)
684227569Sphilip					warn("setting crypt(3) format");
685227569Sphilip				login_close(lc);
686227569Sphilip				pwd->pw_passwd = pw_pwcrypt(line);
687227569Sphilip			}
688227569Sphilip			edited = 1;
689227569Sphilip		}
690227569Sphilip	}
691227569Sphilip
692227569Sphilip	/*
693227569Sphilip	 * Special case: -N only displays & exits
694227569Sphilip	 */
695227569Sphilip	if (getarg(args, 'N') != NULL)
696227569Sphilip		return print_user(pwd,
697227569Sphilip				  getarg(args, 'P') != NULL,
698227569Sphilip				  getarg(args, '7') != NULL);
699227569Sphilip
700227569Sphilip	if (mode == M_ADD) {
701227569Sphilip		edited = 1;	/* Always */
702227569Sphilip		rc = addpwent(pwd);
703227569Sphilip		if (rc == -1) {
704227569Sphilip			warnx("user '%s' already exists", pwd->pw_name);
705227569Sphilip			return EX_IOERR;
706227569Sphilip		} else if (rc != 0) {
707227569Sphilip			warn("passwd file update");
708227569Sphilip			return EX_IOERR;
709227569Sphilip		}
710227569Sphilip		if (cnf->nispasswd && *cnf->nispasswd=='/') {
711227569Sphilip			rc = addnispwent(cnf->nispasswd, pwd);
712227569Sphilip			if (rc == -1)
713227569Sphilip				warnx("User '%s' already exists in NIS passwd", pwd->pw_name);
714227569Sphilip			else
715227569Sphilip				warn("NIS passwd update");
716227569Sphilip			/* NOTE: we treat NIS-only update errors as non-fatal */
717227569Sphilip		}
718227569Sphilip	} else if (mode == M_UPDATE || mode == M_LOCK || mode == M_UNLOCK) {
719227569Sphilip		if (edited) {	/* Only updated this if required */
720227569Sphilip			rc = chgpwent(a_name->val, pwd);
721283514Sarybchik			if (rc == -1) {
722227569Sphilip				warnx("user '%s' does not exist (NIS?)", pwd->pw_name);
723227569Sphilip				return EX_IOERR;
724227569Sphilip			} else if (rc != 0) {
725227569Sphilip				warn("passwd file update");
726227569Sphilip				return EX_IOERR;
727227569Sphilip			}
728227569Sphilip			if ( cnf->nispasswd && *cnf->nispasswd=='/') {
729227569Sphilip				rc = chgnispwent(cnf->nispasswd, a_name->val, pwd);
730227569Sphilip				if (rc == -1)
731227569Sphilip					warn("User '%s' not found in NIS passwd", pwd->pw_name);
732227569Sphilip				else
733227569Sphilip					warn("NIS passwd update");
734227569Sphilip				/* NOTE: NIS-only update errors are not fatal */
735227569Sphilip			}
736227569Sphilip		}
737227569Sphilip	}
738227569Sphilip
739227569Sphilip	/*
740227569Sphilip	 * Ok, user is created or changed - now edit group file
741227569Sphilip	 */
742227569Sphilip
743227569Sphilip	if (mode == M_ADD || getarg(args, 'G') != NULL) {
744227569Sphilip		int i;
745227569Sphilip		for (i = 0; cnf->groups[i] != NULL; i++) {
746227569Sphilip			grp = GETGRNAM(cnf->groups[i]);
747227569Sphilip			grp = gr_add(grp, pwd->pw_name);
748227569Sphilip			/*
749227569Sphilip			 * grp can only be NULL in 2 cases:
750227569Sphilip			 * - the new member is already a member
751227569Sphilip			 * - a problem with memory occurs
752227569Sphilip			 * in both cases we want to skip now.
753227569Sphilip			 */
754227569Sphilip			if (grp == NULL)
755227569Sphilip				continue;
756227569Sphilip			chggrent(cnf->groups[i], grp);
757227569Sphilip			free(grp);
758227569Sphilip		}
759227569Sphilip	}
760227569Sphilip
761227569Sphilip
762227569Sphilip	/* go get a current version of pwd */
763227569Sphilip	pwd = GETPWNAM(a_name->val);
764227569Sphilip	if (pwd == NULL) {
765227569Sphilip		/* This will fail when we rename, so special case that */
766227569Sphilip		if (mode == M_UPDATE && (arg = getarg(args, 'l')) != NULL) {
767227569Sphilip			a_name->val = arg->val;		/* update new name */
768283514Sarybchik			pwd = GETPWNAM(a_name->val);	/* refetch renamed rec */
769227569Sphilip		}
770227569Sphilip	}
771227569Sphilip	if (pwd == NULL)	/* can't go on without this */
772227569Sphilip		errx(EX_NOUSER, "user '%s' disappeared during update", a_name->val);
773227569Sphilip
774227569Sphilip	grp = GETGRGID(pwd->pw_gid);
775227569Sphilip	pw_log(cnf, mode, W_USER, "%s(%ld):%s(%ld):%s:%s:%s",
776227569Sphilip	       pwd->pw_name, (long) pwd->pw_uid,
777227569Sphilip	    grp ? grp->gr_name : "unknown", (long) (grp ? grp->gr_gid : -1),
778227569Sphilip	       pwd->pw_gecos, pwd->pw_dir, pwd->pw_shell);
779227569Sphilip
780227569Sphilip	/*
781227569Sphilip	 * If adding, let's touch and chown the user's mail file. This is not
782227569Sphilip	 * strictly necessary under BSD with a 0755 maildir but it also
783227569Sphilip	 * doesn't hurt anything to create the empty mailfile
784227569Sphilip	 */
785227569Sphilip	if (mode == M_ADD) {
786227569Sphilip		if (!PWALTDIR()) {
787227569Sphilip			sprintf(line, "%s/%s", _PATH_MAILDIR, pwd->pw_name);
788227569Sphilip			close(open(line, O_RDWR | O_CREAT, 0600));	/* Preserve contents &
789227569Sphilip									 * mtime */
790227569Sphilip			chown(line, pwd->pw_uid, pwd->pw_gid);
791227569Sphilip		}
792227569Sphilip	}
793227569Sphilip
794283514Sarybchik	/*
795227569Sphilip	 * Let's create and populate the user's home directory. Note
796227569Sphilip	 * that this also `works' for editing users if -m is used, but
797227569Sphilip	 * existing files will *not* be overwritten.
798227569Sphilip	 */
799227569Sphilip	if (!PWALTDIR() && getarg(args, 'm') != NULL && pwd->pw_dir && *pwd->pw_dir == '/' && pwd->pw_dir[1]) {
800227569Sphilip		copymkdir(pwd->pw_dir, cnf->dotdir, cnf->homemode, pwd->pw_uid, pwd->pw_gid);
801227569Sphilip		pw_log(cnf, mode, W_USER, "%s(%ld) home %s made",
802227569Sphilip		       pwd->pw_name, (long) pwd->pw_uid, pwd->pw_dir);
803227569Sphilip	}
804227569Sphilip
805227569Sphilip
806227569Sphilip	/*
807264461Sgnn	 * Finally, send mail to the new user as well, if we are asked to
808227569Sphilip	 */
809227569Sphilip	if (mode == M_ADD && !PWALTDIR() && cnf->newmail && *cnf->newmail && (fp = fopen(cnf->newmail, "r")) != NULL) {
810227569Sphilip		FILE           *pfp = popen(_PATH_SENDMAIL " -t", "w");
811264461Sgnn
812227569Sphilip		if (pfp == NULL)
813264461Sgnn			warn("sendmail");
814227569Sphilip		else {
815227569Sphilip			fprintf(pfp, "From: root\n" "To: %s\n" "Subject: Welcome!\n\n", pwd->pw_name);
816264461Sgnn			while (fgets(line, sizeof(line), fp) != NULL) {
817227569Sphilip				/* Do substitutions? */
818227569Sphilip				fputs(line, pfp);
819227569Sphilip			}
820227569Sphilip			pclose(pfp);
821264461Sgnn			pw_log(cnf, mode, W_USER, "%s(%ld) new user mail sent",
822227569Sphilip			    pwd->pw_name, (long) pwd->pw_uid);
823227569Sphilip		}
824264461Sgnn		fclose(fp);
825227569Sphilip	}
826227569Sphilip
827227569Sphilip	return EXIT_SUCCESS;
828227569Sphilip}
829227569Sphilip
830227569Sphilip
831227569Sphilipstatic          uid_t
832227569Sphilippw_uidpolicy(struct userconf * cnf, struct cargs * args)
833264461Sgnn{
834227569Sphilip	struct passwd  *pwd;
835283514Sarybchik	uid_t           uid = (uid_t) - 1;
836283514Sarybchik	struct carg    *a_uid = getarg(args, 'u');
837227569Sphilip
838227569Sphilip	/*
839227569Sphilip	 * Check the given uid, if any
840264461Sgnn	 */
841227569Sphilip	if (a_uid != NULL) {
842264461Sgnn		uid = (uid_t) atol(a_uid->val);
843227569Sphilip
844227569Sphilip		if ((pwd = GETPWUID(uid)) != NULL && getarg(args, 'o') == NULL)
845227569Sphilip			errx(EX_DATAERR, "uid `%ld' has already been allocated", (long) pwd->pw_uid);
846227569Sphilip	} else {
847227569Sphilip		struct bitmap   bm;
848227569Sphilip
849227569Sphilip		/*
850227569Sphilip		 * We need to allocate the next available uid under one of
851227569Sphilip		 * two policies a) Grab the first unused uid b) Grab the
852227569Sphilip		 * highest possible unused uid
853227569Sphilip		 */
854227569Sphilip		if (cnf->min_uid >= cnf->max_uid) {	/* Sanity
855227569Sphilip							 * claus^H^H^H^Hheck */
856227569Sphilip			cnf->min_uid = 1000;
857227569Sphilip			cnf->max_uid = 32000;
858227569Sphilip		}
859227569Sphilip		bm = bm_alloc(cnf->max_uid - cnf->min_uid + 1);
860227569Sphilip
861227569Sphilip		/*
862227569Sphilip		 * Now, let's fill the bitmap from the password file
863227569Sphilip		 */
864227569Sphilip		SETPWENT();
865227569Sphilip		while ((pwd = GETPWENT()) != NULL)
866227569Sphilip			if (pwd->pw_uid >= (uid_t) cnf->min_uid && pwd->pw_uid <= (uid_t) cnf->max_uid)
867227569Sphilip				bm_setbit(&bm, pwd->pw_uid - cnf->min_uid);
868227569Sphilip		ENDPWENT();
869227569Sphilip
870227569Sphilip		/*
871227569Sphilip		 * Then apply the policy, with fallback to reuse if necessary
872227569Sphilip		 */
873227569Sphilip		if (cnf->reuse_uids || (uid = (uid_t) (bm_lastset(&bm) + cnf->min_uid + 1)) > cnf->max_uid)
874227569Sphilip			uid = (uid_t) (bm_firstunset(&bm) + cnf->min_uid);
875227569Sphilip
876227569Sphilip		/*
877227569Sphilip		 * Another sanity check
878227569Sphilip		 */
879227569Sphilip		if (uid < cnf->min_uid || uid > cnf->max_uid)
880227569Sphilip			errx(EX_SOFTWARE, "unable to allocate a new uid - range fully used");
881227569Sphilip		bm_dealloc(&bm);
882227569Sphilip	}
883227569Sphilip	return uid;
884227569Sphilip}
885227569Sphilip
886227569Sphilip
887227569Sphilipstatic          uid_t
888227569Sphilippw_gidpolicy(struct userconf * cnf, struct cargs * args, char *nam, gid_t prefer)
889227569Sphilip{
890227569Sphilip	struct group   *grp;
891227569Sphilip	gid_t           gid = (uid_t) - 1;
892227569Sphilip	struct carg    *a_gid = getarg(args, 'g');
893227569Sphilip
894227569Sphilip	/*
895227569Sphilip	 * If no arg given, see if default can help out
896227569Sphilip	 */
897227569Sphilip	if (a_gid == NULL && cnf->default_group && *cnf->default_group)
898227569Sphilip		a_gid = addarg(args, 'g', cnf->default_group);
899227569Sphilip
900227569Sphilip	/*
901227569Sphilip	 * Check the given gid, if any
902227569Sphilip	 */
903227569Sphilip	SETGRENT();
904227569Sphilip	if (a_gid != NULL) {
905227569Sphilip		if ((grp = GETGRNAM(a_gid->val)) == NULL) {
906227569Sphilip			gid = (gid_t) atol(a_gid->val);
907227569Sphilip			if ((gid == 0 && !isdigit((unsigned char)*a_gid->val)) || (grp = GETGRGID(gid)) == NULL)
908227569Sphilip				errx(EX_NOUSER, "group `%s' is not defined", a_gid->val);
909227569Sphilip		}
910227569Sphilip		gid = grp->gr_gid;
911227569Sphilip	} else if ((grp = GETGRNAM(nam)) != NULL && grp->gr_mem[0] == NULL) {
912227569Sphilip		gid = grp->gr_gid;  /* Already created? Use it anyway... */
913227569Sphilip	} else {
914227569Sphilip		struct cargs    grpargs;
915227569Sphilip		char            tmp[32];
916227569Sphilip
917227569Sphilip		LIST_INIT(&grpargs);
918227569Sphilip		addarg(&grpargs, 'n', nam);
919227569Sphilip
920227569Sphilip		/*
921227569Sphilip		 * We need to auto-create a group with the user's name. We
922227569Sphilip		 * can send all the appropriate output to our sister routine
923283514Sarybchik		 * bit first see if we can create a group with gid==uid so we
924227569Sphilip		 * can keep the user and group ids in sync. We purposely do
925227569Sphilip		 * NOT check the gid range if we can force the sync. If the
926227569Sphilip		 * user's name dups an existing group, then the group add
927227569Sphilip		 * function will happily handle that case for us and exit.
928227569Sphilip		 */
929227569Sphilip		if (GETGRGID(prefer) == NULL) {
930227569Sphilip			sprintf(tmp, "%lu", (unsigned long) prefer);
931227569Sphilip			addarg(&grpargs, 'g', tmp);
932227569Sphilip		}
933227569Sphilip		if (getarg(args, 'N'))
934227569Sphilip		{
935227569Sphilip			addarg(&grpargs, 'N', NULL);
936227569Sphilip			addarg(&grpargs, 'q', NULL);
937227569Sphilip			gid = pw_group(cnf, M_NEXT, &grpargs);
938227569Sphilip		}
939227569Sphilip		else
940227569Sphilip		{
941227569Sphilip			pw_group(cnf, M_ADD, &grpargs);
942227569Sphilip			if ((grp = GETGRNAM(nam)) != NULL)
943227569Sphilip				gid = grp->gr_gid;
944227569Sphilip		}
945227569Sphilip		a_gid = LIST_FIRST(&grpargs);
946227569Sphilip		while (a_gid != NULL) {
947227569Sphilip			struct carg    *t = LIST_NEXT(a_gid, list);
948227569Sphilip			LIST_REMOVE(a_gid, list);
949227569Sphilip			a_gid = t;
950227569Sphilip		}
951283514Sarybchik	}
952227569Sphilip	ENDGRENT();
953227569Sphilip	return gid;
954227569Sphilip}
955227569Sphilip
956227569Sphilip
957227569Sphilipstatic          time_t
958227569Sphilippw_pwdpolicy(struct userconf * cnf, struct cargs * args)
959227569Sphilip{
960227569Sphilip	time_t          result = 0;
961227569Sphilip	time_t          now = time(NULL);
962227569Sphilip	struct carg    *arg = getarg(args, 'p');
963227569Sphilip
964227569Sphilip	if (arg != NULL) {
965227569Sphilip		if ((result = parse_date(now, arg->val)) == now)
966279078Sarybchik			errx(EX_DATAERR, "invalid date/time `%s'", arg->val);
967279078Sarybchik	} else if (cnf->password_days > 0)
968279078Sarybchik		result = now + ((long) cnf->password_days * 86400L);
969279078Sarybchik	return result;
970279078Sarybchik}
971279078Sarybchik
972227569Sphilip
973227569Sphilipstatic          time_t
974227569Sphilippw_exppolicy(struct userconf * cnf, struct cargs * args)
975227569Sphilip{
976227569Sphilip	time_t          result = 0;
977227569Sphilip	time_t          now = time(NULL);
978227569Sphilip	struct carg    *arg = getarg(args, 'e');
979227569Sphilip
980227569Sphilip	if (arg != NULL) {
981227569Sphilip		if ((result = parse_date(now, arg->val)) == now)
982283514Sarybchik			errx(EX_DATAERR, "invalid date/time `%s'", arg->val);
983283514Sarybchik	} else if (cnf->expire_days > 0)
984283514Sarybchik		result = now + ((long) cnf->expire_days * 86400L);
985227569Sphilip	return result;
986227569Sphilip}
987227569Sphilip
988227569Sphilip
989227569Sphilipstatic char    *
990227569Sphilippw_homepolicy(struct userconf * cnf, struct cargs * args, char const * user)
991227569Sphilip{
992227569Sphilip	struct carg    *arg = getarg(args, 'd');
993227569Sphilip
994227569Sphilip	if (arg)
995227569Sphilip		return arg->val;
996227569Sphilip	else {
997227569Sphilip		static char     home[128];
998291436Sarybchik
999227569Sphilip		if (cnf->home == NULL || *cnf->home == '\0')
1000283514Sarybchik			errx(EX_CONFIG, "no base home directory set");
1001227569Sphilip		sprintf(home, "%s/%s", cnf->home, user);
1002227569Sphilip		return home;
1003227569Sphilip	}
1004227569Sphilip}
1005227569Sphilip
1006227569Sphilipstatic char    *
1007227569Sphilipshell_path(char const * path, char *shells[], char *sh)
1008227569Sphilip{
1009227569Sphilip	if (sh != NULL && (*sh == '/' || *sh == '\0'))
1010227569Sphilip		return sh;	/* specified full path or forced none */
1011227569Sphilip	else {
1012227569Sphilip		char           *p;
1013227569Sphilip		char            paths[_UC_MAXLINE];
1014227569Sphilip
1015227569Sphilip		/*
1016227569Sphilip		 * We need to search paths
1017227569Sphilip		 */
1018227569Sphilip		strlcpy(paths, path, sizeof(paths));
1019227569Sphilip		for (p = strtok(paths, ": \t\r\n"); p != NULL; p = strtok(NULL, ": \t\r\n")) {
1020227569Sphilip			int             i;
1021227569Sphilip			static char     shellpath[256];
1022227569Sphilip
1023227569Sphilip			if (sh != NULL) {
1024227569Sphilip				sprintf(shellpath, "%s/%s", p, sh);
1025227569Sphilip				if (access(shellpath, X_OK) == 0)
1026227569Sphilip					return shellpath;
1027227569Sphilip			} else
1028227569Sphilip				for (i = 0; i < _UC_MAXSHELLS && shells[i] != NULL; i++) {
1029227569Sphilip					sprintf(shellpath, "%s/%s", p, shells[i]);
1030227569Sphilip					if (access(shellpath, X_OK) == 0)
1031227569Sphilip						return shellpath;
1032227569Sphilip				}
1033227569Sphilip		}
1034227569Sphilip		if (sh == NULL)
1035227569Sphilip			errx(EX_OSFILE, "can't find shell `%s' in shell paths", sh);
1036227569Sphilip		errx(EX_CONFIG, "no default shell available or defined");
1037227569Sphilip		return NULL;
1038227569Sphilip	}
1039227569Sphilip}
1040227569Sphilip
1041227569Sphilip
1042227569Sphilipstatic char    *
1043227569Sphilippw_shellpolicy(struct userconf * cnf, struct cargs * args, char *newshell)
1044227569Sphilip{
1045227569Sphilip	char           *sh = newshell;
1046227569Sphilip	struct carg    *arg = getarg(args, 's');
1047227569Sphilip
1048227569Sphilip	if (newshell == NULL && arg != NULL)
1049227569Sphilip		sh = arg->val;
1050227569Sphilip	return shell_path(cnf->shelldir, cnf->shells, sh ? sh : cnf->shell_default);
1051227569Sphilip}
1052227569Sphilip
1053279141Sarybchik#define	SALTSIZE	32
1054227569Sphilip
1055291436Sarybchikstatic char const chars[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ./";
1056283514Sarybchik
1057227569Sphilipchar           *
1058227569Sphilippw_pwcrypt(char *password)
1059227569Sphilip{
1060227569Sphilip	int             i;
1061227569Sphilip	char            salt[SALTSIZE + 1];
1062227569Sphilip	char		*cryptpw;
1063227569Sphilip
1064227569Sphilip	static char     buf[256];
1065227569Sphilip
1066227569Sphilip	/*
1067227569Sphilip	 * Calculate a salt value
1068227569Sphilip	 */
1069227569Sphilip	for (i = 0; i < SALTSIZE; i++)
1070227569Sphilip		salt[i] = chars[arc4random_uniform(sizeof(chars) - 1)];
1071227569Sphilip	salt[SALTSIZE] = '\0';
1072227569Sphilip
1073227569Sphilip	cryptpw = crypt(password, salt);
1074227569Sphilip	if (cryptpw == NULL)
1075227569Sphilip		errx(EX_CONFIG, "crypt(3) failure");
1076283514Sarybchik	return strcpy(buf, cryptpw);
1077283514Sarybchik}
1078227569Sphilip
1079227569Sphilip
1080227569Sphilipstatic char    *
1081227569Sphilippw_password(struct userconf * cnf, struct cargs * args, char const * user)
1082227569Sphilip{
1083227569Sphilip	int             i, l;
1084227569Sphilip	char            pwbuf[32];
1085227569Sphilip
1086227569Sphilip	switch (cnf->default_password) {
1087227569Sphilip	case -1:		/* Random password */
1088227569Sphilip		l = (arc4random() % 8 + 8);	/* 8 - 16 chars */
1089227569Sphilip		for (i = 0; i < l; i++)
1090227569Sphilip			pwbuf[i] = chars[arc4random_uniform(sizeof(chars)-1)];
1091227569Sphilip		pwbuf[i] = '\0';
1092227569Sphilip
1093227569Sphilip		/*
1094227569Sphilip		 * We give this information back to the user
1095227569Sphilip		 */
1096227569Sphilip		if (getarg(args, 'h') == NULL && getarg(args, 'H') == NULL &&
1097227569Sphilip		    getarg(args, 'N') == NULL) {
1098227569Sphilip			if (isatty(STDOUT_FILENO))
1099227569Sphilip				printf("Password for '%s' is: ", user);
1100227569Sphilip			printf("%s\n", pwbuf);
1101227569Sphilip			fflush(stdout);
1102227569Sphilip		}
1103227569Sphilip		break;
1104227569Sphilip
1105227569Sphilip	case -2:		/* No password at all! */
1106227569Sphilip		return "";
1107227569Sphilip
1108227569Sphilip	case 0:		/* No login - default */
1109227569Sphilip	default:
1110227569Sphilip		return "*";
1111227569Sphilip
1112227569Sphilip	case 1:		/* user's name */
1113227569Sphilip		strlcpy(pwbuf, user, sizeof(pwbuf));
1114227569Sphilip		break;
1115227569Sphilip	}
1116227569Sphilip	return pw_pwcrypt(pwbuf);
1117227569Sphilip}
1118227569Sphilip
1119227569Sphilip
1120227569Sphilipstatic int
1121227569Sphilipprint_user(struct passwd * pwd, int pretty, int v7)
1122227569Sphilip{
1123227569Sphilip	if (!pretty) {
1124227569Sphilip		char            *buf;
1125227569Sphilip
1126227569Sphilip		if (!v7)
1127227569Sphilip			pwd->pw_passwd = (pwd->pw_passwd == NULL) ? "" : "*";
1128227569Sphilip
1129227569Sphilip		buf = v7 ? pw_make_v7(pwd) : pw_make(pwd);
1130227569Sphilip		printf("%s\n", buf);
1131227569Sphilip		free(buf);
1132227569Sphilip	} else {
1133227569Sphilip		int		j;
1134227569Sphilip		char           *p;
1135227569Sphilip		struct group   *grp = GETGRGID(pwd->pw_gid);
1136227569Sphilip		char            uname[60] = "User &", office[60] = "[None]",
1137227569Sphilip		                wphone[60] = "[None]", hphone[60] = "[None]";
1138227569Sphilip		char		acexpire[32] = "[None]", pwexpire[32] = "[None]";
1139227569Sphilip		struct tm *    tptr;
1140283514Sarybchik
1141283514Sarybchik		if ((p = strtok(pwd->pw_gecos, ",")) != NULL) {
1142283514Sarybchik			strlcpy(uname, p, sizeof(uname));
1143283514Sarybchik			if ((p = strtok(NULL, ",")) != NULL) {
1144283514Sarybchik				strlcpy(office, p, sizeof(office));
1145283514Sarybchik				if ((p = strtok(NULL, ",")) != NULL) {
1146283514Sarybchik					strlcpy(wphone, p, sizeof(wphone));
1147283514Sarybchik					if ((p = strtok(NULL, "")) != NULL) {
1148283514Sarybchik						strlcpy(hphone, p,
1149283514Sarybchik						    sizeof(hphone));
1150283514Sarybchik					}
1151283514Sarybchik				}
1152283514Sarybchik			}
1153283514Sarybchik		}
1154283514Sarybchik		/*
1155283514Sarybchik		 * Handle '&' in gecos field
1156283514Sarybchik		 */
1157283514Sarybchik		if ((p = strchr(uname, '&')) != NULL) {
1158283514Sarybchik			int             l = strlen(pwd->pw_name);
1159283514Sarybchik			int             m = strlen(p);
1160283514Sarybchik
1161283514Sarybchik			memmove(p + l, p + 1, m);
1162283514Sarybchik			memmove(p, pwd->pw_name, l);
1163283514Sarybchik			*p = (char) toupper((unsigned char)*p);
1164283514Sarybchik		}
1165283514Sarybchik		if (pwd->pw_expire > (time_t)0 && (tptr = localtime(&pwd->pw_expire)) != NULL)
1166283514Sarybchik			strftime(acexpire, sizeof acexpire, "%c", tptr);
1167283514Sarybchik		if (pwd->pw_change > (time_t)0 && (tptr = localtime(&pwd->pw_change)) != NULL)
1168283514Sarybchik			strftime(pwexpire, sizeof pwexpire, "%c", tptr);
1169283514Sarybchik		printf("Login Name: %-15s   #%-12ld Group: %-15s   #%ld\n"
1170283514Sarybchik		       " Full Name: %s\n"
1171283514Sarybchik		       "      Home: %-26.26s      Class: %s\n"
1172283514Sarybchik		       "     Shell: %-26.26s     Office: %s\n"
1173283514Sarybchik		       "Work Phone: %-26.26s Home Phone: %s\n"
1174283514Sarybchik		       "Acc Expire: %-26.26s Pwd Expire: %s\n",
1175283514Sarybchik		       pwd->pw_name, (long) pwd->pw_uid,
1176283514Sarybchik		       grp ? grp->gr_name : "(invalid)", (long) pwd->pw_gid,
1177283514Sarybchik		       uname, pwd->pw_dir, pwd->pw_class,
1178283514Sarybchik		       pwd->pw_shell, office, wphone, hphone,
1179283514Sarybchik		       acexpire, pwexpire);
1180283514Sarybchik	        SETGRENT();
1181283514Sarybchik		j = 0;
1182283514Sarybchik		while ((grp=GETGRENT()) != NULL)
1183227569Sphilip		{
1184227569Sphilip			int     i = 0;
1185227569Sphilip			while (grp->gr_mem[i] != NULL)
1186227569Sphilip			{
1187227569Sphilip				if (strcmp(grp->gr_mem[i], pwd->pw_name)==0)
1188227569Sphilip				{
1189227569Sphilip					printf(j++ == 0 ? "    Groups: %s" : ",%s", grp->gr_name);
1190227569Sphilip					break;
1191227569Sphilip				}
1192227569Sphilip				++i;
1193227569Sphilip			}
1194227569Sphilip		}
1195227569Sphilip		ENDGRENT();
1196227569Sphilip		printf("%s", j ? "\n" : "");
1197227569Sphilip	}
1198227569Sphilip	return EXIT_SUCCESS;
1199227569Sphilip}
1200227569Sphilip
1201227569Sphilipchar    *
1202227569Sphilippw_checkname(u_char *name, int gecos)
1203227569Sphilip{
1204227569Sphilip	char showch[8];
1205227569Sphilip	u_char const *badchars, *ch, *showtype;
1206227569Sphilip	int reject;
1207227569Sphilip
1208227569Sphilip	ch = name;
1209283514Sarybchik	reject = 0;
1210283514Sarybchik	if (gecos) {
1211227569Sphilip		/* See if the name is valid as a gecos (comment) field. */
1212227569Sphilip		badchars = ":!@";
1213227569Sphilip		showtype = "gecos field";
1214227569Sphilip	} else {
1215227569Sphilip		/* See if the name is valid as a userid or group. */
1216227569Sphilip		badchars = " ,\t:+&#%$^()!@~*?<>=|\\/\"";
1217227569Sphilip		showtype = "userid/group name";
1218227569Sphilip		/* Userids and groups can not have a leading '-'. */
1219227569Sphilip		if (*ch == '-')
1220227569Sphilip			reject = 1;
1221227569Sphilip	}
1222227569Sphilip	if (!reject) {
1223227569Sphilip		while (*ch) {
1224227569Sphilip			if (strchr(badchars, *ch) != NULL || *ch < ' ' ||
1225227569Sphilip			    *ch == 127) {
1226227569Sphilip				reject = 1;
1227227569Sphilip				break;
1228291436Sarybchik			}
1229283514Sarybchik			/* 8-bit characters are only allowed in GECOS fields */
1230227569Sphilip			if (!gecos && (*ch & 0x80)) {
1231227569Sphilip				reject = 1;
1232227569Sphilip				break;
1233227569Sphilip			}
1234279048Sarybchik			ch++;
1235227569Sphilip		}
1236227569Sphilip	}
1237291436Sarybchik	/*
1238227569Sphilip	 * A `$' is allowed as the final character for userids and groups,
1239279182Sarybchik	 * mainly for the benefit of samba.
1240227569Sphilip	 */
1241227569Sphilip	if (reject && !gecos) {
1242227569Sphilip		if (*ch == '$' && *(ch + 1) == '\0') {
1243227569Sphilip			reject = 0;
1244227569Sphilip			ch++;
1245227569Sphilip		}
1246227569Sphilip	}
1247227569Sphilip	if (reject) {
1248227569Sphilip		snprintf(showch, sizeof(showch), (*ch >= ' ' && *ch < 127)
1249227569Sphilip		    ? "`%c'" : "0x%02x", *ch);
1250227569Sphilip		errx(EX_DATAERR, "invalid character %s at position %td in %s",
1251227569Sphilip		    showch, (ch - name), showtype);
1252227569Sphilip	}
1253227569Sphilip	if (!gecos && (ch - name) > LOGNAMESIZE)
1254227569Sphilip		errx(EX_DATAERR, "name too long `%s' (max is %d)", name,
1255227569Sphilip		    LOGNAMESIZE);
1256227569Sphilip	return (char *)name;
1257227569Sphilip}
1258279182Sarybchik
1259227569Sphilip
1260279048Sarybchikstatic void
1261279048Sarybchikrmat(uid_t uid)
1262279048Sarybchik{
1263227569Sphilip	DIR            *d = opendir("/var/at/jobs");
1264227569Sphilip
1265227569Sphilip	if (d != NULL) {
1266227569Sphilip		struct dirent  *e;
1267279048Sarybchik
1268227569Sphilip		while ((e = readdir(d)) != NULL) {
1269227569Sphilip			struct stat     st;
1270227569Sphilip
1271279048Sarybchik			if (strncmp(e->d_name, ".lock", 5) != 0 &&
1272227569Sphilip			    stat(e->d_name, &st) == 0 &&
1273227569Sphilip			    !S_ISDIR(st.st_mode) &&
1274227569Sphilip			    st.st_uid == uid) {
1275227569Sphilip				char            tmp[MAXPATHLEN];
1276227569Sphilip
1277227569Sphilip				sprintf(tmp, "/usr/bin/atrm %s", e->d_name);
1278227569Sphilip				system(tmp);
1279227569Sphilip			}
1280227569Sphilip		}
1281227569Sphilip		closedir(d);
1282291436Sarybchik	}
1283227569Sphilip}
1284227569Sphilip
1285227569Sphilipstatic void
1286227569Sphiliprmopie(char const * name)
1287291436Sarybchik{
1288283514Sarybchik	static const char etcopie[] = "/etc/opiekeys";
1289227569Sphilip	FILE   *fp = fopen(etcopie, "r+");
1290227569Sphilip
1291227569Sphilip	if (fp != NULL) {
1292227569Sphilip		char	tmp[1024];
1293227569Sphilip		off_t	atofs = 0;
1294283514Sarybchik		int	length = strlen(name);
1295227569Sphilip
1296227569Sphilip		while (fgets(tmp, sizeof tmp, fp) != NULL) {
1297227569Sphilip			if (strncmp(name, tmp, length) == 0 && tmp[length]==' ') {
1298227569Sphilip				if (fseek(fp, atofs, SEEK_SET) == 0) {
1299291436Sarybchik					fwrite("#", 1, 1, fp);	/* Comment username out */
1300227569Sphilip				}
1301283514Sarybchik				break;
1302283514Sarybchik			}
1303227569Sphilip			atofs = ftell(fp);
1304283514Sarybchik		}
1305227569Sphilip		/*
1306227569Sphilip		 * If we got an error of any sort, don't update!
1307227569Sphilip		 */
1308227569Sphilip		fclose(fp);
1309227569Sphilip	}
1310227569Sphilip}
1311227569Sphilip
1312227569Sphilip