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