]> git.sur5r.net Git - openldap/blob - libraries/libldap/sasl.c
new rebind proc
[openldap] / libraries / libldap / sasl.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6
7 /*
8  *      BindRequest ::= SEQUENCE {
9  *              version         INTEGER,
10  *              name            DistinguishedName,       -- who
11  *              authentication  CHOICE {
12  *                      simple          [0] OCTET STRING -- passwd
13 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
14  *                      krbv42ldap      [1] OCTET STRING
15  *                      krbv42dsa       [2] OCTET STRING
16 #endif
17  *                      sasl            [3] SaslCredentials     -- LDAPv3
18  *              }
19  *      }
20  *
21  *      BindResponse ::= SEQUENCE {
22  *              COMPONENTS OF LDAPResult,
23  *              serverSaslCreds         OCTET STRING OPTIONAL -- LDAPv3
24  *      }
25  *
26  */
27
28 #include "portable.h"
29
30 #include <stdlib.h>
31 #include <stdio.h>
32
33 #include <ac/socket.h>
34 #include <ac/string.h>
35 #include <ac/time.h>
36 #include <ac/errno.h>
37
38 #include "ldap-int.h"
39
40 /*
41  * ldap_sasl_bind - bind to the ldap server (and X.500).
42  * The dn (usually NULL), mechanism, and credentials are provided.
43  * The message id of the request initiated is provided upon successful
44  * (LDAP_SUCCESS) return.
45  *
46  * Example:
47  *      ldap_sasl_bind( ld, NULL, "mechanism",
48  *              cred, NULL, NULL, &msgid )
49  */
50
51 int
52 ldap_sasl_bind(
53         LDAP                    *ld,
54         LDAP_CONST char *dn,
55         LDAP_CONST char *mechanism,
56         struct berval   *cred,
57         LDAPControl             **sctrls,
58         LDAPControl             **cctrls,
59         int                             *msgidp )
60 {
61         BerElement      *ber;
62         int rc;
63
64         Debug( LDAP_DEBUG_TRACE, "ldap_sasl_bind\n", 0, 0, 0 );
65
66         assert( ld != NULL );
67         assert( LDAP_VALID( ld ) );
68         assert( msgidp != NULL );
69
70         if( msgidp == NULL ) {
71                 ld->ld_errno = LDAP_PARAM_ERROR;
72                 return ld->ld_errno;
73         }
74
75         if( mechanism == LDAP_SASL_SIMPLE ) {
76                 if( dn == NULL && cred != NULL ) {
77                         /* use default binddn */
78                         dn = ld->ld_defbinddn;
79                 }
80
81         } else if( ld->ld_version < LDAP_VERSION3 ) {
82                 ld->ld_errno = LDAP_NOT_SUPPORTED;
83                 return ld->ld_errno;
84         }
85
86         if ( dn == NULL ) {
87                 dn = "";
88         }
89
90         /* create a message to send */
91         if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
92                 ld->ld_errno = LDAP_NO_MEMORY;
93                 return ld->ld_errno;
94         }
95
96         assert( BER_VALID( ber ) );
97
98         if( mechanism == LDAP_SASL_SIMPLE ) {
99                 /* simple bind */
100                 rc = ber_printf( ber, "{it{istON}" /*}*/,
101                         ++ld->ld_msgid, LDAP_REQ_BIND,
102                         ld->ld_version, dn, LDAP_AUTH_SIMPLE,
103                         cred );
104                 
105         } else if ( cred == NULL || !cred->bv_len ) {
106                 /* SASL bind w/o creditials */
107                 rc = ber_printf( ber, "{it{ist{sN}N}" /*}*/,
108                         ++ld->ld_msgid, LDAP_REQ_BIND,
109                         ld->ld_version, dn, LDAP_AUTH_SASL,
110                         mechanism );
111
112         } else {
113                 /* SASL bind w/ creditials */
114                 rc = ber_printf( ber, "{it{ist{sON}N}" /*}*/,
115                         ++ld->ld_msgid, LDAP_REQ_BIND,
116                         ld->ld_version, dn, LDAP_AUTH_SASL,
117                         mechanism, cred );
118         }
119
120         if( rc == -1 ) {
121                 ld->ld_errno = LDAP_ENCODING_ERROR;
122                 ber_free( ber, 1 );
123                 return( -1 );
124         }
125
126         /* Put Server Controls */
127         if( ldap_int_put_controls( ld, sctrls, ber ) != LDAP_SUCCESS ) {
128                 ber_free( ber, 1 );
129                 return ld->ld_errno;
130         }
131
132         if ( ber_printf( ber, /*{*/ "N}" ) == -1 ) {
133                 ld->ld_errno = LDAP_ENCODING_ERROR;
134                 ber_free( ber, 1 );
135                 return ld->ld_errno;
136         }
137
138 #ifndef LDAP_NOCACHE
139         if ( ld->ld_cache != NULL ) {
140                 ldap_flush_cache( ld );
141         }
142 #endif /* !LDAP_NOCACHE */
143
144         /* send the message */
145         *msgidp = ldap_send_initial_request( ld, LDAP_REQ_BIND, dn, ber );
146
147         if(*msgidp < 0)
148                 return ld->ld_errno;
149
150         return LDAP_SUCCESS;
151 }
152
153
154 int
155 ldap_sasl_bind_s(
156         LDAP                    *ld,
157         LDAP_CONST char *dn,
158         LDAP_CONST char *mechanism,
159         struct berval   *cred,
160         LDAPControl             **sctrls,
161         LDAPControl             **cctrls,
162         struct berval   **servercredp )
163 {
164         int     rc, msgid;
165         LDAPMessage     *result;
166         struct berval   *scredp = NULL;
167
168         Debug( LDAP_DEBUG_TRACE, "ldap_sasl_bind_s\n", 0, 0, 0 );
169
170         /* do a quick !LDAPv3 check... ldap_sasl_bind will do the rest. */
171         if( servercredp != NULL ) {
172                 if (ld->ld_version < LDAP_VERSION3) {
173                         ld->ld_errno = LDAP_NOT_SUPPORTED;
174                         return ld->ld_errno;
175                 }
176                 *servercredp = NULL;
177         }
178
179         rc = ldap_sasl_bind( ld, dn, mechanism, cred, sctrls, cctrls, &msgid );
180
181         if ( rc != LDAP_SUCCESS ) {
182                 return( rc );
183         }
184
185         if ( ldap_result( ld, msgid, 1, NULL, &result ) == -1 ) {
186                 return( ld->ld_errno ); /* ldap_result sets ld_errno */
187         }
188
189         /* parse the results */
190         scredp = NULL;
191         if( servercredp != NULL ) {
192                 rc = ldap_parse_sasl_bind_result( ld, result, &scredp, 0 );
193         }
194
195         if ( rc != LDAP_SUCCESS && rc != LDAP_SASL_BIND_IN_PROGRESS ) {
196                 ldap_msgfree( result );
197                 return( rc );
198         }
199
200         rc = ldap_result2error( ld, result, 1 );
201
202         if ( rc == LDAP_SUCCESS || rc == LDAP_SASL_BIND_IN_PROGRESS ) {
203                 if( servercredp != NULL ) {
204                         *servercredp = scredp;
205                         scredp = NULL;
206                 }
207         }
208
209         if ( scredp != NULL ) {
210                 ber_bvfree(scredp);
211         }
212
213         return rc;
214 }
215
216
217 /*
218 * Parse BindResponse:
219 *
220 *   BindResponse ::= [APPLICATION 1] SEQUENCE {
221 *     COMPONENTS OF LDAPResult,
222 *     serverSaslCreds  [7] OCTET STRING OPTIONAL }
223 *
224 *   LDAPResult ::= SEQUENCE {
225 *     resultCode      ENUMERATED,
226 *     matchedDN       LDAPDN,
227 *     errorMessage    LDAPString,
228 *     referral        [3] Referral OPTIONAL }
229 */
230
231 int
232 ldap_parse_sasl_bind_result(
233         LDAP                    *ld,
234         LDAPMessage             *res,
235         struct berval   **servercredp,
236         int                             freeit )
237 {
238         ber_int_t errcode;
239         struct berval* scred;
240
241         ber_tag_t tag;
242         BerElement      *ber;
243
244         Debug( LDAP_DEBUG_TRACE, "ldap_parse_sasl_bind_result\n", 0, 0, 0 );
245
246         assert( ld != NULL );
247         assert( LDAP_VALID( ld ) );
248         assert( res != NULL );
249
250         if ( ld == NULL || res == NULL ) {
251                 return LDAP_PARAM_ERROR;
252         }
253
254         if( servercredp != NULL ) {
255                 if( ld->ld_version < LDAP_VERSION2 ) {
256                         return LDAP_NOT_SUPPORTED;
257                 }
258                 *servercredp = NULL;
259         }
260
261         if( res->lm_msgtype != LDAP_RES_BIND ) {
262                 ld->ld_errno = LDAP_PARAM_ERROR;
263                 return ld->ld_errno;
264         }
265
266         scred = NULL;
267
268         if ( ld->ld_error ) {
269                 LDAP_FREE( ld->ld_error );
270                 ld->ld_error = NULL;
271         }
272         if ( ld->ld_matched ) {
273                 LDAP_FREE( ld->ld_matched );
274                 ld->ld_matched = NULL;
275         }
276
277         /* parse results */
278
279         ber = ber_dup( res->lm_ber );
280
281         if( ber == NULL ) {
282                 ld->ld_errno = LDAP_NO_MEMORY;
283                 return ld->ld_errno;
284         }
285
286         if ( ld->ld_version < LDAP_VERSION2 ) {
287                 tag = ber_scanf( ber, "{ia}",
288                         &errcode, &ld->ld_error );
289
290                 if( tag == LBER_ERROR ) {
291                         ber_free( ber, 0 );
292                         ld->ld_errno = LDAP_DECODING_ERROR;
293                         return ld->ld_errno;
294                 }
295
296         } else {
297                 ber_len_t len;
298
299                 tag = ber_scanf( ber, "{iaa" /*}*/,
300                         &errcode, &ld->ld_matched, &ld->ld_error );
301
302                 if( tag == LBER_ERROR ) {
303                         ber_free( ber, 0 );
304                         ld->ld_errno = LDAP_DECODING_ERROR;
305                         return ld->ld_errno;
306                 }
307
308                 tag = ber_peek_tag(ber, &len);
309
310                 if( tag == LDAP_TAG_REFERRAL ) {
311                         /* skip 'em */
312                         if( ber_scanf( ber, "x" ) == LBER_ERROR ) {
313                                 ber_free( ber, 0 );
314                                 ld->ld_errno = LDAP_DECODING_ERROR;
315                                 return ld->ld_errno;
316                         }
317
318                         tag = ber_peek_tag(ber, &len);
319                 }
320
321                 if( tag == LDAP_TAG_SASL_RES_CREDS ) {
322                         if( ber_scanf( ber, "O", &scred ) == LBER_ERROR ) {
323                                 ber_free( ber, 0 );
324                                 ld->ld_errno = LDAP_DECODING_ERROR;
325                                 return ld->ld_errno;
326                         }
327                 }
328         }
329
330         ber_free( ber, 0 );
331
332         if ( servercredp != NULL ) {
333                 *servercredp = scred;
334
335         } else if ( scred != NULL ) {
336                 ber_bvfree( scred );
337         }
338
339         ld->ld_errno = errcode;
340
341         if ( freeit ) {
342                 ldap_msgfree( res );
343         }
344
345         return( ld->ld_errno );
346 }
347
348 int
349 ldap_pvt_sasl_getmechs ( LDAP *ld, char **pmechlist )
350 {
351         /* we need to query the server for supported mechs anyway */
352         LDAPMessage *res, *e;
353         char *attrs[] = { "supportedSASLMechanisms", NULL };
354         char **values, *mechlist;
355         int rc;
356
357         Debug( LDAP_DEBUG_TRACE, "ldap_pvt_sasl_getmech\n", 0, 0, 0 );
358
359         rc = ldap_search_s( ld, "", LDAP_SCOPE_BASE,
360                 NULL, attrs, 0, &res );
361
362         if ( rc != LDAP_SUCCESS ) {
363                 return ld->ld_errno;
364         }
365                 
366         e = ldap_first_entry( ld, res );
367         if ( e == NULL ) {
368                 ldap_msgfree( res );
369                 if ( ld->ld_errno == LDAP_SUCCESS ) {
370                         ld->ld_errno = LDAP_NO_SUCH_OBJECT;
371                 }
372                 return ld->ld_errno;
373         }
374
375         values = ldap_get_values( ld, e, "supportedSASLMechanisms" );
376         if ( values == NULL ) {
377                 ldap_msgfree( res );
378                 ld->ld_errno = LDAP_NO_SUCH_ATTRIBUTE;
379                 return ld->ld_errno;
380         }
381
382         mechlist = ldap_charray2str( values, " " );
383         if ( mechlist == NULL ) {
384                 LDAP_VFREE( values );
385                 ldap_msgfree( res );
386                 ld->ld_errno = LDAP_NO_MEMORY;
387                 return ld->ld_errno;
388         } 
389
390         LDAP_VFREE( values );
391         ldap_msgfree( res );
392
393         *pmechlist = mechlist;
394
395         return LDAP_SUCCESS;
396 }
397
398 /*
399  * ldap_sasl_interactive_bind_s - interactive SASL authentication
400  *
401  * This routine uses interactive callbacks.
402  *
403  * LDAP_SUCCESS is returned upon success, the ldap error code
404  * otherwise.
405  */
406 int
407 ldap_sasl_interactive_bind_s(
408         LDAP *ld,
409         LDAP_CONST char *dn, /* usually NULL */
410         LDAP_CONST char *mechs,
411         LDAPControl **serverControls,
412         LDAPControl **clientControls,
413         unsigned flags,
414         LDAP_SASL_INTERACT_PROC *interact,
415         void *defaults )
416 {
417         int rc;
418
419 #if defined( LDAP_R_COMPILE ) && defined( HAVE_CYRUS_SASL )
420         ldap_pvt_thread_mutex_lock( &ldap_int_sasl_mutex );
421 #endif
422
423         if( mechs == NULL || *mechs == '\0' ) {
424                 char *smechs;
425
426                 rc = ldap_pvt_sasl_getmechs( ld, &smechs );
427
428                 if( rc != LDAP_SUCCESS ) {
429                         goto done;
430                 }
431
432                 Debug( LDAP_DEBUG_TRACE,
433                         "ldap_interactive_sasl_bind_s: server supports: %s\n",
434                         smechs, 0, 0 );
435
436                 mechs = smechs;
437
438         } else {
439                 Debug( LDAP_DEBUG_TRACE,
440                         "ldap_interactive_sasl_bind_s: user selected: %s\n",
441                         mechs, 0, 0 );
442         }
443
444         rc = ldap_int_sasl_bind( ld, dn, mechs,
445                 serverControls, clientControls,
446                 flags, interact, defaults );
447
448 done:
449 #if defined( LDAP_R_COMPILE ) && defined( HAVE_CYRUS_SASL )
450         ldap_pvt_thread_mutex_unlock( &ldap_int_sasl_mutex );
451 #endif
452
453         return rc;
454 }