17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5ee519a1fSgjelinek * Common Development and Distribution License (the "License"). 6ee519a1fSgjelinek * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 217e362f58Scomay 227c478bd9Sstevel@tonic-gate /* 23865e09a4Sgjelinek * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 247c478bd9Sstevel@tonic-gate * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 287c478bd9Sstevel@tonic-gate 297c478bd9Sstevel@tonic-gate /* 307c478bd9Sstevel@tonic-gate * zoneadm is a command interpreter for zone administration. It is all in 317c478bd9Sstevel@tonic-gate * C (i.e., no lex/yacc), and all the argument passing is argc/argv based. 327c478bd9Sstevel@tonic-gate * main() calls parse_and_run() which calls cmd_match(), then invokes the 337c478bd9Sstevel@tonic-gate * appropriate command's handler function. The rest of the program is the 347c478bd9Sstevel@tonic-gate * handler functions and their helper functions. 357c478bd9Sstevel@tonic-gate * 367c478bd9Sstevel@tonic-gate * Some of the helper functions are used largely to simplify I18N: reducing 377c478bd9Sstevel@tonic-gate * the need for translation notes. This is particularly true of many of 387c478bd9Sstevel@tonic-gate * the zerror() calls: doing e.g. zerror(gettext("%s failed"), "foo") rather 397c478bd9Sstevel@tonic-gate * than zerror(gettext("foo failed")) with a translation note indicating 407c478bd9Sstevel@tonic-gate * that "foo" need not be translated. 417c478bd9Sstevel@tonic-gate */ 427c478bd9Sstevel@tonic-gate 437c478bd9Sstevel@tonic-gate #include <stdio.h> 447c478bd9Sstevel@tonic-gate #include <errno.h> 457c478bd9Sstevel@tonic-gate #include <unistd.h> 467c478bd9Sstevel@tonic-gate #include <signal.h> 477c478bd9Sstevel@tonic-gate #include <stdarg.h> 487c478bd9Sstevel@tonic-gate #include <ctype.h> 497c478bd9Sstevel@tonic-gate #include <stdlib.h> 507c478bd9Sstevel@tonic-gate #include <string.h> 517c478bd9Sstevel@tonic-gate #include <wait.h> 527c478bd9Sstevel@tonic-gate #include <zone.h> 537c478bd9Sstevel@tonic-gate #include <priv.h> 547c478bd9Sstevel@tonic-gate #include <locale.h> 557c478bd9Sstevel@tonic-gate #include <libintl.h> 567c478bd9Sstevel@tonic-gate #include <libzonecfg.h> 577c478bd9Sstevel@tonic-gate #include <bsm/adt.h> 587c478bd9Sstevel@tonic-gate #include <sys/param.h> 597c478bd9Sstevel@tonic-gate #include <sys/types.h> 607c478bd9Sstevel@tonic-gate #include <sys/stat.h> 617c478bd9Sstevel@tonic-gate #include <sys/statvfs.h> 627c478bd9Sstevel@tonic-gate #include <assert.h> 637c478bd9Sstevel@tonic-gate #include <sys/sockio.h> 647c478bd9Sstevel@tonic-gate #include <sys/mntent.h> 657c478bd9Sstevel@tonic-gate #include <limits.h> 660b5de56dSgjelinek #include <dirent.h> 67555afedfScarlsonj #include <uuid/uuid.h> 687c478bd9Sstevel@tonic-gate 697c478bd9Sstevel@tonic-gate #include <fcntl.h> 707c478bd9Sstevel@tonic-gate #include <door.h> 717c478bd9Sstevel@tonic-gate #include <macros.h> 727c478bd9Sstevel@tonic-gate #include <libgen.h> 73865e09a4Sgjelinek #include <fnmatch.h> 74e7f3c547Sgjelinek #include <sys/modctl.h> 757c478bd9Sstevel@tonic-gate 767c478bd9Sstevel@tonic-gate #include <pool.h> 777c478bd9Sstevel@tonic-gate #include <sys/pool.h> 787c478bd9Sstevel@tonic-gate 790b5de56dSgjelinek #include "zoneadm.h" 800b5de56dSgjelinek 817c478bd9Sstevel@tonic-gate #define MAXARGS 8 827c478bd9Sstevel@tonic-gate 837c478bd9Sstevel@tonic-gate /* Reflects kernel zone entries */ 847c478bd9Sstevel@tonic-gate typedef struct zone_entry { 857c478bd9Sstevel@tonic-gate zoneid_t zid; 867c478bd9Sstevel@tonic-gate char zname[ZONENAME_MAX]; 877c478bd9Sstevel@tonic-gate char *zstate_str; 887c478bd9Sstevel@tonic-gate zone_state_t zstate_num; 897c478bd9Sstevel@tonic-gate char zroot[MAXPATHLEN]; 90555afedfScarlsonj char zuuid[UUID_PRINTABLE_STRING_LENGTH]; 917c478bd9Sstevel@tonic-gate } zone_entry_t; 927c478bd9Sstevel@tonic-gate 937c478bd9Sstevel@tonic-gate static zone_entry_t *zents; 947c478bd9Sstevel@tonic-gate static size_t nzents; 957c478bd9Sstevel@tonic-gate 961390a385Sgjelinek #define LOOPBACK_IF "lo0" 971390a385Sgjelinek #define SOCKET_AF(af) (((af) == AF_UNSPEC) ? AF_INET : (af)) 981390a385Sgjelinek 991390a385Sgjelinek struct net_if { 1001390a385Sgjelinek char *name; 1011390a385Sgjelinek int af; 1021390a385Sgjelinek }; 1031390a385Sgjelinek 1047c478bd9Sstevel@tonic-gate /* 0755 is the default directory mode. */ 1057c478bd9Sstevel@tonic-gate #define DEFAULT_DIR_MODE \ 1067c478bd9Sstevel@tonic-gate (S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) 1077c478bd9Sstevel@tonic-gate 1087c478bd9Sstevel@tonic-gate struct cmd { 1097c478bd9Sstevel@tonic-gate uint_t cmd_num; /* command number */ 1107c478bd9Sstevel@tonic-gate char *cmd_name; /* command name */ 1117c478bd9Sstevel@tonic-gate char *short_usage; /* short form help */ 1127c478bd9Sstevel@tonic-gate int (*handler)(int argc, char *argv[]); /* function to call */ 1137c478bd9Sstevel@tonic-gate 1147c478bd9Sstevel@tonic-gate }; 1157c478bd9Sstevel@tonic-gate 1167c478bd9Sstevel@tonic-gate #define SHELP_HELP "help" 1173f2f09c1Sdp #define SHELP_BOOT "boot [-- boot_arguments]" 1187c478bd9Sstevel@tonic-gate #define SHELP_HALT "halt" 1197c478bd9Sstevel@tonic-gate #define SHELP_READY "ready" 1203f2f09c1Sdp #define SHELP_REBOOT "reboot [-- boot_arguments]" 1217c478bd9Sstevel@tonic-gate #define SHELP_LIST "list [-cipv]" 1227c478bd9Sstevel@tonic-gate #define SHELP_VERIFY "verify" 1230b5de56dSgjelinek #define SHELP_INSTALL "install [-x nodataset]" 1247c478bd9Sstevel@tonic-gate #define SHELP_UNINSTALL "uninstall [-F]" 1250b5de56dSgjelinek #define SHELP_CLONE "clone [-m method] [-s <ZFS snapshot>] zonename" 126865e09a4Sgjelinek #define SHELP_MOVE "move zonepath" 1278cd327d5Sgjelinek #define SHELP_DETACH "detach [-n]" 1288cd327d5Sgjelinek #define SHELP_ATTACH "attach [-F] [-n <path>]" 129555afedfScarlsonj #define SHELP_MARK "mark incomplete" 1307c478bd9Sstevel@tonic-gate 1317c478bd9Sstevel@tonic-gate static int help_func(int argc, char *argv[]); 1327c478bd9Sstevel@tonic-gate static int ready_func(int argc, char *argv[]); 1337c478bd9Sstevel@tonic-gate static int boot_func(int argc, char *argv[]); 1347c478bd9Sstevel@tonic-gate static int halt_func(int argc, char *argv[]); 1357c478bd9Sstevel@tonic-gate static int reboot_func(int argc, char *argv[]); 1367c478bd9Sstevel@tonic-gate static int list_func(int argc, char *argv[]); 1377c478bd9Sstevel@tonic-gate static int verify_func(int argc, char *argv[]); 1387c478bd9Sstevel@tonic-gate static int install_func(int argc, char *argv[]); 1397c478bd9Sstevel@tonic-gate static int uninstall_func(int argc, char *argv[]); 140108322fbScarlsonj static int mount_func(int argc, char *argv[]); 141108322fbScarlsonj static int unmount_func(int argc, char *argv[]); 142865e09a4Sgjelinek static int clone_func(int argc, char *argv[]); 143865e09a4Sgjelinek static int move_func(int argc, char *argv[]); 144ee519a1fSgjelinek static int detach_func(int argc, char *argv[]); 145ee519a1fSgjelinek static int attach_func(int argc, char *argv[]); 146555afedfScarlsonj static int mark_func(int argc, char *argv[]); 1477c478bd9Sstevel@tonic-gate static int sanity_check(char *zone, int cmd_num, boolean_t running, 1487c478bd9Sstevel@tonic-gate boolean_t unsafe_when_running); 1497c478bd9Sstevel@tonic-gate static int cmd_match(char *cmd); 1507c478bd9Sstevel@tonic-gate static int verify_details(int); 1517c478bd9Sstevel@tonic-gate 1527c478bd9Sstevel@tonic-gate static struct cmd cmdtab[] = { 1537c478bd9Sstevel@tonic-gate { CMD_HELP, "help", SHELP_HELP, help_func }, 1547c478bd9Sstevel@tonic-gate { CMD_BOOT, "boot", SHELP_BOOT, boot_func }, 1557c478bd9Sstevel@tonic-gate { CMD_HALT, "halt", SHELP_HALT, halt_func }, 1567c478bd9Sstevel@tonic-gate { CMD_READY, "ready", SHELP_READY, ready_func }, 1577c478bd9Sstevel@tonic-gate { CMD_REBOOT, "reboot", SHELP_REBOOT, reboot_func }, 1587c478bd9Sstevel@tonic-gate { CMD_LIST, "list", SHELP_LIST, list_func }, 1597c478bd9Sstevel@tonic-gate { CMD_VERIFY, "verify", SHELP_VERIFY, verify_func }, 1607c478bd9Sstevel@tonic-gate { CMD_INSTALL, "install", SHELP_INSTALL, install_func }, 1617c478bd9Sstevel@tonic-gate { CMD_UNINSTALL, "uninstall", SHELP_UNINSTALL, 162108322fbScarlsonj uninstall_func }, 163865e09a4Sgjelinek /* mount and unmount are private commands for admin/install */ 164108322fbScarlsonj { CMD_MOUNT, "mount", NULL, mount_func }, 165865e09a4Sgjelinek { CMD_UNMOUNT, "unmount", NULL, unmount_func }, 166865e09a4Sgjelinek { CMD_CLONE, "clone", SHELP_CLONE, clone_func }, 167ee519a1fSgjelinek { CMD_MOVE, "move", SHELP_MOVE, move_func }, 168ee519a1fSgjelinek { CMD_DETACH, "detach", SHELP_DETACH, detach_func }, 169555afedfScarlsonj { CMD_ATTACH, "attach", SHELP_ATTACH, attach_func }, 170555afedfScarlsonj { CMD_MARK, "mark", SHELP_MARK, mark_func } 1717c478bd9Sstevel@tonic-gate }; 1727c478bd9Sstevel@tonic-gate 1737c478bd9Sstevel@tonic-gate /* global variables */ 1747c478bd9Sstevel@tonic-gate 1757c478bd9Sstevel@tonic-gate /* set early in main(), never modified thereafter, used all over the place */ 1767c478bd9Sstevel@tonic-gate static char *execname; 1777c478bd9Sstevel@tonic-gate static char *locale; 1780b5de56dSgjelinek char *target_zone; 179555afedfScarlsonj static char *target_uuid; 1807c478bd9Sstevel@tonic-gate 1817c478bd9Sstevel@tonic-gate /* used in do_subproc() and signal handler */ 1827c478bd9Sstevel@tonic-gate static volatile boolean_t child_killed; 1837c478bd9Sstevel@tonic-gate 1840b5de56dSgjelinek char * 1857c478bd9Sstevel@tonic-gate cmd_to_str(int cmd_num) 1867c478bd9Sstevel@tonic-gate { 1877c478bd9Sstevel@tonic-gate assert(cmd_num >= CMD_MIN && cmd_num <= CMD_MAX); 1887c478bd9Sstevel@tonic-gate return (cmdtab[cmd_num].cmd_name); 1897c478bd9Sstevel@tonic-gate } 1907c478bd9Sstevel@tonic-gate 1917c478bd9Sstevel@tonic-gate /* This is a separate function because of gettext() wrapping. */ 1927c478bd9Sstevel@tonic-gate static char * 1937c478bd9Sstevel@tonic-gate long_help(int cmd_num) 1947c478bd9Sstevel@tonic-gate { 1957e362f58Scomay assert(cmd_num >= CMD_MIN && cmd_num <= CMD_MAX); 1967c478bd9Sstevel@tonic-gate switch (cmd_num) { 1977c478bd9Sstevel@tonic-gate case CMD_HELP: 1987c478bd9Sstevel@tonic-gate return (gettext("Print usage message.")); 1997c478bd9Sstevel@tonic-gate case CMD_BOOT: 2003f2f09c1Sdp return (gettext("Activates (boots) specified zone. See " 2013f2f09c1Sdp "zoneadm(1m) for valid boot\n\targuments.")); 2027c478bd9Sstevel@tonic-gate case CMD_HALT: 2039e518655Sgjelinek return (gettext("Halts specified zone, bypassing shutdown " 2049e518655Sgjelinek "scripts and removing runtime\n\tresources of the zone.")); 2057c478bd9Sstevel@tonic-gate case CMD_READY: 2069e518655Sgjelinek return (gettext("Prepares a zone for running applications but " 2079e518655Sgjelinek "does not start any user\n\tprocesses in the zone.")); 2087c478bd9Sstevel@tonic-gate case CMD_REBOOT: 2099e518655Sgjelinek return (gettext("Restarts the zone (equivalent to a halt / " 2103f2f09c1Sdp "boot sequence).\n\tFails if the zone is not active. " 2113f2f09c1Sdp "See zoneadm(1m) for valid boot\n\targuments.")); 2127c478bd9Sstevel@tonic-gate case CMD_LIST: 2137c478bd9Sstevel@tonic-gate return (gettext("Lists the current zones, or a " 2147c478bd9Sstevel@tonic-gate "specific zone if indicated. By default,\n\tall " 2157c478bd9Sstevel@tonic-gate "running zones are listed, though this can be " 2167c478bd9Sstevel@tonic-gate "expanded to all\n\tinstalled zones with the -i " 2177c478bd9Sstevel@tonic-gate "option or all configured zones with the\n\t-c " 218555afedfScarlsonj "option. When used with the general -z <zone> and/or -u " 219555afedfScarlsonj "<uuid-match>\n\toptions, lists only the specified " 220555afedfScarlsonj "matching zone, but lists it\n\tregardless of its state, " 221555afedfScarlsonj "and the -i and -c options are disallowed. The\n\t-v " 222555afedfScarlsonj "option can be used to display verbose information: zone " 223555afedfScarlsonj "name, id,\n\tcurrent state, root directory and options. " 224555afedfScarlsonj "The -p option can be used\n\tto request machine-parsable " 225555afedfScarlsonj "output. The -v and -p options are mutually\n\texclusive." 226555afedfScarlsonj " If neither -v nor -p is used, just the zone name is " 227555afedfScarlsonj "listed.")); 2287c478bd9Sstevel@tonic-gate case CMD_VERIFY: 2297c478bd9Sstevel@tonic-gate return (gettext("Check to make sure the configuration " 2307c478bd9Sstevel@tonic-gate "can safely be instantiated\n\ton the machine: " 2317c478bd9Sstevel@tonic-gate "physical network interfaces exist, etc.")); 2327c478bd9Sstevel@tonic-gate case CMD_INSTALL: 2330b5de56dSgjelinek return (gettext("Install the configuration on to the system. " 2340b5de56dSgjelinek "The -x nodataset option\n\tcan be used to prevent the " 2350b5de56dSgjelinek "creation of a new ZFS file system for the\n\tzone " 2360b5de56dSgjelinek "(assuming the zonepath is within a ZFS file system).")); 2377c478bd9Sstevel@tonic-gate case CMD_UNINSTALL: 2389e518655Sgjelinek return (gettext("Uninstall the configuration from the system. " 2399e518655Sgjelinek "The -F flag can be used\n\tto force the action.")); 240865e09a4Sgjelinek case CMD_CLONE: 2410b5de56dSgjelinek return (gettext("Clone the installation of another zone. " 2420b5de56dSgjelinek "The -m option can be used to\n\tspecify 'copy' which " 2430b5de56dSgjelinek "forces a copy of the source zone. The -s option\n\t" 2440b5de56dSgjelinek "can be used to specify the name of a ZFS snapshot " 2450b5de56dSgjelinek "that was taken from\n\ta previous clone command. The " 2460b5de56dSgjelinek "snapshot will be used as the source\n\tinstead of " 2470b5de56dSgjelinek "creating a new ZFS snapshot.")); 248865e09a4Sgjelinek case CMD_MOVE: 249865e09a4Sgjelinek return (gettext("Move the zone to a new zonepath.")); 2509e518655Sgjelinek case CMD_DETACH: 2519e518655Sgjelinek return (gettext("Detach the zone from the system. The zone " 2529e518655Sgjelinek "state is changed to\n\t'configured' (but the files under " 2539e518655Sgjelinek "the zonepath are untouched).\n\tThe zone can subsequently " 2549e518655Sgjelinek "be attached, or can be moved to another\n\tsystem and " 2558cd327d5Sgjelinek "attached there. The -n option can be used to specify\n\t" 2568cd327d5Sgjelinek "'no-execute' mode. When -n is used, the information " 2578cd327d5Sgjelinek "needed to attach\n\tthe zone is sent to standard output " 2588cd327d5Sgjelinek "but the zone is not actually\n\tdetached.")); 2599e518655Sgjelinek case CMD_ATTACH: 2609e518655Sgjelinek return (gettext("Attach the zone to the system. The zone " 2619e518655Sgjelinek "state must be 'configured'\n\tprior to attach; upon " 2629e518655Sgjelinek "successful completion, the zone state will be\n\t" 2639e518655Sgjelinek "'installed'. The system software on the current " 2649e518655Sgjelinek "system must be\n\tcompatible with the software on the " 2659e518655Sgjelinek "zone's original system.\n\tSpecify -F to force the attach " 2668cd327d5Sgjelinek "and skip software compatibility tests.\n\tThe -n option " 2678cd327d5Sgjelinek "can be used to specify 'no-execute' mode. When -n is\n\t" 2688cd327d5Sgjelinek "used, the information needed to attach the zone is read " 2698cd327d5Sgjelinek "from the\n\tspecified path and the configuration is only " 2708cd327d5Sgjelinek "validated. The path can\n\tbe '-' to specify standard " 2718cd327d5Sgjelinek "input.")); 272555afedfScarlsonj case CMD_MARK: 273555afedfScarlsonj return (gettext("Set the state of the zone. This can be used " 274555afedfScarlsonj "to force the zone\n\tstate to 'incomplete' " 275555afedfScarlsonj "administratively if some activity has rendered\n\tthe " 276555afedfScarlsonj "zone permanently unusable. The only valid state that " 277555afedfScarlsonj "may be\n\tspecified is 'incomplete'.")); 278108322fbScarlsonj default: 279108322fbScarlsonj return (""); 2807c478bd9Sstevel@tonic-gate } 2817c478bd9Sstevel@tonic-gate /* NOTREACHED */ 2827e362f58Scomay return (NULL); 2837c478bd9Sstevel@tonic-gate } 2847c478bd9Sstevel@tonic-gate 2857c478bd9Sstevel@tonic-gate /* 2867c478bd9Sstevel@tonic-gate * Called with explicit B_TRUE when help is explicitly requested, B_FALSE for 2877c478bd9Sstevel@tonic-gate * unexpected errors. 2887c478bd9Sstevel@tonic-gate */ 2897c478bd9Sstevel@tonic-gate 2907c478bd9Sstevel@tonic-gate static int 2917c478bd9Sstevel@tonic-gate usage(boolean_t explicit) 2927c478bd9Sstevel@tonic-gate { 2937c478bd9Sstevel@tonic-gate int i; 2947c478bd9Sstevel@tonic-gate FILE *fd = explicit ? stdout : stderr; 2957c478bd9Sstevel@tonic-gate 2967c478bd9Sstevel@tonic-gate (void) fprintf(fd, "%s:\t%s help\n", gettext("usage"), execname); 297555afedfScarlsonj (void) fprintf(fd, "\t%s [-z <zone>] [-u <uuid-match>] list\n", 298555afedfScarlsonj execname); 299555afedfScarlsonj (void) fprintf(fd, "\t%s {-z <zone>|-u <uuid-match>} <%s>\n", execname, 3007c478bd9Sstevel@tonic-gate gettext("subcommand")); 3017c478bd9Sstevel@tonic-gate (void) fprintf(fd, "\n%s:\n\n", gettext("Subcommands")); 3027c478bd9Sstevel@tonic-gate for (i = CMD_MIN; i <= CMD_MAX; i++) { 303108322fbScarlsonj if (cmdtab[i].short_usage == NULL) 304108322fbScarlsonj continue; 3057c478bd9Sstevel@tonic-gate (void) fprintf(fd, "%s\n", cmdtab[i].short_usage); 3067c478bd9Sstevel@tonic-gate if (explicit) 3077c478bd9Sstevel@tonic-gate (void) fprintf(fd, "\t%s\n\n", long_help(i)); 3087c478bd9Sstevel@tonic-gate } 3097c478bd9Sstevel@tonic-gate if (!explicit) 3107c478bd9Sstevel@tonic-gate (void) fputs("\n", fd); 3117c478bd9Sstevel@tonic-gate return (Z_USAGE); 3127c478bd9Sstevel@tonic-gate } 3137c478bd9Sstevel@tonic-gate 3147c478bd9Sstevel@tonic-gate static void 3157c478bd9Sstevel@tonic-gate sub_usage(char *short_usage, int cmd_num) 3167c478bd9Sstevel@tonic-gate { 3177c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s:\t%s\n", gettext("usage"), short_usage); 3187c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "\t%s\n", long_help(cmd_num)); 3197c478bd9Sstevel@tonic-gate } 3207c478bd9Sstevel@tonic-gate 3217c478bd9Sstevel@tonic-gate /* 3227c478bd9Sstevel@tonic-gate * zperror() is like perror(3c) except that this also prints the executable 3237c478bd9Sstevel@tonic-gate * name at the start of the message, and takes a boolean indicating whether 3247c478bd9Sstevel@tonic-gate * to call libc'c strerror() or that from libzonecfg. 3257c478bd9Sstevel@tonic-gate */ 3267c478bd9Sstevel@tonic-gate 3270b5de56dSgjelinek void 3287c478bd9Sstevel@tonic-gate zperror(const char *str, boolean_t zonecfg_error) 3297c478bd9Sstevel@tonic-gate { 3307c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: %s: %s\n", execname, str, 3317c478bd9Sstevel@tonic-gate zonecfg_error ? zonecfg_strerror(errno) : strerror(errno)); 3327c478bd9Sstevel@tonic-gate } 3337c478bd9Sstevel@tonic-gate 3347c478bd9Sstevel@tonic-gate /* 3357c478bd9Sstevel@tonic-gate * zperror2() is very similar to zperror() above, except it also prints a 3367c478bd9Sstevel@tonic-gate * supplied zone name after the executable. 3377c478bd9Sstevel@tonic-gate * 3387c478bd9Sstevel@tonic-gate * All current consumers of this function want libzonecfg's strerror() rather 3397c478bd9Sstevel@tonic-gate * than libc's; if this ever changes, this function can be made more generic 3407c478bd9Sstevel@tonic-gate * like zperror() above. 3417c478bd9Sstevel@tonic-gate */ 3427c478bd9Sstevel@tonic-gate 3430b5de56dSgjelinek void 3447c478bd9Sstevel@tonic-gate zperror2(const char *zone, const char *str) 3457c478bd9Sstevel@tonic-gate { 3467c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: %s: %s: %s\n", execname, zone, str, 3477c478bd9Sstevel@tonic-gate zonecfg_strerror(errno)); 3487c478bd9Sstevel@tonic-gate } 3497c478bd9Sstevel@tonic-gate 3507c478bd9Sstevel@tonic-gate /* PRINTFLIKE1 */ 3510b5de56dSgjelinek void 3527c478bd9Sstevel@tonic-gate zerror(const char *fmt, ...) 3537c478bd9Sstevel@tonic-gate { 3547c478bd9Sstevel@tonic-gate va_list alist; 3557c478bd9Sstevel@tonic-gate 3567c478bd9Sstevel@tonic-gate va_start(alist, fmt); 3577c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: ", execname); 3587c478bd9Sstevel@tonic-gate if (target_zone != NULL) 3597c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "zone '%s': ", target_zone); 3607c478bd9Sstevel@tonic-gate (void) vfprintf(stderr, fmt, alist); 3617c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "\n"); 3627c478bd9Sstevel@tonic-gate va_end(alist); 3637c478bd9Sstevel@tonic-gate } 3647c478bd9Sstevel@tonic-gate 3657c478bd9Sstevel@tonic-gate static void * 3667c478bd9Sstevel@tonic-gate safe_calloc(size_t nelem, size_t elsize) 3677c478bd9Sstevel@tonic-gate { 3687c478bd9Sstevel@tonic-gate void *r = calloc(nelem, elsize); 3697c478bd9Sstevel@tonic-gate 3707c478bd9Sstevel@tonic-gate if (r == NULL) { 3717c478bd9Sstevel@tonic-gate zerror(gettext("failed to allocate %lu bytes: %s"), 3727c478bd9Sstevel@tonic-gate (ulong_t)nelem * elsize, strerror(errno)); 3737c478bd9Sstevel@tonic-gate exit(Z_ERR); 3747c478bd9Sstevel@tonic-gate } 3757c478bd9Sstevel@tonic-gate return (r); 3767c478bd9Sstevel@tonic-gate } 3777c478bd9Sstevel@tonic-gate 3787c478bd9Sstevel@tonic-gate static void 3797c478bd9Sstevel@tonic-gate zone_print(zone_entry_t *zent, boolean_t verbose, boolean_t parsable) 3807c478bd9Sstevel@tonic-gate { 3817c478bd9Sstevel@tonic-gate static boolean_t firsttime = B_TRUE; 3827c478bd9Sstevel@tonic-gate 3837c478bd9Sstevel@tonic-gate assert(!(verbose && parsable)); 3847c478bd9Sstevel@tonic-gate if (firsttime && verbose) { 3857c478bd9Sstevel@tonic-gate firsttime = B_FALSE; 3867c478bd9Sstevel@tonic-gate (void) printf("%*s %-16s %-14s %-30s\n", ZONEID_WIDTH, "ID", 3877c478bd9Sstevel@tonic-gate "NAME", "STATUS", "PATH"); 3887c478bd9Sstevel@tonic-gate } 3897c478bd9Sstevel@tonic-gate if (!verbose) { 390555afedfScarlsonj char *cp, *clim; 391555afedfScarlsonj 3927c478bd9Sstevel@tonic-gate if (!parsable) { 3937c478bd9Sstevel@tonic-gate (void) printf("%s\n", zent->zname); 3947c478bd9Sstevel@tonic-gate return; 3957c478bd9Sstevel@tonic-gate } 3967c478bd9Sstevel@tonic-gate if (zent->zid == ZONE_ID_UNDEFINED) 3977c478bd9Sstevel@tonic-gate (void) printf("-"); 3987c478bd9Sstevel@tonic-gate else 3997c478bd9Sstevel@tonic-gate (void) printf("%lu", zent->zid); 400555afedfScarlsonj (void) printf(":%s:%s:", zent->zname, zent->zstate_str); 401555afedfScarlsonj cp = zent->zroot; 402555afedfScarlsonj while ((clim = strchr(cp, ':')) != NULL) { 403555afedfScarlsonj (void) printf("%.*s\\:", clim - cp, cp); 404555afedfScarlsonj cp = clim + 1; 405555afedfScarlsonj } 406555afedfScarlsonj (void) printf("%s:%s\n", cp, zent->zuuid); 4077c478bd9Sstevel@tonic-gate return; 4087c478bd9Sstevel@tonic-gate } 4097c478bd9Sstevel@tonic-gate if (zent->zstate_str != NULL) { 4107c478bd9Sstevel@tonic-gate if (zent->zid == ZONE_ID_UNDEFINED) 4117c478bd9Sstevel@tonic-gate (void) printf("%*s", ZONEID_WIDTH, "-"); 4127c478bd9Sstevel@tonic-gate else 4137c478bd9Sstevel@tonic-gate (void) printf("%*lu", ZONEID_WIDTH, zent->zid); 4147c478bd9Sstevel@tonic-gate (void) printf(" %-16s %-14s %-30s\n", zent->zname, 4157c478bd9Sstevel@tonic-gate zent->zstate_str, zent->zroot); 4167c478bd9Sstevel@tonic-gate } 4177c478bd9Sstevel@tonic-gate } 4187c478bd9Sstevel@tonic-gate 4197c478bd9Sstevel@tonic-gate static int 420108322fbScarlsonj lookup_zone_info(const char *zone_name, zoneid_t zid, zone_entry_t *zent) 4217c478bd9Sstevel@tonic-gate { 42245916cd2Sjpk char root[MAXPATHLEN], *cp; 4237c478bd9Sstevel@tonic-gate int err; 424555afedfScarlsonj uuid_t uuid; 4257c478bd9Sstevel@tonic-gate 4267c478bd9Sstevel@tonic-gate (void) strlcpy(zent->zname, zone_name, sizeof (zent->zname)); 4277c478bd9Sstevel@tonic-gate (void) strlcpy(zent->zroot, "???", sizeof (zent->zroot)); 4287c478bd9Sstevel@tonic-gate zent->zstate_str = "???"; 4297c478bd9Sstevel@tonic-gate 430108322fbScarlsonj zent->zid = zid; 4317c478bd9Sstevel@tonic-gate 432555afedfScarlsonj if (zonecfg_get_uuid(zone_name, uuid) == Z_OK && 433555afedfScarlsonj !uuid_is_null(uuid)) 434555afedfScarlsonj uuid_unparse(uuid, zent->zuuid); 435555afedfScarlsonj else 436555afedfScarlsonj zent->zuuid[0] = '\0'; 437555afedfScarlsonj 43845916cd2Sjpk /* 43945916cd2Sjpk * For labeled zones which query the zone path of lower-level 44045916cd2Sjpk * zones, the path needs to be adjusted to drop the final 44145916cd2Sjpk * "/root" component. This adjusted path is then useful 44245916cd2Sjpk * for reading down any exported directories from the 44345916cd2Sjpk * lower-level zone. 44445916cd2Sjpk */ 44545916cd2Sjpk if (is_system_labeled() && zent->zid != ZONE_ID_UNDEFINED) { 44645916cd2Sjpk if (zone_getattr(zent->zid, ZONE_ATTR_ROOT, zent->zroot, 44745916cd2Sjpk sizeof (zent->zroot)) == -1) { 44845916cd2Sjpk zperror2(zent->zname, 44945916cd2Sjpk gettext("could not get zone path.")); 45045916cd2Sjpk return (Z_ERR); 45145916cd2Sjpk } 45245916cd2Sjpk cp = zent->zroot + strlen(zent->zroot) - 5; 45345916cd2Sjpk if (cp > zent->zroot && strcmp(cp, "/root") == 0) 45445916cd2Sjpk *cp = 0; 45545916cd2Sjpk } else { 45645916cd2Sjpk if ((err = zone_get_zonepath(zent->zname, root, 45745916cd2Sjpk sizeof (root))) != Z_OK) { 4587c478bd9Sstevel@tonic-gate errno = err; 45945916cd2Sjpk zperror2(zent->zname, 46045916cd2Sjpk gettext("could not get zone path.")); 4617c478bd9Sstevel@tonic-gate return (Z_ERR); 4627c478bd9Sstevel@tonic-gate } 4637c478bd9Sstevel@tonic-gate (void) strlcpy(zent->zroot, root, sizeof (zent->zroot)); 46445916cd2Sjpk } 4657c478bd9Sstevel@tonic-gate 4667c478bd9Sstevel@tonic-gate if ((err = zone_get_state(zent->zname, &zent->zstate_num)) != Z_OK) { 4677c478bd9Sstevel@tonic-gate errno = err; 4687c478bd9Sstevel@tonic-gate zperror2(zent->zname, gettext("could not get state")); 4697c478bd9Sstevel@tonic-gate return (Z_ERR); 4707c478bd9Sstevel@tonic-gate } 4717c478bd9Sstevel@tonic-gate zent->zstate_str = zone_state_str(zent->zstate_num); 4727c478bd9Sstevel@tonic-gate 4737c478bd9Sstevel@tonic-gate return (Z_OK); 4747c478bd9Sstevel@tonic-gate } 4757c478bd9Sstevel@tonic-gate 4767c478bd9Sstevel@tonic-gate /* 4777c478bd9Sstevel@tonic-gate * fetch_zents() calls zone_list(2) to find out how many zones are running 4787c478bd9Sstevel@tonic-gate * (which is stored in the global nzents), then calls zone_list(2) again 4797c478bd9Sstevel@tonic-gate * to fetch the list of running zones (stored in the global zents). This 4807c478bd9Sstevel@tonic-gate * function may be called multiple times, so if zents is already set, we 4817c478bd9Sstevel@tonic-gate * return immediately to save work. 4827c478bd9Sstevel@tonic-gate */ 4837c478bd9Sstevel@tonic-gate 4847c478bd9Sstevel@tonic-gate static int 485108322fbScarlsonj fetch_zents(void) 4867c478bd9Sstevel@tonic-gate { 4877c478bd9Sstevel@tonic-gate zoneid_t *zids = NULL; 4887c478bd9Sstevel@tonic-gate uint_t nzents_saved; 489108322fbScarlsonj int i, retv; 490108322fbScarlsonj FILE *fp; 491108322fbScarlsonj boolean_t inaltroot; 492108322fbScarlsonj zone_entry_t *zentp; 4937c478bd9Sstevel@tonic-gate 4947c478bd9Sstevel@tonic-gate if (nzents > 0) 4957c478bd9Sstevel@tonic-gate return (Z_OK); 4967c478bd9Sstevel@tonic-gate 4977c478bd9Sstevel@tonic-gate if (zone_list(NULL, &nzents) != 0) { 4987c478bd9Sstevel@tonic-gate zperror(gettext("failed to get zoneid list"), B_FALSE); 4997c478bd9Sstevel@tonic-gate return (Z_ERR); 5007c478bd9Sstevel@tonic-gate } 5017c478bd9Sstevel@tonic-gate 5027c478bd9Sstevel@tonic-gate again: 5037c478bd9Sstevel@tonic-gate if (nzents == 0) 5047c478bd9Sstevel@tonic-gate return (Z_OK); 5057c478bd9Sstevel@tonic-gate 5067c478bd9Sstevel@tonic-gate zids = safe_calloc(nzents, sizeof (zoneid_t)); 5077c478bd9Sstevel@tonic-gate nzents_saved = nzents; 5087c478bd9Sstevel@tonic-gate 5097c478bd9Sstevel@tonic-gate if (zone_list(zids, &nzents) != 0) { 5107c478bd9Sstevel@tonic-gate zperror(gettext("failed to get zone list"), B_FALSE); 5117c478bd9Sstevel@tonic-gate free(zids); 5127c478bd9Sstevel@tonic-gate return (Z_ERR); 5137c478bd9Sstevel@tonic-gate } 5147c478bd9Sstevel@tonic-gate if (nzents != nzents_saved) { 5157c478bd9Sstevel@tonic-gate /* list changed, try again */ 5167c478bd9Sstevel@tonic-gate free(zids); 5177c478bd9Sstevel@tonic-gate goto again; 5187c478bd9Sstevel@tonic-gate } 5197c478bd9Sstevel@tonic-gate 5207c478bd9Sstevel@tonic-gate zents = safe_calloc(nzents, sizeof (zone_entry_t)); 5217c478bd9Sstevel@tonic-gate 522108322fbScarlsonj inaltroot = zonecfg_in_alt_root(); 523108322fbScarlsonj if (inaltroot) 524108322fbScarlsonj fp = zonecfg_open_scratch("", B_FALSE); 525108322fbScarlsonj else 526108322fbScarlsonj fp = NULL; 527108322fbScarlsonj zentp = zents; 528108322fbScarlsonj retv = Z_OK; 5297c478bd9Sstevel@tonic-gate for (i = 0; i < nzents; i++) { 5307c478bd9Sstevel@tonic-gate char name[ZONENAME_MAX]; 531108322fbScarlsonj char altname[ZONENAME_MAX]; 5327c478bd9Sstevel@tonic-gate 533108322fbScarlsonj if (getzonenamebyid(zids[i], name, sizeof (name)) < 0) { 5347c478bd9Sstevel@tonic-gate zperror(gettext("failed to get zone name"), B_FALSE); 535108322fbScarlsonj retv = Z_ERR; 536108322fbScarlsonj continue; 5377c478bd9Sstevel@tonic-gate } 538108322fbScarlsonj if (zonecfg_is_scratch(name)) { 539108322fbScarlsonj /* Ignore scratch zones by default */ 540108322fbScarlsonj if (!inaltroot) 541108322fbScarlsonj continue; 542108322fbScarlsonj if (fp == NULL || 543108322fbScarlsonj zonecfg_reverse_scratch(fp, name, altname, 544108322fbScarlsonj sizeof (altname), NULL, 0) == -1) { 5455ee84fbdSgjelinek zerror(gettext("could not resolve scratch " 546108322fbScarlsonj "zone %s"), name); 547108322fbScarlsonj retv = Z_ERR; 548108322fbScarlsonj continue; 549108322fbScarlsonj } 550108322fbScarlsonj (void) strcpy(name, altname); 551108322fbScarlsonj } else { 552108322fbScarlsonj /* Ignore non-scratch when in an alternate root */ 553108322fbScarlsonj if (inaltroot && strcmp(name, GLOBAL_ZONENAME) != 0) 554108322fbScarlsonj continue; 555108322fbScarlsonj } 556108322fbScarlsonj if (lookup_zone_info(name, zids[i], zentp) != Z_OK) { 557108322fbScarlsonj zerror(gettext("failed to get zone data")); 558108322fbScarlsonj retv = Z_ERR; 559108322fbScarlsonj continue; 560108322fbScarlsonj } 561108322fbScarlsonj zentp++; 562108322fbScarlsonj } 563108322fbScarlsonj nzents = zentp - zents; 564108322fbScarlsonj if (fp != NULL) 565108322fbScarlsonj zonecfg_close_scratch(fp); 5667c478bd9Sstevel@tonic-gate 5677c478bd9Sstevel@tonic-gate free(zids); 568108322fbScarlsonj return (retv); 5697c478bd9Sstevel@tonic-gate } 5707c478bd9Sstevel@tonic-gate 571108322fbScarlsonj static int 5727c478bd9Sstevel@tonic-gate zone_print_list(zone_state_t min_state, boolean_t verbose, boolean_t parsable) 5737c478bd9Sstevel@tonic-gate { 5747c478bd9Sstevel@tonic-gate int i; 5757c478bd9Sstevel@tonic-gate zone_entry_t zent; 5767c478bd9Sstevel@tonic-gate FILE *cookie; 5777c478bd9Sstevel@tonic-gate char *name; 5787c478bd9Sstevel@tonic-gate 5797c478bd9Sstevel@tonic-gate /* 5807c478bd9Sstevel@tonic-gate * First get the list of running zones from the kernel and print them. 5817c478bd9Sstevel@tonic-gate * If that is all we need, then return. 5827c478bd9Sstevel@tonic-gate */ 583108322fbScarlsonj if ((i = fetch_zents()) != Z_OK) { 5847c478bd9Sstevel@tonic-gate /* 5857c478bd9Sstevel@tonic-gate * No need for error messages; fetch_zents() has already taken 5867c478bd9Sstevel@tonic-gate * care of this. 5877c478bd9Sstevel@tonic-gate */ 588108322fbScarlsonj return (i); 5897c478bd9Sstevel@tonic-gate } 590108322fbScarlsonj for (i = 0; i < nzents; i++) 5917c478bd9Sstevel@tonic-gate zone_print(&zents[i], verbose, parsable); 5927c478bd9Sstevel@tonic-gate if (min_state >= ZONE_STATE_RUNNING) 593108322fbScarlsonj return (Z_OK); 5947c478bd9Sstevel@tonic-gate /* 5957c478bd9Sstevel@tonic-gate * Next, get the full list of zones from the configuration, skipping 5967c478bd9Sstevel@tonic-gate * any we have already printed. 5977c478bd9Sstevel@tonic-gate */ 5987c478bd9Sstevel@tonic-gate cookie = setzoneent(); 5997c478bd9Sstevel@tonic-gate while ((name = getzoneent(cookie)) != NULL) { 6007c478bd9Sstevel@tonic-gate for (i = 0; i < nzents; i++) { 6017c478bd9Sstevel@tonic-gate if (strcmp(zents[i].zname, name) == 0) 6027c478bd9Sstevel@tonic-gate break; 6037c478bd9Sstevel@tonic-gate } 6047c478bd9Sstevel@tonic-gate if (i < nzents) { 6057c478bd9Sstevel@tonic-gate free(name); 6067c478bd9Sstevel@tonic-gate continue; 6077c478bd9Sstevel@tonic-gate } 608108322fbScarlsonj if (lookup_zone_info(name, ZONE_ID_UNDEFINED, &zent) != Z_OK) { 6097c478bd9Sstevel@tonic-gate free(name); 6107c478bd9Sstevel@tonic-gate continue; 6117c478bd9Sstevel@tonic-gate } 6127c478bd9Sstevel@tonic-gate free(name); 6137c478bd9Sstevel@tonic-gate if (zent.zstate_num >= min_state) 6147c478bd9Sstevel@tonic-gate zone_print(&zent, verbose, parsable); 6157c478bd9Sstevel@tonic-gate } 6167c478bd9Sstevel@tonic-gate endzoneent(cookie); 617108322fbScarlsonj return (Z_OK); 6187c478bd9Sstevel@tonic-gate } 6197c478bd9Sstevel@tonic-gate 6207c478bd9Sstevel@tonic-gate static zone_entry_t * 6217c478bd9Sstevel@tonic-gate lookup_running_zone(char *str) 6227c478bd9Sstevel@tonic-gate { 6237c478bd9Sstevel@tonic-gate zoneid_t zoneid; 6247c478bd9Sstevel@tonic-gate char *cp; 6257c478bd9Sstevel@tonic-gate int i; 6267c478bd9Sstevel@tonic-gate 6277c478bd9Sstevel@tonic-gate if (fetch_zents() != Z_OK) 6287c478bd9Sstevel@tonic-gate return (NULL); 6297c478bd9Sstevel@tonic-gate 6307c478bd9Sstevel@tonic-gate for (i = 0; i < nzents; i++) { 6317c478bd9Sstevel@tonic-gate if (strcmp(str, zents[i].zname) == 0) 6327c478bd9Sstevel@tonic-gate return (&zents[i]); 6337c478bd9Sstevel@tonic-gate } 6347c478bd9Sstevel@tonic-gate errno = 0; 6357c478bd9Sstevel@tonic-gate zoneid = strtol(str, &cp, 0); 6367c478bd9Sstevel@tonic-gate if (zoneid < MIN_ZONEID || zoneid > MAX_ZONEID || 6377c478bd9Sstevel@tonic-gate errno != 0 || *cp != '\0') 6387c478bd9Sstevel@tonic-gate return (NULL); 6397c478bd9Sstevel@tonic-gate for (i = 0; i < nzents; i++) { 6407c478bd9Sstevel@tonic-gate if (zoneid == zents[i].zid) 6417c478bd9Sstevel@tonic-gate return (&zents[i]); 6427c478bd9Sstevel@tonic-gate } 6437c478bd9Sstevel@tonic-gate return (NULL); 6447c478bd9Sstevel@tonic-gate } 6457c478bd9Sstevel@tonic-gate 6467c478bd9Sstevel@tonic-gate /* 6477c478bd9Sstevel@tonic-gate * Check a bit in a mode_t: if on is B_TRUE, that bit should be on; if 6487c478bd9Sstevel@tonic-gate * B_FALSE, it should be off. Return B_TRUE if the mode is bad (incorrect). 6497c478bd9Sstevel@tonic-gate */ 6507c478bd9Sstevel@tonic-gate static boolean_t 6517c478bd9Sstevel@tonic-gate bad_mode_bit(mode_t mode, mode_t bit, boolean_t on, char *file) 6527c478bd9Sstevel@tonic-gate { 6537c478bd9Sstevel@tonic-gate char *str; 6547c478bd9Sstevel@tonic-gate 6557c478bd9Sstevel@tonic-gate assert(bit == S_IRUSR || bit == S_IWUSR || bit == S_IXUSR || 6567c478bd9Sstevel@tonic-gate bit == S_IRGRP || bit == S_IWGRP || bit == S_IXGRP || 6577c478bd9Sstevel@tonic-gate bit == S_IROTH || bit == S_IWOTH || bit == S_IXOTH); 6587c478bd9Sstevel@tonic-gate /* 6597c478bd9Sstevel@tonic-gate * TRANSLATION_NOTE 6607c478bd9Sstevel@tonic-gate * The strings below will be used as part of a larger message, 6617c478bd9Sstevel@tonic-gate * either: 6627c478bd9Sstevel@tonic-gate * (file name) must be (owner|group|world) (read|writ|execut)able 6637c478bd9Sstevel@tonic-gate * or 6647c478bd9Sstevel@tonic-gate * (file name) must not be (owner|group|world) (read|writ|execut)able 6657c478bd9Sstevel@tonic-gate */ 6667c478bd9Sstevel@tonic-gate switch (bit) { 6677c478bd9Sstevel@tonic-gate case S_IRUSR: 6687c478bd9Sstevel@tonic-gate str = gettext("owner readable"); 6697c478bd9Sstevel@tonic-gate break; 6707c478bd9Sstevel@tonic-gate case S_IWUSR: 6717c478bd9Sstevel@tonic-gate str = gettext("owner writable"); 6727c478bd9Sstevel@tonic-gate break; 6737c478bd9Sstevel@tonic-gate case S_IXUSR: 6747c478bd9Sstevel@tonic-gate str = gettext("owner executable"); 6757c478bd9Sstevel@tonic-gate break; 6767c478bd9Sstevel@tonic-gate case S_IRGRP: 6777c478bd9Sstevel@tonic-gate str = gettext("group readable"); 6787c478bd9Sstevel@tonic-gate break; 6797c478bd9Sstevel@tonic-gate case S_IWGRP: 6807c478bd9Sstevel@tonic-gate str = gettext("group writable"); 6817c478bd9Sstevel@tonic-gate break; 6827c478bd9Sstevel@tonic-gate case S_IXGRP: 6837c478bd9Sstevel@tonic-gate str = gettext("group executable"); 6847c478bd9Sstevel@tonic-gate break; 6857c478bd9Sstevel@tonic-gate case S_IROTH: 6867c478bd9Sstevel@tonic-gate str = gettext("world readable"); 6877c478bd9Sstevel@tonic-gate break; 6887c478bd9Sstevel@tonic-gate case S_IWOTH: 6897c478bd9Sstevel@tonic-gate str = gettext("world writable"); 6907c478bd9Sstevel@tonic-gate break; 6917c478bd9Sstevel@tonic-gate case S_IXOTH: 6927c478bd9Sstevel@tonic-gate str = gettext("world executable"); 6937c478bd9Sstevel@tonic-gate break; 6947c478bd9Sstevel@tonic-gate } 6957c478bd9Sstevel@tonic-gate if ((mode & bit) == (on ? 0 : bit)) { 6967c478bd9Sstevel@tonic-gate /* 6977c478bd9Sstevel@tonic-gate * TRANSLATION_NOTE 6987c478bd9Sstevel@tonic-gate * The first parameter below is a file name; the second 6997c478bd9Sstevel@tonic-gate * is one of the "(owner|group|world) (read|writ|execut)able" 7007c478bd9Sstevel@tonic-gate * strings from above. 7017c478bd9Sstevel@tonic-gate */ 7027c478bd9Sstevel@tonic-gate /* 7037c478bd9Sstevel@tonic-gate * The code below could be simplified but not in a way 7047c478bd9Sstevel@tonic-gate * that would easily translate to non-English locales. 7057c478bd9Sstevel@tonic-gate */ 7067c478bd9Sstevel@tonic-gate if (on) { 7077c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s must be %s.\n"), 7087c478bd9Sstevel@tonic-gate file, str); 7097c478bd9Sstevel@tonic-gate } else { 7107c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s must not be %s.\n"), 7117c478bd9Sstevel@tonic-gate file, str); 7127c478bd9Sstevel@tonic-gate } 7137c478bd9Sstevel@tonic-gate return (B_TRUE); 7147c478bd9Sstevel@tonic-gate } 7157c478bd9Sstevel@tonic-gate return (B_FALSE); 7167c478bd9Sstevel@tonic-gate } 7177c478bd9Sstevel@tonic-gate 7187c478bd9Sstevel@tonic-gate /* 7197c478bd9Sstevel@tonic-gate * We want to make sure that no zone has its zone path as a child node 7207c478bd9Sstevel@tonic-gate * (in the directory sense) of any other. We do that by comparing this 7217c478bd9Sstevel@tonic-gate * zone's path to the path of all other (non-global) zones. The comparison 7227c478bd9Sstevel@tonic-gate * in each case is simple: add '/' to the end of the path, then do a 7237c478bd9Sstevel@tonic-gate * strncmp() of the two paths, using the length of the shorter one. 7247c478bd9Sstevel@tonic-gate */ 7257c478bd9Sstevel@tonic-gate 7267c478bd9Sstevel@tonic-gate static int 7277c478bd9Sstevel@tonic-gate crosscheck_zonepaths(char *path) 7287c478bd9Sstevel@tonic-gate { 7297c478bd9Sstevel@tonic-gate char rpath[MAXPATHLEN]; /* resolved path */ 7307c478bd9Sstevel@tonic-gate char path_copy[MAXPATHLEN]; /* copy of original path */ 7317c478bd9Sstevel@tonic-gate char rpath_copy[MAXPATHLEN]; /* copy of original rpath */ 7327c478bd9Sstevel@tonic-gate struct zoneent *ze; 7337c478bd9Sstevel@tonic-gate int res, err; 7347c478bd9Sstevel@tonic-gate FILE *cookie; 7357c478bd9Sstevel@tonic-gate 7367c478bd9Sstevel@tonic-gate cookie = setzoneent(); 7377c478bd9Sstevel@tonic-gate while ((ze = getzoneent_private(cookie)) != NULL) { 7387c478bd9Sstevel@tonic-gate /* Skip zones which are not installed. */ 7397c478bd9Sstevel@tonic-gate if (ze->zone_state < ZONE_STATE_INSTALLED) { 7407c478bd9Sstevel@tonic-gate free(ze); 7417c478bd9Sstevel@tonic-gate continue; 7427c478bd9Sstevel@tonic-gate } 7437c478bd9Sstevel@tonic-gate /* Skip the global zone and the current target zone. */ 7447c478bd9Sstevel@tonic-gate if (strcmp(ze->zone_name, GLOBAL_ZONENAME) == 0 || 7457c478bd9Sstevel@tonic-gate strcmp(ze->zone_name, target_zone) == 0) { 7467c478bd9Sstevel@tonic-gate free(ze); 7477c478bd9Sstevel@tonic-gate continue; 7487c478bd9Sstevel@tonic-gate } 7497c478bd9Sstevel@tonic-gate if (strlen(ze->zone_path) == 0) { 7507c478bd9Sstevel@tonic-gate /* old index file without path, fall back */ 7517c478bd9Sstevel@tonic-gate if ((err = zone_get_zonepath(ze->zone_name, 7527c478bd9Sstevel@tonic-gate ze->zone_path, sizeof (ze->zone_path))) != Z_OK) { 7537c478bd9Sstevel@tonic-gate errno = err; 7547c478bd9Sstevel@tonic-gate zperror2(ze->zone_name, 7557c478bd9Sstevel@tonic-gate gettext("could not get zone path")); 7567c478bd9Sstevel@tonic-gate free(ze); 7577c478bd9Sstevel@tonic-gate continue; 7587c478bd9Sstevel@tonic-gate } 7597c478bd9Sstevel@tonic-gate } 760108322fbScarlsonj (void) snprintf(path_copy, sizeof (path_copy), "%s%s", 761108322fbScarlsonj zonecfg_get_root(), ze->zone_path); 762108322fbScarlsonj res = resolvepath(path_copy, rpath, sizeof (rpath)); 7637c478bd9Sstevel@tonic-gate if (res == -1) { 7647c478bd9Sstevel@tonic-gate if (errno != ENOENT) { 765108322fbScarlsonj zperror(path_copy, B_FALSE); 7667c478bd9Sstevel@tonic-gate free(ze); 7677c478bd9Sstevel@tonic-gate return (Z_ERR); 7687c478bd9Sstevel@tonic-gate } 7697c478bd9Sstevel@tonic-gate (void) printf(gettext("WARNING: zone %s is installed, " 7707c478bd9Sstevel@tonic-gate "but its %s %s does not exist.\n"), ze->zone_name, 771108322fbScarlsonj "zonepath", path_copy); 7727c478bd9Sstevel@tonic-gate free(ze); 7737c478bd9Sstevel@tonic-gate continue; 7747c478bd9Sstevel@tonic-gate } 7757c478bd9Sstevel@tonic-gate rpath[res] = '\0'; 7767c478bd9Sstevel@tonic-gate (void) snprintf(path_copy, sizeof (path_copy), "%s/", path); 7777c478bd9Sstevel@tonic-gate (void) snprintf(rpath_copy, sizeof (rpath_copy), "%s/", rpath); 7787c478bd9Sstevel@tonic-gate if (strncmp(path_copy, rpath_copy, 7797c478bd9Sstevel@tonic-gate min(strlen(path_copy), strlen(rpath_copy))) == 0) { 7805ee84fbdSgjelinek /* 7815ee84fbdSgjelinek * TRANSLATION_NOTE 7825ee84fbdSgjelinek * zonepath is a literal that should not be translated. 7835ee84fbdSgjelinek */ 7847c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s zonepath (%s) and " 7857c478bd9Sstevel@tonic-gate "%s zonepath (%s) overlap.\n"), 7867c478bd9Sstevel@tonic-gate target_zone, path, ze->zone_name, rpath); 7877c478bd9Sstevel@tonic-gate free(ze); 7887c478bd9Sstevel@tonic-gate return (Z_ERR); 7897c478bd9Sstevel@tonic-gate } 7907c478bd9Sstevel@tonic-gate free(ze); 7917c478bd9Sstevel@tonic-gate } 7927c478bd9Sstevel@tonic-gate endzoneent(cookie); 7937c478bd9Sstevel@tonic-gate return (Z_OK); 7947c478bd9Sstevel@tonic-gate } 7957c478bd9Sstevel@tonic-gate 7967c478bd9Sstevel@tonic-gate static int 7977c478bd9Sstevel@tonic-gate validate_zonepath(char *path, int cmd_num) 7987c478bd9Sstevel@tonic-gate { 7997c478bd9Sstevel@tonic-gate int res; /* result of last library/system call */ 8007c478bd9Sstevel@tonic-gate boolean_t err = B_FALSE; /* have we run into an error? */ 8017c478bd9Sstevel@tonic-gate struct stat stbuf; 8023f2f09c1Sdp struct statvfs64 vfsbuf; 8037c478bd9Sstevel@tonic-gate char rpath[MAXPATHLEN]; /* resolved path */ 8047c478bd9Sstevel@tonic-gate char ppath[MAXPATHLEN]; /* parent path */ 8057c478bd9Sstevel@tonic-gate char rppath[MAXPATHLEN]; /* resolved parent path */ 8067c478bd9Sstevel@tonic-gate char rootpath[MAXPATHLEN]; /* root path */ 8077c478bd9Sstevel@tonic-gate zone_state_t state; 8087c478bd9Sstevel@tonic-gate 8097c478bd9Sstevel@tonic-gate if (path[0] != '/') { 8107c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 8117c478bd9Sstevel@tonic-gate gettext("%s is not an absolute path.\n"), path); 8127c478bd9Sstevel@tonic-gate return (Z_ERR); 8137c478bd9Sstevel@tonic-gate } 8147c478bd9Sstevel@tonic-gate if ((res = resolvepath(path, rpath, sizeof (rpath))) == -1) { 8157c478bd9Sstevel@tonic-gate if ((errno != ENOENT) || 816865e09a4Sgjelinek (cmd_num != CMD_VERIFY && cmd_num != CMD_INSTALL && 817865e09a4Sgjelinek cmd_num != CMD_CLONE && cmd_num != CMD_MOVE)) { 8187c478bd9Sstevel@tonic-gate zperror(path, B_FALSE); 8197c478bd9Sstevel@tonic-gate return (Z_ERR); 8207c478bd9Sstevel@tonic-gate } 8217c478bd9Sstevel@tonic-gate if (cmd_num == CMD_VERIFY) { 8225ee84fbdSgjelinek /* 8235ee84fbdSgjelinek * TRANSLATION_NOTE 8245ee84fbdSgjelinek * zoneadm is a literal that should not be translated. 8255ee84fbdSgjelinek */ 8267c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("WARNING: %s does not " 8275ee84fbdSgjelinek "exist, so it could not be verified.\nWhen " 8285ee84fbdSgjelinek "'zoneadm %s' is run, '%s' will try to create\n%s, " 8295ee84fbdSgjelinek "and '%s' will be tried again,\nbut the '%s' may " 8305ee84fbdSgjelinek "fail if:\nthe parent directory of %s is group- or " 8315ee84fbdSgjelinek "other-writable\nor\n%s overlaps with any other " 8327c478bd9Sstevel@tonic-gate "installed zones.\n"), path, 8337c478bd9Sstevel@tonic-gate cmd_to_str(CMD_INSTALL), cmd_to_str(CMD_INSTALL), 8347c478bd9Sstevel@tonic-gate path, cmd_to_str(CMD_VERIFY), 8357c478bd9Sstevel@tonic-gate cmd_to_str(CMD_VERIFY), path, path); 8367c478bd9Sstevel@tonic-gate return (Z_OK); 8377c478bd9Sstevel@tonic-gate } 8387c478bd9Sstevel@tonic-gate /* 8397c478bd9Sstevel@tonic-gate * The zonepath is supposed to be mode 700 but its 8407c478bd9Sstevel@tonic-gate * parent(s) 755. So use 755 on the mkdirp() then 8417c478bd9Sstevel@tonic-gate * chmod() the zonepath itself to 700. 8427c478bd9Sstevel@tonic-gate */ 8437c478bd9Sstevel@tonic-gate if (mkdirp(path, DEFAULT_DIR_MODE) < 0) { 8447c478bd9Sstevel@tonic-gate zperror(path, B_FALSE); 8457c478bd9Sstevel@tonic-gate return (Z_ERR); 8467c478bd9Sstevel@tonic-gate } 8477c478bd9Sstevel@tonic-gate /* 8487c478bd9Sstevel@tonic-gate * If the chmod() fails, report the error, but might 8497c478bd9Sstevel@tonic-gate * as well continue the verify procedure. 8507c478bd9Sstevel@tonic-gate */ 8517c478bd9Sstevel@tonic-gate if (chmod(path, S_IRWXU) != 0) 8527c478bd9Sstevel@tonic-gate zperror(path, B_FALSE); 8537c478bd9Sstevel@tonic-gate /* 8547c478bd9Sstevel@tonic-gate * Since the mkdir() succeeded, we should not have to 8557c478bd9Sstevel@tonic-gate * worry about a subsequent ENOENT, thus this should 8567c478bd9Sstevel@tonic-gate * only recurse once. 8577c478bd9Sstevel@tonic-gate */ 858865e09a4Sgjelinek return (validate_zonepath(path, cmd_num)); 8597c478bd9Sstevel@tonic-gate } 8607c478bd9Sstevel@tonic-gate rpath[res] = '\0'; 8617c478bd9Sstevel@tonic-gate if (strcmp(path, rpath) != 0) { 8627c478bd9Sstevel@tonic-gate errno = Z_RESOLVED_PATH; 8637c478bd9Sstevel@tonic-gate zperror(path, B_TRUE); 8647c478bd9Sstevel@tonic-gate return (Z_ERR); 8657c478bd9Sstevel@tonic-gate } 8667c478bd9Sstevel@tonic-gate if ((res = stat(rpath, &stbuf)) != 0) { 8677c478bd9Sstevel@tonic-gate zperror(rpath, B_FALSE); 8687c478bd9Sstevel@tonic-gate return (Z_ERR); 8697c478bd9Sstevel@tonic-gate } 8707c478bd9Sstevel@tonic-gate if (!S_ISDIR(stbuf.st_mode)) { 8717c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s is not a directory.\n"), 8727c478bd9Sstevel@tonic-gate rpath); 8737c478bd9Sstevel@tonic-gate return (Z_ERR); 8747c478bd9Sstevel@tonic-gate } 8757c478bd9Sstevel@tonic-gate if ((strcmp(stbuf.st_fstype, MNTTYPE_TMPFS) == 0) || 8767c478bd9Sstevel@tonic-gate (strcmp(stbuf.st_fstype, MNTTYPE_XMEMFS) == 0)) { 8777c478bd9Sstevel@tonic-gate (void) printf(gettext("WARNING: %s is on a temporary " 8780b5de56dSgjelinek "file system.\n"), rpath); 8797c478bd9Sstevel@tonic-gate } 8807c478bd9Sstevel@tonic-gate if (crosscheck_zonepaths(rpath) != Z_OK) 8817c478bd9Sstevel@tonic-gate return (Z_ERR); 8827c478bd9Sstevel@tonic-gate /* 8837c478bd9Sstevel@tonic-gate * Try to collect and report as many minor errors as possible 8847c478bd9Sstevel@tonic-gate * before returning, so the user can learn everything that needs 8857c478bd9Sstevel@tonic-gate * to be fixed up front. 8867c478bd9Sstevel@tonic-gate */ 8877c478bd9Sstevel@tonic-gate if (stbuf.st_uid != 0) { 8887c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s is not owned by root.\n"), 8897c478bd9Sstevel@tonic-gate rpath); 8907c478bd9Sstevel@tonic-gate err = B_TRUE; 8917c478bd9Sstevel@tonic-gate } 8927c478bd9Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IRUSR, B_TRUE, rpath); 8937c478bd9Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IWUSR, B_TRUE, rpath); 8947c478bd9Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IXUSR, B_TRUE, rpath); 8957c478bd9Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IRGRP, B_FALSE, rpath); 8967c478bd9Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IWGRP, B_FALSE, rpath); 8977c478bd9Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IXGRP, B_FALSE, rpath); 8987c478bd9Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IROTH, B_FALSE, rpath); 8997c478bd9Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IWOTH, B_FALSE, rpath); 9007c478bd9Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IXOTH, B_FALSE, rpath); 9017c478bd9Sstevel@tonic-gate 9027c478bd9Sstevel@tonic-gate (void) snprintf(ppath, sizeof (ppath), "%s/..", path); 9037c478bd9Sstevel@tonic-gate if ((res = resolvepath(ppath, rppath, sizeof (rppath))) == -1) { 9047c478bd9Sstevel@tonic-gate zperror(ppath, B_FALSE); 9057c478bd9Sstevel@tonic-gate return (Z_ERR); 9067c478bd9Sstevel@tonic-gate } 9077c478bd9Sstevel@tonic-gate rppath[res] = '\0'; 9087c478bd9Sstevel@tonic-gate if ((res = stat(rppath, &stbuf)) != 0) { 9097c478bd9Sstevel@tonic-gate zperror(rppath, B_FALSE); 9107c478bd9Sstevel@tonic-gate return (Z_ERR); 9117c478bd9Sstevel@tonic-gate } 9127c478bd9Sstevel@tonic-gate /* theoretically impossible */ 9137c478bd9Sstevel@tonic-gate if (!S_ISDIR(stbuf.st_mode)) { 9147c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s is not a directory.\n"), 9157c478bd9Sstevel@tonic-gate rppath); 9167c478bd9Sstevel@tonic-gate return (Z_ERR); 9177c478bd9Sstevel@tonic-gate } 9187c478bd9Sstevel@tonic-gate if (stbuf.st_uid != 0) { 9197c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s is not owned by root.\n"), 9207c478bd9Sstevel@tonic-gate rppath); 9217c478bd9Sstevel@tonic-gate err = B_TRUE; 9227c478bd9Sstevel@tonic-gate } 9237c478bd9Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IRUSR, B_TRUE, rppath); 9247c478bd9Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IWUSR, B_TRUE, rppath); 9257c478bd9Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IXUSR, B_TRUE, rppath); 9267c478bd9Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IWGRP, B_FALSE, rppath); 9277c478bd9Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IWOTH, B_FALSE, rppath); 9287c478bd9Sstevel@tonic-gate if (strcmp(rpath, rppath) == 0) { 9297c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s is its own parent.\n"), 9307c478bd9Sstevel@tonic-gate rppath); 9317c478bd9Sstevel@tonic-gate err = B_TRUE; 9327c478bd9Sstevel@tonic-gate } 9337c478bd9Sstevel@tonic-gate 9343f2f09c1Sdp if (statvfs64(rpath, &vfsbuf) != 0) { 9357c478bd9Sstevel@tonic-gate zperror(rpath, B_FALSE); 9367c478bd9Sstevel@tonic-gate return (Z_ERR); 9377c478bd9Sstevel@tonic-gate } 938b5abaf04Sgjelinek if (strcmp(vfsbuf.f_basetype, MNTTYPE_NFS) == 0) { 9395ee84fbdSgjelinek /* 9405ee84fbdSgjelinek * TRANSLATION_NOTE 9415ee84fbdSgjelinek * Zonepath and NFS are literals that should not be translated. 9425ee84fbdSgjelinek */ 9435ee84fbdSgjelinek (void) fprintf(stderr, gettext("Zonepath %s is on an NFS " 9440b5de56dSgjelinek "mounted file system.\n" 9450b5de56dSgjelinek "\tA local file system must be used.\n"), rpath); 946b5abaf04Sgjelinek return (Z_ERR); 947b5abaf04Sgjelinek } 948b5abaf04Sgjelinek if (vfsbuf.f_flag & ST_NOSUID) { 9495ee84fbdSgjelinek /* 9505ee84fbdSgjelinek * TRANSLATION_NOTE 9515ee84fbdSgjelinek * Zonepath and nosuid are literals that should not be 9525ee84fbdSgjelinek * translated. 9535ee84fbdSgjelinek */ 954b5abaf04Sgjelinek (void) fprintf(stderr, gettext("Zonepath %s is on a nosuid " 9550b5de56dSgjelinek "file system.\n"), rpath); 9567c478bd9Sstevel@tonic-gate return (Z_ERR); 9577c478bd9Sstevel@tonic-gate } 9587c478bd9Sstevel@tonic-gate 9597c478bd9Sstevel@tonic-gate if ((res = zone_get_state(target_zone, &state)) != Z_OK) { 9607c478bd9Sstevel@tonic-gate errno = res; 9617c478bd9Sstevel@tonic-gate zperror2(target_zone, gettext("could not get state")); 9627c478bd9Sstevel@tonic-gate return (Z_ERR); 9637c478bd9Sstevel@tonic-gate } 9647c478bd9Sstevel@tonic-gate /* 9657c478bd9Sstevel@tonic-gate * The existence of the root path is only bad in the configured state, 9667c478bd9Sstevel@tonic-gate * as it is *supposed* to be there at the installed and later states. 967ee519a1fSgjelinek * However, the root path is expected to be there if the zone is 968ee519a1fSgjelinek * detached. 9697c478bd9Sstevel@tonic-gate * State/command mismatches are caught earlier in verify_details(). 9707c478bd9Sstevel@tonic-gate */ 971ee519a1fSgjelinek if (state == ZONE_STATE_CONFIGURED && cmd_num != CMD_ATTACH) { 9727c478bd9Sstevel@tonic-gate if (snprintf(rootpath, sizeof (rootpath), "%s/root", rpath) >= 9737c478bd9Sstevel@tonic-gate sizeof (rootpath)) { 9745ee84fbdSgjelinek /* 9755ee84fbdSgjelinek * TRANSLATION_NOTE 9765ee84fbdSgjelinek * Zonepath is a literal that should not be translated. 9775ee84fbdSgjelinek */ 9787c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 9797c478bd9Sstevel@tonic-gate gettext("Zonepath %s is too long.\n"), rpath); 9807c478bd9Sstevel@tonic-gate return (Z_ERR); 9817c478bd9Sstevel@tonic-gate } 9827c478bd9Sstevel@tonic-gate if ((res = stat(rootpath, &stbuf)) == 0) { 983ee519a1fSgjelinek if (zonecfg_detached(rpath)) 984ee519a1fSgjelinek (void) fprintf(stderr, 985ee519a1fSgjelinek gettext("Cannot %s detached " 986ee519a1fSgjelinek "zone.\nUse attach or remove %s " 987ee519a1fSgjelinek "directory.\n"), cmd_to_str(cmd_num), 988ee519a1fSgjelinek rpath); 989ee519a1fSgjelinek else 990ee519a1fSgjelinek (void) fprintf(stderr, 991ee519a1fSgjelinek gettext("Rootpath %s exists; " 992ee519a1fSgjelinek "remove or move aside prior to %s.\n"), 993ee519a1fSgjelinek rootpath, cmd_to_str(cmd_num)); 9947c478bd9Sstevel@tonic-gate return (Z_ERR); 9957c478bd9Sstevel@tonic-gate } 9967c478bd9Sstevel@tonic-gate } 9977c478bd9Sstevel@tonic-gate 9987c478bd9Sstevel@tonic-gate return (err ? Z_ERR : Z_OK); 9997c478bd9Sstevel@tonic-gate } 10007c478bd9Sstevel@tonic-gate 10017c478bd9Sstevel@tonic-gate static void 10027c478bd9Sstevel@tonic-gate release_lock_file(int lockfd) 10037c478bd9Sstevel@tonic-gate { 10047c478bd9Sstevel@tonic-gate (void) close(lockfd); 10057c478bd9Sstevel@tonic-gate } 10067c478bd9Sstevel@tonic-gate 10077c478bd9Sstevel@tonic-gate static int 10087c478bd9Sstevel@tonic-gate grab_lock_file(const char *zone_name, int *lockfd) 10097c478bd9Sstevel@tonic-gate { 10107c478bd9Sstevel@tonic-gate char pathbuf[PATH_MAX]; 10117c478bd9Sstevel@tonic-gate struct flock flock; 10127c478bd9Sstevel@tonic-gate 1013108322fbScarlsonj if (snprintf(pathbuf, sizeof (pathbuf), "%s%s", zonecfg_get_root(), 1014108322fbScarlsonj ZONES_TMPDIR) >= sizeof (pathbuf)) { 1015108322fbScarlsonj zerror(gettext("alternate root path is too long")); 1016108322fbScarlsonj return (Z_ERR); 1017108322fbScarlsonj } 1018108322fbScarlsonj if (mkdir(pathbuf, S_IRWXU) < 0 && errno != EEXIST) { 1019108322fbScarlsonj zerror(gettext("could not mkdir %s: %s"), pathbuf, 10207c478bd9Sstevel@tonic-gate strerror(errno)); 10217c478bd9Sstevel@tonic-gate return (Z_ERR); 10227c478bd9Sstevel@tonic-gate } 1023108322fbScarlsonj (void) chmod(pathbuf, S_IRWXU); 10247c478bd9Sstevel@tonic-gate 10257c478bd9Sstevel@tonic-gate /* 10267c478bd9Sstevel@tonic-gate * One of these lock files is created for each zone (when needed). 10277c478bd9Sstevel@tonic-gate * The lock files are not cleaned up (except on system reboot), 10287c478bd9Sstevel@tonic-gate * but since there is only one per zone, there is no resource 10297c478bd9Sstevel@tonic-gate * starvation issue. 10307c478bd9Sstevel@tonic-gate */ 1031108322fbScarlsonj if (snprintf(pathbuf, sizeof (pathbuf), "%s%s/%s.zoneadm.lock", 1032108322fbScarlsonj zonecfg_get_root(), ZONES_TMPDIR, zone_name) >= sizeof (pathbuf)) { 1033108322fbScarlsonj zerror(gettext("alternate root path is too long")); 1034108322fbScarlsonj return (Z_ERR); 1035108322fbScarlsonj } 10367c478bd9Sstevel@tonic-gate if ((*lockfd = open(pathbuf, O_RDWR|O_CREAT, S_IRUSR|S_IWUSR)) < 0) { 10377c478bd9Sstevel@tonic-gate zerror(gettext("could not open %s: %s"), pathbuf, 10387c478bd9Sstevel@tonic-gate strerror(errno)); 10397c478bd9Sstevel@tonic-gate return (Z_ERR); 10407c478bd9Sstevel@tonic-gate } 10417c478bd9Sstevel@tonic-gate /* 10427c478bd9Sstevel@tonic-gate * Lock the file to synchronize with other zoneadmds 10437c478bd9Sstevel@tonic-gate */ 10447c478bd9Sstevel@tonic-gate flock.l_type = F_WRLCK; 10457c478bd9Sstevel@tonic-gate flock.l_whence = SEEK_SET; 10467c478bd9Sstevel@tonic-gate flock.l_start = (off_t)0; 10477c478bd9Sstevel@tonic-gate flock.l_len = (off_t)0; 10487c478bd9Sstevel@tonic-gate if (fcntl(*lockfd, F_SETLKW, &flock) < 0) { 10497c478bd9Sstevel@tonic-gate zerror(gettext("unable to lock %s: %s"), pathbuf, 10507c478bd9Sstevel@tonic-gate strerror(errno)); 10517c478bd9Sstevel@tonic-gate release_lock_file(*lockfd); 10527c478bd9Sstevel@tonic-gate return (Z_ERR); 10537c478bd9Sstevel@tonic-gate } 10547c478bd9Sstevel@tonic-gate return (Z_OK); 10557c478bd9Sstevel@tonic-gate } 10567c478bd9Sstevel@tonic-gate 1057108322fbScarlsonj static boolean_t 10587c478bd9Sstevel@tonic-gate get_doorname(const char *zone_name, char *buffer) 10597c478bd9Sstevel@tonic-gate { 1060108322fbScarlsonj return (snprintf(buffer, PATH_MAX, "%s" ZONE_DOOR_PATH, 1061108322fbScarlsonj zonecfg_get_root(), zone_name) < PATH_MAX); 10627c478bd9Sstevel@tonic-gate } 10637c478bd9Sstevel@tonic-gate 10647c478bd9Sstevel@tonic-gate /* 10657c478bd9Sstevel@tonic-gate * system daemons are not audited. For the global zone, this occurs 10667c478bd9Sstevel@tonic-gate * "naturally" since init is started with the default audit 10677c478bd9Sstevel@tonic-gate * characteristics. Since zoneadmd is a system daemon and it starts 10687c478bd9Sstevel@tonic-gate * init for a zone, it is necessary to clear out the audit 10697c478bd9Sstevel@tonic-gate * characteristics inherited from whomever started zoneadmd. This is 10707c478bd9Sstevel@tonic-gate * indicated by the audit id, which is set from the ruid parameter of 10717c478bd9Sstevel@tonic-gate * adt_set_user(), below. 10727c478bd9Sstevel@tonic-gate */ 10737c478bd9Sstevel@tonic-gate 10747c478bd9Sstevel@tonic-gate static void 10757c478bd9Sstevel@tonic-gate prepare_audit_context() 10767c478bd9Sstevel@tonic-gate { 10777c478bd9Sstevel@tonic-gate adt_session_data_t *ah; 10787c478bd9Sstevel@tonic-gate char *failure = gettext("audit failure: %s"); 10797c478bd9Sstevel@tonic-gate 10807c478bd9Sstevel@tonic-gate if (adt_start_session(&ah, NULL, 0)) { 10817c478bd9Sstevel@tonic-gate zerror(failure, strerror(errno)); 10827c478bd9Sstevel@tonic-gate return; 10837c478bd9Sstevel@tonic-gate } 10847c478bd9Sstevel@tonic-gate if (adt_set_user(ah, ADT_NO_AUDIT, ADT_NO_AUDIT, 10857c478bd9Sstevel@tonic-gate ADT_NO_AUDIT, ADT_NO_AUDIT, NULL, ADT_NEW)) { 10867c478bd9Sstevel@tonic-gate zerror(failure, strerror(errno)); 10877c478bd9Sstevel@tonic-gate (void) adt_end_session(ah); 10887c478bd9Sstevel@tonic-gate return; 10897c478bd9Sstevel@tonic-gate } 10907c478bd9Sstevel@tonic-gate if (adt_set_proc(ah)) 10917c478bd9Sstevel@tonic-gate zerror(failure, strerror(errno)); 10927c478bd9Sstevel@tonic-gate 10937c478bd9Sstevel@tonic-gate (void) adt_end_session(ah); 10947c478bd9Sstevel@tonic-gate } 10957c478bd9Sstevel@tonic-gate 10967c478bd9Sstevel@tonic-gate static int 10977c478bd9Sstevel@tonic-gate start_zoneadmd(const char *zone_name) 10987c478bd9Sstevel@tonic-gate { 10997c478bd9Sstevel@tonic-gate char doorpath[PATH_MAX]; 11007c478bd9Sstevel@tonic-gate pid_t child_pid; 11017c478bd9Sstevel@tonic-gate int error = Z_ERR; 11027c478bd9Sstevel@tonic-gate int doorfd, lockfd; 11037c478bd9Sstevel@tonic-gate struct door_info info; 11047c478bd9Sstevel@tonic-gate 1105108322fbScarlsonj if (!get_doorname(zone_name, doorpath)) 1106108322fbScarlsonj return (Z_ERR); 11077c478bd9Sstevel@tonic-gate 11087c478bd9Sstevel@tonic-gate if (grab_lock_file(zone_name, &lockfd) != Z_OK) 11097c478bd9Sstevel@tonic-gate return (Z_ERR); 11107c478bd9Sstevel@tonic-gate 11117c478bd9Sstevel@tonic-gate /* 11127c478bd9Sstevel@tonic-gate * Now that we have the lock, re-confirm that the daemon is 11137c478bd9Sstevel@tonic-gate * *not* up and working fine. If it is still down, we have a green 11147c478bd9Sstevel@tonic-gate * light to start it. 11157c478bd9Sstevel@tonic-gate */ 11167c478bd9Sstevel@tonic-gate if ((doorfd = open(doorpath, O_RDONLY)) < 0) { 11177c478bd9Sstevel@tonic-gate if (errno != ENOENT) { 11187c478bd9Sstevel@tonic-gate zperror(doorpath, B_FALSE); 11197c478bd9Sstevel@tonic-gate goto out; 11207c478bd9Sstevel@tonic-gate } 11217c478bd9Sstevel@tonic-gate } else { 11227c478bd9Sstevel@tonic-gate if (door_info(doorfd, &info) == 0 && 11237c478bd9Sstevel@tonic-gate ((info.di_attributes & DOOR_REVOKED) == 0)) { 11247c478bd9Sstevel@tonic-gate error = Z_OK; 11257c478bd9Sstevel@tonic-gate (void) close(doorfd); 11267c478bd9Sstevel@tonic-gate goto out; 11277c478bd9Sstevel@tonic-gate } 11287c478bd9Sstevel@tonic-gate (void) close(doorfd); 11297c478bd9Sstevel@tonic-gate } 11307c478bd9Sstevel@tonic-gate 11317c478bd9Sstevel@tonic-gate if ((child_pid = fork()) == -1) { 11327c478bd9Sstevel@tonic-gate zperror(gettext("could not fork"), B_FALSE); 11337c478bd9Sstevel@tonic-gate goto out; 11347c478bd9Sstevel@tonic-gate } else if (child_pid == 0) { 1135108322fbScarlsonj const char *argv[6], **ap; 11367c478bd9Sstevel@tonic-gate 1137108322fbScarlsonj /* child process */ 11387c478bd9Sstevel@tonic-gate prepare_audit_context(); 11397c478bd9Sstevel@tonic-gate 1140108322fbScarlsonj ap = argv; 1141108322fbScarlsonj *ap++ = "zoneadmd"; 1142108322fbScarlsonj *ap++ = "-z"; 1143108322fbScarlsonj *ap++ = zone_name; 1144108322fbScarlsonj if (zonecfg_in_alt_root()) { 1145108322fbScarlsonj *ap++ = "-R"; 1146108322fbScarlsonj *ap++ = zonecfg_get_root(); 1147108322fbScarlsonj } 1148108322fbScarlsonj *ap = NULL; 1149108322fbScarlsonj 1150108322fbScarlsonj (void) execv("/usr/lib/zones/zoneadmd", (char * const *)argv); 11515ee84fbdSgjelinek /* 11525ee84fbdSgjelinek * TRANSLATION_NOTE 11535ee84fbdSgjelinek * zoneadmd is a literal that should not be translated. 11545ee84fbdSgjelinek */ 11557c478bd9Sstevel@tonic-gate zperror(gettext("could not exec zoneadmd"), B_FALSE); 11567c478bd9Sstevel@tonic-gate _exit(Z_ERR); 11577c478bd9Sstevel@tonic-gate } else { 11587c478bd9Sstevel@tonic-gate /* parent process */ 11597c478bd9Sstevel@tonic-gate pid_t retval; 11607c478bd9Sstevel@tonic-gate int pstatus = 0; 11617c478bd9Sstevel@tonic-gate 11627c478bd9Sstevel@tonic-gate do { 11637c478bd9Sstevel@tonic-gate retval = waitpid(child_pid, &pstatus, 0); 11647c478bd9Sstevel@tonic-gate } while (retval != child_pid); 11657c478bd9Sstevel@tonic-gate if (WIFSIGNALED(pstatus) || (WIFEXITED(pstatus) && 11667c478bd9Sstevel@tonic-gate WEXITSTATUS(pstatus) != 0)) { 11677c478bd9Sstevel@tonic-gate zerror(gettext("could not start %s"), "zoneadmd"); 11687c478bd9Sstevel@tonic-gate goto out; 11697c478bd9Sstevel@tonic-gate } 11707c478bd9Sstevel@tonic-gate } 11717c478bd9Sstevel@tonic-gate error = Z_OK; 11727c478bd9Sstevel@tonic-gate out: 11737c478bd9Sstevel@tonic-gate release_lock_file(lockfd); 11747c478bd9Sstevel@tonic-gate return (error); 11757c478bd9Sstevel@tonic-gate } 11767c478bd9Sstevel@tonic-gate 11777c478bd9Sstevel@tonic-gate static int 11787c478bd9Sstevel@tonic-gate ping_zoneadmd(const char *zone_name) 11797c478bd9Sstevel@tonic-gate { 11807c478bd9Sstevel@tonic-gate char doorpath[PATH_MAX]; 11817c478bd9Sstevel@tonic-gate int doorfd; 11827c478bd9Sstevel@tonic-gate struct door_info info; 11837c478bd9Sstevel@tonic-gate 1184108322fbScarlsonj if (!get_doorname(zone_name, doorpath)) 1185108322fbScarlsonj return (Z_ERR); 11867c478bd9Sstevel@tonic-gate 11877c478bd9Sstevel@tonic-gate if ((doorfd = open(doorpath, O_RDONLY)) < 0) { 11887c478bd9Sstevel@tonic-gate return (Z_ERR); 11897c478bd9Sstevel@tonic-gate } 11907c478bd9Sstevel@tonic-gate if (door_info(doorfd, &info) == 0 && 11917c478bd9Sstevel@tonic-gate ((info.di_attributes & DOOR_REVOKED) == 0)) { 11927c478bd9Sstevel@tonic-gate (void) close(doorfd); 11937c478bd9Sstevel@tonic-gate return (Z_OK); 11947c478bd9Sstevel@tonic-gate } 11957c478bd9Sstevel@tonic-gate (void) close(doorfd); 11967c478bd9Sstevel@tonic-gate return (Z_ERR); 11977c478bd9Sstevel@tonic-gate } 11987c478bd9Sstevel@tonic-gate 11997c478bd9Sstevel@tonic-gate static int 12007c478bd9Sstevel@tonic-gate call_zoneadmd(const char *zone_name, zone_cmd_arg_t *arg) 12017c478bd9Sstevel@tonic-gate { 12027c478bd9Sstevel@tonic-gate char doorpath[PATH_MAX]; 12037c478bd9Sstevel@tonic-gate int doorfd, result; 12047c478bd9Sstevel@tonic-gate door_arg_t darg; 12057c478bd9Sstevel@tonic-gate 12067c478bd9Sstevel@tonic-gate zoneid_t zoneid; 12077c478bd9Sstevel@tonic-gate uint64_t uniqid = 0; 12087c478bd9Sstevel@tonic-gate 12097c478bd9Sstevel@tonic-gate zone_cmd_rval_t *rvalp; 12107c478bd9Sstevel@tonic-gate size_t rlen; 12117c478bd9Sstevel@tonic-gate char *cp, *errbuf; 12127c478bd9Sstevel@tonic-gate 12137c478bd9Sstevel@tonic-gate rlen = getpagesize(); 12147c478bd9Sstevel@tonic-gate if ((rvalp = malloc(rlen)) == NULL) { 12157c478bd9Sstevel@tonic-gate zerror(gettext("failed to allocate %lu bytes: %s"), rlen, 12167c478bd9Sstevel@tonic-gate strerror(errno)); 12177c478bd9Sstevel@tonic-gate return (-1); 12187c478bd9Sstevel@tonic-gate } 12197c478bd9Sstevel@tonic-gate 12207c478bd9Sstevel@tonic-gate if ((zoneid = getzoneidbyname(zone_name)) != ZONE_ID_UNDEFINED) { 12217c478bd9Sstevel@tonic-gate (void) zone_getattr(zoneid, ZONE_ATTR_UNIQID, &uniqid, 12227c478bd9Sstevel@tonic-gate sizeof (uniqid)); 12237c478bd9Sstevel@tonic-gate } 12247c478bd9Sstevel@tonic-gate arg->uniqid = uniqid; 12257c478bd9Sstevel@tonic-gate (void) strlcpy(arg->locale, locale, sizeof (arg->locale)); 1226108322fbScarlsonj if (!get_doorname(zone_name, doorpath)) { 1227108322fbScarlsonj zerror(gettext("alternate root path is too long")); 1228108322fbScarlsonj free(rvalp); 1229108322fbScarlsonj return (-1); 1230108322fbScarlsonj } 12317c478bd9Sstevel@tonic-gate 12327c478bd9Sstevel@tonic-gate /* 12337c478bd9Sstevel@tonic-gate * Loop trying to start zoneadmd; if something goes seriously 12347c478bd9Sstevel@tonic-gate * wrong we break out and fail. 12357c478bd9Sstevel@tonic-gate */ 12367c478bd9Sstevel@tonic-gate for (;;) { 12377c478bd9Sstevel@tonic-gate if (start_zoneadmd(zone_name) != Z_OK) 12387c478bd9Sstevel@tonic-gate break; 12397c478bd9Sstevel@tonic-gate 12407c478bd9Sstevel@tonic-gate if ((doorfd = open(doorpath, O_RDONLY)) < 0) { 12417c478bd9Sstevel@tonic-gate zperror(gettext("failed to open zone door"), B_FALSE); 12427c478bd9Sstevel@tonic-gate break; 12437c478bd9Sstevel@tonic-gate } 12447c478bd9Sstevel@tonic-gate 12457c478bd9Sstevel@tonic-gate darg.data_ptr = (char *)arg; 12467c478bd9Sstevel@tonic-gate darg.data_size = sizeof (*arg); 12477c478bd9Sstevel@tonic-gate darg.desc_ptr = NULL; 12487c478bd9Sstevel@tonic-gate darg.desc_num = 0; 12497c478bd9Sstevel@tonic-gate darg.rbuf = (char *)rvalp; 12507c478bd9Sstevel@tonic-gate darg.rsize = rlen; 12517c478bd9Sstevel@tonic-gate if (door_call(doorfd, &darg) != 0) { 12527c478bd9Sstevel@tonic-gate (void) close(doorfd); 12537c478bd9Sstevel@tonic-gate /* 12547c478bd9Sstevel@tonic-gate * We'll get EBADF if the door has been revoked. 12557c478bd9Sstevel@tonic-gate */ 12567c478bd9Sstevel@tonic-gate if (errno != EBADF) { 12577c478bd9Sstevel@tonic-gate zperror(gettext("door_call failed"), B_FALSE); 12587c478bd9Sstevel@tonic-gate break; 12597c478bd9Sstevel@tonic-gate } 12607c478bd9Sstevel@tonic-gate continue; /* take another lap */ 12617c478bd9Sstevel@tonic-gate } 12627c478bd9Sstevel@tonic-gate (void) close(doorfd); 12637c478bd9Sstevel@tonic-gate 12647c478bd9Sstevel@tonic-gate if (darg.data_size == 0) { 12657c478bd9Sstevel@tonic-gate /* Door server is going away; kick it again. */ 12667c478bd9Sstevel@tonic-gate continue; 12677c478bd9Sstevel@tonic-gate } 12687c478bd9Sstevel@tonic-gate 12697c478bd9Sstevel@tonic-gate errbuf = rvalp->errbuf; 12707c478bd9Sstevel@tonic-gate while (*errbuf != '\0') { 12717c478bd9Sstevel@tonic-gate /* 12727c478bd9Sstevel@tonic-gate * Remove any newlines since zerror() 12737c478bd9Sstevel@tonic-gate * will append one automatically. 12747c478bd9Sstevel@tonic-gate */ 12757c478bd9Sstevel@tonic-gate cp = strchr(errbuf, '\n'); 12767c478bd9Sstevel@tonic-gate if (cp != NULL) 12777c478bd9Sstevel@tonic-gate *cp = '\0'; 12787c478bd9Sstevel@tonic-gate zerror("%s", errbuf); 12797c478bd9Sstevel@tonic-gate if (cp == NULL) 12807c478bd9Sstevel@tonic-gate break; 12817c478bd9Sstevel@tonic-gate errbuf = cp + 1; 12827c478bd9Sstevel@tonic-gate } 12837c478bd9Sstevel@tonic-gate result = rvalp->rval == 0 ? 0 : -1; 12847c478bd9Sstevel@tonic-gate free(rvalp); 12857c478bd9Sstevel@tonic-gate return (result); 12867c478bd9Sstevel@tonic-gate } 12877c478bd9Sstevel@tonic-gate 12887c478bd9Sstevel@tonic-gate free(rvalp); 12897c478bd9Sstevel@tonic-gate return (-1); 12907c478bd9Sstevel@tonic-gate } 12917c478bd9Sstevel@tonic-gate 12927c478bd9Sstevel@tonic-gate static int 12937c478bd9Sstevel@tonic-gate ready_func(int argc, char *argv[]) 12947c478bd9Sstevel@tonic-gate { 12957c478bd9Sstevel@tonic-gate zone_cmd_arg_t zarg; 12967c478bd9Sstevel@tonic-gate int arg; 12977c478bd9Sstevel@tonic-gate 1298108322fbScarlsonj if (zonecfg_in_alt_root()) { 1299108322fbScarlsonj zerror(gettext("cannot ready zone in alternate root")); 1300108322fbScarlsonj return (Z_ERR); 1301108322fbScarlsonj } 1302108322fbScarlsonj 13037c478bd9Sstevel@tonic-gate optind = 0; 13047c478bd9Sstevel@tonic-gate if ((arg = getopt(argc, argv, "?")) != EOF) { 13057c478bd9Sstevel@tonic-gate switch (arg) { 13067c478bd9Sstevel@tonic-gate case '?': 13077c478bd9Sstevel@tonic-gate sub_usage(SHELP_READY, CMD_READY); 13087c478bd9Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 13097c478bd9Sstevel@tonic-gate default: 13107c478bd9Sstevel@tonic-gate sub_usage(SHELP_READY, CMD_READY); 13117c478bd9Sstevel@tonic-gate return (Z_USAGE); 13127c478bd9Sstevel@tonic-gate } 13137c478bd9Sstevel@tonic-gate } 13147c478bd9Sstevel@tonic-gate if (argc > optind) { 13157c478bd9Sstevel@tonic-gate sub_usage(SHELP_READY, CMD_READY); 13167c478bd9Sstevel@tonic-gate return (Z_USAGE); 13177c478bd9Sstevel@tonic-gate } 13187c478bd9Sstevel@tonic-gate if (sanity_check(target_zone, CMD_READY, B_FALSE, B_FALSE) != Z_OK) 13197c478bd9Sstevel@tonic-gate return (Z_ERR); 13207c478bd9Sstevel@tonic-gate if (verify_details(CMD_READY) != Z_OK) 13217c478bd9Sstevel@tonic-gate return (Z_ERR); 13227c478bd9Sstevel@tonic-gate 13237c478bd9Sstevel@tonic-gate zarg.cmd = Z_READY; 13247c478bd9Sstevel@tonic-gate if (call_zoneadmd(target_zone, &zarg) != 0) { 13257c478bd9Sstevel@tonic-gate zerror(gettext("call to %s failed"), "zoneadmd"); 13267c478bd9Sstevel@tonic-gate return (Z_ERR); 13277c478bd9Sstevel@tonic-gate } 13287c478bd9Sstevel@tonic-gate return (Z_OK); 13297c478bd9Sstevel@tonic-gate } 13307c478bd9Sstevel@tonic-gate 13317c478bd9Sstevel@tonic-gate static int 13327c478bd9Sstevel@tonic-gate boot_func(int argc, char *argv[]) 13337c478bd9Sstevel@tonic-gate { 13347c478bd9Sstevel@tonic-gate zone_cmd_arg_t zarg; 13357c478bd9Sstevel@tonic-gate int arg; 13367c478bd9Sstevel@tonic-gate 1337108322fbScarlsonj if (zonecfg_in_alt_root()) { 1338108322fbScarlsonj zerror(gettext("cannot boot zone in alternate root")); 1339108322fbScarlsonj return (Z_ERR); 1340108322fbScarlsonj } 1341108322fbScarlsonj 13427c478bd9Sstevel@tonic-gate zarg.bootbuf[0] = '\0'; 13437c478bd9Sstevel@tonic-gate 13447c478bd9Sstevel@tonic-gate /* 13453f2f09c1Sdp * The following getopt processes arguments to zone boot; that 13463f2f09c1Sdp * is to say, the [here] portion of the argument string: 13473f2f09c1Sdp * 13483f2f09c1Sdp * zoneadm -z myzone boot [here] -- -v -m verbose 13493f2f09c1Sdp * 13503f2f09c1Sdp * Where [here] can either be nothing, -? (in which case we bail 13513f2f09c1Sdp * and print usage), or -s. Support for -s is vestigal and 13523f2f09c1Sdp * obsolete, but is retained because it was a documented interface 13533f2f09c1Sdp * and there are known consumers including admin/install; the 13543f2f09c1Sdp * proper way to specify boot arguments like -s is: 13553f2f09c1Sdp * 13563f2f09c1Sdp * zoneadm -z myzone boot -- -s -v -m verbose. 13577c478bd9Sstevel@tonic-gate */ 13587c478bd9Sstevel@tonic-gate optind = 0; 13597c478bd9Sstevel@tonic-gate if ((arg = getopt(argc, argv, "?s")) != EOF) { 13607c478bd9Sstevel@tonic-gate switch (arg) { 13617c478bd9Sstevel@tonic-gate case '?': 13627c478bd9Sstevel@tonic-gate sub_usage(SHELP_BOOT, CMD_BOOT); 13637c478bd9Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 13647c478bd9Sstevel@tonic-gate case 's': 13657c478bd9Sstevel@tonic-gate (void) strlcpy(zarg.bootbuf, "-s", 13667c478bd9Sstevel@tonic-gate sizeof (zarg.bootbuf)); 13677c478bd9Sstevel@tonic-gate break; 13687c478bd9Sstevel@tonic-gate default: 13697c478bd9Sstevel@tonic-gate sub_usage(SHELP_BOOT, CMD_BOOT); 13707c478bd9Sstevel@tonic-gate return (Z_USAGE); 13717c478bd9Sstevel@tonic-gate } 13727c478bd9Sstevel@tonic-gate } 13733f2f09c1Sdp 13743f2f09c1Sdp for (; optind < argc; optind++) { 13753f2f09c1Sdp if (strlcat(zarg.bootbuf, argv[optind], 13763f2f09c1Sdp sizeof (zarg.bootbuf)) >= sizeof (zarg.bootbuf)) { 13773f2f09c1Sdp zerror(gettext("Boot argument list too long")); 13783f2f09c1Sdp return (Z_ERR); 13797c478bd9Sstevel@tonic-gate } 13803f2f09c1Sdp if (optind < argc - 1) 13813f2f09c1Sdp if (strlcat(zarg.bootbuf, " ", sizeof (zarg.bootbuf)) >= 13823f2f09c1Sdp sizeof (zarg.bootbuf)) { 13833f2f09c1Sdp zerror(gettext("Boot argument list too long")); 13843f2f09c1Sdp return (Z_ERR); 13853f2f09c1Sdp } 13863f2f09c1Sdp } 13873f2f09c1Sdp 13887c478bd9Sstevel@tonic-gate if (sanity_check(target_zone, CMD_BOOT, B_FALSE, B_FALSE) != Z_OK) 13897c478bd9Sstevel@tonic-gate return (Z_ERR); 13907c478bd9Sstevel@tonic-gate if (verify_details(CMD_BOOT) != Z_OK) 13917c478bd9Sstevel@tonic-gate return (Z_ERR); 13927c478bd9Sstevel@tonic-gate zarg.cmd = Z_BOOT; 13937c478bd9Sstevel@tonic-gate if (call_zoneadmd(target_zone, &zarg) != 0) { 13947c478bd9Sstevel@tonic-gate zerror(gettext("call to %s failed"), "zoneadmd"); 13957c478bd9Sstevel@tonic-gate return (Z_ERR); 13967c478bd9Sstevel@tonic-gate } 13977c478bd9Sstevel@tonic-gate return (Z_OK); 13987c478bd9Sstevel@tonic-gate } 13997c478bd9Sstevel@tonic-gate 14007c478bd9Sstevel@tonic-gate static void 14017c478bd9Sstevel@tonic-gate fake_up_local_zone(zoneid_t zid, zone_entry_t *zeptr) 14027c478bd9Sstevel@tonic-gate { 14037c478bd9Sstevel@tonic-gate ssize_t result; 1404555afedfScarlsonj uuid_t uuid; 1405555afedfScarlsonj FILE *fp; 1406555afedfScarlsonj 1407555afedfScarlsonj (void) memset(zeptr, 0, sizeof (*zeptr)); 14087c478bd9Sstevel@tonic-gate 14097c478bd9Sstevel@tonic-gate zeptr->zid = zid; 1410555afedfScarlsonj 14117c478bd9Sstevel@tonic-gate /* 14127c478bd9Sstevel@tonic-gate * Since we're looking up our own (non-global) zone name, 14137c478bd9Sstevel@tonic-gate * we can be assured that it will succeed. 14147c478bd9Sstevel@tonic-gate */ 14157c478bd9Sstevel@tonic-gate result = getzonenamebyid(zid, zeptr->zname, sizeof (zeptr->zname)); 14167c478bd9Sstevel@tonic-gate assert(result >= 0); 1417555afedfScarlsonj if (zonecfg_is_scratch(zeptr->zname) && 1418555afedfScarlsonj (fp = zonecfg_open_scratch("", B_FALSE)) != NULL) { 1419555afedfScarlsonj (void) zonecfg_reverse_scratch(fp, zeptr->zname, zeptr->zname, 1420555afedfScarlsonj sizeof (zeptr->zname), NULL, 0); 1421555afedfScarlsonj zonecfg_close_scratch(fp); 1422555afedfScarlsonj } 1423555afedfScarlsonj 1424555afedfScarlsonj if (is_system_labeled()) { 142545916cd2Sjpk (void) zone_getattr(zid, ZONE_ATTR_ROOT, zeptr->zroot, 142645916cd2Sjpk sizeof (zeptr->zroot)); 1427555afedfScarlsonj } else { 1428555afedfScarlsonj (void) strlcpy(zeptr->zroot, "/", sizeof (zeptr->zroot)); 142945916cd2Sjpk } 1430555afedfScarlsonj 14317c478bd9Sstevel@tonic-gate zeptr->zstate_str = "running"; 1432555afedfScarlsonj 1433555afedfScarlsonj if (zonecfg_get_uuid(zeptr->zname, uuid) == Z_OK && 1434555afedfScarlsonj !uuid_is_null(uuid)) 1435555afedfScarlsonj uuid_unparse(uuid, zeptr->zuuid); 14367c478bd9Sstevel@tonic-gate } 14377c478bd9Sstevel@tonic-gate 14387c478bd9Sstevel@tonic-gate static int 14397c478bd9Sstevel@tonic-gate list_func(int argc, char *argv[]) 14407c478bd9Sstevel@tonic-gate { 14417c478bd9Sstevel@tonic-gate zone_entry_t *zentp, zent; 1442108322fbScarlsonj int arg, retv; 14437c478bd9Sstevel@tonic-gate boolean_t output = B_FALSE, verbose = B_FALSE, parsable = B_FALSE; 14447c478bd9Sstevel@tonic-gate zone_state_t min_state = ZONE_STATE_RUNNING; 14457c478bd9Sstevel@tonic-gate zoneid_t zone_id = getzoneid(); 14467c478bd9Sstevel@tonic-gate 14477c478bd9Sstevel@tonic-gate if (target_zone == NULL) { 14487c478bd9Sstevel@tonic-gate /* all zones: default view to running but allow override */ 14497c478bd9Sstevel@tonic-gate optind = 0; 14507c478bd9Sstevel@tonic-gate while ((arg = getopt(argc, argv, "?cipv")) != EOF) { 14517c478bd9Sstevel@tonic-gate switch (arg) { 14527c478bd9Sstevel@tonic-gate case '?': 14537c478bd9Sstevel@tonic-gate sub_usage(SHELP_LIST, CMD_LIST); 14547c478bd9Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 1455475d78a4Sjonb /* 1456475d78a4Sjonb * The 'i' and 'c' options are not mutually 1457475d78a4Sjonb * exclusive so if 'c' is given, then min_state 1458475d78a4Sjonb * is set to 0 (ZONE_STATE_CONFIGURED) which is 1459475d78a4Sjonb * the lowest possible state. If 'i' is given, 1460475d78a4Sjonb * then min_state is set to be the lowest state 1461475d78a4Sjonb * so far. 1462475d78a4Sjonb */ 14637c478bd9Sstevel@tonic-gate case 'c': 14647c478bd9Sstevel@tonic-gate min_state = ZONE_STATE_CONFIGURED; 14657c478bd9Sstevel@tonic-gate break; 14667c478bd9Sstevel@tonic-gate case 'i': 1467475d78a4Sjonb min_state = min(ZONE_STATE_INSTALLED, 1468475d78a4Sjonb min_state); 1469475d78a4Sjonb 14707c478bd9Sstevel@tonic-gate break; 14717c478bd9Sstevel@tonic-gate case 'p': 14727c478bd9Sstevel@tonic-gate parsable = B_TRUE; 14737c478bd9Sstevel@tonic-gate break; 14747c478bd9Sstevel@tonic-gate case 'v': 14757c478bd9Sstevel@tonic-gate verbose = B_TRUE; 14767c478bd9Sstevel@tonic-gate break; 14777c478bd9Sstevel@tonic-gate default: 14787c478bd9Sstevel@tonic-gate sub_usage(SHELP_LIST, CMD_LIST); 14797c478bd9Sstevel@tonic-gate return (Z_USAGE); 14807c478bd9Sstevel@tonic-gate } 14817c478bd9Sstevel@tonic-gate } 14827c478bd9Sstevel@tonic-gate if (parsable && verbose) { 14837c478bd9Sstevel@tonic-gate zerror(gettext("%s -p and -v are mutually exclusive."), 14847c478bd9Sstevel@tonic-gate cmd_to_str(CMD_LIST)); 14857c478bd9Sstevel@tonic-gate return (Z_ERR); 14867c478bd9Sstevel@tonic-gate } 148745916cd2Sjpk if (zone_id == GLOBAL_ZONEID || is_system_labeled()) { 1488108322fbScarlsonj retv = zone_print_list(min_state, verbose, parsable); 14897c478bd9Sstevel@tonic-gate } else { 1490108322fbScarlsonj retv = Z_OK; 14917c478bd9Sstevel@tonic-gate fake_up_local_zone(zone_id, &zent); 14927c478bd9Sstevel@tonic-gate zone_print(&zent, verbose, parsable); 14937c478bd9Sstevel@tonic-gate } 1494108322fbScarlsonj return (retv); 14957c478bd9Sstevel@tonic-gate } 14967c478bd9Sstevel@tonic-gate 14977c478bd9Sstevel@tonic-gate /* 14987c478bd9Sstevel@tonic-gate * Specific target zone: disallow -i/-c suboptions. 14997c478bd9Sstevel@tonic-gate */ 15007c478bd9Sstevel@tonic-gate optind = 0; 15017c478bd9Sstevel@tonic-gate while ((arg = getopt(argc, argv, "?pv")) != EOF) { 15027c478bd9Sstevel@tonic-gate switch (arg) { 15037c478bd9Sstevel@tonic-gate case '?': 15047c478bd9Sstevel@tonic-gate sub_usage(SHELP_LIST, CMD_LIST); 15057c478bd9Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 15067c478bd9Sstevel@tonic-gate case 'p': 15077c478bd9Sstevel@tonic-gate parsable = B_TRUE; 15087c478bd9Sstevel@tonic-gate break; 15097c478bd9Sstevel@tonic-gate case 'v': 15107c478bd9Sstevel@tonic-gate verbose = B_TRUE; 15117c478bd9Sstevel@tonic-gate break; 15127c478bd9Sstevel@tonic-gate default: 15137c478bd9Sstevel@tonic-gate sub_usage(SHELP_LIST, CMD_LIST); 15147c478bd9Sstevel@tonic-gate return (Z_USAGE); 15157c478bd9Sstevel@tonic-gate } 15167c478bd9Sstevel@tonic-gate } 15177c478bd9Sstevel@tonic-gate if (parsable && verbose) { 15187c478bd9Sstevel@tonic-gate zerror(gettext("%s -p and -v are mutually exclusive."), 15197c478bd9Sstevel@tonic-gate cmd_to_str(CMD_LIST)); 15207c478bd9Sstevel@tonic-gate return (Z_ERR); 15217c478bd9Sstevel@tonic-gate } 15227c478bd9Sstevel@tonic-gate if (argc > optind) { 15237c478bd9Sstevel@tonic-gate sub_usage(SHELP_LIST, CMD_LIST); 15247c478bd9Sstevel@tonic-gate return (Z_USAGE); 15257c478bd9Sstevel@tonic-gate } 15267c478bd9Sstevel@tonic-gate if (zone_id != GLOBAL_ZONEID) { 15277c478bd9Sstevel@tonic-gate fake_up_local_zone(zone_id, &zent); 15287c478bd9Sstevel@tonic-gate /* 15297c478bd9Sstevel@tonic-gate * main() will issue a Z_NO_ZONE error if it cannot get an 15307c478bd9Sstevel@tonic-gate * id for target_zone, which in a non-global zone should 15317c478bd9Sstevel@tonic-gate * happen for any zone name except `zonename`. Thus we 15327c478bd9Sstevel@tonic-gate * assert() that here but don't otherwise check. 15337c478bd9Sstevel@tonic-gate */ 15347c478bd9Sstevel@tonic-gate assert(strcmp(zent.zname, target_zone) == 0); 15357c478bd9Sstevel@tonic-gate zone_print(&zent, verbose, parsable); 15367c478bd9Sstevel@tonic-gate output = B_TRUE; 15377c478bd9Sstevel@tonic-gate } else if ((zentp = lookup_running_zone(target_zone)) != NULL) { 15387c478bd9Sstevel@tonic-gate zone_print(zentp, verbose, parsable); 15397c478bd9Sstevel@tonic-gate output = B_TRUE; 1540108322fbScarlsonj } else if (lookup_zone_info(target_zone, ZONE_ID_UNDEFINED, 1541108322fbScarlsonj &zent) == Z_OK) { 15427c478bd9Sstevel@tonic-gate zone_print(&zent, verbose, parsable); 15437c478bd9Sstevel@tonic-gate output = B_TRUE; 15447c478bd9Sstevel@tonic-gate } 15457c478bd9Sstevel@tonic-gate return (output ? Z_OK : Z_ERR); 15467c478bd9Sstevel@tonic-gate } 15477c478bd9Sstevel@tonic-gate 15487c478bd9Sstevel@tonic-gate static void 15497c478bd9Sstevel@tonic-gate sigterm(int sig) 15507c478bd9Sstevel@tonic-gate { 15517c478bd9Sstevel@tonic-gate /* 15527c478bd9Sstevel@tonic-gate * Ignore SIG{INT,TERM}, so we don't end up in an infinite loop, 15537c478bd9Sstevel@tonic-gate * then propagate the signal to our process group. 15547c478bd9Sstevel@tonic-gate */ 15557c478bd9Sstevel@tonic-gate (void) sigset(SIGINT, SIG_IGN); 15567c478bd9Sstevel@tonic-gate (void) sigset(SIGTERM, SIG_IGN); 15577c478bd9Sstevel@tonic-gate (void) kill(0, sig); 15587c478bd9Sstevel@tonic-gate child_killed = B_TRUE; 15597c478bd9Sstevel@tonic-gate } 15607c478bd9Sstevel@tonic-gate 15617c478bd9Sstevel@tonic-gate static int 15627c478bd9Sstevel@tonic-gate do_subproc(char *cmdbuf) 15637c478bd9Sstevel@tonic-gate { 15647c478bd9Sstevel@tonic-gate char inbuf[1024]; /* arbitrary large amount */ 15657c478bd9Sstevel@tonic-gate FILE *file; 15667c478bd9Sstevel@tonic-gate 15677c478bd9Sstevel@tonic-gate child_killed = B_FALSE; 15687c478bd9Sstevel@tonic-gate /* 15697c478bd9Sstevel@tonic-gate * We use popen(3c) to launch child processes for [un]install; 15707c478bd9Sstevel@tonic-gate * this library call does not return a PID, so we have to kill 15717c478bd9Sstevel@tonic-gate * the whole process group. To avoid killing our parent, we 15727c478bd9Sstevel@tonic-gate * become a process group leader here. But doing so can wreak 15737c478bd9Sstevel@tonic-gate * havoc with reading from stdin when launched by a non-job-control 15747c478bd9Sstevel@tonic-gate * shell, so we close stdin and reopen it as /dev/null first. 15757c478bd9Sstevel@tonic-gate */ 15767c478bd9Sstevel@tonic-gate (void) close(STDIN_FILENO); 15777c478bd9Sstevel@tonic-gate (void) open("/dev/null", O_RDONLY); 15787c478bd9Sstevel@tonic-gate (void) setpgid(0, 0); 15797c478bd9Sstevel@tonic-gate (void) sigset(SIGINT, sigterm); 15807c478bd9Sstevel@tonic-gate (void) sigset(SIGTERM, sigterm); 15817c478bd9Sstevel@tonic-gate file = popen(cmdbuf, "r"); 15827c478bd9Sstevel@tonic-gate for (;;) { 15837c478bd9Sstevel@tonic-gate if (child_killed || fgets(inbuf, sizeof (inbuf), file) == NULL) 15847c478bd9Sstevel@tonic-gate break; 15857c478bd9Sstevel@tonic-gate (void) fputs(inbuf, stdout); 15867c478bd9Sstevel@tonic-gate } 15877c478bd9Sstevel@tonic-gate (void) sigset(SIGINT, SIG_DFL); 15887c478bd9Sstevel@tonic-gate (void) sigset(SIGTERM, SIG_DFL); 15897c478bd9Sstevel@tonic-gate return (pclose(file)); 15907c478bd9Sstevel@tonic-gate } 15917c478bd9Sstevel@tonic-gate 15927c478bd9Sstevel@tonic-gate static int 15937c478bd9Sstevel@tonic-gate subproc_status(const char *cmd, int status) 15947c478bd9Sstevel@tonic-gate { 15957c478bd9Sstevel@tonic-gate if (WIFEXITED(status)) { 15967c478bd9Sstevel@tonic-gate int exit_code = WEXITSTATUS(status); 15977c478bd9Sstevel@tonic-gate 15987c478bd9Sstevel@tonic-gate if (exit_code == 0) 15997c478bd9Sstevel@tonic-gate return (Z_OK); 16007c478bd9Sstevel@tonic-gate zerror(gettext("'%s' failed with exit code %d."), cmd, 16017c478bd9Sstevel@tonic-gate exit_code); 16027c478bd9Sstevel@tonic-gate } else if (WIFSIGNALED(status)) { 16037c478bd9Sstevel@tonic-gate int signal = WTERMSIG(status); 16047c478bd9Sstevel@tonic-gate char sigstr[SIG2STR_MAX]; 16057c478bd9Sstevel@tonic-gate 16067c478bd9Sstevel@tonic-gate if (sig2str(signal, sigstr) == 0) { 16077c478bd9Sstevel@tonic-gate zerror(gettext("'%s' terminated by signal SIG%s."), cmd, 16087c478bd9Sstevel@tonic-gate sigstr); 16097c478bd9Sstevel@tonic-gate } else { 16107c478bd9Sstevel@tonic-gate zerror(gettext("'%s' terminated by an unknown signal."), 16117c478bd9Sstevel@tonic-gate cmd); 16127c478bd9Sstevel@tonic-gate } 16137c478bd9Sstevel@tonic-gate } else { 16147c478bd9Sstevel@tonic-gate zerror(gettext("'%s' failed for unknown reasons."), cmd); 16157c478bd9Sstevel@tonic-gate } 16167c478bd9Sstevel@tonic-gate return (Z_ERR); 16177c478bd9Sstevel@tonic-gate } 16187c478bd9Sstevel@tonic-gate 16197c478bd9Sstevel@tonic-gate /* 16207c478bd9Sstevel@tonic-gate * Various sanity checks; make sure: 16217c478bd9Sstevel@tonic-gate * 1. We're in the global zone. 16227c478bd9Sstevel@tonic-gate * 2. The calling user has sufficient privilege. 16237c478bd9Sstevel@tonic-gate * 3. The target zone is neither the global zone nor anything starting with 16247c478bd9Sstevel@tonic-gate * "SUNW". 16257c478bd9Sstevel@tonic-gate * 4a. If we're looking for a 'not running' (i.e., configured or installed) 16267c478bd9Sstevel@tonic-gate * zone, the name service knows about it. 16277c478bd9Sstevel@tonic-gate * 4b. For some operations which expect a zone not to be running, that it is 16287c478bd9Sstevel@tonic-gate * not already running (or ready). 16297c478bd9Sstevel@tonic-gate */ 16307c478bd9Sstevel@tonic-gate static int 16317c478bd9Sstevel@tonic-gate sanity_check(char *zone, int cmd_num, boolean_t running, 16327c478bd9Sstevel@tonic-gate boolean_t unsafe_when_running) 16337c478bd9Sstevel@tonic-gate { 16347c478bd9Sstevel@tonic-gate zone_entry_t *zent; 16357c478bd9Sstevel@tonic-gate priv_set_t *privset; 16367c478bd9Sstevel@tonic-gate zone_state_t state; 1637108322fbScarlsonj char kernzone[ZONENAME_MAX]; 1638108322fbScarlsonj FILE *fp; 16397c478bd9Sstevel@tonic-gate 16407c478bd9Sstevel@tonic-gate if (getzoneid() != GLOBAL_ZONEID) { 1641ffbafc53Scomay switch (cmd_num) { 1642ffbafc53Scomay case CMD_HALT: 1643ffbafc53Scomay zerror(gettext("use %s to %s this zone."), "halt(1M)", 16447c478bd9Sstevel@tonic-gate cmd_to_str(cmd_num)); 1645ffbafc53Scomay break; 1646ffbafc53Scomay case CMD_REBOOT: 1647ffbafc53Scomay zerror(gettext("use %s to %s this zone."), 1648ffbafc53Scomay "reboot(1M)", cmd_to_str(cmd_num)); 1649ffbafc53Scomay break; 1650ffbafc53Scomay default: 1651ffbafc53Scomay zerror(gettext("must be in the global zone to %s a " 1652ffbafc53Scomay "zone."), cmd_to_str(cmd_num)); 1653ffbafc53Scomay break; 1654ffbafc53Scomay } 16557c478bd9Sstevel@tonic-gate return (Z_ERR); 16567c478bd9Sstevel@tonic-gate } 16577c478bd9Sstevel@tonic-gate 16587c478bd9Sstevel@tonic-gate if ((privset = priv_allocset()) == NULL) { 16597c478bd9Sstevel@tonic-gate zerror(gettext("%s failed"), "priv_allocset"); 16607c478bd9Sstevel@tonic-gate return (Z_ERR); 16617c478bd9Sstevel@tonic-gate } 16627c478bd9Sstevel@tonic-gate 16637c478bd9Sstevel@tonic-gate if (getppriv(PRIV_EFFECTIVE, privset) != 0) { 16647c478bd9Sstevel@tonic-gate zerror(gettext("%s failed"), "getppriv"); 16657c478bd9Sstevel@tonic-gate priv_freeset(privset); 16667c478bd9Sstevel@tonic-gate return (Z_ERR); 16677c478bd9Sstevel@tonic-gate } 16687c478bd9Sstevel@tonic-gate 16697c478bd9Sstevel@tonic-gate if (priv_isfullset(privset) == B_FALSE) { 16707c478bd9Sstevel@tonic-gate zerror(gettext("only a privileged user may %s a zone."), 16717c478bd9Sstevel@tonic-gate cmd_to_str(cmd_num)); 16727c478bd9Sstevel@tonic-gate priv_freeset(privset); 16737c478bd9Sstevel@tonic-gate return (Z_ERR); 16747c478bd9Sstevel@tonic-gate } 16757c478bd9Sstevel@tonic-gate priv_freeset(privset); 16767c478bd9Sstevel@tonic-gate 16777c478bd9Sstevel@tonic-gate if (zone == NULL) { 16787c478bd9Sstevel@tonic-gate zerror(gettext("no zone specified")); 16797c478bd9Sstevel@tonic-gate return (Z_ERR); 16807c478bd9Sstevel@tonic-gate } 16817c478bd9Sstevel@tonic-gate 16827c478bd9Sstevel@tonic-gate if (strcmp(zone, GLOBAL_ZONENAME) == 0) { 16837c478bd9Sstevel@tonic-gate zerror(gettext("%s operation is invalid for the global zone."), 16847c478bd9Sstevel@tonic-gate cmd_to_str(cmd_num)); 16857c478bd9Sstevel@tonic-gate return (Z_ERR); 16867c478bd9Sstevel@tonic-gate } 16877c478bd9Sstevel@tonic-gate 16887c478bd9Sstevel@tonic-gate if (strncmp(zone, "SUNW", 4) == 0) { 16897c478bd9Sstevel@tonic-gate zerror(gettext("%s operation is invalid for zones starting " 16907c478bd9Sstevel@tonic-gate "with SUNW."), cmd_to_str(cmd_num)); 16917c478bd9Sstevel@tonic-gate return (Z_ERR); 16927c478bd9Sstevel@tonic-gate } 16937c478bd9Sstevel@tonic-gate 1694108322fbScarlsonj if (!zonecfg_in_alt_root()) { 1695108322fbScarlsonj zent = lookup_running_zone(zone); 1696108322fbScarlsonj } else if ((fp = zonecfg_open_scratch("", B_FALSE)) == NULL) { 1697108322fbScarlsonj zent = NULL; 1698108322fbScarlsonj } else { 1699108322fbScarlsonj if (zonecfg_find_scratch(fp, zone, zonecfg_get_root(), 1700108322fbScarlsonj kernzone, sizeof (kernzone)) == 0) 1701108322fbScarlsonj zent = lookup_running_zone(kernzone); 1702108322fbScarlsonj else 1703108322fbScarlsonj zent = NULL; 1704108322fbScarlsonj zonecfg_close_scratch(fp); 1705108322fbScarlsonj } 1706108322fbScarlsonj 17077c478bd9Sstevel@tonic-gate /* 17087c478bd9Sstevel@tonic-gate * Look up from the kernel for 'running' zones. 17097c478bd9Sstevel@tonic-gate */ 17107c478bd9Sstevel@tonic-gate if (running) { 17117c478bd9Sstevel@tonic-gate if (zent == NULL) { 17127c478bd9Sstevel@tonic-gate zerror(gettext("not running")); 17137c478bd9Sstevel@tonic-gate return (Z_ERR); 17147c478bd9Sstevel@tonic-gate } 17157c478bd9Sstevel@tonic-gate } else { 17167c478bd9Sstevel@tonic-gate int err; 17177c478bd9Sstevel@tonic-gate 17187c478bd9Sstevel@tonic-gate if (unsafe_when_running && zent != NULL) { 17197c478bd9Sstevel@tonic-gate /* check whether the zone is ready or running */ 17207c478bd9Sstevel@tonic-gate if ((err = zone_get_state(zent->zname, 17217c478bd9Sstevel@tonic-gate &zent->zstate_num)) != Z_OK) { 17227c478bd9Sstevel@tonic-gate errno = err; 17237c478bd9Sstevel@tonic-gate zperror2(zent->zname, 17247c478bd9Sstevel@tonic-gate gettext("could not get state")); 17257c478bd9Sstevel@tonic-gate /* can't tell, so hedge */ 17267c478bd9Sstevel@tonic-gate zent->zstate_str = "ready/running"; 17277c478bd9Sstevel@tonic-gate } else { 17287c478bd9Sstevel@tonic-gate zent->zstate_str = 17297c478bd9Sstevel@tonic-gate zone_state_str(zent->zstate_num); 17307c478bd9Sstevel@tonic-gate } 17317c478bd9Sstevel@tonic-gate zerror(gettext("%s operation is invalid for %s zones."), 17327c478bd9Sstevel@tonic-gate cmd_to_str(cmd_num), zent->zstate_str); 17337c478bd9Sstevel@tonic-gate return (Z_ERR); 17347c478bd9Sstevel@tonic-gate } 17357c478bd9Sstevel@tonic-gate if ((err = zone_get_state(zone, &state)) != Z_OK) { 17367c478bd9Sstevel@tonic-gate errno = err; 17377c478bd9Sstevel@tonic-gate zperror2(zone, gettext("could not get state")); 17387c478bd9Sstevel@tonic-gate return (Z_ERR); 17397c478bd9Sstevel@tonic-gate } 17407c478bd9Sstevel@tonic-gate switch (cmd_num) { 17417c478bd9Sstevel@tonic-gate case CMD_UNINSTALL: 17427c478bd9Sstevel@tonic-gate if (state == ZONE_STATE_CONFIGURED) { 17437c478bd9Sstevel@tonic-gate zerror(gettext("is already in state '%s'."), 17447c478bd9Sstevel@tonic-gate zone_state_str(ZONE_STATE_CONFIGURED)); 17457c478bd9Sstevel@tonic-gate return (Z_ERR); 17467c478bd9Sstevel@tonic-gate } 17477c478bd9Sstevel@tonic-gate break; 1748ee519a1fSgjelinek case CMD_ATTACH: 1749865e09a4Sgjelinek case CMD_CLONE: 17507c478bd9Sstevel@tonic-gate case CMD_INSTALL: 17517c478bd9Sstevel@tonic-gate if (state == ZONE_STATE_INSTALLED) { 17527c478bd9Sstevel@tonic-gate zerror(gettext("is already %s."), 17537c478bd9Sstevel@tonic-gate zone_state_str(ZONE_STATE_INSTALLED)); 17547c478bd9Sstevel@tonic-gate return (Z_ERR); 17557c478bd9Sstevel@tonic-gate } else if (state == ZONE_STATE_INCOMPLETE) { 17567c478bd9Sstevel@tonic-gate zerror(gettext("zone is %s; %s required."), 17577c478bd9Sstevel@tonic-gate zone_state_str(ZONE_STATE_INCOMPLETE), 17587c478bd9Sstevel@tonic-gate cmd_to_str(CMD_UNINSTALL)); 17597c478bd9Sstevel@tonic-gate return (Z_ERR); 17607c478bd9Sstevel@tonic-gate } 17617c478bd9Sstevel@tonic-gate break; 1762ee519a1fSgjelinek case CMD_DETACH: 1763865e09a4Sgjelinek case CMD_MOVE: 17647c478bd9Sstevel@tonic-gate case CMD_READY: 17657c478bd9Sstevel@tonic-gate case CMD_BOOT: 1766108322fbScarlsonj case CMD_MOUNT: 1767555afedfScarlsonj case CMD_MARK: 17687c478bd9Sstevel@tonic-gate if (state < ZONE_STATE_INSTALLED) { 17697c478bd9Sstevel@tonic-gate zerror(gettext("must be %s before %s."), 17707c478bd9Sstevel@tonic-gate zone_state_str(ZONE_STATE_INSTALLED), 17717c478bd9Sstevel@tonic-gate cmd_to_str(cmd_num)); 17727c478bd9Sstevel@tonic-gate return (Z_ERR); 17737c478bd9Sstevel@tonic-gate } 17747c478bd9Sstevel@tonic-gate break; 17757c478bd9Sstevel@tonic-gate case CMD_VERIFY: 17767c478bd9Sstevel@tonic-gate if (state == ZONE_STATE_INCOMPLETE) { 17777c478bd9Sstevel@tonic-gate zerror(gettext("zone is %s; %s required."), 17787c478bd9Sstevel@tonic-gate zone_state_str(ZONE_STATE_INCOMPLETE), 17797c478bd9Sstevel@tonic-gate cmd_to_str(CMD_UNINSTALL)); 17807c478bd9Sstevel@tonic-gate return (Z_ERR); 17817c478bd9Sstevel@tonic-gate } 17827c478bd9Sstevel@tonic-gate break; 1783108322fbScarlsonj case CMD_UNMOUNT: 1784108322fbScarlsonj if (state != ZONE_STATE_MOUNTED) { 1785108322fbScarlsonj zerror(gettext("must be %s before %s."), 1786108322fbScarlsonj zone_state_str(ZONE_STATE_MOUNTED), 1787108322fbScarlsonj cmd_to_str(cmd_num)); 1788108322fbScarlsonj return (Z_ERR); 1789108322fbScarlsonj } 1790108322fbScarlsonj break; 17917c478bd9Sstevel@tonic-gate } 17927c478bd9Sstevel@tonic-gate } 17937c478bd9Sstevel@tonic-gate return (Z_OK); 17947c478bd9Sstevel@tonic-gate } 17957c478bd9Sstevel@tonic-gate 17967c478bd9Sstevel@tonic-gate static int 17977c478bd9Sstevel@tonic-gate halt_func(int argc, char *argv[]) 17987c478bd9Sstevel@tonic-gate { 17997c478bd9Sstevel@tonic-gate zone_cmd_arg_t zarg; 18007c478bd9Sstevel@tonic-gate int arg; 18017c478bd9Sstevel@tonic-gate 1802108322fbScarlsonj if (zonecfg_in_alt_root()) { 1803108322fbScarlsonj zerror(gettext("cannot halt zone in alternate root")); 1804108322fbScarlsonj return (Z_ERR); 1805108322fbScarlsonj } 1806108322fbScarlsonj 18077c478bd9Sstevel@tonic-gate optind = 0; 18087c478bd9Sstevel@tonic-gate if ((arg = getopt(argc, argv, "?")) != EOF) { 18097c478bd9Sstevel@tonic-gate switch (arg) { 18107c478bd9Sstevel@tonic-gate case '?': 18117c478bd9Sstevel@tonic-gate sub_usage(SHELP_HALT, CMD_HALT); 18127c478bd9Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 18137c478bd9Sstevel@tonic-gate default: 18147c478bd9Sstevel@tonic-gate sub_usage(SHELP_HALT, CMD_HALT); 18157c478bd9Sstevel@tonic-gate return (Z_USAGE); 18167c478bd9Sstevel@tonic-gate } 18177c478bd9Sstevel@tonic-gate } 18187c478bd9Sstevel@tonic-gate if (argc > optind) { 18197c478bd9Sstevel@tonic-gate sub_usage(SHELP_HALT, CMD_HALT); 18207c478bd9Sstevel@tonic-gate return (Z_USAGE); 18217c478bd9Sstevel@tonic-gate } 18227c478bd9Sstevel@tonic-gate /* 18237c478bd9Sstevel@tonic-gate * zoneadmd should be the one to decide whether or not to proceed, 18247c478bd9Sstevel@tonic-gate * so even though it seems that the fourth parameter below should 18257c478bd9Sstevel@tonic-gate * perhaps be B_TRUE, it really shouldn't be. 18267c478bd9Sstevel@tonic-gate */ 18277c478bd9Sstevel@tonic-gate if (sanity_check(target_zone, CMD_HALT, B_FALSE, B_FALSE) != Z_OK) 18287c478bd9Sstevel@tonic-gate return (Z_ERR); 18297c478bd9Sstevel@tonic-gate 18307c478bd9Sstevel@tonic-gate zarg.cmd = Z_HALT; 18317c478bd9Sstevel@tonic-gate return ((call_zoneadmd(target_zone, &zarg) == 0) ? Z_OK : Z_ERR); 18327c478bd9Sstevel@tonic-gate } 18337c478bd9Sstevel@tonic-gate 18347c478bd9Sstevel@tonic-gate static int 18357c478bd9Sstevel@tonic-gate reboot_func(int argc, char *argv[]) 18367c478bd9Sstevel@tonic-gate { 18377c478bd9Sstevel@tonic-gate zone_cmd_arg_t zarg; 18387c478bd9Sstevel@tonic-gate int arg; 18397c478bd9Sstevel@tonic-gate 1840108322fbScarlsonj if (zonecfg_in_alt_root()) { 1841108322fbScarlsonj zerror(gettext("cannot reboot zone in alternate root")); 1842108322fbScarlsonj return (Z_ERR); 1843108322fbScarlsonj } 1844108322fbScarlsonj 18457c478bd9Sstevel@tonic-gate optind = 0; 18467c478bd9Sstevel@tonic-gate if ((arg = getopt(argc, argv, "?")) != EOF) { 18477c478bd9Sstevel@tonic-gate switch (arg) { 18487c478bd9Sstevel@tonic-gate case '?': 18497c478bd9Sstevel@tonic-gate sub_usage(SHELP_REBOOT, CMD_REBOOT); 18507c478bd9Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 18517c478bd9Sstevel@tonic-gate default: 18527c478bd9Sstevel@tonic-gate sub_usage(SHELP_REBOOT, CMD_REBOOT); 18537c478bd9Sstevel@tonic-gate return (Z_USAGE); 18547c478bd9Sstevel@tonic-gate } 18557c478bd9Sstevel@tonic-gate } 18563f2f09c1Sdp 18573f2f09c1Sdp zarg.bootbuf[0] = '\0'; 18583f2f09c1Sdp for (; optind < argc; optind++) { 18593f2f09c1Sdp if (strlcat(zarg.bootbuf, argv[optind], 18603f2f09c1Sdp sizeof (zarg.bootbuf)) >= sizeof (zarg.bootbuf)) { 18613f2f09c1Sdp zerror(gettext("Boot argument list too long")); 18623f2f09c1Sdp return (Z_ERR); 18637c478bd9Sstevel@tonic-gate } 18643f2f09c1Sdp if (optind < argc - 1) 18653f2f09c1Sdp if (strlcat(zarg.bootbuf, " ", sizeof (zarg.bootbuf)) >= 18663f2f09c1Sdp sizeof (zarg.bootbuf)) { 18673f2f09c1Sdp zerror(gettext("Boot argument list too long")); 18683f2f09c1Sdp return (Z_ERR); 18693f2f09c1Sdp } 18703f2f09c1Sdp } 18713f2f09c1Sdp 18723f2f09c1Sdp 18737c478bd9Sstevel@tonic-gate /* 18747c478bd9Sstevel@tonic-gate * zoneadmd should be the one to decide whether or not to proceed, 18757c478bd9Sstevel@tonic-gate * so even though it seems that the fourth parameter below should 18767c478bd9Sstevel@tonic-gate * perhaps be B_TRUE, it really shouldn't be. 18777c478bd9Sstevel@tonic-gate */ 18787c478bd9Sstevel@tonic-gate if (sanity_check(target_zone, CMD_REBOOT, B_TRUE, B_FALSE) != Z_OK) 18797c478bd9Sstevel@tonic-gate return (Z_ERR); 1880b5abaf04Sgjelinek if (verify_details(CMD_REBOOT) != Z_OK) 1881b5abaf04Sgjelinek return (Z_ERR); 18827c478bd9Sstevel@tonic-gate 18837c478bd9Sstevel@tonic-gate zarg.cmd = Z_REBOOT; 18847c478bd9Sstevel@tonic-gate return ((call_zoneadmd(target_zone, &zarg) == 0) ? Z_OK : Z_ERR); 18857c478bd9Sstevel@tonic-gate } 18867c478bd9Sstevel@tonic-gate 18877c478bd9Sstevel@tonic-gate static int 18887c478bd9Sstevel@tonic-gate verify_rctls(zone_dochandle_t handle) 18897c478bd9Sstevel@tonic-gate { 18907c478bd9Sstevel@tonic-gate struct zone_rctltab rctltab; 18917c478bd9Sstevel@tonic-gate size_t rbs = rctlblk_size(); 18927c478bd9Sstevel@tonic-gate rctlblk_t *rctlblk; 18937c478bd9Sstevel@tonic-gate int error = Z_INVAL; 18947c478bd9Sstevel@tonic-gate 18957c478bd9Sstevel@tonic-gate if ((rctlblk = malloc(rbs)) == NULL) { 18967c478bd9Sstevel@tonic-gate zerror(gettext("failed to allocate %lu bytes: %s"), rbs, 18977c478bd9Sstevel@tonic-gate strerror(errno)); 18987c478bd9Sstevel@tonic-gate return (Z_NOMEM); 18997c478bd9Sstevel@tonic-gate } 19007c478bd9Sstevel@tonic-gate 19017c478bd9Sstevel@tonic-gate if (zonecfg_setrctlent(handle) != Z_OK) { 19027c478bd9Sstevel@tonic-gate zerror(gettext("zonecfg_setrctlent failed")); 19037c478bd9Sstevel@tonic-gate free(rctlblk); 19047c478bd9Sstevel@tonic-gate return (error); 19057c478bd9Sstevel@tonic-gate } 19067c478bd9Sstevel@tonic-gate 19077c478bd9Sstevel@tonic-gate rctltab.zone_rctl_valptr = NULL; 19087c478bd9Sstevel@tonic-gate while (zonecfg_getrctlent(handle, &rctltab) == Z_OK) { 19097c478bd9Sstevel@tonic-gate struct zone_rctlvaltab *rctlval; 19107c478bd9Sstevel@tonic-gate const char *name = rctltab.zone_rctl_name; 19117c478bd9Sstevel@tonic-gate 19127c478bd9Sstevel@tonic-gate if (!zonecfg_is_rctl(name)) { 19137c478bd9Sstevel@tonic-gate zerror(gettext("WARNING: Ignoring unrecognized rctl " 19147c478bd9Sstevel@tonic-gate "'%s'."), name); 19157c478bd9Sstevel@tonic-gate zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr); 19167c478bd9Sstevel@tonic-gate rctltab.zone_rctl_valptr = NULL; 19177c478bd9Sstevel@tonic-gate continue; 19187c478bd9Sstevel@tonic-gate } 19197c478bd9Sstevel@tonic-gate 19207c478bd9Sstevel@tonic-gate for (rctlval = rctltab.zone_rctl_valptr; rctlval != NULL; 19217c478bd9Sstevel@tonic-gate rctlval = rctlval->zone_rctlval_next) { 19227c478bd9Sstevel@tonic-gate if (zonecfg_construct_rctlblk(rctlval, rctlblk) 19237c478bd9Sstevel@tonic-gate != Z_OK) { 19247c478bd9Sstevel@tonic-gate zerror(gettext("invalid rctl value: " 19257c478bd9Sstevel@tonic-gate "(priv=%s,limit=%s,action%s)"), 19267c478bd9Sstevel@tonic-gate rctlval->zone_rctlval_priv, 19277c478bd9Sstevel@tonic-gate rctlval->zone_rctlval_limit, 19287c478bd9Sstevel@tonic-gate rctlval->zone_rctlval_action); 19297c478bd9Sstevel@tonic-gate goto out; 19307c478bd9Sstevel@tonic-gate } 19317c478bd9Sstevel@tonic-gate if (!zonecfg_valid_rctl(name, rctlblk)) { 19327c478bd9Sstevel@tonic-gate zerror(gettext("(priv=%s,limit=%s,action=%s) " 19337c478bd9Sstevel@tonic-gate "is not a valid value for rctl '%s'"), 19347c478bd9Sstevel@tonic-gate rctlval->zone_rctlval_priv, 19357c478bd9Sstevel@tonic-gate rctlval->zone_rctlval_limit, 19367c478bd9Sstevel@tonic-gate rctlval->zone_rctlval_action, 19377c478bd9Sstevel@tonic-gate name); 19387c478bd9Sstevel@tonic-gate goto out; 19397c478bd9Sstevel@tonic-gate } 19407c478bd9Sstevel@tonic-gate } 19417c478bd9Sstevel@tonic-gate zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr); 19427c478bd9Sstevel@tonic-gate } 19437c478bd9Sstevel@tonic-gate rctltab.zone_rctl_valptr = NULL; 19447c478bd9Sstevel@tonic-gate error = Z_OK; 19457c478bd9Sstevel@tonic-gate out: 19467c478bd9Sstevel@tonic-gate zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr); 19477c478bd9Sstevel@tonic-gate (void) zonecfg_endrctlent(handle); 19487c478bd9Sstevel@tonic-gate free(rctlblk); 19497c478bd9Sstevel@tonic-gate return (error); 19507c478bd9Sstevel@tonic-gate } 19517c478bd9Sstevel@tonic-gate 19527c478bd9Sstevel@tonic-gate static int 19537c478bd9Sstevel@tonic-gate verify_pool(zone_dochandle_t handle) 19547c478bd9Sstevel@tonic-gate { 19557c478bd9Sstevel@tonic-gate char poolname[MAXPATHLEN]; 19567c478bd9Sstevel@tonic-gate pool_conf_t *poolconf; 19577c478bd9Sstevel@tonic-gate pool_t *pool; 19587c478bd9Sstevel@tonic-gate int status; 19597c478bd9Sstevel@tonic-gate int error; 19607c478bd9Sstevel@tonic-gate 19617c478bd9Sstevel@tonic-gate /* 19627c478bd9Sstevel@tonic-gate * This ends up being very similar to the check done in zoneadmd. 19637c478bd9Sstevel@tonic-gate */ 19647c478bd9Sstevel@tonic-gate error = zonecfg_get_pool(handle, poolname, sizeof (poolname)); 19657c478bd9Sstevel@tonic-gate if (error == Z_NO_ENTRY || (error == Z_OK && strlen(poolname) == 0)) { 19667c478bd9Sstevel@tonic-gate /* 19677c478bd9Sstevel@tonic-gate * No pool specified. 19687c478bd9Sstevel@tonic-gate */ 19697c478bd9Sstevel@tonic-gate return (0); 19707c478bd9Sstevel@tonic-gate } 19717c478bd9Sstevel@tonic-gate if (error != Z_OK) { 19727c478bd9Sstevel@tonic-gate zperror(gettext("Unable to retrieve pool name from " 19737c478bd9Sstevel@tonic-gate "configuration"), B_TRUE); 19747c478bd9Sstevel@tonic-gate return (error); 19757c478bd9Sstevel@tonic-gate } 19767c478bd9Sstevel@tonic-gate /* 19777c478bd9Sstevel@tonic-gate * Don't do anything if pools aren't enabled. 19787c478bd9Sstevel@tonic-gate */ 19797c478bd9Sstevel@tonic-gate if (pool_get_status(&status) != PO_SUCCESS || status != POOL_ENABLED) { 19807c478bd9Sstevel@tonic-gate zerror(gettext("WARNING: pools facility not active; " 19817c478bd9Sstevel@tonic-gate "zone will not be bound to pool '%s'."), poolname); 19827c478bd9Sstevel@tonic-gate return (Z_OK); 19837c478bd9Sstevel@tonic-gate } 19847c478bd9Sstevel@tonic-gate /* 19857c478bd9Sstevel@tonic-gate * Try to provide a sane error message if the requested pool doesn't 19867c478bd9Sstevel@tonic-gate * exist. It isn't clear that pools-related failures should 19877c478bd9Sstevel@tonic-gate * necessarily translate to a failure to verify the zone configuration, 19887c478bd9Sstevel@tonic-gate * hence they are not considered errors. 19897c478bd9Sstevel@tonic-gate */ 19907c478bd9Sstevel@tonic-gate if ((poolconf = pool_conf_alloc()) == NULL) { 19917c478bd9Sstevel@tonic-gate zerror(gettext("WARNING: pool_conf_alloc failed; " 19927c478bd9Sstevel@tonic-gate "using default pool")); 19937c478bd9Sstevel@tonic-gate return (Z_OK); 19947c478bd9Sstevel@tonic-gate } 19957c478bd9Sstevel@tonic-gate if (pool_conf_open(poolconf, pool_dynamic_location(), PO_RDONLY) != 19967c478bd9Sstevel@tonic-gate PO_SUCCESS) { 19977c478bd9Sstevel@tonic-gate zerror(gettext("WARNING: pool_conf_open failed; " 19987c478bd9Sstevel@tonic-gate "using default pool")); 19997c478bd9Sstevel@tonic-gate pool_conf_free(poolconf); 20007c478bd9Sstevel@tonic-gate return (Z_OK); 20017c478bd9Sstevel@tonic-gate } 20027c478bd9Sstevel@tonic-gate pool = pool_get_pool(poolconf, poolname); 20037c478bd9Sstevel@tonic-gate (void) pool_conf_close(poolconf); 20047c478bd9Sstevel@tonic-gate pool_conf_free(poolconf); 20057c478bd9Sstevel@tonic-gate if (pool == NULL) { 20067c478bd9Sstevel@tonic-gate zerror(gettext("WARNING: pool '%s' not found. " 20077c478bd9Sstevel@tonic-gate "using default pool"), poolname); 20087c478bd9Sstevel@tonic-gate } 20097c478bd9Sstevel@tonic-gate 20107c478bd9Sstevel@tonic-gate return (Z_OK); 20117c478bd9Sstevel@tonic-gate } 20127c478bd9Sstevel@tonic-gate 20137c478bd9Sstevel@tonic-gate static int 2014b5abaf04Sgjelinek verify_ipd(zone_dochandle_t handle) 2015b5abaf04Sgjelinek { 2016b5abaf04Sgjelinek int return_code = Z_OK; 2017b5abaf04Sgjelinek struct zone_fstab fstab; 2018b5abaf04Sgjelinek struct stat st; 2019b5abaf04Sgjelinek char specdir[MAXPATHLEN]; 2020b5abaf04Sgjelinek 2021b5abaf04Sgjelinek if (zonecfg_setipdent(handle) != Z_OK) { 20225ee84fbdSgjelinek /* 20235ee84fbdSgjelinek * TRANSLATION_NOTE 20245ee84fbdSgjelinek * inherit-pkg-dirs is a literal that should not be translated. 20255ee84fbdSgjelinek */ 20265ee84fbdSgjelinek (void) fprintf(stderr, gettext("could not verify " 2027b5abaf04Sgjelinek "inherit-pkg-dirs: unable to enumerate mounts\n")); 2028b5abaf04Sgjelinek return (Z_ERR); 2029b5abaf04Sgjelinek } 2030b5abaf04Sgjelinek while (zonecfg_getipdent(handle, &fstab) == Z_OK) { 2031b5abaf04Sgjelinek /* 2032b5abaf04Sgjelinek * Verify fs_dir exists. 2033b5abaf04Sgjelinek */ 2034b5abaf04Sgjelinek (void) snprintf(specdir, sizeof (specdir), "%s%s", 2035b5abaf04Sgjelinek zonecfg_get_root(), fstab.zone_fs_dir); 2036b5abaf04Sgjelinek if (stat(specdir, &st) != 0) { 20375ee84fbdSgjelinek /* 20385ee84fbdSgjelinek * TRANSLATION_NOTE 20395ee84fbdSgjelinek * inherit-pkg-dir is a literal that should not be 20405ee84fbdSgjelinek * translated. 20415ee84fbdSgjelinek */ 20425ee84fbdSgjelinek (void) fprintf(stderr, gettext("could not verify " 2043b5abaf04Sgjelinek "inherit-pkg-dir %s: %s\n"), 2044b5abaf04Sgjelinek fstab.zone_fs_dir, strerror(errno)); 2045b5abaf04Sgjelinek return_code = Z_ERR; 2046b5abaf04Sgjelinek } 2047b5abaf04Sgjelinek if (strcmp(st.st_fstype, MNTTYPE_NFS) == 0) { 20485ee84fbdSgjelinek /* 20495ee84fbdSgjelinek * TRANSLATION_NOTE 20505ee84fbdSgjelinek * inherit-pkg-dir and NFS are literals that should 20515ee84fbdSgjelinek * not be translated. 20525ee84fbdSgjelinek */ 2053b5abaf04Sgjelinek (void) fprintf(stderr, gettext("cannot verify " 20540b5de56dSgjelinek "inherit-pkg-dir %s: NFS mounted file system.\n" 20550b5de56dSgjelinek "\tA local file system must be used.\n"), 2056b5abaf04Sgjelinek fstab.zone_fs_dir); 2057b5abaf04Sgjelinek return_code = Z_ERR; 2058b5abaf04Sgjelinek } 2059b5abaf04Sgjelinek } 2060b5abaf04Sgjelinek (void) zonecfg_endipdent(handle); 2061b5abaf04Sgjelinek 2062b5abaf04Sgjelinek return (return_code); 2063b5abaf04Sgjelinek } 2064b5abaf04Sgjelinek 206520c8013fSlling /* 206620c8013fSlling * Verify that the special device/file system exists and is valid. 206720c8013fSlling */ 206820c8013fSlling static int 206920c8013fSlling verify_fs_special(struct zone_fstab *fstab) 207020c8013fSlling { 207120c8013fSlling struct stat st; 207220c8013fSlling 207320c8013fSlling if (strcmp(fstab->zone_fs_type, MNTTYPE_ZFS) == 0) 207420c8013fSlling return (verify_fs_zfs(fstab)); 207520c8013fSlling 207620c8013fSlling if (stat(fstab->zone_fs_special, &st) != 0) { 20775c358068Slling (void) fprintf(stderr, gettext("could not verify fs " 207820c8013fSlling "%s: could not access %s: %s\n"), fstab->zone_fs_dir, 207920c8013fSlling fstab->zone_fs_special, strerror(errno)); 208020c8013fSlling return (Z_ERR); 208120c8013fSlling } 208220c8013fSlling 208320c8013fSlling if (strcmp(st.st_fstype, MNTTYPE_NFS) == 0) { 208420c8013fSlling /* 208520c8013fSlling * TRANSLATION_NOTE 208620c8013fSlling * fs and NFS are literals that should 208720c8013fSlling * not be translated. 208820c8013fSlling */ 208920c8013fSlling (void) fprintf(stderr, gettext("cannot verify " 20900b5de56dSgjelinek "fs %s: NFS mounted file system.\n" 20910b5de56dSgjelinek "\tA local file system must be used.\n"), 209220c8013fSlling fstab->zone_fs_special); 209320c8013fSlling return (Z_ERR); 209420c8013fSlling } 209520c8013fSlling 209620c8013fSlling return (Z_OK); 209720c8013fSlling } 209820c8013fSlling 2099b5abaf04Sgjelinek static int 21007c478bd9Sstevel@tonic-gate verify_filesystems(zone_dochandle_t handle) 21017c478bd9Sstevel@tonic-gate { 21027c478bd9Sstevel@tonic-gate int return_code = Z_OK; 21037c478bd9Sstevel@tonic-gate struct zone_fstab fstab; 21047c478bd9Sstevel@tonic-gate char cmdbuf[MAXPATHLEN]; 21057c478bd9Sstevel@tonic-gate struct stat st; 21067c478bd9Sstevel@tonic-gate 21077c478bd9Sstevel@tonic-gate /* 21087c478bd9Sstevel@tonic-gate * No need to verify inherit-pkg-dir fs types, as their type is 21097c478bd9Sstevel@tonic-gate * implicitly lofs, which is known. Therefore, the types are only 21107c478bd9Sstevel@tonic-gate * verified for regular file systems below. 21117c478bd9Sstevel@tonic-gate * 21127c478bd9Sstevel@tonic-gate * Since the actual mount point is not known until the dependent mounts 21137c478bd9Sstevel@tonic-gate * are performed, we don't attempt any path validation here: that will 21147c478bd9Sstevel@tonic-gate * happen later when zoneadmd actually does the mounts. 21157c478bd9Sstevel@tonic-gate */ 21167c478bd9Sstevel@tonic-gate if (zonecfg_setfsent(handle) != Z_OK) { 21170b5de56dSgjelinek (void) fprintf(stderr, gettext("could not verify file systems: " 21187c478bd9Sstevel@tonic-gate "unable to enumerate mounts\n")); 21197c478bd9Sstevel@tonic-gate return (Z_ERR); 21207c478bd9Sstevel@tonic-gate } 21217c478bd9Sstevel@tonic-gate while (zonecfg_getfsent(handle, &fstab) == Z_OK) { 21227c478bd9Sstevel@tonic-gate if (!zonecfg_valid_fs_type(fstab.zone_fs_type)) { 21237c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("cannot verify fs %s: " 21247c478bd9Sstevel@tonic-gate "type %s is not allowed.\n"), fstab.zone_fs_dir, 21257c478bd9Sstevel@tonic-gate fstab.zone_fs_type); 21267c478bd9Sstevel@tonic-gate return_code = Z_ERR; 21277c478bd9Sstevel@tonic-gate goto next_fs; 21287c478bd9Sstevel@tonic-gate } 21297c478bd9Sstevel@tonic-gate /* 21307c478bd9Sstevel@tonic-gate * Verify /usr/lib/fs/<fstype>/mount exists. 21317c478bd9Sstevel@tonic-gate */ 21327c478bd9Sstevel@tonic-gate if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/mount", 21337c478bd9Sstevel@tonic-gate fstab.zone_fs_type) > sizeof (cmdbuf)) { 21347c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("cannot verify fs %s: " 21357c478bd9Sstevel@tonic-gate "type %s is too long.\n"), fstab.zone_fs_dir, 21367c478bd9Sstevel@tonic-gate fstab.zone_fs_type); 21377c478bd9Sstevel@tonic-gate return_code = Z_ERR; 21387c478bd9Sstevel@tonic-gate goto next_fs; 21397c478bd9Sstevel@tonic-gate } 21407c478bd9Sstevel@tonic-gate if (stat(cmdbuf, &st) != 0) { 21415ee84fbdSgjelinek (void) fprintf(stderr, gettext("could not verify fs " 21425ee84fbdSgjelinek "%s: could not access %s: %s\n"), fstab.zone_fs_dir, 21437c478bd9Sstevel@tonic-gate cmdbuf, strerror(errno)); 21447c478bd9Sstevel@tonic-gate return_code = Z_ERR; 21457c478bd9Sstevel@tonic-gate goto next_fs; 21467c478bd9Sstevel@tonic-gate } 21477c478bd9Sstevel@tonic-gate if (!S_ISREG(st.st_mode)) { 21485ee84fbdSgjelinek (void) fprintf(stderr, gettext("could not verify fs " 21495ee84fbdSgjelinek "%s: %s is not a regular file\n"), 21505ee84fbdSgjelinek fstab.zone_fs_dir, cmdbuf); 21517c478bd9Sstevel@tonic-gate return_code = Z_ERR; 21527c478bd9Sstevel@tonic-gate goto next_fs; 21537c478bd9Sstevel@tonic-gate } 21547c478bd9Sstevel@tonic-gate /* 21557c478bd9Sstevel@tonic-gate * Verify /usr/lib/fs/<fstype>/fsck exists iff zone_fs_raw is 21567c478bd9Sstevel@tonic-gate * set. 21577c478bd9Sstevel@tonic-gate */ 21587c478bd9Sstevel@tonic-gate if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/fsck", 21597c478bd9Sstevel@tonic-gate fstab.zone_fs_type) > sizeof (cmdbuf)) { 21607c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("cannot verify fs %s: " 21617c478bd9Sstevel@tonic-gate "type %s is too long.\n"), fstab.zone_fs_dir, 21627c478bd9Sstevel@tonic-gate fstab.zone_fs_type); 21637c478bd9Sstevel@tonic-gate return_code = Z_ERR; 21647c478bd9Sstevel@tonic-gate goto next_fs; 21657c478bd9Sstevel@tonic-gate } 21667c478bd9Sstevel@tonic-gate if (fstab.zone_fs_raw[0] == '\0' && stat(cmdbuf, &st) == 0) { 21675ee84fbdSgjelinek (void) fprintf(stderr, gettext("could not verify fs " 21685ee84fbdSgjelinek "%s: must specify 'raw' device for %s " 21690b5de56dSgjelinek "file systems\n"), 21707c478bd9Sstevel@tonic-gate fstab.zone_fs_dir, fstab.zone_fs_type); 21717c478bd9Sstevel@tonic-gate return_code = Z_ERR; 21727c478bd9Sstevel@tonic-gate goto next_fs; 21737c478bd9Sstevel@tonic-gate } 21747c478bd9Sstevel@tonic-gate if (fstab.zone_fs_raw[0] != '\0' && 21757c478bd9Sstevel@tonic-gate (stat(cmdbuf, &st) != 0 || !S_ISREG(st.st_mode))) { 21767c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("cannot verify fs %s: " 21777c478bd9Sstevel@tonic-gate "'raw' device specified but " 21787c478bd9Sstevel@tonic-gate "no fsck executable exists for %s\n"), 21797c478bd9Sstevel@tonic-gate fstab.zone_fs_dir, fstab.zone_fs_type); 21807c478bd9Sstevel@tonic-gate return_code = Z_ERR; 21817c478bd9Sstevel@tonic-gate goto next_fs; 21827c478bd9Sstevel@tonic-gate } 218320c8013fSlling 218420c8013fSlling /* Verify fs_special. */ 218520c8013fSlling if ((return_code = verify_fs_special(&fstab)) != Z_OK) 2186b5abaf04Sgjelinek goto next_fs; 218720c8013fSlling 218820c8013fSlling /* Verify fs_raw. */ 2189b5abaf04Sgjelinek if (fstab.zone_fs_raw[0] != '\0' && 2190b5abaf04Sgjelinek stat(fstab.zone_fs_raw, &st) != 0) { 21915ee84fbdSgjelinek /* 21925ee84fbdSgjelinek * TRANSLATION_NOTE 21935ee84fbdSgjelinek * fs is a literal that should not be translated. 21945ee84fbdSgjelinek */ 21955ee84fbdSgjelinek (void) fprintf(stderr, gettext("could not verify fs " 21965ee84fbdSgjelinek "%s: could not access %s: %s\n"), fstab.zone_fs_dir, 2197b5abaf04Sgjelinek fstab.zone_fs_raw, strerror(errno)); 2198b5abaf04Sgjelinek return_code = Z_ERR; 2199b5abaf04Sgjelinek goto next_fs; 2200b5abaf04Sgjelinek } 22017c478bd9Sstevel@tonic-gate next_fs: 22027c478bd9Sstevel@tonic-gate zonecfg_free_fs_option_list(fstab.zone_fs_options); 22037c478bd9Sstevel@tonic-gate } 22047c478bd9Sstevel@tonic-gate (void) zonecfg_endfsent(handle); 22057c478bd9Sstevel@tonic-gate 22067c478bd9Sstevel@tonic-gate return (return_code); 22077c478bd9Sstevel@tonic-gate } 22087c478bd9Sstevel@tonic-gate 22097c478bd9Sstevel@tonic-gate static int 2210ffbafc53Scomay verify_limitpriv(zone_dochandle_t handle) 2211ffbafc53Scomay { 2212ffbafc53Scomay char *privname = NULL; 2213ffbafc53Scomay int err; 2214ffbafc53Scomay priv_set_t *privs; 2215ffbafc53Scomay 2216ffbafc53Scomay if ((privs = priv_allocset()) == NULL) { 2217ffbafc53Scomay zperror(gettext("failed to allocate privilege set"), B_FALSE); 2218ffbafc53Scomay return (Z_NOMEM); 2219ffbafc53Scomay } 2220ffbafc53Scomay err = zonecfg_get_privset(handle, privs, &privname); 2221ffbafc53Scomay switch (err) { 2222ffbafc53Scomay case Z_OK: 2223ffbafc53Scomay break; 2224ffbafc53Scomay case Z_PRIV_PROHIBITED: 2225ffbafc53Scomay (void) fprintf(stderr, gettext("privilege \"%s\" is not " 2226ffbafc53Scomay "permitted within the zone's privilege set\n"), privname); 2227ffbafc53Scomay break; 2228ffbafc53Scomay case Z_PRIV_REQUIRED: 2229ffbafc53Scomay (void) fprintf(stderr, gettext("required privilege \"%s\" is " 2230ffbafc53Scomay "missing from the zone's privilege set\n"), privname); 2231ffbafc53Scomay break; 2232ffbafc53Scomay case Z_PRIV_UNKNOWN: 2233ffbafc53Scomay (void) fprintf(stderr, gettext("unknown privilege \"%s\" " 2234ffbafc53Scomay "specified in the zone's privilege set\n"), privname); 2235ffbafc53Scomay break; 2236ffbafc53Scomay default: 2237ffbafc53Scomay zperror( 2238ffbafc53Scomay gettext("failed to determine the zone's privilege set"), 2239ffbafc53Scomay B_TRUE); 2240ffbafc53Scomay break; 2241ffbafc53Scomay } 2242ffbafc53Scomay free(privname); 2243ffbafc53Scomay priv_freeset(privs); 2244ffbafc53Scomay return (err); 2245ffbafc53Scomay } 2246ffbafc53Scomay 22471390a385Sgjelinek static void 22481390a385Sgjelinek free_local_netifs(int if_cnt, struct net_if **if_list) 22491390a385Sgjelinek { 22501390a385Sgjelinek int i; 22511390a385Sgjelinek 22521390a385Sgjelinek for (i = 0; i < if_cnt; i++) { 22531390a385Sgjelinek free(if_list[i]->name); 22541390a385Sgjelinek free(if_list[i]); 22551390a385Sgjelinek } 22561390a385Sgjelinek free(if_list); 22571390a385Sgjelinek } 22581390a385Sgjelinek 22591390a385Sgjelinek /* 22601390a385Sgjelinek * Get a list of the network interfaces, along with their address families, 22611390a385Sgjelinek * that are plumbed in the global zone. See if_tcp(7p) for a description 22621390a385Sgjelinek * of the ioctls used here. 22631390a385Sgjelinek */ 22641390a385Sgjelinek static int 22651390a385Sgjelinek get_local_netifs(int *if_cnt, struct net_if ***if_list) 22661390a385Sgjelinek { 22671390a385Sgjelinek int s; 22681390a385Sgjelinek int i; 22691390a385Sgjelinek int res = Z_OK; 22701390a385Sgjelinek int space_needed; 22711390a385Sgjelinek int cnt = 0; 22721390a385Sgjelinek struct lifnum if_num; 22731390a385Sgjelinek struct lifconf if_conf; 22741390a385Sgjelinek struct lifreq *if_reqp; 22751390a385Sgjelinek char *if_buf; 22761390a385Sgjelinek struct net_if **local_ifs = NULL; 22771390a385Sgjelinek 22781390a385Sgjelinek *if_cnt = 0; 22791390a385Sgjelinek *if_list = NULL; 22801390a385Sgjelinek 22811390a385Sgjelinek if ((s = socket(SOCKET_AF(AF_INET), SOCK_DGRAM, 0)) < 0) 22821390a385Sgjelinek return (Z_ERR); 22831390a385Sgjelinek 22841390a385Sgjelinek /* 22851390a385Sgjelinek * Come back here in the unlikely event that the number of interfaces 22861390a385Sgjelinek * increases between the time we get the count and the time we do the 22871390a385Sgjelinek * SIOCGLIFCONF ioctl. 22881390a385Sgjelinek */ 22891390a385Sgjelinek retry: 22901390a385Sgjelinek /* Get the number of interfaces. */ 22911390a385Sgjelinek if_num.lifn_family = AF_UNSPEC; 22921390a385Sgjelinek if_num.lifn_flags = LIFC_NOXMIT; 22931390a385Sgjelinek if (ioctl(s, SIOCGLIFNUM, &if_num) < 0) { 22941390a385Sgjelinek (void) close(s); 22951390a385Sgjelinek return (Z_ERR); 22961390a385Sgjelinek } 22971390a385Sgjelinek 22981390a385Sgjelinek /* Get the interface configuration list. */ 22991390a385Sgjelinek space_needed = if_num.lifn_count * sizeof (struct lifreq); 23001390a385Sgjelinek if ((if_buf = malloc(space_needed)) == NULL) { 23011390a385Sgjelinek (void) close(s); 23021390a385Sgjelinek return (Z_ERR); 23031390a385Sgjelinek } 23041390a385Sgjelinek if_conf.lifc_family = AF_UNSPEC; 23051390a385Sgjelinek if_conf.lifc_flags = LIFC_NOXMIT; 23061390a385Sgjelinek if_conf.lifc_len = space_needed; 23071390a385Sgjelinek if_conf.lifc_buf = if_buf; 23081390a385Sgjelinek if (ioctl(s, SIOCGLIFCONF, &if_conf) < 0) { 23091390a385Sgjelinek free(if_buf); 23101390a385Sgjelinek /* 23111390a385Sgjelinek * SIOCGLIFCONF returns EINVAL if the buffer we passed in is 23121390a385Sgjelinek * too small. In this case go back and get the new if cnt. 23131390a385Sgjelinek */ 23141390a385Sgjelinek if (errno == EINVAL) 23151390a385Sgjelinek goto retry; 23161390a385Sgjelinek 23171390a385Sgjelinek (void) close(s); 23181390a385Sgjelinek return (Z_ERR); 23191390a385Sgjelinek } 23201390a385Sgjelinek (void) close(s); 23211390a385Sgjelinek 23221390a385Sgjelinek /* Get the name and address family for each interface. */ 23231390a385Sgjelinek if_reqp = if_conf.lifc_req; 23241390a385Sgjelinek for (i = 0; i < (if_conf.lifc_len / sizeof (struct lifreq)); i++) { 23251390a385Sgjelinek struct net_if **p; 23261390a385Sgjelinek struct lifreq req; 23271390a385Sgjelinek 23281390a385Sgjelinek if (strcmp(LOOPBACK_IF, if_reqp->lifr_name) == 0) { 23291390a385Sgjelinek if_reqp++; 23301390a385Sgjelinek continue; 23311390a385Sgjelinek } 23321390a385Sgjelinek 23331390a385Sgjelinek if ((s = socket(SOCKET_AF(if_reqp->lifr_addr.ss_family), 23341390a385Sgjelinek SOCK_DGRAM, 0)) == -1) { 23351390a385Sgjelinek res = Z_ERR; 23361390a385Sgjelinek break; 23371390a385Sgjelinek } 23381390a385Sgjelinek 23391390a385Sgjelinek (void) strncpy(req.lifr_name, if_reqp->lifr_name, 23401390a385Sgjelinek sizeof (req.lifr_name)); 23411390a385Sgjelinek if (ioctl(s, SIOCGLIFADDR, &req) < 0) { 23421390a385Sgjelinek (void) close(s); 23431390a385Sgjelinek if_reqp++; 23441390a385Sgjelinek continue; 23451390a385Sgjelinek } 23461390a385Sgjelinek 23471390a385Sgjelinek if ((p = (struct net_if **)realloc(local_ifs, 23481390a385Sgjelinek sizeof (struct net_if *) * (cnt + 1))) == NULL) { 23491390a385Sgjelinek res = Z_ERR; 23501390a385Sgjelinek break; 23511390a385Sgjelinek } 23521390a385Sgjelinek local_ifs = p; 23531390a385Sgjelinek 23541390a385Sgjelinek if ((local_ifs[cnt] = malloc(sizeof (struct net_if))) == NULL) { 23551390a385Sgjelinek res = Z_ERR; 23561390a385Sgjelinek break; 23571390a385Sgjelinek } 23581390a385Sgjelinek 23591390a385Sgjelinek if ((local_ifs[cnt]->name = strdup(if_reqp->lifr_name)) 23601390a385Sgjelinek == NULL) { 23611390a385Sgjelinek free(local_ifs[cnt]); 23621390a385Sgjelinek res = Z_ERR; 23631390a385Sgjelinek break; 23641390a385Sgjelinek } 23651390a385Sgjelinek local_ifs[cnt]->af = req.lifr_addr.ss_family; 23661390a385Sgjelinek cnt++; 23671390a385Sgjelinek 23681390a385Sgjelinek (void) close(s); 23691390a385Sgjelinek if_reqp++; 23701390a385Sgjelinek } 23711390a385Sgjelinek 23721390a385Sgjelinek free(if_buf); 23731390a385Sgjelinek 23741390a385Sgjelinek if (res != Z_OK) { 23751390a385Sgjelinek free_local_netifs(cnt, local_ifs); 23761390a385Sgjelinek } else { 23771390a385Sgjelinek *if_cnt = cnt; 23781390a385Sgjelinek *if_list = local_ifs; 23791390a385Sgjelinek } 23801390a385Sgjelinek 23811390a385Sgjelinek return (res); 23821390a385Sgjelinek } 23831390a385Sgjelinek 23841390a385Sgjelinek static char * 23851390a385Sgjelinek af2str(int af) 23861390a385Sgjelinek { 23871390a385Sgjelinek switch (af) { 23881390a385Sgjelinek case AF_INET: 23891390a385Sgjelinek return ("IPv4"); 23901390a385Sgjelinek case AF_INET6: 23911390a385Sgjelinek return ("IPv6"); 23921390a385Sgjelinek default: 23931390a385Sgjelinek return ("Unknown"); 23941390a385Sgjelinek } 23951390a385Sgjelinek } 23961390a385Sgjelinek 23971390a385Sgjelinek /* 23981390a385Sgjelinek * Cross check the network interface name and address family with the 23991390a385Sgjelinek * interfaces that are set up in the global zone so that we can print the 24001390a385Sgjelinek * appropriate error message. 24011390a385Sgjelinek */ 24021390a385Sgjelinek static void 24031390a385Sgjelinek print_net_err(char *phys, char *addr, int af, char *msg) 24041390a385Sgjelinek { 24051390a385Sgjelinek int i; 24061390a385Sgjelinek int local_if_cnt = 0; 24071390a385Sgjelinek struct net_if **local_ifs = NULL; 24081390a385Sgjelinek boolean_t found_if = B_FALSE; 24091390a385Sgjelinek boolean_t found_af = B_FALSE; 24101390a385Sgjelinek 24111390a385Sgjelinek if (get_local_netifs(&local_if_cnt, &local_ifs) != Z_OK) { 24121390a385Sgjelinek (void) fprintf(stderr, 24131390a385Sgjelinek gettext("could not verify %s %s=%s %s=%s\n\t%s\n"), 24141390a385Sgjelinek "net", "address", addr, "physical", phys, msg); 24151390a385Sgjelinek return; 24161390a385Sgjelinek } 24171390a385Sgjelinek 24181390a385Sgjelinek for (i = 0; i < local_if_cnt; i++) { 24191390a385Sgjelinek if (strcmp(phys, local_ifs[i]->name) == 0) { 24201390a385Sgjelinek found_if = B_TRUE; 24211390a385Sgjelinek if (af == local_ifs[i]->af) { 24221390a385Sgjelinek found_af = B_TRUE; 24231390a385Sgjelinek break; 24241390a385Sgjelinek } 24251390a385Sgjelinek } 24261390a385Sgjelinek } 24271390a385Sgjelinek 24281390a385Sgjelinek free_local_netifs(local_if_cnt, local_ifs); 24291390a385Sgjelinek 24301390a385Sgjelinek if (!found_if) { 24311390a385Sgjelinek (void) fprintf(stderr, 24321390a385Sgjelinek gettext("could not verify %s %s=%s\n\t" 24331390a385Sgjelinek "network interface %s is not plumbed in the global zone\n"), 24341390a385Sgjelinek "net", "physical", phys, phys); 24351390a385Sgjelinek return; 24361390a385Sgjelinek } 24371390a385Sgjelinek 24381390a385Sgjelinek /* 24391390a385Sgjelinek * Print this error if we were unable to find the address family 24401390a385Sgjelinek * for this interface. If the af variable is not initialized to 24411390a385Sgjelinek * to something meaningful by the caller (not AF_UNSPEC) then we 24421390a385Sgjelinek * also skip this message since it wouldn't be informative. 24431390a385Sgjelinek */ 24441390a385Sgjelinek if (!found_af && af != AF_UNSPEC) { 24451390a385Sgjelinek (void) fprintf(stderr, 24461390a385Sgjelinek gettext("could not verify %s %s=%s %s=%s\n\tthe %s address " 24471390a385Sgjelinek "family is not configured on this interface in the\n\t" 24481390a385Sgjelinek "global zone\n"), 24491390a385Sgjelinek "net", "address", addr, "physical", phys, af2str(af)); 24501390a385Sgjelinek return; 24511390a385Sgjelinek } 24521390a385Sgjelinek 24531390a385Sgjelinek (void) fprintf(stderr, 24541390a385Sgjelinek gettext("could not verify %s %s=%s %s=%s\n\t%s\n"), 24551390a385Sgjelinek "net", "address", addr, "physical", phys, msg); 24561390a385Sgjelinek } 24571390a385Sgjelinek 2458ffbafc53Scomay static int 24598cd327d5Sgjelinek verify_handle(int cmd_num, zone_dochandle_t handle) 24607c478bd9Sstevel@tonic-gate { 24617c478bd9Sstevel@tonic-gate struct zone_nwiftab nwiftab; 24627c478bd9Sstevel@tonic-gate int return_code = Z_OK; 24637c478bd9Sstevel@tonic-gate int err; 2464108322fbScarlsonj boolean_t in_alt_root; 24657c478bd9Sstevel@tonic-gate 2466108322fbScarlsonj in_alt_root = zonecfg_in_alt_root(); 2467108322fbScarlsonj if (in_alt_root) 2468108322fbScarlsonj goto no_net; 2469108322fbScarlsonj 24707c478bd9Sstevel@tonic-gate if ((err = zonecfg_setnwifent(handle)) != Z_OK) { 24717c478bd9Sstevel@tonic-gate errno = err; 24727c478bd9Sstevel@tonic-gate zperror(cmd_to_str(cmd_num), B_TRUE); 24737c478bd9Sstevel@tonic-gate zonecfg_fini_handle(handle); 24747c478bd9Sstevel@tonic-gate return (Z_ERR); 24757c478bd9Sstevel@tonic-gate } 24767c478bd9Sstevel@tonic-gate while (zonecfg_getnwifent(handle, &nwiftab) == Z_OK) { 24777c478bd9Sstevel@tonic-gate struct lifreq lifr; 24781390a385Sgjelinek sa_family_t af = AF_UNSPEC; 24797c478bd9Sstevel@tonic-gate int so, res; 24807c478bd9Sstevel@tonic-gate 24817c478bd9Sstevel@tonic-gate /* skip any loopback interfaces */ 24827c478bd9Sstevel@tonic-gate if (strcmp(nwiftab.zone_nwif_physical, "lo0") == 0) 24837c478bd9Sstevel@tonic-gate continue; 24847c478bd9Sstevel@tonic-gate if ((res = zonecfg_valid_net_address(nwiftab.zone_nwif_address, 24857c478bd9Sstevel@tonic-gate &lifr)) != Z_OK) { 24861390a385Sgjelinek print_net_err(nwiftab.zone_nwif_physical, 24871390a385Sgjelinek nwiftab.zone_nwif_address, af, 24881390a385Sgjelinek zonecfg_strerror(res)); 24897c478bd9Sstevel@tonic-gate return_code = Z_ERR; 24907c478bd9Sstevel@tonic-gate continue; 24917c478bd9Sstevel@tonic-gate } 24927c478bd9Sstevel@tonic-gate af = lifr.lifr_addr.ss_family; 24937c478bd9Sstevel@tonic-gate (void) memset(&lifr, 0, sizeof (lifr)); 24947c478bd9Sstevel@tonic-gate (void) strlcpy(lifr.lifr_name, nwiftab.zone_nwif_physical, 24957c478bd9Sstevel@tonic-gate sizeof (lifr.lifr_name)); 24967c478bd9Sstevel@tonic-gate lifr.lifr_addr.ss_family = af; 24977c478bd9Sstevel@tonic-gate if ((so = socket(af, SOCK_DGRAM, 0)) < 0) { 24987c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("could not verify %s " 24997c478bd9Sstevel@tonic-gate "%s=%s %s=%s: could not get socket: %s\n"), "net", 25007c478bd9Sstevel@tonic-gate "address", nwiftab.zone_nwif_address, "physical", 25017c478bd9Sstevel@tonic-gate nwiftab.zone_nwif_physical, strerror(errno)); 25027c478bd9Sstevel@tonic-gate return_code = Z_ERR; 25037c478bd9Sstevel@tonic-gate continue; 25047c478bd9Sstevel@tonic-gate } 25051390a385Sgjelinek if (ioctl(so, SIOCGLIFFLAGS, &lifr) < 0) { 2506*22321485Svp157776 /* 2507*22321485Svp157776 * The interface failed to come up. We continue on 2508*22321485Svp157776 * anyway for the sake of consistency: a zone is not 2509*22321485Svp157776 * shut down if the interface fails any time after 2510*22321485Svp157776 * boot, nor does the global zone fail to boot if an 2511*22321485Svp157776 * interface fails. 2512*22321485Svp157776 */ 2513*22321485Svp157776 (void) fprintf(stderr, 2514*22321485Svp157776 gettext("WARNING: skipping interface '%s' which " 2515*22321485Svp157776 "may not be present/plumbed in the global zone.\n"), 2516*22321485Svp157776 nwiftab.zone_nwif_physical); 2517*22321485Svp157776 25187c478bd9Sstevel@tonic-gate } 25197c478bd9Sstevel@tonic-gate (void) close(so); 25207c478bd9Sstevel@tonic-gate } 25217c478bd9Sstevel@tonic-gate (void) zonecfg_endnwifent(handle); 2522108322fbScarlsonj no_net: 25237c478bd9Sstevel@tonic-gate 2524e7f3c547Sgjelinek /* verify that lofs has not been excluded from the kernel */ 25258cd327d5Sgjelinek if (!(cmd_num == CMD_DETACH || cmd_num == CMD_ATTACH || 25268cd327d5Sgjelinek cmd_num == CMD_MOVE || cmd_num == CMD_CLONE) && 25278cd327d5Sgjelinek modctl(MODLOAD, 1, "fs/lofs", NULL) != 0) { 2528e7f3c547Sgjelinek if (errno == ENXIO) 2529e7f3c547Sgjelinek (void) fprintf(stderr, gettext("could not verify " 2530e7f3c547Sgjelinek "lofs(7FS): possibly excluded in /etc/system\n")); 2531e7f3c547Sgjelinek else 2532e7f3c547Sgjelinek (void) fprintf(stderr, gettext("could not verify " 2533e7f3c547Sgjelinek "lofs(7FS): %s\n"), strerror(errno)); 2534e7f3c547Sgjelinek return_code = Z_ERR; 2535e7f3c547Sgjelinek } 2536e7f3c547Sgjelinek 25377c478bd9Sstevel@tonic-gate if (verify_filesystems(handle) != Z_OK) 25387c478bd9Sstevel@tonic-gate return_code = Z_ERR; 2539b5abaf04Sgjelinek if (verify_ipd(handle) != Z_OK) 2540b5abaf04Sgjelinek return_code = Z_ERR; 2541108322fbScarlsonj if (!in_alt_root && verify_rctls(handle) != Z_OK) 25427c478bd9Sstevel@tonic-gate return_code = Z_ERR; 2543108322fbScarlsonj if (!in_alt_root && verify_pool(handle) != Z_OK) 25447c478bd9Sstevel@tonic-gate return_code = Z_ERR; 2545fa9e4066Sahrens if (!in_alt_root && verify_datasets(handle) != Z_OK) 2546fa9e4066Sahrens return_code = Z_ERR; 2547ffbafc53Scomay 2548ffbafc53Scomay /* 2549ffbafc53Scomay * As the "mount" command is used for patching/upgrading of zones 2550ffbafc53Scomay * or other maintenance processes, the zone's privilege set is not 2551ffbafc53Scomay * checked in this case. Instead, the default, safe set of 2552ffbafc53Scomay * privileges will be used when this zone is created in the 2553ffbafc53Scomay * kernel. 2554ffbafc53Scomay */ 2555ffbafc53Scomay if (!in_alt_root && cmd_num != CMD_MOUNT && 2556ffbafc53Scomay verify_limitpriv(handle) != Z_OK) 2557ffbafc53Scomay return_code = Z_ERR; 25588cd327d5Sgjelinek 25598cd327d5Sgjelinek return (return_code); 25608cd327d5Sgjelinek } 25618cd327d5Sgjelinek 25628cd327d5Sgjelinek static int 25638cd327d5Sgjelinek verify_details(int cmd_num) 25648cd327d5Sgjelinek { 25658cd327d5Sgjelinek zone_dochandle_t handle; 25668cd327d5Sgjelinek char zonepath[MAXPATHLEN], checkpath[MAXPATHLEN]; 25678cd327d5Sgjelinek int return_code = Z_OK; 25688cd327d5Sgjelinek int err; 25698cd327d5Sgjelinek 25708cd327d5Sgjelinek if ((handle = zonecfg_init_handle()) == NULL) { 25718cd327d5Sgjelinek zperror(cmd_to_str(cmd_num), B_TRUE); 25728cd327d5Sgjelinek return (Z_ERR); 25738cd327d5Sgjelinek } 25748cd327d5Sgjelinek if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) { 25758cd327d5Sgjelinek errno = err; 25768cd327d5Sgjelinek zperror(cmd_to_str(cmd_num), B_TRUE); 25778cd327d5Sgjelinek zonecfg_fini_handle(handle); 25788cd327d5Sgjelinek return (Z_ERR); 25798cd327d5Sgjelinek } 25808cd327d5Sgjelinek if ((err = zonecfg_get_zonepath(handle, zonepath, sizeof (zonepath))) != 25818cd327d5Sgjelinek Z_OK) { 25828cd327d5Sgjelinek errno = err; 25838cd327d5Sgjelinek zperror(cmd_to_str(cmd_num), B_TRUE); 25848cd327d5Sgjelinek zonecfg_fini_handle(handle); 25858cd327d5Sgjelinek return (Z_ERR); 25868cd327d5Sgjelinek } 25878cd327d5Sgjelinek /* 25888cd327d5Sgjelinek * zonecfg_get_zonepath() gets its data from the XML repository. 25898cd327d5Sgjelinek * Verify this against the index file, which is checked first by 25908cd327d5Sgjelinek * zone_get_zonepath(). If they don't match, bail out. 25918cd327d5Sgjelinek */ 25928cd327d5Sgjelinek if ((err = zone_get_zonepath(target_zone, checkpath, 25938cd327d5Sgjelinek sizeof (checkpath))) != Z_OK) { 25948cd327d5Sgjelinek errno = err; 25958cd327d5Sgjelinek zperror2(target_zone, gettext("could not get zone path")); 25968cd327d5Sgjelinek return (Z_ERR); 25978cd327d5Sgjelinek } 25988cd327d5Sgjelinek if (strcmp(zonepath, checkpath) != 0) { 25998cd327d5Sgjelinek /* 26008cd327d5Sgjelinek * TRANSLATION_NOTE 26018cd327d5Sgjelinek * XML and zonepath are literals that should not be translated. 26028cd327d5Sgjelinek */ 26038cd327d5Sgjelinek (void) fprintf(stderr, gettext("The XML repository has " 26048cd327d5Sgjelinek "zonepath '%s',\nbut the index file has zonepath '%s'.\n" 26058cd327d5Sgjelinek "These must match, so fix the incorrect entry.\n"), 26068cd327d5Sgjelinek zonepath, checkpath); 26078cd327d5Sgjelinek return (Z_ERR); 26088cd327d5Sgjelinek } 26098cd327d5Sgjelinek if (validate_zonepath(zonepath, cmd_num) != Z_OK) { 26108cd327d5Sgjelinek (void) fprintf(stderr, gettext("could not verify zonepath %s " 26118cd327d5Sgjelinek "because of the above errors.\n"), zonepath); 26128cd327d5Sgjelinek return_code = Z_ERR; 26138cd327d5Sgjelinek } 26148cd327d5Sgjelinek 26158cd327d5Sgjelinek if (verify_handle(cmd_num, handle) != Z_OK) 26168cd327d5Sgjelinek return_code = Z_ERR; 26178cd327d5Sgjelinek 26187c478bd9Sstevel@tonic-gate zonecfg_fini_handle(handle); 26197c478bd9Sstevel@tonic-gate if (return_code == Z_ERR) 26207c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 26217c478bd9Sstevel@tonic-gate gettext("%s: zone %s failed to verify\n"), 26227c478bd9Sstevel@tonic-gate execname, target_zone); 26237c478bd9Sstevel@tonic-gate return (return_code); 26247c478bd9Sstevel@tonic-gate } 26257c478bd9Sstevel@tonic-gate 26267c478bd9Sstevel@tonic-gate static int 26277c478bd9Sstevel@tonic-gate verify_func(int argc, char *argv[]) 26287c478bd9Sstevel@tonic-gate { 26297c478bd9Sstevel@tonic-gate int arg; 26307c478bd9Sstevel@tonic-gate 26317c478bd9Sstevel@tonic-gate optind = 0; 26327c478bd9Sstevel@tonic-gate if ((arg = getopt(argc, argv, "?")) != EOF) { 26337c478bd9Sstevel@tonic-gate switch (arg) { 26347c478bd9Sstevel@tonic-gate case '?': 26357c478bd9Sstevel@tonic-gate sub_usage(SHELP_VERIFY, CMD_VERIFY); 26367c478bd9Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 26377c478bd9Sstevel@tonic-gate default: 26387c478bd9Sstevel@tonic-gate sub_usage(SHELP_VERIFY, CMD_VERIFY); 26397c478bd9Sstevel@tonic-gate return (Z_USAGE); 26407c478bd9Sstevel@tonic-gate } 26417c478bd9Sstevel@tonic-gate } 26427c478bd9Sstevel@tonic-gate if (argc > optind) { 26437c478bd9Sstevel@tonic-gate sub_usage(SHELP_VERIFY, CMD_VERIFY); 26447c478bd9Sstevel@tonic-gate return (Z_USAGE); 26457c478bd9Sstevel@tonic-gate } 26467c478bd9Sstevel@tonic-gate if (sanity_check(target_zone, CMD_VERIFY, B_FALSE, B_FALSE) != Z_OK) 26477c478bd9Sstevel@tonic-gate return (Z_ERR); 26487c478bd9Sstevel@tonic-gate return (verify_details(CMD_VERIFY)); 26497c478bd9Sstevel@tonic-gate } 26507c478bd9Sstevel@tonic-gate 26517c478bd9Sstevel@tonic-gate #define LUCREATEZONE "/usr/lib/lu/lucreatezone" 26527c478bd9Sstevel@tonic-gate 26537c478bd9Sstevel@tonic-gate static int 26547c478bd9Sstevel@tonic-gate install_func(int argc, char *argv[]) 26557c478bd9Sstevel@tonic-gate { 26567c478bd9Sstevel@tonic-gate /* 9: "exec " and " -z " */ 26577c478bd9Sstevel@tonic-gate char cmdbuf[sizeof (LUCREATEZONE) + ZONENAME_MAX + 9]; 26587c478bd9Sstevel@tonic-gate int lockfd; 26597c478bd9Sstevel@tonic-gate int err, arg; 26607c478bd9Sstevel@tonic-gate char zonepath[MAXPATHLEN]; 26617c478bd9Sstevel@tonic-gate int status; 26620b5de56dSgjelinek boolean_t nodataset = B_FALSE; 26637c478bd9Sstevel@tonic-gate 2664108322fbScarlsonj if (zonecfg_in_alt_root()) { 2665108322fbScarlsonj zerror(gettext("cannot install zone in alternate root")); 2666108322fbScarlsonj return (Z_ERR); 2667108322fbScarlsonj } 2668108322fbScarlsonj 26697c478bd9Sstevel@tonic-gate optind = 0; 26700b5de56dSgjelinek if ((arg = getopt(argc, argv, "?x:")) != EOF) { 26717c478bd9Sstevel@tonic-gate switch (arg) { 26727c478bd9Sstevel@tonic-gate case '?': 26737c478bd9Sstevel@tonic-gate sub_usage(SHELP_INSTALL, CMD_INSTALL); 26747c478bd9Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 26750b5de56dSgjelinek case 'x': 26760b5de56dSgjelinek if (strcmp(optarg, "nodataset") != 0) { 26770b5de56dSgjelinek sub_usage(SHELP_INSTALL, CMD_INSTALL); 26780b5de56dSgjelinek return (Z_USAGE); 26790b5de56dSgjelinek } 26800b5de56dSgjelinek nodataset = B_TRUE; 26810b5de56dSgjelinek break; 26827c478bd9Sstevel@tonic-gate default: 26837c478bd9Sstevel@tonic-gate sub_usage(SHELP_INSTALL, CMD_INSTALL); 26847c478bd9Sstevel@tonic-gate return (Z_USAGE); 26857c478bd9Sstevel@tonic-gate } 26867c478bd9Sstevel@tonic-gate } 26877c478bd9Sstevel@tonic-gate if (argc > optind) { 26887c478bd9Sstevel@tonic-gate sub_usage(SHELP_INSTALL, CMD_INSTALL); 26897c478bd9Sstevel@tonic-gate return (Z_USAGE); 26907c478bd9Sstevel@tonic-gate } 26917c478bd9Sstevel@tonic-gate if (sanity_check(target_zone, CMD_INSTALL, B_FALSE, B_TRUE) != Z_OK) 26927c478bd9Sstevel@tonic-gate return (Z_ERR); 26937c478bd9Sstevel@tonic-gate if (verify_details(CMD_INSTALL) != Z_OK) 26947c478bd9Sstevel@tonic-gate return (Z_ERR); 26957c478bd9Sstevel@tonic-gate 26967c478bd9Sstevel@tonic-gate if (grab_lock_file(target_zone, &lockfd) != Z_OK) { 26977c478bd9Sstevel@tonic-gate zerror(gettext("another %s may have an operation in progress."), 2698865e09a4Sgjelinek "zoneadm"); 26997c478bd9Sstevel@tonic-gate return (Z_ERR); 27007c478bd9Sstevel@tonic-gate } 27017c478bd9Sstevel@tonic-gate err = zone_set_state(target_zone, ZONE_STATE_INCOMPLETE); 27027c478bd9Sstevel@tonic-gate if (err != Z_OK) { 27037c478bd9Sstevel@tonic-gate errno = err; 27047c478bd9Sstevel@tonic-gate zperror2(target_zone, gettext("could not set state")); 27057c478bd9Sstevel@tonic-gate goto done; 27067c478bd9Sstevel@tonic-gate } 27077c478bd9Sstevel@tonic-gate 27087c478bd9Sstevel@tonic-gate /* 27097c478bd9Sstevel@tonic-gate * According to the Application Packaging Developer's Guide, a 27107c478bd9Sstevel@tonic-gate * "checkinstall" script when included in a package is executed as 27117c478bd9Sstevel@tonic-gate * the user "install", if such a user exists, or by the user 27127c478bd9Sstevel@tonic-gate * "nobody". In order to support this dubious behavior, the path 27137c478bd9Sstevel@tonic-gate * to the zone being constructed is opened up during the life of 27147c478bd9Sstevel@tonic-gate * the command laying down the zone's root file system. Once this 27157c478bd9Sstevel@tonic-gate * has completed, regardless of whether it was successful, the 27167c478bd9Sstevel@tonic-gate * path to the zone is again restricted. 27177c478bd9Sstevel@tonic-gate */ 27187c478bd9Sstevel@tonic-gate if ((err = zone_get_zonepath(target_zone, zonepath, 27197c478bd9Sstevel@tonic-gate sizeof (zonepath))) != Z_OK) { 27207c478bd9Sstevel@tonic-gate errno = err; 27217c478bd9Sstevel@tonic-gate zperror2(target_zone, gettext("could not get zone path")); 27227c478bd9Sstevel@tonic-gate goto done; 27237c478bd9Sstevel@tonic-gate } 27240b5de56dSgjelinek 27250b5de56dSgjelinek if (!nodataset) 27260b5de56dSgjelinek create_zfs_zonepath(zonepath); 27270b5de56dSgjelinek 27287c478bd9Sstevel@tonic-gate if (chmod(zonepath, DEFAULT_DIR_MODE) != 0) { 27297c478bd9Sstevel@tonic-gate zperror(zonepath, B_FALSE); 27307c478bd9Sstevel@tonic-gate err = Z_ERR; 27317c478bd9Sstevel@tonic-gate goto done; 27327c478bd9Sstevel@tonic-gate } 27337c478bd9Sstevel@tonic-gate 27347c478bd9Sstevel@tonic-gate /* 27357c478bd9Sstevel@tonic-gate * "exec" the command so that the returned status is that of 27367c478bd9Sstevel@tonic-gate * LUCREATEZONE and not the shell. 27377c478bd9Sstevel@tonic-gate */ 27387c478bd9Sstevel@tonic-gate (void) snprintf(cmdbuf, sizeof (cmdbuf), "exec " LUCREATEZONE " -z %s", 27397c478bd9Sstevel@tonic-gate target_zone); 27407c478bd9Sstevel@tonic-gate status = do_subproc(cmdbuf); 27417c478bd9Sstevel@tonic-gate if (chmod(zonepath, S_IRWXU) != 0) { 27427c478bd9Sstevel@tonic-gate zperror(zonepath, B_FALSE); 27437c478bd9Sstevel@tonic-gate err = Z_ERR; 27447c478bd9Sstevel@tonic-gate goto done; 27457c478bd9Sstevel@tonic-gate } 27467c478bd9Sstevel@tonic-gate if ((err = subproc_status(LUCREATEZONE, status)) != Z_OK) 27477c478bd9Sstevel@tonic-gate goto done; 27487c478bd9Sstevel@tonic-gate 27497c478bd9Sstevel@tonic-gate if ((err = zone_set_state(target_zone, ZONE_STATE_INSTALLED)) != Z_OK) { 27507c478bd9Sstevel@tonic-gate errno = err; 27517c478bd9Sstevel@tonic-gate zperror2(target_zone, gettext("could not set state")); 27527c478bd9Sstevel@tonic-gate goto done; 27537c478bd9Sstevel@tonic-gate } 27547c478bd9Sstevel@tonic-gate 27557c478bd9Sstevel@tonic-gate done: 27567c478bd9Sstevel@tonic-gate release_lock_file(lockfd); 27577c478bd9Sstevel@tonic-gate return ((err == Z_OK) ? Z_OK : Z_ERR); 27587c478bd9Sstevel@tonic-gate } 27597c478bd9Sstevel@tonic-gate 27607c478bd9Sstevel@tonic-gate /* 2761865e09a4Sgjelinek * Check that the inherited pkg dirs are the same for the clone and its source. 2762865e09a4Sgjelinek * The easiest way to do that is check that the list of ipds is the same 2763865e09a4Sgjelinek * by matching each one against the other. This algorithm should be fine since 2764865e09a4Sgjelinek * the list of ipds should not be that long. 2765865e09a4Sgjelinek */ 2766865e09a4Sgjelinek static int 2767865e09a4Sgjelinek valid_ipd_clone(zone_dochandle_t s_handle, char *source_zone, 2768865e09a4Sgjelinek zone_dochandle_t t_handle, char *target_zone) 2769865e09a4Sgjelinek { 2770865e09a4Sgjelinek int err; 2771865e09a4Sgjelinek int res = Z_OK; 2772865e09a4Sgjelinek int s_cnt = 0; 2773865e09a4Sgjelinek int t_cnt = 0; 2774865e09a4Sgjelinek struct zone_fstab s_fstab; 2775865e09a4Sgjelinek struct zone_fstab t_fstab; 2776865e09a4Sgjelinek 2777865e09a4Sgjelinek /* 2778865e09a4Sgjelinek * First check the source of the clone against the target. 2779865e09a4Sgjelinek */ 2780865e09a4Sgjelinek if ((err = zonecfg_setipdent(s_handle)) != Z_OK) { 2781865e09a4Sgjelinek errno = err; 2782865e09a4Sgjelinek zperror2(source_zone, gettext("could not enumerate " 2783865e09a4Sgjelinek "inherit-pkg-dirs")); 2784865e09a4Sgjelinek return (Z_ERR); 2785865e09a4Sgjelinek } 2786865e09a4Sgjelinek 2787865e09a4Sgjelinek while (zonecfg_getipdent(s_handle, &s_fstab) == Z_OK) { 2788865e09a4Sgjelinek boolean_t match = B_FALSE; 2789865e09a4Sgjelinek 2790865e09a4Sgjelinek s_cnt++; 2791865e09a4Sgjelinek 2792865e09a4Sgjelinek if ((err = zonecfg_setipdent(t_handle)) != Z_OK) { 2793865e09a4Sgjelinek errno = err; 2794865e09a4Sgjelinek zperror2(target_zone, gettext("could not enumerate " 2795865e09a4Sgjelinek "inherit-pkg-dirs")); 2796865e09a4Sgjelinek (void) zonecfg_endipdent(s_handle); 2797865e09a4Sgjelinek return (Z_ERR); 2798865e09a4Sgjelinek } 2799865e09a4Sgjelinek 2800865e09a4Sgjelinek while (zonecfg_getipdent(t_handle, &t_fstab) == Z_OK) { 2801865e09a4Sgjelinek if (strcmp(s_fstab.zone_fs_dir, t_fstab.zone_fs_dir) 2802865e09a4Sgjelinek == 0) { 2803865e09a4Sgjelinek match = B_TRUE; 2804865e09a4Sgjelinek break; 2805865e09a4Sgjelinek } 2806865e09a4Sgjelinek } 2807865e09a4Sgjelinek (void) zonecfg_endipdent(t_handle); 2808865e09a4Sgjelinek 2809865e09a4Sgjelinek if (!match) { 2810865e09a4Sgjelinek (void) fprintf(stderr, gettext("inherit-pkg-dir " 2811865e09a4Sgjelinek "'%s' is not configured in zone %s.\n"), 2812865e09a4Sgjelinek s_fstab.zone_fs_dir, target_zone); 2813865e09a4Sgjelinek res = Z_ERR; 2814865e09a4Sgjelinek } 2815865e09a4Sgjelinek } 2816865e09a4Sgjelinek 2817865e09a4Sgjelinek (void) zonecfg_endipdent(s_handle); 2818865e09a4Sgjelinek 2819865e09a4Sgjelinek /* skip the next check if we already have errors */ 2820865e09a4Sgjelinek if (res == Z_ERR) 2821865e09a4Sgjelinek return (res); 2822865e09a4Sgjelinek 2823865e09a4Sgjelinek /* 2824865e09a4Sgjelinek * Now check the number of ipds in the target so we can verify 2825865e09a4Sgjelinek * that the source is not a subset of the target. 2826865e09a4Sgjelinek */ 2827865e09a4Sgjelinek if ((err = zonecfg_setipdent(t_handle)) != Z_OK) { 2828865e09a4Sgjelinek errno = err; 2829865e09a4Sgjelinek zperror2(target_zone, gettext("could not enumerate " 2830865e09a4Sgjelinek "inherit-pkg-dirs")); 2831865e09a4Sgjelinek return (Z_ERR); 2832865e09a4Sgjelinek } 2833865e09a4Sgjelinek 2834865e09a4Sgjelinek while (zonecfg_getipdent(t_handle, &t_fstab) == Z_OK) 2835865e09a4Sgjelinek t_cnt++; 2836865e09a4Sgjelinek 2837865e09a4Sgjelinek (void) zonecfg_endipdent(t_handle); 2838865e09a4Sgjelinek 2839865e09a4Sgjelinek if (t_cnt != s_cnt) { 2840865e09a4Sgjelinek (void) fprintf(stderr, gettext("Zone %s is configured " 2841865e09a4Sgjelinek "with inherit-pkg-dirs that are not configured in zone " 2842865e09a4Sgjelinek "%s.\n"), target_zone, source_zone); 2843865e09a4Sgjelinek res = Z_ERR; 2844865e09a4Sgjelinek } 2845865e09a4Sgjelinek 2846865e09a4Sgjelinek return (res); 2847865e09a4Sgjelinek } 2848865e09a4Sgjelinek 2849865e09a4Sgjelinek static void 2850865e09a4Sgjelinek warn_dev_match(zone_dochandle_t s_handle, char *source_zone, 2851865e09a4Sgjelinek zone_dochandle_t t_handle, char *target_zone) 2852865e09a4Sgjelinek { 2853865e09a4Sgjelinek int err; 2854865e09a4Sgjelinek struct zone_devtab s_devtab; 2855865e09a4Sgjelinek struct zone_devtab t_devtab; 2856865e09a4Sgjelinek 2857865e09a4Sgjelinek if ((err = zonecfg_setdevent(t_handle)) != Z_OK) { 2858865e09a4Sgjelinek errno = err; 2859865e09a4Sgjelinek zperror2(target_zone, gettext("could not enumerate devices")); 2860865e09a4Sgjelinek return; 2861865e09a4Sgjelinek } 2862865e09a4Sgjelinek 2863865e09a4Sgjelinek while (zonecfg_getdevent(t_handle, &t_devtab) == Z_OK) { 2864865e09a4Sgjelinek if ((err = zonecfg_setdevent(s_handle)) != Z_OK) { 2865865e09a4Sgjelinek errno = err; 2866865e09a4Sgjelinek zperror2(source_zone, 2867865e09a4Sgjelinek gettext("could not enumerate devices")); 2868865e09a4Sgjelinek (void) zonecfg_enddevent(t_handle); 2869865e09a4Sgjelinek return; 2870865e09a4Sgjelinek } 2871865e09a4Sgjelinek 2872865e09a4Sgjelinek while (zonecfg_getdevent(s_handle, &s_devtab) == Z_OK) { 2873865e09a4Sgjelinek /* 2874865e09a4Sgjelinek * Use fnmatch to catch the case where wildcards 2875865e09a4Sgjelinek * were used in one zone and the other has an 2876865e09a4Sgjelinek * explicit entry (e.g. /dev/dsk/c0t0d0s6 vs. 2877865e09a4Sgjelinek * /dev/\*dsk/c0t0d0s6). 2878865e09a4Sgjelinek */ 2879865e09a4Sgjelinek if (fnmatch(t_devtab.zone_dev_match, 2880865e09a4Sgjelinek s_devtab.zone_dev_match, FNM_PATHNAME) == 0 || 2881865e09a4Sgjelinek fnmatch(s_devtab.zone_dev_match, 2882865e09a4Sgjelinek t_devtab.zone_dev_match, FNM_PATHNAME) == 0) { 2883865e09a4Sgjelinek (void) fprintf(stderr, 2884865e09a4Sgjelinek gettext("WARNING: device '%s' " 2885865e09a4Sgjelinek "is configured in both zones.\n"), 2886865e09a4Sgjelinek t_devtab.zone_dev_match); 2887865e09a4Sgjelinek break; 2888865e09a4Sgjelinek } 2889865e09a4Sgjelinek } 2890865e09a4Sgjelinek (void) zonecfg_enddevent(s_handle); 2891865e09a4Sgjelinek } 2892865e09a4Sgjelinek 2893865e09a4Sgjelinek (void) zonecfg_enddevent(t_handle); 2894865e09a4Sgjelinek } 2895865e09a4Sgjelinek 2896865e09a4Sgjelinek /* 2897865e09a4Sgjelinek * Check if the specified mount option (opt) is contained within the 2898865e09a4Sgjelinek * options string. 2899865e09a4Sgjelinek */ 2900865e09a4Sgjelinek static boolean_t 2901865e09a4Sgjelinek opt_match(char *opt, char *options) 2902865e09a4Sgjelinek { 2903865e09a4Sgjelinek char *p; 2904865e09a4Sgjelinek char *lastp; 2905865e09a4Sgjelinek 2906865e09a4Sgjelinek if ((p = strtok_r(options, ",", &lastp)) != NULL) { 2907865e09a4Sgjelinek if (strcmp(p, opt) == 0) 2908865e09a4Sgjelinek return (B_TRUE); 2909865e09a4Sgjelinek while ((p = strtok_r(NULL, ",", &lastp)) != NULL) { 2910865e09a4Sgjelinek if (strcmp(p, opt) == 0) 2911865e09a4Sgjelinek return (B_TRUE); 2912865e09a4Sgjelinek } 2913865e09a4Sgjelinek } 2914865e09a4Sgjelinek 2915865e09a4Sgjelinek return (B_FALSE); 2916865e09a4Sgjelinek } 2917865e09a4Sgjelinek 29180b5de56dSgjelinek #define RW_LOFS "WARNING: read-write lofs file system on '%s' is configured " \ 2919865e09a4Sgjelinek "in both zones.\n" 2920865e09a4Sgjelinek 2921865e09a4Sgjelinek static void 2922865e09a4Sgjelinek print_fs_warnings(struct zone_fstab *s_fstab, struct zone_fstab *t_fstab) 2923865e09a4Sgjelinek { 2924865e09a4Sgjelinek /* 2925865e09a4Sgjelinek * It is ok to have shared lofs mounted fs but we want to warn if 2926865e09a4Sgjelinek * either is rw since this will effect the other zone. 2927865e09a4Sgjelinek */ 2928865e09a4Sgjelinek if (strcmp(t_fstab->zone_fs_type, "lofs") == 0) { 2929865e09a4Sgjelinek zone_fsopt_t *optp; 2930865e09a4Sgjelinek 2931865e09a4Sgjelinek /* The default is rw so no options means rw */ 2932865e09a4Sgjelinek if (t_fstab->zone_fs_options == NULL || 2933865e09a4Sgjelinek s_fstab->zone_fs_options == NULL) { 2934865e09a4Sgjelinek (void) fprintf(stderr, gettext(RW_LOFS), 2935865e09a4Sgjelinek t_fstab->zone_fs_special); 2936865e09a4Sgjelinek return; 2937865e09a4Sgjelinek } 2938865e09a4Sgjelinek 2939865e09a4Sgjelinek for (optp = s_fstab->zone_fs_options; optp != NULL; 2940865e09a4Sgjelinek optp = optp->zone_fsopt_next) { 2941865e09a4Sgjelinek if (opt_match("rw", optp->zone_fsopt_opt)) { 2942865e09a4Sgjelinek (void) fprintf(stderr, gettext(RW_LOFS), 2943865e09a4Sgjelinek s_fstab->zone_fs_special); 2944865e09a4Sgjelinek return; 2945865e09a4Sgjelinek } 2946865e09a4Sgjelinek } 2947865e09a4Sgjelinek 2948865e09a4Sgjelinek for (optp = t_fstab->zone_fs_options; optp != NULL; 2949865e09a4Sgjelinek optp = optp->zone_fsopt_next) { 2950865e09a4Sgjelinek if (opt_match("rw", optp->zone_fsopt_opt)) { 2951865e09a4Sgjelinek (void) fprintf(stderr, gettext(RW_LOFS), 2952865e09a4Sgjelinek t_fstab->zone_fs_special); 2953865e09a4Sgjelinek return; 2954865e09a4Sgjelinek } 2955865e09a4Sgjelinek } 2956865e09a4Sgjelinek 2957865e09a4Sgjelinek return; 2958865e09a4Sgjelinek } 2959865e09a4Sgjelinek 2960865e09a4Sgjelinek /* 2961865e09a4Sgjelinek * TRANSLATION_NOTE 29620b5de56dSgjelinek * The first variable is the file system type and the second is 29630b5de56dSgjelinek * the file system special device. For example, 29640b5de56dSgjelinek * WARNING: ufs file system on '/dev/dsk/c0t0d0s0' ... 2965865e09a4Sgjelinek */ 29660b5de56dSgjelinek (void) fprintf(stderr, gettext("WARNING: %s file system on '%s' " 2967865e09a4Sgjelinek "is configured in both zones.\n"), t_fstab->zone_fs_type, 2968865e09a4Sgjelinek t_fstab->zone_fs_special); 2969865e09a4Sgjelinek } 2970865e09a4Sgjelinek 2971865e09a4Sgjelinek static void 2972865e09a4Sgjelinek warn_fs_match(zone_dochandle_t s_handle, char *source_zone, 2973865e09a4Sgjelinek zone_dochandle_t t_handle, char *target_zone) 2974865e09a4Sgjelinek { 2975865e09a4Sgjelinek int err; 2976865e09a4Sgjelinek struct zone_fstab s_fstab; 2977865e09a4Sgjelinek struct zone_fstab t_fstab; 2978865e09a4Sgjelinek 2979865e09a4Sgjelinek if ((err = zonecfg_setfsent(t_handle)) != Z_OK) { 2980865e09a4Sgjelinek errno = err; 2981865e09a4Sgjelinek zperror2(target_zone, 29820b5de56dSgjelinek gettext("could not enumerate file systems")); 2983865e09a4Sgjelinek return; 2984865e09a4Sgjelinek } 2985865e09a4Sgjelinek 2986865e09a4Sgjelinek while (zonecfg_getfsent(t_handle, &t_fstab) == Z_OK) { 2987865e09a4Sgjelinek if ((err = zonecfg_setfsent(s_handle)) != Z_OK) { 2988865e09a4Sgjelinek errno = err; 2989865e09a4Sgjelinek zperror2(source_zone, 29900b5de56dSgjelinek gettext("could not enumerate file systems")); 2991865e09a4Sgjelinek (void) zonecfg_endfsent(t_handle); 2992865e09a4Sgjelinek return; 2993865e09a4Sgjelinek } 2994865e09a4Sgjelinek 2995865e09a4Sgjelinek while (zonecfg_getfsent(s_handle, &s_fstab) == Z_OK) { 2996865e09a4Sgjelinek if (strcmp(t_fstab.zone_fs_special, 2997865e09a4Sgjelinek s_fstab.zone_fs_special) == 0) { 2998865e09a4Sgjelinek print_fs_warnings(&s_fstab, &t_fstab); 2999865e09a4Sgjelinek break; 3000865e09a4Sgjelinek } 3001865e09a4Sgjelinek } 3002865e09a4Sgjelinek (void) zonecfg_endfsent(s_handle); 3003865e09a4Sgjelinek } 3004865e09a4Sgjelinek 3005865e09a4Sgjelinek (void) zonecfg_endfsent(t_handle); 3006865e09a4Sgjelinek } 3007865e09a4Sgjelinek 3008865e09a4Sgjelinek /* 3009865e09a4Sgjelinek * We don't catch the case where you used the same IP address but 3010865e09a4Sgjelinek * it is not an exact string match. For example, 192.9.0.128 vs. 192.09.0.128. 3011865e09a4Sgjelinek * However, we're not going to worry about that but we will check for 3012865e09a4Sgjelinek * a possible netmask on one of the addresses (e.g. 10.0.0.1 and 10.0.0.1/24) 3013865e09a4Sgjelinek * and handle that case as a match. 3014865e09a4Sgjelinek */ 3015865e09a4Sgjelinek static void 3016865e09a4Sgjelinek warn_ip_match(zone_dochandle_t s_handle, char *source_zone, 3017865e09a4Sgjelinek zone_dochandle_t t_handle, char *target_zone) 3018865e09a4Sgjelinek { 3019865e09a4Sgjelinek int err; 3020865e09a4Sgjelinek struct zone_nwiftab s_nwiftab; 3021865e09a4Sgjelinek struct zone_nwiftab t_nwiftab; 3022865e09a4Sgjelinek 3023865e09a4Sgjelinek if ((err = zonecfg_setnwifent(t_handle)) != Z_OK) { 3024865e09a4Sgjelinek errno = err; 3025865e09a4Sgjelinek zperror2(target_zone, 3026865e09a4Sgjelinek gettext("could not enumerate network interfaces")); 3027865e09a4Sgjelinek return; 3028865e09a4Sgjelinek } 3029865e09a4Sgjelinek 3030865e09a4Sgjelinek while (zonecfg_getnwifent(t_handle, &t_nwiftab) == Z_OK) { 3031865e09a4Sgjelinek char *p; 3032865e09a4Sgjelinek 3033865e09a4Sgjelinek /* remove an (optional) netmask from the address */ 3034865e09a4Sgjelinek if ((p = strchr(t_nwiftab.zone_nwif_address, '/')) != NULL) 3035865e09a4Sgjelinek *p = '\0'; 3036865e09a4Sgjelinek 3037865e09a4Sgjelinek if ((err = zonecfg_setnwifent(s_handle)) != Z_OK) { 3038865e09a4Sgjelinek errno = err; 3039865e09a4Sgjelinek zperror2(source_zone, 3040865e09a4Sgjelinek gettext("could not enumerate network interfaces")); 3041865e09a4Sgjelinek (void) zonecfg_endnwifent(t_handle); 3042865e09a4Sgjelinek return; 3043865e09a4Sgjelinek } 3044865e09a4Sgjelinek 3045865e09a4Sgjelinek while (zonecfg_getnwifent(s_handle, &s_nwiftab) == Z_OK) { 3046865e09a4Sgjelinek /* remove an (optional) netmask from the address */ 3047865e09a4Sgjelinek if ((p = strchr(s_nwiftab.zone_nwif_address, '/')) 3048865e09a4Sgjelinek != NULL) 3049865e09a4Sgjelinek *p = '\0'; 3050865e09a4Sgjelinek 3051865e09a4Sgjelinek if (strcmp(t_nwiftab.zone_nwif_address, 3052865e09a4Sgjelinek s_nwiftab.zone_nwif_address) == 0) { 3053865e09a4Sgjelinek (void) fprintf(stderr, 3054865e09a4Sgjelinek gettext("WARNING: network address '%s' " 3055865e09a4Sgjelinek "is configured in both zones.\n"), 3056865e09a4Sgjelinek t_nwiftab.zone_nwif_address); 3057865e09a4Sgjelinek break; 3058865e09a4Sgjelinek } 3059865e09a4Sgjelinek } 3060865e09a4Sgjelinek (void) zonecfg_endnwifent(s_handle); 3061865e09a4Sgjelinek } 3062865e09a4Sgjelinek 3063865e09a4Sgjelinek (void) zonecfg_endnwifent(t_handle); 3064865e09a4Sgjelinek } 3065865e09a4Sgjelinek 3066865e09a4Sgjelinek static void 3067865e09a4Sgjelinek warn_dataset_match(zone_dochandle_t s_handle, char *source_zone, 3068865e09a4Sgjelinek zone_dochandle_t t_handle, char *target_zone) 3069865e09a4Sgjelinek { 3070865e09a4Sgjelinek int err; 3071865e09a4Sgjelinek struct zone_dstab s_dstab; 3072865e09a4Sgjelinek struct zone_dstab t_dstab; 3073865e09a4Sgjelinek 3074865e09a4Sgjelinek if ((err = zonecfg_setdsent(t_handle)) != Z_OK) { 3075865e09a4Sgjelinek errno = err; 3076865e09a4Sgjelinek zperror2(target_zone, gettext("could not enumerate datasets")); 3077865e09a4Sgjelinek return; 3078865e09a4Sgjelinek } 3079865e09a4Sgjelinek 3080865e09a4Sgjelinek while (zonecfg_getdsent(t_handle, &t_dstab) == Z_OK) { 3081865e09a4Sgjelinek if ((err = zonecfg_setdsent(s_handle)) != Z_OK) { 3082865e09a4Sgjelinek errno = err; 3083865e09a4Sgjelinek zperror2(source_zone, 3084865e09a4Sgjelinek gettext("could not enumerate datasets")); 3085865e09a4Sgjelinek (void) zonecfg_enddsent(t_handle); 3086865e09a4Sgjelinek return; 3087865e09a4Sgjelinek } 3088865e09a4Sgjelinek 3089865e09a4Sgjelinek while (zonecfg_getdsent(s_handle, &s_dstab) == Z_OK) { 3090865e09a4Sgjelinek if (strcmp(t_dstab.zone_dataset_name, 3091865e09a4Sgjelinek s_dstab.zone_dataset_name) == 0) { 3092865e09a4Sgjelinek (void) fprintf(stderr, 3093865e09a4Sgjelinek gettext("WARNING: dataset '%s' " 3094865e09a4Sgjelinek "is configured in both zones.\n"), 3095865e09a4Sgjelinek t_dstab.zone_dataset_name); 3096865e09a4Sgjelinek break; 3097865e09a4Sgjelinek } 3098865e09a4Sgjelinek } 3099865e09a4Sgjelinek (void) zonecfg_enddsent(s_handle); 3100865e09a4Sgjelinek } 3101865e09a4Sgjelinek 3102865e09a4Sgjelinek (void) zonecfg_enddsent(t_handle); 3103865e09a4Sgjelinek } 3104865e09a4Sgjelinek 3105865e09a4Sgjelinek static int 3106865e09a4Sgjelinek validate_clone(char *source_zone, char *target_zone) 3107865e09a4Sgjelinek { 3108865e09a4Sgjelinek int err = Z_OK; 3109865e09a4Sgjelinek zone_dochandle_t s_handle; 3110865e09a4Sgjelinek zone_dochandle_t t_handle; 3111865e09a4Sgjelinek 3112865e09a4Sgjelinek if ((t_handle = zonecfg_init_handle()) == NULL) { 3113865e09a4Sgjelinek zperror(cmd_to_str(CMD_CLONE), B_TRUE); 3114865e09a4Sgjelinek return (Z_ERR); 3115865e09a4Sgjelinek } 3116865e09a4Sgjelinek if ((err = zonecfg_get_handle(target_zone, t_handle)) != Z_OK) { 3117865e09a4Sgjelinek errno = err; 3118865e09a4Sgjelinek zperror(cmd_to_str(CMD_CLONE), B_TRUE); 3119865e09a4Sgjelinek zonecfg_fini_handle(t_handle); 3120865e09a4Sgjelinek return (Z_ERR); 3121865e09a4Sgjelinek } 3122865e09a4Sgjelinek 3123865e09a4Sgjelinek if ((s_handle = zonecfg_init_handle()) == NULL) { 3124865e09a4Sgjelinek zperror(cmd_to_str(CMD_CLONE), B_TRUE); 3125865e09a4Sgjelinek zonecfg_fini_handle(t_handle); 3126865e09a4Sgjelinek return (Z_ERR); 3127865e09a4Sgjelinek } 3128865e09a4Sgjelinek if ((err = zonecfg_get_handle(source_zone, s_handle)) != Z_OK) { 3129865e09a4Sgjelinek errno = err; 3130865e09a4Sgjelinek zperror(cmd_to_str(CMD_CLONE), B_TRUE); 3131865e09a4Sgjelinek goto done; 3132865e09a4Sgjelinek } 3133865e09a4Sgjelinek 3134865e09a4Sgjelinek /* verify new zone has same inherit-pkg-dirs */ 3135865e09a4Sgjelinek err = valid_ipd_clone(s_handle, source_zone, t_handle, target_zone); 3136865e09a4Sgjelinek 3137865e09a4Sgjelinek /* warn about imported fs's which are the same */ 3138865e09a4Sgjelinek warn_fs_match(s_handle, source_zone, t_handle, target_zone); 3139865e09a4Sgjelinek 3140865e09a4Sgjelinek /* warn about imported IP addresses which are the same */ 3141865e09a4Sgjelinek warn_ip_match(s_handle, source_zone, t_handle, target_zone); 3142865e09a4Sgjelinek 3143865e09a4Sgjelinek /* warn about imported devices which are the same */ 3144865e09a4Sgjelinek warn_dev_match(s_handle, source_zone, t_handle, target_zone); 3145865e09a4Sgjelinek 3146865e09a4Sgjelinek /* warn about imported datasets which are the same */ 3147865e09a4Sgjelinek warn_dataset_match(s_handle, source_zone, t_handle, target_zone); 3148865e09a4Sgjelinek 3149865e09a4Sgjelinek done: 3150865e09a4Sgjelinek zonecfg_fini_handle(t_handle); 3151865e09a4Sgjelinek zonecfg_fini_handle(s_handle); 3152865e09a4Sgjelinek 3153865e09a4Sgjelinek return ((err == Z_OK) ? Z_OK : Z_ERR); 3154865e09a4Sgjelinek } 3155865e09a4Sgjelinek 3156865e09a4Sgjelinek static int 3157865e09a4Sgjelinek copy_zone(char *src, char *dst) 3158865e09a4Sgjelinek { 3159865e09a4Sgjelinek boolean_t out_null = B_FALSE; 3160865e09a4Sgjelinek int status; 3161865e09a4Sgjelinek int err; 3162865e09a4Sgjelinek char *outfile; 3163865e09a4Sgjelinek char cmdbuf[MAXPATHLEN * 2 + 128]; 3164865e09a4Sgjelinek 3165865e09a4Sgjelinek if ((outfile = tempnam("/var/log", "zone")) == NULL) { 3166865e09a4Sgjelinek outfile = "/dev/null"; 3167865e09a4Sgjelinek out_null = B_TRUE; 3168865e09a4Sgjelinek } 3169865e09a4Sgjelinek 31700b5de56dSgjelinek /* 31710b5de56dSgjelinek * Use find to get the list of files to copy. We need to skip 31720b5de56dSgjelinek * files of type "socket" since cpio can't handle those but that 31730b5de56dSgjelinek * should be ok since the app will recreate the socket when it runs. 31740b5de56dSgjelinek * We also need to filter out anything under the .zfs subdir. Since 31750b5de56dSgjelinek * find is running depth-first, we need the extra egrep to filter .zfs. 31760b5de56dSgjelinek */ 3177865e09a4Sgjelinek (void) snprintf(cmdbuf, sizeof (cmdbuf), 31780b5de56dSgjelinek "cd %s && /usr/bin/find . -type s -prune -o -depth -print | " 317907b574eeSgjelinek "/usr/bin/egrep -v '^\\./\\.zfs$|^\\./\\.zfs/' | " 3180865e09a4Sgjelinek "/usr/bin/cpio -pdmuP@ %s > %s 2>&1", 3181865e09a4Sgjelinek src, dst, outfile); 3182865e09a4Sgjelinek 3183865e09a4Sgjelinek status = do_subproc(cmdbuf); 3184865e09a4Sgjelinek 3185865e09a4Sgjelinek if ((err = subproc_status("copy", status)) != Z_OK) { 3186865e09a4Sgjelinek if (!out_null) 3187865e09a4Sgjelinek (void) fprintf(stderr, gettext("\nThe copy failed.\n" 3188865e09a4Sgjelinek "More information can be found in %s\n"), outfile); 3189865e09a4Sgjelinek return (err); 3190865e09a4Sgjelinek } 3191865e09a4Sgjelinek 3192865e09a4Sgjelinek if (!out_null) 3193865e09a4Sgjelinek (void) unlink(outfile); 3194865e09a4Sgjelinek 3195865e09a4Sgjelinek return (Z_OK); 3196865e09a4Sgjelinek } 3197865e09a4Sgjelinek 3198865e09a4Sgjelinek /* 31995cd08338Sgjelinek * Run sys-unconfig on a zone. This will leave the zone in the installed 32005cd08338Sgjelinek * state as long as there were no errors during the sys-unconfig. 3201865e09a4Sgjelinek */ 3202865e09a4Sgjelinek static int 32035cd08338Sgjelinek unconfigure_zone(char *zonepath) 3204865e09a4Sgjelinek { 32055cd08338Sgjelinek int err; 3206865e09a4Sgjelinek int status; 32075cd08338Sgjelinek struct stat unconfig_buf; 32085cd08338Sgjelinek zone_cmd_arg_t zarg; 32095cd08338Sgjelinek char cmdbuf[MAXPATHLEN + 51]; 3210865e09a4Sgjelinek 32115cd08338Sgjelinek /* The zone has to be installed in order to mount the scratch zone. */ 32125cd08338Sgjelinek if ((err = zone_set_state(target_zone, ZONE_STATE_INSTALLED)) != Z_OK) { 32135cd08338Sgjelinek errno = err; 32145cd08338Sgjelinek zperror2(target_zone, gettext("could not set state")); 32155cd08338Sgjelinek return (Z_ERR); 32165cd08338Sgjelinek } 32175cd08338Sgjelinek 32185cd08338Sgjelinek /* 321945916cd2Sjpk * Trusted Extensions requires that cloned zones use the 322045916cd2Sjpk * same sysid configuration, so it is not appropriate to 322145916cd2Sjpk * unconfigure the zone. 322245916cd2Sjpk */ 322345916cd2Sjpk if (is_system_labeled()) 322445916cd2Sjpk return (Z_OK); 322545916cd2Sjpk 322645916cd2Sjpk /* 32275cd08338Sgjelinek * Check if the zone is already sys-unconfiged. This saves us 32285cd08338Sgjelinek * the work of bringing up the scratch zone so we can unconfigure it. 32295cd08338Sgjelinek */ 32305cd08338Sgjelinek (void) snprintf(cmdbuf, sizeof (cmdbuf), "%s/root/etc/.UNCONFIGURED", 32315cd08338Sgjelinek zonepath); 32325cd08338Sgjelinek if (stat(cmdbuf, &unconfig_buf) == 0) 3233865e09a4Sgjelinek return (Z_OK); 3234865e09a4Sgjelinek 32355cd08338Sgjelinek zarg.cmd = Z_MOUNT; 32365cd08338Sgjelinek if (call_zoneadmd(target_zone, &zarg) != 0) { 32375cd08338Sgjelinek zerror(gettext("call to %s failed"), "zoneadmd"); 32385cd08338Sgjelinek (void) zone_set_state(target_zone, ZONE_STATE_INCOMPLETE); 32395cd08338Sgjelinek return (Z_ERR); 3240865e09a4Sgjelinek } 3241865e09a4Sgjelinek 32425cd08338Sgjelinek (void) snprintf(cmdbuf, sizeof (cmdbuf), 32435cd08338Sgjelinek "/usr/sbin/zlogin -S %s /usr/sbin/sys-unconfig -R /a", target_zone); 32445cd08338Sgjelinek 32455cd08338Sgjelinek status = do_subproc(cmdbuf); 32465cd08338Sgjelinek if ((err = subproc_status("sys-unconfig", status)) != Z_OK) { 32475cd08338Sgjelinek errno = err; 32485cd08338Sgjelinek zperror2(target_zone, gettext("sys-unconfig failed\n")); 32495cd08338Sgjelinek (void) zone_set_state(target_zone, ZONE_STATE_INCOMPLETE); 3250865e09a4Sgjelinek } 3251865e09a4Sgjelinek 32525cd08338Sgjelinek zarg.cmd = Z_UNMOUNT; 32535cd08338Sgjelinek if (call_zoneadmd(target_zone, &zarg) != 0) { 32545cd08338Sgjelinek zerror(gettext("call to %s failed"), "zoneadmd"); 32555cd08338Sgjelinek (void) fprintf(stderr, gettext("could not unmount zone\n")); 32565cd08338Sgjelinek return (Z_ERR); 32575cd08338Sgjelinek } 32585cd08338Sgjelinek 32595cd08338Sgjelinek return ((err == Z_OK) ? Z_OK : Z_ERR); 32605cd08338Sgjelinek } 3261865e09a4Sgjelinek 3262865e09a4Sgjelinek /* ARGSUSED */ 32630b5de56dSgjelinek static int 3264865e09a4Sgjelinek zfm_print(const char *p, void *r) { 3265865e09a4Sgjelinek zerror(" %s\n", p); 3266865e09a4Sgjelinek return (0); 3267865e09a4Sgjelinek } 3268865e09a4Sgjelinek 32690b5de56dSgjelinek int 32700b5de56dSgjelinek clone_copy(char *source_zonepath, char *zonepath) 32710b5de56dSgjelinek { 32720b5de56dSgjelinek int err; 32730b5de56dSgjelinek 32740b5de56dSgjelinek /* Don't clone the zone if anything is still mounted there */ 32750b5de56dSgjelinek if (zonecfg_find_mounts(source_zonepath, NULL, NULL)) { 32760b5de56dSgjelinek zerror(gettext("These file systems are mounted on " 32770b5de56dSgjelinek "subdirectories of %s.\n"), source_zonepath); 32780b5de56dSgjelinek (void) zonecfg_find_mounts(source_zonepath, zfm_print, NULL); 32790b5de56dSgjelinek return (Z_ERR); 32800b5de56dSgjelinek } 32810b5de56dSgjelinek 32820b5de56dSgjelinek /* 32830b5de56dSgjelinek * Attempt to create a ZFS fs for the zonepath. As usual, we don't 32840b5de56dSgjelinek * care if this works or not since we always have the default behavior 32850b5de56dSgjelinek * of a simple directory for the zonepath. 32860b5de56dSgjelinek */ 32870b5de56dSgjelinek create_zfs_zonepath(zonepath); 32880b5de56dSgjelinek 32890b5de56dSgjelinek (void) printf(gettext("Copying %s..."), source_zonepath); 32900b5de56dSgjelinek (void) fflush(stdout); 32910b5de56dSgjelinek 32920b5de56dSgjelinek err = copy_zone(source_zonepath, zonepath); 32930b5de56dSgjelinek 32940b5de56dSgjelinek (void) printf("\n"); 32950b5de56dSgjelinek 32960b5de56dSgjelinek return (err); 32970b5de56dSgjelinek } 32980b5de56dSgjelinek 3299865e09a4Sgjelinek static int 3300865e09a4Sgjelinek clone_func(int argc, char *argv[]) 3301865e09a4Sgjelinek { 3302865e09a4Sgjelinek char *source_zone = NULL; 3303865e09a4Sgjelinek int lockfd; 3304865e09a4Sgjelinek int err, arg; 3305865e09a4Sgjelinek char zonepath[MAXPATHLEN]; 3306865e09a4Sgjelinek char source_zonepath[MAXPATHLEN]; 3307865e09a4Sgjelinek zone_state_t state; 3308865e09a4Sgjelinek zone_entry_t *zent; 33090b5de56dSgjelinek char *method = NULL; 33100b5de56dSgjelinek char *snapshot = NULL; 3311865e09a4Sgjelinek 3312865e09a4Sgjelinek if (zonecfg_in_alt_root()) { 3313865e09a4Sgjelinek zerror(gettext("cannot clone zone in alternate root")); 3314865e09a4Sgjelinek return (Z_ERR); 3315865e09a4Sgjelinek } 3316865e09a4Sgjelinek 3317865e09a4Sgjelinek optind = 0; 33180b5de56dSgjelinek if ((arg = getopt(argc, argv, "?m:s:")) != EOF) { 3319865e09a4Sgjelinek switch (arg) { 3320865e09a4Sgjelinek case '?': 3321865e09a4Sgjelinek sub_usage(SHELP_CLONE, CMD_CLONE); 3322865e09a4Sgjelinek return (optopt == '?' ? Z_OK : Z_USAGE); 3323865e09a4Sgjelinek case 'm': 3324865e09a4Sgjelinek method = optarg; 3325865e09a4Sgjelinek break; 33260b5de56dSgjelinek case 's': 33270b5de56dSgjelinek snapshot = optarg; 33280b5de56dSgjelinek break; 3329865e09a4Sgjelinek default: 3330865e09a4Sgjelinek sub_usage(SHELP_CLONE, CMD_CLONE); 3331865e09a4Sgjelinek return (Z_USAGE); 3332865e09a4Sgjelinek } 3333865e09a4Sgjelinek } 33340b5de56dSgjelinek if (argc != (optind + 1) || 33350b5de56dSgjelinek (method != NULL && strcmp(method, "copy") != 0)) { 3336865e09a4Sgjelinek sub_usage(SHELP_CLONE, CMD_CLONE); 3337865e09a4Sgjelinek return (Z_USAGE); 3338865e09a4Sgjelinek } 3339865e09a4Sgjelinek source_zone = argv[optind]; 3340865e09a4Sgjelinek if (sanity_check(target_zone, CMD_CLONE, B_FALSE, B_TRUE) != Z_OK) 3341865e09a4Sgjelinek return (Z_ERR); 3342865e09a4Sgjelinek if (verify_details(CMD_CLONE) != Z_OK) 3343865e09a4Sgjelinek return (Z_ERR); 3344865e09a4Sgjelinek 3345865e09a4Sgjelinek /* 3346865e09a4Sgjelinek * We also need to do some extra validation on the source zone. 3347865e09a4Sgjelinek */ 3348865e09a4Sgjelinek if (strcmp(source_zone, GLOBAL_ZONENAME) == 0) { 3349865e09a4Sgjelinek zerror(gettext("%s operation is invalid for the global zone."), 3350865e09a4Sgjelinek cmd_to_str(CMD_CLONE)); 3351865e09a4Sgjelinek return (Z_ERR); 3352865e09a4Sgjelinek } 3353865e09a4Sgjelinek 3354865e09a4Sgjelinek if (strncmp(source_zone, "SUNW", 4) == 0) { 3355865e09a4Sgjelinek zerror(gettext("%s operation is invalid for zones starting " 3356865e09a4Sgjelinek "with SUNW."), cmd_to_str(CMD_CLONE)); 3357865e09a4Sgjelinek return (Z_ERR); 3358865e09a4Sgjelinek } 3359865e09a4Sgjelinek 3360865e09a4Sgjelinek zent = lookup_running_zone(source_zone); 3361865e09a4Sgjelinek if (zent != NULL) { 3362865e09a4Sgjelinek /* check whether the zone is ready or running */ 3363865e09a4Sgjelinek if ((err = zone_get_state(zent->zname, &zent->zstate_num)) 3364865e09a4Sgjelinek != Z_OK) { 3365865e09a4Sgjelinek errno = err; 3366865e09a4Sgjelinek zperror2(zent->zname, gettext("could not get state")); 3367865e09a4Sgjelinek /* can't tell, so hedge */ 3368865e09a4Sgjelinek zent->zstate_str = "ready/running"; 3369865e09a4Sgjelinek } else { 3370865e09a4Sgjelinek zent->zstate_str = zone_state_str(zent->zstate_num); 3371865e09a4Sgjelinek } 3372865e09a4Sgjelinek zerror(gettext("%s operation is invalid for %s zones."), 3373865e09a4Sgjelinek cmd_to_str(CMD_CLONE), zent->zstate_str); 3374865e09a4Sgjelinek return (Z_ERR); 3375865e09a4Sgjelinek } 3376865e09a4Sgjelinek 3377865e09a4Sgjelinek if ((err = zone_get_state(source_zone, &state)) != Z_OK) { 3378865e09a4Sgjelinek errno = err; 3379865e09a4Sgjelinek zperror2(source_zone, gettext("could not get state")); 3380865e09a4Sgjelinek return (Z_ERR); 3381865e09a4Sgjelinek } 3382865e09a4Sgjelinek if (state != ZONE_STATE_INSTALLED) { 3383865e09a4Sgjelinek (void) fprintf(stderr, 3384865e09a4Sgjelinek gettext("%s: zone %s is %s; %s is required.\n"), 3385865e09a4Sgjelinek execname, source_zone, zone_state_str(state), 3386865e09a4Sgjelinek zone_state_str(ZONE_STATE_INSTALLED)); 3387865e09a4Sgjelinek return (Z_ERR); 3388865e09a4Sgjelinek } 3389865e09a4Sgjelinek 3390865e09a4Sgjelinek /* 3391865e09a4Sgjelinek * The source zone checks out ok, continue with the clone. 3392865e09a4Sgjelinek */ 3393865e09a4Sgjelinek 3394865e09a4Sgjelinek if (validate_clone(source_zone, target_zone) != Z_OK) 3395865e09a4Sgjelinek return (Z_ERR); 3396865e09a4Sgjelinek 3397865e09a4Sgjelinek if (grab_lock_file(target_zone, &lockfd) != Z_OK) { 3398865e09a4Sgjelinek zerror(gettext("another %s may have an operation in progress."), 3399865e09a4Sgjelinek "zoneadm"); 3400865e09a4Sgjelinek return (Z_ERR); 3401865e09a4Sgjelinek } 3402865e09a4Sgjelinek 3403865e09a4Sgjelinek if ((err = zone_get_zonepath(source_zone, source_zonepath, 3404865e09a4Sgjelinek sizeof (source_zonepath))) != Z_OK) { 3405865e09a4Sgjelinek errno = err; 3406865e09a4Sgjelinek zperror2(source_zone, gettext("could not get zone path")); 3407865e09a4Sgjelinek goto done; 3408865e09a4Sgjelinek } 3409865e09a4Sgjelinek 3410865e09a4Sgjelinek if ((err = zone_get_zonepath(target_zone, zonepath, sizeof (zonepath))) 3411865e09a4Sgjelinek != Z_OK) { 3412865e09a4Sgjelinek errno = err; 3413865e09a4Sgjelinek zperror2(target_zone, gettext("could not get zone path")); 3414865e09a4Sgjelinek goto done; 3415865e09a4Sgjelinek } 3416865e09a4Sgjelinek 3417865e09a4Sgjelinek if ((err = zone_set_state(target_zone, ZONE_STATE_INCOMPLETE)) 3418865e09a4Sgjelinek != Z_OK) { 3419865e09a4Sgjelinek errno = err; 3420865e09a4Sgjelinek zperror2(target_zone, gettext("could not set state")); 3421865e09a4Sgjelinek goto done; 3422865e09a4Sgjelinek } 3423865e09a4Sgjelinek 34240b5de56dSgjelinek if (snapshot != NULL) { 34250b5de56dSgjelinek err = clone_snapshot_zfs(snapshot, zonepath); 34260b5de56dSgjelinek } else { 34270b5de56dSgjelinek /* 34280b5de56dSgjelinek * We always copy the clone unless the source is ZFS and a 34290b5de56dSgjelinek * ZFS clone worked. We fallback to copying if the ZFS clone 34300b5de56dSgjelinek * fails for some reason. 34310b5de56dSgjelinek */ 34320b5de56dSgjelinek err = Z_ERR; 34330b5de56dSgjelinek if (method == NULL && is_zonepath_zfs(source_zonepath)) 34340b5de56dSgjelinek err = clone_zfs(source_zone, source_zonepath, zonepath); 3435865e09a4Sgjelinek 34365cd08338Sgjelinek if (err != Z_OK) 34370b5de56dSgjelinek err = clone_copy(source_zonepath, zonepath); 34380b5de56dSgjelinek } 3439865e09a4Sgjelinek 34400b5de56dSgjelinek if (err == Z_OK) 34415cd08338Sgjelinek err = unconfigure_zone(zonepath); 3442865e09a4Sgjelinek 3443865e09a4Sgjelinek done: 3444865e09a4Sgjelinek release_lock_file(lockfd); 3445865e09a4Sgjelinek return ((err == Z_OK) ? Z_OK : Z_ERR); 3446865e09a4Sgjelinek } 3447865e09a4Sgjelinek 3448865e09a4Sgjelinek #define RMCOMMAND "/usr/bin/rm -rf" 3449865e09a4Sgjelinek 345007b574eeSgjelinek /* 34510b5de56dSgjelinek * Used when removing a zonepath after uninstalling or cleaning up after 34520b5de56dSgjelinek * the move subcommand. This handles a zonepath that has non-standard 34530b5de56dSgjelinek * contents so that we will only cleanup the stuff we know about and leave 34540b5de56dSgjelinek * any user data alone. 345507b574eeSgjelinek * 34560b5de56dSgjelinek * If the "all" parameter is true then we should remove the whole zonepath 34570b5de56dSgjelinek * even if it has non-standard files/directories in it. This can be used when 34580b5de56dSgjelinek * we need to cleanup after moving the zonepath across file systems. 34590b5de56dSgjelinek * 34600b5de56dSgjelinek * We "exec" the RMCOMMAND so that the returned status is that of RMCOMMAND 34610b5de56dSgjelinek * and not the shell. 346207b574eeSgjelinek */ 346307b574eeSgjelinek static int 34640b5de56dSgjelinek cleanup_zonepath(char *zonepath, boolean_t all) 346507b574eeSgjelinek { 346607b574eeSgjelinek int status; 34670b5de56dSgjelinek int i; 34680b5de56dSgjelinek boolean_t non_std = B_FALSE; 34690b5de56dSgjelinek struct dirent *dp; 34700b5de56dSgjelinek DIR *dirp; 34710b5de56dSgjelinek char *std_entries[] = {"dev", "lu", "root", NULL}; 34720b5de56dSgjelinek /* (MAXPATHLEN * 3) is for the 3 std_entries dirs */ 34730b5de56dSgjelinek char cmdbuf[sizeof (RMCOMMAND) + (MAXPATHLEN * 3) + 64]; 347407b574eeSgjelinek 347507b574eeSgjelinek /* 34760b5de56dSgjelinek * We shouldn't need these checks but lets be paranoid since we 34770b5de56dSgjelinek * could blow away the whole system here if we got the wrong zonepath. 347807b574eeSgjelinek */ 34790b5de56dSgjelinek if (*zonepath == NULL || strcmp(zonepath, "/") == 0) { 34800b5de56dSgjelinek (void) fprintf(stderr, "invalid zonepath '%s'\n", zonepath); 34810b5de56dSgjelinek return (Z_INVAL); 34820b5de56dSgjelinek } 34830b5de56dSgjelinek 348407b574eeSgjelinek /* 34850b5de56dSgjelinek * If the dirpath is already gone (maybe it was manually removed) then 34860b5de56dSgjelinek * we just return Z_OK so that the cleanup is successful. 348707b574eeSgjelinek */ 34880b5de56dSgjelinek if ((dirp = opendir(zonepath)) == NULL) 34890b5de56dSgjelinek return (Z_OK); 34900b5de56dSgjelinek 34910b5de56dSgjelinek /* 34920b5de56dSgjelinek * Look through the zonepath directory to see if there are any 34930b5de56dSgjelinek * non-standard files/dirs. Also skip .zfs since that might be 34940b5de56dSgjelinek * there but we'll handle ZFS file systems as a special case. 34950b5de56dSgjelinek */ 34960b5de56dSgjelinek while ((dp = readdir(dirp)) != NULL) { 34970b5de56dSgjelinek if (strcmp(dp->d_name, ".") == 0 || 34980b5de56dSgjelinek strcmp(dp->d_name, "..") == 0 || 34990b5de56dSgjelinek strcmp(dp->d_name, ".zfs") == 0) 35000b5de56dSgjelinek continue; 35010b5de56dSgjelinek 35020b5de56dSgjelinek for (i = 0; std_entries[i] != NULL; i++) 35030b5de56dSgjelinek if (strcmp(dp->d_name, std_entries[i]) == 0) 35040b5de56dSgjelinek break; 35050b5de56dSgjelinek 35060b5de56dSgjelinek if (std_entries[i] == NULL) 35070b5de56dSgjelinek non_std = B_TRUE; 35080b5de56dSgjelinek } 35090b5de56dSgjelinek (void) closedir(dirp); 35100b5de56dSgjelinek 35110b5de56dSgjelinek if (!all && non_std) { 35120b5de56dSgjelinek /* 35130b5de56dSgjelinek * There are extra, non-standard directories/files in the 35140b5de56dSgjelinek * zonepath so we don't want to remove the zonepath. We 35150b5de56dSgjelinek * just want to remove the standard directories and leave 35160b5de56dSgjelinek * the user data alone. 35170b5de56dSgjelinek */ 35180b5de56dSgjelinek (void) snprintf(cmdbuf, sizeof (cmdbuf), "exec " RMCOMMAND); 35190b5de56dSgjelinek 35200b5de56dSgjelinek for (i = 0; std_entries[i] != NULL; i++) { 35210b5de56dSgjelinek char tmpbuf[MAXPATHLEN]; 35220b5de56dSgjelinek 35230b5de56dSgjelinek if (snprintf(tmpbuf, sizeof (tmpbuf), " %s/%s", 35240b5de56dSgjelinek zonepath, std_entries[i]) >= sizeof (tmpbuf) || 35250b5de56dSgjelinek strlcat(cmdbuf, tmpbuf, sizeof (cmdbuf)) >= 35260b5de56dSgjelinek sizeof (cmdbuf)) { 35270b5de56dSgjelinek (void) fprintf(stderr, 35280b5de56dSgjelinek gettext("path is too long\n")); 35290b5de56dSgjelinek return (Z_INVAL); 35300b5de56dSgjelinek } 353107b574eeSgjelinek } 353207b574eeSgjelinek 353307b574eeSgjelinek status = do_subproc(cmdbuf); 353407b574eeSgjelinek 35350b5de56dSgjelinek (void) fprintf(stderr, gettext("WARNING: Unable to completely " 35360b5de56dSgjelinek "remove %s\nbecause it contains additional user data. " 35370b5de56dSgjelinek "Only the standard directory\nentries have been " 35380b5de56dSgjelinek "removed.\n"), 35390b5de56dSgjelinek zonepath); 354007b574eeSgjelinek 35410b5de56dSgjelinek return (subproc_status(RMCOMMAND, status)); 35420b5de56dSgjelinek } 35430b5de56dSgjelinek 35440b5de56dSgjelinek /* 35450b5de56dSgjelinek * There is nothing unexpected in the zonepath, try to get rid of the 35460b5de56dSgjelinek * whole zonepath directory. 35470b5de56dSgjelinek * 35480b5de56dSgjelinek * If the zonepath is its own zfs file system, try to destroy the 35490b5de56dSgjelinek * file system. If that fails for some reason (e.g. it has clones) 35500b5de56dSgjelinek * then we'll just remove the contents of the zonepath. 35510b5de56dSgjelinek */ 35520b5de56dSgjelinek if (is_zonepath_zfs(zonepath)) { 35530b5de56dSgjelinek if (destroy_zfs(zonepath) == Z_OK) 35540b5de56dSgjelinek return (Z_OK); 35550b5de56dSgjelinek (void) snprintf(cmdbuf, sizeof (cmdbuf), "exec " RMCOMMAND 35560b5de56dSgjelinek " %s/*", zonepath); 35570b5de56dSgjelinek status = do_subproc(cmdbuf); 35580b5de56dSgjelinek return (subproc_status(RMCOMMAND, status)); 35590b5de56dSgjelinek } 35600b5de56dSgjelinek 35610b5de56dSgjelinek (void) snprintf(cmdbuf, sizeof (cmdbuf), "exec " RMCOMMAND " %s", 35620b5de56dSgjelinek zonepath); 35630b5de56dSgjelinek status = do_subproc(cmdbuf); 35640b5de56dSgjelinek return (subproc_status(RMCOMMAND, status)); 356507b574eeSgjelinek } 356607b574eeSgjelinek 3567865e09a4Sgjelinek static int 3568865e09a4Sgjelinek move_func(int argc, char *argv[]) 3569865e09a4Sgjelinek { 3570865e09a4Sgjelinek char *new_zonepath = NULL; 3571865e09a4Sgjelinek int lockfd; 3572865e09a4Sgjelinek int err, arg; 3573865e09a4Sgjelinek char zonepath[MAXPATHLEN]; 3574865e09a4Sgjelinek zone_dochandle_t handle; 3575865e09a4Sgjelinek boolean_t fast; 35760b5de56dSgjelinek boolean_t is_zfs = B_FALSE; 35770b5de56dSgjelinek struct dirent *dp; 35780b5de56dSgjelinek DIR *dirp; 35790b5de56dSgjelinek boolean_t empty = B_TRUE; 3580865e09a4Sgjelinek boolean_t revert; 3581865e09a4Sgjelinek struct stat zonepath_buf; 3582865e09a4Sgjelinek struct stat new_zonepath_buf; 3583865e09a4Sgjelinek 3584865e09a4Sgjelinek if (zonecfg_in_alt_root()) { 3585865e09a4Sgjelinek zerror(gettext("cannot move zone in alternate root")); 3586865e09a4Sgjelinek return (Z_ERR); 3587865e09a4Sgjelinek } 3588865e09a4Sgjelinek 3589865e09a4Sgjelinek optind = 0; 3590865e09a4Sgjelinek if ((arg = getopt(argc, argv, "?")) != EOF) { 3591865e09a4Sgjelinek switch (arg) { 3592865e09a4Sgjelinek case '?': 3593865e09a4Sgjelinek sub_usage(SHELP_MOVE, CMD_MOVE); 3594865e09a4Sgjelinek return (optopt == '?' ? Z_OK : Z_USAGE); 3595865e09a4Sgjelinek default: 3596865e09a4Sgjelinek sub_usage(SHELP_MOVE, CMD_MOVE); 3597865e09a4Sgjelinek return (Z_USAGE); 3598865e09a4Sgjelinek } 3599865e09a4Sgjelinek } 3600865e09a4Sgjelinek if (argc != (optind + 1)) { 3601865e09a4Sgjelinek sub_usage(SHELP_MOVE, CMD_MOVE); 3602865e09a4Sgjelinek return (Z_USAGE); 3603865e09a4Sgjelinek } 3604865e09a4Sgjelinek new_zonepath = argv[optind]; 3605865e09a4Sgjelinek if (sanity_check(target_zone, CMD_MOVE, B_FALSE, B_TRUE) != Z_OK) 3606865e09a4Sgjelinek return (Z_ERR); 3607865e09a4Sgjelinek if (verify_details(CMD_MOVE) != Z_OK) 3608865e09a4Sgjelinek return (Z_ERR); 3609865e09a4Sgjelinek 3610865e09a4Sgjelinek /* 3611865e09a4Sgjelinek * Check out the new zonepath. This has the side effect of creating 3612865e09a4Sgjelinek * a directory for the new zonepath. We depend on this later when we 36130b5de56dSgjelinek * stat to see if we are doing a cross file system move or not. 3614865e09a4Sgjelinek */ 3615865e09a4Sgjelinek if (validate_zonepath(new_zonepath, CMD_MOVE) != Z_OK) 3616865e09a4Sgjelinek return (Z_ERR); 3617865e09a4Sgjelinek 3618865e09a4Sgjelinek if ((err = zone_get_zonepath(target_zone, zonepath, sizeof (zonepath))) 3619865e09a4Sgjelinek != Z_OK) { 3620865e09a4Sgjelinek errno = err; 3621865e09a4Sgjelinek zperror2(target_zone, gettext("could not get zone path")); 3622865e09a4Sgjelinek return (Z_ERR); 3623865e09a4Sgjelinek } 3624865e09a4Sgjelinek 3625865e09a4Sgjelinek if (stat(zonepath, &zonepath_buf) == -1) { 3626865e09a4Sgjelinek zperror(gettext("could not stat zone path"), B_FALSE); 3627865e09a4Sgjelinek return (Z_ERR); 3628865e09a4Sgjelinek } 3629865e09a4Sgjelinek 3630865e09a4Sgjelinek if (stat(new_zonepath, &new_zonepath_buf) == -1) { 3631865e09a4Sgjelinek zperror(gettext("could not stat new zone path"), B_FALSE); 3632865e09a4Sgjelinek return (Z_ERR); 3633865e09a4Sgjelinek } 3634865e09a4Sgjelinek 36350b5de56dSgjelinek /* 36360b5de56dSgjelinek * Check if the destination directory is empty. 36370b5de56dSgjelinek */ 36380b5de56dSgjelinek if ((dirp = opendir(new_zonepath)) == NULL) { 36390b5de56dSgjelinek zperror(gettext("could not open new zone path"), B_FALSE); 36400b5de56dSgjelinek return (Z_ERR); 36410b5de56dSgjelinek } 36420b5de56dSgjelinek while ((dp = readdir(dirp)) != (struct dirent *)0) { 36430b5de56dSgjelinek if (strcmp(dp->d_name, ".") == 0 || 36440b5de56dSgjelinek strcmp(dp->d_name, "..") == 0) 36450b5de56dSgjelinek continue; 36460b5de56dSgjelinek empty = B_FALSE; 36470b5de56dSgjelinek break; 36480b5de56dSgjelinek } 36490b5de56dSgjelinek (void) closedir(dirp); 36500b5de56dSgjelinek 36510b5de56dSgjelinek /* Error if there is anything in the destination directory. */ 36520b5de56dSgjelinek if (!empty) { 36530b5de56dSgjelinek (void) fprintf(stderr, gettext("could not move zone to %s: " 36540b5de56dSgjelinek "directory not empty\n"), new_zonepath); 36550b5de56dSgjelinek return (Z_ERR); 36560b5de56dSgjelinek } 36570b5de56dSgjelinek 3658865e09a4Sgjelinek /* Don't move the zone if anything is still mounted there */ 3659865e09a4Sgjelinek if (zonecfg_find_mounts(zonepath, NULL, NULL)) { 36600b5de56dSgjelinek zerror(gettext("These file systems are mounted on " 3661865e09a4Sgjelinek "subdirectories of %s.\n"), zonepath); 3662865e09a4Sgjelinek (void) zonecfg_find_mounts(zonepath, zfm_print, NULL); 3663865e09a4Sgjelinek return (Z_ERR); 3664865e09a4Sgjelinek } 3665865e09a4Sgjelinek 3666865e09a4Sgjelinek /* 3667865e09a4Sgjelinek * Check if we are moving in the same file system and can do a fast 3668865e09a4Sgjelinek * move or if we are crossing file systems and have to copy the data. 3669865e09a4Sgjelinek */ 3670865e09a4Sgjelinek fast = (zonepath_buf.st_dev == new_zonepath_buf.st_dev); 3671865e09a4Sgjelinek 3672865e09a4Sgjelinek if ((handle = zonecfg_init_handle()) == NULL) { 3673865e09a4Sgjelinek zperror(cmd_to_str(CMD_MOVE), B_TRUE); 3674865e09a4Sgjelinek return (Z_ERR); 3675865e09a4Sgjelinek } 3676865e09a4Sgjelinek 3677865e09a4Sgjelinek if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) { 3678865e09a4Sgjelinek errno = err; 3679865e09a4Sgjelinek zperror(cmd_to_str(CMD_MOVE), B_TRUE); 3680865e09a4Sgjelinek zonecfg_fini_handle(handle); 3681865e09a4Sgjelinek return (Z_ERR); 3682865e09a4Sgjelinek } 3683865e09a4Sgjelinek 3684865e09a4Sgjelinek if (grab_lock_file(target_zone, &lockfd) != Z_OK) { 3685865e09a4Sgjelinek zerror(gettext("another %s may have an operation in progress."), 3686865e09a4Sgjelinek "zoneadm"); 3687865e09a4Sgjelinek zonecfg_fini_handle(handle); 3688865e09a4Sgjelinek return (Z_ERR); 3689865e09a4Sgjelinek } 3690865e09a4Sgjelinek 3691865e09a4Sgjelinek /* 36920b5de56dSgjelinek * We're making some file system changes now so we have to clean up 36930b5de56dSgjelinek * the file system before we are done. This will either clean up the 3694865e09a4Sgjelinek * new zonepath if the zonecfg update failed or it will clean up the 3695865e09a4Sgjelinek * old zonepath if everything is ok. 3696865e09a4Sgjelinek */ 3697865e09a4Sgjelinek revert = B_TRUE; 3698865e09a4Sgjelinek 36990b5de56dSgjelinek if (is_zonepath_zfs(zonepath) && 37000b5de56dSgjelinek move_zfs(zonepath, new_zonepath) != Z_ERR) { 37010b5de56dSgjelinek is_zfs = B_TRUE; 37020b5de56dSgjelinek 37030b5de56dSgjelinek } else if (fast) { 3704865e09a4Sgjelinek /* same file system, use rename for a quick move */ 3705865e09a4Sgjelinek 3706865e09a4Sgjelinek /* 3707865e09a4Sgjelinek * Remove the new_zonepath directory that got created above 3708865e09a4Sgjelinek * during the validation. It gets in the way of the rename. 3709865e09a4Sgjelinek */ 3710865e09a4Sgjelinek if (rmdir(new_zonepath) != 0) { 3711865e09a4Sgjelinek zperror(gettext("could not rmdir new zone path"), 3712865e09a4Sgjelinek B_FALSE); 3713865e09a4Sgjelinek zonecfg_fini_handle(handle); 3714865e09a4Sgjelinek release_lock_file(lockfd); 3715865e09a4Sgjelinek return (Z_ERR); 3716865e09a4Sgjelinek } 3717865e09a4Sgjelinek 3718865e09a4Sgjelinek if (rename(zonepath, new_zonepath) != 0) { 3719865e09a4Sgjelinek /* 3720865e09a4Sgjelinek * If this fails we don't need to do all of the 3721865e09a4Sgjelinek * cleanup that happens for the rest of the code 3722865e09a4Sgjelinek * so just return from this error. 3723865e09a4Sgjelinek */ 3724865e09a4Sgjelinek zperror(gettext("could not move zone"), B_FALSE); 3725865e09a4Sgjelinek zonecfg_fini_handle(handle); 3726865e09a4Sgjelinek release_lock_file(lockfd); 3727865e09a4Sgjelinek return (Z_ERR); 3728865e09a4Sgjelinek } 3729865e09a4Sgjelinek 3730865e09a4Sgjelinek } else { 37310b5de56dSgjelinek /* 37320b5de56dSgjelinek * Attempt to create a ZFS fs for the new zonepath. As usual, 37330b5de56dSgjelinek * we don't care if this works or not since we always have the 37340b5de56dSgjelinek * default behavior of a simple directory for the zonepath. 37350b5de56dSgjelinek */ 37360b5de56dSgjelinek create_zfs_zonepath(new_zonepath); 37370b5de56dSgjelinek 3738865e09a4Sgjelinek (void) printf(gettext( 37390b5de56dSgjelinek "Moving across file systems; copying zonepath %s..."), 3740865e09a4Sgjelinek zonepath); 3741865e09a4Sgjelinek (void) fflush(stdout); 3742865e09a4Sgjelinek 3743865e09a4Sgjelinek err = copy_zone(zonepath, new_zonepath); 3744865e09a4Sgjelinek 3745865e09a4Sgjelinek (void) printf("\n"); 3746865e09a4Sgjelinek if (err != Z_OK) 3747865e09a4Sgjelinek goto done; 3748865e09a4Sgjelinek } 3749865e09a4Sgjelinek 3750865e09a4Sgjelinek if ((err = zonecfg_set_zonepath(handle, new_zonepath)) != Z_OK) { 3751865e09a4Sgjelinek errno = err; 3752865e09a4Sgjelinek zperror(gettext("could not set new zonepath"), B_TRUE); 3753865e09a4Sgjelinek goto done; 3754865e09a4Sgjelinek } 3755865e09a4Sgjelinek 3756865e09a4Sgjelinek if ((err = zonecfg_save(handle)) != Z_OK) { 3757865e09a4Sgjelinek errno = err; 3758865e09a4Sgjelinek zperror(gettext("zonecfg save failed"), B_TRUE); 3759865e09a4Sgjelinek goto done; 3760865e09a4Sgjelinek } 3761865e09a4Sgjelinek 3762865e09a4Sgjelinek revert = B_FALSE; 3763865e09a4Sgjelinek 3764865e09a4Sgjelinek done: 3765865e09a4Sgjelinek zonecfg_fini_handle(handle); 3766865e09a4Sgjelinek release_lock_file(lockfd); 3767865e09a4Sgjelinek 3768865e09a4Sgjelinek /* 37690b5de56dSgjelinek * Clean up the file system based on how things went. We either 3770865e09a4Sgjelinek * clean up the new zonepath if the operation failed for some reason 3771865e09a4Sgjelinek * or we clean up the old zonepath if everything is ok. 3772865e09a4Sgjelinek */ 3773865e09a4Sgjelinek if (revert) { 3774865e09a4Sgjelinek /* The zonecfg update failed, cleanup the new zonepath. */ 37750b5de56dSgjelinek if (is_zfs) { 37760b5de56dSgjelinek if (move_zfs(new_zonepath, zonepath) == Z_ERR) { 37770b5de56dSgjelinek (void) fprintf(stderr, gettext("could not " 37780b5de56dSgjelinek "restore zonepath, the zfs mountpoint is " 37790b5de56dSgjelinek "set as:\n%s\n"), new_zonepath); 37800b5de56dSgjelinek /* 37810b5de56dSgjelinek * err is already != Z_OK since we're reverting 37820b5de56dSgjelinek */ 37830b5de56dSgjelinek } 37840b5de56dSgjelinek 37850b5de56dSgjelinek } else if (fast) { 3786865e09a4Sgjelinek if (rename(new_zonepath, zonepath) != 0) { 3787865e09a4Sgjelinek zperror(gettext("could not restore zonepath"), 3788865e09a4Sgjelinek B_FALSE); 3789865e09a4Sgjelinek /* 3790865e09a4Sgjelinek * err is already != Z_OK since we're reverting 3791865e09a4Sgjelinek */ 3792865e09a4Sgjelinek } 3793865e09a4Sgjelinek } else { 3794865e09a4Sgjelinek (void) printf(gettext("Cleaning up zonepath %s..."), 3795865e09a4Sgjelinek new_zonepath); 3796865e09a4Sgjelinek (void) fflush(stdout); 37970b5de56dSgjelinek err = cleanup_zonepath(new_zonepath, B_TRUE); 3798865e09a4Sgjelinek (void) printf("\n"); 3799865e09a4Sgjelinek 380007b574eeSgjelinek if (err != Z_OK) { 3801865e09a4Sgjelinek errno = err; 3802865e09a4Sgjelinek zperror(gettext("could not remove new " 3803865e09a4Sgjelinek "zonepath"), B_TRUE); 3804865e09a4Sgjelinek } else { 3805865e09a4Sgjelinek /* 3806865e09a4Sgjelinek * Because we're reverting we know the mainline 3807865e09a4Sgjelinek * code failed but we just reused the err 3808865e09a4Sgjelinek * variable so we reset it back to Z_ERR. 3809865e09a4Sgjelinek */ 3810865e09a4Sgjelinek err = Z_ERR; 3811865e09a4Sgjelinek } 3812865e09a4Sgjelinek } 3813865e09a4Sgjelinek 3814865e09a4Sgjelinek } else { 3815865e09a4Sgjelinek /* The move was successful, cleanup the old zonepath. */ 38160b5de56dSgjelinek if (!is_zfs && !fast) { 3817865e09a4Sgjelinek (void) printf( 3818865e09a4Sgjelinek gettext("Cleaning up zonepath %s..."), zonepath); 3819865e09a4Sgjelinek (void) fflush(stdout); 38200b5de56dSgjelinek err = cleanup_zonepath(zonepath, B_TRUE); 3821865e09a4Sgjelinek (void) printf("\n"); 3822865e09a4Sgjelinek 382307b574eeSgjelinek if (err != Z_OK) { 3824865e09a4Sgjelinek errno = err; 3825865e09a4Sgjelinek zperror(gettext("could not remove zonepath"), 3826865e09a4Sgjelinek B_TRUE); 3827865e09a4Sgjelinek } 3828865e09a4Sgjelinek } 3829865e09a4Sgjelinek } 3830865e09a4Sgjelinek 3831865e09a4Sgjelinek return ((err == Z_OK) ? Z_OK : Z_ERR); 3832865e09a4Sgjelinek } 3833865e09a4Sgjelinek 3834ee519a1fSgjelinek static int 3835ee519a1fSgjelinek detach_func(int argc, char *argv[]) 3836ee519a1fSgjelinek { 3837ee519a1fSgjelinek int lockfd; 3838ee519a1fSgjelinek int err, arg; 3839ee519a1fSgjelinek char zonepath[MAXPATHLEN]; 3840ee519a1fSgjelinek zone_dochandle_t handle; 38418cd327d5Sgjelinek boolean_t execute = B_TRUE; 3842ee519a1fSgjelinek 3843ee519a1fSgjelinek if (zonecfg_in_alt_root()) { 3844ee519a1fSgjelinek zerror(gettext("cannot detach zone in alternate root")); 3845ee519a1fSgjelinek return (Z_ERR); 3846ee519a1fSgjelinek } 3847ee519a1fSgjelinek 3848ee519a1fSgjelinek optind = 0; 38498cd327d5Sgjelinek if ((arg = getopt(argc, argv, "?n")) != EOF) { 3850ee519a1fSgjelinek switch (arg) { 3851ee519a1fSgjelinek case '?': 3852ee519a1fSgjelinek sub_usage(SHELP_DETACH, CMD_DETACH); 3853ee519a1fSgjelinek return (optopt == '?' ? Z_OK : Z_USAGE); 38548cd327d5Sgjelinek case 'n': 38558cd327d5Sgjelinek execute = B_FALSE; 38568cd327d5Sgjelinek break; 3857ee519a1fSgjelinek default: 3858ee519a1fSgjelinek sub_usage(SHELP_DETACH, CMD_DETACH); 3859ee519a1fSgjelinek return (Z_USAGE); 3860ee519a1fSgjelinek } 3861ee519a1fSgjelinek } 38628cd327d5Sgjelinek if (execute) { 38638cd327d5Sgjelinek if (sanity_check(target_zone, CMD_DETACH, B_FALSE, B_TRUE) 38648cd327d5Sgjelinek != Z_OK) 3865ee519a1fSgjelinek return (Z_ERR); 3866ee519a1fSgjelinek if (verify_details(CMD_DETACH) != Z_OK) 3867ee519a1fSgjelinek return (Z_ERR); 38688cd327d5Sgjelinek } else { 38698cd327d5Sgjelinek /* 38708cd327d5Sgjelinek * We want a dry-run to work for a non-privileged user so we 38718cd327d5Sgjelinek * only do minimal validation. 38728cd327d5Sgjelinek */ 38738cd327d5Sgjelinek if (getzoneid() != GLOBAL_ZONEID) { 38748cd327d5Sgjelinek zerror(gettext("must be in the global zone to %s a " 38758cd327d5Sgjelinek "zone."), cmd_to_str(CMD_DETACH)); 38768cd327d5Sgjelinek return (Z_ERR); 38778cd327d5Sgjelinek } 38788cd327d5Sgjelinek 38798cd327d5Sgjelinek if (target_zone == NULL) { 38808cd327d5Sgjelinek zerror(gettext("no zone specified")); 38818cd327d5Sgjelinek return (Z_ERR); 38828cd327d5Sgjelinek } 38838cd327d5Sgjelinek 38848cd327d5Sgjelinek if (strcmp(target_zone, GLOBAL_ZONENAME) == 0) { 38858cd327d5Sgjelinek zerror(gettext("%s operation is invalid for the " 38868cd327d5Sgjelinek "global zone."), cmd_to_str(CMD_DETACH)); 38878cd327d5Sgjelinek return (Z_ERR); 38888cd327d5Sgjelinek } 38898cd327d5Sgjelinek } 3890ee519a1fSgjelinek 3891ee519a1fSgjelinek if ((err = zone_get_zonepath(target_zone, zonepath, sizeof (zonepath))) 3892ee519a1fSgjelinek != Z_OK) { 3893ee519a1fSgjelinek errno = err; 3894ee519a1fSgjelinek zperror2(target_zone, gettext("could not get zone path")); 3895ee519a1fSgjelinek return (Z_ERR); 3896ee519a1fSgjelinek } 3897ee519a1fSgjelinek 3898ee519a1fSgjelinek /* Don't detach the zone if anything is still mounted there */ 38998cd327d5Sgjelinek if (execute && zonecfg_find_mounts(zonepath, NULL, NULL)) { 39000b5de56dSgjelinek zerror(gettext("These file systems are mounted on " 3901ee519a1fSgjelinek "subdirectories of %s.\n"), zonepath); 3902ee519a1fSgjelinek (void) zonecfg_find_mounts(zonepath, zfm_print, NULL); 3903ee519a1fSgjelinek return (Z_ERR); 3904ee519a1fSgjelinek } 3905ee519a1fSgjelinek 3906ee519a1fSgjelinek if ((handle = zonecfg_init_handle()) == NULL) { 3907ee519a1fSgjelinek zperror(cmd_to_str(CMD_DETACH), B_TRUE); 3908ee519a1fSgjelinek return (Z_ERR); 3909ee519a1fSgjelinek } 3910ee519a1fSgjelinek 3911ee519a1fSgjelinek if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) { 3912ee519a1fSgjelinek errno = err; 3913ee519a1fSgjelinek zperror(cmd_to_str(CMD_DETACH), B_TRUE); 3914ee519a1fSgjelinek zonecfg_fini_handle(handle); 3915ee519a1fSgjelinek return (Z_ERR); 3916ee519a1fSgjelinek } 3917ee519a1fSgjelinek 39188cd327d5Sgjelinek if (execute && grab_lock_file(target_zone, &lockfd) != Z_OK) { 3919ee519a1fSgjelinek zerror(gettext("another %s may have an operation in progress."), 3920ee519a1fSgjelinek "zoneadm"); 3921ee519a1fSgjelinek zonecfg_fini_handle(handle); 3922ee519a1fSgjelinek return (Z_ERR); 3923ee519a1fSgjelinek } 3924ee519a1fSgjelinek 3925ee519a1fSgjelinek if ((err = zonecfg_get_detach_info(handle, B_TRUE)) != Z_OK) { 3926ee519a1fSgjelinek errno = err; 3927ee519a1fSgjelinek zperror(gettext("getting the detach information failed"), 3928ee519a1fSgjelinek B_TRUE); 3929ee519a1fSgjelinek goto done; 3930ee519a1fSgjelinek } 3931ee519a1fSgjelinek 39328cd327d5Sgjelinek if ((err = zonecfg_detach_save(handle, (execute ? 0 : ZONE_DRY_RUN))) 39338cd327d5Sgjelinek != Z_OK) { 3934ee519a1fSgjelinek errno = err; 3935ee519a1fSgjelinek zperror(gettext("saving the detach manifest failed"), B_TRUE); 3936ee519a1fSgjelinek goto done; 3937ee519a1fSgjelinek } 3938ee519a1fSgjelinek 39398cd327d5Sgjelinek /* 39408cd327d5Sgjelinek * Set the zone state back to configured unless we are running with the 39418cd327d5Sgjelinek * no-execute option. 39428cd327d5Sgjelinek */ 39438cd327d5Sgjelinek if (execute && (err = zone_set_state(target_zone, 39448cd327d5Sgjelinek ZONE_STATE_CONFIGURED)) != Z_OK) { 3945ee519a1fSgjelinek errno = err; 3946ee519a1fSgjelinek zperror(gettext("could not reset state"), B_TRUE); 3947ee519a1fSgjelinek } 3948ee519a1fSgjelinek 3949ee519a1fSgjelinek done: 3950ee519a1fSgjelinek zonecfg_fini_handle(handle); 39518cd327d5Sgjelinek if (execute) 3952ee519a1fSgjelinek release_lock_file(lockfd); 3953ee519a1fSgjelinek 3954ee519a1fSgjelinek return ((err == Z_OK) ? Z_OK : Z_ERR); 3955ee519a1fSgjelinek } 3956ee519a1fSgjelinek 3957ee519a1fSgjelinek /* 3958ee519a1fSgjelinek * During attach we go through and fix up the /dev entries for the zone 3959ee519a1fSgjelinek * we are attaching. In order to regenerate /dev with the correct devices, 3960ee519a1fSgjelinek * the old /dev will be removed, the zone readied (which generates a new 3961ee519a1fSgjelinek * /dev) then halted, then we use the info from the manifest to update 3962ee519a1fSgjelinek * the modes, owners, etc. on the new /dev. 3963ee519a1fSgjelinek */ 3964ee519a1fSgjelinek static int 3965ee519a1fSgjelinek dev_fix(zone_dochandle_t handle) 3966ee519a1fSgjelinek { 3967ee519a1fSgjelinek int res; 3968ee519a1fSgjelinek int err; 3969ee519a1fSgjelinek int status; 3970ee519a1fSgjelinek struct zone_devpermtab devtab; 3971ee519a1fSgjelinek zone_cmd_arg_t zarg; 3972ee519a1fSgjelinek char devpath[MAXPATHLEN]; 3973ee519a1fSgjelinek /* 6: "exec " and " " */ 3974ee519a1fSgjelinek char cmdbuf[sizeof (RMCOMMAND) + MAXPATHLEN + 6]; 3975ee519a1fSgjelinek 3976ee519a1fSgjelinek if ((res = zonecfg_get_zonepath(handle, devpath, sizeof (devpath))) 3977ee519a1fSgjelinek != Z_OK) 3978ee519a1fSgjelinek return (res); 3979ee519a1fSgjelinek 3980ee519a1fSgjelinek if (strlcat(devpath, "/dev", sizeof (devpath)) >= sizeof (devpath)) 3981ee519a1fSgjelinek return (Z_TOO_BIG); 3982ee519a1fSgjelinek 3983ee519a1fSgjelinek /* 3984ee519a1fSgjelinek * "exec" the command so that the returned status is that of 3985ee519a1fSgjelinek * RMCOMMAND and not the shell. 3986ee519a1fSgjelinek */ 3987ee519a1fSgjelinek (void) snprintf(cmdbuf, sizeof (cmdbuf), "exec " RMCOMMAND " %s", 3988ee519a1fSgjelinek devpath); 3989ee519a1fSgjelinek status = do_subproc(cmdbuf); 3990ee519a1fSgjelinek if ((err = subproc_status(RMCOMMAND, status)) != Z_OK) { 3991ee519a1fSgjelinek (void) fprintf(stderr, 3992ee519a1fSgjelinek gettext("could not remove existing /dev\n")); 3993ee519a1fSgjelinek return (Z_ERR); 3994ee519a1fSgjelinek } 3995ee519a1fSgjelinek 3996ee519a1fSgjelinek /* In order to ready the zone, it must be in the installed state */ 3997ee519a1fSgjelinek if ((err = zone_set_state(target_zone, ZONE_STATE_INSTALLED)) != Z_OK) { 3998ee519a1fSgjelinek errno = err; 3999ee519a1fSgjelinek zperror(gettext("could not reset state"), B_TRUE); 4000ee519a1fSgjelinek return (Z_ERR); 4001ee519a1fSgjelinek } 4002ee519a1fSgjelinek 4003ee519a1fSgjelinek /* We have to ready the zone to regen the dev tree */ 4004ee519a1fSgjelinek zarg.cmd = Z_READY; 4005ee519a1fSgjelinek if (call_zoneadmd(target_zone, &zarg) != 0) { 4006ee519a1fSgjelinek zerror(gettext("call to %s failed"), "zoneadmd"); 4007ee519a1fSgjelinek return (Z_ERR); 4008ee519a1fSgjelinek } 4009ee519a1fSgjelinek 4010ee519a1fSgjelinek zarg.cmd = Z_HALT; 4011ee519a1fSgjelinek if (call_zoneadmd(target_zone, &zarg) != 0) { 4012ee519a1fSgjelinek zerror(gettext("call to %s failed"), "zoneadmd"); 4013ee519a1fSgjelinek return (Z_ERR); 4014ee519a1fSgjelinek } 4015ee519a1fSgjelinek 4016ee519a1fSgjelinek if (zonecfg_setdevperment(handle) != Z_OK) { 4017ee519a1fSgjelinek (void) fprintf(stderr, 4018ee519a1fSgjelinek gettext("unable to enumerate device entries\n")); 4019ee519a1fSgjelinek return (Z_ERR); 4020ee519a1fSgjelinek } 4021ee519a1fSgjelinek 4022ee519a1fSgjelinek while (zonecfg_getdevperment(handle, &devtab) == Z_OK) { 4023ee519a1fSgjelinek int err; 4024ee519a1fSgjelinek 4025ee519a1fSgjelinek if ((err = zonecfg_devperms_apply(handle, 4026ee519a1fSgjelinek devtab.zone_devperm_name, devtab.zone_devperm_uid, 4027ee519a1fSgjelinek devtab.zone_devperm_gid, devtab.zone_devperm_mode, 4028ee519a1fSgjelinek devtab.zone_devperm_acl)) != Z_OK && err != Z_INVAL) 4029ee519a1fSgjelinek (void) fprintf(stderr, gettext("error updating device " 4030ee519a1fSgjelinek "%s: %s\n"), devtab.zone_devperm_name, 4031ee519a1fSgjelinek zonecfg_strerror(err)); 4032ee519a1fSgjelinek 4033ee519a1fSgjelinek free(devtab.zone_devperm_acl); 4034ee519a1fSgjelinek } 4035ee519a1fSgjelinek 4036ee519a1fSgjelinek (void) zonecfg_enddevperment(handle); 4037ee519a1fSgjelinek 4038ee519a1fSgjelinek return (Z_OK); 4039ee519a1fSgjelinek } 4040ee519a1fSgjelinek 40418cd327d5Sgjelinek /* 40428cd327d5Sgjelinek * Validate attaching a zone but don't actually do the work. The zone 40438cd327d5Sgjelinek * does not have to exist, so there is some complexity getting a new zone 40448cd327d5Sgjelinek * configuration set up so that we can perform the validation. This is 40458cd327d5Sgjelinek * handled within zonecfg_attach_manifest() which returns two handles; one 40468cd327d5Sgjelinek * for the the full configuration to validate (rem_handle) and the other 40478cd327d5Sgjelinek * (local_handle) containing only the zone configuration derived from the 40488cd327d5Sgjelinek * manifest. 40498cd327d5Sgjelinek */ 40508cd327d5Sgjelinek static int 40518cd327d5Sgjelinek dryrun_attach(char *manifest_path) 40528cd327d5Sgjelinek { 40538cd327d5Sgjelinek int fd; 40548cd327d5Sgjelinek int err; 40558cd327d5Sgjelinek int res; 40568cd327d5Sgjelinek zone_dochandle_t local_handle; 40578cd327d5Sgjelinek zone_dochandle_t rem_handle = NULL; 40588cd327d5Sgjelinek 40598cd327d5Sgjelinek if (strcmp(manifest_path, "-") == 0) { 40608cd327d5Sgjelinek fd = 0; 40618cd327d5Sgjelinek } else if ((fd = open(manifest_path, O_RDONLY)) < 0) { 40628cd327d5Sgjelinek zperror(gettext("could not open manifest path"), B_FALSE); 40638cd327d5Sgjelinek return (Z_ERR); 40648cd327d5Sgjelinek } 40658cd327d5Sgjelinek 40668cd327d5Sgjelinek if ((local_handle = zonecfg_init_handle()) == NULL) { 40678cd327d5Sgjelinek zperror(cmd_to_str(CMD_ATTACH), B_TRUE); 40688cd327d5Sgjelinek res = Z_ERR; 40698cd327d5Sgjelinek goto done; 40708cd327d5Sgjelinek } 40718cd327d5Sgjelinek 40728cd327d5Sgjelinek if ((rem_handle = zonecfg_init_handle()) == NULL) { 40738cd327d5Sgjelinek zperror(cmd_to_str(CMD_ATTACH), B_TRUE); 40748cd327d5Sgjelinek res = Z_ERR; 40758cd327d5Sgjelinek goto done; 40768cd327d5Sgjelinek } 40778cd327d5Sgjelinek 40788cd327d5Sgjelinek if ((err = zonecfg_attach_manifest(fd, local_handle, rem_handle)) 40798cd327d5Sgjelinek != Z_OK) { 40808cd327d5Sgjelinek if (err == Z_INVALID_DOCUMENT) 40818cd327d5Sgjelinek zerror(gettext("Cannot attach to an earlier release " 40828cd327d5Sgjelinek "of the operating system")); 40838cd327d5Sgjelinek else 40848cd327d5Sgjelinek zperror(cmd_to_str(CMD_ATTACH), B_TRUE); 40858cd327d5Sgjelinek res = Z_ERR; 40868cd327d5Sgjelinek goto done; 40878cd327d5Sgjelinek } 40888cd327d5Sgjelinek 40898cd327d5Sgjelinek res = verify_handle(CMD_ATTACH, local_handle); 40908cd327d5Sgjelinek 40918cd327d5Sgjelinek /* Get the detach information for the locally defined zone. */ 40928cd327d5Sgjelinek if ((err = zonecfg_get_detach_info(local_handle, B_FALSE)) != Z_OK) { 40938cd327d5Sgjelinek errno = err; 40948cd327d5Sgjelinek zperror(gettext("getting the attach information failed"), 40958cd327d5Sgjelinek B_TRUE); 40968cd327d5Sgjelinek res = Z_ERR; 40978cd327d5Sgjelinek } else { 40988cd327d5Sgjelinek /* sw_cmp prints error msgs as necessary */ 40998cd327d5Sgjelinek if (sw_cmp(local_handle, rem_handle, SW_CMP_NONE) != Z_OK) 41008cd327d5Sgjelinek res = Z_ERR; 41018cd327d5Sgjelinek } 41028cd327d5Sgjelinek 41038cd327d5Sgjelinek done: 41048cd327d5Sgjelinek if (strcmp(manifest_path, "-") != 0) 41058cd327d5Sgjelinek (void) close(fd); 41068cd327d5Sgjelinek 41078cd327d5Sgjelinek zonecfg_fini_handle(local_handle); 41088cd327d5Sgjelinek zonecfg_fini_handle(rem_handle); 41098cd327d5Sgjelinek 41108cd327d5Sgjelinek return ((res == Z_OK) ? Z_OK : Z_ERR); 41118cd327d5Sgjelinek } 41128cd327d5Sgjelinek 4113ee519a1fSgjelinek static int 4114ee519a1fSgjelinek attach_func(int argc, char *argv[]) 4115ee519a1fSgjelinek { 4116ee519a1fSgjelinek int lockfd; 4117ee519a1fSgjelinek int err, arg; 4118ee519a1fSgjelinek boolean_t force = B_FALSE; 4119ee519a1fSgjelinek zone_dochandle_t handle; 4120ee519a1fSgjelinek zone_dochandle_t athandle = NULL; 4121ee519a1fSgjelinek char zonepath[MAXPATHLEN]; 41228cd327d5Sgjelinek boolean_t execute = B_TRUE; 41238cd327d5Sgjelinek char *manifest_path; 4124ee519a1fSgjelinek 4125ee519a1fSgjelinek if (zonecfg_in_alt_root()) { 4126ee519a1fSgjelinek zerror(gettext("cannot attach zone in alternate root")); 4127ee519a1fSgjelinek return (Z_ERR); 4128ee519a1fSgjelinek } 4129ee519a1fSgjelinek 4130ee519a1fSgjelinek optind = 0; 41318cd327d5Sgjelinek if ((arg = getopt(argc, argv, "?Fn:")) != EOF) { 4132ee519a1fSgjelinek switch (arg) { 4133ee519a1fSgjelinek case '?': 4134ee519a1fSgjelinek sub_usage(SHELP_ATTACH, CMD_ATTACH); 4135ee519a1fSgjelinek return (optopt == '?' ? Z_OK : Z_USAGE); 4136ee519a1fSgjelinek case 'F': 4137ee519a1fSgjelinek force = B_TRUE; 4138ee519a1fSgjelinek break; 41398cd327d5Sgjelinek case 'n': 41408cd327d5Sgjelinek execute = B_FALSE; 41418cd327d5Sgjelinek manifest_path = optarg; 41428cd327d5Sgjelinek break; 4143ee519a1fSgjelinek default: 4144ee519a1fSgjelinek sub_usage(SHELP_ATTACH, CMD_ATTACH); 4145ee519a1fSgjelinek return (Z_USAGE); 4146ee519a1fSgjelinek } 4147ee519a1fSgjelinek } 41488cd327d5Sgjelinek 41498cd327d5Sgjelinek /* 41508cd327d5Sgjelinek * If the no-execute option was specified, we need to branch down 41518cd327d5Sgjelinek * a completely different path since there is no zone required to be 41528cd327d5Sgjelinek * configured for this option. 41538cd327d5Sgjelinek */ 41548cd327d5Sgjelinek if (!execute) 41558cd327d5Sgjelinek return (dryrun_attach(manifest_path)); 41568cd327d5Sgjelinek 4157ee519a1fSgjelinek if (sanity_check(target_zone, CMD_ATTACH, B_FALSE, B_TRUE) != Z_OK) 4158ee519a1fSgjelinek return (Z_ERR); 4159ee519a1fSgjelinek if (verify_details(CMD_ATTACH) != Z_OK) 4160ee519a1fSgjelinek return (Z_ERR); 4161ee519a1fSgjelinek 4162ee519a1fSgjelinek if ((err = zone_get_zonepath(target_zone, zonepath, sizeof (zonepath))) 4163ee519a1fSgjelinek != Z_OK) { 4164ee519a1fSgjelinek errno = err; 4165ee519a1fSgjelinek zperror2(target_zone, gettext("could not get zone path")); 4166ee519a1fSgjelinek return (Z_ERR); 4167ee519a1fSgjelinek } 4168ee519a1fSgjelinek 4169ee519a1fSgjelinek if ((handle = zonecfg_init_handle()) == NULL) { 4170ee519a1fSgjelinek zperror(cmd_to_str(CMD_ATTACH), B_TRUE); 4171ee519a1fSgjelinek return (Z_ERR); 4172ee519a1fSgjelinek } 4173ee519a1fSgjelinek 4174ee519a1fSgjelinek if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) { 4175ee519a1fSgjelinek errno = err; 4176ee519a1fSgjelinek zperror(cmd_to_str(CMD_ATTACH), B_TRUE); 4177ee519a1fSgjelinek zonecfg_fini_handle(handle); 4178ee519a1fSgjelinek return (Z_ERR); 4179ee519a1fSgjelinek } 4180ee519a1fSgjelinek 4181ee519a1fSgjelinek if (grab_lock_file(target_zone, &lockfd) != Z_OK) { 4182ee519a1fSgjelinek zerror(gettext("another %s may have an operation in progress."), 4183ee519a1fSgjelinek "zoneadm"); 4184ee519a1fSgjelinek zonecfg_fini_handle(handle); 4185ee519a1fSgjelinek return (Z_ERR); 4186ee519a1fSgjelinek } 4187ee519a1fSgjelinek 4188ee519a1fSgjelinek if (force) 4189ee519a1fSgjelinek goto forced; 4190ee519a1fSgjelinek 4191ee519a1fSgjelinek if ((athandle = zonecfg_init_handle()) == NULL) { 4192ee519a1fSgjelinek zperror(cmd_to_str(CMD_ATTACH), B_TRUE); 4193ee519a1fSgjelinek goto done; 4194ee519a1fSgjelinek } 4195ee519a1fSgjelinek 4196ee519a1fSgjelinek if ((err = zonecfg_get_attach_handle(zonepath, target_zone, B_TRUE, 4197ee519a1fSgjelinek athandle)) != Z_OK) { 4198ee519a1fSgjelinek if (err == Z_NO_ZONE) 4199ee519a1fSgjelinek zerror(gettext("Not a detached zone")); 4200ee519a1fSgjelinek else if (err == Z_INVALID_DOCUMENT) 4201ee519a1fSgjelinek zerror(gettext("Cannot attach to an earlier release " 4202ee519a1fSgjelinek "of the operating system")); 4203ee519a1fSgjelinek else 4204ee519a1fSgjelinek zperror(cmd_to_str(CMD_ATTACH), B_TRUE); 4205ee519a1fSgjelinek goto done; 4206ee519a1fSgjelinek } 4207ee519a1fSgjelinek 4208ee519a1fSgjelinek /* Get the detach information for the locally defined zone. */ 4209ee519a1fSgjelinek if ((err = zonecfg_get_detach_info(handle, B_FALSE)) != Z_OK) { 4210ee519a1fSgjelinek errno = err; 4211ee519a1fSgjelinek zperror(gettext("getting the attach information failed"), 4212ee519a1fSgjelinek B_TRUE); 4213ee519a1fSgjelinek goto done; 4214ee519a1fSgjelinek } 4215ee519a1fSgjelinek 4216ee519a1fSgjelinek /* sw_cmp prints error msgs as necessary */ 42170b5de56dSgjelinek if ((err = sw_cmp(handle, athandle, SW_CMP_NONE)) != Z_OK) 4218ee519a1fSgjelinek goto done; 4219ee519a1fSgjelinek 4220ee519a1fSgjelinek if ((err = dev_fix(athandle)) != Z_OK) 4221ee519a1fSgjelinek goto done; 4222ee519a1fSgjelinek 4223ee519a1fSgjelinek forced: 4224ee519a1fSgjelinek 4225ee519a1fSgjelinek zonecfg_rm_detached(handle, force); 4226ee519a1fSgjelinek 4227ee519a1fSgjelinek if ((err = zone_set_state(target_zone, ZONE_STATE_INSTALLED)) != Z_OK) { 4228ee519a1fSgjelinek errno = err; 4229ee519a1fSgjelinek zperror(gettext("could not reset state"), B_TRUE); 4230ee519a1fSgjelinek } 4231ee519a1fSgjelinek 4232ee519a1fSgjelinek done: 4233ee519a1fSgjelinek zonecfg_fini_handle(handle); 4234ee519a1fSgjelinek release_lock_file(lockfd); 4235ee519a1fSgjelinek if (athandle != NULL) 4236ee519a1fSgjelinek zonecfg_fini_handle(athandle); 4237ee519a1fSgjelinek 4238ee519a1fSgjelinek return ((err == Z_OK) ? Z_OK : Z_ERR); 4239ee519a1fSgjelinek } 4240ee519a1fSgjelinek 4241865e09a4Sgjelinek /* 42427c478bd9Sstevel@tonic-gate * On input, TRUE => yes, FALSE => no. 42437c478bd9Sstevel@tonic-gate * On return, TRUE => 1, FALSE => 0, could not ask => -1. 42447c478bd9Sstevel@tonic-gate */ 42457c478bd9Sstevel@tonic-gate 42467c478bd9Sstevel@tonic-gate static int 42477c478bd9Sstevel@tonic-gate ask_yesno(boolean_t default_answer, const char *question) 42487c478bd9Sstevel@tonic-gate { 42497c478bd9Sstevel@tonic-gate char line[64]; /* should be large enough to answer yes or no */ 42507c478bd9Sstevel@tonic-gate 42517c478bd9Sstevel@tonic-gate if (!isatty(STDIN_FILENO)) 42527c478bd9Sstevel@tonic-gate return (-1); 42537c478bd9Sstevel@tonic-gate for (;;) { 42547c478bd9Sstevel@tonic-gate (void) printf("%s (%s)? ", question, 42557c478bd9Sstevel@tonic-gate default_answer ? "[y]/n" : "y/[n]"); 42567c478bd9Sstevel@tonic-gate if (fgets(line, sizeof (line), stdin) == NULL || 42577c478bd9Sstevel@tonic-gate line[0] == '\n') 42587c478bd9Sstevel@tonic-gate return (default_answer ? 1 : 0); 42597c478bd9Sstevel@tonic-gate if (tolower(line[0]) == 'y') 42607c478bd9Sstevel@tonic-gate return (1); 42617c478bd9Sstevel@tonic-gate if (tolower(line[0]) == 'n') 42627c478bd9Sstevel@tonic-gate return (0); 42637c478bd9Sstevel@tonic-gate } 42647c478bd9Sstevel@tonic-gate } 42657c478bd9Sstevel@tonic-gate 42667c478bd9Sstevel@tonic-gate static int 42677c478bd9Sstevel@tonic-gate uninstall_func(int argc, char *argv[]) 42687c478bd9Sstevel@tonic-gate { 42697c478bd9Sstevel@tonic-gate char line[ZONENAME_MAX + 128]; /* Enough for "Are you sure ..." */ 42700b5de56dSgjelinek char rootpath[MAXPATHLEN], zonepath[MAXPATHLEN]; 42717c478bd9Sstevel@tonic-gate boolean_t force = B_FALSE; 42727c478bd9Sstevel@tonic-gate int lockfd, answer; 42737c478bd9Sstevel@tonic-gate int err, arg; 42747c478bd9Sstevel@tonic-gate 4275108322fbScarlsonj if (zonecfg_in_alt_root()) { 4276108322fbScarlsonj zerror(gettext("cannot uninstall zone in alternate root")); 4277108322fbScarlsonj return (Z_ERR); 4278108322fbScarlsonj } 4279108322fbScarlsonj 42807c478bd9Sstevel@tonic-gate optind = 0; 42817c478bd9Sstevel@tonic-gate while ((arg = getopt(argc, argv, "?F")) != EOF) { 42827c478bd9Sstevel@tonic-gate switch (arg) { 42837c478bd9Sstevel@tonic-gate case '?': 42847c478bd9Sstevel@tonic-gate sub_usage(SHELP_UNINSTALL, CMD_UNINSTALL); 42857c478bd9Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 42867c478bd9Sstevel@tonic-gate case 'F': 42877c478bd9Sstevel@tonic-gate force = B_TRUE; 42887c478bd9Sstevel@tonic-gate break; 42897c478bd9Sstevel@tonic-gate default: 42907c478bd9Sstevel@tonic-gate sub_usage(SHELP_UNINSTALL, CMD_UNINSTALL); 42917c478bd9Sstevel@tonic-gate return (Z_USAGE); 42927c478bd9Sstevel@tonic-gate } 42937c478bd9Sstevel@tonic-gate } 42947c478bd9Sstevel@tonic-gate if (argc > optind) { 42957c478bd9Sstevel@tonic-gate sub_usage(SHELP_UNINSTALL, CMD_UNINSTALL); 42967c478bd9Sstevel@tonic-gate return (Z_USAGE); 42977c478bd9Sstevel@tonic-gate } 42987c478bd9Sstevel@tonic-gate 42997c478bd9Sstevel@tonic-gate if (sanity_check(target_zone, CMD_UNINSTALL, B_FALSE, B_TRUE) != Z_OK) 43007c478bd9Sstevel@tonic-gate return (Z_ERR); 43017c478bd9Sstevel@tonic-gate 43027c478bd9Sstevel@tonic-gate if (!force) { 43037c478bd9Sstevel@tonic-gate (void) snprintf(line, sizeof (line), 43047c478bd9Sstevel@tonic-gate gettext("Are you sure you want to %s zone %s"), 43057c478bd9Sstevel@tonic-gate cmd_to_str(CMD_UNINSTALL), target_zone); 43067c478bd9Sstevel@tonic-gate if ((answer = ask_yesno(B_FALSE, line)) == 0) { 43077c478bd9Sstevel@tonic-gate return (Z_OK); 43087c478bd9Sstevel@tonic-gate } else if (answer == -1) { 43097c478bd9Sstevel@tonic-gate zerror(gettext("Input not from terminal and -F " 43107c478bd9Sstevel@tonic-gate "not specified: %s not done."), 43117c478bd9Sstevel@tonic-gate cmd_to_str(CMD_UNINSTALL)); 43127c478bd9Sstevel@tonic-gate return (Z_ERR); 43137c478bd9Sstevel@tonic-gate } 43147c478bd9Sstevel@tonic-gate } 43157c478bd9Sstevel@tonic-gate 43160b5de56dSgjelinek if ((err = zone_get_zonepath(target_zone, zonepath, 43170b5de56dSgjelinek sizeof (zonepath))) != Z_OK) { 43187c478bd9Sstevel@tonic-gate errno = err; 43197c478bd9Sstevel@tonic-gate zperror2(target_zone, gettext("could not get zone path")); 43207c478bd9Sstevel@tonic-gate return (Z_ERR); 43217c478bd9Sstevel@tonic-gate } 43227c478bd9Sstevel@tonic-gate if ((err = zone_get_rootpath(target_zone, rootpath, 43237c478bd9Sstevel@tonic-gate sizeof (rootpath))) != Z_OK) { 43247c478bd9Sstevel@tonic-gate errno = err; 43257c478bd9Sstevel@tonic-gate zperror2(target_zone, gettext("could not get root path")); 43267c478bd9Sstevel@tonic-gate return (Z_ERR); 43277c478bd9Sstevel@tonic-gate } 43287c478bd9Sstevel@tonic-gate 43297c478bd9Sstevel@tonic-gate /* 43307c478bd9Sstevel@tonic-gate * If there seems to be a zoneadmd running for this zone, call it 43317c478bd9Sstevel@tonic-gate * to tell it that an uninstall is happening; if all goes well it 43327c478bd9Sstevel@tonic-gate * will then shut itself down. 43337c478bd9Sstevel@tonic-gate */ 43347c478bd9Sstevel@tonic-gate if (ping_zoneadmd(target_zone) == Z_OK) { 43357c478bd9Sstevel@tonic-gate zone_cmd_arg_t zarg; 43367c478bd9Sstevel@tonic-gate zarg.cmd = Z_NOTE_UNINSTALLING; 43377c478bd9Sstevel@tonic-gate /* we don't care too much if this fails... just plow on */ 43387c478bd9Sstevel@tonic-gate (void) call_zoneadmd(target_zone, &zarg); 43397c478bd9Sstevel@tonic-gate } 43407c478bd9Sstevel@tonic-gate 43417c478bd9Sstevel@tonic-gate if (grab_lock_file(target_zone, &lockfd) != Z_OK) { 43427c478bd9Sstevel@tonic-gate zerror(gettext("another %s may have an operation in progress."), 4343865e09a4Sgjelinek "zoneadm"); 43447c478bd9Sstevel@tonic-gate return (Z_ERR); 43457c478bd9Sstevel@tonic-gate } 43467c478bd9Sstevel@tonic-gate 43477c478bd9Sstevel@tonic-gate /* Don't uninstall the zone if anything is mounted there */ 43487c478bd9Sstevel@tonic-gate err = zonecfg_find_mounts(rootpath, NULL, NULL); 43497c478bd9Sstevel@tonic-gate if (err) { 43500b5de56dSgjelinek zerror(gettext("These file systems are mounted on " 43517c478bd9Sstevel@tonic-gate "subdirectories of %s.\n"), rootpath); 43527c478bd9Sstevel@tonic-gate (void) zonecfg_find_mounts(rootpath, zfm_print, NULL); 43537c478bd9Sstevel@tonic-gate return (Z_ERR); 43547c478bd9Sstevel@tonic-gate } 43557c478bd9Sstevel@tonic-gate 43567c478bd9Sstevel@tonic-gate err = zone_set_state(target_zone, ZONE_STATE_INCOMPLETE); 43577c478bd9Sstevel@tonic-gate if (err != Z_OK) { 43587c478bd9Sstevel@tonic-gate errno = err; 43597c478bd9Sstevel@tonic-gate zperror2(target_zone, gettext("could not set state")); 43607c478bd9Sstevel@tonic-gate goto bad; 43617c478bd9Sstevel@tonic-gate } 43627c478bd9Sstevel@tonic-gate 43630b5de56dSgjelinek if ((err = cleanup_zonepath(zonepath, B_FALSE)) != Z_OK) { 43640b5de56dSgjelinek errno = err; 43650b5de56dSgjelinek zperror2(target_zone, gettext("cleaning up zonepath failed")); 43667c478bd9Sstevel@tonic-gate goto bad; 43670b5de56dSgjelinek } 43680b5de56dSgjelinek 43697c478bd9Sstevel@tonic-gate err = zone_set_state(target_zone, ZONE_STATE_CONFIGURED); 43707c478bd9Sstevel@tonic-gate if (err != Z_OK) { 43717c478bd9Sstevel@tonic-gate errno = err; 43727c478bd9Sstevel@tonic-gate zperror2(target_zone, gettext("could not reset state")); 43737c478bd9Sstevel@tonic-gate } 43747c478bd9Sstevel@tonic-gate bad: 43757c478bd9Sstevel@tonic-gate release_lock_file(lockfd); 43767c478bd9Sstevel@tonic-gate return (err); 43777c478bd9Sstevel@tonic-gate } 43787c478bd9Sstevel@tonic-gate 4379108322fbScarlsonj /* ARGSUSED */ 4380108322fbScarlsonj static int 4381108322fbScarlsonj mount_func(int argc, char *argv[]) 4382108322fbScarlsonj { 4383108322fbScarlsonj zone_cmd_arg_t zarg; 4384108322fbScarlsonj 4385108322fbScarlsonj if (argc > 0) 4386108322fbScarlsonj return (Z_USAGE); 4387108322fbScarlsonj if (sanity_check(target_zone, CMD_MOUNT, B_FALSE, B_FALSE) != Z_OK) 4388108322fbScarlsonj return (Z_ERR); 4389108322fbScarlsonj if (verify_details(CMD_MOUNT) != Z_OK) 4390108322fbScarlsonj return (Z_ERR); 4391108322fbScarlsonj 4392108322fbScarlsonj zarg.cmd = Z_MOUNT; 4393108322fbScarlsonj if (call_zoneadmd(target_zone, &zarg) != 0) { 4394108322fbScarlsonj zerror(gettext("call to %s failed"), "zoneadmd"); 4395108322fbScarlsonj return (Z_ERR); 4396108322fbScarlsonj } 4397108322fbScarlsonj return (Z_OK); 4398108322fbScarlsonj } 4399108322fbScarlsonj 4400108322fbScarlsonj /* ARGSUSED */ 4401108322fbScarlsonj static int 4402108322fbScarlsonj unmount_func(int argc, char *argv[]) 4403108322fbScarlsonj { 4404108322fbScarlsonj zone_cmd_arg_t zarg; 4405108322fbScarlsonj 4406108322fbScarlsonj if (argc > 0) 4407108322fbScarlsonj return (Z_USAGE); 4408108322fbScarlsonj if (sanity_check(target_zone, CMD_UNMOUNT, B_FALSE, B_FALSE) != Z_OK) 4409108322fbScarlsonj return (Z_ERR); 4410108322fbScarlsonj 4411108322fbScarlsonj zarg.cmd = Z_UNMOUNT; 4412108322fbScarlsonj if (call_zoneadmd(target_zone, &zarg) != 0) { 4413108322fbScarlsonj zerror(gettext("call to %s failed"), "zoneadmd"); 4414108322fbScarlsonj return (Z_ERR); 4415108322fbScarlsonj } 4416108322fbScarlsonj return (Z_OK); 4417108322fbScarlsonj } 4418108322fbScarlsonj 44197c478bd9Sstevel@tonic-gate static int 4420555afedfScarlsonj mark_func(int argc, char *argv[]) 4421555afedfScarlsonj { 4422555afedfScarlsonj int err, lockfd; 4423555afedfScarlsonj 4424555afedfScarlsonj if (argc != 1 || strcmp(argv[0], "incomplete") != 0) 4425555afedfScarlsonj return (Z_USAGE); 4426555afedfScarlsonj if (sanity_check(target_zone, CMD_MARK, B_FALSE, B_FALSE) != Z_OK) 4427555afedfScarlsonj return (Z_ERR); 4428555afedfScarlsonj 4429555afedfScarlsonj if (grab_lock_file(target_zone, &lockfd) != Z_OK) { 4430555afedfScarlsonj zerror(gettext("another %s may have an operation in progress."), 4431555afedfScarlsonj "zoneadm"); 4432555afedfScarlsonj return (Z_ERR); 4433555afedfScarlsonj } 4434555afedfScarlsonj 4435555afedfScarlsonj err = zone_set_state(target_zone, ZONE_STATE_INCOMPLETE); 4436555afedfScarlsonj if (err != Z_OK) { 4437555afedfScarlsonj errno = err; 4438555afedfScarlsonj zperror2(target_zone, gettext("could not set state")); 4439555afedfScarlsonj } 4440555afedfScarlsonj release_lock_file(lockfd); 4441555afedfScarlsonj 4442555afedfScarlsonj return (err); 4443555afedfScarlsonj } 4444555afedfScarlsonj 4445555afedfScarlsonj static int 44467c478bd9Sstevel@tonic-gate help_func(int argc, char *argv[]) 44477c478bd9Sstevel@tonic-gate { 44487c478bd9Sstevel@tonic-gate int arg, cmd_num; 44497c478bd9Sstevel@tonic-gate 44507c478bd9Sstevel@tonic-gate if (argc == 0) { 44517c478bd9Sstevel@tonic-gate (void) usage(B_TRUE); 44527c478bd9Sstevel@tonic-gate return (Z_OK); 44537c478bd9Sstevel@tonic-gate } 44547c478bd9Sstevel@tonic-gate optind = 0; 44557c478bd9Sstevel@tonic-gate if ((arg = getopt(argc, argv, "?")) != EOF) { 44567c478bd9Sstevel@tonic-gate switch (arg) { 44577c478bd9Sstevel@tonic-gate case '?': 44587c478bd9Sstevel@tonic-gate sub_usage(SHELP_HELP, CMD_HELP); 44597c478bd9Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 44607c478bd9Sstevel@tonic-gate default: 44617c478bd9Sstevel@tonic-gate sub_usage(SHELP_HELP, CMD_HELP); 44627c478bd9Sstevel@tonic-gate return (Z_USAGE); 44637c478bd9Sstevel@tonic-gate } 44647c478bd9Sstevel@tonic-gate } 44657c478bd9Sstevel@tonic-gate while (optind < argc) { 4466394a25e2Scarlsonj /* Private commands have NULL short_usage; omit them */ 4467394a25e2Scarlsonj if ((cmd_num = cmd_match(argv[optind])) < 0 || 4468394a25e2Scarlsonj cmdtab[cmd_num].short_usage == NULL) { 44697c478bd9Sstevel@tonic-gate sub_usage(SHELP_HELP, CMD_HELP); 44707c478bd9Sstevel@tonic-gate return (Z_USAGE); 44717c478bd9Sstevel@tonic-gate } 44727c478bd9Sstevel@tonic-gate sub_usage(cmdtab[cmd_num].short_usage, cmd_num); 44737c478bd9Sstevel@tonic-gate optind++; 44747c478bd9Sstevel@tonic-gate } 44757c478bd9Sstevel@tonic-gate return (Z_OK); 44767c478bd9Sstevel@tonic-gate } 44777c478bd9Sstevel@tonic-gate 44787c478bd9Sstevel@tonic-gate /* 44797c478bd9Sstevel@tonic-gate * Returns: CMD_MIN thru CMD_MAX on success, -1 on error 44807c478bd9Sstevel@tonic-gate */ 44817c478bd9Sstevel@tonic-gate 44827c478bd9Sstevel@tonic-gate static int 44837c478bd9Sstevel@tonic-gate cmd_match(char *cmd) 44847c478bd9Sstevel@tonic-gate { 44857c478bd9Sstevel@tonic-gate int i; 44867c478bd9Sstevel@tonic-gate 44877c478bd9Sstevel@tonic-gate for (i = CMD_MIN; i <= CMD_MAX; i++) { 44887c478bd9Sstevel@tonic-gate /* return only if there is an exact match */ 44897c478bd9Sstevel@tonic-gate if (strcmp(cmd, cmdtab[i].cmd_name) == 0) 44907c478bd9Sstevel@tonic-gate return (cmdtab[i].cmd_num); 44917c478bd9Sstevel@tonic-gate } 44927c478bd9Sstevel@tonic-gate return (-1); 44937c478bd9Sstevel@tonic-gate } 44947c478bd9Sstevel@tonic-gate 44957c478bd9Sstevel@tonic-gate static int 44967c478bd9Sstevel@tonic-gate parse_and_run(int argc, char *argv[]) 44977c478bd9Sstevel@tonic-gate { 44987c478bd9Sstevel@tonic-gate int i = cmd_match(argv[0]); 44997c478bd9Sstevel@tonic-gate 45007c478bd9Sstevel@tonic-gate if (i < 0) 45017c478bd9Sstevel@tonic-gate return (usage(B_FALSE)); 45027c478bd9Sstevel@tonic-gate return (cmdtab[i].handler(argc - 1, &(argv[1]))); 45037c478bd9Sstevel@tonic-gate } 45047c478bd9Sstevel@tonic-gate 45057c478bd9Sstevel@tonic-gate static char * 45067c478bd9Sstevel@tonic-gate get_execbasename(char *execfullname) 45077c478bd9Sstevel@tonic-gate { 45087c478bd9Sstevel@tonic-gate char *last_slash, *execbasename; 45097c478bd9Sstevel@tonic-gate 45107c478bd9Sstevel@tonic-gate /* guard against '/' at end of command invocation */ 45117c478bd9Sstevel@tonic-gate for (;;) { 45127c478bd9Sstevel@tonic-gate last_slash = strrchr(execfullname, '/'); 45137c478bd9Sstevel@tonic-gate if (last_slash == NULL) { 45147c478bd9Sstevel@tonic-gate execbasename = execfullname; 45157c478bd9Sstevel@tonic-gate break; 45167c478bd9Sstevel@tonic-gate } else { 45177c478bd9Sstevel@tonic-gate execbasename = last_slash + 1; 45187c478bd9Sstevel@tonic-gate if (*execbasename == '\0') { 45197c478bd9Sstevel@tonic-gate *last_slash = '\0'; 45207c478bd9Sstevel@tonic-gate continue; 45217c478bd9Sstevel@tonic-gate } 45227c478bd9Sstevel@tonic-gate break; 45237c478bd9Sstevel@tonic-gate } 45247c478bd9Sstevel@tonic-gate } 45257c478bd9Sstevel@tonic-gate return (execbasename); 45267c478bd9Sstevel@tonic-gate } 45277c478bd9Sstevel@tonic-gate 45287c478bd9Sstevel@tonic-gate int 45297c478bd9Sstevel@tonic-gate main(int argc, char **argv) 45307c478bd9Sstevel@tonic-gate { 45317c478bd9Sstevel@tonic-gate int arg; 45327c478bd9Sstevel@tonic-gate zoneid_t zid; 4533108322fbScarlsonj struct stat st; 45347c478bd9Sstevel@tonic-gate 45357c478bd9Sstevel@tonic-gate if ((locale = setlocale(LC_ALL, "")) == NULL) 45367c478bd9Sstevel@tonic-gate locale = "C"; 45377c478bd9Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN); 45387c478bd9Sstevel@tonic-gate setbuf(stdout, NULL); 45397c478bd9Sstevel@tonic-gate (void) sigset(SIGHUP, SIG_IGN); 45407c478bd9Sstevel@tonic-gate execname = get_execbasename(argv[0]); 45417c478bd9Sstevel@tonic-gate target_zone = NULL; 45427c478bd9Sstevel@tonic-gate if (chdir("/") != 0) { 45437c478bd9Sstevel@tonic-gate zerror(gettext("could not change directory to /.")); 45447c478bd9Sstevel@tonic-gate exit(Z_ERR); 45457c478bd9Sstevel@tonic-gate } 45467c478bd9Sstevel@tonic-gate 454799653d4eSeschrock if (init_zfs() != Z_OK) 454899653d4eSeschrock exit(Z_ERR); 454999653d4eSeschrock 4550555afedfScarlsonj while ((arg = getopt(argc, argv, "?u:z:R:")) != EOF) { 45517c478bd9Sstevel@tonic-gate switch (arg) { 45527c478bd9Sstevel@tonic-gate case '?': 45537c478bd9Sstevel@tonic-gate return (usage(B_TRUE)); 4554555afedfScarlsonj case 'u': 4555555afedfScarlsonj target_uuid = optarg; 4556555afedfScarlsonj break; 45577c478bd9Sstevel@tonic-gate case 'z': 45587c478bd9Sstevel@tonic-gate target_zone = optarg; 45597c478bd9Sstevel@tonic-gate break; 4560108322fbScarlsonj case 'R': /* private option for admin/install use */ 4561108322fbScarlsonj if (*optarg != '/') { 4562108322fbScarlsonj zerror(gettext("root path must be absolute.")); 4563108322fbScarlsonj exit(Z_ERR); 4564108322fbScarlsonj } 4565108322fbScarlsonj if (stat(optarg, &st) == -1 || !S_ISDIR(st.st_mode)) { 4566108322fbScarlsonj zerror( 4567108322fbScarlsonj gettext("root path must be a directory.")); 4568108322fbScarlsonj exit(Z_ERR); 4569108322fbScarlsonj } 4570108322fbScarlsonj zonecfg_set_root(optarg); 4571108322fbScarlsonj break; 45727c478bd9Sstevel@tonic-gate default: 45737c478bd9Sstevel@tonic-gate return (usage(B_FALSE)); 45747c478bd9Sstevel@tonic-gate } 45757c478bd9Sstevel@tonic-gate } 45767c478bd9Sstevel@tonic-gate 45777c478bd9Sstevel@tonic-gate if (optind >= argc) 45787c478bd9Sstevel@tonic-gate return (usage(B_FALSE)); 4579555afedfScarlsonj 4580555afedfScarlsonj if (target_uuid != NULL && *target_uuid != '\0') { 4581555afedfScarlsonj uuid_t uuid; 4582555afedfScarlsonj static char newtarget[ZONENAME_MAX]; 4583555afedfScarlsonj 4584555afedfScarlsonj if (uuid_parse(target_uuid, uuid) == -1) { 4585555afedfScarlsonj zerror(gettext("illegal UUID value specified")); 4586555afedfScarlsonj exit(Z_ERR); 4587555afedfScarlsonj } 4588555afedfScarlsonj if (zonecfg_get_name_by_uuid(uuid, newtarget, 4589555afedfScarlsonj sizeof (newtarget)) == Z_OK) 4590555afedfScarlsonj target_zone = newtarget; 4591555afedfScarlsonj } 4592555afedfScarlsonj 45937c478bd9Sstevel@tonic-gate if (target_zone != NULL && zone_get_id(target_zone, &zid) != 0) { 45947c478bd9Sstevel@tonic-gate errno = Z_NO_ZONE; 45957c478bd9Sstevel@tonic-gate zperror(target_zone, B_TRUE); 45967c478bd9Sstevel@tonic-gate exit(Z_ERR); 45977c478bd9Sstevel@tonic-gate } 45987c478bd9Sstevel@tonic-gate return (parse_and_run(argc - optind, &argv[optind])); 45997c478bd9Sstevel@tonic-gate } 4600