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.
37 #include <isc/msgcat.h>
38 #include <isc/region.h>
39 #include <isc/result.h>
40 #include <isc/types.h>
48 #include <named/bdb.h>
49 #include <named/globals.h>
50 #include <named/config.h>
54 #define DRIVERNAME "bdb"
56 static dns_sdbimplementation_t
*bdb_imp
;
59 bdb_create(const char *zone
, int argc
, char **argv
,
60 void *unused
, void **dbdata
)
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
,
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
);
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",
103 bdb_lookup(const char *zone
, const char *name
, void *dbdata
,
109 isc_consttextregion_t ttltext
;
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",
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
);
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
);
152 return ISC_R_SUCCESS
;
156 bdb_allnodes(const char *zone
, void *dbdata
, dns_sdballnodes_t
*n
)
161 isc_consttextregion_t ttltext
;
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",
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
) !=
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
;
201 return ISC_R_SUCCESS
;
205 bdb_destroy(const char *zone
, void *unused
, void **dbdata
)
211 (*(DB
**)dbdata
)->close(*(DB
**)dbdata
, 0);
213 return ISC_R_SUCCESS
;
219 static dns_sdbmethods_t bdb_methods
= {
227 return dns_sdb_register(DRIVERNAME
, &bdb_methods
, NULL
, 0, ns_g_mctx
,
236 dns_sdb_unregister(&bdb_imp
);