xref: /titanic_50/usr/src/cmd/cmd-inet/usr.lib/inetd/config.c (revision 67318e4a54c292d543e6b077199ce492b3d3a049)
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 (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 /*
29  * Routines used by inetd to read inetd's configuration from the repository,
30  * to validate it and setup inetd's data structures appropriately based on
31  * in.
32  */
33 
34 #include <stdlib.h>
35 #include <string.h>
36 #include <errno.h>
37 #include <unistd.h>
38 #include <netdb.h>
39 #include <netinet/in.h>
40 #include <libintl.h>
41 #include <nss_dbdefs.h>
42 #include <signal.h>
43 #include <wait.h>
44 #include "inetd_impl.h"
45 
46 
47 /* method timeout used if one isn't explicitly specified */
48 #define	DEFAULT_METHOD_TIMEOUT	10
49 
50 
51 /* supported method properties and their attributes */
52 static inetd_prop_t method_props[] = {
53 {PR_EXEC_NAME, "", INET_TYPE_STRING, B_FALSE, IVE_UNSET, NULL, B_FALSE},
54 {PR_ARG0_NAME, "", INET_TYPE_STRING, B_TRUE, IVE_UNSET, NULL, B_FALSE},
55 {SCF_PROPERTY_TIMEOUT, "", INET_TYPE_COUNT, B_TRUE, IVE_UNSET, NULL, B_FALSE},
56 {NULL},
57 };
58 
59 /* enumeration of method properties; used to index into method_props[] */
60 typedef enum {
61 	MP_EXEC,
62 	MP_ARG0,
63 	MP_TIMEOUT
64 } method_prop_t;
65 
66 
67 /* handle used for repository access in read_prop() */
68 static scf_handle_t	*rep_handle = NULL;
69 
70 /* pool used to create proto_info_t lists (generic proto info structure) */
71 static uu_list_pool_t	*proto_info_pool = NULL;
72 
73 static void destroy_method_props(inetd_prop_t *);
74 static int proto_info_compare(const void *, const void *, void *);
75 
76 int
77 config_init(void)
78 {
79 	if ((rep_handle = scf_handle_create(SCF_VERSION)) == NULL) {
80 		error_msg("%s: %s",
81 		    gettext("Failed to create repository handle"),
82 		    scf_strerror(scf_error()));
83 		return (-1);
84 	} else if (make_handle_bound(rep_handle) == -1) {
85 		/* let config_fini clean-up */
86 		return (-1);
87 	}
88 
89 	if ((proto_info_pool = uu_list_pool_create("proto_info_pool",
90 	    sizeof (proto_info_t), offsetof(proto_info_t, link),
91 	    proto_info_compare, UU_LIST_POOL_DEBUG)) == NULL) {
92 		error_msg(gettext("Failed to create uu list pool: %s"),
93 		    uu_strerror(uu_error()));
94 		return (-1);
95 	}
96 
97 	return (0);
98 }
99 
100 void
101 config_fini(void)
102 {
103 	if (rep_handle == NULL)
104 		return;
105 
106 	if (proto_info_pool != NULL) {
107 		uu_list_pool_destroy(proto_info_pool);
108 		proto_info_pool = NULL;
109 	}
110 
111 	(void) scf_handle_unbind(rep_handle);
112 	scf_handle_destroy(rep_handle);
113 	rep_handle = NULL;
114 }
115 
116 static void
117 destroy_method_info(method_info_t *mi)
118 {
119 	if (mi == NULL)
120 		return;
121 
122 	if (mi->wordexp_arg0_backup != NULL) {
123 		/*
124 		 * Return the wordexp structure back to its original
125 		 * state so it can be consumed by wordfree.
126 		 */
127 		free(mi->exec_args_we.we_wordv[0]);
128 		mi->exec_args_we.we_wordv[0] =
129 		    (char *)mi->wordexp_arg0_backup;
130 	}
131 
132 	free(mi->exec_path);
133 
134 	wordfree(&mi->exec_args_we);
135 
136 	free(mi);
137 }
138 
139 /*
140  * Transforms the properties read from the repository for a method into a
141  * method_info_t and returns a pointer to it. If expansion of the exec
142  * property fails, due to an invalid string or memory allocation failure,
143  * NULL is returned and exec_invalid is set appropriately to indicate whether
144  * it was a memory allocation failure or an invalid exec string.
145  */
146 static method_info_t *
147 create_method_info(const inetd_prop_t *mprops, boolean_t *exec_invalid)
148 {
149 	method_info_t	*ret;
150 	int		i;
151 
152 	if ((ret = calloc(1, sizeof (method_info_t))) == NULL)
153 		goto alloc_fail;
154 
155 	/* Expand the exec string. */
156 	if ((i = wordexp(get_prop_value_string(mprops, PR_EXEC_NAME),
157 	    &ret->exec_args_we, WRDE_NOCMD|WRDE_UNDEF)) != 0) {
158 		if (i == WRDE_NOSPACE)
159 			goto alloc_fail;
160 
161 		*exec_invalid = B_TRUE;
162 		free(ret);
163 		return (NULL);
164 	}
165 
166 	if ((ret->exec_path = strdup(ret->exec_args_we.we_wordv[0])) == NULL)
167 		goto alloc_fail;
168 
169 	if (mprops[MP_ARG0].ip_error == IVE_VALID) {	/* arg0 is set */
170 		/*
171 		 * Keep a copy of arg0 of the wordexp structure so that
172 		 * wordfree() gets passed what wordexp() originally returned,
173 		 * as documented as required in the man page.
174 		 */
175 		ret->wordexp_arg0_backup = ret->exec_args_we.we_wordv[0];
176 		if ((ret->exec_args_we.we_wordv[0] =
177 		    strdup(get_prop_value_string(mprops, PR_ARG0_NAME)))
178 		    == NULL)
179 			goto alloc_fail;
180 	}
181 
182 	if (mprops[MP_TIMEOUT].ip_error == IVE_VALID) {
183 		ret->timeout = get_prop_value_count(mprops,
184 		    SCF_PROPERTY_TIMEOUT);
185 	} else {
186 		ret->timeout = DEFAULT_METHOD_TIMEOUT;
187 	}
188 
189 	/* exec_invalid not set on success */
190 
191 	return (ret);
192 
193 alloc_fail:
194 	error_msg(strerror(errno));
195 	destroy_method_info(ret);
196 	*exec_invalid = B_FALSE;
197 	return (NULL);
198 }
199 
200 /*
201  * Returns B_TRUE if the contents of the 2 method_info_t structures are
202  * equivalent, else B_FALSE.
203  */
204 boolean_t
205 method_info_equal(const method_info_t *mi, const method_info_t *mi2)
206 {
207 	int		i;
208 
209 	if ((mi == NULL) && (mi2 == NULL)) {
210 		return (B_TRUE);
211 	} else if (((mi == NULL) || (mi2 == NULL)) ||
212 	    (mi->exec_args_we.we_wordc != mi2->exec_args_we.we_wordc) ||
213 	    (strcmp(mi->exec_path, mi2->exec_path) != 0)) {
214 		return (B_FALSE);
215 	}
216 
217 	for (i = 0; i < mi->exec_args_we.we_wordc; i++) {
218 		if (strcmp(mi->exec_args_we.we_wordv[i],
219 		    mi2->exec_args_we.we_wordv[i]) != 0) {
220 			return (B_FALSE);
221 		}
222 	}
223 
224 	return (B_TRUE);
225 }
226 
227 /*
228  * Checks if the contents of the 2 socket_info_t structures are equivalent.
229  * If 'isrpc' is false, the address components of the two structures are
230  * compared for equality as part of this. If the two structures are
231  * equivalent B_TRUE is returned, else B_FALSE.
232  */
233 boolean_t
234 socket_info_equal(const socket_info_t *si, const socket_info_t *si2,
235     boolean_t isrpc)
236 {
237 	return ((isrpc || (memcmp(&si->local_addr, &si2->local_addr,
238 	    sizeof (si->local_addr)) == 0)) &&
239 	    (si->type == si2->type));
240 
241 }
242 
243 /*
244  * proto_info_t comparison function. Returns 0 on match, else -1, as required
245  * by uu_list_find().
246  */
247 static int
248 proto_info_compare(const void *lv, const void *rv, void *istlx)
249 {
250 	proto_info_t	*pi = (proto_info_t *)lv;
251 	proto_info_t	*pi2 = (proto_info_t *)rv;
252 
253 	/* check their RPC configuration matches */
254 	if (pi->ri != NULL) {
255 		if ((pi2->ri == NULL) || !rpc_info_equal(pi->ri, pi2->ri))
256 			return (-1);
257 	} else if (pi2->ri != NULL) {
258 		return (-1);
259 	}
260 
261 	if (pi->v6only != pi2->v6only)
262 		return (-1);
263 
264 	if (*(boolean_t *)istlx) {
265 		if (tlx_info_equal((tlx_info_t *)lv, (tlx_info_t *)rv,
266 		    pi->ri != NULL))
267 			return (0);
268 	} else {
269 		if (socket_info_equal((socket_info_t *)lv,
270 		    (socket_info_t *)rv, pi->ri != NULL))
271 			return (0);
272 	}
273 	return (-1);
274 }
275 
276 /*
277  * Returns B_TRUE if the bind configuration of the two instance_cfg_t
278  * structures are equivalent, else B_FALSE.
279  */
280 boolean_t
281 bind_config_equal(const basic_cfg_t *c1, const basic_cfg_t *c2)
282 {
283 	proto_info_t	*pi;
284 
285 	if ((c1->iswait != c2->iswait) ||
286 	    (c1->istlx != c2->istlx))
287 		return (B_FALSE);
288 
289 	if (uu_list_numnodes(c1->proto_list) !=
290 	    uu_list_numnodes(c2->proto_list))
291 		return (B_FALSE);
292 	/*
293 	 * For each element in the first configuration's socket/tlx list,
294 	 * check there's a matching one in the other list.
295 	 */
296 	for (pi = uu_list_first(c1->proto_list); pi != NULL;
297 	    pi = uu_list_next(c1->proto_list, pi)) {
298 		uu_list_index_t idx;
299 
300 		if (uu_list_find(c2->proto_list, pi, (void *)&c1->istlx,
301 		    &idx) == NULL)
302 			return (B_FALSE);
303 	}
304 
305 	return (B_TRUE);
306 }
307 
308 /*
309  * Write the default values contained in 'bprops', read by
310  * read_instance_props(), into 'cfg'.
311  * Returns -1 if memory allocation fails, else 0.
312  */
313 static int
314 populate_defaults(inetd_prop_t *bprops, basic_cfg_t *cfg)
315 {
316 	cfg->do_tcp_wrappers = get_prop_value_boolean(bprops,
317 	    PR_DO_TCP_WRAPPERS_NAME);
318 	cfg->do_tcp_trace = get_prop_value_boolean(bprops,
319 	    PR_DO_TCP_TRACE_NAME);
320 	cfg->inherit_env = get_prop_value_boolean(bprops, PR_INHERIT_ENV_NAME);
321 	cfg->wait_fail_cnt = get_prop_value_int(bprops,
322 	    PR_MAX_FAIL_RATE_CNT_NAME);
323 	cfg->wait_fail_interval =  get_prop_value_int(bprops,
324 	    PR_MAX_FAIL_RATE_INTVL_NAME);
325 	cfg->max_copies = get_prop_value_int(bprops, PR_MAX_COPIES_NAME);
326 	cfg->conn_rate_offline = get_prop_value_int(bprops,
327 	    PR_CON_RATE_OFFLINE_NAME);
328 	cfg->conn_rate_max = get_prop_value_int(bprops, PR_CON_RATE_MAX_NAME);
329 	cfg->bind_fail_interval = get_prop_value_int(bprops,
330 	    PR_BIND_FAIL_INTVL_NAME);
331 	cfg->bind_fail_max = get_prop_value_int(bprops, PR_BIND_FAIL_MAX_NAME);
332 	cfg->conn_backlog = get_prop_value_int(bprops,
333 	    PR_CONNECTION_BACKLOG_NAME);
334 	if ((cfg->bind_addr =
335 	    strdup(get_prop_value_string(bprops, PR_BIND_ADDR_NAME))) == NULL) {
336 		error_msg(strerror(errno));
337 		return (-1);
338 	}
339 	return (0);
340 }
341 
342 void
343 destroy_method_infos(method_info_t **mis)
344 {
345 	int i;
346 
347 	for (i = 0; i < NUM_METHODS; i++) {
348 		destroy_method_info(mis[i]);
349 		mis[i] = NULL;
350 	}
351 }
352 
353 /*
354  * For each method, if it was specifed convert its entry in 'mprops',
355  * into an entry in 'mis'. Returns -1 if memory allocation fails or one of the
356  * exec strings was invalid, else 0.
357  */
358 static int
359 create_method_infos(const char *fmri, inetd_prop_t **mprops,
360     method_info_t **mis)
361 {
362 	int i;
363 
364 	for (i = 0; i < NUM_METHODS; i++) {
365 		/*
366 		 * Only create a method info structure if the method properties
367 		 * contain an exec string, which we take to mean the method
368 		 * is specified.
369 		 */
370 		if (mprops[i][MP_EXEC].ip_error == IVE_VALID) {
371 			boolean_t exec_invalid;
372 
373 			if ((mis[i] = create_method_info(mprops[i],
374 			    &exec_invalid)) == NULL) {
375 				if (exec_invalid) {
376 					error_msg(gettext("Property %s for "
377 					    "method %s of instance %s is "
378 					    "invalid"), PR_EXEC_NAME,
379 					    methods[i].name, fmri);
380 				}
381 				return (-1);
382 			}
383 		}
384 	}
385 	return (0);
386 }
387 
388 /*
389  * Try and read each of the method properties for the method 'method' of
390  * instance 'inst', and return a table containing all method properties. If an
391  * error occurs, NULL is returned, with 'err' set to indicate the cause.
392  * Otherwise, a pointer to an inetd_prop_t table is returned containing all
393  * the method properties, and each of the properties is flagged according to
394  * whether it was present or not, and if it was present its value is set in
395  * the property's entry in the table.
396  */
397 static inetd_prop_t *
398 read_method_props(const char *inst, instance_method_t method, scf_error_t *err)
399 {
400 	inetd_prop_t	*ret;
401 	int		i;
402 
403 	if ((ret = calloc(1, sizeof (method_props))) == NULL) {
404 		*err = SCF_ERROR_NO_MEMORY;
405 		return (NULL);
406 	}
407 
408 	(void) memcpy(ret, method_props, sizeof (method_props));
409 	for (i = 0; ret[i].ip_name != NULL; i++) {
410 		*err = read_prop(rep_handle, &ret[i], i, inst,
411 		    methods[method].name);
412 		if ((*err != 0) && (*err != SCF_ERROR_NOT_FOUND)) {
413 			destroy_method_props(ret);
414 			return (NULL);
415 		}
416 	}
417 
418 	return (ret);
419 }
420 
421 static void
422 destroy_method_props(inetd_prop_t *mprop)
423 {
424 	int i;
425 
426 	if (mprop == NULL)
427 		return;
428 
429 	for (i = 0; mprop[i].ip_name != NULL; i++) {
430 		if (mprop[i].ip_type == INET_TYPE_STRING &&
431 		    mprop[i].ip_error == IVE_VALID)
432 			free(mprop[i].ip_value.iv_string);
433 	}
434 
435 	free(mprop);
436 }
437 
438 /*
439  * Destroy the basic and method properties returned by read_inst_props().
440  */
441 static void
442 destroy_inst_props(inetd_prop_t *bprops, inetd_prop_t **mprops)
443 {
444 	int	i;
445 
446 	free_instance_props(bprops);
447 	for (i = 0; i < NUM_METHODS; i++)
448 		destroy_method_props(mprops[i]);
449 }
450 
451 /*
452  * Read all the basic and method properties for instance 'inst', as inetd_prop_t
453  * tables, into the spaces referenced by 'bprops' and 'mprops' respectively.
454  * Each of the properties in the tables are flagged to indicate if the
455  * property was present or not, and if it was the value is stored within it.
456  * If an error occurs at any time -1 is returned and 'err' is set to
457  * indicate the reason, else 0 is returned.
458  */
459 static int
460 read_inst_props(const char *fmri, inetd_prop_t **bprops,
461     inetd_prop_t **mprops, scf_error_t *err)
462 {
463 	size_t		nprops;
464 	int		i;
465 
466 	if ((*bprops = read_instance_props(rep_handle, (char *)fmri, &nprops,
467 	    err)) == NULL)
468 		return (-1);
469 
470 	for (i = 0; i < NUM_METHODS; i++) {
471 		if ((mprops[i] =
472 		    read_method_props(fmri, (instance_method_t)i, err)) ==
473 		    NULL) {
474 			for (i--; i >= 0; i--)
475 				destroy_method_props(mprops[i]);
476 			free_instance_props(*bprops);
477 			return (-1);
478 		}
479 	}
480 
481 	return (0);
482 }
483 
484 /*
485  * Returns B_TRUE if all required properties were read from the repository
486  * (whether taken from the defaults or directly from the instance), they
487  * all had valid values, all the required methods were present, and they
488  * each had the required properties with valid values. Else, returns B_FALSE.
489  * If the function returns B_TRUE, the storage referenced by 'cfg' is set
490  * to point at an allocated instance_cfg_t initialized based on the basic
491  * properties (not method or defaults).
492  */
493 static boolean_t
494 valid_inst_props(const char *fmri, inetd_prop_t *bprops, inetd_prop_t **mprops,
495     basic_cfg_t **cfg)
496 {
497 	boolean_t	valid;
498 	size_t		num_bprops;
499 	int		i;
500 
501 	valid = valid_props(bprops, fmri, cfg, proto_info_pool, conn_ind_pool);
502 
503 	/*
504 	 * Double check we've got all necessary properties (valid_props()
505 	 * doesn't enforce the presence of defaults), and output error messages
506 	 * for each invalid/ missing property.
507 	 */
508 	(void) get_prop_table(&num_bprops);
509 	for (i = 0; bprops[i].ip_name != NULL; i++) {
510 		switch (bprops[i].ip_error) {
511 		case IVE_UNSET:
512 			if (!bprops[i].ip_default)
513 				continue;
514 			if ((i == PT_ARG0_INDEX) || (i == PT_EXEC_INDEX))
515 				continue;
516 			/* FALLTHROUGH */
517 		case IVE_INVALID:
518 			error_msg(gettext("Property '%s' of instance "
519 			    "%s is missing, inconsistent or invalid"),
520 			    bprops[i].ip_name, fmri);
521 			valid = B_FALSE;
522 		}
523 	}
524 
525 	for (i = 0; i < NUM_METHODS; i++) {
526 		int	j;
527 
528 		/* check if any properties are set */
529 		for (j = 0; mprops[i][j].ip_name != NULL; j++) {
530 			if (mprops[i][j].ip_error != IVE_UNSET)
531 				break;
532 		}
533 
534 		if (mprops[i][j].ip_name == NULL) {
535 			/* an unspecified method */
536 			if ((instance_method_t)i == IM_START) {
537 				error_msg(gettext(
538 				    "Unspecified %s method for instance %s"),
539 				    START_METHOD_NAME, fmri);
540 				valid = B_FALSE;
541 			}
542 		} else if (mprops[i][MP_EXEC].ip_error == IVE_UNSET) {
543 			error_msg(gettext("Missing %s property from method %s "
544 			    "of instance %s"), PR_EXEC_NAME,
545 			    methods[(instance_method_t)i].name, fmri);
546 			valid = B_FALSE;
547 		}
548 	}
549 
550 	if (!valid)
551 		destroy_basic_cfg(*cfg);
552 
553 	return (valid);
554 }
555 
556 void
557 destroy_instance_cfg(instance_cfg_t *cfg)
558 {
559 	if (cfg != NULL) {
560 		destroy_basic_cfg(cfg->basic);
561 		destroy_method_infos(cfg->methods);
562 		free(cfg);
563 	}
564 }
565 
566 /*
567  * Returns an allocated instance_cfg_t representation of an instance's
568  * configuration read from the repository. If the configuration is invalid, a
569  * repository error occurred, or a memory allocation occurred returns NULL,
570  * else returns a pointer to the allocated instance_cfg_t.
571  */
572 instance_cfg_t *
573 read_instance_cfg(const char *fmri)
574 {
575 	uint_t		retries;
576 	inetd_prop_t	*bprops;
577 	inetd_prop_t	*mprops[NUM_METHODS];
578 	instance_cfg_t	*ret = NULL;
579 	scf_error_t	err;
580 
581 	if ((ret = calloc(1, sizeof (instance_cfg_t))) == NULL)
582 		return (NULL);
583 
584 	for (retries = 0; retries <= REP_OP_RETRIES; retries++) {
585 		if (make_handle_bound(rep_handle) == -1) {
586 			err = scf_error();
587 			goto read_error;
588 		}
589 
590 		if (read_inst_props(fmri, &bprops, mprops, &err) == 0)
591 			break;
592 		if (err != SCF_ERROR_CONNECTION_BROKEN)
593 			goto read_error;
594 		(void) scf_handle_unbind(rep_handle);
595 	}
596 	if (retries > REP_OP_RETRIES)
597 		goto read_error;
598 
599 	/*
600 	 * Switch off validation of the start method's exec string, since
601 	 * during boot the filesystem it resides on may not have been
602 	 * mounted yet, which would result in a false validation failure.
603 	 * We'll catch any real errors when the start method is first run
604 	 * in passes_basic_exec_checks().
605 	 */
606 	bprops[PT_EXEC_INDEX].ip_error = IVE_UNSET;
607 
608 	if ((!valid_inst_props(fmri, bprops, mprops, &ret->basic)) ||
609 	    (populate_defaults(bprops, ret->basic) != 0) ||
610 	    (create_method_infos(fmri, mprops, ret->methods) != 0)) {
611 		destroy_instance_cfg(ret);
612 		ret = NULL;
613 	}
614 
615 	destroy_inst_props(bprops, mprops);
616 	return (ret);
617 
618 read_error:
619 	error_msg(gettext(
620 	    "Failed to read the configuration of instance %s: %s"), fmri,
621 	    scf_strerror(err));
622 	free(ret);
623 	return (NULL);
624 }
625 
626 /*
627  * Returns a pointer to an allocated method context for the specified method
628  * of the specified instance if it could retrieve it. Else, if there were
629  * errors retrieving it, NULL is returned and the pointer referenced by
630  * 'errstr' is set to point at an appropriate error string.
631  */
632 struct method_context *
633 read_method_context(const char *inst_fmri, const char *method, const char *path,
634     const char **errstr)
635 {
636 	scf_instance_t			*scf_inst = NULL;
637 	struct method_context		*ret;
638 	uint_t				retries;
639 	const char			*tmpstr;
640 
641 	for (retries = 0; retries <= REP_OP_RETRIES; retries++) {
642 		if (make_handle_bound(rep_handle) == -1)
643 			goto inst_failure;
644 
645 		if (((scf_inst = scf_instance_create(rep_handle)) != NULL) &&
646 		    (scf_handle_decode_fmri(rep_handle, inst_fmri, NULL, NULL,
647 		    scf_inst, NULL, NULL, SCF_DECODE_FMRI_EXACT) == 0))
648 			break;
649 		if (scf_error() != SCF_ERROR_CONNECTION_BROKEN) {
650 			scf_instance_destroy(scf_inst);
651 			goto inst_failure;
652 		}
653 
654 		(void) scf_instance_destroy(scf_inst);
655 		scf_inst = NULL;
656 
657 		(void) scf_handle_unbind(rep_handle);
658 	}
659 	if (retries > REP_OP_RETRIES)
660 		goto inst_failure;
661 
662 	if ((tmpstr = restarter_get_method_context(
663 	    RESTARTER_METHOD_CONTEXT_VERSION, scf_inst, NULL, method, path,
664 	    &ret)) != NULL) {
665 		ret = NULL;
666 		*errstr = tmpstr;
667 	}
668 
669 	scf_instance_destroy(scf_inst);
670 	return (ret);
671 
672 inst_failure:
673 	/*
674 	 * We can rely on this string not becoming invalid
675 	 * since we don't call bind_textdomain_codeset() or
676 	 * setlocale(3C) after initialization.
677 	 */
678 	*errstr = gettext("failed to get instance from repository");
679 	return (NULL);
680 }
681 
682 /*
683  * Reads the value of the enabled property from the named property group
684  * of the given instance.
685  * If an error occurs, the SCF error code is returned. The possible errors are:
686  * - SCF_ERROR_INVALID_ARGUMENT: The enabled property is not a boolean.
687  * - SCF_ERROR_NONE: No value exists for the enabled property.
688  * - SCF_ERROR_CONNECTION_BROKEN: Repository connection broken.
689  * - SCF_ERROR_NOT_FOUND: The property wasn't found.
690  * - SCF_ERROR_NO_MEMORY: allocation failure.
691  * Else 0 is returned and 'enabled' set appropriately.
692  */
693 static scf_error_t
694 read_enable_prop(const char *fmri, boolean_t *enabled, const char *pg)
695 {
696 	scf_simple_prop_t	*sp;
697 	uint8_t			*u8p;
698 
699 	if ((sp = scf_simple_prop_get(rep_handle, fmri, pg,
700 	    SCF_PROPERTY_ENABLED)) == NULL)
701 		return (scf_error());
702 
703 	if ((u8p = scf_simple_prop_next_boolean(sp)) == NULL) {
704 		scf_simple_prop_free(sp);
705 		return (scf_error());
706 	}
707 
708 	*enabled = (*u8p != 0);
709 	scf_simple_prop_free(sp);
710 	return (0);
711 }
712 
713 /*
714  * Reads the enabled value for the given instance FMRI. The read value
715  * is based on a merge of the 'standard' enabled property, and the temporary
716  * override one; the merge involves using the latter properties value if
717  * present, else resporting to the formers. If an error occurs -1 is returned,
718  * else 0 is returned and 'enabled' set approriately.
719  */
720 int
721 read_enable_merged(const char *fmri, boolean_t *enabled)
722 {
723 	uint_t		retries;
724 
725 	for (retries = 0; retries <= REP_OP_RETRIES; retries++) {
726 		if (make_handle_bound(rep_handle) == -1)
727 			goto gen_fail;
728 
729 		switch (read_enable_prop(fmri, enabled, SCF_PG_GENERAL_OVR)) {
730 		case 0:
731 			debug_msg("read %d from override", *enabled);
732 			return (0);
733 		case SCF_ERROR_CONNECTION_BROKEN:
734 			break;
735 		case SCF_ERROR_NOT_FOUND:
736 		case SCF_ERROR_NONE:
737 		case SCF_ERROR_INVALID_ARGUMENT:
738 			switch (read_enable_prop(fmri, enabled,
739 			    SCF_PG_GENERAL)) {
740 			case 0:
741 				debug_msg("read %d from non_override",
742 				    *enabled);
743 				return (0);
744 			case SCF_ERROR_CONNECTION_BROKEN:
745 				break;
746 			case SCF_ERROR_NOT_FOUND:
747 			case SCF_ERROR_NONE:
748 			case SCF_ERROR_INVALID_ARGUMENT:
749 				error_msg(gettext("Missing %s property/value "
750 				    "for instance %s"), SCF_PROPERTY_ENABLED,
751 				    fmri);
752 				return (-1);
753 			default:
754 				goto gen_fail;
755 			}
756 			break;
757 		default:
758 			goto gen_fail;
759 		}
760 
761 		(void) scf_handle_unbind(rep_handle);
762 		continue;
763 	}
764 
765 gen_fail:
766 	error_msg(gettext("Failed to read the %s property of instance %s: %s"),
767 	    SCF_PROPERTY_ENABLED, fmri, scf_strerror(scf_error()));
768 	return (-1);
769 }
770 
771 /*
772  * Refresh the value of debug property under the property group "config"
773  * for network/inetd service.
774  */
775 void
776 refresh_debug_flag(void)
777 {
778 	scf_simple_prop_t	*sprop;
779 	uint8_t			*tmp_bool;
780 
781 	if ((sprop = scf_simple_prop_get(rep_handle, INETD_INSTANCE_FMRI,
782 	    PG_NAME_APPLICATION_CONFIG, PR_NAME_DEBUG_FLAG)) == NULL) {
783 		error_msg(gettext("Unable to read %s property from %s property "
784 		    "group. scf_simple_prop_get() failed: %s"),
785 		    PR_NAME_DEBUG_FLAG, PG_NAME_APPLICATION_CONFIG,
786 		    scf_strerror(scf_error()));
787 		return;
788 	} else if ((tmp_bool = scf_simple_prop_next_boolean(sprop)) == NULL) {
789 		error_msg(gettext("Unable to read %s property for %s service. "
790 		    "scf_simple_prop_next_boolean() failed: %s"),
791 		    PR_NAME_DEBUG_FLAG, INETD_INSTANCE_FMRI,
792 		    scf_strerror(scf_error()));
793 	} else {
794 		debug_enabled = ((*tmp_bool == 0) ? B_FALSE : B_TRUE);
795 	}
796 
797 	scf_simple_prop_free(sprop);
798 }
799