dlz_ldap_driver.c
来自「非常好的dns解析软件」· C语言 代码 · 共 1,340 行 · 第 1/3 页
C
1,340 行
}static isc_result_tdlz_ldap_authority(const char *zone, void *driverarg, void *dbdata, dns_sdlzlookup_t *lookup){ UNUSED(driverarg); return ldap_get_results(zone, NULL, NULL, AUTHORITY, dbdata, lookup);}static isc_result_tdlz_ldap_findzone(void *driverarg, void *dbdata, const char *name){ UNUSED(driverarg); return ldap_get_results(name, NULL, NULL, FINDZONE, dbdata, NULL);}static isc_result_tdlz_ldap_lookup(const char *zone, const char *name, void *driverarg, void *dbdata, dns_sdlzlookup_t *lookup){ UNUSED(driverarg); if (strcmp(name, "*") == 0) return ldap_get_results(zone, "~", NULL, LOOKUP, dbdata, lookup); else return ldap_get_results(zone, name, NULL, LOOKUP, dbdata, lookup);}static isc_result_tdlz_ldap_create(const char *dlzname, unsigned int argc, char *argv[], void *driverarg, void **dbdata){ isc_result_t result; ldap_instance_t *ldap_inst = NULL; dbinstance_t *dbi = NULL; int protocol; int method;#ifdef ISC_PLATFORM_USETHREADS /* if multi-threaded, we need a few extra variables. */ int dbcount; char *endp;/* db_list_t *dblist = NULL; */ int i;#endif /* ISC_PLATFORM_USETHREADS */ UNUSED(dlzname); UNUSED(driverarg);#ifdef ISC_PLATFORM_USETHREADS /* if debugging, let user know we are multithreaded. */ isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE, DNS_LOGMODULE_DLZ, ISC_LOG_DEBUG(1), "LDAP driver running multithreaded");#else /* ISC_PLATFORM_USETHREADS */ /* if debugging, let user know we are single threaded. */ isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE, DNS_LOGMODULE_DLZ, ISC_LOG_DEBUG(1), "LDAP driver running single threaded");#endif /* ISC_PLATFORM_USETHREADS */ if (argc < 9) { isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE, DNS_LOGMODULE_DLZ, ISC_LOG_ERROR, "LDAP driver requires at least " "8 command line args."); return (ISC_R_FAILURE); } /* no more than 13 arg's should be passed to the driver */ if (argc > 12) { isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE, DNS_LOGMODULE_DLZ, ISC_LOG_ERROR, "LDAP driver cannot accept more than " "11 command line args."); return (ISC_R_FAILURE); } /* determine protocol version. */ if (strncasecmp(argv[2], V2, strlen(V2)) == 0) { protocol = 2; } else if (strncasecmp(argv[2], V3, strlen(V3)) == 0) { protocol = 3; } else { isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE, DNS_LOGMODULE_DLZ, ISC_LOG_ERROR, "LDAP driver protocol must be either %s or %s", V2, V3); return (ISC_R_FAILURE); } /* determine connection method. */ if (strncasecmp(argv[3], SIMPLE, strlen(SIMPLE)) == 0) { method = LDAP_AUTH_SIMPLE; } else if (strncasecmp(argv[3], KRB41, strlen(KRB41)) == 0) { method = LDAP_AUTH_KRBV41; } else if (strncasecmp(argv[3], KRB42, strlen(KRB42)) == 0) { method = LDAP_AUTH_KRBV42; } else { isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE, DNS_LOGMODULE_DLZ, ISC_LOG_ERROR, "LDAP driver authentication method must be " "one of %s, %s or %s", SIMPLE, KRB41, KRB42); return (ISC_R_FAILURE); } /* multithreaded build can have multiple DB connections */#ifdef ISC_PLATFORM_USETHREADS /* check how many db connections we should create */ dbcount = strtol(argv[1], &endp, 10); if (*endp != '\0' || dbcount < 0) { isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE, DNS_LOGMODULE_DLZ, ISC_LOG_ERROR, "LDAP driver database connection count " "must be positive."); return (ISC_R_FAILURE); }#endif /* check that LDAP URL parameters make sense */ switch(argc) { case 12: result = dlz_ldap_checkURL(argv[11], 0, "allow zone transfer"); if (result != ISC_R_SUCCESS) return result; case 11: result = dlz_ldap_checkURL(argv[10], 3, "all nodes"); if (result != ISC_R_SUCCESS) return result; case 10: if (strlen(argv[9]) > 0) { result = dlz_ldap_checkURL(argv[9], 3, "authority"); if (result != ISC_R_SUCCESS) return result; } case 9: result = dlz_ldap_checkURL(argv[8], 3, "lookup"); if (result != ISC_R_SUCCESS) return result; result = dlz_ldap_checkURL(argv[7], 0, "find zone"); if (result != ISC_R_SUCCESS) return result; break; default: /* not really needed, should shut up compiler. */ result = ISC_R_FAILURE; } /* allocate memory for LDAP instance */ ldap_inst = isc_mem_get(ns_g_mctx, sizeof(ldap_instance_t)); if (ldap_inst == NULL) return (ISC_R_NOMEMORY); memset(ldap_inst, 0, sizeof(ldap_instance_t)); /* store info needed to automatically re-connect. */ ldap_inst->protocol = protocol; ldap_inst->method = method; ldap_inst->hosts = isc_mem_strdup(ns_g_mctx, argv[6]); if (ldap_inst->hosts == NULL) { result = ISC_R_NOMEMORY; goto cleanup; } ldap_inst->user = isc_mem_strdup(ns_g_mctx, argv[4]); if (ldap_inst->user == NULL) { result = ISC_R_NOMEMORY; goto cleanup; } ldap_inst->cred = isc_mem_strdup(ns_g_mctx, argv[5]); if (ldap_inst->cred == NULL) { result = ISC_R_NOMEMORY; goto cleanup; }#ifdef ISC_PLATFORM_USETHREADS /* allocate memory for database connection list */ ldap_inst->db = isc_mem_get(ns_g_mctx, sizeof(db_list_t)); if (ldap_inst->db == NULL) { result = ISC_R_NOMEMORY; goto cleanup; } /* initialize DB connection list */ ISC_LIST_INIT(*(ldap_inst->db)); /* * create the appropriate number of database instances (DBI) * append each new DBI to the end of the list */ for (i = 0; i < dbcount; i++) {#endif /* ISC_PLATFORM_USETHREADS */ /* how many queries were passed in from config file? */ switch(argc) { case 9: result = build_sqldbinstance(ns_g_mctx, NULL, NULL, NULL, argv[7], argv[8], NULL, &dbi); break; case 10: result = build_sqldbinstance(ns_g_mctx, NULL, NULL, argv[9], argv[7], argv[8], NULL, &dbi); break; case 11: result = build_sqldbinstance(ns_g_mctx, argv[10], NULL, argv[9], argv[7], argv[8], NULL, &dbi); break; case 12: result = build_sqldbinstance(ns_g_mctx, argv[10], argv[11], argv[9], argv[7], argv[8], NULL, &dbi); break; default: /* not really needed, should shut up compiler. */ result = ISC_R_FAILURE; } if (result == ISC_R_SUCCESS) { isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE, DNS_LOGMODULE_DLZ, ISC_LOG_DEBUG(2), "LDAP driver created " "database instance object."); } else { /* unsuccessful?, log err msg and cleanup. */ isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE, DNS_LOGMODULE_DLZ, ISC_LOG_ERROR, "LDAP driver could not create " "database instance object."); goto cleanup; }#ifdef ISC_PLATFORM_USETHREADS /* when multithreaded, build a list of DBI's */ ISC_LINK_INIT(dbi, link); ISC_LIST_APPEND(*(ldap_inst->db), dbi, link);#else /* * when single threaded, hold onto the one connection * instance. */ ldap_inst->db = dbi;#endif /* attempt to connect */ result = dlz_ldap_connect(ldap_inst, dbi); /* * if db connection cannot be created, log err msg and * cleanup. */ switch(result) { /* success, do nothing */ case ISC_R_SUCCESS: break; /* * no memory means ldap_init could not * allocate memory */ case ISC_R_NOMEMORY:#ifdef ISC_PLATFORM_USETHREADS isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE, DNS_LOGMODULE_DLZ, ISC_LOG_ERROR, "LDAP driver could not allocate memory " "for connection number %u", i+1);#else isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE, DNS_LOGMODULE_DLZ, ISC_LOG_ERROR, "LDAP driver could not allocate memory " "for connection");#endif goto cleanup; break; /* * no perm means ldap_set_option could not set * protocol version */ case ISC_R_NOPERM: isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE, DNS_LOGMODULE_DLZ, ISC_LOG_ERROR, "LDAP driver could not " "set protocol version."); result = ISC_R_FAILURE; goto cleanup; break; /* failure means couldn't connect to ldap server */ case ISC_R_FAILURE:#ifdef ISC_PLATFORM_USETHREADS isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE, DNS_LOGMODULE_DLZ, ISC_LOG_ERROR, "LDAP driver could not " "bind connection number %u to server.", i+1);#else isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE, DNS_LOGMODULE_DLZ, ISC_LOG_ERROR, "LDAP driver could not " "bind connection to server.");#endif goto cleanup; break; /* * default should never happen. If it does, * major errors. */ default: UNEXPECTED_ERROR(__FILE__, __LINE__, "dlz_ldap_create() failed: %s", isc_result_totext(result)); result = ISC_R_UNEXPECTED; goto cleanup; break; } /* end switch(result) */#ifdef ISC_PLATFORM_USETHREADS /* set DBI = null for next loop through. */ dbi = NULL; } /* end for loop */#endif /* ISC_PLATFORM_USETHREADS */ /* set dbdata to the ldap_instance we created. */ *dbdata = ldap_inst; /* hey, we got through all of that ok, return success. */ return(ISC_R_SUCCESS); cleanup: dlz_ldap_destroy(NULL, ldap_inst); return(ISC_R_FAILURE);}voiddlz_ldap_destroy(void *driverarg, void *dbdata){ UNUSED(driverarg); if (dbdata != NULL) {#ifdef ISC_PLATFORM_USETHREADS /* cleanup the list of DBI's */ ldap_destroy_dblist((db_list_t *) ((ldap_instance_t *)dbdata)->db);#else /* ISC_PLATFORM_USETHREADS */ /* release connection */ if (((ldap_instance_t *)dbdata)->db->dbconn != NULL) ldap_unbind_s((LDAP *) ((ldap_instance_t *)dbdata)->db->dbconn); /* destroy single DB instance */ destroy_sqldbinstance(((ldap_instance_t *)dbdata)->db);#endif /* ISC_PLATFORM_USETHREADS */ if (((ldap_instance_t *)dbdata)->hosts != NULL) isc_mem_free(ns_g_mctx, ((ldap_instance_t *)dbdata)->hosts); if (((ldap_instance_t *)dbdata)->user != NULL) isc_mem_free(ns_g_mctx, ((ldap_instance_t *)dbdata)->user); if (((ldap_instance_t *)dbdata)->cred != NULL) isc_mem_free(ns_g_mctx, ((ldap_instance_t *)dbdata)->cred); isc_mem_put(ns_g_mctx, dbdata, sizeof(ldap_instance_t)); }}static dns_sdlzmethods_t dlz_ldap_methods = { dlz_ldap_create, dlz_ldap_destroy, dlz_ldap_findzone, dlz_ldap_lookup, dlz_ldap_authority, dlz_ldap_allnodes, dlz_ldap_allowzonexfr};/*% * Wrapper around dns_sdlzregister(). */isc_result_tdlz_ldap_init(void) { isc_result_t result; /* * Write debugging message to log */ isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE, DNS_LOGMODULE_DLZ, ISC_LOG_DEBUG(2), "Registering DLZ ldap driver."); result = dns_sdlzregister("ldap", &dlz_ldap_methods, NULL, DNS_SDLZFLAG_RELATIVEOWNER | DNS_SDLZFLAG_RELATIVERDATA, ns_g_mctx, &dlz_ldap); if (result != ISC_R_SUCCESS) { UNEXPECTED_ERROR(__FILE__, __LINE__, "dns_sdlzregister() failed: %s", isc_result_totext(result)); result = ISC_R_UNEXPECTED; } return result;}/*% * Wrapper around dns_sdlzunregister(). */voiddlz_ldap_clear(void) { /* * Write debugging message to log */ isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE, DNS_LOGMODULE_DLZ, ISC_LOG_DEBUG(2), "Unregistering DLZ ldap driver."); if (dlz_ldap != NULL) dns_sdlzunregister(&dlz_ldap);}#endif
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?