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 280Sstevel@tonic-gate /* 290Sstevel@tonic-gate * svccfg(1) interpreter and command execution engine. 300Sstevel@tonic-gate */ 310Sstevel@tonic-gate 320Sstevel@tonic-gate #include <sys/mman.h> 330Sstevel@tonic-gate #include <sys/stat.h> 340Sstevel@tonic-gate #include <sys/types.h> 350Sstevel@tonic-gate #include <assert.h> 360Sstevel@tonic-gate #include <errno.h> 370Sstevel@tonic-gate #include <libintl.h> 380Sstevel@tonic-gate #include <libtecla.h> 390Sstevel@tonic-gate #include <md5.h> 400Sstevel@tonic-gate #include <string.h> 410Sstevel@tonic-gate #include <stdlib.h> 420Sstevel@tonic-gate #include <unistd.h> 430Sstevel@tonic-gate 440Sstevel@tonic-gate #include "manifest_hash.h" 450Sstevel@tonic-gate #include "svccfg.h" 460Sstevel@tonic-gate 470Sstevel@tonic-gate #define MS_PER_US 1000 480Sstevel@tonic-gate 490Sstevel@tonic-gate engine_state_t *est; 500Sstevel@tonic-gate 510Sstevel@tonic-gate /* 520Sstevel@tonic-gate * Replacement lex(1) character retrieval routines. 530Sstevel@tonic-gate */ 540Sstevel@tonic-gate int 550Sstevel@tonic-gate engine_cmd_getc(engine_state_t *E) 560Sstevel@tonic-gate { 570Sstevel@tonic-gate if (E->sc_cmd_file != NULL) 580Sstevel@tonic-gate return (getc(E->sc_cmd_file)); 590Sstevel@tonic-gate 600Sstevel@tonic-gate if (E->sc_cmd_flags & SC_CMD_EOF) 610Sstevel@tonic-gate return (EOF); 620Sstevel@tonic-gate 630Sstevel@tonic-gate if (E->sc_cmd_bufoff < E->sc_cmd_bufsz) 640Sstevel@tonic-gate return (*(E->sc_cmd_buf + E->sc_cmd_bufoff++)); 650Sstevel@tonic-gate 660Sstevel@tonic-gate if (!(E->sc_cmd_flags & SC_CMD_IACTIVE)) { 670Sstevel@tonic-gate E->sc_cmd_flags |= SC_CMD_EOF; 680Sstevel@tonic-gate 690Sstevel@tonic-gate return (EOF); 700Sstevel@tonic-gate } else { 710Sstevel@tonic-gate #ifdef NATIVE_BUILD 720Sstevel@tonic-gate return (EOF); 730Sstevel@tonic-gate #else 740Sstevel@tonic-gate extern int parens; 750Sstevel@tonic-gate 760Sstevel@tonic-gate if (parens <= 0) { 770Sstevel@tonic-gate E->sc_cmd_flags |= SC_CMD_EOF; 780Sstevel@tonic-gate return (EOF); 790Sstevel@tonic-gate } 800Sstevel@tonic-gate 810Sstevel@tonic-gate for (;;) { 820Sstevel@tonic-gate E->sc_cmd_buf = gl_get_line(E->sc_gl, "> ", NULL, -1); 830Sstevel@tonic-gate if (E->sc_cmd_buf != NULL) 840Sstevel@tonic-gate break; 850Sstevel@tonic-gate 860Sstevel@tonic-gate switch (gl_return_status(E->sc_gl)) { 870Sstevel@tonic-gate case GLR_SIGNAL: 880Sstevel@tonic-gate gl_abandon_line(E->sc_gl); 890Sstevel@tonic-gate continue; 900Sstevel@tonic-gate 910Sstevel@tonic-gate case GLR_EOF: 920Sstevel@tonic-gate E->sc_cmd_flags |= SC_CMD_EOF; 930Sstevel@tonic-gate return (EOF); 940Sstevel@tonic-gate 950Sstevel@tonic-gate case GLR_ERROR: 960Sstevel@tonic-gate uu_die(gettext("Error reading terminal: %s.\n"), 970Sstevel@tonic-gate gl_error_message(E->sc_gl, NULL, 0)); 980Sstevel@tonic-gate /* NOTREACHED */ 990Sstevel@tonic-gate 1000Sstevel@tonic-gate default: 1010Sstevel@tonic-gate #ifndef NDEBUG 1020Sstevel@tonic-gate (void) fprintf(stderr, "%s:%d: gl_get_line() " 1030Sstevel@tonic-gate "returned unexpected value %d.\n", __FILE__, 1040Sstevel@tonic-gate __LINE__, gl_return_status(E->sc_gl)); 1050Sstevel@tonic-gate #endif 1060Sstevel@tonic-gate abort(); 1070Sstevel@tonic-gate } 1080Sstevel@tonic-gate } 1090Sstevel@tonic-gate 1100Sstevel@tonic-gate E->sc_cmd_bufsz = strlen(E->sc_cmd_buf); 1110Sstevel@tonic-gate E->sc_cmd_bufoff = 1; 1120Sstevel@tonic-gate 1130Sstevel@tonic-gate return (E->sc_cmd_buf[0]); 1140Sstevel@tonic-gate #endif /* NATIVE_BUILD */ 1150Sstevel@tonic-gate } 1160Sstevel@tonic-gate } 1170Sstevel@tonic-gate 1180Sstevel@tonic-gate int 1190Sstevel@tonic-gate engine_cmd_ungetc(engine_state_t *E, char c) 1200Sstevel@tonic-gate { 1210Sstevel@tonic-gate if (E->sc_cmd_file != NULL) 1220Sstevel@tonic-gate return (ungetc(c, E->sc_cmd_file)); 1230Sstevel@tonic-gate 1240Sstevel@tonic-gate if (E->sc_cmd_buf != NULL) 1250Sstevel@tonic-gate *(E->sc_cmd_buf + --E->sc_cmd_bufoff) = c; 1260Sstevel@tonic-gate 1270Sstevel@tonic-gate return (c); 1280Sstevel@tonic-gate } 1290Sstevel@tonic-gate 1300Sstevel@tonic-gate /*ARGSUSED*/ 1310Sstevel@tonic-gate void 1320Sstevel@tonic-gate engine_cmd_nputs(engine_state_t *E, char *c, size_t n) 1330Sstevel@tonic-gate { 1340Sstevel@tonic-gate /* our lexer shouldn't need this state */ 1350Sstevel@tonic-gate exit(11); 1360Sstevel@tonic-gate } 1370Sstevel@tonic-gate 1380Sstevel@tonic-gate int 1390Sstevel@tonic-gate engine_exec(char *cmd) 1400Sstevel@tonic-gate { 1410Sstevel@tonic-gate est->sc_cmd_buf = cmd; 1420Sstevel@tonic-gate est->sc_cmd_bufsz = strlen(cmd) + 1; 1430Sstevel@tonic-gate est->sc_cmd_bufoff = 0; 1440Sstevel@tonic-gate 1450Sstevel@tonic-gate (void) yyparse(); 1460Sstevel@tonic-gate 1470Sstevel@tonic-gate return (0); 1480Sstevel@tonic-gate } 1490Sstevel@tonic-gate 1500Sstevel@tonic-gate #ifndef NATIVE_BUILD 1510Sstevel@tonic-gate /* ARGSUSED */ 1520Sstevel@tonic-gate static 1530Sstevel@tonic-gate CPL_CHECK_FN(check_xml) 1540Sstevel@tonic-gate { 1550Sstevel@tonic-gate const char *ext; 1560Sstevel@tonic-gate 1570Sstevel@tonic-gate if (strlen(pathname) < 4) 1580Sstevel@tonic-gate return (0); 1590Sstevel@tonic-gate 1600Sstevel@tonic-gate ext = pathname + strlen(pathname) - 4; 1610Sstevel@tonic-gate 1620Sstevel@tonic-gate return (strcmp(ext, ".xml") == 0 ? 1 : 0); 1630Sstevel@tonic-gate } 1640Sstevel@tonic-gate 1650Sstevel@tonic-gate static const char * const whitespace = " \t"; 1660Sstevel@tonic-gate 1670Sstevel@tonic-gate static 1680Sstevel@tonic-gate CPL_MATCH_FN(complete_single_xml_file_arg) 1690Sstevel@tonic-gate { 1700Sstevel@tonic-gate const char *arg1 = data; 1710Sstevel@tonic-gate int arg1end_i, ret; 1720Sstevel@tonic-gate CplFileConf *cfc; 1730Sstevel@tonic-gate 1740Sstevel@tonic-gate arg1end_i = arg1 + strcspn(arg1, whitespace) - line; 1750Sstevel@tonic-gate if (arg1end_i < word_end) 1760Sstevel@tonic-gate return (0); 1770Sstevel@tonic-gate 1780Sstevel@tonic-gate cfc = new_CplFileConf(); 1790Sstevel@tonic-gate if (cfc == NULL) { 1800Sstevel@tonic-gate cpl_record_error(cpl, "Out of memory."); 1810Sstevel@tonic-gate return (1); 1820Sstevel@tonic-gate } 1830Sstevel@tonic-gate 1840Sstevel@tonic-gate cfc_set_check_fn(cfc, check_xml, NULL); 1850Sstevel@tonic-gate 1860Sstevel@tonic-gate ret = cpl_file_completions(cpl, cfc, line, word_end); 1870Sstevel@tonic-gate 1880Sstevel@tonic-gate (void) del_CplFileConf(cfc); 1890Sstevel@tonic-gate return (ret); 1900Sstevel@tonic-gate } 1910Sstevel@tonic-gate 1920Sstevel@tonic-gate static struct cmd_info { 1930Sstevel@tonic-gate const char *name; 1940Sstevel@tonic-gate uint32_t flags; 1950Sstevel@tonic-gate CplMatchFn *complete_args_f; 1960Sstevel@tonic-gate } cmds[] = { 1970Sstevel@tonic-gate { "validate", CS_GLOBAL, complete_single_xml_file_arg }, 1980Sstevel@tonic-gate { "import", CS_GLOBAL, complete_single_xml_file_arg }, 1990Sstevel@tonic-gate { "export", CS_GLOBAL, NULL }, 2000Sstevel@tonic-gate { "archive", CS_GLOBAL, NULL }, 2010Sstevel@tonic-gate { "apply", CS_GLOBAL, complete_single_xml_file_arg }, 2020Sstevel@tonic-gate { "extract", CS_GLOBAL, NULL }, 2030Sstevel@tonic-gate { "repository", CS_GLOBAL, NULL }, 2040Sstevel@tonic-gate { "inventory", CS_GLOBAL, complete_single_xml_file_arg }, 2050Sstevel@tonic-gate { "set", CS_GLOBAL, NULL }, 2060Sstevel@tonic-gate { "end", CS_GLOBAL, NULL }, 2070Sstevel@tonic-gate { "exit", CS_GLOBAL, NULL }, 2080Sstevel@tonic-gate { "quit", CS_GLOBAL, NULL }, 2090Sstevel@tonic-gate { "help", CS_GLOBAL, NULL }, 2100Sstevel@tonic-gate { "delete", CS_GLOBAL, NULL }, 2110Sstevel@tonic-gate { "select", CS_GLOBAL, complete_select }, 2120Sstevel@tonic-gate { "unselect", CS_SVC | CS_INST | CS_SNAP, NULL }, 2130Sstevel@tonic-gate { "list", CS_SCOPE | CS_SVC | CS_SNAP, NULL }, 2140Sstevel@tonic-gate { "add", CS_SCOPE | CS_SVC, NULL }, 2150Sstevel@tonic-gate { "listpg", CS_SVC | CS_INST | CS_SNAP, NULL }, 2160Sstevel@tonic-gate { "addpg", CS_SVC | CS_INST, NULL }, 2170Sstevel@tonic-gate { "delpg", CS_SVC | CS_INST, NULL }, 2187475SPhilippe.Jung@Sun.COM { "delhash", CS_GLOBAL, complete_single_xml_file_arg }, 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 }, 223*7887SLiane.Praza@Sun.COM { "describe", CS_SVC | CS_INST | CS_SNAP, NULL }, 2240Sstevel@tonic-gate { "listsnap", CS_INST | CS_SNAP, NULL }, 2250Sstevel@tonic-gate { "selectsnap", CS_INST | CS_SNAP, NULL }, 2260Sstevel@tonic-gate { "revert", CS_INST | CS_SNAP, NULL }, 2275841Samaguire { "refresh", CS_INST, NULL }, 2280Sstevel@tonic-gate { NULL } 2290Sstevel@tonic-gate }; 2300Sstevel@tonic-gate 2310Sstevel@tonic-gate int 2320Sstevel@tonic-gate add_cmd_matches(WordCompletion *cpl, const char *line, int word_end, 2330Sstevel@tonic-gate uint32_t scope) 2340Sstevel@tonic-gate { 2350Sstevel@tonic-gate int word_start, err; 2360Sstevel@tonic-gate size_t len; 2370Sstevel@tonic-gate const char *bol; 2380Sstevel@tonic-gate struct cmd_info *cip; 2390Sstevel@tonic-gate 2400Sstevel@tonic-gate word_start = strspn(line, whitespace); 2410Sstevel@tonic-gate len = word_end - word_start; 2420Sstevel@tonic-gate bol = line + word_end - len; 2430Sstevel@tonic-gate 2440Sstevel@tonic-gate for (cip = cmds; cip->name != NULL; ++cip) { 2450Sstevel@tonic-gate if ((cip->flags & scope) == 0) 2460Sstevel@tonic-gate continue; 2470Sstevel@tonic-gate 2480Sstevel@tonic-gate if (strncmp(cip->name, bol, len) == 0) { 2490Sstevel@tonic-gate err = cpl_add_completion(cpl, line, word_start, 2500Sstevel@tonic-gate word_end, cip->name + len, "", " "); 2510Sstevel@tonic-gate if (err != 0) 2520Sstevel@tonic-gate return (err); 2530Sstevel@tonic-gate } 2540Sstevel@tonic-gate } 2550Sstevel@tonic-gate 2560Sstevel@tonic-gate return (0); 2570Sstevel@tonic-gate } 2580Sstevel@tonic-gate 2590Sstevel@tonic-gate /* 2600Sstevel@tonic-gate * Suggest completions. We must first determine if the cursor is in command 2610Sstevel@tonic-gate * position or in argument position. If the former, complete_command() finds 2620Sstevel@tonic-gate * matching commands. If the latter, we tail-call the command-specific 2630Sstevel@tonic-gate * argument-completion routine in the cmds table. 2640Sstevel@tonic-gate */ 2650Sstevel@tonic-gate /* ARGSUSED */ 2660Sstevel@tonic-gate static 2670Sstevel@tonic-gate CPL_MATCH_FN(complete) 2680Sstevel@tonic-gate { 2690Sstevel@tonic-gate const char *arg0, *arg1; 2700Sstevel@tonic-gate size_t arg0len; 2710Sstevel@tonic-gate struct cmd_info *cip; 2720Sstevel@tonic-gate 2730Sstevel@tonic-gate arg0 = line + strspn(line, whitespace); 2740Sstevel@tonic-gate arg0len = strcspn(arg0, whitespace); 2750Sstevel@tonic-gate if ((arg0 + arg0len) - line >= word_end || 2760Sstevel@tonic-gate (arg0[arg0len] != ' ' && arg0[arg0len] != '\t')) 2770Sstevel@tonic-gate return (complete_command(cpl, (void *)arg0, line, word_end)); 2780Sstevel@tonic-gate 2790Sstevel@tonic-gate arg1 = arg0 + arg0len; 2800Sstevel@tonic-gate arg1 += strspn(arg1, whitespace); 2810Sstevel@tonic-gate 2820Sstevel@tonic-gate for (cip = cmds; cip->name != NULL; ++cip) { 2830Sstevel@tonic-gate if (strlen(cip->name) != arg0len) 2840Sstevel@tonic-gate continue; 2850Sstevel@tonic-gate 2860Sstevel@tonic-gate if (strncmp(cip->name, arg0, arg0len) != 0) 2870Sstevel@tonic-gate continue; 2880Sstevel@tonic-gate 2890Sstevel@tonic-gate if (cip->complete_args_f == NULL) 2900Sstevel@tonic-gate break; 2910Sstevel@tonic-gate 2920Sstevel@tonic-gate return (cip->complete_args_f(cpl, (void *)arg1, line, 2930Sstevel@tonic-gate word_end)); 2940Sstevel@tonic-gate } 2950Sstevel@tonic-gate 2960Sstevel@tonic-gate return (0); 2970Sstevel@tonic-gate } 2980Sstevel@tonic-gate #endif /* NATIVE_BUILD */ 2990Sstevel@tonic-gate 3000Sstevel@tonic-gate int 3010Sstevel@tonic-gate engine_interp() 3020Sstevel@tonic-gate { 3030Sstevel@tonic-gate #ifdef NATIVE_BUILD 3040Sstevel@tonic-gate uu_die("native build does not support interactive mode."); 3050Sstevel@tonic-gate #else 3060Sstevel@tonic-gate char *selfmri; 3070Sstevel@tonic-gate size_t sfsz; 3080Sstevel@tonic-gate int r; 3090Sstevel@tonic-gate 3100Sstevel@tonic-gate extern int parens; 3110Sstevel@tonic-gate 3120Sstevel@tonic-gate (void) sigset(SIGINT, SIG_IGN); 3130Sstevel@tonic-gate 3140Sstevel@tonic-gate est->sc_gl = new_GetLine(512, 8000); 3150Sstevel@tonic-gate if (est->sc_gl == NULL) 3160Sstevel@tonic-gate uu_die(gettext("Out of memory.\n")); 3170Sstevel@tonic-gate 3180Sstevel@tonic-gate /* The longest string is "[snapname]fmri[:instname]> ". */ 3190Sstevel@tonic-gate sfsz = 1 + max_scf_name_len + 1 + max_scf_fmri_len + 2 + 3200Sstevel@tonic-gate max_scf_name_len + 1 + 2 + 1; 3210Sstevel@tonic-gate selfmri = safe_malloc(sfsz); 3220Sstevel@tonic-gate 3230Sstevel@tonic-gate r = gl_customize_completion(est->sc_gl, NULL, complete); 3240Sstevel@tonic-gate assert(r == 0); 3250Sstevel@tonic-gate 3260Sstevel@tonic-gate for (;;) { 3270Sstevel@tonic-gate lscf_get_selection_str(selfmri, sfsz - 2); 3280Sstevel@tonic-gate (void) strcat(selfmri, "> "); 3290Sstevel@tonic-gate est->sc_cmd_buf = gl_get_line(est->sc_gl, selfmri, NULL, -1); 3300Sstevel@tonic-gate 3310Sstevel@tonic-gate if (est->sc_cmd_buf == NULL) { 3320Sstevel@tonic-gate switch (gl_return_status(est->sc_gl)) { 3330Sstevel@tonic-gate case GLR_SIGNAL: 3340Sstevel@tonic-gate gl_abandon_line(est->sc_gl); 3350Sstevel@tonic-gate continue; 3360Sstevel@tonic-gate 3370Sstevel@tonic-gate case GLR_EOF: 3380Sstevel@tonic-gate break; 3390Sstevel@tonic-gate 3400Sstevel@tonic-gate case GLR_ERROR: 3410Sstevel@tonic-gate uu_die(gettext("Error reading terminal: %s.\n"), 3420Sstevel@tonic-gate gl_error_message(est->sc_gl, NULL, 0)); 3430Sstevel@tonic-gate /* NOTREACHED */ 3440Sstevel@tonic-gate 3450Sstevel@tonic-gate default: 3460Sstevel@tonic-gate #ifndef NDEBUG 3470Sstevel@tonic-gate (void) fprintf(stderr, "%s:%d: gl_get_line() " 3480Sstevel@tonic-gate "returned unexpected value %d.\n", __FILE__, 3490Sstevel@tonic-gate __LINE__, gl_return_status(est->sc_gl)); 3500Sstevel@tonic-gate #endif 3510Sstevel@tonic-gate abort(); 3520Sstevel@tonic-gate } 3530Sstevel@tonic-gate 3540Sstevel@tonic-gate break; 3550Sstevel@tonic-gate } 3560Sstevel@tonic-gate 3570Sstevel@tonic-gate parens = 0; 3580Sstevel@tonic-gate est->sc_cmd_bufsz = strlen(est->sc_cmd_buf); 3590Sstevel@tonic-gate est->sc_cmd_bufoff = 0; 3600Sstevel@tonic-gate est->sc_cmd_flags = SC_CMD_IACTIVE; 3610Sstevel@tonic-gate 3620Sstevel@tonic-gate (void) yyparse(); 3630Sstevel@tonic-gate } 3640Sstevel@tonic-gate 3650Sstevel@tonic-gate free(selfmri); 3660Sstevel@tonic-gate est->sc_gl = del_GetLine(est->sc_gl); /* returns NULL */ 3670Sstevel@tonic-gate 3680Sstevel@tonic-gate #endif /* NATIVE_BUILD */ 3690Sstevel@tonic-gate return (0); 3700Sstevel@tonic-gate } 3710Sstevel@tonic-gate 3720Sstevel@tonic-gate int 3730Sstevel@tonic-gate engine_source(const char *name, boolean_t dont_exit) 3740Sstevel@tonic-gate { 3750Sstevel@tonic-gate engine_state_t *old = est; 3760Sstevel@tonic-gate struct stat st; 3770Sstevel@tonic-gate int ret; 3780Sstevel@tonic-gate 3790Sstevel@tonic-gate est = uu_zalloc(sizeof (engine_state_t)); 3800Sstevel@tonic-gate 3810Sstevel@tonic-gate /* first, copy the stuff set up in engine_init */ 3820Sstevel@tonic-gate est->sc_repo_pid = old->sc_repo_pid; 3830Sstevel@tonic-gate if (old->sc_repo_filename != NULL) 3840Sstevel@tonic-gate est->sc_repo_filename = safe_strdup(old->sc_repo_filename); 3850Sstevel@tonic-gate if (old->sc_repo_doordir != NULL) 3860Sstevel@tonic-gate est->sc_repo_doordir = safe_strdup(old->sc_repo_doordir); 3870Sstevel@tonic-gate if (old->sc_repo_doorname != NULL) 3880Sstevel@tonic-gate est->sc_repo_doorname = safe_strdup(old->sc_repo_doorname); 3890Sstevel@tonic-gate if (old->sc_repo_server != NULL) 3900Sstevel@tonic-gate est->sc_repo_server = safe_strdup(old->sc_repo_server); 3910Sstevel@tonic-gate 3920Sstevel@tonic-gate /* set up the new guy */ 3930Sstevel@tonic-gate est->sc_cmd_lineno = 1; 3940Sstevel@tonic-gate 3950Sstevel@tonic-gate if (dont_exit) 3960Sstevel@tonic-gate est->sc_cmd_flags |= SC_CMD_DONT_EXIT; 3970Sstevel@tonic-gate 3980Sstevel@tonic-gate if (strcmp(name, "-") == 0) { 3990Sstevel@tonic-gate est->sc_cmd_file = stdin; 4000Sstevel@tonic-gate est->sc_cmd_filename = "<stdin>"; 4010Sstevel@tonic-gate } else { 4020Sstevel@tonic-gate errno = 0; 4030Sstevel@tonic-gate est->sc_cmd_filename = name; 4040Sstevel@tonic-gate est->sc_cmd_file = fopen(name, "r"); 4050Sstevel@tonic-gate if (est->sc_cmd_file == NULL) { 4060Sstevel@tonic-gate if (errno == 0) 4070Sstevel@tonic-gate semerr(gettext("No free stdio streams.\n")); 4080Sstevel@tonic-gate else 4090Sstevel@tonic-gate semerr(gettext("Could not open %s"), name); 4100Sstevel@tonic-gate 4110Sstevel@tonic-gate ret = -1; 4120Sstevel@tonic-gate goto fail; 4130Sstevel@tonic-gate } 4140Sstevel@tonic-gate 4155040Swesolows do { 4160Sstevel@tonic-gate ret = fstat(fileno(est->sc_cmd_file), &st); 4175040Swesolows } while (ret != 0 && errno == EINTR); 4180Sstevel@tonic-gate if (ret != 0) { 4190Sstevel@tonic-gate (void) fclose(est->sc_cmd_file); 4200Sstevel@tonic-gate est->sc_cmd_file = NULL; /* for semerr() */ 4210Sstevel@tonic-gate 4220Sstevel@tonic-gate semerr(gettext("Could not stat %s"), name); 4230Sstevel@tonic-gate 4240Sstevel@tonic-gate ret = -1; 4250Sstevel@tonic-gate goto fail; 4260Sstevel@tonic-gate } 4270Sstevel@tonic-gate 4280Sstevel@tonic-gate if (!S_ISREG(st.st_mode)) { 4290Sstevel@tonic-gate (void) fclose(est->sc_cmd_file); 4300Sstevel@tonic-gate est->sc_cmd_file = NULL; /* for semerr() */ 4310Sstevel@tonic-gate 4320Sstevel@tonic-gate semerr(gettext("%s is not a regular file.\n"), name); 4330Sstevel@tonic-gate 4340Sstevel@tonic-gate ret = -1; 4350Sstevel@tonic-gate goto fail; 4360Sstevel@tonic-gate } 4370Sstevel@tonic-gate } 4380Sstevel@tonic-gate 4390Sstevel@tonic-gate (void) yyparse(); 4400Sstevel@tonic-gate 4410Sstevel@tonic-gate if (est->sc_cmd_file != stdin) 4420Sstevel@tonic-gate (void) fclose(est->sc_cmd_file); 4430Sstevel@tonic-gate 4440Sstevel@tonic-gate ret = 0; 4450Sstevel@tonic-gate 4460Sstevel@tonic-gate fail: 4470Sstevel@tonic-gate if (est->sc_repo_pid != old->sc_repo_pid) 4480Sstevel@tonic-gate lscf_cleanup(); /* clean up any new repository */ 4490Sstevel@tonic-gate 4500Sstevel@tonic-gate if (est->sc_repo_filename != NULL) 4510Sstevel@tonic-gate free((void *)est->sc_repo_filename); 4520Sstevel@tonic-gate if (est->sc_repo_doordir != NULL) 4530Sstevel@tonic-gate free((void *)est->sc_repo_doordir); 4540Sstevel@tonic-gate if (est->sc_repo_doorname != NULL) 4550Sstevel@tonic-gate free((void *)est->sc_repo_doorname); 4560Sstevel@tonic-gate if (est->sc_repo_server != NULL) 4570Sstevel@tonic-gate free((void *)est->sc_repo_server); 4580Sstevel@tonic-gate free(est); 4590Sstevel@tonic-gate 4600Sstevel@tonic-gate est = old; 4610Sstevel@tonic-gate 4620Sstevel@tonic-gate return (ret); 4630Sstevel@tonic-gate } 4640Sstevel@tonic-gate 4650Sstevel@tonic-gate /* 4660Sstevel@tonic-gate * Initialize svccfg state. We recognize four environment variables: 4670Sstevel@tonic-gate * 4680Sstevel@tonic-gate * SVCCFG_REPOSITORY Create a private instance of svc.configd(1M) to answer 4690Sstevel@tonic-gate * requests for the specified repository file. 4700Sstevel@tonic-gate * SVCCFG_DOOR_PATH Directory for door creation. 4710Sstevel@tonic-gate * 4720Sstevel@tonic-gate * SVCCFG_DOOR Rendezvous via an alternative repository door. 4730Sstevel@tonic-gate * 4740Sstevel@tonic-gate * SVCCFG_CONFIGD_PATH Resolvable path to alternative svc.configd(1M) binary. 4750Sstevel@tonic-gate */ 4760Sstevel@tonic-gate void 4770Sstevel@tonic-gate engine_init() 4780Sstevel@tonic-gate { 4790Sstevel@tonic-gate const char *cp; 4800Sstevel@tonic-gate 4810Sstevel@tonic-gate est = uu_zalloc(sizeof (engine_state_t)); 4820Sstevel@tonic-gate 4830Sstevel@tonic-gate est->sc_cmd_lineno = 1; 4840Sstevel@tonic-gate est->sc_repo_pid = -1; 4850Sstevel@tonic-gate 4860Sstevel@tonic-gate cp = getenv("SVCCFG_REPOSITORY"); 4870Sstevel@tonic-gate est->sc_repo_filename = cp ? safe_strdup(cp) : NULL; 4880Sstevel@tonic-gate 4890Sstevel@tonic-gate cp = getenv("SVCCFG_DOOR_PATH"); 4900Sstevel@tonic-gate est->sc_repo_doordir = cp ? cp : "/var/run"; 4910Sstevel@tonic-gate 4920Sstevel@tonic-gate cp = getenv("SVCCFG_DOOR"); 4930Sstevel@tonic-gate if (cp != NULL) { 4940Sstevel@tonic-gate if (est->sc_repo_filename != NULL) { 4950Sstevel@tonic-gate uu_warn(gettext("SVCCFG_DOOR unused when " 4960Sstevel@tonic-gate "SVCCFG_REPOSITORY specified\n")); 4970Sstevel@tonic-gate } else { 4980Sstevel@tonic-gate est->sc_repo_doorname = safe_strdup(cp); 4990Sstevel@tonic-gate } 5000Sstevel@tonic-gate } 5010Sstevel@tonic-gate 5020Sstevel@tonic-gate cp = getenv("SVCCFG_CONFIGD_PATH"); 5030Sstevel@tonic-gate est->sc_repo_server = cp ? cp : "/lib/svc/bin/svc.configd"; 5040Sstevel@tonic-gate } 5050Sstevel@tonic-gate 5060Sstevel@tonic-gate int 5070Sstevel@tonic-gate engine_import(uu_list_t *args) 5080Sstevel@tonic-gate { 5090Sstevel@tonic-gate int ret, argc, i, o; 5100Sstevel@tonic-gate bundle_t *b; 5110Sstevel@tonic-gate char *file, *pname; 5121061Scasper uchar_t hash[MHASH_SIZE]; 5130Sstevel@tonic-gate char **argv; 5140Sstevel@tonic-gate string_list_t *slp; 515*7887SLiane.Praza@Sun.COM boolean_t validate = B_FALSE; 516*7887SLiane.Praza@Sun.COM tmpl_validate_status_t vr; 5170Sstevel@tonic-gate uint_t flags = SCI_GENERALLAST; 518*7887SLiane.Praza@Sun.COM tmpl_errors_t *errs; 5190Sstevel@tonic-gate 5200Sstevel@tonic-gate argc = uu_list_numnodes(args); 5210Sstevel@tonic-gate if (argc < 1) 5220Sstevel@tonic-gate return (-2); 5230Sstevel@tonic-gate 5240Sstevel@tonic-gate argv = calloc(argc + 1, sizeof (char *)); 5250Sstevel@tonic-gate if (argv == NULL) 5260Sstevel@tonic-gate uu_die(gettext("Out of memory.\n")); 5270Sstevel@tonic-gate 5280Sstevel@tonic-gate for (slp = uu_list_first(args), i = 0; 5290Sstevel@tonic-gate slp != NULL; 5300Sstevel@tonic-gate slp = uu_list_next(args, slp), ++i) 5310Sstevel@tonic-gate argv[i] = slp->str; 5320Sstevel@tonic-gate 5330Sstevel@tonic-gate argv[i] = NULL; 5340Sstevel@tonic-gate 5350Sstevel@tonic-gate opterr = 0; 5360Sstevel@tonic-gate optind = 0; /* Remember, no argv[0]. */ 5370Sstevel@tonic-gate for (;;) { 5380Sstevel@tonic-gate o = getopt(argc, argv, "nV"); 5390Sstevel@tonic-gate if (o == -1) 5400Sstevel@tonic-gate break; 5410Sstevel@tonic-gate 5420Sstevel@tonic-gate switch (o) { 5430Sstevel@tonic-gate case 'n': 5440Sstevel@tonic-gate flags |= SCI_NOREFRESH; 5450Sstevel@tonic-gate break; 5460Sstevel@tonic-gate 5470Sstevel@tonic-gate case 'V': 548*7887SLiane.Praza@Sun.COM validate = B_TRUE; 5490Sstevel@tonic-gate break; 5500Sstevel@tonic-gate 5510Sstevel@tonic-gate case '?': 5520Sstevel@tonic-gate free(argv); 5530Sstevel@tonic-gate return (-2); 5540Sstevel@tonic-gate 5550Sstevel@tonic-gate default: 5560Sstevel@tonic-gate bad_error("getopt", o); 5570Sstevel@tonic-gate } 5580Sstevel@tonic-gate } 5590Sstevel@tonic-gate 5600Sstevel@tonic-gate argc -= optind; 5610Sstevel@tonic-gate if (argc != 1) { 5620Sstevel@tonic-gate free(argv); 5630Sstevel@tonic-gate return (-2); 5640Sstevel@tonic-gate } 5650Sstevel@tonic-gate 5660Sstevel@tonic-gate file = argv[optind]; 5670Sstevel@tonic-gate free(argv); 5680Sstevel@tonic-gate 569*7887SLiane.Praza@Sun.COM /* If we're in interactive mode, force strict validation. */ 570*7887SLiane.Praza@Sun.COM if (est->sc_cmd_flags & SC_CMD_IACTIVE) 571*7887SLiane.Praza@Sun.COM validate = B_TRUE; 572*7887SLiane.Praza@Sun.COM 5730Sstevel@tonic-gate lscf_prep_hndl(); 5740Sstevel@tonic-gate 5752462Scasper ret = mhash_test_file(g_hndl, file, 0, &pname, hash); 576*7887SLiane.Praza@Sun.COM if (ret != MHASH_NEWFILE) { 577*7887SLiane.Praza@Sun.COM if (ret == MHASH_FAILURE) 578*7887SLiane.Praza@Sun.COM semerr(gettext("Could not hash file %s\n"), file); 579*7887SLiane.Praza@Sun.COM else if (g_verbose && ret == MHASH_RECONCILED) 580*7887SLiane.Praza@Sun.COM warn(gettext("No changes were necessary.\n")); 5810Sstevel@tonic-gate return (ret); 582*7887SLiane.Praza@Sun.COM } 5830Sstevel@tonic-gate 5840Sstevel@tonic-gate /* Load */ 5850Sstevel@tonic-gate b = internal_bundle_new(); 5860Sstevel@tonic-gate 5875040Swesolows if (lxml_get_bundle_file(b, file, SVCCFG_OP_IMPORT) != 0) { 5880Sstevel@tonic-gate internal_bundle_free(b); 5890Sstevel@tonic-gate return (-1); 5900Sstevel@tonic-gate } 5910Sstevel@tonic-gate 592*7887SLiane.Praza@Sun.COM /* Validate */ 593*7887SLiane.Praza@Sun.COM if ((vr = tmpl_validate_bundle(b, &errs)) != TVS_SUCCESS) { 594*7887SLiane.Praza@Sun.COM char *prefix; 595*7887SLiane.Praza@Sun.COM 596*7887SLiane.Praza@Sun.COM if ((validate == 0) || (vr == TVS_WARN)) { 597*7887SLiane.Praza@Sun.COM prefix = gettext("Warning: "); 598*7887SLiane.Praza@Sun.COM } else { 599*7887SLiane.Praza@Sun.COM prefix = ""; 600*7887SLiane.Praza@Sun.COM } 601*7887SLiane.Praza@Sun.COM tmpl_errors_print(stderr, errs, prefix); 602*7887SLiane.Praza@Sun.COM if (validate && (vr != TVS_WARN)) { 603*7887SLiane.Praza@Sun.COM tmpl_errors_destroy(errs); 604*7887SLiane.Praza@Sun.COM semerr(gettext("Import failed.\n")); 605*7887SLiane.Praza@Sun.COM return (-1); 606*7887SLiane.Praza@Sun.COM } 607*7887SLiane.Praza@Sun.COM } 608*7887SLiane.Praza@Sun.COM tmpl_errors_destroy(errs); 609*7887SLiane.Praza@Sun.COM 6100Sstevel@tonic-gate /* Import */ 6110Sstevel@tonic-gate if (lscf_bundle_import(b, file, flags) != 0) { 6120Sstevel@tonic-gate internal_bundle_free(b); 613*7887SLiane.Praza@Sun.COM semerr(gettext("Import failed.\n")); 6140Sstevel@tonic-gate return (-1); 6150Sstevel@tonic-gate } 6160Sstevel@tonic-gate 6170Sstevel@tonic-gate internal_bundle_free(b); 6180Sstevel@tonic-gate 6190Sstevel@tonic-gate if (g_verbose) 6200Sstevel@tonic-gate warn(gettext("Successful import.\n")); 6210Sstevel@tonic-gate 6220Sstevel@tonic-gate if (pname) { 6230Sstevel@tonic-gate char *errstr; 6240Sstevel@tonic-gate 6250Sstevel@tonic-gate if (mhash_store_entry(g_hndl, pname, hash, &errstr)) { 6260Sstevel@tonic-gate if (errstr) 6270Sstevel@tonic-gate semerr(errstr); 6280Sstevel@tonic-gate else 6290Sstevel@tonic-gate semerr(gettext("Unknown error from " 6305040Swesolows "mhash_store_entry()\n")); 6310Sstevel@tonic-gate } 6320Sstevel@tonic-gate 6330Sstevel@tonic-gate free(pname); 6340Sstevel@tonic-gate } 6350Sstevel@tonic-gate 6360Sstevel@tonic-gate return (0); 6370Sstevel@tonic-gate } 6380Sstevel@tonic-gate 6390Sstevel@tonic-gate int 6400Sstevel@tonic-gate engine_apply(const char *file) 6410Sstevel@tonic-gate { 6420Sstevel@tonic-gate int ret; 6430Sstevel@tonic-gate bundle_t *b; 6440Sstevel@tonic-gate char *pname; 6451061Scasper uchar_t hash[MHASH_SIZE]; 6460Sstevel@tonic-gate 6470Sstevel@tonic-gate lscf_prep_hndl(); 6480Sstevel@tonic-gate 6492462Scasper ret = mhash_test_file(g_hndl, file, 1, &pname, hash); 6502462Scasper if (ret != MHASH_NEWFILE) 6510Sstevel@tonic-gate return (ret); 6520Sstevel@tonic-gate 6530Sstevel@tonic-gate b = internal_bundle_new(); 6540Sstevel@tonic-gate 6555040Swesolows if (lxml_get_bundle_file(b, file, SVCCFG_OP_APPLY) != 0) { 6560Sstevel@tonic-gate internal_bundle_free(b); 6570Sstevel@tonic-gate return (-1); 6580Sstevel@tonic-gate } 6590Sstevel@tonic-gate 6605777Stw21770 if (lscf_bundle_apply(b, file) != 0) { 6610Sstevel@tonic-gate internal_bundle_free(b); 6620Sstevel@tonic-gate return (-1); 6630Sstevel@tonic-gate } 6640Sstevel@tonic-gate 6650Sstevel@tonic-gate internal_bundle_free(b); 6660Sstevel@tonic-gate 6670Sstevel@tonic-gate if (pname) { 6680Sstevel@tonic-gate char *errstr; 6690Sstevel@tonic-gate if (mhash_store_entry(g_hndl, pname, hash, &errstr)) 6700Sstevel@tonic-gate semerr(errstr); 6710Sstevel@tonic-gate 6720Sstevel@tonic-gate free(pname); 6730Sstevel@tonic-gate } 6740Sstevel@tonic-gate 6750Sstevel@tonic-gate return (0); 6760Sstevel@tonic-gate } 6770Sstevel@tonic-gate 6780Sstevel@tonic-gate int 6795040Swesolows engine_restore(const char *file) 6805040Swesolows { 6815040Swesolows bundle_t *b; 6825040Swesolows 6835040Swesolows lscf_prep_hndl(); 6845040Swesolows 6855040Swesolows b = internal_bundle_new(); 6865040Swesolows 6875040Swesolows if (lxml_get_bundle_file(b, file, SVCCFG_OP_RESTORE) != 0) { 6885040Swesolows internal_bundle_free(b); 6895040Swesolows return (-1); 6905040Swesolows } 6915040Swesolows 6925040Swesolows if (lscf_bundle_import(b, file, SCI_NOSNAP) != 0) { 6935040Swesolows internal_bundle_free(b); 6945040Swesolows return (-1); 6955040Swesolows } 6965040Swesolows 6975040Swesolows internal_bundle_free(b); 6985040Swesolows 6995040Swesolows return (0); 7005040Swesolows } 7015040Swesolows 7025040Swesolows int 7030Sstevel@tonic-gate engine_set(uu_list_t *args) 7040Sstevel@tonic-gate { 7050Sstevel@tonic-gate uu_list_walk_t *walk; 7060Sstevel@tonic-gate string_list_t *slp; 7070Sstevel@tonic-gate 7080Sstevel@tonic-gate if (uu_list_first(args) == NULL) { 7090Sstevel@tonic-gate /* Display current options. */ 7100Sstevel@tonic-gate if (!g_verbose) 7110Sstevel@tonic-gate (void) fputs("no", stdout); 7120Sstevel@tonic-gate (void) puts("verbose"); 7130Sstevel@tonic-gate 7140Sstevel@tonic-gate return (0); 7150Sstevel@tonic-gate } 7160Sstevel@tonic-gate 7170Sstevel@tonic-gate walk = uu_list_walk_start(args, UU_DEFAULT); 7180Sstevel@tonic-gate if (walk == NULL) 7190Sstevel@tonic-gate uu_die(gettext("Couldn't read arguments")); 7200Sstevel@tonic-gate 7210Sstevel@tonic-gate /* Use getopt? */ 7220Sstevel@tonic-gate for (slp = uu_list_walk_next(walk); 7230Sstevel@tonic-gate slp != NULL; 7240Sstevel@tonic-gate slp = uu_list_walk_next(walk)) { 7250Sstevel@tonic-gate if (slp->str[0] == '-') { 7260Sstevel@tonic-gate char *op; 7270Sstevel@tonic-gate 7280Sstevel@tonic-gate for (op = &slp->str[1]; *op != '\0'; ++op) { 7290Sstevel@tonic-gate switch (*op) { 7300Sstevel@tonic-gate case 'v': 7310Sstevel@tonic-gate g_verbose = 1; 7320Sstevel@tonic-gate break; 7330Sstevel@tonic-gate 7340Sstevel@tonic-gate case 'V': 7350Sstevel@tonic-gate g_verbose = 0; 7360Sstevel@tonic-gate break; 7370Sstevel@tonic-gate 7380Sstevel@tonic-gate default: 7390Sstevel@tonic-gate warn(gettext("Unknown option -%c.\n"), 7400Sstevel@tonic-gate *op); 7410Sstevel@tonic-gate } 7420Sstevel@tonic-gate } 7430Sstevel@tonic-gate } else { 7440Sstevel@tonic-gate warn(gettext("No non-flag arguments defined.\n")); 7450Sstevel@tonic-gate } 7460Sstevel@tonic-gate } 7470Sstevel@tonic-gate 7480Sstevel@tonic-gate return (0); 7490Sstevel@tonic-gate } 7500Sstevel@tonic-gate 7510Sstevel@tonic-gate void 7520Sstevel@tonic-gate help(int com) 7530Sstevel@tonic-gate { 7540Sstevel@tonic-gate int i; 7550Sstevel@tonic-gate 7560Sstevel@tonic-gate if (com == 0) { 7570Sstevel@tonic-gate warn(gettext("General commands: help set repository end\n" 7580Sstevel@tonic-gate "Manifest commands: inventory validate import export " 7590Sstevel@tonic-gate "archive\n" 7600Sstevel@tonic-gate "Profile commands: apply extract\n" 761*7887SLiane.Praza@Sun.COM "Entity commands: list select unselect add delete " 762*7887SLiane.Praza@Sun.COM "describe\n" 7630Sstevel@tonic-gate "Snapshot commands: listsnap selectsnap revert\n" 7645841Samaguire "Instance commands: refresh\n" 7650Sstevel@tonic-gate "Property group commands: listpg addpg delpg\n" 7660Sstevel@tonic-gate "Property commands: listprop setprop delprop editprop\n" 7670Sstevel@tonic-gate "Property value commands: addpropvalue delpropvalue " 7680Sstevel@tonic-gate "setenv unsetenv\n")); 7690Sstevel@tonic-gate return; 7700Sstevel@tonic-gate } 7710Sstevel@tonic-gate 7720Sstevel@tonic-gate for (i = 0; help_messages[i].message != NULL; ++i) { 7730Sstevel@tonic-gate if (help_messages[i].token == com) { 7740Sstevel@tonic-gate warn(gettext("Usage: %s\n"), 7750Sstevel@tonic-gate gettext(help_messages[i].message)); 7760Sstevel@tonic-gate return; 7770Sstevel@tonic-gate } 7780Sstevel@tonic-gate } 7790Sstevel@tonic-gate 7800Sstevel@tonic-gate warn(gettext("Unknown command.\n")); 7810Sstevel@tonic-gate } 782