xref: /onnv-gate/usr/src/cmd/cmd-inet/usr.lib/inetd/env.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate #include <stdlib.h>
30*0Sstevel@tonic-gate #include <strings.h>
31*0Sstevel@tonic-gate #include <libintl.h>
32*0Sstevel@tonic-gate 
33*0Sstevel@tonic-gate #include "inetd_impl.h"
34*0Sstevel@tonic-gate 
35*0Sstevel@tonic-gate extern char **environ;
36*0Sstevel@tonic-gate 
37*0Sstevel@tonic-gate static int
valid_env_var(const char * var,const char * instance,const char * method)38*0Sstevel@tonic-gate valid_env_var(const char *var, const char *instance, const char *method)
39*0Sstevel@tonic-gate {
40*0Sstevel@tonic-gate 	char *cp = strchr(var, '=');
41*0Sstevel@tonic-gate 
42*0Sstevel@tonic-gate 	if (cp == NULL || cp == var) {
43*0Sstevel@tonic-gate 		if (method == NULL)
44*0Sstevel@tonic-gate 			return (0);
45*0Sstevel@tonic-gate 		error_msg(gettext("Invalid environment variable \"%s\" for "
46*0Sstevel@tonic-gate 		    "method %s of instance %s.\n"), var, method, instance);
47*0Sstevel@tonic-gate 		return (0);
48*0Sstevel@tonic-gate 	} else if (strncmp(var, "SMF_", 4) == 0) {
49*0Sstevel@tonic-gate 		if (method == NULL)
50*0Sstevel@tonic-gate 			return (0);
51*0Sstevel@tonic-gate 		error_msg(gettext("Invalid environment variable \"%s\" for "
52*0Sstevel@tonic-gate 		    "method %s of instance %s; \"SMF_\" prefix is reserved.\n"),
53*0Sstevel@tonic-gate 		    var, method, instance);
54*0Sstevel@tonic-gate 		return (0);
55*0Sstevel@tonic-gate 	}
56*0Sstevel@tonic-gate 
57*0Sstevel@tonic-gate 	return (1);
58*0Sstevel@tonic-gate }
59*0Sstevel@tonic-gate 
60*0Sstevel@tonic-gate static char **
find_dup(const char * var,char ** env,const char * instance,const char * method)61*0Sstevel@tonic-gate find_dup(const char *var, char **env, const char *instance, const char *method)
62*0Sstevel@tonic-gate {
63*0Sstevel@tonic-gate 	char **p;
64*0Sstevel@tonic-gate 	char *tmp;
65*0Sstevel@tonic-gate 
66*0Sstevel@tonic-gate 	for (p = env; *p != NULL; p++) {
67*0Sstevel@tonic-gate 		tmp = strchr(*p, '=');
68*0Sstevel@tonic-gate 		assert(tmp != NULL);
69*0Sstevel@tonic-gate 		tmp++;
70*0Sstevel@tonic-gate 		if (strncmp(*p, var, tmp - *p) == 0)
71*0Sstevel@tonic-gate 			break;
72*0Sstevel@tonic-gate 	}
73*0Sstevel@tonic-gate 
74*0Sstevel@tonic-gate 	if (*p == NULL)
75*0Sstevel@tonic-gate 		return (NULL);
76*0Sstevel@tonic-gate 
77*0Sstevel@tonic-gate 	error_msg(gettext("Ignoring duplicate environment variable \"%s\" "
78*0Sstevel@tonic-gate 	    "for method %s of instance %s.\n"), *p, method, instance);
79*0Sstevel@tonic-gate 	return (p);
80*0Sstevel@tonic-gate }
81*0Sstevel@tonic-gate 
82*0Sstevel@tonic-gate /*
83*0Sstevel@tonic-gate  * Create an environment which is appropriate for spawning an SMF aware
84*0Sstevel@tonic-gate  * process.
85*0Sstevel@tonic-gate  *
86*0Sstevel@tonic-gate  * In order to preserve the correctness of the new environment, various
87*0Sstevel@tonic-gate  * checks are performed:
88*0Sstevel@tonic-gate  *
89*0Sstevel@tonic-gate  * - All SMF_ entries are ignored.  All SMF_ entries should be provided
90*0Sstevel@tonic-gate  *   by this function.
91*0Sstevel@tonic-gate  * - Duplicates in the entry are eliminated.
92*0Sstevel@tonic-gate  * - Malformed entries are eliminated.
93*0Sstevel@tonic-gate  *
94*0Sstevel@tonic-gate  * Detected errors are logged but not fatal, since a single bad entry
95*0Sstevel@tonic-gate  * should not be enough to prevent an SMF_ functional environment from
96*0Sstevel@tonic-gate  * being created.
97*0Sstevel@tonic-gate  */
98*0Sstevel@tonic-gate char **
set_smf_env(struct method_context * mthd_ctxt,instance_t * instance,const char * method)99*0Sstevel@tonic-gate set_smf_env(struct method_context *mthd_ctxt, instance_t *instance,
100*0Sstevel@tonic-gate     const char *method)
101*0Sstevel@tonic-gate {
102*0Sstevel@tonic-gate 	char **nenv;
103*0Sstevel@tonic-gate 	char **p, **np;
104*0Sstevel@tonic-gate 	size_t nenv_size;
105*0Sstevel@tonic-gate 
106*0Sstevel@tonic-gate 	/*
107*0Sstevel@tonic-gate 	 * Max. of env, three SMF_ variables, and terminating NULL.
108*0Sstevel@tonic-gate 	 */
109*0Sstevel@tonic-gate 	nenv_size = mthd_ctxt->env_sz + 3 + 1;
110*0Sstevel@tonic-gate 
111*0Sstevel@tonic-gate 	if (instance->config->basic->inherit_env) {
112*0Sstevel@tonic-gate 		for (p = environ; *p != NULL; p++)
113*0Sstevel@tonic-gate 			nenv_size++;
114*0Sstevel@tonic-gate 	}
115*0Sstevel@tonic-gate 
116*0Sstevel@tonic-gate 	nenv = malloc(sizeof (char *) * nenv_size);
117*0Sstevel@tonic-gate 	if (nenv == NULL)
118*0Sstevel@tonic-gate 		return (NULL);
119*0Sstevel@tonic-gate 	(void) memset(nenv, 0, sizeof (char *) * nenv_size);
120*0Sstevel@tonic-gate 
121*0Sstevel@tonic-gate 	np = nenv;
122*0Sstevel@tonic-gate 
123*0Sstevel@tonic-gate 	*np = uu_msprintf("SMF_RESTARTER=%s", INETD_INSTANCE_FMRI);
124*0Sstevel@tonic-gate 	if (*np == NULL)
125*0Sstevel@tonic-gate 		goto fail;
126*0Sstevel@tonic-gate 	else
127*0Sstevel@tonic-gate 		np++;
128*0Sstevel@tonic-gate 	*np = uu_msprintf("SMF_FMRI=%s", instance->fmri);
129*0Sstevel@tonic-gate 	if (*np == NULL)
130*0Sstevel@tonic-gate 		goto fail;
131*0Sstevel@tonic-gate 	else
132*0Sstevel@tonic-gate 		np++;
133*0Sstevel@tonic-gate 	*np = uu_msprintf("SMF_METHOD=%s", method);
134*0Sstevel@tonic-gate 	if (*np == NULL)
135*0Sstevel@tonic-gate 		goto fail;
136*0Sstevel@tonic-gate 	else
137*0Sstevel@tonic-gate 		np++;
138*0Sstevel@tonic-gate 
139*0Sstevel@tonic-gate 	if (instance->config->basic->inherit_env) {
140*0Sstevel@tonic-gate 		for (p = environ; *p != NULL; p++) {
141*0Sstevel@tonic-gate 			if (!valid_env_var(*p, NULL, NULL))
142*0Sstevel@tonic-gate 				continue;
143*0Sstevel@tonic-gate 
144*0Sstevel@tonic-gate 			*np = strdup(*p);
145*0Sstevel@tonic-gate 			if (*np == NULL)
146*0Sstevel@tonic-gate 				goto fail;
147*0Sstevel@tonic-gate 			else
148*0Sstevel@tonic-gate 				np++;
149*0Sstevel@tonic-gate 		}
150*0Sstevel@tonic-gate 	}
151*0Sstevel@tonic-gate 
152*0Sstevel@tonic-gate 	if (mthd_ctxt->env != NULL) {
153*0Sstevel@tonic-gate 		for (p = mthd_ctxt->env; *p != NULL; p++) {
154*0Sstevel@tonic-gate 			char **dup_pos;
155*0Sstevel@tonic-gate 
156*0Sstevel@tonic-gate 			if (!valid_env_var(*p, instance->fmri, method))
157*0Sstevel@tonic-gate 				continue;
158*0Sstevel@tonic-gate 
159*0Sstevel@tonic-gate 			if ((dup_pos = find_dup(*p, nenv, instance->fmri,
160*0Sstevel@tonic-gate 			    method)) != NULL) {
161*0Sstevel@tonic-gate 				free(*dup_pos);
162*0Sstevel@tonic-gate 				*dup_pos = strdup(*p);
163*0Sstevel@tonic-gate 				if (*dup_pos == NULL)
164*0Sstevel@tonic-gate 					goto fail;
165*0Sstevel@tonic-gate 			} else {
166*0Sstevel@tonic-gate 				*np = strdup(*p);
167*0Sstevel@tonic-gate 				if (*np == NULL)
168*0Sstevel@tonic-gate 					goto fail;
169*0Sstevel@tonic-gate 				else
170*0Sstevel@tonic-gate 					np++;
171*0Sstevel@tonic-gate 			}
172*0Sstevel@tonic-gate 		}
173*0Sstevel@tonic-gate 	}
174*0Sstevel@tonic-gate 	*np = NULL;
175*0Sstevel@tonic-gate 
176*0Sstevel@tonic-gate 	return (nenv);
177*0Sstevel@tonic-gate fail:
178*0Sstevel@tonic-gate 	p = nenv;
179*0Sstevel@tonic-gate 	while (nenv_size--)
180*0Sstevel@tonic-gate 		free(*p++);
181*0Sstevel@tonic-gate 	free(nenv);
182*0Sstevel@tonic-gate 	return (NULL);
183*0Sstevel@tonic-gate }
184