1 /* $NetBSD: keydelete.c,v 1.6 2014/12/10 04:37:54 christos Exp $ */ 2 3 /* 4 * Copyright (C) 2004, 2005, 2007, 2009-2011, 2014 Internet Systems Consortium, Inc. ("ISC") 5 * Copyright (C) 2001 Internet Software Consortium. 6 * 7 * Permission to use, copy, modify, and/or distribute this software for any 8 * purpose with or without fee is hereby granted, provided that the above 9 * copyright notice and this permission notice appear in all copies. 10 * 11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH 12 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY 13 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, 14 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM 15 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE 16 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 17 * PERFORMANCE OF THIS SOFTWARE. 18 */ 19 20 /* Id: keydelete.c,v 1.18 2011/01/11 23:47:13 tbox Exp */ 21 22 #include <config.h> 23 24 #include <stdlib.h> 25 #include <string.h> 26 27 #include <isc/app.h> 28 #include <isc/base64.h> 29 #include <isc/entropy.h> 30 #include <isc/hash.h> 31 #include <isc/log.h> 32 #include <isc/mem.h> 33 #include <isc/sockaddr.h> 34 #include <isc/socket.h> 35 #include <isc/task.h> 36 #include <isc/timer.h> 37 #include <isc/util.h> 38 39 #include <dns/dispatch.h> 40 #include <dns/fixedname.h> 41 #include <dns/keyvalues.h> 42 #include <dns/message.h> 43 #include <dns/name.h> 44 #include <dns/request.h> 45 #include <dns/result.h> 46 #include <dns/tkey.h> 47 #include <dns/tsig.h> 48 #include <dns/view.h> 49 50 #include <dst/result.h> 51 52 #define CHECK(str, x) { \ 53 if ((x) != ISC_R_SUCCESS) { \ 54 fprintf(stderr, "I:%s: %s\n", (str), isc_result_totext(x)); \ 55 exit(-1); \ 56 } \ 57 } 58 59 #define RUNCHECK(x) RUNTIME_CHECK((x) == ISC_R_SUCCESS) 60 61 #define PORT 5300 62 #define TIMEOUT 30 63 64 static isc_mem_t *mctx; 65 static dns_tsigkey_t *tsigkey; 66 static dns_tsig_keyring_t *ring; 67 static dns_requestmgr_t *requestmgr; 68 69 static void 70 recvquery(isc_task_t *task, isc_event_t *event) { 71 dns_requestevent_t *reqev = (dns_requestevent_t *)event; 72 isc_result_t result; 73 dns_message_t *query, *response; 74 75 UNUSED(task); 76 77 REQUIRE(reqev != NULL); 78 79 if (reqev->result != ISC_R_SUCCESS) { 80 fprintf(stderr, "I:request event result: %s\n", 81 isc_result_totext(reqev->result)); 82 exit(-1); 83 } 84 85 query = reqev->ev_arg; 86 87 response = NULL; 88 result = dns_message_create(mctx, DNS_MESSAGE_INTENTPARSE, &response); 89 CHECK("dns_message_create", result); 90 91 result = dns_request_getresponse(reqev->request, response, 92 DNS_MESSAGEPARSE_PRESERVEORDER); 93 CHECK("dns_request_getresponse", result); 94 95 if (response->rcode != dns_rcode_noerror) { 96 result = ISC_RESULTCLASS_DNSRCODE + response->rcode; 97 fprintf(stderr, "I:response rcode: %s\n", 98 isc_result_totext(result)); 99 exit(-1); 100 } 101 102 result = dns_tkey_processdeleteresponse(query, response, ring); 103 CHECK("dns_tkey_processdhresponse", result); 104 105 dns_message_destroy(&query); 106 dns_message_destroy(&response); 107 dns_request_destroy(&reqev->request); 108 isc_event_free(&event); 109 isc_app_shutdown(); 110 return; 111 } 112 113 static void 114 sendquery(isc_task_t *task, isc_event_t *event) { 115 struct in_addr inaddr; 116 isc_sockaddr_t address; 117 isc_result_t result; 118 dns_message_t *query; 119 dns_request_t *request; 120 121 isc_event_free(&event); 122 123 result = ISC_R_FAILURE; 124 if (inet_pton(AF_INET, "10.53.0.1", &inaddr) != 1) 125 CHECK("inet_pton", result); 126 isc_sockaddr_fromin(&address, &inaddr, PORT); 127 128 query = NULL; 129 result = dns_message_create(mctx, DNS_MESSAGE_INTENTRENDER, &query); 130 CHECK("dns_message_create", result); 131 132 result = dns_tkey_builddeletequery(query, tsigkey); 133 CHECK("dns_tkey_builddeletequery", result); 134 135 request = NULL; 136 result = dns_request_create(requestmgr, query, &address, 137 DNS_REQUESTOPT_TCP, tsigkey, TIMEOUT, 138 task, recvquery, query, &request); 139 CHECK("dns_request_create", result); 140 } 141 142 int 143 main(int argc, char **argv) { 144 char *keyname; 145 isc_taskmgr_t *taskmgr; 146 isc_timermgr_t *timermgr; 147 isc_socketmgr_t *socketmgr; 148 isc_socket_t *sock; 149 unsigned int attrs, attrmask; 150 isc_sockaddr_t bind_any; 151 dns_dispatchmgr_t *dispatchmgr; 152 dns_dispatch_t *dispatchv4; 153 dns_view_t *view; 154 isc_entropy_t *ectx; 155 dns_tkeyctx_t *tctx; 156 dst_key_t *dstkey; 157 isc_log_t *log; 158 isc_logconfig_t *logconfig; 159 isc_task_t *task; 160 isc_result_t result; 161 int type; 162 163 RUNCHECK(isc_app_start()); 164 165 if (argc < 2) { 166 fprintf(stderr, "I:no key to delete\n"); 167 exit(-1); 168 } 169 keyname = argv[1]; 170 171 dns_result_register(); 172 173 mctx = NULL; 174 RUNCHECK(isc_mem_create(0, 0, &mctx)); 175 176 ectx = NULL; 177 RUNCHECK(isc_entropy_create(mctx, &ectx)); 178 RUNCHECK(isc_entropy_createfilesource(ectx, "../random.data")); 179 RUNCHECK(isc_hash_create(mctx, ectx, DNS_NAME_MAXWIRE)); 180 181 log = NULL; 182 logconfig = NULL; 183 RUNCHECK(isc_log_create(mctx, &log, &logconfig)); 184 185 RUNCHECK(dst_lib_init(mctx, ectx, ISC_ENTROPY_GOODONLY)); 186 187 taskmgr = NULL; 188 RUNCHECK(isc_taskmgr_create(mctx, 1, 0, &taskmgr)); 189 task = NULL; 190 RUNCHECK(isc_task_create(taskmgr, 0, &task)); 191 timermgr = NULL; 192 RUNCHECK(isc_timermgr_create(mctx, &timermgr)); 193 socketmgr = NULL; 194 RUNCHECK(isc_socketmgr_create(mctx, &socketmgr)); 195 dispatchmgr = NULL; 196 RUNCHECK(dns_dispatchmgr_create(mctx, NULL, &dispatchmgr)); 197 isc_sockaddr_any(&bind_any); 198 attrs = DNS_DISPATCHATTR_UDP | 199 DNS_DISPATCHATTR_MAKEQUERY | 200 DNS_DISPATCHATTR_IPV4; 201 attrmask = DNS_DISPATCHATTR_UDP | 202 DNS_DISPATCHATTR_TCP | 203 DNS_DISPATCHATTR_IPV4 | 204 DNS_DISPATCHATTR_IPV6; 205 dispatchv4 = NULL; 206 RUNCHECK(dns_dispatch_getudp(dispatchmgr, socketmgr, taskmgr, 207 &bind_any, 4096, 4, 2, 3, 5, 208 attrs, attrmask, &dispatchv4)); 209 requestmgr = NULL; 210 RUNCHECK(dns_requestmgr_create(mctx, timermgr, socketmgr, taskmgr, 211 dispatchmgr, dispatchv4, NULL, 212 &requestmgr)); 213 214 ring = NULL; 215 RUNCHECK(dns_tsigkeyring_create(mctx, &ring)); 216 tctx = NULL; 217 RUNCHECK(dns_tkeyctx_create(mctx, ectx, &tctx)); 218 219 view = NULL; 220 RUNCHECK(dns_view_create(mctx, 0, "_test", &view)); 221 dns_view_setkeyring(view, ring); 222 223 sock = NULL; 224 RUNCHECK(isc_socket_create(socketmgr, PF_INET, isc_sockettype_udp, 225 &sock)); 226 227 RUNCHECK(isc_app_onrun(mctx, task, sendquery, NULL)); 228 229 dstkey = NULL; 230 type = DST_TYPE_PUBLIC | DST_TYPE_PRIVATE | DST_TYPE_KEY; 231 result = dst_key_fromnamedfile(keyname, NULL, type, mctx, &dstkey); 232 CHECK("dst_key_fromnamedfile", result); 233 result = dns_tsigkey_createfromkey(dst_key_name(dstkey), 234 DNS_TSIG_HMACMD5_NAME, 235 dstkey, ISC_TRUE, NULL, 0, 0, 236 mctx, ring, &tsigkey); 237 dst_key_free(&dstkey); 238 CHECK("dns_tsigkey_createfromkey", result); 239 240 (void)isc_app_run(); 241 242 dns_requestmgr_shutdown(requestmgr); 243 dns_requestmgr_detach(&requestmgr); 244 dns_dispatch_detach(&dispatchv4); 245 dns_dispatchmgr_destroy(&dispatchmgr); 246 isc_task_shutdown(task); 247 isc_task_detach(&task); 248 isc_taskmgr_destroy(&taskmgr); 249 isc_socket_detach(&sock); 250 isc_socketmgr_destroy(&socketmgr); 251 isc_timermgr_destroy(&timermgr); 252 253 dns_tsigkeyring_detach(&ring); 254 255 dns_tsigkey_detach(&tsigkey); 256 257 dns_tkeyctx_destroy(&tctx); 258 259 dns_view_detach(&view); 260 261 isc_log_destroy(&log); 262 263 dst_lib_destroy(); 264 isc_hash_destroy(); 265 isc_entropy_detach(&ectx); 266 267 isc_mem_destroy(&mctx); 268 269 isc_app_finish(); 270 271 return (0); 272 } 273