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