Fix the build.
[kai/samba.git] / source4 / libcli / smb_composite / sesssetup.c
index 75a2a579a26fd2e3db676ad99012a8e89fdac113..7c9d1fb731c9b0d490da6be84fa09a2eaab9eb51 100644 (file)
 
 #include "includes.h"
 #include "libcli/raw/libcliraw.h"
+#include "libcli/raw/raw_proto.h"
 #include "libcli/composite/composite.h"
 #include "libcli/smb_composite/smb_composite.h"
+#include "libcli/smb_composite/proto.h"
 #include "libcli/auth/libcli_auth.h"
 #include "auth/auth.h"
 #include "auth/gensec/gensec.h"
@@ -33,6 +35,7 @@
 
 struct sesssetup_state {
        union smb_sesssetup setup;
+       NTSTATUS remote_status;
        NTSTATUS gensec_status;
        struct smb_composite_sesssetup *io;
        struct smbcli_request *req;
@@ -83,10 +86,26 @@ static void request_handler(struct smbcli_request *req)
        DATA_BLOB session_key = data_blob(NULL, 0);
        DATA_BLOB null_data_blob = data_blob(NULL, 0);
        NTSTATUS session_key_err, nt_status;
+       struct smbcli_request *check_req = NULL;
 
-       c->status = smb_raw_sesssetup_recv(req, state, &state->setup);
+       if (req->sign_caller_checks) {
+               req->do_not_free = true;
+               check_req = req;
+       }
+
+       state->remote_status = smb_raw_sesssetup_recv(req, state, &state->setup);
+       c->status = state->remote_status;
        state->req = NULL;
 
+       /*
+        * we only need to check the signature if the
+        * NT_STATUS_OK is returned
+        */
+       if (!NT_STATUS_IS_OK(state->remote_status)) {
+               talloc_free(check_req);
+               check_req = NULL;
+       }
+
        switch (state->setup.old.level) {
        case RAW_SESSSETUP_OLD:
                state->io->out.vuid = state->setup.old.out.vuid;
@@ -100,6 +119,7 @@ static void request_handler(struct smbcli_request *req)
                                                              state->io, 
                                                              &state->req);
                                if (NT_STATUS_IS_OK(nt_status)) {
+                                       talloc_free(check_req);
                                        c->status = nt_status;
                                        composite_continue_smb(c, state->req, request_handler, c);
                                        return;
@@ -118,6 +138,7 @@ static void request_handler(struct smbcli_request *req)
                                                              state->io, 
                                                              &state->req);
                                if (NT_STATUS_IS_OK(nt_status)) {
+                                       talloc_free(check_req);
                                        c->status = nt_status;
                                        composite_continue_smb(c, state->req, request_handler, c);
                                        return;
@@ -136,6 +157,7 @@ static void request_handler(struct smbcli_request *req)
                                                                      state->io, 
                                                                      &state->req);
                                if (NT_STATUS_IS_OK(nt_status)) {
+                                       talloc_free(check_req);
                                        c->status = nt_status;
                                        composite_continue_smb(c, state->req, request_handler, c);
                                        return;
@@ -167,12 +189,16 @@ static void request_handler(struct smbcli_request *req)
                        state->setup.spnego.in.secblob = data_blob(NULL, 0);
                }
 
-               /* we need to do another round of session setup. We keep going until both sides
-                  are happy */
-               session_key_err = gensec_session_key(session->gensec, &session_key);
-               if (NT_STATUS_IS_OK(session_key_err)) {
-                       set_user_session_key(session, &session_key);
-                       smbcli_transport_simple_set_signing(session->transport, session_key, null_data_blob);
+               if (NT_STATUS_IS_OK(state->remote_status)) {
+                       if (state->setup.spnego.in.secblob.length) {
+                               c->status = NT_STATUS_INTERNAL_ERROR;
+                               break;
+                       }
+                       session_key_err = gensec_session_key(session->gensec, &session_key);
+                       if (NT_STATUS_IS_OK(session_key_err)) {
+                               set_user_session_key(session, &session_key);
+                               smbcli_transport_simple_set_signing(session->transport, session_key, null_data_blob);
+                       }
                }
 
                if (state->setup.spnego.in.secblob.length) {
@@ -184,6 +210,9 @@ static void request_handler(struct smbcli_request *req)
                        session->vuid = state->io->out.vuid;
                        state->req = smb_raw_sesssetup_send(session, &state->setup);
                        session->vuid = vuid;
+                       if (state->req) {
+                               state->req->sign_caller_checks = true;
+                       }
                        composite_continue_smb(c, state->req, request_handler, c);
                        return;
                }
@@ -194,6 +223,15 @@ static void request_handler(struct smbcli_request *req)
                break;
        }
 
+       if (check_req) {
+               check_req->sign_caller_checks = false;
+               if (!smbcli_request_check_sign_mac(check_req)) {
+                       c->status = NT_STATUS_ACCESS_DENIED;
+               }
+               talloc_free(check_req);
+               check_req = NULL;
+       }
+
        /* enforce the local signing required flag */
        if (NT_STATUS_IS_OK(c->status) && !cli_credentials_is_anonymous(state->io->in.credentials)) {
                if (!session->transport->negotiate.sign_info.doing_signing 
@@ -220,12 +258,14 @@ static NTSTATUS session_setup_nt1(struct composite_context *c,
                                  struct smb_composite_sesssetup *io,
                                  struct smbcli_request **req) 
 {
-       NTSTATUS nt_status;
+       NTSTATUS nt_status = NT_STATUS_INTERNAL_ERROR;
        struct sesssetup_state *state = talloc_get_type(c->private_data, struct sesssetup_state);
-       const char *password = cli_credentials_get_password(io->in.credentials);
-       DATA_BLOB names_blob = NTLMv2_generate_names_blob(state, lp_iconv_convenience(global_loadparm), session->transport->socket->hostname, lp_workgroup(global_loadparm));
-       DATA_BLOB session_key;
+       DATA_BLOB names_blob = NTLMv2_generate_names_blob(state, session->transport->socket->hostname, cli_credentials_get_domain(io->in.credentials));
+       DATA_BLOB session_key = data_blob(NULL, 0);
        int flags = CLI_CRED_NTLM_AUTH;
+
+       smbcli_temp_set_signing(session->transport);
+
        if (session->options.lanman_auth) {
                flags |= CLI_CRED_LANMAN_AUTH;
        }
@@ -257,13 +297,8 @@ static NTSTATUS session_setup_nt1(struct composite_context *c,
                                                              &state->setup.nt1.in.password2,
                                                              NULL, &session_key);
                NT_STATUS_NOT_OK_RETURN(nt_status);
-
-               smbcli_transport_simple_set_signing(session->transport, session_key, 
-                                                   state->setup.nt1.in.password2);
-               set_user_session_key(session, &session_key);
-               
-               data_blob_free(&session_key);
        } else if (session->options.plaintext_auth) {
+               const char *password = cli_credentials_get_password(io->in.credentials);
                state->setup.nt1.in.password1 = data_blob_talloc(state, password, strlen(password));
                state->setup.nt1.in.password2 = data_blob(NULL, 0);
        } else {
@@ -275,6 +310,15 @@ static NTSTATUS session_setup_nt1(struct composite_context *c,
        if (!*req) {
                return NT_STATUS_NO_MEMORY;
        }
+
+       if (NT_STATUS_IS_OK(nt_status)) {
+               smbcli_transport_simple_set_signing(session->transport, session_key, 
+                                                   state->setup.nt1.in.password2);
+               set_user_session_key(session, &session_key);
+               
+               data_blob_free(&session_key);
+       }
+
        return (*req)->status;
 }
 
@@ -290,7 +334,7 @@ static NTSTATUS session_setup_old(struct composite_context *c,
        NTSTATUS nt_status;
        struct sesssetup_state *state = talloc_get_type(c->private_data, struct sesssetup_state);
        const char *password = cli_credentials_get_password(io->in.credentials);
-       DATA_BLOB names_blob = NTLMv2_generate_names_blob(state, lp_iconv_convenience(global_loadparm), session->transport->socket->hostname, lp_workgroup(global_loadparm));
+       DATA_BLOB names_blob = NTLMv2_generate_names_blob(state, session->transport->socket->hostname, cli_credentials_get_domain(io->in.credentials));
        DATA_BLOB session_key;
        int flags = 0;
        if (session->options.lanman_auth) {
@@ -348,9 +392,7 @@ static NTSTATUS session_setup_spnego(struct composite_context *c,
                                     struct smbcli_request **req) 
 {
        struct sesssetup_state *state = talloc_get_type(c->private_data, struct sesssetup_state);
-       NTSTATUS status, session_key_err;
-       DATA_BLOB session_key = data_blob(NULL, 0);
-       DATA_BLOB null_data_blob = data_blob(NULL, 0);
+       NTSTATUS status;
        const char *chosen_oid = NULL;
 
        state->setup.spnego.level           = RAW_SESSSETUP_SPNEGO;
@@ -366,7 +408,7 @@ static NTSTATUS session_setup_spnego(struct composite_context *c,
        smbcli_temp_set_signing(session->transport);
 
        status = gensec_client_start(session, &session->gensec, c->event_ctx,
-                                    global_loadparm);
+                                    io->in.gensec_settings);
        if (!NT_STATUS_IS_OK(status)) {
                DEBUG(1, ("Failed to start GENSEC client mode: %s\n", nt_errstr(status)));
                return status;
@@ -400,12 +442,13 @@ static NTSTATUS session_setup_spnego(struct composite_context *c,
                status = gensec_start_mech_by_oid(session->gensec, chosen_oid);
                if (!NT_STATUS_IS_OK(status)) {
                        DEBUG(1, ("Failed to start set GENSEC client mechanism %s: %s\n",
-                                 gensec_get_name_by_oid(chosen_oid), nt_errstr(status)));
+                                 gensec_get_name_by_oid(session->gensec, chosen_oid), nt_errstr(status)));
                        chosen_oid = GENSEC_OID_NTLMSSP;
                        status = gensec_start_mech_by_oid(session->gensec, chosen_oid);
                        if (!NT_STATUS_IS_OK(status)) {
                                DEBUG(1, ("Failed to start set (fallback) GENSEC client mechanism %s: %s\n",
-                                         gensec_get_name_by_oid(chosen_oid), nt_errstr(status)));
+                                         gensec_get_name_by_oid(session->gensec, chosen_oid), 
+                                         nt_errstr(status)));
                        return status;
                        }
                }
@@ -415,7 +458,7 @@ static NTSTATUS session_setup_spnego(struct composite_context *c,
                status = gensec_start_mech_by_oid(session->gensec, chosen_oid);
                if (!NT_STATUS_IS_OK(status)) {
                        DEBUG(1, ("Failed to start set GENSEC client mechanism %s: %s\n",
-                                 gensec_get_name_by_oid(chosen_oid), nt_errstr(status)));
+                                 gensec_get_name_by_oid(session->gensec, chosen_oid), nt_errstr(status)));
                }
        }
 
@@ -433,20 +476,24 @@ static NTSTATUS session_setup_spnego(struct composite_context *c,
        if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED) && 
            !NT_STATUS_IS_OK(status)) {
                DEBUG(1, ("Failed initial gensec_update with mechanism %s: %s\n",
-                         gensec_get_name_by_oid(chosen_oid), nt_errstr(status)));
+                         gensec_get_name_by_oid(session->gensec, chosen_oid), 
+                         nt_errstr(status)));
                return status;
        }
        state->gensec_status = status;
 
-       session_key_err = gensec_session_key(session->gensec, &session_key);
-       if (NT_STATUS_IS_OK(session_key_err)) {
-               smbcli_transport_simple_set_signing(session->transport, session_key, null_data_blob);
-       }
-
        *req = smb_raw_sesssetup_send(session, &state->setup);
        if (!*req) {
                return NT_STATUS_NO_MEMORY;
        }
+
+       /*
+        * we need to check the signature ourself
+        * as the session key might be the acceptor subkey
+        * which comes within the response itself
+        */
+       (*req)->sign_caller_checks = true;
+
        return (*req)->status;
 }