Blame SOURCES/Add-KDC-policy-pluggable-interface.patch

e58a44
From f12b57979012f93b339982ba335093d7c0d364f7 Mon Sep 17 00:00:00 2001
e58a44
From: Robbie Harwood <rharwood@redhat.com>
e58a44
Date: Tue, 27 Jun 2017 17:15:39 -0400
e58a44
Subject: [PATCH] Add KDC policy pluggable interface
e58a44
e58a44
Add the header include/krb5/kdcpolicy_plugin.h, defining a pluggable
e58a44
interface for modules to deny AS and TGS requests and set maximum
e58a44
ticket lifetimes.  This interface replaces the policy.c stub functions.
e58a44
e58a44
Add check_kdcpolicy_as() and check_kdcpolicy_tgs() as entry functions.
e58a44
Call them after auth indicators and ticket lifetimes have been
e58a44
determined.
e58a44
e58a44
Add a test module and a test script with basic kdcpolicy tests.  Add
e58a44
plugin interface documentation in doc/plugindev/policy.rst.
e58a44
e58a44
Also authored by Matt Rogers <mrogers@redhat.com>.
e58a44
e58a44
ticket: 8606 (new)
e58a44
(cherry picked from commit d0969f6a8170344031ef58fd2a161190f1edfb96)
e58a44
[rharwood@redhat.com: mention but do not use kadm_auth]
e58a44
Signed-off-by: Robbie Harwood <rharwood@redhat.com>
e58a44
---
e58a44
 doc/plugindev/index.rst                        |   1 +
e58a44
 doc/plugindev/kdcpolicy.rst                    |  24 +++
e58a44
 src/Makefile.in                                |   1 +
e58a44
 src/configure.in                               |   1 +
e58a44
 src/include/Makefile.in                        |   1 +
e58a44
 src/include/k5-int.h                           |   4 +-
e58a44
 src/include/k5-trace.h                         |   5 +
e58a44
 src/include/krb5/kdcpolicy_plugin.h            | 128 ++++++++++++
e58a44
 src/kdc/do_as_req.c                            |   7 +
e58a44
 src/kdc/do_tgs_req.c                           |   6 +
e58a44
 src/kdc/kdc_util.c                             |   7 -
e58a44
 src/kdc/kdc_util.h                             |  11 -
e58a44
 src/kdc/main.c                                 |   8 +
e58a44
 src/kdc/policy.c                               | 267 +++++++++++++++++++++----
e58a44
 src/kdc/policy.h                               |  19 +-
e58a44
 src/kdc/tgs_policy.c                           |   6 -
e58a44
 src/lib/krb5/krb/plugin.c                      |   4 +-
e58a44
 src/plugins/kdcpolicy/test/Makefile.in         |  20 ++
e58a44
 src/plugins/kdcpolicy/test/deps                |   0
e58a44
 src/plugins/kdcpolicy/test/main.c              | 111 ++++++++++
e58a44
 src/plugins/kdcpolicy/test/policy_test.exports |   1 +
e58a44
 src/tests/Makefile.in                          |   1 +
e58a44
 src/tests/t_kdcpolicy.py                       |  57 ++++++
e58a44
 23 files changed, 616 insertions(+), 74 deletions(-)
e58a44
 create mode 100644 doc/plugindev/kdcpolicy.rst
e58a44
 create mode 100644 src/include/krb5/kdcpolicy_plugin.h
e58a44
 create mode 100644 src/plugins/kdcpolicy/test/Makefile.in
e58a44
 create mode 100644 src/plugins/kdcpolicy/test/deps
e58a44
 create mode 100644 src/plugins/kdcpolicy/test/main.c
e58a44
 create mode 100644 src/plugins/kdcpolicy/test/policy_test.exports
e58a44
 create mode 100644 src/tests/t_kdcpolicy.py
e58a44
e58a44
diff --git a/doc/plugindev/index.rst b/doc/plugindev/index.rst
e58a44
index 67dbc2790..0a012b82b 100644
e58a44
--- a/doc/plugindev/index.rst
e58a44
+++ b/doc/plugindev/index.rst
e58a44
@@ -32,5 +32,6 @@ Contents
e58a44
    gssapi.rst
e58a44
    internal.rst
e58a44
    certauth.rst
e58a44
+   kdcpolicy.rst
e58a44
 
e58a44
 .. TODO: GSSAPI mechanism plugins
e58a44
diff --git a/doc/plugindev/kdcpolicy.rst b/doc/plugindev/kdcpolicy.rst
e58a44
new file mode 100644
e58a44
index 000000000..74f21f08f
e58a44
--- /dev/null
e58a44
+++ b/doc/plugindev/kdcpolicy.rst
e58a44
@@ -0,0 +1,24 @@
e58a44
+.. _kdcpolicy_plugin:
e58a44
+
e58a44
+KDC policy interface (kdcpolicy)
e58a44
+================================
e58a44
+
e58a44
+The kdcpolicy interface was first introduced in release 1.16.  It
e58a44
+allows modules to veto otherwise valid AS and TGS requests or restrict
e58a44
+the lifetime and renew time of the resulting ticket.  For a detailed
e58a44
+description of the kdcpolicy interface, see the header file
e58a44
+``<krb5/kdcpolicy_plugin.h>``.
e58a44
+
e58a44
+The optional **check_as** and **check_tgs** functions allow the module
e58a44
+to perform access control.  Additionally, a module can create and
e58a44
+destroy module data with the **init** and **fini** methods.  Module
e58a44
+data objects last for the lifetime of the KDC process, and are
e58a44
+provided to all other methods.  The data has the type
e58a44
+krb5_kdcpolicy_moddata, which should be cast to the appropriate
e58a44
+internal type.
e58a44
+
e58a44
+kdcpolicy modules can optionally inspect principal entries.  To do
e58a44
+this, the module must also include ``<kdb.h>`` to gain access to the
e58a44
+principal entry structure definition.  As the KDB interface is
e58a44
+explicitly not as stable as other public interfaces, modules which do
e58a44
+this may not retain compatibility across releases.
e58a44
diff --git a/src/Makefile.in b/src/Makefile.in
e58a44
index ad8565056..e47bddcb1 100644
e58a44
--- a/src/Makefile.in
e58a44
+++ b/src/Makefile.in
e58a44
@@ -21,6 +21,7 @@ SUBDIRS=util include lib \
e58a44
 	plugins/kdb/db2 \
e58a44
 	@ldap_plugin_dir@ \
e58a44
 	plugins/kdb/test \
e58a44
+	plugins/kdcpolicy/test \
e58a44
 	plugins/preauth/otp \
e58a44
 	plugins/preauth/pkinit \
e58a44
 	plugins/preauth/test \
e58a44
diff --git a/src/configure.in b/src/configure.in
e58a44
index 4ae2c07d5..ee1983043 100644
e58a44
--- a/src/configure.in
e58a44
+++ b/src/configure.in
e58a44
@@ -1470,6 +1470,7 @@ dnl	ccapi ccapi/lib ccapi/lib/unix ccapi/server ccapi/server/unix ccapi/test
e58a44
 	plugins/kdb/db2/libdb2/recno
e58a44
 	plugins/kdb/db2/libdb2/test
e58a44
 	plugins/kdb/test
e58a44
+	plugins/kdcpolicy/test
e58a44
 	plugins/preauth/otp
e58a44
 	plugins/preauth/test
e58a44
 	plugins/authdata/greet_client
e58a44
diff --git a/src/include/Makefile.in b/src/include/Makefile.in
e58a44
index 0239338a1..6a3fa8242 100644
e58a44
--- a/src/include/Makefile.in
e58a44
+++ b/src/include/Makefile.in
e58a44
@@ -144,6 +144,7 @@ install-headers-unix install: krb5/krb5.h profile.h
e58a44
 	$(INSTALL_DATA) $(srcdir)/krb5/ccselect_plugin.h $(DESTDIR)$(KRB5_INCDIR)$(S)krb5$(S)ccselect_plugin.h
e58a44
 	$(INSTALL_DATA) $(srcdir)/krb5/clpreauth_plugin.h $(DESTDIR)$(KRB5_INCDIR)$(S)krb5$(S)clpreauth_plugin.h
e58a44
 	$(INSTALL_DATA) $(srcdir)/krb5/hostrealm_plugin.h $(DESTDIR)$(KRB5_INCDIR)$(S)krb5$(S)hostrealm_plugin.h
e58a44
+	$(INSTALL_DATA) $(srcdir)/krb5/kdcpolicy_plugin.h $(DESTDIR)$(KRB5_INCDIR)$(S)krb5$(S)kdcpolicy_plugin.h
e58a44
 	$(INSTALL_DATA) $(srcdir)/krb5/kdcpreauth_plugin.h $(DESTDIR)$(KRB5_INCDIR)$(S)krb5$(S)kdcpreauth_plugin.h
e58a44
 	$(INSTALL_DATA) $(srcdir)/krb5/localauth_plugin.h $(DESTDIR)$(KRB5_INCDIR)$(S)krb5$(S)localauth_plugin.h
e58a44
 	$(INSTALL_DATA) $(srcdir)/krb5/locate_plugin.h $(DESTDIR)$(KRB5_INCDIR)$(S)krb5$(S)locate_plugin.h
e58a44
diff --git a/src/include/k5-int.h b/src/include/k5-int.h
e58a44
index ed9c7bf75..39ffb9568 100644
e58a44
--- a/src/include/k5-int.h
e58a44
+++ b/src/include/k5-int.h
e58a44
@@ -1157,7 +1157,9 @@ struct plugin_interface {
e58a44
 #define PLUGIN_INTERFACE_TLS         8
e58a44
 #define PLUGIN_INTERFACE_KDCAUTHDATA 9
e58a44
 #define PLUGIN_INTERFACE_CERTAUTH    10
e58a44
-#define PLUGIN_NUM_INTERFACES        11
e58a44
+#define PLUGIN_INTERFACE_KADM5_AUTH  11
e58a44
+#define PLUGIN_INTERFACE_KDCPOLICY   12
e58a44
+#define PLUGIN_NUM_INTERFACES        13
e58a44
 
e58a44
 /* Retrieve the plugin module of type interface_id and name modname,
e58a44
  * storing the result into module. */
e58a44
diff --git a/src/include/k5-trace.h b/src/include/k5-trace.h
e58a44
index c75e264e0..2885408a2 100644
e58a44
--- a/src/include/k5-trace.h
e58a44
+++ b/src/include/k5-trace.h
e58a44
@@ -454,4 +454,9 @@ void krb5int_trace(krb5_context context, const char *fmt, ...);
e58a44
 #define TRACE_GET_CRED_VIA_TKT_EXT_RETURN(c, ret) \
e58a44
     TRACE(c, "Got cred; {kerr}", ret)
e58a44
 
e58a44
+#define TRACE_KDCPOLICY_VTINIT_FAIL(c, ret)                             \
e58a44
+    TRACE(c, "KDC policy module failed to init vtable: {kerr}", ret)
e58a44
+#define TRACE_KDCPOLICY_INIT_SKIP(c, name)                              \
e58a44
+    TRACE(c, "kadm5_auth module {str} declined to initialize", name)
e58a44
+
e58a44
 #endif /* K5_TRACE_H */
e58a44
diff --git a/src/include/krb5/kdcpolicy_plugin.h b/src/include/krb5/kdcpolicy_plugin.h
e58a44
new file mode 100644
e58a44
index 000000000..c7592c5db
e58a44
--- /dev/null
e58a44
+++ b/src/include/krb5/kdcpolicy_plugin.h
e58a44
@@ -0,0 +1,128 @@
e58a44
+/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
e58a44
+/* include/krb5/kdcpolicy_plugin.h - KDC policy plugin interface */
e58a44
+/*
e58a44
+ * Copyright (C) 2017 by Red Hat, Inc.
e58a44
+ * All rights reserved.
e58a44
+ *
e58a44
+ * Redistribution and use in source and binary forms, with or without
e58a44
+ * modification, are permitted provided that the following conditions
e58a44
+ * are met:
e58a44
+ *
e58a44
+ * * Redistributions of source code must retain the above copyright
e58a44
+ *   notice, this list of conditions and the following disclaimer.
e58a44
+ *
e58a44
+ * * Redistributions in binary form must reproduce the above copyright
e58a44
+ *   notice, this list of conditions and the following disclaimer in
e58a44
+ *   the documentation and/or other materials provided with the
e58a44
+ *   distribution.
e58a44
+ *
e58a44
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
e58a44
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
e58a44
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
e58a44
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
e58a44
+ * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
e58a44
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
e58a44
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
e58a44
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
e58a44
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
e58a44
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
e58a44
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
e58a44
+ * OF THE POSSIBILITY OF SUCH DAMAGE.
e58a44
+ */
e58a44
+
e58a44
+/*
e58a44
+ * Declarations for kdcpolicy plugin module implementors.
e58a44
+ *
e58a44
+ * The kdcpolicy pluggable interface currently has only one supported major
e58a44
+ * version, which is 1.  Major version 1 has a current minor version number of
e58a44
+ * 1.
e58a44
+ *
e58a44
+ * kdcpolicy plugin modules should define a function named
e58a44
+ * kdcpolicy_<modulename>_initvt, matching the signature:
e58a44
+ *
e58a44
+ *   krb5_error_code
e58a44
+ *   kdcpolicy_modname_initvt(krb5_context context, int maj_ver, int min_ver,
e58a44
+ *                            krb5_plugin_vtable vtable);
e58a44
+ *
e58a44
+ * The initvt function should:
e58a44
+ *
e58a44
+ * - Check that the supplied maj_ver number is supported by the module, or
e58a44
+ *   return KRB5_PLUGIN_VER_NOTSUPP if it is not.
e58a44
+ *
e58a44
+ * - Cast the vtable pointer as appropriate for maj_ver:
e58a44
+ *   maj_ver == 1: Cast to krb5_kdcpolicy_vtable
e58a44
+ *
e58a44
+ * - Initialize the methods of the vtable, stopping as appropriate for the
e58a44
+ *   supplied min_ver.  Optional methods may be left uninitialized.
e58a44
+ *
e58a44
+ * Memory for the vtable is allocated by the caller, not by the module.
e58a44
+ */
e58a44
+
e58a44
+#ifndef KRB5_POLICY_PLUGIN_H
e58a44
+#define KRB5_POLICY_PLUGIN_H
e58a44
+
e58a44
+#include <krb5/krb5.h>
e58a44
+
e58a44
+/* Abstract module datatype. */
e58a44
+typedef struct krb5_kdcpolicy_moddata_st *krb5_kdcpolicy_moddata;
e58a44
+
e58a44
+/* A module can optionally include kdb.h to inspect principal entries when
e58a44
+ * authorizing requests. */
e58a44
+struct _krb5_db_entry_new;
e58a44
+
e58a44
+/*
e58a44
+ * Optional: Initialize module data.  Return 0 on success,
e58a44
+ * KRB5_PLUGIN_NO_HANDLE if the module is inoperable (due to configuration, for
e58a44
+ * example), and any other error code to abort KDC startup.  Optionally set
e58a44
+ * *data_out to a module data object to be passed to future calls.
e58a44
+ */
e58a44
+typedef krb5_error_code
e58a44
+(*krb5_kdcpolicy_init_fn)(krb5_context context,
e58a44
+                          krb5_kdcpolicy_moddata *data_out);
e58a44
+
e58a44
+/* Optional: Clean up module data. */
e58a44
+typedef krb5_error_code
e58a44
+(*krb5_kdcpolicy_fini_fn)(krb5_context context,
e58a44
+                          krb5_kdcpolicy_moddata moddata);
e58a44
+
e58a44
+/*
e58a44
+ * Optional: return an error code and set status to an appropriate string
e58a44
+ * literal to deny an AS request; otherwise return 0.  lifetime_out, if set,
e58a44
+ * restricts the ticket lifetime.  renew_lifetime_out, if set, restricts the
e58a44
+ * ticket renewable lifetime.
e58a44
+ */
e58a44
+typedef krb5_error_code
e58a44
+(*krb5_kdcpolicy_check_as_fn)(krb5_context context,
e58a44
+                              krb5_kdcpolicy_moddata moddata,
e58a44
+                              const krb5_kdc_req *request,
e58a44
+                              const struct _krb5_db_entry_new *client,
e58a44
+                              const struct _krb5_db_entry_new *server,
e58a44
+                              const char *const *auth_indicators,
e58a44
+                              const char **status, krb5_deltat *lifetime_out,
e58a44
+                              krb5_deltat *renew_lifetime_out);
e58a44
+
e58a44
+/*
e58a44
+ * Optional: return an error code and set status to an appropriate string
e58a44
+ * literal to deny a TGS request; otherwise return 0.  lifetime_out, if set,
e58a44
+ * restricts the ticket lifetime.  renew_lifetime_out, if set, restricts the
e58a44
+ * ticket renewable lifetime.
e58a44
+ */
e58a44
+typedef krb5_error_code
e58a44
+(*krb5_kdcpolicy_check_tgs_fn)(krb5_context context,
e58a44
+                               krb5_kdcpolicy_moddata moddata,
e58a44
+                               const krb5_kdc_req *request,
e58a44
+                               const struct _krb5_db_entry_new *server,
e58a44
+                               const krb5_ticket *ticket,
e58a44
+                               const char *const *auth_indicators,
e58a44
+                               const char **status, krb5_deltat *lifetime_out,
e58a44
+                               krb5_deltat *renew_lifetime_out);
e58a44
+
e58a44
+typedef struct krb5_kdcpolicy_vtable_st {
e58a44
+    const char *name;
e58a44
+    krb5_kdcpolicy_init_fn init;
e58a44
+    krb5_kdcpolicy_fini_fn fini;
e58a44
+    krb5_kdcpolicy_check_as_fn check_as;
e58a44
+    krb5_kdcpolicy_check_tgs_fn check_tgs;
e58a44
+} *krb5_kdcpolicy_vtable;
e58a44
+
e58a44
+#endif /* KRB5_POLICY_PLUGIN_H */
e58a44
diff --git a/src/kdc/do_as_req.c b/src/kdc/do_as_req.c
e58a44
index 59a39cd30..241b05b40 100644
e58a44
--- a/src/kdc/do_as_req.c
e58a44
+++ b/src/kdc/do_as_req.c
e58a44
@@ -207,6 +207,13 @@ finish_process_as_req(struct as_req_state *state, krb5_error_code errcode)
e58a44
 
e58a44
     state->ticket_reply.enc_part2 = &state->enc_tkt_reply;
e58a44
 
e58a44
+    errcode = check_kdcpolicy_as(kdc_context, state->request, state->client,
e58a44
+                                 state->server, state->auth_indicators,
e58a44
+                                 state->kdc_time, &state->enc_tkt_reply.times,
e58a44
+                                 &state->status);
e58a44
+    if (errcode)
e58a44
+        goto egress;
e58a44
+
e58a44
     /*
e58a44
      * Find the server key
e58a44
      */
e58a44
diff --git a/src/kdc/do_tgs_req.c b/src/kdc/do_tgs_req.c
e58a44
index aacd2f20d..4c722a4a3 100644
e58a44
--- a/src/kdc/do_tgs_req.c
e58a44
+++ b/src/kdc/do_tgs_req.c
e58a44
@@ -518,6 +518,12 @@ process_tgs_req(struct server_handle *handle, krb5_data *pkt,
e58a44
     kdc_get_ticket_renewtime(kdc_active_realm, request, header_enc_tkt, client,
e58a44
                              server, &enc_tkt_reply);
e58a44
 
e58a44
+    errcode = check_kdcpolicy_tgs(kdc_context, request, server, header_ticket,
e58a44
+                                  auth_indicators, kdc_time,
e58a44
+                                  &enc_tkt_reply.times, &status);
e58a44
+    if (errcode)
e58a44
+        goto cleanup;
e58a44
+
e58a44
     /*
e58a44
      * Set authtime to be the same as header or evidence ticket's
e58a44
      */
e58a44
diff --git a/src/kdc/kdc_util.c b/src/kdc/kdc_util.c
e58a44
index 778a629e5..8cbdf2c5b 100644
e58a44
--- a/src/kdc/kdc_util.c
e58a44
+++ b/src/kdc/kdc_util.c
e58a44
@@ -642,7 +642,6 @@ validate_as_request(kdc_realm_t *kdc_active_realm,
e58a44
                     krb5_db_entry server, krb5_timestamp kdc_time,
e58a44
                     const char **status, krb5_pa_data ***e_data)
e58a44
 {
e58a44
-    int errcode;
e58a44
     krb5_error_code ret;
e58a44
 
e58a44
     /*
e58a44
@@ -750,12 +749,6 @@ validate_as_request(kdc_realm_t *kdc_active_realm,
e58a44
     if (ret && ret != KRB5_PLUGIN_OP_NOTSUPP)
e58a44
         return errcode_to_protocol(ret);
e58a44
 
e58a44
-    /* Check against local policy. */
e58a44
-    errcode = against_local_policy_as(request, client, server,
e58a44
-                                      kdc_time, status, e_data);
e58a44
-    if (errcode)
e58a44
-        return errcode;
e58a44
-
e58a44
     return 0;
e58a44
 }
e58a44
 
e58a44
diff --git a/src/kdc/kdc_util.h b/src/kdc/kdc_util.h
e58a44
index 672f94380..dcedfd538 100644
e58a44
--- a/src/kdc/kdc_util.h
e58a44
+++ b/src/kdc/kdc_util.h
e58a44
@@ -166,17 +166,6 @@ kdc_err(krb5_context call_context, errcode_t code, const char *fmt, ...)
e58a44
 #endif
e58a44
     ;
e58a44
 
e58a44
-/* policy.c */
e58a44
-int
e58a44
-against_local_policy_as (krb5_kdc_req *, krb5_db_entry,
e58a44
-                         krb5_db_entry, krb5_timestamp,
e58a44
-                         const char **, krb5_pa_data ***);
e58a44
-
e58a44
-int
e58a44
-against_local_policy_tgs (krb5_kdc_req *, krb5_db_entry,
e58a44
-                          krb5_ticket *, const char **,
e58a44
-                          krb5_pa_data ***);
e58a44
-
e58a44
 /* kdc_preauth.c */
e58a44
 krb5_boolean
e58a44
 enctype_requires_etype_info_2(krb5_enctype enctype);
e58a44
diff --git a/src/kdc/main.c b/src/kdc/main.c
e58a44
index a4dffb29a..ccac3a759 100644
e58a44
--- a/src/kdc/main.c
e58a44
+++ b/src/kdc/main.c
e58a44
@@ -31,6 +31,7 @@
e58a44
 #include "kdc_util.h"
e58a44
 #include "kdc_audit.h"
e58a44
 #include "extern.h"
e58a44
+#include "policy.h"
e58a44
 #include "kdc5_err.h"
e58a44
 #include "kdb_kt.h"
e58a44
 #include "net-server.h"
e58a44
@@ -986,6 +987,12 @@ int main(int argc, char **argv)
e58a44
 
e58a44
     load_preauth_plugins(&shandle, kcontext, ctx);
e58a44
     load_authdata_plugins(kcontext);
e58a44
+    retval = load_kdcpolicy_plugins(kcontext);
e58a44
+    if (retval) {
e58a44
+        kdc_err(kcontext, retval, _("while loading KDC policy plugin"));
e58a44
+        finish_realms();
e58a44
+        return 1;
e58a44
+    }
e58a44
 
e58a44
     retval = setup_sam();
e58a44
     if (retval) {
e58a44
@@ -1068,6 +1075,7 @@ int main(int argc, char **argv)
e58a44
     krb5_klog_syslog(LOG_INFO, _("shutting down"));
e58a44
     unload_preauth_plugins(kcontext);
e58a44
     unload_authdata_plugins(kcontext);
e58a44
+    unload_kdcpolicy_plugins(kcontext);
e58a44
     unload_audit_modules(kcontext);
e58a44
     krb5_klog_close(kcontext);
e58a44
     finish_realms();
e58a44
diff --git a/src/kdc/policy.c b/src/kdc/policy.c
e58a44
index 6cba4303f..e49644e06 100644
e58a44
--- a/src/kdc/policy.c
e58a44
+++ b/src/kdc/policy.c
e58a44
@@ -1,67 +1,246 @@
e58a44
 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
e58a44
 /* kdc/policy.c - Policy decision routines for KDC */
e58a44
 /*
e58a44
- * Copyright 1990 by the Massachusetts Institute of Technology.
e58a44
+ * Copyright (C) 2017 by Red Hat, Inc.
e58a44
+ * All rights reserved.
e58a44
  *
e58a44
- * Export of this software from the United States of America may
e58a44
- *   require a specific license from the United States Government.
e58a44
- *   It is the responsibility of any person or organization contemplating
e58a44
- *   export to obtain such a license before exporting.
e58a44
+ * Redistribution and use in source and binary forms, with or without
e58a44
+ * modification, are permitted provided that the following conditions
e58a44
+ * are met:
e58a44
  *
e58a44
- * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
e58a44
- * distribute this software and its documentation for any purpose and
e58a44
- * without fee is hereby granted, provided that the above copyright
e58a44
- * notice appear in all copies and that both that copyright notice and
e58a44
- * this permission notice appear in supporting documentation, and that
e58a44
- * the name of M.I.T. not be used in advertising or publicity pertaining
e58a44
- * to distribution of the software without specific, written prior
e58a44
- * permission.  Furthermore if you modify this software you must label
e58a44
- * your software as modified software and not distribute it in such a
e58a44
- * fashion that it might be confused with the original M.I.T. software.
e58a44
- * M.I.T. makes no representations about the suitability of
e58a44
- * this software for any purpose.  It is provided "as is" without express
e58a44
- * or implied warranty.
e58a44
+ * * Redistributions of source code must retain the above copyright
e58a44
+ *   notice, this list of conditions and the following disclaimer.
e58a44
+ *
e58a44
+ * * Redistributions in binary form must reproduce the above copyright
e58a44
+ *   notice, this list of conditions and the following disclaimer in
e58a44
+ *   the documentation and/or other materials provided with the
e58a44
+ *   distribution.
e58a44
+ *
e58a44
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
e58a44
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
e58a44
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
e58a44
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
e58a44
+ * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
e58a44
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
e58a44
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
e58a44
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
e58a44
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
e58a44
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
e58a44
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
e58a44
+ * OF THE POSSIBILITY OF SUCH DAMAGE.
e58a44
  */
e58a44
 
e58a44
 #include "k5-int.h"
e58a44
 #include "kdc_util.h"
e58a44
 #include "extern.h"
e58a44
+#include "policy.h"
e58a44
+#include "adm_proto.h"
e58a44
+#include <krb5/kdcpolicy_plugin.h>
e58a44
+#include <syslog.h>
e58a44
 
e58a44
-int
e58a44
-against_local_policy_as(register krb5_kdc_req *request, krb5_db_entry client,
e58a44
-                        krb5_db_entry server, krb5_timestamp kdc_time,
e58a44
-                        const char **status, krb5_pa_data ***e_data)
e58a44
+typedef struct kdcpolicy_handle_st {
e58a44
+    struct krb5_kdcpolicy_vtable_st vt;
e58a44
+    krb5_kdcpolicy_moddata moddata;
e58a44
+} *kdcpolicy_handle;
e58a44
+
e58a44
+static kdcpolicy_handle *handles;
e58a44
+
e58a44
+static void
e58a44
+free_indicators(char **ais)
e58a44
 {
e58a44
-#if 0
e58a44
-    /* An AS request must include the addresses field */
e58a44
-    if (request->addresses == 0) {
e58a44
-        *status = "NO ADDRESS";
e58a44
-        return KRB5KDC_ERR_POLICY;
e58a44
-    }
e58a44
-#endif
e58a44
+    size_t i;
e58a44
 
e58a44
-    return 0;                   /* not against policy */
e58a44
+    if (ais == NULL)
e58a44
+        return;
e58a44
+    for (i = 0; ais[i] != NULL; i++)
e58a44
+        free(ais[i]);
e58a44
+    free(ais);
e58a44
+}
e58a44
+
e58a44
+/* Convert inds to a null-terminated list of C strings. */
e58a44
+static krb5_error_code
e58a44
+authind_strings(krb5_data *const *inds, char ***strs_out)
e58a44
+{
e58a44
+    krb5_error_code ret;
e58a44
+    char **list = NULL;
e58a44
+    size_t i, count;
e58a44
+
e58a44
+    *strs_out = NULL;
e58a44
+
e58a44
+    for (count = 0; inds != NULL && inds[count] != NULL; count++);
e58a44
+    list = k5calloc(count + 1, sizeof(*list), &ret;;
e58a44
+    if (list == NULL)
e58a44
+        goto error;
e58a44
+
e58a44
+    for (i = 0; i < count; i++) {
e58a44
+        list[i] = k5memdup0(inds[i]->data, inds[i]->length, &ret;;
e58a44
+        if (list[i] == NULL)
e58a44
+            goto error;
e58a44
+    }
e58a44
+
e58a44
+    *strs_out = list;
e58a44
+    return 0;
e58a44
+
e58a44
+error:
e58a44
+    free_indicators(list);
e58a44
+    return ret;
e58a44
+}
e58a44
+
e58a44
+/* Constrain times->endtime to life and times->renew_till to rlife, relative to
e58a44
+ * now. */
e58a44
+static void
e58a44
+update_ticket_times(krb5_ticket_times *times, krb5_timestamp now,
e58a44
+                    krb5_deltat life, krb5_deltat rlife)
e58a44
+{
e58a44
+    if (life)
e58a44
+        times->endtime = ts_min(ts_incr(now, life), times->endtime);
e58a44
+    if (rlife)
e58a44
+        times->renew_till = ts_min(ts_incr(now, rlife), times->renew_till);
e58a44
+}
e58a44
+
e58a44
+/* Check an AS request against kdcpolicy modules, updating times with any
e58a44
+ * module endtime constraints.  Set an appropriate status string on error. */
e58a44
+krb5_error_code
e58a44
+check_kdcpolicy_as(krb5_context context, const krb5_kdc_req *request,
e58a44
+                   const krb5_db_entry *client, const krb5_db_entry *server,
e58a44
+                   krb5_data *const *auth_indicators, krb5_timestamp kdc_time,
e58a44
+                   krb5_ticket_times *times, const char **status)
e58a44
+{
e58a44
+    krb5_deltat life, rlife;
e58a44
+    krb5_error_code ret;
e58a44
+    kdcpolicy_handle *hp, h;
e58a44
+    char **ais = NULL;
e58a44
+
e58a44
+    *status = NULL;
e58a44
+
e58a44
+    ret = authind_strings(auth_indicators, &ais);
e58a44
+    if (ret)
e58a44
+        goto done;
e58a44
+
e58a44
+    for (hp = handles; *hp != NULL; hp++) {
e58a44
+        h = *hp;
e58a44
+        if (h->vt.check_as == NULL)
e58a44
+            continue;
e58a44
+
e58a44
+        ret = h->vt.check_as(context, h->moddata, request, client, server,
e58a44
+                             (const char **)ais, status, &life, &rlife);
e58a44
+        if (ret)
e58a44
+            goto done;
e58a44
+
e58a44
+        update_ticket_times(times, kdc_time, life, rlife);
e58a44
+    }
e58a44
+
e58a44
+done:
e58a44
+    free_indicators(ais);
e58a44
+    return ret;
e58a44
 }
e58a44
 
e58a44
 /*
e58a44
- * This is where local policy restrictions for the TGS should placed.
e58a44
+ * Check the TGS request against the local TGS policy.  Accepts an
e58a44
+ * authentication indicator for the module policy decisions.  Returns 0 and a
e58a44
+ * NULL status string on success.
e58a44
  */
e58a44
 krb5_error_code
e58a44
-against_local_policy_tgs(register krb5_kdc_req *request, krb5_db_entry server,
e58a44
-                         krb5_ticket *ticket, const char **status,
e58a44
-                         krb5_pa_data ***e_data)
e58a44
+check_kdcpolicy_tgs(krb5_context context, const krb5_kdc_req *request,
e58a44
+                    const krb5_db_entry *server, const krb5_ticket *ticket,
e58a44
+                    krb5_data *const *auth_indicators, krb5_timestamp kdc_time,
e58a44
+                    krb5_ticket_times *times, const char **status)
e58a44
 {
e58a44
-#if 0
e58a44
-    /*
e58a44
-     * For example, if your site wants to disallow ticket forwarding,
e58a44
-     * you might do something like this:
e58a44
-     */
e58a44
+    krb5_deltat life, rlife;
e58a44
+    krb5_error_code ret;
e58a44
+    kdcpolicy_handle *hp, h;
e58a44
+    char **ais = NULL;
e58a44
 
e58a44
-    if (isflagset(request->kdc_options, KDC_OPT_FORWARDED)) {
e58a44
-        *status = "FORWARD POLICY";
e58a44
-        return KRB5KDC_ERR_POLICY;
e58a44
+    *status = NULL;
e58a44
+
e58a44
+    ret = authind_strings(auth_indicators, &ais);
e58a44
+    if (ret)
e58a44
+        goto done;
e58a44
+
e58a44
+    for (hp = handles; *hp != NULL; hp++) {
e58a44
+        h = *hp;
e58a44
+        if (h->vt.check_tgs == NULL)
e58a44
+            continue;
e58a44
+
e58a44
+        ret = h->vt.check_tgs(context, h->moddata, request, server, ticket,
e58a44
+                              (const char **)ais, status, &life, &rlife);
e58a44
+        if (ret)
e58a44
+            goto done;
e58a44
+
e58a44
+        update_ticket_times(times, kdc_time, life, rlife);
e58a44
     }
e58a44
-#endif
e58a44
 
e58a44
-    return 0;                           /* not against policy */
e58a44
+done:
e58a44
+    free_indicators(ais);
e58a44
+    return ret;
e58a44
+}
e58a44
+
e58a44
+void
e58a44
+unload_kdcpolicy_plugins(krb5_context context)
e58a44
+{
e58a44
+    kdcpolicy_handle *hp, h;
e58a44
+
e58a44
+    for (hp = handles; *hp != NULL; hp++) {
e58a44
+        h = *hp;
e58a44
+        if (h->vt.fini != NULL)
e58a44
+            h->vt.fini(context, h->moddata);
e58a44
+        free(h);
e58a44
+    }
e58a44
+    free(handles);
e58a44
+    handles = NULL;
e58a44
+}
e58a44
+
e58a44
+krb5_error_code
e58a44
+load_kdcpolicy_plugins(krb5_context context)
e58a44
+{
e58a44
+    krb5_error_code ret;
e58a44
+    krb5_plugin_initvt_fn *modules = NULL, *mod;
e58a44
+    kdcpolicy_handle h;
e58a44
+    size_t count;
e58a44
+
e58a44
+    ret = k5_plugin_load_all(context, PLUGIN_INTERFACE_KDCPOLICY, &modules);
e58a44
+    if (ret)
e58a44
+        goto cleanup;
e58a44
+
e58a44
+    for (count = 0; modules[count] != NULL; count++);
e58a44
+    handles = k5calloc(count + 1, sizeof(*handles), &ret;;
e58a44
+    if (handles == NULL)
e58a44
+        goto cleanup;
e58a44
+
e58a44
+    count = 0;
e58a44
+    for (mod = modules; *mod != NULL; mod++) {
e58a44
+        h = k5calloc(1, sizeof(*h), &ret;;
e58a44
+        if (h == NULL)
e58a44
+            goto cleanup;
e58a44
+
e58a44
+        ret = (*mod)(context, 1, 1, (krb5_plugin_vtable)&h->vt);
e58a44
+        if (ret) {              /* Version mismatch. */
e58a44
+            TRACE_KDCPOLICY_VTINIT_FAIL(context, ret);
e58a44
+            free(h);
e58a44
+            continue;
e58a44
+        }
e58a44
+        if (h->vt.init != NULL) {
e58a44
+            ret = h->vt.init(context, &h->moddata);
e58a44
+            if (ret == KRB5_PLUGIN_NO_HANDLE) {
e58a44
+                TRACE_KADM5_AUTH_INIT_SKIP(context, h->vt.name);
e58a44
+                free(h);
e58a44
+                continue;
e58a44
+            }
e58a44
+            if (ret) {
e58a44
+                kdc_err(context, ret, _("while loading policy module %s"),
e58a44
+                        h->vt.name);
e58a44
+                free(h);
e58a44
+                goto cleanup;
e58a44
+            }
e58a44
+        }
e58a44
+        handles[count++] = h;
e58a44
+    }
e58a44
+
e58a44
+    ret = 0;
e58a44
+
e58a44
+cleanup:
e58a44
+    if (ret)
e58a44
+        unload_kdcpolicy_plugins(context);
e58a44
+    k5_plugin_free_modules(context, modules);
e58a44
+    return ret;
e58a44
 }
e58a44
diff --git a/src/kdc/policy.h b/src/kdc/policy.h
e58a44
index 6b000dc90..2a57b0a01 100644
e58a44
--- a/src/kdc/policy.h
e58a44
+++ b/src/kdc/policy.h
e58a44
@@ -26,11 +26,22 @@
e58a44
 #ifndef __KRB5_KDC_POLICY__
e58a44
 #define __KRB5_KDC_POLICY__
e58a44
 
e58a44
-extern int against_postdate_policy (krb5_timestamp);
e58a44
+krb5_error_code
e58a44
+load_kdcpolicy_plugins(krb5_context context);
e58a44
 
e58a44
-extern int against_flag_policy_as (const krb5_kdc_req *);
e58a44
+void
e58a44
+unload_kdcpolicy_plugins(krb5_context context);
e58a44
 
e58a44
-extern int against_flag_policy_tgs (const krb5_kdc_req *,
e58a44
-                                    const krb5_ticket *);
e58a44
+krb5_error_code
e58a44
+check_kdcpolicy_as(krb5_context context, const krb5_kdc_req *request,
e58a44
+                   const krb5_db_entry *client, const krb5_db_entry *server,
e58a44
+                   krb5_data *const *auth_indicators, krb5_timestamp kdc_time,
e58a44
+                   krb5_ticket_times *times, const char **status);
e58a44
+
e58a44
+krb5_error_code
e58a44
+check_kdcpolicy_tgs(krb5_context context, const krb5_kdc_req *request,
e58a44
+                    const krb5_db_entry *server, const krb5_ticket *ticket,
e58a44
+                    krb5_data *const *auth_indicators, krb5_timestamp kdc_time,
e58a44
+                    krb5_ticket_times *times, const char **status);
e58a44
 
e58a44
 #endif /* __KRB5_KDC_POLICY__ */
e58a44
diff --git a/src/kdc/tgs_policy.c b/src/kdc/tgs_policy.c
e58a44
index d0f25d1b7..33cfbcd81 100644
e58a44
--- a/src/kdc/tgs_policy.c
e58a44
+++ b/src/kdc/tgs_policy.c
e58a44
@@ -375,11 +375,5 @@ validate_tgs_request(kdc_realm_t *kdc_active_realm,
e58a44
     if (ret && ret != KRB5_PLUGIN_OP_NOTSUPP)
e58a44
         return errcode_to_protocol(ret);
e58a44
 
e58a44
-    /* Check local policy. */
e58a44
-    errcode = against_local_policy_tgs(request, server, ticket,
e58a44
-                                       status, e_data);
e58a44
-    if (errcode)
e58a44
-        return errcode;
e58a44
-
e58a44
     return 0;
e58a44
 }
e58a44
diff --git a/src/lib/krb5/krb/plugin.c b/src/lib/krb5/krb/plugin.c
e58a44
index 17dd6bd30..31aaf661d 100644
e58a44
--- a/src/lib/krb5/krb/plugin.c
e58a44
+++ b/src/lib/krb5/krb/plugin.c
e58a44
@@ -58,7 +58,9 @@ const char *interface_names[] = {
e58a44
     "audit",
e58a44
     "tls",
e58a44
     "kdcauthdata",
e58a44
-    "certauth"
e58a44
+    "certauth",
e58a44
+    "kadm5_auth",
e58a44
+    "kdcpolicy",
e58a44
 };
e58a44
 
e58a44
 /* Return the context's interface structure for id, or NULL if invalid. */
e58a44
diff --git a/src/plugins/kdcpolicy/test/Makefile.in b/src/plugins/kdcpolicy/test/Makefile.in
e58a44
new file mode 100644
e58a44
index 000000000..b81f1a7ce
e58a44
--- /dev/null
e58a44
+++ b/src/plugins/kdcpolicy/test/Makefile.in
e58a44
@@ -0,0 +1,20 @@
e58a44
+mydir=plugins$(S)policy$(S)test
e58a44
+BUILDTOP=$(REL)..$(S)..$(S)..
e58a44
+
e58a44
+LIBBASE=policy_test
e58a44
+LIBMAJOR=0
e58a44
+LIBMINOR=0
e58a44
+RELDIR=../plugins/kdcpolicy/test
e58a44
+SHLIB_EXPDEPS=$(KRB5_BASE_DEPLIBS)
e58a44
+SHLIB_EXPLIBS=$(KRB5_BASE_LIBS)
e58a44
+
e58a44
+STLIBOBJS=main.o
e58a44
+
e58a44
+SRCS=$(srcdir)/main.c
e58a44
+
e58a44
+all-unix: all-libs
e58a44
+install-unix:
e58a44
+clean-unix:: clean-libs clean-libobjs
e58a44
+
e58a44
+@libnover_frag@
e58a44
+@libobj_frag@
e58a44
diff --git a/src/plugins/kdcpolicy/test/deps b/src/plugins/kdcpolicy/test/deps
e58a44
new file mode 100644
e58a44
index 000000000..e69de29bb
e58a44
diff --git a/src/plugins/kdcpolicy/test/main.c b/src/plugins/kdcpolicy/test/main.c
e58a44
new file mode 100644
e58a44
index 000000000..eb8fde053
e58a44
--- /dev/null
e58a44
+++ b/src/plugins/kdcpolicy/test/main.c
e58a44
@@ -0,0 +1,111 @@
e58a44
+/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
e58a44
+/* include/krb5/kdcpolicy_plugin.h - KDC policy plugin interface */
e58a44
+/*
e58a44
+ * Copyright (C) 2017 by Red Hat, Inc.
e58a44
+ * All rights reserved.
e58a44
+ *
e58a44
+ * Redistribution and use in source and binary forms, with or without
e58a44
+ * modification, are permitted provided that the following conditions
e58a44
+ * are met:
e58a44
+ *
e58a44
+ * * Redistributions of source code must retain the above copyright
e58a44
+ *   notice, this list of conditions and the following disclaimer.
e58a44
+ *
e58a44
+ * * Redistributions in binary form must reproduce the above copyright
e58a44
+ *   notice, this list of conditions and the following disclaimer in
e58a44
+ *   the documentation and/or other materials provided with the
e58a44
+ *   distribution.
e58a44
+ *
e58a44
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
e58a44
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
e58a44
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
e58a44
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
e58a44
+ * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
e58a44
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
e58a44
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
e58a44
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
e58a44
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
e58a44
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
e58a44
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
e58a44
+ * OF THE POSSIBILITY OF SUCH DAMAGE.
e58a44
+ */
e58a44
+
e58a44
+#include "k5-int.h"
e58a44
+#include "kdb.h"
e58a44
+#include <krb5/kdcpolicy_plugin.h>
e58a44
+
e58a44
+static krb5_error_code
e58a44
+output_from_indicator(const char *const *auth_indicators,
e58a44
+                      krb5_deltat *lifetime_out,
e58a44
+                      krb5_deltat *renew_lifetime_out,
e58a44
+                      const char **status)
e58a44
+{
e58a44
+    if (auth_indicators[0] == NULL) {
e58a44
+        *status = NULL;
e58a44
+        return 0;
e58a44
+    }
e58a44
+
e58a44
+    if (strcmp(auth_indicators[0], "ONE_HOUR") == 0) {
e58a44
+        *lifetime_out = 3600;
e58a44
+        *renew_lifetime_out = *lifetime_out * 2;
e58a44
+        return 0;
e58a44
+    } else if (strcmp(auth_indicators[0], "SEVEN_HOURS") == 0) {
e58a44
+        *lifetime_out = 7 * 3600;
e58a44
+        *renew_lifetime_out = *lifetime_out * 2;
e58a44
+        return 0;
e58a44
+    }
e58a44
+
e58a44
+    *status = "LOCAL_POLICY";
e58a44
+    return KRB5KDC_ERR_POLICY;
e58a44
+}
e58a44
+
e58a44
+static krb5_error_code
e58a44
+test_check_as(krb5_context context, krb5_kdcpolicy_moddata moddata,
e58a44
+              const krb5_kdc_req *request, const krb5_db_entry *client,
e58a44
+              const krb5_db_entry *server, const char *const *auth_indicators,
e58a44
+              const char **status, krb5_deltat *lifetime_out,
e58a44
+              krb5_deltat *renew_lifetime_out)
e58a44
+{
e58a44
+    if (request->client != NULL && request->client->length >= 1 &&
e58a44
+        data_eq_string(request->client->data[0], "fail")) {
e58a44
+        *status = "LOCAL_POLICY";
e58a44
+        return KRB5KDC_ERR_POLICY;
e58a44
+    }
e58a44
+    return output_from_indicator(auth_indicators, lifetime_out,
e58a44
+                                 renew_lifetime_out, status);
e58a44
+}
e58a44
+
e58a44
+static krb5_error_code
e58a44
+test_check_tgs(krb5_context context, krb5_kdcpolicy_moddata moddata,
e58a44
+               const krb5_kdc_req *request, const krb5_db_entry *server,
e58a44
+               const krb5_ticket *ticket, const char *const *auth_indicators,
e58a44
+               const char **status, krb5_deltat *lifetime_out,
e58a44
+               krb5_deltat *renew_lifetime_out)
e58a44
+{
e58a44
+    if (request->server != NULL && request->server->length >= 1 &&
e58a44
+        data_eq_string(request->server->data[0], "fail")) {
e58a44
+        *status = "LOCAL_POLICY";
e58a44
+        return KRB5KDC_ERR_POLICY;
e58a44
+    }
e58a44
+    return output_from_indicator(auth_indicators, lifetime_out,
e58a44
+                                 renew_lifetime_out, status);
e58a44
+}
e58a44
+
e58a44
+krb5_error_code
e58a44
+kdcpolicy_test_initvt(krb5_context context, int maj_ver, int min_ver,
e58a44
+                      krb5_plugin_vtable vtable);
e58a44
+krb5_error_code
e58a44
+kdcpolicy_test_initvt(krb5_context context, int maj_ver, int min_ver,
e58a44
+                      krb5_plugin_vtable vtable)
e58a44
+{
e58a44
+    krb5_kdcpolicy_vtable vt;
e58a44
+
e58a44
+    if (maj_ver != 1)
e58a44
+        return KRB5_PLUGIN_VER_NOTSUPP;
e58a44
+
e58a44
+    vt = (krb5_kdcpolicy_vtable)vtable;
e58a44
+    vt->name = "test";
e58a44
+    vt->check_as = test_check_as;
e58a44
+    vt->check_tgs = test_check_tgs;
e58a44
+    return 0;
e58a44
+}
e58a44
diff --git a/src/plugins/kdcpolicy/test/policy_test.exports b/src/plugins/kdcpolicy/test/policy_test.exports
e58a44
new file mode 100644
e58a44
index 000000000..9682ec74f
e58a44
--- /dev/null
e58a44
+++ b/src/plugins/kdcpolicy/test/policy_test.exports
e58a44
@@ -0,0 +1 @@
e58a44
+kdcpolicy_test_initvt
e58a44
diff --git a/src/tests/Makefile.in b/src/tests/Makefile.in
e58a44
index 2b3112537..a2093108b 100644
e58a44
--- a/src/tests/Makefile.in
e58a44
+++ b/src/tests/Makefile.in
e58a44
@@ -169,6 +169,7 @@ check-pytests: localauth plugorder rdreq responder s2p s4u2proxy unlockiter
e58a44
 	$(RUNPYTEST) $(srcdir)/t_tabdump.py $(PYTESTFLAGS)
e58a44
 	$(RUNPYTEST) $(srcdir)/t_certauth.py $(PYTESTFLAGS)
e58a44
 	$(RUNPYTEST) $(srcdir)/t_y2038.py $(PYTESTFLAGS)
e58a44
+	$(RUNPYTEST) $(srcdir)/t_kdcpolicy.py $(PYTESTFLAGS)
e58a44
 
e58a44
 clean:
e58a44
 	$(RM) adata etinfo forward gcred hist hooks hrealm icred kdbtest
e58a44
diff --git a/src/tests/t_kdcpolicy.py b/src/tests/t_kdcpolicy.py
e58a44
new file mode 100644
e58a44
index 000000000..6a745b959
e58a44
--- /dev/null
e58a44
+++ b/src/tests/t_kdcpolicy.py
e58a44
@@ -0,0 +1,57 @@
e58a44
+#!/usr/bin/python
e58a44
+from k5test import *
e58a44
+from datetime import datetime
e58a44
+import re
e58a44
+
e58a44
+testpreauth = os.path.join(buildtop, 'plugins', 'preauth', 'test', 'test.so')
e58a44
+testpolicy = os.path.join(buildtop, 'plugins', 'kdcpolicy', 'test',
e58a44
+                          'policy_test.so')
e58a44
+krb5_conf = {'plugins': {'kdcpreauth': {'module': 'test:' + testpreauth},
e58a44
+                         'clpreauth': {'module': 'test:' + testpreauth},
e58a44
+                         'kdcpolicy': {'module': 'test:' + testpolicy}}}
e58a44
+kdc_conf = {'realms': {'$realm': {'default_principal_flags': '+preauth',
e58a44
+                                  'max_renewable_life': '1d'}}}
e58a44
+realm = K5Realm(krb5_conf=krb5_conf, kdc_conf=kdc_conf)
e58a44
+
e58a44
+realm.run([kadminl, 'addprinc', '-pw', password('fail'), 'fail'])
e58a44
+
e58a44
+def verify_time(out, target_time):
e58a44
+    times = re.findall(r'\d\d/\d\d/\d\d \d\d:\d\d:\d\d', out)
e58a44
+    times = [datetime.strptime(t, '%m/%d/%y %H:%M:%S') for t in times]
e58a44
+    while len(times) > 0:
e58a44
+        starttime = times.pop(0)
e58a44
+        endtime = times.pop(0)
e58a44
+        renewtime = times.pop(0)
e58a44
+
e58a44
+        if str(endtime - starttime) != target_time:
e58a44
+            fail('unexpected lifetime value')
e58a44
+        if str(renewtime - endtime) != target_time:
e58a44
+            fail('unexpected renewable value')
e58a44
+
e58a44
+rflags = ['-r', '1d', '-l', '12h']
e58a44
+
e58a44
+# Test AS+TGS success path.
e58a44
+realm.kinit(realm.user_princ, password('user'),
e58a44
+            rflags + ['-X', 'indicators=SEVEN_HOURS'])
e58a44
+realm.run([kvno, realm.host_princ])
e58a44
+realm.run(['./adata', realm.host_princ], expected_msg='+97: [SEVEN_HOURS]')
e58a44
+out = realm.run([klist, realm.ccache, '-e'])
e58a44
+verify_time(out, '7:00:00')
e58a44
+
e58a44
+# Test AS+TGS success path with different values.
e58a44
+realm.kinit(realm.user_princ, password('user'),
e58a44
+            rflags + ['-X', 'indicators=ONE_HOUR'])
e58a44
+realm.run([kvno, realm.host_princ])
e58a44
+realm.run(['./adata', realm.host_princ], expected_msg='+97: [ONE_HOUR]')
e58a44
+out = realm.run([klist, realm.ccache, '-e'])
e58a44
+verify_time(out, '1:00:00')
e58a44
+
e58a44
+# Test TGS failure path (using previous creds).
e58a44
+realm.run([kvno, 'fail@%s' % realm.realm], expected_code=1,
e58a44
+          expected_msg='KDC policy rejects request')
e58a44
+
e58a44
+# Test AS failure path.
e58a44
+realm.kinit('fail@%s' % realm.realm, password('fail'),
e58a44
+            expected_code=1, expected_msg='KDC policy rejects request')
e58a44
+
e58a44
+success('kdcpolicy tests')