LDB ASYNC: misc changes
[samba.git] / source4 / libcli / ldap / ldap_client.c
index 4fa8cc01464fe1121f276f5acfc944f4afcc2ab5..fc5863b671f32ee3d8ac4d757d35cab185542f64 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 "libcli/util/asn_1.h"
+#include "lib/util/asn1.h"
 #include "lib/util/dlinklist.h"
 #include "lib/events/events.h"
 #include "lib/socket/socket.h"
 #include "libcli/ldap/ldap.h"
+#include "libcli/ldap/ldap_proto.h"
 #include "libcli/ldap/ldap_client.h"
 #include "libcli/composite/composite.h"
 #include "lib/stream/packet.h"
 #include "lib/tls/tls.h"
 #include "auth/gensec/gensec.h"
 #include "system/time.h"
+#include "param/param.h"
+#include "libcli/resolve/resolve.h"
 
-
-/*
+/**
   create a new ldap_connection stucture. The event context is optional
 */
-struct ldap_connection *ldap_new_connection(TALLOC_CTX *mem_ctx, 
-                                           struct event_context *ev)
+_PUBLIC_ struct ldap_connection *ldap4_new_connection(TALLOC_CTX *mem_ctx, 
+                                            struct loadparm_context *lp_ctx,
+                                            struct event_context *ev)
 {
        struct ldap_connection *conn;
 
-       conn = talloc_zero(mem_ctx, struct ldap_connection);
-       if (conn == NULL) {
+       if (ev == NULL) {
                return NULL;
        }
 
-       if (ev == NULL) {
-               ev = event_context_init(conn);
-               if (ev == NULL) {
-                       talloc_free(conn);
-                       return NULL;
-               }
+       conn = talloc_zero(mem_ctx, struct ldap_connection);
+       if (conn == NULL) {
+               return NULL;
        }
 
        conn->next_messageid  = 1;
        conn->event.event_ctx = ev;
 
+       conn->lp_ctx = lp_ctx;
+
        /* set a reasonable request timeout */
        conn->timeout = 60;
 
@@ -77,6 +77,12 @@ static void ldap_connection_dead(struct ldap_connection *conn)
 {
        struct ldap_request *req;
 
+       talloc_free(conn->sock);  /* this will also free event.fde */
+       talloc_free(conn->packet);
+       conn->sock = NULL;
+       conn->event.fde = NULL;
+       conn->packet = NULL;
+
        /* return an error for any pending request ... */
        while (conn->pending) {
                req = conn->pending;
@@ -87,12 +93,6 @@ static void ldap_connection_dead(struct ldap_connection *conn)
                        req->async.fn(req);
                }
        }
-
-       talloc_free(conn->sock);  /* this will also free event.fde */
-       talloc_free(conn->packet);
-       conn->sock = NULL;
-       conn->event.fde = NULL;
-       conn->packet = NULL;
 }
 
 static void ldap_reconnect(struct ldap_connection *conn);
@@ -117,6 +117,7 @@ static void ldap_error_handler(void *private_data, NTSTATUS status)
 static void ldap_match_message(struct ldap_connection *conn, struct ldap_message *msg)
 {
        struct ldap_request *req;
+       int i;
 
        for (req=conn->pending; req; req=req->next) {
                if (req->messageid == msg->messageid) break;
@@ -133,6 +134,20 @@ static void ldap_match_message(struct ldap_connection *conn, struct ldap_message
                return;
        }
 
+       /* Check for undecoded critical extensions */
+       for (i=0; msg->controls && msg->controls[i]; i++) {
+               if (!msg->controls_decoded[i] && 
+                   msg->controls[i]->critical) {
+                       req->status = NT_STATUS_LDAP(LDAP_UNAVAILABLE_CRITICAL_EXTENSION);
+                       req->state = LDAP_REQUEST_DONE;
+                       DLIST_REMOVE(conn->pending, req);
+                       if (req->async.fn) {
+                               req->async.fn(req);
+                       }
+                       return;
+               }
+       }
+
        /* add to the list of replies received */
        talloc_steal(req, msg);
        req->replies = talloc_realloc(req, req->replies, 
@@ -169,27 +184,32 @@ static void ldap_match_message(struct ldap_connection *conn, struct ldap_message
 */
 static NTSTATUS ldap_recv_handler(void *private_data, DATA_BLOB blob)
 {
-       struct asn1_data asn1;
+       NTSTATUS status;
        struct ldap_connection *conn = talloc_get_type(private_data, 
                                                       struct ldap_connection);
        struct ldap_message *msg = talloc(conn, struct ldap_message);
+       struct asn1_data *asn1 = asn1_init(conn);
 
-       if (msg == NULL) {
+       if (asn1 == NULL || msg == NULL) {
                return NT_STATUS_LDAP(LDAP_PROTOCOL_ERROR);
        }
 
-       if (!asn1_load(&asn1, blob)) {
+       if (!asn1_load(asn1, blob)) {
+               talloc_free(msg);
+               talloc_free(asn1);
                return NT_STATUS_LDAP(LDAP_PROTOCOL_ERROR);
        }
        
-       if (!ldap_decode(&asn1, msg)) {
-               return NT_STATUS_LDAP(LDAP_PROTOCOL_ERROR);
+       status = ldap_decode(asn1, msg);
+       if (!NT_STATUS_IS_OK(status)) {
+               asn1_free(asn1);
+               return status;
        }
 
        ldap_match_message(conn, msg);
 
        data_blob_free(&blob);
-       asn1_free(&asn1);
+       asn1_free(asn1);
        return NT_STATUS_OK;
 }
 
@@ -222,7 +242,7 @@ static void ldap_io_handler(struct event_context *ev, struct fd_event *fde,
   parse a ldap URL
 */
 static NTSTATUS ldap_parse_basic_url(TALLOC_CTX *mem_ctx, const char *url,
-                                    char **host, uint16_t *port, BOOL *ldaps)
+                                    char **host, uint16_t *port, bool *ldaps)
 {
        int tmp_port = 0;
        char protocol[11];
@@ -239,10 +259,10 @@ static NTSTATUS ldap_parse_basic_url(TALLOC_CTX *mem_ctx, const char *url,
 
        if (strequal(protocol, "ldap")) {
                *port = 389;
-               *ldaps = False;
+               *ldaps = false;
        } else if (strequal(protocol, "ldaps")) {
                *port = 636;
-               *ldaps = True;
+               *ldaps = true;
        } else {
                DEBUG(0, ("unrecognised ldap protocol (%s)!\n", protocol));
                return NT_STATUS_PROTOCOL_UNREACHABLE;
@@ -269,7 +289,7 @@ struct ldap_connect_state {
 static void ldap_connect_recv_unix_conn(struct composite_context *ctx);
 static void ldap_connect_recv_tcp_conn(struct composite_context *ctx);
 
-struct composite_context *ldap_connect_send(struct ldap_connection *conn,
+_PUBLIC_ struct composite_context *ldap_connect_send(struct ldap_connection *conn,
                                            const char *url)
 {
        struct composite_context *result, *ctx;
@@ -277,7 +297,7 @@ struct composite_context *ldap_connect_send(struct ldap_connection *conn,
        char protocol[11];
        int ret;
 
-       result = talloc_zero(NULL, struct composite_context);
+       result = talloc_zero(conn, struct composite_context);
        if (result == NULL) goto failed;
        result->state = COMPOSITE_STATE_IN_PROGRESS;
        result->async.fn = NULL;
@@ -311,9 +331,18 @@ struct composite_context *ldap_connect_send(struct ldap_connection *conn,
                if (!NT_STATUS_IS_OK(status)) {
                        return NULL;
                }
+               talloc_steal(conn, conn->sock);
                SMB_ASSERT(sizeof(protocol)>10);
                SMB_ASSERT(sizeof(path)>1024);
        
+               /* LDAPI connections are to localhost, so give the local host name as the target for gensec */
+               conn->host = talloc_asprintf(conn, "%s.%s", lp_netbios_name(conn->lp_ctx),  lp_realm(conn->lp_ctx));
+               if (composite_nomem(conn->host, state->ctx)) {
+                       return result;
+               }
+
+               /* The %c specifier doesn't null terminate :-( */
+               ZERO_STRUCT(path);
                ret = sscanf(url, "%10[^:]://%1025c", protocol, path);
                if (ret < 2) {
                        composite_error(state->ctx, NT_STATUS_INVALID_PARAMETER);
@@ -329,7 +358,7 @@ struct composite_context *ldap_connect_send(struct ldap_connection *conn,
                }
 
                ctx = socket_connect_send(conn->sock, NULL, unix_addr, 
-                                         0, conn->event.event_ctx);
+                                         0, lp_resolve_context(conn->lp_ctx), conn->event.event_ctx);
                ctx->async.fn = ldap_connect_recv_unix_conn;
                ctx->async.private_data = state;
                return result;
@@ -342,7 +371,7 @@ struct composite_context *ldap_connect_send(struct ldap_connection *conn,
                }
                
                ctx = socket_connect_multi_send(state, conn->host, 1, &conn->port,
-                                               conn->event.event_ctx);
+                                               lp_resolve_context(conn->lp_ctx), conn->event.event_ctx);
                if (ctx == NULL) goto failed;
 
                ctx->async.fn = ldap_connect_recv_tcp_conn;
@@ -354,27 +383,43 @@ struct composite_context *ldap_connect_send(struct ldap_connection *conn,
        return NULL;
 }
 
-static void ldap_connect_got_sock(struct composite_context *ctx, struct ldap_connection *conn) 
+static void ldap_connect_got_sock(struct composite_context *ctx, 
+                                 struct ldap_connection *conn) 
 {
        /* setup a handler for events on this socket */
        conn->event.fde = event_add_fd(conn->event.event_ctx, conn->sock, 
                                       socket_get_fd(conn->sock), 
-                                      EVENT_FD_READ, ldap_io_handler, conn);
+                                      EVENT_FD_READ | EVENT_FD_AUTOCLOSE, ldap_io_handler, conn);
        if (conn->event.fde == NULL) {
                composite_error(ctx, NT_STATUS_INTERNAL_ERROR);
                return;
        }
 
+       socket_set_flags(conn->sock, SOCKET_FLAG_NOCLOSE);
+
        talloc_steal(conn, conn->sock);
        if (conn->ldaps) {
-               struct socket_context *tls_socket = tls_init_client(conn->sock, conn->event.fde);
+               struct socket_context *tls_socket;
+               struct socket_context *tmp_socket;
+               char *cafile = private_path(conn->sock, conn->lp_ctx, lp_tls_cafile(conn->lp_ctx));
+
+               if (!cafile || !*cafile) {
+                       talloc_free(conn->sock);
+                       return;
+               }
+
+               tls_socket = tls_init_client(conn->sock, conn->event.fde, cafile);
+               talloc_free(cafile);
+
                if (tls_socket == NULL) {
                        talloc_free(conn->sock);
                        return;
                }
-               talloc_unlink(conn, conn->sock);
-               conn->sock = tls_socket;
-               talloc_steal(conn, conn->sock);
+
+               /* the original socket, must become a child of the tls socket */
+               tmp_socket = conn->sock;
+               conn->sock = talloc_steal(conn, tls_socket);
+               talloc_steal(conn->sock, tmp_socket);
        }
 
        conn->packet = packet_init(conn);
@@ -390,7 +435,7 @@ static void ldap_connect_got_sock(struct composite_context *ctx, struct ldap_con
        packet_set_error_handler(conn->packet, ldap_error_handler);
        packet_set_event_context(conn->packet, conn->event.event_ctx);
        packet_set_fde(conn->packet, conn->event.fde);
-       packet_set_serialise(conn->packet);
+/*     packet_set_serialise(conn->packet); */
 
        composite_done(ctx);
 }
@@ -402,10 +447,9 @@ static void ldap_connect_recv_tcp_conn(struct composite_context *ctx)
                                struct ldap_connect_state);
        struct ldap_connection *conn = state->conn;
        uint16_t port;
-
        NTSTATUS status = socket_connect_multi_recv(ctx, state, &conn->sock,
                                                       &port);
-       if (!NT_STATUS_IS_OK(state->ctx->status)) {
+       if (!NT_STATUS_IS_OK(status)) {
                composite_error(state->ctx, status);
                return;
        }
@@ -430,14 +474,14 @@ static void ldap_connect_recv_unix_conn(struct composite_context *ctx)
        ldap_connect_got_sock(state->ctx, conn);
 }
 
-NTSTATUS ldap_connect_recv(struct composite_context *ctx)
+_PUBLIC_ NTSTATUS ldap_connect_recv(struct composite_context *ctx)
 {
        NTSTATUS status = composite_wait(ctx);
        talloc_free(ctx);
        return status;
 }
 
-NTSTATUS ldap_connect(struct ldap_connection *conn, const char *url)
+_PUBLIC_ NTSTATUS ldap_connect(struct ldap_connection *conn, const char *url)
 {
        struct composite_context *ctx = ldap_connect_send(conn, url);
        return ldap_connect_recv(ctx);
@@ -445,7 +489,7 @@ NTSTATUS ldap_connect(struct ldap_connection *conn, const char *url)
 
 /* set reconnect parameters */
 
-void ldap_set_reconn_params(struct ldap_connection *conn, int max_retries)
+_PUBLIC_ void ldap_set_reconn_params(struct ldap_connection *conn, int max_retries)
 {
        if (conn) {
                conn->reconnect.max_retries = max_retries;
@@ -530,7 +574,7 @@ static void ldap_request_complete(struct event_context *ev, struct timed_event *
 /*
   send a ldap message - async interface
 */
-struct ldap_request *ldap_request_send(struct ldap_connection *conn,
+_PUBLIC_ struct ldap_request *ldap_request_send(struct ldap_connection *conn,
                                       struct ldap_message *msg)
 {
        struct ldap_request *req;
@@ -560,6 +604,7 @@ struct ldap_request *ldap_request_send(struct ldap_connection *conn,
        msg->messageid = req->messageid;
 
        if (!ldap_encode(msg, &req->data, req)) {
+               status = NT_STATUS_INTERNAL_ERROR;
                goto failed;            
        }
 
@@ -605,7 +650,7 @@ failed:
   wait for a request to complete
   note that this does not destroy the request
 */
-NTSTATUS ldap_request_wait(struct ldap_request *req)
+_PUBLIC_ NTSTATUS ldap_request_wait(struct ldap_request *req)
 {
        while (req->state < LDAP_REQUEST_DONE) {
                if (event_loop_once(req->conn->event.event_ctx) != 0) {
@@ -651,7 +696,7 @@ static const struct {
        _LDAP_MAP_CODE(LDAP_ALIAS_DEREFERENCING_PROBLEM),
        _LDAP_MAP_CODE(LDAP_INAPPROPRIATE_AUTHENTICATION),
        _LDAP_MAP_CODE(LDAP_INVALID_CREDENTIALS),
-       _LDAP_MAP_CODE(LDAP_INSUFFICIENT_ACCESS_RIGHTs),
+       _LDAP_MAP_CODE(LDAP_INSUFFICIENT_ACCESS_RIGHTS),
        _LDAP_MAP_CODE(LDAP_BUSY),
        _LDAP_MAP_CODE(LDAP_UNAVAILABLE),
        _LDAP_MAP_CODE(LDAP_UNWILLING_TO_PERFORM),
@@ -669,7 +714,7 @@ static const struct {
 /*
   used to setup the status code from a ldap response
 */
-NTSTATUS ldap_check_response(struct ldap_connection *conn, struct ldap_Result *r)
+_PUBLIC_ NTSTATUS ldap_check_response(struct ldap_connection *conn, struct ldap_Result *r)
 {
        int i;
        const char *codename = "unknown";
@@ -702,19 +747,21 @@ NTSTATUS ldap_check_response(struct ldap_connection *conn, struct ldap_Result *r
 /*
   return error string representing the last error
 */
-const char *ldap_errstr(struct ldap_connection *conn, NTSTATUS status)
+_PUBLIC_ const char *ldap_errstr(struct ldap_connection *conn, 
+                       TALLOC_CTX *mem_ctx, 
+                       NTSTATUS status)
 {
        if (NT_STATUS_IS_LDAP(status) && conn->last_error != NULL) {
-               return conn->last_error;
+               return talloc_strdup(mem_ctx, conn->last_error);
        }
-       return nt_errstr(status);
+       return talloc_asprintf(mem_ctx, "LDAP client internal error: %s", nt_errstr(status));
 }
 
 
 /*
   return the Nth result message, waiting if necessary
 */
-NTSTATUS ldap_result_n(struct ldap_request *req, int n, struct ldap_message **msg)
+_PUBLIC_ NTSTATUS ldap_result_n(struct ldap_request *req, int n, struct ldap_message **msg)
 {
        *msg = NULL;
 
@@ -742,7 +789,7 @@ NTSTATUS ldap_result_n(struct ldap_request *req, int n, struct ldap_message **ms
 /*
   return a single result message, checking if it is of the expected LDAP type
 */
-NTSTATUS ldap_result_one(struct ldap_request *req, struct ldap_message **msg, int type)
+_PUBLIC_ NTSTATUS ldap_result_one(struct ldap_request *req, struct ldap_message **msg, int type)
 {
        NTSTATUS status;
        status = ldap_result_n(req, 0, msg);
@@ -760,7 +807,7 @@ NTSTATUS ldap_result_one(struct ldap_request *req, struct ldap_message **msg, in
   a simple ldap transaction, for single result requests that only need a status code
   this relies on single valued requests having the response type == request type + 1
 */
-NTSTATUS ldap_transaction(struct ldap_connection *conn, struct ldap_message *msg)
+_PUBLIC_ NTSTATUS ldap_transaction(struct ldap_connection *conn, struct ldap_message *msg)
 {
        struct ldap_request *req = ldap_request_send(conn, msg);
        struct ldap_message *res;