00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061 #include <config.h>
00062
00063 #include <dns/db.h>
00064 #include <dns/dlz.h>
00065 #include <dns/fixedname.h>
00066 #include <dns/log.h>
00067 #include <dns/master.h>
00068 #include <dns/ssu.h>
00069 #include <dns/zone.h>
00070
00071
00072 #include <isc/buffer.h>
00073 #include <isc/magic.h>
00074 #include <isc/mem.h>
00075 #include <isc/once.h>
00076 #include <isc/rwlock.h>
00077 #include <isc/string.h>
00078 #include <isc/util.h>
00079
00080
00081
00082
00083
00084 static ISC_LIST(dns_dlzimplementation_t) dlz_implementations;
00085 static isc_rwlock_t dlz_implock;
00086 static isc_once_t once = ISC_ONCE_INIT;
00087
00088 static void
00089 dlz_initialize(void) {
00090 RUNTIME_CHECK(isc_rwlock_init(&dlz_implock, 0, 0) == ISC_R_SUCCESS);
00091 ISC_LIST_INIT(dlz_implementations);
00092 }
00093
00094
00095
00096
00097 static inline dns_dlzimplementation_t *
00098 dlz_impfind(const char *name) {
00099 dns_dlzimplementation_t *imp;
00100
00101 for (imp = ISC_LIST_HEAD(dlz_implementations);
00102 imp != NULL;
00103 imp = ISC_LIST_NEXT(imp, link))
00104 if (strcasecmp(name, imp->name) == 0)
00105 return (imp);
00106 return (NULL);
00107 }
00108
00109
00110
00111
00112
00113 isc_result_t
00114 dns_dlzallowzonexfr(dns_view_t *view, dns_name_t *name,
00115 isc_sockaddr_t *clientaddr, dns_db_t **dbp)
00116 {
00117 isc_result_t result = ISC_R_NOTFOUND;
00118 dns_dlzallowzonexfr_t allowzonexfr;
00119 dns_dlzdb_t *dlzdb;
00120
00121
00122
00123
00124 REQUIRE(name != NULL);
00125 REQUIRE(dbp != NULL && *dbp == NULL);
00126
00127
00128
00129
00130 for (dlzdb = ISC_LIST_HEAD(view->dlz_searched);
00131 dlzdb != NULL;
00132 dlzdb = ISC_LIST_NEXT(dlzdb, link))
00133 {
00134 REQUIRE(DNS_DLZ_VALID(dlzdb));
00135
00136 allowzonexfr = dlzdb->implementation->methods->allowzonexfr;
00137 result = (*allowzonexfr)(dlzdb->implementation->driverarg,
00138 dlzdb->dbdata, dlzdb->mctx,
00139 view->rdclass, name, clientaddr, dbp);
00140
00141
00142
00143
00144
00145 if (result == ISC_R_SUCCESS || result == ISC_R_NOPERM)
00146 return (result);
00147 }
00148
00149 if (result == ISC_R_NOTIMPLEMENTED)
00150 result = ISC_R_NOTFOUND;
00151
00152 return (result);
00153 }
00154
00155 isc_result_t
00156 dns_dlzcreate(isc_mem_t *mctx, const char *dlzname, const char *drivername,
00157 unsigned int argc, char *argv[], dns_dlzdb_t **dbp)
00158 {
00159 dns_dlzimplementation_t *impinfo;
00160 isc_result_t result;
00161 dns_dlzdb_t *db = NULL;
00162
00163
00164
00165
00166
00167 RUNTIME_CHECK(isc_once_do(&once, dlz_initialize) == ISC_R_SUCCESS);
00168
00169
00170
00171
00172 REQUIRE(dbp != NULL && *dbp == NULL);
00173 REQUIRE(dlzname != NULL);
00174 REQUIRE(drivername != NULL);
00175 REQUIRE(mctx != NULL);
00176
00177
00178 isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
00179 DNS_LOGMODULE_DLZ, ISC_LOG_INFO,
00180 "Loading '%s' using driver %s", dlzname, drivername);
00181
00182
00183 RWLOCK(&dlz_implock, isc_rwlocktype_read);
00184
00185
00186 impinfo = dlz_impfind(drivername);
00187 if (impinfo == NULL) {
00188 RWUNLOCK(&dlz_implock, isc_rwlocktype_read);
00189
00190 isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
00191 DNS_LOGMODULE_DLZ, ISC_LOG_ERROR,
00192 "unsupported DLZ database driver '%s'."
00193 " %s not loaded.",
00194 drivername, dlzname);
00195
00196 return (ISC_R_NOTFOUND);
00197 }
00198
00199
00200 db = isc_mem_get(mctx, sizeof(dns_dlzdb_t));
00201 if (db == NULL) {
00202 RWUNLOCK(&dlz_implock, isc_rwlocktype_read);
00203 return (ISC_R_NOMEMORY);
00204 }
00205
00206
00207 memset(db, 0, sizeof(dns_dlzdb_t));
00208
00209 ISC_LINK_INIT(db, link);
00210 db->implementation = impinfo;
00211 if (dlzname != NULL)
00212 db->dlzname = isc_mem_strdup(mctx, dlzname);
00213
00214
00215 result = ((impinfo->methods->create)(mctx, dlzname, argc, argv,
00216 impinfo->driverarg,
00217 &db->dbdata));
00218
00219
00220 if (result == ISC_R_SUCCESS) {
00221 RWUNLOCK(&dlz_implock, isc_rwlocktype_read);
00222 db->magic = DNS_DLZ_MAGIC;
00223 isc_mem_attach(mctx, &db->mctx);
00224 isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
00225 DNS_LOGMODULE_DLZ, ISC_LOG_DEBUG(2),
00226 "DLZ driver loaded successfully.");
00227 *dbp = db;
00228 return (ISC_R_SUCCESS);
00229 } else {
00230 isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
00231 DNS_LOGMODULE_DLZ, ISC_LOG_ERROR,
00232 "DLZ driver failed to load.");
00233 }
00234
00235
00236 RWUNLOCK(&dlz_implock, isc_rwlocktype_read);
00237 isc_mem_put(mctx, db, sizeof(dns_dlzdb_t));
00238 return (result);
00239 }
00240
00241 void
00242 dns_dlzdestroy(dns_dlzdb_t **dbp) {
00243 isc_mem_t *mctx;
00244 dns_dlzdestroy_t destroy;
00245
00246
00247 isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
00248 DNS_LOGMODULE_DLZ, ISC_LOG_DEBUG(2),
00249 "Unloading DLZ driver.");
00250
00251
00252
00253
00254 REQUIRE(dbp != NULL && DNS_DLZ_VALID(*dbp));
00255
00256 if ((*dbp)->ssutable != NULL) {
00257 dns_ssutable_detach(&(*dbp)->ssutable);
00258 }
00259
00260
00261 if ((*dbp) != NULL) {
00262 mctx = (*dbp)->mctx;
00263 if ((*dbp)->dlzname != NULL)
00264 isc_mem_free(mctx, (*dbp)->dlzname);
00265 destroy = (*dbp)->implementation->methods->destroy;
00266 (*destroy)((*dbp)->implementation->driverarg,(*dbp)->dbdata);
00267
00268 isc_mem_put(mctx, (*dbp), sizeof(dns_dlzdb_t));
00269 isc_mem_detach(&mctx);
00270 }
00271
00272 *dbp = NULL;
00273 }
00274
00275
00276
00277
00278
00279 isc_result_t
00280 dns_dlzregister(const char *drivername, const dns_dlzmethods_t *methods,
00281 void *driverarg, isc_mem_t *mctx,
00282 dns_dlzimplementation_t **dlzimp)
00283 {
00284
00285 dns_dlzimplementation_t *dlz_imp;
00286
00287
00288 isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
00289 DNS_LOGMODULE_DLZ, ISC_LOG_DEBUG(2),
00290 "Registering DLZ driver '%s'", drivername);
00291
00292
00293
00294
00295 REQUIRE(drivername != NULL);
00296 REQUIRE(methods != NULL);
00297 REQUIRE(methods->create != NULL);
00298 REQUIRE(methods->destroy != NULL);
00299 REQUIRE(methods->findzone != NULL);
00300 REQUIRE(mctx != NULL);
00301 REQUIRE(dlzimp != NULL && *dlzimp == NULL);
00302
00303
00304
00305
00306
00307 RUNTIME_CHECK(isc_once_do(&once, dlz_initialize) == ISC_R_SUCCESS);
00308
00309
00310 RWLOCK(&dlz_implock, isc_rwlocktype_write);
00311
00312
00313
00314
00315
00316 dlz_imp = dlz_impfind(drivername);
00317 if (dlz_imp != NULL) {
00318 isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
00319 DNS_LOGMODULE_DLZ, ISC_LOG_DEBUG(2),
00320 "DLZ Driver '%s' already registered",
00321 drivername);
00322 RWUNLOCK(&dlz_implock, isc_rwlocktype_write);
00323 return (ISC_R_EXISTS);
00324 }
00325
00326
00327
00328
00329
00330 dlz_imp = isc_mem_get(mctx, sizeof(dns_dlzimplementation_t));
00331 if (dlz_imp == NULL) {
00332 RWUNLOCK(&dlz_implock, isc_rwlocktype_write);
00333 return (ISC_R_NOMEMORY);
00334 }
00335
00336
00337 memset(dlz_imp, 0, sizeof(dns_dlzimplementation_t));
00338
00339
00340 dlz_imp->name = drivername;
00341 dlz_imp->methods = methods;
00342 dlz_imp->mctx = NULL;
00343 dlz_imp->driverarg = driverarg;
00344
00345
00346 isc_mem_attach(mctx, &dlz_imp->mctx);
00347
00348
00349
00350
00351
00352 ISC_LINK_INIT(dlz_imp, link);
00353 ISC_LIST_APPEND(dlz_implementations, dlz_imp, link);
00354
00355
00356 RWUNLOCK(&dlz_implock, isc_rwlocktype_write);
00357
00358
00359 *dlzimp = dlz_imp;
00360
00361 return (ISC_R_SUCCESS);
00362 }
00363
00364
00365
00366
00367
00368 static isc_result_t
00369 dns_dlzstrtoargvsub(isc_mem_t *mctx, char *s, unsigned int *argcp,
00370 char ***argvp, unsigned int n)
00371 {
00372 isc_result_t result;
00373
00374 restart:
00375
00376 while (*s == ' ' || *s == '\t')
00377 s++;
00378
00379 if (*s == '\0') {
00380
00381 *argcp = n;
00382 *argvp = isc_mem_get(mctx, n * sizeof(char *));
00383 if (*argvp == NULL)
00384 return (ISC_R_NOMEMORY);
00385 } else {
00386 char *p = s;
00387 while (*p != ' ' && *p != '\t' && *p != '\0' && *p != '{') {
00388 if (*p == '\n') {
00389 *p = ' ';
00390 goto restart;
00391 }
00392 p++;
00393 }
00394
00395
00396 if (*p == '{') {
00397 char *t = p;
00398
00399
00400
00401 while (*t != '\0') {
00402 t++;
00403 *(t-1) = *t;
00404 }
00405 while (*p != '\0' && *p != '}') {
00406 p++;
00407 }
00408
00409 if (*p == '}') {
00410 *p = '\0';
00411 p++;
00412 }
00413
00414 } else if (*p != '\0')
00415 *p++ = '\0';
00416
00417 result = dns_dlzstrtoargvsub(mctx, p, argcp, argvp, n + 1);
00418 if (result != ISC_R_SUCCESS)
00419 return (result);
00420 (*argvp)[n] = s;
00421 }
00422 return (ISC_R_SUCCESS);
00423 }
00424
00425
00426
00427
00428
00429
00430
00431
00432 isc_result_t
00433 dns_dlzstrtoargv(isc_mem_t *mctx, char *s,
00434 unsigned int *argcp, char ***argvp)
00435 {
00436 return(dns_dlzstrtoargvsub(mctx, s, argcp, argvp, 0));
00437 }
00438
00439
00440
00441
00442
00443 void
00444 dns_dlzunregister(dns_dlzimplementation_t **dlzimp) {
00445 dns_dlzimplementation_t *dlz_imp;
00446 isc_mem_t *mctx;
00447
00448
00449 isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
00450 DNS_LOGMODULE_DLZ, ISC_LOG_DEBUG(2),
00451 "Unregistering DLZ driver.");
00452
00453
00454
00455
00456 REQUIRE(dlzimp != NULL && *dlzimp != NULL);
00457
00458
00459
00460
00461
00462 RUNTIME_CHECK(isc_once_do(&once, dlz_initialize) == ISC_R_SUCCESS);
00463
00464 dlz_imp = *dlzimp;
00465
00466
00467 RWLOCK(&dlz_implock, isc_rwlocktype_write);
00468
00469
00470 ISC_LIST_UNLINK(dlz_implementations, dlz_imp, link);
00471 mctx = dlz_imp->mctx;
00472
00473
00474
00475
00476
00477 isc_mem_put(mctx, dlz_imp, sizeof(dns_dlzimplementation_t));
00478 isc_mem_detach(&mctx);
00479
00480
00481 RWUNLOCK(&dlz_implock, isc_rwlocktype_write);
00482 }
00483
00484
00485
00486
00487
00488
00489
00490
00491
00492
00493 isc_result_t
00494 dns_dlz_writeablezone(dns_view_t *view, dns_dlzdb_t *dlzdb,
00495 const char *zone_name)
00496 {
00497 dns_zone_t *zone = NULL;
00498 dns_zone_t *dupzone = NULL;
00499 isc_result_t result;
00500 isc_buffer_t buffer;
00501 dns_fixedname_t fixorigin;
00502 dns_name_t *origin;
00503
00504 REQUIRE(DNS_DLZ_VALID(dlzdb));
00505
00506 REQUIRE(dlzdb->configure_callback != NULL);
00507
00508 isc_buffer_constinit(&buffer, zone_name, strlen(zone_name));
00509 isc_buffer_add(&buffer, strlen(zone_name));
00510 dns_fixedname_init(&fixorigin);
00511 result = dns_name_fromtext(dns_fixedname_name(&fixorigin),
00512 &buffer, dns_rootname, 0, NULL);
00513 if (result != ISC_R_SUCCESS)
00514 goto cleanup;
00515 origin = dns_fixedname_name(&fixorigin);
00516
00517 if (!dlzdb->search) {
00518 isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
00519 DNS_LOGMODULE_DLZ, ISC_LOG_WARNING,
00520 "DLZ %s has 'search no;', but attempted to "
00521 "register writeable zone %s.",
00522 dlzdb->dlzname, zone_name);
00523 result = ISC_R_SUCCESS;
00524 goto cleanup;
00525 }
00526
00527
00528 result = dns_view_findzone(view, origin, &dupzone);
00529 if (result == ISC_R_SUCCESS) {
00530 dns_zone_detach(&dupzone);
00531 result = ISC_R_EXISTS;
00532 goto cleanup;
00533 }
00534 INSIST(dupzone == NULL);
00535
00536
00537 result = dns_zone_create(&zone, view->mctx);
00538 if (result != ISC_R_SUCCESS)
00539 goto cleanup;
00540 result = dns_zone_setorigin(zone, origin);
00541 if (result != ISC_R_SUCCESS)
00542 goto cleanup;
00543 dns_zone_setview(zone, view);
00544
00545 dns_zone_setadded(zone, ISC_TRUE);
00546
00547 if (dlzdb->ssutable == NULL) {
00548 result = dns_ssutable_createdlz(dlzdb->mctx,
00549 &dlzdb->ssutable, dlzdb);
00550 if (result != ISC_R_SUCCESS)
00551 goto cleanup;
00552 }
00553 dns_zone_setssutable(zone, dlzdb->ssutable);
00554
00555 result = dlzdb->configure_callback(view, dlzdb, zone);
00556 if (result != ISC_R_SUCCESS)
00557 goto cleanup;
00558
00559 result = dns_view_addzone(view, zone);
00560
00561
00562 cleanup:
00563 if (zone != NULL)
00564 dns_zone_detach(&zone);
00565
00566 return (result);
00567 }
00568
00569
00570
00571
00572
00573 isc_result_t
00574 dns_dlzconfigure(dns_view_t *view, dns_dlzdb_t *dlzdb,
00575 dlzconfigure_callback_t callback)
00576 {
00577 dns_dlzimplementation_t *impl;
00578 isc_result_t result;
00579
00580 REQUIRE(DNS_DLZ_VALID(dlzdb));
00581 REQUIRE(dlzdb->implementation != NULL);
00582
00583 impl = dlzdb->implementation;
00584
00585 if (impl->methods->configure == NULL)
00586 return (ISC_R_SUCCESS);
00587
00588 dlzdb->configure_callback = callback;
00589
00590 result = impl->methods->configure(impl->driverarg, dlzdb->dbdata,
00591 view, dlzdb);
00592 return (result);
00593 }
00594
00595 isc_boolean_t
00596 dns_dlz_ssumatch(dns_dlzdb_t *dlzdatabase, dns_name_t *signer,
00597 dns_name_t *name, isc_netaddr_t *tcpaddr,
00598 dns_rdatatype_t type, const dst_key_t *key)
00599 {
00600 dns_dlzimplementation_t *impl;
00601 isc_boolean_t r;
00602
00603 REQUIRE(dlzdatabase != NULL);
00604 REQUIRE(dlzdatabase->implementation != NULL);
00605 REQUIRE(dlzdatabase->implementation->methods != NULL);
00606 impl = dlzdatabase->implementation;
00607
00608 if (impl->methods->ssumatch == NULL) {
00609 isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
00610 DNS_LOGMODULE_DLZ, ISC_LOG_INFO,
00611 "No ssumatch method for DLZ database");
00612 return (ISC_FALSE);
00613 }
00614
00615 r = impl->methods->ssumatch(signer, name, tcpaddr, type, key,
00616 impl->driverarg, dlzdatabase->dbdata);
00617 return (r);
00618 }