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