45 #include <libxml/xmlreader.h>
46 #include <libxml/xpath.h>
61 #include "libhsmdns.h"
67 log_msg(NULL, LOG_ERR,
"Error in server_init, no config provided");
73 config->
pidfile = OPENDNSSEC_ENFORCER_PIDFILE;
91 hsm_ctx_t *ctx = NULL;
92 char *hsm_error_message = NULL;
95 char *lock_filename = NULL;
98 log_msg(NULL, LOG_ERR,
"Error in server_main, no config provided");
103 if (policy == NULL) {
104 log_msg(config, LOG_ERR,
"Malloc for policy struct failed");
112 log_msg(config, LOG_ERR,
"Error reading config");
120 result = hsm_open(config->
configfile, hsm_check_pin);
122 result = hsm_open(OPENDNSSEC_CONFIG_FILE, hsm_check_pin);
125 hsm_error_message = hsm_get_error(ctx);
126 if (hsm_error_message) {
127 log_msg(config, LOG_ERR,
"%s", hsm_error_message);
128 free(hsm_error_message);
134 log_msg(config, LOG_ERR,
"hsm_open() result: HSM error");
136 case HSM_PIN_INCORRECT:
137 log_msg(config, LOG_ERR,
"hsm_open() result: incorrect PIN");
139 case HSM_CONFIG_FILE_ERROR:
140 log_msg(config, LOG_ERR,
"hsm_open() result: config file error");
142 case HSM_REPOSITORY_NOT_FOUND:
143 log_msg(config, LOG_ERR,
"hsm_open() result: repository not found");
145 case HSM_NO_REPOSITORIES:
146 log_msg(config, LOG_ERR,
"hsm_open() result: no repositories");
149 log_msg(config, LOG_ERR,
"hsm_open() result: %d", result);
154 log_msg(config, LOG_INFO,
"HSM opened successfully.");
155 ctx = hsm_create_context();
158 log_msg(config, LOG_INFO,
"Checking database connection...");
160 log_msg(config, LOG_ERR,
"Database connection failed");
163 log_msg(config, LOG_INFO,
"Database connection ok.");
167 log_msg(config, LOG_ERR,
"cannot write the pidfile %s: %s",
168 config->
pidfile, strerror(errno));
177 log_msg(config, LOG_ERR,
"Error reading config");
187 lock_filename = NULL;
191 lock_fd = fopen(lock_filename,
"w");
195 log_msg(config, LOG_ERR,
"Error getting db lock");
201 log_msg(config, LOG_INFO,
"Connecting to Database...");
205 if (config->
policy != NULL) {
206 log_msg(config, LOG_INFO,
"Will only process policy \"%s\" as specified on the command line with the --policy option.", config->
policy);
209 log_msg(config, LOG_ERR,
"Policy \"%s\" not found. Exiting.", config->
policy);
220 while (status == 0) {
221 log_msg(config, LOG_INFO,
"Policy %s found.", policy->name);
229 if (policy->denial->version == 3)
234 log_msg(config, LOG_ERR,
"Error (%d) updating salt for %s", status, policy->name);
247 if (policy->keys->purge != -1) {
248 status =
do_purge(policy->keys->purge, policy->id);
255 log_msg(config, LOG_ERR,
"Error querying KASP DB for policies.");
271 log_msg(config, LOG_INFO,
"Disconnecting from Database...");
278 log_msg(config, LOG_ERR,
"Error releasing db lock");
285 if (config->
once ==
true ){
286 log_msg(config, LOG_INFO,
"Running once only, exiting...");
291 if (config->
term == 1 ){
292 log_msg(config, LOG_INFO,
"Received SIGTERM, exiting...");
296 if (config->
term == 2 ){
297 log_msg(config, LOG_INFO,
"Received SIGINT, exiting...");
304 log_msg(config, LOG_INFO,
"Sleeping for %i seconds.",config->
interval);
305 select(0, NULL, NULL, NULL, &tv);
308 if (config->
term == 1 ){
309 log_msg(config, LOG_INFO,
"Received SIGTERM, exiting...");
313 if (config->
term == 2 ){
314 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_ERR,
"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;
601 char *tag_name = NULL;
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);
765 log_msg(config, LOG_ERR,
"Error allocating zsks to zone %s", zone_name);
767 ret = xmlTextReaderRead(reader);
775 log_msg(config, LOG_ERR,
"Error allocating ksks to zone %s", zone_name);
777 ret = xmlTextReaderRead(reader);
787 log_msg(config, LOG_ERR,
"Signconf not written for %s", zone_name);
789 ret = xmlTextReaderRead(reader);
795 else if (status2 != 0) {
796 log_msg(config, LOG_ERR,
"Error writing signconf for %s", zone_name);
798 ret = xmlTextReaderRead(reader);
810 if (datetime == NULL) {
811 log_msg(config, LOG_ERR,
"Couldn't turn \"now\" into a date, quiting...");
819 log_msg(config, LOG_INFO,
"No active KSKs yet for zone %s, can't check for impending rollover", zone_name);
821 else if (status2 != 0) {
822 log_msg(config, LOG_ERR,
"Error checking for impending rollover for %s", zone_name);
825 status2 =
DtDateDiff(ksk_expected, datetime, &roll_time);
827 log_msg(config, LOG_ERR,
"Error checking for impending rollover for %s", zone_name);
830 if (roll_time <= config->rolloverNotify) {
831 log_msg(config, LOG_INFO,
"Rollover of KSK expected at %s for %s", ksk_expected, zone_name);
843 ret = xmlTextReaderRead(reader);
846 xmlFreeTextReader(reader);
848 log_msg(config, LOG_ERR,
"%s : failed to parse", zonelist_filename);
851 log_msg(config, LOG_ERR,
"Unable to open %s", zonelist_filename);
866 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,
int DSSubCKA_ID)
879 char *signer_command;
884 if (datetime == NULL) {
885 log_msg(NULL, LOG_DEBUG,
"Couldn't turn \"now\" into a date, quitting...");
889 if (zone_name == NULL || current_filename == NULL || policy == NULL)
892 log_msg(NULL, LOG_ERR,
"commGenSignConf, NULL policy or zone provided");
898 StrAppend(&old_filename, current_filename);
901 temp_filename = NULL;
902 StrAppend(&temp_filename, current_filename);
905 file = fopen(temp_filename,
"w");
910 log_msg(NULL, LOG_ERR,
"Could not open: %s (%s)", temp_filename,
918 fprintf(file,
"<SignerConfiguration>\n");
919 fprintf(file,
"\t<Zone name=\"%s\">\n", zone_name);
921 fprintf(file,
"\t\t<Signatures>\n");
922 fprintf(file,
"\t\t\t<Resign>PT%dS</Resign>\n", policy->
signature->
resign);
923 fprintf(file,
"\t\t\t<Refresh>PT%dS</Refresh>\n", policy->
signer->
refresh);
924 fprintf(file,
"\t\t\t<Validity>\n");
927 fprintf(file,
"\t\t\t</Validity>\n");
928 fprintf(file,
"\t\t\t<Jitter>PT%dS</Jitter>\n", policy->
signer->
jitter);
929 fprintf(file,
"\t\t\t<InceptionOffset>PT%dS</InceptionOffset>\n", policy->
signature->
clockskew);
930 fprintf(file,
"\t\t</Signatures>\n");
934 fprintf(file,
"\t\t<Denial>\n");
937 fprintf(file,
"\t\t\t<NSEC3>\n");
939 fprintf(file,
"\t\t\t\t<TTL>PT%dS</TTL>\n", policy->
denial->
ttl);
943 fprintf(file,
"\t\t\t\t<OptOut />\n");
945 fprintf(file,
"\t\t\t\t<Hash>\n");
946 fprintf(file,
"\t\t\t\t\t<Algorithm>%d</Algorithm>\n", policy->
denial->
algorithm);
947 fprintf(file,
"\t\t\t\t\t<Iterations>%d</Iterations>\n", policy->
denial->
iteration);
949 fprintf(file,
"\t\t\t\t\t<Salt>-</Salt>\n");
951 fprintf(file,
"\t\t\t\t\t<Salt>%s</Salt>\n", policy->
denial->
salt);
953 fprintf(file,
"\t\t\t\t</Hash>\n");
954 fprintf(file,
"\t\t\t</NSEC3>\n");
956 fprintf(file,
"\t\t\t<NSEC />\n");
959 fprintf(file,
"\t\t</Denial>\n");
964 fprintf(file,
"\t\t<Keys>\n");
965 fprintf(file,
"\t\t\t<TTL>PT%dS</TTL>\n", policy->
ksk->
ttl);
974 log_msg(NULL, LOG_ERR,
"KsmRequestKeys returned: %d", status);
980 if (status2 == 0 && gencnt == 0) {
981 if(man_key_gen == 1) {
982 log_msg(NULL, LOG_ERR,
"There are no KSKs in the generate state; please use \"ods-ksmutil key generate\" to create some.");
984 log_msg(NULL, LOG_WARNING,
"There are no KSKs in the generate state; ods-enforcerd will create some on its next run.");
987 else if (status2 == 0) {
989 if (status2 == 0 && gencnt == 0) {
990 if(man_key_gen == 1) {
991 log_msg(NULL, LOG_ERR,
"There are no ZSKs in the generate state; please use \"ods-ksmutil key generate\" to create some.");
993 log_msg(NULL, LOG_WARNING,
"There are no ZSKs in the generate state; ods-enforcerd will create some on its next run.");
998 log_msg(NULL, LOG_ERR,
"KsmRequestGenerateCount returned: %d", status2);
1002 status = fclose(file);
1003 unlink(temp_filename);
1011 fprintf(file,
"\t\t</Keys>\n");
1013 fprintf(file,
"\n");
1015 fprintf(file,
"\t\t<SOA>\n");
1016 fprintf(file,
"\t\t\t<TTL>PT%dS</TTL>\n", policy->
signer->
soattl);
1017 fprintf(file,
"\t\t\t<Minimum>PT%dS</Minimum>\n", policy->
signer->
soamin);
1019 fprintf(file,
"\t\t</SOA>\n");
1021 fprintf(file,
"\t</Zone>\n");
1022 fprintf(file,
"</SignerConfiguration>\n");
1041 status = fclose(file);
1046 log_msg(NULL, LOG_ERR,
"Could not close: %s", temp_filename);
1053 file = fopen(temp_filename,
"rb");
1057 log_msg(NULL, LOG_ERR,
"Could not reopen: %s", temp_filename);
1063 file2 = fopen(current_filename,
"rb");
1066 if (file2 != NULL) {
1068 while(!feof(file)) {
1069 char1 = fgetc(file);
1071 log_msg(NULL, LOG_ERR,
"Could not read: %s", temp_filename);
1078 char2 = fgetc(file2);
1080 log_msg(NULL, LOG_ERR,
"Could not read: %s", current_filename);
1087 if(char1 != char2) {
1093 status = fclose(file2);
1096 log_msg(NULL, LOG_ERR,
"Could not close: %s", current_filename);
1104 status = fclose(file);
1107 log_msg(NULL, LOG_ERR,
"Could not close: %s", temp_filename);
1118 status = rename(current_filename, old_filename);
1119 if (status != 0 && status != -1)
1122 log_msg(NULL, LOG_ERR,
"Could not rename: %s -> %s", current_filename, old_filename);
1129 if (rename(temp_filename, current_filename) != 0)
1131 log_msg(NULL, LOG_ERR,
"Could not rename: %s -> %s", temp_filename, current_filename);
1137 if (*signer_flag == 1) {
1143 signer_command = NULL;
1144 StrAppend(&signer_command, SIGNER_CLI_UPDATE);
1148 status = system(signer_command);
1151 log_msg(NULL, LOG_ERR,
"Could not call signer engine");
1152 log_msg(NULL, LOG_INFO,
"Will continue: call '%s' to manually update the zone", signer_command);
1156 log_msg(NULL, LOG_INFO,
"Called signer engine: %s", signer_command);
1163 log_msg(NULL, LOG_INFO,
"No change to: %s", current_filename);
1164 if (
remove(temp_filename) != 0)
1166 log_msg(NULL, LOG_ERR,
"Could not remove: %s", temp_filename);
1175 log_msg(NULL, LOG_INFO,
"DSChanged");
1176 status =
NewDSSet(zone_id, zone_name, DSSubmitCmd, DSSubCKA_ID);
1191 FILE *file = (FILE *)context;
1193 fprintf(file,
"\t\t\t<Key>\n");
1194 fprintf(file,
"\t\t\t\t<Flags>%d</Flags>\n", key_data->
keytype);
1195 fprintf(file,
"\t\t\t\t<Algorithm>%d</Algorithm>\n", key_data->
algorithm);
1196 fprintf(file,
"\t\t\t\t<Locator>%s</Locator>\n", key_data->
location);
1200 fprintf(file,
"\t\t\t\t<KSK />\n");
1204 fprintf(file,
"\t\t\t\t<ZSK />\n");
1208 fprintf(file,
"\t\t\t\t<Publish />\n");
1210 fprintf(file,
"\t\t\t</Key>\n");
1211 fprintf(file,
"\n");
1249 int keys_needed = 0;
1250 int keys_in_queue = 0;
1251 int keys_pending_retirement = 0;
1253 int key_pair_id = 0;
1260 if (datetime == NULL) {
1261 log_msg(NULL, LOG_DEBUG,
"Couldn't turn \"now\" into a date, quitting...");
1265 if (policy == NULL) {
1266 log_msg(NULL, LOG_ERR,
"NULL policy sent to allocateKeysToZone");
1272 log_msg(NULL, LOG_ERR,
"Unknown keytype: %i in allocateKeysToZone", key_type);
1286 status =
KsmKeyPredict(policy->
id, key_type, 1, interval, &keys_needed, rollover_scheme, 1);
1288 log_msg(NULL, LOG_ERR,
"Could not predict key requirement for next interval for %s", zone_name);
1296 log_msg(NULL, LOG_ERR,
"Could not count current key numbers for zone %s", zone_name);
1304 log_msg(NULL, LOG_ERR,
"Could not count keys which may retire before the next run (for zone %s)", zone_name);
1310 new_keys = keys_needed - (keys_in_queue - keys_pending_retirement);
1315 for (i=0 ; i < new_keys ; i++){
1319 if (status == -1 || key_pair_id == 0) {
1320 if (man_key_gen == 0) {
1321 log_msg(NULL, LOG_WARNING,
"Not enough keys to satisfy ksk policy for zone: %s", zone_name);
1322 log_msg(NULL, LOG_WARNING,
"ods-enforcerd will create some more keys on its next run");
1325 log_msg(NULL, LOG_ERR,
"Not enough keys to satisfy ksk policy for zone: %s", zone_name);
1326 log_msg(NULL, LOG_ERR,
"please use \"ods-ksmutil key generate\" to create some more keys.");
1330 else if (status != 0) {
1331 log_msg(NULL, LOG_ERR,
"Could not get an unallocated ksk for zone: %s", zone_name);
1336 if (status == -1 || key_pair_id == 0) {
1337 if (man_key_gen == 0) {
1338 log_msg(NULL, LOG_WARNING,
"Not enough keys to satisfy zsk policy for zone: %s", zone_name);
1339 log_msg(NULL, LOG_WARNING,
"ods-enforcerd will create some more keys on its next run");
1342 log_msg(NULL, LOG_ERR,
"Not enough keys to satisfy zsk policy for zone: %s", zone_name);
1343 log_msg(NULL, LOG_ERR,
"please use \"ods-ksmutil key generate\" to create some more keys.");
1347 else if (status != 0) {
1348 log_msg(NULL, LOG_ERR,
"Could not get an unallocated zsk for zone: %s", zone_name);
1352 if(key_pair_id > 0) {
1357 log_msg(NULL, LOG_ERR,
"KsmKeyGetUnallocated returned bad key_id %d for zone: %s; exiting...", key_pair_id, zone_name);
1371 xmlTextReaderPtr reader = NULL;
1372 xmlDocPtr doc = NULL;
1373 xmlXPathContextPtr xpathCtx = NULL;
1374 xmlXPathObjectPtr xpathObj = NULL;
1376 char* temp_char = NULL;
1377 char* tag_name = NULL;
1379 xmlChar *zonelist_expr = (
unsigned char*)
"//Common/ZoneListFile";
1382 reader = xmlNewTextReaderFilename(filename);
1383 if (reader != NULL) {
1384 ret = xmlTextReaderRead(reader);
1386 tag_name = (
char*) xmlTextReaderLocalName(reader);
1388 if (strncmp(tag_name,
"Common", 6) == 0
1389 && xmlTextReaderNodeType(reader) == 1) {
1392 xmlTextReaderExpand(reader);
1393 doc = xmlTextReaderCurrentDoc(reader);
1395 log_msg(NULL, LOG_ERR,
"Error: can not read Common section of %s", filename);
1397 ret = xmlTextReaderRead(reader);
1401 xpathCtx = xmlXPathNewContext(doc);
1402 if(xpathCtx == NULL) {
1403 log_msg(NULL, LOG_ERR,
"Error: can not create XPath context for Common section");
1405 ret = xmlTextReaderRead(reader);
1410 xpathObj = xmlXPathEvalExpression(zonelist_expr, xpathCtx);
1411 if(xpathObj == NULL) {
1412 log_msg(NULL, LOG_ERR,
"Error: unable to evaluate xpath expression: %s", zonelist_expr);
1414 ret = xmlTextReaderRead(reader);
1417 *zone_list_filename = NULL;
1418 temp_char = (
char *)xmlXPathCastToString(xpathObj);
1419 StrAppend(zone_list_filename, temp_char);
1421 xmlXPathFreeObject(xpathObj);
1422 log_msg(NULL, LOG_INFO,
"zonelist filename set to %s.", *zone_list_filename);
1425 ret = xmlTextReaderRead(reader);
1428 xmlFreeTextReader(reader);
1430 log_msg(NULL, LOG_ERR,
"%s : failed to parse", filename);
1434 log_msg(NULL, LOG_ERR,
"Unable to open %s", filename);
1438 xmlXPathFreeContext(xpathCtx);
1479 char* temp_loc = NULL;
1485 hsm_key_t *key = NULL;
1487 log_msg(NULL, LOG_DEBUG,
"Purging keys...");
1492 if (rightnow == NULL) {
1493 log_msg(NULL, LOG_ERR,
"Couldn't turn \"now\" into a date, quitting...");
1498 StrAppend(&sql,
"select distinct id, location from KEYDATA_VIEW where state = 6 ");
1500 if (policy_id != -1) {
1512 while (status == 0) {
1514 DbInt(row, 0, &temp_id);
1523 log_msg(NULL, LOG_ERR,
"DbDateDiff failed\n");
1532 StrAppend(&sql1,
" or state = 6 and DEAD > ");
1587 key = hsm_find_key_by_id(NULL, temp_loc);
1590 log_msg(NULL, LOG_ERR,
"Key not found: %s\n", temp_loc);
1598 status = hsm_remove_key(NULL, key);
1603 log_msg(NULL, LOG_INFO,
"Key remove successful: %s\n", temp_loc);
1605 log_msg(NULL, LOG_ERR,
"Key remove failed: %s\n", temp_loc);
1636 int NewDSSet(
int zone_id,
const char* zone_name,
const char* DSSubmitCmd,
int DSSubCKA_ID) {
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 struct stat stat_ret;
1677 nchar = snprintf(buffer,
sizeof(buffer),
"(%d, %d, %d, %d, %d, %d, %d, %d)",
1681 if (nchar >=
sizeof(buffer)) {
1692 if (zone_id != -1) {
1700 log_msg(NULL, LOG_ERR,
"Error: failed to find ID of key to retire\n");
1704 if (active_count > 0) {
1707 StrAppend(&where_clause,
"select id from KEYDATA_VIEW where state = 4 and keytype = 257 and zone_id = ");
1709 StrAppend(&where_clause,
" and retire = (select min(retire) from KEYDATA_VIEW where state = 4 and keytype = 257 and zone_id = ");
1718 log_msg(NULL, LOG_ERR,
"Error: failed to find ID of key to retire\n");
1728 if (zone_id != -1) {
1750 keyids =
MemMalloc(count *
sizeof(
int));
1758 if (zone_id != -1) {
1770 while (status == 0) {
1771 status =
KsmKey(result, &data);
1802 for (j = 0; j < i; ++j) {
1806 snprintf(buffer,
sizeof(buffer),
"%d", keyids[j]);
1819 log_msg(NULL, LOG_INFO,
"DS Record set has changed, the current set looks like:");
1824 status =
KsmKey(result3, &data3);
1825 while (status == 0) {
1828 key = hsm_find_key_by_id(NULL, data3.
location);
1831 log_msg(NULL, LOG_ERR,
"Key %s in DB but not repository.", data3.
location);
1839 sign_params = hsm_sign_params_new();
1840 sign_params->owner = ldns_rdf_new_frm_str(LDNS_RDF_TYPE_DNAME, zone_name);
1841 sign_params->algorithm = data3.
algorithm;
1842 sign_params->flags = LDNS_KEY_ZONE_KEY;
1843 sign_params->flags += LDNS_KEY_SEP_KEY;
1844 dnskey_rr = hsm_get_dnskey(NULL, key, sign_params);
1854 ldns_rr_set_ttl(dnskey_rr, rrttl);
1858 temp_char = ldns_rr2str(dnskey_rr);
1859 ldns_rr_free(dnskey_rr);
1862 for (i = 0; temp_char[i]; ++i) {
1863 if (temp_char[i] ==
'\t') {
1867 log_msg(NULL, LOG_INFO,
"%s", temp_char);
1871 for (i = 0; temp_char[i]; ++i) {
1872 if (temp_char[i] ==
';') {
1873 temp_char[i] =
'\n';
1874 temp_char[i+1] =
'\0';
1902 hsm_sign_params_free(sign_params);
1904 status =
KsmKey(result3, &data3);
1914 if (DSSubmitCmd[0] !=
'\0') {
1916 if (stat(DSSubmitCmd, &stat_ret) != 0) {
1917 log_msg(NULL, LOG_WARNING,
"Cannot stat file %s: %s", DSSubmitCmd, strerror(errno));
1920 else if (S_ISREG(stat_ret.st_mode) && !(stat_ret.st_mode & S_IXUSR || stat_ret.st_mode & S_IXGRP || stat_ret.st_mode & S_IXOTH)) {
1921 log_msg(NULL, LOG_WARNING,
"File %s is not executable", DSSubmitCmd);
1926 fp = popen(DSSubmitCmd,
"w");
1928 log_msg(NULL, LOG_ERR,
"Failed to run command: %s: %s", DSSubmitCmd, strerror(errno));
1932 bytes_written = fprintf(fp,
"%s", ds_buffer);
1933 if (bytes_written < 0) {
1934 log_msg(NULL, LOG_ERR,
"Failed to write to %s: %s", DSSubmitCmd, strerror(errno));
1938 if (pclose(fp) == -1) {
1939 log_msg(NULL, LOG_ERR,
"Failed to close %s: %s", DSSubmitCmd, strerror(errno));
1950 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);
1962 char *hsm_error_message = NULL;
1964 result = hsm_check_context(*ctx);
1967 if (result != HSM_OK) {
1970 hsm_destroy_context(*ctx);
1973 result = hsm_close();
1976 result = hsm_open(config->
configfile, hsm_check_pin);
1978 result = hsm_open(OPENDNSSEC_CONFIG_FILE, hsm_check_pin);
1981 hsm_error_message = hsm_get_error(*ctx);
1982 if (hsm_error_message) {
1983 log_msg(config, LOG_ERR, hsm_error_message);
1984 free(hsm_error_message);
1991 log_msg(config, LOG_ERR,
"hsm_open() result: HSM error");
1993 case HSM_PIN_INCORRECT:
1994 log_msg(config, LOG_ERR,
"hsm_open() result: incorrect PIN");
1996 case HSM_CONFIG_FILE_ERROR:
1997 log_msg(config, LOG_ERR,
"hsm_open() result: config file error");
1999 case HSM_REPOSITORY_NOT_FOUND:
2000 log_msg(config, LOG_ERR,
"hsm_open() result: repository not found");
2002 case HSM_NO_REPOSITORIES:
2003 log_msg(config, LOG_ERR,
"hsm_open() result: no repositories");
2006 log_msg(config, LOG_ERR,
"hsm_open() result: %d", result);
2012 log_msg(config, LOG_INFO,
"HSM reopened successfully.");
2013 *ctx = hsm_create_context();
2015 log_msg(config, LOG_INFO,
"HSM connection open.");
void DbFreeResult(DB_RESULT result)
int KsmPolicyInit(DB_RESULT *handle, const char *name)
char name[KSM_NAME_LENGTH]
unsigned long sm_capacity
void kaspConnect(DAEMONCONFIG *config, DB_HANDLE *handle)
void server_main(DAEMONCONFIG *config)
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)
#define KSM_STATE_KEYPUBLISH
void DdsFree(char *query)
int read_zonelist_filename(const char *filename, char **zone_list_filename)
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, int DSSubCKA_ID)
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 server_init(DAEMONCONFIG *config)
int DbIntQuery(DB_HANDLE handle, int *value, const char *query)
#define KSM_PAR_KSKTTL_CAT
#define KSM_STATE_PUBLISH
int DbDateDiff(const char *start, int delta, int sign, char *buffer, size_t buflen)
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, int DSSubCKA_ID)
void KsmKeyEnd(DB_RESULT result)
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)