📄 ldap_server.c
字号:
/* Unix SMB/CIFS implementation. LDAP server Copyright (C) Andrew Tridgell 2005 Copyright (C) Volker Lendecke 2004 Copyright (C) Stefan Metzmacher 2004 This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.*/#include "includes.h"#include "lib/events/events.h"#include "auth/auth.h"#include "auth/credentials/credentials.h"#include "librpc/gen_ndr/ndr_samr.h"#include "lib/util/dlinklist.h"#include "lib/util/asn1.h"#include "ldap_server/ldap_server.h"#include "smbd/service_task.h"#include "smbd/service_stream.h"#include "smbd/service.h"#include "smbd/process_model.h"#include "lib/tls/tls.h"#include "lib/messaging/irpc.h"#include "lib/ldb/include/ldb.h"#include "lib/ldb/include/ldb_errors.h"#include "libcli/ldap/ldap.h"#include "libcli/ldap/ldap_proto.h"#include "system/network.h"#include "lib/socket/netif.h"#include "dsdb/samdb/samdb.h"#include "param/param.h"/* close the socket and shutdown a server_context*/void ldapsrv_terminate_connection(struct ldapsrv_connection *conn, const char *reason){ stream_terminate_connection(conn->connection, reason);}/* handle packet errors*/static void ldapsrv_error_handler(void *private, NTSTATUS status){ struct ldapsrv_connection *conn = talloc_get_type(private, struct ldapsrv_connection); ldapsrv_terminate_connection(conn, nt_errstr(status));}/* process a decoded ldap message*/static void ldapsrv_process_message(struct ldapsrv_connection *conn, struct ldap_message *msg){ struct ldapsrv_call *call; NTSTATUS status; DATA_BLOB blob; call = talloc(conn, struct ldapsrv_call); if (!call) { ldapsrv_terminate_connection(conn, "no memory"); return; } call->request = talloc_steal(call, msg); call->conn = conn; call->replies = NULL; call->send_callback = NULL; call->send_private = NULL; /* make the call */ status = ldapsrv_do_call(call); if (!NT_STATUS_IS_OK(status)) { talloc_free(call); return; } blob = data_blob(NULL, 0); if (call->replies == NULL) { talloc_free(call); return; } /* build all the replies into a single blob */ while (call->replies) { DATA_BLOB b; bool ret; msg = call->replies->msg; if (!ldap_encode(msg, &b, call)) { DEBUG(0,("Failed to encode ldap reply of type %d\n", msg->type)); talloc_free(call); return; } ret = data_blob_append(call, &blob, b.data, b.length); data_blob_free(&b); talloc_set_name_const(blob.data, "Outgoing, encoded LDAP packet"); if (!ret) { talloc_free(call); return; } DLIST_REMOVE(call->replies, call->replies); } packet_send_callback(conn->packet, blob, call->send_callback, call->send_private); talloc_free(call); return;}/* decode/process data*/static NTSTATUS ldapsrv_decode(void *private, DATA_BLOB blob){ NTSTATUS status; struct ldapsrv_connection *conn = talloc_get_type(private, struct ldapsrv_connection); struct asn1_data *asn1 = asn1_init(conn); struct ldap_message *msg = talloc(conn, struct ldap_message); if (asn1 == NULL || msg == NULL) { return NT_STATUS_NO_MEMORY; } if (!asn1_load(asn1, blob)) { talloc_free(msg); talloc_free(asn1); return NT_STATUS_NO_MEMORY; } status = ldap_decode(asn1, msg); if (!NT_STATUS_IS_OK(status)) { asn1_free(asn1); return status; } data_blob_free(&blob); talloc_steal(conn, msg); asn1_free(asn1); ldapsrv_process_message(conn, msg); return NT_STATUS_OK;}/* Idle timeout handler*/static void ldapsrv_conn_idle_timeout(struct event_context *ev, struct timed_event *te, struct timeval t, void *private){ struct ldapsrv_connection *conn = talloc_get_type(private, struct ldapsrv_connection); ldapsrv_terminate_connection(conn, "Timeout. No requests after bind");}/* called when a LDAP socket becomes readable*/void ldapsrv_recv(struct stream_connection *c, uint16_t flags){ struct ldapsrv_connection *conn = talloc_get_type(c->private, struct ldapsrv_connection); if (conn->limits.ite) { /* clean initial timeout if any */ talloc_free(conn->limits.ite); conn->limits.ite = NULL; } if (conn->limits.te) { /* clean idle timeout if any */ talloc_free(conn->limits.te); conn->limits.te = NULL; } packet_recv(conn->packet); /* set idle timeout */ conn->limits.te = event_add_timed(c->event.ctx, conn, timeval_current_ofs(conn->limits.conn_idle_time, 0), ldapsrv_conn_idle_timeout, conn);}/* called when a LDAP socket becomes writable*/static void ldapsrv_send(struct stream_connection *c, uint16_t flags){ struct ldapsrv_connection *conn = talloc_get_type(c->private, struct ldapsrv_connection); packet_queue_run(conn->packet);}static void ldapsrv_conn_init_timeout(struct event_context *ev, struct timed_event *te, struct timeval t, void *private){ struct ldapsrv_connection *conn = talloc_get_type(private, struct ldapsrv_connection); ldapsrv_terminate_connection(conn, "Timeout. No requests after initial connection");}static int ldapsrv_load_limits(struct ldapsrv_connection *conn){ TALLOC_CTX *tmp_ctx; const char *attrs[] = { "configurationNamingContext", NULL }; const char *attrs2[] = { "lDAPAdminLimits", NULL }; struct ldb_message_element *el; struct ldb_result *res = NULL; struct ldb_dn *basedn; struct ldb_dn *conf_dn; struct ldb_dn *policy_dn; int i,ret; /* set defaults limits in case of failure */ conn->limits.initial_timeout = 120; conn->limits.conn_idle_time = 900; conn->limits.max_page_size = 1000; conn->limits.search_timeout = 120; tmp_ctx = talloc_new(conn); if (tmp_ctx == NULL) { return -1; } basedn = ldb_dn_new(tmp_ctx, conn->ldb, NULL); if ( ! ldb_dn_validate(basedn)) { goto failed; } ret = ldb_search(conn->ldb, basedn, LDB_SCOPE_BASE, NULL, attrs, &res); if (ret != LDB_SUCCESS) { goto failed; } talloc_steal(tmp_ctx, res); if (res->count != 1) { goto failed; } conf_dn = ldb_msg_find_attr_as_dn(conn->ldb, tmp_ctx, res->msgs[0], "configurationNamingContext"); if (conf_dn == NULL) { goto failed; } policy_dn = ldb_dn_copy(tmp_ctx, conf_dn); ldb_dn_add_child_fmt(policy_dn, "CN=Default Query Policy,CN=Query-Policies,CN=Directory Service,CN=Windows NT,CN=Services"); if (policy_dn == NULL) { goto failed; } ret = ldb_search(conn->ldb, policy_dn, LDB_SCOPE_BASE, NULL, attrs2, &res); if (ret != LDB_SUCCESS) { goto failed; } talloc_steal(tmp_ctx, res); if (res->count != 1) { goto failed; } el = ldb_msg_find_element(res->msgs[0], "lDAPAdminLimits"); if (el == NULL) { goto failed; } for (i = 0; i < el->num_values; i++) { char policy_name[256];
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -