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