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
|
/* Copyright (C) 2022 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 <getopt.h>
#include <stdlib.h>
#include <string.h>
#include "knot/catalog/catalog_db.h"
#include "utils/common/msg.h"
#include "utils/common/params.h"
#include "utils/common/util_conf.h"
#define PROGRAM_NAME "kcatalogprint"
static knot_dname_t *filter_member = NULL;
static knot_dname_t *filter_catalog = NULL;
static void print_help(void)
{
printf("Usage: %s [-c | -C | -D <path>] [parameters]\n"
"\n"
"Parameters:\n"
" -c, --config <file> Path to a textual configuration file.\n"
" (default %s)\n"
" -C, --confdb <dir> Path to a configuration database directory.\n"
" (default %s)\n"
" -D, --dir <path> Path to a catalog database directory, use default\n"
" configuration.\n"
" -a, --catalog <name> Filter the output by catalog zone name.\n"
" -m, --member <name> Filter the output by member zone name.\n"
" -h, --help Print the program help.\n"
" -V, --version Print the program version.\n",
PROGRAM_NAME, CONF_DEFAULT_FILE, CONF_DEFAULT_DBDIR);
}
static void print_dname(const knot_dname_t *d)
{
knot_dname_txt_storage_t tmp;
knot_dname_to_str(tmp, d, sizeof(tmp));
printf("%s ", tmp);
}
static int catalog_print_cb(const knot_dname_t *mem, const knot_dname_t *ow,
const knot_dname_t *cz, const char *group, void *ctx)
{
if (filter_catalog != NULL && !knot_dname_is_equal(filter_catalog, cz)) {
return KNOT_EOK;
}
print_dname(mem);
print_dname(ow);
print_dname(cz);
printf("%s\n", group);
(*(ssize_t *)ctx)++;
return KNOT_EOK;
}
static void catalog_print(catalog_t *cat)
{
ssize_t total = 0;
printf(";; <member zone> <record owner> <catalog zone> <group>\n");
if (cat != NULL) {
int ret = catalog_open(cat);
if (ret == KNOT_EOK) {
ret = catalog_apply(cat, filter_member, catalog_print_cb, &total, false);
}
if (ret != KNOT_EOK) {
ERR2("failed to print catalog (%s)", knot_strerror(ret));
return;
}
}
printf("Total records: %zd\n", total);
}
static void params_cleanup(void)
{
free(filter_member);
free(filter_catalog);
}
int main(int argc, char *argv[])
{
struct option opts[] = {
{ "config", required_argument, NULL, 'c' },
{ "confdb", required_argument, NULL, 'C' },
{ "dir", required_argument, NULL, 'D' },
{ "catalog", required_argument, NULL, 'a' },
{ "member", required_argument, NULL, 'm' },
{ "help", no_argument, NULL, 'h' },
{ "version", no_argument, NULL, 'V' },
{ NULL }
};
int opt = 0;
while ((opt = getopt_long(argc, argv, "c:C:D:a:m:hV", opts, NULL)) != -1) {
switch (opt) {
case 'c':
if (util_conf_init_file(optarg) != KNOT_EOK) {
goto failure;
}
break;
case 'C':
if (util_conf_init_confdb(optarg) != KNOT_EOK) {
goto failure;
}
break;
case 'D':
if (util_conf_init_justdb("catalog-db", optarg) != KNOT_EOK) {
goto failure;
}
break;
case 'a':
free(filter_catalog);
filter_catalog = knot_dname_from_str_alloc(optarg);
knot_dname_to_lower(filter_catalog);
break;
case 'm':
free(filter_member);
filter_member = knot_dname_from_str_alloc(optarg);
knot_dname_to_lower(filter_member);
break;
case 'h':
print_help();
goto success;
case 'V':
print_version(PROGRAM_NAME);
goto success;
default:
print_help();
goto failure;
}
}
// Backward compatibility.
if (argc - optind > 0) {
WARN2("obsolete parameter specified");
if (util_conf_init_justdb("catalog-db", argv[optind]) != KNOT_EOK) {
goto failure;
}
optind++;
}
if (util_conf_init_default(true) != KNOT_EOK) {
goto failure;
}
catalog_t c = { { 0 } };
char *db = conf_db(conf(), C_CATALOG_DB);
catalog_init(&c, db, 0); // mapsize grows automatically
free(db);
catalog_print(&c);
catalog_deinit(&c);
success:
params_cleanup();
util_conf_deinit();
return EXIT_SUCCESS;
failure:
params_cleanup();
util_conf_deinit();
return EXIT_FAILURE;
}
|