s4:ldap_server: remove an useless indentation level from gensec_update_ev()
[metze/samba/wip.git] / source4 / ldap_server / ldap_bind.c
index 986ecbfcebb847b7c637f8612c21c9eea86c71ce..fb4593de95f4243c236473eb6f0bcb38fd30b3ca 100644 (file)
@@ -23,6 +23,7 @@
 #include "smbd/service.h"
 #include <ldb.h>
 #include <ldb_errors.h>
+#include "../lib/util/dlinklist.h"
 #include "dsdb/samdb/samdb.h"
 #include "auth/gensec/gensec.h"
 #include "auth/gensec/gensec_tstream.h"
@@ -59,21 +60,107 @@ static char *ldapsrv_bind_error_msg(TALLOC_CTX *mem_ctx,
        return msg;
 }
 
+struct ldapsrv_bind_wait_context {
+       struct ldapsrv_reply *reply;
+       struct tevent_req *req;
+       NTSTATUS status;
+       bool done;
+};
 
-static NTSTATUS ldapsrv_BindSimple(struct ldapsrv_call *call)
+struct ldapsrv_bind_wait_state {
+       uint8_t dummy;
+};
+
+static struct tevent_req *ldapsrv_bind_wait_send(TALLOC_CTX *mem_ctx,
+                                                struct tevent_context *ev,
+                                                void *private_data)
 {
-       struct ldap_BindRequest *req = &call->request->r.BindRequest;
-       struct ldapsrv_reply *reply;
-       struct ldap_BindResponse *resp;
+       struct ldapsrv_bind_wait_context *bind_wait =
+               talloc_get_type_abort(private_data,
+               struct ldapsrv_bind_wait_context);
+       struct tevent_req *req;
+       struct ldapsrv_bind_wait_state *state;
 
-       int result;
-       const char *errstr;
+       req = tevent_req_create(mem_ctx, &state,
+                               struct ldapsrv_bind_wait_state);
+       if (req == NULL) {
+               return NULL;
+       }
+       bind_wait->req = req;
 
-       struct auth_session_info *session_info;
+       tevent_req_defer_callback(req, ev);
 
-       NTSTATUS status;
+       if (!bind_wait->done) {
+               return req;
+       }
+
+       if (tevent_req_nterror(req, bind_wait->status)) {
+               return tevent_req_post(req, ev);
+       }
+
+       tevent_req_done(req);
+       return tevent_req_post(req, ev);
+}
+
+static NTSTATUS ldapsrv_bind_wait_recv(struct tevent_req *req)
+{
+       return tevent_req_simple_recv_ntstatus(req);
+}
+
+static NTSTATUS ldapsrv_bind_wait_setup(struct ldapsrv_call *call,
+                                       struct ldapsrv_reply *reply)
+{
+       struct ldapsrv_bind_wait_context *bind_wait = NULL;
+
+       if (call->wait_private != NULL) {
+               return NT_STATUS_INTERNAL_ERROR;
+       }
+
+       bind_wait = talloc_zero(call, struct ldapsrv_bind_wait_context);
+       if (bind_wait == NULL) {
+               return NT_STATUS_NO_MEMORY;
+       }
+       bind_wait->reply = reply;
+
+       call->wait_private = bind_wait;
+       call->wait_send = ldapsrv_bind_wait_send;
+       call->wait_recv = ldapsrv_bind_wait_recv;
+       return NT_STATUS_OK;
+}
+
+static void ldapsrv_bind_wait_finished(struct ldapsrv_call *call,
+                                      NTSTATUS status)
+{
+       struct ldapsrv_bind_wait_context *bind_wait =
+               talloc_get_type_abort(call->wait_private,
+               struct ldapsrv_bind_wait_context);
+
+       bind_wait->done = true;
+       bind_wait->status = status;
+
+       if (bind_wait->req == NULL) {
+               return;
+       }
+
+       if (tevent_req_nterror(bind_wait->req, status)) {
+               return;
+       }
+
+       tevent_req_done(bind_wait->req);
+}
 
+static void ldapsrv_BindSimple_done(struct tevent_req *subreq);
+
+static NTSTATUS ldapsrv_BindSimple(struct ldapsrv_call *call)
+{
+       struct ldap_BindRequest *req = &call->request->r.BindRequest;
+       struct ldapsrv_reply *reply = NULL;
+       struct ldap_BindResponse *resp = NULL;
+       int result;
+       const char *errstr = NULL;
+       NTSTATUS status;
        bool using_tls = call->conn->sockets.active == call->conn->sockets.tls;
+       struct tevent_req *subreq = NULL;
 
        DEBUG(10, ("BindSimple dn: %s\n",req->dn));
 
@@ -94,17 +181,61 @@ static NTSTATUS ldapsrv_BindSimple(struct ldapsrv_call *call)
                goto do_reply;
        }
 
-       status = authenticate_ldap_simple_bind(call,
-                                              call->conn->connection->event.ctx,
-                                              call->conn->connection->msg_ctx,
-                                              call->conn->lp_ctx,
-                                              call->conn->connection->remote_address,
-                                              call->conn->connection->local_address,
-                                              using_tls,
-                                              req->dn,
-                                              req->creds.password,
-                                              &session_info);
+       subreq = authenticate_ldap_simple_bind_send(call,
+                                       call->conn->connection->event.ctx,
+                                       call->conn->connection->msg_ctx,
+                                       call->conn->lp_ctx,
+                                       call->conn->connection->remote_address,
+                                       call->conn->connection->local_address,
+                                       using_tls,
+                                       req->dn,
+                                       req->creds.password);
+       if (subreq == NULL) {
+               return NT_STATUS_NO_MEMORY;
+       }
+       tevent_req_set_callback(subreq, ldapsrv_BindSimple_done, call);
 
+       status = ldapsrv_bind_wait_setup(call, reply);
+       if (!NT_STATUS_IS_OK(status)) {
+               TALLOC_FREE(subreq);
+               return status;
+       }
+
+       /*
+        * The rest will be async.
+        */
+       return NT_STATUS_OK;
+
+do_reply:
+       resp = &reply->msg->r.BindResponse;
+       resp->response.resultcode = result;
+       resp->response.errormessage = errstr;
+       resp->response.dn = NULL;
+       resp->response.referral = NULL;
+       resp->SASL.secblob = NULL;
+
+       ldapsrv_queue_reply(call, reply);
+       return NT_STATUS_OK;
+}
+
+static void ldapsrv_BindSimple_done(struct tevent_req *subreq)
+{
+       struct ldapsrv_call *call =
+               tevent_req_callback_data(subreq,
+               struct ldapsrv_call);
+       struct ldapsrv_bind_wait_context *bind_wait =
+               talloc_get_type_abort(call->wait_private,
+               struct ldapsrv_bind_wait_context);
+       struct ldapsrv_reply *reply = bind_wait->reply;
+       struct auth_session_info *session_info = NULL;
+       NTSTATUS status;
+       struct ldap_BindResponse *resp = NULL;
+       int result;
+       const char *errstr = NULL;
+
+       status = authenticate_ldap_simple_bind_recv(subreq,
+                                                   call,
+                                                   &session_info);
        if (NT_STATUS_IS_OK(status)) {
                result = LDAP_SUCCESS;
                errstr = NULL;
@@ -131,7 +262,6 @@ static NTSTATUS ldapsrv_BindSimple(struct ldapsrv_call *call)
                                                0x0C0903A9, status);
        }
 
-do_reply:
        resp = &reply->msg->r.BindResponse;
        resp->response.resultcode = result;
        resp->response.errormessage = errstr;
@@ -140,7 +270,7 @@ do_reply:
        resp->SASL.secblob = NULL;
 
        ldapsrv_queue_reply(call, reply);
-       return NT_STATUS_OK;
+       ldapsrv_bind_wait_finished(call, NT_STATUS_OK);
 }
 
 struct ldapsrv_sasl_postprocess_context {
@@ -244,6 +374,8 @@ static NTSTATUS ldapsrv_BindSASL(struct ldapsrv_call *call)
        int result = 0;
        const char *errstr=NULL;
        NTSTATUS status = NT_STATUS_OK;
+       DATA_BLOB input = data_blob_null;
+       DATA_BLOB output = data_blob_null;
 
        DEBUG(10, ("BindSASL dn: %s\n",req->dn));
 
@@ -252,7 +384,12 @@ static NTSTATUS ldapsrv_BindSASL(struct ldapsrv_call *call)
                return NT_STATUS_NO_MEMORY;
        }
        resp = &reply->msg->r.BindResponse;
-       
+       /* Windows 2000 mmc doesn't like secblob == NULL and reports a decoding error */
+       resp->SASL.secblob = talloc_zero(reply, DATA_BLOB);
+       if (resp->SASL.secblob == NULL) {
+               return NT_STATUS_NO_MEMORY;
+       }
+
        conn = call->conn;
 
        /* 
@@ -271,31 +408,22 @@ static NTSTATUS ldapsrv_BindSASL(struct ldapsrv_call *call)
                        result = LDAP_OPERATIONS_ERROR;
                        errstr = talloc_asprintf(reply, "SASL: Failed to start authentication system: %s", 
                                                 nt_errstr(status));
+                       goto do_reply;
                }
        }
 
-       if (NT_STATUS_IS_OK(status)) {
-               DATA_BLOB input = data_blob(NULL, 0);
-               DATA_BLOB output = data_blob(NULL, 0);
-
-               if (req->creds.SASL.secblob) {
-                       input = *req->creds.SASL.secblob;
-               }
-
-               status = gensec_update_ev(conn->gensec, reply, conn->connection->event.ctx,
-                                         input, &output);
-
-               /* Windows 2000 mmc doesn't like secblob == NULL and reports a decoding error */
-               resp->SASL.secblob = talloc(reply, DATA_BLOB);
-               NT_STATUS_HAVE_NO_MEMORY(resp->SASL.secblob);
-               *resp->SASL.secblob = output;
-       } else {
-               resp->SASL.secblob = NULL;
+       if (req->creds.SASL.secblob) {
+               input = *req->creds.SASL.secblob;
        }
 
+       status = gensec_update_ev(conn->gensec, reply, conn->connection->event.ctx,
+                                 input, &output);
+       *resp->SASL.secblob = output;
+
        if (NT_STATUS_EQUAL(NT_STATUS_MORE_PROCESSING_REQUIRED, status)) {
                result = LDAP_SASL_BIND_IN_PROGRESS;
                errstr = NULL;
+               goto do_reply;
        } else if (NT_STATUS_IS_OK(status)) {
                struct ldapsrv_sasl_postprocess_context *context = NULL;
 
@@ -319,6 +447,7 @@ static NTSTATUS ldapsrv_BindSASL(struct ldapsrv_call *call)
                        errstr = talloc_asprintf(reply,
                                                 "SASL:[%s]: Sign or Seal are not allowed if TLS is used",
                                                 req->creds.SASL.mechanism);
+                       goto do_reply;
                }
 
                if (context && conn->sockets.sasl) {
@@ -328,6 +457,7 @@ static NTSTATUS ldapsrv_BindSASL(struct ldapsrv_call *call)
                        errstr = talloc_asprintf(reply,
                                                 "SASL:[%s]: Sign or Seal are not allowed if SASL encryption has already been set up",
                                                 req->creds.SASL.mechanism);
+                       goto do_reply;
                }
 
                if (context) {
@@ -354,14 +484,15 @@ static NTSTATUS ldapsrv_BindSASL(struct ldapsrv_call *call)
                                errstr = talloc_asprintf(reply,
                                                "SASL:[%s]: not allowed if TLS is used.",
                                                 req->creds.SASL.mechanism);
-                               break;
+                               goto do_reply;
+
                        case LDAP_SERVER_REQUIRE_STRONG_AUTH_YES:
                                status = NT_STATUS_NETWORK_ACCESS_DENIED;
                                result = LDAP_STRONG_AUTH_REQUIRED;
                                errstr = talloc_asprintf(reply,
                                                 "SASL:[%s]: Sign or Seal are required.",
                                                 req->creds.SASL.mechanism);
-                               break;
+                               goto do_reply;
                        }
                }
 
@@ -371,6 +502,7 @@ static NTSTATUS ldapsrv_BindSASL(struct ldapsrv_call *call)
                        errstr = talloc_asprintf(reply, 
                                                 "SASL:[%s]: Failed to setup SASL socket: %s", 
                                                 req->creds.SASL.mechanism, nt_errstr(status));
+                       goto do_reply;
                } else {
                        struct auth_session_info *old_session_info=NULL;
 
@@ -383,6 +515,7 @@ static NTSTATUS ldapsrv_BindSASL(struct ldapsrv_call *call)
                                errstr = talloc_asprintf(reply, 
                                                         "SASL:[%s]: Failed to get session info: %s", 
                                                         req->creds.SASL.mechanism, nt_errstr(status));
+                               goto do_reply;
                        } else {
                                talloc_unlink(conn, old_session_info);
                                
@@ -399,6 +532,7 @@ static NTSTATUS ldapsrv_BindSASL(struct ldapsrv_call *call)
                                                                 "SASL:[%s]: Failed to advise samdb of new credentials: %s", 
                                                                 req->creds.SASL.mechanism, 
                                                                 nt_errstr(status));
+                                       goto do_reply;
                                }
                        }
                }
@@ -419,8 +553,10 @@ static NTSTATUS ldapsrv_BindSASL(struct ldapsrv_call *call)
                }
                talloc_unlink(conn, conn->gensec);
                conn->gensec = NULL;
+               goto do_reply;
        }
 
+do_reply:
        resp->response.resultcode = result;
        resp->response.dn = NULL;
        resp->response.errormessage = errstr;
@@ -481,8 +617,73 @@ NTSTATUS ldapsrv_BindRequest(struct ldapsrv_call *call)
        return NT_STATUS_OK;
 }
 
+struct ldapsrv_unbind_wait_context {
+       uint8_t dummy;
+};
+
+struct ldapsrv_unbind_wait_state {
+       uint8_t dummy;
+};
+
+static struct tevent_req *ldapsrv_unbind_wait_send(TALLOC_CTX *mem_ctx,
+                                                struct tevent_context *ev,
+                                                void *private_data)
+{
+       struct ldapsrv_unbind_wait_context *unbind_wait =
+               talloc_get_type_abort(private_data,
+               struct ldapsrv_unbind_wait_context);
+       struct tevent_req *req;
+       struct ldapsrv_unbind_wait_state *state;
+
+       req = tevent_req_create(mem_ctx, &state,
+                               struct ldapsrv_unbind_wait_state);
+       if (req == NULL) {
+               return NULL;
+       }
+
+       (void)unbind_wait;
+
+       tevent_req_nterror(req, NT_STATUS_LOCAL_DISCONNECT);
+       return tevent_req_post(req, ev);
+}
+
+static NTSTATUS ldapsrv_unbind_wait_recv(struct tevent_req *req)
+{
+       return tevent_req_simple_recv_ntstatus(req);
+}
+
+static NTSTATUS ldapsrv_unbind_wait_setup(struct ldapsrv_call *call)
+{
+       struct ldapsrv_unbind_wait_context *unbind_wait = NULL;
+
+       if (call->wait_private != NULL) {
+               return NT_STATUS_INTERNAL_ERROR;
+       }
+
+       unbind_wait = talloc_zero(call, struct ldapsrv_unbind_wait_context);
+       if (unbind_wait == NULL) {
+               return NT_STATUS_NO_MEMORY;
+       }
+
+       call->wait_private = unbind_wait;
+       call->wait_send = ldapsrv_unbind_wait_send;
+       call->wait_recv = ldapsrv_unbind_wait_recv;
+       return NT_STATUS_OK;
+}
+
 NTSTATUS ldapsrv_UnbindRequest(struct ldapsrv_call *call)
 {
+       struct ldapsrv_call *c = NULL;
+       struct ldapsrv_call *n = NULL;
+
        DEBUG(10, ("UnbindRequest\n"));
-       return NT_STATUS_OK;
+
+       for (c = call->conn->pending_calls; c != NULL; c = n) {
+               n = c->next;
+
+               DLIST_REMOVE(call->conn->pending_calls, c);
+               TALLOC_FREE(c);
+       }
+
+       return ldapsrv_unbind_wait_setup(call);
 }