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