]> git.sur5r.net Git - openldap/blob - libraries/libldap/cyrus.c
Re: Patch: ctype functions require 'unsigned char' args (ITS#1678)
[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
295         assert( sbiod != NULL );
296         assert( SOCKBUF_VALID( sbiod->sbiod_sb ) );
297
298         p = (struct sb_sasl_data *)sbiod->sbiod_pvt;
299
300         /* Are there anything left in the buffer? */
301         if ( p->buf_out.buf_ptr != p->buf_out.buf_end ) {
302                 ret = ber_pvt_sb_do_write( sbiod, &p->buf_out );
303                 if ( ret <= 0 )
304                         return ret;
305         }
306
307         /* now encode the next packet. */
308 #if SASL_VERSION_MAJOR >= 2
309         ber_pvt_sb_buf_init( &p->buf_out );
310 #else
311         ber_pvt_sb_buf_destroy( &p->buf_out );
312 #endif
313         ret = sasl_encode( p->sasl_context, buf, len,
314                 (SASL_CONST char **)&p->buf_out.buf_base,
315                 (unsigned *)&p->buf_out.buf_size );
316         if ( ret != SASL_OK ) {
317                 ber_log_printf( LDAP_DEBUG_ANY, sbiod->sbiod_sb->sb_debug,
318                         "sb_sasl_write: failed to encode packet: %s\n",
319                         sasl_errstring( ret, NULL, NULL ) );
320                 return -1;
321         }
322         p->buf_out.buf_end = p->buf_out.buf_size;
323
324         ret = ber_pvt_sb_do_write( sbiod, &p->buf_out );
325         if ( ret <= 0 )
326                 return ret;
327         return len;
328 }
329
330 static int
331 sb_sasl_ctrl( Sockbuf_IO_Desc *sbiod, int opt, void *arg )
332 {
333         struct sb_sasl_data     *p;
334
335         p = (struct sb_sasl_data *)sbiod->sbiod_pvt;
336
337         if ( opt == LBER_SB_OPT_DATA_READY ) {
338                 if ( p->buf_in.buf_ptr != p->buf_in.buf_end )
339                         return 1;
340         }
341         
342         return LBER_SBIOD_CTRL_NEXT( sbiod, opt, arg );
343 }
344
345 Sockbuf_IO ldap_pvt_sockbuf_io_sasl = {
346         sb_sasl_setup,          /* sbi_setup */
347         sb_sasl_remove,         /* sbi_remove */
348         sb_sasl_ctrl,           /* sbi_ctrl */
349         sb_sasl_read,           /* sbi_read */
350         sb_sasl_write,          /* sbi_write */
351         NULL                    /* sbi_close */
352 };
353
354 int ldap_pvt_sasl_install( Sockbuf *sb, void *ctx_arg )
355 {
356 #ifdef NEW_LOGGING
357         LDAP_LOG (( "cyrus", LDAP_LEVEL_ENTRY, "ldap_pvt_sasl_install\n" ));
358 #else
359         Debug( LDAP_DEBUG_TRACE, "ldap_pvt_sasl_install\n",
360                 0, 0, 0 );
361 #endif
362
363         /* don't install the stuff unless security has been negotiated */
364
365         if ( !ber_sockbuf_ctrl( sb, LBER_SB_OPT_HAS_IO,
366                         &ldap_pvt_sockbuf_io_sasl ) )
367         {
368 #ifdef LDAP_DEBUG
369                 ber_sockbuf_add_io( sb, &ber_sockbuf_io_debug,
370                         LBER_SBIOD_LEVEL_APPLICATION, (void *)"sasl_" );
371 #endif
372                 ber_sockbuf_add_io( sb, &ldap_pvt_sockbuf_io_sasl,
373                         LBER_SBIOD_LEVEL_APPLICATION, ctx_arg );
374         }
375
376         return LDAP_SUCCESS;
377 }
378
379 static int
380 sasl_err2ldap( int saslerr )
381 {
382         int rc;
383
384         switch (saslerr) {
385                 case SASL_CONTINUE:
386                         rc = LDAP_MORE_RESULTS_TO_RETURN;
387                         break;
388                 case SASL_INTERACT:
389                         rc = LDAP_LOCAL_ERROR;
390                         break;
391                 case SASL_OK:
392                         rc = LDAP_SUCCESS;
393                         break;
394                 case SASL_FAIL:
395                         rc = LDAP_LOCAL_ERROR;
396                         break;
397                 case SASL_NOMEM:
398                         rc = LDAP_NO_MEMORY;
399                         break;
400                 case SASL_NOMECH:
401                         rc = LDAP_AUTH_UNKNOWN;
402                         break;
403                 case SASL_BADAUTH:
404                         rc = LDAP_AUTH_UNKNOWN;
405                         break;
406                 case SASL_NOAUTHZ:
407                         rc = LDAP_PARAM_ERROR;
408                         break;
409                 case SASL_TOOWEAK:
410                 case SASL_ENCRYPT:
411                         rc = LDAP_AUTH_UNKNOWN;
412                         break;
413                 default:
414                         rc = LDAP_LOCAL_ERROR;
415                         break;
416         }
417
418         assert( rc == LDAP_SUCCESS || LDAP_API_ERROR( rc ) );
419         return rc;
420 }
421
422 int
423 ldap_int_sasl_open(
424         LDAP *ld, 
425         LDAPConn *lc,
426         const char * host,
427         ber_len_t ssf )
428 {
429         int rc;
430         sasl_conn_t *ctx;
431
432         sasl_callback_t *session_callbacks =
433                 LDAP_CALLOC( 2, sizeof( sasl_callback_t ) );
434
435         if( session_callbacks == NULL ) return LDAP_NO_MEMORY;
436
437         session_callbacks[0].id = SASL_CB_USER;
438         session_callbacks[0].proc = NULL;
439         session_callbacks[0].context = ld;
440
441         session_callbacks[1].id = SASL_CB_LIST_END;
442         session_callbacks[1].proc = NULL;
443         session_callbacks[1].context = NULL;
444
445         assert( lc->lconn_sasl_ctx == NULL );
446
447         if ( host == NULL ) {
448                 ld->ld_errno = LDAP_LOCAL_ERROR;
449                 return ld->ld_errno;
450         }
451
452 #if SASL_VERSION_MAJOR >= 2
453         rc = sasl_client_new( "ldap", host, NULL, NULL,
454                 session_callbacks, 0, &ctx );
455 #else
456         rc = sasl_client_new( "ldap", host, session_callbacks,
457                 SASL_SECURITY_LAYER, &ctx );
458 #endif
459         LDAP_FREE( session_callbacks );
460
461         if ( rc != SASL_OK ) {
462                 ld->ld_errno = sasl_err2ldap( rc );
463                 return ld->ld_errno;
464         }
465
466 #ifdef NEW_LOGGING
467         LDAP_LOG (( "cyrus", LDAP_LEVEL_DETAIL1, 
468                 "ldap_int_sasl_open: host=%s\n", host ));
469 #else
470         Debug( LDAP_DEBUG_TRACE, "ldap_int_sasl_open: host=%s\n",
471                 host, 0, 0 );
472 #endif
473
474         lc->lconn_sasl_ctx = ctx;
475
476         if( ssf ) {
477 #if SASL_VERSION_MAJOR >= 2
478                 (void) sasl_setprop( ctx, SASL_SSF_EXTERNAL,
479                         (void *) &ssf );
480 #else
481                 sasl_external_properties_t extprops;
482                 memset(&extprops, 0L, sizeof(extprops));
483                 extprops.ssf = ssf;
484
485                 (void) sasl_setprop( ctx, SASL_SSF_EXTERNAL,
486                         (void *) &extprops );
487 #endif
488 #ifdef NEW_LOGGING
489                 LDAP_LOG (( "cyrus", LDAP_LEVEL_DETAIL1, 
490                         "ldap_int_sasl_open: ssf=%ld\n", (long) ssf ));
491 #else
492                 Debug( LDAP_DEBUG_TRACE, "ldap_int_sasl_open: ssf=%ld\n",
493                         (long) ssf, 0, 0 );
494 #endif
495         }
496
497         return LDAP_SUCCESS;
498 }
499
500 int ldap_int_sasl_close( LDAP *ld, LDAPConn *lc )
501 {
502         sasl_conn_t *ctx = lc->lconn_sasl_ctx;
503
504         if( ctx != NULL ) {
505                 sasl_dispose( &ctx );
506                 lc->lconn_sasl_ctx = NULL;
507         }
508
509         return LDAP_SUCCESS;
510 }
511
512 int
513 ldap_int_sasl_bind(
514         LDAP                    *ld,
515         const char              *dn,
516         const char              *mechs,
517         LDAPControl             **sctrls,
518         LDAPControl             **cctrls,
519         unsigned                flags,
520         LDAP_SASL_INTERACT_PROC *interact,
521         void * defaults )
522 {
523         char *data;
524         const char *mech = NULL;
525         const char *pmech = NULL;
526         int                     saslrc, rc;
527         sasl_ssf_t              *ssf = NULL;
528         sasl_conn_t     *ctx;
529         sasl_interact_t *prompts = NULL;
530         unsigned credlen;
531         struct berval ccred;
532         ber_socket_t            sd;
533
534 #ifdef NEW_LOGGING
535         LDAP_LOG (( "cyrus", LDAP_LEVEL_ARGS, 
536                         "ldap_int_sasl_bind: %s\n", mechs ? mechs : "<null>" ));
537 #else
538         Debug( LDAP_DEBUG_TRACE, "ldap_int_sasl_bind: %s\n",
539                 mechs ? mechs : "<null>", 0, 0 );
540 #endif
541
542         /* do a quick !LDAPv3 check... ldap_sasl_bind will do the rest. */
543         if (ld->ld_version < LDAP_VERSION3) {
544                 ld->ld_errno = LDAP_NOT_SUPPORTED;
545                 return ld->ld_errno;
546         }
547
548         ber_sockbuf_ctrl( ld->ld_sb, LBER_SB_OPT_GET_FD, &sd );
549
550         if ( sd == AC_SOCKET_INVALID ) {
551                 /* not connected yet */
552                 int rc;
553
554                 rc = ldap_open_defconn( ld );
555                 if( rc < 0 ) return ld->ld_errno;
556
557                 ber_sockbuf_ctrl( ld->ld_sb, LBER_SB_OPT_GET_FD, &sd );
558
559                 if( sd == AC_SOCKET_INVALID ) {
560                         ld->ld_errno = LDAP_LOCAL_ERROR;
561                         return ld->ld_errno;
562                 }
563         }   
564
565         ctx = ld->ld_defconn->lconn_sasl_ctx;
566
567         if( ctx == NULL ) {
568                 ld->ld_errno = LDAP_LOCAL_ERROR;
569                 return ld->ld_errno;
570         }
571
572         /* (re)set security properties */
573         sasl_setprop( ctx, SASL_SEC_PROPS,
574                 &ld->ld_options.ldo_sasl_secprops );
575
576         ccred.bv_val = NULL;
577         ccred.bv_len = 0;
578
579         do {
580                 saslrc = sasl_client_start( ctx,
581                         mechs,
582 #if SASL_VERSION_MAJOR < 2
583                         NULL,
584 #endif
585                         &prompts,
586                         (SASL_CONST char **)&ccred.bv_val,
587                         &credlen,
588                         &mech );
589
590                 if( pmech == NULL && mech != NULL ) {
591                         pmech = mech;
592
593                         if( flags != LDAP_SASL_QUIET ) {
594                                 fprintf(stderr,
595                                         "SASL/%s authentication started\n",
596                                         pmech );
597                         }
598                 }
599
600 #if SASL_VERSION_MAJOR >= 2
601                 /* XXX the application should free interact results. */
602                 if ( prompts != NULL && prompts->result != NULL ) {
603                         LDAP_FREE( (void *)prompts->result );
604                         prompts->result = NULL;
605                 }
606 #endif
607
608                 if( saslrc == SASL_INTERACT ) {
609                         int res;
610                         if( !interact ) break;
611                         res = (interact)( ld, flags, defaults, prompts );
612                         if( res != LDAP_SUCCESS ) {
613                                 break;
614                         }
615                 }
616         } while ( saslrc == SASL_INTERACT );
617
618         ccred.bv_len = credlen;
619
620         if ( (saslrc != SASL_OK) && (saslrc != SASL_CONTINUE) ) {
621                 ld->ld_errno = sasl_err2ldap( saslrc );
622                 return ld->ld_errno;
623         }
624
625         do {
626                 struct berval *scred;
627                 unsigned credlen;
628
629                 scred = NULL;
630
631                 rc = ldap_sasl_bind_s( ld, dn, mech, &ccred, sctrls, cctrls, &scred );
632
633                 if ( ccred.bv_val != NULL ) {
634 #if SASL_VERSION_MAJOR < 2
635                         LDAP_FREE( ccred.bv_val );
636 #endif
637                         ccred.bv_val = NULL;
638                 }
639
640                 if ( rc != LDAP_SUCCESS && rc != LDAP_SASL_BIND_IN_PROGRESS ) {
641                         if( scred && scred->bv_len ) {
642                                 /* and server provided us with data? */
643 #ifdef NEW_LOGGING
644                                 LDAP_LOG (( "cyrus", LDAP_LEVEL_DETAIL1, 
645                                         "ldap_int_sasl_bind: rc=%d sasl=%d len=%ld\n", 
646                                         rc, saslrc, scred->bv_len ));
647 #else
648                                 Debug( LDAP_DEBUG_TRACE,
649                                         "ldap_int_sasl_bind: rc=%d sasl=%d len=%ld\n",
650                                         rc, saslrc, scred->bv_len );
651 #endif
652                                 ber_bvfree( scred );
653                         }
654                         return ld->ld_errno;
655                 }
656
657                 if( rc == LDAP_SUCCESS && saslrc == SASL_OK ) {
658                         /* we're done, no need to step */
659                         if( scred && scred->bv_len ) {
660                                 /* but server provided us with data! */
661 #ifdef NEW_LOGGING
662                                 LDAP_LOG (( "cyrus", LDAP_LEVEL_DETAIL1, 
663                                         "ldap_int_sasl_bind: rc=%d sasl=%d len=%ld\n", 
664                                         rc, saslrc, scred->bv_len ));
665 #else
666                                 Debug( LDAP_DEBUG_TRACE,
667                                         "ldap_int_sasl_bind: rc=%d sasl=%d len=%ld\n",
668                                         rc, saslrc, scred->bv_len );
669 #endif
670                                 ber_bvfree( scred );
671                                 return ld->ld_errno = LDAP_LOCAL_ERROR;
672                         }
673                         break;
674                 }
675
676                 do {
677                         saslrc = sasl_client_step( ctx,
678                                 (scred == NULL) ? NULL : scred->bv_val,
679                                 (scred == NULL) ? 0 : scred->bv_len,
680                                 &prompts,
681                                 (SASL_CONST char **)&ccred.bv_val,
682                                 &credlen );
683
684 #ifdef NEW_LOGGING
685                                 LDAP_LOG (( "cyrus", LDAP_LEVEL_DETAIL1, 
686                                         "ldap_int_sasl_bind: sasl_client_step: %d\n", saslrc ));
687 #else
688                         Debug( LDAP_DEBUG_TRACE, "sasl_client_step: %d\n",
689                                 saslrc, 0, 0 );
690 #endif
691
692 #if SASL_VERSION_MAJOR >= 2
693                         /* XXX the application should free interact results. */
694                         if ( prompts != NULL && prompts->result != NULL ) {
695                                 LDAP_FREE( (void *)prompts->result );
696                                 prompts->result = NULL;
697                         }
698 #endif
699
700                         if( saslrc == SASL_INTERACT ) {
701                                 int res;
702                                 if( !interact ) break;
703                                 res = (interact)( ld, flags, defaults, prompts );
704                                 if( res != LDAP_SUCCESS ) {
705                                         break;
706                                 }
707                         }
708                 } while ( saslrc == SASL_INTERACT );
709
710                 ccred.bv_len = credlen;
711                 ber_bvfree( scred );
712
713                 if ( (saslrc != SASL_OK) && (saslrc != SASL_CONTINUE) ) {
714                         ld->ld_errno = sasl_err2ldap( saslrc );
715                         return ld->ld_errno;
716                 }
717         } while ( rc == LDAP_SASL_BIND_IN_PROGRESS );
718
719         if ( rc != LDAP_SUCCESS ) {
720                 return rc;
721         }
722
723         if ( saslrc != SASL_OK ) {
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 #else
783         memset( &extprops, '\0', sizeof(extprops) );
784         extprops.ssf = ssf;
785         extprops.auth_id = (char *) authid;
786
787         sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL,
788                 (void *) &extprops );
789 #endif
790
791         if ( sc != SASL_OK ) {
792                 return LDAP_LOCAL_ERROR;
793         }
794
795         return LDAP_SUCCESS;
796 }
797
798
799 int ldap_pvt_sasl_secprops(
800         const char *in,
801         sasl_security_properties_t *secprops )
802 {
803         int i;
804         char **props = ldap_str2charray( in, "," );
805         unsigned sflags = 0;
806         int got_sflags = 0;
807         sasl_ssf_t max_ssf = 0;
808         int got_max_ssf = 0;
809         sasl_ssf_t min_ssf = 0;
810         int got_min_ssf = 0;
811         unsigned maxbufsize = 0;
812         int got_maxbufsize = 0;
813
814         if( props == NULL || secprops == NULL ) {
815                 return LDAP_PARAM_ERROR;
816         }
817
818         for( i=0; props[i]; i++ ) {
819                 if( !strcasecmp(props[i], "none") ) {
820                         got_sflags++;
821
822                 } else if( !strcasecmp(props[i], "noplain") ) {
823                         got_sflags++;
824                         sflags |= SASL_SEC_NOPLAINTEXT;
825
826                 } else if( !strcasecmp(props[i], "noactive") ) {
827                         got_sflags++;
828                         sflags |= SASL_SEC_NOACTIVE;
829
830                 } else if( !strcasecmp(props[i], "nodict") ) {
831                         got_sflags++;
832                         sflags |= SASL_SEC_NODICTIONARY;
833
834                 } else if( !strcasecmp(props[i], "forwardsec") ) {
835                         got_sflags++;
836                         sflags |= SASL_SEC_FORWARD_SECRECY;
837
838                 } else if( !strcasecmp(props[i], "noanonymous")) {
839                         got_sflags++;
840                         sflags |= SASL_SEC_NOANONYMOUS;
841
842                 } else if( !strcasecmp(props[i], "passcred") ) {
843                         got_sflags++;
844                         sflags |= SASL_SEC_PASS_CREDENTIALS;
845
846                 } else if( !strncasecmp(props[i],
847                         "minssf=", sizeof("minssf")) )
848                 {
849                         if( isdigit( (unsigned char) props[i][sizeof("minssf")] ) ) {
850                                 got_min_ssf++;
851                                 min_ssf = atoi( &props[i][sizeof("minssf")] );
852                         } else {
853                                 return LDAP_NOT_SUPPORTED;
854                         }
855
856                 } else if( !strncasecmp(props[i],
857                         "maxssf=", sizeof("maxssf")) )
858                 {
859                         if( isdigit( (unsigned char) props[i][sizeof("maxssf")] ) ) {
860                                 got_max_ssf++;
861                                 max_ssf = atoi( &props[i][sizeof("maxssf")] );
862                         } else {
863                                 return LDAP_NOT_SUPPORTED;
864                         }
865
866                 } else if( !strncasecmp(props[i],
867                         "maxbufsize=", sizeof("maxbufsize")) )
868                 {
869                         if( isdigit( (unsigned char) props[i][sizeof("maxbufsize")] ) ) {
870                                 got_maxbufsize++;
871                                 maxbufsize = atoi( &props[i][sizeof("maxbufsize")] );
872                         } else {
873                                 return LDAP_NOT_SUPPORTED;
874                         }
875
876                         if( maxbufsize && (( maxbufsize < SASL_MIN_BUFF_SIZE )
877                                 || (maxbufsize > SASL_MAX_BUFF_SIZE )))
878                         {
879                                 /* bad maxbufsize */
880                                 return LDAP_PARAM_ERROR;
881                         }
882
883                 } else {
884                         return LDAP_NOT_SUPPORTED;
885                 }
886         }
887
888         if(got_sflags) {
889                 secprops->security_flags = sflags;
890         }
891         if(got_min_ssf) {
892                 secprops->min_ssf = min_ssf;
893         }
894         if(got_max_ssf) {
895                 secprops->max_ssf = max_ssf;
896         }
897         if(got_maxbufsize) {
898                 secprops->maxbufsize = maxbufsize;
899         }
900
901         ldap_charray_free( props );
902         return LDAP_SUCCESS;
903 }
904
905 int
906 ldap_int_sasl_config( struct ldapoptions *lo, int option, const char *arg )
907 {
908         int rc;
909
910         switch( option ) {
911         case LDAP_OPT_X_SASL_SECPROPS:
912                 rc = ldap_pvt_sasl_secprops( arg, &lo->ldo_sasl_secprops );
913                 if( rc == LDAP_SUCCESS ) return 0;
914         }
915
916         return -1;
917 }
918
919 int
920 ldap_int_sasl_get_option( LDAP *ld, int option, void *arg )
921 {
922         if ( ld == NULL )
923                 return -1;
924
925         switch ( option ) {
926                 case LDAP_OPT_X_SASL_MECH: {
927                         *(char **)arg = ld->ld_options.ldo_def_sasl_mech
928                                 ? LDAP_STRDUP( ld->ld_options.ldo_def_sasl_mech ) : NULL;
929                 } break;
930                 case LDAP_OPT_X_SASL_REALM: {
931                         *(char **)arg = ld->ld_options.ldo_def_sasl_realm
932                                 ? LDAP_STRDUP( ld->ld_options.ldo_def_sasl_realm ) : NULL;
933                 } break;
934                 case LDAP_OPT_X_SASL_AUTHCID: {
935                         *(char **)arg = ld->ld_options.ldo_def_sasl_authcid
936                                 ? LDAP_STRDUP( ld->ld_options.ldo_def_sasl_authcid ) : NULL;
937                 } break;
938                 case LDAP_OPT_X_SASL_AUTHZID: {
939                         *(char **)arg = ld->ld_options.ldo_def_sasl_authzid
940                                 ? LDAP_STRDUP( ld->ld_options.ldo_def_sasl_authzid ) : NULL;
941                 } break;
942
943                 case LDAP_OPT_X_SASL_SSF: {
944                         int sc;
945                         sasl_ssf_t      *ssf;
946                         sasl_conn_t *ctx;
947
948                         if( ld->ld_defconn == NULL ) {
949                                 return -1;
950                         }
951
952                         ctx = ld->ld_defconn->lconn_sasl_ctx;
953
954                         if ( ctx == NULL ) {
955                                 return -1;
956                         }
957
958                         sc = sasl_getprop( ctx, SASL_SSF,
959                                 (SASL_CONST void **) &ssf );
960
961                         if ( sc != SASL_OK ) {
962                                 return -1;
963                         }
964
965                         *(ber_len_t *)arg = *ssf;
966                 } break;
967
968                 case LDAP_OPT_X_SASL_SSF_EXTERNAL:
969                         /* this option is write only */
970                         return -1;
971
972                 case LDAP_OPT_X_SASL_SSF_MIN:
973                         *(ber_len_t *)arg = ld->ld_options.ldo_sasl_secprops.min_ssf;
974                         break;
975                 case LDAP_OPT_X_SASL_SSF_MAX:
976                         *(ber_len_t *)arg = ld->ld_options.ldo_sasl_secprops.max_ssf;
977                         break;
978                 case LDAP_OPT_X_SASL_MAXBUFSIZE:
979                         *(ber_len_t *)arg = ld->ld_options.ldo_sasl_secprops.maxbufsize;
980                         break;
981
982                 case LDAP_OPT_X_SASL_SECPROPS:
983                         /* this option is write only */
984                         return -1;
985
986                 default:
987                         return -1;
988         }
989         return 0;
990 }
991
992 int
993 ldap_int_sasl_set_option( LDAP *ld, int option, void *arg )
994 {
995         if ( ld == NULL )
996                 return -1;
997
998         switch ( option ) {
999         case LDAP_OPT_X_SASL_SSF:
1000                 /* This option is read-only */
1001                 return -1;
1002
1003         case LDAP_OPT_X_SASL_SSF_EXTERNAL: {
1004                 int sc;
1005 #if SASL_VERSION_MAJOR < 2
1006                 sasl_external_properties_t extprops;
1007 #endif
1008                 sasl_conn_t *ctx;
1009
1010                 if( ld->ld_defconn == NULL ) {
1011                         return -1;
1012                 }
1013
1014                 ctx = ld->ld_defconn->lconn_sasl_ctx;
1015
1016                 if ( ctx == NULL ) {
1017                         return -1;
1018                 }
1019
1020 #if SASL_VERSION_MAJOR >= 2
1021                 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL, arg);
1022 #else
1023                 memset(&extprops, 0L, sizeof(extprops));
1024
1025                 extprops.ssf = * (ber_len_t *) arg;
1026
1027                 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL,
1028                         (void *) &extprops );
1029 #endif
1030
1031                 if ( sc != SASL_OK ) {
1032                         return -1;
1033                 }
1034                 } break;
1035
1036         case LDAP_OPT_X_SASL_SSF_MIN:
1037                 ld->ld_options.ldo_sasl_secprops.min_ssf = *(ber_len_t *)arg;
1038                 break;
1039         case LDAP_OPT_X_SASL_SSF_MAX:
1040                 ld->ld_options.ldo_sasl_secprops.max_ssf = *(ber_len_t *)arg;
1041                 break;
1042         case LDAP_OPT_X_SASL_MAXBUFSIZE:
1043                 ld->ld_options.ldo_sasl_secprops.maxbufsize = *(ber_len_t *)arg;
1044                 break;
1045
1046         case LDAP_OPT_X_SASL_SECPROPS: {
1047                 int sc;
1048                 sc = ldap_pvt_sasl_secprops( (char *) arg,
1049                         &ld->ld_options.ldo_sasl_secprops );
1050
1051                 return sc == LDAP_SUCCESS ? 0 : -1;
1052                 }
1053
1054         default:
1055                 return -1;
1056         }
1057         return 0;
1058 }
1059
1060 #ifdef LDAP_R_COMPILE
1061 void *ldap_pvt_sasl_mutex_new(void)
1062 {
1063         ldap_pvt_thread_mutex_t *mutex;
1064
1065         mutex = (ldap_pvt_thread_mutex_t *) LDAP_MALLOC(
1066                 sizeof(ldap_pvt_thread_mutex_t) );
1067
1068         if ( ldap_pvt_thread_mutex_init( mutex ) == 0 ) {
1069                 return mutex;
1070         }
1071         return NULL;
1072 }
1073
1074 int ldap_pvt_sasl_mutex_lock(void *mutex)
1075 {
1076         return ldap_pvt_thread_mutex_lock( (ldap_pvt_thread_mutex_t *)mutex )
1077                 ? SASL_FAIL : SASL_OK;
1078 }
1079
1080 int ldap_pvt_sasl_mutex_unlock(void *mutex)
1081 {
1082         return ldap_pvt_thread_mutex_unlock( (ldap_pvt_thread_mutex_t *)mutex )
1083                 ? SASL_FAIL : SASL_OK;
1084 }
1085
1086 void ldap_pvt_sasl_mutex_dispose(void *mutex)
1087 {
1088         (void) ldap_pvt_thread_mutex_destroy( (ldap_pvt_thread_mutex_t *)mutex );
1089         LDAP_FREE( mutex );
1090 }
1091 #endif
1092
1093 #else
1094 int ldap_int_sasl_init( void )
1095 { return LDAP_SUCCESS; }
1096
1097 int ldap_int_sasl_close( LDAP *ld, LDAPConn *lc )
1098 { return LDAP_SUCCESS; }
1099
1100 int
1101 ldap_int_sasl_bind(
1102         LDAP                    *ld,
1103         const char              *dn,
1104         const char              *mechs,
1105         LDAPControl             **sctrls,
1106         LDAPControl             **cctrls,
1107         unsigned                flags,
1108         LDAP_SASL_INTERACT_PROC *interact,
1109         void * defaults )
1110 { return LDAP_NOT_SUPPORTED; }
1111
1112 int
1113 ldap_int_sasl_external(
1114         LDAP *ld,
1115         LDAPConn *conn,
1116         const char * authid,
1117         ber_len_t ssf )
1118 { return LDAP_SUCCESS; }
1119
1120 #endif /* HAVE_CYRUS_SASL */