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 <assert.h> 30*0Sstevel@tonic-gate #include <libuutil.h> 31*0Sstevel@tonic-gate #include <stdio.h> 32*0Sstevel@tonic-gate #include <stdlib.h> 33*0Sstevel@tonic-gate #include <string.h> 34*0Sstevel@tonic-gate #include <sys/types.h> 35*0Sstevel@tonic-gate #include <sys/stat.h> 36*0Sstevel@tonic-gate 37*0Sstevel@tonic-gate #include "startd.h" 38*0Sstevel@tonic-gate 39*0Sstevel@tonic-gate /* 40*0Sstevel@tonic-gate * This file contains functions for setting the environment for 41*0Sstevel@tonic-gate * processes started by svc.startd. 42*0Sstevel@tonic-gate */ 43*0Sstevel@tonic-gate 44*0Sstevel@tonic-gate #define MAXCMDL 512 45*0Sstevel@tonic-gate #define DEF_PATH "PATH=/usr/sbin:/usr/bin" 46*0Sstevel@tonic-gate 47*0Sstevel@tonic-gate static char *ENVFILE = "/etc/default/init"; /* Default env. */ 48*0Sstevel@tonic-gate 49*0Sstevel@tonic-gate static char **glob_envp; /* Array of environment strings */ 50*0Sstevel@tonic-gate static int glob_env_n; /* Number of environment slots allocated. */ 51*0Sstevel@tonic-gate 52*0Sstevel@tonic-gate /* 53*0Sstevel@tonic-gate * init_env() 54*0Sstevel@tonic-gate * A clone of the work init.c does to provide as much compatibility 55*0Sstevel@tonic-gate * for startup scripts as possible. 56*0Sstevel@tonic-gate */ 57*0Sstevel@tonic-gate void 58*0Sstevel@tonic-gate init_env() 59*0Sstevel@tonic-gate { 60*0Sstevel@tonic-gate int i; 61*0Sstevel@tonic-gate char line[MAXCMDL]; 62*0Sstevel@tonic-gate FILE *fp; 63*0Sstevel@tonic-gate int inquotes, length, wslength; 64*0Sstevel@tonic-gate char *tokp, *cp1, *cp2; 65*0Sstevel@tonic-gate char **newp; 66*0Sstevel@tonic-gate 67*0Sstevel@tonic-gate glob_env_n = 16; 68*0Sstevel@tonic-gate glob_envp = startd_alloc(sizeof (*glob_envp) * glob_env_n); 69*0Sstevel@tonic-gate 70*0Sstevel@tonic-gate glob_envp[0] = startd_alloc((unsigned)(strlen(DEF_PATH)+2)); 71*0Sstevel@tonic-gate (void) strcpy(glob_envp[0], DEF_PATH); 72*0Sstevel@tonic-gate 73*0Sstevel@tonic-gate if ((fp = fopen(ENVFILE, "r")) == NULL) { 74*0Sstevel@tonic-gate uu_warn("Cannot open %s. Environment not initialized.\n", 75*0Sstevel@tonic-gate ENVFILE); 76*0Sstevel@tonic-gate 77*0Sstevel@tonic-gate glob_envp[1] = NULL; 78*0Sstevel@tonic-gate return; 79*0Sstevel@tonic-gate } 80*0Sstevel@tonic-gate 81*0Sstevel@tonic-gate i = 1; 82*0Sstevel@tonic-gate 83*0Sstevel@tonic-gate while (fgets(line, MAXCMDL - 1, fp) != NULL) { 84*0Sstevel@tonic-gate /* 85*0Sstevel@tonic-gate * Toss newline 86*0Sstevel@tonic-gate */ 87*0Sstevel@tonic-gate length = strlen(line); 88*0Sstevel@tonic-gate if (line[length - 1] == '\n') 89*0Sstevel@tonic-gate line[length - 1] = '\0'; 90*0Sstevel@tonic-gate 91*0Sstevel@tonic-gate /* 92*0Sstevel@tonic-gate * Ignore blank or comment lines. 93*0Sstevel@tonic-gate */ 94*0Sstevel@tonic-gate if (line[0] == '#' || line[0] == '\0' || 95*0Sstevel@tonic-gate (wslength = strspn(line, " \t\n")) == strlen(line) || 96*0Sstevel@tonic-gate strchr(line, '#') == line + wslength) 97*0Sstevel@tonic-gate continue; 98*0Sstevel@tonic-gate 99*0Sstevel@tonic-gate /* 100*0Sstevel@tonic-gate * First make a pass through the line and change 101*0Sstevel@tonic-gate * any non-quoted semi-colons to blanks so they 102*0Sstevel@tonic-gate * will be treated as token separators below. 103*0Sstevel@tonic-gate */ 104*0Sstevel@tonic-gate inquotes = 0; 105*0Sstevel@tonic-gate for (cp1 = line; *cp1 != '\0'; cp1++) { 106*0Sstevel@tonic-gate if (*cp1 == '"') { 107*0Sstevel@tonic-gate if (inquotes == 0) 108*0Sstevel@tonic-gate inquotes = 1; 109*0Sstevel@tonic-gate else 110*0Sstevel@tonic-gate inquotes = 0; 111*0Sstevel@tonic-gate } else if (*cp1 == ';') { 112*0Sstevel@tonic-gate if (inquotes == 0) 113*0Sstevel@tonic-gate *cp1 = ' '; 114*0Sstevel@tonic-gate } 115*0Sstevel@tonic-gate } 116*0Sstevel@tonic-gate 117*0Sstevel@tonic-gate /* 118*0Sstevel@tonic-gate * Tokens within the line are separated by blanks 119*0Sstevel@tonic-gate * and tabs. For each token in the line which 120*0Sstevel@tonic-gate * contains a '=' we strip out any quotes and then 121*0Sstevel@tonic-gate * stick the token in the environment array. 122*0Sstevel@tonic-gate */ 123*0Sstevel@tonic-gate if ((tokp = strtok(line, " \t")) == NULL) 124*0Sstevel@tonic-gate continue; 125*0Sstevel@tonic-gate 126*0Sstevel@tonic-gate do { 127*0Sstevel@tonic-gate cp1 = strchr(tokp, '='); 128*0Sstevel@tonic-gate if (cp1 == NULL || cp1 == tokp) 129*0Sstevel@tonic-gate continue; 130*0Sstevel@tonic-gate length = strlen(tokp); 131*0Sstevel@tonic-gate while ((cp1 = strpbrk(tokp, "\"\'")) != NULL) { 132*0Sstevel@tonic-gate for (cp2 = cp1; cp2 < &tokp[length]; cp2++) 133*0Sstevel@tonic-gate *cp2 = *(cp2 + 1); 134*0Sstevel@tonic-gate length--; 135*0Sstevel@tonic-gate } 136*0Sstevel@tonic-gate 137*0Sstevel@tonic-gate /* 138*0Sstevel@tonic-gate * init already started us with this umask, and we 139*0Sstevel@tonic-gate * handled it in startd.c, so just skip it. 140*0Sstevel@tonic-gate */ 141*0Sstevel@tonic-gate if (strncmp(tokp, "CMASK=", 6) == 0 || 142*0Sstevel@tonic-gate strncmp(tokp, "SMF_", 4) == 0) 143*0Sstevel@tonic-gate continue; 144*0Sstevel@tonic-gate 145*0Sstevel@tonic-gate glob_envp[i] = startd_alloc((unsigned)(length + 1)); 146*0Sstevel@tonic-gate (void) strcpy(glob_envp[i], tokp); 147*0Sstevel@tonic-gate 148*0Sstevel@tonic-gate /* 149*0Sstevel@tonic-gate * Double the environment size whenever it is 150*0Sstevel@tonic-gate * full. 151*0Sstevel@tonic-gate */ 152*0Sstevel@tonic-gate if (++i == glob_env_n) { 153*0Sstevel@tonic-gate glob_env_n *= 2; 154*0Sstevel@tonic-gate newp = startd_alloc(sizeof (*glob_envp) * 155*0Sstevel@tonic-gate glob_env_n); 156*0Sstevel@tonic-gate (void) memcpy(newp, glob_envp, 157*0Sstevel@tonic-gate sizeof (*glob_envp) * glob_env_n / 2); 158*0Sstevel@tonic-gate startd_free(glob_envp, 159*0Sstevel@tonic-gate sizeof (*glob_envp) * glob_env_n / 2); 160*0Sstevel@tonic-gate glob_envp = newp; 161*0Sstevel@tonic-gate } 162*0Sstevel@tonic-gate } while ((tokp = strtok(NULL, " \t")) != NULL); 163*0Sstevel@tonic-gate } 164*0Sstevel@tonic-gate 165*0Sstevel@tonic-gate startd_fclose(fp); 166*0Sstevel@tonic-gate 167*0Sstevel@tonic-gate /* Append a null pointer to the environment array to mark its end. */ 168*0Sstevel@tonic-gate glob_envp[i] = NULL; 169*0Sstevel@tonic-gate } 170*0Sstevel@tonic-gate 171*0Sstevel@tonic-gate static int 172*0Sstevel@tonic-gate valid_env_var(const char *var, const restarter_inst_t *inst, const char *path) 173*0Sstevel@tonic-gate { 174*0Sstevel@tonic-gate char *cp = strchr(var, '='); 175*0Sstevel@tonic-gate 176*0Sstevel@tonic-gate if (cp == NULL || cp == var) { 177*0Sstevel@tonic-gate if (inst != NULL) 178*0Sstevel@tonic-gate log_instance(inst, B_FALSE, "Invalid environment " 179*0Sstevel@tonic-gate "variable \"%s\".", var); 180*0Sstevel@tonic-gate return (0); 181*0Sstevel@tonic-gate } else if (strncmp(var, "SMF_", 4) == 0) { 182*0Sstevel@tonic-gate if (inst != NULL) 183*0Sstevel@tonic-gate log_instance(inst, B_FALSE, "Invalid environment " 184*0Sstevel@tonic-gate "variable \"%s\"; \"SMF_\" prefix is reserved.", 185*0Sstevel@tonic-gate var); 186*0Sstevel@tonic-gate return (0); 187*0Sstevel@tonic-gate } else if (path != NULL && strncmp(var, "PATH=", 5) == 0) { 188*0Sstevel@tonic-gate return (0); 189*0Sstevel@tonic-gate } 190*0Sstevel@tonic-gate 191*0Sstevel@tonic-gate return (1); 192*0Sstevel@tonic-gate } 193*0Sstevel@tonic-gate 194*0Sstevel@tonic-gate static char ** 195*0Sstevel@tonic-gate find_dup(const char *var, char **env, const restarter_inst_t *inst) 196*0Sstevel@tonic-gate { 197*0Sstevel@tonic-gate char **p; 198*0Sstevel@tonic-gate char *tmp; 199*0Sstevel@tonic-gate 200*0Sstevel@tonic-gate for (p = env; *p != NULL; p++) { 201*0Sstevel@tonic-gate assert((tmp = strchr(*p, '=')) != NULL); 202*0Sstevel@tonic-gate tmp++; 203*0Sstevel@tonic-gate if (strncmp(*p, var, tmp - *p) == 0) 204*0Sstevel@tonic-gate break; 205*0Sstevel@tonic-gate } 206*0Sstevel@tonic-gate 207*0Sstevel@tonic-gate if (*p == NULL) 208*0Sstevel@tonic-gate return (NULL); 209*0Sstevel@tonic-gate 210*0Sstevel@tonic-gate if (inst != NULL) 211*0Sstevel@tonic-gate log_instance(inst, B_FALSE, "Ignoring duplicate " 212*0Sstevel@tonic-gate "environment variable \"%s\".", *p); 213*0Sstevel@tonic-gate return (p); 214*0Sstevel@tonic-gate } 215*0Sstevel@tonic-gate 216*0Sstevel@tonic-gate /* 217*0Sstevel@tonic-gate * Create an environment which is appropriate for spawning an SMF 218*0Sstevel@tonic-gate * aware process. The new environment will consist of the values from 219*0Sstevel@tonic-gate * the global environment as modified by the supplied (local) environment. 220*0Sstevel@tonic-gate * 221*0Sstevel@tonic-gate * In order to preserve the correctness of the new environment, 222*0Sstevel@tonic-gate * various checks are performed on the local environment (init_env() 223*0Sstevel@tonic-gate * is relied upon to ensure the global environment is correct): 224*0Sstevel@tonic-gate * 225*0Sstevel@tonic-gate * - All SMF_ entries are ignored. All SMF_ entries should be provided 226*0Sstevel@tonic-gate * by this function. 227*0Sstevel@tonic-gate * - Duplicates in the entry are eliminated. 228*0Sstevel@tonic-gate * - Malformed entries are eliminated. 229*0Sstevel@tonic-gate * 230*0Sstevel@tonic-gate * Detected errors are logged as warnings to the appropriate instance 231*0Sstevel@tonic-gate * logfile, since a single bad entry should not be enough to prevent 232*0Sstevel@tonic-gate * an SMF_ functional environment from being created. The faulty entry 233*0Sstevel@tonic-gate * is then ignored when building the environment. 234*0Sstevel@tonic-gate * 235*0Sstevel@tonic-gate * If env is NULL, then the return is an environment which contains 236*0Sstevel@tonic-gate * all default values. 237*0Sstevel@tonic-gate * 238*0Sstevel@tonic-gate * If "path" is non-NULL, it will silently over-ride any previous 239*0Sstevel@tonic-gate * PATH environment variable. 240*0Sstevel@tonic-gate * 241*0Sstevel@tonic-gate * NB: The returned env and strings are allocated using startd_alloc(). 242*0Sstevel@tonic-gate */ 243*0Sstevel@tonic-gate char ** 244*0Sstevel@tonic-gate set_smf_env(char **env, size_t env_sz, const char *path, 245*0Sstevel@tonic-gate const restarter_inst_t *inst, const char *method) 246*0Sstevel@tonic-gate { 247*0Sstevel@tonic-gate char **nenv; 248*0Sstevel@tonic-gate char **p, **np; 249*0Sstevel@tonic-gate size_t nenv_size; 250*0Sstevel@tonic-gate size_t sz; 251*0Sstevel@tonic-gate 252*0Sstevel@tonic-gate /* 253*0Sstevel@tonic-gate * Max. of glob_env, env, three SMF_ variables, 254*0Sstevel@tonic-gate * path, and terminating NULL. 255*0Sstevel@tonic-gate */ 256*0Sstevel@tonic-gate nenv_size = glob_env_n + env_sz + 3 + 1 + 1; 257*0Sstevel@tonic-gate 258*0Sstevel@tonic-gate nenv = startd_zalloc(sizeof (char *) * nenv_size); 259*0Sstevel@tonic-gate 260*0Sstevel@tonic-gate np = nenv; 261*0Sstevel@tonic-gate 262*0Sstevel@tonic-gate if (path != NULL) { 263*0Sstevel@tonic-gate sz = strlen(path) + 1; 264*0Sstevel@tonic-gate *np = startd_alloc(sz); 265*0Sstevel@tonic-gate (void) strlcpy(*np, path, sz); 266*0Sstevel@tonic-gate np++; 267*0Sstevel@tonic-gate } 268*0Sstevel@tonic-gate 269*0Sstevel@tonic-gate 270*0Sstevel@tonic-gate if (inst) { 271*0Sstevel@tonic-gate sz = sizeof ("SMF_FMRI=") + strlen(inst->ri_i.i_fmri); 272*0Sstevel@tonic-gate *np = startd_alloc(sz); 273*0Sstevel@tonic-gate (void) strlcpy(*np, "SMF_FMRI=", sz); 274*0Sstevel@tonic-gate (void) strlcat(*np, inst->ri_i.i_fmri, sz); 275*0Sstevel@tonic-gate np++; 276*0Sstevel@tonic-gate } 277*0Sstevel@tonic-gate 278*0Sstevel@tonic-gate if (method) { 279*0Sstevel@tonic-gate sz = sizeof ("SMF_METHOD=") + strlen(method); 280*0Sstevel@tonic-gate *np = startd_alloc(sz); 281*0Sstevel@tonic-gate (void) strlcpy(*np, "SMF_METHOD=", sz); 282*0Sstevel@tonic-gate (void) strlcat(*np, method, sz); 283*0Sstevel@tonic-gate np++; 284*0Sstevel@tonic-gate } 285*0Sstevel@tonic-gate 286*0Sstevel@tonic-gate sz = sizeof ("SMF_RESTARTER=") + strlen(SCF_SERVICE_STARTD); 287*0Sstevel@tonic-gate *np = startd_alloc(sz); 288*0Sstevel@tonic-gate (void) strlcpy(*np, "SMF_RESTARTER=", sz); 289*0Sstevel@tonic-gate (void) strlcat(*np, SCF_SERVICE_STARTD, sz); 290*0Sstevel@tonic-gate np++; 291*0Sstevel@tonic-gate 292*0Sstevel@tonic-gate for (p = glob_envp; *p != NULL; p++) { 293*0Sstevel@tonic-gate if (valid_env_var(*p, inst, path)) { 294*0Sstevel@tonic-gate sz = strlen(*p) + 1; 295*0Sstevel@tonic-gate *np = startd_alloc(sz); 296*0Sstevel@tonic-gate (void) strlcpy(*np, *p, sz); 297*0Sstevel@tonic-gate np++; 298*0Sstevel@tonic-gate } 299*0Sstevel@tonic-gate } 300*0Sstevel@tonic-gate 301*0Sstevel@tonic-gate if (env) { 302*0Sstevel@tonic-gate for (p = env; *p != NULL; p++) { 303*0Sstevel@tonic-gate char **dup_pos; 304*0Sstevel@tonic-gate 305*0Sstevel@tonic-gate if (!valid_env_var(*p, inst, path)) 306*0Sstevel@tonic-gate continue; 307*0Sstevel@tonic-gate 308*0Sstevel@tonic-gate if ((dup_pos = find_dup(*p, nenv, inst)) != NULL) { 309*0Sstevel@tonic-gate startd_free(*dup_pos, strlen(*dup_pos) + 1); 310*0Sstevel@tonic-gate sz = strlen(*p) + 1; 311*0Sstevel@tonic-gate *dup_pos = startd_alloc(sz); 312*0Sstevel@tonic-gate (void) strlcpy(*dup_pos, *p, sz); 313*0Sstevel@tonic-gate } else { 314*0Sstevel@tonic-gate sz = strlen(*p) + 1; 315*0Sstevel@tonic-gate *np = startd_alloc(sz); 316*0Sstevel@tonic-gate (void) strlcpy(*np, *p, sz); 317*0Sstevel@tonic-gate np++; 318*0Sstevel@tonic-gate } 319*0Sstevel@tonic-gate } 320*0Sstevel@tonic-gate } 321*0Sstevel@tonic-gate *np = NULL; 322*0Sstevel@tonic-gate 323*0Sstevel@tonic-gate return (nenv); 324*0Sstevel@tonic-gate } 325