42 #include <libxml/xmlreader.h>
43 #include <libxml/xpath.h>
60 #include "libhsmdns.h"
66 log_msg(NULL, LOG_ERR,
"Error in server_init, no config provided");
72 config->
pidfile = OPENDNSSEC_ENFORCER_PIDFILE;
90 hsm_ctx_t *ctx = NULL;
91 char *hsm_error_message = NULL;
94 char *lock_filename = NULL;
97 log_msg(NULL, LOG_ERR,
"Error in server_main, no config provided");
102 if (policy == NULL) {
103 log_msg(config, LOG_ERR,
"Malloc for policy struct failed");
111 log_msg(config, LOG_ERR,
"Error reading config");
119 result = hsm_open(config->
configfile, hsm_prompt_pin, NULL);
121 result = hsm_open(OPENDNSSEC_CONFIG_FILE, hsm_prompt_pin, NULL);
124 hsm_error_message = hsm_get_error(ctx);
125 if (hsm_error_message) {
126 log_msg(config, LOG_ERR,
"%s", hsm_error_message);
127 free(hsm_error_message);
133 log_msg(config, LOG_ERR,
"hsm_open() result: HSM error");
135 case HSM_PIN_INCORRECT:
136 log_msg(config, LOG_ERR,
"hsm_open() result: incorrect PIN");
138 case HSM_CONFIG_FILE_ERROR:
139 log_msg(config, LOG_ERR,
"hsm_open() result: config file error");
141 case HSM_REPOSITORY_NOT_FOUND:
142 log_msg(config, LOG_ERR,
"hsm_open() result: repository not found");
144 case HSM_NO_REPOSITORIES:
145 log_msg(config, LOG_ERR,
"hsm_open() result: no repositories");
148 log_msg(config, LOG_ERR,
"hsm_open() result: %d", result);
153 log_msg(config, LOG_INFO,
"HSM opened successfully.");
154 ctx = hsm_create_context();
157 log_msg(config, LOG_INFO,
"Checking database connection...");
159 log_msg(config, LOG_ERR,
"Database connection failed");
162 log_msg(config, LOG_INFO,
"Database connection ok.");
166 log_msg(config, LOG_ERR,
"cannot write the pidfile %s: %s",
167 config->
pidfile, strerror(errno));
176 log_msg(config, LOG_ERR,
"Error reading config");
186 lock_filename = NULL;
190 lock_fd = fopen(lock_filename,
"w");
194 log_msg(config, LOG_ERR,
"Error getting db lock");
200 log_msg(config, LOG_INFO,
"Connecting to Database...");
208 while (status == 0) {
209 log_msg(config, LOG_INFO,
"Policy %s found.", policy->name);
217 if (policy->denial->version == 3)
222 log_msg(config, LOG_ERR,
"Error (%d) updating salt for %s", status, policy->name);
235 if (policy->keys->purge != -1) {
236 status =
do_purge(policy->keys->purge, policy->id);
243 log_msg(config, LOG_ERR,
"Error querying KASP DB for policies.");
256 log_msg(config, LOG_INFO,
"Disconnecting from Database...");
263 log_msg(config, LOG_ERR,
"Error releasing db lock");
270 if (config->
once ==
true ){
271 log_msg(config, LOG_INFO,
"Running once only, exiting...");
276 if (config->
term == 1 ){
277 log_msg(config, LOG_INFO,
"Received SIGTERM, exiting...");
281 if (config->
term == 2 ){
282 log_msg(config, LOG_INFO,
"Received SIGINT, exiting...");
289 log_msg(config, LOG_INFO,
"Sleeping for %i seconds.",config->
interval);
290 select(0, NULL, NULL, NULL, &tv);
293 if (config->
term == 1 ){
294 log_msg(config, LOG_INFO,
"Received SIGTERM, exiting...");
298 if (config->
term == 2 ){
299 log_msg(config, LOG_INFO,
"Received SIGINT, exiting...");
313 hsm_destroy_context(ctx);
316 result = hsm_close();
317 log_msg(config, LOG_INFO,
"all done! hsm_close result: %d", result);
321 if (unlink(config->
pidfile) == -1) {
322 log_msg(config, LOG_ERR,
"unlink pidfile %s failed: %s",
338 hsm_key_t *key = NULL;
339 char *hsm_error_message = NULL;
343 int keys_in_queue = 0;
345 unsigned int current_count = 0;
348 int ksks_created = 0;
354 log_msg(config, LOG_INFO,
"Key sharing is On");
356 log_msg(config, LOG_INFO,
"Key sharing is Off.");
362 if (rightnow == NULL) {
363 log_msg(config, LOG_DEBUG,
"Couldn't turn \"now\" into a date, quitting...");
383 if (zone_count == 0) {
384 log_msg(config, LOG_INFO,
"No zones on policy %s, skipping...", policy->
name);
389 log_msg(NULL, LOG_ERR,
"Could not count zones on policy %s", policy->
name);
397 log_msg(NULL, LOG_ERR,
"Could not predict ksk requirement for next interval for %s", policy->
name);
403 log_msg(NULL, LOG_ERR,
"Could not count current ksk numbers for policy %s", policy->
name);
408 keys_in_queue /= zone_count;
411 new_keys = ksks_needed - keys_in_queue;
416 current_count = hsm_count_keys_repository(ctx, policy->
ksk->
sm_name);
418 log_msg(config, LOG_ERR,
"Repository %s is full, cannot create more KSKs for policy %s\n", policy->
ksk->
sm_name, policy->
name);
422 log_msg(config, LOG_WARNING,
"Repository %s is nearly full, will create %lu KSKs for policy %s (reduced from %d)\n", policy->
ksk->
sm_name, policy->
ksk->
sm_capacity - current_count, policy->
name, new_keys);
428 for (i=new_keys ; i > 0 ; i--){
429 if (hsm_supported_algorithm(policy->
ksk->
algorithm) == 0) {
433 log_msg(config, LOG_DEBUG,
"Created key in repository %s", policy->
ksk->
sm_name);
435 log_msg(config, LOG_ERR,
"Error creating key in repository %s", policy->
ksk->
sm_name);
436 hsm_error_message = hsm_get_error(ctx);
437 if (hsm_error_message) {
438 log_msg(config, LOG_ERR,
"%s", hsm_error_message);
439 free(hsm_error_message);
444 id = hsm_get_key_id(ctx, key);
448 log_msg(config, LOG_ERR,
"Error creating key in Database");
449 hsm_error_message = hsm_get_error(ctx);
450 if (hsm_error_message) {
451 log_msg(config, LOG_ERR,
"%s", hsm_error_message);
452 free(hsm_error_message);
457 log_msg(config, LOG_INFO,
"Created KSK size: %i, alg: %i with id: %s in repository: %s and database.", policy->
ksk->
bits,
461 log_msg(config, LOG_ERR,
"Key algorithm %d unsupported by libhsm, exiting...", policy->
ksk->
algorithm);
466 ksks_created = new_keys;
476 log_msg(NULL, LOG_ERR,
"Could not predict zsk requirement for next intervalfor %s", policy->
name);
482 log_msg(NULL, LOG_ERR,
"Could not count current zsk numbers for policy %s", policy->
name);
487 keys_in_queue /= zone_count;
491 keys_in_queue -= ksks_needed;
494 new_keys = zsks_needed - keys_in_queue;
499 current_count = hsm_count_keys_repository(ctx, policy->
zsk->
sm_name);
501 log_msg(config, LOG_ERR,
"Repository %s is full, cannot create more ZSKs for policy %s\n", policy->
zsk->
sm_name, policy->
name);
505 log_msg(config, LOG_WARNING,
"Repository %s is nearly full, will create %lu ZSKs for policy %s (reduced from %d)\n", policy->
zsk->
sm_name, policy->
zsk->
sm_capacity - current_count, policy->
name, new_keys);
511 for (i = new_keys ; i > 0 ; i--) {
512 if (hsm_supported_algorithm(policy->
zsk->
algorithm) == 0) {
516 log_msg(config, LOG_DEBUG,
"Created key in repository %s", policy->
zsk->
sm_name);
518 log_msg(config, LOG_ERR,
"Error creating key in repository %s", policy->
zsk->
sm_name);
519 hsm_error_message = hsm_get_error(ctx);
520 if (hsm_error_message) {
521 log_msg(config, LOG_ERR,
"%s", hsm_error_message);
522 free(hsm_error_message);
528 id = hsm_get_key_id(ctx, key);
532 log_msg(config, LOG_ERR,
"Error creating key in Database");
533 hsm_error_message = hsm_get_error(ctx);
534 if (hsm_error_message) {
535 log_msg(config, LOG_ERR,
"%s", hsm_error_message);
536 free(hsm_error_message);
541 log_msg(config, LOG_INFO,
"Created ZSK size: %i, alg: %i with id: %s in repository: %s and database.", policy->
zsk->
bits,
545 log_msg(config, LOG_ERR,
"Key algorithm %d unsupported by libhsm, exiting...", policy->
zsk->
algorithm);
554 log_msg(config, LOG_INFO,
"NOTE: keys generated in repository %s will not become active until they have been backed up", policy->
ksk->
sm_name);
557 log_msg(config, LOG_INFO,
"NOTE: keys generated in repository %s will not become active until they have been backed up", policy->
zsk->
sm_name);
568 xmlTextReaderPtr reader = NULL;
569 xmlDocPtr doc = NULL;
570 xmlXPathContextPtr xpathCtx = NULL;
571 xmlXPathObjectPtr xpathObj = NULL;
574 char* zonelist_filename = NULL;
576 char* current_policy;
577 char* current_filename;
581 char* ksk_expected = NULL;
583 xmlChar *name_expr = (
unsigned char*)
"name";
584 xmlChar *policy_expr = (
unsigned char*)
"//Zone/Policy";
585 xmlChar *filename_expr = (
unsigned char*)
"//Zone/SignerConfiguration";
587 char* temp_char = NULL;
590 char* datetime = NULL;
601 log_msg(NULL, LOG_ERR,
"couldn't read zonelist filename");
607 reader = xmlNewTextReaderFilename(zonelist_filename);
608 if (reader != NULL) {
609 ret = xmlTextReaderRead(reader);
611 tag_name = (
char*) xmlTextReaderLocalName(reader);
613 if (strncmp(tag_name,
"Zone", 4) == 0
614 && strncmp(tag_name,
"ZoneList", 8) != 0
615 && xmlTextReaderNodeType(reader) == 1) {
618 temp_char = (
char*) xmlTextReaderGetAttribute(reader, name_expr);
622 if (zone_name == NULL) {
624 log_msg(NULL, LOG_ERR,
"Error extracting zone name from %s", zonelist_filename);
626 ret = xmlTextReaderRead(reader);
632 log_msg(config, LOG_INFO,
"Zone %s found.", zone_name);
636 if (status != 0 || zone_id == -1)
639 log_msg(NULL, LOG_ERR,
"Error looking up zone \"%s\" in database (please make sure that the zonelist file is up to date)", zone_name);
641 ret = xmlTextReaderRead(reader);
648 xmlTextReaderExpand(reader);
649 doc = xmlTextReaderCurrentDoc(reader);
651 log_msg(config, LOG_ERR,
"Error: can not read zone \"%s\"; skipping", zone_name);
653 ret = xmlTextReaderRead(reader);
661 xpathCtx = xmlXPathNewContext(doc);
662 if(xpathCtx == NULL) {
663 log_msg(config, LOG_ERR,
"Error: can not create XPath context for \"%s\"; skipping zone", zone_name);
665 ret = xmlTextReaderRead(reader);
673 xpathObj = xmlXPathEvalExpression(policy_expr, xpathCtx);
674 if(xpathObj == NULL) {
675 log_msg(config, LOG_ERR,
"Error: unable to evaluate xpath expression: %s; skipping zone", policy_expr);
677 ret = xmlTextReaderRead(reader);
682 current_policy = NULL;
683 temp_char = (
char*) xmlXPathCastToString(xpathObj);
686 log_msg(config, LOG_INFO,
"Policy for %s set to %s.", zone_name, current_policy);
687 xmlXPathFreeObject(xpathObj);
689 if (strcmp(current_policy, policy->
name) != 0) {
697 log_msg(config, LOG_ERR,
"Error reading policy");
698 ret = xmlTextReaderRead(reader);
703 log_msg(config, LOG_INFO,
"Policy %s found in DB.", policy->
name);
711 xpathObj = xmlXPathEvalExpression(filename_expr, xpathCtx);
712 xmlXPathFreeContext(xpathCtx);
714 if(xpathObj == NULL) {
715 log_msg(config, LOG_ERR,
"Error: unable to evaluate xpath expression: %s; skipping zone", filename_expr);
717 ret = xmlTextReaderRead(reader);
722 current_filename = NULL;
723 temp_char = (
char*)xmlXPathCastToString(xpathObj);
726 log_msg(config, LOG_INFO,
"Config will be output to %s.", current_filename);
727 xmlXPathFreeObject(xpathObj);
732 log_msg(config, LOG_ERR,
"Error allocating zsks to zone %s", zone_name);
734 ret = xmlTextReaderRead(reader);
742 log_msg(config, LOG_ERR,
"Error allocating ksks to zone %s", zone_name);
744 ret = xmlTextReaderRead(reader);
754 log_msg(config, LOG_ERR,
"Signconf not written for %s", zone_name);
756 ret = xmlTextReaderRead(reader);
762 else if (status2 != 0) {
763 log_msg(config, LOG_ERR,
"Error writing signconf for %s", zone_name);
765 ret = xmlTextReaderRead(reader);
777 if (datetime == NULL) {
778 log_msg(config, LOG_DEBUG,
"Couldn't turn \"now\" into a date, quitting...");
786 log_msg(config, LOG_INFO,
"No active KSKs yet for zone %s, can't check for impending rollover", zone_name);
788 else if (status2 != 0) {
789 log_msg(config, LOG_ERR,
"Error checking for impending rollover for %s", zone_name);
792 status2 =
DtDateDiff(ksk_expected, datetime, &roll_time);
794 log_msg(config, LOG_ERR,
"Error checking for impending rollover for %s", zone_name);
797 if (roll_time <= config->rolloverNotify) {
798 log_msg(config, LOG_INFO,
"Rollover of KSK expected at %s for %s", ksk_expected, zone_name);
810 ret = xmlTextReaderRead(reader);
813 xmlFreeTextReader(reader);
815 log_msg(config, LOG_ERR,
"%s : failed to parse", zonelist_filename);
818 log_msg(config, LOG_ERR,
"Unable to open %s", zonelist_filename);
833 int commGenSignConf(
char* zone_name,
int zone_id,
char* current_filename,
KSM_POLICY *policy,
int* signer_flag,
int run_interval,
int man_key_gen,
const char* DSSubmitCmd)
845 char *signer_command;
851 if (datetime == NULL) {
852 log_msg(NULL, LOG_DEBUG,
"Couldn't turn \"now\" into a date, quitting...");
856 if (zone_name == NULL || current_filename == NULL || policy == NULL)
859 log_msg(NULL, LOG_ERR,
"commGenSignConf, NULL policy or zone provided");
865 StrAppend(&old_filename, current_filename);
868 temp_filename = NULL;
869 StrAppend(&temp_filename, current_filename);
872 file = fopen(temp_filename,
"w");
877 log_msg(NULL, LOG_ERR,
"Could not open: %s", temp_filename);
884 fprintf(file,
"<SignerConfiguration>\n");
885 fprintf(file,
"\t<Zone name=\"%s\">\n", zone_name);
887 fprintf(file,
"\t\t<Signatures>\n");
888 fprintf(file,
"\t\t\t<Resign>PT%dS</Resign>\n", policy->
signature->
resign);
889 fprintf(file,
"\t\t\t<Refresh>PT%dS</Refresh>\n", policy->
signer->
refresh);
890 fprintf(file,
"\t\t\t<Validity>\n");
893 fprintf(file,
"\t\t\t</Validity>\n");
894 fprintf(file,
"\t\t\t<Jitter>PT%dS</Jitter>\n", policy->
signer->
jitter);
895 fprintf(file,
"\t\t\t<InceptionOffset>PT%dS</InceptionOffset>\n", policy->
signature->
clockskew);
896 fprintf(file,
"\t\t</Signatures>\n");
900 fprintf(file,
"\t\t<Denial>\n");
903 fprintf(file,
"\t\t\t<NSEC3>\n");
906 fprintf(file,
"\t\t\t\t<OptOut />\n");
908 fprintf(file,
"\t\t\t\t<Hash>\n");
909 fprintf(file,
"\t\t\t\t\t<Algorithm>%d</Algorithm>\n", policy->
denial->
algorithm);
910 fprintf(file,
"\t\t\t\t\t<Iterations>%d</Iterations>\n", policy->
denial->
iteration);
912 fprintf(file,
"\t\t\t\t\t<Salt>-</Salt>\n");
914 fprintf(file,
"\t\t\t\t\t<Salt>%s</Salt>\n", policy->
denial->
salt);
916 fprintf(file,
"\t\t\t\t</Hash>\n");
917 fprintf(file,
"\t\t\t</NSEC3>\n");
919 fprintf(file,
"\t\t\t<NSEC />\n");
922 fprintf(file,
"\t\t</Denial>\n");
927 fprintf(file,
"\t\t<Keys>\n");
928 fprintf(file,
"\t\t\t<TTL>PT%dS</TTL>\n", policy->
ksk->
ttl);
937 log_msg(NULL, LOG_ERR,
"KsmRequestKeys returned: %d", status);
943 if (status2 == 0 && gencnt == 0) {
944 if(man_key_gen == 1) {
945 log_msg(NULL, LOG_ERR,
"There are no KSKs in the generate state; please use \"ods-ksmutil key generate\" to create some.");
947 log_msg(NULL, LOG_WARNING,
"There are no KSKs in the generate state; ods-enforcerd will create some on its next run.");
950 else if (status2 == 0) {
952 if (status2 == 0 && gencnt == 0) {
953 if(man_key_gen == 1) {
954 log_msg(NULL, LOG_ERR,
"There are no ZSKs in the generate state; please use \"ods-ksmutil key generate\" to create some.");
956 log_msg(NULL, LOG_WARNING,
"There are no ZSKs in the generate state; ods-enforcerd will create some on its next run.");
961 log_msg(NULL, LOG_ERR,
"KsmRequestGenerateCount returned: %d", status2);
965 status = fclose(file);
966 unlink(temp_filename);
974 fprintf(file,
"\t\t</Keys>\n");
978 fprintf(file,
"\t\t<SOA>\n");
979 fprintf(file,
"\t\t\t<TTL>PT%dS</TTL>\n", policy->
signer->
soattl);
980 fprintf(file,
"\t\t\t<Minimum>PT%dS</Minimum>\n", policy->
signer->
soamin);
982 fprintf(file,
"\t\t</SOA>\n");
984 if (strncmp(policy->
audit,
"NULL", 4) != 0) {
986 fprintf(file,
"\t\t<Audit />\n");
990 fprintf(file,
"\t</Zone>\n");
991 fprintf(file,
"</SignerConfiguration>\n");
1010 status = fclose(file);
1015 log_msg(NULL, LOG_ERR,
"Could not close: %s", temp_filename);
1022 file = fopen(temp_filename,
"rb");
1026 log_msg(NULL, LOG_ERR,
"Could not reopen: %s", temp_filename);
1032 file2 = fopen(current_filename,
"rb");
1035 if (file2 != NULL) {
1037 while(!feof(file)) {
1038 char1 = fgetc(file);
1040 log_msg(NULL, LOG_ERR,
"Could not read: %s", temp_filename);
1047 char2 = fgetc(file2);
1049 log_msg(NULL, LOG_ERR,
"Could not read: %s", current_filename);
1056 if(char1 != char2) {
1062 status = fclose(file2);
1065 log_msg(NULL, LOG_ERR,
"Could not close: %s", current_filename);
1073 status = fclose(file);
1076 log_msg(NULL, LOG_ERR,
"Could not close: %s", temp_filename);
1087 status = rename(current_filename, old_filename);
1088 if (status != 0 && status != -1)
1091 log_msg(NULL, LOG_ERR,
"Could not rename: %s -> %s", current_filename, old_filename);
1098 if (rename(temp_filename, current_filename) != 0)
1100 log_msg(NULL, LOG_ERR,
"Could not rename: %s -> %s", temp_filename, current_filename);
1106 if (*signer_flag == 1) {
1112 signer_command = NULL;
1113 StrAppend(&signer_command, SIGNER_CLI_UPDATE);
1117 status = system(signer_command);
1120 log_msg(NULL, LOG_ERR,
"Could not call signer engine");
1121 log_msg(NULL, LOG_INFO,
"Will continue: call 'ods-signer update' to manually update zones");
1129 log_msg(NULL, LOG_INFO,
"No change to: %s", current_filename);
1130 if (
remove(temp_filename) != 0)
1132 log_msg(NULL, LOG_ERR,
"Could not remove: %s", temp_filename);
1141 log_msg(NULL, LOG_INFO,
"DSChanged");
1142 status =
NewDSSet(zone_id, zone_name, DSSubmitCmd);
1157 FILE *file = (FILE *)context;
1159 fprintf(file,
"\t\t\t<Key>\n");
1160 fprintf(file,
"\t\t\t\t<Flags>%d</Flags>\n", key_data->
keytype);
1161 fprintf(file,
"\t\t\t\t<Algorithm>%d</Algorithm>\n", key_data->
algorithm);
1162 fprintf(file,
"\t\t\t\t<Locator>%s</Locator>\n", key_data->
location);
1166 fprintf(file,
"\t\t\t\t<KSK />\n");
1170 fprintf(file,
"\t\t\t\t<ZSK />\n");
1174 fprintf(file,
"\t\t\t\t<Publish />\n");
1176 fprintf(file,
"\t\t\t</Key>\n");
1177 fprintf(file,
"\n");
1215 int keys_needed = 0;
1216 int keys_in_queue = 0;
1217 int keys_pending_retirement = 0;
1219 int key_pair_id = 0;
1226 if (datetime == NULL) {
1227 log_msg(NULL, LOG_DEBUG,
"Couldn't turn \"now\" into a date, quitting...");
1231 if (policy == NULL) {
1232 log_msg(NULL, LOG_ERR,
"NULL policy sent to allocateKeysToZone");
1238 log_msg(NULL, LOG_ERR,
"Unknown keytype: %i in allocateKeysToZone", key_type);
1252 status =
KsmKeyPredict(policy->
id, key_type, 1, interval, &keys_needed, rollover_scheme, 1);
1254 log_msg(NULL, LOG_ERR,
"Could not predict key requirement for next interval for %s", zone_name);
1262 log_msg(NULL, LOG_ERR,
"Could not count current key numbers for zone %s", zone_name);
1270 log_msg(NULL, LOG_ERR,
"Could not count keys which may retire before the next run (for zone %s)", zone_name);
1276 new_keys = keys_needed - (keys_in_queue - keys_pending_retirement);
1281 for (i=0 ; i < new_keys ; i++){
1285 if (status == -1 || key_pair_id == 0) {
1286 if (man_key_gen == 0) {
1287 log_msg(NULL, LOG_WARNING,
"Not enough keys to satisfy ksk policy for zone: %s", zone_name);
1288 log_msg(NULL, LOG_WARNING,
"ods-enforcerd will create some more keys on its next run");
1291 log_msg(NULL, LOG_ERR,
"Not enough keys to satisfy ksk policy for zone: %s", zone_name);
1292 log_msg(NULL, LOG_ERR,
"please use \"ods-ksmutil key generate\" to create some more keys.");
1296 else if (status != 0) {
1297 log_msg(NULL, LOG_ERR,
"Could not get an unallocated ksk for zone: %s", zone_name);
1302 if (status == -1 || key_pair_id == 0) {
1303 if (man_key_gen == 0) {
1304 log_msg(NULL, LOG_WARNING,
"Not enough keys to satisfy zsk policy for zone: %s", zone_name);
1305 log_msg(NULL, LOG_WARNING,
"ods-enforcerd will create some more keys on its next run");
1308 log_msg(NULL, LOG_ERR,
"Not enough keys to satisfy zsk policy for zone: %s", zone_name);
1309 log_msg(NULL, LOG_ERR,
"please use \"ods-ksmutil key generate\" to create some more keys.");
1313 else if (status != 0) {
1314 log_msg(NULL, LOG_ERR,
"Could not get an unallocated zsk for zone: %s", zone_name);
1318 if(key_pair_id > 0) {
1323 log_msg(NULL, LOG_ERR,
"KsmKeyGetUnallocated returned bad key_id %d for zone: %s; exiting...", key_pair_id, zone_name);
1337 xmlTextReaderPtr reader = NULL;
1338 xmlDocPtr doc = NULL;
1339 xmlXPathContextPtr xpathCtx = NULL;
1340 xmlXPathObjectPtr xpathObj = NULL;
1342 char* temp_char = NULL;
1343 char* tag_name = NULL;
1345 xmlChar *zonelist_expr = (
unsigned char*)
"//Common/ZoneListFile";
1348 reader = xmlNewTextReaderFilename(filename);
1349 if (reader != NULL) {
1350 ret = xmlTextReaderRead(reader);
1352 tag_name = (
char*) xmlTextReaderLocalName(reader);
1354 if (strncmp(tag_name,
"Common", 6) == 0
1355 && xmlTextReaderNodeType(reader) == 1) {
1358 xmlTextReaderExpand(reader);
1359 doc = xmlTextReaderCurrentDoc(reader);
1361 log_msg(NULL, LOG_ERR,
"Error: can not read Common section of %s", filename);
1363 ret = xmlTextReaderRead(reader);
1367 xpathCtx = xmlXPathNewContext(doc);
1368 if(xpathCtx == NULL) {
1369 log_msg(NULL, LOG_ERR,
"Error: can not create XPath context for Common section");
1371 ret = xmlTextReaderRead(reader);
1376 xpathObj = xmlXPathEvalExpression(zonelist_expr, xpathCtx);
1377 if(xpathObj == NULL) {
1378 log_msg(NULL, LOG_ERR,
"Error: unable to evaluate xpath expression: %s", zonelist_expr);
1380 ret = xmlTextReaderRead(reader);
1383 *zone_list_filename = NULL;
1384 temp_char = (
char *)xmlXPathCastToString(xpathObj);
1385 StrAppend(zone_list_filename, temp_char);
1387 xmlXPathFreeObject(xpathObj);
1388 log_msg(NULL, LOG_INFO,
"zonelist filename set to %s.", *zone_list_filename);
1391 ret = xmlTextReaderRead(reader);
1394 xmlFreeTextReader(reader);
1396 log_msg(NULL, LOG_ERR,
"%s : failed to parse", filename);
1400 log_msg(NULL, LOG_ERR,
"Unable to open %s", filename);
1404 xmlXPathFreeContext(xpathCtx);
1446 char* temp_loc = NULL;
1452 hsm_key_t *key = NULL;
1454 log_msg(NULL, LOG_DEBUG,
"Purging keys...");
1459 if (rightnow == NULL) {
1460 log_msg(NULL, LOG_DEBUG,
"Couldn't turn \"now\" into a date, quitting...");
1465 StrAppend(&sql,
"select distinct id, location from KEYDATA_VIEW where state = 6 ");
1467 if (policy_id != -1) {
1479 while (status == 0) {
1481 DbInt(row, 0, &temp_id);
1489 nchar = snprintf(buffer,
sizeof(buffer),
1490 " or state = %d and DEAD > DATE_ADD('%s', INTERVAL -%d SECOND)) ",
KSM_STATE_DEAD, rightnow, interval);
1492 nchar = snprintf(buffer,
sizeof(buffer),
1493 " or state = %d and DEAD > DATETIME('%s', '-%d SECONDS')) ",
KSM_STATE_DEAD, rightnow, interval);
1546 key = hsm_find_key_by_id(NULL, temp_loc);
1549 log_msg(NULL, LOG_ERR,
"Key not found: %s\n", temp_loc);
1556 status = hsm_remove_key(NULL, key);
1561 log_msg(NULL, LOG_INFO,
"Key remove successful.\n");
1563 log_msg(NULL, LOG_ERR,
"Key remove failed.\n");
1593 int NewDSSet(
int zone_id,
const char* zone_name,
const char* DSSubmitCmd) {
1607 char* count_clause = NULL;
1608 char* where_clause = NULL;
1610 int active_count = -1;
1615 char* ds_buffer = NULL;
1616 char* ds_seen_buffer = NULL;
1617 char* temp_char = NULL;
1620 hsm_key_t *key = NULL;
1621 ldns_rr *dnskey_rr = NULL;
1622 hsm_sign_params_t *sign_params = NULL;
1625 int bytes_written = -1;
1627 nchar = snprintf(buffer,
sizeof(buffer),
"(%d, %d, %d, %d, %d, %d, %d, %d)",
1631 if (nchar >=
sizeof(buffer)) {
1642 if (zone_id != -1) {
1650 log_msg(NULL, LOG_ERR,
"Error: failed to find ID of key to retire\n");
1654 if (active_count > 0) {
1657 StrAppend(&where_clause,
"select id from KEYDATA_VIEW where state = 4 and keytype = 257 and zone_id = ");
1659 StrAppend(&where_clause,
" and retire = (select min(retire) from KEYDATA_VIEW where state = 4 and keytype = 257 and zone_id = ");
1668 log_msg(NULL, LOG_ERR,
"Error: failed to find ID of key to retire\n");
1678 if (zone_id != -1) {
1700 keyids =
MemMalloc(count *
sizeof(
int));
1708 if (zone_id != -1) {
1720 while (status == 0) {
1721 status =
KsmKey(result, &data);
1752 for (j = 0; j < i; ++j) {
1756 snprintf(buffer,
sizeof(buffer),
"%d", keyids[j]);
1769 log_msg(NULL, LOG_INFO,
"DS Record set has changed, the current set looks like:");
1774 status =
KsmKey(result3, &data3);
1775 while (status == 0) {
1778 key = hsm_find_key_by_id(NULL, data3.
location);
1781 log_msg(NULL, LOG_ERR,
"Key %s in DB but not repository.", data3.
location);
1789 sign_params = hsm_sign_params_new();
1790 sign_params->owner = ldns_rdf_new_frm_str(LDNS_RDF_TYPE_DNAME, zone_name);
1791 sign_params->algorithm = data3.
algorithm;
1792 sign_params->flags = LDNS_KEY_ZONE_KEY;
1793 sign_params->flags += LDNS_KEY_SEP_KEY;
1794 dnskey_rr = hsm_get_dnskey(NULL, key, sign_params);
1796 temp_char = ldns_rr2str(dnskey_rr);
1797 ldns_rr_free(dnskey_rr);
1800 for (i = 0; temp_char[i]; ++i) {
1801 if (temp_char[i] ==
'\t') {
1805 log_msg(NULL, LOG_INFO,
"%s", temp_char);
1809 for (i = 0; temp_char[i]; ++i) {
1810 if (temp_char[i] ==
';') {
1811 temp_char[i] =
'\n';
1812 temp_char[i+1] =
'\0';
1832 hsm_sign_params_free(sign_params);
1834 status =
KsmKey(result3, &data3);
1844 if (DSSubmitCmd[0] !=
'\0') {
1846 fp = popen(DSSubmitCmd,
"w");
1848 log_msg(NULL, LOG_ERR,
"Failed to run command: %s: %s", DSSubmitCmd, strerror(errno));
1851 bytes_written = fprintf(fp,
"%s", ds_buffer);
1852 if (bytes_written < 0) {
1853 log_msg(NULL, LOG_ERR,
"Failed to write to %s: %s", DSSubmitCmd, strerror(errno));
1857 if (pclose(fp) == -1) {
1858 log_msg(NULL, LOG_ERR,
"Failed to close %s: %s", DSSubmitCmd, strerror(errno));
1865 log_msg(NULL, LOG_INFO,
"Once the new DS records are seen in DNS please issue the ds-seen command for zone %s with the following cka_ids%s", zone_name, ds_seen_buffer);
1877 char *hsm_error_message = NULL;
1879 result = hsm_check_context(*ctx);
1882 if (result != HSM_OK) {
1885 hsm_destroy_context(*ctx);
1888 result = hsm_close();
1891 result = hsm_open(config->
configfile, hsm_prompt_pin, NULL);
1893 result = hsm_open(OPENDNSSEC_CONFIG_FILE, hsm_prompt_pin, NULL);
1896 hsm_error_message = hsm_get_error(*ctx);
1897 if (hsm_error_message) {
1898 log_msg(config, LOG_ERR, hsm_error_message);
1899 free(hsm_error_message);
1906 log_msg(config, LOG_ERR,
"hsm_open() result: HSM error");
1908 case HSM_PIN_INCORRECT:
1909 log_msg(config, LOG_ERR,
"hsm_open() result: incorrect PIN");
1911 case HSM_CONFIG_FILE_ERROR:
1912 log_msg(config, LOG_ERR,
"hsm_open() result: config file error");
1914 case HSM_REPOSITORY_NOT_FOUND:
1915 log_msg(config, LOG_ERR,
"hsm_open() result: repository not found");
1917 case HSM_NO_REPOSITORIES:
1918 log_msg(config, LOG_ERR,
"hsm_open() result: no repositories");
1921 log_msg(config, LOG_ERR,
"hsm_open() result: %d", result);
1927 log_msg(config, LOG_INFO,
"HSM reopened successfully.");
1928 *ctx = hsm_create_context();
1930 log_msg(config, LOG_INFO,
"HSM connection open.");