auth/spnego: use better variable names in gensec_spnego_create_negTokenInit()
[vlendec/samba-autobuild/.git] / auth / gensec / spnego.c
index 0f3aae12b2502f8b25f76559f6684e9495aa9a3a..9a2fdbdb48cbe446dd39080291974c8dc4b9e1ed 100644 (file)
@@ -203,241 +203,6 @@ static NTSTATUS gensec_spnego_server_try_fallback(struct gensec_security *gensec
        return NT_STATUS_INVALID_PARAMETER;
 }
 
-/* 
-   Parse the netTokenInit, either from the client, to the server, or
-   from the server to the client.
-*/
-
-static NTSTATUS gensec_spnego_parse_negTokenInit(struct gensec_security *gensec_security,
-                                                struct spnego_state *spnego_state, 
-                                                TALLOC_CTX *out_mem_ctx, 
-                                                struct tevent_context *ev,
-                                                struct spnego_data *spnego_in,
-                                                DATA_BLOB *unwrapped_out)
-{
-       int i;
-       NTSTATUS nt_status = NT_STATUS_INVALID_PARAMETER;
-       const char * const *mechType = NULL;
-       DATA_BLOB unwrapped_in = data_blob_null;
-       bool ok;
-       const struct gensec_security_ops_wrapper *all_sec = NULL;
-
-       if (spnego_in->type != SPNEGO_NEG_TOKEN_INIT) {
-               return NT_STATUS_INTERNAL_ERROR;
-       }
-
-       mechType = spnego_in->negTokenInit.mechTypes;
-       unwrapped_in = spnego_in->negTokenInit.mechToken;
-
-       all_sec = gensec_security_by_oid_list(gensec_security,
-                                             out_mem_ctx, 
-                                             mechType,
-                                             GENSEC_OID_SPNEGO);
-
-       ok = spnego_write_mech_types(spnego_state,
-                                    mechType,
-                                    &spnego_state->mech_types);
-       if (!ok) {
-               DEBUG(1, ("SPNEGO: Failed to write mechTypes\n"));
-               return NT_STATUS_NO_MEMORY;
-       }
-
-       if (spnego_state->state_position == SPNEGO_SERVER_START) {
-               uint32_t j;
-               for (j=0; mechType && mechType[j]; j++) {
-                       for (i=0; all_sec && all_sec[i].op; i++) {
-                               if (strcmp(mechType[j], all_sec[i].oid) != 0) {
-                                       continue;
-                               }
-
-                               nt_status = gensec_subcontext_start(spnego_state,
-                                                                   gensec_security,
-                                                                   &spnego_state->sub_sec_security);
-                               if (!NT_STATUS_IS_OK(nt_status)) {
-                                       return nt_status;
-                               }
-                               /* select the sub context */
-                               nt_status = gensec_start_mech_by_ops(spnego_state->sub_sec_security,
-                                                                    all_sec[i].op);
-                               if (!NT_STATUS_IS_OK(nt_status)) {
-                                       /*
-                                        * Pretend we never started it
-                                        */
-                                       gensec_spnego_update_sub_abort(spnego_state);
-                                       break;
-                               }
-
-                               if (j > 0) {
-                                       /* no optimistic token */
-                                       spnego_state->neg_oid = all_sec[i].oid;
-                                       *unwrapped_out = data_blob_null;
-                                       nt_status = NT_STATUS_MORE_PROCESSING_REQUIRED;
-                                       /*
-                                        * Indicate the downgrade and request a
-                                        * mic.
-                                        */
-                                       spnego_state->downgraded = true;
-                                       spnego_state->mic_requested = true;
-                                       break;
-                               }
-
-                               nt_status = gensec_update_ev(spnego_state->sub_sec_security,
-                                                         out_mem_ctx, 
-                                                         ev,
-                                                         unwrapped_in,
-                                                         unwrapped_out);
-                               if (NT_STATUS_IS_OK(nt_status)) {
-                                       spnego_state->sub_sec_ready = true;
-                               }
-                               if (NT_STATUS_EQUAL(nt_status, NT_STATUS_INVALID_PARAMETER) || 
-                                   NT_STATUS_EQUAL(nt_status, NT_STATUS_CANT_ACCESS_DOMAIN_INFO)) {
-
-                                       DEBUG(1, ("SPNEGO(%s) NEG_TOKEN_INIT failed to parse contents: %s\n", 
-                                                 spnego_state->sub_sec_security->ops->name, nt_errstr(nt_status)));
-
-                                       /*
-                                        * Pretend we never started it
-                                        */
-                                       gensec_spnego_update_sub_abort(spnego_state);
-                                       break;
-                               }
-
-                               spnego_state->neg_oid = all_sec[i].oid;
-                               break;
-                       }
-                       if (spnego_state->sub_sec_security) {
-                               break;
-                       }
-               }
-
-               if (!spnego_state->sub_sec_security) {
-                       DEBUG(1, ("SPNEGO: Could not find a suitable mechtype in NEG_TOKEN_INIT\n"));
-                       return NT_STATUS_INVALID_PARAMETER;
-               }
-       }
-
-       /* Having tried any optimistic token from the client (if we
-        * were the server), if we didn't get anywhere, walk our list
-        * in our preference order */
-       unwrapped_in = data_blob_null;
-
-       if (!spnego_state->sub_sec_security) {
-               for (i=0; all_sec && all_sec[i].op; i++) {
-                       nt_status = gensec_subcontext_start(spnego_state,
-                                                           gensec_security,
-                                                           &spnego_state->sub_sec_security);
-                       if (!NT_STATUS_IS_OK(nt_status)) {
-                               return nt_status;
-                       }
-                       /* select the sub context */
-                       nt_status = gensec_start_mech_by_ops(spnego_state->sub_sec_security,
-                                                            all_sec[i].op);
-                       if (!NT_STATUS_IS_OK(nt_status)) {
-                               /*
-                                * Pretend we never started it.
-                                */
-                               gensec_spnego_update_sub_abort(spnego_state);
-                               continue;
-                       }
-
-                       spnego_state->neg_oid = all_sec[i].oid;
-
-                       /* only get the helping start blob for the first OID */
-                       nt_status = gensec_update_ev(spnego_state->sub_sec_security,
-                                                 out_mem_ctx, 
-                                                 ev,
-                                                 unwrapped_in,
-                                                 unwrapped_out);
-                       if (NT_STATUS_IS_OK(nt_status)) {
-                               spnego_state->sub_sec_ready = true;
-                       }
-
-                       /* it is likely that a NULL input token will
-                        * not be liked by most server mechs, but if
-                        * we are in the client, we want the first
-                        * update packet to be able to abort the use
-                        * of this mech */
-                       if (spnego_state->state_position != SPNEGO_SERVER_START) {
-                               if (NT_STATUS_EQUAL(nt_status, NT_STATUS_INVALID_PARAMETER) || 
-                                   NT_STATUS_EQUAL(nt_status, NT_STATUS_NO_LOGON_SERVERS) ||
-                                   NT_STATUS_EQUAL(nt_status, NT_STATUS_TIME_DIFFERENCE_AT_DC) ||
-                                   NT_STATUS_EQUAL(nt_status, NT_STATUS_CANT_ACCESS_DOMAIN_INFO)) {
-                                       const char *next = NULL;
-                                       const char *principal = NULL;
-                                       int dbg_level = DBGLVL_WARNING;
-
-                                       if (all_sec[i+1].op != NULL) {
-                                               next = all_sec[i+1].op->name;
-                                               dbg_level = DBGLVL_NOTICE;
-                                       }
-
-                                       if (gensec_security->target.principal != NULL) {
-                                               principal = gensec_security->target.principal;
-                                       } else if (gensec_security->target.service != NULL &&
-                                                  gensec_security->target.hostname != NULL)
-                                       {
-                                               principal = talloc_asprintf(spnego_state->sub_sec_security,
-                                                                           "%s/%s",
-                                                                           gensec_security->target.service,
-                                                                           gensec_security->target.hostname);
-                                       } else {
-                                               principal = gensec_security->target.hostname;
-                                       }
-
-                                       DEBUG(dbg_level, ("SPNEGO(%s) creating NEG_TOKEN_INIT for %s failed (next[%s]): %s\n",
-                                                         spnego_state->sub_sec_security->ops->name,
-                                                         principal,
-                                                         next, nt_errstr(nt_status)));
-
-                                       /*
-                                        * Pretend we never started it.
-                                        */
-                                       gensec_spnego_update_sub_abort(spnego_state);
-                                       continue;
-                               }
-                       }
-
-                       break;
-               }
-       }
-
-       if (spnego_state->sub_sec_security) {
-               /* it is likely that a NULL input token will
-                * not be liked by most server mechs, but this
-                * does the right thing in the CIFS client.
-                * just push us along the merry-go-round
-                * again, and hope for better luck next
-                * time */
-
-               if (NT_STATUS_EQUAL(nt_status, NT_STATUS_INVALID_PARAMETER)) {
-                       *unwrapped_out = data_blob_null;
-                       nt_status = NT_STATUS_MORE_PROCESSING_REQUIRED;
-               }
-
-               if (GENSEC_UPDATE_IS_NTERROR(nt_status)) {
-                       DEBUG(1, ("SPNEGO(%s) NEG_TOKEN_INIT failed: %s\n", 
-                                 spnego_state->sub_sec_security->ops->name, nt_errstr(nt_status)));
-
-                       /* We started the mech correctly, and the
-                        * input from the other side was valid.
-                        * Return the error (say bad password, invalid
-                        * ticket) */
-                       gensec_spnego_update_sub_abort(spnego_state);
-                       return nt_status;
-               }
-
-               return nt_status; /* OK or MORE PROCESSING */
-       }
-
-       DEBUG(1, ("SPNEGO: Could not find a suitable mechtype in NEG_TOKEN_INIT\n"));
-       /* we could re-negotiate here, but it would only work
-        * if the client or server lied about what it could
-        * support the first time.  Lets keep this code to
-        * reality */
-
-       return nt_status;
-}
-
 /** create a negTokenInit 
  *
  * This is the same packet, no matter if the client or server sends it first, but it is always the first packet
@@ -448,134 +213,167 @@ static NTSTATUS gensec_spnego_create_negTokenInit(struct gensec_security *gensec
                                                  struct tevent_context *ev,
                                                  DATA_BLOB *out)
 {
-       int i;
-       NTSTATUS nt_status = NT_STATUS_INVALID_PARAMETER;
+       NTSTATUS status;
        const char **mechTypes = NULL;
        DATA_BLOB unwrapped_out = data_blob_null;
+       size_t all_idx = 0;
        const struct gensec_security_ops_wrapper *all_sec;
+       const struct gensec_security_ops_wrapper *cur_sec = NULL;
+       const char **send_mech_types = NULL;
+       struct spnego_data spnego_out;
+       bool ok;
 
        mechTypes = gensec_security_oids(gensec_security, 
                                         out_mem_ctx, GENSEC_OID_SPNEGO);
+       if (mechTypes == NULL) {
+               DBG_WARNING("gensec_security_oids() failed\n");
+               return NT_STATUS_NO_MEMORY;
+       }
 
        all_sec = gensec_security_by_oid_list(gensec_security, 
                                              out_mem_ctx, 
                                              mechTypes,
                                              GENSEC_OID_SPNEGO);
-       for (i=0; all_sec && all_sec[i].op; i++) {
-               struct spnego_data spnego_out;
-               const char **send_mech_types;
-               bool ok;
+       if (all_sec == NULL) {
+               DBG_WARNING("gensec_security_by_oid_list() failed\n");
+               return NT_STATUS_NO_MEMORY;
+       }
 
-               nt_status = gensec_subcontext_start(spnego_state,
-                                                   gensec_security,
-                                                   &spnego_state->sub_sec_security);
-               if (!NT_STATUS_IS_OK(nt_status)) {
-                       return nt_status;
+       for (; all_sec[all_idx].op != NULL; all_idx++) {
+               const char *next = NULL;
+               const char *principal = NULL;
+               int dbg_level = DBGLVL_WARNING;
+
+               cur_sec = &all_sec[all_idx];
+
+               status = gensec_subcontext_start(spnego_state,
+                                                gensec_security,
+                                                &spnego_state->sub_sec_security);
+               if (!NT_STATUS_IS_OK(status)) {
+                       return status;
                }
                /* select the sub context */
-               nt_status = gensec_start_mech_by_ops(spnego_state->sub_sec_security,
-                                                    all_sec[i].op);
-               if (!NT_STATUS_IS_OK(nt_status)) {
+               status = gensec_start_mech_by_ops(spnego_state->sub_sec_security,
+                                                 cur_sec->op);
+               if (!NT_STATUS_IS_OK(status)) {
                        gensec_spnego_update_sub_abort(spnego_state);
                        continue;
                }
 
+               if (spnego_state->state_position != SPNEGO_CLIENT_START) {
+                       /*
+                        * The server doesn't generate an optimistic token.
+                        */
+                       goto reply;
+               }
+
                /* In the client, try and produce the first (optimistic) packet */
-               if (spnego_state->state_position == SPNEGO_CLIENT_START) {
-                       nt_status = gensec_update_ev(spnego_state->sub_sec_security,
-                                                 out_mem_ctx, 
-                                                 ev,
-                                                 data_blob_null,
-                                                 &unwrapped_out);
-                       if (NT_STATUS_IS_OK(nt_status)) {
-                               spnego_state->sub_sec_ready = true;
-                       }
+               status = gensec_update_ev(spnego_state->sub_sec_security,
+                                         out_mem_ctx,
+                                         ev,
+                                         data_blob_null,
+                                         &unwrapped_out);
+               if (NT_STATUS_IS_OK(status)) {
+                       spnego_state->sub_sec_ready = true;
+               }
 
-                       if (GENSEC_UPDATE_IS_NTERROR(nt_status)) {
-                               const char *next = NULL;
-                               const char *principal = NULL;
-                               int dbg_level = DBGLVL_WARNING;
-
-                               if (all_sec[i+1].op != NULL) {
-                                       next = all_sec[i+1].op->name;
-                                       dbg_level = DBGLVL_NOTICE;
-                               }
-
-                               if (gensec_security->target.principal != NULL) {
-                                       principal = gensec_security->target.principal;
-                               } else if (gensec_security->target.service != NULL &&
-                                          gensec_security->target.hostname != NULL)
-                               {
-                                       principal = talloc_asprintf(spnego_state->sub_sec_security,
-                                                                   "%s/%s",
-                                                                   gensec_security->target.service,
-                                                                   gensec_security->target.hostname);
-                               } else {
-                                       principal = gensec_security->target.hostname;
-                               }
-
-                               DEBUG(dbg_level, ("SPNEGO(%s) creating NEG_TOKEN_INIT for %s failed (next[%s]): %s\n",
-                                         spnego_state->sub_sec_security->ops->name,
-                                         principal,
-                                         next, nt_errstr(nt_status)));
+               if (!GENSEC_UPDATE_IS_NTERROR(status)) {
+                       goto reply;
+               }
 
-                               /*
-                                * Pretend we never started it
-                                */
-                               gensec_spnego_update_sub_abort(spnego_state);
-                               continue;
-                       }
+               if (cur_sec[1].op != NULL) {
+                       next = cur_sec[1].op->name;
+                       dbg_level = DBGLVL_NOTICE;
                }
 
-               spnego_out.type = SPNEGO_NEG_TOKEN_INIT;
+               if (gensec_security->target.principal != NULL) {
+                       principal = gensec_security->target.principal;
+               } else if (gensec_security->target.service != NULL &&
+                          gensec_security->target.hostname != NULL)
+               {
+                       principal = talloc_asprintf(spnego_state->sub_sec_security,
+                                                   "%s/%s",
+                                                   gensec_security->target.service,
+                                                   gensec_security->target.hostname);
+               } else {
+                       principal = gensec_security->target.hostname;
+               }
 
-               send_mech_types = gensec_security_oids_from_ops_wrapped(out_mem_ctx,
-                                                                       &all_sec[i]);
+               DBG_PREFIX(dbg_level, (
+                          "%s: creating NEG_TOKEN_INIT for %s failed "
+                          "(next[%s]): %s\n", cur_sec->op->name,
+                          principal, next, nt_errstr(status)));
 
-               ok = spnego_write_mech_types(spnego_state,
-                                            send_mech_types,
-                                            &spnego_state->mech_types);
-               if (!ok) {
-                       DEBUG(1, ("SPNEGO: Failed to write mechTypes\n"));
-                       return NT_STATUS_NO_MEMORY;
+               if (next == NULL) {
+                       /*
+                        * A hard error without a possible fallback.
+                        */
+                       return status;
                }
 
-               /* List the remaining mechs as options */
-               spnego_out.negTokenInit.mechTypes = send_mech_types;
-               spnego_out.negTokenInit.reqFlags = data_blob_null;
-               spnego_out.negTokenInit.reqFlagsPadding = 0;
+               /*
+                * Pretend we never started it
+                */
+               gensec_spnego_update_sub_abort(spnego_state);
+       }
 
-               if (spnego_state->state_position == SPNEGO_SERVER_START) {
-                       spnego_out.negTokenInit.mechListMIC
-                               = data_blob_string_const(ADS_IGNORE_PRINCIPAL);
-               } else {
-                       spnego_out.negTokenInit.mechListMIC = data_blob_null;
-               }
+       DBG_WARNING("Failed to setup SPNEGO negTokenInit request\n");
+       return NT_STATUS_INVALID_PARAMETER;
 
-               spnego_out.negTokenInit.mechToken = unwrapped_out;
+reply:
+       spnego_out.type = SPNEGO_NEG_TOKEN_INIT;
 
-               if (spnego_write_data(out_mem_ctx, out, &spnego_out) == -1) {
-                       DEBUG(1, ("Failed to write NEG_TOKEN_INIT\n"));
-                               return NT_STATUS_INVALID_PARAMETER;
-               }
+       send_mech_types = gensec_security_oids_from_ops_wrapped(out_mem_ctx,
+                                                               cur_sec);
+       if (send_mech_types == NULL) {
+               DBG_WARNING("gensec_security_oids_from_ops_wrapped() failed\n");
+               return NT_STATUS_NO_MEMORY;
+       }
 
-               /* set next state */
-               spnego_state->neg_oid = all_sec[i].oid;
+       ok = spnego_write_mech_types(spnego_state,
+                                    send_mech_types,
+                                    &spnego_state->mech_types);
+       if (!ok) {
+               DBG_ERR("Failed to write mechTypes\n");
+               return NT_STATUS_NO_MEMORY;
+       }
 
-               if (spnego_state->state_position == SPNEGO_SERVER_START) {
-                       spnego_state->state_position = SPNEGO_SERVER_START;
-                       spnego_state->expected_packet = SPNEGO_NEG_TOKEN_INIT;
-               } else {
-                       spnego_state->state_position = SPNEGO_CLIENT_TARG;
-                       spnego_state->expected_packet = SPNEGO_NEG_TOKEN_TARG;
-               }
+       /* List the remaining mechs as options */
+       spnego_out.negTokenInit.mechTypes = send_mech_types;
+       spnego_out.negTokenInit.reqFlags = data_blob_null;
+       spnego_out.negTokenInit.reqFlagsPadding = 0;
 
-               return NT_STATUS_MORE_PROCESSING_REQUIRED;
+       if (spnego_state->state_position == SPNEGO_SERVER_START) {
+               spnego_out.negTokenInit.mechListMIC
+                       = data_blob_string_const(ADS_IGNORE_PRINCIPAL);
+       } else {
+               spnego_out.negTokenInit.mechListMIC = data_blob_null;
        }
-       gensec_spnego_update_sub_abort(spnego_state);
 
-       DEBUG(10, ("Failed to setup SPNEGO negTokenInit request: %s\n", nt_errstr(nt_status)));
-       return nt_status;
+       spnego_out.negTokenInit.mechToken = unwrapped_out;
+
+       if (spnego_write_data(out_mem_ctx, out, &spnego_out) == -1) {
+               DBG_ERR("Failed to write NEG_TOKEN_INIT\n");
+               return NT_STATUS_INVALID_PARAMETER;
+       }
+
+       /*
+        * Note that 'cur_sec' is temporary memory, but
+        * cur_sec->oid points to a const string in the
+        * backends gensec_security_ops structure.
+        */
+       spnego_state->neg_oid = cur_sec->oid;
+
+       /* set next state */
+       if (spnego_state->state_position == SPNEGO_SERVER_START) {
+               spnego_state->state_position = SPNEGO_SERVER_START;
+               spnego_state->expected_packet = SPNEGO_NEG_TOKEN_INIT;
+       } else {
+               spnego_state->state_position = SPNEGO_CLIENT_TARG;
+               spnego_state->expected_packet = SPNEGO_NEG_TOKEN_TARG;
+       }
+
+       return NT_STATUS_MORE_PROCESSING_REQUIRED;
 }
 
 static NTSTATUS gensec_spnego_client_negTokenInit(struct gensec_security *gensec_security,
@@ -585,8 +383,12 @@ static NTSTATUS gensec_spnego_client_negTokenInit(struct gensec_security *gensec
                                                  TALLOC_CTX *out_mem_ctx,
                                                  DATA_BLOB *out)
 {
+       TALLOC_CTX *frame = talloc_stackframe();
        DATA_BLOB sub_out = data_blob_null;
        const char *tp = NULL;
+       const char * const *mech_types = NULL;
+       size_t all_idx = 0;
+       const struct gensec_security_ops_wrapper *all_sec = NULL;
        struct spnego_data spnego_out;
        const char *my_mechs[] = {NULL, NULL};
        NTSTATUS status;
@@ -604,16 +406,122 @@ static NTSTATUS gensec_spnego_client_negTokenInit(struct gensec_security *gensec
                }
        }
 
-       status = gensec_spnego_parse_negTokenInit(gensec_security,
-                                                 spnego_state,
-                                                 out_mem_ctx,
-                                                 ev,
-                                                 spnego_in,
-                                                 &sub_out);
-       if (GENSEC_UPDATE_IS_NTERROR(status)) {
-               return status;
+       mech_types = spnego_in->negTokenInit.mechTypes;
+       if (mech_types == NULL) {
+               TALLOC_FREE(frame);
+               return NT_STATUS_INVALID_PARAMETER;
+       }
+
+       all_sec = gensec_security_by_oid_list(gensec_security,
+                                             frame, mech_types,
+                                             GENSEC_OID_SPNEGO);
+       if (all_sec == NULL) {
+               DBG_WARNING("gensec_security_by_oid_list() failed\n");
+               TALLOC_FREE(frame);
+               return NT_STATUS_INVALID_PARAMETER;
+       }
+
+       for (; all_sec[all_idx].op; all_idx++) {
+               const struct gensec_security_ops_wrapper *cur_sec =
+                       &all_sec[all_idx];
+               const char *next = NULL;
+               const char *principal = NULL;
+               int dbg_level = DBGLVL_WARNING;
+               bool allow_fallback = false;
+
+               status = gensec_subcontext_start(spnego_state,
+                                                gensec_security,
+                                                &spnego_state->sub_sec_security);
+               if (!NT_STATUS_IS_OK(status)) {
+                       TALLOC_FREE(frame);
+                       return status;
+               }
+
+               /* select the sub context */
+               status = gensec_start_mech_by_ops(spnego_state->sub_sec_security,
+                                                 cur_sec->op);
+               if (!NT_STATUS_IS_OK(status)) {
+                       /*
+                        * Pretend we never started it.
+                        */
+                       gensec_spnego_update_sub_abort(spnego_state);
+                       continue;
+               }
+
+               spnego_state->neg_oid = cur_sec->oid;
+
+               /*
+                * As client we don't use an optimistic token from the server.
+                */
+               status = gensec_update_ev(spnego_state->sub_sec_security,
+                                         frame, ev, data_blob_null, &sub_out);
+               if (NT_STATUS_IS_OK(status)) {
+                       spnego_state->sub_sec_ready = true;
+               }
+
+               if (!GENSEC_UPDATE_IS_NTERROR(status)) {
+                       /* OK or MORE_PROCESSING_REQUIRED */
+                       goto reply;
+               }
+
+               /*
+                * it is likely that a NULL input token will
+                * not be liked by most server mechs, but if
+                * we are in the client, we want the first
+                * update packet to be able to abort the use
+                * of this mech
+                */
+               if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER) ||
+                   NT_STATUS_EQUAL(status, NT_STATUS_NO_LOGON_SERVERS) ||
+                   NT_STATUS_EQUAL(status, NT_STATUS_TIME_DIFFERENCE_AT_DC) ||
+                   NT_STATUS_EQUAL(status, NT_STATUS_CANT_ACCESS_DOMAIN_INFO))
+               {
+                       allow_fallback = true;
+               }
+
+               if (allow_fallback && cur_sec[1].op != NULL) {
+                       next = cur_sec[1].op->name;
+                       dbg_level = DBGLVL_NOTICE;
+               }
+
+               if (gensec_security->target.principal != NULL) {
+                       principal = gensec_security->target.principal;
+               } else if (gensec_security->target.service != NULL &&
+                          gensec_security->target.hostname != NULL)
+               {
+                       principal = talloc_asprintf(spnego_state->sub_sec_security,
+                                                   "%s/%s",
+                                                   gensec_security->target.service,
+                                                   gensec_security->target.hostname);
+               } else {
+                       principal = gensec_security->target.hostname;
+               }
+
+               DBG_PREFIX(dbg_level, (
+                          "%s: creating NEG_TOKEN_INIT "
+                          "for %s failed (next[%s]): %s\n",
+                          spnego_state->sub_sec_security->ops->name,
+                          principal, next, nt_errstr(status)));
+
+               if (next == NULL) {
+                       /*
+                        * A hard error without a possible fallback.
+                        */
+                       TALLOC_FREE(frame);
+                       return status;
+               }
+
+               /*
+                * Pretend we never started it.
+                */
+               gensec_spnego_update_sub_abort(spnego_state);
        }
 
+       DBG_WARNING("Could not find a suitable mechtype in NEG_TOKEN_INIT\n");
+       TALLOC_FREE(frame);
+       return NT_STATUS_INVALID_PARAMETER;
+
+ reply:
        my_mechs[0] = spnego_state->neg_oid;
        /* compose reply */
        spnego_out.type = SPNEGO_NEG_TOKEN_INIT;
@@ -625,6 +533,7 @@ static NTSTATUS gensec_spnego_client_negTokenInit(struct gensec_security *gensec
 
        if (spnego_write_data(out_mem_ctx, out, &spnego_out) == -1) {
                DBG_ERR("Failed to write SPNEGO reply to NEG_TOKEN_INIT\n");
+               TALLOC_FREE(frame);
                return NT_STATUS_INVALID_PARAMETER;
        }
 
@@ -633,6 +542,7 @@ static NTSTATUS gensec_spnego_client_negTokenInit(struct gensec_security *gensec
                                     &spnego_state->mech_types);
        if (!ok) {
                DBG_ERR("failed to write mechTypes\n");
+               TALLOC_FREE(frame);
                return NT_STATUS_NO_MEMORY;
        }
 
@@ -640,6 +550,7 @@ static NTSTATUS gensec_spnego_client_negTokenInit(struct gensec_security *gensec
        spnego_state->expected_packet = SPNEGO_NEG_TOKEN_TARG;
        spnego_state->state_position = SPNEGO_CLIENT_TARG;
 
+       TALLOC_FREE(frame);
        return NT_STATUS_MORE_PROCESSING_REQUIRED;
 }
 
@@ -1023,217 +934,285 @@ static NTSTATUS gensec_spnego_server_negTokenInit(struct gensec_security *gensec
                                                  TALLOC_CTX *out_mem_ctx,
                                                  DATA_BLOB *out)
 {
+       TALLOC_CTX *frame = talloc_stackframe();
        DATA_BLOB sub_out = data_blob_null;
        DATA_BLOB mech_list_mic = data_blob_null;
+       const char * const *mech_types = NULL;
+       size_t all_idx = 0;
+       const struct gensec_security_ops_wrapper *all_sec = NULL;
+       size_t mech_idx = 0;
        NTSTATUS status;
+       bool ok;
 
-       status = gensec_spnego_parse_negTokenInit(gensec_security,
-                                                 spnego_state,
-                                                 out_mem_ctx,
-                                                 ev,
-                                                 spnego_in,
-                                                 &sub_out);
+       mech_types = spnego_in->negTokenInit.mechTypes;
+       if (mech_types == NULL) {
+               TALLOC_FREE(frame);
+               return NT_STATUS_INVALID_PARAMETER;
+       }
 
-       if (spnego_state->simulate_w2k) {
-               /*
-                * Windows 2000 returns the unwrapped token
-                * also in the mech_list_mic field.
-                *
-                * In order to verify our client code,
-                * we need a way to have a server with this
-                * broken behaviour
-                */
-               mech_list_mic = sub_out;
+       all_sec = gensec_security_by_oid_list(gensec_security, frame,
+                                             mech_types, GENSEC_OID_SPNEGO);
+       if (all_sec == NULL) {
+               DBG_WARNING("gensec_security_by_oid_list() failed\n");
+               TALLOC_FREE(frame);
+               return NT_STATUS_INVALID_PARAMETER;
        }
 
-       return gensec_spnego_server_response(spnego_state,
-                                            out_mem_ctx,
-                                            status,
-                                            sub_out,
-                                            mech_list_mic,
-                                            out);
-}
+       ok = spnego_write_mech_types(spnego_state, mech_types,
+                                    &spnego_state->mech_types);
+       if (!ok) {
+               DBG_ERR("Failed to write mechTypes\n");
+               TALLOC_FREE(frame);
+               return NT_STATUS_NO_MEMORY;
+       }
 
-static NTSTATUS gensec_spnego_update_client(struct gensec_security *gensec_security,
-                                           TALLOC_CTX *out_mem_ctx,
-                                           struct tevent_context *ev,
-                                           struct spnego_data *spnego_in,
-                                           DATA_BLOB *out)
-{
-       struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
+       /*
+        * First try the preferred mechs from the client.
+        */
+       for (; mech_types[mech_idx]; mech_idx++) {
+               const char *cur_mech = mech_types[mech_idx];
+               const struct gensec_security_ops_wrapper *cur_sec = NULL;
+               DATA_BLOB sub_in = data_blob_null;
+
+               for (all_idx = 0; all_sec[all_idx].op; all_idx++) {
+                       if (strcmp(cur_mech, all_sec[all_idx].oid) == 0) {
+                               cur_sec = &all_sec[all_idx];
+                               break;
+                       }
+               }
 
-       *out = data_blob_null;
+               if (cur_sec == NULL) {
+                       continue;
+               }
 
-       /* and switch into the state machine */
+               status = gensec_subcontext_start(spnego_state,
+                                                gensec_security,
+                                                &spnego_state->sub_sec_security);
+               if (!NT_STATUS_IS_OK(status)) {
+                       TALLOC_FREE(frame);
+                       return status;
+               }
 
-       switch (spnego_state->state_position) {
-       case SPNEGO_CLIENT_START:
-               return gensec_spnego_client_negTokenInit(gensec_security,
-                                                        spnego_state,
-                                                        ev, spnego_in,
-                                                        out_mem_ctx, out);
+               /* select the sub context */
+               status = gensec_start_mech_by_ops(spnego_state->sub_sec_security,
+                                                 cur_sec->op);
+               if (!NT_STATUS_IS_OK(status)) {
+                       /*
+                        * Pretend we never started it
+                        */
+                       gensec_spnego_update_sub_abort(spnego_state);
+                       continue;
+               }
 
-       case SPNEGO_CLIENT_TARG:
-               return gensec_spnego_client_negTokenTarg(gensec_security,
-                                                        spnego_state,
-                                                        ev, spnego_in,
-                                                        out_mem_ctx, out);
+               if (mech_idx > 0) {
+                       /*
+                        * Indicate the downgrade and request a
+                        * mic.
+                        */
+                       spnego_state->downgraded = true;
+                       spnego_state->mic_requested = true;
+                       /* no optimistic token */
+                       spnego_state->neg_oid = cur_sec->oid;
+                       sub_out = data_blob_null;
+                       status = NT_STATUS_MORE_PROCESSING_REQUIRED;
+                       goto reply;
+               }
 
-       default:
-               break;
+               /*
+                * Try the optimistic token from the client
+                */
+               sub_in = spnego_in->negTokenInit.mechToken;
+               status = gensec_update_ev(spnego_state->sub_sec_security,
+                                         frame, ev, sub_in, &sub_out);
+               if (NT_STATUS_IS_OK(status)) {
+                       spnego_state->sub_sec_ready = true;
+               }
+               if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER) ||
+                   NT_STATUS_EQUAL(status, NT_STATUS_CANT_ACCESS_DOMAIN_INFO)) {
+
+                       DBG_WARNING("%s: NEG_TOKEN_INIT failed to parse contents: %s\n",
+                                   cur_sec->op->name, nt_errstr(status));
+
+                       /*
+                        * Pretend we never started it
+                        */
+                       gensec_spnego_update_sub_abort(spnego_state);
+                       continue;
+               }
+
+               if (GENSEC_UPDATE_IS_NTERROR(status)) {
+                       DBG_WARNING("%s: NEG_TOKEN_INIT failed: %s\n",
+                                   cur_sec->op->name, nt_errstr(status));
+                       goto reply;
+               }
+
+               spnego_state->neg_oid = cur_sec->oid;
+               goto reply; /* OK or MORE PROCESSING */
        }
 
-       smb_panic(__location__);
-       return NT_STATUS_INTERNAL_ERROR;
+       DBG_WARNING("Could not find a suitable mechtype in NEG_TOKEN_INIT\n");
+       status = NT_STATUS_INVALID_PARAMETER;
+
+ reply:
+       if (spnego_state->simulate_w2k) {
+               /*
+                * Windows 2000 returns the unwrapped token
+                * also in the mech_list_mic field.
+                *
+                * In order to verify our client code,
+                * we need a way to have a server with this
+                * broken behaviour
+                */
+               mech_list_mic = sub_out;
+       }
+
+       status = gensec_spnego_server_response(spnego_state,
+                                              out_mem_ctx,
+                                              status,
+                                              sub_out,
+                                              mech_list_mic,
+                                              out);
+       TALLOC_FREE(frame);
+       return status;
 }
 
-static NTSTATUS gensec_spnego_update_server(struct gensec_security *gensec_security,
-                                           TALLOC_CTX *out_mem_ctx,
-                                           struct tevent_context *ev,
-                                           struct spnego_data *spnego_in,
-                                           DATA_BLOB *out)
+static NTSTATUS gensec_spnego_server_negTokenTarg(struct gensec_security *gensec_security,
+                                                 struct spnego_state *spnego_state,
+                                                 struct tevent_context *ev,
+                                                 struct spnego_data *spnego_in,
+                                                 TALLOC_CTX *out_mem_ctx,
+                                                 DATA_BLOB *out)
 {
-       struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
+       const struct spnego_negTokenTarg *ta = &spnego_in->negTokenTarg;
+       DATA_BLOB sub_in = ta->responseToken;
        DATA_BLOB mech_list_mic = data_blob_null;
-       DATA_BLOB unwrapped_out = data_blob_null;
-
-       /* and switch into the state machine */
-
-       switch (spnego_state->state_position) {
-       case SPNEGO_SERVER_START:
-               return gensec_spnego_server_negTokenInit(gensec_security,
-                                                        spnego_state,
-                                                        ev, spnego_in,
-                                                        out_mem_ctx, out);
+       DATA_BLOB sub_out = data_blob_null;
+       NTSTATUS status;
+       bool have_sign = true;
+       bool new_spnego = false;
 
-       case SPNEGO_SERVER_TARG:
-       {
-               NTSTATUS nt_status;
-               bool have_sign = true;
-               bool new_spnego = false;
+       spnego_state->num_targs++;
 
-               spnego_state->num_targs++;
+       if (spnego_state->sub_sec_security == NULL) {
+               DBG_ERR("SPNEGO: Did not setup a mech in NEG_TOKEN_INIT\n");
+               return NT_STATUS_INVALID_PARAMETER;
+       }
 
-               if (!spnego_state->sub_sec_security) {
-                       DEBUG(1, ("SPNEGO: Did not setup a mech in NEG_TOKEN_INIT\n"));
+       if (spnego_state->needs_mic_check) {
+               if (ta->responseToken.length != 0) {
+                       DBG_WARNING("non empty response token not expected\n");
                        return NT_STATUS_INVALID_PARAMETER;
                }
 
-               if (spnego_state->needs_mic_check) {
-                       if (spnego_in->negTokenTarg.responseToken.length != 0) {
-                               DEBUG(1, ("SPNEGO: Did not setup a mech in NEG_TOKEN_INIT\n"));
-                               return NT_STATUS_INVALID_PARAMETER;
-                       }
-
-                       nt_status = gensec_check_packet(spnego_state->sub_sec_security,
-                                                       spnego_state->mech_types.data,
-                                                       spnego_state->mech_types.length,
-                                                       spnego_state->mech_types.data,
-                                                       spnego_state->mech_types.length,
-                                                       &spnego_in->negTokenTarg.mechListMIC);
-                       if (NT_STATUS_IS_OK(nt_status)) {
-                               spnego_state->needs_mic_check = false;
-                               spnego_state->done_mic_check = true;
-                       } else {
-                               DEBUG(2,("GENSEC SPNEGO: failed to verify mechListMIC: %s\n",
-                                       nt_errstr(nt_status)));
-                       }
+               status = gensec_check_packet(spnego_state->sub_sec_security,
+                                            spnego_state->mech_types.data,
+                                            spnego_state->mech_types.length,
+                                            spnego_state->mech_types.data,
+                                            spnego_state->mech_types.length,
+                                            &ta->mechListMIC);
+               if (!NT_STATUS_IS_OK(status)) {
+                       DBG_WARNING("failed to verify mechListMIC: %s\n",
+                                   nt_errstr(status));
                        goto server_response;
                }
 
-               if (!spnego_state->sub_sec_ready) {
-                       nt_status = gensec_update_ev(spnego_state->sub_sec_security,
-                                                    out_mem_ctx, ev,
-                                                    spnego_in->negTokenTarg.responseToken,
-                                                    &unwrapped_out);
-                       if (NT_STATUS_IS_OK(nt_status)) {
-                               spnego_state->sub_sec_ready = true;
-                       }
-                       if (!NT_STATUS_IS_OK(nt_status)) {
-                               goto server_response;
-                       }
-               } else {
-                       nt_status = NT_STATUS_OK;
-               }
+               spnego_state->needs_mic_check = false;
+               spnego_state->done_mic_check = true;
+               goto server_response;
+       }
 
-               have_sign = gensec_have_feature(spnego_state->sub_sec_security,
-                                               GENSEC_FEATURE_SIGN);
-               if (spnego_state->simulate_w2k) {
-                       have_sign = false;
-               }
-               new_spnego = gensec_have_feature(spnego_state->sub_sec_security,
-                                                GENSEC_FEATURE_NEW_SPNEGO);
-               if (spnego_in->negTokenTarg.mechListMIC.length > 0) {
-                       new_spnego = true;
+       if (!spnego_state->sub_sec_ready) {
+               status = gensec_update_ev(spnego_state->sub_sec_security,
+                                         out_mem_ctx, ev,
+                                         sub_in, &sub_out);
+               if (NT_STATUS_IS_OK(status)) {
+                       spnego_state->sub_sec_ready = true;
                }
-
-               if (have_sign && new_spnego) {
-                       spnego_state->needs_mic_check = true;
-                       spnego_state->needs_mic_sign = true;
+               if (!NT_STATUS_IS_OK(status)) {
+                       goto server_response;
                }
+       } else {
+               status = NT_STATUS_OK;
+       }
 
-               if (have_sign && spnego_in->negTokenTarg.mechListMIC.length > 0) {
-                       nt_status = gensec_check_packet(spnego_state->sub_sec_security,
-                                                       spnego_state->mech_types.data,
-                                                       spnego_state->mech_types.length,
-                                                       spnego_state->mech_types.data,
-                                                       spnego_state->mech_types.length,
-                                                       &spnego_in->negTokenTarg.mechListMIC);
-                       if (!NT_STATUS_IS_OK(nt_status)) {
-                               DEBUG(2,("GENSEC SPNEGO: failed to verify mechListMIC: %s\n",
-                                       nt_errstr(nt_status)));
-                               goto server_response;
-                       }
+       have_sign = gensec_have_feature(spnego_state->sub_sec_security,
+                                       GENSEC_FEATURE_SIGN);
+       if (spnego_state->simulate_w2k) {
+               have_sign = false;
+       }
+       new_spnego = gensec_have_feature(spnego_state->sub_sec_security,
+                                        GENSEC_FEATURE_NEW_SPNEGO);
+       if (ta->mechListMIC.length > 0) {
+               new_spnego = true;
+       }
 
-                       spnego_state->needs_mic_check = false;
-                       spnego_state->done_mic_check = true;
-               }
-
-               if (spnego_state->needs_mic_sign) {
-                       nt_status = gensec_sign_packet(spnego_state->sub_sec_security,
-                                                      out_mem_ctx,
-                                                      spnego_state->mech_types.data,
-                                                      spnego_state->mech_types.length,
-                                                      spnego_state->mech_types.data,
-                                                      spnego_state->mech_types.length,
-                                                      &mech_list_mic);
-                       if (!NT_STATUS_IS_OK(nt_status)) {
-                               DEBUG(2,("GENSEC SPNEGO: failed to sign mechListMIC: %s\n",
-                                       nt_errstr(nt_status)));
-                               goto server_response;
-                       }
-                       spnego_state->needs_mic_sign = false;
-               }
+       if (have_sign && new_spnego) {
+               spnego_state->needs_mic_check = true;
+               spnego_state->needs_mic_sign = true;
+       }
 
-               if (spnego_state->needs_mic_check) {
-                       nt_status = NT_STATUS_MORE_PROCESSING_REQUIRED;
+       if (have_sign && ta->mechListMIC.length > 0) {
+               status = gensec_check_packet(spnego_state->sub_sec_security,
+                                            spnego_state->mech_types.data,
+                                            spnego_state->mech_types.length,
+                                            spnego_state->mech_types.data,
+                                            spnego_state->mech_types.length,
+                                            &ta->mechListMIC);
+               if (!NT_STATUS_IS_OK(status)) {
+                       DBG_WARNING("failed to verify mechListMIC: %s\n",
+                                   nt_errstr(status));
+                       goto server_response;
                }
 
- server_response:
-               nt_status = gensec_spnego_server_response(spnego_state,
-                                                         out_mem_ctx,
-                                                         nt_status,
-                                                         unwrapped_out,
-                                                         mech_list_mic,
-                                                         out);
+               spnego_state->needs_mic_check = false;
+               spnego_state->done_mic_check = true;
+       }
 
-               return nt_status;
+       if (spnego_state->needs_mic_sign) {
+               status = gensec_sign_packet(spnego_state->sub_sec_security,
+                                           out_mem_ctx,
+                                           spnego_state->mech_types.data,
+                                           spnego_state->mech_types.length,
+                                           spnego_state->mech_types.data,
+                                           spnego_state->mech_types.length,
+                                           &mech_list_mic);
+               if (!NT_STATUS_IS_OK(status)) {
+                       DBG_WARNING("failed to sign mechListMIC: %s\n",
+                                   nt_errstr(status));
+                       return status;
+               }
+               spnego_state->needs_mic_sign = false;
        }
 
-       default:
-               break;
+       if (spnego_state->needs_mic_check) {
+               status = NT_STATUS_MORE_PROCESSING_REQUIRED;
        }
 
-       smb_panic(__location__);
-       return NT_STATUS_INTERNAL_ERROR;
+ server_response:
+       return gensec_spnego_server_response(spnego_state,
+                                            out_mem_ctx,
+                                            status,
+                                            sub_out,
+                                            mech_list_mic,
+                                            out);
 }
 
 struct gensec_spnego_update_state {
+       struct tevent_context *ev;
        struct gensec_security *gensec;
        struct spnego_state *spnego;
+
        DATA_BLOB full_in;
        struct spnego_data _spnego_in;
        struct spnego_data *spnego_in;
+
+       struct {
+               bool needed;
+               DATA_BLOB in;
+               NTSTATUS status;
+               DATA_BLOB out;
+       } sub;
+
        NTSTATUS status;
        DATA_BLOB out;
 };
@@ -1262,6 +1241,9 @@ static void gensec_spnego_update_cleanup(struct tevent_req *req,
 static NTSTATUS gensec_spnego_update_in(struct gensec_security *gensec_security,
                                        const DATA_BLOB in, TALLOC_CTX *mem_ctx,
                                        DATA_BLOB *full_in);
+static void gensec_spnego_update_pre(struct tevent_req *req);
+static void gensec_spnego_update_done(struct tevent_req *subreq);
+static void gensec_spnego_update_post(struct tevent_req *req);
 static NTSTATUS gensec_spnego_update_out(struct gensec_security *gensec_security,
                                         TALLOC_CTX *out_mem_ctx,
                                         DATA_BLOB *_out);
@@ -1284,6 +1266,7 @@ static struct tevent_req *gensec_spnego_update_send(TALLOC_CTX *mem_ctx,
        if (req == NULL) {
                return NULL;
        }
+       state->ev = ev;
        state->gensec = gensec_security;
        state->spnego = spnego_state;
        tevent_req_set_cleanup_fn(req, gensec_spnego_update_cleanup);
@@ -1389,84 +1372,36 @@ static struct tevent_req *gensec_spnego_update_send(TALLOC_CTX *mem_ctx,
                return NULL;
        }
 
-       /* and switch into the state machine */
-
-       switch (spnego_state->state_position) {
-       case SPNEGO_FALLBACK:
-               status = gensec_update_ev(spnego_state->sub_sec_security,
-                                         state, ev,
-                                         state->full_in,
-                                         &spnego_state->out_frag);
-               break;
-
-       case SPNEGO_CLIENT_START:
-               if (state->spnego_in == NULL) {
-                       /* client to produce negTokenInit */
-                       status = gensec_spnego_create_negTokenInit(gensec_security,
-                                                       spnego_state, state, ev,
-                                                       &spnego_state->out_frag);
-                       break;
-               }
-
-               /* fall through */
-       case SPNEGO_CLIENT_TARG:
-               status = gensec_spnego_update_client(gensec_security,
-                                                    state, ev,
-                                                    state->spnego_in,
-                                                    &spnego_state->out_frag);
-               break;
-
-       case SPNEGO_SERVER_START:
-               if (state->spnego_in == NULL) {
-                       /* server to produce negTokenInit */
-                       status = gensec_spnego_create_negTokenInit(gensec_security,
-                                                       spnego_state, state, ev,
-                                                       &spnego_state->out_frag);
-                       break;
-               }
-
-               /* fall through */
-       case SPNEGO_SERVER_TARG:
-               status = gensec_spnego_update_server(gensec_security,
-                                                    state, ev,
-                                                    state->spnego_in,
-                                                    &spnego_state->out_frag);
-               break;
-
-       default:
-               smb_panic(__location__);
-               return NULL;
-       }
-
-       if (GENSEC_UPDATE_IS_NTERROR(status)) {
-               tevent_req_nterror(req, status);
+       gensec_spnego_update_pre(req);
+       if (!tevent_req_is_in_progress(req)) {
                return tevent_req_post(req, ev);
        }
 
-       if (NT_STATUS_IS_OK(status)) {
-               bool reset_full = true;
-
-               reset_full = !spnego_state->done_mic_check;
+       if (state->sub.needed) {
+               struct tevent_req *subreq = NULL;
 
-               status = gensec_may_reset_crypto(spnego_state->sub_sec_security,
-                                                reset_full);
-               if (tevent_req_nterror(req, status)) {
+               /*
+                * We may need one more roundtrip...
+                */
+               subreq = gensec_update_send(state, state->ev,
+                                           spnego_state->sub_sec_security,
+                                           state->sub.in);
+               if (tevent_req_nomem(subreq, req)) {
                        return tevent_req_post(req, ev);
                }
+               tevent_req_set_callback(subreq,
+                                       gensec_spnego_update_done,
+                                       req);
+               state->sub.needed = false;
+               return req;
        }
 
-       spnego_state->out_status = status;
-
-       status = gensec_spnego_update_out(gensec_security,
-                                         state, &state->out);
-       if (GENSEC_UPDATE_IS_NTERROR(status)) {
-               tevent_req_nterror(req, status);
+       gensec_spnego_update_post(req);
+       if (!tevent_req_is_in_progress(req)) {
                return tevent_req_post(req, ev);
        }
 
-       state->status = status;
-       tevent_req_done(req);
-       return tevent_req_post(req, ev);
+       return req;
 }
 
 static NTSTATUS gensec_spnego_update_in(struct gensec_security *gensec_security,
@@ -1577,6 +1512,172 @@ static NTSTATUS gensec_spnego_update_in(struct gensec_security *gensec_security,
        return NT_STATUS_OK;
 }
 
+static void gensec_spnego_update_pre(struct tevent_req *req)
+{
+       struct gensec_spnego_update_state *state =
+               tevent_req_data(req,
+               struct gensec_spnego_update_state);
+       struct gensec_security *gensec_security = state->gensec;
+       struct spnego_state *spnego_state = state->spnego;
+       struct tevent_context *ev = state->ev;
+       NTSTATUS status;
+
+       state->sub.needed = false;
+       state->sub.in = data_blob_null;
+       state->sub.status = NT_STATUS_INTERNAL_ERROR;
+       state->sub.out = data_blob_null;
+
+       if (spnego_state->state_position == SPNEGO_FALLBACK) {
+               state->sub.in = state->full_in;
+               state->full_in = data_blob_null;
+               state->sub.needed = true;
+               return;
+       }
+
+       switch (spnego_state->state_position) {
+       case SPNEGO_CLIENT_START:
+               if (state->spnego_in == NULL) {
+                       /* client to produce negTokenInit */
+                       status = gensec_spnego_create_negTokenInit(gensec_security,
+                                                       spnego_state, state, ev,
+                                                       &spnego_state->out_frag);
+                       if (GENSEC_UPDATE_IS_NTERROR(status)) {
+                               tevent_req_nterror(req, status);
+                               return;
+                       }
+                       break;
+               }
+
+               status = gensec_spnego_client_negTokenInit(gensec_security,
+                                                       spnego_state, ev,
+                                                       state->spnego_in, state,
+                                                       &spnego_state->out_frag);
+               break;
+
+       case SPNEGO_CLIENT_TARG:
+               status = gensec_spnego_client_negTokenTarg(gensec_security,
+                                                       spnego_state, ev,
+                                                       state->spnego_in, state,
+                                                       &spnego_state->out_frag);
+               if (GENSEC_UPDATE_IS_NTERROR(status)) {
+                       tevent_req_nterror(req, status);
+                       return;
+               }
+               break;
+
+       case SPNEGO_SERVER_START:
+               if (state->spnego_in == NULL) {
+                       /* server to produce negTokenInit */
+                       status = gensec_spnego_create_negTokenInit(gensec_security,
+                                                       spnego_state, state, ev,
+                                                       &spnego_state->out_frag);
+                       if (GENSEC_UPDATE_IS_NTERROR(status)) {
+                               tevent_req_nterror(req, status);
+                               return;
+                       }
+                       break;
+               }
+
+               status = gensec_spnego_server_negTokenInit(gensec_security,
+                                                       spnego_state, ev,
+                                                       state->spnego_in, state,
+                                                       &spnego_state->out_frag);
+               if (GENSEC_UPDATE_IS_NTERROR(status)) {
+                       tevent_req_nterror(req, status);
+                       return;
+               }
+               break;
+
+       case SPNEGO_SERVER_TARG:
+               status = gensec_spnego_server_negTokenTarg(gensec_security,
+                                                       spnego_state, ev,
+                                                       state->spnego_in, state,
+                                                       &spnego_state->out_frag);
+               if (GENSEC_UPDATE_IS_NTERROR(status)) {
+                       tevent_req_nterror(req, status);
+                       return;
+               }
+               break;
+
+       default:
+               smb_panic(__location__);
+               return;
+       }
+
+       spnego_state->out_status = status;
+}
+
+static void gensec_spnego_update_done(struct tevent_req *subreq)
+{
+       struct tevent_req *req =
+               tevent_req_callback_data(subreq,
+               struct tevent_req);
+       struct gensec_spnego_update_state *state =
+               tevent_req_data(req,
+               struct gensec_spnego_update_state);
+       struct spnego_state *spnego_state = state->spnego;
+
+       state->sub.status = gensec_update_recv(subreq, state, &state->sub.out);
+       TALLOC_FREE(subreq);
+       if (NT_STATUS_IS_OK(state->sub.status)) {
+               spnego_state->sub_sec_ready = true;
+       }
+
+       gensec_spnego_update_post(req);
+}
+
+static void gensec_spnego_update_post(struct tevent_req *req)
+{
+       struct gensec_spnego_update_state *state =
+               tevent_req_data(req,
+               struct gensec_spnego_update_state);
+       struct spnego_state *spnego_state = state->spnego;
+       NTSTATUS status;
+
+       state->sub.in = data_blob_null;
+       state->sub.needed = false;
+
+       if (spnego_state->state_position == SPNEGO_FALLBACK) {
+               status = state->sub.status;
+               spnego_state->out_frag = state->sub.out;
+               talloc_steal(spnego_state, spnego_state->out_frag.data);
+               state->sub.out = data_blob_null;
+               goto respond;
+       }
+
+       /*
+        * For now just handle the sync processing done
+        * in gensec_spnego_update_pre()
+        */
+       status = spnego_state->out_status;
+
+       if (NT_STATUS_IS_OK(status)) {
+               bool reset_full = true;
+
+               reset_full = !spnego_state->done_mic_check;
+
+               status = gensec_may_reset_crypto(spnego_state->sub_sec_security,
+                                                reset_full);
+               if (tevent_req_nterror(req, status)) {
+                       return;
+               }
+       }
+
+respond:
+       spnego_state->out_status = status;
+
+       status = gensec_spnego_update_out(state->gensec,
+                                         state, &state->out);
+       if (GENSEC_UPDATE_IS_NTERROR(status)) {
+               tevent_req_nterror(req, status);
+               return;
+       }
+
+       state->status = status;
+       tevent_req_done(req);
+       return;
+}
+
 static NTSTATUS gensec_spnego_update_out(struct gensec_security *gensec_security,
                                         TALLOC_CTX *out_mem_ctx,
                                         DATA_BLOB *_out)