Print this page
5432 zoneadm install could fix zonepath permissions automatically
Reviewed by: Jerry Jelinek <jerry.jelinek@joyent.com>
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/cmd/zoneadm/zoneadm.c
+++ new/usr/src/cmd/zoneadm/zoneadm.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21
22 22 /*
23 23 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24 24 * Copyright 2014 Nexenta Systems, Inc. All rights reserved.
25 25 */
26 26
27 27 /*
28 28 * zoneadm is a command interpreter for zone administration. It is all in
29 29 * C (i.e., no lex/yacc), and all the argument passing is argc/argv based.
30 30 * main() calls parse_and_run() which calls cmd_match(), then invokes the
31 31 * appropriate command's handler function. The rest of the program is the
32 32 * handler functions and their helper functions.
33 33 *
34 34 * Some of the helper functions are used largely to simplify I18N: reducing
35 35 * the need for translation notes. This is particularly true of many of
36 36 * the zerror() calls: doing e.g. zerror(gettext("%s failed"), "foo") rather
37 37 * than zerror(gettext("foo failed")) with a translation note indicating
38 38 * that "foo" need not be translated.
39 39 */
40 40
41 41 #include <stdio.h>
42 42 #include <errno.h>
43 43 #include <unistd.h>
44 44 #include <signal.h>
45 45 #include <stdarg.h>
46 46 #include <ctype.h>
47 47 #include <stdlib.h>
48 48 #include <string.h>
49 49 #include <wait.h>
50 50 #include <zone.h>
51 51 #include <priv.h>
52 52 #include <locale.h>
53 53 #include <libintl.h>
54 54 #include <libzonecfg.h>
55 55 #include <bsm/adt.h>
56 56 #include <sys/brand.h>
57 57 #include <sys/param.h>
58 58 #include <sys/types.h>
59 59 #include <sys/stat.h>
60 60 #include <sys/statvfs.h>
61 61 #include <assert.h>
62 62 #include <sys/sockio.h>
63 63 #include <sys/mntent.h>
64 64 #include <limits.h>
65 65 #include <dirent.h>
66 66 #include <uuid/uuid.h>
67 67 #include <fcntl.h>
68 68 #include <door.h>
69 69 #include <macros.h>
70 70 #include <libgen.h>
71 71 #include <fnmatch.h>
72 72 #include <sys/modctl.h>
73 73 #include <libbrand.h>
74 74 #include <libscf.h>
75 75 #include <procfs.h>
76 76 #include <strings.h>
77 77 #include <pool.h>
78 78 #include <sys/pool.h>
79 79 #include <sys/priocntl.h>
80 80 #include <sys/fsspriocntl.h>
81 81 #include <libdladm.h>
82 82 #include <libdllink.h>
83 83 #include <pwd.h>
84 84 #include <auth_list.h>
85 85 #include <auth_attr.h>
86 86 #include <secdb.h>
87 87
88 88 #include "zoneadm.h"
89 89
90 90 #define MAXARGS 8
91 91 #define SOURCE_ZONE (CMD_MAX + 1)
92 92
93 93 /* Reflects kernel zone entries */
94 94 typedef struct zone_entry {
95 95 zoneid_t zid;
96 96 char zname[ZONENAME_MAX];
97 97 char *zstate_str;
98 98 zone_state_t zstate_num;
99 99 char zbrand[MAXNAMELEN];
100 100 char zroot[MAXPATHLEN];
101 101 char zuuid[UUID_PRINTABLE_STRING_LENGTH];
102 102 zone_iptype_t ziptype;
103 103 } zone_entry_t;
104 104
105 105 #define CLUSTER_BRAND_NAME "cluster"
106 106
107 107 static zone_entry_t *zents;
108 108 static size_t nzents;
109 109
110 110 #define LOOPBACK_IF "lo0"
111 111 #define SOCKET_AF(af) (((af) == AF_UNSPEC) ? AF_INET : (af))
112 112
113 113 struct net_if {
114 114 char *name;
115 115 int af;
116 116 };
117 117
118 118 /* 0755 is the default directory mode. */
119 119 #define DEFAULT_DIR_MODE \
120 120 (S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)
121 121
122 122 struct cmd {
123 123 uint_t cmd_num; /* command number */
124 124 char *cmd_name; /* command name */
125 125 char *short_usage; /* short form help */
126 126 int (*handler)(int argc, char *argv[]); /* function to call */
127 127
128 128 };
129 129
130 130 #define SHELP_HELP "help"
131 131 #define SHELP_BOOT "boot [-- boot_arguments]"
132 132 #define SHELP_HALT "halt"
133 133 #define SHELP_READY "ready"
134 134 #define SHELP_SHUTDOWN "shutdown [-r [-- boot_arguments]]"
135 135 #define SHELP_REBOOT "reboot [-- boot_arguments]"
136 136 #define SHELP_LIST "list [-cipv]"
137 137 #define SHELP_VERIFY "verify"
138 138 #define SHELP_INSTALL "install [brand-specific args]"
139 139 #define SHELP_UNINSTALL "uninstall [-F] [brand-specific args]"
140 140 #define SHELP_CLONE "clone [-m method] [-s <ZFS snapshot>] "\
141 141 "[brand-specific args] zonename"
142 142 #define SHELP_MOVE "move zonepath"
143 143 #define SHELP_DETACH "detach [-n] [brand-specific args]"
144 144 #define SHELP_ATTACH "attach [-F] [-n <path>] [brand-specific args]"
145 145 #define SHELP_MARK "mark incomplete"
146 146
147 147 #define EXEC_PREFIX "exec "
148 148 #define EXEC_LEN (strlen(EXEC_PREFIX))
149 149 #define RMCOMMAND "/usr/bin/rm -rf"
150 150
151 151 static int cleanup_zonepath(char *, boolean_t);
152 152
153 153
154 154 static int help_func(int argc, char *argv[]);
155 155 static int ready_func(int argc, char *argv[]);
156 156 static int boot_func(int argc, char *argv[]);
157 157 static int shutdown_func(int argc, char *argv[]);
158 158 static int halt_func(int argc, char *argv[]);
159 159 static int reboot_func(int argc, char *argv[]);
160 160 static int list_func(int argc, char *argv[]);
161 161 static int verify_func(int argc, char *argv[]);
162 162 static int install_func(int argc, char *argv[]);
163 163 static int uninstall_func(int argc, char *argv[]);
164 164 static int mount_func(int argc, char *argv[]);
165 165 static int unmount_func(int argc, char *argv[]);
166 166 static int clone_func(int argc, char *argv[]);
167 167 static int move_func(int argc, char *argv[]);
168 168 static int detach_func(int argc, char *argv[]);
169 169 static int attach_func(int argc, char *argv[]);
170 170 static int mark_func(int argc, char *argv[]);
171 171 static int apply_func(int argc, char *argv[]);
172 172 static int sysboot_func(int argc, char *argv[]);
173 173 static int sanity_check(char *zone, int cmd_num, boolean_t running,
174 174 boolean_t unsafe_when_running, boolean_t force);
175 175 static int cmd_match(char *cmd);
176 176 static int verify_details(int, char *argv[]);
177 177 static int verify_brand(zone_dochandle_t, int, char *argv[]);
178 178 static int invoke_brand_handler(int, char *argv[]);
179 179
180 180 static struct cmd cmdtab[] = {
181 181 { CMD_HELP, "help", SHELP_HELP, help_func },
182 182 { CMD_BOOT, "boot", SHELP_BOOT, boot_func },
183 183 { CMD_HALT, "halt", SHELP_HALT, halt_func },
184 184 { CMD_READY, "ready", SHELP_READY, ready_func },
185 185 { CMD_SHUTDOWN, "shutdown", SHELP_SHUTDOWN, shutdown_func },
186 186 { CMD_REBOOT, "reboot", SHELP_REBOOT, reboot_func },
187 187 { CMD_LIST, "list", SHELP_LIST, list_func },
188 188 { CMD_VERIFY, "verify", SHELP_VERIFY, verify_func },
189 189 { CMD_INSTALL, "install", SHELP_INSTALL, install_func },
190 190 { CMD_UNINSTALL, "uninstall", SHELP_UNINSTALL,
191 191 uninstall_func },
192 192 /* mount and unmount are private commands for admin/install */
193 193 { CMD_MOUNT, "mount", NULL, mount_func },
194 194 { CMD_UNMOUNT, "unmount", NULL, unmount_func },
195 195 { CMD_CLONE, "clone", SHELP_CLONE, clone_func },
196 196 { CMD_MOVE, "move", SHELP_MOVE, move_func },
197 197 { CMD_DETACH, "detach", SHELP_DETACH, detach_func },
198 198 { CMD_ATTACH, "attach", SHELP_ATTACH, attach_func },
199 199 { CMD_MARK, "mark", SHELP_MARK, mark_func },
200 200 { CMD_APPLY, "apply", NULL, apply_func },
201 201 { CMD_SYSBOOT, "sysboot", NULL, sysboot_func }
202 202 };
203 203
204 204 /* global variables */
205 205
206 206 /* set early in main(), never modified thereafter, used all over the place */
207 207 static char *execname;
208 208 static char target_brand[MAXNAMELEN];
209 209 static char default_brand[MAXPATHLEN];
210 210 static char *locale;
211 211 char *target_zone;
212 212 static char *target_uuid;
213 213 char *username;
214 214
215 215 char *
216 216 cmd_to_str(int cmd_num)
217 217 {
218 218 assert(cmd_num >= CMD_MIN && cmd_num <= CMD_MAX);
219 219 return (cmdtab[cmd_num].cmd_name);
220 220 }
221 221
222 222 /* This is a separate function because of gettext() wrapping. */
223 223 static char *
224 224 long_help(int cmd_num)
225 225 {
226 226 assert(cmd_num >= CMD_MIN && cmd_num <= CMD_MAX);
227 227 switch (cmd_num) {
228 228 case CMD_HELP:
229 229 return (gettext("Print usage message."));
230 230 case CMD_BOOT:
231 231 return (gettext("Activates (boots) specified zone. See "
232 232 "zoneadm(1m) for valid boot\n\targuments."));
233 233 case CMD_HALT:
234 234 return (gettext("Halts specified zone, bypassing shutdown "
235 235 "scripts and removing runtime\n\tresources of the zone."));
236 236 case CMD_READY:
237 237 return (gettext("Prepares a zone for running applications but "
238 238 "does not start any user\n\tprocesses in the zone."));
239 239 case CMD_SHUTDOWN:
240 240 return (gettext("Gracefully shutdown the zone or reboot if "
241 241 "the '-r' option is specified.\n\t"
242 242 "See zoneadm(1m) for valid boot arguments."));
243 243 case CMD_REBOOT:
244 244 return (gettext("Restarts the zone (equivalent to a halt / "
245 245 "boot sequence).\n\tFails if the zone is not active. "
246 246 "See zoneadm(1m) for valid boot\n\targuments."));
247 247 case CMD_LIST:
248 248 return (gettext("Lists the current zones, or a "
249 249 "specific zone if indicated. By default,\n\tall "
250 250 "running zones are listed, though this can be "
251 251 "expanded to all\n\tinstalled zones with the -i "
252 252 "option or all configured zones with the\n\t-c "
253 253 "option. When used with the general -z <zone> and/or -u "
254 254 "<uuid-match>\n\toptions, lists only the specified "
255 255 "matching zone, but lists it\n\tregardless of its state, "
256 256 "and the -i and -c options are disallowed. The\n\t-v "
257 257 "option can be used to display verbose information: zone "
258 258 "name, id,\n\tcurrent state, root directory and options. "
259 259 "The -p option can be used\n\tto request machine-parsable "
260 260 "output. The -v and -p options are mutually\n\texclusive."
261 261 " If neither -v nor -p is used, just the zone name is "
262 262 "listed."));
263 263 case CMD_VERIFY:
264 264 return (gettext("Check to make sure the configuration "
265 265 "can safely be instantiated\n\ton the machine: "
266 266 "physical network interfaces exist, etc."));
267 267 case CMD_INSTALL:
268 268 return (gettext("Install the configuration on to the system. "
269 269 "All arguments are passed to the brand installation "
270 270 "function;\n\tsee brands(5) for more information."));
271 271 case CMD_UNINSTALL:
272 272 return (gettext("Uninstall the configuration from the system. "
273 273 "The -F flag can be used\n\tto force the action. All "
274 274 "other arguments are passed to the brand\n\tuninstall "
275 275 "function; see brands(5) for more information."));
276 276 case CMD_CLONE:
277 277 return (gettext("Clone the installation of another zone. "
278 278 "The -m option can be used to\n\tspecify 'copy' which "
279 279 "forces a copy of the source zone. The -s option\n\t"
280 280 "can be used to specify the name of a ZFS snapshot "
281 281 "that was taken from\n\ta previous clone command. The "
282 282 "snapshot will be used as the source\n\tinstead of "
283 283 "creating a new ZFS snapshot. All other arguments are "
284 284 "passed\n\tto the brand clone function; see "
285 285 "brands(5) for more information."));
286 286 case CMD_MOVE:
287 287 return (gettext("Move the zone to a new zonepath."));
288 288 case CMD_DETACH:
289 289 return (gettext("Detach the zone from the system. The zone "
290 290 "state is changed to\n\t'configured' (but the files under "
291 291 "the zonepath are untouched).\n\tThe zone can subsequently "
292 292 "be attached, or can be moved to another\n\tsystem and "
293 293 "attached there. The -n option can be used to specify\n\t"
294 294 "'no-execute' mode. When -n is used, the information "
295 295 "needed to attach\n\tthe zone is sent to standard output "
296 296 "but the zone is not actually\n\tdetached. All other "
297 297 "arguments are passed to the brand detach function;\n\tsee "
298 298 "brands(5) for more information."));
299 299 case CMD_ATTACH:
300 300 return (gettext("Attach the zone to the system. The zone "
301 301 "state must be 'configured'\n\tprior to attach; upon "
302 302 "successful completion, the zone state will be\n\t"
303 303 "'installed'. The system software on the current "
304 304 "system must be\n\tcompatible with the software on the "
305 305 "zone's original system.\n\tSpecify -F "
306 306 "to force the attach and skip software compatibility "
307 307 "tests.\n\tThe -n option can be used to specify "
308 308 "'no-execute' mode. When -n is\n\tused, the information "
309 309 "needed to attach the zone is read from the\n\tspecified "
310 310 "path and the configuration is only validated. The path "
311 311 "can\n\tbe '-' to specify standard input. The -F and -n "
312 312 "options are mutually\n\texclusive. All other arguments "
313 313 "are passed to the brand attach\n\tfunction; see "
314 314 "brands(5) for more information."));
315 315 case CMD_MARK:
316 316 return (gettext("Set the state of the zone. This can be used "
317 317 "to force the zone\n\tstate to 'incomplete' "
318 318 "administratively if some activity has rendered\n\tthe "
319 319 "zone permanently unusable. The only valid state that "
320 320 "may be\n\tspecified is 'incomplete'."));
321 321 default:
322 322 return ("");
323 323 }
324 324 /* NOTREACHED */
325 325 return (NULL);
326 326 }
327 327
328 328 /*
329 329 * Called with explicit B_TRUE when help is explicitly requested, B_FALSE for
330 330 * unexpected errors.
331 331 */
332 332
333 333 static int
334 334 usage(boolean_t explicit)
335 335 {
336 336 int i;
337 337 FILE *fd = explicit ? stdout : stderr;
338 338
339 339 (void) fprintf(fd, "%s:\t%s help\n", gettext("usage"), execname);
340 340 (void) fprintf(fd, "\t%s [-z <zone>] [-u <uuid-match>] list\n",
341 341 execname);
342 342 (void) fprintf(fd, "\t%s {-z <zone>|-u <uuid-match>} <%s>\n", execname,
343 343 gettext("subcommand"));
344 344 (void) fprintf(fd, "\n%s:\n\n", gettext("Subcommands"));
345 345 for (i = CMD_MIN; i <= CMD_MAX; i++) {
346 346 if (cmdtab[i].short_usage == NULL)
347 347 continue;
348 348 (void) fprintf(fd, "%s\n", cmdtab[i].short_usage);
349 349 if (explicit)
350 350 (void) fprintf(fd, "\t%s\n\n", long_help(i));
351 351 }
352 352 if (!explicit)
353 353 (void) fputs("\n", fd);
354 354 return (Z_USAGE);
355 355 }
356 356
357 357 static void
358 358 sub_usage(char *short_usage, int cmd_num)
359 359 {
360 360 (void) fprintf(stderr, "%s:\t%s\n", gettext("usage"), short_usage);
361 361 (void) fprintf(stderr, "\t%s\n", long_help(cmd_num));
362 362 }
363 363
364 364 /*
365 365 * zperror() is like perror(3c) except that this also prints the executable
366 366 * name at the start of the message, and takes a boolean indicating whether
367 367 * to call libc'c strerror() or that from libzonecfg.
368 368 */
369 369
370 370 void
371 371 zperror(const char *str, boolean_t zonecfg_error)
372 372 {
373 373 (void) fprintf(stderr, "%s: %s: %s\n", execname, str,
374 374 zonecfg_error ? zonecfg_strerror(errno) : strerror(errno));
375 375 }
376 376
377 377 /*
378 378 * zperror2() is very similar to zperror() above, except it also prints a
379 379 * supplied zone name after the executable.
380 380 *
381 381 * All current consumers of this function want libzonecfg's strerror() rather
382 382 * than libc's; if this ever changes, this function can be made more generic
383 383 * like zperror() above.
384 384 */
385 385
386 386 void
387 387 zperror2(const char *zone, const char *str)
388 388 {
389 389 (void) fprintf(stderr, "%s: %s: %s: %s\n", execname, zone, str,
390 390 zonecfg_strerror(errno));
391 391 }
392 392
393 393 /* PRINTFLIKE1 */
394 394 void
395 395 zerror(const char *fmt, ...)
396 396 {
397 397 va_list alist;
398 398
399 399 va_start(alist, fmt);
400 400 (void) fprintf(stderr, "%s: ", execname);
401 401 if (target_zone != NULL)
402 402 (void) fprintf(stderr, "zone '%s': ", target_zone);
403 403 (void) vfprintf(stderr, fmt, alist);
404 404 (void) fprintf(stderr, "\n");
405 405 va_end(alist);
406 406 }
407 407
408 408 static void *
409 409 safe_calloc(size_t nelem, size_t elsize)
410 410 {
411 411 void *r = calloc(nelem, elsize);
412 412
413 413 if (r == NULL) {
414 414 zerror(gettext("failed to allocate %lu bytes: %s"),
415 415 (ulong_t)nelem * elsize, strerror(errno));
416 416 exit(Z_ERR);
417 417 }
418 418 return (r);
419 419 }
420 420
421 421 static void
422 422 zone_print(zone_entry_t *zent, boolean_t verbose, boolean_t parsable)
423 423 {
424 424 static boolean_t firsttime = B_TRUE;
425 425 char *ip_type_str;
426 426
427 427 /* Skip a zone that shutdown while we were collecting data. */
428 428 if (zent->zname[0] == '\0')
429 429 return;
430 430
431 431 if (zent->ziptype == ZS_EXCLUSIVE)
432 432 ip_type_str = "excl";
433 433 else
434 434 ip_type_str = "shared";
435 435
436 436 assert(!(verbose && parsable));
437 437 if (firsttime && verbose) {
438 438 firsttime = B_FALSE;
439 439 (void) printf("%*s %-16s %-10s %-30s %-8s %-6s\n",
440 440 ZONEID_WIDTH, "ID", "NAME", "STATUS", "PATH", "BRAND",
441 441 "IP");
442 442 }
443 443 if (!verbose) {
444 444 char *cp, *clim;
445 445
446 446 if (!parsable) {
447 447 (void) printf("%s\n", zent->zname);
448 448 return;
449 449 }
450 450 if (zent->zid == ZONE_ID_UNDEFINED)
451 451 (void) printf("-");
452 452 else
453 453 (void) printf("%lu", zent->zid);
454 454 (void) printf(":%s:%s:", zent->zname, zent->zstate_str);
455 455 cp = zent->zroot;
456 456 while ((clim = strchr(cp, ':')) != NULL) {
457 457 (void) printf("%.*s\\:", clim - cp, cp);
458 458 cp = clim + 1;
459 459 }
460 460 (void) printf("%s:%s:%s:%s\n", cp, zent->zuuid, zent->zbrand,
461 461 ip_type_str);
462 462 return;
463 463 }
464 464 if (zent->zstate_str != NULL) {
465 465 if (zent->zid == ZONE_ID_UNDEFINED)
466 466 (void) printf("%*s", ZONEID_WIDTH, "-");
467 467 else
468 468 (void) printf("%*lu", ZONEID_WIDTH, zent->zid);
469 469 (void) printf(" %-16s %-10s %-30s %-8s %-6s\n", zent->zname,
470 470 zent->zstate_str, zent->zroot, zent->zbrand, ip_type_str);
471 471 }
472 472 }
473 473
474 474 static int
475 475 lookup_zone_info(const char *zone_name, zoneid_t zid, zone_entry_t *zent)
476 476 {
477 477 char root[MAXPATHLEN], *cp;
478 478 int err;
479 479 uuid_t uuid;
480 480 zone_dochandle_t handle;
481 481
482 482 (void) strlcpy(zent->zname, zone_name, sizeof (zent->zname));
483 483 (void) strlcpy(zent->zroot, "???", sizeof (zent->zroot));
484 484 (void) strlcpy(zent->zbrand, "???", sizeof (zent->zbrand));
485 485 zent->zstate_str = "???";
486 486
487 487 zent->zid = zid;
488 488
489 489 if (zonecfg_get_uuid(zone_name, uuid) == Z_OK &&
490 490 !uuid_is_null(uuid))
491 491 uuid_unparse(uuid, zent->zuuid);
492 492 else
493 493 zent->zuuid[0] = '\0';
494 494
495 495 /*
496 496 * For labeled zones which query the zone path of lower-level
497 497 * zones, the path needs to be adjusted to drop the final
498 498 * "/root" component. This adjusted path is then useful
499 499 * for reading down any exported directories from the
500 500 * lower-level zone.
501 501 */
502 502 if (is_system_labeled() && zent->zid != ZONE_ID_UNDEFINED) {
503 503 if (zone_getattr(zent->zid, ZONE_ATTR_ROOT, zent->zroot,
504 504 sizeof (zent->zroot)) == -1) {
505 505 zperror2(zent->zname,
506 506 gettext("could not get zone path."));
507 507 return (Z_ERR);
508 508 }
509 509 cp = zent->zroot + strlen(zent->zroot) - 5;
510 510 if (cp > zent->zroot && strcmp(cp, "/root") == 0)
511 511 *cp = 0;
512 512 } else {
513 513 if ((err = zone_get_zonepath(zent->zname, root,
514 514 sizeof (root))) != Z_OK) {
515 515 errno = err;
516 516 zperror2(zent->zname,
517 517 gettext("could not get zone path."));
518 518 return (Z_ERR);
519 519 }
520 520 (void) strlcpy(zent->zroot, root, sizeof (zent->zroot));
521 521 }
522 522
523 523 if ((err = zone_get_state(zent->zname, &zent->zstate_num)) != Z_OK) {
524 524 errno = err;
525 525 zperror2(zent->zname, gettext("could not get state"));
526 526 return (Z_ERR);
527 527 }
528 528 zent->zstate_str = zone_state_str(zent->zstate_num);
529 529
530 530 /*
531 531 * A zone's brand is only available in the .xml file describing it,
532 532 * which is only visible to the global zone. This causes
533 533 * zone_get_brand() to fail when called from within a non-global
534 534 * zone. Fortunately we only do this on labeled systems, where we
535 535 * know all zones are native.
536 536 */
537 537 if (getzoneid() != GLOBAL_ZONEID) {
538 538 assert(is_system_labeled() != 0);
539 539 (void) strlcpy(zent->zbrand, default_brand,
540 540 sizeof (zent->zbrand));
541 541 } else if (zone_get_brand(zent->zname, zent->zbrand,
542 542 sizeof (zent->zbrand)) != Z_OK) {
543 543 zperror2(zent->zname, gettext("could not get brand name"));
544 544 return (Z_ERR);
545 545 }
546 546
547 547 /*
548 548 * Get ip type of the zone.
549 549 * Note for global zone, ZS_SHARED is set always.
550 550 */
551 551 if (zid == GLOBAL_ZONEID) {
552 552 zent->ziptype = ZS_SHARED;
553 553 return (Z_OK);
554 554 }
555 555
556 556 /*
557 557 * There is a race condition where the zone could boot while
558 558 * we're walking the index file. In this case the zone state
559 559 * could be seen as running from the call above, but the zoneid
560 560 * would be undefined.
561 561 *
562 562 * There is also a race condition where the zone could shutdown after
563 563 * we got its running state above. This is also not an error and
564 564 * we fall back to getting the ziptype from the zone configuration.
565 565 */
566 566 if (zent->zstate_num == ZONE_STATE_RUNNING &&
567 567 zid != ZONE_ID_UNDEFINED) {
568 568 ushort_t flags;
569 569
570 570 if (zone_getattr(zid, ZONE_ATTR_FLAGS, &flags,
571 571 sizeof (flags)) >= 0) {
572 572 if (flags & ZF_NET_EXCL)
573 573 zent->ziptype = ZS_EXCLUSIVE;
574 574 else
575 575 zent->ziptype = ZS_SHARED;
576 576 return (Z_OK);
577 577 }
578 578 }
579 579
580 580 if ((handle = zonecfg_init_handle()) == NULL) {
581 581 zperror2(zent->zname, gettext("could not init handle"));
582 582 return (Z_ERR);
583 583 }
584 584 if ((err = zonecfg_get_handle(zent->zname, handle)) != Z_OK) {
585 585 zperror2(zent->zname, gettext("could not get handle"));
586 586 zonecfg_fini_handle(handle);
587 587 return (Z_ERR);
588 588 }
589 589
590 590 if ((err = zonecfg_get_iptype(handle, &zent->ziptype)) != Z_OK) {
591 591 zperror2(zent->zname, gettext("could not get ip-type"));
592 592 zonecfg_fini_handle(handle);
593 593 return (Z_ERR);
594 594 }
595 595 zonecfg_fini_handle(handle);
596 596
597 597 return (Z_OK);
598 598 }
599 599
600 600 /*
601 601 * fetch_zents() calls zone_list(2) to find out how many zones are running
602 602 * (which is stored in the global nzents), then calls zone_list(2) again
603 603 * to fetch the list of running zones (stored in the global zents). This
604 604 * function may be called multiple times, so if zents is already set, we
605 605 * return immediately to save work.
606 606 *
607 607 * Note that the data about running zones can change while this function
608 608 * is running, so its possible that the list of zones will have empty slots
609 609 * at the end.
610 610 */
611 611
612 612 static int
613 613 fetch_zents(void)
614 614 {
615 615 zoneid_t *zids = NULL;
616 616 uint_t nzents_saved;
617 617 int i, retv;
618 618 FILE *fp;
619 619 boolean_t inaltroot;
620 620 zone_entry_t *zentp;
621 621 const char *altroot;
622 622
623 623 if (nzents > 0)
624 624 return (Z_OK);
625 625
626 626 if (zone_list(NULL, &nzents) != 0) {
627 627 zperror(gettext("failed to get zoneid list"), B_FALSE);
628 628 return (Z_ERR);
629 629 }
630 630
631 631 again:
632 632 if (nzents == 0)
633 633 return (Z_OK);
634 634
635 635 zids = safe_calloc(nzents, sizeof (zoneid_t));
636 636 nzents_saved = nzents;
637 637
638 638 if (zone_list(zids, &nzents) != 0) {
639 639 zperror(gettext("failed to get zone list"), B_FALSE);
640 640 free(zids);
641 641 return (Z_ERR);
642 642 }
643 643 if (nzents != nzents_saved) {
644 644 /* list changed, try again */
645 645 free(zids);
646 646 goto again;
647 647 }
648 648
649 649 zents = safe_calloc(nzents, sizeof (zone_entry_t));
650 650
651 651 inaltroot = zonecfg_in_alt_root();
652 652 if (inaltroot) {
653 653 fp = zonecfg_open_scratch("", B_FALSE);
654 654 altroot = zonecfg_get_root();
655 655 } else {
656 656 fp = NULL;
657 657 }
658 658 zentp = zents;
659 659 retv = Z_OK;
660 660 for (i = 0; i < nzents; i++) {
661 661 char name[ZONENAME_MAX];
662 662 char altname[ZONENAME_MAX];
663 663 char rev_altroot[MAXPATHLEN];
664 664
665 665 if (getzonenamebyid(zids[i], name, sizeof (name)) < 0) {
666 666 /*
667 667 * There is a race condition where the zone may have
668 668 * shutdown since we retrieved the number of running
669 669 * zones above. This is not an error, there will be
670 670 * an empty slot at the end of the list.
671 671 */
672 672 continue;
673 673 }
674 674 if (zonecfg_is_scratch(name)) {
675 675 /* Ignore scratch zones by default */
676 676 if (!inaltroot)
677 677 continue;
678 678 if (fp == NULL ||
679 679 zonecfg_reverse_scratch(fp, name, altname,
680 680 sizeof (altname), rev_altroot,
681 681 sizeof (rev_altroot)) == -1) {
682 682 zerror(gettext("could not resolve scratch "
683 683 "zone %s"), name);
684 684 retv = Z_ERR;
685 685 continue;
686 686 }
687 687 /* Ignore zones in other alternate roots */
688 688 if (strcmp(rev_altroot, altroot) != 0)
689 689 continue;
690 690 (void) strcpy(name, altname);
691 691 } else {
692 692 /* Ignore non-scratch when in an alternate root */
693 693 if (inaltroot && strcmp(name, GLOBAL_ZONENAME) != 0)
694 694 continue;
695 695 }
696 696 if (lookup_zone_info(name, zids[i], zentp) != Z_OK) {
697 697 /*
698 698 * There is a race condition where the zone may have
699 699 * shutdown since we retrieved the number of running
700 700 * zones above. This is not an error, there will be
701 701 * an empty slot at the end of the list.
702 702 */
703 703 continue;
704 704 }
705 705 zentp++;
706 706 }
707 707 nzents = zentp - zents;
708 708 if (fp != NULL)
709 709 zonecfg_close_scratch(fp);
710 710
711 711 free(zids);
712 712 return (retv);
713 713 }
714 714
715 715 static int
716 716 zone_print_list(zone_state_t min_state, boolean_t verbose, boolean_t parsable)
717 717 {
718 718 int i;
719 719 zone_entry_t zent;
720 720 FILE *cookie;
721 721 char *name;
722 722
723 723 /*
724 724 * First get the list of running zones from the kernel and print them.
725 725 * If that is all we need, then return.
726 726 */
727 727 if ((i = fetch_zents()) != Z_OK) {
728 728 /*
729 729 * No need for error messages; fetch_zents() has already taken
730 730 * care of this.
731 731 */
732 732 return (i);
733 733 }
734 734 for (i = 0; i < nzents; i++)
735 735 zone_print(&zents[i], verbose, parsable);
736 736 if (min_state >= ZONE_STATE_RUNNING)
737 737 return (Z_OK);
738 738 /*
739 739 * Next, get the full list of zones from the configuration, skipping
740 740 * any we have already printed.
741 741 */
742 742 cookie = setzoneent();
743 743 while ((name = getzoneent(cookie)) != NULL) {
744 744 for (i = 0; i < nzents; i++) {
745 745 if (strcmp(zents[i].zname, name) == 0)
746 746 break;
747 747 }
748 748 if (i < nzents) {
749 749 free(name);
750 750 continue;
751 751 }
752 752 if (lookup_zone_info(name, ZONE_ID_UNDEFINED, &zent) != Z_OK) {
753 753 free(name);
754 754 continue;
755 755 }
756 756 free(name);
757 757 if (zent.zstate_num >= min_state)
758 758 zone_print(&zent, verbose, parsable);
759 759 }
760 760 endzoneent(cookie);
761 761 return (Z_OK);
762 762 }
763 763
764 764 /*
765 765 * Retrieve a zone entry by name. Returns NULL if no such zone exists.
766 766 */
767 767 static zone_entry_t *
768 768 lookup_running_zone(const char *str)
769 769 {
770 770 int i;
771 771
772 772 if (fetch_zents() != Z_OK)
773 773 return (NULL);
774 774
775 775 for (i = 0; i < nzents; i++) {
776 776 if (strcmp(str, zents[i].zname) == 0)
777 777 return (&zents[i]);
778 778 }
779 779 return (NULL);
780 780 }
781 781
782 782 /*
783 783 * Check a bit in a mode_t: if on is B_TRUE, that bit should be on; if
784 784 * B_FALSE, it should be off. Return B_TRUE if the mode is bad (incorrect).
785 785 */
786 786 static boolean_t
787 787 bad_mode_bit(mode_t mode, mode_t bit, boolean_t on, char *file)
788 788 {
789 789 char *str;
790 790
791 791 assert(bit == S_IRUSR || bit == S_IWUSR || bit == S_IXUSR ||
792 792 bit == S_IRGRP || bit == S_IWGRP || bit == S_IXGRP ||
793 793 bit == S_IROTH || bit == S_IWOTH || bit == S_IXOTH);
794 794 /*
795 795 * TRANSLATION_NOTE
796 796 * The strings below will be used as part of a larger message,
797 797 * either:
798 798 * (file name) must be (owner|group|world) (read|writ|execut)able
799 799 * or
800 800 * (file name) must not be (owner|group|world) (read|writ|execut)able
801 801 */
802 802 switch (bit) {
803 803 case S_IRUSR:
804 804 str = gettext("owner readable");
805 805 break;
806 806 case S_IWUSR:
807 807 str = gettext("owner writable");
808 808 break;
809 809 case S_IXUSR:
810 810 str = gettext("owner executable");
811 811 break;
812 812 case S_IRGRP:
813 813 str = gettext("group readable");
814 814 break;
815 815 case S_IWGRP:
816 816 str = gettext("group writable");
817 817 break;
818 818 case S_IXGRP:
819 819 str = gettext("group executable");
820 820 break;
821 821 case S_IROTH:
822 822 str = gettext("world readable");
823 823 break;
824 824 case S_IWOTH:
825 825 str = gettext("world writable");
826 826 break;
827 827 case S_IXOTH:
828 828 str = gettext("world executable");
829 829 break;
830 830 }
831 831 if ((mode & bit) == (on ? 0 : bit)) {
832 832 /*
833 833 * TRANSLATION_NOTE
834 834 * The first parameter below is a file name; the second
835 835 * is one of the "(owner|group|world) (read|writ|execut)able"
836 836 * strings from above.
837 837 */
838 838 /*
839 839 * The code below could be simplified but not in a way
840 840 * that would easily translate to non-English locales.
841 841 */
842 842 if (on) {
843 843 (void) fprintf(stderr, gettext("%s must be %s.\n"),
844 844 file, str);
845 845 } else {
846 846 (void) fprintf(stderr, gettext("%s must not be %s.\n"),
847 847 file, str);
848 848 }
849 849 return (B_TRUE);
850 850 }
851 851 return (B_FALSE);
852 852 }
853 853
854 854 /*
855 855 * We want to make sure that no zone has its zone path as a child node
856 856 * (in the directory sense) of any other. We do that by comparing this
857 857 * zone's path to the path of all other (non-global) zones. The comparison
858 858 * in each case is simple: add '/' to the end of the path, then do a
859 859 * strncmp() of the two paths, using the length of the shorter one.
860 860 */
861 861
862 862 static int
863 863 crosscheck_zonepaths(char *path)
864 864 {
865 865 char rpath[MAXPATHLEN]; /* resolved path */
866 866 char path_copy[MAXPATHLEN]; /* copy of original path */
867 867 char rpath_copy[MAXPATHLEN]; /* copy of original rpath */
868 868 struct zoneent *ze;
869 869 int res, err;
870 870 FILE *cookie;
871 871
872 872 cookie = setzoneent();
873 873 while ((ze = getzoneent_private(cookie)) != NULL) {
874 874 /* Skip zones which are not installed. */
875 875 if (ze->zone_state < ZONE_STATE_INSTALLED) {
876 876 free(ze);
877 877 continue;
878 878 }
879 879 /* Skip the global zone and the current target zone. */
880 880 if (strcmp(ze->zone_name, GLOBAL_ZONENAME) == 0 ||
881 881 strcmp(ze->zone_name, target_zone) == 0) {
882 882 free(ze);
883 883 continue;
884 884 }
885 885 if (strlen(ze->zone_path) == 0) {
886 886 /* old index file without path, fall back */
887 887 if ((err = zone_get_zonepath(ze->zone_name,
888 888 ze->zone_path, sizeof (ze->zone_path))) != Z_OK) {
889 889 errno = err;
890 890 zperror2(ze->zone_name,
891 891 gettext("could not get zone path"));
892 892 free(ze);
893 893 continue;
894 894 }
895 895 }
896 896 (void) snprintf(path_copy, sizeof (path_copy), "%s%s",
897 897 zonecfg_get_root(), ze->zone_path);
898 898 res = resolvepath(path_copy, rpath, sizeof (rpath));
899 899 if (res == -1) {
900 900 if (errno != ENOENT) {
901 901 zperror(path_copy, B_FALSE);
902 902 free(ze);
903 903 return (Z_ERR);
904 904 }
905 905 (void) printf(gettext("WARNING: zone %s is installed, "
906 906 "but its %s %s does not exist.\n"), ze->zone_name,
907 907 "zonepath", path_copy);
908 908 free(ze);
909 909 continue;
910 910 }
911 911 rpath[res] = '\0';
912 912 (void) snprintf(path_copy, sizeof (path_copy), "%s/", path);
913 913 (void) snprintf(rpath_copy, sizeof (rpath_copy), "%s/", rpath);
914 914 if (strncmp(path_copy, rpath_copy,
915 915 min(strlen(path_copy), strlen(rpath_copy))) == 0) {
916 916 /*
917 917 * TRANSLATION_NOTE
918 918 * zonepath is a literal that should not be translated.
919 919 */
920 920 (void) fprintf(stderr, gettext("%s zonepath (%s) and "
921 921 "%s zonepath (%s) overlap.\n"),
922 922 target_zone, path, ze->zone_name, rpath);
923 923 free(ze);
924 924 return (Z_ERR);
925 925 }
926 926 free(ze);
927 927 }
928 928 endzoneent(cookie);
929 929 return (Z_OK);
930 930 }
931 931
932 932 static int
933 933 validate_zonepath(char *path, int cmd_num)
934 934 {
935 935 int res; /* result of last library/system call */
936 936 boolean_t err = B_FALSE; /* have we run into an error? */
937 937 struct stat stbuf;
938 938 struct statvfs64 vfsbuf;
939 939 char rpath[MAXPATHLEN]; /* resolved path */
940 940 char ppath[MAXPATHLEN]; /* parent path */
941 941 char rppath[MAXPATHLEN]; /* resolved parent path */
942 942 char rootpath[MAXPATHLEN]; /* root path */
943 943 zone_state_t state;
944 944
945 945 if (path[0] != '/') {
946 946 (void) fprintf(stderr,
947 947 gettext("%s is not an absolute path.\n"), path);
948 948 return (Z_ERR);
949 949 }
950 950 if ((res = resolvepath(path, rpath, sizeof (rpath))) == -1) {
951 951 if ((errno != ENOENT) ||
952 952 (cmd_num != CMD_VERIFY && cmd_num != CMD_INSTALL &&
953 953 cmd_num != CMD_CLONE && cmd_num != CMD_MOVE)) {
954 954 zperror(path, B_FALSE);
955 955 return (Z_ERR);
956 956 }
957 957 if (cmd_num == CMD_VERIFY) {
958 958 /*
959 959 * TRANSLATION_NOTE
960 960 * zoneadm is a literal that should not be translated.
961 961 */
962 962 (void) fprintf(stderr, gettext("WARNING: %s does not "
963 963 "exist, so it could not be verified.\nWhen "
964 964 "'zoneadm %s' is run, '%s' will try to create\n%s, "
965 965 "and '%s' will be tried again,\nbut the '%s' may "
966 966 "fail if:\nthe parent directory of %s is group- or "
967 967 "other-writable\nor\n%s overlaps with any other "
968 968 "installed zones.\n"), path,
969 969 cmd_to_str(CMD_INSTALL), cmd_to_str(CMD_INSTALL),
970 970 path, cmd_to_str(CMD_VERIFY),
971 971 cmd_to_str(CMD_VERIFY), path, path);
972 972 return (Z_OK);
973 973 }
974 974 /*
975 975 * The zonepath is supposed to be mode 700 but its
976 976 * parent(s) 755. So use 755 on the mkdirp() then
977 977 * chmod() the zonepath itself to 700.
978 978 */
979 979 if (mkdirp(path, DEFAULT_DIR_MODE) < 0) {
980 980 zperror(path, B_FALSE);
981 981 return (Z_ERR);
982 982 }
983 983 /*
984 984 * If the chmod() fails, report the error, but might
985 985 * as well continue the verify procedure.
986 986 */
987 987 if (chmod(path, S_IRWXU) != 0)
988 988 zperror(path, B_FALSE);
989 989 /*
990 990 * Since the mkdir() succeeded, we should not have to
991 991 * worry about a subsequent ENOENT, thus this should
992 992 * only recurse once.
993 993 */
994 994 return (validate_zonepath(path, cmd_num));
995 995 }
996 996 rpath[res] = '\0';
997 997 if (strcmp(path, rpath) != 0) {
998 998 errno = Z_RESOLVED_PATH;
999 999 zperror(path, B_TRUE);
1000 1000 return (Z_ERR);
1001 1001 }
1002 1002 if ((res = stat(rpath, &stbuf)) != 0) {
1003 1003 zperror(rpath, B_FALSE);
1004 1004 return (Z_ERR);
1005 1005 }
1006 1006 if (!S_ISDIR(stbuf.st_mode)) {
1007 1007 (void) fprintf(stderr, gettext("%s is not a directory.\n"),
1008 1008 rpath);
1009 1009 return (Z_ERR);
1010 1010 }
1011 1011 if (strcmp(stbuf.st_fstype, MNTTYPE_TMPFS) == 0) {
1012 1012 (void) printf(gettext("WARNING: %s is on a temporary "
1013 1013 "file system.\n"), rpath);
1014 1014 }
1015 1015 if (crosscheck_zonepaths(rpath) != Z_OK)
↓ open down ↓ |
1015 lines elided |
↑ open up ↑ |
1016 1016 return (Z_ERR);
1017 1017 /*
1018 1018 * Try to collect and report as many minor errors as possible
1019 1019 * before returning, so the user can learn everything that needs
1020 1020 * to be fixed up front.
1021 1021 */
1022 1022 if (stbuf.st_uid != 0) {
1023 1023 (void) fprintf(stderr, gettext("%s is not owned by root.\n"),
1024 1024 rpath);
1025 1025 err = B_TRUE;
1026 +
1027 + /* Try to change owner */
1028 + if (cmd_num != CMD_VERIFY) {
1029 + (void) fprintf(stderr, gettext("%s: changing owner "
1030 + "to root.\n"), rpath);
1031 + if (chown(rpath, 0, -1) != 0) {
1032 + zperror(rpath, B_FALSE);
1033 + return (Z_ERR);
1034 + } else {
1035 + err = B_FALSE;
1036 + }
1037 + }
1026 1038 }
1027 1039 err |= bad_mode_bit(stbuf.st_mode, S_IRUSR, B_TRUE, rpath);
1028 1040 err |= bad_mode_bit(stbuf.st_mode, S_IWUSR, B_TRUE, rpath);
1029 1041 err |= bad_mode_bit(stbuf.st_mode, S_IXUSR, B_TRUE, rpath);
1030 1042 err |= bad_mode_bit(stbuf.st_mode, S_IRGRP, B_FALSE, rpath);
1031 1043 err |= bad_mode_bit(stbuf.st_mode, S_IWGRP, B_FALSE, rpath);
1032 1044 err |= bad_mode_bit(stbuf.st_mode, S_IXGRP, B_FALSE, rpath);
1033 1045 err |= bad_mode_bit(stbuf.st_mode, S_IROTH, B_FALSE, rpath);
1034 1046 err |= bad_mode_bit(stbuf.st_mode, S_IWOTH, B_FALSE, rpath);
1035 1047 err |= bad_mode_bit(stbuf.st_mode, S_IXOTH, B_FALSE, rpath);
1036 1048
1049 + /* If the group perms are wrong, fix them */
1050 + if (err && (cmd_num != CMD_VERIFY)) {
1051 + (void) fprintf(stderr, gettext("%s: changing permissions "
1052 + "to 0700.\n"), rpath);
1053 + if (chmod(rpath, S_IRWXU) != 0) {
1054 + zperror(path, B_FALSE);
1055 + } else {
1056 + err = B_FALSE;
1057 + }
1058 + }
1059 +
1037 1060 (void) snprintf(ppath, sizeof (ppath), "%s/..", path);
1038 1061 if ((res = resolvepath(ppath, rppath, sizeof (rppath))) == -1) {
1039 1062 zperror(ppath, B_FALSE);
1040 1063 return (Z_ERR);
1041 1064 }
1042 1065 rppath[res] = '\0';
1043 1066 if ((res = stat(rppath, &stbuf)) != 0) {
1044 1067 zperror(rppath, B_FALSE);
1045 1068 return (Z_ERR);
1046 1069 }
1047 1070 /* theoretically impossible */
1048 1071 if (!S_ISDIR(stbuf.st_mode)) {
1049 1072 (void) fprintf(stderr, gettext("%s is not a directory.\n"),
1050 1073 rppath);
1051 1074 return (Z_ERR);
1052 1075 }
1053 1076 if (stbuf.st_uid != 0) {
1054 1077 (void) fprintf(stderr, gettext("%s is not owned by root.\n"),
1055 1078 rppath);
1056 1079 err = B_TRUE;
1057 1080 }
1058 1081 err |= bad_mode_bit(stbuf.st_mode, S_IRUSR, B_TRUE, rppath);
1059 1082 err |= bad_mode_bit(stbuf.st_mode, S_IWUSR, B_TRUE, rppath);
1060 1083 err |= bad_mode_bit(stbuf.st_mode, S_IXUSR, B_TRUE, rppath);
1061 1084 err |= bad_mode_bit(stbuf.st_mode, S_IWGRP, B_FALSE, rppath);
1062 1085 err |= bad_mode_bit(stbuf.st_mode, S_IWOTH, B_FALSE, rppath);
1063 1086 if (strcmp(rpath, rppath) == 0) {
1064 1087 (void) fprintf(stderr, gettext("%s is its own parent.\n"),
1065 1088 rppath);
1066 1089 err = B_TRUE;
1067 1090 }
1068 1091
1069 1092 if (statvfs64(rpath, &vfsbuf) != 0) {
1070 1093 zperror(rpath, B_FALSE);
1071 1094 return (Z_ERR);
1072 1095 }
1073 1096 if (strcmp(vfsbuf.f_basetype, MNTTYPE_NFS) == 0) {
1074 1097 /*
1075 1098 * TRANSLATION_NOTE
1076 1099 * Zonepath and NFS are literals that should not be translated.
1077 1100 */
1078 1101 (void) fprintf(stderr, gettext("Zonepath %s is on an NFS "
1079 1102 "mounted file system.\n"
1080 1103 "\tA local file system must be used.\n"), rpath);
1081 1104 return (Z_ERR);
1082 1105 }
1083 1106 if (vfsbuf.f_flag & ST_NOSUID) {
1084 1107 /*
1085 1108 * TRANSLATION_NOTE
1086 1109 * Zonepath and nosuid are literals that should not be
1087 1110 * translated.
1088 1111 */
1089 1112 (void) fprintf(stderr, gettext("Zonepath %s is on a nosuid "
1090 1113 "file system.\n"), rpath);
1091 1114 return (Z_ERR);
1092 1115 }
1093 1116
1094 1117 if ((res = zone_get_state(target_zone, &state)) != Z_OK) {
1095 1118 errno = res;
1096 1119 zperror2(target_zone, gettext("could not get state"));
1097 1120 return (Z_ERR);
1098 1121 }
1099 1122 /*
1100 1123 * The existence of the root path is only bad in the configured state,
1101 1124 * as it is *supposed* to be there at the installed and later states.
1102 1125 * However, the root path is expected to be there if the zone is
1103 1126 * detached.
1104 1127 * State/command mismatches are caught earlier in verify_details().
1105 1128 */
1106 1129 if (state == ZONE_STATE_CONFIGURED && cmd_num != CMD_ATTACH) {
1107 1130 if (snprintf(rootpath, sizeof (rootpath), "%s/root", rpath) >=
1108 1131 sizeof (rootpath)) {
1109 1132 /*
1110 1133 * TRANSLATION_NOTE
1111 1134 * Zonepath is a literal that should not be translated.
1112 1135 */
1113 1136 (void) fprintf(stderr,
1114 1137 gettext("Zonepath %s is too long.\n"), rpath);
1115 1138 return (Z_ERR);
1116 1139 }
1117 1140 if ((res = stat(rootpath, &stbuf)) == 0) {
1118 1141 if (zonecfg_detached(rpath)) {
1119 1142 (void) fprintf(stderr,
1120 1143 gettext("Cannot %s detached "
1121 1144 "zone.\nUse attach or remove %s "
1122 1145 "directory.\n"), cmd_to_str(cmd_num),
1123 1146 rpath);
1124 1147 return (Z_ERR);
1125 1148 }
1126 1149
1127 1150 /* Not detached, check if it really looks ok. */
1128 1151
1129 1152 if (!S_ISDIR(stbuf.st_mode)) {
1130 1153 (void) fprintf(stderr, gettext("%s is not a "
1131 1154 "directory.\n"), rootpath);
1132 1155 return (Z_ERR);
1133 1156 }
1134 1157
1135 1158 if (stbuf.st_uid != 0) {
1136 1159 (void) fprintf(stderr, gettext("%s is not "
1137 1160 "owned by root.\n"), rootpath);
1138 1161 return (Z_ERR);
1139 1162 }
1140 1163
1141 1164 if ((stbuf.st_mode & 0777) != 0755) {
1142 1165 (void) fprintf(stderr, gettext("%s mode is not "
1143 1166 "0755.\n"), rootpath);
1144 1167 return (Z_ERR);
1145 1168 }
1146 1169 }
1147 1170 }
1148 1171
1149 1172 return (err ? Z_ERR : Z_OK);
1150 1173 }
1151 1174
1152 1175 static int
1153 1176 invoke_brand_handler(int cmd_num, char *argv[])
1154 1177 {
1155 1178 zone_dochandle_t handle;
1156 1179 int err;
1157 1180
1158 1181 if ((handle = zonecfg_init_handle()) == NULL) {
1159 1182 zperror(cmd_to_str(cmd_num), B_TRUE);
1160 1183 return (Z_ERR);
1161 1184 }
1162 1185 if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) {
1163 1186 errno = err;
1164 1187 zperror(cmd_to_str(cmd_num), B_TRUE);
1165 1188 zonecfg_fini_handle(handle);
1166 1189 return (Z_ERR);
1167 1190 }
1168 1191 if (verify_brand(handle, cmd_num, argv) != Z_OK) {
1169 1192 zonecfg_fini_handle(handle);
1170 1193 return (Z_ERR);
1171 1194 }
1172 1195 zonecfg_fini_handle(handle);
1173 1196 return (Z_OK);
1174 1197 }
1175 1198
1176 1199 static int
1177 1200 ready_func(int argc, char *argv[])
1178 1201 {
1179 1202 zone_cmd_arg_t zarg;
1180 1203 int arg;
1181 1204
1182 1205 if (zonecfg_in_alt_root()) {
1183 1206 zerror(gettext("cannot ready zone in alternate root"));
1184 1207 return (Z_ERR);
1185 1208 }
1186 1209
1187 1210 optind = 0;
1188 1211 if ((arg = getopt(argc, argv, "?")) != EOF) {
1189 1212 switch (arg) {
1190 1213 case '?':
1191 1214 sub_usage(SHELP_READY, CMD_READY);
1192 1215 return (optopt == '?' ? Z_OK : Z_USAGE);
1193 1216 default:
1194 1217 sub_usage(SHELP_READY, CMD_READY);
1195 1218 return (Z_USAGE);
1196 1219 }
1197 1220 }
1198 1221 if (argc > optind) {
1199 1222 sub_usage(SHELP_READY, CMD_READY);
1200 1223 return (Z_USAGE);
1201 1224 }
1202 1225 if (sanity_check(target_zone, CMD_READY, B_FALSE, B_FALSE, B_FALSE)
1203 1226 != Z_OK)
1204 1227 return (Z_ERR);
1205 1228 if (verify_details(CMD_READY, argv) != Z_OK)
1206 1229 return (Z_ERR);
1207 1230
1208 1231 zarg.cmd = Z_READY;
1209 1232 if (zonecfg_call_zoneadmd(target_zone, &zarg, locale, B_TRUE) != 0) {
1210 1233 zerror(gettext("call to %s failed"), "zoneadmd");
1211 1234 return (Z_ERR);
1212 1235 }
1213 1236 return (Z_OK);
1214 1237 }
1215 1238
1216 1239 static int
1217 1240 boot_func(int argc, char *argv[])
1218 1241 {
1219 1242 zone_cmd_arg_t zarg;
1220 1243 boolean_t force = B_FALSE;
1221 1244 int arg;
1222 1245
1223 1246 if (zonecfg_in_alt_root()) {
1224 1247 zerror(gettext("cannot boot zone in alternate root"));
1225 1248 return (Z_ERR);
1226 1249 }
1227 1250
1228 1251 zarg.bootbuf[0] = '\0';
1229 1252
1230 1253 /*
1231 1254 * The following getopt processes arguments to zone boot; that
1232 1255 * is to say, the [here] portion of the argument string:
1233 1256 *
1234 1257 * zoneadm -z myzone boot [here] -- -v -m verbose
1235 1258 *
1236 1259 * Where [here] can either be nothing, -? (in which case we bail
1237 1260 * and print usage), -f (a private option to indicate that the
1238 1261 * boot operation should be 'forced'), or -s. Support for -s is
1239 1262 * vestigal and obsolete, but is retained because it was a
1240 1263 * documented interface and there are known consumers including
1241 1264 * admin/install; the proper way to specify boot arguments like -s
1242 1265 * is:
1243 1266 *
1244 1267 * zoneadm -z myzone boot -- -s -v -m verbose.
1245 1268 */
1246 1269 optind = 0;
1247 1270 while ((arg = getopt(argc, argv, "?fs")) != EOF) {
1248 1271 switch (arg) {
1249 1272 case '?':
1250 1273 sub_usage(SHELP_BOOT, CMD_BOOT);
1251 1274 return (optopt == '?' ? Z_OK : Z_USAGE);
1252 1275 case 's':
1253 1276 (void) strlcpy(zarg.bootbuf, "-s",
1254 1277 sizeof (zarg.bootbuf));
1255 1278 break;
1256 1279 case 'f':
1257 1280 force = B_TRUE;
1258 1281 break;
1259 1282 default:
1260 1283 sub_usage(SHELP_BOOT, CMD_BOOT);
1261 1284 return (Z_USAGE);
1262 1285 }
1263 1286 }
1264 1287
1265 1288 for (; optind < argc; optind++) {
1266 1289 if (strlcat(zarg.bootbuf, argv[optind],
1267 1290 sizeof (zarg.bootbuf)) >= sizeof (zarg.bootbuf)) {
1268 1291 zerror(gettext("Boot argument list too long"));
1269 1292 return (Z_ERR);
1270 1293 }
1271 1294 if (optind < argc - 1)
1272 1295 if (strlcat(zarg.bootbuf, " ", sizeof (zarg.bootbuf)) >=
1273 1296 sizeof (zarg.bootbuf)) {
1274 1297 zerror(gettext("Boot argument list too long"));
1275 1298 return (Z_ERR);
1276 1299 }
1277 1300 }
1278 1301 if (sanity_check(target_zone, CMD_BOOT, B_FALSE, B_FALSE, force)
1279 1302 != Z_OK)
1280 1303 return (Z_ERR);
1281 1304 if (verify_details(CMD_BOOT, argv) != Z_OK)
1282 1305 return (Z_ERR);
1283 1306 zarg.cmd = force ? Z_FORCEBOOT : Z_BOOT;
1284 1307 if (zonecfg_call_zoneadmd(target_zone, &zarg, locale, B_TRUE) != 0) {
1285 1308 zerror(gettext("call to %s failed"), "zoneadmd");
1286 1309 return (Z_ERR);
1287 1310 }
1288 1311
1289 1312 return (Z_OK);
1290 1313 }
1291 1314
1292 1315 static void
1293 1316 fake_up_local_zone(zoneid_t zid, zone_entry_t *zeptr)
1294 1317 {
1295 1318 ssize_t result;
1296 1319 uuid_t uuid;
1297 1320 FILE *fp;
1298 1321 ushort_t flags;
1299 1322
1300 1323 (void) memset(zeptr, 0, sizeof (*zeptr));
1301 1324
1302 1325 zeptr->zid = zid;
1303 1326
1304 1327 /*
1305 1328 * Since we're looking up our own (non-global) zone name,
1306 1329 * we can be assured that it will succeed.
1307 1330 */
1308 1331 result = getzonenamebyid(zid, zeptr->zname, sizeof (zeptr->zname));
1309 1332 assert(result >= 0);
1310 1333 if (zonecfg_is_scratch(zeptr->zname) &&
1311 1334 (fp = zonecfg_open_scratch("", B_FALSE)) != NULL) {
1312 1335 (void) zonecfg_reverse_scratch(fp, zeptr->zname, zeptr->zname,
1313 1336 sizeof (zeptr->zname), NULL, 0);
1314 1337 zonecfg_close_scratch(fp);
1315 1338 }
1316 1339
1317 1340 if (is_system_labeled()) {
1318 1341 (void) zone_getattr(zid, ZONE_ATTR_ROOT, zeptr->zroot,
1319 1342 sizeof (zeptr->zroot));
1320 1343 (void) strlcpy(zeptr->zbrand, NATIVE_BRAND_NAME,
1321 1344 sizeof (zeptr->zbrand));
1322 1345 } else {
1323 1346 (void) strlcpy(zeptr->zroot, "/", sizeof (zeptr->zroot));
1324 1347 (void) zone_getattr(zid, ZONE_ATTR_BRAND, zeptr->zbrand,
1325 1348 sizeof (zeptr->zbrand));
1326 1349 }
1327 1350
1328 1351 zeptr->zstate_str = "running";
1329 1352 if (zonecfg_get_uuid(zeptr->zname, uuid) == Z_OK &&
1330 1353 !uuid_is_null(uuid))
1331 1354 uuid_unparse(uuid, zeptr->zuuid);
1332 1355
1333 1356 if (zone_getattr(zid, ZONE_ATTR_FLAGS, &flags, sizeof (flags)) < 0) {
1334 1357 zperror2(zeptr->zname, gettext("could not get zone flags"));
1335 1358 exit(Z_ERR);
1336 1359 }
1337 1360 if (flags & ZF_NET_EXCL)
1338 1361 zeptr->ziptype = ZS_EXCLUSIVE;
1339 1362 else
1340 1363 zeptr->ziptype = ZS_SHARED;
1341 1364 }
1342 1365
1343 1366 static int
1344 1367 list_func(int argc, char *argv[])
1345 1368 {
1346 1369 zone_entry_t *zentp, zent;
1347 1370 int arg, retv;
1348 1371 boolean_t output = B_FALSE, verbose = B_FALSE, parsable = B_FALSE;
1349 1372 zone_state_t min_state = ZONE_STATE_RUNNING;
1350 1373 zoneid_t zone_id = getzoneid();
1351 1374
1352 1375 if (target_zone == NULL) {
1353 1376 /* all zones: default view to running but allow override */
1354 1377 optind = 0;
1355 1378 while ((arg = getopt(argc, argv, "?cipv")) != EOF) {
1356 1379 switch (arg) {
1357 1380 case '?':
1358 1381 sub_usage(SHELP_LIST, CMD_LIST);
1359 1382 return (optopt == '?' ? Z_OK : Z_USAGE);
1360 1383 /*
1361 1384 * The 'i' and 'c' options are not mutually
1362 1385 * exclusive so if 'c' is given, then min_state
1363 1386 * is set to 0 (ZONE_STATE_CONFIGURED) which is
1364 1387 * the lowest possible state. If 'i' is given,
1365 1388 * then min_state is set to be the lowest state
1366 1389 * so far.
1367 1390 */
1368 1391 case 'c':
1369 1392 min_state = ZONE_STATE_CONFIGURED;
1370 1393 break;
1371 1394 case 'i':
1372 1395 min_state = min(ZONE_STATE_INSTALLED,
1373 1396 min_state);
1374 1397
1375 1398 break;
1376 1399 case 'p':
1377 1400 parsable = B_TRUE;
1378 1401 break;
1379 1402 case 'v':
1380 1403 verbose = B_TRUE;
1381 1404 break;
1382 1405 default:
1383 1406 sub_usage(SHELP_LIST, CMD_LIST);
1384 1407 return (Z_USAGE);
1385 1408 }
1386 1409 }
1387 1410 if (parsable && verbose) {
1388 1411 zerror(gettext("%s -p and -v are mutually exclusive."),
1389 1412 cmd_to_str(CMD_LIST));
1390 1413 return (Z_ERR);
1391 1414 }
1392 1415 if (zone_id == GLOBAL_ZONEID || is_system_labeled()) {
1393 1416 retv = zone_print_list(min_state, verbose, parsable);
1394 1417 } else {
1395 1418 fake_up_local_zone(zone_id, &zent);
1396 1419 retv = Z_OK;
1397 1420 zone_print(&zent, verbose, parsable);
1398 1421 }
1399 1422 return (retv);
1400 1423 }
1401 1424
1402 1425 /*
1403 1426 * Specific target zone: disallow -i/-c suboptions.
1404 1427 */
1405 1428 optind = 0;
1406 1429 while ((arg = getopt(argc, argv, "?pv")) != EOF) {
1407 1430 switch (arg) {
1408 1431 case '?':
1409 1432 sub_usage(SHELP_LIST, CMD_LIST);
1410 1433 return (optopt == '?' ? Z_OK : Z_USAGE);
1411 1434 case 'p':
1412 1435 parsable = B_TRUE;
1413 1436 break;
1414 1437 case 'v':
1415 1438 verbose = B_TRUE;
1416 1439 break;
1417 1440 default:
1418 1441 sub_usage(SHELP_LIST, CMD_LIST);
1419 1442 return (Z_USAGE);
1420 1443 }
1421 1444 }
1422 1445 if (parsable && verbose) {
1423 1446 zerror(gettext("%s -p and -v are mutually exclusive."),
1424 1447 cmd_to_str(CMD_LIST));
1425 1448 return (Z_ERR);
1426 1449 }
1427 1450 if (argc > optind) {
1428 1451 sub_usage(SHELP_LIST, CMD_LIST);
1429 1452 return (Z_USAGE);
1430 1453 }
1431 1454 if (zone_id != GLOBAL_ZONEID && !is_system_labeled()) {
1432 1455 fake_up_local_zone(zone_id, &zent);
1433 1456 /*
1434 1457 * main() will issue a Z_NO_ZONE error if it cannot get an
1435 1458 * id for target_zone, which in a non-global zone should
1436 1459 * happen for any zone name except `zonename`. Thus we
1437 1460 * assert() that here but don't otherwise check.
1438 1461 */
1439 1462 assert(strcmp(zent.zname, target_zone) == 0);
1440 1463 zone_print(&zent, verbose, parsable);
1441 1464 output = B_TRUE;
1442 1465 } else if ((zentp = lookup_running_zone(target_zone)) != NULL) {
1443 1466 zone_print(zentp, verbose, parsable);
1444 1467 output = B_TRUE;
1445 1468 } else if (lookup_zone_info(target_zone, ZONE_ID_UNDEFINED,
1446 1469 &zent) == Z_OK) {
1447 1470 zone_print(&zent, verbose, parsable);
1448 1471 output = B_TRUE;
1449 1472 }
1450 1473
1451 1474 /*
1452 1475 * Invoke brand-specific handler. Note that we do this
1453 1476 * only if we're in the global zone, and target_zone is specified
1454 1477 * and it is not the global zone.
1455 1478 */
1456 1479 if (zone_id == GLOBAL_ZONEID && target_zone != NULL &&
1457 1480 strcmp(target_zone, GLOBAL_ZONENAME) != 0)
1458 1481 if (invoke_brand_handler(CMD_LIST, argv) != Z_OK)
1459 1482 return (Z_ERR);
1460 1483
1461 1484 return (output ? Z_OK : Z_ERR);
1462 1485 }
1463 1486
1464 1487 int
1465 1488 do_subproc(char *cmdbuf)
1466 1489 {
1467 1490 void (*saveint)(int);
1468 1491 void (*saveterm)(int);
1469 1492 void (*savequit)(int);
1470 1493 void (*savehup)(int);
1471 1494 int pid, child, status;
1472 1495
1473 1496 if ((child = vfork()) == 0) {
1474 1497 (void) execl("/bin/sh", "sh", "-c", cmdbuf, (char *)NULL);
1475 1498 }
1476 1499
1477 1500 if (child == -1)
1478 1501 return (-1);
1479 1502
1480 1503 saveint = sigset(SIGINT, SIG_IGN);
1481 1504 saveterm = sigset(SIGTERM, SIG_IGN);
1482 1505 savequit = sigset(SIGQUIT, SIG_IGN);
1483 1506 savehup = sigset(SIGHUP, SIG_IGN);
1484 1507
1485 1508 while ((pid = waitpid(child, &status, 0)) != child && pid != -1)
1486 1509 ;
1487 1510
1488 1511 (void) sigset(SIGINT, saveint);
1489 1512 (void) sigset(SIGTERM, saveterm);
1490 1513 (void) sigset(SIGQUIT, savequit);
1491 1514 (void) sigset(SIGHUP, savehup);
1492 1515
1493 1516 return (pid == -1 ? -1 : status);
1494 1517 }
1495 1518
1496 1519 int
1497 1520 subproc_status(const char *cmd, int status, boolean_t verbose_failure)
1498 1521 {
1499 1522 if (WIFEXITED(status)) {
1500 1523 int exit_code = WEXITSTATUS(status);
1501 1524
1502 1525 if ((verbose_failure) && (exit_code != ZONE_SUBPROC_OK))
1503 1526 zerror(gettext("'%s' failed with exit code %d."), cmd,
1504 1527 exit_code);
1505 1528
1506 1529 return (exit_code);
1507 1530 } else if (WIFSIGNALED(status)) {
1508 1531 int signal = WTERMSIG(status);
1509 1532 char sigstr[SIG2STR_MAX];
1510 1533
1511 1534 if (sig2str(signal, sigstr) == 0) {
1512 1535 zerror(gettext("'%s' terminated by signal SIG%s."), cmd,
1513 1536 sigstr);
1514 1537 } else {
1515 1538 zerror(gettext("'%s' terminated by an unknown signal."),
1516 1539 cmd);
1517 1540 }
1518 1541 } else {
1519 1542 zerror(gettext("'%s' failed for unknown reasons."), cmd);
1520 1543 }
1521 1544
1522 1545 /*
1523 1546 * Assume a subprocess that died due to a signal or an unknown error
1524 1547 * should be considered an exit code of ZONE_SUBPROC_FATAL, as the
1525 1548 * user will likely need to do some manual cleanup.
1526 1549 */
1527 1550 return (ZONE_SUBPROC_FATAL);
1528 1551 }
1529 1552
1530 1553 static int
1531 1554 auth_check(char *user, char *zone, int cmd_num)
1532 1555 {
1533 1556 char authname[MAXAUTHS];
1534 1557
1535 1558 switch (cmd_num) {
1536 1559 case CMD_LIST:
1537 1560 case CMD_HELP:
1538 1561 return (Z_OK);
1539 1562 case SOURCE_ZONE:
1540 1563 (void) strlcpy(authname, ZONE_CLONEFROM_AUTH, MAXAUTHS);
1541 1564 break;
1542 1565 case CMD_BOOT:
1543 1566 case CMD_HALT:
1544 1567 case CMD_READY:
1545 1568 case CMD_SHUTDOWN:
1546 1569 case CMD_REBOOT:
1547 1570 case CMD_SYSBOOT:
1548 1571 case CMD_VERIFY:
1549 1572 case CMD_INSTALL:
1550 1573 case CMD_UNINSTALL:
1551 1574 case CMD_MOUNT:
1552 1575 case CMD_UNMOUNT:
1553 1576 case CMD_CLONE:
1554 1577 case CMD_MOVE:
1555 1578 case CMD_DETACH:
1556 1579 case CMD_ATTACH:
1557 1580 case CMD_MARK:
1558 1581 case CMD_APPLY:
1559 1582 default:
1560 1583 (void) strlcpy(authname, ZONE_MANAGE_AUTH, MAXAUTHS);
1561 1584 break;
1562 1585 }
1563 1586 (void) strlcat(authname, KV_OBJECT, MAXAUTHS);
1564 1587 (void) strlcat(authname, zone, MAXAUTHS);
1565 1588 if (chkauthattr(authname, user) == 0) {
1566 1589 return (Z_ERR);
1567 1590 } else {
1568 1591 /*
1569 1592 * Some subcommands, e.g. install, run subcommands,
1570 1593 * e.g. sysidcfg, that require a real uid of root,
1571 1594 * so switch to root, here.
1572 1595 */
1573 1596 if (setuid(0) == -1) {
1574 1597 zperror(gettext("insufficient privilege"), B_TRUE);
1575 1598 return (Z_ERR);
1576 1599 }
1577 1600 return (Z_OK);
1578 1601 }
1579 1602 }
1580 1603
1581 1604 /*
1582 1605 * Various sanity checks; make sure:
1583 1606 * 1. We're in the global zone.
1584 1607 * 2. The calling user has sufficient privilege.
1585 1608 * 3. The target zone is neither the global zone nor anything starting with
1586 1609 * "SUNW".
1587 1610 * 4a. If we're looking for a 'not running' (i.e., configured or installed)
1588 1611 * zone, the name service knows about it.
1589 1612 * 4b. For some operations which expect a zone not to be running, that it is
1590 1613 * not already running (or ready).
1591 1614 */
1592 1615 static int
1593 1616 sanity_check(char *zone, int cmd_num, boolean_t running,
1594 1617 boolean_t unsafe_when_running, boolean_t force)
1595 1618 {
1596 1619 zone_entry_t *zent;
1597 1620 priv_set_t *privset;
1598 1621 zone_state_t state, min_state;
1599 1622 char kernzone[ZONENAME_MAX];
1600 1623 FILE *fp;
1601 1624
1602 1625 if (getzoneid() != GLOBAL_ZONEID) {
1603 1626 switch (cmd_num) {
1604 1627 case CMD_HALT:
1605 1628 zerror(gettext("use %s to %s this zone."), "halt(1M)",
1606 1629 cmd_to_str(cmd_num));
1607 1630 break;
1608 1631 case CMD_SHUTDOWN:
1609 1632 zerror(gettext("use %s to %s this zone."),
1610 1633 "shutdown(1M)", cmd_to_str(cmd_num));
1611 1634 break;
1612 1635 case CMD_REBOOT:
1613 1636 zerror(gettext("use %s to %s this zone."),
1614 1637 "reboot(1M)", cmd_to_str(cmd_num));
1615 1638 break;
1616 1639 default:
1617 1640 zerror(gettext("must be in the global zone to %s a "
1618 1641 "zone."), cmd_to_str(cmd_num));
1619 1642 break;
1620 1643 }
1621 1644 return (Z_ERR);
1622 1645 }
1623 1646
1624 1647 if ((privset = priv_allocset()) == NULL) {
1625 1648 zerror(gettext("%s failed"), "priv_allocset");
1626 1649 return (Z_ERR);
1627 1650 }
1628 1651
1629 1652 if (getppriv(PRIV_EFFECTIVE, privset) != 0) {
1630 1653 zerror(gettext("%s failed"), "getppriv");
1631 1654 priv_freeset(privset);
1632 1655 return (Z_ERR);
1633 1656 }
1634 1657
1635 1658 if (priv_isfullset(privset) == B_FALSE) {
1636 1659 zerror(gettext("only a privileged user may %s a zone."),
1637 1660 cmd_to_str(cmd_num));
1638 1661 priv_freeset(privset);
1639 1662 return (Z_ERR);
1640 1663 }
1641 1664 priv_freeset(privset);
1642 1665
1643 1666 if (zone == NULL) {
1644 1667 zerror(gettext("no zone specified"));
1645 1668 return (Z_ERR);
1646 1669 }
1647 1670
1648 1671 if (auth_check(username, zone, cmd_num) == Z_ERR) {
1649 1672 zerror(gettext("User %s is not authorized to %s this zone."),
1650 1673 username, cmd_to_str(cmd_num));
1651 1674 return (Z_ERR);
1652 1675 }
1653 1676
1654 1677 if (strcmp(zone, GLOBAL_ZONENAME) == 0) {
1655 1678 zerror(gettext("%s operation is invalid for the global zone."),
1656 1679 cmd_to_str(cmd_num));
1657 1680 return (Z_ERR);
1658 1681 }
1659 1682
1660 1683 if (strncmp(zone, "SUNW", 4) == 0) {
1661 1684 zerror(gettext("%s operation is invalid for zones starting "
1662 1685 "with SUNW."), cmd_to_str(cmd_num));
1663 1686 return (Z_ERR);
1664 1687 }
1665 1688
1666 1689 if (!zonecfg_in_alt_root()) {
1667 1690 zent = lookup_running_zone(zone);
1668 1691 } else if ((fp = zonecfg_open_scratch("", B_FALSE)) == NULL) {
1669 1692 zent = NULL;
1670 1693 } else {
1671 1694 if (zonecfg_find_scratch(fp, zone, zonecfg_get_root(),
1672 1695 kernzone, sizeof (kernzone)) == 0)
1673 1696 zent = lookup_running_zone(kernzone);
1674 1697 else
1675 1698 zent = NULL;
1676 1699 zonecfg_close_scratch(fp);
1677 1700 }
1678 1701
1679 1702 /*
1680 1703 * Look up from the kernel for 'running' zones.
1681 1704 */
1682 1705 if (running && !force) {
1683 1706 if (zent == NULL) {
1684 1707 zerror(gettext("not running"));
1685 1708 return (Z_ERR);
1686 1709 }
1687 1710 } else {
1688 1711 int err;
1689 1712
1690 1713 if (unsafe_when_running && zent != NULL) {
1691 1714 /* check whether the zone is ready or running */
1692 1715 if ((err = zone_get_state(zent->zname,
1693 1716 &zent->zstate_num)) != Z_OK) {
1694 1717 errno = err;
1695 1718 zperror2(zent->zname,
1696 1719 gettext("could not get state"));
1697 1720 /* can't tell, so hedge */
1698 1721 zent->zstate_str = "ready/running";
1699 1722 } else {
1700 1723 zent->zstate_str =
1701 1724 zone_state_str(zent->zstate_num);
1702 1725 }
1703 1726 zerror(gettext("%s operation is invalid for %s zones."),
1704 1727 cmd_to_str(cmd_num), zent->zstate_str);
1705 1728 return (Z_ERR);
1706 1729 }
1707 1730 if ((err = zone_get_state(zone, &state)) != Z_OK) {
1708 1731 errno = err;
1709 1732 zperror2(zone, gettext("could not get state"));
1710 1733 return (Z_ERR);
1711 1734 }
1712 1735 switch (cmd_num) {
1713 1736 case CMD_UNINSTALL:
1714 1737 if (state == ZONE_STATE_CONFIGURED) {
1715 1738 zerror(gettext("is already in state '%s'."),
1716 1739 zone_state_str(ZONE_STATE_CONFIGURED));
1717 1740 return (Z_ERR);
1718 1741 }
1719 1742 break;
1720 1743 case CMD_ATTACH:
1721 1744 if (state == ZONE_STATE_INSTALLED) {
1722 1745 zerror(gettext("is already %s."),
1723 1746 zone_state_str(ZONE_STATE_INSTALLED));
1724 1747 return (Z_ERR);
1725 1748 } else if (state == ZONE_STATE_INCOMPLETE && !force) {
1726 1749 zerror(gettext("zone is %s; %s required."),
1727 1750 zone_state_str(ZONE_STATE_INCOMPLETE),
1728 1751 cmd_to_str(CMD_UNINSTALL));
1729 1752 return (Z_ERR);
1730 1753 }
1731 1754 break;
1732 1755 case CMD_CLONE:
1733 1756 case CMD_INSTALL:
1734 1757 if (state == ZONE_STATE_INSTALLED) {
1735 1758 zerror(gettext("is already %s."),
1736 1759 zone_state_str(ZONE_STATE_INSTALLED));
1737 1760 return (Z_ERR);
1738 1761 } else if (state == ZONE_STATE_INCOMPLETE) {
1739 1762 zerror(gettext("zone is %s; %s required."),
1740 1763 zone_state_str(ZONE_STATE_INCOMPLETE),
1741 1764 cmd_to_str(CMD_UNINSTALL));
1742 1765 return (Z_ERR);
1743 1766 }
1744 1767 break;
1745 1768 case CMD_DETACH:
1746 1769 case CMD_MOVE:
1747 1770 case CMD_READY:
1748 1771 case CMD_BOOT:
1749 1772 case CMD_MOUNT:
1750 1773 case CMD_MARK:
1751 1774 if ((cmd_num == CMD_BOOT || cmd_num == CMD_MOUNT) &&
1752 1775 force)
1753 1776 min_state = ZONE_STATE_INCOMPLETE;
1754 1777 else if (cmd_num == CMD_MARK)
1755 1778 min_state = ZONE_STATE_CONFIGURED;
1756 1779 else
1757 1780 min_state = ZONE_STATE_INSTALLED;
1758 1781
1759 1782 if (state < min_state) {
1760 1783 zerror(gettext("must be %s before %s."),
1761 1784 zone_state_str(min_state),
1762 1785 cmd_to_str(cmd_num));
1763 1786 return (Z_ERR);
1764 1787 }
1765 1788 break;
1766 1789 case CMD_VERIFY:
1767 1790 if (state == ZONE_STATE_INCOMPLETE) {
1768 1791 zerror(gettext("zone is %s; %s required."),
1769 1792 zone_state_str(ZONE_STATE_INCOMPLETE),
1770 1793 cmd_to_str(CMD_UNINSTALL));
1771 1794 return (Z_ERR);
1772 1795 }
1773 1796 break;
1774 1797 case CMD_UNMOUNT:
1775 1798 if (state != ZONE_STATE_MOUNTED) {
1776 1799 zerror(gettext("must be %s before %s."),
1777 1800 zone_state_str(ZONE_STATE_MOUNTED),
1778 1801 cmd_to_str(cmd_num));
1779 1802 return (Z_ERR);
1780 1803 }
1781 1804 break;
1782 1805 case CMD_SYSBOOT:
1783 1806 if (state != ZONE_STATE_INSTALLED) {
1784 1807 zerror(gettext("%s operation is invalid for %s "
1785 1808 "zones."), cmd_to_str(cmd_num),
1786 1809 zone_state_str(state));
1787 1810 return (Z_ERR);
1788 1811 }
1789 1812 break;
1790 1813 }
1791 1814 }
1792 1815 return (Z_OK);
1793 1816 }
1794 1817
1795 1818 static int
1796 1819 halt_func(int argc, char *argv[])
1797 1820 {
1798 1821 zone_cmd_arg_t zarg;
1799 1822 int arg;
1800 1823
1801 1824 if (zonecfg_in_alt_root()) {
1802 1825 zerror(gettext("cannot halt zone in alternate root"));
1803 1826 return (Z_ERR);
1804 1827 }
1805 1828
1806 1829 optind = 0;
1807 1830 if ((arg = getopt(argc, argv, "?")) != EOF) {
1808 1831 switch (arg) {
1809 1832 case '?':
1810 1833 sub_usage(SHELP_HALT, CMD_HALT);
1811 1834 return (optopt == '?' ? Z_OK : Z_USAGE);
1812 1835 default:
1813 1836 sub_usage(SHELP_HALT, CMD_HALT);
1814 1837 return (Z_USAGE);
1815 1838 }
1816 1839 }
1817 1840 if (argc > optind) {
1818 1841 sub_usage(SHELP_HALT, CMD_HALT);
1819 1842 return (Z_USAGE);
1820 1843 }
1821 1844 /*
1822 1845 * zoneadmd should be the one to decide whether or not to proceed,
1823 1846 * so even though it seems that the fourth parameter below should
1824 1847 * perhaps be B_TRUE, it really shouldn't be.
1825 1848 */
1826 1849 if (sanity_check(target_zone, CMD_HALT, B_FALSE, B_FALSE, B_FALSE)
1827 1850 != Z_OK)
1828 1851 return (Z_ERR);
1829 1852
1830 1853 /*
1831 1854 * Invoke brand-specific handler.
1832 1855 */
1833 1856 if (invoke_brand_handler(CMD_HALT, argv) != Z_OK)
1834 1857 return (Z_ERR);
1835 1858
1836 1859 zarg.cmd = Z_HALT;
1837 1860 return ((zonecfg_call_zoneadmd(target_zone, &zarg, locale,
1838 1861 B_TRUE) == 0) ? Z_OK : Z_ERR);
1839 1862 }
1840 1863
1841 1864 static int
1842 1865 shutdown_func(int argc, char *argv[])
1843 1866 {
1844 1867 zone_cmd_arg_t zarg;
1845 1868 int arg;
1846 1869 boolean_t reboot = B_FALSE;
1847 1870
1848 1871 zarg.cmd = Z_SHUTDOWN;
1849 1872
1850 1873 if (zonecfg_in_alt_root()) {
1851 1874 zerror(gettext("cannot shut down zone in alternate root"));
1852 1875 return (Z_ERR);
1853 1876 }
1854 1877
1855 1878 optind = 0;
1856 1879 while ((arg = getopt(argc, argv, "?r")) != EOF) {
1857 1880 switch (arg) {
1858 1881 case '?':
1859 1882 sub_usage(SHELP_SHUTDOWN, CMD_SHUTDOWN);
1860 1883 return (optopt == '?' ? Z_OK : Z_USAGE);
1861 1884 case 'r':
1862 1885 reboot = B_TRUE;
1863 1886 break;
1864 1887 default:
1865 1888 sub_usage(SHELP_SHUTDOWN, CMD_SHUTDOWN);
1866 1889 return (Z_USAGE);
1867 1890 }
1868 1891 }
1869 1892
1870 1893 zarg.bootbuf[0] = '\0';
1871 1894 for (; optind < argc; optind++) {
1872 1895 if (strlcat(zarg.bootbuf, argv[optind],
1873 1896 sizeof (zarg.bootbuf)) >= sizeof (zarg.bootbuf)) {
1874 1897 zerror(gettext("Boot argument list too long"));
1875 1898 return (Z_ERR);
1876 1899 }
1877 1900 if (optind < argc - 1)
1878 1901 if (strlcat(zarg.bootbuf, " ", sizeof (zarg.bootbuf)) >=
1879 1902 sizeof (zarg.bootbuf)) {
1880 1903 zerror(gettext("Boot argument list too long"));
1881 1904 return (Z_ERR);
1882 1905 }
1883 1906 }
1884 1907
1885 1908 /*
1886 1909 * zoneadmd should be the one to decide whether or not to proceed,
1887 1910 * so even though it seems that the third parameter below should
1888 1911 * perhaps be B_TRUE, it really shouldn't be.
1889 1912 */
1890 1913 if (sanity_check(target_zone, CMD_SHUTDOWN, B_TRUE, B_FALSE, B_FALSE)
1891 1914 != Z_OK)
1892 1915 return (Z_ERR);
1893 1916
1894 1917 if (zonecfg_call_zoneadmd(target_zone, &zarg, locale, B_TRUE) != Z_OK)
1895 1918 return (Z_ERR);
1896 1919
1897 1920 if (reboot) {
1898 1921 if (sanity_check(target_zone, CMD_BOOT, B_FALSE, B_FALSE,
1899 1922 B_FALSE) != Z_OK)
1900 1923 return (Z_ERR);
1901 1924
1902 1925 zarg.cmd = Z_BOOT;
1903 1926 if (zonecfg_call_zoneadmd(target_zone, &zarg, locale,
1904 1927 B_TRUE) != Z_OK)
1905 1928 return (Z_ERR);
1906 1929 }
1907 1930 return (Z_OK);
1908 1931 }
1909 1932
1910 1933 static int
1911 1934 reboot_func(int argc, char *argv[])
1912 1935 {
1913 1936 zone_cmd_arg_t zarg;
1914 1937 int arg;
1915 1938
1916 1939 if (zonecfg_in_alt_root()) {
1917 1940 zerror(gettext("cannot reboot zone in alternate root"));
1918 1941 return (Z_ERR);
1919 1942 }
1920 1943
1921 1944 optind = 0;
1922 1945 if ((arg = getopt(argc, argv, "?")) != EOF) {
1923 1946 switch (arg) {
1924 1947 case '?':
1925 1948 sub_usage(SHELP_REBOOT, CMD_REBOOT);
1926 1949 return (optopt == '?' ? Z_OK : Z_USAGE);
1927 1950 default:
1928 1951 sub_usage(SHELP_REBOOT, CMD_REBOOT);
1929 1952 return (Z_USAGE);
1930 1953 }
1931 1954 }
1932 1955
1933 1956 zarg.bootbuf[0] = '\0';
1934 1957 for (; optind < argc; optind++) {
1935 1958 if (strlcat(zarg.bootbuf, argv[optind],
1936 1959 sizeof (zarg.bootbuf)) >= sizeof (zarg.bootbuf)) {
1937 1960 zerror(gettext("Boot argument list too long"));
1938 1961 return (Z_ERR);
1939 1962 }
1940 1963 if (optind < argc - 1)
1941 1964 if (strlcat(zarg.bootbuf, " ", sizeof (zarg.bootbuf)) >=
1942 1965 sizeof (zarg.bootbuf)) {
1943 1966 zerror(gettext("Boot argument list too long"));
1944 1967 return (Z_ERR);
1945 1968 }
1946 1969 }
1947 1970
1948 1971
1949 1972 /*
1950 1973 * zoneadmd should be the one to decide whether or not to proceed,
1951 1974 * so even though it seems that the fourth parameter below should
1952 1975 * perhaps be B_TRUE, it really shouldn't be.
1953 1976 */
1954 1977 if (sanity_check(target_zone, CMD_REBOOT, B_TRUE, B_FALSE, B_FALSE)
1955 1978 != Z_OK)
1956 1979 return (Z_ERR);
1957 1980 if (verify_details(CMD_REBOOT, argv) != Z_OK)
1958 1981 return (Z_ERR);
1959 1982
1960 1983 zarg.cmd = Z_REBOOT;
1961 1984 return ((zonecfg_call_zoneadmd(target_zone, &zarg, locale, B_TRUE) == 0)
1962 1985 ? Z_OK : Z_ERR);
1963 1986 }
1964 1987
1965 1988 static int
1966 1989 get_hook(brand_handle_t bh, char *cmd, size_t len, int (*bp)(brand_handle_t,
1967 1990 const char *, const char *, char *, size_t), char *zonename, char *zonepath)
1968 1991 {
1969 1992 if (strlcpy(cmd, EXEC_PREFIX, len) >= len)
1970 1993 return (Z_ERR);
1971 1994
1972 1995 if (bp(bh, zonename, zonepath, cmd + EXEC_LEN, len - EXEC_LEN) != 0)
1973 1996 return (Z_ERR);
1974 1997
1975 1998 if (strlen(cmd) <= EXEC_LEN)
1976 1999 cmd[0] = '\0';
1977 2000
1978 2001 return (Z_OK);
1979 2002 }
1980 2003
1981 2004 static int
1982 2005 verify_brand(zone_dochandle_t handle, int cmd_num, char *argv[])
1983 2006 {
1984 2007 char cmdbuf[MAXPATHLEN];
1985 2008 int err;
1986 2009 char zonepath[MAXPATHLEN];
1987 2010 brand_handle_t bh = NULL;
1988 2011 int status, i;
1989 2012
1990 2013 /*
1991 2014 * Fetch the verify command from the brand configuration.
1992 2015 * "exec" the command so that the returned status is that of
1993 2016 * the command and not the shell.
1994 2017 */
1995 2018 if (handle == NULL) {
1996 2019 (void) strlcpy(zonepath, "-", sizeof (zonepath));
1997 2020 } else if ((err = zonecfg_get_zonepath(handle, zonepath,
1998 2021 sizeof (zonepath))) != Z_OK) {
1999 2022 errno = err;
2000 2023 zperror(cmd_to_str(cmd_num), B_TRUE);
2001 2024 return (Z_ERR);
2002 2025 }
2003 2026 if ((bh = brand_open(target_brand)) == NULL) {
2004 2027 zerror(gettext("missing or invalid brand"));
2005 2028 return (Z_ERR);
2006 2029 }
2007 2030
2008 2031 /*
2009 2032 * If the brand has its own verification routine, execute it now.
2010 2033 * The verification routine validates the intended zoneadm
2011 2034 * operation for the specific brand. The zoneadm subcommand and
2012 2035 * all its arguments are passed to the routine.
2013 2036 */
2014 2037 err = get_hook(bh, cmdbuf, sizeof (cmdbuf), brand_get_verify_adm,
2015 2038 target_zone, zonepath);
2016 2039 brand_close(bh);
2017 2040 if (err != Z_OK)
2018 2041 return (Z_BRAND_ERROR);
2019 2042 if (cmdbuf[0] == '\0')
2020 2043 return (Z_OK);
2021 2044
2022 2045 if (strlcat(cmdbuf, cmd_to_str(cmd_num),
2023 2046 sizeof (cmdbuf)) >= sizeof (cmdbuf))
2024 2047 return (Z_ERR);
2025 2048
2026 2049 /* Build the argv string */
2027 2050 i = 0;
2028 2051 while (argv[i] != NULL) {
2029 2052 if ((strlcat(cmdbuf, " ",
2030 2053 sizeof (cmdbuf)) >= sizeof (cmdbuf)) ||
2031 2054 (strlcat(cmdbuf, argv[i++],
2032 2055 sizeof (cmdbuf)) >= sizeof (cmdbuf)))
2033 2056 return (Z_ERR);
2034 2057 }
2035 2058
2036 2059 status = do_subproc(cmdbuf);
2037 2060 err = subproc_status(gettext("brand-specific verification"),
2038 2061 status, B_FALSE);
2039 2062
2040 2063 return ((err == ZONE_SUBPROC_OK) ? Z_OK : Z_BRAND_ERROR);
2041 2064 }
2042 2065
2043 2066 static int
2044 2067 verify_rctls(zone_dochandle_t handle)
2045 2068 {
2046 2069 struct zone_rctltab rctltab;
2047 2070 size_t rbs = rctlblk_size();
2048 2071 rctlblk_t *rctlblk;
2049 2072 int error = Z_INVAL;
2050 2073
2051 2074 if ((rctlblk = malloc(rbs)) == NULL) {
2052 2075 zerror(gettext("failed to allocate %lu bytes: %s"), rbs,
2053 2076 strerror(errno));
2054 2077 return (Z_NOMEM);
2055 2078 }
2056 2079
2057 2080 if (zonecfg_setrctlent(handle) != Z_OK) {
2058 2081 zerror(gettext("zonecfg_setrctlent failed"));
2059 2082 free(rctlblk);
2060 2083 return (error);
2061 2084 }
2062 2085
2063 2086 rctltab.zone_rctl_valptr = NULL;
2064 2087 while (zonecfg_getrctlent(handle, &rctltab) == Z_OK) {
2065 2088 struct zone_rctlvaltab *rctlval;
2066 2089 const char *name = rctltab.zone_rctl_name;
2067 2090
2068 2091 if (!zonecfg_is_rctl(name)) {
2069 2092 zerror(gettext("WARNING: Ignoring unrecognized rctl "
2070 2093 "'%s'."), name);
2071 2094 zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
2072 2095 rctltab.zone_rctl_valptr = NULL;
2073 2096 continue;
2074 2097 }
2075 2098
2076 2099 for (rctlval = rctltab.zone_rctl_valptr; rctlval != NULL;
2077 2100 rctlval = rctlval->zone_rctlval_next) {
2078 2101 if (zonecfg_construct_rctlblk(rctlval, rctlblk)
2079 2102 != Z_OK) {
2080 2103 zerror(gettext("invalid rctl value: "
2081 2104 "(priv=%s,limit=%s,action%s)"),
2082 2105 rctlval->zone_rctlval_priv,
2083 2106 rctlval->zone_rctlval_limit,
2084 2107 rctlval->zone_rctlval_action);
2085 2108 goto out;
2086 2109 }
2087 2110 if (!zonecfg_valid_rctl(name, rctlblk)) {
2088 2111 zerror(gettext("(priv=%s,limit=%s,action=%s) "
2089 2112 "is not a valid value for rctl '%s'"),
2090 2113 rctlval->zone_rctlval_priv,
2091 2114 rctlval->zone_rctlval_limit,
2092 2115 rctlval->zone_rctlval_action,
2093 2116 name);
2094 2117 goto out;
2095 2118 }
2096 2119 }
2097 2120 zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
2098 2121 }
2099 2122 rctltab.zone_rctl_valptr = NULL;
2100 2123 error = Z_OK;
2101 2124 out:
2102 2125 zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
2103 2126 (void) zonecfg_endrctlent(handle);
2104 2127 free(rctlblk);
2105 2128 return (error);
2106 2129 }
2107 2130
2108 2131 static int
2109 2132 verify_pool(zone_dochandle_t handle)
2110 2133 {
2111 2134 char poolname[MAXPATHLEN];
2112 2135 pool_conf_t *poolconf;
2113 2136 pool_t *pool;
2114 2137 int status;
2115 2138 int error;
2116 2139
2117 2140 /*
2118 2141 * This ends up being very similar to the check done in zoneadmd.
2119 2142 */
2120 2143 error = zonecfg_get_pool(handle, poolname, sizeof (poolname));
2121 2144 if (error == Z_NO_ENTRY || (error == Z_OK && strlen(poolname) == 0)) {
2122 2145 /*
2123 2146 * No pool specified.
2124 2147 */
2125 2148 return (0);
2126 2149 }
2127 2150 if (error != Z_OK) {
2128 2151 zperror(gettext("Unable to retrieve pool name from "
2129 2152 "configuration"), B_TRUE);
2130 2153 return (error);
2131 2154 }
2132 2155 /*
2133 2156 * Don't do anything if pools aren't enabled.
2134 2157 */
2135 2158 if (pool_get_status(&status) != PO_SUCCESS || status != POOL_ENABLED) {
2136 2159 zerror(gettext("WARNING: pools facility not active; "
2137 2160 "zone will not be bound to pool '%s'."), poolname);
2138 2161 return (Z_OK);
2139 2162 }
2140 2163 /*
2141 2164 * Try to provide a sane error message if the requested pool doesn't
2142 2165 * exist. It isn't clear that pools-related failures should
2143 2166 * necessarily translate to a failure to verify the zone configuration,
2144 2167 * hence they are not considered errors.
2145 2168 */
2146 2169 if ((poolconf = pool_conf_alloc()) == NULL) {
2147 2170 zerror(gettext("WARNING: pool_conf_alloc failed; "
2148 2171 "using default pool"));
2149 2172 return (Z_OK);
2150 2173 }
2151 2174 if (pool_conf_open(poolconf, pool_dynamic_location(), PO_RDONLY) !=
2152 2175 PO_SUCCESS) {
2153 2176 zerror(gettext("WARNING: pool_conf_open failed; "
2154 2177 "using default pool"));
2155 2178 pool_conf_free(poolconf);
2156 2179 return (Z_OK);
2157 2180 }
2158 2181 pool = pool_get_pool(poolconf, poolname);
2159 2182 (void) pool_conf_close(poolconf);
2160 2183 pool_conf_free(poolconf);
2161 2184 if (pool == NULL) {
2162 2185 zerror(gettext("WARNING: pool '%s' not found. "
2163 2186 "using default pool"), poolname);
2164 2187 }
2165 2188
2166 2189 return (Z_OK);
2167 2190 }
2168 2191
2169 2192 /*
2170 2193 * Verify that the special device/file system exists and is valid.
2171 2194 */
2172 2195 static int
2173 2196 verify_fs_special(struct zone_fstab *fstab)
2174 2197 {
2175 2198 struct stat64 st;
2176 2199
2177 2200 /*
2178 2201 * This validation is really intended for standard zone administration.
2179 2202 * If we are in a mini-root or some other upgrade situation where
2180 2203 * we are using the scratch zone, just by-pass this.
2181 2204 */
2182 2205 if (zonecfg_in_alt_root())
2183 2206 return (Z_OK);
2184 2207
2185 2208 if (strcmp(fstab->zone_fs_type, MNTTYPE_ZFS) == 0)
2186 2209 return (verify_fs_zfs(fstab));
2187 2210
2188 2211 if (stat64(fstab->zone_fs_special, &st) != 0) {
2189 2212 (void) fprintf(stderr, gettext("could not verify fs "
2190 2213 "%s: could not access %s: %s\n"), fstab->zone_fs_dir,
2191 2214 fstab->zone_fs_special, strerror(errno));
2192 2215 return (Z_ERR);
2193 2216 }
2194 2217
2195 2218 if (strcmp(st.st_fstype, MNTTYPE_NFS) == 0) {
2196 2219 /*
2197 2220 * TRANSLATION_NOTE
2198 2221 * fs and NFS are literals that should
2199 2222 * not be translated.
2200 2223 */
2201 2224 (void) fprintf(stderr, gettext("cannot verify "
2202 2225 "fs %s: NFS mounted file system.\n"
2203 2226 "\tA local file system must be used.\n"),
2204 2227 fstab->zone_fs_special);
2205 2228 return (Z_ERR);
2206 2229 }
2207 2230
2208 2231 return (Z_OK);
2209 2232 }
2210 2233
2211 2234 static int
2212 2235 isregfile(const char *path)
2213 2236 {
2214 2237 struct stat64 st;
2215 2238
2216 2239 if (stat64(path, &st) == -1)
2217 2240 return (-1);
2218 2241
2219 2242 return (S_ISREG(st.st_mode));
2220 2243 }
2221 2244
2222 2245 static int
2223 2246 verify_filesystems(zone_dochandle_t handle)
2224 2247 {
2225 2248 int return_code = Z_OK;
2226 2249 struct zone_fstab fstab;
2227 2250 char cmdbuf[MAXPATHLEN];
2228 2251 struct stat st;
2229 2252
2230 2253 /*
2231 2254 * Since the actual mount point is not known until the dependent mounts
2232 2255 * are performed, we don't attempt any path validation here: that will
2233 2256 * happen later when zoneadmd actually does the mounts.
2234 2257 */
2235 2258 if (zonecfg_setfsent(handle) != Z_OK) {
2236 2259 (void) fprintf(stderr, gettext("could not verify file systems: "
2237 2260 "unable to enumerate mounts\n"));
2238 2261 return (Z_ERR);
2239 2262 }
2240 2263 while (zonecfg_getfsent(handle, &fstab) == Z_OK) {
2241 2264 if (!zonecfg_valid_fs_type(fstab.zone_fs_type)) {
2242 2265 (void) fprintf(stderr, gettext("cannot verify fs %s: "
2243 2266 "type %s is not allowed.\n"), fstab.zone_fs_dir,
2244 2267 fstab.zone_fs_type);
2245 2268 return_code = Z_ERR;
2246 2269 goto next_fs;
2247 2270 }
2248 2271 /*
2249 2272 * Verify /usr/lib/fs/<fstype>/mount exists.
2250 2273 */
2251 2274 if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/mount",
2252 2275 fstab.zone_fs_type) > sizeof (cmdbuf)) {
2253 2276 (void) fprintf(stderr, gettext("cannot verify fs %s: "
2254 2277 "type %s is too long.\n"), fstab.zone_fs_dir,
2255 2278 fstab.zone_fs_type);
2256 2279 return_code = Z_ERR;
2257 2280 goto next_fs;
2258 2281 }
2259 2282 if (stat(cmdbuf, &st) != 0) {
2260 2283 (void) fprintf(stderr, gettext("could not verify fs "
2261 2284 "%s: could not access %s: %s\n"), fstab.zone_fs_dir,
2262 2285 cmdbuf, strerror(errno));
2263 2286 return_code = Z_ERR;
2264 2287 goto next_fs;
2265 2288 }
2266 2289 if (!S_ISREG(st.st_mode)) {
2267 2290 (void) fprintf(stderr, gettext("could not verify fs "
2268 2291 "%s: %s is not a regular file\n"),
2269 2292 fstab.zone_fs_dir, cmdbuf);
2270 2293 return_code = Z_ERR;
2271 2294 goto next_fs;
2272 2295 }
2273 2296 /*
2274 2297 * If zone_fs_raw is set, verify that there's an fsck
2275 2298 * binary for it. If zone_fs_raw is not set, and it's
2276 2299 * not a regular file (lofi mount), and there's an fsck
2277 2300 * binary for it, complain.
2278 2301 */
2279 2302 if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/fsck",
2280 2303 fstab.zone_fs_type) > sizeof (cmdbuf)) {
2281 2304 (void) fprintf(stderr, gettext("cannot verify fs %s: "
2282 2305 "type %s is too long.\n"), fstab.zone_fs_dir,
2283 2306 fstab.zone_fs_type);
2284 2307 return_code = Z_ERR;
2285 2308 goto next_fs;
2286 2309 }
2287 2310 if (fstab.zone_fs_raw[0] != '\0' &&
2288 2311 (stat(cmdbuf, &st) != 0 || !S_ISREG(st.st_mode))) {
2289 2312 (void) fprintf(stderr, gettext("cannot verify fs %s: "
2290 2313 "'raw' device specified but "
2291 2314 "no fsck executable exists for %s\n"),
2292 2315 fstab.zone_fs_dir, fstab.zone_fs_type);
2293 2316 return_code = Z_ERR;
2294 2317 goto next_fs;
2295 2318 } else if (fstab.zone_fs_raw[0] == '\0' &&
2296 2319 stat(cmdbuf, &st) == 0 &&
2297 2320 isregfile(fstab.zone_fs_special) != 1) {
2298 2321 (void) fprintf(stderr, gettext("could not verify fs "
2299 2322 "%s: must specify 'raw' device for %s "
2300 2323 "file systems\n"),
2301 2324 fstab.zone_fs_dir, fstab.zone_fs_type);
2302 2325 return_code = Z_ERR;
2303 2326 goto next_fs;
2304 2327 }
2305 2328
2306 2329 /* Verify fs_special. */
2307 2330 if ((return_code = verify_fs_special(&fstab)) != Z_OK)
2308 2331 goto next_fs;
2309 2332
2310 2333 /* Verify fs_raw. */
2311 2334 if (fstab.zone_fs_raw[0] != '\0' &&
2312 2335 stat(fstab.zone_fs_raw, &st) != 0) {
2313 2336 /*
2314 2337 * TRANSLATION_NOTE
2315 2338 * fs is a literal that should not be translated.
2316 2339 */
2317 2340 (void) fprintf(stderr, gettext("could not verify fs "
2318 2341 "%s: could not access %s: %s\n"), fstab.zone_fs_dir,
2319 2342 fstab.zone_fs_raw, strerror(errno));
2320 2343 return_code = Z_ERR;
2321 2344 goto next_fs;
2322 2345 }
2323 2346 next_fs:
2324 2347 zonecfg_free_fs_option_list(fstab.zone_fs_options);
2325 2348 }
2326 2349 (void) zonecfg_endfsent(handle);
2327 2350
2328 2351 return (return_code);
2329 2352 }
2330 2353
2331 2354 static int
2332 2355 verify_limitpriv(zone_dochandle_t handle)
2333 2356 {
2334 2357 char *privname = NULL;
2335 2358 int err;
2336 2359 priv_set_t *privs;
2337 2360
2338 2361 if ((privs = priv_allocset()) == NULL) {
2339 2362 zperror(gettext("failed to allocate privilege set"), B_FALSE);
2340 2363 return (Z_NOMEM);
2341 2364 }
2342 2365 err = zonecfg_get_privset(handle, privs, &privname);
2343 2366 switch (err) {
2344 2367 case Z_OK:
2345 2368 break;
2346 2369 case Z_PRIV_PROHIBITED:
2347 2370 (void) fprintf(stderr, gettext("privilege \"%s\" is not "
2348 2371 "permitted within the zone's privilege set\n"), privname);
2349 2372 break;
2350 2373 case Z_PRIV_REQUIRED:
2351 2374 (void) fprintf(stderr, gettext("required privilege \"%s\" is "
2352 2375 "missing from the zone's privilege set\n"), privname);
2353 2376 break;
2354 2377 case Z_PRIV_UNKNOWN:
2355 2378 (void) fprintf(stderr, gettext("unknown privilege \"%s\" "
2356 2379 "specified in the zone's privilege set\n"), privname);
2357 2380 break;
2358 2381 default:
2359 2382 zperror(
2360 2383 gettext("failed to determine the zone's privilege set"),
2361 2384 B_TRUE);
2362 2385 break;
2363 2386 }
2364 2387 free(privname);
2365 2388 priv_freeset(privs);
2366 2389 return (err);
2367 2390 }
2368 2391
2369 2392 static void
2370 2393 free_local_netifs(int if_cnt, struct net_if **if_list)
2371 2394 {
2372 2395 int i;
2373 2396
2374 2397 for (i = 0; i < if_cnt; i++) {
2375 2398 free(if_list[i]->name);
2376 2399 free(if_list[i]);
2377 2400 }
2378 2401 free(if_list);
2379 2402 }
2380 2403
2381 2404 /*
2382 2405 * Get a list of the network interfaces, along with their address families,
2383 2406 * that are plumbed in the global zone. See if_tcp(7p) for a description
2384 2407 * of the ioctls used here.
2385 2408 */
2386 2409 static int
2387 2410 get_local_netifs(int *if_cnt, struct net_if ***if_list)
2388 2411 {
2389 2412 int s;
2390 2413 int i;
2391 2414 int res = Z_OK;
2392 2415 int space_needed;
2393 2416 int cnt = 0;
2394 2417 struct lifnum if_num;
2395 2418 struct lifconf if_conf;
2396 2419 struct lifreq *if_reqp;
2397 2420 char *if_buf;
2398 2421 struct net_if **local_ifs = NULL;
2399 2422
2400 2423 *if_cnt = 0;
2401 2424 *if_list = NULL;
2402 2425
2403 2426 if ((s = socket(SOCKET_AF(AF_INET), SOCK_DGRAM, 0)) < 0)
2404 2427 return (Z_ERR);
2405 2428
2406 2429 /*
2407 2430 * Come back here in the unlikely event that the number of interfaces
2408 2431 * increases between the time we get the count and the time we do the
2409 2432 * SIOCGLIFCONF ioctl.
2410 2433 */
2411 2434 retry:
2412 2435 /* Get the number of interfaces. */
2413 2436 if_num.lifn_family = AF_UNSPEC;
2414 2437 if_num.lifn_flags = LIFC_NOXMIT;
2415 2438 if (ioctl(s, SIOCGLIFNUM, &if_num) < 0) {
2416 2439 (void) close(s);
2417 2440 return (Z_ERR);
2418 2441 }
2419 2442
2420 2443 /* Get the interface configuration list. */
2421 2444 space_needed = if_num.lifn_count * sizeof (struct lifreq);
2422 2445 if ((if_buf = malloc(space_needed)) == NULL) {
2423 2446 (void) close(s);
2424 2447 return (Z_ERR);
2425 2448 }
2426 2449 if_conf.lifc_family = AF_UNSPEC;
2427 2450 if_conf.lifc_flags = LIFC_NOXMIT;
2428 2451 if_conf.lifc_len = space_needed;
2429 2452 if_conf.lifc_buf = if_buf;
2430 2453 if (ioctl(s, SIOCGLIFCONF, &if_conf) < 0) {
2431 2454 free(if_buf);
2432 2455 /*
2433 2456 * SIOCGLIFCONF returns EINVAL if the buffer we passed in is
2434 2457 * too small. In this case go back and get the new if cnt.
2435 2458 */
2436 2459 if (errno == EINVAL)
2437 2460 goto retry;
2438 2461
2439 2462 (void) close(s);
2440 2463 return (Z_ERR);
2441 2464 }
2442 2465 (void) close(s);
2443 2466
2444 2467 /* Get the name and address family for each interface. */
2445 2468 if_reqp = if_conf.lifc_req;
2446 2469 for (i = 0; i < (if_conf.lifc_len / sizeof (struct lifreq)); i++) {
2447 2470 struct net_if **p;
2448 2471 struct lifreq req;
2449 2472
2450 2473 if (strcmp(LOOPBACK_IF, if_reqp->lifr_name) == 0) {
2451 2474 if_reqp++;
2452 2475 continue;
2453 2476 }
2454 2477
2455 2478 if ((s = socket(SOCKET_AF(if_reqp->lifr_addr.ss_family),
2456 2479 SOCK_DGRAM, 0)) == -1) {
2457 2480 res = Z_ERR;
2458 2481 break;
2459 2482 }
2460 2483
2461 2484 (void) strncpy(req.lifr_name, if_reqp->lifr_name,
2462 2485 sizeof (req.lifr_name));
2463 2486 if (ioctl(s, SIOCGLIFADDR, &req) < 0) {
2464 2487 (void) close(s);
2465 2488 if_reqp++;
2466 2489 continue;
2467 2490 }
2468 2491
2469 2492 if ((p = (struct net_if **)realloc(local_ifs,
2470 2493 sizeof (struct net_if *) * (cnt + 1))) == NULL) {
2471 2494 res = Z_ERR;
2472 2495 break;
2473 2496 }
2474 2497 local_ifs = p;
2475 2498
2476 2499 if ((local_ifs[cnt] = malloc(sizeof (struct net_if))) == NULL) {
2477 2500 res = Z_ERR;
2478 2501 break;
2479 2502 }
2480 2503
2481 2504 if ((local_ifs[cnt]->name = strdup(if_reqp->lifr_name))
2482 2505 == NULL) {
2483 2506 free(local_ifs[cnt]);
2484 2507 res = Z_ERR;
2485 2508 break;
2486 2509 }
2487 2510 local_ifs[cnt]->af = req.lifr_addr.ss_family;
2488 2511 cnt++;
2489 2512
2490 2513 (void) close(s);
2491 2514 if_reqp++;
2492 2515 }
2493 2516
2494 2517 free(if_buf);
2495 2518
2496 2519 if (res != Z_OK) {
2497 2520 free_local_netifs(cnt, local_ifs);
2498 2521 } else {
2499 2522 *if_cnt = cnt;
2500 2523 *if_list = local_ifs;
2501 2524 }
2502 2525
2503 2526 return (res);
2504 2527 }
2505 2528
2506 2529 static char *
2507 2530 af2str(int af)
2508 2531 {
2509 2532 switch (af) {
2510 2533 case AF_INET:
2511 2534 return ("IPv4");
2512 2535 case AF_INET6:
2513 2536 return ("IPv6");
2514 2537 default:
2515 2538 return ("Unknown");
2516 2539 }
2517 2540 }
2518 2541
2519 2542 /*
2520 2543 * Cross check the network interface name and address family with the
2521 2544 * interfaces that are set up in the global zone so that we can print the
2522 2545 * appropriate error message.
2523 2546 */
2524 2547 static void
2525 2548 print_net_err(char *phys, char *addr, int af, char *msg)
2526 2549 {
2527 2550 int i;
2528 2551 int local_if_cnt = 0;
2529 2552 struct net_if **local_ifs = NULL;
2530 2553 boolean_t found_if = B_FALSE;
2531 2554 boolean_t found_af = B_FALSE;
2532 2555
2533 2556 if (get_local_netifs(&local_if_cnt, &local_ifs) != Z_OK) {
2534 2557 (void) fprintf(stderr,
2535 2558 gettext("could not verify %s %s=%s %s=%s\n\t%s\n"),
2536 2559 "net", "address", addr, "physical", phys, msg);
2537 2560 return;
2538 2561 }
2539 2562
2540 2563 for (i = 0; i < local_if_cnt; i++) {
2541 2564 if (strcmp(phys, local_ifs[i]->name) == 0) {
2542 2565 found_if = B_TRUE;
2543 2566 if (af == local_ifs[i]->af) {
2544 2567 found_af = B_TRUE;
2545 2568 break;
2546 2569 }
2547 2570 }
2548 2571 }
2549 2572
2550 2573 free_local_netifs(local_if_cnt, local_ifs);
2551 2574
2552 2575 if (!found_if) {
2553 2576 (void) fprintf(stderr,
2554 2577 gettext("could not verify %s %s=%s\n\t"
2555 2578 "network interface %s is not plumbed in the global zone\n"),
2556 2579 "net", "physical", phys, phys);
2557 2580 return;
2558 2581 }
2559 2582
2560 2583 /*
2561 2584 * Print this error if we were unable to find the address family
2562 2585 * for this interface. If the af variable is not initialized to
2563 2586 * to something meaningful by the caller (not AF_UNSPEC) then we
2564 2587 * also skip this message since it wouldn't be informative.
2565 2588 */
2566 2589 if (!found_af && af != AF_UNSPEC) {
2567 2590 (void) fprintf(stderr,
2568 2591 gettext("could not verify %s %s=%s %s=%s\n\tthe %s address "
2569 2592 "family is not configured on this network interface in "
2570 2593 "the\n\tglobal zone\n"),
2571 2594 "net", "address", addr, "physical", phys, af2str(af));
2572 2595 return;
2573 2596 }
2574 2597
2575 2598 (void) fprintf(stderr,
2576 2599 gettext("could not verify %s %s=%s %s=%s\n\t%s\n"),
2577 2600 "net", "address", addr, "physical", phys, msg);
2578 2601 }
2579 2602
2580 2603 static int
2581 2604 verify_handle(int cmd_num, zone_dochandle_t handle, char *argv[])
2582 2605 {
2583 2606 struct zone_nwiftab nwiftab;
2584 2607 int return_code = Z_OK;
2585 2608 int err;
2586 2609 boolean_t in_alt_root;
2587 2610 zone_iptype_t iptype;
2588 2611 dladm_handle_t dh;
2589 2612 dladm_status_t status;
2590 2613 datalink_id_t linkid;
2591 2614 char errmsg[DLADM_STRSIZE];
2592 2615
2593 2616 in_alt_root = zonecfg_in_alt_root();
2594 2617 if (in_alt_root)
2595 2618 goto no_net;
2596 2619
2597 2620 if ((err = zonecfg_get_iptype(handle, &iptype)) != Z_OK) {
2598 2621 errno = err;
2599 2622 zperror(cmd_to_str(cmd_num), B_TRUE);
2600 2623 zonecfg_fini_handle(handle);
2601 2624 return (Z_ERR);
2602 2625 }
2603 2626 if ((err = zonecfg_setnwifent(handle)) != Z_OK) {
2604 2627 errno = err;
2605 2628 zperror(cmd_to_str(cmd_num), B_TRUE);
2606 2629 zonecfg_fini_handle(handle);
2607 2630 return (Z_ERR);
2608 2631 }
2609 2632 while (zonecfg_getnwifent(handle, &nwiftab) == Z_OK) {
2610 2633 struct lifreq lifr;
2611 2634 sa_family_t af = AF_UNSPEC;
2612 2635 char dl_owner_zname[ZONENAME_MAX];
2613 2636 zoneid_t dl_owner_zid;
2614 2637 zoneid_t target_zid;
2615 2638 int res;
2616 2639
2617 2640 /* skip any loopback interfaces */
2618 2641 if (strcmp(nwiftab.zone_nwif_physical, "lo0") == 0)
2619 2642 continue;
2620 2643 switch (iptype) {
2621 2644 case ZS_SHARED:
2622 2645 if ((res = zonecfg_valid_net_address(
2623 2646 nwiftab.zone_nwif_address, &lifr)) != Z_OK) {
2624 2647 print_net_err(nwiftab.zone_nwif_physical,
2625 2648 nwiftab.zone_nwif_address, af,
2626 2649 zonecfg_strerror(res));
2627 2650 return_code = Z_ERR;
2628 2651 continue;
2629 2652 }
2630 2653 af = lifr.lifr_addr.ss_family;
2631 2654 if (!zonecfg_ifname_exists(af,
2632 2655 nwiftab.zone_nwif_physical)) {
2633 2656 /*
2634 2657 * The interface failed to come up. We continue
2635 2658 * on anyway for the sake of consistency: a
2636 2659 * zone is not shut down if the interface fails
2637 2660 * any time after boot, nor does the global zone
2638 2661 * fail to boot if an interface fails.
2639 2662 */
2640 2663 (void) fprintf(stderr,
2641 2664 gettext("WARNING: skipping network "
2642 2665 "interface '%s' which may not be "
2643 2666 "present/plumbed in the global "
2644 2667 "zone.\n"),
2645 2668 nwiftab.zone_nwif_physical);
2646 2669 }
2647 2670 break;
2648 2671 case ZS_EXCLUSIVE:
2649 2672 /* Warning if it exists for either IPv4 or IPv6 */
2650 2673
2651 2674 if (zonecfg_ifname_exists(AF_INET,
2652 2675 nwiftab.zone_nwif_physical) ||
2653 2676 zonecfg_ifname_exists(AF_INET6,
2654 2677 nwiftab.zone_nwif_physical)) {
2655 2678 (void) fprintf(stderr,
2656 2679 gettext("WARNING: skipping network "
2657 2680 "interface '%s' which is used in the "
2658 2681 "global zone.\n"),
2659 2682 nwiftab.zone_nwif_physical);
2660 2683 break;
2661 2684 }
2662 2685
2663 2686 /*
2664 2687 * Verify that the datalink exists and that it isn't
2665 2688 * already assigned to a zone.
2666 2689 */
2667 2690 if ((status = dladm_open(&dh)) == DLADM_STATUS_OK) {
2668 2691 status = dladm_name2info(dh,
2669 2692 nwiftab.zone_nwif_physical, &linkid, NULL,
2670 2693 NULL, NULL);
2671 2694 dladm_close(dh);
2672 2695 }
2673 2696 if (status != DLADM_STATUS_OK) {
2674 2697 (void) fprintf(stderr,
2675 2698 gettext("WARNING: skipping network "
2676 2699 "interface '%s': %s\n"),
2677 2700 nwiftab.zone_nwif_physical,
2678 2701 dladm_status2str(status, errmsg));
2679 2702 break;
2680 2703 }
2681 2704 dl_owner_zid = ALL_ZONES;
2682 2705 if (zone_check_datalink(&dl_owner_zid, linkid) != 0)
2683 2706 break;
2684 2707
2685 2708 /*
2686 2709 * If the zone being verified is
2687 2710 * running and owns the interface
2688 2711 */
2689 2712 target_zid = getzoneidbyname(target_zone);
2690 2713 if (target_zid == dl_owner_zid)
2691 2714 break;
2692 2715
2693 2716 /* Zone id match failed, use name to check */
2694 2717 if (getzonenamebyid(dl_owner_zid, dl_owner_zname,
2695 2718 ZONENAME_MAX) < 0) {
2696 2719 /* No name, show ID instead */
2697 2720 (void) snprintf(dl_owner_zname, ZONENAME_MAX,
2698 2721 "<%d>", dl_owner_zid);
2699 2722 } else if (strcmp(dl_owner_zname, target_zone) == 0)
2700 2723 break;
2701 2724
2702 2725 /*
2703 2726 * Note here we only report a warning that
2704 2727 * the interface is already in use by another
2705 2728 * running zone, and the verify process just
2706 2729 * goes on, if the interface is still in use
2707 2730 * when this zone really boots up, zoneadmd
2708 2731 * will find it. If the name of the zone which
2709 2732 * owns this interface cannot be determined,
2710 2733 * then it is not possible to determine if there
2711 2734 * is a conflict so just report it as a warning.
2712 2735 */
2713 2736 (void) fprintf(stderr,
2714 2737 gettext("WARNING: skipping network interface "
2715 2738 "'%s' which is used by the non-global zone "
2716 2739 "'%s'.\n"), nwiftab.zone_nwif_physical,
2717 2740 dl_owner_zname);
2718 2741 break;
2719 2742 }
2720 2743 }
2721 2744 (void) zonecfg_endnwifent(handle);
2722 2745 no_net:
2723 2746
2724 2747 /* verify that lofs has not been excluded from the kernel */
2725 2748 if (!(cmd_num == CMD_DETACH || cmd_num == CMD_ATTACH ||
2726 2749 cmd_num == CMD_MOVE || cmd_num == CMD_CLONE) &&
2727 2750 modctl(MODLOAD, 1, "fs/lofs", NULL) != 0) {
2728 2751 if (errno == ENXIO)
2729 2752 (void) fprintf(stderr, gettext("could not verify "
2730 2753 "lofs(7FS): possibly excluded in /etc/system\n"));
2731 2754 else
2732 2755 (void) fprintf(stderr, gettext("could not verify "
2733 2756 "lofs(7FS): %s\n"), strerror(errno));
2734 2757 return_code = Z_ERR;
2735 2758 }
2736 2759
2737 2760 if (verify_filesystems(handle) != Z_OK)
2738 2761 return_code = Z_ERR;
2739 2762 if (!in_alt_root && verify_rctls(handle) != Z_OK)
2740 2763 return_code = Z_ERR;
2741 2764 if (!in_alt_root && verify_pool(handle) != Z_OK)
2742 2765 return_code = Z_ERR;
2743 2766 if (!in_alt_root && verify_brand(handle, cmd_num, argv) != Z_OK)
2744 2767 return_code = Z_ERR;
2745 2768 if (!in_alt_root && verify_datasets(handle) != Z_OK)
2746 2769 return_code = Z_ERR;
2747 2770
2748 2771 /*
2749 2772 * As the "mount" command is used for patching/upgrading of zones
2750 2773 * or other maintenance processes, the zone's privilege set is not
2751 2774 * checked in this case. Instead, the default, safe set of
2752 2775 * privileges will be used when this zone is created in the
2753 2776 * kernel.
2754 2777 */
2755 2778 if (!in_alt_root && cmd_num != CMD_MOUNT &&
2756 2779 verify_limitpriv(handle) != Z_OK)
2757 2780 return_code = Z_ERR;
2758 2781
2759 2782 return (return_code);
2760 2783 }
2761 2784
2762 2785 static int
2763 2786 verify_details(int cmd_num, char *argv[])
2764 2787 {
2765 2788 zone_dochandle_t handle;
2766 2789 char zonepath[MAXPATHLEN], checkpath[MAXPATHLEN];
2767 2790 int return_code = Z_OK;
2768 2791 int err;
2769 2792
2770 2793 if ((handle = zonecfg_init_handle()) == NULL) {
2771 2794 zperror(cmd_to_str(cmd_num), B_TRUE);
2772 2795 return (Z_ERR);
2773 2796 }
2774 2797 if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) {
2775 2798 errno = err;
2776 2799 zperror(cmd_to_str(cmd_num), B_TRUE);
2777 2800 zonecfg_fini_handle(handle);
2778 2801 return (Z_ERR);
2779 2802 }
2780 2803 if ((err = zonecfg_get_zonepath(handle, zonepath, sizeof (zonepath))) !=
2781 2804 Z_OK) {
2782 2805 errno = err;
2783 2806 zperror(cmd_to_str(cmd_num), B_TRUE);
2784 2807 zonecfg_fini_handle(handle);
2785 2808 return (Z_ERR);
2786 2809 }
2787 2810 /*
2788 2811 * zonecfg_get_zonepath() gets its data from the XML repository.
2789 2812 * Verify this against the index file, which is checked first by
2790 2813 * zone_get_zonepath(). If they don't match, bail out.
2791 2814 */
2792 2815 if ((err = zone_get_zonepath(target_zone, checkpath,
2793 2816 sizeof (checkpath))) != Z_OK) {
2794 2817 errno = err;
2795 2818 zperror2(target_zone, gettext("could not get zone path"));
2796 2819 zonecfg_fini_handle(handle);
2797 2820 return (Z_ERR);
2798 2821 }
2799 2822 if (strcmp(zonepath, checkpath) != 0) {
2800 2823 /*
2801 2824 * TRANSLATION_NOTE
2802 2825 * XML and zonepath are literals that should not be translated.
2803 2826 */
2804 2827 (void) fprintf(stderr, gettext("The XML repository has "
2805 2828 "zonepath '%s',\nbut the index file has zonepath '%s'.\n"
2806 2829 "These must match, so fix the incorrect entry.\n"),
2807 2830 zonepath, checkpath);
2808 2831 zonecfg_fini_handle(handle);
2809 2832 return (Z_ERR);
2810 2833 }
2811 2834 if (cmd_num != CMD_ATTACH &&
2812 2835 validate_zonepath(zonepath, cmd_num) != Z_OK) {
2813 2836 (void) fprintf(stderr, gettext("could not verify zonepath %s "
2814 2837 "because of the above errors.\n"), zonepath);
2815 2838 return_code = Z_ERR;
2816 2839 }
2817 2840
2818 2841 if (verify_handle(cmd_num, handle, argv) != Z_OK)
2819 2842 return_code = Z_ERR;
2820 2843
2821 2844 zonecfg_fini_handle(handle);
2822 2845 if (return_code == Z_ERR)
2823 2846 (void) fprintf(stderr,
2824 2847 gettext("%s: zone %s failed to verify\n"),
2825 2848 execname, target_zone);
2826 2849 return (return_code);
2827 2850 }
2828 2851
2829 2852 static int
2830 2853 verify_func(int argc, char *argv[])
2831 2854 {
2832 2855 int arg;
2833 2856
2834 2857 optind = 0;
2835 2858 if ((arg = getopt(argc, argv, "?")) != EOF) {
2836 2859 switch (arg) {
2837 2860 case '?':
2838 2861 sub_usage(SHELP_VERIFY, CMD_VERIFY);
2839 2862 return (optopt == '?' ? Z_OK : Z_USAGE);
2840 2863 default:
2841 2864 sub_usage(SHELP_VERIFY, CMD_VERIFY);
2842 2865 return (Z_USAGE);
2843 2866 }
2844 2867 }
2845 2868 if (argc > optind) {
2846 2869 sub_usage(SHELP_VERIFY, CMD_VERIFY);
2847 2870 return (Z_USAGE);
2848 2871 }
2849 2872 if (sanity_check(target_zone, CMD_VERIFY, B_FALSE, B_FALSE, B_FALSE)
2850 2873 != Z_OK)
2851 2874 return (Z_ERR);
2852 2875 return (verify_details(CMD_VERIFY, argv));
2853 2876 }
2854 2877
2855 2878 static int
2856 2879 addoptions(char *buf, char *argv[], size_t len)
2857 2880 {
2858 2881 int i = 0;
2859 2882
2860 2883 if (buf[0] == '\0')
2861 2884 return (Z_OK);
2862 2885
2863 2886 while (argv[i] != NULL) {
2864 2887 if (strlcat(buf, " ", len) >= len ||
2865 2888 strlcat(buf, argv[i++], len) >= len) {
2866 2889 zerror("Command line too long");
2867 2890 return (Z_ERR);
2868 2891 }
2869 2892 }
2870 2893
2871 2894 return (Z_OK);
2872 2895 }
2873 2896
2874 2897 static int
2875 2898 addopt(char *buf, int opt, char *optarg, size_t bufsize)
2876 2899 {
2877 2900 char optstring[4];
2878 2901
2879 2902 if (opt > 0)
2880 2903 (void) sprintf(optstring, " -%c", opt);
2881 2904 else
2882 2905 (void) strcpy(optstring, " ");
2883 2906
2884 2907 if ((strlcat(buf, optstring, bufsize) > bufsize))
2885 2908 return (Z_ERR);
2886 2909
2887 2910 if ((optarg != NULL) && (strlcat(buf, optarg, bufsize) > bufsize))
2888 2911 return (Z_ERR);
2889 2912
2890 2913 return (Z_OK);
2891 2914 }
2892 2915
2893 2916 /* ARGSUSED */
2894 2917 static int
2895 2918 install_func(int argc, char *argv[])
2896 2919 {
2897 2920 char cmdbuf[MAXPATHLEN];
2898 2921 char postcmdbuf[MAXPATHLEN];
2899 2922 int lockfd;
2900 2923 int arg, err, subproc_err;
2901 2924 char zonepath[MAXPATHLEN];
2902 2925 brand_handle_t bh = NULL;
2903 2926 int status;
2904 2927 boolean_t do_postinstall = B_FALSE;
2905 2928 boolean_t brand_help = B_FALSE;
2906 2929 char opts[128];
2907 2930
2908 2931 if (target_zone == NULL) {
2909 2932 sub_usage(SHELP_INSTALL, CMD_INSTALL);
2910 2933 return (Z_USAGE);
2911 2934 }
2912 2935
2913 2936 if (zonecfg_in_alt_root()) {
2914 2937 zerror(gettext("cannot install zone in alternate root"));
2915 2938 return (Z_ERR);
2916 2939 }
2917 2940
2918 2941 if ((err = zone_get_zonepath(target_zone, zonepath,
2919 2942 sizeof (zonepath))) != Z_OK) {
2920 2943 errno = err;
2921 2944 zperror2(target_zone, gettext("could not get zone path"));
2922 2945 return (Z_ERR);
2923 2946 }
2924 2947
2925 2948 /* Fetch the install command from the brand configuration. */
2926 2949 if ((bh = brand_open(target_brand)) == NULL) {
2927 2950 zerror(gettext("missing or invalid brand"));
2928 2951 return (Z_ERR);
2929 2952 }
2930 2953
2931 2954 if (get_hook(bh, cmdbuf, sizeof (cmdbuf), brand_get_install,
2932 2955 target_zone, zonepath) != Z_OK) {
2933 2956 zerror("invalid brand configuration: missing install resource");
2934 2957 brand_close(bh);
2935 2958 return (Z_ERR);
2936 2959 }
2937 2960
2938 2961 if (get_hook(bh, postcmdbuf, sizeof (postcmdbuf), brand_get_postinstall,
2939 2962 target_zone, zonepath) != Z_OK) {
2940 2963 zerror("invalid brand configuration: missing postinstall "
2941 2964 "resource");
2942 2965 brand_close(bh);
2943 2966 return (Z_ERR);
2944 2967 }
2945 2968
2946 2969 if (postcmdbuf[0] != '\0')
2947 2970 do_postinstall = B_TRUE;
2948 2971
2949 2972 (void) strcpy(opts, "?x:");
2950 2973 /*
2951 2974 * Fetch the list of recognized command-line options from
2952 2975 * the brand configuration file.
2953 2976 */
2954 2977 if (brand_get_installopts(bh, opts + strlen(opts),
2955 2978 sizeof (opts) - strlen(opts)) != 0) {
2956 2979 zerror("invalid brand configuration: missing "
2957 2980 "install options resource");
2958 2981 brand_close(bh);
2959 2982 return (Z_ERR);
2960 2983 }
2961 2984
2962 2985 brand_close(bh);
2963 2986
2964 2987 if (cmdbuf[0] == '\0') {
2965 2988 zerror("Missing brand install command");
2966 2989 return (Z_ERR);
2967 2990 }
2968 2991
2969 2992 /* Check the argv string for args we handle internally */
2970 2993 optind = 0;
2971 2994 opterr = 0;
2972 2995 while ((arg = getopt(argc, argv, opts)) != EOF) {
2973 2996 switch (arg) {
2974 2997 case '?':
2975 2998 if (optopt == '?') {
2976 2999 sub_usage(SHELP_INSTALL, CMD_INSTALL);
2977 3000 brand_help = B_TRUE;
2978 3001 }
2979 3002 /* Ignore unknown options - may be brand specific. */
2980 3003 break;
2981 3004 default:
2982 3005 /* Ignore unknown options - may be brand specific. */
2983 3006 break;
2984 3007 }
2985 3008
2986 3009 /*
2987 3010 * Append the option to the command line passed to the
2988 3011 * brand-specific install and postinstall routines.
2989 3012 */
2990 3013 if (addopt(cmdbuf, optopt, optarg, sizeof (cmdbuf)) != Z_OK) {
2991 3014 zerror("Install command line too long");
2992 3015 return (Z_ERR);
2993 3016 }
2994 3017 if (addopt(postcmdbuf, optopt, optarg, sizeof (postcmdbuf))
2995 3018 != Z_OK) {
2996 3019 zerror("Post-Install command line too long");
2997 3020 return (Z_ERR);
2998 3021 }
2999 3022 }
3000 3023
3001 3024 for (; optind < argc; optind++) {
3002 3025 if (addopt(cmdbuf, 0, argv[optind], sizeof (cmdbuf)) != Z_OK) {
3003 3026 zerror("Install command line too long");
3004 3027 return (Z_ERR);
3005 3028 }
3006 3029
3007 3030 if (addopt(postcmdbuf, 0, argv[optind], sizeof (postcmdbuf))
3008 3031 != Z_OK) {
3009 3032 zerror("Post-Install command line too long");
3010 3033 return (Z_ERR);
3011 3034 }
3012 3035 }
3013 3036
3014 3037 if (!brand_help) {
3015 3038 if (sanity_check(target_zone, CMD_INSTALL, B_FALSE, B_TRUE,
3016 3039 B_FALSE) != Z_OK)
3017 3040 return (Z_ERR);
3018 3041 if (verify_details(CMD_INSTALL, argv) != Z_OK)
3019 3042 return (Z_ERR);
3020 3043
3021 3044 if (zonecfg_grab_lock_file(target_zone, &lockfd) != Z_OK) {
3022 3045 zerror(gettext("another %s may have an operation in "
3023 3046 "progress."), "zoneadm");
3024 3047 return (Z_ERR);
3025 3048 }
3026 3049 err = zone_set_state(target_zone, ZONE_STATE_INCOMPLETE);
3027 3050 if (err != Z_OK) {
3028 3051 errno = err;
3029 3052 zperror2(target_zone, gettext("could not set state"));
3030 3053 goto done;
3031 3054 }
3032 3055
3033 3056 create_zfs_zonepath(zonepath);
3034 3057 }
3035 3058
3036 3059 status = do_subproc(cmdbuf);
3037 3060 if ((subproc_err =
3038 3061 subproc_status(gettext("brand-specific installation"), status,
3039 3062 B_FALSE)) != ZONE_SUBPROC_OK) {
3040 3063 if (subproc_err == ZONE_SUBPROC_USAGE && !brand_help) {
3041 3064 sub_usage(SHELP_INSTALL, CMD_INSTALL);
3042 3065 zonecfg_release_lock_file(target_zone, lockfd);
3043 3066 return (Z_ERR);
3044 3067 }
3045 3068 errno = subproc_err;
3046 3069 err = Z_ERR;
3047 3070 goto done;
3048 3071 }
3049 3072
3050 3073 if (brand_help)
3051 3074 return (Z_OK);
3052 3075
3053 3076 if ((err = zone_set_state(target_zone, ZONE_STATE_INSTALLED)) != Z_OK) {
3054 3077 errno = err;
3055 3078 zperror2(target_zone, gettext("could not set state"));
3056 3079 goto done;
3057 3080 }
3058 3081
3059 3082 if (do_postinstall) {
3060 3083 status = do_subproc(postcmdbuf);
3061 3084
3062 3085 if ((subproc_err =
3063 3086 subproc_status(gettext("brand-specific post-install"),
3064 3087 status, B_FALSE)) != ZONE_SUBPROC_OK) {
3065 3088 errno = subproc_err;
3066 3089 err = Z_ERR;
3067 3090 (void) zone_set_state(target_zone,
3068 3091 ZONE_STATE_INCOMPLETE);
3069 3092 }
3070 3093 }
3071 3094
3072 3095 done:
3073 3096 /*
3074 3097 * If the install script exited with ZONE_SUBPROC_NOTCOMPLETE, try to
3075 3098 * clean up the zone and leave the zone in the CONFIGURED state so that
3076 3099 * another install can be attempted without requiring an uninstall
3077 3100 * first.
3078 3101 */
3079 3102 if (subproc_err == ZONE_SUBPROC_NOTCOMPLETE) {
3080 3103 int temp_err;
3081 3104
3082 3105 if ((temp_err = cleanup_zonepath(zonepath, B_FALSE)) != Z_OK) {
3083 3106 errno = err = temp_err;
3084 3107 zperror2(target_zone,
3085 3108 gettext("cleaning up zonepath failed"));
3086 3109 } else if ((temp_err = zone_set_state(target_zone,
3087 3110 ZONE_STATE_CONFIGURED)) != Z_OK) {
3088 3111 errno = err = temp_err;
3089 3112 zperror2(target_zone, gettext("could not set state"));
3090 3113 }
3091 3114 }
3092 3115
3093 3116 if (!brand_help)
3094 3117 zonecfg_release_lock_file(target_zone, lockfd);
3095 3118 return ((err == Z_OK) ? Z_OK : Z_ERR);
3096 3119 }
3097 3120
3098 3121 static void
3099 3122 warn_dev_match(zone_dochandle_t s_handle, char *source_zone,
3100 3123 zone_dochandle_t t_handle, char *target_zone)
3101 3124 {
3102 3125 int err;
3103 3126 struct zone_devtab s_devtab;
3104 3127 struct zone_devtab t_devtab;
3105 3128
3106 3129 if ((err = zonecfg_setdevent(t_handle)) != Z_OK) {
3107 3130 errno = err;
3108 3131 zperror2(target_zone, gettext("could not enumerate devices"));
3109 3132 return;
3110 3133 }
3111 3134
3112 3135 while (zonecfg_getdevent(t_handle, &t_devtab) == Z_OK) {
3113 3136 if ((err = zonecfg_setdevent(s_handle)) != Z_OK) {
3114 3137 errno = err;
3115 3138 zperror2(source_zone,
3116 3139 gettext("could not enumerate devices"));
3117 3140 (void) zonecfg_enddevent(t_handle);
3118 3141 return;
3119 3142 }
3120 3143
3121 3144 while (zonecfg_getdevent(s_handle, &s_devtab) == Z_OK) {
3122 3145 /*
3123 3146 * Use fnmatch to catch the case where wildcards
3124 3147 * were used in one zone and the other has an
3125 3148 * explicit entry (e.g. /dev/dsk/c0t0d0s6 vs.
3126 3149 * /dev/\*dsk/c0t0d0s6).
3127 3150 */
3128 3151 if (fnmatch(t_devtab.zone_dev_match,
3129 3152 s_devtab.zone_dev_match, FNM_PATHNAME) == 0 ||
3130 3153 fnmatch(s_devtab.zone_dev_match,
3131 3154 t_devtab.zone_dev_match, FNM_PATHNAME) == 0) {
3132 3155 (void) fprintf(stderr,
3133 3156 gettext("WARNING: device '%s' "
3134 3157 "is configured in both zones.\n"),
3135 3158 t_devtab.zone_dev_match);
3136 3159 break;
3137 3160 }
3138 3161 }
3139 3162 (void) zonecfg_enddevent(s_handle);
3140 3163 }
3141 3164
3142 3165 (void) zonecfg_enddevent(t_handle);
3143 3166 }
3144 3167
3145 3168 /*
3146 3169 * Check if the specified mount option (opt) is contained within the
3147 3170 * options string.
3148 3171 */
3149 3172 static boolean_t
3150 3173 opt_match(char *opt, char *options)
3151 3174 {
3152 3175 char *p;
3153 3176 char *lastp;
3154 3177
3155 3178 if ((p = strtok_r(options, ",", &lastp)) != NULL) {
3156 3179 if (strcmp(p, opt) == 0)
3157 3180 return (B_TRUE);
3158 3181 while ((p = strtok_r(NULL, ",", &lastp)) != NULL) {
3159 3182 if (strcmp(p, opt) == 0)
3160 3183 return (B_TRUE);
3161 3184 }
3162 3185 }
3163 3186
3164 3187 return (B_FALSE);
3165 3188 }
3166 3189
3167 3190 #define RW_LOFS "WARNING: read-write lofs file system on '%s' is configured " \
3168 3191 "in both zones.\n"
3169 3192
3170 3193 static void
3171 3194 print_fs_warnings(struct zone_fstab *s_fstab, struct zone_fstab *t_fstab)
3172 3195 {
3173 3196 /*
3174 3197 * It is ok to have shared lofs mounted fs but we want to warn if
3175 3198 * either is rw since this will effect the other zone.
3176 3199 */
3177 3200 if (strcmp(t_fstab->zone_fs_type, "lofs") == 0) {
3178 3201 zone_fsopt_t *optp;
3179 3202
3180 3203 /* The default is rw so no options means rw */
3181 3204 if (t_fstab->zone_fs_options == NULL ||
3182 3205 s_fstab->zone_fs_options == NULL) {
3183 3206 (void) fprintf(stderr, gettext(RW_LOFS),
3184 3207 t_fstab->zone_fs_special);
3185 3208 return;
3186 3209 }
3187 3210
3188 3211 for (optp = s_fstab->zone_fs_options; optp != NULL;
3189 3212 optp = optp->zone_fsopt_next) {
3190 3213 if (opt_match("rw", optp->zone_fsopt_opt)) {
3191 3214 (void) fprintf(stderr, gettext(RW_LOFS),
3192 3215 s_fstab->zone_fs_special);
3193 3216 return;
3194 3217 }
3195 3218 }
3196 3219
3197 3220 for (optp = t_fstab->zone_fs_options; optp != NULL;
3198 3221 optp = optp->zone_fsopt_next) {
3199 3222 if (opt_match("rw", optp->zone_fsopt_opt)) {
3200 3223 (void) fprintf(stderr, gettext(RW_LOFS),
3201 3224 t_fstab->zone_fs_special);
3202 3225 return;
3203 3226 }
3204 3227 }
3205 3228
3206 3229 return;
3207 3230 }
3208 3231
3209 3232 /*
3210 3233 * TRANSLATION_NOTE
3211 3234 * The first variable is the file system type and the second is
3212 3235 * the file system special device. For example,
3213 3236 * WARNING: ufs file system on '/dev/dsk/c0t0d0s0' ...
3214 3237 */
3215 3238 (void) fprintf(stderr, gettext("WARNING: %s file system on '%s' "
3216 3239 "is configured in both zones.\n"), t_fstab->zone_fs_type,
3217 3240 t_fstab->zone_fs_special);
3218 3241 }
3219 3242
3220 3243 static void
3221 3244 warn_fs_match(zone_dochandle_t s_handle, char *source_zone,
3222 3245 zone_dochandle_t t_handle, char *target_zone)
3223 3246 {
3224 3247 int err;
3225 3248 struct zone_fstab s_fstab;
3226 3249 struct zone_fstab t_fstab;
3227 3250
3228 3251 if ((err = zonecfg_setfsent(t_handle)) != Z_OK) {
3229 3252 errno = err;
3230 3253 zperror2(target_zone,
3231 3254 gettext("could not enumerate file systems"));
3232 3255 return;
3233 3256 }
3234 3257
3235 3258 while (zonecfg_getfsent(t_handle, &t_fstab) == Z_OK) {
3236 3259 if ((err = zonecfg_setfsent(s_handle)) != Z_OK) {
3237 3260 errno = err;
3238 3261 zperror2(source_zone,
3239 3262 gettext("could not enumerate file systems"));
3240 3263 (void) zonecfg_endfsent(t_handle);
3241 3264 return;
3242 3265 }
3243 3266
3244 3267 while (zonecfg_getfsent(s_handle, &s_fstab) == Z_OK) {
3245 3268 if (strcmp(t_fstab.zone_fs_special,
3246 3269 s_fstab.zone_fs_special) == 0) {
3247 3270 print_fs_warnings(&s_fstab, &t_fstab);
3248 3271 break;
3249 3272 }
3250 3273 }
3251 3274 (void) zonecfg_endfsent(s_handle);
3252 3275 }
3253 3276
3254 3277 (void) zonecfg_endfsent(t_handle);
3255 3278 }
3256 3279
3257 3280 /*
3258 3281 * We don't catch the case where you used the same IP address but
3259 3282 * it is not an exact string match. For example, 192.9.0.128 vs. 192.09.0.128.
3260 3283 * However, we're not going to worry about that but we will check for
3261 3284 * a possible netmask on one of the addresses (e.g. 10.0.0.1 and 10.0.0.1/24)
3262 3285 * and handle that case as a match.
3263 3286 */
3264 3287 static void
3265 3288 warn_ip_match(zone_dochandle_t s_handle, char *source_zone,
3266 3289 zone_dochandle_t t_handle, char *target_zone)
3267 3290 {
3268 3291 int err;
3269 3292 struct zone_nwiftab s_nwiftab;
3270 3293 struct zone_nwiftab t_nwiftab;
3271 3294
3272 3295 if ((err = zonecfg_setnwifent(t_handle)) != Z_OK) {
3273 3296 errno = err;
3274 3297 zperror2(target_zone,
3275 3298 gettext("could not enumerate network interfaces"));
3276 3299 return;
3277 3300 }
3278 3301
3279 3302 while (zonecfg_getnwifent(t_handle, &t_nwiftab) == Z_OK) {
3280 3303 char *p;
3281 3304
3282 3305 /* remove an (optional) netmask from the address */
3283 3306 if ((p = strchr(t_nwiftab.zone_nwif_address, '/')) != NULL)
3284 3307 *p = '\0';
3285 3308
3286 3309 if ((err = zonecfg_setnwifent(s_handle)) != Z_OK) {
3287 3310 errno = err;
3288 3311 zperror2(source_zone,
3289 3312 gettext("could not enumerate network interfaces"));
3290 3313 (void) zonecfg_endnwifent(t_handle);
3291 3314 return;
3292 3315 }
3293 3316
3294 3317 while (zonecfg_getnwifent(s_handle, &s_nwiftab) == Z_OK) {
3295 3318 /* remove an (optional) netmask from the address */
3296 3319 if ((p = strchr(s_nwiftab.zone_nwif_address, '/'))
3297 3320 != NULL)
3298 3321 *p = '\0';
3299 3322
3300 3323 /* For exclusive-IP zones, address is not specified. */
3301 3324 if (strlen(s_nwiftab.zone_nwif_address) == 0)
3302 3325 continue;
3303 3326
3304 3327 if (strcmp(t_nwiftab.zone_nwif_address,
3305 3328 s_nwiftab.zone_nwif_address) == 0) {
3306 3329 (void) fprintf(stderr,
3307 3330 gettext("WARNING: network address '%s' "
3308 3331 "is configured in both zones.\n"),
3309 3332 t_nwiftab.zone_nwif_address);
3310 3333 break;
3311 3334 }
3312 3335 }
3313 3336 (void) zonecfg_endnwifent(s_handle);
3314 3337 }
3315 3338
3316 3339 (void) zonecfg_endnwifent(t_handle);
3317 3340 }
3318 3341
3319 3342 static void
3320 3343 warn_dataset_match(zone_dochandle_t s_handle, char *source,
3321 3344 zone_dochandle_t t_handle, char *target)
3322 3345 {
3323 3346 int err;
3324 3347 struct zone_dstab s_dstab;
3325 3348 struct zone_dstab t_dstab;
3326 3349
3327 3350 if ((err = zonecfg_setdsent(t_handle)) != Z_OK) {
3328 3351 errno = err;
3329 3352 zperror2(target, gettext("could not enumerate datasets"));
3330 3353 return;
3331 3354 }
3332 3355
3333 3356 while (zonecfg_getdsent(t_handle, &t_dstab) == Z_OK) {
3334 3357 if ((err = zonecfg_setdsent(s_handle)) != Z_OK) {
3335 3358 errno = err;
3336 3359 zperror2(source,
3337 3360 gettext("could not enumerate datasets"));
3338 3361 (void) zonecfg_enddsent(t_handle);
3339 3362 return;
3340 3363 }
3341 3364
3342 3365 while (zonecfg_getdsent(s_handle, &s_dstab) == Z_OK) {
3343 3366 if (strcmp(t_dstab.zone_dataset_name,
3344 3367 s_dstab.zone_dataset_name) == 0) {
3345 3368 target_zone = source;
3346 3369 zerror(gettext("WARNING: dataset '%s' "
3347 3370 "is configured in both zones.\n"),
3348 3371 t_dstab.zone_dataset_name);
3349 3372 break;
3350 3373 }
3351 3374 }
3352 3375 (void) zonecfg_enddsent(s_handle);
3353 3376 }
3354 3377
3355 3378 (void) zonecfg_enddsent(t_handle);
3356 3379 }
3357 3380
3358 3381 /*
3359 3382 * Check that the clone and its source have the same brand type.
3360 3383 */
3361 3384 static int
3362 3385 valid_brand_clone(char *source_zone, char *target_zone)
3363 3386 {
3364 3387 brand_handle_t bh;
3365 3388 char source_brand[MAXNAMELEN];
3366 3389
3367 3390 if ((zone_get_brand(source_zone, source_brand,
3368 3391 sizeof (source_brand))) != Z_OK) {
3369 3392 (void) fprintf(stderr, "%s: zone '%s': %s\n",
3370 3393 execname, source_zone, gettext("missing or invalid brand"));
3371 3394 return (Z_ERR);
3372 3395 }
3373 3396
3374 3397 if (strcmp(source_brand, target_brand) != 0) {
3375 3398 (void) fprintf(stderr,
3376 3399 gettext("%s: Zones '%s' and '%s' have different brand "
3377 3400 "types.\n"), execname, source_zone, target_zone);
3378 3401 return (Z_ERR);
3379 3402 }
3380 3403
3381 3404 if ((bh = brand_open(target_brand)) == NULL) {
3382 3405 zerror(gettext("missing or invalid brand"));
3383 3406 return (Z_ERR);
3384 3407 }
3385 3408 brand_close(bh);
3386 3409 return (Z_OK);
3387 3410 }
3388 3411
3389 3412 static int
3390 3413 validate_clone(char *source_zone, char *target_zone)
3391 3414 {
3392 3415 int err = Z_OK;
3393 3416 zone_dochandle_t s_handle;
3394 3417 zone_dochandle_t t_handle;
3395 3418
3396 3419 if ((t_handle = zonecfg_init_handle()) == NULL) {
3397 3420 zperror(cmd_to_str(CMD_CLONE), B_TRUE);
3398 3421 return (Z_ERR);
3399 3422 }
3400 3423 if ((err = zonecfg_get_handle(target_zone, t_handle)) != Z_OK) {
3401 3424 errno = err;
3402 3425 zperror(cmd_to_str(CMD_CLONE), B_TRUE);
3403 3426 zonecfg_fini_handle(t_handle);
3404 3427 return (Z_ERR);
3405 3428 }
3406 3429
3407 3430 if ((s_handle = zonecfg_init_handle()) == NULL) {
3408 3431 zperror(cmd_to_str(CMD_CLONE), B_TRUE);
3409 3432 zonecfg_fini_handle(t_handle);
3410 3433 return (Z_ERR);
3411 3434 }
3412 3435 if ((err = zonecfg_get_handle(source_zone, s_handle)) != Z_OK) {
3413 3436 errno = err;
3414 3437 zperror(cmd_to_str(CMD_CLONE), B_TRUE);
3415 3438 goto done;
3416 3439 }
3417 3440
3418 3441 /* verify new zone has same brand type */
3419 3442 err = valid_brand_clone(source_zone, target_zone);
3420 3443 if (err != Z_OK)
3421 3444 goto done;
3422 3445
3423 3446 /* warn about imported fs's which are the same */
3424 3447 warn_fs_match(s_handle, source_zone, t_handle, target_zone);
3425 3448
3426 3449 /* warn about imported IP addresses which are the same */
3427 3450 warn_ip_match(s_handle, source_zone, t_handle, target_zone);
3428 3451
3429 3452 /* warn about imported devices which are the same */
3430 3453 warn_dev_match(s_handle, source_zone, t_handle, target_zone);
3431 3454
3432 3455 /* warn about imported datasets which are the same */
3433 3456 warn_dataset_match(s_handle, source_zone, t_handle, target_zone);
3434 3457
3435 3458 done:
3436 3459 zonecfg_fini_handle(t_handle);
3437 3460 zonecfg_fini_handle(s_handle);
3438 3461
3439 3462 return ((err == Z_OK) ? Z_OK : Z_ERR);
3440 3463 }
3441 3464
3442 3465 static int
3443 3466 copy_zone(char *src, char *dst)
3444 3467 {
3445 3468 boolean_t out_null = B_FALSE;
3446 3469 int status;
3447 3470 char *outfile;
3448 3471 char cmdbuf[MAXPATHLEN * 2 + 128];
3449 3472
3450 3473 if ((outfile = tempnam("/var/log", "zone")) == NULL) {
3451 3474 outfile = "/dev/null";
3452 3475 out_null = B_TRUE;
3453 3476 }
3454 3477
3455 3478 /*
3456 3479 * Use find to get the list of files to copy. We need to skip
3457 3480 * files of type "socket" since cpio can't handle those but that
3458 3481 * should be ok since the app will recreate the socket when it runs.
3459 3482 * We also need to filter out anything under the .zfs subdir. Since
3460 3483 * find is running depth-first, we need the extra egrep to filter .zfs.
3461 3484 */
3462 3485 (void) snprintf(cmdbuf, sizeof (cmdbuf),
3463 3486 "cd %s && /usr/bin/find . -type s -prune -o -depth -print | "
3464 3487 "/usr/bin/egrep -v '^\\./\\.zfs$|^\\./\\.zfs/' | "
3465 3488 "/usr/bin/cpio -pdmuP@ %s > %s 2>&1",
3466 3489 src, dst, outfile);
3467 3490
3468 3491 status = do_subproc(cmdbuf);
3469 3492
3470 3493 if (subproc_status("copy", status, B_TRUE) != ZONE_SUBPROC_OK) {
3471 3494 if (!out_null)
3472 3495 (void) fprintf(stderr, gettext("\nThe copy failed.\n"
3473 3496 "More information can be found in %s\n"), outfile);
3474 3497 return (Z_ERR);
3475 3498 }
3476 3499
3477 3500 if (!out_null)
3478 3501 (void) unlink(outfile);
3479 3502
3480 3503 return (Z_OK);
3481 3504 }
3482 3505
3483 3506 /* ARGSUSED */
3484 3507 int
3485 3508 zfm_print(const struct mnttab *p, void *r) {
3486 3509 zerror(" %s\n", p->mnt_mountp);
3487 3510 return (0);
3488 3511 }
3489 3512
3490 3513 int
3491 3514 clone_copy(char *source_zonepath, char *zonepath)
3492 3515 {
3493 3516 int err;
3494 3517
3495 3518 /* Don't clone the zone if anything is still mounted there */
3496 3519 if (zonecfg_find_mounts(source_zonepath, NULL, NULL)) {
3497 3520 zerror(gettext("These file systems are mounted on "
3498 3521 "subdirectories of %s.\n"), source_zonepath);
3499 3522 (void) zonecfg_find_mounts(source_zonepath, zfm_print, NULL);
3500 3523 return (Z_ERR);
3501 3524 }
3502 3525
3503 3526 /*
3504 3527 * Attempt to create a ZFS fs for the zonepath. As usual, we don't
3505 3528 * care if this works or not since we always have the default behavior
3506 3529 * of a simple directory for the zonepath.
3507 3530 */
3508 3531 create_zfs_zonepath(zonepath);
3509 3532
3510 3533 (void) printf(gettext("Copying %s..."), source_zonepath);
3511 3534 (void) fflush(stdout);
3512 3535
3513 3536 err = copy_zone(source_zonepath, zonepath);
3514 3537
3515 3538 (void) printf("\n");
3516 3539
3517 3540 return (err);
3518 3541 }
3519 3542
3520 3543 static int
3521 3544 clone_func(int argc, char *argv[])
3522 3545 {
3523 3546 char *source_zone = NULL;
3524 3547 int lockfd;
3525 3548 int err, arg;
3526 3549 char zonepath[MAXPATHLEN];
3527 3550 char source_zonepath[MAXPATHLEN];
3528 3551 zone_state_t state;
3529 3552 zone_entry_t *zent;
3530 3553 char *method = NULL;
3531 3554 char *snapshot = NULL;
3532 3555 char cmdbuf[MAXPATHLEN];
3533 3556 char postcmdbuf[MAXPATHLEN];
3534 3557 char presnapbuf[MAXPATHLEN];
3535 3558 char postsnapbuf[MAXPATHLEN];
3536 3559 char validsnapbuf[MAXPATHLEN];
3537 3560 brand_handle_t bh = NULL;
3538 3561 int status;
3539 3562 boolean_t brand_help = B_FALSE;
3540 3563
3541 3564 if (zonecfg_in_alt_root()) {
3542 3565 zerror(gettext("cannot clone zone in alternate root"));
3543 3566 return (Z_ERR);
3544 3567 }
3545 3568
3546 3569 /* Check the argv string for args we handle internally */
3547 3570 optind = 0;
3548 3571 opterr = 0;
3549 3572 while ((arg = getopt(argc, argv, "?m:s:")) != EOF) {
3550 3573 switch (arg) {
3551 3574 case '?':
3552 3575 if (optopt == '?') {
3553 3576 sub_usage(SHELP_CLONE, CMD_CLONE);
3554 3577 brand_help = B_TRUE;
3555 3578 }
3556 3579 /* Ignore unknown options - may be brand specific. */
3557 3580 break;
3558 3581 case 'm':
3559 3582 method = optarg;
3560 3583 break;
3561 3584 case 's':
3562 3585 snapshot = optarg;
3563 3586 break;
3564 3587 default:
3565 3588 /* Ignore unknown options - may be brand specific. */
3566 3589 break;
3567 3590 }
3568 3591 }
3569 3592
3570 3593 if (argc != (optind + 1)) {
3571 3594 sub_usage(SHELP_CLONE, CMD_CLONE);
3572 3595 return (Z_USAGE);
3573 3596 }
3574 3597
3575 3598 source_zone = argv[optind];
3576 3599
3577 3600 if (!brand_help) {
3578 3601 if (sanity_check(target_zone, CMD_CLONE, B_FALSE, B_TRUE,
3579 3602 B_FALSE) != Z_OK)
3580 3603 return (Z_ERR);
3581 3604 if (verify_details(CMD_CLONE, argv) != Z_OK)
3582 3605 return (Z_ERR);
3583 3606
3584 3607 /*
3585 3608 * We also need to do some extra validation on the source zone.
3586 3609 */
3587 3610 if (strcmp(source_zone, GLOBAL_ZONENAME) == 0) {
3588 3611 zerror(gettext("%s operation is invalid for the "
3589 3612 "global zone."), cmd_to_str(CMD_CLONE));
3590 3613 return (Z_ERR);
3591 3614 }
3592 3615
3593 3616 if (strncmp(source_zone, "SUNW", 4) == 0) {
3594 3617 zerror(gettext("%s operation is invalid for zones "
3595 3618 "starting with SUNW."), cmd_to_str(CMD_CLONE));
3596 3619 return (Z_ERR);
3597 3620 }
3598 3621
3599 3622 if (auth_check(username, source_zone, SOURCE_ZONE) == Z_ERR) {
3600 3623 zerror(gettext("%s operation is invalid because "
3601 3624 "user is not authorized to read source zone."),
3602 3625 cmd_to_str(CMD_CLONE));
3603 3626 return (Z_ERR);
3604 3627 }
3605 3628
3606 3629 zent = lookup_running_zone(source_zone);
3607 3630 if (zent != NULL) {
3608 3631 /* check whether the zone is ready or running */
3609 3632 if ((err = zone_get_state(zent->zname,
3610 3633 &zent->zstate_num)) != Z_OK) {
3611 3634 errno = err;
3612 3635 zperror2(zent->zname, gettext("could not get "
3613 3636 "state"));
3614 3637 /* can't tell, so hedge */
3615 3638 zent->zstate_str = "ready/running";
3616 3639 } else {
3617 3640 zent->zstate_str =
3618 3641 zone_state_str(zent->zstate_num);
3619 3642 }
3620 3643 zerror(gettext("%s operation is invalid for %s zones."),
3621 3644 cmd_to_str(CMD_CLONE), zent->zstate_str);
3622 3645 return (Z_ERR);
3623 3646 }
3624 3647
3625 3648 if ((err = zone_get_state(source_zone, &state)) != Z_OK) {
3626 3649 errno = err;
3627 3650 zperror2(source_zone, gettext("could not get state"));
3628 3651 return (Z_ERR);
3629 3652 }
3630 3653 if (state != ZONE_STATE_INSTALLED) {
3631 3654 (void) fprintf(stderr,
3632 3655 gettext("%s: zone %s is %s; %s is required.\n"),
3633 3656 execname, source_zone, zone_state_str(state),
3634 3657 zone_state_str(ZONE_STATE_INSTALLED));
3635 3658 return (Z_ERR);
3636 3659 }
3637 3660
3638 3661 /*
3639 3662 * The source zone checks out ok, continue with the clone.
3640 3663 */
3641 3664
3642 3665 if (validate_clone(source_zone, target_zone) != Z_OK)
3643 3666 return (Z_ERR);
3644 3667
3645 3668 if (zonecfg_grab_lock_file(target_zone, &lockfd) != Z_OK) {
3646 3669 zerror(gettext("another %s may have an operation in "
3647 3670 "progress."), "zoneadm");
3648 3671 return (Z_ERR);
3649 3672 }
3650 3673 }
3651 3674
3652 3675 if ((err = zone_get_zonepath(source_zone, source_zonepath,
3653 3676 sizeof (source_zonepath))) != Z_OK) {
3654 3677 errno = err;
3655 3678 zperror2(source_zone, gettext("could not get zone path"));
3656 3679 goto done;
3657 3680 }
3658 3681
3659 3682 if ((err = zone_get_zonepath(target_zone, zonepath, sizeof (zonepath)))
3660 3683 != Z_OK) {
3661 3684 errno = err;
3662 3685 zperror2(target_zone, gettext("could not get zone path"));
3663 3686 goto done;
3664 3687 }
3665 3688
3666 3689 /*
3667 3690 * Fetch the clone and postclone hooks from the brand configuration.
3668 3691 */
3669 3692 if ((bh = brand_open(target_brand)) == NULL) {
3670 3693 zerror(gettext("missing or invalid brand"));
3671 3694 err = Z_ERR;
3672 3695 goto done;
3673 3696 }
3674 3697
3675 3698 if (get_hook(bh, cmdbuf, sizeof (cmdbuf), brand_get_clone, target_zone,
3676 3699 zonepath) != Z_OK) {
3677 3700 zerror("invalid brand configuration: missing clone resource");
3678 3701 brand_close(bh);
3679 3702 err = Z_ERR;
3680 3703 goto done;
3681 3704 }
3682 3705
3683 3706 if (get_hook(bh, postcmdbuf, sizeof (postcmdbuf), brand_get_postclone,
3684 3707 target_zone, zonepath) != Z_OK) {
3685 3708 zerror("invalid brand configuration: missing postclone "
3686 3709 "resource");
3687 3710 brand_close(bh);
3688 3711 err = Z_ERR;
3689 3712 goto done;
3690 3713 }
3691 3714
3692 3715 if (get_hook(bh, presnapbuf, sizeof (presnapbuf), brand_get_presnap,
3693 3716 source_zone, source_zonepath) != Z_OK) {
3694 3717 zerror("invalid brand configuration: missing presnap "
3695 3718 "resource");
3696 3719 brand_close(bh);
3697 3720 err = Z_ERR;
3698 3721 goto done;
3699 3722 }
3700 3723
3701 3724 if (get_hook(bh, postsnapbuf, sizeof (postsnapbuf), brand_get_postsnap,
3702 3725 source_zone, source_zonepath) != Z_OK) {
3703 3726 zerror("invalid brand configuration: missing postsnap "
3704 3727 "resource");
3705 3728 brand_close(bh);
3706 3729 err = Z_ERR;
3707 3730 goto done;
3708 3731 }
3709 3732
3710 3733 if (get_hook(bh, validsnapbuf, sizeof (validsnapbuf),
3711 3734 brand_get_validatesnap, target_zone, zonepath) != Z_OK) {
3712 3735 zerror("invalid brand configuration: missing validatesnap "
3713 3736 "resource");
3714 3737 brand_close(bh);
3715 3738 err = Z_ERR;
3716 3739 goto done;
3717 3740 }
3718 3741 brand_close(bh);
3719 3742
3720 3743 /* Append all options to clone hook. */
3721 3744 if (addoptions(cmdbuf, argv, sizeof (cmdbuf)) != Z_OK) {
3722 3745 err = Z_ERR;
3723 3746 goto done;
3724 3747 }
3725 3748
3726 3749 /* Append all options to postclone hook. */
3727 3750 if (addoptions(postcmdbuf, argv, sizeof (postcmdbuf)) != Z_OK) {
3728 3751 err = Z_ERR;
3729 3752 goto done;
3730 3753 }
3731 3754
3732 3755 if (!brand_help) {
3733 3756 if ((err = zone_set_state(target_zone, ZONE_STATE_INCOMPLETE))
3734 3757 != Z_OK) {
3735 3758 errno = err;
3736 3759 zperror2(target_zone, gettext("could not set state"));
3737 3760 goto done;
3738 3761 }
3739 3762 }
3740 3763
3741 3764 /*
3742 3765 * The clone hook is optional. If it exists, use the hook for
3743 3766 * cloning, otherwise use the built-in clone support
3744 3767 */
3745 3768 if (cmdbuf[0] != '\0') {
3746 3769 /* Run the clone hook */
3747 3770 status = do_subproc(cmdbuf);
3748 3771 if ((status = subproc_status(gettext("brand-specific clone"),
3749 3772 status, B_FALSE)) != ZONE_SUBPROC_OK) {
3750 3773 if (status == ZONE_SUBPROC_USAGE && !brand_help)
3751 3774 sub_usage(SHELP_CLONE, CMD_CLONE);
3752 3775 err = Z_ERR;
3753 3776 goto done;
3754 3777 }
3755 3778
3756 3779 if (brand_help)
3757 3780 return (Z_OK);
3758 3781
3759 3782 } else {
3760 3783 /* If just help, we're done since there is no brand help. */
3761 3784 if (brand_help)
3762 3785 return (Z_OK);
3763 3786
3764 3787 /* Run the built-in clone support. */
3765 3788
3766 3789 /* The only explicit built-in method is "copy". */
3767 3790 if (method != NULL && strcmp(method, "copy") != 0) {
3768 3791 sub_usage(SHELP_CLONE, CMD_CLONE);
3769 3792 err = Z_USAGE;
3770 3793 goto done;
3771 3794 }
3772 3795
3773 3796 if (snapshot != NULL) {
3774 3797 err = clone_snapshot_zfs(snapshot, zonepath,
3775 3798 validsnapbuf);
3776 3799 } else {
3777 3800 /*
3778 3801 * We always copy the clone unless the source is ZFS
3779 3802 * and a ZFS clone worked. We fallback to copying if
3780 3803 * the ZFS clone fails for some reason.
3781 3804 */
3782 3805 err = Z_ERR;
3783 3806 if (method == NULL && is_zonepath_zfs(source_zonepath))
3784 3807 err = clone_zfs(source_zonepath, zonepath,
3785 3808 presnapbuf, postsnapbuf);
3786 3809
3787 3810 if (err != Z_OK)
3788 3811 err = clone_copy(source_zonepath, zonepath);
3789 3812 }
3790 3813 }
3791 3814
3792 3815 if (err == Z_OK && postcmdbuf[0] != '\0') {
3793 3816 status = do_subproc(postcmdbuf);
3794 3817 if ((err = subproc_status("postclone", status, B_FALSE))
3795 3818 != ZONE_SUBPROC_OK) {
3796 3819 zerror(gettext("post-clone configuration failed."));
3797 3820 err = Z_ERR;
3798 3821 }
3799 3822 }
3800 3823
3801 3824 done:
3802 3825 /*
3803 3826 * If everything went well, we mark the zone as installed.
3804 3827 */
3805 3828 if (err == Z_OK) {
3806 3829 err = zone_set_state(target_zone, ZONE_STATE_INSTALLED);
3807 3830 if (err != Z_OK) {
3808 3831 errno = err;
3809 3832 zperror2(target_zone, gettext("could not set state"));
3810 3833 }
3811 3834 }
3812 3835 if (!brand_help)
3813 3836 zonecfg_release_lock_file(target_zone, lockfd);
3814 3837 return ((err == Z_OK) ? Z_OK : Z_ERR);
3815 3838 }
3816 3839
3817 3840 /*
3818 3841 * Used when removing a zonepath after uninstalling or cleaning up after
3819 3842 * the move subcommand. This handles a zonepath that has non-standard
3820 3843 * contents so that we will only cleanup the stuff we know about and leave
3821 3844 * any user data alone.
3822 3845 *
3823 3846 * If the "all" parameter is true then we should remove the whole zonepath
3824 3847 * even if it has non-standard files/directories in it. This can be used when
3825 3848 * we need to cleanup after moving the zonepath across file systems.
3826 3849 *
3827 3850 * We "exec" the RMCOMMAND so that the returned status is that of RMCOMMAND
3828 3851 * and not the shell.
3829 3852 */
3830 3853 static int
3831 3854 cleanup_zonepath(char *zonepath, boolean_t all)
3832 3855 {
3833 3856 int status;
3834 3857 int i;
3835 3858 boolean_t non_std = B_FALSE;
3836 3859 struct dirent *dp;
3837 3860 DIR *dirp;
3838 3861 /*
3839 3862 * The SUNWattached.xml file is expected since it might
3840 3863 * exist if the zone was force-attached after a
3841 3864 * migration.
3842 3865 */
3843 3866 char *std_entries[] = {"dev", "lu", "root",
3844 3867 "SUNWattached.xml", NULL};
3845 3868 /* (MAXPATHLEN * 3) is for the 3 std_entries dirs */
3846 3869 char cmdbuf[sizeof (RMCOMMAND) + (MAXPATHLEN * 3) + 64];
3847 3870
3848 3871 /*
3849 3872 * We shouldn't need these checks but lets be paranoid since we
3850 3873 * could blow away the whole system here if we got the wrong zonepath.
3851 3874 */
3852 3875 if (*zonepath == NULL || strcmp(zonepath, "/") == 0) {
3853 3876 (void) fprintf(stderr, "invalid zonepath '%s'\n", zonepath);
3854 3877 return (Z_INVAL);
3855 3878 }
3856 3879
3857 3880 /*
3858 3881 * If the dirpath is already gone (maybe it was manually removed) then
3859 3882 * we just return Z_OK so that the cleanup is successful.
3860 3883 */
3861 3884 if ((dirp = opendir(zonepath)) == NULL)
3862 3885 return (Z_OK);
3863 3886
3864 3887 /*
3865 3888 * Look through the zonepath directory to see if there are any
3866 3889 * non-standard files/dirs. Also skip .zfs since that might be
3867 3890 * there but we'll handle ZFS file systems as a special case.
3868 3891 */
3869 3892 while ((dp = readdir(dirp)) != NULL) {
3870 3893 if (strcmp(dp->d_name, ".") == 0 ||
3871 3894 strcmp(dp->d_name, "..") == 0 ||
3872 3895 strcmp(dp->d_name, ".zfs") == 0)
3873 3896 continue;
3874 3897
3875 3898 for (i = 0; std_entries[i] != NULL; i++)
3876 3899 if (strcmp(dp->d_name, std_entries[i]) == 0)
3877 3900 break;
3878 3901
3879 3902 if (std_entries[i] == NULL)
3880 3903 non_std = B_TRUE;
3881 3904 }
3882 3905 (void) closedir(dirp);
3883 3906
3884 3907 if (!all && non_std) {
3885 3908 /*
3886 3909 * There are extra, non-standard directories/files in the
3887 3910 * zonepath so we don't want to remove the zonepath. We
3888 3911 * just want to remove the standard directories and leave
3889 3912 * the user data alone.
3890 3913 */
3891 3914 (void) snprintf(cmdbuf, sizeof (cmdbuf), "exec " RMCOMMAND);
3892 3915
3893 3916 for (i = 0; std_entries[i] != NULL; i++) {
3894 3917 char tmpbuf[MAXPATHLEN];
3895 3918
3896 3919 if (snprintf(tmpbuf, sizeof (tmpbuf), " %s/%s",
3897 3920 zonepath, std_entries[i]) >= sizeof (tmpbuf) ||
3898 3921 strlcat(cmdbuf, tmpbuf, sizeof (cmdbuf)) >=
3899 3922 sizeof (cmdbuf)) {
3900 3923 (void) fprintf(stderr,
3901 3924 gettext("path is too long\n"));
3902 3925 return (Z_INVAL);
3903 3926 }
3904 3927 }
3905 3928
3906 3929 status = do_subproc(cmdbuf);
3907 3930
3908 3931 (void) fprintf(stderr, gettext("WARNING: Unable to completely "
3909 3932 "remove %s\nbecause it contains additional user data. "
3910 3933 "Only the standard directory\nentries have been "
3911 3934 "removed.\n"),
3912 3935 zonepath);
3913 3936
3914 3937 return ((subproc_status(RMCOMMAND, status, B_TRUE) ==
3915 3938 ZONE_SUBPROC_OK) ? Z_OK : Z_ERR);
3916 3939 }
3917 3940
3918 3941 /*
3919 3942 * There is nothing unexpected in the zonepath, try to get rid of the
3920 3943 * whole zonepath directory.
3921 3944 *
3922 3945 * If the zonepath is its own zfs file system, try to destroy the
3923 3946 * file system. If that fails for some reason (e.g. it has clones)
3924 3947 * then we'll just remove the contents of the zonepath.
3925 3948 */
3926 3949 if (is_zonepath_zfs(zonepath)) {
3927 3950 if (destroy_zfs(zonepath) == Z_OK)
3928 3951 return (Z_OK);
3929 3952 (void) snprintf(cmdbuf, sizeof (cmdbuf), "exec " RMCOMMAND
3930 3953 " %s/*", zonepath);
3931 3954 status = do_subproc(cmdbuf);
3932 3955 return ((subproc_status(RMCOMMAND, status, B_TRUE) ==
3933 3956 ZONE_SUBPROC_OK) ? Z_OK : Z_ERR);
3934 3957 }
3935 3958
3936 3959 (void) snprintf(cmdbuf, sizeof (cmdbuf), "exec " RMCOMMAND " %s",
3937 3960 zonepath);
3938 3961 status = do_subproc(cmdbuf);
3939 3962
3940 3963 return ((subproc_status(RMCOMMAND, status, B_TRUE) == ZONE_SUBPROC_OK)
3941 3964 ? Z_OK : Z_ERR);
3942 3965 }
3943 3966
3944 3967 static int
3945 3968 move_func(int argc, char *argv[])
3946 3969 {
3947 3970 char *new_zonepath = NULL;
3948 3971 int lockfd;
3949 3972 int err, arg;
3950 3973 char zonepath[MAXPATHLEN];
3951 3974 zone_dochandle_t handle;
3952 3975 boolean_t fast;
3953 3976 boolean_t is_zfs = B_FALSE;
3954 3977 boolean_t root_fs_mounted = B_FALSE;
3955 3978 struct dirent *dp;
3956 3979 DIR *dirp;
3957 3980 boolean_t empty = B_TRUE;
3958 3981 boolean_t revert;
3959 3982 struct stat zonepath_buf;
3960 3983 struct stat new_zonepath_buf;
3961 3984 zone_mounts_t mounts;
3962 3985
3963 3986 if (zonecfg_in_alt_root()) {
3964 3987 zerror(gettext("cannot move zone in alternate root"));
3965 3988 return (Z_ERR);
3966 3989 }
3967 3990
3968 3991 optind = 0;
3969 3992 if ((arg = getopt(argc, argv, "?")) != EOF) {
3970 3993 switch (arg) {
3971 3994 case '?':
3972 3995 sub_usage(SHELP_MOVE, CMD_MOVE);
3973 3996 return (optopt == '?' ? Z_OK : Z_USAGE);
3974 3997 default:
3975 3998 sub_usage(SHELP_MOVE, CMD_MOVE);
3976 3999 return (Z_USAGE);
3977 4000 }
3978 4001 }
3979 4002 if (argc != (optind + 1)) {
3980 4003 sub_usage(SHELP_MOVE, CMD_MOVE);
3981 4004 return (Z_USAGE);
3982 4005 }
3983 4006 new_zonepath = argv[optind];
3984 4007 if (sanity_check(target_zone, CMD_MOVE, B_FALSE, B_TRUE, B_FALSE)
3985 4008 != Z_OK)
3986 4009 return (Z_ERR);
3987 4010 if (verify_details(CMD_MOVE, argv) != Z_OK)
3988 4011 return (Z_ERR);
3989 4012
3990 4013 /*
3991 4014 * Check out the new zonepath. This has the side effect of creating
3992 4015 * a directory for the new zonepath. We depend on this later when we
3993 4016 * stat to see if we are doing a cross file system move or not.
3994 4017 */
3995 4018 if (validate_zonepath(new_zonepath, CMD_MOVE) != Z_OK)
3996 4019 return (Z_ERR);
3997 4020
3998 4021 if ((err = zone_get_zonepath(target_zone, zonepath, sizeof (zonepath)))
3999 4022 != Z_OK) {
4000 4023 errno = err;
4001 4024 zperror2(target_zone, gettext("could not get zone path"));
4002 4025 return (Z_ERR);
4003 4026 }
4004 4027
4005 4028 if (stat(zonepath, &zonepath_buf) == -1) {
4006 4029 zperror(gettext("could not stat zone path"), B_FALSE);
4007 4030 return (Z_ERR);
4008 4031 }
4009 4032
4010 4033 if (stat(new_zonepath, &new_zonepath_buf) == -1) {
4011 4034 zperror(gettext("could not stat new zone path"), B_FALSE);
4012 4035 return (Z_ERR);
4013 4036 }
4014 4037
4015 4038 /*
4016 4039 * Check if the destination directory is empty.
4017 4040 */
4018 4041 if ((dirp = opendir(new_zonepath)) == NULL) {
4019 4042 zperror(gettext("could not open new zone path"), B_FALSE);
4020 4043 return (Z_ERR);
4021 4044 }
4022 4045 while ((dp = readdir(dirp)) != (struct dirent *)0) {
4023 4046 if (strcmp(dp->d_name, ".") == 0 ||
4024 4047 strcmp(dp->d_name, "..") == 0)
4025 4048 continue;
4026 4049 empty = B_FALSE;
4027 4050 break;
4028 4051 }
4029 4052 (void) closedir(dirp);
4030 4053
4031 4054 /* Error if there is anything in the destination directory. */
4032 4055 if (!empty) {
4033 4056 (void) fprintf(stderr, gettext("could not move zone to %s: "
4034 4057 "directory not empty\n"), new_zonepath);
4035 4058 return (Z_ERR);
4036 4059 }
4037 4060
4038 4061 /*
4039 4062 * Collect information about mounts within the zone's zonepath.
4040 4063 * Overlay mounts on the zone's root directory are erroneous.
4041 4064 * Bail if we encounter any unexpected mounts.
4042 4065 */
4043 4066 if (zone_mounts_init(&mounts, zonepath) != 0)
4044 4067 return (Z_ERR);
4045 4068 if (mounts.num_root_overlay_mounts != 0) {
4046 4069 zerror(gettext("%d overlay mount(s) detected on %s/root."),
4047 4070 mounts.num_root_overlay_mounts, zonepath);
4048 4071 goto err_and_mounts_destroy;
4049 4072 }
4050 4073 if (mounts.num_unexpected_mounts != 0)
4051 4074 goto err_and_mounts_destroy;
4052 4075
4053 4076 /*
4054 4077 * Check if we are moving in the same file system and can do a fast
4055 4078 * move or if we are crossing file systems and have to copy the data.
4056 4079 */
4057 4080 fast = (zonepath_buf.st_dev == new_zonepath_buf.st_dev);
4058 4081
4059 4082 if ((handle = zonecfg_init_handle()) == NULL) {
4060 4083 zperror(cmd_to_str(CMD_MOVE), B_TRUE);
4061 4084 goto err_and_mounts_destroy;
4062 4085 }
4063 4086
4064 4087 if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) {
4065 4088 errno = err;
4066 4089 zperror(cmd_to_str(CMD_MOVE), B_TRUE);
4067 4090 goto err_and_fini_handle;
4068 4091 }
4069 4092
4070 4093 if (zonecfg_grab_lock_file(target_zone, &lockfd) != Z_OK) {
4071 4094 zerror(gettext("another %s may have an operation in progress."),
4072 4095 "zoneadm");
4073 4096 goto err_and_fini_handle;
4074 4097 }
4075 4098
4076 4099 /*
4077 4100 * Unmount the zone's root filesystem before we move the zone's
4078 4101 * zonepath.
4079 4102 */
4080 4103 if (zone_unmount_rootfs(&mounts, zonepath, B_FALSE) != 0)
4081 4104 goto err_and_rele_lockfile;
4082 4105
4083 4106 /*
4084 4107 * We're making some file system changes now so we have to clean up
4085 4108 * the file system before we are done. This will either clean up the
4086 4109 * new zonepath if the zonecfg update failed or it will clean up the
4087 4110 * old zonepath if everything is ok.
4088 4111 */
4089 4112 revert = B_TRUE;
4090 4113
4091 4114 if (is_zonepath_zfs(zonepath) &&
4092 4115 move_zfs(zonepath, new_zonepath) != Z_ERR) {
4093 4116 is_zfs = B_TRUE;
4094 4117
4095 4118 } else if (fast) {
4096 4119 /* same file system, use rename for a quick move */
4097 4120
4098 4121 /*
4099 4122 * Remove the new_zonepath directory that got created above
4100 4123 * during the validation. It gets in the way of the rename.
4101 4124 */
4102 4125 if (rmdir(new_zonepath) != 0) {
4103 4126 zperror(gettext("could not rmdir new zone path"),
4104 4127 B_FALSE);
4105 4128 (void) zone_mount_rootfs(&mounts, zonepath);
4106 4129 goto err_and_rele_lockfile;
4107 4130 }
4108 4131
4109 4132 if (rename(zonepath, new_zonepath) != 0) {
4110 4133 /*
4111 4134 * If this fails we don't need to do all of the
4112 4135 * cleanup that happens for the rest of the code
4113 4136 * so just return from this error.
4114 4137 */
4115 4138 zperror(gettext("could not move zone"), B_FALSE);
4116 4139 (void) zone_mount_rootfs(&mounts, zonepath);
4117 4140 goto err_and_rele_lockfile;
4118 4141 }
4119 4142
4120 4143 } else {
4121 4144 /*
4122 4145 * Attempt to create a ZFS fs for the new zonepath. As usual,
4123 4146 * we don't care if this works or not since we always have the
4124 4147 * default behavior of a simple directory for the zonepath.
4125 4148 */
4126 4149 create_zfs_zonepath(new_zonepath);
4127 4150
4128 4151 (void) printf(gettext(
4129 4152 "Moving across file systems; copying zonepath %s..."),
4130 4153 zonepath);
4131 4154 (void) fflush(stdout);
4132 4155
4133 4156 err = copy_zone(zonepath, new_zonepath);
4134 4157
4135 4158 (void) printf("\n");
4136 4159 if (err != Z_OK)
4137 4160 goto done;
4138 4161 }
4139 4162
4140 4163 /*
4141 4164 * Mount the zone's root filesystem in the new zonepath if there was
4142 4165 * a root mount prior to the move.
4143 4166 */
4144 4167 if (zone_mount_rootfs(&mounts, new_zonepath) != 0) {
4145 4168 err = Z_ERR;
4146 4169 goto done;
4147 4170 }
4148 4171 root_fs_mounted = B_TRUE;
4149 4172
4150 4173 if ((err = zonecfg_set_zonepath(handle, new_zonepath)) != Z_OK) {
4151 4174 errno = err;
4152 4175 zperror(gettext("could not set new zonepath"), B_TRUE);
4153 4176 goto done;
4154 4177 }
4155 4178
4156 4179 if ((err = zonecfg_save(handle)) != Z_OK) {
4157 4180 errno = err;
4158 4181 zperror(gettext("zonecfg save failed"), B_TRUE);
4159 4182 goto done;
4160 4183 }
4161 4184
4162 4185 revert = B_FALSE;
4163 4186
4164 4187 done:
4165 4188 zonecfg_fini_handle(handle);
4166 4189 zonecfg_release_lock_file(target_zone, lockfd);
4167 4190
4168 4191 /*
4169 4192 * Clean up the file system based on how things went. We either
4170 4193 * clean up the new zonepath if the operation failed for some reason
4171 4194 * or we clean up the old zonepath if everything is ok.
4172 4195 */
4173 4196 if (revert) {
4174 4197 /*
4175 4198 * Check for the unlikely scenario in which the zone's
4176 4199 * zonepath and its root file system moved but libzonecfg
4177 4200 * couldn't save the new zonepath to the zone's configuration
4178 4201 * file. The mounted root filesystem must be unmounted before
4179 4202 * zoneadm restores the zone's zonepath.
4180 4203 */
4181 4204 if (root_fs_mounted && zone_unmount_rootfs(&mounts,
4182 4205 new_zonepath, B_TRUE) != 0) {
4183 4206 /*
4184 4207 * We can't forcibly unmount the zone's root file system
4185 4208 * from the new zonepath. Bail!
4186 4209 */
4187 4210 zerror(gettext("fatal error: cannot unmount %s/root\n"),
4188 4211 new_zonepath);
4189 4212 goto err_and_mounts_destroy;
4190 4213 }
4191 4214
4192 4215 /* The zonecfg update failed, cleanup the new zonepath. */
4193 4216 if (is_zfs) {
4194 4217 if (move_zfs(new_zonepath, zonepath) == Z_ERR) {
4195 4218 (void) fprintf(stderr, gettext("could not "
4196 4219 "restore zonepath, the zfs mountpoint is "
4197 4220 "set as:\n%s\n"), new_zonepath);
4198 4221 /*
4199 4222 * err is already != Z_OK since we're reverting
4200 4223 */
4201 4224 } else {
4202 4225 (void) zone_mount_rootfs(&mounts, zonepath);
4203 4226 }
4204 4227 } else if (fast) {
4205 4228 if (rename(new_zonepath, zonepath) != 0) {
4206 4229 zperror(gettext("could not restore zonepath"),
4207 4230 B_FALSE);
4208 4231 /*
4209 4232 * err is already != Z_OK since we're reverting
4210 4233 */
4211 4234 } else {
4212 4235 (void) zone_mount_rootfs(&mounts, zonepath);
4213 4236 }
4214 4237 } else {
4215 4238 (void) printf(gettext("Cleaning up zonepath %s..."),
4216 4239 new_zonepath);
4217 4240 (void) fflush(stdout);
4218 4241 err = cleanup_zonepath(new_zonepath, B_TRUE);
4219 4242 (void) printf("\n");
4220 4243
4221 4244 if (err != Z_OK) {
4222 4245 errno = err;
4223 4246 zperror(gettext("could not remove new "
4224 4247 "zonepath"), B_TRUE);
4225 4248 } else {
4226 4249 /*
4227 4250 * Because we're reverting we know the mainline
4228 4251 * code failed but we just reused the err
4229 4252 * variable so we reset it back to Z_ERR.
4230 4253 */
4231 4254 err = Z_ERR;
4232 4255 }
4233 4256
4234 4257 (void) zone_mount_rootfs(&mounts, zonepath);
4235 4258 }
4236 4259 } else {
4237 4260 /* The move was successful, cleanup the old zonepath. */
4238 4261 if (!is_zfs && !fast) {
4239 4262 (void) printf(
4240 4263 gettext("Cleaning up zonepath %s..."), zonepath);
4241 4264 (void) fflush(stdout);
4242 4265 err = cleanup_zonepath(zonepath, B_TRUE);
4243 4266 (void) printf("\n");
4244 4267
4245 4268 if (err != Z_OK) {
4246 4269 errno = err;
4247 4270 zperror(gettext("could not remove zonepath"),
4248 4271 B_TRUE);
4249 4272 }
4250 4273 }
4251 4274 }
4252 4275
4253 4276 zone_mounts_destroy(&mounts);
4254 4277 return ((err == Z_OK) ? Z_OK : Z_ERR);
4255 4278
4256 4279 err_and_rele_lockfile:
4257 4280 zonecfg_release_lock_file(target_zone, lockfd);
4258 4281 err_and_fini_handle:
4259 4282 zonecfg_fini_handle(handle);
4260 4283 err_and_mounts_destroy:
4261 4284 zone_mounts_destroy(&mounts);
4262 4285 return (Z_ERR);
4263 4286 }
4264 4287
4265 4288 /* ARGSUSED */
4266 4289 static int
4267 4290 detach_func(int argc, char *argv[])
4268 4291 {
4269 4292 int lockfd = -1;
4270 4293 int err, arg;
4271 4294 char zonepath[MAXPATHLEN];
4272 4295 char cmdbuf[MAXPATHLEN];
4273 4296 char precmdbuf[MAXPATHLEN];
4274 4297 boolean_t execute = B_TRUE;
4275 4298 boolean_t brand_help = B_FALSE;
4276 4299 brand_handle_t bh = NULL;
4277 4300 int status;
4278 4301
4279 4302 if (zonecfg_in_alt_root()) {
4280 4303 zerror(gettext("cannot detach zone in alternate root"));
4281 4304 return (Z_ERR);
4282 4305 }
4283 4306
4284 4307 /* Check the argv string for args we handle internally */
4285 4308 optind = 0;
4286 4309 opterr = 0;
4287 4310 while ((arg = getopt(argc, argv, "?n")) != EOF) {
4288 4311 switch (arg) {
4289 4312 case '?':
4290 4313 if (optopt == '?') {
4291 4314 sub_usage(SHELP_DETACH, CMD_DETACH);
4292 4315 brand_help = B_TRUE;
4293 4316 }
4294 4317 /* Ignore unknown options - may be brand specific. */
4295 4318 break;
4296 4319 case 'n':
4297 4320 execute = B_FALSE;
4298 4321 break;
4299 4322 default:
4300 4323 /* Ignore unknown options - may be brand specific. */
4301 4324 break;
4302 4325 }
4303 4326 }
4304 4327
4305 4328 if (brand_help)
4306 4329 execute = B_FALSE;
4307 4330
4308 4331 if (execute) {
4309 4332 if (sanity_check(target_zone, CMD_DETACH, B_FALSE, B_TRUE,
4310 4333 B_FALSE) != Z_OK)
4311 4334 return (Z_ERR);
4312 4335 if (verify_details(CMD_DETACH, argv) != Z_OK)
4313 4336 return (Z_ERR);
4314 4337 } else {
4315 4338 /*
4316 4339 * We want a dry-run to work for a non-privileged user so we
4317 4340 * only do minimal validation.
4318 4341 */
4319 4342 if (target_zone == NULL) {
4320 4343 zerror(gettext("no zone specified"));
4321 4344 return (Z_ERR);
4322 4345 }
4323 4346
4324 4347 if (strcmp(target_zone, GLOBAL_ZONENAME) == 0) {
4325 4348 zerror(gettext("%s operation is invalid for the "
4326 4349 "global zone."), cmd_to_str(CMD_DETACH));
4327 4350 return (Z_ERR);
4328 4351 }
4329 4352 }
4330 4353
4331 4354 if ((err = zone_get_zonepath(target_zone, zonepath, sizeof (zonepath)))
4332 4355 != Z_OK) {
4333 4356 errno = err;
4334 4357 zperror2(target_zone, gettext("could not get zone path"));
4335 4358 return (Z_ERR);
4336 4359 }
4337 4360
4338 4361 /* Fetch the detach and predetach hooks from the brand configuration. */
4339 4362 if ((bh = brand_open(target_brand)) == NULL) {
4340 4363 zerror(gettext("missing or invalid brand"));
4341 4364 return (Z_ERR);
4342 4365 }
4343 4366
4344 4367 if (get_hook(bh, cmdbuf, sizeof (cmdbuf), brand_get_detach, target_zone,
4345 4368 zonepath) != Z_OK) {
4346 4369 zerror("invalid brand configuration: missing detach resource");
4347 4370 brand_close(bh);
4348 4371 return (Z_ERR);
4349 4372 }
4350 4373
4351 4374 if (get_hook(bh, precmdbuf, sizeof (precmdbuf), brand_get_predetach,
4352 4375 target_zone, zonepath) != Z_OK) {
4353 4376 zerror("invalid brand configuration: missing predetach "
4354 4377 "resource");
4355 4378 brand_close(bh);
4356 4379 return (Z_ERR);
4357 4380 }
4358 4381 brand_close(bh);
4359 4382
4360 4383 /* Append all options to predetach hook. */
4361 4384 if (addoptions(precmdbuf, argv, sizeof (precmdbuf)) != Z_OK)
4362 4385 return (Z_ERR);
4363 4386
4364 4387 /* Append all options to detach hook. */
4365 4388 if (addoptions(cmdbuf, argv, sizeof (cmdbuf)) != Z_OK)
4366 4389 return (Z_ERR);
4367 4390
4368 4391 if (execute && zonecfg_grab_lock_file(target_zone, &lockfd) != Z_OK) {
4369 4392 zerror(gettext("another %s may have an operation in progress."),
4370 4393 "zoneadm");
4371 4394 return (Z_ERR);
4372 4395 }
4373 4396
4374 4397 /* If we have a brand predetach hook, run it. */
4375 4398 if (!brand_help && precmdbuf[0] != '\0') {
4376 4399 status = do_subproc(precmdbuf);
4377 4400 if (subproc_status(gettext("brand-specific predetach"),
4378 4401 status, B_FALSE) != ZONE_SUBPROC_OK) {
4379 4402
4380 4403 if (execute) {
4381 4404 assert(lockfd >= 0);
4382 4405 zonecfg_release_lock_file(target_zone, lockfd);
4383 4406 lockfd = -1;
4384 4407 }
4385 4408
4386 4409 assert(lockfd == -1);
4387 4410 return (Z_ERR);
4388 4411 }
4389 4412 }
4390 4413
4391 4414 if (cmdbuf[0] != '\0') {
4392 4415 /* Run the detach hook */
4393 4416 status = do_subproc(cmdbuf);
4394 4417 if ((status = subproc_status(gettext("brand-specific detach"),
4395 4418 status, B_FALSE)) != ZONE_SUBPROC_OK) {
4396 4419 if (status == ZONE_SUBPROC_USAGE && !brand_help)
4397 4420 sub_usage(SHELP_DETACH, CMD_DETACH);
4398 4421
4399 4422 if (execute) {
4400 4423 assert(lockfd >= 0);
4401 4424 zonecfg_release_lock_file(target_zone, lockfd);
4402 4425 lockfd = -1;
4403 4426 }
4404 4427
4405 4428 assert(lockfd == -1);
4406 4429 return (Z_ERR);
4407 4430 }
4408 4431
4409 4432 } else {
4410 4433 zone_dochandle_t handle;
4411 4434
4412 4435 /* If just help, we're done since there is no brand help. */
4413 4436 if (brand_help) {
4414 4437 assert(lockfd == -1);
4415 4438 return (Z_OK);
4416 4439 }
4417 4440
4418 4441 /*
4419 4442 * Run the built-in detach support. Just generate a simple
4420 4443 * zone definition XML file and detach.
4421 4444 */
4422 4445
4423 4446 /* Don't detach the zone if anything is still mounted there */
4424 4447 if (execute && zonecfg_find_mounts(zonepath, NULL, NULL)) {
4425 4448 (void) fprintf(stderr, gettext("These file systems are "
4426 4449 "mounted on subdirectories of %s.\n"), zonepath);
4427 4450 (void) zonecfg_find_mounts(zonepath, zfm_print, NULL);
4428 4451 err = ZONE_SUBPROC_NOTCOMPLETE;
4429 4452 goto done;
4430 4453 }
4431 4454
4432 4455 if ((handle = zonecfg_init_handle()) == NULL) {
4433 4456 zperror(cmd_to_str(CMD_DETACH), B_TRUE);
4434 4457 err = ZONE_SUBPROC_NOTCOMPLETE;
4435 4458 goto done;
4436 4459 }
4437 4460
4438 4461 if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) {
4439 4462 errno = err;
4440 4463 zperror(cmd_to_str(CMD_DETACH), B_TRUE);
4441 4464
4442 4465 } else if ((err = zonecfg_detach_save(handle,
4443 4466 (execute ? 0 : ZONE_DRY_RUN))) != Z_OK) {
4444 4467 errno = err;
4445 4468 zperror(gettext("saving the detach manifest failed"),
4446 4469 B_TRUE);
4447 4470 }
4448 4471
4449 4472 zonecfg_fini_handle(handle);
4450 4473 if (err != Z_OK)
4451 4474 goto done;
4452 4475 }
4453 4476
4454 4477 /*
4455 4478 * Set the zone state back to configured unless we are running with the
4456 4479 * no-execute option.
4457 4480 */
4458 4481 if (execute && (err = zone_set_state(target_zone,
4459 4482 ZONE_STATE_CONFIGURED)) != Z_OK) {
4460 4483 errno = err;
4461 4484 zperror(gettext("could not reset state"), B_TRUE);
4462 4485 }
4463 4486
4464 4487 done:
4465 4488 if (execute) {
4466 4489 assert(lockfd >= 0);
4467 4490 zonecfg_release_lock_file(target_zone, lockfd);
4468 4491 lockfd = -1;
4469 4492 }
4470 4493
4471 4494 assert(lockfd == -1);
4472 4495 return ((err == Z_OK) ? Z_OK : Z_ERR);
4473 4496 }
4474 4497
4475 4498 /*
4476 4499 * Determine the brand when doing a dry-run attach. The zone does not have to
4477 4500 * exist, so we have to read the incoming manifest to determine the zone's
4478 4501 * brand.
4479 4502 *
4480 4503 * Because the manifest has to be processed twice; once to determine the brand
4481 4504 * and once to do the brand-specific attach logic, we always read it into a tmp
4482 4505 * file. This handles the manifest coming from stdin or a regular file. The
4483 4506 * tmpname parameter returns the name of the temporary file that the manifest
4484 4507 * was read into.
4485 4508 */
4486 4509 static int
4487 4510 dryrun_get_brand(char *manifest_path, char *tmpname, int size)
4488 4511 {
4489 4512 int fd;
4490 4513 int err;
4491 4514 int res = Z_OK;
4492 4515 zone_dochandle_t local_handle;
4493 4516 zone_dochandle_t rem_handle = NULL;
4494 4517 int len;
4495 4518 int ofd;
4496 4519 char buf[512];
4497 4520
4498 4521 if (strcmp(manifest_path, "-") == 0) {
4499 4522 fd = STDIN_FILENO;
4500 4523 } else {
4501 4524 if ((fd = open(manifest_path, O_RDONLY)) < 0) {
4502 4525 if (getcwd(buf, sizeof (buf)) == NULL)
4503 4526 (void) strlcpy(buf, "/", sizeof (buf));
4504 4527 zerror(gettext("could not open manifest path %s%s: %s"),
4505 4528 (*manifest_path == '/' ? "" : buf), manifest_path,
4506 4529 strerror(errno));
4507 4530 return (Z_ERR);
4508 4531 }
4509 4532 }
4510 4533
4511 4534 (void) snprintf(tmpname, size, "/var/run/zone.%d", getpid());
4512 4535
4513 4536 if ((ofd = open(tmpname, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR)) < 0) {
4514 4537 zperror(gettext("could not save manifest"), B_FALSE);
4515 4538 (void) close(fd);
4516 4539 return (Z_ERR);
4517 4540 }
4518 4541
4519 4542 while ((len = read(fd, buf, sizeof (buf))) > 0) {
4520 4543 if (write(ofd, buf, len) == -1) {
4521 4544 zperror(gettext("could not save manifest"), B_FALSE);
4522 4545 (void) close(ofd);
4523 4546 (void) close(fd);
4524 4547 return (Z_ERR);
4525 4548 }
4526 4549 }
4527 4550
4528 4551 if (close(ofd) != 0) {
4529 4552 zperror(gettext("could not save manifest"), B_FALSE);
4530 4553 (void) close(fd);
4531 4554 return (Z_ERR);
4532 4555 }
4533 4556
4534 4557 (void) close(fd);
4535 4558
4536 4559 if ((fd = open(tmpname, O_RDONLY)) < 0) {
4537 4560 zperror(gettext("could not open manifest path"), B_FALSE);
4538 4561 return (Z_ERR);
4539 4562 }
4540 4563
4541 4564 if ((local_handle = zonecfg_init_handle()) == NULL) {
4542 4565 zperror(cmd_to_str(CMD_ATTACH), B_TRUE);
4543 4566 res = Z_ERR;
4544 4567 goto done;
4545 4568 }
4546 4569
4547 4570 if ((rem_handle = zonecfg_init_handle()) == NULL) {
4548 4571 zperror(cmd_to_str(CMD_ATTACH), B_TRUE);
4549 4572 res = Z_ERR;
4550 4573 goto done;
4551 4574 }
4552 4575
4553 4576 if ((err = zonecfg_attach_manifest(fd, local_handle, rem_handle))
4554 4577 != Z_OK) {
4555 4578 res = Z_ERR;
4556 4579
4557 4580 if (err == Z_INVALID_DOCUMENT) {
4558 4581 struct stat st;
4559 4582 char buf[6];
4560 4583
4561 4584 if (strcmp(manifest_path, "-") == 0) {
4562 4585 zerror(gettext("Input is not a valid XML "
4563 4586 "file"));
4564 4587 goto done;
4565 4588 }
4566 4589
4567 4590 if (fstat(fd, &st) == -1 || !S_ISREG(st.st_mode)) {
4568 4591 zerror(gettext("%s is not an XML file"),
4569 4592 manifest_path);
4570 4593 goto done;
4571 4594 }
4572 4595
4573 4596 bzero(buf, sizeof (buf));
4574 4597 (void) lseek(fd, 0L, SEEK_SET);
4575 4598 if (read(fd, buf, sizeof (buf) - 1) < 0 ||
4576 4599 strncmp(buf, "<?xml", 5) != 0)
4577 4600 zerror(gettext("%s is not an XML file"),
4578 4601 manifest_path);
4579 4602 else
4580 4603 zerror(gettext("Cannot attach to an earlier "
4581 4604 "release of the operating system"));
4582 4605 } else {
4583 4606 zperror(cmd_to_str(CMD_ATTACH), B_TRUE);
4584 4607 }
4585 4608 goto done;
4586 4609 }
4587 4610
4588 4611 /* Retrieve remote handle brand type. */
4589 4612 if (zonecfg_get_brand(rem_handle, target_brand, sizeof (target_brand))
4590 4613 != Z_OK) {
4591 4614 zerror(gettext("missing or invalid brand"));
4592 4615 exit(Z_ERR);
4593 4616 }
4594 4617
4595 4618 done:
4596 4619 zonecfg_fini_handle(local_handle);
4597 4620 zonecfg_fini_handle(rem_handle);
4598 4621 (void) close(fd);
4599 4622
4600 4623 return ((res == Z_OK) ? Z_OK : Z_ERR);
4601 4624 }
4602 4625
4603 4626 /* ARGSUSED */
4604 4627 static int
4605 4628 attach_func(int argc, char *argv[])
4606 4629 {
4607 4630 int lockfd = -1;
4608 4631 int err, arg;
4609 4632 boolean_t force = B_FALSE;
4610 4633 zone_dochandle_t handle;
4611 4634 char zonepath[MAXPATHLEN];
4612 4635 char cmdbuf[MAXPATHLEN];
4613 4636 char postcmdbuf[MAXPATHLEN];
4614 4637 boolean_t execute = B_TRUE;
4615 4638 boolean_t brand_help = B_FALSE;
4616 4639 char *manifest_path;
4617 4640 char tmpmanifest[80];
4618 4641 int manifest_pos;
4619 4642 brand_handle_t bh = NULL;
4620 4643 int status;
4621 4644 int last_index = 0;
4622 4645 int offset;
4623 4646 char *up;
4624 4647 boolean_t forced_update = B_FALSE;
4625 4648
4626 4649 if (zonecfg_in_alt_root()) {
4627 4650 zerror(gettext("cannot attach zone in alternate root"));
4628 4651 return (Z_ERR);
4629 4652 }
4630 4653
4631 4654 /* Check the argv string for args we handle internally */
4632 4655 optind = 0;
4633 4656 opterr = 0;
4634 4657 while ((arg = getopt(argc, argv, "?Fn:U")) != EOF) {
4635 4658 switch (arg) {
4636 4659 case '?':
4637 4660 if (optopt == '?') {
4638 4661 sub_usage(SHELP_ATTACH, CMD_ATTACH);
4639 4662 brand_help = B_TRUE;
4640 4663 }
4641 4664 /* Ignore unknown options - may be brand specific. */
4642 4665 break;
4643 4666 case 'F':
4644 4667 force = B_TRUE;
4645 4668 break;
4646 4669 case 'n':
4647 4670 execute = B_FALSE;
4648 4671 manifest_path = optarg;
4649 4672 manifest_pos = optind - 1;
4650 4673 break;
4651 4674 case 'U':
4652 4675 /*
4653 4676 * Undocumented 'force update' option for p2v update on
4654 4677 * attach when zone is in the incomplete state. Change
4655 4678 * the option back to 'u' and set forced_update flag.
4656 4679 */
4657 4680 if (optind == last_index)
4658 4681 offset = optind;
4659 4682 else
4660 4683 offset = optind - 1;
4661 4684 if ((up = index(argv[offset], 'U')) != NULL)
4662 4685 *up = 'u';
4663 4686 forced_update = B_TRUE;
4664 4687 break;
4665 4688 default:
4666 4689 /* Ignore unknown options - may be brand specific. */
4667 4690 break;
4668 4691 }
4669 4692 last_index = optind;
4670 4693 }
4671 4694
4672 4695 if (brand_help) {
4673 4696 force = B_FALSE;
4674 4697 execute = B_TRUE;
4675 4698 }
4676 4699
4677 4700 /* dry-run and force flags are mutually exclusive */
4678 4701 if (!execute && force) {
4679 4702 zerror(gettext("-F and -n flags are mutually exclusive"));
4680 4703 return (Z_ERR);
4681 4704 }
4682 4705
4683 4706 /*
4684 4707 * If the no-execute option was specified, we don't do validation and
4685 4708 * need to figure out the brand, since there is no zone required to be
4686 4709 * configured for this option.
4687 4710 */
4688 4711 if (execute) {
4689 4712 if (!brand_help) {
4690 4713 if (sanity_check(target_zone, CMD_ATTACH, B_FALSE,
4691 4714 B_TRUE, forced_update) != Z_OK)
4692 4715 return (Z_ERR);
4693 4716 if (verify_details(CMD_ATTACH, argv) != Z_OK)
4694 4717 return (Z_ERR);
4695 4718 }
4696 4719
4697 4720 if ((err = zone_get_zonepath(target_zone, zonepath,
4698 4721 sizeof (zonepath))) != Z_OK) {
4699 4722 errno = err;
4700 4723 zperror2(target_zone,
4701 4724 gettext("could not get zone path"));
4702 4725 return (Z_ERR);
4703 4726 }
4704 4727 } else {
4705 4728 if (dryrun_get_brand(manifest_path, tmpmanifest,
4706 4729 sizeof (tmpmanifest)) != Z_OK)
4707 4730 return (Z_ERR);
4708 4731
4709 4732 argv[manifest_pos] = tmpmanifest;
4710 4733 target_zone = "-";
4711 4734 (void) strlcpy(zonepath, "-", sizeof (zonepath));
4712 4735
4713 4736 /* Run the brand's verify_adm hook. */
4714 4737 if (verify_brand(NULL, CMD_ATTACH, argv) != Z_OK)
4715 4738 return (Z_ERR);
4716 4739 }
4717 4740
4718 4741 /*
4719 4742 * Fetch the attach and postattach hooks from the brand configuration.
4720 4743 */
4721 4744 if ((bh = brand_open(target_brand)) == NULL) {
4722 4745 zerror(gettext("missing or invalid brand"));
4723 4746 return (Z_ERR);
4724 4747 }
4725 4748
4726 4749 if (get_hook(bh, cmdbuf, sizeof (cmdbuf), brand_get_attach, target_zone,
4727 4750 zonepath) != Z_OK) {
4728 4751 zerror("invalid brand configuration: missing attach resource");
4729 4752 brand_close(bh);
4730 4753 return (Z_ERR);
4731 4754 }
4732 4755
4733 4756 if (get_hook(bh, postcmdbuf, sizeof (postcmdbuf), brand_get_postattach,
4734 4757 target_zone, zonepath) != Z_OK) {
4735 4758 zerror("invalid brand configuration: missing postattach "
4736 4759 "resource");
4737 4760 brand_close(bh);
4738 4761 return (Z_ERR);
4739 4762 }
4740 4763 brand_close(bh);
4741 4764
4742 4765 /* Append all options to attach hook. */
4743 4766 if (addoptions(cmdbuf, argv, sizeof (cmdbuf)) != Z_OK)
4744 4767 return (Z_ERR);
4745 4768
4746 4769 /* Append all options to postattach hook. */
4747 4770 if (addoptions(postcmdbuf, argv, sizeof (postcmdbuf)) != Z_OK)
4748 4771 return (Z_ERR);
4749 4772
4750 4773 if (execute && !brand_help) {
4751 4774 if (zonecfg_grab_lock_file(target_zone, &lockfd) != Z_OK) {
4752 4775 zerror(gettext("another %s may have an operation in "
4753 4776 "progress."), "zoneadm");
4754 4777 return (Z_ERR);
4755 4778 }
4756 4779 }
4757 4780
4758 4781 if (!force) {
4759 4782 /*
4760 4783 * Not a force-attach, so we need to actually do the work.
4761 4784 */
4762 4785 if (cmdbuf[0] != '\0') {
4763 4786 /* Run the attach hook */
4764 4787 status = do_subproc(cmdbuf);
4765 4788 if ((status = subproc_status(gettext("brand-specific "
4766 4789 "attach"), status, B_FALSE)) != ZONE_SUBPROC_OK) {
4767 4790 if (status == ZONE_SUBPROC_USAGE && !brand_help)
4768 4791 sub_usage(SHELP_ATTACH, CMD_ATTACH);
4769 4792
4770 4793 if (execute && !brand_help) {
4771 4794 assert(zonecfg_lock_file_held(&lockfd));
4772 4795 zonecfg_release_lock_file(target_zone,
4773 4796 lockfd);
4774 4797 lockfd = -1;
4775 4798 }
4776 4799
4777 4800 assert(lockfd == -1);
4778 4801 return (Z_ERR);
4779 4802 }
4780 4803 }
4781 4804
4782 4805 /*
4783 4806 * Else run the built-in attach support.
4784 4807 * This is a no-op since there is nothing to validate.
4785 4808 */
4786 4809
4787 4810 /* If dry-run or help, then we're done. */
4788 4811 if (!execute || brand_help) {
4789 4812 if (!execute)
4790 4813 (void) unlink(tmpmanifest);
4791 4814 assert(lockfd == -1);
4792 4815 return (Z_OK);
4793 4816 }
4794 4817 }
4795 4818
4796 4819 /* Now we can validate that the zonepath exists. */
4797 4820 if (validate_zonepath(zonepath, CMD_ATTACH) != Z_OK) {
4798 4821 (void) fprintf(stderr, gettext("could not verify zonepath %s "
4799 4822 "because of the above errors.\n"), zonepath);
4800 4823
4801 4824 assert(zonecfg_lock_file_held(&lockfd));
4802 4825 zonecfg_release_lock_file(target_zone, lockfd);
4803 4826 return (Z_ERR);
4804 4827 }
4805 4828
4806 4829 if ((handle = zonecfg_init_handle()) == NULL) {
4807 4830 zperror(cmd_to_str(CMD_ATTACH), B_TRUE);
4808 4831 err = Z_ERR;
4809 4832 } else if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) {
4810 4833 errno = err;
4811 4834 zperror(cmd_to_str(CMD_ATTACH), B_TRUE);
4812 4835 zonecfg_fini_handle(handle);
4813 4836 } else {
4814 4837 zonecfg_rm_detached(handle, force);
4815 4838 zonecfg_fini_handle(handle);
4816 4839 }
4817 4840
4818 4841 if (err == Z_OK &&
4819 4842 (err = zone_set_state(target_zone, ZONE_STATE_INSTALLED)) != Z_OK) {
4820 4843 errno = err;
4821 4844 zperror(gettext("could not reset state"), B_TRUE);
4822 4845 }
4823 4846
4824 4847 assert(zonecfg_lock_file_held(&lockfd));
4825 4848 zonecfg_release_lock_file(target_zone, lockfd);
4826 4849 lockfd = -1;
4827 4850
4828 4851 /* If we have a brand postattach hook, run it. */
4829 4852 if (err == Z_OK && !force && postcmdbuf[0] != '\0') {
4830 4853 status = do_subproc(postcmdbuf);
4831 4854 if (subproc_status(gettext("brand-specific postattach"),
4832 4855 status, B_FALSE) != ZONE_SUBPROC_OK) {
4833 4856 if ((err = zone_set_state(target_zone,
4834 4857 ZONE_STATE_CONFIGURED)) != Z_OK) {
4835 4858 errno = err;
4836 4859 zperror(gettext("could not reset state"),
4837 4860 B_TRUE);
4838 4861 }
4839 4862 }
4840 4863 }
4841 4864
4842 4865 assert(lockfd == -1);
4843 4866 return ((err == Z_OK) ? Z_OK : Z_ERR);
4844 4867 }
4845 4868
4846 4869 /*
4847 4870 * On input, TRUE => yes, FALSE => no.
4848 4871 * On return, TRUE => 1, FALSE => 0, could not ask => -1.
4849 4872 */
4850 4873
4851 4874 static int
4852 4875 ask_yesno(boolean_t default_answer, const char *question)
4853 4876 {
4854 4877 char line[64]; /* should be large enough to answer yes or no */
4855 4878
4856 4879 if (!isatty(STDIN_FILENO))
4857 4880 return (-1);
4858 4881 for (;;) {
4859 4882 (void) printf("%s (%s)? ", question,
4860 4883 default_answer ? "[y]/n" : "y/[n]");
4861 4884 if (fgets(line, sizeof (line), stdin) == NULL ||
4862 4885 line[0] == '\n')
4863 4886 return (default_answer ? 1 : 0);
4864 4887 if (tolower(line[0]) == 'y')
4865 4888 return (1);
4866 4889 if (tolower(line[0]) == 'n')
4867 4890 return (0);
4868 4891 }
4869 4892 }
4870 4893
4871 4894 /* ARGSUSED */
4872 4895 static int
4873 4896 uninstall_func(int argc, char *argv[])
4874 4897 {
4875 4898 char line[ZONENAME_MAX + 128]; /* Enough for "Are you sure ..." */
4876 4899 char rootpath[MAXPATHLEN], zonepath[MAXPATHLEN];
4877 4900 char cmdbuf[MAXPATHLEN];
4878 4901 char precmdbuf[MAXPATHLEN];
4879 4902 boolean_t force = B_FALSE;
4880 4903 int lockfd, answer;
4881 4904 int err, arg;
4882 4905 boolean_t brand_help = B_FALSE;
4883 4906 brand_handle_t bh = NULL;
4884 4907 int status;
4885 4908
4886 4909 if (zonecfg_in_alt_root()) {
4887 4910 zerror(gettext("cannot uninstall zone in alternate root"));
4888 4911 return (Z_ERR);
4889 4912 }
4890 4913
4891 4914 /* Check the argv string for args we handle internally */
4892 4915 optind = 0;
4893 4916 opterr = 0;
4894 4917 while ((arg = getopt(argc, argv, "?F")) != EOF) {
4895 4918 switch (arg) {
4896 4919 case '?':
4897 4920 if (optopt == '?') {
4898 4921 sub_usage(SHELP_UNINSTALL, CMD_UNINSTALL);
4899 4922 brand_help = B_TRUE;
4900 4923 }
4901 4924 /* Ignore unknown options - may be brand specific. */
4902 4925 break;
4903 4926 case 'F':
4904 4927 force = B_TRUE;
4905 4928 break;
4906 4929 default:
4907 4930 /* Ignore unknown options - may be brand specific. */
4908 4931 break;
4909 4932 }
4910 4933 }
4911 4934
4912 4935 if (!brand_help) {
4913 4936 if (sanity_check(target_zone, CMD_UNINSTALL, B_FALSE, B_TRUE,
4914 4937 B_FALSE) != Z_OK)
4915 4938 return (Z_ERR);
4916 4939
4917 4940 /*
4918 4941 * Invoke brand-specific handler.
4919 4942 */
4920 4943 if (invoke_brand_handler(CMD_UNINSTALL, argv) != Z_OK)
4921 4944 return (Z_ERR);
4922 4945
4923 4946 if (!force) {
4924 4947 (void) snprintf(line, sizeof (line),
4925 4948 gettext("Are you sure you want to %s zone %s"),
4926 4949 cmd_to_str(CMD_UNINSTALL), target_zone);
4927 4950 if ((answer = ask_yesno(B_FALSE, line)) == 0) {
4928 4951 return (Z_OK);
4929 4952 } else if (answer == -1) {
4930 4953 zerror(gettext("Input not from terminal and -F "
4931 4954 "not specified: %s not done."),
4932 4955 cmd_to_str(CMD_UNINSTALL));
4933 4956 return (Z_ERR);
4934 4957 }
4935 4958 }
4936 4959 }
4937 4960
4938 4961 if ((err = zone_get_zonepath(target_zone, zonepath,
4939 4962 sizeof (zonepath))) != Z_OK) {
4940 4963 errno = err;
4941 4964 zperror2(target_zone, gettext("could not get zone path"));
4942 4965 return (Z_ERR);
4943 4966 }
4944 4967
4945 4968 /*
4946 4969 * Fetch the uninstall and preuninstall hooks from the brand
4947 4970 * configuration.
4948 4971 */
4949 4972 if ((bh = brand_open(target_brand)) == NULL) {
4950 4973 zerror(gettext("missing or invalid brand"));
4951 4974 return (Z_ERR);
4952 4975 }
4953 4976
4954 4977 if (get_hook(bh, cmdbuf, sizeof (cmdbuf), brand_get_uninstall,
4955 4978 target_zone, zonepath) != Z_OK) {
4956 4979 zerror("invalid brand configuration: missing uninstall "
4957 4980 "resource");
4958 4981 brand_close(bh);
4959 4982 return (Z_ERR);
4960 4983 }
4961 4984
4962 4985 if (get_hook(bh, precmdbuf, sizeof (precmdbuf), brand_get_preuninstall,
4963 4986 target_zone, zonepath) != Z_OK) {
4964 4987 zerror("invalid brand configuration: missing preuninstall "
4965 4988 "resource");
4966 4989 brand_close(bh);
4967 4990 return (Z_ERR);
4968 4991 }
4969 4992 brand_close(bh);
4970 4993
4971 4994 /* Append all options to preuninstall hook. */
4972 4995 if (addoptions(precmdbuf, argv, sizeof (precmdbuf)) != Z_OK)
4973 4996 return (Z_ERR);
4974 4997
4975 4998 /* Append all options to uninstall hook. */
4976 4999 if (addoptions(cmdbuf, argv, sizeof (cmdbuf)) != Z_OK)
4977 5000 return (Z_ERR);
4978 5001
4979 5002 if (!brand_help) {
4980 5003 if ((err = zone_get_rootpath(target_zone, rootpath,
4981 5004 sizeof (rootpath))) != Z_OK) {
4982 5005 errno = err;
4983 5006 zperror2(target_zone, gettext("could not get root "
4984 5007 "path"));
4985 5008 return (Z_ERR);
4986 5009 }
4987 5010
4988 5011 /*
4989 5012 * If there seems to be a zoneadmd running for this zone, call
4990 5013 * it to tell it that an uninstall is happening; if all goes
4991 5014 * well it will then shut itself down.
4992 5015 */
4993 5016 if (zonecfg_ping_zoneadmd(target_zone) == Z_OK) {
4994 5017 zone_cmd_arg_t zarg;
4995 5018 zarg.cmd = Z_NOTE_UNINSTALLING;
4996 5019 /* we don't care too much if this fails, just plow on */
4997 5020 (void) zonecfg_call_zoneadmd(target_zone, &zarg, locale,
4998 5021 B_TRUE);
4999 5022 }
5000 5023
5001 5024 if (zonecfg_grab_lock_file(target_zone, &lockfd) != Z_OK) {
5002 5025 zerror(gettext("another %s may have an operation in "
5003 5026 "progress."), "zoneadm");
5004 5027 return (Z_ERR);
5005 5028 }
5006 5029
5007 5030 /* Don't uninstall the zone if anything is mounted there */
5008 5031 err = zonecfg_find_mounts(rootpath, NULL, NULL);
5009 5032 if (err) {
5010 5033 zerror(gettext("These file systems are mounted on "
5011 5034 "subdirectories of %s.\n"), rootpath);
5012 5035 (void) zonecfg_find_mounts(rootpath, zfm_print, NULL);
5013 5036 zonecfg_release_lock_file(target_zone, lockfd);
5014 5037 return (Z_ERR);
5015 5038 }
5016 5039 }
5017 5040
5018 5041 /* If we have a brand preuninstall hook, run it. */
5019 5042 if (!brand_help && precmdbuf[0] != '\0') {
5020 5043 status = do_subproc(precmdbuf);
5021 5044 if (subproc_status(gettext("brand-specific preuninstall"),
5022 5045 status, B_FALSE) != ZONE_SUBPROC_OK) {
5023 5046 zonecfg_release_lock_file(target_zone, lockfd);
5024 5047 return (Z_ERR);
5025 5048 }
5026 5049 }
5027 5050
5028 5051 if (!brand_help) {
5029 5052 err = zone_set_state(target_zone, ZONE_STATE_INCOMPLETE);
5030 5053 if (err != Z_OK) {
5031 5054 errno = err;
5032 5055 zperror2(target_zone, gettext("could not set state"));
5033 5056 goto bad;
5034 5057 }
5035 5058 }
5036 5059
5037 5060 /*
5038 5061 * If there is a brand uninstall hook, use it, otherwise use the
5039 5062 * built-in uninstall code.
5040 5063 */
5041 5064 if (cmdbuf[0] != '\0') {
5042 5065 /* Run the uninstall hook */
5043 5066 status = do_subproc(cmdbuf);
5044 5067 if ((status = subproc_status(gettext("brand-specific "
5045 5068 "uninstall"), status, B_FALSE)) != ZONE_SUBPROC_OK) {
5046 5069 if (status == ZONE_SUBPROC_USAGE && !brand_help)
5047 5070 sub_usage(SHELP_UNINSTALL, CMD_UNINSTALL);
5048 5071 if (!brand_help)
5049 5072 zonecfg_release_lock_file(target_zone, lockfd);
5050 5073 return (Z_ERR);
5051 5074 }
5052 5075
5053 5076 if (brand_help)
5054 5077 return (Z_OK);
5055 5078 } else {
5056 5079 /* If just help, we're done since there is no brand help. */
5057 5080 if (brand_help)
5058 5081 return (Z_OK);
5059 5082
5060 5083 /* Run the built-in uninstall support. */
5061 5084 if ((err = cleanup_zonepath(zonepath, B_FALSE)) != Z_OK) {
5062 5085 errno = err;
5063 5086 zperror2(target_zone, gettext("cleaning up zonepath "
5064 5087 "failed"));
5065 5088 goto bad;
5066 5089 }
5067 5090 }
5068 5091
5069 5092 err = zone_set_state(target_zone, ZONE_STATE_CONFIGURED);
5070 5093 if (err != Z_OK) {
5071 5094 errno = err;
5072 5095 zperror2(target_zone, gettext("could not reset state"));
5073 5096 }
5074 5097 bad:
5075 5098 zonecfg_release_lock_file(target_zone, lockfd);
5076 5099 return (err);
5077 5100 }
5078 5101
5079 5102 /* ARGSUSED */
5080 5103 static int
5081 5104 mount_func(int argc, char *argv[])
5082 5105 {
5083 5106 zone_cmd_arg_t zarg;
5084 5107 boolean_t force = B_FALSE;
5085 5108 int arg;
5086 5109
5087 5110 /*
5088 5111 * The only supported subargument to the "mount" subcommand is
5089 5112 * "-f", which forces us to mount a zone in the INCOMPLETE state.
5090 5113 */
5091 5114 optind = 0;
5092 5115 if ((arg = getopt(argc, argv, "f")) != EOF) {
5093 5116 switch (arg) {
5094 5117 case 'f':
5095 5118 force = B_TRUE;
5096 5119 break;
5097 5120 default:
5098 5121 return (Z_USAGE);
5099 5122 }
5100 5123 }
5101 5124 if (argc > optind)
5102 5125 return (Z_USAGE);
5103 5126
5104 5127 if (sanity_check(target_zone, CMD_MOUNT, B_FALSE, B_FALSE, force)
5105 5128 != Z_OK)
5106 5129 return (Z_ERR);
5107 5130 if (verify_details(CMD_MOUNT, argv) != Z_OK)
5108 5131 return (Z_ERR);
5109 5132
5110 5133 zarg.cmd = force ? Z_FORCEMOUNT : Z_MOUNT;
5111 5134 zarg.bootbuf[0] = '\0';
5112 5135 if (zonecfg_call_zoneadmd(target_zone, &zarg, locale, B_TRUE) != 0) {
5113 5136 zerror(gettext("call to %s failed"), "zoneadmd");
5114 5137 return (Z_ERR);
5115 5138 }
5116 5139 return (Z_OK);
5117 5140 }
5118 5141
5119 5142 /* ARGSUSED */
5120 5143 static int
5121 5144 unmount_func(int argc, char *argv[])
5122 5145 {
5123 5146 zone_cmd_arg_t zarg;
5124 5147
5125 5148 if (argc > 0)
5126 5149 return (Z_USAGE);
5127 5150 if (sanity_check(target_zone, CMD_UNMOUNT, B_FALSE, B_FALSE, B_FALSE)
5128 5151 != Z_OK)
5129 5152 return (Z_ERR);
5130 5153
5131 5154 zarg.cmd = Z_UNMOUNT;
5132 5155 if (zonecfg_call_zoneadmd(target_zone, &zarg, locale, B_TRUE) != 0) {
5133 5156 zerror(gettext("call to %s failed"), "zoneadmd");
5134 5157 return (Z_ERR);
5135 5158 }
5136 5159 return (Z_OK);
5137 5160 }
5138 5161
5139 5162 static int
5140 5163 mark_func(int argc, char *argv[])
5141 5164 {
5142 5165 int err, lockfd;
5143 5166 int arg;
5144 5167 boolean_t force = B_FALSE;
5145 5168 int state;
5146 5169
5147 5170 optind = 0;
5148 5171 opterr = 0;
5149 5172 while ((arg = getopt(argc, argv, "F")) != EOF) {
5150 5173 switch (arg) {
5151 5174 case 'F':
5152 5175 force = B_TRUE;
5153 5176 break;
5154 5177 default:
5155 5178 return (Z_USAGE);
5156 5179 }
5157 5180 }
5158 5181
5159 5182 if (argc != (optind + 1))
5160 5183 return (Z_USAGE);
5161 5184
5162 5185 if (strcmp(argv[optind], "configured") == 0)
5163 5186 state = ZONE_STATE_CONFIGURED;
5164 5187 else if (strcmp(argv[optind], "incomplete") == 0)
5165 5188 state = ZONE_STATE_INCOMPLETE;
5166 5189 else if (strcmp(argv[optind], "installed") == 0)
5167 5190 state = ZONE_STATE_INSTALLED;
5168 5191 else
5169 5192 return (Z_USAGE);
5170 5193
5171 5194 if (state != ZONE_STATE_INCOMPLETE && !force)
5172 5195 return (Z_USAGE);
5173 5196
5174 5197 if (sanity_check(target_zone, CMD_MARK, B_FALSE, B_TRUE, B_FALSE)
5175 5198 != Z_OK)
5176 5199 return (Z_ERR);
5177 5200
5178 5201 /*
5179 5202 * Invoke brand-specific handler.
5180 5203 */
5181 5204 if (invoke_brand_handler(CMD_MARK, argv) != Z_OK)
5182 5205 return (Z_ERR);
5183 5206
5184 5207 if (zonecfg_grab_lock_file(target_zone, &lockfd) != Z_OK) {
5185 5208 zerror(gettext("another %s may have an operation in progress."),
5186 5209 "zoneadm");
5187 5210 return (Z_ERR);
5188 5211 }
5189 5212
5190 5213 err = zone_set_state(target_zone, state);
5191 5214 if (err != Z_OK) {
5192 5215 errno = err;
5193 5216 zperror2(target_zone, gettext("could not set state"));
5194 5217 }
5195 5218 zonecfg_release_lock_file(target_zone, lockfd);
5196 5219
5197 5220 return (err);
5198 5221 }
5199 5222
5200 5223 /*
5201 5224 * Check what scheduling class we're running under and print a warning if
5202 5225 * we're not using FSS.
5203 5226 */
5204 5227 static int
5205 5228 check_sched_fss(zone_dochandle_t handle)
5206 5229 {
5207 5230 char class_name[PC_CLNMSZ];
5208 5231
5209 5232 if (zonecfg_get_dflt_sched_class(handle, class_name,
5210 5233 sizeof (class_name)) != Z_OK) {
5211 5234 zerror(gettext("WARNING: unable to determine the zone's "
5212 5235 "scheduling class"));
5213 5236 } else if (strcmp("FSS", class_name) != 0) {
5214 5237 zerror(gettext("WARNING: The zone.cpu-shares rctl is set but\n"
5215 5238 "FSS is not the default scheduling class for this zone. "
5216 5239 "FSS will be\nused for processes in the zone but to get "
5217 5240 "the full benefit of FSS,\nit should be the default "
5218 5241 "scheduling class. See dispadmin(1M) for\nmore details."));
5219 5242 return (Z_SYSTEM);
5220 5243 }
5221 5244
5222 5245 return (Z_OK);
5223 5246 }
5224 5247
5225 5248 static int
5226 5249 check_cpu_shares_sched(zone_dochandle_t handle)
5227 5250 {
5228 5251 int err;
5229 5252 int res = Z_OK;
5230 5253 struct zone_rctltab rctl;
5231 5254
5232 5255 if ((err = zonecfg_setrctlent(handle)) != Z_OK) {
5233 5256 errno = err;
5234 5257 zperror(cmd_to_str(CMD_APPLY), B_TRUE);
5235 5258 return (err);
5236 5259 }
5237 5260
5238 5261 while (zonecfg_getrctlent(handle, &rctl) == Z_OK) {
5239 5262 if (strcmp(rctl.zone_rctl_name, "zone.cpu-shares") == 0) {
5240 5263 if (check_sched_fss(handle) != Z_OK)
5241 5264 res = Z_SYSTEM;
5242 5265 break;
5243 5266 }
5244 5267 }
5245 5268
5246 5269 (void) zonecfg_endrctlent(handle);
5247 5270
5248 5271 return (res);
5249 5272 }
5250 5273
5251 5274 /*
5252 5275 * Check if there is a mix of processes running in different pools within the
5253 5276 * zone. This is currently only going to be called for the global zone from
5254 5277 * apply_func but that could be generalized in the future.
5255 5278 */
5256 5279 static boolean_t
5257 5280 mixed_pools(zoneid_t zoneid)
5258 5281 {
5259 5282 DIR *dirp;
5260 5283 dirent_t *dent;
5261 5284 boolean_t mixed = B_FALSE;
5262 5285 boolean_t poolid_set = B_FALSE;
5263 5286 poolid_t last_poolid = 0;
5264 5287
5265 5288 if ((dirp = opendir("/proc")) == NULL) {
5266 5289 zerror(gettext("could not open /proc"));
5267 5290 return (B_FALSE);
5268 5291 }
5269 5292
5270 5293 while ((dent = readdir(dirp)) != NULL) {
5271 5294 int procfd;
5272 5295 psinfo_t ps;
5273 5296 char procpath[MAXPATHLEN];
5274 5297
5275 5298 if (dent->d_name[0] == '.')
5276 5299 continue;
5277 5300
5278 5301 (void) snprintf(procpath, sizeof (procpath), "/proc/%s/psinfo",
5279 5302 dent->d_name);
5280 5303
5281 5304 if ((procfd = open(procpath, O_RDONLY)) == -1)
5282 5305 continue;
5283 5306
5284 5307 if (read(procfd, &ps, sizeof (ps)) == sizeof (psinfo_t)) {
5285 5308 /* skip processes in other zones and system processes */
5286 5309 if (zoneid != ps.pr_zoneid || ps.pr_flag & SSYS) {
5287 5310 (void) close(procfd);
5288 5311 continue;
5289 5312 }
5290 5313
5291 5314 if (poolid_set) {
5292 5315 if (ps.pr_poolid != last_poolid)
5293 5316 mixed = B_TRUE;
5294 5317 } else {
5295 5318 last_poolid = ps.pr_poolid;
5296 5319 poolid_set = B_TRUE;
5297 5320 }
5298 5321 }
5299 5322
5300 5323 (void) close(procfd);
5301 5324
5302 5325 if (mixed)
5303 5326 break;
5304 5327 }
5305 5328
5306 5329 (void) closedir(dirp);
5307 5330
5308 5331 return (mixed);
5309 5332 }
5310 5333
5311 5334 /*
5312 5335 * Check if a persistent or temporary pool is configured for the zone.
5313 5336 * This is currently only going to be called for the global zone from
5314 5337 * apply_func but that could be generalized in the future.
5315 5338 */
5316 5339 static boolean_t
5317 5340 pool_configured(zone_dochandle_t handle)
5318 5341 {
5319 5342 int err1, err2;
5320 5343 struct zone_psettab pset_tab;
5321 5344 char poolname[MAXPATHLEN];
5322 5345
5323 5346 err1 = zonecfg_lookup_pset(handle, &pset_tab);
5324 5347 err2 = zonecfg_get_pool(handle, poolname, sizeof (poolname));
5325 5348
5326 5349 if (err1 == Z_NO_ENTRY &&
5327 5350 (err2 == Z_NO_ENTRY || (err2 == Z_OK && strlen(poolname) == 0)))
5328 5351 return (B_FALSE);
5329 5352
5330 5353 return (B_TRUE);
5331 5354 }
5332 5355
5333 5356 /*
5334 5357 * This is an undocumented interface which is currently only used to apply
5335 5358 * the global zone resource management settings when the system boots.
5336 5359 * This function does not yet properly handle updating a running system so
5337 5360 * any projects running in the zone would be trashed if this function
5338 5361 * were to run after the zone had booted. It also does not reset any
5339 5362 * rctl settings that were removed from zonecfg. There is still work to be
5340 5363 * done before we can properly support dynamically updating the resource
5341 5364 * management settings for a running zone (global or non-global). Thus, this
5342 5365 * functionality is undocumented for now.
5343 5366 */
5344 5367 /* ARGSUSED */
5345 5368 static int
5346 5369 apply_func(int argc, char *argv[])
5347 5370 {
5348 5371 int err;
5349 5372 int res = Z_OK;
5350 5373 priv_set_t *privset;
5351 5374 zoneid_t zoneid;
5352 5375 zone_dochandle_t handle;
5353 5376 struct zone_mcaptab mcap;
5354 5377 char pool_err[128];
5355 5378
5356 5379 zoneid = getzoneid();
5357 5380
5358 5381 if (zonecfg_in_alt_root() || zoneid != GLOBAL_ZONEID ||
5359 5382 target_zone == NULL || strcmp(target_zone, GLOBAL_ZONENAME) != 0)
5360 5383 return (usage(B_FALSE));
5361 5384
5362 5385 if ((privset = priv_allocset()) == NULL) {
5363 5386 zerror(gettext("%s failed"), "priv_allocset");
5364 5387 return (Z_ERR);
5365 5388 }
5366 5389
5367 5390 if (getppriv(PRIV_EFFECTIVE, privset) != 0) {
5368 5391 zerror(gettext("%s failed"), "getppriv");
5369 5392 priv_freeset(privset);
5370 5393 return (Z_ERR);
5371 5394 }
5372 5395
5373 5396 if (priv_isfullset(privset) == B_FALSE) {
5374 5397 (void) usage(B_FALSE);
5375 5398 priv_freeset(privset);
5376 5399 return (Z_ERR);
5377 5400 }
5378 5401 priv_freeset(privset);
5379 5402
5380 5403 if ((handle = zonecfg_init_handle()) == NULL) {
5381 5404 zperror(cmd_to_str(CMD_APPLY), B_TRUE);
5382 5405 return (Z_ERR);
5383 5406 }
5384 5407
5385 5408 if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) {
5386 5409 errno = err;
5387 5410 zperror(cmd_to_str(CMD_APPLY), B_TRUE);
5388 5411 zonecfg_fini_handle(handle);
5389 5412 return (Z_ERR);
5390 5413 }
5391 5414
5392 5415 /* specific error msgs are printed within apply_rctls */
5393 5416 if ((err = zonecfg_apply_rctls(target_zone, handle)) != Z_OK) {
5394 5417 errno = err;
5395 5418 zperror(cmd_to_str(CMD_APPLY), B_TRUE);
5396 5419 res = Z_ERR;
5397 5420 }
5398 5421
5399 5422 if ((err = check_cpu_shares_sched(handle)) != Z_OK)
5400 5423 res = Z_ERR;
5401 5424
5402 5425 if (pool_configured(handle)) {
5403 5426 if (mixed_pools(zoneid)) {
5404 5427 zerror(gettext("Zone is using multiple resource "
5405 5428 "pools. The pool\nconfiguration cannot be "
5406 5429 "applied without rebooting."));
5407 5430 res = Z_ERR;
5408 5431 } else {
5409 5432
5410 5433 /*
5411 5434 * The next two blocks of code attempt to set up
5412 5435 * temporary pools as well as persistent pools. In
5413 5436 * both cases we call the functions unconditionally.
5414 5437 * Within each funtion the code will check if the zone
5415 5438 * is actually configured for a temporary pool or
5416 5439 * persistent pool and just return if there is nothing
5417 5440 * to do.
5418 5441 */
5419 5442 if ((err = zonecfg_bind_tmp_pool(handle, zoneid,
5420 5443 pool_err, sizeof (pool_err))) != Z_OK) {
5421 5444 if (err == Z_POOL || err == Z_POOL_CREATE ||
5422 5445 err == Z_POOL_BIND)
5423 5446 zerror("%s: %s", zonecfg_strerror(err),
5424 5447 pool_err);
5425 5448 else
5426 5449 zerror(gettext("could not bind zone to "
5427 5450 "temporary pool: %s"),
5428 5451 zonecfg_strerror(err));
5429 5452 res = Z_ERR;
5430 5453 }
5431 5454
5432 5455 if ((err = zonecfg_bind_pool(handle, zoneid, pool_err,
5433 5456 sizeof (pool_err))) != Z_OK) {
5434 5457 if (err == Z_POOL || err == Z_POOL_BIND)
5435 5458 zerror("%s: %s", zonecfg_strerror(err),
5436 5459 pool_err);
5437 5460 else
5438 5461 zerror("%s", zonecfg_strerror(err));
5439 5462 }
5440 5463 }
5441 5464 }
5442 5465
5443 5466 /*
5444 5467 * If a memory cap is configured, set the cap in the kernel using
5445 5468 * zone_setattr() and make sure the rcapd SMF service is enabled.
5446 5469 */
5447 5470 if (zonecfg_getmcapent(handle, &mcap) == Z_OK) {
5448 5471 uint64_t num;
5449 5472 char smf_err[128];
5450 5473
5451 5474 num = (uint64_t)strtoll(mcap.zone_physmem_cap, NULL, 10);
5452 5475 if (zone_setattr(zoneid, ZONE_ATTR_PHYS_MCAP, &num, 0) == -1) {
5453 5476 zerror(gettext("could not set zone memory cap"));
5454 5477 res = Z_ERR;
5455 5478 }
5456 5479
5457 5480 if (zonecfg_enable_rcapd(smf_err, sizeof (smf_err)) != Z_OK) {
5458 5481 zerror(gettext("enabling system/rcap service failed: "
5459 5482 "%s"), smf_err);
5460 5483 res = Z_ERR;
5461 5484 }
5462 5485 }
5463 5486
5464 5487 zonecfg_fini_handle(handle);
5465 5488
5466 5489 return (res);
5467 5490 }
5468 5491
5469 5492 /*
5470 5493 * This is an undocumented interface that is invoked by the zones SMF service
5471 5494 * for installed zones that won't automatically boot.
5472 5495 */
5473 5496 /* ARGSUSED */
5474 5497 static int
5475 5498 sysboot_func(int argc, char *argv[])
5476 5499 {
5477 5500 int err;
5478 5501 zone_dochandle_t zone_handle;
5479 5502 brand_handle_t brand_handle;
5480 5503 char cmdbuf[MAXPATHLEN];
5481 5504 char zonepath[MAXPATHLEN];
5482 5505
5483 5506 /*
5484 5507 * This subcommand can only be executed in the global zone on non-global
5485 5508 * zones.
5486 5509 */
5487 5510 if (zonecfg_in_alt_root())
5488 5511 return (usage(B_FALSE));
5489 5512 if (sanity_check(target_zone, CMD_SYSBOOT, B_FALSE, B_TRUE, B_FALSE) !=
5490 5513 Z_OK)
5491 5514 return (Z_ERR);
5492 5515
5493 5516 /*
5494 5517 * Fetch the sysboot hook from the target zone's brand.
5495 5518 */
5496 5519 if ((zone_handle = zonecfg_init_handle()) == NULL) {
5497 5520 zperror(cmd_to_str(CMD_SYSBOOT), B_TRUE);
5498 5521 return (Z_ERR);
5499 5522 }
5500 5523 if ((err = zonecfg_get_handle(target_zone, zone_handle)) != Z_OK) {
5501 5524 errno = err;
5502 5525 zperror(cmd_to_str(CMD_SYSBOOT), B_TRUE);
5503 5526 zonecfg_fini_handle(zone_handle);
5504 5527 return (Z_ERR);
5505 5528 }
5506 5529 if ((err = zonecfg_get_zonepath(zone_handle, zonepath,
5507 5530 sizeof (zonepath))) != Z_OK) {
5508 5531 errno = err;
5509 5532 zperror(cmd_to_str(CMD_SYSBOOT), B_TRUE);
5510 5533 zonecfg_fini_handle(zone_handle);
5511 5534 return (Z_ERR);
5512 5535 }
5513 5536 if ((brand_handle = brand_open(target_brand)) == NULL) {
5514 5537 zerror(gettext("missing or invalid brand during %s operation: "
5515 5538 "%s"), cmd_to_str(CMD_SYSBOOT), target_brand);
5516 5539 zonecfg_fini_handle(zone_handle);
5517 5540 return (Z_ERR);
5518 5541 }
5519 5542 err = get_hook(brand_handle, cmdbuf, sizeof (cmdbuf), brand_get_sysboot,
5520 5543 target_zone, zonepath);
5521 5544 brand_close(brand_handle);
5522 5545 zonecfg_fini_handle(zone_handle);
5523 5546 if (err != Z_OK) {
5524 5547 zerror(gettext("unable to get brand hook from brand %s for %s "
5525 5548 "operation"), target_brand, cmd_to_str(CMD_SYSBOOT));
5526 5549 return (Z_ERR);
5527 5550 }
5528 5551
5529 5552 /*
5530 5553 * If the hook wasn't defined (which is OK), then indicate success and
5531 5554 * return. Otherwise, execute the hook.
5532 5555 */
5533 5556 if (cmdbuf[0] != '\0')
5534 5557 return ((subproc_status(gettext("brand sysboot operation"),
5535 5558 do_subproc(cmdbuf), B_FALSE) == ZONE_SUBPROC_OK) ? Z_OK :
5536 5559 Z_BRAND_ERROR);
5537 5560 return (Z_OK);
5538 5561 }
5539 5562
5540 5563 static int
5541 5564 help_func(int argc, char *argv[])
5542 5565 {
5543 5566 int arg, cmd_num;
5544 5567
5545 5568 if (argc == 0) {
5546 5569 (void) usage(B_TRUE);
5547 5570 return (Z_OK);
5548 5571 }
5549 5572 optind = 0;
5550 5573 if ((arg = getopt(argc, argv, "?")) != EOF) {
5551 5574 switch (arg) {
5552 5575 case '?':
5553 5576 sub_usage(SHELP_HELP, CMD_HELP);
5554 5577 return (optopt == '?' ? Z_OK : Z_USAGE);
5555 5578 default:
5556 5579 sub_usage(SHELP_HELP, CMD_HELP);
5557 5580 return (Z_USAGE);
5558 5581 }
5559 5582 }
5560 5583 while (optind < argc) {
5561 5584 /* Private commands have NULL short_usage; omit them */
5562 5585 if ((cmd_num = cmd_match(argv[optind])) < 0 ||
5563 5586 cmdtab[cmd_num].short_usage == NULL) {
5564 5587 sub_usage(SHELP_HELP, CMD_HELP);
5565 5588 return (Z_USAGE);
5566 5589 }
5567 5590 sub_usage(cmdtab[cmd_num].short_usage, cmd_num);
5568 5591 optind++;
5569 5592 }
5570 5593 return (Z_OK);
5571 5594 }
5572 5595
5573 5596 /*
5574 5597 * Returns: CMD_MIN thru CMD_MAX on success, -1 on error
5575 5598 */
5576 5599
5577 5600 static int
5578 5601 cmd_match(char *cmd)
5579 5602 {
5580 5603 int i;
5581 5604
5582 5605 for (i = CMD_MIN; i <= CMD_MAX; i++) {
5583 5606 /* return only if there is an exact match */
5584 5607 if (strcmp(cmd, cmdtab[i].cmd_name) == 0)
5585 5608 return (cmdtab[i].cmd_num);
5586 5609 }
5587 5610 return (-1);
5588 5611 }
5589 5612
5590 5613 static int
5591 5614 parse_and_run(int argc, char *argv[])
5592 5615 {
5593 5616 int i = cmd_match(argv[0]);
5594 5617
5595 5618 if (i < 0)
5596 5619 return (usage(B_FALSE));
5597 5620 return (cmdtab[i].handler(argc - 1, &(argv[1])));
5598 5621 }
5599 5622
5600 5623 static char *
5601 5624 get_execbasename(char *execfullname)
5602 5625 {
5603 5626 char *last_slash, *execbasename;
5604 5627
5605 5628 /* guard against '/' at end of command invocation */
5606 5629 for (;;) {
5607 5630 last_slash = strrchr(execfullname, '/');
5608 5631 if (last_slash == NULL) {
5609 5632 execbasename = execfullname;
5610 5633 break;
5611 5634 } else {
5612 5635 execbasename = last_slash + 1;
5613 5636 if (*execbasename == '\0') {
5614 5637 *last_slash = '\0';
5615 5638 continue;
5616 5639 }
5617 5640 break;
5618 5641 }
5619 5642 }
5620 5643 return (execbasename);
5621 5644 }
5622 5645
5623 5646 static char *
5624 5647 get_username()
5625 5648 {
5626 5649 uid_t uid;
5627 5650 struct passwd *nptr;
5628 5651
5629 5652
5630 5653 /*
5631 5654 * Authorizations are checked to restrict access based on the
5632 5655 * requested operation and zone name, It is assumed that the
5633 5656 * program is running with all privileges, but that the real
5634 5657 * user ID is that of the user or role on whose behalf we are
5635 5658 * operating. So we start by getting the username that will be
5636 5659 * used for subsequent authorization checks.
5637 5660 */
5638 5661
5639 5662 uid = getuid();
5640 5663 if ((nptr = getpwuid(uid)) == NULL) {
5641 5664 zerror(gettext("could not get user name."));
5642 5665 exit(Z_ERR);
5643 5666 }
5644 5667 return (nptr->pw_name);
5645 5668 }
5646 5669
5647 5670 int
5648 5671 main(int argc, char **argv)
5649 5672 {
5650 5673 int arg;
5651 5674 zoneid_t zid;
5652 5675 struct stat st;
5653 5676 char *zone_lock_env;
5654 5677 int err;
5655 5678
5656 5679 if ((locale = setlocale(LC_ALL, "")) == NULL)
5657 5680 locale = "C";
5658 5681 (void) textdomain(TEXT_DOMAIN);
5659 5682 setbuf(stdout, NULL);
5660 5683 (void) sigset(SIGHUP, SIG_IGN);
5661 5684 execname = get_execbasename(argv[0]);
5662 5685 username = get_username();
5663 5686 target_zone = NULL;
5664 5687 if (chdir("/") != 0) {
5665 5688 zerror(gettext("could not change directory to /."));
5666 5689 exit(Z_ERR);
5667 5690 }
5668 5691
5669 5692 /*
5670 5693 * Use the default system mask rather than anything that may have been
5671 5694 * set by the caller.
5672 5695 */
5673 5696 (void) umask(CMASK);
5674 5697
5675 5698 if (init_zfs() != Z_OK)
5676 5699 exit(Z_ERR);
5677 5700
5678 5701 while ((arg = getopt(argc, argv, "?u:z:R:")) != EOF) {
5679 5702 switch (arg) {
5680 5703 case '?':
5681 5704 return (usage(B_TRUE));
5682 5705 case 'u':
5683 5706 target_uuid = optarg;
5684 5707 break;
5685 5708 case 'z':
5686 5709 target_zone = optarg;
5687 5710 break;
5688 5711 case 'R': /* private option for admin/install use */
5689 5712 if (*optarg != '/') {
5690 5713 zerror(gettext("root path must be absolute."));
5691 5714 exit(Z_ERR);
5692 5715 }
5693 5716 if (stat(optarg, &st) == -1 || !S_ISDIR(st.st_mode)) {
5694 5717 zerror(
5695 5718 gettext("root path must be a directory."));
5696 5719 exit(Z_ERR);
5697 5720 }
5698 5721 zonecfg_set_root(optarg);
5699 5722 break;
5700 5723 default:
5701 5724 return (usage(B_FALSE));
5702 5725 }
5703 5726 }
5704 5727
5705 5728 if (optind >= argc)
5706 5729 return (usage(B_FALSE));
5707 5730
5708 5731 if (target_uuid != NULL && *target_uuid != '\0') {
5709 5732 uuid_t uuid;
5710 5733 static char newtarget[ZONENAME_MAX];
5711 5734
5712 5735 if (uuid_parse(target_uuid, uuid) == -1) {
5713 5736 zerror(gettext("illegal UUID value specified"));
5714 5737 exit(Z_ERR);
5715 5738 }
5716 5739 if (zonecfg_get_name_by_uuid(uuid, newtarget,
5717 5740 sizeof (newtarget)) == Z_OK)
5718 5741 target_zone = newtarget;
5719 5742 }
5720 5743
5721 5744 if (target_zone != NULL && zone_get_id(target_zone, &zid) != 0) {
5722 5745 errno = Z_NO_ZONE;
5723 5746 zperror(target_zone, B_TRUE);
5724 5747 exit(Z_ERR);
5725 5748 }
5726 5749
5727 5750 /*
5728 5751 * See if we have inherited the right to manipulate this zone from
5729 5752 * a zoneadm instance in our ancestry. If so, set zone_lock_cnt to
5730 5753 * indicate it. If not, make that explicit in our environment.
5731 5754 */
5732 5755 zonecfg_init_lock_file(target_zone, &zone_lock_env);
5733 5756
5734 5757 /* Figure out what the system's default brand is */
5735 5758 if (zonecfg_default_brand(default_brand,
5736 5759 sizeof (default_brand)) != Z_OK) {
5737 5760 zerror(gettext("unable to determine default brand"));
5738 5761 return (Z_ERR);
5739 5762 }
5740 5763
5741 5764 /*
5742 5765 * If we are going to be operating on a single zone, retrieve its
5743 5766 * brand type and determine whether it is native or not.
5744 5767 */
5745 5768 if ((target_zone != NULL) &&
5746 5769 (strcmp(target_zone, GLOBAL_ZONENAME) != 0)) {
5747 5770 if (zone_get_brand(target_zone, target_brand,
5748 5771 sizeof (target_brand)) != Z_OK) {
5749 5772 zerror(gettext("missing or invalid brand"));
5750 5773 exit(Z_ERR);
5751 5774 }
5752 5775 /*
5753 5776 * In the alternate root environment, the only supported
5754 5777 * operations are mount and unmount. In this case, just treat
5755 5778 * the zone as native if it is cluster. Cluster zones can be
5756 5779 * native for the purpose of LU or upgrade, and the cluster
5757 5780 * brand may not exist in the miniroot (such as in net install
5758 5781 * upgrade).
5759 5782 */
5760 5783 if (strcmp(target_brand, CLUSTER_BRAND_NAME) == 0) {
5761 5784 if (zonecfg_in_alt_root()) {
5762 5785 (void) strlcpy(target_brand, default_brand,
5763 5786 sizeof (target_brand));
5764 5787 }
5765 5788 }
5766 5789 }
5767 5790
5768 5791 err = parse_and_run(argc - optind, &argv[optind]);
5769 5792
5770 5793 return (err);
5771 5794 }
↓ open down ↓ |
4725 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX