2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2005 The OpenLDAP Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
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>.
20 #include <ac/socket.h>
21 #include <ac/stdlib.h>
22 #include <ac/string.h>
26 #include <ac/unistd.h>
34 #ifdef HAVE_CYRUS_SASL
41 #define INT_MAX 2147483647 /* 32 bit signed max */
45 ldap_pvt_thread_mutex_t ldap_int_sasl_mutex;
48 #ifdef HAVE_SASL_SASL_H
49 #include <sasl/sasl.h>
54 #if SASL_VERSION_MAJOR >= 2
55 #define SASL_CONST const
61 * Various Cyrus SASL related stuff.
64 static const sasl_callback_t client_callbacks[] = {
65 #ifdef SASL_CB_GETREALM
66 { SASL_CB_GETREALM, NULL, NULL },
68 { SASL_CB_USER, NULL, NULL },
69 { SASL_CB_AUTHNAME, NULL, NULL },
70 { SASL_CB_PASS, NULL, NULL },
71 { SASL_CB_ECHOPROMPT, NULL, NULL },
72 { SASL_CB_NOECHOPROMPT, NULL, NULL },
73 { SASL_CB_LIST_END, NULL, NULL }
76 int ldap_int_sasl_init( void )
78 /* XXX not threadsafe */
79 static int sasl_initialized = 0;
81 #ifdef HAVE_SASL_VERSION
82 /* stringify the version number, sasl.h doesn't do it for us */
83 #define VSTR0(maj, min, pat) #maj "." #min "." #pat
84 #define VSTR(maj, min, pat) VSTR0(maj, min, pat)
85 #define SASL_VERSION_STRING VSTR(SASL_VERSION_MAJOR, SASL_VERSION_MINOR, \
88 sasl_version( NULL, &rc );
89 if ( ((rc >> 16) != ((SASL_VERSION_MAJOR << 8)|SASL_VERSION_MINOR)) ||
90 (rc & 0xffff) < SASL_VERSION_STEP) {
91 char version[sizeof("xxx.xxx.xxxxx")];
92 sprintf( version, "%u.%d.%d", (unsigned)rc >> 24, (rc >> 16) & 0xff,
95 Debug( LDAP_DEBUG_ANY,
96 "ldap_int_sasl_init: SASL library version mismatch:"
97 " expected " SASL_VERSION_STRING ","
98 " got %s\n", version, 0, 0 );
103 if ( sasl_initialized ) {
107 /* SASL 2 takes care of its own memory completely internally */
108 #if SASL_VERSION_MAJOR < 2 && !defined(CSRIMALLOC)
114 #endif /* CSRIMALLOC */
116 #ifdef LDAP_R_COMPILE
118 ldap_pvt_sasl_mutex_new,
119 ldap_pvt_sasl_mutex_lock,
120 ldap_pvt_sasl_mutex_unlock,
121 ldap_pvt_sasl_mutex_dispose );
124 if ( sasl_client_init( NULL ) == SASL_OK ) {
125 sasl_initialized = 1;
129 #if SASL_VERSION_MAJOR < 2
130 /* A no-op to make sure we link with Cyrus 1.5 */
131 sasl_client_auth( NULL, NULL, NULL, 0, NULL, NULL );
137 * SASL encryption support for LBER Sockbufs
140 struct sb_sasl_data {
141 sasl_conn_t *sasl_context;
142 unsigned *sasl_maxbuf;
143 Sockbuf_Buf sec_buf_in;
149 sb_sasl_setup( Sockbuf_IO_Desc *sbiod, void *arg )
151 struct sb_sasl_data *p;
153 assert( sbiod != NULL );
155 p = LBER_MALLOC( sizeof( *p ) );
158 p->sasl_context = (sasl_conn_t *)arg;
159 ber_pvt_sb_buf_init( &p->sec_buf_in );
160 ber_pvt_sb_buf_init( &p->buf_in );
161 ber_pvt_sb_buf_init( &p->buf_out );
162 if ( ber_pvt_sb_grow_buffer( &p->sec_buf_in, SASL_MIN_BUFF_SIZE ) < 0 ) {
167 sasl_getprop( p->sasl_context, SASL_MAXOUTBUF,
168 (SASL_CONST void **) &p->sasl_maxbuf );
170 sbiod->sbiod_pvt = p;
176 sb_sasl_remove( Sockbuf_IO_Desc *sbiod )
178 struct sb_sasl_data *p;
180 assert( sbiod != NULL );
182 p = (struct sb_sasl_data *)sbiod->sbiod_pvt;
183 #if SASL_VERSION_MAJOR >= 2
185 * SASLv2 encode/decode buffers are managed by
186 * libsasl2. Ensure they are not freed by liblber.
188 p->buf_in.buf_base = NULL;
189 p->buf_out.buf_base = NULL;
191 ber_pvt_sb_buf_destroy( &p->sec_buf_in );
192 ber_pvt_sb_buf_destroy( &p->buf_in );
193 ber_pvt_sb_buf_destroy( &p->buf_out );
195 sbiod->sbiod_pvt = NULL;
200 sb_sasl_pkt_length( const unsigned char *buf, int debuglevel )
204 assert( buf != NULL );
211 if ( size > SASL_MAX_BUFF_SIZE ) {
212 /* somebody is trying to mess me up. */
213 ber_log_printf( LDAP_DEBUG_ANY, debuglevel,
214 "sb_sasl_pkt_length: received illegal packet length "
215 "of %lu bytes\n", (unsigned long)size );
216 size = 16; /* this should lead to an error. */
219 return size + 4; /* include the size !!! */
222 /* Drop a processed packet from the input buffer */
224 sb_sasl_drop_packet ( Sockbuf_Buf *sec_buf_in, int debuglevel )
228 len = sec_buf_in->buf_ptr - sec_buf_in->buf_end;
230 AC_MEMCPY( sec_buf_in->buf_base, sec_buf_in->buf_base +
231 sec_buf_in->buf_end, len );
234 sec_buf_in->buf_end = sb_sasl_pkt_length(
235 (unsigned char *) sec_buf_in->buf_base, debuglevel);
238 sec_buf_in->buf_end = 0;
240 sec_buf_in->buf_ptr = len;
244 sb_sasl_read( Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len)
246 struct sb_sasl_data *p;
247 ber_slen_t ret, bufptr;
249 assert( sbiod != NULL );
250 assert( SOCKBUF_VALID( sbiod->sbiod_sb ) );
252 p = (struct sb_sasl_data *)sbiod->sbiod_pvt;
254 /* Are there anything left in the buffer? */
255 ret = ber_pvt_sb_copy_out( &p->buf_in, buf, len );
262 #if SASL_VERSION_MAJOR >= 2
263 ber_pvt_sb_buf_init( &p->buf_in );
265 ber_pvt_sb_buf_destroy( &p->buf_in );
268 /* Read the length of the packet */
269 while ( p->sec_buf_in.buf_ptr < 4 ) {
270 ret = LBER_SBIOD_READ_NEXT( sbiod, p->sec_buf_in.buf_base +
271 p->sec_buf_in.buf_ptr,
272 4 - p->sec_buf_in.buf_ptr );
274 if ( ( ret < 0 ) && ( errno == EINTR ) )
278 return bufptr ? bufptr : ret;
280 p->sec_buf_in.buf_ptr += ret;
283 /* The new packet always starts at p->sec_buf_in.buf_base */
284 ret = sb_sasl_pkt_length( (unsigned char *) p->sec_buf_in.buf_base,
285 sbiod->sbiod_sb->sb_debug );
287 /* Grow the packet buffer if neccessary */
288 if ( ( p->sec_buf_in.buf_size < (ber_len_t) ret ) &&
289 ber_pvt_sb_grow_buffer( &p->sec_buf_in, ret ) < 0 )
294 p->sec_buf_in.buf_end = ret;
296 /* Did we read the whole encrypted packet? */
297 while ( p->sec_buf_in.buf_ptr < p->sec_buf_in.buf_end ) {
298 /* No, we have got only a part of it */
299 ret = p->sec_buf_in.buf_end - p->sec_buf_in.buf_ptr;
301 ret = LBER_SBIOD_READ_NEXT( sbiod, p->sec_buf_in.buf_base +
302 p->sec_buf_in.buf_ptr, ret );
304 if ( ( ret < 0 ) && ( errno == EINTR ) )
308 return bufptr ? bufptr : ret;
310 p->sec_buf_in.buf_ptr += ret;
313 /* Decode the packet */
315 unsigned tmpsize = p->buf_in.buf_end;
316 ret = sasl_decode( p->sasl_context, p->sec_buf_in.buf_base,
317 p->sec_buf_in.buf_end,
318 (SASL_CONST char **)&p->buf_in.buf_base,
319 (unsigned *)&tmpsize );
320 p->buf_in.buf_end = tmpsize;
323 /* Drop the packet from the input buffer */
324 sb_sasl_drop_packet( &p->sec_buf_in, sbiod->sbiod_sb->sb_debug );
326 if ( ret != SASL_OK ) {
327 ber_log_printf( LDAP_DEBUG_ANY, sbiod->sbiod_sb->sb_debug,
328 "sb_sasl_read: failed to decode packet: %s\n",
329 sasl_errstring( ret, NULL, NULL ) );
334 p->buf_in.buf_size = p->buf_in.buf_end;
336 bufptr += ber_pvt_sb_copy_out( &p->buf_in, (char*) buf + bufptr, len );
342 sb_sasl_write( Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len)
344 struct sb_sasl_data *p;
347 assert( sbiod != NULL );
348 assert( SOCKBUF_VALID( sbiod->sbiod_sb ) );
350 p = (struct sb_sasl_data *)sbiod->sbiod_pvt;
352 /* Are there anything left in the buffer? */
353 if ( p->buf_out.buf_ptr != p->buf_out.buf_end ) {
354 ret = ber_pvt_sb_do_write( sbiod, &p->buf_out );
355 if ( ret < 0 ) return ret;
357 /* Still have something left?? */
358 if ( p->buf_out.buf_ptr != p->buf_out.buf_end ) {
364 /* now encode the next packet. */
365 #if SASL_VERSION_MAJOR >= 2
366 ber_pvt_sb_buf_init( &p->buf_out );
368 ber_pvt_sb_buf_destroy( &p->buf_out );
370 if ( len > *p->sasl_maxbuf - 100 ) {
371 len = *p->sasl_maxbuf - 100; /* For safety margin */
375 unsigned tmpsize = p->buf_out.buf_size;
376 ret = sasl_encode( p->sasl_context, buf, len,
377 (SASL_CONST char **)&p->buf_out.buf_base,
379 p->buf_out.buf_size = tmpsize;
382 if ( ret != SASL_OK ) {
383 ber_log_printf( LDAP_DEBUG_ANY, sbiod->sbiod_sb->sb_debug,
384 "sb_sasl_write: failed to encode packet: %s\n",
385 sasl_errstring( ret, NULL, NULL ) );
389 p->buf_out.buf_end = p->buf_out.buf_size;
391 ret = ber_pvt_sb_do_write( sbiod, &p->buf_out );
393 /* return number of bytes encoded, not written, to ensure
394 * no byte is encoded twice (even if only sent once).
400 sb_sasl_ctrl( Sockbuf_IO_Desc *sbiod, int opt, void *arg )
402 struct sb_sasl_data *p;
404 p = (struct sb_sasl_data *)sbiod->sbiod_pvt;
406 if ( opt == LBER_SB_OPT_DATA_READY ) {
407 if ( p->buf_in.buf_ptr != p->buf_in.buf_end ) return 1;
410 return LBER_SBIOD_CTRL_NEXT( sbiod, opt, arg );
413 Sockbuf_IO ldap_pvt_sockbuf_io_sasl = {
414 sb_sasl_setup, /* sbi_setup */
415 sb_sasl_remove, /* sbi_remove */
416 sb_sasl_ctrl, /* sbi_ctrl */
417 sb_sasl_read, /* sbi_read */
418 sb_sasl_write, /* sbi_write */
422 int ldap_pvt_sasl_install( Sockbuf *sb, void *ctx_arg )
424 Debug( LDAP_DEBUG_TRACE, "ldap_pvt_sasl_install\n",
427 /* don't install the stuff unless security has been negotiated */
429 if ( !ber_sockbuf_ctrl( sb, LBER_SB_OPT_HAS_IO,
430 &ldap_pvt_sockbuf_io_sasl ) )
433 ber_sockbuf_add_io( sb, &ber_sockbuf_io_debug,
434 LBER_SBIOD_LEVEL_APPLICATION, (void *)"sasl_" );
436 ber_sockbuf_add_io( sb, &ldap_pvt_sockbuf_io_sasl,
437 LBER_SBIOD_LEVEL_APPLICATION, ctx_arg );
443 void ldap_pvt_sasl_remove( Sockbuf *sb )
445 ber_sockbuf_remove_io( sb, &ldap_pvt_sockbuf_io_sasl,
446 LBER_SBIOD_LEVEL_APPLICATION );
448 ber_sockbuf_remove_io( sb, &ber_sockbuf_io_debug,
449 LBER_SBIOD_LEVEL_APPLICATION );
454 sasl_err2ldap( int saslerr )
460 rc = LDAP_MORE_RESULTS_TO_RETURN;
463 rc = LDAP_LOCAL_ERROR;
469 rc = LDAP_LOCAL_ERROR;
475 rc = LDAP_AUTH_UNKNOWN;
478 rc = LDAP_AUTH_UNKNOWN;
481 rc = LDAP_PARAM_ERROR;
485 rc = LDAP_AUTH_UNKNOWN;
488 rc = LDAP_LOCAL_ERROR;
492 assert( rc == LDAP_SUCCESS || LDAP_API_ERROR( rc ) );
505 assert( lc->lconn_sasl_authctx == NULL );
507 if ( host == NULL ) {
508 ld->ld_errno = LDAP_LOCAL_ERROR;
512 if ( ldap_int_sasl_init() ) {
513 ld->ld_errno = LDAP_LOCAL_ERROR;
517 #if SASL_VERSION_MAJOR >= 2
518 rc = sasl_client_new( "ldap", host, NULL, NULL,
519 client_callbacks, 0, &ctx );
521 rc = sasl_client_new( "ldap", host, client_callbacks,
522 SASL_SECURITY_LAYER, &ctx );
525 if ( rc != SASL_OK ) {
526 ld->ld_errno = sasl_err2ldap( rc );
530 Debug( LDAP_DEBUG_TRACE, "ldap_int_sasl_open: host=%s\n",
533 lc->lconn_sasl_authctx = ctx;
538 int ldap_int_sasl_close( LDAP *ld, LDAPConn *lc )
540 sasl_conn_t *ctx = lc->lconn_sasl_authctx;
543 sasl_dispose( &ctx );
544 if ( lc->lconn_sasl_sockctx &&
545 lc->lconn_sasl_authctx != lc->lconn_sasl_sockctx ) {
546 ctx = lc->lconn_sasl_sockctx;
547 sasl_dispose( &ctx );
549 lc->lconn_sasl_sockctx = NULL;
550 lc->lconn_sasl_authctx = NULL;
561 LDAPControl **sctrls,
562 LDAPControl **cctrls,
564 LDAP_SASL_INTERACT_PROC *interact,
568 const char *mech = NULL;
569 const char *pmech = NULL;
571 sasl_ssf_t *ssf = NULL;
572 sasl_conn_t *ctx, *oldctx = NULL;
573 sasl_interact_t *prompts = NULL;
579 Debug( LDAP_DEBUG_TRACE, "ldap_int_sasl_bind: %s\n",
580 mechs ? mechs : "<null>", 0, 0 );
582 /* do a quick !LDAPv3 check... ldap_sasl_bind will do the rest. */
583 if (ld->ld_version < LDAP_VERSION3) {
584 ld->ld_errno = LDAP_NOT_SUPPORTED;
588 ber_sockbuf_ctrl( ld->ld_sb, LBER_SB_OPT_GET_FD, &sd );
590 if ( sd == AC_SOCKET_INVALID ) {
591 /* not connected yet */
594 rc = ldap_open_defconn( ld );
595 if( rc < 0 ) return ld->ld_errno;
597 ber_sockbuf_ctrl( ld->ld_defconn->lconn_sb,
598 LBER_SB_OPT_GET_FD, &sd );
600 if( sd == AC_SOCKET_INVALID ) {
601 ld->ld_errno = LDAP_LOCAL_ERROR;
606 oldctx = ld->ld_defconn->lconn_sasl_authctx;
608 /* If we already have an authentication context, clear it out */
610 if ( oldctx != ld->ld_defconn->lconn_sasl_sockctx ) {
611 sasl_dispose( &oldctx );
613 ld->ld_defconn->lconn_sasl_authctx = NULL;
617 char *saslhost = ldap_host_connected_to( ld->ld_defconn->lconn_sb,
619 rc = ldap_int_sasl_open( ld, ld->ld_defconn, saslhost );
620 LDAP_FREE( saslhost );
623 if ( rc != LDAP_SUCCESS ) return rc;
625 ctx = ld->ld_defconn->lconn_sasl_authctx;
628 ssl = ldap_pvt_tls_sb_ctx( ld->ld_defconn->lconn_sb );
630 struct berval authid = BER_BVNULL;
633 fac = ldap_pvt_tls_get_strength( ssl );
634 /* failure is OK, we just can't use SASL EXTERNAL */
635 (void) ldap_pvt_tls_get_my_dn( ssl, &authid, NULL, 0 );
637 (void) ldap_int_sasl_external( ld, ld->ld_defconn, authid.bv_val, fac );
638 LDAP_FREE( authid.bv_val );
642 /* Check for local */
643 if ( ldap_pvt_url_scheme2proto(
644 ld->ld_defconn->lconn_server->lud_scheme ) == LDAP_PROTO_IPC )
646 char authid[sizeof("gidNumber=4294967295+uidNumber=4294967295,"
647 "cn=peercred,cn=external,cn=auth")];
648 sprintf( authid, "gidNumber=%d+uidNumber=%d,"
649 "cn=peercred,cn=external,cn=auth",
650 (int) getegid(), (int) geteuid() );
651 (void) ldap_int_sasl_external( ld, ld->ld_defconn, authid,
652 LDAP_PVT_SASL_LOCAL_SSF );
656 /* (re)set security properties */
657 sasl_setprop( ctx, SASL_SEC_PROPS,
658 &ld->ld_options.ldo_sasl_secprops );
664 saslrc = sasl_client_start( ctx,
666 #if SASL_VERSION_MAJOR < 2
670 (SASL_CONST char **)&ccred.bv_val,
674 if( pmech == NULL && mech != NULL ) {
677 if( flags != LDAP_SASL_QUIET ) {
679 "SASL/%s authentication started\n",
684 if( saslrc == SASL_INTERACT ) {
686 if( !interact ) break;
687 res = (interact)( ld, flags, defaults, prompts );
689 if( res != LDAP_SUCCESS ) break;
691 } while ( saslrc == SASL_INTERACT );
693 ccred.bv_len = credlen;
695 if ( (saslrc != SASL_OK) && (saslrc != SASL_CONTINUE) ) {
696 rc = ld->ld_errno = sasl_err2ldap( saslrc );
697 #if SASL_VERSION_MAJOR >= 2
698 if ( ld->ld_error ) {
699 LDAP_FREE( ld->ld_error );
701 ld->ld_error = LDAP_STRDUP( sasl_errdetail( ctx ) );
707 struct berval *scred;
712 rc = ldap_sasl_bind_s( ld, dn, mech, &ccred, sctrls, cctrls,
715 if ( ccred.bv_val != NULL ) {
716 #if SASL_VERSION_MAJOR < 2
717 LDAP_FREE( ccred.bv_val );
722 if ( rc != LDAP_SUCCESS && rc != LDAP_SASL_BIND_IN_PROGRESS ) {
724 /* and server provided us with data? */
725 Debug( LDAP_DEBUG_TRACE,
726 "ldap_int_sasl_bind: rc=%d sasl=%d len=%ld\n",
727 rc, saslrc, scred ? scred->bv_len : -1 );
735 if( rc == LDAP_SUCCESS && saslrc == SASL_OK ) {
736 /* we're done, no need to step */
738 /* but we got additional data? */
739 Debug( LDAP_DEBUG_TRACE,
740 "ldap_int_sasl_bind: rc=%d sasl=%d len=%ld\n",
741 rc, saslrc, scred ? scred->bv_len : -1 );
744 rc = ld->ld_errno = LDAP_LOCAL_ERROR;
753 Debug( LDAP_DEBUG_TRACE,
754 "ldap_int_sasl_bind: no data in step!\n",
758 saslrc = sasl_client_step( ctx,
759 (scred == NULL) ? NULL : scred->bv_val,
760 (scred == NULL) ? 0 : scred->bv_len,
762 (SASL_CONST char **)&ccred.bv_val,
765 Debug( LDAP_DEBUG_TRACE, "sasl_client_step: %d\n",
768 if( saslrc == SASL_INTERACT ) {
770 if( !interact ) break;
771 res = (interact)( ld, flags, defaults, prompts );
772 if( res != LDAP_SUCCESS ) break;
774 } while ( saslrc == SASL_INTERACT );
776 ccred.bv_len = credlen;
779 if ( (saslrc != SASL_OK) && (saslrc != SASL_CONTINUE) ) {
780 ld->ld_errno = sasl_err2ldap( saslrc );
781 #if SASL_VERSION_MAJOR >= 2
782 if ( ld->ld_error ) {
783 LDAP_FREE( ld->ld_error );
785 ld->ld_error = LDAP_STRDUP( sasl_errdetail( ctx ) );
790 } while ( rc == LDAP_SASL_BIND_IN_PROGRESS );
792 if ( rc != LDAP_SUCCESS ) goto done;
794 if ( saslrc != SASL_OK ) {
795 #if SASL_VERSION_MAJOR >= 2
796 if ( ld->ld_error ) {
797 LDAP_FREE( ld->ld_error );
799 ld->ld_error = LDAP_STRDUP( sasl_errdetail( ctx ) );
801 rc = ld->ld_errno = sasl_err2ldap( saslrc );
805 if( flags != LDAP_SASL_QUIET ) {
806 saslrc = sasl_getprop( ctx, SASL_USERNAME,
807 (SASL_CONST void **) &data );
808 if( saslrc == SASL_OK && data && *data ) {
809 fprintf( stderr, "SASL username: %s\n", data );
812 #if SASL_VERSION_MAJOR < 2
813 saslrc = sasl_getprop( ctx, SASL_REALM,
814 (SASL_CONST void **) &data );
815 if( saslrc == SASL_OK && data && *data ) {
816 fprintf( stderr, "SASL realm: %s\n", data );
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 );
829 if( flags != LDAP_SASL_QUIET ) {
830 fprintf( stderr, "SASL installing layers\n" );
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_defconn->lconn_sb );
837 ldap_pvt_sasl_install( ld->ld_defconn->lconn_sb, ctx );
838 ld->ld_defconn->lconn_sasl_sockctx = ctx;
841 ld->ld_defconn->lconn_sasl_authctx = ctx;
848 ldap_int_sasl_external(
856 #if SASL_VERSION_MAJOR < 2
857 sasl_external_properties_t extprops;
860 ctx = conn->lconn_sasl_authctx;
863 return LDAP_LOCAL_ERROR;
866 #if SASL_VERSION_MAJOR >= 2
867 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL, &ssf );
869 sc = sasl_setprop( ctx, SASL_AUTH_EXTERNAL, authid );
871 memset( &extprops, '\0', sizeof(extprops) );
873 extprops.auth_id = (char *) authid;
875 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL,
876 (void *) &extprops );
879 if ( sc != SASL_OK ) {
880 return LDAP_LOCAL_ERROR;
897 { BER_BVC("none"), 0, 0, 0 },
898 { BER_BVC("nodict"), SASL_SEC_NODICTIONARY, 0, 0 },
899 { BER_BVC("noplain"), SASL_SEC_NOPLAINTEXT, 0, 0 },
900 { BER_BVC("noactive"), SASL_SEC_NOACTIVE, 0, 0 },
901 { BER_BVC("passcred"), SASL_SEC_PASS_CREDENTIALS, 0, 0 },
902 { BER_BVC("forwardsec"), SASL_SEC_FORWARD_SECRECY, 0, 0 },
903 { BER_BVC("noanonymous"), SASL_SEC_NOANONYMOUS, 0, 0 },
904 { BER_BVC("minssf="), 0, GOT_MINSSF, 0 },
905 { BER_BVC("maxssf="), 0, GOT_MAXSSF, INT_MAX },
906 { BER_BVC("maxbufsize="), 0, GOT_MAXBUF, 65536 },
907 { BER_BVNULL, 0, 0, 0 }
910 void ldap_pvt_sasl_secprops_unparse(
911 sasl_security_properties_t *secprops,
918 if ( secprops == NULL || out == NULL ) {
923 for ( i=0; !BER_BVISNULL( &sprops[i].key ); i++ ) {
924 if ( sprops[i].ival ) {
927 switch( sprops[i].ival ) {
928 case GOT_MINSSF: v = secprops->min_ssf; break;
929 case GOT_MAXSSF: v = secprops->max_ssf; break;
930 case GOT_MAXBUF: v = secprops->maxbufsize; break;
932 /* It is the default, ignore it */
933 if ( v == sprops[i].idef ) continue;
935 l += sprops[i].key.bv_len + 24;
936 } else if ( sprops[i].sflag ) {
937 if ( sprops[i].sflag & secprops->security_flags ) {
938 l += sprops[i].key.bv_len;
940 } else if ( secprops->security_flags == 0 ) {
941 l += sprops[i].key.bv_len;
948 out->bv_val = LDAP_MALLOC( l );
949 if ( out->bv_val == NULL ) {
956 for ( i=0; !BER_BVISNULL( &sprops[i].key ); i++ ) {
957 if ( sprops[i].ival ) {
960 switch( sprops[i].ival ) {
961 case GOT_MINSSF: v = secprops->min_ssf; break;
962 case GOT_MAXSSF: v = secprops->max_ssf; break;
963 case GOT_MAXBUF: v = secprops->maxbufsize; break;
965 /* It is the default, ignore it */
966 if ( v == sprops[i].idef ) continue;
968 if ( comma ) *ptr++ = ',';
969 ptr += sprintf(ptr, "%s%d", sprops[i].key.bv_val, v );
971 } else if ( sprops[i].sflag ) {
972 if ( sprops[i].sflag & secprops->security_flags ) {
973 if ( comma ) *ptr++ = ',';
974 ptr += sprintf(ptr, "%s", sprops[i].key.bv_val );
977 } else if ( secprops->security_flags == 0 ) {
978 if ( comma ) *ptr++ = ',';
979 ptr += sprintf(ptr, "%s", sprops[i].key.bv_val );
983 out->bv_len = ptr - out->bv_val;
986 int ldap_pvt_sasl_secprops(
988 sasl_security_properties_t *secprops )
991 char **props = ldap_str2charray( in, "," );
994 sasl_ssf_t max_ssf = 0;
996 sasl_ssf_t min_ssf = 0;
998 unsigned maxbufsize = 0;
999 int got_maxbufsize = 0;
1001 if( props == NULL || secprops == NULL ) {
1002 return LDAP_PARAM_ERROR;
1005 for( i=0; props[i]; i++ ) {
1006 l = strlen( props[i] );
1007 for ( j=0; !BER_BVISNULL( &sprops[j].key ); j++ ) {
1008 if ( l < sprops[j].key.bv_len ) continue;
1009 if ( strncasecmp( props[i], sprops[j].key.bv_val,
1010 sprops[j].key.bv_len )) continue;
1011 if ( sprops[j].ival ) {
1014 if ( !isdigit( props[i][sprops[j].key.bv_len] )) continue;
1015 v = strtoul( &props[i][sprops[j].key.bv_len], &next, 10 );
1016 if ( next == NULL || next[ 0 ] != '\0' ) continue;
1017 switch( sprops[j].ival ) {
1019 min_ssf = v; got_min_ssf++; break;
1021 max_ssf = v; got_max_ssf++; break;
1023 maxbufsize = v; got_maxbufsize++; break;
1026 if ( props[i][sprops[j].key.bv_len] ) continue;
1027 if ( sprops[j].sflag )
1028 sflags |= sprops[j].sflag;
1035 if ( BER_BVISNULL( &sprops[j].key )) {
1036 return LDAP_NOT_SUPPORTED;
1041 secprops->security_flags = sflags;
1044 secprops->min_ssf = min_ssf;
1047 secprops->max_ssf = max_ssf;
1049 if(got_maxbufsize) {
1050 secprops->maxbufsize = maxbufsize;
1053 ldap_charray_free( props );
1054 return LDAP_SUCCESS;
1058 ldap_int_sasl_config( struct ldapoptions *lo, int option, const char *arg )
1063 case LDAP_OPT_X_SASL_SECPROPS:
1064 rc = ldap_pvt_sasl_secprops( arg, &lo->ldo_sasl_secprops );
1065 if( rc == LDAP_SUCCESS ) return 0;
1072 ldap_int_sasl_get_option( LDAP *ld, int option, void *arg )
1078 case LDAP_OPT_X_SASL_MECH: {
1079 *(char **)arg = ld->ld_options.ldo_def_sasl_mech
1080 ? LDAP_STRDUP( ld->ld_options.ldo_def_sasl_mech ) : NULL;
1082 case LDAP_OPT_X_SASL_REALM: {
1083 *(char **)arg = ld->ld_options.ldo_def_sasl_realm
1084 ? LDAP_STRDUP( ld->ld_options.ldo_def_sasl_realm ) : NULL;
1086 case LDAP_OPT_X_SASL_AUTHCID: {
1087 *(char **)arg = ld->ld_options.ldo_def_sasl_authcid
1088 ? LDAP_STRDUP( ld->ld_options.ldo_def_sasl_authcid ) : NULL;
1090 case LDAP_OPT_X_SASL_AUTHZID: {
1091 *(char **)arg = ld->ld_options.ldo_def_sasl_authzid
1092 ? LDAP_STRDUP( ld->ld_options.ldo_def_sasl_authzid ) : NULL;
1095 case LDAP_OPT_X_SASL_SSF: {
1100 if( ld->ld_defconn == NULL ) {
1104 ctx = ld->ld_defconn->lconn_sasl_sockctx;
1106 if ( ctx == NULL ) {
1110 sc = sasl_getprop( ctx, SASL_SSF,
1111 (SASL_CONST void **) &ssf );
1113 if ( sc != SASL_OK ) {
1117 *(ber_len_t *)arg = *ssf;
1120 case LDAP_OPT_X_SASL_SSF_EXTERNAL:
1121 /* this option is write only */
1124 case LDAP_OPT_X_SASL_SSF_MIN:
1125 *(ber_len_t *)arg = ld->ld_options.ldo_sasl_secprops.min_ssf;
1127 case LDAP_OPT_X_SASL_SSF_MAX:
1128 *(ber_len_t *)arg = ld->ld_options.ldo_sasl_secprops.max_ssf;
1130 case LDAP_OPT_X_SASL_MAXBUFSIZE:
1131 *(ber_len_t *)arg = ld->ld_options.ldo_sasl_secprops.maxbufsize;
1134 case LDAP_OPT_X_SASL_SECPROPS:
1135 /* this option is write only */
1145 ldap_int_sasl_set_option( LDAP *ld, int option, void *arg )
1151 case LDAP_OPT_X_SASL_SSF:
1152 /* This option is read-only */
1155 case LDAP_OPT_X_SASL_SSF_EXTERNAL: {
1157 #if SASL_VERSION_MAJOR < 2
1158 sasl_external_properties_t extprops;
1162 if( ld->ld_defconn == NULL ) {
1166 ctx = ld->ld_defconn->lconn_sasl_authctx;
1168 if ( ctx == NULL ) {
1172 #if SASL_VERSION_MAJOR >= 2
1173 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL, arg);
1175 memset(&extprops, 0L, sizeof(extprops));
1177 extprops.ssf = * (ber_len_t *) arg;
1179 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL,
1180 (void *) &extprops );
1183 if ( sc != SASL_OK ) {
1188 case LDAP_OPT_X_SASL_SSF_MIN:
1189 ld->ld_options.ldo_sasl_secprops.min_ssf = *(ber_len_t *)arg;
1191 case LDAP_OPT_X_SASL_SSF_MAX:
1192 ld->ld_options.ldo_sasl_secprops.max_ssf = *(ber_len_t *)arg;
1194 case LDAP_OPT_X_SASL_MAXBUFSIZE:
1195 ld->ld_options.ldo_sasl_secprops.maxbufsize = *(ber_len_t *)arg;
1198 case LDAP_OPT_X_SASL_SECPROPS: {
1200 sc = ldap_pvt_sasl_secprops( (char *) arg,
1201 &ld->ld_options.ldo_sasl_secprops );
1203 return sc == LDAP_SUCCESS ? 0 : -1;
1212 #ifdef LDAP_R_COMPILE
1213 #define LDAP_DEBUG_R_SASL
1214 void *ldap_pvt_sasl_mutex_new(void)
1216 ldap_pvt_thread_mutex_t *mutex;
1218 mutex = (ldap_pvt_thread_mutex_t *) LDAP_MALLOC(
1219 sizeof(ldap_pvt_thread_mutex_t) );
1221 if ( ldap_pvt_thread_mutex_init( mutex ) == 0 ) {
1224 #ifndef LDAP_DEBUG_R_SASL
1226 #endif /* !LDAP_DEBUG_R_SASL */
1230 int ldap_pvt_sasl_mutex_lock(void *mutex)
1232 #ifdef LDAP_DEBUG_R_SASL
1233 if ( mutex == NULL ) {
1236 #else /* !LDAP_DEBUG_R_SASL */
1237 assert( mutex != NULL );
1238 #endif /* !LDAP_DEBUG_R_SASL */
1239 return ldap_pvt_thread_mutex_lock( (ldap_pvt_thread_mutex_t *)mutex )
1240 ? SASL_FAIL : SASL_OK;
1243 int ldap_pvt_sasl_mutex_unlock(void *mutex)
1245 #ifdef LDAP_DEBUG_R_SASL
1246 if ( mutex == NULL ) {
1249 #else /* !LDAP_DEBUG_R_SASL */
1250 assert( mutex != NULL );
1251 #endif /* !LDAP_DEBUG_R_SASL */
1252 return ldap_pvt_thread_mutex_unlock( (ldap_pvt_thread_mutex_t *)mutex )
1253 ? SASL_FAIL : SASL_OK;
1256 void ldap_pvt_sasl_mutex_dispose(void *mutex)
1258 #ifdef LDAP_DEBUG_R_SASL
1259 if ( mutex == NULL ) {
1262 #else /* !LDAP_DEBUG_R_SASL */
1263 assert( mutex != NULL );
1264 #endif /* !LDAP_DEBUG_R_SASL */
1265 (void) ldap_pvt_thread_mutex_destroy( (ldap_pvt_thread_mutex_t *)mutex );
1271 int ldap_int_sasl_init( void )
1272 { return LDAP_SUCCESS; }
1274 int ldap_int_sasl_close( LDAP *ld, LDAPConn *lc )
1275 { return LDAP_SUCCESS; }
1282 LDAPControl **sctrls,
1283 LDAPControl **cctrls,
1285 LDAP_SASL_INTERACT_PROC *interact,
1287 { return LDAP_NOT_SUPPORTED; }
1290 ldap_int_sasl_external(
1293 const char * authid,
1295 { return LDAP_SUCCESS; }
1297 #endif /* HAVE_CYRUS_SASL */