keydelete.c revision 1.3
1/* $NetBSD: keydelete.c,v 1.3 2019/01/09 16:55:04 christos Exp $ */ 2 3/* 4 * Copyright (C) Internet Systems Consortium, Inc. ("ISC") 5 * 6 * This Source Code Form is subject to the terms of the Mozilla Public 7 * License, v. 2.0. If a copy of the MPL was not distributed with this 8 * file, You can obtain one at http://mozilla.org/MPL/2.0/. 9 * 10 * See the COPYRIGHT file distributed with this work for additional 11 * information regarding copyright ownership. 12 */ 13 14 15#include <config.h> 16 17#include <stdlib.h> 18#include <string.h> 19 20#include <isc/app.h> 21#include <isc/base64.h> 22#include <isc/hash.h> 23#include <isc/log.h> 24#include <isc/mem.h> 25#include <isc/print.h> 26#include <isc/random.h> 27#include <isc/sockaddr.h> 28#include <isc/socket.h> 29#include <isc/task.h> 30#include <isc/timer.h> 31#include <isc/util.h> 32 33#include <pk11/site.h> 34 35#include <dns/dispatch.h> 36#include <dns/fixedname.h> 37#include <dns/keyvalues.h> 38#include <dns/message.h> 39#include <dns/name.h> 40#include <dns/request.h> 41#include <dns/result.h> 42#include <dns/tkey.h> 43#include <dns/tsig.h> 44#include <dns/view.h> 45 46#include <dst/result.h> 47 48#define CHECK(str, x) { \ 49 if ((x) != ISC_R_SUCCESS) { \ 50 fprintf(stderr, "I:%s: %s\n", (str), isc_result_totext(x)); \ 51 exit(-1); \ 52 } \ 53} 54 55#define RUNCHECK(x) RUNTIME_CHECK((x) == ISC_R_SUCCESS) 56 57#define PORT 5300 58#define TIMEOUT 30 59 60static isc_mem_t *mctx; 61static dns_tsigkey_t *tsigkey; 62static dns_tsig_keyring_t *ring; 63static dns_requestmgr_t *requestmgr; 64 65static void 66recvquery(isc_task_t *task, isc_event_t *event) { 67 dns_requestevent_t *reqev = (dns_requestevent_t *)event; 68 isc_result_t result; 69 dns_message_t *query, *response; 70 71 UNUSED(task); 72 73 REQUIRE(reqev != NULL); 74 75 if (reqev->result != ISC_R_SUCCESS) { 76 fprintf(stderr, "I:request event result: %s\n", 77 isc_result_totext(reqev->result)); 78 exit(-1); 79 } 80 81 query = reqev->ev_arg; 82 83 response = NULL; 84 result = dns_message_create(mctx, DNS_MESSAGE_INTENTPARSE, &response); 85 CHECK("dns_message_create", result); 86 87 result = dns_request_getresponse(reqev->request, response, 88 DNS_MESSAGEPARSE_PRESERVEORDER); 89 CHECK("dns_request_getresponse", result); 90 91 if (response->rcode != dns_rcode_noerror) { 92 result = ISC_RESULTCLASS_DNSRCODE + response->rcode; 93 fprintf(stderr, "I:response rcode: %s\n", 94 isc_result_totext(result)); 95 exit(-1); 96 } 97 98 result = dns_tkey_processdeleteresponse(query, response, ring); 99 CHECK("dns_tkey_processdhresponse", result); 100 101 dns_message_destroy(&query); 102 dns_message_destroy(&response); 103 dns_request_destroy(&reqev->request); 104 isc_event_free(&event); 105 isc_app_shutdown(); 106 return; 107} 108 109static void 110sendquery(isc_task_t *task, isc_event_t *event) { 111 struct in_addr inaddr; 112 isc_sockaddr_t address; 113 isc_result_t result; 114 dns_message_t *query; 115 dns_request_t *request; 116 117 isc_event_free(&event); 118 119 result = ISC_R_FAILURE; 120 if (inet_pton(AF_INET, "10.53.0.1", &inaddr) != 1) 121 CHECK("inet_pton", result); 122 isc_sockaddr_fromin(&address, &inaddr, PORT); 123 124 query = NULL; 125 result = dns_message_create(mctx, DNS_MESSAGE_INTENTRENDER, &query); 126 CHECK("dns_message_create", result); 127 128 result = dns_tkey_builddeletequery(query, tsigkey); 129 CHECK("dns_tkey_builddeletequery", result); 130 131 request = NULL; 132 result = dns_request_create(requestmgr, query, &address, 133 DNS_REQUESTOPT_TCP, tsigkey, TIMEOUT, 134 task, recvquery, query, &request); 135 CHECK("dns_request_create", result); 136} 137 138int 139main(int argc, char **argv) { 140 char *keyname; 141 isc_taskmgr_t *taskmgr; 142 isc_timermgr_t *timermgr; 143 isc_socketmgr_t *socketmgr; 144 isc_socket_t *sock; 145 unsigned int attrs, attrmask; 146 isc_sockaddr_t bind_any; 147 dns_dispatchmgr_t *dispatchmgr; 148 dns_dispatch_t *dispatchv4; 149 dns_view_t *view; 150 dns_tkeyctx_t *tctx; 151 dst_key_t *dstkey; 152 isc_log_t *log; 153 isc_logconfig_t *logconfig; 154 isc_task_t *task; 155 isc_result_t result; 156 int type; 157 158 RUNCHECK(isc_app_start()); 159 160 161 if (argc < 2) { 162 fprintf(stderr, "I:no key to delete\n"); 163 exit(-1); 164 } 165 if (strcmp(argv[1], "-r") == 0) { 166 fprintf(stderr, "I:The -r options has been deprecated\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 log = NULL; 177 logconfig = NULL; 178 RUNCHECK(isc_log_create(mctx, &log, &logconfig)); 179 180 RUNCHECK(dst_lib_init(mctx, NULL)); 181 182 taskmgr = NULL; 183 RUNCHECK(isc_taskmgr_create(mctx, 1, 0, &taskmgr)); 184 task = NULL; 185 RUNCHECK(isc_task_create(taskmgr, 0, &task)); 186 timermgr = NULL; 187 RUNCHECK(isc_timermgr_create(mctx, &timermgr)); 188 socketmgr = NULL; 189 RUNCHECK(isc_socketmgr_create(mctx, &socketmgr)); 190 dispatchmgr = NULL; 191 RUNCHECK(dns_dispatchmgr_create(mctx, &dispatchmgr)); 192 isc_sockaddr_any(&bind_any); 193 attrs = DNS_DISPATCHATTR_UDP | 194 DNS_DISPATCHATTR_MAKEQUERY | 195 DNS_DISPATCHATTR_IPV4; 196 attrmask = DNS_DISPATCHATTR_UDP | 197 DNS_DISPATCHATTR_TCP | 198 DNS_DISPATCHATTR_IPV4 | 199 DNS_DISPATCHATTR_IPV6; 200 dispatchv4 = NULL; 201 RUNCHECK(dns_dispatch_getudp(dispatchmgr, socketmgr, taskmgr, 202 &bind_any, 4096, 4, 2, 3, 5, 203 attrs, attrmask, &dispatchv4)); 204 requestmgr = NULL; 205 RUNCHECK(dns_requestmgr_create(mctx, timermgr, socketmgr, taskmgr, 206 dispatchmgr, dispatchv4, NULL, 207 &requestmgr)); 208 209 ring = NULL; 210 RUNCHECK(dns_tsigkeyring_create(mctx, &ring)); 211 tctx = NULL; 212 RUNCHECK(dns_tkeyctx_create(mctx, &tctx)); 213 214 view = NULL; 215 RUNCHECK(dns_view_create(mctx, 0, "_test", &view)); 216 dns_view_setkeyring(view, ring); 217 218 sock = NULL; 219 RUNCHECK(isc_socket_create(socketmgr, PF_INET, isc_sockettype_udp, 220 &sock)); 221 222 RUNCHECK(isc_app_onrun(mctx, task, sendquery, NULL)); 223 224 dstkey = NULL; 225 type = DST_TYPE_PUBLIC | DST_TYPE_PRIVATE | DST_TYPE_KEY; 226 result = dst_key_fromnamedfile(keyname, NULL, type, mctx, &dstkey); 227 CHECK("dst_key_fromnamedfile", result); 228 result = dns_tsigkey_createfromkey(dst_key_name(dstkey), 229 DNS_TSIG_HMACMD5_NAME, 230 dstkey, true, NULL, 0, 0, 231 mctx, ring, &tsigkey); 232 dst_key_free(&dstkey); 233 CHECK("dns_tsigkey_createfromkey", result); 234 235 (void)isc_app_run(); 236 237 dns_requestmgr_shutdown(requestmgr); 238 dns_requestmgr_detach(&requestmgr); 239 dns_dispatch_detach(&dispatchv4); 240 dns_dispatchmgr_destroy(&dispatchmgr); 241 isc_task_shutdown(task); 242 isc_task_detach(&task); 243 isc_taskmgr_destroy(&taskmgr); 244 isc_socket_detach(&sock); 245 isc_socketmgr_destroy(&socketmgr); 246 isc_timermgr_destroy(&timermgr); 247 248 dns_tsigkeyring_detach(&ring); 249 250 dns_tsigkey_detach(&tsigkey); 251 252 dns_tkeyctx_destroy(&tctx); 253 254 dns_view_detach(&view); 255 256 isc_log_destroy(&log); 257 258 dst_lib_destroy(); 259 260 isc_mem_destroy(&mctx); 261 262 isc_app_finish(); 263 264 return (0); 265} 266