djbdnscurve6  38
djbdnscurve6
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 
19 int rename(const char *,const char *); // keep compiler silent
20 
21 void nomem(void)
22 {
23  logmsg(WHO,111,FATAL,"out of memory");
24 }
25 
26 void parserr(void)
27 {
28  logmsg(WHO,111,FATAL,"parsing error");
29 }
30 
31 int fd;
32 buffer b;
33 char bspace[1024];
34 
35 int fdcdb;
36 struct cdb_make cdb;
37 
38 static stralloc line;
39 int match = 1;
40 unsigned long linenum = 0;
41 
42 char strnum[FMT_ULONG];
43 
44 void 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 }
49 void die_datatmp(void)
50 {
51  logmsg(WHO,111,FATAL,"unable to create data.tmp");
52 }
53 
54 int 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 
108  if (!stralloc_0(&line)) nomem();
109 
110  flagip6 = 0;
111  k = 0;
112  i = byte_chr(line.s + 1,line.len - 1,':');
113  if (i < line.len - 1) flagip6 = 1;
114  if (byte_equal(line.s,7,V4MAPPREFIX)) { k = 7; flagip6 = 0; }
115 
116  if (flagip6) {
117  byte_zero(ip6,16);
118  ip6_cidr(line.s,ip6,&plen);
119  if (!stralloc_copys(&tmp,"^")) nomem();
120  if (ip6_bytestring(&ipstring,ip6,&plen)) nomem();
121  if (!stralloc_catb(&tmp,ipstring.s,plen)) nomem();
122  plen++;
123  } else {
124  byte_zero(ip4,4);
125  ip4_cidr(line.s+k,ip4,&plen);
126  if (!stralloc_copys(&tmp,"")) nomem();
127  if (ip4_bytestring(&ipstring,ip4,&plen)) nomem();
128  if (!stralloc_catb(&tmp,ipstring.s,plen)) nomem();
129  }
130 
131  if (cdb_make_add(&cdb,tmp.s,plen,"",0) == -1)
132  die_datatmp();
133  break;
134  }
135  }
136 
137  if (cdb_make_finish(&cdb) == -1) die_datatmp();
138  if (fsync(fdcdb) == -1) die_datatmp();
139  if (close(fdcdb) == -1) die_datatmp(); /* NFS stupidity */
140  if (rename("data.tmp","data.cdb") == -1)
141  logmsg(WHO,111,FATAL,"unable to move data.tmp to data.cdb");
142 
143  _exit(0);
144 }
stralloc line
Definition: axfr-get.c:126
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