Blame
Date:
Thu Nov 19 09:25:28 2020 UTC
Message:
update README before 1.5.0 release
001
2020-04-10
pjp
/*
002
2020-04-10
pjp
* Copyright (c) 2020 Peter J. Philipp
003
2020-04-10
pjp
* All rights reserved.
004
2020-04-10
pjp
*
005
2020-04-10
pjp
* Redistribution and use in source and binary forms, with or without
006
2020-04-10
pjp
* modification, are permitted provided that the following conditions
007
2020-04-10
pjp
* are met:
008
2020-04-10
pjp
* 1. Redistributions of source code must retain the above copyright
009
2020-04-10
pjp
* notice, this list of conditions and the following disclaimer.
010
2020-04-10
pjp
* 2. Redistributions in binary form must reproduce the above copyright
011
2020-04-10
pjp
* notice, this list of conditions and the following disclaimer in the
012
2020-04-10
pjp
* documentation and/or other materials provided with the distribution.
013
2020-04-10
pjp
* 3. The name of the author may not be used to endorse or promote products
014
2020-04-10
pjp
* derived from this software without specific prior written permission
015
2020-04-10
pjp
*
016
2020-04-10
pjp
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
017
2020-04-10
pjp
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
018
2020-04-10
pjp
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
019
2020-04-10
pjp
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
020
2020-04-10
pjp
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
021
2020-04-10
pjp
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
022
2020-04-10
pjp
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
023
2020-04-10
pjp
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
024
2020-04-10
pjp
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
025
2020-04-10
pjp
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
026
2020-04-10
pjp
*
027
2020-04-10
pjp
*/
028
2020-04-10
pjp
029
2020-04-10
pjp
/*
030
2020-09-24
pjp
* $Id: query.c,v 1.14 2020/09/24 05:15:23 pjp Exp $
031
2020-04-10
pjp
*/
032
2020-04-10
pjp
033
2020-04-10
pjp
#include <sys/types.h>
034
2020-04-10
pjp
#include <sys/socket.h>
035
2020-04-10
pjp
#include <sys/stat.h>
036
2020-04-11
pjp
#include <sys/time.h>
037
2020-04-10
pjp
038
2020-04-10
pjp
#include <netinet/in.h>
039
2020-04-10
pjp
#include <arpa/inet.h>
040
2020-04-10
pjp
#include <netdb.h>
041
2020-04-10
pjp
042
2020-04-10
pjp
#include <stdio.h>
043
2020-04-10
pjp
#include <stdlib.h>
044
2020-04-10
pjp
#include <stdint.h>
045
2020-04-10
pjp
#include <stdarg.h>
046
2020-04-10
pjp
#include <string.h>
047
2020-04-10
pjp
#include <unistd.h>
048
2020-04-10
pjp
049
2020-04-10
pjp
#ifdef __linux__
050
2020-04-10
pjp
#include <grp.h>
051
2020-04-10
pjp
#define __USE_BSD 1
052
2020-04-10
pjp
#include <endian.h>
053
2020-04-10
pjp
#include <bsd/stdlib.h>
054
2020-04-10
pjp
#include <bsd/string.h>
055
2020-04-10
pjp
#include <bsd/unistd.h>
056
2020-04-10
pjp
#include <bsd/sys/queue.h>
057
2020-04-10
pjp
#define __unused
058
2020-04-10
pjp
#include <bsd/sys/tree.h>
059
2020-04-10
pjp
#include <bsd/sys/endian.h>
060
2020-04-10
pjp
#include "imsg.h"
061
2020-04-10
pjp
#else /* not linux */
062
2020-04-10
pjp
#include <sys/queue.h>
063
2020-04-10
pjp
#include <sys/tree.h>
064
2020-04-10
pjp
#ifdef __FreeBSD__
065
2020-04-10
pjp
#include "imsg.h"
066
2020-04-10
pjp
#else
067
2020-04-10
pjp
#include <imsg.h>
068
2020-04-10
pjp
#endif /* __FreeBSD__ */
069
2020-04-10
pjp
#endif /* __linux__ */
070
2020-04-10
pjp
071
2020-04-10
pjp
#ifndef NTOHS
072
2020-04-10
pjp
#include "endian.h"
073
2020-04-10
pjp
#endif
074
2020-04-10
pjp
075
2020-04-10
pjp
#include "ddd-dns.h"
076
2020-04-10
pjp
#include "ddd-db.h"
077
2020-04-10
pjp
#include "ddd-config.h"
078
2020-04-10
pjp
079
2020-04-10
pjp
static struct timeval tv0;
080
2020-04-10
pjp
static time_t current_time;
081
2020-04-11
pjp
u_int16_t port = 53;
082
2020-04-11
pjp
int bytes_received;
083
2020-04-10
pjp
084
2020-04-11
pjp
085
2020-04-10
pjp
/* prototypes */
086
2020-04-10
pjp
087
2020-04-10
pjp
int dig(int argc, char *argv[]);
088
2020-04-10
pjp
int command_socket(char *);
089
2020-04-10
pjp
int connect_server(char *, int, u_int32_t);
090
2020-07-19
pjp
int lookup_name(FILE *, int, char *, u_int16_t, struct soa *, u_int32_t, char *, u_int16_t, int *, int*, uint16_t);
091
2020-04-10
pjp
int notglue(ddDB *, struct rbtree *, char *);
092
2020-04-10
pjp
093
2020-04-10
pjp
094
2020-04-11
pjp
095
2020-04-10
pjp
extern int debug;
096
2020-04-10
pjp
extern int verbose;
097
2020-04-10
pjp
098
2020-04-11
pjp
extern int dnssec;
099
2020-04-11
pjp
extern int tsig;
100
2020-04-10
pjp
101
2020-04-10
pjp
/* externs */
102
2020-04-10
pjp
103
2020-04-11
pjp
extern int insert_axfr(char *, char *);
104
2020-04-11
pjp
extern int insert_filter(char *, char *);
105
2020-07-16
pjp
extern int insert_passlist(char *, char *);
106
2020-06-25
pjp
extern int insert_notifyddd(char *, char *);
107
2020-04-11
pjp
extern int usage(int argc, char *argv[]);
108
2020-04-11
pjp
extern void dolog(int pri, char *fmt, ...);
109
2020-04-11
pjp
110
2020-04-10
pjp
extern uint32_t unpack32(char *);
111
2020-04-10
pjp
extern uint16_t unpack16(char *);
112
2020-04-10
pjp
extern void unpack(char *, char *, int);
113
2020-04-10
pjp
114
2020-04-10
pjp
extern void pack(char *, char *, int);
115
2020-04-10
pjp
extern void pack32(char *, u_int32_t);
116
2020-04-10
pjp
extern void pack16(char *, u_int16_t);
117
2020-04-10
pjp
extern void pack8(char *, u_int8_t);
118
2020-04-10
pjp
extern int fill_dnskey(char *, char *, u_int32_t, u_int16_t, u_int8_t, u_int8_t, char *);
119
2020-04-10
pjp
extern int fill_rrsig(char *, char *, u_int32_t, char *, u_int8_t, u_int8_t, u_int32_t, u_int64_t, u_int64_t, u_int16_t, char *, char *);
120
2020-04-10
pjp
extern int fill_nsec3param(char *, char *, u_int32_t, u_int8_t, u_int8_t, u_int16_t, char *);
121
2020-04-10
pjp
extern int fill_nsec3(char *, char *, u_int32_t, u_int8_t, u_int8_t, u_int16_t, char *, char *, char *);
122
2020-04-10
pjp
extern char * convert_name(char *name, int namelen);
123
2020-04-10
pjp
124
2020-04-10
pjp
extern int mybase64_encode(u_char const *, size_t, char *, size_t);
125
2020-04-10
pjp
extern int mybase64_decode(char const *, u_char *, size_t);
126
2020-04-10
pjp
extern struct rbtree * Lookup_zone(ddDB *, char *, int, int, int);
127
2020-04-10
pjp
extern struct question *build_fake_question(char *, int, u_int16_t, char *, int);
128
2020-04-10
pjp
extern char * dns_label(char *, int *);
129
2020-04-10
pjp
extern int label_count(char *);
130
2020-04-10
pjp
extern char *get_dns_type(int, int);
131
2020-04-10
pjp
extern char * hash_name(char *, int, struct nsec3param *);
132
2020-04-10
pjp
extern char * base32hex_encode(u_char *input, int len);
133
2020-04-10
pjp
extern int init_entlist(ddDB *);
134
2020-04-10
pjp
extern int check_ent(char *, int);
135
2020-04-10
pjp
extern struct question *build_question(char *, int, int, char *);
136
2020-04-10
pjp
struct rrtab *rrlookup(char *);
137
2020-04-10
pjp
138
2020-04-10
pjp
extern struct rbtree * find_rrset(ddDB *db, char *name, int len);
139
2020-04-10
pjp
extern struct rrset * find_rr(struct rbtree *rbt, u_int16_t rrtype);
140
2020-04-10
pjp
extern int add_rr(struct rbtree *rbt, char *name, int len, u_int16_t rrtype, void *rdata);
141
2020-04-10
pjp
extern char * bin2hex(char *, int);
142
2020-04-10
pjp
extern u_int64_t timethuman(time_t);
143
2020-04-10
pjp
extern char * bitmap2human(char *, int);
144
2020-04-10
pjp
145
2020-07-08
pjp
extern int raxfr_a(FILE *, u_char *, u_char *, u_char *, struct soa *, u_int16_t, HMAC_CTX *);
146
2020-07-08
pjp
extern int raxfr_tlsa(FILE *, u_char *, u_char *, u_char *, struct soa *, u_int16_t, HMAC_CTX *);
147
2020-07-08
pjp
extern int raxfr_srv(FILE *, u_char *, u_char *, u_char *, struct soa *, u_int16_t, HMAC_CTX *);
148
2020-07-08
pjp
extern int raxfr_naptr(FILE *, u_char *, u_char *, u_char *, struct soa *, u_int16_t, HMAC_CTX *);
149
2020-07-08
pjp
extern int raxfr_aaaa(FILE *, u_char *, u_char *, u_char *, struct soa *, u_int16_t, HMAC_CTX *);
150
2020-07-08
pjp
extern int raxfr_cname(FILE *, u_char *, u_char *, u_char *, struct soa *, u_int16_t, HMAC_CTX *);
151
2020-07-08
pjp
extern int raxfr_ns(FILE *, u_char *, u_char *, u_char *, struct soa *, u_int16_t, HMAC_CTX *);
152
2020-07-08
pjp
extern int raxfr_ptr(FILE *, u_char *, u_char *, u_char *, struct soa *, u_int16_t, HMAC_CTX *);
153
2020-07-08
pjp
extern int raxfr_mx(FILE *, u_char *, u_char *, u_char *, struct soa *, u_int16_t, HMAC_CTX *);
154
2020-07-08
pjp
extern int raxfr_txt(FILE *, u_char *, u_char *, u_char *, struct soa *, u_int16_t, HMAC_CTX *);
155
2020-07-08
pjp
extern int raxfr_dnskey(FILE *, u_char *, u_char *, u_char *, struct soa *, u_int16_t, HMAC_CTX *);
156
2020-07-08
pjp
extern int raxfr_rrsig(FILE *, u_char *, u_char *, u_char *, struct soa *, u_int16_t, HMAC_CTX *);
157
2020-07-08
pjp
extern int raxfr_nsec3param(FILE *, u_char *, u_char *, u_char *, struct soa *, u_int16_t, HMAC_CTX *);
158
2020-07-08
pjp
extern int raxfr_nsec3(FILE *, u_char *, u_char *, u_char *, struct soa *, u_int16_t, HMAC_CTX *);
159
2020-07-08
pjp
extern int raxfr_ds(FILE *, u_char *, u_char *, u_char *, struct soa *, u_int16_t, HMAC_CTX *);
160
2020-07-08
pjp
extern int raxfr_sshfp(FILE *, u_char *, u_char *, u_char *, struct soa *, u_int16_t, HMAC_CTX *);
161
2020-04-10
pjp
extern u_int16_t raxfr_skip(FILE *, u_char *, u_char *);
162
2020-07-27
pjp
extern int raxfr_soa(FILE *, u_char *, u_char *, u_char *, struct soa *, int, u_int32_t, u_int16_t, HMAC_CTX *, struct soa_constraints *);
163
2020-07-27
pjp
extern int raxfr_peek(FILE *, u_char *, u_char *, u_char *, int *, int, u_int16_t *, u_int32_t, HMAC_CTX *, char *, int, int);
164
2020-04-10
pjp
165
2020-04-10
pjp
extern int memcasecmp(u_char *, u_char *, int);
166
2020-04-10
pjp
extern int tsig_pseudoheader(char *, uint16_t, time_t, HMAC_CTX *);
167
2020-09-24
pjp
extern int lookup_axfr(FILE *, int, char *, struct soa *, u_int32_t, char *, char *, int *, int *, int *, struct soa_constraints *, uint32_t);
168
2020-04-10
pjp
extern int insert_tsig(char *, char *);
169
2020-04-10
pjp
extern int find_tsig_key(char *, int, char *, int);
170
2020-04-10
pjp
extern int insert_tsig_key(char *, int, char *);
171
2020-04-10
pjp
extern int insert_region(char *, char *);
172
2020-04-10
pjp
173
2020-04-10
pjp
174
2020-04-11
pjp
/* this struct must be under externs */
175
2020-04-10
pjp
176
2020-04-10
pjp
static struct raxfr_logic supported[] = {
177
2020-04-10
pjp
{ DNS_TYPE_A, 0, raxfr_a },
178
2020-04-10
pjp
{ DNS_TYPE_NS, 0, raxfr_ns },
179
2020-04-10
pjp
{ DNS_TYPE_MX, 0, raxfr_mx },
180
2020-04-10
pjp
{ DNS_TYPE_PTR, 0, raxfr_ptr },
181
2020-04-10
pjp
{ DNS_TYPE_AAAA, 0, raxfr_aaaa },
182
2020-04-10
pjp
{ DNS_TYPE_CNAME, 0, raxfr_cname },
183
2020-04-10
pjp
{ DNS_TYPE_TXT, 0, raxfr_txt },
184
2020-04-10
pjp
{ DNS_TYPE_DNSKEY, 1, raxfr_dnskey },
185
2020-04-10
pjp
{ DNS_TYPE_RRSIG, 1, raxfr_rrsig },
186
2020-04-10
pjp
{ DNS_TYPE_NSEC3PARAM, 1, raxfr_nsec3param },
187
2020-04-10
pjp
{ DNS_TYPE_NSEC3, 1, raxfr_nsec3 },
188
2020-04-10
pjp
{ DNS_TYPE_DS, 1, raxfr_ds },
189
2020-04-10
pjp
{ DNS_TYPE_SSHFP, 0, raxfr_sshfp },
190
2020-04-10
pjp
{ DNS_TYPE_TLSA, 0, raxfr_tlsa },
191
2020-04-10
pjp
{ DNS_TYPE_SRV, 0, raxfr_srv },
192
2020-04-10
pjp
{ DNS_TYPE_NAPTR, 0, raxfr_naptr },
193
2020-04-10
pjp
{ 0, 0, NULL }
194
2020-04-10
pjp
};
195
2020-04-10
pjp
196
2020-04-10
pjp
197
2020-04-10
pjp
/*
198
2020-04-10
pjp
* DDDCTL QUERY
199
2020-04-10
pjp
*/
200
2020-04-10
pjp
201
2020-04-10
pjp
int
202
2020-04-10
pjp
dig(int argc, char *argv[])
203
2020-04-10
pjp
{
204
2020-04-10
pjp
FILE *f = stdout;
205
2020-04-10
pjp
struct soa mysoa;
206
2020-04-10
pjp
struct stat sb;
207
2020-04-10
pjp
struct rrtab *rt;
208
2020-04-10
pjp
struct timeval tv;
209
2020-04-10
pjp
char *outputfile = NULL;
210
2020-04-10
pjp
char *domainname = NULL;
211
2020-04-10
pjp
char *nameserver = "127.0.0.1";
212
2020-04-10
pjp
char *yopt, *tsigpass = NULL, *tsigkey = NULL;
213
2020-04-10
pjp
u_int32_t format = 0;
214
2020-04-10
pjp
u_int16_t port = 53;
215
2020-07-19
pjp
uint16_t class = DNS_CLASS_IN;
216
2020-04-10
pjp
int ch, so, ms;
217
2020-04-10
pjp
int type = DNS_TYPE_A;
218
2020-04-10
pjp
int segment = 0;
219
2020-04-10
pjp
int answers = 0;
220
2020-04-10
pjp
int additionalcount = 0;
221
2020-07-27
pjp
struct soa_constraints constraints = { 0, 0, 0 };
222
2020-04-10
pjp
223
2020-07-19
pjp
while ((ch = getopt(argc, argv, "c:@:DIP:TZp:Q:y:")) != -1) {
224
2020-04-10
pjp
switch (ch) {
225
2020-07-19
pjp
case 'c':
226
2020-07-19
pjp
class = atoi(optarg);
227
2020-07-19
pjp
break;
228
2020-04-10
pjp
case '@':
229
2020-04-10
pjp
case 'Q':
230
2020-04-10
pjp
nameserver = optarg;
231
2020-04-10
pjp
break;
232
2020-04-10
pjp
case 'D':
233
2020-04-10
pjp
format |= DNSSEC_FORMAT;
234
2020-04-10
pjp
break;
235
2020-04-10
pjp
case 'I':
236
2020-04-10
pjp
format |= INDENT_FORMAT;
237
2020-04-10
pjp
break;
238
2020-04-10
pjp
case 'P':
239
2020-04-10
pjp
port = atoi(optarg);
240
2020-04-10
pjp
break;
241
2020-04-10
pjp
case 'T':
242
2020-04-10
pjp
format |= TCP_FORMAT;
243
2020-04-10
pjp
break;
244
2020-04-10
pjp
case 'Z':
245
2020-04-10
pjp
format |= ZONE_FORMAT;
246
2020-04-10
pjp
break;
247
2020-04-10
pjp
case 'p':
248
2020-04-10
pjp
outputfile = optarg;
249
2020-04-10
pjp
break;
250
2020-04-10
pjp
case 'y':
251
2020-04-10
pjp
yopt = strdup(optarg);
252
2020-04-10
pjp
if (yopt == NULL) {
253
2020-04-10
pjp
perror("strdup");
254
2020-04-10
pjp
exit(1);
255
2020-04-10
pjp
}
256
2020-04-10
pjp
tsigkey = yopt;
257
2020-04-10
pjp
tsigpass = strchr(yopt, ':');
258
2020-04-10
pjp
if (tsigpass == NULL) {
259
2020-04-10
pjp
fprintf(stderr, "must provide keyname:password for option -y\n");
260
2020-04-10
pjp
exit(1);
261
2020-04-10
pjp
}
262
2020-04-10
pjp
*tsigpass = '\0';
263
2020-04-10
pjp
tsigpass++;
264
2020-04-10
pjp
break;
265
2020-04-10
pjp
default:
266
2020-04-10
pjp
usage(argc, argv);
267
2020-04-10
pjp
exit(1);
268
2020-04-10
pjp
}
269
2020-04-10
pjp
}
270
2020-04-10
pjp
271
2020-04-10
pjp
argc -= optind;
272
2020-04-10
pjp
argv += optind;
273
2020-04-10
pjp
274
2020-04-10
pjp
if (outputfile) {
275
2020-04-10
pjp
if (lstat(outputfile, &sb) != -1) {
276
2020-04-10
pjp
fprintf(stderr, "%s exists, not clobbering\n", outputfile);
277
2020-04-10
pjp
exit(1);
278
2020-04-10
pjp
}
279
2020-04-10
pjp
280
2020-04-10
pjp
f = fopen(outputfile, "w");
281
2020-04-10
pjp
if (f == NULL) {
282
2020-04-10
pjp
perror("fopen");
283
2020-04-10
pjp
exit(1);
284
2020-04-10
pjp
}
285
2020-04-10
pjp
286
2020-04-10
pjp
}
287
2020-04-10
pjp
288
2020-04-10
pjp
if ((format & ZONE_FORMAT) && (format & INDENT_FORMAT)) {
289
2020-04-10
pjp
fprintf(stderr, "you may not specify -I and -Z together\n");
290
2020-04-10
pjp
exit(1);
291
2020-04-10
pjp
}
292
2020-04-10
pjp
293
2020-04-10
pjp
if (argc < 1) {
294
2020-04-10
pjp
fprintf(stderr, "lookup what?\n");
295
2020-04-10
pjp
exit(1);
296
2020-04-10
pjp
}
297
2020-04-10
pjp
298
2020-04-10
pjp
if ((rt = rrlookup(argv[0])) != NULL) {
299
2020-04-10
pjp
domainname = argv[1];
300
2020-04-10
pjp
type = rt->type;
301
2020-04-10
pjp
} else {
302
2020-04-10
pjp
if (strcmp(argv[0], "any") == 0) {
303
2020-04-10
pjp
domainname = argv[1];
304
2020-04-10
pjp
type = DNS_TYPE_ANY;
305
2020-04-10
pjp
} else if (strcmp(argv[0], "axfr") == 0) {
306
2020-04-10
pjp
domainname = argv[1];
307
2020-04-10
pjp
type = DNS_TYPE_AXFR;
308
2020-04-10
pjp
format |= TCP_FORMAT;
309
2020-04-10
pjp
} else {
310
2020-04-10
pjp
if (argc == 2) {
311
2020-04-10
pjp
if ((rt = rrlookup(argv[1])) != NULL) {
312
2020-04-10
pjp
domainname = argv[0];
313
2020-04-10
pjp
type = rt->type;
314
2020-04-10
pjp
} else {
315
2020-04-10
pjp
if (strcmp(argv[1], "any") == 0) {
316
2020-04-10
pjp
domainname = argv[0];
317
2020-04-10
pjp
type = DNS_TYPE_ANY;
318
2020-04-10
pjp
} else if (strcmp(argv[1], "axfr") == 0) {
319
2020-04-10
pjp
domainname = argv[0];
320
2020-04-10
pjp
type = DNS_TYPE_AXFR;
321
2020-04-10
pjp
format |= TCP_FORMAT;
322
2020-04-10
pjp
}
323
2020-04-10
pjp
}
324
2020-04-10
pjp
} else {
325
2020-04-10
pjp
domainname = argv[0];
326
2020-04-10
pjp
}
327
2020-04-10
pjp
}
328
2020-04-10
pjp
}
329
2020-04-10
pjp
330
2020-04-10
pjp
gettimeofday(&tv0, NULL);
331
2020-04-10
pjp
current_time = time(NULL);
332
2020-04-10
pjp
333
2020-04-10
pjp
so = connect_server(nameserver, port, format);
334
2020-04-10
pjp
if (so < 0) {
335
2020-04-10
pjp
exit(1);
336
2020-04-10
pjp
}
337
2020-04-10
pjp
338
2020-04-10
pjp
segment = 0;
339
2020-04-10
pjp
answers = 0;
340
2020-04-10
pjp
341
2020-04-10
pjp
if (type == DNS_TYPE_AXFR) {
342
2020-07-26
pjp
if ((format & ZONE_FORMAT) && f != NULL)
343
2020-07-26
pjp
fprintf(f, "zone \"%s\" {\n", domainname);
344
2020-07-26
pjp
345
2020-09-24
pjp
if (lookup_axfr(f, so, domainname, &mysoa, format, tsigkey, tsigpass, &segment, &answers, &additionalcount, &constraints, 0xffffffff) < 0) {
346
2020-04-10
pjp
exit(1);
347
2020-04-10
pjp
}
348
2020-07-26
pjp
349
2020-07-26
pjp
if ((format & ZONE_FORMAT) && f != NULL)
350
2020-07-26
pjp
fprintf(f, "}\n");
351
2020-07-26
pjp
352
2020-04-10
pjp
353
2020-04-10
pjp
} else {
354
2020-07-19
pjp
if (lookup_name(f, so, domainname, type, &mysoa, format, nameserver, port, &answers, &additionalcount, class) < 0) {
355
2020-04-10
pjp
/* XXX maybe a packet dump here? */
356
2020-04-10
pjp
exit(1);
357
2020-04-10
pjp
}
358
2020-04-10
pjp
}
359
2020-04-10
pjp
360
2020-04-10
pjp
close(so);
361
2020-04-10
pjp
gettimeofday(&tv, NULL);
362
2020-04-10
pjp
363
2020-04-10
pjp
ms = 0;
364
2020-04-10
pjp
365
2020-04-10
pjp
if (tv.tv_usec < tv0.tv_usec) {
366
2020-04-10
pjp
tv.tv_sec--;
367
2020-04-10
pjp
ms += (((tv.tv_usec + 1000000) - tv0.tv_usec) / 1000);
368
2020-04-10
pjp
} else
369
2020-04-10
pjp
ms += (((tv.tv_usec) - tv0.tv_usec) / 1000);
370
2020-04-10
pjp
371
2020-04-10
pjp
if (tv.tv_sec - tv0.tv_sec > 0)
372
2020-04-10
pjp
ms += 1000 * (tv.tv_sec - tv0.tv_sec);
373
2020-04-10
pjp
374
2020-04-10
pjp
fprintf(f, ";; QUERY TIME: %d ms\n", ms);
375
2020-04-10
pjp
fprintf(f, ";; SERVER: %s#%u\n", nameserver, port);
376
2020-04-10
pjp
fprintf(f, ";; WHEN: %s", ctime(&current_time));
377
2020-04-10
pjp
if (type == DNS_TYPE_AXFR) {
378
2020-04-10
pjp
if (format & ZONE_FORMAT)
379
2020-04-10
pjp
answers--;
380
2020-04-10
pjp
381
2020-04-10
pjp
if (additionalcount)
382
2020-04-10
pjp
answers -= additionalcount;
383
2020-04-10
pjp
384
2020-04-10
pjp
fprintf(f, ";; XFR size %d records (messages %d, bytes %d)\n",
385
2020-04-10
pjp
answers, segment, bytes_received);
386
2020-04-10
pjp
} else {
387
2020-04-10
pjp
fprintf(f, ";; MSG SIZE rcvd: %d\n", bytes_received);
388
2020-04-10
pjp
}
389
2020-04-10
pjp
390
2020-04-10
pjp
if (f != stdout && f != NULL)
391
2020-04-10
pjp
fclose(f);
392
2020-04-10
pjp
393
2020-04-10
pjp
return 0;
394
2020-04-10
pjp
}
395
2020-04-10
pjp
396
2020-04-10
pjp
int
397
2020-04-10
pjp
connect_server(char *nameserver, int port, u_int32_t format)
398
2020-04-10
pjp
{
399
2020-04-10
pjp
struct sockaddr_in sin;
400
2020-04-10
pjp
int so;
401
2020-04-10
pjp
int window = 32768;
402
2020-04-10
pjp
403
2020-04-10
pjp
if (format & TCP_FORMAT)
404
2020-04-10
pjp
so = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
405
2020-04-10
pjp
else
406
2020-04-10
pjp
so = socket(AF_INET, SOCK_DGRAM, 0);
407
2020-04-10
pjp
408
2020-04-10
pjp
if (so < 0) {
409
2020-04-10
pjp
perror("socket");
410
2020-04-10
pjp
return -1;
411
2020-04-10
pjp
}
412
2020-04-10
pjp
413
2020-04-10
pjp
#ifndef __linux__
414
2020-04-10
pjp
/* biggen the window */
415
2020-04-10
pjp
416
2020-04-10
pjp
while (setsockopt(so, SOL_SOCKET, SO_RCVBUF, &window, sizeof(window)) != -1)
417
2020-04-10
pjp
window <<= 1;
418
2020-04-10
pjp
#endif
419
2020-04-10
pjp
420
2020-04-10
pjp
421
2020-04-10
pjp
memset(&sin, 0, sizeof(sin));
422
2020-04-10
pjp
sin.sin_family = AF_INET;
423
2020-04-10
pjp
sin.sin_port = htons(port);
424
2020-04-10
pjp
sin.sin_addr.s_addr = inet_addr(nameserver);
425
2020-04-10
pjp
426
2020-04-10
pjp
if (connect(so, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
427
2020-04-10
pjp
perror("connect");
428
2020-04-10
pjp
return -1;
429
2020-04-10
pjp
}
430
2020-04-10
pjp
431
2020-04-10
pjp
432
2020-04-10
pjp
return (so);
433
2020-04-10
pjp
}
434
2020-04-10
pjp
435
2020-04-10
pjp
int
436
2020-07-19
pjp
lookup_name(FILE *f, int so, char *zonename, u_int16_t myrrtype, struct soa *mysoa, u_int32_t format, char *nameserver, u_int16_t port, int *answers, int *additionalcount, uint16_t qclass)
437
2020-04-10
pjp
{
438
2020-04-10
pjp
int len, i, tmp32;
439
2020-04-10
pjp
int numansw, numaddi, numauth;
440
2020-04-10
pjp
int printansw = 1, printauth = 1, printaddi = 1;
441
2020-04-10
pjp
int rrtype, soacount = 0;
442
2020-04-10
pjp
u_int16_t rdlen;
443
2020-04-10
pjp
char query[512];
444
2020-04-10
pjp
char *reply;
445
2020-04-10
pjp
struct raxfr_logic *sr;
446
2020-04-10
pjp
struct question *q;
447
2020-04-10
pjp
struct dns_optrr *optrr;
448
2020-04-10
pjp
struct whole_header {
449
2020-04-10
pjp
struct dns_header dh;
450
2020-04-10
pjp
} *wh, *rwh;
451
2020-07-27
pjp
struct soa_constraints constraints = { 60, 60, 60 };
452
2020-04-10
pjp
453
2020-04-10
pjp
u_char *p, *name;
454
2020-04-10
pjp
455
2020-04-10
pjp
u_char *end, *estart;
456
2020-04-10
pjp
int totallen, zonelen, rrlen;
457
2020-04-10
pjp
int replysize = 0;
458
2020-04-10
pjp
u_int16_t class = 0, type = 0, tcpsize;
459
2020-04-10
pjp
u_int16_t plen;
460
2020-04-10
pjp
u_int16_t tcplen;
461
2020-04-10
pjp
462
2020-04-10
pjp
if (format & TCP_FORMAT)
463
2020-04-10
pjp
replysize = 0xffff;
464
2020-04-10
pjp
else
465
2020-04-10
pjp
replysize = 4096;
466
2020-04-10
pjp
467
2020-04-10
pjp
468
2020-04-10
pjp
memset(&query, 0, sizeof(query));
469
2020-04-10
pjp
470
2020-04-10
pjp
if (format & TCP_FORMAT) {
471
2020-04-10
pjp
tcpsize = unpack16(&query[0]);
472
2020-04-10
pjp
wh = (struct whole_header *)&query[2];
473
2020-04-10
pjp
} else
474
2020-04-10
pjp
wh = (struct whole_header *)&query[0];
475
2020-04-10
pjp
476
2020-04-10
pjp
wh->dh.id = htons(arc4random() & 0xffff);
477
2020-04-10
pjp
wh->dh.query = 0;
478
2020-04-10
pjp
wh->dh.question = htons(1);
479
2020-04-10
pjp
wh->dh.answer = 0;
480
2020-04-10
pjp
wh->dh.nsrr = 0;
481
2020-04-10
pjp
wh->dh.additional = htons(1);;
482
2020-04-10
pjp
483
2020-04-10
pjp
SET_DNS_QUERY(&wh->dh);
484
2020-04-10
pjp
SET_DNS_RECURSION(&wh->dh);
485
2020-04-10
pjp
486
2020-04-10
pjp
487
2020-04-10
pjp
HTONS(wh->dh.query);
488
2020-04-10
pjp
489
2020-04-10
pjp
if (format & TCP_FORMAT)
490
2020-04-10
pjp
totallen = sizeof(struct whole_header) + 2;
491
2020-04-10
pjp
else
492
2020-04-10
pjp
totallen = sizeof(struct whole_header);
493
2020-04-10
pjp
494
2020-04-10
pjp
name = dns_label(zonename, &len);
495
2020-04-10
pjp
if (name == NULL) {
496
2020-04-10
pjp
return -1;
497
2020-04-10
pjp
}
498
2020-04-10
pjp
499
2020-04-10
pjp
zonelen = len;
500
2020-04-10
pjp
501
2020-04-10
pjp
p = (char *)&wh[1];
502
2020-04-10
pjp
503
2020-04-10
pjp
memcpy(p, name, len);
504
2020-04-10
pjp
totallen += len;
505
2020-04-10
pjp
506
2020-04-10
pjp
type = htons(myrrtype);
507
2020-04-10
pjp
pack16(&query[totallen], type);
508
2020-04-10
pjp
totallen += sizeof(u_int16_t);
509
2020-04-10
pjp
510
2020-07-19
pjp
class = htons(qclass);
511
2020-04-10
pjp
pack16(&query[totallen], class);
512
2020-04-10
pjp
totallen += sizeof(u_int16_t);
513
2020-04-10
pjp
514
2020-04-10
pjp
/* attach EDNS0 */
515
2020-04-10
pjp
516
2020-04-10
pjp
optrr = (struct dns_optrr *)&query[totallen];
517
2020-04-10
pjp
518
2020-04-10
pjp
optrr->name[0] = 0;
519
2020-04-10
pjp
optrr->type = htons(DNS_TYPE_OPT);
520
2020-04-10
pjp
optrr->class = htons(replysize);
521
2020-04-10
pjp
optrr->ttl = htonl(0); /* EDNS version 0 */
522
2020-04-10
pjp
if ((format & DNSSEC_FORMAT))
523
2020-04-10
pjp
SET_DNS_ERCODE_DNSSECOK(optrr);
524
2020-04-10
pjp
HTONL(optrr->ttl);
525
2020-04-10
pjp
optrr->rdlen = 0;
526
2020-04-10
pjp
optrr->rdata[0] = 0;
527
2020-04-10
pjp
528
2020-04-10
pjp
totallen += (sizeof(struct dns_optrr));
529
2020-04-10
pjp
530
2020-04-10
pjp
if (format & TCP_FORMAT) {
531
2020-04-10
pjp
tcpsize = htons(totallen - 2);
532
2020-04-10
pjp
pack16(&query[0], tcpsize);
533
2020-04-10
pjp
}
534
2020-04-10
pjp
535
2020-04-10
pjp
if (send(so, query, totallen, 0) < 0) {
536
2020-04-10
pjp
return -1;
537
2020-04-10
pjp
}
538
2020-04-10
pjp
539
2020-04-10
pjp
/* catch reply */
540
2020-04-10
pjp
541
2020-04-10
pjp
reply = calloc(1, replysize + 2);
542
2020-04-10
pjp
if (reply == NULL) {
543
2020-04-10
pjp
perror("calloc");
544
2020-04-10
pjp
return -1;
545
2020-04-10
pjp
}
546
2020-04-10
pjp
547
2020-04-10
pjp
if (format & TCP_FORMAT) {
548
2020-04-10
pjp
if ((len = recv(so, reply, 2, MSG_PEEK | MSG_WAITALL)) < 0) {
549
2020-04-10
pjp
perror("recv");
550
2020-04-10
pjp
return -1;
551
2020-04-10
pjp
}
552
2020-04-10
pjp
553
2020-04-10
pjp
plen = unpack16(reply);
554
2020-04-10
pjp
tcplen = ntohs(plen);
555
2020-04-10
pjp
556
2020-04-10
pjp
if ((len = recv(so, reply, tcplen + 2, MSG_WAITALL)) < 0) {
557
2020-04-10
pjp
perror("recv");
558
2020-04-10
pjp
return -1;
559
2020-04-10
pjp
}
560
2020-04-10
pjp
} else {
561
2020-04-10
pjp
if ((len = recv(so, reply, replysize, 0)) < 0) {
562
2020-04-10
pjp
return -1;
563
2020-04-10
pjp
}
564
2020-04-10
pjp
}
565
2020-04-10
pjp
566
2020-04-10
pjp
if (format & TCP_FORMAT)
567
2020-04-10
pjp
rwh = (struct whole_header *)&reply[2];
568
2020-04-10
pjp
else
569
2020-04-10
pjp
rwh = (struct whole_header *)&reply[0];
570
2020-04-10
pjp
571
2020-04-10
pjp
bytes_received += len;
572
2020-04-10
pjp
573
2020-04-10
pjp
end = &reply[len];
574
2020-04-10
pjp
575
2020-04-10
pjp
if (rwh->dh.id != wh->dh.id) {
576
2020-04-10
pjp
fprintf(stderr, "DNS ID mismatch 2\n");
577
2020-04-10
pjp
return -1;
578
2020-04-10
pjp
}
579
2020-04-10
pjp
580
2020-04-10
pjp
if (!(htons(rwh->dh.query) & DNS_REPLY)) {
581
2020-04-10
pjp
fprintf(stderr, "NOT a DNS reply\n");
582
2020-04-10
pjp
return -1;
583
2020-04-10
pjp
}
584
2020-04-10
pjp
585
2020-04-10
pjp
if (!(format & TCP_FORMAT) && (htons(rwh->dh.query) & DNS_TRUNC)) {
586
2020-04-10
pjp
int ret;
587
2020-04-10
pjp
588
2020-04-10
pjp
fprintf(f, ";; received a truncated answer, retrying with TCP\n");
589
2020-04-10
pjp
format |= TCP_FORMAT;
590
2020-04-10
pjp
591
2020-04-10
pjp
gettimeofday(&tv0, NULL);
592
2020-04-10
pjp
so = connect_server(nameserver, port, format);
593
2020-04-10
pjp
if (so < 0) {
594
2020-04-10
pjp
exit(1);
595
2020-04-10
pjp
}
596
2020-04-10
pjp
597
2020-07-19
pjp
ret = lookup_name(f, so, zonename, myrrtype, mysoa, format, nameserver, port, answers, additionalcount, qclass);
598
2020-04-10
pjp
close(so);
599
2020-04-10
pjp
return (ret);
600
2020-04-10
pjp
}
601
2020-04-10
pjp
602
2020-04-10
pjp
numansw = ntohs(rwh->dh.answer);
603
2020-04-10
pjp
numauth = ntohs(rwh->dh.nsrr);
604
2020-04-10
pjp
numaddi = ntohs(rwh->dh.additional);
605
2020-04-10
pjp
tmp32 = (numansw + numauth + numaddi);
606
2020-04-10
pjp
pack32((char *)answers, tmp32);
607
2020-04-10
pjp
608
2020-04-10
pjp
if (tmp32 < 1) {
609
2020-04-10
pjp
fprintf(stderr, "NO ANSWER provided\n");
610
2020-04-10
pjp
return -1;
611
2020-04-10
pjp
}
612
2020-04-10
pjp
613
2020-04-10
pjp
614
2020-04-10
pjp
q = build_question((char *)&wh->dh, len, wh->dh.additional, NULL);
615
2020-04-10
pjp
if (q == NULL) {
616
2020-04-10
pjp
fprintf(stderr, "failed to build_question\n");
617
2020-04-10
pjp
return -1;
618
2020-04-10
pjp
}
619
2020-04-10
pjp
620
2020-04-10
pjp
if (memcmp(q->hdr->name, name, q->hdr->namelen) != 0) {
621
2020-04-10
pjp
fprintf(stderr, "question name not for what we asked\n");
622
2020-04-10
pjp
return -1;
623
2020-04-10
pjp
}
624
2020-04-10
pjp
625
2020-04-10
pjp
if (q->hdr->qclass != class || q->hdr->qtype != type) {
626
2020-04-10
pjp
fprintf(stderr, "wrong class or type\n");
627
2020-04-10
pjp
return -1;
628
2020-04-10
pjp
}
629
2020-04-10
pjp
630
2020-04-10
pjp
p = (u_char *)&rwh[1];
631
2020-04-10
pjp
632
2020-04-10
pjp
p += q->hdr->namelen;
633
2020-04-10
pjp
p += sizeof(u_int16_t); /* type */
634
2020-04-10
pjp
p += sizeof(u_int16_t); /* class */
635
2020-04-10
pjp
636
2020-04-10
pjp
/* end of question */
637
2020-04-10
pjp
638
2020-04-10
pjp
639
2020-04-10
pjp
estart = (u_char *)&rwh->dh;
640
2020-04-10
pjp
641
2020-04-10
pjp
for (i = *answers; i > 0; i--) {
642
2020-04-10
pjp
if (numansw > 0) {
643
2020-04-10
pjp
numansw--;
644
2020-04-10
pjp
if (printansw-- > 0) {
645
2020-04-10
pjp
printf(";; ANSWER SECTION:\n");
646
2020-04-10
pjp
}
647
2020-04-10
pjp
goto skip;
648
2020-04-10
pjp
}
649
2020-04-10
pjp
if (numansw <= 0 && numauth > 0) {
650
2020-04-10
pjp
numauth--;
651
2020-04-10
pjp
if (printauth-- > 0) {
652
2020-04-10
pjp
printf(";; AUTHORITY SECTION:\n");
653
2020-04-10
pjp
}
654
2020-04-10
pjp
goto skip;
655
2020-04-10
pjp
}
656
2020-04-10
pjp
if (numansw <= 0 && numauth <= 0 && numaddi > 0) {
657
2020-04-10
pjp
numaddi--;
658
2020-04-10
pjp
if (printaddi-- > 0) {
659
2020-04-10
pjp
printf(";; ADDITIONAL SECTION:\n");
660
2020-04-10
pjp
}
661
2020-04-10
pjp
goto skip;
662
2020-04-10
pjp
}
663
2020-04-10
pjp
664
2020-04-10
pjp
skip:
665
2020-04-10
pjp
666
2020-04-10
pjp
667
2020-07-27
pjp
if ((rrlen = raxfr_peek(f, p, estart, end, &rrtype, 0, &rdlen, format, NULL, name, zonelen, 0)) < 0) {
668
2020-04-10
pjp
fprintf(stderr, "not a SOA reply, or ERROR\n");
669
2020-04-10
pjp
return -1;
670
2020-04-10
pjp
}
671
2020-04-10
pjp
672
2020-04-10
pjp
p = (estart + rrlen);
673
2020-04-10
pjp
674
2020-04-10
pjp
if (rrtype == DNS_TYPE_SOA) {
675
2020-07-27
pjp
if ((len = raxfr_soa(f, p, estart, end, mysoa, soacount, format, rdlen, NULL, &constraints)) < 0) {
676
2020-04-10
pjp
fprintf(stderr, "raxxfr_soa failed\n");
677
2020-04-10
pjp
return -1;
678
2020-04-10
pjp
}
679
2020-04-10
pjp
p = (estart + len);
680
2020-04-10
pjp
soacount++;
681
2020-04-10
pjp
} else {
682
2020-04-10
pjp
for (sr = supported; sr->rrtype != 0; sr++) {
683
2020-04-10
pjp
if (rrtype == sr->rrtype) {
684
2020-07-08
pjp
if ((len = (*sr->raxfr)(f, p, estart, end, mysoa, rdlen, NULL)) < 0) {
685
2020-04-10
pjp
fprintf(stderr, "error with rrtype %d\n", sr->rrtype);
686
2020-04-10
pjp
return -1;
687
2020-04-10
pjp
}
688
2020-04-10
pjp
p = (estart + len);
689
2020-04-10
pjp
break;
690
2020-04-10
pjp
}
691
2020-04-10
pjp
}
692
2020-04-10
pjp
693
2020-04-10
pjp
if (sr->rrtype == 0) {
694
2020-04-10
pjp
if (rrtype != 41)
695
2020-04-10
pjp
fprintf(stderr, "unsupported RRTYPE %u\n", rrtype);
696
2020-04-10
pjp
}
697
2020-04-10
pjp
} /* rrtype == DNS_TYPE_SOA */
698
2020-04-10
pjp
699
2020-04-10
pjp
700
2020-04-10
pjp
} /* for () */
701
2020-04-10
pjp
702
2020-04-10
pjp
return 0;
703
2020-04-10
pjp
}
repomaster@centroid.eu