xfrd-disk.c revision 1.1.1.1.8.1 1 /*
2 * xfrd-disk.c - XFR (transfer) Daemon TCP system source file. Read/Write state to disk.
3 *
4 * Copyright (c) 2001-2006, NLnet Labs. All rights reserved.
5 *
6 * See LICENSE for the license.
7 *
8 */
9
10 #include "config.h"
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <ctype.h>
14 #include <errno.h>
15 #include <sys/stat.h>
16 #include <sys/types.h>
17 #include <unistd.h>
18 #include "xfrd-disk.h"
19 #include "xfrd.h"
20 #include "buffer.h"
21 #include "nsd.h"
22 #include "options.h"
23
24 /* quick tokenizer, reads words separated by whitespace.
25 No quoted strings. Comments are skipped (#... eol). */
26 static char*
27 xfrd_read_token(FILE* in)
28 {
29 static char buf[4000];
30 buf[sizeof(buf)-1]=0;
31 while(1) {
32 if(fscanf(in, " %3990s", buf) != 1)
33 return 0;
34
35 if(buf[0] != '#')
36 return buf;
37
38 if(!fgets(buf, sizeof(buf), in))
39 return 0;
40 }
41 }
42
43 static int
44 xfrd_read_i16(FILE *in, uint16_t* v)
45 {
46 char* p = xfrd_read_token(in);
47 if(!p)
48 return 0;
49
50 *v=atoi(p);
51 return 1;
52 }
53
54 static int
55 xfrd_read_i32(FILE *in, uint32_t* v)
56 {
57 char* p = xfrd_read_token(in);
58 if(!p)
59 return 0;
60
61 *v=atoi(p);
62 return 1;
63 }
64
65 static int
66 xfrd_read_time_t(FILE *in, time_t* v)
67 {
68 char* p = xfrd_read_token(in);
69 if(!p)
70 return 0;
71
72 *v=atol(p);
73 return 1;
74 }
75
76 static int
77 xfrd_read_check_str(FILE* in, const char* str)
78 {
79 char *p = xfrd_read_token(in);
80 if(!p)
81 return 0;
82
83 if(strcmp(p, str) != 0)
84 return 0;
85
86 return 1;
87 }
88
89 static int
90 xfrd_read_state_soa(FILE* in, const char* id_acquired,
91 const char* id, xfrd_soa_type* soa, time_t* soatime)
92 {
93 char *p;
94
95 if(!xfrd_read_check_str(in, id_acquired) ||
96 !xfrd_read_time_t(in, soatime)) {
97 return 0;
98 }
99
100 if(*soatime == 0)
101 return 1;
102
103 if(!xfrd_read_check_str(in, id) ||
104 !xfrd_read_i16(in, &soa->type) ||
105 !xfrd_read_i16(in, &soa->klass) ||
106 !xfrd_read_i32(in, &soa->ttl) ||
107 !xfrd_read_i16(in, &soa->rdata_count))
108 {
109 return 0;
110 }
111
112 soa->type = htons(soa->type);
113 soa->klass = htons(soa->klass);
114 soa->ttl = htonl(soa->ttl);
115 soa->rdata_count = htons(soa->rdata_count);
116
117 if(!(p=xfrd_read_token(in)) ||
118 !(soa->prim_ns[0] = dname_parse_wire(soa->prim_ns+1, p)))
119 return 0;
120
121 if(!(p=xfrd_read_token(in)) ||
122 !(soa->email[0] = dname_parse_wire(soa->email+1, p)))
123 return 0;
124
125 if(!xfrd_read_i32(in, &soa->serial) ||
126 !xfrd_read_i32(in, &soa->refresh) ||
127 !xfrd_read_i32(in, &soa->retry) ||
128 !xfrd_read_i32(in, &soa->expire) ||
129 !xfrd_read_i32(in, &soa->minimum))
130 {
131 return 0;
132 }
133
134 soa->serial = htonl(soa->serial);
135 soa->refresh = htonl(soa->refresh);
136 soa->retry = htonl(soa->retry);
137 soa->expire = htonl(soa->expire);
138 soa->minimum = htonl(soa->minimum);
139 return 1;
140 }
141
142 void
143 xfrd_read_state(struct xfrd_state* xfrd)
144 {
145 const char* statefile = xfrd->nsd->options->xfrdfile;
146 FILE *in;
147 uint32_t filetime = 0;
148 uint32_t numzones, i;
149 region_type *tempregion;
150 time_t soa_refresh;
151
152 tempregion = region_create(xalloc, free);
153 if(!tempregion)
154 return;
155
156 in = fopen(statefile, "r");
157 if(!in) {
158 if(errno != ENOENT) {
159 log_msg(LOG_ERR, "xfrd: Could not open file %s for reading: %s",
160 statefile, strerror(errno));
161 } else {
162 DEBUG(DEBUG_XFRD,1, (LOG_INFO, "xfrd: no file %s. refreshing all zones.",
163 statefile));
164 }
165 region_destroy(tempregion);
166 return;
167 }
168 if(!xfrd_read_check_str(in, XFRD_FILE_MAGIC)) {
169 /* older file version; reset everything */
170 DEBUG(DEBUG_XFRD,1, (LOG_INFO, "xfrd: file %s is old version. refreshing all zones.",
171 statefile));
172 fclose(in);
173 region_destroy(tempregion);
174 return;
175 }
176 if(!xfrd_read_check_str(in, "filetime:") ||
177 !xfrd_read_i32(in, &filetime) ||
178 (time_t)filetime > xfrd_time()+15 ||
179 !xfrd_read_check_str(in, "numzones:") ||
180 !xfrd_read_i32(in, &numzones))
181 {
182 log_msg(LOG_ERR, "xfrd: corrupt state file %s dated %d (now=%lld)",
183 statefile, (int)filetime, (long long)xfrd_time());
184 fclose(in);
185 region_destroy(tempregion);
186 return;
187 }
188
189 for(i=0; i<numzones; i++) {
190 char *p;
191 xfrd_zone_type* zone;
192 const dname_type* dname;
193 uint32_t state, masnum, nextmas, round_num, timeout, backoff;
194 xfrd_soa_type soa_nsd_read, soa_disk_read, soa_notified_read;
195 time_t soa_nsd_acquired_read,
196 soa_disk_acquired_read, soa_notified_acquired_read;
197 xfrd_soa_type incoming_soa;
198 time_t incoming_acquired;
199
200 if(nsd.signal_hint_shutdown) {
201 fclose(in);
202 region_destroy(tempregion);
203 return;
204 }
205
206 memset(&soa_nsd_read, 0, sizeof(soa_nsd_read));
207 memset(&soa_disk_read, 0, sizeof(soa_disk_read));
208 memset(&soa_notified_read, 0, sizeof(soa_notified_read));
209
210 if(!xfrd_read_check_str(in, "zone:") ||
211 !xfrd_read_check_str(in, "name:") ||
212 !(p=xfrd_read_token(in)) ||
213 !(dname = dname_parse(tempregion, p)) ||
214 !xfrd_read_check_str(in, "state:") ||
215 !xfrd_read_i32(in, &state) || (state>2) ||
216 !xfrd_read_check_str(in, "master:") ||
217 !xfrd_read_i32(in, &masnum) ||
218 !xfrd_read_check_str(in, "next_master:") ||
219 !xfrd_read_i32(in, &nextmas) ||
220 !xfrd_read_check_str(in, "round_num:") ||
221 !xfrd_read_i32(in, &round_num) ||
222 !xfrd_read_check_str(in, "next_timeout:") ||
223 !xfrd_read_i32(in, &timeout) ||
224 !xfrd_read_check_str(in, "backoff:") ||
225 !xfrd_read_i32(in, &backoff) ||
226 !xfrd_read_state_soa(in, "soa_nsd_acquired:", "soa_nsd:",
227 &soa_nsd_read, &soa_nsd_acquired_read) ||
228 !xfrd_read_state_soa(in, "soa_disk_acquired:", "soa_disk:",
229 &soa_disk_read, &soa_disk_acquired_read) ||
230 !xfrd_read_state_soa(in, "soa_notify_acquired:", "soa_notify:",
231 &soa_notified_read, &soa_notified_acquired_read))
232 {
233 log_msg(LOG_ERR, "xfrd: corrupt state file %s dated %d (now=%lld)",
234 statefile, (int)filetime, (long long)xfrd_time());
235 fclose(in);
236 region_destroy(tempregion);
237 return;
238 }
239
240 zone = (xfrd_zone_type*)rbtree_search(xfrd->zones, dname);
241 if(!zone) {
242 DEBUG(DEBUG_XFRD,1, (LOG_INFO, "xfrd: state file has info for not configured zone %s", p));
243 continue;
244 }
245
246 if(soa_nsd_acquired_read>xfrd_time()+15 ||
247 soa_disk_acquired_read>xfrd_time()+15 ||
248 soa_notified_acquired_read>xfrd_time()+15)
249 {
250 log_msg(LOG_ERR, "xfrd: statefile %s contains"
251 " times in the future for zone %s. Ignoring.",
252 statefile, zone->apex_str);
253 continue;
254 }
255 zone->state = state;
256 zone->master_num = masnum;
257 zone->next_master = nextmas;
258 zone->round_num = round_num;
259 zone->timeout.tv_sec = timeout;
260 zone->timeout.tv_usec = 0;
261 zone->fresh_xfr_timeout = backoff*XFRD_TRANSFER_TIMEOUT_START;
262
263 /* read the zone OK, now set the master properly */
264 zone->master = acl_find_num(zone->zone_options->pattern->
265 request_xfr, zone->master_num);
266 if(!zone->master) {
267 DEBUG(DEBUG_XFRD,1, (LOG_INFO, "xfrd: masters changed for zone %s",
268 zone->apex_str));
269 zone->master = zone->zone_options->pattern->request_xfr;
270 zone->master_num = 0;
271 zone->round_num = 0;
272 }
273
274 /*
275 * There is no timeout,
276 * or there is a notification,
277 * or there is a soa && current time is past refresh point
278 */
279 soa_refresh = ntohl(soa_disk_read.refresh);
280 if (soa_refresh > (time_t)zone->zone_options->pattern->max_refresh_time)
281 soa_refresh = zone->zone_options->pattern->max_refresh_time;
282 else if (soa_refresh < (time_t)zone->zone_options->pattern->min_refresh_time)
283 soa_refresh = zone->zone_options->pattern->min_refresh_time;
284 if(timeout == 0 || soa_notified_acquired_read != 0 ||
285 (soa_disk_acquired_read != 0 &&
286 (uint32_t)xfrd_time() - soa_disk_acquired_read
287 > (uint32_t)soa_refresh))
288 {
289 zone->state = xfrd_zone_refreshing;
290 xfrd_set_refresh_now(zone);
291 }
292
293 /* There is a soa && current time is past expiry point */
294 if(soa_disk_acquired_read!=0 &&
295 (uint32_t)xfrd_time() - soa_disk_acquired_read
296 > ntohl(soa_disk_read.expire))
297 {
298 zone->state = xfrd_zone_expired;
299 xfrd_set_refresh_now(zone);
300 }
301
302 /* there is a zone read and it matches what we had before */
303 if(zone->soa_nsd_acquired && zone->state != xfrd_zone_expired
304 && zone->soa_nsd.serial == soa_nsd_read.serial) {
305 xfrd_deactivate_zone(zone);
306 zone->state = state;
307 xfrd_set_timer(zone, timeout);
308 }
309 if((zone->soa_nsd_acquired == 0 && soa_nsd_acquired_read == 0 &&
310 soa_disk_acquired_read == 0) ||
311 (zone->state != xfrd_zone_ok && timeout != 0)) {
312 /* but don't check now, because that would mean a
313 * storm of attempts on some master servers */
314 xfrd_deactivate_zone(zone);
315 zone->state = state;
316 xfrd_set_timer(zone, timeout);
317 }
318
319 /* handle as an incoming SOA. */
320 incoming_soa = zone->soa_nsd;
321 incoming_acquired = zone->soa_nsd_acquired;
322 zone->soa_nsd = soa_nsd_read;
323 zone->soa_disk = soa_disk_read;
324 zone->soa_notified = soa_notified_read;
325 zone->soa_nsd_acquired = soa_nsd_acquired_read;
326 /* we had better use what we got from starting NSD, not
327 * what we store in this file, because the actual zone
328 * contents trumps the contents of this cache */
329 /* zone->soa_disk_acquired = soa_disk_acquired_read; */
330 zone->soa_notified_acquired = soa_notified_acquired_read;
331 if (zone->state == xfrd_zone_expired)
332 {
333 xfrd_send_expire_notification(zone);
334 }
335 if(incoming_acquired != 0)
336 xfrd_handle_incoming_soa(zone, &incoming_soa, incoming_acquired);
337 }
338
339 if(!xfrd_read_check_str(in, XFRD_FILE_MAGIC)) {
340 log_msg(LOG_ERR, "xfrd: corrupt state file %s dated %d (now=%lld)",
341 statefile, (int)filetime, (long long)xfrd_time());
342 region_destroy(tempregion);
343 fclose(in);
344 return;
345 }
346
347 DEBUG(DEBUG_XFRD,1, (LOG_INFO, "xfrd: read %d zones from state file", (int)numzones));
348 fclose(in);
349 region_destroy(tempregion);
350 }
351
352 /* prints neato days hours and minutes. */
353 static void
354 neato_timeout(FILE* out, const char* str, time_t secs)
355 {
356 fprintf(out, "%s", str);
357 if(secs <= 0) {
358 fprintf(out, " %llds", (long long)secs);
359 return;
360 }
361 if(secs >= 3600*24) {
362 fprintf(out, " %lldd", (long long)(secs/(3600*24)));
363 secs = secs % (3600*24);
364 }
365 if(secs >= 3600) {
366 fprintf(out, " %lldh", (long long)(secs/3600));
367 secs = secs%3600;
368 }
369 if(secs >= 60) {
370 fprintf(out, " %lldm", (long long)(secs/60));
371 secs = secs%60;
372 }
373 if(secs > 0) {
374 fprintf(out, " %llds", (long long)secs);
375 }
376 }
377
378 static void xfrd_write_dname(FILE* out, uint8_t* dname)
379 {
380 uint8_t* d= dname+1;
381 uint8_t len = *d++;
382 uint8_t i;
383
384 if(dname[0]<=1) {
385 fprintf(out, ".");
386 return;
387 }
388
389 while(len)
390 {
391 assert(d - (dname+1) <= dname[0]);
392 for(i=0; i<len; i++)
393 {
394 uint8_t ch = *d++;
395 if (isalnum((unsigned char)ch) || ch == '-' || ch == '_') {
396 fprintf(out, "%c", ch);
397 } else if (ch == '.' || ch == '\\') {
398 fprintf(out, "\\%c", ch);
399 } else {
400 fprintf(out, "\\%03u", (unsigned int)ch);
401 }
402 }
403 fprintf(out, ".");
404 len = *d++;
405 }
406 }
407
408 static void
409 xfrd_write_state_soa(FILE* out, const char* id,
410 xfrd_soa_type* soa, time_t soatime, const dname_type* ATTR_UNUSED(apex))
411 {
412 fprintf(out, "\t%s_acquired: %d", id, (int)soatime);
413 if(!soatime) {
414 fprintf(out, "\n");
415 return;
416 }
417 neato_timeout(out, "\t# was", xfrd_time()-soatime);
418 fprintf(out, " ago\n");
419
420 fprintf(out, "\t%s: %u %u %u %u", id,
421 (unsigned)ntohs(soa->type), (unsigned)ntohs(soa->klass),
422 (unsigned)ntohl(soa->ttl), (unsigned)ntohs(soa->rdata_count));
423 fprintf(out, " ");
424 xfrd_write_dname(out, soa->prim_ns);
425 fprintf(out, " ");
426 xfrd_write_dname(out, soa->email);
427 fprintf(out, " %u", (unsigned)ntohl(soa->serial));
428 fprintf(out, " %u", (unsigned)ntohl(soa->refresh));
429 fprintf(out, " %u", (unsigned)ntohl(soa->retry));
430 fprintf(out, " %u", (unsigned)ntohl(soa->expire));
431 fprintf(out, " %u\n", (unsigned)ntohl(soa->minimum));
432 fprintf(out, "\t#");
433 neato_timeout(out, " refresh =", ntohl(soa->refresh));
434 neato_timeout(out, " retry =", ntohl(soa->retry));
435 neato_timeout(out, " expire =", ntohl(soa->expire));
436 neato_timeout(out, " minimum =", ntohl(soa->minimum));
437 fprintf(out, "\n");
438 }
439
440 void
441 xfrd_write_state(struct xfrd_state* xfrd)
442 {
443 rbnode_type* p;
444 const char* statefile = xfrd->nsd->options->xfrdfile;
445 FILE *out;
446 time_t now = xfrd_time();
447
448 DEBUG(DEBUG_XFRD,1, (LOG_INFO, "xfrd: write file %s", statefile));
449 out = fopen(statefile, "w");
450 if(!out) {
451 log_msg(LOG_ERR, "xfrd: Could not open file %s for writing: %s",
452 statefile, strerror(errno));
453 return;
454 }
455
456 fprintf(out, "%s\n", XFRD_FILE_MAGIC);
457 fprintf(out, "# This file is written on exit by nsd xfr daemon.\n");
458 fprintf(out, "# This file contains slave zone information:\n");
459 fprintf(out, "# * timeouts (when was zone data acquired)\n");
460 fprintf(out, "# * state (OK, refreshing, expired)\n");
461 fprintf(out, "# * which master transfer to attempt next\n");
462 fprintf(out, "# The file is read on start (but not on reload) by nsd xfr daemon.\n");
463 fprintf(out, "# You can edit; but do not change statement order\n");
464 fprintf(out, "# and no fancy stuff (like quoted \"strings\").\n");
465 fprintf(out, "#\n");
466 fprintf(out, "# If you remove a zone entry, it will be refreshed.\n");
467 fprintf(out, "# This can be useful for an expired zone; it revives\n");
468 fprintf(out, "# the zone temporarily, from refresh-expiry time.\n");
469 fprintf(out, "# If you delete the file all slave zones are updated.\n");
470 fprintf(out, "#\n");
471 fprintf(out, "# Note: if you edit this file while nsd is running,\n");
472 fprintf(out, "# it will be overwritten on exit by nsd.\n");
473 fprintf(out, "\n");
474 fprintf(out, "filetime: %lld\t# %s\n", (long long)now, ctime(&now));
475 fprintf(out, "# The number of zone entries in this file\n");
476 fprintf(out, "numzones: %d\n", (int)xfrd->zones->count);
477 fprintf(out, "\n");
478 for(p = rbtree_first(xfrd->zones); p && p!=RBTREE_NULL; p=rbtree_next(p))
479 {
480 xfrd_zone_type* zone = (xfrd_zone_type*)p;
481 fprintf(out, "zone: \tname: %s\n", zone->apex_str);
482 fprintf(out, "\tstate: %d", (int)zone->state);
483 fprintf(out, " # %s", zone->state==xfrd_zone_ok?"OK":(
484 zone->state==xfrd_zone_refreshing?"refreshing":"expired"));
485 fprintf(out, "\n");
486 fprintf(out, "\tmaster: %d\n", zone->master_num);
487 fprintf(out, "\tnext_master: %d\n", zone->next_master);
488 fprintf(out, "\tround_num: %d\n", zone->round_num);
489 fprintf(out, "\tnext_timeout: %d",
490 (zone->zone_handler_flags&EV_TIMEOUT)?(int)zone->timeout.tv_sec:0);
491 if((zone->zone_handler_flags&EV_TIMEOUT)) {
492 neato_timeout(out, "\t# =", zone->timeout.tv_sec);
493 }
494 fprintf(out, "\n");
495 fprintf(out, "\tbackoff: %d\n", zone->fresh_xfr_timeout/XFRD_TRANSFER_TIMEOUT_START);
496 xfrd_write_state_soa(out, "soa_nsd", &zone->soa_nsd,
497 zone->soa_nsd_acquired, zone->apex);
498 xfrd_write_state_soa(out, "soa_disk", &zone->soa_disk,
499 zone->soa_disk_acquired, zone->apex);
500 xfrd_write_state_soa(out, "soa_notify", &zone->soa_notified,
501 zone->soa_notified_acquired, zone->apex);
502 fprintf(out, "\n");
503 }
504
505 fprintf(out, "%s\n", XFRD_FILE_MAGIC);
506 DEBUG(DEBUG_XFRD,1, (LOG_INFO, "xfrd: written %d zones to state file",
507 (int)xfrd->zones->count));
508 fclose(out);
509 }
510
511 /* return tempdirname */
512 static void
513 tempdirname(char* buf, size_t sz, struct nsd* nsd)
514 {
515 snprintf(buf, sz, "%snsd-xfr-%d",
516 nsd->options->xfrdir, (int)nsd->pid);
517 }
518
519 void
520 xfrd_make_tempdir(struct nsd* nsd)
521 {
522 char tnm[1024];
523 tempdirname(tnm, sizeof(tnm), nsd);
524 /* create parent directories if needed (0750 permissions) */
525 if(!create_dirs(tnm)) {
526 log_msg(LOG_ERR, "parentdirs of %s failed", tnm);
527 }
528 /* restrictive permissions here, because this may be in /tmp */
529 if(mkdir(tnm, 0700)==-1) {
530 if(errno != EEXIST) {
531 log_msg(LOG_ERR, "mkdir %s failed: %s",
532 tnm, strerror(errno));
533 }
534 }
535 }
536
537 void
538 xfrd_del_tempdir(struct nsd* nsd)
539 {
540 char tnm[1024];
541 tempdirname(tnm, sizeof(tnm), nsd);
542 /* ignore parent directories, they are likely /var/tmp, /tmp or
543 * /var/cache/nsd and do not have to be deleted */
544 if(rmdir(tnm)==-1 && errno != ENOENT) {
545 log_msg(LOG_WARNING, "rmdir %s failed: %s", tnm,
546 strerror(errno));
547 }
548 }
549
550 /* return name of xfrfile in tempdir */
551 static void
552 tempxfrname(char* buf, size_t sz, struct nsd* nsd, uint64_t number)
553 {
554 char tnm[1024];
555 tempdirname(tnm, sizeof(tnm), nsd);
556 snprintf(buf, sz, "%s/xfr.%lld", tnm, (long long)number);
557 }
558
559 FILE*
560 xfrd_open_xfrfile(struct nsd* nsd, uint64_t number, char* mode)
561 {
562 char fname[1200];
563 FILE* xfr;
564 tempxfrname(fname, sizeof(fname), nsd, number);
565 xfr = fopen(fname, mode);
566 if(!xfr && errno == ENOENT) {
567 /* directory may not exist */
568 xfrd_make_tempdir(nsd);
569 xfr = fopen(fname, mode);
570 }
571 if(!xfr) {
572 log_msg(LOG_ERR, "open %s for %s failed: %s", fname, mode,
573 strerror(errno));
574 return NULL;
575 }
576 return xfr;
577 }
578
579 void
580 xfrd_unlink_xfrfile(struct nsd* nsd, uint64_t number)
581 {
582 char fname[1200];
583 tempxfrname(fname, sizeof(fname), nsd, number);
584 if(unlink(fname) == -1) {
585 log_msg(LOG_WARNING, "could not unlink %s: %s", fname,
586 strerror(errno));
587 }
588 }
589
590 uint64_t
591 xfrd_get_xfrfile_size(struct nsd* nsd, uint64_t number )
592 {
593 char fname[1200];
594 struct stat tempxfr_stat;
595 tempxfrname(fname, sizeof(fname), nsd, number);
596 if( stat( fname, &tempxfr_stat ) < 0 ) {
597 log_msg(LOG_WARNING, "could not get file size %s: %s", fname,
598 strerror(errno));
599 return 0;
600 }
601 return (uint64_t)tempxfr_stat.st_size;
602 }
603