3 * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
10 #include <ac/stdlib.h>
11 #include <ac/string.h>
18 #ifdef HAVE_CYRUS_SASL
21 #ifdef HAVE_SASL_SASL_H
22 #include <sasl/sasl.h>
27 #if SASL_VERSION_MAJOR >= 2
29 #define SASL_CONST const
40 /* Flags for telling slap_sasl_getdn() what type of identity is being passed */
41 #define FLAG_GETDN_AUTHCID 2
42 #define FLAG_GETDN_AUTHZID 4
44 static sasl_security_properties_t sasl_secprops;
52 Connection *conn = context;
56 if ( message == NULL ) {
61 #if SASL_VERSION_MAJOR >= 2
63 level = LDAP_DEBUG_NONE;
67 level = LDAP_DEBUG_ANY;
71 level = LDAP_DEBUG_ANY;
75 level = LDAP_DEBUG_TRACE;
79 level = LDAP_DEBUG_TRACE;
83 level = LDAP_DEBUG_TRACE;
87 level = LDAP_DEBUG_TRACE;
91 level = LDAP_DEBUG_TRACE;
92 label = "Password Trace";
96 level = LDAP_DEBUG_ANY;
99 case SASL_LOG_WARNING:
100 level = LDAP_DEBUG_TRACE;
104 level = LDAP_DEBUG_TRACE;
109 return SASL_BADPARAM;
113 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
114 "SASL [conn=%ld] %s: %s\n",
115 conn ? conn->c_connid : -1,
118 Debug( level, "SASL [conn=%ld] %s: %s\n",
119 conn ? conn->c_connid: -1,
128 /* Take any sort of identity string and return a DN with the "dn:" prefix. The
129 string returned in *dn is in its own allocated memory, and must be free'd
130 by the calling process.
131 -Mark Adamson, Carnegie Mellon
133 The "dn:" prefix is no longer used anywhere inside slapd. It is only used
134 on strings passed in directly from SASL.
135 -Howard Chu, Symas Corp.
141 static struct berval ext_bv = { sizeof("EXTERNAL")-1, "EXTERNAL" };
143 int slap_sasl_getdn( Connection *conn, char *id, int len,
144 char *user_realm, struct berval *dn, int flags )
147 int rc, is_dn = 0, do_norm = 1;
152 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
153 "slap_sasl_getdn: conn %d id=%s\n",
154 conn ? conn->c_connid : -1,
155 id ? (*id ? id : "<empty>") : "NULL" ));
157 Debug( LDAP_DEBUG_ARGS, "slap_sasl_getdn: id=%s\n",
158 id?(*id?id:"<empty>"):"NULL",0,0 );
165 if ( len == 0 ) len = strlen( id );
167 /* Blatantly anonymous ID */
168 if ( len == sizeof("anonymous") - 1 &&
169 !strcasecmp( id, "anonymous" ) ) {
170 return( LDAP_SUCCESS );
176 ctx = conn->c_sasl_context;
178 /* An authcID needs to be converted to authzID form */
179 if( flags & FLAG_GETDN_AUTHCID ) {
180 if( conn->c_is_tls && conn->c_sasl_bind_mech.bv_len == ext_bv.bv_len
181 && ( strcasecmp( ext_bv.bv_val, conn->c_sasl_bind_mech.bv_val ) == 0 ) ) {
182 /* X.509 DN is already normalized */
187 /* convert to u:<username> form */
190 ber_str2bv( id, len, 1, dn );
193 if( !strncasecmp( id, "u:", sizeof("u:")-1 )) {
195 ber_str2bv( id+2, len-2, 1, dn );
196 } else if ( !strncasecmp( id, "dn:", sizeof("dn:")-1) ) {
198 ber_str2bv( id+3, len-3, 1, dn );
202 /* An authzID must be properly prefixed */
203 if( (flags & FLAG_GETDN_AUTHZID) && !is_dn ) {
207 return( LDAP_INAPPROPRIATE_AUTH );
210 /* Username strings */
211 if( is_dn == SET_U ) {
213 len = dn->bv_len + sizeof("uid=")-1 + sizeof(",cn=auth")-1;
215 if( user_realm && *user_realm ) {
216 len += strlen( user_realm ) + sizeof(",cn=")-1;
219 if( conn->c_sasl_bind_mech.bv_len ) {
220 len += conn->c_sasl_bind_mech.bv_len + sizeof(",cn=")-1;
223 /* Build the new dn */
225 dn->bv_val = ch_malloc( len+1 );
226 p = slap_strcopy( dn->bv_val, "uid=" );
227 p = slap_strcopy( p, c1 );
230 if( user_realm && *user_realm ) {
231 p = slap_strcopy( p, ",cn=" );
232 p = slap_strcopy( p, user_realm );
234 if( conn->c_sasl_bind_mech.bv_len ) {
235 p = slap_strcopy( p, ",cn=" );
236 p = slap_strcopy( p, conn->c_sasl_bind_mech.bv_val );
238 p = slap_strcopy( p, ",cn=auth" );
239 dn->bv_len = p - dn->bv_val;
243 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
244 "slap_sasl_getdn: u:id converted to %s.\n", dn->bv_val ));
246 Debug( LDAP_DEBUG_TRACE, "getdn: u:id converted to %s\n", dn->bv_val,0,0 );
250 /* DN strings that are a cn=auth identity to run through regexp */
251 if( is_dn == SET_DN )
253 slap_sasl2dn( dn, &dn2 );
255 ch_free( dn->bv_val );
257 do_norm = 0; /* slap_sasl2dn normalizes */
259 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
260 "slap_sasl_getdn: dn:id converted to %s.\n", dn->bv_val ));
262 Debug( LDAP_DEBUG_TRACE, "getdn: dn:id converted to %s\n",
269 rc = dnNormalize2( NULL, dn, &dn2 );
271 if ( rc != LDAP_SUCCESS ) {
278 return( LDAP_SUCCESS );
281 #if SASL_VERSION_MAJOR >= 2
286 const char *username,
289 struct propctx *propctx)
291 Connection *conn = (Connection *)context;
292 struct berval dn, cred;
296 cred.bv_val = (char *)pass;
297 cred.bv_len = passlen;
299 /* SASL will fallback to its own mechanisms if we don't
300 * find an answer here.
303 rc = slap_sasl_getdn( conn, (char *)username, 0, NULL, &dn,
304 FLAG_GETDN_AUTHCID );
305 if ( rc != LDAP_SUCCESS ) {
306 sasl_seterror( sconn, 0, ldap_err2string( rc ) );
310 if ( dn.bv_len == 0 ) {
311 sasl_seterror( sconn, 0,
312 "No password is associated with the Root DSE" );
313 if ( dn.bv_val != NULL ) {
314 ch_free( dn.bv_val );
319 rc = backend_attribute( NULL, NULL, NULL, NULL, &dn,
320 slap_schema.si_ad_userPassword, &vals);
321 if ( rc != LDAP_SUCCESS ) {
322 ch_free( dn.bv_val );
323 sasl_seterror( sconn, 0, ldap_err2string( rc ) );
324 return SASL_NOVERIFY;
329 if ( vals != NULL ) {
330 for ( bv = vals; bv->bv_val != NULL; bv++ ) {
331 if ( !lutil_passwd( bv, &cred, NULL ) ) {
336 ber_bvarray_free( vals );
339 if ( rc != SASL_OK ) {
340 sasl_seterror( sconn, 0,
341 ldap_err2string( LDAP_INVALID_CREDENTIALS ) );
344 ch_free( dn.bv_val );
349 #if 0 /* CANON isn't for what you think it does. */
351 slap_sasl_canonicalize(
357 const char *user_realm,
362 Connection *conn = (Connection *)context;
369 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
370 "slap_sasl_canonicalize: conn %d %s=\"%s\"\n",
371 conn ? conn->c_connid : -1,
372 (flags == SASL_CU_AUTHID) ? "authcid" : "authzid",
373 in ? in : "<empty>" ));
375 Debug( LDAP_DEBUG_ARGS, "SASL Canonicalize [conn=%ld]: "
377 conn ? conn->c_connid : -1,
378 (flags == SASL_CU_AUTHID) ? "authcid" : "authzid",
379 in ? in : "<empty>" );
382 rc = slap_sasl_getdn( conn, (char *)in, inlen, (char *)user_realm, &dn,
383 (flags == SASL_CU_AUTHID) ? FLAG_GETDN_AUTHCID : FLAG_GETDN_AUTHZID );
384 if ( rc != LDAP_SUCCESS ) {
385 sasl_seterror( sconn, 0, ldap_err2string( rc ) );
389 if ( out_max < dn.bv_len ) {
393 AC_MEMCPY( out, dn.bv_val, dn.bv_len );
394 out[dn.bv_len] = '\0';
396 *out_len = dn.bv_len;
398 ch_free( dn.bv_val );
401 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
402 "slap_sasl_canonicalize: conn %d %s=\"%s\"\n",
403 conn ? conn->c_connid : -1,
404 (flags == SASL_CU_AUTHID) ? "authcDN" : "authzDN",
407 Debug( LDAP_DEBUG_ARGS, "SASL Canonicalize [conn=%ld]: "
409 conn ? conn->c_connid : -1,
410 (flags == SASL_CU_AUTHID) ? "authcDN" : "authzDN",
418 #define CANON_BUF_SIZE 256 /* from saslint.h */
424 char *requested_user,
428 const char *def_realm,
430 struct propctx *propctx)
432 Connection *conn = (Connection *)context;
433 struct berval authcDN, authzDN;
435 int rc, equal = 1, ext = 0;
438 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
439 "slap_sasl_authorize: conn %d authcid=\"%s\" authzid=\"%s\"\n",
440 conn ? conn->c_connid : -1, auth_identity, requested_user));
442 Debug( LDAP_DEBUG_ARGS, "SASL Authorize [conn=%ld]: "
443 "authcid=\"%s\" authzid=\"%s\"\n",
444 conn ? conn->c_connid : -1, auth_identity, requested_user );
447 if ( requested_user )
448 equal = !strcmp( auth_identity, requested_user );
450 /* If using SASL-EXTERNAL, don't modify the ID in any way */
451 if ( conn->c_is_tls && conn->c_sasl_bind_mech.bv_len == ext_bv.bv_len
452 && ( strcasecmp( ext_bv.bv_val, conn->c_sasl_bind_mech.bv_val ) == 0 ) ) {
456 /* Else look for an embedded realm in the name */
457 realm = strchr( auth_identity, '@' );
458 if ( realm ) *realm++ = '\0';
461 rc = slap_sasl_getdn( conn, auth_identity, alen, realm ? realm : (char *)def_realm,
462 &authcDN, FLAG_GETDN_AUTHCID );
466 if ( rc != LDAP_SUCCESS ) {
467 sasl_seterror( sconn, 0, ldap_err2string( rc ) );
472 if ( authcDN.bv_len > CANON_BUF_SIZE ) {
473 free( authcDN.bv_val );
476 AC_MEMCPY( requested_user, authcDN.bv_val, authcDN.bv_len );
484 realm = strchr( requested_user, '@' );
485 if ( realm ) *realm++ = '\0';
488 rc = slap_sasl_getdn( conn, requested_user, rlen, realm ? realm : (char *)def_realm,
489 &authzDN, FLAG_GETDN_AUTHZID );
493 if ( rc != LDAP_SUCCESS ) {
494 free( authcDN.bv_val );
495 sasl_seterror( sconn, 0, ldap_err2string( rc ) );
499 if (authzDN.bv_len > CANON_BUF_SIZE) {
500 free( authcDN.bv_val );
501 free( authzDN.bv_val );
505 rc = slap_sasl_authorized( &authcDN, &authzDN );
506 free( authcDN.bv_val );
507 if ( rc != LDAP_SUCCESS ) {
509 LDAP_LOG(( "sasl", LDAP_LEVEL_INFO,
510 "slap_sasl_authorize: conn %ld authorization disallowed (%d)\n",
511 (long)(conn ? conn->c_connid : -1), rc ));
513 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
514 " authorization disallowed (%d)\n",
515 (long) (conn ? conn->c_connid : -1), rc, 0 );
518 sasl_seterror( sconn, 0, "not authorized" );
519 free( authzDN.bv_val );
522 AC_MEMCPY( requested_user, authzDN.bv_val, authzDN.bv_len );
523 free( authzDN.bv_val );
526 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
527 "slap_sasl_authorize: conn %d authorization allowed\n",
528 (long)(conn ? conn->c_connid : -1 ) ));
530 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
531 " authorization allowed\n",
532 (long) (conn ? conn->c_connid : -1), 0, 0 );
546 struct berval authcDN, authzDN;
548 Connection *conn = context;
549 char *realm, *xrealm;
554 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
555 "slap_sasl_authorize: conn %d authcid=\"%s\" authzid=\"%s\"\n",
556 conn ? conn->c_connid : -1,
557 authcid ? authcid : "<empty>",
558 authzid ? authzid : "<empty>" ));
560 Debug( LDAP_DEBUG_ARGS, "SASL Authorize [conn=%ld]: "
561 "authcid=\"%s\" authzid=\"%s\"\n",
562 (long) (conn ? conn->c_connid : -1),
563 authcid ? authcid : "<empty>",
564 authzid ? authzid : "<empty>" );
567 /* Figure out how much data we have for the dn */
568 rc = sasl_getprop( conn->c_sasl_context, SASL_REALM, (void **)&realm );
569 if( rc != SASL_OK && rc != SASL_NOTDONE ) {
571 LDAP_LOG(( "sasl", LDAP_LEVEL_ERR,
572 "slap_sasl_authorize: getprop(REALM) failed.\n" ));
574 Debug(LDAP_DEBUG_TRACE,
575 "authorize: getprop(REALM) failed!\n", 0,0,0);
577 *errstr = "Could not extract realm";
581 /* Convert the identities to DN's. If no authzid was given, client will
582 be bound as the DN matching their username */
583 if ( conn->c_is_tls && conn->c_sasl_bind_mech.bv_len == ext_bv.bv_len
584 && ( strcasecmp( ext_bv.bv_val, conn->c_sasl_bind_mech.bv_val ) == 0 ) ) {
588 xrealm = strchr( authcid, '@' );
589 if ( xrealm ) *xrealm++ = '\0';
591 rc = slap_sasl_getdn( conn, (char *)authcid, 0, xrealm ? xrealm : realm, &authcDN, FLAG_GETDN_AUTHCID );
592 if ( xrealm ) xrealm[-1] = '@';
593 if( rc != LDAP_SUCCESS ) {
594 *errstr = ldap_err2string( rc );
597 if( ( authzid == NULL ) || !strcmp( authcid,authzid ) ) {
599 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
600 "slap_sasl_authorize: conn %d Using authcDN=%s\n",
601 conn ? conn->c_connid : -1, authcDN.bv_val ));
603 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
604 "Using authcDN=%s\n", (long) (conn ? conn->c_connid : -1), authcDN.bv_val,0 );
607 *user = authcDN.bv_val;
614 xrealm = strchr( authzid, '@' );
615 if ( xrealm ) *xrealm++ = '\0';
617 rc = slap_sasl_getdn( conn, (char *)authzid, 0, xrealm ? xrealm : realm, &authzDN, FLAG_GETDN_AUTHZID );
618 if ( xrealm ) xrealm[-1] = '@';
619 if( rc != LDAP_SUCCESS ) {
620 ch_free( authcDN.bv_val );
621 *errstr = ldap_err2string( rc );
625 rc = slap_sasl_authorized( &authcDN, &authzDN );
628 LDAP_LOG(( "sasl", LDAP_LEVEL_INFO,
629 "slap_sasl_authorize: conn %ld authorization disallowed (%d)\n",
630 (long)(conn ? conn->c_connid : -1), rc ));
632 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
633 " authorization disallowed (%d)\n",
634 (long) (conn ? conn->c_connid : -1), rc, 0 );
637 *errstr = "not authorized";
638 ch_free( authcDN.bv_val );
639 ch_free( authzDN.bv_val );
644 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
645 "slap_sasl_authorize: conn %d authorization allowed\n",
646 (long)(conn ? conn->c_connid : -1 ) ));
648 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
649 " authorization allowed\n",
650 (long) (conn ? conn->c_connid : -1), 0, 0 );
654 ch_free( authcDN.bv_val );
655 *user = authzDN.bv_val;
659 #endif /* SASL_VERSION_MAJOR >= 2 */
662 slap_sasl_err2ldap( int saslerr )
668 rc = LDAP_SASL_BIND_IN_PROGRESS;
677 rc = LDAP_AUTH_METHOD_NOT_SUPPORTED;
680 rc = LDAP_INVALID_CREDENTIALS;
683 rc = LDAP_INSUFFICIENT_ACCESS;
687 rc = LDAP_INAPPROPRIATE_AUTH;
699 int slap_sasl_init( void )
701 #ifdef HAVE_CYRUS_SASL
703 static sasl_callback_t server_callbacks[] = {
704 { SASL_CB_LOG, &slap_sasl_log, NULL },
705 { SASL_CB_LIST_END, NULL, NULL }
715 ldap_pvt_sasl_mutex_new,
716 ldap_pvt_sasl_mutex_lock,
717 ldap_pvt_sasl_mutex_unlock,
718 ldap_pvt_sasl_mutex_dispose );
720 /* should provide callbacks for logging */
721 /* server name should be configurable */
722 rc = sasl_server_init( server_callbacks, "slapd" );
724 if( rc != SASL_OK ) {
726 LDAP_LOG(( "sasl", LDAP_LEVEL_INFO,
727 "slap_sasl_init: init failed.\n" ));
729 Debug( LDAP_DEBUG_ANY, "sasl_server_init failed\n",
737 LDAP_LOG(( "sasl", LDAP_LEVEL_INFO,
738 "slap_sasl_init: initialized!\n"));
740 Debug( LDAP_DEBUG_TRACE, "slap_sasl_init: initialized!\n",
745 /* default security properties */
746 memset( &sasl_secprops, '\0', sizeof(sasl_secprops) );
747 sasl_secprops.max_ssf = INT_MAX;
748 sasl_secprops.maxbufsize = 65536;
749 sasl_secprops.security_flags = SASL_SEC_NOPLAINTEXT|SASL_SEC_NOANONYMOUS;
755 int slap_sasl_destroy( void )
757 #ifdef HAVE_CYRUS_SASL
766 int slap_sasl_open( Connection *conn )
768 int cb, sc = LDAP_SUCCESS;
769 #if SASL_VERSION_MAJOR >= 2
770 char *ipremoteport = NULL, *iplocalport = NULL;
773 #ifdef HAVE_CYRUS_SASL
774 sasl_conn_t *ctx = NULL;
775 sasl_callback_t *session_callbacks;
777 assert( conn->c_sasl_context == NULL );
778 assert( conn->c_sasl_extra == NULL );
780 conn->c_sasl_layers = 0;
783 #if SASL_VERSION_MAJOR >= 2
784 ch_calloc( 5, sizeof(sasl_callback_t));
786 ch_calloc( 3, sizeof(sasl_callback_t));
788 conn->c_sasl_extra = session_callbacks;
790 session_callbacks[cb=0].id = SASL_CB_LOG;
791 session_callbacks[cb].proc = &slap_sasl_log;
792 session_callbacks[cb++].context = conn;
794 session_callbacks[cb].id = SASL_CB_PROXY_POLICY;
795 session_callbacks[cb].proc = &slap_sasl_authorize;
796 session_callbacks[cb++].context = conn;
798 #if SASL_VERSION_MAJOR >= 2
799 #if 0 /* CANON isn't for what you think it does. */
800 session_callbacks[cb].id = SASL_CB_CANON_USER;
801 session_callbacks[cb].proc = &slap_sasl_canonicalize;
802 session_callbacks[cb++].context = conn;
805 /* XXXX: this should be conditional */
806 session_callbacks[cb].id = SASL_CB_SERVER_USERDB_CHECKPASS;
807 session_callbacks[cb].proc = &slap_sasl_checkpass;
808 session_callbacks[cb++].context = conn;
811 session_callbacks[cb].id = SASL_CB_LIST_END;
812 session_callbacks[cb].proc = NULL;
813 session_callbacks[cb++].context = NULL;
815 if( global_host == NULL ) {
816 global_host = ldap_pvt_get_fqdn( NULL );
819 /* create new SASL context */
820 #if SASL_VERSION_MAJOR >= 2
821 if ( conn->c_sock_name.bv_len != 0 &&
822 strncmp( conn->c_sock_name.bv_val, "IP=", 3 ) == 0) {
825 iplocalport = ch_strdup( conn->c_sock_name.bv_val + 3 );
826 /* Convert IPv6 addresses to address;port syntax. */
827 p = strrchr( iplocalport, ' ' );
828 /* Convert IPv4 addresses to address;port syntax. */
829 if ( p == NULL ) p = strchr( iplocalport, ':' );
834 if ( conn->c_peer_name.bv_len != 0 &&
835 strncmp( conn->c_peer_name.bv_val, "IP=", 3 ) == 0) {
838 ipremoteport = ch_strdup( conn->c_peer_name.bv_val + 3 );
839 /* Convert IPv6 addresses to address;port syntax. */
840 p = strrchr( ipremoteport, ' ' );
841 /* Convert IPv4 addresses to address;port syntax. */
842 if ( p == NULL ) p = strchr( ipremoteport, ':' );
847 sc = sasl_server_new( "ldap", global_host, global_realm,
848 iplocalport, ipremoteport, session_callbacks, 0, &ctx );
849 if ( iplocalport != NULL ) {
850 ch_free( iplocalport );
852 if ( ipremoteport != NULL ) {
853 ch_free( ipremoteport );
856 sc = sasl_server_new( "ldap", global_host, global_realm,
857 session_callbacks, SASL_SECURITY_LAYER, &ctx );
860 if( sc != SASL_OK ) {
862 LDAP_LOG(( "sasl", LDAP_LEVEL_ERR,
863 "slap_sasl_open: sasl_server_new failed: %d\n", sc ));
865 Debug( LDAP_DEBUG_ANY, "sasl_server_new failed: %d\n",
872 conn->c_sasl_context = ctx;
874 if( sc == SASL_OK ) {
875 sc = sasl_setprop( ctx,
876 SASL_SEC_PROPS, &sasl_secprops );
878 if( sc != SASL_OK ) {
880 LDAP_LOG(( "sasl", LDAP_LEVEL_ERR,
881 "slap_sasl_open: sasl_setprop failed: %d \n", sc ));
883 Debug( LDAP_DEBUG_ANY, "sasl_setprop failed: %d\n",
887 slap_sasl_close( conn );
892 sc = slap_sasl_err2ldap( sc );
897 int slap_sasl_external(
900 const char *auth_id )
902 #if SASL_VERSION_MAJOR >= 2
904 sasl_conn_t *ctx = conn->c_sasl_context;
907 return LDAP_UNAVAILABLE;
910 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL, &ssf );
912 if ( sc != SASL_OK ) {
916 sc = sasl_setprop( ctx, SASL_AUTH_EXTERNAL, auth_id );
918 if ( sc != SASL_OK ) {
922 #elif defined(HAVE_CYRUS_SASL)
924 sasl_conn_t *ctx = conn->c_sasl_context;
925 sasl_external_properties_t extprops;
928 return LDAP_UNAVAILABLE;
931 memset( &extprops, '\0', sizeof(extprops) );
933 extprops.auth_id = (char *) auth_id;
935 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL,
936 (void *) &extprops );
938 if ( sc != SASL_OK ) {
946 int slap_sasl_reset( Connection *conn )
948 #ifdef HAVE_CYRUS_SASL
949 sasl_conn_t *ctx = conn->c_sasl_context;
954 /* must return "anonymous" */
958 char ** slap_sasl_mechs( Connection *conn )
962 #ifdef HAVE_CYRUS_SASL
963 sasl_conn_t *ctx = conn->c_sasl_context;
967 SASL_CONST char *mechstr;
969 sc = sasl_listmech( ctx,
970 NULL, NULL, ",", NULL,
971 &mechstr, NULL, NULL );
973 if( sc != SASL_OK ) {
975 LDAP_LOG(( "sasl", LDAP_LEVEL_ERR,
976 "slap_sasl_mechs: sasl_listmech failed: %d\n", sc ));
978 Debug( LDAP_DEBUG_ANY, "slap_sasl_listmech failed: %d\n",
985 mechs = str2charray( mechstr, "," );
987 #if SASL_VERSION_MAJOR < 2
996 int slap_sasl_close( Connection *conn )
998 #ifdef HAVE_CYRUS_SASL
999 sasl_conn_t *ctx = conn->c_sasl_context;
1002 sasl_dispose( &ctx );
1005 conn->c_sasl_context = NULL;
1007 free( conn->c_sasl_extra );
1008 conn->c_sasl_extra = NULL;
1011 return LDAP_SUCCESS;
1019 struct berval *cred,
1025 #ifdef HAVE_CYRUS_SASL
1026 sasl_conn_t *ctx = conn->c_sasl_context;
1027 struct berval response;
1028 unsigned reslen = 0;
1029 const char *errstr = NULL;
1033 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
1034 "sasl_bind: conn %ld dn=\"%s\" mech=%s datalen=%ld\n",
1036 dn->bv_len ? dn->bv_val : "",
1037 conn->c_sasl_bind_in_progress ? "<continuing>" : conn->c_sasl_bind_mech.bv_val,
1038 cred ? cred->bv_len : 0 ));
1040 Debug(LDAP_DEBUG_ARGS,
1041 "==> sasl_bind: dn=\"%s\" mech=%s datalen=%ld\n",
1042 dn->bv_len ? dn->bv_val : "",
1043 conn->c_sasl_bind_in_progress ? "<continuing>":conn->c_sasl_bind_mech.bv_val,
1044 cred ? cred->bv_len : 0 );
1049 send_ldap_result( conn, op, LDAP_UNAVAILABLE,
1050 NULL, "SASL unavailable on this session", NULL, NULL );
1054 #if SASL_VERSION_MAJOR >= 2
1055 #define START( ctx, mech, cred, clen, resp, rlen, err ) \
1056 sasl_server_start( ctx, mech, cred, clen, resp, rlen )
1057 #define STEP( ctx, cred, clen, resp, rlen, err ) \
1058 sasl_server_step( ctx, cred, clen, resp, rlen )
1060 #define START( ctx, mech, cred, clen, resp, rlen, err ) \
1061 sasl_server_start( ctx, mech, cred, clen, resp, rlen, err )
1062 #define STEP( ctx, cred, clen, resp, rlen, err ) \
1063 sasl_server_step( ctx, cred, clen, resp, rlen, err )
1066 if ( !conn->c_sasl_bind_in_progress ) {
1068 conn->c_sasl_bind_mech.bv_val,
1069 cred->bv_len ? cred->bv_val : "",
1071 (SASL_CONST char **)&response.bv_val, &reslen, &errstr );
1075 cred->bv_val, cred->bv_len,
1076 (SASL_CONST char **)&response.bv_val, &reslen, &errstr );
1079 response.bv_len = reslen;
1081 if ( sc == SASL_OK ) {
1082 char *username = NULL;
1084 sc = sasl_getprop( ctx,
1085 SASL_USERNAME, (SASL_CONST void **)&username );
1087 if ( sc != SASL_OK ) {
1089 LDAP_LOG(( "sasl", LDAP_LEVEL_ERR,
1090 "slap_sasl_bind: getprop(USERNAME) failed: %d\n", sc ));
1092 Debug(LDAP_DEBUG_TRACE,
1093 "slap_sasl_bind: getprop(USERNAME) failed!\n",
1098 send_ldap_result( conn, op, rc = slap_sasl_err2ldap( sc ),
1099 NULL, "no SASL username", NULL, NULL );
1102 sasl_ssf_t *ssf = NULL;
1105 ber_str2bv( username, 0, 1, edn );
1107 (void) sasl_getprop( ctx, SASL_SSF, (void *)&ssf );
1108 *ssfp = ssf ? *ssf : 0;
1111 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1112 conn->c_sasl_layers++;
1113 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1116 send_ldap_sasl( conn, op, rc,
1117 NULL, NULL, NULL, NULL,
1118 response.bv_len ? &response : NULL );
1121 } else if ( sc == SASL_CONTINUE ) {
1122 send_ldap_sasl( conn, op, rc = LDAP_SASL_BIND_IN_PROGRESS,
1123 NULL, NULL, NULL, NULL, &response );
1126 #if SASL_VERSION_MAJOR >= 2
1127 errstr = sasl_errdetail( ctx );
1129 send_ldap_result( conn, op, rc = slap_sasl_err2ldap( sc ),
1130 NULL, errstr, NULL, NULL );
1133 #if SASL_VERSION_MAJOR < 2
1134 if( response.bv_len ) {
1135 ch_free( response.bv_val );
1140 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
1141 "slap_sasl_bind: rc=%d\n", rc ));
1143 Debug(LDAP_DEBUG_TRACE, "<== slap_sasl_bind: rc=%d\n", rc, 0, 0);
1148 send_ldap_result( conn, op, rc = LDAP_UNAVAILABLE,
1149 NULL, "SASL not supported", NULL, NULL );
1155 char* slap_sasl_secprops( const char *in )
1157 #ifdef HAVE_CYRUS_SASL
1158 int rc = ldap_pvt_sasl_secprops( in, &sasl_secprops );
1160 return rc == LDAP_SUCCESS ? NULL : "Invalid security properties";
1162 return "SASL not supported";