Blame SOURCES/krb5-1.12-ktany.patch

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