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