Home | History | Annotate | Line # | Download | only in user
user.c revision 1.47
      1 /* $NetBSD: user.c,v 1.47 2002/03/31 21:31:10 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.47 2002/03/31 21:31:10 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 (at) 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 		warn("can't create gid: can't rename `%s' to `%s'", f, _PATH_GROUP);
    453 		return 0;
    454 	}
    455 	(void) chmod(_PATH_GROUP, st.st_mode & 07777);
    456 	return 1;
    457 }
    458 
    459 /* modify the group entry with name `group' to be newent */
    460 static int
    461 modify_gid(char *group, char *newent)
    462 {
    463 	struct stat	st;
    464 	FILE		*from;
    465 	FILE		*to;
    466 	char		buf[MaxEntryLen];
    467 	char		f[MaxFileNameLen];
    468 	char		*colon;
    469 	int		groupc;
    470 	int		entc;
    471 	int		fd;
    472 	int		cc;
    473 
    474 	if ((from = fopen(_PATH_GROUP, "r")) == NULL) {
    475 		warn("can't create gid for %s: can't open %s", group, _PATH_GROUP);
    476 		return 0;
    477 	}
    478 	if (flock(fileno(from), LOCK_EX | LOCK_NB) < 0) {
    479 		warn("can't lock `%s'", _PATH_GROUP);
    480 	}
    481 	(void) fstat(fileno(from), &st);
    482 	(void) snprintf(f, sizeof(f), "%s.XXXXXX", _PATH_GROUP);
    483 	if ((fd = mkstemp(f)) < 0) {
    484 		(void) fclose(from);
    485 		warn("can't create gid: mkstemp failed");
    486 		return 0;
    487 	}
    488 	if ((to = fdopen(fd, "w")) == NULL) {
    489 		(void) fclose(from);
    490 		(void) close(fd);
    491 		(void) unlink(f);
    492 		warn("can't create gid: fdopen `%s' failed", f);
    493 		return 0;
    494 	}
    495 	groupc = strlen(group);
    496 	while (fgets(buf, sizeof(buf), from) != NULL) {
    497 		cc = strlen(buf);
    498 		if ((colon = strchr(buf, ':')) == NULL) {
    499 			warn("badly formed entry `%s'", buf);
    500 			continue;
    501 		}
    502 		entc = (int)(colon - buf);
    503 		if (entc == groupc && strncmp(group, buf, (unsigned) entc) == 0) {
    504 			if (newent == NULL) {
    505 				continue;
    506 			} else {
    507 				cc = strlen(newent);
    508 				(void) strlcpy(buf, newent, sizeof(buf));
    509 			}
    510 		}
    511 		if (fwrite(buf, sizeof(char), (unsigned) cc, to) != cc) {
    512 			(void) fclose(from);
    513 			(void) close(fd);
    514 			(void) unlink(f);
    515 			warn("can't create gid: short write to `%s'", f);
    516 			return 0;
    517 		}
    518 	}
    519 	(void) fclose(from);
    520 	(void) fclose(to);
    521 	if (rename(f, _PATH_GROUP) < 0) {
    522 		warn("can't create gid: can't rename `%s' to `%s'", f, _PATH_GROUP);
    523 		return 0;
    524 	}
    525 	(void) chmod(_PATH_GROUP, st.st_mode & 07777);
    526 	return 1;
    527 }
    528 
    529 /* modify the group entries for all `groups', by adding `user' */
    530 static int
    531 append_group(char *user, int ngroups, char **groups)
    532 {
    533 	struct group	*grp;
    534 	struct stat	st;
    535 	FILE		*from;
    536 	FILE		*to;
    537 	char		buf[MaxEntryLen];
    538 	char		f[MaxFileNameLen];
    539 	char		*colon;
    540 	int		groupc;
    541 	int		entc;
    542 	int		fd;
    543 	int		nc;
    544 	int		cc;
    545 	int		i;
    546 	int		j;
    547 
    548 	for (i = 0 ; i < ngroups ; i++) {
    549 		if ((grp = getgrnam(groups[i])) == NULL) {
    550 			warnx("can't append group `%s' for user `%s'", groups[i], user);
    551 		} else {
    552 			for (j = 0 ; grp->gr_mem[j] ; j++) {
    553 				if (strcmp(user, grp->gr_mem[j]) == 0) {
    554 					/* already in it */
    555 					groups[i] = "";
    556 				}
    557 			}
    558 		}
    559 	}
    560 	if ((from = fopen(_PATH_GROUP, "r")) == NULL) {
    561 		warn("can't append group for %s: can't open %s", user, _PATH_GROUP);
    562 		return 0;
    563 	}
    564 	if (flock(fileno(from), LOCK_EX | LOCK_NB) < 0) {
    565 		warn("can't lock `%s'", _PATH_GROUP);
    566 	}
    567 	(void) fstat(fileno(from), &st);
    568 	(void) snprintf(f, sizeof(f), "%s.XXXXXX", _PATH_GROUP);
    569 	if ((fd = mkstemp(f)) < 0) {
    570 		(void) fclose(from);
    571 		warn("can't create gid: mkstemp failed");
    572 		return 0;
    573 	}
    574 	if ((to = fdopen(fd, "w")) == NULL) {
    575 		(void) fclose(from);
    576 		(void) close(fd);
    577 		(void) unlink(f);
    578 		warn("can't create gid: fdopen `%s' failed", f);
    579 		return 0;
    580 	}
    581 	while (fgets(buf, sizeof(buf), from) != NULL) {
    582 		cc = strlen(buf);
    583 		if ((colon = strchr(buf, ':')) == NULL) {
    584 			warn("badly formed entry `%s'", buf);
    585 			continue;
    586 		}
    587 		entc = (int)(colon - buf);
    588 		for (i = 0 ; i < ngroups ; i++) {
    589 			if ((groupc = strlen(groups[i])) == 0) {
    590 				continue;
    591 			}
    592 			if (entc == groupc && strncmp(groups[i], buf, (unsigned) entc) == 0) {
    593 				if ((nc = snprintf(&buf[cc - 1],
    594 						sizeof(buf) - cc + 1,
    595 						"%s%s\n",
    596 						(buf[cc - 2] == ':') ? "" : ",",
    597 						user)) < 0) {
    598 					warnx("Warning: group `%s' entry too long", groups[i]);
    599 				}
    600 				cc += nc - 1;
    601 			}
    602 		}
    603 		if (fwrite(buf, sizeof(char), (unsigned) cc, to) != cc) {
    604 			(void) fclose(from);
    605 			(void) close(fd);
    606 			(void) unlink(f);
    607 			warn("can't create gid: short write to `%s'", f);
    608 			return 0;
    609 		}
    610 	}
    611 	(void) fclose(from);
    612 	(void) fclose(to);
    613 	if (rename(f, _PATH_GROUP) < 0) {
    614 		warn("can't create gid: can't rename `%s' to `%s'", f, _PATH_GROUP);
    615 		return 0;
    616 	}
    617 	(void) chmod(_PATH_GROUP, st.st_mode & 07777);
    618 	return 1;
    619 }
    620 
    621 /* return 1 if `login' is a valid login name */
    622 static int
    623 valid_login(char *login)
    624 {
    625 	char	*cp;
    626 
    627 	for (cp = login ; *cp ; cp++) {
    628 		if (!isalnum(*cp) && *cp != '.' && *cp != '_' && *cp != '-') {
    629 			return 0;
    630 		}
    631 	}
    632 	return 1;
    633 }
    634 
    635 /* return 1 if `group' is a valid group name */
    636 static int
    637 valid_group(char *group)
    638 {
    639 	char	*cp;
    640 
    641 	for (cp = group ; *cp ; cp++) {
    642 		if (!isalnum(*cp)) {
    643 			return 0;
    644 		}
    645 	}
    646 	return 1;
    647 }
    648 
    649 /* find the next gid in the range lo .. hi */
    650 static int
    651 getnextgid(int *gidp, int lo, int hi)
    652 {
    653 	for (*gidp = lo ; *gidp < hi ; *gidp += 1) {
    654 		if (getgrgid((gid_t)*gidp) == NULL) {
    655 			return 1;
    656 		}
    657 	}
    658 	return 0;
    659 }
    660 
    661 #ifdef EXTENSIONS
    662 /* save a range of uids */
    663 static int
    664 save_range(user_t *up, char *cp)
    665 {
    666 	int	from;
    667 	int	to;
    668 	int	i;
    669 
    670 	if (up->u_rsize == 0) {
    671 		up->u_rsize = 32;
    672 		NEWARRAY(range_t, up->u_rv, up->u_rsize, return(0));
    673 	} else if (up->u_rc == up->u_rsize) {
    674 		up->u_rsize *= 2;
    675 		RENEW(range_t, up->u_rv, up->u_rsize, return(0));
    676 	}
    677 	if (up->u_rv && sscanf(cp, "%d..%d", &from, &to) == 2) {
    678 		for (i = 0 ; i < up->u_rc ; i++) {
    679 			if (up->u_rv[i].r_from == from && up->u_rv[i].r_to == to) {
    680 				break;
    681 			}
    682 		}
    683 		if (i == up->u_rc) {
    684 			up->u_rv[up->u_rc].r_from = from;
    685 			up->u_rv[up->u_rc].r_to = to;
    686 			up->u_rc += 1;
    687 		}
    688 	} else {
    689 		warnx("Bad range `%s'", cp);
    690 		return 0;
    691 	}
    692 	return 1;
    693 }
    694 #endif
    695 
    696 /* set the defaults in the defaults file */
    697 static int
    698 setdefaults(user_t *up)
    699 {
    700 	char	template[MaxFileNameLen];
    701 	FILE	*fp;
    702 	int	ret;
    703 	int	fd;
    704 #ifdef EXTENSIONS
    705 	int	i;
    706 #endif
    707 
    708 	(void) snprintf(template, sizeof(template), "%s.XXXXXX", CONFFILE);
    709 	if ((fd = mkstemp(template)) < 0) {
    710 		warnx("can't mkstemp `%s' for writing", CONFFILE);
    711 		return 0;
    712 	}
    713 	if ((fp = fdopen(fd, "w")) == NULL) {
    714 		warn("can't fdopen `%s' for writing", CONFFILE);
    715 		return 0;
    716 	}
    717 	ret = 1;
    718 	if (fprintf(fp, "group\t\t%s\n", up->u_primgrp) <= 0 ||
    719 	    fprintf(fp, "base_dir\t%s\n", up->u_basedir) <= 0 ||
    720 	    fprintf(fp, "skel_dir\t%s\n", up->u_skeldir) <= 0 ||
    721 	    fprintf(fp, "shell\t\t%s\n", up->u_shell) <= 0 ||
    722 #ifdef EXTENSIONS
    723 	    fprintf(fp, "class\t\t%s\n", up->u_class) <= 0 ||
    724 #endif
    725 	    fprintf(fp, "inactive\t%d\n", up->u_inactive) <= 0 ||
    726 	    fprintf(fp, "expire\t\t%s\n", (up->u_expire == NULL) ?  UNSET_EXPIRY : up->u_expire) <= 0 ||
    727 	    fprintf(fp, "preserve\t%s\n", (up->u_preserve == 0) ? "false" : "true") <= 0) {
    728 		warn("can't write to `%s'", CONFFILE);
    729 		ret = 0;
    730 	}
    731 #ifdef EXTENSIONS
    732 	for (i = (up->u_defrc != up->u_rc) ? up->u_defrc : 0 ; i < up->u_rc ; i++) {
    733 		if (fprintf(fp, "range\t\t%d..%d\n", up->u_rv[i].r_from, up->u_rv[i].r_to) <= 0) {
    734 			warn("can't write to `%s'", CONFFILE);
    735 			ret = 0;
    736 		}
    737 	}
    738 #endif
    739 	(void) fclose(fp);
    740 	if (ret) {
    741 		ret = ((rename(template, CONFFILE) == 0) && (chmod(CONFFILE, 0644) == 0));
    742 	}
    743 	return ret;
    744 }
    745 
    746 /* read the defaults file */
    747 static void
    748 read_defaults(user_t *up)
    749 {
    750 	struct stat	st;
    751 	size_t		lineno;
    752 	size_t		len;
    753 	FILE		*fp;
    754 	char		*cp;
    755 	char		*s;
    756 
    757 	memsave(&up->u_primgrp, DEF_GROUP, strlen(DEF_GROUP));
    758 	memsave(&up->u_basedir, DEF_BASEDIR, strlen(DEF_BASEDIR));
    759 	memsave(&up->u_skeldir, DEF_SKELDIR, strlen(DEF_SKELDIR));
    760 	memsave(&up->u_shell, DEF_SHELL, strlen(DEF_SHELL));
    761 	memsave(&up->u_comment, DEF_COMMENT, strlen(DEF_COMMENT));
    762 #ifdef EXTENSIONS
    763 	memsave(&up->u_class, DEF_CLASS, strlen(DEF_CLASS));
    764 #endif
    765 	up->u_rsize = 16;
    766 	NEWARRAY(range_t, up->u_rv, up->u_rsize, exit(1));
    767 	up->u_inactive = DEF_INACTIVE;
    768 	up->u_expire = DEF_EXPIRE;
    769 	if ((fp = fopen(CONFFILE, "r")) == NULL) {
    770 		if (stat(CONFFILE, &st) < 0 && !setdefaults(up)) {
    771 			warn("can't create `%s' defaults file", CONFFILE);
    772 		}
    773 		fp = fopen(CONFFILE, "r");
    774 	}
    775 	if (fp != NULL) {
    776 		while ((s = fparseln(fp, &len, &lineno, NULL, 0)) != NULL) {
    777 			if (strncmp(s, "group", 5) == 0) {
    778 				for (cp = s + 5 ; *cp && isspace(*cp) ; cp++) {
    779 				}
    780 				memsave(&up->u_primgrp, cp, strlen(cp));
    781 			} else if (strncmp(s, "base_dir", 8) == 0) {
    782 				for (cp = s + 8 ; *cp && isspace(*cp) ; cp++) {
    783 				}
    784 				memsave(&up->u_basedir, cp, strlen(cp));
    785 			} else if (strncmp(s, "skel_dir", 8) == 0) {
    786 				for (cp = s + 8 ; *cp && isspace(*cp) ; cp++) {
    787 				}
    788 				memsave(&up->u_skeldir, cp, strlen(cp));
    789 			} else if (strncmp(s, "shell", 5) == 0) {
    790 				for (cp = s + 5 ; *cp && isspace(*cp) ; cp++) {
    791 				}
    792 				memsave(&up->u_shell, cp, strlen(cp));
    793 #ifdef EXTENSIONS
    794 			} else if (strncmp(s, "class", 5) == 0) {
    795 				for (cp = s + 5 ; *cp && isspace(*cp) ; cp++) {
    796 				}
    797 				memsave(&up->u_class, cp, strlen(cp));
    798 #endif
    799 			} else if (strncmp(s, "inactive", 8) == 0) {
    800 				for (cp = s + 8 ; *cp && isspace(*cp) ; cp++) {
    801 				}
    802 				up->u_inactive = atoi(cp);
    803 #ifdef EXTENSIONS
    804 			} else if (strncmp(s, "range", 5) == 0) {
    805 				for (cp = s + 5 ; *cp && isspace(*cp) ; cp++) {
    806 				}
    807 				(void) save_range(up, cp);
    808 #endif
    809 #ifdef EXTENSIONS
    810 			} else if (strncmp(s, "preserve", 8) == 0) {
    811 				for (cp = s + 8 ; *cp && isspace(*cp) ; cp++) {
    812 				}
    813 				up->u_preserve = (strncmp(cp, "true", 4) == 0) ? 1 :
    814 						  (strncmp(cp, "yes", 3) == 0) ? 1 :
    815 						   atoi(cp);
    816 #endif
    817 			} else if (strncmp(s, "expire", 6) == 0) {
    818 				for (cp = s + 6 ; *cp && isspace(*cp) ; cp++) {
    819 				}
    820 				if (strcmp(cp, UNSET_EXPIRY) == 0) {
    821 					if (up->u_expire) {
    822 						FREE(up->u_expire);
    823 					}
    824 					up->u_expire = NULL;
    825 				} else {
    826 					memsave(&up->u_expire, cp, strlen(cp));
    827 				}
    828 			}
    829 			(void) free(s);
    830 		}
    831 		(void) fclose(fp);
    832 	}
    833 	if (up->u_rc == 0) {
    834 		up->u_rv[up->u_rc].r_from = DEF_LOWUID;
    835 		up->u_rv[up->u_rc].r_to = DEF_HIGHUID;
    836 		up->u_rc += 1;
    837 	}
    838 	up->u_defrc = up->u_rc;
    839 }
    840 
    841 /* return the next valid unused uid */
    842 static int
    843 getnextuid(int sync_uid_gid, int *uid, int low_uid, int high_uid)
    844 {
    845 	for (*uid = low_uid ; *uid <= high_uid ; (*uid)++) {
    846 		if (getpwuid((uid_t)(*uid)) == NULL && *uid != NOBODY_UID) {
    847 			if (sync_uid_gid) {
    848 				if (getgrgid((gid_t)(*uid)) == NULL) {
    849 					return 1;
    850 				}
    851 			} else {
    852 				return 1;
    853 			}
    854 		}
    855 	}
    856 	return 0;
    857 }
    858 
    859 /* add a user */
    860 static int
    861 adduser(char *login, user_t *up)
    862 {
    863 	struct group	*grp;
    864 	struct stat	st;
    865 	struct tm	tm;
    866 	time_t		expire;
    867 	char		password[PasswordLength + 1];
    868 	char		home[MaxFileNameLen];
    869 	char		buf[MaxFileNameLen];
    870 	int		sync_uid_gid;
    871 	int		masterfd;
    872 	int		ptmpfd;
    873 	int		gid;
    874 	int		cc;
    875 	int		i;
    876 
    877 	if (!valid_login(login)) {
    878 		errx(EXIT_FAILURE, "`%s' is not a valid login name", login);
    879 	}
    880 	if ((masterfd = open(_PATH_MASTERPASSWD, O_RDONLY)) < 0) {
    881 		err(EXIT_FAILURE, "can't open `%s'", _PATH_MASTERPASSWD);
    882 	}
    883 	if (flock(masterfd, LOCK_EX | LOCK_NB) < 0) {
    884 		err(EXIT_FAILURE, "can't lock `%s'", _PATH_MASTERPASSWD);
    885 	}
    886 	pw_init();
    887 	if ((ptmpfd = pw_lock(WAITSECS)) < 0) {
    888 		(void) close(masterfd);
    889 		err(EXIT_FAILURE, "can't obtain pw_lock");
    890 	}
    891 	while ((cc = read(masterfd, buf, sizeof(buf))) > 0) {
    892 		if (write(ptmpfd, buf, (size_t)(cc)) != cc) {
    893 			(void) close(masterfd);
    894 			(void) close(ptmpfd);
    895 			(void) pw_abort();
    896 			err(EXIT_FAILURE, "short write to /etc/ptmp (not %d chars)", cc);
    897 		}
    898 	}
    899 	/* if no uid was specified, get next one in [low_uid..high_uid] range */
    900 	sync_uid_gid = (strcmp(up->u_primgrp, "=uid") == 0);
    901 	if (up->u_uid == -1) {
    902 		/* default is in index '0' in u_rv array */
    903 		for (i = 1 ; i < up->u_rc ; i++) {
    904 			if (getnextuid(sync_uid_gid, &up->u_uid, up->u_rv[i].r_from, up->u_rv[i].r_to)) {
    905 				break;
    906 			}
    907 		}
    908 		if (i == up->u_rc &&
    909 		    !getnextuid(sync_uid_gid, &up->u_uid, up->u_rv[0].r_from, up->u_rv[0].r_to)) {
    910 			(void) close(ptmpfd);
    911 			(void) pw_abort();
    912 			errx(EXIT_FAILURE, "can't get next uid for %d", up->u_uid);
    913 		}
    914 	}
    915 	/* check uid isn't already allocated */
    916 	if (!(up->u_flags & F_DUPUID) && getpwuid((uid_t)(up->u_uid)) != NULL) {
    917 		(void) close(ptmpfd);
    918 		(void) pw_abort();
    919 		errx(EXIT_FAILURE, "uid %d is already in use", up->u_uid);
    920 	}
    921 	/* if -g=uid was specified, check gid is unused */
    922 	if (sync_uid_gid) {
    923 		if (getgrgid((gid_t)(up->u_uid)) != NULL) {
    924 			(void) close(ptmpfd);
    925 			(void) pw_abort();
    926 			errx(EXIT_FAILURE, "gid %d is already in use", up->u_uid);
    927 		}
    928 		gid = up->u_uid;
    929 	} else if ((grp = getgrnam(up->u_primgrp)) != NULL) {
    930 		gid = grp->gr_gid;
    931 	} else if (is_number(up->u_primgrp) &&
    932 		   (grp = getgrgid((gid_t)atoi(up->u_primgrp))) != NULL) {
    933 		gid = grp->gr_gid;
    934 	} else {
    935 		(void) close(ptmpfd);
    936 		(void) pw_abort();
    937 		errx(EXIT_FAILURE, "group %s not found", up->u_primgrp);
    938 	}
    939 	/* check name isn't already in use */
    940 	if (!(up->u_flags & F_DUPUID) && getpwnam(login) != NULL) {
    941 		(void) close(ptmpfd);
    942 		(void) pw_abort();
    943 		errx(EXIT_FAILURE, "already a `%s' user", login);
    944 	}
    945 	if (up->u_flags & F_HOMEDIR) {
    946 		(void) strlcpy(home, up->u_home, sizeof(home));
    947 	} else {
    948 		/* if home directory hasn't been given, make it up */
    949 		(void) snprintf(home, sizeof(home), "%s/%s", up->u_basedir, login);
    950 	}
    951 	expire = 0;
    952 	if (up->u_expire != NULL) {
    953 		(void) memset(&tm, 0, sizeof(tm));
    954 		if (strptime(up->u_expire, "%c", &tm) == NULL) {
    955 			warnx("invalid time format `%s'", optarg);
    956 		} else {
    957 			expire = mktime(&tm);
    958 		}
    959 	}
    960 	if (lstat(home, &st) < 0 && !(up->u_flags & F_MKDIR)) {
    961 		warnx("Warning: home directory `%s' doesn't exist, and -m was not specified",
    962 		    home);
    963 	}
    964 	password[PasswordLength] = '\0';
    965 	if (up->u_password != NULL &&
    966 	    strlen(up->u_password) == PasswordLength) {
    967 		(void) memcpy(password, up->u_password, PasswordLength);
    968 	} else {
    969 		(void) memset(password, '*', PasswordLength);
    970 		if (up->u_password != NULL) {
    971 			warnx("Password `%s' is invalid: setting it to `%s'",
    972 				up->u_password, password);
    973 		}
    974 	}
    975 	cc = snprintf(buf, sizeof(buf), "%s:%s:%d:%d:%s:%d:%ld:%s:%s:%s\n",
    976 			login,
    977 			password,
    978 			up->u_uid,
    979 			gid,
    980 #ifdef EXTENSIONS
    981 			up->u_class,
    982 #else
    983 			"",
    984 #endif
    985 			up->u_inactive,
    986 			(long) expire,
    987 			up->u_comment,
    988 			home,
    989 			up->u_shell);
    990 	if (write(ptmpfd, buf, (size_t) cc) != cc) {
    991 		(void) close(ptmpfd);
    992 		(void) pw_abort();
    993 		err(EXIT_FAILURE, "can't add `%s'", buf);
    994 	}
    995 	if (up->u_flags & F_MKDIR) {
    996 		if (lstat(home, &st) == 0) {
    997 			(void) close(ptmpfd);
    998 			(void) pw_abort();
    999 			errx(EXIT_FAILURE, "home directory `%s' already exists", home);
   1000 		} else {
   1001 			if (asystem("%s -p %s", MKDIR, home) != 0) {
   1002 				(void) close(ptmpfd);
   1003 				(void) pw_abort();
   1004 				err(EXIT_FAILURE, "can't mkdir `%s'", home);
   1005 			}
   1006 			(void) copydotfiles(up->u_skeldir, up->u_uid, gid, home);
   1007 		}
   1008 	}
   1009 	if (strcmp(up->u_primgrp, "=uid") == 0 &&
   1010 	    getgrnam(login) == NULL &&
   1011 	    !creategid(login, gid, login)) {
   1012 		(void) close(ptmpfd);
   1013 		(void) pw_abort();
   1014 		errx(EXIT_FAILURE, "can't create gid %d for login name %s", gid, login);
   1015 	}
   1016 	if (up->u_groupc > 0 && !append_group(login, up->u_groupc, up->u_groupv)) {
   1017 		(void) close(ptmpfd);
   1018 		(void) pw_abort();
   1019 		errx(EXIT_FAILURE, "can't append `%s' to new groups", login);
   1020 	}
   1021 	(void) close(ptmpfd);
   1022 #if PW_MKDB_ARGC == 2
   1023 	if (pw_mkdb(login, 0) < 0) {
   1024 		err(EXIT_FAILURE, "pw_mkdb failed");
   1025 	}
   1026 #else
   1027 	if (pw_mkdb() < 0) {
   1028 		err(EXIT_FAILURE, "pw_mkdb failed");
   1029 	}
   1030 #endif
   1031 	return 1;
   1032 }
   1033 
   1034 /* modify a user */
   1035 static int
   1036 moduser(char *login, char *newlogin, user_t *up)
   1037 {
   1038 	struct passwd	*pwp;
   1039 	struct group	*grp;
   1040 	struct tm	tm;
   1041 	const char	*homedir;
   1042 	size_t		colonc, len, loginc;
   1043 	size_t		cc;
   1044 	FILE		*master;
   1045 	char		newdir[MaxFileNameLen];
   1046 	char		*buf, *colon, *line;
   1047 	int		masterfd;
   1048 	int		ptmpfd;
   1049 	int		error;
   1050 
   1051 	if (!valid_login(newlogin)) {
   1052 		errx(EXIT_FAILURE, "`%s' is not a valid login name", login);
   1053 	}
   1054 	if ((pwp = getpwnam(login)) == NULL) {
   1055 		errx(EXIT_FAILURE, "No such user `%s'", login);
   1056 	}
   1057 	/* keep dir name in case we need it for '-m' */
   1058 	homedir = pwp->pw_dir;
   1059 
   1060 	if ((masterfd = open(_PATH_MASTERPASSWD, O_RDONLY)) < 0) {
   1061 		err(EXIT_FAILURE, "can't open `%s'", _PATH_MASTERPASSWD);
   1062 	}
   1063 	if (flock(masterfd, LOCK_EX | LOCK_NB) < 0) {
   1064 		err(EXIT_FAILURE, "can't lock `%s'", _PATH_MASTERPASSWD);
   1065 	}
   1066 	pw_init();
   1067 	if ((ptmpfd = pw_lock(WAITSECS)) < 0) {
   1068 		(void) close(masterfd);
   1069 		err(EXIT_FAILURE, "can't obtain pw_lock");
   1070 	}
   1071 	if ((master = fdopen(masterfd, "r")) == NULL) {
   1072 		(void) close(masterfd);
   1073 		(void) close(ptmpfd);
   1074 		(void) pw_abort();
   1075 		err(EXIT_FAILURE, "can't fdopen fd for %s", _PATH_MASTERPASSWD);
   1076 	}
   1077 	if (up != NULL) {
   1078 		if (up->u_flags & F_USERNAME) {
   1079 			/* if changing name, check new name isn't already in use */
   1080 			if (strcmp(login, newlogin) != 0 && getpwnam(newlogin) != NULL) {
   1081 				(void) close(ptmpfd);
   1082 				(void) pw_abort();
   1083 				errx(EXIT_FAILURE, "already a `%s' user", newlogin);
   1084 			}
   1085 			pwp->pw_name = newlogin;
   1086 
   1087 			/*
   1088 			 * Provide a new directory name in case the
   1089 			 * home directory is to be moved.
   1090 			 */
   1091 			if (up->u_flags & F_MKDIR) {
   1092 				snprintf(newdir, sizeof(newdir), "%s/%s", up->u_basedir, newlogin);
   1093 				pwp->pw_dir = newdir;
   1094 			}
   1095 		}
   1096 		if (up->u_flags & F_PASSWORD) {
   1097 			if (up->u_password != NULL && strlen(up->u_password) == PasswordLength)
   1098 				pwp->pw_passwd = up->u_password;
   1099 		}
   1100 		if (up->u_flags & F_UID) {
   1101 			/* check uid isn't already allocated */
   1102 			if (!(up->u_flags & F_DUPUID) && getpwuid((uid_t)(up->u_uid)) != NULL) {
   1103 				(void) close(ptmpfd);
   1104 				(void) pw_abort();
   1105 				errx(EXIT_FAILURE, "uid %d is already in use", up->u_uid);
   1106 			}
   1107 			pwp->pw_uid = up->u_uid;
   1108 		}
   1109 		if (up->u_flags & F_GROUP) {
   1110 			/* if -g=uid was specified, check gid is unused */
   1111 			if (strcmp(up->u_primgrp, "=uid") == 0) {
   1112 				if (getgrgid((gid_t)(up->u_uid)) != NULL) {
   1113 					(void) close(ptmpfd);
   1114 					(void) pw_abort();
   1115 					errx(EXIT_FAILURE, "gid %d is already in use", up->u_uid);
   1116 				}
   1117 				pwp->pw_gid = up->u_uid;
   1118 			} else if ((grp = getgrnam(up->u_primgrp)) != NULL) {
   1119 				pwp->pw_gid = grp->gr_gid;
   1120 			} else if (is_number(up->u_primgrp) &&
   1121 				   (grp = getgrgid((gid_t)atoi(up->u_primgrp))) != NULL) {
   1122 				pwp->pw_gid = grp->gr_gid;
   1123 			} else {
   1124 				(void) close(ptmpfd);
   1125 				(void) pw_abort();
   1126 				errx(EXIT_FAILURE, "group %s not found", up->u_primgrp);
   1127 			}
   1128 		}
   1129 		if (up->u_flags |= F_INACTIVE)
   1130 			pwp->pw_change = up->u_inactive;
   1131 		if (up->u_flags & F_EXPIRE) {
   1132 			(void) memset(&tm, 0, sizeof(tm));
   1133 			if (strptime(up->u_expire, "%c", &tm) == NULL)
   1134 				warnx("invalid time format `%s'", optarg);
   1135 			else
   1136 				pwp->pw_expire = mktime(&tm);
   1137 		}
   1138 		if (up->u_flags & F_COMMENT)
   1139 			pwp->pw_gecos = up->u_comment;
   1140 		if (up->u_flags & F_HOMEDIR)
   1141 			pwp->pw_dir = up->u_home;
   1142 		if (up->u_flags & F_SHELL)
   1143 			pwp->pw_shell = up->u_shell;
   1144 #ifdef EXTENSIONS
   1145 		if (up->u_flags & F_CLASS)
   1146 			pwp->pw_class = up->u_class;
   1147 #endif
   1148 	}
   1149 	loginc = strlen(login);
   1150 	while ((line = fgetln(master, &len)) != NULL) {
   1151 		if ((colon = strchr(line, ':')) == NULL) {
   1152 			warnx("Malformed entry `%s'. Skipping", line);
   1153 			continue;
   1154 		}
   1155 		colonc = (size_t)(colon - line);
   1156 		if (strncmp(login, line, loginc) == 0 && loginc == colonc) {
   1157 			if (up != NULL) {
   1158 				len = (int)asprintf(&buf, "%s:%s:%d:%d:"
   1159 #ifdef EXTENSIONS
   1160 									 "%s"
   1161 #endif
   1162 									      ":%ld:%ld:%s:%s:%s\n",
   1163 					newlogin,
   1164 					pwp->pw_passwd,
   1165 					pwp->pw_uid,
   1166 					pwp->pw_gid,
   1167 #ifdef EXTENSIONS
   1168 					pwp->pw_class,
   1169 #endif
   1170 					(long)pwp->pw_change,
   1171 					(long)pwp->pw_expire,
   1172 					pwp->pw_gecos,
   1173 					pwp->pw_dir,
   1174 					pwp->pw_shell);
   1175 				if (write(ptmpfd, buf, len) != len) {
   1176 					(void) close(ptmpfd);
   1177 					(void) pw_abort();
   1178 					err(EXIT_FAILURE, "can't add `%s'", buf);
   1179 				}
   1180 				(void) free(buf);
   1181 			}
   1182 		} else if ((cc = write(ptmpfd, line, len)) != len) {
   1183 			(void) close(masterfd);
   1184 			(void) close(ptmpfd);
   1185 			(void) pw_abort();
   1186 			err(EXIT_FAILURE, "short write to /etc/ptmp (%lld not %lld chars)",
   1187 				(long long)cc,
   1188 				(long long)len);
   1189 		}
   1190 	}
   1191 	if (up != NULL) {
   1192 		if ((up->u_flags & F_MKDIR) &&
   1193 		    asystem("%s %s %s", MV, homedir, pwp->pw_dir) != 0) {
   1194 			(void) close(ptmpfd);
   1195 			(void) pw_abort();
   1196 			err(EXIT_FAILURE, "can't move `%s' to `%s'",
   1197 				homedir, pwp->pw_dir);
   1198 		}
   1199 		if (up->u_groupc > 0 &&
   1200 		    !append_group(newlogin, up->u_groupc, up->u_groupv)) {
   1201 			(void) close(ptmpfd);
   1202 			(void) pw_abort();
   1203 			errx(EXIT_FAILURE, "can't append `%s' to new groups",
   1204 				newlogin);
   1205 		}
   1206 	}
   1207 	(void) close(ptmpfd);
   1208 #if PW_MKDB_ARGC == 2
   1209 	if (up != NULL && strcmp(login, newlogin) == 0) {
   1210 		error = pw_mkdb(login, 0);
   1211 	} else {
   1212 		error = pw_mkdb(NULL, 0);
   1213 	}
   1214 #else
   1215 	error = pw_mkdb();
   1216 #endif
   1217 	if (error < 0) {
   1218 		err(EXIT_FAILURE, "pw_mkdb failed");
   1219 	}
   1220 
   1221 	return 1;
   1222 }
   1223 
   1224 
   1225 #ifdef EXTENSIONS
   1226 /* see if we can find out the user struct */
   1227 static struct passwd *
   1228 find_user_info(char *name)
   1229 {
   1230 	struct passwd	*pwp;
   1231 
   1232 	if ((pwp = getpwnam(name)) != NULL) {
   1233 		return pwp;
   1234 	}
   1235 	if (is_number(name) && (pwp = getpwuid((uid_t)atoi(name))) != NULL) {
   1236 		return pwp;
   1237 	}
   1238 	return NULL;
   1239 }
   1240 #endif
   1241 
   1242 #ifdef EXTENSIONS
   1243 /* see if we can find out the group struct */
   1244 static struct group *
   1245 find_group_info(char *name)
   1246 {
   1247 	struct group	*grp;
   1248 
   1249 	if ((grp = getgrnam(name)) != NULL) {
   1250 		return grp;
   1251 	}
   1252 	if (is_number(name) && (grp = getgrgid((gid_t)atoi(name))) != NULL) {
   1253 		return grp;
   1254 	}
   1255 	return NULL;
   1256 }
   1257 #endif
   1258 
   1259 /* print out usage message, and then exit */
   1260 void
   1261 usermgmt_usage(const char *prog)
   1262 {
   1263 	if (strcmp(prog, "useradd") == 0) {
   1264 		(void) fprintf(stderr, "Usage: %s -D [-b basedir] [-e expiry] "
   1265 		    "[-f inactive] [-g group]\n\t[-r lowuid..highuid] "
   1266 		    "[-s shell] [-L class]\n", prog);
   1267 		(void) fprintf(stderr, "Usage: %s [-G group] [-b basedir] "
   1268 		    "[-c comment] [-d homedir] [-e expiry]\n\t[-f inactive] "
   1269 		    "[-g group] [-k skeletondir] [-m] [-o] [-p password]\n"
   1270 		    "\t[-r lowuid..highuid] [-s shell]\n\t[-u uid] [-v] user\n",
   1271 		    prog);
   1272 	} else if (strcmp(prog, "usermod") == 0) {
   1273 		(void) fprintf(stderr, "Usage: %s [-G group] [-c comment] "
   1274 		    "[-d homedir] [-e expire] [-f inactive]\n\t[-g group] "
   1275 		    "[-l newname] [-m] [-o] [-p password] [-s shell] [-u uid]\n"
   1276 		    "\t[-L class] [-v] user\n", prog);
   1277 	} else if (strcmp(prog, "userdel") == 0) {
   1278 		(void) fprintf(stderr, "Usage: %s -D [-p preserve]\n", prog);
   1279 		(void) fprintf(stderr,
   1280 		    "Usage: %s [-p preserve] [-r] [-v] user\n", prog);
   1281 #ifdef EXTENSIONS
   1282 	} else if (strcmp(prog, "userinfo") == 0) {
   1283 		(void) fprintf(stderr, "Usage: %s [-e] [-v] user\n", prog);
   1284 #endif
   1285 	} else if (strcmp(prog, "groupadd") == 0) {
   1286 		(void) fprintf(stderr, "Usage: %s [-g gid] [-o] [-v] group\n",
   1287 		    prog);
   1288 	} else if (strcmp(prog, "groupdel") == 0) {
   1289 		(void) fprintf(stderr, "Usage: %s [-v] group\n", prog);
   1290 	} else if (strcmp(prog, "groupmod") == 0) {
   1291 		(void) fprintf(stderr,
   1292 		    "Usage: %s [-g gid] [-o] [-n newname] [-v] group\n", prog);
   1293 	} else if (strcmp(prog, "user") == 0 || strcmp(prog, "group") == 0) {
   1294 		(void) fprintf(stderr,
   1295 		    "Usage: %s ( add | del | mod | info ) ...\n", prog);
   1296 #ifdef EXTENSIONS
   1297 	} else if (strcmp(prog, "groupinfo") == 0) {
   1298 		(void) fprintf(stderr, "Usage: %s [-e] [-v] group\n", prog);
   1299 #endif
   1300 	}
   1301 	exit(EXIT_FAILURE);
   1302 	/* NOTREACHED */
   1303 }
   1304 
   1305 #ifdef EXTENSIONS
   1306 #define ADD_OPT_EXTENSIONS	"p:r:vL:"
   1307 #else
   1308 #define ADD_OPT_EXTENSIONS
   1309 #endif
   1310 
   1311 int
   1312 useradd(int argc, char **argv)
   1313 {
   1314 	user_t	u;
   1315 	int	defaultfield;
   1316 	int	bigD;
   1317 	int	c;
   1318 #ifdef EXTENSIONS
   1319 	int	i;
   1320 #endif
   1321 
   1322 	(void) memset(&u, 0, sizeof(u));
   1323 	read_defaults(&u);
   1324 	u.u_uid = -1;
   1325 	defaultfield = bigD = 0;
   1326 	while ((c = getopt(argc, argv, "DG:b:c:d:e:f:g:k:mou:s:" ADD_OPT_EXTENSIONS)) != -1) {
   1327 		switch(c) {
   1328 		case 'D':
   1329 			bigD = 1;
   1330 			break;
   1331 		case 'G':
   1332 			while ((u.u_groupv[u.u_groupc] = strsep(&optarg, ",")) != NULL &&
   1333 			       u.u_groupc < NGROUPS_MAX) {
   1334 				if (u.u_groupv[u.u_groupc][0] != 0) {
   1335 					u.u_groupc++;
   1336 				}
   1337 			}
   1338 			if (optarg != NULL) {
   1339 				warnx("Truncated list of secondary groups to %d entries", NGROUPS_MAX);
   1340 			}
   1341 			break;
   1342 		case 'b':
   1343 			defaultfield = 1;
   1344 			memsave(&u.u_basedir, optarg, strlen(optarg));
   1345 			break;
   1346 		case 'c':
   1347 			memsave(&u.u_comment, optarg, strlen(optarg));
   1348 			break;
   1349 		case 'd':
   1350 			memsave(&u.u_home, optarg, strlen(optarg));
   1351 			u.u_flags |= F_HOMEDIR;
   1352 			break;
   1353 		case 'e':
   1354 			defaultfield = 1;
   1355 			memsave(&u.u_expire, optarg, strlen(optarg));
   1356 			break;
   1357 		case 'f':
   1358 			defaultfield = 1;
   1359 			u.u_inactive = atoi(optarg);
   1360 			break;
   1361 		case 'g':
   1362 			defaultfield = 1;
   1363 			memsave(&u.u_primgrp, optarg, strlen(optarg));
   1364 			break;
   1365 		case 'k':
   1366 			memsave(&u.u_skeldir, optarg, strlen(optarg));
   1367 			break;
   1368 #ifdef EXTENSIONS
   1369 		case 'L':
   1370 			defaultfield = 1;
   1371 			memsave(&u.u_class, optarg, strlen(optarg));
   1372 			break;
   1373 #endif
   1374 		case 'm':
   1375 			u.u_flags |= F_MKDIR;
   1376 			break;
   1377 		case 'o':
   1378 			u.u_flags |= F_DUPUID;
   1379 			break;
   1380 #ifdef EXTENSIONS
   1381 		case 'p':
   1382 			memsave(&u.u_password, optarg, strlen(optarg));
   1383 			break;
   1384 #endif
   1385 #ifdef EXTENSIONS
   1386 		case 'r':
   1387 			defaultfield = 1;
   1388 			(void) save_range(&u, optarg);
   1389 			break;
   1390 #endif
   1391 		case 's':
   1392 			defaultfield = 1;
   1393 			memsave(&u.u_shell, optarg, strlen(optarg));
   1394 			break;
   1395 		case 'u':
   1396 			if (!is_number(optarg)) {
   1397 				errx(EXIT_FAILURE, "When using [-u uid], the uid must be numeric");
   1398 			}
   1399 			u.u_uid = atoi(optarg);
   1400 			break;
   1401 #ifdef EXTENSIONS
   1402 		case 'v':
   1403 			verbose = 1;
   1404 			break;
   1405 #endif
   1406 		}
   1407 	}
   1408 	if (bigD) {
   1409 		if (defaultfield) {
   1410 			checkeuid();
   1411 			return setdefaults(&u) ? EXIT_SUCCESS : EXIT_FAILURE;
   1412 		}
   1413 		(void) printf("group\t\t%s\n", u.u_primgrp);
   1414 		(void) printf("base_dir\t%s\n", u.u_basedir);
   1415 		(void) printf("skel_dir\t%s\n", u.u_skeldir);
   1416 		(void) printf("shell\t\t%s\n", u.u_shell);
   1417 #ifdef EXTENSIONS
   1418 		(void) printf("class\t\t%s\n", u.u_class);
   1419 #endif
   1420 		(void) printf("inactive\t%d\n", u.u_inactive);
   1421 		(void) printf("expire\t\t%s\n", (u.u_expire == NULL) ? UNSET_EXPIRY : u.u_expire);
   1422 #ifdef EXTENSIONS
   1423 		for (i = 0 ; i < u.u_rc ; i++) {
   1424 			(void) printf("range\t\t%d..%d\n", u.u_rv[i].r_from, u.u_rv[i].r_to);
   1425 		}
   1426 #endif
   1427 		return EXIT_SUCCESS;
   1428 	}
   1429 	argc -= optind;
   1430 	argv += optind;
   1431 	if (argc != 1) {
   1432 		usermgmt_usage("useradd");
   1433 	}
   1434 	checkeuid();
   1435 	return adduser(*argv, &u) ? EXIT_SUCCESS : EXIT_FAILURE;
   1436 }
   1437 
   1438 #ifdef EXTENSIONS
   1439 #define MOD_OPT_EXTENSIONS	"p:vL:"
   1440 #else
   1441 #define MOD_OPT_EXTENSIONS
   1442 #endif
   1443 
   1444 int
   1445 usermod(int argc, char **argv)
   1446 {
   1447 	user_t	u;
   1448 	char	newuser[MaxUserNameLen + 1];
   1449 	int	c, have_new_user;
   1450 
   1451 	(void) memset(&u, 0, sizeof(u));
   1452 	(void) memset(newuser, 0, sizeof(newuser));
   1453 	read_defaults(&u);
   1454 	have_new_user = 0;
   1455 	while ((c = getopt(argc, argv, "G:c:d:e:f:g:l:mos:u:" MOD_OPT_EXTENSIONS)) != -1) {
   1456 		switch(c) {
   1457 		case 'G':
   1458 			while ((u.u_groupv[u.u_groupc] = strsep(&optarg, ",")) != NULL &&
   1459 			       u.u_groupc < NGROUPS_MAX) {
   1460 				if (u.u_groupv[u.u_groupc][0] != 0) {
   1461 					u.u_groupc++;
   1462 				}
   1463 			}
   1464 			if (optarg != NULL) {
   1465 				warnx("Truncated list of secondary groups to %d entries", NGROUPS_MAX);
   1466 			}
   1467 			u.u_flags |= F_SECGROUP;
   1468 			break;
   1469 		case 'c':
   1470 			memsave(&u.u_comment, optarg, strlen(optarg));
   1471 			u.u_flags |= F_COMMENT;
   1472 			break;
   1473 		case 'd':
   1474 			memsave(&u.u_home, optarg, strlen(optarg));
   1475 			u.u_flags |= F_HOMEDIR;
   1476 			break;
   1477 		case 'e':
   1478 			memsave(&u.u_expire, optarg, strlen(optarg));
   1479 			u.u_flags |= F_EXPIRE;
   1480 			break;
   1481 		case 'f':
   1482 			u.u_inactive = atoi(optarg);
   1483 			u.u_flags |= F_INACTIVE;
   1484 			break;
   1485 		case 'g':
   1486 			memsave(&u.u_primgrp, optarg, strlen(optarg));
   1487 			u.u_flags |= F_GROUP;
   1488 			break;
   1489 		case 'l':
   1490 			(void) strlcpy(newuser, optarg, sizeof(newuser));
   1491 			have_new_user = 1;
   1492 			u.u_flags |= F_USERNAME;
   1493 			break;
   1494 #ifdef EXTENSIONS
   1495 		case 'L':
   1496 			memsave(&u.u_class, optarg, strlen(optarg));
   1497 			u.u_flags |= F_CLASS;
   1498 			break;
   1499 #endif
   1500 		case 'm':
   1501 			u.u_flags |= F_MKDIR;
   1502 			break;
   1503 		case 'o':
   1504 			u.u_flags |= F_DUPUID;
   1505 			break;
   1506 #ifdef EXTENSIONS
   1507 		case 'p':
   1508 			memsave(&u.u_password, optarg, strlen(optarg));
   1509 			u.u_flags |= F_PASSWORD;
   1510 			break;
   1511 #endif
   1512 		case 's':
   1513 			memsave(&u.u_shell, optarg, strlen(optarg));
   1514 			u.u_flags |= F_SHELL;
   1515 			break;
   1516 		case 'u':
   1517 			if (!is_number(optarg)) {
   1518 				errx(EXIT_FAILURE, "When using [-u uid], the uid must be numeric");
   1519 			}
   1520 			u.u_uid = atoi(optarg);
   1521 			u.u_flags |= F_UID;
   1522 			break;
   1523 #ifdef EXTENSIONS
   1524 		case 'v':
   1525 			verbose = 1;
   1526 			break;
   1527 #endif
   1528 		}
   1529 	}
   1530 	if ((u.u_flags & F_MKDIR) && !(u.u_flags & F_HOMEDIR) &&
   1531 	    !(u.u_flags & F_USERNAME)) {
   1532 		warnx("option 'm' useless without 'd' or 'l' -- ignored");
   1533 		u.u_flags &= ~F_MKDIR;
   1534 	}
   1535 	argc -= optind;
   1536 	argv += optind;
   1537 	if (argc != 1) {
   1538 		usermgmt_usage("usermod");
   1539 	}
   1540 	checkeuid();
   1541 	return moduser(*argv, (have_new_user) ? newuser : *argv, &u) ? EXIT_SUCCESS : EXIT_FAILURE;
   1542 }
   1543 
   1544 #ifdef EXTENSIONS
   1545 #define DEL_OPT_EXTENSIONS	"Dp:v"
   1546 #else
   1547 #define DEL_OPT_EXTENSIONS
   1548 #endif
   1549 
   1550 int
   1551 userdel(int argc, char **argv)
   1552 {
   1553 	struct passwd	*pwp;
   1554 	user_t		u;
   1555 	char		password[PasswordLength + 1];
   1556 	int		defaultfield;
   1557 	int		rmhome;
   1558 	int		bigD;
   1559 	int		c;
   1560 
   1561 	(void) memset(&u, 0, sizeof(u));
   1562 	read_defaults(&u);
   1563 	defaultfield = bigD = rmhome = 0;
   1564 	while ((c = getopt(argc, argv, "r" DEL_OPT_EXTENSIONS)) != -1) {
   1565 		switch(c) {
   1566 #ifdef EXTENSIONS
   1567 		case 'D':
   1568 			bigD = 1;
   1569 			break;
   1570 #endif
   1571 #ifdef EXTENSIONS
   1572 		case 'p':
   1573 			defaultfield = 1;
   1574 			u.u_preserve = (strcmp(optarg, "true") == 0) ? 1 :
   1575 					(strcmp(optarg, "yes") == 0) ? 1 :
   1576 					 atoi(optarg);
   1577 			break;
   1578 #endif
   1579 		case 'r':
   1580 			rmhome = 1;
   1581 			break;
   1582 #ifdef EXTENSIONS
   1583 		case 'v':
   1584 			verbose = 1;
   1585 			break;
   1586 #endif
   1587 		}
   1588 	}
   1589 #ifdef EXTENSIONS
   1590 	if (bigD) {
   1591 		if (defaultfield) {
   1592 			checkeuid();
   1593 			return setdefaults(&u) ? EXIT_SUCCESS : EXIT_FAILURE;
   1594 		}
   1595 		(void) printf("preserve\t%s\n", (u.u_preserve) ? "true" : "false");
   1596 		return EXIT_SUCCESS;
   1597 	}
   1598 #endif
   1599 	argc -= optind;
   1600 	argv += optind;
   1601 	if (argc != 1) {
   1602 		usermgmt_usage("userdel");
   1603 	}
   1604 	checkeuid();
   1605 	if ((pwp = getpwnam(*argv)) == NULL) {
   1606 		warnx("No such user `%s'", *argv);
   1607 		return EXIT_FAILURE;
   1608 	}
   1609 	if (rmhome)
   1610 		(void)removehomedir(pwp->pw_name, pwp->pw_uid, pwp->pw_dir);
   1611 	if (u.u_preserve) {
   1612 		u.u_flags |= F_SHELL;
   1613 		memsave(&u.u_shell, NOLOGIN, strlen(NOLOGIN));
   1614 		(void) memset(password, '*', PasswordLength);
   1615 		password[PasswordLength] = '\0';
   1616 		memsave(&u.u_password, password, PasswordLength);
   1617 		u.u_flags |= F_PASSWORD;
   1618 		return moduser(*argv, *argv, &u) ? EXIT_SUCCESS : EXIT_FAILURE;
   1619 	}
   1620 	return moduser(*argv, *argv, NULL) ? EXIT_SUCCESS : EXIT_FAILURE;
   1621 }
   1622 
   1623 #ifdef EXTENSIONS
   1624 #define GROUP_ADD_OPT_EXTENSIONS	"v"
   1625 #else
   1626 #define GROUP_ADD_OPT_EXTENSIONS
   1627 #endif
   1628 
   1629 /* add a group */
   1630 int
   1631 groupadd(int argc, char **argv)
   1632 {
   1633 	int	dupgid;
   1634 	int	gid;
   1635 	int	c;
   1636 
   1637 	gid = -1;
   1638 	dupgid = 0;
   1639 	while ((c = getopt(argc, argv, "g:o" GROUP_ADD_OPT_EXTENSIONS)) != -1) {
   1640 		switch(c) {
   1641 		case 'g':
   1642 			if (!is_number(optarg)) {
   1643 				errx(EXIT_FAILURE, "When using [-g gid], the gid must be numeric");
   1644 			}
   1645 			gid = atoi(optarg);
   1646 			break;
   1647 		case 'o':
   1648 			dupgid = 1;
   1649 			break;
   1650 #ifdef EXTENSIONS
   1651 		case 'v':
   1652 			verbose = 1;
   1653 			break;
   1654 #endif
   1655 		}
   1656 	}
   1657 	argc -= optind;
   1658 	argv += optind;
   1659 	if (argc != 1) {
   1660 		usermgmt_usage("groupadd");
   1661 	}
   1662 	checkeuid();
   1663 	if (gid < 0 && !getnextgid(&gid, LowGid, HighGid)) {
   1664 		err(EXIT_FAILURE, "can't add group: can't get next gid");
   1665 	}
   1666 	if (!dupgid && getgrgid((gid_t) gid) != NULL) {
   1667 		errx(EXIT_FAILURE, "can't add group: gid %d is a duplicate", gid);
   1668 	}
   1669 	if (!valid_group(*argv)) {
   1670 		warnx("warning - invalid group name `%s'", *argv);
   1671 	}
   1672 	if (!creategid(*argv, gid, "")) {
   1673 		errx(EXIT_FAILURE, "can't add group: problems with %s file", _PATH_GROUP);
   1674 	}
   1675 	return EXIT_SUCCESS;
   1676 }
   1677 
   1678 #ifdef EXTENSIONS
   1679 #define GROUP_DEL_OPT_EXTENSIONS	"v"
   1680 #else
   1681 #define GROUP_DEL_OPT_EXTENSIONS
   1682 #endif
   1683 
   1684 /* remove a group */
   1685 int
   1686 groupdel(int argc, char **argv)
   1687 {
   1688 	int	c;
   1689 
   1690 	while ((c = getopt(argc, argv, "" GROUP_DEL_OPT_EXTENSIONS)) != -1) {
   1691 		switch(c) {
   1692 #ifdef EXTENSIONS
   1693 		case 'v':
   1694 			verbose = 1;
   1695 			break;
   1696 #endif
   1697 		}
   1698 	}
   1699 	argc -= optind;
   1700 	argv += optind;
   1701 	if (argc != 1) {
   1702 		usermgmt_usage("groupdel");
   1703 	}
   1704 	checkeuid();
   1705 	if (!modify_gid(*argv, NULL)) {
   1706 		err(EXIT_FAILURE, "can't change %s file", _PATH_GROUP);
   1707 	}
   1708 	return EXIT_SUCCESS;
   1709 }
   1710 
   1711 #ifdef EXTENSIONS
   1712 #define GROUP_MOD_OPT_EXTENSIONS	"v"
   1713 #else
   1714 #define GROUP_MOD_OPT_EXTENSIONS
   1715 #endif
   1716 
   1717 /* modify a group */
   1718 int
   1719 groupmod(int argc, char **argv)
   1720 {
   1721 	struct group	*grp;
   1722 	char		buf[MaxEntryLen];
   1723 	char		*newname;
   1724 	char		**cpp;
   1725 	int		dupgid;
   1726 	int		gid;
   1727 	int		cc;
   1728 	int		c;
   1729 
   1730 	gid = -1;
   1731 	dupgid = 0;
   1732 	newname = NULL;
   1733 	while ((c = getopt(argc, argv, "g:on:" GROUP_MOD_OPT_EXTENSIONS)) != -1) {
   1734 		switch(c) {
   1735 		case 'g':
   1736 			if (!is_number(optarg)) {
   1737 				errx(EXIT_FAILURE, "When using [-g gid], the gid must be numeric");
   1738 			}
   1739 			gid = atoi(optarg);
   1740 			break;
   1741 		case 'o':
   1742 			dupgid = 1;
   1743 			break;
   1744 		case 'n':
   1745 			memsave(&newname, optarg, strlen(optarg));
   1746 			break;
   1747 #ifdef EXTENSIONS
   1748 		case 'v':
   1749 			verbose = 1;
   1750 			break;
   1751 #endif
   1752 		}
   1753 	}
   1754 	argc -= optind;
   1755 	argv += optind;
   1756 	if (argc != 1) {
   1757 		usermgmt_usage("groupmod");
   1758 	}
   1759 	checkeuid();
   1760 	if (gid < 0 && newname == NULL) {
   1761 		err(EXIT_FAILURE, "Nothing to change");
   1762 	}
   1763 	if (dupgid && gid < 0) {
   1764 		err(EXIT_FAILURE, "Duplicate which gid?");
   1765 	}
   1766 	if ((grp = getgrnam(*argv)) == NULL) {
   1767 		err(EXIT_FAILURE, "can't find group `%s' to modify", *argv);
   1768 	}
   1769 	if (newname != NULL && !valid_group(newname)) {
   1770 		warn("warning - invalid group name `%s'", newname);
   1771 	}
   1772 	cc = snprintf(buf, sizeof(buf), "%s:%s:%d:",
   1773 			(newname) ? newname : grp->gr_name,
   1774 			grp->gr_passwd,
   1775 			(gid < 0) ? grp->gr_gid : gid);
   1776 	for (cpp = grp->gr_mem ; *cpp && cc < sizeof(buf) ; cpp++) {
   1777 		cc += snprintf(&buf[cc], sizeof(buf) - cc, "%s%s", *cpp,
   1778 			(cpp[1] == NULL) ? "" : ",");
   1779 	}
   1780 	cc += snprintf(&buf[cc], sizeof(buf) - cc, "\n");
   1781 	if (!modify_gid(*argv, buf)) {
   1782 		err(EXIT_FAILURE, "can't change %s file", _PATH_GROUP);
   1783 	}
   1784 	return EXIT_SUCCESS;
   1785 }
   1786 
   1787 #ifdef EXTENSIONS
   1788 /* display user information */
   1789 int
   1790 userinfo(int argc, char **argv)
   1791 {
   1792 	struct passwd	*pwp;
   1793 	struct group	*grp;
   1794 	char		buf[MaxEntryLen];
   1795 	char		**cpp;
   1796 	int		exists;
   1797 	int		cc;
   1798 	int		i;
   1799 
   1800 	exists = 0;
   1801 	while ((i = getopt(argc, argv, "ev")) != -1) {
   1802 		switch(i) {
   1803 		case 'e':
   1804 			exists = 1;
   1805 			break;
   1806 		case 'v':
   1807 			verbose = 1;
   1808 			break;
   1809 		}
   1810 	}
   1811 	argc -= optind;
   1812 	argv += optind;
   1813 	if (argc != 1) {
   1814 		usermgmt_usage("userinfo");
   1815 	}
   1816 	pwp = find_user_info(*argv);
   1817 	if (exists) {
   1818 		exit((pwp) ? EXIT_SUCCESS : EXIT_FAILURE);
   1819 	}
   1820 	if (pwp == NULL) {
   1821 		errx(EXIT_FAILURE, "can't find user `%s'", *argv);
   1822 	}
   1823 	(void) printf("login\t%s\n", pwp->pw_name);
   1824 	(void) printf("passwd\t%s\n", pwp->pw_passwd);
   1825 	(void) printf("uid\t%d\n", pwp->pw_uid);
   1826 	for (cc = 0 ; (grp = getgrent()) != NULL ; ) {
   1827 		for (cpp = grp->gr_mem ; *cpp ; cpp++) {
   1828 			if (strcmp(*cpp, *argv) == 0 && grp->gr_gid != pwp->pw_gid) {
   1829 				cc += snprintf(&buf[cc], sizeof(buf) - cc, "%s ", grp->gr_name);
   1830 			}
   1831 		}
   1832 	}
   1833 	if ((grp = getgrgid(pwp->pw_gid)) == NULL) {
   1834 		(void) printf("groups\t%d %s\n", pwp->pw_gid, buf);
   1835 	} else {
   1836 		(void) printf("groups\t%s %s\n", grp->gr_name, buf);
   1837 	}
   1838 	(void) printf("change\t%s", pwp->pw_change ? ctime(&pwp->pw_change) : "NEVER\n");
   1839 #ifdef EXTENSIONS
   1840 	(void) printf("class\t%s\n", pwp->pw_class);
   1841 #endif
   1842 	(void) printf("gecos\t%s\n", pwp->pw_gecos);
   1843 	(void) printf("dir\t%s\n", pwp->pw_dir);
   1844 	(void) printf("shell\t%s\n", pwp->pw_shell);
   1845 	(void) printf("expire\t%s", pwp->pw_expire ? ctime(&pwp->pw_expire) : "NEVER\n");
   1846 	return EXIT_SUCCESS;
   1847 }
   1848 #endif
   1849 
   1850 #ifdef EXTENSIONS
   1851 /* display user information */
   1852 int
   1853 groupinfo(int argc, char **argv)
   1854 {
   1855 	struct group	*grp;
   1856 	char		**cpp;
   1857 	int		exists;
   1858 	int		i;
   1859 
   1860 	exists = 0;
   1861 	while ((i = getopt(argc, argv, "ev")) != -1) {
   1862 		switch(i) {
   1863 		case 'e':
   1864 			exists = 1;
   1865 			break;
   1866 		case 'v':
   1867 			verbose = 1;
   1868 			break;
   1869 		}
   1870 	}
   1871 	argc -= optind;
   1872 	argv += optind;
   1873 	if (argc != 1) {
   1874 		usermgmt_usage("groupinfo");
   1875 	}
   1876 	grp = find_group_info(*argv);
   1877 	if (exists) {
   1878 		exit((grp) ? EXIT_SUCCESS : EXIT_FAILURE);
   1879 	}
   1880 	if (grp == NULL) {
   1881 		errx(EXIT_FAILURE, "can't find group `%s'", *argv);
   1882 	}
   1883 	(void) printf("name\t%s\n", grp->gr_name);
   1884 	(void) printf("passwd\t%s\n", grp->gr_passwd);
   1885 	(void) printf("gid\t%d\n", grp->gr_gid);
   1886 	(void) printf("members\t");
   1887 	for (cpp = grp->gr_mem ; *cpp ; cpp++) {
   1888 		(void) printf("%s ", *cpp);
   1889 	}
   1890 	(void) fputc('\n', stdout);
   1891 	return EXIT_SUCCESS;
   1892 }
   1893 #endif
   1894