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 2005 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 75 #include <pool.h> 76 #include <sys/pool.h> 77 78 #define MAXARGS 8 79 80 /* Reflects kernel zone entries */ 81 typedef struct zone_entry { 82 zoneid_t zid; 83 char zname[ZONENAME_MAX]; 84 char *zstate_str; 85 zone_state_t zstate_num; 86 char zroot[MAXPATHLEN]; 87 } zone_entry_t; 88 89 static zone_entry_t *zents; 90 static size_t nzents; 91 92 #if !defined(TEXT_DOMAIN) /* should be defined by cc -D */ 93 #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it wasn't */ 94 #endif 95 96 #define Z_ERR 1 97 #define Z_USAGE 2 98 99 /* 0755 is the default directory mode. */ 100 #define DEFAULT_DIR_MODE \ 101 (S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) 102 103 #define CMD_HELP 0 104 #define CMD_BOOT 1 105 #define CMD_HALT 2 106 #define CMD_READY 3 107 #define CMD_REBOOT 4 108 #define CMD_LIST 5 109 #define CMD_VERIFY 6 110 #define CMD_INSTALL 7 111 #define CMD_UNINSTALL 8 112 #define CMD_MOUNT 9 113 #define CMD_UNMOUNT 10 114 115 #define CMD_MIN CMD_HELP 116 #define CMD_MAX CMD_UNMOUNT 117 118 struct cmd { 119 uint_t cmd_num; /* command number */ 120 char *cmd_name; /* command name */ 121 char *short_usage; /* short form help */ 122 int (*handler)(int argc, char *argv[]); /* function to call */ 123 124 }; 125 126 #define SHELP_HELP "help" 127 #define SHELP_BOOT "boot [-s]" 128 #define SHELP_HALT "halt" 129 #define SHELP_READY "ready" 130 #define SHELP_REBOOT "reboot" 131 #define SHELP_LIST "list [-cipv]" 132 #define SHELP_VERIFY "verify" 133 #define SHELP_INSTALL "install" 134 #define SHELP_UNINSTALL "uninstall [-F]" 135 136 static int help_func(int argc, char *argv[]); 137 static int ready_func(int argc, char *argv[]); 138 static int boot_func(int argc, char *argv[]); 139 static int halt_func(int argc, char *argv[]); 140 static int reboot_func(int argc, char *argv[]); 141 static int list_func(int argc, char *argv[]); 142 static int verify_func(int argc, char *argv[]); 143 static int install_func(int argc, char *argv[]); 144 static int uninstall_func(int argc, char *argv[]); 145 static int mount_func(int argc, char *argv[]); 146 static int unmount_func(int argc, char *argv[]); 147 static int sanity_check(char *zone, int cmd_num, boolean_t running, 148 boolean_t unsafe_when_running); 149 static int cmd_match(char *cmd); 150 static int verify_details(int); 151 152 static struct cmd cmdtab[] = { 153 { CMD_HELP, "help", SHELP_HELP, help_func }, 154 { CMD_BOOT, "boot", SHELP_BOOT, boot_func }, 155 { CMD_HALT, "halt", SHELP_HALT, halt_func }, 156 { CMD_READY, "ready", SHELP_READY, ready_func }, 157 { CMD_REBOOT, "reboot", SHELP_REBOOT, reboot_func }, 158 { CMD_LIST, "list", SHELP_LIST, list_func }, 159 { CMD_VERIFY, "verify", SHELP_VERIFY, verify_func }, 160 { CMD_INSTALL, "install", SHELP_INSTALL, install_func }, 161 { CMD_UNINSTALL, "uninstall", SHELP_UNINSTALL, 162 uninstall_func }, 163 /* Private commands for admin/install */ 164 { CMD_MOUNT, "mount", NULL, mount_func }, 165 { CMD_UNMOUNT, "unmount", NULL, unmount_func } 166 }; 167 168 /* global variables */ 169 170 /* set early in main(), never modified thereafter, used all over the place */ 171 static char *execname; 172 static char *target_zone; 173 static char *locale; 174 175 /* used in do_subproc() and signal handler */ 176 static volatile boolean_t child_killed; 177 178 static char * 179 cmd_to_str(int cmd_num) 180 { 181 assert(cmd_num >= CMD_MIN && cmd_num <= CMD_MAX); 182 return (cmdtab[cmd_num].cmd_name); 183 } 184 185 /* This is a separate function because of gettext() wrapping. */ 186 static char * 187 long_help(int cmd_num) 188 { 189 assert(cmd_num >= CMD_MIN && cmd_num <= CMD_MAX); 190 switch (cmd_num) { 191 case CMD_HELP: 192 return (gettext("Print usage message.")); 193 case CMD_BOOT: 194 return (gettext("Activates (boots) specified zone. " 195 "The -s flag can be used\n\tto boot the zone in " 196 "the single-user state.")); 197 case CMD_HALT: 198 return (gettext("Halts specified zone, bypassing " 199 "shutdown scripts and removing runtime\n\t" 200 "resources of the zone.")); 201 case CMD_READY: 202 return (gettext("Prepares a zone for running " 203 "applications but does not start any user\n\t" 204 "processes in the zone.")); 205 case CMD_REBOOT: 206 return (gettext("Restarts the zone (equivalent to a " 207 "halt / boot sequence).\n\tFails if the zone is " 208 "not active.")); 209 case CMD_LIST: 210 return (gettext("Lists the current zones, or a " 211 "specific zone if indicated. By default,\n\tall " 212 "running zones are listed, though this can be " 213 "expanded to all\n\tinstalled zones with the -i " 214 "option or all configured zones with the\n\t-c " 215 "option. When used with the general -z <zone> " 216 "option, lists only the\n\tspecified zone, but " 217 "lists it regardless of its state, and the -i " 218 "and -c\n\toptions are disallowed. The -v option " 219 "can be used to display verbose\n\tinformation: " 220 "zone name, id, current state, root directory and " 221 "options.\n\tThe -p option can be used to request " 222 "machine-parsable output. The -v\n\tand -p " 223 "options are mutually exclusive. If neither -v " 224 "nor -p is used,\n\tjust the zone name is " 225 "listed.")); 226 case CMD_VERIFY: 227 return (gettext("Check to make sure the configuration " 228 "can safely be instantiated\n\ton the machine: " 229 "physical network interfaces exist, etc.")); 230 case CMD_INSTALL: 231 return (gettext("Install the configuration on to the " 232 "system.")); 233 case CMD_UNINSTALL: 234 return (gettext("Uninstall the configuration from the " 235 "system. The -F flag can be used\n\tto force the " 236 "action.")); 237 default: 238 return (""); 239 } 240 /* NOTREACHED */ 241 return (NULL); 242 } 243 244 /* 245 * Called with explicit B_TRUE when help is explicitly requested, B_FALSE for 246 * unexpected errors. 247 */ 248 249 static int 250 usage(boolean_t explicit) 251 { 252 int i; 253 FILE *fd = explicit ? stdout : stderr; 254 255 (void) fprintf(fd, "%s:\t%s help\n", gettext("usage"), execname); 256 (void) fprintf(fd, "\t%s [-z <zone>] list\n", execname); 257 (void) fprintf(fd, "\t%s -z <zone> <%s>\n", execname, 258 gettext("subcommand")); 259 (void) fprintf(fd, "\n%s:\n\n", gettext("Subcommands")); 260 for (i = CMD_MIN; i <= CMD_MAX; i++) { 261 if (cmdtab[i].short_usage == NULL) 262 continue; 263 (void) fprintf(fd, "%s\n", cmdtab[i].short_usage); 264 if (explicit) 265 (void) fprintf(fd, "\t%s\n\n", long_help(i)); 266 } 267 if (!explicit) 268 (void) fputs("\n", fd); 269 return (Z_USAGE); 270 } 271 272 static void 273 sub_usage(char *short_usage, int cmd_num) 274 { 275 (void) fprintf(stderr, "%s:\t%s\n", gettext("usage"), short_usage); 276 (void) fprintf(stderr, "\t%s\n", long_help(cmd_num)); 277 } 278 279 /* 280 * zperror() is like perror(3c) except that this also prints the executable 281 * name at the start of the message, and takes a boolean indicating whether 282 * to call libc'c strerror() or that from libzonecfg. 283 */ 284 285 static void 286 zperror(const char *str, boolean_t zonecfg_error) 287 { 288 (void) fprintf(stderr, "%s: %s: %s\n", execname, str, 289 zonecfg_error ? zonecfg_strerror(errno) : strerror(errno)); 290 } 291 292 /* 293 * zperror2() is very similar to zperror() above, except it also prints a 294 * supplied zone name after the executable. 295 * 296 * All current consumers of this function want libzonecfg's strerror() rather 297 * than libc's; if this ever changes, this function can be made more generic 298 * like zperror() above. 299 */ 300 301 static void 302 zperror2(const char *zone, const char *str) 303 { 304 (void) fprintf(stderr, "%s: %s: %s: %s\n", execname, zone, str, 305 zonecfg_strerror(errno)); 306 } 307 308 /* PRINTFLIKE1 */ 309 static void 310 zerror(const char *fmt, ...) 311 { 312 va_list alist; 313 314 va_start(alist, fmt); 315 (void) fprintf(stderr, "%s: ", execname); 316 if (target_zone != NULL) 317 (void) fprintf(stderr, "zone '%s': ", target_zone); 318 (void) vfprintf(stderr, fmt, alist); 319 (void) fprintf(stderr, "\n"); 320 va_end(alist); 321 } 322 323 static void * 324 safe_calloc(size_t nelem, size_t elsize) 325 { 326 void *r = calloc(nelem, elsize); 327 328 if (r == NULL) { 329 zerror(gettext("failed to allocate %lu bytes: %s"), 330 (ulong_t)nelem * elsize, strerror(errno)); 331 exit(Z_ERR); 332 } 333 return (r); 334 } 335 336 static void 337 zone_print(zone_entry_t *zent, boolean_t verbose, boolean_t parsable) 338 { 339 static boolean_t firsttime = B_TRUE; 340 341 assert(!(verbose && parsable)); 342 if (firsttime && verbose) { 343 firsttime = B_FALSE; 344 (void) printf("%*s %-16s %-14s %-30s\n", ZONEID_WIDTH, "ID", 345 "NAME", "STATUS", "PATH"); 346 } 347 if (!verbose) { 348 if (!parsable) { 349 (void) printf("%s\n", zent->zname); 350 return; 351 } 352 if (zent->zid == ZONE_ID_UNDEFINED) 353 (void) printf("-"); 354 else 355 (void) printf("%lu", zent->zid); 356 (void) printf(":%s:%s:%s\n", zent->zname, zent->zstate_str, 357 zent->zroot); 358 return; 359 } 360 if (zent->zstate_str != NULL) { 361 if (zent->zid == ZONE_ID_UNDEFINED) 362 (void) printf("%*s", ZONEID_WIDTH, "-"); 363 else 364 (void) printf("%*lu", ZONEID_WIDTH, zent->zid); 365 (void) printf(" %-16s %-14s %-30s\n", zent->zname, 366 zent->zstate_str, zent->zroot); 367 } 368 } 369 370 static int 371 lookup_zone_info(const char *zone_name, zoneid_t zid, zone_entry_t *zent) 372 { 373 char root[MAXPATHLEN]; 374 int err; 375 376 (void) strlcpy(zent->zname, zone_name, sizeof (zent->zname)); 377 (void) strlcpy(zent->zroot, "???", sizeof (zent->zroot)); 378 zent->zstate_str = "???"; 379 380 zent->zid = zid; 381 382 if ((err = zone_get_zonepath(zent->zname, root, sizeof (root))) != 383 Z_OK) { 384 errno = err; 385 zperror2(zent->zname, gettext("could not get zone path")); 386 return (Z_ERR); 387 } 388 (void) strlcpy(zent->zroot, root, sizeof (zent->zroot)); 389 390 if ((err = zone_get_state(zent->zname, &zent->zstate_num)) != Z_OK) { 391 errno = err; 392 zperror2(zent->zname, gettext("could not get state")); 393 return (Z_ERR); 394 } 395 zent->zstate_str = zone_state_str(zent->zstate_num); 396 397 return (Z_OK); 398 } 399 400 /* 401 * fetch_zents() calls zone_list(2) to find out how many zones are running 402 * (which is stored in the global nzents), then calls zone_list(2) again 403 * to fetch the list of running zones (stored in the global zents). This 404 * function may be called multiple times, so if zents is already set, we 405 * return immediately to save work. 406 */ 407 408 static int 409 fetch_zents(void) 410 { 411 zoneid_t *zids = NULL; 412 uint_t nzents_saved; 413 int i, retv; 414 FILE *fp; 415 boolean_t inaltroot; 416 zone_entry_t *zentp; 417 418 if (nzents > 0) 419 return (Z_OK); 420 421 if (zone_list(NULL, &nzents) != 0) { 422 zperror(gettext("failed to get zoneid list"), B_FALSE); 423 return (Z_ERR); 424 } 425 426 again: 427 if (nzents == 0) 428 return (Z_OK); 429 430 zids = safe_calloc(nzents, sizeof (zoneid_t)); 431 nzents_saved = nzents; 432 433 if (zone_list(zids, &nzents) != 0) { 434 zperror(gettext("failed to get zone list"), B_FALSE); 435 free(zids); 436 return (Z_ERR); 437 } 438 if (nzents != nzents_saved) { 439 /* list changed, try again */ 440 free(zids); 441 goto again; 442 } 443 444 zents = safe_calloc(nzents, sizeof (zone_entry_t)); 445 446 inaltroot = zonecfg_in_alt_root(); 447 if (inaltroot) 448 fp = zonecfg_open_scratch("", B_FALSE); 449 else 450 fp = NULL; 451 zentp = zents; 452 retv = Z_OK; 453 for (i = 0; i < nzents; i++) { 454 char name[ZONENAME_MAX]; 455 char altname[ZONENAME_MAX]; 456 457 if (getzonenamebyid(zids[i], name, sizeof (name)) < 0) { 458 zperror(gettext("failed to get zone name"), B_FALSE); 459 retv = Z_ERR; 460 continue; 461 } 462 if (zonecfg_is_scratch(name)) { 463 /* Ignore scratch zones by default */ 464 if (!inaltroot) 465 continue; 466 if (fp == NULL || 467 zonecfg_reverse_scratch(fp, name, altname, 468 sizeof (altname), NULL, 0) == -1) { 469 zerror(gettext("cannot resolve scratch " 470 "zone %s"), name); 471 retv = Z_ERR; 472 continue; 473 } 474 (void) strcpy(name, altname); 475 } else { 476 /* Ignore non-scratch when in an alternate root */ 477 if (inaltroot && strcmp(name, GLOBAL_ZONENAME) != 0) 478 continue; 479 } 480 if (lookup_zone_info(name, zids[i], zentp) != Z_OK) { 481 zerror(gettext("failed to get zone data")); 482 retv = Z_ERR; 483 continue; 484 } 485 zentp++; 486 } 487 nzents = zentp - zents; 488 if (fp != NULL) 489 zonecfg_close_scratch(fp); 490 491 free(zids); 492 return (retv); 493 } 494 495 static int 496 zone_print_list(zone_state_t min_state, boolean_t verbose, boolean_t parsable) 497 { 498 int i; 499 zone_entry_t zent; 500 FILE *cookie; 501 char *name; 502 503 /* 504 * First get the list of running zones from the kernel and print them. 505 * If that is all we need, then return. 506 */ 507 if ((i = fetch_zents()) != Z_OK) { 508 /* 509 * No need for error messages; fetch_zents() has already taken 510 * care of this. 511 */ 512 return (i); 513 } 514 for (i = 0; i < nzents; i++) 515 zone_print(&zents[i], verbose, parsable); 516 if (min_state >= ZONE_STATE_RUNNING) 517 return (Z_OK); 518 /* 519 * Next, get the full list of zones from the configuration, skipping 520 * any we have already printed. 521 */ 522 cookie = setzoneent(); 523 while ((name = getzoneent(cookie)) != NULL) { 524 for (i = 0; i < nzents; i++) { 525 if (strcmp(zents[i].zname, name) == 0) 526 break; 527 } 528 if (i < nzents) { 529 free(name); 530 continue; 531 } 532 if (lookup_zone_info(name, ZONE_ID_UNDEFINED, &zent) != Z_OK) { 533 free(name); 534 continue; 535 } 536 free(name); 537 if (zent.zstate_num >= min_state) 538 zone_print(&zent, verbose, parsable); 539 } 540 endzoneent(cookie); 541 return (Z_OK); 542 } 543 544 static zone_entry_t * 545 lookup_running_zone(char *str) 546 { 547 zoneid_t zoneid; 548 char *cp; 549 int i; 550 551 if (fetch_zents() != Z_OK) 552 return (NULL); 553 554 for (i = 0; i < nzents; i++) { 555 if (strcmp(str, zents[i].zname) == 0) 556 return (&zents[i]); 557 } 558 errno = 0; 559 zoneid = strtol(str, &cp, 0); 560 if (zoneid < MIN_ZONEID || zoneid > MAX_ZONEID || 561 errno != 0 || *cp != '\0') 562 return (NULL); 563 for (i = 0; i < nzents; i++) { 564 if (zoneid == zents[i].zid) 565 return (&zents[i]); 566 } 567 return (NULL); 568 } 569 570 /* 571 * Check a bit in a mode_t: if on is B_TRUE, that bit should be on; if 572 * B_FALSE, it should be off. Return B_TRUE if the mode is bad (incorrect). 573 */ 574 static boolean_t 575 bad_mode_bit(mode_t mode, mode_t bit, boolean_t on, char *file) 576 { 577 char *str; 578 579 assert(bit == S_IRUSR || bit == S_IWUSR || bit == S_IXUSR || 580 bit == S_IRGRP || bit == S_IWGRP || bit == S_IXGRP || 581 bit == S_IROTH || bit == S_IWOTH || bit == S_IXOTH); 582 /* 583 * TRANSLATION_NOTE 584 * The strings below will be used as part of a larger message, 585 * either: 586 * (file name) must be (owner|group|world) (read|writ|execut)able 587 * or 588 * (file name) must not be (owner|group|world) (read|writ|execut)able 589 */ 590 switch (bit) { 591 case S_IRUSR: 592 str = gettext("owner readable"); 593 break; 594 case S_IWUSR: 595 str = gettext("owner writable"); 596 break; 597 case S_IXUSR: 598 str = gettext("owner executable"); 599 break; 600 case S_IRGRP: 601 str = gettext("group readable"); 602 break; 603 case S_IWGRP: 604 str = gettext("group writable"); 605 break; 606 case S_IXGRP: 607 str = gettext("group executable"); 608 break; 609 case S_IROTH: 610 str = gettext("world readable"); 611 break; 612 case S_IWOTH: 613 str = gettext("world writable"); 614 break; 615 case S_IXOTH: 616 str = gettext("world executable"); 617 break; 618 } 619 if ((mode & bit) == (on ? 0 : bit)) { 620 /* 621 * TRANSLATION_NOTE 622 * The first parameter below is a file name; the second 623 * is one of the "(owner|group|world) (read|writ|execut)able" 624 * strings from above. 625 */ 626 /* 627 * The code below could be simplified but not in a way 628 * that would easily translate to non-English locales. 629 */ 630 if (on) { 631 (void) fprintf(stderr, gettext("%s must be %s.\n"), 632 file, str); 633 } else { 634 (void) fprintf(stderr, gettext("%s must not be %s.\n"), 635 file, str); 636 } 637 return (B_TRUE); 638 } 639 return (B_FALSE); 640 } 641 642 /* 643 * We want to make sure that no zone has its zone path as a child node 644 * (in the directory sense) of any other. We do that by comparing this 645 * zone's path to the path of all other (non-global) zones. The comparison 646 * in each case is simple: add '/' to the end of the path, then do a 647 * strncmp() of the two paths, using the length of the shorter one. 648 */ 649 650 static int 651 crosscheck_zonepaths(char *path) 652 { 653 char rpath[MAXPATHLEN]; /* resolved path */ 654 char path_copy[MAXPATHLEN]; /* copy of original path */ 655 char rpath_copy[MAXPATHLEN]; /* copy of original rpath */ 656 struct zoneent *ze; 657 int res, err; 658 FILE *cookie; 659 660 cookie = setzoneent(); 661 while ((ze = getzoneent_private(cookie)) != NULL) { 662 /* Skip zones which are not installed. */ 663 if (ze->zone_state < ZONE_STATE_INSTALLED) { 664 free(ze); 665 continue; 666 } 667 /* Skip the global zone and the current target zone. */ 668 if (strcmp(ze->zone_name, GLOBAL_ZONENAME) == 0 || 669 strcmp(ze->zone_name, target_zone) == 0) { 670 free(ze); 671 continue; 672 } 673 if (strlen(ze->zone_path) == 0) { 674 /* old index file without path, fall back */ 675 if ((err = zone_get_zonepath(ze->zone_name, 676 ze->zone_path, sizeof (ze->zone_path))) != Z_OK) { 677 errno = err; 678 zperror2(ze->zone_name, 679 gettext("could not get zone path")); 680 free(ze); 681 continue; 682 } 683 } 684 (void) snprintf(path_copy, sizeof (path_copy), "%s%s", 685 zonecfg_get_root(), ze->zone_path); 686 res = resolvepath(path_copy, rpath, sizeof (rpath)); 687 if (res == -1) { 688 if (errno != ENOENT) { 689 zperror(path_copy, B_FALSE); 690 free(ze); 691 return (Z_ERR); 692 } 693 (void) printf(gettext("WARNING: zone %s is installed, " 694 "but its %s %s does not exist.\n"), ze->zone_name, 695 "zonepath", path_copy); 696 free(ze); 697 continue; 698 } 699 rpath[res] = '\0'; 700 (void) snprintf(path_copy, sizeof (path_copy), "%s/", path); 701 (void) snprintf(rpath_copy, sizeof (rpath_copy), "%s/", rpath); 702 if (strncmp(path_copy, rpath_copy, 703 min(strlen(path_copy), strlen(rpath_copy))) == 0) { 704 (void) fprintf(stderr, gettext("%s zonepath (%s) and " 705 "%s zonepath (%s) overlap.\n"), 706 target_zone, path, ze->zone_name, rpath); 707 free(ze); 708 return (Z_ERR); 709 } 710 free(ze); 711 } 712 endzoneent(cookie); 713 return (Z_OK); 714 } 715 716 static int 717 validate_zonepath(char *path, int cmd_num) 718 { 719 int res; /* result of last library/system call */ 720 boolean_t err = B_FALSE; /* have we run into an error? */ 721 struct stat stbuf; 722 struct statvfs vfsbuf; 723 char rpath[MAXPATHLEN]; /* resolved path */ 724 char ppath[MAXPATHLEN]; /* parent path */ 725 char rppath[MAXPATHLEN]; /* resolved parent path */ 726 char rootpath[MAXPATHLEN]; /* root path */ 727 zone_state_t state; 728 729 if (path[0] != '/') { 730 (void) fprintf(stderr, 731 gettext("%s is not an absolute path.\n"), path); 732 return (Z_ERR); 733 } 734 if ((res = resolvepath(path, rpath, sizeof (rpath))) == -1) { 735 if ((errno != ENOENT) || 736 (cmd_num != CMD_VERIFY && cmd_num != CMD_INSTALL)) { 737 zperror(path, B_FALSE); 738 return (Z_ERR); 739 } 740 if (cmd_num == CMD_VERIFY) { 741 (void) fprintf(stderr, gettext("WARNING: %s does not " 742 "exist, so it cannot be verified.\nWhen 'zoneadm " 743 "%s' is run, '%s' will try to create\n%s, and '%s' " 744 "will be tried again,\nbut the '%s' may fail if:\n" 745 "the parent directory of %s is group- or other-" 746 "writable\nor\n%s overlaps with any other " 747 "installed zones.\n"), path, 748 cmd_to_str(CMD_INSTALL), cmd_to_str(CMD_INSTALL), 749 path, cmd_to_str(CMD_VERIFY), 750 cmd_to_str(CMD_VERIFY), path, path); 751 return (Z_OK); 752 } 753 /* 754 * The zonepath is supposed to be mode 700 but its 755 * parent(s) 755. So use 755 on the mkdirp() then 756 * chmod() the zonepath itself to 700. 757 */ 758 if (mkdirp(path, DEFAULT_DIR_MODE) < 0) { 759 zperror(path, B_FALSE); 760 return (Z_ERR); 761 } 762 /* 763 * If the chmod() fails, report the error, but might 764 * as well continue the verify procedure. 765 */ 766 if (chmod(path, S_IRWXU) != 0) 767 zperror(path, B_FALSE); 768 /* 769 * Since the mkdir() succeeded, we should not have to 770 * worry about a subsequent ENOENT, thus this should 771 * only recurse once. 772 */ 773 return (validate_zonepath(path, CMD_INSTALL)); 774 } 775 rpath[res] = '\0'; 776 if (strcmp(path, rpath) != 0) { 777 errno = Z_RESOLVED_PATH; 778 zperror(path, B_TRUE); 779 return (Z_ERR); 780 } 781 if ((res = stat(rpath, &stbuf)) != 0) { 782 zperror(rpath, B_FALSE); 783 return (Z_ERR); 784 } 785 if (!S_ISDIR(stbuf.st_mode)) { 786 (void) fprintf(stderr, gettext("%s is not a directory.\n"), 787 rpath); 788 return (Z_ERR); 789 } 790 if ((strcmp(stbuf.st_fstype, MNTTYPE_TMPFS) == 0) || 791 (strcmp(stbuf.st_fstype, MNTTYPE_XMEMFS) == 0)) { 792 (void) printf(gettext("WARNING: %s is on a temporary " 793 "file-system.\n"), rpath); 794 } 795 if (crosscheck_zonepaths(rpath) != Z_OK) 796 return (Z_ERR); 797 /* 798 * Try to collect and report as many minor errors as possible 799 * before returning, so the user can learn everything that needs 800 * to be fixed up front. 801 */ 802 if (stbuf.st_uid != 0) { 803 (void) fprintf(stderr, gettext("%s is not owned by root.\n"), 804 rpath); 805 err = B_TRUE; 806 } 807 err |= bad_mode_bit(stbuf.st_mode, S_IRUSR, B_TRUE, rpath); 808 err |= bad_mode_bit(stbuf.st_mode, S_IWUSR, B_TRUE, rpath); 809 err |= bad_mode_bit(stbuf.st_mode, S_IXUSR, B_TRUE, rpath); 810 err |= bad_mode_bit(stbuf.st_mode, S_IRGRP, B_FALSE, rpath); 811 err |= bad_mode_bit(stbuf.st_mode, S_IWGRP, B_FALSE, rpath); 812 err |= bad_mode_bit(stbuf.st_mode, S_IXGRP, B_FALSE, rpath); 813 err |= bad_mode_bit(stbuf.st_mode, S_IROTH, B_FALSE, rpath); 814 err |= bad_mode_bit(stbuf.st_mode, S_IWOTH, B_FALSE, rpath); 815 err |= bad_mode_bit(stbuf.st_mode, S_IXOTH, B_FALSE, rpath); 816 817 (void) snprintf(ppath, sizeof (ppath), "%s/..", path); 818 if ((res = resolvepath(ppath, rppath, sizeof (rppath))) == -1) { 819 zperror(ppath, B_FALSE); 820 return (Z_ERR); 821 } 822 rppath[res] = '\0'; 823 if ((res = stat(rppath, &stbuf)) != 0) { 824 zperror(rppath, B_FALSE); 825 return (Z_ERR); 826 } 827 /* theoretically impossible */ 828 if (!S_ISDIR(stbuf.st_mode)) { 829 (void) fprintf(stderr, gettext("%s is not a directory.\n"), 830 rppath); 831 return (Z_ERR); 832 } 833 if (stbuf.st_uid != 0) { 834 (void) fprintf(stderr, gettext("%s is not owned by root.\n"), 835 rppath); 836 err = B_TRUE; 837 } 838 err |= bad_mode_bit(stbuf.st_mode, S_IRUSR, B_TRUE, rppath); 839 err |= bad_mode_bit(stbuf.st_mode, S_IWUSR, B_TRUE, rppath); 840 err |= bad_mode_bit(stbuf.st_mode, S_IXUSR, B_TRUE, rppath); 841 err |= bad_mode_bit(stbuf.st_mode, S_IWGRP, B_FALSE, rppath); 842 err |= bad_mode_bit(stbuf.st_mode, S_IWOTH, B_FALSE, rppath); 843 if (strcmp(rpath, rppath) == 0) { 844 (void) fprintf(stderr, gettext("%s is its own parent.\n"), 845 rppath); 846 err = B_TRUE; 847 } 848 849 if (statvfs(rpath, &vfsbuf) != 0) { 850 zperror(rpath, B_FALSE); 851 return (Z_ERR); 852 } 853 if (strncmp(vfsbuf.f_basetype, "nfs", 3) == 0) { 854 (void) fprintf(stderr, gettext("Zonepath %s is over NFS, " 855 "which is not currently supported.\n"), rpath); 856 return (Z_ERR); 857 } 858 859 if ((res = zone_get_state(target_zone, &state)) != Z_OK) { 860 errno = res; 861 zperror2(target_zone, gettext("could not get state")); 862 return (Z_ERR); 863 } 864 /* 865 * The existence of the root path is only bad in the configured state, 866 * as it is *supposed* to be there at the installed and later states. 867 * State/command mismatches are caught earlier in verify_details(). 868 */ 869 if (state == ZONE_STATE_CONFIGURED) { 870 if (snprintf(rootpath, sizeof (rootpath), "%s/root", rpath) >= 871 sizeof (rootpath)) { 872 (void) fprintf(stderr, 873 gettext("Zonepath %s is too long.\n"), rpath); 874 return (Z_ERR); 875 } 876 if ((res = stat(rootpath, &stbuf)) == 0) { 877 (void) fprintf(stderr, gettext("Rootpath %s exists; " 878 "remove or move aside prior to %s.\n"), rootpath, 879 cmd_to_str(CMD_INSTALL)); 880 return (Z_ERR); 881 } 882 } 883 884 return (err ? Z_ERR : Z_OK); 885 } 886 887 static void 888 release_lock_file(int lockfd) 889 { 890 (void) close(lockfd); 891 } 892 893 static int 894 grab_lock_file(const char *zone_name, int *lockfd) 895 { 896 char pathbuf[PATH_MAX]; 897 struct flock flock; 898 899 if (snprintf(pathbuf, sizeof (pathbuf), "%s%s", zonecfg_get_root(), 900 ZONES_TMPDIR) >= sizeof (pathbuf)) { 901 zerror(gettext("alternate root path is too long")); 902 return (Z_ERR); 903 } 904 if (mkdir(pathbuf, S_IRWXU) < 0 && errno != EEXIST) { 905 zerror(gettext("could not mkdir %s: %s"), pathbuf, 906 strerror(errno)); 907 return (Z_ERR); 908 } 909 (void) chmod(pathbuf, S_IRWXU); 910 911 /* 912 * One of these lock files is created for each zone (when needed). 913 * The lock files are not cleaned up (except on system reboot), 914 * but since there is only one per zone, there is no resource 915 * starvation issue. 916 */ 917 if (snprintf(pathbuf, sizeof (pathbuf), "%s%s/%s.zoneadm.lock", 918 zonecfg_get_root(), ZONES_TMPDIR, zone_name) >= sizeof (pathbuf)) { 919 zerror(gettext("alternate root path is too long")); 920 return (Z_ERR); 921 } 922 if ((*lockfd = open(pathbuf, O_RDWR|O_CREAT, S_IRUSR|S_IWUSR)) < 0) { 923 zerror(gettext("could not open %s: %s"), pathbuf, 924 strerror(errno)); 925 return (Z_ERR); 926 } 927 /* 928 * Lock the file to synchronize with other zoneadmds 929 */ 930 flock.l_type = F_WRLCK; 931 flock.l_whence = SEEK_SET; 932 flock.l_start = (off_t)0; 933 flock.l_len = (off_t)0; 934 if (fcntl(*lockfd, F_SETLKW, &flock) < 0) { 935 zerror(gettext("unable to lock %s: %s"), pathbuf, 936 strerror(errno)); 937 release_lock_file(*lockfd); 938 return (Z_ERR); 939 } 940 return (Z_OK); 941 } 942 943 static boolean_t 944 get_doorname(const char *zone_name, char *buffer) 945 { 946 return (snprintf(buffer, PATH_MAX, "%s" ZONE_DOOR_PATH, 947 zonecfg_get_root(), zone_name) < PATH_MAX); 948 } 949 950 /* 951 * system daemons are not audited. For the global zone, this occurs 952 * "naturally" since init is started with the default audit 953 * characteristics. Since zoneadmd is a system daemon and it starts 954 * init for a zone, it is necessary to clear out the audit 955 * characteristics inherited from whomever started zoneadmd. This is 956 * indicated by the audit id, which is set from the ruid parameter of 957 * adt_set_user(), below. 958 */ 959 960 static void 961 prepare_audit_context() 962 { 963 adt_session_data_t *ah; 964 char *failure = gettext("audit failure: %s"); 965 966 if (adt_start_session(&ah, NULL, 0)) { 967 zerror(failure, strerror(errno)); 968 return; 969 } 970 if (adt_set_user(ah, ADT_NO_AUDIT, ADT_NO_AUDIT, 971 ADT_NO_AUDIT, ADT_NO_AUDIT, NULL, ADT_NEW)) { 972 zerror(failure, strerror(errno)); 973 (void) adt_end_session(ah); 974 return; 975 } 976 if (adt_set_proc(ah)) 977 zerror(failure, strerror(errno)); 978 979 (void) adt_end_session(ah); 980 } 981 982 static int 983 start_zoneadmd(const char *zone_name) 984 { 985 char doorpath[PATH_MAX]; 986 pid_t child_pid; 987 int error = Z_ERR; 988 int doorfd, lockfd; 989 struct door_info info; 990 991 if (!get_doorname(zone_name, doorpath)) 992 return (Z_ERR); 993 994 if (grab_lock_file(zone_name, &lockfd) != Z_OK) 995 return (Z_ERR); 996 997 /* 998 * Now that we have the lock, re-confirm that the daemon is 999 * *not* up and working fine. If it is still down, we have a green 1000 * light to start it. 1001 */ 1002 if ((doorfd = open(doorpath, O_RDONLY)) < 0) { 1003 if (errno != ENOENT) { 1004 zperror(doorpath, B_FALSE); 1005 goto out; 1006 } 1007 } else { 1008 if (door_info(doorfd, &info) == 0 && 1009 ((info.di_attributes & DOOR_REVOKED) == 0)) { 1010 error = Z_OK; 1011 (void) close(doorfd); 1012 goto out; 1013 } 1014 (void) close(doorfd); 1015 } 1016 1017 if ((child_pid = fork()) == -1) { 1018 zperror(gettext("could not fork"), B_FALSE); 1019 goto out; 1020 } else if (child_pid == 0) { 1021 const char *argv[6], **ap; 1022 1023 /* child process */ 1024 prepare_audit_context(); 1025 1026 ap = argv; 1027 *ap++ = "zoneadmd"; 1028 *ap++ = "-z"; 1029 *ap++ = zone_name; 1030 if (zonecfg_in_alt_root()) { 1031 *ap++ = "-R"; 1032 *ap++ = zonecfg_get_root(); 1033 } 1034 *ap = NULL; 1035 1036 (void) execv("/usr/lib/zones/zoneadmd", (char * const *)argv); 1037 zperror(gettext("could not exec zoneadmd"), B_FALSE); 1038 _exit(Z_ERR); 1039 } else { 1040 /* parent process */ 1041 pid_t retval; 1042 int pstatus = 0; 1043 1044 do { 1045 retval = waitpid(child_pid, &pstatus, 0); 1046 } while (retval != child_pid); 1047 if (WIFSIGNALED(pstatus) || (WIFEXITED(pstatus) && 1048 WEXITSTATUS(pstatus) != 0)) { 1049 zerror(gettext("could not start %s"), "zoneadmd"); 1050 goto out; 1051 } 1052 } 1053 error = Z_OK; 1054 out: 1055 release_lock_file(lockfd); 1056 return (error); 1057 } 1058 1059 static int 1060 ping_zoneadmd(const char *zone_name) 1061 { 1062 char doorpath[PATH_MAX]; 1063 int doorfd; 1064 struct door_info info; 1065 1066 if (!get_doorname(zone_name, doorpath)) 1067 return (Z_ERR); 1068 1069 if ((doorfd = open(doorpath, O_RDONLY)) < 0) { 1070 return (Z_ERR); 1071 } 1072 if (door_info(doorfd, &info) == 0 && 1073 ((info.di_attributes & DOOR_REVOKED) == 0)) { 1074 (void) close(doorfd); 1075 return (Z_OK); 1076 } 1077 (void) close(doorfd); 1078 return (Z_ERR); 1079 } 1080 1081 static int 1082 call_zoneadmd(const char *zone_name, zone_cmd_arg_t *arg) 1083 { 1084 char doorpath[PATH_MAX]; 1085 int doorfd, result; 1086 door_arg_t darg; 1087 1088 zoneid_t zoneid; 1089 uint64_t uniqid = 0; 1090 1091 zone_cmd_rval_t *rvalp; 1092 size_t rlen; 1093 char *cp, *errbuf; 1094 1095 rlen = getpagesize(); 1096 if ((rvalp = malloc(rlen)) == NULL) { 1097 zerror(gettext("failed to allocate %lu bytes: %s"), rlen, 1098 strerror(errno)); 1099 return (-1); 1100 } 1101 1102 if ((zoneid = getzoneidbyname(zone_name)) != ZONE_ID_UNDEFINED) { 1103 (void) zone_getattr(zoneid, ZONE_ATTR_UNIQID, &uniqid, 1104 sizeof (uniqid)); 1105 } 1106 arg->uniqid = uniqid; 1107 (void) strlcpy(arg->locale, locale, sizeof (arg->locale)); 1108 if (!get_doorname(zone_name, doorpath)) { 1109 zerror(gettext("alternate root path is too long")); 1110 free(rvalp); 1111 return (-1); 1112 } 1113 1114 /* 1115 * Loop trying to start zoneadmd; if something goes seriously 1116 * wrong we break out and fail. 1117 */ 1118 for (;;) { 1119 if (start_zoneadmd(zone_name) != Z_OK) 1120 break; 1121 1122 if ((doorfd = open(doorpath, O_RDONLY)) < 0) { 1123 zperror(gettext("failed to open zone door"), B_FALSE); 1124 break; 1125 } 1126 1127 darg.data_ptr = (char *)arg; 1128 darg.data_size = sizeof (*arg); 1129 darg.desc_ptr = NULL; 1130 darg.desc_num = 0; 1131 darg.rbuf = (char *)rvalp; 1132 darg.rsize = rlen; 1133 if (door_call(doorfd, &darg) != 0) { 1134 (void) close(doorfd); 1135 /* 1136 * We'll get EBADF if the door has been revoked. 1137 */ 1138 if (errno != EBADF) { 1139 zperror(gettext("door_call failed"), B_FALSE); 1140 break; 1141 } 1142 continue; /* take another lap */ 1143 } 1144 (void) close(doorfd); 1145 1146 if (darg.data_size == 0) { 1147 /* Door server is going away; kick it again. */ 1148 continue; 1149 } 1150 1151 errbuf = rvalp->errbuf; 1152 while (*errbuf != '\0') { 1153 /* 1154 * Remove any newlines since zerror() 1155 * will append one automatically. 1156 */ 1157 cp = strchr(errbuf, '\n'); 1158 if (cp != NULL) 1159 *cp = '\0'; 1160 zerror("%s", errbuf); 1161 if (cp == NULL) 1162 break; 1163 errbuf = cp + 1; 1164 } 1165 result = rvalp->rval == 0 ? 0 : -1; 1166 free(rvalp); 1167 return (result); 1168 } 1169 1170 free(rvalp); 1171 return (-1); 1172 } 1173 1174 static int 1175 ready_func(int argc, char *argv[]) 1176 { 1177 zone_cmd_arg_t zarg; 1178 int arg; 1179 1180 if (zonecfg_in_alt_root()) { 1181 zerror(gettext("cannot ready zone in alternate root")); 1182 return (Z_ERR); 1183 } 1184 1185 optind = 0; 1186 if ((arg = getopt(argc, argv, "?")) != EOF) { 1187 switch (arg) { 1188 case '?': 1189 sub_usage(SHELP_READY, CMD_READY); 1190 return (optopt == '?' ? Z_OK : Z_USAGE); 1191 default: 1192 sub_usage(SHELP_READY, CMD_READY); 1193 return (Z_USAGE); 1194 } 1195 } 1196 if (argc > optind) { 1197 sub_usage(SHELP_READY, CMD_READY); 1198 return (Z_USAGE); 1199 } 1200 if (sanity_check(target_zone, CMD_READY, B_FALSE, B_FALSE) != Z_OK) 1201 return (Z_ERR); 1202 if (verify_details(CMD_READY) != Z_OK) 1203 return (Z_ERR); 1204 1205 zarg.cmd = Z_READY; 1206 if (call_zoneadmd(target_zone, &zarg) != 0) { 1207 zerror(gettext("call to %s failed"), "zoneadmd"); 1208 return (Z_ERR); 1209 } 1210 return (Z_OK); 1211 } 1212 1213 static int 1214 boot_func(int argc, char *argv[]) 1215 { 1216 zone_cmd_arg_t zarg; 1217 int arg; 1218 1219 if (zonecfg_in_alt_root()) { 1220 zerror(gettext("cannot boot zone in alternate root")); 1221 return (Z_ERR); 1222 } 1223 1224 zarg.bootbuf[0] = '\0'; 1225 1226 /* 1227 * At the current time, the only supported subargument to the 1228 * "boot" subcommand is "-s" which specifies a single-user boot. 1229 * In the future, other boot arguments should be supported 1230 * including "-m" for specifying alternate smf(5) milestones. 1231 */ 1232 optind = 0; 1233 if ((arg = getopt(argc, argv, "?s")) != EOF) { 1234 switch (arg) { 1235 case '?': 1236 sub_usage(SHELP_BOOT, CMD_BOOT); 1237 return (optopt == '?' ? Z_OK : Z_USAGE); 1238 case 's': 1239 (void) strlcpy(zarg.bootbuf, "-s", 1240 sizeof (zarg.bootbuf)); 1241 break; 1242 default: 1243 sub_usage(SHELP_BOOT, CMD_BOOT); 1244 return (Z_USAGE); 1245 } 1246 } 1247 if (argc > optind) { 1248 sub_usage(SHELP_BOOT, CMD_BOOT); 1249 return (Z_USAGE); 1250 } 1251 if (sanity_check(target_zone, CMD_BOOT, B_FALSE, B_FALSE) != Z_OK) 1252 return (Z_ERR); 1253 if (verify_details(CMD_BOOT) != Z_OK) 1254 return (Z_ERR); 1255 zarg.cmd = Z_BOOT; 1256 if (call_zoneadmd(target_zone, &zarg) != 0) { 1257 zerror(gettext("call to %s failed"), "zoneadmd"); 1258 return (Z_ERR); 1259 } 1260 return (Z_OK); 1261 } 1262 1263 static void 1264 fake_up_local_zone(zoneid_t zid, zone_entry_t *zeptr) 1265 { 1266 ssize_t result; 1267 1268 zeptr->zid = zid; 1269 /* 1270 * Since we're looking up our own (non-global) zone name, 1271 * we can be assured that it will succeed. 1272 */ 1273 result = getzonenamebyid(zid, zeptr->zname, sizeof (zeptr->zname)); 1274 assert(result >= 0); 1275 (void) strlcpy(zeptr->zroot, "/", sizeof (zeptr->zroot)); 1276 zeptr->zstate_str = "running"; 1277 } 1278 1279 static int 1280 list_func(int argc, char *argv[]) 1281 { 1282 zone_entry_t *zentp, zent; 1283 int arg, retv; 1284 boolean_t output = B_FALSE, verbose = B_FALSE, parsable = B_FALSE; 1285 zone_state_t min_state = ZONE_STATE_RUNNING; 1286 zoneid_t zone_id = getzoneid(); 1287 1288 if (target_zone == NULL) { 1289 /* all zones: default view to running but allow override */ 1290 optind = 0; 1291 while ((arg = getopt(argc, argv, "?cipv")) != EOF) { 1292 switch (arg) { 1293 case '?': 1294 sub_usage(SHELP_LIST, CMD_LIST); 1295 return (optopt == '?' ? Z_OK : Z_USAGE); 1296 case 'c': 1297 min_state = ZONE_STATE_CONFIGURED; 1298 break; 1299 case 'i': 1300 min_state = ZONE_STATE_INSTALLED; 1301 break; 1302 case 'p': 1303 parsable = B_TRUE; 1304 break; 1305 case 'v': 1306 verbose = B_TRUE; 1307 break; 1308 default: 1309 sub_usage(SHELP_LIST, CMD_LIST); 1310 return (Z_USAGE); 1311 } 1312 } 1313 if (parsable && verbose) { 1314 zerror(gettext("%s -p and -v are mutually exclusive."), 1315 cmd_to_str(CMD_LIST)); 1316 return (Z_ERR); 1317 } 1318 if (zone_id == GLOBAL_ZONEID) { 1319 retv = zone_print_list(min_state, verbose, parsable); 1320 } else { 1321 retv = Z_OK; 1322 fake_up_local_zone(zone_id, &zent); 1323 zone_print(&zent, verbose, parsable); 1324 } 1325 return (retv); 1326 } 1327 1328 /* 1329 * Specific target zone: disallow -i/-c suboptions. 1330 */ 1331 optind = 0; 1332 while ((arg = getopt(argc, argv, "?pv")) != EOF) { 1333 switch (arg) { 1334 case '?': 1335 sub_usage(SHELP_LIST, CMD_LIST); 1336 return (optopt == '?' ? Z_OK : Z_USAGE); 1337 case 'p': 1338 parsable = B_TRUE; 1339 break; 1340 case 'v': 1341 verbose = B_TRUE; 1342 break; 1343 default: 1344 sub_usage(SHELP_LIST, CMD_LIST); 1345 return (Z_USAGE); 1346 } 1347 } 1348 if (parsable && verbose) { 1349 zerror(gettext("%s -p and -v are mutually exclusive."), 1350 cmd_to_str(CMD_LIST)); 1351 return (Z_ERR); 1352 } 1353 if (argc > optind) { 1354 sub_usage(SHELP_LIST, CMD_LIST); 1355 return (Z_USAGE); 1356 } 1357 if (zone_id != GLOBAL_ZONEID) { 1358 fake_up_local_zone(zone_id, &zent); 1359 /* 1360 * main() will issue a Z_NO_ZONE error if it cannot get an 1361 * id for target_zone, which in a non-global zone should 1362 * happen for any zone name except `zonename`. Thus we 1363 * assert() that here but don't otherwise check. 1364 */ 1365 assert(strcmp(zent.zname, target_zone) == 0); 1366 zone_print(&zent, verbose, parsable); 1367 output = B_TRUE; 1368 } else if ((zentp = lookup_running_zone(target_zone)) != NULL) { 1369 zone_print(zentp, verbose, parsable); 1370 output = B_TRUE; 1371 } else if (lookup_zone_info(target_zone, ZONE_ID_UNDEFINED, 1372 &zent) == Z_OK) { 1373 zone_print(&zent, verbose, parsable); 1374 output = B_TRUE; 1375 } 1376 return (output ? Z_OK : Z_ERR); 1377 } 1378 1379 static void 1380 sigterm(int sig) 1381 { 1382 /* 1383 * Ignore SIG{INT,TERM}, so we don't end up in an infinite loop, 1384 * then propagate the signal to our process group. 1385 */ 1386 (void) sigset(SIGINT, SIG_IGN); 1387 (void) sigset(SIGTERM, SIG_IGN); 1388 (void) kill(0, sig); 1389 child_killed = B_TRUE; 1390 } 1391 1392 static int 1393 do_subproc(char *cmdbuf) 1394 { 1395 char inbuf[1024]; /* arbitrary large amount */ 1396 FILE *file; 1397 1398 child_killed = B_FALSE; 1399 /* 1400 * We use popen(3c) to launch child processes for [un]install; 1401 * this library call does not return a PID, so we have to kill 1402 * the whole process group. To avoid killing our parent, we 1403 * become a process group leader here. But doing so can wreak 1404 * havoc with reading from stdin when launched by a non-job-control 1405 * shell, so we close stdin and reopen it as /dev/null first. 1406 */ 1407 (void) close(STDIN_FILENO); 1408 (void) open("/dev/null", O_RDONLY); 1409 (void) setpgid(0, 0); 1410 (void) sigset(SIGINT, sigterm); 1411 (void) sigset(SIGTERM, sigterm); 1412 file = popen(cmdbuf, "r"); 1413 for (;;) { 1414 if (child_killed || fgets(inbuf, sizeof (inbuf), file) == NULL) 1415 break; 1416 (void) fputs(inbuf, stdout); 1417 } 1418 (void) sigset(SIGINT, SIG_DFL); 1419 (void) sigset(SIGTERM, SIG_DFL); 1420 return (pclose(file)); 1421 } 1422 1423 static int 1424 subproc_status(const char *cmd, int status) 1425 { 1426 if (WIFEXITED(status)) { 1427 int exit_code = WEXITSTATUS(status); 1428 1429 if (exit_code == 0) 1430 return (Z_OK); 1431 zerror(gettext("'%s' failed with exit code %d."), cmd, 1432 exit_code); 1433 } else if (WIFSIGNALED(status)) { 1434 int signal = WTERMSIG(status); 1435 char sigstr[SIG2STR_MAX]; 1436 1437 if (sig2str(signal, sigstr) == 0) { 1438 zerror(gettext("'%s' terminated by signal SIG%s."), cmd, 1439 sigstr); 1440 } else { 1441 zerror(gettext("'%s' terminated by an unknown signal."), 1442 cmd); 1443 } 1444 } else { 1445 zerror(gettext("'%s' failed for unknown reasons."), cmd); 1446 } 1447 return (Z_ERR); 1448 } 1449 1450 /* 1451 * Various sanity checks; make sure: 1452 * 1. We're in the global zone. 1453 * 2. The calling user has sufficient privilege. 1454 * 3. The target zone is neither the global zone nor anything starting with 1455 * "SUNW". 1456 * 4a. If we're looking for a 'not running' (i.e., configured or installed) 1457 * zone, the name service knows about it. 1458 * 4b. For some operations which expect a zone not to be running, that it is 1459 * not already running (or ready). 1460 */ 1461 static int 1462 sanity_check(char *zone, int cmd_num, boolean_t running, 1463 boolean_t unsafe_when_running) 1464 { 1465 zone_entry_t *zent; 1466 priv_set_t *privset; 1467 zone_state_t state; 1468 char kernzone[ZONENAME_MAX]; 1469 FILE *fp; 1470 1471 if (getzoneid() != GLOBAL_ZONEID) { 1472 zerror(gettext("must be in the global zone to %s a zone."), 1473 cmd_to_str(cmd_num)); 1474 return (Z_ERR); 1475 } 1476 1477 if ((privset = priv_allocset()) == NULL) { 1478 zerror(gettext("%s failed"), "priv_allocset"); 1479 return (Z_ERR); 1480 } 1481 1482 if (getppriv(PRIV_EFFECTIVE, privset) != 0) { 1483 zerror(gettext("%s failed"), "getppriv"); 1484 priv_freeset(privset); 1485 return (Z_ERR); 1486 } 1487 1488 if (priv_isfullset(privset) == B_FALSE) { 1489 zerror(gettext("only a privileged user may %s a zone."), 1490 cmd_to_str(cmd_num)); 1491 priv_freeset(privset); 1492 return (Z_ERR); 1493 } 1494 priv_freeset(privset); 1495 1496 if (zone == NULL) { 1497 zerror(gettext("no zone specified")); 1498 return (Z_ERR); 1499 } 1500 1501 if (strcmp(zone, GLOBAL_ZONENAME) == 0) { 1502 zerror(gettext("%s operation is invalid for the global zone."), 1503 cmd_to_str(cmd_num)); 1504 return (Z_ERR); 1505 } 1506 1507 if (strncmp(zone, "SUNW", 4) == 0) { 1508 zerror(gettext("%s operation is invalid for zones starting " 1509 "with SUNW."), cmd_to_str(cmd_num)); 1510 return (Z_ERR); 1511 } 1512 1513 if (!zonecfg_in_alt_root()) { 1514 zent = lookup_running_zone(zone); 1515 } else if ((fp = zonecfg_open_scratch("", B_FALSE)) == NULL) { 1516 zent = NULL; 1517 } else { 1518 if (zonecfg_find_scratch(fp, zone, zonecfg_get_root(), 1519 kernzone, sizeof (kernzone)) == 0) 1520 zent = lookup_running_zone(kernzone); 1521 else 1522 zent = NULL; 1523 zonecfg_close_scratch(fp); 1524 } 1525 1526 /* 1527 * Look up from the kernel for 'running' zones. 1528 */ 1529 if (running) { 1530 if (zent == NULL) { 1531 zerror(gettext("not running")); 1532 return (Z_ERR); 1533 } 1534 } else { 1535 int err; 1536 1537 if (unsafe_when_running && zent != NULL) { 1538 /* check whether the zone is ready or running */ 1539 if ((err = zone_get_state(zent->zname, 1540 &zent->zstate_num)) != Z_OK) { 1541 errno = err; 1542 zperror2(zent->zname, 1543 gettext("could not get state")); 1544 /* can't tell, so hedge */ 1545 zent->zstate_str = "ready/running"; 1546 } else { 1547 zent->zstate_str = 1548 zone_state_str(zent->zstate_num); 1549 } 1550 zerror(gettext("%s operation is invalid for %s zones."), 1551 cmd_to_str(cmd_num), zent->zstate_str); 1552 return (Z_ERR); 1553 } 1554 if ((err = zone_get_state(zone, &state)) != Z_OK) { 1555 errno = err; 1556 zperror2(zone, gettext("could not get state")); 1557 return (Z_ERR); 1558 } 1559 switch (cmd_num) { 1560 case CMD_UNINSTALL: 1561 if (state == ZONE_STATE_CONFIGURED) { 1562 zerror(gettext("is already in state '%s'."), 1563 zone_state_str(ZONE_STATE_CONFIGURED)); 1564 return (Z_ERR); 1565 } 1566 break; 1567 case CMD_INSTALL: 1568 if (state == ZONE_STATE_INSTALLED) { 1569 zerror(gettext("is already %s."), 1570 zone_state_str(ZONE_STATE_INSTALLED)); 1571 return (Z_ERR); 1572 } else if (state == ZONE_STATE_INCOMPLETE) { 1573 zerror(gettext("zone is %s; %s required."), 1574 zone_state_str(ZONE_STATE_INCOMPLETE), 1575 cmd_to_str(CMD_UNINSTALL)); 1576 return (Z_ERR); 1577 } 1578 break; 1579 case CMD_READY: 1580 case CMD_BOOT: 1581 case CMD_MOUNT: 1582 if (state < ZONE_STATE_INSTALLED) { 1583 zerror(gettext("must be %s before %s."), 1584 zone_state_str(ZONE_STATE_INSTALLED), 1585 cmd_to_str(cmd_num)); 1586 return (Z_ERR); 1587 } 1588 break; 1589 case CMD_VERIFY: 1590 if (state == ZONE_STATE_INCOMPLETE) { 1591 zerror(gettext("zone is %s; %s required."), 1592 zone_state_str(ZONE_STATE_INCOMPLETE), 1593 cmd_to_str(CMD_UNINSTALL)); 1594 return (Z_ERR); 1595 } 1596 break; 1597 case CMD_UNMOUNT: 1598 if (state != ZONE_STATE_MOUNTED) { 1599 zerror(gettext("must be %s before %s."), 1600 zone_state_str(ZONE_STATE_MOUNTED), 1601 cmd_to_str(cmd_num)); 1602 return (Z_ERR); 1603 } 1604 break; 1605 } 1606 } 1607 return (Z_OK); 1608 } 1609 1610 static int 1611 halt_func(int argc, char *argv[]) 1612 { 1613 zone_cmd_arg_t zarg; 1614 int arg; 1615 1616 if (zonecfg_in_alt_root()) { 1617 zerror(gettext("cannot halt zone in alternate root")); 1618 return (Z_ERR); 1619 } 1620 1621 optind = 0; 1622 if ((arg = getopt(argc, argv, "?")) != EOF) { 1623 switch (arg) { 1624 case '?': 1625 sub_usage(SHELP_HALT, CMD_HALT); 1626 return (optopt == '?' ? Z_OK : Z_USAGE); 1627 default: 1628 sub_usage(SHELP_HALT, CMD_HALT); 1629 return (Z_USAGE); 1630 } 1631 } 1632 if (argc > optind) { 1633 sub_usage(SHELP_HALT, CMD_HALT); 1634 return (Z_USAGE); 1635 } 1636 /* 1637 * zoneadmd should be the one to decide whether or not to proceed, 1638 * so even though it seems that the fourth parameter below should 1639 * perhaps be B_TRUE, it really shouldn't be. 1640 */ 1641 if (sanity_check(target_zone, CMD_HALT, B_FALSE, B_FALSE) != Z_OK) 1642 return (Z_ERR); 1643 1644 zarg.cmd = Z_HALT; 1645 return ((call_zoneadmd(target_zone, &zarg) == 0) ? Z_OK : Z_ERR); 1646 } 1647 1648 static int 1649 reboot_func(int argc, char *argv[]) 1650 { 1651 zone_cmd_arg_t zarg; 1652 int arg; 1653 1654 if (zonecfg_in_alt_root()) { 1655 zerror(gettext("cannot reboot zone in alternate root")); 1656 return (Z_ERR); 1657 } 1658 1659 optind = 0; 1660 if ((arg = getopt(argc, argv, "?")) != EOF) { 1661 switch (arg) { 1662 case '?': 1663 sub_usage(SHELP_REBOOT, CMD_REBOOT); 1664 return (optopt == '?' ? Z_OK : Z_USAGE); 1665 default: 1666 sub_usage(SHELP_REBOOT, CMD_REBOOT); 1667 return (Z_USAGE); 1668 } 1669 } 1670 if (argc > 0) { 1671 sub_usage(SHELP_REBOOT, CMD_REBOOT); 1672 return (Z_USAGE); 1673 } 1674 /* 1675 * zoneadmd should be the one to decide whether or not to proceed, 1676 * so even though it seems that the fourth parameter below should 1677 * perhaps be B_TRUE, it really shouldn't be. 1678 */ 1679 if (sanity_check(target_zone, CMD_REBOOT, B_TRUE, B_FALSE) != Z_OK) 1680 return (Z_ERR); 1681 1682 zarg.cmd = Z_REBOOT; 1683 return ((call_zoneadmd(target_zone, &zarg) == 0) ? Z_OK : Z_ERR); 1684 } 1685 1686 static int 1687 verify_rctls(zone_dochandle_t handle) 1688 { 1689 struct zone_rctltab rctltab; 1690 size_t rbs = rctlblk_size(); 1691 rctlblk_t *rctlblk; 1692 int error = Z_INVAL; 1693 1694 if ((rctlblk = malloc(rbs)) == NULL) { 1695 zerror(gettext("failed to allocate %lu bytes: %s"), rbs, 1696 strerror(errno)); 1697 return (Z_NOMEM); 1698 } 1699 1700 if (zonecfg_setrctlent(handle) != Z_OK) { 1701 zerror(gettext("zonecfg_setrctlent failed")); 1702 free(rctlblk); 1703 return (error); 1704 } 1705 1706 rctltab.zone_rctl_valptr = NULL; 1707 while (zonecfg_getrctlent(handle, &rctltab) == Z_OK) { 1708 struct zone_rctlvaltab *rctlval; 1709 const char *name = rctltab.zone_rctl_name; 1710 1711 if (!zonecfg_is_rctl(name)) { 1712 zerror(gettext("WARNING: Ignoring unrecognized rctl " 1713 "'%s'."), name); 1714 zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr); 1715 rctltab.zone_rctl_valptr = NULL; 1716 continue; 1717 } 1718 1719 for (rctlval = rctltab.zone_rctl_valptr; rctlval != NULL; 1720 rctlval = rctlval->zone_rctlval_next) { 1721 if (zonecfg_construct_rctlblk(rctlval, rctlblk) 1722 != Z_OK) { 1723 zerror(gettext("invalid rctl value: " 1724 "(priv=%s,limit=%s,action%s)"), 1725 rctlval->zone_rctlval_priv, 1726 rctlval->zone_rctlval_limit, 1727 rctlval->zone_rctlval_action); 1728 goto out; 1729 } 1730 if (!zonecfg_valid_rctl(name, rctlblk)) { 1731 zerror(gettext("(priv=%s,limit=%s,action=%s) " 1732 "is not a valid value for rctl '%s'"), 1733 rctlval->zone_rctlval_priv, 1734 rctlval->zone_rctlval_limit, 1735 rctlval->zone_rctlval_action, 1736 name); 1737 goto out; 1738 } 1739 } 1740 zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr); 1741 } 1742 rctltab.zone_rctl_valptr = NULL; 1743 error = Z_OK; 1744 out: 1745 zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr); 1746 (void) zonecfg_endrctlent(handle); 1747 free(rctlblk); 1748 return (error); 1749 } 1750 1751 static int 1752 verify_pool(zone_dochandle_t handle) 1753 { 1754 char poolname[MAXPATHLEN]; 1755 pool_conf_t *poolconf; 1756 pool_t *pool; 1757 int status; 1758 int error; 1759 1760 /* 1761 * This ends up being very similar to the check done in zoneadmd. 1762 */ 1763 error = zonecfg_get_pool(handle, poolname, sizeof (poolname)); 1764 if (error == Z_NO_ENTRY || (error == Z_OK && strlen(poolname) == 0)) { 1765 /* 1766 * No pool specified. 1767 */ 1768 return (0); 1769 } 1770 if (error != Z_OK) { 1771 zperror(gettext("Unable to retrieve pool name from " 1772 "configuration"), B_TRUE); 1773 return (error); 1774 } 1775 /* 1776 * Don't do anything if pools aren't enabled. 1777 */ 1778 if (pool_get_status(&status) != PO_SUCCESS || status != POOL_ENABLED) { 1779 zerror(gettext("WARNING: pools facility not active; " 1780 "zone will not be bound to pool '%s'."), poolname); 1781 return (Z_OK); 1782 } 1783 /* 1784 * Try to provide a sane error message if the requested pool doesn't 1785 * exist. It isn't clear that pools-related failures should 1786 * necessarily translate to a failure to verify the zone configuration, 1787 * hence they are not considered errors. 1788 */ 1789 if ((poolconf = pool_conf_alloc()) == NULL) { 1790 zerror(gettext("WARNING: pool_conf_alloc failed; " 1791 "using default pool")); 1792 return (Z_OK); 1793 } 1794 if (pool_conf_open(poolconf, pool_dynamic_location(), PO_RDONLY) != 1795 PO_SUCCESS) { 1796 zerror(gettext("WARNING: pool_conf_open failed; " 1797 "using default pool")); 1798 pool_conf_free(poolconf); 1799 return (Z_OK); 1800 } 1801 pool = pool_get_pool(poolconf, poolname); 1802 (void) pool_conf_close(poolconf); 1803 pool_conf_free(poolconf); 1804 if (pool == NULL) { 1805 zerror(gettext("WARNING: pool '%s' not found. " 1806 "using default pool"), poolname); 1807 } 1808 1809 return (Z_OK); 1810 } 1811 1812 static int 1813 verify_filesystems(zone_dochandle_t handle) 1814 { 1815 int return_code = Z_OK; 1816 struct zone_fstab fstab; 1817 char cmdbuf[MAXPATHLEN]; 1818 struct stat st; 1819 1820 /* 1821 * No need to verify inherit-pkg-dir fs types, as their type is 1822 * implicitly lofs, which is known. Therefore, the types are only 1823 * verified for regular filesystems below. 1824 * 1825 * Since the actual mount point is not known until the dependent mounts 1826 * are performed, we don't attempt any path validation here: that will 1827 * happen later when zoneadmd actually does the mounts. 1828 */ 1829 if (zonecfg_setfsent(handle) != Z_OK) { 1830 (void) fprintf(stderr, gettext("cannot verify file-systems: " 1831 "unable to enumerate mounts\n")); 1832 return (Z_ERR); 1833 } 1834 while (zonecfg_getfsent(handle, &fstab) == Z_OK) { 1835 if (!zonecfg_valid_fs_type(fstab.zone_fs_type)) { 1836 (void) fprintf(stderr, gettext("cannot verify fs %s: " 1837 "type %s is not allowed.\n"), fstab.zone_fs_dir, 1838 fstab.zone_fs_type); 1839 return_code = Z_ERR; 1840 goto next_fs; 1841 } 1842 /* 1843 * Verify /usr/lib/fs/<fstype>/mount exists. 1844 */ 1845 if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/mount", 1846 fstab.zone_fs_type) > sizeof (cmdbuf)) { 1847 (void) fprintf(stderr, gettext("cannot verify fs %s: " 1848 "type %s is too long.\n"), fstab.zone_fs_dir, 1849 fstab.zone_fs_type); 1850 return_code = Z_ERR; 1851 goto next_fs; 1852 } 1853 if (stat(cmdbuf, &st) != 0) { 1854 (void) fprintf(stderr, gettext("cannot verify fs %s: " 1855 "can't access %s: %s\n"), fstab.zone_fs_dir, 1856 cmdbuf, strerror(errno)); 1857 return_code = Z_ERR; 1858 goto next_fs; 1859 } 1860 if (!S_ISREG(st.st_mode)) { 1861 (void) fprintf(stderr, gettext("cannot verify fs %s: " 1862 "%s is not a regular file\n"), fstab.zone_fs_dir, 1863 cmdbuf); 1864 return_code = Z_ERR; 1865 goto next_fs; 1866 } 1867 /* 1868 * Verify /usr/lib/fs/<fstype>/fsck exists iff zone_fs_raw is 1869 * set. 1870 */ 1871 if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/fsck", 1872 fstab.zone_fs_type) > sizeof (cmdbuf)) { 1873 (void) fprintf(stderr, gettext("cannot verify fs %s: " 1874 "type %s is too long.\n"), fstab.zone_fs_dir, 1875 fstab.zone_fs_type); 1876 return_code = Z_ERR; 1877 goto next_fs; 1878 } 1879 if (fstab.zone_fs_raw[0] == '\0' && stat(cmdbuf, &st) == 0) { 1880 (void) fprintf(stderr, gettext("cannot verify fs %s: " 1881 "must specify 'raw' device for %s file-systems\n"), 1882 fstab.zone_fs_dir, fstab.zone_fs_type); 1883 return_code = Z_ERR; 1884 goto next_fs; 1885 } 1886 if (fstab.zone_fs_raw[0] != '\0' && 1887 (stat(cmdbuf, &st) != 0 || !S_ISREG(st.st_mode))) { 1888 (void) fprintf(stderr, gettext("cannot verify fs %s: " 1889 "'raw' device specified but " 1890 "no fsck executable exists for %s\n"), 1891 fstab.zone_fs_dir, fstab.zone_fs_type); 1892 return_code = Z_ERR; 1893 goto next_fs; 1894 } 1895 next_fs: 1896 zonecfg_free_fs_option_list(fstab.zone_fs_options); 1897 } 1898 (void) zonecfg_endfsent(handle); 1899 1900 return (return_code); 1901 } 1902 1903 const char *current_dataset; 1904 1905 /* 1906 * Custom error handler for errors incurred as part of the checks below. We 1907 * want to trim off the leading 'cannot open ...' to create a better error 1908 * message. The only other way this can fail is if we fail to set the 'zoned' 1909 * property. In this case we just pass the error on verbatim. 1910 */ 1911 static void 1912 zfs_error_handler(const char *fmt, va_list ap) 1913 { 1914 char buf[1024]; 1915 1916 (void) vsnprintf(buf, sizeof (buf), fmt, ap); 1917 1918 if (strncmp(gettext("cannot open "), buf, 1919 strlen(gettext("cannot open "))) == 0) 1920 (void) fprintf(stderr, gettext("cannot verify zfs " 1921 "dataset %s%s\n"), current_dataset, strchr(buf, ':')); 1922 else 1923 (void) fprintf(stderr, gettext("cannot verify zfs dataset " 1924 "%s: %s\n"), current_dataset, buf); 1925 } 1926 1927 /* ARGSUSED */ 1928 static int 1929 check_zvol(zfs_handle_t *zhp, void *unused) 1930 { 1931 int ret; 1932 1933 if (zfs_get_type(zhp) == ZFS_TYPE_VOLUME) { 1934 (void) fprintf(stderr, gettext("cannot verify zfs dataset %s: " 1935 "volumes cannot be specified as a zone dataset resource\n"), 1936 zfs_get_name(zhp)); 1937 ret = -1; 1938 } else { 1939 ret = zfs_iter_children(zhp, check_zvol, NULL); 1940 } 1941 1942 zfs_close(zhp); 1943 1944 return (ret); 1945 } 1946 1947 /* 1948 * Validate that the given dataset exists on the system, and that neither it nor 1949 * its children are zvols. 1950 * 1951 * Note that we don't do anything with the 'zoned' property here. All 1952 * management is done in zoneadmd when the zone is actually rebooted. This 1953 * allows us to automatically set the zoned property even when a zone is 1954 * rebooted by the administrator. 1955 */ 1956 static int 1957 verify_datasets(zone_dochandle_t handle) 1958 { 1959 int return_code = Z_OK; 1960 struct zone_dstab dstab; 1961 zfs_handle_t *zhp; 1962 char propbuf[ZFS_MAXPROPLEN]; 1963 char source[ZFS_MAXNAMELEN]; 1964 zfs_source_t srctype; 1965 1966 if (zonecfg_setdsent(handle) != Z_OK) { 1967 (void) fprintf(stderr, gettext("cannot verify zfs datasets: " 1968 "unable to enumerate datasets\n")); 1969 return (Z_ERR); 1970 } 1971 1972 zfs_set_error_handler(zfs_error_handler); 1973 1974 while (zonecfg_getdsent(handle, &dstab) == Z_OK) { 1975 1976 current_dataset = dstab.zone_dataset_name; 1977 1978 if ((zhp = zfs_open(dstab.zone_dataset_name, 1979 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME)) == NULL) { 1980 return_code = Z_ERR; 1981 continue; 1982 } 1983 1984 if (zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, propbuf, 1985 sizeof (propbuf), &srctype, source, 1986 sizeof (source), 0) == 0 && 1987 (srctype == ZFS_SRC_INHERITED)) { 1988 (void) fprintf(stderr, gettext("cannot verify zfs " 1989 "dataset %s: mountpoint cannot be inherited\n"), 1990 dstab.zone_dataset_name); 1991 return_code = Z_ERR; 1992 zfs_close(zhp); 1993 continue; 1994 } 1995 1996 if (zfs_get_type(zhp) == ZFS_TYPE_VOLUME) { 1997 (void) fprintf(stderr, gettext("cannot verify zfs " 1998 "dataset %s: volumes cannot be specified as a " 1999 "zone dataset resource\n"), 2000 dstab.zone_dataset_name); 2001 return_code = Z_ERR; 2002 } 2003 2004 if (zfs_iter_children(zhp, check_zvol, NULL) != 0) 2005 return_code = Z_ERR; 2006 2007 zfs_close(zhp); 2008 } 2009 (void) zonecfg_enddsent(handle); 2010 2011 return (return_code); 2012 } 2013 2014 static int 2015 verify_details(int cmd_num) 2016 { 2017 zone_dochandle_t handle; 2018 struct zone_nwiftab nwiftab; 2019 char zonepath[MAXPATHLEN], checkpath[MAXPATHLEN]; 2020 int return_code = Z_OK; 2021 int err; 2022 boolean_t in_alt_root; 2023 2024 if ((handle = zonecfg_init_handle()) == NULL) { 2025 zperror(cmd_to_str(cmd_num), B_TRUE); 2026 return (Z_ERR); 2027 } 2028 if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) { 2029 errno = err; 2030 zperror(cmd_to_str(cmd_num), B_TRUE); 2031 zonecfg_fini_handle(handle); 2032 return (Z_ERR); 2033 } 2034 if ((err = zonecfg_get_zonepath(handle, zonepath, sizeof (zonepath))) != 2035 Z_OK) { 2036 errno = err; 2037 zperror(cmd_to_str(cmd_num), B_TRUE); 2038 zonecfg_fini_handle(handle); 2039 return (Z_ERR); 2040 } 2041 /* 2042 * zonecfg_get_zonepath() gets its data from the XML repository. 2043 * Verify this against the index file, which is checked first by 2044 * zone_get_zonepath(). If they don't match, bail out. 2045 */ 2046 if ((err = zone_get_zonepath(target_zone, checkpath, 2047 sizeof (checkpath))) != Z_OK) { 2048 errno = err; 2049 zperror2(target_zone, gettext("could not get zone path")); 2050 return (Z_ERR); 2051 } 2052 if (strcmp(zonepath, checkpath) != 0) { 2053 (void) fprintf(stderr, gettext("The XML repository has " 2054 "zonepath '%s',\nbut the index file has zonepath '%s'.\n" 2055 "These must match, so fix the incorrect entry.\n"), 2056 zonepath, checkpath); 2057 return (Z_ERR); 2058 } 2059 if (validate_zonepath(zonepath, cmd_num) != Z_OK) { 2060 (void) fprintf(stderr, gettext("could not verify zonepath %s " 2061 "because of the above errors.\n"), zonepath); 2062 return_code = Z_ERR; 2063 } 2064 2065 in_alt_root = zonecfg_in_alt_root(); 2066 if (in_alt_root) 2067 goto no_net; 2068 2069 if ((err = zonecfg_setnwifent(handle)) != Z_OK) { 2070 errno = err; 2071 zperror(cmd_to_str(cmd_num), B_TRUE); 2072 zonecfg_fini_handle(handle); 2073 return (Z_ERR); 2074 } 2075 while (zonecfg_getnwifent(handle, &nwiftab) == Z_OK) { 2076 struct lifreq lifr; 2077 sa_family_t af; 2078 int so, res; 2079 2080 /* skip any loopback interfaces */ 2081 if (strcmp(nwiftab.zone_nwif_physical, "lo0") == 0) 2082 continue; 2083 if ((res = zonecfg_valid_net_address(nwiftab.zone_nwif_address, 2084 &lifr)) != Z_OK) { 2085 (void) fprintf(stderr, gettext("could not verify %s " 2086 "%s=%s %s=%s: %s\n"), "net", "address", 2087 nwiftab.zone_nwif_address, "physical", 2088 nwiftab.zone_nwif_physical, zonecfg_strerror(res)); 2089 return_code = Z_ERR; 2090 continue; 2091 } 2092 af = lifr.lifr_addr.ss_family; 2093 (void) memset(&lifr, 0, sizeof (lifr)); 2094 (void) strlcpy(lifr.lifr_name, nwiftab.zone_nwif_physical, 2095 sizeof (lifr.lifr_name)); 2096 lifr.lifr_addr.ss_family = af; 2097 if ((so = socket(af, SOCK_DGRAM, 0)) < 0) { 2098 (void) fprintf(stderr, gettext("could not verify %s " 2099 "%s=%s %s=%s: could not get socket: %s\n"), "net", 2100 "address", nwiftab.zone_nwif_address, "physical", 2101 nwiftab.zone_nwif_physical, strerror(errno)); 2102 return_code = Z_ERR; 2103 continue; 2104 } 2105 if (ioctl(so, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) { 2106 (void) fprintf(stderr, 2107 gettext("could not verify %s %s=%s %s=%s: %s\n"), 2108 "net", "address", nwiftab.zone_nwif_address, 2109 "physical", nwiftab.zone_nwif_physical, 2110 strerror(errno)); 2111 return_code = Z_ERR; 2112 } 2113 (void) close(so); 2114 } 2115 (void) zonecfg_endnwifent(handle); 2116 no_net: 2117 2118 if (verify_filesystems(handle) != Z_OK) 2119 return_code = Z_ERR; 2120 if (!in_alt_root && verify_rctls(handle) != Z_OK) 2121 return_code = Z_ERR; 2122 if (!in_alt_root && verify_pool(handle) != Z_OK) 2123 return_code = Z_ERR; 2124 if (!in_alt_root && verify_datasets(handle) != Z_OK) 2125 return_code = Z_ERR; 2126 zonecfg_fini_handle(handle); 2127 if (return_code == Z_ERR) 2128 (void) fprintf(stderr, 2129 gettext("%s: zone %s failed to verify\n"), 2130 execname, target_zone); 2131 return (return_code); 2132 } 2133 2134 static int 2135 verify_func(int argc, char *argv[]) 2136 { 2137 int arg; 2138 2139 optind = 0; 2140 if ((arg = getopt(argc, argv, "?")) != EOF) { 2141 switch (arg) { 2142 case '?': 2143 sub_usage(SHELP_VERIFY, CMD_VERIFY); 2144 return (optopt == '?' ? Z_OK : Z_USAGE); 2145 default: 2146 sub_usage(SHELP_VERIFY, CMD_VERIFY); 2147 return (Z_USAGE); 2148 } 2149 } 2150 if (argc > optind) { 2151 sub_usage(SHELP_VERIFY, CMD_VERIFY); 2152 return (Z_USAGE); 2153 } 2154 if (sanity_check(target_zone, CMD_VERIFY, B_FALSE, B_FALSE) != Z_OK) 2155 return (Z_ERR); 2156 return (verify_details(CMD_VERIFY)); 2157 } 2158 2159 #define LUCREATEZONE "/usr/lib/lu/lucreatezone" 2160 2161 static int 2162 install_func(int argc, char *argv[]) 2163 { 2164 /* 9: "exec " and " -z " */ 2165 char cmdbuf[sizeof (LUCREATEZONE) + ZONENAME_MAX + 9]; 2166 int lockfd; 2167 int err, arg; 2168 char zonepath[MAXPATHLEN]; 2169 int status; 2170 2171 if (zonecfg_in_alt_root()) { 2172 zerror(gettext("cannot install zone in alternate root")); 2173 return (Z_ERR); 2174 } 2175 2176 optind = 0; 2177 if ((arg = getopt(argc, argv, "?")) != EOF) { 2178 switch (arg) { 2179 case '?': 2180 sub_usage(SHELP_INSTALL, CMD_INSTALL); 2181 return (optopt == '?' ? Z_OK : Z_USAGE); 2182 default: 2183 sub_usage(SHELP_INSTALL, CMD_INSTALL); 2184 return (Z_USAGE); 2185 } 2186 } 2187 if (argc > optind) { 2188 sub_usage(SHELP_INSTALL, CMD_INSTALL); 2189 return (Z_USAGE); 2190 } 2191 if (sanity_check(target_zone, CMD_INSTALL, B_FALSE, B_TRUE) != Z_OK) 2192 return (Z_ERR); 2193 if (verify_details(CMD_INSTALL) != Z_OK) 2194 return (Z_ERR); 2195 2196 if (grab_lock_file(target_zone, &lockfd) != Z_OK) { 2197 zerror(gettext("another %s may have an operation in progress."), 2198 "zoneadmd"); 2199 return (Z_ERR); 2200 } 2201 err = zone_set_state(target_zone, ZONE_STATE_INCOMPLETE); 2202 if (err != Z_OK) { 2203 errno = err; 2204 zperror2(target_zone, gettext("could not set state")); 2205 goto done; 2206 } 2207 2208 /* 2209 * According to the Application Packaging Developer's Guide, a 2210 * "checkinstall" script when included in a package is executed as 2211 * the user "install", if such a user exists, or by the user 2212 * "nobody". In order to support this dubious behavior, the path 2213 * to the zone being constructed is opened up during the life of 2214 * the command laying down the zone's root file system. Once this 2215 * has completed, regardless of whether it was successful, the 2216 * path to the zone is again restricted. 2217 */ 2218 if ((err = zone_get_zonepath(target_zone, zonepath, 2219 sizeof (zonepath))) != Z_OK) { 2220 errno = err; 2221 zperror2(target_zone, gettext("could not get zone path")); 2222 goto done; 2223 } 2224 if (chmod(zonepath, DEFAULT_DIR_MODE) != 0) { 2225 zperror(zonepath, B_FALSE); 2226 err = Z_ERR; 2227 goto done; 2228 } 2229 2230 /* 2231 * "exec" the command so that the returned status is that of 2232 * LUCREATEZONE and not the shell. 2233 */ 2234 (void) snprintf(cmdbuf, sizeof (cmdbuf), "exec " LUCREATEZONE " -z %s", 2235 target_zone); 2236 status = do_subproc(cmdbuf); 2237 if (chmod(zonepath, S_IRWXU) != 0) { 2238 zperror(zonepath, B_FALSE); 2239 err = Z_ERR; 2240 goto done; 2241 } 2242 if ((err = subproc_status(LUCREATEZONE, status)) != Z_OK) 2243 goto done; 2244 2245 if ((err = zone_set_state(target_zone, ZONE_STATE_INSTALLED)) != Z_OK) { 2246 errno = err; 2247 zperror2(target_zone, gettext("could not set state")); 2248 goto done; 2249 } 2250 2251 done: 2252 release_lock_file(lockfd); 2253 return ((err == Z_OK) ? Z_OK : Z_ERR); 2254 } 2255 2256 /* 2257 * On input, TRUE => yes, FALSE => no. 2258 * On return, TRUE => 1, FALSE => 0, could not ask => -1. 2259 */ 2260 2261 static int 2262 ask_yesno(boolean_t default_answer, const char *question) 2263 { 2264 char line[64]; /* should be large enough to answer yes or no */ 2265 2266 if (!isatty(STDIN_FILENO)) 2267 return (-1); 2268 for (;;) { 2269 (void) printf("%s (%s)? ", question, 2270 default_answer ? "[y]/n" : "y/[n]"); 2271 if (fgets(line, sizeof (line), stdin) == NULL || 2272 line[0] == '\n') 2273 return (default_answer ? 1 : 0); 2274 if (tolower(line[0]) == 'y') 2275 return (1); 2276 if (tolower(line[0]) == 'n') 2277 return (0); 2278 } 2279 } 2280 2281 #define RMCOMMAND "/usr/bin/rm -rf" 2282 2283 /* ARGSUSED */ 2284 int 2285 zfm_print(const char *p, void *r) { 2286 zerror(" %s\n", p); 2287 return (0); 2288 } 2289 2290 static int 2291 uninstall_func(int argc, char *argv[]) 2292 { 2293 /* 6: "exec " and " " */ 2294 char cmdbuf[sizeof (RMCOMMAND) + MAXPATHLEN + 6]; 2295 char line[ZONENAME_MAX + 128]; /* Enough for "Are you sure ..." */ 2296 char rootpath[MAXPATHLEN], devpath[MAXPATHLEN]; 2297 boolean_t force = B_FALSE; 2298 int lockfd, answer; 2299 int err, arg; 2300 int status; 2301 2302 if (zonecfg_in_alt_root()) { 2303 zerror(gettext("cannot uninstall zone in alternate root")); 2304 return (Z_ERR); 2305 } 2306 2307 optind = 0; 2308 while ((arg = getopt(argc, argv, "?F")) != EOF) { 2309 switch (arg) { 2310 case '?': 2311 sub_usage(SHELP_UNINSTALL, CMD_UNINSTALL); 2312 return (optopt == '?' ? Z_OK : Z_USAGE); 2313 case 'F': 2314 force = B_TRUE; 2315 break; 2316 default: 2317 sub_usage(SHELP_UNINSTALL, CMD_UNINSTALL); 2318 return (Z_USAGE); 2319 } 2320 } 2321 if (argc > optind) { 2322 sub_usage(SHELP_UNINSTALL, CMD_UNINSTALL); 2323 return (Z_USAGE); 2324 } 2325 2326 if (sanity_check(target_zone, CMD_UNINSTALL, B_FALSE, B_TRUE) != Z_OK) 2327 return (Z_ERR); 2328 2329 if (!force) { 2330 (void) snprintf(line, sizeof (line), 2331 gettext("Are you sure you want to %s zone %s"), 2332 cmd_to_str(CMD_UNINSTALL), target_zone); 2333 if ((answer = ask_yesno(B_FALSE, line)) == 0) { 2334 return (Z_OK); 2335 } else if (answer == -1) { 2336 zerror(gettext("Input not from terminal and -F " 2337 "not specified: %s not done."), 2338 cmd_to_str(CMD_UNINSTALL)); 2339 return (Z_ERR); 2340 } 2341 } 2342 2343 if ((err = zone_get_zonepath(target_zone, devpath, 2344 sizeof (devpath))) != Z_OK) { 2345 errno = err; 2346 zperror2(target_zone, gettext("could not get zone path")); 2347 return (Z_ERR); 2348 } 2349 (void) strlcat(devpath, "/dev", sizeof (devpath)); 2350 if ((err = zone_get_rootpath(target_zone, rootpath, 2351 sizeof (rootpath))) != Z_OK) { 2352 errno = err; 2353 zperror2(target_zone, gettext("could not get root path")); 2354 return (Z_ERR); 2355 } 2356 2357 /* 2358 * If there seems to be a zoneadmd running for this zone, call it 2359 * to tell it that an uninstall is happening; if all goes well it 2360 * will then shut itself down. 2361 */ 2362 if (ping_zoneadmd(target_zone) == Z_OK) { 2363 zone_cmd_arg_t zarg; 2364 zarg.cmd = Z_NOTE_UNINSTALLING; 2365 /* we don't care too much if this fails... just plow on */ 2366 (void) call_zoneadmd(target_zone, &zarg); 2367 } 2368 2369 if (grab_lock_file(target_zone, &lockfd) != Z_OK) { 2370 zerror(gettext("another %s may have an operation in progress."), 2371 "zoneadmd"); 2372 return (Z_ERR); 2373 } 2374 2375 /* Don't uninstall the zone if anything is mounted there */ 2376 err = zonecfg_find_mounts(rootpath, NULL, NULL); 2377 if (err) { 2378 zerror(gettext("These file-systems are mounted on " 2379 "subdirectories of %s.\n"), rootpath); 2380 (void) zonecfg_find_mounts(rootpath, zfm_print, NULL); 2381 return (Z_ERR); 2382 } 2383 2384 err = zone_set_state(target_zone, ZONE_STATE_INCOMPLETE); 2385 if (err != Z_OK) { 2386 errno = err; 2387 zperror2(target_zone, gettext("could not set state")); 2388 goto bad; 2389 } 2390 2391 /* 2392 * "exec" the command so that the returned status is that of 2393 * RMCOMMAND and not the shell. 2394 */ 2395 (void) snprintf(cmdbuf, sizeof (cmdbuf), "exec " RMCOMMAND " %s", 2396 devpath); 2397 status = do_subproc(cmdbuf); 2398 if ((err = subproc_status(RMCOMMAND, status)) != Z_OK) 2399 goto bad; 2400 (void) snprintf(cmdbuf, sizeof (cmdbuf), "exec " RMCOMMAND " %s", 2401 rootpath); 2402 status = do_subproc(cmdbuf); 2403 if ((err = subproc_status(RMCOMMAND, status)) != Z_OK) 2404 goto bad; 2405 err = zone_set_state(target_zone, ZONE_STATE_CONFIGURED); 2406 if (err != Z_OK) { 2407 errno = err; 2408 zperror2(target_zone, gettext("could not reset state")); 2409 } 2410 bad: 2411 release_lock_file(lockfd); 2412 return (err); 2413 } 2414 2415 /* ARGSUSED */ 2416 static int 2417 mount_func(int argc, char *argv[]) 2418 { 2419 zone_cmd_arg_t zarg; 2420 2421 if (argc > 0) 2422 return (Z_USAGE); 2423 if (sanity_check(target_zone, CMD_MOUNT, B_FALSE, B_FALSE) != Z_OK) 2424 return (Z_ERR); 2425 if (verify_details(CMD_MOUNT) != Z_OK) 2426 return (Z_ERR); 2427 2428 zarg.cmd = Z_MOUNT; 2429 if (call_zoneadmd(target_zone, &zarg) != 0) { 2430 zerror(gettext("call to %s failed"), "zoneadmd"); 2431 return (Z_ERR); 2432 } 2433 return (Z_OK); 2434 } 2435 2436 /* ARGSUSED */ 2437 static int 2438 unmount_func(int argc, char *argv[]) 2439 { 2440 zone_cmd_arg_t zarg; 2441 2442 if (argc > 0) 2443 return (Z_USAGE); 2444 if (sanity_check(target_zone, CMD_UNMOUNT, B_FALSE, B_FALSE) != Z_OK) 2445 return (Z_ERR); 2446 2447 zarg.cmd = Z_UNMOUNT; 2448 if (call_zoneadmd(target_zone, &zarg) != 0) { 2449 zerror(gettext("call to %s failed"), "zoneadmd"); 2450 return (Z_ERR); 2451 } 2452 return (Z_OK); 2453 } 2454 2455 static int 2456 help_func(int argc, char *argv[]) 2457 { 2458 int arg, cmd_num; 2459 2460 if (argc == 0) { 2461 (void) usage(B_TRUE); 2462 return (Z_OK); 2463 } 2464 optind = 0; 2465 if ((arg = getopt(argc, argv, "?")) != EOF) { 2466 switch (arg) { 2467 case '?': 2468 sub_usage(SHELP_HELP, CMD_HELP); 2469 return (optopt == '?' ? Z_OK : Z_USAGE); 2470 default: 2471 sub_usage(SHELP_HELP, CMD_HELP); 2472 return (Z_USAGE); 2473 } 2474 } 2475 while (optind < argc) { 2476 if ((cmd_num = cmd_match(argv[optind])) < 0) { 2477 sub_usage(SHELP_HELP, CMD_HELP); 2478 return (Z_USAGE); 2479 } 2480 sub_usage(cmdtab[cmd_num].short_usage, cmd_num); 2481 optind++; 2482 } 2483 return (Z_OK); 2484 } 2485 2486 /* 2487 * Returns: CMD_MIN thru CMD_MAX on success, -1 on error 2488 */ 2489 2490 static int 2491 cmd_match(char *cmd) 2492 { 2493 int i; 2494 2495 for (i = CMD_MIN; i <= CMD_MAX; i++) { 2496 /* return only if there is an exact match */ 2497 if (strcmp(cmd, cmdtab[i].cmd_name) == 0) 2498 return (cmdtab[i].cmd_num); 2499 } 2500 return (-1); 2501 } 2502 2503 static int 2504 parse_and_run(int argc, char *argv[]) 2505 { 2506 int i = cmd_match(argv[0]); 2507 2508 if (i < 0) 2509 return (usage(B_FALSE)); 2510 return (cmdtab[i].handler(argc - 1, &(argv[1]))); 2511 } 2512 2513 static char * 2514 get_execbasename(char *execfullname) 2515 { 2516 char *last_slash, *execbasename; 2517 2518 /* guard against '/' at end of command invocation */ 2519 for (;;) { 2520 last_slash = strrchr(execfullname, '/'); 2521 if (last_slash == NULL) { 2522 execbasename = execfullname; 2523 break; 2524 } else { 2525 execbasename = last_slash + 1; 2526 if (*execbasename == '\0') { 2527 *last_slash = '\0'; 2528 continue; 2529 } 2530 break; 2531 } 2532 } 2533 return (execbasename); 2534 } 2535 2536 int 2537 main(int argc, char **argv) 2538 { 2539 int arg; 2540 zoneid_t zid; 2541 struct stat st; 2542 2543 if ((locale = setlocale(LC_ALL, "")) == NULL) 2544 locale = "C"; 2545 (void) textdomain(TEXT_DOMAIN); 2546 setbuf(stdout, NULL); 2547 (void) sigset(SIGHUP, SIG_IGN); 2548 execname = get_execbasename(argv[0]); 2549 target_zone = NULL; 2550 if (chdir("/") != 0) { 2551 zerror(gettext("could not change directory to /.")); 2552 exit(Z_ERR); 2553 } 2554 2555 while ((arg = getopt(argc, argv, "?z:R:")) != EOF) { 2556 switch (arg) { 2557 case '?': 2558 return (usage(B_TRUE)); 2559 case 'z': 2560 target_zone = optarg; 2561 break; 2562 case 'R': /* private option for admin/install use */ 2563 if (*optarg != '/') { 2564 zerror(gettext("root path must be absolute.")); 2565 exit(Z_ERR); 2566 } 2567 if (stat(optarg, &st) == -1 || !S_ISDIR(st.st_mode)) { 2568 zerror( 2569 gettext("root path must be a directory.")); 2570 exit(Z_ERR); 2571 } 2572 zonecfg_set_root(optarg); 2573 break; 2574 default: 2575 return (usage(B_FALSE)); 2576 } 2577 } 2578 2579 if (optind >= argc) 2580 return (usage(B_FALSE)); 2581 if (target_zone != NULL && zone_get_id(target_zone, &zid) != 0) { 2582 errno = Z_NO_ZONE; 2583 zperror(target_zone, B_TRUE); 2584 exit(Z_ERR); 2585 } 2586 return (parse_and_run(argc - optind, &argv[optind])); 2587 } 2588