Line data Source code
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 environment 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 environment to add the environment 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 : HX509_LIB_FUNCTION int HX509_LIB_CALL
56 0 : hx509_env_add(hx509_context context, hx509_env *env,
57 : const char *key, const char *value)
58 : {
59 0 : hx509_env n;
60 :
61 0 : n = malloc(sizeof(*n));
62 0 : if (n == NULL) {
63 0 : hx509_set_error_string(context, 0, ENOMEM, "out of memory");
64 0 : return ENOMEM;
65 : }
66 :
67 0 : n->type = env_string;
68 0 : n->next = NULL;
69 0 : n->name = strdup(key);
70 0 : if (n->name == NULL) {
71 0 : free(n);
72 0 : return ENOMEM;
73 : }
74 0 : n->u.string = strdup(value);
75 0 : if (n->u.string == NULL) {
76 0 : free(n->name);
77 0 : free(n);
78 0 : return ENOMEM;
79 : }
80 :
81 : /* add to tail */
82 0 : if (*env) {
83 0 : hx509_env e = *env;
84 0 : while (e->next)
85 0 : e = e->next;
86 0 : e->next = n;
87 : } else
88 0 : *env = n;
89 :
90 0 : 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 environment to add the environment 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 : HX509_LIB_FUNCTION int HX509_LIB_CALL
107 0 : hx509_env_add_binding(hx509_context context, hx509_env *env,
108 : const char *key, hx509_env list)
109 : {
110 0 : hx509_env n;
111 :
112 0 : n = malloc(sizeof(*n));
113 0 : if (n == NULL) {
114 0 : hx509_set_error_string(context, 0, ENOMEM, "out of memory");
115 0 : return ENOMEM;
116 : }
117 :
118 0 : n->type = env_list;
119 0 : n->next = NULL;
120 0 : n->name = strdup(key);
121 0 : if (n->name == NULL) {
122 0 : free(n);
123 0 : return ENOMEM;
124 : }
125 0 : n->u.list = list;
126 :
127 : /* add to tail */
128 0 : if (*env) {
129 0 : hx509_env e = *env;
130 0 : while (e->next)
131 0 : e = e->next;
132 0 : e->next = n;
133 : } else
134 0 : *env = n;
135 :
136 0 : 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 environment to add the environment 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 : HX509_LIB_FUNCTION const char * HX509_LIB_CALL
154 0 : hx509_env_lfind(hx509_context context, hx509_env env,
155 : const char *key, size_t len)
156 : {
157 0 : while(env) {
158 0 : if (strncmp(key, env->name ,len) == 0
159 0 : && env->name[len] == '\0' && env->type == env_string)
160 0 : return env->u.string;
161 0 : env = env->next;
162 : }
163 0 : return NULL;
164 : }
165 :
166 : /**
167 : * Search the hx509_env for a key.
168 : *
169 : * @param context A hx509 context.
170 : * @param env environment to add the environment 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 : HX509_LIB_FUNCTION const char * HX509_LIB_CALL
179 0 : hx509_env_find(hx509_context context, hx509_env env, const char *key)
180 : {
181 0 : while(env) {
182 0 : if (strcmp(key, env->name) == 0 && env->type == env_string)
183 0 : return env->u.string;
184 0 : env = env->next;
185 : }
186 0 : return NULL;
187 : }
188 :
189 : /**
190 : * Search the hx509_env for a binding.
191 : *
192 : * @param context A hx509 context.
193 : * @param env environment to add the environment 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 0 : hx509_env_find_binding(hx509_context context,
203 : hx509_env env,
204 : const char *key)
205 : {
206 0 : while(env) {
207 0 : if (strcmp(key, env->name) == 0 && env->type == env_list)
208 0 : return env->u.list;
209 0 : env = env->next;
210 : }
211 0 : return NULL;
212 : }
213 :
214 : static void
215 0 : env_free(hx509_env b)
216 : {
217 0 : while(b) {
218 0 : hx509_env next = b->next;
219 :
220 0 : if (b->type == env_string)
221 0 : free(b->u.string);
222 0 : else if (b->type == env_list)
223 0 : env_free(b->u.list);
224 :
225 0 : free(b->name);
226 0 : free(b);
227 0 : b = next;
228 : }
229 0 : }
230 :
231 : /**
232 : * Free an hx509_env environment context.
233 : *
234 : * @param env the environment to free.
235 : *
236 : * @ingroup hx509_env
237 : */
238 :
239 : HX509_LIB_FUNCTION void HX509_LIB_CALL
240 0 : hx509_env_free(hx509_env *env)
241 : {
242 0 : if (*env)
243 0 : env_free(*env);
244 0 : *env = NULL;
245 0 : }
|