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