]> git.sur5r.net Git - openldap/blob - contrib/ldapsasl/ldapdb.c
b2c408e3bc8bf6fd2bb1adcef17cb7baa4942a76
[openldap] / contrib / ldapsasl / ldapdb.c
1 /* SASL LDAP auxprop implementation
2  * Copyright (C) 2002,2003 Howard Chu, All rights reserved. <hyc@symas.com>
3  *
4  * Permission is granted to anyone to use this software for any purpose
5  * on any computer system, and to alter it and redistribute it, subject
6  * to the following restrictions:
7  *
8  * 1. The author is not responsible for the consequences of use of this
9  *    software, no matter how awful, even if they arise from flaws in it.
10  *
11  * 2. The origin of this software must not be misrepresented, either by
12  *    explicit claim or by omission.  Since few users ever read sources,
13  *    credits should appear in the documentation.
14  *
15  * 3. Altered versions must be plainly marked as such, and must not be
16  *    misrepresented as being the original software.  Since few users
17  *    ever read sources, credits should appear in the documentation.
18  *
19  * 4. This notice may not be removed or altered.
20  */
21
22 #include <config.h>
23
24 #include <stdio.h>
25
26 #include "sasl.h"
27 #include "saslutil.h"
28 #include "saslplug.h"
29
30 #include "plugin_common.h"
31
32 #include <ldap.h>
33
34 static char ldapdb[] = "ldapdb";
35
36 typedef struct ldapctx {
37         const char *uri;        /* URI of LDAP server */
38         struct berval id;       /* SASL authcid to bind as */
39         struct berval pw;       /* password for bind */
40         struct berval mech;     /* SASL mech */
41 } ldapctx;
42
43 typedef struct gluectx {
44         ldapctx *lc;
45         sasl_server_params_t *lp;
46 } gluectx;
47
48 static int ldapdb_interact(LDAP *ld, unsigned flags __attribute__((unused)),
49         void *def, void *inter)
50 {
51         sasl_interact_t *in = inter;
52         gluectx *gc = def;
53         struct berval p;
54
55         for (;in->id != SASL_CB_LIST_END;in++)
56         {
57                 p.bv_val = NULL;
58                 switch(in->id)
59                 {
60                         case SASL_CB_GETREALM:
61                                 ldap_get_option(ld, LDAP_OPT_X_SASL_REALM, &p.bv_val);
62                                 if (p.bv_val) p.bv_len = strlen(p.bv_val);
63                                 break;          
64                         case SASL_CB_AUTHNAME:
65                                 p = gc->lc->id;
66                                 break;
67                         case SASL_CB_PASS:
68                                 p = gc->lc->pw;
69                                 break;
70                 }
71                 if (p.bv_val)
72                 {
73                         in->result = p.bv_val;
74                         in->len = p.bv_len;
75                 }
76         }
77         return LDAP_SUCCESS;
78 }
79
80 static void ldapdb_auxprop_lookup(void *glob_context,
81                                   sasl_server_params_t *sparams,
82                                   unsigned flags,
83                                   const char *user,
84                                   unsigned ulen)
85 {
86     ldapctx *ctx = glob_context;
87     int ret, i, n, *aindx;
88     const struct propval *pr;
89     LDAP *ld = NULL;
90     gluectx gc = { ctx, sparams };
91     struct berval *dn = NULL, **bvals;
92     LDAPMessage *msg, *res;
93     char **attrs = NULL, *authzid = NULL;
94     LDAPControl c, *ctrl[2] = {&c, NULL};
95     
96     if(!ctx || !sparams || !user) return;
97
98     pr = sparams->utils->prop_get(sparams->propctx);
99     if(!pr) return;
100
101     /* count how many attrs to fetch */
102     for(i = 0, n = 0; pr[i].name; i++) {
103         if(pr[i].name[0] == '*' && (flags & SASL_AUXPROP_AUTHZID))
104             continue;
105         if(pr[i].values && !(flags & SASL_AUXPROP_OVERRIDE))
106             continue;
107         n++;
108     }
109     /* nothing to do, bail out */
110     if (!n) return;
111
112     /* alloc an array of attr names for search, and index to the props */
113     attrs = sparams->utils->malloc((n+1)*sizeof(char *)*2);
114     if (!attrs) return;
115
116     aindx = (int *)(attrs + n + 1);
117
118     /* copy attr list */
119     for (i=0, n=0; pr[i].name; i++) {
120         if(pr[i].name[0] == '*' && (flags & SASL_AUXPROP_AUTHZID))
121             continue;
122         if(pr[i].values && !(flags & SASL_AUXPROP_OVERRIDE))
123             continue;
124         attrs[n] = (char *)pr[i].name;
125         if (pr[i].name[0] == '*') attrs[n]++;
126         aindx[n] = i;
127         n++;
128     }
129     attrs[n] = NULL;
130         
131     if(ldap_initialize(&ld, ctx->uri)) {
132         sparams->utils->free(attrs);
133         return;
134     }
135
136     authzid = sparams->utils->malloc(ulen + sizeof("u:"));
137     if (!authzid) goto done;
138     strcpy(authzid, "u:");
139     strcpy(authzid+2, user);
140     c.ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
141     c.ldctl_value.bv_val = authzid;
142     c.ldctl_value.bv_len = ulen + 2;
143     c.ldctl_iscritical = 1;
144
145     i = LDAP_VERSION3;
146     ret = ldap_set_option(ld, LDAP_OPT_PROTOCOL_VERSION, &i);
147
148     ret = ldap_sasl_interactive_bind_s(ld, NULL, ctx->mech.bv_val, NULL, NULL,
149         LDAP_SASL_QUIET, ldapdb_interact, &gc);
150     if (ret != LDAP_SUCCESS) goto done;
151     
152     ret = ldap_whoami_s(ld, &dn, ctrl, NULL);
153     if (ret != LDAP_SUCCESS || !dn) goto done;
154     
155     if (dn->bv_val && !strncmp(dn->bv_val, "dn:", 3))
156     ret = ldap_search_s(ld, dn->bv_val+3, LDAP_SCOPE_BASE, "(objectclass=*)",
157         attrs, 0, &res);
158     ber_bvfree(dn);
159
160     if (ret != LDAP_SUCCESS) goto done;
161
162     for(msg=ldap_first_message(ld, res); msg; msg=ldap_next_message(ld, msg))
163     {
164         if (ldap_msgtype(msg) != LDAP_RES_SEARCH_ENTRY) continue;
165         for (i=0; i<n; i++)
166         {
167             bvals = ldap_get_values_len(ld, msg, attrs[i]);
168             if (!bvals) continue;
169             if (pr[aindx[i]].values)
170                 sparams->utils->prop_erase(sparams->propctx, pr[aindx[i]].name);
171             sparams->utils->prop_set(sparams->propctx, pr[aindx[i]].name,
172                                  bvals[0]->bv_val, bvals[0]->bv_len);
173             ber_bvecfree(bvals);
174         }
175     }
176     ldap_msgfree(res);
177
178  done:
179     if(authzid) sparams->utils->free(authzid);
180     if(attrs) sparams->utils->free(attrs);
181     if(ld) ldap_unbind(ld);
182 }
183
184 static void ldapdb_auxprop_free(void *glob_ctx, const sasl_utils_t *utils)
185 {
186         utils->free(glob_ctx);
187 }
188
189 static sasl_auxprop_plug_t ldapdb_auxprop_plugin = {
190     0,           /* Features */
191     0,           /* spare */
192     NULL,        /* glob_context */
193     ldapdb_auxprop_free,        /* auxprop_free */
194     ldapdb_auxprop_lookup, /* auxprop_lookup */
195     ldapdb,    /* name */
196     NULL         /* spare */
197 };
198
199 static int ldapdb_auxprop_plug_init(const sasl_utils_t *utils,
200                              int max_version,
201                              int *out_version,
202                              sasl_auxprop_plug_t **plug,
203                              const char *plugname __attribute__((unused))) 
204 {
205     ldapctx tmp, *p;
206     const char *s;
207     unsigned len;
208
209     if(!out_version || !plug) return SASL_BADPARAM;
210
211     if(max_version < SASL_AUXPROP_PLUG_VERSION) return SASL_BADVERS;
212     
213     utils->getopt(utils->getopt_context, ldapdb, "ldapdb_uri", &tmp.uri, NULL);
214     if(!tmp.uri) return SASL_BADPARAM;
215
216     utils->getopt(utils->getopt_context, ldapdb, "ldapdb_id",
217         (const char **)&tmp.id.bv_val, &len);
218     tmp.id.bv_len = len;
219     utils->getopt(utils->getopt_context, ldapdb, "ldapdb_pw",
220         (const char **)&tmp.pw.bv_val, &len);
221     tmp.pw.bv_len = len;
222     utils->getopt(utils->getopt_context, ldapdb, "ldapdb_mech",
223         (const char **)&tmp.mech.bv_val, &len);
224     tmp.mech.bv_len = len;
225     utils->getopt(utils->getopt_context, ldapdb, "ldapdb_rc", &s, &len);
226     if (s)
227     {
228         char *str = utils->malloc(sizeof("LDAPRC=")+len);
229         if (!str) return SASL_NOMEM;
230         strcpy( str, "LDAPRC=" );
231         strcpy( str + sizeof("LDAPRC=")-1, s );
232         if (putenv(str))
233         {
234             utils->free(str);
235             return SASL_NOMEM;
236         }
237     }
238
239     p = utils->malloc(sizeof(ldapctx));
240     if (!p) return SASL_NOMEM;
241     *p = tmp;
242     ldapdb_auxprop_plugin.glob_context = p;
243
244     *out_version = SASL_AUXPROP_PLUG_VERSION;
245
246     *plug = &ldapdb_auxprop_plugin;
247
248     return SASL_OK;
249 }
250
251 SASL_AUXPROP_PLUG_INIT( ldapdb )
252