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