r24780: More work allowing libutil to be used by external users.
[kai/samba.git] / source4 / rpc_server / dcesrv_auth.c
index e2a798c1ae0c4943965df59c154a041bce01e06e..49a4c3de13e381b20b2cc0274ce52d9ee8e00323 100644 (file)
@@ -8,7 +8,7 @@
 
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
-   the Free Software Foundation; either version 2 of the License, or
+   the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.
    
    This program is distributed in the hope that it will be useful,
    GNU General Public License for more details.
    
    You should have received a copy of the GNU General Public License
-   along with this program; if not, write to the Free Software
-   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
 #include "includes.h"
-
-/*
-  startup the cryptographic side of an authenticated dcerpc server
-*/
-NTSTATUS dcesrv_crypto_select_type(struct dcesrv_connection *dce_conn,
-                              struct dcesrv_auth *auth)
-{
-       NTSTATUS status;
-       if (auth->auth_info->auth_level != DCERPC_AUTH_LEVEL_INTEGRITY &&
-           auth->auth_info->auth_level != DCERPC_AUTH_LEVEL_PRIVACY) {
-               DEBUG(2,("auth_level %d not supported in dcesrv auth\n", 
-                        auth->auth_info->auth_level));
-               return NT_STATUS_INVALID_PARAMETER;
-       }
-
-       if (auth->gensec_security != NULL) {
-               /* TODO:
-                * this this function should not be called
-                * twice per dcesrv_connection!
-                * 
-                * so we need to find out the right
-                * dcerpc error to return
-                */
-       }
-
-       status = gensec_server_start(&auth->gensec_security);
-       if (!NT_STATUS_IS_OK(status)) {
-               DEBUG(1, ("Failed to start GENSEC server code: %s\n", nt_errstr(status)));
-               return status;
-       }
-
-       status = gensec_start_mech_by_authtype(auth->gensec_security, auth->auth_info->auth_type, 
-                                              auth->auth_info->auth_level);
-
-       if (!NT_STATUS_IS_OK(status)) {
-               DEBUG(1, ("Failed to start GENSEC mech-specific server code (%d): %s\n", 
-                         (int)auth->auth_info->auth_type,
-                         nt_errstr(status)));
-               return status;
-       }
-
-       return status;
-}
+#include "rpc_server/dcerpc_server.h"
+#include "librpc/gen_ndr/ndr_dcerpc.h"
+#include "auth/credentials/credentials.h"
+#include "auth/gensec/gensec.h"
 
 /*
   parse any auth information from a dcerpc bind request
@@ -73,8 +33,10 @@ NTSTATUS dcesrv_crypto_select_type(struct dcesrv_connection *dce_conn,
 */
 BOOL dcesrv_auth_bind(struct dcesrv_call_state *call)
 {
-       struct dcerpc_packet *pkt = &call->pkt;
+       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;
 
        if (pkt->u.bind.auth_info.length == 0) {
@@ -82,21 +44,50 @@ BOOL dcesrv_auth_bind(struct dcesrv_call_state *call)
                return True;
        }
 
-       dce_conn->auth_state.auth_info = talloc_p(dce_conn->mem_ctx, struct dcerpc_auth);
+       dce_conn->auth_state.auth_info = talloc(dce_conn, struct dcerpc_auth);
        if (!dce_conn->auth_state.auth_info) {
                return False;
        }
 
        status = ndr_pull_struct_blob(&pkt->u.bind.auth_info,
-                                     call->mem_ctx,
+                                     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 = dcesrv_crypto_select_type(dce_conn, &dce_conn->auth_state);
+       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;
+       }
+
+       server_credentials 
+               = cli_credentials_init(call);
+       if (!server_credentials) {
+               DEBUG(1, ("Failed to init server credentials\n"));
+               return False;
+       }
+       
+       cli_credentials_set_conf(server_credentials);
+       status = cli_credentials_set_machine_account(server_credentials);
+       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 = gensec_start_mech_by_authtype(auth->gensec_security, auth->auth_info->auth_type, 
+                                              auth->auth_info->auth_level);
+
        if (!NT_STATUS_IS_OK(status)) {
+               DEBUG(1, ("Failed to start GENSEC mechanism for DCERPC server: auth_type=%d, auth_level=%d: %s\n", 
+                         (int)auth->auth_info->auth_type,
+                         (int)auth->auth_info->auth_level,
+                         nt_errstr(status)));
                return False;
        }
 
@@ -107,17 +98,17 @@ BOOL dcesrv_auth_bind(struct dcesrv_call_state *call)
   add any auth information needed in a bind ack, and process the authentication
   information found in the bind.
 */
-BOOL dcesrv_auth_bind_ack(struct dcesrv_call_state *call, struct dcerpc_packet *pkt)
+NTSTATUS dcesrv_auth_bind_ack(struct dcesrv_call_state *call, struct ncacn_packet *pkt)
 {
        struct dcesrv_connection *dce_conn = call->conn;
        NTSTATUS status;
 
        if (!call->conn->auth_state.gensec_security) {
-               return True;
+               return NT_STATUS_OK;
        }
 
        status = gensec_update(dce_conn->auth_state.gensec_security,
-                              call->mem_ctx,
+                              call,
                               dce_conn->auth_state.auth_info->credentials, 
                               &dce_conn->auth_state.auth_info->credentials);
        
@@ -126,19 +117,19 @@ BOOL dcesrv_auth_bind_ack(struct dcesrv_call_state *call, struct dcerpc_packet *
                                             &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 status;
                }
 
-               /* Now that we are authenticated, got back to the generic session key... */
+               /* 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 NT_STATUS_OK;
        } else if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
                dce_conn->auth_state.auth_info->auth_pad_length = 0;
                dce_conn->auth_state.auth_info->auth_reserved = 0;
-               return True;
+               return NT_STATUS_OK;
        } else {
                DEBUG(2, ("Failed to start dcesrv auth negotiate: %s\n", nt_errstr(status)));
-               return False;
+               return status;
        }
 }
 
@@ -148,19 +139,19 @@ BOOL dcesrv_auth_bind_ack(struct dcesrv_call_state *call, struct dcerpc_packet *
 */
 BOOL dcesrv_auth_auth3(struct dcesrv_call_state *call)
 {
-       struct dcerpc_packet *pkt = &call->pkt;
+       struct ncacn_packet *pkt = &call->pkt;
        struct dcesrv_connection *dce_conn = call->conn;
        NTSTATUS status;
 
        /* 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.auth.auth_info.length == 0) {
+           pkt->u.auth3.auth_info.length == 0) {
                return False;
        }
 
-       status = ndr_pull_struct_blob(&pkt->u.auth.auth_info,
-                                     call->mem_ctx,
+       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)) {
@@ -169,7 +160,7 @@ BOOL dcesrv_auth_auth3(struct dcesrv_call_state *call)
 
        /* Pass the extra data we got from the client down to gensec for processing */
        status = gensec_update(dce_conn->auth_state.gensec_security,
-                              call->mem_ctx,
+                              call,
                               dce_conn->auth_state.auth_info->credentials, 
                               &dce_conn->auth_state.auth_info->credentials);
        if (NT_STATUS_IS_OK(status)) {
@@ -179,7 +170,7 @@ BOOL dcesrv_auth_auth3(struct dcesrv_call_state *call)
                        DEBUG(1, ("Failed to establish session_info: %s\n", nt_errstr(status)));
                        return False;
                }
-               /* Now that we are authenticated, got back to the generic session key... */
+               /* Now that we are authenticated, go back to the generic session key... */
                dce_conn->auth_state.session_key = dcesrv_generic_session_key;
                return True;
        } else {
@@ -191,12 +182,122 @@ BOOL dcesrv_auth_auth3(struct dcesrv_call_state *call)
        return True;
 }
 
+/*
+  parse any auth information from a dcerpc alter request
+  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)
+{
+       struct ncacn_packet *pkt = &call->pkt;
+       struct dcesrv_connection *dce_conn = call->conn;
+       NTSTATUS status;
+
+       /* on a pure interface change there is no auth blob */
+       if (pkt->u.alter.auth_info.length == 0) {
+               return True;
+       }
+
+       /* We can't work without an existing gensec state */
+       if (!dce_conn->auth_state.gensec_security) {
+               return False;
+       }
+
+       dce_conn->auth_state.auth_info = talloc(dce_conn, struct dcerpc_auth);
+       if (!dce_conn->auth_state.auth_info) {
+               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;
+       }
+
+       return True;
+}
+
+/*
+  add any auth information needed in a alter ack, and process the authentication
+  information found in the alter.
+*/
+NTSTATUS dcesrv_auth_alter_ack(struct dcesrv_call_state *call, struct ncacn_packet *pkt)
+{
+       struct dcesrv_connection *dce_conn = call->conn;
+       NTSTATUS status;
+
+       /* on a pure interface change there is no auth_info structure
+          setup */
+       if (!call->conn->auth_state.auth_info ||
+           dce_conn->auth_state.auth_info->credentials.length == 0) {
+               return NT_STATUS_OK;
+       }
+
+       if (!call->conn->auth_state.gensec_security) {
+               return NT_STATUS_INVALID_PARAMETER;
+       }
+
+       status = gensec_update(dce_conn->auth_state.gensec_security,
+                              call,
+                              dce_conn->auth_state.auth_info->credentials, 
+                              &dce_conn->auth_state.auth_info->credentials);
+
+       if (NT_STATUS_IS_OK(status)) {
+               status = gensec_session_info(dce_conn->auth_state.gensec_security,
+                                            &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 status;
+               }
+
+               /* Now that we are authenticated, got back to the generic session key... */
+               dce_conn->auth_state.session_key = dcesrv_generic_session_key;
+               return NT_STATUS_OK;
+       } else if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
+               dce_conn->auth_state.auth_info->auth_pad_length = 0;
+               dce_conn->auth_state.auth_info->auth_reserved = 0;
+               return NT_STATUS_OK;
+       }
+
+       DEBUG(2, ("Failed to finish dcesrv auth alter_ack: %s\n", nt_errstr(status)));
+       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)
 {
-       struct dcerpc_packet *pkt = &call->pkt;
+       struct ncacn_packet *pkt = &call->pkt;
        struct dcesrv_connection *dce_conn = call->conn;
        DATA_BLOB auth_blob;
        struct dcerpc_auth auth;
@@ -221,7 +322,7 @@ 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->mem_ctx);
+       ndr = ndr_pull_init_blob(&auth_blob, call);
        if (!ndr) {
                return False;
        }
@@ -232,6 +333,7 @@ BOOL dcesrv_auth_request(struct dcesrv_call_state *call, DATA_BLOB *full_packet)
 
        status = ndr_pull_dcerpc_auth(ndr, NDR_SCALARS|NDR_BUFFERS, &auth);
        if (!NT_STATUS_IS_OK(status)) {
+               talloc_free(ndr);
                return False;
        }
 
@@ -239,7 +341,7 @@ BOOL dcesrv_auth_request(struct dcesrv_call_state *call, DATA_BLOB *full_packet)
        switch (dce_conn->auth_state.auth_info->auth_level) {
        case DCERPC_AUTH_LEVEL_PRIVACY:
                status = gensec_unseal_packet(dce_conn->auth_state.gensec_security,
-                                             call->mem_ctx,
+                                             call,
                                              full_packet->data + DCERPC_REQUEST_LENGTH,
                                              pkt->u.request.stub_and_verifier.length, 
                                              full_packet->data,
@@ -252,7 +354,7 @@ BOOL dcesrv_auth_request(struct dcesrv_call_state *call, DATA_BLOB *full_packet)
 
        case DCERPC_AUTH_LEVEL_INTEGRITY:
                status = gensec_check_packet(dce_conn->auth_state.gensec_security,
-                                            call->mem_ctx,
+                                            call,
                                             pkt->u.request.stub_and_verifier.data, 
                                             pkt->u.request.stub_and_verifier.length,
                                             full_packet->data,
@@ -260,6 +362,10 @@ BOOL dcesrv_auth_request(struct dcesrv_call_state *call, DATA_BLOB *full_packet)
                                             &auth.credentials);
                break;
 
+       case DCERPC_AUTH_LEVEL_CONNECT:
+               status = dcesrv_check_connect_verifier(&auth.credentials);
+               break;
+
        default:
                status = NT_STATUS_INVALID_LEVEL;
                break;
@@ -267,9 +373,11 @@ 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;
        }
        pkt->u.request.stub_and_verifier.length -= auth.auth_pad_length;
+       talloc_free(ndr);
 
        return NT_STATUS_IS_OK(status);
 }
@@ -279,20 +387,21 @@ 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 dcerpc_packet *pkt)
+                         DATA_BLOB *blob, struct ncacn_packet *pkt)
 {
        struct dcesrv_connection *dce_conn = call->conn;
        NTSTATUS status;
        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 = dcerpc_push_auth(blob, call->mem_ctx, pkt, NULL);
+               status = ncacn_push_auth(blob, call, pkt, NULL);
                return NT_STATUS_IS_OK(status);
        }
 
-       ndr = ndr_push_init_ctx(call->mem_ctx);
+       ndr = ndr_push_init_ctx(call);
        if (!ndr) {
                return False;
        }
@@ -301,23 +410,38 @@ BOOL dcesrv_auth_response(struct dcesrv_call_state *call,
                ndr->flags |= LIBNDR_FLAG_BIGENDIAN;
        }
 
-       status = ndr_push_dcerpc_packet(ndr, NDR_SCALARS|NDR_BUFFERS, pkt);
+       status = ndr_push_ncacn_packet(ndr, NDR_SCALARS|NDR_BUFFERS, pkt);
        if (!NT_STATUS_IS_OK(status)) {
                return False;
        }
 
-       /* pad to 8 byte multiple */
-       dce_conn->auth_state.auth_info->auth_pad_length = NDR_ALIGN(ndr, 8);
+       /* 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->credentials
-               = data_blob_talloc(call->mem_ctx, NULL, 
-                                  gensec_sig_size(dce_conn->auth_state.gensec_security));
+
+       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 {
+
+               /* 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);
+       }
 
        /* add the auth verifier */
-       status = ndr_push_dcerpc_auth(ndr, NDR_SCALARS|NDR_BUFFERS, dce_conn->auth_state.auth_info);
+       status = ndr_push_dcerpc_auth(ndr, NDR_SCALARS|NDR_BUFFERS, 
+                                     dce_conn->auth_state.auth_info);
        if (!NT_STATUS_IS_OK(status)) {
                return False;
        }
@@ -329,43 +453,73 @@ BOOL dcesrv_auth_response(struct dcesrv_call_state *call,
           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);
 
        /* sign or seal the packet */
        switch (dce_conn->auth_state.auth_info->auth_level) {
        case DCERPC_AUTH_LEVEL_PRIVACY:
                status = gensec_seal_packet(dce_conn->auth_state.gensec_security, 
-                                           call->mem_ctx,
+                                           call,
                                            ndr->data + DCERPC_REQUEST_LENGTH, 
                                            payload_length,
                                            blob->data,
                                            blob->length - dce_conn->auth_state.auth_info->credentials.length,
-                                             &dce_conn->auth_state.auth_info->credentials);
+                                           &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:
                status = gensec_sign_packet(dce_conn->auth_state.gensec_security, 
-                                           call->mem_ctx,
+                                           call,
                                            ndr->data + DCERPC_REQUEST_LENGTH, 
                                            payload_length,
                                            blob->data,
                                            blob->length - dce_conn->auth_state.auth_info->credentials.length,
-                                           &dce_conn->auth_state.auth_info->credentials);
+                                           &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:
                status = NT_STATUS_INVALID_LEVEL;
                break;
        }
 
+       data_blob_free(&dce_conn->auth_state.auth_info->credentials);
+
        if (!NT_STATUS_IS_OK(status)) {
                return False;
        }       
 
-       memcpy(blob->data + blob->length - dce_conn->auth_state.auth_info->credentials.length, 
-              dce_conn->auth_state.auth_info->credentials.data, dce_conn->auth_state.auth_info->credentials.length);
-       
-       data_blob_free(&dce_conn->auth_state.auth_info->credentials);
-
        return True;
 }