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