xref: /netbsd-src/external/mpl/bind/dist/bin/tools/named-nzd2nzf.c (revision f3cfa6f6ce31685c6c4a758bc430e69eb99f50a4)
1 /*	$NetBSD: named-nzd2nzf.c,v 1.3 2019/01/09 16:55:05 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 http://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 #include <config.h>
15 
16 #ifndef HAVE_LMDB
17 #error This program requires the LMDB library.
18 #endif
19 
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <lmdb.h>
23 
24 #include <dns/view.h>
25 
26 #include <isc/print.h>
27 
28 int
29 main (int argc, char *argv[]) {
30 	int status;
31 	const char *path;
32 	MDB_env *env = NULL;
33 	MDB_txn *txn = NULL;
34 	MDB_cursor *cursor = NULL;
35 	MDB_dbi dbi;
36 	MDB_val key, data;
37 
38 	if (argc != 2) {
39 		fprintf(stderr, "Usage: named-nzd2nzf <nzd-path>\n");
40 		exit(1);
41 	}
42 
43 	path = argv[1];
44 
45 	status = mdb_env_create(&env);
46 	if (status != MDB_SUCCESS) {
47 		fprintf(stderr, "named-nzd2nzf: mdb_env_create: %s",
48 			mdb_strerror(status));
49 		exit(1);
50 	}
51 
52 	status = mdb_env_open(env, path, DNS_LMDB_FLAGS, 0600);
53 	if (status != MDB_SUCCESS) {
54 		fprintf(stderr, "named-nzd2nzf: mdb_env_open: %s",
55 			mdb_strerror(status));
56 		exit(1);
57 	}
58 
59 	status = mdb_txn_begin(env, 0, MDB_RDONLY, &txn);
60 	if (status != MDB_SUCCESS) {
61 		fprintf(stderr, "named-nzd2nzf: mdb_txn_begin: %s",
62 			mdb_strerror(status));
63 		exit(1);
64 	}
65 
66 	status = mdb_dbi_open(txn, NULL, 0, &dbi);
67 	if (status != MDB_SUCCESS) {
68 		fprintf(stderr, "named-nzd2nzf: mdb_dbi_open: %s",
69 			mdb_strerror(status));
70 		exit(1);
71 	}
72 
73 	status = mdb_cursor_open(txn, dbi, &cursor);
74 	if (status != MDB_SUCCESS) {
75 		fprintf(stderr, "named-nzd2nzf: mdb_cursor_open: %s",
76 			mdb_strerror(status));
77 		exit(1);
78 	}
79 
80 	for (status = mdb_cursor_get(cursor, &key, &data, MDB_FIRST);
81 	     status == MDB_SUCCESS;
82 	     status = mdb_cursor_get(cursor, &key, &data, MDB_NEXT))
83 	{
84 		if (key.mv_data == NULL || key.mv_size == 0 ||
85 		    data.mv_data == NULL || data.mv_size == 0)
86 		{
87 			fprintf(stderr,
88 				"named-nzd2nzf: empty column found in "
89 				"database '%s'", path);
90 			exit(1);
91 		}
92 
93 		/* zone zonename { config; }; */
94 		printf("zone \"%.*s\" %.*s;\n",
95 		       (int) key.mv_size, (char *) key.mv_data,
96 		       (int) data.mv_size, (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