dav/config.c

changeset 5
88625853ae74
child 6
9c64d2a3d101
equal deleted inserted replaced
4:ae5a98f0545c 5:88625853ae74
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2013 Olaf Wintermann. 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 are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <ucx/map.h>
33 #include <libxml/tree.h>
34
35 #include "config.h"
36 #include "utils.h"
37
38 #define xstreq(a,b) xmlStrEqual(BAD_CAST a, BAD_CAST b)
39
40 static UcxMap *repos;
41 static UcxMap *keys;
42
43 void load_config() {
44 repos = ucx_map_new(16);
45 keys = ucx_map_new(16);
46
47 char *file = util_concat_path(getenv("HOME"), ".dav/config.xml");
48 xmlDoc *doc = xmlReadFile(file, NULL, 0);
49 free(file);
50
51 xmlNode *xml_root = xmlDocGetRootElement(doc);
52 xmlNode *node = xml_root->children;
53 while(node) {
54 if(node->type == XML_ELEMENT_NODE) {
55 if(xstreq(node->name, "repository")) {
56 load_repository(node);
57 } else if(xstreq(node->name, "key")) {
58 load_key(node);
59 }
60 }
61 node = node->next;
62 }
63
64 // TODO: free doc
65 }
66
67 void load_repository(xmlNode *reponode) {
68 xmlNode *node = reponode->children;
69 Repository *repo = calloc(1, sizeof(Repository));
70 repo->store_key_property = true;
71 repo->decrypt = true;
72 while(node) {
73 if(node->type == XML_ELEMENT_NODE) {
74 char *value = util_xml_get_text(node);
75 if(!value) {
76 // next
77 } else if(xstreq(node->name, "name")) {
78 repo->name = strdup(value);
79 } else if(xstreq(node->name, "url")) {
80 repo->url = strdup(value);
81 } else if(xstreq(node->name, "user")) {
82 repo->user = strdup(value);
83 } else if(xstreq(node->name, "password")) {
84 // TODO: use base64
85 repo->password = strdup(value);
86 } else if(xstreq(node->name, "default-key")) {
87 repo->default_key = strdup(value);
88 } else if(xstreq(node->name, "encrypt")) {
89 repo->encrypt = util_getboolean(value);
90 } else if(xstreq(node->name, "decrypt")) {
91 repo->decrypt = util_getboolean(value);
92 } else if(xstreq(node->name, "store-key-property")) {
93 repo->store_key_property = util_getboolean(value);
94 }
95 }
96 node = node->next;
97 }
98
99 if(repo->name) {
100 ucx_map_cstr_put(repos, repo->name, repo);
101 } else {
102 // TODO: free
103 }
104 }
105
106 void load_key(xmlNode *keynode) {
107 xmlNode *node = keynode->children;
108 Key *key = calloc(1, sizeof(Key));
109 key->type = KEY_AES256;
110
111 while(node) {
112 if(node->type == XML_ELEMENT_NODE) {
113 char *value = util_xml_get_text(node);
114 if(!value) {
115 // next
116 } else if(xstreq(node->name, "name")) {
117 key->name = strdup(value);
118 } else if(xstreq(node->name, "file")) {
119 // load key file
120 sstr_t key_data = load_key_file(value);
121 if(key_data.length > 0) {
122 key->data = key_data.ptr;
123 key->length = key_data.length;
124 }
125 } else if(xstreq(node->name, "type")) {
126 if(!strcmp(value, "aes128")) {
127 key->type = KEY_AES128;
128 } else if(!strcmp(value, "aes256")) {
129 key->type = KEY_AES256;
130 }
131 }
132
133 }
134 node = node->next;
135 }
136
137 if(key->name) {
138 if(key->type == KEY_AES128) {
139 if(key->length < 16) {
140 return;
141 }
142 key->length = 16;
143 }
144 if(key->type == KEY_AES256) {
145 if(key->length < 32) {
146 return;
147 }
148 key->length = 32;
149 }
150 ucx_map_cstr_put(keys, key->name, key);
151 } else {
152 // TODO: free
153 }
154 }
155
156 sstr_t load_key_file(char *filename) {
157 sstr_t k;
158 k.ptr = NULL;
159 k.length = 0;
160
161 FILE *file = NULL;
162 if(filename[0] == '/') {
163 file = fopen(filename, "r");
164 } else {
165 char *path = util_concat_path(getenv("HOME"), ".dav/");
166 char *p2 = util_concat_path(path, filename);
167 file = fopen(p2, "r");
168 free(path);
169 free(p2);
170 }
171
172 if(!file) {
173 return k;
174 }
175
176 char *data = malloc(256);
177 size_t r = fread(data, 1, 256, file);
178 k.ptr = data;
179 k.length = r;
180
181 fclose(file);
182 return k;
183 }
184
185 Repository* get_repository(char *name) {
186 if(!name) {
187 return NULL;
188 }
189 return ucx_map_cstr_get(repos, name);
190 }
191
192 Key* get_key(char *name) {
193 if(!name) {
194 return NULL;
195 }
196 return ucx_map_cstr_get(keys, name);
197 }

mercurial