Sync usage with man page.
[netbsd-mini2440.git] / external / bsd / bind / dist / bin / tests / system / tkey / keydelete.c
blob2fef0b46912b6b338572f6feddf6c3acd8f51a07
1 /* $NetBSD$ */
3 /*
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 */
22 #include <config.h>
24 #include <stdlib.h>
25 #include <string.h>
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>
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>
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)); \
55 exit(-1); \
56 } \
59 #define RUNCHECK(x) RUNTIME_CHECK((x) == ISC_R_SUCCESS)
61 #define PORT 5300
62 #define TIMEOUT 30
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;
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;
75 UNUSED(task);
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));
82 exit(-1);
85 query = reqev->ev_arg;
87 response = NULL;
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));
99 exit(-1);
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);
109 isc_app_shutdown();
110 return;
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;
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);
128 query = NULL;
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);
135 request = NULL;
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) {
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;
163 RUNCHECK(isc_app_start());
165 if (argc < 2) {
166 fprintf(stderr, "I:no key to delete\n");
167 exit(-1);
169 keyname = argv[1];
171 dns_result_register();
173 mctx = NULL;
174 RUNCHECK(isc_mem_create(0, 0, &mctx));
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));
181 log = NULL;
182 logconfig = NULL;
183 RUNCHECK(isc_log_create(mctx, &log, &logconfig));
185 RUNCHECK(dst_lib_init(mctx, ectx, ISC_ENTROPY_GOODONLY));
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));
214 ring = NULL;
215 RUNCHECK(dns_tsigkeyring_create(mctx, &ring));
216 tctx = NULL;
217 RUNCHECK(dns_tkeyctx_create(mctx, ectx, &tctx));
219 view = NULL;
220 RUNCHECK(dns_view_create(mctx, 0, "_test", &view));
221 dns_view_setkeyring(view, ring);
223 sock = NULL;
224 RUNCHECK(isc_socket_create(socketmgr, PF_INET, isc_sockettype_udp,
225 &sock));
227 RUNCHECK(isc_app_onrun(mctx, task, sendquery, NULL));
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 CHECK("dns_tsigkey_createfromkey", result);
239 (void)isc_app_run();
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);
260 dst_lib_destroy();
261 isc_hash_destroy();
262 isc_entropy_detach(&ectx);
264 isc_mem_destroy(&mctx);
266 isc_app_finish();
268 return (0);