10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 51061Scasper * Common Development and Distribution License (the "License"). 61061Scasper * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 215040Swesolows 220Sstevel@tonic-gate /* 235777Stw21770 * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 240Sstevel@tonic-gate * Use is subject to license terms. 250Sstevel@tonic-gate */ 260Sstevel@tonic-gate 270Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 280Sstevel@tonic-gate 290Sstevel@tonic-gate /* 300Sstevel@tonic-gate * svccfg(1) interpreter and command execution engine. 310Sstevel@tonic-gate */ 320Sstevel@tonic-gate 330Sstevel@tonic-gate #include <sys/mman.h> 340Sstevel@tonic-gate #include <sys/stat.h> 350Sstevel@tonic-gate #include <sys/types.h> 360Sstevel@tonic-gate #include <assert.h> 370Sstevel@tonic-gate #include <errno.h> 380Sstevel@tonic-gate #include <libintl.h> 390Sstevel@tonic-gate #include <libtecla.h> 400Sstevel@tonic-gate #include <md5.h> 410Sstevel@tonic-gate #include <string.h> 420Sstevel@tonic-gate #include <stdlib.h> 430Sstevel@tonic-gate #include <unistd.h> 440Sstevel@tonic-gate 450Sstevel@tonic-gate #include "manifest_hash.h" 460Sstevel@tonic-gate #include "svccfg.h" 470Sstevel@tonic-gate 480Sstevel@tonic-gate #define MS_PER_US 1000 490Sstevel@tonic-gate 500Sstevel@tonic-gate engine_state_t *est; 510Sstevel@tonic-gate 520Sstevel@tonic-gate /* 530Sstevel@tonic-gate * Replacement lex(1) character retrieval routines. 540Sstevel@tonic-gate */ 550Sstevel@tonic-gate int 560Sstevel@tonic-gate engine_cmd_getc(engine_state_t *E) 570Sstevel@tonic-gate { 580Sstevel@tonic-gate if (E->sc_cmd_file != NULL) 590Sstevel@tonic-gate return (getc(E->sc_cmd_file)); 600Sstevel@tonic-gate 610Sstevel@tonic-gate if (E->sc_cmd_flags & SC_CMD_EOF) 620Sstevel@tonic-gate return (EOF); 630Sstevel@tonic-gate 640Sstevel@tonic-gate if (E->sc_cmd_bufoff < E->sc_cmd_bufsz) 650Sstevel@tonic-gate return (*(E->sc_cmd_buf + E->sc_cmd_bufoff++)); 660Sstevel@tonic-gate 670Sstevel@tonic-gate if (!(E->sc_cmd_flags & SC_CMD_IACTIVE)) { 680Sstevel@tonic-gate E->sc_cmd_flags |= SC_CMD_EOF; 690Sstevel@tonic-gate 700Sstevel@tonic-gate return (EOF); 710Sstevel@tonic-gate } else { 720Sstevel@tonic-gate #ifdef NATIVE_BUILD 730Sstevel@tonic-gate return (EOF); 740Sstevel@tonic-gate #else 750Sstevel@tonic-gate extern int parens; 760Sstevel@tonic-gate 770Sstevel@tonic-gate if (parens <= 0) { 780Sstevel@tonic-gate E->sc_cmd_flags |= SC_CMD_EOF; 790Sstevel@tonic-gate return (EOF); 800Sstevel@tonic-gate } 810Sstevel@tonic-gate 820Sstevel@tonic-gate for (;;) { 830Sstevel@tonic-gate E->sc_cmd_buf = gl_get_line(E->sc_gl, "> ", NULL, -1); 840Sstevel@tonic-gate if (E->sc_cmd_buf != NULL) 850Sstevel@tonic-gate break; 860Sstevel@tonic-gate 870Sstevel@tonic-gate switch (gl_return_status(E->sc_gl)) { 880Sstevel@tonic-gate case GLR_SIGNAL: 890Sstevel@tonic-gate gl_abandon_line(E->sc_gl); 900Sstevel@tonic-gate continue; 910Sstevel@tonic-gate 920Sstevel@tonic-gate case GLR_EOF: 930Sstevel@tonic-gate E->sc_cmd_flags |= SC_CMD_EOF; 940Sstevel@tonic-gate return (EOF); 950Sstevel@tonic-gate 960Sstevel@tonic-gate case GLR_ERROR: 970Sstevel@tonic-gate uu_die(gettext("Error reading terminal: %s.\n"), 980Sstevel@tonic-gate gl_error_message(E->sc_gl, NULL, 0)); 990Sstevel@tonic-gate /* NOTREACHED */ 1000Sstevel@tonic-gate 1010Sstevel@tonic-gate default: 1020Sstevel@tonic-gate #ifndef NDEBUG 1030Sstevel@tonic-gate (void) fprintf(stderr, "%s:%d: gl_get_line() " 1040Sstevel@tonic-gate "returned unexpected value %d.\n", __FILE__, 1050Sstevel@tonic-gate __LINE__, gl_return_status(E->sc_gl)); 1060Sstevel@tonic-gate #endif 1070Sstevel@tonic-gate abort(); 1080Sstevel@tonic-gate } 1090Sstevel@tonic-gate } 1100Sstevel@tonic-gate 1110Sstevel@tonic-gate E->sc_cmd_bufsz = strlen(E->sc_cmd_buf); 1120Sstevel@tonic-gate E->sc_cmd_bufoff = 1; 1130Sstevel@tonic-gate 1140Sstevel@tonic-gate return (E->sc_cmd_buf[0]); 1150Sstevel@tonic-gate #endif /* NATIVE_BUILD */ 1160Sstevel@tonic-gate } 1170Sstevel@tonic-gate } 1180Sstevel@tonic-gate 1190Sstevel@tonic-gate int 1200Sstevel@tonic-gate engine_cmd_ungetc(engine_state_t *E, char c) 1210Sstevel@tonic-gate { 1220Sstevel@tonic-gate if (E->sc_cmd_file != NULL) 1230Sstevel@tonic-gate return (ungetc(c, E->sc_cmd_file)); 1240Sstevel@tonic-gate 1250Sstevel@tonic-gate if (E->sc_cmd_buf != NULL) 1260Sstevel@tonic-gate *(E->sc_cmd_buf + --E->sc_cmd_bufoff) = c; 1270Sstevel@tonic-gate 1280Sstevel@tonic-gate return (c); 1290Sstevel@tonic-gate } 1300Sstevel@tonic-gate 1310Sstevel@tonic-gate /*ARGSUSED*/ 1320Sstevel@tonic-gate void 1330Sstevel@tonic-gate engine_cmd_nputs(engine_state_t *E, char *c, size_t n) 1340Sstevel@tonic-gate { 1350Sstevel@tonic-gate /* our lexer shouldn't need this state */ 1360Sstevel@tonic-gate exit(11); 1370Sstevel@tonic-gate } 1380Sstevel@tonic-gate 1390Sstevel@tonic-gate int 1400Sstevel@tonic-gate engine_exec(char *cmd) 1410Sstevel@tonic-gate { 1420Sstevel@tonic-gate est->sc_cmd_buf = cmd; 1430Sstevel@tonic-gate est->sc_cmd_bufsz = strlen(cmd) + 1; 1440Sstevel@tonic-gate est->sc_cmd_bufoff = 0; 1450Sstevel@tonic-gate 1460Sstevel@tonic-gate (void) yyparse(); 1470Sstevel@tonic-gate 1480Sstevel@tonic-gate return (0); 1490Sstevel@tonic-gate } 1500Sstevel@tonic-gate 1510Sstevel@tonic-gate #ifndef NATIVE_BUILD 1520Sstevel@tonic-gate /* ARGSUSED */ 1530Sstevel@tonic-gate static 1540Sstevel@tonic-gate CPL_CHECK_FN(check_xml) 1550Sstevel@tonic-gate { 1560Sstevel@tonic-gate const char *ext; 1570Sstevel@tonic-gate 1580Sstevel@tonic-gate if (strlen(pathname) < 4) 1590Sstevel@tonic-gate return (0); 1600Sstevel@tonic-gate 1610Sstevel@tonic-gate ext = pathname + strlen(pathname) - 4; 1620Sstevel@tonic-gate 1630Sstevel@tonic-gate return (strcmp(ext, ".xml") == 0 ? 1 : 0); 1640Sstevel@tonic-gate } 1650Sstevel@tonic-gate 1660Sstevel@tonic-gate static const char * const whitespace = " \t"; 1670Sstevel@tonic-gate 1680Sstevel@tonic-gate static 1690Sstevel@tonic-gate CPL_MATCH_FN(complete_single_xml_file_arg) 1700Sstevel@tonic-gate { 1710Sstevel@tonic-gate const char *arg1 = data; 1720Sstevel@tonic-gate int arg1end_i, ret; 1730Sstevel@tonic-gate CplFileConf *cfc; 1740Sstevel@tonic-gate 1750Sstevel@tonic-gate arg1end_i = arg1 + strcspn(arg1, whitespace) - line; 1760Sstevel@tonic-gate if (arg1end_i < word_end) 1770Sstevel@tonic-gate return (0); 1780Sstevel@tonic-gate 1790Sstevel@tonic-gate cfc = new_CplFileConf(); 1800Sstevel@tonic-gate if (cfc == NULL) { 1810Sstevel@tonic-gate cpl_record_error(cpl, "Out of memory."); 1820Sstevel@tonic-gate return (1); 1830Sstevel@tonic-gate } 1840Sstevel@tonic-gate 1850Sstevel@tonic-gate cfc_set_check_fn(cfc, check_xml, NULL); 1860Sstevel@tonic-gate 1870Sstevel@tonic-gate ret = cpl_file_completions(cpl, cfc, line, word_end); 1880Sstevel@tonic-gate 1890Sstevel@tonic-gate (void) del_CplFileConf(cfc); 1900Sstevel@tonic-gate return (ret); 1910Sstevel@tonic-gate } 1920Sstevel@tonic-gate 1930Sstevel@tonic-gate static struct cmd_info { 1940Sstevel@tonic-gate const char *name; 1950Sstevel@tonic-gate uint32_t flags; 1960Sstevel@tonic-gate CplMatchFn *complete_args_f; 1970Sstevel@tonic-gate } cmds[] = { 1980Sstevel@tonic-gate { "validate", CS_GLOBAL, complete_single_xml_file_arg }, 1990Sstevel@tonic-gate { "import", CS_GLOBAL, complete_single_xml_file_arg }, 2000Sstevel@tonic-gate { "export", CS_GLOBAL, NULL }, 2010Sstevel@tonic-gate { "archive", CS_GLOBAL, NULL }, 2020Sstevel@tonic-gate { "apply", CS_GLOBAL, complete_single_xml_file_arg }, 2030Sstevel@tonic-gate { "extract", CS_GLOBAL, NULL }, 2040Sstevel@tonic-gate { "repository", CS_GLOBAL, NULL }, 2050Sstevel@tonic-gate { "inventory", CS_GLOBAL, complete_single_xml_file_arg }, 2060Sstevel@tonic-gate { "set", CS_GLOBAL, NULL }, 2070Sstevel@tonic-gate { "end", CS_GLOBAL, NULL }, 2080Sstevel@tonic-gate { "exit", CS_GLOBAL, NULL }, 2090Sstevel@tonic-gate { "quit", CS_GLOBAL, NULL }, 2100Sstevel@tonic-gate { "help", CS_GLOBAL, NULL }, 2110Sstevel@tonic-gate { "delete", CS_GLOBAL, NULL }, 2120Sstevel@tonic-gate { "select", CS_GLOBAL, complete_select }, 2130Sstevel@tonic-gate { "unselect", CS_SVC | CS_INST | CS_SNAP, NULL }, 2140Sstevel@tonic-gate { "list", CS_SCOPE | CS_SVC | CS_SNAP, NULL }, 2150Sstevel@tonic-gate { "add", CS_SCOPE | CS_SVC, NULL }, 2160Sstevel@tonic-gate { "listpg", CS_SVC | CS_INST | CS_SNAP, NULL }, 2170Sstevel@tonic-gate { "addpg", CS_SVC | CS_INST, NULL }, 2180Sstevel@tonic-gate { "delpg", CS_SVC | CS_INST, NULL }, 2190Sstevel@tonic-gate { "listprop", CS_SVC | CS_INST | CS_SNAP, NULL }, 2200Sstevel@tonic-gate { "setprop", CS_SVC | CS_INST, NULL }, 2210Sstevel@tonic-gate { "delprop", CS_SVC | CS_INST, NULL }, 2220Sstevel@tonic-gate { "editprop", CS_SVC | CS_INST, NULL }, 2230Sstevel@tonic-gate { "listsnap", CS_INST | CS_SNAP, NULL }, 2240Sstevel@tonic-gate { "selectsnap", CS_INST | CS_SNAP, NULL }, 2250Sstevel@tonic-gate { "revert", CS_INST | CS_SNAP, NULL }, 226*5841Samaguire { "refresh", CS_INST, NULL }, 2270Sstevel@tonic-gate { NULL } 2280Sstevel@tonic-gate }; 2290Sstevel@tonic-gate 2300Sstevel@tonic-gate int 2310Sstevel@tonic-gate add_cmd_matches(WordCompletion *cpl, const char *line, int word_end, 2320Sstevel@tonic-gate uint32_t scope) 2330Sstevel@tonic-gate { 2340Sstevel@tonic-gate int word_start, err; 2350Sstevel@tonic-gate size_t len; 2360Sstevel@tonic-gate const char *bol; 2370Sstevel@tonic-gate struct cmd_info *cip; 2380Sstevel@tonic-gate 2390Sstevel@tonic-gate word_start = strspn(line, whitespace); 2400Sstevel@tonic-gate len = word_end - word_start; 2410Sstevel@tonic-gate bol = line + word_end - len; 2420Sstevel@tonic-gate 2430Sstevel@tonic-gate for (cip = cmds; cip->name != NULL; ++cip) { 2440Sstevel@tonic-gate if ((cip->flags & scope) == 0) 2450Sstevel@tonic-gate continue; 2460Sstevel@tonic-gate 2470Sstevel@tonic-gate if (strncmp(cip->name, bol, len) == 0) { 2480Sstevel@tonic-gate err = cpl_add_completion(cpl, line, word_start, 2490Sstevel@tonic-gate word_end, cip->name + len, "", " "); 2500Sstevel@tonic-gate if (err != 0) 2510Sstevel@tonic-gate return (err); 2520Sstevel@tonic-gate } 2530Sstevel@tonic-gate } 2540Sstevel@tonic-gate 2550Sstevel@tonic-gate return (0); 2560Sstevel@tonic-gate } 2570Sstevel@tonic-gate 2580Sstevel@tonic-gate /* 2590Sstevel@tonic-gate * Suggest completions. We must first determine if the cursor is in command 2600Sstevel@tonic-gate * position or in argument position. If the former, complete_command() finds 2610Sstevel@tonic-gate * matching commands. If the latter, we tail-call the command-specific 2620Sstevel@tonic-gate * argument-completion routine in the cmds table. 2630Sstevel@tonic-gate */ 2640Sstevel@tonic-gate /* ARGSUSED */ 2650Sstevel@tonic-gate static 2660Sstevel@tonic-gate CPL_MATCH_FN(complete) 2670Sstevel@tonic-gate { 2680Sstevel@tonic-gate const char *arg0, *arg1; 2690Sstevel@tonic-gate size_t arg0len; 2700Sstevel@tonic-gate struct cmd_info *cip; 2710Sstevel@tonic-gate 2720Sstevel@tonic-gate arg0 = line + strspn(line, whitespace); 2730Sstevel@tonic-gate arg0len = strcspn(arg0, whitespace); 2740Sstevel@tonic-gate if ((arg0 + arg0len) - line >= word_end || 2750Sstevel@tonic-gate (arg0[arg0len] != ' ' && arg0[arg0len] != '\t')) 2760Sstevel@tonic-gate return (complete_command(cpl, (void *)arg0, line, word_end)); 2770Sstevel@tonic-gate 2780Sstevel@tonic-gate arg1 = arg0 + arg0len; 2790Sstevel@tonic-gate arg1 += strspn(arg1, whitespace); 2800Sstevel@tonic-gate 2810Sstevel@tonic-gate for (cip = cmds; cip->name != NULL; ++cip) { 2820Sstevel@tonic-gate if (strlen(cip->name) != arg0len) 2830Sstevel@tonic-gate continue; 2840Sstevel@tonic-gate 2850Sstevel@tonic-gate if (strncmp(cip->name, arg0, arg0len) != 0) 2860Sstevel@tonic-gate continue; 2870Sstevel@tonic-gate 2880Sstevel@tonic-gate if (cip->complete_args_f == NULL) 2890Sstevel@tonic-gate break; 2900Sstevel@tonic-gate 2910Sstevel@tonic-gate return (cip->complete_args_f(cpl, (void *)arg1, line, 2920Sstevel@tonic-gate word_end)); 2930Sstevel@tonic-gate } 2940Sstevel@tonic-gate 2950Sstevel@tonic-gate return (0); 2960Sstevel@tonic-gate } 2970Sstevel@tonic-gate #endif /* NATIVE_BUILD */ 2980Sstevel@tonic-gate 2990Sstevel@tonic-gate int 3000Sstevel@tonic-gate engine_interp() 3010Sstevel@tonic-gate { 3020Sstevel@tonic-gate #ifdef NATIVE_BUILD 3030Sstevel@tonic-gate uu_die("native build does not support interactive mode."); 3040Sstevel@tonic-gate #else 3050Sstevel@tonic-gate char *selfmri; 3060Sstevel@tonic-gate size_t sfsz; 3070Sstevel@tonic-gate int r; 3080Sstevel@tonic-gate 3090Sstevel@tonic-gate extern int parens; 3100Sstevel@tonic-gate 3110Sstevel@tonic-gate (void) sigset(SIGINT, SIG_IGN); 3120Sstevel@tonic-gate 3130Sstevel@tonic-gate est->sc_gl = new_GetLine(512, 8000); 3140Sstevel@tonic-gate if (est->sc_gl == NULL) 3150Sstevel@tonic-gate uu_die(gettext("Out of memory.\n")); 3160Sstevel@tonic-gate 3170Sstevel@tonic-gate /* The longest string is "[snapname]fmri[:instname]> ". */ 3180Sstevel@tonic-gate sfsz = 1 + max_scf_name_len + 1 + max_scf_fmri_len + 2 + 3190Sstevel@tonic-gate max_scf_name_len + 1 + 2 + 1; 3200Sstevel@tonic-gate selfmri = safe_malloc(sfsz); 3210Sstevel@tonic-gate 3220Sstevel@tonic-gate r = gl_customize_completion(est->sc_gl, NULL, complete); 3230Sstevel@tonic-gate assert(r == 0); 3240Sstevel@tonic-gate 3250Sstevel@tonic-gate for (;;) { 3260Sstevel@tonic-gate lscf_get_selection_str(selfmri, sfsz - 2); 3270Sstevel@tonic-gate (void) strcat(selfmri, "> "); 3280Sstevel@tonic-gate est->sc_cmd_buf = gl_get_line(est->sc_gl, selfmri, NULL, -1); 3290Sstevel@tonic-gate 3300Sstevel@tonic-gate if (est->sc_cmd_buf == NULL) { 3310Sstevel@tonic-gate switch (gl_return_status(est->sc_gl)) { 3320Sstevel@tonic-gate case GLR_SIGNAL: 3330Sstevel@tonic-gate gl_abandon_line(est->sc_gl); 3340Sstevel@tonic-gate continue; 3350Sstevel@tonic-gate 3360Sstevel@tonic-gate case GLR_EOF: 3370Sstevel@tonic-gate break; 3380Sstevel@tonic-gate 3390Sstevel@tonic-gate case GLR_ERROR: 3400Sstevel@tonic-gate uu_die(gettext("Error reading terminal: %s.\n"), 3410Sstevel@tonic-gate gl_error_message(est->sc_gl, NULL, 0)); 3420Sstevel@tonic-gate /* NOTREACHED */ 3430Sstevel@tonic-gate 3440Sstevel@tonic-gate default: 3450Sstevel@tonic-gate #ifndef NDEBUG 3460Sstevel@tonic-gate (void) fprintf(stderr, "%s:%d: gl_get_line() " 3470Sstevel@tonic-gate "returned unexpected value %d.\n", __FILE__, 3480Sstevel@tonic-gate __LINE__, gl_return_status(est->sc_gl)); 3490Sstevel@tonic-gate #endif 3500Sstevel@tonic-gate abort(); 3510Sstevel@tonic-gate } 3520Sstevel@tonic-gate 3530Sstevel@tonic-gate break; 3540Sstevel@tonic-gate } 3550Sstevel@tonic-gate 3560Sstevel@tonic-gate parens = 0; 3570Sstevel@tonic-gate est->sc_cmd_bufsz = strlen(est->sc_cmd_buf); 3580Sstevel@tonic-gate est->sc_cmd_bufoff = 0; 3590Sstevel@tonic-gate est->sc_cmd_flags = SC_CMD_IACTIVE; 3600Sstevel@tonic-gate 3610Sstevel@tonic-gate (void) yyparse(); 3620Sstevel@tonic-gate } 3630Sstevel@tonic-gate 3640Sstevel@tonic-gate free(selfmri); 3650Sstevel@tonic-gate est->sc_gl = del_GetLine(est->sc_gl); /* returns NULL */ 3660Sstevel@tonic-gate 3670Sstevel@tonic-gate #endif /* NATIVE_BUILD */ 3680Sstevel@tonic-gate return (0); 3690Sstevel@tonic-gate } 3700Sstevel@tonic-gate 3710Sstevel@tonic-gate int 3720Sstevel@tonic-gate engine_source(const char *name, boolean_t dont_exit) 3730Sstevel@tonic-gate { 3740Sstevel@tonic-gate engine_state_t *old = est; 3750Sstevel@tonic-gate struct stat st; 3760Sstevel@tonic-gate int ret; 3770Sstevel@tonic-gate 3780Sstevel@tonic-gate est = uu_zalloc(sizeof (engine_state_t)); 3790Sstevel@tonic-gate 3800Sstevel@tonic-gate /* first, copy the stuff set up in engine_init */ 3810Sstevel@tonic-gate est->sc_repo_pid = old->sc_repo_pid; 3820Sstevel@tonic-gate if (old->sc_repo_filename != NULL) 3830Sstevel@tonic-gate est->sc_repo_filename = safe_strdup(old->sc_repo_filename); 3840Sstevel@tonic-gate if (old->sc_repo_doordir != NULL) 3850Sstevel@tonic-gate est->sc_repo_doordir = safe_strdup(old->sc_repo_doordir); 3860Sstevel@tonic-gate if (old->sc_repo_doorname != NULL) 3870Sstevel@tonic-gate est->sc_repo_doorname = safe_strdup(old->sc_repo_doorname); 3880Sstevel@tonic-gate if (old->sc_repo_server != NULL) 3890Sstevel@tonic-gate est->sc_repo_server = safe_strdup(old->sc_repo_server); 3900Sstevel@tonic-gate 3910Sstevel@tonic-gate /* set up the new guy */ 3920Sstevel@tonic-gate est->sc_cmd_lineno = 1; 3930Sstevel@tonic-gate 3940Sstevel@tonic-gate if (dont_exit) 3950Sstevel@tonic-gate est->sc_cmd_flags |= SC_CMD_DONT_EXIT; 3960Sstevel@tonic-gate 3970Sstevel@tonic-gate if (strcmp(name, "-") == 0) { 3980Sstevel@tonic-gate est->sc_cmd_file = stdin; 3990Sstevel@tonic-gate est->sc_cmd_filename = "<stdin>"; 4000Sstevel@tonic-gate } else { 4010Sstevel@tonic-gate errno = 0; 4020Sstevel@tonic-gate est->sc_cmd_filename = name; 4030Sstevel@tonic-gate est->sc_cmd_file = fopen(name, "r"); 4040Sstevel@tonic-gate if (est->sc_cmd_file == NULL) { 4050Sstevel@tonic-gate if (errno == 0) 4060Sstevel@tonic-gate semerr(gettext("No free stdio streams.\n")); 4070Sstevel@tonic-gate else 4080Sstevel@tonic-gate semerr(gettext("Could not open %s"), name); 4090Sstevel@tonic-gate 4100Sstevel@tonic-gate ret = -1; 4110Sstevel@tonic-gate goto fail; 4120Sstevel@tonic-gate } 4130Sstevel@tonic-gate 4145040Swesolows do { 4150Sstevel@tonic-gate ret = fstat(fileno(est->sc_cmd_file), &st); 4165040Swesolows } while (ret != 0 && errno == EINTR); 4170Sstevel@tonic-gate if (ret != 0) { 4180Sstevel@tonic-gate (void) fclose(est->sc_cmd_file); 4190Sstevel@tonic-gate est->sc_cmd_file = NULL; /* for semerr() */ 4200Sstevel@tonic-gate 4210Sstevel@tonic-gate semerr(gettext("Could not stat %s"), name); 4220Sstevel@tonic-gate 4230Sstevel@tonic-gate ret = -1; 4240Sstevel@tonic-gate goto fail; 4250Sstevel@tonic-gate } 4260Sstevel@tonic-gate 4270Sstevel@tonic-gate if (!S_ISREG(st.st_mode)) { 4280Sstevel@tonic-gate (void) fclose(est->sc_cmd_file); 4290Sstevel@tonic-gate est->sc_cmd_file = NULL; /* for semerr() */ 4300Sstevel@tonic-gate 4310Sstevel@tonic-gate semerr(gettext("%s is not a regular file.\n"), name); 4320Sstevel@tonic-gate 4330Sstevel@tonic-gate ret = -1; 4340Sstevel@tonic-gate goto fail; 4350Sstevel@tonic-gate } 4360Sstevel@tonic-gate } 4370Sstevel@tonic-gate 4380Sstevel@tonic-gate (void) yyparse(); 4390Sstevel@tonic-gate 4400Sstevel@tonic-gate if (est->sc_cmd_file != stdin) 4410Sstevel@tonic-gate (void) fclose(est->sc_cmd_file); 4420Sstevel@tonic-gate 4430Sstevel@tonic-gate ret = 0; 4440Sstevel@tonic-gate 4450Sstevel@tonic-gate fail: 4460Sstevel@tonic-gate if (est->sc_repo_pid != old->sc_repo_pid) 4470Sstevel@tonic-gate lscf_cleanup(); /* clean up any new repository */ 4480Sstevel@tonic-gate 4490Sstevel@tonic-gate if (est->sc_repo_filename != NULL) 4500Sstevel@tonic-gate free((void *)est->sc_repo_filename); 4510Sstevel@tonic-gate if (est->sc_repo_doordir != NULL) 4520Sstevel@tonic-gate free((void *)est->sc_repo_doordir); 4530Sstevel@tonic-gate if (est->sc_repo_doorname != NULL) 4540Sstevel@tonic-gate free((void *)est->sc_repo_doorname); 4550Sstevel@tonic-gate if (est->sc_repo_server != NULL) 4560Sstevel@tonic-gate free((void *)est->sc_repo_server); 4570Sstevel@tonic-gate free(est); 4580Sstevel@tonic-gate 4590Sstevel@tonic-gate est = old; 4600Sstevel@tonic-gate 4610Sstevel@tonic-gate return (ret); 4620Sstevel@tonic-gate } 4630Sstevel@tonic-gate 4640Sstevel@tonic-gate /* 4650Sstevel@tonic-gate * Initialize svccfg state. We recognize four environment variables: 4660Sstevel@tonic-gate * 4670Sstevel@tonic-gate * SVCCFG_REPOSITORY Create a private instance of svc.configd(1M) to answer 4680Sstevel@tonic-gate * requests for the specified repository file. 4690Sstevel@tonic-gate * SVCCFG_DOOR_PATH Directory for door creation. 4700Sstevel@tonic-gate * 4710Sstevel@tonic-gate * SVCCFG_DOOR Rendezvous via an alternative repository door. 4720Sstevel@tonic-gate * 4730Sstevel@tonic-gate * SVCCFG_CONFIGD_PATH Resolvable path to alternative svc.configd(1M) binary. 4740Sstevel@tonic-gate */ 4750Sstevel@tonic-gate void 4760Sstevel@tonic-gate engine_init() 4770Sstevel@tonic-gate { 4780Sstevel@tonic-gate const char *cp; 4790Sstevel@tonic-gate 4800Sstevel@tonic-gate est = uu_zalloc(sizeof (engine_state_t)); 4810Sstevel@tonic-gate 4820Sstevel@tonic-gate est->sc_cmd_lineno = 1; 4830Sstevel@tonic-gate est->sc_repo_pid = -1; 4840Sstevel@tonic-gate 4850Sstevel@tonic-gate cp = getenv("SVCCFG_REPOSITORY"); 4860Sstevel@tonic-gate est->sc_repo_filename = cp ? safe_strdup(cp) : NULL; 4870Sstevel@tonic-gate 4880Sstevel@tonic-gate cp = getenv("SVCCFG_DOOR_PATH"); 4890Sstevel@tonic-gate est->sc_repo_doordir = cp ? cp : "/var/run"; 4900Sstevel@tonic-gate 4910Sstevel@tonic-gate cp = getenv("SVCCFG_DOOR"); 4920Sstevel@tonic-gate if (cp != NULL) { 4930Sstevel@tonic-gate if (est->sc_repo_filename != NULL) { 4940Sstevel@tonic-gate uu_warn(gettext("SVCCFG_DOOR unused when " 4950Sstevel@tonic-gate "SVCCFG_REPOSITORY specified\n")); 4960Sstevel@tonic-gate } else { 4970Sstevel@tonic-gate est->sc_repo_doorname = safe_strdup(cp); 4980Sstevel@tonic-gate } 4990Sstevel@tonic-gate } 5000Sstevel@tonic-gate 5010Sstevel@tonic-gate cp = getenv("SVCCFG_CONFIGD_PATH"); 5020Sstevel@tonic-gate est->sc_repo_server = cp ? cp : "/lib/svc/bin/svc.configd"; 5030Sstevel@tonic-gate } 5040Sstevel@tonic-gate 5050Sstevel@tonic-gate int 5060Sstevel@tonic-gate engine_import(uu_list_t *args) 5070Sstevel@tonic-gate { 5080Sstevel@tonic-gate int ret, argc, i, o; 5090Sstevel@tonic-gate bundle_t *b; 5100Sstevel@tonic-gate char *file, *pname; 5111061Scasper uchar_t hash[MHASH_SIZE]; 5120Sstevel@tonic-gate char **argv; 5130Sstevel@tonic-gate string_list_t *slp; 5140Sstevel@tonic-gate boolean_t verify = B_FALSE; 5150Sstevel@tonic-gate uint_t flags = SCI_GENERALLAST; 5160Sstevel@tonic-gate 5170Sstevel@tonic-gate argc = uu_list_numnodes(args); 5180Sstevel@tonic-gate if (argc < 1) 5190Sstevel@tonic-gate return (-2); 5200Sstevel@tonic-gate 5210Sstevel@tonic-gate argv = calloc(argc + 1, sizeof (char *)); 5220Sstevel@tonic-gate if (argv == NULL) 5230Sstevel@tonic-gate uu_die(gettext("Out of memory.\n")); 5240Sstevel@tonic-gate 5250Sstevel@tonic-gate for (slp = uu_list_first(args), i = 0; 5260Sstevel@tonic-gate slp != NULL; 5270Sstevel@tonic-gate slp = uu_list_next(args, slp), ++i) 5280Sstevel@tonic-gate argv[i] = slp->str; 5290Sstevel@tonic-gate 5300Sstevel@tonic-gate argv[i] = NULL; 5310Sstevel@tonic-gate 5320Sstevel@tonic-gate opterr = 0; 5330Sstevel@tonic-gate optind = 0; /* Remember, no argv[0]. */ 5340Sstevel@tonic-gate for (;;) { 5350Sstevel@tonic-gate o = getopt(argc, argv, "nV"); 5360Sstevel@tonic-gate if (o == -1) 5370Sstevel@tonic-gate break; 5380Sstevel@tonic-gate 5390Sstevel@tonic-gate switch (o) { 5400Sstevel@tonic-gate case 'n': 5410Sstevel@tonic-gate flags |= SCI_NOREFRESH; 5420Sstevel@tonic-gate break; 5430Sstevel@tonic-gate 5440Sstevel@tonic-gate case 'V': 5450Sstevel@tonic-gate verify = B_TRUE; 5460Sstevel@tonic-gate break; 5470Sstevel@tonic-gate 5480Sstevel@tonic-gate case '?': 5490Sstevel@tonic-gate free(argv); 5500Sstevel@tonic-gate return (-2); 5510Sstevel@tonic-gate 5520Sstevel@tonic-gate default: 5530Sstevel@tonic-gate bad_error("getopt", o); 5540Sstevel@tonic-gate } 5550Sstevel@tonic-gate } 5560Sstevel@tonic-gate 5570Sstevel@tonic-gate argc -= optind; 5580Sstevel@tonic-gate if (argc != 1) { 5590Sstevel@tonic-gate free(argv); 5600Sstevel@tonic-gate return (-2); 5610Sstevel@tonic-gate } 5620Sstevel@tonic-gate 5630Sstevel@tonic-gate file = argv[optind]; 5640Sstevel@tonic-gate free(argv); 5650Sstevel@tonic-gate 5660Sstevel@tonic-gate lscf_prep_hndl(); 5670Sstevel@tonic-gate 5682462Scasper ret = mhash_test_file(g_hndl, file, 0, &pname, hash); 5692462Scasper if (ret != MHASH_NEWFILE) 5700Sstevel@tonic-gate return (ret); 5710Sstevel@tonic-gate 5720Sstevel@tonic-gate /* Load */ 5730Sstevel@tonic-gate b = internal_bundle_new(); 5740Sstevel@tonic-gate 5755040Swesolows if (lxml_get_bundle_file(b, file, SVCCFG_OP_IMPORT) != 0) { 5760Sstevel@tonic-gate internal_bundle_free(b); 5770Sstevel@tonic-gate return (-1); 5780Sstevel@tonic-gate } 5790Sstevel@tonic-gate 5800Sstevel@tonic-gate /* Import */ 5810Sstevel@tonic-gate if (lscf_bundle_import(b, file, flags) != 0) { 5820Sstevel@tonic-gate internal_bundle_free(b); 5830Sstevel@tonic-gate return (-1); 5840Sstevel@tonic-gate } 5850Sstevel@tonic-gate 5860Sstevel@tonic-gate internal_bundle_free(b); 5870Sstevel@tonic-gate 5880Sstevel@tonic-gate if (g_verbose) 5890Sstevel@tonic-gate warn(gettext("Successful import.\n")); 5900Sstevel@tonic-gate 5910Sstevel@tonic-gate if (pname) { 5920Sstevel@tonic-gate char *errstr; 5930Sstevel@tonic-gate 5940Sstevel@tonic-gate if (mhash_store_entry(g_hndl, pname, hash, &errstr)) { 5950Sstevel@tonic-gate if (errstr) 5960Sstevel@tonic-gate semerr(errstr); 5970Sstevel@tonic-gate else 5980Sstevel@tonic-gate semerr(gettext("Unknown error from " 5995040Swesolows "mhash_store_entry()\n")); 6000Sstevel@tonic-gate } 6010Sstevel@tonic-gate 6020Sstevel@tonic-gate free(pname); 6030Sstevel@tonic-gate } 6040Sstevel@tonic-gate 6050Sstevel@tonic-gate /* Verify */ 6060Sstevel@tonic-gate if (verify) 6070Sstevel@tonic-gate warn(gettext("import -V not implemented.\n")); 6080Sstevel@tonic-gate 6090Sstevel@tonic-gate return (0); 6100Sstevel@tonic-gate } 6110Sstevel@tonic-gate 6120Sstevel@tonic-gate int 6130Sstevel@tonic-gate engine_apply(const char *file) 6140Sstevel@tonic-gate { 6150Sstevel@tonic-gate int ret; 6160Sstevel@tonic-gate bundle_t *b; 6170Sstevel@tonic-gate char *pname; 6181061Scasper uchar_t hash[MHASH_SIZE]; 6190Sstevel@tonic-gate 6200Sstevel@tonic-gate lscf_prep_hndl(); 6210Sstevel@tonic-gate 6222462Scasper ret = mhash_test_file(g_hndl, file, 1, &pname, hash); 6232462Scasper if (ret != MHASH_NEWFILE) 6240Sstevel@tonic-gate return (ret); 6250Sstevel@tonic-gate 6260Sstevel@tonic-gate b = internal_bundle_new(); 6270Sstevel@tonic-gate 6285040Swesolows if (lxml_get_bundle_file(b, file, SVCCFG_OP_APPLY) != 0) { 6290Sstevel@tonic-gate internal_bundle_free(b); 6300Sstevel@tonic-gate return (-1); 6310Sstevel@tonic-gate } 6320Sstevel@tonic-gate 6335777Stw21770 if (lscf_bundle_apply(b, file) != 0) { 6340Sstevel@tonic-gate internal_bundle_free(b); 6350Sstevel@tonic-gate return (-1); 6360Sstevel@tonic-gate } 6370Sstevel@tonic-gate 6380Sstevel@tonic-gate internal_bundle_free(b); 6390Sstevel@tonic-gate 6400Sstevel@tonic-gate if (pname) { 6410Sstevel@tonic-gate char *errstr; 6420Sstevel@tonic-gate if (mhash_store_entry(g_hndl, pname, hash, &errstr)) 6430Sstevel@tonic-gate semerr(errstr); 6440Sstevel@tonic-gate 6450Sstevel@tonic-gate free(pname); 6460Sstevel@tonic-gate } 6470Sstevel@tonic-gate 6480Sstevel@tonic-gate return (0); 6490Sstevel@tonic-gate } 6500Sstevel@tonic-gate 6510Sstevel@tonic-gate int 6525040Swesolows engine_restore(const char *file) 6535040Swesolows { 6545040Swesolows bundle_t *b; 6555040Swesolows 6565040Swesolows lscf_prep_hndl(); 6575040Swesolows 6585040Swesolows b = internal_bundle_new(); 6595040Swesolows 6605040Swesolows if (lxml_get_bundle_file(b, file, SVCCFG_OP_RESTORE) != 0) { 6615040Swesolows internal_bundle_free(b); 6625040Swesolows return (-1); 6635040Swesolows } 6645040Swesolows 6655040Swesolows if (lscf_bundle_import(b, file, SCI_NOSNAP) != 0) { 6665040Swesolows internal_bundle_free(b); 6675040Swesolows return (-1); 6685040Swesolows } 6695040Swesolows 6705040Swesolows internal_bundle_free(b); 6715040Swesolows 6725040Swesolows return (0); 6735040Swesolows } 6745040Swesolows 6755040Swesolows int 6760Sstevel@tonic-gate engine_set(uu_list_t *args) 6770Sstevel@tonic-gate { 6780Sstevel@tonic-gate uu_list_walk_t *walk; 6790Sstevel@tonic-gate string_list_t *slp; 6800Sstevel@tonic-gate 6810Sstevel@tonic-gate if (uu_list_first(args) == NULL) { 6820Sstevel@tonic-gate /* Display current options. */ 6830Sstevel@tonic-gate if (!g_verbose) 6840Sstevel@tonic-gate (void) fputs("no", stdout); 6850Sstevel@tonic-gate (void) puts("verbose"); 6860Sstevel@tonic-gate 6870Sstevel@tonic-gate return (0); 6880Sstevel@tonic-gate } 6890Sstevel@tonic-gate 6900Sstevel@tonic-gate walk = uu_list_walk_start(args, UU_DEFAULT); 6910Sstevel@tonic-gate if (walk == NULL) 6920Sstevel@tonic-gate uu_die(gettext("Couldn't read arguments")); 6930Sstevel@tonic-gate 6940Sstevel@tonic-gate /* Use getopt? */ 6950Sstevel@tonic-gate for (slp = uu_list_walk_next(walk); 6960Sstevel@tonic-gate slp != NULL; 6970Sstevel@tonic-gate slp = uu_list_walk_next(walk)) { 6980Sstevel@tonic-gate if (slp->str[0] == '-') { 6990Sstevel@tonic-gate char *op; 7000Sstevel@tonic-gate 7010Sstevel@tonic-gate for (op = &slp->str[1]; *op != '\0'; ++op) { 7020Sstevel@tonic-gate switch (*op) { 7030Sstevel@tonic-gate case 'v': 7040Sstevel@tonic-gate g_verbose = 1; 7050Sstevel@tonic-gate break; 7060Sstevel@tonic-gate 7070Sstevel@tonic-gate case 'V': 7080Sstevel@tonic-gate g_verbose = 0; 7090Sstevel@tonic-gate break; 7100Sstevel@tonic-gate 7110Sstevel@tonic-gate default: 7120Sstevel@tonic-gate warn(gettext("Unknown option -%c.\n"), 7130Sstevel@tonic-gate *op); 7140Sstevel@tonic-gate } 7150Sstevel@tonic-gate } 7160Sstevel@tonic-gate } else { 7170Sstevel@tonic-gate warn(gettext("No non-flag arguments defined.\n")); 7180Sstevel@tonic-gate } 7190Sstevel@tonic-gate } 7200Sstevel@tonic-gate 7210Sstevel@tonic-gate return (0); 7220Sstevel@tonic-gate } 7230Sstevel@tonic-gate 7240Sstevel@tonic-gate void 7250Sstevel@tonic-gate help(int com) 7260Sstevel@tonic-gate { 7270Sstevel@tonic-gate int i; 7280Sstevel@tonic-gate 7290Sstevel@tonic-gate if (com == 0) { 7300Sstevel@tonic-gate warn(gettext("General commands: help set repository end\n" 7310Sstevel@tonic-gate "Manifest commands: inventory validate import export " 7320Sstevel@tonic-gate "archive\n" 7330Sstevel@tonic-gate "Profile commands: apply extract\n" 7340Sstevel@tonic-gate "Entity commands: list select unselect add delete\n" 7350Sstevel@tonic-gate "Snapshot commands: listsnap selectsnap revert\n" 736*5841Samaguire "Instance commands: refresh\n" 7370Sstevel@tonic-gate "Property group commands: listpg addpg delpg\n" 7380Sstevel@tonic-gate "Property commands: listprop setprop delprop editprop\n" 7390Sstevel@tonic-gate "Property value commands: addpropvalue delpropvalue " 7400Sstevel@tonic-gate "setenv unsetenv\n")); 7410Sstevel@tonic-gate return; 7420Sstevel@tonic-gate } 7430Sstevel@tonic-gate 7440Sstevel@tonic-gate for (i = 0; help_messages[i].message != NULL; ++i) { 7450Sstevel@tonic-gate if (help_messages[i].token == com) { 7460Sstevel@tonic-gate warn(gettext("Usage: %s\n"), 7470Sstevel@tonic-gate gettext(help_messages[i].message)); 7480Sstevel@tonic-gate return; 7490Sstevel@tonic-gate } 7500Sstevel@tonic-gate } 7510Sstevel@tonic-gate 7520Sstevel@tonic-gate warn(gettext("Unknown command.\n")); 7530Sstevel@tonic-gate } 754