[Date Prev][Date Next] [Chronological] [Thread] [Top]

ldapdb auxprop patch for cyrus-sasl 2.1.13



This is a patch for Cyrus-SASL 2.1.13 for including the LDAPDB auxprop
module that is in the contrib dir for openldap 2.1.x with some minor
patches (from the CVS of openldap) and makefile/config script
modifications (blatantly ripped from Simon Loader's ldapauxprop patch). 

-- 
Edward Rudd <eddie@omegaware.com>
diff -Nur --exclude=Makefile.in cyrus-sasl-2.1.13.orig/acconfig.h cyrus-sasl-2.1.13/acconfig.h
--- cyrus-sasl-2.1.13.orig/acconfig.h	Thu Feb 13 13:55:48 2003
+++ cyrus-sasl-2.1.13/acconfig.h	Tue Jun  3 20:05:34 2003
@@ -75,6 +75,9 @@
 #undef STATIC_SASLDB
 #undef STATIC_SRP
 
+/* auxprop mechs we can link staticly? */
+#undef STATIC_LDAPDB
+
 /* This is where plugins will live at runtime */
 #undef PLUGINDIR
 
diff -Nur --exclude=Makefile.in cyrus-sasl-2.1.13.orig/configure.in cyrus-sasl-2.1.13/configure.in
--- cyrus-sasl-2.1.13.orig/configure.in	Mon Apr 14 12:59:44 2003
+++ cyrus-sasl-2.1.13/configure.in	Tue Jun  3 20:09:45 2003
@@ -618,6 +618,50 @@
 esac
 AC_SUBST(LIB_MYSQL)
 
+########################################################################
+# Simon tries to do autoconf for ldap (has a book now)
+
+dnl LDAP
+AC_ARG_WITH(ldapdb, [  --with-ldapdb=PATH         enable authentication from LDAP [no] ],
+  with_ldapdb=$withval,
+  with_ldapdb=no)
+
+if test "$with_ldapdb" =  "yes"; then
+  for ldaploc in lib/ldap lib
+  do
+    if test -f ${prefix}/${ldaploc}/libldap.a; then
+      with_ldapdb="${prefix}"
+      break
+    elif test -f /usr/local/${ldaploc}/libldap.a; then
+      with_ldapdb="/usr/local"
+      break
+    elif test -f /usr/${ldaploc}/libldap.a; then
+      with_ldapdb="/usr"
+      break
+    fi
+  done
+fi
+
+case "$with_ldapdb" in
+    no) true;;
+    *)
+     if test -d ${with_ldapdb}/include/ldap; then
+         CPPFLAGS="${CPPFLAGS} -I${with_ldapdb}/include/ldap"
+         LIB_LDAP="-L${with_ldapdb}/lib/ldap"
+     else
+         CPPFLAGS="${CPPFLAGS} -I${with_ldapdb}/include"
+         LIB_LDAP="-L${with_ldapdb}/lib"
+       fi
+       LIB_LDAP="$LIB_LDAP -lldap -llber"
+       AC_CHECK_LIB(ldap, ldap_open,[
+            SASL_MECHS="$SASL_MECHS libldapdb.la"
+            SASL_STATIC_OBJS="$SASL_STATIC_OBJS ../plugins/ldapdb.o"
+            AC_DEFINE(STATIC_LDAPDB)],
+            [AC_ERROR([LDAP libarary ldap and lber not found])],
+            [-llber -lssl -lcrypto]);;
+esac
+AC_SUBST(LIB_LDAP)
+
 # simon finishes trying to do autoconf
 ###############################################################################
 
diff -Nur cyrus-sasl-2.1.13.orig/lib/staticopen.h cyrus-sasl-2.1.13/lib/staticopen.h
--- cyrus-sasl-2.1.13.orig/lib/staticopen.h	Tue Jun  3 20:43:55 2003
+++ cyrus-sasl-2.1.13/lib/staticopen.h	Tue Jun  3 21:33:23 2003
@@ -119,6 +119,9 @@
 #ifdef STATIC_SASLDB
 extern SPECIFIC_AUXPROP_PLUG_INIT_PROTO( sasldb );
 #endif
+#ifdef STATIC_LDAPDB
+extern SPECIFIC_AUXPROP_PLUG_INIT_PROTO( ldapdb );
+#endif
 #ifdef STATIC_MYSQL
 extern SPECIFIC_AUXPROP_PLUG_INIT_PROTO( mysql );
 #endif
@@ -167,6 +170,9 @@
 #ifdef STATIC_SASLDB
 	SPECIFIC_AUXPROP_PLUG_INIT( sasldb, "SASLDB" ),
 #endif
+#ifdef STATIC_LDAPDB
+	SPECIFIC_AUXPROP_PLUG_INIT( ldapdb, "LDAPDB" ),
+#endif
 #ifdef STATIC_MYSQL
 	SPECIFIC_AUXPROP_PLUG_INIT( mysql, "MYSQL" ),
 #endif
diff -Nur cyrus-sasl-2.1.13.orig/plugins/Makefile.am cyrus-sasl-2.1.13/plugins/Makefile.am
--- cyrus-sasl-2.1.13.orig/plugins/Makefile.am	Tue Jun  3 20:43:55 2003
+++ cyrus-sasl-2.1.13/plugins/Makefile.am	Tue Jun  3 21:32:20 2003
@@ -58,6 +58,7 @@
 ntlm_version = 2:13:0
 otp_version = 2:13:0
 srp_version = 2:13:0
+ldapdb_version = 2:13:0
 
 INCLUDES=-I$(top_srcdir)/include -I$(top_srcdir)/lib -I$(top_srcdir)/sasldb
 LDFLAGS = @LDFLAGS@ -module -export-dynamic -rpath $(plugindir)
@@ -78,7 +79,7 @@
 sasl_LTLIBRARIES = @SASL_MECHS@
 EXTRA_LTLIBRARIES = libplain.la libanonymous.la libkerberos4.la libcrammd5.la \
 	libgssapiv2.la libdigestmd5.la liblogin.la libsrp.la libotp.la \
-	libntlm.la libsasldb.la libmysql.la
+	libntlm.la libsasldb.la libmysql.la libldapdb.la
 
 libplain_la_SOURCES = plain.c plain_init.c $(common_sources)
 libplain_la_LDFLAGS = -version-info $(plain_version)
@@ -141,12 +142,18 @@
 libmysql_la_DEPENDENCIES = $(COMPAT_OBJS)
 libmysql_la_LIBADD = $(COMPAT_OBJS)
 
+# Auxprop Plugins Ldapdb
+libldapdb_la_SOURCES = ldapdb.c ldapdb_init.c $(common_sources)
+libldapdb_la_LDFLAGS = $(LIB_LDAP) -version-info $(ldapdb_version)
+libldapdb_la_DEPENDENCIES = $(COMPAT_OBJS)
+libldapdb_la_LIBADD = $(COMPAT_OBJS)
+
 
 # Instructions for making the _init files
 
 init_src=anonymous_init.c crammd5_init.c digestmd5_init.c gssapiv2_init.c \
 kerberos4_init.c login_init.c plain_init.c srp_init.c otp_init.c ntlm_init.c \
-sasldb_init.c mysqlauxprop_init.c
+sasldb_init.c mysqlauxprop_init.c ldapdb_init.c
 
 
 CLEANFILES=$(init_src)
diff -Nur cyrus-sasl-2.1.13.orig/plugins/ldapdb.c cyrus-sasl-2.1.13/plugins/ldapdb.c
--- cyrus-sasl-2.1.13.orig/plugins/ldapdb.c	Wed Dec 31 18:00:00 1969
+++ cyrus-sasl-2.1.13/plugins/ldapdb.c	Tue Jun  3 21:30:45 2003
@@ -0,0 +1,238 @@
+/* SASL LDAP auxprop implementation
+ * Copyright (C) 2002,2003 Howard Chu, hyc@symas.com
+ *
+ * Permission is granted to anyone to use this software for any purpose
+ * on any computer system, and to alter it and redistribute it, subject
+ * to the following restrictions:
+ *
+ * 1. The author is not responsible for the consequences of use of this
+ *    software, no matter how awful, even if they arise from flaws in it.
+ *
+ * 2. The origin of this software must not be misrepresented, either by
+ *    explicit claim or by omission.  Since few users ever read sources,
+ *    credits should appear in the documentation.
+ *
+ * 3. Altered versions must be plainly marked as such, and must not be
+ *    misrepresented as being the original software.  Since few users
+ *    ever read sources, credits should appear in the documentation.
+ *
+ * 4. This notice may not be removed or altered.
+ */
+
+#include <config.h>
+
+#include <stdio.h>
+
+#include "sasl.h"
+#include "saslutil.h"
+#include "saslplug.h"
+
+#include "plugin_common.h"
+
+#include <ldap.h>
+
+static char ldapdb[] = "ldapdb";
+
+typedef struct ldapctx {
+	const char *uri;	/* URI of LDAP server */
+	struct berval id;	/* SASL authcid to bind as */
+	struct berval pw;	/* password for bind */
+	struct berval mech;	/* SASL mech */
+} ldapctx;
+
+typedef struct gluectx {
+	ldapctx *lc;
+	sasl_server_params_t *lp;
+	struct berval user;
+} gluectx;
+
+static int ldapdb_interact(LDAP *ld, unsigned flags __attribute__((unused)),
+	void *def, void *inter)
+{
+	sasl_interact_t *in = inter;
+	gluectx *gc = def;
+	struct berval p;
+
+	for (;in->id != SASL_CB_LIST_END;in++)
+	{
+		p.bv_val = NULL;
+		switch(in->id)
+		{
+			case SASL_CB_GETREALM:
+				ldap_get_option(ld, LDAP_OPT_X_SASL_REALM, &p.bv_val);
+				if (p.bv_val) p.bv_len = strlen(p.bv_val);
+				break;		
+			case SASL_CB_AUTHNAME:
+				p = gc->lc->id;
+				break;
+			case SASL_CB_PASS:
+				p = gc->lc->pw;
+				break;
+			case SASL_CB_USER:
+				p = gc->user;
+				break;
+		}
+		if (p.bv_val)
+		{
+			in->result = p.bv_val;
+			in->len = p.bv_len;
+		}
+	}
+	return LDAP_SUCCESS;
+}
+
+static void ldapdb_auxprop_lookup(void *glob_context,
+				  sasl_server_params_t *sparams,
+				  unsigned flags,
+				  const char *user,
+				  unsigned ulen)
+{
+    ldapctx *ctx = glob_context;
+    int ret, i, n, *aindx;
+    const struct propval *pr;
+    LDAP *ld = NULL;
+    gluectx gc;
+    struct berval *dn = NULL, **bvals;
+    LDAPMessage *msg, *res;
+    char **attrs = NULL, *authzid = NULL;
+    
+    if(!ctx || !sparams || !user) return;
+
+    pr = sparams->utils->prop_get(sparams->propctx);
+    if(!pr) return;
+
+    /* count how many attrs to fetch */
+    for(i = 0, n = 0; pr[i].name; i++) {
+	if(pr[i].name[0] == '*' && (flags & SASL_AUXPROP_AUTHZID))
+	    continue;
+	if(pr[i].values && !(flags & SASL_AUXPROP_OVERRIDE))
+	    continue;
+	n++;
+    }
+    /* nothing to do, bail out */
+    if (!n) return;
+
+    /* alloc an array of attr names for search, and index to the props */
+    attrs = sparams->utils->malloc((n+1)*sizeof(char *)*2);
+    if (!attrs) return;
+
+    aindx = (int *)(attrs + n + 1);
+
+    /* copy attr list */
+    for (i=0, n=0; pr[i].name; i++) {
+	if(pr[i].name[0] == '*' && (flags & SASL_AUXPROP_AUTHZID))
+	    continue;
+	if(pr[i].values && !(flags & SASL_AUXPROP_OVERRIDE))
+	    continue;
+    	attrs[n] = (char *)pr[i].name;
+	if (pr[i].name[0] == '*') attrs[n]++;
+	aindx[n] = i;
+	n++;
+    }
+    attrs[n] = NULL;
+    	
+    if(ldap_initialize(&ld, ctx->uri)) {
+    	sparams->utils->free(attrs);
+    	return;
+    }
+
+    authzid = sparams->utils->malloc(ulen + sizeof("u:"));
+    if (!authzid) goto done;
+	gc.lc = ctx;
+	gc.lp = sparams;
+    strcpy(authzid, "u:");
+    strcpy(authzid+2, user);
+    gc.user.bv_val = authzid;
+	gc.user.bv_len = ulen + sizeof("u:") - 1;
+
+    i = LDAP_VERSION3;
+    ret = ldap_set_option(ld, LDAP_OPT_PROTOCOL_VERSION, &i);
+
+    ret = ldap_sasl_interactive_bind_s(ld, NULL, ctx->mech.bv_val, NULL, NULL,
+    	LDAP_SASL_QUIET, ldapdb_interact, &gc);
+    if (ret != LDAP_SUCCESS) goto done;
+    
+    ret = ldap_extended_operation_s(ld, LDAP_EXOP_X_WHO_AM_I, NULL, NULL,
+    	NULL, NULL, &dn);
+    if (ret != LDAP_SUCCESS || !dn) goto done;
+    
+    if (dn->bv_val && !strncmp(dn->bv_val, "dn:", 3))
+    ret = ldap_search_s(ld, dn->bv_val+3, LDAP_SCOPE_BASE, "(objectclass=*)",
+    	attrs, 0, &res);
+    ber_bvfree(dn);
+
+    if (ret != LDAP_SUCCESS) goto done;
+
+    for(msg=ldap_first_message(ld, res); msg; msg=ldap_next_message(ld, msg))
+    {
+    	if (ldap_msgtype(msg) != LDAP_RES_SEARCH_ENTRY) continue;
+	for (i=0; i<n; i++)
+	{
+	    bvals = ldap_get_values_len(ld, msg, attrs[i]);
+	    if (!bvals) continue;
+	    if (pr[aindx[i]].values)
+	    	sparams->utils->prop_erase(sparams->propctx, pr[aindx[i]].name);
+	    sparams->utils->prop_set(sparams->propctx, pr[aindx[i]].name,
+				 bvals[0]->bv_val, bvals[0]->bv_len);
+	    ber_bvecfree(bvals);
+	}
+    }
+    ldap_msgfree(res);
+
+ done:
+    if(authzid) sparams->utils->free(authzid);
+    if(attrs) sparams->utils->free(attrs);
+    if(ld) ldap_unbind(ld);
+}
+
+static void ldapdb_auxprop_free(void *glob_ctx, const sasl_utils_t *utils)
+{
+	utils->free(glob_ctx);
+}
+
+static sasl_auxprop_plug_t ldapdb_auxprop_plugin = {
+    0,           /* Features */
+    0,           /* spare */
+    NULL,        /* glob_context */
+    ldapdb_auxprop_free,	/* auxprop_free */
+    ldapdb_auxprop_lookup, /* auxprop_lookup */
+    ldapdb,    /* name */
+    NULL         /* spare */
+};
+
+int ldapdb_auxprop_plug_init(const sasl_utils_t *utils,
+                             int max_version,
+                             int *out_version,
+                             sasl_auxprop_plug_t **plug,
+                             const char *plugname __attribute__((unused))) 
+{
+    ldapctx tmp, *p;
+    const char *s;
+
+    if(!out_version || !plug) return SASL_BADPARAM;
+
+    if(max_version < SASL_AUXPROP_PLUG_VERSION) return SASL_BADVERS;
+    
+    utils->getopt(utils->getopt_context, ldapdb, "ldapdb_uri", &tmp.uri, NULL);
+    if(!tmp.uri) return SASL_BADPARAM;
+
+    utils->getopt(utils->getopt_context, ldapdb, "ldapdb_id",
+		(const char **)&tmp.id.bv_val, (unsigned *)&tmp.id.bv_len);
+    utils->getopt(utils->getopt_context, ldapdb, "ldapdb_pw",
+		(const char **)&tmp.pw.bv_val, (unsigned *)&tmp.pw.bv_len);
+    utils->getopt(utils->getopt_context, ldapdb, "ldapdb_mech",
+		(const char **)&tmp.mech.bv_val, (unsigned *)&tmp.mech.bv_len);
+    utils->getopt(utils->getopt_context, ldapdb, "ldapdb_rc", &s, NULL);
+    if(s && setenv("LDAPRC", s, 1)) return SASL_BADPARAM;
+
+    p = utils->malloc(sizeof(ldapctx));
+    if (!p) return SASL_NOMEM;
+    *p = tmp;
+    ldapdb_auxprop_plugin.glob_context = p;
+
+    *out_version = SASL_AUXPROP_PLUG_VERSION;
+
+    *plug = &ldapdb_auxprop_plugin;
+
+    return SASL_OK;
+}
diff -Nur cyrus-sasl-2.1.13.orig/plugins/makeinit.sh cyrus-sasl-2.1.13/plugins/makeinit.sh
--- cyrus-sasl-2.1.13.orig/plugins/makeinit.sh	Tue Jun  3 20:43:55 2003
+++ cyrus-sasl-2.1.13/plugins/makeinit.sh	Tue Jun  3 21:32:39 2003
@@ -45,7 +45,7 @@
 " > ${mech}_init.c
 done
 
-for mech in sasldb mysql ; do
+for mech in sasldb mysql ldapdb ; do
 
 echo "
 #include <config.h>