4 * Copyright (C) 2004, 2005, 2007, 2009 Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (C) 2001 Internet Software Consortium.
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.
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.
20 /* Id: keydelete.c,v 1.13 2009/07/19 23:47:55 tbox Exp */
28 #include <isc/base64.h>
29 #include <isc/entropy.h>
33 #include <isc/sockaddr.h>
34 #include <isc/socket.h>
36 #include <isc/timer.h>
39 #include <dns/dispatch.h>
40 #include <dns/fixedname.h>
41 #include <dns/keyvalues.h>
42 #include <dns/message.h>
44 #include <dns/request.h>
45 #include <dns/result.h>
50 #include <dst/result.h>
52 #define CHECK(str, x) { \
53 if ((x) != ISC_R_SUCCESS) { \
54 fprintf(stderr, "I:%s: %s\n", (str), isc_result_totext(x)); \
59 #define RUNCHECK(x) RUNTIME_CHECK((x) == ISC_R_SUCCESS)
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
;
70 recvquery(isc_task_t
*task
, isc_event_t
*event
) {
71 dns_requestevent_t
*reqev
= (dns_requestevent_t
*)event
;
73 dns_message_t
*query
, *response
;
77 REQUIRE(reqev
!= NULL
);
79 if (reqev
->result
!= ISC_R_SUCCESS
) {
80 fprintf(stderr
, "I:request event result: %s\n",
81 isc_result_totext(reqev
->result
));
85 query
= reqev
->ev_arg
;
88 result
= dns_message_create(mctx
, DNS_MESSAGE_INTENTPARSE
, &response
);
89 CHECK("dns_message_create", result
);
91 result
= dns_request_getresponse(reqev
->request
, response
,
92 DNS_MESSAGEPARSE_PRESERVEORDER
);
93 CHECK("dns_request_getresponse", result
);
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
));
102 result
= dns_tkey_processdeleteresponse(query
, response
, ring
);
103 CHECK("dns_tkey_processdhresponse", result
);
105 dns_message_destroy(&query
);
106 dns_message_destroy(&response
);
107 dns_request_destroy(&reqev
->request
);
108 isc_event_free(&event
);
114 sendquery(isc_task_t
*task
, isc_event_t
*event
) {
115 struct in_addr inaddr
;
116 isc_sockaddr_t address
;
118 dns_message_t
*query
;
119 dns_request_t
*request
;
121 isc_event_free(&event
);
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
);
129 result
= dns_message_create(mctx
, DNS_MESSAGE_INTENTRENDER
, &query
);
130 CHECK("dns_message_create", result
);
132 result
= dns_tkey_builddeletequery(query
, tsigkey
);
133 CHECK("dns_tkey_builddeletequery", result
);
136 result
= dns_request_create(requestmgr
, query
, &address
,
137 0, tsigkey
, TIMEOUT
, task
,
138 recvquery
, query
, &request
);
139 CHECK("dns_request_create", result
);
143 main(int argc
, char **argv
) {
145 isc_taskmgr_t
*taskmgr
;
146 isc_timermgr_t
*timermgr
;
147 isc_socketmgr_t
*socketmgr
;
149 unsigned int attrs
, attrmask
;
150 isc_sockaddr_t bind_any
;
151 dns_dispatchmgr_t
*dispatchmgr
;
152 dns_dispatch_t
*dispatchv4
;
158 isc_logconfig_t
*logconfig
;
163 RUNCHECK(isc_app_start());
166 fprintf(stderr
, "I:no key to delete\n");
171 dns_result_register();
174 RUNCHECK(isc_mem_create(0, 0, &mctx
));
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
));
183 RUNCHECK(isc_log_create(mctx
, &log
, &logconfig
));
185 RUNCHECK(dst_lib_init(mctx
, ectx
, ISC_ENTROPY_GOODONLY
));
188 RUNCHECK(isc_taskmgr_create(mctx
, 1, 0, &taskmgr
));
190 RUNCHECK(isc_task_create(taskmgr
, 0, &task
));
192 RUNCHECK(isc_timermgr_create(mctx
, &timermgr
));
194 RUNCHECK(isc_socketmgr_create(mctx
, &socketmgr
));
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
;
206 RUNCHECK(dns_dispatch_getudp(dispatchmgr
, socketmgr
, taskmgr
,
207 &bind_any
, 4096, 4, 2, 3, 5,
208 attrs
, attrmask
, &dispatchv4
));
210 RUNCHECK(dns_requestmgr_create(mctx
, timermgr
, socketmgr
, taskmgr
,
211 dispatchmgr
, dispatchv4
, NULL
,
215 RUNCHECK(dns_tsigkeyring_create(mctx
, &ring
));
217 RUNCHECK(dns_tkeyctx_create(mctx
, ectx
, &tctx
));
220 RUNCHECK(dns_view_create(mctx
, 0, "_test", &view
));
221 dns_view_setkeyring(view
, ring
);
224 RUNCHECK(isc_socket_create(socketmgr
, PF_INET
, isc_sockettype_udp
,
227 RUNCHECK(isc_app_onrun(mctx
, task
, sendquery
, 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 CHECK("dns_tsigkey_createfromkey", result
);
241 dns_requestmgr_shutdown(requestmgr
);
242 dns_requestmgr_detach(&requestmgr
);
243 dns_dispatch_detach(&dispatchv4
);
244 dns_dispatchmgr_destroy(&dispatchmgr
);
245 isc_task_shutdown(task
);
246 isc_task_detach(&task
);
247 isc_taskmgr_destroy(&taskmgr
);
248 isc_socket_detach(&sock
);
249 isc_socketmgr_destroy(&socketmgr
);
250 isc_timermgr_destroy(&timermgr
);
252 dns_tsigkey_detach(&tsigkey
);
254 dns_tkeyctx_destroy(&tctx
);
256 dns_view_detach(&view
);
258 isc_log_destroy(&log
);
262 isc_entropy_detach(&ectx
);
264 isc_mem_destroy(&mctx
);