xref: /netbsd-src/external/mpl/bind/dist/bin/tools/named-nzd2nzf.c (revision dd3ee07da436799d8de85f3055253118b76bf345)
1 /*	$NetBSD: named-nzd2nzf.c,v 1.7 2021/04/29 17:26:10 christos Exp $	*/
2 
3 /*
4  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
5  *
6  * This Source Code Form is subject to the terms of the Mozilla Public
7  * License, v. 2.0. If a copy of the MPL was not distributed with this
8  * file, you can obtain one at https://mozilla.org/MPL/2.0/.
9  *
10  * See the COPYRIGHT file distributed with this work for additional
11  * information regarding copyright ownership.
12  */
13 
14 #ifndef HAVE_LMDB
15 #error This program requires the LMDB library.
16 #endif /* ifndef HAVE_LMDB */
17 
18 #include <lmdb.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 
22 #include <isc/print.h>
23 
24 #include <dns/lmdb.h>
25 #include <dns/view.h>
26 
27 int
28 main(int argc, char *argv[]) {
29 	int status;
30 	const char *path;
31 	MDB_env *env = NULL;
32 	MDB_txn *txn = NULL;
33 	MDB_cursor *cursor = NULL;
34 	MDB_dbi dbi;
35 	MDB_val key, data;
36 
37 	if (argc != 2) {
38 		fprintf(stderr, "Usage: named-nzd2nzf <nzd-path>\n");
39 		exit(1);
40 	}
41 
42 	path = argv[1];
43 
44 	status = mdb_env_create(&env);
45 	if (status != MDB_SUCCESS) {
46 		fprintf(stderr, "named-nzd2nzf: mdb_env_create: %s",
47 			mdb_strerror(status));
48 		exit(1);
49 	}
50 
51 	status = mdb_env_open(env, path, DNS_LMDB_FLAGS, 0600);
52 	if (status != MDB_SUCCESS) {
53 		fprintf(stderr, "named-nzd2nzf: mdb_env_open: %s",
54 			mdb_strerror(status));
55 		exit(1);
56 	}
57 
58 	status = mdb_txn_begin(env, 0, MDB_RDONLY, &txn);
59 	if (status != MDB_SUCCESS) {
60 		fprintf(stderr, "named-nzd2nzf: mdb_txn_begin: %s",
61 			mdb_strerror(status));
62 		exit(1);
63 	}
64 
65 	status = mdb_dbi_open(txn, NULL, 0, &dbi);
66 	if (status != MDB_SUCCESS) {
67 		fprintf(stderr, "named-nzd2nzf: mdb_dbi_open: %s",
68 			mdb_strerror(status));
69 		exit(1);
70 	}
71 
72 	status = mdb_cursor_open(txn, dbi, &cursor);
73 	if (status != MDB_SUCCESS) {
74 		fprintf(stderr, "named-nzd2nzf: mdb_cursor_open: %s",
75 			mdb_strerror(status));
76 		exit(1);
77 	}
78 
79 	for (status = mdb_cursor_get(cursor, &key, &data, MDB_FIRST);
80 	     status == MDB_SUCCESS;
81 	     status = mdb_cursor_get(cursor, &key, &data, MDB_NEXT))
82 	{
83 		if (key.mv_data == NULL || key.mv_size == 0 ||
84 		    data.mv_data == NULL || data.mv_size == 0)
85 		{
86 			fprintf(stderr,
87 				"named-nzd2nzf: empty column found in "
88 				"database '%s'",
89 				path);
90 			exit(1);
91 		}
92 
93 		/* zone zonename { config; }; */
94 		printf("zone \"%.*s\" %.*s;\n", (int)key.mv_size,
95 		       (char *)key.mv_data, (int)data.mv_size,
96 		       (char *)data.mv_data);
97 	}
98 
99 	mdb_cursor_close(cursor);
100 	mdb_txn_abort(txn);
101 	mdb_env_close(env);
102 	exit(0);
103 }
104