make smb_signing more generic...
[metze/samba/wip.git] / source / libcli / smb_composite / sesssetup.c
index 6f9e6b0de3c16dfe52c1da3aca801f004ffefba8..4c0cbd327076a9cbedb1cb569d35664b8098b637 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"
@@ -83,6 +85,12 @@ 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;
+
+       if (req->sign_caller_checks) {
+               req->do_not_free = true;
+               check_req = req;
+       }
 
        c->status = smb_raw_sesssetup_recv(req, state, &state->setup);
        state->req = NULL;
@@ -100,6 +108,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 +127,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;
@@ -129,13 +139,14 @@ static void request_handler(struct smbcli_request *req)
        case RAW_SESSSETUP_SPNEGO:
                state->io->out.vuid = state->setup.spnego.out.vuid;
                if (NT_STATUS_EQUAL(c->status, NT_STATUS_LOGON_FAILURE)) {
-                       /* we neet to reset the vuid for a new try */
+                       /* we need to reset the vuid for a new try */
                        session->vuid = 0;
                        if (cli_credentials_wrong_password(state->io->in.credentials)) {
                                nt_status = session_setup_spnego(c, session, 
                                                                      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;
@@ -144,6 +155,7 @@ static void request_handler(struct smbcli_request *req)
                }
                if (!NT_STATUS_EQUAL(c->status, NT_STATUS_MORE_PROCESSING_REQUIRED) && 
                    !NT_STATUS_IS_OK(c->status)) {
+                       talloc_free(check_req);
                        break;
                }
                if (NT_STATUS_EQUAL(state->gensec_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
@@ -161,6 +173,7 @@ static void request_handler(struct smbcli_request *req)
                        c->status = state->gensec_status;
                        if (!NT_STATUS_EQUAL(c->status, NT_STATUS_MORE_PROCESSING_REQUIRED) && 
                            !NT_STATUS_IS_OK(c->status)) {
+                               talloc_free(check_req);
                                break;
                        }
                } else {
@@ -176,11 +189,22 @@ static void request_handler(struct smbcli_request *req)
                }
 
                if (state->setup.spnego.in.secblob.length) {
+                       uint16_t vuid = session->vuid;
+
+                       if (check_req) {
+                               check_req->sign_caller_checks = false;
+                               if (!smbcli_request_check_sign_mac(check_req)) {
+                                       talloc_free(check_req);
+                                       c->status = NT_STATUS_ACCESS_DENIED;
+                                       break;
+                               }
+                               talloc_free(check_req);
+                       }
+
                        /* 
                         * set the session->vuid value only for calling
                         * smb_raw_sesssetup_send()
                         */
-                       uint16_t vuid = session->vuid;
                        session->vuid = state->io->out.vuid;
                        state->req = smb_raw_sesssetup_send(session, &state->setup);
                        session->vuid = vuid;
@@ -194,10 +218,19 @@ 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);
+       }
+
        /* 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 
-                   && session->transport->negotiate.sign_info.mandatory_signing) {
+                   && session->transport->negotiate.sign_info.require_signing) {
+/* TODO: moves this checks*/
                        DEBUG(0, ("SMB signing required, but server does not support it\n"));
                        c->status = NT_STATUS_ACCESS_DENIED;
                }
@@ -222,15 +255,14 @@ static NTSTATUS session_setup_nt1(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, session->transport->socket->hostname, lp_workgroup(global_loadparm));
+       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;
        int flags = CLI_CRED_NTLM_AUTH;
-       if (lp_client_lanman_auth(global_loadparm)) {
+       if (session->options.lanman_auth) {
                flags |= CLI_CRED_LANMAN_AUTH;
        }
 
-       if (lp_client_ntlmv2_auth(global_loadparm)) {
+       if (session->options.ntlmv2_auth) {
                flags |= CLI_CRED_NTLMv2_AUTH;
        }
 
@@ -263,7 +295,8 @@ static NTSTATUS session_setup_nt1(struct composite_context *c,
                set_user_session_key(session, &session_key);
                
                data_blob_free(&session_key);
-       } else if (lp_client_plaintext_auth(global_loadparm)) {
+       } 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 {
@@ -290,14 +323,14 @@ 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, session->transport->socket->hostname, lp_workgroup(global_loadparm));
+       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;
        int flags = 0;
-       if (lp_client_lanman_auth(global_loadparm)) {
+       if (session->options.lanman_auth) {
                flags |= CLI_CRED_LANMAN_AUTH;
        }
 
-       if (lp_client_ntlmv2_auth(global_loadparm)) {
+       if (session->options.ntlmv2_auth) {
                flags |= CLI_CRED_NTLMv2_AUTH;
        }
 
@@ -324,7 +357,7 @@ static NTSTATUS session_setup_old(struct composite_context *c,
                set_user_session_key(session, &session_key);
                
                data_blob_free(&session_key);
-       } else if (lp_client_plaintext_auth(global_loadparm)) {
+       } else if (session->options.plaintext_auth) {
                state->setup.old.in.password = data_blob_talloc(state, password, strlen(password));
        } else {
                /* could match windows client and return 'cannot logon from this workstation', but it just confuses everybody */
@@ -352,6 +385,7 @@ static NTSTATUS session_setup_spnego(struct composite_context *c,
        DATA_BLOB session_key = data_blob(NULL, 0);
        DATA_BLOB null_data_blob = data_blob(NULL, 0);
        const char *chosen_oid = NULL;
+       bool bsrspyl = lp_parm_bool(global_loadparm, NULL, "smbcli", "spnego_start_with_bsrspyl", false);
 
        state->setup.spnego.level           = RAW_SESSSETUP_SPNEGO;
        state->setup.spnego.in.bufsize      = session->transport->options.max_xmit;
@@ -365,7 +399,8 @@ 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);
+       status = gensec_client_start(session, &session->gensec, c->event_ctx,
+                                    global_loadparm);
        if (!NT_STATUS_IS_OK(status)) {
                DEBUG(1, ("Failed to start GENSEC client mode: %s\n", nt_errstr(status)));
                return status;
@@ -375,7 +410,7 @@ static NTSTATUS session_setup_spnego(struct composite_context *c,
 
        status = gensec_set_credentials(session->gensec, io->in.credentials);
        if (!NT_STATUS_IS_OK(status)) {
-               DEBUG(1, ("Failed to start set GENSEC client credentails: %s\n", 
+               DEBUG(1, ("Failed to start set GENSEC client credentials: %s\n", 
                          nt_errstr(status)));
                return status;
        }
@@ -418,7 +453,7 @@ static NTSTATUS session_setup_spnego(struct composite_context *c,
                }
        }
 
-       if (chosen_oid == GENSEC_OID_SPNEGO) {
+       if ((const void *)chosen_oid == (const void *)GENSEC_OID_SPNEGO) {
                status = gensec_update(session->gensec, state,
                                       session->transport->negotiate.secblob,
                                       &state->setup.spnego.in.secblob);
@@ -437,15 +472,46 @@ static NTSTATUS session_setup_spnego(struct composite_context *c,
        }
        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);
+       if (!bsrspyl) {
+               /*
+                * windows servers are happy to accept the first
+                * session setup request to be signed.
+                */
+               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;
        }
+
+       if (bsrspyl) {
+               /*
+                * this code path demonstrates windows client behavior
+                * where the first client request has the BSRSYL signature
+                *
+                * but the reply will be signed by the server with the correct
+                * session key, which means that the seq_num counter has already
+                * started on the server, as if the request would have been signed.
+                */
+               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);
+                       smb_signing_next_seq_num(&session->transport->negotiate.sign_info, false);
+               }
+       }
+
+       if (NT_STATUS_IS_OK(session_key_err)) {
+               /*
+                * as the response might be signed with a session key from within the response
+                * we need to check the signiture of the response ourself.
+                */
+               (*req)->sign_caller_checks = true;
+       }
+
        return (*req)->status;
 }