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
|
#include <stdio.h>
#include <unistd.h>
#include "uint32.h"
#include "uint16.h"
#include "stralloc.h"
#include "error.h"
#include "strerr.h"
#include "getln.h"
#include "buffer.h"
#include "exit.h"
#include "open.h"
#include "scan.h"
#include "byte.h"
#include "str.h"
#include "ip4.h"
#include "timeoutread.h"
#include "timeoutwrite.h"
#include "dns.h"
#define FATAL "axfr-get: fatal: "
void die_usage(void)
{
strerr_die1x(100,"axfr-get: usage: axfr-get zone fn fn.tmp");
}
void die_generate(void)
{
strerr_die2sys(111,FATAL,"unable to generate AXFR query: ");
}
void die_parse(void)
{
strerr_die2sys(111,FATAL,"unable to parse AXFR results: ");
}
unsigned int x_copy(char *buf,unsigned int len,unsigned int pos,char *out,unsigned int outlen)
{
pos = dns_packet_copy(buf,len,pos,out,outlen);
if (!pos) die_parse();
return pos;
}
unsigned int x_getname(char *buf,unsigned int len,unsigned int pos,char **out)
{
pos = dns_packet_getname(buf,len,pos,out);
if (!pos) die_parse();
return pos;
}
unsigned int x_skipname(char *buf,unsigned int len,unsigned int pos)
{
pos = dns_packet_skipname(buf,len,pos);
if (!pos) die_parse();
return pos;
}
static char *zone;
unsigned int zonelen;
char *fn;
char *fntmp;
void die_netread(void)
{
strerr_die2sys(111,FATAL,"unable to read from network: ");
}
void die_netwrite(void)
{
strerr_die2sys(111,FATAL,"unable to write to network: ");
}
void die_read(void)
{
strerr_die4sys(111,FATAL,"unable to read ",fn,": ");
}
void die_write(void)
{
strerr_die4sys(111,FATAL,"unable to write ",fntmp,": ");
}
int saferead(int fd,char *buf,unsigned int len)
{
int r;
r = timeoutread(60,fd,buf,len);
if (r == 0) { errno = error_proto; die_parse(); }
if (r <= 0) die_netread();
return r;
}
int safewrite(int fd,char *buf,unsigned int len)
{
int r;
r = timeoutwrite(60,fd,buf,len);
if (r <= 0) die_netwrite();
return r;
}
char netreadspace[1024];
buffer netread = BUFFER_INIT(saferead,6,netreadspace,sizeof netreadspace);
char netwritespace[1024];
buffer netwrite = BUFFER_INIT(safewrite,7,netwritespace,sizeof netwritespace);
void netget(char *buf,unsigned int len)
{
int r;
while (len > 0) {
r = buffer_get(&netread,buf,len);
buf += r; len -= r;
}
}
int fd;
buffer b;
char bspace[1024];
void put(char *buf,unsigned int len)
{
if (buffer_put(&b,buf,len) == -1) die_write();
}
int printable(char ch)
{
if (ch == '.') return 1;
if ((ch >= 'a') && (ch <= 'z')) return 1;
if ((ch >= '0') && (ch <= '9')) return 1;
if ((ch >= 'A') && (ch <= 'Z')) return 1;
if (ch == '-') return 1;
return 0;
}
static char *d1;
static char *d2;
static char *d3;
stralloc line;
int match;
int numsoa;
unsigned int doit(char *buf,unsigned int len,unsigned int pos)
{
char data[20];
uint32 ttl;
uint16 dlen;
uint16 typenum;
uint32 u32;
int i;
pos = x_getname(buf,len,pos,&d1);
pos = x_copy(buf,len,pos,data,10);
uint16_unpack_big(data,&typenum);
uint32_unpack_big(data + 4,&ttl);
uint16_unpack_big(data + 8,&dlen);
if (len - pos < dlen) { errno = error_proto; return 0; }
len = pos + dlen;
if (!dns_domain_suffix(d1,zone)) return len;
if (byte_diff(data + 2,2,DNS_C_IN)) return len;
if (byte_equal(data,2,DNS_T_SOA)) {
if (++numsoa >= 2) return len;
pos = x_getname(buf,len,pos,&d2);
pos = x_getname(buf,len,pos,&d3);
x_copy(buf,len,pos,data,20);
uint32_unpack_big(data,&u32);
if (!stralloc_copys(&line,"#")) return 0;
if (!stralloc_catulong0(&line,u32,0)) return 0;
if (!stralloc_cats(&line," auto axfr-get\n")) return 0;
if (!stralloc_cats(&line,"Z")) return 0;
if (!dns_domain_todot_cat(&line,d1)) return 0;
if (!stralloc_cats(&line,":")) return 0;
if (!dns_domain_todot_cat(&line,d2)) return 0;
if (!stralloc_cats(&line,".:")) return 0;
if (!dns_domain_todot_cat(&line,d3)) return 0;
if (!stralloc_cats(&line,".")) return 0;
for (i = 0;i < 5;++i) {
uint32_unpack_big(data + 4 * i,&u32);
if (!stralloc_cats(&line,":")) return 0;
if (!stralloc_catulong0(&line,u32,0)) return 0;
}
}
else if (byte_equal(data,2,DNS_T_NS)) {
if (!stralloc_copys(&line,"&")) return 0;
if (byte_equal(d1,2,"\1*")) { errno = error_proto; return 0; }
if (!dns_domain_todot_cat(&line,d1)) return 0;
if (!stralloc_cats(&line,"::")) return 0;
x_getname(buf,len,pos,&d1);
if (!dns_domain_todot_cat(&line,d1)) return 0;
if (!stralloc_cats(&line,".")) return 0;
}
else if (byte_equal(data,2,DNS_T_CNAME)) {
if (!stralloc_copys(&line,"C")) return 0;
if (!dns_domain_todot_cat(&line,d1)) return 0;
if (!stralloc_cats(&line,":")) return 0;
x_getname(buf,len,pos,&d1);
if (!dns_domain_todot_cat(&line,d1)) return 0;
if (!stralloc_cats(&line,".")) return 0;
}
else if (byte_equal(data,2,DNS_T_PTR)) {
if (!stralloc_copys(&line,"^")) return 0;
if (!dns_domain_todot_cat(&line,d1)) return 0;
if (!stralloc_cats(&line,":")) return 0;
x_getname(buf,len,pos,&d1);
if (!dns_domain_todot_cat(&line,d1)) return 0;
if (!stralloc_cats(&line,".")) return 0;
}
else if (byte_equal(data,2,DNS_T_MX)) {
uint16 dist;
if (!stralloc_copys(&line,"@")) return 0;
if (!dns_domain_todot_cat(&line,d1)) return 0;
if (!stralloc_cats(&line,"::")) return 0;
pos = x_copy(buf,len,pos,data,2);
uint16_unpack_big(data,&dist);
x_getname(buf,len,pos,&d1);
if (!dns_domain_todot_cat(&line,d1)) return 0;
if (!stralloc_cats(&line,".:")) return 0;
if (!stralloc_catulong0(&line,dist,0)) return 0;
}
else if (byte_equal(data,2,DNS_T_A) && (dlen == 4)) {
char ipstr[IP4_FMT];
if (!stralloc_copys(&line,"+")) return 0;
if (!dns_domain_todot_cat(&line,d1)) return 0;
if (!stralloc_cats(&line,":")) return 0;
x_copy(buf,len,pos,data,4);
if (!stralloc_catb(&line,ipstr,ip4_fmt(ipstr,data))) return 0;
}
else {
unsigned char ch;
unsigned char ch2;
if (!stralloc_copys(&line,":")) return 0;
if (!dns_domain_todot_cat(&line,d1)) return 0;
if (!stralloc_cats(&line,":")) return 0;
if (!stralloc_catulong0(&line,typenum,0)) return 0;
if (!stralloc_cats(&line,":")) return 0;
for (i = 0;i < dlen;++i) {
pos = x_copy(buf,len,pos,data,1);
ch = data[0];
if (printable(ch)) {
if (!stralloc_catb(&line,&ch,1)) return 0;
}
else {
if (!stralloc_cats(&line,"\\")) return 0;
ch2 = '0' + ((ch >> 6) & 7);
if (!stralloc_catb(&line,&ch2,1)) return 0;
ch2 = '0' + ((ch >> 3) & 7);
if (!stralloc_catb(&line,&ch2,1)) return 0;
ch2 = '0' + (ch & 7);
if (!stralloc_catb(&line,&ch2,1)) return 0;
}
}
}
if (!stralloc_cats(&line,":")) return 0;
if (!stralloc_catulong0(&line,ttl,0)) return 0;
if (!stralloc_cats(&line,"\n")) return 0;
put(line.s,line.len);
return len;
}
stralloc packet;
int main(int argc,char **argv)
{
char out[20];
unsigned long u;
uint16 dlen;
unsigned int pos;
uint32 oldserial = 0;
uint32 newserial = 0;
uint16 numqueries;
uint16 numanswers;
if (!*argv) die_usage();
if (!*++argv) die_usage();
if (!dns_domain_fromdot(&zone,*argv,str_len(*argv))) die_generate();
zonelen = dns_domain_length(zone);
if (!*++argv) die_usage();
fn = *argv;
if (!*++argv) die_usage();
fntmp = *argv;
fd = open_read(fn);
if (fd == -1) {
if (errno != error_noent) die_read();
}
else {
buffer_init(&b,buffer_unixread,fd,bspace,sizeof bspace);
if (getln(&b,&line,&match,'\n') == -1) die_read();
if (!stralloc_0(&line)) die_read();
if (line.s[0] == '#') {
scan_ulong(line.s + 1,&u);
oldserial = u;
}
close(fd);
}
if (!stralloc_copyb(&packet,"\0\0\0\0\0\1\0\0\0\0\0\0",12)) die_generate();
if (!stralloc_catb(&packet,zone,zonelen)) die_generate();
if (!stralloc_catb(&packet,DNS_T_SOA DNS_C_IN,4)) die_generate();
uint16_pack_big(out,packet.len);
buffer_put(&netwrite,out,2);
buffer_put(&netwrite,packet.s,packet.len);
buffer_flush(&netwrite);
netget(out,2);
uint16_unpack_big(out,&dlen);
if (!stralloc_ready(&packet,dlen)) die_parse();
netget(packet.s,dlen);
packet.len = dlen;
pos = x_copy(packet.s,packet.len,0,out,12);
uint16_unpack_big(out + 4,&numqueries);
uint16_unpack_big(out + 6,&numanswers);
while (numqueries) {
--numqueries;
pos = x_skipname(packet.s,packet.len,pos);
pos += 4;
}
if (!numanswers) { errno = error_proto; die_parse(); }
pos = x_getname(packet.s,packet.len,pos,&d1);
if (!dns_domain_equal(zone,d1)) { errno = error_proto; die_parse(); }
pos = x_copy(packet.s,packet.len,pos,out,10);
if (byte_diff(out,4,DNS_T_SOA DNS_C_IN)) { errno = error_proto; die_parse(); }
pos = x_skipname(packet.s,packet.len,pos);
pos = x_skipname(packet.s,packet.len,pos);
pos = x_copy(packet.s,packet.len,pos,out,4);
uint32_unpack_big(out,&newserial);
if (oldserial && newserial) /* allow 0 for very recently modified zones */
if (oldserial == newserial) /* allow serial numbers to move backwards */
_exit(0);
fd = open_trunc(fntmp);
if (fd == -1) die_write();
buffer_init(&b,buffer_unixwrite,fd,bspace,sizeof bspace);
if (!stralloc_copyb(&packet,"\0\0\0\0\0\1\0\0\0\0\0\0",12)) die_generate();
if (!stralloc_catb(&packet,zone,zonelen)) die_generate();
if (!stralloc_catb(&packet,DNS_T_AXFR DNS_C_IN,4)) die_generate();
uint16_pack_big(out,packet.len);
buffer_put(&netwrite,out,2);
buffer_put(&netwrite,packet.s,packet.len);
buffer_flush(&netwrite);
numsoa = 0;
while (numsoa < 2) {
netget(out,2);
uint16_unpack_big(out,&dlen);
if (!stralloc_ready(&packet,dlen)) die_parse();
netget(packet.s,dlen);
packet.len = dlen;
pos = x_copy(packet.s,packet.len,0,out,12);
uint16_unpack_big(out + 4,&numqueries);
while (numqueries) {
--numqueries;
pos = x_skipname(packet.s,packet.len,pos);
pos += 4;
}
while (pos < packet.len) {
pos = doit(packet.s,packet.len,pos);
if (!pos) die_parse();
}
}
if (buffer_flush(&b) == -1) die_write();
if (fsync(fd) == -1) die_write();
if (close(fd) == -1) die_write(); /* NFS dorks */
if (rename(fntmp,fn) == -1)
strerr_die6sys(111,FATAL,"unable to move ",fntmp," to ",fn,": ");
_exit(0);
}
|