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