]> git.sur5r.net Git - openldap/blob - libraries/libldap/cyrus.c
b2707577dfa0e6c6f7dccd4c462a9e5de2cef1d4
[openldap] / libraries / libldap / cyrus.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1999-2002 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6
7 #include "portable.h"
8
9 #include <stdio.h>
10
11 #include <ac/socket.h>
12 #include <ac/stdlib.h>
13 #include <ac/string.h>
14 #include <ac/time.h>
15 #include <ac/errno.h>
16 #include <ac/ctype.h>
17
18 #include "ldap-int.h"
19
20 #ifdef HAVE_CYRUS_SASL
21
22 #ifdef LDAP_R_COMPILE
23 ldap_pvt_thread_mutex_t ldap_int_sasl_mutex;
24 #endif
25
26 #ifdef HAVE_SASL_SASL_H
27 #include <sasl/sasl.h>
28 #else
29 #include <sasl.h>
30 #endif
31
32 #if SASL_VERSION_MAJOR >= 2
33 #define SASL_CONST const
34 #else
35 #define SASL_CONST
36 #endif
37
38 /*
39 * Various Cyrus SASL related stuff.
40 */
41
42 int ldap_int_sasl_init( void )
43 {
44         /* XXX not threadsafe */
45         static int sasl_initialized = 0;
46
47         static sasl_callback_t client_callbacks[] = {
48 #ifdef SASL_CB_GETREALM
49                 { SASL_CB_GETREALM, NULL, NULL },
50 #endif
51                 { SASL_CB_USER, NULL, NULL },
52                 { SASL_CB_AUTHNAME, NULL, NULL },
53                 { SASL_CB_PASS, NULL, NULL },
54                 { SASL_CB_ECHOPROMPT, NULL, NULL },
55                 { SASL_CB_NOECHOPROMPT, NULL, NULL },
56                 { SASL_CB_LIST_END, NULL, NULL }
57         };
58
59         if ( sasl_initialized ) {
60                 return 0;
61         }
62
63 #ifndef CSRIMALLOC
64         sasl_set_alloc(
65                 ber_memalloc,
66                 ber_memcalloc,
67                 ber_memrealloc,
68                 ber_memfree );
69 #endif /* CSRIMALLOC */
70
71 #ifdef LDAP_R_COMPILE
72         sasl_set_mutex(
73                 ldap_pvt_sasl_mutex_new,
74                 ldap_pvt_sasl_mutex_lock,
75                 ldap_pvt_sasl_mutex_unlock,    
76                 ldap_pvt_sasl_mutex_dispose );    
77
78         ldap_pvt_thread_mutex_init( &ldap_int_sasl_mutex );
79 #endif
80
81         if ( sasl_client_init( client_callbacks ) == SASL_OK ) {
82                 sasl_initialized = 1;
83                 return 0;
84         }
85
86         return -1;
87 }
88
89 /*
90  * SASL encryption support for LBER Sockbufs
91  */
92
93 struct sb_sasl_data {
94         sasl_conn_t             *sasl_context;
95         Sockbuf_Buf             sec_buf_in;
96         Sockbuf_Buf             buf_in;
97         Sockbuf_Buf             buf_out;
98 };
99
100 static int
101 sb_sasl_setup( Sockbuf_IO_Desc *sbiod, void *arg )
102 {
103         struct sb_sasl_data     *p;
104
105         assert( sbiod != NULL );
106
107         p = LBER_MALLOC( sizeof( *p ) );
108         if ( p == NULL )
109                 return -1;
110         p->sasl_context = (sasl_conn_t *)arg;
111         ber_pvt_sb_buf_init( &p->sec_buf_in );
112         ber_pvt_sb_buf_init( &p->buf_in );
113         ber_pvt_sb_buf_init( &p->buf_out );
114         if ( ber_pvt_sb_grow_buffer( &p->sec_buf_in, SASL_MIN_BUFF_SIZE ) < 0 ) {
115                 errno = ENOMEM;
116                 return -1;
117         }
118
119         sbiod->sbiod_pvt = p;
120
121         return 0;
122 }
123
124 static int
125 sb_sasl_remove( Sockbuf_IO_Desc *sbiod )
126 {
127         struct sb_sasl_data     *p;
128
129         assert( sbiod != NULL );
130         
131         p = (struct sb_sasl_data *)sbiod->sbiod_pvt;
132 #if SASL_VERSION_MAJOR >= 2
133         /*
134          * SASLv2 encode/decode buffers are managed by
135          * libsasl2. Ensure they are not freed by liblber.
136          */
137         p->buf_in.buf_base = NULL;
138         p->buf_out.buf_base = NULL;
139 #endif
140         ber_pvt_sb_buf_destroy( &p->sec_buf_in );
141         ber_pvt_sb_buf_destroy( &p->buf_in );
142         ber_pvt_sb_buf_destroy( &p->buf_out );
143         LBER_FREE( p );
144         sbiod->sbiod_pvt = NULL;
145         return 0;
146 }
147
148 static ber_len_t
149 sb_sasl_pkt_length( const unsigned char *buf, int debuglevel )
150 {
151         ber_len_t               size;
152
153         assert( buf != NULL );
154
155         size = buf[0] << 24
156                 | buf[1] << 16
157                 | buf[2] << 8
158                 | buf[3];
159    
160         /* we really should check against actual buffer size set
161          * in the secopts.
162          */
163         if ( size > SASL_MAX_BUFF_SIZE ) {
164                 /* somebody is trying to mess me up. */
165                 ber_log_printf( LDAP_DEBUG_ANY, debuglevel,
166                         "sb_sasl_pkt_length: received illegal packet length "
167                         "of %lu bytes\n", (unsigned long)size );      
168                 size = 16; /* this should lead to an error. */
169         }
170
171         return size + 4; /* include the size !!! */
172 }
173
174 /* Drop a processed packet from the input buffer */
175 static void
176 sb_sasl_drop_packet ( Sockbuf_Buf *sec_buf_in, int debuglevel )
177 {
178         ber_slen_t                      len;
179
180         len = sec_buf_in->buf_ptr - sec_buf_in->buf_end;
181         if ( len > 0 )
182                 AC_MEMCPY( sec_buf_in->buf_base, sec_buf_in->buf_base +
183                         sec_buf_in->buf_end, len );
184    
185         if ( len >= 4 ) {
186                 sec_buf_in->buf_end = sb_sasl_pkt_length( sec_buf_in->buf_base,
187                         debuglevel);
188         }
189         else {
190                 sec_buf_in->buf_end = 0;
191         }
192         sec_buf_in->buf_ptr = len;
193 }
194
195 static ber_slen_t
196 sb_sasl_read( Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len)
197 {
198         struct sb_sasl_data     *p;
199         ber_slen_t              ret, bufptr;
200    
201         assert( sbiod != NULL );
202         assert( SOCKBUF_VALID( sbiod->sbiod_sb ) );
203
204         p = (struct sb_sasl_data *)sbiod->sbiod_pvt;
205
206         /* Are there anything left in the buffer? */
207         ret = ber_pvt_sb_copy_out( &p->buf_in, buf, len );
208         bufptr = ret;
209         len -= ret;
210
211         if ( len == 0 )
212                 return bufptr;
213
214 #if SASL_VERSION_MAJOR >= 2
215         ber_pvt_sb_buf_init( &p->buf_in );
216 #else
217         ber_pvt_sb_buf_destroy( &p->buf_in );
218 #endif
219
220         /* Read the length of the packet */
221         while ( p->sec_buf_in.buf_ptr < 4 ) {
222                 ret = LBER_SBIOD_READ_NEXT( sbiod, p->sec_buf_in.buf_base,
223                         4 - p->sec_buf_in.buf_ptr );
224 #ifdef EINTR
225                 if ( ( ret < 0 ) && ( errno == EINTR ) )
226                         continue;
227 #endif
228                 if ( ret <= 0 )
229                         return ret;
230
231                 p->sec_buf_in.buf_ptr += ret;
232         }
233
234         /* The new packet always starts at p->sec_buf_in.buf_base */
235         ret = sb_sasl_pkt_length( p->sec_buf_in.buf_base,
236                 sbiod->sbiod_sb->sb_debug );
237
238         /* Grow the packet buffer if neccessary */
239         if ( ( p->sec_buf_in.buf_size < (ber_len_t) ret ) && 
240                 ber_pvt_sb_grow_buffer( &p->sec_buf_in, ret ) < 0 )
241         {
242                 errno = ENOMEM;
243                 return -1;
244         }
245         p->sec_buf_in.buf_end = ret;
246
247         /* Did we read the whole encrypted packet? */
248         while ( p->sec_buf_in.buf_ptr < p->sec_buf_in.buf_end ) {
249                 /* No, we have got only a part of it */
250                 ret = p->sec_buf_in.buf_end - p->sec_buf_in.buf_ptr;
251
252                 ret = LBER_SBIOD_READ_NEXT( sbiod, p->sec_buf_in.buf_base +
253                         p->sec_buf_in.buf_ptr, ret );
254 #ifdef EINTR
255                 if ( ( ret < 0 ) && ( errno == EINTR ) )
256                         continue;
257 #endif
258                 if ( ret <= 0 )
259                         return ret;
260
261                 p->sec_buf_in.buf_ptr += ret;
262         }
263
264         /* Decode the packet */
265         ret = sasl_decode( p->sasl_context, p->sec_buf_in.buf_base,
266                 p->sec_buf_in.buf_end,
267                 (SASL_CONST char **)&p->buf_in.buf_base,
268                 (unsigned *)&p->buf_in.buf_end );
269         if ( ret != SASL_OK ) {
270                 ber_log_printf( LDAP_DEBUG_ANY, sbiod->sbiod_sb->sb_debug,
271                         "sb_sasl_read: failed to decode packet: %s\n",
272                         sasl_errstring( ret, NULL, NULL ) );
273                 sb_sasl_drop_packet( &p->sec_buf_in,
274                         sbiod->sbiod_sb->sb_debug );
275                 errno = EIO;
276                 return -1;
277         }
278         
279         /* Drop the packet from the input buffer */
280         sb_sasl_drop_packet( &p->sec_buf_in, sbiod->sbiod_sb->sb_debug );
281
282         p->buf_in.buf_size = p->buf_in.buf_end;
283
284         bufptr += ber_pvt_sb_copy_out( &p->buf_in, (char*) buf + bufptr, len );
285
286         return bufptr;
287 }
288
289 static ber_slen_t
290 sb_sasl_write( Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len)
291 {
292         struct sb_sasl_data     *p;
293         int                     ret;
294         unsigned                *max;
295
296         assert( sbiod != NULL );
297         assert( SOCKBUF_VALID( sbiod->sbiod_sb ) );
298
299         p = (struct sb_sasl_data *)sbiod->sbiod_pvt;
300
301         /* Are there anything left in the buffer? */
302         if ( p->buf_out.buf_ptr != p->buf_out.buf_end ) {
303                 ret = ber_pvt_sb_do_write( sbiod, &p->buf_out );
304                 if ( ret <= 0 )
305                         return ret;
306         }
307
308         /* now encode the next packet. */
309 #if SASL_VERSION_MAJOR >= 2
310         ber_pvt_sb_buf_init( &p->buf_out );
311         sasl_getprop( p->sasl_context, SASL_MAXOUTBUF, (const void **)&max );
312 #else
313         ber_pvt_sb_buf_destroy( &p->buf_out );
314         sasl_getprop( p->sasl_context, SASL_MAXOUTBUF, (void **)&max );
315 #endif
316         if ( len > *max - 100 )
317                 len = *max - 100;       /* For safety margin */
318         ret = sasl_encode( p->sasl_context, buf, len,
319                 (SASL_CONST char **)&p->buf_out.buf_base,
320                 (unsigned *)&p->buf_out.buf_size );
321         if ( ret != SASL_OK ) {
322                 ber_log_printf( LDAP_DEBUG_ANY, sbiod->sbiod_sb->sb_debug,
323                         "sb_sasl_write: failed to encode packet: %s\n",
324                         sasl_errstring( ret, NULL, NULL ) );
325                 return -1;
326         }
327         p->buf_out.buf_end = p->buf_out.buf_size;
328
329         ret = ber_pvt_sb_do_write( sbiod, &p->buf_out );
330         if ( ret <= 0 )
331                 return ret;
332         return len;
333 }
334
335 static int
336 sb_sasl_ctrl( Sockbuf_IO_Desc *sbiod, int opt, void *arg )
337 {
338         struct sb_sasl_data     *p;
339
340         p = (struct sb_sasl_data *)sbiod->sbiod_pvt;
341
342         if ( opt == LBER_SB_OPT_DATA_READY ) {
343                 if ( p->buf_in.buf_ptr != p->buf_in.buf_end )
344                         return 1;
345         }
346         
347         return LBER_SBIOD_CTRL_NEXT( sbiod, opt, arg );
348 }
349
350 Sockbuf_IO ldap_pvt_sockbuf_io_sasl = {
351         sb_sasl_setup,          /* sbi_setup */
352         sb_sasl_remove,         /* sbi_remove */
353         sb_sasl_ctrl,           /* sbi_ctrl */
354         sb_sasl_read,           /* sbi_read */
355         sb_sasl_write,          /* sbi_write */
356         NULL                    /* sbi_close */
357 };
358
359 int ldap_pvt_sasl_install( Sockbuf *sb, void *ctx_arg )
360 {
361 #ifdef NEW_LOGGING
362         LDAP_LOG (( "cyrus", LDAP_LEVEL_ENTRY, "ldap_pvt_sasl_install\n" ));
363 #else
364         Debug( LDAP_DEBUG_TRACE, "ldap_pvt_sasl_install\n",
365                 0, 0, 0 );
366 #endif
367
368         /* don't install the stuff unless security has been negotiated */
369
370         if ( !ber_sockbuf_ctrl( sb, LBER_SB_OPT_HAS_IO,
371                         &ldap_pvt_sockbuf_io_sasl ) )
372         {
373 #ifdef LDAP_DEBUG
374                 ber_sockbuf_add_io( sb, &ber_sockbuf_io_debug,
375                         LBER_SBIOD_LEVEL_APPLICATION, (void *)"sasl_" );
376 #endif
377                 ber_sockbuf_add_io( sb, &ldap_pvt_sockbuf_io_sasl,
378                         LBER_SBIOD_LEVEL_APPLICATION, ctx_arg );
379         }
380
381         return LDAP_SUCCESS;
382 }
383
384 static int
385 sasl_err2ldap( int saslerr )
386 {
387         int rc;
388
389         switch (saslerr) {
390                 case SASL_CONTINUE:
391                         rc = LDAP_MORE_RESULTS_TO_RETURN;
392                         break;
393                 case SASL_INTERACT:
394                         rc = LDAP_LOCAL_ERROR;
395                         break;
396                 case SASL_OK:
397                         rc = LDAP_SUCCESS;
398                         break;
399                 case SASL_FAIL:
400                         rc = LDAP_LOCAL_ERROR;
401                         break;
402                 case SASL_NOMEM:
403                         rc = LDAP_NO_MEMORY;
404                         break;
405                 case SASL_NOMECH:
406                         rc = LDAP_AUTH_UNKNOWN;
407                         break;
408                 case SASL_BADAUTH:
409                         rc = LDAP_AUTH_UNKNOWN;
410                         break;
411                 case SASL_NOAUTHZ:
412                         rc = LDAP_PARAM_ERROR;
413                         break;
414                 case SASL_TOOWEAK:
415                 case SASL_ENCRYPT:
416                         rc = LDAP_AUTH_UNKNOWN;
417                         break;
418                 default:
419                         rc = LDAP_LOCAL_ERROR;
420                         break;
421         }
422
423         assert( rc == LDAP_SUCCESS || LDAP_API_ERROR( rc ) );
424         return rc;
425 }
426
427 int
428 ldap_int_sasl_open(
429         LDAP *ld, 
430         LDAPConn *lc,
431         const char * host,
432         ber_len_t ssf )
433 {
434         int rc;
435         sasl_conn_t *ctx;
436
437         assert( lc->lconn_sasl_ctx == NULL );
438
439         if ( host == NULL ) {
440                 ld->ld_errno = LDAP_LOCAL_ERROR;
441                 return ld->ld_errno;
442         }
443
444 #if SASL_VERSION_MAJOR >= 2
445         rc = sasl_client_new( "ldap", host, NULL, NULL,
446                 NULL, 0, &ctx );
447 #else
448         rc = sasl_client_new( "ldap", host, NULL,
449                 SASL_SECURITY_LAYER, &ctx );
450 #endif
451
452         if ( rc != SASL_OK ) {
453                 ld->ld_errno = sasl_err2ldap( rc );
454                 return ld->ld_errno;
455         }
456
457 #ifdef NEW_LOGGING
458         LDAP_LOG (( "cyrus", LDAP_LEVEL_DETAIL1, 
459                 "ldap_int_sasl_open: host=%s\n", host ));
460 #else
461         Debug( LDAP_DEBUG_TRACE, "ldap_int_sasl_open: host=%s\n",
462                 host, 0, 0 );
463 #endif
464
465         lc->lconn_sasl_ctx = ctx;
466
467         if( ssf ) {
468 #if SASL_VERSION_MAJOR >= 2
469                 (void) sasl_setprop( ctx, SASL_SSF_EXTERNAL,
470                         (void *) &ssf );
471 #else
472                 sasl_external_properties_t extprops;
473                 memset(&extprops, 0L, sizeof(extprops));
474                 extprops.ssf = ssf;
475
476                 (void) sasl_setprop( ctx, SASL_SSF_EXTERNAL,
477                         (void *) &extprops );
478 #endif
479 #ifdef NEW_LOGGING
480                 LDAP_LOG (( "cyrus", LDAP_LEVEL_DETAIL1, 
481                         "ldap_int_sasl_open: ssf=%ld\n", (long) ssf ));
482 #else
483                 Debug( LDAP_DEBUG_TRACE, "ldap_int_sasl_open: ssf=%ld\n",
484                         (long) ssf, 0, 0 );
485 #endif
486         }
487
488         return LDAP_SUCCESS;
489 }
490
491 int ldap_int_sasl_close( LDAP *ld, LDAPConn *lc )
492 {
493         sasl_conn_t *ctx = lc->lconn_sasl_ctx;
494
495         if( ctx != NULL ) {
496                 sasl_dispose( &ctx );
497                 lc->lconn_sasl_ctx = NULL;
498         }
499
500         return LDAP_SUCCESS;
501 }
502
503 int
504 ldap_int_sasl_bind(
505         LDAP                    *ld,
506         const char              *dn,
507         const char              *mechs,
508         LDAPControl             **sctrls,
509         LDAPControl             **cctrls,
510         unsigned                flags,
511         LDAP_SASL_INTERACT_PROC *interact,
512         void * defaults )
513 {
514         char *data;
515         const char *mech = NULL;
516         const char *pmech = NULL;
517         int                     saslrc, rc;
518         sasl_ssf_t              *ssf = NULL;
519         sasl_conn_t     *ctx;
520         sasl_interact_t *prompts = NULL;
521         unsigned credlen;
522         struct berval ccred;
523         ber_socket_t            sd;
524
525 #ifdef NEW_LOGGING
526         LDAP_LOG (( "cyrus", LDAP_LEVEL_ARGS, 
527                         "ldap_int_sasl_bind: %s\n", mechs ? mechs : "<null>" ));
528 #else
529         Debug( LDAP_DEBUG_TRACE, "ldap_int_sasl_bind: %s\n",
530                 mechs ? mechs : "<null>", 0, 0 );
531 #endif
532
533         /* do a quick !LDAPv3 check... ldap_sasl_bind will do the rest. */
534         if (ld->ld_version < LDAP_VERSION3) {
535                 ld->ld_errno = LDAP_NOT_SUPPORTED;
536                 return ld->ld_errno;
537         }
538
539         ber_sockbuf_ctrl( ld->ld_sb, LBER_SB_OPT_GET_FD, &sd );
540
541         if ( sd == AC_SOCKET_INVALID ) {
542                 /* not connected yet */
543                 int rc;
544
545                 rc = ldap_open_defconn( ld );
546                 if( rc < 0 ) return ld->ld_errno;
547
548                 ber_sockbuf_ctrl( ld->ld_sb, LBER_SB_OPT_GET_FD, &sd );
549
550                 if( sd == AC_SOCKET_INVALID ) {
551                         ld->ld_errno = LDAP_LOCAL_ERROR;
552                         return ld->ld_errno;
553                 }
554         }   
555
556         ctx = ld->ld_defconn->lconn_sasl_ctx;
557
558         if( ctx == NULL ) {
559                 ld->ld_errno = LDAP_LOCAL_ERROR;
560                 return ld->ld_errno;
561         }
562
563         /* (re)set security properties */
564         sasl_setprop( ctx, SASL_SEC_PROPS,
565                 &ld->ld_options.ldo_sasl_secprops );
566
567         ccred.bv_val = NULL;
568         ccred.bv_len = 0;
569
570         do {
571                 saslrc = sasl_client_start( ctx,
572                         mechs,
573 #if SASL_VERSION_MAJOR < 2
574                         NULL,
575 #endif
576                         &prompts,
577                         (SASL_CONST char **)&ccred.bv_val,
578                         &credlen,
579                         &mech );
580
581                 if( pmech == NULL && mech != NULL ) {
582                         pmech = mech;
583
584                         if( flags != LDAP_SASL_QUIET ) {
585                                 fprintf(stderr,
586                                         "SASL/%s authentication started\n",
587                                         pmech );
588                         }
589                 }
590
591 #if SASL_VERSION_MAJOR >= 2
592                 /* XXX the application should free interact results. */
593                 if ( prompts != NULL && prompts->result != NULL ) {
594                         LDAP_FREE( (void *)prompts->result );
595                         prompts->result = NULL;
596                 }
597 #endif
598
599                 if( saslrc == SASL_INTERACT ) {
600                         int res;
601                         if( !interact ) break;
602                         res = (interact)( ld, flags, defaults, prompts );
603                         if( res != LDAP_SUCCESS ) {
604                                 break;
605                         }
606                 }
607         } while ( saslrc == SASL_INTERACT );
608
609         ccred.bv_len = credlen;
610
611         if ( (saslrc != SASL_OK) && (saslrc != SASL_CONTINUE) ) {
612                 ld->ld_errno = sasl_err2ldap( saslrc );
613 #if SASL_VERSION_MAJOR >= 2
614                 ld->ld_error = (char *)sasl_errdetail( ctx );
615 #endif
616                 return ld->ld_errno;
617         }
618
619         do {
620                 struct berval *scred;
621                 unsigned credlen;
622
623                 scred = NULL;
624
625                 rc = ldap_sasl_bind_s( ld, dn, mech, &ccred, sctrls, cctrls, &scred );
626
627                 if ( ccred.bv_val != NULL ) {
628 #if SASL_VERSION_MAJOR < 2
629                         LDAP_FREE( ccred.bv_val );
630 #endif
631                         ccred.bv_val = NULL;
632                 }
633
634                 if ( rc != LDAP_SUCCESS && rc != LDAP_SASL_BIND_IN_PROGRESS ) {
635                         if( scred && scred->bv_len ) {
636                                 /* and server provided us with data? */
637 #ifdef NEW_LOGGING
638                                 LDAP_LOG (( "cyrus", LDAP_LEVEL_DETAIL1, 
639                                         "ldap_int_sasl_bind: rc=%d sasl=%d len=%ld\n", 
640                                         rc, saslrc, scred->bv_len ));
641 #else
642                                 Debug( LDAP_DEBUG_TRACE,
643                                         "ldap_int_sasl_bind: rc=%d sasl=%d len=%ld\n",
644                                         rc, saslrc, scred->bv_len );
645 #endif
646                                 ber_bvfree( scred );
647                         }
648                         return ld->ld_errno;
649                 }
650
651                 if( rc == LDAP_SUCCESS && saslrc == SASL_OK ) {
652                         /* we're done, no need to step */
653                         if( scred && scred->bv_len ) {
654                                 /* but server provided us with data! */
655 #ifdef NEW_LOGGING
656                                 LDAP_LOG (( "cyrus", LDAP_LEVEL_DETAIL1, 
657                                         "ldap_int_sasl_bind: rc=%d sasl=%d len=%ld\n", 
658                                         rc, saslrc, scred->bv_len ));
659 #else
660                                 Debug( LDAP_DEBUG_TRACE,
661                                         "ldap_int_sasl_bind: rc=%d sasl=%d len=%ld\n",
662                                         rc, saslrc, scred->bv_len );
663 #endif
664                                 ber_bvfree( scred );
665                                 return ld->ld_errno = LDAP_LOCAL_ERROR;
666                         }
667                         break;
668                 }
669
670                 do {
671                         saslrc = sasl_client_step( ctx,
672                                 (scred == NULL) ? NULL : scred->bv_val,
673                                 (scred == NULL) ? 0 : scred->bv_len,
674                                 &prompts,
675                                 (SASL_CONST char **)&ccred.bv_val,
676                                 &credlen );
677
678 #ifdef NEW_LOGGING
679                                 LDAP_LOG (( "cyrus", LDAP_LEVEL_DETAIL1, 
680                                         "ldap_int_sasl_bind: sasl_client_step: %d\n", saslrc ));
681 #else
682                         Debug( LDAP_DEBUG_TRACE, "sasl_client_step: %d\n",
683                                 saslrc, 0, 0 );
684 #endif
685
686 #if SASL_VERSION_MAJOR >= 2
687                         /* XXX the application should free interact results. */
688                         if ( prompts != NULL && prompts->result != NULL ) {
689                                 LDAP_FREE( (void *)prompts->result );
690                                 prompts->result = NULL;
691                         }
692 #endif
693
694                         if( saslrc == SASL_INTERACT ) {
695                                 int res;
696                                 if( !interact ) break;
697                                 res = (interact)( ld, flags, defaults, prompts );
698                                 if( res != LDAP_SUCCESS ) {
699                                         break;
700                                 }
701                         }
702                 } while ( saslrc == SASL_INTERACT );
703
704                 ccred.bv_len = credlen;
705                 ber_bvfree( scred );
706
707                 if ( (saslrc != SASL_OK) && (saslrc != SASL_CONTINUE) ) {
708                         ld->ld_errno = sasl_err2ldap( saslrc );
709 #if SASL_VERSION_MAJOR >= 2
710                         ld->ld_error = (char *)sasl_errdetail( ctx );
711 #endif
712                         return ld->ld_errno;
713                 }
714         } while ( rc == LDAP_SASL_BIND_IN_PROGRESS );
715
716         if ( rc != LDAP_SUCCESS ) {
717                 return rc;
718         }
719
720         if ( saslrc != SASL_OK ) {
721 #if SASL_VERSION_MAJOR >= 2
722                 ld->ld_error = (char *)sasl_errdetail( ctx );
723 #endif
724                 return ld->ld_errno = sasl_err2ldap( saslrc );
725         }
726
727         if( flags != LDAP_SASL_QUIET ) {
728                 saslrc = sasl_getprop( ctx, SASL_USERNAME, (SASL_CONST void **) &data );
729                 if( saslrc == SASL_OK && data && *data ) {
730                         fprintf( stderr, "SASL username: %s\n", data );
731                 }
732
733 #if SASL_VERSION_MAJOR >= 2
734                 saslrc = sasl_getprop( ctx, SASL_DEFUSERREALM, (SASL_CONST void **) &data );
735 #else
736                 saslrc = sasl_getprop( ctx, SASL_REALM, (SASL_CONST void **) &data );
737 #endif
738                 if( saslrc == SASL_OK && data && *data ) {
739                         fprintf( stderr, "SASL realm: %s\n", data );
740                 }
741         }
742
743         saslrc = sasl_getprop( ctx, SASL_SSF, (SASL_CONST void **) &ssf );
744         if( saslrc == SASL_OK ) {
745                 if( flags != LDAP_SASL_QUIET ) {
746                         fprintf( stderr, "SASL SSF: %lu\n",
747                                 (unsigned long) *ssf );
748                 }
749
750                 if( ssf && *ssf ) {
751                         if( flags != LDAP_SASL_QUIET ) {
752                                 fprintf( stderr, "SASL installing layers\n" );
753                         }
754                         ldap_pvt_sasl_install( ld->ld_conns->lconn_sb, ctx );
755                 }
756         }
757
758         return rc;
759 }
760
761 int
762 ldap_int_sasl_external(
763         LDAP *ld,
764         LDAPConn *conn,
765         const char * authid,
766         ber_len_t ssf )
767 {
768         int sc;
769         sasl_conn_t *ctx;
770 #if SASL_VERSION_MAJOR < 2
771         sasl_external_properties_t extprops;
772 #endif
773
774         ctx = conn->lconn_sasl_ctx;
775
776         if ( ctx == NULL ) {
777                 return LDAP_LOCAL_ERROR;
778         }
779    
780 #if SASL_VERSION_MAJOR >= 2
781         sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL, &ssf );
782         if ( sc == SASL_OK )
783                 sc = sasl_setprop( ctx, SASL_AUTH_EXTERNAL, authid );
784 #else
785         memset( &extprops, '\0', sizeof(extprops) );
786         extprops.ssf = ssf;
787         extprops.auth_id = (char *) authid;
788
789         sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL,
790                 (void *) &extprops );
791 #endif
792
793         if ( sc != SASL_OK ) {
794                 return LDAP_LOCAL_ERROR;
795         }
796
797         return LDAP_SUCCESS;
798 }
799
800
801 int ldap_pvt_sasl_secprops(
802         const char *in,
803         sasl_security_properties_t *secprops )
804 {
805         int i;
806         char **props = ldap_str2charray( in, "," );
807         unsigned sflags = 0;
808         int got_sflags = 0;
809         sasl_ssf_t max_ssf = 0;
810         int got_max_ssf = 0;
811         sasl_ssf_t min_ssf = 0;
812         int got_min_ssf = 0;
813         unsigned maxbufsize = 0;
814         int got_maxbufsize = 0;
815
816         if( props == NULL || secprops == NULL ) {
817                 return LDAP_PARAM_ERROR;
818         }
819
820         for( i=0; props[i]; i++ ) {
821                 if( !strcasecmp(props[i], "none") ) {
822                         got_sflags++;
823
824                 } else if( !strcasecmp(props[i], "noplain") ) {
825                         got_sflags++;
826                         sflags |= SASL_SEC_NOPLAINTEXT;
827
828                 } else if( !strcasecmp(props[i], "noactive") ) {
829                         got_sflags++;
830                         sflags |= SASL_SEC_NOACTIVE;
831
832                 } else if( !strcasecmp(props[i], "nodict") ) {
833                         got_sflags++;
834                         sflags |= SASL_SEC_NODICTIONARY;
835
836                 } else if( !strcasecmp(props[i], "forwardsec") ) {
837                         got_sflags++;
838                         sflags |= SASL_SEC_FORWARD_SECRECY;
839
840                 } else if( !strcasecmp(props[i], "noanonymous")) {
841                         got_sflags++;
842                         sflags |= SASL_SEC_NOANONYMOUS;
843
844                 } else if( !strcasecmp(props[i], "passcred") ) {
845                         got_sflags++;
846                         sflags |= SASL_SEC_PASS_CREDENTIALS;
847
848                 } else if( !strncasecmp(props[i],
849                         "minssf=", sizeof("minssf")) )
850                 {
851                         if( isdigit( (unsigned char) props[i][sizeof("minssf")] ) ) {
852                                 got_min_ssf++;
853                                 min_ssf = atoi( &props[i][sizeof("minssf")] );
854                         } else {
855                                 return LDAP_NOT_SUPPORTED;
856                         }
857
858                 } else if( !strncasecmp(props[i],
859                         "maxssf=", sizeof("maxssf")) )
860                 {
861                         if( isdigit( (unsigned char) props[i][sizeof("maxssf")] ) ) {
862                                 got_max_ssf++;
863                                 max_ssf = atoi( &props[i][sizeof("maxssf")] );
864                         } else {
865                                 return LDAP_NOT_SUPPORTED;
866                         }
867
868                 } else if( !strncasecmp(props[i],
869                         "maxbufsize=", sizeof("maxbufsize")) )
870                 {
871                         if( isdigit( (unsigned char) props[i][sizeof("maxbufsize")] ) ) {
872                                 got_maxbufsize++;
873                                 maxbufsize = atoi( &props[i][sizeof("maxbufsize")] );
874                         } else {
875                                 return LDAP_NOT_SUPPORTED;
876                         }
877
878                         if( maxbufsize && (( maxbufsize < SASL_MIN_BUFF_SIZE )
879                                 || (maxbufsize > SASL_MAX_BUFF_SIZE )))
880                         {
881                                 /* bad maxbufsize */
882                                 return LDAP_PARAM_ERROR;
883                         }
884
885                 } else {
886                         return LDAP_NOT_SUPPORTED;
887                 }
888         }
889
890         if(got_sflags) {
891                 secprops->security_flags = sflags;
892         }
893         if(got_min_ssf) {
894                 secprops->min_ssf = min_ssf;
895         }
896         if(got_max_ssf) {
897                 secprops->max_ssf = max_ssf;
898         }
899         if(got_maxbufsize) {
900                 secprops->maxbufsize = maxbufsize;
901         }
902
903         ldap_charray_free( props );
904         return LDAP_SUCCESS;
905 }
906
907 int
908 ldap_int_sasl_config( struct ldapoptions *lo, int option, const char *arg )
909 {
910         int rc;
911
912         switch( option ) {
913         case LDAP_OPT_X_SASL_SECPROPS:
914                 rc = ldap_pvt_sasl_secprops( arg, &lo->ldo_sasl_secprops );
915                 if( rc == LDAP_SUCCESS ) return 0;
916         }
917
918         return -1;
919 }
920
921 int
922 ldap_int_sasl_get_option( LDAP *ld, int option, void *arg )
923 {
924         if ( ld == NULL )
925                 return -1;
926
927         switch ( option ) {
928                 case LDAP_OPT_X_SASL_MECH: {
929                         *(char **)arg = ld->ld_options.ldo_def_sasl_mech
930                                 ? LDAP_STRDUP( ld->ld_options.ldo_def_sasl_mech ) : NULL;
931                 } break;
932                 case LDAP_OPT_X_SASL_REALM: {
933                         *(char **)arg = ld->ld_options.ldo_def_sasl_realm
934                                 ? LDAP_STRDUP( ld->ld_options.ldo_def_sasl_realm ) : NULL;
935                 } break;
936                 case LDAP_OPT_X_SASL_AUTHCID: {
937                         *(char **)arg = ld->ld_options.ldo_def_sasl_authcid
938                                 ? LDAP_STRDUP( ld->ld_options.ldo_def_sasl_authcid ) : NULL;
939                 } break;
940                 case LDAP_OPT_X_SASL_AUTHZID: {
941                         *(char **)arg = ld->ld_options.ldo_def_sasl_authzid
942                                 ? LDAP_STRDUP( ld->ld_options.ldo_def_sasl_authzid ) : NULL;
943                 } break;
944
945                 case LDAP_OPT_X_SASL_SSF: {
946                         int sc;
947                         sasl_ssf_t      *ssf;
948                         sasl_conn_t *ctx;
949
950                         if( ld->ld_defconn == NULL ) {
951                                 return -1;
952                         }
953
954                         ctx = ld->ld_defconn->lconn_sasl_ctx;
955
956                         if ( ctx == NULL ) {
957                                 return -1;
958                         }
959
960                         sc = sasl_getprop( ctx, SASL_SSF,
961                                 (SASL_CONST void **) &ssf );
962
963                         if ( sc != SASL_OK ) {
964                                 return -1;
965                         }
966
967                         *(ber_len_t *)arg = *ssf;
968                 } break;
969
970                 case LDAP_OPT_X_SASL_SSF_EXTERNAL:
971                         /* this option is write only */
972                         return -1;
973
974                 case LDAP_OPT_X_SASL_SSF_MIN:
975                         *(ber_len_t *)arg = ld->ld_options.ldo_sasl_secprops.min_ssf;
976                         break;
977                 case LDAP_OPT_X_SASL_SSF_MAX:
978                         *(ber_len_t *)arg = ld->ld_options.ldo_sasl_secprops.max_ssf;
979                         break;
980                 case LDAP_OPT_X_SASL_MAXBUFSIZE:
981                         *(ber_len_t *)arg = ld->ld_options.ldo_sasl_secprops.maxbufsize;
982                         break;
983
984                 case LDAP_OPT_X_SASL_SECPROPS:
985                         /* this option is write only */
986                         return -1;
987
988                 default:
989                         return -1;
990         }
991         return 0;
992 }
993
994 int
995 ldap_int_sasl_set_option( LDAP *ld, int option, void *arg )
996 {
997         if ( ld == NULL )
998                 return -1;
999
1000         switch ( option ) {
1001         case LDAP_OPT_X_SASL_SSF:
1002                 /* This option is read-only */
1003                 return -1;
1004
1005         case LDAP_OPT_X_SASL_SSF_EXTERNAL: {
1006                 int sc;
1007 #if SASL_VERSION_MAJOR < 2
1008                 sasl_external_properties_t extprops;
1009 #endif
1010                 sasl_conn_t *ctx;
1011
1012                 if( ld->ld_defconn == NULL ) {
1013                         return -1;
1014                 }
1015
1016                 ctx = ld->ld_defconn->lconn_sasl_ctx;
1017
1018                 if ( ctx == NULL ) {
1019                         return -1;
1020                 }
1021
1022 #if SASL_VERSION_MAJOR >= 2
1023                 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL, arg);
1024 #else
1025                 memset(&extprops, 0L, sizeof(extprops));
1026
1027                 extprops.ssf = * (ber_len_t *) arg;
1028
1029                 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL,
1030                         (void *) &extprops );
1031 #endif
1032
1033                 if ( sc != SASL_OK ) {
1034                         return -1;
1035                 }
1036                 } break;
1037
1038         case LDAP_OPT_X_SASL_SSF_MIN:
1039                 ld->ld_options.ldo_sasl_secprops.min_ssf = *(ber_len_t *)arg;
1040                 break;
1041         case LDAP_OPT_X_SASL_SSF_MAX:
1042                 ld->ld_options.ldo_sasl_secprops.max_ssf = *(ber_len_t *)arg;
1043                 break;
1044         case LDAP_OPT_X_SASL_MAXBUFSIZE:
1045                 ld->ld_options.ldo_sasl_secprops.maxbufsize = *(ber_len_t *)arg;
1046                 break;
1047
1048         case LDAP_OPT_X_SASL_SECPROPS: {
1049                 int sc;
1050                 sc = ldap_pvt_sasl_secprops( (char *) arg,
1051                         &ld->ld_options.ldo_sasl_secprops );
1052
1053                 return sc == LDAP_SUCCESS ? 0 : -1;
1054                 }
1055
1056         default:
1057                 return -1;
1058         }
1059         return 0;
1060 }
1061
1062 #ifdef LDAP_R_COMPILE
1063 void *ldap_pvt_sasl_mutex_new(void)
1064 {
1065         ldap_pvt_thread_mutex_t *mutex;
1066
1067         mutex = (ldap_pvt_thread_mutex_t *) LDAP_MALLOC(
1068                 sizeof(ldap_pvt_thread_mutex_t) );
1069
1070         if ( ldap_pvt_thread_mutex_init( mutex ) == 0 ) {
1071                 return mutex;
1072         }
1073         return NULL;
1074 }
1075
1076 int ldap_pvt_sasl_mutex_lock(void *mutex)
1077 {
1078         return ldap_pvt_thread_mutex_lock( (ldap_pvt_thread_mutex_t *)mutex )
1079                 ? SASL_FAIL : SASL_OK;
1080 }
1081
1082 int ldap_pvt_sasl_mutex_unlock(void *mutex)
1083 {
1084         return ldap_pvt_thread_mutex_unlock( (ldap_pvt_thread_mutex_t *)mutex )
1085                 ? SASL_FAIL : SASL_OK;
1086 }
1087
1088 void ldap_pvt_sasl_mutex_dispose(void *mutex)
1089 {
1090         (void) ldap_pvt_thread_mutex_destroy( (ldap_pvt_thread_mutex_t *)mutex );
1091         LDAP_FREE( mutex );
1092 }
1093 #endif
1094
1095 #else
1096 int ldap_int_sasl_init( void )
1097 { return LDAP_SUCCESS; }
1098
1099 int ldap_int_sasl_close( LDAP *ld, LDAPConn *lc )
1100 { return LDAP_SUCCESS; }
1101
1102 int
1103 ldap_int_sasl_bind(
1104         LDAP                    *ld,
1105         const char              *dn,
1106         const char              *mechs,
1107         LDAPControl             **sctrls,
1108         LDAPControl             **cctrls,
1109         unsigned                flags,
1110         LDAP_SASL_INTERACT_PROC *interact,
1111         void * defaults )
1112 { return LDAP_NOT_SUPPORTED; }
1113
1114 int
1115 ldap_int_sasl_external(
1116         LDAP *ld,
1117         LDAPConn *conn,
1118         const char * authid,
1119         ber_len_t ssf )
1120 { return LDAP_SUCCESS; }
1121
1122 #endif /* HAVE_CYRUS_SASL */