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