1 /* $OpenBSD: env.c,v 1.23 2011/01/18 14:29:21 millert Exp $ */ 2 3 /* Copyright 1988,1990,1993,1994 by Paul Vixie 4 * All rights reserved 5 */ 6 7 /* 8 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") 9 * Copyright (c) 1997,2000 by Internet Software Consortium, Inc. 10 * 11 * Permission to use, copy, modify, and distribute this software for any 12 * purpose with or without fee is hereby granted, provided that the above 13 * copyright notice and this permission notice appear in all copies. 14 * 15 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES 16 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 17 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR 18 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 19 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 20 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT 21 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 22 */ 23 24 #include "cron.h" 25 26 char ** 27 env_init(void) { 28 char **p = (char **) malloc(sizeof(char **)); 29 30 if (p != NULL) 31 p[0] = NULL; 32 return (p); 33 } 34 35 void 36 env_free(char **envp) { 37 char **p; 38 39 for (p = envp; *p != NULL; p++) 40 free(*p); 41 free(envp); 42 } 43 44 char ** 45 env_copy(char **envp) { 46 int count, i, save_errno; 47 char **p; 48 49 for (count = 0; envp[count] != NULL; count++) 50 continue; 51 p = (char **) calloc(count+1, sizeof(char *)); /* 1 for the NULL */ 52 if (p != NULL) { 53 for (i = 0; i < count; i++) 54 if ((p[i] = strdup(envp[i])) == NULL) { 55 save_errno = errno; 56 while (--i >= 0) 57 free(p[i]); 58 free(p); 59 errno = save_errno; 60 return (NULL); 61 } 62 p[count] = NULL; 63 } 64 return (p); 65 } 66 67 char ** 68 env_set(char **envp, char *envstr) { 69 int count, found; 70 char **p, *envtmp; 71 72 /* 73 * count the number of elements, including the null pointer; 74 * also set 'found' to -1 or index of entry if already in here. 75 */ 76 found = -1; 77 for (count = 0; envp[count] != NULL; count++) { 78 if (!strcmp_until(envp[count], envstr, '=')) 79 found = count; 80 } 81 count++; /* for the NULL */ 82 83 if (found != -1) { 84 /* 85 * it exists already, so just free the existing setting, 86 * save our new one there, and return the existing array. 87 */ 88 if ((envtmp = strdup(envstr)) == NULL) 89 return (NULL); 90 free(envp[found]); 91 envp[found] = envtmp; 92 return (envp); 93 } 94 95 /* 96 * it doesn't exist yet, so resize the array, move null pointer over 97 * one, save our string over the old null pointer, and return resized 98 * array. 99 */ 100 if ((envtmp = strdup(envstr)) == NULL) 101 return (NULL); 102 p = (char **) realloc((void *) envp, 103 (size_t) ((count+1) * sizeof(char **))); 104 if (p == NULL) { 105 free(envtmp); 106 return (NULL); 107 } 108 p[count] = p[count-1]; 109 p[count-1] = envtmp; 110 return (p); 111 } 112 113 /* The following states are used by load_env(), traversed in order: */ 114 enum env_state { 115 NAMEI, /* First char of NAME, may be quote */ 116 NAME, /* Subsequent chars of NAME */ 117 EQ1, /* After end of name, looking for '=' sign */ 118 EQ2, /* After '=', skipping whitespace */ 119 VALUEI, /* First char of VALUE, may be quote */ 120 VALUE, /* Subsequent chars of VALUE */ 121 FINI, /* All done, skipping trailing whitespace */ 122 ERROR /* Error */ 123 }; 124 125 /* return ERR = end of file 126 * FALSE = not an env setting (file was repositioned) 127 * TRUE = was an env setting 128 */ 129 int 130 load_env(char *envstr, FILE *f) { 131 long filepos; 132 int fileline; 133 enum env_state state; 134 char name[MAX_ENVSTR], val[MAX_ENVSTR]; 135 char quotechar, *c, *str; 136 137 filepos = ftell(f); 138 fileline = LineNumber; 139 skip_comments(f); 140 if (EOF == get_string(envstr, MAX_ENVSTR, f, "\n")) 141 return (ERR); 142 143 Debug(DPARS, ("load_env, read <%s>\n", envstr)) 144 145 bzero(name, sizeof name); 146 bzero(val, sizeof val); 147 str = name; 148 state = NAMEI; 149 quotechar = '\0'; 150 c = envstr; 151 while (state != ERROR && *c) { 152 switch (state) { 153 case NAMEI: 154 case VALUEI: 155 if (*c == '\'' || *c == '"') 156 quotechar = *c++; 157 state++; 158 /* FALLTHROUGH */ 159 case NAME: 160 case VALUE: 161 if (quotechar) { 162 if (*c == quotechar) { 163 state++; 164 c++; 165 break; 166 } 167 if (state == NAME && *c == '=') { 168 state = ERROR; 169 break; 170 } 171 } else { 172 if (state == NAME) { 173 if (isspace((unsigned char)*c)) { 174 c++; 175 state++; 176 break; 177 } 178 if (*c == '=') { 179 state++; 180 break; 181 } 182 } 183 } 184 *str++ = *c++; 185 break; 186 case EQ1: 187 if (*c == '=') { 188 state++; 189 str = val; 190 quotechar = '\0'; 191 } else { 192 if (!isspace((unsigned char)*c)) 193 state = ERROR; 194 } 195 c++; 196 break; 197 case EQ2: 198 case FINI: 199 if (isspace((unsigned char)*c)) 200 c++; 201 else 202 state++; 203 break; 204 case ERROR: 205 /* handled below */ 206 break; 207 } 208 } 209 if (state != FINI && !(state == VALUE && !quotechar)) { 210 Debug(DPARS, ("load_env, not an env var, state = %d\n", state)) 211 fseek(f, filepos, SEEK_SET); 212 Set_LineNum(fileline); 213 return (FALSE); 214 } 215 if (state == VALUE) { 216 /* End of unquoted value: trim trailing whitespace */ 217 c = val + strlen(val); 218 while (c > val && isspace((unsigned char)c[-1])) 219 *(--c) = '\0'; 220 } 221 222 /* 2 fields from parser; looks like an env setting */ 223 224 /* 225 * This can't overflow because get_string() limited the size of the 226 * name and val fields. Still, it doesn't hurt to be careful... 227 */ 228 if (snprintf(envstr, MAX_ENVSTR, "%s=%s", name, val) >= MAX_ENVSTR) 229 return (FALSE); 230 Debug(DPARS, ("load_env, <%s> <%s> -> <%s>\n", name, val, envstr)) 231 return (TRUE); 232 } 233 234 char * 235 env_get(char *name, char **envp) { 236 int len = strlen(name); 237 char *p, *q; 238 239 while ((p = *envp++) != NULL) { 240 if (!(q = strchr(p, '='))) 241 continue; 242 if ((q - p) == len && !strncmp(p, name, len)) 243 return (q+1); 244 } 245 return (NULL); 246 } 247