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 /* 22*13d8aaa1SSean Wilcox * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 237c478bd9Sstevel@tonic-gate * Use is subject to license terms. 247c478bd9Sstevel@tonic-gate */ 257c478bd9Sstevel@tonic-gate 267c478bd9Sstevel@tonic-gate #include <assert.h> 277c478bd9Sstevel@tonic-gate #include <libuutil.h> 287c478bd9Sstevel@tonic-gate #include <stdio.h> 297c478bd9Sstevel@tonic-gate #include <stdlib.h> 307c478bd9Sstevel@tonic-gate #include <string.h> 316927f468Sdp #include <zone.h> 327c478bd9Sstevel@tonic-gate #include <sys/types.h> 337c478bd9Sstevel@tonic-gate #include <sys/stat.h> 347c478bd9Sstevel@tonic-gate 357c478bd9Sstevel@tonic-gate #include "startd.h" 367c478bd9Sstevel@tonic-gate 377c478bd9Sstevel@tonic-gate /* 387c478bd9Sstevel@tonic-gate * This file contains functions for setting the environment for 397c478bd9Sstevel@tonic-gate * processes started by svc.startd. 407c478bd9Sstevel@tonic-gate */ 417c478bd9Sstevel@tonic-gate 427c478bd9Sstevel@tonic-gate #define MAXCMDL 512 437c478bd9Sstevel@tonic-gate #define DEF_PATH "PATH=/usr/sbin:/usr/bin" 447c478bd9Sstevel@tonic-gate 457c478bd9Sstevel@tonic-gate static char *ENVFILE = "/etc/default/init"; /* Default env. */ 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 587c478bd9Sstevel@tonic-gate init_env() 597c478bd9Sstevel@tonic-gate { 607c478bd9Sstevel@tonic-gate int i; 617c478bd9Sstevel@tonic-gate char line[MAXCMDL]; 627c478bd9Sstevel@tonic-gate FILE *fp; 637c478bd9Sstevel@tonic-gate int inquotes, length, wslength; 647c478bd9Sstevel@tonic-gate char *tokp, *cp1, *cp2; 657c478bd9Sstevel@tonic-gate char **newp; 667c478bd9Sstevel@tonic-gate 677c478bd9Sstevel@tonic-gate glob_env_n = 16; 687c478bd9Sstevel@tonic-gate glob_envp = startd_alloc(sizeof (*glob_envp) * glob_env_n); 697c478bd9Sstevel@tonic-gate 707c478bd9Sstevel@tonic-gate glob_envp[0] = startd_alloc((unsigned)(strlen(DEF_PATH)+2)); 717c478bd9Sstevel@tonic-gate (void) strcpy(glob_envp[0], DEF_PATH); 727c478bd9Sstevel@tonic-gate 737c478bd9Sstevel@tonic-gate if ((fp = fopen(ENVFILE, "r")) == NULL) { 747c478bd9Sstevel@tonic-gate uu_warn("Cannot open %s. Environment not initialized.\n", 757c478bd9Sstevel@tonic-gate ENVFILE); 767c478bd9Sstevel@tonic-gate 777c478bd9Sstevel@tonic-gate glob_envp[1] = NULL; 787c478bd9Sstevel@tonic-gate return; 797c478bd9Sstevel@tonic-gate } 807c478bd9Sstevel@tonic-gate 817c478bd9Sstevel@tonic-gate i = 1; 827c478bd9Sstevel@tonic-gate 837c478bd9Sstevel@tonic-gate while (fgets(line, MAXCMDL - 1, fp) != NULL) { 847c478bd9Sstevel@tonic-gate /* 857c478bd9Sstevel@tonic-gate * Toss newline 867c478bd9Sstevel@tonic-gate */ 877c478bd9Sstevel@tonic-gate length = strlen(line); 887c478bd9Sstevel@tonic-gate if (line[length - 1] == '\n') 897c478bd9Sstevel@tonic-gate line[length - 1] = '\0'; 907c478bd9Sstevel@tonic-gate 917c478bd9Sstevel@tonic-gate /* 927c478bd9Sstevel@tonic-gate * Ignore blank or comment lines. 937c478bd9Sstevel@tonic-gate */ 947c478bd9Sstevel@tonic-gate if (line[0] == '#' || line[0] == '\0' || 957c478bd9Sstevel@tonic-gate (wslength = strspn(line, " \t\n")) == strlen(line) || 967c478bd9Sstevel@tonic-gate strchr(line, '#') == line + wslength) 977c478bd9Sstevel@tonic-gate continue; 987c478bd9Sstevel@tonic-gate 997c478bd9Sstevel@tonic-gate /* 1007c478bd9Sstevel@tonic-gate * First make a pass through the line and change 1017c478bd9Sstevel@tonic-gate * any non-quoted semi-colons to blanks so they 1027c478bd9Sstevel@tonic-gate * will be treated as token separators below. 1037c478bd9Sstevel@tonic-gate */ 1047c478bd9Sstevel@tonic-gate inquotes = 0; 1057c478bd9Sstevel@tonic-gate for (cp1 = line; *cp1 != '\0'; cp1++) { 1067c478bd9Sstevel@tonic-gate if (*cp1 == '"') { 1077c478bd9Sstevel@tonic-gate if (inquotes == 0) 1087c478bd9Sstevel@tonic-gate inquotes = 1; 1097c478bd9Sstevel@tonic-gate else 1107c478bd9Sstevel@tonic-gate inquotes = 0; 1117c478bd9Sstevel@tonic-gate } else if (*cp1 == ';') { 1127c478bd9Sstevel@tonic-gate if (inquotes == 0) 1137c478bd9Sstevel@tonic-gate *cp1 = ' '; 1147c478bd9Sstevel@tonic-gate } 1157c478bd9Sstevel@tonic-gate } 1167c478bd9Sstevel@tonic-gate 1177c478bd9Sstevel@tonic-gate /* 1187c478bd9Sstevel@tonic-gate * Tokens within the line are separated by blanks 1197c478bd9Sstevel@tonic-gate * and tabs. For each token in the line which 1207c478bd9Sstevel@tonic-gate * contains a '=' we strip out any quotes and then 1217c478bd9Sstevel@tonic-gate * stick the token in the environment array. 1227c478bd9Sstevel@tonic-gate */ 1237c478bd9Sstevel@tonic-gate if ((tokp = strtok(line, " \t")) == NULL) 1247c478bd9Sstevel@tonic-gate continue; 1257c478bd9Sstevel@tonic-gate 1267c478bd9Sstevel@tonic-gate do { 1277c478bd9Sstevel@tonic-gate cp1 = strchr(tokp, '='); 1287c478bd9Sstevel@tonic-gate if (cp1 == NULL || cp1 == tokp) 1297c478bd9Sstevel@tonic-gate continue; 1307c478bd9Sstevel@tonic-gate length = strlen(tokp); 1317c478bd9Sstevel@tonic-gate while ((cp1 = strpbrk(tokp, "\"\'")) != NULL) { 1327c478bd9Sstevel@tonic-gate for (cp2 = cp1; cp2 < &tokp[length]; cp2++) 1337c478bd9Sstevel@tonic-gate *cp2 = *(cp2 + 1); 1347c478bd9Sstevel@tonic-gate length--; 1357c478bd9Sstevel@tonic-gate } 1367c478bd9Sstevel@tonic-gate 1377c478bd9Sstevel@tonic-gate /* 1387c478bd9Sstevel@tonic-gate * init already started us with this umask, and we 1397c478bd9Sstevel@tonic-gate * handled it in startd.c, so just skip it. 1407c478bd9Sstevel@tonic-gate */ 1417c478bd9Sstevel@tonic-gate if (strncmp(tokp, "CMASK=", 6) == 0 || 1427c478bd9Sstevel@tonic-gate strncmp(tokp, "SMF_", 4) == 0) 1437c478bd9Sstevel@tonic-gate continue; 1447c478bd9Sstevel@tonic-gate 1457c478bd9Sstevel@tonic-gate glob_envp[i] = startd_alloc((unsigned)(length + 1)); 1467c478bd9Sstevel@tonic-gate (void) strcpy(glob_envp[i], tokp); 1477c478bd9Sstevel@tonic-gate 1487c478bd9Sstevel@tonic-gate /* 1497c478bd9Sstevel@tonic-gate * Double the environment size whenever it is 1507c478bd9Sstevel@tonic-gate * full. 1517c478bd9Sstevel@tonic-gate */ 1527c478bd9Sstevel@tonic-gate if (++i == glob_env_n) { 1537c478bd9Sstevel@tonic-gate glob_env_n *= 2; 1547c478bd9Sstevel@tonic-gate newp = startd_alloc(sizeof (*glob_envp) * 1557c478bd9Sstevel@tonic-gate glob_env_n); 1567c478bd9Sstevel@tonic-gate (void) memcpy(newp, glob_envp, 1577c478bd9Sstevel@tonic-gate sizeof (*glob_envp) * glob_env_n / 2); 1587c478bd9Sstevel@tonic-gate startd_free(glob_envp, 1597c478bd9Sstevel@tonic-gate sizeof (*glob_envp) * glob_env_n / 2); 1607c478bd9Sstevel@tonic-gate glob_envp = newp; 1617c478bd9Sstevel@tonic-gate } 1627c478bd9Sstevel@tonic-gate } while ((tokp = strtok(NULL, " \t")) != NULL); 1637c478bd9Sstevel@tonic-gate } 1647c478bd9Sstevel@tonic-gate 1657c478bd9Sstevel@tonic-gate startd_fclose(fp); 1667c478bd9Sstevel@tonic-gate 1677c478bd9Sstevel@tonic-gate /* Append a null pointer to the environment array to mark its end. */ 1687c478bd9Sstevel@tonic-gate glob_envp[i] = NULL; 1696927f468Sdp 1706927f468Sdp /* 1716927f468Sdp * Get the zonename once; it is used to set SMF_ZONENAME for methods. 1726927f468Sdp */ 1736927f468Sdp (void) getzonenamebyid(getzoneid(), zonename, sizeof (zonename)); 1746927f468Sdp 1757c478bd9Sstevel@tonic-gate } 1767c478bd9Sstevel@tonic-gate 1777c478bd9Sstevel@tonic-gate static int 1787c478bd9Sstevel@tonic-gate valid_env_var(const char *var, const restarter_inst_t *inst, const char *path) 1797c478bd9Sstevel@tonic-gate { 1807c478bd9Sstevel@tonic-gate char *cp = strchr(var, '='); 1817c478bd9Sstevel@tonic-gate 1827c478bd9Sstevel@tonic-gate if (cp == NULL || cp == var) { 1837c478bd9Sstevel@tonic-gate if (inst != NULL) 1847c478bd9Sstevel@tonic-gate log_instance(inst, B_FALSE, "Invalid environment " 1857c478bd9Sstevel@tonic-gate "variable \"%s\".", var); 1867c478bd9Sstevel@tonic-gate return (0); 1877c478bd9Sstevel@tonic-gate } else if (strncmp(var, "SMF_", 4) == 0) { 1887c478bd9Sstevel@tonic-gate if (inst != NULL) 1897c478bd9Sstevel@tonic-gate log_instance(inst, B_FALSE, "Invalid environment " 1907c478bd9Sstevel@tonic-gate "variable \"%s\"; \"SMF_\" prefix is reserved.", 1917c478bd9Sstevel@tonic-gate var); 1927c478bd9Sstevel@tonic-gate return (0); 1937c478bd9Sstevel@tonic-gate } else if (path != NULL && strncmp(var, "PATH=", 5) == 0) { 1947c478bd9Sstevel@tonic-gate return (0); 1957c478bd9Sstevel@tonic-gate } 1967c478bd9Sstevel@tonic-gate 1977c478bd9Sstevel@tonic-gate return (1); 1987c478bd9Sstevel@tonic-gate } 1997c478bd9Sstevel@tonic-gate 2007c478bd9Sstevel@tonic-gate static char ** 2017c478bd9Sstevel@tonic-gate find_dup(const char *var, char **env, const restarter_inst_t *inst) 2027c478bd9Sstevel@tonic-gate { 2037c478bd9Sstevel@tonic-gate char **p; 2047c478bd9Sstevel@tonic-gate char *tmp; 2057c478bd9Sstevel@tonic-gate 2067c478bd9Sstevel@tonic-gate for (p = env; *p != NULL; p++) { 2077c478bd9Sstevel@tonic-gate assert((tmp = strchr(*p, '=')) != NULL); 2087c478bd9Sstevel@tonic-gate tmp++; 2097c478bd9Sstevel@tonic-gate if (strncmp(*p, var, tmp - *p) == 0) 2107c478bd9Sstevel@tonic-gate break; 2117c478bd9Sstevel@tonic-gate } 2127c478bd9Sstevel@tonic-gate 2137c478bd9Sstevel@tonic-gate if (*p == NULL) 2147c478bd9Sstevel@tonic-gate return (NULL); 2157c478bd9Sstevel@tonic-gate 216*13d8aaa1SSean Wilcox /* 217*13d8aaa1SSean Wilcox * The first entry in the array can be ignored when it is the 218*13d8aaa1SSean Wilcox * default path. 219*13d8aaa1SSean Wilcox */ 220*13d8aaa1SSean Wilcox if (inst != NULL && p != env && 221*13d8aaa1SSean Wilcox strncmp(*p, DEF_PATH, strlen(DEF_PATH)) != 0) { 2227c478bd9Sstevel@tonic-gate log_instance(inst, B_FALSE, "Ignoring duplicate " 2237c478bd9Sstevel@tonic-gate "environment variable \"%s\".", *p); 224*13d8aaa1SSean Wilcox } 225*13d8aaa1SSean Wilcox 2267c478bd9Sstevel@tonic-gate return (p); 2277c478bd9Sstevel@tonic-gate } 2287c478bd9Sstevel@tonic-gate 2297c478bd9Sstevel@tonic-gate /* 2307c478bd9Sstevel@tonic-gate * Create an environment which is appropriate for spawning an SMF 2317c478bd9Sstevel@tonic-gate * aware process. The new environment will consist of the values from 2327c478bd9Sstevel@tonic-gate * the global environment as modified by the supplied (local) environment. 2337c478bd9Sstevel@tonic-gate * 2347c478bd9Sstevel@tonic-gate * In order to preserve the correctness of the new environment, 2357c478bd9Sstevel@tonic-gate * various checks are performed on the local environment (init_env() 2367c478bd9Sstevel@tonic-gate * is relied upon to ensure the global environment is correct): 2377c478bd9Sstevel@tonic-gate * 2387c478bd9Sstevel@tonic-gate * - All SMF_ entries are ignored. All SMF_ entries should be provided 2397c478bd9Sstevel@tonic-gate * by this function. 2407c478bd9Sstevel@tonic-gate * - Duplicates in the entry are eliminated. 2417c478bd9Sstevel@tonic-gate * - Malformed entries are eliminated. 2427c478bd9Sstevel@tonic-gate * 2437c478bd9Sstevel@tonic-gate * Detected errors are logged as warnings to the appropriate instance 2447c478bd9Sstevel@tonic-gate * logfile, since a single bad entry should not be enough to prevent 2457c478bd9Sstevel@tonic-gate * an SMF_ functional environment from being created. The faulty entry 2467c478bd9Sstevel@tonic-gate * is then ignored when building the environment. 2477c478bd9Sstevel@tonic-gate * 2487c478bd9Sstevel@tonic-gate * If env is NULL, then the return is an environment which contains 2497c478bd9Sstevel@tonic-gate * all default values. 2507c478bd9Sstevel@tonic-gate * 2517c478bd9Sstevel@tonic-gate * If "path" is non-NULL, it will silently over-ride any previous 2527c478bd9Sstevel@tonic-gate * PATH environment variable. 2537c478bd9Sstevel@tonic-gate * 2547c478bd9Sstevel@tonic-gate * NB: The returned env and strings are allocated using startd_alloc(). 2557c478bd9Sstevel@tonic-gate */ 2567c478bd9Sstevel@tonic-gate char ** 2577c478bd9Sstevel@tonic-gate set_smf_env(char **env, size_t env_sz, const char *path, 2587c478bd9Sstevel@tonic-gate const restarter_inst_t *inst, const char *method) 2597c478bd9Sstevel@tonic-gate { 2607c478bd9Sstevel@tonic-gate char **nenv; 2617c478bd9Sstevel@tonic-gate char **p, **np; 2627c478bd9Sstevel@tonic-gate size_t nenv_size; 2637c478bd9Sstevel@tonic-gate size_t sz; 2647c478bd9Sstevel@tonic-gate 2657c478bd9Sstevel@tonic-gate /* 2666927f468Sdp * Max. of glob_env, env, four SMF_ variables, 2677c478bd9Sstevel@tonic-gate * path, and terminating NULL. 2687c478bd9Sstevel@tonic-gate */ 2696927f468Sdp nenv_size = glob_env_n + env_sz + 4 + 1 + 1; 2707c478bd9Sstevel@tonic-gate 2717c478bd9Sstevel@tonic-gate nenv = startd_zalloc(sizeof (char *) * nenv_size); 2727c478bd9Sstevel@tonic-gate 2737c478bd9Sstevel@tonic-gate np = nenv; 2747c478bd9Sstevel@tonic-gate 2757c478bd9Sstevel@tonic-gate if (path != NULL) { 2767c478bd9Sstevel@tonic-gate sz = strlen(path) + 1; 2777c478bd9Sstevel@tonic-gate *np = startd_alloc(sz); 2787c478bd9Sstevel@tonic-gate (void) strlcpy(*np, path, sz); 2797c478bd9Sstevel@tonic-gate np++; 2807c478bd9Sstevel@tonic-gate } 2817c478bd9Sstevel@tonic-gate 2827c478bd9Sstevel@tonic-gate if (inst) { 2837c478bd9Sstevel@tonic-gate sz = sizeof ("SMF_FMRI=") + strlen(inst->ri_i.i_fmri); 2847c478bd9Sstevel@tonic-gate *np = startd_alloc(sz); 2857c478bd9Sstevel@tonic-gate (void) strlcpy(*np, "SMF_FMRI=", sz); 2867c478bd9Sstevel@tonic-gate (void) strlcat(*np, inst->ri_i.i_fmri, sz); 2877c478bd9Sstevel@tonic-gate np++; 2887c478bd9Sstevel@tonic-gate } 2897c478bd9Sstevel@tonic-gate 2907c478bd9Sstevel@tonic-gate if (method) { 2917c478bd9Sstevel@tonic-gate sz = sizeof ("SMF_METHOD=") + strlen(method); 2927c478bd9Sstevel@tonic-gate *np = startd_alloc(sz); 2937c478bd9Sstevel@tonic-gate (void) strlcpy(*np, "SMF_METHOD=", sz); 2947c478bd9Sstevel@tonic-gate (void) strlcat(*np, method, sz); 2957c478bd9Sstevel@tonic-gate np++; 2967c478bd9Sstevel@tonic-gate } 2977c478bd9Sstevel@tonic-gate 2987c478bd9Sstevel@tonic-gate sz = sizeof ("SMF_RESTARTER=") + strlen(SCF_SERVICE_STARTD); 2997c478bd9Sstevel@tonic-gate *np = startd_alloc(sz); 3007c478bd9Sstevel@tonic-gate (void) strlcpy(*np, "SMF_RESTARTER=", sz); 3017c478bd9Sstevel@tonic-gate (void) strlcat(*np, SCF_SERVICE_STARTD, sz); 3027c478bd9Sstevel@tonic-gate np++; 3037c478bd9Sstevel@tonic-gate 3046927f468Sdp sz = sizeof ("SMF_ZONENAME=") + strlen(zonename); 3056927f468Sdp *np = startd_alloc(sz); 3066927f468Sdp (void) strlcpy(*np, "SMF_ZONENAME=", sz); 3076927f468Sdp (void) strlcat(*np, zonename, sz); 3086927f468Sdp np++; 3096927f468Sdp 3107c478bd9Sstevel@tonic-gate for (p = glob_envp; *p != NULL; p++) { 3117c478bd9Sstevel@tonic-gate if (valid_env_var(*p, inst, path)) { 3127c478bd9Sstevel@tonic-gate sz = strlen(*p) + 1; 3137c478bd9Sstevel@tonic-gate *np = startd_alloc(sz); 3147c478bd9Sstevel@tonic-gate (void) strlcpy(*np, *p, sz); 3157c478bd9Sstevel@tonic-gate np++; 3167c478bd9Sstevel@tonic-gate } 3177c478bd9Sstevel@tonic-gate } 3187c478bd9Sstevel@tonic-gate 3197c478bd9Sstevel@tonic-gate if (env) { 3207c478bd9Sstevel@tonic-gate for (p = env; *p != NULL; p++) { 3217c478bd9Sstevel@tonic-gate char **dup_pos; 3227c478bd9Sstevel@tonic-gate 3237c478bd9Sstevel@tonic-gate if (!valid_env_var(*p, inst, path)) 3247c478bd9Sstevel@tonic-gate continue; 3257c478bd9Sstevel@tonic-gate 3267c478bd9Sstevel@tonic-gate if ((dup_pos = find_dup(*p, nenv, inst)) != NULL) { 3277c478bd9Sstevel@tonic-gate startd_free(*dup_pos, strlen(*dup_pos) + 1); 3287c478bd9Sstevel@tonic-gate sz = strlen(*p) + 1; 3297c478bd9Sstevel@tonic-gate *dup_pos = startd_alloc(sz); 3307c478bd9Sstevel@tonic-gate (void) strlcpy(*dup_pos, *p, sz); 3317c478bd9Sstevel@tonic-gate } else { 3327c478bd9Sstevel@tonic-gate sz = strlen(*p) + 1; 3337c478bd9Sstevel@tonic-gate *np = startd_alloc(sz); 3347c478bd9Sstevel@tonic-gate (void) strlcpy(*np, *p, sz); 3357c478bd9Sstevel@tonic-gate np++; 3367c478bd9Sstevel@tonic-gate } 3377c478bd9Sstevel@tonic-gate } 3387c478bd9Sstevel@tonic-gate } 3397c478bd9Sstevel@tonic-gate *np = NULL; 3407c478bd9Sstevel@tonic-gate 3417c478bd9Sstevel@tonic-gate return (nenv); 3427c478bd9Sstevel@tonic-gate } 343