xref: /illumos-gate/usr/src/cmd/cmd-inet/usr.lib/inetd/env.c (revision 2a8bcb4efb45d99ac41c94a75c396b362c414f7f)
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 #include <stdlib.h>
28*7c478bd9Sstevel@tonic-gate #include <strings.h>
29*7c478bd9Sstevel@tonic-gate #include <libintl.h>
30*7c478bd9Sstevel@tonic-gate 
31*7c478bd9Sstevel@tonic-gate #include "inetd_impl.h"
32*7c478bd9Sstevel@tonic-gate 
33*7c478bd9Sstevel@tonic-gate extern char **environ;
34*7c478bd9Sstevel@tonic-gate 
35*7c478bd9Sstevel@tonic-gate static int
valid_env_var(const char * var,const char * instance,const char * method)36*7c478bd9Sstevel@tonic-gate valid_env_var(const char *var, const char *instance, const char *method)
37*7c478bd9Sstevel@tonic-gate {
38*7c478bd9Sstevel@tonic-gate 	char *cp = strchr(var, '=');
39*7c478bd9Sstevel@tonic-gate 
40*7c478bd9Sstevel@tonic-gate 	if (cp == NULL || cp == var) {
41*7c478bd9Sstevel@tonic-gate 		if (method == NULL)
42*7c478bd9Sstevel@tonic-gate 			return (0);
43*7c478bd9Sstevel@tonic-gate 		error_msg(gettext("Invalid environment variable \"%s\" for "
44*7c478bd9Sstevel@tonic-gate 		    "method %s of instance %s.\n"), var, method, instance);
45*7c478bd9Sstevel@tonic-gate 		return (0);
46*7c478bd9Sstevel@tonic-gate 	} else if (strncmp(var, "SMF_", 4) == 0) {
47*7c478bd9Sstevel@tonic-gate 		if (method == NULL)
48*7c478bd9Sstevel@tonic-gate 			return (0);
49*7c478bd9Sstevel@tonic-gate 		error_msg(gettext("Invalid environment variable \"%s\" for "
50*7c478bd9Sstevel@tonic-gate 		    "method %s of instance %s; \"SMF_\" prefix is reserved.\n"),
51*7c478bd9Sstevel@tonic-gate 		    var, method, instance);
52*7c478bd9Sstevel@tonic-gate 		return (0);
53*7c478bd9Sstevel@tonic-gate 	}
54*7c478bd9Sstevel@tonic-gate 
55*7c478bd9Sstevel@tonic-gate 	return (1);
56*7c478bd9Sstevel@tonic-gate }
57*7c478bd9Sstevel@tonic-gate 
58*7c478bd9Sstevel@tonic-gate static char **
find_dup(const char * var,char ** env,const char * instance,const char * method)59*7c478bd9Sstevel@tonic-gate find_dup(const char *var, char **env, const char *instance, const char *method)
60*7c478bd9Sstevel@tonic-gate {
61*7c478bd9Sstevel@tonic-gate 	char **p;
62*7c478bd9Sstevel@tonic-gate 	char *tmp;
63*7c478bd9Sstevel@tonic-gate 
64*7c478bd9Sstevel@tonic-gate 	for (p = env; *p != NULL; p++) {
65*7c478bd9Sstevel@tonic-gate 		tmp = strchr(*p, '=');
66*7c478bd9Sstevel@tonic-gate 		assert(tmp != NULL);
67*7c478bd9Sstevel@tonic-gate 		tmp++;
68*7c478bd9Sstevel@tonic-gate 		if (strncmp(*p, var, tmp - *p) == 0)
69*7c478bd9Sstevel@tonic-gate 			break;
70*7c478bd9Sstevel@tonic-gate 	}
71*7c478bd9Sstevel@tonic-gate 
72*7c478bd9Sstevel@tonic-gate 	if (*p == NULL)
73*7c478bd9Sstevel@tonic-gate 		return (NULL);
74*7c478bd9Sstevel@tonic-gate 
75*7c478bd9Sstevel@tonic-gate 	error_msg(gettext("Ignoring duplicate environment variable \"%s\" "
76*7c478bd9Sstevel@tonic-gate 	    "for method %s of instance %s.\n"), *p, method, instance);
77*7c478bd9Sstevel@tonic-gate 	return (p);
78*7c478bd9Sstevel@tonic-gate }
79*7c478bd9Sstevel@tonic-gate 
80*7c478bd9Sstevel@tonic-gate /*
81*7c478bd9Sstevel@tonic-gate  * Create an environment which is appropriate for spawning an SMF aware
82*7c478bd9Sstevel@tonic-gate  * process.
83*7c478bd9Sstevel@tonic-gate  *
84*7c478bd9Sstevel@tonic-gate  * In order to preserve the correctness of the new environment, various
85*7c478bd9Sstevel@tonic-gate  * checks are performed:
86*7c478bd9Sstevel@tonic-gate  *
87*7c478bd9Sstevel@tonic-gate  * - All SMF_ entries are ignored.  All SMF_ entries should be provided
88*7c478bd9Sstevel@tonic-gate  *   by this function.
89*7c478bd9Sstevel@tonic-gate  * - Duplicates in the entry are eliminated.
90*7c478bd9Sstevel@tonic-gate  * - Malformed entries are eliminated.
91*7c478bd9Sstevel@tonic-gate  *
92*7c478bd9Sstevel@tonic-gate  * Detected errors are logged but not fatal, since a single bad entry
93*7c478bd9Sstevel@tonic-gate  * should not be enough to prevent an SMF_ functional environment from
94*7c478bd9Sstevel@tonic-gate  * being created.
95*7c478bd9Sstevel@tonic-gate  */
96*7c478bd9Sstevel@tonic-gate char **
set_smf_env(struct method_context * mthd_ctxt,instance_t * instance,const char * method)97*7c478bd9Sstevel@tonic-gate set_smf_env(struct method_context *mthd_ctxt, instance_t *instance,
98*7c478bd9Sstevel@tonic-gate     const char *method)
99*7c478bd9Sstevel@tonic-gate {
100*7c478bd9Sstevel@tonic-gate 	char **nenv;
101*7c478bd9Sstevel@tonic-gate 	char **p, **np;
102*7c478bd9Sstevel@tonic-gate 	size_t nenv_size;
103*7c478bd9Sstevel@tonic-gate 
104*7c478bd9Sstevel@tonic-gate 	/*
105*7c478bd9Sstevel@tonic-gate 	 * Max. of env, three SMF_ variables, and terminating NULL.
106*7c478bd9Sstevel@tonic-gate 	 */
107*7c478bd9Sstevel@tonic-gate 	nenv_size = mthd_ctxt->env_sz + 3 + 1;
108*7c478bd9Sstevel@tonic-gate 
109*7c478bd9Sstevel@tonic-gate 	if (instance->config->basic->inherit_env) {
110*7c478bd9Sstevel@tonic-gate 		for (p = environ; *p != NULL; p++)
111*7c478bd9Sstevel@tonic-gate 			nenv_size++;
112*7c478bd9Sstevel@tonic-gate 	}
113*7c478bd9Sstevel@tonic-gate 
114*7c478bd9Sstevel@tonic-gate 	nenv = malloc(sizeof (char *) * nenv_size);
115*7c478bd9Sstevel@tonic-gate 	if (nenv == NULL)
116*7c478bd9Sstevel@tonic-gate 		return (NULL);
117*7c478bd9Sstevel@tonic-gate 	(void) memset(nenv, 0, sizeof (char *) * nenv_size);
118*7c478bd9Sstevel@tonic-gate 
119*7c478bd9Sstevel@tonic-gate 	np = nenv;
120*7c478bd9Sstevel@tonic-gate 
121*7c478bd9Sstevel@tonic-gate 	*np = uu_msprintf("SMF_RESTARTER=%s", INETD_INSTANCE_FMRI);
122*7c478bd9Sstevel@tonic-gate 	if (*np == NULL)
123*7c478bd9Sstevel@tonic-gate 		goto fail;
124*7c478bd9Sstevel@tonic-gate 	else
125*7c478bd9Sstevel@tonic-gate 		np++;
126*7c478bd9Sstevel@tonic-gate 	*np = uu_msprintf("SMF_FMRI=%s", instance->fmri);
127*7c478bd9Sstevel@tonic-gate 	if (*np == NULL)
128*7c478bd9Sstevel@tonic-gate 		goto fail;
129*7c478bd9Sstevel@tonic-gate 	else
130*7c478bd9Sstevel@tonic-gate 		np++;
131*7c478bd9Sstevel@tonic-gate 	*np = uu_msprintf("SMF_METHOD=%s", method);
132*7c478bd9Sstevel@tonic-gate 	if (*np == NULL)
133*7c478bd9Sstevel@tonic-gate 		goto fail;
134*7c478bd9Sstevel@tonic-gate 	else
135*7c478bd9Sstevel@tonic-gate 		np++;
136*7c478bd9Sstevel@tonic-gate 
137*7c478bd9Sstevel@tonic-gate 	if (instance->config->basic->inherit_env) {
138*7c478bd9Sstevel@tonic-gate 		for (p = environ; *p != NULL; p++) {
139*7c478bd9Sstevel@tonic-gate 			if (!valid_env_var(*p, NULL, NULL))
140*7c478bd9Sstevel@tonic-gate 				continue;
141*7c478bd9Sstevel@tonic-gate 
142*7c478bd9Sstevel@tonic-gate 			*np = strdup(*p);
143*7c478bd9Sstevel@tonic-gate 			if (*np == NULL)
144*7c478bd9Sstevel@tonic-gate 				goto fail;
145*7c478bd9Sstevel@tonic-gate 			else
146*7c478bd9Sstevel@tonic-gate 				np++;
147*7c478bd9Sstevel@tonic-gate 		}
148*7c478bd9Sstevel@tonic-gate 	}
149*7c478bd9Sstevel@tonic-gate 
150*7c478bd9Sstevel@tonic-gate 	if (mthd_ctxt->env != NULL) {
151*7c478bd9Sstevel@tonic-gate 		for (p = mthd_ctxt->env; *p != NULL; p++) {
152*7c478bd9Sstevel@tonic-gate 			char **dup_pos;
153*7c478bd9Sstevel@tonic-gate 
154*7c478bd9Sstevel@tonic-gate 			if (!valid_env_var(*p, instance->fmri, method))
155*7c478bd9Sstevel@tonic-gate 				continue;
156*7c478bd9Sstevel@tonic-gate 
157*7c478bd9Sstevel@tonic-gate 			if ((dup_pos = find_dup(*p, nenv, instance->fmri,
158*7c478bd9Sstevel@tonic-gate 			    method)) != NULL) {
159*7c478bd9Sstevel@tonic-gate 				free(*dup_pos);
160*7c478bd9Sstevel@tonic-gate 				*dup_pos = strdup(*p);
161*7c478bd9Sstevel@tonic-gate 				if (*dup_pos == NULL)
162*7c478bd9Sstevel@tonic-gate 					goto fail;
163*7c478bd9Sstevel@tonic-gate 			} else {
164*7c478bd9Sstevel@tonic-gate 				*np = strdup(*p);
165*7c478bd9Sstevel@tonic-gate 				if (*np == NULL)
166*7c478bd9Sstevel@tonic-gate 					goto fail;
167*7c478bd9Sstevel@tonic-gate 				else
168*7c478bd9Sstevel@tonic-gate 					np++;
169*7c478bd9Sstevel@tonic-gate 			}
170*7c478bd9Sstevel@tonic-gate 		}
171*7c478bd9Sstevel@tonic-gate 	}
172*7c478bd9Sstevel@tonic-gate 	*np = NULL;
173*7c478bd9Sstevel@tonic-gate 
174*7c478bd9Sstevel@tonic-gate 	return (nenv);
175*7c478bd9Sstevel@tonic-gate fail:
176*7c478bd9Sstevel@tonic-gate 	p = nenv;
177*7c478bd9Sstevel@tonic-gate 	while (nenv_size--)
178*7c478bd9Sstevel@tonic-gate 		free(*p++);
179*7c478bd9Sstevel@tonic-gate 	free(nenv);
180*7c478bd9Sstevel@tonic-gate 	return (NULL);
181*7c478bd9Sstevel@tonic-gate }
182