Avoid including libds/common/roles.h in public loadparm.h header.
[obnox/samba/samba-obnox.git] / source4 / cldap_server / cldap_server.c
index 783e31d1ae622267ba55ed65a59f891d8157fb49..ced2a2088f255ba3a8500c67eb5d06e46ce15341 100644 (file)
 */
 
 #include "includes.h"
-#include "libcli/ldap/ldap.h"
-#include "lib/socket/socket.h"
+#include <talloc.h>
 #include "lib/messaging/irpc.h"
 #include "smbd/service_task.h"
 #include "smbd/service.h"
 #include "cldap_server/cldap_server.h"
 #include "system/network.h"
 #include "lib/socket/netif.h"
-#include "lib/ldb/include/ldb.h"
-#include "lib/ldb/include/ldb_errors.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(void);
 
 /*
   handle incoming cldap requests
 */
-static void cldapd_request_handler(struct cldap_socket *cldap, 
-                                  struct ldap_message *ldap_msg, 
-                                  struct socket_address *src)
+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 (ldap_msg->type != LDAP_TAG_SearchRequest) {
-               DEBUG(0,("Invalid CLDAP request type %d from %s:%d\n", 
-                        ldap_msg->type, src->addr, src->port));
-               cldap_error_reply(cldap, ldap_msg->messageid, src,
+
+       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 = &ldap_msg->r.SearchRequest;
+       search = &in->ldap_msg->r.SearchRequest;
 
        if (strcmp("", search->basedn) != 0) {
-               DEBUG(0,("Invalid CLDAP basedn '%s' from %s:%d\n", 
-                        search->basedn, src->addr, src->port));
-               cldap_error_reply(cldap, ldap_msg->messageid, src,
+               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:%d\n", 
-                        search->scope, src->addr, src->port));
-               cldap_error_reply(cldap, ldap_msg->messageid, src,
+               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;
        }
 
-       if (search->num_attributes == 1 &&
-           strcasecmp(search->attributes[0], "netlogon") == 0) {
-               cldapd_netlogon_request(cldap, ldap_msg->messageid,
-                                       search->tree, src);
-               return;
-       }
-
-       cldapd_rootdse_request(cldap, ldap_msg->messageid,
-                              search, src);
+       cldapd_rootdse_request(cldap, cldapd, in,
+                              in->ldap_msg->messageid,
+                              search, in->src);
+       talloc_free(in);
 }
 
 
@@ -88,63 +102,81 @@ static NTSTATUS cldapd_add_socket(struct cldapd_server *cldapd, struct loadparm_
                                  const char *address)
 {
        struct cldap_socket *cldapsock;
-       struct socket_address *socket_address;
+       struct tsocket_address *socket_address;
        NTSTATUS status;
-
-       /* listen for unicasts on the CLDAP port (389) */
-       cldapsock = cldap_socket_init(cldapd, cldapd->task->event_ctx, lp_iconv_convenience(cldapd->task->lp_ctx));
-       NT_STATUS_HAVE_NO_MEMORY(cldapsock);
-
-       socket_address = socket_address_from_strings(cldapsock, cldapsock->sock->backend_name, 
-                                                    address, lp_cldap_port(lp_ctx));
-       if (!socket_address) {
-               talloc_free(cldapsock);
-               return NT_STATUS_NO_MEMORY;
+       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;
        }
 
-       status = socket_listen(cldapsock->sock, socket_address, 0, 0);
+       /* 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:%d - %s\n", 
-                        address, lp_cldap_port(lp_ctx), nt_errstr(status)));
-               talloc_free(cldapsock);
+               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_request_handler, cldapd);
+       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 num_interfaces;
+       int i, num_interfaces;
        TALLOC_CTX *tmp_ctx = talloc_new(cldapd);
        NTSTATUS status;
 
-       num_interfaces = iface_count(ifaces);
+       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 (!lp_bind_interfaces_only(lp_ctx)) {
-               status = cldapd_add_socket(cldapd, lp_ctx, "0.0.0.0");
-               NT_STATUS_NOT_OK_RETURN(status);
-       } else {
-               int i;
-
-               for (i=0; i<num_interfaces; i++) {
-                       const char *address = talloc_strdup(tmp_ctx, iface_n_ip(ifaces, i));
-                       status = cldapd_add_socket(cldapd, lp_ctx, address);
-                       NT_STATUS_NOT_OK_RETURN(status);
+       if (!lpcfg_bind_interfaces_only(lp_ctx)) {
+               int 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;
@@ -159,21 +191,23 @@ static void cldapd_task_init(struct task_server *task)
        NTSTATUS status;
        struct interface *ifaces;
        
-       load_interfaces(task, lp_interfaces(task->lp_ctx), &ifaces);
+       load_interface_list(task, task->lp_ctx, &ifaces);
 
-       if (iface_count(ifaces) == 0) {
-               task_server_terminate(task, "cldapd: no network interfaces configured");
+       if (iface_list_count(ifaces) == 0) {
+               task_server_terminate(task, "cldapd: no network interfaces configured", false);
                return;
        }
 
-       switch (lp_server_role(task->lp_ctx)) {
+       switch (lpcfg_server_role(task->lp_ctx)) {
        case ROLE_STANDALONE:
-               task_server_terminate(task, "cldap_server: no CLDAP server required in standalone configuration");
+               task_server_terminate(task, "cldap_server: no CLDAP server required in standalone configuration", 
+                                     false);
                return;
        case ROLE_DOMAIN_MEMBER:
-               task_server_terminate(task, "cldap_server: no CLDAP server required in member server configuration");
+               task_server_terminate(task, "cldap_server: no CLDAP server required in member server configuration",
+                                     false);
                return;
-       case ROLE_DOMAIN_CONTROLLER:
+       case ROLE_ACTIVE_DIRECTORY_DC:
                /* Yes, we want an CLDAP server */
                break;
        }
@@ -182,21 +216,21 @@ static void cldapd_task_init(struct task_server *task)
 
        cldapd = talloc(task, struct cldapd_server);
        if (cldapd == NULL) {
-               task_server_terminate(task, "cldapd: out of memory");
+               task_server_terminate(task, "cldapd: out of memory", true);
                return;
        }
 
        cldapd->task = task;
-       cldapd->samctx = samdb_connect(cldapd, task->lp_ctx, anonymous_session(cldapd, task->lp_ctx));
+       cldapd->samctx = samdb_connect(cldapd, task->event_ctx, task->lp_ctx, system_session(task->lp_ctx), 0);
        if (cldapd->samctx == NULL) {
-               task_server_terminate(task, "cldapd failed to open samdb");
+               task_server_terminate(task, "cldapd failed to open samdb", true);
                return;
        }
 
        /* 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");
+               task_server_terminate(task, "cldapd failed to setup interfaces", true);
                return;
        }