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