Home | History | Annotate | Line # | Download | only in user
user.c revision 1.6
      1 /* $NetBSD: user.c,v 1.6 1999/12/08 18:12:16 hubertf 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, "%s -D [-bbasedir] [-eexpiry] [-finactive] [-ggroup] [-rrange] [-sshell]\n", prog);
    787 		(void) fprintf(stderr, "%s [-Ggroup] [-bbasedir] [-ccomment] [-dhomedir] [-eexpiry] [-finactive]\n\t[-ggroup] [-kskeletondir] [-m] [-o] [-ppassword] [-rrange] [-sshell]\n\t[-uuid] [-v] user\n", prog);
    788 	} else if (strcmp(prog, "usermod") == 0) {
    789 		(void) fprintf(stderr, "%s [-Ggroup] [-ccomment] [-dhomedir] [-eexpire] [-finactive] [-ggroup] [-lnewname] [-m] [-o] [-ppassword] [-sshell] [-uuid] [-v] user\n", prog);
    790 	} else if (strcmp(prog, "userdel") == 0) {
    791 		(void) fprintf(stderr, "%s -D [-ppreserve]\n", prog);
    792 		(void) fprintf(stderr, "%s [-ppreserve] [-r] [-v] user\n", prog);
    793 	} else if (strcmp(prog, "groupadd") == 0) {
    794 		(void) fprintf(stderr, "%s [-ggid] [-o] [-v] group\n", prog);
    795 	} else if (strcmp(prog, "groupdel") == 0) {
    796 		(void) fprintf(stderr, "%s [-v] group\n", prog);
    797 	} else if (strcmp(prog, "groupmod") == 0) {
    798 		(void) fprintf(stderr, "%s [-ggid] [-o] [-nnewname] [-v] group\n", prog);
    799 	} else if ((strcmp(prog, "user") == 0) || (strcmp(prog, "group") == 0)) {
    800 		(void) fprintf(stderr, "%s ( add | del | mod ) ...\n", prog);
    801 	} else {
    802 		warn("usage() called with unknown prog `%s'", prog);
    803 	}
    804 	exit(EXIT_FAILURE);
    805 	/* NOTREACHED */
    806 }
    807 
    808 extern int	optind;
    809 extern char	*optarg;
    810 
    811 #ifdef EXTENSIONS
    812 #define ADD_OPT_EXTENSIONS	"p:r:v"
    813 #else
    814 #define ADD_OPT_EXTENSIONS
    815 #endif
    816 
    817 static int
    818 useradd(int argc, char **argv)
    819 {
    820 	user_t	u;
    821 	int	defaultfield;
    822 	int	bigD;
    823 	int	c;
    824 	int	i;
    825 
    826 	(void) memset(&u, 0, sizeof(u));
    827 	read_defaults(&u);
    828 	u.u_uid = -1;
    829 	defaultfield = bigD = 0;
    830 	while ((c = getopt(argc, argv, "DG:b:c:d:e:f:g:k:mou:s:" ADD_OPT_EXTENSIONS)) != -1) {
    831 		switch(c) {
    832 		case 'D':
    833 			bigD = 1;
    834 			break;
    835 		case 'G':
    836 			memsave(&u.u_groupv[u.u_groupc++], optarg, strlen(optarg));
    837 			break;
    838 		case 'b':
    839 			defaultfield = 1;
    840 			memsave(&u.u_basedir, optarg, strlen(optarg));
    841 			break;
    842 		case 'c':
    843 			memsave(&u.u_comment, optarg, strlen(optarg));
    844 			break;
    845 		case 'd':
    846 			u.u_homeset = 1;
    847 			memsave(&u.u_home, optarg, strlen(optarg));
    848 			break;
    849 		case 'e':
    850 			defaultfield = 1;
    851 			memsave(&u.u_expire, optarg, strlen(optarg));
    852 			break;
    853 		case 'f':
    854 			defaultfield = 1;
    855 			u.u_inactive = atoi(optarg);
    856 			break;
    857 		case 'g':
    858 			defaultfield = 1;
    859 			memsave(&u.u_primgrp, optarg, strlen(optarg));
    860 			break;
    861 		case 'k':
    862 			memsave(&u.u_skeldir, optarg, strlen(optarg));
    863 			break;
    864 		case 'm':
    865 			u.u_mkdir = 1;
    866 			break;
    867 		case 'o':
    868 			u.u_dupuid = 1;
    869 			break;
    870 #ifdef EXTENSIONS
    871 		case 'p':
    872 			memsave(&u.u_password, optarg, strlen(optarg));
    873 			break;
    874 #endif
    875 #ifdef EXTENSIONS
    876 		case 'r':
    877 			defaultfield = 1;
    878 			(void) save_range(&u, optarg);
    879 			break;
    880 #endif
    881 		case 's':
    882 			defaultfield = 1;
    883 			memsave(&u.u_shell, optarg, strlen(optarg));
    884 			break;
    885 		case 'u':
    886 			u.u_uid = atoi(optarg);
    887 			break;
    888 #ifdef EXTENSIONS
    889 		case 'v':
    890 			verbose = 1;
    891 			break;
    892 #endif
    893 		}
    894 	}
    895 	if (bigD) {
    896 		if (defaultfield) {
    897 			return setdefaults(&u) ? EXIT_SUCCESS : EXIT_FAILURE;
    898 		}
    899 		(void) printf("group\t\t%s\n", u.u_primgrp);
    900 		(void) printf("base_dir\t%s\n", u.u_basedir);
    901 		(void) printf("skel_dir\t%s\n", u.u_skeldir);
    902 		(void) printf("shell\t\t%s\n", u.u_shell);
    903 		(void) printf("inactive\t%d\n", u.u_inactive);
    904 		(void) printf("expire\t\t%s\n", (u.u_expire == (char *) NULL) ? UNSET_EXPIRY : u.u_expire);
    905 #ifdef EXTENSIONS
    906 		for (i = 0 ; i < u.u_rc ; i++) {
    907 			(void) printf("range\t\t%d..%d\n", u.u_rv[i].r_from, u.u_rv[i].r_to);
    908 		}
    909 #endif
    910 		return EXIT_SUCCESS;
    911 	}
    912 	if (argc == optind) {
    913 		usage("useradd");
    914 	}
    915 	return adduser(argv[optind], &u) ? EXIT_SUCCESS : EXIT_FAILURE;
    916 }
    917 
    918 #ifdef EXTENSIONS
    919 #define MOD_OPT_EXTENSIONS	"p:v"
    920 #else
    921 #define MOD_OPT_EXTENSIONS
    922 #endif
    923 
    924 static int
    925 usermod(int argc, char **argv)
    926 {
    927 	user_t	u;
    928 	char	newuser[MaxUserNameLen + 1];
    929 	int	have_new_user;
    930 	int	c;
    931 
    932 	(void) memset(&u, 0, sizeof(u));
    933 	(void) memset(newuser, 0, sizeof(newuser));
    934 	read_defaults(&u);
    935 	u.u_uid = -1;
    936 	have_new_user = 0;
    937 	while ((c = getopt(argc, argv, "G:c:d:e:f:g:l:mos:u:" MOD_OPT_EXTENSIONS)) != -1) {
    938 		switch(c) {
    939 		case 'G':
    940 			memsave(&u.u_groupv[u.u_groupc++], optarg, strlen(optarg));
    941 			break;
    942 		case 'c':
    943 			memsave(&u.u_comment, optarg, strlen(optarg));
    944 			break;
    945 		case 'd':
    946 			u.u_homeset = 1;
    947 			memsave(&u.u_home, optarg, strlen(optarg));
    948 			break;
    949 		case 'e':
    950 			memsave(&u.u_expire, optarg, strlen(optarg));
    951 			break;
    952 		case 'f':
    953 			u.u_inactive = atoi(optarg);
    954 			break;
    955 		case 'g':
    956 			memsave(&u.u_primgrp, optarg, strlen(optarg));
    957 			break;
    958 		case 'l':
    959 			have_new_user = 1;
    960 #if (__NetBSD_Version__ < 104110000)
    961 			(void) strnncpy(newuser, sizeof(newuser), optarg, strlen(optarg));
    962 #else
    963 			(void) strlcpy(newuser, optarg, sizeof(newuser));
    964 #endif /* __NetBSD_Version__ < 104110000 */
    965 			break;
    966 		case 'm':
    967 			u.u_mkdir = 1;
    968 			break;
    969 		case 'o':
    970 			u.u_dupuid = 1;
    971 			break;
    972 #ifdef EXTENSIONS
    973 		case 'p':
    974 			memsave(&u.u_password, optarg, strlen(optarg));
    975 			break;
    976 #endif
    977 		case 's':
    978 			memsave(&u.u_shell, optarg, strlen(optarg));
    979 			break;
    980 		case 'u':
    981 			u.u_uid = atoi(optarg);
    982 			break;
    983 #ifdef EXTENSIONS
    984 		case 'v':
    985 			verbose = 1;
    986 			break;
    987 #endif
    988 		}
    989 	}
    990 	if (argc == optind) {
    991 		usage("usermod");
    992 	}
    993 	return moduser(argv[optind], (have_new_user) ? newuser : argv[optind], &u) ? EXIT_SUCCESS : EXIT_FAILURE;
    994 }
    995 
    996 #ifdef EXTENSIONS
    997 #define DEL_OPT_EXTENSIONS	"Dp:v"
    998 #else
    999 #define DEL_OPT_EXTENSIONS
   1000 #endif
   1001 
   1002 static int
   1003 userdel(int argc, char **argv)
   1004 {
   1005 	struct passwd	*pwp;
   1006 	struct stat	st;
   1007 	user_t		u;
   1008 	char		password[PasswordLength + 1];
   1009 	int		defaultfield;
   1010 	int		rmhome;
   1011 	int		bigD;
   1012 	int		c;
   1013 
   1014 	(void) memset(&u, 0, sizeof(u));
   1015 	read_defaults(&u);
   1016 	defaultfield = bigD = rmhome = 0;
   1017 	while ((c = getopt(argc, argv, "r" DEL_OPT_EXTENSIONS)) != -1) {
   1018 		switch(c) {
   1019 #ifdef EXTENSIONS
   1020 		case 'D':
   1021 			bigD = 1;
   1022 			break;
   1023 #endif
   1024 #ifdef EXTENSIONS
   1025 		case 'p':
   1026 			defaultfield = 1;
   1027 			u.u_preserve = (strcmp(optarg, "true") == 0) ? 1 :
   1028 					(strcmp(optarg, "yes") == 0) ? 1 :
   1029 					 atoi(optarg);
   1030 			break;
   1031 #endif
   1032 		case 'r':
   1033 			rmhome = 1;
   1034 			break;
   1035 #ifdef EXTENSIONS
   1036 		case 'v':
   1037 			verbose = 1;
   1038 			break;
   1039 #endif
   1040 		}
   1041 	}
   1042 #ifdef EXTENSIONS
   1043 	if (bigD) {
   1044 		if (defaultfield) {
   1045 			return setdefaults(&u) ? EXIT_SUCCESS : EXIT_FAILURE;
   1046 		}
   1047 		(void) printf("preserve\t%s\n", (u.u_preserve) ? "true" : "false");
   1048 		return EXIT_SUCCESS;
   1049 	}
   1050 #endif
   1051 	if (argc == optind) {
   1052 		usage("userdel");
   1053 	}
   1054 	if ((pwp = getpwnam(argv[optind])) == (struct passwd *) NULL) {
   1055 		warn("No such user `%s'", argv[optind]);
   1056 		return EXIT_FAILURE;
   1057 	}
   1058 	if (rmhome) {
   1059 		if (stat(pwp->pw_dir, &st) < 0) {
   1060 			warn("Home directory `%s' does not exist", pwp->pw_dir);
   1061 			return EXIT_FAILURE;
   1062 		}
   1063 		(void) asystem("%s -rf %s", RM, pwp->pw_dir);
   1064 	}
   1065 	if (u.u_preserve) {
   1066 		memsave(&u.u_shell, FALSE_PROG, strlen(FALSE_PROG));
   1067 		(void) memset(password, '*', PasswordLength);
   1068 		password[PasswordLength] = 0;
   1069 		memsave(&u.u_password, password, PasswordLength);
   1070 		return moduser(argv[optind], argv[optind], &u) ? EXIT_SUCCESS : EXIT_FAILURE;
   1071 	}
   1072 	return moduser(argv[optind], argv[optind], (user_t *) NULL) ? EXIT_SUCCESS : EXIT_FAILURE;
   1073 }
   1074 
   1075 #ifdef EXTENSIONS
   1076 #define GROUP_ADD_OPT_EXTENSIONS	"v"
   1077 #else
   1078 #define GROUP_ADD_OPT_EXTENSIONS
   1079 #endif
   1080 
   1081 /* add a group */
   1082 int
   1083 groupadd(int argc, char **argv)
   1084 {
   1085 	int	dupgid;
   1086 	int	gid;
   1087 	int	c;
   1088 
   1089 	gid = -1;
   1090 	dupgid = 0;
   1091 	while ((c = getopt(argc, argv, "g:o" GROUP_ADD_OPT_EXTENSIONS)) != -1) {
   1092 		switch(c) {
   1093 		case 'g':
   1094 			gid = atoi(optarg);
   1095 			break;
   1096 		case 'o':
   1097 			dupgid = 1;
   1098 			break;
   1099 #ifdef EXTENSIONS
   1100 		case 'v':
   1101 			verbose = 1;
   1102 			break;
   1103 #endif
   1104 		}
   1105 	}
   1106 	if (argc == optind) {
   1107 		usage("groupadd");
   1108 	}
   1109 	if (gid < 0 && !getnextgid(&gid, LowGid, HighGid)) {
   1110 		err(EXIT_FAILURE, "can't add group: can't get next gid");
   1111 	}
   1112 	if (!dupgid && getgrgid((gid_t) gid) != (struct group *) NULL) {
   1113 		err(EXIT_FAILURE, "can't add group: gid %d is a duplicate", gid);
   1114 	}
   1115 	if (!valid_group(argv[optind])) {
   1116 		warn("warning - invalid group name `%s'", argv[optind]);
   1117 	}
   1118 	if (!creategid(argv[optind], gid, "")) {
   1119 		err(EXIT_FAILURE, "can't add group: problems with %s file", ETCGROUP);
   1120 	}
   1121 	return EXIT_SUCCESS;
   1122 }
   1123 
   1124 #ifdef EXTENSIONS
   1125 #define GROUP_DEL_OPT_EXTENSIONS	"v"
   1126 #else
   1127 #define GROUP_DEL_OPT_EXTENSIONS
   1128 #endif
   1129 
   1130 /* remove a group */
   1131 int
   1132 groupdel(int argc, char **argv)
   1133 {
   1134 	int	c;
   1135 
   1136 	while ((c = getopt(argc, argv, "" GROUP_DEL_OPT_EXTENSIONS)) != -1) {
   1137 		switch(c) {
   1138 #ifdef EXTENSIONS
   1139 		case 'v':
   1140 			verbose = 1;
   1141 			break;
   1142 #endif
   1143 		}
   1144 	}
   1145 	if (argc == optind) {
   1146 		usage("groupdel");
   1147 	}
   1148 	if (!modify_gid(argv[optind], (char *) NULL)) {
   1149 		err(EXIT_FAILURE, "can't change %s file", ETCGROUP);
   1150 	}
   1151 	return EXIT_SUCCESS;
   1152 }
   1153 
   1154 #ifdef EXTENSIONS
   1155 #define GROUP_MOD_OPT_EXTENSIONS	"v"
   1156 #else
   1157 #define GROUP_MOD_OPT_EXTENSIONS
   1158 #endif
   1159 
   1160 /* modify a group */
   1161 int
   1162 groupmod(int argc, char **argv)
   1163 {
   1164 	struct group	*grp;
   1165 	char		buf[MaxEntryLen];
   1166 	char		*newname;
   1167 	char		**cpp;
   1168 	int		dupgid;
   1169 	int		gid;
   1170 	int		cc;
   1171 	int		c;
   1172 
   1173 	gid = -1;
   1174 	dupgid = 0;
   1175 	newname = (char *) NULL;
   1176 	while ((c = getopt(argc, argv, "g:on:" GROUP_MOD_OPT_EXTENSIONS)) != -1) {
   1177 		switch(c) {
   1178 		case 'g':
   1179 			gid = atoi(optarg);
   1180 			break;
   1181 		case 'o':
   1182 			dupgid = 1;
   1183 			break;
   1184 		case 'n':
   1185 			memsave(&newname, optarg, strlen(optarg));
   1186 			break;
   1187 #ifdef EXTENSIONS
   1188 		case 'v':
   1189 			verbose = 1;
   1190 			break;
   1191 #endif
   1192 		}
   1193 	}
   1194 	if (argc == optind) {
   1195 		usage("groupmod");
   1196 	}
   1197 	if (gid < 0 && newname == (char *) NULL) {
   1198 		err(EXIT_FAILURE, "Nothing to change");
   1199 	}
   1200 	if (dupgid && gid < 0) {
   1201 		err(EXIT_FAILURE, "Duplicate which gid?");
   1202 	}
   1203 	if ((grp = getgrnam(argv[optind])) == (struct group *) NULL) {
   1204 		err(EXIT_FAILURE, "can't find group `%s' to modify", argv[optind]);
   1205 	}
   1206 	if (newname != (char *) NULL && !valid_group(newname)) {
   1207 		warn("warning - invalid group name `%s'", newname);
   1208 	}
   1209 	cc = snprintf(buf, sizeof(buf), "%s:%s:%d:",
   1210 			(newname) ? newname : grp->gr_name,
   1211 			grp->gr_passwd,
   1212 			(gid < 0) ? grp->gr_gid : gid);
   1213 	for (cpp = grp->gr_mem ; *cpp && cc < sizeof(buf) ; cpp++) {
   1214 		cc += snprintf(&buf[cc], sizeof(buf) - cc, "%s%s", *cpp,
   1215 			(cpp[1] == (char *) NULL) ? "" : ",");
   1216 	}
   1217 	if (!modify_gid(argv[optind], buf)) {
   1218 		err(EXIT_FAILURE, "can't change %s file", ETCGROUP);
   1219 	}
   1220 	return EXIT_SUCCESS;
   1221 }
   1222 
   1223 /* this struct describes a command */
   1224 typedef struct cmd_t {
   1225 	char	*c_progname;	/* program name */
   1226 	char	*c_word1;	/* alternative program name */
   1227 	char	*c_word2;	/* alternative command word */
   1228 	int	(*c_func)(int argc, char **argv);	/* called function */
   1229 } cmd_t;
   1230 
   1231 /* despatch table for commands */
   1232 static cmd_t	cmds[] = {
   1233 	{	"useradd",	"user",		"add",	useradd		},
   1234 	{	"usermod",	"user",		"mod",	usermod		},
   1235 	{	"userdel",	"user",		"del",	userdel		},
   1236 	{	"groupadd",	"group",	"add",	groupadd	},
   1237 	{	"groupmod",	"group",	"mod",	groupmod	},
   1238 	{	"groupdel",	"group",	"del",	groupdel	},
   1239 	{	NULL	}
   1240 };
   1241 
   1242 extern char	*__progname;
   1243 
   1244 int
   1245 main(int argc, char **argv)
   1246 {
   1247 	cmd_t	*cmdp;
   1248 
   1249 	for (cmdp = cmds ; cmdp->c_progname ; cmdp++) {
   1250 		if (strcmp(__progname, cmdp->c_progname) == 0) {
   1251 			return (*cmdp->c_func)(argc, argv);
   1252 		}
   1253 
   1254 		if (strcmp(__progname, cmdp->c_word1) == 0 &&
   1255 		    argc > 1 && strcmp(argv[1], cmdp->c_word2) == 0)
   1256 			return (*cmdp->c_func)(argc - 1, argv + 1);
   1257 	}
   1258 	errx(EXIT_FAILURE, "Program `%s' not recognised", __progname);
   1259 	/* NOTREACHED */
   1260 }
   1261