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