xref: /titanic_44/usr/src/cmd/oamuser/user/useradd.c (revision f48205be61a214698b763ff550ab9e657525104c)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50523b0a4Sbasabi  * Common Development and Distribution License (the "License").
60523b0a4Sbasabi  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*f48205beScasper  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
277c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #include	<sys/types.h>
337c478bd9Sstevel@tonic-gate #include	<sys/stat.h>
347c478bd9Sstevel@tonic-gate #include	<sys/param.h>
357c478bd9Sstevel@tonic-gate #include	<stdio.h>
367c478bd9Sstevel@tonic-gate #include	<stdlib.h>
377c478bd9Sstevel@tonic-gate #include	<ctype.h>
387c478bd9Sstevel@tonic-gate #include	<limits.h>
397c478bd9Sstevel@tonic-gate #include	<string.h>
407c478bd9Sstevel@tonic-gate #include	<userdefs.h>
417c478bd9Sstevel@tonic-gate #include	<errno.h>
427c478bd9Sstevel@tonic-gate #include	<project.h>
437c478bd9Sstevel@tonic-gate #include	<unistd.h>
447c478bd9Sstevel@tonic-gate #include	<user_attr.h>
457c478bd9Sstevel@tonic-gate #include	"users.h"
467c478bd9Sstevel@tonic-gate #include	"messages.h"
477c478bd9Sstevel@tonic-gate #include	"userdisp.h"
487c478bd9Sstevel@tonic-gate #include	"funcs.h"
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate /*
517c478bd9Sstevel@tonic-gate  *  useradd [-u uid [-o] | -g group | -G group [[, group]...] | -d dir [-m]
520523b0a4Sbasabi  *		| -s shell | -c comment | -k skel_dir | -b base_dir] ]
537c478bd9Sstevel@tonic-gate  *		[ -A authorization [, authorization ...]]
547c478bd9Sstevel@tonic-gate  *		[ -P profile [, profile ...]]
557c478bd9Sstevel@tonic-gate  *		[ -K key=value ]
567c478bd9Sstevel@tonic-gate  *		[ -R role [, role ...]] [-p project [, project ...]] login
570523b0a4Sbasabi  *  useradd -D [ -g group ] [ -b base_dir | -f inactive | -e expire |
580523b0a4Sbasabi  *		-s shell | -k skel_dir ]
597c478bd9Sstevel@tonic-gate  *		[ -A authorization [, authorization ...]]
607c478bd9Sstevel@tonic-gate  *		[ -P profile [, profile ...]] [ -K key=value ]
617c478bd9Sstevel@tonic-gate  *		[ -R role [, role ...]] [-p project [, project ...]] login
627c478bd9Sstevel@tonic-gate  *
637c478bd9Sstevel@tonic-gate  *	This command adds new user logins to the system.  Arguments are:
647c478bd9Sstevel@tonic-gate  *
657c478bd9Sstevel@tonic-gate  *	uid - an integer
667c478bd9Sstevel@tonic-gate  *	group - an existing group's integer ID or char string name
677c478bd9Sstevel@tonic-gate  *	dir - home directory
687c478bd9Sstevel@tonic-gate  *	shell - a program to be used as a shell
697c478bd9Sstevel@tonic-gate  *	comment - any text string
707c478bd9Sstevel@tonic-gate  *	skel_dir - a skeleton directory
717c478bd9Sstevel@tonic-gate  *	base_dir - a directory
727c478bd9Sstevel@tonic-gate  *	login - a string of printable chars except colon(:)
737c478bd9Sstevel@tonic-gate  *	authorization - One or more comma separated authorizations defined
747c478bd9Sstevel@tonic-gate  *			in auth_attr(4).
757c478bd9Sstevel@tonic-gate  *	profile - One or more comma separated execution profiles defined
767c478bd9Sstevel@tonic-gate  *		  in prof_attr(4)
777c478bd9Sstevel@tonic-gate  *	role - One or more comma-separated role names defined in user_attr(4)
787c478bd9Sstevel@tonic-gate  *	project - One or more comma-separated project names or numbers
797c478bd9Sstevel@tonic-gate  *
807c478bd9Sstevel@tonic-gate  */
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate extern struct userdefs *getusrdef();
837c478bd9Sstevel@tonic-gate extern void dispusrdef();
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate static void cleanup();
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate extern uid_t findnextuid(void);
887c478bd9Sstevel@tonic-gate extern int check_perm(), valid_expire();
897c478bd9Sstevel@tonic-gate extern int putusrdef(), valid_uid();
907c478bd9Sstevel@tonic-gate extern int call_passmgmt(), edit_group(), create_home();
917c478bd9Sstevel@tonic-gate extern int edit_project();
927c478bd9Sstevel@tonic-gate extern int **valid_lgroup();
937c478bd9Sstevel@tonic-gate extern projid_t **valid_lproject();
947c478bd9Sstevel@tonic-gate extern void update_def(struct userdefs *);
957c478bd9Sstevel@tonic-gate extern void import_def(struct userdefs *);
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate static uid_t uid;			/* new uid */
987c478bd9Sstevel@tonic-gate static char *logname;			/* login name to add */
997c478bd9Sstevel@tonic-gate static struct userdefs *usrdefs;	/* defaults for useradd */
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate char *cmdname;
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate static char homedir[ PATH_MAX + 1 ];	/* home directory */
1047c478bd9Sstevel@tonic-gate static char gidstring[32];		/* group id string representation */
1057c478bd9Sstevel@tonic-gate static gid_t gid;			/* gid of new login */
1067c478bd9Sstevel@tonic-gate static char uidstring[32];		/* user id string representation */
1077c478bd9Sstevel@tonic-gate static char *uidstr = NULL;		/* uid from command line */
1087c478bd9Sstevel@tonic-gate static char *base_dir = NULL;		/* base_dir from command line */
1097c478bd9Sstevel@tonic-gate static char *group = NULL;		/* group from command line */
1107c478bd9Sstevel@tonic-gate static char *grps = NULL;		/* multi groups from command line */
1117c478bd9Sstevel@tonic-gate static char *dir = NULL;		/* home dir from command line */
1127c478bd9Sstevel@tonic-gate static char *shell = NULL;		/* shell from command line */
1137c478bd9Sstevel@tonic-gate static char *comment = NULL;		/* comment from command line */
1147c478bd9Sstevel@tonic-gate static char *skel_dir = NULL;		/* skel dir from command line */
1157c478bd9Sstevel@tonic-gate static long inact;			/* inactive days */
1167c478bd9Sstevel@tonic-gate static char *inactstr = NULL;		/* inactive from command line */
1177c478bd9Sstevel@tonic-gate static char inactstring[10];		/* inactivity string representation */
1187c478bd9Sstevel@tonic-gate static char *expirestr = NULL;		/* expiration date from command line */
1197c478bd9Sstevel@tonic-gate static char *projects = NULL;		/* project id's from command line */
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate static char *usertype = NULL;	/* type of user, either role or normal */
1227c478bd9Sstevel@tonic-gate 
1230523b0a4Sbasabi typedef enum {
1240523b0a4Sbasabi 	BASEDIR	= 0,
1250523b0a4Sbasabi 	SKELDIR,
1260523b0a4Sbasabi 	SHELL
1270523b0a4Sbasabi } path_opt_t;
1280523b0a4Sbasabi 
1290523b0a4Sbasabi 
1300523b0a4Sbasabi static void valid_input(path_opt_t, const char *);
1310523b0a4Sbasabi 
1327c478bd9Sstevel@tonic-gate int
1337c478bd9Sstevel@tonic-gate main(argc, argv)
1347c478bd9Sstevel@tonic-gate int argc;
1357c478bd9Sstevel@tonic-gate char *argv[];
1367c478bd9Sstevel@tonic-gate {
1377c478bd9Sstevel@tonic-gate 	int ch, ret, mflag = 0, oflag = 0, Dflag = 0, **gidlist;
1387c478bd9Sstevel@tonic-gate 	projid_t **projlist;
1397c478bd9Sstevel@tonic-gate 	char *ptr;			/* loc in a str, may be set by strtol */
1407c478bd9Sstevel@tonic-gate 	struct group *g_ptr;
1417c478bd9Sstevel@tonic-gate 	struct project p_ptr;
1427c478bd9Sstevel@tonic-gate 	char mybuf[PROJECT_BUFSZ];
1437c478bd9Sstevel@tonic-gate 	struct stat statbuf;		/* status buffer for stat */
1447c478bd9Sstevel@tonic-gate 	int warning;
1457c478bd9Sstevel@tonic-gate 	int busy = 0;
1467c478bd9Sstevel@tonic-gate 	char **nargv;			/* arguments for execvp of passmgmt */
1477c478bd9Sstevel@tonic-gate 	int argindex;			/* argument index into nargv */
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 	cmdname = argv[0];
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate 	if (geteuid() != 0) {
1527c478bd9Sstevel@tonic-gate 		errmsg(M_PERM_DENIED);
1537c478bd9Sstevel@tonic-gate 		exit(EX_NO_PERM);
1547c478bd9Sstevel@tonic-gate 	}
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate 	opterr = 0;			/* no print errors from getopt */
1577c478bd9Sstevel@tonic-gate 	usertype = getusertype(argv[0]);
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate 	change_key(USERATTR_TYPE_KW, usertype);
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate 	while ((ch = getopt(argc, argv,
1627c478bd9Sstevel@tonic-gate 		    "b:c:Dd:e:f:G:g:k:mop:s:u:A:P:R:K:")) != EOF)
1637c478bd9Sstevel@tonic-gate 		switch (ch) {
1647c478bd9Sstevel@tonic-gate 		case 'b':
1657c478bd9Sstevel@tonic-gate 			base_dir = optarg;
1667c478bd9Sstevel@tonic-gate 			break;
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate 		case 'c':
1697c478bd9Sstevel@tonic-gate 			comment = optarg;
1707c478bd9Sstevel@tonic-gate 			break;
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate 		case 'D':
1737c478bd9Sstevel@tonic-gate 			Dflag++;
1747c478bd9Sstevel@tonic-gate 			break;
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate 		case 'd':
1777c478bd9Sstevel@tonic-gate 			dir = optarg;
1787c478bd9Sstevel@tonic-gate 			break;
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 		case 'e':
1817c478bd9Sstevel@tonic-gate 			expirestr = optarg;
1827c478bd9Sstevel@tonic-gate 			break;
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 		case 'f':
1857c478bd9Sstevel@tonic-gate 			inactstr = optarg;
1867c478bd9Sstevel@tonic-gate 			break;
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 		case 'G':
1897c478bd9Sstevel@tonic-gate 			grps = optarg;
1907c478bd9Sstevel@tonic-gate 			break;
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 		case 'g':
1937c478bd9Sstevel@tonic-gate 			group = optarg;
1947c478bd9Sstevel@tonic-gate 			break;
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate 		case 'k':
1977c478bd9Sstevel@tonic-gate 			skel_dir = optarg;
1987c478bd9Sstevel@tonic-gate 			break;
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate 		case 'm':
2017c478bd9Sstevel@tonic-gate 			mflag++;
2027c478bd9Sstevel@tonic-gate 			break;
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 		case 'o':
2057c478bd9Sstevel@tonic-gate 			oflag++;
2067c478bd9Sstevel@tonic-gate 			break;
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 		case 'p':
2097c478bd9Sstevel@tonic-gate 			projects = optarg;
2107c478bd9Sstevel@tonic-gate 			break;
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate 		case 's':
2137c478bd9Sstevel@tonic-gate 			shell = optarg;
2147c478bd9Sstevel@tonic-gate 			break;
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate 		case 'u':
2177c478bd9Sstevel@tonic-gate 			uidstr = optarg;
2187c478bd9Sstevel@tonic-gate 			break;
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 		case 'A':
2217c478bd9Sstevel@tonic-gate 			change_key(USERATTR_AUTHS_KW, optarg);
2227c478bd9Sstevel@tonic-gate 			break;
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 		case 'P':
2257c478bd9Sstevel@tonic-gate 			change_key(USERATTR_PROFILES_KW, optarg);
2267c478bd9Sstevel@tonic-gate 			break;
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate 		case 'R':
2297c478bd9Sstevel@tonic-gate 			if (is_role(usertype)) {
2307c478bd9Sstevel@tonic-gate 				errmsg(M_ARUSAGE);
2317c478bd9Sstevel@tonic-gate 				exit(EX_SYNTAX);
2327c478bd9Sstevel@tonic-gate 			}
2337c478bd9Sstevel@tonic-gate 			change_key(USERATTR_ROLES_KW, optarg);
2347c478bd9Sstevel@tonic-gate 			break;
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate 		case 'K':
2377c478bd9Sstevel@tonic-gate 			change_key(NULL, optarg);
2387c478bd9Sstevel@tonic-gate 			break;
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate 		default:
2417c478bd9Sstevel@tonic-gate 		case '?':
2427c478bd9Sstevel@tonic-gate 			if (is_role(usertype))
2437c478bd9Sstevel@tonic-gate 				errmsg(M_ARUSAGE);
2447c478bd9Sstevel@tonic-gate 			else
2457c478bd9Sstevel@tonic-gate 				errmsg(M_AUSAGE);
2467c478bd9Sstevel@tonic-gate 			exit(EX_SYNTAX);
2477c478bd9Sstevel@tonic-gate 		}
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate 	/* get defaults for adding new users */
2507c478bd9Sstevel@tonic-gate 	usrdefs = getusrdef(usertype);
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate 	if (Dflag) {
2537c478bd9Sstevel@tonic-gate 		/* DISPLAY mode */
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate 		/* check syntax */
2567c478bd9Sstevel@tonic-gate 		if (optind != argc) {
2577c478bd9Sstevel@tonic-gate 			if (is_role(usertype))
2587c478bd9Sstevel@tonic-gate 				errmsg(M_ARUSAGE);
2597c478bd9Sstevel@tonic-gate 			else
2607c478bd9Sstevel@tonic-gate 				errmsg(M_AUSAGE);
2617c478bd9Sstevel@tonic-gate 			exit(EX_SYNTAX);
2627c478bd9Sstevel@tonic-gate 		}
2637c478bd9Sstevel@tonic-gate 
2640523b0a4Sbasabi 		if (uidstr != NULL || oflag || grps != NULL ||
2650523b0a4Sbasabi 		    dir != NULL || mflag || comment != NULL) {
2667c478bd9Sstevel@tonic-gate 			if (is_role(usertype))
2677c478bd9Sstevel@tonic-gate 				errmsg(M_ARUSAGE);
2687c478bd9Sstevel@tonic-gate 			else
2697c478bd9Sstevel@tonic-gate 				errmsg(M_AUSAGE);
2707c478bd9Sstevel@tonic-gate 			exit(EX_SYNTAX);
2717c478bd9Sstevel@tonic-gate 		}
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate 		/* Group must be an existing group */
2740523b0a4Sbasabi 		if (group != NULL) {
2757c478bd9Sstevel@tonic-gate 			switch (valid_group(group, &g_ptr, &warning)) {
2767c478bd9Sstevel@tonic-gate 			case INVALID:
2777c478bd9Sstevel@tonic-gate 				errmsg(M_INVALID, group, "group id");
2787c478bd9Sstevel@tonic-gate 				exit(EX_BADARG);
2797c478bd9Sstevel@tonic-gate 				/*NOTREACHED*/
2807c478bd9Sstevel@tonic-gate 			case TOOBIG:
2817c478bd9Sstevel@tonic-gate 				errmsg(M_TOOBIG, "gid", group);
2827c478bd9Sstevel@tonic-gate 				exit(EX_BADARG);
2837c478bd9Sstevel@tonic-gate 				/*NOTREACHED*/
2847c478bd9Sstevel@tonic-gate 			case RESERVED:
2857c478bd9Sstevel@tonic-gate 			case UNIQUE:
2867c478bd9Sstevel@tonic-gate 				errmsg(M_GRP_NOTUSED, group);
2877c478bd9Sstevel@tonic-gate 				exit(EX_NAME_NOT_EXIST);
2887c478bd9Sstevel@tonic-gate 			}
2897c478bd9Sstevel@tonic-gate 			if (warning)
2907c478bd9Sstevel@tonic-gate 				warningmsg(warning, group);
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate 			usrdefs->defgroup = g_ptr->gr_gid;
2937c478bd9Sstevel@tonic-gate 			usrdefs->defgname = g_ptr->gr_name;
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate 		}
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate 		/* project must be an existing project */
2980523b0a4Sbasabi 		if (projects != NULL) {
2997c478bd9Sstevel@tonic-gate 			switch (valid_project(projects, &p_ptr, mybuf,
3007c478bd9Sstevel@tonic-gate 			    sizeof (mybuf), &warning)) {
3017c478bd9Sstevel@tonic-gate 			case INVALID:
3027c478bd9Sstevel@tonic-gate 				errmsg(M_INVALID, projects, "project id");
3037c478bd9Sstevel@tonic-gate 				exit(EX_BADARG);
3047c478bd9Sstevel@tonic-gate 				/*NOTREACHED*/
3057c478bd9Sstevel@tonic-gate 			case TOOBIG:
3067c478bd9Sstevel@tonic-gate 				errmsg(M_TOOBIG, "projid", projects);
3077c478bd9Sstevel@tonic-gate 				exit(EX_BADARG);
3087c478bd9Sstevel@tonic-gate 				/*NOTREACHED*/
3097c478bd9Sstevel@tonic-gate 			case UNIQUE:
3107c478bd9Sstevel@tonic-gate 				errmsg(M_PROJ_NOTUSED, projects);
3117c478bd9Sstevel@tonic-gate 				exit(EX_NAME_NOT_EXIST);
3127c478bd9Sstevel@tonic-gate 			}
3137c478bd9Sstevel@tonic-gate 			if (warning)
3147c478bd9Sstevel@tonic-gate 				warningmsg(warning, projects);
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate 			usrdefs->defproj = p_ptr.pj_projid;
3177c478bd9Sstevel@tonic-gate 			usrdefs->defprojname = p_ptr.pj_name;
3187c478bd9Sstevel@tonic-gate 		}
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate 		/* base_dir must be an existing directory */
3210523b0a4Sbasabi 		if (base_dir != NULL) {
3220523b0a4Sbasabi 			valid_input(BASEDIR, base_dir);
3237c478bd9Sstevel@tonic-gate 			usrdefs->defparent = base_dir;
3247c478bd9Sstevel@tonic-gate 		}
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate 		/* inactivity period is an integer */
3270523b0a4Sbasabi 		if (inactstr != NULL) {
3287c478bd9Sstevel@tonic-gate 			/* convert inactstr to integer */
3297c478bd9Sstevel@tonic-gate 			inact = strtol(inactstr, &ptr, 10);
3307c478bd9Sstevel@tonic-gate 			if (*ptr || inact < 0) {
3317c478bd9Sstevel@tonic-gate 				errmsg(M_INVALID, inactstr,
3327c478bd9Sstevel@tonic-gate 				    "inactivity period");
3337c478bd9Sstevel@tonic-gate 				exit(EX_BADARG);
3347c478bd9Sstevel@tonic-gate 			}
3357c478bd9Sstevel@tonic-gate 
3367c478bd9Sstevel@tonic-gate 			usrdefs->definact = inact;
3377c478bd9Sstevel@tonic-gate 		}
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate 		/* expiration string is a date, newer than today */
3400523b0a4Sbasabi 		if (expirestr != NULL) {
3417c478bd9Sstevel@tonic-gate 			if (*expirestr) {
3427c478bd9Sstevel@tonic-gate 				if (valid_expire(expirestr, (time_t *)0)
3437c478bd9Sstevel@tonic-gate 				    == INVALID) {
3447c478bd9Sstevel@tonic-gate 					errmsg(M_INVALID, expirestr,
3457c478bd9Sstevel@tonic-gate 					    "expiration date");
3467c478bd9Sstevel@tonic-gate 					exit(EX_BADARG);
3477c478bd9Sstevel@tonic-gate 				}
3487c478bd9Sstevel@tonic-gate 				usrdefs->defexpire = expirestr;
3497c478bd9Sstevel@tonic-gate 			} else
3507c478bd9Sstevel@tonic-gate 				/* Unset the expiration date */
3517c478bd9Sstevel@tonic-gate 				usrdefs->defexpire = "";
3527c478bd9Sstevel@tonic-gate 		}
3537c478bd9Sstevel@tonic-gate 
3540523b0a4Sbasabi 		if (shell != NULL) {
3550523b0a4Sbasabi 			valid_input(SHELL, shell);
3560523b0a4Sbasabi 			usrdefs->defshell = shell;
3570523b0a4Sbasabi 		}
3580523b0a4Sbasabi 		if (skel_dir != NULL) {
3590523b0a4Sbasabi 			valid_input(SKELDIR, skel_dir);
3600523b0a4Sbasabi 			usrdefs->defskel = skel_dir;
3610523b0a4Sbasabi 		}
3627c478bd9Sstevel@tonic-gate 		update_def(usrdefs);
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate 		/* change defaults for useradd */
3657c478bd9Sstevel@tonic-gate 		if (putusrdef(usrdefs, usertype) < 0) {
3667c478bd9Sstevel@tonic-gate 			errmsg(M_UPDATE, "created");
3677c478bd9Sstevel@tonic-gate 			exit(EX_UPDATE);
3687c478bd9Sstevel@tonic-gate 		}
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate 		/* Now, display */
3717c478bd9Sstevel@tonic-gate 		dispusrdef(stdout, (D_ALL & ~D_RID), usertype);
3727c478bd9Sstevel@tonic-gate 		exit(EX_SUCCESS);
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate 	}
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate 	/* ADD mode */
3777c478bd9Sstevel@tonic-gate 
3787c478bd9Sstevel@tonic-gate 	/* check syntax */
3790523b0a4Sbasabi 	if (optind != argc - 1 || (skel_dir != NULL && !mflag)) {
3807c478bd9Sstevel@tonic-gate 		if (is_role(usertype))
3817c478bd9Sstevel@tonic-gate 			errmsg(M_ARUSAGE);
3827c478bd9Sstevel@tonic-gate 		else
3837c478bd9Sstevel@tonic-gate 			errmsg(M_AUSAGE);
3847c478bd9Sstevel@tonic-gate 		exit(EX_SYNTAX);
3857c478bd9Sstevel@tonic-gate 	}
3867c478bd9Sstevel@tonic-gate 
3877c478bd9Sstevel@tonic-gate 	logname = argv[optind];
3887c478bd9Sstevel@tonic-gate 	switch (valid_login(logname, (struct passwd **)NULL, &warning)) {
3897c478bd9Sstevel@tonic-gate 	case INVALID:
3907c478bd9Sstevel@tonic-gate 		errmsg(M_INVALID, logname, "login name");
3917c478bd9Sstevel@tonic-gate 		exit(EX_BADARG);
3927c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
3937c478bd9Sstevel@tonic-gate 
3947c478bd9Sstevel@tonic-gate 	case NOTUNIQUE:
3957c478bd9Sstevel@tonic-gate 		errmsg(M_USED, logname);
3967c478bd9Sstevel@tonic-gate 		exit(EX_NAME_EXISTS);
3977c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
3987c478bd9Sstevel@tonic-gate 	}
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate 	if (warning)
4017c478bd9Sstevel@tonic-gate 		warningmsg(warning, logname);
4020523b0a4Sbasabi 	if (uidstr != NULL) {
4037c478bd9Sstevel@tonic-gate 		/* convert uidstr to integer */
4047c478bd9Sstevel@tonic-gate 		errno = 0;
4057c478bd9Sstevel@tonic-gate 		uid = (uid_t)strtol(uidstr, &ptr, (int)10);
4067c478bd9Sstevel@tonic-gate 		if (*ptr || errno == ERANGE) {
4077c478bd9Sstevel@tonic-gate 			errmsg(M_INVALID, uidstr, "user id");
4087c478bd9Sstevel@tonic-gate 			exit(EX_BADARG);
4097c478bd9Sstevel@tonic-gate 		}
4107c478bd9Sstevel@tonic-gate 
4117c478bd9Sstevel@tonic-gate 		switch (valid_uid(uid, NULL)) {
4127c478bd9Sstevel@tonic-gate 		case NOTUNIQUE:
4137c478bd9Sstevel@tonic-gate 			if (!oflag) {
4147c478bd9Sstevel@tonic-gate 				/* override not specified */
4157c478bd9Sstevel@tonic-gate 				errmsg(M_UID_USED, uid);
4167c478bd9Sstevel@tonic-gate 				exit(EX_ID_EXISTS);
4177c478bd9Sstevel@tonic-gate 			}
4187c478bd9Sstevel@tonic-gate 			break;
4197c478bd9Sstevel@tonic-gate 		case RESERVED:
4207c478bd9Sstevel@tonic-gate 			errmsg(M_RESERVED, uid);
4217c478bd9Sstevel@tonic-gate 			break;
4227c478bd9Sstevel@tonic-gate 		case TOOBIG:
4237c478bd9Sstevel@tonic-gate 			errmsg(M_TOOBIG, "uid", uid);
4247c478bd9Sstevel@tonic-gate 			exit(EX_BADARG);
4257c478bd9Sstevel@tonic-gate 			break;
4267c478bd9Sstevel@tonic-gate 		}
4277c478bd9Sstevel@tonic-gate 
4287c478bd9Sstevel@tonic-gate 	} else {
4297c478bd9Sstevel@tonic-gate 
4307c478bd9Sstevel@tonic-gate 		if ((uid = findnextuid()) < 0) {
4317c478bd9Sstevel@tonic-gate 			errmsg(M_INVALID, "default id", "user id");
4327c478bd9Sstevel@tonic-gate 			exit(EX_ID_EXISTS);
4337c478bd9Sstevel@tonic-gate 		}
4347c478bd9Sstevel@tonic-gate 	}
4357c478bd9Sstevel@tonic-gate 
4360523b0a4Sbasabi 	if (group != NULL) {
4377c478bd9Sstevel@tonic-gate 		switch (valid_group(group, &g_ptr, &warning)) {
4387c478bd9Sstevel@tonic-gate 		case INVALID:
4397c478bd9Sstevel@tonic-gate 			errmsg(M_INVALID, group, "group id");
4407c478bd9Sstevel@tonic-gate 			exit(EX_BADARG);
4417c478bd9Sstevel@tonic-gate 			/*NOTREACHED*/
4427c478bd9Sstevel@tonic-gate 		case TOOBIG:
4437c478bd9Sstevel@tonic-gate 			errmsg(M_TOOBIG, "gid", group);
4447c478bd9Sstevel@tonic-gate 			exit(EX_BADARG);
4457c478bd9Sstevel@tonic-gate 			/*NOTREACHED*/
4467c478bd9Sstevel@tonic-gate 		case RESERVED:
4477c478bd9Sstevel@tonic-gate 		case UNIQUE:
4487c478bd9Sstevel@tonic-gate 			errmsg(M_GRP_NOTUSED, group);
4497c478bd9Sstevel@tonic-gate 			exit(EX_NAME_NOT_EXIST);
4507c478bd9Sstevel@tonic-gate 			/*NOTREACHED*/
4517c478bd9Sstevel@tonic-gate 		}
4527c478bd9Sstevel@tonic-gate 
4537c478bd9Sstevel@tonic-gate 		if (warning)
4547c478bd9Sstevel@tonic-gate 			warningmsg(warning, group);
4557c478bd9Sstevel@tonic-gate 		gid = g_ptr->gr_gid;
4567c478bd9Sstevel@tonic-gate 
4577c478bd9Sstevel@tonic-gate 	} else gid = usrdefs->defgroup;
4587c478bd9Sstevel@tonic-gate 
4590523b0a4Sbasabi 	if (grps != NULL) {
4607c478bd9Sstevel@tonic-gate 		if (!*grps)
4617c478bd9Sstevel@tonic-gate 			/* ignore -G "" */
4627c478bd9Sstevel@tonic-gate 			grps = (char *)0;
4637c478bd9Sstevel@tonic-gate 		else if (!(gidlist = valid_lgroup(grps, gid)))
4647c478bd9Sstevel@tonic-gate 			exit(EX_BADARG);
4657c478bd9Sstevel@tonic-gate 	}
4667c478bd9Sstevel@tonic-gate 
4670523b0a4Sbasabi 	if (projects != NULL) {
4687c478bd9Sstevel@tonic-gate 		if (! *projects)
4697c478bd9Sstevel@tonic-gate 			projects = (char *)0;
4707c478bd9Sstevel@tonic-gate 		else if (! (projlist = valid_lproject(projects)))
4717c478bd9Sstevel@tonic-gate 			exit(EX_BADARG);
4727c478bd9Sstevel@tonic-gate 	}
4737c478bd9Sstevel@tonic-gate 
4740523b0a4Sbasabi 	/* if base_dir is provided, check its validity; otherwise default */
4750523b0a4Sbasabi 	if (base_dir != NULL)
4760523b0a4Sbasabi 		valid_input(BASEDIR, base_dir);
4770523b0a4Sbasabi 	else
4780523b0a4Sbasabi 		base_dir = usrdefs->defparent;
4790523b0a4Sbasabi 
4800523b0a4Sbasabi 	if (dir == NULL) {
4817c478bd9Sstevel@tonic-gate 		/* set homedir to home directory made from base_dir */
4820523b0a4Sbasabi 		(void) sprintf(homedir, "%s/%s", base_dir, logname);
4837c478bd9Sstevel@tonic-gate 
4847c478bd9Sstevel@tonic-gate 	} else if (REL_PATH(dir)) {
4857c478bd9Sstevel@tonic-gate 		errmsg(M_RELPATH, dir);
4867c478bd9Sstevel@tonic-gate 		exit(EX_BADARG);
4877c478bd9Sstevel@tonic-gate 
4887c478bd9Sstevel@tonic-gate 	} else
4897c478bd9Sstevel@tonic-gate 		(void) strcpy(homedir, dir);
4907c478bd9Sstevel@tonic-gate 
4917c478bd9Sstevel@tonic-gate 	if (mflag) {
4927c478bd9Sstevel@tonic-gate 		/* Does home dir. already exist? */
4937c478bd9Sstevel@tonic-gate 		if (stat(homedir, &statbuf) == 0) {
4947c478bd9Sstevel@tonic-gate 			/* directory exists - don't try to create */
4957c478bd9Sstevel@tonic-gate 			mflag = 0;
4967c478bd9Sstevel@tonic-gate 
4977c478bd9Sstevel@tonic-gate 			if (check_perm(statbuf, uid, gid, S_IXOTH) != 0)
4987c478bd9Sstevel@tonic-gate 				errmsg(M_NO_PERM, logname, homedir);
4997c478bd9Sstevel@tonic-gate 		}
5007c478bd9Sstevel@tonic-gate 	}
5010523b0a4Sbasabi 	/*
5020523b0a4Sbasabi 	 * if shell, skel_dir are provided, check their validity.
5030523b0a4Sbasabi 	 * Otherwise default.
5040523b0a4Sbasabi 	 */
5050523b0a4Sbasabi 	if (shell != NULL)
5060523b0a4Sbasabi 		valid_input(SHELL, shell);
5070523b0a4Sbasabi 	else
5080523b0a4Sbasabi 		shell = usrdefs->defshell;
5097c478bd9Sstevel@tonic-gate 
5100523b0a4Sbasabi 	if (skel_dir != NULL)
5110523b0a4Sbasabi 		valid_input(SKELDIR, skel_dir);
5120523b0a4Sbasabi 	else
5130523b0a4Sbasabi 		skel_dir = usrdefs->defskel;
5147c478bd9Sstevel@tonic-gate 
5150523b0a4Sbasabi 	if (inactstr != NULL) {
5167c478bd9Sstevel@tonic-gate 		/* convert inactstr to integer */
5177c478bd9Sstevel@tonic-gate 		inact = strtol(inactstr, &ptr, 10);
5187c478bd9Sstevel@tonic-gate 		if (*ptr || inact < 0) {
5197c478bd9Sstevel@tonic-gate 			errmsg(M_INVALID, inactstr, "inactivity period");
5207c478bd9Sstevel@tonic-gate 			exit(EX_BADARG);
5217c478bd9Sstevel@tonic-gate 		}
5227c478bd9Sstevel@tonic-gate 	} else inact = usrdefs->definact;
5237c478bd9Sstevel@tonic-gate 
5247c478bd9Sstevel@tonic-gate 	/* expiration string is a date, newer than today */
5250523b0a4Sbasabi 	if (expirestr != NULL) {
5267c478bd9Sstevel@tonic-gate 		if (*expirestr) {
5277c478bd9Sstevel@tonic-gate 			if (valid_expire(expirestr, (time_t *)0) == INVALID) {
5287c478bd9Sstevel@tonic-gate 				errmsg(M_INVALID, expirestr, "expiration date");
5297c478bd9Sstevel@tonic-gate 				exit(EX_BADARG);
5307c478bd9Sstevel@tonic-gate 			}
5317c478bd9Sstevel@tonic-gate 			usrdefs->defexpire = expirestr;
5327c478bd9Sstevel@tonic-gate 		} else
5337c478bd9Sstevel@tonic-gate 			/* Unset the expiration date */
5347c478bd9Sstevel@tonic-gate 			expirestr = (char *)0;
5357c478bd9Sstevel@tonic-gate 
5367c478bd9Sstevel@tonic-gate 	} else expirestr = usrdefs->defexpire;
5377c478bd9Sstevel@tonic-gate 
5387c478bd9Sstevel@tonic-gate 	import_def(usrdefs);
5397c478bd9Sstevel@tonic-gate 
5407c478bd9Sstevel@tonic-gate 	/* must now call passmgmt */
5417c478bd9Sstevel@tonic-gate 
5427c478bd9Sstevel@tonic-gate 	/* set up arguments to  passmgmt in nargv array */
5437c478bd9Sstevel@tonic-gate 	nargv = malloc((30 + nkeys * 2) * sizeof (char *));
5447c478bd9Sstevel@tonic-gate 	argindex = 0;
5457c478bd9Sstevel@tonic-gate 	nargv[argindex++] = "passmgmt";
5467c478bd9Sstevel@tonic-gate 	nargv[argindex++] = "-a";	/* add */
5477c478bd9Sstevel@tonic-gate 
5480523b0a4Sbasabi 	if (comment != NULL) {
5497c478bd9Sstevel@tonic-gate 		/* comment */
5507c478bd9Sstevel@tonic-gate 		nargv[argindex++] = "-c";
5517c478bd9Sstevel@tonic-gate 		nargv[argindex++] = comment;
5527c478bd9Sstevel@tonic-gate 	}
5537c478bd9Sstevel@tonic-gate 
5547c478bd9Sstevel@tonic-gate 	/* flags for home directory */
5557c478bd9Sstevel@tonic-gate 	nargv[argindex++] = "-h";
5567c478bd9Sstevel@tonic-gate 	nargv[argindex++] = homedir;
5577c478bd9Sstevel@tonic-gate 
5587c478bd9Sstevel@tonic-gate 	/* set gid flag */
5597c478bd9Sstevel@tonic-gate 	nargv[argindex++] = "-g";
560*f48205beScasper 	(void) sprintf(gidstring, "%u", gid);
5617c478bd9Sstevel@tonic-gate 	nargv[argindex++] = gidstring;
5627c478bd9Sstevel@tonic-gate 
5637c478bd9Sstevel@tonic-gate 	/* shell */
5647c478bd9Sstevel@tonic-gate 	nargv[argindex++] = "-s";
5657c478bd9Sstevel@tonic-gate 	nargv[argindex++] = shell;
5667c478bd9Sstevel@tonic-gate 
5677c478bd9Sstevel@tonic-gate 	/* set inactive */
5687c478bd9Sstevel@tonic-gate 	nargv[argindex++] = "-f";
5697c478bd9Sstevel@tonic-gate 	(void) sprintf(inactstring, "%ld", inact);
5707c478bd9Sstevel@tonic-gate 	nargv[argindex++] = inactstring;
5717c478bd9Sstevel@tonic-gate 
5727c478bd9Sstevel@tonic-gate 	/* set expiration date */
5730523b0a4Sbasabi 	if (expirestr != NULL) {
5747c478bd9Sstevel@tonic-gate 		nargv[argindex++] = "-e";
5757c478bd9Sstevel@tonic-gate 		nargv[argindex++] = expirestr;
5767c478bd9Sstevel@tonic-gate 	}
5777c478bd9Sstevel@tonic-gate 
5787c478bd9Sstevel@tonic-gate 	/* set uid flag */
5797c478bd9Sstevel@tonic-gate 	nargv[argindex++] = "-u";
580*f48205beScasper 	(void) sprintf(uidstring, "%u", uid);
5817c478bd9Sstevel@tonic-gate 	nargv[argindex++] = uidstring;
5827c478bd9Sstevel@tonic-gate 
5837c478bd9Sstevel@tonic-gate 	if (oflag) nargv[argindex++] = "-o";
5847c478bd9Sstevel@tonic-gate 
5857c478bd9Sstevel@tonic-gate 	if (nkeys > 1)
5867c478bd9Sstevel@tonic-gate 		addkey_args(nargv, &argindex);
5877c478bd9Sstevel@tonic-gate 
5887c478bd9Sstevel@tonic-gate 	/* finally - login name */
5897c478bd9Sstevel@tonic-gate 	nargv[argindex++] = logname;
5907c478bd9Sstevel@tonic-gate 
5917c478bd9Sstevel@tonic-gate 	/* set the last to null */
5927c478bd9Sstevel@tonic-gate 	nargv[argindex++] = NULL;
5937c478bd9Sstevel@tonic-gate 
5947c478bd9Sstevel@tonic-gate 	/* now call passmgmt */
5957c478bd9Sstevel@tonic-gate 	ret = PEX_FAILED;
5967c478bd9Sstevel@tonic-gate 	/*
5977c478bd9Sstevel@tonic-gate 	 * If call_passmgmt fails for any reason other than PEX_BADUID, exit
5987c478bd9Sstevel@tonic-gate 	 * is invoked with an appropriate error message. If PEX_BADUID is
5997c478bd9Sstevel@tonic-gate 	 * returned, then if the user specified the ID, exit is invoked
6007c478bd9Sstevel@tonic-gate 	 * with an appropriate error message. Otherwise we try to pick a
6017c478bd9Sstevel@tonic-gate 	 * different ID and try again. If we run out of IDs, i.e. no more
6027c478bd9Sstevel@tonic-gate 	 * users can be created, then -1 is returned and we terminate via exit.
6037c478bd9Sstevel@tonic-gate 	 * If PEX_BUSY is returned we increment a count, since we will stop
6047c478bd9Sstevel@tonic-gate 	 * trying if PEX_BUSY reaches 3. For PEX_SUCCESS we immediately
6057c478bd9Sstevel@tonic-gate 	 * terminate the loop.
6067c478bd9Sstevel@tonic-gate 	 */
6077c478bd9Sstevel@tonic-gate 	while (busy < 3 && ret != PEX_SUCCESS) {
6087c478bd9Sstevel@tonic-gate 		switch (ret = call_passmgmt(nargv)) {
6097c478bd9Sstevel@tonic-gate 		case PEX_SUCCESS:
6107c478bd9Sstevel@tonic-gate 			break;
6117c478bd9Sstevel@tonic-gate 		case PEX_BUSY:
6127c478bd9Sstevel@tonic-gate 			busy++;
6137c478bd9Sstevel@tonic-gate 			break;
6147c478bd9Sstevel@tonic-gate 		case PEX_HOSED_FILES:
6157c478bd9Sstevel@tonic-gate 			errmsg(M_HOSED_FILES);
6167c478bd9Sstevel@tonic-gate 			exit(EX_INCONSISTENT);
6177c478bd9Sstevel@tonic-gate 			break;
6187c478bd9Sstevel@tonic-gate 
6197c478bd9Sstevel@tonic-gate 		case PEX_SYNTAX:
6207c478bd9Sstevel@tonic-gate 		case PEX_BADARG:
6217c478bd9Sstevel@tonic-gate 			/* should NEVER occur that passmgmt usage is wrong */
6227c478bd9Sstevel@tonic-gate 			if (is_role(usertype))
6237c478bd9Sstevel@tonic-gate 				errmsg(M_ARUSAGE);
6247c478bd9Sstevel@tonic-gate 			else
6257c478bd9Sstevel@tonic-gate 				errmsg(M_AUSAGE);
6267c478bd9Sstevel@tonic-gate 			exit(EX_SYNTAX);
6277c478bd9Sstevel@tonic-gate 			break;
6287c478bd9Sstevel@tonic-gate 
6297c478bd9Sstevel@tonic-gate 		case PEX_BADUID:
6307c478bd9Sstevel@tonic-gate 			/*
6317c478bd9Sstevel@tonic-gate 			 * The uid has been taken. If it was specified by a
6327c478bd9Sstevel@tonic-gate 			 * user, then we must fail. Otherwise, keep trying
6337c478bd9Sstevel@tonic-gate 			 * to get a good uid until we run out of IDs.
6347c478bd9Sstevel@tonic-gate 			 */
6357c478bd9Sstevel@tonic-gate 			if (uidstr != NULL) {
6367c478bd9Sstevel@tonic-gate 				errmsg(M_UID_USED, uid);
6377c478bd9Sstevel@tonic-gate 				exit(EX_ID_EXISTS);
6387c478bd9Sstevel@tonic-gate 			} else {
6397c478bd9Sstevel@tonic-gate 				if ((uid = findnextuid()) < 0) {
6407c478bd9Sstevel@tonic-gate 					errmsg(M_INVALID, "default id",
6417c478bd9Sstevel@tonic-gate 					    "user id");
6427c478bd9Sstevel@tonic-gate 					exit(EX_ID_EXISTS);
6437c478bd9Sstevel@tonic-gate 				}
644*f48205beScasper 				(void) sprintf(uidstring, "%u", uid);
6457c478bd9Sstevel@tonic-gate 			}
6467c478bd9Sstevel@tonic-gate 			break;
6477c478bd9Sstevel@tonic-gate 
6487c478bd9Sstevel@tonic-gate 		case PEX_BADNAME:
6497c478bd9Sstevel@tonic-gate 			/* invalid loname */
6507c478bd9Sstevel@tonic-gate 			errmsg(M_USED, logname);
6517c478bd9Sstevel@tonic-gate 			exit(EX_NAME_EXISTS);
6527c478bd9Sstevel@tonic-gate 			break;
6537c478bd9Sstevel@tonic-gate 
6547c478bd9Sstevel@tonic-gate 		default:
6557c478bd9Sstevel@tonic-gate 			errmsg(M_UPDATE, "created");
6567c478bd9Sstevel@tonic-gate 			exit(ret);
6577c478bd9Sstevel@tonic-gate 			break;
6587c478bd9Sstevel@tonic-gate 		}
6597c478bd9Sstevel@tonic-gate 	}
6607c478bd9Sstevel@tonic-gate 	if (busy == 3) {
6617c478bd9Sstevel@tonic-gate 		errmsg(M_UPDATE, "created");
6627c478bd9Sstevel@tonic-gate 		exit(ret);
6637c478bd9Sstevel@tonic-gate 	}
6647c478bd9Sstevel@tonic-gate 
6657c478bd9Sstevel@tonic-gate 	/* add group entry */
6660523b0a4Sbasabi 	if ((grps != NULL) && edit_group(logname, (char *)0, gidlist, 0)) {
6677c478bd9Sstevel@tonic-gate 		errmsg(M_UPDATE, "created");
6687c478bd9Sstevel@tonic-gate 		cleanup(logname);
6697c478bd9Sstevel@tonic-gate 		exit(EX_UPDATE);
6707c478bd9Sstevel@tonic-gate 	}
6717c478bd9Sstevel@tonic-gate 
6727c478bd9Sstevel@tonic-gate 	/* update project database */
6730523b0a4Sbasabi 	if ((projects != NULL) &&
6740523b0a4Sbasabi 	    edit_project(logname, (char *)NULL, projlist, 0)) {
6757c478bd9Sstevel@tonic-gate 		errmsg(M_UPDATE, "created");
6767c478bd9Sstevel@tonic-gate 		cleanup(logname);
6777c478bd9Sstevel@tonic-gate 		exit(EX_UPDATE);
6787c478bd9Sstevel@tonic-gate 	}
6797c478bd9Sstevel@tonic-gate 
6807c478bd9Sstevel@tonic-gate 	/* create home directory */
6817c478bd9Sstevel@tonic-gate 	if (mflag &&
6827c478bd9Sstevel@tonic-gate 	    (create_home(homedir, skel_dir, uid, gid) != EX_SUCCESS)) {
6837c478bd9Sstevel@tonic-gate 		(void) edit_group(logname, (char *)0, (int **)0, 1);
6847c478bd9Sstevel@tonic-gate 		cleanup(logname);
6857c478bd9Sstevel@tonic-gate 		exit(EX_HOMEDIR);
6867c478bd9Sstevel@tonic-gate 	}
6877c478bd9Sstevel@tonic-gate 
6887c478bd9Sstevel@tonic-gate 	return (ret);
6897c478bd9Sstevel@tonic-gate }
6907c478bd9Sstevel@tonic-gate 
6917c478bd9Sstevel@tonic-gate static void
6927c478bd9Sstevel@tonic-gate cleanup(logname)
6937c478bd9Sstevel@tonic-gate char *logname;
6947c478bd9Sstevel@tonic-gate {
6957c478bd9Sstevel@tonic-gate 	char *nargv[4];
6967c478bd9Sstevel@tonic-gate 
6977c478bd9Sstevel@tonic-gate 	nargv[0] = "passmgmt";
6987c478bd9Sstevel@tonic-gate 	nargv[1] = "-d";
6997c478bd9Sstevel@tonic-gate 	nargv[2] = logname;
7007c478bd9Sstevel@tonic-gate 	nargv[3] = NULL;
7017c478bd9Sstevel@tonic-gate 
7027c478bd9Sstevel@tonic-gate 	switch (call_passmgmt(nargv)) {
7037c478bd9Sstevel@tonic-gate 	case PEX_SUCCESS:
7047c478bd9Sstevel@tonic-gate 		break;
7057c478bd9Sstevel@tonic-gate 
7067c478bd9Sstevel@tonic-gate 	case PEX_SYNTAX:
7077c478bd9Sstevel@tonic-gate 		/* should NEVER occur that passmgmt usage is wrong */
7087c478bd9Sstevel@tonic-gate 		if (is_role(usertype))
7097c478bd9Sstevel@tonic-gate 			errmsg(M_ARUSAGE);
7107c478bd9Sstevel@tonic-gate 		else
7117c478bd9Sstevel@tonic-gate 			errmsg(M_AUSAGE);
7127c478bd9Sstevel@tonic-gate 		break;
7137c478bd9Sstevel@tonic-gate 
7147c478bd9Sstevel@tonic-gate 	case PEX_BADUID:
7157c478bd9Sstevel@tonic-gate 		/* uid is used - shouldn't happen but print message anyway */
7167c478bd9Sstevel@tonic-gate 		errmsg(M_UID_USED, uid);
7177c478bd9Sstevel@tonic-gate 		break;
7187c478bd9Sstevel@tonic-gate 
7197c478bd9Sstevel@tonic-gate 	case PEX_BADNAME:
7207c478bd9Sstevel@tonic-gate 		/* invalid loname */
7217c478bd9Sstevel@tonic-gate 		errmsg(M_USED, logname);
7227c478bd9Sstevel@tonic-gate 		break;
7237c478bd9Sstevel@tonic-gate 
7247c478bd9Sstevel@tonic-gate 	default:
7257c478bd9Sstevel@tonic-gate 		errmsg(M_UPDATE, "created");
7267c478bd9Sstevel@tonic-gate 		break;
7277c478bd9Sstevel@tonic-gate 	}
7287c478bd9Sstevel@tonic-gate }
7290523b0a4Sbasabi 
7300523b0a4Sbasabi /* Check the validity for shell, base_dir and skel_dir */
7310523b0a4Sbasabi 
7320523b0a4Sbasabi void
7330523b0a4Sbasabi valid_input(path_opt_t opt, const char *input)
7340523b0a4Sbasabi {
7350523b0a4Sbasabi 	struct stat	statbuf;
7360523b0a4Sbasabi 
7370523b0a4Sbasabi 	if (REL_PATH(input)) {
7380523b0a4Sbasabi 		errmsg(M_RELPATH, input);
7390523b0a4Sbasabi 		exit(EX_BADARG);
7400523b0a4Sbasabi 	}
7410523b0a4Sbasabi 	if (stat(input, &statbuf) == -1) {
7420523b0a4Sbasabi 		errmsg(M_INVALID, input, "path");
7430523b0a4Sbasabi 		exit(EX_BADARG);
7440523b0a4Sbasabi 	}
7450523b0a4Sbasabi 	if (opt == SHELL) {
7460523b0a4Sbasabi 		if (!S_ISREG(statbuf.st_mode) ||
7470523b0a4Sbasabi 		    (statbuf.st_mode & 0555) != 0555) {
7480523b0a4Sbasabi 			errmsg(M_INVALID, input, "shell");
7490523b0a4Sbasabi 			exit(EX_BADARG);
7500523b0a4Sbasabi 		}
7510523b0a4Sbasabi 	} else {
7520523b0a4Sbasabi 		if (!S_ISDIR(statbuf.st_mode)) {
7530523b0a4Sbasabi 			errmsg(M_INVALID, input, "directory");
7540523b0a4Sbasabi 			exit(EX_BADARG);
7550523b0a4Sbasabi 		}
7560523b0a4Sbasabi 	}
7570523b0a4Sbasabi }
758