2 /* SASL LDAP auxprop implementation
3 * Copyright (C) 2002,2003 Howard Chu, All rights reserved. <hyc@symas.com>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted only as authorized by the OpenLDAP
9 * A copy of this license is available in the file LICENSE in the
10 * top-level directory of the distribution or, alternatively, at
11 * <http://www.OpenLDAP.org/license.html>.
22 #define SASL_VERSION_FULL ((SASL_VERSION_MAJOR << 16) |\
23 (SASL_VERSION_MINOR << 8) |SASL_VERSION_STEP)
25 #include "plugin_common.h"
29 static char ldapdb[] = "ldapdb";
31 typedef struct ldapctx {
32 const char *uri; /* URI of LDAP server */
33 struct berval id; /* SASL authcid to bind as */
34 struct berval pw; /* password for bind */
35 struct berval mech; /* SASL mech */
36 int use_tls; /* Issue StartTLS request? */
39 static int ldapdb_interact(LDAP *ld, unsigned flags __attribute__((unused)),
40 void *def, void *inter)
42 sasl_interact_t *in = inter;
46 for (;in->id != SASL_CB_LIST_END;in++)
51 case SASL_CB_GETREALM:
52 ldap_get_option(ld, LDAP_OPT_X_SASL_REALM, &p.bv_val);
53 if (p.bv_val) p.bv_len = strlen(p.bv_val);
55 case SASL_CB_AUTHNAME:
64 in->result = p.bv_val;
71 typedef struct connparm {
78 static int ldapdb_connect(ldapctx *ctx, sasl_server_params_t *sparams,
79 const char *user, unsigned ulen, connparm *cp)
84 if((i=ldap_initialize(&cp->ld, ctx->uri))) {
88 authzid = sparams->utils->malloc(ulen + sizeof("u:"));
90 return LDAP_NO_MEMORY;
92 strcpy(authzid, "u:");
93 strcpy(authzid+2, user);
94 cp->c.ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
95 cp->c.ldctl_value.bv_val = authzid;
96 cp->c.ldctl_value.bv_len = ulen + 2;
97 cp->c.ldctl_iscritical = 1;
100 ldap_set_option(cp->ld, LDAP_OPT_PROTOCOL_VERSION, &i);
102 /* If TLS is set and it fails, continue or bail out as requested */
103 if (ctx->use_tls && (i=ldap_start_tls_s(cp->ld, NULL, NULL)) != LDAP_SUCCESS
104 && ctx->use_tls > 1) {
105 sparams->utils->free(authzid);
109 i = ldap_sasl_interactive_bind_s(cp->ld, NULL, ctx->mech.bv_val, NULL,
110 NULL, LDAP_SASL_QUIET, ldapdb_interact, ctx);
111 if (i != LDAP_SUCCESS) {
112 sparams->utils->free(authzid);
116 cp->ctrl[0] = &cp->c;
118 i = ldap_whoami_s(cp->ld, &cp->dn, cp->ctrl, NULL);
119 if (i == LDAP_SUCCESS && cp->dn) {
120 if (!cp->dn->bv_val || strncmp(cp->dn->bv_val, "dn:", 3)) {
123 i = LDAP_INVALID_SYNTAX;
125 cp->c.ldctl_value = *(cp->dn);
128 sparams->utils->free(authzid);
132 static void ldapdb_auxprop_lookup(void *glob_context,
133 sasl_server_params_t *sparams,
138 ldapctx *ctx = glob_context;
140 int ret, i, n, *aindx;
141 const struct propval *pr;
142 struct berval **bvals;
143 LDAPMessage *msg, *res;
146 if(!ctx || !sparams || !user) return;
148 pr = sparams->utils->prop_get(sparams->propctx);
151 /* count how many attrs to fetch */
152 for(i = 0, n = 0; pr[i].name; i++) {
153 if(pr[i].name[0] == '*' && (flags & SASL_AUXPROP_AUTHZID))
155 if(pr[i].values && !(flags & SASL_AUXPROP_OVERRIDE))
159 /* nothing to do, bail out */
162 /* alloc an array of attr names for search, and index to the props */
163 attrs = sparams->utils->malloc((n+1)*sizeof(char *)*2);
166 aindx = (int *)(attrs + n + 1);
169 for (i=0, n=0; pr[i].name; i++) {
170 if(pr[i].name[0] == '*' && (flags & SASL_AUXPROP_AUTHZID))
172 if(pr[i].values && !(flags & SASL_AUXPROP_OVERRIDE))
174 attrs[n] = (char *)pr[i].name;
175 if (pr[i].name[0] == '*') attrs[n]++;
181 if(ldapdb_connect(ctx, sparams, user, ulen, &cp)) {
185 ret = ldap_search_ext_s(cp.ld, cp.dn->bv_val+3, LDAP_SCOPE_BASE,
186 "(objectclass=*)", attrs, 0, cp.ctrl, NULL, NULL, 1, &res);
189 if (ret != LDAP_SUCCESS) goto done;
191 for(msg=ldap_first_message(cp.ld, res); msg; msg=ldap_next_message(cp.ld, msg))
193 if (ldap_msgtype(msg) != LDAP_RES_SEARCH_ENTRY) continue;
196 bvals = ldap_get_values_len(cp.ld, msg, attrs[i]);
197 if (!bvals) continue;
198 if (pr[aindx[i]].values)
199 sparams->utils->prop_erase(sparams->propctx, pr[aindx[i]].name);
200 sparams->utils->prop_set(sparams->propctx, pr[aindx[i]].name,
201 bvals[0]->bv_val, bvals[0]->bv_len);
208 if(attrs) sparams->utils->free(attrs);
209 if(cp.ld) ldap_unbind(cp.ld);
212 #if SASL_VERSION_FULL >= 0x020110
213 static int ldapdb_auxprop_store(void *glob_context,
214 sasl_server_params_t *sparams,
215 struct propctx *prctx,
219 ldapctx *ctx = glob_context;
221 const struct propval *pr;
225 /* just checking if we are enabled */
226 if (!prctx) return SASL_OK;
228 if (!sparams || !user) return SASL_BADPARAM;
230 pr = sparams->utils->prop_get(prctx);
231 if (!pr) return SASL_BADPARAM;
233 for (n=0; pr[n].name; n++);
234 if (!n) return SASL_BADPARAM;
236 mods = sparams->utils->malloc((n+1) * sizeof(LDAPMod*) + n * sizeof(LDAPMod));
237 if (!mods) return SASL_NOMEM;
239 if((i=ldapdb_connect(ctx, sparams, user, ulen, &cp)) == 0) {
241 for (i=0; i<n; i++) {
242 mods[i] = (LDAPMod *)((char *)(mods+n+1) + i * sizeof(LDAPMod));
243 mods[i]->mod_op = LDAP_MOD_REPLACE;
244 mods[i]->mod_type = (char *)pr[i].name;
245 mods[i]->mod_values = (char **)pr[i].values;
249 i = ldap_modify_ext_s(cp.ld, cp.dn->bv_val+3, mods, cp.ctrl, NULL);
253 sparams->utils->free(mods);
256 sparams->utils->seterror(sparams->utils->conn, 0,
258 if (i == LDAP_NO_MEMORY) i = SASL_NOMEM;
261 if (cp.ld) ldap_unbind(cp.ld);
264 #endif /* SASL_VERSION_FULL >= 2.1.16 */
266 static void ldapdb_auxprop_free(void *glob_ctx, const sasl_utils_t *utils)
268 utils->free(glob_ctx);
271 static sasl_auxprop_plug_t ldapdb_auxprop_plugin = {
274 NULL, /* glob_context */
275 ldapdb_auxprop_free, /* auxprop_free */
276 ldapdb_auxprop_lookup, /* auxprop_lookup */
278 #if SASL_VERSION_FULL >=0x020110
279 ldapdb_auxprop_store /* spare if <2.1.16*/
285 static int ldapdb_auxprop_plug_init(const sasl_utils_t *utils,
288 sasl_auxprop_plug_t **plug,
289 const char *plugname __attribute__((unused)))
295 if(!out_version || !plug) return SASL_BADPARAM;
297 if(max_version < SASL_AUXPROP_PLUG_VERSION) return SASL_BADVERS;
299 memset(&tmp, 0, sizeof(tmp));
301 utils->getopt(utils->getopt_context, ldapdb, "ldapdb_uri", &tmp.uri, NULL);
302 if(!tmp.uri) return SASL_BADPARAM;
304 utils->getopt(utils->getopt_context, ldapdb, "ldapdb_id",
305 (const char **)&tmp.id.bv_val, &len);
307 utils->getopt(utils->getopt_context, ldapdb, "ldapdb_pw",
308 (const char **)&tmp.pw.bv_val, &len);
310 utils->getopt(utils->getopt_context, ldapdb, "ldapdb_mech",
311 (const char **)&tmp.mech.bv_val, &len);
312 tmp.mech.bv_len = len;
313 utils->getopt(utils->getopt_context, ldapdb, "ldapdb_starttls", &s, NULL);
316 if (!strcasecmp(s, "demand")) tmp.use_tls = 2;
317 else if (!strcasecmp(s, "try")) tmp.use_tls = 1;
319 utils->getopt(utils->getopt_context, ldapdb, "ldapdb_rc", &s, &len);
322 char *str = utils->malloc(sizeof("LDAPRC=")+len);
323 if (!str) return SASL_NOMEM;
324 strcpy( str, "LDAPRC=" );
325 strcpy( str + sizeof("LDAPRC=")-1, s );
333 p = utils->malloc(sizeof(ldapctx));
334 if (!p) return SASL_NOMEM;
336 ldapdb_auxprop_plugin.glob_context = p;
338 *out_version = SASL_AUXPROP_PLUG_VERSION;
340 *plug = &ldapdb_auxprop_plugin;
345 SASL_AUXPROP_PLUG_INIT( ldapdb )