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