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