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