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...");
204 if (config->
policy != NULL) {
205 log_msg(config, LOG_INFO,
"Will only process policy \"%s\" as specified on the command line with the --policy option.", config->
policy);
208 log_msg(config, LOG_ERR,
"Policy \"%s\" not found. Exiting.", config->
policy);
219 while (status == 0) {
220 log_msg(config, LOG_INFO,
"Policy %s found.", policy->name);
228 if (policy->denial->version == 3)
233 log_msg(config, LOG_ERR,
"Error (%d) updating salt for %s", status, policy->name);
246 if (policy->keys->purge != -1) {
247 status =
do_purge(policy->keys->purge, policy->id);
254 log_msg(config, LOG_ERR,
"Error querying KASP DB for policies.");
270 log_msg(config, LOG_INFO,
"Disconnecting from Database...");
277 log_msg(config, LOG_ERR,
"Error releasing db lock");
284 if (config->
once ==
true ){
285 log_msg(config, LOG_INFO,
"Running once only, exiting...");
290 if (config->
term == 1 ){
291 log_msg(config, LOG_INFO,
"Received SIGTERM, exiting...");
295 if (config->
term == 2 ){
296 log_msg(config, LOG_INFO,
"Received SIGINT, exiting...");
303 log_msg(config, LOG_INFO,
"Sleeping for %i seconds.",config->
interval);
304 select(0, NULL, NULL, NULL, &tv);
307 if (config->
term == 1 ){
308 log_msg(config, LOG_INFO,
"Received SIGTERM, exiting...");
312 if (config->
term == 2 ){
313 log_msg(config, LOG_INFO,
"Received SIGINT, exiting...");
327 hsm_destroy_context(ctx);
330 result = hsm_close();
331 log_msg(config, LOG_INFO,
"all done! hsm_close result: %d", result);
335 if (unlink(config->
pidfile) == -1) {
336 log_msg(config, LOG_ERR,
"unlink pidfile %s failed: %s",
352 hsm_key_t *key = NULL;
353 char *hsm_error_message = NULL;
357 int keys_in_queue = 0;
359 unsigned int current_count = 0;
362 int ksks_created = 0;
368 log_msg(config, LOG_INFO,
"Key sharing is On");
370 log_msg(config, LOG_INFO,
"Key sharing is Off.");
376 if (rightnow == NULL) {
377 log_msg(config, LOG_DEBUG,
"Couldn't turn \"now\" into a date, quitting...");
397 if (zone_count == 0) {
398 log_msg(config, LOG_INFO,
"No zones on policy %s, skipping...", policy->
name);
403 log_msg(NULL, LOG_ERR,
"Could not count zones on policy %s", policy->
name);
407 log_msg(config, LOG_INFO,
"%d zone(s) found on policy \"%s\"\n", zone_count, policy->
name);
412 log_msg(NULL, LOG_ERR,
"Could not predict ksk requirement for next interval for %s", policy->
name);
418 log_msg(NULL, LOG_ERR,
"Could not count current ksk numbers for policy %s", policy->
name);
423 new_keys = ksks_needed - keys_in_queue;
428 current_count = hsm_count_keys_repository(ctx, policy->
ksk->
sm_name);
430 log_msg(config, LOG_ERR,
"Repository %s is full, cannot create more KSKs for policy %s\n", policy->
ksk->
sm_name, policy->
name);
434 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);
438 if (new_keys <= 0 ) {
439 log_msg(config, LOG_INFO,
"No new KSKs need to be created.\n");
442 log_msg(config, LOG_INFO,
"%d new KSK(s) (%d bits) need to be created.\n", new_keys, policy->
ksk->
bits);
446 for (i=new_keys ; i > 0 ; i--){
447 if (hsm_supported_algorithm(policy->
ksk->
algorithm) == 0) {
451 log_msg(config, LOG_DEBUG,
"Created key in repository %s", policy->
ksk->
sm_name);
453 log_msg(config, LOG_ERR,
"Error creating key in repository %s", policy->
ksk->
sm_name);
454 hsm_error_message = hsm_get_error(ctx);
455 if (hsm_error_message) {
456 log_msg(config, LOG_ERR,
"%s", hsm_error_message);
457 free(hsm_error_message);
462 id = hsm_get_key_id(ctx, key);
466 log_msg(config, LOG_ERR,
"Error creating key in Database");
467 hsm_error_message = hsm_get_error(ctx);
468 if (hsm_error_message) {
469 log_msg(config, LOG_ERR,
"%s", hsm_error_message);
470 free(hsm_error_message);
475 log_msg(config, LOG_INFO,
"Created KSK size: %i, alg: %i with id: %s in repository: %s and database.", policy->
ksk->
bits,
479 log_msg(config, LOG_ERR,
"Key algorithm %d unsupported by libhsm, exiting...", policy->
ksk->
algorithm);
484 ksks_created = new_keys;
494 log_msg(NULL, LOG_ERR,
"Could not predict zsk requirement for next intervalfor %s", policy->
name);
500 log_msg(NULL, LOG_ERR,
"Could not count current zsk numbers for policy %s", policy->
name);
506 keys_in_queue -= ksks_needed;
509 new_keys = zsks_needed - keys_in_queue;
514 current_count = hsm_count_keys_repository(ctx, policy->
zsk->
sm_name);
516 log_msg(config, LOG_ERR,
"Repository %s is full, cannot create more ZSKs for policy %s\n", policy->
zsk->
sm_name, policy->
name);
520 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);
525 if (new_keys <= 0 ) {
527 log_msg(config, LOG_INFO,
"No new ZSKs need to be created.\n");
530 log_msg(config, LOG_INFO,
"%d new ZSK(s) (%d bits) need to be created.\n", new_keys, policy->
zsk->
bits);
534 for (i = new_keys ; i > 0 ; i--) {
535 if (hsm_supported_algorithm(policy->
zsk->
algorithm) == 0) {
539 log_msg(config, LOG_DEBUG,
"Created key in repository %s", policy->
zsk->
sm_name);
541 log_msg(config, LOG_ERR,
"Error creating key in repository %s", policy->
zsk->
sm_name);
542 hsm_error_message = hsm_get_error(ctx);
543 if (hsm_error_message) {
544 log_msg(config, LOG_ERR,
"%s", hsm_error_message);
545 free(hsm_error_message);
551 id = hsm_get_key_id(ctx, key);
555 log_msg(config, LOG_ERR,
"Error creating key in Database");
556 hsm_error_message = hsm_get_error(ctx);
557 if (hsm_error_message) {
558 log_msg(config, LOG_ERR,
"%s", hsm_error_message);
559 free(hsm_error_message);
564 log_msg(config, LOG_INFO,
"Created ZSK size: %i, alg: %i with id: %s in repository: %s and database.", policy->
zsk->
bits,
568 log_msg(config, LOG_ERR,
"Key algorithm %d unsupported by libhsm, exiting...", policy->
zsk->
algorithm);
577 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);
580 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);
591 xmlTextReaderPtr reader = NULL;
592 xmlDocPtr doc = NULL;
593 xmlXPathContextPtr xpathCtx = NULL;
594 xmlXPathObjectPtr xpathObj = NULL;
597 char* zonelist_filename = NULL;
599 char* current_policy;
600 char* current_filename;
604 char* ksk_expected = NULL;
606 xmlChar *name_expr = (
unsigned char*)
"name";
607 xmlChar *policy_expr = (
unsigned char*)
"//Zone/Policy";
608 xmlChar *filename_expr = (
unsigned char*)
"//Zone/SignerConfiguration";
610 char* temp_char = NULL;
613 char* datetime = NULL;
624 log_msg(NULL, LOG_ERR,
"couldn't read zonelist filename");
630 reader = xmlNewTextReaderFilename(zonelist_filename);
631 if (reader != NULL) {
632 ret = xmlTextReaderRead(reader);
634 tag_name = (
char*) xmlTextReaderLocalName(reader);
636 if (strncmp(tag_name,
"Zone", 4) == 0
637 && strncmp(tag_name,
"ZoneList", 8) != 0
638 && xmlTextReaderNodeType(reader) == 1) {
641 temp_char = (
char*) xmlTextReaderGetAttribute(reader, name_expr);
645 if (zone_name == NULL) {
647 log_msg(NULL, LOG_ERR,
"Error extracting zone name from %s", zonelist_filename);
649 ret = xmlTextReaderRead(reader);
655 log_msg(config, LOG_INFO,
"Zone %s found.", zone_name);
659 if (status != 0 || zone_id == -1)
662 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);
664 ret = xmlTextReaderRead(reader);
671 xmlTextReaderExpand(reader);
672 doc = xmlTextReaderCurrentDoc(reader);
674 log_msg(config, LOG_ERR,
"Error: can not read zone \"%s\"; skipping", zone_name);
676 ret = xmlTextReaderRead(reader);
684 xpathCtx = xmlXPathNewContext(doc);
685 if(xpathCtx == NULL) {
686 log_msg(config, LOG_ERR,
"Error: can not create XPath context for \"%s\"; skipping zone", zone_name);
688 ret = xmlTextReaderRead(reader);
696 xpathObj = xmlXPathEvalExpression(policy_expr, xpathCtx);
697 if(xpathObj == NULL) {
698 log_msg(config, LOG_ERR,
"Error: unable to evaluate xpath expression: %s; skipping zone", policy_expr);
700 ret = xmlTextReaderRead(reader);
705 current_policy = NULL;
706 temp_char = (
char*) xmlXPathCastToString(xpathObj);
709 log_msg(config, LOG_INFO,
"Policy for %s set to %s.", zone_name, current_policy);
710 xmlXPathFreeObject(xpathObj);
712 if (strcmp(current_policy, policy->
name) != 0) {
713 if ( !all_policies ) {
715 log_msg(config, LOG_INFO,
"Skipping zone %s as not on specified policy \"%s\".", zone_name, policy->
name);
717 ret = xmlTextReaderRead(reader);
729 log_msg(config, LOG_ERR,
"Error reading policy");
730 ret = xmlTextReaderRead(reader);
735 log_msg(config, LOG_INFO,
"Policy %s found in DB.", policy->
name);
743 xpathObj = xmlXPathEvalExpression(filename_expr, xpathCtx);
744 xmlXPathFreeContext(xpathCtx);
746 if(xpathObj == NULL) {
747 log_msg(config, LOG_ERR,
"Error: unable to evaluate xpath expression: %s; skipping zone", filename_expr);
749 ret = xmlTextReaderRead(reader);
754 current_filename = NULL;
755 temp_char = (
char*)xmlXPathCastToString(xpathObj);
758 log_msg(config, LOG_INFO,
"Config will be output to %s.", current_filename);
759 xmlXPathFreeObject(xpathObj);
764 log_msg(config, LOG_ERR,
"Error allocating zsks to zone %s", zone_name);
766 ret = xmlTextReaderRead(reader);
774 log_msg(config, LOG_ERR,
"Error allocating ksks to zone %s", zone_name);
776 ret = xmlTextReaderRead(reader);
786 log_msg(config, LOG_ERR,
"Signconf not written for %s", zone_name);
788 ret = xmlTextReaderRead(reader);
794 else if (status2 != 0) {
795 log_msg(config, LOG_ERR,
"Error writing signconf for %s", zone_name);
797 ret = xmlTextReaderRead(reader);
809 if (datetime == NULL) {
810 log_msg(config, LOG_DEBUG,
"Couldn't turn \"now\" into a date, quitting...");
818 log_msg(config, LOG_INFO,
"No active KSKs yet for zone %s, can't check for impending rollover", zone_name);
820 else if (status2 != 0) {
821 log_msg(config, LOG_ERR,
"Error checking for impending rollover for %s", zone_name);
824 status2 =
DtDateDiff(ksk_expected, datetime, &roll_time);
826 log_msg(config, LOG_ERR,
"Error checking for impending rollover for %s", zone_name);
829 if (roll_time <= config->rolloverNotify) {
830 log_msg(config, LOG_INFO,
"Rollover of KSK expected at %s for %s", ksk_expected, zone_name);
842 ret = xmlTextReaderRead(reader);
845 xmlFreeTextReader(reader);
847 log_msg(config, LOG_ERR,
"%s : failed to parse", zonelist_filename);
850 log_msg(config, LOG_ERR,
"Unable to open %s", zonelist_filename);
865 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)
877 char *signer_command;
883 if (datetime == NULL) {
884 log_msg(NULL, LOG_DEBUG,
"Couldn't turn \"now\" into a date, quitting...");
888 if (zone_name == NULL || current_filename == NULL || policy == NULL)
891 log_msg(NULL, LOG_ERR,
"commGenSignConf, NULL policy or zone provided");
897 StrAppend(&old_filename, current_filename);
900 temp_filename = NULL;
901 StrAppend(&temp_filename, current_filename);
904 file = fopen(temp_filename,
"w");
909 log_msg(NULL, LOG_ERR,
"Could not open: %s", temp_filename);
916 fprintf(file,
"<SignerConfiguration>\n");
917 fprintf(file,
"\t<Zone name=\"%s\">\n", zone_name);
919 fprintf(file,
"\t\t<Signatures>\n");
920 fprintf(file,
"\t\t\t<Resign>PT%dS</Resign>\n", policy->
signature->
resign);
921 fprintf(file,
"\t\t\t<Refresh>PT%dS</Refresh>\n", policy->
signer->
refresh);
922 fprintf(file,
"\t\t\t<Validity>\n");
925 fprintf(file,
"\t\t\t</Validity>\n");
926 fprintf(file,
"\t\t\t<Jitter>PT%dS</Jitter>\n", policy->
signer->
jitter);
927 fprintf(file,
"\t\t\t<InceptionOffset>PT%dS</InceptionOffset>\n", policy->
signature->
clockskew);
928 fprintf(file,
"\t\t</Signatures>\n");
932 fprintf(file,
"\t\t<Denial>\n");
935 fprintf(file,
"\t\t\t<NSEC3>\n");
937 fprintf(file,
"\t\t\t\t<TTL>PT%dS</TTL>\n", policy->
denial->
ttl);
941 fprintf(file,
"\t\t\t\t<OptOut />\n");
943 fprintf(file,
"\t\t\t\t<Hash>\n");
944 fprintf(file,
"\t\t\t\t\t<Algorithm>%d</Algorithm>\n", policy->
denial->
algorithm);
945 fprintf(file,
"\t\t\t\t\t<Iterations>%d</Iterations>\n", policy->
denial->
iteration);
947 fprintf(file,
"\t\t\t\t\t<Salt>-</Salt>\n");
949 fprintf(file,
"\t\t\t\t\t<Salt>%s</Salt>\n", policy->
denial->
salt);
951 fprintf(file,
"\t\t\t\t</Hash>\n");
952 fprintf(file,
"\t\t\t</NSEC3>\n");
954 fprintf(file,
"\t\t\t<NSEC />\n");
957 fprintf(file,
"\t\t</Denial>\n");
962 fprintf(file,
"\t\t<Keys>\n");
963 fprintf(file,
"\t\t\t<TTL>PT%dS</TTL>\n", policy->
ksk->
ttl);
972 log_msg(NULL, LOG_ERR,
"KsmRequestKeys returned: %d", status);
978 if (status2 == 0 && gencnt == 0) {
979 if(man_key_gen == 1) {
980 log_msg(NULL, LOG_ERR,
"There are no KSKs in the generate state; please use \"ods-ksmutil key generate\" to create some.");
982 log_msg(NULL, LOG_WARNING,
"There are no KSKs in the generate state; ods-enforcerd will create some on its next run.");
985 else if (status2 == 0) {
987 if (status2 == 0 && gencnt == 0) {
988 if(man_key_gen == 1) {
989 log_msg(NULL, LOG_ERR,
"There are no ZSKs in the generate state; please use \"ods-ksmutil key generate\" to create some.");
991 log_msg(NULL, LOG_WARNING,
"There are no ZSKs in the generate state; ods-enforcerd will create some on its next run.");
996 log_msg(NULL, LOG_ERR,
"KsmRequestGenerateCount returned: %d", status2);
1000 status = fclose(file);
1001 unlink(temp_filename);
1009 fprintf(file,
"\t\t</Keys>\n");
1011 fprintf(file,
"\n");
1013 fprintf(file,
"\t\t<SOA>\n");
1014 fprintf(file,
"\t\t\t<TTL>PT%dS</TTL>\n", policy->
signer->
soattl);
1015 fprintf(file,
"\t\t\t<Minimum>PT%dS</Minimum>\n", policy->
signer->
soamin);
1017 fprintf(file,
"\t\t</SOA>\n");
1019 if (strncmp(policy->
audit,
"NULL", 4) != 0) {
1020 fprintf(file,
"\n");
1021 fprintf(file,
"\t\t<Audit />\n");
1022 fprintf(file,
"\n");
1025 fprintf(file,
"\t</Zone>\n");
1026 fprintf(file,
"</SignerConfiguration>\n");
1045 status = fclose(file);
1050 log_msg(NULL, LOG_ERR,
"Could not close: %s", temp_filename);
1057 file = fopen(temp_filename,
"rb");
1061 log_msg(NULL, LOG_ERR,
"Could not reopen: %s", temp_filename);
1067 file2 = fopen(current_filename,
"rb");
1070 if (file2 != NULL) {
1072 while(!feof(file)) {
1073 char1 = fgetc(file);
1075 log_msg(NULL, LOG_ERR,
"Could not read: %s", temp_filename);
1082 char2 = fgetc(file2);
1084 log_msg(NULL, LOG_ERR,
"Could not read: %s", current_filename);
1091 if(char1 != char2) {
1097 status = fclose(file2);
1100 log_msg(NULL, LOG_ERR,
"Could not close: %s", current_filename);
1108 status = fclose(file);
1111 log_msg(NULL, LOG_ERR,
"Could not close: %s", temp_filename);
1122 status = rename(current_filename, old_filename);
1123 if (status != 0 && status != -1)
1126 log_msg(NULL, LOG_ERR,
"Could not rename: %s -> %s", current_filename, old_filename);
1133 if (rename(temp_filename, current_filename) != 0)
1135 log_msg(NULL, LOG_ERR,
"Could not rename: %s -> %s", temp_filename, current_filename);
1141 if (*signer_flag == 1) {
1147 signer_command = NULL;
1148 StrAppend(&signer_command, SIGNER_CLI_UPDATE);
1152 status = system(signer_command);
1155 log_msg(NULL, LOG_ERR,
"Could not call signer engine");
1156 log_msg(NULL, LOG_INFO,
"Will continue: call 'ods-signer update' to manually update zones");
1164 log_msg(NULL, LOG_INFO,
"No change to: %s", current_filename);
1165 if (
remove(temp_filename) != 0)
1167 log_msg(NULL, LOG_ERR,
"Could not remove: %s", temp_filename);
1176 log_msg(NULL, LOG_INFO,
"DSChanged");
1177 status =
NewDSSet(zone_id, zone_name, DSSubmitCmd);
1192 FILE *file = (FILE *)context;
1194 fprintf(file,
"\t\t\t<Key>\n");
1195 fprintf(file,
"\t\t\t\t<Flags>%d</Flags>\n", key_data->
keytype);
1196 fprintf(file,
"\t\t\t\t<Algorithm>%d</Algorithm>\n", key_data->
algorithm);
1197 fprintf(file,
"\t\t\t\t<Locator>%s</Locator>\n", key_data->
location);
1201 fprintf(file,
"\t\t\t\t<KSK />\n");
1205 fprintf(file,
"\t\t\t\t<ZSK />\n");
1209 fprintf(file,
"\t\t\t\t<Publish />\n");
1211 fprintf(file,
"\t\t\t</Key>\n");
1212 fprintf(file,
"\n");
1250 int keys_needed = 0;
1251 int keys_in_queue = 0;
1252 int keys_pending_retirement = 0;
1254 int key_pair_id = 0;
1261 if (datetime == NULL) {
1262 log_msg(NULL, LOG_DEBUG,
"Couldn't turn \"now\" into a date, quitting...");
1266 if (policy == NULL) {
1267 log_msg(NULL, LOG_ERR,
"NULL policy sent to allocateKeysToZone");
1273 log_msg(NULL, LOG_ERR,
"Unknown keytype: %i in allocateKeysToZone", key_type);
1287 status =
KsmKeyPredict(policy->
id, key_type, 1, interval, &keys_needed, rollover_scheme, 1);
1289 log_msg(NULL, LOG_ERR,
"Could not predict key requirement for next interval for %s", zone_name);
1297 log_msg(NULL, LOG_ERR,
"Could not count current key numbers for zone %s", zone_name);
1305 log_msg(NULL, LOG_ERR,
"Could not count keys which may retire before the next run (for zone %s)", zone_name);
1311 new_keys = keys_needed - (keys_in_queue - keys_pending_retirement);
1316 for (i=0 ; i < new_keys ; i++){
1320 if (status == -1 || key_pair_id == 0) {
1321 if (man_key_gen == 0) {
1322 log_msg(NULL, LOG_WARNING,
"Not enough keys to satisfy ksk policy for zone: %s", zone_name);
1323 log_msg(NULL, LOG_WARNING,
"ods-enforcerd will create some more keys on its next run");
1326 log_msg(NULL, LOG_ERR,
"Not enough keys to satisfy ksk policy for zone: %s", zone_name);
1327 log_msg(NULL, LOG_ERR,
"please use \"ods-ksmutil key generate\" to create some more keys.");
1331 else if (status != 0) {
1332 log_msg(NULL, LOG_ERR,
"Could not get an unallocated ksk for zone: %s", zone_name);
1337 if (status == -1 || key_pair_id == 0) {
1338 if (man_key_gen == 0) {
1339 log_msg(NULL, LOG_WARNING,
"Not enough keys to satisfy zsk policy for zone: %s", zone_name);
1340 log_msg(NULL, LOG_WARNING,
"ods-enforcerd will create some more keys on its next run");
1343 log_msg(NULL, LOG_ERR,
"Not enough keys to satisfy zsk policy for zone: %s", zone_name);
1344 log_msg(NULL, LOG_ERR,
"please use \"ods-ksmutil key generate\" to create some more keys.");
1348 else if (status != 0) {
1349 log_msg(NULL, LOG_ERR,
"Could not get an unallocated zsk for zone: %s", zone_name);
1353 if(key_pair_id > 0) {
1358 log_msg(NULL, LOG_ERR,
"KsmKeyGetUnallocated returned bad key_id %d for zone: %s; exiting...", key_pair_id, zone_name);
1372 xmlTextReaderPtr reader = NULL;
1373 xmlDocPtr doc = NULL;
1374 xmlXPathContextPtr xpathCtx = NULL;
1375 xmlXPathObjectPtr xpathObj = NULL;
1377 char* temp_char = NULL;
1378 char* tag_name = NULL;
1380 xmlChar *zonelist_expr = (
unsigned char*)
"//Common/ZoneListFile";
1383 reader = xmlNewTextReaderFilename(filename);
1384 if (reader != NULL) {
1385 ret = xmlTextReaderRead(reader);
1387 tag_name = (
char*) xmlTextReaderLocalName(reader);
1389 if (strncmp(tag_name,
"Common", 6) == 0
1390 && xmlTextReaderNodeType(reader) == 1) {
1393 xmlTextReaderExpand(reader);
1394 doc = xmlTextReaderCurrentDoc(reader);
1396 log_msg(NULL, LOG_ERR,
"Error: can not read Common section of %s", filename);
1398 ret = xmlTextReaderRead(reader);
1402 xpathCtx = xmlXPathNewContext(doc);
1403 if(xpathCtx == NULL) {
1404 log_msg(NULL, LOG_ERR,
"Error: can not create XPath context for Common section");
1406 ret = xmlTextReaderRead(reader);
1411 xpathObj = xmlXPathEvalExpression(zonelist_expr, xpathCtx);
1412 if(xpathObj == NULL) {
1413 log_msg(NULL, LOG_ERR,
"Error: unable to evaluate xpath expression: %s", zonelist_expr);
1415 ret = xmlTextReaderRead(reader);
1418 *zone_list_filename = NULL;
1419 temp_char = (
char *)xmlXPathCastToString(xpathObj);
1420 StrAppend(zone_list_filename, temp_char);
1422 xmlXPathFreeObject(xpathObj);
1423 log_msg(NULL, LOG_INFO,
"zonelist filename set to %s.", *zone_list_filename);
1426 ret = xmlTextReaderRead(reader);
1429 xmlFreeTextReader(reader);
1431 log_msg(NULL, LOG_ERR,
"%s : failed to parse", filename);
1435 log_msg(NULL, LOG_ERR,
"Unable to open %s", filename);
1439 xmlXPathFreeContext(xpathCtx);
1481 char* temp_loc = NULL;
1487 hsm_key_t *key = NULL;
1489 log_msg(NULL, LOG_DEBUG,
"Purging keys...");
1494 if (rightnow == NULL) {
1495 log_msg(NULL, LOG_DEBUG,
"Couldn't turn \"now\" into a date, quitting...");
1500 StrAppend(&sql,
"select distinct id, location from KEYDATA_VIEW where state = 6 ");
1502 if (policy_id != -1) {
1514 while (status == 0) {
1516 DbInt(row, 0, &temp_id);
1524 nchar = snprintf(buffer,
sizeof(buffer),
1525 " or state = %d and DEAD > DATE_ADD('%s', INTERVAL -%d SECOND)) ",
KSM_STATE_DEAD, rightnow, interval);
1527 nchar = snprintf(buffer,
sizeof(buffer),
1528 " or state = %d and DEAD > DATETIME('%s', '-%d SECONDS')) ",
KSM_STATE_DEAD, rightnow, interval);
1531 if (nchar >=
sizeof(buffer)) {
1532 log_msg(NULL, LOG_ERR,
"Error: failed to create SQL statement to purge keys\n");
1589 key = hsm_find_key_by_id(NULL, temp_loc);
1592 log_msg(NULL, LOG_ERR,
"Key not found: %s\n", temp_loc);
1599 status = hsm_remove_key(NULL, key);
1604 log_msg(NULL, LOG_INFO,
"Key remove successful.\n");
1606 log_msg(NULL, LOG_ERR,
"Key remove failed.\n");
1636 int NewDSSet(
int zone_id,
const char* zone_name,
const char* DSSubmitCmd) {
1650 char* count_clause = NULL;
1651 char* where_clause = NULL;
1653 int active_count = -1;
1658 char* ds_buffer = NULL;
1659 char* ds_seen_buffer = NULL;
1660 char* temp_char = NULL;
1668 hsm_key_t *key = NULL;
1669 ldns_rr *dnskey_rr = NULL;
1670 hsm_sign_params_t *sign_params = NULL;
1673 int bytes_written = -1;
1675 nchar = snprintf(buffer,
sizeof(buffer),
"(%d, %d, %d, %d, %d, %d, %d, %d)",
1679 if (nchar >=
sizeof(buffer)) {
1690 if (zone_id != -1) {
1698 log_msg(NULL, LOG_ERR,
"Error: failed to find ID of key to retire\n");
1702 if (active_count > 0) {
1705 StrAppend(&where_clause,
"select id from KEYDATA_VIEW where state = 4 and keytype = 257 and zone_id = ");
1707 StrAppend(&where_clause,
" and retire = (select min(retire) from KEYDATA_VIEW where state = 4 and keytype = 257 and zone_id = ");
1716 log_msg(NULL, LOG_ERR,
"Error: failed to find ID of key to retire\n");
1726 if (zone_id != -1) {
1748 keyids =
MemMalloc(count *
sizeof(
int));
1756 if (zone_id != -1) {
1768 while (status == 0) {
1769 status =
KsmKey(result, &data);
1800 for (j = 0; j < i; ++j) {
1804 snprintf(buffer,
sizeof(buffer),
"%d", keyids[j]);
1817 log_msg(NULL, LOG_INFO,
"DS Record set has changed, the current set looks like:");
1822 status =
KsmKey(result3, &data3);
1823 while (status == 0) {
1826 key = hsm_find_key_by_id(NULL, data3.
location);
1829 log_msg(NULL, LOG_ERR,
"Key %s in DB but not repository.", data3.
location);
1837 sign_params = hsm_sign_params_new();
1838 sign_params->owner = ldns_rdf_new_frm_str(LDNS_RDF_TYPE_DNAME, zone_name);
1839 sign_params->algorithm = data3.
algorithm;
1840 sign_params->flags = LDNS_KEY_ZONE_KEY;
1841 sign_params->flags += LDNS_KEY_SEP_KEY;
1842 dnskey_rr = hsm_get_dnskey(NULL, key, sign_params);
1852 ldns_rr_set_ttl(dnskey_rr, rrttl);
1856 temp_char = ldns_rr2str(dnskey_rr);
1857 ldns_rr_free(dnskey_rr);
1860 for (i = 0; temp_char[i]; ++i) {
1861 if (temp_char[i] ==
'\t') {
1865 log_msg(NULL, LOG_INFO,
"%s", temp_char);
1869 for (i = 0; temp_char[i]; ++i) {
1870 if (temp_char[i] ==
';') {
1871 temp_char[i] =
'\n';
1872 temp_char[i+1] =
'\0';
1892 hsm_sign_params_free(sign_params);
1894 status =
KsmKey(result3, &data3);
1904 if (DSSubmitCmd[0] !=
'\0') {
1906 fp = popen(DSSubmitCmd,
"w");
1908 log_msg(NULL, LOG_ERR,
"Failed to run command: %s: %s", DSSubmitCmd, strerror(errno));
1911 bytes_written = fprintf(fp,
"%s", ds_buffer);
1912 if (bytes_written < 0) {
1913 log_msg(NULL, LOG_ERR,
"Failed to write to %s: %s", DSSubmitCmd, strerror(errno));
1917 if (pclose(fp) == -1) {
1918 log_msg(NULL, LOG_ERR,
"Failed to close %s: %s", DSSubmitCmd, strerror(errno));
1925 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);
1937 char *hsm_error_message = NULL;
1939 result = hsm_check_context(*ctx);
1942 if (result != HSM_OK) {
1945 hsm_destroy_context(*ctx);
1948 result = hsm_close();
1951 result = hsm_open(config->
configfile, hsm_prompt_pin, NULL);
1953 result = hsm_open(OPENDNSSEC_CONFIG_FILE, hsm_prompt_pin, NULL);
1956 hsm_error_message = hsm_get_error(*ctx);
1957 if (hsm_error_message) {
1958 log_msg(config, LOG_ERR, hsm_error_message);
1959 free(hsm_error_message);
1966 log_msg(config, LOG_ERR,
"hsm_open() result: HSM error");
1968 case HSM_PIN_INCORRECT:
1969 log_msg(config, LOG_ERR,
"hsm_open() result: incorrect PIN");
1971 case HSM_CONFIG_FILE_ERROR:
1972 log_msg(config, LOG_ERR,
"hsm_open() result: config file error");
1974 case HSM_REPOSITORY_NOT_FOUND:
1975 log_msg(config, LOG_ERR,
"hsm_open() result: repository not found");
1977 case HSM_NO_REPOSITORIES:
1978 log_msg(config, LOG_ERR,
"hsm_open() result: no repositories");
1981 log_msg(config, LOG_ERR,
"hsm_open() result: %d", result);
1987 log_msg(config, LOG_INFO,
"HSM reopened successfully.");
1988 *ctx = hsm_create_context();
1990 log_msg(config, LOG_INFO,
"HSM connection open.");
void DbFreeResult(DB_RESULT result)
void server_main(DAEMONCONFIG *config)
int KsmPolicyInit(DB_RESULT *handle, const char *name)
char name[KSM_NAME_LENGTH]
unsigned long sm_capacity
void kaspConnect(DAEMONCONFIG *config, DB_HANDLE *handle)
int DbFetchRow(DB_RESULT result, DB_ROW *row)
int KsmPolicy(DB_RESULT handle, KSM_POLICY *data)
char * DqsSpecifyInit(const char *table, const char *fields)
char location[KSM_NAME_LENGTH]
int KsmKeyCountQueue(int keytype, int *count, int zone_id)
KSM_POLICY * KsmPolicyAlloc()
KSM_COMMON_KEY_POLICY * keys
int KsmParameterCollection(KSM_PARCOLL *data, int policy_id)
int kaspTryConnect(DAEMONCONFIG *config, DB_HANDLE *handle)
void check_hsm_connection(hsm_ctx_t **ctx, DAEMONCONFIG *config)
void kaspDisconnect(DB_HANDLE *handle)
void DqsConditionKeyword(char **query, const char *field, DQS_COMPARISON compare, const char *value, int index)
int KsmKeyPairCreate(int policy_id, const char *HSMKeyID, int smID, int size, int alg, const char *generate, DB_ID *id)
int KsmZoneCount(DB_RESULT handle, int *count)
char sm_name[KSM_NAME_LENGTH]
int KsmPolicyUpdateSalt(KSM_POLICY *policy)
int KsmPolicyRead(KSM_POLICY *policy)
void DqsFree(char *query)
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)
#define KSM_STATE_KEYPUBLISH
void DdsFree(char *query)
int read_zonelist_filename(const char *filename, char **zone_list_filename)
char * DqsCountInit(const char *table)
int KsmCheckNextRollover(int keytype, int zone_id, char **datetime)
int DbString(DB_ROW row, int field_index, char **result)
void DqsConditionInt(char **query, const char *field, DQS_COMPARISON compare, int value, int index)
char salt[KSM_SALT_LENGTH]
void DdsConditionInt(char **query, const char *field, DQS_COMPARISON compare, int value, int index)
int KsmPolicyIdFromZoneId(int zone_id, int *policy_id)
char * DdsInit(const char *table)
char * DtParseDateTimeString(const char *string)
#define KSM_STATE_DSPUBLISH
KSM_DENIAL_POLICY * denial
int KsmZoneIdFromName(const char *zone_name, int *zone_id)
int do_communication(DAEMONCONFIG *config, KSM_POLICY *policy, bool all_policies)
int KsmParameterValue(const char *name, const char *category, int *value, int policy_id, int *parameter_id)
int KsmKeyInitSql(DB_RESULT *result, const char *sql)
int do_keygen(DAEMONCONFIG *config, KSM_POLICY *policy, hsm_ctx_t *ctx)
#define DB_KEYDATA_FIELDS
const char * DbErrmsg(DB_HANDLE handle)
void KsmPolicyFree(KSM_POLICY *policy)
void DbFreeRow(DB_ROW row)
int KsmKey(DB_RESULT result, KSM_KEYDATA *data)
KSM_SIGNER_POLICY * signer
int ReadConfig(DAEMONCONFIG *config, int verbose)
int KsmKeyPredict(int policy_id, int keytype, int shared_keys, int interval, int *count, int rollover_scheme, int zone_count)
void kaspSetPolicyDefaults(KSM_POLICY *policy, char *name)
const char * KsmKeywordSerialValueToName(int value)
int KsmDnssecKeyCreate(int zone_id, int keypair_id, int keytype, int state, const char *time, const char *retTime, DB_ID *id)
int DbExecuteSql(DB_HANDLE handle, const char *stmt_str, DB_RESULT *result)
int KsmRequestGenerateCount(int keytype, int *count, int zone_id)
int release_lite_lock(FILE *lock_fd)
void StrAppend(char **str1, const char *str2)
int DbIntQuery(DB_HANDLE handle, int *value, const char *query)
#define KSM_PAR_KSKTTL_CAT
#define KSM_STATE_PUBLISH
void KsmParameterCollectionCache(int enable)
int KsmKeyGetUnallocated(int policy_id, int sm, int bits, int algorithm, int zone_id, int share_keys, int *keypair_id)
int get_lite_lock(char *lock_filename, FILE *lock_fd)
int KsmPolicyExists(const char *name)
int allocateKeysToZone(KSM_POLICY *policy, int key_type, int zone_id, uint16_t interval, const char *zone_name, int man_key_gen, int rollover_scheme)
int writepid(DAEMONCONFIG *config)
#define KSM_PAR_KSKTTL_STRING
void log_msg(DAEMONCONFIG *config, int priority, const char *format,...)
int KsmRequestPendingRetireCount(int keytype, const char *datetime, KSM_PARCOLL *parameters, int *count, int zone_id, int interval)
int KsmZoneCountInit(DB_RESULT *handle, int id)
void DdsEnd(char **query)
int DbInt(DB_ROW row, int field_index, int *value)
void * MemMalloc(size_t size)
#define KSM_STATE_DSREADY
int NewDSSet(int zone_id, const char *zone_name, const char *DSSubmitCmd)
void KsmKeyEnd(DB_RESULT result)
int server_init(DAEMONCONFIG *config)
int KsmRequestKeys(int keytype, int rollover, const char *datetime, KSM_REQUEST_CALLBACK callback, void *context, int policy_id, int zone_id, int run_interval, int *NewDS)
#define KSM_STATE_GENERATE
void DqsEnd(char **query)
int kaspReadPolicy(KSM_POLICY *policy)
int DtDateDiff(const char *date1, const char *date2, int *result)
int DbExecuteSqlNoResult(DB_HANDLE handle, const char *stmt_str)
int KsmKeyCountStillGood(int policy_id, int sm, int bits, int algorithm, int interval, const char *datetime, int *count, int keytype)
int do_purge(int interval, int policy_id)
KSM_SIGNATURE_POLICY * signature
int commKeyConfig(void *context, KSM_KEYDATA *key_data)
void DbStringFree(char *string)