Home | History | Annotate | Line # | Download | only in user
user.c revision 1.7
      1 /* $NetBSD: user.c,v 1.7 1999/12/08 21:45:54 lukem 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 static int	verbose;
     55 
     56 static int	useradd __P((int, char **));
     57 static int	usermod __P((int, char **));
     58 static int	userdel __P((int, char **));
     59 static int	groupadd __P((int, char **));
     60 static int	groupdel __P((int, char **));
     61 static int	groupmod __P((int, char **));
     62 
     63 /* if *cpp is non-null, free it, then assign `n' chars of `s' to it */
     64 static void
     65 memsave(char **cpp, char *s, size_t n)
     66 {
     67 	if (*cpp != (char *) NULL) {
     68 		FREE(*cpp);
     69 	}
     70 	NEWARRAY(char, *cpp, n + 1, exit(1));
     71 	(void) memcpy(*cpp, s, n);
     72 	(*cpp)[n] = 0;
     73 }
     74 
     75 /* a replacement for system(3) */
     76 static int
     77 asystem(char *fmt, ...)
     78 {
     79 	va_list	vp;
     80 	char	buf[MaxCommandLen];
     81 	int	ret;
     82 
     83 	va_start(vp, fmt);
     84 	(void) vsnprintf(buf, sizeof(buf), fmt, vp);
     85 	va_end(vp);
     86 	if (verbose) {
     87 		(void) printf("Command: %s\n", buf);
     88 	}
     89 	if ((ret = system(buf)) != 0) {
     90 		warnx("[Warning] can't system `%s'", buf);
     91 	}
     92 	return ret;
     93 }
     94 
     95 #if (__NetBSD_Version__ < 104110000)
     96 /*
     97  * strlcpy() can only be used in 1.4K and later, use this for
     98  * systems older than that.
     99  */
    100 /* bounds checking strncpy */
    101 static char *
    102 strnncpy(char *to, size_t tosize, char *from, size_t fromsize)
    103 {
    104 	size_t	n = MIN(tosize - 1, fromsize);
    105 
    106 	(void) memcpy(to, from, n);
    107 	to[n] = 0;
    108 	return to;
    109 }
    110 #endif /* __NetBSD_Version__ < 105000000 */
    111 
    112 /* copy any dot files into the user's home directory */
    113 static int
    114 copydotfiles(char *skeldir, int uid, int gid, char *dir)
    115 {
    116 	struct dirent	*dp;
    117 	DIR		*dirp;
    118 	int		n;
    119 
    120 	if ((dirp = opendir(skeldir)) == (DIR *) NULL) {
    121 		warn("can't open source . files dir `%s'", skeldir);
    122 		return 0;
    123 	}
    124 	for (n = 0; (dp = readdir(dirp)) != (struct dirent *) NULL && n == 0 ; ) {
    125 		if (strcmp(dp->d_name, ".") == 0 ||
    126 		    strcmp(dp->d_name, "..") == 0) {
    127 			continue;
    128 		}
    129 		if (dp->d_name[0] == '.' && isalnum(dp->d_name[1])) {
    130 			n = 1;
    131 		}
    132 	}
    133 	(void) closedir(dirp);
    134 	if (n == 0) {
    135 		warnx("No \"dot\" initialisation files found");
    136 	} else {
    137 		(void) asystem("%s -p -R %s/.[A-z]* %s", CP, skeldir, dir);
    138 	}
    139 	(void) asystem("%s -R %d:%d %s", CHOWN, uid, gid, dir);
    140 	return n;
    141 }
    142 
    143 /* create a group entry with gid `gid' */
    144 static int
    145 creategid(char *group, int gid, char *name)
    146 {
    147 	struct stat	st;
    148 	FILE		*from;
    149 	FILE		*to;
    150 	char		buf[MaxEntryLen];
    151 	char		f[MaxFileNameLen];
    152 	int		fd;
    153 	int		cc;
    154 
    155 	if ((from = fopen(ETCGROUP, "r")) == (FILE *) NULL) {
    156 		warn("can't create gid for %s: can't open %s", name, ETCGROUP);
    157 		return 0;
    158 	}
    159 	if (flock(fileno(from), LOCK_EX | LOCK_NB) < 0) {
    160 		warn("can't lock `%s'", ETCGROUP);
    161 	}
    162 	(void) fstat(fileno(from), &st);
    163 	(void) snprintf(f, sizeof(f), "%s.XXXXXX", ETCGROUP);
    164 	if ((fd = mkstemp(f)) < 0) {
    165 		(void) fclose(from);
    166 		warn("can't create gid: mkstemp failed");
    167 		return 0;
    168 	}
    169 	if ((to = fdopen(fd, "w")) == (FILE *) NULL) {
    170 		(void) fclose(from);
    171 		(void) close(fd);
    172 		(void) unlink(f);
    173 		warn("can't create gid: fdopen `%s' failed", f);
    174 		return 0;
    175 	}
    176 	while ((cc = fread(buf, sizeof(char), sizeof(buf), from)) > 0) {
    177 		if (fwrite(buf, sizeof(char), (unsigned) cc, to) != cc) {
    178 			(void) fclose(from);
    179 			(void) close(fd);
    180 			(void) unlink(f);
    181 			warn("can't create gid: short write to `%s'", f);
    182 			return 0;
    183 		}
    184 	}
    185 	(void) fprintf(to, "%s:*:%d:%s\n", group, gid, name);
    186 	(void) fclose(from);
    187 	(void) fclose(to);
    188 	if (rename(f, ETCGROUP) < 0) {
    189 		warn("can't create gid: can't rename `%s' to `%s'", f, ETCGROUP);
    190 		return 0;
    191 	}
    192 	(void) chmod(ETCGROUP, st.st_mode & 07777);
    193 	return 1;
    194 }
    195 
    196 /* modify the group entry with name `group' to be newent */
    197 static int
    198 modify_gid(char *group, char *newent)
    199 {
    200 	struct stat	st;
    201 	FILE		*from;
    202 	FILE		*to;
    203 	char		buf[MaxEntryLen];
    204 	char		f[MaxFileNameLen];
    205 	char		*colon;
    206 	int		groupc;
    207 	int		entc;
    208 	int		fd;
    209 	int		cc;
    210 
    211 	if ((from = fopen(ETCGROUP, "r")) == (FILE *) NULL) {
    212 		warn("can't create gid for %s: can't open %s", group, ETCGROUP);
    213 		return 0;
    214 	}
    215 	if (flock(fileno(from), LOCK_EX | LOCK_NB) < 0) {
    216 		warn("can't lock `%s'", ETCGROUP);
    217 	}
    218 	(void) fstat(fileno(from), &st);
    219 	(void) snprintf(f, sizeof(f), "%s.XXXXXX", ETCGROUP);
    220 	if ((fd = mkstemp(f)) < 0) {
    221 		(void) fclose(from);
    222 		warn("can't create gid: mkstemp failed");
    223 		return 0;
    224 	}
    225 	if ((to = fdopen(fd, "w")) == (FILE *) NULL) {
    226 		(void) fclose(from);
    227 		(void) close(fd);
    228 		(void) unlink(f);
    229 		warn("can't create gid: fdopen `%s' failed", f);
    230 		return 0;
    231 	}
    232 	groupc = strlen(group);
    233 	while ((cc = fread(buf, sizeof(char), sizeof(buf), from)) > 0) {
    234 		if ((colon = strchr(buf, ':')) == (char *) NULL) {
    235 			warn("badly formed entry `%s'", buf);
    236 			continue;
    237 		}
    238 		entc = (int)(colon - buf);
    239 		if (entc == groupc && strncmp(group, buf, (unsigned) entc) == 0) {
    240 			if (newent == (char *) NULL) {
    241 				continue;
    242 			} else {
    243 				cc = strlen(newent);
    244 #if (__NetBSD_Version__ < 104110000)
    245 				(void) strnncpy(buf, sizeof(buf), newent, (unsigned) cc);
    246 #else
    247 				(void) strlcpy(buf, newent, sizeof(buf));
    248 #endif /* __NetBSD_Version__ < 104110000 */
    249 			}
    250 		}
    251 		if (fwrite(buf, sizeof(char), (unsigned) cc, to) != cc) {
    252 			(void) fclose(from);
    253 			(void) close(fd);
    254 			(void) unlink(f);
    255 			warn("can't create gid: short write to `%s'", f);
    256 			return 0;
    257 		}
    258 	}
    259 	(void) fclose(from);
    260 	(void) fclose(to);
    261 	if (rename(f, ETCGROUP) < 0) {
    262 		warn("can't create gid: can't rename `%s' to `%s'", f, ETCGROUP);
    263 		return 0;
    264 	}
    265 	(void) chmod(ETCGROUP, st.st_mode & 07777);
    266 	return 1;
    267 }
    268 
    269 /* return 1 if `login' is a valid login name */
    270 static int
    271 valid_login(char *login)
    272 {
    273 	char	*cp;
    274 
    275 	for (cp = login ; *cp ; cp++) {
    276 		if (!isalnum(*cp) && *cp != '.' && *cp != '_' && *cp != '-') {
    277 			return 0;
    278 		}
    279 	}
    280 	return 1;
    281 }
    282 
    283 /* return 1 if `group' is a valid group name */
    284 static int
    285 valid_group(char *group)
    286 {
    287 	char	*cp;
    288 
    289 	for (cp = group ; *cp ; cp++) {
    290 		if (!isalnum(*cp)) {
    291 			return 0;
    292 		}
    293 	}
    294 	return 1;
    295 }
    296 
    297 /* find the next gid in the range lo .. hi */
    298 static int
    299 getnextgid(int *gidp, int lo, int hi)
    300 {
    301 	for (*gidp = lo ; *gidp < hi ; *gidp += 1) {
    302 		if (getgrgid((gid_t)*gidp) == (struct group *) NULL) {
    303 			return 1;
    304 		}
    305 	}
    306 	return 0;
    307 }
    308 
    309 #ifdef EXTENSIONS
    310 /* save a range of uids */
    311 static int
    312 save_range(user_t *up, char *cp)
    313 {
    314 	int	from;
    315 	int	to;
    316 	int	i;
    317 
    318 	if (up->u_rsize == 0) {
    319 		up->u_rsize = 32;
    320 		NEWARRAY(range_t, up->u_rv, up->u_rsize, return(0));
    321 	} else if (up->u_rc == up->u_rsize) {
    322 		up->u_rsize *= 2;
    323 		RENEW(range_t, up->u_rv, up->u_rsize, return(0));
    324 	}
    325 	if (up->u_rv && sscanf(cp, "%d..%d", &from, &to) == 2) {
    326 		for (i = 0 ; i < up->u_rc ; i++) {
    327 			if (up->u_rv[i].r_from == from && up->u_rv[i].r_to == to) {
    328 				break;
    329 			}
    330 		}
    331 		if (i == up->u_rc) {
    332 			up->u_rv[up->u_rc].r_from = from;
    333 			up->u_rv[up->u_rc].r_to = to;
    334 			up->u_rc += 1;
    335 		}
    336 	} else {
    337 		warnx("Bad range `%s'", cp);
    338 		return 0;
    339 	}
    340 	return 1;
    341 }
    342 #endif
    343 
    344 /* set the defaults in the defaults file */
    345 static int
    346 setdefaults(user_t *up)
    347 {
    348 	char	template[MaxFileNameLen];
    349 	FILE	*fp;
    350 	int	ret;
    351 	int	fd;
    352 	int	i;
    353 
    354 	(void) snprintf(template, sizeof(template), "%s.XXXXXX", CONFFILE);
    355 	if ((fd = mkstemp(template)) < 0) {
    356 		warnx("can't mkstemp `%s' for writing", CONFFILE);
    357 		return 0;
    358 	}
    359 	if ((fp = fdopen(fd, "w")) == (FILE *) NULL) {
    360 		warn("can't fdopen `%s' for writing", CONFFILE);
    361 		return 0;
    362 	}
    363 	ret = 1;
    364 	if (fprintf(fp, "group\t\t%s\n", up->u_primgrp) <= 0 ||
    365 	    fprintf(fp, "base_dir\t%s\n", up->u_basedir) <= 0 ||
    366 	    fprintf(fp, "skel_dir\t%s\n", up->u_skeldir) <= 0 ||
    367 	    fprintf(fp, "shell\t\t%s\n", up->u_shell) <= 0 ||
    368 	    fprintf(fp, "inactive\t%d\n", up->u_inactive) <= 0 ||
    369 	    fprintf(fp, "expire\t\t%s\n", (up->u_expire == (char *) NULL) ? UNSET_EXPIRY : up->u_expire) <= 0) {
    370 		warn("can't write to `%s'", CONFFILE);
    371 		ret = 0;
    372 	}
    373 #ifdef EXTENSIONS
    374 	for (i = 0 ; i < up->u_rc ; i++) {
    375 		if (fprintf(fp, "range\t\t%d..%d\n", up->u_rv[i].r_from, up->u_rv[i].r_to) <= 0) {
    376 			warn("can't write to `%s'", CONFFILE);
    377 			ret = 0;
    378 		}
    379 	}
    380 #endif
    381 	(void) fclose(fp);
    382 	if (ret) {
    383 		ret = ((rename(template, CONFFILE) == 0) && (chmod(CONFFILE, 0644) == 0));
    384 	}
    385 	return ret;
    386 }
    387 
    388 /* read the defaults file */
    389 static void
    390 read_defaults(user_t *up)
    391 {
    392 	struct stat	st;
    393 	size_t		lineno;
    394 	size_t		len;
    395 	FILE		*fp;
    396 	char		*cp;
    397 	char		*s;
    398 
    399 	memsave(&up->u_primgrp, DEF_GROUP, strlen(DEF_GROUP));
    400 	memsave(&up->u_basedir, DEF_BASEDIR, strlen(DEF_BASEDIR));
    401 	memsave(&up->u_skeldir, DEF_SKELDIR, strlen(DEF_SKELDIR));
    402 	memsave(&up->u_shell, DEF_SHELL, strlen(DEF_SHELL));
    403 	memsave(&up->u_comment, DEF_COMMENT, strlen(DEF_COMMENT));
    404 	up->u_inactive = DEF_INACTIVE;
    405 	up->u_rsize = 16;
    406 	NEWARRAY(range_t, up->u_rv, up->u_rsize, exit(1));
    407 	up->u_rv[up->u_rc].r_from = DEF_LOWUID;
    408 	up->u_rv[up->u_rc].r_to = DEF_HIGHUID;
    409 	up->u_rc += 1;
    410 	up->u_expire = DEF_EXPIRE;
    411 	if ((fp = fopen(CONFFILE, "r")) == (FILE *) NULL) {
    412 		if (stat(CONFFILE, &st) < 0 && !setdefaults(up)) {
    413 			warn("can't create `%s' defaults file", CONFFILE);
    414 		}
    415 		fp = fopen(CONFFILE, "r");
    416 	}
    417 	if (fp != (FILE *) NULL) {
    418 		while ((s = fparseln(fp, &len, &lineno, NULL, 0)) != (char *) NULL) {
    419 			if (strncmp(s, "group", 5) == 0) {
    420 				for (cp = s + 5 ; *cp && isspace(*cp) ; cp++) {
    421 				}
    422 				memsave(&up->u_primgrp, cp, strlen(cp));
    423 			} else if (strncmp(s, "base_dir", 8) == 0) {
    424 				for (cp = s + 8 ; *cp && isspace(*cp) ; cp++) {
    425 				}
    426 				memsave(&up->u_basedir, cp, strlen(cp));
    427 			} else if (strncmp(s, "skel_dir", 8) == 0) {
    428 				for (cp = s + 8 ; *cp && isspace(*cp) ; cp++) {
    429 				}
    430 				memsave(&up->u_skeldir, cp, strlen(cp));
    431 			} else if (strncmp(s, "shell", 5) == 0) {
    432 				for (cp = s + 5 ; *cp && isspace(*cp) ; cp++) {
    433 				}
    434 				memsave(&up->u_shell, cp, strlen(cp));
    435 			} else if (strncmp(s, "inactive", 8) == 0) {
    436 				for (cp = s + 8 ; *cp && isspace(*cp) ; cp++) {
    437 				}
    438 				up->u_inactive = atoi(cp);
    439 #ifdef EXTENSIONS
    440 			} else if (strncmp(s, "range", 5) == 0) {
    441 				for (cp = s + 5 ; *cp && isspace(*cp) ; cp++) {
    442 				}
    443 				(void) save_range(up, cp);
    444 #endif
    445 #ifdef EXTENSIONS
    446 			} else if (strncmp(s, "preserve", 8) == 0) {
    447 				for (cp = s + 8 ; *cp && isspace(*cp) ; cp++) {
    448 				}
    449 				up->u_preserve = (strncmp(cp, "true", 4) == 0) ? 1 :
    450 						  (strncmp(cp, "yes", 3) == 0) ? 1 :
    451 						   atoi(cp);
    452 #endif
    453 			} else if (strncmp(s, "expire", 6) == 0) {
    454 				for (cp = s + 6 ; *cp && isspace(*cp) ; cp++) {
    455 				}
    456 				if (strcmp(cp, UNSET_EXPIRY) == 0) {
    457 					if (up->u_expire) {
    458 						FREE(up->u_expire);
    459 					}
    460 					up->u_expire = (char *) NULL;
    461 				} else {
    462 					memsave(&up->u_expire, cp, strlen(cp));
    463 				}
    464 			}
    465 			(void) free(s);
    466 		}
    467 		(void) fclose(fp);
    468 	}
    469 }
    470 
    471 /* return the next valid unused uid */
    472 static int
    473 getnextuid(int sync_uid_gid, int *uid, int low_uid, int high_uid)
    474 {
    475 	for (*uid = low_uid ; *uid <= high_uid ; (*uid)++) {
    476 		if (getpwuid((uid_t)(*uid)) == (struct passwd *) NULL && *uid != NOBODY_UID) {
    477 			if (sync_uid_gid) {
    478 				if (getgrgid((gid_t)(*uid)) == (struct group *) NULL) {
    479 					return 1;
    480 				}
    481 			} else {
    482 				return 1;
    483 			}
    484 		}
    485 	}
    486 	return 0;
    487 }
    488 
    489 /* add a user */
    490 static int
    491 adduser(char *login, user_t *up)
    492 {
    493 	struct group	*grp;
    494 	struct stat	st;
    495 	struct tm	tm;
    496 	time_t		expire;
    497 	char		password[PasswordLength + 1];
    498 	char		home[MaxFileNameLen];
    499 	char		buf[MaxFileNameLen];
    500 	int		sync_uid_gid;
    501 	int		masterfd;
    502 	int		ptmpfd;
    503 	int		gid;
    504 	int		cc;
    505 	int		i;
    506 
    507 	if (!valid_login(login)) {
    508 		errx(EXIT_FAILURE, "`%s' is not a valid login name", login);
    509 	}
    510 	if ((masterfd = open(MASTER, O_RDONLY)) < 0) {
    511 		err(EXIT_FAILURE, "can't open `%s'", MASTER);
    512 	}
    513 	if (flock(masterfd, LOCK_EX | LOCK_NB) < 0) {
    514 		err(EXIT_FAILURE, "can't lock `%s'", MASTER);
    515 	}
    516 	pw_init();
    517 	if ((ptmpfd = pw_lock(WAITSECS)) < 0) {
    518 		(void) close(masterfd);
    519 		err(EXIT_FAILURE, "can't obtain pw_lock");
    520 	}
    521 	while ((cc = read(masterfd, buf, sizeof(buf))) > 0) {
    522 		if (write(ptmpfd, buf, (size_t)(cc)) != cc) {
    523 			(void) close(masterfd);
    524 			(void) close(ptmpfd);
    525 			(void) pw_abort();
    526 			err(EXIT_FAILURE, "short write to /etc/ptmp (not %d chars)", cc);
    527 		}
    528 	}
    529 	/* if no uid was specified, get next one in [low_uid..high_uid] range */
    530 	sync_uid_gid = (strcmp(up->u_primgrp, "=uid") == 0);
    531 	if (up->u_uid == -1) {
    532 		for (i = 0 ; i < up->u_rc ; i++) {
    533 			if (getnextuid(sync_uid_gid, &up->u_uid, up->u_rv[i].r_from, up->u_rv[i].r_to)) {
    534 				break;
    535 			}
    536 		}
    537 		if (i == up->u_rc) {
    538 			(void) close(ptmpfd);
    539 			(void) pw_abort();
    540 			errx(EXIT_FAILURE, "can't get next uid for %d", up->u_uid);
    541 		}
    542 	}
    543 	/* check uid isn't already allocated */
    544 	if (!up->u_dupuid && getpwuid((uid_t)(up->u_uid)) != (struct passwd *) NULL) {
    545 		(void) close(ptmpfd);
    546 		(void) pw_abort();
    547 		errx(EXIT_FAILURE, "uid %d is already in use", up->u_uid);
    548 	}
    549 	/* if -g=uid was specified, check gid is unused */
    550 	if (sync_uid_gid) {
    551 		if (getgrgid((gid_t)(up->u_uid)) != (struct group *) NULL) {
    552 			(void) close(ptmpfd);
    553 			(void) pw_abort();
    554 			errx(EXIT_FAILURE, "gid %d is already in use", up->u_uid);
    555 		}
    556 		gid = up->u_uid;
    557 	} else if ((grp = getgrnam(up->u_primgrp)) != (struct group *) NULL) {
    558 		gid = grp->gr_gid;
    559 	} else if ((grp = getgrgid((gid_t)atoi(up->u_primgrp))) != (struct group *) NULL) {
    560 		gid = grp->gr_gid;
    561 	} else {
    562 		(void) close(ptmpfd);
    563 		(void) pw_abort();
    564 		errx(EXIT_FAILURE, "group %s not found", up->u_primgrp);
    565 	}
    566 	/* check name isn't already in use */
    567 	if (!up->u_dupuid && getpwnam(login) != (struct passwd *) NULL) {
    568 		(void) close(ptmpfd);
    569 		(void) pw_abort();
    570 		errx(EXIT_FAILURE, "already a `%s' user", login);
    571 	}
    572 	/* if home directory hasn't been given, make it up */
    573 	if (!up->u_homeset) {
    574 		(void) snprintf(home, sizeof(home), "%s/%s", up->u_basedir, login);
    575 	}
    576 	expire = 0;
    577 	if (up->u_expire != (char *) NULL) {
    578 		(void) memset(&tm, 0, sizeof(tm));
    579 		if (strptime(up->u_expire, "%c", &tm) == (char *) NULL) {
    580 			warnx("invalid time format `%s'", optarg);
    581 		} else {
    582 			expire = mktime(&tm);
    583 		}
    584 	}
    585 	password[PasswordLength] = 0;
    586 	if (up->u_password != (char *) NULL &&
    587 	    strlen(up->u_password) == PasswordLength) {
    588 		(void) memcpy(password, up->u_password, PasswordLength);
    589 	} else {
    590 		(void) memset(password, '*', PasswordLength);
    591 		if (up->u_password != (char *) NULL) {
    592 			warnx("Password `%s' is invalid: setting it to `%s'",
    593 			    password, "*");
    594 		}
    595 	}
    596 	cc = snprintf(buf, sizeof(buf), "%s:%s:%d:%d::%d:%ld:%s:%s:%s\n",
    597 			login,
    598 			password,
    599 			up->u_uid,
    600 			gid,
    601 			up->u_inactive,
    602 			(long)expire,
    603 			up->u_comment,
    604 			home,
    605 			up->u_shell);
    606 	if (write(ptmpfd, buf, (size_t) cc) != cc) {
    607 		(void) close(ptmpfd);
    608 		(void) pw_abort();
    609 		err(EXIT_FAILURE, "can't add `%s'", buf);
    610 	}
    611 	if (up->u_mkdir) {
    612 		if (lstat(home, &st) < 0 && asystem("%s -p %s", MKDIR, home) != 0) {
    613 			(void) close(ptmpfd);
    614 			(void) pw_abort();
    615 			err(EXIT_FAILURE, "can't mkdir `%s'", home);
    616 		}
    617 		(void) copydotfiles(up->u_skeldir, up->u_uid, gid, home);
    618 	}
    619 	if (strcmp(up->u_primgrp, "=uid") == 0 &&
    620 	    getgrnam(login) == (struct group *) NULL &&
    621 	    !creategid(login, gid, login)) {
    622 		(void) close(ptmpfd);
    623 		(void) pw_abort();
    624 		err(EXIT_FAILURE, "can't create gid %d for login name %s", gid, login);
    625 	}
    626 	(void) close(ptmpfd);
    627 	if (pw_mkdb() < 0) {
    628 		err(EXIT_FAILURE, "pw_mkdb failed");
    629 	}
    630 	return 1;
    631 }
    632 
    633 /* modify a user */
    634 static int
    635 moduser(char *login, char *newlogin, user_t *up)
    636 {
    637 	struct passwd	*pwp;
    638 	struct group	*grp;
    639 	struct tm	tm;
    640 	time_t		expire;
    641 	size_t		loginc;
    642 	size_t		colonc;
    643 	FILE		*master;
    644 	char		password[PasswordLength + 1];
    645 	char		oldhome[MaxFileNameLen];
    646 	char		home[MaxFileNameLen];
    647 	char		buf[MaxFileNameLen];
    648 	char		*colon;
    649 	int		masterfd;
    650 	int		ptmpfd;
    651 	int		gid;
    652 	int		cc;
    653 
    654 	if (!valid_login(newlogin)) {
    655 		errx(EXIT_FAILURE, "`%s' is not a valid login name", login);
    656 	}
    657 	if ((pwp = getpwnam(login)) == (struct passwd *) NULL) {
    658 		err(EXIT_FAILURE, "No such user `%s'", login);
    659 	}
    660 	if ((masterfd = open(MASTER, O_RDONLY)) < 0) {
    661 		err(EXIT_FAILURE, "can't open `%s'", MASTER);
    662 	}
    663 	if (flock(masterfd, LOCK_EX | LOCK_NB) < 0) {
    664 		err(EXIT_FAILURE, "can't lock `%s'", MASTER);
    665 	}
    666 	pw_init();
    667 	if ((ptmpfd = pw_lock(WAITSECS)) < 0) {
    668 		(void) close(masterfd);
    669 		err(EXIT_FAILURE, "can't obtain pw_lock");
    670 	}
    671 	if ((master = fdopen(masterfd, "r")) == (FILE *) NULL) {
    672 		(void) close(masterfd);
    673 		(void) close(ptmpfd);
    674 		(void) pw_abort();
    675 		err(EXIT_FAILURE, "can't fdopen fd for %s", MASTER);
    676 	}
    677 	if (up != (user_t *) NULL) {
    678 		if (up->u_mkdir) {
    679 			(void) strcpy(oldhome, pwp->pw_dir);
    680 		}
    681 		if (up->u_uid == -1) {
    682 			up->u_uid = pwp->pw_uid;
    683 		}
    684 		/* if -g=uid was specified, check gid is unused */
    685 		if (strcmp(up->u_primgrp, "=uid") == 0) {
    686 			if (getgrgid((gid_t)(up->u_uid)) != (struct group *) NULL) {
    687 				(void) close(ptmpfd);
    688 				(void) pw_abort();
    689 				errx(EXIT_FAILURE, "gid %d is already in use", up->u_uid);
    690 			}
    691 			gid = up->u_uid;
    692 		} else if ((grp = getgrnam(up->u_primgrp)) != (struct group *) NULL) {
    693 			gid = grp->gr_gid;
    694 		} else if ((grp = getgrgid((gid_t)atoi(up->u_primgrp))) != (struct group *) NULL) {
    695 			gid = grp->gr_gid;
    696 		} else {
    697 			(void) close(ptmpfd);
    698 			(void) pw_abort();
    699 			errx(EXIT_FAILURE, "group %s not found", up->u_primgrp);
    700 		}
    701 		/* if changing name, check new name isn't already in use */
    702 		if (strcmp(login, newlogin) != 0 && getpwnam(newlogin) != (struct passwd *) NULL) {
    703 			(void) close(ptmpfd);
    704 			(void) pw_abort();
    705 			errx(EXIT_FAILURE, "already a `%s' user", newlogin);
    706 		}
    707 		/* if home directory hasn't been given, use the old one */
    708 		if (!up->u_homeset) {
    709 			(void) strcpy(home, pwp->pw_dir);
    710 		}
    711 		expire = 0;
    712 		if (up->u_expire != (char *) NULL) {
    713 			(void) memset(&tm, 0, sizeof(tm));
    714 			if (strptime(up->u_expire, "%c", &tm) == (char *) NULL) {
    715 				warnx("invalid time format `%s'", optarg);
    716 			} else {
    717 				expire = mktime(&tm);
    718 			}
    719 		}
    720 		password[PasswordLength] = 0;
    721 		if (up->u_password != (char *) NULL &&
    722 		    strlen(up->u_password) == PasswordLength) {
    723 			(void) memcpy(password, up->u_password, PasswordLength);
    724 		} else {
    725 			(void) memcpy(password, pwp->pw_passwd, PasswordLength);
    726 		}
    727 		if (strcmp(up->u_comment, DEF_COMMENT) == 0) {
    728 			memsave(&up->u_comment, pwp->pw_gecos, strlen(pwp->pw_gecos));
    729 		}
    730 		if (strcmp(up->u_shell, DEF_SHELL) == 0 && strcmp(pwp->pw_shell, DEF_SHELL) != 0) {
    731 			memsave(&up->u_comment, pwp->pw_shell, strlen(pwp->pw_shell));
    732 		}
    733 	}
    734 	loginc = strlen(login);
    735 	while (fgets(buf, sizeof(buf), master) != (char *) NULL) {
    736 		cc = strlen(buf);
    737 		if ((colon = strchr(buf, ':')) == (char *) NULL) {
    738 			warnx("Malformed entry `%s'. Skipping", buf);
    739 			continue;
    740 		}
    741 		colonc = (size_t)(colon - buf);
    742 		if (strncmp(login, buf, loginc) == 0 && loginc == colonc) {
    743 			if (up != (user_t *) NULL) {
    744 				cc = snprintf(buf, sizeof(buf), "%s:%s:%d:%d::%d:%ld:%s:%s:%s\n",
    745 					newlogin,
    746 					password,
    747 					up->u_uid,
    748 					gid,
    749 					up->u_inactive,
    750 					(long)expire,
    751 					up->u_comment,
    752 					home,
    753 					up->u_shell);
    754 				if (write(ptmpfd, buf, (size_t) cc) != cc) {
    755 					(void) close(ptmpfd);
    756 					(void) pw_abort();
    757 					err(EXIT_FAILURE, "can't add `%s'", buf);
    758 				}
    759 			}
    760 		} else if (write(ptmpfd, buf, (size_t)(cc)) != cc) {
    761 			(void) close(masterfd);
    762 			(void) close(ptmpfd);
    763 			(void) pw_abort();
    764 			err(EXIT_FAILURE, "short write to /etc/ptmp (not %d chars)", cc);
    765 		}
    766 	}
    767 	if (up != (user_t *) NULL &&
    768 	    up->u_mkdir &&
    769 	    asystem("%s %s %s", MV, oldhome, home) != 0) {
    770 		(void) close(ptmpfd);
    771 		(void) pw_abort();
    772 		err(EXIT_FAILURE, "can't move `%s' to `%s'", oldhome, home);
    773 	}
    774 	(void) close(ptmpfd);
    775 	if (pw_mkdb() < 0) {
    776 		err(EXIT_FAILURE, "pw_mkdb failed");
    777 	}
    778 	return 1;
    779 }
    780 
    781 /* print out usage message, and then exit */
    782 static void
    783 usage(char *prog)
    784 {
    785 	if (strcmp(prog, "useradd") == 0) {
    786 		(void) fprintf(stderr,
    787 "usage: %s -D [-b basedir] [-e expiry] [-f inactive] [-g group]\n"
    788 "	[-r range] [-sshell]\n", prog);
    789 		(void) fprintf(stderr,
    790 "usage: %s [-G group] [-b basedir] [-c comment] [-d homedir] [-e expiry]\n"
    791 "	[-f inactive] [-g group] [-k skeletondir] [-m] [-o] [-p password]\n"
    792 "	[-r range] [-s shell] [-u uid] [-v] user\n", prog);
    793 
    794 	} else if (strcmp(prog, "userdel") == 0) {
    795 		(void) fprintf(stderr,
    796 "usage: %s -D [-p preserve]\n", prog);
    797 		(void) fprintf(stderr,
    798 "usage: %s [-p preserve] [-r] [-v] user\n", prog);
    799 
    800 	} else if (strcmp(prog, "usermod") == 0) {
    801 		(void) fprintf(stderr,
    802 "usage: %s [-G group] [-c comment] [-d homedir] [-e expire] [-f inactive]\n"
    803 "	[-g group] [-l newname] [-m] [-o] [-p password] [-s shell] [-u uid]\n"
    804 "	[-v] user\n", prog);
    805 
    806 	} else if (strcmp(prog, "groupadd") == 0) {
    807 		(void) fprintf(stderr,
    808 "usage: %s [-g gid] [-o] [-v] group\n", prog);
    809 
    810 	} else if (strcmp(prog, "groupdel") == 0) {
    811 		(void) fprintf(stderr,
    812 "usage: %s [-v] group\n", prog);
    813 
    814 	} else if (strcmp(prog, "groupmod") == 0) {
    815 		(void) fprintf(stderr,
    816 "usage: %s [-g gid] [-o] [-n newname] [-v] group\n", prog);
    817 
    818 	} else if ((strcmp(prog, "user") == 0) || (strcmp(prog, "group") == 0)) {
    819 		(void) fprintf(stderr,
    820 "usage: %s ( add | del | mod ) ...\n", prog);
    821 
    822 	} else {
    823 		warn("usage() called with unknown prog `%s'", prog);
    824 	}
    825 	exit(EXIT_FAILURE);
    826 	/* NOTREACHED */
    827 }
    828 
    829 extern int	optind;
    830 extern char	*optarg;
    831 
    832 #ifdef EXTENSIONS
    833 #define ADD_OPT_EXTENSIONS	"p:r:v"
    834 #else
    835 #define ADD_OPT_EXTENSIONS
    836 #endif
    837 
    838 static int
    839 useradd(int argc, char **argv)
    840 {
    841 	user_t	u;
    842 	int	defaultfield;
    843 	int	bigD;
    844 	int	c;
    845 	int	i;
    846 
    847 	(void) memset(&u, 0, sizeof(u));
    848 	read_defaults(&u);
    849 	u.u_uid = -1;
    850 	defaultfield = bigD = 0;
    851 	while ((c = getopt(argc, argv, "DG:b:c:d:e:f:g:k:mou:s:" ADD_OPT_EXTENSIONS)) != -1) {
    852 		switch(c) {
    853 		case 'D':
    854 			bigD = 1;
    855 			break;
    856 		case 'G':
    857 			memsave(&u.u_groupv[u.u_groupc++], optarg, strlen(optarg));
    858 			break;
    859 		case 'b':
    860 			defaultfield = 1;
    861 			memsave(&u.u_basedir, optarg, strlen(optarg));
    862 			break;
    863 		case 'c':
    864 			memsave(&u.u_comment, optarg, strlen(optarg));
    865 			break;
    866 		case 'd':
    867 			u.u_homeset = 1;
    868 			memsave(&u.u_home, optarg, strlen(optarg));
    869 			break;
    870 		case 'e':
    871 			defaultfield = 1;
    872 			memsave(&u.u_expire, optarg, strlen(optarg));
    873 			break;
    874 		case 'f':
    875 			defaultfield = 1;
    876 			u.u_inactive = atoi(optarg);
    877 			break;
    878 		case 'g':
    879 			defaultfield = 1;
    880 			memsave(&u.u_primgrp, optarg, strlen(optarg));
    881 			break;
    882 		case 'k':
    883 			memsave(&u.u_skeldir, optarg, strlen(optarg));
    884 			break;
    885 		case 'm':
    886 			u.u_mkdir = 1;
    887 			break;
    888 		case 'o':
    889 			u.u_dupuid = 1;
    890 			break;
    891 #ifdef EXTENSIONS
    892 		case 'p':
    893 			memsave(&u.u_password, optarg, strlen(optarg));
    894 			break;
    895 #endif
    896 #ifdef EXTENSIONS
    897 		case 'r':
    898 			defaultfield = 1;
    899 			(void) save_range(&u, optarg);
    900 			break;
    901 #endif
    902 		case 's':
    903 			defaultfield = 1;
    904 			memsave(&u.u_shell, optarg, strlen(optarg));
    905 			break;
    906 		case 'u':
    907 			u.u_uid = atoi(optarg);
    908 			break;
    909 #ifdef EXTENSIONS
    910 		case 'v':
    911 			verbose = 1;
    912 			break;
    913 #endif
    914 		}
    915 	}
    916 	if (bigD) {
    917 		if (defaultfield) {
    918 			return setdefaults(&u) ? EXIT_SUCCESS : EXIT_FAILURE;
    919 		}
    920 		(void) printf("group\t\t%s\n", u.u_primgrp);
    921 		(void) printf("base_dir\t%s\n", u.u_basedir);
    922 		(void) printf("skel_dir\t%s\n", u.u_skeldir);
    923 		(void) printf("shell\t\t%s\n", u.u_shell);
    924 		(void) printf("inactive\t%d\n", u.u_inactive);
    925 		(void) printf("expire\t\t%s\n", (u.u_expire == (char *) NULL) ? UNSET_EXPIRY : u.u_expire);
    926 #ifdef EXTENSIONS
    927 		for (i = 0 ; i < u.u_rc ; i++) {
    928 			(void) printf("range\t\t%d..%d\n", u.u_rv[i].r_from, u.u_rv[i].r_to);
    929 		}
    930 #endif
    931 		return EXIT_SUCCESS;
    932 	}
    933 	if (argc == optind) {
    934 		usage("useradd");
    935 	}
    936 	return adduser(argv[optind], &u) ? EXIT_SUCCESS : EXIT_FAILURE;
    937 }
    938 
    939 #ifdef EXTENSIONS
    940 #define MOD_OPT_EXTENSIONS	"p:v"
    941 #else
    942 #define MOD_OPT_EXTENSIONS
    943 #endif
    944 
    945 static int
    946 usermod(int argc, char **argv)
    947 {
    948 	user_t	u;
    949 	char	newuser[MaxUserNameLen + 1];
    950 	int	have_new_user;
    951 	int	c;
    952 
    953 	(void) memset(&u, 0, sizeof(u));
    954 	(void) memset(newuser, 0, sizeof(newuser));
    955 	read_defaults(&u);
    956 	u.u_uid = -1;
    957 	have_new_user = 0;
    958 	while ((c = getopt(argc, argv, "G:c:d:e:f:g:l:mos:u:" MOD_OPT_EXTENSIONS)) != -1) {
    959 		switch(c) {
    960 		case 'G':
    961 			memsave(&u.u_groupv[u.u_groupc++], optarg, strlen(optarg));
    962 			break;
    963 		case 'c':
    964 			memsave(&u.u_comment, optarg, strlen(optarg));
    965 			break;
    966 		case 'd':
    967 			u.u_homeset = 1;
    968 			memsave(&u.u_home, optarg, strlen(optarg));
    969 			break;
    970 		case 'e':
    971 			memsave(&u.u_expire, optarg, strlen(optarg));
    972 			break;
    973 		case 'f':
    974 			u.u_inactive = atoi(optarg);
    975 			break;
    976 		case 'g':
    977 			memsave(&u.u_primgrp, optarg, strlen(optarg));
    978 			break;
    979 		case 'l':
    980 			have_new_user = 1;
    981 #if (__NetBSD_Version__ < 104110000)
    982 			(void) strnncpy(newuser, sizeof(newuser), optarg, strlen(optarg));
    983 #else
    984 			(void) strlcpy(newuser, optarg, sizeof(newuser));
    985 #endif /* __NetBSD_Version__ < 104110000 */
    986 			break;
    987 		case 'm':
    988 			u.u_mkdir = 1;
    989 			break;
    990 		case 'o':
    991 			u.u_dupuid = 1;
    992 			break;
    993 #ifdef EXTENSIONS
    994 		case 'p':
    995 			memsave(&u.u_password, optarg, strlen(optarg));
    996 			break;
    997 #endif
    998 		case 's':
    999 			memsave(&u.u_shell, optarg, strlen(optarg));
   1000 			break;
   1001 		case 'u':
   1002 			u.u_uid = atoi(optarg);
   1003 			break;
   1004 #ifdef EXTENSIONS
   1005 		case 'v':
   1006 			verbose = 1;
   1007 			break;
   1008 #endif
   1009 		}
   1010 	}
   1011 	if (argc == optind) {
   1012 		usage("usermod");
   1013 	}
   1014 	return moduser(argv[optind], (have_new_user) ? newuser : argv[optind], &u) ? EXIT_SUCCESS : EXIT_FAILURE;
   1015 }
   1016 
   1017 #ifdef EXTENSIONS
   1018 #define DEL_OPT_EXTENSIONS	"Dp:v"
   1019 #else
   1020 #define DEL_OPT_EXTENSIONS
   1021 #endif
   1022 
   1023 static int
   1024 userdel(int argc, char **argv)
   1025 {
   1026 	struct passwd	*pwp;
   1027 	struct stat	st;
   1028 	user_t		u;
   1029 	char		password[PasswordLength + 1];
   1030 	int		defaultfield;
   1031 	int		rmhome;
   1032 	int		bigD;
   1033 	int		c;
   1034 
   1035 	(void) memset(&u, 0, sizeof(u));
   1036 	read_defaults(&u);
   1037 	defaultfield = bigD = rmhome = 0;
   1038 	while ((c = getopt(argc, argv, "r" DEL_OPT_EXTENSIONS)) != -1) {
   1039 		switch(c) {
   1040 #ifdef EXTENSIONS
   1041 		case 'D':
   1042 			bigD = 1;
   1043 			break;
   1044 #endif
   1045 #ifdef EXTENSIONS
   1046 		case 'p':
   1047 			defaultfield = 1;
   1048 			u.u_preserve = (strcmp(optarg, "true") == 0) ? 1 :
   1049 					(strcmp(optarg, "yes") == 0) ? 1 :
   1050 					 atoi(optarg);
   1051 			break;
   1052 #endif
   1053 		case 'r':
   1054 			rmhome = 1;
   1055 			break;
   1056 #ifdef EXTENSIONS
   1057 		case 'v':
   1058 			verbose = 1;
   1059 			break;
   1060 #endif
   1061 		}
   1062 	}
   1063 #ifdef EXTENSIONS
   1064 	if (bigD) {
   1065 		if (defaultfield) {
   1066 			return setdefaults(&u) ? EXIT_SUCCESS : EXIT_FAILURE;
   1067 		}
   1068 		(void) printf("preserve\t%s\n", (u.u_preserve) ? "true" : "false");
   1069 		return EXIT_SUCCESS;
   1070 	}
   1071 #endif
   1072 	if (argc == optind) {
   1073 		usage("userdel");
   1074 	}
   1075 	if ((pwp = getpwnam(argv[optind])) == (struct passwd *) NULL) {
   1076 		warn("No such user `%s'", argv[optind]);
   1077 		return EXIT_FAILURE;
   1078 	}
   1079 	if (rmhome) {
   1080 		if (stat(pwp->pw_dir, &st) < 0) {
   1081 			warn("Home directory `%s' does not exist", pwp->pw_dir);
   1082 			return EXIT_FAILURE;
   1083 		}
   1084 		(void) asystem("%s -rf %s", RM, pwp->pw_dir);
   1085 	}
   1086 	if (u.u_preserve) {
   1087 		memsave(&u.u_shell, FALSE_PROG, strlen(FALSE_PROG));
   1088 		(void) memset(password, '*', PasswordLength);
   1089 		password[PasswordLength] = 0;
   1090 		memsave(&u.u_password, password, PasswordLength);
   1091 		return moduser(argv[optind], argv[optind], &u) ? EXIT_SUCCESS : EXIT_FAILURE;
   1092 	}
   1093 	return moduser(argv[optind], argv[optind], (user_t *) NULL) ? EXIT_SUCCESS : EXIT_FAILURE;
   1094 }
   1095 
   1096 #ifdef EXTENSIONS
   1097 #define GROUP_ADD_OPT_EXTENSIONS	"v"
   1098 #else
   1099 #define GROUP_ADD_OPT_EXTENSIONS
   1100 #endif
   1101 
   1102 /* add a group */
   1103 int
   1104 groupadd(int argc, char **argv)
   1105 {
   1106 	int	dupgid;
   1107 	int	gid;
   1108 	int	c;
   1109 
   1110 	gid = -1;
   1111 	dupgid = 0;
   1112 	while ((c = getopt(argc, argv, "g:o" GROUP_ADD_OPT_EXTENSIONS)) != -1) {
   1113 		switch(c) {
   1114 		case 'g':
   1115 			gid = atoi(optarg);
   1116 			break;
   1117 		case 'o':
   1118 			dupgid = 1;
   1119 			break;
   1120 #ifdef EXTENSIONS
   1121 		case 'v':
   1122 			verbose = 1;
   1123 			break;
   1124 #endif
   1125 		}
   1126 	}
   1127 	if (argc == optind) {
   1128 		usage("groupadd");
   1129 	}
   1130 	if (gid < 0 && !getnextgid(&gid, LowGid, HighGid)) {
   1131 		err(EXIT_FAILURE, "can't add group: can't get next gid");
   1132 	}
   1133 	if (!dupgid && getgrgid((gid_t) gid) != (struct group *) NULL) {
   1134 		err(EXIT_FAILURE, "can't add group: gid %d is a duplicate", gid);
   1135 	}
   1136 	if (!valid_group(argv[optind])) {
   1137 		warn("warning - invalid group name `%s'", argv[optind]);
   1138 	}
   1139 	if (!creategid(argv[optind], gid, "")) {
   1140 		err(EXIT_FAILURE, "can't add group: problems with %s file", ETCGROUP);
   1141 	}
   1142 	return EXIT_SUCCESS;
   1143 }
   1144 
   1145 #ifdef EXTENSIONS
   1146 #define GROUP_DEL_OPT_EXTENSIONS	"v"
   1147 #else
   1148 #define GROUP_DEL_OPT_EXTENSIONS
   1149 #endif
   1150 
   1151 /* remove a group */
   1152 int
   1153 groupdel(int argc, char **argv)
   1154 {
   1155 	int	c;
   1156 
   1157 	while ((c = getopt(argc, argv, "" GROUP_DEL_OPT_EXTENSIONS)) != -1) {
   1158 		switch(c) {
   1159 #ifdef EXTENSIONS
   1160 		case 'v':
   1161 			verbose = 1;
   1162 			break;
   1163 #endif
   1164 		}
   1165 	}
   1166 	if (argc == optind) {
   1167 		usage("groupdel");
   1168 	}
   1169 	if (!modify_gid(argv[optind], (char *) NULL)) {
   1170 		err(EXIT_FAILURE, "can't change %s file", ETCGROUP);
   1171 	}
   1172 	return EXIT_SUCCESS;
   1173 }
   1174 
   1175 #ifdef EXTENSIONS
   1176 #define GROUP_MOD_OPT_EXTENSIONS	"v"
   1177 #else
   1178 #define GROUP_MOD_OPT_EXTENSIONS
   1179 #endif
   1180 
   1181 /* modify a group */
   1182 int
   1183 groupmod(int argc, char **argv)
   1184 {
   1185 	struct group	*grp;
   1186 	char		buf[MaxEntryLen];
   1187 	char		*newname;
   1188 	char		**cpp;
   1189 	int		dupgid;
   1190 	int		gid;
   1191 	int		cc;
   1192 	int		c;
   1193 
   1194 	gid = -1;
   1195 	dupgid = 0;
   1196 	newname = (char *) NULL;
   1197 	while ((c = getopt(argc, argv, "g:on:" GROUP_MOD_OPT_EXTENSIONS)) != -1) {
   1198 		switch(c) {
   1199 		case 'g':
   1200 			gid = atoi(optarg);
   1201 			break;
   1202 		case 'o':
   1203 			dupgid = 1;
   1204 			break;
   1205 		case 'n':
   1206 			memsave(&newname, optarg, strlen(optarg));
   1207 			break;
   1208 #ifdef EXTENSIONS
   1209 		case 'v':
   1210 			verbose = 1;
   1211 			break;
   1212 #endif
   1213 		}
   1214 	}
   1215 	if (argc == optind) {
   1216 		usage("groupmod");
   1217 	}
   1218 	if (gid < 0 && newname == (char *) NULL) {
   1219 		err(EXIT_FAILURE, "Nothing to change");
   1220 	}
   1221 	if (dupgid && gid < 0) {
   1222 		err(EXIT_FAILURE, "Duplicate which gid?");
   1223 	}
   1224 	if ((grp = getgrnam(argv[optind])) == (struct group *) NULL) {
   1225 		err(EXIT_FAILURE, "can't find group `%s' to modify", argv[optind]);
   1226 	}
   1227 	if (newname != (char *) NULL && !valid_group(newname)) {
   1228 		warn("warning - invalid group name `%s'", newname);
   1229 	}
   1230 	cc = snprintf(buf, sizeof(buf), "%s:%s:%d:",
   1231 			(newname) ? newname : grp->gr_name,
   1232 			grp->gr_passwd,
   1233 			(gid < 0) ? grp->gr_gid : gid);
   1234 	for (cpp = grp->gr_mem ; *cpp && cc < sizeof(buf) ; cpp++) {
   1235 		cc += snprintf(&buf[cc], sizeof(buf) - cc, "%s%s", *cpp,
   1236 			(cpp[1] == (char *) NULL) ? "" : ",");
   1237 	}
   1238 	if (!modify_gid(argv[optind], buf)) {
   1239 		err(EXIT_FAILURE, "can't change %s file", ETCGROUP);
   1240 	}
   1241 	return EXIT_SUCCESS;
   1242 }
   1243 
   1244 /* this struct describes a command */
   1245 typedef struct cmd_t {
   1246 	char	*c_progname;	/* program name */
   1247 	char	*c_word1;	/* alternative program name */
   1248 	char	*c_word2;	/* alternative command word */
   1249 	int	(*c_func)(int argc, char **argv);	/* called function */
   1250 } cmd_t;
   1251 
   1252 /* despatch table for commands */
   1253 static cmd_t	cmds[] = {
   1254 	{	"useradd",	"user",		"add",	useradd		},
   1255 	{	"usermod",	"user",		"mod",	usermod		},
   1256 	{	"userdel",	"user",		"del",	userdel		},
   1257 	{	"groupadd",	"group",	"add",	groupadd	},
   1258 	{	"groupmod",	"group",	"mod",	groupmod	},
   1259 	{	"groupdel",	"group",	"del",	groupdel	},
   1260 	{	NULL	}
   1261 };
   1262 
   1263 extern char	*__progname;
   1264 
   1265 int
   1266 main(int argc, char **argv)
   1267 {
   1268 	cmd_t	*cmdp;
   1269 	int	 firstok;
   1270 
   1271 	firstok = 0;
   1272 	for (cmdp = cmds ; cmdp->c_progname ; cmdp++) {
   1273 		if (strcmp(__progname, cmdp->c_progname) == 0) {
   1274 			return (*cmdp->c_func)(argc, argv);
   1275 		}
   1276 		if (strcmp(__progname, cmdp->c_word1) == 0) {
   1277 			firstok = 1;
   1278 			if (argc > 1 && strcmp(argv[1], cmdp->c_word2) == 0)
   1279 				return (*cmdp->c_func)(argc - 1, argv + 1);
   1280 		}
   1281 	}
   1282 	if (firstok)
   1283 		usage(__progname);
   1284 	errx(EXIT_FAILURE, "Program `%s' not recognised", __progname);
   1285 	/* NOTREACHED */
   1286 }
   1287