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