r11692: added a full composite (async) spnego session setup for SMB2. This
authorAndrew Tridgell <tridge@samba.org>
Sat, 12 Nov 2005 01:08:43 +0000 (01:08 +0000)
committerGerald (Jerry) Carter <jerry@samba.org>
Wed, 10 Oct 2007 18:46:14 +0000 (13:46 -0500)
simplies the torture code a lot.
(This used to be commit 7bf1046fbb7fd83fecb2fa645628ba9a17aab037)

source4/libcli/smb2/request.c
source4/libcli/smb2/session.c
source4/libcli/smb2/smb2.h
source4/libcli/smb_composite/sesssetup.c
source4/torture/smb2/connect.c

index 108cf0ca554c3eb265f6b1db255a34f57af2e8d9..ccffef9b8814af1fe3a865765f53349a3b072412 100644 (file)
@@ -39,6 +39,8 @@ struct smb2_request *smb2_request_init(struct smb2_transport *transport,
 
        req->state     = SMB2_REQUEST_INIT;
        req->transport = transport;
+       req->session   = NULL;
+       req->tree      = NULL;
        req->seqnum    = transport->seqnum++;
        req->status    = NT_STATUS_OK;
        req->async.fn  = NULL;
@@ -88,6 +90,8 @@ struct smb2_request *smb2_request_init_tree(struct smb2_tree *tree,
 
        SBVAL(req->out.hdr,  SMB2_HDR_UID, tree->session->uid);
        SIVAL(req->out.hdr,  SMB2_HDR_TID, tree->tid);
+       req->session = tree->session;
+       req->tree = tree;
 
        return req;     
 }
index 9d945243d289e7f40aa35123bbb7b261dc447822..baa706cf8b8357bfd5aad4e21d2b5f1e8760a9cb 100644 (file)
@@ -24,6 +24,8 @@
 #include "libcli/raw/libcliraw.h"
 #include "libcli/smb2/smb2.h"
 #include "libcli/smb2/smb2_calls.h"
+#include "libcli/composite/composite.h"
+#include "auth/gensec/gensec.h"
 
 /*
   initialise a smb2_session structure
@@ -73,6 +75,8 @@ struct smb2_request *smb2_session_setup_send(struct smb2_session *session,
        SIVAL(req->out.body, 0x04, io->in.unknown2);
        SIVAL(req->out.body, 0x08, io->in.unknown3);
        
+       req->session = session;
+       
        status = smb2_push_ofs_blob(req, req->out.body+0x0C, io->in.secblob);
        if (!NT_STATUS_IS_OK(status)) {
                talloc_free(req);
@@ -127,3 +131,145 @@ NTSTATUS smb2_session_setup(struct smb2_session *session,
        struct smb2_request *req = smb2_session_setup_send(session, io);
        return smb2_session_setup_recv(req, mem_ctx, io);
 }
+
+
+struct smb2_session_state {
+       struct smb2_session_setup io;
+       struct smb2_request *req;
+       NTSTATUS gensec_status;
+};
+
+/*
+  handle continuations of the spnego session setup
+*/
+static void session_request_handler(struct smb2_request *req)
+{
+       struct composite_context *c = talloc_get_type(req->async.private, 
+                                                     struct composite_context);
+       struct smb2_session_state *state = talloc_get_type(c->private_data, 
+                                                          struct smb2_session_state);
+       struct smb2_session *session = req->session;
+
+       c->status = smb2_session_setup_recv(req, c, &state->io);
+       if (NT_STATUS_EQUAL(c->status, NT_STATUS_MORE_PROCESSING_REQUIRED) ||
+           (NT_STATUS_IS_OK(c->status) && 
+            NT_STATUS_EQUAL(state->gensec_status, NT_STATUS_MORE_PROCESSING_REQUIRED))) {
+               c->status = gensec_update(req->session->gensec, c, 
+                                         state->io.out.secblob,
+                                         &state->io.in.secblob);
+               state->gensec_status = c->status;
+       }
+
+       session->uid = state->io.out.uid;
+
+       if (NT_STATUS_EQUAL(c->status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
+               state->req = smb2_session_setup_send(session, &state->io);
+               if (state->req == NULL) {
+                       composite_error(c, NT_STATUS_NO_MEMORY);
+               }
+
+               state->req->async.fn = session_request_handler;
+               state->req->async.private = c;
+               return;
+       }
+
+       if (!NT_STATUS_IS_OK(c->status)) {
+               composite_error(c, c->status);
+               return;
+       }
+
+       composite_done(c);
+}
+
+/*
+  a composite function that does a full SPNEGO session setup
+ */
+struct composite_context *smb2_session_setup_spnego_send(struct smb2_session *session, 
+                                                        struct cli_credentials *credentials)
+{
+       struct composite_context *c;
+       struct smb2_session_state *state;
+
+       c = talloc_zero(session, struct composite_context);
+       if (c == NULL) return NULL;
+
+       state = talloc(c, struct smb2_session_state);
+       if (state == NULL) {
+               c->status = NT_STATUS_NO_MEMORY;
+               goto failed;
+       }
+
+       c->state = COMPOSITE_STATE_IN_PROGRESS;
+       c->private_data = state;
+       c->event_ctx = session->transport->socket->event.ctx;
+
+       ZERO_STRUCT(state->io);
+       state->io.in.unknown1 = 0x11;
+       state->io.in.unknown2 = 0xF;
+       state->io.in.unknown3 = 0x00;
+
+       c->status = gensec_set_credentials(session->gensec, credentials);
+       if (!NT_STATUS_IS_OK(c->status)) {
+               goto failed;
+       }
+
+       c->status = gensec_set_target_hostname(session->gensec, 
+                                              session->transport->socket->hostname);
+       if (!NT_STATUS_IS_OK(c->status)) {
+               goto failed;
+       }
+
+       c->status = gensec_set_target_service(session->gensec, "cifs");
+       if (!NT_STATUS_IS_OK(c->status)) {
+               goto failed;
+       }
+
+       c->status = gensec_start_mech_by_oid(session->gensec, GENSEC_OID_SPNEGO);
+       if (!NT_STATUS_IS_OK(c->status)) {
+               goto failed;
+       }
+
+       c->status = gensec_update(session->gensec, c, 
+                                 session->transport->negotiate.secblob,
+                                 &state->io.in.secblob);
+       if (!NT_STATUS_EQUAL(c->status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
+               goto failed;
+       }
+       state->gensec_status = c->status;
+               
+       state->req = smb2_session_setup_send(session, &state->io);
+       if (state->req == NULL) {
+               c->status = NT_STATUS_NO_MEMORY;
+               goto failed;
+       }
+
+       state->req->async.fn = session_request_handler;
+       state->req->async.private = c;
+
+       return c;
+
+failed:
+       composite_trigger_error(c);
+       return c;
+}
+
+/*
+  receive a composite session setup reply
+*/
+NTSTATUS smb2_session_setup_spnego_recv(struct composite_context *c)
+{
+       NTSTATUS status;
+       status = composite_wait(c);
+       talloc_free(c);
+       return status;
+}
+
+/*
+  sync version of smb2_session_setup_spnego
+*/
+NTSTATUS smb2_session_setup_spnego(struct smb2_session *session, 
+                                  struct cli_credentials *credentials)
+{
+       struct composite_context *c = smb2_session_setup_spnego_send(session, credentials);
+       return smb2_session_setup_spnego_recv(c);
+}
index f6847bfc9b42f7b23e2df5ba2a3c732d1efb92d6..f452852cf93de76dc41032a529ecb5dc0aad1334 100644 (file)
@@ -114,6 +114,8 @@ struct smb2_request {
        enum smb2_request_state state;
        
        struct smb2_transport *transport;
+       struct smb2_session   *session;
+       struct smb2_tree      *tree;
 
        uint64_t seqnum;
 
index 0d0904b9698ad3846535c99bbdf5f57e0f5708bd..4cdc4c5e6b9b1d17ffc1581f472e587d35465f77 100644 (file)
@@ -370,8 +370,8 @@ struct composite_context *smb_composite_sesssetup_send(struct smbcli_session *se
 
        state = talloc(c, struct sesssetup_state);
        if (state == NULL) {
-               c->state = COMPOSITE_STATE_ERROR;
-               c->status = NT_STATUS_NO_MEMORY;
+               talloc_free(c);
+               return NULL;
        }
 
        state->io = io;
index 2af6bfb5763958c94365c26a790a6bfaa6e20076..e88db8ac5bff3358aaa9af02163ed732bc187d5f 100644 (file)
@@ -27,7 +27,6 @@
 #include "librpc/gen_ndr/ndr_security.h"
 #include "lib/cmdline/popt_common.h"
 #include "lib/events/events.h"
-#include "auth/gensec/gensec.h"
 
 #define BASEDIR "\\testsmb2"
 
@@ -88,77 +87,16 @@ static struct smb2_session *torture_smb2_session(struct smb2_transport *transpor
                                                 struct cli_credentials *credentials)
 {
        struct smb2_session *session;
-       struct smb2_session_setup io;
        NTSTATUS status;
-       TALLOC_CTX *tmp_ctx = talloc_new(transport);
-       DATA_BLOB secblob;
-
-       ZERO_STRUCT(io);
-       io.in.unknown1 = 0x11;
-       io.in.unknown2 = 0xF;
-       io.in.unknown3 = 0x00;
-
+       
        session = smb2_session_init(transport, transport, True);
 
-       status = gensec_set_credentials(session->gensec, credentials);
-       if (!NT_STATUS_IS_OK(status)) {
-               DEBUG(1, ("Failed to start set GENSEC client credentails: %s\n", 
-                         nt_errstr(status)));
-               return NULL;
-       }
-
-       status = gensec_set_target_hostname(session->gensec, transport->socket->hostname);
-       if (!NT_STATUS_IS_OK(status)) {
-               DEBUG(1, ("Failed to start set GENSEC target hostname: %s\n", 
-                         nt_errstr(status)));
-               return NULL;
-       }
-
-       status = gensec_set_target_service(session->gensec, "cifs");
-       if (!NT_STATUS_IS_OK(status)) {
-               DEBUG(1, ("Failed to start set GENSEC target service: %s\n", 
-                         nt_errstr(status)));
-               return NULL;
-       }
-
-       status = gensec_start_mech_by_oid(session->gensec, GENSEC_OID_SPNEGO);
+       status = smb2_session_setup_spnego(session, credentials);
        if (!NT_STATUS_IS_OK(status)) {
-               DEBUG(1, ("Failed to start set GENSEC client - %s\n",
-                         nt_errstr(status)));
+               printf("Session setup failed - %s\n", nt_errstr(status));
                return NULL;
        }
-
-       secblob = session->transport->negotiate.secblob;
-
-       do {
-               NTSTATUS status1;
-
-               status1 = gensec_update(session->gensec, tmp_ctx, secblob, &io.in.secblob);
-               if (!NT_STATUS_EQUAL(status1, NT_STATUS_MORE_PROCESSING_REQUIRED) && 
-                   !NT_STATUS_IS_OK(status1)) {
-                       DEBUG(1, ("Failed initial gensec_update : %s\n", 
-                                 nt_errstr(status1)));
-                       status = status1;
-                       break;
-               }
-               
-               status = smb2_session_setup(session, tmp_ctx, &io);
-               secblob = io.out.secblob;
-
-               session->uid = io.out.uid;
-
-               if (NT_STATUS_IS_OK(status) && 
-                   NT_STATUS_EQUAL(status1, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
-                       status = gensec_update(session->gensec, tmp_ctx, secblob, 
-                                              &io.in.secblob);
-               }
-       } while (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED));
-
-       if (!NT_STATUS_IS_OK(status)) {
-               printf("session setup failed - %s\n", nt_errstr(status));
-               return NULL;
-       }
-
+       
        printf("Session setup gave UID 0x%016llx\n", session->uid);
 
        return session;
@@ -292,8 +230,14 @@ BOOL torture_smb2_connect(void)
        struct smb2_handle h1, h2;
 
        transport = torture_smb2_negprot(mem_ctx, host);
+       if (transport == NULL) return False;
+
        session   = torture_smb2_session(transport, credentials);
+       if (session == NULL) return False;
+
        tree      = torture_smb2_tree(session, share);
+       if (tree == NULL) return False;
+
        h1        = torture_smb2_create(tree, "test9.dat");
        h2        = torture_smb2_create(tree, "test9.dat");
        torture_smb2_close(tree, h1);