OpenDNSSEC-signer
1.3.8
|
00001 /* 00002 * $Id: signconfparser.c 5809 2011-10-25 11:12:50Z matthijs $ 00003 * 00004 * Copyright (c) 2009 NLNet Labs. All rights reserved. 00005 * 00006 * Redistribution and use in source and binary forms, with or without 00007 * modification, are permitted provided that the following conditions 00008 * are met: 00009 * 1. Redistributions of source code must retain the above copyright 00010 * notice, this list of conditions and the following disclaimer. 00011 * 2. Redistributions in binary form must reproduce the above copyright 00012 * notice, this list of conditions and the following disclaimer in the 00013 * documentation and/or other materials provided with the distribution. 00014 * 00015 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 00016 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 00017 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00018 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY 00019 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00020 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 00021 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00022 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 00023 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 00024 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN 00025 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00026 * 00027 */ 00028 00034 #include "parser/confparser.h" 00035 #include "parser/signconfparser.h" 00036 #include "shared/allocator.h" 00037 #include "shared/duration.h" 00038 #include "shared/log.h" 00039 #include "signer/keys.h" 00040 00041 #include <libxml/parser.h> 00042 #include <libxml/xpath.h> 00043 #include <libxml/xpathInternals.h> 00044 #include <libxml/xmlreader.h> 00045 #include <stdlib.h> 00046 00047 static const char* parser_str = "parser"; 00048 00049 00054 keylist_type* 00055 parse_sc_keys(allocator_type* allocator, const char* cfgfile) 00056 { 00057 xmlDocPtr doc = NULL; 00058 xmlXPathContextPtr xpathCtx = NULL; 00059 xmlXPathObjectPtr xpathObj = NULL; 00060 xmlNode* curNode = NULL; 00061 xmlChar* xexpr = NULL; 00062 key_type* new_key = NULL; 00063 keylist_type* kl = NULL; 00064 char* locator = NULL; 00065 char* flags = NULL; 00066 char* algorithm = NULL; 00067 int ksk, zsk, publish, i; 00068 00069 if (!cfgfile) { 00070 ods_log_error("[%s] could not parse <Keys>, no cfgfile given", 00071 parser_str); 00072 return NULL; 00073 } 00074 ods_log_assert(cfgfile); 00075 00076 /* Load XML document */ 00077 doc = xmlParseFile(cfgfile); 00078 if (doc == NULL) { 00079 ods_log_error("[%s] could not parse <Keys>, xmlParseFile failed", 00080 parser_str); 00081 return NULL; 00082 } 00083 /* Create xpath evaluation context */ 00084 xpathCtx = xmlXPathNewContext(doc); 00085 if(xpathCtx == NULL) { 00086 xmlFreeDoc(doc); 00087 ods_log_error("[%s] could not parse <Keys>, xmlXPathNewContext failed", 00088 parser_str); 00089 return NULL; 00090 } 00091 /* Evaluate xpath expression */ 00092 xexpr = (xmlChar*) "//SignerConfiguration/Zone/Keys/Key"; 00093 xpathObj = xmlXPathEvalExpression(xexpr, xpathCtx); 00094 if(xpathObj == NULL) { 00095 xmlXPathFreeContext(xpathCtx); 00096 xmlFreeDoc(doc); 00097 ods_log_error("[%s] could not parse <Keys>, xmlXPathEvalExpression " 00098 "failed", parser_str); 00099 return NULL; 00100 } 00101 00102 kl = keylist_create(allocator); 00103 if (xpathObj->nodesetval && xpathObj->nodesetval->nodeNr > 0) { 00104 for (i = 0; i < xpathObj->nodesetval->nodeNr; i++) { 00105 locator = NULL; 00106 flags = NULL; 00107 algorithm = NULL; 00108 ksk = 0; 00109 zsk = 0; 00110 publish = 0; 00111 00112 curNode = xpathObj->nodesetval->nodeTab[i]->xmlChildrenNode; 00113 while (curNode) { 00114 if (xmlStrEqual(curNode->name, (const xmlChar *)"Locator")) { 00115 locator = (char *) xmlNodeGetContent(curNode); 00116 } else if (xmlStrEqual(curNode->name, (const xmlChar *)"Algorithm")) { 00117 algorithm = (char *) xmlNodeGetContent(curNode); 00118 } else if (xmlStrEqual(curNode->name, (const xmlChar *)"Flags")) { 00119 flags = (char *) xmlNodeGetContent(curNode); 00120 } else if (xmlStrEqual(curNode->name, (const xmlChar *)"KSK")) { 00121 ksk = 1; 00122 } else if (xmlStrEqual(curNode->name, (const xmlChar *)"ZSK")) { 00123 zsk = 1; 00124 } else if (xmlStrEqual(curNode->name, (const xmlChar *)"Publish")) { 00125 publish = 1; 00126 } 00127 curNode = curNode->next; 00128 } 00129 if (locator && algorithm && flags) { 00130 /* search for duplicates */ 00131 new_key = keylist_lookup(kl, locator); 00132 if (new_key && 00133 new_key->algorithm == (uint8_t) atoi(algorithm) && 00134 new_key->flags == (uint32_t) atoi(flags) && 00135 new_key->publish == publish && 00136 new_key->ksk == ksk && 00137 new_key->zsk == zsk) { 00138 /* duplicate */ 00139 ods_log_warning("[%s] found duplicate key %s, skipping", 00140 parser_str, locator); 00141 } else { 00142 new_key = key_create(allocator, locator, 00143 (uint8_t) atoi(algorithm), (uint32_t) atoi(flags), 00144 publish, ksk, zsk); 00145 if (keylist_push(kl, new_key) != ODS_STATUS_OK) { 00146 ods_log_error("[%s] failed to push key %s to keylist", 00147 parser_str, locator); 00148 } 00149 } 00150 } else { 00151 ods_log_error("[%s] Key missing required elements, skipping", 00152 parser_str); 00153 } 00154 free((void*)locator); 00155 free((void*)algorithm); 00156 free((void*)flags); 00157 } 00158 } 00159 00160 xmlXPathFreeObject(xpathObj); 00161 xmlXPathFreeContext(xpathCtx); 00162 if (doc) { 00163 xmlFreeDoc(doc); 00164 } 00165 return kl; 00166 } 00167 00168 00173 duration_type* 00174 parse_sc_sig_resign_interval(const char* cfgfile) 00175 { 00176 duration_type* duration = NULL; 00177 const char* str = parse_conf_string(cfgfile, 00178 "//SignerConfiguration/Zone/Signatures/Resign", 00179 1); 00180 if (!str) { 00181 return NULL; 00182 } 00183 duration = duration_create_from_string(str); 00184 free((void*)str); 00185 return duration; 00186 } 00187 00188 00189 duration_type* 00190 parse_sc_sig_refresh_interval(const char* cfgfile) 00191 { 00192 duration_type* duration = NULL; 00193 const char* str = parse_conf_string(cfgfile, 00194 "//SignerConfiguration/Zone/Signatures/Refresh", 00195 1); 00196 if (!str) { 00197 return NULL; 00198 } 00199 duration = duration_create_from_string(str); 00200 free((void*)str); 00201 return duration; 00202 } 00203 00204 00205 duration_type* 00206 parse_sc_sig_validity_default(const char* cfgfile) 00207 { 00208 duration_type* duration = NULL; 00209 const char* str = parse_conf_string(cfgfile, 00210 "//SignerConfiguration/Zone/Signatures/Validity/Default", 00211 1); 00212 if (!str) { 00213 return NULL; 00214 } 00215 duration = duration_create_from_string(str); 00216 free((void*)str); 00217 return duration; 00218 } 00219 00220 00221 duration_type* 00222 parse_sc_sig_validity_denial(const char* cfgfile) 00223 { 00224 duration_type* duration = NULL; 00225 const char* str = parse_conf_string(cfgfile, 00226 "//SignerConfiguration/Zone/Signatures/Validity/Denial", 00227 1); 00228 if (!str) { 00229 return NULL; 00230 } 00231 duration = duration_create_from_string(str); 00232 free((void*)str); 00233 return duration; 00234 } 00235 00236 00237 duration_type* 00238 parse_sc_sig_jitter(const char* cfgfile) 00239 { 00240 duration_type* duration = NULL; 00241 const char* str = parse_conf_string(cfgfile, 00242 "//SignerConfiguration/Zone/Signatures/Jitter", 00243 1); 00244 if (!str) { 00245 return NULL; 00246 } 00247 duration = duration_create_from_string(str); 00248 free((void*)str); 00249 return duration; 00250 } 00251 00252 00253 duration_type* 00254 parse_sc_sig_inception_offset(const char* cfgfile) 00255 { 00256 duration_type* duration = NULL; 00257 const char* str = parse_conf_string(cfgfile, 00258 "//SignerConfiguration/Zone/Signatures/InceptionOffset", 00259 1); 00260 if (!str) { 00261 return NULL; 00262 } 00263 duration = duration_create_from_string(str); 00264 free((void*)str); 00265 return duration; 00266 } 00267 00268 00269 duration_type* 00270 parse_sc_dnskey_ttl(const char* cfgfile) 00271 { 00272 duration_type* duration = NULL; 00273 const char* str = parse_conf_string(cfgfile, 00274 "//SignerConfiguration/Zone/Keys/TTL", 00275 1); 00276 if (!str) { 00277 return NULL; 00278 } 00279 duration = duration_create_from_string(str); 00280 free((void*)str); 00281 return duration; 00282 } 00283 00284 00285 duration_type* 00286 parse_sc_soa_ttl(const char* cfgfile) 00287 { 00288 duration_type* duration = NULL; 00289 const char* str = parse_conf_string(cfgfile, 00290 "//SignerConfiguration/Zone/SOA/TTL", 00291 1); 00292 if (!str) { 00293 return NULL; 00294 } 00295 duration = duration_create_from_string(str); 00296 free((void*)str); 00297 return duration; 00298 } 00299 00300 00301 duration_type* 00302 parse_sc_soa_min(const char* cfgfile) 00303 { 00304 duration_type* duration = NULL; 00305 const char* str = parse_conf_string(cfgfile, 00306 "//SignerConfiguration/Zone/SOA/Minimum", 00307 1); 00308 if (!str) { 00309 return NULL; 00310 } 00311 duration = duration_create_from_string(str); 00312 free((void*)str); 00313 return duration; 00314 } 00315 00316 00321 ldns_rr_type 00322 parse_sc_nsec_type(const char* cfgfile) 00323 { 00324 const char* str = parse_conf_string(cfgfile, 00325 "//SignerConfiguration/Zone/Denial/NSEC3", 00326 0); 00327 if (str) { 00328 free((void*)str); 00329 return LDNS_RR_TYPE_NSEC3; 00330 } 00331 00332 str = parse_conf_string(cfgfile, 00333 "//SignerConfiguration/Zone/Denial/NSEC", 00334 0); 00335 if (str) { 00336 free((void*)str); 00337 return LDNS_RR_TYPE_NSEC; 00338 } 00339 00340 return LDNS_RR_TYPE_FIRST; 00341 } 00342 00343 00348 uint32_t 00349 parse_sc_nsec3_algorithm(const char* cfgfile) 00350 { 00351 int ret = 0; 00352 const char* str = parse_conf_string(cfgfile, 00353 "//SignerConfiguration/Zone/Denial/NSEC3/Hash/Algorithm", 00354 1); 00355 if (str) { 00356 if (strlen(str) > 0) { 00357 ret = atoi(str); 00358 } 00359 free((void*)str); 00360 } 00361 return ret; 00362 } 00363 00364 00365 uint32_t 00366 parse_sc_nsec3_iterations(const char* cfgfile) 00367 { 00368 int ret = 0; 00369 const char* str = parse_conf_string(cfgfile, 00370 "//SignerConfiguration/Zone/Denial/NSEC3/Hash/Iterations", 00371 1); 00372 if (str) { 00373 if (strlen(str) > 0) { 00374 ret = atoi(str); 00375 } 00376 free((void*)str); 00377 } 00378 return ret; 00379 } 00380 00381 00382 int 00383 parse_sc_nsec3_optout(const char* cfgfile) 00384 { 00385 int ret = 0; 00386 const char* str = parse_conf_string(cfgfile, 00387 "//SignerConfiguration/Zone/Denial/NSEC3/OptOut", 00388 0); 00389 if (str) { 00390 ret = 1; 00391 free((void*)str); 00392 } 00393 return ret; 00394 } 00395 00396 00397 int 00398 parse_sc_audit(const char* cfgfile) 00399 { 00400 int ret = 0; 00401 const char* str = parse_conf_string(cfgfile, 00402 "//SignerConfiguration/Zone/Audit", 00403 0); 00404 if (str) { 00405 ret = 1; 00406 free((void*)str); 00407 } 00408 return ret; 00409 } 00410 00411 00416 const char* 00417 parse_sc_soa_serial(allocator_type* allocator, const char* cfgfile) 00418 { 00419 const char* dup = NULL; 00420 const char* str = parse_conf_string( 00421 cfgfile, 00422 "//SignerConfiguration/Zone/SOA/Serial", 00423 1); 00424 00425 if (str) { 00426 dup = allocator_strdup(allocator, str); 00427 free((void*)str); 00428 } 00429 return dup; 00430 } 00431 00432 00433 const char* 00434 parse_sc_nsec3_salt(allocator_type* allocator, const char* cfgfile) 00435 { 00436 const char* dup = NULL; 00437 const char* str = parse_conf_string( 00438 cfgfile, 00439 "//SignerConfiguration/Zone/Denial/NSEC3/Hash/Salt", 00440 1); 00441 00442 if (str) { 00443 dup = allocator_strdup(allocator, str); 00444 free((void*)str); 00445 } 00446 return dup; 00447 }