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