📄 ldb_ildap.c
字号:
struct ldap_message *msg; int n; req->handle = NULL; if (!req->callback || !req->context) { ldb_set_errstring(module->ldb, "Async interface called with NULL callback function or NULL context"); return LDB_ERR_OPERATIONS_ERROR; } if (req->op.search.tree == NULL) { ldb_set_errstring(module->ldb, "Invalid expression parse tree"); return LDB_ERR_OPERATIONS_ERROR; } msg = new_ldap_message(req); if (msg == NULL) { ldb_set_errstring(module->ldb, "Out of Memory"); return LDB_ERR_OPERATIONS_ERROR; } msg->type = LDAP_TAG_SearchRequest; if (req->op.search.base == NULL) { msg->r.SearchRequest.basedn = talloc_strdup(msg, ""); } else { msg->r.SearchRequest.basedn = ldb_dn_alloc_linearized(msg, req->op.search.base); } if (msg->r.SearchRequest.basedn == NULL) { ldb_set_errstring(module->ldb, "Unable to determine baseDN"); talloc_free(msg); return LDB_ERR_OPERATIONS_ERROR; } if (req->op.search.scope == LDB_SCOPE_DEFAULT) { msg->r.SearchRequest.scope = LDB_SCOPE_SUBTREE; } else { msg->r.SearchRequest.scope = req->op.search.scope; } msg->r.SearchRequest.deref = LDAP_DEREFERENCE_NEVER; msg->r.SearchRequest.timelimit = 0; msg->r.SearchRequest.sizelimit = 0; msg->r.SearchRequest.attributesonly = 0; msg->r.SearchRequest.tree = discard_const(req->op.search.tree); for (n = 0; req->op.search.attrs && req->op.search.attrs[n]; n++) /* noop */ ; msg->r.SearchRequest.num_attributes = n; msg->r.SearchRequest.attributes = discard_const(req->op.search.attrs); msg->controls = req->controls; return ildb_request_send(ildb, msg, req);}/* add a record*/static int ildb_add(struct ldb_module *module, struct ldb_request *req){ struct ildb_private *ildb = talloc_get_type(module->private_data, struct ildb_private); struct ldap_message *msg; struct ldap_mod **mods; int i,n; req->handle = NULL; /* ignore ltdb specials */ if (ldb_dn_is_special(req->op.add.message->dn)) { return ildb_request_noop(ildb, req); } msg = new_ldap_message(req); if (msg == NULL) { return LDB_ERR_OPERATIONS_ERROR; } msg->type = LDAP_TAG_AddRequest; msg->r.AddRequest.dn = ldb_dn_alloc_linearized(msg, req->op.add.message->dn); if (msg->r.AddRequest.dn == NULL) { talloc_free(msg); return LDB_ERR_INVALID_DN_SYNTAX; } mods = ildb_msg_to_mods(msg, &n, req->op.add.message, 0); if (mods == NULL) { talloc_free(msg); return LDB_ERR_OPERATIONS_ERROR; } msg->r.AddRequest.num_attributes = n; msg->r.AddRequest.attributes = talloc_array(msg, struct ldb_message_element, n); if (msg->r.AddRequest.attributes == NULL) { talloc_free(msg); return LDB_ERR_OPERATIONS_ERROR; } for (i = 0; i < n; i++) { msg->r.AddRequest.attributes[i] = mods[i]->attrib; } return ildb_request_send(ildb, msg, req);}/* modify a record*/static int ildb_modify(struct ldb_module *module, struct ldb_request *req){ struct ildb_private *ildb = talloc_get_type(module->private_data, struct ildb_private); struct ldap_message *msg; struct ldap_mod **mods; int i,n; req->handle = NULL; /* ignore ltdb specials */ if (ldb_dn_is_special(req->op.mod.message->dn)) { return ildb_request_noop(ildb, req); } msg = new_ldap_message(req); if (msg == NULL) { return LDB_ERR_OPERATIONS_ERROR; } msg->type = LDAP_TAG_ModifyRequest; msg->r.ModifyRequest.dn = ldb_dn_alloc_linearized(msg, req->op.mod.message->dn); if (msg->r.ModifyRequest.dn == NULL) { talloc_free(msg); return LDB_ERR_INVALID_DN_SYNTAX; } mods = ildb_msg_to_mods(msg, &n, req->op.mod.message, 1); if (mods == NULL) { talloc_free(msg); return LDB_ERR_OPERATIONS_ERROR; } msg->r.ModifyRequest.num_mods = n; msg->r.ModifyRequest.mods = talloc_array(msg, struct ldap_mod, n); if (msg->r.ModifyRequest.mods == NULL) { talloc_free(msg); return LDB_ERR_OPERATIONS_ERROR; } for (i = 0; i < n; i++) { msg->r.ModifyRequest.mods[i] = *mods[i]; } return ildb_request_send(ildb, msg, req);}/* delete a record*/static int ildb_delete(struct ldb_module *module, struct ldb_request *req){ struct ildb_private *ildb = talloc_get_type(module->private_data, struct ildb_private); struct ldap_message *msg; req->handle = NULL; /* ignore ltdb specials */ if (ldb_dn_is_special(req->op.del.dn)) { return ildb_request_noop(ildb, req); } msg = new_ldap_message(req); if (msg == NULL) { return LDB_ERR_OPERATIONS_ERROR; } msg->type = LDAP_TAG_DelRequest; msg->r.DelRequest.dn = ldb_dn_alloc_linearized(msg, req->op.del.dn); if (msg->r.DelRequest.dn == NULL) { talloc_free(msg); return LDB_ERR_INVALID_DN_SYNTAX; } return ildb_request_send(ildb, msg, req);}/* rename a record*/static int ildb_rename(struct ldb_module *module, struct ldb_request *req){ struct ildb_private *ildb = talloc_get_type(module->private_data, struct ildb_private); struct ldap_message *msg; req->handle = NULL; /* ignore ltdb specials */ if (ldb_dn_is_special(req->op.rename.olddn) || ldb_dn_is_special(req->op.rename.newdn)) { return ildb_request_noop(ildb, req); } msg = new_ldap_message(req); if (msg == NULL) { return LDB_ERR_OPERATIONS_ERROR; } msg->type = LDAP_TAG_ModifyDNRequest; msg->r.ModifyDNRequest.dn = ldb_dn_alloc_linearized(msg, req->op.rename.olddn); if (msg->r.ModifyDNRequest.dn == NULL) { talloc_free(msg); return LDB_ERR_INVALID_DN_SYNTAX; } msg->r.ModifyDNRequest.newrdn = talloc_asprintf(msg, "%s=%s", ldb_dn_get_rdn_name(req->op.rename.newdn), ldb_dn_escape_value(msg, *ldb_dn_get_rdn_val(req->op.rename.newdn))); if (msg->r.ModifyDNRequest.newrdn == NULL) { talloc_free(msg); return LDB_ERR_OPERATIONS_ERROR; } msg->r.ModifyDNRequest.newsuperior = ldb_dn_alloc_linearized(msg, ldb_dn_get_parent(msg, req->op.rename.newdn)); if (msg->r.ModifyDNRequest.newsuperior == NULL) { talloc_free(msg); return LDB_ERR_INVALID_DN_SYNTAX; } msg->r.ModifyDNRequest.deleteolddn = true; return ildb_request_send(ildb, msg, req);}static int ildb_start_trans(struct ldb_module *module){ /* TODO implement a local locking mechanism here */ return LDB_SUCCESS;}static int ildb_end_trans(struct ldb_module *module){ /* TODO implement a local transaction mechanism here */ return LDB_SUCCESS;}static int ildb_del_trans(struct ldb_module *module){ /* TODO implement a local locking mechanism here */ return LDB_SUCCESS;}static int ildb_request(struct ldb_module *module, struct ldb_request *req){ return LDB_ERR_OPERATIONS_ERROR;}static int ildb_wait(struct ldb_handle *handle, enum ldb_wait_type type){ struct ildb_context *ac = talloc_get_type(handle->private_data, struct ildb_context); if (handle->state == LDB_ASYNC_DONE) { return handle->status; } if (!ac) { return LDB_ERR_OPERATIONS_ERROR; } handle->state = LDB_ASYNC_INIT; switch(type) { case LDB_WAIT_NONE: if (event_loop_once(ac->req->conn->event.event_ctx) != 0) { return LDB_ERR_OTHER; } break; case LDB_WAIT_ALL: while (handle->status == LDB_SUCCESS && handle->state != LDB_ASYNC_DONE) { if (event_loop_once(ac->req->conn->event.event_ctx) != 0) { return LDB_ERR_OTHER; } } break; default: return LDB_ERR_OPERATIONS_ERROR; } return handle->status;}static const struct ldb_module_ops ildb_ops = { .name = "ldap", .search = ildb_search, .add = ildb_add, .modify = ildb_modify, .del = ildb_delete, .rename = ildb_rename, .request = ildb_request, .start_transaction = ildb_start_trans, .end_transaction = ildb_end_trans, .del_transaction = ildb_del_trans, .wait = ildb_wait};/* connect to the database*/static int ildb_connect(struct ldb_context *ldb, const char *url, unsigned int flags, const char *options[], struct ldb_module **_module){ struct ldb_module *module; struct ildb_private *ildb; NTSTATUS status; struct cli_credentials *creds; struct event_context *event_ctx; module = talloc(ldb, struct ldb_module); if (!module) { ldb_oom(ldb); return -1; } talloc_set_name_const(module, "ldb_ildap backend"); module->ldb = ldb; module->prev = module->next = NULL; module->private_data = NULL; module->ops = &ildb_ops; ildb = talloc(module, struct ildb_private); if (!ildb) { ldb_oom(ldb); goto failed; } module->private_data = ildb; ildb->module = module; event_ctx = ldb_get_opaque(ldb, "EventContext"); /* FIXME: We must make the event context an explicit parameter, but we * need to build the events library separately first. Hack a new event * context so that CMD line utilities work until we have libevents for * standalone builds ready */ if (event_ctx == NULL) { event_ctx = event_context_init(NULL); } ildb->ldap = ldap4_new_connection(ildb, ldb_get_opaque(ldb, "loadparm"), event_ctx); if (!ildb->ldap) { ldb_oom(ldb); goto failed; } if (flags & LDB_FLG_RECONNECT) { ldap_set_reconn_params(ildb->ldap, 10); } status = ldap_connect(ildb->ldap, url); if (!NT_STATUS_IS_OK(status)) { ldb_debug(ldb, LDB_DEBUG_ERROR, "Failed to connect to ldap URL '%s' - %s\n", url, ldap_errstr(ildb->ldap, module, status)); goto failed; } /* caller can optionally setup credentials using the opaque token 'credentials' */ creds = talloc_get_type(ldb_get_opaque(ldb, "credentials"), struct cli_credentials); if (creds == NULL) { struct auth_session_info *session_info = talloc_get_type(ldb_get_opaque(ldb, "sessionInfo"), struct auth_session_info); if (session_info) { creds = session_info->credentials; } } if (creds != NULL && cli_credentials_authentication_requested(creds)) { const char *bind_dn = cli_credentials_get_bind_dn(creds); if (bind_dn) { const char *password = cli_credentials_get_password(creds); status = ldap_bind_simple(ildb->ldap, bind_dn, password); if (!NT_STATUS_IS_OK(status)) { ldb_debug(ldb, LDB_DEBUG_ERROR, "Failed to bind - %s\n", ldap_errstr(ildb->ldap, module, status)); goto failed; } } else { status = ldap_bind_sasl(ildb->ldap, creds, ldb_get_opaque(ldb, "loadparm")); if (!NT_STATUS_IS_OK(status)) { ldb_debug(ldb, LDB_DEBUG_ERROR, "Failed to bind - %s\n", ldap_errstr(ildb->ldap, module, status)); goto failed; } } } *_module = module; return 0;failed: talloc_free(module); return -1;}_PUBLIC_ const struct ldb_backend_ops ldb_ldap_backend_ops = { .name = "ldap", .connect_fn = ildb_connect};_PUBLIC_ const struct ldb_backend_ops ldb_ldapi_backend_ops = { .name = "ldapi", .connect_fn = ildb_connect};_PUBLIC_ const struct ldb_backend_ops ldb_ldaps_backend_ops = { .name = "ldaps", .connect_fn = ildb_connect};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -