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 51507Sgjelinek * Common Development and Distribution License (the "License"). 61507Sgjelinek * 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 */ 21222Scomay 220Sstevel@tonic-gate /* 231300Sgjelinek * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 240Sstevel@tonic-gate * Use is subject to license terms. 250Sstevel@tonic-gate */ 260Sstevel@tonic-gate 270Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 280Sstevel@tonic-gate 290Sstevel@tonic-gate /* 300Sstevel@tonic-gate * zoneadm is a command interpreter for zone administration. It is all in 310Sstevel@tonic-gate * C (i.e., no lex/yacc), and all the argument passing is argc/argv based. 320Sstevel@tonic-gate * main() calls parse_and_run() which calls cmd_match(), then invokes the 330Sstevel@tonic-gate * appropriate command's handler function. The rest of the program is the 340Sstevel@tonic-gate * handler functions and their helper functions. 350Sstevel@tonic-gate * 360Sstevel@tonic-gate * Some of the helper functions are used largely to simplify I18N: reducing 370Sstevel@tonic-gate * the need for translation notes. This is particularly true of many of 380Sstevel@tonic-gate * the zerror() calls: doing e.g. zerror(gettext("%s failed"), "foo") rather 390Sstevel@tonic-gate * than zerror(gettext("foo failed")) with a translation note indicating 400Sstevel@tonic-gate * that "foo" need not be translated. 410Sstevel@tonic-gate */ 420Sstevel@tonic-gate 430Sstevel@tonic-gate #include <stdio.h> 440Sstevel@tonic-gate #include <errno.h> 450Sstevel@tonic-gate #include <unistd.h> 460Sstevel@tonic-gate #include <signal.h> 470Sstevel@tonic-gate #include <stdarg.h> 480Sstevel@tonic-gate #include <ctype.h> 490Sstevel@tonic-gate #include <stdlib.h> 500Sstevel@tonic-gate #include <string.h> 510Sstevel@tonic-gate #include <wait.h> 520Sstevel@tonic-gate #include <zone.h> 530Sstevel@tonic-gate #include <priv.h> 540Sstevel@tonic-gate #include <locale.h> 550Sstevel@tonic-gate #include <libintl.h> 560Sstevel@tonic-gate #include <libzonecfg.h> 570Sstevel@tonic-gate #include <bsm/adt.h> 582712Snn35248 #include <sys/brand.h> 590Sstevel@tonic-gate #include <sys/param.h> 600Sstevel@tonic-gate #include <sys/types.h> 610Sstevel@tonic-gate #include <sys/stat.h> 620Sstevel@tonic-gate #include <sys/statvfs.h> 630Sstevel@tonic-gate #include <assert.h> 640Sstevel@tonic-gate #include <sys/sockio.h> 650Sstevel@tonic-gate #include <sys/mntent.h> 660Sstevel@tonic-gate #include <limits.h> 671867Sgjelinek #include <dirent.h> 682303Scarlsonj #include <uuid/uuid.h> 690Sstevel@tonic-gate 700Sstevel@tonic-gate #include <fcntl.h> 710Sstevel@tonic-gate #include <door.h> 720Sstevel@tonic-gate #include <macros.h> 730Sstevel@tonic-gate #include <libgen.h> 741300Sgjelinek #include <fnmatch.h> 751931Sgjelinek #include <sys/modctl.h> 762712Snn35248 #include <libbrand.h> 770Sstevel@tonic-gate 780Sstevel@tonic-gate #include <pool.h> 790Sstevel@tonic-gate #include <sys/pool.h> 800Sstevel@tonic-gate 811867Sgjelinek #include "zoneadm.h" 821867Sgjelinek 830Sstevel@tonic-gate #define MAXARGS 8 840Sstevel@tonic-gate 850Sstevel@tonic-gate /* Reflects kernel zone entries */ 860Sstevel@tonic-gate typedef struct zone_entry { 870Sstevel@tonic-gate zoneid_t zid; 880Sstevel@tonic-gate char zname[ZONENAME_MAX]; 890Sstevel@tonic-gate char *zstate_str; 900Sstevel@tonic-gate zone_state_t zstate_num; 912712Snn35248 char zbrand[MAXNAMELEN]; 920Sstevel@tonic-gate char zroot[MAXPATHLEN]; 932303Scarlsonj char zuuid[UUID_PRINTABLE_STRING_LENGTH]; 940Sstevel@tonic-gate } zone_entry_t; 950Sstevel@tonic-gate 960Sstevel@tonic-gate static zone_entry_t *zents; 970Sstevel@tonic-gate static size_t nzents; 982712Snn35248 static boolean_t is_native_zone = B_TRUE; 990Sstevel@tonic-gate 1001915Sgjelinek #define LOOPBACK_IF "lo0" 1011915Sgjelinek #define SOCKET_AF(af) (((af) == AF_UNSPEC) ? AF_INET : (af)) 1021915Sgjelinek 1031915Sgjelinek struct net_if { 1041915Sgjelinek char *name; 1051915Sgjelinek int af; 1061915Sgjelinek }; 1071915Sgjelinek 1080Sstevel@tonic-gate /* 0755 is the default directory mode. */ 1090Sstevel@tonic-gate #define DEFAULT_DIR_MODE \ 1100Sstevel@tonic-gate (S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) 1110Sstevel@tonic-gate 1120Sstevel@tonic-gate struct cmd { 1130Sstevel@tonic-gate uint_t cmd_num; /* command number */ 1140Sstevel@tonic-gate char *cmd_name; /* command name */ 1150Sstevel@tonic-gate char *short_usage; /* short form help */ 1160Sstevel@tonic-gate int (*handler)(int argc, char *argv[]); /* function to call */ 1170Sstevel@tonic-gate 1180Sstevel@tonic-gate }; 1190Sstevel@tonic-gate 1200Sstevel@tonic-gate #define SHELP_HELP "help" 1212267Sdp #define SHELP_BOOT "boot [-- boot_arguments]" 1220Sstevel@tonic-gate #define SHELP_HALT "halt" 1230Sstevel@tonic-gate #define SHELP_READY "ready" 1242267Sdp #define SHELP_REBOOT "reboot [-- boot_arguments]" 1250Sstevel@tonic-gate #define SHELP_LIST "list [-cipv]" 1260Sstevel@tonic-gate #define SHELP_VERIFY "verify" 1272712Snn35248 #define SHELP_INSTALL "install [-x nodataset] [brand-specific args]" 1280Sstevel@tonic-gate #define SHELP_UNINSTALL "uninstall [-F]" 1291867Sgjelinek #define SHELP_CLONE "clone [-m method] [-s <ZFS snapshot>] zonename" 1301300Sgjelinek #define SHELP_MOVE "move zonepath" 1312078Sgjelinek #define SHELP_DETACH "detach [-n]" 1322078Sgjelinek #define SHELP_ATTACH "attach [-F] [-n <path>]" 1332303Scarlsonj #define SHELP_MARK "mark incomplete" 1340Sstevel@tonic-gate 1352712Snn35248 #define EXEC_PREFIX "exec " 1362712Snn35248 #define EXEC_LEN (strlen(EXEC_PREFIX)) 1372712Snn35248 #define RMCOMMAND "/usr/bin/rm -rf" 1382712Snn35248 1392712Snn35248 static int cleanup_zonepath(char *, boolean_t); 1402712Snn35248 1410Sstevel@tonic-gate static int help_func(int argc, char *argv[]); 1420Sstevel@tonic-gate static int ready_func(int argc, char *argv[]); 1430Sstevel@tonic-gate static int boot_func(int argc, char *argv[]); 1440Sstevel@tonic-gate static int halt_func(int argc, char *argv[]); 1450Sstevel@tonic-gate static int reboot_func(int argc, char *argv[]); 1460Sstevel@tonic-gate static int list_func(int argc, char *argv[]); 1470Sstevel@tonic-gate static int verify_func(int argc, char *argv[]); 1480Sstevel@tonic-gate static int install_func(int argc, char *argv[]); 1490Sstevel@tonic-gate static int uninstall_func(int argc, char *argv[]); 150766Scarlsonj static int mount_func(int argc, char *argv[]); 151766Scarlsonj static int unmount_func(int argc, char *argv[]); 1521300Sgjelinek static int clone_func(int argc, char *argv[]); 1531300Sgjelinek static int move_func(int argc, char *argv[]); 1541507Sgjelinek static int detach_func(int argc, char *argv[]); 1551507Sgjelinek static int attach_func(int argc, char *argv[]); 1562303Scarlsonj static int mark_func(int argc, char *argv[]); 1570Sstevel@tonic-gate static int sanity_check(char *zone, int cmd_num, boolean_t running, 1582712Snn35248 boolean_t unsafe_when_running, boolean_t force); 1590Sstevel@tonic-gate static int cmd_match(char *cmd); 1600Sstevel@tonic-gate static int verify_details(int); 1610Sstevel@tonic-gate 1620Sstevel@tonic-gate static struct cmd cmdtab[] = { 1630Sstevel@tonic-gate { CMD_HELP, "help", SHELP_HELP, help_func }, 1640Sstevel@tonic-gate { CMD_BOOT, "boot", SHELP_BOOT, boot_func }, 1650Sstevel@tonic-gate { CMD_HALT, "halt", SHELP_HALT, halt_func }, 1660Sstevel@tonic-gate { CMD_READY, "ready", SHELP_READY, ready_func }, 1670Sstevel@tonic-gate { CMD_REBOOT, "reboot", SHELP_REBOOT, reboot_func }, 1680Sstevel@tonic-gate { CMD_LIST, "list", SHELP_LIST, list_func }, 1690Sstevel@tonic-gate { CMD_VERIFY, "verify", SHELP_VERIFY, verify_func }, 1700Sstevel@tonic-gate { CMD_INSTALL, "install", SHELP_INSTALL, install_func }, 1710Sstevel@tonic-gate { CMD_UNINSTALL, "uninstall", SHELP_UNINSTALL, 172766Scarlsonj uninstall_func }, 1731300Sgjelinek /* mount and unmount are private commands for admin/install */ 174766Scarlsonj { CMD_MOUNT, "mount", NULL, mount_func }, 1751300Sgjelinek { CMD_UNMOUNT, "unmount", NULL, unmount_func }, 1761300Sgjelinek { CMD_CLONE, "clone", SHELP_CLONE, clone_func }, 1771507Sgjelinek { CMD_MOVE, "move", SHELP_MOVE, move_func }, 1781507Sgjelinek { CMD_DETACH, "detach", SHELP_DETACH, detach_func }, 1792303Scarlsonj { CMD_ATTACH, "attach", SHELP_ATTACH, attach_func }, 1802303Scarlsonj { CMD_MARK, "mark", SHELP_MARK, mark_func } 1810Sstevel@tonic-gate }; 1820Sstevel@tonic-gate 1830Sstevel@tonic-gate /* global variables */ 1840Sstevel@tonic-gate 1850Sstevel@tonic-gate /* set early in main(), never modified thereafter, used all over the place */ 1860Sstevel@tonic-gate static char *execname; 1872712Snn35248 static char target_brand[MAXNAMELEN]; 1880Sstevel@tonic-gate static char *locale; 1891867Sgjelinek char *target_zone; 1902303Scarlsonj static char *target_uuid; 1910Sstevel@tonic-gate 1920Sstevel@tonic-gate /* used in do_subproc() and signal handler */ 1930Sstevel@tonic-gate static volatile boolean_t child_killed; 1942712Snn35248 static int do_subproc_cnt = 0; 1952712Snn35248 1962712Snn35248 /* 1972712Snn35248 * Used to indicate whether this zoneadm instance has another zoneadm 1982712Snn35248 * instance in its ancestry. 1992712Snn35248 */ 2002712Snn35248 static boolean_t zoneadm_is_nested = B_FALSE; 2012712Snn35248 2022712Snn35248 /* used to track nested zone-lock operations */ 2032712Snn35248 static int zone_lock_cnt = 0; 2042712Snn35248 2052712Snn35248 /* used to communicate lock status to children */ 2062712Snn35248 #define LOCK_ENV_VAR "_ZONEADM_LOCK_HELD" 2072712Snn35248 static char zoneadm_lock_held[] = LOCK_ENV_VAR"=1"; 2082712Snn35248 static char zoneadm_lock_not_held[] = LOCK_ENV_VAR"=0"; 2090Sstevel@tonic-gate 2101867Sgjelinek char * 2110Sstevel@tonic-gate cmd_to_str(int cmd_num) 2120Sstevel@tonic-gate { 2130Sstevel@tonic-gate assert(cmd_num >= CMD_MIN && cmd_num <= CMD_MAX); 2140Sstevel@tonic-gate return (cmdtab[cmd_num].cmd_name); 2150Sstevel@tonic-gate } 2160Sstevel@tonic-gate 2170Sstevel@tonic-gate /* This is a separate function because of gettext() wrapping. */ 2180Sstevel@tonic-gate static char * 2190Sstevel@tonic-gate long_help(int cmd_num) 2200Sstevel@tonic-gate { 221222Scomay assert(cmd_num >= CMD_MIN && cmd_num <= CMD_MAX); 2220Sstevel@tonic-gate switch (cmd_num) { 2231634Sgjelinek case CMD_HELP: 2241634Sgjelinek return (gettext("Print usage message.")); 2251634Sgjelinek case CMD_BOOT: 2262267Sdp return (gettext("Activates (boots) specified zone. See " 2272267Sdp "zoneadm(1m) for valid boot\n\targuments.")); 2281634Sgjelinek case CMD_HALT: 2291634Sgjelinek return (gettext("Halts specified zone, bypassing shutdown " 2301634Sgjelinek "scripts and removing runtime\n\tresources of the zone.")); 2311634Sgjelinek case CMD_READY: 2321634Sgjelinek return (gettext("Prepares a zone for running applications but " 2331634Sgjelinek "does not start any user\n\tprocesses in the zone.")); 2341634Sgjelinek case CMD_REBOOT: 2351634Sgjelinek return (gettext("Restarts the zone (equivalent to a halt / " 2362267Sdp "boot sequence).\n\tFails if the zone is not active. " 2372267Sdp "See zoneadm(1m) for valid boot\n\targuments.")); 2381634Sgjelinek case CMD_LIST: 2391634Sgjelinek return (gettext("Lists the current zones, or a " 2401634Sgjelinek "specific zone if indicated. By default,\n\tall " 2411634Sgjelinek "running zones are listed, though this can be " 2421634Sgjelinek "expanded to all\n\tinstalled zones with the -i " 2431634Sgjelinek "option or all configured zones with the\n\t-c " 2442303Scarlsonj "option. When used with the general -z <zone> and/or -u " 2452303Scarlsonj "<uuid-match>\n\toptions, lists only the specified " 2462303Scarlsonj "matching zone, but lists it\n\tregardless of its state, " 2472303Scarlsonj "and the -i and -c options are disallowed. The\n\t-v " 2482303Scarlsonj "option can be used to display verbose information: zone " 2492303Scarlsonj "name, id,\n\tcurrent state, root directory and options. " 2502303Scarlsonj "The -p option can be used\n\tto request machine-parsable " 2512303Scarlsonj "output. The -v and -p options are mutually\n\texclusive." 2522303Scarlsonj " If neither -v nor -p is used, just the zone name is " 2532303Scarlsonj "listed.")); 2541634Sgjelinek case CMD_VERIFY: 2551634Sgjelinek return (gettext("Check to make sure the configuration " 2561634Sgjelinek "can safely be instantiated\n\ton the machine: " 2571634Sgjelinek "physical network interfaces exist, etc.")); 2581634Sgjelinek case CMD_INSTALL: 2591867Sgjelinek return (gettext("Install the configuration on to the system. " 2601867Sgjelinek "The -x nodataset option\n\tcan be used to prevent the " 2611867Sgjelinek "creation of a new ZFS file system for the\n\tzone " 2622712Snn35248 "(assuming the zonepath is within a ZFS file system).\n\t" 2632712Snn35248 "All other arguments are passed to the brand installation " 2642712Snn35248 "function;\n\tsee brand(4) for more information.")); 2651634Sgjelinek case CMD_UNINSTALL: 2661634Sgjelinek return (gettext("Uninstall the configuration from the system. " 2671634Sgjelinek "The -F flag can be used\n\tto force the action.")); 2681634Sgjelinek case CMD_CLONE: 2691867Sgjelinek return (gettext("Clone the installation of another zone. " 2701867Sgjelinek "The -m option can be used to\n\tspecify 'copy' which " 2711867Sgjelinek "forces a copy of the source zone. The -s option\n\t" 2721867Sgjelinek "can be used to specify the name of a ZFS snapshot " 2731867Sgjelinek "that was taken from\n\ta previous clone command. The " 2741867Sgjelinek "snapshot will be used as the source\n\tinstead of " 2751867Sgjelinek "creating a new ZFS snapshot.")); 2761634Sgjelinek case CMD_MOVE: 2771634Sgjelinek return (gettext("Move the zone to a new zonepath.")); 2781634Sgjelinek case CMD_DETACH: 2791634Sgjelinek return (gettext("Detach the zone from the system. The zone " 2801634Sgjelinek "state is changed to\n\t'configured' (but the files under " 2811634Sgjelinek "the zonepath are untouched).\n\tThe zone can subsequently " 2821634Sgjelinek "be attached, or can be moved to another\n\tsystem and " 2832078Sgjelinek "attached there. The -n option can be used to specify\n\t" 2842078Sgjelinek "'no-execute' mode. When -n is used, the information " 2852078Sgjelinek "needed to attach\n\tthe zone is sent to standard output " 2862078Sgjelinek "but the zone is not actually\n\tdetached.")); 2871634Sgjelinek case CMD_ATTACH: 2881634Sgjelinek return (gettext("Attach the zone to the system. The zone " 2891634Sgjelinek "state must be 'configured'\n\tprior to attach; upon " 2901634Sgjelinek "successful completion, the zone state will be\n\t" 2911634Sgjelinek "'installed'. The system software on the current " 2921634Sgjelinek "system must be\n\tcompatible with the software on the " 2931634Sgjelinek "zone's original system.\n\tSpecify -F to force the attach " 2942078Sgjelinek "and skip software compatibility tests.\n\tThe -n option " 2952078Sgjelinek "can be used to specify 'no-execute' mode. When -n is\n\t" 2962078Sgjelinek "used, the information needed to attach the zone is read " 2972078Sgjelinek "from the\n\tspecified path and the configuration is only " 2982078Sgjelinek "validated. The path can\n\tbe '-' to specify standard " 2992078Sgjelinek "input.")); 3002303Scarlsonj case CMD_MARK: 3012303Scarlsonj return (gettext("Set the state of the zone. This can be used " 3022303Scarlsonj "to force the zone\n\tstate to 'incomplete' " 3032303Scarlsonj "administratively if some activity has rendered\n\tthe " 3042303Scarlsonj "zone permanently unusable. The only valid state that " 3052303Scarlsonj "may be\n\tspecified is 'incomplete'.")); 3061634Sgjelinek default: 3071634Sgjelinek return (""); 3080Sstevel@tonic-gate } 3090Sstevel@tonic-gate /* NOTREACHED */ 310222Scomay return (NULL); 3110Sstevel@tonic-gate } 3120Sstevel@tonic-gate 3130Sstevel@tonic-gate /* 3140Sstevel@tonic-gate * Called with explicit B_TRUE when help is explicitly requested, B_FALSE for 3150Sstevel@tonic-gate * unexpected errors. 3160Sstevel@tonic-gate */ 3170Sstevel@tonic-gate 3180Sstevel@tonic-gate static int 3190Sstevel@tonic-gate usage(boolean_t explicit) 3200Sstevel@tonic-gate { 3210Sstevel@tonic-gate int i; 3220Sstevel@tonic-gate FILE *fd = explicit ? stdout : stderr; 3230Sstevel@tonic-gate 3240Sstevel@tonic-gate (void) fprintf(fd, "%s:\t%s help\n", gettext("usage"), execname); 3252303Scarlsonj (void) fprintf(fd, "\t%s [-z <zone>] [-u <uuid-match>] list\n", 3262303Scarlsonj execname); 3272303Scarlsonj (void) fprintf(fd, "\t%s {-z <zone>|-u <uuid-match>} <%s>\n", execname, 3280Sstevel@tonic-gate gettext("subcommand")); 3290Sstevel@tonic-gate (void) fprintf(fd, "\n%s:\n\n", gettext("Subcommands")); 3300Sstevel@tonic-gate for (i = CMD_MIN; i <= CMD_MAX; i++) { 331766Scarlsonj if (cmdtab[i].short_usage == NULL) 332766Scarlsonj continue; 3330Sstevel@tonic-gate (void) fprintf(fd, "%s\n", cmdtab[i].short_usage); 3340Sstevel@tonic-gate if (explicit) 3350Sstevel@tonic-gate (void) fprintf(fd, "\t%s\n\n", long_help(i)); 3360Sstevel@tonic-gate } 3370Sstevel@tonic-gate if (!explicit) 3380Sstevel@tonic-gate (void) fputs("\n", fd); 3390Sstevel@tonic-gate return (Z_USAGE); 3400Sstevel@tonic-gate } 3410Sstevel@tonic-gate 3420Sstevel@tonic-gate static void 3430Sstevel@tonic-gate sub_usage(char *short_usage, int cmd_num) 3440Sstevel@tonic-gate { 3450Sstevel@tonic-gate (void) fprintf(stderr, "%s:\t%s\n", gettext("usage"), short_usage); 3460Sstevel@tonic-gate (void) fprintf(stderr, "\t%s\n", long_help(cmd_num)); 3470Sstevel@tonic-gate } 3480Sstevel@tonic-gate 3490Sstevel@tonic-gate /* 3500Sstevel@tonic-gate * zperror() is like perror(3c) except that this also prints the executable 3510Sstevel@tonic-gate * name at the start of the message, and takes a boolean indicating whether 3520Sstevel@tonic-gate * to call libc'c strerror() or that from libzonecfg. 3530Sstevel@tonic-gate */ 3540Sstevel@tonic-gate 3551867Sgjelinek void 3560Sstevel@tonic-gate zperror(const char *str, boolean_t zonecfg_error) 3570Sstevel@tonic-gate { 3580Sstevel@tonic-gate (void) fprintf(stderr, "%s: %s: %s\n", execname, str, 3590Sstevel@tonic-gate zonecfg_error ? zonecfg_strerror(errno) : strerror(errno)); 3600Sstevel@tonic-gate } 3610Sstevel@tonic-gate 3620Sstevel@tonic-gate /* 3630Sstevel@tonic-gate * zperror2() is very similar to zperror() above, except it also prints a 3640Sstevel@tonic-gate * supplied zone name after the executable. 3650Sstevel@tonic-gate * 3660Sstevel@tonic-gate * All current consumers of this function want libzonecfg's strerror() rather 3670Sstevel@tonic-gate * than libc's; if this ever changes, this function can be made more generic 3680Sstevel@tonic-gate * like zperror() above. 3690Sstevel@tonic-gate */ 3700Sstevel@tonic-gate 3711867Sgjelinek void 3720Sstevel@tonic-gate zperror2(const char *zone, const char *str) 3730Sstevel@tonic-gate { 3740Sstevel@tonic-gate (void) fprintf(stderr, "%s: %s: %s: %s\n", execname, zone, str, 3750Sstevel@tonic-gate zonecfg_strerror(errno)); 3760Sstevel@tonic-gate } 3770Sstevel@tonic-gate 3780Sstevel@tonic-gate /* PRINTFLIKE1 */ 3791867Sgjelinek void 3800Sstevel@tonic-gate zerror(const char *fmt, ...) 3810Sstevel@tonic-gate { 3820Sstevel@tonic-gate va_list alist; 3830Sstevel@tonic-gate 3840Sstevel@tonic-gate va_start(alist, fmt); 3850Sstevel@tonic-gate (void) fprintf(stderr, "%s: ", execname); 3860Sstevel@tonic-gate if (target_zone != NULL) 3870Sstevel@tonic-gate (void) fprintf(stderr, "zone '%s': ", target_zone); 3880Sstevel@tonic-gate (void) vfprintf(stderr, fmt, alist); 3890Sstevel@tonic-gate (void) fprintf(stderr, "\n"); 3900Sstevel@tonic-gate va_end(alist); 3910Sstevel@tonic-gate } 3920Sstevel@tonic-gate 3930Sstevel@tonic-gate static void * 3940Sstevel@tonic-gate safe_calloc(size_t nelem, size_t elsize) 3950Sstevel@tonic-gate { 3960Sstevel@tonic-gate void *r = calloc(nelem, elsize); 3970Sstevel@tonic-gate 3980Sstevel@tonic-gate if (r == NULL) { 3990Sstevel@tonic-gate zerror(gettext("failed to allocate %lu bytes: %s"), 4000Sstevel@tonic-gate (ulong_t)nelem * elsize, strerror(errno)); 4010Sstevel@tonic-gate exit(Z_ERR); 4020Sstevel@tonic-gate } 4030Sstevel@tonic-gate return (r); 4040Sstevel@tonic-gate } 4050Sstevel@tonic-gate 4060Sstevel@tonic-gate static void 4070Sstevel@tonic-gate zone_print(zone_entry_t *zent, boolean_t verbose, boolean_t parsable) 4080Sstevel@tonic-gate { 4090Sstevel@tonic-gate static boolean_t firsttime = B_TRUE; 4100Sstevel@tonic-gate 4110Sstevel@tonic-gate assert(!(verbose && parsable)); 4120Sstevel@tonic-gate if (firsttime && verbose) { 4130Sstevel@tonic-gate firsttime = B_FALSE; 4142712Snn35248 (void) printf("%*s %-16s %-14s %-30s %-10s\n", ZONEID_WIDTH, 4152712Snn35248 "ID", "NAME", "STATUS", "PATH", "BRAND"); 4160Sstevel@tonic-gate } 4170Sstevel@tonic-gate if (!verbose) { 4182303Scarlsonj char *cp, *clim; 4192303Scarlsonj 4200Sstevel@tonic-gate if (!parsable) { 4210Sstevel@tonic-gate (void) printf("%s\n", zent->zname); 4220Sstevel@tonic-gate return; 4230Sstevel@tonic-gate } 4240Sstevel@tonic-gate if (zent->zid == ZONE_ID_UNDEFINED) 4250Sstevel@tonic-gate (void) printf("-"); 4260Sstevel@tonic-gate else 4270Sstevel@tonic-gate (void) printf("%lu", zent->zid); 4282303Scarlsonj (void) printf(":%s:%s:", zent->zname, zent->zstate_str); 4292303Scarlsonj cp = zent->zroot; 4302303Scarlsonj while ((clim = strchr(cp, ':')) != NULL) { 4312303Scarlsonj (void) printf("%.*s\\:", clim - cp, cp); 4322303Scarlsonj cp = clim + 1; 4332303Scarlsonj } 4342712Snn35248 (void) printf("%s:%s:%s\n", cp, zent->zuuid, zent->zbrand); 4350Sstevel@tonic-gate return; 4360Sstevel@tonic-gate } 4370Sstevel@tonic-gate if (zent->zstate_str != NULL) { 4380Sstevel@tonic-gate if (zent->zid == ZONE_ID_UNDEFINED) 4390Sstevel@tonic-gate (void) printf("%*s", ZONEID_WIDTH, "-"); 4400Sstevel@tonic-gate else 4410Sstevel@tonic-gate (void) printf("%*lu", ZONEID_WIDTH, zent->zid); 4422712Snn35248 (void) printf(" %-16s %-14s %-30s %-10s\n", zent->zname, 4432712Snn35248 zent->zstate_str, zent->zroot, zent->zbrand); 4440Sstevel@tonic-gate } 4450Sstevel@tonic-gate } 4460Sstevel@tonic-gate 4470Sstevel@tonic-gate static int 448766Scarlsonj lookup_zone_info(const char *zone_name, zoneid_t zid, zone_entry_t *zent) 4490Sstevel@tonic-gate { 4501676Sjpk char root[MAXPATHLEN], *cp; 4510Sstevel@tonic-gate int err; 4522303Scarlsonj uuid_t uuid; 4530Sstevel@tonic-gate 4540Sstevel@tonic-gate (void) strlcpy(zent->zname, zone_name, sizeof (zent->zname)); 4550Sstevel@tonic-gate (void) strlcpy(zent->zroot, "???", sizeof (zent->zroot)); 4562712Snn35248 (void) strlcpy(zent->zbrand, "???", sizeof (zent->zbrand)); 4570Sstevel@tonic-gate zent->zstate_str = "???"; 4580Sstevel@tonic-gate 459766Scarlsonj zent->zid = zid; 4600Sstevel@tonic-gate 4612303Scarlsonj if (zonecfg_get_uuid(zone_name, uuid) == Z_OK && 4622303Scarlsonj !uuid_is_null(uuid)) 4632303Scarlsonj uuid_unparse(uuid, zent->zuuid); 4642303Scarlsonj else 4652303Scarlsonj zent->zuuid[0] = '\0'; 4662303Scarlsonj 4671676Sjpk /* 4681676Sjpk * For labeled zones which query the zone path of lower-level 4691676Sjpk * zones, the path needs to be adjusted to drop the final 4701676Sjpk * "/root" component. This adjusted path is then useful 4711676Sjpk * for reading down any exported directories from the 4721676Sjpk * lower-level zone. 4731676Sjpk */ 4741676Sjpk if (is_system_labeled() && zent->zid != ZONE_ID_UNDEFINED) { 4751676Sjpk if (zone_getattr(zent->zid, ZONE_ATTR_ROOT, zent->zroot, 4761676Sjpk sizeof (zent->zroot)) == -1) { 4771676Sjpk zperror2(zent->zname, 4781676Sjpk gettext("could not get zone path.")); 4791676Sjpk return (Z_ERR); 4801676Sjpk } 4811676Sjpk cp = zent->zroot + strlen(zent->zroot) - 5; 4821676Sjpk if (cp > zent->zroot && strcmp(cp, "/root") == 0) 4831676Sjpk *cp = 0; 4841676Sjpk } else { 4851676Sjpk if ((err = zone_get_zonepath(zent->zname, root, 4861676Sjpk sizeof (root))) != Z_OK) { 4871676Sjpk errno = err; 4881676Sjpk zperror2(zent->zname, 4891676Sjpk gettext("could not get zone path.")); 4901676Sjpk return (Z_ERR); 4911676Sjpk } 4921676Sjpk (void) strlcpy(zent->zroot, root, sizeof (zent->zroot)); 4931676Sjpk } 4940Sstevel@tonic-gate 4950Sstevel@tonic-gate if ((err = zone_get_state(zent->zname, &zent->zstate_num)) != Z_OK) { 4960Sstevel@tonic-gate errno = err; 4970Sstevel@tonic-gate zperror2(zent->zname, gettext("could not get state")); 4980Sstevel@tonic-gate return (Z_ERR); 4990Sstevel@tonic-gate } 5000Sstevel@tonic-gate zent->zstate_str = zone_state_str(zent->zstate_num); 5013009Snn35248 5023009Snn35248 /* 5033009Snn35248 * A zone's brand is only available in the .xml file describing it, 5043009Snn35248 * which is only visible to the global zone. This causes 5053009Snn35248 * zone_get_brand() to fail when called from within a non-global 5063009Snn35248 * zone. Fortunately we only do this on labeled systems, where we 5073009Snn35248 * know all zones are native. 5083009Snn35248 */ 5093009Snn35248 if (getzoneid() != GLOBAL_ZONEID) { 5103009Snn35248 assert(is_system_labeled() != 0); 5113009Snn35248 (void) strlcpy(zent->zbrand, NATIVE_BRAND_NAME, 5123009Snn35248 sizeof (zent->zbrand)); 5133009Snn35248 } else if (zone_get_brand(zent->zname, zent->zbrand, 5142712Snn35248 sizeof (zent->zbrand)) != Z_OK) { 5152712Snn35248 zperror2(zent->zname, gettext("could not get brand name")); 5162712Snn35248 return (Z_ERR); 5172712Snn35248 } 5180Sstevel@tonic-gate 5190Sstevel@tonic-gate return (Z_OK); 5200Sstevel@tonic-gate } 5210Sstevel@tonic-gate 5220Sstevel@tonic-gate /* 5230Sstevel@tonic-gate * fetch_zents() calls zone_list(2) to find out how many zones are running 5240Sstevel@tonic-gate * (which is stored in the global nzents), then calls zone_list(2) again 5250Sstevel@tonic-gate * to fetch the list of running zones (stored in the global zents). This 5260Sstevel@tonic-gate * function may be called multiple times, so if zents is already set, we 5270Sstevel@tonic-gate * return immediately to save work. 5280Sstevel@tonic-gate */ 5290Sstevel@tonic-gate 5300Sstevel@tonic-gate static int 531766Scarlsonj fetch_zents(void) 5320Sstevel@tonic-gate { 5330Sstevel@tonic-gate zoneid_t *zids = NULL; 5340Sstevel@tonic-gate uint_t nzents_saved; 535766Scarlsonj int i, retv; 536766Scarlsonj FILE *fp; 537766Scarlsonj boolean_t inaltroot; 538766Scarlsonj zone_entry_t *zentp; 5390Sstevel@tonic-gate 5400Sstevel@tonic-gate if (nzents > 0) 5410Sstevel@tonic-gate return (Z_OK); 5420Sstevel@tonic-gate 5430Sstevel@tonic-gate if (zone_list(NULL, &nzents) != 0) { 5440Sstevel@tonic-gate zperror(gettext("failed to get zoneid list"), B_FALSE); 5450Sstevel@tonic-gate return (Z_ERR); 5460Sstevel@tonic-gate } 5470Sstevel@tonic-gate 5480Sstevel@tonic-gate again: 5490Sstevel@tonic-gate if (nzents == 0) 5500Sstevel@tonic-gate return (Z_OK); 5510Sstevel@tonic-gate 5520Sstevel@tonic-gate zids = safe_calloc(nzents, sizeof (zoneid_t)); 5530Sstevel@tonic-gate nzents_saved = nzents; 5540Sstevel@tonic-gate 5550Sstevel@tonic-gate if (zone_list(zids, &nzents) != 0) { 5560Sstevel@tonic-gate zperror(gettext("failed to get zone list"), B_FALSE); 5570Sstevel@tonic-gate free(zids); 5580Sstevel@tonic-gate return (Z_ERR); 5590Sstevel@tonic-gate } 5600Sstevel@tonic-gate if (nzents != nzents_saved) { 5610Sstevel@tonic-gate /* list changed, try again */ 5620Sstevel@tonic-gate free(zids); 5630Sstevel@tonic-gate goto again; 5640Sstevel@tonic-gate } 5650Sstevel@tonic-gate 5660Sstevel@tonic-gate zents = safe_calloc(nzents, sizeof (zone_entry_t)); 5670Sstevel@tonic-gate 568766Scarlsonj inaltroot = zonecfg_in_alt_root(); 569766Scarlsonj if (inaltroot) 570766Scarlsonj fp = zonecfg_open_scratch("", B_FALSE); 571766Scarlsonj else 572766Scarlsonj fp = NULL; 573766Scarlsonj zentp = zents; 574766Scarlsonj retv = Z_OK; 5750Sstevel@tonic-gate for (i = 0; i < nzents; i++) { 5760Sstevel@tonic-gate char name[ZONENAME_MAX]; 577766Scarlsonj char altname[ZONENAME_MAX]; 5780Sstevel@tonic-gate 579766Scarlsonj if (getzonenamebyid(zids[i], name, sizeof (name)) < 0) { 5800Sstevel@tonic-gate zperror(gettext("failed to get zone name"), B_FALSE); 581766Scarlsonj retv = Z_ERR; 582766Scarlsonj continue; 583766Scarlsonj } 584766Scarlsonj if (zonecfg_is_scratch(name)) { 585766Scarlsonj /* Ignore scratch zones by default */ 586766Scarlsonj if (!inaltroot) 587766Scarlsonj continue; 588766Scarlsonj if (fp == NULL || 589766Scarlsonj zonecfg_reverse_scratch(fp, name, altname, 590766Scarlsonj sizeof (altname), NULL, 0) == -1) { 591924Sgjelinek zerror(gettext("could not resolve scratch " 592766Scarlsonj "zone %s"), name); 593766Scarlsonj retv = Z_ERR; 594766Scarlsonj continue; 595766Scarlsonj } 596766Scarlsonj (void) strcpy(name, altname); 597766Scarlsonj } else { 598766Scarlsonj /* Ignore non-scratch when in an alternate root */ 599766Scarlsonj if (inaltroot && strcmp(name, GLOBAL_ZONENAME) != 0) 600766Scarlsonj continue; 601766Scarlsonj } 602766Scarlsonj if (lookup_zone_info(name, zids[i], zentp) != Z_OK) { 603766Scarlsonj zerror(gettext("failed to get zone data")); 604766Scarlsonj retv = Z_ERR; 605766Scarlsonj continue; 606766Scarlsonj } 607766Scarlsonj zentp++; 6080Sstevel@tonic-gate } 609766Scarlsonj nzents = zentp - zents; 610766Scarlsonj if (fp != NULL) 611766Scarlsonj zonecfg_close_scratch(fp); 6120Sstevel@tonic-gate 6130Sstevel@tonic-gate free(zids); 614766Scarlsonj return (retv); 6150Sstevel@tonic-gate } 6160Sstevel@tonic-gate 617766Scarlsonj static int 6180Sstevel@tonic-gate zone_print_list(zone_state_t min_state, boolean_t verbose, boolean_t parsable) 6190Sstevel@tonic-gate { 6200Sstevel@tonic-gate int i; 6210Sstevel@tonic-gate zone_entry_t zent; 6220Sstevel@tonic-gate FILE *cookie; 6230Sstevel@tonic-gate char *name; 6240Sstevel@tonic-gate 6250Sstevel@tonic-gate /* 6260Sstevel@tonic-gate * First get the list of running zones from the kernel and print them. 6270Sstevel@tonic-gate * If that is all we need, then return. 6280Sstevel@tonic-gate */ 629766Scarlsonj if ((i = fetch_zents()) != Z_OK) { 6300Sstevel@tonic-gate /* 6310Sstevel@tonic-gate * No need for error messages; fetch_zents() has already taken 6320Sstevel@tonic-gate * care of this. 6330Sstevel@tonic-gate */ 634766Scarlsonj return (i); 6350Sstevel@tonic-gate } 636766Scarlsonj for (i = 0; i < nzents; i++) 6370Sstevel@tonic-gate zone_print(&zents[i], verbose, parsable); 6380Sstevel@tonic-gate if (min_state >= ZONE_STATE_RUNNING) 639766Scarlsonj return (Z_OK); 6400Sstevel@tonic-gate /* 6410Sstevel@tonic-gate * Next, get the full list of zones from the configuration, skipping 6420Sstevel@tonic-gate * any we have already printed. 6430Sstevel@tonic-gate */ 6440Sstevel@tonic-gate cookie = setzoneent(); 6450Sstevel@tonic-gate while ((name = getzoneent(cookie)) != NULL) { 6460Sstevel@tonic-gate for (i = 0; i < nzents; i++) { 6470Sstevel@tonic-gate if (strcmp(zents[i].zname, name) == 0) 6480Sstevel@tonic-gate break; 6490Sstevel@tonic-gate } 6500Sstevel@tonic-gate if (i < nzents) { 6510Sstevel@tonic-gate free(name); 6520Sstevel@tonic-gate continue; 6530Sstevel@tonic-gate } 654766Scarlsonj if (lookup_zone_info(name, ZONE_ID_UNDEFINED, &zent) != Z_OK) { 6550Sstevel@tonic-gate free(name); 6560Sstevel@tonic-gate continue; 6570Sstevel@tonic-gate } 6580Sstevel@tonic-gate free(name); 6590Sstevel@tonic-gate if (zent.zstate_num >= min_state) 6600Sstevel@tonic-gate zone_print(&zent, verbose, parsable); 6610Sstevel@tonic-gate } 6620Sstevel@tonic-gate endzoneent(cookie); 663766Scarlsonj return (Z_OK); 6640Sstevel@tonic-gate } 6650Sstevel@tonic-gate 6660Sstevel@tonic-gate static zone_entry_t * 6670Sstevel@tonic-gate lookup_running_zone(char *str) 6680Sstevel@tonic-gate { 6690Sstevel@tonic-gate zoneid_t zoneid; 6700Sstevel@tonic-gate char *cp; 6710Sstevel@tonic-gate int i; 6720Sstevel@tonic-gate 6730Sstevel@tonic-gate if (fetch_zents() != Z_OK) 6740Sstevel@tonic-gate return (NULL); 6750Sstevel@tonic-gate 6760Sstevel@tonic-gate for (i = 0; i < nzents; i++) { 6770Sstevel@tonic-gate if (strcmp(str, zents[i].zname) == 0) 6780Sstevel@tonic-gate return (&zents[i]); 6790Sstevel@tonic-gate } 6800Sstevel@tonic-gate errno = 0; 6810Sstevel@tonic-gate zoneid = strtol(str, &cp, 0); 6820Sstevel@tonic-gate if (zoneid < MIN_ZONEID || zoneid > MAX_ZONEID || 6830Sstevel@tonic-gate errno != 0 || *cp != '\0') 6840Sstevel@tonic-gate return (NULL); 6850Sstevel@tonic-gate for (i = 0; i < nzents; i++) { 6860Sstevel@tonic-gate if (zoneid == zents[i].zid) 6870Sstevel@tonic-gate return (&zents[i]); 6880Sstevel@tonic-gate } 6890Sstevel@tonic-gate return (NULL); 6900Sstevel@tonic-gate } 6910Sstevel@tonic-gate 6920Sstevel@tonic-gate /* 6930Sstevel@tonic-gate * Check a bit in a mode_t: if on is B_TRUE, that bit should be on; if 6940Sstevel@tonic-gate * B_FALSE, it should be off. Return B_TRUE if the mode is bad (incorrect). 6950Sstevel@tonic-gate */ 6960Sstevel@tonic-gate static boolean_t 6970Sstevel@tonic-gate bad_mode_bit(mode_t mode, mode_t bit, boolean_t on, char *file) 6980Sstevel@tonic-gate { 6990Sstevel@tonic-gate char *str; 7000Sstevel@tonic-gate 7010Sstevel@tonic-gate assert(bit == S_IRUSR || bit == S_IWUSR || bit == S_IXUSR || 7020Sstevel@tonic-gate bit == S_IRGRP || bit == S_IWGRP || bit == S_IXGRP || 7030Sstevel@tonic-gate bit == S_IROTH || bit == S_IWOTH || bit == S_IXOTH); 7040Sstevel@tonic-gate /* 7050Sstevel@tonic-gate * TRANSLATION_NOTE 7060Sstevel@tonic-gate * The strings below will be used as part of a larger message, 7070Sstevel@tonic-gate * either: 7080Sstevel@tonic-gate * (file name) must be (owner|group|world) (read|writ|execut)able 7090Sstevel@tonic-gate * or 7100Sstevel@tonic-gate * (file name) must not be (owner|group|world) (read|writ|execut)able 7110Sstevel@tonic-gate */ 7120Sstevel@tonic-gate switch (bit) { 7130Sstevel@tonic-gate case S_IRUSR: 7140Sstevel@tonic-gate str = gettext("owner readable"); 7150Sstevel@tonic-gate break; 7160Sstevel@tonic-gate case S_IWUSR: 7170Sstevel@tonic-gate str = gettext("owner writable"); 7180Sstevel@tonic-gate break; 7190Sstevel@tonic-gate case S_IXUSR: 7200Sstevel@tonic-gate str = gettext("owner executable"); 7210Sstevel@tonic-gate break; 7220Sstevel@tonic-gate case S_IRGRP: 7230Sstevel@tonic-gate str = gettext("group readable"); 7240Sstevel@tonic-gate break; 7250Sstevel@tonic-gate case S_IWGRP: 7260Sstevel@tonic-gate str = gettext("group writable"); 7270Sstevel@tonic-gate break; 7280Sstevel@tonic-gate case S_IXGRP: 7290Sstevel@tonic-gate str = gettext("group executable"); 7300Sstevel@tonic-gate break; 7310Sstevel@tonic-gate case S_IROTH: 7320Sstevel@tonic-gate str = gettext("world readable"); 7330Sstevel@tonic-gate break; 7340Sstevel@tonic-gate case S_IWOTH: 7350Sstevel@tonic-gate str = gettext("world writable"); 7360Sstevel@tonic-gate break; 7370Sstevel@tonic-gate case S_IXOTH: 7380Sstevel@tonic-gate str = gettext("world executable"); 7390Sstevel@tonic-gate break; 7400Sstevel@tonic-gate } 7410Sstevel@tonic-gate if ((mode & bit) == (on ? 0 : bit)) { 7420Sstevel@tonic-gate /* 7430Sstevel@tonic-gate * TRANSLATION_NOTE 7440Sstevel@tonic-gate * The first parameter below is a file name; the second 7450Sstevel@tonic-gate * is one of the "(owner|group|world) (read|writ|execut)able" 7460Sstevel@tonic-gate * strings from above. 7470Sstevel@tonic-gate */ 7480Sstevel@tonic-gate /* 7490Sstevel@tonic-gate * The code below could be simplified but not in a way 7500Sstevel@tonic-gate * that would easily translate to non-English locales. 7510Sstevel@tonic-gate */ 7520Sstevel@tonic-gate if (on) { 7530Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s must be %s.\n"), 7540Sstevel@tonic-gate file, str); 7550Sstevel@tonic-gate } else { 7560Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s must not be %s.\n"), 7570Sstevel@tonic-gate file, str); 7580Sstevel@tonic-gate } 7590Sstevel@tonic-gate return (B_TRUE); 7600Sstevel@tonic-gate } 7610Sstevel@tonic-gate return (B_FALSE); 7620Sstevel@tonic-gate } 7630Sstevel@tonic-gate 7640Sstevel@tonic-gate /* 7650Sstevel@tonic-gate * We want to make sure that no zone has its zone path as a child node 7660Sstevel@tonic-gate * (in the directory sense) of any other. We do that by comparing this 7670Sstevel@tonic-gate * zone's path to the path of all other (non-global) zones. The comparison 7680Sstevel@tonic-gate * in each case is simple: add '/' to the end of the path, then do a 7690Sstevel@tonic-gate * strncmp() of the two paths, using the length of the shorter one. 7700Sstevel@tonic-gate */ 7710Sstevel@tonic-gate 7720Sstevel@tonic-gate static int 7730Sstevel@tonic-gate crosscheck_zonepaths(char *path) 7740Sstevel@tonic-gate { 7750Sstevel@tonic-gate char rpath[MAXPATHLEN]; /* resolved path */ 7760Sstevel@tonic-gate char path_copy[MAXPATHLEN]; /* copy of original path */ 7770Sstevel@tonic-gate char rpath_copy[MAXPATHLEN]; /* copy of original rpath */ 7780Sstevel@tonic-gate struct zoneent *ze; 7790Sstevel@tonic-gate int res, err; 7800Sstevel@tonic-gate FILE *cookie; 7810Sstevel@tonic-gate 7820Sstevel@tonic-gate cookie = setzoneent(); 7830Sstevel@tonic-gate while ((ze = getzoneent_private(cookie)) != NULL) { 7840Sstevel@tonic-gate /* Skip zones which are not installed. */ 7850Sstevel@tonic-gate if (ze->zone_state < ZONE_STATE_INSTALLED) { 7860Sstevel@tonic-gate free(ze); 7870Sstevel@tonic-gate continue; 7880Sstevel@tonic-gate } 7890Sstevel@tonic-gate /* Skip the global zone and the current target zone. */ 7900Sstevel@tonic-gate if (strcmp(ze->zone_name, GLOBAL_ZONENAME) == 0 || 7910Sstevel@tonic-gate strcmp(ze->zone_name, target_zone) == 0) { 7920Sstevel@tonic-gate free(ze); 7930Sstevel@tonic-gate continue; 7940Sstevel@tonic-gate } 7950Sstevel@tonic-gate if (strlen(ze->zone_path) == 0) { 7960Sstevel@tonic-gate /* old index file without path, fall back */ 7970Sstevel@tonic-gate if ((err = zone_get_zonepath(ze->zone_name, 7980Sstevel@tonic-gate ze->zone_path, sizeof (ze->zone_path))) != Z_OK) { 7990Sstevel@tonic-gate errno = err; 8000Sstevel@tonic-gate zperror2(ze->zone_name, 8010Sstevel@tonic-gate gettext("could not get zone path")); 8020Sstevel@tonic-gate free(ze); 8030Sstevel@tonic-gate continue; 8040Sstevel@tonic-gate } 8050Sstevel@tonic-gate } 806766Scarlsonj (void) snprintf(path_copy, sizeof (path_copy), "%s%s", 807766Scarlsonj zonecfg_get_root(), ze->zone_path); 808766Scarlsonj res = resolvepath(path_copy, rpath, sizeof (rpath)); 8090Sstevel@tonic-gate if (res == -1) { 8100Sstevel@tonic-gate if (errno != ENOENT) { 811766Scarlsonj zperror(path_copy, B_FALSE); 8120Sstevel@tonic-gate free(ze); 8130Sstevel@tonic-gate return (Z_ERR); 8140Sstevel@tonic-gate } 8150Sstevel@tonic-gate (void) printf(gettext("WARNING: zone %s is installed, " 8160Sstevel@tonic-gate "but its %s %s does not exist.\n"), ze->zone_name, 817766Scarlsonj "zonepath", path_copy); 8180Sstevel@tonic-gate free(ze); 8190Sstevel@tonic-gate continue; 8200Sstevel@tonic-gate } 8210Sstevel@tonic-gate rpath[res] = '\0'; 8220Sstevel@tonic-gate (void) snprintf(path_copy, sizeof (path_copy), "%s/", path); 8230Sstevel@tonic-gate (void) snprintf(rpath_copy, sizeof (rpath_copy), "%s/", rpath); 8240Sstevel@tonic-gate if (strncmp(path_copy, rpath_copy, 8250Sstevel@tonic-gate min(strlen(path_copy), strlen(rpath_copy))) == 0) { 826924Sgjelinek /* 827924Sgjelinek * TRANSLATION_NOTE 828924Sgjelinek * zonepath is a literal that should not be translated. 829924Sgjelinek */ 8300Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s zonepath (%s) and " 8310Sstevel@tonic-gate "%s zonepath (%s) overlap.\n"), 8320Sstevel@tonic-gate target_zone, path, ze->zone_name, rpath); 8330Sstevel@tonic-gate free(ze); 8340Sstevel@tonic-gate return (Z_ERR); 8350Sstevel@tonic-gate } 8360Sstevel@tonic-gate free(ze); 8370Sstevel@tonic-gate } 8380Sstevel@tonic-gate endzoneent(cookie); 8390Sstevel@tonic-gate return (Z_OK); 8400Sstevel@tonic-gate } 8410Sstevel@tonic-gate 8420Sstevel@tonic-gate static int 8430Sstevel@tonic-gate validate_zonepath(char *path, int cmd_num) 8440Sstevel@tonic-gate { 8450Sstevel@tonic-gate int res; /* result of last library/system call */ 8460Sstevel@tonic-gate boolean_t err = B_FALSE; /* have we run into an error? */ 8470Sstevel@tonic-gate struct stat stbuf; 8482267Sdp struct statvfs64 vfsbuf; 8490Sstevel@tonic-gate char rpath[MAXPATHLEN]; /* resolved path */ 8500Sstevel@tonic-gate char ppath[MAXPATHLEN]; /* parent path */ 8510Sstevel@tonic-gate char rppath[MAXPATHLEN]; /* resolved parent path */ 8520Sstevel@tonic-gate char rootpath[MAXPATHLEN]; /* root path */ 8530Sstevel@tonic-gate zone_state_t state; 8540Sstevel@tonic-gate 8550Sstevel@tonic-gate if (path[0] != '/') { 8560Sstevel@tonic-gate (void) fprintf(stderr, 8570Sstevel@tonic-gate gettext("%s is not an absolute path.\n"), path); 8580Sstevel@tonic-gate return (Z_ERR); 8590Sstevel@tonic-gate } 8600Sstevel@tonic-gate if ((res = resolvepath(path, rpath, sizeof (rpath))) == -1) { 8610Sstevel@tonic-gate if ((errno != ENOENT) || 8621300Sgjelinek (cmd_num != CMD_VERIFY && cmd_num != CMD_INSTALL && 8631300Sgjelinek cmd_num != CMD_CLONE && cmd_num != CMD_MOVE)) { 8640Sstevel@tonic-gate zperror(path, B_FALSE); 8650Sstevel@tonic-gate return (Z_ERR); 8660Sstevel@tonic-gate } 8670Sstevel@tonic-gate if (cmd_num == CMD_VERIFY) { 868924Sgjelinek /* 869924Sgjelinek * TRANSLATION_NOTE 870924Sgjelinek * zoneadm is a literal that should not be translated. 871924Sgjelinek */ 8720Sstevel@tonic-gate (void) fprintf(stderr, gettext("WARNING: %s does not " 873924Sgjelinek "exist, so it could not be verified.\nWhen " 874924Sgjelinek "'zoneadm %s' is run, '%s' will try to create\n%s, " 875924Sgjelinek "and '%s' will be tried again,\nbut the '%s' may " 876924Sgjelinek "fail if:\nthe parent directory of %s is group- or " 877924Sgjelinek "other-writable\nor\n%s overlaps with any other " 8780Sstevel@tonic-gate "installed zones.\n"), path, 8790Sstevel@tonic-gate cmd_to_str(CMD_INSTALL), cmd_to_str(CMD_INSTALL), 8800Sstevel@tonic-gate path, cmd_to_str(CMD_VERIFY), 8810Sstevel@tonic-gate cmd_to_str(CMD_VERIFY), path, path); 8820Sstevel@tonic-gate return (Z_OK); 8830Sstevel@tonic-gate } 8840Sstevel@tonic-gate /* 8850Sstevel@tonic-gate * The zonepath is supposed to be mode 700 but its 8860Sstevel@tonic-gate * parent(s) 755. So use 755 on the mkdirp() then 8870Sstevel@tonic-gate * chmod() the zonepath itself to 700. 8880Sstevel@tonic-gate */ 8890Sstevel@tonic-gate if (mkdirp(path, DEFAULT_DIR_MODE) < 0) { 8900Sstevel@tonic-gate zperror(path, B_FALSE); 8910Sstevel@tonic-gate return (Z_ERR); 8920Sstevel@tonic-gate } 8930Sstevel@tonic-gate /* 8940Sstevel@tonic-gate * If the chmod() fails, report the error, but might 8950Sstevel@tonic-gate * as well continue the verify procedure. 8960Sstevel@tonic-gate */ 8970Sstevel@tonic-gate if (chmod(path, S_IRWXU) != 0) 8980Sstevel@tonic-gate zperror(path, B_FALSE); 8990Sstevel@tonic-gate /* 9000Sstevel@tonic-gate * Since the mkdir() succeeded, we should not have to 9010Sstevel@tonic-gate * worry about a subsequent ENOENT, thus this should 9020Sstevel@tonic-gate * only recurse once. 9030Sstevel@tonic-gate */ 9041300Sgjelinek return (validate_zonepath(path, cmd_num)); 9050Sstevel@tonic-gate } 9060Sstevel@tonic-gate rpath[res] = '\0'; 9070Sstevel@tonic-gate if (strcmp(path, rpath) != 0) { 9080Sstevel@tonic-gate errno = Z_RESOLVED_PATH; 9090Sstevel@tonic-gate zperror(path, B_TRUE); 9100Sstevel@tonic-gate return (Z_ERR); 9110Sstevel@tonic-gate } 9120Sstevel@tonic-gate if ((res = stat(rpath, &stbuf)) != 0) { 9130Sstevel@tonic-gate zperror(rpath, B_FALSE); 9140Sstevel@tonic-gate return (Z_ERR); 9150Sstevel@tonic-gate } 9160Sstevel@tonic-gate if (!S_ISDIR(stbuf.st_mode)) { 9170Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s is not a directory.\n"), 9180Sstevel@tonic-gate rpath); 9190Sstevel@tonic-gate return (Z_ERR); 9200Sstevel@tonic-gate } 9210Sstevel@tonic-gate if ((strcmp(stbuf.st_fstype, MNTTYPE_TMPFS) == 0) || 9220Sstevel@tonic-gate (strcmp(stbuf.st_fstype, MNTTYPE_XMEMFS) == 0)) { 9230Sstevel@tonic-gate (void) printf(gettext("WARNING: %s is on a temporary " 9241867Sgjelinek "file system.\n"), rpath); 9250Sstevel@tonic-gate } 9260Sstevel@tonic-gate if (crosscheck_zonepaths(rpath) != Z_OK) 9270Sstevel@tonic-gate return (Z_ERR); 9280Sstevel@tonic-gate /* 9290Sstevel@tonic-gate * Try to collect and report as many minor errors as possible 9300Sstevel@tonic-gate * before returning, so the user can learn everything that needs 9310Sstevel@tonic-gate * to be fixed up front. 9320Sstevel@tonic-gate */ 9330Sstevel@tonic-gate if (stbuf.st_uid != 0) { 9340Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s is not owned by root.\n"), 9350Sstevel@tonic-gate rpath); 9360Sstevel@tonic-gate err = B_TRUE; 9370Sstevel@tonic-gate } 9380Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IRUSR, B_TRUE, rpath); 9390Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IWUSR, B_TRUE, rpath); 9400Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IXUSR, B_TRUE, rpath); 9410Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IRGRP, B_FALSE, rpath); 9420Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IWGRP, B_FALSE, rpath); 9430Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IXGRP, B_FALSE, rpath); 9440Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IROTH, B_FALSE, rpath); 9450Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IWOTH, B_FALSE, rpath); 9460Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IXOTH, B_FALSE, rpath); 9470Sstevel@tonic-gate 9480Sstevel@tonic-gate (void) snprintf(ppath, sizeof (ppath), "%s/..", path); 9490Sstevel@tonic-gate if ((res = resolvepath(ppath, rppath, sizeof (rppath))) == -1) { 9500Sstevel@tonic-gate zperror(ppath, B_FALSE); 9510Sstevel@tonic-gate return (Z_ERR); 9520Sstevel@tonic-gate } 9530Sstevel@tonic-gate rppath[res] = '\0'; 9540Sstevel@tonic-gate if ((res = stat(rppath, &stbuf)) != 0) { 9550Sstevel@tonic-gate zperror(rppath, B_FALSE); 9560Sstevel@tonic-gate return (Z_ERR); 9570Sstevel@tonic-gate } 9580Sstevel@tonic-gate /* theoretically impossible */ 9590Sstevel@tonic-gate if (!S_ISDIR(stbuf.st_mode)) { 9600Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s is not a directory.\n"), 9610Sstevel@tonic-gate rppath); 9620Sstevel@tonic-gate return (Z_ERR); 9630Sstevel@tonic-gate } 9640Sstevel@tonic-gate if (stbuf.st_uid != 0) { 9650Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s is not owned by root.\n"), 9660Sstevel@tonic-gate rppath); 9670Sstevel@tonic-gate err = B_TRUE; 9680Sstevel@tonic-gate } 9690Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IRUSR, B_TRUE, rppath); 9700Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IWUSR, B_TRUE, rppath); 9710Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IXUSR, B_TRUE, rppath); 9720Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IWGRP, B_FALSE, rppath); 9730Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IWOTH, B_FALSE, rppath); 9740Sstevel@tonic-gate if (strcmp(rpath, rppath) == 0) { 9750Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s is its own parent.\n"), 9760Sstevel@tonic-gate rppath); 9770Sstevel@tonic-gate err = B_TRUE; 9780Sstevel@tonic-gate } 9790Sstevel@tonic-gate 9802267Sdp if (statvfs64(rpath, &vfsbuf) != 0) { 9810Sstevel@tonic-gate zperror(rpath, B_FALSE); 9820Sstevel@tonic-gate return (Z_ERR); 9830Sstevel@tonic-gate } 984823Sgjelinek if (strcmp(vfsbuf.f_basetype, MNTTYPE_NFS) == 0) { 985924Sgjelinek /* 986924Sgjelinek * TRANSLATION_NOTE 987924Sgjelinek * Zonepath and NFS are literals that should not be translated. 988924Sgjelinek */ 989924Sgjelinek (void) fprintf(stderr, gettext("Zonepath %s is on an NFS " 9901867Sgjelinek "mounted file system.\n" 9911867Sgjelinek "\tA local file system must be used.\n"), rpath); 992823Sgjelinek return (Z_ERR); 993823Sgjelinek } 994823Sgjelinek if (vfsbuf.f_flag & ST_NOSUID) { 995924Sgjelinek /* 996924Sgjelinek * TRANSLATION_NOTE 997924Sgjelinek * Zonepath and nosuid are literals that should not be 998924Sgjelinek * translated. 999924Sgjelinek */ 1000823Sgjelinek (void) fprintf(stderr, gettext("Zonepath %s is on a nosuid " 10011867Sgjelinek "file system.\n"), rpath); 10020Sstevel@tonic-gate return (Z_ERR); 10030Sstevel@tonic-gate } 10040Sstevel@tonic-gate 10050Sstevel@tonic-gate if ((res = zone_get_state(target_zone, &state)) != Z_OK) { 10060Sstevel@tonic-gate errno = res; 10070Sstevel@tonic-gate zperror2(target_zone, gettext("could not get state")); 10080Sstevel@tonic-gate return (Z_ERR); 10090Sstevel@tonic-gate } 10100Sstevel@tonic-gate /* 10110Sstevel@tonic-gate * The existence of the root path is only bad in the configured state, 10120Sstevel@tonic-gate * as it is *supposed* to be there at the installed and later states. 10131507Sgjelinek * However, the root path is expected to be there if the zone is 10141507Sgjelinek * detached. 10150Sstevel@tonic-gate * State/command mismatches are caught earlier in verify_details(). 10160Sstevel@tonic-gate */ 10171507Sgjelinek if (state == ZONE_STATE_CONFIGURED && cmd_num != CMD_ATTACH) { 10180Sstevel@tonic-gate if (snprintf(rootpath, sizeof (rootpath), "%s/root", rpath) >= 10190Sstevel@tonic-gate sizeof (rootpath)) { 1020924Sgjelinek /* 1021924Sgjelinek * TRANSLATION_NOTE 1022924Sgjelinek * Zonepath is a literal that should not be translated. 1023924Sgjelinek */ 10240Sstevel@tonic-gate (void) fprintf(stderr, 10250Sstevel@tonic-gate gettext("Zonepath %s is too long.\n"), rpath); 10260Sstevel@tonic-gate return (Z_ERR); 10270Sstevel@tonic-gate } 10280Sstevel@tonic-gate if ((res = stat(rootpath, &stbuf)) == 0) { 10291507Sgjelinek if (zonecfg_detached(rpath)) 10301507Sgjelinek (void) fprintf(stderr, 10311507Sgjelinek gettext("Cannot %s detached " 10321507Sgjelinek "zone.\nUse attach or remove %s " 10331507Sgjelinek "directory.\n"), cmd_to_str(cmd_num), 10341507Sgjelinek rpath); 10351507Sgjelinek else 10361507Sgjelinek (void) fprintf(stderr, 10371507Sgjelinek gettext("Rootpath %s exists; " 10381507Sgjelinek "remove or move aside prior to %s.\n"), 10391507Sgjelinek rootpath, cmd_to_str(cmd_num)); 10400Sstevel@tonic-gate return (Z_ERR); 10410Sstevel@tonic-gate } 10420Sstevel@tonic-gate } 10430Sstevel@tonic-gate 10440Sstevel@tonic-gate return (err ? Z_ERR : Z_OK); 10450Sstevel@tonic-gate } 10460Sstevel@tonic-gate 10472712Snn35248 /* 10482712Snn35248 * The following two routines implement a simple locking mechanism to 10492712Snn35248 * ensure that only one instance of zoneadm at a time is able to manipulate 10502712Snn35248 * a given zone. The lock is built on top of an fcntl(2) lock of 10512712Snn35248 * [<altroot>]/var/run/zones/<zonename>.zoneadm.lock. If a zoneadm instance 10522712Snn35248 * can grab that lock, it is allowed to manipulate the zone. 10532712Snn35248 * 10542712Snn35248 * Since zoneadm may call external applications which in turn invoke 10552712Snn35248 * zoneadm again, we introduce the notion of "lock inheritance". Any 10562712Snn35248 * instance of zoneadm that has another instance in its ancestry is assumed 10572712Snn35248 * to be acting on behalf of the original zoneadm, and is thus allowed to 10582712Snn35248 * manipulate its zone. 10592712Snn35248 * 10602712Snn35248 * This inheritance is implemented via the _ZONEADM_LOCK_HELD environment 10612712Snn35248 * variable. When zoneadm is granted a lock on its zone, this environment 10622712Snn35248 * variable is set to 1. When it releases the lock, the variable is set to 10632712Snn35248 * 0. Since a child process inherits its parent's environment, checking 10642712Snn35248 * the state of this variable indicates whether or not any ancestor owns 10652712Snn35248 * the lock. 10662712Snn35248 */ 10670Sstevel@tonic-gate static void 10680Sstevel@tonic-gate release_lock_file(int lockfd) 10690Sstevel@tonic-gate { 10702712Snn35248 /* 10712712Snn35248 * If we are cleaning up from a failed attempt to lock the zone for 10722712Snn35248 * the first time, we might have a zone_lock_cnt of 0. In that 10732712Snn35248 * error case, we don't want to do anything but close the lock 10742712Snn35248 * file. 10752712Snn35248 */ 10762712Snn35248 assert(zone_lock_cnt >= 0); 10772712Snn35248 if (zone_lock_cnt > 0) { 10782712Snn35248 assert(getenv(LOCK_ENV_VAR) != NULL); 10792712Snn35248 assert(atoi(getenv(LOCK_ENV_VAR)) == 1); 10802712Snn35248 if (--zone_lock_cnt > 0) { 10812712Snn35248 assert(lockfd == -1); 10822712Snn35248 return; 10832712Snn35248 } 10842712Snn35248 if (putenv(zoneadm_lock_not_held) != 0) { 10852712Snn35248 zperror(target_zone, B_TRUE); 10862712Snn35248 exit(Z_ERR); 10872712Snn35248 } 10882712Snn35248 } 10892712Snn35248 assert(lockfd >= 0); 10900Sstevel@tonic-gate (void) close(lockfd); 10910Sstevel@tonic-gate } 10920Sstevel@tonic-gate 10930Sstevel@tonic-gate static int 10940Sstevel@tonic-gate grab_lock_file(const char *zone_name, int *lockfd) 10950Sstevel@tonic-gate { 10960Sstevel@tonic-gate char pathbuf[PATH_MAX]; 10970Sstevel@tonic-gate struct flock flock; 10980Sstevel@tonic-gate 10992712Snn35248 /* 11002712Snn35248 * If we already have the lock, we can skip this expensive song 11012712Snn35248 * and dance. 11022712Snn35248 */ 11032712Snn35248 if (zone_lock_cnt > 0) { 11042712Snn35248 zone_lock_cnt++; 11052712Snn35248 *lockfd = -1; 11062712Snn35248 return (Z_OK); 11072712Snn35248 } 11082712Snn35248 assert(getenv(LOCK_ENV_VAR) != NULL); 11092712Snn35248 assert(atoi(getenv(LOCK_ENV_VAR)) == 0); 11102712Snn35248 1111766Scarlsonj if (snprintf(pathbuf, sizeof (pathbuf), "%s%s", zonecfg_get_root(), 1112766Scarlsonj ZONES_TMPDIR) >= sizeof (pathbuf)) { 1113766Scarlsonj zerror(gettext("alternate root path is too long")); 1114766Scarlsonj return (Z_ERR); 1115766Scarlsonj } 1116766Scarlsonj if (mkdir(pathbuf, S_IRWXU) < 0 && errno != EEXIST) { 1117766Scarlsonj zerror(gettext("could not mkdir %s: %s"), pathbuf, 11180Sstevel@tonic-gate strerror(errno)); 11190Sstevel@tonic-gate return (Z_ERR); 11200Sstevel@tonic-gate } 1121766Scarlsonj (void) chmod(pathbuf, S_IRWXU); 11220Sstevel@tonic-gate 11230Sstevel@tonic-gate /* 11240Sstevel@tonic-gate * One of these lock files is created for each zone (when needed). 11250Sstevel@tonic-gate * The lock files are not cleaned up (except on system reboot), 11260Sstevel@tonic-gate * but since there is only one per zone, there is no resource 11270Sstevel@tonic-gate * starvation issue. 11280Sstevel@tonic-gate */ 1129766Scarlsonj if (snprintf(pathbuf, sizeof (pathbuf), "%s%s/%s.zoneadm.lock", 1130766Scarlsonj zonecfg_get_root(), ZONES_TMPDIR, zone_name) >= sizeof (pathbuf)) { 1131766Scarlsonj zerror(gettext("alternate root path is too long")); 1132766Scarlsonj return (Z_ERR); 1133766Scarlsonj } 11340Sstevel@tonic-gate if ((*lockfd = open(pathbuf, O_RDWR|O_CREAT, S_IRUSR|S_IWUSR)) < 0) { 11350Sstevel@tonic-gate zerror(gettext("could not open %s: %s"), pathbuf, 11360Sstevel@tonic-gate strerror(errno)); 11370Sstevel@tonic-gate return (Z_ERR); 11380Sstevel@tonic-gate } 11390Sstevel@tonic-gate /* 11400Sstevel@tonic-gate * Lock the file to synchronize with other zoneadmds 11410Sstevel@tonic-gate */ 11420Sstevel@tonic-gate flock.l_type = F_WRLCK; 11430Sstevel@tonic-gate flock.l_whence = SEEK_SET; 11440Sstevel@tonic-gate flock.l_start = (off_t)0; 11450Sstevel@tonic-gate flock.l_len = (off_t)0; 11462712Snn35248 if ((fcntl(*lockfd, F_SETLKW, &flock) < 0) || 11472712Snn35248 (putenv(zoneadm_lock_held) != 0)) { 11480Sstevel@tonic-gate zerror(gettext("unable to lock %s: %s"), pathbuf, 11490Sstevel@tonic-gate strerror(errno)); 11500Sstevel@tonic-gate release_lock_file(*lockfd); 11510Sstevel@tonic-gate return (Z_ERR); 11520Sstevel@tonic-gate } 11532712Snn35248 zone_lock_cnt = 1; 11540Sstevel@tonic-gate return (Z_OK); 11550Sstevel@tonic-gate } 11560Sstevel@tonic-gate 1157766Scarlsonj static boolean_t 11580Sstevel@tonic-gate get_doorname(const char *zone_name, char *buffer) 11590Sstevel@tonic-gate { 1160766Scarlsonj return (snprintf(buffer, PATH_MAX, "%s" ZONE_DOOR_PATH, 1161766Scarlsonj zonecfg_get_root(), zone_name) < PATH_MAX); 11620Sstevel@tonic-gate } 11630Sstevel@tonic-gate 11640Sstevel@tonic-gate /* 11650Sstevel@tonic-gate * system daemons are not audited. For the global zone, this occurs 11660Sstevel@tonic-gate * "naturally" since init is started with the default audit 11670Sstevel@tonic-gate * characteristics. Since zoneadmd is a system daemon and it starts 11680Sstevel@tonic-gate * init for a zone, it is necessary to clear out the audit 11690Sstevel@tonic-gate * characteristics inherited from whomever started zoneadmd. This is 11700Sstevel@tonic-gate * indicated by the audit id, which is set from the ruid parameter of 11710Sstevel@tonic-gate * adt_set_user(), below. 11720Sstevel@tonic-gate */ 11730Sstevel@tonic-gate 11740Sstevel@tonic-gate static void 11750Sstevel@tonic-gate prepare_audit_context() 11760Sstevel@tonic-gate { 11770Sstevel@tonic-gate adt_session_data_t *ah; 11780Sstevel@tonic-gate char *failure = gettext("audit failure: %s"); 11790Sstevel@tonic-gate 11800Sstevel@tonic-gate if (adt_start_session(&ah, NULL, 0)) { 11810Sstevel@tonic-gate zerror(failure, strerror(errno)); 11820Sstevel@tonic-gate return; 11830Sstevel@tonic-gate } 11840Sstevel@tonic-gate if (adt_set_user(ah, ADT_NO_AUDIT, ADT_NO_AUDIT, 11850Sstevel@tonic-gate ADT_NO_AUDIT, ADT_NO_AUDIT, NULL, ADT_NEW)) { 11860Sstevel@tonic-gate zerror(failure, strerror(errno)); 11870Sstevel@tonic-gate (void) adt_end_session(ah); 11880Sstevel@tonic-gate return; 11890Sstevel@tonic-gate } 11900Sstevel@tonic-gate if (adt_set_proc(ah)) 11910Sstevel@tonic-gate zerror(failure, strerror(errno)); 11920Sstevel@tonic-gate 11930Sstevel@tonic-gate (void) adt_end_session(ah); 11940Sstevel@tonic-gate } 11950Sstevel@tonic-gate 11960Sstevel@tonic-gate static int 11970Sstevel@tonic-gate start_zoneadmd(const char *zone_name) 11980Sstevel@tonic-gate { 11990Sstevel@tonic-gate char doorpath[PATH_MAX]; 12000Sstevel@tonic-gate pid_t child_pid; 12010Sstevel@tonic-gate int error = Z_ERR; 12020Sstevel@tonic-gate int doorfd, lockfd; 12030Sstevel@tonic-gate struct door_info info; 12040Sstevel@tonic-gate 1205766Scarlsonj if (!get_doorname(zone_name, doorpath)) 1206766Scarlsonj return (Z_ERR); 12070Sstevel@tonic-gate 12080Sstevel@tonic-gate if (grab_lock_file(zone_name, &lockfd) != Z_OK) 12090Sstevel@tonic-gate return (Z_ERR); 12100Sstevel@tonic-gate 12110Sstevel@tonic-gate /* 12120Sstevel@tonic-gate * Now that we have the lock, re-confirm that the daemon is 12130Sstevel@tonic-gate * *not* up and working fine. If it is still down, we have a green 12140Sstevel@tonic-gate * light to start it. 12150Sstevel@tonic-gate */ 12160Sstevel@tonic-gate if ((doorfd = open(doorpath, O_RDONLY)) < 0) { 12170Sstevel@tonic-gate if (errno != ENOENT) { 12180Sstevel@tonic-gate zperror(doorpath, B_FALSE); 12190Sstevel@tonic-gate goto out; 12200Sstevel@tonic-gate } 12210Sstevel@tonic-gate } else { 12220Sstevel@tonic-gate if (door_info(doorfd, &info) == 0 && 12230Sstevel@tonic-gate ((info.di_attributes & DOOR_REVOKED) == 0)) { 12240Sstevel@tonic-gate error = Z_OK; 12250Sstevel@tonic-gate (void) close(doorfd); 12260Sstevel@tonic-gate goto out; 12270Sstevel@tonic-gate } 12280Sstevel@tonic-gate (void) close(doorfd); 12290Sstevel@tonic-gate } 12300Sstevel@tonic-gate 12310Sstevel@tonic-gate if ((child_pid = fork()) == -1) { 12320Sstevel@tonic-gate zperror(gettext("could not fork"), B_FALSE); 12330Sstevel@tonic-gate goto out; 12340Sstevel@tonic-gate } else if (child_pid == 0) { 1235766Scarlsonj const char *argv[6], **ap; 1236766Scarlsonj 12370Sstevel@tonic-gate /* child process */ 12380Sstevel@tonic-gate prepare_audit_context(); 12390Sstevel@tonic-gate 1240766Scarlsonj ap = argv; 1241766Scarlsonj *ap++ = "zoneadmd"; 1242766Scarlsonj *ap++ = "-z"; 1243766Scarlsonj *ap++ = zone_name; 1244766Scarlsonj if (zonecfg_in_alt_root()) { 1245766Scarlsonj *ap++ = "-R"; 1246766Scarlsonj *ap++ = zonecfg_get_root(); 1247766Scarlsonj } 1248766Scarlsonj *ap = NULL; 1249766Scarlsonj 1250766Scarlsonj (void) execv("/usr/lib/zones/zoneadmd", (char * const *)argv); 1251924Sgjelinek /* 1252924Sgjelinek * TRANSLATION_NOTE 1253924Sgjelinek * zoneadmd is a literal that should not be translated. 1254924Sgjelinek */ 12550Sstevel@tonic-gate zperror(gettext("could not exec zoneadmd"), B_FALSE); 12560Sstevel@tonic-gate _exit(Z_ERR); 12570Sstevel@tonic-gate } else { 12580Sstevel@tonic-gate /* parent process */ 12590Sstevel@tonic-gate pid_t retval; 12600Sstevel@tonic-gate int pstatus = 0; 12610Sstevel@tonic-gate 12620Sstevel@tonic-gate do { 12630Sstevel@tonic-gate retval = waitpid(child_pid, &pstatus, 0); 12640Sstevel@tonic-gate } while (retval != child_pid); 12650Sstevel@tonic-gate if (WIFSIGNALED(pstatus) || (WIFEXITED(pstatus) && 12660Sstevel@tonic-gate WEXITSTATUS(pstatus) != 0)) { 12670Sstevel@tonic-gate zerror(gettext("could not start %s"), "zoneadmd"); 12680Sstevel@tonic-gate goto out; 12690Sstevel@tonic-gate } 12700Sstevel@tonic-gate } 12710Sstevel@tonic-gate error = Z_OK; 12720Sstevel@tonic-gate out: 12730Sstevel@tonic-gate release_lock_file(lockfd); 12740Sstevel@tonic-gate return (error); 12750Sstevel@tonic-gate } 12760Sstevel@tonic-gate 12770Sstevel@tonic-gate static int 12780Sstevel@tonic-gate ping_zoneadmd(const char *zone_name) 12790Sstevel@tonic-gate { 12800Sstevel@tonic-gate char doorpath[PATH_MAX]; 12810Sstevel@tonic-gate int doorfd; 12820Sstevel@tonic-gate struct door_info info; 12830Sstevel@tonic-gate 1284766Scarlsonj if (!get_doorname(zone_name, doorpath)) 1285766Scarlsonj return (Z_ERR); 12860Sstevel@tonic-gate 12870Sstevel@tonic-gate if ((doorfd = open(doorpath, O_RDONLY)) < 0) { 12880Sstevel@tonic-gate return (Z_ERR); 12890Sstevel@tonic-gate } 12900Sstevel@tonic-gate if (door_info(doorfd, &info) == 0 && 12910Sstevel@tonic-gate ((info.di_attributes & DOOR_REVOKED) == 0)) { 12920Sstevel@tonic-gate (void) close(doorfd); 12930Sstevel@tonic-gate return (Z_OK); 12940Sstevel@tonic-gate } 12950Sstevel@tonic-gate (void) close(doorfd); 12960Sstevel@tonic-gate return (Z_ERR); 12970Sstevel@tonic-gate } 12980Sstevel@tonic-gate 12990Sstevel@tonic-gate static int 13000Sstevel@tonic-gate call_zoneadmd(const char *zone_name, zone_cmd_arg_t *arg) 13010Sstevel@tonic-gate { 13020Sstevel@tonic-gate char doorpath[PATH_MAX]; 13030Sstevel@tonic-gate int doorfd, result; 13040Sstevel@tonic-gate door_arg_t darg; 13050Sstevel@tonic-gate 13060Sstevel@tonic-gate zoneid_t zoneid; 13070Sstevel@tonic-gate uint64_t uniqid = 0; 13080Sstevel@tonic-gate 13090Sstevel@tonic-gate zone_cmd_rval_t *rvalp; 13100Sstevel@tonic-gate size_t rlen; 13110Sstevel@tonic-gate char *cp, *errbuf; 13120Sstevel@tonic-gate 13130Sstevel@tonic-gate rlen = getpagesize(); 13140Sstevel@tonic-gate if ((rvalp = malloc(rlen)) == NULL) { 13150Sstevel@tonic-gate zerror(gettext("failed to allocate %lu bytes: %s"), rlen, 13160Sstevel@tonic-gate strerror(errno)); 13170Sstevel@tonic-gate return (-1); 13180Sstevel@tonic-gate } 13190Sstevel@tonic-gate 13200Sstevel@tonic-gate if ((zoneid = getzoneidbyname(zone_name)) != ZONE_ID_UNDEFINED) { 13210Sstevel@tonic-gate (void) zone_getattr(zoneid, ZONE_ATTR_UNIQID, &uniqid, 13220Sstevel@tonic-gate sizeof (uniqid)); 13230Sstevel@tonic-gate } 13240Sstevel@tonic-gate arg->uniqid = uniqid; 13250Sstevel@tonic-gate (void) strlcpy(arg->locale, locale, sizeof (arg->locale)); 1326766Scarlsonj if (!get_doorname(zone_name, doorpath)) { 1327766Scarlsonj zerror(gettext("alternate root path is too long")); 1328766Scarlsonj free(rvalp); 1329766Scarlsonj return (-1); 1330766Scarlsonj } 13310Sstevel@tonic-gate 13320Sstevel@tonic-gate /* 13330Sstevel@tonic-gate * Loop trying to start zoneadmd; if something goes seriously 13340Sstevel@tonic-gate * wrong we break out and fail. 13350Sstevel@tonic-gate */ 13360Sstevel@tonic-gate for (;;) { 13370Sstevel@tonic-gate if (start_zoneadmd(zone_name) != Z_OK) 13380Sstevel@tonic-gate break; 13390Sstevel@tonic-gate 13400Sstevel@tonic-gate if ((doorfd = open(doorpath, O_RDONLY)) < 0) { 13410Sstevel@tonic-gate zperror(gettext("failed to open zone door"), B_FALSE); 13420Sstevel@tonic-gate break; 13430Sstevel@tonic-gate } 13440Sstevel@tonic-gate 13450Sstevel@tonic-gate darg.data_ptr = (char *)arg; 13460Sstevel@tonic-gate darg.data_size = sizeof (*arg); 13470Sstevel@tonic-gate darg.desc_ptr = NULL; 13480Sstevel@tonic-gate darg.desc_num = 0; 13490Sstevel@tonic-gate darg.rbuf = (char *)rvalp; 13500Sstevel@tonic-gate darg.rsize = rlen; 13510Sstevel@tonic-gate if (door_call(doorfd, &darg) != 0) { 13520Sstevel@tonic-gate (void) close(doorfd); 13530Sstevel@tonic-gate /* 13540Sstevel@tonic-gate * We'll get EBADF if the door has been revoked. 13550Sstevel@tonic-gate */ 13560Sstevel@tonic-gate if (errno != EBADF) { 13570Sstevel@tonic-gate zperror(gettext("door_call failed"), B_FALSE); 13580Sstevel@tonic-gate break; 13590Sstevel@tonic-gate } 13600Sstevel@tonic-gate continue; /* take another lap */ 13610Sstevel@tonic-gate } 13620Sstevel@tonic-gate (void) close(doorfd); 13630Sstevel@tonic-gate 13640Sstevel@tonic-gate if (darg.data_size == 0) { 13650Sstevel@tonic-gate /* Door server is going away; kick it again. */ 13660Sstevel@tonic-gate continue; 13670Sstevel@tonic-gate } 13680Sstevel@tonic-gate 13690Sstevel@tonic-gate errbuf = rvalp->errbuf; 13700Sstevel@tonic-gate while (*errbuf != '\0') { 13710Sstevel@tonic-gate /* 13720Sstevel@tonic-gate * Remove any newlines since zerror() 13730Sstevel@tonic-gate * will append one automatically. 13740Sstevel@tonic-gate */ 13750Sstevel@tonic-gate cp = strchr(errbuf, '\n'); 13760Sstevel@tonic-gate if (cp != NULL) 13770Sstevel@tonic-gate *cp = '\0'; 13780Sstevel@tonic-gate zerror("%s", errbuf); 13790Sstevel@tonic-gate if (cp == NULL) 13800Sstevel@tonic-gate break; 13810Sstevel@tonic-gate errbuf = cp + 1; 13820Sstevel@tonic-gate } 13830Sstevel@tonic-gate result = rvalp->rval == 0 ? 0 : -1; 13840Sstevel@tonic-gate free(rvalp); 13850Sstevel@tonic-gate return (result); 13860Sstevel@tonic-gate } 13870Sstevel@tonic-gate 13880Sstevel@tonic-gate free(rvalp); 13890Sstevel@tonic-gate return (-1); 13900Sstevel@tonic-gate } 13910Sstevel@tonic-gate 13920Sstevel@tonic-gate static int 13930Sstevel@tonic-gate ready_func(int argc, char *argv[]) 13940Sstevel@tonic-gate { 13950Sstevel@tonic-gate zone_cmd_arg_t zarg; 13960Sstevel@tonic-gate int arg; 13970Sstevel@tonic-gate 1398766Scarlsonj if (zonecfg_in_alt_root()) { 1399766Scarlsonj zerror(gettext("cannot ready zone in alternate root")); 1400766Scarlsonj return (Z_ERR); 1401766Scarlsonj } 1402766Scarlsonj 14030Sstevel@tonic-gate optind = 0; 14040Sstevel@tonic-gate if ((arg = getopt(argc, argv, "?")) != EOF) { 14050Sstevel@tonic-gate switch (arg) { 14060Sstevel@tonic-gate case '?': 14070Sstevel@tonic-gate sub_usage(SHELP_READY, CMD_READY); 14080Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 14090Sstevel@tonic-gate default: 14100Sstevel@tonic-gate sub_usage(SHELP_READY, CMD_READY); 14110Sstevel@tonic-gate return (Z_USAGE); 14120Sstevel@tonic-gate } 14130Sstevel@tonic-gate } 14140Sstevel@tonic-gate if (argc > optind) { 14150Sstevel@tonic-gate sub_usage(SHELP_READY, CMD_READY); 14160Sstevel@tonic-gate return (Z_USAGE); 14170Sstevel@tonic-gate } 14182712Snn35248 if (sanity_check(target_zone, CMD_READY, B_FALSE, B_FALSE, B_FALSE) 14192712Snn35248 != Z_OK) 14200Sstevel@tonic-gate return (Z_ERR); 14210Sstevel@tonic-gate if (verify_details(CMD_READY) != Z_OK) 14220Sstevel@tonic-gate return (Z_ERR); 14230Sstevel@tonic-gate 14240Sstevel@tonic-gate zarg.cmd = Z_READY; 14250Sstevel@tonic-gate if (call_zoneadmd(target_zone, &zarg) != 0) { 14260Sstevel@tonic-gate zerror(gettext("call to %s failed"), "zoneadmd"); 14270Sstevel@tonic-gate return (Z_ERR); 14280Sstevel@tonic-gate } 14290Sstevel@tonic-gate return (Z_OK); 14300Sstevel@tonic-gate } 14310Sstevel@tonic-gate 14320Sstevel@tonic-gate static int 14330Sstevel@tonic-gate boot_func(int argc, char *argv[]) 14340Sstevel@tonic-gate { 14350Sstevel@tonic-gate zone_cmd_arg_t zarg; 14362712Snn35248 boolean_t force = B_FALSE; 14370Sstevel@tonic-gate int arg; 14380Sstevel@tonic-gate 1439766Scarlsonj if (zonecfg_in_alt_root()) { 1440766Scarlsonj zerror(gettext("cannot boot zone in alternate root")); 1441766Scarlsonj return (Z_ERR); 1442766Scarlsonj } 1443766Scarlsonj 14440Sstevel@tonic-gate zarg.bootbuf[0] = '\0'; 14450Sstevel@tonic-gate 14460Sstevel@tonic-gate /* 14472267Sdp * The following getopt processes arguments to zone boot; that 14482267Sdp * is to say, the [here] portion of the argument string: 14492267Sdp * 14502267Sdp * zoneadm -z myzone boot [here] -- -v -m verbose 14512267Sdp * 14522267Sdp * Where [here] can either be nothing, -? (in which case we bail 14532712Snn35248 * and print usage), -f (a private option to indicate that the 14542712Snn35248 * boot operation should be 'forced'), or -s. Support for -s is 14552712Snn35248 * vestigal and obsolete, but is retained because it was a 14562712Snn35248 * documented interface and there are known consumers including 14572712Snn35248 * admin/install; the proper way to specify boot arguments like -s 14582712Snn35248 * is: 14592267Sdp * 14602267Sdp * zoneadm -z myzone boot -- -s -v -m verbose. 14610Sstevel@tonic-gate */ 14620Sstevel@tonic-gate optind = 0; 14632712Snn35248 while ((arg = getopt(argc, argv, "?fs")) != EOF) { 14640Sstevel@tonic-gate switch (arg) { 14650Sstevel@tonic-gate case '?': 14660Sstevel@tonic-gate sub_usage(SHELP_BOOT, CMD_BOOT); 14670Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 14680Sstevel@tonic-gate case 's': 14690Sstevel@tonic-gate (void) strlcpy(zarg.bootbuf, "-s", 14700Sstevel@tonic-gate sizeof (zarg.bootbuf)); 14710Sstevel@tonic-gate break; 14722712Snn35248 case 'f': 14732712Snn35248 force = B_TRUE; 14742712Snn35248 break; 14750Sstevel@tonic-gate default: 14760Sstevel@tonic-gate sub_usage(SHELP_BOOT, CMD_BOOT); 14770Sstevel@tonic-gate return (Z_USAGE); 14780Sstevel@tonic-gate } 14790Sstevel@tonic-gate } 14802267Sdp 14812267Sdp for (; optind < argc; optind++) { 14822267Sdp if (strlcat(zarg.bootbuf, argv[optind], 14832267Sdp sizeof (zarg.bootbuf)) >= sizeof (zarg.bootbuf)) { 14842267Sdp zerror(gettext("Boot argument list too long")); 14852267Sdp return (Z_ERR); 14862267Sdp } 14872267Sdp if (optind < argc - 1) 14882267Sdp if (strlcat(zarg.bootbuf, " ", sizeof (zarg.bootbuf)) >= 14892267Sdp sizeof (zarg.bootbuf)) { 14902267Sdp zerror(gettext("Boot argument list too long")); 14912267Sdp return (Z_ERR); 14922267Sdp } 14932267Sdp } 14942712Snn35248 if (sanity_check(target_zone, CMD_BOOT, B_FALSE, B_FALSE, force) 14952712Snn35248 != Z_OK) 14960Sstevel@tonic-gate return (Z_ERR); 14970Sstevel@tonic-gate if (verify_details(CMD_BOOT) != Z_OK) 14980Sstevel@tonic-gate return (Z_ERR); 14992712Snn35248 zarg.cmd = force ? Z_FORCEBOOT : Z_BOOT; 15000Sstevel@tonic-gate if (call_zoneadmd(target_zone, &zarg) != 0) { 15010Sstevel@tonic-gate zerror(gettext("call to %s failed"), "zoneadmd"); 15020Sstevel@tonic-gate return (Z_ERR); 15030Sstevel@tonic-gate } 15040Sstevel@tonic-gate return (Z_OK); 15050Sstevel@tonic-gate } 15060Sstevel@tonic-gate 15070Sstevel@tonic-gate static void 15080Sstevel@tonic-gate fake_up_local_zone(zoneid_t zid, zone_entry_t *zeptr) 15090Sstevel@tonic-gate { 15100Sstevel@tonic-gate ssize_t result; 15112303Scarlsonj uuid_t uuid; 15122303Scarlsonj FILE *fp; 15132303Scarlsonj 15142303Scarlsonj (void) memset(zeptr, 0, sizeof (*zeptr)); 15150Sstevel@tonic-gate 15160Sstevel@tonic-gate zeptr->zid = zid; 15172303Scarlsonj 15180Sstevel@tonic-gate /* 15190Sstevel@tonic-gate * Since we're looking up our own (non-global) zone name, 15200Sstevel@tonic-gate * we can be assured that it will succeed. 15210Sstevel@tonic-gate */ 15220Sstevel@tonic-gate result = getzonenamebyid(zid, zeptr->zname, sizeof (zeptr->zname)); 15230Sstevel@tonic-gate assert(result >= 0); 15242303Scarlsonj if (zonecfg_is_scratch(zeptr->zname) && 15252303Scarlsonj (fp = zonecfg_open_scratch("", B_FALSE)) != NULL) { 15262303Scarlsonj (void) zonecfg_reverse_scratch(fp, zeptr->zname, zeptr->zname, 15272303Scarlsonj sizeof (zeptr->zname), NULL, 0); 15282303Scarlsonj zonecfg_close_scratch(fp); 15292303Scarlsonj } 15302303Scarlsonj 15312303Scarlsonj if (is_system_labeled()) { 15321676Sjpk (void) zone_getattr(zid, ZONE_ATTR_ROOT, zeptr->zroot, 15331676Sjpk sizeof (zeptr->zroot)); 15342712Snn35248 (void) strlcpy(zeptr->zbrand, NATIVE_BRAND_NAME, 15352712Snn35248 sizeof (zeptr->zbrand)); 15362303Scarlsonj } else { 15372303Scarlsonj (void) strlcpy(zeptr->zroot, "/", sizeof (zeptr->zroot)); 15382712Snn35248 (void) zone_getattr(zid, ZONE_ATTR_BRAND, zeptr->zbrand, 15392712Snn35248 sizeof (zeptr->zbrand)); 15402303Scarlsonj } 15412303Scarlsonj 15420Sstevel@tonic-gate zeptr->zstate_str = "running"; 15432303Scarlsonj if (zonecfg_get_uuid(zeptr->zname, uuid) == Z_OK && 15442303Scarlsonj !uuid_is_null(uuid)) 15452303Scarlsonj uuid_unparse(uuid, zeptr->zuuid); 15460Sstevel@tonic-gate } 15470Sstevel@tonic-gate 15480Sstevel@tonic-gate static int 15490Sstevel@tonic-gate list_func(int argc, char *argv[]) 15500Sstevel@tonic-gate { 15510Sstevel@tonic-gate zone_entry_t *zentp, zent; 1552766Scarlsonj int arg, retv; 15530Sstevel@tonic-gate boolean_t output = B_FALSE, verbose = B_FALSE, parsable = B_FALSE; 15540Sstevel@tonic-gate zone_state_t min_state = ZONE_STATE_RUNNING; 15550Sstevel@tonic-gate zoneid_t zone_id = getzoneid(); 15560Sstevel@tonic-gate 15570Sstevel@tonic-gate if (target_zone == NULL) { 15580Sstevel@tonic-gate /* all zones: default view to running but allow override */ 15590Sstevel@tonic-gate optind = 0; 15600Sstevel@tonic-gate while ((arg = getopt(argc, argv, "?cipv")) != EOF) { 15610Sstevel@tonic-gate switch (arg) { 15620Sstevel@tonic-gate case '?': 15630Sstevel@tonic-gate sub_usage(SHELP_LIST, CMD_LIST); 15640Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 15651339Sjonb /* 15661339Sjonb * The 'i' and 'c' options are not mutually 15671339Sjonb * exclusive so if 'c' is given, then min_state 15681339Sjonb * is set to 0 (ZONE_STATE_CONFIGURED) which is 15691339Sjonb * the lowest possible state. If 'i' is given, 15701339Sjonb * then min_state is set to be the lowest state 15711339Sjonb * so far. 15721339Sjonb */ 15730Sstevel@tonic-gate case 'c': 15740Sstevel@tonic-gate min_state = ZONE_STATE_CONFIGURED; 15750Sstevel@tonic-gate break; 15760Sstevel@tonic-gate case 'i': 15771339Sjonb min_state = min(ZONE_STATE_INSTALLED, 15781339Sjonb min_state); 15791339Sjonb 15800Sstevel@tonic-gate break; 15810Sstevel@tonic-gate case 'p': 15820Sstevel@tonic-gate parsable = B_TRUE; 15830Sstevel@tonic-gate break; 15840Sstevel@tonic-gate case 'v': 15850Sstevel@tonic-gate verbose = B_TRUE; 15860Sstevel@tonic-gate break; 15870Sstevel@tonic-gate default: 15880Sstevel@tonic-gate sub_usage(SHELP_LIST, CMD_LIST); 15890Sstevel@tonic-gate return (Z_USAGE); 15900Sstevel@tonic-gate } 15910Sstevel@tonic-gate } 15920Sstevel@tonic-gate if (parsable && verbose) { 15930Sstevel@tonic-gate zerror(gettext("%s -p and -v are mutually exclusive."), 15940Sstevel@tonic-gate cmd_to_str(CMD_LIST)); 15950Sstevel@tonic-gate return (Z_ERR); 15960Sstevel@tonic-gate } 15971676Sjpk if (zone_id == GLOBAL_ZONEID || is_system_labeled()) { 1598766Scarlsonj retv = zone_print_list(min_state, verbose, parsable); 15990Sstevel@tonic-gate } else { 16002712Snn35248 fake_up_local_zone(zone_id, &zent); 1601766Scarlsonj retv = Z_OK; 16020Sstevel@tonic-gate zone_print(&zent, verbose, parsable); 16030Sstevel@tonic-gate } 1604766Scarlsonj return (retv); 16050Sstevel@tonic-gate } 16060Sstevel@tonic-gate 16070Sstevel@tonic-gate /* 16080Sstevel@tonic-gate * Specific target zone: disallow -i/-c suboptions. 16090Sstevel@tonic-gate */ 16100Sstevel@tonic-gate optind = 0; 16110Sstevel@tonic-gate while ((arg = getopt(argc, argv, "?pv")) != EOF) { 16120Sstevel@tonic-gate switch (arg) { 16130Sstevel@tonic-gate case '?': 16140Sstevel@tonic-gate sub_usage(SHELP_LIST, CMD_LIST); 16150Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 16160Sstevel@tonic-gate case 'p': 16170Sstevel@tonic-gate parsable = B_TRUE; 16180Sstevel@tonic-gate break; 16190Sstevel@tonic-gate case 'v': 16200Sstevel@tonic-gate verbose = B_TRUE; 16210Sstevel@tonic-gate break; 16220Sstevel@tonic-gate default: 16230Sstevel@tonic-gate sub_usage(SHELP_LIST, CMD_LIST); 16240Sstevel@tonic-gate return (Z_USAGE); 16250Sstevel@tonic-gate } 16260Sstevel@tonic-gate } 16270Sstevel@tonic-gate if (parsable && verbose) { 16280Sstevel@tonic-gate zerror(gettext("%s -p and -v are mutually exclusive."), 16290Sstevel@tonic-gate cmd_to_str(CMD_LIST)); 16300Sstevel@tonic-gate return (Z_ERR); 16310Sstevel@tonic-gate } 16320Sstevel@tonic-gate if (argc > optind) { 16330Sstevel@tonic-gate sub_usage(SHELP_LIST, CMD_LIST); 16340Sstevel@tonic-gate return (Z_USAGE); 16350Sstevel@tonic-gate } 16360Sstevel@tonic-gate if (zone_id != GLOBAL_ZONEID) { 16370Sstevel@tonic-gate fake_up_local_zone(zone_id, &zent); 16380Sstevel@tonic-gate /* 16390Sstevel@tonic-gate * main() will issue a Z_NO_ZONE error if it cannot get an 16400Sstevel@tonic-gate * id for target_zone, which in a non-global zone should 16410Sstevel@tonic-gate * happen for any zone name except `zonename`. Thus we 16420Sstevel@tonic-gate * assert() that here but don't otherwise check. 16430Sstevel@tonic-gate */ 16440Sstevel@tonic-gate assert(strcmp(zent.zname, target_zone) == 0); 16450Sstevel@tonic-gate zone_print(&zent, verbose, parsable); 16460Sstevel@tonic-gate output = B_TRUE; 16470Sstevel@tonic-gate } else if ((zentp = lookup_running_zone(target_zone)) != NULL) { 16480Sstevel@tonic-gate zone_print(zentp, verbose, parsable); 16490Sstevel@tonic-gate output = B_TRUE; 1650766Scarlsonj } else if (lookup_zone_info(target_zone, ZONE_ID_UNDEFINED, 1651766Scarlsonj &zent) == Z_OK) { 16520Sstevel@tonic-gate zone_print(&zent, verbose, parsable); 16530Sstevel@tonic-gate output = B_TRUE; 16540Sstevel@tonic-gate } 16550Sstevel@tonic-gate return (output ? Z_OK : Z_ERR); 16560Sstevel@tonic-gate } 16570Sstevel@tonic-gate 16580Sstevel@tonic-gate static void 16590Sstevel@tonic-gate sigterm(int sig) 16600Sstevel@tonic-gate { 16610Sstevel@tonic-gate /* 16620Sstevel@tonic-gate * Ignore SIG{INT,TERM}, so we don't end up in an infinite loop, 16630Sstevel@tonic-gate * then propagate the signal to our process group. 16640Sstevel@tonic-gate */ 16652712Snn35248 assert(sig == SIGINT || sig == SIGTERM); 16660Sstevel@tonic-gate (void) sigset(SIGINT, SIG_IGN); 16670Sstevel@tonic-gate (void) sigset(SIGTERM, SIG_IGN); 16680Sstevel@tonic-gate (void) kill(0, sig); 16690Sstevel@tonic-gate child_killed = B_TRUE; 16700Sstevel@tonic-gate } 16710Sstevel@tonic-gate 16720Sstevel@tonic-gate static int 16730Sstevel@tonic-gate do_subproc(char *cmdbuf) 16740Sstevel@tonic-gate { 16750Sstevel@tonic-gate char inbuf[1024]; /* arbitrary large amount */ 16760Sstevel@tonic-gate FILE *file; 16770Sstevel@tonic-gate 16782712Snn35248 do_subproc_cnt++; 16790Sstevel@tonic-gate child_killed = B_FALSE; 16800Sstevel@tonic-gate /* 16810Sstevel@tonic-gate * We use popen(3c) to launch child processes for [un]install; 16820Sstevel@tonic-gate * this library call does not return a PID, so we have to kill 16830Sstevel@tonic-gate * the whole process group. To avoid killing our parent, we 16840Sstevel@tonic-gate * become a process group leader here. But doing so can wreak 16850Sstevel@tonic-gate * havoc with reading from stdin when launched by a non-job-control 16860Sstevel@tonic-gate * shell, so we close stdin and reopen it as /dev/null first. 16870Sstevel@tonic-gate */ 16880Sstevel@tonic-gate (void) close(STDIN_FILENO); 16892712Snn35248 (void) openat(STDIN_FILENO, "/dev/null", O_RDONLY); 16902712Snn35248 if (!zoneadm_is_nested) 16912712Snn35248 (void) setpgid(0, 0); 16920Sstevel@tonic-gate (void) sigset(SIGINT, sigterm); 16930Sstevel@tonic-gate (void) sigset(SIGTERM, sigterm); 16940Sstevel@tonic-gate file = popen(cmdbuf, "r"); 16950Sstevel@tonic-gate for (;;) { 16960Sstevel@tonic-gate if (child_killed || fgets(inbuf, sizeof (inbuf), file) == NULL) 16970Sstevel@tonic-gate break; 16980Sstevel@tonic-gate (void) fputs(inbuf, stdout); 16990Sstevel@tonic-gate } 17000Sstevel@tonic-gate (void) sigset(SIGINT, SIG_DFL); 17010Sstevel@tonic-gate (void) sigset(SIGTERM, SIG_DFL); 17020Sstevel@tonic-gate return (pclose(file)); 17030Sstevel@tonic-gate } 17040Sstevel@tonic-gate 17050Sstevel@tonic-gate static int 17062712Snn35248 do_subproc_interactive(char *cmdbuf) 17072712Snn35248 { 17082712Snn35248 void (*saveint)(int); 17092712Snn35248 void (*saveterm)(int); 17102712Snn35248 void (*savequit)(int); 17112712Snn35248 void (*savehup)(int); 17122712Snn35248 int pid, child, status; 17132712Snn35248 17142712Snn35248 /* 17152712Snn35248 * do_subproc() links stdin to /dev/null, which would break any 17162712Snn35248 * interactive subprocess we try to launch here. Similarly, we 17172712Snn35248 * can't have been launched as a subprocess ourselves. 17182712Snn35248 */ 17192712Snn35248 assert(do_subproc_cnt == 0 && !zoneadm_is_nested); 17202712Snn35248 17212712Snn35248 if ((child = vfork()) == 0) { 17222712Snn35248 (void) execl("/bin/sh", "sh", "-c", cmdbuf, (char *)NULL); 17232712Snn35248 } 17242712Snn35248 17252712Snn35248 if (child == -1) 17262712Snn35248 return (-1); 17272712Snn35248 17282712Snn35248 saveint = sigset(SIGINT, SIG_IGN); 17292712Snn35248 saveterm = sigset(SIGTERM, SIG_IGN); 17302712Snn35248 savequit = sigset(SIGQUIT, SIG_IGN); 17312712Snn35248 savehup = sigset(SIGHUP, SIG_IGN); 17322712Snn35248 17332712Snn35248 while ((pid = waitpid(child, &status, 0)) != child && pid != -1) 17342712Snn35248 ; 17352712Snn35248 17362712Snn35248 (void) sigset(SIGINT, saveint); 17372712Snn35248 (void) sigset(SIGTERM, saveterm); 17382712Snn35248 (void) sigset(SIGQUIT, savequit); 17392712Snn35248 (void) sigset(SIGHUP, savehup); 17402712Snn35248 17412712Snn35248 return (pid == -1 ? -1 : status); 17422712Snn35248 } 17432712Snn35248 17442712Snn35248 static int 17452712Snn35248 subproc_status(const char *cmd, int status, boolean_t verbose_failure) 17460Sstevel@tonic-gate { 17470Sstevel@tonic-gate if (WIFEXITED(status)) { 17480Sstevel@tonic-gate int exit_code = WEXITSTATUS(status); 17490Sstevel@tonic-gate 17502712Snn35248 if ((verbose_failure) && (exit_code != ZONE_SUBPROC_OK)) 17512712Snn35248 zerror(gettext("'%s' failed with exit code %d."), cmd, 17522712Snn35248 exit_code); 17532712Snn35248 17542712Snn35248 return (exit_code); 17550Sstevel@tonic-gate } else if (WIFSIGNALED(status)) { 17560Sstevel@tonic-gate int signal = WTERMSIG(status); 17570Sstevel@tonic-gate char sigstr[SIG2STR_MAX]; 17580Sstevel@tonic-gate 17590Sstevel@tonic-gate if (sig2str(signal, sigstr) == 0) { 17600Sstevel@tonic-gate zerror(gettext("'%s' terminated by signal SIG%s."), cmd, 17610Sstevel@tonic-gate sigstr); 17620Sstevel@tonic-gate } else { 17630Sstevel@tonic-gate zerror(gettext("'%s' terminated by an unknown signal."), 17640Sstevel@tonic-gate cmd); 17650Sstevel@tonic-gate } 17660Sstevel@tonic-gate } else { 17670Sstevel@tonic-gate zerror(gettext("'%s' failed for unknown reasons."), cmd); 17680Sstevel@tonic-gate } 17692712Snn35248 17702712Snn35248 /* 17712712Snn35248 * Assume a subprocess that died due to a signal or an unknown error 17722712Snn35248 * should be considered an exit code of ZONE_SUBPROC_FATAL, as the 17732712Snn35248 * user will likely need to do some manual cleanup. 17742712Snn35248 */ 17752712Snn35248 return (ZONE_SUBPROC_FATAL); 17760Sstevel@tonic-gate } 17770Sstevel@tonic-gate 17780Sstevel@tonic-gate /* 17790Sstevel@tonic-gate * Various sanity checks; make sure: 17800Sstevel@tonic-gate * 1. We're in the global zone. 17810Sstevel@tonic-gate * 2. The calling user has sufficient privilege. 17820Sstevel@tonic-gate * 3. The target zone is neither the global zone nor anything starting with 17830Sstevel@tonic-gate * "SUNW". 17840Sstevel@tonic-gate * 4a. If we're looking for a 'not running' (i.e., configured or installed) 17850Sstevel@tonic-gate * zone, the name service knows about it. 17860Sstevel@tonic-gate * 4b. For some operations which expect a zone not to be running, that it is 17870Sstevel@tonic-gate * not already running (or ready). 17880Sstevel@tonic-gate */ 17890Sstevel@tonic-gate static int 17900Sstevel@tonic-gate sanity_check(char *zone, int cmd_num, boolean_t running, 17912712Snn35248 boolean_t unsafe_when_running, boolean_t force) 17920Sstevel@tonic-gate { 17930Sstevel@tonic-gate zone_entry_t *zent; 17940Sstevel@tonic-gate priv_set_t *privset; 17952712Snn35248 zone_state_t state, min_state; 1796766Scarlsonj char kernzone[ZONENAME_MAX]; 1797766Scarlsonj FILE *fp; 17980Sstevel@tonic-gate 17990Sstevel@tonic-gate if (getzoneid() != GLOBAL_ZONEID) { 18001645Scomay switch (cmd_num) { 18011645Scomay case CMD_HALT: 18021645Scomay zerror(gettext("use %s to %s this zone."), "halt(1M)", 18031645Scomay cmd_to_str(cmd_num)); 18041645Scomay break; 18051645Scomay case CMD_REBOOT: 18061645Scomay zerror(gettext("use %s to %s this zone."), 18071645Scomay "reboot(1M)", cmd_to_str(cmd_num)); 18081645Scomay break; 18091645Scomay default: 18101645Scomay zerror(gettext("must be in the global zone to %s a " 18111645Scomay "zone."), cmd_to_str(cmd_num)); 18121645Scomay break; 18131645Scomay } 18140Sstevel@tonic-gate return (Z_ERR); 18150Sstevel@tonic-gate } 18160Sstevel@tonic-gate 18170Sstevel@tonic-gate if ((privset = priv_allocset()) == NULL) { 18180Sstevel@tonic-gate zerror(gettext("%s failed"), "priv_allocset"); 18190Sstevel@tonic-gate return (Z_ERR); 18200Sstevel@tonic-gate } 18210Sstevel@tonic-gate 18220Sstevel@tonic-gate if (getppriv(PRIV_EFFECTIVE, privset) != 0) { 18230Sstevel@tonic-gate zerror(gettext("%s failed"), "getppriv"); 18240Sstevel@tonic-gate priv_freeset(privset); 18250Sstevel@tonic-gate return (Z_ERR); 18260Sstevel@tonic-gate } 18270Sstevel@tonic-gate 18280Sstevel@tonic-gate if (priv_isfullset(privset) == B_FALSE) { 18290Sstevel@tonic-gate zerror(gettext("only a privileged user may %s a zone."), 18300Sstevel@tonic-gate cmd_to_str(cmd_num)); 18310Sstevel@tonic-gate priv_freeset(privset); 18320Sstevel@tonic-gate return (Z_ERR); 18330Sstevel@tonic-gate } 18340Sstevel@tonic-gate priv_freeset(privset); 18350Sstevel@tonic-gate 18360Sstevel@tonic-gate if (zone == NULL) { 18370Sstevel@tonic-gate zerror(gettext("no zone specified")); 18380Sstevel@tonic-gate return (Z_ERR); 18390Sstevel@tonic-gate } 18400Sstevel@tonic-gate 18410Sstevel@tonic-gate if (strcmp(zone, GLOBAL_ZONENAME) == 0) { 18420Sstevel@tonic-gate zerror(gettext("%s operation is invalid for the global zone."), 18430Sstevel@tonic-gate cmd_to_str(cmd_num)); 18440Sstevel@tonic-gate return (Z_ERR); 18450Sstevel@tonic-gate } 18460Sstevel@tonic-gate 18470Sstevel@tonic-gate if (strncmp(zone, "SUNW", 4) == 0) { 18480Sstevel@tonic-gate zerror(gettext("%s operation is invalid for zones starting " 18490Sstevel@tonic-gate "with SUNW."), cmd_to_str(cmd_num)); 18500Sstevel@tonic-gate return (Z_ERR); 18510Sstevel@tonic-gate } 18520Sstevel@tonic-gate 18532712Snn35248 if (!is_native_zone && cmd_num == CMD_MOUNT) { 18542712Snn35248 zerror(gettext("%s operation is invalid for branded zones."), 18552712Snn35248 cmd_to_str(cmd_num)); 18562712Snn35248 return (Z_ERR); 18572712Snn35248 } 18582712Snn35248 1859766Scarlsonj if (!zonecfg_in_alt_root()) { 1860766Scarlsonj zent = lookup_running_zone(zone); 1861766Scarlsonj } else if ((fp = zonecfg_open_scratch("", B_FALSE)) == NULL) { 1862766Scarlsonj zent = NULL; 1863766Scarlsonj } else { 1864766Scarlsonj if (zonecfg_find_scratch(fp, zone, zonecfg_get_root(), 1865766Scarlsonj kernzone, sizeof (kernzone)) == 0) 1866766Scarlsonj zent = lookup_running_zone(kernzone); 1867766Scarlsonj else 1868766Scarlsonj zent = NULL; 1869766Scarlsonj zonecfg_close_scratch(fp); 1870766Scarlsonj } 1871766Scarlsonj 18720Sstevel@tonic-gate /* 18730Sstevel@tonic-gate * Look up from the kernel for 'running' zones. 18740Sstevel@tonic-gate */ 18752712Snn35248 if (running && !force) { 18760Sstevel@tonic-gate if (zent == NULL) { 18770Sstevel@tonic-gate zerror(gettext("not running")); 18780Sstevel@tonic-gate return (Z_ERR); 18790Sstevel@tonic-gate } 18800Sstevel@tonic-gate } else { 18810Sstevel@tonic-gate int err; 18820Sstevel@tonic-gate 18830Sstevel@tonic-gate if (unsafe_when_running && zent != NULL) { 18840Sstevel@tonic-gate /* check whether the zone is ready or running */ 18850Sstevel@tonic-gate if ((err = zone_get_state(zent->zname, 18860Sstevel@tonic-gate &zent->zstate_num)) != Z_OK) { 18870Sstevel@tonic-gate errno = err; 18880Sstevel@tonic-gate zperror2(zent->zname, 18890Sstevel@tonic-gate gettext("could not get state")); 18900Sstevel@tonic-gate /* can't tell, so hedge */ 18910Sstevel@tonic-gate zent->zstate_str = "ready/running"; 18920Sstevel@tonic-gate } else { 18930Sstevel@tonic-gate zent->zstate_str = 18940Sstevel@tonic-gate zone_state_str(zent->zstate_num); 18950Sstevel@tonic-gate } 18960Sstevel@tonic-gate zerror(gettext("%s operation is invalid for %s zones."), 18970Sstevel@tonic-gate cmd_to_str(cmd_num), zent->zstate_str); 18980Sstevel@tonic-gate return (Z_ERR); 18990Sstevel@tonic-gate } 19000Sstevel@tonic-gate if ((err = zone_get_state(zone, &state)) != Z_OK) { 19010Sstevel@tonic-gate errno = err; 19020Sstevel@tonic-gate zperror2(zone, gettext("could not get state")); 19030Sstevel@tonic-gate return (Z_ERR); 19040Sstevel@tonic-gate } 19050Sstevel@tonic-gate switch (cmd_num) { 19060Sstevel@tonic-gate case CMD_UNINSTALL: 19070Sstevel@tonic-gate if (state == ZONE_STATE_CONFIGURED) { 19080Sstevel@tonic-gate zerror(gettext("is already in state '%s'."), 19090Sstevel@tonic-gate zone_state_str(ZONE_STATE_CONFIGURED)); 19100Sstevel@tonic-gate return (Z_ERR); 19110Sstevel@tonic-gate } 19120Sstevel@tonic-gate break; 19131507Sgjelinek case CMD_ATTACH: 19141300Sgjelinek case CMD_CLONE: 19150Sstevel@tonic-gate case CMD_INSTALL: 19160Sstevel@tonic-gate if (state == ZONE_STATE_INSTALLED) { 19170Sstevel@tonic-gate zerror(gettext("is already %s."), 19180Sstevel@tonic-gate zone_state_str(ZONE_STATE_INSTALLED)); 19190Sstevel@tonic-gate return (Z_ERR); 19200Sstevel@tonic-gate } else if (state == ZONE_STATE_INCOMPLETE) { 19210Sstevel@tonic-gate zerror(gettext("zone is %s; %s required."), 19220Sstevel@tonic-gate zone_state_str(ZONE_STATE_INCOMPLETE), 19230Sstevel@tonic-gate cmd_to_str(CMD_UNINSTALL)); 19240Sstevel@tonic-gate return (Z_ERR); 19250Sstevel@tonic-gate } 19260Sstevel@tonic-gate break; 19271507Sgjelinek case CMD_DETACH: 19281300Sgjelinek case CMD_MOVE: 19290Sstevel@tonic-gate case CMD_READY: 19300Sstevel@tonic-gate case CMD_BOOT: 1931766Scarlsonj case CMD_MOUNT: 19322303Scarlsonj case CMD_MARK: 19332712Snn35248 if ((cmd_num == CMD_BOOT || cmd_num == CMD_MOUNT) && 19342712Snn35248 force) 19352712Snn35248 min_state = ZONE_STATE_INCOMPLETE; 19362712Snn35248 else 19372712Snn35248 min_state = ZONE_STATE_INSTALLED; 19382712Snn35248 19392712Snn35248 if (force && cmd_num == CMD_BOOT && is_native_zone) { 19402712Snn35248 zerror(gettext("Only branded zones may be " 19412712Snn35248 "force-booted.")); 19422712Snn35248 return (Z_ERR); 19432712Snn35248 } 19442712Snn35248 19452712Snn35248 if (state < min_state) { 19460Sstevel@tonic-gate zerror(gettext("must be %s before %s."), 19472712Snn35248 zone_state_str(min_state), 19480Sstevel@tonic-gate cmd_to_str(cmd_num)); 19490Sstevel@tonic-gate return (Z_ERR); 19500Sstevel@tonic-gate } 19510Sstevel@tonic-gate break; 19520Sstevel@tonic-gate case CMD_VERIFY: 19530Sstevel@tonic-gate if (state == ZONE_STATE_INCOMPLETE) { 19540Sstevel@tonic-gate zerror(gettext("zone is %s; %s required."), 19550Sstevel@tonic-gate zone_state_str(ZONE_STATE_INCOMPLETE), 19560Sstevel@tonic-gate cmd_to_str(CMD_UNINSTALL)); 19570Sstevel@tonic-gate return (Z_ERR); 19580Sstevel@tonic-gate } 19590Sstevel@tonic-gate break; 1960766Scarlsonj case CMD_UNMOUNT: 1961766Scarlsonj if (state != ZONE_STATE_MOUNTED) { 1962766Scarlsonj zerror(gettext("must be %s before %s."), 1963766Scarlsonj zone_state_str(ZONE_STATE_MOUNTED), 1964766Scarlsonj cmd_to_str(cmd_num)); 1965766Scarlsonj return (Z_ERR); 1966766Scarlsonj } 1967766Scarlsonj break; 19680Sstevel@tonic-gate } 19690Sstevel@tonic-gate } 19700Sstevel@tonic-gate return (Z_OK); 19710Sstevel@tonic-gate } 19720Sstevel@tonic-gate 19730Sstevel@tonic-gate static int 19740Sstevel@tonic-gate halt_func(int argc, char *argv[]) 19750Sstevel@tonic-gate { 19760Sstevel@tonic-gate zone_cmd_arg_t zarg; 19770Sstevel@tonic-gate int arg; 19780Sstevel@tonic-gate 1979766Scarlsonj if (zonecfg_in_alt_root()) { 1980766Scarlsonj zerror(gettext("cannot halt zone in alternate root")); 1981766Scarlsonj return (Z_ERR); 1982766Scarlsonj } 1983766Scarlsonj 19840Sstevel@tonic-gate optind = 0; 19850Sstevel@tonic-gate if ((arg = getopt(argc, argv, "?")) != EOF) { 19860Sstevel@tonic-gate switch (arg) { 19870Sstevel@tonic-gate case '?': 19880Sstevel@tonic-gate sub_usage(SHELP_HALT, CMD_HALT); 19890Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 19900Sstevel@tonic-gate default: 19910Sstevel@tonic-gate sub_usage(SHELP_HALT, CMD_HALT); 19920Sstevel@tonic-gate return (Z_USAGE); 19930Sstevel@tonic-gate } 19940Sstevel@tonic-gate } 19950Sstevel@tonic-gate if (argc > optind) { 19960Sstevel@tonic-gate sub_usage(SHELP_HALT, CMD_HALT); 19970Sstevel@tonic-gate return (Z_USAGE); 19980Sstevel@tonic-gate } 19990Sstevel@tonic-gate /* 20000Sstevel@tonic-gate * zoneadmd should be the one to decide whether or not to proceed, 20010Sstevel@tonic-gate * so even though it seems that the fourth parameter below should 20020Sstevel@tonic-gate * perhaps be B_TRUE, it really shouldn't be. 20030Sstevel@tonic-gate */ 20042712Snn35248 if (sanity_check(target_zone, CMD_HALT, B_FALSE, B_FALSE, B_FALSE) 20052712Snn35248 != Z_OK) 20060Sstevel@tonic-gate return (Z_ERR); 20070Sstevel@tonic-gate 20080Sstevel@tonic-gate zarg.cmd = Z_HALT; 20090Sstevel@tonic-gate return ((call_zoneadmd(target_zone, &zarg) == 0) ? Z_OK : Z_ERR); 20100Sstevel@tonic-gate } 20110Sstevel@tonic-gate 20120Sstevel@tonic-gate static int 20130Sstevel@tonic-gate reboot_func(int argc, char *argv[]) 20140Sstevel@tonic-gate { 20150Sstevel@tonic-gate zone_cmd_arg_t zarg; 20160Sstevel@tonic-gate int arg; 20170Sstevel@tonic-gate 2018766Scarlsonj if (zonecfg_in_alt_root()) { 2019766Scarlsonj zerror(gettext("cannot reboot zone in alternate root")); 2020766Scarlsonj return (Z_ERR); 2021766Scarlsonj } 2022766Scarlsonj 20230Sstevel@tonic-gate optind = 0; 20240Sstevel@tonic-gate if ((arg = getopt(argc, argv, "?")) != EOF) { 20250Sstevel@tonic-gate switch (arg) { 20260Sstevel@tonic-gate case '?': 20270Sstevel@tonic-gate sub_usage(SHELP_REBOOT, CMD_REBOOT); 20280Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 20290Sstevel@tonic-gate default: 20300Sstevel@tonic-gate sub_usage(SHELP_REBOOT, CMD_REBOOT); 20310Sstevel@tonic-gate return (Z_USAGE); 20320Sstevel@tonic-gate } 20330Sstevel@tonic-gate } 20342267Sdp 20352267Sdp zarg.bootbuf[0] = '\0'; 20362267Sdp for (; optind < argc; optind++) { 20372267Sdp if (strlcat(zarg.bootbuf, argv[optind], 20382267Sdp sizeof (zarg.bootbuf)) >= sizeof (zarg.bootbuf)) { 20392267Sdp zerror(gettext("Boot argument list too long")); 20402267Sdp return (Z_ERR); 20412267Sdp } 20422267Sdp if (optind < argc - 1) 20432267Sdp if (strlcat(zarg.bootbuf, " ", sizeof (zarg.bootbuf)) >= 20442267Sdp sizeof (zarg.bootbuf)) { 20452267Sdp zerror(gettext("Boot argument list too long")); 20462267Sdp return (Z_ERR); 20472267Sdp } 20482267Sdp } 20492267Sdp 20502267Sdp 20510Sstevel@tonic-gate /* 20520Sstevel@tonic-gate * zoneadmd should be the one to decide whether or not to proceed, 20530Sstevel@tonic-gate * so even though it seems that the fourth parameter below should 20540Sstevel@tonic-gate * perhaps be B_TRUE, it really shouldn't be. 20550Sstevel@tonic-gate */ 20562712Snn35248 if (sanity_check(target_zone, CMD_REBOOT, B_TRUE, B_FALSE, B_FALSE) 20572712Snn35248 != Z_OK) 20580Sstevel@tonic-gate return (Z_ERR); 2059823Sgjelinek if (verify_details(CMD_REBOOT) != Z_OK) 2060823Sgjelinek return (Z_ERR); 20610Sstevel@tonic-gate 20620Sstevel@tonic-gate zarg.cmd = Z_REBOOT; 20630Sstevel@tonic-gate return ((call_zoneadmd(target_zone, &zarg) == 0) ? Z_OK : Z_ERR); 20640Sstevel@tonic-gate } 20650Sstevel@tonic-gate 20660Sstevel@tonic-gate static int 20672712Snn35248 verify_brand(zone_dochandle_t handle) 20682712Snn35248 { 20692712Snn35248 char cmdbuf[MAXPATHLEN]; 20702712Snn35248 int err; 20712712Snn35248 char zonepath[MAXPATHLEN]; 20722727Sedp brand_handle_t bh = NULL; 20732712Snn35248 int status; 20742712Snn35248 20752712Snn35248 /* 20762712Snn35248 * Fetch the verify command from the brand configuration. 20772712Snn35248 * "exec" the command so that the returned status is that of 20782712Snn35248 * the command and not the shell. 20792712Snn35248 */ 20802712Snn35248 if ((err = zonecfg_get_zonepath(handle, zonepath, sizeof (zonepath))) != 20812712Snn35248 Z_OK) { 20822712Snn35248 errno = err; 20832712Snn35248 zperror(cmd_to_str(CMD_VERIFY), B_TRUE); 20842712Snn35248 return (Z_ERR); 20852712Snn35248 } 20862727Sedp if ((bh = brand_open(target_brand)) == NULL) { 20872712Snn35248 zerror(gettext("missing or invalid brand")); 20882712Snn35248 return (Z_ERR); 20892712Snn35248 } 20902712Snn35248 20912712Snn35248 /* 20922712Snn35248 * If the brand has its own verification routine, execute it now. 20932712Snn35248 */ 20942712Snn35248 (void) strcpy(cmdbuf, EXEC_PREFIX); 20952727Sedp err = brand_get_verify_adm(bh, target_zone, zonepath, 20962712Snn35248 cmdbuf + EXEC_LEN, sizeof (cmdbuf) - EXEC_LEN, 0, NULL); 20972727Sedp brand_close(bh); 20982712Snn35248 if (err == 0 && strlen(cmdbuf) > EXEC_LEN) { 20992712Snn35248 status = do_subproc_interactive(cmdbuf); 21002712Snn35248 err = subproc_status(gettext("brand-specific verification"), 21012712Snn35248 status, B_FALSE); 21022712Snn35248 return ((err == ZONE_SUBPROC_OK) ? Z_OK : Z_BRAND_ERROR); 21032712Snn35248 } 21042712Snn35248 21052712Snn35248 return ((err == Z_OK) ? Z_OK : Z_BRAND_ERROR); 21062712Snn35248 } 21072712Snn35248 21082712Snn35248 static int 21090Sstevel@tonic-gate verify_rctls(zone_dochandle_t handle) 21100Sstevel@tonic-gate { 21110Sstevel@tonic-gate struct zone_rctltab rctltab; 21120Sstevel@tonic-gate size_t rbs = rctlblk_size(); 21130Sstevel@tonic-gate rctlblk_t *rctlblk; 21140Sstevel@tonic-gate int error = Z_INVAL; 21150Sstevel@tonic-gate 21160Sstevel@tonic-gate if ((rctlblk = malloc(rbs)) == NULL) { 21170Sstevel@tonic-gate zerror(gettext("failed to allocate %lu bytes: %s"), rbs, 21180Sstevel@tonic-gate strerror(errno)); 21190Sstevel@tonic-gate return (Z_NOMEM); 21200Sstevel@tonic-gate } 21210Sstevel@tonic-gate 21220Sstevel@tonic-gate if (zonecfg_setrctlent(handle) != Z_OK) { 21230Sstevel@tonic-gate zerror(gettext("zonecfg_setrctlent failed")); 21240Sstevel@tonic-gate free(rctlblk); 21250Sstevel@tonic-gate return (error); 21260Sstevel@tonic-gate } 21270Sstevel@tonic-gate 21280Sstevel@tonic-gate rctltab.zone_rctl_valptr = NULL; 21290Sstevel@tonic-gate while (zonecfg_getrctlent(handle, &rctltab) == Z_OK) { 21300Sstevel@tonic-gate struct zone_rctlvaltab *rctlval; 21310Sstevel@tonic-gate const char *name = rctltab.zone_rctl_name; 21320Sstevel@tonic-gate 21330Sstevel@tonic-gate if (!zonecfg_is_rctl(name)) { 21340Sstevel@tonic-gate zerror(gettext("WARNING: Ignoring unrecognized rctl " 21350Sstevel@tonic-gate "'%s'."), name); 21360Sstevel@tonic-gate zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr); 21370Sstevel@tonic-gate rctltab.zone_rctl_valptr = NULL; 21380Sstevel@tonic-gate continue; 21390Sstevel@tonic-gate } 21400Sstevel@tonic-gate 21410Sstevel@tonic-gate for (rctlval = rctltab.zone_rctl_valptr; rctlval != NULL; 21420Sstevel@tonic-gate rctlval = rctlval->zone_rctlval_next) { 21430Sstevel@tonic-gate if (zonecfg_construct_rctlblk(rctlval, rctlblk) 21440Sstevel@tonic-gate != Z_OK) { 21450Sstevel@tonic-gate zerror(gettext("invalid rctl value: " 21460Sstevel@tonic-gate "(priv=%s,limit=%s,action%s)"), 21470Sstevel@tonic-gate rctlval->zone_rctlval_priv, 21480Sstevel@tonic-gate rctlval->zone_rctlval_limit, 21490Sstevel@tonic-gate rctlval->zone_rctlval_action); 21500Sstevel@tonic-gate goto out; 21510Sstevel@tonic-gate } 21520Sstevel@tonic-gate if (!zonecfg_valid_rctl(name, rctlblk)) { 21530Sstevel@tonic-gate zerror(gettext("(priv=%s,limit=%s,action=%s) " 21540Sstevel@tonic-gate "is not a valid value for rctl '%s'"), 21550Sstevel@tonic-gate rctlval->zone_rctlval_priv, 21560Sstevel@tonic-gate rctlval->zone_rctlval_limit, 21570Sstevel@tonic-gate rctlval->zone_rctlval_action, 21580Sstevel@tonic-gate name); 21590Sstevel@tonic-gate goto out; 21600Sstevel@tonic-gate } 21610Sstevel@tonic-gate } 21620Sstevel@tonic-gate zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr); 21630Sstevel@tonic-gate } 21640Sstevel@tonic-gate rctltab.zone_rctl_valptr = NULL; 21650Sstevel@tonic-gate error = Z_OK; 21660Sstevel@tonic-gate out: 21670Sstevel@tonic-gate zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr); 21680Sstevel@tonic-gate (void) zonecfg_endrctlent(handle); 21690Sstevel@tonic-gate free(rctlblk); 21700Sstevel@tonic-gate return (error); 21710Sstevel@tonic-gate } 21720Sstevel@tonic-gate 21730Sstevel@tonic-gate static int 21740Sstevel@tonic-gate verify_pool(zone_dochandle_t handle) 21750Sstevel@tonic-gate { 21760Sstevel@tonic-gate char poolname[MAXPATHLEN]; 21770Sstevel@tonic-gate pool_conf_t *poolconf; 21780Sstevel@tonic-gate pool_t *pool; 21790Sstevel@tonic-gate int status; 21800Sstevel@tonic-gate int error; 21810Sstevel@tonic-gate 21820Sstevel@tonic-gate /* 21830Sstevel@tonic-gate * This ends up being very similar to the check done in zoneadmd. 21840Sstevel@tonic-gate */ 21850Sstevel@tonic-gate error = zonecfg_get_pool(handle, poolname, sizeof (poolname)); 21860Sstevel@tonic-gate if (error == Z_NO_ENTRY || (error == Z_OK && strlen(poolname) == 0)) { 21870Sstevel@tonic-gate /* 21880Sstevel@tonic-gate * No pool specified. 21890Sstevel@tonic-gate */ 21900Sstevel@tonic-gate return (0); 21910Sstevel@tonic-gate } 21920Sstevel@tonic-gate if (error != Z_OK) { 21930Sstevel@tonic-gate zperror(gettext("Unable to retrieve pool name from " 21940Sstevel@tonic-gate "configuration"), B_TRUE); 21950Sstevel@tonic-gate return (error); 21960Sstevel@tonic-gate } 21970Sstevel@tonic-gate /* 21980Sstevel@tonic-gate * Don't do anything if pools aren't enabled. 21990Sstevel@tonic-gate */ 22000Sstevel@tonic-gate if (pool_get_status(&status) != PO_SUCCESS || status != POOL_ENABLED) { 22010Sstevel@tonic-gate zerror(gettext("WARNING: pools facility not active; " 22020Sstevel@tonic-gate "zone will not be bound to pool '%s'."), poolname); 22030Sstevel@tonic-gate return (Z_OK); 22040Sstevel@tonic-gate } 22050Sstevel@tonic-gate /* 22060Sstevel@tonic-gate * Try to provide a sane error message if the requested pool doesn't 22070Sstevel@tonic-gate * exist. It isn't clear that pools-related failures should 22080Sstevel@tonic-gate * necessarily translate to a failure to verify the zone configuration, 22090Sstevel@tonic-gate * hence they are not considered errors. 22100Sstevel@tonic-gate */ 22110Sstevel@tonic-gate if ((poolconf = pool_conf_alloc()) == NULL) { 22120Sstevel@tonic-gate zerror(gettext("WARNING: pool_conf_alloc failed; " 22130Sstevel@tonic-gate "using default pool")); 22140Sstevel@tonic-gate return (Z_OK); 22150Sstevel@tonic-gate } 22160Sstevel@tonic-gate if (pool_conf_open(poolconf, pool_dynamic_location(), PO_RDONLY) != 22170Sstevel@tonic-gate PO_SUCCESS) { 22180Sstevel@tonic-gate zerror(gettext("WARNING: pool_conf_open failed; " 22190Sstevel@tonic-gate "using default pool")); 22200Sstevel@tonic-gate pool_conf_free(poolconf); 22210Sstevel@tonic-gate return (Z_OK); 22220Sstevel@tonic-gate } 22230Sstevel@tonic-gate pool = pool_get_pool(poolconf, poolname); 22240Sstevel@tonic-gate (void) pool_conf_close(poolconf); 22250Sstevel@tonic-gate pool_conf_free(poolconf); 22260Sstevel@tonic-gate if (pool == NULL) { 22270Sstevel@tonic-gate zerror(gettext("WARNING: pool '%s' not found. " 22280Sstevel@tonic-gate "using default pool"), poolname); 22290Sstevel@tonic-gate } 22300Sstevel@tonic-gate 22310Sstevel@tonic-gate return (Z_OK); 22320Sstevel@tonic-gate } 22330Sstevel@tonic-gate 22340Sstevel@tonic-gate static int 2235823Sgjelinek verify_ipd(zone_dochandle_t handle) 2236823Sgjelinek { 2237823Sgjelinek int return_code = Z_OK; 2238823Sgjelinek struct zone_fstab fstab; 2239823Sgjelinek struct stat st; 2240823Sgjelinek char specdir[MAXPATHLEN]; 2241823Sgjelinek 2242823Sgjelinek if (zonecfg_setipdent(handle) != Z_OK) { 2243924Sgjelinek /* 2244924Sgjelinek * TRANSLATION_NOTE 2245924Sgjelinek * inherit-pkg-dirs is a literal that should not be translated. 2246924Sgjelinek */ 2247924Sgjelinek (void) fprintf(stderr, gettext("could not verify " 2248823Sgjelinek "inherit-pkg-dirs: unable to enumerate mounts\n")); 2249823Sgjelinek return (Z_ERR); 2250823Sgjelinek } 2251823Sgjelinek while (zonecfg_getipdent(handle, &fstab) == Z_OK) { 2252823Sgjelinek /* 2253823Sgjelinek * Verify fs_dir exists. 2254823Sgjelinek */ 2255823Sgjelinek (void) snprintf(specdir, sizeof (specdir), "%s%s", 2256823Sgjelinek zonecfg_get_root(), fstab.zone_fs_dir); 2257823Sgjelinek if (stat(specdir, &st) != 0) { 2258924Sgjelinek /* 2259924Sgjelinek * TRANSLATION_NOTE 2260924Sgjelinek * inherit-pkg-dir is a literal that should not be 2261924Sgjelinek * translated. 2262924Sgjelinek */ 2263924Sgjelinek (void) fprintf(stderr, gettext("could not verify " 2264823Sgjelinek "inherit-pkg-dir %s: %s\n"), 2265823Sgjelinek fstab.zone_fs_dir, strerror(errno)); 2266823Sgjelinek return_code = Z_ERR; 2267823Sgjelinek } 2268823Sgjelinek if (strcmp(st.st_fstype, MNTTYPE_NFS) == 0) { 2269924Sgjelinek /* 2270924Sgjelinek * TRANSLATION_NOTE 2271924Sgjelinek * inherit-pkg-dir and NFS are literals that should 2272924Sgjelinek * not be translated. 2273924Sgjelinek */ 2274823Sgjelinek (void) fprintf(stderr, gettext("cannot verify " 22751867Sgjelinek "inherit-pkg-dir %s: NFS mounted file system.\n" 22761867Sgjelinek "\tA local file system must be used.\n"), 2277823Sgjelinek fstab.zone_fs_dir); 2278823Sgjelinek return_code = Z_ERR; 2279823Sgjelinek } 2280823Sgjelinek } 2281823Sgjelinek (void) zonecfg_endipdent(handle); 2282823Sgjelinek 2283823Sgjelinek return (return_code); 2284823Sgjelinek } 2285823Sgjelinek 22861393Slling /* 22871867Sgjelinek * Verify that the special device/file system exists and is valid. 22881393Slling */ 22891393Slling static int 22901393Slling verify_fs_special(struct zone_fstab *fstab) 22911393Slling { 22921393Slling struct stat st; 22931393Slling 22942971Sgjelinek /* 22952971Sgjelinek * This validation is really intended for standard zone administration. 22962971Sgjelinek * If we are in a mini-root or some other upgrade situation where 22972971Sgjelinek * we are using the scratch zone, just by-pass this. 22982971Sgjelinek */ 22992971Sgjelinek if (zonecfg_in_alt_root()) 23002971Sgjelinek return (Z_OK); 23012971Sgjelinek 23021393Slling if (strcmp(fstab->zone_fs_type, MNTTYPE_ZFS) == 0) 23031393Slling return (verify_fs_zfs(fstab)); 23041393Slling 23051393Slling if (stat(fstab->zone_fs_special, &st) != 0) { 23061397Slling (void) fprintf(stderr, gettext("could not verify fs " 23071393Slling "%s: could not access %s: %s\n"), fstab->zone_fs_dir, 23081393Slling fstab->zone_fs_special, strerror(errno)); 23091393Slling return (Z_ERR); 23101393Slling } 23111393Slling 23121393Slling if (strcmp(st.st_fstype, MNTTYPE_NFS) == 0) { 23131393Slling /* 23141393Slling * TRANSLATION_NOTE 23151393Slling * fs and NFS are literals that should 23161393Slling * not be translated. 23171393Slling */ 23181393Slling (void) fprintf(stderr, gettext("cannot verify " 23191867Sgjelinek "fs %s: NFS mounted file system.\n" 23201867Sgjelinek "\tA local file system must be used.\n"), 23211393Slling fstab->zone_fs_special); 23221393Slling return (Z_ERR); 23231393Slling } 23241393Slling 23251393Slling return (Z_OK); 23261393Slling } 23271393Slling 2328823Sgjelinek static int 23290Sstevel@tonic-gate verify_filesystems(zone_dochandle_t handle) 23300Sstevel@tonic-gate { 23310Sstevel@tonic-gate int return_code = Z_OK; 23320Sstevel@tonic-gate struct zone_fstab fstab; 23330Sstevel@tonic-gate char cmdbuf[MAXPATHLEN]; 23340Sstevel@tonic-gate struct stat st; 23350Sstevel@tonic-gate 23360Sstevel@tonic-gate /* 23370Sstevel@tonic-gate * No need to verify inherit-pkg-dir fs types, as their type is 23380Sstevel@tonic-gate * implicitly lofs, which is known. Therefore, the types are only 23391867Sgjelinek * verified for regular file systems below. 23400Sstevel@tonic-gate * 23410Sstevel@tonic-gate * Since the actual mount point is not known until the dependent mounts 23420Sstevel@tonic-gate * are performed, we don't attempt any path validation here: that will 23430Sstevel@tonic-gate * happen later when zoneadmd actually does the mounts. 23440Sstevel@tonic-gate */ 23450Sstevel@tonic-gate if (zonecfg_setfsent(handle) != Z_OK) { 23461867Sgjelinek (void) fprintf(stderr, gettext("could not verify file systems: " 23470Sstevel@tonic-gate "unable to enumerate mounts\n")); 23480Sstevel@tonic-gate return (Z_ERR); 23490Sstevel@tonic-gate } 23500Sstevel@tonic-gate while (zonecfg_getfsent(handle, &fstab) == Z_OK) { 23510Sstevel@tonic-gate if (!zonecfg_valid_fs_type(fstab.zone_fs_type)) { 23520Sstevel@tonic-gate (void) fprintf(stderr, gettext("cannot verify fs %s: " 23530Sstevel@tonic-gate "type %s is not allowed.\n"), fstab.zone_fs_dir, 23540Sstevel@tonic-gate fstab.zone_fs_type); 23550Sstevel@tonic-gate return_code = Z_ERR; 23560Sstevel@tonic-gate goto next_fs; 23570Sstevel@tonic-gate } 23580Sstevel@tonic-gate /* 23590Sstevel@tonic-gate * Verify /usr/lib/fs/<fstype>/mount exists. 23600Sstevel@tonic-gate */ 23610Sstevel@tonic-gate if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/mount", 23620Sstevel@tonic-gate fstab.zone_fs_type) > sizeof (cmdbuf)) { 23630Sstevel@tonic-gate (void) fprintf(stderr, gettext("cannot verify fs %s: " 23640Sstevel@tonic-gate "type %s is too long.\n"), fstab.zone_fs_dir, 23650Sstevel@tonic-gate fstab.zone_fs_type); 23660Sstevel@tonic-gate return_code = Z_ERR; 23670Sstevel@tonic-gate goto next_fs; 23680Sstevel@tonic-gate } 23690Sstevel@tonic-gate if (stat(cmdbuf, &st) != 0) { 2370924Sgjelinek (void) fprintf(stderr, gettext("could not verify fs " 2371924Sgjelinek "%s: could not access %s: %s\n"), fstab.zone_fs_dir, 23720Sstevel@tonic-gate cmdbuf, strerror(errno)); 23730Sstevel@tonic-gate return_code = Z_ERR; 23740Sstevel@tonic-gate goto next_fs; 23750Sstevel@tonic-gate } 23760Sstevel@tonic-gate if (!S_ISREG(st.st_mode)) { 2377924Sgjelinek (void) fprintf(stderr, gettext("could not verify fs " 2378924Sgjelinek "%s: %s is not a regular file\n"), 2379924Sgjelinek fstab.zone_fs_dir, cmdbuf); 23800Sstevel@tonic-gate return_code = Z_ERR; 23810Sstevel@tonic-gate goto next_fs; 23820Sstevel@tonic-gate } 23830Sstevel@tonic-gate /* 23840Sstevel@tonic-gate * Verify /usr/lib/fs/<fstype>/fsck exists iff zone_fs_raw is 23850Sstevel@tonic-gate * set. 23860Sstevel@tonic-gate */ 23870Sstevel@tonic-gate if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/fsck", 23880Sstevel@tonic-gate fstab.zone_fs_type) > sizeof (cmdbuf)) { 23890Sstevel@tonic-gate (void) fprintf(stderr, gettext("cannot verify fs %s: " 23900Sstevel@tonic-gate "type %s is too long.\n"), fstab.zone_fs_dir, 23910Sstevel@tonic-gate fstab.zone_fs_type); 23920Sstevel@tonic-gate return_code = Z_ERR; 23930Sstevel@tonic-gate goto next_fs; 23940Sstevel@tonic-gate } 23950Sstevel@tonic-gate if (fstab.zone_fs_raw[0] == '\0' && stat(cmdbuf, &st) == 0) { 2396924Sgjelinek (void) fprintf(stderr, gettext("could not verify fs " 2397924Sgjelinek "%s: must specify 'raw' device for %s " 23981867Sgjelinek "file systems\n"), 23990Sstevel@tonic-gate fstab.zone_fs_dir, fstab.zone_fs_type); 24000Sstevel@tonic-gate return_code = Z_ERR; 24010Sstevel@tonic-gate goto next_fs; 24020Sstevel@tonic-gate } 24030Sstevel@tonic-gate if (fstab.zone_fs_raw[0] != '\0' && 24040Sstevel@tonic-gate (stat(cmdbuf, &st) != 0 || !S_ISREG(st.st_mode))) { 24050Sstevel@tonic-gate (void) fprintf(stderr, gettext("cannot verify fs %s: " 24060Sstevel@tonic-gate "'raw' device specified but " 24070Sstevel@tonic-gate "no fsck executable exists for %s\n"), 24080Sstevel@tonic-gate fstab.zone_fs_dir, fstab.zone_fs_type); 24090Sstevel@tonic-gate return_code = Z_ERR; 24100Sstevel@tonic-gate goto next_fs; 24110Sstevel@tonic-gate } 24121393Slling 24131393Slling /* Verify fs_special. */ 24141393Slling if ((return_code = verify_fs_special(&fstab)) != Z_OK) 2415823Sgjelinek goto next_fs; 24161393Slling 24171393Slling /* Verify fs_raw. */ 2418823Sgjelinek if (fstab.zone_fs_raw[0] != '\0' && 2419823Sgjelinek stat(fstab.zone_fs_raw, &st) != 0) { 2420924Sgjelinek /* 2421924Sgjelinek * TRANSLATION_NOTE 2422924Sgjelinek * fs is a literal that should not be translated. 2423924Sgjelinek */ 2424924Sgjelinek (void) fprintf(stderr, gettext("could not verify fs " 2425924Sgjelinek "%s: could not access %s: %s\n"), fstab.zone_fs_dir, 2426823Sgjelinek fstab.zone_fs_raw, strerror(errno)); 2427823Sgjelinek return_code = Z_ERR; 2428823Sgjelinek goto next_fs; 2429823Sgjelinek } 24300Sstevel@tonic-gate next_fs: 24310Sstevel@tonic-gate zonecfg_free_fs_option_list(fstab.zone_fs_options); 24320Sstevel@tonic-gate } 24330Sstevel@tonic-gate (void) zonecfg_endfsent(handle); 24340Sstevel@tonic-gate 24350Sstevel@tonic-gate return (return_code); 24360Sstevel@tonic-gate } 24370Sstevel@tonic-gate 24380Sstevel@tonic-gate static int 24391645Scomay verify_limitpriv(zone_dochandle_t handle) 24401645Scomay { 24411645Scomay char *privname = NULL; 24421645Scomay int err; 24431645Scomay priv_set_t *privs; 24441645Scomay 24451645Scomay if ((privs = priv_allocset()) == NULL) { 24461645Scomay zperror(gettext("failed to allocate privilege set"), B_FALSE); 24471645Scomay return (Z_NOMEM); 24481645Scomay } 24491645Scomay err = zonecfg_get_privset(handle, privs, &privname); 24501645Scomay switch (err) { 24511645Scomay case Z_OK: 24521645Scomay break; 24531645Scomay case Z_PRIV_PROHIBITED: 24541645Scomay (void) fprintf(stderr, gettext("privilege \"%s\" is not " 24551645Scomay "permitted within the zone's privilege set\n"), privname); 24561645Scomay break; 24571645Scomay case Z_PRIV_REQUIRED: 24581645Scomay (void) fprintf(stderr, gettext("required privilege \"%s\" is " 24591645Scomay "missing from the zone's privilege set\n"), privname); 24601645Scomay break; 24611645Scomay case Z_PRIV_UNKNOWN: 24621645Scomay (void) fprintf(stderr, gettext("unknown privilege \"%s\" " 24631645Scomay "specified in the zone's privilege set\n"), privname); 24641645Scomay break; 24651645Scomay default: 24661645Scomay zperror( 24671645Scomay gettext("failed to determine the zone's privilege set"), 24681645Scomay B_TRUE); 24691645Scomay break; 24701645Scomay } 24711645Scomay free(privname); 24721645Scomay priv_freeset(privs); 24731645Scomay return (err); 24741645Scomay } 24751645Scomay 24761915Sgjelinek static void 24771915Sgjelinek free_local_netifs(int if_cnt, struct net_if **if_list) 24781915Sgjelinek { 24791915Sgjelinek int i; 24801915Sgjelinek 24811915Sgjelinek for (i = 0; i < if_cnt; i++) { 24821915Sgjelinek free(if_list[i]->name); 24831915Sgjelinek free(if_list[i]); 24841915Sgjelinek } 24851915Sgjelinek free(if_list); 24861915Sgjelinek } 24871915Sgjelinek 24881915Sgjelinek /* 24891915Sgjelinek * Get a list of the network interfaces, along with their address families, 24901915Sgjelinek * that are plumbed in the global zone. See if_tcp(7p) for a description 24911915Sgjelinek * of the ioctls used here. 24921915Sgjelinek */ 24931915Sgjelinek static int 24941915Sgjelinek get_local_netifs(int *if_cnt, struct net_if ***if_list) 24951915Sgjelinek { 24961915Sgjelinek int s; 24971915Sgjelinek int i; 24981915Sgjelinek int res = Z_OK; 24991915Sgjelinek int space_needed; 25001915Sgjelinek int cnt = 0; 25011915Sgjelinek struct lifnum if_num; 25021915Sgjelinek struct lifconf if_conf; 25031915Sgjelinek struct lifreq *if_reqp; 25041915Sgjelinek char *if_buf; 25051915Sgjelinek struct net_if **local_ifs = NULL; 25061915Sgjelinek 25071915Sgjelinek *if_cnt = 0; 25081915Sgjelinek *if_list = NULL; 25091915Sgjelinek 25101915Sgjelinek if ((s = socket(SOCKET_AF(AF_INET), SOCK_DGRAM, 0)) < 0) 25111915Sgjelinek return (Z_ERR); 25121915Sgjelinek 25131915Sgjelinek /* 25141915Sgjelinek * Come back here in the unlikely event that the number of interfaces 25151915Sgjelinek * increases between the time we get the count and the time we do the 25161915Sgjelinek * SIOCGLIFCONF ioctl. 25171915Sgjelinek */ 25181915Sgjelinek retry: 25191915Sgjelinek /* Get the number of interfaces. */ 25201915Sgjelinek if_num.lifn_family = AF_UNSPEC; 25211915Sgjelinek if_num.lifn_flags = LIFC_NOXMIT; 25221915Sgjelinek if (ioctl(s, SIOCGLIFNUM, &if_num) < 0) { 25231915Sgjelinek (void) close(s); 25241915Sgjelinek return (Z_ERR); 25251915Sgjelinek } 25261915Sgjelinek 25271915Sgjelinek /* Get the interface configuration list. */ 25281915Sgjelinek space_needed = if_num.lifn_count * sizeof (struct lifreq); 25291915Sgjelinek if ((if_buf = malloc(space_needed)) == NULL) { 25301915Sgjelinek (void) close(s); 25311915Sgjelinek return (Z_ERR); 25321915Sgjelinek } 25331915Sgjelinek if_conf.lifc_family = AF_UNSPEC; 25341915Sgjelinek if_conf.lifc_flags = LIFC_NOXMIT; 25351915Sgjelinek if_conf.lifc_len = space_needed; 25361915Sgjelinek if_conf.lifc_buf = if_buf; 25371915Sgjelinek if (ioctl(s, SIOCGLIFCONF, &if_conf) < 0) { 25381915Sgjelinek free(if_buf); 25391915Sgjelinek /* 25401915Sgjelinek * SIOCGLIFCONF returns EINVAL if the buffer we passed in is 25411915Sgjelinek * too small. In this case go back and get the new if cnt. 25421915Sgjelinek */ 25431915Sgjelinek if (errno == EINVAL) 25441915Sgjelinek goto retry; 25451915Sgjelinek 25461915Sgjelinek (void) close(s); 25471915Sgjelinek return (Z_ERR); 25481915Sgjelinek } 25491915Sgjelinek (void) close(s); 25501915Sgjelinek 25511915Sgjelinek /* Get the name and address family for each interface. */ 25521915Sgjelinek if_reqp = if_conf.lifc_req; 25531915Sgjelinek for (i = 0; i < (if_conf.lifc_len / sizeof (struct lifreq)); i++) { 25541915Sgjelinek struct net_if **p; 25551915Sgjelinek struct lifreq req; 25561915Sgjelinek 25571915Sgjelinek if (strcmp(LOOPBACK_IF, if_reqp->lifr_name) == 0) { 25581915Sgjelinek if_reqp++; 25591915Sgjelinek continue; 25601915Sgjelinek } 25611915Sgjelinek 25621915Sgjelinek if ((s = socket(SOCKET_AF(if_reqp->lifr_addr.ss_family), 25631915Sgjelinek SOCK_DGRAM, 0)) == -1) { 25641915Sgjelinek res = Z_ERR; 25651915Sgjelinek break; 25661915Sgjelinek } 25671915Sgjelinek 25681915Sgjelinek (void) strncpy(req.lifr_name, if_reqp->lifr_name, 25691915Sgjelinek sizeof (req.lifr_name)); 25701915Sgjelinek if (ioctl(s, SIOCGLIFADDR, &req) < 0) { 25711915Sgjelinek (void) close(s); 25721915Sgjelinek if_reqp++; 25731915Sgjelinek continue; 25741915Sgjelinek } 25751915Sgjelinek 25761915Sgjelinek if ((p = (struct net_if **)realloc(local_ifs, 25771915Sgjelinek sizeof (struct net_if *) * (cnt + 1))) == NULL) { 25781915Sgjelinek res = Z_ERR; 25791915Sgjelinek break; 25801915Sgjelinek } 25811915Sgjelinek local_ifs = p; 25821915Sgjelinek 25831915Sgjelinek if ((local_ifs[cnt] = malloc(sizeof (struct net_if))) == NULL) { 25841915Sgjelinek res = Z_ERR; 25851915Sgjelinek break; 25861915Sgjelinek } 25871915Sgjelinek 25881915Sgjelinek if ((local_ifs[cnt]->name = strdup(if_reqp->lifr_name)) 25891915Sgjelinek == NULL) { 25901915Sgjelinek free(local_ifs[cnt]); 25911915Sgjelinek res = Z_ERR; 25921915Sgjelinek break; 25931915Sgjelinek } 25941915Sgjelinek local_ifs[cnt]->af = req.lifr_addr.ss_family; 25951915Sgjelinek cnt++; 25961915Sgjelinek 25971915Sgjelinek (void) close(s); 25981915Sgjelinek if_reqp++; 25991915Sgjelinek } 26001915Sgjelinek 26011915Sgjelinek free(if_buf); 26021915Sgjelinek 26031915Sgjelinek if (res != Z_OK) { 26041915Sgjelinek free_local_netifs(cnt, local_ifs); 26051915Sgjelinek } else { 26061915Sgjelinek *if_cnt = cnt; 26071915Sgjelinek *if_list = local_ifs; 26081915Sgjelinek } 26091915Sgjelinek 26101915Sgjelinek return (res); 26111915Sgjelinek } 26121915Sgjelinek 26131915Sgjelinek static char * 26141915Sgjelinek af2str(int af) 26151915Sgjelinek { 26161915Sgjelinek switch (af) { 26171915Sgjelinek case AF_INET: 26181915Sgjelinek return ("IPv4"); 26191915Sgjelinek case AF_INET6: 26201915Sgjelinek return ("IPv6"); 26211915Sgjelinek default: 26221915Sgjelinek return ("Unknown"); 26231915Sgjelinek } 26241915Sgjelinek } 26251915Sgjelinek 26261915Sgjelinek /* 26271915Sgjelinek * Cross check the network interface name and address family with the 26281915Sgjelinek * interfaces that are set up in the global zone so that we can print the 26291915Sgjelinek * appropriate error message. 26301915Sgjelinek */ 26311915Sgjelinek static void 26321915Sgjelinek print_net_err(char *phys, char *addr, int af, char *msg) 26331915Sgjelinek { 26341915Sgjelinek int i; 26351915Sgjelinek int local_if_cnt = 0; 26361915Sgjelinek struct net_if **local_ifs = NULL; 26371915Sgjelinek boolean_t found_if = B_FALSE; 26381915Sgjelinek boolean_t found_af = B_FALSE; 26391915Sgjelinek 26401915Sgjelinek if (get_local_netifs(&local_if_cnt, &local_ifs) != Z_OK) { 26411915Sgjelinek (void) fprintf(stderr, 26421915Sgjelinek gettext("could not verify %s %s=%s %s=%s\n\t%s\n"), 26431915Sgjelinek "net", "address", addr, "physical", phys, msg); 26441915Sgjelinek return; 26451915Sgjelinek } 26461915Sgjelinek 26471915Sgjelinek for (i = 0; i < local_if_cnt; i++) { 26481915Sgjelinek if (strcmp(phys, local_ifs[i]->name) == 0) { 26491915Sgjelinek found_if = B_TRUE; 26501915Sgjelinek if (af == local_ifs[i]->af) { 26511915Sgjelinek found_af = B_TRUE; 26521915Sgjelinek break; 26531915Sgjelinek } 26541915Sgjelinek } 26551915Sgjelinek } 26561915Sgjelinek 26571915Sgjelinek free_local_netifs(local_if_cnt, local_ifs); 26581915Sgjelinek 26591915Sgjelinek if (!found_if) { 26601915Sgjelinek (void) fprintf(stderr, 26611915Sgjelinek gettext("could not verify %s %s=%s\n\t" 26621915Sgjelinek "network interface %s is not plumbed in the global zone\n"), 26631915Sgjelinek "net", "physical", phys, phys); 26641915Sgjelinek return; 26651915Sgjelinek } 26661915Sgjelinek 26671915Sgjelinek /* 26681915Sgjelinek * Print this error if we were unable to find the address family 26691915Sgjelinek * for this interface. If the af variable is not initialized to 26701915Sgjelinek * to something meaningful by the caller (not AF_UNSPEC) then we 26711915Sgjelinek * also skip this message since it wouldn't be informative. 26721915Sgjelinek */ 26731915Sgjelinek if (!found_af && af != AF_UNSPEC) { 26741915Sgjelinek (void) fprintf(stderr, 26751915Sgjelinek gettext("could not verify %s %s=%s %s=%s\n\tthe %s address " 26761915Sgjelinek "family is not configured on this interface in the\n\t" 26771915Sgjelinek "global zone\n"), 26781915Sgjelinek "net", "address", addr, "physical", phys, af2str(af)); 26791915Sgjelinek return; 26801915Sgjelinek } 26811915Sgjelinek 26821915Sgjelinek (void) fprintf(stderr, 26831915Sgjelinek gettext("could not verify %s %s=%s %s=%s\n\t%s\n"), 26841915Sgjelinek "net", "address", addr, "physical", phys, msg); 26851915Sgjelinek } 26861915Sgjelinek 26871645Scomay static int 26882078Sgjelinek verify_handle(int cmd_num, zone_dochandle_t handle) 26890Sstevel@tonic-gate { 26900Sstevel@tonic-gate struct zone_nwiftab nwiftab; 26910Sstevel@tonic-gate int return_code = Z_OK; 26920Sstevel@tonic-gate int err; 2693766Scarlsonj boolean_t in_alt_root; 26940Sstevel@tonic-gate 2695766Scarlsonj in_alt_root = zonecfg_in_alt_root(); 2696766Scarlsonj if (in_alt_root) 2697766Scarlsonj goto no_net; 2698766Scarlsonj 26990Sstevel@tonic-gate if ((err = zonecfg_setnwifent(handle)) != Z_OK) { 27000Sstevel@tonic-gate errno = err; 27010Sstevel@tonic-gate zperror(cmd_to_str(cmd_num), B_TRUE); 27020Sstevel@tonic-gate zonecfg_fini_handle(handle); 27030Sstevel@tonic-gate return (Z_ERR); 27040Sstevel@tonic-gate } 27050Sstevel@tonic-gate while (zonecfg_getnwifent(handle, &nwiftab) == Z_OK) { 27060Sstevel@tonic-gate struct lifreq lifr; 27071915Sgjelinek sa_family_t af = AF_UNSPEC; 27080Sstevel@tonic-gate int so, res; 27090Sstevel@tonic-gate 27100Sstevel@tonic-gate /* skip any loopback interfaces */ 27110Sstevel@tonic-gate if (strcmp(nwiftab.zone_nwif_physical, "lo0") == 0) 27120Sstevel@tonic-gate continue; 27130Sstevel@tonic-gate if ((res = zonecfg_valid_net_address(nwiftab.zone_nwif_address, 27140Sstevel@tonic-gate &lifr)) != Z_OK) { 27151915Sgjelinek print_net_err(nwiftab.zone_nwif_physical, 27161915Sgjelinek nwiftab.zone_nwif_address, af, 27171915Sgjelinek zonecfg_strerror(res)); 27180Sstevel@tonic-gate return_code = Z_ERR; 27190Sstevel@tonic-gate continue; 27200Sstevel@tonic-gate } 27210Sstevel@tonic-gate af = lifr.lifr_addr.ss_family; 27220Sstevel@tonic-gate (void) memset(&lifr, 0, sizeof (lifr)); 27230Sstevel@tonic-gate (void) strlcpy(lifr.lifr_name, nwiftab.zone_nwif_physical, 27240Sstevel@tonic-gate sizeof (lifr.lifr_name)); 27250Sstevel@tonic-gate lifr.lifr_addr.ss_family = af; 27260Sstevel@tonic-gate if ((so = socket(af, SOCK_DGRAM, 0)) < 0) { 27270Sstevel@tonic-gate (void) fprintf(stderr, gettext("could not verify %s " 27280Sstevel@tonic-gate "%s=%s %s=%s: could not get socket: %s\n"), "net", 27290Sstevel@tonic-gate "address", nwiftab.zone_nwif_address, "physical", 27300Sstevel@tonic-gate nwiftab.zone_nwif_physical, strerror(errno)); 27310Sstevel@tonic-gate return_code = Z_ERR; 27320Sstevel@tonic-gate continue; 27330Sstevel@tonic-gate } 27341915Sgjelinek if (ioctl(so, SIOCGLIFFLAGS, &lifr) < 0) { 27352611Svp157776 /* 27362611Svp157776 * The interface failed to come up. We continue on 27372611Svp157776 * anyway for the sake of consistency: a zone is not 27382611Svp157776 * shut down if the interface fails any time after 27392611Svp157776 * boot, nor does the global zone fail to boot if an 27402611Svp157776 * interface fails. 27412611Svp157776 */ 27422611Svp157776 (void) fprintf(stderr, 27432611Svp157776 gettext("WARNING: skipping interface '%s' which " 27442611Svp157776 "may not be present/plumbed in the global zone.\n"), 27452611Svp157776 nwiftab.zone_nwif_physical); 27462611Svp157776 27470Sstevel@tonic-gate } 27480Sstevel@tonic-gate (void) close(so); 27490Sstevel@tonic-gate } 27500Sstevel@tonic-gate (void) zonecfg_endnwifent(handle); 2751766Scarlsonj no_net: 27520Sstevel@tonic-gate 27531931Sgjelinek /* verify that lofs has not been excluded from the kernel */ 27542078Sgjelinek if (!(cmd_num == CMD_DETACH || cmd_num == CMD_ATTACH || 27552078Sgjelinek cmd_num == CMD_MOVE || cmd_num == CMD_CLONE) && 27562078Sgjelinek modctl(MODLOAD, 1, "fs/lofs", NULL) != 0) { 27571931Sgjelinek if (errno == ENXIO) 27581931Sgjelinek (void) fprintf(stderr, gettext("could not verify " 27591931Sgjelinek "lofs(7FS): possibly excluded in /etc/system\n")); 27601931Sgjelinek else 27611931Sgjelinek (void) fprintf(stderr, gettext("could not verify " 27621931Sgjelinek "lofs(7FS): %s\n"), strerror(errno)); 27631931Sgjelinek return_code = Z_ERR; 27641931Sgjelinek } 27651931Sgjelinek 27660Sstevel@tonic-gate if (verify_filesystems(handle) != Z_OK) 27670Sstevel@tonic-gate return_code = Z_ERR; 2768823Sgjelinek if (verify_ipd(handle) != Z_OK) 2769823Sgjelinek return_code = Z_ERR; 2770766Scarlsonj if (!in_alt_root && verify_rctls(handle) != Z_OK) 27710Sstevel@tonic-gate return_code = Z_ERR; 2772766Scarlsonj if (!in_alt_root && verify_pool(handle) != Z_OK) 27730Sstevel@tonic-gate return_code = Z_ERR; 27742712Snn35248 if (!in_alt_root && verify_brand(handle) != Z_OK) 27752712Snn35248 return_code = Z_ERR; 2776789Sahrens if (!in_alt_root && verify_datasets(handle) != Z_OK) 2777789Sahrens return_code = Z_ERR; 27781645Scomay 27791645Scomay /* 27801645Scomay * As the "mount" command is used for patching/upgrading of zones 27811645Scomay * or other maintenance processes, the zone's privilege set is not 27821645Scomay * checked in this case. Instead, the default, safe set of 27831645Scomay * privileges will be used when this zone is created in the 27841645Scomay * kernel. 27851645Scomay */ 27861645Scomay if (!in_alt_root && cmd_num != CMD_MOUNT && 27871645Scomay verify_limitpriv(handle) != Z_OK) 27881645Scomay return_code = Z_ERR; 27892078Sgjelinek 27902078Sgjelinek return (return_code); 27912078Sgjelinek } 27922078Sgjelinek 27932078Sgjelinek static int 27942078Sgjelinek verify_details(int cmd_num) 27952078Sgjelinek { 27962078Sgjelinek zone_dochandle_t handle; 27972078Sgjelinek char zonepath[MAXPATHLEN], checkpath[MAXPATHLEN]; 27982078Sgjelinek int return_code = Z_OK; 27992078Sgjelinek int err; 28002078Sgjelinek 28012078Sgjelinek if ((handle = zonecfg_init_handle()) == NULL) { 28022078Sgjelinek zperror(cmd_to_str(cmd_num), B_TRUE); 28032078Sgjelinek return (Z_ERR); 28042078Sgjelinek } 28052078Sgjelinek if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) { 28062078Sgjelinek errno = err; 28072078Sgjelinek zperror(cmd_to_str(cmd_num), B_TRUE); 28082078Sgjelinek zonecfg_fini_handle(handle); 28092078Sgjelinek return (Z_ERR); 28102078Sgjelinek } 28112078Sgjelinek if ((err = zonecfg_get_zonepath(handle, zonepath, sizeof (zonepath))) != 28122078Sgjelinek Z_OK) { 28132078Sgjelinek errno = err; 28142078Sgjelinek zperror(cmd_to_str(cmd_num), B_TRUE); 28152078Sgjelinek zonecfg_fini_handle(handle); 28162078Sgjelinek return (Z_ERR); 28172078Sgjelinek } 28182078Sgjelinek /* 28192078Sgjelinek * zonecfg_get_zonepath() gets its data from the XML repository. 28202078Sgjelinek * Verify this against the index file, which is checked first by 28212078Sgjelinek * zone_get_zonepath(). If they don't match, bail out. 28222078Sgjelinek */ 28232078Sgjelinek if ((err = zone_get_zonepath(target_zone, checkpath, 28242078Sgjelinek sizeof (checkpath))) != Z_OK) { 28252078Sgjelinek errno = err; 28262078Sgjelinek zperror2(target_zone, gettext("could not get zone path")); 28272078Sgjelinek return (Z_ERR); 28282078Sgjelinek } 28292078Sgjelinek if (strcmp(zonepath, checkpath) != 0) { 28302078Sgjelinek /* 28312078Sgjelinek * TRANSLATION_NOTE 28322078Sgjelinek * XML and zonepath are literals that should not be translated. 28332078Sgjelinek */ 28342078Sgjelinek (void) fprintf(stderr, gettext("The XML repository has " 28352078Sgjelinek "zonepath '%s',\nbut the index file has zonepath '%s'.\n" 28362078Sgjelinek "These must match, so fix the incorrect entry.\n"), 28372078Sgjelinek zonepath, checkpath); 28382078Sgjelinek return (Z_ERR); 28392078Sgjelinek } 28402078Sgjelinek if (validate_zonepath(zonepath, cmd_num) != Z_OK) { 28412078Sgjelinek (void) fprintf(stderr, gettext("could not verify zonepath %s " 28422078Sgjelinek "because of the above errors.\n"), zonepath); 28432078Sgjelinek return_code = Z_ERR; 28442078Sgjelinek } 28452078Sgjelinek 28462078Sgjelinek if (verify_handle(cmd_num, handle) != Z_OK) 28472078Sgjelinek return_code = Z_ERR; 28482078Sgjelinek 28490Sstevel@tonic-gate zonecfg_fini_handle(handle); 28500Sstevel@tonic-gate if (return_code == Z_ERR) 28510Sstevel@tonic-gate (void) fprintf(stderr, 28520Sstevel@tonic-gate gettext("%s: zone %s failed to verify\n"), 28530Sstevel@tonic-gate execname, target_zone); 28540Sstevel@tonic-gate return (return_code); 28550Sstevel@tonic-gate } 28560Sstevel@tonic-gate 28570Sstevel@tonic-gate static int 28580Sstevel@tonic-gate verify_func(int argc, char *argv[]) 28590Sstevel@tonic-gate { 28600Sstevel@tonic-gate int arg; 28610Sstevel@tonic-gate 28620Sstevel@tonic-gate optind = 0; 28630Sstevel@tonic-gate if ((arg = getopt(argc, argv, "?")) != EOF) { 28640Sstevel@tonic-gate switch (arg) { 28650Sstevel@tonic-gate case '?': 28660Sstevel@tonic-gate sub_usage(SHELP_VERIFY, CMD_VERIFY); 28670Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 28680Sstevel@tonic-gate default: 28690Sstevel@tonic-gate sub_usage(SHELP_VERIFY, CMD_VERIFY); 28700Sstevel@tonic-gate return (Z_USAGE); 28710Sstevel@tonic-gate } 28720Sstevel@tonic-gate } 28730Sstevel@tonic-gate if (argc > optind) { 28740Sstevel@tonic-gate sub_usage(SHELP_VERIFY, CMD_VERIFY); 28750Sstevel@tonic-gate return (Z_USAGE); 28760Sstevel@tonic-gate } 28772712Snn35248 if (sanity_check(target_zone, CMD_VERIFY, B_FALSE, B_FALSE, B_FALSE) 28782712Snn35248 != Z_OK) 28790Sstevel@tonic-gate return (Z_ERR); 28800Sstevel@tonic-gate return (verify_details(CMD_VERIFY)); 28810Sstevel@tonic-gate } 28820Sstevel@tonic-gate 28832712Snn35248 static int 28842712Snn35248 addopt(char *buf, int opt, char *optarg, size_t bufsize) 28852712Snn35248 { 28862712Snn35248 char optstring[4]; 28872712Snn35248 28882712Snn35248 if (opt > 0) 28892712Snn35248 (void) sprintf(optstring, " -%c", opt); 28902712Snn35248 else 28912712Snn35248 (void) strcpy(optstring, " "); 28922712Snn35248 28932712Snn35248 if ((strlcat(buf, optstring, bufsize) > bufsize)) 28942712Snn35248 return (Z_ERR); 28952712Snn35248 if ((optarg != NULL) && (strlcat(buf, optarg, bufsize) > bufsize)) 28962712Snn35248 return (Z_ERR); 28972712Snn35248 return (Z_OK); 28982712Snn35248 } 28990Sstevel@tonic-gate 29000Sstevel@tonic-gate static int 29010Sstevel@tonic-gate install_func(int argc, char *argv[]) 29020Sstevel@tonic-gate { 29032712Snn35248 char cmdbuf[MAXPATHLEN]; 29040Sstevel@tonic-gate int lockfd; 29052712Snn35248 int arg, err, subproc_err; 29060Sstevel@tonic-gate char zonepath[MAXPATHLEN]; 29072727Sedp brand_handle_t bh = NULL; 29080Sstevel@tonic-gate int status; 29091867Sgjelinek boolean_t nodataset = B_FALSE; 29102712Snn35248 char opts[128]; 29112712Snn35248 29122712Snn35248 if (target_zone == NULL) { 29132712Snn35248 sub_usage(SHELP_INSTALL, CMD_INSTALL); 29142712Snn35248 return (Z_USAGE); 29152712Snn35248 } 29160Sstevel@tonic-gate 2917766Scarlsonj if (zonecfg_in_alt_root()) { 2918766Scarlsonj zerror(gettext("cannot install zone in alternate root")); 2919766Scarlsonj return (Z_ERR); 2920766Scarlsonj } 2921766Scarlsonj 29222712Snn35248 if ((err = zone_get_zonepath(target_zone, zonepath, 29232712Snn35248 sizeof (zonepath))) != Z_OK) { 29242712Snn35248 errno = err; 29252712Snn35248 zperror2(target_zone, gettext("could not get zone path")); 29262712Snn35248 return (Z_ERR); 29272712Snn35248 } 29282712Snn35248 29292712Snn35248 /* Fetch the install command from the brand configuration. */ 29302727Sedp if ((bh = brand_open(target_brand)) == NULL) { 29312712Snn35248 zerror(gettext("missing or invalid brand")); 29322712Snn35248 return (Z_ERR); 29332712Snn35248 } 29342712Snn35248 29352712Snn35248 (void) strcpy(cmdbuf, EXEC_PREFIX); 29362727Sedp if (brand_get_install(bh, target_zone, zonepath, cmdbuf + EXEC_LEN, 29372712Snn35248 sizeof (cmdbuf) - EXEC_LEN, 0, NULL) != 0) { 29382712Snn35248 zerror("invalid brand configuration: missing install resource"); 29392727Sedp brand_close(bh); 29402712Snn35248 return (Z_ERR); 29412712Snn35248 } 29422712Snn35248 29432712Snn35248 (void) strcpy(opts, "?x:"); 29442712Snn35248 if (!is_native_zone) { 29452712Snn35248 /* 29462712Snn35248 * Fetch the list of recognized command-line options from 29472712Snn35248 * the brand configuration file. 29482712Snn35248 */ 29492727Sedp if (brand_get_installopts(bh, opts + strlen(opts), 29502712Snn35248 sizeof (opts) - strlen(opts)) != 0) { 29512712Snn35248 zerror("invalid brand configuration: missing " 29522712Snn35248 "install options resource"); 29532727Sedp brand_close(bh); 29542712Snn35248 return (Z_ERR); 29552712Snn35248 } 29562712Snn35248 } 29572727Sedp brand_close(bh); 29582712Snn35248 29590Sstevel@tonic-gate optind = 0; 29602712Snn35248 while ((arg = getopt(argc, argv, opts)) != EOF) { 29610Sstevel@tonic-gate switch (arg) { 29620Sstevel@tonic-gate case '?': 29630Sstevel@tonic-gate sub_usage(SHELP_INSTALL, CMD_INSTALL); 29640Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 29651867Sgjelinek case 'x': 29661867Sgjelinek if (strcmp(optarg, "nodataset") != 0) { 29671867Sgjelinek sub_usage(SHELP_INSTALL, CMD_INSTALL); 29681867Sgjelinek return (Z_USAGE); 29691867Sgjelinek } 29701867Sgjelinek nodataset = B_TRUE; 29711867Sgjelinek break; 29720Sstevel@tonic-gate default: 29732712Snn35248 if (is_native_zone) { 29742712Snn35248 sub_usage(SHELP_INSTALL, CMD_INSTALL); 29752712Snn35248 return (Z_USAGE); 29762712Snn35248 } 29772712Snn35248 29782712Snn35248 /* 29792712Snn35248 * This option isn't for zoneadm, so append it to 29802712Snn35248 * the command line passed to the brand-specific 29812712Snn35248 * install routine. 29822712Snn35248 */ 29832712Snn35248 if (addopt(cmdbuf, optopt, optarg, 29842712Snn35248 sizeof (cmdbuf)) != Z_OK) { 29852712Snn35248 zerror("Install command line too long"); 29862712Snn35248 return (Z_ERR); 29872712Snn35248 } 29882712Snn35248 break; 29890Sstevel@tonic-gate } 29900Sstevel@tonic-gate } 29912712Snn35248 29922712Snn35248 if (!is_native_zone) { 29932712Snn35248 for (; optind < argc; optind++) { 29942712Snn35248 if (addopt(cmdbuf, 0, argv[optind], 29952712Snn35248 sizeof (cmdbuf)) != Z_OK) { 29962712Snn35248 zerror("Install command line too long"); 29972712Snn35248 return (Z_ERR); 29982712Snn35248 } 29992712Snn35248 } 30002712Snn35248 } 30012712Snn35248 30022712Snn35248 if (sanity_check(target_zone, CMD_INSTALL, B_FALSE, B_TRUE, B_FALSE) 30032712Snn35248 != Z_OK) 30040Sstevel@tonic-gate return (Z_ERR); 30050Sstevel@tonic-gate if (verify_details(CMD_INSTALL) != Z_OK) 30060Sstevel@tonic-gate return (Z_ERR); 30070Sstevel@tonic-gate 30080Sstevel@tonic-gate if (grab_lock_file(target_zone, &lockfd) != Z_OK) { 30090Sstevel@tonic-gate zerror(gettext("another %s may have an operation in progress."), 30101300Sgjelinek "zoneadm"); 30110Sstevel@tonic-gate return (Z_ERR); 30120Sstevel@tonic-gate } 30130Sstevel@tonic-gate err = zone_set_state(target_zone, ZONE_STATE_INCOMPLETE); 30140Sstevel@tonic-gate if (err != Z_OK) { 30150Sstevel@tonic-gate errno = err; 30160Sstevel@tonic-gate zperror2(target_zone, gettext("could not set state")); 30170Sstevel@tonic-gate goto done; 30180Sstevel@tonic-gate } 30190Sstevel@tonic-gate 30202712Snn35248 if (!nodataset) 30212712Snn35248 create_zfs_zonepath(zonepath); 30222712Snn35248 30230Sstevel@tonic-gate /* 30240Sstevel@tonic-gate * According to the Application Packaging Developer's Guide, a 30250Sstevel@tonic-gate * "checkinstall" script when included in a package is executed as 30260Sstevel@tonic-gate * the user "install", if such a user exists, or by the user 30270Sstevel@tonic-gate * "nobody". In order to support this dubious behavior, the path 30280Sstevel@tonic-gate * to the zone being constructed is opened up during the life of 30290Sstevel@tonic-gate * the command laying down the zone's root file system. Once this 30300Sstevel@tonic-gate * has completed, regardless of whether it was successful, the 30310Sstevel@tonic-gate * path to the zone is again restricted. 30320Sstevel@tonic-gate */ 30330Sstevel@tonic-gate if (chmod(zonepath, DEFAULT_DIR_MODE) != 0) { 30340Sstevel@tonic-gate zperror(zonepath, B_FALSE); 30350Sstevel@tonic-gate err = Z_ERR; 30360Sstevel@tonic-gate goto done; 30370Sstevel@tonic-gate } 30380Sstevel@tonic-gate 30392712Snn35248 if (is_native_zone) 30402712Snn35248 status = do_subproc(cmdbuf); 30412712Snn35248 else 30422712Snn35248 status = do_subproc_interactive(cmdbuf); 30432712Snn35248 30440Sstevel@tonic-gate if (chmod(zonepath, S_IRWXU) != 0) { 30450Sstevel@tonic-gate zperror(zonepath, B_FALSE); 30460Sstevel@tonic-gate err = Z_ERR; 30470Sstevel@tonic-gate goto done; 30480Sstevel@tonic-gate } 30492712Snn35248 if ((subproc_err = 30502712Snn35248 subproc_status(gettext("brand-specific installation"), status, 30512712Snn35248 B_FALSE)) != ZONE_SUBPROC_OK) { 30522712Snn35248 err = Z_ERR; 30530Sstevel@tonic-gate goto done; 30542712Snn35248 } 30550Sstevel@tonic-gate 30560Sstevel@tonic-gate if ((err = zone_set_state(target_zone, ZONE_STATE_INSTALLED)) != Z_OK) { 30570Sstevel@tonic-gate errno = err; 30580Sstevel@tonic-gate zperror2(target_zone, gettext("could not set state")); 30590Sstevel@tonic-gate goto done; 30600Sstevel@tonic-gate } 30610Sstevel@tonic-gate 30620Sstevel@tonic-gate done: 30632712Snn35248 /* 30642712Snn35248 * If the install script exited with ZONE_SUBPROC_USAGE or 30652712Snn35248 * ZONE_SUBPROC_NOTCOMPLETE, try to clean up the zone and leave the 30662712Snn35248 * zone in the CONFIGURED state so that another install can be 30672712Snn35248 * attempted without requiring an uninstall first. 30682712Snn35248 */ 30692712Snn35248 if ((subproc_err == ZONE_SUBPROC_USAGE) || 30702712Snn35248 (subproc_err == ZONE_SUBPROC_NOTCOMPLETE)) { 30712712Snn35248 if ((err = cleanup_zonepath(zonepath, B_FALSE)) != Z_OK) { 30722712Snn35248 errno = err; 30732712Snn35248 zperror2(target_zone, 30742712Snn35248 gettext("cleaning up zonepath failed")); 30752712Snn35248 } else if ((err = zone_set_state(target_zone, 30762712Snn35248 ZONE_STATE_CONFIGURED)) != Z_OK) { 30772712Snn35248 errno = err; 30782712Snn35248 zperror2(target_zone, gettext("could not set state")); 30792712Snn35248 } 30802712Snn35248 } 30812712Snn35248 30820Sstevel@tonic-gate release_lock_file(lockfd); 30830Sstevel@tonic-gate return ((err == Z_OK) ? Z_OK : Z_ERR); 30840Sstevel@tonic-gate } 30850Sstevel@tonic-gate 30860Sstevel@tonic-gate /* 30871300Sgjelinek * Check that the inherited pkg dirs are the same for the clone and its source. 30881300Sgjelinek * The easiest way to do that is check that the list of ipds is the same 30891300Sgjelinek * by matching each one against the other. This algorithm should be fine since 30901300Sgjelinek * the list of ipds should not be that long. 30911300Sgjelinek */ 30921300Sgjelinek static int 30931300Sgjelinek valid_ipd_clone(zone_dochandle_t s_handle, char *source_zone, 30941300Sgjelinek zone_dochandle_t t_handle, char *target_zone) 30951300Sgjelinek { 30961300Sgjelinek int err; 30971300Sgjelinek int res = Z_OK; 30981300Sgjelinek int s_cnt = 0; 30991300Sgjelinek int t_cnt = 0; 31001300Sgjelinek struct zone_fstab s_fstab; 31011300Sgjelinek struct zone_fstab t_fstab; 31021300Sgjelinek 31031300Sgjelinek /* 31041300Sgjelinek * First check the source of the clone against the target. 31051300Sgjelinek */ 31061300Sgjelinek if ((err = zonecfg_setipdent(s_handle)) != Z_OK) { 31071300Sgjelinek errno = err; 31081300Sgjelinek zperror2(source_zone, gettext("could not enumerate " 31091300Sgjelinek "inherit-pkg-dirs")); 31101300Sgjelinek return (Z_ERR); 31111300Sgjelinek } 31121300Sgjelinek 31131300Sgjelinek while (zonecfg_getipdent(s_handle, &s_fstab) == Z_OK) { 31141300Sgjelinek boolean_t match = B_FALSE; 31151300Sgjelinek 31161300Sgjelinek s_cnt++; 31171300Sgjelinek 31181300Sgjelinek if ((err = zonecfg_setipdent(t_handle)) != Z_OK) { 31191300Sgjelinek errno = err; 31201300Sgjelinek zperror2(target_zone, gettext("could not enumerate " 31211300Sgjelinek "inherit-pkg-dirs")); 31221300Sgjelinek (void) zonecfg_endipdent(s_handle); 31231300Sgjelinek return (Z_ERR); 31241300Sgjelinek } 31251300Sgjelinek 31261300Sgjelinek while (zonecfg_getipdent(t_handle, &t_fstab) == Z_OK) { 31271300Sgjelinek if (strcmp(s_fstab.zone_fs_dir, t_fstab.zone_fs_dir) 31281300Sgjelinek == 0) { 31291300Sgjelinek match = B_TRUE; 31301300Sgjelinek break; 31311300Sgjelinek } 31321300Sgjelinek } 31331300Sgjelinek (void) zonecfg_endipdent(t_handle); 31341300Sgjelinek 31351300Sgjelinek if (!match) { 31361300Sgjelinek (void) fprintf(stderr, gettext("inherit-pkg-dir " 31371300Sgjelinek "'%s' is not configured in zone %s.\n"), 31381300Sgjelinek s_fstab.zone_fs_dir, target_zone); 31391300Sgjelinek res = Z_ERR; 31401300Sgjelinek } 31411300Sgjelinek } 31421300Sgjelinek 31431300Sgjelinek (void) zonecfg_endipdent(s_handle); 31441300Sgjelinek 31451300Sgjelinek /* skip the next check if we already have errors */ 31461300Sgjelinek if (res == Z_ERR) 31471300Sgjelinek return (res); 31481300Sgjelinek 31491300Sgjelinek /* 31501300Sgjelinek * Now check the number of ipds in the target so we can verify 31511300Sgjelinek * that the source is not a subset of the target. 31521300Sgjelinek */ 31531300Sgjelinek if ((err = zonecfg_setipdent(t_handle)) != Z_OK) { 31541300Sgjelinek errno = err; 31551300Sgjelinek zperror2(target_zone, gettext("could not enumerate " 31561300Sgjelinek "inherit-pkg-dirs")); 31571300Sgjelinek return (Z_ERR); 31581300Sgjelinek } 31591300Sgjelinek 31601300Sgjelinek while (zonecfg_getipdent(t_handle, &t_fstab) == Z_OK) 31611300Sgjelinek t_cnt++; 31621300Sgjelinek 31631300Sgjelinek (void) zonecfg_endipdent(t_handle); 31641300Sgjelinek 31651300Sgjelinek if (t_cnt != s_cnt) { 31661300Sgjelinek (void) fprintf(stderr, gettext("Zone %s is configured " 31671300Sgjelinek "with inherit-pkg-dirs that are not configured in zone " 31681300Sgjelinek "%s.\n"), target_zone, source_zone); 31691300Sgjelinek res = Z_ERR; 31701300Sgjelinek } 31711300Sgjelinek 31721300Sgjelinek return (res); 31731300Sgjelinek } 31741300Sgjelinek 31751300Sgjelinek static void 31761300Sgjelinek warn_dev_match(zone_dochandle_t s_handle, char *source_zone, 31771300Sgjelinek zone_dochandle_t t_handle, char *target_zone) 31781300Sgjelinek { 31791300Sgjelinek int err; 31801300Sgjelinek struct zone_devtab s_devtab; 31811300Sgjelinek struct zone_devtab t_devtab; 31821300Sgjelinek 31831300Sgjelinek if ((err = zonecfg_setdevent(t_handle)) != Z_OK) { 31841300Sgjelinek errno = err; 31851300Sgjelinek zperror2(target_zone, gettext("could not enumerate devices")); 31861300Sgjelinek return; 31871300Sgjelinek } 31881300Sgjelinek 31891300Sgjelinek while (zonecfg_getdevent(t_handle, &t_devtab) == Z_OK) { 31901300Sgjelinek if ((err = zonecfg_setdevent(s_handle)) != Z_OK) { 31911300Sgjelinek errno = err; 31921300Sgjelinek zperror2(source_zone, 31931300Sgjelinek gettext("could not enumerate devices")); 31941300Sgjelinek (void) zonecfg_enddevent(t_handle); 31951300Sgjelinek return; 31961300Sgjelinek } 31971300Sgjelinek 31981300Sgjelinek while (zonecfg_getdevent(s_handle, &s_devtab) == Z_OK) { 31991300Sgjelinek /* 32001300Sgjelinek * Use fnmatch to catch the case where wildcards 32011300Sgjelinek * were used in one zone and the other has an 32021300Sgjelinek * explicit entry (e.g. /dev/dsk/c0t0d0s6 vs. 32031300Sgjelinek * /dev/\*dsk/c0t0d0s6). 32041300Sgjelinek */ 32051300Sgjelinek if (fnmatch(t_devtab.zone_dev_match, 32061300Sgjelinek s_devtab.zone_dev_match, FNM_PATHNAME) == 0 || 32071300Sgjelinek fnmatch(s_devtab.zone_dev_match, 32081300Sgjelinek t_devtab.zone_dev_match, FNM_PATHNAME) == 0) { 32091300Sgjelinek (void) fprintf(stderr, 32101300Sgjelinek gettext("WARNING: device '%s' " 32111300Sgjelinek "is configured in both zones.\n"), 32121300Sgjelinek t_devtab.zone_dev_match); 32131300Sgjelinek break; 32141300Sgjelinek } 32151300Sgjelinek } 32161300Sgjelinek (void) zonecfg_enddevent(s_handle); 32171300Sgjelinek } 32181300Sgjelinek 32191300Sgjelinek (void) zonecfg_enddevent(t_handle); 32201300Sgjelinek } 32211300Sgjelinek 32221300Sgjelinek /* 32231300Sgjelinek * Check if the specified mount option (opt) is contained within the 32241300Sgjelinek * options string. 32251300Sgjelinek */ 32261300Sgjelinek static boolean_t 32271300Sgjelinek opt_match(char *opt, char *options) 32281300Sgjelinek { 32291300Sgjelinek char *p; 32301300Sgjelinek char *lastp; 32311300Sgjelinek 32321300Sgjelinek if ((p = strtok_r(options, ",", &lastp)) != NULL) { 32331300Sgjelinek if (strcmp(p, opt) == 0) 32341300Sgjelinek return (B_TRUE); 32351300Sgjelinek while ((p = strtok_r(NULL, ",", &lastp)) != NULL) { 32361300Sgjelinek if (strcmp(p, opt) == 0) 32371300Sgjelinek return (B_TRUE); 32381300Sgjelinek } 32391300Sgjelinek } 32401300Sgjelinek 32411300Sgjelinek return (B_FALSE); 32421300Sgjelinek } 32431300Sgjelinek 32441867Sgjelinek #define RW_LOFS "WARNING: read-write lofs file system on '%s' is configured " \ 32451300Sgjelinek "in both zones.\n" 32461300Sgjelinek 32471300Sgjelinek static void 32481300Sgjelinek print_fs_warnings(struct zone_fstab *s_fstab, struct zone_fstab *t_fstab) 32491300Sgjelinek { 32501300Sgjelinek /* 32511300Sgjelinek * It is ok to have shared lofs mounted fs but we want to warn if 32521300Sgjelinek * either is rw since this will effect the other zone. 32531300Sgjelinek */ 32541300Sgjelinek if (strcmp(t_fstab->zone_fs_type, "lofs") == 0) { 32551300Sgjelinek zone_fsopt_t *optp; 32561300Sgjelinek 32571300Sgjelinek /* The default is rw so no options means rw */ 32581300Sgjelinek if (t_fstab->zone_fs_options == NULL || 32591300Sgjelinek s_fstab->zone_fs_options == NULL) { 32601300Sgjelinek (void) fprintf(stderr, gettext(RW_LOFS), 32611300Sgjelinek t_fstab->zone_fs_special); 32621300Sgjelinek return; 32631300Sgjelinek } 32641300Sgjelinek 32651300Sgjelinek for (optp = s_fstab->zone_fs_options; optp != NULL; 32661300Sgjelinek optp = optp->zone_fsopt_next) { 32671300Sgjelinek if (opt_match("rw", optp->zone_fsopt_opt)) { 32681300Sgjelinek (void) fprintf(stderr, gettext(RW_LOFS), 32691300Sgjelinek s_fstab->zone_fs_special); 32701300Sgjelinek return; 32711300Sgjelinek } 32721300Sgjelinek } 32731300Sgjelinek 32741300Sgjelinek for (optp = t_fstab->zone_fs_options; optp != NULL; 32751300Sgjelinek optp = optp->zone_fsopt_next) { 32761300Sgjelinek if (opt_match("rw", optp->zone_fsopt_opt)) { 32771300Sgjelinek (void) fprintf(stderr, gettext(RW_LOFS), 32781300Sgjelinek t_fstab->zone_fs_special); 32791300Sgjelinek return; 32801300Sgjelinek } 32811300Sgjelinek } 32821300Sgjelinek 32831300Sgjelinek return; 32841300Sgjelinek } 32851300Sgjelinek 32861300Sgjelinek /* 32871300Sgjelinek * TRANSLATION_NOTE 32881867Sgjelinek * The first variable is the file system type and the second is 32891867Sgjelinek * the file system special device. For example, 32901867Sgjelinek * WARNING: ufs file system on '/dev/dsk/c0t0d0s0' ... 32911300Sgjelinek */ 32921867Sgjelinek (void) fprintf(stderr, gettext("WARNING: %s file system on '%s' " 32931300Sgjelinek "is configured in both zones.\n"), t_fstab->zone_fs_type, 32941300Sgjelinek t_fstab->zone_fs_special); 32951300Sgjelinek } 32961300Sgjelinek 32971300Sgjelinek static void 32981300Sgjelinek warn_fs_match(zone_dochandle_t s_handle, char *source_zone, 32991300Sgjelinek zone_dochandle_t t_handle, char *target_zone) 33001300Sgjelinek { 33011300Sgjelinek int err; 33021300Sgjelinek struct zone_fstab s_fstab; 33031300Sgjelinek struct zone_fstab t_fstab; 33041300Sgjelinek 33051300Sgjelinek if ((err = zonecfg_setfsent(t_handle)) != Z_OK) { 33061300Sgjelinek errno = err; 33071300Sgjelinek zperror2(target_zone, 33081867Sgjelinek gettext("could not enumerate file systems")); 33091300Sgjelinek return; 33101300Sgjelinek } 33111300Sgjelinek 33121300Sgjelinek while (zonecfg_getfsent(t_handle, &t_fstab) == Z_OK) { 33131300Sgjelinek if ((err = zonecfg_setfsent(s_handle)) != Z_OK) { 33141300Sgjelinek errno = err; 33151300Sgjelinek zperror2(source_zone, 33161867Sgjelinek gettext("could not enumerate file systems")); 33171300Sgjelinek (void) zonecfg_endfsent(t_handle); 33181300Sgjelinek return; 33191300Sgjelinek } 33201300Sgjelinek 33211300Sgjelinek while (zonecfg_getfsent(s_handle, &s_fstab) == Z_OK) { 33221300Sgjelinek if (strcmp(t_fstab.zone_fs_special, 33231300Sgjelinek s_fstab.zone_fs_special) == 0) { 33241300Sgjelinek print_fs_warnings(&s_fstab, &t_fstab); 33251300Sgjelinek break; 33261300Sgjelinek } 33271300Sgjelinek } 33281300Sgjelinek (void) zonecfg_endfsent(s_handle); 33291300Sgjelinek } 33301300Sgjelinek 33311300Sgjelinek (void) zonecfg_endfsent(t_handle); 33321300Sgjelinek } 33331300Sgjelinek 33341300Sgjelinek /* 33351300Sgjelinek * We don't catch the case where you used the same IP address but 33361300Sgjelinek * it is not an exact string match. For example, 192.9.0.128 vs. 192.09.0.128. 33371300Sgjelinek * However, we're not going to worry about that but we will check for 33381300Sgjelinek * a possible netmask on one of the addresses (e.g. 10.0.0.1 and 10.0.0.1/24) 33391300Sgjelinek * and handle that case as a match. 33401300Sgjelinek */ 33411300Sgjelinek static void 33421300Sgjelinek warn_ip_match(zone_dochandle_t s_handle, char *source_zone, 33431300Sgjelinek zone_dochandle_t t_handle, char *target_zone) 33441300Sgjelinek { 33451300Sgjelinek int err; 33461300Sgjelinek struct zone_nwiftab s_nwiftab; 33471300Sgjelinek struct zone_nwiftab t_nwiftab; 33481300Sgjelinek 33491300Sgjelinek if ((err = zonecfg_setnwifent(t_handle)) != Z_OK) { 33501300Sgjelinek errno = err; 33511300Sgjelinek zperror2(target_zone, 33521300Sgjelinek gettext("could not enumerate network interfaces")); 33531300Sgjelinek return; 33541300Sgjelinek } 33551300Sgjelinek 33561300Sgjelinek while (zonecfg_getnwifent(t_handle, &t_nwiftab) == Z_OK) { 33571300Sgjelinek char *p; 33581300Sgjelinek 33591300Sgjelinek /* remove an (optional) netmask from the address */ 33601300Sgjelinek if ((p = strchr(t_nwiftab.zone_nwif_address, '/')) != NULL) 33611300Sgjelinek *p = '\0'; 33621300Sgjelinek 33631300Sgjelinek if ((err = zonecfg_setnwifent(s_handle)) != Z_OK) { 33641300Sgjelinek errno = err; 33651300Sgjelinek zperror2(source_zone, 33661300Sgjelinek gettext("could not enumerate network interfaces")); 33671300Sgjelinek (void) zonecfg_endnwifent(t_handle); 33681300Sgjelinek return; 33691300Sgjelinek } 33701300Sgjelinek 33711300Sgjelinek while (zonecfg_getnwifent(s_handle, &s_nwiftab) == Z_OK) { 33721300Sgjelinek /* remove an (optional) netmask from the address */ 33731300Sgjelinek if ((p = strchr(s_nwiftab.zone_nwif_address, '/')) 33741300Sgjelinek != NULL) 33751300Sgjelinek *p = '\0'; 33761300Sgjelinek 33771300Sgjelinek if (strcmp(t_nwiftab.zone_nwif_address, 33781300Sgjelinek s_nwiftab.zone_nwif_address) == 0) { 33791300Sgjelinek (void) fprintf(stderr, 33801300Sgjelinek gettext("WARNING: network address '%s' " 33811300Sgjelinek "is configured in both zones.\n"), 33821300Sgjelinek t_nwiftab.zone_nwif_address); 33831300Sgjelinek break; 33841300Sgjelinek } 33851300Sgjelinek } 33861300Sgjelinek (void) zonecfg_endnwifent(s_handle); 33871300Sgjelinek } 33881300Sgjelinek 33891300Sgjelinek (void) zonecfg_endnwifent(t_handle); 33901300Sgjelinek } 33911300Sgjelinek 33921300Sgjelinek static void 33932712Snn35248 warn_dataset_match(zone_dochandle_t s_handle, char *source, 33942712Snn35248 zone_dochandle_t t_handle, char *target) 33951300Sgjelinek { 33961300Sgjelinek int err; 33971300Sgjelinek struct zone_dstab s_dstab; 33981300Sgjelinek struct zone_dstab t_dstab; 33991300Sgjelinek 34001300Sgjelinek if ((err = zonecfg_setdsent(t_handle)) != Z_OK) { 34011300Sgjelinek errno = err; 34022712Snn35248 zperror2(target, gettext("could not enumerate datasets")); 34031300Sgjelinek return; 34041300Sgjelinek } 34051300Sgjelinek 34061300Sgjelinek while (zonecfg_getdsent(t_handle, &t_dstab) == Z_OK) { 34071300Sgjelinek if ((err = zonecfg_setdsent(s_handle)) != Z_OK) { 34081300Sgjelinek errno = err; 34092712Snn35248 zperror2(source, 34101300Sgjelinek gettext("could not enumerate datasets")); 34111300Sgjelinek (void) zonecfg_enddsent(t_handle); 34121300Sgjelinek return; 34131300Sgjelinek } 34141300Sgjelinek 34151300Sgjelinek while (zonecfg_getdsent(s_handle, &s_dstab) == Z_OK) { 34161300Sgjelinek if (strcmp(t_dstab.zone_dataset_name, 34171300Sgjelinek s_dstab.zone_dataset_name) == 0) { 34182712Snn35248 target_zone = source; 34192712Snn35248 zerror(gettext("WARNING: dataset '%s' " 34201300Sgjelinek "is configured in both zones.\n"), 34211300Sgjelinek t_dstab.zone_dataset_name); 34221300Sgjelinek break; 34231300Sgjelinek } 34241300Sgjelinek } 34251300Sgjelinek (void) zonecfg_enddsent(s_handle); 34261300Sgjelinek } 34271300Sgjelinek 34281300Sgjelinek (void) zonecfg_enddsent(t_handle); 34291300Sgjelinek } 34301300Sgjelinek 34312712Snn35248 /* 34322712Snn35248 * Check that the clone and its source have the same brand type. 34332712Snn35248 */ 34342712Snn35248 static int 34352712Snn35248 valid_brand_clone(char *source_zone, char *target_zone) 34362712Snn35248 { 34372727Sedp brand_handle_t bh; 34382712Snn35248 char source_brand[MAXNAMELEN]; 34392712Snn35248 34402712Snn35248 if ((zone_get_brand(source_zone, source_brand, 34412712Snn35248 sizeof (source_brand))) != Z_OK) { 34422712Snn35248 (void) fprintf(stderr, "%s: zone '%s': %s\n", 34432712Snn35248 execname, source_zone, gettext("missing or invalid brand")); 34442712Snn35248 return (Z_ERR); 34452712Snn35248 } 34462712Snn35248 34472712Snn35248 if (strcmp(source_brand, target_brand) != NULL) { 34482712Snn35248 (void) fprintf(stderr, 34492712Snn35248 gettext("%s: Zones '%s' and '%s' have different brand " 34502712Snn35248 "types.\n"), execname, source_zone, target_zone); 34512712Snn35248 return (Z_ERR); 34522712Snn35248 } 34532712Snn35248 34542727Sedp if ((bh = brand_open(target_brand)) == NULL) { 34552712Snn35248 zerror(gettext("missing or invalid brand")); 34562712Snn35248 return (Z_ERR); 34572712Snn35248 } 34582727Sedp brand_close(bh); 34592712Snn35248 return (Z_OK); 34602712Snn35248 } 34612712Snn35248 34621300Sgjelinek static int 34631300Sgjelinek validate_clone(char *source_zone, char *target_zone) 34641300Sgjelinek { 34651300Sgjelinek int err = Z_OK; 34661300Sgjelinek zone_dochandle_t s_handle; 34671300Sgjelinek zone_dochandle_t t_handle; 34681300Sgjelinek 34691300Sgjelinek if ((t_handle = zonecfg_init_handle()) == NULL) { 34701300Sgjelinek zperror(cmd_to_str(CMD_CLONE), B_TRUE); 34711300Sgjelinek return (Z_ERR); 34721300Sgjelinek } 34731300Sgjelinek if ((err = zonecfg_get_handle(target_zone, t_handle)) != Z_OK) { 34741300Sgjelinek errno = err; 34751300Sgjelinek zperror(cmd_to_str(CMD_CLONE), B_TRUE); 34761300Sgjelinek zonecfg_fini_handle(t_handle); 34771300Sgjelinek return (Z_ERR); 34781300Sgjelinek } 34791300Sgjelinek 34801300Sgjelinek if ((s_handle = zonecfg_init_handle()) == NULL) { 34811300Sgjelinek zperror(cmd_to_str(CMD_CLONE), B_TRUE); 34821300Sgjelinek zonecfg_fini_handle(t_handle); 34831300Sgjelinek return (Z_ERR); 34841300Sgjelinek } 34851300Sgjelinek if ((err = zonecfg_get_handle(source_zone, s_handle)) != Z_OK) { 34861300Sgjelinek errno = err; 34871300Sgjelinek zperror(cmd_to_str(CMD_CLONE), B_TRUE); 34881300Sgjelinek goto done; 34891300Sgjelinek } 34901300Sgjelinek 34912712Snn35248 /* verify new zone has same brand type */ 34922712Snn35248 err = valid_brand_clone(source_zone, target_zone); 34932712Snn35248 if (err != Z_OK) 34942712Snn35248 goto done; 34952712Snn35248 34961300Sgjelinek /* verify new zone has same inherit-pkg-dirs */ 34971300Sgjelinek err = valid_ipd_clone(s_handle, source_zone, t_handle, target_zone); 34981300Sgjelinek 34991300Sgjelinek /* warn about imported fs's which are the same */ 35001300Sgjelinek warn_fs_match(s_handle, source_zone, t_handle, target_zone); 35011300Sgjelinek 35021300Sgjelinek /* warn about imported IP addresses which are the same */ 35031300Sgjelinek warn_ip_match(s_handle, source_zone, t_handle, target_zone); 35041300Sgjelinek 35051300Sgjelinek /* warn about imported devices which are the same */ 35061300Sgjelinek warn_dev_match(s_handle, source_zone, t_handle, target_zone); 35071300Sgjelinek 35081300Sgjelinek /* warn about imported datasets which are the same */ 35091300Sgjelinek warn_dataset_match(s_handle, source_zone, t_handle, target_zone); 35101300Sgjelinek 35111300Sgjelinek done: 35121300Sgjelinek zonecfg_fini_handle(t_handle); 35131300Sgjelinek zonecfg_fini_handle(s_handle); 35141300Sgjelinek 35151300Sgjelinek return ((err == Z_OK) ? Z_OK : Z_ERR); 35161300Sgjelinek } 35171300Sgjelinek 35181300Sgjelinek static int 35191300Sgjelinek copy_zone(char *src, char *dst) 35201300Sgjelinek { 35211300Sgjelinek boolean_t out_null = B_FALSE; 35221300Sgjelinek int status; 35231300Sgjelinek char *outfile; 35241300Sgjelinek char cmdbuf[MAXPATHLEN * 2 + 128]; 35251300Sgjelinek 35261300Sgjelinek if ((outfile = tempnam("/var/log", "zone")) == NULL) { 35271300Sgjelinek outfile = "/dev/null"; 35281300Sgjelinek out_null = B_TRUE; 35291300Sgjelinek } 35301300Sgjelinek 35311867Sgjelinek /* 35321867Sgjelinek * Use find to get the list of files to copy. We need to skip 35331867Sgjelinek * files of type "socket" since cpio can't handle those but that 35341867Sgjelinek * should be ok since the app will recreate the socket when it runs. 35351867Sgjelinek * We also need to filter out anything under the .zfs subdir. Since 35361867Sgjelinek * find is running depth-first, we need the extra egrep to filter .zfs. 35371867Sgjelinek */ 35381300Sgjelinek (void) snprintf(cmdbuf, sizeof (cmdbuf), 35391867Sgjelinek "cd %s && /usr/bin/find . -type s -prune -o -depth -print | " 35401607Sgjelinek "/usr/bin/egrep -v '^\\./\\.zfs$|^\\./\\.zfs/' | " 35411300Sgjelinek "/usr/bin/cpio -pdmuP@ %s > %s 2>&1", 35421300Sgjelinek src, dst, outfile); 35431300Sgjelinek 35441300Sgjelinek status = do_subproc(cmdbuf); 35451300Sgjelinek 35462712Snn35248 if (subproc_status("copy", status, B_TRUE) != ZONE_SUBPROC_OK) { 35471300Sgjelinek if (!out_null) 35481300Sgjelinek (void) fprintf(stderr, gettext("\nThe copy failed.\n" 35491300Sgjelinek "More information can be found in %s\n"), outfile); 35502712Snn35248 return (Z_ERR); 35511300Sgjelinek } 35521300Sgjelinek 35531300Sgjelinek if (!out_null) 35541300Sgjelinek (void) unlink(outfile); 35551300Sgjelinek 35561300Sgjelinek return (Z_OK); 35571300Sgjelinek } 35581300Sgjelinek 35591300Sgjelinek static int 35602712Snn35248 zone_postclone(char *zonepath) 35611300Sgjelinek { 35622712Snn35248 char cmdbuf[MAXPATHLEN]; 35632712Snn35248 int status; 35642727Sedp brand_handle_t bh; 35652712Snn35248 int err = Z_OK; 35661676Sjpk 35671676Sjpk /* 35682712Snn35248 * Fetch the post-clone command, if any, from the brand 35692712Snn35248 * configuration. 35701568Sgjelinek */ 35712727Sedp if ((bh = brand_open(target_brand)) == NULL) { 35722712Snn35248 zerror(gettext("missing or invalid brand")); 35731568Sgjelinek return (Z_ERR); 35741568Sgjelinek } 35752712Snn35248 (void) strcpy(cmdbuf, EXEC_PREFIX); 35762727Sedp err = brand_get_postclone(bh, target_zone, zonepath, cmdbuf + EXEC_LEN, 35772712Snn35248 sizeof (cmdbuf) - EXEC_LEN, 0, NULL); 35782727Sedp brand_close(bh); 35792712Snn35248 35802712Snn35248 if (err == 0 && strlen(cmdbuf) > EXEC_LEN) { 35812712Snn35248 status = do_subproc(cmdbuf); 35822712Snn35248 if ((err = subproc_status("postclone", status, B_FALSE)) 35832712Snn35248 != ZONE_SUBPROC_OK) { 35842712Snn35248 zerror(gettext("post-clone configuration failed.")); 35852712Snn35248 err = Z_ERR; 35862712Snn35248 } 35872712Snn35248 } 35882712Snn35248 35892712Snn35248 return (err); 35901300Sgjelinek } 35911300Sgjelinek 35921300Sgjelinek /* ARGSUSED */ 35931867Sgjelinek static int 35941300Sgjelinek zfm_print(const char *p, void *r) { 35951300Sgjelinek zerror(" %s\n", p); 35961300Sgjelinek return (0); 35971300Sgjelinek } 35981300Sgjelinek 35991867Sgjelinek int 36001867Sgjelinek clone_copy(char *source_zonepath, char *zonepath) 36011867Sgjelinek { 36021867Sgjelinek int err; 36031867Sgjelinek 36041867Sgjelinek /* Don't clone the zone if anything is still mounted there */ 36051867Sgjelinek if (zonecfg_find_mounts(source_zonepath, NULL, NULL)) { 36061867Sgjelinek zerror(gettext("These file systems are mounted on " 36071867Sgjelinek "subdirectories of %s.\n"), source_zonepath); 36081867Sgjelinek (void) zonecfg_find_mounts(source_zonepath, zfm_print, NULL); 36091867Sgjelinek return (Z_ERR); 36101867Sgjelinek } 36111867Sgjelinek 36121867Sgjelinek /* 36131867Sgjelinek * Attempt to create a ZFS fs for the zonepath. As usual, we don't 36141867Sgjelinek * care if this works or not since we always have the default behavior 36151867Sgjelinek * of a simple directory for the zonepath. 36161867Sgjelinek */ 36171867Sgjelinek create_zfs_zonepath(zonepath); 36181867Sgjelinek 36191867Sgjelinek (void) printf(gettext("Copying %s..."), source_zonepath); 36201867Sgjelinek (void) fflush(stdout); 36211867Sgjelinek 36221867Sgjelinek err = copy_zone(source_zonepath, zonepath); 36231867Sgjelinek 36241867Sgjelinek (void) printf("\n"); 36251867Sgjelinek 36261867Sgjelinek return (err); 36271867Sgjelinek } 36281867Sgjelinek 36291300Sgjelinek static int 36301300Sgjelinek clone_func(int argc, char *argv[]) 36311300Sgjelinek { 36321300Sgjelinek char *source_zone = NULL; 36331300Sgjelinek int lockfd; 36341300Sgjelinek int err, arg; 36351300Sgjelinek char zonepath[MAXPATHLEN]; 36361300Sgjelinek char source_zonepath[MAXPATHLEN]; 36371300Sgjelinek zone_state_t state; 36381300Sgjelinek zone_entry_t *zent; 36391867Sgjelinek char *method = NULL; 36401867Sgjelinek char *snapshot = NULL; 36411300Sgjelinek 36421300Sgjelinek if (zonecfg_in_alt_root()) { 36431300Sgjelinek zerror(gettext("cannot clone zone in alternate root")); 36441300Sgjelinek return (Z_ERR); 36451300Sgjelinek } 36461300Sgjelinek 36471300Sgjelinek optind = 0; 36481867Sgjelinek if ((arg = getopt(argc, argv, "?m:s:")) != EOF) { 36491300Sgjelinek switch (arg) { 36501300Sgjelinek case '?': 36511300Sgjelinek sub_usage(SHELP_CLONE, CMD_CLONE); 36521300Sgjelinek return (optopt == '?' ? Z_OK : Z_USAGE); 36531300Sgjelinek case 'm': 36541300Sgjelinek method = optarg; 36551300Sgjelinek break; 36561867Sgjelinek case 's': 36571867Sgjelinek snapshot = optarg; 36581867Sgjelinek break; 36591300Sgjelinek default: 36601300Sgjelinek sub_usage(SHELP_CLONE, CMD_CLONE); 36611300Sgjelinek return (Z_USAGE); 36621300Sgjelinek } 36631300Sgjelinek } 36641867Sgjelinek if (argc != (optind + 1) || 36651867Sgjelinek (method != NULL && strcmp(method, "copy") != 0)) { 36661300Sgjelinek sub_usage(SHELP_CLONE, CMD_CLONE); 36671300Sgjelinek return (Z_USAGE); 36681300Sgjelinek } 36691300Sgjelinek source_zone = argv[optind]; 36702712Snn35248 if (sanity_check(target_zone, CMD_CLONE, B_FALSE, B_TRUE, B_FALSE) 36712712Snn35248 != Z_OK) 36721300Sgjelinek return (Z_ERR); 36731300Sgjelinek if (verify_details(CMD_CLONE) != Z_OK) 36741300Sgjelinek return (Z_ERR); 36751300Sgjelinek 36761300Sgjelinek /* 36771300Sgjelinek * We also need to do some extra validation on the source zone. 36781300Sgjelinek */ 36791300Sgjelinek if (strcmp(source_zone, GLOBAL_ZONENAME) == 0) { 36801300Sgjelinek zerror(gettext("%s operation is invalid for the global zone."), 36811300Sgjelinek cmd_to_str(CMD_CLONE)); 36821300Sgjelinek return (Z_ERR); 36831300Sgjelinek } 36841300Sgjelinek 36851300Sgjelinek if (strncmp(source_zone, "SUNW", 4) == 0) { 36861300Sgjelinek zerror(gettext("%s operation is invalid for zones starting " 36871300Sgjelinek "with SUNW."), cmd_to_str(CMD_CLONE)); 36881300Sgjelinek return (Z_ERR); 36891300Sgjelinek } 36901300Sgjelinek 36911300Sgjelinek zent = lookup_running_zone(source_zone); 36921300Sgjelinek if (zent != NULL) { 36931300Sgjelinek /* check whether the zone is ready or running */ 36941300Sgjelinek if ((err = zone_get_state(zent->zname, &zent->zstate_num)) 36951300Sgjelinek != Z_OK) { 36961300Sgjelinek errno = err; 36971300Sgjelinek zperror2(zent->zname, gettext("could not get state")); 36981300Sgjelinek /* can't tell, so hedge */ 36991300Sgjelinek zent->zstate_str = "ready/running"; 37001300Sgjelinek } else { 37011300Sgjelinek zent->zstate_str = zone_state_str(zent->zstate_num); 37021300Sgjelinek } 37031300Sgjelinek zerror(gettext("%s operation is invalid for %s zones."), 37041300Sgjelinek cmd_to_str(CMD_CLONE), zent->zstate_str); 37051300Sgjelinek return (Z_ERR); 37061300Sgjelinek } 37071300Sgjelinek 37081300Sgjelinek if ((err = zone_get_state(source_zone, &state)) != Z_OK) { 37091300Sgjelinek errno = err; 37101300Sgjelinek zperror2(source_zone, gettext("could not get state")); 37111300Sgjelinek return (Z_ERR); 37121300Sgjelinek } 37131300Sgjelinek if (state != ZONE_STATE_INSTALLED) { 37141300Sgjelinek (void) fprintf(stderr, 37151300Sgjelinek gettext("%s: zone %s is %s; %s is required.\n"), 37161300Sgjelinek execname, source_zone, zone_state_str(state), 37171300Sgjelinek zone_state_str(ZONE_STATE_INSTALLED)); 37181300Sgjelinek return (Z_ERR); 37191300Sgjelinek } 37201300Sgjelinek 37211300Sgjelinek /* 37221300Sgjelinek * The source zone checks out ok, continue with the clone. 37231300Sgjelinek */ 37241300Sgjelinek 37251300Sgjelinek if (validate_clone(source_zone, target_zone) != Z_OK) 37261300Sgjelinek return (Z_ERR); 37271300Sgjelinek 37281300Sgjelinek if (grab_lock_file(target_zone, &lockfd) != Z_OK) { 37291300Sgjelinek zerror(gettext("another %s may have an operation in progress."), 37301300Sgjelinek "zoneadm"); 37311300Sgjelinek return (Z_ERR); 37321300Sgjelinek } 37331300Sgjelinek 37341300Sgjelinek if ((err = zone_get_zonepath(source_zone, source_zonepath, 37351300Sgjelinek sizeof (source_zonepath))) != Z_OK) { 37361300Sgjelinek errno = err; 37371300Sgjelinek zperror2(source_zone, gettext("could not get zone path")); 37381300Sgjelinek goto done; 37391300Sgjelinek } 37401300Sgjelinek 37411300Sgjelinek if ((err = zone_get_zonepath(target_zone, zonepath, sizeof (zonepath))) 37421300Sgjelinek != Z_OK) { 37431300Sgjelinek errno = err; 37441300Sgjelinek zperror2(target_zone, gettext("could not get zone path")); 37451300Sgjelinek goto done; 37461300Sgjelinek } 37471300Sgjelinek 37481300Sgjelinek if ((err = zone_set_state(target_zone, ZONE_STATE_INCOMPLETE)) 37491300Sgjelinek != Z_OK) { 37501300Sgjelinek errno = err; 37511300Sgjelinek zperror2(target_zone, gettext("could not set state")); 37521300Sgjelinek goto done; 37531300Sgjelinek } 37541300Sgjelinek 37551867Sgjelinek if (snapshot != NULL) { 37561867Sgjelinek err = clone_snapshot_zfs(snapshot, zonepath); 37571867Sgjelinek } else { 37581867Sgjelinek /* 37591867Sgjelinek * We always copy the clone unless the source is ZFS and a 37601867Sgjelinek * ZFS clone worked. We fallback to copying if the ZFS clone 37611867Sgjelinek * fails for some reason. 37621867Sgjelinek */ 37631867Sgjelinek err = Z_ERR; 37641867Sgjelinek if (method == NULL && is_zonepath_zfs(source_zonepath)) 37651867Sgjelinek err = clone_zfs(source_zone, source_zonepath, zonepath); 37661867Sgjelinek 37671867Sgjelinek if (err != Z_OK) 37681867Sgjelinek err = clone_copy(source_zonepath, zonepath); 37691867Sgjelinek } 37701867Sgjelinek 37712712Snn35248 /* 37722712Snn35248 * Trusted Extensions requires that cloned zones use the same sysid 37732712Snn35248 * configuration, so it is not appropriate to perform any 37742712Snn35248 * post-clone reconfiguration. 37752712Snn35248 */ 37762712Snn35248 if ((err == Z_OK) && !is_system_labeled()) 37772712Snn35248 err = zone_postclone(zonepath); 37781300Sgjelinek 37791300Sgjelinek done: 37802712Snn35248 /* 37812712Snn35248 * If everything went well, we mark the zone as installed. 37822712Snn35248 */ 37832712Snn35248 if (err == Z_OK) { 37842712Snn35248 err = zone_set_state(target_zone, ZONE_STATE_INSTALLED); 37852712Snn35248 if (err != Z_OK) { 37862712Snn35248 errno = err; 37872712Snn35248 zperror2(target_zone, gettext("could not set state")); 37882712Snn35248 } 37892712Snn35248 } 37901300Sgjelinek release_lock_file(lockfd); 37911300Sgjelinek return ((err == Z_OK) ? Z_OK : Z_ERR); 37921300Sgjelinek } 37931300Sgjelinek 37941607Sgjelinek /* 37951867Sgjelinek * Used when removing a zonepath after uninstalling or cleaning up after 37961867Sgjelinek * the move subcommand. This handles a zonepath that has non-standard 37971867Sgjelinek * contents so that we will only cleanup the stuff we know about and leave 37981867Sgjelinek * any user data alone. 37991607Sgjelinek * 38001867Sgjelinek * If the "all" parameter is true then we should remove the whole zonepath 38011867Sgjelinek * even if it has non-standard files/directories in it. This can be used when 38021867Sgjelinek * we need to cleanup after moving the zonepath across file systems. 38031867Sgjelinek * 38041867Sgjelinek * We "exec" the RMCOMMAND so that the returned status is that of RMCOMMAND 38051867Sgjelinek * and not the shell. 38061607Sgjelinek */ 38071607Sgjelinek static int 38081867Sgjelinek cleanup_zonepath(char *zonepath, boolean_t all) 38091607Sgjelinek { 38101867Sgjelinek int status; 38111867Sgjelinek int i; 38121867Sgjelinek boolean_t non_std = B_FALSE; 38131867Sgjelinek struct dirent *dp; 38141867Sgjelinek DIR *dirp; 38151867Sgjelinek char *std_entries[] = {"dev", "lu", "root", NULL}; 38161867Sgjelinek /* (MAXPATHLEN * 3) is for the 3 std_entries dirs */ 38171867Sgjelinek char cmdbuf[sizeof (RMCOMMAND) + (MAXPATHLEN * 3) + 64]; 38181867Sgjelinek 38191867Sgjelinek /* 38201867Sgjelinek * We shouldn't need these checks but lets be paranoid since we 38211867Sgjelinek * could blow away the whole system here if we got the wrong zonepath. 38221867Sgjelinek */ 38231867Sgjelinek if (*zonepath == NULL || strcmp(zonepath, "/") == 0) { 38241867Sgjelinek (void) fprintf(stderr, "invalid zonepath '%s'\n", zonepath); 38251867Sgjelinek return (Z_INVAL); 38261867Sgjelinek } 38271867Sgjelinek 38281867Sgjelinek /* 38291867Sgjelinek * If the dirpath is already gone (maybe it was manually removed) then 38301867Sgjelinek * we just return Z_OK so that the cleanup is successful. 38311867Sgjelinek */ 38321867Sgjelinek if ((dirp = opendir(zonepath)) == NULL) 38331867Sgjelinek return (Z_OK); 38341867Sgjelinek 38351867Sgjelinek /* 38361867Sgjelinek * Look through the zonepath directory to see if there are any 38371867Sgjelinek * non-standard files/dirs. Also skip .zfs since that might be 38381867Sgjelinek * there but we'll handle ZFS file systems as a special case. 38391867Sgjelinek */ 38401867Sgjelinek while ((dp = readdir(dirp)) != NULL) { 38411867Sgjelinek if (strcmp(dp->d_name, ".") == 0 || 38421867Sgjelinek strcmp(dp->d_name, "..") == 0 || 38431867Sgjelinek strcmp(dp->d_name, ".zfs") == 0) 38441867Sgjelinek continue; 38451867Sgjelinek 38461867Sgjelinek for (i = 0; std_entries[i] != NULL; i++) 38471867Sgjelinek if (strcmp(dp->d_name, std_entries[i]) == 0) 38481867Sgjelinek break; 38491867Sgjelinek 38501867Sgjelinek if (std_entries[i] == NULL) 38511867Sgjelinek non_std = B_TRUE; 38521867Sgjelinek } 38531867Sgjelinek (void) closedir(dirp); 38541867Sgjelinek 38551867Sgjelinek if (!all && non_std) { 38561607Sgjelinek /* 38571867Sgjelinek * There are extra, non-standard directories/files in the 38581867Sgjelinek * zonepath so we don't want to remove the zonepath. We 38591867Sgjelinek * just want to remove the standard directories and leave 38601867Sgjelinek * the user data alone. 38611607Sgjelinek */ 38621867Sgjelinek (void) snprintf(cmdbuf, sizeof (cmdbuf), "exec " RMCOMMAND); 38631867Sgjelinek 38641867Sgjelinek for (i = 0; std_entries[i] != NULL; i++) { 38651867Sgjelinek char tmpbuf[MAXPATHLEN]; 38661867Sgjelinek 38671867Sgjelinek if (snprintf(tmpbuf, sizeof (tmpbuf), " %s/%s", 38681867Sgjelinek zonepath, std_entries[i]) >= sizeof (tmpbuf) || 38691867Sgjelinek strlcat(cmdbuf, tmpbuf, sizeof (cmdbuf)) >= 38701867Sgjelinek sizeof (cmdbuf)) { 38711867Sgjelinek (void) fprintf(stderr, 38721867Sgjelinek gettext("path is too long\n")); 38731867Sgjelinek return (Z_INVAL); 38741867Sgjelinek } 38751867Sgjelinek } 38761867Sgjelinek 38771867Sgjelinek status = do_subproc(cmdbuf); 38781867Sgjelinek 38791867Sgjelinek (void) fprintf(stderr, gettext("WARNING: Unable to completely " 38801867Sgjelinek "remove %s\nbecause it contains additional user data. " 38811867Sgjelinek "Only the standard directory\nentries have been " 38821867Sgjelinek "removed.\n"), 38831867Sgjelinek zonepath); 38841867Sgjelinek 38852712Snn35248 return ((subproc_status(RMCOMMAND, status, B_TRUE) == 38862712Snn35248 ZONE_SUBPROC_OK) ? Z_OK : Z_ERR); 38871607Sgjelinek } 38881607Sgjelinek 38891867Sgjelinek /* 38901867Sgjelinek * There is nothing unexpected in the zonepath, try to get rid of the 38911867Sgjelinek * whole zonepath directory. 38921867Sgjelinek * 38931867Sgjelinek * If the zonepath is its own zfs file system, try to destroy the 38941867Sgjelinek * file system. If that fails for some reason (e.g. it has clones) 38951867Sgjelinek * then we'll just remove the contents of the zonepath. 38961867Sgjelinek */ 38971867Sgjelinek if (is_zonepath_zfs(zonepath)) { 38981867Sgjelinek if (destroy_zfs(zonepath) == Z_OK) 38991867Sgjelinek return (Z_OK); 39001867Sgjelinek (void) snprintf(cmdbuf, sizeof (cmdbuf), "exec " RMCOMMAND 39011867Sgjelinek " %s/*", zonepath); 39021867Sgjelinek status = do_subproc(cmdbuf); 39032712Snn35248 return ((subproc_status(RMCOMMAND, status, B_TRUE) == 39042712Snn35248 ZONE_SUBPROC_OK) ? Z_OK : Z_ERR); 39051867Sgjelinek } 39061867Sgjelinek 39071867Sgjelinek (void) snprintf(cmdbuf, sizeof (cmdbuf), "exec " RMCOMMAND " %s", 39081867Sgjelinek zonepath); 39091607Sgjelinek status = do_subproc(cmdbuf); 39102712Snn35248 39112712Snn35248 return ((subproc_status(RMCOMMAND, status, B_TRUE) == ZONE_SUBPROC_OK) 39122712Snn35248 ? Z_OK : Z_ERR); 39131607Sgjelinek } 39141607Sgjelinek 39151300Sgjelinek static int 39161300Sgjelinek move_func(int argc, char *argv[]) 39171300Sgjelinek { 39181300Sgjelinek char *new_zonepath = NULL; 39191300Sgjelinek int lockfd; 39201300Sgjelinek int err, arg; 39211300Sgjelinek char zonepath[MAXPATHLEN]; 39221300Sgjelinek zone_dochandle_t handle; 39231300Sgjelinek boolean_t fast; 39241867Sgjelinek boolean_t is_zfs = B_FALSE; 39251867Sgjelinek struct dirent *dp; 39261867Sgjelinek DIR *dirp; 39271867Sgjelinek boolean_t empty = B_TRUE; 39281300Sgjelinek boolean_t revert; 39291300Sgjelinek struct stat zonepath_buf; 39301300Sgjelinek struct stat new_zonepath_buf; 39311300Sgjelinek 39321300Sgjelinek if (zonecfg_in_alt_root()) { 39331300Sgjelinek zerror(gettext("cannot move zone in alternate root")); 39341300Sgjelinek return (Z_ERR); 39351300Sgjelinek } 39361300Sgjelinek 39371300Sgjelinek optind = 0; 39381300Sgjelinek if ((arg = getopt(argc, argv, "?")) != EOF) { 39391300Sgjelinek switch (arg) { 39401300Sgjelinek case '?': 39411300Sgjelinek sub_usage(SHELP_MOVE, CMD_MOVE); 39421300Sgjelinek return (optopt == '?' ? Z_OK : Z_USAGE); 39431300Sgjelinek default: 39441300Sgjelinek sub_usage(SHELP_MOVE, CMD_MOVE); 39451300Sgjelinek return (Z_USAGE); 39461300Sgjelinek } 39471300Sgjelinek } 39481300Sgjelinek if (argc != (optind + 1)) { 39491300Sgjelinek sub_usage(SHELP_MOVE, CMD_MOVE); 39501300Sgjelinek return (Z_USAGE); 39511300Sgjelinek } 39521300Sgjelinek new_zonepath = argv[optind]; 39532712Snn35248 if (sanity_check(target_zone, CMD_MOVE, B_FALSE, B_TRUE, B_FALSE) 39542712Snn35248 != Z_OK) 39551300Sgjelinek return (Z_ERR); 39561300Sgjelinek if (verify_details(CMD_MOVE) != Z_OK) 39571300Sgjelinek return (Z_ERR); 39581300Sgjelinek 39591300Sgjelinek /* 39601300Sgjelinek * Check out the new zonepath. This has the side effect of creating 39611300Sgjelinek * a directory for the new zonepath. We depend on this later when we 39621867Sgjelinek * stat to see if we are doing a cross file system move or not. 39631300Sgjelinek */ 39641300Sgjelinek if (validate_zonepath(new_zonepath, CMD_MOVE) != Z_OK) 39651300Sgjelinek return (Z_ERR); 39661300Sgjelinek 39671300Sgjelinek if ((err = zone_get_zonepath(target_zone, zonepath, sizeof (zonepath))) 39681300Sgjelinek != Z_OK) { 39691300Sgjelinek errno = err; 39701300Sgjelinek zperror2(target_zone, gettext("could not get zone path")); 39711300Sgjelinek return (Z_ERR); 39721300Sgjelinek } 39731300Sgjelinek 39741300Sgjelinek if (stat(zonepath, &zonepath_buf) == -1) { 39751300Sgjelinek zperror(gettext("could not stat zone path"), B_FALSE); 39761300Sgjelinek return (Z_ERR); 39771300Sgjelinek } 39781300Sgjelinek 39791300Sgjelinek if (stat(new_zonepath, &new_zonepath_buf) == -1) { 39801300Sgjelinek zperror(gettext("could not stat new zone path"), B_FALSE); 39811300Sgjelinek return (Z_ERR); 39821300Sgjelinek } 39831300Sgjelinek 39841867Sgjelinek /* 39851867Sgjelinek * Check if the destination directory is empty. 39861867Sgjelinek */ 39871867Sgjelinek if ((dirp = opendir(new_zonepath)) == NULL) { 39881867Sgjelinek zperror(gettext("could not open new zone path"), B_FALSE); 39891867Sgjelinek return (Z_ERR); 39901867Sgjelinek } 39911867Sgjelinek while ((dp = readdir(dirp)) != (struct dirent *)0) { 39921867Sgjelinek if (strcmp(dp->d_name, ".") == 0 || 39931867Sgjelinek strcmp(dp->d_name, "..") == 0) 39941867Sgjelinek continue; 39951867Sgjelinek empty = B_FALSE; 39961867Sgjelinek break; 39971867Sgjelinek } 39981867Sgjelinek (void) closedir(dirp); 39991867Sgjelinek 40001867Sgjelinek /* Error if there is anything in the destination directory. */ 40011867Sgjelinek if (!empty) { 40021867Sgjelinek (void) fprintf(stderr, gettext("could not move zone to %s: " 40031867Sgjelinek "directory not empty\n"), new_zonepath); 40041867Sgjelinek return (Z_ERR); 40051867Sgjelinek } 40061867Sgjelinek 40071300Sgjelinek /* Don't move the zone if anything is still mounted there */ 40081300Sgjelinek if (zonecfg_find_mounts(zonepath, NULL, NULL)) { 40091867Sgjelinek zerror(gettext("These file systems are mounted on " 40101300Sgjelinek "subdirectories of %s.\n"), zonepath); 40111300Sgjelinek (void) zonecfg_find_mounts(zonepath, zfm_print, NULL); 40121300Sgjelinek return (Z_ERR); 40131300Sgjelinek } 40141300Sgjelinek 40151300Sgjelinek /* 40161867Sgjelinek * Check if we are moving in the same file system and can do a fast 40171867Sgjelinek * move or if we are crossing file systems and have to copy the data. 40181300Sgjelinek */ 40191300Sgjelinek fast = (zonepath_buf.st_dev == new_zonepath_buf.st_dev); 40201300Sgjelinek 40211300Sgjelinek if ((handle = zonecfg_init_handle()) == NULL) { 40221300Sgjelinek zperror(cmd_to_str(CMD_MOVE), B_TRUE); 40231300Sgjelinek return (Z_ERR); 40241300Sgjelinek } 40251300Sgjelinek 40261300Sgjelinek if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) { 40271300Sgjelinek errno = err; 40281300Sgjelinek zperror(cmd_to_str(CMD_MOVE), B_TRUE); 40291300Sgjelinek zonecfg_fini_handle(handle); 40301300Sgjelinek return (Z_ERR); 40311300Sgjelinek } 40321300Sgjelinek 40331300Sgjelinek if (grab_lock_file(target_zone, &lockfd) != Z_OK) { 40341300Sgjelinek zerror(gettext("another %s may have an operation in progress."), 40351300Sgjelinek "zoneadm"); 40361300Sgjelinek zonecfg_fini_handle(handle); 40371300Sgjelinek return (Z_ERR); 40381300Sgjelinek } 40391300Sgjelinek 40401300Sgjelinek /* 40411867Sgjelinek * We're making some file system changes now so we have to clean up 40421867Sgjelinek * the file system before we are done. This will either clean up the 40431300Sgjelinek * new zonepath if the zonecfg update failed or it will clean up the 40441300Sgjelinek * old zonepath if everything is ok. 40451300Sgjelinek */ 40461300Sgjelinek revert = B_TRUE; 40471300Sgjelinek 40481867Sgjelinek if (is_zonepath_zfs(zonepath) && 40491867Sgjelinek move_zfs(zonepath, new_zonepath) != Z_ERR) { 40501867Sgjelinek is_zfs = B_TRUE; 40511867Sgjelinek 40521867Sgjelinek } else if (fast) { 40531867Sgjelinek /* same file system, use rename for a quick move */ 40541300Sgjelinek 40551300Sgjelinek /* 40561300Sgjelinek * Remove the new_zonepath directory that got created above 40571300Sgjelinek * during the validation. It gets in the way of the rename. 40581300Sgjelinek */ 40591300Sgjelinek if (rmdir(new_zonepath) != 0) { 40601300Sgjelinek zperror(gettext("could not rmdir new zone path"), 40611300Sgjelinek B_FALSE); 40621300Sgjelinek zonecfg_fini_handle(handle); 40631300Sgjelinek release_lock_file(lockfd); 40641300Sgjelinek return (Z_ERR); 40651300Sgjelinek } 40661300Sgjelinek 40671300Sgjelinek if (rename(zonepath, new_zonepath) != 0) { 40681300Sgjelinek /* 40691300Sgjelinek * If this fails we don't need to do all of the 40701300Sgjelinek * cleanup that happens for the rest of the code 40711300Sgjelinek * so just return from this error. 40721300Sgjelinek */ 40731300Sgjelinek zperror(gettext("could not move zone"), B_FALSE); 40741300Sgjelinek zonecfg_fini_handle(handle); 40751300Sgjelinek release_lock_file(lockfd); 40761300Sgjelinek return (Z_ERR); 40771300Sgjelinek } 40781300Sgjelinek 40791300Sgjelinek } else { 40801867Sgjelinek /* 40811867Sgjelinek * Attempt to create a ZFS fs for the new zonepath. As usual, 40821867Sgjelinek * we don't care if this works or not since we always have the 40831867Sgjelinek * default behavior of a simple directory for the zonepath. 40841867Sgjelinek */ 40851867Sgjelinek create_zfs_zonepath(new_zonepath); 40861867Sgjelinek 40871300Sgjelinek (void) printf(gettext( 40881867Sgjelinek "Moving across file systems; copying zonepath %s..."), 40891300Sgjelinek zonepath); 40901300Sgjelinek (void) fflush(stdout); 40911300Sgjelinek 40921300Sgjelinek err = copy_zone(zonepath, new_zonepath); 40931300Sgjelinek 40941300Sgjelinek (void) printf("\n"); 40951300Sgjelinek if (err != Z_OK) 40961300Sgjelinek goto done; 40971300Sgjelinek } 40981300Sgjelinek 40991300Sgjelinek if ((err = zonecfg_set_zonepath(handle, new_zonepath)) != Z_OK) { 41001300Sgjelinek errno = err; 41011300Sgjelinek zperror(gettext("could not set new zonepath"), B_TRUE); 41021300Sgjelinek goto done; 41031300Sgjelinek } 41041300Sgjelinek 41051300Sgjelinek if ((err = zonecfg_save(handle)) != Z_OK) { 41061300Sgjelinek errno = err; 41071300Sgjelinek zperror(gettext("zonecfg save failed"), B_TRUE); 41081300Sgjelinek goto done; 41091300Sgjelinek } 41101300Sgjelinek 41111300Sgjelinek revert = B_FALSE; 41121300Sgjelinek 41131300Sgjelinek done: 41141300Sgjelinek zonecfg_fini_handle(handle); 41151300Sgjelinek release_lock_file(lockfd); 41161300Sgjelinek 41171300Sgjelinek /* 41181867Sgjelinek * Clean up the file system based on how things went. We either 41191300Sgjelinek * clean up the new zonepath if the operation failed for some reason 41201300Sgjelinek * or we clean up the old zonepath if everything is ok. 41211300Sgjelinek */ 41221300Sgjelinek if (revert) { 41231300Sgjelinek /* The zonecfg update failed, cleanup the new zonepath. */ 41241867Sgjelinek if (is_zfs) { 41251867Sgjelinek if (move_zfs(new_zonepath, zonepath) == Z_ERR) { 41261867Sgjelinek (void) fprintf(stderr, gettext("could not " 41271867Sgjelinek "restore zonepath, the zfs mountpoint is " 41281867Sgjelinek "set as:\n%s\n"), new_zonepath); 41291867Sgjelinek /* 41301867Sgjelinek * err is already != Z_OK since we're reverting 41311867Sgjelinek */ 41321867Sgjelinek } 41331867Sgjelinek 41341867Sgjelinek } else if (fast) { 41351300Sgjelinek if (rename(new_zonepath, zonepath) != 0) { 41361300Sgjelinek zperror(gettext("could not restore zonepath"), 41371300Sgjelinek B_FALSE); 41381300Sgjelinek /* 41391300Sgjelinek * err is already != Z_OK since we're reverting 41401300Sgjelinek */ 41411300Sgjelinek } 41421300Sgjelinek } else { 41431300Sgjelinek (void) printf(gettext("Cleaning up zonepath %s..."), 41441300Sgjelinek new_zonepath); 41451300Sgjelinek (void) fflush(stdout); 41461867Sgjelinek err = cleanup_zonepath(new_zonepath, B_TRUE); 41471300Sgjelinek (void) printf("\n"); 41481300Sgjelinek 41491607Sgjelinek if (err != Z_OK) { 41501300Sgjelinek errno = err; 41511300Sgjelinek zperror(gettext("could not remove new " 41521300Sgjelinek "zonepath"), B_TRUE); 41531300Sgjelinek } else { 41541300Sgjelinek /* 41551300Sgjelinek * Because we're reverting we know the mainline 41561300Sgjelinek * code failed but we just reused the err 41571300Sgjelinek * variable so we reset it back to Z_ERR. 41581300Sgjelinek */ 41591300Sgjelinek err = Z_ERR; 41601300Sgjelinek } 41611300Sgjelinek } 41621300Sgjelinek 41631300Sgjelinek } else { 41641300Sgjelinek /* The move was successful, cleanup the old zonepath. */ 41651867Sgjelinek if (!is_zfs && !fast) { 41661300Sgjelinek (void) printf( 41671300Sgjelinek gettext("Cleaning up zonepath %s..."), zonepath); 41681300Sgjelinek (void) fflush(stdout); 41691867Sgjelinek err = cleanup_zonepath(zonepath, B_TRUE); 41701300Sgjelinek (void) printf("\n"); 41711300Sgjelinek 41721607Sgjelinek if (err != Z_OK) { 41731300Sgjelinek errno = err; 41741300Sgjelinek zperror(gettext("could not remove zonepath"), 41751300Sgjelinek B_TRUE); 41761300Sgjelinek } 41771300Sgjelinek } 41781300Sgjelinek } 41791300Sgjelinek 41801300Sgjelinek return ((err == Z_OK) ? Z_OK : Z_ERR); 41811300Sgjelinek } 41821300Sgjelinek 41831507Sgjelinek static int 41841507Sgjelinek detach_func(int argc, char *argv[]) 41851507Sgjelinek { 41861507Sgjelinek int lockfd; 41871507Sgjelinek int err, arg; 41881507Sgjelinek char zonepath[MAXPATHLEN]; 41891507Sgjelinek zone_dochandle_t handle; 41902078Sgjelinek boolean_t execute = B_TRUE; 41911507Sgjelinek 41921507Sgjelinek if (zonecfg_in_alt_root()) { 41931507Sgjelinek zerror(gettext("cannot detach zone in alternate root")); 41941507Sgjelinek return (Z_ERR); 41951507Sgjelinek } 41961507Sgjelinek 41971507Sgjelinek optind = 0; 41982078Sgjelinek if ((arg = getopt(argc, argv, "?n")) != EOF) { 41991507Sgjelinek switch (arg) { 42001507Sgjelinek case '?': 42011507Sgjelinek sub_usage(SHELP_DETACH, CMD_DETACH); 42021507Sgjelinek return (optopt == '?' ? Z_OK : Z_USAGE); 42032078Sgjelinek case 'n': 42042078Sgjelinek execute = B_FALSE; 42052078Sgjelinek break; 42061507Sgjelinek default: 42071507Sgjelinek sub_usage(SHELP_DETACH, CMD_DETACH); 42081507Sgjelinek return (Z_USAGE); 42091507Sgjelinek } 42101507Sgjelinek } 42112712Snn35248 42122078Sgjelinek if (execute) { 42132712Snn35248 if (sanity_check(target_zone, CMD_DETACH, B_FALSE, B_TRUE, 42142712Snn35248 B_FALSE) != Z_OK) 42152078Sgjelinek return (Z_ERR); 42162078Sgjelinek if (verify_details(CMD_DETACH) != Z_OK) 42172078Sgjelinek return (Z_ERR); 42182078Sgjelinek } else { 42192078Sgjelinek /* 42202078Sgjelinek * We want a dry-run to work for a non-privileged user so we 42212078Sgjelinek * only do minimal validation. 42222078Sgjelinek */ 42232078Sgjelinek if (getzoneid() != GLOBAL_ZONEID) { 42242078Sgjelinek zerror(gettext("must be in the global zone to %s a " 42252078Sgjelinek "zone."), cmd_to_str(CMD_DETACH)); 42262078Sgjelinek return (Z_ERR); 42272078Sgjelinek } 42282078Sgjelinek 42292078Sgjelinek if (target_zone == NULL) { 42302078Sgjelinek zerror(gettext("no zone specified")); 42312078Sgjelinek return (Z_ERR); 42322078Sgjelinek } 42332078Sgjelinek 42342078Sgjelinek if (strcmp(target_zone, GLOBAL_ZONENAME) == 0) { 42352078Sgjelinek zerror(gettext("%s operation is invalid for the " 42362078Sgjelinek "global zone."), cmd_to_str(CMD_DETACH)); 42372078Sgjelinek return (Z_ERR); 42382078Sgjelinek } 42392078Sgjelinek } 42401507Sgjelinek 42411507Sgjelinek if ((err = zone_get_zonepath(target_zone, zonepath, sizeof (zonepath))) 42421507Sgjelinek != Z_OK) { 42431507Sgjelinek errno = err; 42441507Sgjelinek zperror2(target_zone, gettext("could not get zone path")); 42451507Sgjelinek return (Z_ERR); 42461507Sgjelinek } 42471507Sgjelinek 42481507Sgjelinek /* Don't detach the zone if anything is still mounted there */ 42492078Sgjelinek if (execute && zonecfg_find_mounts(zonepath, NULL, NULL)) { 42501867Sgjelinek zerror(gettext("These file systems are mounted on " 42511507Sgjelinek "subdirectories of %s.\n"), zonepath); 42521507Sgjelinek (void) zonecfg_find_mounts(zonepath, zfm_print, NULL); 42531507Sgjelinek return (Z_ERR); 42541507Sgjelinek } 42551507Sgjelinek 42561507Sgjelinek if ((handle = zonecfg_init_handle()) == NULL) { 42571507Sgjelinek zperror(cmd_to_str(CMD_DETACH), B_TRUE); 42581507Sgjelinek return (Z_ERR); 42591507Sgjelinek } 42601507Sgjelinek 42611507Sgjelinek if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) { 42621507Sgjelinek errno = err; 42631507Sgjelinek zperror(cmd_to_str(CMD_DETACH), B_TRUE); 42641507Sgjelinek zonecfg_fini_handle(handle); 42651507Sgjelinek return (Z_ERR); 42661507Sgjelinek } 42671507Sgjelinek 42682078Sgjelinek if (execute && grab_lock_file(target_zone, &lockfd) != Z_OK) { 42691507Sgjelinek zerror(gettext("another %s may have an operation in progress."), 42701507Sgjelinek "zoneadm"); 42711507Sgjelinek zonecfg_fini_handle(handle); 42721507Sgjelinek return (Z_ERR); 42731507Sgjelinek } 42741507Sgjelinek 42751507Sgjelinek if ((err = zonecfg_get_detach_info(handle, B_TRUE)) != Z_OK) { 42761507Sgjelinek errno = err; 42771507Sgjelinek zperror(gettext("getting the detach information failed"), 42781507Sgjelinek B_TRUE); 42791507Sgjelinek goto done; 42801507Sgjelinek } 42811507Sgjelinek 42822078Sgjelinek if ((err = zonecfg_detach_save(handle, (execute ? 0 : ZONE_DRY_RUN))) 42832078Sgjelinek != Z_OK) { 42841507Sgjelinek errno = err; 42851507Sgjelinek zperror(gettext("saving the detach manifest failed"), B_TRUE); 42861507Sgjelinek goto done; 42871507Sgjelinek } 42881507Sgjelinek 42892078Sgjelinek /* 42902078Sgjelinek * Set the zone state back to configured unless we are running with the 42912078Sgjelinek * no-execute option. 42922078Sgjelinek */ 42932078Sgjelinek if (execute && (err = zone_set_state(target_zone, 42942078Sgjelinek ZONE_STATE_CONFIGURED)) != Z_OK) { 42951507Sgjelinek errno = err; 42961507Sgjelinek zperror(gettext("could not reset state"), B_TRUE); 42971507Sgjelinek } 42981507Sgjelinek 42991507Sgjelinek done: 43001507Sgjelinek zonecfg_fini_handle(handle); 43012078Sgjelinek if (execute) 43022078Sgjelinek release_lock_file(lockfd); 43031507Sgjelinek 43041507Sgjelinek return ((err == Z_OK) ? Z_OK : Z_ERR); 43051507Sgjelinek } 43061507Sgjelinek 43071507Sgjelinek /* 43081507Sgjelinek * During attach we go through and fix up the /dev entries for the zone 43091507Sgjelinek * we are attaching. In order to regenerate /dev with the correct devices, 43101507Sgjelinek * the old /dev will be removed, the zone readied (which generates a new 43111507Sgjelinek * /dev) then halted, then we use the info from the manifest to update 43121507Sgjelinek * the modes, owners, etc. on the new /dev. 43131507Sgjelinek */ 43141507Sgjelinek static int 43151507Sgjelinek dev_fix(zone_dochandle_t handle) 43161507Sgjelinek { 43171507Sgjelinek int res; 43181507Sgjelinek int err; 43191507Sgjelinek int status; 43201507Sgjelinek struct zone_devpermtab devtab; 43211507Sgjelinek zone_cmd_arg_t zarg; 43221507Sgjelinek char devpath[MAXPATHLEN]; 43231507Sgjelinek /* 6: "exec " and " " */ 43241507Sgjelinek char cmdbuf[sizeof (RMCOMMAND) + MAXPATHLEN + 6]; 43251507Sgjelinek 43261507Sgjelinek if ((res = zonecfg_get_zonepath(handle, devpath, sizeof (devpath))) 43271507Sgjelinek != Z_OK) 43281507Sgjelinek return (res); 43291507Sgjelinek 43301507Sgjelinek if (strlcat(devpath, "/dev", sizeof (devpath)) >= sizeof (devpath)) 43311507Sgjelinek return (Z_TOO_BIG); 43321507Sgjelinek 43331507Sgjelinek /* 43341507Sgjelinek * "exec" the command so that the returned status is that of 43351507Sgjelinek * RMCOMMAND and not the shell. 43361507Sgjelinek */ 43372712Snn35248 (void) snprintf(cmdbuf, sizeof (cmdbuf), EXEC_PREFIX RMCOMMAND " %s", 43381507Sgjelinek devpath); 43391507Sgjelinek status = do_subproc(cmdbuf); 43402712Snn35248 if ((err = subproc_status(RMCOMMAND, status, B_TRUE)) != 43412712Snn35248 ZONE_SUBPROC_OK) { 43421507Sgjelinek (void) fprintf(stderr, 43431507Sgjelinek gettext("could not remove existing /dev\n")); 43441507Sgjelinek return (Z_ERR); 43451507Sgjelinek } 43461507Sgjelinek 43471507Sgjelinek /* In order to ready the zone, it must be in the installed state */ 43481507Sgjelinek if ((err = zone_set_state(target_zone, ZONE_STATE_INSTALLED)) != Z_OK) { 43491507Sgjelinek errno = err; 43501507Sgjelinek zperror(gettext("could not reset state"), B_TRUE); 43511507Sgjelinek return (Z_ERR); 43521507Sgjelinek } 43531507Sgjelinek 43541507Sgjelinek /* We have to ready the zone to regen the dev tree */ 43551507Sgjelinek zarg.cmd = Z_READY; 43561507Sgjelinek if (call_zoneadmd(target_zone, &zarg) != 0) { 43571507Sgjelinek zerror(gettext("call to %s failed"), "zoneadmd"); 43581507Sgjelinek return (Z_ERR); 43591507Sgjelinek } 43601507Sgjelinek 43611507Sgjelinek zarg.cmd = Z_HALT; 43621507Sgjelinek if (call_zoneadmd(target_zone, &zarg) != 0) { 43631507Sgjelinek zerror(gettext("call to %s failed"), "zoneadmd"); 43641507Sgjelinek return (Z_ERR); 43651507Sgjelinek } 43661507Sgjelinek 43671507Sgjelinek if (zonecfg_setdevperment(handle) != Z_OK) { 43681507Sgjelinek (void) fprintf(stderr, 43691507Sgjelinek gettext("unable to enumerate device entries\n")); 43701507Sgjelinek return (Z_ERR); 43711507Sgjelinek } 43721507Sgjelinek 43731507Sgjelinek while (zonecfg_getdevperment(handle, &devtab) == Z_OK) { 43741507Sgjelinek int err; 43751507Sgjelinek 43761507Sgjelinek if ((err = zonecfg_devperms_apply(handle, 43771507Sgjelinek devtab.zone_devperm_name, devtab.zone_devperm_uid, 43781507Sgjelinek devtab.zone_devperm_gid, devtab.zone_devperm_mode, 43791507Sgjelinek devtab.zone_devperm_acl)) != Z_OK && err != Z_INVAL) 43801507Sgjelinek (void) fprintf(stderr, gettext("error updating device " 43811507Sgjelinek "%s: %s\n"), devtab.zone_devperm_name, 43821507Sgjelinek zonecfg_strerror(err)); 43831507Sgjelinek 43841507Sgjelinek free(devtab.zone_devperm_acl); 43851507Sgjelinek } 43861507Sgjelinek 43871507Sgjelinek (void) zonecfg_enddevperment(handle); 43881507Sgjelinek 43891507Sgjelinek return (Z_OK); 43901507Sgjelinek } 43911507Sgjelinek 43922078Sgjelinek /* 43932078Sgjelinek * Validate attaching a zone but don't actually do the work. The zone 43942078Sgjelinek * does not have to exist, so there is some complexity getting a new zone 43952078Sgjelinek * configuration set up so that we can perform the validation. This is 43962078Sgjelinek * handled within zonecfg_attach_manifest() which returns two handles; one 43972078Sgjelinek * for the the full configuration to validate (rem_handle) and the other 43982078Sgjelinek * (local_handle) containing only the zone configuration derived from the 43992078Sgjelinek * manifest. 44002078Sgjelinek */ 44012078Sgjelinek static int 44022078Sgjelinek dryrun_attach(char *manifest_path) 44032078Sgjelinek { 44042078Sgjelinek int fd; 44052078Sgjelinek int err; 44062078Sgjelinek int res; 44072078Sgjelinek zone_dochandle_t local_handle; 44082078Sgjelinek zone_dochandle_t rem_handle = NULL; 44092078Sgjelinek 44102078Sgjelinek if (strcmp(manifest_path, "-") == 0) { 44112078Sgjelinek fd = 0; 44122078Sgjelinek } else if ((fd = open(manifest_path, O_RDONLY)) < 0) { 44132078Sgjelinek zperror(gettext("could not open manifest path"), B_FALSE); 44142078Sgjelinek return (Z_ERR); 44152078Sgjelinek } 44162078Sgjelinek 44172078Sgjelinek if ((local_handle = zonecfg_init_handle()) == NULL) { 44182078Sgjelinek zperror(cmd_to_str(CMD_ATTACH), B_TRUE); 44192078Sgjelinek res = Z_ERR; 44202078Sgjelinek goto done; 44212078Sgjelinek } 44222078Sgjelinek 44232078Sgjelinek if ((rem_handle = zonecfg_init_handle()) == NULL) { 44242078Sgjelinek zperror(cmd_to_str(CMD_ATTACH), B_TRUE); 44252078Sgjelinek res = Z_ERR; 44262078Sgjelinek goto done; 44272078Sgjelinek } 44282078Sgjelinek 44292078Sgjelinek if ((err = zonecfg_attach_manifest(fd, local_handle, rem_handle)) 44302078Sgjelinek != Z_OK) { 44312078Sgjelinek if (err == Z_INVALID_DOCUMENT) 44322078Sgjelinek zerror(gettext("Cannot attach to an earlier release " 44332078Sgjelinek "of the operating system")); 44342078Sgjelinek else 44352078Sgjelinek zperror(cmd_to_str(CMD_ATTACH), B_TRUE); 44362078Sgjelinek res = Z_ERR; 44372078Sgjelinek goto done; 44382078Sgjelinek } 44392078Sgjelinek 4440*3172Sgjelinek /* 4441*3172Sgjelinek * Retrieve remote handle brand type and determine whether it is 4442*3172Sgjelinek * native or not. 4443*3172Sgjelinek */ 4444*3172Sgjelinek if (zonecfg_get_brand(rem_handle, target_brand, sizeof (target_brand)) 4445*3172Sgjelinek != Z_OK) { 4446*3172Sgjelinek zerror(gettext("missing or invalid brand")); 4447*3172Sgjelinek exit(Z_ERR); 4448*3172Sgjelinek } 4449*3172Sgjelinek is_native_zone = (strcmp(target_brand, NATIVE_BRAND_NAME) == 0); 4450*3172Sgjelinek 44512078Sgjelinek res = verify_handle(CMD_ATTACH, local_handle); 44522078Sgjelinek 44532078Sgjelinek /* Get the detach information for the locally defined zone. */ 44542078Sgjelinek if ((err = zonecfg_get_detach_info(local_handle, B_FALSE)) != Z_OK) { 44552078Sgjelinek errno = err; 44562078Sgjelinek zperror(gettext("getting the attach information failed"), 44572078Sgjelinek B_TRUE); 44582078Sgjelinek res = Z_ERR; 44592078Sgjelinek } else { 44602078Sgjelinek /* sw_cmp prints error msgs as necessary */ 44612078Sgjelinek if (sw_cmp(local_handle, rem_handle, SW_CMP_NONE) != Z_OK) 44622078Sgjelinek res = Z_ERR; 44632078Sgjelinek } 44642078Sgjelinek 44652078Sgjelinek done: 44662078Sgjelinek if (strcmp(manifest_path, "-") != 0) 44672078Sgjelinek (void) close(fd); 44682078Sgjelinek 44692078Sgjelinek zonecfg_fini_handle(local_handle); 44702078Sgjelinek zonecfg_fini_handle(rem_handle); 44712078Sgjelinek 44722078Sgjelinek return ((res == Z_OK) ? Z_OK : Z_ERR); 44732078Sgjelinek } 44742078Sgjelinek 44751507Sgjelinek static int 44761507Sgjelinek attach_func(int argc, char *argv[]) 44771507Sgjelinek { 44781507Sgjelinek int lockfd; 44791507Sgjelinek int err, arg; 44801507Sgjelinek boolean_t force = B_FALSE; 44811507Sgjelinek zone_dochandle_t handle; 44821507Sgjelinek zone_dochandle_t athandle = NULL; 44831507Sgjelinek char zonepath[MAXPATHLEN]; 44842712Snn35248 char brand[MAXNAMELEN], atbrand[MAXNAMELEN]; 44852078Sgjelinek boolean_t execute = B_TRUE; 44862078Sgjelinek char *manifest_path; 44871507Sgjelinek 44881507Sgjelinek if (zonecfg_in_alt_root()) { 44891507Sgjelinek zerror(gettext("cannot attach zone in alternate root")); 44901507Sgjelinek return (Z_ERR); 44911507Sgjelinek } 44921507Sgjelinek 44931507Sgjelinek optind = 0; 44942078Sgjelinek if ((arg = getopt(argc, argv, "?Fn:")) != EOF) { 44951507Sgjelinek switch (arg) { 44961507Sgjelinek case '?': 44971507Sgjelinek sub_usage(SHELP_ATTACH, CMD_ATTACH); 44981507Sgjelinek return (optopt == '?' ? Z_OK : Z_USAGE); 44991507Sgjelinek case 'F': 45001507Sgjelinek force = B_TRUE; 45011507Sgjelinek break; 45022078Sgjelinek case 'n': 45032078Sgjelinek execute = B_FALSE; 45042078Sgjelinek manifest_path = optarg; 45052078Sgjelinek break; 45061507Sgjelinek default: 45071507Sgjelinek sub_usage(SHELP_ATTACH, CMD_ATTACH); 45081507Sgjelinek return (Z_USAGE); 45091507Sgjelinek } 45101507Sgjelinek } 45112078Sgjelinek 45122078Sgjelinek /* 45132078Sgjelinek * If the no-execute option was specified, we need to branch down 45142078Sgjelinek * a completely different path since there is no zone required to be 45152078Sgjelinek * configured for this option. 45162078Sgjelinek */ 45172078Sgjelinek if (!execute) 45182078Sgjelinek return (dryrun_attach(manifest_path)); 45192078Sgjelinek 45202712Snn35248 if (sanity_check(target_zone, CMD_ATTACH, B_FALSE, B_TRUE, B_FALSE) 45212712Snn35248 != Z_OK) 45221507Sgjelinek return (Z_ERR); 45231507Sgjelinek if (verify_details(CMD_ATTACH) != Z_OK) 45241507Sgjelinek return (Z_ERR); 45251507Sgjelinek 45261507Sgjelinek if ((err = zone_get_zonepath(target_zone, zonepath, sizeof (zonepath))) 45271507Sgjelinek != Z_OK) { 45281507Sgjelinek errno = err; 45291507Sgjelinek zperror2(target_zone, gettext("could not get zone path")); 45301507Sgjelinek return (Z_ERR); 45311507Sgjelinek } 45321507Sgjelinek 45331507Sgjelinek if ((handle = zonecfg_init_handle()) == NULL) { 45341507Sgjelinek zperror(cmd_to_str(CMD_ATTACH), B_TRUE); 45351507Sgjelinek return (Z_ERR); 45361507Sgjelinek } 45371507Sgjelinek 45381507Sgjelinek if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) { 45391507Sgjelinek errno = err; 45401507Sgjelinek zperror(cmd_to_str(CMD_ATTACH), B_TRUE); 45411507Sgjelinek zonecfg_fini_handle(handle); 45421507Sgjelinek return (Z_ERR); 45431507Sgjelinek } 45441507Sgjelinek 45451507Sgjelinek if (grab_lock_file(target_zone, &lockfd) != Z_OK) { 45461507Sgjelinek zerror(gettext("another %s may have an operation in progress."), 45471507Sgjelinek "zoneadm"); 45481507Sgjelinek zonecfg_fini_handle(handle); 45491507Sgjelinek return (Z_ERR); 45501507Sgjelinek } 45511507Sgjelinek 45521507Sgjelinek if (force) 45531507Sgjelinek goto forced; 45541507Sgjelinek 45551507Sgjelinek if ((athandle = zonecfg_init_handle()) == NULL) { 45561507Sgjelinek zperror(cmd_to_str(CMD_ATTACH), B_TRUE); 45571507Sgjelinek goto done; 45581507Sgjelinek } 45591507Sgjelinek 45601507Sgjelinek if ((err = zonecfg_get_attach_handle(zonepath, target_zone, B_TRUE, 45611507Sgjelinek athandle)) != Z_OK) { 45621507Sgjelinek if (err == Z_NO_ZONE) 45631507Sgjelinek zerror(gettext("Not a detached zone")); 45641507Sgjelinek else if (err == Z_INVALID_DOCUMENT) 45651507Sgjelinek zerror(gettext("Cannot attach to an earlier release " 45661507Sgjelinek "of the operating system")); 45671507Sgjelinek else 45681507Sgjelinek zperror(cmd_to_str(CMD_ATTACH), B_TRUE); 45691507Sgjelinek goto done; 45701507Sgjelinek } 45711507Sgjelinek 45721507Sgjelinek /* Get the detach information for the locally defined zone. */ 45731507Sgjelinek if ((err = zonecfg_get_detach_info(handle, B_FALSE)) != Z_OK) { 45741507Sgjelinek errno = err; 45751507Sgjelinek zperror(gettext("getting the attach information failed"), 45761507Sgjelinek B_TRUE); 45771507Sgjelinek goto done; 45781507Sgjelinek } 45791507Sgjelinek 45802712Snn35248 /* 45812712Snn35248 * Ensure that the detached and locally defined zones are both of 45822712Snn35248 * the same brand. 45832712Snn35248 */ 45842712Snn35248 if ((zonecfg_get_brand(handle, brand, sizeof (brand)) != 0) || 45852712Snn35248 (zonecfg_get_brand(athandle, atbrand, sizeof (atbrand)) != 0)) { 45862712Snn35248 err = Z_ERR; 45872712Snn35248 zerror(gettext("missing or invalid brand")); 45882712Snn35248 goto done; 45892712Snn35248 } 45902712Snn35248 45912712Snn35248 if (strcmp(atbrand, brand) != NULL) { 45922712Snn35248 err = Z_ERR; 45932712Snn35248 zerror(gettext("Trying to attach a '%s' zone to a '%s' " 45942712Snn35248 "configuration."), atbrand, brand); 45952712Snn35248 goto done; 45962712Snn35248 } 45972712Snn35248 45981507Sgjelinek /* sw_cmp prints error msgs as necessary */ 45991867Sgjelinek if ((err = sw_cmp(handle, athandle, SW_CMP_NONE)) != Z_OK) 46001507Sgjelinek goto done; 46011507Sgjelinek 46021507Sgjelinek if ((err = dev_fix(athandle)) != Z_OK) 46031507Sgjelinek goto done; 46041507Sgjelinek 46051507Sgjelinek forced: 46061507Sgjelinek 46071507Sgjelinek zonecfg_rm_detached(handle, force); 46081507Sgjelinek 46091507Sgjelinek if ((err = zone_set_state(target_zone, ZONE_STATE_INSTALLED)) != Z_OK) { 46101507Sgjelinek errno = err; 46111507Sgjelinek zperror(gettext("could not reset state"), B_TRUE); 46121507Sgjelinek } 46131507Sgjelinek 46141507Sgjelinek done: 46151507Sgjelinek zonecfg_fini_handle(handle); 46161507Sgjelinek release_lock_file(lockfd); 46171507Sgjelinek if (athandle != NULL) 46181507Sgjelinek zonecfg_fini_handle(athandle); 46191507Sgjelinek 46201507Sgjelinek return ((err == Z_OK) ? Z_OK : Z_ERR); 46211507Sgjelinek } 46221507Sgjelinek 46231300Sgjelinek /* 46240Sstevel@tonic-gate * On input, TRUE => yes, FALSE => no. 46250Sstevel@tonic-gate * On return, TRUE => 1, FALSE => 0, could not ask => -1. 46260Sstevel@tonic-gate */ 46270Sstevel@tonic-gate 46280Sstevel@tonic-gate static int 46290Sstevel@tonic-gate ask_yesno(boolean_t default_answer, const char *question) 46300Sstevel@tonic-gate { 46310Sstevel@tonic-gate char line[64]; /* should be large enough to answer yes or no */ 46320Sstevel@tonic-gate 46330Sstevel@tonic-gate if (!isatty(STDIN_FILENO)) 46340Sstevel@tonic-gate return (-1); 46350Sstevel@tonic-gate for (;;) { 46360Sstevel@tonic-gate (void) printf("%s (%s)? ", question, 46370Sstevel@tonic-gate default_answer ? "[y]/n" : "y/[n]"); 46380Sstevel@tonic-gate if (fgets(line, sizeof (line), stdin) == NULL || 46390Sstevel@tonic-gate line[0] == '\n') 46400Sstevel@tonic-gate return (default_answer ? 1 : 0); 46410Sstevel@tonic-gate if (tolower(line[0]) == 'y') 46420Sstevel@tonic-gate return (1); 46430Sstevel@tonic-gate if (tolower(line[0]) == 'n') 46440Sstevel@tonic-gate return (0); 46450Sstevel@tonic-gate } 46460Sstevel@tonic-gate } 46470Sstevel@tonic-gate 46480Sstevel@tonic-gate static int 46490Sstevel@tonic-gate uninstall_func(int argc, char *argv[]) 46500Sstevel@tonic-gate { 46510Sstevel@tonic-gate char line[ZONENAME_MAX + 128]; /* Enough for "Are you sure ..." */ 46521867Sgjelinek char rootpath[MAXPATHLEN], zonepath[MAXPATHLEN]; 46530Sstevel@tonic-gate boolean_t force = B_FALSE; 46540Sstevel@tonic-gate int lockfd, answer; 46550Sstevel@tonic-gate int err, arg; 46560Sstevel@tonic-gate 4657766Scarlsonj if (zonecfg_in_alt_root()) { 4658766Scarlsonj zerror(gettext("cannot uninstall zone in alternate root")); 4659766Scarlsonj return (Z_ERR); 4660766Scarlsonj } 4661766Scarlsonj 46620Sstevel@tonic-gate optind = 0; 46630Sstevel@tonic-gate while ((arg = getopt(argc, argv, "?F")) != EOF) { 46640Sstevel@tonic-gate switch (arg) { 46650Sstevel@tonic-gate case '?': 46660Sstevel@tonic-gate sub_usage(SHELP_UNINSTALL, CMD_UNINSTALL); 46670Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 46680Sstevel@tonic-gate case 'F': 46690Sstevel@tonic-gate force = B_TRUE; 46700Sstevel@tonic-gate break; 46710Sstevel@tonic-gate default: 46720Sstevel@tonic-gate sub_usage(SHELP_UNINSTALL, CMD_UNINSTALL); 46730Sstevel@tonic-gate return (Z_USAGE); 46740Sstevel@tonic-gate } 46750Sstevel@tonic-gate } 46760Sstevel@tonic-gate if (argc > optind) { 46770Sstevel@tonic-gate sub_usage(SHELP_UNINSTALL, CMD_UNINSTALL); 46780Sstevel@tonic-gate return (Z_USAGE); 46790Sstevel@tonic-gate } 46800Sstevel@tonic-gate 46812712Snn35248 if (sanity_check(target_zone, CMD_UNINSTALL, B_FALSE, B_TRUE, B_FALSE) 46822712Snn35248 != Z_OK) 46830Sstevel@tonic-gate return (Z_ERR); 46840Sstevel@tonic-gate 46850Sstevel@tonic-gate if (!force) { 46860Sstevel@tonic-gate (void) snprintf(line, sizeof (line), 46870Sstevel@tonic-gate gettext("Are you sure you want to %s zone %s"), 46880Sstevel@tonic-gate cmd_to_str(CMD_UNINSTALL), target_zone); 46890Sstevel@tonic-gate if ((answer = ask_yesno(B_FALSE, line)) == 0) { 46900Sstevel@tonic-gate return (Z_OK); 46910Sstevel@tonic-gate } else if (answer == -1) { 46920Sstevel@tonic-gate zerror(gettext("Input not from terminal and -F " 46930Sstevel@tonic-gate "not specified: %s not done."), 46940Sstevel@tonic-gate cmd_to_str(CMD_UNINSTALL)); 46950Sstevel@tonic-gate return (Z_ERR); 46960Sstevel@tonic-gate } 46970Sstevel@tonic-gate } 46980Sstevel@tonic-gate 46991867Sgjelinek if ((err = zone_get_zonepath(target_zone, zonepath, 47001867Sgjelinek sizeof (zonepath))) != Z_OK) { 47010Sstevel@tonic-gate errno = err; 47020Sstevel@tonic-gate zperror2(target_zone, gettext("could not get zone path")); 47030Sstevel@tonic-gate return (Z_ERR); 47040Sstevel@tonic-gate } 47050Sstevel@tonic-gate if ((err = zone_get_rootpath(target_zone, rootpath, 47060Sstevel@tonic-gate sizeof (rootpath))) != Z_OK) { 47070Sstevel@tonic-gate errno = err; 47080Sstevel@tonic-gate zperror2(target_zone, gettext("could not get root path")); 47090Sstevel@tonic-gate return (Z_ERR); 47100Sstevel@tonic-gate } 47110Sstevel@tonic-gate 47120Sstevel@tonic-gate /* 47130Sstevel@tonic-gate * If there seems to be a zoneadmd running for this zone, call it 47140Sstevel@tonic-gate * to tell it that an uninstall is happening; if all goes well it 47150Sstevel@tonic-gate * will then shut itself down. 47160Sstevel@tonic-gate */ 47170Sstevel@tonic-gate if (ping_zoneadmd(target_zone) == Z_OK) { 47180Sstevel@tonic-gate zone_cmd_arg_t zarg; 47190Sstevel@tonic-gate zarg.cmd = Z_NOTE_UNINSTALLING; 47200Sstevel@tonic-gate /* we don't care too much if this fails... just plow on */ 47210Sstevel@tonic-gate (void) call_zoneadmd(target_zone, &zarg); 47220Sstevel@tonic-gate } 47230Sstevel@tonic-gate 47240Sstevel@tonic-gate if (grab_lock_file(target_zone, &lockfd) != Z_OK) { 47250Sstevel@tonic-gate zerror(gettext("another %s may have an operation in progress."), 47261300Sgjelinek "zoneadm"); 47270Sstevel@tonic-gate return (Z_ERR); 47280Sstevel@tonic-gate } 47290Sstevel@tonic-gate 47300Sstevel@tonic-gate /* Don't uninstall the zone if anything is mounted there */ 47310Sstevel@tonic-gate err = zonecfg_find_mounts(rootpath, NULL, NULL); 47320Sstevel@tonic-gate if (err) { 47331867Sgjelinek zerror(gettext("These file systems are mounted on " 47341645Scomay "subdirectories of %s.\n"), rootpath); 47350Sstevel@tonic-gate (void) zonecfg_find_mounts(rootpath, zfm_print, NULL); 47360Sstevel@tonic-gate return (Z_ERR); 47370Sstevel@tonic-gate } 47380Sstevel@tonic-gate 47390Sstevel@tonic-gate err = zone_set_state(target_zone, ZONE_STATE_INCOMPLETE); 47400Sstevel@tonic-gate if (err != Z_OK) { 47410Sstevel@tonic-gate errno = err; 47420Sstevel@tonic-gate zperror2(target_zone, gettext("could not set state")); 47430Sstevel@tonic-gate goto bad; 47440Sstevel@tonic-gate } 47450Sstevel@tonic-gate 47461867Sgjelinek if ((err = cleanup_zonepath(zonepath, B_FALSE)) != Z_OK) { 47471867Sgjelinek errno = err; 47481867Sgjelinek zperror2(target_zone, gettext("cleaning up zonepath failed")); 47490Sstevel@tonic-gate goto bad; 47501867Sgjelinek } 47511867Sgjelinek 47520Sstevel@tonic-gate err = zone_set_state(target_zone, ZONE_STATE_CONFIGURED); 47530Sstevel@tonic-gate if (err != Z_OK) { 47540Sstevel@tonic-gate errno = err; 47550Sstevel@tonic-gate zperror2(target_zone, gettext("could not reset state")); 47560Sstevel@tonic-gate } 47570Sstevel@tonic-gate bad: 47580Sstevel@tonic-gate release_lock_file(lockfd); 47590Sstevel@tonic-gate return (err); 47600Sstevel@tonic-gate } 47610Sstevel@tonic-gate 4762766Scarlsonj /* ARGSUSED */ 4763766Scarlsonj static int 4764766Scarlsonj mount_func(int argc, char *argv[]) 4765766Scarlsonj { 4766766Scarlsonj zone_cmd_arg_t zarg; 47672712Snn35248 boolean_t force = B_FALSE; 47682712Snn35248 int arg; 47692712Snn35248 47702712Snn35248 /* 47712712Snn35248 * The only supported subargument to the "mount" subcommand is 47722712Snn35248 * "-f", which forces us to mount a zone in the INCOMPLETE state. 47732712Snn35248 */ 47742712Snn35248 optind = 0; 47752712Snn35248 if ((arg = getopt(argc, argv, "f")) != EOF) { 47762712Snn35248 switch (arg) { 47772712Snn35248 case 'f': 47782712Snn35248 force = B_TRUE; 47792712Snn35248 break; 47802712Snn35248 default: 47812712Snn35248 return (Z_USAGE); 47822712Snn35248 } 47832712Snn35248 } 47842712Snn35248 if (argc > optind) 4785766Scarlsonj return (Z_USAGE); 47862712Snn35248 47872712Snn35248 if (sanity_check(target_zone, CMD_MOUNT, B_FALSE, B_FALSE, force) 47882712Snn35248 != Z_OK) 4789766Scarlsonj return (Z_ERR); 4790766Scarlsonj if (verify_details(CMD_MOUNT) != Z_OK) 4791766Scarlsonj return (Z_ERR); 4792766Scarlsonj 47932712Snn35248 zarg.cmd = force ? Z_FORCEMOUNT : Z_MOUNT; 4794766Scarlsonj if (call_zoneadmd(target_zone, &zarg) != 0) { 4795766Scarlsonj zerror(gettext("call to %s failed"), "zoneadmd"); 4796766Scarlsonj return (Z_ERR); 4797766Scarlsonj } 4798766Scarlsonj return (Z_OK); 4799766Scarlsonj } 4800766Scarlsonj 4801766Scarlsonj /* ARGSUSED */ 4802766Scarlsonj static int 4803766Scarlsonj unmount_func(int argc, char *argv[]) 4804766Scarlsonj { 4805766Scarlsonj zone_cmd_arg_t zarg; 4806766Scarlsonj 4807766Scarlsonj if (argc > 0) 4808766Scarlsonj return (Z_USAGE); 48092712Snn35248 if (sanity_check(target_zone, CMD_UNMOUNT, B_FALSE, B_FALSE, B_FALSE) 48102712Snn35248 != Z_OK) 4811766Scarlsonj return (Z_ERR); 4812766Scarlsonj 4813766Scarlsonj zarg.cmd = Z_UNMOUNT; 4814766Scarlsonj if (call_zoneadmd(target_zone, &zarg) != 0) { 4815766Scarlsonj zerror(gettext("call to %s failed"), "zoneadmd"); 4816766Scarlsonj return (Z_ERR); 4817766Scarlsonj } 4818766Scarlsonj return (Z_OK); 4819766Scarlsonj } 4820766Scarlsonj 48210Sstevel@tonic-gate static int 48222303Scarlsonj mark_func(int argc, char *argv[]) 48232303Scarlsonj { 48242303Scarlsonj int err, lockfd; 48252303Scarlsonj 48262303Scarlsonj if (argc != 1 || strcmp(argv[0], "incomplete") != 0) 48272303Scarlsonj return (Z_USAGE); 48282712Snn35248 if (sanity_check(target_zone, CMD_MARK, B_FALSE, B_FALSE, B_FALSE) 48292712Snn35248 != Z_OK) 48302303Scarlsonj return (Z_ERR); 48312303Scarlsonj 48322303Scarlsonj if (grab_lock_file(target_zone, &lockfd) != Z_OK) { 48332303Scarlsonj zerror(gettext("another %s may have an operation in progress."), 48342303Scarlsonj "zoneadm"); 48352303Scarlsonj return (Z_ERR); 48362303Scarlsonj } 48372303Scarlsonj 48382303Scarlsonj err = zone_set_state(target_zone, ZONE_STATE_INCOMPLETE); 48392303Scarlsonj if (err != Z_OK) { 48402303Scarlsonj errno = err; 48412303Scarlsonj zperror2(target_zone, gettext("could not set state")); 48422303Scarlsonj } 48432303Scarlsonj release_lock_file(lockfd); 48442303Scarlsonj 48452303Scarlsonj return (err); 48462303Scarlsonj } 48472303Scarlsonj 48482303Scarlsonj static int 48490Sstevel@tonic-gate help_func(int argc, char *argv[]) 48500Sstevel@tonic-gate { 48510Sstevel@tonic-gate int arg, cmd_num; 48520Sstevel@tonic-gate 48530Sstevel@tonic-gate if (argc == 0) { 48540Sstevel@tonic-gate (void) usage(B_TRUE); 48550Sstevel@tonic-gate return (Z_OK); 48560Sstevel@tonic-gate } 48570Sstevel@tonic-gate optind = 0; 48580Sstevel@tonic-gate if ((arg = getopt(argc, argv, "?")) != EOF) { 48590Sstevel@tonic-gate switch (arg) { 48600Sstevel@tonic-gate case '?': 48610Sstevel@tonic-gate sub_usage(SHELP_HELP, CMD_HELP); 48620Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 48630Sstevel@tonic-gate default: 48640Sstevel@tonic-gate sub_usage(SHELP_HELP, CMD_HELP); 48650Sstevel@tonic-gate return (Z_USAGE); 48660Sstevel@tonic-gate } 48670Sstevel@tonic-gate } 48680Sstevel@tonic-gate while (optind < argc) { 4869988Scarlsonj /* Private commands have NULL short_usage; omit them */ 4870988Scarlsonj if ((cmd_num = cmd_match(argv[optind])) < 0 || 4871988Scarlsonj cmdtab[cmd_num].short_usage == NULL) { 48720Sstevel@tonic-gate sub_usage(SHELP_HELP, CMD_HELP); 48730Sstevel@tonic-gate return (Z_USAGE); 48740Sstevel@tonic-gate } 48750Sstevel@tonic-gate sub_usage(cmdtab[cmd_num].short_usage, cmd_num); 48760Sstevel@tonic-gate optind++; 48770Sstevel@tonic-gate } 48780Sstevel@tonic-gate return (Z_OK); 48790Sstevel@tonic-gate } 48800Sstevel@tonic-gate 48810Sstevel@tonic-gate /* 48820Sstevel@tonic-gate * Returns: CMD_MIN thru CMD_MAX on success, -1 on error 48830Sstevel@tonic-gate */ 48840Sstevel@tonic-gate 48850Sstevel@tonic-gate static int 48860Sstevel@tonic-gate cmd_match(char *cmd) 48870Sstevel@tonic-gate { 48880Sstevel@tonic-gate int i; 48890Sstevel@tonic-gate 48900Sstevel@tonic-gate for (i = CMD_MIN; i <= CMD_MAX; i++) { 48910Sstevel@tonic-gate /* return only if there is an exact match */ 48920Sstevel@tonic-gate if (strcmp(cmd, cmdtab[i].cmd_name) == 0) 48930Sstevel@tonic-gate return (cmdtab[i].cmd_num); 48940Sstevel@tonic-gate } 48950Sstevel@tonic-gate return (-1); 48960Sstevel@tonic-gate } 48970Sstevel@tonic-gate 48980Sstevel@tonic-gate static int 48990Sstevel@tonic-gate parse_and_run(int argc, char *argv[]) 49000Sstevel@tonic-gate { 49010Sstevel@tonic-gate int i = cmd_match(argv[0]); 49020Sstevel@tonic-gate 49030Sstevel@tonic-gate if (i < 0) 49040Sstevel@tonic-gate return (usage(B_FALSE)); 49050Sstevel@tonic-gate return (cmdtab[i].handler(argc - 1, &(argv[1]))); 49060Sstevel@tonic-gate } 49070Sstevel@tonic-gate 49080Sstevel@tonic-gate static char * 49090Sstevel@tonic-gate get_execbasename(char *execfullname) 49100Sstevel@tonic-gate { 49110Sstevel@tonic-gate char *last_slash, *execbasename; 49120Sstevel@tonic-gate 49130Sstevel@tonic-gate /* guard against '/' at end of command invocation */ 49140Sstevel@tonic-gate for (;;) { 49150Sstevel@tonic-gate last_slash = strrchr(execfullname, '/'); 49160Sstevel@tonic-gate if (last_slash == NULL) { 49170Sstevel@tonic-gate execbasename = execfullname; 49180Sstevel@tonic-gate break; 49190Sstevel@tonic-gate } else { 49200Sstevel@tonic-gate execbasename = last_slash + 1; 49210Sstevel@tonic-gate if (*execbasename == '\0') { 49220Sstevel@tonic-gate *last_slash = '\0'; 49230Sstevel@tonic-gate continue; 49240Sstevel@tonic-gate } 49250Sstevel@tonic-gate break; 49260Sstevel@tonic-gate } 49270Sstevel@tonic-gate } 49280Sstevel@tonic-gate return (execbasename); 49290Sstevel@tonic-gate } 49300Sstevel@tonic-gate 49310Sstevel@tonic-gate int 49320Sstevel@tonic-gate main(int argc, char **argv) 49330Sstevel@tonic-gate { 49340Sstevel@tonic-gate int arg; 49350Sstevel@tonic-gate zoneid_t zid; 4936766Scarlsonj struct stat st; 49372712Snn35248 char *zone_lock_env; 49382712Snn35248 int err; 49390Sstevel@tonic-gate 49400Sstevel@tonic-gate if ((locale = setlocale(LC_ALL, "")) == NULL) 49410Sstevel@tonic-gate locale = "C"; 49420Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN); 49430Sstevel@tonic-gate setbuf(stdout, NULL); 49440Sstevel@tonic-gate (void) sigset(SIGHUP, SIG_IGN); 49450Sstevel@tonic-gate execname = get_execbasename(argv[0]); 49460Sstevel@tonic-gate target_zone = NULL; 49470Sstevel@tonic-gate if (chdir("/") != 0) { 49480Sstevel@tonic-gate zerror(gettext("could not change directory to /.")); 49490Sstevel@tonic-gate exit(Z_ERR); 49500Sstevel@tonic-gate } 49510Sstevel@tonic-gate 49522082Seschrock if (init_zfs() != Z_OK) 49532082Seschrock exit(Z_ERR); 49542082Seschrock 49552303Scarlsonj while ((arg = getopt(argc, argv, "?u:z:R:")) != EOF) { 49560Sstevel@tonic-gate switch (arg) { 49570Sstevel@tonic-gate case '?': 49580Sstevel@tonic-gate return (usage(B_TRUE)); 49592303Scarlsonj case 'u': 49602303Scarlsonj target_uuid = optarg; 49612303Scarlsonj break; 49620Sstevel@tonic-gate case 'z': 49630Sstevel@tonic-gate target_zone = optarg; 49640Sstevel@tonic-gate break; 4965766Scarlsonj case 'R': /* private option for admin/install use */ 4966766Scarlsonj if (*optarg != '/') { 4967766Scarlsonj zerror(gettext("root path must be absolute.")); 4968766Scarlsonj exit(Z_ERR); 4969766Scarlsonj } 4970766Scarlsonj if (stat(optarg, &st) == -1 || !S_ISDIR(st.st_mode)) { 4971766Scarlsonj zerror( 4972766Scarlsonj gettext("root path must be a directory.")); 4973766Scarlsonj exit(Z_ERR); 4974766Scarlsonj } 4975766Scarlsonj zonecfg_set_root(optarg); 4976766Scarlsonj break; 49770Sstevel@tonic-gate default: 49780Sstevel@tonic-gate return (usage(B_FALSE)); 49790Sstevel@tonic-gate } 49800Sstevel@tonic-gate } 49810Sstevel@tonic-gate 49820Sstevel@tonic-gate if (optind >= argc) 49830Sstevel@tonic-gate return (usage(B_FALSE)); 49842303Scarlsonj 49852303Scarlsonj if (target_uuid != NULL && *target_uuid != '\0') { 49862303Scarlsonj uuid_t uuid; 49872303Scarlsonj static char newtarget[ZONENAME_MAX]; 49882303Scarlsonj 49892303Scarlsonj if (uuid_parse(target_uuid, uuid) == -1) { 49902303Scarlsonj zerror(gettext("illegal UUID value specified")); 49912303Scarlsonj exit(Z_ERR); 49922303Scarlsonj } 49932303Scarlsonj if (zonecfg_get_name_by_uuid(uuid, newtarget, 49942303Scarlsonj sizeof (newtarget)) == Z_OK) 49952303Scarlsonj target_zone = newtarget; 49962303Scarlsonj } 49972303Scarlsonj 49980Sstevel@tonic-gate if (target_zone != NULL && zone_get_id(target_zone, &zid) != 0) { 49990Sstevel@tonic-gate errno = Z_NO_ZONE; 50000Sstevel@tonic-gate zperror(target_zone, B_TRUE); 50010Sstevel@tonic-gate exit(Z_ERR); 50020Sstevel@tonic-gate } 50032712Snn35248 50042712Snn35248 /* 50052712Snn35248 * See if we have inherited the right to manipulate this zone from 50062712Snn35248 * a zoneadm instance in our ancestry. If so, set zone_lock_cnt to 50072712Snn35248 * indicate it. If not, make that explicit in our environment. 50082712Snn35248 */ 50092712Snn35248 zone_lock_env = getenv(LOCK_ENV_VAR); 50102712Snn35248 if (zone_lock_env == NULL) { 50112712Snn35248 if (putenv(zoneadm_lock_not_held) != 0) { 50122712Snn35248 zperror(target_zone, B_TRUE); 50132712Snn35248 exit(Z_ERR); 50142712Snn35248 } 50152712Snn35248 } else { 50162712Snn35248 zoneadm_is_nested = B_TRUE; 50172712Snn35248 if (atoi(zone_lock_env) == 1) 50182712Snn35248 zone_lock_cnt = 1; 50192712Snn35248 } 50202712Snn35248 50212712Snn35248 /* 50222712Snn35248 * If we are going to be operating on a single zone, retrieve its 50232712Snn35248 * brand type and determine whether it is native or not. 50242712Snn35248 */ 50252712Snn35248 if ((target_zone != NULL) && 50262712Snn35248 (strcmp(target_zone, GLOBAL_ZONENAME) != NULL)) { 50272712Snn35248 if (zone_get_brand(target_zone, target_brand, 50282712Snn35248 sizeof (target_brand)) != Z_OK) { 50292712Snn35248 zerror(gettext("missing or invalid brand")); 50302712Snn35248 exit(Z_ERR); 50312712Snn35248 } 50322712Snn35248 is_native_zone = (strcmp(target_brand, NATIVE_BRAND_NAME) == 0); 50332712Snn35248 } 50342712Snn35248 50352712Snn35248 err = parse_and_run(argc - optind, &argv[optind]); 50362712Snn35248 50372712Snn35248 return (err); 50380Sstevel@tonic-gate } 5039