Home | History | Annotate | Line # | Download | only in user
user.c revision 1.93
      1 /* $NetBSD: user.c,v 1.93 2005/09/18 21:41:26 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.93 2005/09/18 21:41:26 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 #ifdef EXTENSIONS
     51 #include <login_cap.h>
     52 #endif
     53 #include <paths.h>
     54 #include <pwd.h>
     55 #include <regex.h>
     56 #include <stdarg.h>
     57 #include <stdio.h>
     58 #include <stdlib.h>
     59 #include <string.h>
     60 #include <syslog.h>
     61 #include <time.h>
     62 #include <unistd.h>
     63 #include <util.h>
     64 #include <errno.h>
     65 
     66 #include "defs.h"
     67 #include "usermgmt.h"
     68 
     69 
     70 /* this struct describes a uid range */
     71 typedef struct range_t {
     72 	int	r_from;		/* low uid */
     73 	int	r_to;		/* high uid */
     74 } range_t;
     75 
     76 /* this struct encapsulates the user information */
     77 typedef struct user_t {
     78 	int		u_flags;		/* see below */
     79 	int		u_uid;			/* uid of user */
     80 	char	       *u_password;		/* encrypted password */
     81 	char	       *u_comment;		/* comment field */
     82 	char	       *u_home;			/* home directory */
     83 	char	       *u_primgrp;		/* primary group */
     84 	int		u_groupc;		/* # of secondary groups */
     85 	const char     *u_groupv[NGROUPS_MAX];	/* secondary groups */
     86 	char	       *u_shell;		/* user's shell */
     87 	char	       *u_basedir;		/* base directory for home */
     88 	char	       *u_expire;		/* when password will expire */
     89 	char	       *u_inactive;		/* when account will expire */
     90 	char	       *u_skeldir;		/* directory for startup files */
     91 	char	       *u_class;		/* login class */
     92 	unsigned	u_rsize;		/* size of range array */
     93 	unsigned	u_rc;			/* # of ranges */
     94 	range_t	       *u_rv;			/* the ranges */
     95 	unsigned	u_defrc;		/* # of ranges in defaults */
     96 	int		u_preserve;		/* preserve uids on deletion */
     97 	int		u_allow_samba;		/* allow trailing '$' for samba login names */
     98 	int		u_locked;		/* user account lock */
     99 } user_t;
    100 
    101 /* flags for which fields of the user_t replace the passwd entry */
    102 enum {
    103 	F_COMMENT	= 0x0001,
    104 	F_DUPUID  	= 0x0002,
    105 	F_EXPIRE	= 0x0004,
    106 	F_GROUP		= 0x0008,
    107 	F_HOMEDIR	= 0x0010,
    108 	F_MKDIR		= 0x0020,
    109 	F_INACTIVE	= 0x0040,
    110 	F_PASSWORD	= 0x0080,
    111 	F_SECGROUP	= 0x0100,
    112 	F_SHELL 	= 0x0200,
    113 	F_UID		= 0x0400,
    114 	F_USERNAME	= 0x0800,
    115 	F_CLASS		= 0x1000
    116 };
    117 
    118 #define	UNLOCK		0
    119 #define LOCK		1
    120 #define LOCKED		"*LOCKED*"
    121 
    122 #define CONFFILE	"/etc/usermgmt.conf"
    123 #define	PATH_LOGINCONF	"/etc/login.conf"
    124 
    125 #ifndef DEF_GROUP
    126 #define DEF_GROUP	"users"
    127 #endif
    128 
    129 #ifndef DEF_BASEDIR
    130 #define DEF_BASEDIR	"/home"
    131 #endif
    132 
    133 #ifndef DEF_SKELDIR
    134 #define DEF_SKELDIR	"/etc/skel"
    135 #endif
    136 
    137 #ifndef DEF_SHELL
    138 #define DEF_SHELL	_PATH_CSHELL
    139 #endif
    140 
    141 #ifndef DEF_COMMENT
    142 #define DEF_COMMENT	""
    143 #endif
    144 
    145 #ifndef DEF_LOWUID
    146 #define DEF_LOWUID	1000
    147 #endif
    148 
    149 #ifndef DEF_HIGHUID
    150 #define DEF_HIGHUID	60000
    151 #endif
    152 
    153 #ifndef DEF_INACTIVE
    154 #define DEF_INACTIVE	0
    155 #endif
    156 
    157 #ifndef DEF_EXPIRE
    158 #define DEF_EXPIRE	NULL
    159 #endif
    160 
    161 #ifndef DEF_CLASS
    162 #define DEF_CLASS	""
    163 #endif
    164 
    165 #ifndef WAITSECS
    166 #define WAITSECS	10
    167 #endif
    168 
    169 #ifndef NOBODY_UID
    170 #define NOBODY_UID	32767
    171 #endif
    172 
    173 /* some useful constants */
    174 enum {
    175 	MaxShellNameLen = 256,
    176 	MaxFileNameLen = MAXPATHLEN,
    177 	MaxUserNameLen = LOGIN_NAME_MAX - 1,
    178 	MaxCommandLen = 2048,
    179 	MaxEntryLen = 2048,
    180 	PasswordLength = 2048,
    181 
    182 	DES_Len = 13,
    183 
    184 	LowGid = DEF_LOWUID,
    185 	HighGid = DEF_HIGHUID
    186 };
    187 
    188 /* Full paths of programs used here */
    189 #define CHMOD		"/bin/chmod"
    190 #define CHOWN		"/usr/sbin/chown"
    191 #define MKDIR		"/bin/mkdir"
    192 #define MV		"/bin/mv"
    193 #define NOLOGIN		"/sbin/nologin"
    194 #define PAX		"/bin/pax"
    195 #define RM		"/bin/rm"
    196 
    197 #define UNSET_INACTIVE	"Null (unset)"
    198 #define UNSET_EXPIRY	"Null (unset)"
    199 
    200 static int		asystem(const char *fmt, ...)
    201 			    __attribute__((__format__(__printf__, 1, 2)));
    202 static int		is_number(const char *);
    203 static struct group	*find_group_info(const char *);
    204 static int		verbose;
    205 
    206 static char *
    207 skipspace(char *s)
    208 {
    209 	for (; *s && isspace((unsigned char)*s) ; s++) {
    210 	}
    211 	return s;
    212 }
    213 
    214 static int
    215 check_numeric(const char *val, const char *name)
    216 {
    217 	if (!is_number(val)) {
    218 		errx(EXIT_FAILURE, "When using [-%c %s], "
    219 		    "the %s must be numeric", *name, name, name);
    220 	}
    221 	return atoi(val);
    222 }
    223 
    224 /* if *cpp is non-null, free it, then assign `n' chars of `s' to it */
    225 static void
    226 memsave(char **cpp, const char *s, size_t n)
    227 {
    228 	if (*cpp != NULL) {
    229 		FREE(*cpp);
    230 	}
    231 	NEWARRAY(char, *cpp, n + 1, exit(1));
    232 	(void)memcpy(*cpp, s, n);
    233 	(*cpp)[n] = '\0';
    234 }
    235 
    236 /* a replacement for system(3) */
    237 static int
    238 asystem(const char *fmt, ...)
    239 {
    240 	va_list	vp;
    241 	char	buf[MaxCommandLen];
    242 	int	ret;
    243 
    244 	va_start(vp, fmt);
    245 	(void)vsnprintf(buf, sizeof(buf), fmt, vp);
    246 	va_end(vp);
    247 	if (verbose) {
    248 		(void)printf("Command: %s\n", buf);
    249 	}
    250 	if ((ret = system(buf)) != 0) {
    251 		warn("Error running `%s'", buf);
    252 	}
    253 	return ret;
    254 }
    255 
    256 /* remove a users home directory, returning 1 for success (ie, no problems encountered) */
    257 static int
    258 removehomedir(const char *user, uid_t uid, const char *dir)
    259 {
    260 	struct stat st;
    261 
    262 	/* userid not root? */
    263 	if (uid == 0) {
    264 		warnx("Not deleting home directory `%s'; userid is 0", dir);
    265 		return 0;
    266 	}
    267 
    268 	/* directory exists (and is a directory!) */
    269 	if (stat(dir, &st) < 0) {
    270 		warn("Cannot access home directory `%s'", dir);
    271 		return 0;
    272 	}
    273 	if (!S_ISDIR(st.st_mode)) {
    274 		warnx("Home directory `%s' is not a directory", dir);
    275 		return 0;
    276 	}
    277 
    278 	/* userid matches directory owner? */
    279 	if (st.st_uid != uid) {
    280 		warnx("User `%s' doesn't own directory `%s', not removed",
    281 		    user, dir);
    282 		return 0;
    283 	}
    284 
    285 	(void)seteuid(uid);
    286 	/* we add the "|| true" to keep asystem() quiet if there is a non-zero exit status. */
    287 	(void)asystem("%s -rf %s > /dev/null 2>&1 || true", RM, dir);
    288 	(void)seteuid(0);
    289 	if (rmdir(dir) < 0) {
    290 		warn("Unable to remove all files in `%s'", dir);
    291 		return 0;
    292 	}
    293 	return 1;
    294 }
    295 
    296 #define NetBSD_1_4_K	104110000
    297 
    298 #if defined(__NetBSD_Version__) && (__NetBSD_Version__ < NetBSD_1_4_K)
    299 /* bounds checking strncpy */
    300 static int
    301 strlcpy(char *to, char *from, size_t tosize)
    302 {
    303 	size_t	n;
    304 	int	fromsize;
    305 
    306 	fromsize = strlen(from);
    307 	n = MIN(tosize - 1, fromsize);
    308 	(void)memcpy(to, from, n);
    309 	to[n] = '\0';
    310 	return fromsize;
    311 }
    312 #endif /* NetBSD < 1.4K */
    313 
    314 /*
    315  * Copyright (c) 1997 Todd C. Miller <Todd.Miller (at) courtesan.com>
    316  * All rights reserved.
    317  *
    318  * Redistribution and use in source and binary forms, with or without
    319  * modification, are permitted provided that the following conditions
    320  * are met:
    321  * 1. Redistributions of source code must retain the above copyright
    322  *    notice, this list of conditions and the following disclaimer.
    323  * 2. Redistributions in binary form must reproduce the above copyright
    324  *    notice, this list of conditions and the following disclaimer in the
    325  *    documentation and/or other materials provided with the distribution.
    326  * 3. The name of the author may not be used to endorse or promote products
    327  *    derived from this software without specific prior written permission.
    328  *
    329  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
    330  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
    331  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
    332  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
    333  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
    334  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
    335  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
    336  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
    337  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
    338  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    339  */
    340 
    341 /*
    342  * research has shown that NetBSD 1.3H was the first version of -current
    343  * with asprintf in libc. agc
    344  */
    345 #define NetBSD_1_3_H	103080000
    346 
    347 #if defined(__NetBSD_Version__) && (__NetBSD_Version__ < NetBSD_1_3_H)
    348 
    349 int
    350 asprintf(char **str, char const *fmt, ...)
    351 {
    352 	int ret;
    353 	va_list ap;
    354 	FILE f;
    355 	unsigned char *_base;
    356 
    357 	f._flags = __SWR | __SSTR | __SALC;
    358 	f._bf._base = f._p = (unsigned char *)malloc(128);
    359 	if (f._bf._base == NULL)
    360 		goto err;
    361 	f._bf._size = f._w = 127;		/* Leave room for the NUL */
    362 	va_start(ap, fmt);
    363 	ret = vfprintf(&f, fmt, ap);
    364 	va_end(ap);
    365 	if (ret == -1)
    366 		goto err;
    367 	*f._p = '\0';
    368 	_base = realloc(f._bf._base, (size_t)(ret + 1));
    369 	if (_base == NULL)
    370 		goto err;
    371 	*str = (char *)_base;
    372 	return (ret);
    373 
    374 err:
    375 	if (f._bf._base)
    376 		free(f._bf._base);
    377 	*str = NULL;
    378 	return (-1);
    379 }
    380 #endif /* NetBSD < 1.3H */
    381 
    382 /* return 1 if all of `s' is numeric */
    383 static int
    384 is_number(const char *s)
    385 {
    386 	for ( ; *s ; s++) {
    387 		if (!isdigit((unsigned char) *s)) {
    388 			return 0;
    389 		}
    390 	}
    391 	return 1;
    392 }
    393 
    394 /*
    395  * check that the effective uid is 0 - called from funcs which will
    396  * modify data and config files.
    397  */
    398 static void
    399 checkeuid(void)
    400 {
    401 	if (geteuid() != 0) {
    402 		errx(EXIT_FAILURE, "Program must be run as root");
    403 	}
    404 }
    405 
    406 /* copy any dot files into the user's home directory */
    407 static int
    408 copydotfiles(char *skeldir, int uid, int gid, char *dir)
    409 {
    410 	struct dirent	*dp;
    411 	DIR		*dirp;
    412 	int		n;
    413 
    414 	if ((dirp = opendir(skeldir)) == NULL) {
    415 		warn("Can't open source . files dir `%s'", skeldir);
    416 		return 0;
    417 	}
    418 	for (n = 0; (dp = readdir(dirp)) != NULL && n == 0 ; ) {
    419 		if (strcmp(dp->d_name, ".") == 0 ||
    420 		    strcmp(dp->d_name, "..") == 0) {
    421 			continue;
    422 		}
    423 		n = 1;
    424 	}
    425 	(void)closedir(dirp);
    426 	if (n == 0) {
    427 		warnx("No \"dot\" initialisation files found");
    428 	} else {
    429 		(void)asystem("cd %s && %s -rw -pe %s . %s",
    430 				skeldir, PAX, (verbose) ? "-v" : "", dir);
    431 	}
    432 	(void)asystem("%s -R -h %d:%d %s", CHOWN, uid, gid, dir);
    433 	(void)asystem("%s -R u+w %s", CHMOD, dir);
    434 	return n;
    435 }
    436 
    437 /* create a group entry with gid `gid' */
    438 static int
    439 creategid(char *group, int gid, const char *name)
    440 {
    441 	struct stat	st;
    442 	FILE		*from;
    443 	FILE		*to;
    444 	char		buf[MaxEntryLen];
    445 	char		f[MaxFileNameLen];
    446 	int		fd;
    447 	int		cc;
    448 
    449 	if (getgrnam(group) != NULL) {
    450 		warnx("Group `%s' already exists", group);
    451 		return 0;
    452 	}
    453 	if ((from = fopen(_PATH_GROUP, "r")) == NULL) {
    454 		warn("Can't create gid for `%s': can't open `%s'", name,
    455 		    _PATH_GROUP);
    456 		return 0;
    457 	}
    458 	if (flock(fileno(from), LOCK_EX | LOCK_NB) < 0) {
    459 		warn("Can't lock `%s'", _PATH_GROUP);
    460 	}
    461 	(void)fstat(fileno(from), &st);
    462 	(void)snprintf(f, sizeof(f), "%s.XXXXXX", _PATH_GROUP);
    463 	if ((fd = mkstemp(f)) < 0) {
    464 		warn("Can't create gid: mkstemp failed");
    465 		(void)fclose(from);
    466 		return 0;
    467 	}
    468 	if ((to = fdopen(fd, "w")) == NULL) {
    469 		warn("Can't create gid: fdopen `%s' failed", f);
    470 		(void)fclose(from);
    471 		(void)close(fd);
    472 		(void)unlink(f);
    473 		return 0;
    474 	}
    475 	while ((cc = fread(buf, sizeof(char), sizeof(buf), from)) > 0) {
    476 		if (fwrite(buf, sizeof(char), (unsigned) cc, to) != cc) {
    477 			warn("Can't create gid: short write to `%s'", f);
    478 			(void)fclose(from);
    479 			(void)close(fd);
    480 			(void)unlink(f);
    481 			return 0;
    482 		}
    483 	}
    484 	(void)fprintf(to, "%s:*:%d:%s\n", group, gid, name);
    485 	(void)fclose(from);
    486 	(void)fclose(to);
    487 	if (rename(f, _PATH_GROUP) < 0) {
    488 		warn("Can't create gid: can't rename `%s' to `%s'", f,
    489 		    _PATH_GROUP);
    490 		(void)unlink(f);
    491 		return 0;
    492 	}
    493 	(void)chmod(_PATH_GROUP, st.st_mode & 07777);
    494 	syslog(LOG_INFO, "New group added: name=%s, gid=%d", group, gid);
    495 	return 1;
    496 }
    497 
    498 /* modify the group entry with name `group' to be newent */
    499 static int
    500 modify_gid(char *group, char *newent)
    501 {
    502 	struct stat	st;
    503 	FILE		*from;
    504 	FILE		*to;
    505 	char		buf[MaxEntryLen];
    506 	char		f[MaxFileNameLen];
    507 	char		*colon;
    508 	int		groupc;
    509 	int		entc;
    510 	int		fd;
    511 	int		cc;
    512 
    513 	if ((from = fopen(_PATH_GROUP, "r")) == NULL) {
    514 		warn("Can't create gid for `%s': can't open `%s'",
    515 		    group, _PATH_GROUP);
    516 		return 0;
    517 	}
    518 	if (flock(fileno(from), LOCK_EX | LOCK_NB) < 0) {
    519 		warn("Can't lock `%s'", _PATH_GROUP);
    520 	}
    521 	(void)fstat(fileno(from), &st);
    522 	(void)snprintf(f, sizeof(f), "%s.XXXXXX", _PATH_GROUP);
    523 	if ((fd = mkstemp(f)) < 0) {
    524 		warn("Can't create gid: mkstemp failed");
    525 		(void)fclose(from);
    526 		return 0;
    527 	}
    528 	if ((to = fdopen(fd, "w")) == NULL) {
    529 		warn("Can't create gid: fdopen `%s' failed", f);
    530 		(void)fclose(from);
    531 		(void)close(fd);
    532 		(void)unlink(f);
    533 		return 0;
    534 	}
    535 	groupc = strlen(group);
    536 	while (fgets(buf, sizeof(buf), from) != NULL) {
    537 		cc = strlen(buf);
    538 		if ((colon = strchr(buf, ':')) == NULL) {
    539 			warnx("Badly formed entry `%s'", buf);
    540 			continue;
    541 		}
    542 		entc = (int)(colon - buf);
    543 		if (entc == groupc &&
    544 		    strncmp(group, buf, (unsigned) entc) == 0) {
    545 			if (newent == NULL) {
    546 				struct group	*grp_rm;
    547 				struct passwd	*user_pwd;
    548 
    549 				/*
    550 				 * Check that the group being removed
    551 				 * isn't any user's Primary group. Just
    552 				 * warn if it is. This could cause problems
    553 				 * if the group GID was reused for a
    554 				 * different purpose.
    555 				 */
    556 
    557 				grp_rm = find_group_info(group);
    558 				while ((user_pwd = getpwent()) != NULL) {
    559 					if (user_pwd->pw_gid == grp_rm->gr_gid) {
    560 						warnx("Warning: group `%s'(%d)"
    561 						   " is the primary group of"
    562 						   " `%s'. Use caution if you"
    563 						   " later add this GID.",
    564 						   grp_rm->gr_name,
    565 						   grp_rm->gr_gid, user_pwd->pw_name);
    566 					}
    567 				}
    568 				endpwent();
    569 				continue;
    570 			} else {
    571 				cc = strlen(newent);
    572 				(void)strlcpy(buf, newent, sizeof(buf));
    573 			}
    574 		}
    575 		if (fwrite(buf, sizeof(char), (unsigned) cc, to) != cc) {
    576 			warn("Can't create gid: short write to `%s'", f);
    577 			(void)fclose(from);
    578 			(void)close(fd);
    579 			(void)unlink(f);
    580 			return 0;
    581 		}
    582 	}
    583 	(void)fclose(from);
    584 	(void)fclose(to);
    585 	if (rename(f, _PATH_GROUP) < 0) {
    586 		warn("Can't create gid: can't rename `%s' to `%s'", f,
    587 		    _PATH_GROUP);
    588 		(void)unlink(f);
    589 		return 0;
    590 	}
    591 	(void)chmod(_PATH_GROUP, st.st_mode & 07777);
    592 	if (newent == NULL) {
    593 		syslog(LOG_INFO, "group deleted: name=%s", group);
    594 	} else {
    595 		syslog(LOG_INFO, "group information modified: name=%s", group);
    596 	}
    597 	return 1;
    598 }
    599 
    600 /* modify the group entries for all `groups', by adding `user' */
    601 static int
    602 append_group(char *user, int ngroups, const char **groups)
    603 {
    604 	struct group	*grp;
    605 	struct stat	st;
    606 	FILE		*from;
    607 	FILE		*to;
    608 	char		buf[MaxEntryLen];
    609 	char		f[MaxFileNameLen];
    610 	char		*colon;
    611 	int		groupc;
    612 	int		entc;
    613 	int		fd;
    614 	int		nc;
    615 	int		cc;
    616 	int		i;
    617 	int		j;
    618 
    619 	for (i = 0 ; i < ngroups ; i++) {
    620 		if ((grp = getgrnam(groups[i])) == NULL) {
    621 			warnx("Can't append group `%s' for user `%s'",
    622 			    groups[i], user);
    623 		} else {
    624 			for (j = 0 ; grp->gr_mem[j] ; j++) {
    625 				if (strcmp(user, grp->gr_mem[j]) == 0) {
    626 					/* already in it */
    627 					groups[i] = "";
    628 				}
    629 			}
    630 		}
    631 	}
    632 	if ((from = fopen(_PATH_GROUP, "r")) == NULL) {
    633 		warn("Can't append group for `%s': can't open `%s'",
    634 		    user, _PATH_GROUP);
    635 		return 0;
    636 	}
    637 	if (flock(fileno(from), LOCK_EX | LOCK_NB) < 0) {
    638 		warn("Can't lock `%s'", _PATH_GROUP);
    639 	}
    640 	(void)fstat(fileno(from), &st);
    641 	(void)snprintf(f, sizeof(f), "%s.XXXXXX", _PATH_GROUP);
    642 	if ((fd = mkstemp(f)) < 0) {
    643 		warn("Can't create gid: mkstemp failed");
    644 		(void)fclose(from);
    645 		return 0;
    646 	}
    647 	if ((to = fdopen(fd, "w")) == NULL) {
    648 		warn("Can't create gid: fdopen `%s' failed", f);
    649 		(void)fclose(from);
    650 		(void)close(fd);
    651 		(void)unlink(f);
    652 		return 0;
    653 	}
    654 	while (fgets(buf, sizeof(buf), from) != NULL) {
    655 		cc = strlen(buf);
    656 		if ((colon = strchr(buf, ':')) == NULL) {
    657 			warnx("Badly formed entry `%s'", buf);
    658 			continue;
    659 		}
    660 		entc = (int)(colon - buf);
    661 		for (i = 0 ; i < ngroups ; i++) {
    662 			if ((groupc = strlen(groups[i])) == 0) {
    663 				continue;
    664 			}
    665 			if (entc == groupc &&
    666 			    strncmp(groups[i], buf, (unsigned) entc) == 0) {
    667 				if ((nc = snprintf(&buf[cc - 1],
    668 				    sizeof(buf) - cc + 1, "%s%s\n",
    669 				    (buf[cc - 2] == ':') ? "" : ",", user)) < 0) {
    670 					warnx("Warning: group `%s' "
    671 					    "entry too long", groups[i]);
    672 				}
    673 				cc += nc - 1;
    674 			}
    675 		}
    676 		if (fwrite(buf, sizeof(char), (unsigned) cc, to) != cc) {
    677 			warn("Can't create gid: short write to `%s'", f);
    678 			(void)fclose(from);
    679 			(void)close(fd);
    680 			(void)unlink(f);
    681 			return 0;
    682 		}
    683 	}
    684 	(void)fclose(from);
    685 	(void)fclose(to);
    686 	if (rename(f, _PATH_GROUP) < 0) {
    687 		warn("Can't create gid: can't rename `%s' to `%s'",
    688 		    f, _PATH_GROUP);
    689 		(void)unlink(f);
    690 		return 0;
    691 	}
    692 	(void)chmod(_PATH_GROUP, st.st_mode & 07777);
    693 	return 1;
    694 }
    695 
    696 /* the valid characters for login and group names */
    697 #define VALID_CHAR(c)	(isalnum(c) || (c) == '.' || (c) == '_' || (c) == '-')
    698 
    699 /* return 1 if `login' is a valid login name */
    700 static int
    701 valid_login(char *login_name, int allow_samba)
    702 {
    703 	unsigned char	*cp;
    704 
    705 	/* First character of a login name cannot be '-'. */
    706 	if (*login_name == '-') {
    707 		return 0;
    708 	}
    709 	if (strlen(login_name) >= LOGIN_NAME_MAX) {
    710 		return 0;
    711 	}
    712 	for (cp = (unsigned char *)login_name ; *cp ; cp++) {
    713 		if (!VALID_CHAR(*cp)) {
    714 #ifdef EXTENSIONS
    715 			/* check for a trailing '$' in a Samba user name */
    716 			if (allow_samba && *cp == '$' && *(cp + 1) == 0x0) {
    717 				return 1;
    718 			}
    719 #endif
    720 			return 0;
    721 		}
    722 	}
    723 	return 1;
    724 }
    725 
    726 /* return 1 if `group' is a valid group name */
    727 static int
    728 valid_group(char *group)
    729 {
    730 	unsigned char	*cp;
    731 
    732 	for (cp = (unsigned char *)group; *cp; cp++) {
    733 		if (!VALID_CHAR(*cp)) {
    734 			return 0;
    735 		}
    736 	}
    737 	return 1;
    738 }
    739 
    740 /* find the next gid in the range lo .. hi */
    741 static int
    742 getnextgid(int *gidp, int lo, int hi)
    743 {
    744 	for (*gidp = lo ; *gidp < hi ; *gidp += 1) {
    745 		if (getgrgid((gid_t)*gidp) == NULL) {
    746 			return 1;
    747 		}
    748 	}
    749 	return 0;
    750 }
    751 
    752 #ifdef EXTENSIONS
    753 /* save a range of uids */
    754 static int
    755 save_range(user_t *up, char *cp)
    756 {
    757 	int	from;
    758 	int	to;
    759 	int	i;
    760 
    761 	if (up->u_rsize == 0) {
    762 		up->u_rsize = 32;
    763 		NEWARRAY(range_t, up->u_rv, up->u_rsize, return(0));
    764 	} else if (up->u_rc == up->u_rsize) {
    765 		up->u_rsize *= 2;
    766 		RENEW(range_t, up->u_rv, up->u_rsize, return(0));
    767 	}
    768 	if (up->u_rv && sscanf(cp, "%d..%d", &from, &to) == 2) {
    769 		for (i = up->u_defrc ; i < up->u_rc ; i++) {
    770 			if (up->u_rv[i].r_from == from &&
    771 			    up->u_rv[i].r_to == to) {
    772 				break;
    773 			}
    774 		}
    775 		if (i == up->u_rc) {
    776 			up->u_rv[up->u_rc].r_from = from;
    777 			up->u_rv[up->u_rc].r_to = to;
    778 			up->u_rc += 1;
    779 		}
    780 	} else {
    781 		warnx("Bad range `%s'", cp);
    782 		return 0;
    783 	}
    784 	return 1;
    785 }
    786 #endif
    787 
    788 /* set the defaults in the defaults file */
    789 static int
    790 setdefaults(user_t *up)
    791 {
    792 	char	template[MaxFileNameLen];
    793 	FILE	*fp;
    794 	int	ret;
    795 	int	fd;
    796 #ifdef EXTENSIONS
    797 	int	i;
    798 #endif
    799 
    800 	(void)snprintf(template, sizeof(template), "%s.XXXXXX", CONFFILE);
    801 	if ((fd = mkstemp(template)) < 0) {
    802 		warnx("Can't mkstemp `%s' for writing", CONFFILE);
    803 		return 0;
    804 	}
    805 	if ((fp = fdopen(fd, "w")) == NULL) {
    806 		warn("Can't fdopen `%s' for writing", CONFFILE);
    807 		return 0;
    808 	}
    809 	ret = 1;
    810 	if (fprintf(fp, "group\t\t%s\n", up->u_primgrp) <= 0 ||
    811 	    fprintf(fp, "base_dir\t%s\n", up->u_basedir) <= 0 ||
    812 	    fprintf(fp, "skel_dir\t%s\n", up->u_skeldir) <= 0 ||
    813 	    fprintf(fp, "shell\t\t%s\n", up->u_shell) <= 0 ||
    814 #ifdef EXTENSIONS
    815 	    fprintf(fp, "class\t\t%s\n", up->u_class) <= 0 ||
    816 #endif
    817 	    fprintf(fp, "inactive\t%s\n", (up->u_inactive == NULL) ?
    818 		UNSET_INACTIVE : up->u_inactive) <= 0 ||
    819 	    fprintf(fp, "expire\t\t%s\n", (up->u_expire == NULL) ?
    820 		UNSET_EXPIRY : up->u_expire) <= 0 ||
    821 	    fprintf(fp, "preserve\t%s\n", (up->u_preserve == 0) ?
    822 		"false" : "true") <= 0) {
    823 		warn("Can't write to `%s'", CONFFILE);
    824 		ret = 0;
    825 	}
    826 #ifdef EXTENSIONS
    827 	for (i = (up->u_defrc != up->u_rc) ? up->u_defrc : 0;
    828 	    i < up->u_rc ; i++) {
    829 		if (fprintf(fp, "range\t\t%d..%d\n", up->u_rv[i].r_from,
    830 		    up->u_rv[i].r_to) <= 0) {
    831 			warn("Can't write to `%s'", CONFFILE);
    832 			ret = 0;
    833 		}
    834 	}
    835 #endif
    836 	(void)fclose(fp);
    837 	if (ret) {
    838 		ret = ((rename(template, CONFFILE) == 0) &&
    839 		    (chmod(CONFFILE, 0644) == 0));
    840 	}
    841 	return ret;
    842 }
    843 
    844 /* read the defaults file */
    845 static void
    846 read_defaults(user_t *up)
    847 {
    848 	struct stat	st;
    849 	size_t		lineno;
    850 	size_t		len;
    851 	FILE		*fp;
    852 	char		*cp;
    853 	char		*s;
    854 
    855 	memsave(&up->u_primgrp, DEF_GROUP, strlen(DEF_GROUP));
    856 	memsave(&up->u_basedir, DEF_BASEDIR, strlen(DEF_BASEDIR));
    857 	memsave(&up->u_skeldir, DEF_SKELDIR, strlen(DEF_SKELDIR));
    858 	memsave(&up->u_shell, DEF_SHELL, strlen(DEF_SHELL));
    859 	memsave(&up->u_comment, DEF_COMMENT, strlen(DEF_COMMENT));
    860 #ifdef EXTENSIONS
    861 	memsave(&up->u_class, DEF_CLASS, strlen(DEF_CLASS));
    862 #endif
    863 	up->u_rsize = 16;
    864 	up->u_defrc = 0;
    865 	NEWARRAY(range_t, up->u_rv, up->u_rsize, exit(1));
    866 	up->u_inactive = DEF_INACTIVE;
    867 	up->u_expire = DEF_EXPIRE;
    868 	if ((fp = fopen(CONFFILE, "r")) == NULL) {
    869 		if (stat(CONFFILE, &st) < 0 && !setdefaults(up)) {
    870 			warn("Can't create `%s' defaults file", CONFFILE);
    871 		}
    872 		fp = fopen(CONFFILE, "r");
    873 	}
    874 	if (fp != NULL) {
    875 		while ((s = fparseln(fp, &len, &lineno, NULL, 0)) != NULL) {
    876 			if (strncmp(s, "group", 5) == 0) {
    877 				cp = skipspace(s + 5);
    878 				memsave(&up->u_primgrp, (char *)cp, strlen(cp));
    879 			} else if (strncmp(s, "base_dir", 8) == 0) {
    880 				cp = skipspace(s + 8);
    881 				memsave(&up->u_basedir, (char *)cp, strlen(cp));
    882 			} else if (strncmp(s, "skel_dir", 8) == 0) {
    883 				cp = skipspace(s + 8);
    884 				memsave(&up->u_skeldir, (char *)cp, strlen(cp));
    885 			} else if (strncmp(s, "shell", 5) == 0) {
    886 				cp = skipspace(s + 5);
    887 				memsave(&up->u_shell, cp, strlen(cp));
    888 #ifdef EXTENSIONS
    889 			} else if (strncmp((char *)s, "class", 5) == 0) {
    890 				cp = skipspace(s + 5);
    891 				memsave(&up->u_class, cp, strlen(cp));
    892 #endif
    893 			} else if (strncmp(s, "inactive", 8) == 0) {
    894 				cp = skipspace(s + 8);
    895 				if (strcmp(cp, UNSET_INACTIVE) == 0) {
    896 					if (up->u_inactive) {
    897 						FREE(up->u_inactive);
    898 					}
    899 					up->u_inactive = NULL;
    900 				} else {
    901 					memsave(&up->u_inactive, cp, strlen(cp));
    902 				}
    903 #ifdef EXTENSIONS
    904 			} else if (strncmp(s, "range", 5) == 0) {
    905 				cp = skipspace(s + 5);
    906 				(void)save_range(up, cp);
    907 #endif
    908 #ifdef EXTENSIONS
    909 			} else if (strncmp(s, "preserve", 8) == 0) {
    910 				cp = skipspace(s + 8);
    911 				up->u_preserve =
    912 				    (strncmp(cp, "true", 4) == 0) ? 1 :
    913 				    (strncmp(cp, "yes", 3) == 0) ? 1 : atoi(cp);
    914 #endif
    915 			} else if (strncmp(s, "expire", 6) == 0) {
    916 				cp = skipspace(s + 6);
    917 				if (strcmp(cp, UNSET_EXPIRY) == 0) {
    918 					if (up->u_expire) {
    919 						FREE(up->u_expire);
    920 					}
    921 					up->u_expire = NULL;
    922 				} else {
    923 					memsave(&up->u_expire, cp, strlen(cp));
    924 				}
    925 			}
    926 			(void)free(s);
    927 		}
    928 		(void)fclose(fp);
    929 	}
    930 	if (up->u_rc == 0) {
    931 		up->u_rv[up->u_rc].r_from = DEF_LOWUID;
    932 		up->u_rv[up->u_rc].r_to = DEF_HIGHUID;
    933 		up->u_rc += 1;
    934 	}
    935 	up->u_defrc = up->u_rc;
    936 }
    937 
    938 /* return the next valid unused uid */
    939 static int
    940 getnextuid(int sync_uid_gid, int *uid, int low_uid, int high_uid)
    941 {
    942 	for (*uid = low_uid ; *uid <= high_uid ; (*uid)++) {
    943 		if (getpwuid((uid_t)(*uid)) == NULL && *uid != NOBODY_UID) {
    944 			if (sync_uid_gid) {
    945 				if (getgrgid((gid_t)(*uid)) == NULL) {
    946 					return 1;
    947 				}
    948 			} else {
    949 				return 1;
    950 			}
    951 		}
    952 	}
    953 	return 0;
    954 }
    955 
    956 /* structure which defines a password type */
    957 typedef struct passwd_type_t {
    958 	const char     *type;		/* optional type descriptor */
    959 	size_t		desc_length;	/* length of type descriptor */
    960 	size_t		length;		/* length of password */
    961 	const char     *regex;		/* regexp to output the password */
    962 	size_t		re_sub;		/* subscript of regexp to use */
    963 } passwd_type_t;
    964 
    965 static passwd_type_t	passwd_types[] = {
    966 	{ "$sha1",	5,	28,	"\\$[^$]+\\$[^$]+\\$[^$]+\\$(.*)", 1 },	/* SHA1 */
    967 	{ "$2a",	3,	54,	"\\$[^$]+\\$[^$]+\\$(.*)",	1 },	/* Blowfish */
    968 	{ "$1",		2,	34,	NULL,				0 },	/* MD5 */
    969 	{ "",		0,	DES_Len,NULL,				0 },	/* standard DES */
    970 	{ NULL,		(size_t)~0,	(size_t)~0,	NULL,		0 }
    971 	/* none - terminate search */
    972 };
    973 
    974 /* return non-zero if it's a valid password - check length for cipher type */
    975 static int
    976 valid_password_length(char *newpasswd)
    977 {
    978 	passwd_type_t  *pwtp;
    979 	regmatch_t	matchv[10];
    980 	regex_t		r;
    981 
    982 	for (pwtp = passwd_types; pwtp->desc_length != (size_t)~0; pwtp++) {
    983 		if (strncmp(newpasswd, pwtp->type, pwtp->desc_length) == 0) {
    984 			if (pwtp->regex == NULL) {
    985 				return strlen(newpasswd) == pwtp->length;
    986 			}
    987 			(void)regcomp(&r, pwtp->regex, REG_EXTENDED);
    988 			if (regexec(&r, newpasswd, 10, matchv, 0) == 0) {
    989 				regfree(&r);
    990 				return (int)(matchv[pwtp->re_sub].rm_eo -
    991 				    matchv[pwtp->re_sub].rm_so + 1) ==
    992 				    pwtp->length;
    993 			}
    994 			regfree(&r);
    995 		}
    996 	}
    997 	return 0;
    998 }
    999 
   1000 #ifdef EXTENSIONS
   1001 static int
   1002 valid_class(char *class)
   1003 {
   1004 	login_cap_t *lc;
   1005 
   1006 	if (class == NULL || *class == '\0') {
   1007 		return 1;
   1008 	}
   1009 	/*
   1010 	 * Check if /etc/login.conf exists. login_getclass() will
   1011 	 * return 1 due to it not existing, so not informing the
   1012 	 * user the actual login class does not exist.
   1013 	 */
   1014 
   1015 	if (access(PATH_LOGINCONF, R_OK) == -1) {
   1016 		warn("Access failed for `%s'; will not validate class `%s'",
   1017 		    PATH_LOGINCONF, class);
   1018 		return 1;
   1019 	}
   1020 
   1021 	if ((lc = login_getclass(class)) != NULL) {
   1022 		login_close(lc);
   1023 		return 1;
   1024 	}
   1025 	return 0;
   1026 }
   1027 #endif
   1028 
   1029 /* look for a valid time, return 0 if it was specified but bad */
   1030 static int
   1031 scantime(time_t *tp, char *s)
   1032 {
   1033 	struct tm	tm;
   1034 	char *ep;
   1035 	long val;
   1036 
   1037 	*tp = 0;
   1038 	if (s != NULL) {
   1039 		(void)memset(&tm, 0, sizeof(tm));
   1040 		if (strptime(s, "%c", &tm) != NULL) {
   1041 			*tp = mktime(&tm);
   1042 			return (*tp == -1) ? 0 : 1;
   1043 		} else if (strptime(s, "%B %d %Y", &tm) != NULL) {
   1044 			*tp = mktime(&tm);
   1045 			return (*tp == -1) ? 0 : 1;
   1046 		} else {
   1047 			errno = 0;
   1048 			*tp = val = strtol(s, &ep, 10);
   1049 			if (*ep != '\0' || *tp < -1 || errno == ERANGE) {
   1050 				*tp = 0;
   1051 				return 0;
   1052 			}
   1053 			if (*tp != val) {
   1054 				return 0;
   1055 			}
   1056 		}
   1057 	}
   1058 	return 1;
   1059 }
   1060 
   1061 /* add a user */
   1062 static int
   1063 adduser(char *login_name, user_t *up)
   1064 {
   1065 	struct group	*grp;
   1066 	struct stat	st;
   1067 	time_t		expire;
   1068 	time_t		inactive;
   1069 	char		password[PasswordLength + 1];
   1070 	char		home[MaxFileNameLen];
   1071 	char		buf[MaxFileNameLen];
   1072 	int		sync_uid_gid;
   1073 	int		masterfd;
   1074 	int		ptmpfd;
   1075 	int		gid;
   1076 	int		cc;
   1077 	int		i;
   1078 
   1079 	if (!valid_login(login_name, up->u_allow_samba)) {
   1080 		errx(EXIT_FAILURE, "`%s' is not a valid login name", login_name);
   1081 	}
   1082 #ifdef EXTENSIONS
   1083 	if (!valid_class(up->u_class)) {
   1084 		errx(EXIT_FAILURE, "No such login class `%s'", up->u_class);
   1085 	}
   1086 #endif
   1087 	if ((masterfd = open(_PATH_MASTERPASSWD, O_RDONLY)) < 0) {
   1088 		err(EXIT_FAILURE, "Can't open `%s'", _PATH_MASTERPASSWD);
   1089 	}
   1090 	if (flock(masterfd, LOCK_EX | LOCK_NB) < 0) {
   1091 		err(EXIT_FAILURE, "Can't lock `%s'", _PATH_MASTERPASSWD);
   1092 	}
   1093 	pw_init();
   1094 	if ((ptmpfd = pw_lock(WAITSECS)) < 0) {
   1095 		int serrno = errno;
   1096 		(void)close(masterfd);
   1097 		errno = serrno;
   1098 		err(EXIT_FAILURE, "Can't obtain pw_lock");
   1099 	}
   1100 	while ((cc = read(masterfd, buf, sizeof(buf))) > 0) {
   1101 		if (write(ptmpfd, buf, (size_t)(cc)) != cc) {
   1102 			int serrno = errno;
   1103 			(void)close(masterfd);
   1104 			(void)close(ptmpfd);
   1105 			(void)pw_abort();
   1106 			errno = serrno;
   1107 			err(EXIT_FAILURE, "Short write to /etc/ptmp "
   1108 			    "(not %d chars)", cc);
   1109 		}
   1110 	}
   1111 	/* if no uid was specified, get next one in [low_uid..high_uid] range */
   1112 	sync_uid_gid = (strcmp(up->u_primgrp, "=uid") == 0);
   1113 	if (up->u_uid == -1) {
   1114 		int	got_id = 0;
   1115 
   1116 		/*
   1117 		 * Look for a free UID in the command line ranges (if any).
   1118 		 * These start after the ranges specified in the config file.
   1119 		 */
   1120 		for (i = up->u_defrc; !got_id && i < up->u_rc ; i++) {
   1121 			got_id = getnextuid(sync_uid_gid, &up->u_uid,
   1122 					up->u_rv[i].r_from, up->u_rv[i].r_to);
   1123 		}
   1124 		/*
   1125 		 * If there were no free UIDs in the command line ranges,
   1126 		 * try the ranges from the config file (there will always
   1127 		 * be at least one default).
   1128 		 */
   1129 		for (i = 0; !got_id && i < up->u_defrc; i++) {
   1130 			got_id = getnextuid(sync_uid_gid, &up->u_uid,
   1131 					up->u_rv[i].r_from, up->u_rv[i].r_to);
   1132 		}
   1133 		if (!got_id) {
   1134 			(void)close(ptmpfd);
   1135 			(void)pw_abort();
   1136 			errx(EXIT_FAILURE, "can't get next uid for %d",
   1137 			    up->u_uid);
   1138 		}
   1139 	}
   1140 	/* check uid isn't already allocated */
   1141 	if (!(up->u_flags & F_DUPUID) && getpwuid((uid_t)(up->u_uid)) != NULL) {
   1142 		(void)close(ptmpfd);
   1143 		(void)pw_abort();
   1144 		errx(EXIT_FAILURE, "uid %d is already in use", up->u_uid);
   1145 	}
   1146 	/* if -g=uid was specified, check gid is unused */
   1147 	if (sync_uid_gid) {
   1148 		if (getgrgid((gid_t)(up->u_uid)) != NULL) {
   1149 			(void)close(ptmpfd);
   1150 			(void)pw_abort();
   1151 			errx(EXIT_FAILURE, "gid %d is already in use",
   1152 			    up->u_uid);
   1153 		}
   1154 		gid = up->u_uid;
   1155 	} else if ((grp = getgrnam(up->u_primgrp)) != NULL) {
   1156 		gid = grp->gr_gid;
   1157 	} else if (is_number(up->u_primgrp) &&
   1158 		   (grp = getgrgid((gid_t)atoi(up->u_primgrp))) != NULL) {
   1159 		gid = grp->gr_gid;
   1160 	} else {
   1161 		(void)close(ptmpfd);
   1162 		(void)pw_abort();
   1163 		errx(EXIT_FAILURE, "group %s not found", up->u_primgrp);
   1164 	}
   1165 	/* check name isn't already in use */
   1166 	if (!(up->u_flags & F_DUPUID) && getpwnam(login_name) != NULL) {
   1167 		(void)close(ptmpfd);
   1168 		(void)pw_abort();
   1169 		errx(EXIT_FAILURE, "already a `%s' user", login_name);
   1170 	}
   1171 	if (up->u_flags & F_HOMEDIR) {
   1172 		(void)strlcpy(home, up->u_home, sizeof(home));
   1173 	} else {
   1174 		/* if home directory hasn't been given, make it up */
   1175 		(void)snprintf(home, sizeof(home), "%s/%s", up->u_basedir,
   1176 		    login_name);
   1177 	}
   1178 	if (!scantime(&inactive, up->u_inactive)) {
   1179 		warnx("Warning: inactive time `%s' invalid, password expiry off",
   1180 				up->u_inactive);
   1181 	}
   1182 	if (!scantime(&expire, up->u_expire) || expire == -1) {
   1183 		warnx("Warning: expire time `%s' invalid, account expiry off",
   1184 				up->u_expire);
   1185 		expire = 0; /* Just in case. */
   1186 	}
   1187 	if (lstat(home, &st) < 0 && !(up->u_flags & F_MKDIR)) {
   1188 		warnx("Warning: home directory `%s' doesn't exist, "
   1189 		    "and -m was not specified", home);
   1190 	}
   1191 	password[sizeof(password) - 1] = '\0';
   1192 	if (up->u_password != NULL && valid_password_length(up->u_password)) {
   1193 		(void)strlcpy(password, up->u_password, sizeof(password));
   1194 	} else {
   1195 		(void)memset(password, '*', DES_Len);
   1196 		password[DES_Len] = 0;
   1197 		if (up->u_password != NULL) {
   1198 			warnx("Password `%s' is invalid: setting it to `%s'",
   1199 				up->u_password, password);
   1200 		}
   1201 	}
   1202 	cc = snprintf(buf, sizeof(buf), "%s:%s:%d:%d:%s:%ld:%ld:%s:%s:%s\n",
   1203 			login_name,
   1204 			password,
   1205 			up->u_uid,
   1206 			gid,
   1207 #ifdef EXTENSIONS
   1208 			up->u_class,
   1209 #else
   1210 			"",
   1211 #endif
   1212 			(long) inactive,
   1213 			(long) expire,
   1214 			up->u_comment,
   1215 			home,
   1216 			up->u_shell);
   1217 	if (write(ptmpfd, buf, (size_t) cc) != cc) {
   1218 		int serrno = errno;
   1219 		(void)close(ptmpfd);
   1220 		(void)pw_abort();
   1221 		errno = serrno;
   1222 		err(EXIT_FAILURE, "Can't add `%s'", buf);
   1223 	}
   1224 	if (up->u_flags & F_MKDIR) {
   1225 		if (lstat(home, &st) == 0) {
   1226 			(void)close(ptmpfd);
   1227 			(void)pw_abort();
   1228 			errx(EXIT_FAILURE,
   1229 			    "Home directory `%s' already exists", home);
   1230 		} else {
   1231 			if (asystem("%s -p %s", MKDIR, home) != 0) {
   1232 				int serrno = errno;
   1233 				(void)close(ptmpfd);
   1234 				(void)pw_abort();
   1235 				errno = serrno;
   1236 				err(EXIT_FAILURE, "Can't mkdir `%s'", home);
   1237 			}
   1238 			(void)copydotfiles(up->u_skeldir, up->u_uid, gid, home);
   1239 		}
   1240 	}
   1241 	if (strcmp(up->u_primgrp, "=uid") == 0 &&
   1242 	    getgrnam(login_name) == NULL &&
   1243 	    !creategid(login_name, gid, login_name)) {
   1244 		(void)close(ptmpfd);
   1245 		(void)pw_abort();
   1246 		errx(EXIT_FAILURE, "Can't create gid %d for login name %s",
   1247 		    gid, login_name);
   1248 	}
   1249 	if (up->u_groupc > 0 &&
   1250 	    !append_group(login_name, up->u_groupc, up->u_groupv)) {
   1251 		(void)close(ptmpfd);
   1252 		(void)pw_abort();
   1253 		errx(EXIT_FAILURE, "Can't append `%s' to new groups",
   1254 		    login_name);
   1255 	}
   1256 	(void)close(ptmpfd);
   1257 #if PW_MKDB_ARGC == 2
   1258 	if (pw_mkdb(login_name, 0) < 0)
   1259 #else
   1260 	if (pw_mkdb() < 0)
   1261 #endif
   1262 	{
   1263 		(void)pw_abort();
   1264 		errx(EXIT_FAILURE, "pw_mkdb failed");
   1265 	}
   1266 	syslog(LOG_INFO, "New user added: name=%s, uid=%d, gid=%d, home=%s, "
   1267 	    "shell=%s", login_name, up->u_uid, gid, home, up->u_shell);
   1268 	return 1;
   1269 }
   1270 
   1271 /* remove a user from the groups file */
   1272 static int
   1273 rm_user_from_groups(char *login_name)
   1274 {
   1275 	struct stat	st;
   1276 	regmatch_t	matchv[10];
   1277 	regex_t		r;
   1278 	FILE		*from;
   1279 	FILE		*to;
   1280 	char		line[MaxEntryLen];
   1281 	char		buf[MaxEntryLen];
   1282 	char		f[MaxFileNameLen];
   1283 	int		fd;
   1284 	int		cc;
   1285 	int		sc;
   1286 
   1287 	(void)snprintf(line, sizeof(line), "(:|,)(%s)(,|$)", login_name);
   1288 	if ((sc = regcomp(&r, line, REG_EXTENDED|REG_NEWLINE)) != 0) {
   1289 		(void)regerror(sc, &r, buf, sizeof(buf));
   1290 		warnx("Can't compile regular expression `%s' (%s)", line,
   1291 		    buf);
   1292 		return 0;
   1293 	}
   1294 	if ((from = fopen(_PATH_GROUP, "r")) == NULL) {
   1295 		warn("Can't remove gid for `%s': can't open `%s'",
   1296 		    login_name, _PATH_GROUP);
   1297 		return 0;
   1298 	}
   1299 	if (flock(fileno(from), LOCK_EX | LOCK_NB) < 0) {
   1300 		warn("can't lock `%s'", _PATH_GROUP);
   1301 	}
   1302 	(void)fstat(fileno(from), &st);
   1303 	(void)snprintf(f, sizeof(f), "%s.XXXXXX", _PATH_GROUP);
   1304 	if ((fd = mkstemp(f)) < 0) {
   1305 		warn("Can't create gid: mkstemp failed");
   1306 		(void)fclose(from);
   1307 		return 0;
   1308 	}
   1309 	if ((to = fdopen(fd, "w")) == NULL) {
   1310 		warn("Can't create gid: fdopen `%s' failed", f);
   1311 		(void)fclose(from);
   1312 		(void)close(fd);
   1313 		(void)unlink(f);
   1314 		return 0;
   1315 	}
   1316 	while (fgets(buf, sizeof(buf), from) != NULL) {
   1317 		cc = strlen(buf);
   1318 		if (regexec(&r, buf, 10, matchv, 0) == 0) {
   1319 			if (buf[(int)matchv[1].rm_so] == ',') {
   1320 				matchv[2].rm_so = matchv[1].rm_so;
   1321 			} else if (matchv[2].rm_eo != matchv[3].rm_eo) {
   1322 				matchv[2].rm_eo = matchv[3].rm_eo;
   1323 			}
   1324 			cc -= (int) matchv[2].rm_eo;
   1325 			sc = (int) matchv[2].rm_so;
   1326 			if (fwrite(buf, sizeof(char), (size_t)sc, to) != sc ||
   1327 			    fwrite(&buf[(int)matchv[2].rm_eo], sizeof(char),
   1328 				(size_t)cc, to) != cc) {
   1329 				warn("Can't create gid: short write to `%s'",
   1330 				    f);
   1331 				(void)fclose(from);
   1332 				(void)close(fd);
   1333 				(void)unlink(f);
   1334 				return 0;
   1335 			}
   1336 		} else if (fwrite(buf, sizeof(char), (unsigned) cc, to) != cc) {
   1337 			warn("Can't create gid: short write to `%s'", f);
   1338 			(void)fclose(from);
   1339 			(void)close(fd);
   1340 			(void)unlink(f);
   1341 			return 0;
   1342 		}
   1343 	}
   1344 	(void)fclose(from);
   1345 	(void)fclose(to);
   1346 	if (rename(f, _PATH_GROUP) < 0) {
   1347 		warn("Can't create gid: can't rename `%s' to `%s'",
   1348 		    f, _PATH_GROUP);
   1349 		(void)unlink(f);
   1350 		return 0;
   1351 	}
   1352 	(void)chmod(_PATH_GROUP, st.st_mode & 07777);
   1353 	return 1;
   1354 }
   1355 
   1356 /* check that the user or group is local, not from YP/NIS */
   1357 static int
   1358 is_local(char *name, const char *file)
   1359 {
   1360 	FILE	       *fp;
   1361 	char		buf[MaxEntryLen];
   1362 	size_t		len;
   1363 	int		ret;
   1364 
   1365 	if ((fp = fopen(file, "r")) == NULL) {
   1366 		err(EXIT_FAILURE, "Can't open `%s'", file);
   1367 	}
   1368 	len = strlen(name);
   1369 	for (ret = 0 ; fgets(buf, sizeof(buf), fp) != NULL ; ) {
   1370 		if (strncmp(buf, name, len) == 0 && buf[len] == ':') {
   1371 			ret = 1;
   1372 			break;
   1373 		}
   1374 	}
   1375 	(void)fclose(fp);
   1376 	return ret;
   1377 }
   1378 
   1379 /* modify a user */
   1380 static int
   1381 moduser(char *login_name, char *newlogin, user_t *up, int allow_samba)
   1382 {
   1383 	struct passwd  *pwp;
   1384 	struct group   *grp;
   1385 	const char     *homedir;
   1386 	char	       *locked_pwd;
   1387 	size_t		colonc;
   1388 	size_t		loginc;
   1389 	size_t		len;
   1390 	size_t		cc;
   1391 	FILE	       *master;
   1392 	char		newdir[MaxFileNameLen];
   1393 	char	        buf[MaxEntryLen];
   1394 	char	       *colon;
   1395 	int		masterfd;
   1396 	int		ptmpfd;
   1397 	int		error;
   1398 
   1399 	if (!valid_login(newlogin, allow_samba)) {
   1400 		errx(EXIT_FAILURE, "`%s' is not a valid login name",
   1401 		    login_name);
   1402 	}
   1403 	if ((pwp = getpwnam(login_name)) == NULL) {
   1404 		errx(EXIT_FAILURE, "No such user `%s'", login_name);
   1405 	}
   1406 	if (!is_local(login_name, _PATH_MASTERPASSWD)) {
   1407 		errx(EXIT_FAILURE, "User `%s' must be a local user",
   1408 		    login_name);
   1409 	}
   1410 	/* keep dir name in case we need it for '-m' */
   1411 	homedir = pwp->pw_dir;
   1412 
   1413 	if ((masterfd = open(_PATH_MASTERPASSWD, O_RDONLY)) < 0) {
   1414 		err(EXIT_FAILURE, "Can't open `%s'", _PATH_MASTERPASSWD);
   1415 	}
   1416 	if (flock(masterfd, LOCK_EX | LOCK_NB) < 0) {
   1417 		err(EXIT_FAILURE, "Can't lock `%s'", _PATH_MASTERPASSWD);
   1418 	}
   1419 	pw_init();
   1420 	if ((ptmpfd = pw_lock(WAITSECS)) < 0) {
   1421 		int serrno = errno;
   1422 		(void)close(masterfd);
   1423 		errno = serrno;
   1424 		err(EXIT_FAILURE, "Can't obtain pw_lock");
   1425 	}
   1426 	if ((master = fdopen(masterfd, "r")) == NULL) {
   1427 		int serrno = errno;
   1428 		(void)close(masterfd);
   1429 		(void)close(ptmpfd);
   1430 		(void)pw_abort();
   1431 		errno = serrno;
   1432 		err(EXIT_FAILURE, "Can't fdopen fd for %s", _PATH_MASTERPASSWD);
   1433 	}
   1434 	if (up != NULL) {
   1435 		if (up->u_flags & F_USERNAME) {
   1436 			/*
   1437 			 * If changing name,
   1438 			 * check new name isn't already in use
   1439 			 */
   1440 			if (strcmp(login_name, newlogin) != 0 &&
   1441 			    getpwnam(newlogin) != NULL) {
   1442 				(void)close(ptmpfd);
   1443 				(void)pw_abort();
   1444 				errx(EXIT_FAILURE, "Already a `%s' user",
   1445 				    newlogin);
   1446 			}
   1447 			pwp->pw_name = newlogin;
   1448 
   1449 			/*
   1450 			 * Provide a new directory name in case the
   1451 			 * home directory is to be moved.
   1452 			 */
   1453 			if (up->u_flags & F_MKDIR) {
   1454 				(void)snprintf(newdir, sizeof(newdir), "%s/%s",
   1455 				    up->u_basedir, newlogin);
   1456 				pwp->pw_dir = newdir;
   1457 			}
   1458 		}
   1459 		if (up->u_flags & F_PASSWORD) {
   1460 			if (up->u_password != NULL) {
   1461 				if (!valid_password_length(up->u_password)) {
   1462 					(void)close(ptmpfd);
   1463 					(void)pw_abort();
   1464 					errx(EXIT_FAILURE,
   1465 					    "Invalid password: `%s'",
   1466 					    up->u_password);
   1467 				}
   1468 				if ((locked_pwd =
   1469 				    strstr(pwp->pw_passwd, LOCKED)) != NULL) {
   1470 					/*
   1471 					 * account is locked - keep it locked
   1472 					 * and just change the password.
   1473 					 */
   1474 					if (asprintf(&locked_pwd, "%s%s",
   1475 					    LOCKED, up->u_password) == -1) {
   1476 						err(EXIT_FAILURE,
   1477 						    "asprintf failed");
   1478 					}
   1479 					pwp->pw_passwd = locked_pwd;
   1480 				} else {
   1481 					pwp->pw_passwd = up->u_password;
   1482 				}
   1483 			}
   1484 		}
   1485 
   1486 		/* check whether we should lock the account. */
   1487 		if (up->u_locked == LOCK) {
   1488 			/* check to see account if already locked. */
   1489 			if ((locked_pwd = strstr(pwp->pw_passwd, LOCKED))
   1490 			    != NULL) {
   1491 				warnx("Account is already locked");
   1492 			} else {
   1493 				if (asprintf(&locked_pwd, "%s%s", LOCKED,
   1494 				    pwp->pw_passwd) == -1) {
   1495 					err(EXIT_FAILURE, "asprintf failed");
   1496 				}
   1497 				pwp->pw_passwd = locked_pwd;
   1498 			}
   1499 		} else if (up->u_locked == UNLOCK) {
   1500 			if ((locked_pwd = strstr(pwp->pw_passwd, LOCKED))
   1501 			    == NULL) {
   1502 				warnx("Account '%s' is not locked", login_name);
   1503 			} else {
   1504 				pwp->pw_passwd = locked_pwd + strlen(LOCKED);
   1505 			}
   1506 		}
   1507 
   1508 		if (up->u_flags & F_UID) {
   1509 			/* check uid isn't already allocated */
   1510 			if (!(up->u_flags & F_DUPUID) &&
   1511 			    getpwuid((uid_t)(up->u_uid)) != NULL) {
   1512 				(void)close(ptmpfd);
   1513 				(void)pw_abort();
   1514 				errx(EXIT_FAILURE, "Uid %d is already in use",
   1515 				    up->u_uid);
   1516 			}
   1517 			pwp->pw_uid = up->u_uid;
   1518 		}
   1519 		if (up->u_flags & F_GROUP) {
   1520 			/* if -g=uid was specified, check gid is unused */
   1521 			if (strcmp(up->u_primgrp, "=uid") == 0) {
   1522 				if (getgrgid((gid_t)(up->u_uid)) != NULL) {
   1523 					(void)close(ptmpfd);
   1524 					(void)pw_abort();
   1525 					errx(EXIT_FAILURE,
   1526 					    "Gid %d is already in use",
   1527 					    up->u_uid);
   1528 				}
   1529 				pwp->pw_gid = up->u_uid;
   1530 			} else if ((grp = getgrnam(up->u_primgrp)) != NULL) {
   1531 				pwp->pw_gid = grp->gr_gid;
   1532 			} else if (is_number(up->u_primgrp) &&
   1533 				   (grp = getgrgid(
   1534 				   (gid_t)atoi(up->u_primgrp))) != NULL) {
   1535 				pwp->pw_gid = grp->gr_gid;
   1536 			} else {
   1537 				(void)close(ptmpfd);
   1538 				(void)pw_abort();
   1539 				errx(EXIT_FAILURE, "Group %s not found",
   1540 				    up->u_primgrp);
   1541 			}
   1542 		}
   1543 		if (up->u_flags & F_INACTIVE) {
   1544 			if (!scantime(&pwp->pw_change, up->u_inactive)) {
   1545 				warnx("Warning: inactive time `%s' invalid, "
   1546 				    "password expiry off",
   1547 					up->u_inactive);
   1548 			}
   1549 		}
   1550 		if (up->u_flags & F_EXPIRE) {
   1551 			if (!scantime(&pwp->pw_expire, up->u_expire) ||
   1552 			      pwp->pw_expire == -1) {
   1553 				warnx("Warning: expire time `%s' invalid, "
   1554 				    "account expiry off",
   1555 					up->u_expire);
   1556 				pwp->pw_expire = 0;
   1557 			}
   1558 		}
   1559 		if (up->u_flags & F_COMMENT) {
   1560 			pwp->pw_gecos = up->u_comment;
   1561 		}
   1562 		if (up->u_flags & F_HOMEDIR) {
   1563 			pwp->pw_dir = up->u_home;
   1564 		}
   1565 		if (up->u_flags & F_SHELL) {
   1566 			pwp->pw_shell = up->u_shell;
   1567 		}
   1568 #ifdef EXTENSIONS
   1569 		if (up->u_flags & F_CLASS) {
   1570 			if (!valid_class(up->u_class)) {
   1571 				(void)close(ptmpfd);
   1572 				(void)pw_abort();
   1573 				errx(EXIT_FAILURE, "No such login class `%s'",
   1574 				    up->u_class);
   1575 			}
   1576 			pwp->pw_class = up->u_class;
   1577 		}
   1578 #endif
   1579 	}
   1580 	loginc = strlen(login_name);
   1581 	while (fgets(buf, sizeof(buf), master) != NULL) {
   1582 		if ((colon = strchr(buf, ':')) == NULL) {
   1583 			warnx("Malformed entry `%s'. Skipping", buf);
   1584 			continue;
   1585 		}
   1586 		colonc = (size_t)(colon - buf);
   1587 		if (strncmp(login_name, buf, loginc) == 0 && loginc == colonc) {
   1588 			if (up != NULL) {
   1589 				len = snprintf(buf, sizeof(buf), "%s:%s:%d:%d:"
   1590 #ifdef EXTENSIONS
   1591 				    "%s"
   1592 #endif
   1593 				    ":%ld:%ld:%s:%s:%s\n",
   1594 				    newlogin,
   1595 				    pwp->pw_passwd,
   1596 				    pwp->pw_uid,
   1597 				    pwp->pw_gid,
   1598 #ifdef EXTENSIONS
   1599 				    pwp->pw_class,
   1600 #endif
   1601 				    (long)pwp->pw_change,
   1602 				    (long)pwp->pw_expire,
   1603 				    pwp->pw_gecos,
   1604 				    pwp->pw_dir,
   1605 				    pwp->pw_shell);
   1606 				if (write(ptmpfd, buf, len) != len) {
   1607 					int serrno = errno;
   1608 					(void)close(ptmpfd);
   1609 					(void)pw_abort();
   1610 					errno = serrno;
   1611 					err(EXIT_FAILURE, "Can't add `%s'",
   1612 					    buf);
   1613 				}
   1614 			}
   1615 		} else {
   1616 			len = strlen(buf);
   1617 			if ((cc = write(ptmpfd, buf, len)) != len) {
   1618 				int serrno = errno;
   1619 				(void)close(masterfd);
   1620 				(void)close(ptmpfd);
   1621 				(void)pw_abort();
   1622 				errno = serrno;
   1623 				err(EXIT_FAILURE, "Short write to /etc/ptmp "
   1624 				    "(%ld not %ld chars)",
   1625 					(unsigned long)cc,
   1626 					(unsigned long)len);
   1627 			}
   1628 		}
   1629 	}
   1630 	if (up != NULL) {
   1631 		if ((up->u_flags & F_MKDIR) &&
   1632 		    asystem("%s %s %s", MV, homedir, pwp->pw_dir) != 0) {
   1633 			int serrno = errno;
   1634 			(void)close(ptmpfd);
   1635 			(void)pw_abort();
   1636 			errno = serrno;
   1637 			err(EXIT_FAILURE, "Can't move `%s' to `%s'",
   1638 			    homedir, pwp->pw_dir);
   1639 		}
   1640 		if (up->u_groupc > 0 &&
   1641 		    !append_group(newlogin, up->u_groupc, up->u_groupv)) {
   1642 			(void)close(ptmpfd);
   1643 			(void)pw_abort();
   1644 			errx(EXIT_FAILURE, "Can't append `%s' to new groups",
   1645 			    newlogin);
   1646 		}
   1647 	}
   1648 	(void)close(ptmpfd);
   1649 #if PW_MKDB_ARGC == 2
   1650 	if (up != NULL && strcmp(login_name, newlogin) == 0) {
   1651 		error = pw_mkdb(login_name, 0);
   1652 	} else {
   1653 		error = pw_mkdb(NULL, 0);
   1654 	}
   1655 #else
   1656 	error = pw_mkdb();
   1657 #endif
   1658 	if (error < 0) {
   1659 		(void)pw_abort();
   1660 		errx(EXIT_FAILURE, "pw_mkdb failed");
   1661 	}
   1662 	if (up == NULL) {
   1663 		syslog(LOG_INFO, "User removed: name=%s", login_name);
   1664 	} else if (strcmp(login_name, newlogin) == 0) {
   1665 		syslog(LOG_INFO, "User information modified: name=%s, uid=%d, "
   1666 		    "gid=%d, home=%s, shell=%s",
   1667 		    login_name, pwp->pw_uid, pwp->pw_gid, pwp->pw_dir,
   1668 		    pwp->pw_shell);
   1669 	} else {
   1670 		syslog(LOG_INFO, "User information modified: name=%s, "
   1671 		    "new name=%s, uid=%d, gid=%d, home=%s, shell=%s",
   1672 		    login_name, newlogin, pwp->pw_uid, pwp->pw_gid,
   1673 		    pwp->pw_dir, pwp->pw_shell);
   1674 	}
   1675 	return 1;
   1676 }
   1677 
   1678 #ifdef EXTENSIONS
   1679 /* see if we can find out the user struct */
   1680 static struct passwd *
   1681 find_user_info(const char *name)
   1682 {
   1683 	struct passwd	*pwp;
   1684 
   1685 	if ((pwp = getpwnam(name)) != NULL) {
   1686 		return pwp;
   1687 	}
   1688 	if (is_number(name) && (pwp = getpwuid((uid_t)atoi(name))) != NULL) {
   1689 		return pwp;
   1690 	}
   1691 	return NULL;
   1692 }
   1693 #endif
   1694 
   1695 /* see if we can find out the group struct */
   1696 static struct group *
   1697 find_group_info(const char *name)
   1698 {
   1699 	struct group	*grp;
   1700 
   1701 	if ((grp = getgrnam(name)) != NULL) {
   1702 		return grp;
   1703 	}
   1704 	if (is_number(name) && (grp = getgrgid((gid_t)atoi(name))) != NULL) {
   1705 		return grp;
   1706 	}
   1707 	return NULL;
   1708 }
   1709 
   1710 /* print out usage message, and then exit */
   1711 void
   1712 usermgmt_usage(const char *prog)
   1713 {
   1714 	if (strcmp(prog, "useradd") == 0) {
   1715 		(void)fprintf(stderr, "usage: %s -D [-F] [-b base-dir] "
   1716 		    "[-e expiry-time] [-f inactive-time]\n"
   1717 		    "\t[-g gid | name | =uid] [-k skel-dir] [-L login-class]\n"
   1718 		    "\t[-r lowuid..highuid] [-s shell]\n", prog);
   1719 		(void)fprintf(stderr, "usage: %s [-moSv] [-b base-dir] "
   1720 		    "[-c comment] [-d home-dir] [-e expiry-time]\n"
   1721 		    "\t[-f inactive-time] [-G secondary-group] "
   1722 		    "[-g gid | name | =uid]\n"
   1723 		    "\t[-k skeletondir] [-L login-class] [-p password]"
   1724 		    "[-r lowuid..highuid]\n"
   1725 		    "\t[-s shell] [-u uid] user\n",
   1726 		    prog);
   1727 	} else if (strcmp(prog, "usermod") == 0) {
   1728 		(void)fprintf(stderr, "usage: %s [-FmoSv] [-C yes/no] "
   1729 		    "[-c comment] [-d home-dir] [-e expiry-time]\n"
   1730 		    "\t[-f inactive] [-G secondary-group] "
   1731 		    "[-g gid | name | =uid]\n"
   1732 		    "\t[-L login-class] [-l new-login] [-p password] "
   1733 		    "[-s shell] [-u uid]\n"
   1734 		    "\tuser\n", prog);
   1735 	} else if (strcmp(prog, "userdel") == 0) {
   1736 		(void)fprintf(stderr, "usage: %s -D [-p preserve-value]\n", prog);
   1737 		(void)fprintf(stderr,
   1738 		    "usage: %s [-rSv] [-p preserve-value] user\n", prog);
   1739 #ifdef EXTENSIONS
   1740 	} else if (strcmp(prog, "userinfo") == 0) {
   1741 		(void)fprintf(stderr, "usage: %s [-ev] user\n", prog);
   1742 #endif
   1743 	} else if (strcmp(prog, "groupadd") == 0) {
   1744 		(void)fprintf(stderr, "usage: %s [-ov] [-g gid]"
   1745 		    " [-r lowgid..highgid] group\n", prog);
   1746 	} else if (strcmp(prog, "groupdel") == 0) {
   1747 		(void)fprintf(stderr, "usage: %s [-v] group\n", prog);
   1748 	} else if (strcmp(prog, "groupmod") == 0) {
   1749 		(void)fprintf(stderr,
   1750 		    "usage: %s [-ov] [-g gid] [-n newname] group\n", prog);
   1751 	} else if (strcmp(prog, "user") == 0 || strcmp(prog, "group") == 0) {
   1752 		(void)fprintf(stderr,
   1753 		    "usage: %s ( add | del | mod | info ) ...\n", prog);
   1754 #ifdef EXTENSIONS
   1755 	} else if (strcmp(prog, "groupinfo") == 0) {
   1756 		(void)fprintf(stderr, "usage: %s [-ev] group\n", prog);
   1757 #endif
   1758 	}
   1759 	exit(EXIT_FAILURE);
   1760 	/* NOTREACHED */
   1761 }
   1762 
   1763 #ifdef EXTENSIONS
   1764 #define ADD_OPT_EXTENSIONS	"p:r:vL:S"
   1765 #else
   1766 #define ADD_OPT_EXTENSIONS
   1767 #endif
   1768 
   1769 int
   1770 useradd(int argc, char **argv)
   1771 {
   1772 	user_t	u;
   1773 	int	defaultfield;
   1774 	int	bigD;
   1775 	int	c;
   1776 #ifdef EXTENSIONS
   1777 	int	i;
   1778 #endif
   1779 
   1780 	(void)memset(&u, 0, sizeof(u));
   1781 	read_defaults(&u);
   1782 	u.u_uid = -1;
   1783 	defaultfield = bigD = 0;
   1784 	while ((c = getopt(argc, argv, "DFG:b:c:d:e:f:g:k:mou:s:"
   1785 	    ADD_OPT_EXTENSIONS)) != -1) {
   1786 		switch(c) {
   1787 		case 'D':
   1788 			bigD = 1;
   1789 			break;
   1790 		case 'F':
   1791 			/*
   1792 			 * Setting -1 will force the new user to
   1793 			 * change their password as soon as they
   1794 			 * next log in - passwd(5).
   1795 			 */
   1796 			defaultfield = 1;
   1797 			memsave(&u.u_inactive, "-1", strlen("-1"));
   1798 			break;
   1799 		case 'G':
   1800 			while ((u.u_groupv[u.u_groupc] = strsep(&optarg, ",")) != NULL &&
   1801 			       u.u_groupc < NGROUPS_MAX) {
   1802 				if (u.u_groupv[u.u_groupc][0] != 0) {
   1803 					u.u_groupc++;
   1804 				}
   1805 			}
   1806 			if (optarg != NULL) {
   1807 				warnx("Truncated list of secondary groups "
   1808 				    "to %d entries", NGROUPS_MAX);
   1809 			}
   1810 			break;
   1811 #ifdef EXTENSIONS
   1812 		case 'S':
   1813 			u.u_allow_samba = 1;
   1814 			break;
   1815 #endif
   1816 		case 'b':
   1817 			defaultfield = 1;
   1818 			memsave(&u.u_basedir, optarg, strlen(optarg));
   1819 			break;
   1820 		case 'c':
   1821 			memsave(&u.u_comment, optarg, strlen(optarg));
   1822 			break;
   1823 		case 'd':
   1824 			memsave(&u.u_home, optarg, strlen(optarg));
   1825 			u.u_flags |= F_HOMEDIR;
   1826 			break;
   1827 		case 'e':
   1828 			defaultfield = 1;
   1829 			memsave(&u.u_expire, optarg, strlen(optarg));
   1830 			break;
   1831 		case 'f':
   1832 			defaultfield = 1;
   1833 			memsave(&u.u_inactive, optarg, strlen(optarg));
   1834 			break;
   1835 		case 'g':
   1836 			defaultfield = 1;
   1837 			memsave(&u.u_primgrp, optarg, strlen(optarg));
   1838 			break;
   1839 		case 'k':
   1840 			defaultfield = 1;
   1841 			memsave(&u.u_skeldir, optarg, strlen(optarg));
   1842 			break;
   1843 #ifdef EXTENSIONS
   1844 		case 'L':
   1845 			defaultfield = 1;
   1846 			memsave(&u.u_class, optarg, strlen(optarg));
   1847 			break;
   1848 #endif
   1849 		case 'm':
   1850 			u.u_flags |= F_MKDIR;
   1851 			break;
   1852 		case 'o':
   1853 			u.u_flags |= F_DUPUID;
   1854 			break;
   1855 #ifdef EXTENSIONS
   1856 		case 'p':
   1857 			memsave(&u.u_password, optarg, strlen(optarg));
   1858 			break;
   1859 #endif
   1860 #ifdef EXTENSIONS
   1861 		case 'r':
   1862 			defaultfield = 1;
   1863 			(void)save_range(&u, optarg);
   1864 			break;
   1865 #endif
   1866 		case 's':
   1867 			defaultfield = 1;
   1868 			memsave(&u.u_shell, optarg, strlen(optarg));
   1869 			break;
   1870 		case 'u':
   1871 			u.u_uid = check_numeric(optarg, "uid");
   1872 			break;
   1873 #ifdef EXTENSIONS
   1874 		case 'v':
   1875 			verbose = 1;
   1876 			break;
   1877 #endif
   1878 		default:
   1879 			usermgmt_usage("useradd");
   1880 			/* NOTREACHED */
   1881 		}
   1882 	}
   1883 	if (bigD) {
   1884 		if (defaultfield) {
   1885 			checkeuid();
   1886 			return setdefaults(&u) ? EXIT_SUCCESS : EXIT_FAILURE;
   1887 		}
   1888 		(void)printf("group\t\t%s\n", u.u_primgrp);
   1889 		(void)printf("base_dir\t%s\n", u.u_basedir);
   1890 		(void)printf("skel_dir\t%s\n", u.u_skeldir);
   1891 		(void)printf("shell\t\t%s\n", u.u_shell);
   1892 #ifdef EXTENSIONS
   1893 		(void)printf("class\t\t%s\n", u.u_class);
   1894 #endif
   1895 		(void)printf("inactive\t%s\n", (u.u_inactive == NULL) ?
   1896 		    UNSET_INACTIVE : u.u_inactive);
   1897 		(void)printf("expire\t\t%s\n", (u.u_expire == NULL) ?
   1898 		    UNSET_EXPIRY : u.u_expire);
   1899 #ifdef EXTENSIONS
   1900 		for (i = 0 ; i < u.u_rc ; i++) {
   1901 			(void)printf("range\t\t%d..%d\n",
   1902 			    u.u_rv[i].r_from, u.u_rv[i].r_to);
   1903 		}
   1904 #endif
   1905 		return EXIT_SUCCESS;
   1906 	}
   1907 	argc -= optind;
   1908 	argv += optind;
   1909 	if (argc != 1) {
   1910 		usermgmt_usage("useradd");
   1911 	}
   1912 	checkeuid();
   1913 	openlog("useradd", LOG_PID, LOG_USER);
   1914 	return adduser(*argv, &u) ? EXIT_SUCCESS : EXIT_FAILURE;
   1915 }
   1916 
   1917 #ifdef EXTENSIONS
   1918 #define MOD_OPT_EXTENSIONS	"p:vL:S"
   1919 #else
   1920 #define MOD_OPT_EXTENSIONS
   1921 #endif
   1922 
   1923 int
   1924 usermod(int argc, char **argv)
   1925 {
   1926 	user_t	u;
   1927 	char	newuser[MaxUserNameLen + 1];
   1928 	int	c, have_new_user;
   1929 
   1930 	(void)memset(&u, 0, sizeof(u));
   1931 	(void)memset(newuser, 0, sizeof(newuser));
   1932 	read_defaults(&u);
   1933 	have_new_user = 0;
   1934 	u.u_locked = -1;
   1935 	while ((c = getopt(argc, argv, "C:FG:c:d:e:f:g:l:mos:u:"
   1936 	    MOD_OPT_EXTENSIONS)) != -1) {
   1937 		switch(c) {
   1938 		case 'G':
   1939 			while ((u.u_groupv[u.u_groupc] =
   1940 			    strsep(&optarg, ",")) != NULL &&
   1941 			    u.u_groupc < NGROUPS_MAX) {
   1942 				if (u.u_groupv[u.u_groupc][0] != 0) {
   1943 					u.u_groupc++;
   1944 				}
   1945 			}
   1946 			if (optarg != NULL) {
   1947 				warnx("Truncated list of secondary groups "
   1948 				    "to %d entries", NGROUPS_MAX);
   1949 			}
   1950 			u.u_flags |= F_SECGROUP;
   1951 			break;
   1952 #ifdef EXTENSIONS
   1953 		case 'S':
   1954 			u.u_allow_samba = 1;
   1955 			break;
   1956 #endif
   1957 		case 'c':
   1958 			memsave(&u.u_comment, optarg, strlen(optarg));
   1959 			u.u_flags |= F_COMMENT;
   1960 			break;
   1961 		case 'C':
   1962 			if (strcasecmp(optarg, "yes") == 0) {
   1963 				u.u_locked = LOCK;
   1964 			} else if (strcasecmp(optarg, "no") == 0) {
   1965 				u.u_locked = UNLOCK;
   1966 			} else {
   1967 				/* No idea. */
   1968 				errx(EXIT_FAILURE,
   1969 					"Please type 'yes' or 'no'");
   1970 			}
   1971 			break;
   1972 		case 'F':
   1973 			memsave(&u.u_inactive, "-1", strlen("-1"));
   1974 			u.u_flags |= F_INACTIVE;
   1975 			break;
   1976 		case 'd':
   1977 			memsave(&u.u_home, optarg, strlen(optarg));
   1978 			u.u_flags |= F_HOMEDIR;
   1979 			break;
   1980 		case 'e':
   1981 			memsave(&u.u_expire, optarg, strlen(optarg));
   1982 			u.u_flags |= F_EXPIRE;
   1983 			break;
   1984 		case 'f':
   1985 			memsave(&u.u_inactive, optarg, strlen(optarg));
   1986 			u.u_flags |= F_INACTIVE;
   1987 			break;
   1988 		case 'g':
   1989 			memsave(&u.u_primgrp, optarg, strlen(optarg));
   1990 			u.u_flags |= F_GROUP;
   1991 			break;
   1992 		case 'l':
   1993 			(void)strlcpy(newuser, optarg, sizeof(newuser));
   1994 			have_new_user = 1;
   1995 			u.u_flags |= F_USERNAME;
   1996 			break;
   1997 #ifdef EXTENSIONS
   1998 		case 'L':
   1999 			memsave(&u.u_class, optarg, strlen(optarg));
   2000 			u.u_flags |= F_CLASS;
   2001 			break;
   2002 #endif
   2003 		case 'm':
   2004 			u.u_flags |= F_MKDIR;
   2005 			break;
   2006 		case 'o':
   2007 			u.u_flags |= F_DUPUID;
   2008 			break;
   2009 #ifdef EXTENSIONS
   2010 		case 'p':
   2011 			memsave(&u.u_password, optarg, strlen(optarg));
   2012 			u.u_flags |= F_PASSWORD;
   2013 			break;
   2014 #endif
   2015 		case 's':
   2016 			memsave(&u.u_shell, optarg, strlen(optarg));
   2017 			u.u_flags |= F_SHELL;
   2018 			break;
   2019 		case 'u':
   2020 			u.u_uid = check_numeric(optarg, "uid");
   2021 			u.u_flags |= F_UID;
   2022 			break;
   2023 #ifdef EXTENSIONS
   2024 		case 'v':
   2025 			verbose = 1;
   2026 			break;
   2027 #endif
   2028 		default:
   2029 			usermgmt_usage("usermod");
   2030 			/* NOTREACHED */
   2031 		}
   2032 	}
   2033 	if ((u.u_flags & F_MKDIR) && !(u.u_flags & F_HOMEDIR) &&
   2034 	    !(u.u_flags & F_USERNAME)) {
   2035 		warnx("Option 'm' useless without 'd' or 'l' -- ignored");
   2036 		u.u_flags &= ~F_MKDIR;
   2037 	}
   2038 	argc -= optind;
   2039 	argv += optind;
   2040 	if (argc != 1) {
   2041 		usermgmt_usage("usermod");
   2042 	}
   2043 	checkeuid();
   2044 	openlog("usermod", LOG_PID, LOG_USER);
   2045 	return moduser(*argv, (have_new_user) ? newuser : *argv, &u,
   2046 	    u.u_allow_samba) ? EXIT_SUCCESS : EXIT_FAILURE;
   2047 }
   2048 
   2049 #ifdef EXTENSIONS
   2050 #define DEL_OPT_EXTENSIONS	"Dp:vS"
   2051 #else
   2052 #define DEL_OPT_EXTENSIONS
   2053 #endif
   2054 
   2055 int
   2056 userdel(int argc, char **argv)
   2057 {
   2058 	struct passwd	*pwp;
   2059 	user_t		u;
   2060 	char		password[PasswordLength + 1];
   2061 	int		defaultfield;
   2062 	int		rmhome;
   2063 	int		bigD;
   2064 	int		c;
   2065 
   2066 	(void)memset(&u, 0, sizeof(u));
   2067 	read_defaults(&u);
   2068 	defaultfield = bigD = rmhome = 0;
   2069 	while ((c = getopt(argc, argv, "r" DEL_OPT_EXTENSIONS)) != -1) {
   2070 		switch(c) {
   2071 #ifdef EXTENSIONS
   2072 		case 'D':
   2073 			bigD = 1;
   2074 			break;
   2075 #endif
   2076 #ifdef EXTENSIONS
   2077 		case 'S':
   2078 			u.u_allow_samba = 1;
   2079 			break;
   2080 #endif
   2081 #ifdef EXTENSIONS
   2082 		case 'p':
   2083 			defaultfield = 1;
   2084 			u.u_preserve = (strcmp(optarg, "true") == 0) ? 1 :
   2085 					(strcmp(optarg, "yes") == 0) ? 1 :
   2086 					 atoi(optarg);
   2087 			break;
   2088 #endif
   2089 		case 'r':
   2090 			rmhome = 1;
   2091 			break;
   2092 #ifdef EXTENSIONS
   2093 		case 'v':
   2094 			verbose = 1;
   2095 			break;
   2096 #endif
   2097 		default:
   2098 			usermgmt_usage("userdel");
   2099 			/* NOTREACHED */
   2100 		}
   2101 	}
   2102 #ifdef EXTENSIONS
   2103 	if (bigD) {
   2104 		if (defaultfield) {
   2105 			checkeuid();
   2106 			return setdefaults(&u) ? EXIT_SUCCESS : EXIT_FAILURE;
   2107 		}
   2108 		(void)printf("preserve\t%s\n", (u.u_preserve) ? "true" :
   2109 		    "false");
   2110 		return EXIT_SUCCESS;
   2111 	}
   2112 #endif
   2113 	argc -= optind;
   2114 	argv += optind;
   2115 	if (argc != 1) {
   2116 		usermgmt_usage("userdel");
   2117 	}
   2118 	checkeuid();
   2119 	if ((pwp = getpwnam(*argv)) == NULL) {
   2120 		warnx("No such user `%s'", *argv);
   2121 		return EXIT_FAILURE;
   2122 	}
   2123 	if (rmhome) {
   2124 		(void)removehomedir(pwp->pw_name, pwp->pw_uid, pwp->pw_dir);
   2125 	}
   2126 	if (u.u_preserve) {
   2127 		u.u_flags |= F_SHELL;
   2128 		memsave(&u.u_shell, NOLOGIN, strlen(NOLOGIN));
   2129 		(void)memset(password, '*', DES_Len);
   2130 		password[DES_Len] = 0;
   2131 		memsave(&u.u_password, password, strlen(password));
   2132 		u.u_flags |= F_PASSWORD;
   2133 		openlog("userdel", LOG_PID, LOG_USER);
   2134 		return moduser(*argv, *argv, &u, u.u_allow_samba) ?
   2135 		    EXIT_SUCCESS : EXIT_FAILURE;
   2136 	}
   2137 	if (!rm_user_from_groups(*argv)) {
   2138 		return 0;
   2139 	}
   2140 	openlog("userdel", LOG_PID, LOG_USER);
   2141 	return moduser(*argv, *argv, NULL, u.u_allow_samba) ?
   2142 	    EXIT_SUCCESS : EXIT_FAILURE;
   2143 }
   2144 
   2145 #ifdef EXTENSIONS
   2146 #define GROUP_ADD_OPT_EXTENSIONS	"r:v"
   2147 #else
   2148 #define GROUP_ADD_OPT_EXTENSIONS
   2149 #endif
   2150 
   2151 /* add a group */
   2152 int
   2153 groupadd(int argc, char **argv)
   2154 {
   2155 	int	dupgid;
   2156 	int	gid;
   2157 	int	c;
   2158 	int	lowgid;
   2159 	int	highgid;
   2160 
   2161 	gid = -1;
   2162 	dupgid = 0;
   2163 	lowgid = LowGid;
   2164 	highgid = HighGid;
   2165 	while ((c = getopt(argc, argv, "g:o" GROUP_ADD_OPT_EXTENSIONS)) != -1) {
   2166 		switch(c) {
   2167 		case 'g':
   2168 			gid = check_numeric(optarg, "gid");
   2169 			break;
   2170 		case 'o':
   2171 			dupgid = 1;
   2172 			break;
   2173 #ifdef EXTENSIONS
   2174 		case 'r':
   2175 			if (sscanf(optarg, "%d..%d", &lowgid, &highgid) != 2) {
   2176 				errx(EXIT_FAILURE, "Bad range `%s'", optarg);
   2177 			}
   2178 			break;
   2179 		case 'v':
   2180 			verbose = 1;
   2181 			break;
   2182 #endif
   2183 		default:
   2184 			usermgmt_usage("groupadd");
   2185 			/* NOTREACHED */
   2186 		}
   2187 	}
   2188 	argc -= optind;
   2189 	argv += optind;
   2190 	if (argc != 1) {
   2191 		usermgmt_usage("groupadd");
   2192 	}
   2193 	checkeuid();
   2194 	if (gid < 0 && !getnextgid(&gid, lowgid, highgid)) {
   2195 		err(EXIT_FAILURE, "Can't add group: can't get next gid");
   2196 	}
   2197 	if (!dupgid && getgrgid((gid_t) gid) != NULL) {
   2198 		errx(EXIT_FAILURE, "Can't add group: gid %d is a duplicate",
   2199 		    gid);
   2200 	}
   2201 	if (!valid_group(*argv)) {
   2202 		warnx("Invalid group name `%s'", *argv);
   2203 	}
   2204 	openlog("groupadd", LOG_PID, LOG_USER);
   2205 	if (!creategid(*argv, gid, "")) {
   2206 		errx(EXIT_FAILURE, "Can't add group: problems with %s file",
   2207 		    _PATH_GROUP);
   2208 	}
   2209 	return EXIT_SUCCESS;
   2210 }
   2211 
   2212 #ifdef EXTENSIONS
   2213 #define GROUP_DEL_OPT_EXTENSIONS	"v"
   2214 #else
   2215 #define GROUP_DEL_OPT_EXTENSIONS
   2216 #endif
   2217 
   2218 /* remove a group */
   2219 int
   2220 groupdel(int argc, char **argv)
   2221 {
   2222 	int	c;
   2223 
   2224 	while ((c = getopt(argc, argv, "" GROUP_DEL_OPT_EXTENSIONS)) != -1) {
   2225 		switch(c) {
   2226 #ifdef EXTENSIONS
   2227 		case 'v':
   2228 			verbose = 1;
   2229 			break;
   2230 #endif
   2231 		default:
   2232 			usermgmt_usage("groupdel");
   2233 			/* NOTREACHED */
   2234 		}
   2235 	}
   2236 	argc -= optind;
   2237 	argv += optind;
   2238 	if (argc != 1) {
   2239 		usermgmt_usage("groupdel");
   2240 	}
   2241 	if (getgrnam(*argv) == NULL) {
   2242 		errx(EXIT_FAILURE, "No such group `%s'", *argv);
   2243 	}
   2244 	checkeuid();
   2245 	openlog("groupdel", LOG_PID, LOG_USER);
   2246 	if (!modify_gid(*argv, NULL)) {
   2247 		errx(EXIT_FAILURE, "Can't change `%s' file", _PATH_GROUP);
   2248 	}
   2249 	return EXIT_SUCCESS;
   2250 }
   2251 
   2252 #ifdef EXTENSIONS
   2253 #define GROUP_MOD_OPT_EXTENSIONS	"v"
   2254 #else
   2255 #define GROUP_MOD_OPT_EXTENSIONS
   2256 #endif
   2257 
   2258 /* modify a group */
   2259 int
   2260 groupmod(int argc, char **argv)
   2261 {
   2262 	struct group	*grp;
   2263 	char		buf[MaxEntryLen];
   2264 	char		*newname;
   2265 	char		**cpp;
   2266 	int		dupgid;
   2267 	int		gid;
   2268 	int		cc;
   2269 	int		c;
   2270 
   2271 	gid = -1;
   2272 	dupgid = 0;
   2273 	newname = NULL;
   2274 	while ((c = getopt(argc, argv, "g:on:" GROUP_MOD_OPT_EXTENSIONS)) != -1) {
   2275 		switch(c) {
   2276 		case 'g':
   2277 			gid = check_numeric(optarg, "gid");
   2278 			break;
   2279 		case 'o':
   2280 			dupgid = 1;
   2281 			break;
   2282 		case 'n':
   2283 			memsave(&newname, optarg, strlen(optarg));
   2284 			break;
   2285 #ifdef EXTENSIONS
   2286 		case 'v':
   2287 			verbose = 1;
   2288 			break;
   2289 #endif
   2290 		default:
   2291 			usermgmt_usage("groupmod");
   2292 			/* NOTREACHED */
   2293 		}
   2294 	}
   2295 	argc -= optind;
   2296 	argv += optind;
   2297 	if (argc != 1) {
   2298 		usermgmt_usage("groupmod");
   2299 	}
   2300 	checkeuid();
   2301 	if (gid < 0 && newname == NULL) {
   2302 		errx(EXIT_FAILURE, "Nothing to change");
   2303 	}
   2304 	if (dupgid && gid < 0) {
   2305 		errx(EXIT_FAILURE, "Duplicate which gid?");
   2306 	}
   2307 	if ((grp = find_group_info(*argv)) == NULL) {
   2308 		errx(EXIT_FAILURE, "Can't find group `%s' to modify", *argv);
   2309 	}
   2310 	if (!is_local(*argv, _PATH_GROUP)) {
   2311 		errx(EXIT_FAILURE, "Group `%s' must be a local group", *argv);
   2312 	}
   2313 	if (newname != NULL && !valid_group(newname)) {
   2314 		warnx("Invalid group name `%s'", newname);
   2315 	}
   2316 	cc = snprintf(buf, sizeof(buf), "%s:%s:%d:",
   2317 			(newname) ? newname : grp->gr_name,
   2318 			grp->gr_passwd,
   2319 			(gid < 0) ? grp->gr_gid : gid);
   2320 	for (cpp = grp->gr_mem ; *cpp && cc < sizeof(buf) ; cpp++) {
   2321 		cc += snprintf(&buf[cc], sizeof(buf) - cc, "%s%s", *cpp,
   2322 			(cpp[1] == NULL) ? "" : ",");
   2323 	}
   2324 	cc += snprintf(&buf[cc], sizeof(buf) - cc, "\n");
   2325 	openlog("groupmod", LOG_PID, LOG_USER);
   2326 	if (!modify_gid(*argv, buf)) {
   2327 		errx(EXIT_FAILURE, "Can't change %s file", _PATH_GROUP);
   2328 	}
   2329 	return EXIT_SUCCESS;
   2330 }
   2331 
   2332 #ifdef EXTENSIONS
   2333 /* display user information */
   2334 int
   2335 userinfo(int argc, char **argv)
   2336 {
   2337 	struct passwd	*pwp;
   2338 	struct group	*grp;
   2339 	char		buf[MaxEntryLen];
   2340 	char		**cpp;
   2341 	int		exists;
   2342 	int		cc;
   2343 	int		i;
   2344 
   2345 	exists = 0;
   2346 	while ((i = getopt(argc, argv, "ev")) != -1) {
   2347 		switch(i) {
   2348 		case 'e':
   2349 			exists = 1;
   2350 			break;
   2351 		case 'v':
   2352 			verbose = 1;
   2353 			break;
   2354 		default:
   2355 			usermgmt_usage("userinfo");
   2356 			/* NOTREACHED */
   2357 		}
   2358 	}
   2359 	argc -= optind;
   2360 	argv += optind;
   2361 	if (argc != 1) {
   2362 		usermgmt_usage("userinfo");
   2363 	}
   2364 	pwp = find_user_info(*argv);
   2365 	if (exists) {
   2366 		exit((pwp) ? EXIT_SUCCESS : EXIT_FAILURE);
   2367 	}
   2368 	if (pwp == NULL) {
   2369 		errx(EXIT_FAILURE, "Can't find user `%s'", *argv);
   2370 	}
   2371 	(void)printf("login\t%s\n", pwp->pw_name);
   2372 	(void)printf("passwd\t%s\n", pwp->pw_passwd);
   2373 	(void)printf("uid\t%d\n", pwp->pw_uid);
   2374 	for (cc = 0 ; (grp = getgrent()) != NULL ; ) {
   2375 		for (cpp = grp->gr_mem ; *cpp ; cpp++) {
   2376 			if (strcmp(*cpp, *argv) == 0 &&
   2377 			    grp->gr_gid != pwp->pw_gid) {
   2378 				cc += snprintf(&buf[cc], sizeof(buf) - cc,
   2379 				    "%s ", grp->gr_name);
   2380 			}
   2381 		}
   2382 	}
   2383 	if ((grp = getgrgid(pwp->pw_gid)) == NULL) {
   2384 		(void)printf("groups\t%d %s\n", pwp->pw_gid, buf);
   2385 	} else {
   2386 		(void)printf("groups\t%s %s\n", grp->gr_name, buf);
   2387 	}
   2388 	(void)printf("change\t%s", pwp->pw_change > 0 ?
   2389 	    ctime(&pwp->pw_change) : pwp->pw_change == -1 ?
   2390 	    "NEXT LOGIN\n" : "NEVER\n");
   2391 	(void)printf("class\t%s\n", pwp->pw_class);
   2392 	(void)printf("gecos\t%s\n", pwp->pw_gecos);
   2393 	(void)printf("dir\t%s\n", pwp->pw_dir);
   2394 	(void)printf("shell\t%s\n", pwp->pw_shell);
   2395 	(void)printf("expire\t%s", pwp->pw_expire ?
   2396 	    ctime(&pwp->pw_expire) : "NEVER\n");
   2397 	return EXIT_SUCCESS;
   2398 }
   2399 #endif
   2400 
   2401 #ifdef EXTENSIONS
   2402 /* display user information */
   2403 int
   2404 groupinfo(int argc, char **argv)
   2405 {
   2406 	struct group	*grp;
   2407 	char		**cpp;
   2408 	int		exists;
   2409 	int		i;
   2410 
   2411 	exists = 0;
   2412 	while ((i = getopt(argc, argv, "ev")) != -1) {
   2413 		switch(i) {
   2414 		case 'e':
   2415 			exists = 1;
   2416 			break;
   2417 		case 'v':
   2418 			verbose = 1;
   2419 			break;
   2420 		default:
   2421 			usermgmt_usage("groupinfo");
   2422 			/* NOTREACHED */
   2423 		}
   2424 	}
   2425 	argc -= optind;
   2426 	argv += optind;
   2427 	if (argc != 1) {
   2428 		usermgmt_usage("groupinfo");
   2429 	}
   2430 	grp = find_group_info(*argv);
   2431 	if (exists) {
   2432 		exit((grp) ? EXIT_SUCCESS : EXIT_FAILURE);
   2433 	}
   2434 	if (grp == NULL) {
   2435 		errx(EXIT_FAILURE, "Can't find group `%s'", *argv);
   2436 	}
   2437 	(void)printf("name\t%s\n", grp->gr_name);
   2438 	(void)printf("passwd\t%s\n", grp->gr_passwd);
   2439 	(void)printf("gid\t%d\n", grp->gr_gid);
   2440 	(void)printf("members\t");
   2441 	for (cpp = grp->gr_mem ; *cpp ; cpp++) {
   2442 		(void)printf("%s", *cpp);
   2443 		if (*(cpp + 1)) {
   2444 			(void) printf(", ");
   2445 		}
   2446 	}
   2447 	(void)fputc('\n', stdout);
   2448 	return EXIT_SUCCESS;
   2449 }
   2450 #endif
   2451