s4:rpc_server: add dcesrv_iface_state_{store,find}_{assoc,conn}() helpers
[samba.git] / source4 / rpc_server / dcesrv_auth.c
index 1d89441170be883ca9feb19a74f46b45a4e462d1..98827ffd2141cb41e57cc902d7c0fae3fbbacf6c 100644 (file)
 #include "includes.h"
 #include "rpc_server/dcerpc_server.h"
 #include "rpc_server/dcerpc_server_proto.h"
+#include "rpc_server/common/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"
+#include "librpc/rpc/rpc_common.h"
 
-/*
-  parse any auth information from a dcerpc bind request
-  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)
+static bool dcesrv_auth_prepare_gensec(struct dcesrv_call_state *call)
 {
-       struct cli_credentials *server_credentials;
-       struct ncacn_packet *pkt = &call->pkt;
+       struct cli_credentials *server_credentials = NULL;
        struct dcesrv_connection *dce_conn = call->conn;
-       struct dcesrv_auth *auth = &dce_conn->auth_state;
+       struct dcesrv_auth *auth = call->auth_state;
+       bool want_header_signing = false;
        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;
+       if (auth->auth_started) {
+               return false;
        }
 
-       dce_conn->auth_state.auth_info = talloc(dce_conn, struct dcerpc_auth);
-       if (!dce_conn->auth_state.auth_info) {
+       auth->auth_started = true;
+
+       if (auth->auth_invalid) {
                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)) {
+       if (auth->auth_finished) {
                return false;
        }
 
-       status = gensec_server_start(dce_conn, call->event_ctx, call->conn->dce_ctx->lp_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)));
+       if (auth->gensec_security != NULL) {
                return false;
        }
 
+       switch (call->in_auth_info.auth_level) {
+       case DCERPC_AUTH_LEVEL_CONNECT:
+       case DCERPC_AUTH_LEVEL_CALL:
+       case DCERPC_AUTH_LEVEL_PACKET:
+       case DCERPC_AUTH_LEVEL_INTEGRITY:
+       case DCERPC_AUTH_LEVEL_PRIVACY:
+               /*
+                * We evaluate auth_type only if auth_level was valid
+                */
+               break;
+       default:
+               /*
+                * Setting DCERPC_AUTH_LEVEL_NONE,
+                * gives the caller the reject_reason
+                * as auth_context_id.
+                *
+                * Note: DCERPC_AUTH_LEVEL_NONE == 1
+                */
+               auth->auth_type = DCERPC_AUTH_TYPE_NONE;
+               auth->auth_level = DCERPC_AUTH_LEVEL_NONE;
+               auth->auth_context_id = DCERPC_BIND_NAK_REASON_NOT_SPECIFIED;
+               return false;
+       }
+
+       auth->auth_type = call->in_auth_info.auth_type;
+       auth->auth_level = call->in_auth_info.auth_level;
+       auth->auth_context_id = call->in_auth_info.auth_context_id;
+
        server_credentials 
-               = cli_credentials_init(call);
+               = cli_credentials_init(auth);
        if (!server_credentials) {
                DEBUG(1, ("Failed to init server credentials\n"));
                return false;
@@ -77,456 +96,500 @@ bool dcesrv_auth_bind(struct dcesrv_call_state *call)
        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;
+               DEBUG(1, ("Failed to obtain server credentials: %s\n",
+                         nt_errstr(status)));
+               return false;
        }
 
-       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);
-
+       status = samba_server_gensec_start(auth,
+                                          call->event_ctx,
+                                          call->msg_ctx,
+                                          call->conn->dce_ctx->lp_ctx,
+                                          server_credentials,
+                                          NULL,
+                                          &auth->gensec_security);
        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,
+               DEBUG(1, ("Failed to call samba_server_gensec_start %s\n",
                          nt_errstr(status)));
                return false;
        }
 
-       return true;
-}
-
-/*
-  add any auth information needed in a bind ack, and process the authentication
-  information found in the bind.
-*/
-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 NT_STATUS_OK;
+       /*
+        * We have to call this because we set the target_service for
+        * Kerberos to NULL above, and in any case we wish to log a
+        * more specific service target.
+        *
+        */
+       status = gensec_set_target_service_description(auth->gensec_security,
+                                                      "DCE/RPC");
+       if (!NT_STATUS_IS_OK(status)) {
+               DEBUG(1, ("Failed to call gensec_set_target_service_description %s\n",
+                         nt_errstr(status)));
+               return false;
        }
 
-       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 (call->conn->remote_address != NULL) {
+               status = gensec_set_remote_address(auth->gensec_security,
+                                               call->conn->remote_address);
                if (!NT_STATUS_IS_OK(status)) {
-                       DEBUG(1, ("Failed to establish session_info: %s\n", nt_errstr(status)));
-                       return status;
+                       DEBUG(1, ("Failed to call gensec_set_remote_address() %s\n",
+                                 nt_errstr(status)));
+                       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 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;
-       } else {
-               DEBUG(2, ("Failed to start dcesrv auth negotiate: %s\n", nt_errstr(status)));
-               return status;
        }
-}
 
+       if (call->conn->local_address != NULL) {
+               status = gensec_set_local_address(auth->gensec_security,
+                                                 call->conn->local_address);
+               if (!NT_STATUS_IS_OK(status)) {
+                       DEBUG(1, ("Failed to call gensec_set_local_address() %s\n",
+                                 nt_errstr(status)));
+                       return false;
+               }
+       }
 
-/*
-  process the final stage of a auth request
-*/
-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;
+       status = gensec_start_mech_by_authtype(auth->gensec_security, auth->auth_type,
+                                              auth->auth_level);
+       if (!NT_STATUS_IS_OK(status)) {
+               const char *backend_name =
+                       gensec_get_name_by_authtype(auth->gensec_security,
+                                                   auth->auth_type);
+
+               DEBUG(3, ("Failed to start GENSEC mechanism for DCERPC server: "
+                         "auth_type=%d (%s), auth_level=%d: %s\n",
+                         (int)auth->auth_type, backend_name,
+                         (int)auth->auth_level,
+                         nt_errstr(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.auth3.auth_info.length == 0) {
+               /*
+                * Setting DCERPC_AUTH_LEVEL_NONE,
+                * gives the caller the reject_reason
+                * as auth_context_id.
+                *
+                * Note: DCERPC_AUTH_LEVEL_NONE == 1
+                */
+               auth->auth_type = DCERPC_AUTH_TYPE_NONE;
+               auth->auth_level = DCERPC_AUTH_LEVEL_NONE;
+               if (backend_name != NULL) {
+                       auth->auth_context_id =
+                               DCERPC_BIND_NAK_REASON_INVALID_CHECKSUM;
+               } else {
+                       auth->auth_context_id =
+                               DCERPC_BIND_NAK_REASON_INVALID_AUTH_TYPE;
+               }
                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;
+       if (call->pkt.pfc_flags & DCERPC_PFC_FLAG_SUPPORT_HEADER_SIGN) {
+               auth->client_hdr_signing = true;
+               want_header_signing = true;
        }
 
-       /* Pass the extra data we got from the client down to gensec for processing */
-       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 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;
-       } else {
-               DEBUG(4, ("dcesrv_auth_auth3: failed to authenticate: %s\n", 
-                         nt_errstr(status)));
-               return false;
+       if (want_header_signing) {
+               want_header_signing = gensec_have_feature(auth->gensec_security,
+                                               GENSEC_FEATURE_SIGN_PKT_HEADER);
+       }
+
+       if (want_header_signing) {
+               want_header_signing = lpcfg_parm_bool(dce_conn->dce_ctx->lp_ctx,
+                                                     NULL,
+                                                     "dcesrv",
+                                                     "header signing",
+                                                     true);
+       }
+
+       if (want_header_signing) {
+               gensec_want_feature(auth->gensec_security,
+                                   GENSEC_FEATURE_SIGN_PKT_HEADER);
+               auth->hdr_signing = true;
        }
 
        return true;
 }
 
+static void log_successful_dcesrv_authz_event(struct dcesrv_call_state *call)
+{
+       struct dcesrv_auth *auth = call->auth_state;
+       enum dcerpc_transport_t transport =
+               dcerpc_binding_get_transport(call->conn->endpoint->ep_description);
+       const char *auth_type = derpc_transport_string_by_transport(transport);
+       const char *transport_protection = AUTHZ_TRANSPORT_PROTECTION_NONE;
+
+       if (transport == NCACN_NP) {
+               transport_protection = AUTHZ_TRANSPORT_PROTECTION_SMB;
+       }
+
+       /*
+        * Log the authorization to this RPC interface.  This
+        * covered ncacn_np pass-through auth, and anonymous
+        * DCE/RPC (eg epmapper, netlogon etc)
+        */
+       log_successful_authz_event(call->conn->msg_ctx,
+                                  call->conn->dce_ctx->lp_ctx,
+                                  call->conn->remote_address,
+                                  call->conn->local_address,
+                                  "DCE/RPC",
+                                  auth_type,
+                                  transport_protection,
+                                  auth->session_info);
+}
+
 /*
-  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?))
+  parse any auth information from a dcerpc bind request
+  return false if we can't handle the auth request for some
+  reason (in which case we send a bind_nak)
 */
-bool dcesrv_auth_alter(struct dcesrv_call_state *call)
+bool dcesrv_auth_bind(struct dcesrv_call_state *call)
 {
        struct ncacn_packet *pkt = &call->pkt;
-       struct dcesrv_connection *dce_conn = call->conn;
-       enum ndr_err_code ndr_err;
+       struct dcesrv_auth *auth = call->auth_state;
+       NTSTATUS status;
 
-       /* on a pure interface change there is no auth blob */
-       if (pkt->u.alter.auth_info.length == 0) {
-               return true;
-       }
+       if (pkt->auth_length == 0) {
+               auth->auth_type = DCERPC_AUTH_TYPE_NONE;
+               auth->auth_level = DCERPC_AUTH_LEVEL_NONE;
+               auth->auth_context_id = 0;
+               auth->auth_started = true;
 
-       /* We can't work without an existing gensec state */
-       if (!dce_conn->auth_state.gensec_security) {
-               return false;
-       }
+               log_successful_dcesrv_authz_event(call);
 
-       dce_conn->auth_state.auth_info = talloc(dce_conn, struct dcerpc_auth);
-       if (!dce_conn->auth_state.auth_info) {
-               return false;
+               return true;
        }
 
-       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)) {
+       status = dcerpc_pull_auth_trailer(pkt, call, &pkt->u.bind.auth_info,
+                                         &call->in_auth_info,
+                                         NULL, true);
+       if (!NT_STATUS_IS_OK(status)) {
+               /*
+                * Setting DCERPC_AUTH_LEVEL_NONE,
+                * gives the caller the reject_reason
+                * as auth_context_id.
+                *
+                * Note: DCERPC_AUTH_LEVEL_NONE == 1
+                */
+               auth->auth_type = DCERPC_AUTH_TYPE_NONE;
+               auth->auth_level = DCERPC_AUTH_LEVEL_NONE;
+               auth->auth_context_id =
+                       DCERPC_BIND_NAK_REASON_PROTOCOL_VERSION_NOT_SUPPORTED;
                return false;
        }
 
-       return true;
+       return dcesrv_auth_prepare_gensec(call);
 }
 
-/*
-  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)
+NTSTATUS dcesrv_auth_complete(struct dcesrv_call_state *call, NTSTATUS status)
 {
-       struct dcesrv_connection *dce_conn = call->conn;
-       NTSTATUS status;
+       struct dcesrv_auth *auth = call->auth_state;
+       const char *pdu = "<unknown>";
 
-       /* 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;
+       switch (call->pkt.ptype) {
+       case DCERPC_PKT_BIND:
+               pdu = "BIND";
+               break;
+       case DCERPC_PKT_ALTER:
+               pdu = "ALTER";
+               break;
+       case DCERPC_PKT_AUTH3:
+               pdu = "AUTH3";
+               if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
+                       DEBUG(4, ("GENSEC not finished at at %s\n", pdu));
+                       return NT_STATUS_RPC_SEC_PKG_ERROR;
+               }
+               break;
+       default:
+               return NT_STATUS_INTERNAL_ERROR;
        }
 
-       if (!call->conn->auth_state.gensec_security) {
-               return NT_STATUS_INVALID_PARAMETER;
+       if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
+               return NT_STATUS_OK;
        }
 
-       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)) {
+               DEBUG(4, ("GENSEC mech rejected the incoming authentication "
+                         "at %s: %s\n", pdu, nt_errstr(status)));
+               return status;
+       }
 
-       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;
-               }
+       status = gensec_session_info(auth->gensec_security,
+                                    auth,
+                                    &auth->session_info);
+       if (!NT_STATUS_IS_OK(status)) {
+               DEBUG(1, ("Failed to establish session_info: %s\n",
+                         nt_errstr(status)));
+               return status;
+       }
+       auth->auth_finished = true;
 
-               /* 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;
+       if (call->pkt.ptype != DCERPC_PKT_AUTH3) {
                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;
+       if (call->out_auth_info->credentials.length != 0) {
+               DEBUG(4, ("GENSEC produced output token (len=%zu) at %s\n",
+                         call->out_auth_info->credentials.length, pdu));
+               return NT_STATUS_RPC_SEC_PKG_ERROR;
        }
-       SIVAL(blob->data, 0, 1);
-       memset(blob->data+4, 0, 12);
+
        return NT_STATUS_OK;
 }
 
 /*
-  generate a CONNECT level verifier
+  add any auth information needed in a bind ack, and process the authentication
+  information found in the bind.
 */
-static NTSTATUS dcesrv_check_connect_verifier(DATA_BLOB *blob)
+NTSTATUS dcesrv_auth_prepare_bind_ack(struct dcesrv_call_state *call, struct ncacn_packet *pkt)
 {
-       if (blob->length != 16 ||
-           IVAL(blob->data, 0) != 1) {
-               return NT_STATUS_ACCESS_DENIED;
+       struct dcesrv_connection *dce_conn = call->conn;
+       struct dcesrv_auth *auth = call->auth_state;
+
+       dce_conn->allow_alter = true;
+
+       if (call->pkt.auth_length == 0) {
+               auth->auth_finished = true;
+               return NT_STATUS_OK;
+       }
+
+       /* We can't work without an existing gensec state */
+       if (auth->gensec_security == NULL) {
+               return NT_STATUS_INTERNAL_ERROR;
        }
+
+       if (auth->hdr_signing) {
+               pkt->pfc_flags |= DCERPC_PFC_FLAG_SUPPORT_HEADER_SIGN;
+       }
+
+       call->_out_auth_info = (struct dcerpc_auth) {
+               .auth_type = auth->auth_type,
+               .auth_level = auth->auth_level,
+               .auth_context_id = auth->auth_context_id,
+       };
+       call->out_auth_info = &call->_out_auth_info;
+
        return NT_STATUS_OK;
 }
 
-
 /*
-  check credentials on a request
+  process the final stage of a auth request
 */
-bool dcesrv_auth_request(struct dcesrv_call_state *call, DATA_BLOB *full_packet)
+bool dcesrv_auth_prepare_auth3(struct dcesrv_call_state *call)
 {
        struct ncacn_packet *pkt = &call->pkt;
-       struct dcesrv_connection *dce_conn = call->conn;
-       DATA_BLOB auth_blob;
-       struct dcerpc_auth auth;
-       struct ndr_pull *ndr;
+       struct dcesrv_auth *auth = call->auth_state;
        NTSTATUS status;
-       enum ndr_err_code ndr_err;
 
-       if (!dce_conn->auth_state.auth_info ||
-           !dce_conn->auth_state.gensec_security) {
-               return true;
+       if (pkt->auth_length == 0) {
+               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) {
+       if (auth->auth_finished) {
                return false;
        }
 
-       auth_blob.data = 
-               pkt->u.request.stub_and_verifier.data + 
-               pkt->u.request.stub_and_verifier.length - auth_blob.length;
-       pkt->u.request.stub_and_verifier.length -= auth_blob.length;
-
-       /* pull the auth structure */
-       ndr = ndr_pull_init_blob(&auth_blob, call, lp_iconv_convenience(call->conn->dce_ctx->lp_ctx));
-       if (!ndr) {
+       /* We can't work without an existing gensec state */
+       if (auth->gensec_security == NULL) {
                return false;
        }
 
-       if (!(pkt->drep[0] & DCERPC_DREP_LE)) {
-               ndr->flags |= LIBNDR_FLAG_BIGENDIAN;
+       status = dcerpc_pull_auth_trailer(pkt, call, &pkt->u.auth3.auth_info,
+                                         &call->in_auth_info, NULL, true);
+       if (!NT_STATUS_IS_OK(status)) {
+               /*
+                * Windows returns DCERPC_NCA_S_FAULT_REMOTE_NO_MEMORY
+                * instead of DCERPC_NCA_S_PROTO_ERROR.
+                */
+               call->fault_code = DCERPC_NCA_S_FAULT_REMOTE_NO_MEMORY;
+               return false;
        }
 
-       ndr_err = ndr_pull_dcerpc_auth(ndr, NDR_SCALARS|NDR_BUFFERS, &auth);
-       if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
-               talloc_free(ndr);
+       if (call->in_auth_info.auth_type != auth->auth_type) {
                return false;
        }
 
-       /* check signature or unseal the 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,
-                                             full_packet->data + DCERPC_REQUEST_LENGTH,
-                                             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,
-                      pkt->u.request.stub_and_verifier.length);
-               break;
-
-       case DCERPC_AUTH_LEVEL_INTEGRITY:
-               status = gensec_check_packet(dce_conn->auth_state.gensec_security,
-                                            call,
-                                            pkt->u.request.stub_and_verifier.data, 
-                                            pkt->u.request.stub_and_verifier.length,
-                                            full_packet->data,
-                                            full_packet->length-auth.credentials.length,
-                                            &auth.credentials);
-               break;
-
-       case DCERPC_AUTH_LEVEL_CONNECT:
-               status = dcesrv_check_connect_verifier(&auth.credentials);
-               break;
-
-       default:
-               status = NT_STATUS_INVALID_LEVEL;
-               break;
+       if (call->in_auth_info.auth_level != auth->auth_level) {
+               return false;
        }
 
-       /* remove the indicated amount of padding */
-       if (pkt->u.request.stub_and_verifier.length < auth.auth_pad_length) {
-               talloc_free(ndr);
+       if (call->in_auth_info.auth_context_id != auth->auth_context_id) {
                return false;
        }
-       pkt->u.request.stub_and_verifier.length -= auth.auth_pad_length;
-       talloc_free(ndr);
 
-       return NT_STATUS_IS_OK(status);
-}
+       call->_out_auth_info = (struct dcerpc_auth) {
+               .auth_type = auth->auth_type,
+               .auth_level = auth->auth_level,
+               .auth_context_id = auth->auth_context_id,
+       };
+       call->out_auth_info = &call->_out_auth_info;
 
+       return true;
+}
 
-/* 
-   push a signed or sealed dcerpc request packet into a blob
+/*
+  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_response(struct dcesrv_call_state *call,
-                         DATA_BLOB *blob, struct ncacn_packet *pkt)
+bool dcesrv_auth_alter(struct dcesrv_call_state *call)
 {
-       struct dcesrv_connection *dce_conn = call->conn;
+       struct ncacn_packet *pkt = &call->pkt;
+       struct dcesrv_auth *auth = call->auth_state;
        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, lp_iconv_convenience(dce_conn->dce_ctx->lp_ctx), pkt, NULL);
-               return NT_STATUS_IS_OK(status);
+       /* on a pure interface change there is no auth blob */
+       if (pkt->auth_length == 0) {
+               if (!auth->auth_finished) {
+                       return false;
+               }
+               return true;
        }
 
-       ndr = ndr_push_init_ctx(call, lp_iconv_convenience(dce_conn->dce_ctx->lp_ctx));
-       if (!ndr) {
+       if (auth->auth_finished) {
+               call->fault_code = DCERPC_FAULT_ACCESS_DENIED;
                return false;
        }
 
-       if (!(pkt->drep[0] & DCERPC_DREP_LE)) {
-               ndr->flags |= LIBNDR_FLAG_BIGENDIAN;
+       /* We can't work without an existing gensec state */
+       if (auth->gensec_security == NULL) {
+               return false;
        }
 
-       ndr_err = ndr_push_ncacn_packet(ndr, NDR_SCALARS|NDR_BUFFERS, pkt);
-       if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
+       status = dcerpc_pull_auth_trailer(pkt, call, &pkt->u.alter.auth_info,
+                                         &call->in_auth_info, NULL, true);
+       if (!NT_STATUS_IS_OK(status)) {
+               call->fault_code = DCERPC_NCA_S_PROTO_ERROR;
                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;
-
-       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 {
+       if (call->in_auth_info.auth_type == DCERPC_AUTH_TYPE_NONE) {
+               call->fault_code = DCERPC_FAULT_ACCESS_DENIED;
+               return false;
+       }
 
-               /* 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);
+       if (call->in_auth_info.auth_type != auth->auth_type) {
+               return false;
        }
 
-       /* add the auth verifier */
-       ndr_err = ndr_push_dcerpc_auth(ndr, NDR_SCALARS|NDR_BUFFERS,
-                                     dce_conn->auth_state.auth_info);
-       if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
+       if (call->in_auth_info.auth_level != auth->auth_level) {
                return false;
        }
 
-       /* extract the whole packet as a blob */
-       *blob = ndr_push_blob(ndr);
+       if (call->in_auth_info.auth_context_id != auth->auth_context_id) {
+               return false;
+       }
 
-       /* 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);
+       return true;
+}
 
-       /* 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);
+/*
+  add any auth information needed in a alter ack, and process the authentication
+  information found in the alter.
+*/
+NTSTATUS dcesrv_auth_prepare_alter_ack(struct dcesrv_call_state *call, struct ncacn_packet *pkt)
+{
+       struct dcesrv_auth *auth = call->auth_state;
 
-       /* 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,
-                                           ndr->data + DCERPC_REQUEST_LENGTH, 
-                                           payload_length,
-                                           blob->data,
-                                           blob->length - dce_conn->auth_state.auth_info->credentials.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;
-               }
+       /* on a pure interface change there is no auth_info structure
+          setup */
+       if (call->pkt.auth_length == 0) {
+               return 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;
+       if (auth->gensec_security == NULL) {
+               return NT_STATUS_INTERNAL_ERROR;
+       }
 
-       case DCERPC_AUTH_LEVEL_INTEGRITY:
-               status = gensec_sign_packet(dce_conn->auth_state.gensec_security, 
-                                           call,
-                                           ndr->data + DCERPC_REQUEST_LENGTH, 
-                                           payload_length,
-                                           blob->data,
-                                           blob->length - dce_conn->auth_state.auth_info->credentials.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;
-               }
+       call->_out_auth_info = (struct dcerpc_auth) {
+               .auth_type = auth->auth_type,
+               .auth_level = auth->auth_level,
+               .auth_context_id = auth->auth_context_id,
+       };
+       call->out_auth_info = &call->_out_auth_info;
 
-               /* 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;
+       return NT_STATUS_OK;
+}
 
-       case DCERPC_AUTH_LEVEL_CONNECT:
-               break;
+/*
+  check credentials on a packet
+*/
+bool dcesrv_auth_pkt_pull(struct dcesrv_call_state *call,
+                         DATA_BLOB *full_packet,
+                         uint8_t required_flags,
+                         uint8_t optional_flags,
+                         uint8_t payload_offset,
+                         DATA_BLOB *payload_and_verifier)
+{
+       struct ncacn_packet *pkt = &call->pkt;
+       struct dcesrv_auth *auth = call->auth_state;
+       const struct dcerpc_auth tmp_auth = {
+               .auth_type = auth->auth_type,
+               .auth_level = auth->auth_level,
+               .auth_context_id = auth->auth_context_id,
+       };
+       NTSTATUS status;
 
-       default:
-               status = NT_STATUS_INVALID_LEVEL;
-               break;
+       if (!auth->auth_finished) {
+               call->fault_code = DCERPC_NCA_S_PROTO_ERROR;
+               return false;
        }
 
-       data_blob_free(&dce_conn->auth_state.auth_info->credentials);
+       if (auth->auth_invalid) {
+               return false;
+       }
 
+       status = dcerpc_ncacn_pull_pkt_auth(&tmp_auth,
+                                           auth->gensec_security,
+                                           call,
+                                           pkt->ptype,
+                                           required_flags,
+                                           optional_flags,
+                                           payload_offset,
+                                           payload_and_verifier,
+                                           full_packet,
+                                           pkt);
+       if (NT_STATUS_EQUAL(status, NT_STATUS_RPC_PROTOCOL_ERROR)) {
+               call->fault_code = DCERPC_NCA_S_PROTO_ERROR;
+               return false;
+       }
+       if (NT_STATUS_EQUAL(status, NT_STATUS_RPC_UNSUPPORTED_AUTHN_LEVEL)) {
+               call->fault_code = DCERPC_NCA_S_UNSUPPORTED_AUTHN_LEVEL;
+               return false;
+       }
+       if (NT_STATUS_EQUAL(status, NT_STATUS_RPC_SEC_PKG_ERROR)) {
+               call->fault_code = DCERPC_FAULT_SEC_PKG_ERROR;
+               return false;
+       }
+       if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
+               call->fault_code = DCERPC_FAULT_ACCESS_DENIED;
+               return false;
+       }
        if (!NT_STATUS_IS_OK(status)) {
                return false;
-       }       
+       }
 
        return true;
 }
+
+/* 
+   push a signed or sealed dcerpc request packet into a blob
+*/
+bool dcesrv_auth_pkt_push(struct dcesrv_call_state *call,
+                         DATA_BLOB *blob, size_t sig_size,
+                         uint8_t payload_offset,
+                         const DATA_BLOB *payload,
+                         const struct ncacn_packet *pkt)
+{
+       struct dcesrv_auth *auth = call->auth_state;
+       const struct dcerpc_auth tmp_auth = {
+               .auth_type = auth->auth_type,
+               .auth_level = auth->auth_level,
+               .auth_context_id = auth->auth_context_id,
+       };
+       NTSTATUS status;
+
+       status = dcerpc_ncacn_push_pkt_auth(&tmp_auth,
+                                           auth->gensec_security,
+                                           call, blob, sig_size,
+                                           payload_offset,
+                                           payload,
+                                           pkt);
+       return NT_STATUS_IS_OK(status);
+}