Blame SOURCES/Continue-after-KDC_ERR_PREAUTH_FAILED.patch

167778
From 64c15ad2b8f4af57ffd998fc27f3781cc02bff29 Mon Sep 17 00:00:00 2001
167778
From: Greg Hudson <ghudson@mit.edu>
167778
Date: Mon, 16 Jan 2017 15:09:32 -0500
167778
Subject: [PATCH] Continue after KDC_ERR_PREAUTH_FAILED
167778
167778
If the KDC sends KDC_ERR_PREAUTH_FAILED, try another mechanism, or
167778
send an unauthenticated request if optimistic preauth failed.
167778
167778
ticket: 8537
167778
(cherry picked from commit 52d2de31bc4728dbc2f59c6033dcdab86da919e9)
167778
---
167778
 src/lib/krb5/krb/get_in_tkt.c | 45 ++++++++++++++++++++++++++++-------
167778
 1 file changed, 36 insertions(+), 9 deletions(-)
167778
167778
diff --git a/src/lib/krb5/krb/get_in_tkt.c b/src/lib/krb5/krb/get_in_tkt.c
167778
index 8d0f964f9..c7d7bfe74 100644
167778
--- a/src/lib/krb5/krb/get_in_tkt.c
167778
+++ b/src/lib/krb5/krb/get_in_tkt.c
167778
@@ -1308,6 +1308,7 @@ init_creds_step_request(krb5_context context,
167778
     krb5_error_code code;
167778
     krb5_preauthtype pa_type;
167778
     struct errinfo save = EMPTY_ERRINFO;
167778
+    uint32_t rcode = (ctx->err_reply == NULL) ? 0 : ctx->err_reply->error;
167778
 
167778
     if (ctx->loopcount >= MAX_IN_TKT_LOOPS) {
167778
         code = KRB5_GET_IN_TKT_LOOP;
167778
@@ -1358,8 +1359,10 @@ init_creds_step_request(krb5_context context,
167778
         TRACE_INIT_CREDS_PREAUTH_MORE(context, ctx->selected_preauth_type);
167778
         code = k5_preauth(context, ctx, ctx->more_padata, TRUE,
167778
                           &ctx->request->padata, &pa_type);
167778
-    } else if (ctx->err_reply != NULL &&
167778
-               ctx->err_reply->error != KDC_ERR_PREAUTH_REQUIRED) {
167778
+    } else if (rcode == KDC_ERR_PREAUTH_FAILED) {
167778
+        /* Report the KDC-side failure code if we can't try another mech. */
167778
+        code = KRB5KDC_ERR_PREAUTH_FAILED;
167778
+    } else if (rcode && rcode != KDC_ERR_PREAUTH_REQUIRED) {
167778
         /* Retrying after an error (possibly mechanism-specific), using error
167778
          * padata to figure out what to change. */
167778
         TRACE_INIT_CREDS_PREAUTH_TRYAGAIN(context, ctx->err_reply->error,
167778
@@ -1380,7 +1383,7 @@ init_creds_step_request(krb5_context context,
167778
 
167778
     if (ctx->request->padata == NULL && ctx->method_padata != NULL) {
167778
         /* Retrying after KDC_ERR_PREAUTH_REQUIRED, or trying again with a
167778
-         * different mechanism after a client-side failure. */
167778
+         * different mechanism after a failure. */
167778
         TRACE_INIT_CREDS_PREAUTH(context);
167778
         code = k5_preauth(context, ctx, ctx->method_padata, TRUE,
167778
                           &ctx->request->padata, &ctx->selected_preauth_type);
167778
@@ -1480,6 +1483,18 @@ is_referral(krb5_context context, krb5_error *err, krb5_principal client)
167778
     return !krb5_realm_compare(context, err->client, client);
167778
 }
167778
 
167778
+/* Transfer error padata to method data in ctx and sort it according to
167778
+ * configuration. */
167778
+static krb5_error_code
167778
+accept_method_data(krb5_context context, krb5_init_creds_context ctx)
167778
+{
167778
+    krb5_free_pa_data(context, ctx->method_padata);
167778
+    ctx->method_padata = ctx->err_padata;
167778
+    ctx->err_padata = NULL;
167778
+    return sort_krb5_padata_sequence(context, &ctx->request->client->realm,
167778
+                                     ctx->method_padata);
167778
+}
167778
+
167778
 static krb5_error_code
167778
 init_creds_step_reply(krb5_context context,
167778
                       krb5_init_creds_context ctx,
167778
@@ -1538,14 +1553,26 @@ init_creds_step_reply(krb5_context context,
167778
             ctx->restarted = FALSE;
167778
             code = restart_init_creds_loop(context, ctx, FALSE);
167778
         } else if (reply_code == KDC_ERR_PREAUTH_REQUIRED && retry) {
167778
-            krb5_free_pa_data(context, ctx->method_padata);
167778
-            ctx->method_padata = ctx->err_padata;
167778
-            ctx->err_padata = NULL;
167778
             note_req_timestamp(context, ctx, ctx->err_reply->stime,
167778
                                ctx->err_reply->susec);
167778
-            code = sort_krb5_padata_sequence(context,
167778
-                                             &ctx->request->client->realm,
167778
-                                             ctx->method_padata);
167778
+            code = accept_method_data(context, ctx);
167778
+        } else if (reply_code == KDC_ERR_PREAUTH_FAILED && retry) {
167778
+            note_req_timestamp(context, ctx, ctx->err_reply->stime,
167778
+                               ctx->err_reply->susec);
167778
+            if (ctx->method_padata == NULL) {
167778
+                /* Optimistic preauth failed on the KDC.  Allow all mechanisms
167778
+                 * to be tried again using method data. */
167778
+                k5_reset_preauth_types_tried(ctx);
167778
+            } else {
167778
+                /* Don't try again with the mechanism that failed. */
167778
+                code = k5_preauth_note_failed(ctx, ctx->selected_preauth_type);
167778
+                if (code)
167778
+                    goto cleanup;
167778
+            }
167778
+            ctx->selected_preauth_type = KRB5_PADATA_NONE;
167778
+            /* Accept or update method data if the KDC sent it. */
167778
+            if (ctx->err_padata != NULL)
167778
+                code = accept_method_data(context, ctx);
167778
         } else if (reply_code == KDC_ERR_MORE_PREAUTH_DATA_REQUIRED && retry) {
167778
             ctx->more_padata = ctx->err_padata;
167778
             ctx->err_padata = NULL;