r24780: More work allowing libutil to be used by external users.
[kai/samba.git] / source4 / ldap_server / ldap_server.c
index 8aacbb63693927da23e3da98faee63fbead9e823..0de76052d1c3c66398af3c95b19a76f5a1ee5945 100644 (file)
@@ -9,7 +9,7 @@
    
    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 2 of the License, or
+   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,
    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, write to the Free Software
-   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+   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 "dlinklist.h"
+#include "auth/credentials/credentials.h"
+#include "librpc/gen_ndr/ndr_samr.h"
+#include "lib/util/dlinklist.h"
 #include "libcli/util/asn_1.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 "system/network.h"
-#include "netif/netif.h"
-
+#include "lib/socket/netif.h"
+#include "dsdb/samdb/samdb.h"
 /*
   close the socket and shutdown a server_context
 */
@@ -96,6 +98,7 @@ static void ldapsrv_process_message(struct ldapsrv_connection *conn,
        /* 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)) {
@@ -104,12 +107,12 @@ static void ldapsrv_process_message(struct ldapsrv_connection *conn,
                        return;
                }
 
-               status = data_blob_append(call, &blob, b.data, b.length);
+               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 (!NT_STATUS_IS_OK(status)) {
+               if (!ret) {
                        talloc_free(call);
                        return;
                }
@@ -128,27 +131,31 @@ static void ldapsrv_process_message(struct ldapsrv_connection *conn,
 */
 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;
+       struct asn1_data *asn1 = asn1_init(conn);
        struct ldap_message *msg = talloc(conn, struct ldap_message);
 
-       if (msg == NULL) {
+       if (asn1 == NULL || msg == NULL) {
                return NT_STATUS_NO_MEMORY;
        }
 
-       if (!asn1_load(&asn1, blob)) {
+       if (!asn1_load(asn1, blob)) {
+               talloc_free(msg);
+               talloc_free(asn1);
                return NT_STATUS_NO_MEMORY;
        }
 
-       if (!ldap_decode(&asn1, msg)) {
-               asn1_free(&asn1);
-               return NT_STATUS_LDAP(LDAP_PROTOCOL_ERROR);
+       status = ldap_decode(asn1, msg);
+       if (!NT_STATUS_IS_OK(status)) {
+               asn1_free(asn1);
+               return status;
        }
 
        data_blob_free(&blob);
        ldapsrv_process_message(conn, msg);
-       asn1_free(&asn1);
+       asn1_free(asn1);
        return NT_STATUS_OK;
 }
 
@@ -217,7 +224,6 @@ static int ldapsrv_load_limits(struct ldapsrv_connection *conn)
        TALLOC_CTX *tmp_ctx;
        const char *attrs[] = { "configurationNamingContext", NULL };
        const char *attrs2[] = { "lDAPAdminLimits", NULL };
-       const char *conf_dn_s;
        struct ldb_message_element *el;
        struct ldb_result *res = NULL;
        struct ldb_dn *basedn;
@@ -237,34 +243,41 @@ static int ldapsrv_load_limits(struct ldapsrv_connection *conn)
                return -1;
        }
 
-       basedn = ldb_dn_explode(tmp_ctx, "");
-       if (basedn == NULL) {
+       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);
-       talloc_steal(tmp_ctx, res);
-       if (ret != LDB_SUCCESS || res->count != 1) {
+       if (ret != LDB_SUCCESS) {
                goto failed;
        }
 
-       conf_dn_s = ldb_msg_find_string(res->msgs[0], "configurationNamingContext", NULL);
-       if (conf_dn_s == NULL) {
+       talloc_steal(tmp_ctx, res);
+
+       if (res->count != 1) {
                goto failed;
        }
-       conf_dn = ldb_dn_explode(tmp_ctx, conf_dn_s);
+
+       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_string_compose(tmp_ctx, conf_dn, "CN=Default Query Policy,CN=Query-Policies,CN=Directory Service,CN=Windows NT,CN=Services");
+       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 (ret != LDB_SUCCESS || res->count != 1) {
+
+       if (res->count != 1) {
                goto failed;
        }
 
@@ -432,6 +445,11 @@ static NTSTATUS add_socket(struct event_context *event_context,
 {
        uint16_t port = 389;
        NTSTATUS status;
+       const char *attrs[] = { "options", NULL };
+       int ret;
+       struct ldb_result *res;
+       struct ldb_context *ldb;
+       int options;
 
        status = stream_setup_socket(event_context, model_ops, &ldap_stream_ops, 
                                     "ipv4", address, &port, ldap_service);
@@ -451,8 +469,28 @@ static NTSTATUS add_socket(struct event_context *event_context,
                }
        }
 
-       /* if we are a PDC, then also enable the global catalog server port, 3268 */
-       if (lp_server_role() == ROLE_DOMAIN_PDC) {
+       /* Load LDAP database */
+       ldb = samdb_connect(ldap_service, system_session(ldap_service));
+       if (!ldb) {
+               return NT_STATUS_INTERNAL_DB_CORRUPTION;
+       }
+       
+       /* Query cn=ntds settings,.... */
+       ret = ldb_search(ldb, samdb_ntds_settings_dn(ldb), LDB_SCOPE_BASE, NULL, attrs, &res);
+       if (ret) {
+               return NT_STATUS_INTERNAL_DB_CORRUPTION;
+       }
+       if (res->count != 1) {
+               talloc_free(res);
+               return NT_STATUS_NOT_FOUND;
+       }
+
+       options = ldb_msg_find_attr_as_int(res->msgs[0], "options", 0);
+       talloc_free(res);
+       talloc_free(ldb);
+
+       /* if options attribute has the 0x00000001 flag set, then enable the global catlog */
+       if (options & 0x000000001) {
                port = 3268;
                status = stream_setup_socket(event_context, model_ops, &ldap_stream_ops, 
                                             "ipv4", address, &port, ldap_service);
@@ -472,9 +510,14 @@ static void ldapsrv_task_init(struct task_server *task)
 {      
        struct ldapsrv_service *ldap_service;
        NTSTATUS status;
+       const struct model_ops *model_ops;
 
        task_server_set_title(task, "task[ldapsrv]");
 
+       /* run the ldap server as a single process */
+       model_ops = process_model_byname("single");
+       if (!model_ops) goto failed;
+
        ldap_service = talloc_zero(task, struct ldapsrv_service);
        if (ldap_service == NULL) goto failed;
 
@@ -491,11 +534,11 @@ static void ldapsrv_task_init(struct task_server *task)
                */
                for(i = 0; i < num_interfaces; i++) {
                        const char *address = iface_n_ip(i);
-                       status = add_socket(task->event_ctx, task->model_ops, address, ldap_service);
+                       status = add_socket(task->event_ctx, model_ops, address, ldap_service);
                        if (!NT_STATUS_IS_OK(status)) goto failed;
                }
        } else {
-               status = add_socket(task->event_ctx, task->model_ops, lp_socket_address(), ldap_service);
+               status = add_socket(task->event_ctx, model_ops, lp_socket_address(), ldap_service);
                if (!NT_STATUS_IS_OK(status)) goto failed;
        }