xref: /illumos-gate/usr/src/cmd/svc/startd/env.c (revision a28480fe)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
56927f468Sdp  * Common Development and Distribution License (the "License").
66927f468Sdp  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
2213d8aaa1SSean Wilcox  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
24*a28480feSAndy Fiddaman  * Copyright 2021 OmniOS Community Edition (OmniOSce) Association.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #include <assert.h>
28*a28480feSAndy Fiddaman #include <definit.h>
297c478bd9Sstevel@tonic-gate #include <libuutil.h>
307c478bd9Sstevel@tonic-gate #include <stdio.h>
317c478bd9Sstevel@tonic-gate #include <stdlib.h>
327c478bd9Sstevel@tonic-gate #include <string.h>
336927f468Sdp #include <zone.h>
347c478bd9Sstevel@tonic-gate #include <sys/types.h>
357c478bd9Sstevel@tonic-gate #include <sys/stat.h>
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate #include "startd.h"
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate /*
407c478bd9Sstevel@tonic-gate  * This file contains functions for setting the environment for
417c478bd9Sstevel@tonic-gate  * processes started by svc.startd.
427c478bd9Sstevel@tonic-gate  */
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate #define	MAXCMDL		512
457c478bd9Sstevel@tonic-gate #define	DEF_PATH	"PATH=/usr/sbin:/usr/bin"
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate static char **glob_envp;	/* Array of environment strings */
487c478bd9Sstevel@tonic-gate static int glob_env_n;		/* Number of environment slots allocated. */
497c478bd9Sstevel@tonic-gate 
506927f468Sdp static char zonename[ZONENAME_MAX];
516927f468Sdp 
527c478bd9Sstevel@tonic-gate /*
537c478bd9Sstevel@tonic-gate  * init_env()
547c478bd9Sstevel@tonic-gate  *   A clone of the work init.c does to provide as much compatibility
557c478bd9Sstevel@tonic-gate  *   for startup scripts as possible.
567c478bd9Sstevel@tonic-gate  */
577c478bd9Sstevel@tonic-gate void
init_env()587c478bd9Sstevel@tonic-gate init_env()
597c478bd9Sstevel@tonic-gate {
60*a28480feSAndy Fiddaman 	void		*dstate;
61*a28480feSAndy Fiddaman 	const char	*tokp;
62*a28480feSAndy Fiddaman 	int		i;
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate 	glob_env_n = 16;
657c478bd9Sstevel@tonic-gate 	glob_envp = startd_alloc(sizeof (*glob_envp) * glob_env_n);
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate 	glob_envp[0] = startd_alloc((unsigned)(strlen(DEF_PATH)+2));
687c478bd9Sstevel@tonic-gate 	(void) strcpy(glob_envp[0], DEF_PATH);
697c478bd9Sstevel@tonic-gate 
70*a28480feSAndy Fiddaman 	if (definit_open(DEFINIT_DEFAULT_FILE, &dstate) != 0) {
717c478bd9Sstevel@tonic-gate 		uu_warn("Cannot open %s. Environment not initialized.\n",
72*a28480feSAndy Fiddaman 		    DEFINIT_DEFAULT_FILE);
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate 		glob_envp[1] = NULL;
757c478bd9Sstevel@tonic-gate 		return;
767c478bd9Sstevel@tonic-gate 	}
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate 	i = 1;
79*a28480feSAndy Fiddaman 	while ((tokp = definit_token(dstate)) != NULL) {
80*a28480feSAndy Fiddaman 		size_t length = strlen(tokp);
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate 		/*
83*a28480feSAndy Fiddaman 		 * init already started us with this umask, and we
84*a28480feSAndy Fiddaman 		 * handled it in startd.c, so just skip it.
857c478bd9Sstevel@tonic-gate 		 */
86*a28480feSAndy Fiddaman 		if (strncmp(tokp, "CMASK=", 6) == 0 ||
87*a28480feSAndy Fiddaman 		    strncmp(tokp, "SMF_", 4) == 0) {
887c478bd9Sstevel@tonic-gate 			continue;
897c478bd9Sstevel@tonic-gate 		}
907c478bd9Sstevel@tonic-gate 
91*a28480feSAndy Fiddaman 		glob_envp[i] = startd_alloc((unsigned)(length + 1));
92*a28480feSAndy Fiddaman 		(void) strcpy(glob_envp[i], tokp);
93*a28480feSAndy Fiddaman 
947c478bd9Sstevel@tonic-gate 		/*
95*a28480feSAndy Fiddaman 		 * Double the environment size whenever it is
96*a28480feSAndy Fiddaman 		 * full.
977c478bd9Sstevel@tonic-gate 		 */
98*a28480feSAndy Fiddaman 		if (++i == glob_env_n) {
99*a28480feSAndy Fiddaman 			char **newp;
100*a28480feSAndy Fiddaman 
101*a28480feSAndy Fiddaman 			glob_env_n *= 2;
102*a28480feSAndy Fiddaman 			newp = startd_alloc(sizeof (*glob_envp) * glob_env_n);
103*a28480feSAndy Fiddaman 			(void) memcpy(newp, glob_envp,
104*a28480feSAndy Fiddaman 			    sizeof (*glob_envp) * glob_env_n / 2);
105*a28480feSAndy Fiddaman 			startd_free(glob_envp,
106*a28480feSAndy Fiddaman 			    sizeof (*glob_envp) * glob_env_n / 2);
107*a28480feSAndy Fiddaman 			glob_envp = newp;
108*a28480feSAndy Fiddaman 		}
1097c478bd9Sstevel@tonic-gate 	}
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate 	/* Append a null pointer to the environment array to mark its end. */
1127c478bd9Sstevel@tonic-gate 	glob_envp[i] = NULL;
1136927f468Sdp 
114*a28480feSAndy Fiddaman 	definit_close(dstate);
115*a28480feSAndy Fiddaman 
1166927f468Sdp 	/*
1176927f468Sdp 	 * Get the zonename once; it is used to set SMF_ZONENAME for methods.
1186927f468Sdp 	 */
1196927f468Sdp 	(void) getzonenamebyid(getzoneid(), zonename, sizeof (zonename));
1206927f468Sdp 
1217c478bd9Sstevel@tonic-gate }
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate static int
valid_env_var(const char * var,const restarter_inst_t * inst,const char * path)1247c478bd9Sstevel@tonic-gate valid_env_var(const char *var, const restarter_inst_t *inst, const char *path)
1257c478bd9Sstevel@tonic-gate {
1267c478bd9Sstevel@tonic-gate 	char *cp = strchr(var, '=');
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate 	if (cp == NULL || cp == var) {
1297c478bd9Sstevel@tonic-gate 		if (inst != NULL)
1307c478bd9Sstevel@tonic-gate 			log_instance(inst, B_FALSE, "Invalid environment "
1317c478bd9Sstevel@tonic-gate 			    "variable \"%s\".", var);
1327c478bd9Sstevel@tonic-gate 		return (0);
1337c478bd9Sstevel@tonic-gate 	} else if (strncmp(var, "SMF_", 4) == 0) {
1347c478bd9Sstevel@tonic-gate 		if (inst != NULL)
1357c478bd9Sstevel@tonic-gate 			log_instance(inst, B_FALSE, "Invalid environment "
1367c478bd9Sstevel@tonic-gate 			    "variable \"%s\"; \"SMF_\" prefix is reserved.",
1377c478bd9Sstevel@tonic-gate 			    var);
1387c478bd9Sstevel@tonic-gate 		return (0);
1397c478bd9Sstevel@tonic-gate 	} else if (path != NULL && strncmp(var, "PATH=", 5) == 0) {
1407c478bd9Sstevel@tonic-gate 		return (0);
1417c478bd9Sstevel@tonic-gate 	}
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate 	return (1);
1447c478bd9Sstevel@tonic-gate }
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate static char **
find_dup(const char * var,char ** env,const restarter_inst_t * inst)1477c478bd9Sstevel@tonic-gate find_dup(const char *var, char **env, const restarter_inst_t *inst)
1487c478bd9Sstevel@tonic-gate {
1497c478bd9Sstevel@tonic-gate 	char **p;
1507c478bd9Sstevel@tonic-gate 	char *tmp;
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 	for (p = env; *p != NULL; p++) {
1537c478bd9Sstevel@tonic-gate 		assert((tmp = strchr(*p, '=')) != NULL);
1547c478bd9Sstevel@tonic-gate 		tmp++;
1557c478bd9Sstevel@tonic-gate 		if (strncmp(*p, var, tmp - *p) == 0)
1567c478bd9Sstevel@tonic-gate 			break;
1577c478bd9Sstevel@tonic-gate 	}
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate 	if (*p == NULL)
1607c478bd9Sstevel@tonic-gate 		return (NULL);
1617c478bd9Sstevel@tonic-gate 
16213d8aaa1SSean Wilcox 	/*
16313d8aaa1SSean Wilcox 	 * The first entry in the array can be ignored when it is the
16413d8aaa1SSean Wilcox 	 * default path.
16513d8aaa1SSean Wilcox 	 */
16613d8aaa1SSean Wilcox 	if (inst != NULL && p != env &&
16713d8aaa1SSean Wilcox 	    strncmp(*p, DEF_PATH, strlen(DEF_PATH)) != 0) {
1687c478bd9Sstevel@tonic-gate 		log_instance(inst, B_FALSE, "Ignoring duplicate "
1697c478bd9Sstevel@tonic-gate 		    "environment variable \"%s\".", *p);
17013d8aaa1SSean Wilcox 	}
17113d8aaa1SSean Wilcox 
1727c478bd9Sstevel@tonic-gate 	return (p);
1737c478bd9Sstevel@tonic-gate }
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate /*
1767c478bd9Sstevel@tonic-gate  * Create an environment which is appropriate for spawning an SMF
1777c478bd9Sstevel@tonic-gate  * aware process. The new environment will consist of the values from
1787c478bd9Sstevel@tonic-gate  * the global environment as modified by the supplied (local) environment.
1797c478bd9Sstevel@tonic-gate  *
1807c478bd9Sstevel@tonic-gate  * In order to preserve the correctness of the new environment,
1817c478bd9Sstevel@tonic-gate  * various checks are performed on the local environment (init_env()
1827c478bd9Sstevel@tonic-gate  * is relied upon to ensure the global environment is correct):
1837c478bd9Sstevel@tonic-gate  *
1847c478bd9Sstevel@tonic-gate  * - All SMF_ entries are ignored. All SMF_ entries should be provided
1857c478bd9Sstevel@tonic-gate  *   by this function.
1867c478bd9Sstevel@tonic-gate  * - Duplicates in the entry are eliminated.
1877c478bd9Sstevel@tonic-gate  * - Malformed entries are eliminated.
1887c478bd9Sstevel@tonic-gate  *
1897c478bd9Sstevel@tonic-gate  * Detected errors are logged as warnings to the appropriate instance
1907c478bd9Sstevel@tonic-gate  * logfile, since a single bad entry should not be enough to prevent
1917c478bd9Sstevel@tonic-gate  * an SMF_ functional environment from being created. The faulty entry
1927c478bd9Sstevel@tonic-gate  * is then ignored when building the environment.
1937c478bd9Sstevel@tonic-gate  *
1947c478bd9Sstevel@tonic-gate  * If env is NULL, then the return is an environment which contains
1957c478bd9Sstevel@tonic-gate  * all default values.
1967c478bd9Sstevel@tonic-gate  *
1977c478bd9Sstevel@tonic-gate  * If "path" is non-NULL, it will silently over-ride any previous
1987c478bd9Sstevel@tonic-gate  * PATH environment variable.
1997c478bd9Sstevel@tonic-gate  *
2007c478bd9Sstevel@tonic-gate  * NB: The returned env and strings are allocated using startd_alloc().
2017c478bd9Sstevel@tonic-gate  */
2027c478bd9Sstevel@tonic-gate char **
set_smf_env(char ** env,size_t env_sz,const char * path,const restarter_inst_t * inst,const char * method)2037c478bd9Sstevel@tonic-gate set_smf_env(char **env, size_t env_sz, const char *path,
2047c478bd9Sstevel@tonic-gate     const restarter_inst_t *inst, const char *method)
2057c478bd9Sstevel@tonic-gate {
2067c478bd9Sstevel@tonic-gate 	char **nenv;
2077c478bd9Sstevel@tonic-gate 	char **p, **np;
2087c478bd9Sstevel@tonic-gate 	size_t nenv_size;
2097c478bd9Sstevel@tonic-gate 	size_t sz;
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate 	/*
2126927f468Sdp 	 * Max. of glob_env, env, four SMF_ variables,
2137c478bd9Sstevel@tonic-gate 	 * path, and terminating NULL.
2147c478bd9Sstevel@tonic-gate 	 */
2156927f468Sdp 	nenv_size = glob_env_n + env_sz + 4 + 1 + 1;
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate 	nenv = startd_zalloc(sizeof (char *) * nenv_size);
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate 	np = nenv;
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate 	if (path != NULL) {
2227c478bd9Sstevel@tonic-gate 		sz = strlen(path) + 1;
2237c478bd9Sstevel@tonic-gate 		*np = startd_alloc(sz);
2247c478bd9Sstevel@tonic-gate 		(void) strlcpy(*np, path, sz);
2257c478bd9Sstevel@tonic-gate 		np++;
2267c478bd9Sstevel@tonic-gate 	}
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate 	if (inst) {
2297c478bd9Sstevel@tonic-gate 		sz = sizeof ("SMF_FMRI=") + strlen(inst->ri_i.i_fmri);
2307c478bd9Sstevel@tonic-gate 		*np = startd_alloc(sz);
2317c478bd9Sstevel@tonic-gate 		(void) strlcpy(*np, "SMF_FMRI=", sz);
2327c478bd9Sstevel@tonic-gate 		(void) strlcat(*np, inst->ri_i.i_fmri, sz);
2337c478bd9Sstevel@tonic-gate 		np++;
2347c478bd9Sstevel@tonic-gate 	}
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate 	if (method) {
2377c478bd9Sstevel@tonic-gate 		sz = sizeof ("SMF_METHOD=") + strlen(method);
2387c478bd9Sstevel@tonic-gate 		*np = startd_alloc(sz);
2397c478bd9Sstevel@tonic-gate 		(void) strlcpy(*np, "SMF_METHOD=", sz);
2407c478bd9Sstevel@tonic-gate 		(void) strlcat(*np, method, sz);
2417c478bd9Sstevel@tonic-gate 		np++;
2427c478bd9Sstevel@tonic-gate 	}
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate 	sz = sizeof ("SMF_RESTARTER=") + strlen(SCF_SERVICE_STARTD);
2457c478bd9Sstevel@tonic-gate 	*np = startd_alloc(sz);
2467c478bd9Sstevel@tonic-gate 	(void) strlcpy(*np, "SMF_RESTARTER=", sz);
2477c478bd9Sstevel@tonic-gate 	(void) strlcat(*np, SCF_SERVICE_STARTD, sz);
2487c478bd9Sstevel@tonic-gate 	np++;
2497c478bd9Sstevel@tonic-gate 
2506927f468Sdp 	sz = sizeof ("SMF_ZONENAME=") + strlen(zonename);
2516927f468Sdp 	*np = startd_alloc(sz);
2526927f468Sdp 	(void) strlcpy(*np, "SMF_ZONENAME=", sz);
2536927f468Sdp 	(void) strlcat(*np, zonename, sz);
2546927f468Sdp 	np++;
2556927f468Sdp 
2567c478bd9Sstevel@tonic-gate 	for (p = glob_envp; *p != NULL; p++) {
2577c478bd9Sstevel@tonic-gate 		if (valid_env_var(*p, inst, path)) {
2587c478bd9Sstevel@tonic-gate 			sz = strlen(*p) + 1;
2597c478bd9Sstevel@tonic-gate 			*np = startd_alloc(sz);
2607c478bd9Sstevel@tonic-gate 			(void) strlcpy(*np, *p, sz);
2617c478bd9Sstevel@tonic-gate 			np++;
2627c478bd9Sstevel@tonic-gate 		}
2637c478bd9Sstevel@tonic-gate 	}
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate 	if (env) {
2667c478bd9Sstevel@tonic-gate 		for (p = env; *p != NULL; p++) {
2677c478bd9Sstevel@tonic-gate 			char **dup_pos;
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate 			if (!valid_env_var(*p, inst, path))
2707c478bd9Sstevel@tonic-gate 				continue;
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate 			if ((dup_pos = find_dup(*p, nenv, inst)) != NULL) {
2737c478bd9Sstevel@tonic-gate 				startd_free(*dup_pos, strlen(*dup_pos) + 1);
2747c478bd9Sstevel@tonic-gate 				sz = strlen(*p) + 1;
2757c478bd9Sstevel@tonic-gate 				*dup_pos = startd_alloc(sz);
2767c478bd9Sstevel@tonic-gate 				(void) strlcpy(*dup_pos, *p, sz);
2777c478bd9Sstevel@tonic-gate 			} else {
2787c478bd9Sstevel@tonic-gate 				sz = strlen(*p) + 1;
2797c478bd9Sstevel@tonic-gate 				*np = startd_alloc(sz);
2807c478bd9Sstevel@tonic-gate 				(void) strlcpy(*np, *p, sz);
2817c478bd9Sstevel@tonic-gate 				np++;
2827c478bd9Sstevel@tonic-gate 			}
2837c478bd9Sstevel@tonic-gate 		}
2847c478bd9Sstevel@tonic-gate 	}
2857c478bd9Sstevel@tonic-gate 	*np = NULL;
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate 	return (nenv);
2887c478bd9Sstevel@tonic-gate }
289