-
Notifications
You must be signed in to change notification settings - Fork 5
/
nsnotify.c
432 lines (394 loc) · 9.94 KB
/
nsnotify.c
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
/* nsnotify: send DNS NOTIFY messages to lots of targets
*
* Written by Tony Finch <[email protected]> in Cambridge.
*
* Permission is hereby granted to use, copy, modify, and/or
* distribute this software for any purpose with or without fee.
*
* This software is provided 'as is', without warranty of any kind.
* In no event shall the authors be liable for any damages arising
* from the use of this software.
*
* SPDX-License-Identifier: 0BSD OR MIT-0
*/
#define BIND_8_COMPAT
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/nameser.h>
#include <netinet/in.h>
#include <err.h>
#include <fcntl.h>
#include <netdb.h>
#include <resolv.h>
#include <unistd.h>
#include "version.h"
typedef unsigned char byte;
static const char what_ident[] =
"@(#) $Program: nsnotify $\n"
"@(#) $Version: " VERSION " $\n"
"@(#) $Date: " REVDATE " $\n"
"@(#) $Author: Tony Finch ([email protected]) $\n"
"@(#) $URL: http://dotat.at/prog/nsnotifyd/ $\n"
;
static int
version(void) {
const char *p = what_ident;
for(;;) {
while(*++p != '$')
if(*p == '\0')
return(0);
while(*++p != '$')
putchar(*p);
putchar('\n');
}
}
/*
* When sending over TCP, nsnotifyd will handle only one message at a
* time, but we want to ensure it does the right thing when we send
* faster than that. But while we are sending, we also need to handle
* replies to avoid filling up buffers and blocking. So here's a fun
* little select() loop.
*/
static int
tcp_write(int s, const byte *msgv[], size_t msgc, int debug) {
/* length followed by maximum message size */
static byte rdbuf[2 + 0xffff];
int r;
r = fcntl(s, F_GETFL, 0);
if(r < 0) {
warn("fcntl(O_NONBLOCK)");
return(-1);
}
r = fcntl(s, F_SETFL, r | O_NONBLOCK);
if(r < 0) {
warn("fcntl(O_NONBLOCK)");
return(-1);
}
size_t wrmsg = 0, wrlen = 0, wrpos = 0;
size_t rdmsg = 0, rdlen = 0, rdpos = 0;
wrlen = ns_get16(msgv[wrmsg]);
rdlen = 2;
for(;;) {
fd_set rdset; FD_ZERO(&rdset); FD_SET(s, &rdset);
fd_set wrset; FD_ZERO(&wrset); FD_SET(s, &wrset);
r = select(s + 1, &rdset, &wrset, NULL, NULL);
if(r < 0 && errno == EINTR) continue;
if(r < 0) {
warn("select");
return(-1);
}
if(FD_ISSET(s, &rdset)) {
ssize_t n = read(s, rdbuf + rdpos, rdlen - rdpos);
if(n < 0 && errno == EINTR) continue;
if(n < 0 && errno == EAGAIN) continue;
if(n < 0) {
warn("read");
return(-1);
}
rdpos += (size_t)n;
if(debug)
fprintf(stderr, "; R %zu %zi %zu/%zu\n",
rdmsg, n, rdpos, rdlen);
if(rdpos == 2) {
rdlen = ns_get16(rdbuf);
} else if(rdpos >= rdlen) {
rdmsg += 1;
rdpos = 0;
rdlen = 2;
if(rdmsg >= msgc)
return(0);
}
}
if(FD_ISSET(s, &wrset) && wrmsg < msgc) {
ssize_t n = write(s, msgv[wrmsg] + wrpos, wrlen - wrpos);
if(n < 0 && errno == EINTR) continue;
if(n < 0 && errno == EAGAIN) continue;
if(n < 0) {
warn("write");
return(-1);
}
wrpos += (size_t)n;
if(debug)
fprintf(stderr, "; W %zu %zi %zu/%zu\n",
wrmsg, n, wrpos, wrlen);
if(wrpos >= wrlen) {
wrmsg += 1;
wrpos = 0;
if(wrmsg < msgc)
wrlen = ns_get16(msgv[wrmsg]);
else
wrlen = 0;
}
}
}
}
/*
* When sending over UDP, there's no need to handle replies
*/
static int
udp_write(int s, const byte *msgv[], size_t msgc, int debug) {
for(size_t msgi = 0; msgi < msgc; msgi++) {
size_t len = ns_get16(msgv[msgi]);
ssize_t r = write(s, msgv[msgi] + 2, len);
if(debug)
fprintf(stderr, "; W %zu %zi/%zu\n",
msgi, r, len);
if(r < 0) {
warn("write");
return(-1);
}
}
return(0);
}
static int
notify(struct addrinfo *hints, struct addrinfo *sai,
const char *target, const char *port,
const byte *msgv[], size_t msgc, int debug) {
struct addrinfo *ai0, *ai;
int r = getaddrinfo(target, port, hints, &ai0);
if(r != 0) {
warnx("%s: %s", target, gai_strerror(r));
return(-1);
}
for(ai = ai0; ai != NULL; ai = ai->ai_next) {
if(debug) {
char host[NI_MAXHOST], serv[NI_MAXSERV];
int e = getnameinfo(ai->ai_addr, ai->ai_addrlen,
host, sizeof(host),
serv, sizeof(serv),
NI_NUMERICHOST | NI_NUMERICSERV);
if(e == 0)
fprintf(stderr, "; -> %s [%s#%s]\n",
target, host, serv);
else
fprintf(stderr, "; -> %s [%s]\n",
target, gai_strerror(e));
}
int s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
if(s < 0) {
warn("socket");
r |= -1;
continue;
}
const char *f = NULL;
if(sai != NULL && bind(s, sai->ai_addr, sai->ai_addrlen) < 0) {
f = "bind";
}
if(f == NULL && connect(s, ai->ai_addr, ai->ai_addrlen) < 0) {
f = "connect";
}
if(f != NULL) {
close(s);
warn("%s", f);
r |= -1;
continue;
}
if(ai->ai_socktype == SOCK_STREAM)
r |= tcp_write(s, msgv, msgc, debug);
else
r |= udp_write(s, msgv, msgc, debug);
close(s);
}
freeaddrinfo(ai0);
return(r);
}
static const byte *
make_a_message(const char *zone, int debug) {
unsigned buf[NS_PACKETSZ / sizeof(unsigned)];
HEADER *header = (void*)&buf;
byte *msg = (void*)&buf;
int msglen = res_mkquery(ns_o_query, zone, ns_c_in, ns_t_soa,
NULL, 0, NULL, msg, sizeof(msg));
if(msglen < 0)
errx(1, "could not make DNS NOTIFY message for %s", zone);
header->opcode = ns_o_notify;
header->rd = 0;
if(debug > 1)
res_pquery(&_res, msg, msglen, stderr);
byte *tcpmsg = malloc((size_t)msglen + 2);
if(tcpmsg == NULL)
err(1, "could not make DNS NOTIFY message for %s", zone);
ns_put16(msglen, tcpmsg);
memcpy(tcpmsg + 2, msg, (size_t)msglen);
return(tcpmsg);
}
static size_t
make_messages(const byte ***msgvp, const char *file, int debug) {
FILE *fh = fopen(file, "r");
if(fh == NULL)
err(1, "open %s", file);
size_t msgc = 0;
size_t maxmsg = 16;
const byte **msgv = malloc(sizeof(*msgv) * maxmsg);
if(msgv == NULL)
err(1, "malloc");
char zone[NS_MAXDNAME];
while(fgets(zone, sizeof(zone), fh) != NULL) {
size_t len = strlen(zone);
if(len > 0 && zone[len-1] == '\n')
zone[--len] = '\0';
msgv[msgc++] = make_a_message(zone, debug);
if(msgc == maxmsg) {
maxmsg *= 2;
msgv = realloc(msgv, sizeof(*msgv) * maxmsg);
if(msgv == NULL)
err(1, "malloc");
}
}
if(ferror(fh) || fclose(fh))
err(1, "read %s", file);
*msgvp = msgv;
return(msgc);
}
static int
usage(void) {
fprintf(stderr,
"usage: nsnotify [-46dFfpstV] zone [targets]\n"
" -4 send on IPv4 only\n"
" -6 send on IPv6 only\n"
" -d debugging mode\n"
" (use twice to print DNS messages)\n"
" -F zones read domain names from file instead of command line\n"
" -f targets read targets from file instead of command line\n"
" -p port send notifies to this port number\n"
" (default 53)\n"
" -s addr source address and optional port\n"
" -t send notifies over TCP instead of UDP\n"
" -V print version information\n"
" zone the zone for which to send notifies\n"
" targets destinations of notify messages\n"
" (may be command-line arguments\n"
" or read from stdin, one per line)\n"
);
return(1);
}
int
main(int argc, char *argv[]) {
const char *port = "domain";
const char *targets_fn = NULL;
const char *zones_fn = NULL;
char *source = NULL;
int protocol = SOCK_DGRAM;
int family = PF_UNSPEC;
int debug = 0;
int r;
while((r = getopt(argc, argv, "46dF:f:p:s:tV")) != -1)
switch(r) {
case('4'):
family = PF_INET;
continue;
case('6'):
family = PF_INET6;
continue;
case('d'):
debug++;
continue;
case('F'):
zones_fn = optarg;
continue;
case('f'):
targets_fn = optarg;
continue;
case('p'):
port = optarg;
continue;
case('s'):
source = optarg;
continue;
case('t'):
protocol = SOCK_STREAM;
continue;
case('V'):
exit(version());
default:
exit(usage());
}
res_init();
if(debug > 1) _res.options |= RES_DEBUG;
argc -= optind;
argv += optind;
if(targets_fn == NULL && zones_fn == NULL && argc < 2)
exit(usage());
if(targets_fn != NULL && zones_fn == NULL && argc < 1)
exit(usage());
if(targets_fn == NULL && zones_fn != NULL && argc < 1)
exit(usage());
if(targets_fn != NULL && zones_fn != NULL && argc > 0)
exit(usage());
struct addrinfo hints, *sai = NULL;
memset(&hints, 0, sizeof(hints));
hints.ai_family = family;
hints.ai_socktype = protocol;
if(source != NULL) {
hints.ai_flags = AI_PASSIVE;
r = getaddrinfo(source, NULL, &hints, &sai);
if(r != 0) {
errx(1, "%s: %s", source, gai_strerror(r));
}
if(sai->ai_next != NULL) {
errx(1, "%s: ambiguous source address", source);
}
if(debug) {
char host[NI_MAXHOST], serv[NI_MAXSERV];
int e = getnameinfo(sai->ai_addr, sai->ai_addrlen,
host, sizeof(host),
serv, sizeof(serv),
NI_NUMERICHOST | NI_NUMERICSERV);
if(e == 0)
fprintf(stderr, "; source %s#%s\n",
host, serv);
else
fprintf(stderr, "; source %s: %s\n",
source, gai_strerror(e));
}
hints.ai_family = sai->ai_family;
hints.ai_flags = 0;
}
const byte **msgv, *msg1;
size_t msgc;
if(zones_fn != NULL) {
msgc = make_messages(&msgv, zones_fn, debug);
} else if(targets_fn != NULL) {
msgc = (size_t)argc;
msgv = malloc(sizeof(*msgv) * msgc);
if(msgv == NULL)
err(1, "malloc");
for(int i = 0; i < argc; i++)
msgv[i] = make_a_message(argv[i], debug);
argv += argc;
argc = 0;
} else {
const char *zone = *argv++; argc--;
msg1 = make_a_message(zone, debug);
msgv = &msg1;
msgc = 1;
}
r = 0;
for(int i = 0; i < argc; i++)
r |= notify(&hints, sai, argv[i], port, msgv, msgc, debug);
if(targets_fn == NULL)
exit(!!r);
FILE *fh;
if(strcmp(targets_fn, "-") == 0) {
targets_fn = "stdin";
fh = stdin;
} else {
fh = fopen(targets_fn, "r");
if(fh == NULL)
err(1, "open %s", targets_fn);
}
char target[NI_MAXHOST];
while(fgets(target, sizeof(target), fh) != NULL) {
size_t len = strlen(target);
if(len > 0 && target[len-1] == '\n')
target[--len] = '\0';
r |= notify(&hints, sai, target, port, msgv, msgc, debug);
}
if(ferror(fh) || fclose(fh))
err(1, "read %s", targets_fn);
exit(!!r);
}