signer.c revision b18192fd96726bf2cf553bd0e209dfe231abb1d9
07dcadc6e24f4572f2b7d7cae7d47d0f5256d129Andreas Gustafsson
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence#include <config.h>
7c74e180c206e6ed99e8beb820da5f399d845c3eDavid Lawrence
7c74e180c206e6ed99e8beb820da5f399d845c3eDavid Lawrence#include <stdio.h>
7c74e180c206e6ed99e8beb820da5f399d845c3eDavid Lawrence#include <stdlib.h>
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence#include <string.h>
15a44745412679c30a6d022733925af70a38b715David Lawrence
15a44745412679c30a6d022733925af70a38b715David Lawrence#include <isc/types.h>
15a44745412679c30a6d022733925af70a38b715David Lawrence#include <isc/assertions.h>
15a44745412679c30a6d022733925af70a38b715David Lawrence#include <isc/boolean.h>
15a44745412679c30a6d022733925af70a38b715David Lawrence#include <isc/buffer.h>
15a44745412679c30a6d022733925af70a38b715David Lawrence#include <isc/error.h>
15a44745412679c30a6d022733925af70a38b715David Lawrence#include <isc/mem.h>
15a44745412679c30a6d022733925af70a38b715David Lawrence#include <isc/stdtime.h>
9c3531d72aeaad6c5f01efe6a1c82023e1379e4dDavid Lawrence
866d106459313499d0ca7bfccb4b2d23d5e4377cDavid Lawrence#include <dns/types.h>
866d106459313499d0ca7bfccb4b2d23d5e4377cDavid Lawrence#include <dns/name.h>
866d106459313499d0ca7bfccb4b2d23d5e4377cDavid Lawrence#include <dns/fixedname.h>
866d106459313499d0ca7bfccb4b2d23d5e4377cDavid Lawrence#include <dns/db.h>
7c74e180c206e6ed99e8beb820da5f399d845c3eDavid Lawrence#include <dns/dbiterator.h>
7c014c5bf41dc38802e8889c0a9110204eb1a552Andreas Gustafsson#include <dns/rdata.h>
ea31416b4fcdf23732355a8002f93f29e3b3d2dbAndreas Gustafsson#include <dns/rdatalist.h>
866d106459313499d0ca7bfccb4b2d23d5e4377cDavid Lawrence#include <dns/rdataset.h>
64e41159a919b0711321fe688ca5da4f4d1b7d80Bob Halley#include <dns/rdatasetiter.h>
a5d43b72413db3edd6b36a58f9bdf2cf6ff692f2Bob Halley#include <dns/rdatastruct.h>
a5d43b72413db3edd6b36a58f9bdf2cf6ff692f2Bob Halley#include <dns/result.h>
be801b0fdbcf9b55b3a8cc6bf042ff6c86be6b11Mark Andrews#include <dns/dnssec.h>
a5d43b72413db3edd6b36a58f9bdf2cf6ff692f2Bob Halley#include <dns/keyvalues.h>
a5d43b72413db3edd6b36a58f9bdf2cf6ff692f2Bob Halley#include <dns/nxt.h>
a5d43b72413db3edd6b36a58f9bdf2cf6ff692f2Bob Halley
ccdac53c027e8964753b36c4d8c7b0e98af501c2Michael Graff#include <dst/dst.h>
ccdac53c027e8964753b36c4d8c7b0e98af501c2Michael Graff
ccdac53c027e8964753b36c4d8c7b0e98af501c2Michael Graff#define MAXKEYS 10
ccdac53c027e8964753b36c4d8c7b0e98af501c2Michael Graff#define is_zone_key(key) ((dst_key_flags(key) & DNS_KEYFLAG_OWNERMASK) \
ccdac53c027e8964753b36c4d8c7b0e98af501c2Michael Graff == DNS_KEYOWNER_ZONE)
ccdac53c027e8964753b36c4d8c7b0e98af501c2Michael Graff
ccdac53c027e8964753b36c4d8c7b0e98af501c2Michael Graffstatic isc_mem_t *mctx = NULL;
3d776d762914d1b675b4fd49728ce353ccf6f77eBrian Wellington
ccdac53c027e8964753b36c4d8c7b0e98af501c2Michael Graffstatic inline void
75a4dd0d377dca2f85cea44e28bf110314c1fe8cDavid Lawrencefatal(char *message) {
75a4dd0d377dca2f85cea44e28bf110314c1fe8cDavid Lawrence fprintf(stderr, "%s\n", message);
75a4dd0d377dca2f85cea44e28bf110314c1fe8cDavid Lawrence exit(1);
75a4dd0d377dca2f85cea44e28bf110314c1fe8cDavid Lawrence}
75a4dd0d377dca2f85cea44e28bf110314c1fe8cDavid Lawrence
91306d962f9d147d94b82fb14edb28f8d907cae7Andreas Gustafssonstatic inline void
91306d962f9d147d94b82fb14edb28f8d907cae7Andreas Gustafssoncheck_result(isc_result_t result, char *message) {
91306d962f9d147d94b82fb14edb28f8d907cae7Andreas Gustafsson if (result != ISC_R_SUCCESS) {
91306d962f9d147d94b82fb14edb28f8d907cae7Andreas Gustafsson fprintf(stderr, "%s: %s\n", message,
91306d962f9d147d94b82fb14edb28f8d907cae7Andreas Gustafsson isc_result_totext(result));
e893dce91279d7313a579f72caae3941f6dc5a27David Lawrence exit(1);
e893dce91279d7313a579f72caae3941f6dc5a27David Lawrence }
e893dce91279d7313a579f72caae3941f6dc5a27David Lawrence}
e893dce91279d7313a579f72caae3941f6dc5a27David Lawrence
e893dce91279d7313a579f72caae3941f6dc5a27David Lawrencestatic void
e893dce91279d7313a579f72caae3941f6dc5a27David Lawrenceset_bit(unsigned char *array, unsigned int index, unsigned int bit) {
e893dce91279d7313a579f72caae3941f6dc5a27David Lawrence unsigned int byte, shift, mask;
e893dce91279d7313a579f72caae3941f6dc5a27David Lawrence
e893dce91279d7313a579f72caae3941f6dc5a27David Lawrence byte = array[index / 8];
e893dce91279d7313a579f72caae3941f6dc5a27David Lawrence shift = 7 - (index % 8);
3b77946b751f39bd4db5a7d1fe48a81e6b1e7a28Bob Halley mask = 1 << shift;
3b77946b751f39bd4db5a7d1fe48a81e6b1e7a28Bob Halley
3b77946b751f39bd4db5a7d1fe48a81e6b1e7a28Bob Halley if (bit)
8e06cea14c857429ab7e7299af2dce5eeeaa5ff0Michael Graff array[index / 8] |= mask;
ce8c568e0d6106bb87069453505e09bc66754b40Andreas Gustafsson else
3b77946b751f39bd4db5a7d1fe48a81e6b1e7a28Bob Halley array[index / 8] &= (~mask & 0xFF);
3b77946b751f39bd4db5a7d1fe48a81e6b1e7a28Bob Halley}
3b77946b751f39bd4db5a7d1fe48a81e6b1e7a28Bob Halley
3b77946b751f39bd4db5a7d1fe48a81e6b1e7a28Bob Halleydns_result_t
3b77946b751f39bd4db5a7d1fe48a81e6b1e7a28Bob Halleyfind_apex_keys(dns_db_t *db, dns_dbversion_t *ver, dns_dbnode_t *node,
3b77946b751f39bd4db5a7d1fe48a81e6b1e7a28Bob Halley dns_name_t *name, isc_mem_t *mctx, unsigned int maxkeys,
3b77946b751f39bd4db5a7d1fe48a81e6b1e7a28Bob Halley dst_key_t **keys, unsigned int *nkeys)
3b77946b751f39bd4db5a7d1fe48a81e6b1e7a28Bob Halley{
3b77946b751f39bd4db5a7d1fe48a81e6b1e7a28Bob Halley dns_rdataset_t rdataset;
3b77946b751f39bd4db5a7d1fe48a81e6b1e7a28Bob Halley dns_rdata_t rdata;
3b77946b751f39bd4db5a7d1fe48a81e6b1e7a28Bob Halley isc_result_t result;
3b77946b751f39bd4db5a7d1fe48a81e6b1e7a28Bob Halley dst_key_t *pubkey;
3b77946b751f39bd4db5a7d1fe48a81e6b1e7a28Bob Halley unsigned int count = 0;
8e06cea14c857429ab7e7299af2dce5eeeaa5ff0Michael Graff
8e06cea14c857429ab7e7299af2dce5eeeaa5ff0Michael Graff *nkeys = 0;
3ecf3394e37dc2848a09ffc643565d454e9e6974Andreas Gustafsson dns_rdataset_init(&rdataset);
3ecf3394e37dc2848a09ffc643565d454e9e6974Andreas Gustafsson result = dns_db_findrdataset(db, node, ver, dns_rdatatype_key, 0, 0,
3ecf3394e37dc2848a09ffc643565d454e9e6974Andreas Gustafsson &rdataset, NULL);
3ecf3394e37dc2848a09ffc643565d454e9e6974Andreas Gustafsson check_result(result, "dns_db_findrdataset()");
3ecf3394e37dc2848a09ffc643565d454e9e6974Andreas Gustafsson result = dns_rdataset_first(&rdataset);
3ecf3394e37dc2848a09ffc643565d454e9e6974Andreas Gustafsson check_result(result, "dns_rdataset_first()");
df3c4c7988b9bae7d121a8ac9ed17a23366a948dDavid Lawrence while (result == ISC_R_SUCCESS && count < maxkeys) {
df3c4c7988b9bae7d121a8ac9ed17a23366a948dDavid Lawrence pubkey = NULL;
df3c4c7988b9bae7d121a8ac9ed17a23366a948dDavid Lawrence dns_rdataset_current(&rdataset, &rdata);
df3c4c7988b9bae7d121a8ac9ed17a23366a948dDavid Lawrence result = dns_dnssec_keyfromrdata(name, &rdata, mctx, &pubkey);
df3c4c7988b9bae7d121a8ac9ed17a23366a948dDavid Lawrence check_result(result, "dns_dnssec_keyfromrdata()");
df3c4c7988b9bae7d121a8ac9ed17a23366a948dDavid Lawrence result = dst_key_fromfile(dst_key_name(pubkey),
df3c4c7988b9bae7d121a8ac9ed17a23366a948dDavid Lawrence dst_key_id(pubkey),
df3c4c7988b9bae7d121a8ac9ed17a23366a948dDavid Lawrence dst_key_alg(pubkey),
df3c4c7988b9bae7d121a8ac9ed17a23366a948dDavid Lawrence DST_TYPE_PRIVATE,
df3c4c7988b9bae7d121a8ac9ed17a23366a948dDavid Lawrence mctx, &keys[count++]);
df3c4c7988b9bae7d121a8ac9ed17a23366a948dDavid Lawrence check_result(result, "dst_key_fromfile()");
df3c4c7988b9bae7d121a8ac9ed17a23366a948dDavid Lawrence dst_key_free(pubkey);
df3c4c7988b9bae7d121a8ac9ed17a23366a948dDavid Lawrence pubkey = NULL;
df3c4c7988b9bae7d121a8ac9ed17a23366a948dDavid Lawrence result = dns_rdataset_next(&rdataset);
df3c4c7988b9bae7d121a8ac9ed17a23366a948dDavid Lawrence }
df3c4c7988b9bae7d121a8ac9ed17a23366a948dDavid Lawrence if (result != DNS_R_NOMORE)
df3c4c7988b9bae7d121a8ac9ed17a23366a948dDavid Lawrence check_result(result, "iteration over zone keys");
df3c4c7988b9bae7d121a8ac9ed17a23366a948dDavid Lawrence result = DNS_R_SUCCESS;
df3c4c7988b9bae7d121a8ac9ed17a23366a948dDavid Lawrence if (count == 0)
df3c4c7988b9bae7d121a8ac9ed17a23366a948dDavid Lawrence check_result(ISC_R_FAILURE, "no key found");
ae4cbb69eef32ced103fe4561e8d2031ee4c3497David Lawrence
ae4cbb69eef32ced103fe4561e8d2031ee4c3497David Lawrence/* failure:*/
ae4cbb69eef32ced103fe4561e8d2031ee4c3497David Lawrence if (dns_rdataset_isassociated(&rdataset))
ae4cbb69eef32ced103fe4561e8d2031ee4c3497David Lawrence dns_rdataset_disassociate(&rdataset);
ae4cbb69eef32ced103fe4561e8d2031ee4c3497David Lawrence if (pubkey != NULL)
ae4cbb69eef32ced103fe4561e8d2031ee4c3497David Lawrence dst_key_free(pubkey);
ae4cbb69eef32ced103fe4561e8d2031ee4c3497David Lawrence *nkeys = count;
ae4cbb69eef32ced103fe4561e8d2031ee4c3497David Lawrence return (result);
ae4cbb69eef32ced103fe4561e8d2031ee4c3497David Lawrence}
ae4cbb69eef32ced103fe4561e8d2031ee4c3497David Lawrence
ae4cbb69eef32ced103fe4561e8d2031ee4c3497David Lawrence
ae4cbb69eef32ced103fe4561e8d2031ee4c3497David Lawrencestatic void
ae4cbb69eef32ced103fe4561e8d2031ee4c3497David Lawrencesign_with_key(dns_name_t *name, dns_rdataset_t *rdataset, dns_rdata_t *rdata,
ae4cbb69eef32ced103fe4561e8d2031ee4c3497David Lawrence dns_rdatalist_t *sigrdatalist, isc_stdtime_t *now,
ae4cbb69eef32ced103fe4561e8d2031ee4c3497David Lawrence isc_stdtime_t *later, dst_key_t *key,
df3c4c7988b9bae7d121a8ac9ed17a23366a948dDavid Lawrence unsigned char *array, int len)
df3c4c7988b9bae7d121a8ac9ed17a23366a948dDavid Lawrence{
df3c4c7988b9bae7d121a8ac9ed17a23366a948dDavid Lawrence isc_buffer_t b;
df3c4c7988b9bae7d121a8ac9ed17a23366a948dDavid Lawrence isc_region_t r;
df3c4c7988b9bae7d121a8ac9ed17a23366a948dDavid Lawrence isc_result_t result;
df3c4c7988b9bae7d121a8ac9ed17a23366a948dDavid Lawrence
df3c4c7988b9bae7d121a8ac9ed17a23366a948dDavid Lawrence r.base = array;
df3c4c7988b9bae7d121a8ac9ed17a23366a948dDavid Lawrence r.length = len;
0293ad13207aa29bd5844cdc87d085ffc009d749David Lawrence memset(r.base, 0, r.length);
0293ad13207aa29bd5844cdc87d085ffc009d749David Lawrence
0293ad13207aa29bd5844cdc87d085ffc009d749David Lawrence dns_rdata_init(rdata);
0293ad13207aa29bd5844cdc87d085ffc009d749David Lawrence isc_buffer_init(&b, r.base, r.length, ISC_BUFFERTYPE_BINARY);
df3c4c7988b9bae7d121a8ac9ed17a23366a948dDavid Lawrence result = dns_dnssec_sign(name, rdataset, key, now, later,
df3c4c7988b9bae7d121a8ac9ed17a23366a948dDavid Lawrence mctx, &b, rdata);
df3c4c7988b9bae7d121a8ac9ed17a23366a948dDavid Lawrence check_result(result, "dns_dnssec_sign()");
0293ad13207aa29bd5844cdc87d085ffc009d749David Lawrence result = dns_dnssec_verify(name, rdataset, key, mctx, rdata);
0293ad13207aa29bd5844cdc87d085ffc009d749David Lawrence check_result(result, "dns_dnssec_verify()");
0293ad13207aa29bd5844cdc87d085ffc009d749David Lawrence ISC_LIST_APPEND(sigrdatalist->rdata, rdata, link);
0293ad13207aa29bd5844cdc87d085ffc009d749David Lawrence}
0293ad13207aa29bd5844cdc87d085ffc009d749David Lawrence
0293ad13207aa29bd5844cdc87d085ffc009d749David Lawrencevoid
0293ad13207aa29bd5844cdc87d085ffc009d749David Lawrenceresign_set(dns_name_t *name, dns_name_t *origin, dns_rdataset_t *rdataset,
0293ad13207aa29bd5844cdc87d085ffc009d749David Lawrence dns_rdata_t *rdata, dns_rdatalist_t *sigrdatalist,
0293ad13207aa29bd5844cdc87d085ffc009d749David Lawrence dns_rdata_t *oldsigrdata, isc_stdtime_t *now, isc_stdtime_t *later,
0293ad13207aa29bd5844cdc87d085ffc009d749David Lawrence dst_key_t **keys, int nkeys, unsigned char *array, int len)
0293ad13207aa29bd5844cdc87d085ffc009d749David Lawrence{
df3c4c7988b9bae7d121a8ac9ed17a23366a948dDavid Lawrence dns_rdata_generic_sig_t sig;
df3c4c7988b9bae7d121a8ac9ed17a23366a948dDavid Lawrence isc_result_t result;
df3c4c7988b9bae7d121a8ac9ed17a23366a948dDavid Lawrence isc_buffer_t b;
df3c4c7988b9bae7d121a8ac9ed17a23366a948dDavid Lawrence int i;
df3c4c7988b9bae7d121a8ac9ed17a23366a948dDavid Lawrence isc_boolean_t done = ISC_FALSE;
df3c4c7988b9bae7d121a8ac9ed17a23366a948dDavid Lawrence isc_boolean_t foundnonzone = ISC_FALSE;
df3c4c7988b9bae7d121a8ac9ed17a23366a948dDavid Lawrence
df3c4c7988b9bae7d121a8ac9ed17a23366a948dDavid Lawrence result = dns_rdata_tostruct(oldsigrdata, &sig, mctx);
657ce0b9d84fbd66514df53d61a087e8f1161187Michael Graff check_result(result, "dns_rdata_tostruct()");
657ce0b9d84fbd66514df53d61a087e8f1161187Michael Graff
657ce0b9d84fbd66514df53d61a087e8f1161187Michael Graff /*
657ce0b9d84fbd66514df53d61a087e8f1161187Michael Graff * Is this a real signture that we should regenerate?
657ce0b9d84fbd66514df53d61a087e8f1161187Michael Graff */
1ce985ab3c6670662d555c108b35fed84a6a1001David Lawrence if (dns_name_compare(sig.signer, origin) == 0) {
df3c4c7988b9bae7d121a8ac9ed17a23366a948dDavid Lawrence for (i = 0; i < nkeys; i++) {
df3c4c7988b9bae7d121a8ac9ed17a23366a948dDavid Lawrence dst_key_t *key = keys[i];
df3c4c7988b9bae7d121a8ac9ed17a23366a948dDavid Lawrence if (dst_key_id(key) == sig.keyid &&
df3c4c7988b9bae7d121a8ac9ed17a23366a948dDavid Lawrence dst_key_alg(key) == sig.algorithm)
df3c4c7988b9bae7d121a8ac9ed17a23366a948dDavid Lawrence {
df3c4c7988b9bae7d121a8ac9ed17a23366a948dDavid Lawrence if (!is_zone_key(key))
4bcaefbcd3ced942139fdc830e007c6ea2b8d2feDavid Lawrence foundnonzone = ISC_TRUE;
4bcaefbcd3ced942139fdc830e007c6ea2b8d2feDavid Lawrence else
4bcaefbcd3ced942139fdc830e007c6ea2b8d2feDavid Lawrence break;
4bcaefbcd3ced942139fdc830e007c6ea2b8d2feDavid Lawrence }
4bcaefbcd3ced942139fdc830e007c6ea2b8d2feDavid Lawrence }
4bcaefbcd3ced942139fdc830e007c6ea2b8d2feDavid Lawrence if (i < nkeys) {
4bcaefbcd3ced942139fdc830e007c6ea2b8d2feDavid Lawrence sign_with_key(name, rdataset, rdata, sigrdatalist,
4bcaefbcd3ced942139fdc830e007c6ea2b8d2feDavid Lawrence now, later, keys[i], array, len);
4bcaefbcd3ced942139fdc830e007c6ea2b8d2feDavid Lawrence done = ISC_TRUE;
4bcaefbcd3ced942139fdc830e007c6ea2b8d2feDavid Lawrence }
4bcaefbcd3ced942139fdc830e007c6ea2b8d2feDavid Lawrence }
4bcaefbcd3ced942139fdc830e007c6ea2b8d2feDavid Lawrence if (!done) {
4bcaefbcd3ced942139fdc830e007c6ea2b8d2feDavid Lawrence if (dns_name_compare(sig.signer, origin) != 0 || foundnonzone) {
4bcaefbcd3ced942139fdc830e007c6ea2b8d2feDavid Lawrence printf("saving old sig...\n");
4bcaefbcd3ced942139fdc830e007c6ea2b8d2feDavid Lawrence isc_buffer_init(&b, array, len, ISC_BUFFERTYPE_BINARY);
4bcaefbcd3ced942139fdc830e007c6ea2b8d2feDavid Lawrence result = dns_rdata_fromstruct(rdata, rdataset->rdclass,
657ce0b9d84fbd66514df53d61a087e8f1161187Michael Graff dns_rdatatype_sig,
657ce0b9d84fbd66514df53d61a087e8f1161187Michael Graff &sig, &b);
657ce0b9d84fbd66514df53d61a087e8f1161187Michael Graff ISC_LIST_APPEND(sigrdatalist->rdata, rdata, link);
657ce0b9d84fbd66514df53d61a087e8f1161187Michael Graff check_result(result, "dns_rdata_fromstruct()");
657ce0b9d84fbd66514df53d61a087e8f1161187Michael Graff }
657ce0b9d84fbd66514df53d61a087e8f1161187Michael Graff else
657ce0b9d84fbd66514df53d61a087e8f1161187Michael Graff printf("couldn't find key <origin>/%d, dropping sig\n",
657ce0b9d84fbd66514df53d61a087e8f1161187Michael Graff sig.keyid);
657ce0b9d84fbd66514df53d61a087e8f1161187Michael Graff }
657ce0b9d84fbd66514df53d61a087e8f1161187Michael Graff
5ff133b82082d82f0ba89b7c999c6b62b6298e46Andreas Gustafsson dns_rdata_freestruct(&sig);
5ff133b82082d82f0ba89b7c999c6b62b6298e46Andreas Gustafsson}
5ff133b82082d82f0ba89b7c999c6b62b6298e46Andreas Gustafsson
5ff133b82082d82f0ba89b7c999c6b62b6298e46Andreas Gustafssonstatic void
31d3464c0c0a35236c7924f698c5a8a66a9ed534Mark Andrewsgenerate_sig(dns_db_t *db, dns_dbversion_t *version, dns_dbnode_t *node,
5ff133b82082d82f0ba89b7c999c6b62b6298e46Andreas Gustafsson dns_name_t *name, dst_key_t **keys, isc_boolean_t *defaultkey,
5ff133b82082d82f0ba89b7c999c6b62b6298e46Andreas Gustafsson int nkeys)
5ff133b82082d82f0ba89b7c999c6b62b6298e46Andreas Gustafsson{
c654449ccf403ccd2b81be2038b1013d6fbb06ccMark Andrews isc_result_t result;
6fcb2f0faad67a6d2cb2e30ec57157d75fbfe58fAndreas Gustafsson dns_name_t *origin;
6fcb2f0faad67a6d2cb2e30ec57157d75fbfe58fAndreas Gustafsson dns_rdata_t rdata, rdatas[MAXKEYS];
6fcb2f0faad67a6d2cb2e30ec57157d75fbfe58fAndreas Gustafsson dns_rdataset_t rdataset, sigrdataset, oldsigset;
6fcb2f0faad67a6d2cb2e30ec57157d75fbfe58fAndreas Gustafsson dns_rdatalist_t sigrdatalist;
6fcb2f0faad67a6d2cb2e30ec57157d75fbfe58fAndreas Gustafsson dns_rdatasetiter_t *rdsiter;
6fcb2f0faad67a6d2cb2e30ec57157d75fbfe58fAndreas Gustafsson isc_stdtime_t now, later;
47fd46791da765e3dbedd987e9b263b3bee25986Brian Wellington unsigned char array[MAXKEYS][1024];
47fd46791da765e3dbedd987e9b263b3bee25986Brian Wellington int i;
47fd46791da765e3dbedd987e9b263b3bee25986Brian Wellington isc_boolean_t alreadysigned;
47fd46791da765e3dbedd987e9b263b3bee25986Brian Wellington
47fd46791da765e3dbedd987e9b263b3bee25986Brian Wellington origin = dns_db_origin(db);
47fd46791da765e3dbedd987e9b263b3bee25986Brian Wellington
47fd46791da765e3dbedd987e9b263b3bee25986Brian Wellington dns_rdataset_init(&rdataset);
47fd46791da765e3dbedd987e9b263b3bee25986Brian Wellington rdsiter = NULL;
47fd46791da765e3dbedd987e9b263b3bee25986Brian Wellington result = dns_db_allrdatasets(db, node, version, 0, &rdsiter);
47fd46791da765e3dbedd987e9b263b3bee25986Brian Wellington check_result(result, "dns_db_allrdatasets()");
47fd46791da765e3dbedd987e9b263b3bee25986Brian Wellington result = dns_rdatasetiter_first(rdsiter);
6fcb2f0faad67a6d2cb2e30ec57157d75fbfe58fAndreas Gustafsson while (result == ISC_R_SUCCESS) {
6fcb2f0faad67a6d2cb2e30ec57157d75fbfe58fAndreas Gustafsson dns_rdatasetiter_current(rdsiter, &rdataset);
6fcb2f0faad67a6d2cb2e30ec57157d75fbfe58fAndreas Gustafsson
6fcb2f0faad67a6d2cb2e30ec57157d75fbfe58fAndreas Gustafsson if (rdataset.type == dns_rdatatype_sig ||
5ff133b82082d82f0ba89b7c999c6b62b6298e46Andreas Gustafsson (rdataset.type == dns_rdatatype_key &&
8f3dd8f8e73e4465221a5297819db70e6b383138Mark Andrews dns_name_compare(name, origin) == 0))
5ff133b82082d82f0ba89b7c999c6b62b6298e46Andreas Gustafsson {
754cca729dd82ae8363917dc00ad44f9d900635bMark Andrews dns_rdataset_disassociate(&rdataset);
754cca729dd82ae8363917dc00ad44f9d900635bMark Andrews result = dns_rdatasetiter_next(rdsiter);
754cca729dd82ae8363917dc00ad44f9d900635bMark Andrews continue;
754cca729dd82ae8363917dc00ad44f9d900635bMark Andrews }
754cca729dd82ae8363917dc00ad44f9d900635bMark Andrews
754cca729dd82ae8363917dc00ad44f9d900635bMark Andrews dns_rdataset_init(&oldsigset);
754cca729dd82ae8363917dc00ad44f9d900635bMark Andrews result = dns_db_findrdataset(db, node, version,
5ff133b82082d82f0ba89b7c999c6b62b6298e46Andreas Gustafsson dns_rdatatype_sig, rdataset.type,
40dd9cb8cc240c33d820fe79f176ed51e4c06a1aMark Andrews 0, &oldsigset, NULL);
40dd9cb8cc240c33d820fe79f176ed51e4c06a1aMark Andrews if (result == ISC_R_SUCCESS)
40dd9cb8cc240c33d820fe79f176ed51e4c06a1aMark Andrews alreadysigned = ISC_TRUE;
40dd9cb8cc240c33d820fe79f176ed51e4c06a1aMark Andrews else if (result == ISC_R_NOTFOUND) {
40dd9cb8cc240c33d820fe79f176ed51e4c06a1aMark Andrews alreadysigned = ISC_FALSE;
40dd9cb8cc240c33d820fe79f176ed51e4c06a1aMark Andrews result = ISC_R_SUCCESS;
40dd9cb8cc240c33d820fe79f176ed51e4c06a1aMark Andrews }
40dd9cb8cc240c33d820fe79f176ed51e4c06a1aMark Andrews else
40dd9cb8cc240c33d820fe79f176ed51e4c06a1aMark Andrews alreadysigned = ISC_FALSE; /* not that this matters */
40dd9cb8cc240c33d820fe79f176ed51e4c06a1aMark Andrews check_result(result, "dns_db_findrdataset()");
40dd9cb8cc240c33d820fe79f176ed51e4c06a1aMark Andrews
40dd9cb8cc240c33d820fe79f176ed51e4c06a1aMark Andrews /*
40dd9cb8cc240c33d820fe79f176ed51e4c06a1aMark Andrews * There probably should be a dns_nxtsetbit or something,
40dd9cb8cc240c33d820fe79f176ed51e4c06a1aMark Andrews * but it can get complicated if we need to extend the
40dd9cb8cc240c33d820fe79f176ed51e4c06a1aMark Andrews * length. In this case, since the NXT bit is set and
40dd9cb8cc240c33d820fe79f176ed51e4c06a1aMark Andrews * SIG < NXT, the easy way works.
40dd9cb8cc240c33d820fe79f176ed51e4c06a1aMark Andrews */
40dd9cb8cc240c33d820fe79f176ed51e4c06a1aMark Andrews if (rdataset.type == dns_rdatatype_nxt && !alreadysigned) {
40dd9cb8cc240c33d820fe79f176ed51e4c06a1aMark Andrews unsigned char *nxt_bits;
40dd9cb8cc240c33d820fe79f176ed51e4c06a1aMark Andrews dns_name_t nxtname;
40dd9cb8cc240c33d820fe79f176ed51e4c06a1aMark Andrews isc_region_t r, r2;
5ff133b82082d82f0ba89b7c999c6b62b6298e46Andreas Gustafsson
5ff133b82082d82f0ba89b7c999c6b62b6298e46Andreas Gustafsson result = dns_rdataset_first(&rdataset);
5ff133b82082d82f0ba89b7c999c6b62b6298e46Andreas Gustafsson check_result(result, "dns_rdataset_first()");
5ff133b82082d82f0ba89b7c999c6b62b6298e46Andreas Gustafsson dns_rdataset_current(&rdataset, &rdata);
5ff133b82082d82f0ba89b7c999c6b62b6298e46Andreas Gustafsson dns_rdata_toregion(&rdata, &r);
5ff133b82082d82f0ba89b7c999c6b62b6298e46Andreas Gustafsson dns_name_init(&nxtname, NULL);
5ff133b82082d82f0ba89b7c999c6b62b6298e46Andreas Gustafsson dns_name_fromregion(&nxtname, &r);
5ff133b82082d82f0ba89b7c999c6b62b6298e46Andreas Gustafsson dns_name_toregion(&nxtname, &r2);
5ff133b82082d82f0ba89b7c999c6b62b6298e46Andreas Gustafsson nxt_bits = r.base + r2.length;
5ff133b82082d82f0ba89b7c999c6b62b6298e46Andreas Gustafsson set_bit(nxt_bits, dns_rdatatype_sig, 1);
5ff133b82082d82f0ba89b7c999c6b62b6298e46Andreas Gustafsson
5ff133b82082d82f0ba89b7c999c6b62b6298e46Andreas Gustafsson }
5ff133b82082d82f0ba89b7c999c6b62b6298e46Andreas Gustafsson
a1898260ad19d02e88ab76c1855d33c67add9defMark Andrews isc_stdtime_get(&now);
5ff133b82082d82f0ba89b7c999c6b62b6298e46Andreas Gustafsson later = 100000 + now;
5ff133b82082d82f0ba89b7c999c6b62b6298e46Andreas Gustafsson ISC_LIST_INIT(sigrdatalist.rdata);
5ff133b82082d82f0ba89b7c999c6b62b6298e46Andreas Gustafsson
5ff133b82082d82f0ba89b7c999c6b62b6298e46Andreas Gustafsson if (!alreadysigned) {
5ff133b82082d82f0ba89b7c999c6b62b6298e46Andreas Gustafsson for (i = 0; i < nkeys; i++) {
5ff133b82082d82f0ba89b7c999c6b62b6298e46Andreas Gustafsson if (!defaultkey[i] || !is_zone_key(keys[i]))
5ff133b82082d82f0ba89b7c999c6b62b6298e46Andreas Gustafsson continue;
5ff133b82082d82f0ba89b7c999c6b62b6298e46Andreas Gustafsson sign_with_key(name, &rdataset, &rdatas[i],
5ff133b82082d82f0ba89b7c999c6b62b6298e46Andreas Gustafsson &sigrdatalist, &now, &later,
5ff133b82082d82f0ba89b7c999c6b62b6298e46Andreas Gustafsson keys[i], array[i],
5ff133b82082d82f0ba89b7c999c6b62b6298e46Andreas Gustafsson sizeof(array[i]));
a1898260ad19d02e88ab76c1855d33c67add9defMark Andrews }
5ff133b82082d82f0ba89b7c999c6b62b6298e46Andreas Gustafsson }
5ff133b82082d82f0ba89b7c999c6b62b6298e46Andreas Gustafsson else {
5ff133b82082d82f0ba89b7c999c6b62b6298e46Andreas Gustafsson dns_rdata_t sigrdata;
5ff133b82082d82f0ba89b7c999c6b62b6298e46Andreas Gustafsson
5ff133b82082d82f0ba89b7c999c6b62b6298e46Andreas Gustafsson dns_rdata_init(&sigrdata);
5ff133b82082d82f0ba89b7c999c6b62b6298e46Andreas Gustafsson result = dns_rdataset_first(&oldsigset);
5ff133b82082d82f0ba89b7c999c6b62b6298e46Andreas Gustafsson i = 0;
5ff133b82082d82f0ba89b7c999c6b62b6298e46Andreas Gustafsson while (result == ISC_R_SUCCESS) {
5ff133b82082d82f0ba89b7c999c6b62b6298e46Andreas Gustafsson dns_rdataset_current(&oldsigset, &sigrdata);
5ff133b82082d82f0ba89b7c999c6b62b6298e46Andreas Gustafsson resign_set(name, origin, &rdataset, &rdatas[i],
5ff133b82082d82f0ba89b7c999c6b62b6298e46Andreas Gustafsson &sigrdatalist, &sigrdata,
5ff133b82082d82f0ba89b7c999c6b62b6298e46Andreas Gustafsson &now, &later, keys, nkeys, array[i],
5ff133b82082d82f0ba89b7c999c6b62b6298e46Andreas Gustafsson sizeof(array[i]));
90407942d3afe50f04ccea361de3b164a5a1702dMichael Graff i++;
90407942d3afe50f04ccea361de3b164a5a1702dMichael Graff result = dns_rdataset_next(&oldsigset);
90407942d3afe50f04ccea361de3b164a5a1702dMichael Graff }
90407942d3afe50f04ccea361de3b164a5a1702dMichael Graff dns_rdataset_disassociate(&oldsigset);
90407942d3afe50f04ccea361de3b164a5a1702dMichael Graff }
13faa8b6a2d0d45e0659049983928366252ab3faMichael Graff
13faa8b6a2d0d45e0659049983928366252ab3faMichael Graff sigrdatalist.rdclass = rdataset.rdclass;
13faa8b6a2d0d45e0659049983928366252ab3faMichael Graff sigrdatalist.type = dns_rdatatype_sig;
5989aea4bbe79e09290792f04aeb557e2b2da02eAndreas Gustafsson sigrdatalist.covers = rdataset.type;
13faa8b6a2d0d45e0659049983928366252ab3faMichael Graff sigrdatalist.ttl = rdataset.ttl;
61d5bfc06be978ea962b1c64309894ac80351771Mark Andrews dns_rdataset_init(&sigrdataset);
61d5bfc06be978ea962b1c64309894ac80351771Mark Andrews result = dns_rdatalist_tordataset(&sigrdatalist, &sigrdataset);
5f9e583552f53de12062bfff12e47250abce378fBrian Wellington check_result(result, "dns_rdatalist_tordataset");
13faa8b6a2d0d45e0659049983928366252ab3faMichael Graff result = dns_db_addrdataset(db, node, version, 0, &sigrdataset,
5f9e583552f53de12062bfff12e47250abce378fBrian Wellington ISC_FALSE, NULL);
5f9e583552f53de12062bfff12e47250abce378fBrian Wellington if (result == DNS_R_UNCHANGED)
3fcf6b956f47405750724bd84e1b2290b61c9186Brian Wellington result = ISC_R_SUCCESS;
3fcf6b956f47405750724bd84e1b2290b61c9186Brian Wellington check_result(result, "dns_db_addrdataset");
3fcf6b956f47405750724bd84e1b2290b61c9186Brian Wellington dns_rdataset_disassociate(&sigrdataset);
5f9e583552f53de12062bfff12e47250abce378fBrian Wellington
5f9e583552f53de12062bfff12e47250abce378fBrian Wellington dns_rdataset_disassociate(&rdataset);
5989aea4bbe79e09290792f04aeb557e2b2da02eAndreas Gustafsson result = dns_rdatasetiter_next(rdsiter);
5989aea4bbe79e09290792f04aeb557e2b2da02eAndreas Gustafsson }
5989aea4bbe79e09290792f04aeb557e2b2da02eAndreas Gustafsson if (result != DNS_R_NOMORE)
a0ad323da3597f942881eb2112a82c21450a9d0dBrian Wellington fatal("rdataset iteration failed");
68f72235f8f41fa949823551d8e6476057ec5bd6Andreas Gustafsson dns_rdatasetiter_destroy(&rdsiter);
68f72235f8f41fa949823551d8e6476057ec5bd6Andreas Gustafsson}
68f72235f8f41fa949823551d8e6476057ec5bd6Andreas Gustafsson
68f72235f8f41fa949823551d8e6476057ec5bd6Andreas Gustafssonstatic inline isc_boolean_t
68f72235f8f41fa949823551d8e6476057ec5bd6Andreas Gustafssonactive_node(dns_db_t *db, dns_dbversion_t *version, dns_dbnode_t *node) {
68f72235f8f41fa949823551d8e6476057ec5bd6Andreas Gustafsson dns_rdatasetiter_t *rdsiter;
68f72235f8f41fa949823551d8e6476057ec5bd6Andreas Gustafsson isc_boolean_t active = ISC_FALSE;
68f72235f8f41fa949823551d8e6476057ec5bd6Andreas Gustafsson isc_result_t result;
68f72235f8f41fa949823551d8e6476057ec5bd6Andreas Gustafsson dns_rdataset_t rdataset;
5989aea4bbe79e09290792f04aeb557e2b2da02eAndreas Gustafsson
68f72235f8f41fa949823551d8e6476057ec5bd6Andreas Gustafsson dns_rdataset_init(&rdataset);
5989aea4bbe79e09290792f04aeb557e2b2da02eAndreas Gustafsson rdsiter = NULL;
80f323528ac699026a609a5e3b765dc6e88fe37cAndreas Gustafsson result = dns_db_allrdatasets(db, node, version, 0, &rdsiter);
5989aea4bbe79e09290792f04aeb557e2b2da02eAndreas Gustafsson check_result(result, "dns_db_allrdatasets()");
5989aea4bbe79e09290792f04aeb557e2b2da02eAndreas Gustafsson result = dns_rdatasetiter_first(rdsiter);
3fcf6b956f47405750724bd84e1b2290b61c9186Brian Wellington while (result == ISC_R_SUCCESS) {
e4c7b24ab12572b6781d5c545c7b7949cbd2a6f7Brian Wellington dns_rdatasetiter_current(rdsiter, &rdataset);
5f9e583552f53de12062bfff12e47250abce378fBrian Wellington if (rdataset.type != dns_rdatatype_nxt)
5f9e583552f53de12062bfff12e47250abce378fBrian Wellington active = ISC_TRUE;
5989aea4bbe79e09290792f04aeb557e2b2da02eAndreas Gustafsson dns_rdataset_disassociate(&rdataset);
5989aea4bbe79e09290792f04aeb557e2b2da02eAndreas Gustafsson if (!active)
5989aea4bbe79e09290792f04aeb557e2b2da02eAndreas Gustafsson result = dns_rdatasetiter_next(rdsiter);
5989aea4bbe79e09290792f04aeb557e2b2da02eAndreas Gustafsson else
5989aea4bbe79e09290792f04aeb557e2b2da02eAndreas Gustafsson result = DNS_R_NOMORE;
5989aea4bbe79e09290792f04aeb557e2b2da02eAndreas Gustafsson }
5989aea4bbe79e09290792f04aeb557e2b2da02eAndreas Gustafsson if (result != DNS_R_NOMORE)
919caa020b8f9b856d77b3a72e0c9301dfa495c7Andreas Gustafsson fatal("rdataset iteration failed");
5989aea4bbe79e09290792f04aeb557e2b2da02eAndreas Gustafsson dns_rdatasetiter_destroy(&rdsiter);
5989aea4bbe79e09290792f04aeb557e2b2da02eAndreas Gustafsson
cb2a4cad76fbda226e7cd9dd3ca017b52521e3d1Andreas Gustafsson if (!active) {
5989aea4bbe79e09290792f04aeb557e2b2da02eAndreas Gustafsson /*
bd1db480f30e025bba719799f910b34848a9a997Mark Andrews * Make sure there is no NXT record for this node.
bd1db480f30e025bba719799f910b34848a9a997Mark Andrews */
bd1db480f30e025bba719799f910b34848a9a997Mark Andrews result = dns_db_deleterdataset(db, node, version,
5989aea4bbe79e09290792f04aeb557e2b2da02eAndreas Gustafsson dns_rdatatype_nxt);
5989aea4bbe79e09290792f04aeb557e2b2da02eAndreas Gustafsson if (result == DNS_R_UNCHANGED)
5989aea4bbe79e09290792f04aeb557e2b2da02eAndreas Gustafsson result = ISC_R_SUCCESS;
5989aea4bbe79e09290792f04aeb557e2b2da02eAndreas Gustafsson check_result(result, "dns_db_deleterdataset");
5989aea4bbe79e09290792f04aeb557e2b2da02eAndreas Gustafsson }
caa736a754e90f44bbc249e22f96bcbf4e04b849Andreas Gustafsson
caa736a754e90f44bbc249e22f96bcbf4e04b849Andreas Gustafsson return (active);
5989aea4bbe79e09290792f04aeb557e2b2da02eAndreas Gustafsson}
5989aea4bbe79e09290792f04aeb557e2b2da02eAndreas Gustafsson
5f9e583552f53de12062bfff12e47250abce378fBrian Wellingtonstatic inline isc_result_t
5f9e583552f53de12062bfff12e47250abce378fBrian Wellingtonnext_active(dns_db_t *db, dns_dbversion_t *version, dns_dbiterator_t *dbiter,
08a768e82ad64ede97f640c88e02984b59122753Michael Graff dns_name_t *name, dns_dbnode_t **nodep)
08a768e82ad64ede97f640c88e02984b59122753Michael Graff{
08a768e82ad64ede97f640c88e02984b59122753Michael Graff isc_result_t result;
08a768e82ad64ede97f640c88e02984b59122753Michael Graff isc_boolean_t active;
08a768e82ad64ede97f640c88e02984b59122753Michael Graff
08a768e82ad64ede97f640c88e02984b59122753Michael Graff do {
3fcf6b956f47405750724bd84e1b2290b61c9186Brian Wellington active = ISC_FALSE;
0e40083fdd5445703bd30e46e5bfe7d047bced12Brian Wellington result = dns_dbiterator_current(dbiter, nodep, name);
0e40083fdd5445703bd30e46e5bfe7d047bced12Brian Wellington if (result == ISC_R_SUCCESS) {
08a768e82ad64ede97f640c88e02984b59122753Michael Graff active = active_node(db, version, *nodep);
3ec6b563d7b6cb11a047f23faa2a0f206ccd93e7Brian Wellington if (!active) {
3ec6b563d7b6cb11a047f23faa2a0f206ccd93e7Brian Wellington dns_db_detachnode(db, nodep);
3ec6b563d7b6cb11a047f23faa2a0f206ccd93e7Brian Wellington result = dns_dbiterator_next(dbiter);
fee5012c43744322c1785e5c3e0c322443faa304Brian Wellington }
fee5012c43744322c1785e5c3e0c322443faa304Brian Wellington }
fee5012c43744322c1785e5c3e0c322443faa304Brian Wellington } while (result == ISC_R_SUCCESS && !active);
fee5012c43744322c1785e5c3e0c322443faa304Brian Wellington
fee5012c43744322c1785e5c3e0c322443faa304Brian Wellington return (result);
fee5012c43744322c1785e5c3e0c322443faa304Brian Wellington}
fee5012c43744322c1785e5c3e0c322443faa304Brian Wellington
fee5012c43744322c1785e5c3e0c322443faa304Brian Wellingtonstatic void
fee5012c43744322c1785e5c3e0c322443faa304Brian Wellingtonsign(char *filename) {
fee5012c43744322c1785e5c3e0c322443faa304Brian Wellington isc_result_t result, nxtresult;
fee5012c43744322c1785e5c3e0c322443faa304Brian Wellington dns_db_t *db;
fee5012c43744322c1785e5c3e0c322443faa304Brian Wellington dns_dbversion_t *wversion;
fee5012c43744322c1785e5c3e0c322443faa304Brian Wellington dns_dbnode_t *node, *nextnode, *curnode;
fee5012c43744322c1785e5c3e0c322443faa304Brian Wellington char *origintext;
fee5012c43744322c1785e5c3e0c322443faa304Brian Wellington dns_fixedname_t fname, fnextname;
fee5012c43744322c1785e5c3e0c322443faa304Brian Wellington dns_name_t *name, *nextname, *target, curname;
fee5012c43744322c1785e5c3e0c322443faa304Brian Wellington isc_buffer_t b;
fee5012c43744322c1785e5c3e0c322443faa304Brian Wellington size_t len;
fee5012c43744322c1785e5c3e0c322443faa304Brian Wellington dns_dbiterator_t *dbiter;
fee5012c43744322c1785e5c3e0c322443faa304Brian Wellington char newfilename[1024];
fee5012c43744322c1785e5c3e0c322443faa304Brian Wellington dst_key_t *keys[MAXKEYS];
fee5012c43744322c1785e5c3e0c322443faa304Brian Wellington isc_boolean_t defaultkey[MAXKEYS];
fee5012c43744322c1785e5c3e0c322443faa304Brian Wellington unsigned char curdata[1024];
fee5012c43744322c1785e5c3e0c322443faa304Brian Wellington isc_buffer_t curbuf;
fee5012c43744322c1785e5c3e0c322443faa304Brian Wellington unsigned int nkeys = 0;
fee5012c43744322c1785e5c3e0c322443faa304Brian Wellington unsigned int i;
3ec6b563d7b6cb11a047f23faa2a0f206ccd93e7Brian Wellington
3ec6b563d7b6cb11a047f23faa2a0f206ccd93e7Brian Wellington dns_fixedname_init(&fname);
3ec6b563d7b6cb11a047f23faa2a0f206ccd93e7Brian Wellington name = dns_fixedname_name(&fname);
0e40083fdd5445703bd30e46e5bfe7d047bced12Brian Wellington dns_fixedname_init(&fnextname);
0e40083fdd5445703bd30e46e5bfe7d047bced12Brian Wellington nextname = dns_fixedname_name(&fnextname);
0e40083fdd5445703bd30e46e5bfe7d047bced12Brian Wellington
0e40083fdd5445703bd30e46e5bfe7d047bced12Brian Wellington origintext = strrchr(filename, '/');
0e40083fdd5445703bd30e46e5bfe7d047bced12Brian Wellington if (origintext == NULL)
0e40083fdd5445703bd30e46e5bfe7d047bced12Brian Wellington origintext = filename;
0e40083fdd5445703bd30e46e5bfe7d047bced12Brian Wellington else
3ec6b563d7b6cb11a047f23faa2a0f206ccd93e7Brian Wellington origintext++; /* Skip '/'. */
a14613fce99dee3cad5bf842fd6be78f8e463582Brian Wellington len = strlen(origintext);
a14613fce99dee3cad5bf842fd6be78f8e463582Brian Wellington isc_buffer_init(&b, origintext, len, ISC_BUFFERTYPE_TEXT);
a14613fce99dee3cad5bf842fd6be78f8e463582Brian Wellington isc_buffer_add(&b, len);
a14613fce99dee3cad5bf842fd6be78f8e463582Brian Wellington result = dns_name_fromtext(name, &b, dns_rootname, ISC_FALSE, NULL);
a14613fce99dee3cad5bf842fd6be78f8e463582Brian Wellington check_result(result, "dns_name_fromtext()");
a14613fce99dee3cad5bf842fd6be78f8e463582Brian Wellington db = NULL;
a14613fce99dee3cad5bf842fd6be78f8e463582Brian Wellington result = dns_db_create(mctx, "rbt", name, ISC_FALSE,
a14613fce99dee3cad5bf842fd6be78f8e463582Brian Wellington dns_rdataclass_in, 0, NULL, &db);
a14613fce99dee3cad5bf842fd6be78f8e463582Brian Wellington check_result(result, "dns_db_create()");
a14613fce99dee3cad5bf842fd6be78f8e463582Brian Wellington result = dns_db_load(db, filename);
a14613fce99dee3cad5bf842fd6be78f8e463582Brian Wellington check_result(result, "dns_db_load()");
a14613fce99dee3cad5bf842fd6be78f8e463582Brian Wellington
af5ad488cbf17988fbd36a25c908737412ccd382Brian Wellington node = NULL;
a14613fce99dee3cad5bf842fd6be78f8e463582Brian Wellington result = dns_db_findnode(db, name, ISC_FALSE, &node);
a14613fce99dee3cad5bf842fd6be78f8e463582Brian Wellington check_result(result, "dns_db_findnode()");
af5ad488cbf17988fbd36a25c908737412ccd382Brian Wellington result = find_apex_keys(db, NULL, node, name, mctx, MAXKEYS,
a14613fce99dee3cad5bf842fd6be78f8e463582Brian Wellington keys, &nkeys);
a14613fce99dee3cad5bf842fd6be78f8e463582Brian Wellington check_result(result, "dns_dnssec_findzonekeys()");
b495fd2992c63472b3ad2d9517ffe9b50118840aAndreas Gustafsson dns_db_detachnode(db, &node);
af5ad488cbf17988fbd36a25c908737412ccd382Brian Wellington for (i = 0; i < nkeys; i++)
f317c00e0d5978f29285ea062b34ec73dc419095Brian Wellington defaultkey[i] = ISC_TRUE;
f317c00e0d5978f29285ea062b34ec73dc419095Brian Wellington
a14613fce99dee3cad5bf842fd6be78f8e463582Brian Wellington wversion = NULL;
a14613fce99dee3cad5bf842fd6be78f8e463582Brian Wellington result = dns_db_newversion(db, &wversion);
a14613fce99dee3cad5bf842fd6be78f8e463582Brian Wellington check_result(result, "dns_db_newversion()");
a14613fce99dee3cad5bf842fd6be78f8e463582Brian Wellington
dee520f1be8c59e10a55b6995844395e811c310fBrian Wellington dbiter = NULL;
dee520f1be8c59e10a55b6995844395e811c310fBrian Wellington result = dns_db_createiterator(db, ISC_FALSE, &dbiter);
dee520f1be8c59e10a55b6995844395e811c310fBrian Wellington check_result(result, "dns_db_createiterator()");
a14613fce99dee3cad5bf842fd6be78f8e463582Brian Wellington result = dns_dbiterator_first(dbiter);
529ff4b4959fb157194f985394951108ff5286e4Brian Wellington node = NULL;
a14613fce99dee3cad5bf842fd6be78f8e463582Brian Wellington result = next_active(db, wversion, dbiter, name, &node);
a14613fce99dee3cad5bf842fd6be78f8e463582Brian Wellington while (result == ISC_R_SUCCESS) {
a14613fce99dee3cad5bf842fd6be78f8e463582Brian Wellington nextnode = NULL;
a14613fce99dee3cad5bf842fd6be78f8e463582Brian Wellington curnode = NULL;
9a2574531e3d2ced31072200b416467fdee0c29cDavid Lawrence dns_name_init(&curname, NULL);
9a2574531e3d2ced31072200b416467fdee0c29cDavid Lawrence isc_buffer_init(&curbuf, curdata, sizeof(curdata),
9a2574531e3d2ced31072200b416467fdee0c29cDavid Lawrence ISC_BUFFERTYPE_BINARY);
9a2574531e3d2ced31072200b416467fdee0c29cDavid Lawrence dns_name_setbuffer(&curname, &curbuf);
9a2574531e3d2ced31072200b416467fdee0c29cDavid Lawrence dns_dbiterator_current(dbiter, &curnode, &curname);
e2b585787f4779f49bd0982562acbbb7d0b65a95Andreas Gustafsson result = dns_dbiterator_next(dbiter);
e2b585787f4779f49bd0982562acbbb7d0b65a95Andreas Gustafsson if (result == ISC_R_SUCCESS)
e2b585787f4779f49bd0982562acbbb7d0b65a95Andreas Gustafsson result = next_active(db, wversion, dbiter, nextname,
e2b585787f4779f49bd0982562acbbb7d0b65a95Andreas Gustafsson &nextnode);
e2b585787f4779f49bd0982562acbbb7d0b65a95Andreas Gustafsson if (result == ISC_R_SUCCESS)
e2b585787f4779f49bd0982562acbbb7d0b65a95Andreas Gustafsson target = nextname;
e2b585787f4779f49bd0982562acbbb7d0b65a95Andreas Gustafsson else if (result == DNS_R_NOMORE)
e2b585787f4779f49bd0982562acbbb7d0b65a95Andreas Gustafsson target = dns_db_origin(db);
e2b585787f4779f49bd0982562acbbb7d0b65a95Andreas Gustafsson else {
e2b585787f4779f49bd0982562acbbb7d0b65a95Andreas Gustafsson target = NULL; /* Make compiler happy. */
7f9bc71eca311843611a4b0cfdeb12eda324b689Mark Andrews fatal("db iteration failed");
e2b585787f4779f49bd0982562acbbb7d0b65a95Andreas Gustafsson }
e2b585787f4779f49bd0982562acbbb7d0b65a95Andreas Gustafsson nxtresult = dns_buildnxt(db, wversion, node, target);
e2b585787f4779f49bd0982562acbbb7d0b65a95Andreas Gustafsson check_result(nxtresult, "dns_buildnxt()");
7f9bc71eca311843611a4b0cfdeb12eda324b689Mark Andrews generate_sig(db, wversion, node, &curname, keys, defaultkey,
e2b585787f4779f49bd0982562acbbb7d0b65a95Andreas Gustafsson nkeys);
e2b585787f4779f49bd0982562acbbb7d0b65a95Andreas Gustafsson dns_name_invalidate(&curname);
e2b585787f4779f49bd0982562acbbb7d0b65a95Andreas Gustafsson dns_db_detachnode(db, &node);
6ef891fbe943d6776ed17439ccb8bbb8e314b7d8Andreas Gustafsson dns_db_detachnode(db, &curnode);
6ef891fbe943d6776ed17439ccb8bbb8e314b7d8Andreas Gustafsson node = nextnode;
6ef891fbe943d6776ed17439ccb8bbb8e314b7d8Andreas Gustafsson }
9d266ed4d7630d8366fea0a4a627d8c3873821c5Brian Wellington if (result != DNS_R_NOMORE)
9d266ed4d7630d8366fea0a4a627d8c3873821c5Brian Wellington fatal("db iteration failed");
9d266ed4d7630d8366fea0a4a627d8c3873821c5Brian Wellington dns_dbiterator_destroy(&dbiter);
712bf9b0cc4ed34f4bf33b437f8b0e45853b93ceMark Andrews /*
967fafd9674da590f605d1cbe5f66dd7ddbeb849David Lawrence * XXXRTH For now, we don't increment the SOA serial.
e2b585787f4779f49bd0982562acbbb7d0b65a95Andreas Gustafsson */
712bf9b0cc4ed34f4bf33b437f8b0e45853b93ceMark Andrews dns_db_closeversion(db, &wversion, ISC_TRUE);
712bf9b0cc4ed34f4bf33b437f8b0e45853b93ceMark Andrews len = strlen(filename);
712bf9b0cc4ed34f4bf33b437f8b0e45853b93ceMark Andrews if (len + 4 + 1 > sizeof newfilename)
e2b585787f4779f49bd0982562acbbb7d0b65a95Andreas Gustafsson fatal("filename too long");
e2b585787f4779f49bd0982562acbbb7d0b65a95Andreas Gustafsson sprintf(newfilename, "%s.new", filename);
e2b585787f4779f49bd0982562acbbb7d0b65a95Andreas Gustafsson result = dns_db_dump(db, NULL, newfilename);
e2b585787f4779f49bd0982562acbbb7d0b65a95Andreas Gustafsson check_result(result, "dns_db_dump");
e2b585787f4779f49bd0982562acbbb7d0b65a95Andreas Gustafsson dns_db_detach(&db);
1c0ff8a9cc1e1edd55acff6802f8811966732653Brian Wellington for (i = 0; i < nkeys; i++)
e2b585787f4779f49bd0982562acbbb7d0b65a95Andreas Gustafsson dst_key_free(keys[i]);
e2b585787f4779f49bd0982562acbbb7d0b65a95Andreas Gustafsson}
e2b585787f4779f49bd0982562acbbb7d0b65a95Andreas Gustafsson
e2b585787f4779f49bd0982562acbbb7d0b65a95Andreas Gustafssonint
e2b585787f4779f49bd0982562acbbb7d0b65a95Andreas Gustafssonmain(int argc, char *argv[]) {
e2b585787f4779f49bd0982562acbbb7d0b65a95Andreas Gustafsson int i;
e2b585787f4779f49bd0982562acbbb7d0b65a95Andreas Gustafsson isc_result_t result;
e2b585787f4779f49bd0982562acbbb7d0b65a95Andreas Gustafsson
e2b585787f4779f49bd0982562acbbb7d0b65a95Andreas Gustafsson dns_result_register();
e2b585787f4779f49bd0982562acbbb7d0b65a95Andreas Gustafsson
e2b585787f4779f49bd0982562acbbb7d0b65a95Andreas Gustafsson result = isc_mem_create(0, 0, &mctx);
e2b585787f4779f49bd0982562acbbb7d0b65a95Andreas Gustafsson check_result(result, "isc_mem_create()");
e2b585787f4779f49bd0982562acbbb7d0b65a95Andreas Gustafsson
e2b585787f4779f49bd0982562acbbb7d0b65a95Andreas Gustafsson argc--;
e2b585787f4779f49bd0982562acbbb7d0b65a95Andreas Gustafsson argv++;
e2b585787f4779f49bd0982562acbbb7d0b65a95Andreas Gustafsson
9a2574531e3d2ced31072200b416467fdee0c29cDavid Lawrence for (i = 0; i < argc; i++)
e2b585787f4779f49bd0982562acbbb7d0b65a95Andreas Gustafsson sign(argv[i]);
9a2574531e3d2ced31072200b416467fdee0c29cDavid Lawrence
e2b585787f4779f49bd0982562acbbb7d0b65a95Andreas Gustafsson isc_mem_stats(mctx, stdout);
9a2574531e3d2ced31072200b416467fdee0c29cDavid Lawrence isc_mem_destroy(&mctx);
9a2574531e3d2ced31072200b416467fdee0c29cDavid Lawrence
9a2574531e3d2ced31072200b416467fdee0c29cDavid Lawrence return (0);
9a2574531e3d2ced31072200b416467fdee0c29cDavid Lawrence}
9a2574531e3d2ced31072200b416467fdee0c29cDavid Lawrence