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