bc603de
Adds an "ANY" keytab type which is a list of other keytab locations to search
bc603de
when searching for a specific entry.  When iterated through, it only presents
bc603de
the contents of the first keytab.
bc603de
bc603de
diff -up /dev/null krb5-1.7/src/lib/krb5/keytab/kt_any.c
bc603de
--- /dev/null	2009-06-04 10:34:55.169007373 -0400
bc603de
+++ krb5-1.7/src/lib/krb5/keytab/kt_any.c	2009-06-04 13:54:36.000000000 -0400
bc603de
@@ -0,0 +1,292 @@
bc603de
+/*
bc603de
+ * lib/krb5/keytab/kt_any.c
bc603de
+ *
bc603de
+ * Copyright 1998, 1999 by the Massachusetts Institute of Technology.
bc603de
+ * All Rights Reserved.
bc603de
+ *
bc603de
+ * Export of this software from the United States of America may
bc603de
+ *   require a specific license from the United States Government.
bc603de
+ *   It is the responsibility of any person or organization contemplating
bc603de
+ *   export to obtain such a license before exporting.
bc603de
+ * 
bc603de
+ * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
bc603de
+ * distribute this software and its documentation for any purpose and
bc603de
+ * without fee is hereby granted, provided that the above copyright
bc603de
+ * notice appear in all copies and that both that copyright notice and
bc603de
+ * this permission notice appear in supporting documentation, and that
bc603de
+ * the name of M.I.T. not be used in advertising or publicity pertaining
bc603de
+ * to distribution of the software without specific, written prior
bc603de
+ * permission.  M.I.T. makes no representations about the suitability of
bc603de
+ * this software for any purpose.  It is provided "as is" without express
bc603de
+ * or implied warranty.
bc603de
+ * 
bc603de
+ *
bc603de
+ * krb5_kta_ops
bc603de
+ */
bc603de
+
bc603de
+#include "k5-int.h"
bc603de
+
bc603de
+typedef struct _krb5_ktany_data {
bc603de
+    char *name;
bc603de
+    krb5_keytab *choices;
bc603de
+    int nchoices;
bc603de
+} krb5_ktany_data;
bc603de
+
bc603de
+typedef struct _krb5_ktany_cursor_data {
bc603de
+    int which;
bc603de
+    krb5_kt_cursor cursor;
bc603de
+} krb5_ktany_cursor_data;
bc603de
+
bc603de
+static krb5_error_code krb5_ktany_resolve
bc603de
+	          (krb5_context,
bc603de
+		   const char *,
bc603de
+		   krb5_keytab *);
bc603de
+static krb5_error_code krb5_ktany_get_name
bc603de
+	          (krb5_context context,
bc603de
+		   krb5_keytab id,
bc603de
+		   char *name,
bc603de
+		   unsigned int len);
bc603de
+static krb5_error_code krb5_ktany_close
bc603de
+	          (krb5_context context,
bc603de
+		   krb5_keytab id);
bc603de
+static krb5_error_code krb5_ktany_get_entry
bc603de
+	          (krb5_context context,
bc603de
+		   krb5_keytab id,
bc603de
+		   krb5_const_principal principal,
bc603de
+		   krb5_kvno kvno,
bc603de
+		   krb5_enctype enctype,
bc603de
+		   krb5_keytab_entry *entry);
bc603de
+static krb5_error_code krb5_ktany_start_seq_get
bc603de
+	          (krb5_context context,
bc603de
+		   krb5_keytab id,
bc603de
+		   krb5_kt_cursor *cursorp);
bc603de
+static krb5_error_code krb5_ktany_next_entry
bc603de
+	          (krb5_context context,
bc603de
+		   krb5_keytab id,
bc603de
+		   krb5_keytab_entry *entry,
bc603de
+		   krb5_kt_cursor *cursor);
bc603de
+static krb5_error_code krb5_ktany_end_seq_get
bc603de
+	          (krb5_context context,
bc603de
+		   krb5_keytab id,
bc603de
+		   krb5_kt_cursor *cursor);
bc603de
+static void cleanup
bc603de
+	          (krb5_context context,
bc603de
+		   krb5_ktany_data *data,
bc603de
+		   int nchoices);
bc603de
+
bc603de
+struct _krb5_kt_ops krb5_kta_ops = {
bc603de
+    0,
bc603de
+    "ANY", 	/* Prefix -- this string should not appear anywhere else! */
bc603de
+    krb5_ktany_resolve,
bc603de
+    krb5_ktany_get_name,
bc603de
+    krb5_ktany_close,
bc603de
+    krb5_ktany_get_entry,
bc603de
+    krb5_ktany_start_seq_get,
bc603de
+    krb5_ktany_next_entry,
bc603de
+    krb5_ktany_end_seq_get,
ecf57bb
+    NULL,
ecf57bb
+    NULL,
ecf57bb
+    NULL,
bc603de
+};
bc603de
+
bc603de
+static krb5_error_code
bc603de
+krb5_ktany_resolve(context, name, id)
bc603de
+    krb5_context context;
bc603de
+    const char *name;
bc603de
+    krb5_keytab *id;
bc603de
+{
bc603de
+    const char *p, *q;
bc603de
+    char *copy;
bc603de
+    krb5_error_code kerror;
bc603de
+    krb5_ktany_data *data;
bc603de
+    int i;
bc603de
+
bc603de
+    /* Allocate space for our data and remember a copy of the name. */
bc603de
+    if ((data = (krb5_ktany_data *)malloc(sizeof(krb5_ktany_data))) == NULL)
bc603de
+	return(ENOMEM);
bc603de
+    if ((data->name = (char *)malloc(strlen(name) + 1)) == NULL) {
bc603de
+	krb5_xfree(data);
bc603de
+	return(ENOMEM);
bc603de
+    }
bc603de
+    strcpy(data->name, name);
bc603de
+
bc603de
+    /* Count the number of choices and allocate memory for them. */
bc603de
+    data->nchoices = 1;
bc603de
+    for (p = name; (q = strchr(p, ',')) != NULL; p = q + 1)
bc603de
+	data->nchoices++;
bc603de
+    if ((data->choices = (krb5_keytab *)
bc603de
+	 malloc(data->nchoices * sizeof(krb5_keytab))) == NULL) {
bc603de
+	krb5_xfree(data->name);
bc603de
+	krb5_xfree(data);
bc603de
+	return(ENOMEM);
bc603de
+    }
bc603de
+
bc603de
+    /* Resolve each of the choices. */
bc603de
+    i = 0;
bc603de
+    for (p = name; (q = strchr(p, ',')) != NULL; p = q + 1) {
bc603de
+	/* Make a copy of the choice name so we can terminate it. */
bc603de
+	if ((copy = (char *)malloc(q - p + 1)) == NULL) {
bc603de
+	    cleanup(context, data, i);
bc603de
+	    return(ENOMEM);
bc603de
+	}
bc603de
+	memcpy(copy, p, q - p);
bc603de
+	copy[q - p] = 0;
bc603de
+
bc603de
+	/* Try resolving the choice name. */
bc603de
+	kerror = krb5_kt_resolve(context, copy, &data->choices[i]);
bc603de
+	krb5_xfree(copy);
bc603de
+	if (kerror) {
bc603de
+	    cleanup(context, data, i);
bc603de
+	    return(kerror);
bc603de
+	}
bc603de
+	i++;
bc603de
+    }
bc603de
+    if ((kerror = krb5_kt_resolve(context, p, &data->choices[i]))) {
bc603de
+	cleanup(context, data, i);
bc603de
+	return(kerror);
bc603de
+    }
bc603de
+
bc603de
+    /* Allocate and fill in an ID for the caller. */
bc603de
+    if ((*id = (krb5_keytab)malloc(sizeof(**id))) == NULL) {
bc603de
+	cleanup(context, data, i);
bc603de
+	return(ENOMEM);
bc603de
+    }
bc603de
+    (*id)->ops = &krb5_kta_ops;
bc603de
+    (*id)->data = (krb5_pointer)data;
bc603de
+    (*id)->magic = KV5M_KEYTAB;
bc603de
+
bc603de
+    return(0);
bc603de
+}
bc603de
+
bc603de
+static krb5_error_code
bc603de
+krb5_ktany_get_name(context, id, name, len)
bc603de
+    krb5_context context;
bc603de
+    krb5_keytab id;
bc603de
+    char *name;
bc603de
+    unsigned int len;
bc603de
+{
bc603de
+    krb5_ktany_data *data = (krb5_ktany_data *)id->data;
bc603de
+
bc603de
+    if (len < strlen(data->name) + 1)
bc603de
+	return(KRB5_KT_NAME_TOOLONG);
bc603de
+    strcpy(name, data->name);
bc603de
+    return(0);
bc603de
+}
bc603de
+
bc603de
+static krb5_error_code
bc603de
+krb5_ktany_close(context, id)
bc603de
+    krb5_context context;
bc603de
+    krb5_keytab id;
bc603de
+{
bc603de
+    krb5_ktany_data *data = (krb5_ktany_data *)id->data;
bc603de
+
bc603de
+    cleanup(context, data, data->nchoices);
bc603de
+    id->ops = 0;
bc603de
+    krb5_xfree(id);
bc603de
+    return(0);
bc603de
+}
bc603de
+
bc603de
+static krb5_error_code
bc603de
+krb5_ktany_get_entry(context, id, principal, kvno, enctype, entry)
bc603de
+    krb5_context context;
bc603de
+    krb5_keytab id;
bc603de
+    krb5_const_principal principal;
bc603de
+    krb5_kvno kvno;
bc603de
+    krb5_enctype enctype;
bc603de
+    krb5_keytab_entry *entry;
bc603de
+{
bc603de
+    krb5_ktany_data *data = (krb5_ktany_data *)id->data;
bc603de
+    krb5_error_code kerror = KRB5_KT_NOTFOUND;
bc603de
+    int i;
bc603de
+
bc603de
+    for (i = 0; i < data->nchoices; i++) {
bc603de
+	if ((kerror = krb5_kt_get_entry(context, data->choices[i], principal,
bc603de
+					kvno, enctype, entry)) != ENOENT)
bc603de
+	    return kerror;
bc603de
+    }
bc603de
+    return kerror;
bc603de
+}
bc603de
+
bc603de
+static krb5_error_code
bc603de
+krb5_ktany_start_seq_get(context, id, cursorp)
bc603de
+    krb5_context context;
bc603de
+    krb5_keytab id;
bc603de
+    krb5_kt_cursor *cursorp;
bc603de
+{
bc603de
+    krb5_ktany_data *data = (krb5_ktany_data *)id->data;
bc603de
+    krb5_ktany_cursor_data *cdata;
bc603de
+    krb5_error_code kerror = ENOENT;
bc603de
+    int i;
bc603de
+
bc603de
+    if ((cdata = (krb5_ktany_cursor_data *)
bc603de
+	 malloc(sizeof(krb5_ktany_cursor_data))) == NULL)
bc603de
+	return(ENOMEM);
bc603de
+
bc603de
+    /* Find a choice which can handle the serialization request. */
bc603de
+    for (i = 0; i < data->nchoices; i++) {
bc603de
+	if ((kerror = krb5_kt_start_seq_get(context, data->choices[i],
bc603de
+					    &cdata->cursor)) == 0)
bc603de
+	    break;
bc603de
+	else if (kerror != ENOENT) {
bc603de
+	    krb5_xfree(cdata);
bc603de
+	    return(kerror);
bc603de
+	}
bc603de
+    }
bc603de
+
bc603de
+    if (i == data->nchoices) {
bc603de
+	/* Everyone returned ENOENT, so no go. */
bc603de
+	krb5_xfree(cdata);
bc603de
+	return(kerror);
bc603de
+    }
bc603de
+
bc603de
+    cdata->which = i;
bc603de
+    *cursorp = (krb5_kt_cursor)cdata;
bc603de
+    return(0);
bc603de
+}
bc603de
+
bc603de
+static krb5_error_code
bc603de
+krb5_ktany_next_entry(context, id, entry, cursor)
bc603de
+    krb5_context context;
bc603de
+    krb5_keytab id;
bc603de
+    krb5_keytab_entry *entry;
bc603de
+    krb5_kt_cursor *cursor;
bc603de
+{
bc603de
+    krb5_ktany_data *data = (krb5_ktany_data *)id->data;
bc603de
+    krb5_ktany_cursor_data *cdata = (krb5_ktany_cursor_data *)*cursor;
bc603de
+    krb5_keytab choice_id;
bc603de
+
bc603de
+    choice_id = data->choices[cdata->which];
bc603de
+    return(krb5_kt_next_entry(context, choice_id, entry, &cdata->cursor));
bc603de
+}
bc603de
+
bc603de
+static krb5_error_code
bc603de
+krb5_ktany_end_seq_get(context, id, cursor)
bc603de
+    krb5_context context;
bc603de
+    krb5_keytab id;
bc603de
+    krb5_kt_cursor *cursor;
bc603de
+{
bc603de
+    krb5_ktany_data *data = (krb5_ktany_data *)id->data;
bc603de
+    krb5_ktany_cursor_data *cdata = (krb5_ktany_cursor_data *)*cursor;
bc603de
+    krb5_keytab choice_id;
bc603de
+    krb5_error_code kerror;
bc603de
+
bc603de
+    choice_id = data->choices[cdata->which];
bc603de
+    kerror = krb5_kt_end_seq_get(context, choice_id, &cdata->cursor);
bc603de
+    krb5_xfree(cdata);
bc603de
+    return(kerror);
bc603de
+}
bc603de
+
bc603de
+static void
bc603de
+cleanup(context, data, nchoices)
bc603de
+    krb5_context context;
bc603de
+    krb5_ktany_data *data;
bc603de
+    int nchoices;
bc603de
+{
bc603de
+    int i;
bc603de
+
bc603de
+    krb5_xfree(data->name);
bc603de
+    for (i = 0; i < nchoices; i++)
bc603de
+	krb5_kt_close(context, data->choices[i]);
bc603de
+    krb5_xfree(data->choices);
bc603de
+    krb5_xfree(data);
bc603de
+}
bc603de
diff -up krb5-1.7/src/lib/krb5/keytab/ktbase.c krb5-1.7/src/lib/krb5/keytab/ktbase.c
bc603de
--- krb5-1.7/src/lib/krb5/keytab/ktbase.c	2009-02-18 13:18:56.000000000 -0500
bc603de
+++ krb5-1.7/src/lib/krb5/keytab/ktbase.c	2009-06-04 13:54:36.000000000 -0400
bc603de
@@ -59,14 +59,19 @@ extern const krb5_kt_ops krb5_ktf_ops;
bc603de
 extern const krb5_kt_ops krb5_ktf_writable_ops;
bc603de
 extern const krb5_kt_ops krb5_kts_ops;
bc603de
 extern const krb5_kt_ops krb5_mkt_ops;
bc603de
+extern const krb5_kt_ops krb5_kta_ops;
bc603de
 
bc603de
 struct krb5_kt_typelist {
bc603de
     const krb5_kt_ops *ops;
bc603de
     const struct krb5_kt_typelist *next;
bc603de
 };
bc603de
+static struct krb5_kt_typelist krb5_kt_typelist_any = {
bc603de
+    &krb5_kta_ops,
bc603de
+    NULL
bc603de
+};
bc603de
 const static struct krb5_kt_typelist krb5_kt_typelist_srvtab = {
bc603de
     &krb5_kts_ops,
bc603de
-    NULL
bc603de
+    &krb5_kt_typelist_any
bc603de
 };
bc603de
 const static struct krb5_kt_typelist krb5_kt_typelist_memory = {
bc603de
     &krb5_mkt_ops,
bc603de
diff -up krb5-1.7/src/lib/krb5/keytab/Makefile.in krb5-1.7/src/lib/krb5/keytab/Makefile.in
bc603de
--- krb5-1.7/src/lib/krb5/keytab/Makefile.in	2009-01-05 15:27:53.000000000 -0500
bc603de
+++ krb5-1.7/src/lib/krb5/keytab/Makefile.in	2009-06-04 13:54:36.000000000 -0400
bc603de
@@ -19,6 +19,7 @@ STLIBOBJS= \
bc603de
 	ktfr_entry.o	\
bc603de
 	ktremove.o	\
bc603de
 	ktfns.o		\
bc603de
+	kt_any.o	\
bc603de
 	kt_file.o	\
bc603de
 	kt_memory.o	\
bc603de
 	kt_srvtab.o	\
bc603de
@@ -31,6 +32,7 @@ OBJS=	\
bc603de
 	$(OUTPRE)ktfr_entry.$(OBJEXT)	\
bc603de
 	$(OUTPRE)ktremove.$(OBJEXT)	\
bc603de
 	$(OUTPRE)ktfns.$(OBJEXT)	\
bc603de
+	$(OUTPRE)kt_any.$(OBJEXT)	\
bc603de
 	$(OUTPRE)kt_file.$(OBJEXT)	\
bc603de
 	$(OUTPRE)kt_memory.$(OBJEXT)	\
bc603de
 	$(OUTPRE)kt_srvtab.$(OBJEXT)	\
bc603de
@@ -43,6 +45,7 @@ SRCS=	\
bc603de
 	$(srcdir)/ktfr_entry.c	\
bc603de
 	$(srcdir)/ktremove.c	\
bc603de
 	$(srcdir)/ktfns.c	\
bc603de
+	$(srcdir)/kt_any.c	\
bc603de
 	$(srcdir)/kt_file.c	\
bc603de
 	$(srcdir)/kt_memory.c	\
bc603de
 	$(srcdir)/kt_srvtab.c	\