]> git.sur5r.net Git - openldap/blob - libraries/libldap/sasl.c
0624394c3f859c05037a065de7f738aabe6478a9
[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, LDAP_MSG_ALL, 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                 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, "{eAA" /*}*/,
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         Debug( LDAP_DEBUG_TRACE, "ldap_pvt_sasl_getmech\n", 0, 0, 0 );
368
369         rc = ldap_search_s( ld, "", LDAP_SCOPE_BASE,
370                 NULL, attrs, 0, &res );
371
372         if ( rc != LDAP_SUCCESS ) {
373                 return ld->ld_errno;
374         }
375                 
376         e = ldap_first_entry( ld, res );
377         if ( e == NULL ) {
378                 ldap_msgfree( res );
379                 if ( ld->ld_errno == LDAP_SUCCESS ) {
380                         ld->ld_errno = LDAP_NO_SUCH_OBJECT;
381                 }
382                 return ld->ld_errno;
383         }
384
385         values = ldap_get_values( ld, e, "supportedSASLMechanisms" );
386         if ( values == NULL ) {
387                 ldap_msgfree( res );
388                 ld->ld_errno = LDAP_NO_SUCH_ATTRIBUTE;
389                 return ld->ld_errno;
390         }
391
392         mechlist = ldap_charray2str( values, " " );
393         if ( mechlist == NULL ) {
394                 LDAP_VFREE( values );
395                 ldap_msgfree( res );
396                 ld->ld_errno = LDAP_NO_MEMORY;
397                 return ld->ld_errno;
398         } 
399
400         LDAP_VFREE( values );
401         ldap_msgfree( res );
402
403         *pmechlist = mechlist;
404
405         return LDAP_SUCCESS;
406 }
407
408 /*
409  * ldap_sasl_interactive_bind_s - interactive SASL authentication
410  *
411  * This routine uses interactive callbacks.
412  *
413  * LDAP_SUCCESS is returned upon success, the ldap error code
414  * otherwise.
415  */
416 int
417 ldap_sasl_interactive_bind_s(
418         LDAP *ld,
419         LDAP_CONST char *dn, /* usually NULL */
420         LDAP_CONST char *mechs,
421         LDAPControl **serverControls,
422         LDAPControl **clientControls,
423         unsigned flags,
424         LDAP_SASL_INTERACT_PROC *interact,
425         void *defaults )
426 {
427         int rc;
428         char *smechs = NULL;
429
430 #if defined( LDAP_R_COMPILE ) && defined( HAVE_CYRUS_SASL )
431         ldap_pvt_thread_mutex_lock( &ldap_int_sasl_mutex );
432 #endif
433 #ifdef LDAP_CONNECTIONLESS
434         if( LDAP_IS_UDP(ld) ) {
435                 /* Just force it to simple bind, silly to make the user
436                  * ask all the time. No, we don't ever actually bind, but I'll
437                  * let the final bind handler take care of saving the cdn.
438                  */
439                 rc = ldap_simple_bind( ld, dn, NULL );
440                 rc = rc < 0 ? rc : 0;
441                 goto done;
442         } else
443 #endif
444
445 #ifdef HAVE_CYRUS_SASL
446         if( mechs == NULL || *mechs == '\0' ) {
447                 mechs = ld->ld_options.ldo_def_sasl_mech;
448         }
449 #endif
450                 
451         if( mechs == NULL || *mechs == '\0' ) {
452                 rc = ldap_pvt_sasl_getmechs( ld, &smechs );
453                 if( rc != LDAP_SUCCESS ) {
454                         goto done;
455                 }
456
457                 Debug( LDAP_DEBUG_TRACE,
458                         "ldap_sasl_interactive_bind_s: server supports: %s\n",
459                         smechs, 0, 0 );
460
461                 mechs = smechs;
462
463         } else {
464                 Debug( LDAP_DEBUG_TRACE,
465                         "ldap_sasl_interactive_bind_s: user selected: %s\n",
466                         mechs, 0, 0 );
467         }
468
469         rc = ldap_int_sasl_bind( ld, dn, mechs,
470                 serverControls, clientControls,
471                 flags, interact, defaults );
472
473 done:
474 #if defined( LDAP_R_COMPILE ) && defined( HAVE_CYRUS_SASL )
475         ldap_pvt_thread_mutex_unlock( &ldap_int_sasl_mutex );
476 #endif
477         if ( smechs ) LDAP_FREE( smechs );
478
479         return rc;
480 }