summaryrefslogtreecommitdiffstats
path: root/usr.sbin/named/dnsquery/dnsquery.c
blob: 7a61479940a3a96913689aee327e95a1a8348741 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*	$OpenBSD: dnsquery.c,v 1.4 1999/12/04 00:22:34 deraadt Exp $	*/

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/nameser.h>
#include <netdb.h>
#include <resolv.h>
#include <errno.h>

#include "../conf/portability.h"

extern int errno;
extern int h_errno;
extern char *h_errlist[];

main(argc, argv)
int argc;
char *argv[];
{
	char name[MAXDNAME];
	u_char answer[8*1024];
	register int c, i = 0;
	unsigned long ul;
	int nameservers = 0, class, type, len;
	struct in_addr q_nsaddr[MAXNS];
	struct hostent *q_nsname;
	extern int optind, opterr;
	extern char *optarg;
	HEADER *hp;
	int stream = 0, debug = 0;

	/* set defaults */
	len = MAXDNAME;
	gethostname(name, len);
	class = C_IN;
	type = T_ANY;

	/* if no args, exit */
	if (argc == 1) {
		fprintf(stderr, "Usage:  %s [-h] host [-n ns] [-t type] [-c class] [-r retry] [-p period] [-s] [-v] [-d] [-a]\n", argv[0]);
		exit(-1);
	}

	/* handle args */
	while ((c = getopt(argc, argv, "c:dh:n:p:r:st:u:v")) != EOF) {
		switch (c) {

		case 'r' :	_res.retry = atoi(optarg);
				break;

		case 'p' :	_res.retrans = atoi(optarg);
				break;

		case 'h' :	if (strlen(optarg) > sizeof(name)-1) {
				    fprintf(stderr,
				    "Domain name too long (%s)\n", optarg);
					exit(-1);
				}
				strlcpy(name, optarg, sizeof name);
				break;

		case 'c' : {
				int success, proto_class;

				proto_class = sym_ston(__p_class_syms,
						       optarg, &success);
				if (success)
					class = proto_class;
				else {
				    fprintf(stderr, "Bad class (%s)\n", optarg);
					exit(-1);
				}
			    }
				break;

		case 't' : {
				int success, proto_type;

				proto_type = sym_ston(__p_type_syms,
						      optarg, &success);
				if (success)
					type = proto_type;
				else {
				    fprintf(stderr, "Bad type (%s)\n", optarg);
					exit(-1);
				}
			    }
				break;

		case 'd' :	debug++;
				break;

		case 's' :	
		case 'v' :	stream++;
				break;

		case 'n' :	
				/*
				 *  If we set some nameservers here without
				 *  using gethostbyname() first, then they will
				 *  get overwritten when we do the first query.
				 *  So, we must init the resolver before any 
				 *  of this.
				 */
				if (!(_res.options & RES_INIT))
					if (res_init() == -1) {
						fprintf(stderr,
							"res_init() failed\n");
						exit(-1);
				}
				if (nameservers >= MAXNS) break;
				(void) inet_aton(optarg,
						 &q_nsaddr[nameservers]);
				if (!inet_aton(optarg, &ul)) {
					q_nsname = gethostbyname(optarg);
					if (q_nsname == 0) {
						fprintf(stderr,
						       "Bad nameserver (%s)\n",
							optarg);
						exit(-1);
					}
					bcopy((char *) q_nsname->h_addr,
					      (char *) &q_nsaddr[nameservers],
					      INADDRSZ);
				}
				else
					q_nsaddr[nameservers].s_addr = ul;
				nameservers++;
				break;

		default : 	fprintf(stderr, 
				"\tUsage:  %s [-n ns] [-h host] [-t type] [-c class] [-r retry] [-p period] [-s] [-v] [-d] [-a]\n", argv[0]);
				exit(-1);
		}
	}
	if (optind < argc) {
		if (strlen(argv[optind]) > sizeof(name)-1) {
		    fprintf(stderr,
		    "Domain name too long (%s)\n", argv[optind]);
			exit(-1);
		}
		strlcpy(name, argv[optind], sizeof name);
	}

	len = sizeof(answer);

	/* 
	 * set these here so they aren't set for a possible call to
	 * gethostbyname above
	*/
	if (debug || stream) {
		if (!(_res.options & RES_INIT))
			if (res_init() == -1) {
				fprintf(stderr, "res_init() failed\n");
				exit(-1);
			}
		if (debug) 
			_res.options |= RES_DEBUG;
		if (stream)
		 	_res.options |= RES_USEVC;
	}

	/* if the -n flag was used, add them to the resolver's list */
	if (nameservers != 0) {
		_res.nscount = nameservers;
		for (i = nameservers - 1; i >= 0; i--) {
			_res.nsaddr_list[i].sin_addr.s_addr = q_nsaddr[i].s_addr;
			_res.nsaddr_list[i].sin_family = AF_INET;
			_res.nsaddr_list[i].sin_port = htons(NAMESERVER_PORT);
		}
	}

	/*
	 * if the -h arg is fully-qualified, use res_query() since
	 * using res_search() will lead to use of res_querydomain()
	 * which will strip the trailing dot
	 */
	if (name[strlen(name) - 1] == '.') {
		if (res_query(name, class, type, answer, len) < 0) {
			hp = (HEADER *) answer;
			if ((hp->rcode == 0) && (hp->ancount > 0))
				__p_query(answer);
			else
				fprintf(stderr, "Query failed (h_errno = %d) : %s\n", 
						h_errno, h_errlist[h_errno]);
			exit(-1);
		}
	}
	else if (res_search(name, class, type, answer, len) < 0) {
		hp = (HEADER *) answer;
		if ((hp->rcode == 0) && (hp->ancount > 0))
			__p_query(answer);
		else
			fprintf(stderr, "Query failed (h_errno = %d) : %s\n", 
						h_errno, h_errlist[h_errno]);
		exit(-1);
	}
	__p_query(answer);
	exit(0);
}