-
Notifications
You must be signed in to change notification settings - Fork 55
/
backup.c
640 lines (548 loc) · 16.9 KB
/
backup.c
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
/* Copyright (C) 2021 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include "knot/zone/backup.h"
#include "contrib/files.h"
#include "contrib/getline.h"
#include "contrib/macros.h"
#include "contrib/string.h"
#include "knot/catalog/catalog_db.h"
#include "knot/common/log.h"
#include "knot/dnssec/kasp/kasp_zone.h"
#include "knot/dnssec/kasp/keystore.h"
#include "knot/journal/journal_metadata.h"
#include "knot/zone/zonefile.h"
#include "libdnssec/error.h"
static void _backup_swap(zone_backup_ctx_t *ctx, void **local, void **remote)
{
if (ctx->restore_mode) {
void *temp = *local;
*local = *remote;
*remote = temp;
}
}
// Current backup format version for output.
#define BACKUP_VERSION BACKUP_FORMAT_2 // Starting with release 3.1.0.
#define LABEL_FILE "knot_backup.label"
#define LOCK_FILE "lock.knot_backup"
#define LABEL_FILE_HEAD "label: Knot DNS Backup\n"
#define LABEL_FILE_FORMAT "backup_format: %d\n"
#define LABEL_FILE_TIME_FORMAT "%Y-%m-%d %H:%M:%S %Z"
#define MAX_LENGTH (MAX(sizeof(LABEL_FILE), sizeof(LOCK_FILE)))
#define BACKUP_SWAP(ctx, from, to) _backup_swap((ctx), (void **)&(from), (void **)&(to))
#if defined(_POSIX_HOST_NAME_MAX)
# define HOSTNAME_MAX (_POSIX_HOST_NAME_MAX + 1) // _POSIX_HOST_NAME_MAX doesn't include '\0'.
#else
# define HOSTNAME_MAX 256
#endif
static const char *label_file_name = LABEL_FILE;
static const char *lock_file_name = LOCK_FILE;
static const char *label_file_head = LABEL_FILE_HEAD;
static int make_label_file(zone_backup_ctx_t *ctx, char *full_path)
{
int ret;
FILE *file = fopen(full_path, "w");
if (file == NULL) {
return knot_map_errno();
}
// Prepare the hostname.
char hostname[HOSTNAME_MAX];
gethostname(hostname, HOSTNAME_MAX);
// When the name doesn't fit, the \0 terminator isn't always guaranteed.
hostname[HOSTNAME_MAX - 1] = '\0';
// Prepare the timestamps.
char started_time[64], finished_time[64];
struct tm tm;
localtime_r(&ctx->init_time, &tm);
strftime(started_time, sizeof(started_time), LABEL_FILE_TIME_FORMAT, &tm);
time_t now = time(NULL);
localtime_r(&now, &tm);
strftime(finished_time, sizeof(finished_time), LABEL_FILE_TIME_FORMAT, &tm);
// Print the label contents.
ret = fprintf(file,
"%s"
LABEL_FILE_FORMAT
"host: %s\n"
"started_time: %s\n"
"finished_time: %s\n"
"knot_version: %s\n"
"parameters: +%szonefile +%sjournal +%stimers +%skaspdb +%scatalog "
"+backupdir %s\n"
"zone_count: %d\n",
label_file_head,
BACKUP_VERSION, hostname, started_time, finished_time, PACKAGE_VERSION,
ctx->backup_zonefile ? "" : "no",
ctx->backup_journal ? "" : "no",
ctx->backup_timers ? "" : "no",
ctx->backup_kaspdb ? "" : "no",
ctx->backup_catalog ? "" : "no",
ctx->backup_dir,
ctx->zone_count);
ret = (ret < 0) ? knot_map_errno() : KNOT_EOK;
fclose(file);
return ret;
}
static int get_backup_format(const char *full_path, bool forced, knot_backup_format_t *format)
{
int ret = KNOT_EMALF;
struct stat sb;
if (stat(full_path, &sb) != 0) {
ret = knot_map_errno();
if (ret == KNOT_ENOENT) {
if (forced) {
*format = BACKUP_FORMAT_1;
ret = KNOT_EOK;
} else {
ret = KNOT_EMALF;
}
}
return ret;
}
// getline() from an empty file results in EAGAIN, therefore avoid doing so.
if (!S_ISREG(sb.st_mode) || sb.st_size == 0) {
return ret;
}
FILE *file = fopen(full_path, "r");
if (file == NULL) {
return knot_map_errno();
}
char *line = NULL;
size_t line_size = 0;
// Check for the header line first.
if (knot_getline(&line, &line_size, file) == -1) {
ret = knot_map_errno();
goto done;
}
if (strcmp(line, label_file_head) != 0) {
goto done;
}
int value;
while (knot_getline(&line, &line_size, file) != -1) {
if (sscanf(line, LABEL_FILE_FORMAT, &value) != 0) {
if ((BACKUP_FORMAT_1 < value) && (value < BACKUP_FORMAT_TERM)) {
*format = value;
ret = KNOT_EOK;
}
break;
}
}
done:
free(line);
fclose(file);
return ret;
}
int zone_backup_init(bool restore_mode, bool forced, const char *backup_dir,
size_t kasp_db_size, size_t timer_db_size, size_t journal_db_size,
size_t catalog_db_size, zone_backup_ctx_t **out_ctx)
{
if (backup_dir == NULL || out_ctx == NULL) {
return KNOT_EINVAL;
}
size_t backup_dir_len = strlen(backup_dir) + 1;
zone_backup_ctx_t *ctx = malloc(sizeof(*ctx) + backup_dir_len);
if (ctx == NULL) {
return KNOT_ENOMEM;
}
ctx->restore_mode = restore_mode;
ctx->backup_format = BACKUP_VERSION;
ctx->backup_global = false;
ctx->readers = 1;
ctx->failed = false;
ctx->init_time = time(NULL);
ctx->zone_count = 0;
ctx->backup_dir = (char *)(ctx + 1);
memcpy(ctx->backup_dir, backup_dir, backup_dir_len);
int ret;
struct stat sb;
// Make sure the source/target backup directory exists.
if (restore_mode) {
if (stat(backup_dir, &sb) != 0) {
free(ctx);
return knot_map_errno();
}
if (!S_ISDIR(sb.st_mode)) {
free(ctx);
return KNOT_ENOTDIR;
}
} else {
ret = make_dir(backup_dir, S_IRWXU|S_IRWXG, true);
if (ret != KNOT_EOK) {
free(ctx);
return ret;
}
}
// The \0 terminator is already included in the sizeof() value, thus the sum
// covers one additional char for '/'.
char full_path[backup_dir_len + MAX_LENGTH];
// Check for existence of a label file and the backup format used.
sprintf(full_path, "%s/%s", (ctx)->backup_dir, label_file_name);
if (restore_mode) {
ret = get_backup_format(full_path, forced, &ctx->backup_format);
if (ret != KNOT_EOK) {
free(ctx);
return ret;
}
} else {
if (stat(full_path, &sb) == 0) {
free(ctx);
return KNOT_EEXIST;
}
}
// Make (or check for existence of) a lock file.
sprintf(full_path, "%s/%s", (ctx)->backup_dir, lock_file_name);
if (restore_mode) {
// Just check.
if (stat(full_path, &sb) == 0) {
free(ctx);
return KNOT_EBUSY;
}
} else {
// Create it (which also checks for its existence).
int lock_file = open(full_path, O_CREAT|O_EXCL, S_IRUSR|S_IWUSR);
if (lock_file < 0) {
free(ctx);
// Make the reported error better understandable than KNOT_EEXIST.
return errno == EEXIST ? KNOT_EBUSY : knot_map_errno();
}
close(lock_file);
}
knot_spin_init(&ctx->readers_spinlock);
char db_dir[backup_dir_len + 16];
(void)snprintf(db_dir, sizeof(db_dir), "%s/keys", backup_dir);
knot_lmdb_init(&ctx->bck_kasp_db, db_dir, kasp_db_size, 0, "keys_db");
(void)snprintf(db_dir, sizeof(db_dir), "%s/timers", backup_dir);
knot_lmdb_init(&ctx->bck_timer_db, db_dir, timer_db_size, 0, NULL);
(void)snprintf(db_dir, sizeof(db_dir), "%s/journal", backup_dir);
knot_lmdb_init(&ctx->bck_journal, db_dir, journal_db_size, 0, NULL);
(void)snprintf(db_dir, sizeof(db_dir), "%s/catalog", backup_dir);
knot_lmdb_init(&ctx->bck_catalog, db_dir, catalog_db_size, 0, NULL);
*out_ctx = ctx;
return KNOT_EOK;
}
int zone_backup_deinit(zone_backup_ctx_t *ctx)
{
if (ctx == NULL) {
return KNOT_ENOENT;
}
int ret = KNOT_EOK;
knot_spin_lock(&ctx->readers_spinlock);
assert(ctx->readers > 0);
size_t left = --ctx->readers;
knot_spin_unlock(&ctx->readers_spinlock);
if (left == 0) {
knot_lmdb_deinit(&ctx->bck_catalog);
knot_lmdb_deinit(&ctx->bck_journal);
knot_lmdb_deinit(&ctx->bck_timer_db);
knot_lmdb_deinit(&ctx->bck_kasp_db);
knot_spin_destroy(&ctx->readers_spinlock);
size_t backup_dir_len = strlen((ctx)->backup_dir) + 1;
char full_path[backup_dir_len + MAX_LENGTH];
if (!ctx->restore_mode && !ctx->failed) {
// Create the label file first.
sprintf(full_path, "%s/%s", (ctx)->backup_dir, label_file_name);
ret = make_label_file(ctx, full_path);
if (ret == KNOT_EOK) {
// Remove the lock file only when the label file has benn created.
sprintf(full_path, "%s/%s", (ctx)->backup_dir, lock_file_name);
unlink(full_path);
} else {
log_error("failed to create a backup label in %s", (ctx)->backup_dir);
}
}
zone_backups_rem(ctx);
free(ctx);
}
return ret;
}
void zone_backups_init(zone_backup_ctxs_t *ctxs)
{
init_list(&ctxs->ctxs);
pthread_mutex_init(&ctxs->mutex, NULL);
}
void zone_backups_deinit(zone_backup_ctxs_t *ctxs)
{
zone_backup_ctx_t *ctx, *nxt;
WALK_LIST_DELSAFE(ctx, nxt, ctxs->ctxs) {
log_warning("backup to '%s' in progress, terminating, will be incomplete",
ctx->backup_dir);
ctx->readers = 1; // ensure full deinit
ctx->failed = true;
(void)zone_backup_deinit(ctx);
}
pthread_mutex_destroy(&ctxs->mutex);
}
void zone_backups_add(zone_backup_ctxs_t *ctxs, zone_backup_ctx_t *ctx)
{
pthread_mutex_lock(&ctxs->mutex);
add_tail(&ctxs->ctxs, (node_t *)ctx);
pthread_mutex_unlock(&ctxs->mutex);
}
static zone_backup_ctxs_t *get_ctxs_trick(zone_backup_ctx_t *ctx)
{
node_t *n = (node_t *)ctx;
while (n->prev != NULL) {
n = n->prev;
}
return (zone_backup_ctxs_t *)n;
}
void zone_backups_rem(zone_backup_ctx_t *ctx)
{
zone_backup_ctxs_t *ctxs = get_ctxs_trick(ctx);
pthread_mutex_lock(&ctxs->mutex);
rem_node((node_t *)ctx);
pthread_mutex_unlock(&ctxs->mutex);
}
static char *dir_file(const char *dir_name, const char *file_name)
{
const char *basename = strrchr(file_name, '/');
if (basename == NULL) {
basename = file_name;
} else {
basename++;
}
return sprintf_alloc("%s/%s", dir_name, basename);
}
static int backup_key(key_params_t *parm, dnssec_keystore_t *from, dnssec_keystore_t *to)
{
dnssec_key_t *key = NULL;
int ret = dnssec_key_new(&key);
if (ret != DNSSEC_EOK) {
return knot_error_from_libdnssec(ret);
}
dnssec_key_set_algorithm(key, parm->algorithm);
ret = dnssec_keystore_get_private(from, parm->id, key);
if (ret == DNSSEC_EOK) {
ret = dnssec_keystore_set_private(to, key);
}
dnssec_key_free(key);
return knot_error_from_libdnssec(ret);
}
static conf_val_t get_zone_policy(conf_t *conf, const knot_dname_t *zone)
{
conf_val_t policy;
// Global modules don't use DNSSEC policy so check zone modules only.
conf_val_t modules = conf_zone_get(conf, C_MODULE, zone);
while (modules.code == KNOT_EOK) {
conf_mod_id_t *mod_id = conf_mod_id(&modules);
if (mod_id != NULL && strcmp(mod_id->name + 1, "mod-onlinesign") == 0) {
policy = conf_mod_get(conf, C_POLICY, mod_id);
conf_id_fix_default(&policy);
conf_free_mod_id(mod_id);
return policy;
}
conf_free_mod_id(mod_id);
conf_val_next(&modules);
}
// Use default policy if none is configured.
policy = conf_zone_get(conf, C_DNSSEC_POLICY, zone);
conf_id_fix_default(&policy);
return policy;
}
#define LOG_FAIL(action) log_zone_warning(zone->name, "%s, %s failed (%s)", ctx->restore_mode ? \
"restore" : "backup", (action), knot_strerror(ret))
#define LOG_MARK_FAIL(action) LOG_FAIL(action); \
ctx->failed = true
static int backup_keystore(conf_t *conf, zone_t *zone, zone_backup_ctx_t *ctx)
{
dnssec_keystore_t *from = NULL, *to = NULL;
conf_val_t policy_id = get_zone_policy(conf, zone->name);
unsigned backend_type = 0;
int ret = zone_init_keystore(conf, &policy_id, &from, &backend_type);
if (ret != KNOT_EOK) {
LOG_FAIL("keystore init");
return ret;
}
if (backend_type == KEYSTORE_BACKEND_PKCS11) {
log_zone_warning(zone->name, "private keys from PKCS#11 aren't subject of backup/restore");
(void)dnssec_keystore_deinit(from);
return KNOT_EOK;
}
char kasp_dir[strlen(ctx->backup_dir) + 6];
(void)snprintf(kasp_dir, sizeof(kasp_dir), "%s/keys", ctx->backup_dir);
ret = keystore_load("keys", KEYSTORE_BACKEND_PEM, kasp_dir, &to);
if (ret != KNOT_EOK) {
LOG_FAIL("keystore load");
goto done;
}
BACKUP_SWAP(ctx, from, to);
list_t key_params;
init_list(&key_params);
ret = kasp_db_list_keys(zone->kaspdb, zone->name, &key_params);
ret = (ret == KNOT_ENOENT ? KNOT_EOK : ret);
if (ret != KNOT_EOK) {
LOG_FAIL("keystore list");
goto done;
}
ptrnode_t *n;
WALK_LIST(n, key_params) {
key_params_t *parm = n->d;
if (ret == KNOT_EOK && !parm->is_pub_only) {
ret = backup_key(parm, from, to);
}
free_key_params(parm);
}
if (ret != KNOT_EOK) {
LOG_FAIL("key copy");
}
ptrlist_deep_free(&key_params, NULL);
done:
(void)dnssec_keystore_deinit(to);
(void)dnssec_keystore_deinit(from);
return ret;
}
#define ABORT_IF_ENOMEM(param) if (param == NULL) { \
ret = KNOT_ENOMEM; \
goto done_zfile; \
}
int zone_backup(conf_t *conf, zone_t *zone)
{
zone_backup_ctx_t *ctx = zone->backup_ctx;
if (ctx == NULL) {
return KNOT_EINVAL;
}
int ret = KNOT_EOK;
int ret_deinit;
if (ctx->backup_zonefile) {
char *local_zf = conf_zonefile(conf, zone->name);
char *backup_zfiles_dir = NULL, *backup_zf, *zone_name_str;
switch (ctx->backup_format) {
case BACKUP_FORMAT_1:
backup_zf = dir_file(ctx->backup_dir, local_zf);
ABORT_IF_ENOMEM(backup_zf);
break;
case BACKUP_FORMAT_2:
default:
backup_zfiles_dir = dir_file(ctx->backup_dir, "zonefiles");
ABORT_IF_ENOMEM(backup_zfiles_dir);
zone_name_str = knot_dname_to_str_alloc(zone->name);
ABORT_IF_ENOMEM(zone_name_str);
backup_zf = sprintf_alloc("%s/%szone", backup_zfiles_dir, zone_name_str);
ABORT_IF_ENOMEM(backup_zf);
free(zone_name_str);
}
if (ctx->restore_mode) {
struct stat st;
if (stat(backup_zf, &st) == 0) {
ret = make_path(local_zf, S_IRWXU | S_IRWXG);
if (ret == KNOT_EOK) {
ret = copy_file(local_zf, backup_zf);
}
} else {
ret = errno == ENOENT ? KNOT_EFILE : knot_map_errno();
/* If there's no zone file in the backup, remove any old zone file
* from the repository.
*/
if (ret == KNOT_EFILE) {
unlink(local_zf);
}
}
} else {
conf_val_t val = conf_zone_get(conf, C_ZONEFILE_SYNC, zone->name);
bool can_flush = (conf_int(&val) > -1);
ret = make_dir(backup_zfiles_dir, S_IRWXU | S_IRWXG, true);
if (ret == KNOT_EOK) {
if (can_flush) {
if (zone->contents != NULL) {
ret = zonefile_write(backup_zf, zone->contents);
} else {
log_zone_notice(zone->name,
"empty zone, skipping a zone file backup");
}
} else {
ret = copy_file(backup_zf, local_zf);
}
}
}
done_zfile:
free(backup_zf);
free(backup_zfiles_dir);
free(local_zf);
if (ret == KNOT_EFILE) {
log_zone_notice(zone->name, "no zone file, skipping a zone file %s",
ctx->restore_mode ? "restore" : "backup");
ret = KNOT_EOK;
} else if (ret != KNOT_EOK) {
LOG_MARK_FAIL("zone file");
goto done;
}
}
if (ctx->backup_kaspdb) {
knot_lmdb_db_t *kasp_from = zone->kaspdb, *kasp_to = &ctx->bck_kasp_db;
BACKUP_SWAP(ctx, kasp_from, kasp_to);
if (knot_lmdb_exists(kasp_from)) {
ret = kasp_db_backup(zone->name, kasp_from, kasp_to);
if (ret != KNOT_EOK) {
LOG_MARK_FAIL("KASP database");
goto done;
}
ret = backup_keystore(conf, zone, ctx);
if (ret != KNOT_EOK) {
ctx->failed = true;
goto done;
}
}
}
if (ctx->backup_journal) {
knot_lmdb_db_t *j_from = zone->journaldb, *j_to = &ctx->bck_journal;
BACKUP_SWAP(ctx, j_from, j_to);
ret = journal_copy_with_md(j_from, j_to, zone->name);
} else if (ctx->restore_mode) {
ret = journal_scrape_with_md(zone_journal(zone), true);
}
if (ret != KNOT_EOK) {
LOG_MARK_FAIL("journal");
goto done;
}
if (ctx->backup_timers) {
ret = knot_lmdb_open(&ctx->bck_timer_db);
if (ret != KNOT_EOK) {
LOG_MARK_FAIL("timers open");
goto done;
}
if (ctx->restore_mode) {
ret = zone_timers_read(&ctx->bck_timer_db, zone->name, &zone->timers);
zone_timers_sanitize(conf, zone);
} else {
ret = zone_timers_write(&ctx->bck_timer_db, zone->name, &zone->timers);
}
if (ret != KNOT_EOK) {
LOG_MARK_FAIL("timers");
goto done;
}
}
done:
ret_deinit = zone_backup_deinit(ctx);
zone->backup_ctx = NULL;
return (ret != KNOT_EOK) ? ret : ret_deinit;
}
int global_backup(zone_backup_ctx_t *ctx, catalog_t *catalog,
const knot_dname_t *zone_only)
{
if (!ctx->backup_catalog) {
return KNOT_EOK;
}
knot_lmdb_db_t *cat_from = &catalog->db, *cat_to = &ctx->bck_catalog;
BACKUP_SWAP(ctx, cat_from, cat_to);
int ret = catalog_copy(cat_from, cat_to, zone_only, !ctx->restore_mode);
if (ret != KNOT_EOK) {
ctx->failed = true;
}
return ret;
}