23#define TTL_POSITIVE 86400
24#define TTL_NEGATIVE 2560
27#define WHO "tinydns-data"
29int rename(
const char *,
const char *);
33 logmsg(
WHO,111,FATAL,
"unable to create data.tmp");
38 logmsg(
WHO,111,FATAL,
"nomem");
47 for (i = 0; (i < 16) && (i < sa->
len); ++i) {
49 if ((ch >=
'0') && (ch <=
'9'))
51 else if ((ch >=
'a') && (ch <=
'f'))
55 if (!(i & 1)) ch <<= 4;
62 loc[0] = (sa->len > 0) ? sa->s[0] : 0;
63 loc[1] = (sa->len > 1) ? sa->s[1] : 0;
75 if (!stralloc_copys(
out,
""))
nomem();
76 if (!str_len(s))
return;
78 if (str_chr(s,
':') < str_len(s)) {
79 ip6_cidr(s,
ip6,&plen);
80 ip6_bytestring(
out,
ip6,plen);
81 }
else if (str_chr(s,
'.') < str_len(s)) {
82 ip4_cidr(s,
ip4,&plen);
83 ip4_bytestring(
out,
ip4,plen);
97 if ((ch >=
'0') && (ch <=
'9')) ch -= 48;
98 if ((ch >=
'a') && (ch <=
'f')) ch -= 87;
101 if ((lo >=
'0') && (lo <=
'9')) lo -= 48;
102 if ((lo >=
'a') && (lo <=
'f')) lo -= 87;
117 while (i < sa->
len) {
120 if (i >= sa->len)
break;
122 if ((ch >=
'0') && (ch <=
'7')) {
124 if ((i < sa->
len) && (sa->s[i] >=
'0') && (sa->s[i] <=
'7')) {
126 ch += sa->s[i++] -
'0';
127 if ((i < sa->
len) && (sa->s[i] >=
'0') && (sa->s[i] <=
'7')) {
129 ch += sa->s[i++] -
'0';
147 while (i < sa->
len) {
149 if (ch >=
' ' && ch <=
'~')
150 { sa->s[j] = ch; j++; }
161 if (fstat(
fd,&st) == -1)
162 logmsg(
WHO,111,FATAL,
"unable to stat data");
166 byte_copy(
defaultsoa + 4,16,
"\0\0\100\000\0\0\010\000\0\020\000\000\0\0\012\000");
172static stralloc ipstring;
173static stralloc result;
185void rr_start(
const char type[2],
unsigned long ttl,
const char ttd[8],
const char loc[2])
188 if (!stralloc_copyb(&result,
type,2))
nomem();
189 if (byte_equal(loc,2,
"\0\0"))
195 uint32_pack_big(
buf,ttl);
202 if (byte_equal(
owner,2,
"\1*")) {
207 case_lowerb(key.s,key.len);
208 if (cdb_make_add(&
cdb,key.s,key.len,result.s,result.len) == -1)
232 logmsg(
WHO,111,FATAL,B(
"unable to parse data line: ",
strnum,why));
235static unsigned int scan_u32(
const char *s,uint32 *u)
238 unsigned int r = scan_ulong(s,&l);
239 if ((uint32)l != l)
return 0;
259 unsigned char us, sl, ty;
262 fddata = open_read(
"data");
264 logmsg(
WHO,111,FATAL,
"unable to open data");
267 buffer_init(&
b,buffer_unixread,fddata,
bspace,
sizeof(
bspace));
269 fdcdb = open_trunc(
"data.tmp");
276 logmsg(
WHO,111,FATAL,
"unable to read line");
280 if ((ch !=
' ') && (ch !=
'\t') && (ch !=
'\n'))
break;
283 if (!
line.len)
continue;
284 if (
line.s[0] ==
'#')
continue;
285 if (
line.s[0] ==
'-')
continue;
291 if (!stralloc_copys(&f[i],
""))
nomem();
294 k = byte_chr(
line.s + j,
line.len - j,
'|');
295 if (!stralloc_copyb(&f[i],
line.s + j,k))
nomem();
304 if (!stralloc_copyb(&key,
"\0%",2))
nomem();
305 if (!stralloc_0(&f[1]))
nomem();
307 if (ipstring.len > 1)
308 if (!stralloc_catb(&key,ipstring.s,ipstring.len - 1))
nomem();
309 if (cdb_make_add(&
cdb,key.s,key.len,loc,2) == -1)
316 if (!stralloc_0(&f[3]))
nomem();
317 if (!scan_u32(f[3].s,&
u)) uint32_unpack_big(
defaultsoa,&
u);
318 uint32_pack_big(soa,
u);
319 if (!stralloc_0(&f[4]))
nomem();
320 if (!scan_u32(f[4].s,&
u)) uint32_unpack_big(
defaultsoa + 4,&
u);
321 uint32_pack_big(soa + 4,
u);
322 if (!stralloc_0(&f[5]))
nomem();
323 if (!scan_u32(f[5].s,&
u)) uint32_unpack_big(
defaultsoa + 8,&
u);
324 uint32_pack_big(soa + 8,
u);
325 if (!stralloc_0(&f[6]))
nomem();
326 if (!scan_u32(f[6].s,&
u)) uint32_unpack_big(
defaultsoa + 12,&
u);
327 uint32_pack_big(soa + 12,
u);
328 if (!stralloc_0(&f[7]))
nomem();
329 if (!scan_u32(f[7].s,&
u)) uint32_unpack_big(
defaultsoa + 16,&
u);
330 uint32_pack_big(soa + 16,
u);
332 if (!stralloc_0(&f[8]))
nomem();
348 if (!stralloc_0(&f[3]))
nomem();
349 if (!scan_ulong(f[3].s,&ttl)) ttl =
TTL_NS;
353 if (!stralloc_0(&f[1]))
nomem();
355 if (byte_chr(f[2].s,f[2].
len,
'.') >= f[2].
len) {
356 if (!stralloc_cats(&f[2],
".ns."))
nomem();
357 if (!stralloc_catb(&f[2],f[0].s,f[0].
len))
nomem();
361 if (
line.s[0] ==
'.') {
364 rr_add(
"\12hostmaster",11);
374 if (byte_chr(f[1].s,f[1].
len,
':') < f[1].
len) {
375 if (ip6_scan(f[1].s,
ip6)) {
381 if (ip4_scan(f[1].s,
ip4)) {
391 if (!stralloc_0(&f[2]))
nomem();
396 if (!stralloc_0(&f[1]))
nomem();
398 if (ip4_scan(f[1].s,
ip4)) {
403 if (
line.s[0] ==
'=') {
414 if (!stralloc_0(&f[2]))
nomem();
419 if (!stralloc_0(&f[1]))
nomem();
421 if (ip6_scan(f[1].s,
ip6)) {
426 if (
line.s[0] ==
':') {
437 if (!stralloc_0(&f[4]))
nomem();
442 if (!stralloc_0(&f[1]))
nomem();
444 if (byte_chr(f[2].s,f[2].
len,
'.') >= f[2].
len) {
445 if (!stralloc_cats(&f[2],
".mx."))
nomem();
446 if (!stralloc_catb(&f[2],f[0].s,f[0].
len))
nomem();
450 if (!stralloc_0(&f[3]))
nomem();
451 if (!scan_u32(f[3].s,&
u))
u = 0;
454 uint16_pack_big(
buf,
u);
459 if (byte_chr(f[1].s,f[1].
len,
':') < f[1].
len) {
460 if (ip6_scan(f[1].s,
ip6))
461 if (!ip6_isv4mapped(
ip6)) {
467 if (ip4_scan(f[1].s,
ip4)) {
477 if (!stralloc_0(&f[2]))
nomem();
482 if (
line.s[0] ==
'C')
492 if (!stralloc_0(&f[2]))
nomem();
501 while (i < f[1].
len) {
517 if (!stralloc_0(&f[7]))
nomem();
525 if (!stralloc_copys(&f[8],
"v=DKIM1\; "))
nomem();
528 if (!stralloc_cats(&f[8],
"k=rsa\; "))
nomem();
530 if (!stralloc_cats(&f[8],
"k="))
nomem();
531 if (!stralloc_catb(&f[8],f[3].s,f[3].
len))
nomem();
532 if (!stralloc_cats(&f[8],
"\; "))
nomem();
536 if (!stralloc_cats(&f[8],
"h=sha256\; "))
nomem();
538 if (!stralloc_cats(&f[8],
"h="))
nomem();
539 if (!stralloc_catb(&f[8],f[4].s,f[4].
len))
nomem();
540 if (!stralloc_cats(&f[8],
"\; "))
nomem();
544 if (!stralloc_cats(&f[8],
"s="))
nomem();
545 if (!stralloc_catb(&f[8],f[5].s,f[5].
len))
nomem();
546 if (!stralloc_cats(&f[8],
"\; "))
nomem();
550 if (!stralloc_cats(&f[8],
"t="))
nomem();
551 if (!stralloc_catb(&f[8],f[6].s,f[6].
len))
nomem();
552 if (!stralloc_cats(&f[8],
"\; "))
nomem();
559 if (!stralloc_cats(&f[8],
"p="))
nomem();
560 if (!stralloc_catb(&f[8],f[1].s,f[1].
len))
nomem();
566 if (!stralloc_copyb(&f[9],f[2].s,f[2].
len))
nomem();
567 if (!stralloc_cats(&f[9],
"."))
nomem();
569 if (!stralloc_cats(&f[9],
"default."))
nomem();
571 if (!stralloc_cats(&f[9],
"_domainkey."))
nomem();
572 if (!stralloc_catb(&f[9],f[0].s,f[0].
len))
nomem();
578 while (i < f[8].
len) {
594 if (!stralloc_0(&f[7]))
nomem();
599 if (!stralloc_0(&f[1]))
nomem();
600 if (!scan_uint(f[1].s,&us)) us = 003;
601 if (!stralloc_0(&f[2]))
nomem();
602 if (!scan_uint(f[2].s,&sl)) sl = 001;
605 if (f[3].
len == 64) ty = 001;
606 if (f[3].
len == 128) ty = 002;
608 if (f[4].
len == 0 && f[5].
len == 0 && f[6].
len == 0) {
609 if (!stralloc_copys(&f[2],
"_25._tcp.mail."))
nomem();
610 if (!stralloc_catb(&f[2],f[0].s,f[0].
len))
nomem();
611 }
else if (f[4].s[0] !=
'_') {
612 if (!stralloc_copys(&f[2],
"_"))
nomem();
614 if (!stralloc_catb(&f[2],f[5].s,f[5].
len))
nomem();
616 if (!stralloc_cats(&f[2],
"25"))
nomem();
617 if (!stralloc_cats(&f[2],
"._"))
nomem();
619 if (!stralloc_catb(&f[2],f[6].s,f[6].
len))
nomem();
621 if (!stralloc_cats(&f[2],
"tcp"))
nomem();
622 if (f[4].s[0] !=
'.')
623 if (!stralloc_cats(&f[2],
"."))
nomem();
624 if (!stralloc_catb(&f[2],f[4].s,f[4].
len))
nomem();
625 if (!stralloc_cats(&f[2],
"."))
nomem();
626 if (!stralloc_catb(&f[2],f[0].s,f[0].
len))
nomem();
628 if (!stralloc_copy(&f[2],&f[4]))
nomem();
641 case_lowerb(f[3].s,f[3].
len);
644 while (i < f[3].
len) {
657 if (!stralloc_0(&f[3]))
nomem();
662 if (!stralloc_0(&f[1]))
nomem();
664 uint16_pack_big(
type,
u);
667 if (byte_equal(
type,2,
"\0\0"))
695 if (
rename(
"data.tmp",
"data.cdb") == -1)
696 logmsg(
WHO,111,FATAL,
"unable to move data.tmp to data.cdb");
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)