39 #include <libidav/utils.h> |
39 #include <libidav/utils.h> |
40 |
40 |
41 #define xstreq(a,b) xmlStrEqual(BAD_CAST a, BAD_CAST b) |
41 #define xstreq(a,b) xmlStrEqual(BAD_CAST a, BAD_CAST b) |
42 #define xstrEQ(a,b) !xmlStrcasecmp(BAD_CAST a, BAD_CAST b) |
42 #define xstrEQ(a,b) !xmlStrcasecmp(BAD_CAST a, BAD_CAST b) |
43 |
43 |
44 #define print_error(...) \ |
44 #define print_error(lineno, ...) \ |
45 do {\ |
45 do {\ |
46 fprintf(stderr, "Error (config.xml): " __VA_ARGS__); \ |
46 fprintf(stderr, "Error (config.xml line %u): ", lineno); \ |
|
47 fprintf(stderr, __VA_ARGS__); \ |
47 fprintf(stderr, "Abort.\n"); \ |
48 fprintf(stderr, "Abort.\n"); \ |
48 } while(0); |
49 } while(0); |
49 #define print_warning(...) \ |
50 #define print_warning(lineno, ...) \ |
50 fprintf(stderr, "Warning (config.xml): " __VA_ARGS__); |
51 do {\ |
|
52 fprintf(stderr, "Warning (config.xml line %u): ", lineno); \ |
|
53 fprintf(stderr, __VA_ARGS__); \ |
|
54 } while(0); |
51 |
55 |
52 #ifdef _WIN32 |
56 #ifdef _WIN32 |
53 #define ENV_HOME getenv("USERPROFILE") |
57 #define ENV_HOME getenv("USERPROFILE") |
54 #else |
58 #else |
55 #define ENV_HOME getenv("HOME") |
59 #define ENV_HOME getenv("HOME") |
180 repo->ssl_version = CURL_SSLVERSION_DEFAULT; |
184 repo->ssl_version = CURL_SSLVERSION_DEFAULT; |
181 repo->authmethods = CURLAUTH_BASIC; |
185 repo->authmethods = CURLAUTH_BASIC; |
182 return repo; |
186 return repo; |
183 } |
187 } |
184 |
188 |
185 int load_repository(xmlNode *reponode) { |
189 static int repo_add_config(Repository *repo, const xmlNode* node) { |
186 Repository *repo = repository_new(); |
190 unsigned short lineno = node->line; |
187 { |
191 char *key = (char*)node->name; |
188 xmlNode *node = reponode->children; |
192 char *value = util_xml_get_text(node); |
189 int ret = 0; |
193 |
190 while(node && !ret) { |
|
191 if(node->type == XML_ELEMENT_NODE) { |
|
192 char *name = (char*)node->name; |
|
193 char *value = util_xml_get_text(node); |
|
194 ret = repo_add_config(repo, name, value); |
|
195 } |
|
196 node = node->next; |
|
197 } |
|
198 if(ret) { |
|
199 free(repo); |
|
200 return 1; |
|
201 } |
|
202 } |
|
203 |
|
204 if(!repo->name) { |
|
205 print_error("missing name for repository.\n"); |
|
206 return 1; |
|
207 } |
|
208 if(!repo->url) { |
|
209 print_error("missing url for repository '%s'.\n", repo->name); |
|
210 return 1; |
|
211 } |
|
212 |
|
213 ucx_map_cstr_put(repos, repo->name, repo); |
|
214 return 0; |
|
215 } |
|
216 |
|
217 int repo_add_config(Repository *repo, char *key, char *value) { |
|
218 /* every key needs a value */ |
194 /* every key needs a value */ |
219 if(!value) { |
195 if(!value) { |
220 /* TODO: maybe this should only be reported, if the key is valid |
196 /* TODO: maybe this should only be reported, if the key is valid |
221 * But this makes the code very ugly. |
197 * But this makes the code very ugly. |
222 */ |
198 */ |
223 print_error("missing value for config element: %s\n", key); |
199 print_error(lineno, "missing value for config element: %s\n", key); |
224 return 1; |
200 return 1; |
225 } |
201 } |
226 |
202 |
227 if(xstreq(key, "name")) { |
203 if(xstreq(key, "name")) { |
228 repo->name = strdup(value); |
204 repo->name = strdup(value); |
277 repo->ssl_version = CURL_SSLVERSION_TLSv1_2; |
253 repo->ssl_version = CURL_SSLVERSION_TLSv1_2; |
278 } |
254 } |
279 #endif |
255 #endif |
280 #endif |
256 #endif |
281 else { |
257 else { |
282 print_warning("unknown ssl version: %s\n", value); |
258 print_warning(lineno, "unknown ssl version: %s\n", value); |
283 } |
259 } |
284 } else if(xstreq(key, "authmethods")) { |
260 } else if(xstreq(key, "authmethods")) { |
285 repo->authmethods = CURLAUTH_NONE; |
261 repo->authmethods = CURLAUTH_NONE; |
286 const char *delims = " \r\n"; |
262 const char *delims = " \r\n"; |
287 char *meths = strdup(value); |
263 char *meths = strdup(value); |
298 } else if(xstrEQ(value, "any")) { |
274 } else if(xstrEQ(value, "any")) { |
299 repo->authmethods = CURLAUTH_ANY; |
275 repo->authmethods = CURLAUTH_ANY; |
300 } else if(xstrEQ(value, "none")) { |
276 } else if(xstrEQ(value, "none")) { |
301 /* skip */ |
277 /* skip */ |
302 } else { |
278 } else { |
303 print_warning("unknown authentication method: %s\n", value); |
279 print_warning(lineno, |
|
280 "unknown authentication method: %s\n", value); |
304 } |
281 } |
305 meth = strtok(NULL, delims); |
282 meth = strtok(NULL, delims); |
306 } |
283 } |
307 free(meths); |
284 free(meths); |
308 } else { |
285 } else { |
309 print_error("unkown repository config element: %s\n", key); |
286 print_error(lineno, "unkown repository config element: %s\n", key); |
310 return 1; |
287 return 1; |
311 } |
288 } |
312 return 0; |
289 return 0; |
313 } |
290 } |
314 |
291 |
315 int load_proxy(DavProxy *proxy, xmlNode *proxynode, int type) { |
292 int load_repository(const xmlNode *reponode) { |
|
293 Repository *repo = repository_new(); |
|
294 { |
|
295 xmlNode *node = reponode->children; |
|
296 int ret = 0; |
|
297 while(node && !ret) { |
|
298 if(node->type == XML_ELEMENT_NODE) { |
|
299 ret = repo_add_config(repo, node); |
|
300 } |
|
301 node = node->next; |
|
302 } |
|
303 if(ret) { |
|
304 free(repo); |
|
305 return 1; |
|
306 } |
|
307 } |
|
308 |
|
309 if(!repo->name) { |
|
310 print_error(reponode->line, "missing name for repository.\n"); |
|
311 return 1; |
|
312 } |
|
313 if(!repo->url) { |
|
314 print_error(reponode->line, |
|
315 "missing url for repository '%s'.\n", repo->name); |
|
316 return 1; |
|
317 } |
|
318 |
|
319 ucx_map_cstr_put(repos, repo->name, repo); |
|
320 return 0; |
|
321 } |
|
322 |
|
323 int load_proxy(DavProxy *proxy, const xmlNode *proxynode, int type) { |
316 const char *stype; |
324 const char *stype; |
317 if(type == HTTPS_PROXY) { |
325 if(type == HTTPS_PROXY) { |
318 stype = "https"; |
326 stype = "https"; |
319 } else if(type == HTTP_PROXY) { |
327 } else if(type == HTTP_PROXY) { |
320 stype = "http"; |
328 stype = "http"; |
321 } |
329 } |
322 |
330 |
323 if(!proxy) { |
331 if(!proxy) { |
324 print_error("no memory reserved for %s proxy.\n", stype); |
332 // no xml error - so report this directly via fprintf |
|
333 fprintf(stderr, "no memory reserved for %s proxy.\n", stype); |
325 return 1; |
334 return 1; |
326 } |
335 } |
327 |
336 |
328 xmlNode *node = proxynode->children; |
337 xmlNode *node = proxynode->children; |
329 int ret = 0; |
338 int ret = 0; |
346 } else if(xstreq(node->name, "no")) { |
355 } else if(xstreq(node->name, "no")) { |
347 if(!(reportmissingvalue = !value)) { |
356 if(!(reportmissingvalue = !value)) { |
348 proxy->no_proxy = strdup(value); |
357 proxy->no_proxy = strdup(value); |
349 } |
358 } |
350 } else { |
359 } else { |
351 print_error("invalid element for proxy config: %s\n", |
360 print_error(node->line, |
|
361 "invalid element for proxy config: %s\n", node->name); |
|
362 ret = 1; |
|
363 } |
|
364 if (reportmissingvalue) { |
|
365 print_error(node->line, |
|
366 "missing value for proxy configuration element: %s\n", |
352 node->name); |
367 node->name); |
353 ret = 1; |
368 ret = 1; |
354 } |
369 } |
355 if (reportmissingvalue) { |
|
356 print_error("missing value for proxy configuration " |
|
357 "element '%s'.\n", node->name); |
|
358 ret = 1; |
|
359 } |
|
360 } |
370 } |
361 node = node->next; |
371 node = node->next; |
362 } |
372 } |
363 |
373 |
364 if(!ret && !proxy->url) { |
374 if(!ret && !proxy->url) { |
365 print_error("missing url for %s proxy.\n", stype); |
375 print_error(proxynode->line, "missing url for %s proxy.\n", stype); |
366 return 1; |
376 return 1; |
367 } |
377 } |
368 |
378 |
369 return ret; |
379 return ret; |
370 } |
380 } |
371 |
381 |
372 int load_key(xmlNode *keynode) { |
382 int load_key(const xmlNode *keynode) { |
373 xmlNode *node = keynode->children; |
383 xmlNode *node = keynode->children; |
374 Key *key = calloc(1, sizeof(Key)); |
384 Key *key = calloc(1, sizeof(Key)); |
375 key->type = KEY_AES256; |
385 key->type = KEY_AES256; |
376 |
386 |
377 int error = 0; |
387 int error = 0; |
387 sstr_t key_data = load_key_file(value); |
397 sstr_t key_data = load_key_file(value); |
388 if(key_data.length > 0) { |
398 if(key_data.length > 0) { |
389 key->data = key_data.ptr; |
399 key->data = key_data.ptr; |
390 key->length = key_data.length; |
400 key->length = key_data.length; |
391 } else { |
401 } else { |
392 print_error("cannot get key from file: %s\n", value); |
402 print_error(node->line, |
|
403 "cannot get key from file: %s\n", value); |
393 error = 1; |
404 error = 1; |
394 } |
405 } |
395 } else if(xstreq(node->name, "type")) { |
406 } else if(xstreq(node->name, "type")) { |
396 if(!strcmp(value, "aes128")) { |
407 if(!strcmp(value, "aes128")) { |
397 key->type = KEY_AES128; |
408 key->type = KEY_AES128; |
398 } else if(!strcmp(value, "aes256")) { |
409 } else if(!strcmp(value, "aes256")) { |
399 key->type = KEY_AES256; |
410 key->type = KEY_AES256; |
400 } else { |
411 } else { |
401 print_error("unknown key type %s\n", value); |
412 print_error(node->line, "unknown key type %s\n", value); |
402 error = 1; |
413 error = 1; |
403 } |
414 } |
404 } |
415 } |
405 |
416 |
406 } |
417 } |