1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 22 /* 23 * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 #pragma ident "%Z%%M% %I% %E% SMI" 28 29 /* 30 * svccfg(1) interpreter and command execution engine. 31 */ 32 33 #include <sys/mman.h> 34 #include <sys/stat.h> 35 #include <sys/types.h> 36 #include <assert.h> 37 #include <errno.h> 38 #include <libintl.h> 39 #include <libtecla.h> 40 #include <md5.h> 41 #include <string.h> 42 #include <stdlib.h> 43 #include <unistd.h> 44 45 #include "manifest_hash.h" 46 #include "svccfg.h" 47 48 #define MS_PER_US 1000 49 50 engine_state_t *est; 51 52 /* 53 * Replacement lex(1) character retrieval routines. 54 */ 55 int 56 engine_cmd_getc(engine_state_t *E) 57 { 58 if (E->sc_cmd_file != NULL) 59 return (getc(E->sc_cmd_file)); 60 61 if (E->sc_cmd_flags & SC_CMD_EOF) 62 return (EOF); 63 64 if (E->sc_cmd_bufoff < E->sc_cmd_bufsz) 65 return (*(E->sc_cmd_buf + E->sc_cmd_bufoff++)); 66 67 if (!(E->sc_cmd_flags & SC_CMD_IACTIVE)) { 68 E->sc_cmd_flags |= SC_CMD_EOF; 69 70 return (EOF); 71 } else { 72 #ifdef NATIVE_BUILD 73 return (EOF); 74 #else 75 extern int parens; 76 77 if (parens <= 0) { 78 E->sc_cmd_flags |= SC_CMD_EOF; 79 return (EOF); 80 } 81 82 for (;;) { 83 E->sc_cmd_buf = gl_get_line(E->sc_gl, "> ", NULL, -1); 84 if (E->sc_cmd_buf != NULL) 85 break; 86 87 switch (gl_return_status(E->sc_gl)) { 88 case GLR_SIGNAL: 89 gl_abandon_line(E->sc_gl); 90 continue; 91 92 case GLR_EOF: 93 E->sc_cmd_flags |= SC_CMD_EOF; 94 return (EOF); 95 96 case GLR_ERROR: 97 uu_die(gettext("Error reading terminal: %s.\n"), 98 gl_error_message(E->sc_gl, NULL, 0)); 99 /* NOTREACHED */ 100 101 default: 102 #ifndef NDEBUG 103 (void) fprintf(stderr, "%s:%d: gl_get_line() " 104 "returned unexpected value %d.\n", __FILE__, 105 __LINE__, gl_return_status(E->sc_gl)); 106 #endif 107 abort(); 108 } 109 } 110 111 E->sc_cmd_bufsz = strlen(E->sc_cmd_buf); 112 E->sc_cmd_bufoff = 1; 113 114 return (E->sc_cmd_buf[0]); 115 #endif /* NATIVE_BUILD */ 116 } 117 } 118 119 int 120 engine_cmd_ungetc(engine_state_t *E, char c) 121 { 122 if (E->sc_cmd_file != NULL) 123 return (ungetc(c, E->sc_cmd_file)); 124 125 if (E->sc_cmd_buf != NULL) 126 *(E->sc_cmd_buf + --E->sc_cmd_bufoff) = c; 127 128 return (c); 129 } 130 131 /*ARGSUSED*/ 132 void 133 engine_cmd_nputs(engine_state_t *E, char *c, size_t n) 134 { 135 /* our lexer shouldn't need this state */ 136 exit(11); 137 } 138 139 int 140 engine_exec(char *cmd) 141 { 142 est->sc_cmd_buf = cmd; 143 est->sc_cmd_bufsz = strlen(cmd) + 1; 144 est->sc_cmd_bufoff = 0; 145 146 (void) yyparse(); 147 148 return (0); 149 } 150 151 #ifndef NATIVE_BUILD 152 /* ARGSUSED */ 153 static 154 CPL_CHECK_FN(check_xml) 155 { 156 const char *ext; 157 158 if (strlen(pathname) < 4) 159 return (0); 160 161 ext = pathname + strlen(pathname) - 4; 162 163 return (strcmp(ext, ".xml") == 0 ? 1 : 0); 164 } 165 166 static const char * const whitespace = " \t"; 167 168 static 169 CPL_MATCH_FN(complete_single_xml_file_arg) 170 { 171 const char *arg1 = data; 172 int arg1end_i, ret; 173 CplFileConf *cfc; 174 175 arg1end_i = arg1 + strcspn(arg1, whitespace) - line; 176 if (arg1end_i < word_end) 177 return (0); 178 179 cfc = new_CplFileConf(); 180 if (cfc == NULL) { 181 cpl_record_error(cpl, "Out of memory."); 182 return (1); 183 } 184 185 cfc_set_check_fn(cfc, check_xml, NULL); 186 187 ret = cpl_file_completions(cpl, cfc, line, word_end); 188 189 (void) del_CplFileConf(cfc); 190 return (ret); 191 } 192 193 static struct cmd_info { 194 const char *name; 195 uint32_t flags; 196 CplMatchFn *complete_args_f; 197 } cmds[] = { 198 { "validate", CS_GLOBAL, complete_single_xml_file_arg }, 199 { "import", CS_GLOBAL, complete_single_xml_file_arg }, 200 { "export", CS_GLOBAL, NULL }, 201 { "archive", CS_GLOBAL, NULL }, 202 { "apply", CS_GLOBAL, complete_single_xml_file_arg }, 203 { "extract", CS_GLOBAL, NULL }, 204 { "repository", CS_GLOBAL, NULL }, 205 { "inventory", CS_GLOBAL, complete_single_xml_file_arg }, 206 { "set", CS_GLOBAL, NULL }, 207 { "end", CS_GLOBAL, NULL }, 208 { "exit", CS_GLOBAL, NULL }, 209 { "quit", CS_GLOBAL, NULL }, 210 { "help", CS_GLOBAL, NULL }, 211 { "delete", CS_GLOBAL, NULL }, 212 { "select", CS_GLOBAL, complete_select }, 213 { "unselect", CS_SVC | CS_INST | CS_SNAP, NULL }, 214 { "list", CS_SCOPE | CS_SVC | CS_SNAP, NULL }, 215 { "add", CS_SCOPE | CS_SVC, NULL }, 216 { "listpg", CS_SVC | CS_INST | CS_SNAP, NULL }, 217 { "addpg", CS_SVC | CS_INST, NULL }, 218 { "delpg", CS_SVC | CS_INST, NULL }, 219 { "listprop", CS_SVC | CS_INST | CS_SNAP, NULL }, 220 { "setprop", CS_SVC | CS_INST, NULL }, 221 { "delprop", CS_SVC | CS_INST, NULL }, 222 { "editprop", CS_SVC | CS_INST, NULL }, 223 { "listsnap", CS_INST | CS_SNAP, NULL }, 224 { "selectsnap", CS_INST | CS_SNAP, NULL }, 225 { "revert", CS_INST | CS_SNAP, NULL }, 226 { NULL } 227 }; 228 229 int 230 add_cmd_matches(WordCompletion *cpl, const char *line, int word_end, 231 uint32_t scope) 232 { 233 int word_start, err; 234 size_t len; 235 const char *bol; 236 struct cmd_info *cip; 237 238 word_start = strspn(line, whitespace); 239 len = word_end - word_start; 240 bol = line + word_end - len; 241 242 for (cip = cmds; cip->name != NULL; ++cip) { 243 if ((cip->flags & scope) == 0) 244 continue; 245 246 if (strncmp(cip->name, bol, len) == 0) { 247 err = cpl_add_completion(cpl, line, word_start, 248 word_end, cip->name + len, "", " "); 249 if (err != 0) 250 return (err); 251 } 252 } 253 254 return (0); 255 } 256 257 /* 258 * Suggest completions. We must first determine if the cursor is in command 259 * position or in argument position. If the former, complete_command() finds 260 * matching commands. If the latter, we tail-call the command-specific 261 * argument-completion routine in the cmds table. 262 */ 263 /* ARGSUSED */ 264 static 265 CPL_MATCH_FN(complete) 266 { 267 const char *arg0, *arg1; 268 size_t arg0len; 269 struct cmd_info *cip; 270 271 arg0 = line + strspn(line, whitespace); 272 arg0len = strcspn(arg0, whitespace); 273 if ((arg0 + arg0len) - line >= word_end || 274 (arg0[arg0len] != ' ' && arg0[arg0len] != '\t')) 275 return (complete_command(cpl, (void *)arg0, line, word_end)); 276 277 arg1 = arg0 + arg0len; 278 arg1 += strspn(arg1, whitespace); 279 280 for (cip = cmds; cip->name != NULL; ++cip) { 281 if (strlen(cip->name) != arg0len) 282 continue; 283 284 if (strncmp(cip->name, arg0, arg0len) != 0) 285 continue; 286 287 if (cip->complete_args_f == NULL) 288 break; 289 290 return (cip->complete_args_f(cpl, (void *)arg1, line, 291 word_end)); 292 } 293 294 return (0); 295 } 296 #endif /* NATIVE_BUILD */ 297 298 int 299 engine_interp() 300 { 301 #ifdef NATIVE_BUILD 302 uu_die("native build does not support interactive mode."); 303 #else 304 char *selfmri; 305 size_t sfsz; 306 int r; 307 308 extern int parens; 309 310 (void) sigset(SIGINT, SIG_IGN); 311 312 est->sc_gl = new_GetLine(512, 8000); 313 if (est->sc_gl == NULL) 314 uu_die(gettext("Out of memory.\n")); 315 316 /* The longest string is "[snapname]fmri[:instname]> ". */ 317 sfsz = 1 + max_scf_name_len + 1 + max_scf_fmri_len + 2 + 318 max_scf_name_len + 1 + 2 + 1; 319 selfmri = safe_malloc(sfsz); 320 321 r = gl_customize_completion(est->sc_gl, NULL, complete); 322 assert(r == 0); 323 324 for (;;) { 325 lscf_get_selection_str(selfmri, sfsz - 2); 326 (void) strcat(selfmri, "> "); 327 est->sc_cmd_buf = gl_get_line(est->sc_gl, selfmri, NULL, -1); 328 329 if (est->sc_cmd_buf == NULL) { 330 switch (gl_return_status(est->sc_gl)) { 331 case GLR_SIGNAL: 332 gl_abandon_line(est->sc_gl); 333 continue; 334 335 case GLR_EOF: 336 break; 337 338 case GLR_ERROR: 339 uu_die(gettext("Error reading terminal: %s.\n"), 340 gl_error_message(est->sc_gl, NULL, 0)); 341 /* NOTREACHED */ 342 343 default: 344 #ifndef NDEBUG 345 (void) fprintf(stderr, "%s:%d: gl_get_line() " 346 "returned unexpected value %d.\n", __FILE__, 347 __LINE__, gl_return_status(est->sc_gl)); 348 #endif 349 abort(); 350 } 351 352 break; 353 } 354 355 parens = 0; 356 est->sc_cmd_bufsz = strlen(est->sc_cmd_buf); 357 est->sc_cmd_bufoff = 0; 358 est->sc_cmd_flags = SC_CMD_IACTIVE; 359 360 (void) yyparse(); 361 } 362 363 free(selfmri); 364 est->sc_gl = del_GetLine(est->sc_gl); /* returns NULL */ 365 366 #endif /* NATIVE_BUILD */ 367 return (0); 368 } 369 370 int 371 engine_source(const char *name, boolean_t dont_exit) 372 { 373 engine_state_t *old = est; 374 struct stat st; 375 int ret; 376 377 est = uu_zalloc(sizeof (engine_state_t)); 378 379 /* first, copy the stuff set up in engine_init */ 380 est->sc_repo_pid = old->sc_repo_pid; 381 if (old->sc_repo_filename != NULL) 382 est->sc_repo_filename = safe_strdup(old->sc_repo_filename); 383 if (old->sc_repo_doordir != NULL) 384 est->sc_repo_doordir = safe_strdup(old->sc_repo_doordir); 385 if (old->sc_repo_doorname != NULL) 386 est->sc_repo_doorname = safe_strdup(old->sc_repo_doorname); 387 if (old->sc_repo_server != NULL) 388 est->sc_repo_server = safe_strdup(old->sc_repo_server); 389 390 /* set up the new guy */ 391 est->sc_cmd_lineno = 1; 392 393 if (dont_exit) 394 est->sc_cmd_flags |= SC_CMD_DONT_EXIT; 395 396 if (strcmp(name, "-") == 0) { 397 est->sc_cmd_file = stdin; 398 est->sc_cmd_filename = "<stdin>"; 399 } else { 400 errno = 0; 401 est->sc_cmd_filename = name; 402 est->sc_cmd_file = fopen(name, "r"); 403 if (est->sc_cmd_file == NULL) { 404 if (errno == 0) 405 semerr(gettext("No free stdio streams.\n")); 406 else 407 semerr(gettext("Could not open %s"), name); 408 409 ret = -1; 410 goto fail; 411 } 412 413 do { 414 ret = fstat(fileno(est->sc_cmd_file), &st); 415 } while (ret != 0 && errno == EINTR); 416 if (ret != 0) { 417 (void) fclose(est->sc_cmd_file); 418 est->sc_cmd_file = NULL; /* for semerr() */ 419 420 semerr(gettext("Could not stat %s"), name); 421 422 ret = -1; 423 goto fail; 424 } 425 426 if (!S_ISREG(st.st_mode)) { 427 (void) fclose(est->sc_cmd_file); 428 est->sc_cmd_file = NULL; /* for semerr() */ 429 430 semerr(gettext("%s is not a regular file.\n"), name); 431 432 ret = -1; 433 goto fail; 434 } 435 } 436 437 (void) yyparse(); 438 439 if (est->sc_cmd_file != stdin) 440 (void) fclose(est->sc_cmd_file); 441 442 ret = 0; 443 444 fail: 445 if (est->sc_repo_pid != old->sc_repo_pid) 446 lscf_cleanup(); /* clean up any new repository */ 447 448 if (est->sc_repo_filename != NULL) 449 free((void *)est->sc_repo_filename); 450 if (est->sc_repo_doordir != NULL) 451 free((void *)est->sc_repo_doordir); 452 if (est->sc_repo_doorname != NULL) 453 free((void *)est->sc_repo_doorname); 454 if (est->sc_repo_server != NULL) 455 free((void *)est->sc_repo_server); 456 free(est); 457 458 est = old; 459 460 return (ret); 461 } 462 463 /* 464 * Initialize svccfg state. We recognize four environment variables: 465 * 466 * SVCCFG_REPOSITORY Create a private instance of svc.configd(1M) to answer 467 * requests for the specified repository file. 468 * SVCCFG_DOOR_PATH Directory for door creation. 469 * 470 * SVCCFG_DOOR Rendezvous via an alternative repository door. 471 * 472 * SVCCFG_CONFIGD_PATH Resolvable path to alternative svc.configd(1M) binary. 473 */ 474 void 475 engine_init() 476 { 477 const char *cp; 478 479 est = uu_zalloc(sizeof (engine_state_t)); 480 481 est->sc_cmd_lineno = 1; 482 est->sc_repo_pid = -1; 483 484 cp = getenv("SVCCFG_REPOSITORY"); 485 est->sc_repo_filename = cp ? safe_strdup(cp) : NULL; 486 487 cp = getenv("SVCCFG_DOOR_PATH"); 488 est->sc_repo_doordir = cp ? cp : "/var/run"; 489 490 cp = getenv("SVCCFG_DOOR"); 491 if (cp != NULL) { 492 if (est->sc_repo_filename != NULL) { 493 uu_warn(gettext("SVCCFG_DOOR unused when " 494 "SVCCFG_REPOSITORY specified\n")); 495 } else { 496 est->sc_repo_doorname = safe_strdup(cp); 497 } 498 } 499 500 cp = getenv("SVCCFG_CONFIGD_PATH"); 501 est->sc_repo_server = cp ? cp : "/lib/svc/bin/svc.configd"; 502 } 503 504 int 505 engine_import(uu_list_t *args) 506 { 507 int ret, argc, i, o; 508 bundle_t *b; 509 char *file, *pname; 510 uchar_t hash[MHASH_SIZE]; 511 char **argv; 512 string_list_t *slp; 513 boolean_t verify = B_FALSE; 514 uint_t flags = SCI_GENERALLAST; 515 516 argc = uu_list_numnodes(args); 517 if (argc < 1) 518 return (-2); 519 520 argv = calloc(argc + 1, sizeof (char *)); 521 if (argv == NULL) 522 uu_die(gettext("Out of memory.\n")); 523 524 for (slp = uu_list_first(args), i = 0; 525 slp != NULL; 526 slp = uu_list_next(args, slp), ++i) 527 argv[i] = slp->str; 528 529 argv[i] = NULL; 530 531 opterr = 0; 532 optind = 0; /* Remember, no argv[0]. */ 533 for (;;) { 534 o = getopt(argc, argv, "nV"); 535 if (o == -1) 536 break; 537 538 switch (o) { 539 case 'n': 540 flags |= SCI_NOREFRESH; 541 break; 542 543 case 'V': 544 verify = B_TRUE; 545 break; 546 547 case '?': 548 free(argv); 549 return (-2); 550 551 default: 552 bad_error("getopt", o); 553 } 554 } 555 556 argc -= optind; 557 if (argc != 1) { 558 free(argv); 559 return (-2); 560 } 561 562 file = argv[optind]; 563 free(argv); 564 565 lscf_prep_hndl(); 566 567 ret = mhash_test_file(g_hndl, file, 0, &pname, hash); 568 if (ret != MHASH_NEWFILE) 569 return (ret); 570 571 /* Load */ 572 b = internal_bundle_new(); 573 574 if (lxml_get_bundle_file(b, file, SVCCFG_OP_IMPORT) != 0) { 575 internal_bundle_free(b); 576 return (-1); 577 } 578 579 /* Import */ 580 if (lscf_bundle_import(b, file, flags) != 0) { 581 internal_bundle_free(b); 582 return (-1); 583 } 584 585 internal_bundle_free(b); 586 587 if (g_verbose) 588 warn(gettext("Successful import.\n")); 589 590 if (pname) { 591 char *errstr; 592 593 if (mhash_store_entry(g_hndl, pname, hash, &errstr)) { 594 if (errstr) 595 semerr(errstr); 596 else 597 semerr(gettext("Unknown error from " 598 "mhash_store_entry()\n")); 599 } 600 601 free(pname); 602 } 603 604 /* Verify */ 605 if (verify) 606 warn(gettext("import -V not implemented.\n")); 607 608 return (0); 609 } 610 611 int 612 engine_apply(const char *file) 613 { 614 int ret; 615 bundle_t *b; 616 char *pname; 617 uchar_t hash[MHASH_SIZE]; 618 619 lscf_prep_hndl(); 620 621 ret = mhash_test_file(g_hndl, file, 1, &pname, hash); 622 if (ret != MHASH_NEWFILE) 623 return (ret); 624 625 b = internal_bundle_new(); 626 627 if (lxml_get_bundle_file(b, file, SVCCFG_OP_APPLY) != 0) { 628 internal_bundle_free(b); 629 return (-1); 630 } 631 632 if (lscf_bundle_apply(b) != 0) { 633 internal_bundle_free(b); 634 return (-1); 635 } 636 637 internal_bundle_free(b); 638 639 if (pname) { 640 char *errstr; 641 if (mhash_store_entry(g_hndl, pname, hash, &errstr)) 642 semerr(errstr); 643 644 free(pname); 645 } 646 647 return (0); 648 } 649 650 int 651 engine_restore(const char *file) 652 { 653 bundle_t *b; 654 655 lscf_prep_hndl(); 656 657 b = internal_bundle_new(); 658 659 if (lxml_get_bundle_file(b, file, SVCCFG_OP_RESTORE) != 0) { 660 internal_bundle_free(b); 661 return (-1); 662 } 663 664 if (lscf_bundle_import(b, file, SCI_NOSNAP) != 0) { 665 internal_bundle_free(b); 666 return (-1); 667 } 668 669 internal_bundle_free(b); 670 671 return (0); 672 } 673 674 int 675 engine_set(uu_list_t *args) 676 { 677 uu_list_walk_t *walk; 678 string_list_t *slp; 679 680 if (uu_list_first(args) == NULL) { 681 /* Display current options. */ 682 if (!g_verbose) 683 (void) fputs("no", stdout); 684 (void) puts("verbose"); 685 686 return (0); 687 } 688 689 walk = uu_list_walk_start(args, UU_DEFAULT); 690 if (walk == NULL) 691 uu_die(gettext("Couldn't read arguments")); 692 693 /* Use getopt? */ 694 for (slp = uu_list_walk_next(walk); 695 slp != NULL; 696 slp = uu_list_walk_next(walk)) { 697 if (slp->str[0] == '-') { 698 char *op; 699 700 for (op = &slp->str[1]; *op != '\0'; ++op) { 701 switch (*op) { 702 case 'v': 703 g_verbose = 1; 704 break; 705 706 case 'V': 707 g_verbose = 0; 708 break; 709 710 default: 711 warn(gettext("Unknown option -%c.\n"), 712 *op); 713 } 714 } 715 } else { 716 warn(gettext("No non-flag arguments defined.\n")); 717 } 718 } 719 720 return (0); 721 } 722 723 void 724 help(int com) 725 { 726 int i; 727 728 if (com == 0) { 729 warn(gettext("General commands: help set repository end\n" 730 "Manifest commands: inventory validate import export " 731 "archive\n" 732 "Profile commands: apply extract\n" 733 "Entity commands: list select unselect add delete\n" 734 "Snapshot commands: listsnap selectsnap revert\n" 735 "Property group commands: listpg addpg delpg\n" 736 "Property commands: listprop setprop delprop editprop\n" 737 "Property value commands: addpropvalue delpropvalue " 738 "setenv unsetenv\n")); 739 return; 740 } 741 742 for (i = 0; help_messages[i].message != NULL; ++i) { 743 if (help_messages[i].token == com) { 744 warn(gettext("Usage: %s\n"), 745 gettext(help_messages[i].message)); 746 return; 747 } 748 } 749 750 warn(gettext("Unknown command.\n")); 751 } 752