s4-drs: implement more of DsUpdateRefs
[nivanova/samba-autobuild/.git] / source4 / rpc_server / dcesrv_auth.c
index 0843a4376151d84433f615940aa509d31208e2d0..3499ca2187725cfc43132bd9bb6aab75a33390b0 100644 (file)
 
 #include "includes.h"
 #include "rpc_server/dcerpc_server.h"
+#include "rpc_server/dcerpc_server_proto.h"
+#include "librpc/rpc/dcerpc_proto.h"
 #include "librpc/gen_ndr/ndr_dcerpc.h"
 #include "auth/credentials/credentials.h"
 #include "auth/gensec/gensec.h"
+#include "auth/auth.h"
 #include "param/param.h"
 
 /*
   parse any auth information from a dcerpc bind request
-  return False if we can't handle the auth request for some 
+  return false if we can't handle the auth request for some 
   reason (in which case we send a bind_nak)
 */
-BOOL dcesrv_auth_bind(struct dcesrv_call_state *call)
+bool dcesrv_auth_bind(struct dcesrv_call_state *call)
 {
        struct cli_credentials *server_credentials;
        struct ncacn_packet *pkt = &call->pkt;
        struct dcesrv_connection *dce_conn = call->conn;
        struct dcesrv_auth *auth = &dce_conn->auth_state;
        NTSTATUS status;
+       enum ndr_err_code ndr_err;
 
        if (pkt->u.bind.auth_info.length == 0) {
                dce_conn->auth_state.auth_info = NULL;
-               return True;
+               return true;
        }
 
        dce_conn->auth_state.auth_info = talloc(dce_conn, struct dcerpc_auth);
        if (!dce_conn->auth_state.auth_info) {
-               return False;
+               return false;
        }
 
-       status = ndr_pull_struct_blob(&pkt->u.bind.auth_info,
-                                     call,
-                                     dce_conn->auth_state.auth_info,
-                                     (ndr_pull_flags_fn_t)ndr_pull_dcerpc_auth);
-       if (!NT_STATUS_IS_OK(status)) {
-               return False;
-       }
-
-       status = gensec_server_start(dce_conn, call->event_ctx, call->msg_ctx, &auth->gensec_security);
-       if (!NT_STATUS_IS_OK(status)) {
-               DEBUG(1, ("Failed to start GENSEC for DCERPC server: %s\n", nt_errstr(status)));
-               return False;
+       ndr_err = ndr_pull_struct_blob(&pkt->u.bind.auth_info,
+                                      call, NULL,
+                                      dce_conn->auth_state.auth_info,
+                                      (ndr_pull_flags_fn_t)ndr_pull_dcerpc_auth);
+       if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
+               return false;
        }
 
        server_credentials 
                = cli_credentials_init(call);
        if (!server_credentials) {
                DEBUG(1, ("Failed to init server credentials\n"));
-               return False;
+               return false;
        }
        
-       cli_credentials_set_conf(server_credentials, global_loadparm);
-       status = cli_credentials_set_machine_account(server_credentials);
+       cli_credentials_set_conf(server_credentials, call->conn->dce_ctx->lp_ctx);
+       status = cli_credentials_set_machine_account(server_credentials, call->conn->dce_ctx->lp_ctx);
        if (!NT_STATUS_IS_OK(status)) {
                DEBUG(10, ("Failed to obtain server credentials, perhaps a standalone server?: %s\n", nt_errstr(status)));
                talloc_free(server_credentials);
                server_credentials = NULL;
        }
 
-       gensec_set_credentials(auth->gensec_security, server_credentials);
+       status = samba_server_gensec_start(dce_conn, call->event_ctx, 
+                                          call->msg_ctx,
+                                          call->conn->dce_ctx->lp_ctx, 
+                                          server_credentials,
+                                          NULL,
+                                          &auth->gensec_security);
 
        status = gensec_start_mech_by_authtype(auth->gensec_security, auth->auth_info->auth_type, 
                                               auth->auth_info->auth_level);
@@ -89,10 +92,14 @@ BOOL dcesrv_auth_bind(struct dcesrv_call_state *call)
                          (int)auth->auth_info->auth_type,
                          (int)auth->auth_info->auth_level,
                          nt_errstr(status)));
-               return False;
+               return false;
+       }
+
+       if (call->conn->state_flags & DCESRV_CALL_STATE_FLAG_HEADER_SIGNING) {
+               gensec_want_feature(auth->gensec_security, GENSEC_FEATURE_SIGN_PKT_HEADER);
        }
 
-       return True;
+       return true;
 }
 
 /*
@@ -121,6 +128,11 @@ NTSTATUS dcesrv_auth_bind_ack(struct dcesrv_call_state *call, struct ncacn_packe
                        return status;
                }
 
+               if (dce_conn->state_flags & DCESRV_CALL_STATE_FLAG_HEADER_SIGNING) {
+                       gensec_want_feature(dce_conn->auth_state.gensec_security,
+                                           GENSEC_FEATURE_SIGN_PKT_HEADER);
+               }
+
                /* Now that we are authenticated, go back to the generic session key... */
                dce_conn->auth_state.session_key = dcesrv_generic_session_key;
                return NT_STATUS_OK;
@@ -138,25 +150,26 @@ NTSTATUS dcesrv_auth_bind_ack(struct dcesrv_call_state *call, struct ncacn_packe
 /*
   process the final stage of a auth request
 */
-BOOL dcesrv_auth_auth3(struct dcesrv_call_state *call)
+bool dcesrv_auth_auth3(struct dcesrv_call_state *call)
 {
        struct ncacn_packet *pkt = &call->pkt;
        struct dcesrv_connection *dce_conn = call->conn;
        NTSTATUS status;
+       enum ndr_err_code ndr_err;
 
        /* We can't work without an existing gensec state, and an new blob to feed it */
        if (!dce_conn->auth_state.auth_info ||
            !dce_conn->auth_state.gensec_security ||
            pkt->u.auth3.auth_info.length == 0) {
-               return False;
+               return false;
        }
 
-       status = ndr_pull_struct_blob(&pkt->u.auth3.auth_info,
-                                     call,
-                                     dce_conn->auth_state.auth_info,
-                                     (ndr_pull_flags_fn_t)ndr_pull_dcerpc_auth);
-       if (!NT_STATUS_IS_OK(status)) {
-               return False;
+       ndr_err = ndr_pull_struct_blob(&pkt->u.auth3.auth_info,
+                                      call, NULL,
+                                      dce_conn->auth_state.auth_info,
+                                      (ndr_pull_flags_fn_t)ndr_pull_dcerpc_auth);
+       if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
+               return false;
        }
 
        /* Pass the extra data we got from the client down to gensec for processing */
@@ -169,55 +182,55 @@ BOOL dcesrv_auth_auth3(struct dcesrv_call_state *call)
                                             &dce_conn->auth_state.session_info);
                if (!NT_STATUS_IS_OK(status)) {
                        DEBUG(1, ("Failed to establish session_info: %s\n", nt_errstr(status)));
-                       return False;
+                       return false;
                }
                /* Now that we are authenticated, go back to the generic session key... */
                dce_conn->auth_state.session_key = dcesrv_generic_session_key;
-               return True;
+               return true;
        } else {
                DEBUG(4, ("dcesrv_auth_auth3: failed to authenticate: %s\n", 
                          nt_errstr(status)));
-               return False;
+               return false;
        }
 
-       return True;
+       return true;
 }
 
 /*
   parse any auth information from a dcerpc alter request
-  return False if we can't handle the auth request for some 
+  return false if we can't handle the auth request for some 
   reason (in which case we send a bind_nak (is this true for here?))
 */
-BOOL dcesrv_auth_alter(struct dcesrv_call_state *call)
+bool dcesrv_auth_alter(struct dcesrv_call_state *call)
 {
        struct ncacn_packet *pkt = &call->pkt;
        struct dcesrv_connection *dce_conn = call->conn;
-       NTSTATUS status;
+       enum ndr_err_code ndr_err;
 
        /* on a pure interface change there is no auth blob */
        if (pkt->u.alter.auth_info.length == 0) {
-               return True;
+               return true;
        }
 
        /* We can't work without an existing gensec state */
        if (!dce_conn->auth_state.gensec_security) {
-               return False;
+               return false;
        }
 
        dce_conn->auth_state.auth_info = talloc(dce_conn, struct dcerpc_auth);
        if (!dce_conn->auth_state.auth_info) {
-               return False;
+               return false;
        }
 
-       status = ndr_pull_struct_blob(&pkt->u.alter.auth_info,
-                                     call,
-                                     dce_conn->auth_state.auth_info,
-                                     (ndr_pull_flags_fn_t)ndr_pull_dcerpc_auth);
-       if (!NT_STATUS_IS_OK(status)) {
-               return False;
+       ndr_err = ndr_pull_struct_blob(&pkt->u.alter.auth_info,
+                                      call, NULL,
+                                      dce_conn->auth_state.auth_info,
+                                      (ndr_pull_flags_fn_t)ndr_pull_dcerpc_auth);
+       if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
+               return false;
        }
 
-       return True;
+       return true;
 }
 
 /*
@@ -266,37 +279,10 @@ NTSTATUS dcesrv_auth_alter_ack(struct dcesrv_call_state *call, struct ncacn_pack
        return status;
 }
 
-/*
-  generate a CONNECT level verifier
-*/
-static NTSTATUS dcesrv_connect_verifier(TALLOC_CTX *mem_ctx, DATA_BLOB *blob)
-{
-       *blob = data_blob_talloc(mem_ctx, NULL, 16);
-       if (blob->data == NULL) {
-               return NT_STATUS_NO_MEMORY;
-       }
-       SIVAL(blob->data, 0, 1);
-       memset(blob->data+4, 0, 12);
-       return NT_STATUS_OK;
-}
-
-/*
-  generate a CONNECT level verifier
-*/
-static NTSTATUS dcesrv_check_connect_verifier(DATA_BLOB *blob)
-{
-       if (blob->length != 16 ||
-           IVAL(blob->data, 0) != 1) {
-               return NT_STATUS_ACCESS_DENIED;
-       }
-       return NT_STATUS_OK;
-}
-
-
 /*
   check credentials on a request
 */
-BOOL dcesrv_auth_request(struct dcesrv_call_state *call, DATA_BLOB *full_packet)
+bool dcesrv_auth_request(struct dcesrv_call_state *call, DATA_BLOB *full_packet)
 {
        struct ncacn_packet *pkt = &call->pkt;
        struct dcesrv_connection *dce_conn = call->conn;
@@ -304,17 +290,39 @@ BOOL dcesrv_auth_request(struct dcesrv_call_state *call, DATA_BLOB *full_packet)
        struct dcerpc_auth auth;
        struct ndr_pull *ndr;
        NTSTATUS status;
+       enum ndr_err_code ndr_err;
+       size_t hdr_size = DCERPC_REQUEST_LENGTH;
 
        if (!dce_conn->auth_state.auth_info ||
            !dce_conn->auth_state.gensec_security) {
-               return True;
+               return true;
+       }
+
+       switch (dce_conn->auth_state.auth_info->auth_level) {
+       case DCERPC_AUTH_LEVEL_PRIVACY:
+       case DCERPC_AUTH_LEVEL_INTEGRITY:
+               break;
+
+       case DCERPC_AUTH_LEVEL_CONNECT:
+               if (pkt->auth_length != 0) {
+                       break;
+               }
+               return true;
+       case DCERPC_AUTH_LEVEL_NONE:
+               if (pkt->auth_length != 0) {
+                       return false;
+               }
+               return true;
+
+       default:
+               return false;
        }
 
        auth_blob.length = 8 + pkt->auth_length;
 
        /* check for a valid length */
        if (pkt->u.request.stub_and_verifier.length < auth_blob.length) {
-               return False;
+               return false;
        }
 
        auth_blob.data = 
@@ -323,19 +331,24 @@ BOOL dcesrv_auth_request(struct dcesrv_call_state *call, DATA_BLOB *full_packet)
        pkt->u.request.stub_and_verifier.length -= auth_blob.length;
 
        /* pull the auth structure */
-       ndr = ndr_pull_init_blob(&auth_blob, call);
+       ndr = ndr_pull_init_blob(&auth_blob, call, lp_iconv_convenience(call->conn->dce_ctx->lp_ctx));
        if (!ndr) {
-               return False;
+               return false;
        }
 
        if (!(pkt->drep[0] & DCERPC_DREP_LE)) {
                ndr->flags |= LIBNDR_FLAG_BIGENDIAN;
        }
 
-       status = ndr_pull_dcerpc_auth(ndr, NDR_SCALARS|NDR_BUFFERS, &auth);
-       if (!NT_STATUS_IS_OK(status)) {
+       if (pkt->pfc_flags & DCERPC_PFC_FLAG_OBJECT_UUID) {
+               ndr->flags |= LIBNDR_FLAG_OBJECT_PRESENT;
+               hdr_size += 16;
+       }
+
+       ndr_err = ndr_pull_dcerpc_auth(ndr, NDR_SCALARS|NDR_BUFFERS, &auth);
+       if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
                talloc_free(ndr);
-               return False;
+               return false;
        }
 
        /* check signature or unseal the packet */
@@ -343,13 +356,13 @@ BOOL dcesrv_auth_request(struct dcesrv_call_state *call, DATA_BLOB *full_packet)
        case DCERPC_AUTH_LEVEL_PRIVACY:
                status = gensec_unseal_packet(dce_conn->auth_state.gensec_security,
                                              call,
-                                             full_packet->data + DCERPC_REQUEST_LENGTH,
+                                             full_packet->data + hdr_size,
                                              pkt->u.request.stub_and_verifier.length, 
                                              full_packet->data,
                                              full_packet->length-auth.credentials.length,
                                              &auth.credentials);
                memcpy(pkt->u.request.stub_and_verifier.data, 
-                      full_packet->data + DCERPC_REQUEST_LENGTH,
+                      full_packet->data + hdr_size,
                       pkt->u.request.stub_and_verifier.length);
                break;
 
@@ -364,7 +377,8 @@ BOOL dcesrv_auth_request(struct dcesrv_call_state *call, DATA_BLOB *full_packet)
                break;
 
        case DCERPC_AUTH_LEVEL_CONNECT:
-               status = dcesrv_check_connect_verifier(&auth.credentials);
+               /* for now we ignore possible signatures here */
+               status = NT_STATUS_OK;
                break;
 
        default:
@@ -375,7 +389,7 @@ BOOL dcesrv_auth_request(struct dcesrv_call_state *call, DATA_BLOB *full_packet)
        /* remove the indicated amount of padding */
        if (pkt->u.request.stub_and_verifier.length < auth.auth_pad_length) {
                talloc_free(ndr);
-               return False;
+               return false;
        }
        pkt->u.request.stub_and_verifier.length -= auth.auth_pad_length;
        talloc_free(ndr);
@@ -387,77 +401,90 @@ BOOL dcesrv_auth_request(struct dcesrv_call_state *call, DATA_BLOB *full_packet)
 /* 
    push a signed or sealed dcerpc request packet into a blob
 */
-BOOL dcesrv_auth_response(struct dcesrv_call_state *call,
-                         DATA_BLOB *blob, struct ncacn_packet *pkt)
+bool dcesrv_auth_response(struct dcesrv_call_state *call,
+                         DATA_BLOB *blob, size_t sig_size,
+                         struct ncacn_packet *pkt)
 {
        struct dcesrv_connection *dce_conn = call->conn;
        NTSTATUS status;
+       enum ndr_err_code ndr_err;
        struct ndr_push *ndr;
        uint32_t payload_length;
        DATA_BLOB creds2;
 
        /* non-signed packets are simple */
-       if (!dce_conn->auth_state.auth_info || !dce_conn->auth_state.gensec_security) {
-               status = ncacn_push_auth(blob, call, pkt, NULL);
+       if (sig_size == 0) {
+               status = ncacn_push_auth(blob, call, lp_iconv_convenience(dce_conn->dce_ctx->lp_ctx), pkt, NULL);
                return NT_STATUS_IS_OK(status);
        }
 
-       ndr = ndr_push_init_ctx(call);
+       switch (dce_conn->auth_state.auth_info->auth_level) {
+       case DCERPC_AUTH_LEVEL_PRIVACY:
+       case DCERPC_AUTH_LEVEL_INTEGRITY:
+               break;
+
+       case DCERPC_AUTH_LEVEL_CONNECT:
+               /*
+                * TODO: let the gensec mech decide if it wants to generate a signature
+                *       that might be needed for schannel...
+                */
+               status = ncacn_push_auth(blob, call, lp_iconv_convenience(dce_conn->dce_ctx->lp_ctx), pkt, NULL);
+               return NT_STATUS_IS_OK(status);
+
+       case DCERPC_AUTH_LEVEL_NONE:
+               status = ncacn_push_auth(blob, call, lp_iconv_convenience(dce_conn->dce_ctx->lp_ctx), pkt, NULL);
+               return NT_STATUS_IS_OK(status);
+
+       default:
+               return false;
+       }
+
+       ndr = ndr_push_init_ctx(call, lp_iconv_convenience(dce_conn->dce_ctx->lp_ctx));
        if (!ndr) {
-               return False;
+               return false;
        }
 
        if (!(pkt->drep[0] & DCERPC_DREP_LE)) {
                ndr->flags |= LIBNDR_FLAG_BIGENDIAN;
        }
 
-       status = ndr_push_ncacn_packet(ndr, NDR_SCALARS|NDR_BUFFERS, pkt);
-       if (!NT_STATUS_IS_OK(status)) {
-               return False;
+       ndr_err = ndr_push_ncacn_packet(ndr, NDR_SCALARS|NDR_BUFFERS, pkt);
+       if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
+               return false;
        }
 
        /* pad to 16 byte multiple, match win2k3 */
-       dce_conn->auth_state.auth_info->auth_pad_length = NDR_ALIGN(ndr, 16);
-       ndr_push_zero(ndr, dce_conn->auth_state.auth_info->auth_pad_length);
-
-       payload_length = ndr->offset - DCERPC_REQUEST_LENGTH;
+       dce_conn->auth_state.auth_info->auth_pad_length =
+               (16 - (pkt->u.response.stub_and_verifier.length & 15)) & 15;
+       ndr_err = ndr_push_zero(ndr, dce_conn->auth_state.auth_info->auth_pad_length);
+       if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
+               return false;
+       }
 
-       if (dce_conn->auth_state.auth_info->auth_level == DCERPC_AUTH_LEVEL_CONNECT) {
-               status = dcesrv_connect_verifier(call,
-                                                &dce_conn->auth_state.auth_info->credentials);
-               if (!NT_STATUS_IS_OK(status)) {
-                       return False;
-               }
-       } else {
+       payload_length = pkt->u.response.stub_and_verifier.length +
+               dce_conn->auth_state.auth_info->auth_pad_length;
 
-               /* We hope this length is accruate.  If must be if the
-                * GENSEC mech does AEAD signing of the packet
-                * headers */
-               dce_conn->auth_state.auth_info->credentials
-                       = data_blob_talloc(call, NULL, 
-                                          gensec_sig_size(dce_conn->auth_state.gensec_security, 
-                                                          payload_length));
-               data_blob_clear(&dce_conn->auth_state.auth_info->credentials);
-       }
+       /* we start without signature, it will appended later */
+       dce_conn->auth_state.auth_info->credentials = data_blob(NULL, 0);
 
        /* add the auth verifier */
-       status = ndr_push_dcerpc_auth(ndr, NDR_SCALARS|NDR_BUFFERS, 
+       ndr_err = ndr_push_dcerpc_auth(ndr, NDR_SCALARS|NDR_BUFFERS,
                                      dce_conn->auth_state.auth_info);
-       if (!NT_STATUS_IS_OK(status)) {
-               return False;
+       if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
+               return false;
        }
 
        /* extract the whole packet as a blob */
        *blob = ndr_push_blob(ndr);
 
-       /* fill in the fragment length and auth_length, we can't fill
-          in these earlier as we don't know the signature length (it
-          could be variable length) */
-       dcerpc_set_frag_length(blob, blob->length);
-
-       /* We hope this value is accruate.  If must be if the GENSEC
-        * mech does AEAD signing of the packet headers */
-       dcerpc_set_auth_length(blob, dce_conn->auth_state.auth_info->credentials.length);
+       /*
+        * Setup the frag and auth length in the packet buffer.
+        * This is needed if the GENSEC mech does AEAD signing
+        * of the packet headers. The signature itself will be
+        * appended later.
+        */
+       dcerpc_set_frag_length(blob, blob->length + sig_size);
+       dcerpc_set_auth_length(blob, sig_size);
 
        /* sign or seal the packet */
        switch (dce_conn->auth_state.auth_info->auth_level) {
@@ -467,22 +494,8 @@ BOOL dcesrv_auth_response(struct dcesrv_call_state *call,
                                            ndr->data + DCERPC_REQUEST_LENGTH, 
                                            payload_length,
                                            blob->data,
-                                           blob->length - dce_conn->auth_state.auth_info->credentials.length,
+                                           blob->length,
                                            &creds2);
-
-               if (NT_STATUS_IS_OK(status)) {
-                       blob->length -= dce_conn->auth_state.auth_info->credentials.length;
-                       if (!data_blob_append(call, blob, creds2.data, creds2.length))
-                               status = NT_STATUS_NO_MEMORY;
-                       else
-                               status = NT_STATUS_OK;
-               }
-
-               /* If we did AEAD signing of the packet headers, then we hope
-                * this value didn't change... */
-               dcerpc_set_auth_length(blob, creds2.length);
-               dcerpc_set_frag_length(blob, dcerpc_get_frag_length(blob)+creds2.length);
-               data_blob_free(&creds2);
                break;
 
        case DCERPC_AUTH_LEVEL_INTEGRITY:
@@ -491,24 +504,8 @@ BOOL dcesrv_auth_response(struct dcesrv_call_state *call,
                                            ndr->data + DCERPC_REQUEST_LENGTH, 
                                            payload_length,
                                            blob->data,
-                                           blob->length - dce_conn->auth_state.auth_info->credentials.length,
+                                           blob->length,
                                            &creds2);
-               if (NT_STATUS_IS_OK(status)) {
-                       blob->length -= dce_conn->auth_state.auth_info->credentials.length;
-                       if (!data_blob_append(call, blob, creds2.data, creds2.length))
-                               status = NT_STATUS_NO_MEMORY;
-                       else
-                               status = NT_STATUS_OK;
-               }
-
-               /* If we did AEAD signing of the packet headers, then we hope
-                * this value didn't change... */
-               dcerpc_set_auth_length(blob, creds2.length);
-               dcerpc_set_frag_length(blob, dcerpc_get_frag_length(blob)+creds2.length);
-               data_blob_free(&creds2);
-               break;
-
-       case DCERPC_AUTH_LEVEL_CONNECT:
                break;
 
        default:
@@ -516,11 +513,27 @@ BOOL dcesrv_auth_response(struct dcesrv_call_state *call,
                break;
        }
 
-       data_blob_free(&dce_conn->auth_state.auth_info->credentials);
+       if (NT_STATUS_IS_OK(status)) {
+               if (creds2.length != sig_size) {
+                       DEBUG(0,("dcesrv_auth_response: creds2.length[%u] != sig_size[%u] pad[%u] stub[%u]\n",
+                                (unsigned)creds2.length, (uint32_t)sig_size,
+                                (unsigned)dce_conn->auth_state.auth_info->auth_pad_length,
+                                (unsigned)pkt->u.response.stub_and_verifier.length));
+                       data_blob_free(&creds2);
+                       status = NT_STATUS_INTERNAL_ERROR;
+               }
+       }
+
+       if (NT_STATUS_IS_OK(status)) {
+               if (!data_blob_append(call, blob, creds2.data, creds2.length)) {
+                       status = NT_STATUS_NO_MEMORY;
+               }
+               data_blob_free(&creds2);
+       }
 
        if (!NT_STATUS_IS_OK(status)) {
-               return False;
+               return false;
        }       
 
-       return True;
+       return true;
 }