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