GSS_S_PROMPTING_NEEDED is a bit
[cyrus-sasl.git] / plugins / ldapdb.c
1 /* $OpenLDAP: pkg/ldap/contrib/ldapsasl/ldapdb.c,v 1.1.2.7 2003/11/29 22:10:03 hyc Exp $ */
2 /* SASL LDAP auxprop implementation
3  * Copyright (C) 2002,2003 Howard Chu, All rights reserved. <hyc@symas.com>
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted only as authorized by the OpenLDAP
7  * Public License.
8  *
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>.
12  */
13
14 #include <config.h>
15
16 #include <stdio.h>
17
18 #include "sasl.h"
19 #include "saslutil.h"
20 #include "saslplug.h"
21
22 #include "plugin_common.h"
23
24 #include <ldap.h>
25
26 static char ldapdb[] = "ldapdb";
27
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? */
34 } ldapctx;
35
36 static int ldapdb_interact(LDAP *ld, unsigned flags __attribute__((unused)),
37         void *def, void *inter)
38 {
39         sasl_interact_t *in = inter;
40         ldapctx *ctx = def;
41         struct berval p;
42
43         for (;in->id != SASL_CB_LIST_END;in++)
44         {
45                 p.bv_val = NULL;
46                 switch(in->id)
47                 {
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);
51                                 break;          
52                         case SASL_CB_AUTHNAME:
53                                 p = ctx->id;
54                                 break;
55                         case SASL_CB_PASS:
56                                 p = ctx->pw;
57                                 break;
58                 }
59                 if (p.bv_val)
60                 {
61                         in->result = p.bv_val;
62                         in->len = p.bv_len;
63                 }
64         }
65         return LDAP_SUCCESS;
66 }
67
68 typedef struct connparm {
69         LDAP *ld;
70         LDAPControl c;
71         LDAPControl *ctrl[2];
72         struct berval *dn;
73 } connparm;
74
75 static int ldapdb_connect(ldapctx *ctx, sasl_server_params_t *sparams,
76         const char *user, unsigned ulen, connparm *cp)
77 {
78     int i;
79     char *authzid;
80
81     if((i=ldap_initialize(&cp->ld, ctx->uri))) {
82         return i;
83     }
84
85     authzid = sparams->utils->malloc(ulen + sizeof("u:"));
86     if (!authzid) {
87         return LDAP_NO_MEMORY;
88     } 
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;
95
96     i = LDAP_VERSION3;
97     ldap_set_option(cp->ld, LDAP_OPT_PROTOCOL_VERSION, &i);
98
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);
103         return i;
104     }
105
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);
110         return i;
111     }
112     
113     cp->ctrl[0] = &cp->c;
114     cp->ctrl[1] = NULL;
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)) {
118             ber_bvfree(cp->dn);
119             cp->dn = NULL;
120             i = LDAP_INVALID_SYNTAX;
121         } else {
122             cp->c.ldctl_value = *(cp->dn);
123         }
124     }
125     sparams->utils->free(authzid);
126     return i;
127 }
128
129 static void ldapdb_auxprop_lookup(void *glob_context,
130                                   sasl_server_params_t *sparams,
131                                   unsigned flags,
132                                   const char *user,
133                                   unsigned ulen)
134 {
135     ldapctx *ctx = glob_context;
136     connparm cp;
137     int ret, i, n, *aindx;
138     const struct propval *pr;
139     struct berval **bvals;
140     LDAPMessage *msg, *res;
141     char **attrs = NULL;
142     
143     if(!ctx || !sparams || !user) return;
144
145     pr = sparams->utils->prop_get(sparams->propctx);
146     if(!pr) return;
147
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))
151             continue;
152         if(pr[i].values && !(flags & SASL_AUXPROP_OVERRIDE))
153             continue;
154         n++;
155     }
156     /* nothing to do, bail out */
157     if (!n) return;
158
159     /* alloc an array of attr names for search, and index to the props */
160     attrs = sparams->utils->malloc((n+1)*sizeof(char *)*2);
161     if (!attrs) return;
162
163     aindx = (int *)(attrs + n + 1);
164
165     /* copy attr list */
166     for (i=0, n=0; pr[i].name; i++) {
167         if(pr[i].name[0] == '*' && (flags & SASL_AUXPROP_AUTHZID))
168             continue;
169         if(pr[i].values && !(flags & SASL_AUXPROP_OVERRIDE))
170             continue;
171         attrs[n] = (char *)pr[i].name;
172         if (pr[i].name[0] == '*') attrs[n]++;
173         aindx[n] = i;
174         n++;
175     }
176     attrs[n] = NULL;
177
178     if(ldapdb_connect(ctx, sparams, user, ulen, &cp)) {
179         goto done;
180     }
181
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);
184     ber_bvfree(cp.dn);
185
186     if (ret != LDAP_SUCCESS) goto done;
187
188     for(msg=ldap_first_message(cp.ld, res); msg; msg=ldap_next_message(cp.ld, msg))
189     {
190         if (ldap_msgtype(msg) != LDAP_RES_SEARCH_ENTRY) continue;
191         for (i=0; i<n; i++)
192         {
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);
199             ber_bvecfree(bvals);
200         }
201     }
202     ldap_msgfree(res);
203
204  done:
205     if(attrs) sparams->utils->free(attrs);
206     if(cp.ld) ldap_unbind(cp.ld);
207 }
208
209 static int ldapdb_auxprop_store(void *glob_context,
210                                   sasl_server_params_t *sparams,
211                                   struct propctx *prctx,
212                                   const char *user,
213                                   unsigned ulen)
214 {
215     ldapctx *ctx = glob_context;
216     connparm cp;
217     const struct propval *pr;
218     int i, n;
219     LDAPMod **mods;
220
221     /* just checking if we are enabled */
222     if (!prctx) return SASL_OK;
223
224     if (!sparams || !user) return SASL_BADPARAM;
225
226     pr = sparams->utils->prop_get(prctx);
227     if (!pr) return SASL_BADPARAM;
228
229     for (n=0; pr[n].name; n++);
230     if (!n) return SASL_BADPARAM;
231
232     mods = sparams->utils->malloc((n+1) * sizeof(LDAPMod*) + n * sizeof(LDAPMod));
233     if (!mods) return SASL_NOMEM;
234
235     if((i=ldapdb_connect(ctx, sparams, user, ulen, &cp)) == 0) {
236
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;
242         }
243         mods[i] = NULL;
244
245         i = ldap_modify_ext_s(cp.ld, cp.dn->bv_val+3, mods, cp.ctrl, NULL);
246         ber_bvfree(cp.dn);
247     }
248
249     sparams->utils->free(mods);
250
251     if (i) {
252         sparams->utils->seterror(sparams->utils->conn, 0,
253             ldap_err2string(i));
254         if (i == LDAP_NO_MEMORY) i = SASL_NOMEM;
255         else i = SASL_FAIL;
256     }
257     if (cp.ld) ldap_unbind(cp.ld);
258     return i;
259 }
260
261 static void ldapdb_auxprop_free(void *glob_ctx, const sasl_utils_t *utils)
262 {
263         utils->free(glob_ctx);
264 }
265
266 static sasl_auxprop_plug_t ldapdb_auxprop_plugin = {
267     0,                          /* Features */
268     0,                          /* spare */
269     NULL,                       /* glob_context */
270     ldapdb_auxprop_free,        /* auxprop_free */
271     ldapdb_auxprop_lookup,      /* auxprop_lookup */
272     ldapdb,                     /* name */
273     ldapdb_auxprop_store        /* auxprop store */
274 };
275
276 int ldapdb_auxprop_plug_init(const sasl_utils_t *utils,
277                              int max_version,
278                              int *out_version,
279                              sasl_auxprop_plug_t **plug,
280                              const char *plugname __attribute__((unused))) 
281 {
282     ldapctx tmp, *p;
283     const char *s;
284     unsigned len;
285
286     if(!out_version || !plug) return SASL_BADPARAM;
287
288     if(max_version < SASL_AUXPROP_PLUG_VERSION) return SASL_BADVERS;
289     
290     memset(&tmp, 0, sizeof(tmp));
291
292     utils->getopt(utils->getopt_context, ldapdb, "ldapdb_uri", &tmp.uri, NULL);
293     if(!tmp.uri) return SASL_BADPARAM;
294
295     utils->getopt(utils->getopt_context, ldapdb, "ldapdb_id",
296         (const char **)&tmp.id.bv_val, &len);
297     tmp.id.bv_len = len;
298     utils->getopt(utils->getopt_context, ldapdb, "ldapdb_pw",
299         (const char **)&tmp.pw.bv_val, &len);
300     tmp.pw.bv_len = 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);
305     if (s)
306     {
307         if (!strcasecmp(s, "demand")) tmp.use_tls = 2;
308         else if (!strcasecmp(s, "try")) tmp.use_tls = 1;
309     }
310     utils->getopt(utils->getopt_context, ldapdb, "ldapdb_rc", &s, &len);
311     if (s)
312     {
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 );
317         if (putenv(str))
318         {
319             utils->free(str);
320             return SASL_NOMEM;
321         }
322     }
323
324     p = utils->malloc(sizeof(ldapctx));
325     if (!p) return SASL_NOMEM;
326     *p = tmp;
327     ldapdb_auxprop_plugin.glob_context = p;
328
329     *out_version = SASL_AUXPROP_PLUG_VERSION;
330
331     *plug = &ldapdb_auxprop_plugin;
332
333     return SASL_OK;
334 }