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