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