Home | History | Annotate | Line # | Download | only in user
user.c revision 1.23
      1 /* $NetBSD: user.c,v 1.23 2000/09/29 10:37:26 agc Exp $ */
      2 
      3 /*
      4  * Copyright (c) 1999 Alistair G. Crooks.  All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  * 3. All advertising materials mentioning features or use of this software
     15  *    must display the following acknowledgement:
     16  *	This product includes software developed by Alistair G. Crooks.
     17  * 4. The name of the author may not be used to endorse or promote
     18  *    products derived from this software without specific prior written
     19  *    permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     22  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     23  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
     25  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
     27  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     29  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
     30  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     31  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32  */
     33 #include <sys/cdefs.h>
     34 
     35 #ifndef lint
     36 __COPYRIGHT(
     37 	"@(#) Copyright (c) 1999 \
     38 	        The NetBSD Foundation, Inc.  All rights reserved.");
     39 __RCSID("$NetBSD: user.c,v 1.23 2000/09/29 10:37:26 agc Exp $");
     40 #endif
     41 
     42 #include <sys/types.h>
     43 #include <sys/param.h>
     44 #include <sys/stat.h>
     45 
     46 #include <ctype.h>
     47 #include <dirent.h>
     48 #include <err.h>
     49 #include <fcntl.h>
     50 #include <grp.h>
     51 #include <pwd.h>
     52 #include <stdarg.h>
     53 #include <stdio.h>
     54 #include <stdlib.h>
     55 #include <string.h>
     56 #include <time.h>
     57 #include <unistd.h>
     58 #include <util.h>
     59 
     60 #include "defs.h"
     61 #include "usermgmt.h"
     62 
     63 /* this struct describes a uid range */
     64 typedef struct range_t {
     65 	int	r_from;		/* low uid */
     66 	int	r_to;		/* high uid */
     67 } range_t;
     68 
     69 /* this struct encapsulates the user information */
     70 typedef struct user_t {
     71 	int		u_uid;			/* uid of user */
     72 	char		*u_password;		/* encrypted password */
     73 	char		*u_comment;		/* comment field */
     74 	int		u_homeset;		/* home dir has been set */
     75 	char		*u_home;		/* home directory */
     76 	char		*u_primgrp;		/* primary group */
     77 	int		u_groupc;		/* # of secondary groups */
     78 	char		*u_groupv[NGROUPS_MAX];	/* secondary groups */
     79 	char		*u_shell;		/* user's shell */
     80 	char		*u_basedir;		/* base directory for home */
     81 	char		*u_expire;		/* when password will expire */
     82 	int		u_inactive;		/* inactive */
     83 	int		u_mkdir;		/* make the home directory */
     84 	int		u_dupuid;		/* duplicate uids are allowed */
     85 	char		*u_skeldir;		/* directory for startup files */
     86 	unsigned	u_rsize;		/* size of range array */
     87 	unsigned	u_rc;			/* # of ranges */
     88 	range_t		*u_rv;			/* the ranges */
     89 	unsigned	u_defrc;		/* # of ranges in defaults */
     90 	int		u_preserve;		/* preserve uids on deletion */
     91 } user_t;
     92 
     93 #define CONFFILE	"/etc/usermgmt.conf"
     94 
     95 #ifndef DEF_GROUP
     96 #define DEF_GROUP	"users"
     97 #endif
     98 
     99 #ifndef DEF_BASEDIR
    100 #define DEF_BASEDIR	"/home"
    101 #endif
    102 
    103 #ifndef DEF_SKELDIR
    104 #define DEF_SKELDIR	"/etc/skel"
    105 #endif
    106 
    107 #ifndef DEF_SHELL
    108 #define DEF_SHELL	"/bin/csh"
    109 #endif
    110 
    111 #ifndef DEF_COMMENT
    112 #define DEF_COMMENT	""
    113 #endif
    114 
    115 #ifndef DEF_LOWUID
    116 #define DEF_LOWUID	1000
    117 #endif
    118 
    119 #ifndef DEF_HIGHUID
    120 #define DEF_HIGHUID	60000
    121 #endif
    122 
    123 #ifndef DEF_INACTIVE
    124 #define DEF_INACTIVE	0
    125 #endif
    126 
    127 #ifndef DEF_EXPIRE
    128 #define DEF_EXPIRE	(char *) NULL
    129 #endif
    130 
    131 #ifndef WAITSECS
    132 #define WAITSECS	10
    133 #endif
    134 
    135 #ifndef NOBODY_UID
    136 #define NOBODY_UID	32767
    137 #endif
    138 
    139 /* some useful constants */
    140 enum {
    141 	MaxShellNameLen = 256,
    142 	MaxFileNameLen = MAXPATHLEN,
    143 	MaxUserNameLen = 32,
    144 	MaxFieldNameLen = 32,
    145 	MaxCommandLen = 2048,
    146 	MaxEntryLen = 2048,
    147 	PasswordLength = 13,
    148 
    149 	LowGid = DEF_LOWUID,
    150 	HighGid = DEF_HIGHUID
    151 };
    152 
    153 /* Full paths of programs used here */
    154 #define CHOWN		"/usr/sbin/chown"
    155 #define MKDIR		"/bin/mkdir"
    156 #define MV		"/bin/mv"
    157 #define NOLOGIN		"/sbin/nologin"
    158 #define PAX		"/bin/pax"
    159 #define RM		"/bin/rm"
    160 
    161 #define UNSET_EXPIRY	"Null (unset)"
    162 
    163 static int	verbose;
    164 
    165 /* if *cpp is non-null, free it, then assign `n' chars of `s' to it */
    166 static void
    167 memsave(char **cpp, char *s, size_t n)
    168 {
    169 	if (*cpp != (char *) NULL) {
    170 		FREE(*cpp);
    171 	}
    172 	NEWARRAY(char, *cpp, n + 1, exit(1));
    173 	(void) memcpy(*cpp, s, n);
    174 	(*cpp)[n] = '\0';
    175 }
    176 
    177 /* a replacement for system(3) */
    178 static int
    179 asystem(char *fmt, ...)
    180 {
    181 	va_list	vp;
    182 	char	buf[MaxCommandLen];
    183 	int	ret;
    184 
    185 	va_start(vp, fmt);
    186 	(void) vsnprintf(buf, sizeof(buf), fmt, vp);
    187 	va_end(vp);
    188 	if (verbose) {
    189 		(void) printf("Command: %s\n", buf);
    190 	}
    191 	if ((ret = system(buf)) != 0) {
    192 		warnx("[Warning] can't system `%s'", buf);
    193 	}
    194 	return ret;
    195 }
    196 
    197 #define NetBSD_1_4_K	104110000
    198 
    199 #if defined(__NetBSD_Version__) && (__NetBSD_Version__ < NetBSD_1_4_K)
    200 /* bounds checking strncpy */
    201 static int
    202 strlcpy(char *to, char *from, size_t tosize)
    203 {
    204 	size_t	n;
    205 	int	fromsize;
    206 
    207 	fromsize = strlen(from);
    208 	n = MIN(tosize - 1, fromsize);
    209 	(void) memcpy(to, from, n);
    210 	to[n] = '\0';
    211 	return fromsize;
    212 }
    213 #endif
    214 
    215 /* return 1 if all of `s' is numeric */
    216 static int
    217 is_number(char *s)
    218 {
    219 	for ( ; *s ; s++) {
    220 		if (!isdigit(*s)) {
    221 			return 0;
    222 		}
    223 	}
    224 	return 1;
    225 }
    226 
    227 /*
    228  * check that the effective uid is 0 - called from funcs which will
    229  * modify data and config files.
    230  */
    231 static void
    232 checkeuid(void)
    233 {
    234 	if (geteuid() != 0) {
    235 		errx(EXIT_FAILURE, "Program must be run as root");
    236 	}
    237 }
    238 
    239 /* copy any dot files into the user's home directory */
    240 static int
    241 copydotfiles(char *skeldir, int uid, int gid, char *dir)
    242 {
    243 	struct dirent	*dp;
    244 	DIR		*dirp;
    245 	int		n;
    246 
    247 	if ((dirp = opendir(skeldir)) == (DIR *) NULL) {
    248 		warn("can't open source . files dir `%s'", skeldir);
    249 		return 0;
    250 	}
    251 	for (n = 0; (dp = readdir(dirp)) != (struct dirent *) NULL && n == 0 ; ) {
    252 		if (strcmp(dp->d_name, ".") == 0 ||
    253 		    strcmp(dp->d_name, "..") == 0) {
    254 			continue;
    255 		}
    256 		n = 1;
    257 	}
    258 	(void) closedir(dirp);
    259 	if (n == 0) {
    260 		warnx("No \"dot\" initialisation files found");
    261 	} else {
    262 		(void) asystem("cd %s; %s -rw -pe %s . %s",
    263 				skeldir, PAX, (verbose) ? "-v" : "", dir);
    264 	}
    265 	(void) asystem("%s -R -h %d:%d %s", CHOWN, uid, gid, dir);
    266 	return n;
    267 }
    268 
    269 /* create a group entry with gid `gid' */
    270 static int
    271 creategid(char *group, int gid, char *name)
    272 {
    273 	struct stat	st;
    274 	FILE		*from;
    275 	FILE		*to;
    276 	char		buf[MaxEntryLen];
    277 	char		f[MaxFileNameLen];
    278 	int		fd;
    279 	int		cc;
    280 
    281 	if (getgrnam(group) != (struct group *) NULL) {
    282 		warnx("group `%s' already exists", group);
    283 		return 0;
    284 	}
    285 	if ((from = fopen(_PATH_GROUP, "r")) == (FILE *) NULL) {
    286 		warn("can't create gid for %s: can't open %s", name, _PATH_GROUP);
    287 		return 0;
    288 	}
    289 	if (flock(fileno(from), LOCK_EX | LOCK_NB) < 0) {
    290 		warn("can't lock `%s'", _PATH_GROUP);
    291 	}
    292 	(void) fstat(fileno(from), &st);
    293 	(void) snprintf(f, sizeof(f), "%s.XXXXXX", _PATH_GROUP);
    294 	if ((fd = mkstemp(f)) < 0) {
    295 		(void) fclose(from);
    296 		warn("can't create gid: mkstemp failed");
    297 		return 0;
    298 	}
    299 	if ((to = fdopen(fd, "w")) == (FILE *) NULL) {
    300 		(void) fclose(from);
    301 		(void) close(fd);
    302 		(void) unlink(f);
    303 		warn("can't create gid: fdopen `%s' failed", f);
    304 		return 0;
    305 	}
    306 	while ((cc = fread(buf, sizeof(char), sizeof(buf), from)) > 0) {
    307 		if (fwrite(buf, sizeof(char), (unsigned) cc, to) != cc) {
    308 			(void) fclose(from);
    309 			(void) close(fd);
    310 			(void) unlink(f);
    311 			warn("can't create gid: short write to `%s'", f);
    312 			return 0;
    313 		}
    314 	}
    315 	(void) fprintf(to, "%s:*:%d:%s\n", group, gid, name);
    316 	(void) fclose(from);
    317 	(void) fclose(to);
    318 	if (rename(f, _PATH_GROUP) < 0) {
    319 		warn("can't create gid: can't rename `%s' to `%s'", f, _PATH_GROUP);
    320 		return 0;
    321 	}
    322 	(void) chmod(_PATH_GROUP, st.st_mode & 07777);
    323 	return 1;
    324 }
    325 
    326 /* modify the group entry with name `group' to be newent */
    327 static int
    328 modify_gid(char *group, char *newent)
    329 {
    330 	struct stat	st;
    331 	FILE		*from;
    332 	FILE		*to;
    333 	char		buf[MaxEntryLen];
    334 	char		f[MaxFileNameLen];
    335 	char		*colon;
    336 	int		groupc;
    337 	int		entc;
    338 	int		fd;
    339 	int		cc;
    340 
    341 	if ((from = fopen(_PATH_GROUP, "r")) == (FILE *) NULL) {
    342 		warn("can't create gid for %s: can't open %s", group, _PATH_GROUP);
    343 		return 0;
    344 	}
    345 	if (flock(fileno(from), LOCK_EX | LOCK_NB) < 0) {
    346 		warn("can't lock `%s'", _PATH_GROUP);
    347 	}
    348 	(void) fstat(fileno(from), &st);
    349 	(void) snprintf(f, sizeof(f), "%s.XXXXXX", _PATH_GROUP);
    350 	if ((fd = mkstemp(f)) < 0) {
    351 		(void) fclose(from);
    352 		warn("can't create gid: mkstemp failed");
    353 		return 0;
    354 	}
    355 	if ((to = fdopen(fd, "w")) == (FILE *) NULL) {
    356 		(void) fclose(from);
    357 		(void) close(fd);
    358 		(void) unlink(f);
    359 		warn("can't create gid: fdopen `%s' failed", f);
    360 		return 0;
    361 	}
    362 	groupc = strlen(group);
    363 	while (fgets(buf, sizeof(buf), from) != NULL) {
    364 		cc = strlen(buf);
    365 		if ((colon = strchr(buf, ':')) == NULL) {
    366 			warn("badly formed entry `%s'", buf);
    367 			continue;
    368 		}
    369 		entc = (int)(colon - buf);
    370 		if (entc == groupc && strncmp(group, buf, (unsigned) entc) == 0) {
    371 			if (newent == NULL) {
    372 				continue;
    373 			} else {
    374 				cc = strlen(newent);
    375 				(void) strlcpy(buf, newent, sizeof(buf));
    376 			}
    377 		}
    378 		if (fwrite(buf, sizeof(char), (unsigned) cc, to) != cc) {
    379 			(void) fclose(from);
    380 			(void) close(fd);
    381 			(void) unlink(f);
    382 			warn("can't create gid: short write to `%s'", f);
    383 			return 0;
    384 		}
    385 	}
    386 	(void) fclose(from);
    387 	(void) fclose(to);
    388 	if (rename(f, _PATH_GROUP) < 0) {
    389 		warn("can't create gid: can't rename `%s' to `%s'", f, _PATH_GROUP);
    390 		return 0;
    391 	}
    392 	(void) chmod(_PATH_GROUP, st.st_mode & 07777);
    393 	return 1;
    394 }
    395 
    396 /* modify the group entries for all `groups', by adding `user' */
    397 static int
    398 append_group(char *user, int ngroups, char **groups)
    399 {
    400 	struct group	*grp;
    401 	struct stat	st;
    402 	FILE		*from;
    403 	FILE		*to;
    404 	char		buf[MaxEntryLen];
    405 	char		f[MaxFileNameLen];
    406 	char		*colon;
    407 	int		groupc;
    408 	int		entc;
    409 	int		fd;
    410 	int		nc;
    411 	int		cc;
    412 	int		i;
    413 	int		j;
    414 
    415 	for (i = 0 ; i < ngroups ; i++) {
    416 		if ((grp = getgrnam(groups[i])) != NULL) {
    417 			for (j = 0 ; grp->gr_mem[j] ; j++) {
    418 				if (strcmp(user, grp->gr_mem[j]) == 0) {
    419 					/* already in it */
    420 					groups[i] = "";
    421 				}
    422 			}
    423 		}
    424 	}
    425 	if ((from = fopen(_PATH_GROUP, "r")) == (FILE *) NULL) {
    426 		warn("can't append group for %s: can't open %s", user, _PATH_GROUP);
    427 		return 0;
    428 	}
    429 	if (flock(fileno(from), LOCK_EX | LOCK_NB) < 0) {
    430 		warn("can't lock `%s'", _PATH_GROUP);
    431 	}
    432 	(void) fstat(fileno(from), &st);
    433 	(void) snprintf(f, sizeof(f), "%s.XXXXXX", _PATH_GROUP);
    434 	if ((fd = mkstemp(f)) < 0) {
    435 		(void) fclose(from);
    436 		warn("can't create gid: mkstemp failed");
    437 		return 0;
    438 	}
    439 	if ((to = fdopen(fd, "w")) == (FILE *) NULL) {
    440 		(void) fclose(from);
    441 		(void) close(fd);
    442 		(void) unlink(f);
    443 		warn("can't create gid: fdopen `%s' failed", f);
    444 		return 0;
    445 	}
    446 	while (fgets(buf, sizeof(buf), from) != NULL) {
    447 		cc = strlen(buf);
    448 		if ((colon = strchr(buf, ':')) == NULL) {
    449 			warn("badly formed entry `%s'", buf);
    450 			continue;
    451 		}
    452 		entc = (int)(colon - buf);
    453 		for (i = 0 ; i < ngroups ; i++) {
    454 			if ((groupc = strlen(groups[i])) == 0) {
    455 				continue;
    456 			}
    457 			if (entc == groupc && strncmp(groups[i], buf, (unsigned) entc) == 0) {
    458 				if ((nc = snprintf(&buf[cc - 1],
    459 						sizeof(buf) - cc + 1,
    460 						"%s%s\n",
    461 						(buf[cc - 2] == ':') ? "" : ",",
    462 						user)) < 0) {
    463 					warnx(
    464 "Warning: group `%s' entry too long", groups[i]);
    465 				}
    466 				cc += nc - 1;
    467 			}
    468 		}
    469 		if (fwrite(buf, sizeof(char), (unsigned) cc, to) != cc) {
    470 			(void) fclose(from);
    471 			(void) close(fd);
    472 			(void) unlink(f);
    473 			warn("can't create gid: short write to `%s'", f);
    474 			return 0;
    475 		}
    476 	}
    477 	(void) fclose(from);
    478 	(void) fclose(to);
    479 	if (rename(f, _PATH_GROUP) < 0) {
    480 		warn("can't create gid: can't rename `%s' to `%s'", f, _PATH_GROUP);
    481 		return 0;
    482 	}
    483 	(void) chmod(_PATH_GROUP, st.st_mode & 07777);
    484 	return 1;
    485 }
    486 
    487 /* return 1 if `login' is a valid login name */
    488 static int
    489 valid_login(char *login)
    490 {
    491 	char	*cp;
    492 
    493 	for (cp = login ; *cp ; cp++) {
    494 		if (!isalnum(*cp) && *cp != '.' && *cp != '_' && *cp != '-') {
    495 			return 0;
    496 		}
    497 	}
    498 	return 1;
    499 }
    500 
    501 /* return 1 if `group' is a valid group name */
    502 static int
    503 valid_group(char *group)
    504 {
    505 	char	*cp;
    506 
    507 	for (cp = group ; *cp ; cp++) {
    508 		if (!isalnum(*cp)) {
    509 			return 0;
    510 		}
    511 	}
    512 	return 1;
    513 }
    514 
    515 /* find the next gid in the range lo .. hi */
    516 static int
    517 getnextgid(int *gidp, int lo, int hi)
    518 {
    519 	for (*gidp = lo ; *gidp < hi ; *gidp += 1) {
    520 		if (getgrgid((gid_t)*gidp) == (struct group *) NULL) {
    521 			return 1;
    522 		}
    523 	}
    524 	return 0;
    525 }
    526 
    527 #ifdef EXTENSIONS
    528 /* save a range of uids */
    529 static int
    530 save_range(user_t *up, char *cp)
    531 {
    532 	int	from;
    533 	int	to;
    534 	int	i;
    535 
    536 	if (up->u_rsize == 0) {
    537 		up->u_rsize = 32;
    538 		NEWARRAY(range_t, up->u_rv, up->u_rsize, return(0));
    539 	} else if (up->u_rc == up->u_rsize) {
    540 		up->u_rsize *= 2;
    541 		RENEW(range_t, up->u_rv, up->u_rsize, return(0));
    542 	}
    543 	if (up->u_rv && sscanf(cp, "%d..%d", &from, &to) == 2) {
    544 		for (i = 0 ; i < up->u_rc ; i++) {
    545 			if (up->u_rv[i].r_from == from && up->u_rv[i].r_to == to) {
    546 				break;
    547 			}
    548 		}
    549 		if (i == up->u_rc) {
    550 			up->u_rv[up->u_rc].r_from = from;
    551 			up->u_rv[up->u_rc].r_to = to;
    552 			up->u_rc += 1;
    553 		}
    554 	} else {
    555 		warnx("Bad range `%s'", cp);
    556 		return 0;
    557 	}
    558 	return 1;
    559 }
    560 #endif
    561 
    562 /* set the defaults in the defaults file */
    563 static int
    564 setdefaults(user_t *up)
    565 {
    566 	char	template[MaxFileNameLen];
    567 	FILE	*fp;
    568 	int	ret;
    569 	int	fd;
    570 	int	i;
    571 
    572 	(void) snprintf(template, sizeof(template), "%s.XXXXXX", CONFFILE);
    573 	if ((fd = mkstemp(template)) < 0) {
    574 		warnx("can't mkstemp `%s' for writing", CONFFILE);
    575 		return 0;
    576 	}
    577 	if ((fp = fdopen(fd, "w")) == (FILE *) NULL) {
    578 		warn("can't fdopen `%s' for writing", CONFFILE);
    579 		return 0;
    580 	}
    581 	ret = 1;
    582 	if (fprintf(fp, "group\t\t%s\n", up->u_primgrp) <= 0 ||
    583 	    fprintf(fp, "base_dir\t%s\n", up->u_basedir) <= 0 ||
    584 	    fprintf(fp, "skel_dir\t%s\n", up->u_skeldir) <= 0 ||
    585 	    fprintf(fp, "shell\t\t%s\n", up->u_shell) <= 0 ||
    586 	    fprintf(fp, "inactive\t%d\n", up->u_inactive) <= 0 ||
    587 	    fprintf(fp, "expire\t\t%s\n", (up->u_expire == NULL) ? UNSET_EXPIRY : up->u_expire) <= 0) {
    588 		warn("can't write to `%s'", CONFFILE);
    589 		ret = 0;
    590 	}
    591 #ifdef EXTENSIONS
    592 	for (i = (up->u_defrc != up->u_rc) ? up->u_defrc : 0 ; i < up->u_rc ; i++) {
    593 		if (fprintf(fp, "range\t\t%d..%d\n", up->u_rv[i].r_from, up->u_rv[i].r_to) <= 0) {
    594 			warn("can't write to `%s'", CONFFILE);
    595 			ret = 0;
    596 		}
    597 	}
    598 #endif
    599 	(void) fclose(fp);
    600 	if (ret) {
    601 		ret = ((rename(template, CONFFILE) == 0) && (chmod(CONFFILE, 0644) == 0));
    602 	}
    603 	return ret;
    604 }
    605 
    606 /* read the defaults file */
    607 static void
    608 read_defaults(user_t *up)
    609 {
    610 	struct stat	st;
    611 	size_t		lineno;
    612 	size_t		len;
    613 	FILE		*fp;
    614 	char		*cp;
    615 	char		*s;
    616 
    617 	memsave(&up->u_primgrp, DEF_GROUP, strlen(DEF_GROUP));
    618 	memsave(&up->u_basedir, DEF_BASEDIR, strlen(DEF_BASEDIR));
    619 	memsave(&up->u_skeldir, DEF_SKELDIR, strlen(DEF_SKELDIR));
    620 	memsave(&up->u_shell, DEF_SHELL, strlen(DEF_SHELL));
    621 	memsave(&up->u_comment, DEF_COMMENT, strlen(DEF_COMMENT));
    622 	up->u_rsize = 16;
    623 	NEWARRAY(range_t, up->u_rv, up->u_rsize, exit(1));
    624 	up->u_inactive = DEF_INACTIVE;
    625 	up->u_expire = DEF_EXPIRE;
    626 	if ((fp = fopen(CONFFILE, "r")) == (FILE *) NULL) {
    627 		if (stat(CONFFILE, &st) < 0 && !setdefaults(up)) {
    628 			warn("can't create `%s' defaults file", CONFFILE);
    629 		}
    630 		fp = fopen(CONFFILE, "r");
    631 	}
    632 	if (fp != (FILE *) NULL) {
    633 		while ((s = fparseln(fp, &len, &lineno, NULL, 0)) != NULL) {
    634 			if (strncmp(s, "group", 5) == 0) {
    635 				for (cp = s + 5 ; *cp && isspace(*cp) ; cp++) {
    636 				}
    637 				memsave(&up->u_primgrp, cp, strlen(cp));
    638 			} else if (strncmp(s, "base_dir", 8) == 0) {
    639 				for (cp = s + 8 ; *cp && isspace(*cp) ; cp++) {
    640 				}
    641 				memsave(&up->u_basedir, cp, strlen(cp));
    642 			} else if (strncmp(s, "skel_dir", 8) == 0) {
    643 				for (cp = s + 8 ; *cp && isspace(*cp) ; cp++) {
    644 				}
    645 				memsave(&up->u_skeldir, cp, strlen(cp));
    646 			} else if (strncmp(s, "shell", 5) == 0) {
    647 				for (cp = s + 5 ; *cp && isspace(*cp) ; cp++) {
    648 				}
    649 				memsave(&up->u_shell, cp, strlen(cp));
    650 			} else if (strncmp(s, "inactive", 8) == 0) {
    651 				for (cp = s + 8 ; *cp && isspace(*cp) ; cp++) {
    652 				}
    653 				up->u_inactive = atoi(cp);
    654 #ifdef EXTENSIONS
    655 			} else if (strncmp(s, "range", 5) == 0) {
    656 				for (cp = s + 5 ; *cp && isspace(*cp) ; cp++) {
    657 				}
    658 				(void) save_range(up, cp);
    659 #endif
    660 #ifdef EXTENSIONS
    661 			} else if (strncmp(s, "preserve", 8) == 0) {
    662 				for (cp = s + 8 ; *cp && isspace(*cp) ; cp++) {
    663 				}
    664 				up->u_preserve = (strncmp(cp, "true", 4) == 0) ? 1 :
    665 						  (strncmp(cp, "yes", 3) == 0) ? 1 :
    666 						   atoi(cp);
    667 #endif
    668 			} else if (strncmp(s, "expire", 6) == 0) {
    669 				for (cp = s + 6 ; *cp && isspace(*cp) ; cp++) {
    670 				}
    671 				if (strcmp(cp, UNSET_EXPIRY) == 0) {
    672 					if (up->u_expire) {
    673 						FREE(up->u_expire);
    674 					}
    675 					up->u_expire = NULL;
    676 				} else {
    677 					memsave(&up->u_expire, cp, strlen(cp));
    678 				}
    679 			}
    680 			(void) free(s);
    681 		}
    682 		(void) fclose(fp);
    683 	}
    684 	if (up->u_rc == 0) {
    685 		up->u_rv[up->u_rc].r_from = DEF_LOWUID;
    686 		up->u_rv[up->u_rc].r_to = DEF_HIGHUID;
    687 		up->u_rc += 1;
    688 	}
    689 	up->u_defrc = up->u_rc;
    690 }
    691 
    692 /* return the next valid unused uid */
    693 static int
    694 getnextuid(int sync_uid_gid, int *uid, int low_uid, int high_uid)
    695 {
    696 	for (*uid = low_uid ; *uid <= high_uid ; (*uid)++) {
    697 		if (getpwuid((uid_t)(*uid)) == (struct passwd *) NULL && *uid != NOBODY_UID) {
    698 			if (sync_uid_gid) {
    699 				if (getgrgid((gid_t)(*uid)) == (struct group *) NULL) {
    700 					return 1;
    701 				}
    702 			} else {
    703 				return 1;
    704 			}
    705 		}
    706 	}
    707 	return 0;
    708 }
    709 
    710 /* add a user */
    711 static int
    712 adduser(char *login, user_t *up)
    713 {
    714 	struct group	*grp;
    715 	struct stat	st;
    716 	struct tm	tm;
    717 	time_t		expire;
    718 	char		password[PasswordLength + 1];
    719 	char		home[MaxFileNameLen];
    720 	char		buf[MaxFileNameLen];
    721 	int		sync_uid_gid;
    722 	int		masterfd;
    723 	int		ptmpfd;
    724 	int		gid;
    725 	int		cc;
    726 	int		i;
    727 
    728 	if (!valid_login(login)) {
    729 		errx(EXIT_FAILURE, "`%s' is not a valid login name", login);
    730 	}
    731 	if ((masterfd = open(_PATH_MASTERPASSWD, O_RDONLY)) < 0) {
    732 		err(EXIT_FAILURE, "can't open `%s'", _PATH_MASTERPASSWD);
    733 	}
    734 	if (flock(masterfd, LOCK_EX | LOCK_NB) < 0) {
    735 		err(EXIT_FAILURE, "can't lock `%s'", _PATH_MASTERPASSWD);
    736 	}
    737 	pw_init();
    738 	if ((ptmpfd = pw_lock(WAITSECS)) < 0) {
    739 		(void) close(masterfd);
    740 		err(EXIT_FAILURE, "can't obtain pw_lock");
    741 	}
    742 	while ((cc = read(masterfd, buf, sizeof(buf))) > 0) {
    743 		if (write(ptmpfd, buf, (size_t)(cc)) != cc) {
    744 			(void) close(masterfd);
    745 			(void) close(ptmpfd);
    746 			(void) pw_abort();
    747 			err(EXIT_FAILURE, "short write to /etc/ptmp (not %d chars)", cc);
    748 		}
    749 	}
    750 	/* if no uid was specified, get next one in [low_uid..high_uid] range */
    751 	sync_uid_gid = (strcmp(up->u_primgrp, "=uid") == 0);
    752 	if (up->u_uid == -1) {
    753 		for (i = 0 ; i < up->u_rc ; i++) {
    754 			if (getnextuid(sync_uid_gid, &up->u_uid, up->u_rv[i].r_from, up->u_rv[i].r_to)) {
    755 				break;
    756 			}
    757 		}
    758 		if (i == up->u_rc) {
    759 			(void) close(ptmpfd);
    760 			(void) pw_abort();
    761 			errx(EXIT_FAILURE, "can't get next uid for %d", up->u_uid);
    762 		}
    763 	}
    764 	/* check uid isn't already allocated */
    765 	if (!up->u_dupuid && getpwuid((uid_t)(up->u_uid)) != (struct passwd *) NULL) {
    766 		(void) close(ptmpfd);
    767 		(void) pw_abort();
    768 		errx(EXIT_FAILURE, "uid %d is already in use", up->u_uid);
    769 	}
    770 	/* if -g=uid was specified, check gid is unused */
    771 	if (sync_uid_gid) {
    772 		if (getgrgid((gid_t)(up->u_uid)) != (struct group *) NULL) {
    773 			(void) close(ptmpfd);
    774 			(void) pw_abort();
    775 			errx(EXIT_FAILURE, "gid %d is already in use", up->u_uid);
    776 		}
    777 		gid = up->u_uid;
    778 	} else if ((grp = getgrnam(up->u_primgrp)) != (struct group *) NULL) {
    779 		gid = grp->gr_gid;
    780 	} else if (is_number(up->u_primgrp) &&
    781 		   (grp = getgrgid((gid_t)atoi(up->u_primgrp))) != (struct group *) NULL) {
    782 		gid = grp->gr_gid;
    783 	} else {
    784 		(void) close(ptmpfd);
    785 		(void) pw_abort();
    786 		errx(EXIT_FAILURE, "group %s not found", up->u_primgrp);
    787 	}
    788 	/* check name isn't already in use */
    789 	if (!up->u_dupuid && getpwnam(login) != (struct passwd *) NULL) {
    790 		(void) close(ptmpfd);
    791 		(void) pw_abort();
    792 		errx(EXIT_FAILURE, "already a `%s' user", login);
    793 	}
    794 	if (up->u_homeset) {
    795 		(void) strlcpy(home, up->u_home, sizeof(home));
    796 	} else {
    797 		/* if home directory hasn't been given, make it up */
    798 		(void) snprintf(home, sizeof(home), "%s/%s", up->u_basedir, login);
    799 	}
    800 	expire = 0;
    801 	if (up->u_expire != NULL) {
    802 		(void) memset(&tm, 0, sizeof(tm));
    803 		if (strptime(up->u_expire, "%c", &tm) == NULL) {
    804 			warnx("invalid time format `%s'", optarg);
    805 		} else {
    806 			expire = mktime(&tm);
    807 		}
    808 	}
    809 	if (lstat(home, &st) < 0 && !up->u_mkdir) {
    810 		warnx(
    811 "Warning: home directory `%s' doesn't exist, and -m was not specified",
    812 			home);
    813 	}
    814 	password[PasswordLength] = '\0';
    815 	if (up->u_password != NULL &&
    816 	    strlen(up->u_password) == PasswordLength) {
    817 		(void) memcpy(password, up->u_password, PasswordLength);
    818 	} else {
    819 		(void) memset(password, '*', PasswordLength);
    820 		if (up->u_password != NULL) {
    821 			warnx("Password `%s' is invalid: setting it to `%s'",
    822 				up->u_password, password);
    823 		}
    824 	}
    825 	cc = snprintf(buf, sizeof(buf), "%s:%s:%d:%d::%d:%ld:%s:%s:%s\n",
    826 			login,
    827 			password,
    828 			up->u_uid,
    829 			gid,
    830 			up->u_inactive,
    831 			(long) expire,
    832 			up->u_comment,
    833 			home,
    834 			up->u_shell);
    835 	if (write(ptmpfd, buf, (size_t) cc) != cc) {
    836 		(void) close(ptmpfd);
    837 		(void) pw_abort();
    838 		err(EXIT_FAILURE, "can't add `%s'", buf);
    839 	}
    840 	if (up->u_mkdir) {
    841 		if (lstat(home, &st) < 0 && asystem("%s -p %s", MKDIR, home) != 0) {
    842 			(void) close(ptmpfd);
    843 			(void) pw_abort();
    844 			err(EXIT_FAILURE, "can't mkdir `%s'", home);
    845 		}
    846 		(void) copydotfiles(up->u_skeldir, up->u_uid, gid, home);
    847 	}
    848 	if (strcmp(up->u_primgrp, "=uid") == 0 &&
    849 	    getgrnam(login) == (struct group *) NULL &&
    850 	    !creategid(login, gid, login)) {
    851 		(void) close(ptmpfd);
    852 		(void) pw_abort();
    853 		err(EXIT_FAILURE, "can't create gid %d for login name %s", gid, login);
    854 	}
    855 	(void) close(ptmpfd);
    856 	if (pw_mkdb() < 0) {
    857 		err(EXIT_FAILURE, "pw_mkdb failed");
    858 	}
    859 	return 1;
    860 }
    861 
    862 /* modify a user */
    863 static int
    864 moduser(char *login, char *newlogin, user_t *up)
    865 {
    866 	struct passwd	*pwp;
    867 	struct group	*grp;
    868 	struct tm	tm;
    869 	time_t		expire;
    870 	size_t		loginc;
    871 	size_t		colonc;
    872 	FILE		*master;
    873 	char		password[PasswordLength + 1];
    874 	char		oldhome[MaxFileNameLen];
    875 	char		home[MaxFileNameLen];
    876 	char		buf[MaxFileNameLen];
    877 	char		*colon;
    878 	int		masterfd;
    879 	int		ptmpfd;
    880 	int		gid;
    881 	int		cc;
    882 
    883 	if (!valid_login(newlogin)) {
    884 		errx(EXIT_FAILURE, "`%s' is not a valid login name", login);
    885 	}
    886 	if ((pwp = getpwnam(login)) == (struct passwd *) NULL) {
    887 		errx(EXIT_FAILURE, "No such user `%s'", login);
    888 	}
    889 	if ((masterfd = open(_PATH_MASTERPASSWD, O_RDONLY)) < 0) {
    890 		err(EXIT_FAILURE, "can't open `%s'", _PATH_MASTERPASSWD);
    891 	}
    892 	if (flock(masterfd, LOCK_EX | LOCK_NB) < 0) {
    893 		err(EXIT_FAILURE, "can't lock `%s'", _PATH_MASTERPASSWD);
    894 	}
    895 	pw_init();
    896 	if ((ptmpfd = pw_lock(WAITSECS)) < 0) {
    897 		(void) close(masterfd);
    898 		err(EXIT_FAILURE, "can't obtain pw_lock");
    899 	}
    900 	if ((master = fdopen(masterfd, "r")) == (FILE *) NULL) {
    901 		(void) close(masterfd);
    902 		(void) close(ptmpfd);
    903 		(void) pw_abort();
    904 		err(EXIT_FAILURE, "can't fdopen fd for %s", _PATH_MASTERPASSWD);
    905 	}
    906 	if (up != (user_t *) NULL) {
    907 		if (up->u_mkdir) {
    908 			(void) strcpy(oldhome, pwp->pw_dir);
    909 		}
    910 		if (up->u_uid == -1) {
    911 			up->u_uid = pwp->pw_uid;
    912 		}
    913 		/* if -g=uid was specified, check gid is unused */
    914 		if (strcmp(up->u_primgrp, "=uid") == 0) {
    915 			if (getgrgid((gid_t)(up->u_uid)) != (struct group *) NULL) {
    916 				(void) close(ptmpfd);
    917 				(void) pw_abort();
    918 				errx(EXIT_FAILURE, "gid %d is already in use", up->u_uid);
    919 			}
    920 			gid = up->u_uid;
    921 		} else if ((grp = getgrnam(up->u_primgrp)) != (struct group *) NULL) {
    922 			gid = grp->gr_gid;
    923 		} else if (is_number(up->u_primgrp) &&
    924 			   (grp = getgrgid((gid_t)atoi(up->u_primgrp))) != (struct group *) NULL) {
    925 			gid = grp->gr_gid;
    926 		} else {
    927 			(void) close(ptmpfd);
    928 			(void) pw_abort();
    929 			errx(EXIT_FAILURE, "group %s not found", up->u_primgrp);
    930 		}
    931 		/* if changing name, check new name isn't already in use */
    932 		if (strcmp(login, newlogin) != 0 && getpwnam(newlogin) != (struct passwd *) NULL) {
    933 			(void) close(ptmpfd);
    934 			(void) pw_abort();
    935 			errx(EXIT_FAILURE, "already a `%s' user", newlogin);
    936 		}
    937 		/* if home directory hasn't been given, use the old one */
    938 		if (!up->u_homeset) {
    939 			(void) strcpy(home, pwp->pw_dir);
    940 		}
    941 		expire = 0;
    942 		if (up->u_expire != NULL) {
    943 			(void) memset(&tm, 0, sizeof(tm));
    944 			if (strptime(up->u_expire, "%c", &tm) == NULL) {
    945 				warnx("invalid time format `%s'", optarg);
    946 			} else {
    947 				expire = mktime(&tm);
    948 			}
    949 		}
    950 		password[PasswordLength] = '\0';
    951 		if (up->u_password != NULL &&
    952 		    strlen(up->u_password) == PasswordLength) {
    953 			(void) memcpy(password, up->u_password, PasswordLength);
    954 		} else {
    955 			(void) memcpy(password, pwp->pw_passwd, PasswordLength);
    956 		}
    957 		if (strcmp(up->u_comment, DEF_COMMENT) == 0) {
    958 			memsave(&up->u_comment, pwp->pw_gecos, strlen(pwp->pw_gecos));
    959 		}
    960 		if (strcmp(up->u_shell, DEF_SHELL) == 0 && strcmp(pwp->pw_shell, DEF_SHELL) != 0) {
    961 			memsave(&up->u_shell, pwp->pw_shell, strlen(pwp->pw_shell));
    962 		}
    963 	}
    964 	loginc = strlen(login);
    965 	while (fgets(buf, sizeof(buf), master) != NULL) {
    966 		cc = strlen(buf);
    967 		if ((colon = strchr(buf, ':')) == NULL) {
    968 			warnx("Malformed entry `%s'. Skipping", buf);
    969 			continue;
    970 		}
    971 		colonc = (size_t)(colon - buf);
    972 		if (strncmp(login, buf, loginc) == 0 && loginc == colonc) {
    973 			if (up != (user_t *) NULL) {
    974 				cc = snprintf(buf, sizeof(buf), "%s:%s:%d:%d::%d:%ld:%s:%s:%s\n",
    975 					newlogin,
    976 					password,
    977 					up->u_uid,
    978 					gid,
    979 					up->u_inactive,
    980 					(long) expire,
    981 					up->u_comment,
    982 					home,
    983 					up->u_shell);
    984 				if (write(ptmpfd, buf, (size_t) cc) != cc) {
    985 					(void) close(ptmpfd);
    986 					(void) pw_abort();
    987 					err(EXIT_FAILURE, "can't add `%s'", buf);
    988 				}
    989 			}
    990 		} else if (write(ptmpfd, buf, (size_t)(cc)) != cc) {
    991 			(void) close(masterfd);
    992 			(void) close(ptmpfd);
    993 			(void) pw_abort();
    994 			err(EXIT_FAILURE,
    995 				"short write to /etc/ptmp (not %d chars)",
    996 				cc);
    997 		}
    998 	}
    999 	if (up != (user_t *) NULL) {
   1000 		if (up->u_mkdir &&
   1001 		    asystem("%s %s %s", MV, oldhome, home) != 0) {
   1002 			(void) close(ptmpfd);
   1003 			(void) pw_abort();
   1004 			err(EXIT_FAILURE, "can't move `%s' to `%s'",
   1005 					oldhome, home);
   1006 		}
   1007 		if (up->u_groupc > 0 &&
   1008 		    !append_group(newlogin, up->u_groupc, up->u_groupv)) {
   1009 			(void) close(ptmpfd);
   1010 			(void) pw_abort();
   1011 			errx(EXIT_FAILURE,
   1012 				"can't append `%s' to new groups",
   1013 				newlogin);
   1014 		}
   1015 	}
   1016 	(void) close(ptmpfd);
   1017 	if (pw_mkdb() < 0) {
   1018 		err(EXIT_FAILURE, "pw_mkdb failed");
   1019 	}
   1020 	return 1;
   1021 }
   1022 
   1023 
   1024 #ifdef EXTENSIONS
   1025 /* see if we can find out the user struct */
   1026 static struct passwd *
   1027 find_user_info(char *name)
   1028 {
   1029 	struct passwd	*pwp;
   1030 
   1031 	if ((pwp = getpwnam(name)) != (struct passwd *) NULL) {
   1032 		return pwp;
   1033 	}
   1034 	if (is_number(name) && (pwp = getpwuid((uid_t)atoi(name))) != (struct passwd *) NULL) {
   1035 		return pwp;
   1036 	}
   1037 	return (struct passwd *) NULL;
   1038 }
   1039 #endif
   1040 
   1041 #ifdef EXTENSIONS
   1042 /* see if we can find out the group struct */
   1043 static struct group *
   1044 find_group_info(char *name)
   1045 {
   1046 	struct group	*grp;
   1047 
   1048 	if ((grp = getgrnam(name)) != (struct group *) NULL) {
   1049 		return grp;
   1050 	}
   1051 	if (is_number(name) && (grp = getgrgid((gid_t)atoi(name))) != (struct group *) NULL) {
   1052 		return grp;
   1053 	}
   1054 	return (struct group *) NULL;
   1055 }
   1056 #endif
   1057 
   1058 /* print out usage message, and then exit */
   1059 void
   1060 usermgmt_usage(char *prog)
   1061 {
   1062 	if (strcmp(prog, "useradd") == 0) {
   1063 		(void) fprintf(stderr, "Usage: %s -D [-b basedir] [-e expiry] "
   1064 		    "[-f inactive] [-g group] [-r lowuid..highuid] [-s shell]"
   1065 		    "\n", prog);
   1066 		(void) fprintf(stderr, "Usage: %s [-G group] [-b basedir] "
   1067 		    "[-c comment] [-d homedir] [-e expiry] [-f inactive]\n"
   1068 		    "\t[-g group] [-k skeletondir] [-m] [-o] [-p password] "
   1069 		    "[-r lowuid..highuid] [-s shell]\n\t[-u uid] [-v] user\n",
   1070 		    prog);
   1071 	} else if (strcmp(prog, "usermod") == 0) {
   1072 		(void) fprintf(stderr, "Usage: %s [-G group] [-c comment] "
   1073 		    "[-d homedir] [-e expire] [-f inactive] [-g group] "
   1074 		    "[-l newname] [-m] [-o] [-p password] [-s shell] [-u uid] "
   1075 		    "[-v] user\n", prog);
   1076 	} else if (strcmp(prog, "userdel") == 0) {
   1077 		(void) fprintf(stderr, "Usage: %s -D [-p preserve]\n", prog);
   1078 		(void) fprintf(stderr, "Usage: %s [-p preserve] [-r] [-v] "
   1079 			"user\n", prog);
   1080 #ifdef EXTENSIONS
   1081 	} else if (strcmp(prog, "userinfo") == 0) {
   1082 		(void) fprintf(stderr, "Usage: %s [-e] [-v] user\n", prog);
   1083 #endif
   1084 	} else if (strcmp(prog, "groupadd") == 0) {
   1085 		(void) fprintf(stderr, "Usage: %s [-g gid] [-o] [-v] group\n",
   1086 			prog);
   1087 	} else if (strcmp(prog, "groupdel") == 0) {
   1088 		(void) fprintf(stderr, "Usage: %s [-v] group\n", prog);
   1089 	} else if (strcmp(prog, "groupmod") == 0) {
   1090 		(void) fprintf(stderr, "Usage: %s [-g gid] [-o] [-n newname] "
   1091 			"[-v] group\n", prog);
   1092 	} else if (strcmp(prog, "user") == 0 || strcmp(prog, "group") == 0) {
   1093 		(void) fprintf(stderr,
   1094 			"Usage: %s ( add | del | mod | info ) ...\n",
   1095 			prog);
   1096 #ifdef EXTENSIONS
   1097 	} else if (strcmp(prog, "groupinfo") == 0) {
   1098 		(void) fprintf(stderr, "Usage: %s [-e] [-v] group\n", prog);
   1099 #endif
   1100 	}
   1101 	exit(EXIT_FAILURE);
   1102 	/* NOTREACHED */
   1103 }
   1104 
   1105 #ifdef EXTENSIONS
   1106 #define ADD_OPT_EXTENSIONS	"p:r:v"
   1107 #else
   1108 #define ADD_OPT_EXTENSIONS
   1109 #endif
   1110 
   1111 int
   1112 useradd(int argc, char **argv)
   1113 {
   1114 	user_t	u;
   1115 	int	defaultfield;
   1116 	int	bigD;
   1117 	int	c;
   1118 	int	i;
   1119 
   1120 	(void) memset(&u, 0, sizeof(u));
   1121 	read_defaults(&u);
   1122 	u.u_uid = -1;
   1123 	defaultfield = bigD = 0;
   1124 	while ((c = getopt(argc, argv, "DG:b:c:d:e:f:g:k:mou:s:" ADD_OPT_EXTENSIONS)) != -1) {
   1125 		switch(c) {
   1126 		case 'D':
   1127 			bigD = 1;
   1128 			break;
   1129 		case 'G':
   1130 			while ((u.u_groupv[u.u_groupc] = strsep(&optarg, ",")) != NULL &&
   1131 			       u.u_groupc < NGROUPS_MAX) {
   1132 				if (u.u_groupv[u.u_groupc][0] != 0) {
   1133 					u.u_groupc++;
   1134 				}
   1135 			}
   1136 			if (optarg != NULL) {
   1137 				warnx("Truncated list of secondary groups to %d entries", NGROUPS_MAX);
   1138 			}
   1139 			break;
   1140 		case 'b':
   1141 			defaultfield = 1;
   1142 			memsave(&u.u_basedir, optarg, strlen(optarg));
   1143 			break;
   1144 		case 'c':
   1145 			memsave(&u.u_comment, optarg, strlen(optarg));
   1146 			break;
   1147 		case 'd':
   1148 			u.u_homeset = 1;
   1149 			memsave(&u.u_home, optarg, strlen(optarg));
   1150 			break;
   1151 		case 'e':
   1152 			defaultfield = 1;
   1153 			memsave(&u.u_expire, optarg, strlen(optarg));
   1154 			break;
   1155 		case 'f':
   1156 			defaultfield = 1;
   1157 			u.u_inactive = atoi(optarg);
   1158 			break;
   1159 		case 'g':
   1160 			defaultfield = 1;
   1161 			memsave(&u.u_primgrp, optarg, strlen(optarg));
   1162 			break;
   1163 		case 'k':
   1164 			memsave(&u.u_skeldir, optarg, strlen(optarg));
   1165 			break;
   1166 		case 'm':
   1167 			u.u_mkdir = 1;
   1168 			break;
   1169 		case 'o':
   1170 			u.u_dupuid = 1;
   1171 			break;
   1172 #ifdef EXTENSIONS
   1173 		case 'p':
   1174 			memsave(&u.u_password, optarg, strlen(optarg));
   1175 			break;
   1176 #endif
   1177 #ifdef EXTENSIONS
   1178 		case 'r':
   1179 			defaultfield = 1;
   1180 			(void) save_range(&u, optarg);
   1181 			break;
   1182 #endif
   1183 		case 's':
   1184 			defaultfield = 1;
   1185 			memsave(&u.u_shell, optarg, strlen(optarg));
   1186 			break;
   1187 		case 'u':
   1188 			if (!is_number(optarg)) {
   1189 				errx(EXIT_FAILURE, "When using [-u uid], the uid must be numeric");
   1190 			}
   1191 			u.u_uid = atoi(optarg);
   1192 			break;
   1193 #ifdef EXTENSIONS
   1194 		case 'v':
   1195 			verbose = 1;
   1196 			break;
   1197 #endif
   1198 		}
   1199 	}
   1200 	if (bigD) {
   1201 		if (defaultfield) {
   1202 			checkeuid();
   1203 			return setdefaults(&u) ? EXIT_SUCCESS : EXIT_FAILURE;
   1204 		}
   1205 		(void) printf("group\t\t%s\n", u.u_primgrp);
   1206 		(void) printf("base_dir\t%s\n", u.u_basedir);
   1207 		(void) printf("skel_dir\t%s\n", u.u_skeldir);
   1208 		(void) printf("shell\t\t%s\n", u.u_shell);
   1209 		(void) printf("inactive\t%d\n", u.u_inactive);
   1210 		(void) printf("expire\t\t%s\n", (u.u_expire == NULL) ? UNSET_EXPIRY : u.u_expire);
   1211 #ifdef EXTENSIONS
   1212 		for (i = 0 ; i < u.u_rc ; i++) {
   1213 			(void) printf("range\t\t%d..%d\n", u.u_rv[i].r_from, u.u_rv[i].r_to);
   1214 		}
   1215 #endif
   1216 		return EXIT_SUCCESS;
   1217 	}
   1218 	if (argc == optind) {
   1219 		usermgmt_usage("useradd");
   1220 	}
   1221 	checkeuid();
   1222 	return adduser(argv[optind], &u) ? EXIT_SUCCESS : EXIT_FAILURE;
   1223 }
   1224 
   1225 #ifdef EXTENSIONS
   1226 #define MOD_OPT_EXTENSIONS	"p:v"
   1227 #else
   1228 #define MOD_OPT_EXTENSIONS
   1229 #endif
   1230 
   1231 int
   1232 usermod(int argc, char **argv)
   1233 {
   1234 	user_t	u;
   1235 	char	newuser[MaxUserNameLen + 1];
   1236 	int	have_new_user;
   1237 	int	c;
   1238 
   1239 	(void) memset(&u, 0, sizeof(u));
   1240 	(void) memset(newuser, 0, sizeof(newuser));
   1241 	read_defaults(&u);
   1242 	u.u_uid = -1;
   1243 	have_new_user = 0;
   1244 	while ((c = getopt(argc, argv, "G:c:d:e:f:g:l:mos:u:" MOD_OPT_EXTENSIONS)) != -1) {
   1245 		switch(c) {
   1246 		case 'G':
   1247 			while ((u.u_groupv[u.u_groupc] = strsep(&optarg, ",")) != NULL &&
   1248 			       u.u_groupc < NGROUPS_MAX) {
   1249 				if (u.u_groupv[u.u_groupc][0] != 0) {
   1250 					u.u_groupc++;
   1251 				}
   1252 			}
   1253 			if (optarg != NULL) {
   1254 				warnx("Truncated list of secondary groups to %d entries", NGROUPS_MAX);
   1255 			}
   1256 			break;
   1257 		case 'c':
   1258 			memsave(&u.u_comment, optarg, strlen(optarg));
   1259 			break;
   1260 		case 'd':
   1261 			u.u_homeset = 1;
   1262 			memsave(&u.u_home, optarg, strlen(optarg));
   1263 			break;
   1264 		case 'e':
   1265 			memsave(&u.u_expire, optarg, strlen(optarg));
   1266 			break;
   1267 		case 'f':
   1268 			u.u_inactive = atoi(optarg);
   1269 			break;
   1270 		case 'g':
   1271 			memsave(&u.u_primgrp, optarg, strlen(optarg));
   1272 			break;
   1273 		case 'l':
   1274 			have_new_user = 1;
   1275 			(void) strlcpy(newuser, optarg, sizeof(newuser));
   1276 			break;
   1277 		case 'm':
   1278 			u.u_mkdir = 1;
   1279 			break;
   1280 		case 'o':
   1281 			u.u_dupuid = 1;
   1282 			break;
   1283 #ifdef EXTENSIONS
   1284 		case 'p':
   1285 			memsave(&u.u_password, optarg, strlen(optarg));
   1286 			break;
   1287 #endif
   1288 		case 's':
   1289 			memsave(&u.u_shell, optarg, strlen(optarg));
   1290 			break;
   1291 		case 'u':
   1292 			if (!is_number(optarg)) {
   1293 				errx(EXIT_FAILURE, "When using [-u uid], the uid must be numeric");
   1294 			}
   1295 			u.u_uid = atoi(optarg);
   1296 			break;
   1297 #ifdef EXTENSIONS
   1298 		case 'v':
   1299 			verbose = 1;
   1300 			break;
   1301 #endif
   1302 		}
   1303 	}
   1304 	if (argc == optind) {
   1305 		usermgmt_usage("usermod");
   1306 	}
   1307 	checkeuid();
   1308 	return moduser(argv[optind], (have_new_user) ? newuser : argv[optind], &u) ? EXIT_SUCCESS : EXIT_FAILURE;
   1309 }
   1310 
   1311 #ifdef EXTENSIONS
   1312 #define DEL_OPT_EXTENSIONS	"Dp:v"
   1313 #else
   1314 #define DEL_OPT_EXTENSIONS
   1315 #endif
   1316 
   1317 int
   1318 userdel(int argc, char **argv)
   1319 {
   1320 	struct passwd	*pwp;
   1321 	struct stat	st;
   1322 	user_t		u;
   1323 	char		password[PasswordLength + 1];
   1324 	int		defaultfield;
   1325 	int		rmhome;
   1326 	int		bigD;
   1327 	int		c;
   1328 
   1329 	(void) memset(&u, 0, sizeof(u));
   1330 	read_defaults(&u);
   1331 	defaultfield = bigD = rmhome = 0;
   1332 	while ((c = getopt(argc, argv, "r" DEL_OPT_EXTENSIONS)) != -1) {
   1333 		switch(c) {
   1334 #ifdef EXTENSIONS
   1335 		case 'D':
   1336 			bigD = 1;
   1337 			break;
   1338 #endif
   1339 #ifdef EXTENSIONS
   1340 		case 'p':
   1341 			defaultfield = 1;
   1342 			u.u_preserve = (strcmp(optarg, "true") == 0) ? 1 :
   1343 					(strcmp(optarg, "yes") == 0) ? 1 :
   1344 					 atoi(optarg);
   1345 			break;
   1346 #endif
   1347 		case 'r':
   1348 			rmhome = 1;
   1349 			break;
   1350 #ifdef EXTENSIONS
   1351 		case 'v':
   1352 			verbose = 1;
   1353 			break;
   1354 #endif
   1355 		}
   1356 	}
   1357 #ifdef EXTENSIONS
   1358 	if (bigD) {
   1359 		if (defaultfield) {
   1360 			checkeuid();
   1361 			return setdefaults(&u) ? EXIT_SUCCESS : EXIT_FAILURE;
   1362 		}
   1363 		(void) printf("preserve\t%s\n", (u.u_preserve) ? "true" : "false");
   1364 		return EXIT_SUCCESS;
   1365 	}
   1366 #endif
   1367 	if (argc == optind) {
   1368 		usermgmt_usage("userdel");
   1369 	}
   1370 	checkeuid();
   1371 	if ((pwp = getpwnam(argv[optind])) == (struct passwd *) NULL) {
   1372 		warnx("No such user `%s'", argv[optind]);
   1373 		return EXIT_FAILURE;
   1374 	}
   1375 	if (rmhome) {
   1376 		if (stat(pwp->pw_dir, &st) < 0) {
   1377 			warn("Home directory `%s' does not exist", pwp->pw_dir);
   1378 			return EXIT_FAILURE;
   1379 		}
   1380 		(void) asystem("%s -rf %s", RM, pwp->pw_dir);
   1381 	}
   1382 	if (u.u_preserve) {
   1383 		memsave(&u.u_shell, NOLOGIN, strlen(NOLOGIN));
   1384 		(void) memset(password, '*', PasswordLength);
   1385 		password[PasswordLength] = '\0';
   1386 		memsave(&u.u_password, password, PasswordLength);
   1387 		return moduser(argv[optind], argv[optind], &u) ? EXIT_SUCCESS : EXIT_FAILURE;
   1388 	}
   1389 	return moduser(argv[optind], argv[optind], (user_t *) NULL) ? EXIT_SUCCESS : EXIT_FAILURE;
   1390 }
   1391 
   1392 #ifdef EXTENSIONS
   1393 #define GROUP_ADD_OPT_EXTENSIONS	"v"
   1394 #else
   1395 #define GROUP_ADD_OPT_EXTENSIONS
   1396 #endif
   1397 
   1398 /* add a group */
   1399 int
   1400 groupadd(int argc, char **argv)
   1401 {
   1402 	int	dupgid;
   1403 	int	gid;
   1404 	int	c;
   1405 
   1406 	gid = -1;
   1407 	dupgid = 0;
   1408 	while ((c = getopt(argc, argv, "g:o" GROUP_ADD_OPT_EXTENSIONS)) != -1) {
   1409 		switch(c) {
   1410 		case 'g':
   1411 			if (!is_number(optarg)) {
   1412 				errx(EXIT_FAILURE, "When using [-g gid], the gid must be numeric");
   1413 			}
   1414 			gid = atoi(optarg);
   1415 			break;
   1416 		case 'o':
   1417 			dupgid = 1;
   1418 			break;
   1419 #ifdef EXTENSIONS
   1420 		case 'v':
   1421 			verbose = 1;
   1422 			break;
   1423 #endif
   1424 		}
   1425 	}
   1426 	if (argc == optind) {
   1427 		usermgmt_usage("groupadd");
   1428 	}
   1429 	checkeuid();
   1430 	if (gid < 0 && !getnextgid(&gid, LowGid, HighGid)) {
   1431 		err(EXIT_FAILURE, "can't add group: can't get next gid");
   1432 	}
   1433 	if (!dupgid && getgrgid((gid_t) gid) != (struct group *) NULL) {
   1434 		errx(EXIT_FAILURE, "can't add group: gid %d is a duplicate", gid);
   1435 	}
   1436 	if (!valid_group(argv[optind])) {
   1437 		warnx("warning - invalid group name `%s'", argv[optind]);
   1438 	}
   1439 	if (!creategid(argv[optind], gid, "")) {
   1440 		err(EXIT_FAILURE, "can't add group: problems with %s file", _PATH_GROUP);
   1441 	}
   1442 	return EXIT_SUCCESS;
   1443 }
   1444 
   1445 #ifdef EXTENSIONS
   1446 #define GROUP_DEL_OPT_EXTENSIONS	"v"
   1447 #else
   1448 #define GROUP_DEL_OPT_EXTENSIONS
   1449 #endif
   1450 
   1451 /* remove a group */
   1452 int
   1453 groupdel(int argc, char **argv)
   1454 {
   1455 	int	c;
   1456 
   1457 	while ((c = getopt(argc, argv, "" GROUP_DEL_OPT_EXTENSIONS)) != -1) {
   1458 		switch(c) {
   1459 #ifdef EXTENSIONS
   1460 		case 'v':
   1461 			verbose = 1;
   1462 			break;
   1463 #endif
   1464 		}
   1465 	}
   1466 	if (argc == optind) {
   1467 		usermgmt_usage("groupdel");
   1468 	}
   1469 	checkeuid();
   1470 	if (!modify_gid(argv[optind], NULL)) {
   1471 		err(EXIT_FAILURE, "can't change %s file", _PATH_GROUP);
   1472 	}
   1473 	return EXIT_SUCCESS;
   1474 }
   1475 
   1476 #ifdef EXTENSIONS
   1477 #define GROUP_MOD_OPT_EXTENSIONS	"v"
   1478 #else
   1479 #define GROUP_MOD_OPT_EXTENSIONS
   1480 #endif
   1481 
   1482 /* modify a group */
   1483 int
   1484 groupmod(int argc, char **argv)
   1485 {
   1486 	struct group	*grp;
   1487 	char		buf[MaxEntryLen];
   1488 	char		*newname;
   1489 	char		**cpp;
   1490 	int		dupgid;
   1491 	int		gid;
   1492 	int		cc;
   1493 	int		c;
   1494 
   1495 	gid = -1;
   1496 	dupgid = 0;
   1497 	newname = NULL;
   1498 	while ((c = getopt(argc, argv, "g:on:" GROUP_MOD_OPT_EXTENSIONS)) != -1) {
   1499 		switch(c) {
   1500 		case 'g':
   1501 			if (!is_number(optarg)) {
   1502 				errx(EXIT_FAILURE, "When using [-g gid], the gid must be numeric");
   1503 			}
   1504 			gid = atoi(optarg);
   1505 			break;
   1506 		case 'o':
   1507 			dupgid = 1;
   1508 			break;
   1509 		case 'n':
   1510 			memsave(&newname, optarg, strlen(optarg));
   1511 			break;
   1512 #ifdef EXTENSIONS
   1513 		case 'v':
   1514 			verbose = 1;
   1515 			break;
   1516 #endif
   1517 		}
   1518 	}
   1519 	if (argc == optind) {
   1520 		usermgmt_usage("groupmod");
   1521 	}
   1522 	checkeuid();
   1523 	if (gid < 0 && newname == NULL) {
   1524 		err(EXIT_FAILURE, "Nothing to change");
   1525 	}
   1526 	if (dupgid && gid < 0) {
   1527 		err(EXIT_FAILURE, "Duplicate which gid?");
   1528 	}
   1529 	if ((grp = getgrnam(argv[optind])) == (struct group *) NULL) {
   1530 		err(EXIT_FAILURE, "can't find group `%s' to modify", argv[optind]);
   1531 	}
   1532 	if (newname != NULL && !valid_group(newname)) {
   1533 		warn("warning - invalid group name `%s'", newname);
   1534 	}
   1535 	cc = snprintf(buf, sizeof(buf), "%s:%s:%d:",
   1536 			(newname) ? newname : grp->gr_name,
   1537 			grp->gr_passwd,
   1538 			(gid < 0) ? grp->gr_gid : gid);
   1539 	for (cpp = grp->gr_mem ; *cpp && cc < sizeof(buf) ; cpp++) {
   1540 		cc += snprintf(&buf[cc], sizeof(buf) - cc, "%s%s", *cpp,
   1541 			(cpp[1] == NULL) ? "" : ",");
   1542 	}
   1543 	if (!modify_gid(argv[optind], buf)) {
   1544 		err(EXIT_FAILURE, "can't change %s file", _PATH_GROUP);
   1545 	}
   1546 	return EXIT_SUCCESS;
   1547 }
   1548 
   1549 #ifdef EXTENSIONS
   1550 /* display user information */
   1551 int
   1552 userinfo(int argc, char **argv)
   1553 {
   1554 	struct passwd	*pwp;
   1555 	struct group	*grp;
   1556 	char		buf[MaxEntryLen];
   1557 	char		**cpp;
   1558 	int		exists;
   1559 	int		cc;
   1560 	int		i;
   1561 
   1562 	exists = 0;
   1563 	while ((i = getopt(argc, argv, "ev")) != -1) {
   1564 		switch(i) {
   1565 		case 'e':
   1566 			exists = 1;
   1567 			break;
   1568 		case 'v':
   1569 			verbose = 1;
   1570 			break;
   1571 		}
   1572 	}
   1573 	if (argc == optind) {
   1574 		usermgmt_usage("userinfo");
   1575 	}
   1576 	pwp = find_user_info(argv[optind]);
   1577 	if (exists) {
   1578 		exit((pwp) ? EXIT_SUCCESS : EXIT_FAILURE);
   1579 	}
   1580 	if (pwp == (struct passwd *) NULL) {
   1581 		errx(EXIT_FAILURE, "can't find user `%s'", argv[optind]);
   1582 	}
   1583 	(void) printf("login\t%s\n", pwp->pw_name);
   1584 	(void) printf("passwd\t%s\n", pwp->pw_passwd);
   1585 	(void) printf("uid\t%d\n", pwp->pw_uid);
   1586 	for (cc = 0 ; (grp = getgrent()) != (struct group *) NULL ; ) {
   1587 		for (cpp = grp->gr_mem ; *cpp ; cpp++) {
   1588 			if (strcmp(*cpp, argv[optind]) == 0 && grp->gr_gid != pwp->pw_gid) {
   1589 				cc += snprintf(&buf[cc], sizeof(buf) - cc, "%s ", grp->gr_name);
   1590 			}
   1591 		}
   1592 	}
   1593 	if ((grp = getgrgid(pwp->pw_gid)) == (struct group *) NULL) {
   1594 		(void) printf("groups\t%d %s\n", pwp->pw_gid, buf);
   1595 	} else {
   1596 		(void) printf("groups\t%s %s\n", grp->gr_name, buf);
   1597 	}
   1598 	(void) printf("change\t%s", ctime(&pwp->pw_change));
   1599 	(void) printf("class\t%s\n", pwp->pw_class);
   1600 	(void) printf("gecos\t%s\n", pwp->pw_gecos);
   1601 	(void) printf("dir\t%s\n", pwp->pw_dir);
   1602 	(void) printf("shell\t%s\n", pwp->pw_shell);
   1603 	(void) printf("expire\t%s", ctime(&pwp->pw_expire));
   1604 	return EXIT_SUCCESS;
   1605 }
   1606 #endif
   1607 
   1608 #ifdef EXTENSIONS
   1609 /* display user information */
   1610 int
   1611 groupinfo(int argc, char **argv)
   1612 {
   1613 	struct group	*grp;
   1614 	char		**cpp;
   1615 	int		exists;
   1616 	int		i;
   1617 
   1618 	exists = 0;
   1619 	while ((i = getopt(argc, argv, "ev")) != -1) {
   1620 		switch(i) {
   1621 		case 'e':
   1622 			exists = 1;
   1623 			break;
   1624 		case 'v':
   1625 			verbose = 1;
   1626 			break;
   1627 		}
   1628 	}
   1629 	if (argc == optind) {
   1630 		usermgmt_usage("groupinfo");
   1631 	}
   1632 	grp = find_group_info(argv[optind]);
   1633 	if (exists) {
   1634 		exit((grp) ? EXIT_SUCCESS : EXIT_FAILURE);
   1635 	}
   1636 	if (grp == (struct group *) NULL) {
   1637 		errx(EXIT_FAILURE, "can't find group `%s'", argv[optind]);
   1638 	}
   1639 	(void) printf("name\t%s\n", grp->gr_name);
   1640 	(void) printf("passwd\t%s\n", grp->gr_passwd);
   1641 	(void) printf("gid\t%d\n", grp->gr_gid);
   1642 	(void) printf("members\t");
   1643 	for (cpp = grp->gr_mem ; *cpp ; cpp++) {
   1644 		(void) printf("%s ", *cpp);
   1645 	}
   1646 	(void) fputc('\n', stdout);
   1647 	return EXIT_SUCCESS;
   1648 }
   1649 #endif
   1650