File: tinydyndns-data.c

package info (click to toggle)
tinydyndns 0.4.2.debian1-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,432 kB
  • sloc: ansic: 10,256; sh: 291; makefile: 42
file content (211 lines) | stat: -rw-r--r-- 5,182 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#include <stdio.h>
#include <unistd.h>
#include "strerr.h"
#include "error.h"
#include "cdb.h"
#include "cdb_make.h"
#include "open.h"
#include "buffer.h"
#include "stralloc.h"
#include "str.h"
#include "byte.h"
#include "dns.h"
#include "ip4.h"
#include "case.h"
#include "seek.h"
#include "tai.h"
#include "env.h"
#include "scan.h"

#define USAGE " add|remove fqdn ..."
#define FATAL "tinydyndns-data: fatal: "
#define WARNING "tinydyndns-data: warning: "
#define INFO "tinydyndns-data: info: "

char *progname;

void usage () { strerr_die4x(1, "usage: ", progname, USAGE, "\n"); }
void die_nomem() { strerr_die2x(111, FATAL, "out of memory."); }
void fatal(const char *m1, const char *m2) {
  strerr_die4sys(111, FATAL, m1, m2, ": ");
}
void warn(const char *m1, const char *m2) {
  strerr_warn3(WARNING, m1, m2, 0);
}
void info(const char *m1, const char *m2) {
  buffer_puts(buffer_1, INFO);
  buffer_puts(buffer_1, m1);
  buffer_puts(buffer_1, m2);
  buffer_putsflush(buffer_1, "\n");
}

unsigned int add =0;
unsigned int rem =0;
unsigned long ttl;
unsigned long ttd;
const char *ip;
const char *loc;

int rc =0;

int fdcdb;
int fdtmp;
stralloc sa ={0};
static char *host;
struct cdb_make c;
struct cdb cdb;
char **fqdn;
uint32 pos =0;

void get(buffer *b, char *buf, unsigned int l) {
  int r;
  while (l > 0) {
    r =buffer_get(b, buf, l);
    if (r == -1) fatal("unable to read data.cdb", 0);
    if (r == 0)
      strerr_die2x(111, FATAL, "unable to read data.cdb: truncated file");
    buf +=r;
    l -=r;
  }
}

int main(int argc, char ** argv) {
  buffer bcdb;
  char bcdbspace[4096];
  char buf[8];
  char ipbuf[4];
  uint32 eod;
  uint32 klen;
  uint32 dlen;
  stralloc key ={0};
  char *s;
  struct tai tt, plus;

  progname =*argv++;

  if (! *argv) usage();
  switch (**argv) {
  case 'a': /* add */
    add =1;
    break;
  case 'r': /* remove */
    rem =1;
    break;
  default:
    usage();
  }
  if (! *++argv) usage();

  if ((s =env_get("TTL"))) scan_ulong(s, &ttl);
  if ((s =env_get("TTD"))) scan_ulong(s, &ttd);
  if (! (ip =env_get("IP"))) ip ="127.14.14.14";
  if ((loc =env_get("LOC"))) if (! loc[0] || ! loc[1]) loc =0;

  /* open data.cdb */
  if ((fdcdb =open_read("data.cdb")) == -1)
    fatal("unable to open data.cdb", 0);
  buffer_init(&bcdb, buffer_unixread, fdcdb, bcdbspace, sizeof bcdbspace);

  /* lock data.cdb */

  /* open data.tmp */
  if ((fdtmp =open_trunc("data.tmp")) == -1)
    fatal("unable to open data.tmp", 0);
  if (cdb_make_start(&c, fdtmp) == -1)
    fatal("unable to create ok.cdb.tmp", 0);

  /* size of database */
  get(&bcdb, buf, 4);
  pos +=4;
  uint32_unpack(buf, &eod);

  while (pos < 2048) {
    get(&bcdb, buf, 4);
    pos +=4;
  }
  while (pos < eod) {
    get(&bcdb, buf, 4);
    pos +=4;
    uint32_unpack(buf, &klen);
    get(&bcdb, buf, 4);
    pos +=4;
    uint32_unpack(buf, &dlen);

    if (! stralloc_ready(&sa, klen +dlen)) die_nomem();
    get(&bcdb, sa.s, klen +dlen);
    pos +=klen +dlen;
    if (rem) {
      int skip =0;

      for (fqdn =argv; *fqdn; ++fqdn) {
	if (! dns_domain_fromdot(&host, *fqdn, str_len(*fqdn))) die_nomem();
	case_lowerb(host, dns_domain_length(host));
	if (byte_equal(host, dns_domain_length(host), sa.s)) {
	  info("remove: ", *fqdn);
	  skip =1;
	  rc--;
	  break;
	}
      }
      if (skip) continue;
    }

    if (cdb_make_add(&c, sa.s, klen, sa.s +klen, dlen) == -1)
      fatal("unable to write data.tmp", 0);
  }

  if (add) {
    seek_begin(fdcdb);
    cdb_init(&cdb, fdcdb);
    if (! ip4_scan(ip, ipbuf))
      strerr_die3x(100, FATAL, "unable to scan IP address: ", ip);
    for (fqdn =argv; *fqdn; ++fqdn) {
      if (! dns_domain_fromdot(&host, *fqdn, str_len(*fqdn))) die_nomem();
      case_lowerb(host, dns_domain_length(host));
      if (cdb_find(&cdb, host, dns_domain_length(host)) == 1) {
	warn(*fqdn, ": already in cdb.");
	rc++;
	continue;
      }
      if (! stralloc_copyb(&sa, DNS_T_A, 2)) die_nomem();
      if (loc) {
	if (! stralloc_append(&sa, ">")) die_nomem();
	if (! stralloc_catb(&sa, loc, 2)) die_nomem();
      }
      else
	if (! stralloc_append(&sa, "=")) die_nomem();
      if (! ttd && ! ttl) ttl =5;
      uint32_pack_big(buf, ttl);
      if (! stralloc_catb(&sa, buf, 4)) die_nomem(); /* ttl */
      if (ttd) {
	tai_now(&tt);
	tai_uint(&plus, ttd);
	tai_add(&tt, &tt, &plus);
	tai_pack(buf, &tt);
	if (! stralloc_catb(&sa, buf, 8)) die_nomem();
      }
      else
	if (! stralloc_catb(&sa, "\0\0\0\0\0\0\0\0", 8)) die_nomem(); /* ttd */
      if (! stralloc_catb(&sa, ipbuf, 4)) die_nomem();
      if (! stralloc_copyb(&key, host, dns_domain_length(host))) die_nomem();
      if (cdb_make_add(&c, key.s, key.len, sa.s, sa.len) == -1)
	fatal("unable to write data.tmp", 0);
      info("add: ", *fqdn);
    }
    cdb_free(&cdb);
  }
  close(fdcdb);

  if (cdb_make_finish(&c) == -1) fatal("unable to write data.tmp", 0);
  if (fsync(fdtmp) == -1) fatal("unable to write data.cdb.tmp", 0);
  close(fdtmp);
  if (rename("data.tmp","data.cdb") == -1)
    fatal("unable to move data.tmp to data.cdb", 0);

  if (rem)
    for (fqdn =argv; *fqdn; ++fqdn)
      rc++;
  /*  info("done", ""); */
  if (rc > 100) rc =100;
  _exit(rc);
}