xref: /onnv-gate/usr/src/cmd/zoneadm/zoneadm.c (revision 766:c521de78a32f)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
22222Scomay 
230Sstevel@tonic-gate /*
240Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
250Sstevel@tonic-gate  * Use is subject to license terms.
260Sstevel@tonic-gate  */
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
290Sstevel@tonic-gate 
300Sstevel@tonic-gate /*
310Sstevel@tonic-gate  * zoneadm is a command interpreter for zone administration.  It is all in
320Sstevel@tonic-gate  * C (i.e., no lex/yacc), and all the argument passing is argc/argv based.
330Sstevel@tonic-gate  * main() calls parse_and_run() which calls cmd_match(), then invokes the
340Sstevel@tonic-gate  * appropriate command's handler function.  The rest of the program is the
350Sstevel@tonic-gate  * handler functions and their helper functions.
360Sstevel@tonic-gate  *
370Sstevel@tonic-gate  * Some of the helper functions are used largely to simplify I18N: reducing
380Sstevel@tonic-gate  * the need for translation notes.  This is particularly true of many of
390Sstevel@tonic-gate  * the zerror() calls: doing e.g. zerror(gettext("%s failed"), "foo") rather
400Sstevel@tonic-gate  * than zerror(gettext("foo failed")) with a translation note indicating
410Sstevel@tonic-gate  * that "foo" need not be translated.
420Sstevel@tonic-gate  */
430Sstevel@tonic-gate 
440Sstevel@tonic-gate #include <stdio.h>
450Sstevel@tonic-gate #include <errno.h>
460Sstevel@tonic-gate #include <unistd.h>
470Sstevel@tonic-gate #include <signal.h>
480Sstevel@tonic-gate #include <stdarg.h>
490Sstevel@tonic-gate #include <ctype.h>
500Sstevel@tonic-gate #include <stdlib.h>
510Sstevel@tonic-gate #include <string.h>
520Sstevel@tonic-gate #include <wait.h>
530Sstevel@tonic-gate #include <zone.h>
540Sstevel@tonic-gate #include <priv.h>
550Sstevel@tonic-gate #include <locale.h>
560Sstevel@tonic-gate #include <libintl.h>
570Sstevel@tonic-gate #include <libzonecfg.h>
580Sstevel@tonic-gate #include <bsm/adt.h>
590Sstevel@tonic-gate #include <sys/utsname.h>
600Sstevel@tonic-gate #include <sys/param.h>
610Sstevel@tonic-gate #include <sys/types.h>
620Sstevel@tonic-gate #include <sys/stat.h>
630Sstevel@tonic-gate #include <sys/statvfs.h>
640Sstevel@tonic-gate #include <assert.h>
650Sstevel@tonic-gate #include <sys/sockio.h>
660Sstevel@tonic-gate #include <sys/mntent.h>
670Sstevel@tonic-gate #include <limits.h>
680Sstevel@tonic-gate 
690Sstevel@tonic-gate #include <fcntl.h>
700Sstevel@tonic-gate #include <door.h>
710Sstevel@tonic-gate #include <macros.h>
720Sstevel@tonic-gate #include <libgen.h>
730Sstevel@tonic-gate 
740Sstevel@tonic-gate #include <pool.h>
750Sstevel@tonic-gate #include <sys/pool.h>
760Sstevel@tonic-gate 
770Sstevel@tonic-gate #define	MAXARGS	8
780Sstevel@tonic-gate 
790Sstevel@tonic-gate /* Reflects kernel zone entries */
800Sstevel@tonic-gate typedef struct zone_entry {
810Sstevel@tonic-gate 	zoneid_t	zid;
820Sstevel@tonic-gate 	char		zname[ZONENAME_MAX];
830Sstevel@tonic-gate 	char		*zstate_str;
840Sstevel@tonic-gate 	zone_state_t	zstate_num;
850Sstevel@tonic-gate 	char		zroot[MAXPATHLEN];
860Sstevel@tonic-gate } zone_entry_t;
870Sstevel@tonic-gate 
880Sstevel@tonic-gate static zone_entry_t *zents;
890Sstevel@tonic-gate static size_t nzents;
900Sstevel@tonic-gate 
910Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)		/* should be defined by cc -D */
920Sstevel@tonic-gate #define	TEXT_DOMAIN	"SYS_TEST"	/* Use this only if it wasn't */
930Sstevel@tonic-gate #endif
940Sstevel@tonic-gate 
950Sstevel@tonic-gate #define	Z_ERR	1
960Sstevel@tonic-gate #define	Z_USAGE	2
970Sstevel@tonic-gate 
980Sstevel@tonic-gate /* 0755 is the default directory mode. */
990Sstevel@tonic-gate #define	DEFAULT_DIR_MODE \
1000Sstevel@tonic-gate 	(S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate #define	CMD_HELP	0
1030Sstevel@tonic-gate #define	CMD_BOOT	1
1040Sstevel@tonic-gate #define	CMD_HALT	2
1050Sstevel@tonic-gate #define	CMD_READY	3
1060Sstevel@tonic-gate #define	CMD_REBOOT	4
1070Sstevel@tonic-gate #define	CMD_LIST	5
1080Sstevel@tonic-gate #define	CMD_VERIFY	6
1090Sstevel@tonic-gate #define	CMD_INSTALL	7
1100Sstevel@tonic-gate #define	CMD_UNINSTALL	8
111*766Scarlsonj #define	CMD_MOUNT	9
112*766Scarlsonj #define	CMD_UNMOUNT	10
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate #define	CMD_MIN		CMD_HELP
115*766Scarlsonj #define	CMD_MAX		CMD_UNMOUNT
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate struct cmd {
1180Sstevel@tonic-gate 	uint_t	cmd_num;				/* command number */
1190Sstevel@tonic-gate 	char	*cmd_name;				/* command name */
1200Sstevel@tonic-gate 	char	*short_usage;				/* short form help */
1210Sstevel@tonic-gate 	int	(*handler)(int argc, char *argv[]);	/* function to call */
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate };
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate #define	SHELP_HELP	"help"
1260Sstevel@tonic-gate #define	SHELP_BOOT	"boot [-s]"
1270Sstevel@tonic-gate #define	SHELP_HALT	"halt"
1280Sstevel@tonic-gate #define	SHELP_READY	"ready"
1290Sstevel@tonic-gate #define	SHELP_REBOOT	"reboot"
1300Sstevel@tonic-gate #define	SHELP_LIST	"list [-cipv]"
1310Sstevel@tonic-gate #define	SHELP_VERIFY	"verify"
1320Sstevel@tonic-gate #define	SHELP_INSTALL	"install"
1330Sstevel@tonic-gate #define	SHELP_UNINSTALL	"uninstall [-F]"
1340Sstevel@tonic-gate 
1350Sstevel@tonic-gate static int help_func(int argc, char *argv[]);
1360Sstevel@tonic-gate static int ready_func(int argc, char *argv[]);
1370Sstevel@tonic-gate static int boot_func(int argc, char *argv[]);
1380Sstevel@tonic-gate static int halt_func(int argc, char *argv[]);
1390Sstevel@tonic-gate static int reboot_func(int argc, char *argv[]);
1400Sstevel@tonic-gate static int list_func(int argc, char *argv[]);
1410Sstevel@tonic-gate static int verify_func(int argc, char *argv[]);
1420Sstevel@tonic-gate static int install_func(int argc, char *argv[]);
1430Sstevel@tonic-gate static int uninstall_func(int argc, char *argv[]);
144*766Scarlsonj static int mount_func(int argc, char *argv[]);
145*766Scarlsonj static int unmount_func(int argc, char *argv[]);
1460Sstevel@tonic-gate static int sanity_check(char *zone, int cmd_num, boolean_t running,
1470Sstevel@tonic-gate     boolean_t unsafe_when_running);
1480Sstevel@tonic-gate static int cmd_match(char *cmd);
1490Sstevel@tonic-gate static int verify_details(int);
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate static struct cmd cmdtab[] = {
1520Sstevel@tonic-gate 	{ CMD_HELP,		"help",		SHELP_HELP,	help_func },
1530Sstevel@tonic-gate 	{ CMD_BOOT,		"boot",		SHELP_BOOT,	boot_func },
1540Sstevel@tonic-gate 	{ CMD_HALT,		"halt",		SHELP_HALT,	halt_func },
1550Sstevel@tonic-gate 	{ CMD_READY,		"ready",	SHELP_READY,	ready_func },
1560Sstevel@tonic-gate 	{ CMD_REBOOT,		"reboot",	SHELP_REBOOT,	reboot_func },
1570Sstevel@tonic-gate 	{ CMD_LIST,		"list",		SHELP_LIST,	list_func },
1580Sstevel@tonic-gate 	{ CMD_VERIFY,		"verify",	SHELP_VERIFY,	verify_func },
1590Sstevel@tonic-gate 	{ CMD_INSTALL,		"install",	SHELP_INSTALL,	install_func },
1600Sstevel@tonic-gate 	{ CMD_UNINSTALL,	"uninstall",	SHELP_UNINSTALL,
161*766Scarlsonj 	    uninstall_func },
162*766Scarlsonj 	/* Private commands for admin/install */
163*766Scarlsonj 	{ CMD_MOUNT,		"mount",	NULL,		mount_func },
164*766Scarlsonj 	{ CMD_UNMOUNT,		"unmount",	NULL,		unmount_func }
1650Sstevel@tonic-gate };
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate /* global variables */
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate /* set early in main(), never modified thereafter, used all over the place */
1700Sstevel@tonic-gate static char *execname;
1710Sstevel@tonic-gate static char *target_zone;
1720Sstevel@tonic-gate static char *locale;
1730Sstevel@tonic-gate 
1740Sstevel@tonic-gate /* used in do_subproc() and signal handler */
1750Sstevel@tonic-gate static volatile boolean_t child_killed;
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate static char *
1780Sstevel@tonic-gate cmd_to_str(int cmd_num)
1790Sstevel@tonic-gate {
1800Sstevel@tonic-gate 	assert(cmd_num >= CMD_MIN && cmd_num <= CMD_MAX);
1810Sstevel@tonic-gate 	return (cmdtab[cmd_num].cmd_name);
1820Sstevel@tonic-gate }
1830Sstevel@tonic-gate 
1840Sstevel@tonic-gate /* This is a separate function because of gettext() wrapping. */
1850Sstevel@tonic-gate static char *
1860Sstevel@tonic-gate long_help(int cmd_num)
1870Sstevel@tonic-gate {
188222Scomay 	assert(cmd_num >= CMD_MIN && cmd_num <= CMD_MAX);
1890Sstevel@tonic-gate 	switch (cmd_num) {
1900Sstevel@tonic-gate 		case CMD_HELP:
1910Sstevel@tonic-gate 			return (gettext("Print usage message."));
1920Sstevel@tonic-gate 		case CMD_BOOT:
1930Sstevel@tonic-gate 			return (gettext("Activates (boots) specified zone.  "
1940Sstevel@tonic-gate 			    "The -s flag can be used\n\tto boot the zone in "
1950Sstevel@tonic-gate 			    "the single-user state."));
1960Sstevel@tonic-gate 		case CMD_HALT:
1970Sstevel@tonic-gate 			return (gettext("Halts specified zone, bypassing "
1980Sstevel@tonic-gate 			    "shutdown scripts and removing runtime\n\t"
1990Sstevel@tonic-gate 			    "resources of the zone."));
2000Sstevel@tonic-gate 		case CMD_READY:
2010Sstevel@tonic-gate 			return (gettext("Prepares a zone for running "
2020Sstevel@tonic-gate 			    "applications but does not start any user\n\t"
2030Sstevel@tonic-gate 			    "processes in the zone."));
2040Sstevel@tonic-gate 		case CMD_REBOOT:
2050Sstevel@tonic-gate 			return (gettext("Restarts the zone (equivalent to a "
2060Sstevel@tonic-gate 			    "halt / boot sequence).\n\tFails if the zone is "
2070Sstevel@tonic-gate 			    "not active."));
2080Sstevel@tonic-gate 		case CMD_LIST:
2090Sstevel@tonic-gate 			return (gettext("Lists the current zones, or a "
2100Sstevel@tonic-gate 			    "specific zone if indicated.  By default,\n\tall "
2110Sstevel@tonic-gate 			    "running zones are listed, though this can be "
2120Sstevel@tonic-gate 			    "expanded to all\n\tinstalled zones with the -i "
2130Sstevel@tonic-gate 			    "option or all configured zones with the\n\t-c "
2140Sstevel@tonic-gate 			    "option.  When used with the general -z <zone> "
2150Sstevel@tonic-gate 			    "option, lists only the\n\tspecified zone, but "
2160Sstevel@tonic-gate 			    "lists it regardless of its state, and the -i "
2170Sstevel@tonic-gate 			    "and -c\n\toptions are disallowed.  The -v option "
2180Sstevel@tonic-gate 			    "can be used to display verbose\n\tinformation: "
2190Sstevel@tonic-gate 			    "zone name, id, current state, root directory and "
2200Sstevel@tonic-gate 			    "options.\n\tThe -p option can be used to request "
2210Sstevel@tonic-gate 			    "machine-parsable output.  The -v\n\tand -p "
2220Sstevel@tonic-gate 			    "options are mutually exclusive.  If neither -v "
2230Sstevel@tonic-gate 			    "nor -p is used,\n\tjust the zone name is "
2240Sstevel@tonic-gate 			    "listed."));
2250Sstevel@tonic-gate 		case CMD_VERIFY:
2260Sstevel@tonic-gate 			return (gettext("Check to make sure the configuration "
2270Sstevel@tonic-gate 			    "can safely be instantiated\n\ton the machine: "
2280Sstevel@tonic-gate 			    "physical network interfaces exist, etc."));
2290Sstevel@tonic-gate 		case CMD_INSTALL:
2300Sstevel@tonic-gate 			return (gettext("Install the configuration on to the "
2310Sstevel@tonic-gate 			    "system."));
2320Sstevel@tonic-gate 		case CMD_UNINSTALL:
2330Sstevel@tonic-gate 			return (gettext("Uninstall the configuration from the "
2340Sstevel@tonic-gate 			    "system.  The -F flag can be used\n\tto force the "
2350Sstevel@tonic-gate 			    "action."));
236*766Scarlsonj 		default:
237*766Scarlsonj 			return ("");
2380Sstevel@tonic-gate 	}
2390Sstevel@tonic-gate 	/* NOTREACHED */
240222Scomay 	return (NULL);
2410Sstevel@tonic-gate }
2420Sstevel@tonic-gate 
2430Sstevel@tonic-gate /*
2440Sstevel@tonic-gate  * Called with explicit B_TRUE when help is explicitly requested, B_FALSE for
2450Sstevel@tonic-gate  * unexpected errors.
2460Sstevel@tonic-gate  */
2470Sstevel@tonic-gate 
2480Sstevel@tonic-gate static int
2490Sstevel@tonic-gate usage(boolean_t explicit)
2500Sstevel@tonic-gate {
2510Sstevel@tonic-gate 	int i;
2520Sstevel@tonic-gate 	FILE *fd = explicit ? stdout : stderr;
2530Sstevel@tonic-gate 
2540Sstevel@tonic-gate 	(void) fprintf(fd, "%s:\t%s help\n", gettext("usage"), execname);
2550Sstevel@tonic-gate 	(void) fprintf(fd, "\t%s [-z <zone>] list\n", execname);
2560Sstevel@tonic-gate 	(void) fprintf(fd, "\t%s -z <zone> <%s>\n", execname,
2570Sstevel@tonic-gate 	    gettext("subcommand"));
2580Sstevel@tonic-gate 	(void) fprintf(fd, "\n%s:\n\n", gettext("Subcommands"));
2590Sstevel@tonic-gate 	for (i = CMD_MIN; i <= CMD_MAX; i++) {
260*766Scarlsonj 		if (cmdtab[i].short_usage == NULL)
261*766Scarlsonj 			continue;
2620Sstevel@tonic-gate 		(void) fprintf(fd, "%s\n", cmdtab[i].short_usage);
2630Sstevel@tonic-gate 		if (explicit)
2640Sstevel@tonic-gate 			(void) fprintf(fd, "\t%s\n\n", long_help(i));
2650Sstevel@tonic-gate 	}
2660Sstevel@tonic-gate 	if (!explicit)
2670Sstevel@tonic-gate 		(void) fputs("\n", fd);
2680Sstevel@tonic-gate 	return (Z_USAGE);
2690Sstevel@tonic-gate }
2700Sstevel@tonic-gate 
2710Sstevel@tonic-gate static void
2720Sstevel@tonic-gate sub_usage(char *short_usage, int cmd_num)
2730Sstevel@tonic-gate {
2740Sstevel@tonic-gate 	(void) fprintf(stderr, "%s:\t%s\n", gettext("usage"), short_usage);
2750Sstevel@tonic-gate 	(void) fprintf(stderr, "\t%s\n", long_help(cmd_num));
2760Sstevel@tonic-gate }
2770Sstevel@tonic-gate 
2780Sstevel@tonic-gate /*
2790Sstevel@tonic-gate  * zperror() is like perror(3c) except that this also prints the executable
2800Sstevel@tonic-gate  * name at the start of the message, and takes a boolean indicating whether
2810Sstevel@tonic-gate  * to call libc'c strerror() or that from libzonecfg.
2820Sstevel@tonic-gate  */
2830Sstevel@tonic-gate 
2840Sstevel@tonic-gate static void
2850Sstevel@tonic-gate zperror(const char *str, boolean_t zonecfg_error)
2860Sstevel@tonic-gate {
2870Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: %s: %s\n", execname, str,
2880Sstevel@tonic-gate 	    zonecfg_error ? zonecfg_strerror(errno) : strerror(errno));
2890Sstevel@tonic-gate }
2900Sstevel@tonic-gate 
2910Sstevel@tonic-gate /*
2920Sstevel@tonic-gate  * zperror2() is very similar to zperror() above, except it also prints a
2930Sstevel@tonic-gate  * supplied zone name after the executable.
2940Sstevel@tonic-gate  *
2950Sstevel@tonic-gate  * All current consumers of this function want libzonecfg's strerror() rather
2960Sstevel@tonic-gate  * than libc's; if this ever changes, this function can be made more generic
2970Sstevel@tonic-gate  * like zperror() above.
2980Sstevel@tonic-gate  */
2990Sstevel@tonic-gate 
3000Sstevel@tonic-gate static void
3010Sstevel@tonic-gate zperror2(const char *zone, const char *str)
3020Sstevel@tonic-gate {
3030Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: %s: %s: %s\n", execname, zone, str,
3040Sstevel@tonic-gate 	    zonecfg_strerror(errno));
3050Sstevel@tonic-gate }
3060Sstevel@tonic-gate 
3070Sstevel@tonic-gate /* PRINTFLIKE1 */
3080Sstevel@tonic-gate static void
3090Sstevel@tonic-gate zerror(const char *fmt, ...)
3100Sstevel@tonic-gate {
3110Sstevel@tonic-gate 	va_list alist;
3120Sstevel@tonic-gate 
3130Sstevel@tonic-gate 	va_start(alist, fmt);
3140Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: ", execname);
3150Sstevel@tonic-gate 	if (target_zone != NULL)
3160Sstevel@tonic-gate 		(void) fprintf(stderr, "zone '%s': ", target_zone);
3170Sstevel@tonic-gate 	(void) vfprintf(stderr, fmt, alist);
3180Sstevel@tonic-gate 	(void) fprintf(stderr, "\n");
3190Sstevel@tonic-gate 	va_end(alist);
3200Sstevel@tonic-gate }
3210Sstevel@tonic-gate 
3220Sstevel@tonic-gate static void *
3230Sstevel@tonic-gate safe_calloc(size_t nelem, size_t elsize)
3240Sstevel@tonic-gate {
3250Sstevel@tonic-gate 	void *r = calloc(nelem, elsize);
3260Sstevel@tonic-gate 
3270Sstevel@tonic-gate 	if (r == NULL) {
3280Sstevel@tonic-gate 		zerror(gettext("failed to allocate %lu bytes: %s"),
3290Sstevel@tonic-gate 		    (ulong_t)nelem * elsize, strerror(errno));
3300Sstevel@tonic-gate 		exit(Z_ERR);
3310Sstevel@tonic-gate 	}
3320Sstevel@tonic-gate 	return (r);
3330Sstevel@tonic-gate }
3340Sstevel@tonic-gate 
3350Sstevel@tonic-gate static void
3360Sstevel@tonic-gate zone_print(zone_entry_t *zent, boolean_t verbose, boolean_t parsable)
3370Sstevel@tonic-gate {
3380Sstevel@tonic-gate 	static boolean_t firsttime = B_TRUE;
3390Sstevel@tonic-gate 
3400Sstevel@tonic-gate 	assert(!(verbose && parsable));
3410Sstevel@tonic-gate 	if (firsttime && verbose) {
3420Sstevel@tonic-gate 		firsttime = B_FALSE;
3430Sstevel@tonic-gate 		(void) printf("%*s %-16s %-14s %-30s\n", ZONEID_WIDTH, "ID",
3440Sstevel@tonic-gate 		    "NAME", "STATUS", "PATH");
3450Sstevel@tonic-gate 	}
3460Sstevel@tonic-gate 	if (!verbose) {
3470Sstevel@tonic-gate 		if (!parsable) {
3480Sstevel@tonic-gate 			(void) printf("%s\n", zent->zname);
3490Sstevel@tonic-gate 			return;
3500Sstevel@tonic-gate 		}
3510Sstevel@tonic-gate 		if (zent->zid == ZONE_ID_UNDEFINED)
3520Sstevel@tonic-gate 			(void) printf("-");
3530Sstevel@tonic-gate 		else
3540Sstevel@tonic-gate 			(void) printf("%lu", zent->zid);
3550Sstevel@tonic-gate 		(void) printf(":%s:%s:%s\n", zent->zname, zent->zstate_str,
3560Sstevel@tonic-gate 		    zent->zroot);
3570Sstevel@tonic-gate 		return;
3580Sstevel@tonic-gate 	}
3590Sstevel@tonic-gate 	if (zent->zstate_str != NULL) {
3600Sstevel@tonic-gate 		if (zent->zid == ZONE_ID_UNDEFINED)
3610Sstevel@tonic-gate 			(void) printf("%*s", ZONEID_WIDTH, "-");
3620Sstevel@tonic-gate 		else
3630Sstevel@tonic-gate 			(void) printf("%*lu", ZONEID_WIDTH, zent->zid);
3640Sstevel@tonic-gate 		(void) printf(" %-16s %-14s %-30s\n", zent->zname,
3650Sstevel@tonic-gate 		    zent->zstate_str, zent->zroot);
3660Sstevel@tonic-gate 	}
3670Sstevel@tonic-gate }
3680Sstevel@tonic-gate 
3690Sstevel@tonic-gate static int
370*766Scarlsonj lookup_zone_info(const char *zone_name, zoneid_t zid, zone_entry_t *zent)
3710Sstevel@tonic-gate {
3720Sstevel@tonic-gate 	char root[MAXPATHLEN];
3730Sstevel@tonic-gate 	int err;
3740Sstevel@tonic-gate 
3750Sstevel@tonic-gate 	(void) strlcpy(zent->zname, zone_name, sizeof (zent->zname));
3760Sstevel@tonic-gate 	(void) strlcpy(zent->zroot, "???", sizeof (zent->zroot));
3770Sstevel@tonic-gate 	zent->zstate_str = "???";
3780Sstevel@tonic-gate 
379*766Scarlsonj 	zent->zid = zid;
3800Sstevel@tonic-gate 
3810Sstevel@tonic-gate 	if ((err = zone_get_zonepath(zent->zname, root, sizeof (root))) !=
3820Sstevel@tonic-gate 	    Z_OK) {
3830Sstevel@tonic-gate 		errno = err;
3840Sstevel@tonic-gate 		zperror2(zent->zname, gettext("could not get zone path"));
3850Sstevel@tonic-gate 		return (Z_ERR);
3860Sstevel@tonic-gate 	}
3870Sstevel@tonic-gate 	(void) strlcpy(zent->zroot, root, sizeof (zent->zroot));
3880Sstevel@tonic-gate 
3890Sstevel@tonic-gate 	if ((err = zone_get_state(zent->zname, &zent->zstate_num)) != Z_OK) {
3900Sstevel@tonic-gate 		errno = err;
3910Sstevel@tonic-gate 		zperror2(zent->zname, gettext("could not get state"));
3920Sstevel@tonic-gate 		return (Z_ERR);
3930Sstevel@tonic-gate 	}
3940Sstevel@tonic-gate 	zent->zstate_str = zone_state_str(zent->zstate_num);
3950Sstevel@tonic-gate 
3960Sstevel@tonic-gate 	return (Z_OK);
3970Sstevel@tonic-gate }
3980Sstevel@tonic-gate 
3990Sstevel@tonic-gate /*
4000Sstevel@tonic-gate  * fetch_zents() calls zone_list(2) to find out how many zones are running
4010Sstevel@tonic-gate  * (which is stored in the global nzents), then calls zone_list(2) again
4020Sstevel@tonic-gate  * to fetch the list of running zones (stored in the global zents).  This
4030Sstevel@tonic-gate  * function may be called multiple times, so if zents is already set, we
4040Sstevel@tonic-gate  * return immediately to save work.
4050Sstevel@tonic-gate  */
4060Sstevel@tonic-gate 
4070Sstevel@tonic-gate static int
408*766Scarlsonj fetch_zents(void)
4090Sstevel@tonic-gate {
4100Sstevel@tonic-gate 	zoneid_t *zids = NULL;
4110Sstevel@tonic-gate 	uint_t nzents_saved;
412*766Scarlsonj 	int i, retv;
413*766Scarlsonj 	FILE *fp;
414*766Scarlsonj 	boolean_t inaltroot;
415*766Scarlsonj 	zone_entry_t *zentp;
4160Sstevel@tonic-gate 
4170Sstevel@tonic-gate 	if (nzents > 0)
4180Sstevel@tonic-gate 		return (Z_OK);
4190Sstevel@tonic-gate 
4200Sstevel@tonic-gate 	if (zone_list(NULL, &nzents) != 0) {
4210Sstevel@tonic-gate 		zperror(gettext("failed to get zoneid list"), B_FALSE);
4220Sstevel@tonic-gate 		return (Z_ERR);
4230Sstevel@tonic-gate 	}
4240Sstevel@tonic-gate 
4250Sstevel@tonic-gate again:
4260Sstevel@tonic-gate 	if (nzents == 0)
4270Sstevel@tonic-gate 		return (Z_OK);
4280Sstevel@tonic-gate 
4290Sstevel@tonic-gate 	zids = safe_calloc(nzents, sizeof (zoneid_t));
4300Sstevel@tonic-gate 	nzents_saved = nzents;
4310Sstevel@tonic-gate 
4320Sstevel@tonic-gate 	if (zone_list(zids, &nzents) != 0) {
4330Sstevel@tonic-gate 		zperror(gettext("failed to get zone list"), B_FALSE);
4340Sstevel@tonic-gate 		free(zids);
4350Sstevel@tonic-gate 		return (Z_ERR);
4360Sstevel@tonic-gate 	}
4370Sstevel@tonic-gate 	if (nzents != nzents_saved) {
4380Sstevel@tonic-gate 		/* list changed, try again */
4390Sstevel@tonic-gate 		free(zids);
4400Sstevel@tonic-gate 		goto again;
4410Sstevel@tonic-gate 	}
4420Sstevel@tonic-gate 
4430Sstevel@tonic-gate 	zents = safe_calloc(nzents, sizeof (zone_entry_t));
4440Sstevel@tonic-gate 
445*766Scarlsonj 	inaltroot = zonecfg_in_alt_root();
446*766Scarlsonj 	if (inaltroot)
447*766Scarlsonj 		fp = zonecfg_open_scratch("", B_FALSE);
448*766Scarlsonj 	else
449*766Scarlsonj 		fp = NULL;
450*766Scarlsonj 	zentp = zents;
451*766Scarlsonj 	retv = Z_OK;
4520Sstevel@tonic-gate 	for (i = 0; i < nzents; i++) {
4530Sstevel@tonic-gate 		char name[ZONENAME_MAX];
454*766Scarlsonj 		char altname[ZONENAME_MAX];
4550Sstevel@tonic-gate 
456*766Scarlsonj 		if (getzonenamebyid(zids[i], name, sizeof (name)) < 0) {
4570Sstevel@tonic-gate 			zperror(gettext("failed to get zone name"), B_FALSE);
458*766Scarlsonj 			retv = Z_ERR;
459*766Scarlsonj 			continue;
460*766Scarlsonj 		}
461*766Scarlsonj 		if (zonecfg_is_scratch(name)) {
462*766Scarlsonj 			/* Ignore scratch zones by default */
463*766Scarlsonj 			if (!inaltroot)
464*766Scarlsonj 				continue;
465*766Scarlsonj 			if (fp == NULL ||
466*766Scarlsonj 			    zonecfg_reverse_scratch(fp, name, altname,
467*766Scarlsonj 			    sizeof (altname), NULL, 0) == -1) {
468*766Scarlsonj 				zerror(gettext("cannot resolve scratch "
469*766Scarlsonj 				    "zone %s"), name);
470*766Scarlsonj 				retv = Z_ERR;
471*766Scarlsonj 				continue;
472*766Scarlsonj 			}
473*766Scarlsonj 			(void) strcpy(name, altname);
474*766Scarlsonj 		} else {
475*766Scarlsonj 			/* Ignore non-scratch when in an alternate root */
476*766Scarlsonj 			if (inaltroot && strcmp(name, GLOBAL_ZONENAME) != 0)
477*766Scarlsonj 				continue;
478*766Scarlsonj 		}
479*766Scarlsonj 		if (lookup_zone_info(name, zids[i], zentp) != Z_OK) {
480*766Scarlsonj 			zerror(gettext("failed to get zone data"));
481*766Scarlsonj 			retv = Z_ERR;
482*766Scarlsonj 			continue;
483*766Scarlsonj 		}
484*766Scarlsonj 		zentp++;
4850Sstevel@tonic-gate 	}
486*766Scarlsonj 	nzents = zentp - zents;
487*766Scarlsonj 	if (fp != NULL)
488*766Scarlsonj 		zonecfg_close_scratch(fp);
4890Sstevel@tonic-gate 
4900Sstevel@tonic-gate 	free(zids);
491*766Scarlsonj 	return (retv);
4920Sstevel@tonic-gate }
4930Sstevel@tonic-gate 
494*766Scarlsonj static int
4950Sstevel@tonic-gate zone_print_list(zone_state_t min_state, boolean_t verbose, boolean_t parsable)
4960Sstevel@tonic-gate {
4970Sstevel@tonic-gate 	int i;
4980Sstevel@tonic-gate 	zone_entry_t zent;
4990Sstevel@tonic-gate 	FILE *cookie;
5000Sstevel@tonic-gate 	char *name;
5010Sstevel@tonic-gate 
5020Sstevel@tonic-gate 	/*
5030Sstevel@tonic-gate 	 * First get the list of running zones from the kernel and print them.
5040Sstevel@tonic-gate 	 * If that is all we need, then return.
5050Sstevel@tonic-gate 	 */
506*766Scarlsonj 	if ((i = fetch_zents()) != Z_OK) {
5070Sstevel@tonic-gate 		/*
5080Sstevel@tonic-gate 		 * No need for error messages; fetch_zents() has already taken
5090Sstevel@tonic-gate 		 * care of this.
5100Sstevel@tonic-gate 		 */
511*766Scarlsonj 		return (i);
5120Sstevel@tonic-gate 	}
513*766Scarlsonj 	for (i = 0; i < nzents; i++)
5140Sstevel@tonic-gate 		zone_print(&zents[i], verbose, parsable);
5150Sstevel@tonic-gate 	if (min_state >= ZONE_STATE_RUNNING)
516*766Scarlsonj 		return (Z_OK);
5170Sstevel@tonic-gate 	/*
5180Sstevel@tonic-gate 	 * Next, get the full list of zones from the configuration, skipping
5190Sstevel@tonic-gate 	 * any we have already printed.
5200Sstevel@tonic-gate 	 */
5210Sstevel@tonic-gate 	cookie = setzoneent();
5220Sstevel@tonic-gate 	while ((name = getzoneent(cookie)) != NULL) {
5230Sstevel@tonic-gate 		for (i = 0; i < nzents; i++) {
5240Sstevel@tonic-gate 			if (strcmp(zents[i].zname, name) == 0)
5250Sstevel@tonic-gate 				break;
5260Sstevel@tonic-gate 		}
5270Sstevel@tonic-gate 		if (i < nzents) {
5280Sstevel@tonic-gate 			free(name);
5290Sstevel@tonic-gate 			continue;
5300Sstevel@tonic-gate 		}
531*766Scarlsonj 		if (lookup_zone_info(name, ZONE_ID_UNDEFINED, &zent) != Z_OK) {
5320Sstevel@tonic-gate 			free(name);
5330Sstevel@tonic-gate 			continue;
5340Sstevel@tonic-gate 		}
5350Sstevel@tonic-gate 		free(name);
5360Sstevel@tonic-gate 		if (zent.zstate_num >= min_state)
5370Sstevel@tonic-gate 			zone_print(&zent, verbose, parsable);
5380Sstevel@tonic-gate 	}
5390Sstevel@tonic-gate 	endzoneent(cookie);
540*766Scarlsonj 	return (Z_OK);
5410Sstevel@tonic-gate }
5420Sstevel@tonic-gate 
5430Sstevel@tonic-gate static zone_entry_t *
5440Sstevel@tonic-gate lookup_running_zone(char *str)
5450Sstevel@tonic-gate {
5460Sstevel@tonic-gate 	zoneid_t zoneid;
5470Sstevel@tonic-gate 	char *cp;
5480Sstevel@tonic-gate 	int i;
5490Sstevel@tonic-gate 
5500Sstevel@tonic-gate 	if (fetch_zents() != Z_OK)
5510Sstevel@tonic-gate 		return (NULL);
5520Sstevel@tonic-gate 
5530Sstevel@tonic-gate 	for (i = 0; i < nzents; i++) {
5540Sstevel@tonic-gate 		if (strcmp(str, zents[i].zname) == 0)
5550Sstevel@tonic-gate 			return (&zents[i]);
5560Sstevel@tonic-gate 	}
5570Sstevel@tonic-gate 	errno = 0;
5580Sstevel@tonic-gate 	zoneid = strtol(str, &cp, 0);
5590Sstevel@tonic-gate 	if (zoneid < MIN_ZONEID || zoneid > MAX_ZONEID ||
5600Sstevel@tonic-gate 	    errno != 0 || *cp != '\0')
5610Sstevel@tonic-gate 		return (NULL);
5620Sstevel@tonic-gate 	for (i = 0; i < nzents; i++) {
5630Sstevel@tonic-gate 		if (zoneid == zents[i].zid)
5640Sstevel@tonic-gate 			return (&zents[i]);
5650Sstevel@tonic-gate 	}
5660Sstevel@tonic-gate 	return (NULL);
5670Sstevel@tonic-gate }
5680Sstevel@tonic-gate 
5690Sstevel@tonic-gate /*
5700Sstevel@tonic-gate  * Check a bit in a mode_t: if on is B_TRUE, that bit should be on; if
5710Sstevel@tonic-gate  * B_FALSE, it should be off.  Return B_TRUE if the mode is bad (incorrect).
5720Sstevel@tonic-gate  */
5730Sstevel@tonic-gate static boolean_t
5740Sstevel@tonic-gate bad_mode_bit(mode_t mode, mode_t bit, boolean_t on, char *file)
5750Sstevel@tonic-gate {
5760Sstevel@tonic-gate 	char *str;
5770Sstevel@tonic-gate 
5780Sstevel@tonic-gate 	assert(bit == S_IRUSR || bit == S_IWUSR || bit == S_IXUSR ||
5790Sstevel@tonic-gate 	    bit == S_IRGRP || bit == S_IWGRP || bit == S_IXGRP ||
5800Sstevel@tonic-gate 	    bit == S_IROTH || bit == S_IWOTH || bit == S_IXOTH);
5810Sstevel@tonic-gate 	/*
5820Sstevel@tonic-gate 	 * TRANSLATION_NOTE
5830Sstevel@tonic-gate 	 * The strings below will be used as part of a larger message,
5840Sstevel@tonic-gate 	 * either:
5850Sstevel@tonic-gate 	 * (file name) must be (owner|group|world) (read|writ|execut)able
5860Sstevel@tonic-gate 	 * or
5870Sstevel@tonic-gate 	 * (file name) must not be (owner|group|world) (read|writ|execut)able
5880Sstevel@tonic-gate 	 */
5890Sstevel@tonic-gate 	switch (bit) {
5900Sstevel@tonic-gate 	case S_IRUSR:
5910Sstevel@tonic-gate 		str = gettext("owner readable");
5920Sstevel@tonic-gate 		break;
5930Sstevel@tonic-gate 	case S_IWUSR:
5940Sstevel@tonic-gate 		str = gettext("owner writable");
5950Sstevel@tonic-gate 		break;
5960Sstevel@tonic-gate 	case S_IXUSR:
5970Sstevel@tonic-gate 		str = gettext("owner executable");
5980Sstevel@tonic-gate 		break;
5990Sstevel@tonic-gate 	case S_IRGRP:
6000Sstevel@tonic-gate 		str = gettext("group readable");
6010Sstevel@tonic-gate 		break;
6020Sstevel@tonic-gate 	case S_IWGRP:
6030Sstevel@tonic-gate 		str = gettext("group writable");
6040Sstevel@tonic-gate 		break;
6050Sstevel@tonic-gate 	case S_IXGRP:
6060Sstevel@tonic-gate 		str = gettext("group executable");
6070Sstevel@tonic-gate 		break;
6080Sstevel@tonic-gate 	case S_IROTH:
6090Sstevel@tonic-gate 		str = gettext("world readable");
6100Sstevel@tonic-gate 		break;
6110Sstevel@tonic-gate 	case S_IWOTH:
6120Sstevel@tonic-gate 		str = gettext("world writable");
6130Sstevel@tonic-gate 		break;
6140Sstevel@tonic-gate 	case S_IXOTH:
6150Sstevel@tonic-gate 		str = gettext("world executable");
6160Sstevel@tonic-gate 		break;
6170Sstevel@tonic-gate 	}
6180Sstevel@tonic-gate 	if ((mode & bit) == (on ? 0 : bit)) {
6190Sstevel@tonic-gate 		/*
6200Sstevel@tonic-gate 		 * TRANSLATION_NOTE
6210Sstevel@tonic-gate 		 * The first parameter below is a file name; the second
6220Sstevel@tonic-gate 		 * is one of the "(owner|group|world) (read|writ|execut)able"
6230Sstevel@tonic-gate 		 * strings from above.
6240Sstevel@tonic-gate 		 */
6250Sstevel@tonic-gate 		/*
6260Sstevel@tonic-gate 		 * The code below could be simplified but not in a way
6270Sstevel@tonic-gate 		 * that would easily translate to non-English locales.
6280Sstevel@tonic-gate 		 */
6290Sstevel@tonic-gate 		if (on) {
6300Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("%s must be %s.\n"),
6310Sstevel@tonic-gate 			    file, str);
6320Sstevel@tonic-gate 		} else {
6330Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("%s must not be %s.\n"),
6340Sstevel@tonic-gate 			    file, str);
6350Sstevel@tonic-gate 		}
6360Sstevel@tonic-gate 		return (B_TRUE);
6370Sstevel@tonic-gate 	}
6380Sstevel@tonic-gate 	return (B_FALSE);
6390Sstevel@tonic-gate }
6400Sstevel@tonic-gate 
6410Sstevel@tonic-gate /*
6420Sstevel@tonic-gate  * We want to make sure that no zone has its zone path as a child node
6430Sstevel@tonic-gate  * (in the directory sense) of any other.  We do that by comparing this
6440Sstevel@tonic-gate  * zone's path to the path of all other (non-global) zones.  The comparison
6450Sstevel@tonic-gate  * in each case is simple: add '/' to the end of the path, then do a
6460Sstevel@tonic-gate  * strncmp() of the two paths, using the length of the shorter one.
6470Sstevel@tonic-gate  */
6480Sstevel@tonic-gate 
6490Sstevel@tonic-gate static int
6500Sstevel@tonic-gate crosscheck_zonepaths(char *path)
6510Sstevel@tonic-gate {
6520Sstevel@tonic-gate 	char rpath[MAXPATHLEN];		/* resolved path */
6530Sstevel@tonic-gate 	char path_copy[MAXPATHLEN];	/* copy of original path */
6540Sstevel@tonic-gate 	char rpath_copy[MAXPATHLEN];	/* copy of original rpath */
6550Sstevel@tonic-gate 	struct zoneent *ze;
6560Sstevel@tonic-gate 	int res, err;
6570Sstevel@tonic-gate 	FILE *cookie;
6580Sstevel@tonic-gate 
6590Sstevel@tonic-gate 	cookie = setzoneent();
6600Sstevel@tonic-gate 	while ((ze = getzoneent_private(cookie)) != NULL) {
6610Sstevel@tonic-gate 		/* Skip zones which are not installed. */
6620Sstevel@tonic-gate 		if (ze->zone_state < ZONE_STATE_INSTALLED) {
6630Sstevel@tonic-gate 			free(ze);
6640Sstevel@tonic-gate 			continue;
6650Sstevel@tonic-gate 		}
6660Sstevel@tonic-gate 		/* Skip the global zone and the current target zone. */
6670Sstevel@tonic-gate 		if (strcmp(ze->zone_name, GLOBAL_ZONENAME) == 0 ||
6680Sstevel@tonic-gate 		    strcmp(ze->zone_name, target_zone) == 0) {
6690Sstevel@tonic-gate 			free(ze);
6700Sstevel@tonic-gate 			continue;
6710Sstevel@tonic-gate 		}
6720Sstevel@tonic-gate 		if (strlen(ze->zone_path) == 0) {
6730Sstevel@tonic-gate 			/* old index file without path, fall back */
6740Sstevel@tonic-gate 			if ((err = zone_get_zonepath(ze->zone_name,
6750Sstevel@tonic-gate 			    ze->zone_path, sizeof (ze->zone_path))) != Z_OK) {
6760Sstevel@tonic-gate 				errno = err;
6770Sstevel@tonic-gate 				zperror2(ze->zone_name,
6780Sstevel@tonic-gate 				    gettext("could not get zone path"));
6790Sstevel@tonic-gate 				free(ze);
6800Sstevel@tonic-gate 				continue;
6810Sstevel@tonic-gate 			}
6820Sstevel@tonic-gate 		}
683*766Scarlsonj 		(void) snprintf(path_copy, sizeof (path_copy), "%s%s",
684*766Scarlsonj 		    zonecfg_get_root(), ze->zone_path);
685*766Scarlsonj 		res = resolvepath(path_copy, rpath, sizeof (rpath));
6860Sstevel@tonic-gate 		if (res == -1) {
6870Sstevel@tonic-gate 			if (errno != ENOENT) {
688*766Scarlsonj 				zperror(path_copy, B_FALSE);
6890Sstevel@tonic-gate 				free(ze);
6900Sstevel@tonic-gate 				return (Z_ERR);
6910Sstevel@tonic-gate 			}
6920Sstevel@tonic-gate 			(void) printf(gettext("WARNING: zone %s is installed, "
6930Sstevel@tonic-gate 			    "but its %s %s does not exist.\n"), ze->zone_name,
694*766Scarlsonj 			    "zonepath", path_copy);
6950Sstevel@tonic-gate 			free(ze);
6960Sstevel@tonic-gate 			continue;
6970Sstevel@tonic-gate 		}
6980Sstevel@tonic-gate 		rpath[res] = '\0';
6990Sstevel@tonic-gate 		(void) snprintf(path_copy, sizeof (path_copy), "%s/", path);
7000Sstevel@tonic-gate 		(void) snprintf(rpath_copy, sizeof (rpath_copy), "%s/", rpath);
7010Sstevel@tonic-gate 		if (strncmp(path_copy, rpath_copy,
7020Sstevel@tonic-gate 		    min(strlen(path_copy), strlen(rpath_copy))) == 0) {
7030Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("%s zonepath (%s) and "
7040Sstevel@tonic-gate 			    "%s zonepath (%s) overlap.\n"),
7050Sstevel@tonic-gate 			    target_zone, path, ze->zone_name, rpath);
7060Sstevel@tonic-gate 			free(ze);
7070Sstevel@tonic-gate 			return (Z_ERR);
7080Sstevel@tonic-gate 		}
7090Sstevel@tonic-gate 		free(ze);
7100Sstevel@tonic-gate 	}
7110Sstevel@tonic-gate 	endzoneent(cookie);
7120Sstevel@tonic-gate 	return (Z_OK);
7130Sstevel@tonic-gate }
7140Sstevel@tonic-gate 
7150Sstevel@tonic-gate static int
7160Sstevel@tonic-gate validate_zonepath(char *path, int cmd_num)
7170Sstevel@tonic-gate {
7180Sstevel@tonic-gate 	int res;			/* result of last library/system call */
7190Sstevel@tonic-gate 	boolean_t err = B_FALSE;	/* have we run into an error? */
7200Sstevel@tonic-gate 	struct stat stbuf;
7210Sstevel@tonic-gate 	struct statvfs vfsbuf;
7220Sstevel@tonic-gate 	char rpath[MAXPATHLEN];		/* resolved path */
7230Sstevel@tonic-gate 	char ppath[MAXPATHLEN];		/* parent path */
7240Sstevel@tonic-gate 	char rppath[MAXPATHLEN];	/* resolved parent path */
7250Sstevel@tonic-gate 	char rootpath[MAXPATHLEN];	/* root path */
7260Sstevel@tonic-gate 	zone_state_t state;
7270Sstevel@tonic-gate 
7280Sstevel@tonic-gate 	if (path[0] != '/') {
7290Sstevel@tonic-gate 		(void) fprintf(stderr,
7300Sstevel@tonic-gate 		    gettext("%s is not an absolute path.\n"), path);
7310Sstevel@tonic-gate 		return (Z_ERR);
7320Sstevel@tonic-gate 	}
7330Sstevel@tonic-gate 	if ((res = resolvepath(path, rpath, sizeof (rpath))) == -1) {
7340Sstevel@tonic-gate 		if ((errno != ENOENT) ||
7350Sstevel@tonic-gate 		    (cmd_num != CMD_VERIFY && cmd_num != CMD_INSTALL)) {
7360Sstevel@tonic-gate 			zperror(path, B_FALSE);
7370Sstevel@tonic-gate 			return (Z_ERR);
7380Sstevel@tonic-gate 		}
7390Sstevel@tonic-gate 		if (cmd_num == CMD_VERIFY) {
7400Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("WARNING: %s does not "
7410Sstevel@tonic-gate 			    "exist, so it cannot be verified.\nWhen 'zoneadm "
7420Sstevel@tonic-gate 			    "%s' is run, '%s' will try to create\n%s, and '%s' "
7430Sstevel@tonic-gate 			    "will be tried again,\nbut the '%s' may fail if:\n"
7440Sstevel@tonic-gate 			    "the parent directory of %s is group- or other-"
7450Sstevel@tonic-gate 			    "writable\nor\n%s overlaps with any other "
7460Sstevel@tonic-gate 			    "installed zones.\n"), path,
7470Sstevel@tonic-gate 			    cmd_to_str(CMD_INSTALL), cmd_to_str(CMD_INSTALL),
7480Sstevel@tonic-gate 			    path, cmd_to_str(CMD_VERIFY),
7490Sstevel@tonic-gate 			    cmd_to_str(CMD_VERIFY), path, path);
7500Sstevel@tonic-gate 			return (Z_OK);
7510Sstevel@tonic-gate 		}
7520Sstevel@tonic-gate 		/*
7530Sstevel@tonic-gate 		 * The zonepath is supposed to be mode 700 but its
7540Sstevel@tonic-gate 		 * parent(s) 755.  So use 755 on the mkdirp() then
7550Sstevel@tonic-gate 		 * chmod() the zonepath itself to 700.
7560Sstevel@tonic-gate 		 */
7570Sstevel@tonic-gate 		if (mkdirp(path, DEFAULT_DIR_MODE) < 0) {
7580Sstevel@tonic-gate 			zperror(path, B_FALSE);
7590Sstevel@tonic-gate 			return (Z_ERR);
7600Sstevel@tonic-gate 		}
7610Sstevel@tonic-gate 		/*
7620Sstevel@tonic-gate 		 * If the chmod() fails, report the error, but might
7630Sstevel@tonic-gate 		 * as well continue the verify procedure.
7640Sstevel@tonic-gate 		 */
7650Sstevel@tonic-gate 		if (chmod(path, S_IRWXU) != 0)
7660Sstevel@tonic-gate 			zperror(path, B_FALSE);
7670Sstevel@tonic-gate 		/*
7680Sstevel@tonic-gate 		 * Since the mkdir() succeeded, we should not have to
7690Sstevel@tonic-gate 		 * worry about a subsequent ENOENT, thus this should
7700Sstevel@tonic-gate 		 * only recurse once.
7710Sstevel@tonic-gate 		 */
7720Sstevel@tonic-gate 		return (validate_zonepath(path, CMD_INSTALL));
7730Sstevel@tonic-gate 	}
7740Sstevel@tonic-gate 	rpath[res] = '\0';
7750Sstevel@tonic-gate 	if (strcmp(path, rpath) != 0) {
7760Sstevel@tonic-gate 		errno = Z_RESOLVED_PATH;
7770Sstevel@tonic-gate 		zperror(path, B_TRUE);
7780Sstevel@tonic-gate 		return (Z_ERR);
7790Sstevel@tonic-gate 	}
7800Sstevel@tonic-gate 	if ((res = stat(rpath, &stbuf)) != 0) {
7810Sstevel@tonic-gate 		zperror(rpath, B_FALSE);
7820Sstevel@tonic-gate 		return (Z_ERR);
7830Sstevel@tonic-gate 	}
7840Sstevel@tonic-gate 	if (!S_ISDIR(stbuf.st_mode)) {
7850Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s is not a directory.\n"),
7860Sstevel@tonic-gate 		    rpath);
7870Sstevel@tonic-gate 		return (Z_ERR);
7880Sstevel@tonic-gate 	}
7890Sstevel@tonic-gate 	if ((strcmp(stbuf.st_fstype, MNTTYPE_TMPFS) == 0) ||
7900Sstevel@tonic-gate 	    (strcmp(stbuf.st_fstype, MNTTYPE_XMEMFS) == 0)) {
7910Sstevel@tonic-gate 		(void) printf(gettext("WARNING: %s is on a temporary "
7920Sstevel@tonic-gate 		    "file-system.\n"), rpath);
7930Sstevel@tonic-gate 	}
7940Sstevel@tonic-gate 	if (crosscheck_zonepaths(rpath) != Z_OK)
7950Sstevel@tonic-gate 		return (Z_ERR);
7960Sstevel@tonic-gate 	/*
7970Sstevel@tonic-gate 	 * Try to collect and report as many minor errors as possible
7980Sstevel@tonic-gate 	 * before returning, so the user can learn everything that needs
7990Sstevel@tonic-gate 	 * to be fixed up front.
8000Sstevel@tonic-gate 	 */
8010Sstevel@tonic-gate 	if (stbuf.st_uid != 0) {
8020Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s is not owned by root.\n"),
8030Sstevel@tonic-gate 		    rpath);
8040Sstevel@tonic-gate 		err = B_TRUE;
8050Sstevel@tonic-gate 	}
8060Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IRUSR, B_TRUE, rpath);
8070Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IWUSR, B_TRUE, rpath);
8080Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IXUSR, B_TRUE, rpath);
8090Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IRGRP, B_FALSE, rpath);
8100Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IWGRP, B_FALSE, rpath);
8110Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IXGRP, B_FALSE, rpath);
8120Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IROTH, B_FALSE, rpath);
8130Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IWOTH, B_FALSE, rpath);
8140Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IXOTH, B_FALSE, rpath);
8150Sstevel@tonic-gate 
8160Sstevel@tonic-gate 	(void) snprintf(ppath, sizeof (ppath), "%s/..", path);
8170Sstevel@tonic-gate 	if ((res = resolvepath(ppath, rppath, sizeof (rppath))) == -1) {
8180Sstevel@tonic-gate 		zperror(ppath, B_FALSE);
8190Sstevel@tonic-gate 		return (Z_ERR);
8200Sstevel@tonic-gate 	}
8210Sstevel@tonic-gate 	rppath[res] = '\0';
8220Sstevel@tonic-gate 	if ((res = stat(rppath, &stbuf)) != 0) {
8230Sstevel@tonic-gate 		zperror(rppath, B_FALSE);
8240Sstevel@tonic-gate 		return (Z_ERR);
8250Sstevel@tonic-gate 	}
8260Sstevel@tonic-gate 	/* theoretically impossible */
8270Sstevel@tonic-gate 	if (!S_ISDIR(stbuf.st_mode)) {
8280Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s is not a directory.\n"),
8290Sstevel@tonic-gate 		    rppath);
8300Sstevel@tonic-gate 		return (Z_ERR);
8310Sstevel@tonic-gate 	}
8320Sstevel@tonic-gate 	if (stbuf.st_uid != 0) {
8330Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s is not owned by root.\n"),
8340Sstevel@tonic-gate 		    rppath);
8350Sstevel@tonic-gate 		err = B_TRUE;
8360Sstevel@tonic-gate 	}
8370Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IRUSR, B_TRUE, rppath);
8380Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IWUSR, B_TRUE, rppath);
8390Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IXUSR, B_TRUE, rppath);
8400Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IWGRP, B_FALSE, rppath);
8410Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IWOTH, B_FALSE, rppath);
8420Sstevel@tonic-gate 	if (strcmp(rpath, rppath) == 0) {
8430Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s is its own parent.\n"),
8440Sstevel@tonic-gate 		    rppath);
8450Sstevel@tonic-gate 		err = B_TRUE;
8460Sstevel@tonic-gate 	}
8470Sstevel@tonic-gate 
8480Sstevel@tonic-gate 	if (statvfs(rpath, &vfsbuf) != 0) {
8490Sstevel@tonic-gate 		zperror(rpath, B_FALSE);
8500Sstevel@tonic-gate 		return (Z_ERR);
8510Sstevel@tonic-gate 	}
8520Sstevel@tonic-gate 	if (strncmp(vfsbuf.f_basetype, "nfs", 3) == 0) {
8530Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("Zonepath %s is over NFS, "
8540Sstevel@tonic-gate 		    "which is not currently supported.\n"), rpath);
8550Sstevel@tonic-gate 		return (Z_ERR);
8560Sstevel@tonic-gate 	}
8570Sstevel@tonic-gate 
8580Sstevel@tonic-gate 	if ((res = zone_get_state(target_zone, &state)) != Z_OK) {
8590Sstevel@tonic-gate 		errno = res;
8600Sstevel@tonic-gate 		zperror2(target_zone, gettext("could not get state"));
8610Sstevel@tonic-gate 		return (Z_ERR);
8620Sstevel@tonic-gate 	}
8630Sstevel@tonic-gate 	/*
8640Sstevel@tonic-gate 	 * The existence of the root path is only bad in the configured state,
8650Sstevel@tonic-gate 	 * as it is *supposed* to be there at the installed and later states.
8660Sstevel@tonic-gate 	 * State/command mismatches are caught earlier in verify_details().
8670Sstevel@tonic-gate 	 */
8680Sstevel@tonic-gate 	if (state == ZONE_STATE_CONFIGURED) {
8690Sstevel@tonic-gate 		if (snprintf(rootpath, sizeof (rootpath), "%s/root", rpath) >=
8700Sstevel@tonic-gate 		    sizeof (rootpath)) {
8710Sstevel@tonic-gate 			(void) fprintf(stderr,
8720Sstevel@tonic-gate 			    gettext("Zonepath %s is too long.\n"), rpath);
8730Sstevel@tonic-gate 			return (Z_ERR);
8740Sstevel@tonic-gate 		}
8750Sstevel@tonic-gate 		if ((res = stat(rootpath, &stbuf)) == 0) {
8760Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("Rootpath %s exists; "
8770Sstevel@tonic-gate 			    "remove or move aside prior to %s.\n"), rootpath,
8780Sstevel@tonic-gate 			    cmd_to_str(CMD_INSTALL));
8790Sstevel@tonic-gate 			return (Z_ERR);
8800Sstevel@tonic-gate 		}
8810Sstevel@tonic-gate 	}
8820Sstevel@tonic-gate 
8830Sstevel@tonic-gate 	return (err ? Z_ERR : Z_OK);
8840Sstevel@tonic-gate }
8850Sstevel@tonic-gate 
8860Sstevel@tonic-gate static void
8870Sstevel@tonic-gate release_lock_file(int lockfd)
8880Sstevel@tonic-gate {
8890Sstevel@tonic-gate 	(void) close(lockfd);
8900Sstevel@tonic-gate }
8910Sstevel@tonic-gate 
8920Sstevel@tonic-gate static int
8930Sstevel@tonic-gate grab_lock_file(const char *zone_name, int *lockfd)
8940Sstevel@tonic-gate {
8950Sstevel@tonic-gate 	char pathbuf[PATH_MAX];
8960Sstevel@tonic-gate 	struct flock flock;
8970Sstevel@tonic-gate 
898*766Scarlsonj 	if (snprintf(pathbuf, sizeof (pathbuf), "%s%s", zonecfg_get_root(),
899*766Scarlsonj 	    ZONES_TMPDIR) >= sizeof (pathbuf)) {
900*766Scarlsonj 		zerror(gettext("alternate root path is too long"));
901*766Scarlsonj 		return (Z_ERR);
902*766Scarlsonj 	}
903*766Scarlsonj 	if (mkdir(pathbuf, S_IRWXU) < 0 && errno != EEXIST) {
904*766Scarlsonj 		zerror(gettext("could not mkdir %s: %s"), pathbuf,
9050Sstevel@tonic-gate 		    strerror(errno));
9060Sstevel@tonic-gate 		return (Z_ERR);
9070Sstevel@tonic-gate 	}
908*766Scarlsonj 	(void) chmod(pathbuf, S_IRWXU);
9090Sstevel@tonic-gate 
9100Sstevel@tonic-gate 	/*
9110Sstevel@tonic-gate 	 * One of these lock files is created for each zone (when needed).
9120Sstevel@tonic-gate 	 * The lock files are not cleaned up (except on system reboot),
9130Sstevel@tonic-gate 	 * but since there is only one per zone, there is no resource
9140Sstevel@tonic-gate 	 * starvation issue.
9150Sstevel@tonic-gate 	 */
916*766Scarlsonj 	if (snprintf(pathbuf, sizeof (pathbuf), "%s%s/%s.zoneadm.lock",
917*766Scarlsonj 	    zonecfg_get_root(), ZONES_TMPDIR, zone_name) >= sizeof (pathbuf)) {
918*766Scarlsonj 		zerror(gettext("alternate root path is too long"));
919*766Scarlsonj 		return (Z_ERR);
920*766Scarlsonj 	}
9210Sstevel@tonic-gate 	if ((*lockfd = open(pathbuf, O_RDWR|O_CREAT, S_IRUSR|S_IWUSR)) < 0) {
9220Sstevel@tonic-gate 		zerror(gettext("could not open %s: %s"), pathbuf,
9230Sstevel@tonic-gate 		    strerror(errno));
9240Sstevel@tonic-gate 		return (Z_ERR);
9250Sstevel@tonic-gate 	}
9260Sstevel@tonic-gate 	/*
9270Sstevel@tonic-gate 	 * Lock the file to synchronize with other zoneadmds
9280Sstevel@tonic-gate 	 */
9290Sstevel@tonic-gate 	flock.l_type = F_WRLCK;
9300Sstevel@tonic-gate 	flock.l_whence = SEEK_SET;
9310Sstevel@tonic-gate 	flock.l_start = (off_t)0;
9320Sstevel@tonic-gate 	flock.l_len = (off_t)0;
9330Sstevel@tonic-gate 	if (fcntl(*lockfd, F_SETLKW, &flock) < 0) {
9340Sstevel@tonic-gate 		zerror(gettext("unable to lock %s: %s"), pathbuf,
9350Sstevel@tonic-gate 		    strerror(errno));
9360Sstevel@tonic-gate 		release_lock_file(*lockfd);
9370Sstevel@tonic-gate 		return (Z_ERR);
9380Sstevel@tonic-gate 	}
9390Sstevel@tonic-gate 	return (Z_OK);
9400Sstevel@tonic-gate }
9410Sstevel@tonic-gate 
942*766Scarlsonj static boolean_t
9430Sstevel@tonic-gate get_doorname(const char *zone_name, char *buffer)
9440Sstevel@tonic-gate {
945*766Scarlsonj 	return (snprintf(buffer, PATH_MAX, "%s" ZONE_DOOR_PATH,
946*766Scarlsonj 	    zonecfg_get_root(), zone_name) < PATH_MAX);
9470Sstevel@tonic-gate }
9480Sstevel@tonic-gate 
9490Sstevel@tonic-gate /*
9500Sstevel@tonic-gate  * system daemons are not audited.  For the global zone, this occurs
9510Sstevel@tonic-gate  * "naturally" since init is started with the default audit
9520Sstevel@tonic-gate  * characteristics.  Since zoneadmd is a system daemon and it starts
9530Sstevel@tonic-gate  * init for a zone, it is necessary to clear out the audit
9540Sstevel@tonic-gate  * characteristics inherited from whomever started zoneadmd.  This is
9550Sstevel@tonic-gate  * indicated by the audit id, which is set from the ruid parameter of
9560Sstevel@tonic-gate  * adt_set_user(), below.
9570Sstevel@tonic-gate  */
9580Sstevel@tonic-gate 
9590Sstevel@tonic-gate static void
9600Sstevel@tonic-gate prepare_audit_context()
9610Sstevel@tonic-gate {
9620Sstevel@tonic-gate 	adt_session_data_t	*ah;
9630Sstevel@tonic-gate 	char			*failure = gettext("audit failure: %s");
9640Sstevel@tonic-gate 
9650Sstevel@tonic-gate 	if (adt_start_session(&ah, NULL, 0)) {
9660Sstevel@tonic-gate 		zerror(failure, strerror(errno));
9670Sstevel@tonic-gate 		return;
9680Sstevel@tonic-gate 	}
9690Sstevel@tonic-gate 	if (adt_set_user(ah, ADT_NO_AUDIT, ADT_NO_AUDIT,
9700Sstevel@tonic-gate 	    ADT_NO_AUDIT, ADT_NO_AUDIT, NULL, ADT_NEW)) {
9710Sstevel@tonic-gate 		zerror(failure, strerror(errno));
9720Sstevel@tonic-gate 		(void) adt_end_session(ah);
9730Sstevel@tonic-gate 		return;
9740Sstevel@tonic-gate 	}
9750Sstevel@tonic-gate 	if (adt_set_proc(ah))
9760Sstevel@tonic-gate 		zerror(failure, strerror(errno));
9770Sstevel@tonic-gate 
9780Sstevel@tonic-gate 	(void) adt_end_session(ah);
9790Sstevel@tonic-gate }
9800Sstevel@tonic-gate 
9810Sstevel@tonic-gate static int
9820Sstevel@tonic-gate start_zoneadmd(const char *zone_name)
9830Sstevel@tonic-gate {
9840Sstevel@tonic-gate 	char doorpath[PATH_MAX];
9850Sstevel@tonic-gate 	pid_t child_pid;
9860Sstevel@tonic-gate 	int error = Z_ERR;
9870Sstevel@tonic-gate 	int doorfd, lockfd;
9880Sstevel@tonic-gate 	struct door_info info;
9890Sstevel@tonic-gate 
990*766Scarlsonj 	if (!get_doorname(zone_name, doorpath))
991*766Scarlsonj 		return (Z_ERR);
9920Sstevel@tonic-gate 
9930Sstevel@tonic-gate 	if (grab_lock_file(zone_name, &lockfd) != Z_OK)
9940Sstevel@tonic-gate 		return (Z_ERR);
9950Sstevel@tonic-gate 
9960Sstevel@tonic-gate 	/*
9970Sstevel@tonic-gate 	 * Now that we have the lock, re-confirm that the daemon is
9980Sstevel@tonic-gate 	 * *not* up and working fine.  If it is still down, we have a green
9990Sstevel@tonic-gate 	 * light to start it.
10000Sstevel@tonic-gate 	 */
10010Sstevel@tonic-gate 	if ((doorfd = open(doorpath, O_RDONLY)) < 0) {
10020Sstevel@tonic-gate 		if (errno != ENOENT) {
10030Sstevel@tonic-gate 			zperror(doorpath, B_FALSE);
10040Sstevel@tonic-gate 			goto out;
10050Sstevel@tonic-gate 		}
10060Sstevel@tonic-gate 	} else {
10070Sstevel@tonic-gate 		if (door_info(doorfd, &info) == 0 &&
10080Sstevel@tonic-gate 		    ((info.di_attributes & DOOR_REVOKED) == 0)) {
10090Sstevel@tonic-gate 			error = Z_OK;
10100Sstevel@tonic-gate 			(void) close(doorfd);
10110Sstevel@tonic-gate 			goto out;
10120Sstevel@tonic-gate 		}
10130Sstevel@tonic-gate 		(void) close(doorfd);
10140Sstevel@tonic-gate 	}
10150Sstevel@tonic-gate 
10160Sstevel@tonic-gate 	if ((child_pid = fork()) == -1) {
10170Sstevel@tonic-gate 		zperror(gettext("could not fork"), B_FALSE);
10180Sstevel@tonic-gate 		goto out;
10190Sstevel@tonic-gate 	} else if (child_pid == 0) {
1020*766Scarlsonj 		const char *argv[6], **ap;
1021*766Scarlsonj 
10220Sstevel@tonic-gate 		/* child process */
10230Sstevel@tonic-gate 		prepare_audit_context();
10240Sstevel@tonic-gate 
1025*766Scarlsonj 		ap = argv;
1026*766Scarlsonj 		*ap++ = "zoneadmd";
1027*766Scarlsonj 		*ap++ = "-z";
1028*766Scarlsonj 		*ap++ = zone_name;
1029*766Scarlsonj 		if (zonecfg_in_alt_root()) {
1030*766Scarlsonj 			*ap++ = "-R";
1031*766Scarlsonj 			*ap++ = zonecfg_get_root();
1032*766Scarlsonj 		}
1033*766Scarlsonj 		*ap = NULL;
1034*766Scarlsonj 
1035*766Scarlsonj 		(void) execv("/usr/lib/zones/zoneadmd", (char * const *)argv);
10360Sstevel@tonic-gate 		zperror(gettext("could not exec zoneadmd"), B_FALSE);
10370Sstevel@tonic-gate 		_exit(Z_ERR);
10380Sstevel@tonic-gate 	} else {
10390Sstevel@tonic-gate 		/* parent process */
10400Sstevel@tonic-gate 		pid_t retval;
10410Sstevel@tonic-gate 		int pstatus = 0;
10420Sstevel@tonic-gate 
10430Sstevel@tonic-gate 		do {
10440Sstevel@tonic-gate 			retval = waitpid(child_pid, &pstatus, 0);
10450Sstevel@tonic-gate 		} while (retval != child_pid);
10460Sstevel@tonic-gate 		if (WIFSIGNALED(pstatus) || (WIFEXITED(pstatus) &&
10470Sstevel@tonic-gate 		    WEXITSTATUS(pstatus) != 0)) {
10480Sstevel@tonic-gate 			zerror(gettext("could not start %s"), "zoneadmd");
10490Sstevel@tonic-gate 			goto out;
10500Sstevel@tonic-gate 		}
10510Sstevel@tonic-gate 	}
10520Sstevel@tonic-gate 	error = Z_OK;
10530Sstevel@tonic-gate out:
10540Sstevel@tonic-gate 	release_lock_file(lockfd);
10550Sstevel@tonic-gate 	return (error);
10560Sstevel@tonic-gate }
10570Sstevel@tonic-gate 
10580Sstevel@tonic-gate static int
10590Sstevel@tonic-gate ping_zoneadmd(const char *zone_name)
10600Sstevel@tonic-gate {
10610Sstevel@tonic-gate 	char doorpath[PATH_MAX];
10620Sstevel@tonic-gate 	int doorfd;
10630Sstevel@tonic-gate 	struct door_info info;
10640Sstevel@tonic-gate 
1065*766Scarlsonj 	if (!get_doorname(zone_name, doorpath))
1066*766Scarlsonj 		return (Z_ERR);
10670Sstevel@tonic-gate 
10680Sstevel@tonic-gate 	if ((doorfd = open(doorpath, O_RDONLY)) < 0) {
10690Sstevel@tonic-gate 		return (Z_ERR);
10700Sstevel@tonic-gate 	}
10710Sstevel@tonic-gate 	if (door_info(doorfd, &info) == 0 &&
10720Sstevel@tonic-gate 	    ((info.di_attributes & DOOR_REVOKED) == 0)) {
10730Sstevel@tonic-gate 		(void) close(doorfd);
10740Sstevel@tonic-gate 		return (Z_OK);
10750Sstevel@tonic-gate 	}
10760Sstevel@tonic-gate 	(void) close(doorfd);
10770Sstevel@tonic-gate 	return (Z_ERR);
10780Sstevel@tonic-gate }
10790Sstevel@tonic-gate 
10800Sstevel@tonic-gate static int
10810Sstevel@tonic-gate call_zoneadmd(const char *zone_name, zone_cmd_arg_t *arg)
10820Sstevel@tonic-gate {
10830Sstevel@tonic-gate 	char doorpath[PATH_MAX];
10840Sstevel@tonic-gate 	int doorfd, result;
10850Sstevel@tonic-gate 	door_arg_t darg;
10860Sstevel@tonic-gate 
10870Sstevel@tonic-gate 	zoneid_t zoneid;
10880Sstevel@tonic-gate 	uint64_t uniqid = 0;
10890Sstevel@tonic-gate 
10900Sstevel@tonic-gate 	zone_cmd_rval_t *rvalp;
10910Sstevel@tonic-gate 	size_t rlen;
10920Sstevel@tonic-gate 	char *cp, *errbuf;
10930Sstevel@tonic-gate 
10940Sstevel@tonic-gate 	rlen = getpagesize();
10950Sstevel@tonic-gate 	if ((rvalp = malloc(rlen)) == NULL) {
10960Sstevel@tonic-gate 		zerror(gettext("failed to allocate %lu bytes: %s"), rlen,
10970Sstevel@tonic-gate 		    strerror(errno));
10980Sstevel@tonic-gate 		return (-1);
10990Sstevel@tonic-gate 	}
11000Sstevel@tonic-gate 
11010Sstevel@tonic-gate 	if ((zoneid = getzoneidbyname(zone_name)) != ZONE_ID_UNDEFINED) {
11020Sstevel@tonic-gate 		(void) zone_getattr(zoneid, ZONE_ATTR_UNIQID, &uniqid,
11030Sstevel@tonic-gate 		    sizeof (uniqid));
11040Sstevel@tonic-gate 	}
11050Sstevel@tonic-gate 	arg->uniqid = uniqid;
11060Sstevel@tonic-gate 	(void) strlcpy(arg->locale, locale, sizeof (arg->locale));
1107*766Scarlsonj 	if (!get_doorname(zone_name, doorpath)) {
1108*766Scarlsonj 		zerror(gettext("alternate root path is too long"));
1109*766Scarlsonj 		free(rvalp);
1110*766Scarlsonj 		return (-1);
1111*766Scarlsonj 	}
11120Sstevel@tonic-gate 
11130Sstevel@tonic-gate 	/*
11140Sstevel@tonic-gate 	 * Loop trying to start zoneadmd; if something goes seriously
11150Sstevel@tonic-gate 	 * wrong we break out and fail.
11160Sstevel@tonic-gate 	 */
11170Sstevel@tonic-gate 	for (;;) {
11180Sstevel@tonic-gate 		if (start_zoneadmd(zone_name) != Z_OK)
11190Sstevel@tonic-gate 			break;
11200Sstevel@tonic-gate 
11210Sstevel@tonic-gate 		if ((doorfd = open(doorpath, O_RDONLY)) < 0) {
11220Sstevel@tonic-gate 			zperror(gettext("failed to open zone door"), B_FALSE);
11230Sstevel@tonic-gate 			break;
11240Sstevel@tonic-gate 		}
11250Sstevel@tonic-gate 
11260Sstevel@tonic-gate 		darg.data_ptr = (char *)arg;
11270Sstevel@tonic-gate 		darg.data_size = sizeof (*arg);
11280Sstevel@tonic-gate 		darg.desc_ptr = NULL;
11290Sstevel@tonic-gate 		darg.desc_num = 0;
11300Sstevel@tonic-gate 		darg.rbuf = (char *)rvalp;
11310Sstevel@tonic-gate 		darg.rsize = rlen;
11320Sstevel@tonic-gate 		if (door_call(doorfd, &darg) != 0) {
11330Sstevel@tonic-gate 			(void) close(doorfd);
11340Sstevel@tonic-gate 			/*
11350Sstevel@tonic-gate 			 * We'll get EBADF if the door has been revoked.
11360Sstevel@tonic-gate 			 */
11370Sstevel@tonic-gate 			if (errno != EBADF) {
11380Sstevel@tonic-gate 				zperror(gettext("door_call failed"), B_FALSE);
11390Sstevel@tonic-gate 				break;
11400Sstevel@tonic-gate 			}
11410Sstevel@tonic-gate 			continue;	/* take another lap */
11420Sstevel@tonic-gate 		}
11430Sstevel@tonic-gate 		(void) close(doorfd);
11440Sstevel@tonic-gate 
11450Sstevel@tonic-gate 		if (darg.data_size == 0) {
11460Sstevel@tonic-gate 			/* Door server is going away; kick it again. */
11470Sstevel@tonic-gate 			continue;
11480Sstevel@tonic-gate 		}
11490Sstevel@tonic-gate 
11500Sstevel@tonic-gate 		errbuf = rvalp->errbuf;
11510Sstevel@tonic-gate 		while (*errbuf != '\0') {
11520Sstevel@tonic-gate 			/*
11530Sstevel@tonic-gate 			 * Remove any newlines since zerror()
11540Sstevel@tonic-gate 			 * will append one automatically.
11550Sstevel@tonic-gate 			 */
11560Sstevel@tonic-gate 			cp = strchr(errbuf, '\n');
11570Sstevel@tonic-gate 			if (cp != NULL)
11580Sstevel@tonic-gate 				*cp = '\0';
11590Sstevel@tonic-gate 			zerror("%s", errbuf);
11600Sstevel@tonic-gate 			if (cp == NULL)
11610Sstevel@tonic-gate 				break;
11620Sstevel@tonic-gate 			errbuf = cp + 1;
11630Sstevel@tonic-gate 		}
11640Sstevel@tonic-gate 		result = rvalp->rval == 0 ? 0 : -1;
11650Sstevel@tonic-gate 		free(rvalp);
11660Sstevel@tonic-gate 		return (result);
11670Sstevel@tonic-gate 	}
11680Sstevel@tonic-gate 
11690Sstevel@tonic-gate 	free(rvalp);
11700Sstevel@tonic-gate 	return (-1);
11710Sstevel@tonic-gate }
11720Sstevel@tonic-gate 
11730Sstevel@tonic-gate static int
11740Sstevel@tonic-gate ready_func(int argc, char *argv[])
11750Sstevel@tonic-gate {
11760Sstevel@tonic-gate 	zone_cmd_arg_t zarg;
11770Sstevel@tonic-gate 	int arg;
11780Sstevel@tonic-gate 
1179*766Scarlsonj 	if (zonecfg_in_alt_root()) {
1180*766Scarlsonj 		zerror(gettext("cannot ready zone in alternate root"));
1181*766Scarlsonj 		return (Z_ERR);
1182*766Scarlsonj 	}
1183*766Scarlsonj 
11840Sstevel@tonic-gate 	optind = 0;
11850Sstevel@tonic-gate 	if ((arg = getopt(argc, argv, "?")) != EOF) {
11860Sstevel@tonic-gate 		switch (arg) {
11870Sstevel@tonic-gate 		case '?':
11880Sstevel@tonic-gate 			sub_usage(SHELP_READY, CMD_READY);
11890Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
11900Sstevel@tonic-gate 		default:
11910Sstevel@tonic-gate 			sub_usage(SHELP_READY, CMD_READY);
11920Sstevel@tonic-gate 			return (Z_USAGE);
11930Sstevel@tonic-gate 		}
11940Sstevel@tonic-gate 	}
11950Sstevel@tonic-gate 	if (argc > optind) {
11960Sstevel@tonic-gate 		sub_usage(SHELP_READY, CMD_READY);
11970Sstevel@tonic-gate 		return (Z_USAGE);
11980Sstevel@tonic-gate 	}
11990Sstevel@tonic-gate 	if (sanity_check(target_zone, CMD_READY, B_FALSE, B_FALSE) != Z_OK)
12000Sstevel@tonic-gate 		return (Z_ERR);
12010Sstevel@tonic-gate 	if (verify_details(CMD_READY) != Z_OK)
12020Sstevel@tonic-gate 		return (Z_ERR);
12030Sstevel@tonic-gate 
12040Sstevel@tonic-gate 	zarg.cmd = Z_READY;
12050Sstevel@tonic-gate 	if (call_zoneadmd(target_zone, &zarg) != 0) {
12060Sstevel@tonic-gate 		zerror(gettext("call to %s failed"), "zoneadmd");
12070Sstevel@tonic-gate 		return (Z_ERR);
12080Sstevel@tonic-gate 	}
12090Sstevel@tonic-gate 	return (Z_OK);
12100Sstevel@tonic-gate }
12110Sstevel@tonic-gate 
12120Sstevel@tonic-gate static int
12130Sstevel@tonic-gate boot_func(int argc, char *argv[])
12140Sstevel@tonic-gate {
12150Sstevel@tonic-gate 	zone_cmd_arg_t zarg;
12160Sstevel@tonic-gate 	int arg;
12170Sstevel@tonic-gate 
1218*766Scarlsonj 	if (zonecfg_in_alt_root()) {
1219*766Scarlsonj 		zerror(gettext("cannot boot zone in alternate root"));
1220*766Scarlsonj 		return (Z_ERR);
1221*766Scarlsonj 	}
1222*766Scarlsonj 
12230Sstevel@tonic-gate 	zarg.bootbuf[0] = '\0';
12240Sstevel@tonic-gate 
12250Sstevel@tonic-gate 	/*
12260Sstevel@tonic-gate 	 * At the current time, the only supported subargument to the
12270Sstevel@tonic-gate 	 * "boot" subcommand is "-s" which specifies a single-user boot.
12280Sstevel@tonic-gate 	 * In the future, other boot arguments should be supported
12290Sstevel@tonic-gate 	 * including "-m" for specifying alternate smf(5) milestones.
12300Sstevel@tonic-gate 	 */
12310Sstevel@tonic-gate 	optind = 0;
12320Sstevel@tonic-gate 	if ((arg = getopt(argc, argv, "?s")) != EOF) {
12330Sstevel@tonic-gate 		switch (arg) {
12340Sstevel@tonic-gate 		case '?':
12350Sstevel@tonic-gate 			sub_usage(SHELP_BOOT, CMD_BOOT);
12360Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
12370Sstevel@tonic-gate 		case 's':
12380Sstevel@tonic-gate 			(void) strlcpy(zarg.bootbuf, "-s",
12390Sstevel@tonic-gate 			    sizeof (zarg.bootbuf));
12400Sstevel@tonic-gate 			break;
12410Sstevel@tonic-gate 		default:
12420Sstevel@tonic-gate 			sub_usage(SHELP_BOOT, CMD_BOOT);
12430Sstevel@tonic-gate 			return (Z_USAGE);
12440Sstevel@tonic-gate 		}
12450Sstevel@tonic-gate 	}
12460Sstevel@tonic-gate 	if (argc > optind) {
12470Sstevel@tonic-gate 		sub_usage(SHELP_BOOT, CMD_BOOT);
12480Sstevel@tonic-gate 		return (Z_USAGE);
12490Sstevel@tonic-gate 	}
12500Sstevel@tonic-gate 	if (sanity_check(target_zone, CMD_BOOT, B_FALSE, B_FALSE) != Z_OK)
12510Sstevel@tonic-gate 		return (Z_ERR);
12520Sstevel@tonic-gate 	if (verify_details(CMD_BOOT) != Z_OK)
12530Sstevel@tonic-gate 		return (Z_ERR);
12540Sstevel@tonic-gate 	zarg.cmd = Z_BOOT;
12550Sstevel@tonic-gate 	if (call_zoneadmd(target_zone, &zarg) != 0) {
12560Sstevel@tonic-gate 		zerror(gettext("call to %s failed"), "zoneadmd");
12570Sstevel@tonic-gate 		return (Z_ERR);
12580Sstevel@tonic-gate 	}
12590Sstevel@tonic-gate 	return (Z_OK);
12600Sstevel@tonic-gate }
12610Sstevel@tonic-gate 
12620Sstevel@tonic-gate static void
12630Sstevel@tonic-gate fake_up_local_zone(zoneid_t zid, zone_entry_t *zeptr)
12640Sstevel@tonic-gate {
12650Sstevel@tonic-gate 	ssize_t result;
12660Sstevel@tonic-gate 
12670Sstevel@tonic-gate 	zeptr->zid = zid;
12680Sstevel@tonic-gate 	/*
12690Sstevel@tonic-gate 	 * Since we're looking up our own (non-global) zone name,
12700Sstevel@tonic-gate 	 * we can be assured that it will succeed.
12710Sstevel@tonic-gate 	 */
12720Sstevel@tonic-gate 	result = getzonenamebyid(zid, zeptr->zname, sizeof (zeptr->zname));
12730Sstevel@tonic-gate 	assert(result >= 0);
12740Sstevel@tonic-gate 	(void) strlcpy(zeptr->zroot, "/", sizeof (zeptr->zroot));
12750Sstevel@tonic-gate 	zeptr->zstate_str = "running";
12760Sstevel@tonic-gate }
12770Sstevel@tonic-gate 
12780Sstevel@tonic-gate static int
12790Sstevel@tonic-gate list_func(int argc, char *argv[])
12800Sstevel@tonic-gate {
12810Sstevel@tonic-gate 	zone_entry_t *zentp, zent;
1282*766Scarlsonj 	int arg, retv;
12830Sstevel@tonic-gate 	boolean_t output = B_FALSE, verbose = B_FALSE, parsable = B_FALSE;
12840Sstevel@tonic-gate 	zone_state_t min_state = ZONE_STATE_RUNNING;
12850Sstevel@tonic-gate 	zoneid_t zone_id = getzoneid();
12860Sstevel@tonic-gate 
12870Sstevel@tonic-gate 	if (target_zone == NULL) {
12880Sstevel@tonic-gate 		/* all zones: default view to running but allow override */
12890Sstevel@tonic-gate 		optind = 0;
12900Sstevel@tonic-gate 		while ((arg = getopt(argc, argv, "?cipv")) != EOF) {
12910Sstevel@tonic-gate 			switch (arg) {
12920Sstevel@tonic-gate 			case '?':
12930Sstevel@tonic-gate 				sub_usage(SHELP_LIST, CMD_LIST);
12940Sstevel@tonic-gate 				return (optopt == '?' ? Z_OK : Z_USAGE);
12950Sstevel@tonic-gate 			case 'c':
12960Sstevel@tonic-gate 				min_state = ZONE_STATE_CONFIGURED;
12970Sstevel@tonic-gate 				break;
12980Sstevel@tonic-gate 			case 'i':
12990Sstevel@tonic-gate 				min_state = ZONE_STATE_INSTALLED;
13000Sstevel@tonic-gate 				break;
13010Sstevel@tonic-gate 			case 'p':
13020Sstevel@tonic-gate 				parsable = B_TRUE;
13030Sstevel@tonic-gate 				break;
13040Sstevel@tonic-gate 			case 'v':
13050Sstevel@tonic-gate 				verbose = B_TRUE;
13060Sstevel@tonic-gate 				break;
13070Sstevel@tonic-gate 			default:
13080Sstevel@tonic-gate 				sub_usage(SHELP_LIST, CMD_LIST);
13090Sstevel@tonic-gate 				return (Z_USAGE);
13100Sstevel@tonic-gate 			}
13110Sstevel@tonic-gate 		}
13120Sstevel@tonic-gate 		if (parsable && verbose) {
13130Sstevel@tonic-gate 			zerror(gettext("%s -p and -v are mutually exclusive."),
13140Sstevel@tonic-gate 			    cmd_to_str(CMD_LIST));
13150Sstevel@tonic-gate 			return (Z_ERR);
13160Sstevel@tonic-gate 		}
13170Sstevel@tonic-gate 		if (zone_id == GLOBAL_ZONEID) {
1318*766Scarlsonj 			retv = zone_print_list(min_state, verbose, parsable);
13190Sstevel@tonic-gate 		} else {
1320*766Scarlsonj 			retv = Z_OK;
13210Sstevel@tonic-gate 			fake_up_local_zone(zone_id, &zent);
13220Sstevel@tonic-gate 			zone_print(&zent, verbose, parsable);
13230Sstevel@tonic-gate 		}
1324*766Scarlsonj 		return (retv);
13250Sstevel@tonic-gate 	}
13260Sstevel@tonic-gate 
13270Sstevel@tonic-gate 	/*
13280Sstevel@tonic-gate 	 * Specific target zone: disallow -i/-c suboptions.
13290Sstevel@tonic-gate 	 */
13300Sstevel@tonic-gate 	optind = 0;
13310Sstevel@tonic-gate 	while ((arg = getopt(argc, argv, "?pv")) != EOF) {
13320Sstevel@tonic-gate 		switch (arg) {
13330Sstevel@tonic-gate 		case '?':
13340Sstevel@tonic-gate 			sub_usage(SHELP_LIST, CMD_LIST);
13350Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
13360Sstevel@tonic-gate 		case 'p':
13370Sstevel@tonic-gate 			parsable = B_TRUE;
13380Sstevel@tonic-gate 			break;
13390Sstevel@tonic-gate 		case 'v':
13400Sstevel@tonic-gate 			verbose = B_TRUE;
13410Sstevel@tonic-gate 			break;
13420Sstevel@tonic-gate 		default:
13430Sstevel@tonic-gate 			sub_usage(SHELP_LIST, CMD_LIST);
13440Sstevel@tonic-gate 			return (Z_USAGE);
13450Sstevel@tonic-gate 		}
13460Sstevel@tonic-gate 	}
13470Sstevel@tonic-gate 	if (parsable && verbose) {
13480Sstevel@tonic-gate 		zerror(gettext("%s -p and -v are mutually exclusive."),
13490Sstevel@tonic-gate 		    cmd_to_str(CMD_LIST));
13500Sstevel@tonic-gate 		return (Z_ERR);
13510Sstevel@tonic-gate 	}
13520Sstevel@tonic-gate 	if (argc > optind) {
13530Sstevel@tonic-gate 		sub_usage(SHELP_LIST, CMD_LIST);
13540Sstevel@tonic-gate 		return (Z_USAGE);
13550Sstevel@tonic-gate 	}
13560Sstevel@tonic-gate 	if (zone_id != GLOBAL_ZONEID) {
13570Sstevel@tonic-gate 		fake_up_local_zone(zone_id, &zent);
13580Sstevel@tonic-gate 		/*
13590Sstevel@tonic-gate 		 * main() will issue a Z_NO_ZONE error if it cannot get an
13600Sstevel@tonic-gate 		 * id for target_zone, which in a non-global zone should
13610Sstevel@tonic-gate 		 * happen for any zone name except `zonename`.  Thus we
13620Sstevel@tonic-gate 		 * assert() that here but don't otherwise check.
13630Sstevel@tonic-gate 		 */
13640Sstevel@tonic-gate 		assert(strcmp(zent.zname, target_zone) == 0);
13650Sstevel@tonic-gate 		zone_print(&zent, verbose, parsable);
13660Sstevel@tonic-gate 		output = B_TRUE;
13670Sstevel@tonic-gate 	} else if ((zentp = lookup_running_zone(target_zone)) != NULL) {
13680Sstevel@tonic-gate 		zone_print(zentp, verbose, parsable);
13690Sstevel@tonic-gate 		output = B_TRUE;
1370*766Scarlsonj 	} else if (lookup_zone_info(target_zone, ZONE_ID_UNDEFINED,
1371*766Scarlsonj 	    &zent) == Z_OK) {
13720Sstevel@tonic-gate 		zone_print(&zent, verbose, parsable);
13730Sstevel@tonic-gate 		output = B_TRUE;
13740Sstevel@tonic-gate 	}
13750Sstevel@tonic-gate 	return (output ? Z_OK : Z_ERR);
13760Sstevel@tonic-gate }
13770Sstevel@tonic-gate 
13780Sstevel@tonic-gate static void
13790Sstevel@tonic-gate sigterm(int sig)
13800Sstevel@tonic-gate {
13810Sstevel@tonic-gate 	/*
13820Sstevel@tonic-gate 	 * Ignore SIG{INT,TERM}, so we don't end up in an infinite loop,
13830Sstevel@tonic-gate 	 * then propagate the signal to our process group.
13840Sstevel@tonic-gate 	 */
13850Sstevel@tonic-gate 	(void) sigset(SIGINT, SIG_IGN);
13860Sstevel@tonic-gate 	(void) sigset(SIGTERM, SIG_IGN);
13870Sstevel@tonic-gate 	(void) kill(0, sig);
13880Sstevel@tonic-gate 	child_killed = B_TRUE;
13890Sstevel@tonic-gate }
13900Sstevel@tonic-gate 
13910Sstevel@tonic-gate static int
13920Sstevel@tonic-gate do_subproc(char *cmdbuf)
13930Sstevel@tonic-gate {
13940Sstevel@tonic-gate 	char inbuf[1024];	/* arbitrary large amount */
13950Sstevel@tonic-gate 	FILE *file;
13960Sstevel@tonic-gate 
13970Sstevel@tonic-gate 	child_killed = B_FALSE;
13980Sstevel@tonic-gate 	/*
13990Sstevel@tonic-gate 	 * We use popen(3c) to launch child processes for [un]install;
14000Sstevel@tonic-gate 	 * this library call does not return a PID, so we have to kill
14010Sstevel@tonic-gate 	 * the whole process group.  To avoid killing our parent, we
14020Sstevel@tonic-gate 	 * become a process group leader here.  But doing so can wreak
14030Sstevel@tonic-gate 	 * havoc with reading from stdin when launched by a non-job-control
14040Sstevel@tonic-gate 	 * shell, so we close stdin and reopen it as /dev/null first.
14050Sstevel@tonic-gate 	 */
14060Sstevel@tonic-gate 	(void) close(STDIN_FILENO);
14070Sstevel@tonic-gate 	(void) open("/dev/null", O_RDONLY);
14080Sstevel@tonic-gate 	(void) setpgid(0, 0);
14090Sstevel@tonic-gate 	(void) sigset(SIGINT, sigterm);
14100Sstevel@tonic-gate 	(void) sigset(SIGTERM, sigterm);
14110Sstevel@tonic-gate 	file = popen(cmdbuf, "r");
14120Sstevel@tonic-gate 	for (;;) {
14130Sstevel@tonic-gate 		if (child_killed || fgets(inbuf, sizeof (inbuf), file) == NULL)
14140Sstevel@tonic-gate 			break;
14150Sstevel@tonic-gate 		(void) fputs(inbuf, stdout);
14160Sstevel@tonic-gate 	}
14170Sstevel@tonic-gate 	(void) sigset(SIGINT, SIG_DFL);
14180Sstevel@tonic-gate 	(void) sigset(SIGTERM, SIG_DFL);
14190Sstevel@tonic-gate 	return (pclose(file));
14200Sstevel@tonic-gate }
14210Sstevel@tonic-gate 
14220Sstevel@tonic-gate static int
14230Sstevel@tonic-gate subproc_status(const char *cmd, int status)
14240Sstevel@tonic-gate {
14250Sstevel@tonic-gate 	if (WIFEXITED(status)) {
14260Sstevel@tonic-gate 		int exit_code = WEXITSTATUS(status);
14270Sstevel@tonic-gate 
14280Sstevel@tonic-gate 		if (exit_code == 0)
14290Sstevel@tonic-gate 			return (Z_OK);
14300Sstevel@tonic-gate 		zerror(gettext("'%s' failed with exit code %d."), cmd,
14310Sstevel@tonic-gate 		    exit_code);
14320Sstevel@tonic-gate 	} else if (WIFSIGNALED(status)) {
14330Sstevel@tonic-gate 		int signal = WTERMSIG(status);
14340Sstevel@tonic-gate 		char sigstr[SIG2STR_MAX];
14350Sstevel@tonic-gate 
14360Sstevel@tonic-gate 		if (sig2str(signal, sigstr) == 0) {
14370Sstevel@tonic-gate 			zerror(gettext("'%s' terminated by signal SIG%s."), cmd,
14380Sstevel@tonic-gate 			    sigstr);
14390Sstevel@tonic-gate 		} else {
14400Sstevel@tonic-gate 			zerror(gettext("'%s' terminated by an unknown signal."),
14410Sstevel@tonic-gate 			    cmd);
14420Sstevel@tonic-gate 		}
14430Sstevel@tonic-gate 	} else {
14440Sstevel@tonic-gate 		zerror(gettext("'%s' failed for unknown reasons."), cmd);
14450Sstevel@tonic-gate 	}
14460Sstevel@tonic-gate 	return (Z_ERR);
14470Sstevel@tonic-gate }
14480Sstevel@tonic-gate 
14490Sstevel@tonic-gate /*
14500Sstevel@tonic-gate  * Various sanity checks; make sure:
14510Sstevel@tonic-gate  * 1. We're in the global zone.
14520Sstevel@tonic-gate  * 2. The calling user has sufficient privilege.
14530Sstevel@tonic-gate  * 3. The target zone is neither the global zone nor anything starting with
14540Sstevel@tonic-gate  *    "SUNW".
14550Sstevel@tonic-gate  * 4a. If we're looking for a 'not running' (i.e., configured or installed)
14560Sstevel@tonic-gate  *     zone, the name service knows about it.
14570Sstevel@tonic-gate  * 4b. For some operations which expect a zone not to be running, that it is
14580Sstevel@tonic-gate  *     not already running (or ready).
14590Sstevel@tonic-gate  */
14600Sstevel@tonic-gate static int
14610Sstevel@tonic-gate sanity_check(char *zone, int cmd_num, boolean_t running,
14620Sstevel@tonic-gate     boolean_t unsafe_when_running)
14630Sstevel@tonic-gate {
14640Sstevel@tonic-gate 	zone_entry_t *zent;
14650Sstevel@tonic-gate 	priv_set_t *privset;
14660Sstevel@tonic-gate 	zone_state_t state;
1467*766Scarlsonj 	char kernzone[ZONENAME_MAX];
1468*766Scarlsonj 	FILE *fp;
14690Sstevel@tonic-gate 
14700Sstevel@tonic-gate 	if (getzoneid() != GLOBAL_ZONEID) {
14710Sstevel@tonic-gate 		zerror(gettext("must be in the global zone to %s a zone."),
14720Sstevel@tonic-gate 		    cmd_to_str(cmd_num));
14730Sstevel@tonic-gate 		return (Z_ERR);
14740Sstevel@tonic-gate 	}
14750Sstevel@tonic-gate 
14760Sstevel@tonic-gate 	if ((privset = priv_allocset()) == NULL) {
14770Sstevel@tonic-gate 		zerror(gettext("%s failed"), "priv_allocset");
14780Sstevel@tonic-gate 		return (Z_ERR);
14790Sstevel@tonic-gate 	}
14800Sstevel@tonic-gate 
14810Sstevel@tonic-gate 	if (getppriv(PRIV_EFFECTIVE, privset) != 0) {
14820Sstevel@tonic-gate 		zerror(gettext("%s failed"), "getppriv");
14830Sstevel@tonic-gate 		priv_freeset(privset);
14840Sstevel@tonic-gate 		return (Z_ERR);
14850Sstevel@tonic-gate 	}
14860Sstevel@tonic-gate 
14870Sstevel@tonic-gate 	if (priv_isfullset(privset) == B_FALSE) {
14880Sstevel@tonic-gate 		zerror(gettext("only a privileged user may %s a zone."),
14890Sstevel@tonic-gate 		    cmd_to_str(cmd_num));
14900Sstevel@tonic-gate 		priv_freeset(privset);
14910Sstevel@tonic-gate 		return (Z_ERR);
14920Sstevel@tonic-gate 	}
14930Sstevel@tonic-gate 	priv_freeset(privset);
14940Sstevel@tonic-gate 
14950Sstevel@tonic-gate 	if (zone == NULL) {
14960Sstevel@tonic-gate 		zerror(gettext("no zone specified"));
14970Sstevel@tonic-gate 		return (Z_ERR);
14980Sstevel@tonic-gate 	}
14990Sstevel@tonic-gate 
15000Sstevel@tonic-gate 	if (strcmp(zone, GLOBAL_ZONENAME) == 0) {
15010Sstevel@tonic-gate 		zerror(gettext("%s operation is invalid for the global zone."),
15020Sstevel@tonic-gate 		    cmd_to_str(cmd_num));
15030Sstevel@tonic-gate 		return (Z_ERR);
15040Sstevel@tonic-gate 	}
15050Sstevel@tonic-gate 
15060Sstevel@tonic-gate 	if (strncmp(zone, "SUNW", 4) == 0) {
15070Sstevel@tonic-gate 		zerror(gettext("%s operation is invalid for zones starting "
15080Sstevel@tonic-gate 		    "with SUNW."), cmd_to_str(cmd_num));
15090Sstevel@tonic-gate 		return (Z_ERR);
15100Sstevel@tonic-gate 	}
15110Sstevel@tonic-gate 
1512*766Scarlsonj 	if (!zonecfg_in_alt_root()) {
1513*766Scarlsonj 		zent = lookup_running_zone(zone);
1514*766Scarlsonj 	} else if ((fp = zonecfg_open_scratch("", B_FALSE)) == NULL) {
1515*766Scarlsonj 		zent = NULL;
1516*766Scarlsonj 	} else {
1517*766Scarlsonj 		if (zonecfg_find_scratch(fp, zone, zonecfg_get_root(),
1518*766Scarlsonj 		    kernzone, sizeof (kernzone)) == 0)
1519*766Scarlsonj 			zent = lookup_running_zone(kernzone);
1520*766Scarlsonj 		else
1521*766Scarlsonj 			zent = NULL;
1522*766Scarlsonj 		zonecfg_close_scratch(fp);
1523*766Scarlsonj 	}
1524*766Scarlsonj 
15250Sstevel@tonic-gate 	/*
15260Sstevel@tonic-gate 	 * Look up from the kernel for 'running' zones.
15270Sstevel@tonic-gate 	 */
15280Sstevel@tonic-gate 	if (running) {
15290Sstevel@tonic-gate 		if (zent == NULL) {
15300Sstevel@tonic-gate 			zerror(gettext("not running"));
15310Sstevel@tonic-gate 			return (Z_ERR);
15320Sstevel@tonic-gate 		}
15330Sstevel@tonic-gate 	} else {
15340Sstevel@tonic-gate 		int err;
15350Sstevel@tonic-gate 
15360Sstevel@tonic-gate 		if (unsafe_when_running && zent != NULL) {
15370Sstevel@tonic-gate 			/* check whether the zone is ready or running */
15380Sstevel@tonic-gate 			if ((err = zone_get_state(zent->zname,
15390Sstevel@tonic-gate 			    &zent->zstate_num)) != Z_OK) {
15400Sstevel@tonic-gate 				errno = err;
15410Sstevel@tonic-gate 				zperror2(zent->zname,
15420Sstevel@tonic-gate 				    gettext("could not get state"));
15430Sstevel@tonic-gate 				/* can't tell, so hedge */
15440Sstevel@tonic-gate 				zent->zstate_str = "ready/running";
15450Sstevel@tonic-gate 			} else {
15460Sstevel@tonic-gate 				zent->zstate_str =
15470Sstevel@tonic-gate 				    zone_state_str(zent->zstate_num);
15480Sstevel@tonic-gate 			}
15490Sstevel@tonic-gate 			zerror(gettext("%s operation is invalid for %s zones."),
15500Sstevel@tonic-gate 			    cmd_to_str(cmd_num), zent->zstate_str);
15510Sstevel@tonic-gate 			return (Z_ERR);
15520Sstevel@tonic-gate 		}
15530Sstevel@tonic-gate 		if ((err = zone_get_state(zone, &state)) != Z_OK) {
15540Sstevel@tonic-gate 			errno = err;
15550Sstevel@tonic-gate 			zperror2(zone, gettext("could not get state"));
15560Sstevel@tonic-gate 			return (Z_ERR);
15570Sstevel@tonic-gate 		}
15580Sstevel@tonic-gate 		switch (cmd_num) {
15590Sstevel@tonic-gate 		case CMD_UNINSTALL:
15600Sstevel@tonic-gate 			if (state == ZONE_STATE_CONFIGURED) {
15610Sstevel@tonic-gate 				zerror(gettext("is already in state '%s'."),
15620Sstevel@tonic-gate 				    zone_state_str(ZONE_STATE_CONFIGURED));
15630Sstevel@tonic-gate 				return (Z_ERR);
15640Sstevel@tonic-gate 			}
15650Sstevel@tonic-gate 			break;
15660Sstevel@tonic-gate 		case CMD_INSTALL:
15670Sstevel@tonic-gate 			if (state == ZONE_STATE_INSTALLED) {
15680Sstevel@tonic-gate 				zerror(gettext("is already %s."),
15690Sstevel@tonic-gate 				    zone_state_str(ZONE_STATE_INSTALLED));
15700Sstevel@tonic-gate 				return (Z_ERR);
15710Sstevel@tonic-gate 			} else if (state == ZONE_STATE_INCOMPLETE) {
15720Sstevel@tonic-gate 				zerror(gettext("zone is %s; %s required."),
15730Sstevel@tonic-gate 				    zone_state_str(ZONE_STATE_INCOMPLETE),
15740Sstevel@tonic-gate 				    cmd_to_str(CMD_UNINSTALL));
15750Sstevel@tonic-gate 				return (Z_ERR);
15760Sstevel@tonic-gate 			}
15770Sstevel@tonic-gate 			break;
15780Sstevel@tonic-gate 		case CMD_READY:
15790Sstevel@tonic-gate 		case CMD_BOOT:
1580*766Scarlsonj 		case CMD_MOUNT:
15810Sstevel@tonic-gate 			if (state < ZONE_STATE_INSTALLED) {
15820Sstevel@tonic-gate 				zerror(gettext("must be %s before %s."),
15830Sstevel@tonic-gate 				    zone_state_str(ZONE_STATE_INSTALLED),
15840Sstevel@tonic-gate 				    cmd_to_str(cmd_num));
15850Sstevel@tonic-gate 				return (Z_ERR);
15860Sstevel@tonic-gate 			}
15870Sstevel@tonic-gate 			break;
15880Sstevel@tonic-gate 		case CMD_VERIFY:
15890Sstevel@tonic-gate 			if (state == ZONE_STATE_INCOMPLETE) {
15900Sstevel@tonic-gate 				zerror(gettext("zone is %s; %s required."),
15910Sstevel@tonic-gate 				    zone_state_str(ZONE_STATE_INCOMPLETE),
15920Sstevel@tonic-gate 				    cmd_to_str(CMD_UNINSTALL));
15930Sstevel@tonic-gate 				return (Z_ERR);
15940Sstevel@tonic-gate 			}
15950Sstevel@tonic-gate 			break;
1596*766Scarlsonj 		case CMD_UNMOUNT:
1597*766Scarlsonj 			if (state != ZONE_STATE_MOUNTED) {
1598*766Scarlsonj 				zerror(gettext("must be %s before %s."),
1599*766Scarlsonj 				    zone_state_str(ZONE_STATE_MOUNTED),
1600*766Scarlsonj 				    cmd_to_str(cmd_num));
1601*766Scarlsonj 				return (Z_ERR);
1602*766Scarlsonj 			}
1603*766Scarlsonj 			break;
16040Sstevel@tonic-gate 		}
16050Sstevel@tonic-gate 	}
16060Sstevel@tonic-gate 	return (Z_OK);
16070Sstevel@tonic-gate }
16080Sstevel@tonic-gate 
16090Sstevel@tonic-gate static int
16100Sstevel@tonic-gate halt_func(int argc, char *argv[])
16110Sstevel@tonic-gate {
16120Sstevel@tonic-gate 	zone_cmd_arg_t zarg;
16130Sstevel@tonic-gate 	int arg;
16140Sstevel@tonic-gate 
1615*766Scarlsonj 	if (zonecfg_in_alt_root()) {
1616*766Scarlsonj 		zerror(gettext("cannot halt zone in alternate root"));
1617*766Scarlsonj 		return (Z_ERR);
1618*766Scarlsonj 	}
1619*766Scarlsonj 
16200Sstevel@tonic-gate 	optind = 0;
16210Sstevel@tonic-gate 	if ((arg = getopt(argc, argv, "?")) != EOF) {
16220Sstevel@tonic-gate 		switch (arg) {
16230Sstevel@tonic-gate 		case '?':
16240Sstevel@tonic-gate 			sub_usage(SHELP_HALT, CMD_HALT);
16250Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
16260Sstevel@tonic-gate 		default:
16270Sstevel@tonic-gate 			sub_usage(SHELP_HALT, CMD_HALT);
16280Sstevel@tonic-gate 			return (Z_USAGE);
16290Sstevel@tonic-gate 		}
16300Sstevel@tonic-gate 	}
16310Sstevel@tonic-gate 	if (argc > optind) {
16320Sstevel@tonic-gate 		sub_usage(SHELP_HALT, CMD_HALT);
16330Sstevel@tonic-gate 		return (Z_USAGE);
16340Sstevel@tonic-gate 	}
16350Sstevel@tonic-gate 	/*
16360Sstevel@tonic-gate 	 * zoneadmd should be the one to decide whether or not to proceed,
16370Sstevel@tonic-gate 	 * so even though it seems that the fourth parameter below should
16380Sstevel@tonic-gate 	 * perhaps be B_TRUE, it really shouldn't be.
16390Sstevel@tonic-gate 	 */
16400Sstevel@tonic-gate 	if (sanity_check(target_zone, CMD_HALT, B_FALSE, B_FALSE) != Z_OK)
16410Sstevel@tonic-gate 		return (Z_ERR);
16420Sstevel@tonic-gate 
16430Sstevel@tonic-gate 	zarg.cmd = Z_HALT;
16440Sstevel@tonic-gate 	return ((call_zoneadmd(target_zone, &zarg) == 0) ? Z_OK : Z_ERR);
16450Sstevel@tonic-gate }
16460Sstevel@tonic-gate 
16470Sstevel@tonic-gate static int
16480Sstevel@tonic-gate reboot_func(int argc, char *argv[])
16490Sstevel@tonic-gate {
16500Sstevel@tonic-gate 	zone_cmd_arg_t zarg;
16510Sstevel@tonic-gate 	int arg;
16520Sstevel@tonic-gate 
1653*766Scarlsonj 	if (zonecfg_in_alt_root()) {
1654*766Scarlsonj 		zerror(gettext("cannot reboot zone in alternate root"));
1655*766Scarlsonj 		return (Z_ERR);
1656*766Scarlsonj 	}
1657*766Scarlsonj 
16580Sstevel@tonic-gate 	optind = 0;
16590Sstevel@tonic-gate 	if ((arg = getopt(argc, argv, "?")) != EOF) {
16600Sstevel@tonic-gate 		switch (arg) {
16610Sstevel@tonic-gate 		case '?':
16620Sstevel@tonic-gate 			sub_usage(SHELP_REBOOT, CMD_REBOOT);
16630Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
16640Sstevel@tonic-gate 		default:
16650Sstevel@tonic-gate 			sub_usage(SHELP_REBOOT, CMD_REBOOT);
16660Sstevel@tonic-gate 			return (Z_USAGE);
16670Sstevel@tonic-gate 		}
16680Sstevel@tonic-gate 	}
16690Sstevel@tonic-gate 	if (argc > 0) {
16700Sstevel@tonic-gate 		sub_usage(SHELP_REBOOT, CMD_REBOOT);
16710Sstevel@tonic-gate 		return (Z_USAGE);
16720Sstevel@tonic-gate 	}
16730Sstevel@tonic-gate 	/*
16740Sstevel@tonic-gate 	 * zoneadmd should be the one to decide whether or not to proceed,
16750Sstevel@tonic-gate 	 * so even though it seems that the fourth parameter below should
16760Sstevel@tonic-gate 	 * perhaps be B_TRUE, it really shouldn't be.
16770Sstevel@tonic-gate 	 */
16780Sstevel@tonic-gate 	if (sanity_check(target_zone, CMD_REBOOT, B_TRUE, B_FALSE) != Z_OK)
16790Sstevel@tonic-gate 		return (Z_ERR);
16800Sstevel@tonic-gate 
16810Sstevel@tonic-gate 	zarg.cmd = Z_REBOOT;
16820Sstevel@tonic-gate 	return ((call_zoneadmd(target_zone, &zarg) == 0) ? Z_OK : Z_ERR);
16830Sstevel@tonic-gate }
16840Sstevel@tonic-gate 
16850Sstevel@tonic-gate static int
16860Sstevel@tonic-gate verify_rctls(zone_dochandle_t handle)
16870Sstevel@tonic-gate {
16880Sstevel@tonic-gate 	struct zone_rctltab rctltab;
16890Sstevel@tonic-gate 	size_t rbs = rctlblk_size();
16900Sstevel@tonic-gate 	rctlblk_t *rctlblk;
16910Sstevel@tonic-gate 	int error = Z_INVAL;
16920Sstevel@tonic-gate 
16930Sstevel@tonic-gate 	if ((rctlblk = malloc(rbs)) == NULL) {
16940Sstevel@tonic-gate 		zerror(gettext("failed to allocate %lu bytes: %s"), rbs,
16950Sstevel@tonic-gate 		    strerror(errno));
16960Sstevel@tonic-gate 		return (Z_NOMEM);
16970Sstevel@tonic-gate 	}
16980Sstevel@tonic-gate 
16990Sstevel@tonic-gate 	if (zonecfg_setrctlent(handle) != Z_OK) {
17000Sstevel@tonic-gate 		zerror(gettext("zonecfg_setrctlent failed"));
17010Sstevel@tonic-gate 		free(rctlblk);
17020Sstevel@tonic-gate 		return (error);
17030Sstevel@tonic-gate 	}
17040Sstevel@tonic-gate 
17050Sstevel@tonic-gate 	rctltab.zone_rctl_valptr = NULL;
17060Sstevel@tonic-gate 	while (zonecfg_getrctlent(handle, &rctltab) == Z_OK) {
17070Sstevel@tonic-gate 		struct zone_rctlvaltab *rctlval;
17080Sstevel@tonic-gate 		const char *name = rctltab.zone_rctl_name;
17090Sstevel@tonic-gate 
17100Sstevel@tonic-gate 		if (!zonecfg_is_rctl(name)) {
17110Sstevel@tonic-gate 			zerror(gettext("WARNING: Ignoring unrecognized rctl "
17120Sstevel@tonic-gate 			    "'%s'."),  name);
17130Sstevel@tonic-gate 			zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
17140Sstevel@tonic-gate 			rctltab.zone_rctl_valptr = NULL;
17150Sstevel@tonic-gate 			continue;
17160Sstevel@tonic-gate 		}
17170Sstevel@tonic-gate 
17180Sstevel@tonic-gate 		for (rctlval = rctltab.zone_rctl_valptr; rctlval != NULL;
17190Sstevel@tonic-gate 		    rctlval = rctlval->zone_rctlval_next) {
17200Sstevel@tonic-gate 			if (zonecfg_construct_rctlblk(rctlval, rctlblk)
17210Sstevel@tonic-gate 			    != Z_OK) {
17220Sstevel@tonic-gate 				zerror(gettext("invalid rctl value: "
17230Sstevel@tonic-gate 				    "(priv=%s,limit=%s,action%s)"),
17240Sstevel@tonic-gate 				    rctlval->zone_rctlval_priv,
17250Sstevel@tonic-gate 				    rctlval->zone_rctlval_limit,
17260Sstevel@tonic-gate 				    rctlval->zone_rctlval_action);
17270Sstevel@tonic-gate 				goto out;
17280Sstevel@tonic-gate 			}
17290Sstevel@tonic-gate 			if (!zonecfg_valid_rctl(name, rctlblk)) {
17300Sstevel@tonic-gate 				zerror(gettext("(priv=%s,limit=%s,action=%s) "
17310Sstevel@tonic-gate 				    "is not a valid value for rctl '%s'"),
17320Sstevel@tonic-gate 				    rctlval->zone_rctlval_priv,
17330Sstevel@tonic-gate 				    rctlval->zone_rctlval_limit,
17340Sstevel@tonic-gate 				    rctlval->zone_rctlval_action,
17350Sstevel@tonic-gate 				    name);
17360Sstevel@tonic-gate 				goto out;
17370Sstevel@tonic-gate 			}
17380Sstevel@tonic-gate 		}
17390Sstevel@tonic-gate 		zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
17400Sstevel@tonic-gate 	}
17410Sstevel@tonic-gate 	rctltab.zone_rctl_valptr = NULL;
17420Sstevel@tonic-gate 	error = Z_OK;
17430Sstevel@tonic-gate out:
17440Sstevel@tonic-gate 	zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
17450Sstevel@tonic-gate 	(void) zonecfg_endrctlent(handle);
17460Sstevel@tonic-gate 	free(rctlblk);
17470Sstevel@tonic-gate 	return (error);
17480Sstevel@tonic-gate }
17490Sstevel@tonic-gate 
17500Sstevel@tonic-gate static int
17510Sstevel@tonic-gate verify_pool(zone_dochandle_t handle)
17520Sstevel@tonic-gate {
17530Sstevel@tonic-gate 	char poolname[MAXPATHLEN];
17540Sstevel@tonic-gate 	pool_conf_t *poolconf;
17550Sstevel@tonic-gate 	pool_t *pool;
17560Sstevel@tonic-gate 	int status;
17570Sstevel@tonic-gate 	int error;
17580Sstevel@tonic-gate 
17590Sstevel@tonic-gate 	/*
17600Sstevel@tonic-gate 	 * This ends up being very similar to the check done in zoneadmd.
17610Sstevel@tonic-gate 	 */
17620Sstevel@tonic-gate 	error = zonecfg_get_pool(handle, poolname, sizeof (poolname));
17630Sstevel@tonic-gate 	if (error == Z_NO_ENTRY || (error == Z_OK && strlen(poolname) == 0)) {
17640Sstevel@tonic-gate 		/*
17650Sstevel@tonic-gate 		 * No pool specified.
17660Sstevel@tonic-gate 		 */
17670Sstevel@tonic-gate 		return (0);
17680Sstevel@tonic-gate 	}
17690Sstevel@tonic-gate 	if (error != Z_OK) {
17700Sstevel@tonic-gate 		zperror(gettext("Unable to retrieve pool name from "
17710Sstevel@tonic-gate 		    "configuration"), B_TRUE);
17720Sstevel@tonic-gate 		return (error);
17730Sstevel@tonic-gate 	}
17740Sstevel@tonic-gate 	/*
17750Sstevel@tonic-gate 	 * Don't do anything if pools aren't enabled.
17760Sstevel@tonic-gate 	 */
17770Sstevel@tonic-gate 	if (pool_get_status(&status) != PO_SUCCESS || status != POOL_ENABLED) {
17780Sstevel@tonic-gate 		zerror(gettext("WARNING: pools facility not active; "
17790Sstevel@tonic-gate 		    "zone will not be bound to pool '%s'."), poolname);
17800Sstevel@tonic-gate 		return (Z_OK);
17810Sstevel@tonic-gate 	}
17820Sstevel@tonic-gate 	/*
17830Sstevel@tonic-gate 	 * Try to provide a sane error message if the requested pool doesn't
17840Sstevel@tonic-gate 	 * exist.  It isn't clear that pools-related failures should
17850Sstevel@tonic-gate 	 * necessarily translate to a failure to verify the zone configuration,
17860Sstevel@tonic-gate 	 * hence they are not considered errors.
17870Sstevel@tonic-gate 	 */
17880Sstevel@tonic-gate 	if ((poolconf = pool_conf_alloc()) == NULL) {
17890Sstevel@tonic-gate 		zerror(gettext("WARNING: pool_conf_alloc failed; "
17900Sstevel@tonic-gate 		    "using default pool"));
17910Sstevel@tonic-gate 		return (Z_OK);
17920Sstevel@tonic-gate 	}
17930Sstevel@tonic-gate 	if (pool_conf_open(poolconf, pool_dynamic_location(), PO_RDONLY) !=
17940Sstevel@tonic-gate 	    PO_SUCCESS) {
17950Sstevel@tonic-gate 		zerror(gettext("WARNING: pool_conf_open failed; "
17960Sstevel@tonic-gate 		    "using default pool"));
17970Sstevel@tonic-gate 		pool_conf_free(poolconf);
17980Sstevel@tonic-gate 		return (Z_OK);
17990Sstevel@tonic-gate 	}
18000Sstevel@tonic-gate 	pool = pool_get_pool(poolconf, poolname);
18010Sstevel@tonic-gate 	(void) pool_conf_close(poolconf);
18020Sstevel@tonic-gate 	pool_conf_free(poolconf);
18030Sstevel@tonic-gate 	if (pool == NULL) {
18040Sstevel@tonic-gate 		zerror(gettext("WARNING: pool '%s' not found. "
18050Sstevel@tonic-gate 		    "using default pool"), poolname);
18060Sstevel@tonic-gate 	}
18070Sstevel@tonic-gate 
18080Sstevel@tonic-gate 	return (Z_OK);
18090Sstevel@tonic-gate }
18100Sstevel@tonic-gate 
18110Sstevel@tonic-gate static int
18120Sstevel@tonic-gate verify_filesystems(zone_dochandle_t handle)
18130Sstevel@tonic-gate {
18140Sstevel@tonic-gate 	int return_code = Z_OK;
18150Sstevel@tonic-gate 	struct zone_fstab fstab;
18160Sstevel@tonic-gate 	char cmdbuf[MAXPATHLEN];
18170Sstevel@tonic-gate 	struct stat st;
18180Sstevel@tonic-gate 
18190Sstevel@tonic-gate 	/*
18200Sstevel@tonic-gate 	 * No need to verify inherit-pkg-dir fs types, as their type is
18210Sstevel@tonic-gate 	 * implicitly lofs, which is known.  Therefore, the types are only
18220Sstevel@tonic-gate 	 * verified for regular filesystems below.
18230Sstevel@tonic-gate 	 *
18240Sstevel@tonic-gate 	 * Since the actual mount point is not known until the dependent mounts
18250Sstevel@tonic-gate 	 * are performed, we don't attempt any path validation here: that will
18260Sstevel@tonic-gate 	 * happen later when zoneadmd actually does the mounts.
18270Sstevel@tonic-gate 	 */
18280Sstevel@tonic-gate 	if (zonecfg_setfsent(handle) != Z_OK) {
18290Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("cannot verify file-systems: "
18300Sstevel@tonic-gate 		    "unable to enumerate mounts\n"));
18310Sstevel@tonic-gate 		return (Z_ERR);
18320Sstevel@tonic-gate 	}
18330Sstevel@tonic-gate 	while (zonecfg_getfsent(handle, &fstab) == Z_OK) {
18340Sstevel@tonic-gate 		if (!zonecfg_valid_fs_type(fstab.zone_fs_type)) {
18350Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("cannot verify fs %s: "
18360Sstevel@tonic-gate 			    "type %s is not allowed.\n"), fstab.zone_fs_dir,
18370Sstevel@tonic-gate 			    fstab.zone_fs_type);
18380Sstevel@tonic-gate 			return_code = Z_ERR;
18390Sstevel@tonic-gate 			goto next_fs;
18400Sstevel@tonic-gate 		}
18410Sstevel@tonic-gate 		/*
18420Sstevel@tonic-gate 		 * Verify /usr/lib/fs/<fstype>/mount exists.
18430Sstevel@tonic-gate 		 */
18440Sstevel@tonic-gate 		if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/mount",
18450Sstevel@tonic-gate 		    fstab.zone_fs_type) > sizeof (cmdbuf)) {
18460Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("cannot verify fs %s: "
18470Sstevel@tonic-gate 			    "type %s is too long.\n"), fstab.zone_fs_dir,
18480Sstevel@tonic-gate 			    fstab.zone_fs_type);
18490Sstevel@tonic-gate 			return_code = Z_ERR;
18500Sstevel@tonic-gate 			goto next_fs;
18510Sstevel@tonic-gate 		}
18520Sstevel@tonic-gate 		if (stat(cmdbuf, &st) != 0) {
18530Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("cannot verify fs %s: "
18540Sstevel@tonic-gate 			    "can't access %s: %s\n"), fstab.zone_fs_dir,
18550Sstevel@tonic-gate 			    cmdbuf, strerror(errno));
18560Sstevel@tonic-gate 			return_code = Z_ERR;
18570Sstevel@tonic-gate 			goto next_fs;
18580Sstevel@tonic-gate 		}
18590Sstevel@tonic-gate 		if (!S_ISREG(st.st_mode)) {
18600Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("cannot verify fs %s: "
18610Sstevel@tonic-gate 			    "%s is not a regular file\n"), fstab.zone_fs_dir,
18620Sstevel@tonic-gate 			    cmdbuf);
18630Sstevel@tonic-gate 			return_code = Z_ERR;
18640Sstevel@tonic-gate 			goto next_fs;
18650Sstevel@tonic-gate 		}
18660Sstevel@tonic-gate 		/*
18670Sstevel@tonic-gate 		 * Verify /usr/lib/fs/<fstype>/fsck exists iff zone_fs_raw is
18680Sstevel@tonic-gate 		 * set.
18690Sstevel@tonic-gate 		 */
18700Sstevel@tonic-gate 		if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/fsck",
18710Sstevel@tonic-gate 		    fstab.zone_fs_type) > sizeof (cmdbuf)) {
18720Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("cannot verify fs %s: "
18730Sstevel@tonic-gate 			    "type %s is too long.\n"), fstab.zone_fs_dir,
18740Sstevel@tonic-gate 			    fstab.zone_fs_type);
18750Sstevel@tonic-gate 			return_code = Z_ERR;
18760Sstevel@tonic-gate 			goto next_fs;
18770Sstevel@tonic-gate 		}
18780Sstevel@tonic-gate 		if (fstab.zone_fs_raw[0] == '\0' && stat(cmdbuf, &st) == 0) {
18790Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("cannot verify fs %s: "
18800Sstevel@tonic-gate 			    "must specify 'raw' device for %s file-systems\n"),
18810Sstevel@tonic-gate 			    fstab.zone_fs_dir, fstab.zone_fs_type);
18820Sstevel@tonic-gate 			return_code = Z_ERR;
18830Sstevel@tonic-gate 			goto next_fs;
18840Sstevel@tonic-gate 		}
18850Sstevel@tonic-gate 		if (fstab.zone_fs_raw[0] != '\0' &&
18860Sstevel@tonic-gate 		    (stat(cmdbuf, &st) != 0 || !S_ISREG(st.st_mode))) {
18870Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("cannot verify fs %s: "
18880Sstevel@tonic-gate 			    "'raw' device specified but "
18890Sstevel@tonic-gate 			    "no fsck executable exists for %s\n"),
18900Sstevel@tonic-gate 			    fstab.zone_fs_dir, fstab.zone_fs_type);
18910Sstevel@tonic-gate 			return_code = Z_ERR;
18920Sstevel@tonic-gate 			goto next_fs;
18930Sstevel@tonic-gate 		}
18940Sstevel@tonic-gate next_fs:
18950Sstevel@tonic-gate 		zonecfg_free_fs_option_list(fstab.zone_fs_options);
18960Sstevel@tonic-gate 	}
18970Sstevel@tonic-gate 	(void) zonecfg_endfsent(handle);
18980Sstevel@tonic-gate 
18990Sstevel@tonic-gate 	return (return_code);
19000Sstevel@tonic-gate }
19010Sstevel@tonic-gate 
19020Sstevel@tonic-gate static int
19030Sstevel@tonic-gate verify_details(int cmd_num)
19040Sstevel@tonic-gate {
19050Sstevel@tonic-gate 	zone_dochandle_t handle;
19060Sstevel@tonic-gate 	struct zone_nwiftab nwiftab;
19070Sstevel@tonic-gate 	char zonepath[MAXPATHLEN], checkpath[MAXPATHLEN];
19080Sstevel@tonic-gate 	int return_code = Z_OK;
19090Sstevel@tonic-gate 	int err;
1910*766Scarlsonj 	boolean_t in_alt_root;
19110Sstevel@tonic-gate 
19120Sstevel@tonic-gate 	if ((handle = zonecfg_init_handle()) == NULL) {
19130Sstevel@tonic-gate 		zperror(cmd_to_str(cmd_num), B_TRUE);
19140Sstevel@tonic-gate 		return (Z_ERR);
19150Sstevel@tonic-gate 	}
19160Sstevel@tonic-gate 	if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) {
19170Sstevel@tonic-gate 		errno = err;
19180Sstevel@tonic-gate 		zperror(cmd_to_str(cmd_num), B_TRUE);
19190Sstevel@tonic-gate 		zonecfg_fini_handle(handle);
19200Sstevel@tonic-gate 		return (Z_ERR);
19210Sstevel@tonic-gate 	}
19220Sstevel@tonic-gate 	if ((err = zonecfg_get_zonepath(handle, zonepath, sizeof (zonepath))) !=
19230Sstevel@tonic-gate 	    Z_OK) {
19240Sstevel@tonic-gate 		errno = err;
19250Sstevel@tonic-gate 		zperror(cmd_to_str(cmd_num), B_TRUE);
19260Sstevel@tonic-gate 		zonecfg_fini_handle(handle);
19270Sstevel@tonic-gate 		return (Z_ERR);
19280Sstevel@tonic-gate 	}
19290Sstevel@tonic-gate 	/*
19300Sstevel@tonic-gate 	 * zonecfg_get_zonepath() gets its data from the XML repository.
19310Sstevel@tonic-gate 	 * Verify this against the index file, which is checked first by
19320Sstevel@tonic-gate 	 * zone_get_zonepath().  If they don't match, bail out.
19330Sstevel@tonic-gate 	 */
19340Sstevel@tonic-gate 	if ((err = zone_get_zonepath(target_zone, checkpath,
19350Sstevel@tonic-gate 	    sizeof (checkpath))) != Z_OK) {
19360Sstevel@tonic-gate 		errno = err;
19370Sstevel@tonic-gate 		zperror2(target_zone, gettext("could not get zone path"));
19380Sstevel@tonic-gate 		return (Z_ERR);
19390Sstevel@tonic-gate 	}
19400Sstevel@tonic-gate 	if (strcmp(zonepath, checkpath) != 0) {
19410Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("The XML repository has "
19420Sstevel@tonic-gate 		    "zonepath '%s',\nbut the index file has zonepath '%s'.\n"
19430Sstevel@tonic-gate 		    "These must match, so fix the incorrect entry.\n"),
19440Sstevel@tonic-gate 		    zonepath, checkpath);
19450Sstevel@tonic-gate 		return (Z_ERR);
19460Sstevel@tonic-gate 	}
19470Sstevel@tonic-gate 	if (validate_zonepath(zonepath, cmd_num) != Z_OK) {
19480Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("could not verify zonepath %s "
19490Sstevel@tonic-gate 		    "because of the above errors.\n"), zonepath);
19500Sstevel@tonic-gate 		return_code = Z_ERR;
19510Sstevel@tonic-gate 	}
19520Sstevel@tonic-gate 
1953*766Scarlsonj 	in_alt_root = zonecfg_in_alt_root();
1954*766Scarlsonj 	if (in_alt_root)
1955*766Scarlsonj 		goto no_net;
1956*766Scarlsonj 
19570Sstevel@tonic-gate 	if ((err = zonecfg_setnwifent(handle)) != Z_OK) {
19580Sstevel@tonic-gate 		errno = err;
19590Sstevel@tonic-gate 		zperror(cmd_to_str(cmd_num), B_TRUE);
19600Sstevel@tonic-gate 		zonecfg_fini_handle(handle);
19610Sstevel@tonic-gate 		return (Z_ERR);
19620Sstevel@tonic-gate 	}
19630Sstevel@tonic-gate 	while (zonecfg_getnwifent(handle, &nwiftab) == Z_OK) {
19640Sstevel@tonic-gate 		struct lifreq lifr;
19650Sstevel@tonic-gate 		sa_family_t af;
19660Sstevel@tonic-gate 		int so, res;
19670Sstevel@tonic-gate 
19680Sstevel@tonic-gate 		/* skip any loopback interfaces */
19690Sstevel@tonic-gate 		if (strcmp(nwiftab.zone_nwif_physical, "lo0") == 0)
19700Sstevel@tonic-gate 			continue;
19710Sstevel@tonic-gate 		if ((res = zonecfg_valid_net_address(nwiftab.zone_nwif_address,
19720Sstevel@tonic-gate 		    &lifr)) != Z_OK) {
19730Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("could not verify %s "
19740Sstevel@tonic-gate 			    "%s=%s %s=%s: %s\n"), "net", "address",
19750Sstevel@tonic-gate 			    nwiftab.zone_nwif_address, "physical",
19760Sstevel@tonic-gate 			    nwiftab.zone_nwif_physical, zonecfg_strerror(res));
19770Sstevel@tonic-gate 			return_code = Z_ERR;
19780Sstevel@tonic-gate 			continue;
19790Sstevel@tonic-gate 		}
19800Sstevel@tonic-gate 		af = lifr.lifr_addr.ss_family;
19810Sstevel@tonic-gate 		(void) memset(&lifr, 0, sizeof (lifr));
19820Sstevel@tonic-gate 		(void) strlcpy(lifr.lifr_name, nwiftab.zone_nwif_physical,
19830Sstevel@tonic-gate 		    sizeof (lifr.lifr_name));
19840Sstevel@tonic-gate 		lifr.lifr_addr.ss_family = af;
19850Sstevel@tonic-gate 		if ((so = socket(af, SOCK_DGRAM, 0)) < 0) {
19860Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("could not verify %s "
19870Sstevel@tonic-gate 			    "%s=%s %s=%s: could not get socket: %s\n"), "net",
19880Sstevel@tonic-gate 			    "address", nwiftab.zone_nwif_address, "physical",
19890Sstevel@tonic-gate 			    nwiftab.zone_nwif_physical, strerror(errno));
19900Sstevel@tonic-gate 			return_code = Z_ERR;
19910Sstevel@tonic-gate 			continue;
19920Sstevel@tonic-gate 		}
19930Sstevel@tonic-gate 		if (ioctl(so, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) {
19940Sstevel@tonic-gate 			(void) fprintf(stderr,
19950Sstevel@tonic-gate 			    gettext("could not verify %s %s=%s %s=%s: %s\n"),
19960Sstevel@tonic-gate 			    "net", "address", nwiftab.zone_nwif_address,
19970Sstevel@tonic-gate 			    "physical", nwiftab.zone_nwif_physical,
19980Sstevel@tonic-gate 			    strerror(errno));
19990Sstevel@tonic-gate 			return_code = Z_ERR;
20000Sstevel@tonic-gate 		}
20010Sstevel@tonic-gate 		(void) close(so);
20020Sstevel@tonic-gate 	}
20030Sstevel@tonic-gate 	(void) zonecfg_endnwifent(handle);
2004*766Scarlsonj no_net:
20050Sstevel@tonic-gate 
20060Sstevel@tonic-gate 	if (verify_filesystems(handle) != Z_OK)
20070Sstevel@tonic-gate 		return_code = Z_ERR;
2008*766Scarlsonj 	if (!in_alt_root && verify_rctls(handle) != Z_OK)
20090Sstevel@tonic-gate 		return_code = Z_ERR;
2010*766Scarlsonj 	if (!in_alt_root && verify_pool(handle) != Z_OK)
20110Sstevel@tonic-gate 		return_code = Z_ERR;
20120Sstevel@tonic-gate 	zonecfg_fini_handle(handle);
20130Sstevel@tonic-gate 	if (return_code == Z_ERR)
20140Sstevel@tonic-gate 		(void) fprintf(stderr,
20150Sstevel@tonic-gate 		    gettext("%s: zone %s failed to verify\n"),
20160Sstevel@tonic-gate 		    execname, target_zone);
20170Sstevel@tonic-gate 	return (return_code);
20180Sstevel@tonic-gate }
20190Sstevel@tonic-gate 
20200Sstevel@tonic-gate static int
20210Sstevel@tonic-gate verify_func(int argc, char *argv[])
20220Sstevel@tonic-gate {
20230Sstevel@tonic-gate 	int arg;
20240Sstevel@tonic-gate 
20250Sstevel@tonic-gate 	optind = 0;
20260Sstevel@tonic-gate 	if ((arg = getopt(argc, argv, "?")) != EOF) {
20270Sstevel@tonic-gate 		switch (arg) {
20280Sstevel@tonic-gate 		case '?':
20290Sstevel@tonic-gate 			sub_usage(SHELP_VERIFY, CMD_VERIFY);
20300Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
20310Sstevel@tonic-gate 		default:
20320Sstevel@tonic-gate 			sub_usage(SHELP_VERIFY, CMD_VERIFY);
20330Sstevel@tonic-gate 			return (Z_USAGE);
20340Sstevel@tonic-gate 		}
20350Sstevel@tonic-gate 	}
20360Sstevel@tonic-gate 	if (argc > optind) {
20370Sstevel@tonic-gate 		sub_usage(SHELP_VERIFY, CMD_VERIFY);
20380Sstevel@tonic-gate 		return (Z_USAGE);
20390Sstevel@tonic-gate 	}
20400Sstevel@tonic-gate 	if (sanity_check(target_zone, CMD_VERIFY, B_FALSE, B_FALSE) != Z_OK)
20410Sstevel@tonic-gate 		return (Z_ERR);
20420Sstevel@tonic-gate 	return (verify_details(CMD_VERIFY));
20430Sstevel@tonic-gate }
20440Sstevel@tonic-gate 
20450Sstevel@tonic-gate #define	LUCREATEZONE	"/usr/lib/lu/lucreatezone"
20460Sstevel@tonic-gate 
20470Sstevel@tonic-gate static int
20480Sstevel@tonic-gate install_func(int argc, char *argv[])
20490Sstevel@tonic-gate {
20500Sstevel@tonic-gate 	/* 9: "exec " and " -z " */
20510Sstevel@tonic-gate 	char cmdbuf[sizeof (LUCREATEZONE) + ZONENAME_MAX + 9];
20520Sstevel@tonic-gate 	int lockfd;
20530Sstevel@tonic-gate 	int err, arg;
20540Sstevel@tonic-gate 	char zonepath[MAXPATHLEN];
20550Sstevel@tonic-gate 	int status;
20560Sstevel@tonic-gate 
2057*766Scarlsonj 	if (zonecfg_in_alt_root()) {
2058*766Scarlsonj 		zerror(gettext("cannot install zone in alternate root"));
2059*766Scarlsonj 		return (Z_ERR);
2060*766Scarlsonj 	}
2061*766Scarlsonj 
20620Sstevel@tonic-gate 	optind = 0;
20630Sstevel@tonic-gate 	if ((arg = getopt(argc, argv, "?")) != EOF) {
20640Sstevel@tonic-gate 		switch (arg) {
20650Sstevel@tonic-gate 		case '?':
20660Sstevel@tonic-gate 			sub_usage(SHELP_INSTALL, CMD_INSTALL);
20670Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
20680Sstevel@tonic-gate 		default:
20690Sstevel@tonic-gate 			sub_usage(SHELP_INSTALL, CMD_INSTALL);
20700Sstevel@tonic-gate 			return (Z_USAGE);
20710Sstevel@tonic-gate 		}
20720Sstevel@tonic-gate 	}
20730Sstevel@tonic-gate 	if (argc > optind) {
20740Sstevel@tonic-gate 		sub_usage(SHELP_INSTALL, CMD_INSTALL);
20750Sstevel@tonic-gate 		return (Z_USAGE);
20760Sstevel@tonic-gate 	}
20770Sstevel@tonic-gate 	if (sanity_check(target_zone, CMD_INSTALL, B_FALSE, B_TRUE) != Z_OK)
20780Sstevel@tonic-gate 		return (Z_ERR);
20790Sstevel@tonic-gate 	if (verify_details(CMD_INSTALL) != Z_OK)
20800Sstevel@tonic-gate 		return (Z_ERR);
20810Sstevel@tonic-gate 
20820Sstevel@tonic-gate 	if (grab_lock_file(target_zone, &lockfd) != Z_OK) {
20830Sstevel@tonic-gate 		zerror(gettext("another %s may have an operation in progress."),
20840Sstevel@tonic-gate 		    "zoneadmd");
20850Sstevel@tonic-gate 		return (Z_ERR);
20860Sstevel@tonic-gate 	}
20870Sstevel@tonic-gate 	err = zone_set_state(target_zone, ZONE_STATE_INCOMPLETE);
20880Sstevel@tonic-gate 	if (err != Z_OK) {
20890Sstevel@tonic-gate 		errno = err;
20900Sstevel@tonic-gate 		zperror2(target_zone, gettext("could not set state"));
20910Sstevel@tonic-gate 		goto done;
20920Sstevel@tonic-gate 	}
20930Sstevel@tonic-gate 
20940Sstevel@tonic-gate 	/*
20950Sstevel@tonic-gate 	 * According to the Application Packaging Developer's Guide, a
20960Sstevel@tonic-gate 	 * "checkinstall" script when included in a package is executed as
20970Sstevel@tonic-gate 	 * the user "install", if such a user exists, or by the user
20980Sstevel@tonic-gate 	 * "nobody".  In order to support this dubious behavior, the path
20990Sstevel@tonic-gate 	 * to the zone being constructed is opened up during the life of
21000Sstevel@tonic-gate 	 * the command laying down the zone's root file system.  Once this
21010Sstevel@tonic-gate 	 * has completed, regardless of whether it was successful, the
21020Sstevel@tonic-gate 	 * path to the zone is again restricted.
21030Sstevel@tonic-gate 	 */
21040Sstevel@tonic-gate 	if ((err = zone_get_zonepath(target_zone, zonepath,
21050Sstevel@tonic-gate 	    sizeof (zonepath))) != Z_OK) {
21060Sstevel@tonic-gate 		errno = err;
21070Sstevel@tonic-gate 		zperror2(target_zone, gettext("could not get zone path"));
21080Sstevel@tonic-gate 		goto done;
21090Sstevel@tonic-gate 	}
21100Sstevel@tonic-gate 	if (chmod(zonepath, DEFAULT_DIR_MODE) != 0) {
21110Sstevel@tonic-gate 		zperror(zonepath, B_FALSE);
21120Sstevel@tonic-gate 		err = Z_ERR;
21130Sstevel@tonic-gate 		goto done;
21140Sstevel@tonic-gate 	}
21150Sstevel@tonic-gate 
21160Sstevel@tonic-gate 	/*
21170Sstevel@tonic-gate 	 * "exec" the command so that the returned status is that of
21180Sstevel@tonic-gate 	 * LUCREATEZONE and not the shell.
21190Sstevel@tonic-gate 	 */
21200Sstevel@tonic-gate 	(void) snprintf(cmdbuf, sizeof (cmdbuf), "exec " LUCREATEZONE " -z %s",
21210Sstevel@tonic-gate 	    target_zone);
21220Sstevel@tonic-gate 	status = do_subproc(cmdbuf);
21230Sstevel@tonic-gate 	if (chmod(zonepath, S_IRWXU) != 0) {
21240Sstevel@tonic-gate 		zperror(zonepath, B_FALSE);
21250Sstevel@tonic-gate 		err = Z_ERR;
21260Sstevel@tonic-gate 		goto done;
21270Sstevel@tonic-gate 	}
21280Sstevel@tonic-gate 	if ((err = subproc_status(LUCREATEZONE, status)) != Z_OK)
21290Sstevel@tonic-gate 		goto done;
21300Sstevel@tonic-gate 
21310Sstevel@tonic-gate 	if ((err = zone_set_state(target_zone, ZONE_STATE_INSTALLED)) != Z_OK) {
21320Sstevel@tonic-gate 		errno = err;
21330Sstevel@tonic-gate 		zperror2(target_zone, gettext("could not set state"));
21340Sstevel@tonic-gate 		goto done;
21350Sstevel@tonic-gate 	}
21360Sstevel@tonic-gate 
21370Sstevel@tonic-gate done:
21380Sstevel@tonic-gate 	release_lock_file(lockfd);
21390Sstevel@tonic-gate 	return ((err == Z_OK) ? Z_OK : Z_ERR);
21400Sstevel@tonic-gate }
21410Sstevel@tonic-gate 
21420Sstevel@tonic-gate /*
21430Sstevel@tonic-gate  * On input, TRUE => yes, FALSE => no.
21440Sstevel@tonic-gate  * On return, TRUE => 1, FALSE => 0, could not ask => -1.
21450Sstevel@tonic-gate  */
21460Sstevel@tonic-gate 
21470Sstevel@tonic-gate static int
21480Sstevel@tonic-gate ask_yesno(boolean_t default_answer, const char *question)
21490Sstevel@tonic-gate {
21500Sstevel@tonic-gate 	char line[64];	/* should be large enough to answer yes or no */
21510Sstevel@tonic-gate 
21520Sstevel@tonic-gate 	if (!isatty(STDIN_FILENO))
21530Sstevel@tonic-gate 		return (-1);
21540Sstevel@tonic-gate 	for (;;) {
21550Sstevel@tonic-gate 		(void) printf("%s (%s)? ", question,
21560Sstevel@tonic-gate 		    default_answer ? "[y]/n" : "y/[n]");
21570Sstevel@tonic-gate 		if (fgets(line, sizeof (line), stdin) == NULL ||
21580Sstevel@tonic-gate 		    line[0] == '\n')
21590Sstevel@tonic-gate 			return (default_answer ? 1 : 0);
21600Sstevel@tonic-gate 		if (tolower(line[0]) == 'y')
21610Sstevel@tonic-gate 			return (1);
21620Sstevel@tonic-gate 		if (tolower(line[0]) == 'n')
21630Sstevel@tonic-gate 			return (0);
21640Sstevel@tonic-gate 	}
21650Sstevel@tonic-gate }
21660Sstevel@tonic-gate 
21670Sstevel@tonic-gate #define	RMCOMMAND	"/usr/bin/rm -rf"
21680Sstevel@tonic-gate 
21690Sstevel@tonic-gate /* ARGSUSED */
21700Sstevel@tonic-gate int
21710Sstevel@tonic-gate zfm_print(const char *p, void *r) {
21720Sstevel@tonic-gate 	zerror("  %s\n", p);
21730Sstevel@tonic-gate 	return (0);
21740Sstevel@tonic-gate }
21750Sstevel@tonic-gate 
21760Sstevel@tonic-gate static int
21770Sstevel@tonic-gate uninstall_func(int argc, char *argv[])
21780Sstevel@tonic-gate {
21790Sstevel@tonic-gate 	/* 6: "exec " and " " */
21800Sstevel@tonic-gate 	char cmdbuf[sizeof (RMCOMMAND) + MAXPATHLEN + 6];
21810Sstevel@tonic-gate 	char line[ZONENAME_MAX + 128];	/* Enough for "Are you sure ..." */
21820Sstevel@tonic-gate 	char rootpath[MAXPATHLEN], devpath[MAXPATHLEN];
21830Sstevel@tonic-gate 	boolean_t force = B_FALSE;
21840Sstevel@tonic-gate 	int lockfd, answer;
21850Sstevel@tonic-gate 	int err, arg;
21860Sstevel@tonic-gate 	int status;
21870Sstevel@tonic-gate 
2188*766Scarlsonj 	if (zonecfg_in_alt_root()) {
2189*766Scarlsonj 		zerror(gettext("cannot uninstall zone in alternate root"));
2190*766Scarlsonj 		return (Z_ERR);
2191*766Scarlsonj 	}
2192*766Scarlsonj 
21930Sstevel@tonic-gate 	optind = 0;
21940Sstevel@tonic-gate 	while ((arg = getopt(argc, argv, "?F")) != EOF) {
21950Sstevel@tonic-gate 		switch (arg) {
21960Sstevel@tonic-gate 		case '?':
21970Sstevel@tonic-gate 			sub_usage(SHELP_UNINSTALL, CMD_UNINSTALL);
21980Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
21990Sstevel@tonic-gate 		case 'F':
22000Sstevel@tonic-gate 			force = B_TRUE;
22010Sstevel@tonic-gate 			break;
22020Sstevel@tonic-gate 		default:
22030Sstevel@tonic-gate 			sub_usage(SHELP_UNINSTALL, CMD_UNINSTALL);
22040Sstevel@tonic-gate 			return (Z_USAGE);
22050Sstevel@tonic-gate 		}
22060Sstevel@tonic-gate 	}
22070Sstevel@tonic-gate 	if (argc > optind) {
22080Sstevel@tonic-gate 		sub_usage(SHELP_UNINSTALL, CMD_UNINSTALL);
22090Sstevel@tonic-gate 		return (Z_USAGE);
22100Sstevel@tonic-gate 	}
22110Sstevel@tonic-gate 
22120Sstevel@tonic-gate 	if (sanity_check(target_zone, CMD_UNINSTALL, B_FALSE, B_TRUE) != Z_OK)
22130Sstevel@tonic-gate 		return (Z_ERR);
22140Sstevel@tonic-gate 
22150Sstevel@tonic-gate 	if (!force) {
22160Sstevel@tonic-gate 		(void) snprintf(line, sizeof (line),
22170Sstevel@tonic-gate 		    gettext("Are you sure you want to %s zone %s"),
22180Sstevel@tonic-gate 		    cmd_to_str(CMD_UNINSTALL), target_zone);
22190Sstevel@tonic-gate 		if ((answer = ask_yesno(B_FALSE, line)) == 0) {
22200Sstevel@tonic-gate 			return (Z_OK);
22210Sstevel@tonic-gate 		} else if (answer == -1) {
22220Sstevel@tonic-gate 			zerror(gettext("Input not from terminal and -F "
22230Sstevel@tonic-gate 			    "not specified: %s not done."),
22240Sstevel@tonic-gate 			    cmd_to_str(CMD_UNINSTALL));
22250Sstevel@tonic-gate 			return (Z_ERR);
22260Sstevel@tonic-gate 		}
22270Sstevel@tonic-gate 	}
22280Sstevel@tonic-gate 
22290Sstevel@tonic-gate 	if ((err = zone_get_zonepath(target_zone, devpath,
22300Sstevel@tonic-gate 	    sizeof (devpath))) != Z_OK) {
22310Sstevel@tonic-gate 		errno = err;
22320Sstevel@tonic-gate 		zperror2(target_zone, gettext("could not get zone path"));
22330Sstevel@tonic-gate 		return (Z_ERR);
22340Sstevel@tonic-gate 	}
22350Sstevel@tonic-gate 	(void) strlcat(devpath, "/dev", sizeof (devpath));
22360Sstevel@tonic-gate 	if ((err = zone_get_rootpath(target_zone, rootpath,
22370Sstevel@tonic-gate 	    sizeof (rootpath))) != Z_OK) {
22380Sstevel@tonic-gate 		errno = err;
22390Sstevel@tonic-gate 		zperror2(target_zone, gettext("could not get root path"));
22400Sstevel@tonic-gate 		return (Z_ERR);
22410Sstevel@tonic-gate 	}
22420Sstevel@tonic-gate 
22430Sstevel@tonic-gate 	/*
22440Sstevel@tonic-gate 	 * If there seems to be a zoneadmd running for this zone, call it
22450Sstevel@tonic-gate 	 * to tell it that an uninstall is happening; if all goes well it
22460Sstevel@tonic-gate 	 * will then shut itself down.
22470Sstevel@tonic-gate 	 */
22480Sstevel@tonic-gate 	if (ping_zoneadmd(target_zone) == Z_OK) {
22490Sstevel@tonic-gate 		zone_cmd_arg_t zarg;
22500Sstevel@tonic-gate 		zarg.cmd = Z_NOTE_UNINSTALLING;
22510Sstevel@tonic-gate 		/* we don't care too much if this fails... just plow on */
22520Sstevel@tonic-gate 		(void) call_zoneadmd(target_zone, &zarg);
22530Sstevel@tonic-gate 	}
22540Sstevel@tonic-gate 
22550Sstevel@tonic-gate 	if (grab_lock_file(target_zone, &lockfd) != Z_OK) {
22560Sstevel@tonic-gate 		zerror(gettext("another %s may have an operation in progress."),
22570Sstevel@tonic-gate 		    "zoneadmd");
22580Sstevel@tonic-gate 		return (Z_ERR);
22590Sstevel@tonic-gate 	}
22600Sstevel@tonic-gate 
22610Sstevel@tonic-gate 	/* Don't uninstall the zone if anything is mounted there */
22620Sstevel@tonic-gate 	err = zonecfg_find_mounts(rootpath, NULL, NULL);
22630Sstevel@tonic-gate 	if (err) {
22640Sstevel@tonic-gate 		zerror(gettext("These file-systems are mounted on "
22650Sstevel@tonic-gate 			"subdirectories of %s.\n"), rootpath);
22660Sstevel@tonic-gate 		(void) zonecfg_find_mounts(rootpath, zfm_print, NULL);
22670Sstevel@tonic-gate 		return (Z_ERR);
22680Sstevel@tonic-gate 	}
22690Sstevel@tonic-gate 
22700Sstevel@tonic-gate 	err = zone_set_state(target_zone, ZONE_STATE_INCOMPLETE);
22710Sstevel@tonic-gate 	if (err != Z_OK) {
22720Sstevel@tonic-gate 		errno = err;
22730Sstevel@tonic-gate 		zperror2(target_zone, gettext("could not set state"));
22740Sstevel@tonic-gate 		goto bad;
22750Sstevel@tonic-gate 	}
22760Sstevel@tonic-gate 
22770Sstevel@tonic-gate 	/*
22780Sstevel@tonic-gate 	 * "exec" the command so that the returned status is that of
22790Sstevel@tonic-gate 	 * RMCOMMAND and not the shell.
22800Sstevel@tonic-gate 	 */
22810Sstevel@tonic-gate 	(void) snprintf(cmdbuf, sizeof (cmdbuf), "exec " RMCOMMAND " %s",
22820Sstevel@tonic-gate 	    devpath);
22830Sstevel@tonic-gate 	status = do_subproc(cmdbuf);
22840Sstevel@tonic-gate 	if ((err = subproc_status(RMCOMMAND, status)) != Z_OK)
22850Sstevel@tonic-gate 		goto bad;
22860Sstevel@tonic-gate 	(void) snprintf(cmdbuf, sizeof (cmdbuf), "exec " RMCOMMAND " %s",
22870Sstevel@tonic-gate 	    rootpath);
22880Sstevel@tonic-gate 	status = do_subproc(cmdbuf);
22890Sstevel@tonic-gate 	if ((err = subproc_status(RMCOMMAND, status)) != Z_OK)
22900Sstevel@tonic-gate 		goto bad;
22910Sstevel@tonic-gate 	err = zone_set_state(target_zone, ZONE_STATE_CONFIGURED);
22920Sstevel@tonic-gate 	if (err != Z_OK) {
22930Sstevel@tonic-gate 		errno = err;
22940Sstevel@tonic-gate 		zperror2(target_zone, gettext("could not reset state"));
22950Sstevel@tonic-gate 	}
22960Sstevel@tonic-gate bad:
22970Sstevel@tonic-gate 	release_lock_file(lockfd);
22980Sstevel@tonic-gate 	return (err);
22990Sstevel@tonic-gate }
23000Sstevel@tonic-gate 
2301*766Scarlsonj /* ARGSUSED */
2302*766Scarlsonj static int
2303*766Scarlsonj mount_func(int argc, char *argv[])
2304*766Scarlsonj {
2305*766Scarlsonj 	zone_cmd_arg_t zarg;
2306*766Scarlsonj 
2307*766Scarlsonj 	if (argc > 0)
2308*766Scarlsonj 		return (Z_USAGE);
2309*766Scarlsonj 	if (sanity_check(target_zone, CMD_MOUNT, B_FALSE, B_FALSE) != Z_OK)
2310*766Scarlsonj 		return (Z_ERR);
2311*766Scarlsonj 	if (verify_details(CMD_MOUNT) != Z_OK)
2312*766Scarlsonj 		return (Z_ERR);
2313*766Scarlsonj 
2314*766Scarlsonj 	zarg.cmd = Z_MOUNT;
2315*766Scarlsonj 	if (call_zoneadmd(target_zone, &zarg) != 0) {
2316*766Scarlsonj 		zerror(gettext("call to %s failed"), "zoneadmd");
2317*766Scarlsonj 		return (Z_ERR);
2318*766Scarlsonj 	}
2319*766Scarlsonj 	return (Z_OK);
2320*766Scarlsonj }
2321*766Scarlsonj 
2322*766Scarlsonj /* ARGSUSED */
2323*766Scarlsonj static int
2324*766Scarlsonj unmount_func(int argc, char *argv[])
2325*766Scarlsonj {
2326*766Scarlsonj 	zone_cmd_arg_t zarg;
2327*766Scarlsonj 
2328*766Scarlsonj 	if (argc > 0)
2329*766Scarlsonj 		return (Z_USAGE);
2330*766Scarlsonj 	if (sanity_check(target_zone, CMD_UNMOUNT, B_FALSE, B_FALSE) != Z_OK)
2331*766Scarlsonj 		return (Z_ERR);
2332*766Scarlsonj 
2333*766Scarlsonj 	zarg.cmd = Z_UNMOUNT;
2334*766Scarlsonj 	if (call_zoneadmd(target_zone, &zarg) != 0) {
2335*766Scarlsonj 		zerror(gettext("call to %s failed"), "zoneadmd");
2336*766Scarlsonj 		return (Z_ERR);
2337*766Scarlsonj 	}
2338*766Scarlsonj 	return (Z_OK);
2339*766Scarlsonj }
2340*766Scarlsonj 
23410Sstevel@tonic-gate static int
23420Sstevel@tonic-gate help_func(int argc, char *argv[])
23430Sstevel@tonic-gate {
23440Sstevel@tonic-gate 	int arg, cmd_num;
23450Sstevel@tonic-gate 
23460Sstevel@tonic-gate 	if (argc == 0) {
23470Sstevel@tonic-gate 		(void) usage(B_TRUE);
23480Sstevel@tonic-gate 		return (Z_OK);
23490Sstevel@tonic-gate 	}
23500Sstevel@tonic-gate 	optind = 0;
23510Sstevel@tonic-gate 	if ((arg = getopt(argc, argv, "?")) != EOF) {
23520Sstevel@tonic-gate 		switch (arg) {
23530Sstevel@tonic-gate 		case '?':
23540Sstevel@tonic-gate 			sub_usage(SHELP_HELP, CMD_HELP);
23550Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
23560Sstevel@tonic-gate 		default:
23570Sstevel@tonic-gate 			sub_usage(SHELP_HELP, CMD_HELP);
23580Sstevel@tonic-gate 			return (Z_USAGE);
23590Sstevel@tonic-gate 		}
23600Sstevel@tonic-gate 	}
23610Sstevel@tonic-gate 	while (optind < argc) {
23620Sstevel@tonic-gate 		if ((cmd_num = cmd_match(argv[optind])) < 0) {
23630Sstevel@tonic-gate 			sub_usage(SHELP_HELP, CMD_HELP);
23640Sstevel@tonic-gate 			return (Z_USAGE);
23650Sstevel@tonic-gate 		}
23660Sstevel@tonic-gate 		sub_usage(cmdtab[cmd_num].short_usage, cmd_num);
23670Sstevel@tonic-gate 		optind++;
23680Sstevel@tonic-gate 	}
23690Sstevel@tonic-gate 	return (Z_OK);
23700Sstevel@tonic-gate }
23710Sstevel@tonic-gate 
23720Sstevel@tonic-gate /*
23730Sstevel@tonic-gate  * Returns: CMD_MIN thru CMD_MAX on success, -1 on error
23740Sstevel@tonic-gate  */
23750Sstevel@tonic-gate 
23760Sstevel@tonic-gate static int
23770Sstevel@tonic-gate cmd_match(char *cmd)
23780Sstevel@tonic-gate {
23790Sstevel@tonic-gate 	int i;
23800Sstevel@tonic-gate 
23810Sstevel@tonic-gate 	for (i = CMD_MIN; i <= CMD_MAX; i++) {
23820Sstevel@tonic-gate 		/* return only if there is an exact match */
23830Sstevel@tonic-gate 		if (strcmp(cmd, cmdtab[i].cmd_name) == 0)
23840Sstevel@tonic-gate 			return (cmdtab[i].cmd_num);
23850Sstevel@tonic-gate 	}
23860Sstevel@tonic-gate 	return (-1);
23870Sstevel@tonic-gate }
23880Sstevel@tonic-gate 
23890Sstevel@tonic-gate static int
23900Sstevel@tonic-gate parse_and_run(int argc, char *argv[])
23910Sstevel@tonic-gate {
23920Sstevel@tonic-gate 	int i = cmd_match(argv[0]);
23930Sstevel@tonic-gate 
23940Sstevel@tonic-gate 	if (i < 0)
23950Sstevel@tonic-gate 		return (usage(B_FALSE));
23960Sstevel@tonic-gate 	return (cmdtab[i].handler(argc - 1, &(argv[1])));
23970Sstevel@tonic-gate }
23980Sstevel@tonic-gate 
23990Sstevel@tonic-gate static char *
24000Sstevel@tonic-gate get_execbasename(char *execfullname)
24010Sstevel@tonic-gate {
24020Sstevel@tonic-gate 	char *last_slash, *execbasename;
24030Sstevel@tonic-gate 
24040Sstevel@tonic-gate 	/* guard against '/' at end of command invocation */
24050Sstevel@tonic-gate 	for (;;) {
24060Sstevel@tonic-gate 		last_slash = strrchr(execfullname, '/');
24070Sstevel@tonic-gate 		if (last_slash == NULL) {
24080Sstevel@tonic-gate 			execbasename = execfullname;
24090Sstevel@tonic-gate 			break;
24100Sstevel@tonic-gate 		} else {
24110Sstevel@tonic-gate 			execbasename = last_slash + 1;
24120Sstevel@tonic-gate 			if (*execbasename == '\0') {
24130Sstevel@tonic-gate 				*last_slash = '\0';
24140Sstevel@tonic-gate 				continue;
24150Sstevel@tonic-gate 			}
24160Sstevel@tonic-gate 			break;
24170Sstevel@tonic-gate 		}
24180Sstevel@tonic-gate 	}
24190Sstevel@tonic-gate 	return (execbasename);
24200Sstevel@tonic-gate }
24210Sstevel@tonic-gate 
24220Sstevel@tonic-gate int
24230Sstevel@tonic-gate main(int argc, char **argv)
24240Sstevel@tonic-gate {
24250Sstevel@tonic-gate 	int arg;
24260Sstevel@tonic-gate 	zoneid_t zid;
2427*766Scarlsonj 	struct stat st;
24280Sstevel@tonic-gate 
24290Sstevel@tonic-gate 	if ((locale = setlocale(LC_ALL, "")) == NULL)
24300Sstevel@tonic-gate 		locale = "C";
24310Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
24320Sstevel@tonic-gate 	setbuf(stdout, NULL);
24330Sstevel@tonic-gate 	(void) sigset(SIGHUP, SIG_IGN);
24340Sstevel@tonic-gate 	execname = get_execbasename(argv[0]);
24350Sstevel@tonic-gate 	target_zone = NULL;
24360Sstevel@tonic-gate 	if (chdir("/") != 0) {
24370Sstevel@tonic-gate 		zerror(gettext("could not change directory to /."));
24380Sstevel@tonic-gate 		exit(Z_ERR);
24390Sstevel@tonic-gate 	}
24400Sstevel@tonic-gate 
2441*766Scarlsonj 	while ((arg = getopt(argc, argv, "?z:R:")) != EOF) {
24420Sstevel@tonic-gate 		switch (arg) {
24430Sstevel@tonic-gate 		case '?':
24440Sstevel@tonic-gate 			return (usage(B_TRUE));
24450Sstevel@tonic-gate 		case 'z':
24460Sstevel@tonic-gate 			target_zone = optarg;
24470Sstevel@tonic-gate 			break;
2448*766Scarlsonj 		case 'R':	/* private option for admin/install use */
2449*766Scarlsonj 			if (*optarg != '/') {
2450*766Scarlsonj 				zerror(gettext("root path must be absolute."));
2451*766Scarlsonj 				exit(Z_ERR);
2452*766Scarlsonj 			}
2453*766Scarlsonj 			if (stat(optarg, &st) == -1 || !S_ISDIR(st.st_mode)) {
2454*766Scarlsonj 				zerror(
2455*766Scarlsonj 				    gettext("root path must be a directory."));
2456*766Scarlsonj 				exit(Z_ERR);
2457*766Scarlsonj 			}
2458*766Scarlsonj 			zonecfg_set_root(optarg);
2459*766Scarlsonj 			break;
24600Sstevel@tonic-gate 		default:
24610Sstevel@tonic-gate 			return (usage(B_FALSE));
24620Sstevel@tonic-gate 		}
24630Sstevel@tonic-gate 	}
24640Sstevel@tonic-gate 
24650Sstevel@tonic-gate 	if (optind >= argc)
24660Sstevel@tonic-gate 		return (usage(B_FALSE));
24670Sstevel@tonic-gate 	if (target_zone != NULL && zone_get_id(target_zone, &zid) != 0) {
24680Sstevel@tonic-gate 		errno = Z_NO_ZONE;
24690Sstevel@tonic-gate 		zperror(target_zone, B_TRUE);
24700Sstevel@tonic-gate 		exit(Z_ERR);
24710Sstevel@tonic-gate 	}
24720Sstevel@tonic-gate 	return (parse_and_run(argc - optind, &argv[optind]));
24730Sstevel@tonic-gate }
2474