Blame SOURCES/krb5-1.12-ktany.patch

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