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...");
209 while (status == 0) {
210 log_msg(config, LOG_INFO,
"Policy %s found.", policy->name);
218 if (policy->denial->version == 3)
223 log_msg(config, LOG_ERR,
"Error (%d) updating salt for %s", status, policy->name);
236 if (policy->keys->purge != -1) {
237 status =
do_purge(policy->keys->purge, policy->id);
244 log_msg(config, LOG_ERR,
"Error querying KASP DB for policies.");
257 log_msg(config, LOG_INFO,
"Disconnecting from Database...");
264 log_msg(config, LOG_ERR,
"Error releasing db lock");
271 if (config->
once ==
true ){
272 log_msg(config, LOG_INFO,
"Running once only, exiting...");
277 if (config->
term == 1 ){
278 log_msg(config, LOG_INFO,
"Received SIGTERM, exiting...");
282 if (config->
term == 2 ){
283 log_msg(config, LOG_INFO,
"Received SIGINT, exiting...");
290 log_msg(config, LOG_INFO,
"Sleeping for %i seconds.",config->
interval);
291 select(0, NULL, NULL, NULL, &tv);
294 if (config->
term == 1 ){
295 log_msg(config, LOG_INFO,
"Received SIGTERM, exiting...");
299 if (config->
term == 2 ){
300 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_ERR,
"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;
578 char *tag_name = NULL;
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);
733 log_msg(config, LOG_ERR,
"Error allocating zsks to zone %s", zone_name);
735 ret = xmlTextReaderRead(reader);
743 log_msg(config, LOG_ERR,
"Error allocating ksks to zone %s", zone_name);
745 ret = xmlTextReaderRead(reader);
755 log_msg(config, LOG_ERR,
"Signconf not written for %s", zone_name);
757 ret = xmlTextReaderRead(reader);
763 else if (status2 != 0) {
764 log_msg(config, LOG_ERR,
"Error writing signconf for %s", zone_name);
766 ret = xmlTextReaderRead(reader);
778 if (datetime == NULL) {
779 log_msg(config, LOG_ERR,
"Couldn't turn \"now\" into a date, quiting...");
787 log_msg(config, LOG_INFO,
"No active KSKs yet for zone %s, can't check for impending rollover", zone_name);
789 else if (status2 != 0) {
790 log_msg(config, LOG_ERR,
"Error checking for impending rollover for %s", zone_name);
793 status2 =
DtDateDiff(ksk_expected, datetime, &roll_time);
795 log_msg(config, LOG_ERR,
"Error checking for impending rollover for %s", zone_name);
798 if (roll_time <= config->rolloverNotify) {
799 log_msg(config, LOG_INFO,
"Rollover of KSK expected at %s for %s", ksk_expected, zone_name);
811 ret = xmlTextReaderRead(reader);
814 xmlFreeTextReader(reader);
816 log_msg(config, LOG_ERR,
"%s : failed to parse", zonelist_filename);
819 log_msg(config, LOG_ERR,
"Unable to open %s", zonelist_filename);
834 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)
847 char *signer_command;
852 if (datetime == NULL) {
853 log_msg(NULL, LOG_DEBUG,
"Couldn't turn \"now\" into a date, quitting...");
857 if (zone_name == NULL || current_filename == NULL || policy == NULL)
860 log_msg(NULL, LOG_ERR,
"commGenSignConf, NULL policy or zone provided");
866 StrAppend(&old_filename, current_filename);
869 temp_filename = NULL;
870 StrAppend(&temp_filename, current_filename);
873 file = fopen(temp_filename,
"w");
878 log_msg(NULL, LOG_ERR,
"Could not open: %s (%s)", temp_filename,
886 fprintf(file,
"<SignerConfiguration>\n");
887 fprintf(file,
"\t<Zone name=\"%s\">\n", zone_name);
889 fprintf(file,
"\t\t<Signatures>\n");
890 fprintf(file,
"\t\t\t<Resign>PT%dS</Resign>\n", policy->
signature->
resign);
891 fprintf(file,
"\t\t\t<Refresh>PT%dS</Refresh>\n", policy->
signer->
refresh);
892 fprintf(file,
"\t\t\t<Validity>\n");
895 fprintf(file,
"\t\t\t</Validity>\n");
896 fprintf(file,
"\t\t\t<Jitter>PT%dS</Jitter>\n", policy->
signer->
jitter);
897 fprintf(file,
"\t\t\t<InceptionOffset>PT%dS</InceptionOffset>\n", policy->
signature->
clockskew);
898 fprintf(file,
"\t\t</Signatures>\n");
902 fprintf(file,
"\t\t<Denial>\n");
905 fprintf(file,
"\t\t\t<NSEC3>\n");
908 fprintf(file,
"\t\t\t\t<OptOut />\n");
910 fprintf(file,
"\t\t\t\t<Hash>\n");
911 fprintf(file,
"\t\t\t\t\t<Algorithm>%d</Algorithm>\n", policy->
denial->
algorithm);
912 fprintf(file,
"\t\t\t\t\t<Iterations>%d</Iterations>\n", policy->
denial->
iteration);
914 fprintf(file,
"\t\t\t\t\t<Salt>-</Salt>\n");
916 fprintf(file,
"\t\t\t\t\t<Salt>%s</Salt>\n", policy->
denial->
salt);
918 fprintf(file,
"\t\t\t\t</Hash>\n");
919 fprintf(file,
"\t\t\t</NSEC3>\n");
921 fprintf(file,
"\t\t\t<NSEC />\n");
924 fprintf(file,
"\t\t</Denial>\n");
929 fprintf(file,
"\t\t<Keys>\n");
930 fprintf(file,
"\t\t\t<TTL>PT%dS</TTL>\n", policy->
ksk->
ttl);
939 log_msg(NULL, LOG_ERR,
"KsmRequestKeys returned: %d", status);
945 if (status2 == 0 && gencnt == 0) {
946 if(man_key_gen == 1) {
947 log_msg(NULL, LOG_ERR,
"There are no KSKs in the generate state; please use \"ods-ksmutil key generate\" to create some.");
949 log_msg(NULL, LOG_WARNING,
"There are no KSKs in the generate state; ods-enforcerd will create some on its next run.");
952 else if (status2 == 0) {
954 if (status2 == 0 && gencnt == 0) {
955 if(man_key_gen == 1) {
956 log_msg(NULL, LOG_ERR,
"There are no ZSKs in the generate state; please use \"ods-ksmutil key generate\" to create some.");
958 log_msg(NULL, LOG_WARNING,
"There are no ZSKs in the generate state; ods-enforcerd will create some on its next run.");
963 log_msg(NULL, LOG_ERR,
"KsmRequestGenerateCount returned: %d", status2);
967 status = fclose(file);
968 unlink(temp_filename);
976 fprintf(file,
"\t\t</Keys>\n");
980 fprintf(file,
"\t\t<SOA>\n");
981 fprintf(file,
"\t\t\t<TTL>PT%dS</TTL>\n", policy->
signer->
soattl);
982 fprintf(file,
"\t\t\t<Minimum>PT%dS</Minimum>\n", policy->
signer->
soamin);
984 fprintf(file,
"\t\t</SOA>\n");
986 fprintf(file,
"\t</Zone>\n");
987 fprintf(file,
"</SignerConfiguration>\n");
1006 status = fclose(file);
1011 log_msg(NULL, LOG_ERR,
"Could not close: %s", temp_filename);
1018 file = fopen(temp_filename,
"rb");
1022 log_msg(NULL, LOG_ERR,
"Could not reopen: %s", temp_filename);
1028 file2 = fopen(current_filename,
"rb");
1031 if (file2 != NULL) {
1033 while(!feof(file)) {
1034 char1 = fgetc(file);
1036 log_msg(NULL, LOG_ERR,
"Could not read: %s", temp_filename);
1043 char2 = fgetc(file2);
1045 log_msg(NULL, LOG_ERR,
"Could not read: %s", current_filename);
1052 if(char1 != char2) {
1058 status = fclose(file2);
1061 log_msg(NULL, LOG_ERR,
"Could not close: %s", current_filename);
1069 status = fclose(file);
1072 log_msg(NULL, LOG_ERR,
"Could not close: %s", temp_filename);
1083 status = rename(current_filename, old_filename);
1084 if (status != 0 && status != -1)
1087 log_msg(NULL, LOG_ERR,
"Could not rename: %s -> %s", current_filename, old_filename);
1094 if (rename(temp_filename, current_filename) != 0)
1096 log_msg(NULL, LOG_ERR,
"Could not rename: %s -> %s", temp_filename, current_filename);
1102 if (*signer_flag == 1) {
1108 signer_command = NULL;
1109 StrAppend(&signer_command, SIGNER_CLI_UPDATE);
1113 status = system(signer_command);
1116 log_msg(NULL, LOG_ERR,
"Could not call signer engine");
1117 log_msg(NULL, LOG_INFO,
"Will continue: call '%s' to manually update the zone", signer_command);
1121 log_msg(NULL, LOG_INFO,
"Called signer engine: %s", signer_command);
1128 log_msg(NULL, LOG_INFO,
"No change to: %s", current_filename);
1129 if (
remove(temp_filename) != 0)
1131 log_msg(NULL, LOG_ERR,
"Could not remove: %s", temp_filename);
1140 log_msg(NULL, LOG_INFO,
"DSChanged");
1141 status =
NewDSSet(zone_id, zone_name, DSSubmitCmd, DSSubCKA_ID);
1156 FILE *file = (FILE *)context;
1158 fprintf(file,
"\t\t\t<Key>\n");
1159 fprintf(file,
"\t\t\t\t<Flags>%d</Flags>\n", key_data->
keytype);
1160 fprintf(file,
"\t\t\t\t<Algorithm>%d</Algorithm>\n", key_data->
algorithm);
1161 fprintf(file,
"\t\t\t\t<Locator>%s</Locator>\n", key_data->
location);
1165 fprintf(file,
"\t\t\t\t<KSK />\n");
1169 fprintf(file,
"\t\t\t\t<ZSK />\n");
1173 fprintf(file,
"\t\t\t\t<Publish />\n");
1175 fprintf(file,
"\t\t\t</Key>\n");
1176 fprintf(file,
"\n");
1214 int keys_needed = 0;
1215 int keys_in_queue = 0;
1216 int keys_pending_retirement = 0;
1218 int key_pair_id = 0;
1225 if (datetime == NULL) {
1226 log_msg(NULL, LOG_DEBUG,
"Couldn't turn \"now\" into a date, quitting...");
1230 if (policy == NULL) {
1231 log_msg(NULL, LOG_ERR,
"NULL policy sent to allocateKeysToZone");
1237 log_msg(NULL, LOG_ERR,
"Unknown keytype: %i in allocateKeysToZone", key_type);
1251 status =
KsmKeyPredict(policy->
id, key_type, 1, interval, &keys_needed, rollover_scheme, 1);
1253 log_msg(NULL, LOG_ERR,
"Could not predict key requirement for next interval for %s", zone_name);
1261 log_msg(NULL, LOG_ERR,
"Could not count current key numbers for zone %s", zone_name);
1269 log_msg(NULL, LOG_ERR,
"Could not count keys which may retire before the next run (for zone %s)", zone_name);
1275 new_keys = keys_needed - (keys_in_queue - keys_pending_retirement);
1280 for (i=0 ; i < new_keys ; i++){
1284 if (status == -1 || key_pair_id == 0) {
1285 if (man_key_gen == 0) {
1286 log_msg(NULL, LOG_WARNING,
"Not enough keys to satisfy ksk policy for zone: %s", zone_name);
1287 log_msg(NULL, LOG_WARNING,
"ods-enforcerd will create some more keys on its next run");
1290 log_msg(NULL, LOG_ERR,
"Not enough keys to satisfy ksk policy for zone: %s", zone_name);
1291 log_msg(NULL, LOG_ERR,
"please use \"ods-ksmutil key generate\" to create some more keys.");
1295 else if (status != 0) {
1296 log_msg(NULL, LOG_ERR,
"Could not get an unallocated ksk for zone: %s", zone_name);
1301 if (status == -1 || key_pair_id == 0) {
1302 if (man_key_gen == 0) {
1303 log_msg(NULL, LOG_WARNING,
"Not enough keys to satisfy zsk policy for zone: %s", zone_name);
1304 log_msg(NULL, LOG_WARNING,
"ods-enforcerd will create some more keys on its next run");
1307 log_msg(NULL, LOG_ERR,
"Not enough keys to satisfy zsk policy for zone: %s", zone_name);
1308 log_msg(NULL, LOG_ERR,
"please use \"ods-ksmutil key generate\" to create some more keys.");
1312 else if (status != 0) {
1313 log_msg(NULL, LOG_ERR,
"Could not get an unallocated zsk for zone: %s", zone_name);
1317 if(key_pair_id > 0) {
1322 log_msg(NULL, LOG_ERR,
"KsmKeyGetUnallocated returned bad key_id %d for zone: %s; exiting...", key_pair_id, zone_name);
1336 xmlTextReaderPtr reader = NULL;
1337 xmlDocPtr doc = NULL;
1338 xmlXPathContextPtr xpathCtx = NULL;
1339 xmlXPathObjectPtr xpathObj = NULL;
1341 char* temp_char = NULL;
1342 char* tag_name = NULL;
1344 xmlChar *zonelist_expr = (
unsigned char*)
"//Common/ZoneListFile";
1347 reader = xmlNewTextReaderFilename(filename);
1348 if (reader != NULL) {
1349 ret = xmlTextReaderRead(reader);
1351 tag_name = (
char*) xmlTextReaderLocalName(reader);
1353 if (strncmp(tag_name,
"Common", 6) == 0
1354 && xmlTextReaderNodeType(reader) == 1) {
1357 xmlTextReaderExpand(reader);
1358 doc = xmlTextReaderCurrentDoc(reader);
1360 log_msg(NULL, LOG_ERR,
"Error: can not read Common section of %s", filename);
1362 ret = xmlTextReaderRead(reader);
1366 xpathCtx = xmlXPathNewContext(doc);
1367 if(xpathCtx == NULL) {
1368 log_msg(NULL, LOG_ERR,
"Error: can not create XPath context for Common section");
1370 ret = xmlTextReaderRead(reader);
1375 xpathObj = xmlXPathEvalExpression(zonelist_expr, xpathCtx);
1376 if(xpathObj == NULL) {
1377 log_msg(NULL, LOG_ERR,
"Error: unable to evaluate xpath expression: %s", zonelist_expr);
1379 ret = xmlTextReaderRead(reader);
1382 *zone_list_filename = NULL;
1383 temp_char = (
char *)xmlXPathCastToString(xpathObj);
1384 StrAppend(zone_list_filename, temp_char);
1386 xmlXPathFreeObject(xpathObj);
1387 log_msg(NULL, LOG_INFO,
"zonelist filename set to %s.", *zone_list_filename);
1390 ret = xmlTextReaderRead(reader);
1393 xmlFreeTextReader(reader);
1395 log_msg(NULL, LOG_ERR,
"%s : failed to parse", filename);
1399 log_msg(NULL, LOG_ERR,
"Unable to open %s", filename);
1403 xmlXPathFreeContext(xpathCtx);
1444 char* temp_loc = NULL;
1450 hsm_key_t *key = NULL;
1452 log_msg(NULL, LOG_DEBUG,
"Purging keys...");
1457 if (rightnow == NULL) {
1458 log_msg(NULL, LOG_ERR,
"Couldn't turn \"now\" into a date, quitting...");
1463 StrAppend(&sql,
"select distinct id, location from KEYDATA_VIEW where state = 6 ");
1465 if (policy_id != -1) {
1477 while (status == 0) {
1479 DbInt(row, 0, &temp_id);
1488 log_msg(NULL, LOG_ERR,
"DbDateDiff failed\n");
1497 StrAppend(&sql1,
" or state = 6 and DEAD > ");
1552 key = hsm_find_key_by_id(NULL, temp_loc);
1555 log_msg(NULL, LOG_ERR,
"Key not found: %s\n", temp_loc);
1563 status = hsm_remove_key(NULL, key);
1568 log_msg(NULL, LOG_INFO,
"Key remove successful: %s\n", temp_loc);
1570 log_msg(NULL, LOG_ERR,
"Key remove failed: %s\n", temp_loc);
1601 int NewDSSet(
int zone_id,
const char* zone_name,
const char* DSSubmitCmd,
int DSSubCKA_ID) {
1615 char* count_clause = NULL;
1616 char* where_clause = NULL;
1618 int active_count = -1;
1623 char* ds_buffer = NULL;
1624 char* ds_seen_buffer = NULL;
1625 char* temp_char = NULL;
1633 hsm_key_t *key = NULL;
1634 ldns_rr *dnskey_rr = NULL;
1635 hsm_sign_params_t *sign_params = NULL;
1638 int bytes_written = -1;
1640 struct stat stat_ret;
1642 nchar = snprintf(buffer,
sizeof(buffer),
"(%d, %d, %d, %d, %d, %d, %d, %d)",
1646 if (nchar >=
sizeof(buffer)) {
1657 if (zone_id != -1) {
1665 log_msg(NULL, LOG_ERR,
"Error: failed to find ID of key to retire\n");
1669 if (active_count > 0) {
1672 StrAppend(&where_clause,
"select id from KEYDATA_VIEW where state = 4 and keytype = 257 and zone_id = ");
1674 StrAppend(&where_clause,
" and retire = (select min(retire) from KEYDATA_VIEW where state = 4 and keytype = 257 and zone_id = ");
1683 log_msg(NULL, LOG_ERR,
"Error: failed to find ID of key to retire\n");
1693 if (zone_id != -1) {
1715 keyids =
MemMalloc(count *
sizeof(
int));
1723 if (zone_id != -1) {
1735 while (status == 0) {
1736 status =
KsmKey(result, &data);
1767 for (j = 0; j < i; ++j) {
1771 snprintf(buffer,
sizeof(buffer),
"%d", keyids[j]);
1784 log_msg(NULL, LOG_INFO,
"DS Record set has changed, the current set looks like:");
1789 status =
KsmKey(result3, &data3);
1790 while (status == 0) {
1793 key = hsm_find_key_by_id(NULL, data3.
location);
1796 log_msg(NULL, LOG_ERR,
"Key %s in DB but not repository.", data3.
location);
1804 sign_params = hsm_sign_params_new();
1805 sign_params->owner = ldns_rdf_new_frm_str(LDNS_RDF_TYPE_DNAME, zone_name);
1806 sign_params->algorithm = data3.
algorithm;
1807 sign_params->flags = LDNS_KEY_ZONE_KEY;
1808 sign_params->flags += LDNS_KEY_SEP_KEY;
1809 dnskey_rr = hsm_get_dnskey(NULL, key, sign_params);
1819 ldns_rr_set_ttl(dnskey_rr, rrttl);
1823 temp_char = ldns_rr2str(dnskey_rr);
1824 ldns_rr_free(dnskey_rr);
1827 for (i = 0; temp_char[i]; ++i) {
1828 if (temp_char[i] ==
'\t') {
1832 log_msg(NULL, LOG_INFO,
"%s", temp_char);
1836 for (i = 0; temp_char[i]; ++i) {
1837 if (temp_char[i] ==
';') {
1838 temp_char[i] =
'\n';
1839 temp_char[i+1] =
'\0';
1867 hsm_sign_params_free(sign_params);
1869 status =
KsmKey(result3, &data3);
1879 if (DSSubmitCmd[0] !=
'\0') {
1881 if (stat(DSSubmitCmd, &stat_ret) != 0) {
1882 log_msg(NULL, LOG_WARNING,
"Cannot stat file %s: %s", DSSubmitCmd, strerror(errno));
1885 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)) {
1886 log_msg(NULL, LOG_WARNING,
"File %s is not executable", DSSubmitCmd);
1891 fp = popen(DSSubmitCmd,
"w");
1893 log_msg(NULL, LOG_ERR,
"Failed to run command: %s: %s", DSSubmitCmd, strerror(errno));
1897 bytes_written = fprintf(fp,
"%s", ds_buffer);
1898 if (bytes_written < 0) {
1899 log_msg(NULL, LOG_ERR,
"Failed to write to %s: %s", DSSubmitCmd, strerror(errno));
1903 if (pclose(fp) == -1) {
1904 log_msg(NULL, LOG_ERR,
"Failed to close %s: %s", DSSubmitCmd, strerror(errno));
1915 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);
1927 char *hsm_error_message = NULL;
1929 result = hsm_check_context(*ctx);
1932 if (result != HSM_OK) {
1935 hsm_destroy_context(*ctx);
1938 result = hsm_close();
1941 result = hsm_open(config->
configfile, hsm_check_pin);
1943 result = hsm_open(OPENDNSSEC_CONFIG_FILE, hsm_check_pin);
1946 hsm_error_message = hsm_get_error(*ctx);
1947 if (hsm_error_message) {
1948 log_msg(config, LOG_ERR, hsm_error_message);
1949 free(hsm_error_message);
1956 log_msg(config, LOG_ERR,
"hsm_open() result: HSM error");
1958 case HSM_PIN_INCORRECT:
1959 log_msg(config, LOG_ERR,
"hsm_open() result: incorrect PIN");
1961 case HSM_CONFIG_FILE_ERROR:
1962 log_msg(config, LOG_ERR,
"hsm_open() result: config file error");
1964 case HSM_REPOSITORY_NOT_FOUND:
1965 log_msg(config, LOG_ERR,
"hsm_open() result: repository not found");
1967 case HSM_NO_REPOSITORIES:
1968 log_msg(config, LOG_ERR,
"hsm_open() result: no repositories");
1971 log_msg(config, LOG_ERR,
"hsm_open() result: %d", result);
1977 log_msg(config, LOG_INFO,
"HSM reopened successfully.");
1978 *ctx = hsm_create_context();
1980 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)
#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 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
int DbDateDiff(const char *start, int delta, int sign, char *buffer, size_t buflen)
void KsmParameterCollectionCache(int enable)
int do_communication(DAEMONCONFIG *config, KSM_POLICY *policy)
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 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 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)