pw_user.c revision 23318
1169689Skan/*-
2132718Skan * Copyright (C) 1996
3132718Skan *	David L. Nugent.  All rights reserved.
4132718Skan *
5132718Skan * Redistribution and use in source and binary forms, with or without
6132718Skan * modification, are permitted provided that the following conditions
7132718Skan * are met:
8132718Skan * 1. Redistributions of source code must retain the above copyright
9132718Skan *    notice, this list of conditions and the following disclaimer.
10132718Skan * 2. Redistributions in binary form must reproduce the above copyright
11132718Skan *    notice, this list of conditions and the following disclaimer in the
12132718Skan *    documentation and/or other materials provided with the distribution.
13132718Skan *
14132718Skan * THIS SOFTWARE IS PROVIDED BY DAVID L. NUGENT AND CONTRIBUTORS ``AS IS'' AND
15132718Skan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16132718Skan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17132718Skan * ARE DISCLAIMED.  IN NO EVENT SHALL DAVID L. NUGENT OR CONTRIBUTORS BE LIABLE
18132718Skan * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19132718Skan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20169689Skan * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21132718Skan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22132718Skan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23132718Skan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24132718Skan * SUCH DAMAGE.
25132718Skan *
26132718Skan *	$Id: pw_user.c,v 1.15 1997/02/22 16:12:30 peter Exp $
27132718Skan */
28132718Skan
29132718Skan#include <unistd.h>
30132718Skan#include <fcntl.h>
31132718Skan#include <ctype.h>
32132718Skan#include <paths.h>
33132718Skan#include <sys/param.h>
34132718Skan#include <dirent.h>
35132718Skan#include <termios.h>
36132718Skan#include <sys/types.h>
37132718Skan#include <sys/time.h>
38132718Skan#include <sys/resource.h>
39132718Skan#include <utmp.h>
40132718Skan#if defined(USE_MD5RAND)
41132718Skan#include <md5.h>
42169689Skan#endif
43132718Skan#include "pw.h"
44132718Skan#include "bitmap.h"
45132718Skan#include "pwupd.h"
46132718Skan
47132718Skan#if (MAXLOGNAME-1) > UT_NAMESIZE
48132718Skan#define LOGNAMESIZE UT_NAMESIZE
49132718Skan#else
50132718Skan#define LOGNAMESIZE (MAXLOGNAME-1)
51132718Skan#endif
52132718Skan
53132718Skanstatic int      print_user(struct passwd * pwd, int pretty);
54132718Skanstatic uid_t    pw_uidpolicy(struct userconf * cnf, struct cargs * args);
55132718Skanstatic uid_t    pw_gidpolicy(struct userconf * cnf, struct cargs * args, char *nam, gid_t prefer);
56132718Skanstatic time_t   pw_pwdpolicy(struct userconf * cnf, struct cargs * args);
57132718Skanstatic time_t   pw_exppolicy(struct userconf * cnf, struct cargs * args);
58132718Skanstatic char    *pw_homepolicy(struct userconf * cnf, struct cargs * args, char const * user);
59132718Skanstatic char    *pw_shellpolicy(struct userconf * cnf, struct cargs * args, char *newshell);
60132718Skanstatic char    *pw_password(struct userconf * cnf, struct cargs * args, char const * user);
61132718Skanstatic char    *shell_path(char const * path, char *shells[], char *sh);
62132718Skanstatic void     rmat(uid_t uid);
63132718Skanstatic void	rmskey(char const * name);
64132718Skan
65132718Skan/*-
66132718Skan * -C config      configuration file
67132718Skan * -q             quiet operation
68132718Skan * -n name        login name
69132718Skan * -u uid         user id
70132718Skan * -c comment     user name/comment
71132718Skan * -d directory   home directory
72132718Skan * -e date        account expiry date
73132718Skan * -p date        password expiry date
74132718Skan * -g grp         primary group
75132718Skan * -G grp1,grp2   additional groups
76132718Skan * -m [ -k dir ]  create and set up home
77132718Skan * -s shell       name of login shell
78132718Skan * -o             duplicate uid ok
79132718Skan * -L class       user class
80132718Skan * -l name        new login name
81132718Skan * -h fd          password filehandle
82132718Skan * -F             force print or add
83132718Skan *   Setting defaults:
84132718Skan * -D             set user defaults
85132718Skan * -b dir         default home root dir
86132718Skan * -e period      default expiry period
87132718Skan * -p period      default password change period
88132718Skan * -g group       default group
89132718Skan * -G             grp1,grp2.. default additional groups
90132718Skan * -L class       default login class
91132718Skan * -k dir         default home skeleton
92132718Skan * -s shell       default shell
93132718Skan * -w method      default password method
94132718Skan */
95132718Skan
96132718Skanint
97132718Skanpw_user(struct userconf * cnf, int mode, struct cargs * args)
98132718Skan{
99132718Skan	int	        r, r1;
100132718Skan	char           *p = NULL;
101132718Skan	struct carg    *a_name;
102132718Skan	struct carg    *a_uid;
103132718Skan	struct carg    *arg;
104132718Skan	struct passwd  *pwd = NULL;
105132718Skan	struct group   *grp;
106132718Skan	struct stat     st;
107132718Skan	char            line[_PASSWORD_LEN+1];
108132718Skan
109132718Skan	static struct passwd fakeuser =
110132718Skan	{
111132718Skan		NULL,
112132718Skan		"*",
113132718Skan		-1,
114132718Skan		-1,
115132718Skan		0,
116132718Skan		"",
117132718Skan		"User &",
118132718Skan		"/bin/sh",
119132718Skan		0,
120132718Skan		0
121132718Skan	};
122132718Skan
123132718Skan	/*
124132718Skan	 * With M_NEXT, we only need to return the
125132718Skan	 * next uid to stdout
126132718Skan	 */
127132718Skan	if (mode == M_NEXT)
128132718Skan	{
129132718Skan		uid_t next = pw_uidpolicy(cnf, args);
130132718Skan		if (getarg(args, 'q'))
131132718Skan			return next;
132132718Skan		printf("%ld:", (long)next);
133132718Skan		pw_group(cnf, mode, args);
134132718Skan		return EXIT_SUCCESS;
135132718Skan	}
136132718Skan
137132718Skan	/*
138132718Skan	 * We can do all of the common legwork here
139132718Skan	 */
140132718Skan
141132718Skan	if ((arg = getarg(args, 'b')) != NULL) {
142132718Skan		cnf->home = arg->val;
143132718Skan	}
144132718Skan
145132718Skan	/*
146132718Skan	 * If we'll need to use it or we're updating it,
147132718Skan	 * then create the base home directory if necessary
148132718Skan	 */
149132718Skan	if (arg != NULL || getarg(args, 'm') != NULL) {
150132718Skan		int	l = strlen(cnf->home);
151132718Skan
152132718Skan		if (l > 1 && cnf->home[l-1] == '/')	/* Shave off any trailing path delimiter */
153132718Skan			cnf->home[--l] = '\0';
154132718Skan
155132718Skan		if (l < 2 || *cnf->home != '/')		/* Check for absolute path name */
156132718Skan			cmderr(EX_DATAERR, "invalid base directory for home '%s'\n", cnf->home);
157132718Skan
158132718Skan		if (stat(cnf->home, &st) == -1) {
159132718Skan			char	dbuf[MAXPATHLEN];
160132718Skan
161132718Skan			/*
162132718Skan			 * This is a kludge especially for Joerg :)
163132718Skan			 * If the home directory would be created in the root partition, then
164132718Skan			 * we really create it under /usr which is likely to have more space.
165132718Skan			 * But we create a symlink from cnf->home -> "/usr" -> cnf->home
166132718Skan			 */
167132718Skan			if (strchr(cnf->home+1, '/') == NULL) {
168132718Skan				strcpy(dbuf, "/usr");
169132718Skan				strncat(dbuf, cnf->home, MAXPATHLEN-5);
170132718Skan				if (mkdir(dbuf, 0755) != -1 || errno == EEXIST) {
171132718Skan					chown(dbuf, 0, 0);
172132718Skan					symlink(dbuf, cnf->home);
173132718Skan				}
174132718Skan				/* If this falls, fall back to old method */
175132718Skan			}
176132718Skan			p = strncpy(dbuf, cnf->home, sizeof dbuf);
177132718Skan			dbuf[MAXPATHLEN-1] = '\0';
178132718Skan			if (stat(dbuf, &st) == -1) {
179132718Skan				while ((p = strchr(++p, '/')) != NULL) {
180132718Skan					*p = '\0';
181132718Skan					if (stat(dbuf, &st) == -1) {
182132718Skan						if (mkdir(dbuf, 0755) == -1)
183132718Skan							goto direrr;
184132718Skan						chown(dbuf, 0, 0);
185132718Skan					} else if (!S_ISDIR(st.st_mode))
186132718Skan						cmderr(EX_OSFILE, "'%s' (root home parent) is not a directory\n", dbuf);
187132718Skan					*p = '/';
188132718Skan				}
189132718Skan			}
190132718Skan			if (stat(dbuf, &st) == -1) {
191132718Skan				if (mkdir(dbuf, 0755) == -1) {
192132718Skan				direrr:	cmderr(EX_OSFILE, "mkdir '%s': %s\n", dbuf, strerror(errno));
193132718Skan				}
194132718Skan				chown(dbuf, 0, 0);
195132718Skan			}
196132718Skan		} else if (!S_ISDIR(st.st_mode))
197132718Skan			cmderr(EX_OSFILE, "root home `%s' is not a directory\n", cnf->home);
198132718Skan	}
199132718Skan
200132718Skan
201132718Skan	if ((arg = getarg(args, 'e')) != NULL)
202132718Skan		cnf->expire_days = atoi(arg->val);
203132718Skan
204132718Skan	if ((arg = getarg(args, 'y')) != NULL)
205132718Skan		cnf->nispasswd = arg->val;
206132718Skan
207132718Skan	if ((arg = getarg(args, 'p')) != NULL && arg->val)
208132718Skan		cnf->password_days = atoi(arg->val);
209132718Skan
210132718Skan	if ((arg = getarg(args, 'g')) != NULL) {
211132718Skan		p = arg->val;
212132718Skan		if ((grp = getgrnam(p)) == NULL) {
213132718Skan			if (!isdigit(*p) || (grp = getgrgid((gid_t) atoi(p))) == NULL)
214132718Skan				cmderr(EX_NOUSER, "group `%s' does not exist\n", p);
215259563Spfg		}
216259563Spfg		cnf->default_group = newstr(grp->gr_name);
217259563Spfg	}
218259563Spfg	if ((arg = getarg(args, 'L')) != NULL)
219259563Spfg		cnf->default_class = pw_checkname((u_char *)arg->val, 0);
220132718Skan
221132718Skan	if ((arg = getarg(args, 'G')) != NULL && arg->val) {
222132718Skan		int             i = 0;
223132718Skan
224132718Skan		for (p = strtok(arg->val, ", \t"); p != NULL; p = strtok(NULL, ", \t")) {
225132718Skan			if ((grp = getgrnam(p)) == NULL) {
226132718Skan				if (!isdigit(*p) || (grp = getgrgid((gid_t) atoi(p))) == NULL)
227132718Skan					cmderr(EX_NOUSER, "group `%s' does not exist\n", p);
228132718Skan			}
229132718Skan			if (extendarray(&cnf->groups, &cnf->numgroups, i + 2) != -1)
230132718Skan				cnf->groups[i++] = newstr(grp->gr_name);
231132718Skan		}
232132718Skan		while (i < cnf->numgroups)
233132718Skan			cnf->groups[i++] = NULL;
234132718Skan	}
235132718Skan	if ((arg = getarg(args, 'k')) != NULL) {
236132718Skan		if (stat(cnf->dotdir = arg->val, &st) == -1 || S_ISDIR(st.st_mode))
237132718Skan			cmderr(EX_OSFILE, "skeleton `%s' is not a directory or does not exist\n", cnf->dotdir);
238132718Skan	}
239132718Skan	if ((arg = getarg(args, 's')) != NULL)
240132718Skan		cnf->shell_default = arg->val;
241132718Skan
242132718Skan	if (mode == M_ADD && getarg(args, 'D')) {
243132718Skan		if (getarg(args, 'n') != NULL)
244132718Skan			cmderr(EX_DATAERR, "can't combine `-D' with `-n name'\n");
245132718Skan		if ((arg = getarg(args, 'u')) != NULL && (p = strtok(arg->val, ", \t")) != NULL) {
246132718Skan			if ((cnf->min_uid = (uid_t) atoi(p)) == 0)
247132718Skan				cnf->min_uid = 1000;
248132718Skan			if ((p = strtok(NULL, " ,\t")) == NULL || (cnf->max_uid = (uid_t) atoi(p)) < cnf->min_uid)
249132718Skan				cnf->max_uid = 32000;
250132718Skan		}
251132718Skan		if ((arg = getarg(args, 'i')) != NULL && (p = strtok(arg->val, ", \t")) != NULL) {
252132718Skan			if ((cnf->min_gid = (gid_t) atoi(p)) == 0)
253132718Skan				cnf->min_gid = 1000;
254132718Skan			if ((p = strtok(NULL, " ,\t")) == NULL || (cnf->max_gid = (gid_t) atoi(p)) < cnf->min_gid)
255132718Skan				cnf->max_gid = 32000;
256132718Skan		}
257132718Skan		if ((arg = getarg(args, 'w')) != NULL)
258132718Skan			cnf->default_password = boolean_val(arg->val, cnf->default_password);
259132718Skan
260132718Skan		arg = getarg(args, 'C');
261132718Skan		if (write_userconfig(arg ? arg->val : NULL))
262132718Skan			return EXIT_SUCCESS;
263132718Skan		perror("config update");
264132718Skan		return EX_IOERR;
265132718Skan	}
266132718Skan	if (mode == M_PRINT && getarg(args, 'a')) {
267132718Skan		int             pretty = getarg(args, 'P') != NULL;
268132718Skan
269132718Skan		setpwent();
270132718Skan		while ((pwd = getpwent()) != NULL)
271132718Skan			print_user(pwd, pretty);
272132718Skan		endpwent();
273132718Skan		return EXIT_SUCCESS;
274132718Skan	}
275132718Skan	if ((a_name = getarg(args, 'n')) != NULL)
276169689Skan		pwd = getpwnam(pw_checkname((u_char *)a_name->val, 0));
277132718Skan	a_uid = getarg(args, 'u');
278132718Skan
279132718Skan	if (a_uid == NULL) {
280132718Skan		if (a_name == NULL)
281132718Skan			cmderr(EX_DATAERR, "user name or id required\n");
282132718Skan
283132718Skan		/*
284132718Skan		 * Determine whether 'n' switch is name or uid - we don't
285132718Skan		 * really don't really care which we have, but we need to
286132718Skan		 * know.
287132718Skan		 */
288132718Skan		if (mode != M_ADD && pwd == NULL && isdigit(*a_name->val) && atoi(a_name->val) > 0) {	/* Assume uid */
289132718Skan			(a_uid = a_name)->ch = 'u';
290132718Skan			a_name = NULL;
291132718Skan		}
292132718Skan	}
293132718Skan	/*
294132718Skan	 * Update, delete & print require that the user exists
295132718Skan	 */
296132718Skan	if (mode == M_UPDATE || mode == M_DELETE || mode == M_PRINT) {
297132718Skan		if (a_name == NULL && pwd == NULL)	/* Try harder */
298132718Skan			pwd = getpwuid(atoi(a_uid->val));
299132718Skan
300132718Skan		if (pwd == NULL) {
301132718Skan			if (mode == M_PRINT && getarg(args, 'F')) {
302132718Skan				fakeuser.pw_name = a_name ? a_name->val : "nouser";
303132718Skan				fakeuser.pw_uid = a_uid ? (uid_t) atol(a_uid->val) : -1;
304132718Skan				return print_user(&fakeuser, getarg(args, 'P') != NULL);
305132718Skan			}
306132718Skan			if (a_name == NULL)
307132718Skan				cmderr(EX_NOUSER, "no such uid `%s'\n", a_uid->val);
308132718Skan			cmderr(EX_NOUSER, "no such user `%s'\n", a_name->val);
309132718Skan		}
310132718Skan		if (a_name == NULL)	/* May be needed later */
311132718Skan			a_name = addarg(args, 'n', newstr(pwd->pw_name));
312132718Skan
313132718Skan		/*
314132718Skan		 * Handle deletions now
315132718Skan		 */
316132718Skan		if (mode == M_DELETE) {
317132718Skan			char            file[MAXPATHLEN];
318132718Skan			char            home[MAXPATHLEN];
319132718Skan			uid_t           uid = pwd->pw_uid;
320132718Skan
321132718Skan			if (strcmp(pwd->pw_name, "root") == 0)
322132718Skan				cmderr(EX_DATAERR, "cannot remove user 'root'\n");
323132718Skan
324132718Skan			/*
325132718Skan			 * Remove skey record from /etc/skeykeys
326132718Skan			 */
327132718Skan
328132718Skan			rmskey(pwd->pw_name);
329132718Skan
330132718Skan			/*
331132718Skan			 * Remove crontabs
332132718Skan			 */
333132718Skan			sprintf(file, "/var/cron/tabs/%s", pwd->pw_name);
334132718Skan			if (access(file, F_OK) == 0) {
335132718Skan				sprintf(file, "crontab -u %s -r", pwd->pw_name);
336132718Skan				system(file);
337132718Skan			}
338132718Skan			/*
339132718Skan			 * Save these for later, since contents of pwd may be
340132718Skan			 * invalidated by deletion
341132718Skan			 */
342132718Skan			sprintf(file, "%s/%s", _PATH_MAILDIR, pwd->pw_name);
343132718Skan			strncpy(home, pwd->pw_dir, sizeof home);
344132718Skan			home[sizeof home - 1] = '\0';
345132718Skan
346132718Skan			if (!delpwent(pwd))
347132718Skan				cmderr(EX_IOERR, "Error updating passwd file: %s\n", strerror(errno));
348132718Skan
349132718Skan			if (cnf->nispasswd && *cnf->nispasswd=='/' && !delnispwent(cnf->nispasswd, a_name->val))
350132718Skan				perror("WARNING: NIS passwd update");
351132718Skan
352132718Skan			editgroups(a_name->val, NULL);
353132718Skan
354132718Skan			pw_log(cnf, mode, W_USER, "%s(%ld) account removed", a_name->val, (long) uid);
355132718Skan
356132718Skan			/*
357132718Skan			 * Remove mail file
358132718Skan			 */
359132718Skan			remove(file);
360132718Skan
361132718Skan			/*
362132718Skan			 * Remove at jobs
363132718Skan			 */
364132718Skan			if (getpwuid(uid) == NULL)
365169689Skan				rmat(uid);
366169689Skan
367169689Skan			/*
368169689Skan			 * Remove home directory and contents
369169689Skan			 */
370169689Skan			if (getarg(args, 'r') != NULL && *home == '/' && getpwuid(uid) == NULL) {
371169689Skan				if (stat(home, &st) != -1) {
372169689Skan					rm_r(home, uid);
373169689Skan					pw_log(cnf, mode, W_USER, "%s(%ld) home '%s' %sremoved",
374169689Skan					       a_name->val, (long) uid, home,
375169689Skan					       stat(home, &st) == -1 ? "" : "not completely ");
376169689Skan				}
377169689Skan			}
378169689Skan			return EXIT_SUCCESS;
379169689Skan		} else if (mode == M_PRINT)
380169689Skan			return print_user(pwd, getarg(args, 'P') != NULL);
381169689Skan
382169689Skan		/*
383169689Skan		 * The rest is edit code
384169689Skan		 */
385169689Skan		if ((arg = getarg(args, 'l')) != NULL) {
386132718Skan			if (strcmp(pwd->pw_name, "root") == 0)
387132718Skan				cmderr(EX_DATAERR, "can't rename `root' account\n");
388132718Skan			pwd->pw_name = pw_checkname((u_char *)arg->val, 0);
389132718Skan		}
390132718Skan		if ((arg = getarg(args, 'u')) != NULL && isdigit(*arg->val)) {
391132718Skan			pwd->pw_uid = (uid_t) atol(arg->val);
392132718Skan			if (pwd->pw_uid != 0 && strcmp(pwd->pw_name, "root") == 0)
393132718Skan				cmderr(EX_DATAERR, "can't change uid of `root' account\n");
394169689Skan			if (pwd->pw_uid == 0 && strcmp(pwd->pw_name, "root") != 0)
395132718Skan				fprintf(stderr, "WARNING: account `%s' will have a uid of 0 (superuser access!)\n", pwd->pw_name);
396132718Skan		}
397132718Skan		if ((arg = getarg(args, 'g')) != NULL && pwd->pw_uid != 0)	/* Already checked this */
398132718Skan			pwd->pw_gid = (gid_t) getgrnam(cnf->default_group)->gr_gid;
399132718Skan
400132718Skan		if ((arg = getarg(args, 'p')) != NULL) {
401132718Skan			if (*arg->val == '\0' || strcmp(arg->val, "0") == 0)
402132718Skan				pwd->pw_change = 0;
403132718Skan			else {
404132718Skan				time_t          now = time(NULL);
405132718Skan				time_t          expire = parse_date(now, arg->val);
406132718Skan
407132718Skan				if (now == expire)
408132718Skan					cmderr(EX_DATAERR, "Invalid password change date `%s'\n", arg->val);
409132718Skan				pwd->pw_change = expire;
410132718Skan			}
411132718Skan		}
412132718Skan		if ((arg = getarg(args, 'e')) != NULL) {
413132718Skan			if (*arg->val == '\0' || strcmp(arg->val, "0") == 0)
414132718Skan				pwd->pw_expire = 0;
415132718Skan			else {
416132718Skan				time_t          now = time(NULL);
417132718Skan				time_t          expire = parse_date(now, arg->val);
418132718Skan
419132718Skan				if (now == expire)
420132718Skan					cmderr(EX_DATAERR, "Invalid account expiry date `%s'\n", arg->val);
421169689Skan				pwd->pw_expire = expire;
422132718Skan			}
423132718Skan		}
424132718Skan		if ((arg = getarg(args, 's')) != NULL)
425132718Skan			pwd->pw_shell = shell_path(cnf->shelldir, cnf->shells, arg->val);
426132718Skan
427132718Skan		if (getarg(args, 'L'))
428132718Skan			pwd->pw_class = cnf->default_class;
429132718Skan
430132718Skan		if ((arg  = getarg(args, 'd')) != NULL) {
431132718Skan			if (stat(pwd->pw_dir = arg->val, &st) == -1) {
432132718Skan				if (getarg(args, 'm') == NULL && strcmp(pwd->pw_dir, "/nonexistent") != 0)
433132718Skan				  fprintf(stderr, "WARNING: home `%s' does not exist\n", pwd->pw_dir);
434132718Skan			} else if (!S_ISDIR(st.st_mode))
435132718Skan				fprintf(stderr, "WARNING: home `%s' is not a directory\n", pwd->pw_dir);
436132718Skan		}
437132718Skan
438132718Skan		if ((arg = getarg(args, 'w')) != NULL && getarg(args, 'h') == NULL)
439132718Skan			pwd->pw_passwd = pw_password(cnf, args, pwd->pw_name);
440132718Skan
441132718Skan	} else {
442132718Skan		if (a_name == NULL)	/* Required */
443132718Skan			cmderr(EX_DATAERR, "login name required\n");
444132718Skan		else if ((pwd = getpwnam(a_name->val)) != NULL)	/* Exists */
445132718Skan			cmderr(EX_DATAERR, "login name `%s' already exists\n", a_name->val);
446132718Skan
447132718Skan		/*
448132718Skan		 * Now, set up defaults for a new user
449132718Skan		 */
450132718Skan		pwd = &fakeuser;
451132718Skan		pwd->pw_name = a_name->val;
452132718Skan		pwd->pw_class = cnf->default_class ? cnf->default_class : "";
453132718Skan		pwd->pw_passwd = pw_password(cnf, args, pwd->pw_name);
454132718Skan		pwd->pw_uid = pw_uidpolicy(cnf, args);
455132718Skan		pwd->pw_gid = pw_gidpolicy(cnf, args, pwd->pw_name, (gid_t) pwd->pw_uid);
456132718Skan		pwd->pw_change = pw_pwdpolicy(cnf, args);
457132718Skan		pwd->pw_expire = pw_exppolicy(cnf, args);
458132718Skan		pwd->pw_dir = pw_homepolicy(cnf, args, pwd->pw_name);
459132718Skan		pwd->pw_shell = pw_shellpolicy(cnf, args, NULL);
460132718Skan
461132718Skan		if (pwd->pw_uid == 0 && strcmp(pwd->pw_name, "root") != 0)
462132718Skan			fprintf(stderr, "WARNING: new account `%s' has a uid of 0 (superuser access!)\n", pwd->pw_name);
463132718Skan	}
464132718Skan
465132718Skan	/*
466132718Skan	 * Shared add/edit code
467132718Skan	 */
468169689Skan	if ((arg = getarg(args, 'c')) != NULL)
469169689Skan		pwd->pw_gecos = pw_checkname((u_char *)arg->val, 1);
470169689Skan
471169689Skan	if ((arg = getarg(args, 'h')) != NULL) {
472169689Skan		if (strcmp(arg->val, "-") == 0)
473169689Skan			pwd->pw_passwd = "*";	/* No access */
474169689Skan		else {
475169689Skan			int             fd = atoi(arg->val);
476169689Skan			int             b;
477169689Skan			int             istty = isatty(fd);
478169689Skan			struct termios  t;
479169689Skan
480169689Skan			if (istty) {
481169689Skan				if (tcgetattr(fd, &t) == -1)
482169689Skan					istty = 0;
483169689Skan				else {
484169689Skan					struct termios  n = t;
485169689Skan
486169689Skan					/* Disable echo */
487169689Skan					n.c_lflag &= ~(ECHO);
488169689Skan					tcsetattr(fd, TCSANOW, &n);
489169689Skan					printf("%sassword for user %s:", (mode == M_UPDATE) ? "New p" : "P", pwd->pw_name);
490169689Skan					fflush(stdout);
491169689Skan				}
492169689Skan			}
493169689Skan			b = read(fd, line, sizeof(line) - 1);
494169689Skan			if (istty) {	/* Restore state */
495169689Skan				tcsetattr(fd, TCSANOW, &t);
496169689Skan				fputc('\n', stdout);
497169689Skan				fflush(stdout);
498169689Skan			}
499169689Skan			if (b < 0) {
500169689Skan				perror("-h file descriptor");
501169689Skan				return EX_IOERR;
502169689Skan			}
503169689Skan			line[b] = '\0';
504169689Skan			if ((p = strpbrk(line, " \t\r\n")) != NULL)
505169689Skan				*p = '\0';
506169689Skan			if (!*line)
507169689Skan				cmderr(EX_DATAERR, "empty password read on file descriptor %d\n", fd);
508169689Skan			pwd->pw_passwd = pw_pwcrypt(line);
509169689Skan		}
510169689Skan	}
511169689Skan
512169689Skan	/*
513169689Skan	 * Special case: -N only displays & exits
514169689Skan	 */
515169689Skan	if (getarg(args, 'N') != NULL)
516169689Skan		return print_user(pwd, getarg(args, 'P') != NULL);
517169689Skan
518169689Skan	r = r1 = 1;
519169689Skan	if (mode == M_ADD) {
520169689Skan		r = addpwent(pwd);
521169689Skan		if (r && cnf->nispasswd && *cnf->nispasswd=='/')
522169689Skan			r1 = addnispwent(cnf->nispasswd, pwd);
523169689Skan	} else if (mode == M_UPDATE) {
524169689Skan		r = chgpwent(a_name->val, pwd);
525169689Skan		if (r && cnf->nispasswd && *cnf->nispasswd=='/')
526169689Skan			r1 = chgnispwent(cnf->nispasswd, a_name->val, pwd);
527169689Skan	}
528169689Skan
529169689Skan	if (!r) {
530169689Skan		perror("password update");
531169689Skan		return EX_IOERR;
532169689Skan	} else if (!r1) {
533169689Skan		perror("WARNING: NIS password update");
534169689Skan		/* Keep on trucking */
535169689Skan	}
536169689Skan
537169689Skan	/*
538169689Skan	 * Ok, user is created or changed - now edit group file
539169689Skan	 */
540169689Skan
541169689Skan	if (mode == M_ADD || getarg(args, 'G') != NULL)
542169689Skan		editgroups(pwd->pw_name, cnf->groups);
543169689Skan
544169689Skan	/* pwd may have been invalidated */
545169689Skan	if ((pwd = getpwnam(a_name->val)) == NULL)
546169689Skan		cmderr(EX_NOUSER, "user '%s' disappeared during update\n", a_name->val);
547169689Skan
548169689Skan	grp = getgrgid(pwd->pw_gid);
549169689Skan	pw_log(cnf, mode, W_USER, "%s(%ld):%s(%d):%s:%s:%s",
550169689Skan	       pwd->pw_name, (long) pwd->pw_uid,
551169689Skan	    grp ? grp->gr_name : "unknown", (long) (grp ? grp->gr_gid : -1),
552169689Skan	       pwd->pw_gecos, pwd->pw_dir, pwd->pw_shell);
553169689Skan
554169689Skan	/*
555169689Skan	 * If adding, let's touch and chown the user's mail file. This is not
556169689Skan	 * strictly necessary under BSD with a 0755 maildir but it also
557169689Skan	 * doesn't hurt anything to create the empty mailfile
558169689Skan	 */
559169689Skan	if (mode == M_ADD) {
560169689Skan		FILE           *fp;
561169689Skan
562169689Skan		sprintf(line, "%s/%s", _PATH_MAILDIR, pwd->pw_name);
563169689Skan		close(open(line, O_RDWR | O_CREAT, 0600));	/* Preserve contents &
564169689Skan								 * mtime */
565169689Skan		chown(line, pwd->pw_uid, pwd->pw_gid);
566169689Skan
567169689Skan		/*
568169689Skan		 * Send mail to the new user as well, if we are asked to
569169689Skan		 */
570169689Skan		if (cnf->newmail && *cnf->newmail && (fp = fopen(cnf->newmail, "r")) != NULL) {
571169689Skan			FILE           *pfp = popen(_PATH_SENDMAIL " -t", "w");
572169689Skan
573169689Skan			if (pfp == NULL)
574169689Skan				perror("sendmail");
575169689Skan			else {
576169689Skan				fprintf(pfp, "From: root\n" "To: %s\n" "Subject: Welcome!\n\n", pwd->pw_name);
577169689Skan				while (fgets(line, sizeof(line), fp) != NULL) {
578169689Skan					/* Do substitutions? */
579169689Skan					fputs(line, pfp);
580169689Skan				}
581169689Skan				pclose(pfp);
582169689Skan				pw_log(cnf, mode, W_USER, "%s(%ld) new user mail sent",
583169689Skan				       pwd->pw_name, (long) pwd->pw_uid);
584169689Skan			}
585169689Skan			fclose(fp);
586169689Skan		}
587169689Skan	}
588169689Skan	/*
589169689Skan	 * Finally, let's create and populate the user's home directory. Note
590169689Skan	 * that this also `works' for editing users if -m is used, but
591169689Skan	 * existing files will *not* be overwritten.
592169689Skan	 */
593169689Skan	if (getarg(args, 'm') != NULL && pwd->pw_dir && *pwd->pw_dir == '/' && pwd->pw_dir[1]) {
594169689Skan		copymkdir(pwd->pw_dir, cnf->dotdir, 0755, pwd->pw_uid, pwd->pw_gid);
595169689Skan		pw_log(cnf, mode, W_USER, "%s(%ld) home %s made",
596169689Skan		       pwd->pw_name, (long) pwd->pw_uid, pwd->pw_dir);
597169689Skan	}
598169689Skan	return EXIT_SUCCESS;
599169689Skan}
600169689Skan
601169689Skan
602169689Skanstatic          uid_t
603169689Skanpw_uidpolicy(struct userconf * cnf, struct cargs * args)
604169689Skan{
605169689Skan	struct passwd  *pwd;
606169689Skan	uid_t           uid = (uid_t) - 1;
607169689Skan	struct carg    *a_uid = getarg(args, 'u');
608169689Skan
609169689Skan	/*
610169689Skan	 * Check the given uid, if any
611169689Skan	 */
612169689Skan	if (a_uid != NULL) {
613169689Skan		uid = (uid_t) atol(a_uid->val);
614169689Skan
615169689Skan		if ((pwd = getpwuid(uid)) != NULL && getarg(args, 'o') == NULL)
616169689Skan			cmderr(EX_DATAERR, "uid `%ld' has already been allocated\n", (long) pwd->pw_uid);
617169689Skan	} else {
618169689Skan		struct bitmap   bm;
619169689Skan
620169689Skan		/*
621169689Skan		 * We need to allocate the next available uid under one of
622169689Skan		 * two policies a) Grab the first unused uid b) Grab the
623169689Skan		 * highest possible unused uid
624169689Skan		 */
625169689Skan		if (cnf->min_uid >= cnf->max_uid) {	/* Sanity
626169689Skan							 * claus^H^H^H^Hheck */
627169689Skan			cnf->min_uid = 1000;
628169689Skan			cnf->max_uid = 32000;
629169689Skan		}
630169689Skan		bm = bm_alloc(cnf->max_uid - cnf->min_uid + 1);
631169689Skan
632169689Skan		/*
633169689Skan		 * Now, let's fill the bitmap from the password file
634169689Skan		 */
635169689Skan		setpwent();
636169689Skan		while ((pwd = getpwent()) != NULL)
637169689Skan			if (pwd->pw_uid >= (int) cnf->min_uid && pwd->pw_uid <= (int) cnf->max_uid)
638169689Skan				bm_setbit(&bm, pwd->pw_uid - cnf->min_uid);
639169689Skan		endpwent();
640169689Skan
641169689Skan		/*
642169689Skan		 * Then apply the policy, with fallback to reuse if necessary
643169689Skan		 */
644169689Skan		if (cnf->reuse_uids || (uid = (uid_t) (bm_lastset(&bm) + cnf->min_uid + 1)) > cnf->max_uid)
645169689Skan			uid = (uid_t) (bm_firstunset(&bm) + cnf->min_uid);
646169689Skan
647169689Skan		/*
648169689Skan		 * Another sanity check
649169689Skan		 */
650169689Skan		if (uid < cnf->min_uid || uid > cnf->max_uid)
651169689Skan			cmderr(EX_SOFTWARE, "unable to allocate a new uid - range fully used\n");
652169689Skan		bm_dealloc(&bm);
653169689Skan	}
654169689Skan	return uid;
655169689Skan}
656169689Skan
657169689Skan
658169689Skanstatic          uid_t
659169689Skanpw_gidpolicy(struct userconf * cnf, struct cargs * args, char *nam, gid_t prefer)
660169689Skan{
661169689Skan	struct group   *grp;
662169689Skan	gid_t           gid = (uid_t) - 1;
663169689Skan	struct carg    *a_gid = getarg(args, 'g');
664169689Skan
665169689Skan	/*
666169689Skan	 * If no arg given, see if default can help out
667169689Skan	 */
668169689Skan	if (a_gid == NULL && cnf->default_group && *cnf->default_group)
669169689Skan		a_gid = addarg(args, 'g', cnf->default_group);
670169689Skan
671169689Skan	/*
672169689Skan	 * Check the given gid, if any
673169689Skan	 */
674169689Skan	setgrent();
675169689Skan	if (a_gid != NULL) {
676169689Skan		if ((grp = getgrnam(a_gid->val)) == NULL) {
677169689Skan			gid = (gid_t) atol(a_gid->val);
678169689Skan			if ((gid == 0 && !isdigit(*a_gid->val)) || (grp = getgrgid(gid)) == NULL)
679169689Skan				cmderr(EX_NOUSER, "group `%s' is not defined\n", a_gid->val);
680169689Skan		}
681169689Skan		gid = grp->gr_gid;
682169689Skan	} else if ((grp = getgrnam(nam)) != NULL && grp->gr_mem[0] == NULL) {
683169689Skan		gid = grp->gr_gid;  /* Already created? Use it anyway... */
684169689Skan	} else {
685169689Skan		struct cargs    grpargs;
686169689Skan		char            tmp[32];
687169689Skan
688169689Skan		LIST_INIT(&grpargs);
689169689Skan		addarg(&grpargs, 'n', nam);
690169689Skan
691169689Skan		/*
692169689Skan		 * We need to auto-create a group with the user's name. We
693132718Skan		 * can send all the appropriate output to our sister routine
694132718Skan		 * bit first see if we can create a group with gid==uid so we
695132718Skan		 * can keep the user and group ids in sync. We purposely do
696132718Skan		 * NOT check the gid range if we can force the sync. If the
697132718Skan		 * user's name dups an existing group, then the group add
698132718Skan		 * function will happily handle that case for us and exit.
699132718Skan		 */
700132718Skan		if (getgrgid(prefer) == NULL) {
701132718Skan			sprintf(tmp, "%lu", (unsigned long) prefer);
702132718Skan			addarg(&grpargs, 'g', tmp);
703132718Skan		}
704132718Skan		if (getarg(args, 'N'))
705132718Skan		{
706132718Skan			addarg(&grpargs, 'N', NULL);
707132718Skan			addarg(&grpargs, 'q', NULL);
708132718Skan			gid = pw_group(cnf, M_NEXT, &grpargs);
709132718Skan		}
710132718Skan		else
711132718Skan		{
712132718Skan			pw_group(cnf, M_ADD, &grpargs);
713132718Skan			if ((grp = getgrnam(nam)) != NULL)
714132718Skan				gid = grp->gr_gid;
715132718Skan		}
716132718Skan		a_gid = grpargs.lh_first;
717132718Skan		while (a_gid != NULL) {
718132718Skan			struct carg    *t = a_gid->list.le_next;
719132718Skan			LIST_REMOVE(a_gid, list);
720132718Skan			a_gid = t;
721132718Skan		}
722132718Skan	}
723132718Skan	endgrent();
724132718Skan	return gid;
725132718Skan}
726132718Skan
727132718Skan
728132718Skanstatic          time_t
729132718Skanpw_pwdpolicy(struct userconf * cnf, struct cargs * args)
730132718Skan{
731132718Skan	time_t          result = 0;
732132718Skan	time_t          now = time(NULL);
733132718Skan	struct carg    *arg = getarg(args, 'e');
734132718Skan
735132718Skan	if (arg != NULL) {
736		if ((result = parse_date(now, arg->val)) == now)
737			cmderr(EX_DATAERR, "invalid date/time `%s'\n", arg->val);
738	} else if (cnf->password_days > 0)
739		result = now + ((long) cnf->password_days * 86400L);
740	return result;
741}
742
743
744static          time_t
745pw_exppolicy(struct userconf * cnf, struct cargs * args)
746{
747	time_t          result = 0;
748	time_t          now = time(NULL);
749	struct carg    *arg = getarg(args, 'e');
750
751	if (arg != NULL) {
752		if ((result = parse_date(now, arg->val)) == now)
753			cmderr(EX_DATAERR, "invalid date/time `%s'\n", arg->val);
754	} else if (cnf->expire_days > 0)
755		result = now + ((long) cnf->expire_days * 86400L);
756	return result;
757}
758
759
760static char    *
761pw_homepolicy(struct userconf * cnf, struct cargs * args, char const * user)
762{
763	struct carg    *arg = getarg(args, 'd');
764
765	if (arg)
766		return arg->val;
767	else {
768		static char     home[128];
769
770		if (cnf->home == NULL || *cnf->home == '\0')
771			cmderr(EX_CONFIG, "no base home directory set\n");
772		sprintf(home, "%s/%s", cnf->home, user);
773		return home;
774	}
775}
776
777static char    *
778shell_path(char const * path, char *shells[], char *sh)
779{
780	if (sh != NULL && (*sh == '/' || *sh == '\0'))
781		return sh;	/* specified full path or forced none */
782	else {
783		char           *p;
784		char            paths[_UC_MAXLINE];
785
786		/*
787		 * We need to search paths
788		 */
789		strncpy(paths, path, sizeof paths);
790		paths[sizeof paths - 1] = '\0';
791		for (p = strtok(paths, ": \t\r\n"); p != NULL; p = strtok(NULL, ": \t\r\n")) {
792			int             i;
793			static char     shellpath[256];
794
795			if (sh != NULL) {
796				sprintf(shellpath, "%s/%s", p, sh);
797				if (access(shellpath, X_OK) == 0)
798					return shellpath;
799			} else
800				for (i = 0; i < _UC_MAXSHELLS && shells[i] != NULL; i++) {
801					sprintf(shellpath, "%s/%s", p, shells[i]);
802					if (access(shellpath, X_OK) == 0)
803						return shellpath;
804				}
805		}
806		if (sh == NULL)
807			cmderr(EX_OSFILE, "can't find shell `%s' in shell paths\n", sh);
808		cmderr(EX_CONFIG, "no default shell available or defined\n");
809		return NULL;
810	}
811}
812
813
814static char    *
815pw_shellpolicy(struct userconf * cnf, struct cargs * args, char *newshell)
816{
817	char           *sh = newshell;
818	struct carg    *arg = getarg(args, 's');
819
820	if (newshell == NULL && arg != NULL)
821		sh = arg->val;
822	return shell_path(cnf->shelldir, cnf->shells, sh ? sh : cnf->shell_default);
823}
824
825static char const chars[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.";
826
827char           *
828pw_pwcrypt(char *password)
829{
830	int             i;
831	char            salt[12];
832
833	static char     buf[256];
834
835	/*
836	 * Calculate a salt value
837	 */
838	srandom((unsigned) (time(NULL) ^ getpid()));
839	for (i = 0; i < 8; i++)
840		salt[i] = chars[random() % 63];
841	salt[i] = '\0';
842
843	return strcpy(buf, crypt(password, salt));
844}
845
846#if defined(__FreeBSD__)
847
848#if defined(USE_MD5RAND)
849u_char *
850pw_getrand(u_char *buf, int len)	/* cryptographically secure rng */
851{
852	int i;
853	for (i=0;i<len;i+=16) {
854		u_char ubuf[16];
855
856		MD5_CTX md5_ctx;
857		struct timeval tv, tvo;
858		struct rusage ru;
859		int n=0;
860		int t;
861
862		MD5Init (&md5_ctx);
863		t=getpid();
864		MD5Update (&md5_ctx, (u_char*)&t, sizeof t);
865		t=getppid();
866		MD5Update (&md5_ctx, (u_char*)&t, sizeof t);
867		gettimeofday (&tvo, NULL);
868		do {
869			getrusage (RUSAGE_SELF, &ru);
870			MD5Update (&md5_ctx, (u_char*)&ru, sizeof ru);
871			gettimeofday (&tv, NULL);
872			MD5Update (&md5_ctx, (u_char*)&tv, sizeof tv);
873		} while (n++<20 || tv.tv_usec-tvo.tv_usec<100*1000);
874		MD5Final (ubuf, &md5_ctx);
875		memcpy(buf+i, ubuf, MIN(16, len-n));
876	}
877	return buf;
878}
879
880#else	/* Use random device (preferred) */
881
882static u_char *
883pw_getrand(u_char *buf, int len)
884{
885	int		fd;
886	fd = open("/dev/urandom", O_RDONLY);
887	if (fd==-1)
888		cmderr(EX_OSFILE, "can't open /dev/urandom: %s\n", strerror(errno));
889	else if (read(fd, buf, len)!=len)
890		cmderr(EX_IOERR, "read error on /dev/urandom\n");
891	close(fd);
892	return buf;
893}
894
895#endif
896
897#else	/* Portable version */
898
899static u_char *
900pw_getrand(u_char *buf, int len)
901{
902	int i;
903
904	for (i = 0; i < len; i++) {
905		unsigned val = random();
906		/* Use all bits in the random value */
907		buf[i]=(u_char)((val >> 24) ^ (val >> 16) ^ (val >> 8) ^ val);
908	}
909	return buf;
910}
911
912#endif
913
914static char    *
915pw_password(struct userconf * cnf, struct cargs * args, char const * user)
916{
917	int             i, l;
918	char            pwbuf[32];
919	u_char		rndbuf[sizeof pwbuf];
920
921	switch (cnf->default_password) {
922	case -1:		/* Random password */
923		srandom((unsigned) (time(NULL) ^ getpid()));
924		l = (random() % 8 + 8);	/* 8 - 16 chars */
925		pw_getrand(rndbuf, l);
926		for (i = 0; i < l; i++)
927			pwbuf[i] = chars[rndbuf[i] % sizeof(chars)];
928		pwbuf[i] = '\0';
929
930		/*
931		 * We give this information back to the user
932		 */
933		if (getarg(args, 'h') == NULL && getarg(args, 'N') == NULL) {
934			if (isatty(1))
935				printf("Password for '%s' is: ", user);
936			printf("%s\n", pwbuf);
937			fflush(stdout);
938		}
939		break;
940
941	case -2:		/* No password at all! */
942		return "";
943
944	case 0:		/* No login - default */
945	default:
946		return "*";
947
948	case 1:		/* user's name */
949		strncpy(pwbuf, user, sizeof pwbuf);
950		pwbuf[sizeof pwbuf - 1] = '\0';
951		break;
952	}
953	return pw_pwcrypt(pwbuf);
954}
955
956
957static int
958print_user(struct passwd * pwd, int pretty)
959{
960	if (!pretty) {
961		char            buf[_UC_MAXLINE];
962
963		fmtpwent(buf, pwd);
964		fputs(buf, stdout);
965	} else {
966		int		j;
967		char           *p;
968		struct group   *grp = getgrgid(pwd->pw_gid);
969		char            uname[60] = "User &", office[60] = "[None]",
970		                wphone[60] = "[None]", hphone[60] = "[None]";
971		char		acexpire[32] = "[None]", pwexpire[32] = "[None]";
972		struct tm *    tptr;
973
974		if ((p = strtok(pwd->pw_gecos, ",")) != NULL) {
975			strncpy(uname, p, sizeof uname);
976			uname[sizeof uname - 1] = '\0';
977			if ((p = strtok(NULL, ",")) != NULL) {
978				strncpy(office, p, sizeof office);
979				office[sizeof office - 1] = '\0';
980				if ((p = strtok(NULL, ",")) != NULL) {
981					strncpy(wphone, p, sizeof wphone);
982					wphone[sizeof wphone - 1] = '\0';
983					if ((p = strtok(NULL, "")) != NULL) {
984						strncpy(hphone, p, sizeof hphone);
985						hphone[sizeof hphone - 1] = '\0';
986					}
987				}
988			}
989		}
990		/*
991		 * Handle '&' in gecos field
992		 */
993		if ((p = strchr(uname, '&')) != NULL) {
994			int             l = strlen(pwd->pw_name);
995			int             m = strlen(p);
996
997			memmove(p + l, p + 1, m);
998			memmove(p, pwd->pw_name, l);
999			*p = (char) toupper(*p);
1000		}
1001		if (pwd->pw_expire > (time_t)0 && (tptr = localtime(&pwd->pw_expire)) != NULL)
1002		  strftime(acexpire, sizeof acexpire, "%c", tptr);
1003		if (pwd->pw_change > (time_t)9 && (tptr = localtime(&pwd->pw_change)) != NULL)
1004		  strftime(pwexpire, sizeof pwexpire, "%c", tptr);
1005		printf("Login Name: %-15s   #%-12ld Group: %-15s   #%ld\n"
1006		       " Full Name: %s\n"
1007		       "      Home: %-26.26s      Class: %s\n"
1008		       "     Shell: %-26.26s     Office: %s\n"
1009		       "Work Phone: %-26.26s Home Phone: %s\n"
1010		       "Acc Expire: %-26.26s Pwd Expire: %s\n",
1011		       pwd->pw_name, (long) pwd->pw_uid,
1012		       grp ? grp->gr_name : "(invalid)", (long) pwd->pw_gid,
1013		       uname, pwd->pw_dir, pwd->pw_class,
1014		       pwd->pw_shell, office, wphone, hphone,
1015		       acexpire, pwexpire);
1016	        setgrent();
1017		j = 0;
1018		while ((grp=getgrent()) != NULL)
1019		{
1020			int     i = 0;
1021			while (grp->gr_mem[i] != NULL)
1022			{
1023				if (strcmp(grp->gr_mem[i], pwd->pw_name)==0)
1024				{
1025					printf(j++ == 0 ? "    Groups: %s" : ",%s", grp->gr_name);
1026					break;
1027				}
1028				++i;
1029			}
1030		}
1031		endgrent();
1032		printf("%s\n", j ? "\n" : "");
1033	}
1034	return EXIT_SUCCESS;
1035}
1036
1037char    *
1038pw_checkname(u_char *name, int gecos)
1039{
1040	int             l = 0;
1041	char const     *notch = gecos ? ":!@" : " ,\t:+&#%$^()!@~*?<>=|\\/\"";
1042
1043	while (name[l]) {
1044		if (strchr(notch, name[l]) != NULL || name[l] < ' ' || name[l] == 127 ||
1045			(!gecos && l==0 && name[l] == '-') ||	/* leading '-' */
1046			(!gecos && name[l] & 0x80))	/* 8-bit */
1047			cmderr(EX_DATAERR, (name[l] >= ' ' && name[l] < 127)
1048					    ? "invalid character `%c' in field\n"
1049					    : "invalid character 0x%02x in field\n",
1050					    name[l]);
1051		++l;
1052	}
1053	if (!gecos && l > LOGNAMESIZE)
1054		cmderr(EX_DATAERR, "name too long `%s'\n", name);
1055	return (char *)name;
1056}
1057
1058
1059static void
1060rmat(uid_t uid)
1061{
1062	DIR            *d = opendir("/var/at/jobs");
1063
1064	if (d != NULL) {
1065		struct dirent  *e;
1066
1067		while ((e = readdir(d)) != NULL) {
1068			struct stat     st;
1069
1070			if (strncmp(e->d_name, ".lock", 5) != 0 &&
1071			    stat(e->d_name, &st) == 0 &&
1072			    !S_ISDIR(st.st_mode) &&
1073			    st.st_uid == uid) {
1074				char            tmp[MAXPATHLEN];
1075
1076				sprintf(tmp, "/usr/bin/atrm %s", e->d_name);
1077				system(tmp);
1078			}
1079		}
1080		closedir(d);
1081	}
1082}
1083
1084static void
1085rmskey(char const * name)
1086{
1087	static const char etcskey[] = "/etc/skeykeys";
1088	FILE   *fp = fopen(etcskey, "r+");
1089
1090	if (fp != NULL) {
1091		char	tmp[1024];
1092		off_t	atofs = 0;
1093		int	length = strlen(name);
1094
1095		while (fgets(tmp, sizeof tmp, fp) != NULL) {
1096			if (strncmp(name, tmp, length) == 0 && tmp[length]==' ') {
1097				if (fseek(fp, atofs, SEEK_SET) == 0) {
1098					fwrite("#", 1, 1, fp);	/* Comment username out */
1099				}
1100				break;
1101			}
1102			atofs = ftell(fp);
1103		}
1104		/*
1105		 * If we got an error of any sort, don't update!
1106		 */
1107		fclose(fp);
1108	}
1109}
1110
1111