1 /* 2 * Copyright (c) 2007 - 2008 Kungliga Tekniska Högskolan 3 * (Royal Institute of Technology, Stockholm, Sweden). 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * 3. Neither the name of the Institute nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34 #include "hx_locl.h" 35 36 /** 37 * @page page_env Hx509 enviroment functions 38 * 39 * See the library functions here: @ref hx509_env 40 */ 41 42 /** 43 * Add a new key/value pair to the hx509_env. 44 * 45 * @param context A hx509 context. 46 * @param env enviroment to add the enviroment variable too. 47 * @param key key to add 48 * @param value value to add 49 * 50 * @return An hx509 error code, see hx509_get_error_string(). 51 * 52 * @ingroup hx509_env 53 */ 54 55 int 56 hx509_env_add(hx509_context context, hx509_env *env, 57 const char *key, const char *value) 58 { 59 hx509_env n; 60 61 n = malloc(sizeof(*n)); 62 if (n == NULL) { 63 hx509_set_error_string(context, 0, ENOMEM, "out of memory"); 64 return ENOMEM; 65 } 66 67 n->type = env_string; 68 n->next = NULL; 69 n->name = strdup(key); 70 if (n->name == NULL) { 71 free(n); 72 return ENOMEM; 73 } 74 n->u.string = strdup(value); 75 if (n->u.string == NULL) { 76 free(n->name); 77 free(n); 78 return ENOMEM; 79 } 80 81 /* add to tail */ 82 if (*env) { 83 hx509_env e = *env; 84 while (e->next) 85 e = e->next; 86 e->next = n; 87 } else 88 *env = n; 89 90 return 0; 91 } 92 93 /** 94 * Add a new key/binding pair to the hx509_env. 95 * 96 * @param context A hx509 context. 97 * @param env enviroment to add the enviroment variable too. 98 * @param key key to add 99 * @param list binding list to add 100 * 101 * @return An hx509 error code, see hx509_get_error_string(). 102 * 103 * @ingroup hx509_env 104 */ 105 106 int 107 hx509_env_add_binding(hx509_context context, hx509_env *env, 108 const char *key, hx509_env list) 109 { 110 hx509_env n; 111 112 n = malloc(sizeof(*n)); 113 if (n == NULL) { 114 hx509_set_error_string(context, 0, ENOMEM, "out of memory"); 115 return ENOMEM; 116 } 117 118 n->type = env_list; 119 n->next = NULL; 120 n->name = strdup(key); 121 if (n->name == NULL) { 122 free(n); 123 return ENOMEM; 124 } 125 n->u.list = list; 126 127 /* add to tail */ 128 if (*env) { 129 hx509_env e = *env; 130 while (e->next) 131 e = e->next; 132 e->next = n; 133 } else 134 *env = n; 135 136 return 0; 137 } 138 139 140 /** 141 * Search the hx509_env for a length based key. 142 * 143 * @param context A hx509 context. 144 * @param env enviroment to add the enviroment variable too. 145 * @param key key to search for. 146 * @param len length of key. 147 * 148 * @return the value if the key is found, NULL otherwise. 149 * 150 * @ingroup hx509_env 151 */ 152 153 const char * 154 hx509_env_lfind(hx509_context context, hx509_env env, 155 const char *key, size_t len) 156 { 157 while(env) { 158 if (strncmp(key, env->name ,len) == 0 159 && env->name[len] == '\0' && env->type == env_string) 160 return env->u.string; 161 env = env->next; 162 } 163 return NULL; 164 } 165 166 /** 167 * Search the hx509_env for a key. 168 * 169 * @param context A hx509 context. 170 * @param env enviroment to add the enviroment variable too. 171 * @param key key to search for. 172 * 173 * @return the value if the key is found, NULL otherwise. 174 * 175 * @ingroup hx509_env 176 */ 177 178 const char * 179 hx509_env_find(hx509_context context, hx509_env env, const char *key) 180 { 181 while(env) { 182 if (strcmp(key, env->name) == 0 && env->type == env_string) 183 return env->u.string; 184 env = env->next; 185 } 186 return NULL; 187 } 188 189 /** 190 * Search the hx509_env for a binding. 191 * 192 * @param context A hx509 context. 193 * @param env enviroment to add the enviroment variable too. 194 * @param key key to search for. 195 * 196 * @return the binding if the key is found, NULL if not found. 197 * 198 * @ingroup hx509_env 199 */ 200 201 hx509_env 202 hx509_env_find_binding(hx509_context context, 203 hx509_env env, 204 const char *key) 205 { 206 while(env) { 207 if (strcmp(key, env->name) == 0 && env->type == env_list) 208 return env->u.list; 209 env = env->next; 210 } 211 return NULL; 212 } 213 214 static void 215 env_free(hx509_env b) 216 { 217 while(b) { 218 hx509_env next = b->next; 219 220 if (b->type == env_string) 221 free(b->u.string); 222 else if (b->type == env_list) 223 env_free(b->u.list); 224 225 free(b->name); 226 free(b); 227 b = next; 228 } 229 } 230 231 /** 232 * Free an hx509_env enviroment context. 233 * 234 * @param env the enviroment to free. 235 * 236 * @ingroup hx509_env 237 */ 238 239 void 240 hx509_env_free(hx509_env *env) 241 { 242 if (*env) 243 env_free(*env); 244 *env = NULL; 245 } 246