Blame SOURCES/krb5-1.12-ktany.patch

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