61 #include <libhsmdns.h>
62 #include <ldns/ldns.h>
64 #include <libxml/tree.h>
65 #include <libxml/parser.h>
66 #include <libxml/xpointer.h>
67 #include <libxml/xpath.h>
68 #include <libxml/xpathInternals.h>
69 #include <libxml/relaxng.h>
70 #include <libxml/xmlreader.h>
71 #include <libxml/xmlsave.h>
73 #define MAX(a, b) ((a) > (b) ? (a) : (b))
77 #define DURATION_TYPE 1
81 #define ROLLOVER_TYPE 5
82 #define INT_TYPE_NO_FREE 6
85 # define MAXPATHLEN 4096
90 #define DEFAULT_LOG_FACILITY LOG_DAEMON
92 #define DEFAULT_LOG_FACILITY LOG_USER
98 char *
config = (
char *) OPENDNSSEC_CONFIG_FILE;
115 static int all_flag = 0;
116 static int ds_flag = 0;
117 static int retire_flag = 1;
118 static int verbose_flag = 0;
119 static int xml_flag = 1;
120 static int td_flag = 0;
122 static int restart_enforcerd(
void);
129 " --version aka -V\n");
137 "\tImport config into a database (deletes current contents)\n");
144 " start|stop|notify\n"
145 "\tStart, stop or SIGHUP the ods-enforcerd\n");
156 "\tUpdate database from config\n");
164 "\t--zone <zone> aka -z\n"
165 "\t[--policy <policy>] aka -p\n"
166 "\t[--signerconf <signerconf.xml>] aka -s\n"
167 "\t[--input <input>] aka -i\n"
168 "\t[--output <output>] aka -o\n"
169 "\t[--no-xml] aka -m\n");
177 "\t--zone <zone> | --all aka -z / -a\n"
178 "\t[--no-xml] aka -m\n");
192 "usage: %s [-c <config> | --config <config>] zone \n\n",
203 " repository list\n");
211 "\t--policy [policy_name] | --all aka -p / -a\n");
239 "usage: %s [-c <config> | --config <config>] \n\n",
253 "\t--zone <zone> | --all aka -z / -a\n"
255 "\t(will appear soon:\n"
256 "\t[--keystate <state>] aka -e\n"
257 "\t[--keytype <type>] aka -t\n"
268 "\t--zone <zone> | --all aka -z / -a\n"
269 "\t[--keystate <state>] aka -e\n"
270 "\t[--keytype <type>] aka -t\n"
271 "\t[--ds] aka -d\n");
279 "\t--cka_id <CKA_ID> aka -k\n"
280 "\t--repository <repository> aka -r\n"
281 "\t--zone <zone> aka -z\n"
282 "\t--bits <size> aka -b\n"
283 "\t--algorithm <algorithm> aka -g\n"
284 "\t--keystate <state> aka -e\n"
285 "\t--keytype <type> aka -t\n"
286 "\t--time <time> aka -w\n"
287 "\t[--retire <retire>] aka -y\n");
295 "\t--zone zone [--keytype <type>] aka -z\n"
297 "\t--policy policy [--keytype <type>] aka -p\n");
305 "\t--zone <zone> aka -z\n"
307 "\t--policy <policy> aka -p\n");
315 "\t--policy <policy>\n"
316 "\t--interval <interval>\n");
324 "\t--zone <zone> aka -z\n"
325 "\t--keytag <keytag> | --cka_id <CKA_ID> aka -x / -k\n");
334 "\t--zone <zone> aka -z\n"
335 "\t--keytag <keytag> | --cka_id <CKA_ID> aka -x / -k\n"
343 "usage: %s [-c <config> | --config <config>] \n\n",
360 "\t--repository <repository> aka -r\n"
362 "\t--repository <repository> aka -r\n"
364 "\t--repository <repository> aka -r\n"
366 "\t--repository <repository> aka -r\n"
368 "\t--repository <repository> aka -r\n");
376 "\t[--zone <zone>]\n");
384 "\t[--output <output>] aka -o\n");
392 " zonelist import\n");
399 "usage: %s [-c <config> | --config <config>] command [options]\n\n",
432 "\n\tAllowed date/time strings are of the form:\n"
434 "\tYYYYMMDD[HH[MM[SS]]] (all numeric)\n"
436 "\tor D-MMM-YYYY[:| ]HH[:MM[:SS]] (alphabetic month)\n"
437 "\tor DD-MMM-YYYY[:| ]HH[:MM[:SS]] (alphabetic month)\n"
438 "\tor YYYY-MMM-DD[:| ]HH[:MM[:SS]] (alphabetic month)\n"
440 "\tD-MM-YYYY[:| ]HH[:MM[:SS]] (numeric month)\n"
441 "\tDD-MM-YYYY[:| ]HH[:MM[:SS]] (numeric month)\n"
442 "\tor YYYY-MM-DD[:| ]HH[:MM[:SS]] (numeric month)\n"
444 "\t... and the distinction between them is given by the location of the\n"
452 "key states: GENERATE|PUBLISH|READY|ACTIVE|RETIRE|DEAD\n");
459 "key types: KSK|ZSK\n");
469 FILE* lock_fd = NULL;
470 char* zone_list_filename;
475 char *dbschema = NULL;
479 char *password = NULL;
484 char* setup_command = NULL;
485 char* lock_filename = NULL;
488 printf(
"*WARNING* This will erase all data in the database; are you sure? [y/N] ");
490 user_certain = getchar();
491 if (user_certain !=
'y' && user_certain !=
'Y') {
492 printf(
"Okay, quitting...\n");
499 status =
get_db_details(&dbschema, &host, &port, &user, &password);
518 lock_fd = fopen(lock_filename,
"w");
521 printf(
"Error getting db lock\n");
522 if (lock_fd != NULL) {
543 if (system(setup_command) != 0)
545 printf(
"Could not call db setup command:\n\t%s\n", setup_command);
561 printf(
"Couldn't fix permissions on file %s\n", dbschema);
562 printf(
"Will coninue with setup, but you may need to manually change ownership\n");
572 printf(
"Failed to connect to database, username too long.\n");
585 printf(
"Failed to connect to database, password too long.\n");
607 if (password != NULL) {
609 StrAppend(&setup_command, quoted_password);
617 if (system(setup_command) != 0)
619 printf(
"Could not call db setup command:\n\t%s\n", setup_command);
632 status =
DbConnect(&dbhandle, dbschema, host, password, user, port);
634 printf(
"Failed to connect to database\n");
657 printf(
"Failed to read conf.xml\n");
668 printf(
"Failed to update repositories\n");
680 printf(
"Failed to update policies\n");
681 printf(
"SETUP FAILED\n");
696 printf(
"Failed to update zones\n");
719 FILE* lock_fd = NULL;
720 char* zone_list_filename = NULL;
721 char* kasp_filename = NULL;
723 int done_something = 0;
728 printf(
"Failed to connect to database\n");
737 if (strncmp(qualifier,
"ZONELIST", 8) == 0 ||
738 strncmp(qualifier,
"KASP", 4) == 0 ||
739 strncmp(qualifier,
"ALL", 3) == 0) {
742 printf(
"Failed to read conf.xml\n");
752 if (strncmp(qualifier,
"CONF", 4) == 0 ||
753 strncmp(qualifier,
"ALL", 3) == 0) {
756 printf(
"Failed to update repositories\n");
758 if (strncmp(qualifier,
"ALL", 3) == 0) {
771 if (strncmp(qualifier,
"KASP", 4) == 0 ||
772 strncmp(qualifier,
"ALL", 3) == 0) {
775 printf(
"Failed to update policies\n");
788 if (strncmp(qualifier,
"ZONELIST", 8) == 0 ||
789 strncmp(qualifier,
"ALL", 3) == 0) {
792 printf(
"Failed to update zones\n");
804 if (done_something == 0) {
805 printf(
"Unrecognised command update %s. Please specify one of:\n", qualifier);
809 if (restart_enforcerd() != 0)
811 fprintf(stderr,
"Could not HUP ods-enforcerd\n");
821 if (kasp_filename != NULL) {
824 if (zone_list_filename != NULL) {
843 FILE* lock_fd = NULL;
844 char* zonelist_filename = NULL;
845 char* backup_filename = NULL;
847 char* sig_conf_name = NULL;
848 char* input_name = NULL;
849 char* output_name = NULL;
856 xmlDocPtr doc = NULL;
862 printf(
"Couldn't malloc path: %s\n", strerror(errno));
868 printf(
"Please specify a zone with the --zone option\n");
881 StrAppend(&sig_conf_name, OPENDNSSEC_STATE_DIR);
895 StrAppend(&input_name, OPENDNSSEC_STATE_DIR);
908 StrAppend(&output_name, OPENDNSSEC_STATE_DIR);
925 printf(
"couldn't read zonelist\n");
940 printf(
"Failed to connect to database\n");
952 printf(
"Error, can't find policy : %s\n",
o_policy);
953 printf(
"Failed to update zones\n");
961 status =
KsmImportZone(
o_zone, policy_id, 1, &new_zone, sig_conf_name, input_name, output_name);
964 printf(
"Failed to Import zone %s; it already exists\n",
o_zone);
965 }
else if (status == -3) {
966 printf(
"Failed to Import zone %s; it already exists both with and without a trailing dot\n",
o_zone);
968 printf(
"Failed to Import zone\n");
982 printf(
"Can't retrieve shared-keys parameter for policy\n");
992 printf(
"Can't retrieve shared-keys parameter for policy\n");
1003 if (data.
value == 1) {
1006 printf(
"Failed to Link Keys to zone\n");
1024 if (xml_flag == 1) {
1027 xmlKeepBlanksDefault(0);
1028 xmlTreeIndentString =
"\t";
1041 StrAppend(&backup_filename, zonelist_filename);
1043 status =
backup_file(zonelist_filename, backup_filename);
1051 status = xmlSaveFormatFile(zonelist_filename, doc, 1);
1056 printf(
"couldn't save zonelist\n");
1064 if (xml_flag == 0) {
1065 printf(
"Imported zone: %s into database only, please run \"ods-ksmutil zonelist export\" to update zonelist.xml\n",
o_zone);
1067 printf(
"Imported zone: %s\n",
o_zone);
1081 char* zonelist_filename = NULL;
1082 char* backup_filename = NULL;
1087 xmlDocPtr doc = NULL;
1094 FILE* lock_fd = NULL;
1097 if (all_flag &&
o_zone != NULL) {
1098 printf(
"can not use --all with --zone\n");
1101 else if (!all_flag &&
o_zone == NULL) {
1102 printf(
"please specify either --zone <zone> or --all\n");
1107 if (all_flag == 1) {
1108 printf(
"*WARNING* This will remove all zones from OpenDNSSEC; are you sure? [y/N] ");
1110 user_certain = getchar();
1111 if (user_certain !=
'y' && user_certain !=
'Y') {
1112 printf(
"Okay, quitting...\n");
1120 printf(
"Failed to connect to database\n");
1133 if (xml_flag == 1) {
1137 printf(
"couldn't read zonelist\n");
1162 StrAppend(&backup_filename, zonelist_filename);
1164 status =
backup_file(zonelist_filename, backup_filename);
1173 status = xmlSaveFormatFile(zonelist_filename, doc, 1);
1177 printf(
"Could not save %s\n", zonelist_filename);
1188 if (all_flag == 0) {
1191 printf(
"Couldn't find zone %s\n",
o_zone);
1201 printf(
"Error: failed to mark keys as dead in database\n");
1210 printf(
"Error: failed to remove zone%s from database\n", (all_flag == 1) ?
"s" :
"");
1216 if (all_flag == 0) {
1217 if (system(SIGNER_CLI_UPDATE) != 0)
1219 printf(
"Could not call signer engine\n");
1226 if (xml_flag == 0) {
1227 printf(
"Deleted zone: %s from database only, please run \"ods-ksmutil zonelist export\" to update zonelist.xml\n",
o_zone);
1241 FILE* lock_fd = NULL;
1243 char* zonelist_filename = NULL;
1246 xmlTextReaderPtr reader = NULL;
1248 char* tag_name = NULL;
1250 int file_zone_count = 0;
1256 char* temp_name = NULL;
1263 printf(
"couldn't read zonelist\n");
1264 if (zonelist_filename != NULL) {
1273 printf(
"Failed to connect to database\n");
1279 reader = xmlNewTextReaderFilename(zonelist_filename);
1280 if (reader != NULL) {
1281 ret = xmlTextReaderRead(reader);
1283 tag_name = (
char*) xmlTextReaderLocalName(reader);
1285 if (strncmp(tag_name,
"Zone", 4) == 0
1286 && strncmp(tag_name,
"ZoneList", 8) != 0
1287 && xmlTextReaderNodeType(reader) == 1) {
1291 ret = xmlTextReaderRead(reader);
1294 xmlFreeTextReader(reader);
1296 printf(
"%s : failed to parse\n", zonelist_filename);
1300 printf(
"Unable to open %s\n", zonelist_filename);
1305 zone_ids =
MemMalloc(file_zone_count *
sizeof(
int));
1311 if (file_zone_count != 0) {
1312 StrAppend(&sql,
"select name from zones where id not in (");
1313 for (j = 0; j < file_zone_count; ++j) {
1317 snprintf(buffer,
sizeof(buffer),
"%d", zone_ids[j]);
1322 StrAppend(&sql,
"select name from zones");
1328 while (status == 0) {
1332 printf(
"Found zone %s in DB but not zonelist.\n", temp_name);
1349 if (file_zone_count == 0) {
1350 printf(
"No zones in DB or zonelist.\n");
1376 char *case_keytype = NULL;
1377 char *case_keystate = NULL;
1378 char *zone_name = NULL;
1381 hsm_key_t *key = NULL;
1382 ldns_rr *dnskey_rr = NULL;
1383 ldns_rr *ds_sha1_rr = NULL;
1384 ldns_rr *ds_sha256_rr = NULL;
1385 hsm_sign_params_t *sign_params = NULL;
1393 int done_something = 0;
1400 if (strncmp(case_keystate,
"KEYPUBLISH", 10) == 0 || strncmp(
o_keystate,
"10", 2) == 0) {
1403 else if (strncmp(case_keystate,
"GENERATE", 8) == 0 || strncmp(
o_keystate,
"1", 1) == 0) {
1406 else if (strncmp(case_keystate,
"PUBLISH", 7) == 0 || strncmp(
o_keystate,
"2", 1) == 0) {
1409 else if (strncmp(case_keystate,
"READY", 5) == 0 || strncmp(
o_keystate,
"3", 1) == 0) {
1412 else if (strncmp(case_keystate,
"ACTIVE", 6) == 0 || strncmp(
o_keystate,
"4", 1) == 0) {
1415 else if (strncmp(case_keystate,
"RETIRE", 6) == 0 || strncmp(
o_keystate,
"5", 1) == 0) {
1418 else if (strncmp(case_keystate,
"DEAD", 4) == 0 || strncmp(
o_keystate,
"6", 1) == 0) {
1421 else if (strncmp(case_keystate,
"DSSUB", 5) == 0 || strncmp(
o_keystate,
"7", 1) == 0) {
1424 else if (strncmp(case_keystate,
"DSPUBLISH", 9) == 0 || strncmp(
o_keystate,
"8", 1) == 0) {
1427 else if (strncmp(case_keystate,
"DSREADY", 7) == 0 || strncmp(
o_keystate,
"9", 1) == 0) {
1431 printf(
"Error: Unrecognised state %s; should be one of GENERATE, PUBLISH, READY, ACTIVE, RETIRE, DEAD, DSSUB, DSPUBLISH, DSREADY or KEYPUBLISH\n",
o_keystate);
1443 if (strncmp(case_keytype,
"KSK", 3) == 0 || strncmp(
o_keytype,
"257", 3) == 0) {
1446 else if (strncmp(case_keytype,
"ZSK", 3) == 0 || strncmp(
o_keytype,
"256", 3) == 0) {
1450 printf(
"Error: Unrecognised keytype %s; should be one of KSK or ZSK\n",
o_keytype);
1461 printf(
"Failed to connect to database\n");
1473 printf(
"Error: unable to find a zone named \"%s\" in database\n",
o_zone);
1479 status = hsm_open(
config, hsm_prompt_pin, NULL);
1481 hsm_print_error(NULL);
1486 if (state_id != -1) {
1489 nchar = snprintf(buffer,
sizeof(buffer),
"(%d, %d, %d, %d, %d, %d)",
1492 if (nchar >=
sizeof(buffer)) {
1500 if (zone_id != -1) {
1508 status =
KsmKey(result, &data);
1509 while (status == 0) {
1512 key = hsm_find_key_by_id(NULL, data.
location);
1515 printf(
"Key %s in DB but not repository\n", data.
location);
1519 sign_params = hsm_sign_params_new();
1521 if (zone_id == -1) {
1524 printf(
"Error: unable to find zone name for id %d\n", zone_id);
1525 hsm_sign_params_free(sign_params);
1528 sign_params->owner = ldns_rdf_new_frm_str(LDNS_RDF_TYPE_DNAME, zone_name);
1532 sign_params->owner = ldns_rdf_new_frm_str(LDNS_RDF_TYPE_DNAME,
o_zone);
1535 sign_params->algorithm = data.
algorithm;
1536 sign_params->flags = LDNS_KEY_ZONE_KEY;
1538 sign_params->flags += LDNS_KEY_SEP_KEY;
1540 dnskey_rr = hsm_get_dnskey(NULL, key, sign_params);
1541 sign_params->keytag = ldns_calc_keytag(dnskey_rr);
1545 ldns_rr_print(stdout, dnskey_rr);
1550 ds_sha1_rr = ldns_key_rr2ds(dnskey_rr, LDNS_SHA1);
1551 ldns_rr_print(stdout, ds_sha1_rr);
1554 ds_sha256_rr = ldns_key_rr2ds(dnskey_rr, LDNS_SHA256);
1555 ldns_rr_print(stdout, ds_sha256_rr);
1560 hsm_sign_params_free(sign_params);
1562 status =
KsmKey(result, &data);
1574 if (!done_something) {
1575 if (state_id != -1) {
1578 printf(
"No keys in READY state or higher to export.\n");
1584 if (dnskey_rr != NULL) {
1585 ldns_rr_free(dnskey_rr);
1587 if (ds_sha1_rr != NULL) {
1588 ldns_rr_free(ds_sha1_rr);
1590 if (ds_sha256_rr != NULL) {
1591 ldns_rr_free(ds_sha256_rr);
1610 xmlDocPtr doc = xmlNewDoc((
const xmlChar *)
"1.0");
1617 if (all_flag &&
o_policy != NULL) {
1618 printf(
"can not use --all with --policy\n");
1621 else if (!all_flag &&
o_policy == NULL) {
1622 printf(
"please specify either --policy <policy> or --all\n");
1629 printf(
"Failed to connect to database\n");
1648 policy->
zone == NULL || policy->
parent == NULL ||
1649 policy->
keys == NULL ||
1650 policy->
ksk == NULL || policy->
zsk == NULL ||
1652 fprintf(stderr,
"Malloc for policy struct failed\n");
1657 xmlKeepBlanksDefault(0);
1658 xmlTreeIndentString =
" ";
1659 root = xmlNewDocNode(doc, NULL, (
const xmlChar *)
"KASP", NULL);
1660 (void) xmlDocSetRootElement(doc, root);
1669 while (status == 0) {
1679 xmlSaveFormatFile(
"-", doc, 1);
1700 xmlDocPtr doc = xmlNewDoc((
const xmlChar *)
"1.0");
1703 int prev_policy_id = -1;
1710 printf(
"Failed to connect to database\n");
1717 fprintf(stderr,
"Malloc for zone struct failed\n");
1722 xmlKeepBlanksDefault(0);
1723 xmlTreeIndentString =
" ";
1724 root = xmlNewDocNode(doc, NULL, (
const xmlChar *)
"ZoneList", NULL);
1725 (void) xmlDocSetRootElement(doc, root);
1731 status =
KsmZone(result, zone);
1733 while (status == 0) {
1734 if (zone->
policy_id != prev_policy_id) {
1738 fprintf(stderr,
"Couldn't get name for policy with ID: %d, exiting...\n", zone->
policy_id);
1745 status =
KsmZone(result, zone);
1750 xmlSaveFormatFile(
"-", doc, 1);
1768 FILE* lock_fd = NULL;
1788 printf(
"Failed to connect to database\n");
1818 if (data.
value == 1) {
1819 printf(
"*WARNING* This zone shares keys with others, all instances of the active key on this zone will be retired; are you sure? [y/N] ");
1821 user_certain = getchar();
1822 if (user_certain !=
'y' && user_certain !=
'Y') {
1823 printf(
"Okay, quitting...\n");
1829 status =
keyRoll(zone_id, -1, key_type);
1839 if (restart_enforcerd() != 0)
1841 fprintf(stderr,
"Could not HUP ods-enforcerd\n");
1857 FILE* lock_fd = NULL;
1861 int zone_count = -1;
1878 printf(
"Failed to connect to database\n");
1885 printf(
"Error, can't find policy : %s\n",
o_policy);
1891 printf(
"*WARNING* This will roll all keys on the policy; are you sure? [y/N] ");
1893 user_certain = getchar();
1894 if (user_certain !=
'y' && user_certain !=
'Y') {
1895 printf(
"Okay, quitting...\n");
1910 if (zone_count == 0) {
1911 printf(
"No zones on policy; nothing to roll\n");
1916 printf(
"Couldn't count zones on policy; quitting...\n");
1921 status =
keyRoll(-1, policy_id, key_type);
1927 if (restart_enforcerd() != 0)
1929 fprintf(stderr,
"Could not HUP ods-enforcerd\n");
1950 FILE* lock_fd = NULL;
1955 printf(
"Failed to connect to database\n");
1964 printf(
"Error: unable to find a policy named \"%s\" in database\n",
o_policy);
1978 printf(
"Error: unable to find a zone named \"%s\" in database\n",
o_zone);
1988 printf(
"Error: failed to purge dead keys\n");
2012 FILE* lock_fd = NULL;
2017 if (datetime == NULL) {
2018 printf(
"Couldn't turn \"now\" into a date, quitting...\n");
2025 printf(
"Failed to connect to database\n");
2035 printf(
"Error: unable to find a repository named \"%s\" in database\n",
o_repository);
2043 if (strncmp(qualifier,
"PREPARE", 7) == 0 ||
2044 strncmp(qualifier,
"DONE", 4) == 0 ) {
2047 printf(
"There were no keys to mark\n");
2049 else if (status != 0) {
2050 printf(
"Error: failed to mark pre_backup as done\n");
2055 if (strncmp(qualifier,
"PREPARE", 7) == 0) {
2057 printf(
"Marked repository %s as pre-backed up at %s\n",
o_repository, datetime);
2059 printf(
"Marked all repositories as pre-backed up at %s\n", datetime);
2066 if (strncmp(qualifier,
"COMMIT", 6) == 0 ||
2067 strncmp(qualifier,
"DONE", 4) == 0 ) {
2070 printf(
"There were no keys to mark\n");
2072 else if (status != 0) {
2073 printf(
"Error: failed to mark backup as done\n");
2079 printf(
"Marked repository %s as backed up at %s\n",
o_repository, datetime);
2081 printf(
"Marked all repositories as backed up at %s\n", datetime);
2087 if (strncmp(qualifier,
"ROLLBACK", 6) == 0 ) {
2090 printf(
"There were no keys to rollback\n");
2092 else if (status != 0) {
2093 printf(
"Error: failed to mark backup as done\n");
2099 printf(
"Rolled back pre-backup of repository %s\n",
o_repository);
2101 printf(
"Rolled back pre-backup of all repositories\n");
2122 int qualifier_id = -1;
2126 FILE* lock_fd = NULL;
2131 printf(
"Failed to connect to database\n");
2144 printf(
"Error: unable to find a zone named \"%s\" in database\n",
o_zone);
2151 printf(
"Rollovers:\n");
2156 printf(
"Error: failed to list rollovers\n");
2178 int qualifier_id = -1;
2182 FILE* lock_fd = NULL;
2187 printf(
"Failed to connect to database\n");
2196 printf(
"Error: unable to find a repository named \"%s\" in database\n",
o_repository);
2202 printf(
"Backups:\n");
2206 printf(
"Error: failed to list backups\n");
2229 FILE* lock_fd = NULL;
2234 printf(
"Failed to connect to database\n");
2239 printf(
"Repositories:\n");
2244 printf(
"Error: failed to list repositories\n");
2245 if (lock_fd != NULL) {
2270 FILE* lock_fd = NULL;
2275 printf(
"Failed to connect to database\n");
2280 printf(
"Policies:\n");
2285 printf(
"Error: failed to list policies\n");
2306 int qualifier_id = -1;
2310 FILE* lock_fd = NULL;
2315 printf(
"Failed to connect to database\n");
2328 printf(
"Error: unable to find a zone named \"%s\" in database\n",
o_zone);
2340 printf(
"Error: failed to list keys\n");
2367 int keytag_int = -1;
2368 int temp_key_state = -1;
2369 int temp_keypair_id = -1;
2370 char* temp_cka_id = NULL;
2375 FILE* lock_fd = NULL;
2380 if (datetime == NULL) {
2381 printf(
"Couldn't turn \"now\" into a date, quitting...\n");
2387 printf(
"*WARNING* This will retire the currently active KSK; are you sure? [y/N] ");
2389 user_certain = getchar();
2390 if (user_certain !=
'y' && user_certain !=
'Y') {
2391 printf(
"Okay, quitting...\n");
2398 printf(
"Failed to connect to database\n");
2412 printf(
"Error: unable to find a zone named \"%s\" in database\n",
o_zone);
2425 printf(
"Error: Unable to convert keytag \"%s\"; to an integer\n",
o_keytag);
2431 printf(
"Error: keytag \"%s\"; should be numeric only\n",
o_keytag);
2441 printf(
"Please provide a zone or details of the key to roll\n");
2450 printf(
"Error: failed to count active keys\n");
2457 if (key_count < 2) {
2458 printf(
"Error: completing this action would leave no active keys on zone, quitting...\n");
2467 printf(
"Error: failed to find policy for zone\n");
2476 printf(
"Old key retired\n");
2478 printf(
"Old key NOT retired\n");
2486 status =
CountKeys(&zone_id, keytag_int,
o_cka_id, &key_count, &temp_cka_id, &temp_key_state, &temp_keypair_id);
2488 printf(
"Error: failed to count keys\n");
2495 if (key_count > 1) {
2496 printf(
"More than one key matched your parameters, please include more information from the above keys\n");
2504 printf(
"No keys in the ACTIVE state matched your parameters, please check the parameters\n");
2512 printf(
"Error: failed to count active keys\n");
2519 if (key_count < 2) {
2520 printf(
"Error: completing this action would leave no active keys on zone, quitting...\n");
2529 printf(
"Error: failed to find policy for zone\n");
2540 printf(
"Key %s retired\n", temp_cka_id);
2567 int retired_count = -1;
2568 int keytag_int = -1;
2569 int temp_key_state = -1;
2570 int temp_keypair_id = -1;
2571 char* temp_cka_id = NULL;
2576 FILE* lock_fd = NULL;
2583 if (datetime == NULL) {
2584 printf(
"Couldn't turn \"now\" into a date, quitting...\n");
2591 printf(
"Please provide a keytag or a CKA_ID for the key (CKA_ID will be used if both are provided\n");
2599 printf(
"*WARNING* This will retire the currently active KSK; are you sure? [y/N] ");
2601 user_certain = getchar();
2602 if (user_certain !=
'y' && user_certain !=
'Y') {
2603 printf(
"Okay, quitting...\n");
2610 printf(
"Failed to connect to database\n");
2621 printf(
"Please specify a zone using the --zone flag\n");
2627 else if (
o_zone != NULL) {
2634 printf(
"Error: unable to find a zone named \"%s\" in database\n",
o_zone);
2641 else if (all_flag) {
2642 printf(
"*WARNING* This will act on every zone where this key is in use; are you sure? [y/N] ");
2644 user_certain = getchar();
2645 if (user_certain !=
'y' && user_certain !=
'Y') {
2646 printf(
"Okay, quitting...\n");
2658 printf(
"Error: Unable to convert keytag \"%s\"; to an integer\n",
o_keytag);
2664 printf(
"Error: keytag \"%s\"; should be numeric only\n",
o_keytag);
2675 status =
CountKeys(&zone_id, keytag_int,
o_cka_id, &key_count, &temp_cka_id, &temp_key_state, &temp_keypair_id);
2677 printf(
"Error: failed to count keys\n");
2684 if (key_count > 1) {
2685 printf(
"More than one key matched your parameters, please include more information from the above keys\n");
2693 printf(
"Key is already active\n");
2700 if (key_count == 0) {
2701 printf(
"No keys in the READY state matched your parameters, please check the parameters\n");
2710 printf(
"Error: failed to find policy for zone\n");
2717 status =
MarkDSSeen(temp_keypair_id, zone_id, policy_id, datetime, temp_key_state);
2721 snprintf(logmsg, 256,
"Key %s made %s", temp_cka_id, (temp_key_state ==
KSM_STATE_READY) ?
"active" :
"into standby");
2722 printf(
"%s\n", logmsg);
2726 syslog(LOG_INFO,
"%s", logmsg);
2733 if (retire_flag == 1) {
2738 printf(
"Error: failed to count active keys\n");
2745 if (key_count < 2) {
2750 printf(
"Error: failed to count retired keys\n");
2759 if (retired_count != 0) {
2760 printf(
"Error: retiring a key would leave no active keys on zone, skipping...\n");
2765 if (restart_enforcerd() != 0)
2767 fprintf(stderr,
"Could not HUP ods-enforcerd\n");
2777 printf(
"Old key retired\n");
2779 printf(
"Old key NOT retired\n");
2782 printf(
"Old key NOT retired\n");
2787 if (restart_enforcerd() != 0)
2789 fprintf(stderr,
"Could not HUP ods-enforcerd\n");
2811 char* case_keytype = NULL;
2812 char* case_algorithm = NULL;
2813 char* case_state = NULL;
2818 int cka_id_exists = -1;
2819 int keytype_id = -1;
2826 DB_ID keypair_id = 0;
2835 FILE* lock_fd = NULL;
2845 printf(
"Error: please specify a CKA_ID with the --cka_id <CKA_ID>\n");
2849 printf(
"Error: please specify a repository with the --repository <repository>\n");
2853 printf(
"Error: please specify a zone with the --zone <zone>\n");
2857 printf(
"Error: please specify the number of bits with the --bits <size>\n");
2861 printf(
"Error: please specify the algorithm with the --algorithm <algorithm>\n");
2865 printf(
"Error: please specify the state with the --keystate <state>\n");
2869 printf(
"Error: please specify a keytype, KSK or ZSK, with the --keytype <type>\n");
2873 printf(
"Error: please specify the time of when the key entered the given state with the --time <time>\n");
2880 printf(
"Failed to connect to database\n");
2888 printf(
"Error: unable to find a repository named \"%s\" in database\n",
o_repository);
2900 printf(
"Error: unable to find a zone named \"%s\" in database\n",
o_zone);
2912 if (cka_id_exists == 1) {
2913 printf(
"Error: key with CKA_ID \"%s\" already exists in database\n",
o_cka_id);
2921 if (strncmp(case_keytype,
"KSK", 3) == 0 || strncmp(
o_keytype,
"257", 3) == 0) {
2924 else if (strncmp(case_keytype,
"ZSK", 3) == 0 || strncmp(
o_keytype,
"256", 3) == 0) {
2928 printf(
"Error: Unrecognised keytype %s; should be one of KSK or ZSK\n",
o_keytype);
2940 printf(
"Error: Unable to convert bits \"%s\"; to an integer\n",
o_size);
2945 printf(
"Error: Bits \"%s\"; should be numeric only\n",
o_size);
2963 if (status != 0 || algo_id == 0 || hsm_supported_algorithm(algo_id) != 0) {
2964 printf(
"Error: Key algorithm %s not supported; try one of RSASHA1, RSASHA1-NSEC3-SHA1 or RSASHA256\n",
o_algo);
2972 if (strncmp(case_state,
"GENERATE", 8) == 0 || strncmp(
o_keystate,
"1", 1) == 0) {
2975 else if (strncmp(case_state,
"PUBLISH", 7) == 0 || strncmp(
o_keystate,
"2", 1) == 0) {
2978 else if (strncmp(case_state,
"READY", 5) == 0 || strncmp(
o_keystate,
"3", 1) == 0) {
2981 else if (strncmp(case_state,
"ACTIVE", 6) == 0 || strncmp(
o_keystate,
"4", 1) == 0) {
2984 else if (strncmp(case_state,
"RETIRE", 6) == 0 || strncmp(
o_keystate,
"5", 1) == 0) {
2988 printf(
"Error: Unrecognised state %s; should be one of GENERATE, PUBLISH, READY, ACTIVE or RETIRE\n",
o_keystate);
2999 printf(
"Error: unable to convert \"%s\" into a date\n",
o_time);
3006 snprintf(form_time,
KSM_TIME_LENGTH,
"%4.4d-%2.2d-%2.2d %2.2d:%2.2d:%2.2d",
3007 datetime.tm_year + 1900, datetime.tm_mon + 1, datetime.tm_mday,
3008 datetime.tm_hour, datetime.tm_min, datetime.tm_sec);
3014 printf(
"Error: unable to specify retire time for a key in state \"%s\"\n",
o_keystate);
3021 printf(
"Error: unable to convert retire time \"%s\" into a date\n",
o_retire);
3028 snprintf(form_opt_time,
KSM_TIME_LENGTH,
"%4.4d-%2.2d-%2.2d %2.2d:%2.2d:%2.2d",
3029 datetime.tm_year + 1900, datetime.tm_mon + 1, datetime.tm_mday,
3030 datetime.tm_hour, datetime.tm_min, datetime.tm_sec);
3034 form_opt_time[0] =
'\0';
3051 if (data.
value == 1) {
3052 printf(
"*WARNING* This zone shares keys with others, the key will be added to all; are you sure? [y/N] ");
3054 user_certain = getchar();
3055 if (user_certain !=
'y' && user_certain !=
'Y') {
3056 printf(
"Okay, quitting...\n");
3063 status =
KsmImportKeyPair(policy_id,
o_cka_id, repo_id, size_int, algo_id, state_id, form_time, fix_time, &keypair_id);
3065 printf(
"Error: couldn't import key\n");
3075 status =
KsmDnssecKeyCreate(zone_id, (
int) keypair_id, keytype_id, state_id, form_time, form_opt_time, &ignore);
3078 printf(
"Error: couldn't allocate key to zone(s)\n");
3083 printf(
"Key imported into zone(s)\n");
3099 FILE* lock_fd = NULL;
3102 char *dbschema = NULL;
3106 char *password = NULL;
3110 char* backup_filename = NULL;
3111 char* lock_filename;
3113 char *path = getenv(
"PWD");
3116 printf(
"Sorry, currently this utility can only backup a sqlite database file\n");
3121 status =
get_db_details(&dbschema, &host, &port, &user, &password);
3132 lock_filename = NULL;
3136 lock_fd = fopen(lock_filename,
"w");
3139 printf(
"Error getting db lock\n");
3140 if (lock_fd != NULL) {
3189 char* kasp_filename = NULL;
3190 char* zonelist_filename = NULL;
3191 char* backup_filename = NULL;
3194 FILE* lock_fd = NULL;
3203 int zone_count = -1;
3205 xmlDocPtr doc = NULL;
3208 printf(
"*WARNING* This feature is experimental and has not been fully tested; are you sure? [y/N] ");
3210 user_certain = getchar();
3211 if (user_certain !=
'y' && user_certain !=
'Y') {
3212 printf(
"Okay, quitting...\n");
3219 printf(
"Failed to read conf.xml\n");
3225 StrAppend(&backup_filename, kasp_filename);
3227 status =
backup_file(kasp_filename, backup_filename);
3236 if ((test = fopen(kasp_filename,
"ab"))==NULL) {
3237 printf(
"Cannot open kasp.xml for writing: %s\n", strerror(errno));
3246 printf(
"Failed to connect to database\n");
3263 if (policy == NULL) {
3264 printf(
"Malloc for policy struct failed\n");
3273 while (status == 0) {
3283 if (zone_count == 0) {
3284 printf(
"No zones on policy %s; purging...\n", policy->
name);
3286 size = snprintf(sql,
KSM_SQL_SIZE,
"update dnsseckeys set state = %d where keypair_id in (select id from keypairs where policy_id = %d)",
KSM_STATE_DEAD, policy->
id);
3290 printf(
"Couldn't construct SQL to kill orphaned keys\n");
3309 printf(
"Key purge failed for policy %s\n", policy->
name);
3316 sql2 =
DdsInit(
"parameters_policies");
3355 status = xmlSaveFormatFile(kasp_filename, doc, 1);
3358 printf(
"Could not save %s\n", kasp_filename);
3367 printf(
"Couldn't count zones on policy; quitting...\n");
3404 char* ods_control_cmd = NULL;
3405 char* ptr = command;
3410 *ptr = tolower((
int) *ptr);
3416 StrAppend(&ods_control_cmd, ODS_EN_CONTROL);
3419 status = system(ods_control_cmd);
3422 fprintf(stderr,
"Couldn't run %s\n", ods_control_cmd);
3438 char* case_command = NULL;
3439 char* case_verb = NULL;
3441 int option_index = 0;
3442 static struct option long_options[] =
3444 {
"all", no_argument, 0,
'a'},
3445 {
"bits", required_argument, 0,
'b'},
3446 {
"config", required_argument, 0,
'c'},
3447 {
"ds", no_argument, 0,
'd'},
3448 {
"keystate", required_argument, 0,
'e'},
3449 {
"no-retire", no_argument, 0,
'f'},
3450 {
"algorithm", required_argument, 0,
'g'},
3451 {
"help", no_argument, 0,
'h'},
3452 {
"input", required_argument, 0,
'i'},
3453 {
"cka_id", required_argument, 0,
'k'},
3454 {
"no-xml", no_argument, 0,
'm'},
3455 {
"interval", required_argument, 0,
'n'},
3456 {
"output", required_argument, 0,
'o'},
3457 {
"policy", required_argument, 0,
'p'},
3458 {
"repository", required_argument, 0,
'r'},
3459 {
"signerconf", required_argument, 0,
's'},
3460 {
"keytype", required_argument, 0,
't'},
3461 {
"time", required_argument, 0,
'w'},
3462 {
"verbose", no_argument, 0,
'v'},
3463 {
"version", no_argument, 0,
'V'},
3464 {
"keytag", required_argument, 0,
'x'},
3465 {
"retire", required_argument, 0,
'y'},
3466 {
"zone", required_argument, 0,
'z'},
3472 while ((ch = getopt_long(argc, argv,
"ab:c:de:fg:hi:k:n:o:p:r:s:t:vVw:x:y:z:", long_options, &option_index)) != -1) {
3530 printf(
"%s version %s\n", PACKAGE_NAME, PACKAGE_VERSION);
3585 if (!strncmp(case_command,
"SETUP", 5)) {
3589 }
else if (!strncmp(case_command,
"UPDATE", 6)) {
3593 }
else if (!strncmp(case_command,
"START", 5) ||
3594 !strncmp(case_command,
"STOP", 4) ||
3595 !strncmp(case_command,
"NOTIFY", 6)) {
3599 }
else if (!strncmp(case_command,
"ZONE", 4) && strlen(case_command) == 4) {
3604 if (!strncmp(case_verb,
"ADD", 3)) {
3606 }
else if (!strncmp(case_verb,
"DELETE", 6)) {
3608 }
else if (!strncmp(case_verb,
"LIST", 4)) {
3611 printf(
"Unknown command: zone %s\n", case_verb);
3615 }
else if (!strncmp(case_command,
"REPOSITORY", 10)) {
3619 if (!strncmp(case_verb,
"LIST", 4)) {
3622 printf(
"Unknown command: repository %s\n", case_verb);
3626 }
else if (!strncmp(case_command,
"POLICY", 6)) {
3630 if (!strncmp(case_verb,
"EXPORT", 6)) {
3632 }
else if (!strncmp(case_verb,
"IMPORT", 6)) {
3634 }
else if (!strncmp(case_verb,
"LIST", 4)) {
3636 }
else if (!strncmp(case_verb,
"PURGE", 5)) {
3639 printf(
"Unknown command: policy %s\n", case_verb);
3643 }
else if (!strncmp(case_command,
"KEY", 3)) {
3647 if (!strncmp(case_verb,
"LIST", 4)) {
3650 else if (!strncmp(case_verb,
"EXPORT", 6)) {
3653 else if (!strncmp(case_verb,
"IMPORT", 6)) {
3656 else if (!strncmp(case_verb,
"ROLLOVER", 8)) {
3665 printf(
"Please provide either a zone OR a policy to rollover\n");
3670 else if (!strncmp(case_verb,
"PURGE", 5)) {
3676 printf(
"Please provide either a zone OR a policy to key purge\n");
3681 else if (!strncmp(case_verb,
"GENERATE", 8)) {
3684 else if (!strncmp(case_verb,
"KSK-RETIRE", 10)) {
3687 else if (!strncmp(case_verb,
"DS-SEEN", 7)) {
3690 printf(
"Unknown command: key %s\n", case_verb);
3694 }
else if (!strncmp(case_command,
"BACKUP", 6)) {
3698 if (!strncmp(case_verb,
"DONE", 4) ||
3699 !strncmp(case_verb,
"PREPARE", 7) ||
3700 !strncmp(case_verb,
"COMMIT", 6) ||
3701 !strncmp(case_verb,
"ROLLBACK", 8)) {
3704 else if (!strncmp(case_verb,
"LIST", 4)) {
3707 printf(
"Unknown command: backup %s\n", case_verb);
3711 }
else if (!strncmp(case_command,
"ROLLOVER", 8)) {
3714 if (!strncmp(case_verb,
"LIST", 4)) {
3717 printf(
"Unknown command: rollover %s\n", case_verb);
3721 }
else if (!strncmp(case_command,
"DATABASE", 8)) {
3725 if (!strncmp(case_verb,
"BACKUP", 6)) {
3728 printf(
"Unknown command: database %s\n", case_verb);
3732 }
else if (!strncmp(case_command,
"ZONELIST", 8)) {
3736 if (!strncmp(case_verb,
"EXPORT", 6)) {
3739 else if (!strncmp(case_verb,
"IMPORT", 6)) {
3742 printf(
"Unknown command: zonelist %s\n", case_verb);
3747 printf(
"Unknown command: %s\n", argv[0]);
3759 xmlCleanupGlobals();
3760 xmlCleanupThreads();
3782 char *dbschema = NULL;
3786 char *password = NULL;
3790 char* backup_filename = NULL;
3791 char* lock_filename;
3794 status =
get_db_details(&dbschema, &host, &port, &user, &password);
3810 if (lock_fd != NULL) {
3811 lock_filename = NULL;
3815 *lock_fd = fopen(lock_filename,
"w");
3818 printf(
"Error getting db lock\n");
3819 if (*lock_fd != NULL) {
3842 if (lock_fd != NULL) {
3857 status =
DbConnect(dbhandle, dbschema, host, password, user, port);
3879 if (lock_fd != NULL) {
3882 printf(
"Error releasing db lock");
3903 if (lock_fd == NULL) {
3904 printf(
"%s could not be opened\n", lock_filename);
3908 memset(&fl, 0,
sizeof(
struct flock));
3909 fl.l_type = F_WRLCK;
3910 fl.l_whence = SEEK_SET;
3911 fl.l_pid = getpid();
3913 while (fcntl(fileno(lock_fd), F_SETLK, &fl) == -1) {
3914 if (errno == EACCES || errno == EAGAIN) {
3915 printf(
"%s already locked, sleep\n", lock_filename);
3920 select(0, NULL, NULL, NULL, &tv);
3923 printf(
"couldn't get lock on %s; %s\n", lock_filename, strerror(errno));
3936 if (lock_fd == NULL) {
3940 memset(&fl, 0,
sizeof(
struct flock));
3941 fl.l_type = F_UNLCK;
3942 fl.l_whence = SEEK_SET;
3944 if (fcntl(fileno(lock_fd), F_SETLK, &fl) == -1) {
3957 xmlTextReaderPtr reader = NULL;
3958 xmlDocPtr doc = NULL;
3959 xmlXPathContextPtr xpathCtx = NULL;
3960 xmlXPathObjectPtr xpathObj = NULL;
3962 char* tag_name = NULL;
3963 char* temp_char = NULL;
3965 xmlChar *zonelist_expr = (
unsigned char*)
"//Common/ZoneListFile";
3966 xmlChar *kaspfile_expr = (
unsigned char*)
"//Common/PolicyFile";
3969 reader = xmlNewTextReaderFilename(
config);
3970 if (reader != NULL) {
3971 ret = xmlTextReaderRead(reader);
3973 tag_name = (
char*) xmlTextReaderLocalName(reader);
3975 if (strncmp(tag_name,
"Common", 6) == 0
3976 && xmlTextReaderNodeType(reader) == 1) {
3979 xmlTextReaderExpand(reader);
3980 doc = xmlTextReaderCurrentDoc(reader);
3982 printf(
"Error: can not read Common section\n");
3984 ret = xmlTextReaderRead(reader);
3988 xpathCtx = xmlXPathNewContext(doc);
3989 if(xpathCtx == NULL) {
3990 printf(
"Error: can not create XPath context for Common section\n");
3992 ret = xmlTextReaderRead(reader);
3997 xpathObj = xmlXPathEvalExpression(zonelist_expr, xpathCtx);
3998 if(xpathObj == NULL) {
3999 printf(
"Error: unable to evaluate xpath expression: %s\n", zonelist_expr);
4001 ret = xmlTextReaderRead(reader);
4004 *zone_list_filename = NULL;
4005 temp_char = (
char*) xmlXPathCastToString(xpathObj);
4006 StrAppend(zone_list_filename, temp_char);
4008 xmlXPathFreeObject(xpathObj);
4009 printf(
"zonelist filename set to %s.\n", *zone_list_filename);
4012 xpathObj = xmlXPathEvalExpression(kaspfile_expr, xpathCtx);
4013 xmlXPathFreeContext(xpathCtx);
4014 if(xpathObj == NULL) {
4015 printf(
"Error: unable to evaluate xpath expression: %s\n", kaspfile_expr);
4017 ret = xmlTextReaderRead(reader);
4020 *kasp_filename = NULL;
4021 if (xpathObj->nodesetval != NULL && xpathObj->nodesetval->nodeNr > 0) {
4025 temp_char = (
char*) xmlXPathCastToString(xpathObj);
4033 StrAppend(kasp_filename, OPENDNSSEC_CONFIG_DIR);
4036 printf(
"kasp filename set to %s.\n", *kasp_filename);
4038 xmlXPathFreeObject(xpathObj);
4041 ret = xmlTextReaderRead(reader);
4045 xmlFreeTextReader(reader);
4047 printf(
"%s : failed to parse\n",
config);
4051 printf(
"Unable to open %s\n",
config);
4068 xmlDocPtr doc = NULL;
4069 xmlXPathContextPtr xpathCtx = NULL;
4070 xmlXPathObjectPtr xpathObj = NULL;
4072 char* repo_name = NULL;
4073 char* repo_capacity = NULL;
4074 int require_backup = 0;
4077 xmlChar *node_expr = (
unsigned char*)
"//Configuration/RepositoryList/Repository";
4081 doc = xmlParseFile(
config);
4083 printf(
"Unable to open %s\n",
config);
4088 xpathCtx = xmlXPathNewContext(doc);
4089 if(xpathCtx == NULL) {
4095 xpathObj = xmlXPathEvalExpression(node_expr, xpathCtx);
4096 if(xpathObj == NULL) {
4097 xmlXPathFreeContext(xpathCtx);
4102 if (xpathObj->nodesetval) {
4103 for (i = 0; i < xpathObj->nodesetval->nodeNr; i++) {
4108 curNode = xpathObj->nodesetval->nodeTab[i]->xmlChildrenNode;
4109 repo_name = (
char *) xmlGetProp(xpathObj->nodesetval->nodeTab[i],
4110 (
const xmlChar *)
"name");
4112 if (xmlStrEqual(curNode->name, (
const xmlChar *)
"Capacity")) {
4113 repo_capacity = (
char *) xmlNodeGetContent(curNode);
4115 if (xmlStrEqual(curNode->name, (
const xmlChar *)
"RequireBackup")) {
4119 curNode = curNode->next;
4122 if (strlen(repo_name) != 0) {
4124 printf(
"Repository %s found\n", repo_name);
4125 if (strlen(repo_capacity) == 0) {
4126 printf(
"No Maximum Capacity set.\n");
4132 printf(
"Capacity set to %s.\n", repo_capacity);
4138 if (require_backup == 0) {
4139 printf(
"RequireBackup NOT set; please make sure that you know the potential problems of using keys which are not recoverable\n");
4141 printf(
"RequireBackup set.\n");
4145 printf(
"Error Importing Repository %s", repo_name);
4149 printf(
"WARNING: Repository found with NULL name, skipping...\n");
4157 xmlXPathFreeObject(xpathObj);
4160 xmlXPathFreeContext(xpathCtx);
4175 char *policy_name = NULL;
4176 char *policy_description = NULL;
4179 xmlDocPtr doc = NULL;
4180 xmlDocPtr pol_doc = NULL;
4181 xmlDocPtr rngdoc = NULL;
4184 xmlNode *childNode2;
4185 xmlNode *childNode3;
4186 xmlChar *opt_out_flag = (xmlChar *)
"N";
4187 xmlChar *share_keys_flag = (xmlChar *)
"N";
4188 xmlChar *man_roll_flag = (xmlChar *)
"N";
4189 xmlChar *rfc5011_flag = (xmlChar *)
"N";
4190 int standby_keys_flag = 0;
4191 xmlXPathContextPtr xpathCtx = NULL;
4192 xmlXPathObjectPtr xpathObj = NULL;
4193 xmlRelaxNGParserCtxtPtr rngpctx = NULL;
4194 xmlRelaxNGValidCtxtPtr rngctx = NULL;
4195 xmlRelaxNGPtr schema = NULL;
4198 xmlChar *node_expr = (
unsigned char*)
"//Policy";
4202 int audit_found = 0;
4207 const char* rngfilename = OPENDNSSEC_SCHEMA_DIR
"/kasp.rng";
4208 char* kaspcheck_cmd = NULL;
4209 char* kaspcheck_cmd_version = NULL;
4211 StrAppend(&kaspcheck_cmd, ODS_AU_KASPCHECK);
4215 StrAppend(&kaspcheck_cmd_version, ODS_AU_KASPCHECK);
4216 StrAppend(&kaspcheck_cmd_version,
" -v > /dev/null");
4219 status = system(kaspcheck_cmd_version);
4222 status = system(kaspcheck_cmd);
4225 fprintf(stderr,
"ods-kaspcheck returned an error, please check your policy\n");
4227 StrFree(kaspcheck_cmd_version);
4233 fprintf(stderr,
"Couldn't run ods-kaspcheck (Auditor is not installed), will carry on\n");
4237 StrFree(kaspcheck_cmd_version);
4240 doc = xmlParseFile(kasp_filename);
4242 printf(
"Error: unable to parse file \"%s\"\n", kasp_filename);
4247 rngdoc = xmlParseFile(rngfilename);
4248 if (rngdoc == NULL) {
4249 printf(
"Error: unable to parse file \"%s\"\n", rngfilename);
4254 rngpctx = xmlRelaxNGNewDocParserCtxt(rngdoc);
4255 if (rngpctx == NULL) {
4256 printf(
"Error: unable to create XML RelaxNGs parser context\n");
4261 schema = xmlRelaxNGParse(rngpctx);
4262 if (schema == NULL) {
4263 printf(
"Error: unable to parse a schema definition resource\n");
4268 rngctx = xmlRelaxNGNewValidCtxt(schema);
4269 if (rngctx == NULL) {
4270 printf(
"Error: unable to create RelaxNGs validation context based on the schema\n");
4275 status = xmlRelaxNGValidateDoc(rngctx,doc);
4277 printf(
"Error validating file \"%s\"\n", kasp_filename);
4283 if (policy == NULL) {
4284 printf(
"Malloc for policy struct failed");
4289 xpathCtx = xmlXPathNewContext(doc);
4290 if(xpathCtx == NULL) {
4297 xpathObj = xmlXPathEvalExpression(node_expr, xpathCtx);
4298 if(xpathObj == NULL) {
4299 xmlXPathFreeContext(xpathCtx);
4305 if (xpathObj->nodesetval) {
4306 for (i = 0; i < xpathObj->nodesetval->nodeNr; i++) {
4308 curNode = xpathObj->nodesetval->nodeTab[i]->xmlChildrenNode;
4309 policy_name = (
char *) xmlGetProp(xpathObj->nodesetval->nodeTab[i], (
const xmlChar *)
"name");
4310 if (strlen(policy_name) == 0) {
4312 printf(
"Error extracting policy name from %s\n", kasp_filename);
4317 printf(
"Policy %s found\n", policy_name);
4319 if (xmlStrEqual(curNode->name, (
const xmlChar *)
"Description")) {
4320 policy_description = (
char *) xmlNodeGetContent(curNode);
4330 printf(
"Error: unable to read policy %s; skipping\n", policy_name);
4331 curNode = curNode->next;
4340 printf(
"Error: unable to insert policy %s; skipping\n", policy_name);
4347 printf(
"Error: unable to get policy id for %s; skipping\n", policy_name);
4353 else if (xmlStrEqual(curNode->name, (
const xmlChar *)
"Signatures")) {
4354 childNode = curNode->children;
4356 if (xmlStrEqual(childNode->name, (
const xmlChar *)
"Resign")) {
4359 else if (xmlStrEqual(childNode->name, (
const xmlChar *)
"Refresh")) {
4362 else if (xmlStrEqual(childNode->name, (
const xmlChar *)
"Validity")) {
4363 childNode2 = childNode->children;
4365 if (xmlStrEqual(childNode2->name, (
const xmlChar *)
"Default")) {
4368 else if (xmlStrEqual(childNode2->name, (
const xmlChar *)
"Denial")) {
4371 childNode2 = childNode2->next;
4374 else if (xmlStrEqual(childNode->name, (
const xmlChar *)
"Jitter")) {
4377 else if (xmlStrEqual(childNode->name, (
const xmlChar *)
"InceptionOffset")) {
4380 childNode = childNode->next;
4383 else if (xmlStrEqual(curNode->name, (
const xmlChar *)
"Denial")) {
4384 opt_out_flag = (xmlChar *)
"N";
4385 childNode = curNode->children;
4387 if (xmlStrEqual(childNode->name, (
const xmlChar *)
"NSEC3")) {
4391 printf(
"Error: unable to insert/update %s for policy\n",
"Denial version");
4393 childNode2 = childNode->children;
4395 if (xmlStrEqual(childNode2->name, (
const xmlChar *)
"OptOut")) {
4396 opt_out_flag = (xmlChar *)
"Y";
4398 else if (xmlStrEqual(childNode2->name, (
const xmlChar *)
"Resalt")) {
4401 else if (xmlStrEqual(childNode2->name, (
const xmlChar *)
"Hash")) {
4402 childNode3 = childNode2->children;
4404 if (xmlStrEqual(childNode3->name, (
const xmlChar *)
"Algorithm")) {
4407 else if (xmlStrEqual(childNode3->name, (
const xmlChar *)
"Iterations")) {
4410 else if (xmlStrEqual(childNode3->name, (
const xmlChar *)
"Salt")) {
4413 childNode3 = childNode3->next;
4417 childNode2 = childNode2->next;
4422 else if (xmlStrEqual(childNode->name, (
const xmlChar *)
"NSEC")) {
4425 printf(
"Error: unable to insert/update %s for policy\n",
"Denial version");
4428 childNode = childNode->next;
4431 else if (xmlStrEqual(curNode->name, (
const xmlChar *)
"Keys")) {
4432 share_keys_flag = (xmlChar *)
"N";
4433 childNode = curNode->children;
4435 if (xmlStrEqual(childNode->name, (
const xmlChar *)
"TTL")) {
4438 else if (xmlStrEqual(childNode->name, (
const xmlChar *)
"RetireSafety")) {
4441 else if (xmlStrEqual(childNode->name, (
const xmlChar *)
"PublishSafety")) {
4444 else if (xmlStrEqual(childNode->name, (
const xmlChar *)
"ShareKeys")) {
4445 share_keys_flag = (xmlChar *)
"Y";
4447 else if (xmlStrEqual(childNode->name, (
const xmlChar *)
"Purge")) {
4451 else if (xmlStrEqual(childNode->name, (
const xmlChar *)
"KSK")) {
4452 man_roll_flag = (xmlChar *)
"N";
4453 rfc5011_flag = (xmlChar *)
"N";
4454 childNode2 = childNode->children;
4456 if (xmlStrEqual(childNode2->name, (
const xmlChar *)
"Algorithm")) {
4461 else if (xmlStrEqual(childNode2->name, (
const xmlChar *)
"Lifetime")) {
4464 else if (xmlStrEqual(childNode2->name, (
const xmlChar *)
"Repository")) {
4466 printf(
"Please either add the repository to conf.xml or remove the reference to it from kasp.xml\n");
4468 xmlFreeDoc(pol_doc);
4469 xmlXPathFreeContext(xpathCtx);
4470 xmlRelaxNGFree(schema);
4471 xmlRelaxNGFreeValidCtxt(rngctx);
4472 xmlRelaxNGFreeParserCtxt(rngpctx);
4480 else if (xmlStrEqual(childNode2->name, (
const xmlChar *)
"Standby")) {
4482 standby_keys_flag = 1;
4484 else if (xmlStrEqual(childNode2->name, (
const xmlChar *)
"ManualRollover")) {
4485 man_roll_flag = (xmlChar *)
"Y";
4487 else if (xmlStrEqual(childNode2->name, (
const xmlChar *)
"RFC5011")) {
4488 rfc5011_flag = (xmlChar *)
"Y";
4493 childNode2 = childNode2->next;
4498 if (standby_keys_flag == 0) {
4501 standby_keys_flag = 0;
4505 else if (xmlStrEqual(childNode->name, (
const xmlChar *)
"ZSK")) {
4506 man_roll_flag = (xmlChar *)
"N";
4507 childNode2 = childNode->children;
4509 if (xmlStrEqual(childNode2->name, (
const xmlChar *)
"Algorithm")) {
4514 else if (xmlStrEqual(childNode2->name, (
const xmlChar *)
"Lifetime")) {
4517 else if (xmlStrEqual(childNode2->name, (
const xmlChar *)
"Repository")) {
4519 printf(
"Please either add the repository to conf.xml or remove the reference to it from kasp.xml\n");
4521 xmlFreeDoc(pol_doc);
4522 xmlXPathFreeContext(xpathCtx);
4523 xmlRelaxNGFree(schema);
4524 xmlRelaxNGFreeValidCtxt(rngctx);
4525 xmlRelaxNGFreeParserCtxt(rngpctx);
4533 else if (xmlStrEqual(childNode2->name, (
const xmlChar *)
"Standby")) {
4535 standby_keys_flag = 1;
4537 else if (xmlStrEqual(childNode2->name, (
const xmlChar *)
"ManualRollover")) {
4538 man_roll_flag = (xmlChar *)
"Y";
4540 childNode2 = childNode2->next;
4546 childNode = childNode->next;
4550 if (standby_keys_flag == 0) {
4553 standby_keys_flag = 0;
4558 else if (xmlStrEqual(curNode->name, (
const xmlChar *)
"Zone")) {
4559 childNode = curNode->children;
4561 if (xmlStrEqual(childNode->name, (
const xmlChar *)
"PropagationDelay")) {
4564 else if (xmlStrEqual(childNode->name, (
const xmlChar *)
"SOA")) {
4565 childNode2 = childNode->children;
4567 if (xmlStrEqual(childNode2->name, (
const xmlChar *)
"TTL")) {
4570 else if (xmlStrEqual(childNode2->name, (
const xmlChar *)
"Minimum")) {
4573 else if (xmlStrEqual(childNode2->name, (
const xmlChar *)
"Serial")) {
4576 childNode2 = childNode2->next;
4579 childNode = childNode->next;
4583 else if (xmlStrEqual(curNode->name, (
const xmlChar *)
"Parent")) {
4584 childNode = curNode->children;
4586 if (xmlStrEqual(childNode->name, (
const xmlChar *)
"PropagationDelay")) {
4589 else if (xmlStrEqual(childNode->name, (
const xmlChar *)
"DS")) {
4590 childNode2 = childNode->children;
4592 if (xmlStrEqual(childNode2->name, (
const xmlChar *)
"TTL")) {
4595 childNode2 = childNode2->next;
4598 else if (xmlStrEqual(childNode->name, (
const xmlChar *)
"SOA")) {
4599 childNode2 = childNode->children;
4601 if (xmlStrEqual(childNode2->name, (
const xmlChar *)
"TTL")) {
4604 else if (xmlStrEqual(childNode2->name, (
const xmlChar *)
"Minimum")) {
4607 childNode2 = childNode2->next;
4610 childNode = childNode->next;
4614 else if (xmlStrEqual(curNode->name, (
const xmlChar *)
"Audit")) {
4616 childNode = curNode->children;
4618 if (xmlStrEqual(childNode->name, (
const xmlChar *)
"Partial")) {
4621 childNode = childNode->next;
4625 printf(
"Error: unable to insert Audit info for policy %s\n", policy->
name);
4629 curNode = curNode->next;
4632 if (audit_found == 0) {
4644 xmlXPathFreeContext(xpathCtx);
4645 xmlRelaxNGFree(schema);
4646 xmlRelaxNGFreeValidCtxt(rngctx);
4647 xmlRelaxNGFreeParserCtxt(rngpctx);
4659 xmlTextReaderPtr reader = NULL;
4660 xmlDocPtr doc = NULL;
4661 xmlXPathContextPtr xpathCtx = NULL;
4662 xmlXPathObjectPtr xpathObj = NULL;
4664 char* zone_name = NULL;
4665 char* policy_name = NULL;
4666 char* current_policy = NULL;
4667 char* current_signconf = NULL;
4668 char* current_input = NULL;
4669 char* current_output = NULL;
4670 char* temp_char = NULL;
4671 char* tag_name = NULL;
4674 int file_zone_count = 0;
4675 int db_zone_count = 0;
4689 xmlChar *name_expr = (
unsigned char*)
"name";
4690 xmlChar *policy_expr = (
unsigned char*)
"//Zone/Policy";
4691 xmlChar *signconf_expr = (
unsigned char*)
"//Zone/SignerConfiguration";
4692 xmlChar *input_expr = (
unsigned char*)
"//Zone/Adapters/Input/File";
4693 xmlChar *output_expr = (
unsigned char*)
"//Zone/Adapters/Output/File";
4697 reader = xmlNewTextReaderFilename(zone_list_filename);
4698 if (reader != NULL) {
4699 ret = xmlTextReaderRead(reader);
4701 tag_name = (
char*) xmlTextReaderLocalName(reader);
4703 if (strncmp(tag_name,
"Zone", 4) == 0
4704 && strncmp(tag_name,
"ZoneList", 8) != 0
4705 && xmlTextReaderNodeType(reader) == 1) {
4709 ret = xmlTextReaderRead(reader);
4712 xmlFreeTextReader(reader);
4714 printf(
"%s : failed to parse\n", zone_list_filename);
4718 printf(
"Unable to open %s\n", zone_list_filename);
4723 zone_ids =
MemMalloc(file_zone_count *
sizeof(
int));
4726 reader = xmlNewTextReaderFilename(zone_list_filename);
4727 if (reader != NULL) {
4728 ret = xmlTextReaderRead(reader);
4730 tag_name = (
char*) xmlTextReaderLocalName(reader);
4732 if (strncmp(tag_name,
"Zone", 4) == 0
4733 && strncmp(tag_name,
"ZoneList", 8) != 0
4734 && xmlTextReaderNodeType(reader) == 1) {
4737 temp_char = (
char*) xmlTextReaderGetAttribute(reader, name_expr);
4751 if (zone_name == NULL) {
4753 printf(
"Error extracting zone name from %s\n", zone_list_filename);
4755 ret = xmlTextReaderRead(reader);
4759 printf(
"Zone %s found\n", zone_name);
4762 xmlTextReaderExpand(reader);
4763 doc = xmlTextReaderCurrentDoc(reader);
4765 printf(
"Error: can not read zone \"%s\"; skipping\n", zone_name);
4767 ret = xmlTextReaderRead(reader);
4771 xpathCtx = xmlXPathNewContext(doc);
4772 if(xpathCtx == NULL) {
4773 printf(
"Error: can not create XPath context for \"%s\"; skipping zone\n", zone_name);
4775 ret = xmlTextReaderRead(reader);
4781 xpathObj = xmlXPathEvalExpression(policy_expr, xpathCtx);
4782 if(xpathObj == NULL) {
4783 printf(
"Error: unable to evaluate xpath expression: %s; skipping zone\n", policy_expr);
4785 ret = xmlTextReaderRead(reader);
4789 current_policy = NULL;
4790 temp_char = (
char *)xmlXPathCastToString(xpathObj);
4793 printf(
"Policy set to %s.\n", current_policy);
4794 xmlXPathFreeObject(xpathObj);
4797 if (policy_name == NULL || strcmp(current_policy, policy_name) != 0) {
4799 StrAppend(&policy_name, current_policy);
4803 printf(
"Error, can't find policy : %s\n", policy_name);
4805 ret = xmlTextReaderRead(reader);
4812 xpathObj = xmlXPathEvalExpression(signconf_expr, xpathCtx);
4813 if(xpathObj == NULL) {
4814 printf(
"Error: unable to evaluate xpath expression: %s; skipping zone\n", signconf_expr);
4816 ret = xmlTextReaderRead(reader);
4820 current_signconf = NULL;
4821 temp_char = (
char *)xmlXPathCastToString(xpathObj);
4822 StrAppend(¤t_signconf, temp_char);
4824 xmlXPathFreeObject(xpathObj);
4828 xpathObj = xmlXPathEvalExpression(input_expr, xpathCtx);
4829 if(xpathObj == NULL) {
4830 printf(
"Error: unable to evaluate xpath expression: %s; skipping zone\n", input_expr);
4832 ret = xmlTextReaderRead(reader);
4836 current_input = NULL;
4837 temp_char = (
char *)xmlXPathCastToString(xpathObj);
4840 xmlXPathFreeObject(xpathObj);
4844 xpathObj = xmlXPathEvalExpression(output_expr, xpathCtx);
4845 xmlXPathFreeContext(xpathCtx);
4846 if(xpathObj == NULL) {
4847 printf(
"Error: unable to evaluate xpath expression: %s; skipping zone\n", output_expr);
4849 ret = xmlTextReaderRead(reader);
4853 current_output = NULL;
4854 temp_char = (
char *)xmlXPathCastToString(xpathObj);
4857 xmlXPathFreeObject(xpathObj);
4862 status =
KsmImportZone(zone_name, policy_id, 0, &new_zone, current_signconf, current_input, current_output);
4865 printf(
"Error Importing zone %s; it already exists both with and without a trailing dot\n", zone_name);
4867 printf(
"Error Importing Zone %s\n", zone_name);
4870 ret = xmlTextReaderRead(reader);
4875 if (new_zone == 1) {
4876 printf(
"Added zone %s to database\n", zone_name);
4890 printf(
"Error: unable to find a zone named \"%s\" in database\n", zone_name);
4891 printf(
"Error: Possibly two domains differ only by having a trailing dot or not?\n");
4897 zone_ids[i] = temp_id;
4910 ret = xmlTextReaderRead(reader);
4913 xmlFreeTextReader(reader);
4915 printf(
"%s : failed to parse\n", zone_list_filename);
4918 printf(
"Unable to open %s\n", zone_list_filename);
4934 if (file_zone_count == db_zone_count) {
4939 else if (file_zone_count > db_zone_count) {
4940 printf(
"Failed to add all zones from zonelist\n");
4956 while (status == 0) {
4957 DbInt(row, 0, &temp_id);
4959 DbInt(row, 2, &policy_id);
4962 for (i = 0; i < db_zone_count; ++i) {
4963 if (temp_id == zone_ids[i]) {
4969 if (seen_zone == 0) {
4972 printf(
"Removing zone %s from database\n", zone_name);
4974 status =
KsmParameterInit(&result2,
"zones_share_keys",
"keys", policy_id);
4998 if ((shared.
value == 1 && temp_count == 1) || shared.
value == 0) {
5001 printf(
"Error: failed to mark keys as dead in database\n");
5035 int SetParamOnPolicy(
const xmlChar* new_value,
const char* name,
const char* category,
int current_value,
int policy_id,
int value_type)
5039 char* temp_char = (
char *)new_value;
5043 if (strlen(temp_char) != 0) {
5046 printf(
"Error: unable to convert interval %s to seconds, error: %i\n", temp_char, status);
5050 else if (status == -1) {
5051 printf(
"Info: converting %s to seconds; M interpreted as 31 days, Y interpreted as 365 days\n", temp_char);
5060 if (strncmp(temp_char,
"Y", 1) == 0) {
5070 printf(
"Error: unable to find repository %s\n", temp_char);
5080 printf(
"Error: unable to find serial type %s\n", temp_char);
5090 printf(
"Error: unable to find rollover scheme %s\n", temp_char);
5099 printf(
"Error: unable to convert %s to int\n", temp_char);
5109 if (value != current_value || current_value == 0) {
5112 printf(
"Error: unable to insert/update %s for policy\n", name);
5113 printf(
"Error: Is your database schema up to date?\n");
5119 if (strncmp(name,
"saltlength", 10) == 0) {
5122 printf(
"Error: unable to insert/update %s for policy\n", name);
5123 printf(
"Error: Is your database schema up to date?\n");
5134 if (policy == NULL) {
5135 printf(
"Error, no policy provided");
5172 policy->
ksk->
sm = 0;
5184 policy->
zsk->
sm = 0;
5221 if((from = fopen( orig_file,
"rb"))==NULL) {
5222 if (errno == ENOENT) {
5223 printf(
"File %s does not exist, nothing to backup\n", orig_file);
5227 printf(
"Cannot open source file.\n");
5233 if((to = fopen(backup_file,
"wb"))==NULL) {
5234 printf(
"Cannot open destination file, will not make backup.\n");
5240 while(!feof(from)) {
5243 printf(
"Error reading source file.\n");
5248 if(!feof(from)) fputc(ch, to);
5250 printf(
"Error writing destination file.\n");
5257 if(fclose(from)==EOF) {
5258 printf(
"Error closing source file.\n");
5263 if(fclose(to)==EOF) {
5264 printf(
"Error closing destination file.\n");
5282 get_db_details(
char** dbschema,
char** host,
char** port,
char** user,
char** password)
5287 xmlXPathContextPtr xpathCtx;
5288 xmlXPathObjectPtr xpathObj;
5289 xmlRelaxNGParserCtxtPtr rngpctx;
5290 xmlRelaxNGValidCtxtPtr rngctx;
5291 xmlRelaxNGPtr schema;
5292 xmlChar *litexpr = (
unsigned char*)
"//Configuration/Enforcer/Datastore/SQLite";
5293 xmlChar *mysql_host = (
unsigned char*)
"//Configuration/Enforcer/Datastore/MySQL/Host";
5294 xmlChar *mysql_port = (
unsigned char*)
"//Configuration/Enforcer/Datastore/MySQL/Host/@port";
5295 xmlChar *mysql_db = (
unsigned char*)
"//Configuration/Enforcer/Datastore/MySQL/Database";
5296 xmlChar *mysql_user = (
unsigned char*)
"//Configuration/Enforcer/Datastore/MySQL/Username";
5297 xmlChar *mysql_pass = (
unsigned char*)
"//Configuration/Enforcer/Datastore/MySQL/Password";
5301 char* temp_char = NULL;
5304 const char* rngfilename = OPENDNSSEC_SCHEMA_DIR
"/conf.rng";
5307 doc = xmlParseFile(
config);
5309 printf(
"Error: unable to parse file \"%s\"\n",
config);
5314 rngdoc = xmlParseFile(rngfilename);
5315 if (rngdoc == NULL) {
5316 printf(
"Error: unable to parse file \"%s\"\n", rngfilename);
5322 rngpctx = xmlRelaxNGNewDocParserCtxt(rngdoc);
5324 if (rngpctx == NULL) {
5325 printf(
"Error: unable to create XML RelaxNGs parser context\n");
5331 schema = xmlRelaxNGParse(rngpctx);
5332 xmlRelaxNGFreeParserCtxt(rngpctx);
5333 if (schema == NULL) {
5334 printf(
"Error: unable to parse a schema definition resource\n");
5340 rngctx = xmlRelaxNGNewValidCtxt(schema);
5341 if (rngctx == NULL) {
5342 printf(
"Error: unable to create RelaxNGs validation context based on the schema\n");
5343 xmlRelaxNGFree(schema);
5349 status = xmlRelaxNGValidateDoc(rngctx,doc);
5350 xmlRelaxNGFreeValidCtxt(rngctx);
5351 xmlRelaxNGFree(schema);
5353 printf(
"Error validating file \"%s\"\n",
config);
5360 xpathCtx = xmlXPathNewContext(doc);
5361 if(xpathCtx == NULL) {
5362 printf(
"Error: unable to create new XPath context\n");
5368 xpathObj = xmlXPathEvalExpression(litexpr, xpathCtx);
5369 if(xpathObj == NULL) {
5370 printf(
"Error: unable to evaluate xpath expression: %s\n", litexpr);
5371 xmlXPathFreeContext(xpathCtx);
5375 if(xpathObj->nodesetval != NULL && xpathObj->nodesetval->nodeNr > 0) {
5377 temp_char = (
char *)xmlXPathCastToString(xpathObj);
5381 fprintf(stderr,
"SQLite database set to: %s\n", *dbschema);
5384 xmlXPathFreeObject(xpathObj);
5386 if (db_found == 0) {
5391 xpathObj = xmlXPathEvalExpression(mysql_host, xpathCtx);
5392 if(xpathObj == NULL) {
5393 printf(
"Error: unable to evaluate xpath expression: %s\n", mysql_host);
5394 xmlXPathFreeContext(xpathCtx);
5398 if(xpathObj->nodesetval != NULL && xpathObj->nodesetval->nodeNr > 0) {
5399 temp_char = (
char *)xmlXPathCastToString(xpathObj);
5403 fprintf(stderr,
"MySQL database host set to: %s\n", *host);
5406 xmlXPathFreeObject(xpathObj);
5409 xpathObj = xmlXPathEvalExpression(mysql_port, xpathCtx);
5410 if(xpathObj == NULL) {
5411 printf(
"Error: unable to evaluate xpath expression: %s\n", mysql_port);
5412 xmlXPathFreeContext(xpathCtx);
5416 if(xpathObj->nodesetval != NULL && xpathObj->nodesetval->nodeNr > 0) {
5417 temp_char = (
char *)xmlXPathCastToString(xpathObj);
5421 fprintf(stderr,
"MySQL database port set to: %s\n", *port);
5424 xmlXPathFreeObject(xpathObj);
5427 xpathObj = xmlXPathEvalExpression(mysql_db, xpathCtx);
5428 if(xpathObj == NULL) {
5429 printf(
"Error: unable to evaluate xpath expression: %s\n", mysql_db);
5430 xmlXPathFreeContext(xpathCtx);
5434 if(xpathObj->nodesetval != NULL && xpathObj->nodesetval->nodeNr > 0) {
5435 temp_char = (
char *)xmlXPathCastToString(xpathObj);
5439 fprintf(stderr,
"MySQL database schema set to: %s\n", *dbschema);
5444 xmlXPathFreeObject(xpathObj);
5447 xpathObj = xmlXPathEvalExpression(mysql_user, xpathCtx);
5448 if(xpathObj == NULL) {
5449 printf(
"Error: unable to evaluate xpath expression: %s\n", mysql_user);
5450 xmlXPathFreeContext(xpathCtx);
5454 if(xpathObj->nodesetval != NULL && xpathObj->nodesetval->nodeNr > 0) {
5455 temp_char = (
char *)xmlXPathCastToString(xpathObj);
5459 fprintf(stderr,
"MySQL database user set to: %s\n", *user);
5464 xmlXPathFreeObject(xpathObj);
5467 xpathObj = xmlXPathEvalExpression(mysql_pass, xpathCtx);
5468 if(xpathObj == NULL) {
5469 printf(
"Error: unable to evaluate xpath expression: %s\n", mysql_pass);
5470 xmlXPathFreeContext(xpathCtx);
5475 temp_char = (
char *)xmlXPathCastToString(xpathObj);
5478 xmlXPathFreeObject(xpathObj);
5481 fprintf(stderr,
"MySQL database password set\n");
5486 xmlXPathFreeContext(xpathCtx);
5491 printf(
"Error: unable to find complete database connection expression\n");
5497 printf(
"Error: database in config file does not match libksm\n");
5511 xmlTextReaderPtr reader = NULL;
5512 xmlDocPtr doc = NULL;
5513 xmlXPathContextPtr xpathCtx = NULL;
5514 xmlXPathObjectPtr xpathObj = NULL;
5516 char* temp_char = NULL;
5517 char* tag_name = NULL;
5519 xmlChar *zonelist_expr = (
unsigned char*)
"//Common/ZoneListFile";
5522 reader = xmlNewTextReaderFilename(
config);
5523 if (reader != NULL) {
5524 ret = xmlTextReaderRead(reader);
5526 tag_name = (
char*) xmlTextReaderLocalName(reader);
5528 if (strncmp(tag_name,
"Common", 6) == 0
5529 && xmlTextReaderNodeType(reader) == 1) {
5532 xmlTextReaderExpand(reader);
5533 doc = xmlTextReaderCurrentDoc(reader);
5535 printf(
"Error: can not read Common section\n");
5537 ret = xmlTextReaderRead(reader);
5541 xpathCtx = xmlXPathNewContext(doc);
5542 if(xpathCtx == NULL) {
5543 printf(
"Error: can not create XPath context for Common section\n");
5545 ret = xmlTextReaderRead(reader);
5550 xpathObj = xmlXPathEvalExpression(zonelist_expr, xpathCtx);
5551 if(xpathObj == NULL) {
5552 printf(
"Error: unable to evaluate xpath expression: %s\n", zonelist_expr);
5554 ret = xmlTextReaderRead(reader);
5557 *zone_list_filename = NULL;
5558 temp_char = (
char *)xmlXPathCastToString(xpathObj);
5559 xmlXPathFreeObject(xpathObj);
5560 StrAppend(zone_list_filename, temp_char);
5562 printf(
"zonelist filename set to %s.\n", *zone_list_filename);
5565 ret = xmlTextReaderRead(reader);
5568 xmlFreeTextReader(reader);
5570 printf(
"%s : failed to parse\n",
config);
5574 printf(
"Unable to open %s\n",
config);
5578 xmlXPathFreeContext(xpathCtx);
5588 const char *zone_name,
5589 const char *policy_name,
5590 const char *sig_conf_name,
5591 const char *input_name,
5592 const char *output_name)
5596 xmlNodePtr newzonenode;
5597 xmlNodePtr newadaptnode;
5598 xmlNodePtr newinputnode;
5599 xmlNodePtr newoutputnode;
5600 doc = xmlParseFile(docname);
5602 fprintf(stderr,
"Document not parsed successfully. \n");
5605 cur = xmlDocGetRootElement(doc);
5607 fprintf(stderr,
"empty document\n");
5611 if (xmlStrcmp(cur->name, (
const xmlChar *)
"ZoneList")) {
5612 fprintf(stderr,
"document of the wrong type, root node != %s",
"ZoneList");
5616 newzonenode = xmlNewTextChild(cur, NULL, (
const xmlChar *)
"Zone", NULL);
5617 (void) xmlNewProp(newzonenode, (
const xmlChar *)
"name", (
const xmlChar *)zone_name);
5619 (void) xmlNewTextChild (newzonenode, NULL, (
const xmlChar *)
"Policy", (
const xmlChar *)policy_name);
5621 (void) xmlNewTextChild (newzonenode, NULL, (
const xmlChar *)
"SignerConfiguration", (
const xmlChar *)sig_conf_name);
5623 newadaptnode = xmlNewChild (newzonenode, NULL, (
const xmlChar *)
"Adapters", NULL);
5625 newinputnode = xmlNewChild (newadaptnode, NULL, (
const xmlChar *)
"Input", NULL);
5627 (void) xmlNewTextChild (newinputnode, NULL, (
const xmlChar *)
"File", (
const xmlChar *)input_name);
5629 newoutputnode = xmlNewChild (newadaptnode, NULL, (
const xmlChar *)
"Output", NULL);
5631 (void) xmlNewTextChild (newoutputnode, NULL, (
const xmlChar *)
"File", (
const xmlChar *)output_name);
5637 const char *zone_name)
5643 doc = xmlParseFile(docname);
5645 fprintf(stderr,
"Document not parsed successfully. \n");
5648 root = xmlDocGetRootElement(doc);
5650 fprintf(stderr,
"empty document\n");
5654 if (xmlStrcmp(root->name, (
const xmlChar *)
"ZoneList")) {
5655 fprintf(stderr,
"document of the wrong type, root node != %s",
"ZoneList");
5661 if (all_flag == 1) {
5662 cur = root->children;
5668 cur = root->children;
5674 for(cur = root->children; cur != NULL; cur = cur->next)
5677 if (xmlStrcmp( xmlGetProp(cur, (xmlChar *)
"name"), (
const xmlChar *) zone_name) == 0)
5681 cur = root->children;
5696 xmlChar *polChar = NULL;
5697 xmlChar *propChar = NULL;
5703 doc = xmlParseFile(docname);
5705 fprintf(stderr,
"Document not parsed successfully. \n");
5708 root = xmlDocGetRootElement(doc);
5710 fprintf(stderr,
"empty document\n");
5714 if (xmlStrcmp(root->name, (
const xmlChar *)
"ZoneList")) {
5715 fprintf(stderr,
"document of the wrong type, root node != %s",
"ZoneList");
5721 for(cur = root->children; cur != NULL; cur = cur->next)
5723 if (xmlStrcmp( cur->name, (
const xmlChar *)
"Zone") == 0) {
5724 propChar = xmlGetProp(cur, (xmlChar *)
"name");
5725 printf(
"Found Zone: %s", propChar);
5731 printf(
" (zone not in database)");
5734 zone_ids[i] = temp_id;
5739 for(pol = cur->children; pol != NULL; pol = pol->next)
5741 if (xmlStrcmp( pol->name, (
const xmlChar *)
"Policy") == 0)
5743 polChar = xmlNodeGetContent(pol);
5744 printf(
"; on policy %s\n", polChar);
5763 xmlNodePtr policy_node;
5764 xmlNodePtr signatures_node;
5765 xmlNodePtr validity_node;
5766 xmlNodePtr denial_node;
5767 xmlNodePtr nsec_node;
5768 xmlNodePtr hash_node;
5769 xmlNodePtr salt_node;
5770 xmlNodePtr keys_node;
5771 xmlNodePtr ksk_node;
5772 xmlNodePtr ksk_alg_node;
5773 xmlNodePtr zsk_node;
5774 xmlNodePtr zsk_alg_node;
5775 xmlNodePtr zone_node;
5776 xmlNodePtr zone_soa_node;
5777 xmlNodePtr parent_node;
5778 xmlNodePtr parent_ds_node;
5779 xmlNodePtr parent_soa_node;
5783 root = xmlDocGetRootElement(doc);
5785 fprintf(stderr,
"empty document\n");
5788 if (xmlStrcmp(root->name, (
const xmlChar *)
"KASP")) {
5789 fprintf(stderr,
"document of the wrong type, root node != %s",
"KASP");
5793 policy_node = xmlNewTextChild(root, NULL, (
const xmlChar *)
"Policy", NULL);
5794 (void) xmlNewProp(policy_node, (
const xmlChar *)
"name", (
const xmlChar *)policy->
name);
5795 (void) xmlNewTextChild(policy_node, NULL, (
const xmlChar *)
"Description", (
const xmlChar *)policy->
description);
5798 signatures_node = xmlNewTextChild(policy_node, NULL, (
const xmlChar *)
"Signatures", NULL);
5800 (void) xmlNewTextChild(signatures_node, NULL, (
const xmlChar *)
"Resign", (
const xmlChar *)temp_time);
5802 (void) xmlNewTextChild(signatures_node, NULL, (
const xmlChar *)
"Refresh", (
const xmlChar *)temp_time);
5803 validity_node = xmlNewTextChild(signatures_node, NULL, (
const xmlChar *)
"Validity", NULL);
5805 (void) xmlNewTextChild(validity_node, NULL, (
const xmlChar *)
"Default", (
const xmlChar *)temp_time);
5807 (void) xmlNewTextChild(validity_node, NULL, (
const xmlChar *)
"Denial", (
const xmlChar *)temp_time);
5808 snprintf(temp_time, 32,
"PT%dS", policy->
signer->
jitter);
5809 (void) xmlNewTextChild(signatures_node, NULL, (
const xmlChar *)
"Jitter", (
const xmlChar *)temp_time);
5811 (void) xmlNewTextChild(signatures_node, NULL, (
const xmlChar *)
"InceptionOffset", (
const xmlChar *)temp_time);
5814 denial_node = xmlNewTextChild(policy_node, NULL, (
const xmlChar *)
"Denial", NULL);
5817 (void) xmlNewTextChild(denial_node, NULL, (
const xmlChar *)
"NSEC", NULL);
5821 nsec_node = xmlNewTextChild(denial_node, NULL, (
const xmlChar *)
"NSEC3", NULL);
5824 (void) xmlNewTextChild(nsec_node, NULL, (
const xmlChar *)
"OptOut", NULL);
5826 snprintf(temp_time, 32,
"PT%dS", policy->
denial->
resalt);
5827 (void) xmlNewTextChild(nsec_node, NULL, (
const xmlChar *)
"Resalt", (
const xmlChar *)temp_time);
5828 hash_node = xmlNewTextChild(nsec_node, NULL, (
const xmlChar *)
"Hash", NULL);
5830 (void) xmlNewTextChild(hash_node, NULL, (
const xmlChar *)
"Algorithm", (
const xmlChar *)temp_time);
5832 (void) xmlNewTextChild(hash_node, NULL, (
const xmlChar *)
"Iteration", (
const xmlChar *)temp_time);
5834 salt_node = xmlNewTextChild(hash_node, NULL, (
const xmlChar *)
"Salt", NULL);
5835 (void) xmlNewProp(salt_node, (
const xmlChar *)
"length", (
const xmlChar *)temp_time);
5839 keys_node = xmlNewTextChild(policy_node, NULL, (
const xmlChar *)
"Keys", NULL);
5840 snprintf(temp_time, 32,
"PT%dS", policy->
keys->
ttl);
5841 (void) xmlNewTextChild(keys_node, NULL, (
const xmlChar *)
"TTL", (
const xmlChar *)temp_time);
5843 (void) xmlNewTextChild(keys_node, NULL, (
const xmlChar *)
"RetireSafety", (
const xmlChar *)temp_time);
5845 (void) xmlNewTextChild(keys_node, NULL, (
const xmlChar *)
"PublishSafety", (
const xmlChar *)temp_time);
5848 (void) xmlNewTextChild(keys_node, NULL, (
const xmlChar *)
"ShareKeys", NULL);
5851 snprintf(temp_time, 32,
"PT%dS", policy->
keys->
purge);
5852 (void) xmlNewTextChild(keys_node, NULL, (
const xmlChar *)
"Purge", (
const xmlChar *)temp_time);
5856 ksk_node = xmlNewTextChild(keys_node, NULL, (
const xmlChar *)
"KSK", NULL);
5858 ksk_alg_node = xmlNewTextChild(ksk_node, NULL, (
const xmlChar *)
"Algorithm", (
const xmlChar *)temp_time);
5859 snprintf(temp_time, 32,
"%d", policy->
ksk->
bits);
5860 (void) xmlNewProp(ksk_alg_node, (
const xmlChar *)
"length", (
const xmlChar *)temp_time);
5861 snprintf(temp_time, 32,
"PT%dS", policy->
ksk->
lifetime);
5862 (void) xmlNewTextChild(ksk_node, NULL, (
const xmlChar *)
"Lifetime", (
const xmlChar *)temp_time);
5863 (void) xmlNewTextChild(ksk_node, NULL, (
const xmlChar *)
"Repository", (
const xmlChar *)policy->
ksk->
sm_name);
5865 (void) xmlNewTextChild(ksk_node, NULL, (
const xmlChar *)
"Standby", (
const xmlChar *)temp_time);
5868 (void) xmlNewTextChild(ksk_node, NULL, (
const xmlChar *)
"ManualRollover", NULL);
5872 (void) xmlNewTextChild(ksk_node, NULL, (
const xmlChar *)
"RFC5011", NULL);
5880 zsk_node = xmlNewTextChild(keys_node, NULL, (
const xmlChar *)
"ZSK", NULL);
5882 zsk_alg_node = xmlNewTextChild(zsk_node, NULL, (
const xmlChar *)
"Algorithm", (
const xmlChar *)temp_time);
5883 snprintf(temp_time, 32,
"%d", policy->
zsk->
bits);
5884 (void) xmlNewProp(zsk_alg_node, (
const xmlChar *)
"length", (
const xmlChar *)temp_time);
5885 snprintf(temp_time, 32,
"PT%dS", policy->
zsk->
lifetime);
5886 (void) xmlNewTextChild(zsk_node, NULL, (
const xmlChar *)
"Lifetime", (
const xmlChar *)temp_time);
5887 (void) xmlNewTextChild(zsk_node, NULL, (
const xmlChar *)
"Repository", (
const xmlChar *)policy->
zsk->
sm_name);
5889 (void) xmlNewTextChild(zsk_node, NULL, (
const xmlChar *)
"Standby", (
const xmlChar *)temp_time);
5892 (void) xmlNewTextChild(zsk_node, NULL, (
const xmlChar *)
"ManualRollover", NULL);
5896 zone_node = xmlNewTextChild(policy_node, NULL, (
const xmlChar *)
"Zone", NULL);
5898 (void) xmlNewTextChild(zone_node, NULL, (
const xmlChar *)
"PropagationDelay", (
const xmlChar *)temp_time);
5899 zone_soa_node = xmlNewTextChild(zone_node, NULL, (
const xmlChar *)
"SOA", NULL);
5900 snprintf(temp_time, 32,
"PT%dS", policy->
zone->
soa_ttl);
5901 (void) xmlNewTextChild(zone_soa_node, NULL, (
const xmlChar *)
"TTL", (
const xmlChar *)temp_time);
5902 snprintf(temp_time, 32,
"PT%dS", policy->
zone->
soa_min);
5903 (void) xmlNewTextChild(zone_soa_node, NULL, (
const xmlChar *)
"Minimum", (
const xmlChar *)temp_time);
5907 parent_node = xmlNewTextChild(policy_node, NULL, (
const xmlChar *)
"Parent", NULL);
5909 (void) xmlNewTextChild(parent_node, NULL, (
const xmlChar *)
"PropagationDelay", (
const xmlChar *)temp_time);
5910 parent_ds_node = xmlNewTextChild(parent_node, NULL, (
const xmlChar *)
"DS", NULL);
5911 snprintf(temp_time, 32,
"PT%dS", policy->
parent->
ds_ttl);
5912 (void) xmlNewTextChild(parent_ds_node, NULL, (
const xmlChar *)
"TTL", (
const xmlChar *)temp_time);
5913 parent_soa_node = xmlNewTextChild(parent_node, NULL, (
const xmlChar *)
"SOA", NULL);
5915 (void) xmlNewTextChild(parent_soa_node, NULL, (
const xmlChar *)
"TTL", (
const xmlChar *)temp_time);
5917 (void) xmlNewTextChild(parent_soa_node, NULL, (
const xmlChar *)
"Minimum", (
const xmlChar *)temp_time);
5920 if (strncmp(policy->
audit,
"NULL", 4) != 0) {
5921 (void) xmlNewChild(policy_node, NULL, (
const xmlChar *)
"Audit", NULL);
5931 const char *policy_name)
5937 doc = xmlParseFile(docname);
5939 fprintf(stderr,
"Document not parsed successfully. \n");
5942 root = xmlDocGetRootElement(doc);
5944 fprintf(stderr,
"empty document\n");
5948 if (xmlStrcmp(root->name, (
const xmlChar *)
"KASP")) {
5949 fprintf(stderr,
"document of the wrong type, root node != %s",
"KASP");
5956 for(cur = root->children; cur != NULL; cur = cur->next)
5959 if (xmlStrcmp( xmlGetProp(cur, (xmlChar *)
"name"), (
const xmlChar *) policy_name) == 0)
5963 cur = root->children;
5979 fprintf(stdout,
"KSK:");
5983 fprintf(stdout,
"ZSK:");
5985 fprintf(stdout,
" %s Retired\n", key_data->
location);
5997 fprintf(stderr,
"%s\n", format);
6024 char* temp_zone = NULL;
6027 char* temp_ready = NULL;
6028 char* temp_active = NULL;
6029 char* temp_retire = NULL;
6030 char* temp_dead = NULL;
6031 char* temp_loc = NULL;
6032 char* temp_hsm = NULL;
6036 hsm_key_t *key = NULL;
6037 ldns_rr *dnskey_rr = NULL;
6038 hsm_sign_params_t *sign_params = NULL;
6042 status = hsm_open(
config, hsm_prompt_pin, NULL);
6044 hsm_print_error(NULL);
6050 StrAppend(&sql,
"select z.name, k.keytype, k.state, k.ready, k.active, k.retire, k.dead, k.location, s.name, k.algorithm from securitymodules s, zones z, KEYDATA_VIEW k where z.id = k.zone_id and s.id = k.securitymodule_id and state != 6 and zone_id is not null ");
6051 if (zone_id != -1) {
6064 if (verbose_flag == 1) {
6065 printf(
"Zone: Keytype: State: Date of next transition: CKA_ID: Repository: Keytag:\n");
6068 printf(
"Zone: Keytype: State: Date of next transition:\n");
6070 while (status == 0) {
6073 DbInt(row, 1, &temp_type);
6074 DbInt(row, 2, &temp_state);
6081 DbInt(row, 9, &temp_alg);
6093 printf(
"%-31s %-13s %-9s %-26s", temp_zone, (temp_type ==
KSM_TYPE_KSK) ?
"KSK" :
"ZSK",
KsmKeywordStateValueToName(temp_state), (temp_retire == NULL) ?
"(not scheduled)" : temp_retire);
6105 printf(
"%-31s %-13s %-9s %-26s", temp_zone,
"KSK",
KsmKeywordStateValueToName(temp_state), (temp_ready == NULL) ?
"(not scheduled)" : temp_ready);
6113 printf(
"%-31s %-13s %-9s %-26s", temp_zone,
"KSK",
KsmKeywordStateValueToName(temp_state), (temp_active == NULL) ?
"(not scheduled)" : temp_active);
6117 if (done_row == 1 && verbose_flag == 1) {
6118 key = hsm_find_key_by_id(NULL, temp_loc);
6120 printf(
"%-33s %s NOT IN repository\n", temp_loc, temp_hsm);
6122 sign_params = hsm_sign_params_new();
6123 sign_params->owner = ldns_rdf_new_frm_str(LDNS_RDF_TYPE_DNAME, temp_zone);
6124 sign_params->algorithm = temp_alg;
6125 sign_params->flags = LDNS_KEY_ZONE_KEY;
6127 sign_params->flags += LDNS_KEY_SEP_KEY;
6129 dnskey_rr = hsm_get_dnskey(NULL, key, sign_params);
6130 sign_params->keytag = ldns_calc_keytag(dnskey_rr);
6132 printf(
"%-33s %-33s %d\n", temp_loc, temp_hsm, sign_params->keytag);
6134 hsm_sign_params_free(sign_params);
6138 else if (done_row == 1) {
6165 if (dnskey_rr != NULL) {
6166 ldns_rr_free(dnskey_rr);
6204 char* temp_loc = NULL;
6207 int done_something = 0;
6210 hsm_key_t *key = NULL;
6212 if ((zone_id == -1 && policy_id == -1) ||
6213 (zone_id != -1 && policy_id != -1)){
6214 printf(
"Please provide either a zone OR a policy to key purge\n");
6220 status = hsm_open(
config, hsm_prompt_pin, NULL);
6222 hsm_print_error(NULL);
6227 StrAppend(&sql,
"select distinct id, location from KEYDATA_VIEW where state = 6 ");
6228 if (zone_id != -1) {
6233 if (policy_id != -1) {
6244 while (status == 0) {
6246 DbInt(row, 0, &temp_id);
6300 key = hsm_find_key_by_id(NULL, temp_loc);
6303 printf(
"Key not found: %s\n", temp_loc);
6309 status = hsm_remove_key(NULL, key);
6314 printf(
"Key remove successful.\n");
6316 printf(
"Key remove failed.\n");
6336 if (done_something == 0) {
6337 printf(
"No keys to purge.\n");
6355 hsm_ctx_t *ctx = NULL;
6360 hsm_key_t *key = NULL;
6361 char *hsm_error_message = NULL;
6363 int ksks_needed = 0;
6364 int zsks_needed = 0;
6365 int keys_in_queue = 0;
6367 unsigned int current_count = 0;
6373 int ksks_created = 0;
6377 FILE* lock_fd = NULL;
6382 printf(
"Failed to connect to database\n");
6388 if (policy == NULL) {
6389 printf(
"Malloc for policy struct failed\n");
6395 printf(
"Please provide a policy name with the --policy option\n");
6401 printf(
"Please provide an interval with the --interval option\n");
6414 printf(
"Error: unable to read policy %s from database\n",
o_policy);
6420 printf(
"Error: policy %s doesn't exist in database\n",
o_policy);
6427 printf(
"Key sharing is On\n");
6429 printf(
"Key sharing is Off\n");
6434 printf(
"Error: unable to convert Interval %s to seconds, error: ",
o_interval);
6437 printf(
"invalid interval-type.\n");
6440 printf(
"unable to translate string.\n");
6443 printf(
"interval too long to be an int. E.g. Maximum is ~68 years on a system with 32-bit integers.\n");
6446 printf(
"invalid pointers or text string NULL.\n");
6449 printf(
"unknown\n");
6455 else if (status == -1) {
6456 printf(
"Info: converting %s to seconds; M interpreted as 31 days, Y interpreted as 365 days\n",
o_interval);
6460 status = hsm_open(
config, hsm_prompt_pin, NULL);
6462 hsm_error_message = hsm_get_error(ctx);
6463 if (hsm_error_message) {
6464 printf(
"%s\n", hsm_error_message);
6465 free(hsm_error_message);
6471 printf(
"hsm_open() result: HSM error\n");
6473 case HSM_PIN_INCORRECT:
6474 printf(
"hsm_open() result: incorrect PIN\n");
6476 case HSM_CONFIG_FILE_ERROR:
6477 printf(
"hsm_open() result: config file error\n");
6479 case HSM_REPOSITORY_NOT_FOUND:
6480 printf(
"hsm_open() result: repository not found\n");
6482 case HSM_NO_REPOSITORIES:
6483 printf(
"hsm_open() result: no repositories\n");
6486 printf(
"hsm_open() result: %d", status);
6493 printf(
"HSM opened successfully.\n");
6494 ctx = hsm_create_context();
6499 if (rightnow == NULL) {
6500 printf(
"Couldn't turn \"now\" into a date, quitting...\n");
6521 if (zone_count == 0) {
6522 printf(
"No zones on policy %s, skipping...", policy->
name);
6525 hsm_destroy_context(ctx);
6532 printf(
"Could not count zones on policy %s", policy->
name);
6535 hsm_destroy_context(ctx);
6545 printf(
"Could not predict ksk requirement for next interval for %s\n", policy->
name);
6551 printf(
"Could not count current ksk numbers for policy %s\n", policy->
name);
6556 keys_in_queue /= zone_count;
6559 new_keys = ksks_needed - keys_in_queue;
6564 current_count = hsm_count_keys_repository(ctx, policy->
ksk->
sm_name);
6566 printf(
"Repository %s is full, cannot create more KSKs for policy %s\n", policy->
ksk->
sm_name, policy->
name);
6570 printf(
"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);
6576 for (i=new_keys ; i > 0 ; i--){
6577 if (hsm_supported_algorithm(policy->
ksk->
algorithm) == 0) {
6582 printf(
"Created key in repository %s\n", policy->
ksk->
sm_name);
6585 printf(
"Error creating key in repository %s\n", policy->
ksk->
sm_name);
6586 hsm_error_message = hsm_get_error(ctx);
6587 if (hsm_error_message) {
6588 printf(
"%s\n", hsm_error_message);
6589 free(hsm_error_message);
6595 id = hsm_get_key_id(ctx, key);
6599 printf(
"Error creating key in Database\n");
6600 hsm_error_message = hsm_get_error(ctx);
6601 if (hsm_error_message) {
6602 printf(
"%s\n", hsm_error_message);
6603 free(hsm_error_message);
6609 printf(
"Created KSK size: %i, alg: %i with id: %s in repository: %s and database.\n", policy->
ksk->
bits,
6613 printf(
"Key algorithm %d unsupported by libhsm.\n", policy->
ksk->
algorithm);
6619 ksks_created = new_keys;
6629 printf(
"Could not predict zsk requirement for next interval for %s\n", policy->
name);
6635 printf(
"Could not count current zsk numbers for policy %s\n", policy->
name);
6640 keys_in_queue /= zone_count;
6644 keys_in_queue -= ksks_needed;
6647 new_keys = zsks_needed - keys_in_queue;
6652 current_count = hsm_count_keys_repository(ctx, policy->
zsk->
sm_name);
6654 printf(
"Repository %s is full, cannot create more ZSKs for policy %s\n", policy->
zsk->
sm_name, policy->
name);
6658 printf(
"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);
6664 for (i = new_keys ; i > 0 ; i--) {
6665 if (hsm_supported_algorithm(policy->
zsk->
algorithm) == 0) {
6670 printf(
"Created key in repository %s\n", policy->
zsk->
sm_name);
6673 printf(
"Error creating key in repository %s\n", policy->
zsk->
sm_name);
6674 hsm_error_message = hsm_get_error(ctx);
6675 if (hsm_error_message) {
6676 printf(
"%s\n", hsm_error_message);
6677 free(hsm_error_message);
6683 id = hsm_get_key_id(ctx, key);
6687 printf(
"Error creating key in Database\n");
6688 hsm_error_message = hsm_get_error(ctx);
6689 if (hsm_error_message) {
6690 printf(
"%s\n", hsm_error_message);
6691 free(hsm_error_message);
6697 printf(
"Created ZSK size: %i, alg: %i with id: %s in repository: %s and database.\n", policy->
zsk->
bits,
6701 printf(
"Key algorithm %d unsupported by libhsm.\n", policy->
zsk->
algorithm);
6711 printf(
"NOTE: keys generated in repository %s will not become active until they have been backed up\n", policy->
ksk->
sm_name);
6714 printf(
"NOTE: keys generated in repository %s will not become active until they have been backed up\n", policy->
zsk->
sm_name);
6721 hsm_destroy_context(ctx);
6723 status = hsm_close();
6724 printf(
"all done! hsm_close result: %d\n", status);
6738 struct stat stat_ret;
6742 xmlDocPtr doc = NULL;
6743 xmlDocPtr rngdoc = NULL;
6744 xmlXPathContextPtr xpathCtx = NULL;
6745 xmlXPathObjectPtr xpathObj = NULL;
6746 xmlRelaxNGParserCtxtPtr rngpctx = NULL;
6747 xmlRelaxNGValidCtxtPtr rngctx = NULL;
6748 xmlRelaxNGPtr schema = NULL;
6749 xmlChar *user_expr = (
unsigned char*)
"//Configuration/Enforcer/Privileges/User";
6750 xmlChar *group_expr = (
unsigned char*)
"//Configuration/Enforcer/Privileges/Group";
6752 char* filename = OPENDNSSEC_CONFIG_FILE;
6753 char* rngfilename = OPENDNSSEC_SCHEMA_DIR
"/conf.rng";
6754 char* temp_char = NULL;
6761 char *username = NULL;
6762 char *groupname = NULL;
6764 printf(
"fixing permissions on file %s\n", dbschema);
6766 if (geteuid() != 0) {
6771 if (stat(dbschema, &stat_ret) != 0) {
6772 printf(
"cannot stat file %s: %s", dbschema, strerror(errno));
6778 doc = xmlParseFile(filename);
6780 printf(
"Error: unable to parse file \"%s\"", filename);
6785 rngdoc = xmlParseFile(rngfilename);
6786 if (rngdoc == NULL) {
6787 printf(
"Error: unable to parse file \"%s\"", rngfilename);
6792 rngpctx = xmlRelaxNGNewDocParserCtxt(rngdoc);
6793 if (rngpctx == NULL) {
6794 printf(
"Error: unable to create XML RelaxNGs parser context");
6799 schema = xmlRelaxNGParse(rngpctx);
6800 if (schema == NULL) {
6801 printf(
"Error: unable to parse a schema definition resource");
6806 rngctx = xmlRelaxNGNewValidCtxt(schema);
6807 if (rngctx == NULL) {
6808 printf(
"Error: unable to create RelaxNGs validation context based on the schema");
6813 status = xmlRelaxNGValidateDoc(rngctx,doc);
6815 printf(
"Error validating file \"%s\"", filename);
6821 xpathCtx = xmlXPathNewContext(doc);
6822 if(xpathCtx == NULL) {
6823 printf(
"Error: unable to create new XPath context");
6829 xpathObj = xmlXPathEvalExpression(group_expr, xpathCtx);
6830 if(xpathObj == NULL) {
6831 printf(
"Error: unable to evaluate xpath expression: %s", group_expr);
6832 xmlXPathFreeContext(xpathCtx);
6836 if (xpathObj->nodesetval != NULL && xpathObj->nodesetval->nodeNr > 0) {
6837 temp_char = (
char*) xmlXPathCastToString(xpathObj);
6840 xmlXPathFreeObject(xpathObj);
6846 xpathObj = xmlXPathEvalExpression(user_expr, xpathCtx);
6847 if(xpathObj == NULL) {
6848 printf(
"Error: unable to evaluate xpath expression: %s", user_expr);
6849 xmlXPathFreeContext(xpathCtx);
6853 if (xpathObj->nodesetval != NULL && xpathObj->nodesetval->nodeNr > 0) {
6854 temp_char = (
char*) xmlXPathCastToString(xpathObj);
6857 xmlXPathFreeObject(xpathObj);
6863 xmlXPathFreeContext(xpathCtx);
6864 xmlRelaxNGFree(schema);
6865 xmlRelaxNGFreeValidCtxt(rngctx);
6866 xmlRelaxNGFreeParserCtxt(rngpctx);
6871 if (username != NULL) {
6873 if ((pwd = getpwnam(username)) == NULL) {
6874 printf(
"user '%s' does not exist. cannot chown %s...\n", username, dbschema);
6883 if ((grp = getgrnam(groupname)) == NULL) {
6884 printf(
"group '%s' does not exist. cannot chown %s...\n", groupname, dbschema);
6893 if (chown(dbschema, uid, gid) == -1) {
6894 printf(
"cannot chown(%u,%u) %s: %s",
6895 (
unsigned) uid, (
unsigned) gid, dbschema, strerror(errno));
6904 if (chown(temp_char, uid, gid) == -1) {
6905 printf(
"cannot chown(%u,%u) %s: %s",
6906 (
unsigned) uid, (
unsigned) gid, temp_char, strerror(errno));
6948 int CountKeys(
int *zone_id,
int keytag,
const char *cka_id,
int *key_count,
char **temp_cka_id,
int *temp_key_state,
int *temp_keypair_id)
6961 int temp_zone_id = 0;
6962 char* temp_loc = NULL;
6965 int temp_keypair = 0;
6970 hsm_key_t *key = NULL;
6971 ldns_rr *dnskey_rr = NULL;
6972 hsm_sign_params_t *sign_params = NULL;
6975 status = hsm_open(
config, hsm_prompt_pin, NULL);
6977 hsm_print_error(NULL);
6982 nchar = snprintf(buffer,
sizeof(buffer),
"(%d, %d, %d)",
6984 if (nchar >=
sizeof(buffer)) {
6985 printf(
"Error: Overran buffer in CountKeys\n");
6990 StrAppend(&sql,
"select k.zone_id, k.location, k.algorithm, k.state, k.id from KEYDATA_VIEW k where state in ");
6992 StrAppend(&sql,
" and zone_id is not null and k.keytype = 257");
6994 if (*zone_id != -1) {
6999 if (cka_id != NULL) {
7017 while (status == 0) {
7019 DbInt(row, 0, &temp_zone_id);
7021 DbInt(row, 2, &temp_alg);
7022 DbInt(row, 3, &temp_state);
7023 DbInt(row, 4, &temp_keypair);
7027 if (keytag == -1 && cka_id == NULL)
7029 *temp_key_state = temp_state;
7032 key = hsm_find_key_by_id(NULL, temp_loc);
7034 printf(
"cka_id %-33s in DB but NOT IN repository\n", temp_loc);
7035 }
else if (keytag != -1) {
7036 sign_params = hsm_sign_params_new();
7037 sign_params->owner = ldns_rdf_new_frm_str(LDNS_RDF_TYPE_DNAME,
"temp_zone");
7038 sign_params->algorithm = temp_alg;
7039 sign_params->flags = LDNS_KEY_ZONE_KEY;
7040 sign_params->flags += LDNS_KEY_SEP_KEY;
7042 dnskey_rr = hsm_get_dnskey(NULL, key, sign_params);
7043 sign_params->keytag = ldns_calc_keytag(dnskey_rr);
7046 if (keytag == sign_params->keytag) {
7049 *temp_cka_id = NULL;
7051 *zone_id = temp_zone_id;
7052 *temp_key_state = temp_state;
7053 *temp_keypair_id = temp_keypair;
7054 printf(
"Found key with CKA_ID %s\n", temp_loc);
7057 hsm_sign_params_free(sign_params);
7059 if (key && cka_id != NULL && strncmp(cka_id, temp_loc, strlen(temp_loc)) == 0) {
7061 if (done_row == 0) {
7063 *temp_cka_id = NULL;
7065 *zone_id = temp_zone_id;
7066 *temp_key_state = temp_state;
7067 *temp_keypair_id = temp_keypair;
7068 printf(
"Found key with CKA_ID %s\n", temp_loc);
7088 *key_count = temp_count;
7095 if (dnskey_rr != NULL) {
7096 ldns_rr_free(dnskey_rr);
7129 int MarkDSSeen(
int keypair_id,
int zone_id,
int policy_id,
const char *datetime,
int key_state)
7148 printf(
"Error: failed to read policy\n");
7169 nchar = snprintf(buffer,
sizeof(buffer),
7170 "DATE_ADD('%s', INTERVAL %d SECOND) ", datetime, deltat);
7172 nchar = snprintf(buffer,
sizeof(buffer),
7173 "DATETIME('%s', '+%d SECONDS') ", datetime, deltat);
7194 nchar = snprintf(buffer,
sizeof(buffer),
7195 "DATE_ADD('%s', INTERVAL %d SECOND) ", datetime, deltat);
7197 nchar = snprintf(buffer,
sizeof(buffer),
7198 "DATETIME('%s', '+%d SECONDS') ", datetime, deltat);
7259 char* where_clause = NULL;
7275 printf(
"Error: failed to read policy\n");
7291 StrAppend(&where_clause,
"select id from KEYDATA_VIEW where state = 4 and keytype = 257 and zone_id = ");
7293 StrAppend(&where_clause,
" and retire = (select min(retire) from KEYDATA_VIEW where state = 4 and keytype = 257 and zone_id = ");
7302 printf(
"Error: failed to find ID of key to retire\n");
7311 nchar = snprintf(buffer,
sizeof(buffer),
7312 "DATE_ADD('%s', INTERVAL %d SECOND) ", datetime, deltat);
7314 nchar = snprintf(buffer,
sizeof(buffer),
7315 "DATETIME('%s', '+%d SECONDS') ", datetime, deltat);
7382 if (zone_id != -1) {
7391 printf(
"Error in CountKeysInState\n");
7428 int ChangeKeyState(
int keytype,
const char *cka_id,
int zone_id,
int policy_id,
const char *datetime,
int keystate)
7457 printf(
"Error: failed to read policy\n");
7465 if (zone_id != -1) {
7484 keyids =
MemMalloc(count *
sizeof(
int));
7491 if (zone_id != -1) {
7500 while (status == 0) {
7501 status =
KsmKey(result, &data);
7532 for (j = 0; j < i; ++j) {
7536 snprintf(buffer,
sizeof(buffer),
"%d", keyids[j]);
7559 nchar = snprintf(buffer,
sizeof(buffer),
7560 "DATE_ADD('%s', INTERVAL %d SECOND) ", datetime, deltat);
7562 nchar = snprintf(buffer,
sizeof(buffer),
7563 "DATETIME('%s', '+%d SECONDS') ", datetime, deltat);
7573 if (zone_id != -1) {
7591 nchar = snprintf(buffer,
sizeof(buffer),
7592 "DATE_ADD('%s', INTERVAL %d SECOND) ", datetime, deltat);
7594 nchar = snprintf(buffer,
sizeof(buffer),
7595 "DATETIME('%s', '+%d SECONDS') ", datetime, deltat);
7605 if (zone_id != -1) {
7616 nchar = snprintf(buffer,
sizeof(buffer),
7617 "DATE_ADD('%s', INTERVAL %d SECOND) ", datetime, deltat);
7619 nchar = snprintf(buffer,
sizeof(buffer),
7620 "DATETIME('%s', '+%d SECONDS') ", datetime, deltat);
7630 if (zone_id != -1) {
7665 static int restart_enforcerd()
7669 return system(RESTART_ENFORCERD_CMD);
7681 xmlDocPtr doc = NULL;
7682 xmlXPathContextPtr xpathCtx = NULL;
7683 xmlXPathObjectPtr xpathObj = NULL;
7684 char* temp_char = NULL;
7686 xmlChar *iv_expr = (
unsigned char*)
"//Configuration/Enforcer/Interval";
7687 xmlChar *mk_expr = (
unsigned char*)
"//Configuration/Enforcer/ManualKeyGeneration";
7690 doc = xmlParseFile(
config);
7692 printf(
"Error: unable to parse file \"%s\"\n",
config);
7697 xpathCtx = xmlXPathNewContext(doc);
7698 if(xpathCtx == NULL) {
7699 printf(
"Error: unable to create new XPath context\n");
7705 xpathObj = xmlXPathEvalExpression(iv_expr, xpathCtx);
7706 if(xpathObj == NULL) {
7707 printf(
"Error: unable to evaluate xpath expression: %s", iv_expr);
7708 xmlXPathFreeContext(xpathCtx);
7713 temp_char = (
char *)xmlXPathCastToString(xpathObj);
7716 printf(
"Error: unable to convert Interval %s to seconds, error: %i\n", temp_char, status);
7720 else if (status == -1) {
7721 printf(
"Info: converting %s to seconds; M interpreted as 31 days, Y interpreted as 365 days\n", temp_char);
7725 xmlXPathFreeObject(xpathObj);
7728 xpathObj = xmlXPathEvalExpression(mk_expr, xpathCtx);
7729 if(xpathObj == NULL) {
7730 printf(
"Error: unable to evaluate xpath expression: %s\n", mk_expr);
7731 xmlXPathFreeContext(xpathCtx);
7736 if (xpathObj->nodesetval != NULL && xpathObj->nodesetval->nodeNr > 0) {
7744 xmlXPathFreeObject(xpathObj);
7747 xmlXPathFreeContext(xpathCtx);
7787 int man_key_gen = -1;
7798 printf(
"Failed to Link Keys to zone\n");
7808 if (policy == NULL) {
7809 printf(
"Malloc for policy struct failed\n");
7819 printf(
"Error: unable to read policy %s from database\n",
o_policy);
7824 printf(
"Error: policy %s doesn't exist in database\n",
o_policy);
7832 printf(
"Error allocating zsks to zone %s", zone_name);
7838 printf(
"Error allocating ksks to zone %s", zone_name);
7880 int keys_needed = 0;
7881 int keys_in_queue = 0;
7882 int keys_pending_retirement = 0;
7884 int key_pair_id = 0;
7891 if (datetime == NULL) {
7892 printf(
"Couldn't turn \"now\" into a date, quitting...");
7896 if (policy == NULL) {
7897 printf(
"NULL policy sent to allocateKeysToZone");
7903 printf(
"Unknown keytype: %i in allocateKeysToZone", key_type);
7917 status =
KsmKeyPredict(policy->
id, key_type, 1, interval, &keys_needed, rollover_scheme, 1);
7919 printf(
"Could not predict key requirement for next interval for %s", zone_name);
7927 printf(
"Could not count current key numbers for zone %s", zone_name);
7935 printf(
"Could not count keys which may retire before the next run (for zone %s)", zone_name);
7941 new_keys = keys_needed - (keys_in_queue - keys_pending_retirement);
7946 for (i=0 ; i < new_keys ; i++){
7950 if (status == -1 || key_pair_id == 0) {
7951 if (man_key_gen == 0) {
7952 printf(
"Not enough keys to satisfy ksk policy for zone: %s", zone_name);
7953 printf(
"ods-enforcerd will create some more keys on its next run");
7956 printf(
"Not enough keys to satisfy ksk policy for zone: %s", zone_name);
7957 printf(
"please use \"ods-ksmutil key generate\" to create some more keys.");
7961 else if (status != 0) {
7962 printf(
"Could not get an unallocated ksk for zone: %s", zone_name);
7967 if (status == -1 || key_pair_id == 0) {
7968 if (man_key_gen == 0) {
7969 printf(
"Not enough keys to satisfy zsk policy for zone: %s", zone_name);
7970 printf(
"ods-enforcerd will create some more keys on its next run");
7973 printf(
"Not enough keys to satisfy zsk policy for zone: %s", zone_name);
7974 printf(
"please use \"ods-ksmutil key generate\" to create some more keys.");
7978 else if (status != 0) {
7979 printf(
"Could not get an unallocated zsk for zone: %s", zone_name);
7983 if(key_pair_id > 0) {
7988 printf(
"KsmKeyGetUnallocated returned bad key_id %d for zone: %s; exiting...", key_pair_id, zone_name);
8016 int keyRoll(
int zone_id,
int policy_id,
int key_type)
8029 int temp_zone_id = -1;
8035 char* insql1 = NULL;
8036 char* insql2 = NULL;
8042 if (datetime == NULL) {
8043 printf(
"Couldn't turn \"now\" into a date, quitting...\n");
8051 if (zone_id != -1) {
8054 if (policy_id != -1) {
8058 if (key_type != -1) {
8067 while (status == 0) {
8069 DbInt(row, 0, &temp_id);
8070 DbInt(row, 1, &temp_type);
8074 DusSetInt(&sql1,
"compromisedflag", 1, 1);
8135 size = snprintf(sql2,
KSM_SQL_SIZE,
"select zone_id from dnsseckeys where retire = \"%s\" and keypair_id = %d", datetime, temp_id);
8139 while (status == 0) {
8141 DbInt(row2, 0, &temp_zone_id);
8146 snprintf(buffer,
sizeof(buffer),
"%d", temp_zone_id);
8169 while (status == 0) {
8171 DbInt(row2, 0, &temp_zone_id);
8176 snprintf(buffer,
sizeof(buffer),
"%d", temp_zone_id);
8198 printf(
"Couldn't construct SQL to promote standby key\n");
8261 else if (status == -1) {}
8276 xmlNodePtr zone_node;
8277 xmlNodePtr adapters_node;
8278 xmlNodePtr input_node;
8279 xmlNodePtr output_node;
8281 root = xmlDocGetRootElement(doc);
8283 fprintf(stderr,
"empty document\n");
8286 if (xmlStrcmp(root->name, (
const xmlChar *)
"ZoneList")) {
8287 fprintf(stderr,
"document of the wrong type, root node != %s",
"ZoneList");
8291 zone_node = xmlNewTextChild(root, NULL, (
const xmlChar *)
"Zone", NULL);
8292 (void) xmlNewProp(zone_node, (
const xmlChar *)
"name", (
const xmlChar *)zone->
name);
8295 (void) xmlNewTextChild(zone_node, NULL, (
const xmlChar *)
"Policy", (
const xmlChar *)zone->
policy_name);
8298 (void) xmlNewTextChild(zone_node, NULL, (
const xmlChar *)
"SignerConfiguration", (
const xmlChar *)zone->
signconf);
8301 adapters_node = xmlNewTextChild(zone_node, NULL, (
const xmlChar *)
"Adapters", NULL);
8303 input_node = xmlNewTextChild(adapters_node, NULL, (
const xmlChar *)
"Input", NULL);
8304 (void) xmlNewTextChild(input_node, NULL, (
const xmlChar *)
"File", (
const xmlChar *)zone->
input);
8306 output_node = xmlNewTextChild(adapters_node, NULL, (
const xmlChar *)
"Output", NULL);
8307 (void) xmlNewTextChild(output_node, NULL, (
const xmlChar *)
"File", (
const xmlChar *)zone->
output);
8318 size_t len = strlen(
string);
8321 for (i = 0; i < len; ++i) {
8322 if (
string[i] ==
'\'') {
8327 buffer[j++] =
string[i];
8331 return ( (j <= buflen) ? 0 : 1);
8336 char* signconf = NULL;
8337 char* moved_signconf = NULL;
8338 char* zone_name = NULL;
8342 xmlDocPtr doc = NULL;
8344 xmlXPathContextPtr xpathCtx = NULL;
8345 xmlXPathObjectPtr xpathObj = NULL;
8347 xmlChar *node_expr = (
unsigned char*)
"//Zone";
8349 doc = xmlParseFile(zonelist_filename);
8351 printf(
"Error: unable to parse file \"%s\"\n", zonelist_filename);
8355 xpathCtx = xmlXPathNewContext(doc);
8356 if(xpathCtx == NULL) {
8362 xpathObj = xmlXPathEvalExpression(node_expr, xpathCtx);
8363 if(xpathObj == NULL) {
8364 xmlXPathFreeContext(xpathCtx);
8369 if (xpathObj->nodesetval) {
8370 for (i = 0; i < xpathObj->nodesetval->nodeNr; i++) {
8372 curNode = xpathObj->nodesetval->nodeTab[i]->xmlChildrenNode;
8373 zone_name = (
char *) xmlGetProp(xpathObj->nodesetval->nodeTab[i], (
const xmlChar *)
"name");
8375 if (all_flag || (strlen(zone_name) == strlen(o_zone) &&
8376 strncmp(zone_name, o_zone, strlen(zone_name)) == 0)) {
8380 if (xmlStrEqual(curNode->name, (
const xmlChar *)
"SignerConfiguration")) {
8381 StrAppend(&signconf, (
char *) xmlNodeGetContent(curNode));
8383 StrAppend(&moved_signconf,
".ZONE_DELETED");
8385 status = rename(signconf, moved_signconf);
8386 if (status != 0 && errno != ENOENT)
8389 printf(
"Could not rename: %s -> %s", signconf, moved_signconf);
8400 curNode = curNode->next;