summaryrefslogtreecommitdiffstats
path: root/source4/cldap_server
diff options
context:
space:
mode:
Diffstat (limited to 'source4/cldap_server')
-rw-r--r--source4/cldap_server/cldap_server.c260
-rw-r--r--source4/cldap_server/cldap_server.h35
-rw-r--r--source4/cldap_server/rootdse.c183
-rw-r--r--source4/cldap_server/wscript_build17
4 files changed, 495 insertions, 0 deletions
diff --git a/source4/cldap_server/cldap_server.c b/source4/cldap_server/cldap_server.c
new file mode 100644
index 0000000..3a8b3be
--- /dev/null
+++ b/source4/cldap_server/cldap_server.c
@@ -0,0 +1,260 @@
+/*
+ Unix SMB/CIFS implementation.
+
+ CLDAP server task
+
+ Copyright (C) Andrew Tridgell 2005
+
+ 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 <talloc.h>
+#include "lib/messaging/irpc.h"
+#include "samba/service_task.h"
+#include "samba/service.h"
+#include "cldap_server/cldap_server.h"
+#include "system/network.h"
+#include "lib/socket/netif.h"
+#include <ldb.h>
+#include <ldb_errors.h>
+#include "dsdb/samdb/samdb.h"
+#include "ldb_wrap.h"
+#include "auth/auth.h"
+#include "param/param.h"
+#include "../lib/tsocket/tsocket.h"
+#include "libds/common/roles.h"
+
+NTSTATUS server_service_cldapd_init(TALLOC_CTX *);
+
+/*
+ handle incoming cldap requests
+*/
+static void cldapd_request_handler(struct cldap_socket *cldap,
+ void *private_data,
+ struct cldap_incoming *in)
+{
+ struct cldapd_server *cldapd = talloc_get_type(private_data,
+ struct cldapd_server);
+ struct ldap_SearchRequest *search;
+
+ if (in->ldap_msg->type == LDAP_TAG_AbandonRequest) {
+ DEBUG(10,("Got (and ignoring) CLDAP AbandonRequest from %s.",
+ tsocket_address_string(in->src, in)));
+ talloc_free(in);
+ return;
+ }
+
+ if (in->ldap_msg->type != LDAP_TAG_SearchRequest) {
+ DEBUG(0,("Invalid CLDAP request type %d from %s\n",
+ in->ldap_msg->type,
+ tsocket_address_string(in->src, in)));
+ cldap_error_reply(cldap, in->ldap_msg->messageid, in->src,
+ LDAP_OPERATIONS_ERROR, "Invalid CLDAP request");
+ talloc_free(in);
+ return;
+ }
+
+ search = &in->ldap_msg->r.SearchRequest;
+
+ if (strcmp("", search->basedn) != 0) {
+ DEBUG(0,("Invalid CLDAP basedn '%s' from %s\n",
+ search->basedn,
+ tsocket_address_string(in->src, in)));
+ cldap_error_reply(cldap, in->ldap_msg->messageid, in->src,
+ LDAP_OPERATIONS_ERROR, "Invalid CLDAP basedn");
+ talloc_free(in);
+ return;
+ }
+
+ if (search->scope != LDAP_SEARCH_SCOPE_BASE) {
+ DEBUG(0,("Invalid CLDAP scope %d from %s\n",
+ search->scope,
+ tsocket_address_string(in->src, in)));
+ cldap_error_reply(cldap, in->ldap_msg->messageid, in->src,
+ LDAP_OPERATIONS_ERROR, "Invalid CLDAP scope");
+ talloc_free(in);
+ return;
+ }
+
+ cldapd_rootdse_request(cldap, cldapd, in,
+ in->ldap_msg->messageid,
+ search, in->src);
+ talloc_free(in);
+}
+
+
+/*
+ start listening on the given address
+*/
+static NTSTATUS cldapd_add_socket(struct cldapd_server *cldapd, struct loadparm_context *lp_ctx,
+ const char *address)
+{
+ struct cldap_socket *cldapsock;
+ struct tsocket_address *socket_address;
+ NTSTATUS status;
+ int ret;
+
+ ret = tsocket_address_inet_from_strings(cldapd,
+ "ip",
+ address,
+ lpcfg_cldap_port(lp_ctx),
+ &socket_address);
+ if (ret != 0) {
+ status = map_nt_error_from_unix_common(errno);
+ DEBUG(0,("invalid address %s:%d - %s:%s\n",
+ address, lpcfg_cldap_port(lp_ctx),
+ gai_strerror(ret), nt_errstr(status)));
+ return status;
+ }
+
+ /* listen for unicasts on the CLDAP port (389) */
+ status = cldap_socket_init(cldapd,
+ socket_address,
+ NULL,
+ &cldapsock);
+ if (!NT_STATUS_IS_OK(status)) {
+ DEBUG(0,("Failed to bind to %s - %s\n",
+ tsocket_address_string(socket_address, socket_address),
+ nt_errstr(status)));
+ talloc_free(socket_address);
+ return status;
+ }
+ talloc_free(socket_address);
+
+ cldap_set_incoming_handler(cldapsock, cldapd->task->event_ctx,
+ cldapd_request_handler, cldapd);
+
+ return NT_STATUS_OK;
+}
+
+/*
+ setup our listening sockets on the configured network interfaces
+*/
+static NTSTATUS cldapd_startup_interfaces(struct cldapd_server *cldapd, struct loadparm_context *lp_ctx,
+ struct interface *ifaces)
+{
+ int i, num_interfaces;
+ TALLOC_CTX *tmp_ctx = talloc_new(cldapd);
+ NTSTATUS status;
+
+ num_interfaces = iface_list_count(ifaces);
+
+ /* if we are allowing incoming packets from any address, then
+ we need to bind to the wildcard address */
+ if (!lpcfg_bind_interfaces_only(lp_ctx)) {
+ size_t num_binds = 0;
+ char **wcard = iface_list_wildcard(cldapd);
+ NT_STATUS_HAVE_NO_MEMORY(wcard);
+ for (i=0; wcard[i]; i++) {
+ status = cldapd_add_socket(cldapd, lp_ctx, wcard[i]);
+ if (NT_STATUS_IS_OK(status)) {
+ num_binds++;
+ }
+ }
+ talloc_free(wcard);
+ if (num_binds == 0) {
+ return NT_STATUS_INVALID_PARAMETER_MIX;
+ }
+ }
+
+ /* now we have to also listen on the specific interfaces,
+ so that replies always come from the right IP */
+ for (i=0; i<num_interfaces; i++) {
+ const char *address = talloc_strdup(tmp_ctx, iface_list_n_ip(ifaces, i));
+ status = cldapd_add_socket(cldapd, lp_ctx, address);
+ NT_STATUS_NOT_OK_RETURN(status);
+ }
+
+ talloc_free(tmp_ctx);
+
+ return NT_STATUS_OK;
+}
+
+/*
+ startup the cldapd task
+*/
+static NTSTATUS cldapd_task_init(struct task_server *task)
+{
+ struct cldapd_server *cldapd;
+ NTSTATUS status;
+ struct interface *ifaces;
+
+ load_interface_list(task, task->lp_ctx, &ifaces);
+
+ if (iface_list_count(ifaces) == 0) {
+ task_server_terminate(task, "cldapd: no network interfaces configured", false);
+ return NT_STATUS_UNSUCCESSFUL;
+ }
+
+ switch (lpcfg_server_role(task->lp_ctx)) {
+ case ROLE_STANDALONE:
+ task_server_terminate(task, "cldap_server: no CLDAP server required in standalone configuration",
+ false);
+ return NT_STATUS_INVALID_DOMAIN_ROLE;
+ case ROLE_DOMAIN_MEMBER:
+ task_server_terminate(task, "cldap_server: no CLDAP server required in member server configuration",
+ false);
+ return NT_STATUS_INVALID_DOMAIN_ROLE;
+ case ROLE_ACTIVE_DIRECTORY_DC:
+ /* Yes, we want an CLDAP server */
+ break;
+ }
+
+ task_server_set_title(task, "task[cldapd]");
+
+ cldapd = talloc(task, struct cldapd_server);
+ if (cldapd == NULL) {
+ task_server_terminate(task, "cldapd: out of memory", true);
+ return NT_STATUS_NO_MEMORY;
+ }
+
+ cldapd->task = task;
+ cldapd->samctx = samdb_connect(cldapd,
+ task->event_ctx,
+ task->lp_ctx,
+ system_session(task->lp_ctx),
+ NULL,
+ 0);
+ if (cldapd->samctx == NULL) {
+ task_server_terminate(task, "cldapd failed to open samdb", true);
+ return NT_STATUS_UNSUCCESSFUL;
+ }
+
+ /* start listening on the configured network interfaces */
+ status = cldapd_startup_interfaces(cldapd, task->lp_ctx, ifaces);
+ if (!NT_STATUS_IS_OK(status)) {
+ task_server_terminate(task, "cldapd failed to setup interfaces", true);
+ return status;
+ }
+
+ irpc_add_name(task->msg_ctx, "cldap_server");
+
+ return NT_STATUS_OK;
+}
+
+
+/*
+ register ourselves as a available server
+*/
+NTSTATUS server_service_cldapd_init(TALLOC_CTX *ctx)
+{
+ static const struct service_details details = {
+ .inhibit_fork_on_accept = true,
+ .inhibit_pre_fork = true,
+ .task_init = cldapd_task_init,
+ .post_fork = NULL
+ };
+ return register_server_service(ctx, "cldap", &details);
+}
diff --git a/source4/cldap_server/cldap_server.h b/source4/cldap_server/cldap_server.h
new file mode 100644
index 0000000..0725284
--- /dev/null
+++ b/source4/cldap_server/cldap_server.h
@@ -0,0 +1,35 @@
+/*
+ Unix SMB/CIFS implementation.
+
+ CLDAP server structures
+
+ Copyright (C) Andrew Tridgell 2005
+
+ 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 "libcli/cldap/cldap.h"
+#include "libcli/ldap/libcli_ldap.h"
+
+/*
+ top level context structure for the cldap server
+*/
+struct cldapd_server {
+ struct task_server *task;
+ struct ldb_context *samctx;
+};
+
+struct ldap_SearchRequest;
+
+#include "cldap_server/proto.h"
diff --git a/source4/cldap_server/rootdse.c b/source4/cldap_server/rootdse.c
new file mode 100644
index 0000000..89f0a66
--- /dev/null
+++ b/source4/cldap_server/rootdse.c
@@ -0,0 +1,183 @@
+/*
+ Unix SMB/CIFS implementation.
+
+ CLDAP server - rootdse handling
+
+ Copyright (C) Stefan Metzmacher 2006
+
+ 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 <tevent.h>
+#include <ldb.h>
+#include <ldb_errors.h>
+#include "samba/service_task.h"
+#include "cldap_server/cldap_server.h"
+#include "librpc/gen_ndr/ndr_misc.h"
+#include "dsdb/samdb/samdb.h"
+#include "ldb_wrap.h"
+
+static void cldapd_rootdse_fill(struct cldapd_server *cldapd,
+ TALLOC_CTX *mem_ctx,
+ struct ldap_SearchRequest *search,
+ struct ldap_SearchResEntry **response,
+ struct ldap_Result *result)
+{
+ struct ldap_SearchResEntry *ent = NULL;
+ struct ldb_result *res = NULL;
+ struct ldb_request *lreq;
+ const char **attrs = NULL;
+ const char *errstr = NULL;
+ int ret = LDAP_SUCCESS, ldb_ret;
+
+ if (search->num_attributes >= 1) {
+ int i;
+
+ attrs = talloc_array(mem_ctx, const char *, search->num_attributes+1);
+ if (attrs == NULL) goto nomem;
+
+ for (i=0; i < search->num_attributes; i++) {
+ attrs[i] = search->attributes[i];
+ }
+ attrs[i] = NULL;
+ }
+
+ res = talloc_zero(mem_ctx, struct ldb_result);
+ if (res == NULL) goto nomem;
+
+ ldb_ret = ldb_build_search_req_ex(&lreq, cldapd->samctx, mem_ctx,
+ NULL, LDB_SCOPE_BASE,
+ search->tree, attrs,
+ NULL,
+ res, ldb_search_default_callback,
+ NULL);
+
+ if (ldb_ret != LDB_SUCCESS) {
+ goto reply;
+ }
+
+ /* Copy the timeout from the incoming call */
+ ldb_set_timeout(cldapd->samctx, lreq, search->timelimit);
+
+ ldb_ret = ldb_request(cldapd->samctx, lreq);
+ if (ldb_ret != LDB_SUCCESS) {
+ goto reply;
+ }
+
+ ldb_ret = ldb_wait(lreq->handle, LDB_WAIT_ALL);
+ if (ldb_ret != LDB_SUCCESS) {
+ goto reply;
+ }
+
+ if (res->count > 1) {
+ errstr = "Internal error: to much replies";
+ ldb_ret = LDB_ERR_OTHER;
+ goto reply;
+ } else if (res->count == 1) {
+ int j;
+
+ ent = talloc(mem_ctx, struct ldap_SearchResEntry);
+ if (ent == NULL) goto nomem;
+
+ ent->dn = ldb_dn_alloc_linearized(ent, res->msgs[0]->dn);
+ if (ent->dn == NULL) goto nomem;
+ ent->num_attributes = 0;
+ ent->attributes = NULL;
+ if (res->msgs[0]->num_elements == 0) {
+ goto reply;
+ }
+ ent->num_attributes = res->msgs[0]->num_elements;
+ ent->attributes = talloc_array(ent, struct ldb_message_element,
+ ent->num_attributes);
+ if (ent->attributes == NULL) goto nomem;
+ for (j=0; j < ent->num_attributes; j++) {
+ ent->attributes[j].name = talloc_steal(ent->attributes,
+ res->msgs[0]->elements[j].name);
+ ent->attributes[j].num_values = 0;
+ ent->attributes[j].values = NULL;
+ if (search->attributesonly && (res->msgs[0]->elements[j].num_values == 0)) {
+ continue;
+ }
+ ent->attributes[j].num_values = res->msgs[0]->elements[j].num_values;
+ ent->attributes[j].values = res->msgs[0]->elements[j].values;
+ talloc_steal(ent->attributes, res->msgs[0]->elements[j].values);
+ }
+ }
+
+reply:
+ if (ret != LDAP_SUCCESS) {
+ /* nothing ... */
+ } else if (ldb_ret == LDB_SUCCESS) {
+ ret = LDAP_SUCCESS;
+ errstr = NULL;
+ } else {
+ ret = ldb_ret;
+ errstr = ldb_errstring(cldapd->samctx);
+ }
+ goto done;
+nomem:
+ talloc_free(ent);
+ ret = LDAP_OPERATIONS_ERROR;
+ errstr = "No memory";
+done:
+ *response = ent;
+ result->resultcode = ret;
+ result->errormessage = (errstr?talloc_strdup(mem_ctx, errstr):NULL);
+}
+
+/*
+ handle incoming cldap requests
+*/
+void cldapd_rootdse_request(struct cldap_socket *cldap,
+ struct cldapd_server *cldapd,
+ TALLOC_CTX *tmp_ctx,
+ uint32_t message_id,
+ struct ldap_SearchRequest *search,
+ struct tsocket_address *src)
+{
+ NTSTATUS status;
+ struct cldap_reply reply;
+ struct ldap_Result result;
+
+ ZERO_STRUCT(result);
+
+ reply.messageid = message_id;
+ reply.dest = src;
+ reply.response = NULL;
+ reply.result = &result;
+
+ /* Note: The remoteAddress should rather be set on a ldb request.
+ * We can set this savely on the context here,
+ * since cldapd_rootdse_fill operates synchronously. */
+ ldb_set_opaque(cldapd->samctx, "remoteAddress", src);
+
+ cldapd_rootdse_fill(cldapd, tmp_ctx, search, &reply.response,
+ reply.result);
+
+ /*
+ * We clear this after cldapd_rootdse_fill as this is shared ldb
+ * and if it was not cleared the audit logging would report changes
+ * made by internal processes as coming from the last cldap requester
+ */
+ ldb_set_opaque(cldapd->samctx, "remoteAddress", NULL);
+
+ status = cldap_reply_send(cldap, &reply);
+ if (!NT_STATUS_IS_OK(status)) {
+ DEBUG(2,("cldap rootdse query failed '%s' - %s\n",
+ ldb_filter_from_tree(tmp_ctx, search->tree), nt_errstr(status)));
+ }
+
+ return;
+}
diff --git a/source4/cldap_server/wscript_build b/source4/cldap_server/wscript_build
new file mode 100644
index 0000000..928b91b
--- /dev/null
+++ b/source4/cldap_server/wscript_build
@@ -0,0 +1,17 @@
+#!/usr/bin/env python
+
+bld.SAMBA_MODULE('service_cldap',
+ source='cldap_server.c',
+ subsystem='service',
+ init_function='server_service_cldapd_init',
+ internal_module=False,
+ deps='CLDAPD process_model netif'
+ )
+
+
+bld.SAMBA_SUBSYSTEM('CLDAPD',
+ source='rootdse.c',
+ autoproto='proto.h',
+ deps='cli_cldap ldbsamba'
+ )
+