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 */ 210Sstevel@tonic-gate /* 22*2462Scasper * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 230Sstevel@tonic-gate * Use is subject to license terms. 240Sstevel@tonic-gate */ 250Sstevel@tonic-gate 260Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 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 }, 2180Sstevel@tonic-gate { "listprop", CS_SVC | CS_INST | CS_SNAP, NULL }, 2190Sstevel@tonic-gate { "setprop", CS_SVC | CS_INST, NULL }, 2200Sstevel@tonic-gate { "delprop", CS_SVC | CS_INST, NULL }, 2210Sstevel@tonic-gate { "editprop", CS_SVC | CS_INST, NULL }, 2220Sstevel@tonic-gate { "listsnap", CS_INST | CS_SNAP, NULL }, 2230Sstevel@tonic-gate { "selectsnap", CS_INST | CS_SNAP, NULL }, 2240Sstevel@tonic-gate { "revert", CS_INST | CS_SNAP, NULL }, 2250Sstevel@tonic-gate { NULL } 2260Sstevel@tonic-gate }; 2270Sstevel@tonic-gate 2280Sstevel@tonic-gate int 2290Sstevel@tonic-gate add_cmd_matches(WordCompletion *cpl, const char *line, int word_end, 2300Sstevel@tonic-gate uint32_t scope) 2310Sstevel@tonic-gate { 2320Sstevel@tonic-gate int word_start, err; 2330Sstevel@tonic-gate size_t len; 2340Sstevel@tonic-gate const char *bol; 2350Sstevel@tonic-gate struct cmd_info *cip; 2360Sstevel@tonic-gate 2370Sstevel@tonic-gate word_start = strspn(line, whitespace); 2380Sstevel@tonic-gate len = word_end - word_start; 2390Sstevel@tonic-gate bol = line + word_end - len; 2400Sstevel@tonic-gate 2410Sstevel@tonic-gate for (cip = cmds; cip->name != NULL; ++cip) { 2420Sstevel@tonic-gate if ((cip->flags & scope) == 0) 2430Sstevel@tonic-gate continue; 2440Sstevel@tonic-gate 2450Sstevel@tonic-gate if (strncmp(cip->name, bol, len) == 0) { 2460Sstevel@tonic-gate err = cpl_add_completion(cpl, line, word_start, 2470Sstevel@tonic-gate word_end, cip->name + len, "", " "); 2480Sstevel@tonic-gate if (err != 0) 2490Sstevel@tonic-gate return (err); 2500Sstevel@tonic-gate } 2510Sstevel@tonic-gate } 2520Sstevel@tonic-gate 2530Sstevel@tonic-gate return (0); 2540Sstevel@tonic-gate } 2550Sstevel@tonic-gate 2560Sstevel@tonic-gate /* 2570Sstevel@tonic-gate * Suggest completions. We must first determine if the cursor is in command 2580Sstevel@tonic-gate * position or in argument position. If the former, complete_command() finds 2590Sstevel@tonic-gate * matching commands. If the latter, we tail-call the command-specific 2600Sstevel@tonic-gate * argument-completion routine in the cmds table. 2610Sstevel@tonic-gate */ 2620Sstevel@tonic-gate /* ARGSUSED */ 2630Sstevel@tonic-gate static 2640Sstevel@tonic-gate CPL_MATCH_FN(complete) 2650Sstevel@tonic-gate { 2660Sstevel@tonic-gate const char *arg0, *arg1; 2670Sstevel@tonic-gate size_t arg0len; 2680Sstevel@tonic-gate struct cmd_info *cip; 2690Sstevel@tonic-gate 2700Sstevel@tonic-gate arg0 = line + strspn(line, whitespace); 2710Sstevel@tonic-gate arg0len = strcspn(arg0, whitespace); 2720Sstevel@tonic-gate if ((arg0 + arg0len) - line >= word_end || 2730Sstevel@tonic-gate (arg0[arg0len] != ' ' && arg0[arg0len] != '\t')) 2740Sstevel@tonic-gate return (complete_command(cpl, (void *)arg0, line, word_end)); 2750Sstevel@tonic-gate 2760Sstevel@tonic-gate arg1 = arg0 + arg0len; 2770Sstevel@tonic-gate arg1 += strspn(arg1, whitespace); 2780Sstevel@tonic-gate 2790Sstevel@tonic-gate for (cip = cmds; cip->name != NULL; ++cip) { 2800Sstevel@tonic-gate if (strlen(cip->name) != arg0len) 2810Sstevel@tonic-gate continue; 2820Sstevel@tonic-gate 2830Sstevel@tonic-gate if (strncmp(cip->name, arg0, arg0len) != 0) 2840Sstevel@tonic-gate continue; 2850Sstevel@tonic-gate 2860Sstevel@tonic-gate if (cip->complete_args_f == NULL) 2870Sstevel@tonic-gate break; 2880Sstevel@tonic-gate 2890Sstevel@tonic-gate return (cip->complete_args_f(cpl, (void *)arg1, line, 2900Sstevel@tonic-gate word_end)); 2910Sstevel@tonic-gate } 2920Sstevel@tonic-gate 2930Sstevel@tonic-gate return (0); 2940Sstevel@tonic-gate } 2950Sstevel@tonic-gate #endif /* NATIVE_BUILD */ 2960Sstevel@tonic-gate 2970Sstevel@tonic-gate int 2980Sstevel@tonic-gate engine_interp() 2990Sstevel@tonic-gate { 3000Sstevel@tonic-gate #ifdef NATIVE_BUILD 3010Sstevel@tonic-gate uu_die("native build does not support interactive mode."); 3020Sstevel@tonic-gate #else 3030Sstevel@tonic-gate char *selfmri; 3040Sstevel@tonic-gate size_t sfsz; 3050Sstevel@tonic-gate int r; 3060Sstevel@tonic-gate 3070Sstevel@tonic-gate extern int parens; 3080Sstevel@tonic-gate 3090Sstevel@tonic-gate (void) sigset(SIGINT, SIG_IGN); 3100Sstevel@tonic-gate 3110Sstevel@tonic-gate est->sc_gl = new_GetLine(512, 8000); 3120Sstevel@tonic-gate if (est->sc_gl == NULL) 3130Sstevel@tonic-gate uu_die(gettext("Out of memory.\n")); 3140Sstevel@tonic-gate 3150Sstevel@tonic-gate /* The longest string is "[snapname]fmri[:instname]> ". */ 3160Sstevel@tonic-gate sfsz = 1 + max_scf_name_len + 1 + max_scf_fmri_len + 2 + 3170Sstevel@tonic-gate max_scf_name_len + 1 + 2 + 1; 3180Sstevel@tonic-gate selfmri = safe_malloc(sfsz); 3190Sstevel@tonic-gate 3200Sstevel@tonic-gate r = gl_customize_completion(est->sc_gl, NULL, complete); 3210Sstevel@tonic-gate assert(r == 0); 3220Sstevel@tonic-gate 3230Sstevel@tonic-gate for (;;) { 3240Sstevel@tonic-gate lscf_get_selection_str(selfmri, sfsz - 2); 3250Sstevel@tonic-gate (void) strcat(selfmri, "> "); 3260Sstevel@tonic-gate est->sc_cmd_buf = gl_get_line(est->sc_gl, selfmri, NULL, -1); 3270Sstevel@tonic-gate 3280Sstevel@tonic-gate if (est->sc_cmd_buf == NULL) { 3290Sstevel@tonic-gate switch (gl_return_status(est->sc_gl)) { 3300Sstevel@tonic-gate case GLR_SIGNAL: 3310Sstevel@tonic-gate gl_abandon_line(est->sc_gl); 3320Sstevel@tonic-gate continue; 3330Sstevel@tonic-gate 3340Sstevel@tonic-gate case GLR_EOF: 3350Sstevel@tonic-gate break; 3360Sstevel@tonic-gate 3370Sstevel@tonic-gate case GLR_ERROR: 3380Sstevel@tonic-gate uu_die(gettext("Error reading terminal: %s.\n"), 3390Sstevel@tonic-gate gl_error_message(est->sc_gl, NULL, 0)); 3400Sstevel@tonic-gate /* NOTREACHED */ 3410Sstevel@tonic-gate 3420Sstevel@tonic-gate default: 3430Sstevel@tonic-gate #ifndef NDEBUG 3440Sstevel@tonic-gate (void) fprintf(stderr, "%s:%d: gl_get_line() " 3450Sstevel@tonic-gate "returned unexpected value %d.\n", __FILE__, 3460Sstevel@tonic-gate __LINE__, gl_return_status(est->sc_gl)); 3470Sstevel@tonic-gate #endif 3480Sstevel@tonic-gate abort(); 3490Sstevel@tonic-gate } 3500Sstevel@tonic-gate 3510Sstevel@tonic-gate break; 3520Sstevel@tonic-gate } 3530Sstevel@tonic-gate 3540Sstevel@tonic-gate parens = 0; 3550Sstevel@tonic-gate est->sc_cmd_bufsz = strlen(est->sc_cmd_buf); 3560Sstevel@tonic-gate est->sc_cmd_bufoff = 0; 3570Sstevel@tonic-gate est->sc_cmd_flags = SC_CMD_IACTIVE; 3580Sstevel@tonic-gate 3590Sstevel@tonic-gate (void) yyparse(); 3600Sstevel@tonic-gate } 3610Sstevel@tonic-gate 3620Sstevel@tonic-gate free(selfmri); 3630Sstevel@tonic-gate est->sc_gl = del_GetLine(est->sc_gl); /* returns NULL */ 3640Sstevel@tonic-gate 3650Sstevel@tonic-gate #endif /* NATIVE_BUILD */ 3660Sstevel@tonic-gate return (0); 3670Sstevel@tonic-gate } 3680Sstevel@tonic-gate 3690Sstevel@tonic-gate int 3700Sstevel@tonic-gate engine_source(const char *name, boolean_t dont_exit) 3710Sstevel@tonic-gate { 3720Sstevel@tonic-gate engine_state_t *old = est; 3730Sstevel@tonic-gate struct stat st; 3740Sstevel@tonic-gate int ret; 3750Sstevel@tonic-gate 3760Sstevel@tonic-gate est = uu_zalloc(sizeof (engine_state_t)); 3770Sstevel@tonic-gate 3780Sstevel@tonic-gate /* first, copy the stuff set up in engine_init */ 3790Sstevel@tonic-gate est->sc_repo_pid = old->sc_repo_pid; 3800Sstevel@tonic-gate if (old->sc_repo_filename != NULL) 3810Sstevel@tonic-gate est->sc_repo_filename = safe_strdup(old->sc_repo_filename); 3820Sstevel@tonic-gate if (old->sc_repo_doordir != NULL) 3830Sstevel@tonic-gate est->sc_repo_doordir = safe_strdup(old->sc_repo_doordir); 3840Sstevel@tonic-gate if (old->sc_repo_doorname != NULL) 3850Sstevel@tonic-gate est->sc_repo_doorname = safe_strdup(old->sc_repo_doorname); 3860Sstevel@tonic-gate if (old->sc_repo_server != NULL) 3870Sstevel@tonic-gate est->sc_repo_server = safe_strdup(old->sc_repo_server); 3880Sstevel@tonic-gate 3890Sstevel@tonic-gate /* set up the new guy */ 3900Sstevel@tonic-gate est->sc_cmd_lineno = 1; 3910Sstevel@tonic-gate 3920Sstevel@tonic-gate if (dont_exit) 3930Sstevel@tonic-gate est->sc_cmd_flags |= SC_CMD_DONT_EXIT; 3940Sstevel@tonic-gate 3950Sstevel@tonic-gate if (strcmp(name, "-") == 0) { 3960Sstevel@tonic-gate est->sc_cmd_file = stdin; 3970Sstevel@tonic-gate est->sc_cmd_filename = "<stdin>"; 3980Sstevel@tonic-gate } else { 3990Sstevel@tonic-gate errno = 0; 4000Sstevel@tonic-gate est->sc_cmd_filename = name; 4010Sstevel@tonic-gate est->sc_cmd_file = fopen(name, "r"); 4020Sstevel@tonic-gate if (est->sc_cmd_file == NULL) { 4030Sstevel@tonic-gate if (errno == 0) 4040Sstevel@tonic-gate semerr(gettext("No free stdio streams.\n")); 4050Sstevel@tonic-gate else 4060Sstevel@tonic-gate semerr(gettext("Could not open %s"), name); 4070Sstevel@tonic-gate 4080Sstevel@tonic-gate ret = -1; 4090Sstevel@tonic-gate goto fail; 4100Sstevel@tonic-gate } 4110Sstevel@tonic-gate 4120Sstevel@tonic-gate do 4130Sstevel@tonic-gate ret = fstat(fileno(est->sc_cmd_file), &st); 4140Sstevel@tonic-gate while (ret != 0 && errno == EINTR); 4150Sstevel@tonic-gate if (ret != 0) { 4160Sstevel@tonic-gate (void) fclose(est->sc_cmd_file); 4170Sstevel@tonic-gate est->sc_cmd_file = NULL; /* for semerr() */ 4180Sstevel@tonic-gate 4190Sstevel@tonic-gate semerr(gettext("Could not stat %s"), name); 4200Sstevel@tonic-gate 4210Sstevel@tonic-gate ret = -1; 4220Sstevel@tonic-gate goto fail; 4230Sstevel@tonic-gate } 4240Sstevel@tonic-gate 4250Sstevel@tonic-gate if (!S_ISREG(st.st_mode)) { 4260Sstevel@tonic-gate (void) fclose(est->sc_cmd_file); 4270Sstevel@tonic-gate est->sc_cmd_file = NULL; /* for semerr() */ 4280Sstevel@tonic-gate 4290Sstevel@tonic-gate semerr(gettext("%s is not a regular file.\n"), name); 4300Sstevel@tonic-gate 4310Sstevel@tonic-gate ret = -1; 4320Sstevel@tonic-gate goto fail; 4330Sstevel@tonic-gate } 4340Sstevel@tonic-gate } 4350Sstevel@tonic-gate 4360Sstevel@tonic-gate (void) yyparse(); 4370Sstevel@tonic-gate 4380Sstevel@tonic-gate if (est->sc_cmd_file != stdin) 4390Sstevel@tonic-gate (void) fclose(est->sc_cmd_file); 4400Sstevel@tonic-gate 4410Sstevel@tonic-gate ret = 0; 4420Sstevel@tonic-gate 4430Sstevel@tonic-gate fail: 4440Sstevel@tonic-gate if (est->sc_repo_pid != old->sc_repo_pid) 4450Sstevel@tonic-gate lscf_cleanup(); /* clean up any new repository */ 4460Sstevel@tonic-gate 4470Sstevel@tonic-gate if (est->sc_repo_filename != NULL) 4480Sstevel@tonic-gate free((void *)est->sc_repo_filename); 4490Sstevel@tonic-gate if (est->sc_repo_doordir != NULL) 4500Sstevel@tonic-gate free((void *)est->sc_repo_doordir); 4510Sstevel@tonic-gate if (est->sc_repo_doorname != NULL) 4520Sstevel@tonic-gate free((void *)est->sc_repo_doorname); 4530Sstevel@tonic-gate if (est->sc_repo_server != NULL) 4540Sstevel@tonic-gate free((void *)est->sc_repo_server); 4550Sstevel@tonic-gate free(est); 4560Sstevel@tonic-gate 4570Sstevel@tonic-gate est = old; 4580Sstevel@tonic-gate 4590Sstevel@tonic-gate return (ret); 4600Sstevel@tonic-gate } 4610Sstevel@tonic-gate 4620Sstevel@tonic-gate /* 4630Sstevel@tonic-gate * Initialize svccfg state. We recognize four environment variables: 4640Sstevel@tonic-gate * 4650Sstevel@tonic-gate * SVCCFG_REPOSITORY Create a private instance of svc.configd(1M) to answer 4660Sstevel@tonic-gate * requests for the specified repository file. 4670Sstevel@tonic-gate * SVCCFG_DOOR_PATH Directory for door creation. 4680Sstevel@tonic-gate * 4690Sstevel@tonic-gate * SVCCFG_DOOR Rendezvous via an alternative repository door. 4700Sstevel@tonic-gate * 4710Sstevel@tonic-gate * SVCCFG_CONFIGD_PATH Resolvable path to alternative svc.configd(1M) binary. 4720Sstevel@tonic-gate */ 4730Sstevel@tonic-gate void 4740Sstevel@tonic-gate engine_init() 4750Sstevel@tonic-gate { 4760Sstevel@tonic-gate const char *cp; 4770Sstevel@tonic-gate 4780Sstevel@tonic-gate est = uu_zalloc(sizeof (engine_state_t)); 4790Sstevel@tonic-gate 4800Sstevel@tonic-gate est->sc_cmd_lineno = 1; 4810Sstevel@tonic-gate est->sc_repo_pid = -1; 4820Sstevel@tonic-gate 4830Sstevel@tonic-gate cp = getenv("SVCCFG_REPOSITORY"); 4840Sstevel@tonic-gate est->sc_repo_filename = cp ? safe_strdup(cp) : NULL; 4850Sstevel@tonic-gate 4860Sstevel@tonic-gate cp = getenv("SVCCFG_DOOR_PATH"); 4870Sstevel@tonic-gate est->sc_repo_doordir = cp ? cp : "/var/run"; 4880Sstevel@tonic-gate 4890Sstevel@tonic-gate cp = getenv("SVCCFG_DOOR"); 4900Sstevel@tonic-gate if (cp != NULL) { 4910Sstevel@tonic-gate if (est->sc_repo_filename != NULL) { 4920Sstevel@tonic-gate uu_warn(gettext("SVCCFG_DOOR unused when " 4930Sstevel@tonic-gate "SVCCFG_REPOSITORY specified\n")); 4940Sstevel@tonic-gate } else { 4950Sstevel@tonic-gate est->sc_repo_doorname = safe_strdup(cp); 4960Sstevel@tonic-gate } 4970Sstevel@tonic-gate } 4980Sstevel@tonic-gate 4990Sstevel@tonic-gate cp = getenv("SVCCFG_CONFIGD_PATH"); 5000Sstevel@tonic-gate est->sc_repo_server = cp ? cp : "/lib/svc/bin/svc.configd"; 5010Sstevel@tonic-gate } 5020Sstevel@tonic-gate 5030Sstevel@tonic-gate int 5040Sstevel@tonic-gate engine_import(uu_list_t *args) 5050Sstevel@tonic-gate { 5060Sstevel@tonic-gate int ret, argc, i, o; 5070Sstevel@tonic-gate bundle_t *b; 5080Sstevel@tonic-gate char *file, *pname; 5091061Scasper uchar_t hash[MHASH_SIZE]; 5100Sstevel@tonic-gate char **argv; 5110Sstevel@tonic-gate string_list_t *slp; 5120Sstevel@tonic-gate boolean_t verify = B_FALSE; 5130Sstevel@tonic-gate uint_t flags = SCI_GENERALLAST; 5140Sstevel@tonic-gate 5150Sstevel@tonic-gate argc = uu_list_numnodes(args); 5160Sstevel@tonic-gate if (argc < 1) 5170Sstevel@tonic-gate return (-2); 5180Sstevel@tonic-gate 5190Sstevel@tonic-gate argv = calloc(argc + 1, sizeof (char *)); 5200Sstevel@tonic-gate if (argv == NULL) 5210Sstevel@tonic-gate uu_die(gettext("Out of memory.\n")); 5220Sstevel@tonic-gate 5230Sstevel@tonic-gate for (slp = uu_list_first(args), i = 0; 5240Sstevel@tonic-gate slp != NULL; 5250Sstevel@tonic-gate slp = uu_list_next(args, slp), ++i) 5260Sstevel@tonic-gate argv[i] = slp->str; 5270Sstevel@tonic-gate 5280Sstevel@tonic-gate argv[i] = NULL; 5290Sstevel@tonic-gate 5300Sstevel@tonic-gate opterr = 0; 5310Sstevel@tonic-gate optind = 0; /* Remember, no argv[0]. */ 5320Sstevel@tonic-gate for (;;) { 5330Sstevel@tonic-gate o = getopt(argc, argv, "nV"); 5340Sstevel@tonic-gate if (o == -1) 5350Sstevel@tonic-gate break; 5360Sstevel@tonic-gate 5370Sstevel@tonic-gate switch (o) { 5380Sstevel@tonic-gate case 'n': 5390Sstevel@tonic-gate flags |= SCI_NOREFRESH; 5400Sstevel@tonic-gate break; 5410Sstevel@tonic-gate 5420Sstevel@tonic-gate case 'V': 5430Sstevel@tonic-gate verify = B_TRUE; 5440Sstevel@tonic-gate break; 5450Sstevel@tonic-gate 5460Sstevel@tonic-gate case '?': 5470Sstevel@tonic-gate free(argv); 5480Sstevel@tonic-gate return (-2); 5490Sstevel@tonic-gate 5500Sstevel@tonic-gate default: 5510Sstevel@tonic-gate bad_error("getopt", o); 5520Sstevel@tonic-gate } 5530Sstevel@tonic-gate } 5540Sstevel@tonic-gate 5550Sstevel@tonic-gate argc -= optind; 5560Sstevel@tonic-gate if (argc != 1) { 5570Sstevel@tonic-gate free(argv); 5580Sstevel@tonic-gate return (-2); 5590Sstevel@tonic-gate } 5600Sstevel@tonic-gate 5610Sstevel@tonic-gate file = argv[optind]; 5620Sstevel@tonic-gate free(argv); 5630Sstevel@tonic-gate 5640Sstevel@tonic-gate lscf_prep_hndl(); 5650Sstevel@tonic-gate 566*2462Scasper ret = mhash_test_file(g_hndl, file, 0, &pname, hash); 567*2462Scasper if (ret != MHASH_NEWFILE) 5680Sstevel@tonic-gate return (ret); 5690Sstevel@tonic-gate 5700Sstevel@tonic-gate /* Load */ 5710Sstevel@tonic-gate b = internal_bundle_new(); 5720Sstevel@tonic-gate 5730Sstevel@tonic-gate if (lxml_get_bundle_file(b, file, 0) != 0) { 5740Sstevel@tonic-gate internal_bundle_free(b); 5750Sstevel@tonic-gate return (-1); 5760Sstevel@tonic-gate } 5770Sstevel@tonic-gate 5780Sstevel@tonic-gate /* Import */ 5790Sstevel@tonic-gate if (lscf_bundle_import(b, file, flags) != 0) { 5800Sstevel@tonic-gate internal_bundle_free(b); 5810Sstevel@tonic-gate return (-1); 5820Sstevel@tonic-gate } 5830Sstevel@tonic-gate 5840Sstevel@tonic-gate internal_bundle_free(b); 5850Sstevel@tonic-gate 5860Sstevel@tonic-gate if (g_verbose) 5870Sstevel@tonic-gate warn(gettext("Successful import.\n")); 5880Sstevel@tonic-gate 5890Sstevel@tonic-gate if (pname) { 5900Sstevel@tonic-gate char *errstr; 5910Sstevel@tonic-gate 5920Sstevel@tonic-gate if (mhash_store_entry(g_hndl, pname, hash, &errstr)) { 5930Sstevel@tonic-gate if (errstr) 5940Sstevel@tonic-gate semerr(errstr); 5950Sstevel@tonic-gate else 5960Sstevel@tonic-gate semerr(gettext("Unknown error from " 5970Sstevel@tonic-gate "mhash_store_entry()\n")); 5980Sstevel@tonic-gate } 5990Sstevel@tonic-gate 6000Sstevel@tonic-gate free(pname); 6010Sstevel@tonic-gate } 6020Sstevel@tonic-gate 6030Sstevel@tonic-gate /* Verify */ 6040Sstevel@tonic-gate if (verify) 6050Sstevel@tonic-gate warn(gettext("import -V not implemented.\n")); 6060Sstevel@tonic-gate 6070Sstevel@tonic-gate return (0); 6080Sstevel@tonic-gate } 6090Sstevel@tonic-gate 6100Sstevel@tonic-gate int 6110Sstevel@tonic-gate engine_apply(const char *file) 6120Sstevel@tonic-gate { 6130Sstevel@tonic-gate int ret; 6140Sstevel@tonic-gate bundle_t *b; 6150Sstevel@tonic-gate char *pname; 6161061Scasper uchar_t hash[MHASH_SIZE]; 6170Sstevel@tonic-gate 6180Sstevel@tonic-gate lscf_prep_hndl(); 6190Sstevel@tonic-gate 620*2462Scasper ret = mhash_test_file(g_hndl, file, 1, &pname, hash); 621*2462Scasper if (ret != MHASH_NEWFILE) 6220Sstevel@tonic-gate return (ret); 6230Sstevel@tonic-gate 6240Sstevel@tonic-gate b = internal_bundle_new(); 6250Sstevel@tonic-gate 6260Sstevel@tonic-gate if (lxml_get_bundle_file(b, file, 1) != 0) { 6270Sstevel@tonic-gate internal_bundle_free(b); 6280Sstevel@tonic-gate return (-1); 6290Sstevel@tonic-gate } 6300Sstevel@tonic-gate 6310Sstevel@tonic-gate if (lscf_bundle_apply(b) != 0) { 6320Sstevel@tonic-gate internal_bundle_free(b); 6330Sstevel@tonic-gate return (-1); 6340Sstevel@tonic-gate } 6350Sstevel@tonic-gate 6360Sstevel@tonic-gate internal_bundle_free(b); 6370Sstevel@tonic-gate 6380Sstevel@tonic-gate if (pname) { 6390Sstevel@tonic-gate char *errstr; 6400Sstevel@tonic-gate if (mhash_store_entry(g_hndl, pname, hash, &errstr)) 6410Sstevel@tonic-gate semerr(errstr); 6420Sstevel@tonic-gate 6430Sstevel@tonic-gate free(pname); 6440Sstevel@tonic-gate } 6450Sstevel@tonic-gate 6460Sstevel@tonic-gate return (0); 6470Sstevel@tonic-gate } 6480Sstevel@tonic-gate 6490Sstevel@tonic-gate int 6500Sstevel@tonic-gate engine_set(uu_list_t *args) 6510Sstevel@tonic-gate { 6520Sstevel@tonic-gate uu_list_walk_t *walk; 6530Sstevel@tonic-gate string_list_t *slp; 6540Sstevel@tonic-gate 6550Sstevel@tonic-gate if (uu_list_first(args) == NULL) { 6560Sstevel@tonic-gate /* Display current options. */ 6570Sstevel@tonic-gate if (!g_verbose) 6580Sstevel@tonic-gate (void) fputs("no", stdout); 6590Sstevel@tonic-gate (void) puts("verbose"); 6600Sstevel@tonic-gate 6610Sstevel@tonic-gate return (0); 6620Sstevel@tonic-gate } 6630Sstevel@tonic-gate 6640Sstevel@tonic-gate walk = uu_list_walk_start(args, UU_DEFAULT); 6650Sstevel@tonic-gate if (walk == NULL) 6660Sstevel@tonic-gate uu_die(gettext("Couldn't read arguments")); 6670Sstevel@tonic-gate 6680Sstevel@tonic-gate /* Use getopt? */ 6690Sstevel@tonic-gate for (slp = uu_list_walk_next(walk); 6700Sstevel@tonic-gate slp != NULL; 6710Sstevel@tonic-gate slp = uu_list_walk_next(walk)) { 6720Sstevel@tonic-gate if (slp->str[0] == '-') { 6730Sstevel@tonic-gate char *op; 6740Sstevel@tonic-gate 6750Sstevel@tonic-gate for (op = &slp->str[1]; *op != '\0'; ++op) { 6760Sstevel@tonic-gate switch (*op) { 6770Sstevel@tonic-gate case 'v': 6780Sstevel@tonic-gate g_verbose = 1; 6790Sstevel@tonic-gate break; 6800Sstevel@tonic-gate 6810Sstevel@tonic-gate case 'V': 6820Sstevel@tonic-gate g_verbose = 0; 6830Sstevel@tonic-gate break; 6840Sstevel@tonic-gate 6850Sstevel@tonic-gate default: 6860Sstevel@tonic-gate warn(gettext("Unknown option -%c.\n"), 6870Sstevel@tonic-gate *op); 6880Sstevel@tonic-gate } 6890Sstevel@tonic-gate } 6900Sstevel@tonic-gate } else { 6910Sstevel@tonic-gate warn(gettext("No non-flag arguments defined.\n")); 6920Sstevel@tonic-gate } 6930Sstevel@tonic-gate } 6940Sstevel@tonic-gate 6950Sstevel@tonic-gate return (0); 6960Sstevel@tonic-gate } 6970Sstevel@tonic-gate 6980Sstevel@tonic-gate void 6990Sstevel@tonic-gate help(int com) 7000Sstevel@tonic-gate { 7010Sstevel@tonic-gate int i; 7020Sstevel@tonic-gate 7030Sstevel@tonic-gate if (com == 0) { 7040Sstevel@tonic-gate warn(gettext("General commands: help set repository end\n" 7050Sstevel@tonic-gate "Manifest commands: inventory validate import export " 7060Sstevel@tonic-gate "archive\n" 7070Sstevel@tonic-gate "Profile commands: apply extract\n" 7080Sstevel@tonic-gate "Entity commands: list select unselect add delete\n" 7090Sstevel@tonic-gate "Snapshot commands: listsnap selectsnap revert\n" 7100Sstevel@tonic-gate "Property group commands: listpg addpg delpg\n" 7110Sstevel@tonic-gate "Property commands: listprop setprop delprop editprop\n" 7120Sstevel@tonic-gate "Property value commands: addpropvalue delpropvalue " 7130Sstevel@tonic-gate "setenv unsetenv\n")); 7140Sstevel@tonic-gate return; 7150Sstevel@tonic-gate } 7160Sstevel@tonic-gate 7170Sstevel@tonic-gate for (i = 0; help_messages[i].message != NULL; ++i) { 7180Sstevel@tonic-gate if (help_messages[i].token == com) { 7190Sstevel@tonic-gate warn(gettext("Usage: %s\n"), 7200Sstevel@tonic-gate gettext(help_messages[i].message)); 7210Sstevel@tonic-gate return; 7220Sstevel@tonic-gate } 7230Sstevel@tonic-gate } 7240Sstevel@tonic-gate 7250Sstevel@tonic-gate warn(gettext("Unknown command.\n")); 7260Sstevel@tonic-gate } 727