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