3 * Copyright 1999-2000 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
12 #include <ac/socket.h>
13 #include <ac/string.h>
19 #include "ldap_pvt_thread.h"
22 #ifdef HAVE_CYRUS_SASL
26 * Various Cyrus SASL related stuff.
29 #define SASL_MAX_BUFF_SIZE 65536
30 #define SASL_MIN_BUFF_SIZE 4096
32 int ldap_int_sasl_init( void )
34 /* XXX not threadsafe */
35 static int sasl_initialized = 0;
37 static sasl_callback_t client_callbacks[] = {
38 #ifdef SASL_CB_GETREALM
39 { SASL_CB_GETREALM, NULL, NULL },
41 { SASL_CB_USER, NULL, NULL },
42 { SASL_CB_AUTHNAME, NULL, NULL },
43 { SASL_CB_PASS, NULL, NULL },
44 { SASL_CB_ECHOPROMPT, NULL, NULL },
45 { SASL_CB_NOECHOPROMPT, NULL, NULL },
46 { SASL_CB_LIST_END, NULL, NULL }
49 if ( sasl_initialized ) {
59 #endif /* CSRIMALLOC */
63 ldap_pvt_sasl_mutex_new,
64 ldap_pvt_sasl_mutex_lock,
65 ldap_pvt_sasl_mutex_unlock,
66 ldap_pvt_sasl_mutex_dispose );
69 if ( sasl_client_init( client_callbacks ) == SASL_OK ) {
78 * SASL encryption support for LBER Sockbufs
82 sasl_conn_t *sasl_context;
83 Sockbuf_Buf sec_buf_in;
89 sb_sasl_setup( Sockbuf_IO_Desc *sbiod, void *arg )
91 struct sb_sasl_data *p;
93 assert( sbiod != NULL );
95 p = LBER_MALLOC( sizeof( *p ) );
98 p->sasl_context = (sasl_conn_t *)arg;
99 ber_pvt_sb_buf_init( &p->sec_buf_in );
100 ber_pvt_sb_buf_init( &p->buf_in );
101 ber_pvt_sb_buf_init( &p->buf_out );
102 if ( ber_pvt_sb_grow_buffer( &p->sec_buf_in, SASL_MIN_BUFF_SIZE ) < 0 ) {
107 sbiod->sbiod_pvt = p;
113 sb_sasl_remove( Sockbuf_IO_Desc *sbiod )
115 struct sb_sasl_data *p;
117 assert( sbiod != NULL );
119 p = (struct sb_sasl_data *)sbiod->sbiod_pvt;
120 ber_pvt_sb_buf_destroy( &p->sec_buf_in );
121 ber_pvt_sb_buf_destroy( &p->buf_in );
122 ber_pvt_sb_buf_destroy( &p->buf_out );
124 sbiod->sbiod_pvt = NULL;
129 sb_sasl_pkt_length( const char *buf, int debuglevel )
134 assert( buf != NULL );
136 tmp = *((long *)buf);
139 if ( size > SASL_MAX_BUFF_SIZE ) {
140 /* somebody is trying to mess me up. */
141 ber_log_printf( LDAP_DEBUG_ANY, debuglevel,
142 "sb_sasl_pkt_length: received illegal packet length "
143 "of %lu bytes\n", (unsigned long)size );
144 size = 16; /* this should lead to an error. */
147 return size + 4; /* include the size !!! */
150 /* Drop a processed packet from the input buffer */
152 sb_sasl_drop_packet ( Sockbuf_Buf *sec_buf_in, int debuglevel )
156 len = sec_buf_in->buf_ptr - sec_buf_in->buf_end;
158 memmove( sec_buf_in->buf_base, sec_buf_in->buf_base +
159 sec_buf_in->buf_end, len );
162 sec_buf_in->buf_end = sb_sasl_pkt_length( sec_buf_in->buf_base,
166 sec_buf_in->buf_end = 0;
168 sec_buf_in->buf_ptr = len;
172 sb_sasl_read( Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len)
174 struct sb_sasl_data *p;
175 ber_slen_t ret, bufptr;
177 assert( sbiod != NULL );
178 assert( SOCKBUF_VALID( sbiod->sbiod_sb ) );
180 p = (struct sb_sasl_data *)sbiod->sbiod_pvt;
182 /* Are there anything left in the buffer? */
183 ret = ber_pvt_sb_copy_out( &p->buf_in, buf, len );
190 ber_pvt_sb_buf_destroy( &p->buf_in );
192 /* Read the length of the packet */
193 while ( p->sec_buf_in.buf_ptr < 4 ) {
194 ret = LBER_SBIOD_READ_NEXT( sbiod, p->sec_buf_in.buf_base,
195 4 - p->sec_buf_in.buf_ptr );
197 if ( ( ret < 0 ) && ( errno == EINTR ) )
203 p->sec_buf_in.buf_ptr += ret;
206 /* The new packet always starts at p->sec_buf_in.buf_base */
207 ret = sb_sasl_pkt_length( p->sec_buf_in.buf_base,
208 sbiod->sbiod_sb->sb_debug );
210 /* Grow the packet buffer if neccessary */
211 if ( ( p->sec_buf_in.buf_size < ret ) &&
212 ber_pvt_sb_grow_buffer( &p->sec_buf_in, ret ) < 0 ) {
216 p->sec_buf_in.buf_end = ret;
218 /* Did we read the whole encrypted packet? */
219 while ( p->sec_buf_in.buf_ptr < p->sec_buf_in.buf_end ) {
220 /* No, we have got only a part of it */
221 ret = p->sec_buf_in.buf_end - p->sec_buf_in.buf_ptr;
223 ret = LBER_SBIOD_READ_NEXT( sbiod, p->sec_buf_in.buf_base +
224 p->sec_buf_in.buf_ptr, ret );
226 if ( ( ret < 0 ) && ( errno == EINTR ) )
232 p->sec_buf_in.buf_ptr += ret;
235 /* Decode the packet */
236 ret = sasl_decode( p->sasl_context, p->sec_buf_in.buf_base,
237 p->sec_buf_in.buf_end, &p->buf_in.buf_base,
238 (unsigned *)&p->buf_in.buf_end );
239 if ( ret != SASL_OK ) {
240 ber_log_printf( LDAP_DEBUG_ANY, sbiod->sbiod_sb->sb_debug,
241 "sb_sasl_read: failed to decode packet: %s\n",
242 sasl_errstring( ret, NULL, NULL ) );
243 sb_sasl_drop_packet( &p->sec_buf_in,
244 sbiod->sbiod_sb->sb_debug );
249 /* Drop the packet from the input buffer */
250 sb_sasl_drop_packet( &p->sec_buf_in, sbiod->sbiod_sb->sb_debug );
252 p->buf_in.buf_size = p->buf_in.buf_end;
254 bufptr += ber_pvt_sb_copy_out( &p->buf_in, (char*) buf + bufptr, len );
260 sb_sasl_write( Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len)
262 struct sb_sasl_data *p;
265 assert( sbiod != NULL );
266 assert( SOCKBUF_VALID( sbiod->sbiod_sb ) );
268 p = (struct sb_sasl_data *)sbiod->sbiod_pvt;
270 /* Are there anything left in the buffer? */
271 if ( p->buf_out.buf_ptr != p->buf_out.buf_end ) {
272 ret = ber_pvt_sb_do_write( sbiod, &p->buf_out );
277 /* now encode the next packet. */
278 ber_pvt_sb_buf_destroy( &p->buf_out );
279 ret = sasl_encode( p->sasl_context, buf, len, &p->buf_out.buf_base,
280 (unsigned *)&p->buf_out.buf_size );
281 if ( ret != SASL_OK ) {
282 ber_log_printf( LDAP_DEBUG_ANY, sbiod->sbiod_sb->sb_debug,
283 "sb_sasl_write: failed to encode packet: %s\n",
284 sasl_errstring( ret, NULL, NULL ) );
287 p->buf_out.buf_end = p->buf_out.buf_size;
289 ret = ber_pvt_sb_do_write( sbiod, &p->buf_out );
296 sb_sasl_ctrl( Sockbuf_IO_Desc *sbiod, int opt, void *arg )
298 struct sb_sasl_data *p;
300 p = (struct sb_sasl_data *)sbiod->sbiod_pvt;
302 if ( opt == LBER_SB_OPT_DATA_READY ) {
303 if ( p->buf_in.buf_ptr != p->buf_in.buf_end )
307 return LBER_SBIOD_CTRL_NEXT( sbiod, opt, arg );
310 Sockbuf_IO ldap_pvt_sockbuf_io_sasl = {
311 sb_sasl_setup, /* sbi_setup */
312 sb_sasl_remove, /* sbi_remove */
313 sb_sasl_ctrl, /* sbi_ctrl */
314 sb_sasl_read, /* sbi_read */
315 sb_sasl_write, /* sbi_write */
319 int ldap_pvt_sasl_install( Sockbuf *sb, void *ctx_arg )
321 Debug( LDAP_DEBUG_TRACE, "ldap_pvt_sasl_install\n",
324 /* don't install the stuff unless security has been negotiated */
326 if ( !ber_sockbuf_ctrl( sb, LBER_SB_OPT_HAS_IO,
327 &ldap_pvt_sockbuf_io_sasl ) )
328 ber_sockbuf_add_io( sb, &ldap_pvt_sockbuf_io_sasl,
329 LBER_SBIOD_LEVEL_APPLICATION, ctx_arg );
335 sasl_err2ldap( int saslerr )
341 rc = LDAP_MORE_RESULTS_TO_RETURN;
344 rc = LDAP_LOCAL_ERROR;
350 rc = LDAP_LOCAL_ERROR;
356 rc = LDAP_AUTH_UNKNOWN;
359 rc = LDAP_AUTH_UNKNOWN;
362 rc = LDAP_PARAM_ERROR;
366 rc = LDAP_AUTH_UNKNOWN;
369 rc = LDAP_LOCAL_ERROR;
373 assert( rc == LDAP_SUCCESS || LDAP_API_ERROR( rc ) );
387 sasl_callback_t *session_callbacks =
388 ber_memcalloc( 2, sizeof( sasl_callback_t ) );
390 if( session_callbacks == NULL ) return LDAP_NO_MEMORY;
392 session_callbacks[0].id = SASL_CB_USER;
393 session_callbacks[0].proc = NULL;
394 session_callbacks[0].context = ld;
396 session_callbacks[1].id = SASL_CB_LIST_END;
397 session_callbacks[1].proc = NULL;
398 session_callbacks[1].context = NULL;
400 assert( lc->lconn_sasl_ctx == NULL );
402 if ( host == NULL ) {
403 ld->ld_errno = LDAP_UNAVAILABLE;
407 rc = sasl_client_new( "ldap", host,
409 #ifdef LDAP_SASL_SECURITY_LAYER
416 if ( rc != SASL_OK ) {
417 ld->ld_errno = sasl_err2ldap( rc );
421 Debug( LDAP_DEBUG_TRACE, "ldap_int_sasl_open: %s\n",
424 lc->lconn_sasl_ctx = ctx;
427 sasl_external_properties_t extprops;
428 memset(&extprops, 0L, sizeof(extprops));
431 (void) sasl_setprop( ctx, SASL_SSF_EXTERNAL,
432 (void *) &extprops );
434 Debug( LDAP_DEBUG_TRACE, "ldap_int_sasl_open: ssf=%ld\n",
441 int ldap_int_sasl_close( LDAP *ld, LDAPConn *lc )
443 sasl_conn_t *ctx = lc->lconn_sasl_ctx;
444 assert( ctx != NULL );
447 sasl_dispose( &ctx );
448 lc->lconn_sasl_ctx = NULL;
459 LDAPControl **sctrls,
460 LDAPControl **cctrls,
462 LDAP_SASL_INTERACT_PROC *interact,
466 const char *mech = NULL;
467 const char *pmech = NULL;
469 sasl_ssf_t *ssf = NULL;
471 sasl_interact_t *prompts = NULL;
473 struct berval ccred, *scred;
476 Debug( LDAP_DEBUG_TRACE, "ldap_int_sasl_bind: %s\n",
477 mechs ? mechs : "<null>", 0, 0 );
479 /* do a quick !LDAPv3 check... ldap_sasl_bind will do the rest. */
480 if (ld->ld_version < LDAP_VERSION3) {
481 ld->ld_errno = LDAP_NOT_SUPPORTED;
485 ber_sockbuf_ctrl( ld->ld_sb, LBER_SB_OPT_GET_FD, &sd );
487 if ( sd == AC_SOCKET_INVALID ) {
488 /* not connected yet */
489 int rc = ldap_open_defconn( ld );
491 if( rc < 0 ) return ld->ld_errno;
492 ber_sockbuf_ctrl( ld->ld_sb, LBER_SB_OPT_GET_FD, &sd );
494 if( sd == AC_SOCKET_INVALID ) {
495 ld->ld_errno = LDAP_UNAVAILABLE;
500 ctx = ld->ld_defconn->lconn_sasl_ctx;
503 ld->ld_errno = LDAP_UNAVAILABLE;
507 /* (re)set security properties */
508 sasl_setprop( ctx, SASL_SEC_PROPS,
509 &ld->ld_options.ldo_sasl_secprops );
515 saslrc = sasl_client_start( ctx,
523 if( pmech == NULL && mech != NULL ) {
526 if( flags != LDAP_SASL_QUIET ) {
528 "SASL/%s authentication started\n",
533 if( saslrc == SASL_INTERACT ) {
535 if( !interact ) break;
536 res = (interact)( ld, flags, defaults, prompts );
537 if( res != LDAP_SUCCESS ) {
541 } while ( saslrc == SASL_INTERACT );
543 ccred.bv_len = credlen;
545 if ( (saslrc != SASL_OK) && (saslrc != SASL_CONTINUE) ) {
546 ld->ld_errno = sasl_err2ldap( saslrc );
555 rc = ldap_sasl_bind_s( ld, dn, mech, &ccred, sctrls, cctrls, &scred );
557 if ( rc == LDAP_SUCCESS ) {
559 } else if ( rc != LDAP_SASL_BIND_IN_PROGRESS ) {
560 if ( ccred.bv_val != NULL ) {
561 LDAP_FREE( ccred.bv_val );
566 if ( ccred.bv_val != NULL ) {
567 LDAP_FREE( ccred.bv_val );
572 saslrc = sasl_client_step( ctx,
573 (scred == NULL) ? NULL : scred->bv_val,
574 (scred == NULL) ? 0 : scred->bv_len,
579 Debug( LDAP_DEBUG_TRACE, "sasl_client_start: %d\n",
582 if( saslrc == SASL_INTERACT ) {
584 if( !interact ) break;
585 res = (interact)( ld, flags, defaults, prompts );
586 if( res != LDAP_SUCCESS ) {
590 } while ( saslrc == SASL_INTERACT );
592 ccred.bv_len = credlen;
595 if ( (saslrc != SASL_OK) && (saslrc != SASL_CONTINUE) ) {
596 ld->ld_errno = sasl_err2ldap( saslrc );
599 } while ( rc == LDAP_SASL_BIND_IN_PROGRESS );
601 assert ( rc == LDAP_SUCCESS );
603 /* likely should add a quiet option */
605 if( flags != LDAP_SASL_QUIET ) {
606 saslrc = sasl_getprop( ctx, SASL_USERNAME, (void **) &data );
607 if( saslrc == SASL_OK && data && *data ) {
608 fprintf( stderr, "SASL username: %s\n", data );
611 saslrc = sasl_getprop( ctx, SASL_REALM, (void **) &data );
612 if( saslrc == SASL_OK && data && *data ) {
613 fprintf( stderr, "SASL realm: %s\n", data );
617 saslrc = sasl_getprop( ctx, SASL_SSF, (void **) &ssf );
618 if( saslrc == SASL_OK ) {
619 if( flags != LDAP_SASL_QUIET ) {
620 fprintf( stderr, "SASL SSF: %lu\n",
621 (unsigned long) *ssf );
624 #ifdef LDAP_SASL_SECURITY_LAYER
626 if( flags != LDAP_SASL_QUIET ) {
627 fprintf( stderr, "SASL installing layers\n" );
629 ldap_pvt_sasl_install( ld->ld_sb, ctx );
637 int ldap_pvt_sasl_secprops(
639 sasl_security_properties_t *secprops )
642 char **props = ldap_str2charray( in, "," );
650 int got_maxbufsize = 0;
652 if( props == NULL || secprops == NULL ) {
653 return LDAP_PARAM_ERROR;
656 for( i=0; props[i]; i++ ) {
657 if( !strcasecmp(props[i], "none") ) {
660 } else if( !strcasecmp(props[i], "noplain") ) {
662 sflags |= SASL_SEC_NOPLAINTEXT;
664 } else if( !strcasecmp(props[i], "noactive") ) {
666 sflags |= SASL_SEC_NOACTIVE;
668 } else if( !strcasecmp(props[i], "nodict") ) {
670 sflags |= SASL_SEC_NODICTIONARY;
672 } else if( !strcasecmp(props[i], "forwardsec") ) {
674 sflags |= SASL_SEC_FORWARD_SECRECY;
676 } else if( !strcasecmp(props[i], "noanonymous")) {
678 sflags |= SASL_SEC_NOANONYMOUS;
680 } else if( !strcasecmp(props[i], "passcred") ) {
682 sflags |= SASL_SEC_PASS_CREDENTIALS;
684 } else if( !strncasecmp(props[i],
685 "minssf=", sizeof("minssf")) )
687 if( isdigit( props[i][sizeof("minssf")] ) ) {
689 min_ssf = atoi( &props[i][sizeof("minssf")] );
691 return LDAP_NOT_SUPPORTED;
694 } else if( !strncasecmp(props[i],
695 "maxssf=", sizeof("maxssf")) )
697 if( isdigit( props[i][sizeof("maxssf")] ) ) {
699 max_ssf = atoi( &props[i][sizeof("maxssf")] );
701 return LDAP_NOT_SUPPORTED;
704 } else if( !strncasecmp(props[i],
705 "maxbufsize=", sizeof("maxbufsize")) )
707 if( isdigit( props[i][sizeof("maxbufsize")] ) ) {
709 maxbufsize = atoi( &props[i][sizeof("maxbufsize")] );
711 return LDAP_NOT_SUPPORTED;
715 return LDAP_NOT_SUPPORTED;
720 secprops->security_flags = sflags;
723 secprops->min_ssf = min_ssf;
726 secprops->max_ssf = max_ssf;
729 secprops->maxbufsize = maxbufsize;
732 ldap_charray_free( props );
737 ldap_int_sasl_config( struct ldapoptions *lo, int option, const char *arg )
742 case LDAP_OPT_X_SASL_SECPROPS:
743 rc = ldap_pvt_sasl_secprops( arg, &lo->ldo_sasl_secprops );
744 if( rc == LDAP_SUCCESS ) return 0;
751 ldap_int_sasl_get_option( LDAP *ld, int option, void *arg )
757 case LDAP_OPT_X_SASL_MECH: {
758 *(char **)arg = ld->ld_options.ldo_def_sasl_mech
759 ? LDAP_STRDUP( ld->ld_options.ldo_def_sasl_mech ) : NULL;
761 case LDAP_OPT_X_SASL_REALM: {
762 *(char **)arg = ld->ld_options.ldo_def_sasl_realm
763 ? LDAP_STRDUP( ld->ld_options.ldo_def_sasl_realm ) : NULL;
765 case LDAP_OPT_X_SASL_AUTHCID: {
766 *(char **)arg = ld->ld_options.ldo_def_sasl_authcid
767 ? LDAP_STRDUP( ld->ld_options.ldo_def_sasl_authcid ) : NULL;
769 case LDAP_OPT_X_SASL_AUTHZID: {
770 *(char **)arg = ld->ld_options.ldo_def_sasl_authzid
771 ? LDAP_STRDUP( ld->ld_options.ldo_def_sasl_authzid ) : NULL;
774 case LDAP_OPT_X_SASL_SSF: {
779 if( ld->ld_defconn == NULL ) {
783 ctx = ld->ld_defconn->lconn_sasl_ctx;
789 sc = sasl_getprop( ctx, SASL_SSF,
792 if ( sc != SASL_OK ) {
796 *(ber_len_t *)arg = *ssf;
799 case LDAP_OPT_X_SASL_SSF_EXTERNAL:
800 /* this option is write only */
803 case LDAP_OPT_X_SASL_SSF_MIN:
804 *(ber_len_t *)arg = ld->ld_options.ldo_sasl_secprops.min_ssf;
806 case LDAP_OPT_X_SASL_SSF_MAX:
807 *(ber_len_t *)arg = ld->ld_options.ldo_sasl_secprops.max_ssf;
809 case LDAP_OPT_X_SASL_MAXBUFSIZE:
810 *(ber_len_t *)arg = ld->ld_options.ldo_sasl_secprops.maxbufsize;
813 case LDAP_OPT_X_SASL_SECPROPS:
814 /* this option is write only */
824 ldap_int_sasl_set_option( LDAP *ld, int option, void *arg )
830 case LDAP_OPT_X_SASL_SSF:
831 /* This option is read-only */
834 case LDAP_OPT_X_SASL_SSF_EXTERNAL: {
836 sasl_external_properties_t extprops;
839 if( ld->ld_defconn == NULL ) {
843 ctx = ld->ld_defconn->lconn_sasl_ctx;
849 memset(&extprops, 0L, sizeof(extprops));
851 extprops.ssf = * (ber_len_t *) arg;
853 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL,
854 (void *) &extprops );
856 if ( sc != SASL_OK ) {
861 case LDAP_OPT_X_SASL_SSF_MIN:
862 ld->ld_options.ldo_sasl_secprops.min_ssf = *(ber_len_t *)arg;
864 case LDAP_OPT_X_SASL_SSF_MAX:
865 ld->ld_options.ldo_sasl_secprops.max_ssf = *(ber_len_t *)arg;
867 case LDAP_OPT_X_SASL_MAXBUFSIZE:
868 ld->ld_options.ldo_sasl_secprops.maxbufsize = *(ber_len_t *)arg;
871 case LDAP_OPT_X_SASL_SECPROPS: {
873 sc = ldap_pvt_sasl_secprops( (char *) arg,
874 &ld->ld_options.ldo_sasl_secprops );
876 return sc == LDAP_SUCCESS ? 0 : -1;
885 #ifdef LDAP_R_COMPILE
886 void *ldap_pvt_sasl_mutex_new(void)
888 ldap_pvt_thread_mutex_t *mutex;
890 mutex = (ldap_pvt_thread_mutex_t *) LDAP_MALLOC(
891 sizeof(ldap_pvt_thread_mutex_t) );
893 if ( ldap_pvt_thread_mutex_init( mutex ) == 0 ) {
899 int ldap_pvt_sasl_mutex_lock(void *mutex)
901 return ldap_pvt_thread_mutex_lock( (ldap_pvt_thread_mutex_t *)mutex )
902 ? SASL_FAIL : SASL_OK;
905 int ldap_pvt_sasl_mutex_unlock(void *mutex)
907 return ldap_pvt_thread_mutex_unlock( (ldap_pvt_thread_mutex_t *)mutex )
908 ? SASL_FAIL : SASL_OK;
911 void ldap_pvt_sasl_mutex_dispose(void *mutex)
913 (void) ldap_pvt_thread_mutex_destroy( (ldap_pvt_thread_mutex_t *)mutex );
919 int ldap_int_sasl_init( void )
920 { return LDAP_SUCCESS; }
922 int ldap_int_sasl_close( LDAP *ld, LDAPConn *lc )
923 { return LDAP_SUCCESS; }
930 LDAPControl **sctrls,
931 LDAPControl **cctrls,
933 LDAP_SASL_INTERACT_PROC *interact,
935 { return LDAP_NOT_SUPPORTED; }
936 #endif /* HAVE_CYRUS_SASL */