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