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