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