15e9cd1aeSAssar Westerlund /* 2*ae771770SStanislav Sedov * Copyright (c) 2000 Kungliga Tekniska Högskolan 35e9cd1aeSAssar Westerlund * (Royal Institute of Technology, Stockholm, Sweden). 45e9cd1aeSAssar Westerlund * All rights reserved. 55e9cd1aeSAssar Westerlund * 65e9cd1aeSAssar Westerlund * Redistribution and use in source and binary forms, with or without 75e9cd1aeSAssar Westerlund * modification, are permitted provided that the following conditions 85e9cd1aeSAssar Westerlund * are met: 95e9cd1aeSAssar Westerlund * 105e9cd1aeSAssar Westerlund * 1. Redistributions of source code must retain the above copyright 115e9cd1aeSAssar Westerlund * notice, this list of conditions and the following disclaimer. 125e9cd1aeSAssar Westerlund * 135e9cd1aeSAssar Westerlund * 2. Redistributions in binary form must reproduce the above copyright 145e9cd1aeSAssar Westerlund * notice, this list of conditions and the following disclaimer in the 155e9cd1aeSAssar Westerlund * documentation and/or other materials provided with the distribution. 165e9cd1aeSAssar Westerlund * 175e9cd1aeSAssar Westerlund * 3. Neither the name of the Institute nor the names of its contributors 185e9cd1aeSAssar Westerlund * may be used to endorse or promote products derived from this software 195e9cd1aeSAssar Westerlund * without specific prior written permission. 205e9cd1aeSAssar Westerlund * 215e9cd1aeSAssar Westerlund * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 225e9cd1aeSAssar Westerlund * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 235e9cd1aeSAssar Westerlund * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 245e9cd1aeSAssar Westerlund * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 255e9cd1aeSAssar Westerlund * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 265e9cd1aeSAssar Westerlund * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 275e9cd1aeSAssar Westerlund * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 285e9cd1aeSAssar Westerlund * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 295e9cd1aeSAssar Westerlund * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 305e9cd1aeSAssar Westerlund * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 315e9cd1aeSAssar Westerlund * SUCH DAMAGE. 325e9cd1aeSAssar Westerlund */ 335e9cd1aeSAssar Westerlund 345e9cd1aeSAssar Westerlund #include "login_locl.h" 35*ae771770SStanislav Sedov RCSID("$Id$"); 365e9cd1aeSAssar Westerlund 375e9cd1aeSAssar Westerlund /* 385e9cd1aeSAssar Westerlund * the environment we will send to execle and the shell. 395e9cd1aeSAssar Westerlund */ 405e9cd1aeSAssar Westerlund 415e9cd1aeSAssar Westerlund char **env; 425e9cd1aeSAssar Westerlund int num_env; 435e9cd1aeSAssar Westerlund 445e9cd1aeSAssar Westerlund void 455e9cd1aeSAssar Westerlund extend_env(char *str) 465e9cd1aeSAssar Westerlund { 475e9cd1aeSAssar Westerlund env = realloc(env, (num_env + 1) * sizeof(*env)); 485e9cd1aeSAssar Westerlund if(env == NULL) 495e9cd1aeSAssar Westerlund errx(1, "Out of memory!"); 505e9cd1aeSAssar Westerlund env[num_env++] = str; 515e9cd1aeSAssar Westerlund } 525e9cd1aeSAssar Westerlund 535e9cd1aeSAssar Westerlund void 545e9cd1aeSAssar Westerlund add_env(const char *var, const char *value) 555e9cd1aeSAssar Westerlund { 565e9cd1aeSAssar Westerlund int i; 575e9cd1aeSAssar Westerlund char *str; 585e9cd1aeSAssar Westerlund asprintf(&str, "%s=%s", var, value); 595e9cd1aeSAssar Westerlund if(str == NULL) 605e9cd1aeSAssar Westerlund errx(1, "Out of memory!"); 615e9cd1aeSAssar Westerlund for(i = 0; i < num_env; i++) 625e9cd1aeSAssar Westerlund if(strncmp(env[i], var, strlen(var)) == 0 && 635e9cd1aeSAssar Westerlund env[i][strlen(var)] == '='){ 645e9cd1aeSAssar Westerlund free(env[i]); 655e9cd1aeSAssar Westerlund env[i] = str; 665e9cd1aeSAssar Westerlund return; 675e9cd1aeSAssar Westerlund } 685e9cd1aeSAssar Westerlund 695e9cd1aeSAssar Westerlund extend_env(str); 705e9cd1aeSAssar Westerlund } 715e9cd1aeSAssar Westerlund 72*ae771770SStanislav Sedov #if !HAVE_DECL_ENVIRON 73*ae771770SStanislav Sedov extern char **environ; 74*ae771770SStanislav Sedov #endif 75*ae771770SStanislav Sedov 76*ae771770SStanislav Sedov 775e9cd1aeSAssar Westerlund void 785e9cd1aeSAssar Westerlund copy_env(void) 795e9cd1aeSAssar Westerlund { 805e9cd1aeSAssar Westerlund char **p; 815e9cd1aeSAssar Westerlund for(p = environ; *p; p++) 825e9cd1aeSAssar Westerlund extend_env(*p); 835e9cd1aeSAssar Westerlund } 845e9cd1aeSAssar Westerlund 85*ae771770SStanislav Sedov void 865e9cd1aeSAssar Westerlund login_read_env(const char *file) 875e9cd1aeSAssar Westerlund { 885e9cd1aeSAssar Westerlund char **newenv; 895e9cd1aeSAssar Westerlund char *p; 905e9cd1aeSAssar Westerlund int i, j; 915e9cd1aeSAssar Westerlund 925e9cd1aeSAssar Westerlund newenv = NULL; 935e9cd1aeSAssar Westerlund i = read_environment(file, &newenv); 945e9cd1aeSAssar Westerlund for (j = 0; j < i; j++) { 955e9cd1aeSAssar Westerlund p = strchr(newenv[j], '='); 96*ae771770SStanislav Sedov if (p == NULL) 97*ae771770SStanislav Sedov errx(1, "%s: missing = in string %s", 98*ae771770SStanislav Sedov file, newenv[j]); 995e9cd1aeSAssar Westerlund *p++ = 0; 1005e9cd1aeSAssar Westerlund add_env(newenv[j], p); 1015e9cd1aeSAssar Westerlund *--p = '='; 1025e9cd1aeSAssar Westerlund free(newenv[j]); 1035e9cd1aeSAssar Westerlund } 1045e9cd1aeSAssar Westerlund free(newenv); 1055e9cd1aeSAssar Westerlund } 106