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