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