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