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