summaryrefslogtreecommitdiff
path: root/usr/src/cmd/svc/svccfg/svccfg_engine.c
blob: 927026c1385bf49e2db500a0012ebb0e6818e5fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

/*
 * svccfg(1) interpreter and command execution engine.
 */

#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <assert.h>
#include <errno.h>
#include <libintl.h>
#include <libtecla.h>
#include <md5.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>

#include "manifest_hash.h"
#include "svccfg.h"

#define	MS_PER_US		1000

engine_state_t *est;

/*
 * Replacement lex(1) character retrieval routines.
 */
int
engine_cmd_getc(engine_state_t *E)
{
	if (E->sc_cmd_file != NULL)
		return (getc(E->sc_cmd_file));

	if (E->sc_cmd_flags & SC_CMD_EOF)
		return (EOF);

	if (E->sc_cmd_bufoff < E->sc_cmd_bufsz)
		return (*(E->sc_cmd_buf + E->sc_cmd_bufoff++));

	if (!(E->sc_cmd_flags & SC_CMD_IACTIVE)) {
		E->sc_cmd_flags |= SC_CMD_EOF;

		return (EOF);
	} else {
#ifdef NATIVE_BUILD
		return (EOF);
#else
		extern int parens;

		if (parens <= 0) {
			E->sc_cmd_flags |= SC_CMD_EOF;
			return (EOF);
		}

		for (;;) {
			E->sc_cmd_buf = gl_get_line(E->sc_gl, "> ", NULL, -1);
			if (E->sc_cmd_buf != NULL)
				break;

			switch (gl_return_status(E->sc_gl)) {
			case GLR_SIGNAL:
				gl_abandon_line(E->sc_gl);
				continue;

			case GLR_EOF:
				E->sc_cmd_flags |= SC_CMD_EOF;
				return (EOF);

			case GLR_ERROR:
				uu_die(gettext("Error reading terminal: %s.\n"),
				    gl_error_message(E->sc_gl, NULL, 0));
				/* NOTREACHED */

			default:
#ifndef NDEBUG
				(void) fprintf(stderr, "%s:%d: gl_get_line() "
				    "returned unexpected value %d.\n", __FILE__,
				    __LINE__, gl_return_status(E->sc_gl));
#endif
				abort();
			}
		}

		E->sc_cmd_bufsz = strlen(E->sc_cmd_buf);
		E->sc_cmd_bufoff = 1;

		return (E->sc_cmd_buf[0]);
#endif	/* NATIVE_BUILD */
	}
}

int
engine_cmd_ungetc(engine_state_t *E, char c)
{
	if (E->sc_cmd_file != NULL)
		return (ungetc(c, E->sc_cmd_file));

	if (E->sc_cmd_buf != NULL)
		*(E->sc_cmd_buf + --E->sc_cmd_bufoff) = c;

	return (c);
}

/*ARGSUSED*/
void
engine_cmd_nputs(engine_state_t *E, char *c, size_t n)
{
	/* our lexer shouldn't need this state */
	exit(11);
}

int
engine_exec(char *cmd)
{
	est->sc_cmd_buf = cmd;
	est->sc_cmd_bufsz = strlen(cmd) + 1;
	est->sc_cmd_bufoff = 0;

	(void) yyparse();

	return (0);
}

#ifndef NATIVE_BUILD
/* ARGSUSED */
static
CPL_CHECK_FN(check_xml)
{
	const char *ext;

	if (strlen(pathname) < 4)
		return (0);

	ext = pathname + strlen(pathname) - 4;

	return (strcmp(ext, ".xml") == 0 ? 1 : 0);
}

static const char * const whitespace = " \t";

static
CPL_MATCH_FN(complete_single_xml_file_arg)
{
	const char *arg1 = data;
	int arg1end_i, ret;
	CplFileConf *cfc;

	arg1end_i = arg1 + strcspn(arg1, whitespace) - line;
	if (arg1end_i < word_end)
		return (0);

	cfc = new_CplFileConf();
	if (cfc == NULL) {
		cpl_record_error(cpl, "Out of memory.");
		return (1);
	}

	cfc_set_check_fn(cfc, check_xml, NULL);

	ret = cpl_file_completions(cpl, cfc, line, word_end);

	(void) del_CplFileConf(cfc);
	return (ret);
}

static struct cmd_info {
	const char	*name;
	uint32_t	flags;
	CplMatchFn	*complete_args_f;
} cmds[] = {
	{ "validate", CS_GLOBAL, complete_single_xml_file_arg },
	{ "import", CS_GLOBAL, complete_single_xml_file_arg },
	{ "export", CS_GLOBAL, NULL },
	{ "archive", CS_GLOBAL, NULL },
	{ "apply", CS_GLOBAL, complete_single_xml_file_arg },
	{ "extract", CS_GLOBAL, NULL },
	{ "repository", CS_GLOBAL, NULL },
	{ "inventory", CS_GLOBAL, complete_single_xml_file_arg },
	{ "set", CS_GLOBAL, NULL },
	{ "end", CS_GLOBAL, NULL },
	{ "exit", CS_GLOBAL, NULL },
	{ "quit", CS_GLOBAL, NULL },
	{ "help", CS_GLOBAL, NULL },
	{ "delete", CS_GLOBAL, NULL },
	{ "select", CS_GLOBAL, complete_select },
	{ "unselect", CS_SVC | CS_INST | CS_SNAP, NULL },
	{ "list", CS_SCOPE | CS_SVC | CS_SNAP, NULL },
	{ "add", CS_SCOPE | CS_SVC, NULL },
	{ "listpg", CS_SVC | CS_INST | CS_SNAP, NULL },
	{ "addpg", CS_SVC | CS_INST, NULL },
	{ "delpg", CS_SVC | CS_INST, NULL },
	{ "listprop", CS_SVC | CS_INST | CS_SNAP, NULL },
	{ "setprop", CS_SVC | CS_INST, NULL },
	{ "delprop", CS_SVC | CS_INST, NULL },
	{ "editprop", CS_SVC | CS_INST, NULL },
	{ "listsnap", CS_INST | CS_SNAP, NULL },
	{ "selectsnap", CS_INST | CS_SNAP, NULL },
	{ "revert", CS_INST | CS_SNAP, NULL },
	{ NULL }
};

int
add_cmd_matches(WordCompletion *cpl, const char *line, int word_end,
    uint32_t scope)
{
	int word_start, err;
	size_t len;
	const char *bol;
	struct cmd_info *cip;

	word_start = strspn(line, whitespace);
	len = word_end - word_start;
	bol = line + word_end - len;

	for (cip = cmds; cip->name != NULL; ++cip) {
		if ((cip->flags & scope) == 0)
			continue;

		if (strncmp(cip->name, bol, len) == 0) {
			err = cpl_add_completion(cpl, line, word_start,
			    word_end, cip->name + len, "", " ");
			if (err != 0)
				return (err);
		}
	}

	return (0);
}

/*
 * Suggest completions.  We must first determine if the cursor is in command
 * position or in argument position.  If the former, complete_command() finds
 * matching commands.  If the latter, we tail-call the command-specific
 * argument-completion routine in the cmds table.
 */
/* ARGSUSED */
static
CPL_MATCH_FN(complete)
{
	const char *arg0, *arg1;
	size_t arg0len;
	struct cmd_info *cip;

	arg0 = line + strspn(line, whitespace);
	arg0len = strcspn(arg0, whitespace);
	if ((arg0 + arg0len) - line >= word_end ||
	    (arg0[arg0len] != ' ' && arg0[arg0len] != '\t'))
		return (complete_command(cpl, (void *)arg0, line, word_end));

	arg1 = arg0 + arg0len;
	arg1 += strspn(arg1, whitespace);

	for (cip = cmds; cip->name != NULL; ++cip) {
		if (strlen(cip->name) != arg0len)
			continue;

		if (strncmp(cip->name, arg0, arg0len) != 0)
			continue;

		if (cip->complete_args_f == NULL)
			break;

		return (cip->complete_args_f(cpl, (void *)arg1, line,
		    word_end));
	}

	return (0);
}
#endif	/* NATIVE_BUILD */

int
engine_interp()
{
#ifdef NATIVE_BUILD
	uu_die("native build does not support interactive mode.");
#else
	char *selfmri;
	size_t sfsz;
	int r;

	extern int parens;

	(void) sigset(SIGINT, SIG_IGN);

	est->sc_gl = new_GetLine(512, 8000);
	if (est->sc_gl == NULL)
		uu_die(gettext("Out of memory.\n"));

	/* The longest string is "[snapname]fmri[:instname]> ". */
	sfsz = 1 + max_scf_name_len + 1 + max_scf_fmri_len + 2 +
	    max_scf_name_len + 1 + 2 + 1;
	selfmri = safe_malloc(sfsz);

	r = gl_customize_completion(est->sc_gl, NULL, complete);
	assert(r == 0);

	for (;;) {
		lscf_get_selection_str(selfmri, sfsz - 2);
		(void) strcat(selfmri, "> ");
		est->sc_cmd_buf = gl_get_line(est->sc_gl, selfmri, NULL, -1);

		if (est->sc_cmd_buf == NULL) {
			switch (gl_return_status(est->sc_gl)) {
			case GLR_SIGNAL:
				gl_abandon_line(est->sc_gl);
				continue;

			case GLR_EOF:
				break;

			case GLR_ERROR:
				uu_die(gettext("Error reading terminal: %s.\n"),
				    gl_error_message(est->sc_gl, NULL, 0));
				/* NOTREACHED */

			default:
#ifndef NDEBUG
				(void) fprintf(stderr, "%s:%d: gl_get_line() "
				    "returned unexpected value %d.\n", __FILE__,
				    __LINE__, gl_return_status(est->sc_gl));
#endif
				abort();
			}

			break;
		}

		parens = 0;
		est->sc_cmd_bufsz = strlen(est->sc_cmd_buf);
		est->sc_cmd_bufoff = 0;
		est->sc_cmd_flags = SC_CMD_IACTIVE;

		(void) yyparse();
	}

	free(selfmri);
	est->sc_gl = del_GetLine(est->sc_gl);	/* returns NULL */

#endif	/* NATIVE_BUILD */
	return (0);
}

int
engine_source(const char *name, boolean_t dont_exit)
{
	engine_state_t *old = est;
	struct stat st;
	int ret;

	est = uu_zalloc(sizeof (engine_state_t));

	/* first, copy the stuff set up in engine_init */
	est->sc_repo_pid = old->sc_repo_pid;
	if (old->sc_repo_filename != NULL)
		est->sc_repo_filename = safe_strdup(old->sc_repo_filename);
	if (old->sc_repo_doordir != NULL)
		est->sc_repo_doordir = safe_strdup(old->sc_repo_doordir);
	if (old->sc_repo_doorname != NULL)
		est->sc_repo_doorname = safe_strdup(old->sc_repo_doorname);
	if (old->sc_repo_server != NULL)
		est->sc_repo_server = safe_strdup(old->sc_repo_server);

	/* set up the new guy */
	est->sc_cmd_lineno = 1;

	if (dont_exit)
		est->sc_cmd_flags |= SC_CMD_DONT_EXIT;

	if (strcmp(name, "-") == 0) {
		est->sc_cmd_file = stdin;
		est->sc_cmd_filename = "<stdin>";
	} else {
		errno = 0;
		est->sc_cmd_filename = name;
		est->sc_cmd_file = fopen(name, "r");
		if (est->sc_cmd_file == NULL) {
			if (errno == 0)
				semerr(gettext("No free stdio streams.\n"));
			else
				semerr(gettext("Could not open %s"), name);

			ret = -1;
			goto fail;
		}

		do
			ret = fstat(fileno(est->sc_cmd_file), &st);
		while (ret != 0 && errno == EINTR);
		if (ret != 0) {
			(void) fclose(est->sc_cmd_file);
			est->sc_cmd_file = NULL;	/* for semerr() */

			semerr(gettext("Could not stat %s"), name);

			ret = -1;
			goto fail;
		}

		if (!S_ISREG(st.st_mode)) {
			(void) fclose(est->sc_cmd_file);
			est->sc_cmd_file = NULL;	/* for semerr() */

			semerr(gettext("%s is not a regular file.\n"), name);

			ret = -1;
			goto fail;
		}
	}

	(void) yyparse();

	if (est->sc_cmd_file != stdin)
		(void) fclose(est->sc_cmd_file);

	ret = 0;

fail:
	if (est->sc_repo_pid != old->sc_repo_pid)
		lscf_cleanup();		/* clean up any new repository */

	if (est->sc_repo_filename != NULL)
		free((void *)est->sc_repo_filename);
	if (est->sc_repo_doordir != NULL)
		free((void *)est->sc_repo_doordir);
	if (est->sc_repo_doorname != NULL)
		free((void *)est->sc_repo_doorname);
	if (est->sc_repo_server != NULL)
		free((void *)est->sc_repo_server);
	free(est);

	est = old;

	return (ret);
}

/*
 * Initialize svccfg state.  We recognize four environment variables:
 *
 * SVCCFG_REPOSITORY	Create a private instance of svc.configd(1M) to answer
 *			requests for the specified repository file.
 * SVCCFG_DOOR_PATH	Directory for door creation.
 *
 * SVCCFG_DOOR		Rendezvous via an alternative repository door.
 *
 * SVCCFG_CONFIGD_PATH	Resolvable path to alternative svc.configd(1M) binary.
 */
void
engine_init()
{
	const char *cp;

	est = uu_zalloc(sizeof (engine_state_t));

	est->sc_cmd_lineno = 1;
	est->sc_repo_pid = -1;

	cp = getenv("SVCCFG_REPOSITORY");
	est->sc_repo_filename = cp ? safe_strdup(cp) : NULL;

	cp = getenv("SVCCFG_DOOR_PATH");
	est->sc_repo_doordir = cp ? cp : "/var/run";

	cp = getenv("SVCCFG_DOOR");
	if (cp != NULL) {
		if (est->sc_repo_filename != NULL) {
			uu_warn(gettext("SVCCFG_DOOR unused when "
			    "SVCCFG_REPOSITORY specified\n"));
		} else {
			est->sc_repo_doorname = safe_strdup(cp);
		}
	}

	cp = getenv("SVCCFG_CONFIGD_PATH");
	est->sc_repo_server = cp ? cp : "/lib/svc/bin/svc.configd";
}

int
engine_import(uu_list_t *args)
{
	int ret, argc, i, o;
	bundle_t *b;
	char *file, *pname;
	uchar_t hash[MHASH_SIZE];
	char **argv;
	string_list_t *slp;
	boolean_t verify = B_FALSE;
	uint_t flags = SCI_GENERALLAST;

	argc = uu_list_numnodes(args);
	if (argc < 1)
		return (-2);

	argv = calloc(argc + 1, sizeof (char *));
	if (argv == NULL)
		uu_die(gettext("Out of memory.\n"));

	for (slp = uu_list_first(args), i = 0;
	    slp != NULL;
	    slp = uu_list_next(args, slp), ++i)
		argv[i] = slp->str;

	argv[i] = NULL;

	opterr = 0;
	optind = 0;				/* Remember, no argv[0]. */
	for (;;) {
		o = getopt(argc, argv, "nV");
		if (o == -1)
			break;

		switch (o) {
		case 'n':
			flags |= SCI_NOREFRESH;
			break;

		case 'V':
			verify = B_TRUE;
			break;

		case '?':
			free(argv);
			return (-2);

		default:
			bad_error("getopt", o);
		}
	}

	argc -= optind;
	if (argc != 1) {
		free(argv);
		return (-2);
	}

	file = argv[optind];
	free(argv);

	lscf_prep_hndl();

	ret = mhash_test_file(g_hndl, file, 0, &pname, hash);
	if (ret != MHASH_NEWFILE)
		return (ret);

	/* Load */
	b = internal_bundle_new();

	if (lxml_get_bundle_file(b, file, 0) != 0) {
		internal_bundle_free(b);
		return (-1);
	}

	/* Import */
	if (lscf_bundle_import(b, file, flags) != 0) {
		internal_bundle_free(b);
		return (-1);
	}

	internal_bundle_free(b);

	if (g_verbose)
		warn(gettext("Successful import.\n"));

	if (pname) {
		char *errstr;

		if (mhash_store_entry(g_hndl, pname, hash, &errstr)) {
			if (errstr)
				semerr(errstr);
			else
				semerr(gettext("Unknown error from "
					"mhash_store_entry()\n"));
		}

		free(pname);
	}

	/* Verify */
	if (verify)
		warn(gettext("import -V not implemented.\n"));

	return (0);
}

int
engine_apply(const char *file)
{
	int ret;
	bundle_t *b;
	char *pname;
	uchar_t hash[MHASH_SIZE];

	lscf_prep_hndl();

	ret = mhash_test_file(g_hndl, file, 1, &pname, hash);
	if (ret != MHASH_NEWFILE)
		return (ret);

	b = internal_bundle_new();

	if (lxml_get_bundle_file(b, file, 1) != 0) {
		internal_bundle_free(b);
		return (-1);
	}

	if (lscf_bundle_apply(b) != 0) {
		internal_bundle_free(b);
		return (-1);
	}

	internal_bundle_free(b);

	if (pname) {
		char *errstr;
		if (mhash_store_entry(g_hndl, pname, hash, &errstr))
			semerr(errstr);

		free(pname);
	}

	return (0);
}

int
engine_set(uu_list_t *args)
{
	uu_list_walk_t *walk;
	string_list_t *slp;

	if (uu_list_first(args) == NULL) {
		/* Display current options. */
		if (!g_verbose)
			(void) fputs("no", stdout);
		(void) puts("verbose");

		return (0);
	}

	walk = uu_list_walk_start(args, UU_DEFAULT);
	if (walk == NULL)
		uu_die(gettext("Couldn't read arguments"));

	/* Use getopt? */
	for (slp = uu_list_walk_next(walk);
	    slp != NULL;
	    slp = uu_list_walk_next(walk)) {
		if (slp->str[0] == '-') {
			char *op;

			for (op = &slp->str[1]; *op != '\0'; ++op) {
				switch (*op) {
				case 'v':
					g_verbose = 1;
					break;

				case 'V':
					g_verbose = 0;
					break;

				default:
					warn(gettext("Unknown option -%c.\n"),
					    *op);
				}
			}
		} else {
			warn(gettext("No non-flag arguments defined.\n"));
		}
	}

	return (0);
}

void
help(int com)
{
	int i;

	if (com == 0) {
		warn(gettext("General commands:	 help set repository end\n"
		    "Manifest commands:	 inventory validate import export "
		    "archive\n"
		    "Profile commands:	 apply extract\n"
		    "Entity commands:	 list select unselect add delete\n"
		    "Snapshot commands:	 listsnap selectsnap revert\n"
		    "Property group commands: listpg addpg delpg\n"
		    "Property commands:	 listprop setprop delprop editprop\n"
		    "Property value commands: addpropvalue delpropvalue "
		    "setenv unsetenv\n"));
		return;
	}

	for (i = 0; help_messages[i].message != NULL; ++i) {
		if (help_messages[i].token == com) {
			warn(gettext("Usage: %s\n"),
			    gettext(help_messages[i].message));
			return;
		}
	}

	warn(gettext("Unknown command.\n"));
}