483b06
From 0956c8149f11921ed427d67b10bb9b6c4b97df48 Mon Sep 17 00:00:00 2001
483b06
From: Sumit Bose <sbose@redhat.com>
483b06
Date: Thu, 2 Feb 2017 12:32:13 +0100
483b06
Subject: [PATCH] IPA certauth plugin
483b06
483b06
This patch add a certauth plugin which allows the IPA server to support
483b06
PKINIT for certificates which do not include a special SAN extension
483b06
which contains a Kerberos principal but allow other mappings with the
483b06
help of SSSD's certmap library.
483b06
483b06
Related to https://pagure.io/freeipa/issue/4905
483b06
483b06
Reviewed-By: Alexander Bokovoy <abokovoy@redhat.com>
483b06
Reviewed-By: David Kupka <dkupka@redhat.com>
483b06
---
483b06
 daemons/ipa-kdb/Makefile.am        |  24 ++-
483b06
 daemons/ipa-kdb/ipa-certauth.in    |   5 +
483b06
 daemons/ipa-kdb/ipa_kdb.c          |   2 +
483b06
 daemons/ipa-kdb/ipa_kdb.exports    |   1 +
483b06
 daemons/ipa-kdb/ipa_kdb.h          |   5 +
483b06
 daemons/ipa-kdb/ipa_kdb_certauth.c | 398 +++++++++++++++++++++++++++++++++++++
483b06
 freeipa.spec.in                    |   2 +
483b06
 server.m4                          |  13 ++
483b06
 8 files changed, 449 insertions(+), 1 deletion(-)
483b06
 create mode 100644 daemons/ipa-kdb/ipa-certauth.in
483b06
 create mode 100644 daemons/ipa-kdb/ipa_kdb_certauth.c
483b06
483b06
diff --git a/daemons/ipa-kdb/Makefile.am b/daemons/ipa-kdb/Makefile.am
483b06
index 6a2caa0637bf076c796b50efc92412062524f35f..715666e779a4fa64c2c0f71767f09efb19b5f908 100644
483b06
--- a/daemons/ipa-kdb/Makefile.am
483b06
+++ b/daemons/ipa-kdb/Makefile.am
483b06
@@ -18,6 +18,7 @@ AM_CPPFLAGS =						\
483b06
 	$(WARN_CFLAGS)					\
483b06
 	$(NDRPAC_CFLAGS)				\
483b06
 	$(NSS_CFLAGS)					\
483b06
+	$(SSSCERTMAP_CFLAGS)				\
483b06
 	$(NULL)
483b06
 
483b06
 plugindir = $(libdir)/krb5/plugins/kdb
483b06
@@ -39,6 +40,20 @@ ipadb_la_SOURCES = 		\
483b06
 	ipa_kdb_audit_as.c	\
483b06
 	$(NULL)
483b06
 
483b06
+if BUILD_IPA_CERTAUTH_PLUGIN
483b06
+ipadb_la_SOURCES += ipa_kdb_certauth.c
483b06
+
483b06
+
483b06
+%: %.in
483b06
+	sed \
483b06
+		-e 's|@plugindir@|$(plugindir)|g' \
483b06
+		'$(srcdir)/$@.in' >$@
483b06
+
483b06
+krb5confdir = $(sysconfdir)/krb5.conf.d
483b06
+krb5conf_DATA = ipa-certauth
483b06
+CLEANFILES = $(krb5conf_DATA)
483b06
+endif
483b06
+
483b06
 ipadb_la_LDFLAGS = 		\
483b06
 	-avoid-version 		\
483b06
 	-module			\
483b06
@@ -50,6 +65,7 @@ ipadb_la_LIBADD = 		\
483b06
 	$(NDRPAC_LIBS)		\
483b06
 	$(UNISTRING_LIBS)	\
483b06
 	$(NSS_LIBS)             \
483b06
+	$(SSSCERTMAP_LIBS)	\
483b06
 	$(top_builddir)/util/libutil.la	\
483b06
 	$(NULL)
483b06
 
483b06
@@ -70,6 +86,11 @@ ipa_kdb_tests_SOURCES =        \
483b06
        ipa_kdb_delegation.c    \
483b06
        ipa_kdb_audit_as.c      \
483b06
        $(NULL)
483b06
+
483b06
+if BUILD_IPA_CERTAUTH_PLUGIN
483b06
+ipa_kdb_tests_SOURCES += ipa_kdb_certauth.c
483b06
+endif
483b06
+
483b06
 ipa_kdb_tests_CFLAGS = $(CMOCKA_CFLAGS)
483b06
 ipa_kdb_tests_LDADD =          \
483b06
        $(CMOCKA_LIBS)          \
483b06
@@ -78,12 +99,13 @@ ipa_kdb_tests_LDADD =          \
483b06
        $(NDRPAC_LIBS)          \
483b06
        $(UNISTRING_LIBS)       \
483b06
        $(NSS_LIBS)             \
483b06
+       $(SSSCERTMAP_LIBS)      \
483b06
        $(top_builddir)/util/libutil.la	\
483b06
        -lkdb5                  \
483b06
        -lsss_idmap             \
483b06
        $(NULL)
483b06
 
483b06
-dist_noinst_DATA = ipa_kdb.exports
483b06
+dist_noinst_DATA = ipa_kdb.exports ipa-certauth.in
483b06
 
483b06
 clean-local:
483b06
 	rm -f tests/.dirstamp
483b06
diff --git a/daemons/ipa-kdb/ipa-certauth.in b/daemons/ipa-kdb/ipa-certauth.in
483b06
new file mode 100644
483b06
index 0000000000000000000000000000000000000000..eda89a26f02fbea449eb754b232b8115904acd21
483b06
--- /dev/null
483b06
+++ b/daemons/ipa-kdb/ipa-certauth.in
483b06
@@ -0,0 +1,5 @@
483b06
+[plugins]
483b06
+ certauth = {
483b06
+  module = ipakdb:@plugindir@/ipadb.so
483b06
+  enable_only = ipakdb
483b06
+ }
483b06
diff --git a/daemons/ipa-kdb/ipa_kdb.c b/daemons/ipa-kdb/ipa_kdb.c
483b06
index c19b7c40e2e88173ab8367a3ef1d7f46245fd174..a961e4e57cf5379eb237551d56e3bc8dc82d952d 100644
483b06
--- a/daemons/ipa-kdb/ipa_kdb.c
483b06
+++ b/daemons/ipa-kdb/ipa_kdb.c
483b06
@@ -67,6 +67,8 @@ static void ipadb_context_free(krb5_context kcontext,
483b06
         }
483b06
         free(cfg->authz_data);
483b06
 
483b06
+        ipa_certauth_free_moddata(&((*ctx)->certauth_moddata));
483b06
+
483b06
         free(*ctx);
483b06
         *ctx = NULL;
483b06
     }
483b06
diff --git a/daemons/ipa-kdb/ipa_kdb.exports b/daemons/ipa-kdb/ipa_kdb.exports
483b06
index d2c3f30246cc7ebcba02a9ec5d134e604fa0dbb9..27ce92d2edd741245061a5f4ee9275169440c932 100644
483b06
--- a/daemons/ipa-kdb/ipa_kdb.exports
483b06
+++ b/daemons/ipa-kdb/ipa_kdb.exports
483b06
@@ -3,6 +3,7 @@ EXPORTED {
483b06
 	# public symbols
483b06
 	global:
483b06
 		kdb_function_table;
483b06
+		certauth_ipakdb_initvt;
483b06
 
483b06
 	# everything else is local
483b06
 	local:
483b06
diff --git a/daemons/ipa-kdb/ipa_kdb.h b/daemons/ipa-kdb/ipa_kdb.h
483b06
index 72f2675809a3267cce30bc06c77335697c7287ad..632c1979d15e88aec86d5e408ed6c7017d8362b8 100644
483b06
--- a/daemons/ipa-kdb/ipa_kdb.h
483b06
+++ b/daemons/ipa-kdb/ipa_kdb.h
483b06
@@ -40,6 +40,7 @@
483b06
 #include <arpa/inet.h>
483b06
 #include <endian.h>
483b06
 #include <unistd.h>
483b06
+#include <krb5/certauth_plugin.h>
483b06
 
483b06
 #include "ipa_krb5.h"
483b06
 #include "ipa_pwd.h"
483b06
@@ -111,6 +112,7 @@ struct ipadb_context {
483b06
     krb5_key_salt_tuple *def_encs;
483b06
     int n_def_encs;
483b06
     struct ipadb_mspac *mspac;
483b06
+    krb5_certauth_moddata certauth_moddata;
483b06
 
483b06
     /* Don't access this directly, use ipadb_get_global_config(). */
483b06
     struct ipadb_global_config config;
483b06
@@ -331,3 +333,6 @@ ipadb_get_global_config(struct ipadb_context *ipactx);
483b06
 int ipadb_get_enc_salt_types(struct ipadb_context *ipactx, LDAPMessage *entry,
483b06
                              char *attr, krb5_key_salt_tuple **enc_salt_types,
483b06
                              int *n_enc_salt_types);
483b06
+
483b06
+/* CERTAUTH PLUGIN */
483b06
+void ipa_certauth_free_moddata(krb5_certauth_moddata *moddata);
483b06
diff --git a/daemons/ipa-kdb/ipa_kdb_certauth.c b/daemons/ipa-kdb/ipa_kdb_certauth.c
483b06
new file mode 100644
483b06
index 0000000000000000000000000000000000000000..a53a2ce4e7ceb06ec8de117cdbca2666fdb5a97a
483b06
--- /dev/null
483b06
+++ b/daemons/ipa-kdb/ipa_kdb_certauth.c
483b06
@@ -0,0 +1,398 @@
483b06
+/** BEGIN COPYRIGHT BLOCK
483b06
+ * This program is free software; you can redistribute it and/or modify
483b06
+ * it under the terms of the GNU General Public License as published by
483b06
+ * the Free Software Foundation, either version 3 of the License, or
483b06
+ * (at your option) any later version.
483b06
+ *
483b06
+ * This program is distributed in the hope that it will be useful,
483b06
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
483b06
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
483b06
+ * GNU General Public License for more details.
483b06
+ *
483b06
+ * You should have received a copy of the GNU General Public License
483b06
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
483b06
+ *
483b06
+ * Additional permission under GPLv3 section 7:
483b06
+ *
483b06
+ * In the following paragraph, "GPL" means the GNU General Public
483b06
+ * License, version 3 or any later version, and "Non-GPL Code" means
483b06
+ * code that is governed neither by the GPL nor a license
483b06
+ * compatible with the GPL.
483b06
+ *
483b06
+ * You may link the code of this Program with Non-GPL Code and convey
483b06
+ * linked combinations including the two, provided that such Non-GPL
483b06
+ * Code only links to the code of this Program through those well
483b06
+ * defined interfaces identified in the file named EXCEPTION found in
483b06
+ * the source code files (the "Approved Interfaces"). The files of
483b06
+ * Non-GPL Code may instantiate templates or use macros or inline
483b06
+ * functions from the Approved Interfaces without causing the resulting
483b06
+ * work to be covered by the GPL. Only the copyright holders of this
483b06
+ * Program may make changes or additions to the list of Approved
483b06
+ * Interfaces.
483b06
+ *
483b06
+ * Authors:
483b06
+ * Sumit Bose <sbose@redhat.com>
483b06
+ *
483b06
+ * Copyright (C) 2017 Red Hat, Inc.
483b06
+ * All rights reserved.
483b06
+ * END COPYRIGHT BLOCK **/
483b06
+
483b06
+#include <errno.h>
483b06
+//#include <krb5/certauth_plugin.h>
483b06
+#include <syslog.h>
483b06
+#include <sss_certmap.h>
483b06
+
483b06
+#include "util/ipa_krb5.h"
483b06
+#include "ipa_kdb.h"
483b06
+
483b06
+#define IPA_OC_CERTMAP_RULE "ipaCertMapRule"
483b06
+#define IPA_CERTMAP_MAPRULE "ipaCertMapMapRule"
483b06
+#define IPA_CERTMAP_MATCHRULE "ipaCertMapMatchRule"
483b06
+#define IPA_CERTMAP_PRIORITY "ipaCertMapPriority"
483b06
+#define IPA_ENABLED_FLAG "ipaEnabledFlag"
483b06
+#define IPA_TRUE_VALUE "TRUE"
483b06
+#define IPA_ASSOCIATED_DOMAIN "associatedDomain"
483b06
+
483b06
+#define OBJECTCLASS "objectClass"
483b06
+
483b06
+#define CERTMAP_FILTER "(&("OBJECTCLASS"="IPA_OC_CERTMAP_RULE")" \
483b06
+                              "("IPA_ENABLED_FLAG"="IPA_TRUE_VALUE"))"
483b06
+
483b06
+#ifndef discard_const
483b06
+#define discard_const(ptr) ((void *)((uintptr_t)(ptr)))
483b06
+#endif
483b06
+
483b06
+
483b06
+struct krb5_certauth_moddata_st {
483b06
+    char *local_domain;
483b06
+    struct sss_certmap_ctx *sss_certmap_ctx;
483b06
+    struct ipadb_context *ipactx;
483b06
+};
483b06
+
483b06
+void ipa_certmap_debug(void *private,
483b06
+                       const char *file, long line,
483b06
+                       const char *function,
483b06
+                       const char *format, ...)
483b06
+{
483b06
+    va_list ap;
483b06
+    char str[255] = { 0 };
483b06
+
483b06
+    va_start(ap, format);
483b06
+    vsnprintf(str, sizeof(str)-1, format, ap);
483b06
+    va_end(ap);
483b06
+    krb5_klog_syslog(LOG_INFO, str);
483b06
+}
483b06
+
483b06
+void ipa_certauth_free_moddata(krb5_certauth_moddata *moddata)
483b06
+{
483b06
+    if (moddata == NULL || *moddata == NULL) {
483b06
+        return;
483b06
+    }
483b06
+
483b06
+    free((*moddata)->local_domain);
483b06
+    (*moddata)->local_domain = NULL;
483b06
+    sss_certmap_free_ctx((*moddata)->sss_certmap_ctx);
483b06
+    (*moddata)->sss_certmap_ctx = NULL;
483b06
+
483b06
+    free(*moddata);
483b06
+
483b06
+    return;
483b06
+}
483b06
+
483b06
+static krb5_error_code ipa_get_init_data(krb5_context kcontext,
483b06
+                                         krb5_certauth_moddata moddata_out)
483b06
+{
483b06
+    int ret;
483b06
+    struct sss_certmap_ctx *ctx = NULL;
483b06
+    struct ipadb_context *ipactx;
483b06
+    krb5_error_code kerr;
483b06
+    char *basedn = NULL;
483b06
+    LDAPMessage *result = NULL;
483b06
+    LDAPMessage *le;
483b06
+    LDAP *lc;
483b06
+    size_t c;
483b06
+    uint32_t prio;
483b06
+    char *map_rule = NULL;
483b06
+    char *match_rule = NULL;
483b06
+    char **domains = NULL;
483b06
+
483b06
+    const char *certmap_attrs[] = { OBJECTCLASS,
483b06
+                                    IPA_CERTMAP_PRIORITY,
483b06
+                                    IPA_CERTMAP_MATCHRULE,
483b06
+                                    IPA_CERTMAP_MAPRULE,
483b06
+                                    IPA_ASSOCIATED_DOMAIN,
483b06
+                                    IPA_ENABLED_FLAG,
483b06
+                                    NULL};
483b06
+
483b06
+
483b06
+    krb5_klog_syslog(LOG_INFO, "Initializing IPA certauth plugin.");
483b06
+
483b06
+    ipactx = ipadb_get_context(kcontext);
483b06
+    if (ipactx == NULL) {
483b06
+        return KRB5_KDB_DBNOTINITED;
483b06
+    }
483b06
+
483b06
+    if (ipactx->certauth_moddata == NULL) {
483b06
+        ret = asprintf(&basedn, "cn=certmap,%s", ipactx->base);
483b06
+        if (ret == -1) {
483b06
+            return ENOMEM;
483b06
+        }
483b06
+
483b06
+        kerr = ipadb_simple_search(ipactx,basedn, LDAP_SCOPE_SUBTREE,
483b06
+                                   CERTMAP_FILTER, discard_const(certmap_attrs),
483b06
+                                   &result);
483b06
+        if (kerr != 0 && kerr != KRB5_KDB_NOENTRY) {
483b06
+            goto done;
483b06
+        }
483b06
+
483b06
+        ret = sss_certmap_init(NULL, ipa_certmap_debug, NULL, &ctx;;
483b06
+        if (ret != 0) {
483b06
+            return ret;
483b06
+        }
483b06
+
483b06
+        if (kerr == KRB5_KDB_NOENTRY) {
483b06
+            ret = sss_certmap_add_rule(ctx, SSS_CERTMAP_MIN_PRIO,
483b06
+                                       NULL, NULL, NULL);
483b06
+            if (ret != 0) {
483b06
+                goto done;
483b06
+            }
483b06
+        } else {
483b06
+            lc = ipactx->lcontext;
483b06
+
483b06
+            for (le = ldap_first_entry(lc, result); le;
483b06
+                                                 le = ldap_next_entry(lc, le)) {
483b06
+                prio = SSS_CERTMAP_MIN_PRIO;
483b06
+                ret = ipadb_ldap_attr_to_uint32(lc, le, IPA_CERTMAP_PRIORITY,
483b06
+                                                &prio;;
483b06
+                if (ret != 0 && ret != ENOENT) {
483b06
+                    goto done;
483b06
+                }
483b06
+
483b06
+                free(map_rule);
483b06
+                map_rule = NULL;
483b06
+                ret = ipadb_ldap_attr_to_str(lc, le, IPA_CERTMAP_MAPRULE,
483b06
+                                             &map_rule);
483b06
+                if (ret != 0 && ret != ENOENT) {
483b06
+                    goto done;
483b06
+                }
483b06
+
483b06
+                free(match_rule);
483b06
+                match_rule = NULL;
483b06
+                ret = ipadb_ldap_attr_to_str(lc, le, IPA_CERTMAP_MATCHRULE,
483b06
+                                             &match_rule);
483b06
+                if (ret != 0 && ret != ENOENT) {
483b06
+                    goto done;
483b06
+                }
483b06
+
483b06
+                if (domains != NULL) {
483b06
+                    for (c = 0; domains[c] != NULL; c++) {
483b06
+                        free(domains[c]);
483b06
+                    }
483b06
+                    free(domains);
483b06
+                    domains = NULL;
483b06
+                }
483b06
+                ret = ipadb_ldap_attr_to_strlist(lc, le, IPA_ASSOCIATED_DOMAIN,
483b06
+                                                 &domains);
483b06
+                if (ret != 0 && ret != ENOENT) {
483b06
+                    goto done;
483b06
+                }
483b06
+
483b06
+                ret = sss_certmap_add_rule(ctx, prio, match_rule, map_rule,
483b06
+                                           (const char **) domains);
483b06
+                if (ret != 0) {
483b06
+                    goto done;
483b06
+                }
483b06
+            }
483b06
+        }
483b06
+
483b06
+        ipactx->certauth_moddata = moddata_out;
483b06
+
483b06
+        if (ipactx->realm != NULL) {
483b06
+            ipactx->certauth_moddata->local_domain = strdup(ipactx->realm);
483b06
+            if (ipactx->certauth_moddata->local_domain == NULL) {
483b06
+                free(ipactx->certauth_moddata);
483b06
+                ipactx->certauth_moddata = NULL;
483b06
+                ret = ENOMEM;
483b06
+                goto done;
483b06
+            }
483b06
+        }
483b06
+
483b06
+        ipactx->certauth_moddata->sss_certmap_ctx = ctx;
483b06
+        ipactx->certauth_moddata->ipactx = ipactx;
483b06
+
483b06
+    }
483b06
+
483b06
+    ret = 0;
483b06
+
483b06
+done:
483b06
+    ldap_msgfree(result);
483b06
+    free(basedn);
483b06
+    free(map_rule);
483b06
+    free(match_rule);
483b06
+    if (domains != NULL) {
483b06
+        for (c = 0; domains[c] != NULL; c++) {
483b06
+            free(domains[c]);
483b06
+        }
483b06
+        free(domains);
483b06
+        domains = NULL;
483b06
+    }
483b06
+
483b06
+    if (ret != 0) {
483b06
+        sss_certmap_free_ctx(ctx);
483b06
+    }
483b06
+
483b06
+    return ret;
483b06
+}
483b06
+
483b06
+static krb5_error_code ipa_certauth_authorize(krb5_context context,
483b06
+                                              krb5_certauth_moddata moddata,
483b06
+                                              const uint8_t *cert,
483b06
+                                              size_t cert_len,
483b06
+                                              krb5_const_principal princ,
483b06
+                                              const void *opts,
483b06
+                                              const krb5_db_entry *db_entry,
483b06
+                                              char ***authinds_out)
483b06
+{
483b06
+    char *cert_filter = NULL;
483b06
+    char **domains = NULL;
483b06
+    int ret;
483b06
+    size_t c;
483b06
+    char *principal = NULL;
483b06
+    LDAPMessage *res = NULL;
483b06
+    krb5_error_code kerr;
483b06
+    LDAPMessage *lentry;
483b06
+
483b06
+    if (moddata == NULL) {
483b06
+        return KRB5_PLUGIN_NO_HANDLE;
483b06
+    }
483b06
+
483b06
+    if (moddata->sss_certmap_ctx == NULL) {
483b06
+        kerr = ipa_get_init_data(context, moddata);
483b06
+        if (kerr != 0) {
483b06
+            krb5_klog_syslog(LOG_ERR, "Failed to init certmapping data");
483b06
+            return KRB5_PLUGIN_NO_HANDLE;
483b06
+        }
483b06
+    }
483b06
+
483b06
+    ret = krb5_unparse_name(context, princ, &principal);
483b06
+    if (ret != 0) {
483b06
+        ret = KRB5KDC_ERR_CERTIFICATE_MISMATCH;
483b06
+        goto done;
483b06
+    }
483b06
+    krb5_klog_syslog(LOG_INFO, "Doing certauth authorize for [%s]", principal);
483b06
+
483b06
+    ret = sss_certmap_get_search_filter(moddata->sss_certmap_ctx,
483b06
+                                        cert, cert_len,
483b06
+                                        &cert_filter, &domains);
483b06
+    if (ret != 0) {
483b06
+        if (ret == ENOENT) {
483b06
+            ret = KRB5KDC_ERR_CERTIFICATE_MISMATCH;
483b06
+        }
483b06
+        goto done;
483b06
+    }
483b06
+    krb5_klog_syslog(LOG_INFO, "Got cert filter [%s]", cert_filter);
483b06
+
483b06
+    /* If there are no domains assigned the rule will apply to the local
483b06
+     * domain only. */
483b06
+    if (domains != NULL) {
483b06
+
483b06
+        if (moddata->local_domain == NULL) {
483b06
+        /* We don't know our own domain name, in general this should not
483b06
+         * happen. But to be fault tolerant we allow matching rule which
483b06
+         * do not have a domain assigned. */
483b06
+
483b06
+            ret = KRB5KDC_ERR_CERTIFICATE_MISMATCH;
483b06
+            goto done;
483b06
+        }
483b06
+
483b06
+        for (c = 0; domains[c] != NULL; c++) {
483b06
+            if (strcasecmp(domains[c], moddata->local_domain) == 0) {
483b06
+                break;
483b06
+            }
483b06
+        }
483b06
+
483b06
+        /* Our domain was not in the list */
483b06
+        if (domains[c] == NULL) {
483b06
+            ret = KRB5KDC_ERR_CERTIFICATE_MISMATCH;
483b06
+            goto done;
483b06
+        }
483b06
+    }
483b06
+
483b06
+    kerr = ipadb_fetch_principals_with_extra_filter(moddata->ipactx,
483b06
+                                                    KRB5_KDB_FLAG_ALIAS_OK,
483b06
+                                                    principal,
483b06
+                                                    cert_filter,
483b06
+                                                    &res;;
483b06
+    if (kerr != 0) {
483b06
+        krb5_klog_syslog(LOG_ERR, "Search failed [%d]", kerr);
483b06
+        ret = KRB5KDC_ERR_CERTIFICATE_MISMATCH;
483b06
+        goto done;
483b06
+    }
483b06
+
483b06
+    kerr = ipadb_find_principal(context, KRB5_KDB_FLAG_ALIAS_OK, res,
483b06
+                                &principal, &lentry);
483b06
+    if (kerr == KRB5_KDB_NOENTRY) {
483b06
+        krb5_klog_syslog(LOG_INFO, "No matching entry found");
483b06
+        ret = KRB5KDC_ERR_CERTIFICATE_MISMATCH;
483b06
+        goto done;
483b06
+    } else if (kerr != 0) {
483b06
+        krb5_klog_syslog(LOG_ERR, "ipadb_find_principal failed [%d]", kerr);
483b06
+        ret = KRB5KDC_ERR_CERTIFICATE_MISMATCH;
483b06
+        goto done;
483b06
+    }
483b06
+
483b06
+    /* TODO: add more tests ? */
483b06
+
483b06
+    ret = 0;
483b06
+
483b06
+done:
483b06
+    sss_certmap_free_filter_and_domains(cert_filter, domains);
483b06
+    krb5_free_unparsed_name(context, principal);
483b06
+    ldap_msgfree(res);
483b06
+
483b06
+    return ret;
483b06
+}
483b06
+
483b06
+static krb5_error_code ipa_certauth_init(krb5_context kcontext,
483b06
+                                         krb5_certauth_moddata *moddata_out)
483b06
+{
483b06
+    struct krb5_certauth_moddata_st *certauth_moddata;
483b06
+
483b06
+    certauth_moddata = calloc(1, sizeof(struct krb5_certauth_moddata_st));
483b06
+    if (certauth_moddata == NULL) {
483b06
+        return ENOMEM;
483b06
+    }
483b06
+
483b06
+    *moddata_out = certauth_moddata;
483b06
+
483b06
+    return 0;
483b06
+}
483b06
+
483b06
+static void ipa_certauth_fini(krb5_context context,
483b06
+                              krb5_certauth_moddata moddata_out)
483b06
+{
483b06
+    krb5_klog_syslog(LOG_INFO, "IPA certauth plugin un-loaded.");
483b06
+    return;
483b06
+}
483b06
+
483b06
+
483b06
+krb5_error_code certauth_ipakdb_initvt(krb5_context context,
483b06
+                                          int maj_ver, int min_ver,
483b06
+                                          krb5_plugin_vtable vtable)
483b06
+{
483b06
+    krb5_certauth_vtable vt;
483b06
+
483b06
+    if (maj_ver != 1) {
483b06
+        return KRB5_PLUGIN_VER_NOTSUPP;
483b06
+    }
483b06
+
483b06
+    vt = (krb5_certauth_vtable) vtable;
483b06
+
483b06
+    vt->name = "ipakdb";
483b06
+    vt->authorize = ipa_certauth_authorize;
483b06
+    vt->init = ipa_certauth_init;
483b06
+    vt->fini = ipa_certauth_fini;
483b06
+    /* currently we do not return authentication indicators */
483b06
+    vt->free_ind = NULL;
483b06
+    return 0;
483b06
+}
483b06
diff --git a/freeipa.spec.in b/freeipa.spec.in
483b06
index f776b34af88cc8ccd02da0713cb6eaca161c99f5..18291a5793a6b69dcd719f42e80e1652169e5e1d 100644
483b06
--- a/freeipa.spec.in
483b06
+++ b/freeipa.spec.in
483b06
@@ -120,6 +120,7 @@ BuildRequires:  libtalloc-devel
483b06
 BuildRequires:  libtevent-devel
483b06
 BuildRequires:  libuuid-devel
483b06
 BuildRequires:  libsss_idmap-devel
483b06
+BuildRequires:  libsss_certmap-devel
483b06
 # 1.14.0: sss_nss_getnamebycert (https://fedorahosted.org/sssd/ticket/2897)
483b06
 BuildRequires:  libsss_nss_idmap-devel >= 1.14.0
483b06
 BuildRequires:  rhino
483b06
@@ -1164,6 +1165,7 @@ fi
483b06
 %attr(0755,root,root) %{_libexecdir}/ipa/oddjob/org.freeipa.server.conncheck
483b06
 %config(noreplace) %{_sysconfdir}/dbus-1/system.d/org.freeipa.server.conf
483b06
 %config(noreplace) %{_sysconfdir}/oddjobd.conf.d/ipa-server.conf
483b06
+%config(noreplace) %{_sysconfdir}/krb5.conf.d/ipa-certauth
483b06
 %dir %{_libexecdir}/ipa/certmonger
483b06
 %attr(755,root,root) %{_libexecdir}/ipa/certmonger/*
483b06
 # NOTE: systemd specific section
483b06
diff --git a/server.m4 b/server.m4
483b06
index 92b5cdd3a6ff90b70a9002360ff3d3aec5053392..7b2e94df91a4803849e496142788a4ed87ef487d 100644
483b06
--- a/server.m4
483b06
+++ b/server.m4
483b06
@@ -30,6 +30,19 @@ dnl -- sss_idmap is needed by the extdom exop --
483b06
 PKG_CHECK_MODULES([SSSIDMAP], [sss_idmap])
483b06
 PKG_CHECK_MODULES([SSSNSSIDMAP], [sss_nss_idmap >= 1.13.90])
483b06
 
483b06
+dnl -- sss_certmap and certauth.h are needed by the IPA KDB certauth plugin --
483b06
+PKG_CHECK_EXISTS([sss_certmap],
483b06
+                 [PKG_CHECK_MODULES([SSSCERTMAP], [sss_certmap])],
483b06
+                 [AC_MSG_NOTICE([sss_certmap not found])])
483b06
+AC_CHECK_HEADER([krb5/certauth_plugin.h],
483b06
+                [have_certauth_plugin=yes],
483b06
+                [have_certauth_plugin=no])
483b06
+AM_CONDITIONAL([BUILD_IPA_CERTAUTH_PLUGIN],
483b06
+               [test x$have_certauth_plugin = xyes -a x"$SSSCERTMAP_LIBS" != x])
483b06
+AM_COND_IF([BUILD_IPA_CERTAUTH_PLUGIN],
483b06
+           [AC_MSG_NOTICE([Build IPA KDB certauth plugin])],
483b06
+           [AC_MSG_WARN([Cannot build IPA KDB certauth plugin])])
483b06
+
483b06
 dnl ---------------------------------------------------------------------------
483b06
 dnl - Check for KRB5 krad
483b06
 dnl ---------------------------------------------------------------------------
483b06
-- 
483b06
2.12.1
483b06