22 #define TTL_POSITIVE 86400
23 #define TTL_NEGATIVE 2560
25 #define WHO "tinydns-data"
27 int rename(
const char *,
const char *);
31 logmsg(
WHO,111,FATAL,
"unable to create data.tmp");
36 logmsg(
WHO,111,FATAL,
"nomem");
45 for (i = 0; (i < 16) && (i < sa->
len); ++i) {
47 if ((ch >=
'0') && (ch <=
'9'))
49 else if ((ch >=
'a') && (ch <=
'f'))
53 if (!(i & 1)) ch <<= 4;
60 loc[0] = (sa->len > 0) ? sa->s[0] : 0;
61 loc[1] = (sa->len > 1) ? sa->s[1] : 0;
72 if (!stralloc_catb(
out,
"s",1) || !stralloc_cats(
out,s))
nomem();
75 if (!stralloc_catb(
out,
"f",1))
nomem();
83 if (!stralloc_catb(
out,&ch,1))
nomem();
98 if ((ch >=
'0') && (ch <=
'9')) ch -= 48;
99 if ((ch >=
'a') && (ch <=
'f')) ch -= 87;
102 if ((lo >=
'0') && (lo <=
'9')) lo -= 48;
103 if ((lo >=
'a') && (lo <=
'f')) lo -= 87;
118 while (i < sa->
len) {
121 if (i >= sa->len)
break;
123 if ((ch >=
'0') && (ch <=
'7')) {
125 if ((i < sa->
len) && (sa->s[i] >=
'0') && (sa->s[i] <=
'7')) {
127 ch += sa->s[i++] -
'0';
128 if ((i < sa->
len) && (sa->s[i] >=
'0') && (sa->s[i] <=
'7')) {
130 ch += sa->s[i++] -
'0';
148 while (i < sa->
len) {
150 if (ch >=
' ' && ch <=
'~')
151 { sa->s[j] = ch; j++; }
162 if (fstat(
fd,&st) == -1)
163 logmsg(
WHO,111,FATAL,
"unable to stat data");
167 byte_copy(
defaultsoa + 4,16,
"\0\0\100\000\0\0\010\000\0\020\000\000\0\0\012\000");
173 static stralloc result;
185 void 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)
215 static stralloc
line;
232 logmsg(
WHO,111,FATAL,B(
"unable to parse data line: ",
strnum,why));
235 static 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;
292 if (!stralloc_copys(&f[i],
""))
nomem();
295 k = byte_chr(
line.s + j,
line.len - j,
'|');
296 if (!stralloc_copyb(&f[i],
line.s + j,k))
nomem();
305 if (!stralloc_copyb(&key,
"\0%",2))
nomem();
306 if (!stralloc_0(&f[1]))
nomem();
308 if (cdb_make_add(&
cdb,key.s,key.len,loc,2) == -1)
315 if (!stralloc_0(&f[3]))
nomem();
316 if (!scan_u32(f[3].s,&
u)) uint32_unpack_big(
defaultsoa,&
u);
317 uint32_pack_big(soa,
u);
318 if (!stralloc_0(&f[4]))
nomem();
319 if (!scan_u32(f[4].s,&
u)) uint32_unpack_big(
defaultsoa + 4,&
u);
320 uint32_pack_big(soa + 4,
u);
321 if (!stralloc_0(&f[5]))
nomem();
322 if (!scan_u32(f[5].s,&
u)) uint32_unpack_big(
defaultsoa + 8,&
u);
323 uint32_pack_big(soa + 8,
u);
324 if (!stralloc_0(&f[6]))
nomem();
325 if (!scan_u32(f[6].s,&
u)) uint32_unpack_big(
defaultsoa + 12,&
u);
326 uint32_pack_big(soa + 12,
u);
327 if (!stralloc_0(&f[7]))
nomem();
328 if (!scan_u32(f[7].s,&
u)) uint32_unpack_big(
defaultsoa + 16,&
u);
329 uint32_pack_big(soa + 16,
u);
331 if (!stralloc_0(&f[8]))
nomem();
347 if (!stralloc_0(&f[3]))
nomem();
348 if (!scan_ulong(f[3].s,&ttl)) ttl =
TTL_NS;
352 if (!stralloc_0(&f[1]))
nomem();
354 if (byte_chr(f[2].s,f[2].
len,
'.') >= f[2].
len) {
355 if (!stralloc_cats(&f[2],
".ns."))
nomem();
356 if (!stralloc_catb(&f[2],f[0].s,f[0].
len))
nomem();
360 if (
line.s[0] ==
'.') {
363 rr_add(
"\12hostmaster",11);
373 if (byte_chr(f[1].s,f[1].
len,
':') < f[1].
len) {
374 if (ip6_scan(f[1].s,
ip6)) {
380 if (ip4_scan(f[1].s,
ip4)) {
390 if (!stralloc_0(&f[2]))
nomem();
395 if (!stralloc_0(&f[1]))
nomem();
397 if (ip4_scan(f[1].s,
ip4)) {
402 if (
line.s[0] ==
'=') {
413 if (!stralloc_0(&f[2]))
nomem();
418 if (!stralloc_0(&f[1]))
nomem();
420 if (ip6_scan(f[1].s,
ip6)) {
425 if (
line.s[0] ==
':') {
436 if (!stralloc_0(&f[4]))
nomem();
441 if (!stralloc_0(&f[1]))
nomem();
443 if (byte_chr(f[2].s,f[2].
len,
'.') >= f[2].
len) {
444 if (!stralloc_cats(&f[2],
".mx."))
nomem();
445 if (!stralloc_catb(&f[2],f[0].s,f[0].
len))
nomem();
449 if (!stralloc_0(&f[3]))
nomem();
450 if (!scan_u32(f[3].s,&
u))
u = 0;
453 uint16_pack_big(
buf,
u);
458 if (byte_chr(f[1].s,f[1].
len,
':') < f[1].
len) {
459 if (ip6_scan(f[1].s,
ip6))
460 if (!ip6_isv4mapped(
ip6)) {
466 if (ip4_scan(f[1].s,
ip4)) {
476 if (!stralloc_0(&f[2]))
nomem();
481 if (
line.s[0] ==
'C')
491 if (!stralloc_0(&f[2]))
nomem();
500 while (i < f[1].
len) {
502 if (k > 127) k = 127;
516 if (!stralloc_0(&f[7]))
nomem();
521 if (!stralloc_0(&f[1]))
nomem();
522 if (!scan_uint(f[1].s,&us)) us = 3;
524 if (!stralloc_0(&f[2]))
nomem();
525 if (!scan_uint(f[2].s,&sl)) sl = 0;
529 if (f[3].
len == 64) ty = 001;
530 if (f[3].
len == 128) ty = 002;
532 if (f[4].
len == 0 && f[5].
len == 0 && f[6].
len == 0) {
533 if (!stralloc_copys(&f[2],
"_25._tcp.mail."))
nomem();
534 if (!stralloc_catb(&f[2],f[0].s,f[0].
len))
nomem();
535 }
else if (f[4].s[0] !=
'_') {
536 if (!stralloc_copys(&f[2],
"_"))
nomem();
538 if (!stralloc_catb(&f[2],f[5].s,f[5].
len))
nomem();
540 if (!stralloc_cats(&f[2],
"25"))
nomem();
541 if (!stralloc_cats(&f[2],
"._"))
nomem();
543 if (!stralloc_catb(&f[2],f[6].s,f[6].
len))
nomem();
545 if (!stralloc_cats(&f[2],
"tcp"))
nomem();
546 if (f[4].s[0] !=
'.')
547 if (!stralloc_cats(&f[2],
"."))
nomem();
548 if (!stralloc_catb(&f[2],f[4].s,f[4].
len))
nomem();
549 if (!stralloc_cats(&f[2],
"."))
nomem();
550 if (!stralloc_catb(&f[2],f[0].s,f[0].
len))
nomem();
552 if (!stralloc_copy(&f[2],&f[4]))
nomem();
565 case_lowerb(f[3].s,f[3].
len);
573 if (!stralloc_0(&f[3]))
nomem();
578 if (!stralloc_0(&f[1]))
nomem();
580 uint16_pack_big(
type,
u);
583 if (byte_equal(
type,2,
"\0\0"))
611 if (
rename(
"data.tmp",
"data.cdb") == -1)
612 logmsg(
WHO,111,FATAL,
"unable to move data.tmp to data.cdb");
int dns_domain_fromdot(char **, const char *, unsigned int)
int dns_name4_domain(char *, const char *)
unsigned int dns_domain_length(const char *)
int dns_name6_domain(char *, const char *)
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 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])
void ipprefix_cat(stralloc *out, char *s)
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)