22#define TTL_POSITIVE 86400
23#define TTL_NEGATIVE 2560
26#define WHO "tinydns-data"
28int rename(
const char *,
const char *);
32 logmsg(
WHO,111,FATAL,
"unable to create data.tmp");
37 logmsg(
WHO,111,FATAL,
"nomem");
46 for (i = 0; (i < 16) && (i < sa->
len); ++i) {
48 if ((ch >=
'0') && (ch <=
'9'))
50 else if ((ch >=
'a') && (ch <=
'f'))
54 if (!(i & 1)) ch <<= 4;
61 loc[0] = (sa->len > 0) ? sa->s[0] : 0;
62 loc[1] = (sa->len > 1) ? sa->s[1] : 0;
74 if (!stralloc_copys(
out,
""))
nomem();
75 if (!str_len(s))
return;
77 if (str_chr(s,
':') < str_len(s)) {
78 ip6_cidr(s,
ip6,&plen);
79 ip6_bytestring(
out,
ip6,plen);
80 }
else if (str_chr(s,
'.') < str_len(s)) {
81 ip4_cidr(s,
ip4,&plen);
82 ip4_bytestring(
out,
ip4,plen);
96 if ((ch >=
'0') && (ch <=
'9')) ch -= 48;
97 if ((ch >=
'a') && (ch <=
'f')) ch -= 87;
100 if ((lo >=
'0') && (lo <=
'9')) lo -= 48;
101 if ((lo >=
'a') && (lo <=
'f')) lo -= 87;
116 while (i < sa->
len) {
119 if (i >= sa->len)
break;
121 if ((ch >=
'0') && (ch <=
'7')) {
123 if ((i < sa->
len) && (sa->s[i] >=
'0') && (sa->s[i] <=
'7')) {
125 ch += sa->s[i++] -
'0';
126 if ((i < sa->
len) && (sa->s[i] >=
'0') && (sa->s[i] <=
'7')) {
128 ch += sa->s[i++] -
'0';
146 while (i < sa->
len) {
148 if (ch >=
' ' && ch <=
'~')
149 { sa->s[j] = ch; j++; }
160 if (fstat(
fd,&st) == -1)
161 logmsg(
WHO,111,FATAL,
"unable to stat data");
165 byte_copy(
defaultsoa + 4,16,
"\0\0\100\000\0\0\010\000\0\020\000\000\0\0\012\000");
171static stralloc ipstring;
172static stralloc result;
184void rr_start(
const char type[2],
unsigned long ttl,
const char ttd[8],
const char loc[2])
187 if (!stralloc_copyb(&result,
type,2))
nomem();
188 if (byte_equal(loc,2,
"\0\0"))
194 uint32_pack_big(
buf,ttl);
201 if (byte_equal(
owner,2,
"\1*")) {
206 case_lowerb(key.s,key.len);
207 if (cdb_make_add(&
cdb,key.s,key.len,result.s,result.len) == -1)
231 logmsg(
WHO,111,FATAL,B(
"unable to parse data line: ",
strnum,why));
250 unsigned char us, sl, ty;
253 fddata = open_read(
"data");
255 logmsg(
WHO,111,FATAL,
"unable to open data");
258 buffer_init(&
b,buffer_unixread,fddata,
bspace,
sizeof(
bspace));
260 fdcdb = open_trunc(
"data.tmp");
267 logmsg(
WHO,111,FATAL,
"unable to read line");
271 if ((ch !=
' ') && (ch !=
'\t') && (ch !=
'\n'))
break;
274 if (!
line.len)
continue;
275 if (
line.s[0] ==
'#')
continue;
276 if (
line.s[0] ==
'-')
continue;
282 if (!stralloc_copys(&f[i],
""))
nomem();
285 k = byte_chr(
line.s + j,
line.len - j,
'|');
286 if (!stralloc_copyb(&f[i],
line.s + j,k))
nomem();
295 if (!stralloc_copyb(&key,
"\0%",2))
nomem();
296 if (!stralloc_0(&f[1]))
nomem();
298 if (ipstring.len > 1)
299 if (!stralloc_catb(&key,ipstring.s,ipstring.len - 1))
nomem();
300 if (cdb_make_add(&
cdb,key.s,key.len,loc,2) == -1)
307 if (!stralloc_0(&f[3]))
nomem();
308 if (!scan_dnum(f[3].s,&
u)) uint32_unpack_big(
defaultsoa,&
u);
309 uint32_pack_big(soa,
u);
310 if (!stralloc_0(&f[4]))
nomem();
311 if (!scan_dnum(f[4].s,&
u)) uint32_unpack_big(
defaultsoa + 4,&
u);
312 uint32_pack_big(soa + 4,
u);
313 if (!stralloc_0(&f[5]))
nomem();
314 if (!scan_dnum(f[5].s,&
u)) uint32_unpack_big(
defaultsoa + 8,&
u);
315 uint32_pack_big(soa + 8,
u);
316 if (!stralloc_0(&f[6]))
nomem();
317 if (!scan_dnum(f[6].s,&
u)) uint32_unpack_big(
defaultsoa + 12,&
u);
318 uint32_pack_big(soa + 12,
u);
319 if (!stralloc_0(&f[7]))
nomem();
320 if (!scan_dnum(f[7].s,&
u)) uint32_unpack_big(
defaultsoa + 16,&
u);
321 uint32_pack_big(soa + 16,
u);
323 if (!stralloc_0(&f[8]))
nomem();
339 if (!stralloc_0(&f[3]))
nomem();
340 if (!scan_dnum(f[3].s,&ttl)) ttl =
TTL_NS;
344 if (!stralloc_0(&f[1]))
nomem();
346 if (byte_chr(f[2].s,f[2].
len,
'.') >= f[2].
len) {
347 if (!stralloc_cats(&f[2],
".ns."))
nomem();
348 if (!stralloc_catb(&f[2],f[0].s,f[0].
len))
nomem();
352 if (
line.s[0] ==
'.') {
355 rr_add(
"\12hostmaster",11);
365 if (byte_chr(f[1].s,f[1].
len,
':') < f[1].
len) {
366 if (ip6_scan(f[1].s,
ip6)) {
372 if (ip4_scan(f[1].s,
ip4)) {
382 if (!stralloc_0(&f[2]))
nomem();
387 if (!stralloc_0(&f[1]))
nomem();
389 if (ip4_scan(f[1].s,
ip4)) {
394 if (
line.s[0] ==
'=') {
405 if (!stralloc_0(&f[2]))
nomem();
410 if (!stralloc_0(&f[1]))
nomem();
412 if (ip6_scan(f[1].s,
ip6)) {
417 if (
line.s[0] ==
':') {
428 if (!stralloc_0(&f[4]))
nomem();
433 if (!stralloc_0(&f[1]))
nomem();
435 if (byte_chr(f[2].s,f[2].
len,
'.') >= f[2].
len) {
436 if (!stralloc_cats(&f[2],
".mx."))
nomem();
437 if (!stralloc_catb(&f[2],f[0].s,f[0].
len))
nomem();
441 if (!stralloc_0(&f[3]))
nomem();
442 if (!scan_dnum(f[3].s,&
u))
u = 0;
445 uint16_pack_big(
buf,
u);
450 if (byte_chr(f[1].s,f[1].
len,
':') < f[1].
len) {
451 if (ip6_scan(f[1].s,
ip6))
452 if (!ip6_isv4mapped(
ip6)) {
458 if (ip4_scan(f[1].s,
ip4)) {
468 if (!stralloc_0(&f[2]))
nomem();
473 if (
line.s[0] ==
'C')
483 if (!stralloc_0(&f[2]))
nomem();
492 while (i < f[1].
len) {
508 if (!stralloc_0(&f[7]))
nomem();
516 if (!stralloc_copys(&f[8],
"v=DKIM1;"))
nomem();
519 if (!stralloc_cats(&f[8],
"k=rsa;"))
nomem();
521 if (!stralloc_cats(&f[8],
"k="))
nomem();
522 if (!stralloc_catb(&f[8],f[3].s,f[3].
len))
nomem();
523 if (!stralloc_cats(&f[8],
";"))
nomem();
527 if (!stralloc_cats(&f[8],
"h=sha256;"))
nomem();
529 if (!stralloc_cats(&f[8],
"h="))
nomem();
530 if (!stralloc_catb(&f[8],f[4].s,f[4].
len))
nomem();
531 if (!stralloc_cats(&f[8],
";"))
nomem();
535 if (!stralloc_cats(&f[8],
"s="))
nomem();
536 if (!stralloc_catb(&f[8],f[5].s,f[5].
len))
nomem();
537 if (!stralloc_cats(&f[8],
";"))
nomem();
541 if (!stralloc_cats(&f[8],
"t="))
nomem();
542 if (!stralloc_catb(&f[8],f[6].s,f[6].
len))
nomem();
543 if (!stralloc_cats(&f[8],
";"))
nomem();
550 if (!stralloc_cats(&f[8],
"p="))
nomem();
551 if (!stralloc_catb(&f[8],f[1].s,f[1].
len))
nomem();
557 if (!stralloc_copyb(&f[9],f[2].s,f[2].
len))
nomem();
558 if (!stralloc_cats(&f[9],
"."))
nomem();
560 if (!stralloc_cats(&f[9],
"default."))
nomem();
562 if (!stralloc_cats(&f[9],
"_domainkey."))
nomem();
563 if (!stralloc_catb(&f[9],f[0].s,f[0].
len))
nomem();
569 while (i < f[8].
len) {
585 if (!stralloc_0(&f[7]))
nomem();
590 if (!stralloc_0(&f[1]))
nomem();
591 if (!scan_uint(f[1].s,(uint *)&us)) us = 003;
592 if (!stralloc_0(&f[2]))
nomem();
593 if (!scan_uint(f[2].s,(uint *)&sl)) sl = 001;
596 if (f[3].
len == 64) ty = 001;
597 if (f[3].
len == 128) ty = 002;
599 if (f[4].
len == 0 && f[5].
len == 0 && f[6].
len == 0) {
600 if (!stralloc_copys(&f[2],
"_25._tcp.mail."))
nomem();
601 if (!stralloc_catb(&f[2],f[0].s,f[0].
len))
nomem();
602 }
else if (f[4].s[0] !=
'_') {
603 if (!stralloc_copys(&f[2],
"_"))
nomem();
605 if (!stralloc_catb(&f[2],f[5].s,f[5].
len))
nomem();
607 if (!stralloc_cats(&f[2],
"25"))
nomem();
608 if (!stralloc_cats(&f[2],
"._"))
nomem();
610 if (!stralloc_catb(&f[2],f[6].s,f[6].
len))
nomem();
612 if (!stralloc_cats(&f[2],
"tcp"))
nomem();
613 if (f[4].s[0] !=
'.')
614 if (!stralloc_cats(&f[2],
"."))
nomem();
615 if (!stralloc_catb(&f[2],f[4].s,f[4].
len))
nomem();
616 if (!stralloc_cats(&f[2],
"."))
nomem();
617 if (!stralloc_catb(&f[2],f[0].s,f[0].
len))
nomem();
619 if (!stralloc_copy(&f[2],&f[4]))
nomem();
632 case_lowerb(f[3].s,f[3].
len);
635 while (i < f[3].
len) {
648 if (!stralloc_0(&f[3]))
nomem();
653 if (!stralloc_0(&f[1]))
nomem();
654 scan_dnum(f[1].s,&
u);
655 uint16_pack_big(
type,
u);
658 if (byte_equal(
type,2,
"\0\0"))
682 if (!stralloc_0(&f[3]))
nomem();
687 if (!stralloc_0(&f[1]))
nomem();
688 if (ip4_scan(f[1].s,
ip4)) {
690 if (f[2].
len) ip6_scan(f[2].s,ip64 + 4);
691 byte_copy(ip64 + 12,4,
ip4);
697 if (
line.s[0] ==
'X') {
714 if (
rename(
"data.tmp",
"data.cdb") == -1)
715 logmsg(
WHO,111,FATAL,
"unable to move data.tmp to data.cdb");
#define IP6_64PREFIX_GLOBAL
int dns_domain_fromdot(char **out, const char *buf, unsigned int n)
unsigned int dns_domain_length(const char *dn)
int dns_name4_domain(char name[DNS_NAME4_DOMAIN], const char ip[4])
int dns_name6_domain(char name[DNS_NAME6_DOMAIN], const char ip[16])
void owner(int uid, int gid)
void out(const char *s, unsigned int len)
void d(const char *home, const char *subdir, int uid, int gid, int mode)
void ipprefix_get(stralloc *out, char *s)
void textparse(stralloc *sa)
void ttdparse(stralloc *sa, char ttd[8])
void rr_addname(const char *d)
void hexparse(stralloc *sa)
int rename(const char *, const char *)
char d4ptr[DNS_NAME4_DOMAIN]
void locparse(stralloc *sa, char loc[2])
char d6ptr[DNS_NAME6_DOMAIN]
void rr_add(const char *buf, unsigned int len)
void syntaxerror(const char *why)
void txtparse(stralloc *sa)
void rr_start(const char type[2], unsigned long ttl, const char ttd[8], const char loc[2])
void defaultsoa_init(int fd)
void rr_finish(const char *owner)