environment.c
1 /* environment.c -- Function for extracting data from the OpenVPN environment table 2 * 3 * GPLv2 only - Copyright (C) 2008 - 2012 4 * David Sommerseth <dazo@users.sourceforge.net> 5 * 6 * This program is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU General Public License 8 * as published by the Free Software Foundation; version 2 9 * of the License. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program; if not, write to the Free Software 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 19 * 20 */ 21 22 /** 23 * @file environment.c 24 * @author David Sommerseth <dazo@users.sourceforge.net> 25 * @date 2008-08-06 26 * 27 * @brief Function for extracting data from the OpenVPN environment table. 28 * 29 */ 30 31 #include <stdio.h> 32 #include <stdarg.h> 33 #include <string.h> 34 35 #include <eurephia_nullsafe.h> 36 #include <eurephia_context.h> 37 #include <eurephia_log.h> 38 39 /** 40 * get_env() retrieve values from the openvpn environment table 41 * 42 * @param ctx eurephiaCTX context 43 * @param logmasking If 1, the value will be masked in the log files (eg. to hide password) 44 * @param len How many bytes to copy out of the environment variable 45 * @param envp the environment table 46 * @param fmt The key to look for (stdarg) 47 * 48 * @return Returns a const char * with the value, or NULL if not found 49 */ 50 char *get_env(eurephiaCTX *ctx, int logmasking, size_t len, 51 const char *envp[], const char *fmt, ... ) 52 { 53 if (envp) { 54 va_list ap; 55 char key[384]; 56 int keylen = 0; 57 int i; 58 59 // Build up the key we are looking for 60 memset(&key, 0, 384); 61 va_start(ap, fmt); 62 vsnprintf(key, 382, fmt, ap); 63 64 // Find the key 65 keylen = strlen (key); 66 for (i = 0; envp[i]; ++i) { 67 if (!strncmp (envp[i], key, keylen)) { 68 const char *cp = envp[i] + keylen; 69 char *ret = NULL; 70 if (*cp == '=') { 71 ret = malloc_nullsafe(ctx, len+2); 72 strncpy(ret, cp+1, len); 73 #ifdef ENABLE_DEBUG 74 int do_mask = 0; 75 #ifndef SHOW_SECRETS 76 do_mask = logmasking; 77 #endif 78 if( ctx != NULL ) { 79 DEBUG(ctx, 30, "Function call: get_env(envp, '%s') == '%s'", 80 key, (do_mask == 0 ? ret : "xxxxxxxxxxxxxx")); 81 } 82 #endif 83 return ret; 84 } 85 } 86 } 87 if( ctx != NULL ) { 88 DEBUG(ctx, 15, "Function call: get_env(envp, '%s') -- environment variable not found", 89 key); 90 } 91 va_end(ap); 92 } 93 return NULL; 94 }