Merge from vendor branch TEXINFO:
[dragonfly.git] / contrib / bind-9.2.4rc7 / bin / nsupdate / nsupdate.c
1 /*
2  * Copyright (C) 2004  Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (C) 2000-2003  Internet Software Consortium.
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15  * PERFORMANCE OF THIS SOFTWARE.
16  */
17
18 /* $Id: nsupdate.c,v 1.103.2.23 2004/05/12 04:46:17 marka Exp $ */
19
20 #include <config.h>
21
22 #include <ctype.h>
23 #include <errno.h>
24 #include <limits.h>
25 #include <netdb.h>
26 #include <stdlib.h>
27 #include <unistd.h>
28
29 #include <isc/app.h>
30 #include <isc/base64.h>
31 #include <isc/buffer.h>
32 #include <isc/commandline.h>
33 #include <isc/entropy.h>
34 #include <isc/event.h>
35 #include <isc/hash.h>
36 #include <isc/lex.h>
37 #include <isc/mem.h>
38 #include <isc/region.h>
39 #include <isc/sockaddr.h>
40 #include <isc/socket.h>
41 #include <isc/stdio.h>
42 #include <isc/string.h>
43 #include <isc/task.h>
44 #include <isc/timer.h>
45 #include <isc/types.h>
46 #include <isc/util.h>
47
48 #include <dns/callbacks.h>
49 #include <dns/dispatch.h>
50 #include <dns/events.h>
51 #include <dns/fixedname.h>
52 #include <dns/masterdump.h>
53 #include <dns/message.h>
54 #include <dns/name.h>
55 #include <dns/rcode.h>
56 #include <dns/rdata.h>
57 #include <dns/rdataclass.h>
58 #include <dns/rdatalist.h>
59 #include <dns/rdataset.h>
60 #include <dns/rdatastruct.h>
61 #include <dns/rdatatype.h>
62 #include <dns/request.h>
63 #include <dns/result.h>
64 #include <dns/tsig.h>
65
66 #include <dst/dst.h>
67
68 #include <lwres/lwres.h>
69 #include <lwres/net.h>
70
71 #ifdef HAVE_ADDRINFO
72 #ifdef HAVE_GETADDRINFO
73 #ifdef HAVE_GAISTRERROR
74 #define USE_GETADDRINFO
75 #endif
76 #endif
77 #endif
78
79 #ifndef USE_GETADDRINFO
80 #ifndef ISC_PLATFORM_NONSTDHERRNO
81 extern int h_errno;
82 #endif
83 #endif
84
85 #define MAXCMD (4 * 1024)
86 #define MAXWIRE (64 * 1024)
87 #define NAMEBUF 512
88 #define WORDLEN 512
89 #define PACKETSIZE ((64 * 1024) - 1)
90 #define INITTEXT (2 * 1024)
91 #define MAXTEXT (128 * 1024)
92 #define FIND_TIMEOUT 5
93 #define TTL_MAX 2147483647U     /* Maximum signed 32 bit integer. */
94
95 #define DNSDEFAULTPORT 53
96
97 #ifndef RESOLV_CONF
98 #define RESOLV_CONF "/etc/resolv.conf"
99 #endif
100
101 static isc_boolean_t debugging = ISC_FALSE, ddebugging = ISC_FALSE;
102 static isc_boolean_t memdebugging = ISC_FALSE;
103 static isc_boolean_t have_ipv4 = ISC_FALSE;
104 static isc_boolean_t have_ipv6 = ISC_FALSE;
105 static isc_boolean_t is_dst_up = ISC_FALSE;
106 static isc_boolean_t usevc = ISC_FALSE;
107 static isc_taskmgr_t *taskmgr = NULL;
108 static isc_task_t *global_task = NULL;
109 static isc_event_t *global_event = NULL;
110 static isc_mem_t *mctx = NULL;
111 static dns_dispatchmgr_t *dispatchmgr = NULL;
112 static dns_requestmgr_t *requestmgr = NULL;
113 static isc_socketmgr_t *socketmgr = NULL;
114 static isc_timermgr_t *timermgr = NULL;
115 static dns_dispatch_t *dispatchv4 = NULL;
116 static dns_dispatch_t *dispatchv6 = NULL;
117 static dns_message_t *updatemsg = NULL;
118 static dns_fixedname_t fuserzone;
119 static dns_name_t *userzone = NULL;
120 static dns_tsigkey_t *key = NULL;
121 static lwres_context_t *lwctx = NULL;
122 static lwres_conf_t *lwconf;
123 static isc_sockaddr_t *servers;
124 static int ns_inuse = 0;
125 static int ns_total = 0;
126 static isc_sockaddr_t *userserver = NULL;
127 static isc_sockaddr_t *localaddr = NULL;
128 static char *keystr = NULL, *keyfile = NULL;
129 static isc_entropy_t *entp = NULL;
130 static isc_boolean_t shuttingdown = ISC_FALSE;
131 static FILE *input;
132 static isc_boolean_t interactive = ISC_TRUE;
133 static isc_boolean_t seenerror = ISC_FALSE;
134 static const dns_master_style_t *style;
135 static int requests = 0;
136 static dns_rdataclass_t defaultclass = dns_rdataclass_in;
137 static dns_rdataclass_t zoneclass = dns_rdataclass_none;
138
139 typedef struct nsu_requestinfo {
140         dns_message_t *msg;
141         isc_sockaddr_t *addr;
142 } nsu_requestinfo_t;
143
144 static void
145 sendrequest(isc_sockaddr_t *srcaddr, isc_sockaddr_t *destaddr,
146             dns_message_t *msg, dns_request_t **request);
147 static void
148 fatal(const char *format, ...) ISC_FORMAT_PRINTF(1, 2);
149
150 static void
151 debug(const char *format, ...) ISC_FORMAT_PRINTF(1, 2);
152
153 static void
154 ddebug(const char *format, ...) ISC_FORMAT_PRINTF(1, 2);
155
156 #define STATUS_MORE     (isc_uint16_t)0
157 #define STATUS_SEND     (isc_uint16_t)1
158 #define STATUS_QUIT     (isc_uint16_t)2
159 #define STATUS_SYNTAX   (isc_uint16_t)3
160
161 static dns_rdataclass_t
162 getzoneclass(void) {
163         if (zoneclass == dns_rdataclass_none)
164                 zoneclass = defaultclass;
165         return (zoneclass);
166 }
167
168 static isc_boolean_t
169 setzoneclass(dns_rdataclass_t rdclass) {
170         if (zoneclass == dns_rdataclass_none ||
171             rdclass == dns_rdataclass_none)
172                 zoneclass = rdclass;
173         if (zoneclass != rdclass)
174                 return (ISC_FALSE);
175         return (ISC_TRUE);
176 }
177
178 static void
179 fatal(const char *format, ...) {
180         va_list args;
181
182         va_start(args, format);
183         vfprintf(stderr, format, args);
184         va_end(args);
185         fprintf(stderr, "\n");
186         exit(1);
187 }
188
189 static void
190 debug(const char *format, ...) {
191         va_list args;
192
193         if (debugging) {
194                 va_start(args, format);
195                 vfprintf(stderr, format, args);
196                 va_end(args);
197                 fprintf(stderr, "\n");
198         }
199 }
200
201 static void
202 ddebug(const char *format, ...) {
203         va_list args;
204
205         if (ddebugging) {
206                 va_start(args, format);
207                 vfprintf(stderr, format, args);
208                 va_end(args);
209                 fprintf(stderr, "\n");
210         }
211 }
212
213 static inline void
214 check_result(isc_result_t result, const char *msg) {
215         if (result != ISC_R_SUCCESS)
216                 fatal("%s: %s", msg, isc_result_totext(result));
217 }
218
219 static void *
220 mem_alloc(void *arg, size_t size) {
221         return (isc_mem_get(arg, size));
222 }
223
224 static void
225 mem_free(void *arg, void *mem, size_t size) {
226         isc_mem_put(arg, mem, size);
227 }
228
229 static char *
230 nsu_strsep(char **stringp, const char *delim) {
231         char *string = *stringp;
232         char *s;
233         const char *d;
234         char sc, dc;
235
236         if (string == NULL)
237                 return (NULL);
238
239         for (; *string != '\0'; string++) {
240                 sc = *string;
241                 for (d = delim; (dc = *d) != '\0'; d++) {
242                         if (sc == dc)
243                                 break;
244                 }
245                 if (dc == 0)
246                         break;
247         }
248
249         for (s = string; *s != '\0'; s++) {
250                 sc = *s;
251                 for (d = delim; (dc = *d) != '\0'; d++) {
252                         if (sc == dc) {
253                                 *s++ = '\0';
254                                 *stringp = s;
255                                 return (string);
256                         }
257                 }
258         }
259         *stringp = NULL;
260         return (string);
261 }
262
263 static void
264 reset_system(void) {
265         isc_result_t result;
266
267         ddebug("reset_system()");
268         /* If the update message is still around, destroy it */
269         if (updatemsg != NULL)
270                 dns_message_reset(updatemsg, DNS_MESSAGE_INTENTRENDER);
271         else {
272                 result = dns_message_create(mctx, DNS_MESSAGE_INTENTRENDER,
273                                             &updatemsg);
274                 check_result(result, "dns_message_create");
275         }
276         updatemsg->opcode = dns_opcode_update;
277 }
278
279 static void
280 setup_keystr(void) {
281         unsigned char *secret = NULL;
282         int secretlen;
283         isc_buffer_t secretbuf;
284         isc_result_t result;
285         isc_buffer_t keynamesrc;
286         char *secretstr;
287         char *s;
288         dns_fixedname_t fkeyname;
289         dns_name_t *keyname;
290
291         dns_fixedname_init(&fkeyname);
292         keyname = dns_fixedname_name(&fkeyname);
293
294         debug("Creating key...");
295
296         s = strchr(keystr, ':');
297         if (s == NULL || s == keystr || *s == 0)
298                 fatal("key option must specify keyname:secret");
299         secretstr = s + 1;
300
301         isc_buffer_init(&keynamesrc, keystr, s - keystr);
302         isc_buffer_add(&keynamesrc, s - keystr);
303
304         debug("namefromtext");
305         result = dns_name_fromtext(keyname, &keynamesrc, dns_rootname,
306                                    ISC_FALSE, NULL);
307         check_result(result, "dns_name_fromtext");
308
309         secretlen = strlen(secretstr) * 3 / 4;
310         secret = isc_mem_allocate(mctx, secretlen);
311         if (secret == NULL)
312                 fatal("out of memory");
313
314         isc_buffer_init(&secretbuf, secret, secretlen);
315         result = isc_base64_decodestring(secretstr, &secretbuf);
316         if (result != ISC_R_SUCCESS) {
317                 fprintf(stderr, "could not create key from %s: %s\n",
318                         keystr, isc_result_totext(result));
319                 goto failure;
320         }
321
322         secretlen = isc_buffer_usedlength(&secretbuf);
323
324         debug("keycreate");
325         result = dns_tsigkey_create(keyname, dns_tsig_hmacmd5_name,
326                                     secret, secretlen, ISC_TRUE, NULL,
327                                     0, 0, mctx, NULL, &key);
328         if (result != ISC_R_SUCCESS)
329                 fprintf(stderr, "could not create key from %s: %s\n",
330                         keystr, dns_result_totext(result));
331  failure:
332         if (secret != NULL)
333                 isc_mem_free(mctx, secret);
334 }
335
336 static void
337 setup_keyfile(void) {
338         dst_key_t *dstkey = NULL;
339         isc_result_t result;
340
341         debug("Creating key...");
342
343         result = dst_key_fromnamedfile(keyfile, DST_TYPE_PRIVATE, mctx,
344                                        &dstkey);
345         if (result != ISC_R_SUCCESS) {
346                 fprintf(stderr, "could not read key from %s: %s\n",
347                         keyfile, isc_result_totext(result));
348                 return;
349         }
350         result = dns_tsigkey_createfromkey(dst_key_name(dstkey),
351                                            dns_tsig_hmacmd5_name,
352                                            dstkey, ISC_FALSE, NULL,
353                                            0, 0, mctx, NULL, &key);
354         if (result != ISC_R_SUCCESS) {
355                 fprintf(stderr, "could not create key from %s: %s\n",
356                         keyfile, isc_result_totext(result));
357                 dst_key_free(&dstkey);
358                 return;
359         }
360 }
361
362 static void
363 doshutdown(void) {
364         isc_task_detach(&global_task);
365
366         if (userserver != NULL)
367                 isc_mem_put(mctx, userserver, sizeof(isc_sockaddr_t));
368
369         if (localaddr != NULL)
370                 isc_mem_put(mctx, localaddr, sizeof(isc_sockaddr_t));
371
372         if (key != NULL) {
373                 ddebug("Freeing key");
374                 dns_tsigkey_detach(&key);
375         }
376
377         if (updatemsg != NULL)
378                 dns_message_destroy(&updatemsg);
379
380         if (is_dst_up) {
381                 ddebug("Destroy DST lib");
382                 dst_lib_destroy();
383                 is_dst_up = ISC_FALSE;
384         }
385
386         if (entp != NULL) {
387                 ddebug("Detach from entropy");
388                 isc_entropy_detach(&entp);
389         }
390
391         lwres_conf_clear(lwctx);
392         lwres_context_destroy(&lwctx);
393
394         isc_mem_put(mctx, servers, ns_total * sizeof(isc_sockaddr_t));
395
396         ddebug("Destroying request manager");
397         dns_requestmgr_detach(&requestmgr);
398
399         ddebug("Freeing the dispatchers");
400         if (have_ipv4)
401                 dns_dispatch_detach(&dispatchv4);
402         if (have_ipv6)
403                 dns_dispatch_detach(&dispatchv6);
404
405         ddebug("Shutting down dispatch manager");
406         dns_dispatchmgr_destroy(&dispatchmgr);
407
408 }
409
410 static void
411 maybeshutdown(void) {
412         ddebug("Shutting down request manager");
413         dns_requestmgr_shutdown(requestmgr);
414
415         if (requests != 0)
416                 return;
417
418         doshutdown();
419 }
420
421 static void
422 shutdown_program(isc_task_t *task, isc_event_t *event) {
423         REQUIRE(task == global_task);
424         UNUSED(task);
425
426         ddebug("shutdown_program()");
427         isc_event_free(&event);
428
429         shuttingdown = ISC_TRUE;
430         maybeshutdown();
431 }
432
433 static void
434 setup_system(void) {
435         isc_result_t result;
436         isc_sockaddr_t bind_any, bind_any6;
437         lwres_result_t lwresult;
438         unsigned int attrs, attrmask;
439         int i;
440
441         ddebug("setup_system()");
442
443         dns_result_register();
444
445         result = isc_net_probeipv4();
446         if (result == ISC_R_SUCCESS)
447                 have_ipv4 = ISC_TRUE;
448
449         result = isc_net_probeipv6();
450         if (result == ISC_R_SUCCESS)
451                 have_ipv6 = ISC_TRUE;
452
453         if (!have_ipv4 && !have_ipv6)
454                 fatal("could not find either IPv4 or IPv6");
455
456         result = isc_mem_create(0, 0, &mctx);
457         check_result(result, "isc_mem_create");
458
459         lwresult = lwres_context_create(&lwctx, mctx, mem_alloc, mem_free, 1);
460         if (lwresult != LWRES_R_SUCCESS)
461                 fatal("lwres_context_create failed");
462
463         (void)lwres_conf_parse(lwctx, RESOLV_CONF);
464         lwconf = lwres_conf_get(lwctx);
465
466         ns_total = lwconf->nsnext;
467         if (ns_total <= 0) {
468                 /* No name servers in resolv.conf; default to loopback. */
469                 struct in_addr localhost;
470                 ns_total = 1;
471                 servers = isc_mem_get(mctx, ns_total * sizeof(isc_sockaddr_t));
472                 if (servers == NULL)
473                         fatal("out of memory");
474                 localhost.s_addr = htonl(INADDR_LOOPBACK);
475                 isc_sockaddr_fromin(&servers[0], &localhost, DNSDEFAULTPORT);
476         } else {
477                 servers = isc_mem_get(mctx, ns_total * sizeof(isc_sockaddr_t));
478                 if (servers == NULL)
479                         fatal("out of memory");
480                 for (i = 0; i < ns_total; i++) {
481                         if (lwconf->nameservers[i].family == LWRES_ADDRTYPE_V4) {
482                                 struct in_addr in4;
483                                 memcpy(&in4, lwconf->nameservers[i].address, 4);
484                                 isc_sockaddr_fromin(&servers[i], &in4, DNSDEFAULTPORT);
485                         } else {
486                                 struct in6_addr in6;
487                                 memcpy(&in6, lwconf->nameservers[i].address, 16);
488                                 isc_sockaddr_fromin6(&servers[i], &in6,
489                                                      DNSDEFAULTPORT);
490                         }
491                 }
492         }
493
494         result = isc_entropy_create(mctx, &entp);
495         check_result(result, "isc_entropy_create");
496
497         result = isc_hash_create(mctx, entp, DNS_NAME_MAXWIRE);
498         check_result(result, "isc_hash_create");
499         isc_hash_init();
500
501         result = dns_dispatchmgr_create(mctx, entp, &dispatchmgr);
502         check_result(result, "dns_dispatchmgr_create");
503
504         result = isc_socketmgr_create(mctx, &socketmgr);
505         check_result(result, "dns_socketmgr_create");
506
507         result = isc_timermgr_create(mctx, &timermgr);
508         check_result(result, "dns_timermgr_create");
509
510         result = isc_taskmgr_create(mctx, 1, 0, &taskmgr);
511         check_result(result, "isc_taskmgr_create");
512
513         result = isc_task_create(taskmgr, 0, &global_task);
514         check_result(result, "isc_task_create");
515
516         result = isc_task_onshutdown(global_task, shutdown_program, NULL);
517         check_result(result, "isc_task_onshutdown");
518
519         result = dst_lib_init(mctx, entp, 0);
520         check_result(result, "dst_lib_init");
521         is_dst_up = ISC_TRUE;
522
523         attrmask = DNS_DISPATCHATTR_UDP | DNS_DISPATCHATTR_TCP;
524         attrmask |= DNS_DISPATCHATTR_IPV4 | DNS_DISPATCHATTR_IPV6;
525
526         if (have_ipv6) {
527                 attrs = DNS_DISPATCHATTR_UDP;
528                 attrs |= DNS_DISPATCHATTR_MAKEQUERY;
529                 attrs |= DNS_DISPATCHATTR_IPV6;
530                 isc_sockaddr_any6(&bind_any6);
531                 result = dns_dispatch_getudp(dispatchmgr, socketmgr, taskmgr,
532                                              &bind_any6, PACKETSIZE,
533                                              4, 2, 3, 5,
534                                              attrs, attrmask, &dispatchv6);
535                 check_result(result, "dns_dispatch_getudp (v6)");
536         }
537
538         if (have_ipv4) {
539                 attrs = DNS_DISPATCHATTR_UDP;
540                 attrs |= DNS_DISPATCHATTR_MAKEQUERY;
541                 attrs |= DNS_DISPATCHATTR_IPV4;
542                 isc_sockaddr_any(&bind_any);
543                 result = dns_dispatch_getudp(dispatchmgr, socketmgr, taskmgr,
544                                              &bind_any, PACKETSIZE,
545                                              4, 2, 3, 5,
546                                              attrs, attrmask, &dispatchv4);
547                 check_result(result, "dns_dispatch_getudp (v4)");
548         }
549
550         result = dns_requestmgr_create(mctx, timermgr,
551                                        socketmgr, taskmgr, dispatchmgr,
552                                        dispatchv4, dispatchv6, &requestmgr);
553         check_result(result, "dns_requestmgr_create");
554
555         if (keystr != NULL)
556                 setup_keystr();
557         else if (keyfile != NULL)
558                 setup_keyfile();
559 }
560
561 static void
562 get_address(char *host, in_port_t port, isc_sockaddr_t *sockaddr) {
563         struct in_addr in4;
564         struct in6_addr in6;
565 #ifdef USE_GETADDRINFO
566         struct addrinfo *res = NULL, hints;
567         int result;
568 #else
569         struct hostent *he;
570 #endif
571
572         ddebug("get_address()");
573
574         /*
575          * Assume we have v4 if we don't have v6, since setup_libs
576          * fatal()'s out if we don't have either.
577          */
578         if (have_ipv6 && inet_pton(AF_INET6, host, &in6) == 1)
579                 isc_sockaddr_fromin6(sockaddr, &in6, port);
580         else if (inet_pton(AF_INET, host, &in4) == 1)
581                 isc_sockaddr_fromin(sockaddr, &in4, port);
582         else {
583 #ifdef USE_GETADDRINFO
584                 memset(&hints, 0, sizeof(hints));
585                 if (!have_ipv6)
586                         hints.ai_family = PF_INET;
587                 else if (!have_ipv4)
588                         hints.ai_family = PF_INET6;
589                 else {
590                         hints.ai_family = PF_UNSPEC;
591 #ifdef AI_ADDRCONFIG
592                         hints.ai_flags = AI_ADDRCONFIG;
593 #endif
594                 }
595                 debug ("before getaddrinfo()");
596                 isc_app_block();
597 #ifdef AI_ADDRCONFIG
598  again:
599 #endif
600                 result = getaddrinfo(host, NULL, &hints, &res);
601 #ifdef AI_ADDRCONFIG
602                 if (result == EAI_BADFLAGS &&
603                     (hints.ai_flags & AI_ADDRCONFIG) != 0) {
604                         hints.ai_flags &= ~AI_ADDRCONFIG;
605                         goto again;
606                 }
607 #endif
608                 isc_app_unblock();
609                 if (result != 0) {
610                         fatal("couldn't find server '%s': %s",
611                               host, gai_strerror(result));
612                 }
613                 memcpy(&sockaddr->type.sa,res->ai_addr, res->ai_addrlen);
614                 sockaddr->length = res->ai_addrlen;
615                 isc_sockaddr_setport(sockaddr, port);
616                 freeaddrinfo(res);
617 #else
618                 debug ("before gethostbyname()");
619                 isc_app_block();
620                 he = gethostbyname(host);
621                 isc_app_unblock();
622                 if (he == NULL)
623                      fatal("couldn't find server '%s' (h_errno=%d)",
624                            host, h_errno);
625                 INSIST(he->h_addrtype == AF_INET);
626                 isc_sockaddr_fromin(sockaddr,
627                                     (struct in_addr *)(he->h_addr_list[0]),
628                                     port);
629 #endif
630         }
631 }
632
633 static void
634 parse_args(int argc, char **argv) {
635         int ch;
636         isc_result_t result;
637
638         debug("parse_args");
639         while ((ch = isc_commandline_parse(argc, argv, "dDMy:vk:")) != -1) {
640                 switch (ch) {
641                 case 'd':
642                         debugging = ISC_TRUE;
643                         break;
644                 case 'D': /* was -dd */
645                         debugging = ISC_TRUE;
646                         ddebugging = ISC_TRUE;
647                         break;
648                 case 'M': /* was -dm */
649                         debugging = ISC_TRUE;
650                         ddebugging = ISC_TRUE;
651                         memdebugging = ISC_TRUE;
652                         isc_mem_debugging = ISC_MEM_DEBUGTRACE |
653                                             ISC_MEM_DEBUGRECORD;
654                         break;
655                 case 'y':
656                         keystr = isc_commandline_argument;
657                         break;
658                 case 'v':
659                         usevc = ISC_TRUE;
660                         break;
661                 case 'k':
662                         keyfile = isc_commandline_argument;
663                         break;
664                 default:
665                         fprintf(stderr, "%s: invalid argument -%c\n",
666                                 argv[0], ch);
667                         fprintf(stderr, "usage: nsupdate [-d] "
668                                 "[-y keyname:secret | -k keyfile] [-v] "
669                                 "[filename]\n");
670                         exit(1);
671                 }
672         }
673         if (keyfile != NULL && keystr != NULL) {
674                 fprintf(stderr, "%s: cannot specify both -k and -y\n",
675                         argv[0]);
676                 exit(1);
677         }
678
679         if (argv[isc_commandline_index] != NULL) {
680                 if (strcmp(argv[isc_commandline_index], "-") == 0) {
681                         input = stdin;
682                 } else {
683                         result = isc_stdio_open(argv[isc_commandline_index],
684                                                 "r", &input);
685                         if (result != ISC_R_SUCCESS) {
686                                 fprintf(stderr, "could not open '%s': %s\n",
687                                         argv[isc_commandline_index],
688                                         isc_result_totext(result));
689                                 exit(1);
690                         }
691                 }
692                 interactive = ISC_FALSE;
693         }
694 }
695
696 static isc_uint16_t
697 parse_name(char **cmdlinep, dns_message_t *msg, dns_name_t **namep) {
698         isc_result_t result;
699         char *word;
700         isc_buffer_t *namebuf = NULL;
701         isc_buffer_t source;
702
703         word = nsu_strsep(cmdlinep, " \t\r\n");
704         if (*word == 0) {
705                 fprintf(stderr, "could not read owner name\n");
706                 return (STATUS_SYNTAX);
707         }
708
709         result = dns_message_gettempname(msg, namep);
710         check_result(result, "dns_message_gettempname");
711         result = isc_buffer_allocate(mctx, &namebuf, NAMEBUF);
712         check_result(result, "isc_buffer_allocate");
713         dns_name_init(*namep, NULL);
714         dns_name_setbuffer(*namep, namebuf);
715         dns_message_takebuffer(msg, &namebuf);
716         isc_buffer_init(&source, word, strlen(word));
717         isc_buffer_add(&source, strlen(word));
718         result = dns_name_fromtext(*namep, &source, dns_rootname,
719                                    ISC_FALSE, NULL);
720         check_result(result, "dns_name_fromtext");
721         isc_buffer_invalidate(&source);
722         return (STATUS_MORE);
723 }
724
725 static isc_uint16_t
726 parse_rdata(char **cmdlinep, dns_rdataclass_t rdataclass,
727             dns_rdatatype_t rdatatype, dns_message_t *msg,
728             dns_rdata_t *rdata)
729 {
730         char *cmdline = *cmdlinep;
731         isc_buffer_t source, *buf = NULL, *newbuf = NULL;
732         isc_region_t r;
733         isc_lex_t *lex = NULL;
734         dns_rdatacallbacks_t callbacks;
735         isc_result_t result;
736
737         while (*cmdline != 0 && isspace((unsigned char)*cmdline))
738                 cmdline++;
739
740         if (*cmdline != 0) {
741                 dns_rdatacallbacks_init(&callbacks);
742                 result = isc_lex_create(mctx, strlen(cmdline), &lex);
743                 check_result(result, "isc_lex_create");
744                 isc_buffer_init(&source, cmdline, strlen(cmdline));
745                 isc_buffer_add(&source, strlen(cmdline));
746                 result = isc_lex_openbuffer(lex, &source);
747                 check_result(result, "isc_lex_openbuffer");
748                 result = isc_buffer_allocate(mctx, &buf, MAXWIRE);
749                 check_result(result, "isc_buffer_allocate");
750                 result = dns_rdata_fromtext(rdata, rdataclass, rdatatype, lex,
751                                             dns_rootname, ISC_FALSE, mctx, buf,
752                                             &callbacks);
753                 isc_lex_destroy(&lex);
754                 if (result == ISC_R_SUCCESS) {
755                         isc_buffer_usedregion(buf, &r);
756                         result = isc_buffer_allocate(mctx, &newbuf, r.length);
757                         check_result(result, "isc_buffer_allocate");
758                         isc_buffer_putmem(newbuf, r.base, r.length);
759                         isc_buffer_usedregion(newbuf, &r);
760                         dns_rdata_fromregion(rdata, rdataclass, rdatatype, &r);
761                         isc_buffer_free(&buf);
762                         dns_message_takebuffer(msg, &newbuf);
763                 } else {
764                         fprintf(stderr, "invalid rdata format: %s\n",
765                                 isc_result_totext(result));
766                         isc_buffer_free(&buf);
767                         return (STATUS_SYNTAX);
768                 }
769         } else {
770                 rdata->flags = DNS_RDATA_UPDATE;
771         }
772         *cmdlinep = cmdline;
773         return (STATUS_MORE);
774 }
775
776 static isc_uint16_t
777 make_prereq(char *cmdline, isc_boolean_t ispositive, isc_boolean_t isrrset) {
778         isc_result_t result;
779         char *word;
780         dns_name_t *name = NULL;
781         isc_textregion_t region;
782         dns_rdataset_t *rdataset = NULL;
783         dns_rdatalist_t *rdatalist = NULL;
784         dns_rdataclass_t rdataclass;
785         dns_rdatatype_t rdatatype;
786         dns_rdata_t *rdata = NULL;
787         isc_uint16_t retval;
788
789         ddebug("make_prereq()");
790
791         /*
792          * Read the owner name
793          */
794         retval = parse_name(&cmdline, updatemsg, &name);
795         if (retval != STATUS_MORE)
796                 return (retval);
797
798         /*
799          * If this is an rrset prereq, read the class or type.
800          */
801         if (isrrset) {
802                 word = nsu_strsep(&cmdline, " \t\r\n");
803                 if (*word == 0) {
804                         fprintf(stderr, "could not read class or type\n");
805                         goto failure;
806                 }
807                 region.base = word;
808                 region.length = strlen(word);
809                 result = dns_rdataclass_fromtext(&rdataclass, &region);
810                 if (result == ISC_R_SUCCESS) {
811                         if (!setzoneclass(rdataclass)) {
812                                 fprintf(stderr, "class mismatch: %s\n", word);
813                                 goto failure;
814                         }
815                         /*
816                          * Now read the type.
817                          */
818                         word = nsu_strsep(&cmdline, " \t\r\n");
819                         if (*word == 0) {
820                                 fprintf(stderr, "could not read type\n");
821                                 goto failure;
822                         }
823                         region.base = word;
824                         region.length = strlen(word);
825                         result = dns_rdatatype_fromtext(&rdatatype, &region);
826                         if (result != ISC_R_SUCCESS) {
827                                 fprintf(stderr, "invalid type: %s\n", word);
828                                 goto failure;
829                         }
830                 } else {
831                         rdataclass = getzoneclass();
832                         result = dns_rdatatype_fromtext(&rdatatype, &region);
833                         if (result != ISC_R_SUCCESS) {
834                                 fprintf(stderr, "invalid type: %s\n", word);
835                                 goto failure;
836                         }
837                 }
838         } else
839                 rdatatype = dns_rdatatype_any;
840
841         result = dns_message_gettemprdata(updatemsg, &rdata);
842         check_result(result, "dns_message_gettemprdata");
843
844         rdata->data = NULL;
845         rdata->length = 0;
846
847         if (isrrset && ispositive) {
848                 retval = parse_rdata(&cmdline, rdataclass, rdatatype,
849                                      updatemsg, rdata);
850                 if (retval != STATUS_MORE)
851                         goto failure;
852         } else
853                 rdata->flags = DNS_RDATA_UPDATE;
854
855         result = dns_message_gettemprdatalist(updatemsg, &rdatalist);
856         check_result(result, "dns_message_gettemprdatalist");
857         result = dns_message_gettemprdataset(updatemsg, &rdataset);
858         check_result(result, "dns_message_gettemprdataset");
859         dns_rdatalist_init(rdatalist);
860         rdatalist->type = rdatatype;
861         if (ispositive) {
862                 if (isrrset && rdata->data != NULL)
863                         rdatalist->rdclass = rdataclass;
864                 else
865                         rdatalist->rdclass = dns_rdataclass_any;
866         } else
867                 rdatalist->rdclass = dns_rdataclass_none;
868         rdatalist->covers = 0;
869         rdatalist->ttl = 0;
870         rdata->rdclass = rdatalist->rdclass;
871         rdata->type = rdatatype;
872         ISC_LIST_INIT(rdatalist->rdata);
873         ISC_LIST_APPEND(rdatalist->rdata, rdata, link);
874         dns_rdataset_init(rdataset);
875         dns_rdatalist_tordataset(rdatalist, rdataset);
876         ISC_LIST_INIT(name->list);
877         ISC_LIST_APPEND(name->list, rdataset, link);
878         dns_message_addname(updatemsg, name, DNS_SECTION_PREREQUISITE);
879         return (STATUS_MORE);
880
881  failure:
882         if (name != NULL)
883                 dns_message_puttempname(updatemsg, &name);
884         return (STATUS_SYNTAX);
885 }
886
887 static isc_uint16_t
888 evaluate_prereq(char *cmdline) {
889         char *word;
890         isc_boolean_t ispositive, isrrset;
891
892         ddebug("evaluate_prereq()");
893         word = nsu_strsep(&cmdline, " \t\r\n");
894         if (*word == 0) {
895                 fprintf(stderr, "could not read operation code\n");
896                 return (STATUS_SYNTAX);
897         }
898         if (strcasecmp(word, "nxdomain") == 0) {
899                 ispositive = ISC_FALSE;
900                 isrrset = ISC_FALSE;
901         } else if (strcasecmp(word, "yxdomain") == 0) {
902                 ispositive = ISC_TRUE;
903                 isrrset = ISC_FALSE;
904         } else if (strcasecmp(word, "nxrrset") == 0) {
905                 ispositive = ISC_FALSE;
906                 isrrset = ISC_TRUE;
907         } else if (strcasecmp(word, "yxrrset") == 0) {
908                 ispositive = ISC_TRUE;
909                 isrrset = ISC_TRUE;
910         } else {
911                 fprintf(stderr, "incorrect operation code: %s\n", word);
912                 return (STATUS_SYNTAX);
913         }
914         return (make_prereq(cmdline, ispositive, isrrset));
915 }
916
917 static isc_uint16_t
918 evaluate_server(char *cmdline) {
919         char *word, *server;
920         long port;
921
922         word = nsu_strsep(&cmdline, " \t\r\n");
923         if (*word == 0) {
924                 fprintf(stderr, "could not read server name\n");
925                 return (STATUS_SYNTAX);
926         }
927         server = word;
928
929         word = nsu_strsep(&cmdline, " \t\r\n");
930         if (*word == 0)
931                 port = DNSDEFAULTPORT;
932         else {
933                 char *endp;
934                 port = strtol(word, &endp, 10);
935                 if (*endp != 0) {
936                         fprintf(stderr, "port '%s' is not numeric\n", word);
937                         return (STATUS_SYNTAX);
938                 } else if (port < 1 || port > 65535) {
939                         fprintf(stderr, "port '%s' is out of range "
940                                 "(1 to 65535)\n", word);
941                         return (STATUS_SYNTAX);
942                 }
943         }
944
945         if (userserver == NULL) {
946                 userserver = isc_mem_get(mctx, sizeof(isc_sockaddr_t));
947                 if (userserver == NULL)
948                         fatal("out of memory");
949         }
950
951         get_address(server, (in_port_t)port, userserver);
952
953         return (STATUS_MORE);
954 }
955
956 static isc_uint16_t
957 evaluate_local(char *cmdline) {
958         char *word, *local;
959         long port;
960         struct in_addr in4;
961         struct in6_addr in6;
962
963         word = nsu_strsep(&cmdline, " \t\r\n");
964         if (*word == 0) {
965                 fprintf(stderr, "could not read server name\n");
966                 return (STATUS_SYNTAX);
967         }
968         local = word;
969
970         word = nsu_strsep(&cmdline, " \t\r\n");
971         if (*word == 0)
972                 port = 0;
973         else {
974                 char *endp;
975                 port = strtol(word, &endp, 10);
976                 if (*endp != 0) {
977                         fprintf(stderr, "port '%s' is not numeric\n", word);
978                         return (STATUS_SYNTAX);
979                 } else if (port < 1 || port > 65535) {
980                         fprintf(stderr, "port '%s' is out of range "
981                                 "(1 to 65535)\n", word);
982                         return (STATUS_SYNTAX);
983                 }
984         }
985
986         if (localaddr == NULL) {
987                 localaddr = isc_mem_get(mctx, sizeof(isc_sockaddr_t));
988                 if (localaddr == NULL)
989                         fatal("out of memory");
990         }
991
992         if (have_ipv6 && inet_pton(AF_INET6, local, &in6) == 1)
993                 isc_sockaddr_fromin6(localaddr, &in6, (in_port_t)port);
994         else if (have_ipv4 && inet_pton(AF_INET, local, &in4) == 1)
995                 isc_sockaddr_fromin(localaddr, &in4, (in_port_t)port);
996         else {
997                 fprintf(stderr, "invalid address %s", local);
998                 return (STATUS_SYNTAX);
999         }
1000
1001         return (STATUS_MORE);
1002 }
1003
1004 static isc_uint16_t
1005 evaluate_key(char *cmdline) {
1006         char *namestr;
1007         char *secretstr;
1008         isc_buffer_t b;
1009         isc_result_t result;
1010         dns_fixedname_t fkeyname;
1011         dns_name_t *keyname;
1012         int secretlen;
1013         unsigned char *secret = NULL;
1014         isc_buffer_t secretbuf;
1015
1016         namestr = nsu_strsep(&cmdline, " \t\r\n");
1017         if (*namestr == 0) {
1018                 fprintf(stderr, "could not read key name\n");
1019                 return (STATUS_SYNTAX);
1020         }
1021
1022         dns_fixedname_init(&fkeyname);
1023         keyname = dns_fixedname_name(&fkeyname);
1024
1025         isc_buffer_init(&b, namestr, strlen(namestr));
1026         isc_buffer_add(&b, strlen(namestr));
1027         result = dns_name_fromtext(keyname, &b, dns_rootname, ISC_FALSE, NULL);
1028         if (result != ISC_R_SUCCESS) {
1029                 fprintf(stderr, "could not parse key name\n");
1030                 return (STATUS_SYNTAX);
1031         }
1032
1033         secretstr = nsu_strsep(&cmdline, "\r\n");
1034         if (*secretstr == 0) {
1035                 fprintf(stderr, "could not read key secret\n");
1036                 return (STATUS_SYNTAX);
1037         }
1038         secretlen = strlen(secretstr) * 3 / 4;
1039         secret = isc_mem_allocate(mctx, secretlen);
1040         if (secret == NULL)
1041                 fatal("out of memory");
1042         
1043         isc_buffer_init(&secretbuf, secret, secretlen);
1044         result = isc_base64_decodestring(secretstr, &secretbuf);
1045         if (result != ISC_R_SUCCESS) {
1046                 fprintf(stderr, "could not create key from %s: %s\n",
1047                         secretstr, isc_result_totext(result));
1048                 isc_mem_free(mctx, secret);
1049                 return (STATUS_SYNTAX);
1050         }
1051         secretlen = isc_buffer_usedlength(&secretbuf);
1052
1053         if (key != NULL)
1054                 dns_tsigkey_detach(&key);
1055         result = dns_tsigkey_create(keyname, dns_tsig_hmacmd5_name,
1056                                     secret, secretlen, ISC_TRUE, NULL, 0, 0,
1057                                     mctx, NULL, &key);
1058         isc_mem_free(mctx, secret);
1059         if (result != ISC_R_SUCCESS) {
1060                 fprintf(stderr, "could not create key from %s %s: %s\n",
1061                         namestr, secretstr, dns_result_totext(result));
1062                 return (STATUS_SYNTAX);
1063         }
1064         return (STATUS_MORE);
1065 }
1066
1067 static isc_uint16_t
1068 evaluate_zone(char *cmdline) {
1069         char *word;
1070         isc_buffer_t b;
1071         isc_result_t result;
1072
1073         word = nsu_strsep(&cmdline, " \t\r\n");
1074         if (*word == 0) {
1075                 fprintf(stderr, "could not read zone name\n");
1076                 return (STATUS_SYNTAX);
1077         }
1078
1079         dns_fixedname_init(&fuserzone);
1080         userzone = dns_fixedname_name(&fuserzone);
1081         isc_buffer_init(&b, word, strlen(word));
1082         isc_buffer_add(&b, strlen(word));
1083         result = dns_name_fromtext(userzone, &b, dns_rootname, ISC_FALSE,
1084                                    NULL);
1085         if (result != ISC_R_SUCCESS) {
1086                 userzone = NULL; /* Lest it point to an invalid name */
1087                 fprintf(stderr, "could not parse zone name\n");
1088                 return (STATUS_SYNTAX);
1089         }
1090
1091         return (STATUS_MORE);
1092 }
1093
1094 static isc_uint16_t
1095 evaluate_class(char *cmdline) {
1096         char *word;
1097         isc_textregion_t r;
1098         isc_result_t result;
1099         dns_rdataclass_t rdclass;
1100
1101         word = nsu_strsep(&cmdline, " \t\r\n");
1102         if (*word == 0) {
1103                 fprintf(stderr, "could not read class name\n");
1104                 return (STATUS_SYNTAX);
1105         }
1106
1107         r.base = word;
1108         r.length = strlen(word);
1109         result = dns_rdataclass_fromtext(&rdclass, &r);
1110         if (result != ISC_R_SUCCESS) {
1111                 fprintf(stderr, "could not parse class name: %s\n", word);
1112                 return (STATUS_SYNTAX);
1113         }
1114         switch (rdclass) {
1115         case dns_rdataclass_none:
1116         case dns_rdataclass_any:
1117         case dns_rdataclass_reserved0:
1118                 fprintf(stderr, "bad default class: %s\n", word);
1119                 return (STATUS_SYNTAX);
1120         default:
1121                 defaultclass = rdclass;
1122         }
1123
1124         return (STATUS_MORE);
1125 }
1126
1127 static isc_uint16_t
1128 update_addordelete(char *cmdline, isc_boolean_t isdelete) {
1129         isc_result_t result;
1130         dns_name_t *name = NULL;
1131         unsigned long ttl;
1132         char *word;
1133         dns_rdataclass_t rdataclass;
1134         dns_rdatatype_t rdatatype;
1135         dns_rdata_t *rdata = NULL;
1136         dns_rdatalist_t *rdatalist = NULL;
1137         dns_rdataset_t *rdataset = NULL;
1138         isc_textregion_t region;
1139         char *endp;
1140         isc_uint16_t retval;
1141
1142         ddebug("update_addordelete()");
1143
1144         /*
1145          * Read the owner name.
1146          */
1147         retval = parse_name(&cmdline, updatemsg, &name);
1148         if (retval != STATUS_MORE)
1149                 return (retval);
1150
1151         result = dns_message_gettemprdata(updatemsg, &rdata);
1152         check_result(result, "dns_message_gettemprdata");
1153
1154         rdata->rdclass = 0;
1155         rdata->type = 0;
1156         rdata->data = NULL;
1157         rdata->length = 0;
1158
1159         /*
1160          * If this is an add, read the TTL and verify that it's in range.
1161          * If it's a delete, ignore a TTL if present (for compatibility).
1162          */
1163         word = nsu_strsep(&cmdline, " \t\r\n");
1164         if (*word == 0) {
1165                 if (!isdelete) {
1166                         fprintf(stderr, "could not read owner ttl\n");
1167                         goto failure;
1168                 }
1169                 else {
1170                         ttl = 0;
1171                         rdataclass = dns_rdataclass_any;
1172                         rdatatype = dns_rdatatype_any;
1173                         rdata->flags = DNS_RDATA_UPDATE;
1174                         goto doneparsing;
1175                 }
1176         }
1177         ttl = strtoul(word, &endp, 10);
1178         if (!isdigit((unsigned char)*word) || *endp != '\0') {
1179                 if (isdelete) {
1180                         ttl = 0;
1181                         goto parseclass;
1182                 } else {
1183                         fprintf(stderr, "ttl '%s' is not legal\n", word);
1184                         goto failure;
1185                 }
1186         }
1187
1188         if (isdelete)
1189                 ttl = 0;
1190         else if (ttl > TTL_MAX) {
1191                 fprintf(stderr, "ttl '%s' is out of range (0 to %u)\n",
1192                         word, TTL_MAX);
1193                 goto failure;
1194         }
1195
1196         /*
1197          * Read the class or type.
1198          */
1199         word = nsu_strsep(&cmdline, " \t\r\n");
1200  parseclass:
1201         if (*word == 0) {
1202                 if (isdelete) {
1203                         rdataclass = dns_rdataclass_any;
1204                         rdatatype = dns_rdatatype_any;
1205                         rdata->flags = DNS_RDATA_UPDATE;
1206                         goto doneparsing;
1207                 } else {
1208                         fprintf(stderr, "could not read class or type\n");
1209                         goto failure;
1210                 }
1211         }
1212         region.base = word;
1213         region.length = strlen(word);
1214         result = dns_rdataclass_fromtext(&rdataclass, &region);
1215         if (result == ISC_R_SUCCESS) {
1216                 if (!setzoneclass(rdataclass)) {
1217                         fprintf(stderr, "class mismatch: %s\n", word);
1218                         goto failure;
1219                 }
1220                 /*
1221                  * Now read the type.
1222                  */
1223                 word = nsu_strsep(&cmdline, " \t\r\n");
1224                 if (*word == 0) {
1225                         if (isdelete) {
1226                                 rdataclass = dns_rdataclass_any;
1227                                 rdatatype = dns_rdatatype_any;
1228                                 rdata->flags = DNS_RDATA_UPDATE;
1229                                 goto doneparsing;
1230                         } else {
1231                                 fprintf(stderr, "could not read type\n");
1232                                 goto failure;
1233                         }
1234                 }
1235                 region.base = word;
1236                 region.length = strlen(word);
1237                 result = dns_rdatatype_fromtext(&rdatatype, &region);
1238                 if (result != ISC_R_SUCCESS) {
1239                         fprintf(stderr, "'%s' is not a valid type: %s\n",
1240                                 word, isc_result_totext(result));
1241                         goto failure;
1242                 }
1243         } else {
1244                 rdataclass = getzoneclass();
1245                 result = dns_rdatatype_fromtext(&rdatatype, &region);
1246                 if (result != ISC_R_SUCCESS) {
1247                         fprintf(stderr, "'%s' is not a valid class or type: "
1248                                 "%s\n", word, isc_result_totext(result));
1249                         goto failure;
1250                 }
1251         }
1252
1253         retval = parse_rdata(&cmdline, rdataclass, rdatatype, updatemsg,
1254                              rdata);
1255         if (retval != STATUS_MORE)
1256                 goto failure;
1257
1258         if (isdelete) {
1259                 if ((rdata->flags & DNS_RDATA_UPDATE) != 0)
1260                         rdataclass = dns_rdataclass_any;
1261                 else
1262                         rdataclass = dns_rdataclass_none;
1263         } else {
1264                 if ((rdata->flags & DNS_RDATA_UPDATE) != 0) {
1265                         fprintf(stderr, "could not read rdata\n");
1266                         goto failure;
1267                 }
1268         }
1269
1270  doneparsing:
1271
1272         result = dns_message_gettemprdatalist(updatemsg, &rdatalist);
1273         check_result(result, "dns_message_gettemprdatalist");
1274         result = dns_message_gettemprdataset(updatemsg, &rdataset);
1275         check_result(result, "dns_message_gettemprdataset");
1276         dns_rdatalist_init(rdatalist);
1277         rdatalist->type = rdatatype;
1278         rdatalist->rdclass = rdataclass;
1279         rdatalist->covers = rdatatype;
1280         rdatalist->ttl = (dns_ttl_t)ttl;
1281         ISC_LIST_INIT(rdatalist->rdata);
1282         ISC_LIST_APPEND(rdatalist->rdata, rdata, link);
1283         dns_rdataset_init(rdataset);
1284         dns_rdatalist_tordataset(rdatalist, rdataset);
1285         ISC_LIST_INIT(name->list);
1286         ISC_LIST_APPEND(name->list, rdataset, link);
1287         dns_message_addname(updatemsg, name, DNS_SECTION_UPDATE);
1288         return (STATUS_MORE);
1289
1290  failure:
1291         if (name != NULL)
1292                 dns_message_puttempname(updatemsg, &name);
1293         if (rdata != NULL)
1294                 dns_message_puttemprdata(updatemsg, &rdata);
1295         return (STATUS_SYNTAX);
1296 }
1297
1298 static isc_uint16_t
1299 evaluate_update(char *cmdline) {
1300         char *word;
1301         isc_boolean_t isdelete;
1302
1303         ddebug("evaluate_update()");
1304         word = nsu_strsep(&cmdline, " \t\r\n");
1305         if (*word == 0) {
1306                 fprintf(stderr, "could not read operation code\n");
1307                 return (STATUS_SYNTAX);
1308         }
1309         if (strcasecmp(word, "delete") == 0)
1310                 isdelete = ISC_TRUE;
1311         else if (strcasecmp(word, "add") == 0)
1312                 isdelete = ISC_FALSE;
1313         else {
1314                 fprintf(stderr, "incorrect operation code: %s\n", word);
1315                 return (STATUS_SYNTAX);
1316         }
1317         return (update_addordelete(cmdline, isdelete));
1318 }
1319
1320 static void
1321 show_message(dns_message_t *msg) {
1322         isc_result_t result;
1323         isc_buffer_t *buf = NULL;
1324         int bufsz;
1325
1326         ddebug("show_message()");
1327         bufsz = INITTEXT;
1328         do { 
1329                 if (bufsz > MAXTEXT) {
1330                         fprintf(stderr, "could not allocate large enough "
1331                                 "buffer to display message\n");
1332                         exit(1);
1333                 }
1334                 if (buf != NULL)
1335                         isc_buffer_free(&buf);
1336                 result = isc_buffer_allocate(mctx, &buf, bufsz);
1337                 check_result(result, "isc_buffer_allocate");
1338                 result = dns_message_totext(msg, style, 0, buf);
1339                 bufsz *= 2;
1340         } while (result == ISC_R_NOSPACE);
1341         if (result != ISC_R_SUCCESS) {
1342                 fprintf(stderr, "could not convert message to text format.\n");
1343                 isc_buffer_free(&buf);
1344                 return;
1345         }
1346         printf("Outgoing update query:\n%.*s",
1347                (int)isc_buffer_usedlength(buf),
1348                (char*)isc_buffer_base(buf));
1349         isc_buffer_free(&buf);
1350 }
1351
1352
1353 static isc_uint16_t
1354 get_next_command(void) {
1355         char cmdlinebuf[MAXCMD];
1356         char *cmdline;
1357         char *word;
1358
1359         ddebug("get_next_command()");
1360         if (interactive)
1361                 fprintf(stdout, "> ");
1362         isc_app_block();
1363         cmdline = fgets(cmdlinebuf, MAXCMD, input);
1364         isc_app_unblock();
1365         if (cmdline == NULL)
1366                 return (STATUS_QUIT);
1367         word = nsu_strsep(&cmdline, " \t\r\n");
1368
1369         if (feof(input))
1370                 return (STATUS_QUIT);
1371         if (*word == 0)
1372                 return (STATUS_SEND);
1373         if (word[0] == ';')
1374                 return (STATUS_MORE);
1375         if (strcasecmp(word, "quit") == 0)
1376                 return (STATUS_QUIT);
1377         if (strcasecmp(word, "prereq") == 0)
1378                 return (evaluate_prereq(cmdline));
1379         if (strcasecmp(word, "update") == 0)
1380                 return (evaluate_update(cmdline));
1381         if (strcasecmp(word, "server") == 0)
1382                 return (evaluate_server(cmdline));
1383         if (strcasecmp(word, "local") == 0)
1384                 return (evaluate_local(cmdline));
1385         if (strcasecmp(word, "zone") == 0)
1386                 return (evaluate_zone(cmdline));
1387         if (strcasecmp(word, "class") == 0)
1388                 return (evaluate_class(cmdline));
1389         if (strcasecmp(word, "send") == 0)
1390                 return (STATUS_SEND);
1391         if (strcasecmp(word, "show") == 0) {
1392                 show_message(updatemsg);
1393                 return (STATUS_MORE);
1394         }
1395         if (strcasecmp(word, "key") == 0)
1396                 return (evaluate_key(cmdline));
1397         fprintf(stderr, "incorrect section name: %s\n", word);
1398         return (STATUS_SYNTAX);
1399 }
1400
1401 static isc_boolean_t
1402 user_interaction(void) {
1403         isc_uint16_t result = STATUS_MORE;
1404
1405         ddebug("user_interaction()");
1406         while ((result == STATUS_MORE) || (result == STATUS_SYNTAX))
1407                 result = get_next_command();
1408         if (result == STATUS_SEND)
1409                 return (ISC_TRUE);
1410         return (ISC_FALSE);
1411
1412 }
1413
1414 static void
1415 done_update(void) {
1416         isc_event_t *event = global_event;
1417         ddebug("done_update()");
1418         isc_task_send(global_task, &event);
1419 }
1420
1421 static void
1422 check_tsig_error(dns_rdataset_t *rdataset, isc_buffer_t *b) {
1423         isc_result_t result;
1424         dns_rdata_t rdata = DNS_RDATA_INIT;
1425         dns_rdata_any_tsig_t tsig;
1426
1427         result = dns_rdataset_first(rdataset);
1428         check_result(result, "dns_rdataset_first");
1429         dns_rdataset_current(rdataset, &rdata);
1430         result = dns_rdata_tostruct(&rdata, &tsig, NULL);
1431         check_result(result, "dns_rdata_tostruct");
1432         if (tsig.error != 0) {
1433                 if (isc_buffer_remaininglength(b) < 1)
1434                       check_result(ISC_R_NOSPACE, "isc_buffer_remaininglength");
1435                 isc__buffer_putstr(b, "(" /*)*/);
1436                 result = dns_tsigrcode_totext(tsig.error, b);
1437                 check_result(result, "dns_tsigrcode_totext");
1438                 if (isc_buffer_remaininglength(b) < 1)
1439                       check_result(ISC_R_NOSPACE, "isc_buffer_remaininglength");
1440                 isc__buffer_putstr(b,  /*(*/ ")");
1441         }
1442 }
1443
1444 static void
1445 update_completed(isc_task_t *task, isc_event_t *event) {
1446         dns_requestevent_t *reqev = NULL;
1447         isc_result_t result;
1448         dns_message_t *rcvmsg = NULL;
1449         dns_request_t *request;
1450
1451         UNUSED(task);
1452
1453         ddebug("update_completed()");
1454
1455         requests--;
1456
1457         REQUIRE(event->ev_type == DNS_EVENT_REQUESTDONE);
1458         reqev = (dns_requestevent_t *)event;
1459         request = reqev->request;
1460
1461         if (shuttingdown) {
1462                 dns_request_destroy(&request);
1463                 isc_event_free(&event);
1464                 maybeshutdown();
1465                 return;
1466         }
1467
1468         if (reqev->result != ISC_R_SUCCESS) {
1469                 fprintf(stderr, "; Communication with server failed: %s\n",
1470                         isc_result_totext(reqev->result));
1471                 seenerror = ISC_TRUE;
1472                 goto done;
1473         }
1474
1475         result = dns_message_create(mctx, DNS_MESSAGE_INTENTPARSE, &rcvmsg);
1476         check_result(result, "dns_message_create");
1477         result = dns_request_getresponse(request, rcvmsg,
1478                                          DNS_MESSAGEPARSE_PRESERVEORDER);
1479         switch (result) {
1480         case ISC_R_SUCCESS:
1481                 break;
1482         case DNS_R_CLOCKSKEW:
1483         case DNS_R_EXPECTEDTSIG:
1484         case DNS_R_TSIGERRORSET:
1485         case DNS_R_TSIGVERIFYFAILURE:
1486         case DNS_R_UNEXPECTEDTSIG:
1487                 fprintf(stderr, "; TSIG error with server: %s\n",
1488                         isc_result_totext(result));
1489                 seenerror = ISC_TRUE;
1490                 break;
1491         default:
1492                 check_result(result, "dns_request_getresponse");
1493         }
1494
1495         if (rcvmsg->rcode != dns_rcode_noerror) {
1496                 seenerror = ISC_TRUE;
1497                 if (!debugging) {
1498                         char buf[64];
1499                         isc_buffer_t b;
1500                         dns_rdataset_t *rds;
1501                         
1502                         isc_buffer_init(&b, buf, sizeof(buf) - 1);
1503                         result = dns_rcode_totext(rcvmsg->rcode, &b);
1504                         check_result(result, "dns_rcode_totext");
1505                         rds = dns_message_gettsig(rcvmsg, NULL);
1506                         if (rds != NULL)
1507                                 check_tsig_error(rds, &b);
1508                         fprintf(stderr, "update failed: %.*s\n",
1509                                 (int)isc_buffer_usedlength(&b), buf);
1510                 }
1511         }
1512         if (debugging) {
1513                 isc_buffer_t *buf = NULL;
1514                 int bufsz;
1515
1516                 bufsz = INITTEXT;
1517                 do { 
1518                         if (bufsz > MAXTEXT) {
1519                                 fprintf(stderr, "could not allocate large "
1520                                         "enough buffer to display message\n");
1521                                 exit(1);
1522                         }
1523                         if (buf != NULL)
1524                                 isc_buffer_free(&buf);
1525                         result = isc_buffer_allocate(mctx, &buf, bufsz);
1526                         check_result(result, "isc_buffer_allocate");
1527                         result = dns_message_totext(rcvmsg, style, 0, buf);
1528                         bufsz *= 2;
1529                 } while (result == ISC_R_NOSPACE);
1530                 check_result(result, "dns_message_totext");
1531                 fprintf(stderr, "\nReply from update query:\n%.*s\n",
1532                         (int)isc_buffer_usedlength(buf),
1533                         (char*)isc_buffer_base(buf));
1534                 isc_buffer_free(&buf);
1535         }
1536         dns_message_destroy(&rcvmsg);
1537  done:
1538         dns_request_destroy(&request);
1539         isc_event_free(&event);
1540         done_update();
1541 }
1542
1543 static void
1544 send_update(dns_name_t *zonename, isc_sockaddr_t *master,
1545             isc_sockaddr_t *srcaddr)
1546 {
1547         isc_result_t result;
1548         dns_request_t *request = NULL;
1549         dns_name_t *name = NULL;
1550         dns_rdataset_t *rdataset = NULL;
1551         unsigned int options = 0;
1552
1553         ddebug("send_update()");
1554
1555         result = dns_message_gettempname(updatemsg, &name);
1556         check_result(result, "dns_message_gettempname");
1557         dns_name_init(name, NULL);
1558         dns_name_clone(zonename, name);
1559         result = dns_message_gettemprdataset(updatemsg, &rdataset);
1560         check_result(result, "dns_message_gettemprdataset");
1561         dns_rdataset_makequestion(rdataset, getzoneclass(), dns_rdatatype_soa);
1562         ISC_LIST_INIT(name->list);
1563         ISC_LIST_APPEND(name->list, rdataset, link);
1564         dns_message_addname(updatemsg, name, DNS_SECTION_ZONE);
1565
1566         if (usevc)
1567                 options |= DNS_REQUESTOPT_TCP;
1568         result = dns_request_createvia(requestmgr, updatemsg, srcaddr,
1569                                        master, options, key,
1570                                        FIND_TIMEOUT, global_task,
1571                                        update_completed, NULL, &request);
1572         check_result(result, "dns_request_createvia");
1573         requests++;
1574 }
1575
1576 static void
1577 recvsoa(isc_task_t *task, isc_event_t *event) {
1578         dns_requestevent_t *reqev = NULL;
1579         dns_request_t *request = NULL;
1580         isc_result_t result, eresult;
1581         dns_message_t *rcvmsg = NULL;
1582         dns_section_t section;
1583         dns_name_t *name = NULL;
1584         dns_rdataset_t *soaset = NULL;
1585         dns_rdata_soa_t soa;
1586         dns_rdata_t soarr = DNS_RDATA_INIT;
1587         int pass = 0;
1588         dns_name_t master;
1589         isc_sockaddr_t *serveraddr, tempaddr;
1590         dns_name_t *zonename;
1591         nsu_requestinfo_t *reqinfo;
1592         dns_message_t *soaquery = NULL;
1593         isc_sockaddr_t *addr;
1594         isc_boolean_t seencname = ISC_FALSE;
1595
1596         UNUSED(task);
1597
1598         ddebug("recvsoa()");
1599
1600         requests--;
1601         
1602         REQUIRE(event->ev_type == DNS_EVENT_REQUESTDONE);
1603         reqev = (dns_requestevent_t *)event;
1604         request = reqev->request;
1605         eresult = reqev->result;
1606         reqinfo = reqev->ev_arg;
1607         soaquery = reqinfo->msg;
1608         addr = reqinfo->addr;
1609
1610         if (shuttingdown) {
1611                 dns_request_destroy(&request);
1612                 dns_message_destroy(&soaquery);
1613                 isc_mem_put(mctx, reqinfo, sizeof(nsu_requestinfo_t));
1614                 isc_event_free(&event);
1615                 maybeshutdown();
1616                 return;
1617         }
1618
1619         if (eresult != ISC_R_SUCCESS) {
1620                 char addrbuf[ISC_SOCKADDR_FORMATSIZE];
1621
1622                 isc_sockaddr_format(addr, addrbuf, sizeof(addrbuf));
1623                 fprintf(stderr, "; Communication with %s failed: %s\n",
1624                        addrbuf, isc_result_totext(eresult));
1625                 if (userserver != NULL)
1626                         fatal("could not talk to specified name server");
1627                 else if (++ns_inuse >= lwconf->nsnext)
1628                         fatal("could not talk to any default name server");
1629                 ddebug("Destroying request [%p]", request);
1630                 dns_request_destroy(&request);
1631                 dns_message_renderreset(soaquery);
1632                 sendrequest(localaddr, &servers[ns_inuse], soaquery, &request);
1633                 isc_mem_put(mctx, reqinfo, sizeof(nsu_requestinfo_t));
1634                 isc_event_free(&event);
1635                 setzoneclass(dns_rdataclass_none);
1636                 return;
1637         }
1638         isc_mem_put(mctx, reqinfo, sizeof(nsu_requestinfo_t));
1639
1640         isc_event_free(&event);
1641         reqev = NULL;
1642
1643         ddebug("About to create rcvmsg");
1644         result = dns_message_create(mctx, DNS_MESSAGE_INTENTPARSE, &rcvmsg);
1645         check_result(result, "dns_message_create");
1646         result = dns_request_getresponse(request, rcvmsg,
1647                                          DNS_MESSAGEPARSE_PRESERVEORDER);
1648         if (result == DNS_R_TSIGERRORSET && userserver != NULL) {
1649                 dns_message_destroy(&rcvmsg);
1650                 ddebug("Destroying request [%p]", request);
1651                 dns_request_destroy(&request);
1652                 reqinfo = isc_mem_get(mctx, sizeof(nsu_requestinfo_t));
1653                 if (reqinfo == NULL)
1654                         fatal("out of memory");
1655                 reqinfo->msg = soaquery;
1656                 reqinfo->addr = addr;
1657                 dns_message_renderreset(soaquery);
1658                 ddebug("retrying soa request without TSIG");
1659                 result = dns_request_createvia(requestmgr, soaquery,
1660                                                localaddr, addr, 0, NULL,
1661                                                FIND_TIMEOUT, global_task,
1662                                                recvsoa, reqinfo, &request);
1663                 check_result(result, "dns_request_createvia");
1664                 requests++;
1665                 return;
1666         }
1667         check_result(result, "dns_request_getresponse");
1668         section = DNS_SECTION_ANSWER;
1669         if (debugging) {
1670                 isc_buffer_t *buf = NULL;
1671                 int bufsz;
1672                 bufsz = INITTEXT;
1673                 do {
1674                         if (buf != NULL)
1675                                 isc_buffer_free(&buf);
1676                         if (bufsz > MAXTEXT) {
1677                                 fprintf(stderr, "could not allocate enough "
1678                                          "space for debugging message\n");
1679                                 exit(1);
1680                         }
1681                         result = isc_buffer_allocate(mctx, &buf, bufsz);
1682                         check_result(result, "isc_buffer_allocate");
1683                         result = dns_message_totext(rcvmsg, style, 0, buf);
1684                 } while (result == ISC_R_NOSPACE);
1685                 check_result(result, "dns_message_totext");
1686                 fprintf(stderr, "Reply from SOA query:\n%.*s\n",
1687                         (int)isc_buffer_usedlength(buf),
1688                         (char*)isc_buffer_base(buf));
1689                 isc_buffer_free(&buf);
1690         }
1691
1692         if (rcvmsg->rcode != dns_rcode_noerror &&
1693             rcvmsg->rcode != dns_rcode_nxdomain)
1694                 fatal("response to SOA query was unsuccessful");
1695
1696  lookforsoa:
1697         if (pass == 0)
1698                 section = DNS_SECTION_ANSWER;
1699         else if (pass == 1)
1700                 section = DNS_SECTION_AUTHORITY;
1701         else
1702                 fatal("response to SOA query didn't contain an SOA");
1703
1704
1705         result = dns_message_firstname(rcvmsg, section);
1706         if (result != ISC_R_SUCCESS) {
1707                 pass++;
1708                 goto lookforsoa;
1709         }
1710         while (result == ISC_R_SUCCESS) {
1711                 name = NULL;
1712                 dns_message_currentname(rcvmsg, section, &name);
1713                 soaset = NULL;
1714                 result = dns_message_findtype(name, dns_rdatatype_soa, 0,
1715                                               &soaset);
1716                 if (result == ISC_R_SUCCESS)
1717                         break;
1718                 if (section == DNS_SECTION_ANSWER) {
1719                         dns_rdataset_t *tset = NULL;
1720                         if (dns_message_findtype(name, dns_rdatatype_cname, 0,
1721                                                  &tset) == ISC_R_SUCCESS
1722                             ||
1723                             dns_message_findtype(name, dns_rdatatype_dname, 0,
1724                                                  &tset) == ISC_R_SUCCESS
1725                             )
1726                         {
1727                                 seencname = ISC_TRUE;
1728                                 break;
1729                         }
1730                 }
1731                                 
1732                 result = dns_message_nextname(rcvmsg, section);
1733         }
1734
1735         if (soaset == NULL && !seencname) {
1736                 pass++;
1737                 goto lookforsoa;
1738         }
1739
1740         if (seencname) {
1741                 dns_name_t tname;
1742                 unsigned int nlabels;
1743
1744                 result = dns_message_firstname(soaquery, DNS_SECTION_QUESTION);
1745                 INSIST(result == ISC_R_SUCCESS);
1746                 name = NULL;
1747                 dns_message_currentname(soaquery, DNS_SECTION_QUESTION, &name);
1748                 nlabels = dns_name_countlabels(name);
1749                 if (nlabels == 1)
1750                         fatal("could not find enclosing zone");
1751                 dns_name_init(&tname, NULL);
1752                 dns_name_getlabelsequence(name, 1, nlabels - 1, &tname);
1753                 dns_name_clone(&tname, name);
1754                 dns_request_destroy(&request);
1755                 dns_message_renderreset(soaquery);
1756                 if (userserver != NULL)
1757                         sendrequest(localaddr, userserver, soaquery, &request);
1758                 else
1759                         sendrequest(localaddr, &servers[ns_inuse], soaquery,
1760                                     &request);
1761                 goto out;
1762         }
1763
1764         if (debugging) {
1765                 char namestr[DNS_NAME_FORMATSIZE];
1766                 dns_name_format(name, namestr, sizeof(namestr));
1767                 fprintf(stderr, "Found zone name: %s\n", namestr);
1768         }
1769
1770         result = dns_rdataset_first(soaset);
1771         check_result(result, "dns_rdataset_first");
1772
1773         dns_rdata_init(&soarr);
1774         dns_rdataset_current(soaset, &soarr);
1775         result = dns_rdata_tostruct(&soarr, &soa, NULL);
1776         check_result(result, "dns_rdata_tostruct");
1777
1778         dns_name_init(&master, NULL);
1779         dns_name_clone(&soa.origin, &master);
1780
1781         if (userzone != NULL)
1782                 zonename = userzone;
1783         else
1784                 zonename = name;
1785
1786         if (debugging) {
1787                 char namestr[DNS_NAME_FORMATSIZE];
1788                 dns_name_format(&master, namestr, sizeof(namestr));
1789                 fprintf(stderr, "The master is: %s\n", namestr);
1790         }
1791
1792         if (userserver != NULL)
1793                 serveraddr = userserver;
1794         else {
1795                 char serverstr[DNS_NAME_MAXTEXT+1];
1796                 isc_buffer_t buf;
1797
1798                 isc_buffer_init(&buf, serverstr, sizeof(serverstr));
1799                 result = dns_name_totext(&master, ISC_TRUE, &buf);
1800                 check_result(result, "dns_name_totext");
1801                 serverstr[isc_buffer_usedlength(&buf)] = 0;
1802                 get_address(serverstr, DNSDEFAULTPORT, &tempaddr);
1803                 serveraddr = &tempaddr;
1804         }
1805
1806         send_update(zonename, serveraddr, localaddr);
1807
1808         dns_message_destroy(&soaquery);
1809         dns_request_destroy(&request);
1810
1811  out:
1812         setzoneclass(dns_rdataclass_none);
1813         dns_rdata_freestruct(&soa);
1814         dns_message_destroy(&rcvmsg);
1815         ddebug("Out of recvsoa");
1816 }
1817
1818 static void
1819 sendrequest(isc_sockaddr_t *srcaddr, isc_sockaddr_t *destaddr,
1820             dns_message_t *msg, dns_request_t **request)
1821 {
1822         isc_result_t result;
1823         nsu_requestinfo_t *reqinfo;
1824
1825         reqinfo = isc_mem_get(mctx, sizeof(nsu_requestinfo_t));
1826         if (reqinfo == NULL)
1827                 fatal("out of memory");
1828         reqinfo->msg = msg;
1829         reqinfo->addr = destaddr;
1830         result = dns_request_createvia(requestmgr, msg, srcaddr, destaddr, 0,
1831                                        (userserver != NULL) ? key : NULL,
1832                                        FIND_TIMEOUT, global_task,
1833                                        recvsoa, reqinfo, request);
1834         check_result(result, "dns_request_createvia");
1835         requests++;
1836 }
1837
1838 static void
1839 start_update(void) {
1840         isc_result_t result;
1841         dns_rdataset_t *rdataset = NULL;
1842         dns_name_t *name = NULL;
1843         dns_request_t *request = NULL;
1844         dns_message_t *soaquery = NULL;
1845         dns_name_t *firstname;
1846         dns_section_t section = DNS_SECTION_UPDATE;
1847
1848         ddebug("start_update()");
1849
1850         result = dns_message_firstname(updatemsg, section);
1851         if (result == ISC_R_NOMORE) {
1852                 section = DNS_SECTION_PREREQUISITE;
1853                 result = dns_message_firstname(updatemsg, section);
1854         }
1855         if (result != ISC_R_SUCCESS) {
1856                 done_update();
1857                 return;
1858         }
1859
1860         if (userzone != NULL && userserver != NULL) {
1861                 send_update(userzone, userserver, localaddr);
1862                 setzoneclass(dns_rdataclass_none);
1863                 return;
1864         }
1865
1866         result = dns_message_create(mctx, DNS_MESSAGE_INTENTRENDER,
1867                                     &soaquery);
1868         check_result(result, "dns_message_create");
1869
1870         soaquery->flags |= DNS_MESSAGEFLAG_RD;
1871
1872         result = dns_message_gettempname(soaquery, &name);
1873         check_result(result, "dns_message_gettempname");
1874
1875         result = dns_message_gettemprdataset(soaquery, &rdataset);
1876         check_result(result, "dns_message_gettemprdataset");
1877
1878         dns_rdataset_makequestion(rdataset, getzoneclass(), dns_rdatatype_soa);
1879
1880         firstname = NULL;
1881         dns_message_currentname(updatemsg, section, &firstname);
1882         dns_name_init(name, NULL);
1883         dns_name_clone(firstname, name);
1884
1885         ISC_LIST_INIT(name->list);
1886         ISC_LIST_APPEND(name->list, rdataset, link);
1887         dns_message_addname(soaquery, name, DNS_SECTION_QUESTION);
1888
1889         if (userserver != NULL)
1890                 sendrequest(localaddr, userserver, soaquery, &request);
1891         else {
1892                 ns_inuse = 0;
1893                 sendrequest(localaddr, &servers[ns_inuse], soaquery, &request);
1894         }
1895 }
1896
1897 static void
1898 cleanup(void) {
1899         ddebug("cleanup()");
1900
1901         ddebug("Shutting down task manager");
1902         isc_taskmgr_destroy(&taskmgr);
1903
1904         ddebug("Destroying event");
1905         isc_event_free(&global_event);
1906
1907         ddebug("Shutting down socket manager");
1908         isc_socketmgr_destroy(&socketmgr);
1909
1910         ddebug("Shutting down timer manager");
1911         isc_timermgr_destroy(&timermgr);
1912
1913         ddebug("Destroying hash context");
1914         isc_hash_destroy();
1915
1916         ddebug("Destroying memory context");
1917         if (memdebugging)
1918                 isc_mem_stats(mctx, stderr);
1919         isc_mem_destroy(&mctx);
1920 }
1921
1922 static void
1923 getinput(isc_task_t *task, isc_event_t *event) {
1924         isc_boolean_t more;
1925
1926         UNUSED(task);
1927
1928         if (shuttingdown) {
1929                 maybeshutdown();
1930                 return;
1931         }
1932
1933         if (global_event == NULL)
1934                 global_event = event;
1935
1936         reset_system();
1937         more = user_interaction();
1938         if (!more) {
1939                 isc_app_shutdown();
1940                 return;
1941         }
1942         start_update();
1943         return;
1944 }
1945
1946 int
1947 main(int argc, char **argv) {
1948         isc_result_t result;
1949         style = &dns_master_style_debug;
1950
1951         input = stdin;
1952
1953         interactive = ISC_TF(isatty(0));
1954
1955         isc_app_start();
1956
1957         parse_args(argc, argv);
1958
1959         setup_system();
1960
1961         result = isc_app_onrun(mctx, global_task, getinput, NULL);
1962         check_result(result, "isc_app_onrun");
1963
1964         (void)isc_app_run();
1965
1966         cleanup();
1967
1968         isc_app_finish();
1969
1970         if (seenerror)
1971                 return (2);
1972         else
1973                 return (0);
1974 }