1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License, Version 1.0 only 6 * (the "License"). You may not use this file except in compliance 7 * with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or http://www.opensolaris.org/os/licensing. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22 23 /* 24 * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 25 * Use is subject to license terms. 26 */ 27 28 #pragma ident "%Z%%M% %I% %E% SMI" 29 30 /* 31 * zoneadm is a command interpreter for zone administration. It is all in 32 * C (i.e., no lex/yacc), and all the argument passing is argc/argv based. 33 * main() calls parse_and_run() which calls cmd_match(), then invokes the 34 * appropriate command's handler function. The rest of the program is the 35 * handler functions and their helper functions. 36 * 37 * Some of the helper functions are used largely to simplify I18N: reducing 38 * the need for translation notes. This is particularly true of many of 39 * the zerror() calls: doing e.g. zerror(gettext("%s failed"), "foo") rather 40 * than zerror(gettext("foo failed")) with a translation note indicating 41 * that "foo" need not be translated. 42 */ 43 44 #include <stdio.h> 45 #include <errno.h> 46 #include <unistd.h> 47 #include <signal.h> 48 #include <stdarg.h> 49 #include <ctype.h> 50 #include <stdlib.h> 51 #include <string.h> 52 #include <wait.h> 53 #include <zone.h> 54 #include <priv.h> 55 #include <locale.h> 56 #include <libintl.h> 57 #include <libzonecfg.h> 58 #include <bsm/adt.h> 59 #include <sys/utsname.h> 60 #include <sys/param.h> 61 #include <sys/types.h> 62 #include <sys/stat.h> 63 #include <sys/statvfs.h> 64 #include <assert.h> 65 #include <sys/sockio.h> 66 #include <sys/mntent.h> 67 #include <limits.h> 68 #include <libzfs.h> 69 70 #include <fcntl.h> 71 #include <door.h> 72 #include <macros.h> 73 #include <libgen.h> 74 #include <fnmatch.h> 75 76 #include <pool.h> 77 #include <sys/pool.h> 78 79 #define MAXARGS 8 80 81 /* Reflects kernel zone entries */ 82 typedef struct zone_entry { 83 zoneid_t zid; 84 char zname[ZONENAME_MAX]; 85 char *zstate_str; 86 zone_state_t zstate_num; 87 char zroot[MAXPATHLEN]; 88 } zone_entry_t; 89 90 static zone_entry_t *zents; 91 static size_t nzents; 92 93 #if !defined(TEXT_DOMAIN) /* should be defined by cc -D */ 94 #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it wasn't */ 95 #endif 96 97 #define Z_ERR 1 98 #define Z_USAGE 2 99 100 /* 0755 is the default directory mode. */ 101 #define DEFAULT_DIR_MODE \ 102 (S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) 103 104 #define CMD_HELP 0 105 #define CMD_BOOT 1 106 #define CMD_HALT 2 107 #define CMD_READY 3 108 #define CMD_REBOOT 4 109 #define CMD_LIST 5 110 #define CMD_VERIFY 6 111 #define CMD_INSTALL 7 112 #define CMD_UNINSTALL 8 113 #define CMD_MOUNT 9 114 #define CMD_UNMOUNT 10 115 #define CMD_CLONE 11 116 #define CMD_MOVE 12 117 118 #define CMD_MIN CMD_HELP 119 #define CMD_MAX CMD_MOVE 120 121 #define SINGLE_USER_RETRY 30 122 123 struct cmd { 124 uint_t cmd_num; /* command number */ 125 char *cmd_name; /* command name */ 126 char *short_usage; /* short form help */ 127 int (*handler)(int argc, char *argv[]); /* function to call */ 128 129 }; 130 131 #define SHELP_HELP "help" 132 #define SHELP_BOOT "boot [-s]" 133 #define SHELP_HALT "halt" 134 #define SHELP_READY "ready" 135 #define SHELP_REBOOT "reboot" 136 #define SHELP_LIST "list [-cipv]" 137 #define SHELP_VERIFY "verify" 138 #define SHELP_INSTALL "install" 139 #define SHELP_UNINSTALL "uninstall [-F]" 140 #define SHELP_CLONE "clone [-m method] zonename" 141 #define SHELP_MOVE "move zonepath" 142 143 static int help_func(int argc, char *argv[]); 144 static int ready_func(int argc, char *argv[]); 145 static int boot_func(int argc, char *argv[]); 146 static int halt_func(int argc, char *argv[]); 147 static int reboot_func(int argc, char *argv[]); 148 static int list_func(int argc, char *argv[]); 149 static int verify_func(int argc, char *argv[]); 150 static int install_func(int argc, char *argv[]); 151 static int uninstall_func(int argc, char *argv[]); 152 static int mount_func(int argc, char *argv[]); 153 static int unmount_func(int argc, char *argv[]); 154 static int clone_func(int argc, char *argv[]); 155 static int move_func(int argc, char *argv[]); 156 static int sanity_check(char *zone, int cmd_num, boolean_t running, 157 boolean_t unsafe_when_running); 158 static int cmd_match(char *cmd); 159 static int verify_details(int); 160 161 static struct cmd cmdtab[] = { 162 { CMD_HELP, "help", SHELP_HELP, help_func }, 163 { CMD_BOOT, "boot", SHELP_BOOT, boot_func }, 164 { CMD_HALT, "halt", SHELP_HALT, halt_func }, 165 { CMD_READY, "ready", SHELP_READY, ready_func }, 166 { CMD_REBOOT, "reboot", SHELP_REBOOT, reboot_func }, 167 { CMD_LIST, "list", SHELP_LIST, list_func }, 168 { CMD_VERIFY, "verify", SHELP_VERIFY, verify_func }, 169 { CMD_INSTALL, "install", SHELP_INSTALL, install_func }, 170 { CMD_UNINSTALL, "uninstall", SHELP_UNINSTALL, 171 uninstall_func }, 172 /* mount and unmount are private commands for admin/install */ 173 { CMD_MOUNT, "mount", NULL, mount_func }, 174 { CMD_UNMOUNT, "unmount", NULL, unmount_func }, 175 { CMD_CLONE, "clone", SHELP_CLONE, clone_func }, 176 { CMD_MOVE, "move", SHELP_MOVE, move_func } 177 }; 178 179 /* global variables */ 180 181 /* set early in main(), never modified thereafter, used all over the place */ 182 static char *execname; 183 static char *target_zone; 184 static char *locale; 185 186 /* used in do_subproc() and signal handler */ 187 static volatile boolean_t child_killed; 188 189 static char * 190 cmd_to_str(int cmd_num) 191 { 192 assert(cmd_num >= CMD_MIN && cmd_num <= CMD_MAX); 193 return (cmdtab[cmd_num].cmd_name); 194 } 195 196 /* This is a separate function because of gettext() wrapping. */ 197 static char * 198 long_help(int cmd_num) 199 { 200 assert(cmd_num >= CMD_MIN && cmd_num <= CMD_MAX); 201 switch (cmd_num) { 202 case CMD_HELP: 203 return (gettext("Print usage message.")); 204 case CMD_BOOT: 205 return (gettext("Activates (boots) specified zone. " 206 "The -s flag can be used\n\tto boot the zone in " 207 "the single-user state.")); 208 case CMD_HALT: 209 return (gettext("Halts specified zone, bypassing " 210 "shutdown scripts and removing runtime\n\t" 211 "resources of the zone.")); 212 case CMD_READY: 213 return (gettext("Prepares a zone for running " 214 "applications but does not start any user\n\t" 215 "processes in the zone.")); 216 case CMD_REBOOT: 217 return (gettext("Restarts the zone (equivalent to a " 218 "halt / boot sequence).\n\tFails if the zone is " 219 "not active.")); 220 case CMD_LIST: 221 return (gettext("Lists the current zones, or a " 222 "specific zone if indicated. By default,\n\tall " 223 "running zones are listed, though this can be " 224 "expanded to all\n\tinstalled zones with the -i " 225 "option or all configured zones with the\n\t-c " 226 "option. When used with the general -z <zone> " 227 "option, lists only the\n\tspecified zone, but " 228 "lists it regardless of its state, and the -i " 229 "and -c\n\toptions are disallowed. The -v option " 230 "can be used to display verbose\n\tinformation: " 231 "zone name, id, current state, root directory and " 232 "options.\n\tThe -p option can be used to request " 233 "machine-parsable output. The -v\n\tand -p " 234 "options are mutually exclusive. If neither -v " 235 "nor -p is used,\n\tjust the zone name is " 236 "listed.")); 237 case CMD_VERIFY: 238 return (gettext("Check to make sure the configuration " 239 "can safely be instantiated\n\ton the machine: " 240 "physical network interfaces exist, etc.")); 241 case CMD_INSTALL: 242 return (gettext("Install the configuration on to the " 243 "system.")); 244 case CMD_UNINSTALL: 245 return (gettext("Uninstall the configuration from the " 246 "system. The -F flag can be used\n\tto force the " 247 "action.")); 248 case CMD_CLONE: 249 return (gettext("Clone the installation of another " 250 "zone.")); 251 case CMD_MOVE: 252 return (gettext("Move the zone to a new zonepath.")); 253 default: 254 return (""); 255 } 256 /* NOTREACHED */ 257 return (NULL); 258 } 259 260 /* 261 * Called with explicit B_TRUE when help is explicitly requested, B_FALSE for 262 * unexpected errors. 263 */ 264 265 static int 266 usage(boolean_t explicit) 267 { 268 int i; 269 FILE *fd = explicit ? stdout : stderr; 270 271 (void) fprintf(fd, "%s:\t%s help\n", gettext("usage"), execname); 272 (void) fprintf(fd, "\t%s [-z <zone>] list\n", execname); 273 (void) fprintf(fd, "\t%s -z <zone> <%s>\n", execname, 274 gettext("subcommand")); 275 (void) fprintf(fd, "\n%s:\n\n", gettext("Subcommands")); 276 for (i = CMD_MIN; i <= CMD_MAX; i++) { 277 if (cmdtab[i].short_usage == NULL) 278 continue; 279 (void) fprintf(fd, "%s\n", cmdtab[i].short_usage); 280 if (explicit) 281 (void) fprintf(fd, "\t%s\n\n", long_help(i)); 282 } 283 if (!explicit) 284 (void) fputs("\n", fd); 285 return (Z_USAGE); 286 } 287 288 static void 289 sub_usage(char *short_usage, int cmd_num) 290 { 291 (void) fprintf(stderr, "%s:\t%s\n", gettext("usage"), short_usage); 292 (void) fprintf(stderr, "\t%s\n", long_help(cmd_num)); 293 } 294 295 /* 296 * zperror() is like perror(3c) except that this also prints the executable 297 * name at the start of the message, and takes a boolean indicating whether 298 * to call libc'c strerror() or that from libzonecfg. 299 */ 300 301 static void 302 zperror(const char *str, boolean_t zonecfg_error) 303 { 304 (void) fprintf(stderr, "%s: %s: %s\n", execname, str, 305 zonecfg_error ? zonecfg_strerror(errno) : strerror(errno)); 306 } 307 308 /* 309 * zperror2() is very similar to zperror() above, except it also prints a 310 * supplied zone name after the executable. 311 * 312 * All current consumers of this function want libzonecfg's strerror() rather 313 * than libc's; if this ever changes, this function can be made more generic 314 * like zperror() above. 315 */ 316 317 static void 318 zperror2(const char *zone, const char *str) 319 { 320 (void) fprintf(stderr, "%s: %s: %s: %s\n", execname, zone, str, 321 zonecfg_strerror(errno)); 322 } 323 324 /* PRINTFLIKE1 */ 325 static void 326 zerror(const char *fmt, ...) 327 { 328 va_list alist; 329 330 va_start(alist, fmt); 331 (void) fprintf(stderr, "%s: ", execname); 332 if (target_zone != NULL) 333 (void) fprintf(stderr, "zone '%s': ", target_zone); 334 (void) vfprintf(stderr, fmt, alist); 335 (void) fprintf(stderr, "\n"); 336 va_end(alist); 337 } 338 339 static void * 340 safe_calloc(size_t nelem, size_t elsize) 341 { 342 void *r = calloc(nelem, elsize); 343 344 if (r == NULL) { 345 zerror(gettext("failed to allocate %lu bytes: %s"), 346 (ulong_t)nelem * elsize, strerror(errno)); 347 exit(Z_ERR); 348 } 349 return (r); 350 } 351 352 static void 353 zone_print(zone_entry_t *zent, boolean_t verbose, boolean_t parsable) 354 { 355 static boolean_t firsttime = B_TRUE; 356 357 assert(!(verbose && parsable)); 358 if (firsttime && verbose) { 359 firsttime = B_FALSE; 360 (void) printf("%*s %-16s %-14s %-30s\n", ZONEID_WIDTH, "ID", 361 "NAME", "STATUS", "PATH"); 362 } 363 if (!verbose) { 364 if (!parsable) { 365 (void) printf("%s\n", zent->zname); 366 return; 367 } 368 if (zent->zid == ZONE_ID_UNDEFINED) 369 (void) printf("-"); 370 else 371 (void) printf("%lu", zent->zid); 372 (void) printf(":%s:%s:%s\n", zent->zname, zent->zstate_str, 373 zent->zroot); 374 return; 375 } 376 if (zent->zstate_str != NULL) { 377 if (zent->zid == ZONE_ID_UNDEFINED) 378 (void) printf("%*s", ZONEID_WIDTH, "-"); 379 else 380 (void) printf("%*lu", ZONEID_WIDTH, zent->zid); 381 (void) printf(" %-16s %-14s %-30s\n", zent->zname, 382 zent->zstate_str, zent->zroot); 383 } 384 } 385 386 static int 387 lookup_zone_info(const char *zone_name, zoneid_t zid, zone_entry_t *zent) 388 { 389 char root[MAXPATHLEN]; 390 int err; 391 392 (void) strlcpy(zent->zname, zone_name, sizeof (zent->zname)); 393 (void) strlcpy(zent->zroot, "???", sizeof (zent->zroot)); 394 zent->zstate_str = "???"; 395 396 zent->zid = zid; 397 398 if ((err = zone_get_zonepath(zent->zname, root, sizeof (root))) != 399 Z_OK) { 400 errno = err; 401 zperror2(zent->zname, gettext("could not get zone path")); 402 return (Z_ERR); 403 } 404 (void) strlcpy(zent->zroot, root, sizeof (zent->zroot)); 405 406 if ((err = zone_get_state(zent->zname, &zent->zstate_num)) != Z_OK) { 407 errno = err; 408 zperror2(zent->zname, gettext("could not get state")); 409 return (Z_ERR); 410 } 411 zent->zstate_str = zone_state_str(zent->zstate_num); 412 413 return (Z_OK); 414 } 415 416 /* 417 * fetch_zents() calls zone_list(2) to find out how many zones are running 418 * (which is stored in the global nzents), then calls zone_list(2) again 419 * to fetch the list of running zones (stored in the global zents). This 420 * function may be called multiple times, so if zents is already set, we 421 * return immediately to save work. 422 */ 423 424 static int 425 fetch_zents(void) 426 { 427 zoneid_t *zids = NULL; 428 uint_t nzents_saved; 429 int i, retv; 430 FILE *fp; 431 boolean_t inaltroot; 432 zone_entry_t *zentp; 433 434 if (nzents > 0) 435 return (Z_OK); 436 437 if (zone_list(NULL, &nzents) != 0) { 438 zperror(gettext("failed to get zoneid list"), B_FALSE); 439 return (Z_ERR); 440 } 441 442 again: 443 if (nzents == 0) 444 return (Z_OK); 445 446 zids = safe_calloc(nzents, sizeof (zoneid_t)); 447 nzents_saved = nzents; 448 449 if (zone_list(zids, &nzents) != 0) { 450 zperror(gettext("failed to get zone list"), B_FALSE); 451 free(zids); 452 return (Z_ERR); 453 } 454 if (nzents != nzents_saved) { 455 /* list changed, try again */ 456 free(zids); 457 goto again; 458 } 459 460 zents = safe_calloc(nzents, sizeof (zone_entry_t)); 461 462 inaltroot = zonecfg_in_alt_root(); 463 if (inaltroot) 464 fp = zonecfg_open_scratch("", B_FALSE); 465 else 466 fp = NULL; 467 zentp = zents; 468 retv = Z_OK; 469 for (i = 0; i < nzents; i++) { 470 char name[ZONENAME_MAX]; 471 char altname[ZONENAME_MAX]; 472 473 if (getzonenamebyid(zids[i], name, sizeof (name)) < 0) { 474 zperror(gettext("failed to get zone name"), B_FALSE); 475 retv = Z_ERR; 476 continue; 477 } 478 if (zonecfg_is_scratch(name)) { 479 /* Ignore scratch zones by default */ 480 if (!inaltroot) 481 continue; 482 if (fp == NULL || 483 zonecfg_reverse_scratch(fp, name, altname, 484 sizeof (altname), NULL, 0) == -1) { 485 zerror(gettext("could not resolve scratch " 486 "zone %s"), name); 487 retv = Z_ERR; 488 continue; 489 } 490 (void) strcpy(name, altname); 491 } else { 492 /* Ignore non-scratch when in an alternate root */ 493 if (inaltroot && strcmp(name, GLOBAL_ZONENAME) != 0) 494 continue; 495 } 496 if (lookup_zone_info(name, zids[i], zentp) != Z_OK) { 497 zerror(gettext("failed to get zone data")); 498 retv = Z_ERR; 499 continue; 500 } 501 zentp++; 502 } 503 nzents = zentp - zents; 504 if (fp != NULL) 505 zonecfg_close_scratch(fp); 506 507 free(zids); 508 return (retv); 509 } 510 511 static int 512 zone_print_list(zone_state_t min_state, boolean_t verbose, boolean_t parsable) 513 { 514 int i; 515 zone_entry_t zent; 516 FILE *cookie; 517 char *name; 518 519 /* 520 * First get the list of running zones from the kernel and print them. 521 * If that is all we need, then return. 522 */ 523 if ((i = fetch_zents()) != Z_OK) { 524 /* 525 * No need for error messages; fetch_zents() has already taken 526 * care of this. 527 */ 528 return (i); 529 } 530 for (i = 0; i < nzents; i++) 531 zone_print(&zents[i], verbose, parsable); 532 if (min_state >= ZONE_STATE_RUNNING) 533 return (Z_OK); 534 /* 535 * Next, get the full list of zones from the configuration, skipping 536 * any we have already printed. 537 */ 538 cookie = setzoneent(); 539 while ((name = getzoneent(cookie)) != NULL) { 540 for (i = 0; i < nzents; i++) { 541 if (strcmp(zents[i].zname, name) == 0) 542 break; 543 } 544 if (i < nzents) { 545 free(name); 546 continue; 547 } 548 if (lookup_zone_info(name, ZONE_ID_UNDEFINED, &zent) != Z_OK) { 549 free(name); 550 continue; 551 } 552 free(name); 553 if (zent.zstate_num >= min_state) 554 zone_print(&zent, verbose, parsable); 555 } 556 endzoneent(cookie); 557 return (Z_OK); 558 } 559 560 static zone_entry_t * 561 lookup_running_zone(char *str) 562 { 563 zoneid_t zoneid; 564 char *cp; 565 int i; 566 567 if (fetch_zents() != Z_OK) 568 return (NULL); 569 570 for (i = 0; i < nzents; i++) { 571 if (strcmp(str, zents[i].zname) == 0) 572 return (&zents[i]); 573 } 574 errno = 0; 575 zoneid = strtol(str, &cp, 0); 576 if (zoneid < MIN_ZONEID || zoneid > MAX_ZONEID || 577 errno != 0 || *cp != '\0') 578 return (NULL); 579 for (i = 0; i < nzents; i++) { 580 if (zoneid == zents[i].zid) 581 return (&zents[i]); 582 } 583 return (NULL); 584 } 585 586 /* 587 * Check a bit in a mode_t: if on is B_TRUE, that bit should be on; if 588 * B_FALSE, it should be off. Return B_TRUE if the mode is bad (incorrect). 589 */ 590 static boolean_t 591 bad_mode_bit(mode_t mode, mode_t bit, boolean_t on, char *file) 592 { 593 char *str; 594 595 assert(bit == S_IRUSR || bit == S_IWUSR || bit == S_IXUSR || 596 bit == S_IRGRP || bit == S_IWGRP || bit == S_IXGRP || 597 bit == S_IROTH || bit == S_IWOTH || bit == S_IXOTH); 598 /* 599 * TRANSLATION_NOTE 600 * The strings below will be used as part of a larger message, 601 * either: 602 * (file name) must be (owner|group|world) (read|writ|execut)able 603 * or 604 * (file name) must not be (owner|group|world) (read|writ|execut)able 605 */ 606 switch (bit) { 607 case S_IRUSR: 608 str = gettext("owner readable"); 609 break; 610 case S_IWUSR: 611 str = gettext("owner writable"); 612 break; 613 case S_IXUSR: 614 str = gettext("owner executable"); 615 break; 616 case S_IRGRP: 617 str = gettext("group readable"); 618 break; 619 case S_IWGRP: 620 str = gettext("group writable"); 621 break; 622 case S_IXGRP: 623 str = gettext("group executable"); 624 break; 625 case S_IROTH: 626 str = gettext("world readable"); 627 break; 628 case S_IWOTH: 629 str = gettext("world writable"); 630 break; 631 case S_IXOTH: 632 str = gettext("world executable"); 633 break; 634 } 635 if ((mode & bit) == (on ? 0 : bit)) { 636 /* 637 * TRANSLATION_NOTE 638 * The first parameter below is a file name; the second 639 * is one of the "(owner|group|world) (read|writ|execut)able" 640 * strings from above. 641 */ 642 /* 643 * The code below could be simplified but not in a way 644 * that would easily translate to non-English locales. 645 */ 646 if (on) { 647 (void) fprintf(stderr, gettext("%s must be %s.\n"), 648 file, str); 649 } else { 650 (void) fprintf(stderr, gettext("%s must not be %s.\n"), 651 file, str); 652 } 653 return (B_TRUE); 654 } 655 return (B_FALSE); 656 } 657 658 /* 659 * We want to make sure that no zone has its zone path as a child node 660 * (in the directory sense) of any other. We do that by comparing this 661 * zone's path to the path of all other (non-global) zones. The comparison 662 * in each case is simple: add '/' to the end of the path, then do a 663 * strncmp() of the two paths, using the length of the shorter one. 664 */ 665 666 static int 667 crosscheck_zonepaths(char *path) 668 { 669 char rpath[MAXPATHLEN]; /* resolved path */ 670 char path_copy[MAXPATHLEN]; /* copy of original path */ 671 char rpath_copy[MAXPATHLEN]; /* copy of original rpath */ 672 struct zoneent *ze; 673 int res, err; 674 FILE *cookie; 675 676 cookie = setzoneent(); 677 while ((ze = getzoneent_private(cookie)) != NULL) { 678 /* Skip zones which are not installed. */ 679 if (ze->zone_state < ZONE_STATE_INSTALLED) { 680 free(ze); 681 continue; 682 } 683 /* Skip the global zone and the current target zone. */ 684 if (strcmp(ze->zone_name, GLOBAL_ZONENAME) == 0 || 685 strcmp(ze->zone_name, target_zone) == 0) { 686 free(ze); 687 continue; 688 } 689 if (strlen(ze->zone_path) == 0) { 690 /* old index file without path, fall back */ 691 if ((err = zone_get_zonepath(ze->zone_name, 692 ze->zone_path, sizeof (ze->zone_path))) != Z_OK) { 693 errno = err; 694 zperror2(ze->zone_name, 695 gettext("could not get zone path")); 696 free(ze); 697 continue; 698 } 699 } 700 (void) snprintf(path_copy, sizeof (path_copy), "%s%s", 701 zonecfg_get_root(), ze->zone_path); 702 res = resolvepath(path_copy, rpath, sizeof (rpath)); 703 if (res == -1) { 704 if (errno != ENOENT) { 705 zperror(path_copy, B_FALSE); 706 free(ze); 707 return (Z_ERR); 708 } 709 (void) printf(gettext("WARNING: zone %s is installed, " 710 "but its %s %s does not exist.\n"), ze->zone_name, 711 "zonepath", path_copy); 712 free(ze); 713 continue; 714 } 715 rpath[res] = '\0'; 716 (void) snprintf(path_copy, sizeof (path_copy), "%s/", path); 717 (void) snprintf(rpath_copy, sizeof (rpath_copy), "%s/", rpath); 718 if (strncmp(path_copy, rpath_copy, 719 min(strlen(path_copy), strlen(rpath_copy))) == 0) { 720 /* 721 * TRANSLATION_NOTE 722 * zonepath is a literal that should not be translated. 723 */ 724 (void) fprintf(stderr, gettext("%s zonepath (%s) and " 725 "%s zonepath (%s) overlap.\n"), 726 target_zone, path, ze->zone_name, rpath); 727 free(ze); 728 return (Z_ERR); 729 } 730 free(ze); 731 } 732 endzoneent(cookie); 733 return (Z_OK); 734 } 735 736 static int 737 validate_zonepath(char *path, int cmd_num) 738 { 739 int res; /* result of last library/system call */ 740 boolean_t err = B_FALSE; /* have we run into an error? */ 741 struct stat stbuf; 742 struct statvfs vfsbuf; 743 char rpath[MAXPATHLEN]; /* resolved path */ 744 char ppath[MAXPATHLEN]; /* parent path */ 745 char rppath[MAXPATHLEN]; /* resolved parent path */ 746 char rootpath[MAXPATHLEN]; /* root path */ 747 zone_state_t state; 748 749 if (path[0] != '/') { 750 (void) fprintf(stderr, 751 gettext("%s is not an absolute path.\n"), path); 752 return (Z_ERR); 753 } 754 if ((res = resolvepath(path, rpath, sizeof (rpath))) == -1) { 755 if ((errno != ENOENT) || 756 (cmd_num != CMD_VERIFY && cmd_num != CMD_INSTALL && 757 cmd_num != CMD_CLONE && cmd_num != CMD_MOVE)) { 758 zperror(path, B_FALSE); 759 return (Z_ERR); 760 } 761 if (cmd_num == CMD_VERIFY) { 762 /* 763 * TRANSLATION_NOTE 764 * zoneadm is a literal that should not be translated. 765 */ 766 (void) fprintf(stderr, gettext("WARNING: %s does not " 767 "exist, so it could not be verified.\nWhen " 768 "'zoneadm %s' is run, '%s' will try to create\n%s, " 769 "and '%s' will be tried again,\nbut the '%s' may " 770 "fail if:\nthe parent directory of %s is group- or " 771 "other-writable\nor\n%s overlaps with any other " 772 "installed zones.\n"), path, 773 cmd_to_str(CMD_INSTALL), cmd_to_str(CMD_INSTALL), 774 path, cmd_to_str(CMD_VERIFY), 775 cmd_to_str(CMD_VERIFY), path, path); 776 return (Z_OK); 777 } 778 /* 779 * The zonepath is supposed to be mode 700 but its 780 * parent(s) 755. So use 755 on the mkdirp() then 781 * chmod() the zonepath itself to 700. 782 */ 783 if (mkdirp(path, DEFAULT_DIR_MODE) < 0) { 784 zperror(path, B_FALSE); 785 return (Z_ERR); 786 } 787 /* 788 * If the chmod() fails, report the error, but might 789 * as well continue the verify procedure. 790 */ 791 if (chmod(path, S_IRWXU) != 0) 792 zperror(path, B_FALSE); 793 /* 794 * Since the mkdir() succeeded, we should not have to 795 * worry about a subsequent ENOENT, thus this should 796 * only recurse once. 797 */ 798 return (validate_zonepath(path, cmd_num)); 799 } 800 rpath[res] = '\0'; 801 if (strcmp(path, rpath) != 0) { 802 errno = Z_RESOLVED_PATH; 803 zperror(path, B_TRUE); 804 return (Z_ERR); 805 } 806 if ((res = stat(rpath, &stbuf)) != 0) { 807 zperror(rpath, B_FALSE); 808 return (Z_ERR); 809 } 810 if (!S_ISDIR(stbuf.st_mode)) { 811 (void) fprintf(stderr, gettext("%s is not a directory.\n"), 812 rpath); 813 return (Z_ERR); 814 } 815 if ((strcmp(stbuf.st_fstype, MNTTYPE_TMPFS) == 0) || 816 (strcmp(stbuf.st_fstype, MNTTYPE_XMEMFS) == 0)) { 817 (void) printf(gettext("WARNING: %s is on a temporary " 818 "file-system.\n"), rpath); 819 } 820 if (crosscheck_zonepaths(rpath) != Z_OK) 821 return (Z_ERR); 822 /* 823 * Try to collect and report as many minor errors as possible 824 * before returning, so the user can learn everything that needs 825 * to be fixed up front. 826 */ 827 if (stbuf.st_uid != 0) { 828 (void) fprintf(stderr, gettext("%s is not owned by root.\n"), 829 rpath); 830 err = B_TRUE; 831 } 832 err |= bad_mode_bit(stbuf.st_mode, S_IRUSR, B_TRUE, rpath); 833 err |= bad_mode_bit(stbuf.st_mode, S_IWUSR, B_TRUE, rpath); 834 err |= bad_mode_bit(stbuf.st_mode, S_IXUSR, B_TRUE, rpath); 835 err |= bad_mode_bit(stbuf.st_mode, S_IRGRP, B_FALSE, rpath); 836 err |= bad_mode_bit(stbuf.st_mode, S_IWGRP, B_FALSE, rpath); 837 err |= bad_mode_bit(stbuf.st_mode, S_IXGRP, B_FALSE, rpath); 838 err |= bad_mode_bit(stbuf.st_mode, S_IROTH, B_FALSE, rpath); 839 err |= bad_mode_bit(stbuf.st_mode, S_IWOTH, B_FALSE, rpath); 840 err |= bad_mode_bit(stbuf.st_mode, S_IXOTH, B_FALSE, rpath); 841 842 (void) snprintf(ppath, sizeof (ppath), "%s/..", path); 843 if ((res = resolvepath(ppath, rppath, sizeof (rppath))) == -1) { 844 zperror(ppath, B_FALSE); 845 return (Z_ERR); 846 } 847 rppath[res] = '\0'; 848 if ((res = stat(rppath, &stbuf)) != 0) { 849 zperror(rppath, B_FALSE); 850 return (Z_ERR); 851 } 852 /* theoretically impossible */ 853 if (!S_ISDIR(stbuf.st_mode)) { 854 (void) fprintf(stderr, gettext("%s is not a directory.\n"), 855 rppath); 856 return (Z_ERR); 857 } 858 if (stbuf.st_uid != 0) { 859 (void) fprintf(stderr, gettext("%s is not owned by root.\n"), 860 rppath); 861 err = B_TRUE; 862 } 863 err |= bad_mode_bit(stbuf.st_mode, S_IRUSR, B_TRUE, rppath); 864 err |= bad_mode_bit(stbuf.st_mode, S_IWUSR, B_TRUE, rppath); 865 err |= bad_mode_bit(stbuf.st_mode, S_IXUSR, B_TRUE, rppath); 866 err |= bad_mode_bit(stbuf.st_mode, S_IWGRP, B_FALSE, rppath); 867 err |= bad_mode_bit(stbuf.st_mode, S_IWOTH, B_FALSE, rppath); 868 if (strcmp(rpath, rppath) == 0) { 869 (void) fprintf(stderr, gettext("%s is its own parent.\n"), 870 rppath); 871 err = B_TRUE; 872 } 873 874 if (statvfs(rpath, &vfsbuf) != 0) { 875 zperror(rpath, B_FALSE); 876 return (Z_ERR); 877 } 878 if (strcmp(vfsbuf.f_basetype, MNTTYPE_NFS) == 0) { 879 /* 880 * TRANSLATION_NOTE 881 * Zonepath and NFS are literals that should not be translated. 882 */ 883 (void) fprintf(stderr, gettext("Zonepath %s is on an NFS " 884 "mounted file-system.\n" 885 "\tA local file-system must be used.\n"), rpath); 886 return (Z_ERR); 887 } 888 if (vfsbuf.f_flag & ST_NOSUID) { 889 /* 890 * TRANSLATION_NOTE 891 * Zonepath and nosuid are literals that should not be 892 * translated. 893 */ 894 (void) fprintf(stderr, gettext("Zonepath %s is on a nosuid " 895 "file-system.\n"), rpath); 896 return (Z_ERR); 897 } 898 899 if ((res = zone_get_state(target_zone, &state)) != Z_OK) { 900 errno = res; 901 zperror2(target_zone, gettext("could not get state")); 902 return (Z_ERR); 903 } 904 /* 905 * The existence of the root path is only bad in the configured state, 906 * as it is *supposed* to be there at the installed and later states. 907 * State/command mismatches are caught earlier in verify_details(). 908 */ 909 if (state == ZONE_STATE_CONFIGURED) { 910 if (snprintf(rootpath, sizeof (rootpath), "%s/root", rpath) >= 911 sizeof (rootpath)) { 912 /* 913 * TRANSLATION_NOTE 914 * Zonepath is a literal that should not be translated. 915 */ 916 (void) fprintf(stderr, 917 gettext("Zonepath %s is too long.\n"), rpath); 918 return (Z_ERR); 919 } 920 if ((res = stat(rootpath, &stbuf)) == 0) { 921 (void) fprintf(stderr, gettext("Rootpath %s exists; " 922 "remove or move aside prior to %s.\n"), rootpath, 923 cmd_to_str(CMD_INSTALL)); 924 return (Z_ERR); 925 } 926 } 927 928 return (err ? Z_ERR : Z_OK); 929 } 930 931 static void 932 release_lock_file(int lockfd) 933 { 934 (void) close(lockfd); 935 } 936 937 static int 938 grab_lock_file(const char *zone_name, int *lockfd) 939 { 940 char pathbuf[PATH_MAX]; 941 struct flock flock; 942 943 if (snprintf(pathbuf, sizeof (pathbuf), "%s%s", zonecfg_get_root(), 944 ZONES_TMPDIR) >= sizeof (pathbuf)) { 945 zerror(gettext("alternate root path is too long")); 946 return (Z_ERR); 947 } 948 if (mkdir(pathbuf, S_IRWXU) < 0 && errno != EEXIST) { 949 zerror(gettext("could not mkdir %s: %s"), pathbuf, 950 strerror(errno)); 951 return (Z_ERR); 952 } 953 (void) chmod(pathbuf, S_IRWXU); 954 955 /* 956 * One of these lock files is created for each zone (when needed). 957 * The lock files are not cleaned up (except on system reboot), 958 * but since there is only one per zone, there is no resource 959 * starvation issue. 960 */ 961 if (snprintf(pathbuf, sizeof (pathbuf), "%s%s/%s.zoneadm.lock", 962 zonecfg_get_root(), ZONES_TMPDIR, zone_name) >= sizeof (pathbuf)) { 963 zerror(gettext("alternate root path is too long")); 964 return (Z_ERR); 965 } 966 if ((*lockfd = open(pathbuf, O_RDWR|O_CREAT, S_IRUSR|S_IWUSR)) < 0) { 967 zerror(gettext("could not open %s: %s"), pathbuf, 968 strerror(errno)); 969 return (Z_ERR); 970 } 971 /* 972 * Lock the file to synchronize with other zoneadmds 973 */ 974 flock.l_type = F_WRLCK; 975 flock.l_whence = SEEK_SET; 976 flock.l_start = (off_t)0; 977 flock.l_len = (off_t)0; 978 if (fcntl(*lockfd, F_SETLKW, &flock) < 0) { 979 zerror(gettext("unable to lock %s: %s"), pathbuf, 980 strerror(errno)); 981 release_lock_file(*lockfd); 982 return (Z_ERR); 983 } 984 return (Z_OK); 985 } 986 987 static boolean_t 988 get_doorname(const char *zone_name, char *buffer) 989 { 990 return (snprintf(buffer, PATH_MAX, "%s" ZONE_DOOR_PATH, 991 zonecfg_get_root(), zone_name) < PATH_MAX); 992 } 993 994 /* 995 * system daemons are not audited. For the global zone, this occurs 996 * "naturally" since init is started with the default audit 997 * characteristics. Since zoneadmd is a system daemon and it starts 998 * init for a zone, it is necessary to clear out the audit 999 * characteristics inherited from whomever started zoneadmd. This is 1000 * indicated by the audit id, which is set from the ruid parameter of 1001 * adt_set_user(), below. 1002 */ 1003 1004 static void 1005 prepare_audit_context() 1006 { 1007 adt_session_data_t *ah; 1008 char *failure = gettext("audit failure: %s"); 1009 1010 if (adt_start_session(&ah, NULL, 0)) { 1011 zerror(failure, strerror(errno)); 1012 return; 1013 } 1014 if (adt_set_user(ah, ADT_NO_AUDIT, ADT_NO_AUDIT, 1015 ADT_NO_AUDIT, ADT_NO_AUDIT, NULL, ADT_NEW)) { 1016 zerror(failure, strerror(errno)); 1017 (void) adt_end_session(ah); 1018 return; 1019 } 1020 if (adt_set_proc(ah)) 1021 zerror(failure, strerror(errno)); 1022 1023 (void) adt_end_session(ah); 1024 } 1025 1026 static int 1027 start_zoneadmd(const char *zone_name) 1028 { 1029 char doorpath[PATH_MAX]; 1030 pid_t child_pid; 1031 int error = Z_ERR; 1032 int doorfd, lockfd; 1033 struct door_info info; 1034 1035 if (!get_doorname(zone_name, doorpath)) 1036 return (Z_ERR); 1037 1038 if (grab_lock_file(zone_name, &lockfd) != Z_OK) 1039 return (Z_ERR); 1040 1041 /* 1042 * Now that we have the lock, re-confirm that the daemon is 1043 * *not* up and working fine. If it is still down, we have a green 1044 * light to start it. 1045 */ 1046 if ((doorfd = open(doorpath, O_RDONLY)) < 0) { 1047 if (errno != ENOENT) { 1048 zperror(doorpath, B_FALSE); 1049 goto out; 1050 } 1051 } else { 1052 if (door_info(doorfd, &info) == 0 && 1053 ((info.di_attributes & DOOR_REVOKED) == 0)) { 1054 error = Z_OK; 1055 (void) close(doorfd); 1056 goto out; 1057 } 1058 (void) close(doorfd); 1059 } 1060 1061 if ((child_pid = fork()) == -1) { 1062 zperror(gettext("could not fork"), B_FALSE); 1063 goto out; 1064 } else if (child_pid == 0) { 1065 const char *argv[6], **ap; 1066 1067 /* child process */ 1068 prepare_audit_context(); 1069 1070 ap = argv; 1071 *ap++ = "zoneadmd"; 1072 *ap++ = "-z"; 1073 *ap++ = zone_name; 1074 if (zonecfg_in_alt_root()) { 1075 *ap++ = "-R"; 1076 *ap++ = zonecfg_get_root(); 1077 } 1078 *ap = NULL; 1079 1080 (void) execv("/usr/lib/zones/zoneadmd", (char * const *)argv); 1081 /* 1082 * TRANSLATION_NOTE 1083 * zoneadmd is a literal that should not be translated. 1084 */ 1085 zperror(gettext("could not exec zoneadmd"), B_FALSE); 1086 _exit(Z_ERR); 1087 } else { 1088 /* parent process */ 1089 pid_t retval; 1090 int pstatus = 0; 1091 1092 do { 1093 retval = waitpid(child_pid, &pstatus, 0); 1094 } while (retval != child_pid); 1095 if (WIFSIGNALED(pstatus) || (WIFEXITED(pstatus) && 1096 WEXITSTATUS(pstatus) != 0)) { 1097 zerror(gettext("could not start %s"), "zoneadmd"); 1098 goto out; 1099 } 1100 } 1101 error = Z_OK; 1102 out: 1103 release_lock_file(lockfd); 1104 return (error); 1105 } 1106 1107 static int 1108 ping_zoneadmd(const char *zone_name) 1109 { 1110 char doorpath[PATH_MAX]; 1111 int doorfd; 1112 struct door_info info; 1113 1114 if (!get_doorname(zone_name, doorpath)) 1115 return (Z_ERR); 1116 1117 if ((doorfd = open(doorpath, O_RDONLY)) < 0) { 1118 return (Z_ERR); 1119 } 1120 if (door_info(doorfd, &info) == 0 && 1121 ((info.di_attributes & DOOR_REVOKED) == 0)) { 1122 (void) close(doorfd); 1123 return (Z_OK); 1124 } 1125 (void) close(doorfd); 1126 return (Z_ERR); 1127 } 1128 1129 static int 1130 call_zoneadmd(const char *zone_name, zone_cmd_arg_t *arg) 1131 { 1132 char doorpath[PATH_MAX]; 1133 int doorfd, result; 1134 door_arg_t darg; 1135 1136 zoneid_t zoneid; 1137 uint64_t uniqid = 0; 1138 1139 zone_cmd_rval_t *rvalp; 1140 size_t rlen; 1141 char *cp, *errbuf; 1142 1143 rlen = getpagesize(); 1144 if ((rvalp = malloc(rlen)) == NULL) { 1145 zerror(gettext("failed to allocate %lu bytes: %s"), rlen, 1146 strerror(errno)); 1147 return (-1); 1148 } 1149 1150 if ((zoneid = getzoneidbyname(zone_name)) != ZONE_ID_UNDEFINED) { 1151 (void) zone_getattr(zoneid, ZONE_ATTR_UNIQID, &uniqid, 1152 sizeof (uniqid)); 1153 } 1154 arg->uniqid = uniqid; 1155 (void) strlcpy(arg->locale, locale, sizeof (arg->locale)); 1156 if (!get_doorname(zone_name, doorpath)) { 1157 zerror(gettext("alternate root path is too long")); 1158 free(rvalp); 1159 return (-1); 1160 } 1161 1162 /* 1163 * Loop trying to start zoneadmd; if something goes seriously 1164 * wrong we break out and fail. 1165 */ 1166 for (;;) { 1167 if (start_zoneadmd(zone_name) != Z_OK) 1168 break; 1169 1170 if ((doorfd = open(doorpath, O_RDONLY)) < 0) { 1171 zperror(gettext("failed to open zone door"), B_FALSE); 1172 break; 1173 } 1174 1175 darg.data_ptr = (char *)arg; 1176 darg.data_size = sizeof (*arg); 1177 darg.desc_ptr = NULL; 1178 darg.desc_num = 0; 1179 darg.rbuf = (char *)rvalp; 1180 darg.rsize = rlen; 1181 if (door_call(doorfd, &darg) != 0) { 1182 (void) close(doorfd); 1183 /* 1184 * We'll get EBADF if the door has been revoked. 1185 */ 1186 if (errno != EBADF) { 1187 zperror(gettext("door_call failed"), B_FALSE); 1188 break; 1189 } 1190 continue; /* take another lap */ 1191 } 1192 (void) close(doorfd); 1193 1194 if (darg.data_size == 0) { 1195 /* Door server is going away; kick it again. */ 1196 continue; 1197 } 1198 1199 errbuf = rvalp->errbuf; 1200 while (*errbuf != '\0') { 1201 /* 1202 * Remove any newlines since zerror() 1203 * will append one automatically. 1204 */ 1205 cp = strchr(errbuf, '\n'); 1206 if (cp != NULL) 1207 *cp = '\0'; 1208 zerror("%s", errbuf); 1209 if (cp == NULL) 1210 break; 1211 errbuf = cp + 1; 1212 } 1213 result = rvalp->rval == 0 ? 0 : -1; 1214 free(rvalp); 1215 return (result); 1216 } 1217 1218 free(rvalp); 1219 return (-1); 1220 } 1221 1222 static int 1223 ready_func(int argc, char *argv[]) 1224 { 1225 zone_cmd_arg_t zarg; 1226 int arg; 1227 1228 if (zonecfg_in_alt_root()) { 1229 zerror(gettext("cannot ready zone in alternate root")); 1230 return (Z_ERR); 1231 } 1232 1233 optind = 0; 1234 if ((arg = getopt(argc, argv, "?")) != EOF) { 1235 switch (arg) { 1236 case '?': 1237 sub_usage(SHELP_READY, CMD_READY); 1238 return (optopt == '?' ? Z_OK : Z_USAGE); 1239 default: 1240 sub_usage(SHELP_READY, CMD_READY); 1241 return (Z_USAGE); 1242 } 1243 } 1244 if (argc > optind) { 1245 sub_usage(SHELP_READY, CMD_READY); 1246 return (Z_USAGE); 1247 } 1248 if (sanity_check(target_zone, CMD_READY, B_FALSE, B_FALSE) != Z_OK) 1249 return (Z_ERR); 1250 if (verify_details(CMD_READY) != Z_OK) 1251 return (Z_ERR); 1252 1253 zarg.cmd = Z_READY; 1254 if (call_zoneadmd(target_zone, &zarg) != 0) { 1255 zerror(gettext("call to %s failed"), "zoneadmd"); 1256 return (Z_ERR); 1257 } 1258 return (Z_OK); 1259 } 1260 1261 static int 1262 boot_func(int argc, char *argv[]) 1263 { 1264 zone_cmd_arg_t zarg; 1265 int arg; 1266 1267 if (zonecfg_in_alt_root()) { 1268 zerror(gettext("cannot boot zone in alternate root")); 1269 return (Z_ERR); 1270 } 1271 1272 zarg.bootbuf[0] = '\0'; 1273 1274 /* 1275 * At the current time, the only supported subargument to the 1276 * "boot" subcommand is "-s" which specifies a single-user boot. 1277 * In the future, other boot arguments should be supported 1278 * including "-m" for specifying alternate smf(5) milestones. 1279 */ 1280 optind = 0; 1281 if ((arg = getopt(argc, argv, "?s")) != EOF) { 1282 switch (arg) { 1283 case '?': 1284 sub_usage(SHELP_BOOT, CMD_BOOT); 1285 return (optopt == '?' ? Z_OK : Z_USAGE); 1286 case 's': 1287 (void) strlcpy(zarg.bootbuf, "-s", 1288 sizeof (zarg.bootbuf)); 1289 break; 1290 default: 1291 sub_usage(SHELP_BOOT, CMD_BOOT); 1292 return (Z_USAGE); 1293 } 1294 } 1295 if (argc > optind) { 1296 sub_usage(SHELP_BOOT, CMD_BOOT); 1297 return (Z_USAGE); 1298 } 1299 if (sanity_check(target_zone, CMD_BOOT, B_FALSE, B_FALSE) != Z_OK) 1300 return (Z_ERR); 1301 if (verify_details(CMD_BOOT) != Z_OK) 1302 return (Z_ERR); 1303 zarg.cmd = Z_BOOT; 1304 if (call_zoneadmd(target_zone, &zarg) != 0) { 1305 zerror(gettext("call to %s failed"), "zoneadmd"); 1306 return (Z_ERR); 1307 } 1308 return (Z_OK); 1309 } 1310 1311 static void 1312 fake_up_local_zone(zoneid_t zid, zone_entry_t *zeptr) 1313 { 1314 ssize_t result; 1315 1316 zeptr->zid = zid; 1317 /* 1318 * Since we're looking up our own (non-global) zone name, 1319 * we can be assured that it will succeed. 1320 */ 1321 result = getzonenamebyid(zid, zeptr->zname, sizeof (zeptr->zname)); 1322 assert(result >= 0); 1323 (void) strlcpy(zeptr->zroot, "/", sizeof (zeptr->zroot)); 1324 zeptr->zstate_str = "running"; 1325 } 1326 1327 static int 1328 list_func(int argc, char *argv[]) 1329 { 1330 zone_entry_t *zentp, zent; 1331 int arg, retv; 1332 boolean_t output = B_FALSE, verbose = B_FALSE, parsable = B_FALSE; 1333 zone_state_t min_state = ZONE_STATE_RUNNING; 1334 zoneid_t zone_id = getzoneid(); 1335 1336 if (target_zone == NULL) { 1337 /* all zones: default view to running but allow override */ 1338 optind = 0; 1339 while ((arg = getopt(argc, argv, "?cipv")) != EOF) { 1340 switch (arg) { 1341 case '?': 1342 sub_usage(SHELP_LIST, CMD_LIST); 1343 return (optopt == '?' ? Z_OK : Z_USAGE); 1344 /* 1345 * The 'i' and 'c' options are not mutually 1346 * exclusive so if 'c' is given, then min_state 1347 * is set to 0 (ZONE_STATE_CONFIGURED) which is 1348 * the lowest possible state. If 'i' is given, 1349 * then min_state is set to be the lowest state 1350 * so far. 1351 */ 1352 case 'c': 1353 min_state = ZONE_STATE_CONFIGURED; 1354 break; 1355 case 'i': 1356 min_state = min(ZONE_STATE_INSTALLED, 1357 min_state); 1358 1359 break; 1360 case 'p': 1361 parsable = B_TRUE; 1362 break; 1363 case 'v': 1364 verbose = B_TRUE; 1365 break; 1366 default: 1367 sub_usage(SHELP_LIST, CMD_LIST); 1368 return (Z_USAGE); 1369 } 1370 } 1371 if (parsable && verbose) { 1372 zerror(gettext("%s -p and -v are mutually exclusive."), 1373 cmd_to_str(CMD_LIST)); 1374 return (Z_ERR); 1375 } 1376 if (zone_id == GLOBAL_ZONEID) { 1377 retv = zone_print_list(min_state, verbose, parsable); 1378 } else { 1379 retv = Z_OK; 1380 fake_up_local_zone(zone_id, &zent); 1381 zone_print(&zent, verbose, parsable); 1382 } 1383 return (retv); 1384 } 1385 1386 /* 1387 * Specific target zone: disallow -i/-c suboptions. 1388 */ 1389 optind = 0; 1390 while ((arg = getopt(argc, argv, "?pv")) != EOF) { 1391 switch (arg) { 1392 case '?': 1393 sub_usage(SHELP_LIST, CMD_LIST); 1394 return (optopt == '?' ? Z_OK : Z_USAGE); 1395 case 'p': 1396 parsable = B_TRUE; 1397 break; 1398 case 'v': 1399 verbose = B_TRUE; 1400 break; 1401 default: 1402 sub_usage(SHELP_LIST, CMD_LIST); 1403 return (Z_USAGE); 1404 } 1405 } 1406 if (parsable && verbose) { 1407 zerror(gettext("%s -p and -v are mutually exclusive."), 1408 cmd_to_str(CMD_LIST)); 1409 return (Z_ERR); 1410 } 1411 if (argc > optind) { 1412 sub_usage(SHELP_LIST, CMD_LIST); 1413 return (Z_USAGE); 1414 } 1415 if (zone_id != GLOBAL_ZONEID) { 1416 fake_up_local_zone(zone_id, &zent); 1417 /* 1418 * main() will issue a Z_NO_ZONE error if it cannot get an 1419 * id for target_zone, which in a non-global zone should 1420 * happen for any zone name except `zonename`. Thus we 1421 * assert() that here but don't otherwise check. 1422 */ 1423 assert(strcmp(zent.zname, target_zone) == 0); 1424 zone_print(&zent, verbose, parsable); 1425 output = B_TRUE; 1426 } else if ((zentp = lookup_running_zone(target_zone)) != NULL) { 1427 zone_print(zentp, verbose, parsable); 1428 output = B_TRUE; 1429 } else if (lookup_zone_info(target_zone, ZONE_ID_UNDEFINED, 1430 &zent) == Z_OK) { 1431 zone_print(&zent, verbose, parsable); 1432 output = B_TRUE; 1433 } 1434 return (output ? Z_OK : Z_ERR); 1435 } 1436 1437 static void 1438 sigterm(int sig) 1439 { 1440 /* 1441 * Ignore SIG{INT,TERM}, so we don't end up in an infinite loop, 1442 * then propagate the signal to our process group. 1443 */ 1444 (void) sigset(SIGINT, SIG_IGN); 1445 (void) sigset(SIGTERM, SIG_IGN); 1446 (void) kill(0, sig); 1447 child_killed = B_TRUE; 1448 } 1449 1450 static int 1451 do_subproc(char *cmdbuf) 1452 { 1453 char inbuf[1024]; /* arbitrary large amount */ 1454 FILE *file; 1455 1456 child_killed = B_FALSE; 1457 /* 1458 * We use popen(3c) to launch child processes for [un]install; 1459 * this library call does not return a PID, so we have to kill 1460 * the whole process group. To avoid killing our parent, we 1461 * become a process group leader here. But doing so can wreak 1462 * havoc with reading from stdin when launched by a non-job-control 1463 * shell, so we close stdin and reopen it as /dev/null first. 1464 */ 1465 (void) close(STDIN_FILENO); 1466 (void) open("/dev/null", O_RDONLY); 1467 (void) setpgid(0, 0); 1468 (void) sigset(SIGINT, sigterm); 1469 (void) sigset(SIGTERM, sigterm); 1470 file = popen(cmdbuf, "r"); 1471 for (;;) { 1472 if (child_killed || fgets(inbuf, sizeof (inbuf), file) == NULL) 1473 break; 1474 (void) fputs(inbuf, stdout); 1475 } 1476 (void) sigset(SIGINT, SIG_DFL); 1477 (void) sigset(SIGTERM, SIG_DFL); 1478 return (pclose(file)); 1479 } 1480 1481 static int 1482 subproc_status(const char *cmd, int status) 1483 { 1484 if (WIFEXITED(status)) { 1485 int exit_code = WEXITSTATUS(status); 1486 1487 if (exit_code == 0) 1488 return (Z_OK); 1489 zerror(gettext("'%s' failed with exit code %d."), cmd, 1490 exit_code); 1491 } else if (WIFSIGNALED(status)) { 1492 int signal = WTERMSIG(status); 1493 char sigstr[SIG2STR_MAX]; 1494 1495 if (sig2str(signal, sigstr) == 0) { 1496 zerror(gettext("'%s' terminated by signal SIG%s."), cmd, 1497 sigstr); 1498 } else { 1499 zerror(gettext("'%s' terminated by an unknown signal."), 1500 cmd); 1501 } 1502 } else { 1503 zerror(gettext("'%s' failed for unknown reasons."), cmd); 1504 } 1505 return (Z_ERR); 1506 } 1507 1508 /* 1509 * Various sanity checks; make sure: 1510 * 1. We're in the global zone. 1511 * 2. The calling user has sufficient privilege. 1512 * 3. The target zone is neither the global zone nor anything starting with 1513 * "SUNW". 1514 * 4a. If we're looking for a 'not running' (i.e., configured or installed) 1515 * zone, the name service knows about it. 1516 * 4b. For some operations which expect a zone not to be running, that it is 1517 * not already running (or ready). 1518 */ 1519 static int 1520 sanity_check(char *zone, int cmd_num, boolean_t running, 1521 boolean_t unsafe_when_running) 1522 { 1523 zone_entry_t *zent; 1524 priv_set_t *privset; 1525 zone_state_t state; 1526 char kernzone[ZONENAME_MAX]; 1527 FILE *fp; 1528 1529 if (getzoneid() != GLOBAL_ZONEID) { 1530 zerror(gettext("must be in the global zone to %s a zone."), 1531 cmd_to_str(cmd_num)); 1532 return (Z_ERR); 1533 } 1534 1535 if ((privset = priv_allocset()) == NULL) { 1536 zerror(gettext("%s failed"), "priv_allocset"); 1537 return (Z_ERR); 1538 } 1539 1540 if (getppriv(PRIV_EFFECTIVE, privset) != 0) { 1541 zerror(gettext("%s failed"), "getppriv"); 1542 priv_freeset(privset); 1543 return (Z_ERR); 1544 } 1545 1546 if (priv_isfullset(privset) == B_FALSE) { 1547 zerror(gettext("only a privileged user may %s a zone."), 1548 cmd_to_str(cmd_num)); 1549 priv_freeset(privset); 1550 return (Z_ERR); 1551 } 1552 priv_freeset(privset); 1553 1554 if (zone == NULL) { 1555 zerror(gettext("no zone specified")); 1556 return (Z_ERR); 1557 } 1558 1559 if (strcmp(zone, GLOBAL_ZONENAME) == 0) { 1560 zerror(gettext("%s operation is invalid for the global zone."), 1561 cmd_to_str(cmd_num)); 1562 return (Z_ERR); 1563 } 1564 1565 if (strncmp(zone, "SUNW", 4) == 0) { 1566 zerror(gettext("%s operation is invalid for zones starting " 1567 "with SUNW."), cmd_to_str(cmd_num)); 1568 return (Z_ERR); 1569 } 1570 1571 if (!zonecfg_in_alt_root()) { 1572 zent = lookup_running_zone(zone); 1573 } else if ((fp = zonecfg_open_scratch("", B_FALSE)) == NULL) { 1574 zent = NULL; 1575 } else { 1576 if (zonecfg_find_scratch(fp, zone, zonecfg_get_root(), 1577 kernzone, sizeof (kernzone)) == 0) 1578 zent = lookup_running_zone(kernzone); 1579 else 1580 zent = NULL; 1581 zonecfg_close_scratch(fp); 1582 } 1583 1584 /* 1585 * Look up from the kernel for 'running' zones. 1586 */ 1587 if (running) { 1588 if (zent == NULL) { 1589 zerror(gettext("not running")); 1590 return (Z_ERR); 1591 } 1592 } else { 1593 int err; 1594 1595 if (unsafe_when_running && zent != NULL) { 1596 /* check whether the zone is ready or running */ 1597 if ((err = zone_get_state(zent->zname, 1598 &zent->zstate_num)) != Z_OK) { 1599 errno = err; 1600 zperror2(zent->zname, 1601 gettext("could not get state")); 1602 /* can't tell, so hedge */ 1603 zent->zstate_str = "ready/running"; 1604 } else { 1605 zent->zstate_str = 1606 zone_state_str(zent->zstate_num); 1607 } 1608 zerror(gettext("%s operation is invalid for %s zones."), 1609 cmd_to_str(cmd_num), zent->zstate_str); 1610 return (Z_ERR); 1611 } 1612 if ((err = zone_get_state(zone, &state)) != Z_OK) { 1613 errno = err; 1614 zperror2(zone, gettext("could not get state")); 1615 return (Z_ERR); 1616 } 1617 switch (cmd_num) { 1618 case CMD_UNINSTALL: 1619 if (state == ZONE_STATE_CONFIGURED) { 1620 zerror(gettext("is already in state '%s'."), 1621 zone_state_str(ZONE_STATE_CONFIGURED)); 1622 return (Z_ERR); 1623 } 1624 break; 1625 case CMD_CLONE: 1626 case CMD_INSTALL: 1627 if (state == ZONE_STATE_INSTALLED) { 1628 zerror(gettext("is already %s."), 1629 zone_state_str(ZONE_STATE_INSTALLED)); 1630 return (Z_ERR); 1631 } else if (state == ZONE_STATE_INCOMPLETE) { 1632 zerror(gettext("zone is %s; %s required."), 1633 zone_state_str(ZONE_STATE_INCOMPLETE), 1634 cmd_to_str(CMD_UNINSTALL)); 1635 return (Z_ERR); 1636 } 1637 break; 1638 case CMD_MOVE: 1639 case CMD_READY: 1640 case CMD_BOOT: 1641 case CMD_MOUNT: 1642 if (state < ZONE_STATE_INSTALLED) { 1643 zerror(gettext("must be %s before %s."), 1644 zone_state_str(ZONE_STATE_INSTALLED), 1645 cmd_to_str(cmd_num)); 1646 return (Z_ERR); 1647 } 1648 break; 1649 case CMD_VERIFY: 1650 if (state == ZONE_STATE_INCOMPLETE) { 1651 zerror(gettext("zone is %s; %s required."), 1652 zone_state_str(ZONE_STATE_INCOMPLETE), 1653 cmd_to_str(CMD_UNINSTALL)); 1654 return (Z_ERR); 1655 } 1656 break; 1657 case CMD_UNMOUNT: 1658 if (state != ZONE_STATE_MOUNTED) { 1659 zerror(gettext("must be %s before %s."), 1660 zone_state_str(ZONE_STATE_MOUNTED), 1661 cmd_to_str(cmd_num)); 1662 return (Z_ERR); 1663 } 1664 break; 1665 } 1666 } 1667 return (Z_OK); 1668 } 1669 1670 static int 1671 halt_func(int argc, char *argv[]) 1672 { 1673 zone_cmd_arg_t zarg; 1674 int arg; 1675 1676 if (zonecfg_in_alt_root()) { 1677 zerror(gettext("cannot halt zone in alternate root")); 1678 return (Z_ERR); 1679 } 1680 1681 optind = 0; 1682 if ((arg = getopt(argc, argv, "?")) != EOF) { 1683 switch (arg) { 1684 case '?': 1685 sub_usage(SHELP_HALT, CMD_HALT); 1686 return (optopt == '?' ? Z_OK : Z_USAGE); 1687 default: 1688 sub_usage(SHELP_HALT, CMD_HALT); 1689 return (Z_USAGE); 1690 } 1691 } 1692 if (argc > optind) { 1693 sub_usage(SHELP_HALT, CMD_HALT); 1694 return (Z_USAGE); 1695 } 1696 /* 1697 * zoneadmd should be the one to decide whether or not to proceed, 1698 * so even though it seems that the fourth parameter below should 1699 * perhaps be B_TRUE, it really shouldn't be. 1700 */ 1701 if (sanity_check(target_zone, CMD_HALT, B_FALSE, B_FALSE) != Z_OK) 1702 return (Z_ERR); 1703 1704 zarg.cmd = Z_HALT; 1705 return ((call_zoneadmd(target_zone, &zarg) == 0) ? Z_OK : Z_ERR); 1706 } 1707 1708 static int 1709 reboot_func(int argc, char *argv[]) 1710 { 1711 zone_cmd_arg_t zarg; 1712 int arg; 1713 1714 if (zonecfg_in_alt_root()) { 1715 zerror(gettext("cannot reboot zone in alternate root")); 1716 return (Z_ERR); 1717 } 1718 1719 optind = 0; 1720 if ((arg = getopt(argc, argv, "?")) != EOF) { 1721 switch (arg) { 1722 case '?': 1723 sub_usage(SHELP_REBOOT, CMD_REBOOT); 1724 return (optopt == '?' ? Z_OK : Z_USAGE); 1725 default: 1726 sub_usage(SHELP_REBOOT, CMD_REBOOT); 1727 return (Z_USAGE); 1728 } 1729 } 1730 if (argc > 0) { 1731 sub_usage(SHELP_REBOOT, CMD_REBOOT); 1732 return (Z_USAGE); 1733 } 1734 /* 1735 * zoneadmd should be the one to decide whether or not to proceed, 1736 * so even though it seems that the fourth parameter below should 1737 * perhaps be B_TRUE, it really shouldn't be. 1738 */ 1739 if (sanity_check(target_zone, CMD_REBOOT, B_TRUE, B_FALSE) != Z_OK) 1740 return (Z_ERR); 1741 if (verify_details(CMD_REBOOT) != Z_OK) 1742 return (Z_ERR); 1743 1744 zarg.cmd = Z_REBOOT; 1745 return ((call_zoneadmd(target_zone, &zarg) == 0) ? Z_OK : Z_ERR); 1746 } 1747 1748 static int 1749 verify_rctls(zone_dochandle_t handle) 1750 { 1751 struct zone_rctltab rctltab; 1752 size_t rbs = rctlblk_size(); 1753 rctlblk_t *rctlblk; 1754 int error = Z_INVAL; 1755 1756 if ((rctlblk = malloc(rbs)) == NULL) { 1757 zerror(gettext("failed to allocate %lu bytes: %s"), rbs, 1758 strerror(errno)); 1759 return (Z_NOMEM); 1760 } 1761 1762 if (zonecfg_setrctlent(handle) != Z_OK) { 1763 zerror(gettext("zonecfg_setrctlent failed")); 1764 free(rctlblk); 1765 return (error); 1766 } 1767 1768 rctltab.zone_rctl_valptr = NULL; 1769 while (zonecfg_getrctlent(handle, &rctltab) == Z_OK) { 1770 struct zone_rctlvaltab *rctlval; 1771 const char *name = rctltab.zone_rctl_name; 1772 1773 if (!zonecfg_is_rctl(name)) { 1774 zerror(gettext("WARNING: Ignoring unrecognized rctl " 1775 "'%s'."), name); 1776 zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr); 1777 rctltab.zone_rctl_valptr = NULL; 1778 continue; 1779 } 1780 1781 for (rctlval = rctltab.zone_rctl_valptr; rctlval != NULL; 1782 rctlval = rctlval->zone_rctlval_next) { 1783 if (zonecfg_construct_rctlblk(rctlval, rctlblk) 1784 != Z_OK) { 1785 zerror(gettext("invalid rctl value: " 1786 "(priv=%s,limit=%s,action%s)"), 1787 rctlval->zone_rctlval_priv, 1788 rctlval->zone_rctlval_limit, 1789 rctlval->zone_rctlval_action); 1790 goto out; 1791 } 1792 if (!zonecfg_valid_rctl(name, rctlblk)) { 1793 zerror(gettext("(priv=%s,limit=%s,action=%s) " 1794 "is not a valid value for rctl '%s'"), 1795 rctlval->zone_rctlval_priv, 1796 rctlval->zone_rctlval_limit, 1797 rctlval->zone_rctlval_action, 1798 name); 1799 goto out; 1800 } 1801 } 1802 zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr); 1803 } 1804 rctltab.zone_rctl_valptr = NULL; 1805 error = Z_OK; 1806 out: 1807 zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr); 1808 (void) zonecfg_endrctlent(handle); 1809 free(rctlblk); 1810 return (error); 1811 } 1812 1813 static int 1814 verify_pool(zone_dochandle_t handle) 1815 { 1816 char poolname[MAXPATHLEN]; 1817 pool_conf_t *poolconf; 1818 pool_t *pool; 1819 int status; 1820 int error; 1821 1822 /* 1823 * This ends up being very similar to the check done in zoneadmd. 1824 */ 1825 error = zonecfg_get_pool(handle, poolname, sizeof (poolname)); 1826 if (error == Z_NO_ENTRY || (error == Z_OK && strlen(poolname) == 0)) { 1827 /* 1828 * No pool specified. 1829 */ 1830 return (0); 1831 } 1832 if (error != Z_OK) { 1833 zperror(gettext("Unable to retrieve pool name from " 1834 "configuration"), B_TRUE); 1835 return (error); 1836 } 1837 /* 1838 * Don't do anything if pools aren't enabled. 1839 */ 1840 if (pool_get_status(&status) != PO_SUCCESS || status != POOL_ENABLED) { 1841 zerror(gettext("WARNING: pools facility not active; " 1842 "zone will not be bound to pool '%s'."), poolname); 1843 return (Z_OK); 1844 } 1845 /* 1846 * Try to provide a sane error message if the requested pool doesn't 1847 * exist. It isn't clear that pools-related failures should 1848 * necessarily translate to a failure to verify the zone configuration, 1849 * hence they are not considered errors. 1850 */ 1851 if ((poolconf = pool_conf_alloc()) == NULL) { 1852 zerror(gettext("WARNING: pool_conf_alloc failed; " 1853 "using default pool")); 1854 return (Z_OK); 1855 } 1856 if (pool_conf_open(poolconf, pool_dynamic_location(), PO_RDONLY) != 1857 PO_SUCCESS) { 1858 zerror(gettext("WARNING: pool_conf_open failed; " 1859 "using default pool")); 1860 pool_conf_free(poolconf); 1861 return (Z_OK); 1862 } 1863 pool = pool_get_pool(poolconf, poolname); 1864 (void) pool_conf_close(poolconf); 1865 pool_conf_free(poolconf); 1866 if (pool == NULL) { 1867 zerror(gettext("WARNING: pool '%s' not found. " 1868 "using default pool"), poolname); 1869 } 1870 1871 return (Z_OK); 1872 } 1873 1874 static int 1875 verify_ipd(zone_dochandle_t handle) 1876 { 1877 int return_code = Z_OK; 1878 struct zone_fstab fstab; 1879 struct stat st; 1880 char specdir[MAXPATHLEN]; 1881 1882 if (zonecfg_setipdent(handle) != Z_OK) { 1883 /* 1884 * TRANSLATION_NOTE 1885 * inherit-pkg-dirs is a literal that should not be translated. 1886 */ 1887 (void) fprintf(stderr, gettext("could not verify " 1888 "inherit-pkg-dirs: unable to enumerate mounts\n")); 1889 return (Z_ERR); 1890 } 1891 while (zonecfg_getipdent(handle, &fstab) == Z_OK) { 1892 /* 1893 * Verify fs_dir exists. 1894 */ 1895 (void) snprintf(specdir, sizeof (specdir), "%s%s", 1896 zonecfg_get_root(), fstab.zone_fs_dir); 1897 if (stat(specdir, &st) != 0) { 1898 /* 1899 * TRANSLATION_NOTE 1900 * inherit-pkg-dir is a literal that should not be 1901 * translated. 1902 */ 1903 (void) fprintf(stderr, gettext("could not verify " 1904 "inherit-pkg-dir %s: %s\n"), 1905 fstab.zone_fs_dir, strerror(errno)); 1906 return_code = Z_ERR; 1907 } 1908 if (strcmp(st.st_fstype, MNTTYPE_NFS) == 0) { 1909 /* 1910 * TRANSLATION_NOTE 1911 * inherit-pkg-dir and NFS are literals that should 1912 * not be translated. 1913 */ 1914 (void) fprintf(stderr, gettext("cannot verify " 1915 "inherit-pkg-dir %s: NFS mounted file-system.\n" 1916 "\tA local file-system must be used.\n"), 1917 fstab.zone_fs_dir); 1918 return_code = Z_ERR; 1919 } 1920 } 1921 (void) zonecfg_endipdent(handle); 1922 1923 return (return_code); 1924 } 1925 1926 static int 1927 verify_filesystems(zone_dochandle_t handle) 1928 { 1929 int return_code = Z_OK; 1930 struct zone_fstab fstab; 1931 char cmdbuf[MAXPATHLEN]; 1932 struct stat st; 1933 1934 /* 1935 * No need to verify inherit-pkg-dir fs types, as their type is 1936 * implicitly lofs, which is known. Therefore, the types are only 1937 * verified for regular filesystems below. 1938 * 1939 * Since the actual mount point is not known until the dependent mounts 1940 * are performed, we don't attempt any path validation here: that will 1941 * happen later when zoneadmd actually does the mounts. 1942 */ 1943 if (zonecfg_setfsent(handle) != Z_OK) { 1944 (void) fprintf(stderr, gettext("could not verify file-systems: " 1945 "unable to enumerate mounts\n")); 1946 return (Z_ERR); 1947 } 1948 while (zonecfg_getfsent(handle, &fstab) == Z_OK) { 1949 if (!zonecfg_valid_fs_type(fstab.zone_fs_type)) { 1950 (void) fprintf(stderr, gettext("cannot verify fs %s: " 1951 "type %s is not allowed.\n"), fstab.zone_fs_dir, 1952 fstab.zone_fs_type); 1953 return_code = Z_ERR; 1954 goto next_fs; 1955 } 1956 /* 1957 * Verify /usr/lib/fs/<fstype>/mount exists. 1958 */ 1959 if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/mount", 1960 fstab.zone_fs_type) > sizeof (cmdbuf)) { 1961 (void) fprintf(stderr, gettext("cannot verify fs %s: " 1962 "type %s is too long.\n"), fstab.zone_fs_dir, 1963 fstab.zone_fs_type); 1964 return_code = Z_ERR; 1965 goto next_fs; 1966 } 1967 if (stat(cmdbuf, &st) != 0) { 1968 (void) fprintf(stderr, gettext("could not verify fs " 1969 "%s: could not access %s: %s\n"), fstab.zone_fs_dir, 1970 cmdbuf, strerror(errno)); 1971 return_code = Z_ERR; 1972 goto next_fs; 1973 } 1974 if (!S_ISREG(st.st_mode)) { 1975 (void) fprintf(stderr, gettext("could not verify fs " 1976 "%s: %s is not a regular file\n"), 1977 fstab.zone_fs_dir, cmdbuf); 1978 return_code = Z_ERR; 1979 goto next_fs; 1980 } 1981 /* 1982 * Verify /usr/lib/fs/<fstype>/fsck exists iff zone_fs_raw is 1983 * set. 1984 */ 1985 if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/fsck", 1986 fstab.zone_fs_type) > sizeof (cmdbuf)) { 1987 (void) fprintf(stderr, gettext("cannot verify fs %s: " 1988 "type %s is too long.\n"), fstab.zone_fs_dir, 1989 fstab.zone_fs_type); 1990 return_code = Z_ERR; 1991 goto next_fs; 1992 } 1993 if (fstab.zone_fs_raw[0] == '\0' && stat(cmdbuf, &st) == 0) { 1994 (void) fprintf(stderr, gettext("could not verify fs " 1995 "%s: must specify 'raw' device for %s " 1996 "file-systems\n"), 1997 fstab.zone_fs_dir, fstab.zone_fs_type); 1998 return_code = Z_ERR; 1999 goto next_fs; 2000 } 2001 if (fstab.zone_fs_raw[0] != '\0' && 2002 (stat(cmdbuf, &st) != 0 || !S_ISREG(st.st_mode))) { 2003 (void) fprintf(stderr, gettext("cannot verify fs %s: " 2004 "'raw' device specified but " 2005 "no fsck executable exists for %s\n"), 2006 fstab.zone_fs_dir, fstab.zone_fs_type); 2007 return_code = Z_ERR; 2008 goto next_fs; 2009 } 2010 /* 2011 * Verify fs_special and optionally fs_raw, exists. 2012 */ 2013 if (stat(fstab.zone_fs_special, &st) != 0) { 2014 (void) fprintf(stderr, gettext("could not verify fs " 2015 "%s: could not access %s: %s\n"), fstab.zone_fs_dir, 2016 fstab.zone_fs_special, strerror(errno)); 2017 return_code = Z_ERR; 2018 goto next_fs; 2019 } 2020 if (strcmp(st.st_fstype, MNTTYPE_NFS) == 0) { 2021 /* 2022 * TRANSLATION_NOTE 2023 * fs and NFS are literals that should 2024 * not be translated. 2025 */ 2026 (void) fprintf(stderr, gettext("cannot verify " 2027 "fs %s: NFS mounted file-system.\n" 2028 "\tA local file-system must be used.\n"), 2029 fstab.zone_fs_special); 2030 return_code = Z_ERR; 2031 goto next_fs; 2032 } 2033 if (fstab.zone_fs_raw[0] != '\0' && 2034 stat(fstab.zone_fs_raw, &st) != 0) { 2035 /* 2036 * TRANSLATION_NOTE 2037 * fs is a literal that should not be translated. 2038 */ 2039 (void) fprintf(stderr, gettext("could not verify fs " 2040 "%s: could not access %s: %s\n"), fstab.zone_fs_dir, 2041 fstab.zone_fs_raw, strerror(errno)); 2042 return_code = Z_ERR; 2043 goto next_fs; 2044 } 2045 next_fs: 2046 zonecfg_free_fs_option_list(fstab.zone_fs_options); 2047 } 2048 (void) zonecfg_endfsent(handle); 2049 2050 return (return_code); 2051 } 2052 2053 const char *current_dataset; 2054 2055 /* 2056 * Custom error handler for errors incurred as part of the checks below. We 2057 * want to trim off the leading 'cannot open ...' to create a better error 2058 * message. The only other way this can fail is if we fail to set the 'zoned' 2059 * property. In this case we just pass the error on verbatim. 2060 */ 2061 static void 2062 zfs_error_handler(const char *fmt, va_list ap) 2063 { 2064 char buf[1024]; 2065 2066 (void) vsnprintf(buf, sizeof (buf), fmt, ap); 2067 2068 if (strncmp(gettext("cannot open "), buf, 2069 strlen(gettext("cannot open "))) == 0) 2070 /* 2071 * TRANSLATION_NOTE 2072 * zfs and dataset are literals that should not be translated. 2073 */ 2074 (void) fprintf(stderr, gettext("could not verify zfs " 2075 "dataset %s%s\n"), current_dataset, strchr(buf, ':')); 2076 else 2077 (void) fprintf(stderr, gettext("could not verify zfs dataset " 2078 "%s: %s\n"), current_dataset, buf); 2079 } 2080 2081 /* ARGSUSED */ 2082 static int 2083 check_zvol(zfs_handle_t *zhp, void *unused) 2084 { 2085 int ret; 2086 2087 if (zfs_get_type(zhp) == ZFS_TYPE_VOLUME) { 2088 /* 2089 * TRANSLATION_NOTE 2090 * zfs and dataset are literals that should not be translated. 2091 */ 2092 (void) fprintf(stderr, gettext("cannot verify zfs dataset %s: " 2093 "volumes cannot be specified as a zone dataset resource\n"), 2094 zfs_get_name(zhp)); 2095 ret = -1; 2096 } else { 2097 ret = zfs_iter_children(zhp, check_zvol, NULL); 2098 } 2099 2100 zfs_close(zhp); 2101 2102 return (ret); 2103 } 2104 2105 /* 2106 * Validate that the given dataset exists on the system, and that neither it nor 2107 * its children are zvols. 2108 * 2109 * Note that we don't do anything with the 'zoned' property here. All 2110 * management is done in zoneadmd when the zone is actually rebooted. This 2111 * allows us to automatically set the zoned property even when a zone is 2112 * rebooted by the administrator. 2113 */ 2114 static int 2115 verify_datasets(zone_dochandle_t handle) 2116 { 2117 int return_code = Z_OK; 2118 struct zone_dstab dstab; 2119 zfs_handle_t *zhp; 2120 char propbuf[ZFS_MAXPROPLEN]; 2121 char source[ZFS_MAXNAMELEN]; 2122 zfs_source_t srctype; 2123 2124 if (zonecfg_setdsent(handle) != Z_OK) { 2125 /* 2126 * TRANSLATION_NOTE 2127 * zfs and dataset are literals that should not be translated. 2128 */ 2129 (void) fprintf(stderr, gettext("could not verify zfs datasets: " 2130 "unable to enumerate datasets\n")); 2131 return (Z_ERR); 2132 } 2133 2134 zfs_set_error_handler(zfs_error_handler); 2135 2136 while (zonecfg_getdsent(handle, &dstab) == Z_OK) { 2137 2138 current_dataset = dstab.zone_dataset_name; 2139 2140 if ((zhp = zfs_open(dstab.zone_dataset_name, 2141 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME)) == NULL) { 2142 return_code = Z_ERR; 2143 continue; 2144 } 2145 2146 if (zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, propbuf, 2147 sizeof (propbuf), &srctype, source, 2148 sizeof (source), 0) == 0 && 2149 (srctype == ZFS_SRC_INHERITED)) { 2150 (void) fprintf(stderr, gettext("could not verify zfs " 2151 "dataset %s: mountpoint cannot be inherited\n"), 2152 dstab.zone_dataset_name); 2153 return_code = Z_ERR; 2154 zfs_close(zhp); 2155 continue; 2156 } 2157 2158 if (zfs_get_type(zhp) == ZFS_TYPE_VOLUME) { 2159 (void) fprintf(stderr, gettext("cannot verify zfs " 2160 "dataset %s: volumes cannot be specified as a " 2161 "zone dataset resource\n"), 2162 dstab.zone_dataset_name); 2163 return_code = Z_ERR; 2164 } 2165 2166 if (zfs_iter_children(zhp, check_zvol, NULL) != 0) 2167 return_code = Z_ERR; 2168 2169 zfs_close(zhp); 2170 } 2171 (void) zonecfg_enddsent(handle); 2172 2173 return (return_code); 2174 } 2175 2176 static int 2177 verify_details(int cmd_num) 2178 { 2179 zone_dochandle_t handle; 2180 struct zone_nwiftab nwiftab; 2181 char zonepath[MAXPATHLEN], checkpath[MAXPATHLEN]; 2182 int return_code = Z_OK; 2183 int err; 2184 boolean_t in_alt_root; 2185 2186 if ((handle = zonecfg_init_handle()) == NULL) { 2187 zperror(cmd_to_str(cmd_num), B_TRUE); 2188 return (Z_ERR); 2189 } 2190 if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) { 2191 errno = err; 2192 zperror(cmd_to_str(cmd_num), B_TRUE); 2193 zonecfg_fini_handle(handle); 2194 return (Z_ERR); 2195 } 2196 if ((err = zonecfg_get_zonepath(handle, zonepath, sizeof (zonepath))) != 2197 Z_OK) { 2198 errno = err; 2199 zperror(cmd_to_str(cmd_num), B_TRUE); 2200 zonecfg_fini_handle(handle); 2201 return (Z_ERR); 2202 } 2203 /* 2204 * zonecfg_get_zonepath() gets its data from the XML repository. 2205 * Verify this against the index file, which is checked first by 2206 * zone_get_zonepath(). If they don't match, bail out. 2207 */ 2208 if ((err = zone_get_zonepath(target_zone, checkpath, 2209 sizeof (checkpath))) != Z_OK) { 2210 errno = err; 2211 zperror2(target_zone, gettext("could not get zone path")); 2212 return (Z_ERR); 2213 } 2214 if (strcmp(zonepath, checkpath) != 0) { 2215 /* 2216 * TRANSLATION_NOTE 2217 * XML and zonepath are literals that should not be translated. 2218 */ 2219 (void) fprintf(stderr, gettext("The XML repository has " 2220 "zonepath '%s',\nbut the index file has zonepath '%s'.\n" 2221 "These must match, so fix the incorrect entry.\n"), 2222 zonepath, checkpath); 2223 return (Z_ERR); 2224 } 2225 if (validate_zonepath(zonepath, cmd_num) != Z_OK) { 2226 (void) fprintf(stderr, gettext("could not verify zonepath %s " 2227 "because of the above errors.\n"), zonepath); 2228 return_code = Z_ERR; 2229 } 2230 2231 in_alt_root = zonecfg_in_alt_root(); 2232 if (in_alt_root) 2233 goto no_net; 2234 2235 if ((err = zonecfg_setnwifent(handle)) != Z_OK) { 2236 errno = err; 2237 zperror(cmd_to_str(cmd_num), B_TRUE); 2238 zonecfg_fini_handle(handle); 2239 return (Z_ERR); 2240 } 2241 while (zonecfg_getnwifent(handle, &nwiftab) == Z_OK) { 2242 struct lifreq lifr; 2243 sa_family_t af; 2244 int so, res; 2245 2246 /* skip any loopback interfaces */ 2247 if (strcmp(nwiftab.zone_nwif_physical, "lo0") == 0) 2248 continue; 2249 if ((res = zonecfg_valid_net_address(nwiftab.zone_nwif_address, 2250 &lifr)) != Z_OK) { 2251 (void) fprintf(stderr, gettext("could not verify %s " 2252 "%s=%s %s=%s: %s\n"), "net", "address", 2253 nwiftab.zone_nwif_address, "physical", 2254 nwiftab.zone_nwif_physical, zonecfg_strerror(res)); 2255 return_code = Z_ERR; 2256 continue; 2257 } 2258 af = lifr.lifr_addr.ss_family; 2259 (void) memset(&lifr, 0, sizeof (lifr)); 2260 (void) strlcpy(lifr.lifr_name, nwiftab.zone_nwif_physical, 2261 sizeof (lifr.lifr_name)); 2262 lifr.lifr_addr.ss_family = af; 2263 if ((so = socket(af, SOCK_DGRAM, 0)) < 0) { 2264 (void) fprintf(stderr, gettext("could not verify %s " 2265 "%s=%s %s=%s: could not get socket: %s\n"), "net", 2266 "address", nwiftab.zone_nwif_address, "physical", 2267 nwiftab.zone_nwif_physical, strerror(errno)); 2268 return_code = Z_ERR; 2269 continue; 2270 } 2271 if (ioctl(so, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) { 2272 (void) fprintf(stderr, 2273 gettext("could not verify %s %s=%s %s=%s: %s\n"), 2274 "net", "address", nwiftab.zone_nwif_address, 2275 "physical", nwiftab.zone_nwif_physical, 2276 strerror(errno)); 2277 return_code = Z_ERR; 2278 } 2279 (void) close(so); 2280 } 2281 (void) zonecfg_endnwifent(handle); 2282 no_net: 2283 2284 if (verify_filesystems(handle) != Z_OK) 2285 return_code = Z_ERR; 2286 if (verify_ipd(handle) != Z_OK) 2287 return_code = Z_ERR; 2288 if (!in_alt_root && verify_rctls(handle) != Z_OK) 2289 return_code = Z_ERR; 2290 if (!in_alt_root && verify_pool(handle) != Z_OK) 2291 return_code = Z_ERR; 2292 if (!in_alt_root && verify_datasets(handle) != Z_OK) 2293 return_code = Z_ERR; 2294 zonecfg_fini_handle(handle); 2295 if (return_code == Z_ERR) 2296 (void) fprintf(stderr, 2297 gettext("%s: zone %s failed to verify\n"), 2298 execname, target_zone); 2299 return (return_code); 2300 } 2301 2302 static int 2303 verify_func(int argc, char *argv[]) 2304 { 2305 int arg; 2306 2307 optind = 0; 2308 if ((arg = getopt(argc, argv, "?")) != EOF) { 2309 switch (arg) { 2310 case '?': 2311 sub_usage(SHELP_VERIFY, CMD_VERIFY); 2312 return (optopt == '?' ? Z_OK : Z_USAGE); 2313 default: 2314 sub_usage(SHELP_VERIFY, CMD_VERIFY); 2315 return (Z_USAGE); 2316 } 2317 } 2318 if (argc > optind) { 2319 sub_usage(SHELP_VERIFY, CMD_VERIFY); 2320 return (Z_USAGE); 2321 } 2322 if (sanity_check(target_zone, CMD_VERIFY, B_FALSE, B_FALSE) != Z_OK) 2323 return (Z_ERR); 2324 return (verify_details(CMD_VERIFY)); 2325 } 2326 2327 #define LUCREATEZONE "/usr/lib/lu/lucreatezone" 2328 2329 static int 2330 install_func(int argc, char *argv[]) 2331 { 2332 /* 9: "exec " and " -z " */ 2333 char cmdbuf[sizeof (LUCREATEZONE) + ZONENAME_MAX + 9]; 2334 int lockfd; 2335 int err, arg; 2336 char zonepath[MAXPATHLEN]; 2337 int status; 2338 2339 if (zonecfg_in_alt_root()) { 2340 zerror(gettext("cannot install zone in alternate root")); 2341 return (Z_ERR); 2342 } 2343 2344 optind = 0; 2345 if ((arg = getopt(argc, argv, "?")) != EOF) { 2346 switch (arg) { 2347 case '?': 2348 sub_usage(SHELP_INSTALL, CMD_INSTALL); 2349 return (optopt == '?' ? Z_OK : Z_USAGE); 2350 default: 2351 sub_usage(SHELP_INSTALL, CMD_INSTALL); 2352 return (Z_USAGE); 2353 } 2354 } 2355 if (argc > optind) { 2356 sub_usage(SHELP_INSTALL, CMD_INSTALL); 2357 return (Z_USAGE); 2358 } 2359 if (sanity_check(target_zone, CMD_INSTALL, B_FALSE, B_TRUE) != Z_OK) 2360 return (Z_ERR); 2361 if (verify_details(CMD_INSTALL) != Z_OK) 2362 return (Z_ERR); 2363 2364 if (grab_lock_file(target_zone, &lockfd) != Z_OK) { 2365 zerror(gettext("another %s may have an operation in progress."), 2366 "zoneadm"); 2367 return (Z_ERR); 2368 } 2369 err = zone_set_state(target_zone, ZONE_STATE_INCOMPLETE); 2370 if (err != Z_OK) { 2371 errno = err; 2372 zperror2(target_zone, gettext("could not set state")); 2373 goto done; 2374 } 2375 2376 /* 2377 * According to the Application Packaging Developer's Guide, a 2378 * "checkinstall" script when included in a package is executed as 2379 * the user "install", if such a user exists, or by the user 2380 * "nobody". In order to support this dubious behavior, the path 2381 * to the zone being constructed is opened up during the life of 2382 * the command laying down the zone's root file system. Once this 2383 * has completed, regardless of whether it was successful, the 2384 * path to the zone is again restricted. 2385 */ 2386 if ((err = zone_get_zonepath(target_zone, zonepath, 2387 sizeof (zonepath))) != Z_OK) { 2388 errno = err; 2389 zperror2(target_zone, gettext("could not get zone path")); 2390 goto done; 2391 } 2392 if (chmod(zonepath, DEFAULT_DIR_MODE) != 0) { 2393 zperror(zonepath, B_FALSE); 2394 err = Z_ERR; 2395 goto done; 2396 } 2397 2398 /* 2399 * "exec" the command so that the returned status is that of 2400 * LUCREATEZONE and not the shell. 2401 */ 2402 (void) snprintf(cmdbuf, sizeof (cmdbuf), "exec " LUCREATEZONE " -z %s", 2403 target_zone); 2404 status = do_subproc(cmdbuf); 2405 if (chmod(zonepath, S_IRWXU) != 0) { 2406 zperror(zonepath, B_FALSE); 2407 err = Z_ERR; 2408 goto done; 2409 } 2410 if ((err = subproc_status(LUCREATEZONE, status)) != Z_OK) 2411 goto done; 2412 2413 if ((err = zone_set_state(target_zone, ZONE_STATE_INSTALLED)) != Z_OK) { 2414 errno = err; 2415 zperror2(target_zone, gettext("could not set state")); 2416 goto done; 2417 } 2418 2419 done: 2420 release_lock_file(lockfd); 2421 return ((err == Z_OK) ? Z_OK : Z_ERR); 2422 } 2423 2424 /* 2425 * Check that the inherited pkg dirs are the same for the clone and its source. 2426 * The easiest way to do that is check that the list of ipds is the same 2427 * by matching each one against the other. This algorithm should be fine since 2428 * the list of ipds should not be that long. 2429 */ 2430 static int 2431 valid_ipd_clone(zone_dochandle_t s_handle, char *source_zone, 2432 zone_dochandle_t t_handle, char *target_zone) 2433 { 2434 int err; 2435 int res = Z_OK; 2436 int s_cnt = 0; 2437 int t_cnt = 0; 2438 struct zone_fstab s_fstab; 2439 struct zone_fstab t_fstab; 2440 2441 /* 2442 * First check the source of the clone against the target. 2443 */ 2444 if ((err = zonecfg_setipdent(s_handle)) != Z_OK) { 2445 errno = err; 2446 zperror2(source_zone, gettext("could not enumerate " 2447 "inherit-pkg-dirs")); 2448 return (Z_ERR); 2449 } 2450 2451 while (zonecfg_getipdent(s_handle, &s_fstab) == Z_OK) { 2452 boolean_t match = B_FALSE; 2453 2454 s_cnt++; 2455 2456 if ((err = zonecfg_setipdent(t_handle)) != Z_OK) { 2457 errno = err; 2458 zperror2(target_zone, gettext("could not enumerate " 2459 "inherit-pkg-dirs")); 2460 (void) zonecfg_endipdent(s_handle); 2461 return (Z_ERR); 2462 } 2463 2464 while (zonecfg_getipdent(t_handle, &t_fstab) == Z_OK) { 2465 if (strcmp(s_fstab.zone_fs_dir, t_fstab.zone_fs_dir) 2466 == 0) { 2467 match = B_TRUE; 2468 break; 2469 } 2470 } 2471 (void) zonecfg_endipdent(t_handle); 2472 2473 if (!match) { 2474 (void) fprintf(stderr, gettext("inherit-pkg-dir " 2475 "'%s' is not configured in zone %s.\n"), 2476 s_fstab.zone_fs_dir, target_zone); 2477 res = Z_ERR; 2478 } 2479 } 2480 2481 (void) zonecfg_endipdent(s_handle); 2482 2483 /* skip the next check if we already have errors */ 2484 if (res == Z_ERR) 2485 return (res); 2486 2487 /* 2488 * Now check the number of ipds in the target so we can verify 2489 * that the source is not a subset of the target. 2490 */ 2491 if ((err = zonecfg_setipdent(t_handle)) != Z_OK) { 2492 errno = err; 2493 zperror2(target_zone, gettext("could not enumerate " 2494 "inherit-pkg-dirs")); 2495 return (Z_ERR); 2496 } 2497 2498 while (zonecfg_getipdent(t_handle, &t_fstab) == Z_OK) 2499 t_cnt++; 2500 2501 (void) zonecfg_endipdent(t_handle); 2502 2503 if (t_cnt != s_cnt) { 2504 (void) fprintf(stderr, gettext("Zone %s is configured " 2505 "with inherit-pkg-dirs that are not configured in zone " 2506 "%s.\n"), target_zone, source_zone); 2507 res = Z_ERR; 2508 } 2509 2510 return (res); 2511 } 2512 2513 static void 2514 warn_dev_match(zone_dochandle_t s_handle, char *source_zone, 2515 zone_dochandle_t t_handle, char *target_zone) 2516 { 2517 int err; 2518 struct zone_devtab s_devtab; 2519 struct zone_devtab t_devtab; 2520 2521 if ((err = zonecfg_setdevent(t_handle)) != Z_OK) { 2522 errno = err; 2523 zperror2(target_zone, gettext("could not enumerate devices")); 2524 return; 2525 } 2526 2527 while (zonecfg_getdevent(t_handle, &t_devtab) == Z_OK) { 2528 if ((err = zonecfg_setdevent(s_handle)) != Z_OK) { 2529 errno = err; 2530 zperror2(source_zone, 2531 gettext("could not enumerate devices")); 2532 (void) zonecfg_enddevent(t_handle); 2533 return; 2534 } 2535 2536 while (zonecfg_getdevent(s_handle, &s_devtab) == Z_OK) { 2537 /* 2538 * Use fnmatch to catch the case where wildcards 2539 * were used in one zone and the other has an 2540 * explicit entry (e.g. /dev/dsk/c0t0d0s6 vs. 2541 * /dev/\*dsk/c0t0d0s6). 2542 */ 2543 if (fnmatch(t_devtab.zone_dev_match, 2544 s_devtab.zone_dev_match, FNM_PATHNAME) == 0 || 2545 fnmatch(s_devtab.zone_dev_match, 2546 t_devtab.zone_dev_match, FNM_PATHNAME) == 0) { 2547 (void) fprintf(stderr, 2548 gettext("WARNING: device '%s' " 2549 "is configured in both zones.\n"), 2550 t_devtab.zone_dev_match); 2551 break; 2552 } 2553 } 2554 (void) zonecfg_enddevent(s_handle); 2555 } 2556 2557 (void) zonecfg_enddevent(t_handle); 2558 } 2559 2560 /* 2561 * Check if the specified mount option (opt) is contained within the 2562 * options string. 2563 */ 2564 static boolean_t 2565 opt_match(char *opt, char *options) 2566 { 2567 char *p; 2568 char *lastp; 2569 2570 if ((p = strtok_r(options, ",", &lastp)) != NULL) { 2571 if (strcmp(p, opt) == 0) 2572 return (B_TRUE); 2573 while ((p = strtok_r(NULL, ",", &lastp)) != NULL) { 2574 if (strcmp(p, opt) == 0) 2575 return (B_TRUE); 2576 } 2577 } 2578 2579 return (B_FALSE); 2580 } 2581 2582 #define RW_LOFS "WARNING: read-write lofs file-system on '%s' is configured " \ 2583 "in both zones.\n" 2584 2585 static void 2586 print_fs_warnings(struct zone_fstab *s_fstab, struct zone_fstab *t_fstab) 2587 { 2588 /* 2589 * It is ok to have shared lofs mounted fs but we want to warn if 2590 * either is rw since this will effect the other zone. 2591 */ 2592 if (strcmp(t_fstab->zone_fs_type, "lofs") == 0) { 2593 zone_fsopt_t *optp; 2594 2595 /* The default is rw so no options means rw */ 2596 if (t_fstab->zone_fs_options == NULL || 2597 s_fstab->zone_fs_options == NULL) { 2598 (void) fprintf(stderr, gettext(RW_LOFS), 2599 t_fstab->zone_fs_special); 2600 return; 2601 } 2602 2603 for (optp = s_fstab->zone_fs_options; optp != NULL; 2604 optp = optp->zone_fsopt_next) { 2605 if (opt_match("rw", optp->zone_fsopt_opt)) { 2606 (void) fprintf(stderr, gettext(RW_LOFS), 2607 s_fstab->zone_fs_special); 2608 return; 2609 } 2610 } 2611 2612 for (optp = t_fstab->zone_fs_options; optp != NULL; 2613 optp = optp->zone_fsopt_next) { 2614 if (opt_match("rw", optp->zone_fsopt_opt)) { 2615 (void) fprintf(stderr, gettext(RW_LOFS), 2616 t_fstab->zone_fs_special); 2617 return; 2618 } 2619 } 2620 2621 return; 2622 } 2623 2624 /* 2625 * TRANSLATION_NOTE 2626 * The first variable is the file-system type and the second is 2627 * the file-system special device. For example, 2628 * WARNING: ufs file-system on '/dev/dsk/c0t0d0s0' ... 2629 */ 2630 (void) fprintf(stderr, gettext("WARNING: %s file-system on '%s' " 2631 "is configured in both zones.\n"), t_fstab->zone_fs_type, 2632 t_fstab->zone_fs_special); 2633 } 2634 2635 static void 2636 warn_fs_match(zone_dochandle_t s_handle, char *source_zone, 2637 zone_dochandle_t t_handle, char *target_zone) 2638 { 2639 int err; 2640 struct zone_fstab s_fstab; 2641 struct zone_fstab t_fstab; 2642 2643 if ((err = zonecfg_setfsent(t_handle)) != Z_OK) { 2644 errno = err; 2645 zperror2(target_zone, 2646 gettext("could not enumerate file-systems")); 2647 return; 2648 } 2649 2650 while (zonecfg_getfsent(t_handle, &t_fstab) == Z_OK) { 2651 if ((err = zonecfg_setfsent(s_handle)) != Z_OK) { 2652 errno = err; 2653 zperror2(source_zone, 2654 gettext("could not enumerate file-systems")); 2655 (void) zonecfg_endfsent(t_handle); 2656 return; 2657 } 2658 2659 while (zonecfg_getfsent(s_handle, &s_fstab) == Z_OK) { 2660 if (strcmp(t_fstab.zone_fs_special, 2661 s_fstab.zone_fs_special) == 0) { 2662 print_fs_warnings(&s_fstab, &t_fstab); 2663 break; 2664 } 2665 } 2666 (void) zonecfg_endfsent(s_handle); 2667 } 2668 2669 (void) zonecfg_endfsent(t_handle); 2670 } 2671 2672 /* 2673 * We don't catch the case where you used the same IP address but 2674 * it is not an exact string match. For example, 192.9.0.128 vs. 192.09.0.128. 2675 * However, we're not going to worry about that but we will check for 2676 * a possible netmask on one of the addresses (e.g. 10.0.0.1 and 10.0.0.1/24) 2677 * and handle that case as a match. 2678 */ 2679 static void 2680 warn_ip_match(zone_dochandle_t s_handle, char *source_zone, 2681 zone_dochandle_t t_handle, char *target_zone) 2682 { 2683 int err; 2684 struct zone_nwiftab s_nwiftab; 2685 struct zone_nwiftab t_nwiftab; 2686 2687 if ((err = zonecfg_setnwifent(t_handle)) != Z_OK) { 2688 errno = err; 2689 zperror2(target_zone, 2690 gettext("could not enumerate network interfaces")); 2691 return; 2692 } 2693 2694 while (zonecfg_getnwifent(t_handle, &t_nwiftab) == Z_OK) { 2695 char *p; 2696 2697 /* remove an (optional) netmask from the address */ 2698 if ((p = strchr(t_nwiftab.zone_nwif_address, '/')) != NULL) 2699 *p = '\0'; 2700 2701 if ((err = zonecfg_setnwifent(s_handle)) != Z_OK) { 2702 errno = err; 2703 zperror2(source_zone, 2704 gettext("could not enumerate network interfaces")); 2705 (void) zonecfg_endnwifent(t_handle); 2706 return; 2707 } 2708 2709 while (zonecfg_getnwifent(s_handle, &s_nwiftab) == Z_OK) { 2710 /* remove an (optional) netmask from the address */ 2711 if ((p = strchr(s_nwiftab.zone_nwif_address, '/')) 2712 != NULL) 2713 *p = '\0'; 2714 2715 if (strcmp(t_nwiftab.zone_nwif_address, 2716 s_nwiftab.zone_nwif_address) == 0) { 2717 (void) fprintf(stderr, 2718 gettext("WARNING: network address '%s' " 2719 "is configured in both zones.\n"), 2720 t_nwiftab.zone_nwif_address); 2721 break; 2722 } 2723 } 2724 (void) zonecfg_endnwifent(s_handle); 2725 } 2726 2727 (void) zonecfg_endnwifent(t_handle); 2728 } 2729 2730 static void 2731 warn_dataset_match(zone_dochandle_t s_handle, char *source_zone, 2732 zone_dochandle_t t_handle, char *target_zone) 2733 { 2734 int err; 2735 struct zone_dstab s_dstab; 2736 struct zone_dstab t_dstab; 2737 2738 if ((err = zonecfg_setdsent(t_handle)) != Z_OK) { 2739 errno = err; 2740 zperror2(target_zone, gettext("could not enumerate datasets")); 2741 return; 2742 } 2743 2744 while (zonecfg_getdsent(t_handle, &t_dstab) == Z_OK) { 2745 if ((err = zonecfg_setdsent(s_handle)) != Z_OK) { 2746 errno = err; 2747 zperror2(source_zone, 2748 gettext("could not enumerate datasets")); 2749 (void) zonecfg_enddsent(t_handle); 2750 return; 2751 } 2752 2753 while (zonecfg_getdsent(s_handle, &s_dstab) == Z_OK) { 2754 if (strcmp(t_dstab.zone_dataset_name, 2755 s_dstab.zone_dataset_name) == 0) { 2756 (void) fprintf(stderr, 2757 gettext("WARNING: dataset '%s' " 2758 "is configured in both zones.\n"), 2759 t_dstab.zone_dataset_name); 2760 break; 2761 } 2762 } 2763 (void) zonecfg_enddsent(s_handle); 2764 } 2765 2766 (void) zonecfg_enddsent(t_handle); 2767 } 2768 2769 static int 2770 validate_clone(char *source_zone, char *target_zone) 2771 { 2772 int err = Z_OK; 2773 zone_dochandle_t s_handle; 2774 zone_dochandle_t t_handle; 2775 2776 if ((t_handle = zonecfg_init_handle()) == NULL) { 2777 zperror(cmd_to_str(CMD_CLONE), B_TRUE); 2778 return (Z_ERR); 2779 } 2780 if ((err = zonecfg_get_handle(target_zone, t_handle)) != Z_OK) { 2781 errno = err; 2782 zperror(cmd_to_str(CMD_CLONE), B_TRUE); 2783 zonecfg_fini_handle(t_handle); 2784 return (Z_ERR); 2785 } 2786 2787 if ((s_handle = zonecfg_init_handle()) == NULL) { 2788 zperror(cmd_to_str(CMD_CLONE), B_TRUE); 2789 zonecfg_fini_handle(t_handle); 2790 return (Z_ERR); 2791 } 2792 if ((err = zonecfg_get_handle(source_zone, s_handle)) != Z_OK) { 2793 errno = err; 2794 zperror(cmd_to_str(CMD_CLONE), B_TRUE); 2795 goto done; 2796 } 2797 2798 /* verify new zone has same inherit-pkg-dirs */ 2799 err = valid_ipd_clone(s_handle, source_zone, t_handle, target_zone); 2800 2801 /* warn about imported fs's which are the same */ 2802 warn_fs_match(s_handle, source_zone, t_handle, target_zone); 2803 2804 /* warn about imported IP addresses which are the same */ 2805 warn_ip_match(s_handle, source_zone, t_handle, target_zone); 2806 2807 /* warn about imported devices which are the same */ 2808 warn_dev_match(s_handle, source_zone, t_handle, target_zone); 2809 2810 /* warn about imported datasets which are the same */ 2811 warn_dataset_match(s_handle, source_zone, t_handle, target_zone); 2812 2813 done: 2814 zonecfg_fini_handle(t_handle); 2815 zonecfg_fini_handle(s_handle); 2816 2817 return ((err == Z_OK) ? Z_OK : Z_ERR); 2818 } 2819 2820 static int 2821 copy_zone(char *src, char *dst) 2822 { 2823 boolean_t out_null = B_FALSE; 2824 int status; 2825 int err; 2826 char *outfile; 2827 char cmdbuf[MAXPATHLEN * 2 + 128]; 2828 2829 if ((outfile = tempnam("/var/log", "zone")) == NULL) { 2830 outfile = "/dev/null"; 2831 out_null = B_TRUE; 2832 } 2833 2834 (void) snprintf(cmdbuf, sizeof (cmdbuf), 2835 "cd %s && /usr/bin/find . -depth -print | " 2836 "/usr/bin/cpio -pdmuP@ %s > %s 2>&1", 2837 src, dst, outfile); 2838 2839 status = do_subproc(cmdbuf); 2840 2841 if ((err = subproc_status("copy", status)) != Z_OK) { 2842 if (!out_null) 2843 (void) fprintf(stderr, gettext("\nThe copy failed.\n" 2844 "More information can be found in %s\n"), outfile); 2845 return (err); 2846 } 2847 2848 if (!out_null) 2849 (void) unlink(outfile); 2850 2851 return (Z_OK); 2852 } 2853 2854 /* 2855 * Wait until the target_zone has booted to single-user. Return Z_OK once 2856 * the zone has booted to that level or return Z_BAD_ZONE_STATE if the zone 2857 * has not booted to single-user after the timeout. 2858 */ 2859 static int 2860 zone_wait_single_user() 2861 { 2862 char cmdbuf[ZONENAME_MAX + 256]; 2863 int retry; 2864 2865 (void) snprintf(cmdbuf, sizeof (cmdbuf), 2866 "test \"`/usr/sbin/zlogin -S %s /usr/bin/svcprop -p " 2867 "restarter/state svc:/milestone/single-user:default 2>/dev/null`\" " 2868 "= \"online\"", 2869 target_zone); 2870 2871 for (retry = 0; retry < SINGLE_USER_RETRY; retry++) { 2872 int status; 2873 2874 status = do_subproc(cmdbuf); 2875 if (WIFEXITED(status)) { 2876 if (WEXITSTATUS(status) == 0) 2877 return (Z_OK); 2878 2879 (void) sleep(2); 2880 } else { 2881 return (Z_BAD_ZONE_STATE); 2882 } 2883 } 2884 2885 return (Z_BAD_ZONE_STATE); 2886 } 2887 2888 2889 /* ARGSUSED */ 2890 int 2891 zfm_print(const char *p, void *r) { 2892 zerror(" %s\n", p); 2893 return (0); 2894 } 2895 2896 static int 2897 clone_func(int argc, char *argv[]) 2898 { 2899 char cmdbuf[MAXPATHLEN]; 2900 char *source_zone = NULL; 2901 int lockfd; 2902 int err, arg; 2903 char zonepath[MAXPATHLEN]; 2904 char source_zonepath[MAXPATHLEN]; 2905 int status; 2906 zone_state_t state; 2907 zone_entry_t *zent; 2908 char *method = "copy"; 2909 char *boot_args[] = { "-s", NULL }; 2910 char *halt_args[] = { NULL }; 2911 struct stat unconfig_buf; 2912 boolean_t revert; 2913 2914 if (zonecfg_in_alt_root()) { 2915 zerror(gettext("cannot clone zone in alternate root")); 2916 return (Z_ERR); 2917 } 2918 2919 optind = 0; 2920 if ((arg = getopt(argc, argv, "?m:")) != EOF) { 2921 switch (arg) { 2922 case '?': 2923 sub_usage(SHELP_CLONE, CMD_CLONE); 2924 return (optopt == '?' ? Z_OK : Z_USAGE); 2925 case 'm': 2926 method = optarg; 2927 break; 2928 default: 2929 sub_usage(SHELP_CLONE, CMD_CLONE); 2930 return (Z_USAGE); 2931 } 2932 } 2933 if (argc != (optind + 1) || strcmp(method, "copy") != 0) { 2934 sub_usage(SHELP_CLONE, CMD_CLONE); 2935 return (Z_USAGE); 2936 } 2937 source_zone = argv[optind]; 2938 if (sanity_check(target_zone, CMD_CLONE, B_FALSE, B_TRUE) != Z_OK) 2939 return (Z_ERR); 2940 if (verify_details(CMD_CLONE) != Z_OK) 2941 return (Z_ERR); 2942 2943 /* 2944 * We also need to do some extra validation on the source zone. 2945 */ 2946 2947 if (strcmp(source_zone, GLOBAL_ZONENAME) == 0) { 2948 zerror(gettext("%s operation is invalid for the global zone."), 2949 cmd_to_str(CMD_CLONE)); 2950 return (Z_ERR); 2951 } 2952 2953 if (strncmp(source_zone, "SUNW", 4) == 0) { 2954 zerror(gettext("%s operation is invalid for zones starting " 2955 "with SUNW."), cmd_to_str(CMD_CLONE)); 2956 return (Z_ERR); 2957 } 2958 2959 zent = lookup_running_zone(source_zone); 2960 if (zent != NULL) { 2961 /* check whether the zone is ready or running */ 2962 if ((err = zone_get_state(zent->zname, &zent->zstate_num)) 2963 != Z_OK) { 2964 errno = err; 2965 zperror2(zent->zname, gettext("could not get state")); 2966 /* can't tell, so hedge */ 2967 zent->zstate_str = "ready/running"; 2968 } else { 2969 zent->zstate_str = zone_state_str(zent->zstate_num); 2970 } 2971 zerror(gettext("%s operation is invalid for %s zones."), 2972 cmd_to_str(CMD_CLONE), zent->zstate_str); 2973 return (Z_ERR); 2974 } 2975 2976 if ((err = zone_get_state(source_zone, &state)) != Z_OK) { 2977 errno = err; 2978 zperror2(source_zone, gettext("could not get state")); 2979 return (Z_ERR); 2980 } 2981 if (state != ZONE_STATE_INSTALLED) { 2982 (void) fprintf(stderr, 2983 gettext("%s: zone %s is %s; %s is required.\n"), 2984 execname, source_zone, zone_state_str(state), 2985 zone_state_str(ZONE_STATE_INSTALLED)); 2986 return (Z_ERR); 2987 } 2988 2989 /* 2990 * The source zone checks out ok, continue with the clone. 2991 */ 2992 2993 if (validate_clone(source_zone, target_zone) != Z_OK) 2994 return (Z_ERR); 2995 2996 if (grab_lock_file(target_zone, &lockfd) != Z_OK) { 2997 zerror(gettext("another %s may have an operation in progress."), 2998 "zoneadm"); 2999 return (Z_ERR); 3000 } 3001 3002 if ((err = zone_get_zonepath(source_zone, source_zonepath, 3003 sizeof (source_zonepath))) != Z_OK) { 3004 errno = err; 3005 zperror2(source_zone, gettext("could not get zone path")); 3006 goto done; 3007 } 3008 3009 if ((err = zone_get_zonepath(target_zone, zonepath, sizeof (zonepath))) 3010 != Z_OK) { 3011 errno = err; 3012 zperror2(target_zone, gettext("could not get zone path")); 3013 goto done; 3014 } 3015 3016 /* Don't clone the zone if anything is still mounted there */ 3017 if (zonecfg_find_mounts(source_zonepath, NULL, NULL)) { 3018 zerror(gettext("These file-systems are mounted on " 3019 "subdirectories of %s.\n"), source_zonepath); 3020 (void) zonecfg_find_mounts(source_zonepath, zfm_print, NULL); 3021 err = Z_ERR; 3022 goto done; 3023 } 3024 3025 if ((err = zone_set_state(target_zone, ZONE_STATE_INCOMPLETE)) 3026 != Z_OK) { 3027 errno = err; 3028 zperror2(target_zone, gettext("could not set state")); 3029 goto done; 3030 } 3031 3032 (void) printf(gettext("Cloning zonepath %s..."), source_zonepath); 3033 (void) fflush(stdout); 3034 3035 if ((err = copy_zone(source_zonepath, zonepath)) != Z_OK) 3036 goto done; 3037 3038 /* 3039 * We have to set the state of the zone to installed so that we 3040 * can boot it and sys-unconfig it from within the zone. However, 3041 * if something fails during the boot/sys-unconfig, we want to set 3042 * the state back to incomplete. We use the revert flag to keep 3043 * track of this. 3044 */ 3045 revert = B_TRUE; 3046 3047 if ((err = zone_set_state(target_zone, ZONE_STATE_INSTALLED)) != Z_OK) { 3048 errno = err; 3049 zperror2(target_zone, gettext("\ncould not set state")); 3050 goto done; 3051 } 3052 3053 /* 3054 * Check if the zone is already sys-unconfiged. This saves us 3055 * the work of booting the zone so we can unconfigure it. 3056 */ 3057 (void) snprintf(cmdbuf, sizeof (cmdbuf), "%s/root/etc/.UNCONFIGURED", 3058 zonepath); 3059 if (stat(cmdbuf, &unconfig_buf) == -1) { 3060 if ((err = boot_func(1, boot_args)) != Z_OK) { 3061 errno = err; 3062 zperror2(target_zone, gettext("\nCould not boot zone " 3063 "for sys-unconfig\n")); 3064 goto done; 3065 } 3066 3067 if ((err = zone_wait_single_user()) != Z_OK) { 3068 errno = err; 3069 zperror2(target_zone, gettext("\nCould not boot zone " 3070 "for sys-unconfig\n")); 3071 (void) halt_func(0, halt_args); 3072 goto done; 3073 } 3074 3075 (void) snprintf(cmdbuf, sizeof (cmdbuf), 3076 "echo y | /usr/sbin/zlogin -S %s /usr/sbin/sys-unconfig", 3077 target_zone); 3078 3079 status = do_subproc(cmdbuf); 3080 if ((err = subproc_status("sys-unconfig", status)) != Z_OK) { 3081 errno = err; 3082 zperror2(target_zone, 3083 gettext("\nsys-unconfig failed\n")); 3084 /* 3085 * The sys-unconfig halts the zone but if it failed, 3086 * for some reason, we'll try to halt it now. 3087 */ 3088 (void) halt_func(0, halt_args); 3089 goto done; 3090 } 3091 } 3092 3093 revert = B_FALSE; 3094 3095 done: 3096 (void) printf("\n"); 3097 3098 if (revert) 3099 (void) zone_set_state(target_zone, ZONE_STATE_INCOMPLETE); 3100 3101 release_lock_file(lockfd); 3102 return ((err == Z_OK) ? Z_OK : Z_ERR); 3103 } 3104 3105 #define RMCOMMAND "/usr/bin/rm -rf" 3106 3107 static int 3108 move_func(int argc, char *argv[]) 3109 { 3110 /* 6: "exec " and " " */ 3111 char cmdbuf[sizeof (RMCOMMAND) + MAXPATHLEN + 6]; 3112 char *new_zonepath = NULL; 3113 int lockfd; 3114 int err, arg; 3115 char zonepath[MAXPATHLEN]; 3116 zone_dochandle_t handle; 3117 boolean_t fast; 3118 boolean_t revert; 3119 struct stat zonepath_buf; 3120 struct stat new_zonepath_buf; 3121 3122 if (zonecfg_in_alt_root()) { 3123 zerror(gettext("cannot move zone in alternate root")); 3124 return (Z_ERR); 3125 } 3126 3127 optind = 0; 3128 if ((arg = getopt(argc, argv, "?")) != EOF) { 3129 switch (arg) { 3130 case '?': 3131 sub_usage(SHELP_MOVE, CMD_MOVE); 3132 return (optopt == '?' ? Z_OK : Z_USAGE); 3133 default: 3134 sub_usage(SHELP_MOVE, CMD_MOVE); 3135 return (Z_USAGE); 3136 } 3137 } 3138 if (argc != (optind + 1)) { 3139 sub_usage(SHELP_MOVE, CMD_MOVE); 3140 return (Z_USAGE); 3141 } 3142 new_zonepath = argv[optind]; 3143 if (sanity_check(target_zone, CMD_MOVE, B_FALSE, B_TRUE) != Z_OK) 3144 return (Z_ERR); 3145 if (verify_details(CMD_MOVE) != Z_OK) 3146 return (Z_ERR); 3147 3148 /* 3149 * Check out the new zonepath. This has the side effect of creating 3150 * a directory for the new zonepath. We depend on this later when we 3151 * stat to see if we are doing a cross file-system move or not. 3152 */ 3153 if (validate_zonepath(new_zonepath, CMD_MOVE) != Z_OK) 3154 return (Z_ERR); 3155 3156 if ((err = zone_get_zonepath(target_zone, zonepath, sizeof (zonepath))) 3157 != Z_OK) { 3158 errno = err; 3159 zperror2(target_zone, gettext("could not get zone path")); 3160 return (Z_ERR); 3161 } 3162 3163 if (stat(zonepath, &zonepath_buf) == -1) { 3164 zperror(gettext("could not stat zone path"), B_FALSE); 3165 return (Z_ERR); 3166 } 3167 3168 if (stat(new_zonepath, &new_zonepath_buf) == -1) { 3169 zperror(gettext("could not stat new zone path"), B_FALSE); 3170 return (Z_ERR); 3171 } 3172 3173 /* Don't move the zone if anything is still mounted there */ 3174 if (zonecfg_find_mounts(zonepath, NULL, NULL)) { 3175 zerror(gettext("These file-systems are mounted on " 3176 "subdirectories of %s.\n"), zonepath); 3177 (void) zonecfg_find_mounts(zonepath, zfm_print, NULL); 3178 return (Z_ERR); 3179 } 3180 3181 /* 3182 * Check if we are moving in the same filesystem and can do a fast 3183 * move or if we are crossing filesystems and have to copy the data. 3184 */ 3185 fast = (zonepath_buf.st_dev == new_zonepath_buf.st_dev); 3186 3187 if ((handle = zonecfg_init_handle()) == NULL) { 3188 zperror(cmd_to_str(CMD_MOVE), B_TRUE); 3189 return (Z_ERR); 3190 } 3191 3192 if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) { 3193 errno = err; 3194 zperror(cmd_to_str(CMD_MOVE), B_TRUE); 3195 zonecfg_fini_handle(handle); 3196 return (Z_ERR); 3197 } 3198 3199 if (grab_lock_file(target_zone, &lockfd) != Z_OK) { 3200 zerror(gettext("another %s may have an operation in progress."), 3201 "zoneadm"); 3202 zonecfg_fini_handle(handle); 3203 return (Z_ERR); 3204 } 3205 3206 /* 3207 * We're making some file-system changes now so we have to clean up 3208 * the file-system before we are done. This will either clean up the 3209 * new zonepath if the zonecfg update failed or it will clean up the 3210 * old zonepath if everything is ok. 3211 */ 3212 revert = B_TRUE; 3213 3214 if (fast) { 3215 /* same filesystem, use rename for a quick move */ 3216 3217 /* 3218 * Remove the new_zonepath directory that got created above 3219 * during the validation. It gets in the way of the rename. 3220 */ 3221 if (rmdir(new_zonepath) != 0) { 3222 zperror(gettext("could not rmdir new zone path"), 3223 B_FALSE); 3224 zonecfg_fini_handle(handle); 3225 release_lock_file(lockfd); 3226 return (Z_ERR); 3227 } 3228 3229 if (rename(zonepath, new_zonepath) != 0) { 3230 /* 3231 * If this fails we don't need to do all of the 3232 * cleanup that happens for the rest of the code 3233 * so just return from this error. 3234 */ 3235 zperror(gettext("could not move zone"), B_FALSE); 3236 zonecfg_fini_handle(handle); 3237 release_lock_file(lockfd); 3238 return (Z_ERR); 3239 } 3240 3241 } else { 3242 (void) printf(gettext( 3243 "Moving across file-systems; copying zonepath %s..."), 3244 zonepath); 3245 (void) fflush(stdout); 3246 3247 err = copy_zone(zonepath, new_zonepath); 3248 3249 (void) printf("\n"); 3250 if (err != Z_OK) 3251 goto done; 3252 } 3253 3254 if ((err = zonecfg_set_zonepath(handle, new_zonepath)) != Z_OK) { 3255 errno = err; 3256 zperror(gettext("could not set new zonepath"), B_TRUE); 3257 goto done; 3258 } 3259 3260 if ((err = zonecfg_save(handle)) != Z_OK) { 3261 errno = err; 3262 zperror(gettext("zonecfg save failed"), B_TRUE); 3263 goto done; 3264 } 3265 3266 revert = B_FALSE; 3267 3268 done: 3269 zonecfg_fini_handle(handle); 3270 release_lock_file(lockfd); 3271 3272 /* 3273 * Clean up the file-system based on how things went. We either 3274 * clean up the new zonepath if the operation failed for some reason 3275 * or we clean up the old zonepath if everything is ok. 3276 */ 3277 if (revert) { 3278 /* The zonecfg update failed, cleanup the new zonepath. */ 3279 if (fast) { 3280 if (rename(new_zonepath, zonepath) != 0) { 3281 zperror(gettext("could not restore zonepath"), 3282 B_FALSE); 3283 /* 3284 * err is already != Z_OK since we're reverting 3285 */ 3286 } 3287 } else { 3288 int status; 3289 3290 (void) printf(gettext("Cleaning up zonepath %s..."), 3291 new_zonepath); 3292 (void) fflush(stdout); 3293 3294 /* 3295 * "exec" the command so that the returned status is 3296 * that of rm and not the shell. 3297 */ 3298 (void) snprintf(cmdbuf, sizeof (cmdbuf), 3299 "exec " RMCOMMAND " %s", new_zonepath); 3300 3301 status = do_subproc(cmdbuf); 3302 3303 (void) printf("\n"); 3304 3305 if ((err = subproc_status("rm", status)) != Z_OK) { 3306 errno = err; 3307 zperror(gettext("could not remove new " 3308 "zonepath"), B_TRUE); 3309 } else { 3310 /* 3311 * Because we're reverting we know the mainline 3312 * code failed but we just reused the err 3313 * variable so we reset it back to Z_ERR. 3314 */ 3315 err = Z_ERR; 3316 } 3317 } 3318 3319 } else { 3320 /* The move was successful, cleanup the old zonepath. */ 3321 if (!fast) { 3322 int status; 3323 3324 (void) printf( 3325 gettext("Cleaning up zonepath %s..."), zonepath); 3326 (void) fflush(stdout); 3327 3328 /* 3329 * "exec" the command so that the returned status is 3330 * that of rm and not the shell. 3331 */ 3332 (void) snprintf(cmdbuf, sizeof (cmdbuf), 3333 "exec " RMCOMMAND " %s", zonepath); 3334 3335 status = do_subproc(cmdbuf); 3336 3337 (void) printf("\n"); 3338 3339 if ((err = subproc_status("rm", status)) != Z_OK) { 3340 errno = err; 3341 zperror(gettext("could not remove zonepath"), 3342 B_TRUE); 3343 } 3344 } 3345 } 3346 3347 return ((err == Z_OK) ? Z_OK : Z_ERR); 3348 } 3349 3350 /* 3351 * On input, TRUE => yes, FALSE => no. 3352 * On return, TRUE => 1, FALSE => 0, could not ask => -1. 3353 */ 3354 3355 static int 3356 ask_yesno(boolean_t default_answer, const char *question) 3357 { 3358 char line[64]; /* should be large enough to answer yes or no */ 3359 3360 if (!isatty(STDIN_FILENO)) 3361 return (-1); 3362 for (;;) { 3363 (void) printf("%s (%s)? ", question, 3364 default_answer ? "[y]/n" : "y/[n]"); 3365 if (fgets(line, sizeof (line), stdin) == NULL || 3366 line[0] == '\n') 3367 return (default_answer ? 1 : 0); 3368 if (tolower(line[0]) == 'y') 3369 return (1); 3370 if (tolower(line[0]) == 'n') 3371 return (0); 3372 } 3373 } 3374 3375 static int 3376 uninstall_func(int argc, char *argv[]) 3377 { 3378 /* 6: "exec " and " " */ 3379 char cmdbuf[sizeof (RMCOMMAND) + MAXPATHLEN + 6]; 3380 char line[ZONENAME_MAX + 128]; /* Enough for "Are you sure ..." */ 3381 char rootpath[MAXPATHLEN], devpath[MAXPATHLEN]; 3382 boolean_t force = B_FALSE; 3383 int lockfd, answer; 3384 int err, arg; 3385 int status; 3386 3387 if (zonecfg_in_alt_root()) { 3388 zerror(gettext("cannot uninstall zone in alternate root")); 3389 return (Z_ERR); 3390 } 3391 3392 optind = 0; 3393 while ((arg = getopt(argc, argv, "?F")) != EOF) { 3394 switch (arg) { 3395 case '?': 3396 sub_usage(SHELP_UNINSTALL, CMD_UNINSTALL); 3397 return (optopt == '?' ? Z_OK : Z_USAGE); 3398 case 'F': 3399 force = B_TRUE; 3400 break; 3401 default: 3402 sub_usage(SHELP_UNINSTALL, CMD_UNINSTALL); 3403 return (Z_USAGE); 3404 } 3405 } 3406 if (argc > optind) { 3407 sub_usage(SHELP_UNINSTALL, CMD_UNINSTALL); 3408 return (Z_USAGE); 3409 } 3410 3411 if (sanity_check(target_zone, CMD_UNINSTALL, B_FALSE, B_TRUE) != Z_OK) 3412 return (Z_ERR); 3413 3414 if (!force) { 3415 (void) snprintf(line, sizeof (line), 3416 gettext("Are you sure you want to %s zone %s"), 3417 cmd_to_str(CMD_UNINSTALL), target_zone); 3418 if ((answer = ask_yesno(B_FALSE, line)) == 0) { 3419 return (Z_OK); 3420 } else if (answer == -1) { 3421 zerror(gettext("Input not from terminal and -F " 3422 "not specified: %s not done."), 3423 cmd_to_str(CMD_UNINSTALL)); 3424 return (Z_ERR); 3425 } 3426 } 3427 3428 if ((err = zone_get_zonepath(target_zone, devpath, 3429 sizeof (devpath))) != Z_OK) { 3430 errno = err; 3431 zperror2(target_zone, gettext("could not get zone path")); 3432 return (Z_ERR); 3433 } 3434 (void) strlcat(devpath, "/dev", sizeof (devpath)); 3435 if ((err = zone_get_rootpath(target_zone, rootpath, 3436 sizeof (rootpath))) != Z_OK) { 3437 errno = err; 3438 zperror2(target_zone, gettext("could not get root path")); 3439 return (Z_ERR); 3440 } 3441 3442 /* 3443 * If there seems to be a zoneadmd running for this zone, call it 3444 * to tell it that an uninstall is happening; if all goes well it 3445 * will then shut itself down. 3446 */ 3447 if (ping_zoneadmd(target_zone) == Z_OK) { 3448 zone_cmd_arg_t zarg; 3449 zarg.cmd = Z_NOTE_UNINSTALLING; 3450 /* we don't care too much if this fails... just plow on */ 3451 (void) call_zoneadmd(target_zone, &zarg); 3452 } 3453 3454 if (grab_lock_file(target_zone, &lockfd) != Z_OK) { 3455 zerror(gettext("another %s may have an operation in progress."), 3456 "zoneadm"); 3457 return (Z_ERR); 3458 } 3459 3460 /* Don't uninstall the zone if anything is mounted there */ 3461 err = zonecfg_find_mounts(rootpath, NULL, NULL); 3462 if (err) { 3463 zerror(gettext("These file-systems are mounted on " 3464 "subdirectories of %s.\n"), rootpath); 3465 (void) zonecfg_find_mounts(rootpath, zfm_print, NULL); 3466 return (Z_ERR); 3467 } 3468 3469 err = zone_set_state(target_zone, ZONE_STATE_INCOMPLETE); 3470 if (err != Z_OK) { 3471 errno = err; 3472 zperror2(target_zone, gettext("could not set state")); 3473 goto bad; 3474 } 3475 3476 /* 3477 * "exec" the command so that the returned status is that of 3478 * RMCOMMAND and not the shell. 3479 */ 3480 (void) snprintf(cmdbuf, sizeof (cmdbuf), "exec " RMCOMMAND " %s", 3481 devpath); 3482 status = do_subproc(cmdbuf); 3483 if ((err = subproc_status(RMCOMMAND, status)) != Z_OK) 3484 goto bad; 3485 (void) snprintf(cmdbuf, sizeof (cmdbuf), "exec " RMCOMMAND " %s", 3486 rootpath); 3487 status = do_subproc(cmdbuf); 3488 if ((err = subproc_status(RMCOMMAND, status)) != Z_OK) 3489 goto bad; 3490 err = zone_set_state(target_zone, ZONE_STATE_CONFIGURED); 3491 if (err != Z_OK) { 3492 errno = err; 3493 zperror2(target_zone, gettext("could not reset state")); 3494 } 3495 bad: 3496 release_lock_file(lockfd); 3497 return (err); 3498 } 3499 3500 /* ARGSUSED */ 3501 static int 3502 mount_func(int argc, char *argv[]) 3503 { 3504 zone_cmd_arg_t zarg; 3505 3506 if (argc > 0) 3507 return (Z_USAGE); 3508 if (sanity_check(target_zone, CMD_MOUNT, B_FALSE, B_FALSE) != Z_OK) 3509 return (Z_ERR); 3510 if (verify_details(CMD_MOUNT) != Z_OK) 3511 return (Z_ERR); 3512 3513 zarg.cmd = Z_MOUNT; 3514 if (call_zoneadmd(target_zone, &zarg) != 0) { 3515 zerror(gettext("call to %s failed"), "zoneadmd"); 3516 return (Z_ERR); 3517 } 3518 return (Z_OK); 3519 } 3520 3521 /* ARGSUSED */ 3522 static int 3523 unmount_func(int argc, char *argv[]) 3524 { 3525 zone_cmd_arg_t zarg; 3526 3527 if (argc > 0) 3528 return (Z_USAGE); 3529 if (sanity_check(target_zone, CMD_UNMOUNT, B_FALSE, B_FALSE) != Z_OK) 3530 return (Z_ERR); 3531 3532 zarg.cmd = Z_UNMOUNT; 3533 if (call_zoneadmd(target_zone, &zarg) != 0) { 3534 zerror(gettext("call to %s failed"), "zoneadmd"); 3535 return (Z_ERR); 3536 } 3537 return (Z_OK); 3538 } 3539 3540 static int 3541 help_func(int argc, char *argv[]) 3542 { 3543 int arg, cmd_num; 3544 3545 if (argc == 0) { 3546 (void) usage(B_TRUE); 3547 return (Z_OK); 3548 } 3549 optind = 0; 3550 if ((arg = getopt(argc, argv, "?")) != EOF) { 3551 switch (arg) { 3552 case '?': 3553 sub_usage(SHELP_HELP, CMD_HELP); 3554 return (optopt == '?' ? Z_OK : Z_USAGE); 3555 default: 3556 sub_usage(SHELP_HELP, CMD_HELP); 3557 return (Z_USAGE); 3558 } 3559 } 3560 while (optind < argc) { 3561 /* Private commands have NULL short_usage; omit them */ 3562 if ((cmd_num = cmd_match(argv[optind])) < 0 || 3563 cmdtab[cmd_num].short_usage == NULL) { 3564 sub_usage(SHELP_HELP, CMD_HELP); 3565 return (Z_USAGE); 3566 } 3567 sub_usage(cmdtab[cmd_num].short_usage, cmd_num); 3568 optind++; 3569 } 3570 return (Z_OK); 3571 } 3572 3573 /* 3574 * Returns: CMD_MIN thru CMD_MAX on success, -1 on error 3575 */ 3576 3577 static int 3578 cmd_match(char *cmd) 3579 { 3580 int i; 3581 3582 for (i = CMD_MIN; i <= CMD_MAX; i++) { 3583 /* return only if there is an exact match */ 3584 if (strcmp(cmd, cmdtab[i].cmd_name) == 0) 3585 return (cmdtab[i].cmd_num); 3586 } 3587 return (-1); 3588 } 3589 3590 static int 3591 parse_and_run(int argc, char *argv[]) 3592 { 3593 int i = cmd_match(argv[0]); 3594 3595 if (i < 0) 3596 return (usage(B_FALSE)); 3597 return (cmdtab[i].handler(argc - 1, &(argv[1]))); 3598 } 3599 3600 static char * 3601 get_execbasename(char *execfullname) 3602 { 3603 char *last_slash, *execbasename; 3604 3605 /* guard against '/' at end of command invocation */ 3606 for (;;) { 3607 last_slash = strrchr(execfullname, '/'); 3608 if (last_slash == NULL) { 3609 execbasename = execfullname; 3610 break; 3611 } else { 3612 execbasename = last_slash + 1; 3613 if (*execbasename == '\0') { 3614 *last_slash = '\0'; 3615 continue; 3616 } 3617 break; 3618 } 3619 } 3620 return (execbasename); 3621 } 3622 3623 int 3624 main(int argc, char **argv) 3625 { 3626 int arg; 3627 zoneid_t zid; 3628 struct stat st; 3629 3630 if ((locale = setlocale(LC_ALL, "")) == NULL) 3631 locale = "C"; 3632 (void) textdomain(TEXT_DOMAIN); 3633 setbuf(stdout, NULL); 3634 (void) sigset(SIGHUP, SIG_IGN); 3635 execname = get_execbasename(argv[0]); 3636 target_zone = NULL; 3637 if (chdir("/") != 0) { 3638 zerror(gettext("could not change directory to /.")); 3639 exit(Z_ERR); 3640 } 3641 3642 while ((arg = getopt(argc, argv, "?z:R:")) != EOF) { 3643 switch (arg) { 3644 case '?': 3645 return (usage(B_TRUE)); 3646 case 'z': 3647 target_zone = optarg; 3648 break; 3649 case 'R': /* private option for admin/install use */ 3650 if (*optarg != '/') { 3651 zerror(gettext("root path must be absolute.")); 3652 exit(Z_ERR); 3653 } 3654 if (stat(optarg, &st) == -1 || !S_ISDIR(st.st_mode)) { 3655 zerror( 3656 gettext("root path must be a directory.")); 3657 exit(Z_ERR); 3658 } 3659 zonecfg_set_root(optarg); 3660 break; 3661 default: 3662 return (usage(B_FALSE)); 3663 } 3664 } 3665 3666 if (optind >= argc) 3667 return (usage(B_FALSE)); 3668 if (target_zone != NULL && zone_get_id(target_zone, &zid) != 0) { 3669 errno = Z_NO_ZONE; 3670 zperror(target_zone, B_TRUE); 3671 exit(Z_ERR); 3672 } 3673 return (parse_and_run(argc - optind, &argv[optind])); 3674 } 3675