libidav/config.c

changeset 60
ee4e4742391e
parent 49
2f71f4ee247a
child 62
dbde5e038ea9
equal deleted inserted replaced
59:6bd37fe6d905 60:ee4e4742391e
77 DavCfgNamespace **list_end, 77 DavCfgNamespace **list_end,
78 xmlNode *node); 78 xmlNode *node);
79 static int load_secretstore(DavConfig *config, xmlNode *node); 79 static int load_secretstore(DavConfig *config, xmlNode *node);
80 80
81 81
82 int dav_cfg_string_set_value(DavConfig *config, CfgString *str, xmlNode *node) { 82 int dav_cfg_string_set_node_value(DavConfig *config, CfgString *str, xmlNode *node) {
83 str->node = node; 83 str->node = node;
84 char *value = util_xml_get_text(node); 84 char *value = util_xml_get_text(node);
85 if(value) { 85 if(value) {
86 str->value = cx_strdup_a(config->mp->allocator, cx_str(value)); 86 str->value = cx_strdup_a(config->mp->allocator, cx_str(value));
87 return 0; 87 return 0;
89 str->value = (cxmutstr){NULL, 0}; 89 str->value = (cxmutstr){NULL, 0};
90 return 1; 90 return 1;
91 } 91 }
92 } 92 }
93 93
94 void dav_cfg_bool_set_value(DavConfig *config, CfgBool *cbool, xmlNode *node) { 94 void dav_cfg_bool_set_node_value(DavConfig *config, CfgBool *cbool, xmlNode *node) {
95 cbool->node = node; 95 cbool->node = node;
96 char *value = util_xml_get_text(node); 96 char *value = util_xml_get_text(node);
97 cbool->value = util_getboolean(value); 97 cbool->value = util_getboolean(value);
98 }
99
100 static void set_xml_content(xmlNode *node, const char *content) {
101 xmlNode *child = node->children;
102 while(child) {
103 xmlNode *next = child->next;
104 xmlUnlinkNode(child);
105 xmlFreeNode(child);
106 child = next;
107 }
108
109 if(content) {
110 xmlChar *encoded = xmlEncodeSpecialChars(node->doc, (const xmlChar*)content);
111 if(encoded) {
112 xmlNodeSetContent(node, encoded);
113 xmlFree(encoded);
114 }
115 }
116 }
117
118 void dav_cfg_string_set_value(DavConfig *config, CfgString *str, xmlNode *parent, cxstring new_value, const char *nodename) {
119 if(str->value.ptr) {
120 cxFree(config->mp->allocator, str->value.ptr);
121 }
122 if(new_value.ptr) {
123 str->value = cx_strdup_a(config->mp->allocator, new_value);
124 } else {
125 str->value = cx_mutstrn(NULL, 0);
126 }
127
128 if(!str->node) {
129 str->node = xmlNewNode(NULL, (const xmlChar*) nodename);
130 xmlAddChild(parent, str->node);
131 }
132 set_xml_content(str->node, new_value.ptr);
133 }
134
135 void dav_cfg_bool_set_value(DavConfig *config, CfgBool *cbool, xmlNode *parent, DavBool new_value, const char *nodename) {
136 const char *content = new_value ? "true" : "false";
137 cbool->value = new_value;
138 if(!cbool->node) {
139 cbool->node = xmlNewNode(NULL, (const xmlChar*) nodename);
140 xmlAddChild(parent, cbool->node);
141 }
142 set_xml_content(cbool->node, content);
143 }
144
145 void dav_cfg_int_set_value(DavConfig *config, CfgInt *cint, xmlNode *parent, int64_t new_value, const char *nodename) {
146 char content[32];
147 snprintf(content, 32, "%" PRId64, new_value);
148 cint->value = new_value;
149 if(!cint->node) {
150 cint->node = xmlNewNode(NULL, (const xmlChar*) nodename);
151 xmlAddChild(parent, cint->node);
152 }
153 set_xml_content(cint->node, content);
154 }
155
156 void dav_cfg_uint_set_value(DavConfig *config, CfgUInt *cint, xmlNode *parent, uint64_t new_value, const char *nodename) {
157 char content[32];
158 snprintf(content, 32, "%" PRIu64, new_value);
159 cint->value = new_value;
160 if(!cint->node) {
161 cint->node = xmlNewNode(NULL, (const xmlChar*) nodename);
162 xmlAddChild(parent, cint->node);
163 }
164 set_xml_content(cint->node, content);
165 }
166
167 void dav_cfg_string_remove(CfgString *str) {
168 if(str->node) {
169 xmlUnlinkNode(str->node);
170 xmlFreeNode(str->node);
171 str->node = NULL;
172 }
173 }
174
175 void dav_cfg_bool_remove(CfgBool *cbool) {
176 if(cbool->node) {
177 xmlUnlinkNode(cbool->node);
178 xmlFreeNode(cbool->node);
179 cbool->node = NULL;
180 }
181 }
182
183 void dav_cfg_int_remove(CfgInt *cint) {
184 if(cint->node) {
185 xmlUnlinkNode(cint->node);
186 xmlFreeNode(cint->node);
187 cint->node = NULL;
188 }
189 }
190
191 void dav_cfg_uint_remove(CfgUInt *cint) {
192 if(cint->node) {
193 xmlUnlinkNode(cint->node);
194 xmlFreeNode(cint->node);
195 cint->node = NULL;
196 }
98 } 197 }
99 198
100 199
101 DavConfig* dav_config_new(xmlDoc *doc) { 200 DavConfig* dav_config_new(xmlDoc *doc) {
102 CxMempool *cfg_mp = cxMempoolCreate(128, NULL); 201 CxMempool *cfg_mp = cxMempoolCreate(128, NULL);
210 print_error(lineno, "missing value for config element: %s\n", key); 309 print_error(lineno, "missing value for config element: %s\n", key);
211 return 1; 310 return 1;
212 } 311 }
213 312
214 if(xstreq(key, "name")) { 313 if(xstreq(key, "name")) {
215 dav_cfg_string_set_value(config, &repo->name, node); 314 dav_cfg_string_set_node_value(config, &repo->name, node);
216 } else if(xstreq(key, "url")) { 315 } else if(xstreq(key, "url")) {
217 dav_cfg_string_set_value(config, &repo->url, node); 316 dav_cfg_string_set_node_value(config, &repo->url, node);
218 } else if(xstreq(key, "user")) { 317 } else if(xstreq(key, "user")) {
219 dav_cfg_string_set_value(config, &repo->user, node); 318 dav_cfg_string_set_node_value(config, &repo->user, node);
220 } else if(xstreq(key, "password")) { 319 } else if(xstreq(key, "password")) {
221 dav_cfg_string_set_value(config, &repo->password, node); 320 dav_cfg_string_set_node_value(config, &repo->password, node);
222 } else if(xstreq(key, "stored-user")) { 321 } else if(xstreq(key, "stored-user")) {
223 dav_cfg_string_set_value(config, &repo->stored_user, node); 322 dav_cfg_string_set_node_value(config, &repo->stored_user, node);
224 } else if(xstreq(key, "default-key")) { 323 } else if(xstreq(key, "default-key")) {
225 dav_cfg_string_set_value(config, &repo->default_key, node); 324 dav_cfg_string_set_node_value(config, &repo->default_key, node);
226 } else if(xstreq(key, "full-encryption")) { 325 } else if(xstreq(key, "full-encryption")) {
227 dav_cfg_bool_set_value(config, &repo->full_encryption, node); 326 dav_cfg_bool_set_node_value(config, &repo->full_encryption, node);
228 } else if(xstreq(key, "content-encryption")) { 327 } else if(xstreq(key, "content-encryption")) {
229 dav_cfg_bool_set_value(config, &repo->content_encryption, node); 328 dav_cfg_bool_set_node_value(config, &repo->content_encryption, node);
230 } else if(xstreq(key, "decrypt-content")) { 329 } else if(xstreq(key, "decrypt-content")) {
231 dav_cfg_bool_set_value(config, &repo->decrypt_content, node); 330 dav_cfg_bool_set_node_value(config, &repo->decrypt_content, node);
232 } else if(xstreq(key, "decrypt-name")) { 331 } else if(xstreq(key, "decrypt-name")) {
233 dav_cfg_bool_set_value(config, &repo->decrypt_name, node); 332 dav_cfg_bool_set_node_value(config, &repo->decrypt_name, node);
234 } else if(xstreq(key, "cert")) { 333 } else if(xstreq(key, "cert")) {
235 dav_cfg_string_set_value(config, &repo->cert, node); 334 dav_cfg_string_set_node_value(config, &repo->cert, node);
236 } else if(xstreq(key, "verification")) { 335 } else if(xstreq(key, "verification")) {
237 dav_cfg_bool_set_value(config, &repo->verification, node); 336 dav_cfg_bool_set_node_value(config, &repo->verification, node);
238 } else if(xstreq(key, "ssl-version")) { 337 } else if(xstreq(key, "ssl-version")) {
239 repo->ssl_version.node = node; 338 repo->ssl_version.node = node;
240 if(xstrEQ(value, "TLSv1")) { 339 int ssl_version = dav_str2ssl_version((const char*)value);
241 repo->ssl_version.value = CURL_SSLVERSION_TLSv1; 340 if(ssl_version == -1) {
242 } else if(xstrEQ(value, "SSLv2")) {
243 repo->ssl_version.value = CURL_SSLVERSION_SSLv2;
244 } else if(xstrEQ(value, "SSLv3")) {
245 repo->ssl_version.value = CURL_SSLVERSION_SSLv3;
246 }
247 #if LIBCURL_VERSION_MAJOR * 1000 + LIBCURL_VERSION_MINOR >= 7034
248 else if(xstrEQ(value, "TLSv1.0")) {
249 repo->ssl_version.value = CURL_SSLVERSION_TLSv1_0;
250 } else if(xstrEQ(value, "TLSv1.1")) {
251 repo->ssl_version.value = CURL_SSLVERSION_TLSv1_1;
252 } else if(xstrEQ(value, "TLSv1.2")) {
253 repo->ssl_version.value = CURL_SSLVERSION_TLSv1_2;
254 }
255 #endif
256 #if LIBCURL_VERSION_MAJOR * 1000 + LIBCURL_VERSION_MINOR >= 7052
257 else if(xstrEQ(value, "TLSv1.3")) {
258 repo->ssl_version.value = CURL_SSLVERSION_TLSv1_3;
259 }
260 #endif
261 else {
262 print_warning(lineno, "unknown ssl version: %s\n", value); 341 print_warning(lineno, "unknown ssl version: %s\n", value);
263 repo->ssl_version.value = CURL_SSLVERSION_DEFAULT; 342 repo->ssl_version.value = CURL_SSLVERSION_DEFAULT;
343 } else {
344 repo->ssl_version.value = ssl_version;
264 } 345 }
265 } else if(xstreq(key, "authmethods")) { 346 } else if(xstreq(key, "authmethods")) {
266 repo->authmethods.node = node; 347 repo->authmethods.node = node;
267 repo->authmethods.value = CURLAUTH_NONE; 348 repo->authmethods.value = CURLAUTH_NONE;
268 const char *delims = " \t\r\n"; 349 const char *delims = " \t\r\n";
293 return 1; 374 return 1;
294 } 375 }
295 return 0; 376 return 0;
296 } 377 }
297 378
379 int dav_str2ssl_version(const char *value) {
380 if(xstrEQ(value, "TLSv1")) {
381 return CURL_SSLVERSION_TLSv1;
382 } else if(xstrEQ(value, "SSLv2")) {
383 return CURL_SSLVERSION_SSLv2;
384 } else if(xstrEQ(value, "SSLv3")) {
385 return CURL_SSLVERSION_SSLv3;
386 }
387 #if LIBCURL_VERSION_MAJOR * 1000 + LIBCURL_VERSION_MINOR >= 7034
388 else if(xstrEQ(value, "TLSv1.0")) {
389 return CURL_SSLVERSION_TLSv1_0;
390 } else if(xstrEQ(value, "TLSv1.1")) {
391 return CURL_SSLVERSION_TLSv1_1;
392 } else if(xstrEQ(value, "TLSv1.2")) {
393 return CURL_SSLVERSION_TLSv1_2;
394 }
395 #endif
396 #if LIBCURL_VERSION_MAJOR * 1000 + LIBCURL_VERSION_MINOR >= 7052
397 else if(xstrEQ(value, "TLSv1.3")) {
398 return CURL_SSLVERSION_TLSv1_3;
399 }
400 #endif
401 return -1;
402 }
403
298 static int load_repository( 404 static int load_repository(
299 DavConfig *config, 405 DavConfig *config,
300 DavCfgRepository **list_begin, 406 DavCfgRepository **list_begin,
301 DavCfgRepository **list_end, 407 DavCfgRepository **list_end,
302 xmlNode *reponode) 408 xmlNode *reponode)
518 624
519 int error = 0; 625 int error = 0;
520 while(node) { 626 while(node) {
521 if(node->type == XML_ELEMENT_NODE) { 627 if(node->type == XML_ELEMENT_NODE) {
522 if(xstreq(node->name, "name")) { 628 if(xstreq(node->name, "name")) {
523 dav_cfg_string_set_value(config, &key->name, node); 629 dav_cfg_string_set_node_value(config, &key->name, node);
524 } else if(xstreq(node->name, "file")) { 630 } else if(xstreq(node->name, "file")) {
525 dav_cfg_string_set_value(config, &key->file, node); 631 dav_cfg_string_set_node_value(config, &key->file, node);
526 } else if(xstreq(node->name, "type")) { 632 } else if(xstreq(node->name, "type")) {
527 const char *value = util_xml_get_text(node); 633 const char *value = util_xml_get_text(node);
528 key->type_node = node; 634 key->type_node = node;
529 if(!strcmp(value, "aes128")) { 635 if(!strcmp(value, "aes128")) {
530 key->type = DAV_KEY_TYPE_AES128; 636 key->type = DAV_KEY_TYPE_AES128;
611 int ret = 0; 717 int ret = 0;
612 while(node && !ret) { 718 while(node && !ret) {
613 if(node->type == XML_ELEMENT_NODE) { 719 if(node->type == XML_ELEMENT_NODE) {
614 int reportmissingvalue = 0; 720 int reportmissingvalue = 0;
615 if(xstreq(node->name, "url")) { 721 if(xstreq(node->name, "url")) {
616 reportmissingvalue = dav_cfg_string_set_value(config, &proxy->url, node); 722 reportmissingvalue = dav_cfg_string_set_node_value(config, &proxy->url, node);
617 } else if(xstreq(node->name, "user")) { 723 } else if(xstreq(node->name, "user")) {
618 reportmissingvalue = dav_cfg_string_set_value(config, &proxy->user, node); 724 reportmissingvalue = dav_cfg_string_set_node_value(config, &proxy->user, node);
619 } else if(xstreq(node->name, "password")) { 725 } else if(xstreq(node->name, "password")) {
620 reportmissingvalue = dav_cfg_string_set_value(config, &proxy->password, node); 726 reportmissingvalue = dav_cfg_string_set_node_value(config, &proxy->password, node);
621 } else if(xstreq(node->name, "no")) { 727 } else if(xstreq(node->name, "no")) {
622 reportmissingvalue = dav_cfg_string_set_value(config, &proxy->noproxy, node); 728 reportmissingvalue = dav_cfg_string_set_node_value(config, &proxy->noproxy, node);
623 } else { 729 } else {
624 proxy->unknown_elements++; 730 proxy->unknown_elements++;
625 } 731 }
626 732
627 if (reportmissingvalue) { 733 if (reportmissingvalue) {
722 node = node->children; 828 node = node->children;
723 int error = 0; 829 int error = 0;
724 while(node) { 830 while(node) {
725 if(node->type == XML_ELEMENT_NODE) { 831 if(node->type == XML_ELEMENT_NODE) {
726 if(xstreq(node->name, "unlock-command")) { 832 if(xstreq(node->name, "unlock-command")) {
727 dav_cfg_string_set_value(config, &config->secretstore->unlock_cmd, node); 833 dav_cfg_string_set_node_value(config, &config->secretstore->unlock_cmd, node);
728 } else if(xstreq(node->name, "lock-command")) { 834 } else if(xstreq(node->name, "lock-command")) {
729 dav_cfg_string_set_value(config, &config->secretstore->lock_cmd, node); 835 dav_cfg_string_set_node_value(config, &config->secretstore->lock_cmd, node);
730 } 836 }
731 } 837 }
732 node = node->next; 838 node = node->next;
733 } 839 }
734 840

mercurial