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 #include "plugin_common.h"
26 static char ldapdb[] = "ldapdb";
28 typedef struct ldapctx {
29 const char *uri; /* URI of LDAP server */
30 struct berval id; /* SASL authcid to bind as */
31 struct berval pw; /* password for bind */
32 struct berval mech; /* SASL mech */
33 int use_tls; /* Issue StartTLS request? */
36 static int ldapdb_interact(LDAP *ld, unsigned flags __attribute__((unused)),
37 void *def, void *inter)
39 sasl_interact_t *in = inter;
43 for (;in->id != SASL_CB_LIST_END;in++)
48 case SASL_CB_GETREALM:
49 ldap_get_option(ld, LDAP_OPT_X_SASL_REALM, &p.bv_val);
50 if (p.bv_val) p.bv_len = strlen(p.bv_val);
52 case SASL_CB_AUTHNAME:
61 in->result = p.bv_val;
68 typedef struct connparm {
75 static int ldapdb_connect(ldapctx *ctx, sasl_server_params_t *sparams,
76 const char *user, unsigned ulen, connparm *cp)
81 if((i=ldap_initialize(&cp->ld, ctx->uri))) {
85 authzid = sparams->utils->malloc(ulen + sizeof("u:"));
87 return LDAP_NO_MEMORY;
89 strcpy(authzid, "u:");
90 strcpy(authzid+2, user);
91 cp->c.ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
92 cp->c.ldctl_value.bv_val = authzid;
93 cp->c.ldctl_value.bv_len = ulen + 2;
94 cp->c.ldctl_iscritical = 1;
97 ldap_set_option(cp->ld, LDAP_OPT_PROTOCOL_VERSION, &i);
99 /* If TLS is set and it fails, continue or bail out as requested */
100 if (ctx->use_tls && (i=ldap_start_tls_s(cp->ld, NULL, NULL)) != LDAP_SUCCESS
101 && ctx->use_tls > 1) {
102 sparams->utils->free(authzid);
106 i = ldap_sasl_interactive_bind_s(cp->ld, NULL, ctx->mech.bv_val, NULL,
107 NULL, LDAP_SASL_QUIET, ldapdb_interact, ctx);
108 if (i != LDAP_SUCCESS) {
109 sparams->utils->free(authzid);
113 cp->ctrl[0] = &cp->c;
115 i = ldap_whoami_s(cp->ld, &cp->dn, cp->ctrl, NULL);
116 if (i == LDAP_SUCCESS && cp->dn) {
117 if (!cp->dn->bv_val || strncmp(cp->dn->bv_val, "dn:", 3)) {
120 i = LDAP_INVALID_SYNTAX;
122 cp->c.ldctl_value = *(cp->dn);
125 sparams->utils->free(authzid);
129 static void ldapdb_auxprop_lookup(void *glob_context,
130 sasl_server_params_t *sparams,
135 ldapctx *ctx = glob_context;
137 int ret, i, n, *aindx;
138 const struct propval *pr;
139 struct berval **bvals;
140 LDAPMessage *msg, *res;
143 if(!ctx || !sparams || !user) return;
145 pr = sparams->utils->prop_get(sparams->propctx);
148 /* count how many attrs to fetch */
149 for(i = 0, n = 0; pr[i].name; i++) {
150 if(pr[i].name[0] == '*' && (flags & SASL_AUXPROP_AUTHZID))
152 if(pr[i].values && !(flags & SASL_AUXPROP_OVERRIDE))
156 /* nothing to do, bail out */
159 /* alloc an array of attr names for search, and index to the props */
160 attrs = sparams->utils->malloc((n+1)*sizeof(char *)*2);
163 aindx = (int *)(attrs + n + 1);
166 for (i=0, n=0; pr[i].name; i++) {
167 if(pr[i].name[0] == '*' && (flags & SASL_AUXPROP_AUTHZID))
169 if(pr[i].values && !(flags & SASL_AUXPROP_OVERRIDE))
171 attrs[n] = (char *)pr[i].name;
172 if (pr[i].name[0] == '*') attrs[n]++;
178 if(ldapdb_connect(ctx, sparams, user, ulen, &cp)) {
182 ret = ldap_search_ext_s(cp.ld, cp.dn->bv_val+3, LDAP_SCOPE_BASE,
183 "(objectclass=*)", attrs, 0, cp.ctrl, NULL, NULL, 1, &res);
186 if (ret != LDAP_SUCCESS) goto done;
188 for(msg=ldap_first_message(cp.ld, res); msg; msg=ldap_next_message(cp.ld, msg))
190 if (ldap_msgtype(msg) != LDAP_RES_SEARCH_ENTRY) continue;
193 bvals = ldap_get_values_len(cp.ld, msg, attrs[i]);
194 if (!bvals) continue;
195 if (pr[aindx[i]].values)
196 sparams->utils->prop_erase(sparams->propctx, pr[aindx[i]].name);
197 sparams->utils->prop_set(sparams->propctx, pr[aindx[i]].name,
198 bvals[0]->bv_val, bvals[0]->bv_len);
205 if(attrs) sparams->utils->free(attrs);
206 if(cp.ld) ldap_unbind(cp.ld);
209 static int ldapdb_auxprop_store(void *glob_context,
210 sasl_server_params_t *sparams,
211 struct propctx *prctx,
215 ldapctx *ctx = glob_context;
217 const struct propval *pr;
221 /* just checking if we are enabled */
222 if (!prctx) return SASL_OK;
224 if (!sparams || !user) return SASL_BADPARAM;
226 pr = sparams->utils->prop_get(prctx);
227 if (!pr) return SASL_BADPARAM;
229 for (n=0; pr[n].name; n++);
230 if (!n) return SASL_BADPARAM;
232 mods = sparams->utils->malloc((n+1) * sizeof(LDAPMod*) + n * sizeof(LDAPMod));
233 if (!mods) return SASL_NOMEM;
235 if((i=ldapdb_connect(ctx, sparams, user, ulen, &cp)) == 0) {
237 for (i=0; i<n; i++) {
238 mods[i] = (LDAPMod *)((char *)(mods+n+1) + i * sizeof(LDAPMod));
239 mods[i]->mod_op = LDAP_MOD_REPLACE;
240 mods[i]->mod_type = (char *)pr[i].name;
241 mods[i]->mod_values = (char **)pr[i].values;
245 i = ldap_modify_ext_s(cp.ld, cp.dn->bv_val+3, mods, cp.ctrl, NULL);
249 sparams->utils->free(mods);
252 sparams->utils->seterror(sparams->utils->conn, 0,
254 if (i == LDAP_NO_MEMORY) i = SASL_NOMEM;
257 if (cp.ld) ldap_unbind(cp.ld);
261 static void ldapdb_auxprop_free(void *glob_ctx, const sasl_utils_t *utils)
263 utils->free(glob_ctx);
266 static sasl_auxprop_plug_t ldapdb_auxprop_plugin = {
269 NULL, /* glob_context */
270 ldapdb_auxprop_free, /* auxprop_free */
271 ldapdb_auxprop_lookup, /* auxprop_lookup */
273 ldapdb_auxprop_store /* spare if <2.1.16*/
276 static int ldapdb_auxprop_plug_init(const sasl_utils_t *utils,
279 sasl_auxprop_plug_t **plug,
280 const char *plugname __attribute__((unused)))
286 if(!out_version || !plug) return SASL_BADPARAM;
288 if(max_version < SASL_AUXPROP_PLUG_VERSION) return SASL_BADVERS;
290 memset(&tmp, 0, sizeof(tmp));
292 utils->getopt(utils->getopt_context, ldapdb, "ldapdb_uri", &tmp.uri, NULL);
293 if(!tmp.uri) return SASL_BADPARAM;
295 utils->getopt(utils->getopt_context, ldapdb, "ldapdb_id",
296 (const char **)&tmp.id.bv_val, &len);
298 utils->getopt(utils->getopt_context, ldapdb, "ldapdb_pw",
299 (const char **)&tmp.pw.bv_val, &len);
301 utils->getopt(utils->getopt_context, ldapdb, "ldapdb_mech",
302 (const char **)&tmp.mech.bv_val, &len);
303 tmp.mech.bv_len = len;
304 utils->getopt(utils->getopt_context, ldapdb, "ldapdb_starttls", &s, NULL);
307 if (!strcasecmp(s, "demand")) tmp.use_tls = 2;
308 else if (!strcasecmp(s, "try")) tmp.use_tls = 1;
310 utils->getopt(utils->getopt_context, ldapdb, "ldapdb_rc", &s, &len);
313 char *str = utils->malloc(sizeof("LDAPRC=")+len);
314 if (!str) return SASL_NOMEM;
315 strcpy( str, "LDAPRC=" );
316 strcpy( str + sizeof("LDAPRC=")-1, s );
324 p = utils->malloc(sizeof(ldapctx));
325 if (!p) return SASL_NOMEM;
327 ldapdb_auxprop_plugin.glob_context = p;
329 *out_version = SASL_AUXPROP_PLUG_VERSION;
331 *plug = &ldapdb_auxprop_plugin;
336 SASL_AUXPROP_PLUG_INIT( ldapdb )