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 5*6927f468Sdp * Common Development and Distribution License (the "License"). 6*6927f468Sdp * 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*6927f468Sdp * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 237c478bd9Sstevel@tonic-gate * Use is subject to license terms. 247c478bd9Sstevel@tonic-gate */ 257c478bd9Sstevel@tonic-gate 267c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 277c478bd9Sstevel@tonic-gate 287c478bd9Sstevel@tonic-gate #include <assert.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> 33*6927f468Sdp #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 *ENVFILE = "/etc/default/init"; /* Default env. */ 487c478bd9Sstevel@tonic-gate 497c478bd9Sstevel@tonic-gate static char **glob_envp; /* Array of environment strings */ 507c478bd9Sstevel@tonic-gate static int glob_env_n; /* Number of environment slots allocated. */ 517c478bd9Sstevel@tonic-gate 52*6927f468Sdp static char zonename[ZONENAME_MAX]; 53*6927f468Sdp 547c478bd9Sstevel@tonic-gate /* 557c478bd9Sstevel@tonic-gate * init_env() 567c478bd9Sstevel@tonic-gate * A clone of the work init.c does to provide as much compatibility 577c478bd9Sstevel@tonic-gate * for startup scripts as possible. 587c478bd9Sstevel@tonic-gate */ 597c478bd9Sstevel@tonic-gate void 607c478bd9Sstevel@tonic-gate init_env() 617c478bd9Sstevel@tonic-gate { 627c478bd9Sstevel@tonic-gate int i; 637c478bd9Sstevel@tonic-gate char line[MAXCMDL]; 647c478bd9Sstevel@tonic-gate FILE *fp; 657c478bd9Sstevel@tonic-gate int inquotes, length, wslength; 667c478bd9Sstevel@tonic-gate char *tokp, *cp1, *cp2; 677c478bd9Sstevel@tonic-gate char **newp; 687c478bd9Sstevel@tonic-gate 697c478bd9Sstevel@tonic-gate glob_env_n = 16; 707c478bd9Sstevel@tonic-gate glob_envp = startd_alloc(sizeof (*glob_envp) * glob_env_n); 717c478bd9Sstevel@tonic-gate 727c478bd9Sstevel@tonic-gate glob_envp[0] = startd_alloc((unsigned)(strlen(DEF_PATH)+2)); 737c478bd9Sstevel@tonic-gate (void) strcpy(glob_envp[0], DEF_PATH); 747c478bd9Sstevel@tonic-gate 757c478bd9Sstevel@tonic-gate if ((fp = fopen(ENVFILE, "r")) == NULL) { 767c478bd9Sstevel@tonic-gate uu_warn("Cannot open %s. Environment not initialized.\n", 777c478bd9Sstevel@tonic-gate ENVFILE); 787c478bd9Sstevel@tonic-gate 797c478bd9Sstevel@tonic-gate glob_envp[1] = NULL; 807c478bd9Sstevel@tonic-gate return; 817c478bd9Sstevel@tonic-gate } 827c478bd9Sstevel@tonic-gate 837c478bd9Sstevel@tonic-gate i = 1; 847c478bd9Sstevel@tonic-gate 857c478bd9Sstevel@tonic-gate while (fgets(line, MAXCMDL - 1, fp) != NULL) { 867c478bd9Sstevel@tonic-gate /* 877c478bd9Sstevel@tonic-gate * Toss newline 887c478bd9Sstevel@tonic-gate */ 897c478bd9Sstevel@tonic-gate length = strlen(line); 907c478bd9Sstevel@tonic-gate if (line[length - 1] == '\n') 917c478bd9Sstevel@tonic-gate line[length - 1] = '\0'; 927c478bd9Sstevel@tonic-gate 937c478bd9Sstevel@tonic-gate /* 947c478bd9Sstevel@tonic-gate * Ignore blank or comment lines. 957c478bd9Sstevel@tonic-gate */ 967c478bd9Sstevel@tonic-gate if (line[0] == '#' || line[0] == '\0' || 977c478bd9Sstevel@tonic-gate (wslength = strspn(line, " \t\n")) == strlen(line) || 987c478bd9Sstevel@tonic-gate strchr(line, '#') == line + wslength) 997c478bd9Sstevel@tonic-gate continue; 1007c478bd9Sstevel@tonic-gate 1017c478bd9Sstevel@tonic-gate /* 1027c478bd9Sstevel@tonic-gate * First make a pass through the line and change 1037c478bd9Sstevel@tonic-gate * any non-quoted semi-colons to blanks so they 1047c478bd9Sstevel@tonic-gate * will be treated as token separators below. 1057c478bd9Sstevel@tonic-gate */ 1067c478bd9Sstevel@tonic-gate inquotes = 0; 1077c478bd9Sstevel@tonic-gate for (cp1 = line; *cp1 != '\0'; cp1++) { 1087c478bd9Sstevel@tonic-gate if (*cp1 == '"') { 1097c478bd9Sstevel@tonic-gate if (inquotes == 0) 1107c478bd9Sstevel@tonic-gate inquotes = 1; 1117c478bd9Sstevel@tonic-gate else 1127c478bd9Sstevel@tonic-gate inquotes = 0; 1137c478bd9Sstevel@tonic-gate } else if (*cp1 == ';') { 1147c478bd9Sstevel@tonic-gate if (inquotes == 0) 1157c478bd9Sstevel@tonic-gate *cp1 = ' '; 1167c478bd9Sstevel@tonic-gate } 1177c478bd9Sstevel@tonic-gate } 1187c478bd9Sstevel@tonic-gate 1197c478bd9Sstevel@tonic-gate /* 1207c478bd9Sstevel@tonic-gate * Tokens within the line are separated by blanks 1217c478bd9Sstevel@tonic-gate * and tabs. For each token in the line which 1227c478bd9Sstevel@tonic-gate * contains a '=' we strip out any quotes and then 1237c478bd9Sstevel@tonic-gate * stick the token in the environment array. 1247c478bd9Sstevel@tonic-gate */ 1257c478bd9Sstevel@tonic-gate if ((tokp = strtok(line, " \t")) == NULL) 1267c478bd9Sstevel@tonic-gate continue; 1277c478bd9Sstevel@tonic-gate 1287c478bd9Sstevel@tonic-gate do { 1297c478bd9Sstevel@tonic-gate cp1 = strchr(tokp, '='); 1307c478bd9Sstevel@tonic-gate if (cp1 == NULL || cp1 == tokp) 1317c478bd9Sstevel@tonic-gate continue; 1327c478bd9Sstevel@tonic-gate length = strlen(tokp); 1337c478bd9Sstevel@tonic-gate while ((cp1 = strpbrk(tokp, "\"\'")) != NULL) { 1347c478bd9Sstevel@tonic-gate for (cp2 = cp1; cp2 < &tokp[length]; cp2++) 1357c478bd9Sstevel@tonic-gate *cp2 = *(cp2 + 1); 1367c478bd9Sstevel@tonic-gate length--; 1377c478bd9Sstevel@tonic-gate } 1387c478bd9Sstevel@tonic-gate 1397c478bd9Sstevel@tonic-gate /* 1407c478bd9Sstevel@tonic-gate * init already started us with this umask, and we 1417c478bd9Sstevel@tonic-gate * handled it in startd.c, so just skip it. 1427c478bd9Sstevel@tonic-gate */ 1437c478bd9Sstevel@tonic-gate if (strncmp(tokp, "CMASK=", 6) == 0 || 1447c478bd9Sstevel@tonic-gate strncmp(tokp, "SMF_", 4) == 0) 1457c478bd9Sstevel@tonic-gate continue; 1467c478bd9Sstevel@tonic-gate 1477c478bd9Sstevel@tonic-gate glob_envp[i] = startd_alloc((unsigned)(length + 1)); 1487c478bd9Sstevel@tonic-gate (void) strcpy(glob_envp[i], tokp); 1497c478bd9Sstevel@tonic-gate 1507c478bd9Sstevel@tonic-gate /* 1517c478bd9Sstevel@tonic-gate * Double the environment size whenever it is 1527c478bd9Sstevel@tonic-gate * full. 1537c478bd9Sstevel@tonic-gate */ 1547c478bd9Sstevel@tonic-gate if (++i == glob_env_n) { 1557c478bd9Sstevel@tonic-gate glob_env_n *= 2; 1567c478bd9Sstevel@tonic-gate newp = startd_alloc(sizeof (*glob_envp) * 1577c478bd9Sstevel@tonic-gate glob_env_n); 1587c478bd9Sstevel@tonic-gate (void) memcpy(newp, glob_envp, 1597c478bd9Sstevel@tonic-gate sizeof (*glob_envp) * glob_env_n / 2); 1607c478bd9Sstevel@tonic-gate startd_free(glob_envp, 1617c478bd9Sstevel@tonic-gate sizeof (*glob_envp) * glob_env_n / 2); 1627c478bd9Sstevel@tonic-gate glob_envp = newp; 1637c478bd9Sstevel@tonic-gate } 1647c478bd9Sstevel@tonic-gate } while ((tokp = strtok(NULL, " \t")) != NULL); 1657c478bd9Sstevel@tonic-gate } 1667c478bd9Sstevel@tonic-gate 1677c478bd9Sstevel@tonic-gate startd_fclose(fp); 1687c478bd9Sstevel@tonic-gate 1697c478bd9Sstevel@tonic-gate /* Append a null pointer to the environment array to mark its end. */ 1707c478bd9Sstevel@tonic-gate glob_envp[i] = NULL; 171*6927f468Sdp 172*6927f468Sdp /* 173*6927f468Sdp * Get the zonename once; it is used to set SMF_ZONENAME for methods. 174*6927f468Sdp */ 175*6927f468Sdp (void) getzonenamebyid(getzoneid(), zonename, sizeof (zonename)); 176*6927f468Sdp 1777c478bd9Sstevel@tonic-gate } 1787c478bd9Sstevel@tonic-gate 1797c478bd9Sstevel@tonic-gate static int 1807c478bd9Sstevel@tonic-gate valid_env_var(const char *var, const restarter_inst_t *inst, const char *path) 1817c478bd9Sstevel@tonic-gate { 1827c478bd9Sstevel@tonic-gate char *cp = strchr(var, '='); 1837c478bd9Sstevel@tonic-gate 1847c478bd9Sstevel@tonic-gate if (cp == NULL || cp == var) { 1857c478bd9Sstevel@tonic-gate if (inst != NULL) 1867c478bd9Sstevel@tonic-gate log_instance(inst, B_FALSE, "Invalid environment " 1877c478bd9Sstevel@tonic-gate "variable \"%s\".", var); 1887c478bd9Sstevel@tonic-gate return (0); 1897c478bd9Sstevel@tonic-gate } else if (strncmp(var, "SMF_", 4) == 0) { 1907c478bd9Sstevel@tonic-gate if (inst != NULL) 1917c478bd9Sstevel@tonic-gate log_instance(inst, B_FALSE, "Invalid environment " 1927c478bd9Sstevel@tonic-gate "variable \"%s\"; \"SMF_\" prefix is reserved.", 1937c478bd9Sstevel@tonic-gate var); 1947c478bd9Sstevel@tonic-gate return (0); 1957c478bd9Sstevel@tonic-gate } else if (path != NULL && strncmp(var, "PATH=", 5) == 0) { 1967c478bd9Sstevel@tonic-gate return (0); 1977c478bd9Sstevel@tonic-gate } 1987c478bd9Sstevel@tonic-gate 1997c478bd9Sstevel@tonic-gate return (1); 2007c478bd9Sstevel@tonic-gate } 2017c478bd9Sstevel@tonic-gate 2027c478bd9Sstevel@tonic-gate static char ** 2037c478bd9Sstevel@tonic-gate find_dup(const char *var, char **env, const restarter_inst_t *inst) 2047c478bd9Sstevel@tonic-gate { 2057c478bd9Sstevel@tonic-gate char **p; 2067c478bd9Sstevel@tonic-gate char *tmp; 2077c478bd9Sstevel@tonic-gate 2087c478bd9Sstevel@tonic-gate for (p = env; *p != NULL; p++) { 2097c478bd9Sstevel@tonic-gate assert((tmp = strchr(*p, '=')) != NULL); 2107c478bd9Sstevel@tonic-gate tmp++; 2117c478bd9Sstevel@tonic-gate if (strncmp(*p, var, tmp - *p) == 0) 2127c478bd9Sstevel@tonic-gate break; 2137c478bd9Sstevel@tonic-gate } 2147c478bd9Sstevel@tonic-gate 2157c478bd9Sstevel@tonic-gate if (*p == NULL) 2167c478bd9Sstevel@tonic-gate return (NULL); 2177c478bd9Sstevel@tonic-gate 2187c478bd9Sstevel@tonic-gate if (inst != NULL) 2197c478bd9Sstevel@tonic-gate log_instance(inst, B_FALSE, "Ignoring duplicate " 2207c478bd9Sstevel@tonic-gate "environment variable \"%s\".", *p); 2217c478bd9Sstevel@tonic-gate return (p); 2227c478bd9Sstevel@tonic-gate } 2237c478bd9Sstevel@tonic-gate 2247c478bd9Sstevel@tonic-gate /* 2257c478bd9Sstevel@tonic-gate * Create an environment which is appropriate for spawning an SMF 2267c478bd9Sstevel@tonic-gate * aware process. The new environment will consist of the values from 2277c478bd9Sstevel@tonic-gate * the global environment as modified by the supplied (local) environment. 2287c478bd9Sstevel@tonic-gate * 2297c478bd9Sstevel@tonic-gate * In order to preserve the correctness of the new environment, 2307c478bd9Sstevel@tonic-gate * various checks are performed on the local environment (init_env() 2317c478bd9Sstevel@tonic-gate * is relied upon to ensure the global environment is correct): 2327c478bd9Sstevel@tonic-gate * 2337c478bd9Sstevel@tonic-gate * - All SMF_ entries are ignored. All SMF_ entries should be provided 2347c478bd9Sstevel@tonic-gate * by this function. 2357c478bd9Sstevel@tonic-gate * - Duplicates in the entry are eliminated. 2367c478bd9Sstevel@tonic-gate * - Malformed entries are eliminated. 2377c478bd9Sstevel@tonic-gate * 2387c478bd9Sstevel@tonic-gate * Detected errors are logged as warnings to the appropriate instance 2397c478bd9Sstevel@tonic-gate * logfile, since a single bad entry should not be enough to prevent 2407c478bd9Sstevel@tonic-gate * an SMF_ functional environment from being created. The faulty entry 2417c478bd9Sstevel@tonic-gate * is then ignored when building the environment. 2427c478bd9Sstevel@tonic-gate * 2437c478bd9Sstevel@tonic-gate * If env is NULL, then the return is an environment which contains 2447c478bd9Sstevel@tonic-gate * all default values. 2457c478bd9Sstevel@tonic-gate * 2467c478bd9Sstevel@tonic-gate * If "path" is non-NULL, it will silently over-ride any previous 2477c478bd9Sstevel@tonic-gate * PATH environment variable. 2487c478bd9Sstevel@tonic-gate * 2497c478bd9Sstevel@tonic-gate * NB: The returned env and strings are allocated using startd_alloc(). 2507c478bd9Sstevel@tonic-gate */ 2517c478bd9Sstevel@tonic-gate char ** 2527c478bd9Sstevel@tonic-gate set_smf_env(char **env, size_t env_sz, const char *path, 2537c478bd9Sstevel@tonic-gate const restarter_inst_t *inst, const char *method) 2547c478bd9Sstevel@tonic-gate { 2557c478bd9Sstevel@tonic-gate char **nenv; 2567c478bd9Sstevel@tonic-gate char **p, **np; 2577c478bd9Sstevel@tonic-gate size_t nenv_size; 2587c478bd9Sstevel@tonic-gate size_t sz; 2597c478bd9Sstevel@tonic-gate 2607c478bd9Sstevel@tonic-gate /* 261*6927f468Sdp * Max. of glob_env, env, four SMF_ variables, 2627c478bd9Sstevel@tonic-gate * path, and terminating NULL. 2637c478bd9Sstevel@tonic-gate */ 264*6927f468Sdp nenv_size = glob_env_n + env_sz + 4 + 1 + 1; 2657c478bd9Sstevel@tonic-gate 2667c478bd9Sstevel@tonic-gate nenv = startd_zalloc(sizeof (char *) * nenv_size); 2677c478bd9Sstevel@tonic-gate 2687c478bd9Sstevel@tonic-gate np = nenv; 2697c478bd9Sstevel@tonic-gate 2707c478bd9Sstevel@tonic-gate if (path != NULL) { 2717c478bd9Sstevel@tonic-gate sz = strlen(path) + 1; 2727c478bd9Sstevel@tonic-gate *np = startd_alloc(sz); 2737c478bd9Sstevel@tonic-gate (void) strlcpy(*np, path, sz); 2747c478bd9Sstevel@tonic-gate np++; 2757c478bd9Sstevel@tonic-gate } 2767c478bd9Sstevel@tonic-gate 2777c478bd9Sstevel@tonic-gate if (inst) { 2787c478bd9Sstevel@tonic-gate sz = sizeof ("SMF_FMRI=") + strlen(inst->ri_i.i_fmri); 2797c478bd9Sstevel@tonic-gate *np = startd_alloc(sz); 2807c478bd9Sstevel@tonic-gate (void) strlcpy(*np, "SMF_FMRI=", sz); 2817c478bd9Sstevel@tonic-gate (void) strlcat(*np, inst->ri_i.i_fmri, sz); 2827c478bd9Sstevel@tonic-gate np++; 2837c478bd9Sstevel@tonic-gate } 2847c478bd9Sstevel@tonic-gate 2857c478bd9Sstevel@tonic-gate if (method) { 2867c478bd9Sstevel@tonic-gate sz = sizeof ("SMF_METHOD=") + strlen(method); 2877c478bd9Sstevel@tonic-gate *np = startd_alloc(sz); 2887c478bd9Sstevel@tonic-gate (void) strlcpy(*np, "SMF_METHOD=", sz); 2897c478bd9Sstevel@tonic-gate (void) strlcat(*np, method, sz); 2907c478bd9Sstevel@tonic-gate np++; 2917c478bd9Sstevel@tonic-gate } 2927c478bd9Sstevel@tonic-gate 2937c478bd9Sstevel@tonic-gate sz = sizeof ("SMF_RESTARTER=") + strlen(SCF_SERVICE_STARTD); 2947c478bd9Sstevel@tonic-gate *np = startd_alloc(sz); 2957c478bd9Sstevel@tonic-gate (void) strlcpy(*np, "SMF_RESTARTER=", sz); 2967c478bd9Sstevel@tonic-gate (void) strlcat(*np, SCF_SERVICE_STARTD, sz); 2977c478bd9Sstevel@tonic-gate np++; 2987c478bd9Sstevel@tonic-gate 299*6927f468Sdp sz = sizeof ("SMF_ZONENAME=") + strlen(zonename); 300*6927f468Sdp *np = startd_alloc(sz); 301*6927f468Sdp (void) strlcpy(*np, "SMF_ZONENAME=", sz); 302*6927f468Sdp (void) strlcat(*np, zonename, sz); 303*6927f468Sdp np++; 304*6927f468Sdp 3057c478bd9Sstevel@tonic-gate for (p = glob_envp; *p != NULL; p++) { 3067c478bd9Sstevel@tonic-gate if (valid_env_var(*p, inst, path)) { 3077c478bd9Sstevel@tonic-gate sz = strlen(*p) + 1; 3087c478bd9Sstevel@tonic-gate *np = startd_alloc(sz); 3097c478bd9Sstevel@tonic-gate (void) strlcpy(*np, *p, sz); 3107c478bd9Sstevel@tonic-gate np++; 3117c478bd9Sstevel@tonic-gate } 3127c478bd9Sstevel@tonic-gate } 3137c478bd9Sstevel@tonic-gate 3147c478bd9Sstevel@tonic-gate if (env) { 3157c478bd9Sstevel@tonic-gate for (p = env; *p != NULL; p++) { 3167c478bd9Sstevel@tonic-gate char **dup_pos; 3177c478bd9Sstevel@tonic-gate 3187c478bd9Sstevel@tonic-gate if (!valid_env_var(*p, inst, path)) 3197c478bd9Sstevel@tonic-gate continue; 3207c478bd9Sstevel@tonic-gate 3217c478bd9Sstevel@tonic-gate if ((dup_pos = find_dup(*p, nenv, inst)) != NULL) { 3227c478bd9Sstevel@tonic-gate startd_free(*dup_pos, strlen(*dup_pos) + 1); 3237c478bd9Sstevel@tonic-gate sz = strlen(*p) + 1; 3247c478bd9Sstevel@tonic-gate *dup_pos = startd_alloc(sz); 3257c478bd9Sstevel@tonic-gate (void) strlcpy(*dup_pos, *p, sz); 3267c478bd9Sstevel@tonic-gate } else { 3277c478bd9Sstevel@tonic-gate sz = strlen(*p) + 1; 3287c478bd9Sstevel@tonic-gate *np = startd_alloc(sz); 3297c478bd9Sstevel@tonic-gate (void) strlcpy(*np, *p, sz); 3307c478bd9Sstevel@tonic-gate np++; 3317c478bd9Sstevel@tonic-gate } 3327c478bd9Sstevel@tonic-gate } 3337c478bd9Sstevel@tonic-gate } 3347c478bd9Sstevel@tonic-gate *np = NULL; 3357c478bd9Sstevel@tonic-gate 3367c478bd9Sstevel@tonic-gate return (nenv); 3377c478bd9Sstevel@tonic-gate } 338