9568cf3
diff -up dhcp-4.3.2b1/configure.ac.ldapgssapi dhcp-4.3.2b1/configure.ac
9568cf3
--- dhcp-4.3.2b1/configure.ac.ldapgssapi	2015-02-08 18:01:27.962621131 +0100
9568cf3
+++ dhcp-4.3.2b1/configure.ac	2015-02-08 18:01:27.966621074 +0100
18026f4
@@ -671,19 +671,40 @@ AC_ARG_WITH(ldapcrypto,
18026f4
     [ldapcrypto=$withval],
18026f4
     [ldapcrypto=no])
18026f4
 
18026f4
+# Gssapi to allow LDAP to authenticate with a keytab
18026f4
+AC_ARG_WITH(krb5,
18026f4
+    AC_HELP_STRING([--with-krb5],
18026f4
+                   [enable krb5/gssapi authentication for OpenLDAP in dhcpd (default is no)]),
18026f4
+    [krb5=$withval],
18026f4
+    [krb5=no])
18026f4
+
18026f4
 # OpenLDAP support is disabled by default, if enabled then SSL support is an
18026f4
 # extra optional that is also disabled by default.  Enabling LDAP SSL support
18026f4
-# implies enabling LDAP support.
18026f4
-if test x$ldap = xyes || test x$ldapcrypto = xyes ; then
18026f4
+# implies enabling LDAP support. Similarly, KRB5 support implies LDAP support,
18026f4
+# but doesn't include SSL. The two are not dependant.
18026f4
+if test x$ldap = xyes || test x$ldapcrypto = xyes || test x$krb5 = xyes; then
18026f4
     AC_SEARCH_LIBS(ldap_initialize, [ldap], ,
18026f4
 		   AC_MSG_FAILURE([*** Cannot find ldap_initialize with -lldap - do you need to install an OpenLDAP2 Devel package?]))
18026f4
     AC_SEARCH_LIBS(ber_pvt_opt_on, [lber], ,
18026f4
 		   AC_MSG_FAILURE([*** Cannot find ber_pvt_opt_on with -llber - do you need to install an OpenLDAP2 Devel package?]))
18026f4
-
18026f4
+    if test x$krb5 = xyes ; then
18026f4
+    AC_SEARCH_LIBS(krb5_init_context, [krb5], ,
18026f4
+		   AC_MSG_FAILURE([*** Cannot find krb5_init_context with -lkrb5 - do you need to install a Kerberos Devel package?]))
18026f4
+    fi
18026f4
+ 
18026f4
+    # Can this be done better?
18026f4
     if test x$ldapcrypto = xyes ; then
18026f4
-	AC_SUBST(LDAP_CFLAGS, ["-DLDAP_CONFIGURATION -DLDAP_USE_SSL"])
18026f4
+        if test x$krb5 = xyes; then
18026f4
+	    AC_SUBST(LDAP_CFLAGS, ["-DLDAP_CONFIGURATION -DLDAP_USE_SSL -DLDAP_USE_GSSAPI"])
18026f4
+        else
18026f4
+	    AC_SUBST(LDAP_CFLAGS, ["-DLDAP_CONFIGURATION -DLDAP_USE_SSL"])
18026f4
+        fi
18026f4
     else
18026f4
-	AC_SUBST(LDAP_CFLAGS, ["-DLDAP_CONFIGURATION"])
18026f4
+        if test x$krb5 = xyes; then
18026f4
+	    AC_SUBST(LDAP_CFLAGS, ["-DLDAP_CONFIGURATION -DLDAP_USE_GSSAPI"])
18026f4
+        else
18026f4
+	    AC_SUBST(LDAP_CFLAGS, ["-DLDAP_CONFIGURATION"])
18026f4
+        fi
18026f4
     fi
18026f4
 fi
18026f4
 
9568cf3
diff -up dhcp-4.3.2b1/includes/dhcpd.h.ldapgssapi dhcp-4.3.2b1/includes/dhcpd.h
9568cf3
--- dhcp-4.3.2b1/includes/dhcpd.h.ldapgssapi	2015-02-08 18:01:27.924621669 +0100
9568cf3
+++ dhcp-4.3.2b1/includes/dhcpd.h	2015-02-08 18:03:03.145273551 +0100
18026f4
@@ -103,7 +103,14 @@ typedef time_t TIME;
18026f4
 #if defined(LDAP_CONFIGURATION)
18026f4
 # include <ldap.h>
18026f4
 # include <sys/utsname.h> /* for uname() */
18026f4
-#endif
18026f4
+# if defined(LDAP_USE_GSSAPI)                                                     
18026f4
+#  include <krb5.h>
18026f4
+#  include <string.h>
18026f4
+#  include <stdio.h>
18026f4
+#  include <time.h>
18026f4
+#  include <unistd.h>
18026f4
+# endif /* GSSAPI */
18026f4
+#endif /* LDAP CONFIGURATION */
18026f4
 
18026f4
 #if !defined (BYTE_NAME_HASH_SIZE)
18026f4
 # define BYTE_NAME_HASH_SIZE	401	/* Default would be ridiculous. */
9568cf3
@@ -743,6 +750,13 @@ struct lease_state {
9568cf3
 #define SV_SERVER_ID_CHECK		86
9568cf3
 #define SV_PREFIX_LEN_MODE		87
18026f4
 
18026f4
+#if defined(LDAP_CONFIGURATION)
18026f4
+#if defined (LDAP_USE_GSSAPI)
9568cf3
+# define SV_LDAP_GSSAPI_KEYTAB	 	88
9568cf3
+# define SV_LDAP_GSSAPI_PRINCIPAL 	89
18026f4
+#endif
18026f4
+#endif
18026f4
+
18026f4
 #if !defined (DEFAULT_PING_TIMEOUT)
18026f4
 # define DEFAULT_PING_TIMEOUT 1
18026f4
 #endif
9568cf3
diff -up dhcp-4.3.2b1/server/krb_helper.c.ldapgssapi dhcp-4.3.2b1/server/krb_helper.c
9568cf3
--- dhcp-4.3.2b1/server/krb_helper.c.ldapgssapi	2015-02-08 18:01:27.967621060 +0100
9568cf3
+++ dhcp-4.3.2b1/server/krb_helper.c	2015-02-08 18:01:27.967621060 +0100
18026f4
@@ -0,0 +1,220 @@
18026f4
+/* krb_helper.c
18026f4
+
18026f4
+   Helper routings for allowing LDAP to read configuration with GSSAPI/krb auth */
18026f4
+
18026f4
+/*
18026f4
+ * Copyright (c) 2014 William B.
18026f4
+ * All rights reserved.
18026f4
+ *
18026f4
+ * Redistribution and use in source and binary forms, with or without
18026f4
+ * modification, are permitted provided that the following conditions
18026f4
+ * are met:
18026f4
+ *
18026f4
+ * 1. Redistributions of source code must retain the above copyright
18026f4
+ *    notice, this list of conditions and the following disclaimer.
18026f4
+ * 2. Redistributions in binary form must reproduce the above copyright
18026f4
+ *    notice, this list of conditions and the following disclaimer in the
18026f4
+ *    documentation and/or other materials provided with the distribution.
18026f4
+ * 3. Neither the name of The Internet Software Consortium nor the names
18026f4
+ *    of its contributors may be used to endorse or promote products derived
18026f4
+ *    from this software without specific prior written permission.
18026f4
+ *
18026f4
+ * THIS SOFTWARE IS PROVIDED BY THE INTERNET SOFTWARE CONSORTIUM AND
18026f4
+ * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18026f4
+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18026f4
+ * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18026f4
+ * DISCLAIMED.  IN NO EVENT SHALL THE INTERNET SOFTWARE CONSORTIUM OR
18026f4
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
18026f4
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
18026f4
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
18026f4
+ * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
18026f4
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
18026f4
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
18026f4
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
18026f4
+ * SUCH DAMAGE.
18026f4
+ *
18026f4
+ * This helper was written by William Brown <william@adelaide.edu.au>, 
18026f4
+ * inspired by krb5_helper.c from bind-dyndb-ldap by Simo Sorce (Redhat)
18026f4
+ */
18026f4
+
18026f4
+#include "dhcpd.h"
18026f4
+#include "krb_helper.h"
18026f4
+
18026f4
+#if defined(LDAP_USE_GSSAPI)
18026f4
+
18026f4
+//#include "ktinit.h"
18026f4
+//#include <string.h>
18026f4
+//#include <krb5.h>
18026f4
+//#include <stdio.h>
18026f4
+//#include <unistd.h>
18026f4
+//#include <time.h>
18026f4
+
18026f4
+#define KRB_DEFAULT_KEYTAB "FILE:/etc/dhcp/dhcp.keytab"
18026f4
+#define KRB_MIN_TIME 300
18026f4
+
18026f4
+#define CHECK_KRB5(ctx, err, msg, ...) \
18026f4
+    do { \
18026f4
+        if (err) { \
18026f4
+            const char * errmsg = krb5_get_error_message(ctx, err); \
18026f4
+            log_error("Err: %s -> %s\n", msg, errmsg); \
18026f4
+            result = ISC_R_FAILURE; \
18026f4
+            goto cleanup; \
18026f4
+        } \
18026f4
+    } while (0)
18026f4
+
18026f4
+#define CHECK(ret_code, msg) \
18026f4
+    if (ret_code != 0) { \
18026f4
+        log_error("Error, %i %s\n", ret_code, msg); \
18026f4
+        goto cleanup; \
18026f4
+    }
18026f4
+
18026f4
+static isc_result_t
18026f4
+check_credentials(krb5_context context, krb5_ccache ccache, krb5_principal service)
18026f4
+{
18026f4
+    char *realm = NULL;
18026f4
+    krb5_creds creds;
18026f4
+    krb5_creds mcreds;
18026f4
+    krb5_error_code krberr;
18026f4
+    krb5_timestamp now;
18026f4
+    isc_result_t result = ISC_R_FAILURE;
18026f4
+
18026f4
+    memset(&mcreds, 0, sizeof(mcreds));
18026f4
+    memset(&creds, 0, sizeof(creds));
18026f4
+
18026f4
+    krberr = krb5_get_default_realm(context, &realm;;
18026f4
+    CHECK_KRB5(context, krberr, "Failed to retrieve default realm");
18026f4
+
18026f4
+    krberr = krb5_build_principal(context, &mcreds.server,
18026f4
+                    strlen(realm), realm,
18026f4
+                    "krbtgt", realm, NULL);
18026f4
+    CHECK_KRB5(context, krberr, "Failed to build 'krbtgt/REALM' principal");
18026f4
+
18026f4
+    mcreds.client = service;
18026f4
+
18026f4
+    krberr = krb5_cc_retrieve_cred(context, ccache, 0, &mcreds, &creds);
18026f4
+
18026f4
+    if (krberr) {
18026f4
+        const char * errmsg = krb5_get_error_message(context, krberr);
18026f4
+        log_error("Credentials are not present in cache (%s)\n", errmsg);
18026f4
+        krb5_free_error_message(context, errmsg);
18026f4
+        result = ISC_R_FAILURE;
18026f4
+        goto cleanup;
18026f4
+    }
18026f4
+    CHECK_KRB5(context, krberr, "Credentials are not present in cache ");
18026f4
+   
18026f4
+    krberr = krb5_timeofday(context, &now;;
18026f4
+    CHECK_KRB5(context, krberr, "Failed to get time of day");
18026f4
+
18026f4
+
18026f4
+    if (now > (creds.times.endtime + KRB_MIN_TIME)) {
18026f4
+        log_error("Credentials cache expired");
18026f4
+        result = ISC_R_FAILURE;
18026f4
+        goto cleanup;
18026f4
+    } else { 
18026f4
+        char buf[255];
18026f4
+        char fill = ' ';
18026f4
+        krb5_timestamp_to_sfstring(creds.times.endtime, buf, 16, &fill;;
18026f4
+        log_info("Credentials valid til %s\n", buf);
18026f4
+    }
18026f4
+
18026f4
+    result = ISC_R_SUCCESS;
18026f4
+
18026f4
+cleanup:
18026f4
+    krb5_free_cred_contents(context, &creds);
18026f4
+    if (mcreds.server) krb5_free_principal(context, mcreds.server);
18026f4
+    if (realm) krb5_free_default_realm(context, realm);
18026f4
+    return result;
18026f4
+}
18026f4
+
18026f4
+isc_result_t
18026f4
+krb5_get_tgt(const char *principal, const char *keyfile)
18026f4
+{
18026f4
+    isc_result_t result = ISC_R_FAILURE;
18026f4
+    char *ccname = NULL;
18026f4
+    krb5_context context = NULL;
18026f4
+    krb5_error_code krberr;
18026f4
+    krb5_ccache ccache = NULL;
18026f4
+    krb5_principal kprincpw = NULL;
18026f4
+    krb5_creds my_creds;
18026f4
+    krb5_creds * my_creds_ptr = NULL;
18026f4
+    krb5_get_init_creds_opt options;
18026f4
+    krb5_keytab keytab = NULL;
18026f4
+    int ret;
18026f4
+    
18026f4
+    if (keyfile == NULL || keyfile[0] == '\0') {
18026f4
+        keyfile = KRB_DEFAULT_KEYTAB;
18026f4
+        log_info("Using default keytab %s\n", keyfile);
18026f4
+    } else {
18026f4
+        if (strncmp(keyfile, "FILE:", 5) != 0) {
18026f4
+            log_error("Unknown keytab path format: Does it start with FILE:?\n");
18026f4
+            return ISC_R_FAILURE;
18026f4
+        }
18026f4
+    }
18026f4
+
18026f4
+    krberr = krb5_init_context(&context);
18026f4
+    CHECK_KRB5(NULL, krberr, "Kerberos context initialization failed");
18026f4
+
18026f4
+    result = ISC_R_SUCCESS;
18026f4
+
18026f4
+    ccname = "MEMORY:dhcp_ld_krb5_cc";
18026f4
+    log_info("Using ccache %s\n" , ccname);
18026f4
+
18026f4
+    ret = setenv("KRB5CCNAME", ccname, 1);
18026f4
+    if (ret == -1) {
18026f4
+        log_error("Failed to setup environment\n");
18026f4
+        result = ISC_R_FAILURE;
18026f4
+        goto cleanup;
18026f4
+    }
18026f4
+
18026f4
+    krberr = krb5_cc_resolve(context, ccname, &ccache);
18026f4
+    CHECK_KRB5(context, krberr, "Couldnt resolve ccache '%s'", ccname);
18026f4
+
18026f4
+    krberr = krb5_parse_name(context, principal, &kprincpw);
18026f4
+    CHECK_KRB5(context, krberr, "Failed to parse princ '%s'", princpal);
18026f4
+
18026f4
+    result = check_credentials(context, ccache, kprincpw);
18026f4
+    if (result == ISC_R_SUCCESS) {
18026f4
+        log_info("Found valid kerberos credentials\n");
18026f4
+        goto cleanup;
18026f4
+    } else {
18026f4
+        log_error("No valid krb5 credentials\n");
18026f4
+    }
18026f4
+
18026f4
+    krberr = krb5_kt_resolve(context, keyfile, &keytab);
18026f4
+    CHECK_KRB5(context, krberr, 
18026f4
+            "Failed to resolve kt files '%s'\n", keyfile);
18026f4
+
18026f4
+    memset(&my_creds, 0, sizeof(my_creds));
18026f4
+    memset(&options, 0, sizeof(options));
18026f4
+
18026f4
+    krb5_get_init_creds_opt_set_tkt_life(&options, KRB_MIN_TIME * 2);
18026f4
+    krb5_get_init_creds_opt_set_address_list(&options, NULL);
18026f4
+    krb5_get_init_creds_opt_set_forwardable(&options, 0);
18026f4
+    krb5_get_init_creds_opt_set_proxiable(&options, 0);
18026f4
+
18026f4
+    krberr = krb5_get_init_creds_keytab(context, &my_creds, kprincpw,
18026f4
+                keytab, 0, NULL, &options);
18026f4
+    CHECK_KRB5(context, krberr, "Failed to get initial credentials TGT\n");
18026f4
+    
18026f4
+    my_creds_ptr = &my_creds;
18026f4
+
18026f4
+    krberr = krb5_cc_initialize(context, ccache, kprincpw);
18026f4
+    CHECK_KRB5(context, krberr, "Failed to init ccache\n");
18026f4
+
18026f4
+    krberr = krb5_cc_store_cred(context, ccache, &my_creds);
18026f4
+    CHECK_KRB5(context, krberr, "Failed to store credentials\n");
18026f4
+
18026f4
+    result = ISC_R_SUCCESS;
18026f4
+    log_info("Successfully init krb tgt %s", principal);
18026f4
+
18026f4
+cleanup:
18026f4
+    if (ccache) krb5_cc_close(context, ccache);
18026f4
+    if (keytab) krb5_kt_close(context, keytab);
18026f4
+    if (kprincpw) krb5_free_principal(context, kprincpw);
18026f4
+    if (my_creds_ptr) krb5_free_cred_contents(context, &my_creds);
18026f4
+    if (context) krb5_free_context(context);
18026f4
+    return result;
18026f4
+}
18026f4
+
18026f4
+#endif
18026f4
+
9568cf3
diff -up dhcp-4.3.2b1/server/krb_helper.h.ldapgssapi dhcp-4.3.2b1/server/krb_helper.h
9568cf3
--- dhcp-4.3.2b1/server/krb_helper.h.ldapgssapi	2015-02-08 18:01:27.968621046 +0100
9568cf3
+++ dhcp-4.3.2b1/server/krb_helper.h	2015-02-08 18:01:27.967621060 +0100
18026f4
@@ -0,0 +1,40 @@
18026f4
+/* krb_helper.h
18026f4
+
18026f4
+   Helper routings for allowing LDAP to read configuration with GSSAPI/krb auth */
18026f4
+
18026f4
+/*
18026f4
+ * Copyright (c) 2014 William B.
18026f4
+ * All rights reserved.
18026f4
+ *
18026f4
+ * Redistribution and use in source and binary forms, with or without
18026f4
+ * modification, are permitted provided that the following conditions
18026f4
+ * are met:
18026f4
+ *
18026f4
+ * 1. Redistributions of source code must retain the above copyright
18026f4
+ *    notice, this list of conditions and the following disclaimer.
18026f4
+ * 2. Redistributions in binary form must reproduce the above copyright
18026f4
+ *    notice, this list of conditions and the following disclaimer in the
18026f4
+ *    documentation and/or other materials provided with the distribution.
18026f4
+ * 3. Neither the name of The Internet Software Consortium nor the names
18026f4
+ *    of its contributors may be used to endorse or promote products derived
18026f4
+ *    from this software without specific prior written permission.
18026f4
+ *
18026f4
+ * THIS SOFTWARE IS PROVIDED BY THE INTERNET SOFTWARE CONSORTIUM AND
18026f4
+ * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18026f4
+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18026f4
+ * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18026f4
+ * DISCLAIMED.  IN NO EVENT SHALL THE INTERNET SOFTWARE CONSORTIUM OR
18026f4
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
18026f4
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
18026f4
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
18026f4
+ * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
18026f4
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
18026f4
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
18026f4
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
18026f4
+ * SUCH DAMAGE.
18026f4
+ *
18026f4
+ * This helper was written by William Brown <william@adelaide.edu.au>, 
18026f4
+ * inspired by krb5_helper.c from bind-dyndb-ldap by Simo Sorce (Redhat)
18026f4
+ */
18026f4
+
18026f4
+isc_result_t krb5_get_tgt(const char *, const char *);
9568cf3
diff -up dhcp-4.3.2b1/server/ldap.c.ldapgssapi dhcp-4.3.2b1/server/ldap.c
9568cf3
--- dhcp-4.3.2b1/server/ldap.c.ldapgssapi	2015-02-08 18:01:27.955621230 +0100
9568cf3
+++ dhcp-4.3.2b1/server/ldap.c	2015-02-08 18:01:27.968621046 +0100
18026f4
@@ -39,10 +39,16 @@
18026f4
 
18026f4
 #include "dhcpd.h"
18026f4
 #include <signal.h>
18026f4
-#include <errno.h>
18026f4
+//#include <errno.h>
18026f4
+#define LDAP_DEBUG 1
18026f4
 
18026f4
 #if defined(LDAP_CONFIGURATION)
18026f4
 
18026f4
+#if defined(LDAP_USE_GSSAPI)
18026f4
+#include <sasl/sasl.h>
18026f4
+#include "krb_helper.h"
18026f4
+#endif
18026f4
+
18026f4
 #if defined(LDAP_CASA_AUTH)
18026f4
 #include "ldap_casa.h"
18026f4
 #endif
18026f4
@@ -69,6 +75,20 @@ static char *ldap_tls_ca_file = NULL,
18026f4
             *ldap_tls_ciphers = NULL,
18026f4
             *ldap_tls_randfile = NULL;
18026f4
 #endif
18026f4
+#if defined (LDAP_USE_GSSAPI)
18026f4
+static char *ldap_gssapi_keytab = NULL,
18026f4
+            *ldap_gssapi_principal = NULL;
18026f4
+
18026f4
+static struct ldap_sasl_instance {
18026f4
+    char        *sasl_mech;
18026f4
+    char        *sasl_realm;
18026f4
+    char        *sasl_authz_id;
18026f4
+    char        *sasl_authc_id;
18026f4
+    char        *sasl_password;
18026f4
+};
18026f4
+
18026f4
+static struct ldap_sasl_instance *ldap_sasl_inst = NULL;
18026f4
+#endif 
18026f4
 static struct ldap_config_stack *ldap_stack = NULL;
18026f4
 
18026f4
 typedef struct ldap_dn_node {
18026f4
@@ -545,6 +565,62 @@ _do_lookup_dhcp_enum_option (struct opti
18026f4
   return (ret);
18026f4
 }
18026f4
 
18026f4
+#if defined(LDAP_USE_GSSAPI)
18026f4
+static int
18026f4
+_ldap_sasl_interact(LDAP *ld, unsigned flags, void *defaults, void *sin) 
18026f4
+{
18026f4
+  sasl_interact_t *in;
18026f4
+  struct ldap_sasl_instance *ldap_inst = defaults;
18026f4
+  int ret = LDAP_OTHER;
18026f4
+  size_t size;
18026f4
+
18026f4
+  if (ld == NULL || sin == NULL)
18026f4
+    return LDAP_PARAM_ERROR;
18026f4
+
18026f4
+  log_info("doing interactive bind");
18026f4
+  for (in = sin; in != NULL && in->id != SASL_CB_LIST_END; in++) {
18026f4
+    switch (in->id) {
18026f4
+      case SASL_CB_USER:
18026f4
+        log_info("got request for SASL_CB_USER %s", ldap_inst->sasl_authz_id);
18026f4
+        size = strlen(ldap_inst->sasl_authz_id);
18026f4
+        in->result = ldap_inst->sasl_authz_id;
18026f4
+        in->len = size;
18026f4
+        ret = LDAP_SUCCESS;
18026f4
+        break;
18026f4
+      case SASL_CB_GETREALM:
18026f4
+        log_info("got request for SASL_CB_GETREALM %s", ldap_inst->sasl_realm);
18026f4
+        size = strlen(ldap_inst->sasl_realm);
18026f4
+        in->result = ldap_inst->sasl_realm;
18026f4
+        in->len = size;
18026f4
+        ret = LDAP_SUCCESS;
18026f4
+        break;
18026f4
+      case SASL_CB_AUTHNAME:
18026f4
+        log_info("got request for SASL_CB_AUTHNAME %s", ldap_inst->sasl_authc_id);
18026f4
+        size = strlen(ldap_inst->sasl_authc_id);
18026f4
+        in->result = ldap_inst->sasl_authc_id;
18026f4
+        in->len = size;
18026f4
+        ret = LDAP_SUCCESS;
18026f4
+        break;
18026f4
+      case SASL_CB_PASS:
18026f4
+        log_info("got request for SASL_CB_PASS %s", ldap_inst->sasl_password);
18026f4
+        size = strlen(ldap_inst->sasl_password);
18026f4
+        in->result = ldap_inst->sasl_password;
18026f4
+        in->len = size;
18026f4
+        ret = LDAP_SUCCESS;
18026f4
+        break;
18026f4
+      default:
18026f4
+        goto cleanup;
18026f4
+    }
18026f4
+  }
18026f4
+  return ret;
18026f4
+
18026f4
+cleanup:
18026f4
+  in->result = NULL;
18026f4
+  in->len = 0;
18026f4
+  return LDAP_OTHER;
18026f4
+}
18026f4
+#endif 
18026f4
+
18026f4
 int
18026f4
 ldap_rebind_cb (LDAP *ld, LDAP_CONST char *url, ber_tag_t request, ber_int_t msgid, void *parms)
18026f4
 {
18026f4
@@ -595,20 +671,48 @@ ldap_rebind_cb (LDAP *ld, LDAP_CONST cha
18026f4
     }
18026f4
 #endif
18026f4
 
18026f4
-
18026f4
-  if (ldap_username != NULL || *ldap_username != '\0')
18026f4
+  if (ldap_username != NULL && *ldap_username != '\0')
18026f4
     {
18026f4
-      who = ldap_username;
18026f4
+#if defined(LDAP_USE_GSSAPI)
18026f4
+      if (ldap_gssapi_principal != NULL) {
18026f4
+        log_error("Cannot use gssapi and username / password simultaneously");
18026f4
+        ldap_stop();
18026f4
+        return;
18026f4
+      }
18026f4
+#endif 
18026f4
       creds.bv_val = strdup(ldap_password);
18026f4
       creds.bv_len = strlen(ldap_password);
18026f4
-    }
18026f4
 
18026f4
-  if ((ret = ldap_sasl_bind_s (ld, who, LDAP_SASL_SIMPLE, &creds,
18026f4
-                               NULL, NULL, NULL)) != LDAP_SUCCESS)
18026f4
-    {
18026f4
-      log_error ("Error: Cannot login into ldap server %s:%d: %s",
18026f4
-                 ldapurl->lud_host, ldapurl->lud_port, ldap_err2string (ret));
18026f4
+      if ((ret = ldap_sasl_bind_s (ld, ldap_username, LDAP_SASL_SIMPLE,
18026f4
+                                   &creds, NULL, NULL, NULL)) != LDAP_SUCCESS)
18026f4
+        {
18026f4
+          log_error ("Error: Cannot login into ldap server %s:%d: %s",
18026f4
+                     ldap_server, ldap_port, ldap_err2string (ret));
18026f4
+          ldap_stop();
18026f4
+          return ret;
18026f4
+        }
18026f4
+#if defined(LDAP_USE_GSSAPI)
18026f4
+    } else {
18026f4
+      if (ldap_gssapi_principal != NULL) {
18026f4
+        krb5_get_tgt(ldap_gssapi_principal, ldap_gssapi_keytab);
18026f4
+        if ((ret = ldap_sasl_interactive_bind_s(ld, NULL, ldap_sasl_inst->sasl_mech,
18026f4
+                                                NULL, NULL, LDAP_SASL_AUTOMATIC,
18026f4
+                                                _ldap_sasl_interact, ldap_sasl_inst)
18026f4
+            ) != LDAP_SUCCESS)
18026f4
+        {
18026f4
+          log_error ("Error: Cannot SASL bind to ldap server %s:%d: %s",
18026f4
+                     ldap_server, ldap_port, ldap_err2string (ret));
18026f4
+          char *msg=NULL;
18026f4
+          ldap_get_option( ld, LDAP_OPT_DIAGNOSTIC_MESSAGE, (void*)&msg;;
18026f4
+          log_error ("\tAdditional info: %s", msg);
18026f4
+          ldap_memfree(msg);
18026f4
+          ldap_stop();
18026f4
+          return ret;
18026f4
+        }
18026f4
+      }
18026f4
+#endif
18026f4
     }
18026f4
+
18026f4
   return ret;
18026f4
 }
18026f4
 
18026f4
@@ -618,6 +722,12 @@ ldap_start (void)
18026f4
   struct option_state *options;
18026f4
   int ret, version;
18026f4
   char *uri = NULL;
18026f4
+#if defined(LDAP_USE_GSSAPI)
18026f4
+  char *gssapi_realm = NULL;
18026f4
+  char *gssapi_user = NULL;
18026f4
+  char *running = NULL;
18026f4
+  const char *gssapi_delim = "@";
18026f4
+#endif
18026f4
   struct berval creds;
18026f4
 
18026f4
   if (ld != NULL)
18026f4
@@ -656,6 +766,26 @@ ldap_start (void)
18026f4
           ldap_tls_randfile = _do_lookup_dhcp_string_option (options, SV_LDAP_TLS_RANDFILE);
18026f4
         }
18026f4
 #endif
18026f4
+#if defined (LDAP_USE_GSSAPI)
18026f4
+      ldap_gssapi_keytab = _do_lookup_dhcp_string_option (options, SV_LDAP_GSSAPI_KEYTAB);
18026f4
+      ldap_gssapi_principal = _do_lookup_dhcp_string_option (options, SV_LDAP_GSSAPI_PRINCIPAL);
18026f4
+
18026f4
+      running = strdup(ldap_gssapi_principal);
18026f4
+      gssapi_user = strtok(running, gssapi_delim);
18026f4
+      gssapi_realm = strtok(NULL, gssapi_delim);
18026f4
+      ldap_sasl_inst = malloc(sizeof(struct ldap_sasl_instance));
18026f4
+      if (ldap_sasl_inst == NULL) {
18026f4
+        log_error("Could not allocate memory for sasl instance! Can not run!");
18026f4
+        ldap_stop();
18026f4
+        return;
18026f4
+      }
18026f4
+      ldap_sasl_inst->sasl_mech  = ber_strdup("GSSAPI");
18026f4
+      ldap_sasl_inst->sasl_realm      = ber_strdup(gssapi_realm);
18026f4
+      ldap_sasl_inst->sasl_authz_id   = ber_strdup(gssapi_user);
18026f4
+      ldap_sasl_inst->sasl_authc_id   = NULL;
18026f4
+      ldap_sasl_inst->sasl_password   = NULL; //"" before
18026f4
+      free(running);
18026f4
+#endif
18026f4
 
18026f4
 #if defined (LDAP_CASA_AUTH)
18026f4
       if (!load_uname_pwd_from_miCASA(&ldap_username,&ldap_password))
18026f4
@@ -870,6 +1000,13 @@ ldap_start (void)
18026f4
 
18026f4
   if (ldap_username != NULL && *ldap_username != '\0')
18026f4
     {
18026f4
+#if defined(LDAP_USE_GSSAPI)
18026f4
+      if (ldap_gssapi_principal != NULL) {
18026f4
+        log_error("Cannot use gssapi and username / password simultaneously");
18026f4
+        ldap_stop();
18026f4
+        return;
18026f4
+      }
18026f4
+#endif 
18026f4
       creds.bv_val = strdup(ldap_password);
18026f4
       creds.bv_len = strlen(ldap_password);
18026f4
 
18026f4
@@ -881,6 +1018,26 @@ ldap_start (void)
18026f4
           ldap_stop();
18026f4
           return;
18026f4
         }
18026f4
+#if defined(LDAP_USE_GSSAPI)
18026f4
+    } else {
18026f4
+      if (ldap_gssapi_principal != NULL) {
18026f4
+        krb5_get_tgt(ldap_gssapi_principal, ldap_gssapi_keytab);
18026f4
+        if ((ret = ldap_sasl_interactive_bind_s(ld, NULL, ldap_sasl_inst->sasl_mech,
18026f4
+                                                NULL, NULL, LDAP_SASL_AUTOMATIC,
18026f4
+                                                _ldap_sasl_interact, ldap_sasl_inst)
18026f4
+            ) != LDAP_SUCCESS)
18026f4
+        {
18026f4
+          log_error ("Error: Cannot SASL bind to ldap server %s:%d: %s",
18026f4
+                     ldap_server, ldap_port, ldap_err2string (ret));
18026f4
+          char *msg=NULL;
18026f4
+          ldap_get_option( ld, LDAP_OPT_DIAGNOSTIC_MESSAGE, (void*)&msg;;
18026f4
+          log_error ("\tAdditional info: %s", msg);
18026f4
+          ldap_memfree(msg);
18026f4
+          ldap_stop();
18026f4
+          return;
18026f4
+        }
18026f4
+      }
18026f4
+#endif
18026f4
     }
18026f4
 
18026f4
 #if defined (DEBUG_LDAP)
9568cf3
diff -up dhcp-4.3.2b1/server/Makefile.am.ldapgssapi dhcp-4.3.2b1/server/Makefile.am
9568cf3
--- dhcp-4.3.2b1/server/Makefile.am.ldapgssapi	2015-02-08 18:01:27.943621400 +0100
9568cf3
+++ dhcp-4.3.2b1/server/Makefile.am	2015-02-08 18:01:27.968621046 +0100
18026f4
@@ -10,7 +10,7 @@ dist_sysconf_DATA = dhcpd.conf.example
18026f4
 sbin_PROGRAMS = dhcpd
18026f4
 dhcpd_SOURCES = dhcpd.c dhcp.c bootp.c confpars.c db.c class.c failover.c \
18026f4
 		omapi.c mdb.c stables.c salloc.c ddns.c dhcpleasequery.c \
18026f4
-		dhcpv6.c mdb6.c ldap.c ldap_casa.c probes.d trace.h
18026f4
+		dhcpv6.c mdb6.c ldap.c ldap_casa.c krb_helper.c probes.d trace.h
18026f4
 
18026f4
 dhcpd_CFLAGS = $(LDAP_CFLAGS)
18026f4
 dhcpd_LDADD = ../common/libdhcp.a ../omapip/libomapi.la ../dhcpctl/libdhcpctl.la \
9568cf3
diff -up dhcp-4.3.2b1/server/stables.c.ldapgssapi dhcp-4.3.2b1/server/stables.c
9568cf3
--- dhcp-4.3.2b1/server/stables.c.ldapgssapi	2015-01-29 14:52:57.000000000 +0100
9568cf3
+++ dhcp-4.3.2b1/server/stables.c	2015-02-08 18:01:27.969621032 +0100
18026f4
@@ -259,6 +259,10 @@ static struct option server_options[] =
18026f4
 	{ "ldap-tls-ciphers", "t",		&server_universe,  76, 1 },
18026f4
 	{ "ldap-tls-randfile", "t",		&server_universe,  77, 1 },
18026f4
 #endif /* LDAP_USE_SSL */
18026f4
+#if defined(LDAP_USE_GSSAPI)
18026f4
+	{ "ldap-gssapi-keytab", "t", 		&server_universe,  78, 1},
18026f4
+	{ "ldap-gssapi-principal", "t",		&server_universe,  79, 1},
18026f4
+#endif /* LDAP_USE_GSSAPI */
18026f4
 #endif /* LDAP_CONFIGURATION */
18026f4
 	{ "dhcp-cache-threshold", "B",		&server_universe,  78, 1 },
18026f4
 	{ "dont-use-fsync", "f",		&server_universe,  79, 1 },