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>
20 #include "ldap_pvt_thread.h"
23 #ifdef HAVE_CYRUS_SASL
27 * Various Cyrus SASL related stuff.
30 #define SASL_MAX_BUFF_SIZE 65536
31 #define SASL_MIN_BUFF_SIZE 4096
33 int ldap_int_sasl_init( void )
35 /* XXX not threadsafe */
36 static int sasl_initialized = 0;
38 static sasl_callback_t client_callbacks[] = {
39 #ifdef SASL_CB_GETREALM
40 { SASL_CB_GETREALM, NULL, NULL },
42 { SASL_CB_USER, NULL, NULL },
43 { SASL_CB_AUTHNAME, NULL, NULL },
44 { SASL_CB_PASS, NULL, NULL },
45 { SASL_CB_ECHOPROMPT, NULL, NULL },
46 { SASL_CB_NOECHOPROMPT, NULL, NULL },
47 { SASL_CB_LIST_END, NULL, NULL }
50 if ( sasl_initialized ) {
60 #endif /* CSRIMALLOC */
64 ldap_pvt_sasl_mutex_new,
65 ldap_pvt_sasl_mutex_lock,
66 ldap_pvt_sasl_mutex_unlock,
67 ldap_pvt_sasl_mutex_dispose );
70 if ( sasl_client_init( client_callbacks ) == SASL_OK ) {
79 * SASL encryption support for LBER Sockbufs
83 sasl_conn_t *sasl_context;
84 Sockbuf_Buf sec_buf_in;
90 sb_sasl_setup( Sockbuf_IO_Desc *sbiod, void *arg )
92 struct sb_sasl_data *p;
94 assert( sbiod != NULL );
96 p = LBER_MALLOC( sizeof( *p ) );
99 p->sasl_context = (sasl_conn_t *)arg;
100 ber_pvt_sb_buf_init( &p->sec_buf_in );
101 ber_pvt_sb_buf_init( &p->buf_in );
102 ber_pvt_sb_buf_init( &p->buf_out );
103 if ( ber_pvt_sb_grow_buffer( &p->sec_buf_in, SASL_MIN_BUFF_SIZE ) < 0 ) {
108 sbiod->sbiod_pvt = p;
114 sb_sasl_remove( Sockbuf_IO_Desc *sbiod )
116 struct sb_sasl_data *p;
118 assert( sbiod != NULL );
120 p = (struct sb_sasl_data *)sbiod->sbiod_pvt;
121 ber_pvt_sb_buf_destroy( &p->sec_buf_in );
122 ber_pvt_sb_buf_destroy( &p->buf_in );
123 ber_pvt_sb_buf_destroy( &p->buf_out );
125 sbiod->sbiod_pvt = NULL;
130 sb_sasl_pkt_length( const char *buf, int debuglevel )
135 assert( buf != NULL );
137 tmp = *((long *)buf);
140 if ( size > SASL_MAX_BUFF_SIZE ) {
141 /* somebody is trying to mess me up. */
142 ber_log_printf( LDAP_DEBUG_ANY, debuglevel,
143 "sb_sasl_pkt_length: received illegal packet length "
144 "of %lu bytes\n", (unsigned long)size );
145 size = 16; /* this should lead to an error. */
148 return size + 4; /* include the size !!! */
151 /* Drop a processed packet from the input buffer */
153 sb_sasl_drop_packet ( Sockbuf_Buf *sec_buf_in, int debuglevel )
157 len = sec_buf_in->buf_ptr - sec_buf_in->buf_end;
159 memmove( sec_buf_in->buf_base, sec_buf_in->buf_base +
160 sec_buf_in->buf_end, len );
163 sec_buf_in->buf_end = sb_sasl_pkt_length( sec_buf_in->buf_base,
167 sec_buf_in->buf_end = 0;
169 sec_buf_in->buf_ptr = len;
173 sb_sasl_read( Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len)
175 struct sb_sasl_data *p;
176 ber_slen_t ret, bufptr;
178 assert( sbiod != NULL );
179 assert( SOCKBUF_VALID( sbiod->sbiod_sb ) );
181 p = (struct sb_sasl_data *)sbiod->sbiod_pvt;
183 /* Are there anything left in the buffer? */
184 ret = ber_pvt_sb_copy_out( &p->buf_in, buf, len );
191 ber_pvt_sb_buf_destroy( &p->buf_in );
193 /* Read the length of the packet */
194 while ( p->sec_buf_in.buf_ptr < 4 ) {
195 ret = LBER_SBIOD_READ_NEXT( sbiod, p->sec_buf_in.buf_base,
196 4 - p->sec_buf_in.buf_ptr );
198 if ( ( ret < 0 ) && ( errno == EINTR ) )
204 p->sec_buf_in.buf_ptr += ret;
207 /* The new packet always starts at p->sec_buf_in.buf_base */
208 ret = sb_sasl_pkt_length( p->sec_buf_in.buf_base,
209 sbiod->sbiod_sb->sb_debug );
211 /* Grow the packet buffer if neccessary */
212 if ( ( p->sec_buf_in.buf_size < ret ) &&
213 ber_pvt_sb_grow_buffer( &p->sec_buf_in, ret ) < 0 )
218 p->sec_buf_in.buf_end = ret;
220 /* Did we read the whole encrypted packet? */
221 while ( p->sec_buf_in.buf_ptr < p->sec_buf_in.buf_end ) {
222 /* No, we have got only a part of it */
223 ret = p->sec_buf_in.buf_end - p->sec_buf_in.buf_ptr;
225 ret = LBER_SBIOD_READ_NEXT( sbiod, p->sec_buf_in.buf_base +
226 p->sec_buf_in.buf_ptr, ret );
228 if ( ( ret < 0 ) && ( errno == EINTR ) )
234 p->sec_buf_in.buf_ptr += ret;
237 /* Decode the packet */
238 ret = sasl_decode( p->sasl_context, p->sec_buf_in.buf_base,
239 p->sec_buf_in.buf_end, &p->buf_in.buf_base,
240 (unsigned *)&p->buf_in.buf_end );
241 if ( ret != SASL_OK ) {
242 ber_log_printf( LDAP_DEBUG_ANY, sbiod->sbiod_sb->sb_debug,
243 "sb_sasl_read: failed to decode packet: %s\n",
244 sasl_errstring( ret, NULL, NULL ) );
245 sb_sasl_drop_packet( &p->sec_buf_in,
246 sbiod->sbiod_sb->sb_debug );
251 /* Drop the packet from the input buffer */
252 sb_sasl_drop_packet( &p->sec_buf_in, sbiod->sbiod_sb->sb_debug );
254 p->buf_in.buf_size = p->buf_in.buf_end;
256 bufptr += ber_pvt_sb_copy_out( &p->buf_in, (char*) buf + bufptr, len );
262 sb_sasl_write( Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len)
264 struct sb_sasl_data *p;
267 assert( sbiod != NULL );
268 assert( SOCKBUF_VALID( sbiod->sbiod_sb ) );
270 p = (struct sb_sasl_data *)sbiod->sbiod_pvt;
272 /* Are there anything left in the buffer? */
273 if ( p->buf_out.buf_ptr != p->buf_out.buf_end ) {
274 ret = ber_pvt_sb_do_write( sbiod, &p->buf_out );
279 /* now encode the next packet. */
280 ber_pvt_sb_buf_destroy( &p->buf_out );
281 ret = sasl_encode( p->sasl_context, buf, len, &p->buf_out.buf_base,
282 (unsigned *)&p->buf_out.buf_size );
283 if ( ret != SASL_OK ) {
284 ber_log_printf( LDAP_DEBUG_ANY, sbiod->sbiod_sb->sb_debug,
285 "sb_sasl_write: failed to encode packet: %s\n",
286 sasl_errstring( ret, NULL, NULL ) );
289 p->buf_out.buf_end = p->buf_out.buf_size;
291 ret = ber_pvt_sb_do_write( sbiod, &p->buf_out );
298 sb_sasl_ctrl( Sockbuf_IO_Desc *sbiod, int opt, void *arg )
300 struct sb_sasl_data *p;
302 p = (struct sb_sasl_data *)sbiod->sbiod_pvt;
304 if ( opt == LBER_SB_OPT_DATA_READY ) {
305 if ( p->buf_in.buf_ptr != p->buf_in.buf_end )
309 return LBER_SBIOD_CTRL_NEXT( sbiod, opt, arg );
312 Sockbuf_IO ldap_pvt_sockbuf_io_sasl = {
313 sb_sasl_setup, /* sbi_setup */
314 sb_sasl_remove, /* sbi_remove */
315 sb_sasl_ctrl, /* sbi_ctrl */
316 sb_sasl_read, /* sbi_read */
317 sb_sasl_write, /* sbi_write */
321 int ldap_pvt_sasl_install( Sockbuf *sb, void *ctx_arg )
323 Debug( LDAP_DEBUG_TRACE, "ldap_pvt_sasl_install\n",
326 /* don't install the stuff unless security has been negotiated */
328 if ( !ber_sockbuf_ctrl( sb, LBER_SB_OPT_HAS_IO,
329 &ldap_pvt_sockbuf_io_sasl ) )
330 ber_sockbuf_add_io( sb, &ldap_pvt_sockbuf_io_sasl,
331 LBER_SBIOD_LEVEL_APPLICATION, ctx_arg );
337 sasl_err2ldap( int saslerr )
343 rc = LDAP_MORE_RESULTS_TO_RETURN;
346 rc = LDAP_LOCAL_ERROR;
352 rc = LDAP_LOCAL_ERROR;
358 rc = LDAP_AUTH_UNKNOWN;
361 rc = LDAP_AUTH_UNKNOWN;
364 rc = LDAP_PARAM_ERROR;
368 rc = LDAP_AUTH_UNKNOWN;
371 rc = LDAP_LOCAL_ERROR;
375 assert( rc == LDAP_SUCCESS || LDAP_API_ERROR( rc ) );
389 sasl_callback_t *session_callbacks =
390 ber_memcalloc( 2, sizeof( sasl_callback_t ) );
392 if( session_callbacks == NULL ) return LDAP_NO_MEMORY;
394 session_callbacks[0].id = SASL_CB_USER;
395 session_callbacks[0].proc = NULL;
396 session_callbacks[0].context = ld;
398 session_callbacks[1].id = SASL_CB_LIST_END;
399 session_callbacks[1].proc = NULL;
400 session_callbacks[1].context = NULL;
402 assert( lc->lconn_sasl_ctx == NULL );
404 if ( host == NULL ) {
405 ld->ld_errno = LDAP_UNAVAILABLE;
409 rc = sasl_client_new( "ldap", host, session_callbacks,
410 SASL_SECURITY_LAYER, &ctx );
412 if ( rc != SASL_OK ) {
413 ld->ld_errno = sasl_err2ldap( rc );
417 Debug( LDAP_DEBUG_TRACE, "ldap_int_sasl_open: %s\n",
420 lc->lconn_sasl_ctx = ctx;
423 sasl_external_properties_t extprops;
424 memset(&extprops, 0L, sizeof(extprops));
427 (void) sasl_setprop( ctx, SASL_SSF_EXTERNAL,
428 (void *) &extprops );
430 Debug( LDAP_DEBUG_TRACE, "ldap_int_sasl_open: ssf=%ld\n",
437 int ldap_int_sasl_close( LDAP *ld, LDAPConn *lc )
439 sasl_conn_t *ctx = lc->lconn_sasl_ctx;
440 assert( ctx != NULL );
443 sasl_dispose( &ctx );
444 lc->lconn_sasl_ctx = NULL;
455 LDAPControl **sctrls,
456 LDAPControl **cctrls,
458 LDAP_SASL_INTERACT_PROC *interact,
462 const char *mech = NULL;
463 const char *pmech = NULL;
465 sasl_ssf_t *ssf = NULL;
467 sasl_interact_t *prompts = NULL;
472 Debug( LDAP_DEBUG_TRACE, "ldap_int_sasl_bind: %s\n",
473 mechs ? mechs : "<null>", 0, 0 );
475 /* do a quick !LDAPv3 check... ldap_sasl_bind will do the rest. */
476 if (ld->ld_version < LDAP_VERSION3) {
477 ld->ld_errno = LDAP_NOT_SUPPORTED;
481 ber_sockbuf_ctrl( ld->ld_sb, LBER_SB_OPT_GET_FD, &sd );
483 if ( sd == AC_SOCKET_INVALID ) {
484 /* not connected yet */
485 int rc = ldap_open_defconn( ld );
487 if( rc < 0 ) return ld->ld_errno;
488 ber_sockbuf_ctrl( ld->ld_sb, LBER_SB_OPT_GET_FD, &sd );
490 if( sd == AC_SOCKET_INVALID ) {
491 ld->ld_errno = LDAP_UNAVAILABLE;
496 ctx = ld->ld_defconn->lconn_sasl_ctx;
499 ld->ld_errno = LDAP_UNAVAILABLE;
503 /* (re)set security properties */
504 sasl_setprop( ctx, SASL_SEC_PROPS,
505 &ld->ld_options.ldo_sasl_secprops );
511 saslrc = sasl_client_start( ctx,
519 if( pmech == NULL && mech != NULL ) {
522 if( flags != LDAP_SASL_QUIET ) {
524 "SASL/%s authentication started\n",
529 if( saslrc == SASL_INTERACT ) {
531 if( !interact ) break;
532 res = (interact)( ld, flags, defaults, prompts );
533 if( res != LDAP_SUCCESS ) {
537 } while ( saslrc == SASL_INTERACT );
539 ccred.bv_len = credlen;
541 if ( (saslrc != SASL_OK) && (saslrc != SASL_CONTINUE) ) {
542 ld->ld_errno = sasl_err2ldap( saslrc );
547 struct berval *scred;
552 rc = ldap_sasl_bind_s( ld, dn, mech, &ccred, sctrls, cctrls, &scred );
554 if ( ccred.bv_val != NULL ) {
555 LDAP_FREE( ccred.bv_val );
559 if ( rc != LDAP_SUCCESS && rc != LDAP_SASL_BIND_IN_PROGRESS ) {
561 /* and server provided us with data? */
562 Debug( LDAP_DEBUG_TRACE,
563 "ldap_int_sasl_bind: rc=%d sasl=%d len=%ld\n",
564 rc, saslrc, scred->bv_len );
570 if( rc == LDAP_SUCCESS && saslrc == SASL_OK ) {
571 /* we're done, no need to step */
573 /* but server provided us with data! */
574 Debug( LDAP_DEBUG_TRACE,
575 "ldap_int_sasl_bind: rc=%d sasl=%d len=%ld\n",
576 rc, saslrc, scred->bv_len );
578 return ld->ld_errno = LDAP_LOCAL_ERROR;
584 saslrc = sasl_client_step( ctx,
585 (scred == NULL) ? NULL : scred->bv_val,
586 (scred == NULL) ? 0 : scred->bv_len,
591 Debug( LDAP_DEBUG_TRACE, "sasl_client_start: %d\n",
594 if( saslrc == SASL_INTERACT ) {
596 if( !interact ) break;
597 res = (interact)( ld, flags, defaults, prompts );
598 if( res != LDAP_SUCCESS ) {
602 } while ( saslrc == SASL_INTERACT );
604 ccred.bv_len = credlen;
607 if ( (saslrc != SASL_OK) && (saslrc != SASL_CONTINUE) ) {
608 ld->ld_errno = sasl_err2ldap( saslrc );
611 } while ( rc == LDAP_SASL_BIND_IN_PROGRESS );
613 if ( rc != LDAP_SUCCESS ) {
617 if ( saslrc != SASL_OK ) {
618 return ld->ld_errno = sasl_err2ldap( saslrc );
621 if( flags != LDAP_SASL_QUIET ) {
622 saslrc = sasl_getprop( ctx, SASL_USERNAME, (void **) &data );
623 if( saslrc == SASL_OK && data && *data ) {
624 fprintf( stderr, "SASL username: %s\n", data );
627 saslrc = sasl_getprop( ctx, SASL_REALM, (void **) &data );
628 if( saslrc == SASL_OK && data && *data ) {
629 fprintf( stderr, "SASL realm: %s\n", data );
633 saslrc = sasl_getprop( ctx, SASL_SSF, (void **) &ssf );
634 if( saslrc == SASL_OK ) {
635 if( flags != LDAP_SASL_QUIET ) {
636 fprintf( stderr, "SASL SSF: %lu\n",
637 (unsigned long) *ssf );
641 if( flags != LDAP_SASL_QUIET ) {
642 fprintf( stderr, "SASL installing layers\n" );
644 ldap_pvt_sasl_install( ld->ld_sb, ctx );
651 int ldap_pvt_sasl_secprops(
653 sasl_security_properties_t *secprops )
656 char **props = ldap_str2charray( in, "," );
659 sasl_ssf_t max_ssf = 0;
661 sasl_ssf_t min_ssf = 0;
663 unsigned maxbufsize = 0;
664 int got_maxbufsize = 0;
666 if( props == NULL || secprops == NULL ) {
667 return LDAP_PARAM_ERROR;
670 for( i=0; props[i]; i++ ) {
671 if( !strcasecmp(props[i], "none") ) {
674 } else if( !strcasecmp(props[i], "noplain") ) {
676 sflags |= SASL_SEC_NOPLAINTEXT;
678 } else if( !strcasecmp(props[i], "noactive") ) {
680 sflags |= SASL_SEC_NOACTIVE;
682 } else if( !strcasecmp(props[i], "nodict") ) {
684 sflags |= SASL_SEC_NODICTIONARY;
686 } else if( !strcasecmp(props[i], "forwardsec") ) {
688 sflags |= SASL_SEC_FORWARD_SECRECY;
690 } else if( !strcasecmp(props[i], "noanonymous")) {
692 sflags |= SASL_SEC_NOANONYMOUS;
694 } else if( !strcasecmp(props[i], "passcred") ) {
696 sflags |= SASL_SEC_PASS_CREDENTIALS;
698 } else if( !strncasecmp(props[i],
699 "minssf=", sizeof("minssf")) )
701 if( isdigit( props[i][sizeof("minssf")] ) ) {
703 min_ssf = atoi( &props[i][sizeof("minssf")] );
705 return LDAP_NOT_SUPPORTED;
708 } else if( !strncasecmp(props[i],
709 "maxssf=", sizeof("maxssf")) )
711 if( isdigit( props[i][sizeof("maxssf")] ) ) {
713 max_ssf = atoi( &props[i][sizeof("maxssf")] );
715 return LDAP_NOT_SUPPORTED;
718 } else if( !strncasecmp(props[i],
719 "maxbufsize=", sizeof("maxbufsize")) )
721 if( isdigit( props[i][sizeof("maxbufsize")] ) ) {
723 maxbufsize = atoi( &props[i][sizeof("maxbufsize")] );
725 return LDAP_NOT_SUPPORTED;
729 return LDAP_NOT_SUPPORTED;
734 secprops->security_flags = sflags;
737 secprops->min_ssf = min_ssf;
740 secprops->max_ssf = max_ssf;
743 secprops->maxbufsize = maxbufsize;
746 ldap_charray_free( props );
751 ldap_int_sasl_config( struct ldapoptions *lo, int option, const char *arg )
756 case LDAP_OPT_X_SASL_SECPROPS:
757 rc = ldap_pvt_sasl_secprops( arg, &lo->ldo_sasl_secprops );
758 if( rc == LDAP_SUCCESS ) return 0;
765 ldap_int_sasl_get_option( LDAP *ld, int option, void *arg )
771 case LDAP_OPT_X_SASL_MECH: {
772 *(char **)arg = ld->ld_options.ldo_def_sasl_mech
773 ? LDAP_STRDUP( ld->ld_options.ldo_def_sasl_mech ) : NULL;
775 case LDAP_OPT_X_SASL_REALM: {
776 *(char **)arg = ld->ld_options.ldo_def_sasl_realm
777 ? LDAP_STRDUP( ld->ld_options.ldo_def_sasl_realm ) : NULL;
779 case LDAP_OPT_X_SASL_AUTHCID: {
780 *(char **)arg = ld->ld_options.ldo_def_sasl_authcid
781 ? LDAP_STRDUP( ld->ld_options.ldo_def_sasl_authcid ) : NULL;
783 case LDAP_OPT_X_SASL_AUTHZID: {
784 *(char **)arg = ld->ld_options.ldo_def_sasl_authzid
785 ? LDAP_STRDUP( ld->ld_options.ldo_def_sasl_authzid ) : NULL;
788 case LDAP_OPT_X_SASL_SSF: {
793 if( ld->ld_defconn == NULL ) {
797 ctx = ld->ld_defconn->lconn_sasl_ctx;
803 sc = sasl_getprop( ctx, SASL_SSF,
806 if ( sc != SASL_OK ) {
810 *(ber_len_t *)arg = *ssf;
813 case LDAP_OPT_X_SASL_SSF_EXTERNAL:
814 /* this option is write only */
817 case LDAP_OPT_X_SASL_SSF_MIN:
818 *(ber_len_t *)arg = ld->ld_options.ldo_sasl_secprops.min_ssf;
820 case LDAP_OPT_X_SASL_SSF_MAX:
821 *(ber_len_t *)arg = ld->ld_options.ldo_sasl_secprops.max_ssf;
823 case LDAP_OPT_X_SASL_MAXBUFSIZE:
824 *(ber_len_t *)arg = ld->ld_options.ldo_sasl_secprops.maxbufsize;
827 case LDAP_OPT_X_SASL_SECPROPS:
828 /* this option is write only */
838 ldap_int_sasl_set_option( LDAP *ld, int option, void *arg )
844 case LDAP_OPT_X_SASL_SSF:
845 /* This option is read-only */
848 case LDAP_OPT_X_SASL_SSF_EXTERNAL: {
850 sasl_external_properties_t extprops;
853 if( ld->ld_defconn == NULL ) {
857 ctx = ld->ld_defconn->lconn_sasl_ctx;
863 memset(&extprops, 0L, sizeof(extprops));
865 extprops.ssf = * (ber_len_t *) arg;
867 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL,
868 (void *) &extprops );
870 if ( sc != SASL_OK ) {
875 case LDAP_OPT_X_SASL_SSF_MIN:
876 ld->ld_options.ldo_sasl_secprops.min_ssf = *(ber_len_t *)arg;
878 case LDAP_OPT_X_SASL_SSF_MAX:
879 ld->ld_options.ldo_sasl_secprops.max_ssf = *(ber_len_t *)arg;
881 case LDAP_OPT_X_SASL_MAXBUFSIZE:
882 ld->ld_options.ldo_sasl_secprops.maxbufsize = *(ber_len_t *)arg;
885 case LDAP_OPT_X_SASL_SECPROPS: {
887 sc = ldap_pvt_sasl_secprops( (char *) arg,
888 &ld->ld_options.ldo_sasl_secprops );
890 return sc == LDAP_SUCCESS ? 0 : -1;
899 #ifdef LDAP_R_COMPILE
900 void *ldap_pvt_sasl_mutex_new(void)
902 ldap_pvt_thread_mutex_t *mutex;
904 mutex = (ldap_pvt_thread_mutex_t *) LDAP_MALLOC(
905 sizeof(ldap_pvt_thread_mutex_t) );
907 if ( ldap_pvt_thread_mutex_init( mutex ) == 0 ) {
913 int ldap_pvt_sasl_mutex_lock(void *mutex)
915 return ldap_pvt_thread_mutex_lock( (ldap_pvt_thread_mutex_t *)mutex )
916 ? SASL_FAIL : SASL_OK;
919 int ldap_pvt_sasl_mutex_unlock(void *mutex)
921 return ldap_pvt_thread_mutex_unlock( (ldap_pvt_thread_mutex_t *)mutex )
922 ? SASL_FAIL : SASL_OK;
925 void ldap_pvt_sasl_mutex_dispose(void *mutex)
927 (void) ldap_pvt_thread_mutex_destroy( (ldap_pvt_thread_mutex_t *)mutex );
933 int ldap_int_sasl_init( void )
934 { return LDAP_SUCCESS; }
936 int ldap_int_sasl_close( LDAP *ld, LDAPConn *lc )
937 { return LDAP_SUCCESS; }
944 LDAPControl **sctrls,
945 LDAPControl **cctrls,
947 LDAP_SASL_INTERACT_PROC *interact,
949 { return LDAP_NOT_SUPPORTED; }
950 #endif /* HAVE_CYRUS_SASL */