]> git.sur5r.net Git - openldap/blob - libraries/libldap/sasl.c
28802c868feb971fa2cf0d46f69682a8c14103b0
[openldap] / libraries / libldap / sasl.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2003 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 <stdio.h>
31
32 #include <ac/socket.h>
33 #include <ac/stdlib.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         ber_int_t id;
64
65 #ifdef NEW_LOGGING
66         LDAP_LOG ( TRANSPORT, ENTRY, "ldap_sasl_bind\n", 0, 0, 0 );
67 #else
68         Debug( LDAP_DEBUG_TRACE, "ldap_sasl_bind\n", 0, 0, 0 );
69 #endif
70
71         assert( ld != NULL );
72         assert( LDAP_VALID( ld ) );
73         assert( msgidp != NULL );
74
75         /* check client controls */
76         rc = ldap_int_client_controls( ld, cctrls );
77         if( rc != LDAP_SUCCESS ) return rc;
78
79         if( mechanism == LDAP_SASL_SIMPLE ) {
80                 if( dn == NULL && cred != NULL && cred->bv_len ) {
81                         /* use default binddn */
82                         dn = ld->ld_defbinddn;
83                 }
84
85         } else if( ld->ld_version < LDAP_VERSION3 ) {
86                 ld->ld_errno = LDAP_NOT_SUPPORTED;
87                 return ld->ld_errno;
88         }
89
90         if ( dn == NULL ) {
91                 dn = "";
92         }
93
94         /* create a message to send */
95         if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
96                 ld->ld_errno = LDAP_NO_MEMORY;
97                 return ld->ld_errno;
98         }
99
100         assert( LBER_VALID( ber ) );
101
102         LDAP_NEXT_MSGID( ld, id );
103         if( mechanism == LDAP_SASL_SIMPLE ) {
104                 /* simple bind */
105                 rc = ber_printf( ber, "{it{istON}" /*}*/,
106                         id, LDAP_REQ_BIND,
107                         ld->ld_version, dn, LDAP_AUTH_SIMPLE,
108                         cred );
109                 
110         } else if ( cred == NULL || cred->bv_val == NULL ) {
111                 /* SASL bind w/o creditials */
112                 rc = ber_printf( ber, "{it{ist{sN}N}" /*}*/,
113                         id, LDAP_REQ_BIND,
114                         ld->ld_version, dn, LDAP_AUTH_SASL,
115                         mechanism );
116
117         } else {
118                 /* SASL bind w/ creditials */
119                 rc = ber_printf( ber, "{it{ist{sON}N}" /*}*/,
120                         id, LDAP_REQ_BIND,
121                         ld->ld_version, dn, LDAP_AUTH_SASL,
122                         mechanism, cred );
123         }
124
125         if( rc == -1 ) {
126                 ld->ld_errno = LDAP_ENCODING_ERROR;
127                 ber_free( ber, 1 );
128                 return( -1 );
129         }
130
131         /* Put Server Controls */
132         if( ldap_int_put_controls( ld, sctrls, ber ) != LDAP_SUCCESS ) {
133                 ber_free( ber, 1 );
134                 return ld->ld_errno;
135         }
136
137         if ( ber_printf( ber, /*{*/ "N}" ) == -1 ) {
138                 ld->ld_errno = LDAP_ENCODING_ERROR;
139                 ber_free( ber, 1 );
140                 return ld->ld_errno;
141         }
142
143
144         /* send the message */
145         *msgidp = ldap_send_initial_request( ld, LDAP_REQ_BIND, dn, ber, id );
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 #ifdef NEW_LOGGING
169         LDAP_LOG ( TRANSPORT, ENTRY, "ldap_sasl_bind_s\n", 0, 0, 0 );
170 #else
171         Debug( LDAP_DEBUG_TRACE, "ldap_sasl_bind_s\n", 0, 0, 0 );
172 #endif
173
174         /* do a quick !LDAPv3 check... ldap_sasl_bind will do the rest. */
175         if( servercredp != NULL ) {
176                 if (ld->ld_version < LDAP_VERSION3) {
177                         ld->ld_errno = LDAP_NOT_SUPPORTED;
178                         return ld->ld_errno;
179                 }
180                 *servercredp = NULL;
181         }
182
183         rc = ldap_sasl_bind( ld, dn, mechanism, cred, sctrls, cctrls, &msgid );
184
185         if ( rc != LDAP_SUCCESS ) {
186                 return( rc );
187         }
188
189 #ifdef LDAP_CONNECTIONLESS
190         if (LDAP_IS_UDP(ld)) {
191                 return( rc );
192         }
193 #endif
194
195         if ( ldap_result( ld, msgid, 1, NULL, &result ) == -1 ) {
196                 return( ld->ld_errno ); /* ldap_result sets ld_errno */
197         }
198
199         /* parse the results */
200         scredp = NULL;
201         if( servercredp != NULL ) {
202                 rc = ldap_parse_sasl_bind_result( ld, result, &scredp, 0 );
203         }
204
205         if ( rc != LDAP_SUCCESS && rc != LDAP_SASL_BIND_IN_PROGRESS ) {
206                 ldap_msgfree( result );
207                 return( rc );
208         }
209
210         rc = ldap_result2error( ld, result, 1 );
211
212         if ( rc == LDAP_SUCCESS || rc == LDAP_SASL_BIND_IN_PROGRESS ) {
213                 if( servercredp != NULL ) {
214                         *servercredp = scredp;
215                         scredp = NULL;
216                 }
217         }
218
219         if ( scredp != NULL ) {
220                 ber_bvfree(scredp);
221         }
222
223         return rc;
224 }
225
226
227 /*
228 * Parse BindResponse:
229 *
230 *   BindResponse ::= [APPLICATION 1] SEQUENCE {
231 *     COMPONENTS OF LDAPResult,
232 *     serverSaslCreds  [7] OCTET STRING OPTIONAL }
233 *
234 *   LDAPResult ::= SEQUENCE {
235 *     resultCode      ENUMERATED,
236 *     matchedDN       LDAPDN,
237 *     errorMessage    LDAPString,
238 *     referral        [3] Referral OPTIONAL }
239 */
240
241 int
242 ldap_parse_sasl_bind_result(
243         LDAP                    *ld,
244         LDAPMessage             *res,
245         struct berval   **servercredp,
246         int                             freeit )
247 {
248         ber_int_t errcode;
249         struct berval* scred;
250
251         ber_tag_t tag;
252         BerElement      *ber;
253
254 #ifdef NEW_LOGGING
255         LDAP_LOG ( TRANSPORT, ENTRY, "ldap_parse_sasl_bind_result\n", 0, 0, 0 );
256 #else
257         Debug( LDAP_DEBUG_TRACE, "ldap_parse_sasl_bind_result\n", 0, 0, 0 );
258 #endif
259
260         assert( ld != NULL );
261         assert( LDAP_VALID( ld ) );
262         assert( res != NULL );
263
264         if( servercredp != NULL ) {
265                 if( ld->ld_version < LDAP_VERSION2 ) {
266                         return LDAP_NOT_SUPPORTED;
267                 }
268                 *servercredp = NULL;
269         }
270
271         if( res->lm_msgtype != LDAP_RES_BIND ) {
272                 ld->ld_errno = LDAP_PARAM_ERROR;
273                 return ld->ld_errno;
274         }
275
276         scred = NULL;
277
278         if ( ld->ld_error ) {
279                 LDAP_FREE( ld->ld_error );
280                 ld->ld_error = NULL;
281         }
282         if ( ld->ld_matched ) {
283                 LDAP_FREE( ld->ld_matched );
284                 ld->ld_matched = NULL;
285         }
286
287         /* parse results */
288
289         ber = ber_dup( res->lm_ber );
290
291         if( ber == NULL ) {
292                 ld->ld_errno = LDAP_NO_MEMORY;
293                 return ld->ld_errno;
294         }
295
296         if ( ld->ld_version < LDAP_VERSION2 ) {
297                 tag = ber_scanf( ber, "{ia}",
298                         &errcode, &ld->ld_error );
299
300                 if( tag == LBER_ERROR ) {
301                         ber_free( ber, 0 );
302                         ld->ld_errno = LDAP_DECODING_ERROR;
303                         return ld->ld_errno;
304                 }
305
306         } else {
307                 ber_len_t len;
308
309                 tag = ber_scanf( ber, "{iaa" /*}*/,
310                         &errcode, &ld->ld_matched, &ld->ld_error );
311
312                 if( tag == 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                 if( tag == LDAP_TAG_REFERRAL ) {
321                         /* skip 'em */
322                         if( ber_scanf( ber, "x" ) == LBER_ERROR ) {
323                                 ber_free( ber, 0 );
324                                 ld->ld_errno = LDAP_DECODING_ERROR;
325                                 return ld->ld_errno;
326                         }
327
328                         tag = ber_peek_tag(ber, &len);
329                 }
330
331                 if( tag == LDAP_TAG_SASL_RES_CREDS ) {
332                         if( ber_scanf( ber, "O", &scred ) == LBER_ERROR ) {
333                                 ber_free( ber, 0 );
334                                 ld->ld_errno = LDAP_DECODING_ERROR;
335                                 return ld->ld_errno;
336                         }
337                 }
338         }
339
340         ber_free( ber, 0 );
341
342         if ( servercredp != NULL ) {
343                 *servercredp = scred;
344
345         } else if ( scred != NULL ) {
346                 ber_bvfree( scred );
347         }
348
349         ld->ld_errno = errcode;
350
351         if ( freeit ) {
352                 ldap_msgfree( res );
353         }
354
355         return( ld->ld_errno );
356 }
357
358 int
359 ldap_pvt_sasl_getmechs ( LDAP *ld, char **pmechlist )
360 {
361         /* we need to query the server for supported mechs anyway */
362         LDAPMessage *res, *e;
363         char *attrs[] = { "supportedSASLMechanisms", NULL };
364         char **values, *mechlist;
365         int rc;
366
367 #ifdef NEW_LOGGING
368         LDAP_LOG ( TRANSPORT, ENTRY, "ldap_pvt_sasl_getmech\n", 0, 0, 0 );
369 #else
370         Debug( LDAP_DEBUG_TRACE, "ldap_pvt_sasl_getmech\n", 0, 0, 0 );
371 #endif
372
373         rc = ldap_search_s( ld, "", LDAP_SCOPE_BASE,
374                 NULL, attrs, 0, &res );
375
376         if ( rc != LDAP_SUCCESS ) {
377                 return ld->ld_errno;
378         }
379                 
380         e = ldap_first_entry( ld, res );
381         if ( e == NULL ) {
382                 ldap_msgfree( res );
383                 if ( ld->ld_errno == LDAP_SUCCESS ) {
384                         ld->ld_errno = LDAP_NO_SUCH_OBJECT;
385                 }
386                 return ld->ld_errno;
387         }
388
389         values = ldap_get_values( ld, e, "supportedSASLMechanisms" );
390         if ( values == NULL ) {
391                 ldap_msgfree( res );
392                 ld->ld_errno = LDAP_NO_SUCH_ATTRIBUTE;
393                 return ld->ld_errno;
394         }
395
396         mechlist = ldap_charray2str( values, " " );
397         if ( mechlist == NULL ) {
398                 LDAP_VFREE( values );
399                 ldap_msgfree( res );
400                 ld->ld_errno = LDAP_NO_MEMORY;
401                 return ld->ld_errno;
402         } 
403
404         LDAP_VFREE( values );
405         ldap_msgfree( res );
406
407         *pmechlist = mechlist;
408
409         return LDAP_SUCCESS;
410 }
411
412 /*
413  * ldap_sasl_interactive_bind_s - interactive SASL authentication
414  *
415  * This routine uses interactive callbacks.
416  *
417  * LDAP_SUCCESS is returned upon success, the ldap error code
418  * otherwise.
419  */
420 int
421 ldap_sasl_interactive_bind_s(
422         LDAP *ld,
423         LDAP_CONST char *dn, /* usually NULL */
424         LDAP_CONST char *mechs,
425         LDAPControl **serverControls,
426         LDAPControl **clientControls,
427         unsigned flags,
428         LDAP_SASL_INTERACT_PROC *interact,
429         void *defaults )
430 {
431         int rc;
432
433 #if defined( LDAP_R_COMPILE ) && defined( HAVE_CYRUS_SASL )
434         ldap_pvt_thread_mutex_lock( &ldap_int_sasl_mutex );
435 #endif
436 #ifdef LDAP_CONNECTIONLESS
437         if( LDAP_IS_UDP(ld) ) {
438                 /* Just force it to simple bind, silly to make the user
439                  * ask all the time. No, we don't ever actually bind, but I'll
440                  * let the final bind handler take care of saving the cdn.
441                  */
442                 rc = ldap_simple_bind(ld, dn, NULL);
443                 return rc < 0 ? rc : 0;
444         } else
445 #endif
446         if( mechs == NULL || *mechs == '\0' ) {
447                 char *smechs;
448
449                 rc = ldap_pvt_sasl_getmechs( ld, &smechs );
450
451                 if( rc != LDAP_SUCCESS ) {
452                         goto done;
453                 }
454
455 #ifdef NEW_LOGGING
456                 LDAP_LOG ( TRANSPORT, DETAIL1, 
457                         "ldap_interactive_sasl_bind_s: server supports: %s\n", 
458                         smechs, 0, 0 );
459 #else
460                 Debug( LDAP_DEBUG_TRACE,
461                         "ldap_interactive_sasl_bind_s: server supports: %s\n",
462                         smechs, 0, 0 );
463 #endif
464
465                 mechs = smechs;
466
467         } else {
468 #ifdef NEW_LOGGING
469                 LDAP_LOG ( TRANSPORT, DETAIL1, 
470                         "ldap_interactive_sasl_bind_s: user selected: %s\n", mechs, 0, 0 );
471 #else
472                 Debug( LDAP_DEBUG_TRACE,
473                         "ldap_interactive_sasl_bind_s: user selected: %s\n",
474                         mechs, 0, 0 );
475 #endif
476         }
477
478         rc = ldap_int_sasl_bind( ld, dn, mechs,
479                 serverControls, clientControls,
480                 flags, interact, defaults );
481
482 done:
483 #if defined( LDAP_R_COMPILE ) && defined( HAVE_CYRUS_SASL )
484         ldap_pvt_thread_mutex_unlock( &ldap_int_sasl_mutex );
485 #endif
486
487         return rc;
488 }