djbdnscurve6 51
djbdnscurve6
Loading...
Searching...
No Matches
rbldns-data.c
Go to the documentation of this file.
1#include <sys/types.h>
2#include <sys/stat.h>
3#include <unistd.h>
4#include "buffer.h"
5#include "exit.h"
6#include "cdbmake.h"
7#include "open.h"
8#include "stralloc.h"
9#include "getln.h"
10#include "logmsg.h"
11#include "byte.h"
12#include "scan.h"
13#include "fmt.h"
14#include "ip.h"
15#include "str.h"
16
17#define WHO "rbldns-data"
18
19int rename(const char *,const char *); // keep compiler silent
20
21void nomem(void)
22{
23 logmsg(WHO,111,FATAL,"out of memory");
24}
25
26void parserr(void)
27{
28 logmsg(WHO,111,FATAL,"parsing error");
29}
30
31int fd;
32buffer b;
33char bspace[1024];
34
36struct cdb_make cdb;
37
38static stralloc line;
39int match = 1;
40unsigned long linenum = 0;
41
42char strnum[FMT_ULONG];
43
44void syntaxerror(const char *why)
45{
46 strnum[fmt_ulong(strnum,linenum)] = 0;
47 logmsg(WHO,-99,WARN,B("unable to parse data line: ",strnum,why));
48}
49void die_datatmp(void)
50{
51 logmsg(WHO,111,FATAL,"unable to create data.tmp");
52}
53
54int main()
55{
56 char ipout[4]; /* always return 127.0.0.x IP address */
57 unsigned int i;
58 unsigned int j;
59 unsigned int k;
60 unsigned long plen;
61 int flagip6;
62 char ch;
63 char ip4[4];
64 char ip6[16];
65 stralloc tmp = {0}; /* needs to be here, local and not global */
66 stralloc ipstring = {0};
67
68 umask(022);
69
70 fd = open_read("data");
71 if (fd == -1) logmsg(WHO,111,FATAL,"unable to open data");
72 buffer_init(&b,buffer_unixread,fd,bspace,sizeof(bspace));
73
74 fdcdb = open_trunc("data.tmp");
75 if (fdcdb == -1) die_datatmp();
76 if (cdb_make_start(&cdb,fdcdb) == -1) die_datatmp();
77
78 while (match) {
79 ++linenum;
80 if (getln(&b,&line,&match,'\n') == -1)
81 logmsg(WHO,111,FATAL,"unable to read line");
82
83 while (line.len) {
84 ch = line.s[line.len - 1];
85 if ((ch != ' ') && (ch != '\t') && (ch != '\n')) break;
86 --line.len;
87 }
88 if (!line.len) continue;
89
90 switch (line.s[0]) {
91 default:
92 syntaxerror(": unrecognized leading character");
93 case '#':
94 break;
95 case '=': /* allow IPv6 mapped IPv4 addresses */
96 j = byte_chr(line.s + 1,line.len - 1,':');
97 if (j >= line.len - 1) syntaxerror(": missing colon");
98 if (ip4_scan(line.s + 1,ipout) != j) syntaxerror(": malformed IPv4 address");
99 if (!stralloc_copyb(&tmp,ipout,4)) nomem();
100 if (!stralloc_catb(&tmp,line.s + j + 2,line.len - j - 2)) nomem();
101 if (cdb_make_add(&cdb,"",0,tmp.s,tmp.len) == -1)
102 die_datatmp();
103 break;
104 case '0': case '1': case '2': case '3': case '4':
105 case '5': case '6': case '7': case '8': case '9': case ':':
106 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
107 if (!stralloc_0(&line)) nomem();
108
109 flagip6 = 0;
110 k = 0;
111 i = byte_chr(line.s + 1,line.len - 1,':');
112 if (i < line.len - 1) flagip6 = 1;
113 if (byte_equal(line.s,7,V4MAPPREFIX)) { k = 7; flagip6 = 0; }
114
115 if (flagip6) {
116 byte_zero(ip6,16);
117 ip6_cidr(line.s,ip6,&plen);
118 if (!stralloc_copys(&tmp,"^")) nomem();
119 if (ip6_bytestring(&ipstring,ip6,plen) < 0) nomem();
120 if (!stralloc_catb(&tmp,ipstring.s,plen)) nomem();
121 plen++;
122 } else {
123 byte_zero(ip4,4);
124 ip4_cidr(line.s + k,ip4,&plen);
125 if (!stralloc_copys(&tmp,"")) nomem();
126 if (ip4_bytestring(&ipstring,ip4,plen) < 0) nomem();
127 if (!stralloc_catb(&tmp,ipstring.s,plen)) nomem();
128 }
129
130 if (cdb_make_add(&cdb,tmp.s,plen,"",0) == -1)
131 die_datatmp();
132 break;
133 }
134 }
135
136 if (cdb_make_finish(&cdb) == -1) die_datatmp();
137 if (fsync(fdcdb) == -1) die_datatmp();
138 if (close(fdcdb) == -1) die_datatmp(); /* NFS stupidity */
139 if (rename("data.tmp","data.cdb") == -1)
140 logmsg(WHO,111,FATAL,"unable to move data.tmp to data.cdb");
141
142 _exit(0);
143}
char ip6[16]
Definition: dnsfilter.c:51
char ip4[4]
Definition: dnsfilter.c:50
struct line tmp
Definition: dnsfilter.c:32
void die_datatmp(void)
Definition: rbldns-data.c:49
struct cdb_make cdb
Definition: rbldns-data.c:36
char bspace[1024]
Definition: rbldns-data.c:33
void parserr(void)
Definition: rbldns-data.c:26
int fdcdb
Definition: rbldns-data.c:35
unsigned long linenum
Definition: rbldns-data.c:40
char strnum[FMT_ULONG]
Definition: rbldns-data.c:42
int rename(const char *, const char *)
buffer b
Definition: rbldns-data.c:32
int fd
Definition: rbldns-data.c:31
void nomem(void)
Definition: rbldns-data.c:21
void syntaxerror(const char *why)
Definition: rbldns-data.c:44
int match
Definition: rbldns-data.c:39
#define WHO
Definition: rbldns-data.c:17
int main()
Definition: rbldns-data.c:54
Definition: dnsfilter.c:23