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