10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 51639Sbasabi * Common Development and Distribution License (the "License"). 61639Sbasabi * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 210Sstevel@tonic-gate /* 22*4321Scasper * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 230Sstevel@tonic-gate * Use is subject to license terms. 240Sstevel@tonic-gate */ 250Sstevel@tonic-gate 260Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 270Sstevel@tonic-gate /* All Rights Reserved */ 280Sstevel@tonic-gate 290Sstevel@tonic-gate 300Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 310Sstevel@tonic-gate 320Sstevel@tonic-gate #include <sys/types.h> 330Sstevel@tonic-gate #include <sys/stat.h> 340Sstevel@tonic-gate #include <sys/param.h> 350Sstevel@tonic-gate #include <stdio.h> 360Sstevel@tonic-gate #include <stdlib.h> 370Sstevel@tonic-gate #include <ctype.h> 380Sstevel@tonic-gate #include <limits.h> 390Sstevel@tonic-gate #include <string.h> 400Sstevel@tonic-gate #include <userdefs.h> 410Sstevel@tonic-gate #include <errno.h> 420Sstevel@tonic-gate #include <project.h> 430Sstevel@tonic-gate #include <unistd.h> 440Sstevel@tonic-gate #include <user_attr.h> 450Sstevel@tonic-gate #include "users.h" 460Sstevel@tonic-gate #include "messages.h" 470Sstevel@tonic-gate #include "userdisp.h" 480Sstevel@tonic-gate #include "funcs.h" 490Sstevel@tonic-gate 500Sstevel@tonic-gate /* 510Sstevel@tonic-gate * useradd [-u uid [-o] | -g group | -G group [[, group]...] | -d dir [-m] 521639Sbasabi * | -s shell | -c comment | -k skel_dir | -b base_dir] ] 530Sstevel@tonic-gate * [ -A authorization [, authorization ...]] 540Sstevel@tonic-gate * [ -P profile [, profile ...]] 550Sstevel@tonic-gate * [ -K key=value ] 560Sstevel@tonic-gate * [ -R role [, role ...]] [-p project [, project ...]] login 571639Sbasabi * useradd -D [ -g group ] [ -b base_dir | -f inactive | -e expire | 581639Sbasabi * -s shell | -k skel_dir ] 590Sstevel@tonic-gate * [ -A authorization [, authorization ...]] 600Sstevel@tonic-gate * [ -P profile [, profile ...]] [ -K key=value ] 610Sstevel@tonic-gate * [ -R role [, role ...]] [-p project [, project ...]] login 620Sstevel@tonic-gate * 630Sstevel@tonic-gate * This command adds new user logins to the system. Arguments are: 640Sstevel@tonic-gate * 650Sstevel@tonic-gate * uid - an integer 660Sstevel@tonic-gate * group - an existing group's integer ID or char string name 670Sstevel@tonic-gate * dir - home directory 680Sstevel@tonic-gate * shell - a program to be used as a shell 690Sstevel@tonic-gate * comment - any text string 700Sstevel@tonic-gate * skel_dir - a skeleton directory 710Sstevel@tonic-gate * base_dir - a directory 720Sstevel@tonic-gate * login - a string of printable chars except colon(:) 730Sstevel@tonic-gate * authorization - One or more comma separated authorizations defined 740Sstevel@tonic-gate * in auth_attr(4). 750Sstevel@tonic-gate * profile - One or more comma separated execution profiles defined 760Sstevel@tonic-gate * in prof_attr(4) 770Sstevel@tonic-gate * role - One or more comma-separated role names defined in user_attr(4) 780Sstevel@tonic-gate * project - One or more comma-separated project names or numbers 790Sstevel@tonic-gate * 800Sstevel@tonic-gate */ 810Sstevel@tonic-gate 820Sstevel@tonic-gate extern struct userdefs *getusrdef(); 830Sstevel@tonic-gate extern void dispusrdef(); 840Sstevel@tonic-gate 850Sstevel@tonic-gate static void cleanup(); 860Sstevel@tonic-gate 870Sstevel@tonic-gate extern uid_t findnextuid(void); 880Sstevel@tonic-gate extern int check_perm(), valid_expire(); 890Sstevel@tonic-gate extern int putusrdef(), valid_uid(); 900Sstevel@tonic-gate extern int call_passmgmt(), edit_group(), create_home(); 910Sstevel@tonic-gate extern int edit_project(); 920Sstevel@tonic-gate extern int **valid_lgroup(); 930Sstevel@tonic-gate extern projid_t **valid_lproject(); 940Sstevel@tonic-gate extern void update_def(struct userdefs *); 950Sstevel@tonic-gate extern void import_def(struct userdefs *); 960Sstevel@tonic-gate 970Sstevel@tonic-gate static uid_t uid; /* new uid */ 980Sstevel@tonic-gate static char *logname; /* login name to add */ 990Sstevel@tonic-gate static struct userdefs *usrdefs; /* defaults for useradd */ 1000Sstevel@tonic-gate 1010Sstevel@tonic-gate char *cmdname; 1020Sstevel@tonic-gate 1030Sstevel@tonic-gate static char homedir[ PATH_MAX + 1 ]; /* home directory */ 1040Sstevel@tonic-gate static char gidstring[32]; /* group id string representation */ 1050Sstevel@tonic-gate static gid_t gid; /* gid of new login */ 1060Sstevel@tonic-gate static char uidstring[32]; /* user id string representation */ 1070Sstevel@tonic-gate static char *uidstr = NULL; /* uid from command line */ 1080Sstevel@tonic-gate static char *base_dir = NULL; /* base_dir from command line */ 1090Sstevel@tonic-gate static char *group = NULL; /* group from command line */ 1100Sstevel@tonic-gate static char *grps = NULL; /* multi groups from command line */ 1110Sstevel@tonic-gate static char *dir = NULL; /* home dir from command line */ 1120Sstevel@tonic-gate static char *shell = NULL; /* shell from command line */ 1130Sstevel@tonic-gate static char *comment = NULL; /* comment from command line */ 1140Sstevel@tonic-gate static char *skel_dir = NULL; /* skel dir from command line */ 1150Sstevel@tonic-gate static long inact; /* inactive days */ 1160Sstevel@tonic-gate static char *inactstr = NULL; /* inactive from command line */ 1170Sstevel@tonic-gate static char inactstring[10]; /* inactivity string representation */ 1180Sstevel@tonic-gate static char *expirestr = NULL; /* expiration date from command line */ 1190Sstevel@tonic-gate static char *projects = NULL; /* project id's from command line */ 1200Sstevel@tonic-gate 1210Sstevel@tonic-gate static char *usertype = NULL; /* type of user, either role or normal */ 1220Sstevel@tonic-gate 1231639Sbasabi typedef enum { 1241639Sbasabi BASEDIR = 0, 1251639Sbasabi SKELDIR, 1261639Sbasabi SHELL 1271639Sbasabi } path_opt_t; 1281639Sbasabi 1291639Sbasabi 1301639Sbasabi static void valid_input(path_opt_t, const char *); 1311639Sbasabi 1320Sstevel@tonic-gate int 1330Sstevel@tonic-gate main(argc, argv) 1340Sstevel@tonic-gate int argc; 1350Sstevel@tonic-gate char *argv[]; 1360Sstevel@tonic-gate { 1370Sstevel@tonic-gate int ch, ret, mflag = 0, oflag = 0, Dflag = 0, **gidlist; 1380Sstevel@tonic-gate projid_t **projlist; 1390Sstevel@tonic-gate char *ptr; /* loc in a str, may be set by strtol */ 1400Sstevel@tonic-gate struct group *g_ptr; 1410Sstevel@tonic-gate struct project p_ptr; 1420Sstevel@tonic-gate char mybuf[PROJECT_BUFSZ]; 1430Sstevel@tonic-gate struct stat statbuf; /* status buffer for stat */ 1440Sstevel@tonic-gate int warning; 1450Sstevel@tonic-gate int busy = 0; 1460Sstevel@tonic-gate char **nargv; /* arguments for execvp of passmgmt */ 1470Sstevel@tonic-gate int argindex; /* argument index into nargv */ 1480Sstevel@tonic-gate 1490Sstevel@tonic-gate cmdname = argv[0]; 1500Sstevel@tonic-gate 1510Sstevel@tonic-gate if (geteuid() != 0) { 1520Sstevel@tonic-gate errmsg(M_PERM_DENIED); 1530Sstevel@tonic-gate exit(EX_NO_PERM); 1540Sstevel@tonic-gate } 1550Sstevel@tonic-gate 1560Sstevel@tonic-gate opterr = 0; /* no print errors from getopt */ 1570Sstevel@tonic-gate usertype = getusertype(argv[0]); 1580Sstevel@tonic-gate 1590Sstevel@tonic-gate change_key(USERATTR_TYPE_KW, usertype); 1600Sstevel@tonic-gate 1610Sstevel@tonic-gate while ((ch = getopt(argc, argv, 1620Sstevel@tonic-gate "b:c:Dd:e:f:G:g:k:mop:s:u:A:P:R:K:")) != EOF) 1630Sstevel@tonic-gate switch (ch) { 1640Sstevel@tonic-gate case 'b': 1650Sstevel@tonic-gate base_dir = optarg; 1660Sstevel@tonic-gate break; 1670Sstevel@tonic-gate 1680Sstevel@tonic-gate case 'c': 1690Sstevel@tonic-gate comment = optarg; 1700Sstevel@tonic-gate break; 1710Sstevel@tonic-gate 1720Sstevel@tonic-gate case 'D': 1730Sstevel@tonic-gate Dflag++; 1740Sstevel@tonic-gate break; 1750Sstevel@tonic-gate 1760Sstevel@tonic-gate case 'd': 1770Sstevel@tonic-gate dir = optarg; 1780Sstevel@tonic-gate break; 1790Sstevel@tonic-gate 1800Sstevel@tonic-gate case 'e': 1810Sstevel@tonic-gate expirestr = optarg; 1820Sstevel@tonic-gate break; 1830Sstevel@tonic-gate 1840Sstevel@tonic-gate case 'f': 1850Sstevel@tonic-gate inactstr = optarg; 1860Sstevel@tonic-gate break; 1870Sstevel@tonic-gate 1880Sstevel@tonic-gate case 'G': 1890Sstevel@tonic-gate grps = optarg; 1900Sstevel@tonic-gate break; 1910Sstevel@tonic-gate 1920Sstevel@tonic-gate case 'g': 1930Sstevel@tonic-gate group = optarg; 1940Sstevel@tonic-gate break; 1950Sstevel@tonic-gate 1960Sstevel@tonic-gate case 'k': 1970Sstevel@tonic-gate skel_dir = optarg; 1980Sstevel@tonic-gate break; 1990Sstevel@tonic-gate 2000Sstevel@tonic-gate case 'm': 2010Sstevel@tonic-gate mflag++; 2020Sstevel@tonic-gate break; 2030Sstevel@tonic-gate 2040Sstevel@tonic-gate case 'o': 2050Sstevel@tonic-gate oflag++; 2060Sstevel@tonic-gate break; 2070Sstevel@tonic-gate 2080Sstevel@tonic-gate case 'p': 2090Sstevel@tonic-gate projects = optarg; 2100Sstevel@tonic-gate break; 2110Sstevel@tonic-gate 2120Sstevel@tonic-gate case 's': 2130Sstevel@tonic-gate shell = optarg; 2140Sstevel@tonic-gate break; 2150Sstevel@tonic-gate 2160Sstevel@tonic-gate case 'u': 2170Sstevel@tonic-gate uidstr = optarg; 2180Sstevel@tonic-gate break; 2190Sstevel@tonic-gate 2200Sstevel@tonic-gate case 'A': 2210Sstevel@tonic-gate change_key(USERATTR_AUTHS_KW, optarg); 2220Sstevel@tonic-gate break; 2230Sstevel@tonic-gate 2240Sstevel@tonic-gate case 'P': 2250Sstevel@tonic-gate change_key(USERATTR_PROFILES_KW, optarg); 2260Sstevel@tonic-gate break; 2270Sstevel@tonic-gate 2280Sstevel@tonic-gate case 'R': 2290Sstevel@tonic-gate if (is_role(usertype)) { 2300Sstevel@tonic-gate errmsg(M_ARUSAGE); 2310Sstevel@tonic-gate exit(EX_SYNTAX); 2320Sstevel@tonic-gate } 2330Sstevel@tonic-gate change_key(USERATTR_ROLES_KW, optarg); 2340Sstevel@tonic-gate break; 2350Sstevel@tonic-gate 2360Sstevel@tonic-gate case 'K': 2370Sstevel@tonic-gate change_key(NULL, optarg); 2380Sstevel@tonic-gate break; 2390Sstevel@tonic-gate 2400Sstevel@tonic-gate default: 2410Sstevel@tonic-gate case '?': 2420Sstevel@tonic-gate if (is_role(usertype)) 2430Sstevel@tonic-gate errmsg(M_ARUSAGE); 2440Sstevel@tonic-gate else 2450Sstevel@tonic-gate errmsg(M_AUSAGE); 2460Sstevel@tonic-gate exit(EX_SYNTAX); 2470Sstevel@tonic-gate } 2480Sstevel@tonic-gate 2490Sstevel@tonic-gate /* get defaults for adding new users */ 2500Sstevel@tonic-gate usrdefs = getusrdef(usertype); 2510Sstevel@tonic-gate 2520Sstevel@tonic-gate if (Dflag) { 2530Sstevel@tonic-gate /* DISPLAY mode */ 2540Sstevel@tonic-gate 2550Sstevel@tonic-gate /* check syntax */ 2560Sstevel@tonic-gate if (optind != argc) { 2570Sstevel@tonic-gate if (is_role(usertype)) 2580Sstevel@tonic-gate errmsg(M_ARUSAGE); 2590Sstevel@tonic-gate else 2600Sstevel@tonic-gate errmsg(M_AUSAGE); 2610Sstevel@tonic-gate exit(EX_SYNTAX); 2620Sstevel@tonic-gate } 2630Sstevel@tonic-gate 2641639Sbasabi if (uidstr != NULL || oflag || grps != NULL || 2651639Sbasabi dir != NULL || mflag || comment != NULL) { 2660Sstevel@tonic-gate if (is_role(usertype)) 2670Sstevel@tonic-gate errmsg(M_ARUSAGE); 2680Sstevel@tonic-gate else 2690Sstevel@tonic-gate errmsg(M_AUSAGE); 2700Sstevel@tonic-gate exit(EX_SYNTAX); 2710Sstevel@tonic-gate } 2720Sstevel@tonic-gate 2730Sstevel@tonic-gate /* Group must be an existing group */ 2741639Sbasabi if (group != NULL) { 2750Sstevel@tonic-gate switch (valid_group(group, &g_ptr, &warning)) { 2760Sstevel@tonic-gate case INVALID: 2770Sstevel@tonic-gate errmsg(M_INVALID, group, "group id"); 2780Sstevel@tonic-gate exit(EX_BADARG); 2790Sstevel@tonic-gate /*NOTREACHED*/ 2800Sstevel@tonic-gate case TOOBIG: 2810Sstevel@tonic-gate errmsg(M_TOOBIG, "gid", group); 2820Sstevel@tonic-gate exit(EX_BADARG); 2830Sstevel@tonic-gate /*NOTREACHED*/ 2840Sstevel@tonic-gate case RESERVED: 2850Sstevel@tonic-gate case UNIQUE: 2860Sstevel@tonic-gate errmsg(M_GRP_NOTUSED, group); 2870Sstevel@tonic-gate exit(EX_NAME_NOT_EXIST); 2880Sstevel@tonic-gate } 2890Sstevel@tonic-gate if (warning) 2900Sstevel@tonic-gate warningmsg(warning, group); 2910Sstevel@tonic-gate 2920Sstevel@tonic-gate usrdefs->defgroup = g_ptr->gr_gid; 2930Sstevel@tonic-gate usrdefs->defgname = g_ptr->gr_name; 2940Sstevel@tonic-gate 2950Sstevel@tonic-gate } 2960Sstevel@tonic-gate 2970Sstevel@tonic-gate /* project must be an existing project */ 2981639Sbasabi if (projects != NULL) { 2990Sstevel@tonic-gate switch (valid_project(projects, &p_ptr, mybuf, 3000Sstevel@tonic-gate sizeof (mybuf), &warning)) { 3010Sstevel@tonic-gate case INVALID: 3020Sstevel@tonic-gate errmsg(M_INVALID, projects, "project id"); 3030Sstevel@tonic-gate exit(EX_BADARG); 3040Sstevel@tonic-gate /*NOTREACHED*/ 3050Sstevel@tonic-gate case TOOBIG: 3060Sstevel@tonic-gate errmsg(M_TOOBIG, "projid", projects); 3070Sstevel@tonic-gate exit(EX_BADARG); 3080Sstevel@tonic-gate /*NOTREACHED*/ 3090Sstevel@tonic-gate case UNIQUE: 3100Sstevel@tonic-gate errmsg(M_PROJ_NOTUSED, projects); 3110Sstevel@tonic-gate exit(EX_NAME_NOT_EXIST); 3120Sstevel@tonic-gate } 3130Sstevel@tonic-gate if (warning) 3140Sstevel@tonic-gate warningmsg(warning, projects); 3150Sstevel@tonic-gate 3160Sstevel@tonic-gate usrdefs->defproj = p_ptr.pj_projid; 3170Sstevel@tonic-gate usrdefs->defprojname = p_ptr.pj_name; 3180Sstevel@tonic-gate } 3190Sstevel@tonic-gate 3200Sstevel@tonic-gate /* base_dir must be an existing directory */ 3211639Sbasabi if (base_dir != NULL) { 3221639Sbasabi valid_input(BASEDIR, base_dir); 3230Sstevel@tonic-gate usrdefs->defparent = base_dir; 3240Sstevel@tonic-gate } 3250Sstevel@tonic-gate 3260Sstevel@tonic-gate /* inactivity period is an integer */ 3271639Sbasabi if (inactstr != NULL) { 3280Sstevel@tonic-gate /* convert inactstr to integer */ 3290Sstevel@tonic-gate inact = strtol(inactstr, &ptr, 10); 3300Sstevel@tonic-gate if (*ptr || inact < 0) { 3310Sstevel@tonic-gate errmsg(M_INVALID, inactstr, 3320Sstevel@tonic-gate "inactivity period"); 3330Sstevel@tonic-gate exit(EX_BADARG); 3340Sstevel@tonic-gate } 3350Sstevel@tonic-gate 3360Sstevel@tonic-gate usrdefs->definact = inact; 3370Sstevel@tonic-gate } 3380Sstevel@tonic-gate 3390Sstevel@tonic-gate /* expiration string is a date, newer than today */ 3401639Sbasabi if (expirestr != NULL) { 3410Sstevel@tonic-gate if (*expirestr) { 3420Sstevel@tonic-gate if (valid_expire(expirestr, (time_t *)0) 3430Sstevel@tonic-gate == INVALID) { 3440Sstevel@tonic-gate errmsg(M_INVALID, expirestr, 3450Sstevel@tonic-gate "expiration date"); 3460Sstevel@tonic-gate exit(EX_BADARG); 3470Sstevel@tonic-gate } 3480Sstevel@tonic-gate usrdefs->defexpire = expirestr; 3490Sstevel@tonic-gate } else 3500Sstevel@tonic-gate /* Unset the expiration date */ 3510Sstevel@tonic-gate usrdefs->defexpire = ""; 3520Sstevel@tonic-gate } 3530Sstevel@tonic-gate 3541639Sbasabi if (shell != NULL) { 3551639Sbasabi valid_input(SHELL, shell); 3561639Sbasabi usrdefs->defshell = shell; 3571639Sbasabi } 3581639Sbasabi if (skel_dir != NULL) { 3591639Sbasabi valid_input(SKELDIR, skel_dir); 3601639Sbasabi usrdefs->defskel = skel_dir; 3611639Sbasabi } 3620Sstevel@tonic-gate update_def(usrdefs); 3630Sstevel@tonic-gate 3640Sstevel@tonic-gate /* change defaults for useradd */ 3650Sstevel@tonic-gate if (putusrdef(usrdefs, usertype) < 0) { 3660Sstevel@tonic-gate errmsg(M_UPDATE, "created"); 3670Sstevel@tonic-gate exit(EX_UPDATE); 3680Sstevel@tonic-gate } 3690Sstevel@tonic-gate 3700Sstevel@tonic-gate /* Now, display */ 3710Sstevel@tonic-gate dispusrdef(stdout, (D_ALL & ~D_RID), usertype); 3720Sstevel@tonic-gate exit(EX_SUCCESS); 3730Sstevel@tonic-gate 3740Sstevel@tonic-gate } 3750Sstevel@tonic-gate 3760Sstevel@tonic-gate /* ADD mode */ 3770Sstevel@tonic-gate 3780Sstevel@tonic-gate /* check syntax */ 3791639Sbasabi if (optind != argc - 1 || (skel_dir != NULL && !mflag)) { 3800Sstevel@tonic-gate if (is_role(usertype)) 3810Sstevel@tonic-gate errmsg(M_ARUSAGE); 3820Sstevel@tonic-gate else 3830Sstevel@tonic-gate errmsg(M_AUSAGE); 3840Sstevel@tonic-gate exit(EX_SYNTAX); 3850Sstevel@tonic-gate } 3860Sstevel@tonic-gate 3870Sstevel@tonic-gate logname = argv[optind]; 3880Sstevel@tonic-gate switch (valid_login(logname, (struct passwd **)NULL, &warning)) { 3890Sstevel@tonic-gate case INVALID: 3900Sstevel@tonic-gate errmsg(M_INVALID, logname, "login name"); 3910Sstevel@tonic-gate exit(EX_BADARG); 3920Sstevel@tonic-gate /*NOTREACHED*/ 3930Sstevel@tonic-gate 3940Sstevel@tonic-gate case NOTUNIQUE: 3950Sstevel@tonic-gate errmsg(M_USED, logname); 3960Sstevel@tonic-gate exit(EX_NAME_EXISTS); 3970Sstevel@tonic-gate /*NOTREACHED*/ 3980Sstevel@tonic-gate } 3990Sstevel@tonic-gate 4000Sstevel@tonic-gate if (warning) 4010Sstevel@tonic-gate warningmsg(warning, logname); 4021639Sbasabi if (uidstr != NULL) { 4030Sstevel@tonic-gate /* convert uidstr to integer */ 4040Sstevel@tonic-gate errno = 0; 4050Sstevel@tonic-gate uid = (uid_t)strtol(uidstr, &ptr, (int)10); 4060Sstevel@tonic-gate if (*ptr || errno == ERANGE) { 4070Sstevel@tonic-gate errmsg(M_INVALID, uidstr, "user id"); 4080Sstevel@tonic-gate exit(EX_BADARG); 4090Sstevel@tonic-gate } 4100Sstevel@tonic-gate 4110Sstevel@tonic-gate switch (valid_uid(uid, NULL)) { 4120Sstevel@tonic-gate case NOTUNIQUE: 4130Sstevel@tonic-gate if (!oflag) { 4140Sstevel@tonic-gate /* override not specified */ 4150Sstevel@tonic-gate errmsg(M_UID_USED, uid); 4160Sstevel@tonic-gate exit(EX_ID_EXISTS); 4170Sstevel@tonic-gate } 4180Sstevel@tonic-gate break; 4190Sstevel@tonic-gate case RESERVED: 4200Sstevel@tonic-gate errmsg(M_RESERVED, uid); 4210Sstevel@tonic-gate break; 4220Sstevel@tonic-gate case TOOBIG: 4230Sstevel@tonic-gate errmsg(M_TOOBIG, "uid", uid); 4240Sstevel@tonic-gate exit(EX_BADARG); 4250Sstevel@tonic-gate break; 4260Sstevel@tonic-gate } 4270Sstevel@tonic-gate 4280Sstevel@tonic-gate } else { 4290Sstevel@tonic-gate 4300Sstevel@tonic-gate if ((uid = findnextuid()) < 0) { 4310Sstevel@tonic-gate errmsg(M_INVALID, "default id", "user id"); 4320Sstevel@tonic-gate exit(EX_ID_EXISTS); 4330Sstevel@tonic-gate } 4340Sstevel@tonic-gate } 4350Sstevel@tonic-gate 4361639Sbasabi if (group != NULL) { 4370Sstevel@tonic-gate switch (valid_group(group, &g_ptr, &warning)) { 4380Sstevel@tonic-gate case INVALID: 4390Sstevel@tonic-gate errmsg(M_INVALID, group, "group id"); 4400Sstevel@tonic-gate exit(EX_BADARG); 4410Sstevel@tonic-gate /*NOTREACHED*/ 4420Sstevel@tonic-gate case TOOBIG: 4430Sstevel@tonic-gate errmsg(M_TOOBIG, "gid", group); 4440Sstevel@tonic-gate exit(EX_BADARG); 4450Sstevel@tonic-gate /*NOTREACHED*/ 4460Sstevel@tonic-gate case RESERVED: 4470Sstevel@tonic-gate case UNIQUE: 4480Sstevel@tonic-gate errmsg(M_GRP_NOTUSED, group); 4490Sstevel@tonic-gate exit(EX_NAME_NOT_EXIST); 4500Sstevel@tonic-gate /*NOTREACHED*/ 4510Sstevel@tonic-gate } 4520Sstevel@tonic-gate 4530Sstevel@tonic-gate if (warning) 4540Sstevel@tonic-gate warningmsg(warning, group); 4550Sstevel@tonic-gate gid = g_ptr->gr_gid; 4560Sstevel@tonic-gate 4570Sstevel@tonic-gate } else gid = usrdefs->defgroup; 4580Sstevel@tonic-gate 4591639Sbasabi if (grps != NULL) { 4600Sstevel@tonic-gate if (!*grps) 4610Sstevel@tonic-gate /* ignore -G "" */ 4620Sstevel@tonic-gate grps = (char *)0; 4630Sstevel@tonic-gate else if (!(gidlist = valid_lgroup(grps, gid))) 4640Sstevel@tonic-gate exit(EX_BADARG); 4650Sstevel@tonic-gate } 4660Sstevel@tonic-gate 4671639Sbasabi if (projects != NULL) { 4680Sstevel@tonic-gate if (! *projects) 4690Sstevel@tonic-gate projects = (char *)0; 4700Sstevel@tonic-gate else if (! (projlist = valid_lproject(projects))) 4710Sstevel@tonic-gate exit(EX_BADARG); 4720Sstevel@tonic-gate } 4730Sstevel@tonic-gate 4741639Sbasabi /* if base_dir is provided, check its validity; otherwise default */ 4751639Sbasabi if (base_dir != NULL) 4761639Sbasabi valid_input(BASEDIR, base_dir); 4771639Sbasabi else 4781639Sbasabi base_dir = usrdefs->defparent; 4791639Sbasabi 4801639Sbasabi if (dir == NULL) { 4810Sstevel@tonic-gate /* set homedir to home directory made from base_dir */ 4821639Sbasabi (void) sprintf(homedir, "%s/%s", base_dir, logname); 4830Sstevel@tonic-gate 4840Sstevel@tonic-gate } else if (REL_PATH(dir)) { 4850Sstevel@tonic-gate errmsg(M_RELPATH, dir); 4860Sstevel@tonic-gate exit(EX_BADARG); 4870Sstevel@tonic-gate 4880Sstevel@tonic-gate } else 4890Sstevel@tonic-gate (void) strcpy(homedir, dir); 4900Sstevel@tonic-gate 4910Sstevel@tonic-gate if (mflag) { 4920Sstevel@tonic-gate /* Does home dir. already exist? */ 4930Sstevel@tonic-gate if (stat(homedir, &statbuf) == 0) { 4940Sstevel@tonic-gate /* directory exists - don't try to create */ 4950Sstevel@tonic-gate mflag = 0; 4960Sstevel@tonic-gate 4970Sstevel@tonic-gate if (check_perm(statbuf, uid, gid, S_IXOTH) != 0) 4980Sstevel@tonic-gate errmsg(M_NO_PERM, logname, homedir); 4990Sstevel@tonic-gate } 5000Sstevel@tonic-gate } 5011639Sbasabi /* 5021639Sbasabi * if shell, skel_dir are provided, check their validity. 5031639Sbasabi * Otherwise default. 5041639Sbasabi */ 5051639Sbasabi if (shell != NULL) 5061639Sbasabi valid_input(SHELL, shell); 5071639Sbasabi else 5081639Sbasabi shell = usrdefs->defshell; 5090Sstevel@tonic-gate 5101639Sbasabi if (skel_dir != NULL) 5111639Sbasabi valid_input(SKELDIR, skel_dir); 5121639Sbasabi else 5131639Sbasabi skel_dir = usrdefs->defskel; 5140Sstevel@tonic-gate 5151639Sbasabi if (inactstr != NULL) { 5160Sstevel@tonic-gate /* convert inactstr to integer */ 5170Sstevel@tonic-gate inact = strtol(inactstr, &ptr, 10); 5180Sstevel@tonic-gate if (*ptr || inact < 0) { 5190Sstevel@tonic-gate errmsg(M_INVALID, inactstr, "inactivity period"); 5200Sstevel@tonic-gate exit(EX_BADARG); 5210Sstevel@tonic-gate } 5220Sstevel@tonic-gate } else inact = usrdefs->definact; 5230Sstevel@tonic-gate 5240Sstevel@tonic-gate /* expiration string is a date, newer than today */ 5251639Sbasabi if (expirestr != NULL) { 5260Sstevel@tonic-gate if (*expirestr) { 5270Sstevel@tonic-gate if (valid_expire(expirestr, (time_t *)0) == INVALID) { 5280Sstevel@tonic-gate errmsg(M_INVALID, expirestr, "expiration date"); 5290Sstevel@tonic-gate exit(EX_BADARG); 5300Sstevel@tonic-gate } 5310Sstevel@tonic-gate usrdefs->defexpire = expirestr; 5320Sstevel@tonic-gate } else 5330Sstevel@tonic-gate /* Unset the expiration date */ 5340Sstevel@tonic-gate expirestr = (char *)0; 5350Sstevel@tonic-gate 5360Sstevel@tonic-gate } else expirestr = usrdefs->defexpire; 5370Sstevel@tonic-gate 5380Sstevel@tonic-gate import_def(usrdefs); 5390Sstevel@tonic-gate 5400Sstevel@tonic-gate /* must now call passmgmt */ 5410Sstevel@tonic-gate 5420Sstevel@tonic-gate /* set up arguments to passmgmt in nargv array */ 5430Sstevel@tonic-gate nargv = malloc((30 + nkeys * 2) * sizeof (char *)); 5440Sstevel@tonic-gate argindex = 0; 5450Sstevel@tonic-gate nargv[argindex++] = "passmgmt"; 5460Sstevel@tonic-gate nargv[argindex++] = "-a"; /* add */ 5470Sstevel@tonic-gate 5481639Sbasabi if (comment != NULL) { 5490Sstevel@tonic-gate /* comment */ 5500Sstevel@tonic-gate nargv[argindex++] = "-c"; 5510Sstevel@tonic-gate nargv[argindex++] = comment; 5520Sstevel@tonic-gate } 5530Sstevel@tonic-gate 5540Sstevel@tonic-gate /* flags for home directory */ 5550Sstevel@tonic-gate nargv[argindex++] = "-h"; 5560Sstevel@tonic-gate nargv[argindex++] = homedir; 5570Sstevel@tonic-gate 5580Sstevel@tonic-gate /* set gid flag */ 5590Sstevel@tonic-gate nargv[argindex++] = "-g"; 560*4321Scasper (void) sprintf(gidstring, "%u", gid); 5610Sstevel@tonic-gate nargv[argindex++] = gidstring; 5620Sstevel@tonic-gate 5630Sstevel@tonic-gate /* shell */ 5640Sstevel@tonic-gate nargv[argindex++] = "-s"; 5650Sstevel@tonic-gate nargv[argindex++] = shell; 5660Sstevel@tonic-gate 5670Sstevel@tonic-gate /* set inactive */ 5680Sstevel@tonic-gate nargv[argindex++] = "-f"; 5690Sstevel@tonic-gate (void) sprintf(inactstring, "%ld", inact); 5700Sstevel@tonic-gate nargv[argindex++] = inactstring; 5710Sstevel@tonic-gate 5720Sstevel@tonic-gate /* set expiration date */ 5731639Sbasabi if (expirestr != NULL) { 5740Sstevel@tonic-gate nargv[argindex++] = "-e"; 5750Sstevel@tonic-gate nargv[argindex++] = expirestr; 5760Sstevel@tonic-gate } 5770Sstevel@tonic-gate 5780Sstevel@tonic-gate /* set uid flag */ 5790Sstevel@tonic-gate nargv[argindex++] = "-u"; 580*4321Scasper (void) sprintf(uidstring, "%u", uid); 5810Sstevel@tonic-gate nargv[argindex++] = uidstring; 5820Sstevel@tonic-gate 5830Sstevel@tonic-gate if (oflag) nargv[argindex++] = "-o"; 5840Sstevel@tonic-gate 5850Sstevel@tonic-gate if (nkeys > 1) 5860Sstevel@tonic-gate addkey_args(nargv, &argindex); 5870Sstevel@tonic-gate 5880Sstevel@tonic-gate /* finally - login name */ 5890Sstevel@tonic-gate nargv[argindex++] = logname; 5900Sstevel@tonic-gate 5910Sstevel@tonic-gate /* set the last to null */ 5920Sstevel@tonic-gate nargv[argindex++] = NULL; 5930Sstevel@tonic-gate 5940Sstevel@tonic-gate /* now call passmgmt */ 5950Sstevel@tonic-gate ret = PEX_FAILED; 5960Sstevel@tonic-gate /* 5970Sstevel@tonic-gate * If call_passmgmt fails for any reason other than PEX_BADUID, exit 5980Sstevel@tonic-gate * is invoked with an appropriate error message. If PEX_BADUID is 5990Sstevel@tonic-gate * returned, then if the user specified the ID, exit is invoked 6000Sstevel@tonic-gate * with an appropriate error message. Otherwise we try to pick a 6010Sstevel@tonic-gate * different ID and try again. If we run out of IDs, i.e. no more 6020Sstevel@tonic-gate * users can be created, then -1 is returned and we terminate via exit. 6030Sstevel@tonic-gate * If PEX_BUSY is returned we increment a count, since we will stop 6040Sstevel@tonic-gate * trying if PEX_BUSY reaches 3. For PEX_SUCCESS we immediately 6050Sstevel@tonic-gate * terminate the loop. 6060Sstevel@tonic-gate */ 6070Sstevel@tonic-gate while (busy < 3 && ret != PEX_SUCCESS) { 6080Sstevel@tonic-gate switch (ret = call_passmgmt(nargv)) { 6090Sstevel@tonic-gate case PEX_SUCCESS: 6100Sstevel@tonic-gate break; 6110Sstevel@tonic-gate case PEX_BUSY: 6120Sstevel@tonic-gate busy++; 6130Sstevel@tonic-gate break; 6140Sstevel@tonic-gate case PEX_HOSED_FILES: 6150Sstevel@tonic-gate errmsg(M_HOSED_FILES); 6160Sstevel@tonic-gate exit(EX_INCONSISTENT); 6170Sstevel@tonic-gate break; 6180Sstevel@tonic-gate 6190Sstevel@tonic-gate case PEX_SYNTAX: 6200Sstevel@tonic-gate case PEX_BADARG: 6210Sstevel@tonic-gate /* should NEVER occur that passmgmt usage is wrong */ 6220Sstevel@tonic-gate if (is_role(usertype)) 6230Sstevel@tonic-gate errmsg(M_ARUSAGE); 6240Sstevel@tonic-gate else 6250Sstevel@tonic-gate errmsg(M_AUSAGE); 6260Sstevel@tonic-gate exit(EX_SYNTAX); 6270Sstevel@tonic-gate break; 6280Sstevel@tonic-gate 6290Sstevel@tonic-gate case PEX_BADUID: 6300Sstevel@tonic-gate /* 6310Sstevel@tonic-gate * The uid has been taken. If it was specified by a 6320Sstevel@tonic-gate * user, then we must fail. Otherwise, keep trying 6330Sstevel@tonic-gate * to get a good uid until we run out of IDs. 6340Sstevel@tonic-gate */ 6350Sstevel@tonic-gate if (uidstr != NULL) { 6360Sstevel@tonic-gate errmsg(M_UID_USED, uid); 6370Sstevel@tonic-gate exit(EX_ID_EXISTS); 6380Sstevel@tonic-gate } else { 6390Sstevel@tonic-gate if ((uid = findnextuid()) < 0) { 6400Sstevel@tonic-gate errmsg(M_INVALID, "default id", 6410Sstevel@tonic-gate "user id"); 6420Sstevel@tonic-gate exit(EX_ID_EXISTS); 6430Sstevel@tonic-gate } 644*4321Scasper (void) sprintf(uidstring, "%u", uid); 6450Sstevel@tonic-gate } 6460Sstevel@tonic-gate break; 6470Sstevel@tonic-gate 6480Sstevel@tonic-gate case PEX_BADNAME: 6490Sstevel@tonic-gate /* invalid loname */ 6500Sstevel@tonic-gate errmsg(M_USED, logname); 6510Sstevel@tonic-gate exit(EX_NAME_EXISTS); 6520Sstevel@tonic-gate break; 6530Sstevel@tonic-gate 6540Sstevel@tonic-gate default: 6550Sstevel@tonic-gate errmsg(M_UPDATE, "created"); 6560Sstevel@tonic-gate exit(ret); 6570Sstevel@tonic-gate break; 6580Sstevel@tonic-gate } 6590Sstevel@tonic-gate } 6600Sstevel@tonic-gate if (busy == 3) { 6610Sstevel@tonic-gate errmsg(M_UPDATE, "created"); 6620Sstevel@tonic-gate exit(ret); 6630Sstevel@tonic-gate } 6640Sstevel@tonic-gate 6650Sstevel@tonic-gate /* add group entry */ 6661639Sbasabi if ((grps != NULL) && edit_group(logname, (char *)0, gidlist, 0)) { 6670Sstevel@tonic-gate errmsg(M_UPDATE, "created"); 6680Sstevel@tonic-gate cleanup(logname); 6690Sstevel@tonic-gate exit(EX_UPDATE); 6700Sstevel@tonic-gate } 6710Sstevel@tonic-gate 6720Sstevel@tonic-gate /* update project database */ 6731639Sbasabi if ((projects != NULL) && 6741639Sbasabi edit_project(logname, (char *)NULL, projlist, 0)) { 6750Sstevel@tonic-gate errmsg(M_UPDATE, "created"); 6760Sstevel@tonic-gate cleanup(logname); 6770Sstevel@tonic-gate exit(EX_UPDATE); 6780Sstevel@tonic-gate } 6790Sstevel@tonic-gate 6800Sstevel@tonic-gate /* create home directory */ 6810Sstevel@tonic-gate if (mflag && 6820Sstevel@tonic-gate (create_home(homedir, skel_dir, uid, gid) != EX_SUCCESS)) { 6830Sstevel@tonic-gate (void) edit_group(logname, (char *)0, (int **)0, 1); 6840Sstevel@tonic-gate cleanup(logname); 6850Sstevel@tonic-gate exit(EX_HOMEDIR); 6860Sstevel@tonic-gate } 6870Sstevel@tonic-gate 6880Sstevel@tonic-gate return (ret); 6890Sstevel@tonic-gate } 6900Sstevel@tonic-gate 6910Sstevel@tonic-gate static void 6920Sstevel@tonic-gate cleanup(logname) 6930Sstevel@tonic-gate char *logname; 6940Sstevel@tonic-gate { 6950Sstevel@tonic-gate char *nargv[4]; 6960Sstevel@tonic-gate 6970Sstevel@tonic-gate nargv[0] = "passmgmt"; 6980Sstevel@tonic-gate nargv[1] = "-d"; 6990Sstevel@tonic-gate nargv[2] = logname; 7000Sstevel@tonic-gate nargv[3] = NULL; 7010Sstevel@tonic-gate 7020Sstevel@tonic-gate switch (call_passmgmt(nargv)) { 7030Sstevel@tonic-gate case PEX_SUCCESS: 7040Sstevel@tonic-gate break; 7050Sstevel@tonic-gate 7060Sstevel@tonic-gate case PEX_SYNTAX: 7070Sstevel@tonic-gate /* should NEVER occur that passmgmt usage is wrong */ 7080Sstevel@tonic-gate if (is_role(usertype)) 7090Sstevel@tonic-gate errmsg(M_ARUSAGE); 7100Sstevel@tonic-gate else 7110Sstevel@tonic-gate errmsg(M_AUSAGE); 7120Sstevel@tonic-gate break; 7130Sstevel@tonic-gate 7140Sstevel@tonic-gate case PEX_BADUID: 7150Sstevel@tonic-gate /* uid is used - shouldn't happen but print message anyway */ 7160Sstevel@tonic-gate errmsg(M_UID_USED, uid); 7170Sstevel@tonic-gate break; 7180Sstevel@tonic-gate 7190Sstevel@tonic-gate case PEX_BADNAME: 7200Sstevel@tonic-gate /* invalid loname */ 7210Sstevel@tonic-gate errmsg(M_USED, logname); 7220Sstevel@tonic-gate break; 7230Sstevel@tonic-gate 7240Sstevel@tonic-gate default: 7250Sstevel@tonic-gate errmsg(M_UPDATE, "created"); 7260Sstevel@tonic-gate break; 7270Sstevel@tonic-gate } 7280Sstevel@tonic-gate } 7291639Sbasabi 7301639Sbasabi /* Check the validity for shell, base_dir and skel_dir */ 7311639Sbasabi 7321639Sbasabi void 7331639Sbasabi valid_input(path_opt_t opt, const char *input) 7341639Sbasabi { 7351639Sbasabi struct stat statbuf; 7361639Sbasabi 7371639Sbasabi if (REL_PATH(input)) { 7381639Sbasabi errmsg(M_RELPATH, input); 7391639Sbasabi exit(EX_BADARG); 7401639Sbasabi } 7411639Sbasabi if (stat(input, &statbuf) == -1) { 7421639Sbasabi errmsg(M_INVALID, input, "path"); 7431639Sbasabi exit(EX_BADARG); 7441639Sbasabi } 7451639Sbasabi if (opt == SHELL) { 7461639Sbasabi if (!S_ISREG(statbuf.st_mode) || 7471639Sbasabi (statbuf.st_mode & 0555) != 0555) { 7481639Sbasabi errmsg(M_INVALID, input, "shell"); 7491639Sbasabi exit(EX_BADARG); 7501639Sbasabi } 7511639Sbasabi } else { 7521639Sbasabi if (!S_ISDIR(statbuf.st_mode)) { 7531639Sbasabi errmsg(M_INVALID, input, "directory"); 7541639Sbasabi exit(EX_BADARG); 7551639Sbasabi } 7561639Sbasabi } 7571639Sbasabi } 758