Sync usage with man page.
[netbsd-mini2440.git] / external / bsd / bind / dist / contrib / sdb / bdb / bdb.c
blob3d6d989b6ccbc6ba4db21a71186549aedb1b3c2f
1 /* $NetBSD$ */
3 /*
4 * Copyright (C) 2002 Nuno M. Rodrigues.
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND NUNO M. RODRIGUES
11 * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
12 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
13 * INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
14 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
15 * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
16 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
17 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 /* Id: bdb.c,v 1.1 2002/05/16 04:25:22 marka Exp */
23 * BIND 9.1.x simple database driver
24 * implementation, using Berkeley DB.
27 #include <errno.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <stdlib.h>
32 #include <isc/file.h>
33 #include <isc/log.h>
34 #include <isc/lib.h>
35 #include <isc/mem.h>
36 #include <isc/msgs.h>
37 #include <isc/msgcat.h>
38 #include <isc/region.h>
39 #include <isc/result.h>
40 #include <isc/types.h>
41 #include <isc/util.h>
43 #include <dns/sdb.h>
44 #include <dns/log.h>
45 #include <dns/lib.h>
46 #include <dns/ttl.h>
48 #include <named/bdb.h>
49 #include <named/globals.h>
50 #include <named/config.h>
52 #include <db.h>
54 #define DRIVERNAME "bdb"
56 static dns_sdbimplementation_t *bdb_imp;
58 static isc_result_t
59 bdb_create(const char *zone, int argc, char **argv,
60 void *unused, void **dbdata)
62 int ret;
64 UNUSED(zone);
65 UNUSED(unused);
67 if (argc < 1)
68 return ISC_R_FAILURE; /* database path must be given */
70 if (db_create((DB **)dbdata, NULL, 0) != 0) {
72 * XXX Should use dns_msgcat et al
73 * but seems to be unavailable.
75 isc_log_iwrite(dns_lctx, DNS_LOGCATEGORY_DATABASE,
76 DNS_LOGMODULE_SDB, ISC_LOG_CRITICAL, isc_msgcat,
77 ISC_MSGSET_GENERAL, ISC_MSG_FATALERROR,
78 "db_create");
79 return ISC_R_FAILURE;
82 if (isc_file_exists(*argv) != ISC_TRUE) {
83 isc_log_iwrite(dns_lctx, DNS_LOGCATEGORY_DATABASE,
84 DNS_LOGMODULE_SDB, ISC_LOG_CRITICAL, isc_msgcat,
85 ISC_MSGSET_GENERAL, ISC_MSG_FATALERROR,
86 "isc_file_exists: %s", *argv);
87 return ISC_R_FAILURE;
90 if ((ret = (*(DB **)dbdata)->open(*(DB **)dbdata, *argv, NULL, DB_HASH,
91 DB_RDONLY, 0)) != 0) {
92 isc_log_iwrite(dns_lctx, DNS_LOGCATEGORY_DATABASE,
93 DNS_LOGMODULE_SDB, ISC_LOG_CRITICAL,
94 isc_msgcat, ISC_MSGSET_GENERAL,
95 ISC_MSG_FATALERROR, "DB->open: %s",
96 db_strerror(ret));
97 return ISC_R_FAILURE;
99 return ISC_R_SUCCESS;
102 static isc_result_t
103 bdb_lookup(const char *zone, const char *name, void *dbdata,
104 dns_sdblookup_t *l)
106 int ret;
107 char *type, *rdata;
108 dns_ttl_t ttl;
109 isc_consttextregion_t ttltext;
110 DBC *c;
111 DBT key, data;
113 UNUSED(zone);
115 if ((ret = ((DB *)dbdata)->cursor((DB *)dbdata, NULL, &c, 0)) != 0) {
116 isc_log_iwrite(dns_lctx, DNS_LOGCATEGORY_DATABASE,
117 DNS_LOGMODULE_SDB, ISC_LOG_ERROR,
118 isc_msgcat, ISC_MSGSET_GENERAL,
119 ISC_MSG_FAILED, "DB->cursor: %s",
120 db_strerror(ret));
121 return ISC_R_FAILURE;
124 memset(&key, 0, sizeof(DBT));
125 memset(&data, 0, sizeof(DBT));
127 (const char *)key.data = name;
128 key.size = strlen(name);
130 ret = c->c_get(c, &key, &data, DB_SET);
131 while (ret == 0) {
132 ((char *)key.data)[key.size] = 0;
133 ((char *)data.data)[data.size] = 0;
134 ttltext.base = strtok((char *)data.data, " ");
135 ttltext.length = strlen(ttltext.base);
136 dns_ttl_fromtext((isc_textregion_t *)&ttltext, &ttl);
137 type = strtok(NULL, " ");
138 rdata = type + strlen(type) + 1;
140 if (dns_sdb_putrr(l, type, ttl, rdata) != ISC_R_SUCCESS) {
141 isc_log_iwrite(dns_lctx,
142 DNS_LOGCATEGORY_DATABASE,
143 DNS_LOGMODULE_SDB, ISC_LOG_ERROR,
144 isc_msgcat, ISC_MSGSET_GENERAL,
145 ISC_MSG_FAILED, "dns_sdb_putrr");
146 return ISC_R_FAILURE;
148 ret = c->c_get(c, &key, &data, DB_NEXT_DUP);
151 c->c_close(c);
152 return ISC_R_SUCCESS;
155 static isc_result_t
156 bdb_allnodes(const char *zone, void *dbdata, dns_sdballnodes_t *n)
158 int ret;
159 char *type, *rdata;
160 dns_ttl_t ttl;
161 isc_consttextregion_t ttltext;
162 DBC *c;
163 DBT key, data;
165 UNUSED(zone);
167 if ((ret = ((DB *)dbdata)->cursor((DB *)dbdata, NULL, &c, 0)) != 0) {
168 isc_log_iwrite(dns_lctx, DNS_LOGCATEGORY_DATABASE,
169 DNS_LOGMODULE_SDB, ISC_LOG_ERROR,
170 isc_msgcat, ISC_MSGSET_GENERAL,
171 ISC_MSG_FAILED, "DB->cursor: %s",
172 db_strerror(ret));
173 return ISC_R_FAILURE;
176 memset(&key, 0, sizeof(DBT));
177 memset(&data, 0, sizeof(DBT));
179 while (c->c_get(c, &key, &data, DB_NEXT) == 0) {
180 ((char *)key.data)[key.size] = 0;
181 ((char *)data.data)[data.size] = 0;
182 ttltext.base = strtok((char *)data.data, " ");
183 ttltext.length = strlen(ttltext.base);
184 dns_ttl_fromtext((isc_textregion_t *)&ttltext, &ttl);
185 type = strtok(NULL, " ");
186 rdata = type + strlen(type) + 1;
188 if (dns_sdb_putnamedrr(n, key.data, type, ttl, rdata) !=
189 ISC_R_SUCCESS) {
190 isc_log_iwrite(dns_lctx,
191 DNS_LOGCATEGORY_DATABASE,
192 DNS_LOGMODULE_SDB, ISC_LOG_ERROR,
193 isc_msgcat, ISC_MSGSET_GENERAL,
194 ISC_MSG_FAILED, "dns_sdb_putnamedrr");
195 return ISC_R_FAILURE;
200 c->c_close(c);
201 return ISC_R_SUCCESS;
204 static isc_result_t
205 bdb_destroy(const char *zone, void *unused, void **dbdata)
208 UNUSED(zone);
209 UNUSED(unused);
211 (*(DB **)dbdata)->close(*(DB **)dbdata, 0);
213 return ISC_R_SUCCESS;
216 isc_result_t
217 bdb_init(void)
219 static dns_sdbmethods_t bdb_methods = {
220 bdb_lookup,
221 NULL,
222 bdb_allnodes,
223 bdb_create,
224 bdb_destroy
227 return dns_sdb_register(DRIVERNAME, &bdb_methods, NULL, 0, ns_g_mctx,
228 &bdb_imp);
231 void
232 bdb_clear(void)
235 if (bdb_imp != NULL)
236 dns_sdb_unregister(&bdb_imp);