58aed41
From 259f691fac41a06c238aea1d812b0f3889f06877 Mon Sep 17 00:00:00 2001
14f0285
From: Robbie Harwood <rharwood@redhat.com>
14f0285
Date: Tue, 23 Aug 2016 16:33:53 -0400
32ef372
Subject: [PATCH] krb5-1.12-ktany.patch
14f0285
a89bdde
Adds an "ANY" keytab type which is a list of other keytab locations to search
a89bdde
when searching for a specific entry.  When iterated through, it only presents
a89bdde
the contents of the first keytab.
14f0285
---
14f0285
 src/lib/krb5/keytab/Makefile.in |   3 +
14f0285
 src/lib/krb5/keytab/kt_any.c    | 292 ++++++++++++++++++++++++++++++++++++++++
14f0285
 src/lib/krb5/keytab/ktbase.c    |   7 +-
14f0285
 3 files changed, 301 insertions(+), 1 deletion(-)
14f0285
 create mode 100644 src/lib/krb5/keytab/kt_any.c
a89bdde
14f0285
diff --git a/src/lib/krb5/keytab/Makefile.in b/src/lib/krb5/keytab/Makefile.in
621f3cf
index 2a8fceb00..ffd179fb2 100644
14f0285
--- a/src/lib/krb5/keytab/Makefile.in
14f0285
+++ b/src/lib/krb5/keytab/Makefile.in
14f0285
@@ -12,6 +12,7 @@ STLIBOBJS= \
14f0285
 	ktfr_entry.o	\
14f0285
 	ktremove.o	\
14f0285
 	ktfns.o		\
14f0285
+	kt_any.o	\
14f0285
 	kt_file.o	\
14f0285
 	kt_memory.o	\
14f0285
 	kt_srvtab.o	\
14f0285
@@ -24,6 +25,7 @@ OBJS=	\
14f0285
 	$(OUTPRE)ktfr_entry.$(OBJEXT)	\
14f0285
 	$(OUTPRE)ktremove.$(OBJEXT)	\
14f0285
 	$(OUTPRE)ktfns.$(OBJEXT)	\
14f0285
+	$(OUTPRE)kt_any.$(OBJEXT)	\
14f0285
 	$(OUTPRE)kt_file.$(OBJEXT)	\
14f0285
 	$(OUTPRE)kt_memory.$(OBJEXT)	\
14f0285
 	$(OUTPRE)kt_srvtab.$(OBJEXT)	\
14f0285
@@ -36,6 +38,7 @@ SRCS=	\
14f0285
 	$(srcdir)/ktfr_entry.c	\
14f0285
 	$(srcdir)/ktremove.c	\
14f0285
 	$(srcdir)/ktfns.c	\
14f0285
+	$(srcdir)/kt_any.c	\
14f0285
 	$(srcdir)/kt_file.c	\
14f0285
 	$(srcdir)/kt_memory.c	\
14f0285
 	$(srcdir)/kt_srvtab.c	\
14f0285
diff --git a/src/lib/krb5/keytab/kt_any.c b/src/lib/krb5/keytab/kt_any.c
14f0285
new file mode 100644
621f3cf
index 000000000..1b9b7765b
14f0285
--- /dev/null
14f0285
+++ b/src/lib/krb5/keytab/kt_any.c
a89bdde
@@ -0,0 +1,292 @@
a89bdde
+/*
a89bdde
+ * lib/krb5/keytab/kt_any.c
a89bdde
+ *
a89bdde
+ * Copyright 1998, 1999 by the Massachusetts Institute of Technology.
a89bdde
+ * All Rights Reserved.
a89bdde
+ *
a89bdde
+ * Export of this software from the United States of America may
a89bdde
+ *   require a specific license from the United States Government.
a89bdde
+ *   It is the responsibility of any person or organization contemplating
a89bdde
+ *   export to obtain such a license before exporting.
a89bdde
+ * 
a89bdde
+ * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
a89bdde
+ * distribute this software and its documentation for any purpose and
a89bdde
+ * without fee is hereby granted, provided that the above copyright
a89bdde
+ * notice appear in all copies and that both that copyright notice and
a89bdde
+ * this permission notice appear in supporting documentation, and that
a89bdde
+ * the name of M.I.T. not be used in advertising or publicity pertaining
a89bdde
+ * to distribution of the software without specific, written prior
a89bdde
+ * permission.  M.I.T. makes no representations about the suitability of
a89bdde
+ * this software for any purpose.  It is provided "as is" without express
a89bdde
+ * or implied warranty.
a89bdde
+ * 
a89bdde
+ *
a89bdde
+ * krb5_kta_ops
a89bdde
+ */
a89bdde
+
a89bdde
+#include "k5-int.h"
a89bdde
+
a89bdde
+typedef struct _krb5_ktany_data {
a89bdde
+    char *name;
a89bdde
+    krb5_keytab *choices;
a89bdde
+    int nchoices;
a89bdde
+} krb5_ktany_data;
a89bdde
+
a89bdde
+typedef struct _krb5_ktany_cursor_data {
a89bdde
+    int which;
a89bdde
+    krb5_kt_cursor cursor;
a89bdde
+} krb5_ktany_cursor_data;
a89bdde
+
a89bdde
+static krb5_error_code krb5_ktany_resolve
a89bdde
+	          (krb5_context,
a89bdde
+		   const char *,
a89bdde
+		   krb5_keytab *);
a89bdde
+static krb5_error_code krb5_ktany_get_name
a89bdde
+	          (krb5_context context,
a89bdde
+		   krb5_keytab id,
a89bdde
+		   char *name,
a89bdde
+		   unsigned int len);
a89bdde
+static krb5_error_code krb5_ktany_close
a89bdde
+	          (krb5_context context,
a89bdde
+		   krb5_keytab id);
a89bdde
+static krb5_error_code krb5_ktany_get_entry
a89bdde
+	          (krb5_context context,
a89bdde
+		   krb5_keytab id,
a89bdde
+		   krb5_const_principal principal,
a89bdde
+		   krb5_kvno kvno,
a89bdde
+		   krb5_enctype enctype,
a89bdde
+		   krb5_keytab_entry *entry);
a89bdde
+static krb5_error_code krb5_ktany_start_seq_get
a89bdde
+	          (krb5_context context,
a89bdde
+		   krb5_keytab id,
a89bdde
+		   krb5_kt_cursor *cursorp);
a89bdde
+static krb5_error_code krb5_ktany_next_entry
a89bdde
+	          (krb5_context context,
a89bdde
+		   krb5_keytab id,
a89bdde
+		   krb5_keytab_entry *entry,
a89bdde
+		   krb5_kt_cursor *cursor);
a89bdde
+static krb5_error_code krb5_ktany_end_seq_get
a89bdde
+	          (krb5_context context,
a89bdde
+		   krb5_keytab id,
a89bdde
+		   krb5_kt_cursor *cursor);
a89bdde
+static void cleanup
a89bdde
+	          (krb5_context context,
a89bdde
+		   krb5_ktany_data *data,
a89bdde
+		   int nchoices);
a89bdde
+
a89bdde
+struct _krb5_kt_ops krb5_kta_ops = {
a89bdde
+    0,
a89bdde
+    "ANY", 	/* Prefix -- this string should not appear anywhere else! */
a89bdde
+    krb5_ktany_resolve,
a89bdde
+    krb5_ktany_get_name,
a89bdde
+    krb5_ktany_close,
a89bdde
+    krb5_ktany_get_entry,
a89bdde
+    krb5_ktany_start_seq_get,
a89bdde
+    krb5_ktany_next_entry,
a89bdde
+    krb5_ktany_end_seq_get,
a89bdde
+    NULL,
a89bdde
+    NULL,
a89bdde
+    NULL,
a89bdde
+};
a89bdde
+
a89bdde
+static krb5_error_code
a89bdde
+krb5_ktany_resolve(context, name, id)
a89bdde
+    krb5_context context;
a89bdde
+    const char *name;
a89bdde
+    krb5_keytab *id;
a89bdde
+{
a89bdde
+    const char *p, *q;
a89bdde
+    char *copy;
a89bdde
+    krb5_error_code kerror;
a89bdde
+    krb5_ktany_data *data;
a89bdde
+    int i;
a89bdde
+
a89bdde
+    /* Allocate space for our data and remember a copy of the name. */
a89bdde
+    if ((data = (krb5_ktany_data *)malloc(sizeof(krb5_ktany_data))) == NULL)
a89bdde
+	return(ENOMEM);
a89bdde
+    if ((data->name = (char *)malloc(strlen(name) + 1)) == NULL) {
a89bdde
+	free(data);
a89bdde
+	return(ENOMEM);
a89bdde
+    }
a89bdde
+    strcpy(data->name, name);
a89bdde
+
a89bdde
+    /* Count the number of choices and allocate memory for them. */
a89bdde
+    data->nchoices = 1;
a89bdde
+    for (p = name; (q = strchr(p, ',')) != NULL; p = q + 1)
a89bdde
+	data->nchoices++;
a89bdde
+    if ((data->choices = (krb5_keytab *)
a89bdde
+	 malloc(data->nchoices * sizeof(krb5_keytab))) == NULL) {
a89bdde
+	free(data->name);
a89bdde
+	free(data);
a89bdde
+	return(ENOMEM);
a89bdde
+    }
a89bdde
+
a89bdde
+    /* Resolve each of the choices. */
a89bdde
+    i = 0;
a89bdde
+    for (p = name; (q = strchr(p, ',')) != NULL; p = q + 1) {
a89bdde
+	/* Make a copy of the choice name so we can terminate it. */
a89bdde
+	if ((copy = (char *)malloc(q - p + 1)) == NULL) {
a89bdde
+	    cleanup(context, data, i);
a89bdde
+	    return(ENOMEM);
a89bdde
+	}
a89bdde
+	memcpy(copy, p, q - p);
a89bdde
+	copy[q - p] = 0;
a89bdde
+
a89bdde
+	/* Try resolving the choice name. */
a89bdde
+	kerror = krb5_kt_resolve(context, copy, &data->choices[i]);
a89bdde
+	free(copy);
a89bdde
+	if (kerror) {
a89bdde
+	    cleanup(context, data, i);
a89bdde
+	    return(kerror);
a89bdde
+	}
a89bdde
+	i++;
a89bdde
+    }
a89bdde
+    if ((kerror = krb5_kt_resolve(context, p, &data->choices[i]))) {
a89bdde
+	cleanup(context, data, i);
a89bdde
+	return(kerror);
a89bdde
+    }
a89bdde
+
a89bdde
+    /* Allocate and fill in an ID for the caller. */
a89bdde
+    if ((*id = (krb5_keytab)malloc(sizeof(**id))) == NULL) {
a89bdde
+	cleanup(context, data, i);
a89bdde
+	return(ENOMEM);
a89bdde
+    }
a89bdde
+    (*id)->ops = &krb5_kta_ops;
a89bdde
+    (*id)->data = (krb5_pointer)data;
a89bdde
+    (*id)->magic = KV5M_KEYTAB;
a89bdde
+
a89bdde
+    return(0);
a89bdde
+}
a89bdde
+
a89bdde
+static krb5_error_code
a89bdde
+krb5_ktany_get_name(context, id, name, len)
a89bdde
+    krb5_context context;
a89bdde
+    krb5_keytab id;
a89bdde
+    char *name;
a89bdde
+    unsigned int len;
a89bdde
+{
a89bdde
+    krb5_ktany_data *data = (krb5_ktany_data *)id->data;
a89bdde
+
a89bdde
+    if (len < strlen(data->name) + 1)
a89bdde
+	return(KRB5_KT_NAME_TOOLONG);
a89bdde
+    strcpy(name, data->name);
a89bdde
+    return(0);
a89bdde
+}
a89bdde
+
a89bdde
+static krb5_error_code
a89bdde
+krb5_ktany_close(context, id)
a89bdde
+    krb5_context context;
a89bdde
+    krb5_keytab id;
a89bdde
+{
a89bdde
+    krb5_ktany_data *data = (krb5_ktany_data *)id->data;
a89bdde
+
a89bdde
+    cleanup(context, data, data->nchoices);
a89bdde
+    id->ops = 0;
a89bdde
+    free(id);
a89bdde
+    return(0);
a89bdde
+}
a89bdde
+
a89bdde
+static krb5_error_code
a89bdde
+krb5_ktany_get_entry(context, id, principal, kvno, enctype, entry)
a89bdde
+    krb5_context context;
a89bdde
+    krb5_keytab id;
a89bdde
+    krb5_const_principal principal;
a89bdde
+    krb5_kvno kvno;
a89bdde
+    krb5_enctype enctype;
a89bdde
+    krb5_keytab_entry *entry;
a89bdde
+{
a89bdde
+    krb5_ktany_data *data = (krb5_ktany_data *)id->data;
a89bdde
+    krb5_error_code kerror = KRB5_KT_NOTFOUND;
a89bdde
+    int i;
a89bdde
+
a89bdde
+    for (i = 0; i < data->nchoices; i++) {
a89bdde
+	if ((kerror = krb5_kt_get_entry(context, data->choices[i], principal,
a89bdde
+					kvno, enctype, entry)) != ENOENT)
a89bdde
+	    return kerror;
a89bdde
+    }
a89bdde
+    return kerror;
a89bdde
+}
a89bdde
+
a89bdde
+static krb5_error_code
a89bdde
+krb5_ktany_start_seq_get(context, id, cursorp)
a89bdde
+    krb5_context context;
a89bdde
+    krb5_keytab id;
a89bdde
+    krb5_kt_cursor *cursorp;
a89bdde
+{
a89bdde
+    krb5_ktany_data *data = (krb5_ktany_data *)id->data;
a89bdde
+    krb5_ktany_cursor_data *cdata;
a89bdde
+    krb5_error_code kerror = ENOENT;
a89bdde
+    int i;
a89bdde
+
a89bdde
+    if ((cdata = (krb5_ktany_cursor_data *)
a89bdde
+	 malloc(sizeof(krb5_ktany_cursor_data))) == NULL)
a89bdde
+	return(ENOMEM);
a89bdde
+
a89bdde
+    /* Find a choice which can handle the serialization request. */
a89bdde
+    for (i = 0; i < data->nchoices; i++) {
a89bdde
+	if ((kerror = krb5_kt_start_seq_get(context, data->choices[i],
a89bdde
+					    &cdata->cursor)) == 0)
a89bdde
+	    break;
a89bdde
+	else if (kerror != ENOENT) {
a89bdde
+	    free(cdata);
a89bdde
+	    return(kerror);
a89bdde
+	}
a89bdde
+    }
a89bdde
+
a89bdde
+    if (i == data->nchoices) {
a89bdde
+	/* Everyone returned ENOENT, so no go. */
a89bdde
+	free(cdata);
a89bdde
+	return(kerror);
a89bdde
+    }
a89bdde
+
a89bdde
+    cdata->which = i;
a89bdde
+    *cursorp = (krb5_kt_cursor)cdata;
a89bdde
+    return(0);
a89bdde
+}
a89bdde
+
a89bdde
+static krb5_error_code
a89bdde
+krb5_ktany_next_entry(context, id, entry, cursor)
a89bdde
+    krb5_context context;
a89bdde
+    krb5_keytab id;
a89bdde
+    krb5_keytab_entry *entry;
a89bdde
+    krb5_kt_cursor *cursor;
a89bdde
+{
a89bdde
+    krb5_ktany_data *data = (krb5_ktany_data *)id->data;
a89bdde
+    krb5_ktany_cursor_data *cdata = (krb5_ktany_cursor_data *)*cursor;
a89bdde
+    krb5_keytab choice_id;
a89bdde
+
a89bdde
+    choice_id = data->choices[cdata->which];
a89bdde
+    return(krb5_kt_next_entry(context, choice_id, entry, &cdata->cursor));
a89bdde
+}
a89bdde
+
a89bdde
+static krb5_error_code
a89bdde
+krb5_ktany_end_seq_get(context, id, cursor)
a89bdde
+    krb5_context context;
a89bdde
+    krb5_keytab id;
a89bdde
+    krb5_kt_cursor *cursor;
a89bdde
+{
a89bdde
+    krb5_ktany_data *data = (krb5_ktany_data *)id->data;
a89bdde
+    krb5_ktany_cursor_data *cdata = (krb5_ktany_cursor_data *)*cursor;
a89bdde
+    krb5_keytab choice_id;
a89bdde
+    krb5_error_code kerror;
a89bdde
+
a89bdde
+    choice_id = data->choices[cdata->which];
a89bdde
+    kerror = krb5_kt_end_seq_get(context, choice_id, &cdata->cursor);
a89bdde
+    free(cdata);
a89bdde
+    return(kerror);
a89bdde
+}
a89bdde
+
a89bdde
+static void
a89bdde
+cleanup(context, data, nchoices)
a89bdde
+    krb5_context context;
a89bdde
+    krb5_ktany_data *data;
a89bdde
+    int nchoices;
a89bdde
+{
a89bdde
+    int i;
a89bdde
+
a89bdde
+    free(data->name);
a89bdde
+    for (i = 0; i < nchoices; i++)
a89bdde
+	krb5_kt_close(context, data->choices[i]);
a89bdde
+    free(data->choices);
a89bdde
+    free(data);
a89bdde
+}
14f0285
diff --git a/src/lib/krb5/keytab/ktbase.c b/src/lib/krb5/keytab/ktbase.c
621f3cf
index 0d39b2940..6534d7c52 100644
14f0285
--- a/src/lib/krb5/keytab/ktbase.c
14f0285
+++ b/src/lib/krb5/keytab/ktbase.c
14f0285
@@ -57,14 +57,19 @@ extern const krb5_kt_ops krb5_ktf_ops;
a89bdde
 extern const krb5_kt_ops krb5_ktf_writable_ops;
a89bdde
 extern const krb5_kt_ops krb5_kts_ops;
a89bdde
 extern const krb5_kt_ops krb5_mkt_ops;
a89bdde
+extern const krb5_kt_ops krb5_kta_ops;
a89bdde
 
a89bdde
 struct krb5_kt_typelist {
a89bdde
     const krb5_kt_ops *ops;
a89bdde
     const struct krb5_kt_typelist *next;
a89bdde
 };
a89bdde
+static struct krb5_kt_typelist krb5_kt_typelist_any = {
a89bdde
+    &krb5_kta_ops,
a89bdde
+    NULL
a89bdde
+};
a89bdde
 const static struct krb5_kt_typelist krb5_kt_typelist_srvtab = {
a89bdde
     &krb5_kts_ops,
a89bdde
-    NULL
a89bdde
+    &krb5_kt_typelist_any
a89bdde
 };
a89bdde
 const static struct krb5_kt_typelist krb5_kt_typelist_memory = {
a89bdde
     &krb5_mkt_ops,