xfrd-disk.c revision 1.1.1.2.4.2 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 if(timeout != 0 && filetime + timeout < (uint32_t)xfrd_time()) {
293 /* timeout is in the past, refresh the zone */
294 timeout = 0;
295 if(zone->state == xfrd_zone_ok)
296 zone->state = xfrd_zone_refreshing;
297 xfrd_set_refresh_now(zone);
298 }
299
300 /* There is a soa && current time is past expiry point */
301 if(soa_disk_acquired_read!=0 &&
302 (uint32_t)xfrd_time() - soa_disk_acquired_read
303 > ntohl(soa_disk_read.expire))
304 {
305 zone->state = xfrd_zone_expired;
306 xfrd_set_refresh_now(zone);
307 }
308
309 /* there is a zone read and it matches what we had before */
310 if(zone->soa_nsd_acquired && zone->state != xfrd_zone_expired
311 && zone->soa_nsd.serial == soa_nsd_read.serial) {
312 xfrd_deactivate_zone(zone);
313 zone->state = state;
314 xfrd_set_timer(zone, timeout);
315 }
316 if((zone->soa_nsd_acquired == 0 && soa_nsd_acquired_read == 0 &&
317 soa_disk_acquired_read == 0) ||
318 (zone->state != xfrd_zone_ok && timeout != 0)) {
319 /* but don't check now, because that would mean a
320 * storm of attempts on some master servers */
321 xfrd_deactivate_zone(zone);
322 zone->state = state;
323 xfrd_set_timer(zone, timeout);
324 }
325
326 /* handle as an incoming SOA. */
327 incoming_soa = zone->soa_nsd;
328 incoming_acquired = zone->soa_nsd_acquired;
329 zone->soa_nsd = soa_nsd_read;
330 zone->soa_disk = soa_disk_read;
331 zone->soa_notified = soa_notified_read;
332 zone->soa_nsd_acquired = soa_nsd_acquired_read;
333 /* we had better use what we got from starting NSD, not
334 * what we store in this file, because the actual zone
335 * contents trumps the contents of this cache */
336 /* zone->soa_disk_acquired = soa_disk_acquired_read; */
337 zone->soa_notified_acquired = soa_notified_acquired_read;
338 if (zone->state == xfrd_zone_expired)
339 {
340 xfrd_send_expire_notification(zone);
341 }
342 if(incoming_acquired != 0)
343 xfrd_handle_incoming_soa(zone, &incoming_soa, incoming_acquired);
344 }
345
346 if(!xfrd_read_check_str(in, XFRD_FILE_MAGIC)) {
347 log_msg(LOG_ERR, "xfrd: corrupt state file %s dated %d (now=%lld)",
348 statefile, (int)filetime, (long long)xfrd_time());
349 region_destroy(tempregion);
350 fclose(in);
351 return;
352 }
353
354 DEBUG(DEBUG_XFRD,1, (LOG_INFO, "xfrd: read %d zones from state file", (int)numzones));
355 fclose(in);
356 region_destroy(tempregion);
357 }
358
359 /* prints neato days hours and minutes. */
360 static void
361 neato_timeout(FILE* out, const char* str, time_t secs)
362 {
363 fprintf(out, "%s", str);
364 if(secs <= 0) {
365 fprintf(out, " %llds", (long long)secs);
366 return;
367 }
368 if(secs >= 3600*24) {
369 fprintf(out, " %lldd", (long long)(secs/(3600*24)));
370 secs = secs % (3600*24);
371 }
372 if(secs >= 3600) {
373 fprintf(out, " %lldh", (long long)(secs/3600));
374 secs = secs%3600;
375 }
376 if(secs >= 60) {
377 fprintf(out, " %lldm", (long long)(secs/60));
378 secs = secs%60;
379 }
380 if(secs > 0) {
381 fprintf(out, " %llds", (long long)secs);
382 }
383 }
384
385 static void xfrd_write_dname(FILE* out, uint8_t* dname)
386 {
387 uint8_t* d= dname+1;
388 uint8_t len = *d++;
389 uint8_t i;
390
391 if(dname[0]<=1) {
392 fprintf(out, ".");
393 return;
394 }
395
396 while(len)
397 {
398 assert(d - (dname+1) <= dname[0]);
399 for(i=0; i<len; i++)
400 {
401 uint8_t ch = *d++;
402 if (isalnum((unsigned char)ch) || ch == '-' || ch == '_') {
403 fprintf(out, "%c", ch);
404 } else if (ch == '.' || ch == '\\') {
405 fprintf(out, "\\%c", ch);
406 } else {
407 fprintf(out, "\\%03u", (unsigned int)ch);
408 }
409 }
410 fprintf(out, ".");
411 len = *d++;
412 }
413 }
414
415 static void
416 xfrd_write_state_soa(FILE* out, const char* id,
417 xfrd_soa_type* soa, time_t soatime, const dname_type* ATTR_UNUSED(apex))
418 {
419 fprintf(out, "\t%s_acquired: %d", id, (int)soatime);
420 if(!soatime) {
421 fprintf(out, "\n");
422 return;
423 }
424 neato_timeout(out, "\t# was", xfrd_time()-soatime);
425 fprintf(out, " ago\n");
426
427 fprintf(out, "\t%s: %u %u %u %u", id,
428 (unsigned)ntohs(soa->type), (unsigned)ntohs(soa->klass),
429 (unsigned)ntohl(soa->ttl), (unsigned)ntohs(soa->rdata_count));
430 fprintf(out, " ");
431 xfrd_write_dname(out, soa->prim_ns);
432 fprintf(out, " ");
433 xfrd_write_dname(out, soa->email);
434 fprintf(out, " %u", (unsigned)ntohl(soa->serial));
435 fprintf(out, " %u", (unsigned)ntohl(soa->refresh));
436 fprintf(out, " %u", (unsigned)ntohl(soa->retry));
437 fprintf(out, " %u", (unsigned)ntohl(soa->expire));
438 fprintf(out, " %u\n", (unsigned)ntohl(soa->minimum));
439 fprintf(out, "\t#");
440 neato_timeout(out, " refresh =", ntohl(soa->refresh));
441 neato_timeout(out, " retry =", ntohl(soa->retry));
442 neato_timeout(out, " expire =", ntohl(soa->expire));
443 neato_timeout(out, " minimum =", ntohl(soa->minimum));
444 fprintf(out, "\n");
445 }
446
447 void
448 xfrd_write_state(struct xfrd_state* xfrd)
449 {
450 rbnode_type* p;
451 const char* statefile = xfrd->nsd->options->xfrdfile;
452 FILE *out;
453 time_t now = xfrd_time();
454
455 DEBUG(DEBUG_XFRD,1, (LOG_INFO, "xfrd: write file %s", statefile));
456 out = fopen(statefile, "w");
457 if(!out) {
458 log_msg(LOG_ERR, "xfrd: Could not open file %s for writing: %s",
459 statefile, strerror(errno));
460 return;
461 }
462
463 fprintf(out, "%s\n", XFRD_FILE_MAGIC);
464 fprintf(out, "# This file is written on exit by nsd xfr daemon.\n");
465 fprintf(out, "# This file contains slave zone information:\n");
466 fprintf(out, "# * timeouts (when was zone data acquired)\n");
467 fprintf(out, "# * state (OK, refreshing, expired)\n");
468 fprintf(out, "# * which master transfer to attempt next\n");
469 fprintf(out, "# The file is read on start (but not on reload) by nsd xfr daemon.\n");
470 fprintf(out, "# You can edit; but do not change statement order\n");
471 fprintf(out, "# and no fancy stuff (like quoted \"strings\").\n");
472 fprintf(out, "#\n");
473 fprintf(out, "# If you remove a zone entry, it will be refreshed.\n");
474 fprintf(out, "# This can be useful for an expired zone; it revives\n");
475 fprintf(out, "# the zone temporarily, from refresh-expiry time.\n");
476 fprintf(out, "# If you delete the file all slave zones are updated.\n");
477 fprintf(out, "#\n");
478 fprintf(out, "# Note: if you edit this file while nsd is running,\n");
479 fprintf(out, "# it will be overwritten on exit by nsd.\n");
480 fprintf(out, "\n");
481 fprintf(out, "filetime: %lld\t# %s\n", (long long)now, ctime(&now));
482 fprintf(out, "# The number of zone entries in this file\n");
483 fprintf(out, "numzones: %d\n", (int)xfrd->zones->count);
484 fprintf(out, "\n");
485 for(p = rbtree_first(xfrd->zones); p && p!=RBTREE_NULL; p=rbtree_next(p))
486 {
487 xfrd_zone_type* zone = (xfrd_zone_type*)p;
488 fprintf(out, "zone: \tname: %s\n", zone->apex_str);
489 fprintf(out, "\tstate: %d", (int)zone->state);
490 fprintf(out, " # %s", zone->state==xfrd_zone_ok?"OK":(
491 zone->state==xfrd_zone_refreshing?"refreshing":"expired"));
492 fprintf(out, "\n");
493 fprintf(out, "\tmaster: %d\n", zone->master_num);
494 fprintf(out, "\tnext_master: %d\n", zone->next_master);
495 fprintf(out, "\tround_num: %d\n", zone->round_num);
496 fprintf(out, "\tnext_timeout: %d",
497 (zone->zone_handler_flags&EV_TIMEOUT)?(int)zone->timeout.tv_sec:0);
498 if((zone->zone_handler_flags&EV_TIMEOUT)) {
499 neato_timeout(out, "\t# =", zone->timeout.tv_sec);
500 }
501 fprintf(out, "\n");
502 fprintf(out, "\tbackoff: %d\n", zone->fresh_xfr_timeout/XFRD_TRANSFER_TIMEOUT_START);
503 xfrd_write_state_soa(out, "soa_nsd", &zone->soa_nsd,
504 zone->soa_nsd_acquired, zone->apex);
505 xfrd_write_state_soa(out, "soa_disk", &zone->soa_disk,
506 zone->soa_disk_acquired, zone->apex);
507 xfrd_write_state_soa(out, "soa_notify", &zone->soa_notified,
508 zone->soa_notified_acquired, zone->apex);
509 fprintf(out, "\n");
510 }
511
512 fprintf(out, "%s\n", XFRD_FILE_MAGIC);
513 DEBUG(DEBUG_XFRD,1, (LOG_INFO, "xfrd: written %d zones to state file",
514 (int)xfrd->zones->count));
515 fclose(out);
516 }
517
518 /* return tempdirname */
519 static void
520 tempdirname(char* buf, size_t sz, struct nsd* nsd)
521 {
522 snprintf(buf, sz, "%snsd-xfr-%d",
523 nsd->options->xfrdir, (int)nsd->pid);
524 }
525
526 void
527 xfrd_make_tempdir(struct nsd* nsd)
528 {
529 char tnm[1024];
530 tempdirname(tnm, sizeof(tnm), nsd);
531 /* create parent directories if needed (0750 permissions) */
532 if(!create_dirs(tnm)) {
533 log_msg(LOG_ERR, "parentdirs of %s failed", tnm);
534 }
535 /* restrictive permissions here, because this may be in /tmp */
536 if(mkdir(tnm, 0700)==-1) {
537 if(errno != EEXIST) {
538 log_msg(LOG_ERR, "mkdir %s failed: %s",
539 tnm, strerror(errno));
540 }
541 }
542 }
543
544 void
545 xfrd_del_tempdir(struct nsd* nsd)
546 {
547 char tnm[1024];
548 tempdirname(tnm, sizeof(tnm), nsd);
549 /* ignore parent directories, they are likely /var/tmp, /tmp or
550 * /var/cache/nsd and do not have to be deleted */
551 if(rmdir(tnm)==-1 && errno != ENOENT) {
552 log_msg(LOG_WARNING, "rmdir %s failed: %s", tnm,
553 strerror(errno));
554 }
555 }
556
557 /* return name of xfrfile in tempdir */
558 static void
559 tempxfrname(char* buf, size_t sz, struct nsd* nsd, uint64_t number)
560 {
561 char tnm[1024];
562 tempdirname(tnm, sizeof(tnm), nsd);
563 snprintf(buf, sz, "%s/xfr.%lld", tnm, (long long)number);
564 }
565
566 FILE*
567 xfrd_open_xfrfile(struct nsd* nsd, uint64_t number, char* mode)
568 {
569 char fname[1200];
570 FILE* xfr;
571 tempxfrname(fname, sizeof(fname), nsd, number);
572 xfr = fopen(fname, mode);
573 if(!xfr && errno == ENOENT) {
574 /* directory may not exist */
575 xfrd_make_tempdir(nsd);
576 xfr = fopen(fname, mode);
577 }
578 if(!xfr) {
579 log_msg(LOG_ERR, "open %s for %s failed: %s", fname, mode,
580 strerror(errno));
581 return NULL;
582 }
583 return xfr;
584 }
585
586 void
587 xfrd_unlink_xfrfile(struct nsd* nsd, uint64_t number)
588 {
589 char fname[1200];
590 tempxfrname(fname, sizeof(fname), nsd, number);
591 if(unlink(fname) == -1) {
592 log_msg(LOG_WARNING, "could not unlink %s: %s", fname,
593 strerror(errno));
594 }
595 }
596
597 uint64_t
598 xfrd_get_xfrfile_size(struct nsd* nsd, uint64_t number )
599 {
600 char fname[1200];
601 struct stat tempxfr_stat;
602 tempxfrname(fname, sizeof(fname), nsd, number);
603 if( stat( fname, &tempxfr_stat ) < 0 ) {
604 log_msg(LOG_WARNING, "could not get file size %s: %s", fname,
605 strerror(errno));
606 return 0;
607 }
608 return (uint64_t)tempxfr_stat.st_size;
609 }
610