dig.c revision 5eb91bd90e3ad3426e5e3213031556a737cf3809
/*
* Copyright (C) 2000, 2001 Internet Software Consortium.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
* DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
* INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
* FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
* NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
* WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/* $Id: dig.c,v 1.182 2003/07/25 00:01:03 marka Exp $ */
#include <config.h>
#include <stdlib.h>
#include <time.h>
#include <ctype.h>
#include <isc/parseint.h>
#include <dns/fixedname.h>
#include <dns/masterdump.h>
#include <dns/rdataset.h>
#include <dns/rdatatype.h>
#include <dns/rdataclass.h>
extern dig_serverlist_t server_list;
#define ADD_STRING(b, s) { \
if (strlen(s) >= isc_buffer_availablelength(b)) \
return (ISC_R_NOSPACE); \
else \
isc_buffer_putstr(b, s); \
}
extern unsigned int timeout;
extern dns_messageid_t id;
extern int sendcount;
extern int ndots;
extern int lookup_counter;
extern int exitcode;
extern isc_sockaddr_t bind_address;
extern char keynametext[MXNAME];
extern dns_tsigkey_t *key;
extern isc_boolean_t validated;
extern isc_taskmgr_t *taskmgr;
extern isc_task_t *global_task;
extern isc_boolean_t free_now;
static char *argv0;
static char domainopt[DNS_NAME_MAXTEXT];
static const char *opcodetext[] = {
"QUERY",
"IQUERY",
"STATUS",
"RESERVED3",
"NOTIFY",
"UPDATE",
"RESERVED6",
"RESERVED7",
"RESERVED8",
"RESERVED9",
"RESERVED10",
"RESERVED11",
"RESERVED12",
"RESERVED13",
"RESERVED14",
"RESERVED15"
};
static const char *rcodetext[] = {
"NOERROR",
"FORMERR",
"SERVFAIL",
"NXDOMAIN",
"NOTIMP",
"REFUSED",
"YXDOMAIN",
"YXRRSET",
"NXRRSET",
"NOTAUTH",
"NOTZONE",
"RESERVED11",
"RESERVED12",
"RESERVED13",
"RESERVED14",
"RESERVED15",
"BADVERS"
};
extern char *progname;
static void
"Usage: dig [@global-server] [domain] [q-type] [q-class] {q-opt}\n"
" {global-d-opt} host [@local-server] {local-d-opt}\n"
" [ host [@local-server] {local-d-opt} [...]]\n", fp);
}
static void
usage(void) {
fputs("\nUse \"dig -h\" (or \"dig -h | more\") "
"for complete list of options\n", stderr);
exit(1);
}
static void
version(void) {
}
static void
help(void) {
"Where: domain are in the Domain Name System\n"
" q-class is one of (in,hs,ch,...) [default: in]\n"
" q-type is one of (a,any,mx,ns,soa,hinfo,axfr,txt,...) [default:a]\n"
" (Use ixfr=version for type ixfr)\n"
" q-opt is one of:\n"
" -x dot-notation (shortcut for in-addr lookups)\n"
" -i (IP6.INT reverse IPv6 lookups)\n"
" -f filename (batch mode)\n"
" -b address (bind to source address)\n"
" -p port (specify port number)\n"
" -t type (specify query type)\n"
" -c class (specify query class)\n"
" -k keyfile (specify tsig key file)\n"
" -y name:key (specify named base64 tsig key)\n"
" d-opt is of the form +keyword[=value], where keyword is:\n"
" +[no]vc (TCP mode)\n"
" +[no]tcp (TCP mode, alternate syntax)\n"
" +time=### (Set query timeout) [5]\n"
" +tries=### (Set number of UDP attempts) [3]\n"
" +retry=### (Set number of UDP retries) [2]\n"
" +domain=### (Set default domainname)\n"
" +bufsize=### (Set EDNS0 Max UDP packet size)\n"
" +ndots=### (Set NDOTS value)\n"
" +[no]search (Set whether to use searchlist)\n"
" +[no]defname (Ditto)\n"
" +[no]recurse (Recursive mode)\n"
" +[no]ignore (Don't revert to TCP for TC responses.)"
"\n"
" +[no]fail (Don't try next server on SERVFAIL)\n"
" +[no]besteffort (Try to parse even illegal messages)\n"
" +[no]aaonly (Set AA flag in query)\n"
" +[no]adflag (Set AD flag in query)\n"
" +[no]cdflag (Set CD flag in query)\n"
" +[no]cl (Control display of class in records)\n"
" +[no]cmd (Control display of command line)\n"
" +[no]comments (Control display of comment lines)\n"
" +[no]question (Control display of question)\n"
" +[no]answer (Control display of answer)\n"
" +[no]authority (Control display of authority)\n"
" +[no]additional (Control display of additional)\n"
" +[no]stats (Control display of statistics)\n"
" +[no]short (Disable everything except short\n"
" form of answer)\n"
" +[no]ttlid (Control display of ttls in records)\n"
" +[no]all (Set or clear all display flags)\n"
" +[no]qr (Print question before sending)\n"
" +[no]nssearch (Search all authoritative nameservers)\n"
" +[no]identify (ID responders in short answers)\n"
" +[no]trace (Trace delegation down from root)\n"
" +[no]dnssec (Request DNSSEC records)\n"
" +[no]multiline (Print records in an expanded format)\n"
" global d-opts and servers (before host name) affect all queries.\n"
" local d-opts and servers (after host name) affect only that lookup.\n"
" -h (print help and exit)\n"
" -v (print version and exit)\n",
stdout);
}
/*
* Callback from dighost.c to print the received message.
*/
void
char fromtext[ISC_SOCKADDR_FORMATSIZE];
printf(";; XFR size: %u records (messages %u)\n",
} else {
}
if (!validated)
puts(";; WARNING -- Some TSIG could not "
"be validated");
}
puts(";; WARNING -- TSIG key was not used.");
}
puts("");
printf(";; Received %u bytes from %s(%s) in %d ms\n\n",
(int)diff/1000);
}
}
/*
* Callback from dighost.c to print that it is trying a server.
* Not used in dig.
* XXX print_trying
*/
void
}
/*
* Internal print routine used to print short form replies.
*/
static isc_result_t
char store[sizeof("12345678901234567890")];
if (result != ISC_R_SUCCESS)
return (result);
}
}
return (ISC_R_SUCCESS);
}
/*
* short_form message print handler. Calls above say_message()
*/
static isc_result_t
{
char t[4096];
if (result == ISC_R_NOMORE)
return (ISC_R_SUCCESS);
else if (result != ISC_R_SUCCESS)
return (result);
for (;;) {
isc_buffer_init(&target, t, sizeof(t));
while (loopresult == ISC_R_SUCCESS) {
buf);
}
}
if (result == ISC_R_NOMORE)
break;
else if (result != ISC_R_SUCCESS)
return (result);
}
return (ISC_R_SUCCESS);
}
/*
* Callback from dighost.c to print the reply from a server
*/
unsigned int styleflags = 0;
if (nottl)
if (noclass)
if (multiline) {
}
else
if (!short_form)
}
flags = 0;
if (!headers) {
}
printf(";; Sending:\n");
else
printf(";; Got answer:\n");
if (headers) {
printf(";; ->>HEADER<<- opcode: %s, status: %s, "
"id: %u\n",
printf(";; flags:");
printf(" qr");
printf(" aa");
printf(" tc");
printf(" rd");
printf(" ra");
printf(" ad");
printf(" cd");
printf("; QUERY: %u, ANSWER: %u, "
"AUTHORITY: %u, ADDITIONAL: %u\n",
}
}
{
if (result == ISC_R_NOSPACE) {
if (result == ISC_R_SUCCESS)
goto repopulate_buffer;
else
goto cleanup;
}
"dns_message_pseudosectiontotext");
}
if (!short_form) {
if (result == ISC_R_NOSPACE)
goto buftoosmall;
}
}
if (!short_form) {
if (result == ISC_R_NOSPACE)
goto buftoosmall;
} else {
if (result == ISC_R_NOSPACE)
goto buftoosmall;
}
}
if (!short_form) {
if (result == ISC_R_NOSPACE)
goto buftoosmall;
}
}
if (!short_form) {
if (result == ISC_R_NOSPACE)
goto buftoosmall;
/*
* Only print the signature on the first record.
*/
if (headers) {
msg,
if (result == ISC_R_NOSPACE)
goto buftoosmall;
"dns_message_pseudosectiontotext");
msg,
if (result == ISC_R_NOSPACE)
goto buftoosmall;
"dns_message_pseudosectiontotext");
}
}
}
printf("\n");
(char *)isc_buffer_base(buf));
return (result);
}
/*
* print the greeting message when the program first starts up.
*/
static void
int i;
int remaining;
if (printcmd) {
i = 1;
while (i < argc) {
}
if (first) {
";; global options: %s %s\n",
}
}
}
/*
* Reorder an argument list so that server names all come at the end.
* This is a bit of a hack, to allow batch-mode processing to properly
* handle the server options.
*/
static void
int i, j;
char *ptr;
int end;
debug("reorder_args()");
end--;
if (end == 0)
return;
}
if (argv[i][0] == '@') {
for (j = i + 1; j < end; j++) {
}
end--;
if (end < 1)
return;
}
}
}
static isc_uint32_t
if (result != ISC_R_SUCCESS)
return (tmp);
}
/*
* We're not using isc_commandline_parse() here since the command line
* syntax of dig is quite a bit different from that which can be described
* by that routine.
* XXX doc options
*/
static void
{
char option_store[256];
ptr = option_store;
return;
}
cmd += 2;
}
#define FULLCHECK(A) \
do { \
goto invalid_option; \
} while (0)
#define FULLCHECK2(A, B) \
do { \
goto invalid_option; \
} while (0)
switch (cmd[0]) {
case 'a':
switch (cmd[1]) {
case 'a': /* aaflag */
FULLCHECK("aaflag");
break;
case 'd':
switch (cmd[2]) {
case 'd': /* additional */
FULLCHECK("additional");
break;
case 'f': /* adflag */
FULLCHECK("adflag");
break;
default:
goto invalid_option;
}
break;
case 'l': /* all */
FULLCHECK("all");
break;
case 'n': /* answer */
FULLCHECK("answer");
break;
case 'u': /* authority */
FULLCHECK("authority");
break;
default:
goto invalid_option;
}
break;
case 'b':
switch (cmd[1]) {
case 'e':/* besteffort */
FULLCHECK("besteffort");
break;
case 'u':/* bufsize */
FULLCHECK("bufsize");
goto need_value;
if (!state)
goto invalid_option;
"buffer size", COMMSIZE);
break;
default:
goto invalid_option;
}
break;
case 'c':
switch (cmd[1]) {
case 'd':/* cdflag */
FULLCHECK("cdflag");
break;
case 'l': /* cl */
FULLCHECK("cl");
break;
case 'm': /* cmd */
FULLCHECK("cmd");
break;
case 'o': /* comments */
FULLCHECK("comments");
if (lookup == default_lookup)
break;
default:
goto invalid_option;
}
break;
case 'd':
switch (cmd[1]) {
case 'e': /* defname */
FULLCHECK("defname");
break;
case 'n': /* dnssec */
FULLCHECK("dnssec");
break;
case 'o': /* domain */
FULLCHECK("domain");
goto need_value;
if (!state)
goto invalid_option;
break;
default:
goto invalid_option;
}
break;
case 'f': /* fail */
FULLCHECK("fail");
break;
case 'i':
switch (cmd[1]) {
case 'd': /* identify */
FULLCHECK("identify");
break;
case 'g': /* ignore */
default: /* Inherets default for compatibility */
FULLCHECK("ignore");
}
break;
case 'm': /* multiline */
FULLCHECK("multiline");
break;
case 'n':
switch (cmd[1]) {
case 'd': /* ndots */
FULLCHECK("ndots");
goto need_value;
if (!state)
goto invalid_option;
break;
case 's': /* nssearch */
FULLCHECK("nssearch");
if (state) {
}
break;
default:
goto invalid_option;
}
break;
case 'q':
switch (cmd[1]) {
case 'r': /* qr */
FULLCHECK("qr");
break;
case 'u': /* question */
FULLCHECK("question");
if (lookup == default_lookup)
break;
default:
goto invalid_option;
}
break;
case 'r':
switch (cmd[1]) {
case 'e':
switch (cmd[2]) {
case 'c': /* recurse */
FULLCHECK("recurse");
break;
case 't': /* retry / retries */
goto need_value;
if (!state)
goto invalid_option;
MAXTRIES - 1);
break;
default:
goto invalid_option;
}
break;
default:
goto invalid_option;
}
break;
case 's':
switch (cmd[1]) {
case 'e': /* search */
FULLCHECK("search");
break;
case 'h': /* short */
FULLCHECK("short");
short_form = state;
if (state) {
}
break;
case 't': /* stats */
FULLCHECK("stats");
break;
default:
goto invalid_option;
}
break;
case 't':
switch (cmd[1]) {
case 'c': /* tcp */
FULLCHECK("tcp");
if (!is_batchfile)
break;
case 'i': /* timeout */
FULLCHECK("timeout");
goto need_value;
if (!state)
goto invalid_option;
if (timeout == 0)
timeout = 1;
break;
case 'r':
switch (cmd[2]) {
case 'a': /* trace */
FULLCHECK("trace");
if (state) {
}
break;
case 'i': /* tries */
FULLCHECK("tries");
goto need_value;
if (!state)
goto invalid_option;
MAXTRIES);
break;
default:
goto invalid_option;
}
break;
case 't': /* ttlid */
FULLCHECK("ttlid");
break;
default:
goto invalid_option;
}
break;
case 'v':
FULLCHECK("vc");
if (!is_batchfile)
break;
default:
option);
usage();
}
return;
}
/*
* ISC_TRUE returned if value was used
*/
static isc_boolean_t
{
char *hash;
} else {
}
switch (cmd) {
case 'd':
return (ISC_FALSE);
case 'h':
help();
exit(0);
break;
case 'i':
return (ISC_FALSE);
case 'm': /* memdebug */
/* memdebug is handled in preparse_args() */
return (ISC_FALSE);
case 'n':
/* deprecated */
return (ISC_FALSE);
case 'v':
version();
exit(0);
break;
}
goto invalid_option;
switch (cmd) {
case 'b':
"port number", MAXPORT);
*hash = '\0';
} else
srcport = 0;
else {
*hash = '#';
}
*hash = '#';
return (value_from_next);
case 'c':
if ((*lookup)->rdclassset) {
}
(isc_textregion_t *)&tr);
if (result == ISC_R_SUCCESS) {
} else
"invalid class %s\n",
value);
return (value_from_next);
case 'f':
return (value_from_next);
case 'k':
return (value_from_next);
case 'p':
return (value_from_next);
case 't':
} else {
(isc_textregion_t *)&tr);
if (result == ISC_R_SUCCESS &&
{
}
}
if (result == ISC_R_SUCCESS) {
"extra type option\n");
}
if (rdtype == dns_rdatatype_ixfr) {
(*lookup)->ixfr_serial =
} else {
if (rdtype == dns_rdatatype_axfr) {
}
}
} else
"invalid type %s\n",
value);
return (value_from_next);
case 'y':
usage();
}
usage();
return (value_from_next);
case 'x':
== ISC_R_SUCCESS)
{
(*lookup)->ns_search_only);
if (!(*lookup)->rdclassset)
{
}
} else {
exit(1);
}
return (value_from_next);
default:
usage();
}
return (ISC_FALSE);
}
/*
* Because we may be trying to do memory allocation recording, we're going
* to need to parse the arguments for the -m *before* we start the main
* argument parsing routine.
* I'd prefer not to have to do this, but I am not quite sure how else to
* fix the problem. Argument parsing in dig involves memory allocation
* by its nature, so it can't be done in the main argument parser.
*/
static void
int rc;
char **rv;
return;
}
}
}
static void
int bargc;
char *bargv[64];
int rc;
char **rv;
#ifndef NOPOSIX
char *homedir;
char rcfile[256];
#endif
char *input;
/*
* The semantics for parsing the args is a bit complex; if
* we don't have a host yet, make the arg apply globally,
* otherwise make it apply to the latest host. This is
* a bit different than the previous versions, but should
* form a consistent user interface.
*
* First, create a "default lookup" which won't actually be used
* anywhere, except for cloning into new lookups
*/
debug("parse_args()");
if (!is_batchfile) {
debug("making new lookup");
#ifndef NOPOSIX
/*
* Treat ${HOME}/.digrc as a special batchfile
*/
unsigned int n;
homedir);
if (n < sizeof(rcfile))
}
batchfp) != 0) {
bargc = 1;
(bargc < 62)) {
bargc++;
}
(char **)bargv);
}
}
#endif
}
break;
} else if (rv[0][0] == '+') {
lookup);
} else if (rv[0][0] == '-') {
if (rc <= 1) {
rc--;
rv++;
}
} else {
rc--;
rv++;
}
}
} else {
/*
* Anything which isn't an option
*/
if (open_type_class) {
} else {
(isc_textregion_t *)&tr);
if (result == ISC_R_SUCCESS &&
{
"ixfr requires a "
"serial number\n");
continue;
}
}
if (result == ISC_R_SUCCESS) {
"extra type option\n");
}
if (rdtype == dns_rdatatype_ixfr) {
"serial number",
} else {
if (rdtype ==
}
}
continue;
}
(isc_textregion_t *)&tr);
if (result == ISC_R_SUCCESS) {
if (lookup->rdclassset) {
"extra class option\n");
}
continue;
}
}
if (!config_only) {
ISC_TRUE);
if (firstarg) {
}
}
/* XXX Error message */
}
}
/*
* If we have a batchfile, seed the lookup list with the
* first entry, then trust the callback in dighost_shutdown
* to get the rest
*/
else
if (exitcode < 8)
exitcode = 8;
fatal("couldn't open specified batch file");
}
/* XXX Remove code dup from shutdown code */
bargc = 1;
goto next_line;
bargc++;
}
}
}
/*
* If no lookup specified, search for root
*/
if (firstarg) {
}
}
}
/*
* Callback from dighost.c to allow program-specific shutdown code. Here,
* Here, we're possibly reading from a batch file, then shutting down for
* real if there's nothing in the batch file to read.
*/
void
dighost_shutdown(void) {
int bargc;
char *bargv[16];
char *input;
return;
}
return;
}
bargc = 1;
bargc++;
}
start_lookup();
} else {
return;
}
}
int
dig_server_t *s, *s2;
debug("main()");
result = isc_app_start();
setup_libs();
setup_system();
if (domainopt[0] != '\0') {
}
isc_app_run();
while (s != NULL) {
debug("freeing server %p belonging to %p",
s, default_lookup);
s2 = s;
s = ISC_LIST_NEXT(s, link);
}
}
cancel_all();
destroy_libs();
return (exitcode);
}