int response_addname(char *d) { unsigned int dlen; unsigned int i; char buf[2]; dlen = dns_domain_length(d); while (*d) { for (i = 0;i < name_num;++i) if (dns_domain_equal(d,name[i])) { uint16_pack_big(buf,49152 + name_ptr[i]); return response_addbytes(buf,2); } if (dlen <= 128) if (name_num < NAMES) { byte_copy(name[name_num],dlen,d); name_ptr[name_num] = response_len; ++name_num; } i = (unsigned char) *d; ++i; if (!response_addbytes(d,i)) return 0; d += i; dlen -= i; } return response_addbytes(d,1); }
static int irrelevant (const struct dns_transmit *d, const char *buf, unsigned int len) { unsigned int pos = 0; char out[12], *dn = NULL; if (!(pos = dns_packet_copy (buf, len, 0, out, 12))) return 1; if (byte_diff (out, 2, d->query + 2)) return 1; if (out[4] != 0) return 1; if (out[5] != 1) return 1; if (!(pos = dns_packet_getname (buf, len, pos, &dn))) return 1; if (!dns_domain_equal (dn, d->query + 14)) { alloc_free (dn); return 1; } alloc_free (dn); if (!(pos = dns_packet_copy (buf, len, pos, out, 4))) return 1; if (byte_diff (out, 2, d->qtype)) return 1; if (byte_diff (out + 2, 2, DNS_C_IN)) return 1; return 0; }
void axfrcheck (char *q) { int i = 0, j = 0; if (!axfr) return; for (;;) { if (!axfr[i] || (axfr[i] == '/')) { if (i > j) { if (!dns_domain_fromdot (&axfrok, axfr + j, i - j)) err (-1, "could not allocate enough memory"); if (dns_domain_equal (q, axfrok)) return; } j = i + 1; } if (!axfr[i]) break; ++i; } err (-1, "zone transfer request not allowed"); }
int dd(char *q,char *base,char ip[4]) { int j; unsigned int x; for (j = 0;;++j) { if (dns_domain_equal(q,base)) return j; if (j >= 4) return -1; if (*q <= 0) return -1; if (*q >= 4) return -1; if ((q[1] < '0') || (q[1] > '9')) return -1; x = q[1] - '0'; if (*q == 1) { ip[j] = x; q += 2; continue; } if (!x) return -1; if ((q[2] < '0') || (q[2] > '9')) return -1; x = x * 10 + (q[2] - '0'); if (*q == 2) { ip[j] = x; q += 3; continue; } if ((q[3] < '0') || (q[3] > '9')) return -1; x = x * 10 + (q[3] - '0'); if (x > 255) return -1; ip[j] = x; q += 4; } }
int dnszones_exist(struct dnszones *dnszones, unsigned char *q) { long long i; for(i = 0; i < dnszones->zoneslen; ++i) { if (dns_domain_equal(q, dnszones->zones[i]->q)) {errno = EEXIST; return 1;} } return 0; }
static int merge_equal (struct dns_transmit *a, struct dns_transmit *b) { const char *ip1 = a->servers + 4 * a->curserver; const char *ip2 = b->servers + 4 * b->curserver; return byte_equal (ip1, 4, ip2) && byte_equal (a->qtype, 2, b->qtype) && dns_domain_equal(a->query + 14, b->query + 14); }
int dns_domain_suffix(const unsigned char *big, const unsigned char *little) { unsigned char c; for (;;) { if (dns_domain_equal(big, little)) return 1; c = *big++; if (!c) return 0; big += c; } }
long long dns_domain_suffixpos(const unsigned char *big, const unsigned char *little) { const unsigned char *orig = big; unsigned char c; for (;;) { if (dns_domain_equal(big, little)) return (big - orig); c = *big++; if (!c) return 0; big += c; } }
static int roots_find(char *q) { int i; int j; i = 0; while (i < data.len) { j = dns_domain_length(data.s + i); if (dns_domain_equal(data.s + i,q)) return i + j; i += j; i += 64; } return -1; }
static int want(const char *owner,const char type[2]) { unsigned int pos; static char *d; char x[10]; uint16 datalen; pos = dns_packet_skipname(response,response_len,12); if (!pos) return 0; pos += 4; while (pos < response_len) { pos = dns_packet_getname(response,response_len,pos,&d); if (!pos) return 0; pos = dns_packet_copy(response,response_len,pos,x,10); if (!pos) return 0; if (dns_domain_equal(d,owner)) if (byte_equal(type,2,x)) return 0; uint16_unpack_big(x + 8,&datalen); pos += datalen; } return 1; }
int main () { stralloc out ={0}; stralloc sa ={0}; char ip[4]; char *dn =0; stralloc_copys(&sa, "abcdefg"); dns_ip4(&out, &sa); dns_ip4_qualify(&out, &sa, &sa); dns_name4(&out, ip); dns_mx(&out, &sa); dns_txt(&out, &sa); dns_domain_length(sa.s); dns_domain_equal(sa.s, sa.s); dns_domain_copy(&dn, sa.s); dns_domain_fromdot(&dn, sa.s, sa.len); return 0; }
void axfrcheck(char *q) { int i; int j; if (!axfr) return; i = j = 0; for (;;) { if (!axfr[i] || (axfr[i] == '/')) { if (i > j) { if (!dns_domain_fromdot(&axfrok,axfr + j,i - j)) nomem(); if (dns_domain_equal(q,axfrok)) return; } j = i + 1; } if (!axfr[i]) break; ++i; } strerr_die2x(111,FATAL,"disallowed zone transfer request"); }
int main(int argc,char **argv) { unsigned long ttl; struct stat st; int i; int j; int k; char ch; if (!*argv) die_usage(); if (!*++argv) die_usage(); fn = *argv; if (!*++argv) die_usage(); fnnew = *argv; if (!*++argv) die_usage(); if (str_diff(*argv,"add")) die_usage(); if (!*++argv) die_usage(); if (str_equal(*argv,"ns")) mode = '.'; else if (str_equal(*argv,"childns")) mode = '&'; else if (str_equal(*argv,"host")) mode = '='; else if (str_equal(*argv,"host6")) mode = '6'; else if (str_equal(*argv,"alias")) mode = '+'; else if (str_equal(*argv,"alias6")) mode = '3'; else if (str_equal(*argv,"mx")) mode = '@'; else die_usage(); if (!*++argv) die_usage(); if (!dns_domain_fromdot(&target,*argv,str_len(*argv))) nomem(); if (!*++argv) die_usage(); if (mode == '6' || mode == '3') { if (!ip6_scan(*argv,targetip6)) die_usage(); } else { if (!ip4_scan(*argv,targetip)) die_usage(); } umask(077); fd = open_read(fn); if (fd == -1) die_read(); if (fstat(fd,&st) == -1) die_read(); buffer_init(&b,buffer_unixread,fd,bspace,sizeof bspace); fdnew = open_trunc(fnnew); if (fdnew == -1) die_write(); if (fchmod(fdnew,st.st_mode & 0644) == -1) die_write(); buffer_init(&bnew,buffer_unixwrite,fdnew,bnewspace,sizeof bnewspace); switch(mode) { case '.': case '&': ttl = TTL_NS; for (i = 0;i < 26;++i) { ch = 'a' + i; if (!stralloc_copyb(&f[0],&ch,1)) nomem(); if (!stralloc_cats(&f[0],".ns.")) nomem(); if (!dns_domain_todot_cat(&f[0],target)) nomem(); if (!dns_domain_fromdot(&names[i],f[0].s,f[0].len)) nomem(); } break; case '+': case '=': case '6': case '3': ttl = TTL_POSITIVE; break; case '@': ttl = TTL_POSITIVE; for (i = 0;i < 26;++i) { ch = 'a' + i; if (!stralloc_copyb(&f[0],&ch,1)) nomem(); if (!stralloc_cats(&f[0],".mx.")) nomem(); if (!dns_domain_todot_cat(&f[0],target)) nomem(); if (!dns_domain_fromdot(&names[i],f[0].s,f[0].len)) nomem(); } break; } while (match) { if (getln(&b,&line,&match,'\n') == -1) die_read(); put(line.s,line.len); if (line.len && !match) put("\n",1); while (line.len) { ch = line.s[line.len - 1]; if ((ch != ' ') && (ch != '\t') && (ch != '\n')) break; --line.len; } if (!line.len) continue; if (line.s[0] == '#') continue; j = 1; for (i = 0;i < NUMFIELDS;++i) { if (j >= line.len) { if (!stralloc_copys(&f[i],"")) nomem(); } else { k = byte_chr(line.s + j,line.len - j,':'); if (!stralloc_copyb(&f[i],line.s + j,k)) nomem(); j += k + 1; } } switch(mode) { case '.': case '&': if (line.s[0] == mode) { if (!dns_domain_fromdot(&d1,f[0].s,f[0].len)) nomem(); if (dns_domain_equal(d1,target)) { if (byte_chr(f[2].s,f[2].len,'.') >= f[2].len) { if (!stralloc_cats(&f[2],".ns.")) nomem(); if (!stralloc_catb(&f[2],f[0].s,f[0].len)) nomem(); } if (!dns_domain_fromdot(&d2,f[2].s,f[2].len)) nomem(); if (!stralloc_0(&f[3])) nomem(); if (!scan_ulong(f[3].s,&ttl)) ttl = TTL_NS; for (i = 0;i < 26;++i) if (dns_domain_equal(d2,names[i])) { used[i] = 1; break; } } } break; case '=': if (line.s[0] == '=') { if (!dns_domain_fromdot(&d1,f[0].s,f[0].len)) nomem(); if (dns_domain_equal(d1,target)) strerr_die2x(100,FATAL,"host name already used"); if (!stralloc_0(&f[1])) nomem(); if (ip4_scan(f[1].s,ip)) if (byte_equal(ip,4,targetip)) strerr_die2x(100,FATAL,"IP address already used"); } break; case '6': if (line.s[0] == '6') { if (!dns_domain_fromdot(&d1,f[0].s,f[0].len)) nomem(); if (dns_domain_equal(d1,target)) strerr_die2x(100,FATAL,"host name already used"); if (!stralloc_0(&f[1])) nomem(); if (ip6_scan(f[1].s,ip6)) if (byte_equal(ip,16,targetip6)) strerr_die2x(100,FATAL,"IPv6 address already used"); } break; case '@': if (line.s[0] == '@') { if (!dns_domain_fromdot(&d1,f[0].s,f[0].len)) nomem(); if (dns_domain_equal(d1,target)) { if (byte_chr(f[2].s,f[2].len,'.') >= f[2].len) { if (!stralloc_cats(&f[2],".mx.")) nomem(); if (!stralloc_catb(&f[2],f[0].s,f[0].len)) nomem(); } if (!dns_domain_fromdot(&d2,f[2].s,f[2].len)) nomem(); if (!stralloc_0(&f[4])) nomem(); if (!scan_ulong(f[4].s,&ttl)) ttl = TTL_POSITIVE; for (i = 0;i < 26;++i) if (dns_domain_equal(d2,names[i])) { used[i] = 1; break; } } } break; } } if (!stralloc_copyb(&f[0],&mode,1)) nomem(); if (!dns_domain_todot_cat(&f[0],target)) nomem(); if (!stralloc_cats(&f[0],":")) nomem(); if (mode == '6' || mode == '3') { if (!stralloc_catb(&f[0],ip6str,ip6_fmt_flat(ip6str,targetip6))) nomem(); } else { if (!stralloc_catb(&f[0],ipstr,ip4_fmt(ipstr,targetip))) nomem(); } switch(mode) { case '.': case '&': case '@': for (i = 0;i < 26;++i) if (!used[i]) break; if (i >= 26) strerr_die2x(100,FATAL,"too many records for that domain"); ch = 'a' + i; if (!stralloc_cats(&f[0],":")) nomem(); if (!stralloc_catb(&f[0],&ch,1)) nomem(); if (mode == '@') if (!stralloc_cats(&f[0],":")) nomem(); break; } if (!stralloc_cats(&f[0],":")) nomem(); if (!stralloc_catb(&f[0],strnum,fmt_ulong(strnum,ttl))) nomem(); if (!stralloc_cats(&f[0],"\n")) nomem(); put(f[0].s,f[0].len); if (buffer_flush(&bnew) == -1) die_write(); if (fsync(fdnew) == -1) die_write(); if (close(fdnew) == -1) die_write(); /* NFS dorks */ if (rename(fnnew,fn) == -1) strerr_die6sys(111,FATAL,"unable to move ",fnnew," to ",fn,": "); _exit(0); }
int main (int argc, char *argv[]) { char ch = 0; struct stat st; unsigned long ttl = 0; unsigned i = 0, j = 0, k = 0; prog = strdup ((d1 = strrchr (argv[0], '/')) != NULL ? d1 + 1 : argv[0]); i = check_option (argc, argv); argv += i; argc -= i; d1 = NULL; if (argc < 6) { usage (); return -1; } fn = *argv; argv++; fnnew = *argv; argv++; if (str_diff (*argv, "add")) { usage (); return -1; } argv++; if (str_equal (*argv, "ns")) mode = '.'; else if (str_equal (*argv, "childns")) mode = '&'; else if (str_equal (*argv, "host")) mode = '='; else if (str_equal (*argv, "alias")) mode = '+'; else if (str_equal (*argv, "mx")) mode = '@'; else errx (-1, "invalid record type `%s'", *argv); argv++; if (!dns_domain_fromdot (&target, *argv, str_len (*argv))) err (-1, "could not allocate enough memory"); argv++; if (!ip4_scan (*argv, targetip)) errx (-1, "could not parse IP `%s'", *argv); umask(077); fd = open_read (fn); if (fd == -1) err (-1, "could not read from `%s'", fn); if (fstat (fd, &st) == -1) err (-1, "could not read from `%s'", fn); buffer_init (&b, buffer_unixread, fd, bspace, sizeof bspace); fdnew = open_trunc (fnnew); if (fdnew == -1) err (-1, "could not write to `%s'", fnnew); if (fchmod (fdnew, st.st_mode & 0644) == -1) err (-1, "could not write to `%s'", fnnew); buffer_init (&bnew, buffer_unixwrite, fdnew, bnewspace, sizeof bnewspace); switch (mode) { case '.': case '&': ttl = TTL_NS; for (i = 0; i < 26; i++) { ch = 'a' + i; if (!stralloc_copyb (&f[0], &ch, 1)) err (-1, "could not allocate enough memory"); if (!stralloc_cats (&f[0], ".ns.")) err (-1, "could not allocate enough memory"); if (!dns_domain_todot_cat (&f[0], target)) err (-1, "could not allocate enough memory"); if (!dns_domain_fromdot (&names[i], f[0].s, f[0].len)) err (-1, "could not allocate enough memory"); } break; case '+': case '=': ttl = TTL_POSITIVE; break; case '@': ttl = TTL_POSITIVE; for (i = 0; i < 26; i++) { ch = 'a' + i; if (!stralloc_copyb (&f[0], &ch, 1)) err (-1, "could not allocate enough memory"); if (!stralloc_cats (&f[0], ".mx.")) err (-1, "could not allocate enough memory"); if (!dns_domain_todot_cat (&f[0], target)) err (-1, "could not allocate enough memory"); if (!dns_domain_fromdot (&names[i], f[0].s, f[0].len)) err (-1, "could not allocate enough memory"); } break; } while (match) { if (getln (&b, &line, &match, '\n') == -1) err (-1, "could not read from `%s'", fn); put (line.s, line.len); if (line.len && !match) put ("\n", 1); while (line.len) { ch = line.s[line.len - 1]; if ((ch != ' ') && (ch != '\t') && (ch != '\n')) break; --line.len; } if (!line.len || line.s[0] == '#') continue; j = 1; for (i = 0; i < NUMFIELDS; i++) { if (j >= line.len) { if (!stralloc_copys (&f[i], "")) err (-1, "could not allocate enough memory"); } else { k = byte_chr (line.s + j, line.len - j, ':'); if (!stralloc_copyb (&f[i], line.s + j, k)) err (-1, "could not allocate enough memory"); j += k + 1; } } switch(mode) { case '.': case '&': if (line.s[0] == mode) { if (!dns_domain_fromdot (&d1, f[0].s, f[0].len)) err (-1, "could not allocate enough memory"); if (dns_domain_equal (d1, target)) { if (byte_chr (f[2].s, f[2].len, '.') >= f[2].len) { if (!stralloc_cats (&f[2], ".ns.")) err (-1, "could not allocate enough memory"); if (!stralloc_catb (&f[2], f[0].s, f[0].len)) err (-1, "could not allocate enough memory"); } if (!dns_domain_fromdot (&d2, f[2].s, f[2].len)) err (-1, "could not allocate enough memory"); if (!stralloc_0 (&f[3])) err (-1, "could not allocate enough memory"); if (!scan_ulong (f[3].s, &ttl)) ttl = TTL_NS; for (i = 0; i < 26; i++) { if (dns_domain_equal (d2, names[i])) { used[i] = 1; break; } } } } break; case '=': if (line.s[0] == '=') { if (!dns_domain_fromdot (&d1, f[0].s, f[0].len)) err (-1, "could not allocate enough memory"); if (dns_domain_equal (d1, target)) errx (-1, "host name is already used"); if (!stralloc_0 (&f[1])) err (-1, "could not allocate enough memory"); if (ip4_scan (f[1].s, ip)) if (byte_equal(ip, 4, targetip)) errx (-1, "IP address is already used"); } break; case '@': if (line.s[0] == '@') { if (!dns_domain_fromdot (&d1, f[0].s, f[0].len)) err (-1, "could not allocate enough memory"); if (dns_domain_equal (d1, target)) { if (byte_chr (f[2].s, f[2].len, '.') >= f[2].len) { if (!stralloc_cats (&f[2], ".mx.")) err (-1, "could not allocate enough memory"); if (!stralloc_catb (&f[2], f[0].s, f[0].len)) err (-1, "could not allocate enough memory"); } if (!dns_domain_fromdot (&d2, f[2].s, f[2].len)) err (-1, "could not allocate enough memory"); if (!stralloc_0 (&f[4])) err (-1, "could not allocate enough memory"); if (!scan_ulong (f[4].s, &ttl)) ttl = TTL_POSITIVE; for (i = 0; i < 26; i++) { if (dns_domain_equal (d2, names[i])) { used[i] = 1; break; } } } } break; } } if (!stralloc_copyb (&f[0], &mode, 1)) err (-1, "could not allocate enough memory"); if (!dns_domain_todot_cat (&f[0], target)) err (-1, "could not allocate enough memory"); if (!stralloc_cats (&f[0], ":")) err (-1, "could not allocate enough memory"); if (!stralloc_catb (&f[0], ipstr, ip4_fmt (ipstr, targetip))) err (-1, "could not allocate enough memory"); switch (mode) { case '.': case '&': case '@': for (i = 0; i < 26; i++) { if (!used[i]) break; } if (i >= 26) errx (-1, "too many records for domain `%s'", target); ch = 'a' + i; if (!stralloc_cats (&f[0], ":")) err (-1, "could not allocate enough memory"); if (!stralloc_catb (&f[0], &ch, 1)) err (-1, "could not allocate enough memory"); if (mode == '@') if (!stralloc_cats (&f[0], ":")) err (-1, "could not allocate enough memory"); break; } if (!stralloc_cats (&f[0], ":")) err (-1, "could not allocate enough memory"); if (!stralloc_catb (&f[0], strnum, fmt_ulong (strnum, ttl))) err (-1, "could not allocate enough memory"); if (!stralloc_cats (&f[0], "\n")) err (-1, "could not allocate enough memory"); put (f[0].s, f[0].len); if (buffer_flush (&bnew) == -1) err (-1, "could not write to `%s'", fnnew); if (fsync (fdnew) == -1) err (-1, "could not write to `%s'", fnnew); if (close (fdnew) == -1) err (-1, "could not write to `%s'", fnnew); /* NFS dorks */ if (rename (fnnew, fn) == -1) err (-1, "could not move `%s' to `%s'", fnnew, fn); return 0; }
unsigned int printrecord_cat(stralloc *out,char *buf,unsigned int len,unsigned int pos,char *q,char qtype[2]) { char *x; char misc[20]; uint16 datalen; uint16 u16; uint32 u32; unsigned int newpos; int i; unsigned char ch; pos = dns_packet_getname(buf,len,pos,&d); if (!pos) return 0; pos = dns_packet_copy(buf,len,pos,misc,10); if (!pos) return 0; uint16_unpack_big(misc + 8,&datalen); newpos = pos + datalen; if (q) { if (!dns_domain_equal(d,q)) return newpos; if (byte_diff(qtype,2,misc) && byte_diff(qtype,2,DNS_T_ANY)) return newpos; } if (!dns_domain_todot_cat(out,d)) return 0; if (!stralloc_cats(out," ")) return 0; uint32_unpack_big(misc + 4,&u32); if (!stralloc_catulong0(out,u32,0)) return 0; if (byte_diff(misc + 2,2,DNS_C_IN)) { if (!stralloc_cats(out," weird class\n")) return 0; return newpos; } x = 0; if (byte_equal(misc,2,DNS_T_NS)) x = " NS "; if (byte_equal(misc,2,DNS_T_PTR)) x = " PTR "; if (byte_equal(misc,2,DNS_T_CNAME)) x = " CNAME "; if (x) { pos = dns_packet_getname(buf,len,pos,&d); if (!pos) return 0; if (!stralloc_cats(out,x)) return 0; if (!dns_domain_todot_cat(out,d)) return 0; } else if (byte_equal(misc,2,DNS_T_MX)) { if (!stralloc_cats(out," MX ")) return 0; pos = dns_packet_copy(buf,len,pos,misc,2); if (!pos) return 0; pos = dns_packet_getname(buf,len,pos,&d); if (!pos) return 0; uint16_unpack_big(misc,&u16); if (!stralloc_catulong0(out,u16,0)) return 0; if (!stralloc_cats(out," ")) return 0; if (!dns_domain_todot_cat(out,d)) return 0; } else if (byte_equal(misc,2,DNS_T_SOA)) { if (!stralloc_cats(out," SOA ")) return 0; pos = dns_packet_getname(buf,len,pos,&d); if (!pos) return 0; if (!dns_domain_todot_cat(out,d)) return 0; if (!stralloc_cats(out," ")) return 0; pos = dns_packet_getname(buf,len,pos,&d); if (!pos) return 0; if (!dns_domain_todot_cat(out,d)) return 0; pos = dns_packet_copy(buf,len,pos,misc,20); if (!pos) return 0; for (i = 0;i < 5;++i) { if (!stralloc_cats(out," ")) return 0; uint32_unpack_big(misc + 4 * i,&u32); if (!stralloc_catulong0(out,u32,0)) return 0; } } else if (byte_equal(misc,2,DNS_T_A)) { if (datalen != 4) { errno = error_proto; return 0; } if (!stralloc_cats(out," A ")) return 0; pos = dns_packet_copy(buf,len,pos,misc,4); if (!pos) return 0; for (i = 0;i < 4;++i) { ch = misc[i]; if (i) if (!stralloc_cats(out,".")) return 0; if (!stralloc_catulong0(out,ch,0)) return 0; } } else if (byte_equal(misc,2,DNS_T_AAAA)) { char ip6str[IP6_FMT]; int stringlen; if (datalen != 16) { errno = error_proto; return 0; } if (!stralloc_cats(out," AAAA ")) return 0; pos = dns_packet_copy(buf,len,pos,misc,16); if (!pos) return 0; stringlen=ip6_fmt(ip6str,misc); if (!stralloc_cats(out,ip6str)) return 0; } else { if (!stralloc_cats(out," ")) return 0; uint16_unpack_big(misc,&u16); if (!stralloc_catulong0(out,u16,0)) return 0; if (!stralloc_cats(out," ")) return 0; while (datalen--) { pos = dns_packet_copy(buf,len,pos,misc,1); if (!pos) return 0; if ((misc[0] >= 33) && (misc[0] <= 126) && (misc[0] != '\\')) { if (!stralloc_catb(out,misc,1)) return 0; } else { ch = misc[0]; misc[3] = '0' + (7 & ch); ch >>= 3; misc[2] = '0' + (7 & ch); ch >>= 3; misc[1] = '0' + (7 & ch); misc[0] = '\\'; if (!stralloc_catb(out,misc,4)) return 0; } } } if (!stralloc_cats(out,"\n")) return 0; if (pos != newpos) { errno = error_proto; return 0; } return newpos; }