xref: /netbsd-src/usr.sbin/user/user.c (revision 5e4c038a45edbc7d63b7c2daa76e29f88b64a4e3)
1 /* $NetBSD: user.c,v 1.52 2002/06/01 06:28:06 grant Exp $ */
2 
3 /*
4  * Copyright (c) 1999 Alistair G. Crooks.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. All advertising materials mentioning features or use of this software
15  *    must display the following acknowledgement:
16  *	This product includes software developed by Alistair G. Crooks.
17  * 4. The name of the author may not be used to endorse or promote
18  *    products derived from this software without specific prior written
19  *    permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
22  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
25  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 #include <sys/cdefs.h>
34 
35 #ifndef lint
36 __COPYRIGHT("@(#) Copyright (c) 1999 \
37 	        The NetBSD Foundation, Inc.  All rights reserved.");
38 __RCSID("$NetBSD: user.c,v 1.52 2002/06/01 06:28:06 grant Exp $");
39 #endif
40 
41 #include <sys/types.h>
42 #include <sys/param.h>
43 #include <sys/stat.h>
44 
45 #include <ctype.h>
46 #include <dirent.h>
47 #include <err.h>
48 #include <fcntl.h>
49 #include <grp.h>
50 #include <paths.h>
51 #include <pwd.h>
52 #include <stdarg.h>
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <string.h>
56 #include <time.h>
57 #include <unistd.h>
58 #include <util.h>
59 
60 #include "defs.h"
61 #include "usermgmt.h"
62 
63 
64 /* this struct describes a uid range */
65 typedef struct range_t {
66 	int	r_from;		/* low uid */
67 	int	r_to;		/* high uid */
68 } range_t;
69 
70 /* this struct encapsulates the user information */
71 typedef struct user_t {
72 	int		u_flags;		/* see below */
73 	int		u_uid;			/* uid of user */
74 	char		*u_password;		/* encrypted password */
75 	char		*u_comment;		/* comment field */
76 	char		*u_home;		/* home directory */
77 	char		*u_primgrp;		/* primary group */
78 	int		u_groupc;		/* # of secondary groups */
79 	char		*u_groupv[NGROUPS_MAX];	/* secondary groups */
80 	char		*u_shell;		/* user's shell */
81 	char		*u_basedir;		/* base directory for home */
82 	char		*u_expire;		/* when password will expire */
83 	char		*u_inactive;		/* when account will expire */
84 	char		*u_skeldir;		/* directory for startup files */
85 	char		*u_class;		/* login class */
86 	unsigned	u_rsize;		/* size of range array */
87 	unsigned	u_rc;			/* # of ranges */
88 	range_t		*u_rv;			/* the ranges */
89 	unsigned	u_defrc;		/* # of ranges in defaults */
90 	int		u_preserve;		/* preserve uids on deletion */
91 } user_t;
92 
93 /* flags for which fields of the user_t replace the passwd entry */
94 enum {
95 	F_COMMENT	= 0x0001,
96 	F_DUPUID  	= 0x0002,
97 	F_EXPIRE	= 0x0004,
98 	F_GROUP		= 0x0008,
99 	F_HOMEDIR	= 0x0010,
100 	F_MKDIR		= 0x0020,
101 	F_INACTIVE	= 0x0040,
102 	F_PASSWORD	= 0x0080,
103 	F_SECGROUP	= 0x0100,
104 	F_SHELL 	= 0x0200,
105 	F_UID		= 0x0400,
106 	F_USERNAME	= 0x0800,
107 	F_CLASS		= 0x1000
108 };
109 
110 #define CONFFILE	"/etc/usermgmt.conf"
111 
112 #ifndef DEF_GROUP
113 #define DEF_GROUP	"users"
114 #endif
115 
116 #ifndef DEF_BASEDIR
117 #define DEF_BASEDIR	"/home"
118 #endif
119 
120 #ifndef DEF_SKELDIR
121 #define DEF_SKELDIR	"/etc/skel"
122 #endif
123 
124 #ifndef DEF_SHELL
125 #define DEF_SHELL	_PATH_CSHELL
126 #endif
127 
128 #ifndef DEF_COMMENT
129 #define DEF_COMMENT	""
130 #endif
131 
132 #ifndef DEF_LOWUID
133 #define DEF_LOWUID	1000
134 #endif
135 
136 #ifndef DEF_HIGHUID
137 #define DEF_HIGHUID	60000
138 #endif
139 
140 #ifndef DEF_INACTIVE
141 #define DEF_INACTIVE	0
142 #endif
143 
144 #ifndef DEF_EXPIRE
145 #define DEF_EXPIRE	NULL
146 #endif
147 
148 #ifndef DEF_CLASS
149 #define DEF_CLASS	""
150 #endif
151 
152 #ifndef WAITSECS
153 #define WAITSECS	10
154 #endif
155 
156 #ifndef NOBODY_UID
157 #define NOBODY_UID	32767
158 #endif
159 
160 /* some useful constants */
161 enum {
162 	MaxShellNameLen = 256,
163 	MaxFileNameLen = MAXPATHLEN,
164 	MaxUserNameLen = 32,
165 	MaxFieldNameLen = 32,
166 	MaxCommandLen = 2048,
167 	MaxEntryLen = 2048,
168 	PasswordLength = 13,
169 
170 	LowGid = DEF_LOWUID,
171 	HighGid = DEF_HIGHUID
172 };
173 
174 /* Full paths of programs used here */
175 #define CHMOD		"/bin/chmod"
176 #define CHOWN		"/usr/sbin/chown"
177 #define MKDIR		"/bin/mkdir"
178 #define MV		"/bin/mv"
179 #define NOLOGIN		"/sbin/nologin"
180 #define PAX		"/bin/pax"
181 #define RM		"/bin/rm"
182 
183 #define UNSET_INACTIVE	"Null (unset)"
184 #define UNSET_EXPIRY	"Null (unset)"
185 
186 static int asystem(const char *fmt, ...)
187 	__attribute__((__format__(__printf__, 1, 2)));
188 
189 static int	verbose;
190 
191 /* if *cpp is non-null, free it, then assign `n' chars of `s' to it */
192 static void
193 memsave(char **cpp, char *s, size_t n)
194 {
195 	if (*cpp != NULL) {
196 		FREE(*cpp);
197 	}
198 	NEWARRAY(char, *cpp, n + 1, exit(1));
199 	(void) memcpy(*cpp, s, n);
200 	(*cpp)[n] = '\0';
201 }
202 
203 /* a replacement for system(3) */
204 static int
205 asystem(const char *fmt, ...)
206 {
207 	va_list	vp;
208 	char	buf[MaxCommandLen];
209 	int	ret;
210 
211 	va_start(vp, fmt);
212 	(void) vsnprintf(buf, sizeof(buf), fmt, vp);
213 	va_end(vp);
214 	if (verbose) {
215 		(void) printf("Command: %s\n", buf);
216 	}
217 	if ((ret = system(buf)) != 0) {
218 		warnx("[Warning] can't system `%s'", buf);
219 	}
220 	return ret;
221 }
222 
223 /* remove a users home directory, returning 1 for success (ie, no problems encountered) */
224 static int
225 removehomedir(const char *user, int uid, const char *dir)
226 {
227 	struct stat st;
228 
229 	/* userid not root? */
230 	if (uid == 0) {
231 		warnx("Not deleting home directory `%s'; userid is 0", dir);
232 		return 0;
233 	}
234 
235 	/* directory exists (and is a directory!) */
236 	if (stat(dir, &st) < 0) {
237 		warnx("Home directory `%s' doesn't exist", dir);
238 		return 0;
239 	}
240 	if (!S_ISDIR(st.st_mode)) {
241 		warnx("Home directory `%s' is not a directory", dir);
242 		return 0;
243 	}
244 
245 	/* userid matches directory owner? */
246 	if (st.st_uid != uid) {
247 		warnx("User `%s' doesn't own directory `%s', not removed\n", user, dir);
248 		return 0;
249 	}
250 
251 	(void) seteuid(uid);
252 	/* we add the "|| true" to keep asystem() quiet if there is a non-zero exit status. */
253 	(void) asystem("%s -rf %s > /dev/null 2>&1 || true", RM, dir);
254 	(void) seteuid(0);
255 	if (rmdir(dir) < 0) {
256 		warnx("Unable to remove all files in `%s'\n", dir);
257 		return 0;
258 	}
259 	return 1;
260 }
261 
262 #define NetBSD_1_4_K	104110000
263 
264 #if defined(__NetBSD_Version__) && (__NetBSD_Version__ < NetBSD_1_4_K)
265 /* bounds checking strncpy */
266 static int
267 strlcpy(char *to, char *from, size_t tosize)
268 {
269 	size_t	n;
270 	int	fromsize;
271 
272 	fromsize = strlen(from);
273 	n = MIN(tosize - 1, fromsize);
274 	(void) memcpy(to, from, n);
275 	to[n] = '\0';
276 	return fromsize;
277 }
278 #endif /* NetBSD < 1.4K */
279 
280 /*
281  * Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com>
282  * All rights reserved.
283  *
284  * Redistribution and use in source and binary forms, with or without
285  * modification, are permitted provided that the following conditions
286  * are met:
287  * 1. Redistributions of source code must retain the above copyright
288  *    notice, this list of conditions and the following disclaimer.
289  * 2. Redistributions in binary form must reproduce the above copyright
290  *    notice, this list of conditions and the following disclaimer in the
291  *    documentation and/or other materials provided with the distribution.
292  * 3. The name of the author may not be used to endorse or promote products
293  *    derived from this software without specific prior written permission.
294  *
295  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
296  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
297  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
298  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
299  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
300  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
301  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
302  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
303  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
304  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
305  */
306 
307 /*
308  * research has shown that NetBSD 1.3H was the first version of -current
309  * with asprintf in libc. agc
310  */
311 #define NetBSD_1_3_H	103080000
312 
313 #if defined(__NetBSD_Version__) && (__NetBSD_Version__ < NetBSD_1_3_H)
314 
315 int
316 asprintf(char **str, char const *fmt, ...)
317 {
318 	int ret;
319 	va_list ap;
320 	FILE f;
321 	unsigned char *_base;
322 
323 	f._flags = __SWR | __SSTR | __SALC;
324 	f._bf._base = f._p = (unsigned char *)malloc(128);
325 	if (f._bf._base == NULL)
326 		goto err;
327 	f._bf._size = f._w = 127;		/* Leave room for the NUL */
328 	va_start(ap, fmt);
329 	ret = vfprintf(&f, fmt, ap);
330 	va_end(ap);
331 	if (ret == -1)
332 		goto err;
333 	*f._p = '\0';
334 	_base = realloc(f._bf._base, (size_t)(ret + 1));
335 	if (_base == NULL)
336 		goto err;
337 	*str = (char *)_base;
338 	return (ret);
339 
340 err:
341 	if (f._bf._base)
342 		free(f._bf._base);
343 	*str = NULL;
344 	return (-1);
345 }
346 #endif /* NetBSD < 1.3H */
347 
348 /* return 1 if all of `s' is numeric */
349 static int
350 is_number(char *s)
351 {
352 	for ( ; *s ; s++) {
353 		if (!isdigit(*s)) {
354 			return 0;
355 		}
356 	}
357 	return 1;
358 }
359 
360 /*
361  * check that the effective uid is 0 - called from funcs which will
362  * modify data and config files.
363  */
364 static void
365 checkeuid(void)
366 {
367 	if (geteuid() != 0) {
368 		errx(EXIT_FAILURE, "Program must be run as root");
369 	}
370 }
371 
372 /* copy any dot files into the user's home directory */
373 static int
374 copydotfiles(char *skeldir, int uid, int gid, char *dir)
375 {
376 	struct dirent	*dp;
377 	DIR		*dirp;
378 	int		n;
379 
380 	if ((dirp = opendir(skeldir)) == NULL) {
381 		warn("can't open source . files dir `%s'", skeldir);
382 		return 0;
383 	}
384 	for (n = 0; (dp = readdir(dirp)) != NULL && n == 0 ; ) {
385 		if (strcmp(dp->d_name, ".") == 0 ||
386 		    strcmp(dp->d_name, "..") == 0) {
387 			continue;
388 		}
389 		n = 1;
390 	}
391 	(void) closedir(dirp);
392 	if (n == 0) {
393 		warnx("No \"dot\" initialisation files found");
394 	} else {
395 		(void) asystem("cd %s && %s -rw -pe %s . %s",
396 				skeldir, PAX, (verbose) ? "-v" : "", dir);
397 	}
398 	(void) asystem("%s -R -h %d:%d %s", CHOWN, uid, gid, dir);
399 	(void) asystem("%s -R u+w %s", CHMOD, dir);
400 	return n;
401 }
402 
403 /* create a group entry with gid `gid' */
404 static int
405 creategid(char *group, int gid, char *name)
406 {
407 	struct stat	st;
408 	FILE		*from;
409 	FILE		*to;
410 	char		buf[MaxEntryLen];
411 	char		f[MaxFileNameLen];
412 	int		fd;
413 	int		cc;
414 
415 	if (getgrnam(group) != NULL) {
416 		warnx("group `%s' already exists", group);
417 		return 0;
418 	}
419 	if ((from = fopen(_PATH_GROUP, "r")) == NULL) {
420 		warn("can't create gid for `%s': can't open `%s'", name, _PATH_GROUP);
421 		return 0;
422 	}
423 	if (flock(fileno(from), LOCK_EX | LOCK_NB) < 0) {
424 		warn("can't lock `%s'", _PATH_GROUP);
425 	}
426 	(void) fstat(fileno(from), &st);
427 	(void) snprintf(f, sizeof(f), "%s.XXXXXX", _PATH_GROUP);
428 	if ((fd = mkstemp(f)) < 0) {
429 		(void) fclose(from);
430 		warn("can't create gid: mkstemp failed");
431 		return 0;
432 	}
433 	if ((to = fdopen(fd, "w")) == NULL) {
434 		(void) fclose(from);
435 		(void) close(fd);
436 		(void) unlink(f);
437 		warn("can't create gid: fdopen `%s' failed", f);
438 		return 0;
439 	}
440 	while ((cc = fread(buf, sizeof(char), sizeof(buf), from)) > 0) {
441 		if (fwrite(buf, sizeof(char), (unsigned) cc, to) != cc) {
442 			(void) fclose(from);
443 			(void) close(fd);
444 			(void) unlink(f);
445 			warn("can't create gid: short write to `%s'", f);
446 			return 0;
447 		}
448 	}
449 	(void) fprintf(to, "%s:*:%d:%s\n", group, gid, name);
450 	(void) fclose(from);
451 	(void) fclose(to);
452 	if (rename(f, _PATH_GROUP) < 0) {
453 		(void) unlink(f);
454 		warn("can't create gid: can't rename `%s' to `%s'", f, _PATH_GROUP);
455 		return 0;
456 	}
457 	(void) chmod(_PATH_GROUP, st.st_mode & 07777);
458 	return 1;
459 }
460 
461 /* modify the group entry with name `group' to be newent */
462 static int
463 modify_gid(char *group, char *newent)
464 {
465 	struct stat	st;
466 	FILE		*from;
467 	FILE		*to;
468 	char		buf[MaxEntryLen];
469 	char		f[MaxFileNameLen];
470 	char		*colon;
471 	int		groupc;
472 	int		entc;
473 	int		fd;
474 	int		cc;
475 
476 	if ((from = fopen(_PATH_GROUP, "r")) == NULL) {
477 		warn("can't create gid for `%s': can't open `%s'", group, _PATH_GROUP);
478 		return 0;
479 	}
480 	if (flock(fileno(from), LOCK_EX | LOCK_NB) < 0) {
481 		warn("can't lock `%s'", _PATH_GROUP);
482 	}
483 	(void) fstat(fileno(from), &st);
484 	(void) snprintf(f, sizeof(f), "%s.XXXXXX", _PATH_GROUP);
485 	if ((fd = mkstemp(f)) < 0) {
486 		(void) fclose(from);
487 		warn("can't create gid: mkstemp failed");
488 		return 0;
489 	}
490 	if ((to = fdopen(fd, "w")) == NULL) {
491 		(void) fclose(from);
492 		(void) close(fd);
493 		(void) unlink(f);
494 		warn("can't create gid: fdopen `%s' failed", f);
495 		return 0;
496 	}
497 	groupc = strlen(group);
498 	while (fgets(buf, sizeof(buf), from) != NULL) {
499 		cc = strlen(buf);
500 		if ((colon = strchr(buf, ':')) == NULL) {
501 			warn("badly formed entry `%s'", buf);
502 			continue;
503 		}
504 		entc = (int)(colon - buf);
505 		if (entc == groupc && strncmp(group, buf, (unsigned) entc) == 0) {
506 			if (newent == NULL) {
507 				continue;
508 			} else {
509 				cc = strlen(newent);
510 				(void) strlcpy(buf, newent, sizeof(buf));
511 			}
512 		}
513 		if (fwrite(buf, sizeof(char), (unsigned) cc, to) != cc) {
514 			(void) fclose(from);
515 			(void) close(fd);
516 			(void) unlink(f);
517 			warn("can't create gid: short write to `%s'", f);
518 			return 0;
519 		}
520 	}
521 	(void) fclose(from);
522 	(void) fclose(to);
523 	if (rename(f, _PATH_GROUP) < 0) {
524 		(void) unlink(f);
525 		warn("can't create gid: can't rename `%s' to `%s'", f, _PATH_GROUP);
526 		return 0;
527 	}
528 	(void) chmod(_PATH_GROUP, st.st_mode & 07777);
529 	return 1;
530 }
531 
532 /* modify the group entries for all `groups', by adding `user' */
533 static int
534 append_group(char *user, int ngroups, char **groups)
535 {
536 	struct group	*grp;
537 	struct stat	st;
538 	FILE		*from;
539 	FILE		*to;
540 	char		buf[MaxEntryLen];
541 	char		f[MaxFileNameLen];
542 	char		*colon;
543 	int		groupc;
544 	int		entc;
545 	int		fd;
546 	int		nc;
547 	int		cc;
548 	int		i;
549 	int		j;
550 
551 	for (i = 0 ; i < ngroups ; i++) {
552 		if ((grp = getgrnam(groups[i])) == NULL) {
553 			warnx("can't append group `%s' for user `%s'", groups[i], user);
554 		} else {
555 			for (j = 0 ; grp->gr_mem[j] ; j++) {
556 				if (strcmp(user, grp->gr_mem[j]) == 0) {
557 					/* already in it */
558 					groups[i] = "";
559 				}
560 			}
561 		}
562 	}
563 	if ((from = fopen(_PATH_GROUP, "r")) == NULL) {
564 		warn("can't append group for `%s': can't open `%s'", user, _PATH_GROUP);
565 		return 0;
566 	}
567 	if (flock(fileno(from), LOCK_EX | LOCK_NB) < 0) {
568 		warn("can't lock `%s'", _PATH_GROUP);
569 	}
570 	(void) fstat(fileno(from), &st);
571 	(void) snprintf(f, sizeof(f), "%s.XXXXXX", _PATH_GROUP);
572 	if ((fd = mkstemp(f)) < 0) {
573 		(void) fclose(from);
574 		warn("can't create gid: mkstemp failed");
575 		return 0;
576 	}
577 	if ((to = fdopen(fd, "w")) == NULL) {
578 		(void) fclose(from);
579 		(void) close(fd);
580 		(void) unlink(f);
581 		warn("can't create gid: fdopen `%s' failed", f);
582 		return 0;
583 	}
584 	while (fgets(buf, sizeof(buf), from) != NULL) {
585 		cc = strlen(buf);
586 		if ((colon = strchr(buf, ':')) == NULL) {
587 			warn("badly formed entry `%s'", buf);
588 			continue;
589 		}
590 		entc = (int)(colon - buf);
591 		for (i = 0 ; i < ngroups ; i++) {
592 			if ((groupc = strlen(groups[i])) == 0) {
593 				continue;
594 			}
595 			if (entc == groupc && strncmp(groups[i], buf, (unsigned) entc) == 0) {
596 				if ((nc = snprintf(&buf[cc - 1],
597 						sizeof(buf) - cc + 1,
598 						"%s%s\n",
599 						(buf[cc - 2] == ':') ? "" : ",",
600 						user)) < 0) {
601 					warnx("Warning: group `%s' entry too long", groups[i]);
602 				}
603 				cc += nc - 1;
604 			}
605 		}
606 		if (fwrite(buf, sizeof(char), (unsigned) cc, to) != cc) {
607 			(void) fclose(from);
608 			(void) close(fd);
609 			(void) unlink(f);
610 			warn("can't create gid: short write to `%s'", f);
611 			return 0;
612 		}
613 	}
614 	(void) fclose(from);
615 	(void) fclose(to);
616 	if (rename(f, _PATH_GROUP) < 0) {
617 		(void) unlink(f);
618 		warn("can't create gid: can't rename `%s' to `%s'", f, _PATH_GROUP);
619 		return 0;
620 	}
621 	(void) chmod(_PATH_GROUP, st.st_mode & 07777);
622 	return 1;
623 }
624 
625 /* return 1 if `login' is a valid login name */
626 static int
627 valid_login(char *login)
628 {
629 	char	*cp;
630 
631 	for (cp = login ; *cp ; cp++) {
632 		if (!isalnum(*cp) && *cp != '.' && *cp != '_' && *cp != '-') {
633 			return 0;
634 		}
635 	}
636 	return 1;
637 }
638 
639 /* return 1 if `group' is a valid group name */
640 static int
641 valid_group(char *group)
642 {
643 	char	*cp;
644 
645 	for (cp = group ; *cp ; cp++) {
646 		if (!isalnum(*cp)) {
647 			return 0;
648 		}
649 	}
650 	return 1;
651 }
652 
653 /* find the next gid in the range lo .. hi */
654 static int
655 getnextgid(int *gidp, int lo, int hi)
656 {
657 	for (*gidp = lo ; *gidp < hi ; *gidp += 1) {
658 		if (getgrgid((gid_t)*gidp) == NULL) {
659 			return 1;
660 		}
661 	}
662 	return 0;
663 }
664 
665 #ifdef EXTENSIONS
666 /* save a range of uids */
667 static int
668 save_range(user_t *up, char *cp)
669 {
670 	int	from;
671 	int	to;
672 	int	i;
673 
674 	if (up->u_rsize == 0) {
675 		up->u_rsize = 32;
676 		NEWARRAY(range_t, up->u_rv, up->u_rsize, return(0));
677 	} else if (up->u_rc == up->u_rsize) {
678 		up->u_rsize *= 2;
679 		RENEW(range_t, up->u_rv, up->u_rsize, return(0));
680 	}
681 	if (up->u_rv && sscanf(cp, "%d..%d", &from, &to) == 2) {
682 		for (i = up->u_defrc ; i < up->u_rc ; i++) {
683 			if (up->u_rv[i].r_from == from && up->u_rv[i].r_to == to) {
684 				break;
685 			}
686 		}
687 		if (i == up->u_rc) {
688 			up->u_rv[up->u_rc].r_from = from;
689 			up->u_rv[up->u_rc].r_to = to;
690 			up->u_rc += 1;
691 		}
692 	} else {
693 		warnx("Bad range `%s'", cp);
694 		return 0;
695 	}
696 	return 1;
697 }
698 #endif
699 
700 /* set the defaults in the defaults file */
701 static int
702 setdefaults(user_t *up)
703 {
704 	char	template[MaxFileNameLen];
705 	FILE	*fp;
706 	int	ret;
707 	int	fd;
708 #ifdef EXTENSIONS
709 	int	i;
710 #endif
711 
712 	(void) snprintf(template, sizeof(template), "%s.XXXXXX", CONFFILE);
713 	if ((fd = mkstemp(template)) < 0) {
714 		warnx("can't mkstemp `%s' for writing", CONFFILE);
715 		return 0;
716 	}
717 	if ((fp = fdopen(fd, "w")) == NULL) {
718 		warn("can't fdopen `%s' for writing", CONFFILE);
719 		return 0;
720 	}
721 	ret = 1;
722 	if (fprintf(fp, "group\t\t%s\n", up->u_primgrp) <= 0 ||
723 	    fprintf(fp, "base_dir\t%s\n", up->u_basedir) <= 0 ||
724 	    fprintf(fp, "skel_dir\t%s\n", up->u_skeldir) <= 0 ||
725 	    fprintf(fp, "shell\t\t%s\n", up->u_shell) <= 0 ||
726 #ifdef EXTENSIONS
727 	    fprintf(fp, "class\t\t%s\n", up->u_class) <= 0 ||
728 #endif
729 	    fprintf(fp, "inactive\t%s\n", (up->u_inactive == NULL) ?  UNSET_INACTIVE : up->u_inactive) <= 0 ||
730 	    fprintf(fp, "expire\t\t%s\n", (up->u_expire == NULL) ?  UNSET_EXPIRY : up->u_expire) <= 0 ||
731 	    fprintf(fp, "preserve\t%s\n", (up->u_preserve == 0) ? "false" : "true") <= 0) {
732 		warn("can't write to `%s'", CONFFILE);
733 		ret = 0;
734 	}
735 #ifdef EXTENSIONS
736 	for (i = (up->u_defrc != up->u_rc) ? up->u_defrc : 0 ; i < up->u_rc ; i++) {
737 		if (fprintf(fp, "range\t\t%d..%d\n", up->u_rv[i].r_from, up->u_rv[i].r_to) <= 0) {
738 			warn("can't write to `%s'", CONFFILE);
739 			ret = 0;
740 		}
741 	}
742 #endif
743 	(void) fclose(fp);
744 	if (ret) {
745 		ret = ((rename(template, CONFFILE) == 0) && (chmod(CONFFILE, 0644) == 0));
746 	}
747 	return ret;
748 }
749 
750 /* read the defaults file */
751 static void
752 read_defaults(user_t *up)
753 {
754 	struct stat	st;
755 	size_t		lineno;
756 	size_t		len;
757 	FILE		*fp;
758 	char		*cp;
759 	char		*s;
760 
761 	memsave(&up->u_primgrp, DEF_GROUP, strlen(DEF_GROUP));
762 	memsave(&up->u_basedir, DEF_BASEDIR, strlen(DEF_BASEDIR));
763 	memsave(&up->u_skeldir, DEF_SKELDIR, strlen(DEF_SKELDIR));
764 	memsave(&up->u_shell, DEF_SHELL, strlen(DEF_SHELL));
765 	memsave(&up->u_comment, DEF_COMMENT, strlen(DEF_COMMENT));
766 #ifdef EXTENSIONS
767 	memsave(&up->u_class, DEF_CLASS, strlen(DEF_CLASS));
768 #endif
769 	up->u_rsize = 16;
770 	up->u_defrc = 0;
771 	NEWARRAY(range_t, up->u_rv, up->u_rsize, exit(1));
772 	up->u_inactive = DEF_INACTIVE;
773 	up->u_expire = DEF_EXPIRE;
774 	if ((fp = fopen(CONFFILE, "r")) == NULL) {
775 		if (stat(CONFFILE, &st) < 0 && !setdefaults(up)) {
776 			warn("can't create `%s' defaults file", CONFFILE);
777 		}
778 		fp = fopen(CONFFILE, "r");
779 	}
780 	if (fp != NULL) {
781 		while ((s = fparseln(fp, &len, &lineno, NULL, 0)) != NULL) {
782 			if (strncmp(s, "group", 5) == 0) {
783 				for (cp = s + 5 ; *cp && isspace(*cp) ; cp++) {
784 				}
785 				memsave(&up->u_primgrp, cp, strlen(cp));
786 			} else if (strncmp(s, "base_dir", 8) == 0) {
787 				for (cp = s + 8 ; *cp && isspace(*cp) ; cp++) {
788 				}
789 				memsave(&up->u_basedir, cp, strlen(cp));
790 			} else if (strncmp(s, "skel_dir", 8) == 0) {
791 				for (cp = s + 8 ; *cp && isspace(*cp) ; cp++) {
792 				}
793 				memsave(&up->u_skeldir, cp, strlen(cp));
794 			} else if (strncmp(s, "shell", 5) == 0) {
795 				for (cp = s + 5 ; *cp && isspace(*cp) ; cp++) {
796 				}
797 				memsave(&up->u_shell, cp, strlen(cp));
798 #ifdef EXTENSIONS
799 			} else if (strncmp(s, "class", 5) == 0) {
800 				for (cp = s + 5 ; *cp && isspace(*cp) ; cp++) {
801 				}
802 				memsave(&up->u_class, cp, strlen(cp));
803 #endif
804 			} else if (strncmp(s, "inactive", 8) == 0) {
805 				for (cp = s + 8 ; *cp && isspace(*cp) ; cp++) {
806 				}
807 				if (strcmp(cp, UNSET_INACTIVE) == 0) {
808 					if (up->u_inactive) {
809 						FREE(up->u_inactive);
810 					}
811 					up->u_inactive = NULL;
812 				} else {
813 					memsave(&up->u_inactive, cp, strlen(cp));
814 				}
815 #ifdef EXTENSIONS
816 			} else if (strncmp(s, "range", 5) == 0) {
817 				for (cp = s + 5 ; *cp && isspace(*cp) ; cp++) {
818 				}
819 				(void) save_range(up, cp);
820 #endif
821 #ifdef EXTENSIONS
822 			} else if (strncmp(s, "preserve", 8) == 0) {
823 				for (cp = s + 8 ; *cp && isspace(*cp) ; cp++) {
824 				}
825 				up->u_preserve = (strncmp(cp, "true", 4) == 0) ? 1 :
826 						  (strncmp(cp, "yes", 3) == 0) ? 1 :
827 						   atoi(cp);
828 #endif
829 			} else if (strncmp(s, "expire", 6) == 0) {
830 				for (cp = s + 6 ; *cp && isspace(*cp) ; cp++) {
831 				}
832 				if (strcmp(cp, UNSET_EXPIRY) == 0) {
833 					if (up->u_expire) {
834 						FREE(up->u_expire);
835 					}
836 					up->u_expire = NULL;
837 				} else {
838 					memsave(&up->u_expire, cp, strlen(cp));
839 				}
840 			}
841 			(void) free(s);
842 		}
843 		(void) fclose(fp);
844 	}
845 	if (up->u_rc == 0) {
846 		up->u_rv[up->u_rc].r_from = DEF_LOWUID;
847 		up->u_rv[up->u_rc].r_to = DEF_HIGHUID;
848 		up->u_rc += 1;
849 	}
850 	up->u_defrc = up->u_rc;
851 }
852 
853 /* return the next valid unused uid */
854 static int
855 getnextuid(int sync_uid_gid, int *uid, int low_uid, int high_uid)
856 {
857 	for (*uid = low_uid ; *uid <= high_uid ; (*uid)++) {
858 		if (getpwuid((uid_t)(*uid)) == NULL && *uid != NOBODY_UID) {
859 			if (sync_uid_gid) {
860 				if (getgrgid((gid_t)(*uid)) == NULL) {
861 					return 1;
862 				}
863 			} else {
864 				return 1;
865 			}
866 		}
867 	}
868 	return 0;
869 }
870 
871 /* add a user */
872 static int
873 adduser(char *login, user_t *up)
874 {
875 	struct group	*grp;
876 	struct stat	st;
877 	struct tm	tm;
878 	time_t		expire;
879 	time_t		inactive;
880 	char		password[PasswordLength + 1];
881 	char		home[MaxFileNameLen];
882 	char		buf[MaxFileNameLen];
883 	int		sync_uid_gid;
884 	int		masterfd;
885 	int		ptmpfd;
886 	int		gid;
887 	int		cc;
888 	int		i;
889 
890 	if (!valid_login(login)) {
891 		errx(EXIT_FAILURE, "`%s' is not a valid login name", login);
892 	}
893 	if ((masterfd = open(_PATH_MASTERPASSWD, O_RDONLY)) < 0) {
894 		err(EXIT_FAILURE, "can't open `%s'", _PATH_MASTERPASSWD);
895 	}
896 	if (flock(masterfd, LOCK_EX | LOCK_NB) < 0) {
897 		err(EXIT_FAILURE, "can't lock `%s'", _PATH_MASTERPASSWD);
898 	}
899 	pw_init();
900 	if ((ptmpfd = pw_lock(WAITSECS)) < 0) {
901 		(void) close(masterfd);
902 		err(EXIT_FAILURE, "can't obtain pw_lock");
903 	}
904 	while ((cc = read(masterfd, buf, sizeof(buf))) > 0) {
905 		if (write(ptmpfd, buf, (size_t)(cc)) != cc) {
906 			(void) close(masterfd);
907 			(void) close(ptmpfd);
908 			(void) pw_abort();
909 			err(EXIT_FAILURE, "short write to /etc/ptmp (not %d chars)", cc);
910 		}
911 	}
912 	/* if no uid was specified, get next one in [low_uid..high_uid] range */
913 	sync_uid_gid = (strcmp(up->u_primgrp, "=uid") == 0);
914 	if (up->u_uid == -1) {
915 		int	got_id = 0;
916 
917 		/*
918 		 * Look for a free UID in the command line ranges (if any).
919 		 * These start after the ranges specified in the config file.
920 		 */
921 		for (i = up->u_defrc; !got_id && i < up->u_rc ; i++) {
922 			got_id = getnextuid(sync_uid_gid, &up->u_uid,
923 					up->u_rv[i].r_from, up->u_rv[i].r_to);
924 		}
925 		/*
926 		 * If there were no free UIDs in the command line ranges,
927 		 * try the ranges from the config file (there will always
928 		 * be at least one default).
929 		 */
930 		for (i = 0; !got_id && i < up->u_defrc; i++) {
931 			got_id = getnextuid(sync_uid_gid, &up->u_uid,
932 					up->u_rv[i].r_from, up->u_rv[i].r_to);
933 		}
934 		if (!got_id) {
935 			(void) close(ptmpfd);
936 			(void) pw_abort();
937 			errx(EXIT_FAILURE, "can't get next uid for %d", up->u_uid);
938 		}
939 	}
940 	/* check uid isn't already allocated */
941 	if (!(up->u_flags & F_DUPUID) && getpwuid((uid_t)(up->u_uid)) != NULL) {
942 		(void) close(ptmpfd);
943 		(void) pw_abort();
944 		errx(EXIT_FAILURE, "uid %d is already in use", up->u_uid);
945 	}
946 	/* if -g=uid was specified, check gid is unused */
947 	if (sync_uid_gid) {
948 		if (getgrgid((gid_t)(up->u_uid)) != NULL) {
949 			(void) close(ptmpfd);
950 			(void) pw_abort();
951 			errx(EXIT_FAILURE, "gid %d is already in use", up->u_uid);
952 		}
953 		gid = up->u_uid;
954 	} else if ((grp = getgrnam(up->u_primgrp)) != NULL) {
955 		gid = grp->gr_gid;
956 	} else if (is_number(up->u_primgrp) &&
957 		   (grp = getgrgid((gid_t)atoi(up->u_primgrp))) != NULL) {
958 		gid = grp->gr_gid;
959 	} else {
960 		(void) close(ptmpfd);
961 		(void) pw_abort();
962 		errx(EXIT_FAILURE, "group %s not found", up->u_primgrp);
963 	}
964 	/* check name isn't already in use */
965 	if (!(up->u_flags & F_DUPUID) && getpwnam(login) != NULL) {
966 		(void) close(ptmpfd);
967 		(void) pw_abort();
968 		errx(EXIT_FAILURE, "already a `%s' user", login);
969 	}
970 	if (up->u_flags & F_HOMEDIR) {
971 		(void) strlcpy(home, up->u_home, sizeof(home));
972 	} else {
973 		/* if home directory hasn't been given, make it up */
974 		(void) snprintf(home, sizeof(home), "%s/%s", up->u_basedir, login);
975 	}
976 	inactive = 0;
977 	if (up->u_inactive != NULL) {
978 		(void) memset(&tm, 0, sizeof(tm));
979 		if (strptime(up->u_inactive, "%c", &tm) != NULL) {
980 			inactive = mktime(&tm);
981 		} else if (strptime(up->u_inactive, "%B %d %Y", &tm) != NULL) {
982 			inactive = mktime(&tm);
983 		} else if (isdigit(up->u_inactive[0]) != NULL) {
984 			inactive = atoi(up->u_inactive);
985 		} else {
986 			warnx("Warning: inactive time `%s' invalid, account expiry off",
987 				up->u_inactive);
988 		}
989 	}
990 	expire = 0;
991 	if (up->u_expire != NULL) {
992 		(void) memset(&tm, 0, sizeof(tm));
993 		if (strptime(up->u_expire, "%c", &tm) != NULL) {
994 			expire = mktime(&tm);
995 		} else if (strptime(up->u_expire, "%B %d %Y", &tm) != NULL) {
996 			expire = mktime(&tm);
997 		} else if (isdigit(up->u_expire[0]) != NULL) {
998 			expire = atoi(up->u_expire);
999 		} else {
1000 			warnx("Warning: expire time `%s' invalid, password expiry off",
1001 				up->u_expire);
1002 		}
1003 	}
1004 	if (lstat(home, &st) < 0 && !(up->u_flags & F_MKDIR)) {
1005 		warnx("Warning: home directory `%s' doesn't exist, and -m was not specified",
1006 		    home);
1007 	}
1008 	password[PasswordLength] = '\0';
1009 	if (up->u_password != NULL &&
1010 	    strlen(up->u_password) == PasswordLength) {
1011 		(void) memcpy(password, up->u_password, PasswordLength);
1012 	} else {
1013 		(void) memset(password, '*', PasswordLength);
1014 		if (up->u_password != NULL) {
1015 			warnx("Password `%s' is invalid: setting it to `%s'",
1016 				up->u_password, password);
1017 		}
1018 	}
1019 	cc = snprintf(buf, sizeof(buf), "%s:%s:%d:%d:%s:%ld:%ld:%s:%s:%s\n",
1020 			login,
1021 			password,
1022 			up->u_uid,
1023 			gid,
1024 #ifdef EXTENSIONS
1025 			up->u_class,
1026 #else
1027 			"",
1028 #endif
1029 			(long) inactive,
1030 			(long) expire,
1031 			up->u_comment,
1032 			home,
1033 			up->u_shell);
1034 	if (write(ptmpfd, buf, (size_t) cc) != cc) {
1035 		(void) close(ptmpfd);
1036 		(void) pw_abort();
1037 		err(EXIT_FAILURE, "can't add `%s'", buf);
1038 	}
1039 	if (up->u_flags & F_MKDIR) {
1040 		if (lstat(home, &st) == 0) {
1041 			(void) close(ptmpfd);
1042 			(void) pw_abort();
1043 			errx(EXIT_FAILURE, "home directory `%s' already exists", home);
1044 		} else {
1045 			if (asystem("%s -p %s", MKDIR, home) != 0) {
1046 				(void) close(ptmpfd);
1047 				(void) pw_abort();
1048 				err(EXIT_FAILURE, "can't mkdir `%s'", home);
1049 			}
1050 			(void) copydotfiles(up->u_skeldir, up->u_uid, gid, home);
1051 		}
1052 	}
1053 	if (strcmp(up->u_primgrp, "=uid") == 0 &&
1054 	    getgrnam(login) == NULL &&
1055 	    !creategid(login, gid, login)) {
1056 		(void) close(ptmpfd);
1057 		(void) pw_abort();
1058 		errx(EXIT_FAILURE, "can't create gid %d for login name %s", gid, login);
1059 	}
1060 	if (up->u_groupc > 0 && !append_group(login, up->u_groupc, up->u_groupv)) {
1061 		(void) close(ptmpfd);
1062 		(void) pw_abort();
1063 		errx(EXIT_FAILURE, "can't append `%s' to new groups", login);
1064 	}
1065 	(void) close(ptmpfd);
1066 #if PW_MKDB_ARGC == 2
1067 	if (pw_mkdb(login, 0) < 0) {
1068 		err(EXIT_FAILURE, "pw_mkdb failed");
1069 	}
1070 #else
1071 	if (pw_mkdb() < 0) {
1072 		err(EXIT_FAILURE, "pw_mkdb failed");
1073 	}
1074 #endif
1075 	return 1;
1076 }
1077 
1078 /* modify a user */
1079 static int
1080 moduser(char *login, char *newlogin, user_t *up)
1081 {
1082 	struct passwd	*pwp;
1083 	struct group	*grp;
1084 	struct tm	tm;
1085 	const char	*homedir;
1086 	size_t		colonc, len, loginc;
1087 	size_t		cc;
1088 	FILE		*master;
1089 	char		newdir[MaxFileNameLen];
1090 	char		*buf, *colon, *line;
1091 	int		masterfd;
1092 	int		ptmpfd;
1093 	int		error;
1094 
1095 	if (!valid_login(newlogin)) {
1096 		errx(EXIT_FAILURE, "`%s' is not a valid login name", login);
1097 	}
1098 	if ((pwp = getpwnam(login)) == NULL) {
1099 		errx(EXIT_FAILURE, "No such user `%s'", login);
1100 	}
1101 	/* keep dir name in case we need it for '-m' */
1102 	homedir = pwp->pw_dir;
1103 
1104 	if ((masterfd = open(_PATH_MASTERPASSWD, O_RDONLY)) < 0) {
1105 		err(EXIT_FAILURE, "can't open `%s'", _PATH_MASTERPASSWD);
1106 	}
1107 	if (flock(masterfd, LOCK_EX | LOCK_NB) < 0) {
1108 		err(EXIT_FAILURE, "can't lock `%s'", _PATH_MASTERPASSWD);
1109 	}
1110 	pw_init();
1111 	if ((ptmpfd = pw_lock(WAITSECS)) < 0) {
1112 		(void) close(masterfd);
1113 		err(EXIT_FAILURE, "can't obtain pw_lock");
1114 	}
1115 	if ((master = fdopen(masterfd, "r")) == NULL) {
1116 		(void) close(masterfd);
1117 		(void) close(ptmpfd);
1118 		(void) pw_abort();
1119 		err(EXIT_FAILURE, "can't fdopen fd for %s", _PATH_MASTERPASSWD);
1120 	}
1121 	if (up != NULL) {
1122 		if (up->u_flags & F_USERNAME) {
1123 			/* if changing name, check new name isn't already in use */
1124 			if (strcmp(login, newlogin) != 0 && getpwnam(newlogin) != NULL) {
1125 				(void) close(ptmpfd);
1126 				(void) pw_abort();
1127 				errx(EXIT_FAILURE, "already a `%s' user", newlogin);
1128 			}
1129 			pwp->pw_name = newlogin;
1130 
1131 			/*
1132 			 * Provide a new directory name in case the
1133 			 * home directory is to be moved.
1134 			 */
1135 			if (up->u_flags & F_MKDIR) {
1136 				snprintf(newdir, sizeof(newdir), "%s/%s", up->u_basedir, newlogin);
1137 				pwp->pw_dir = newdir;
1138 			}
1139 		}
1140 		if (up->u_flags & F_PASSWORD) {
1141 			if (up->u_password != NULL && strlen(up->u_password) == PasswordLength)
1142 				pwp->pw_passwd = up->u_password;
1143 		}
1144 		if (up->u_flags & F_UID) {
1145 			/* check uid isn't already allocated */
1146 			if (!(up->u_flags & F_DUPUID) && getpwuid((uid_t)(up->u_uid)) != NULL) {
1147 				(void) close(ptmpfd);
1148 				(void) pw_abort();
1149 				errx(EXIT_FAILURE, "uid %d is already in use", up->u_uid);
1150 			}
1151 			pwp->pw_uid = up->u_uid;
1152 		}
1153 		if (up->u_flags & F_GROUP) {
1154 			/* if -g=uid was specified, check gid is unused */
1155 			if (strcmp(up->u_primgrp, "=uid") == 0) {
1156 				if (getgrgid((gid_t)(up->u_uid)) != NULL) {
1157 					(void) close(ptmpfd);
1158 					(void) pw_abort();
1159 					errx(EXIT_FAILURE, "gid %d is already in use", up->u_uid);
1160 				}
1161 				pwp->pw_gid = up->u_uid;
1162 			} else if ((grp = getgrnam(up->u_primgrp)) != NULL) {
1163 				pwp->pw_gid = grp->gr_gid;
1164 			} else if (is_number(up->u_primgrp) &&
1165 				   (grp = getgrgid((gid_t)atoi(up->u_primgrp))) != NULL) {
1166 				pwp->pw_gid = grp->gr_gid;
1167 			} else {
1168 				(void) close(ptmpfd);
1169 				(void) pw_abort();
1170 				errx(EXIT_FAILURE, "group %s not found", up->u_primgrp);
1171 			}
1172 		}
1173 		if (up->u_flags & F_INACTIVE) {
1174 			(void) memset(&tm, 0, sizeof(tm));
1175 			if (strptime(up->u_inactive, "%c", &tm) != NULL) {
1176 				pwp->pw_change = mktime(&tm);
1177 			} else if (strptime(up->u_inactive, "%B %d %Y", &tm) != NULL) {
1178 				pwp->pw_change = mktime(&tm);
1179 			} else if (isdigit(up->u_inactive[0]) != NULL) {
1180 				pwp->pw_change = atoi(up->u_inactive);
1181 			} else {
1182 				warnx("Warning: inactive time `%s' invalid, password expiry off",
1183 					up->u_inactive);
1184 			}
1185 		}
1186 		if (up->u_flags & F_EXPIRE) {
1187 			(void) memset(&tm, 0, sizeof(tm));
1188 			if (strptime(up->u_expire, "%c", &tm) != NULL) {
1189 				pwp->pw_expire = mktime(&tm);
1190 			} else if (strptime(up->u_expire, "%B %d %Y", &tm) != NULL) {
1191 				pwp->pw_expire = mktime(&tm);
1192 			} else if (isdigit(up->u_expire[0]) != NULL) {
1193 				pwp->pw_expire = atoi(up->u_expire);
1194 			} else {
1195 				warnx("Warning: expire time `%s' invalid, password expiry off",
1196 					up->u_expire);
1197 			}
1198 		}
1199 		if (up->u_flags & F_COMMENT)
1200 			pwp->pw_gecos = up->u_comment;
1201 		if (up->u_flags & F_HOMEDIR)
1202 			pwp->pw_dir = up->u_home;
1203 		if (up->u_flags & F_SHELL)
1204 			pwp->pw_shell = up->u_shell;
1205 #ifdef EXTENSIONS
1206 		if (up->u_flags & F_CLASS)
1207 			pwp->pw_class = up->u_class;
1208 #endif
1209 	}
1210 	loginc = strlen(login);
1211 	while ((line = fgetln(master, &len)) != NULL) {
1212 		if ((colon = strchr(line, ':')) == NULL) {
1213 			warnx("Malformed entry `%s'. Skipping", line);
1214 			continue;
1215 		}
1216 		colonc = (size_t)(colon - line);
1217 		if (strncmp(login, line, loginc) == 0 && loginc == colonc) {
1218 			if (up != NULL) {
1219 				len = (int)asprintf(&buf, "%s:%s:%d:%d:"
1220 #ifdef EXTENSIONS
1221 									 "%s"
1222 #endif
1223 									      ":%ld:%ld:%s:%s:%s\n",
1224 					newlogin,
1225 					pwp->pw_passwd,
1226 					pwp->pw_uid,
1227 					pwp->pw_gid,
1228 #ifdef EXTENSIONS
1229 					pwp->pw_class,
1230 #endif
1231 					(long)pwp->pw_change,
1232 					(long)pwp->pw_expire,
1233 					pwp->pw_gecos,
1234 					pwp->pw_dir,
1235 					pwp->pw_shell);
1236 				if (write(ptmpfd, buf, len) != len) {
1237 					(void) close(ptmpfd);
1238 					(void) pw_abort();
1239 					err(EXIT_FAILURE, "can't add `%s'", buf);
1240 				}
1241 				(void) free(buf);
1242 			}
1243 		} else if ((cc = write(ptmpfd, line, len)) != len) {
1244 			(void) close(masterfd);
1245 			(void) close(ptmpfd);
1246 			(void) pw_abort();
1247 			err(EXIT_FAILURE, "short write to /etc/ptmp (%lld not %lld chars)",
1248 				(long long)cc,
1249 				(long long)len);
1250 		}
1251 	}
1252 	if (up != NULL) {
1253 		if ((up->u_flags & F_MKDIR) &&
1254 		    asystem("%s %s %s", MV, homedir, pwp->pw_dir) != 0) {
1255 			(void) close(ptmpfd);
1256 			(void) pw_abort();
1257 			err(EXIT_FAILURE, "can't move `%s' to `%s'",
1258 				homedir, pwp->pw_dir);
1259 		}
1260 		if (up->u_groupc > 0 &&
1261 		    !append_group(newlogin, up->u_groupc, up->u_groupv)) {
1262 			(void) close(ptmpfd);
1263 			(void) pw_abort();
1264 			errx(EXIT_FAILURE, "can't append `%s' to new groups",
1265 				newlogin);
1266 		}
1267 	}
1268 	(void) close(ptmpfd);
1269 #if PW_MKDB_ARGC == 2
1270 	if (up != NULL && strcmp(login, newlogin) == 0) {
1271 		error = pw_mkdb(login, 0);
1272 	} else {
1273 		error = pw_mkdb(NULL, 0);
1274 	}
1275 #else
1276 	error = pw_mkdb();
1277 #endif
1278 	if (error < 0) {
1279 		err(EXIT_FAILURE, "pw_mkdb failed");
1280 	}
1281 
1282 	return 1;
1283 }
1284 
1285 
1286 #ifdef EXTENSIONS
1287 /* see if we can find out the user struct */
1288 static struct passwd *
1289 find_user_info(char *name)
1290 {
1291 	struct passwd	*pwp;
1292 
1293 	if ((pwp = getpwnam(name)) != NULL) {
1294 		return pwp;
1295 	}
1296 	if (is_number(name) && (pwp = getpwuid((uid_t)atoi(name))) != NULL) {
1297 		return pwp;
1298 	}
1299 	return NULL;
1300 }
1301 #endif
1302 
1303 #ifdef EXTENSIONS
1304 /* see if we can find out the group struct */
1305 static struct group *
1306 find_group_info(char *name)
1307 {
1308 	struct group	*grp;
1309 
1310 	if ((grp = getgrnam(name)) != NULL) {
1311 		return grp;
1312 	}
1313 	if (is_number(name) && (grp = getgrgid((gid_t)atoi(name))) != NULL) {
1314 		return grp;
1315 	}
1316 	return NULL;
1317 }
1318 #endif
1319 
1320 /* print out usage message, and then exit */
1321 void
1322 usermgmt_usage(const char *prog)
1323 {
1324 	if (strcmp(prog, "useradd") == 0) {
1325 		(void) fprintf(stderr, "Usage: %s -D [-b basedir] [-e expiry] "
1326 		    "[-f inactive] [-g group]\n\t[-r lowuid..highuid] "
1327 		    "[-s shell] [-L class]\n", prog);
1328 		(void) fprintf(stderr, "Usage: %s [-G group] [-b basedir] "
1329 		    "[-c comment] [-d homedir] [-e expiry]\n\t[-f inactive] "
1330 		    "[-g group] [-k skeletondir] [-m] [-o] [-p password]\n"
1331 		    "\t[-r lowuid..highuid] [-s shell]\n\t[-u uid] [-v] user\n",
1332 		    prog);
1333 	} else if (strcmp(prog, "usermod") == 0) {
1334 		(void) fprintf(stderr, "Usage: %s [-G group] [-c comment] "
1335 		    "[-d homedir] [-e expire] [-f inactive]\n\t[-g group] "
1336 		    "[-l newname] [-m] [-o] [-p password] [-s shell] [-u uid]\n"
1337 		    "\t[-L class] [-v] user\n", prog);
1338 	} else if (strcmp(prog, "userdel") == 0) {
1339 		(void) fprintf(stderr, "Usage: %s -D [-p preserve]\n", prog);
1340 		(void) fprintf(stderr,
1341 		    "Usage: %s [-p preserve] [-r] [-v] user\n", prog);
1342 #ifdef EXTENSIONS
1343 	} else if (strcmp(prog, "userinfo") == 0) {
1344 		(void) fprintf(stderr, "Usage: %s [-e] [-v] user\n", prog);
1345 #endif
1346 	} else if (strcmp(prog, "groupadd") == 0) {
1347 		(void) fprintf(stderr, "Usage: %s [-g gid] [-o] [-v] group\n",
1348 		    prog);
1349 	} else if (strcmp(prog, "groupdel") == 0) {
1350 		(void) fprintf(stderr, "Usage: %s [-v] group\n", prog);
1351 	} else if (strcmp(prog, "groupmod") == 0) {
1352 		(void) fprintf(stderr,
1353 		    "Usage: %s [-g gid] [-o] [-n newname] [-v] group\n", prog);
1354 	} else if (strcmp(prog, "user") == 0 || strcmp(prog, "group") == 0) {
1355 		(void) fprintf(stderr,
1356 		    "Usage: %s ( add | del | mod | info ) ...\n", prog);
1357 #ifdef EXTENSIONS
1358 	} else if (strcmp(prog, "groupinfo") == 0) {
1359 		(void) fprintf(stderr, "Usage: %s [-e] [-v] group\n", prog);
1360 #endif
1361 	}
1362 	exit(EXIT_FAILURE);
1363 	/* NOTREACHED */
1364 }
1365 
1366 #ifdef EXTENSIONS
1367 #define ADD_OPT_EXTENSIONS	"p:r:vL:"
1368 #else
1369 #define ADD_OPT_EXTENSIONS
1370 #endif
1371 
1372 int
1373 useradd(int argc, char **argv)
1374 {
1375 	user_t	u;
1376 	int	defaultfield;
1377 	int	bigD;
1378 	int	c;
1379 #ifdef EXTENSIONS
1380 	int	i;
1381 #endif
1382 
1383 	(void) memset(&u, 0, sizeof(u));
1384 	read_defaults(&u);
1385 	u.u_uid = -1;
1386 	defaultfield = bigD = 0;
1387 	while ((c = getopt(argc, argv, "DG:b:c:d:e:f:g:k:mou:s:" ADD_OPT_EXTENSIONS)) != -1) {
1388 		switch(c) {
1389 		case 'D':
1390 			bigD = 1;
1391 			break;
1392 		case 'G':
1393 			while ((u.u_groupv[u.u_groupc] = strsep(&optarg, ",")) != NULL &&
1394 			       u.u_groupc < NGROUPS_MAX) {
1395 				if (u.u_groupv[u.u_groupc][0] != 0) {
1396 					u.u_groupc++;
1397 				}
1398 			}
1399 			if (optarg != NULL) {
1400 				warnx("Truncated list of secondary groups to %d entries", NGROUPS_MAX);
1401 			}
1402 			break;
1403 		case 'b':
1404 			defaultfield = 1;
1405 			memsave(&u.u_basedir, optarg, strlen(optarg));
1406 			break;
1407 		case 'c':
1408 			memsave(&u.u_comment, optarg, strlen(optarg));
1409 			break;
1410 		case 'd':
1411 			memsave(&u.u_home, optarg, strlen(optarg));
1412 			u.u_flags |= F_HOMEDIR;
1413 			break;
1414 		case 'e':
1415 			defaultfield = 1;
1416 			memsave(&u.u_expire, optarg, strlen(optarg));
1417 			break;
1418 		case 'f':
1419 			defaultfield = 1;
1420 			memsave(&u.u_inactive, optarg, strlen(optarg));
1421 			break;
1422 		case 'g':
1423 			defaultfield = 1;
1424 			memsave(&u.u_primgrp, optarg, strlen(optarg));
1425 			break;
1426 		case 'k':
1427 			defaultfield = 1;
1428 			memsave(&u.u_skeldir, optarg, strlen(optarg));
1429 			break;
1430 #ifdef EXTENSIONS
1431 		case 'L':
1432 			defaultfield = 1;
1433 			memsave(&u.u_class, optarg, strlen(optarg));
1434 			break;
1435 #endif
1436 		case 'm':
1437 			u.u_flags |= F_MKDIR;
1438 			break;
1439 		case 'o':
1440 			u.u_flags |= F_DUPUID;
1441 			break;
1442 #ifdef EXTENSIONS
1443 		case 'p':
1444 			memsave(&u.u_password, optarg, strlen(optarg));
1445 			break;
1446 #endif
1447 #ifdef EXTENSIONS
1448 		case 'r':
1449 			defaultfield = 1;
1450 			(void) save_range(&u, optarg);
1451 			break;
1452 #endif
1453 		case 's':
1454 			defaultfield = 1;
1455 			memsave(&u.u_shell, optarg, strlen(optarg));
1456 			break;
1457 		case 'u':
1458 			if (!is_number(optarg)) {
1459 				errx(EXIT_FAILURE, "When using [-u uid], the uid must be numeric");
1460 			}
1461 			u.u_uid = atoi(optarg);
1462 			break;
1463 #ifdef EXTENSIONS
1464 		case 'v':
1465 			verbose = 1;
1466 			break;
1467 #endif
1468 		}
1469 	}
1470 	if (bigD) {
1471 		if (defaultfield) {
1472 			checkeuid();
1473 			return setdefaults(&u) ? EXIT_SUCCESS : EXIT_FAILURE;
1474 		}
1475 		(void) printf("group\t\t%s\n", u.u_primgrp);
1476 		(void) printf("base_dir\t%s\n", u.u_basedir);
1477 		(void) printf("skel_dir\t%s\n", u.u_skeldir);
1478 		(void) printf("shell\t\t%s\n", u.u_shell);
1479 #ifdef EXTENSIONS
1480 		(void) printf("class\t\t%s\n", u.u_class);
1481 #endif
1482 		(void) printf("inactive\t%s\n", (u.u_inactive == NULL) ? UNSET_INACTIVE : u.u_inactive);
1483 		(void) printf("expire\t\t%s\n", (u.u_expire == NULL) ? UNSET_EXPIRY : u.u_expire);
1484 #ifdef EXTENSIONS
1485 		for (i = 0 ; i < u.u_rc ; i++) {
1486 			(void) printf("range\t\t%d..%d\n", u.u_rv[i].r_from, u.u_rv[i].r_to);
1487 		}
1488 #endif
1489 		return EXIT_SUCCESS;
1490 	}
1491 	argc -= optind;
1492 	argv += optind;
1493 	if (argc != 1) {
1494 		usermgmt_usage("useradd");
1495 	}
1496 	checkeuid();
1497 	return adduser(*argv, &u) ? EXIT_SUCCESS : EXIT_FAILURE;
1498 }
1499 
1500 #ifdef EXTENSIONS
1501 #define MOD_OPT_EXTENSIONS	"p:vL:"
1502 #else
1503 #define MOD_OPT_EXTENSIONS
1504 #endif
1505 
1506 int
1507 usermod(int argc, char **argv)
1508 {
1509 	user_t	u;
1510 	char	newuser[MaxUserNameLen + 1];
1511 	int	c, have_new_user;
1512 
1513 	(void) memset(&u, 0, sizeof(u));
1514 	(void) memset(newuser, 0, sizeof(newuser));
1515 	read_defaults(&u);
1516 	have_new_user = 0;
1517 	while ((c = getopt(argc, argv, "G:c:d:e:f:g:l:mos:u:" MOD_OPT_EXTENSIONS)) != -1) {
1518 		switch(c) {
1519 		case 'G':
1520 			while ((u.u_groupv[u.u_groupc] = strsep(&optarg, ",")) != NULL &&
1521 			       u.u_groupc < NGROUPS_MAX) {
1522 				if (u.u_groupv[u.u_groupc][0] != 0) {
1523 					u.u_groupc++;
1524 				}
1525 			}
1526 			if (optarg != NULL) {
1527 				warnx("Truncated list of secondary groups to %d entries", NGROUPS_MAX);
1528 			}
1529 			u.u_flags |= F_SECGROUP;
1530 			break;
1531 		case 'c':
1532 			memsave(&u.u_comment, optarg, strlen(optarg));
1533 			u.u_flags |= F_COMMENT;
1534 			break;
1535 		case 'd':
1536 			memsave(&u.u_home, optarg, strlen(optarg));
1537 			u.u_flags |= F_HOMEDIR;
1538 			break;
1539 		case 'e':
1540 			memsave(&u.u_expire, optarg, strlen(optarg));
1541 			u.u_flags |= F_EXPIRE;
1542 			break;
1543 		case 'f':
1544 			memsave(&u.u_inactive, optarg, strlen(optarg));
1545 			u.u_flags |= F_INACTIVE;
1546 			break;
1547 		case 'g':
1548 			memsave(&u.u_primgrp, optarg, strlen(optarg));
1549 			u.u_flags |= F_GROUP;
1550 			break;
1551 		case 'l':
1552 			(void) strlcpy(newuser, optarg, sizeof(newuser));
1553 			have_new_user = 1;
1554 			u.u_flags |= F_USERNAME;
1555 			break;
1556 #ifdef EXTENSIONS
1557 		case 'L':
1558 			memsave(&u.u_class, optarg, strlen(optarg));
1559 			u.u_flags |= F_CLASS;
1560 			break;
1561 #endif
1562 		case 'm':
1563 			u.u_flags |= F_MKDIR;
1564 			break;
1565 		case 'o':
1566 			u.u_flags |= F_DUPUID;
1567 			break;
1568 #ifdef EXTENSIONS
1569 		case 'p':
1570 			memsave(&u.u_password, optarg, strlen(optarg));
1571 			u.u_flags |= F_PASSWORD;
1572 			break;
1573 #endif
1574 		case 's':
1575 			memsave(&u.u_shell, optarg, strlen(optarg));
1576 			u.u_flags |= F_SHELL;
1577 			break;
1578 		case 'u':
1579 			if (!is_number(optarg)) {
1580 				errx(EXIT_FAILURE, "When using [-u uid], the uid must be numeric");
1581 			}
1582 			u.u_uid = atoi(optarg);
1583 			u.u_flags |= F_UID;
1584 			break;
1585 #ifdef EXTENSIONS
1586 		case 'v':
1587 			verbose = 1;
1588 			break;
1589 #endif
1590 		}
1591 	}
1592 	if ((u.u_flags & F_MKDIR) && !(u.u_flags & F_HOMEDIR) &&
1593 	    !(u.u_flags & F_USERNAME)) {
1594 		warnx("option 'm' useless without 'd' or 'l' -- ignored");
1595 		u.u_flags &= ~F_MKDIR;
1596 	}
1597 	argc -= optind;
1598 	argv += optind;
1599 	if (argc != 1) {
1600 		usermgmt_usage("usermod");
1601 	}
1602 	checkeuid();
1603 	return moduser(*argv, (have_new_user) ? newuser : *argv, &u) ? EXIT_SUCCESS : EXIT_FAILURE;
1604 }
1605 
1606 #ifdef EXTENSIONS
1607 #define DEL_OPT_EXTENSIONS	"Dp:v"
1608 #else
1609 #define DEL_OPT_EXTENSIONS
1610 #endif
1611 
1612 int
1613 userdel(int argc, char **argv)
1614 {
1615 	struct passwd	*pwp;
1616 	user_t		u;
1617 	char		password[PasswordLength + 1];
1618 	int		defaultfield;
1619 	int		rmhome;
1620 	int		bigD;
1621 	int		c;
1622 
1623 	(void) memset(&u, 0, sizeof(u));
1624 	read_defaults(&u);
1625 	defaultfield = bigD = rmhome = 0;
1626 	while ((c = getopt(argc, argv, "r" DEL_OPT_EXTENSIONS)) != -1) {
1627 		switch(c) {
1628 #ifdef EXTENSIONS
1629 		case 'D':
1630 			bigD = 1;
1631 			break;
1632 #endif
1633 #ifdef EXTENSIONS
1634 		case 'p':
1635 			defaultfield = 1;
1636 			u.u_preserve = (strcmp(optarg, "true") == 0) ? 1 :
1637 					(strcmp(optarg, "yes") == 0) ? 1 :
1638 					 atoi(optarg);
1639 			break;
1640 #endif
1641 		case 'r':
1642 			rmhome = 1;
1643 			break;
1644 #ifdef EXTENSIONS
1645 		case 'v':
1646 			verbose = 1;
1647 			break;
1648 #endif
1649 		}
1650 	}
1651 #ifdef EXTENSIONS
1652 	if (bigD) {
1653 		if (defaultfield) {
1654 			checkeuid();
1655 			return setdefaults(&u) ? EXIT_SUCCESS : EXIT_FAILURE;
1656 		}
1657 		(void) printf("preserve\t%s\n", (u.u_preserve) ? "true" : "false");
1658 		return EXIT_SUCCESS;
1659 	}
1660 #endif
1661 	argc -= optind;
1662 	argv += optind;
1663 	if (argc != 1) {
1664 		usermgmt_usage("userdel");
1665 	}
1666 	checkeuid();
1667 	if ((pwp = getpwnam(*argv)) == NULL) {
1668 		warnx("No such user `%s'", *argv);
1669 		return EXIT_FAILURE;
1670 	}
1671 	if (rmhome)
1672 		(void)removehomedir(pwp->pw_name, pwp->pw_uid, pwp->pw_dir);
1673 	if (u.u_preserve) {
1674 		u.u_flags |= F_SHELL;
1675 		memsave(&u.u_shell, NOLOGIN, strlen(NOLOGIN));
1676 		(void) memset(password, '*', PasswordLength);
1677 		password[PasswordLength] = '\0';
1678 		memsave(&u.u_password, password, PasswordLength);
1679 		u.u_flags |= F_PASSWORD;
1680 		return moduser(*argv, *argv, &u) ? EXIT_SUCCESS : EXIT_FAILURE;
1681 	}
1682 	return moduser(*argv, *argv, NULL) ? EXIT_SUCCESS : EXIT_FAILURE;
1683 }
1684 
1685 #ifdef EXTENSIONS
1686 #define GROUP_ADD_OPT_EXTENSIONS	"v"
1687 #else
1688 #define GROUP_ADD_OPT_EXTENSIONS
1689 #endif
1690 
1691 /* add a group */
1692 int
1693 groupadd(int argc, char **argv)
1694 {
1695 	int	dupgid;
1696 	int	gid;
1697 	int	c;
1698 
1699 	gid = -1;
1700 	dupgid = 0;
1701 	while ((c = getopt(argc, argv, "g:o" GROUP_ADD_OPT_EXTENSIONS)) != -1) {
1702 		switch(c) {
1703 		case 'g':
1704 			if (!is_number(optarg)) {
1705 				errx(EXIT_FAILURE, "When using [-g gid], the gid must be numeric");
1706 			}
1707 			gid = atoi(optarg);
1708 			break;
1709 		case 'o':
1710 			dupgid = 1;
1711 			break;
1712 #ifdef EXTENSIONS
1713 		case 'v':
1714 			verbose = 1;
1715 			break;
1716 #endif
1717 		}
1718 	}
1719 	argc -= optind;
1720 	argv += optind;
1721 	if (argc != 1) {
1722 		usermgmt_usage("groupadd");
1723 	}
1724 	checkeuid();
1725 	if (gid < 0 && !getnextgid(&gid, LowGid, HighGid)) {
1726 		err(EXIT_FAILURE, "can't add group: can't get next gid");
1727 	}
1728 	if (!dupgid && getgrgid((gid_t) gid) != NULL) {
1729 		errx(EXIT_FAILURE, "can't add group: gid %d is a duplicate", gid);
1730 	}
1731 	if (!valid_group(*argv)) {
1732 		warnx("warning - invalid group name `%s'", *argv);
1733 	}
1734 	if (!creategid(*argv, gid, "")) {
1735 		errx(EXIT_FAILURE, "can't add group: problems with %s file", _PATH_GROUP);
1736 	}
1737 	return EXIT_SUCCESS;
1738 }
1739 
1740 #ifdef EXTENSIONS
1741 #define GROUP_DEL_OPT_EXTENSIONS	"v"
1742 #else
1743 #define GROUP_DEL_OPT_EXTENSIONS
1744 #endif
1745 
1746 /* remove a group */
1747 int
1748 groupdel(int argc, char **argv)
1749 {
1750 	int	c;
1751 
1752 	while ((c = getopt(argc, argv, "" GROUP_DEL_OPT_EXTENSIONS)) != -1) {
1753 		switch(c) {
1754 #ifdef EXTENSIONS
1755 		case 'v':
1756 			verbose = 1;
1757 			break;
1758 #endif
1759 		}
1760 	}
1761 	argc -= optind;
1762 	argv += optind;
1763 	if (argc != 1) {
1764 		usermgmt_usage("groupdel");
1765 	}
1766 	checkeuid();
1767 	if (!modify_gid(*argv, NULL)) {
1768 		err(EXIT_FAILURE, "can't change %s file", _PATH_GROUP);
1769 	}
1770 	return EXIT_SUCCESS;
1771 }
1772 
1773 #ifdef EXTENSIONS
1774 #define GROUP_MOD_OPT_EXTENSIONS	"v"
1775 #else
1776 #define GROUP_MOD_OPT_EXTENSIONS
1777 #endif
1778 
1779 /* modify a group */
1780 int
1781 groupmod(int argc, char **argv)
1782 {
1783 	struct group	*grp;
1784 	char		buf[MaxEntryLen];
1785 	char		*newname;
1786 	char		**cpp;
1787 	int		dupgid;
1788 	int		gid;
1789 	int		cc;
1790 	int		c;
1791 
1792 	gid = -1;
1793 	dupgid = 0;
1794 	newname = NULL;
1795 	while ((c = getopt(argc, argv, "g:on:" GROUP_MOD_OPT_EXTENSIONS)) != -1) {
1796 		switch(c) {
1797 		case 'g':
1798 			if (!is_number(optarg)) {
1799 				errx(EXIT_FAILURE, "When using [-g gid], the gid must be numeric");
1800 			}
1801 			gid = atoi(optarg);
1802 			break;
1803 		case 'o':
1804 			dupgid = 1;
1805 			break;
1806 		case 'n':
1807 			memsave(&newname, optarg, strlen(optarg));
1808 			break;
1809 #ifdef EXTENSIONS
1810 		case 'v':
1811 			verbose = 1;
1812 			break;
1813 #endif
1814 		}
1815 	}
1816 	argc -= optind;
1817 	argv += optind;
1818 	if (argc != 1) {
1819 		usermgmt_usage("groupmod");
1820 	}
1821 	checkeuid();
1822 	if (gid < 0 && newname == NULL) {
1823 		err(EXIT_FAILURE, "Nothing to change");
1824 	}
1825 	if (dupgid && gid < 0) {
1826 		err(EXIT_FAILURE, "Duplicate which gid?");
1827 	}
1828 	if ((grp = getgrnam(*argv)) == NULL) {
1829 		err(EXIT_FAILURE, "can't find group `%s' to modify", *argv);
1830 	}
1831 	if (newname != NULL && !valid_group(newname)) {
1832 		warn("warning - invalid group name `%s'", newname);
1833 	}
1834 	cc = snprintf(buf, sizeof(buf), "%s:%s:%d:",
1835 			(newname) ? newname : grp->gr_name,
1836 			grp->gr_passwd,
1837 			(gid < 0) ? grp->gr_gid : gid);
1838 	for (cpp = grp->gr_mem ; *cpp && cc < sizeof(buf) ; cpp++) {
1839 		cc += snprintf(&buf[cc], sizeof(buf) - cc, "%s%s", *cpp,
1840 			(cpp[1] == NULL) ? "" : ",");
1841 	}
1842 	cc += snprintf(&buf[cc], sizeof(buf) - cc, "\n");
1843 	if (!modify_gid(*argv, buf)) {
1844 		err(EXIT_FAILURE, "can't change %s file", _PATH_GROUP);
1845 	}
1846 	return EXIT_SUCCESS;
1847 }
1848 
1849 #ifdef EXTENSIONS
1850 /* display user information */
1851 int
1852 userinfo(int argc, char **argv)
1853 {
1854 	struct passwd	*pwp;
1855 	struct group	*grp;
1856 	char		buf[MaxEntryLen];
1857 	char		**cpp;
1858 	int		exists;
1859 	int		cc;
1860 	int		i;
1861 
1862 	exists = 0;
1863 	while ((i = getopt(argc, argv, "ev")) != -1) {
1864 		switch(i) {
1865 		case 'e':
1866 			exists = 1;
1867 			break;
1868 		case 'v':
1869 			verbose = 1;
1870 			break;
1871 		}
1872 	}
1873 	argc -= optind;
1874 	argv += optind;
1875 	if (argc != 1) {
1876 		usermgmt_usage("userinfo");
1877 	}
1878 	pwp = find_user_info(*argv);
1879 	if (exists) {
1880 		exit((pwp) ? EXIT_SUCCESS : EXIT_FAILURE);
1881 	}
1882 	if (pwp == NULL) {
1883 		errx(EXIT_FAILURE, "can't find user `%s'", *argv);
1884 	}
1885 	(void) printf("login\t%s\n", pwp->pw_name);
1886 	(void) printf("passwd\t%s\n", pwp->pw_passwd);
1887 	(void) printf("uid\t%d\n", pwp->pw_uid);
1888 	for (cc = 0 ; (grp = getgrent()) != NULL ; ) {
1889 		for (cpp = grp->gr_mem ; *cpp ; cpp++) {
1890 			if (strcmp(*cpp, *argv) == 0 && grp->gr_gid != pwp->pw_gid) {
1891 				cc += snprintf(&buf[cc], sizeof(buf) - cc, "%s ", grp->gr_name);
1892 			}
1893 		}
1894 	}
1895 	if ((grp = getgrgid(pwp->pw_gid)) == NULL) {
1896 		(void) printf("groups\t%d %s\n", pwp->pw_gid, buf);
1897 	} else {
1898 		(void) printf("groups\t%s %s\n", grp->gr_name, buf);
1899 	}
1900 	(void) printf("change\t%s", pwp->pw_change ? ctime(&pwp->pw_change) : "NEVER\n");
1901 #ifdef EXTENSIONS
1902 	(void) printf("class\t%s\n", pwp->pw_class);
1903 #endif
1904 	(void) printf("gecos\t%s\n", pwp->pw_gecos);
1905 	(void) printf("dir\t%s\n", pwp->pw_dir);
1906 	(void) printf("shell\t%s\n", pwp->pw_shell);
1907 	(void) printf("expire\t%s", pwp->pw_expire ? ctime(&pwp->pw_expire) : "NEVER\n");
1908 	return EXIT_SUCCESS;
1909 }
1910 #endif
1911 
1912 #ifdef EXTENSIONS
1913 /* display user information */
1914 int
1915 groupinfo(int argc, char **argv)
1916 {
1917 	struct group	*grp;
1918 	char		**cpp;
1919 	int		exists;
1920 	int		i;
1921 
1922 	exists = 0;
1923 	while ((i = getopt(argc, argv, "ev")) != -1) {
1924 		switch(i) {
1925 		case 'e':
1926 			exists = 1;
1927 			break;
1928 		case 'v':
1929 			verbose = 1;
1930 			break;
1931 		}
1932 	}
1933 	argc -= optind;
1934 	argv += optind;
1935 	if (argc != 1) {
1936 		usermgmt_usage("groupinfo");
1937 	}
1938 	grp = find_group_info(*argv);
1939 	if (exists) {
1940 		exit((grp) ? EXIT_SUCCESS : EXIT_FAILURE);
1941 	}
1942 	if (grp == NULL) {
1943 		errx(EXIT_FAILURE, "can't find group `%s'", *argv);
1944 	}
1945 	(void) printf("name\t%s\n", grp->gr_name);
1946 	(void) printf("passwd\t%s\n", grp->gr_passwd);
1947 	(void) printf("gid\t%d\n", grp->gr_gid);
1948 	(void) printf("members\t");
1949 	for (cpp = grp->gr_mem ; *cpp ; cpp++) {
1950 		(void) printf("%s ", *cpp);
1951 	}
1952 	(void) fputc('\n', stdout);
1953 	return EXIT_SUCCESS;
1954 }
1955 #endif
1956