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
23 #if SASL_VERSION_MAJOR >= 2
25 #define SASL_CONST const
36 /* Flags for telling slap_sasl_getdn() what type of identity is being passed */
37 #define FLAG_GETDN_FINAL 1
38 #define FLAG_GETDN_AUTHCID 2
39 #define FLAG_GETDN_AUTHZID 4
41 static sasl_security_properties_t sasl_secprops;
49 Connection *conn = context;
53 if ( message == NULL ) {
58 #if SASL_VERSION_MAJOR >= 2
60 level = LDAP_DEBUG_NONE;
64 level = LDAP_DEBUG_ANY;
68 level = LDAP_DEBUG_ANY;
72 level = LDAP_DEBUG_TRACE;
76 level = LDAP_DEBUG_TRACE;
80 level = LDAP_DEBUG_TRACE;
84 level = LDAP_DEBUG_TRACE;
88 level = LDAP_DEBUG_TRACE;
89 label = "Password Trace";
93 level = LDAP_DEBUG_ANY;
96 case SASL_LOG_WARNING:
97 level = LDAP_DEBUG_TRACE;
101 level = LDAP_DEBUG_TRACE;
106 return SASL_BADPARAM;
110 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
111 "SASL [conn=%ld] %s: %s\n",
112 conn ? conn->c_connid : -1,
115 Debug( level, "SASL [conn=%ld] %s: %s\n",
116 conn ? conn->c_connid: -1,
125 /* Take any sort of identity string and return a DN with the "dn:" prefix. The
126 string returned in *dn is in its own allocated memory, and must be free'd
127 by the calling process.
128 -Mark Adamson, Carnegie Mellon
134 static struct berval ext_bv = { sizeof("EXTERNAL")-1, "EXTERNAL" };
136 int slap_sasl_getdn( Connection *conn, char *id, char *user_realm, struct berval *dn, int flags )
139 int rc, len, is_dn = 0;
144 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
145 "slap_sasl_getdn: conn %d id=%s\n",
146 conn ? conn->c_connid : -1,
147 id ? (*id ? id : "<empty>") : "NULL" ));
149 Debug( LDAP_DEBUG_ARGS, "slap_sasl_getdn: id=%s\n",
150 id?(*id?id:"<empty>"):"NULL",0,0 );
156 /* Blatantly anonymous ID */
158 ( id[sizeof( "anonymous" )-1] == '\0'
159 || id[sizeof( "anonymous" )-1] == '@' ) &&
160 !strncasecmp( id, "anonymous", sizeof( "anonymous" )-1) ) {
161 return( LDAP_SUCCESS );
163 ctx = conn->c_sasl_context;
166 /* An authcID needs to be converted to authzID form */
167 if( flags & FLAG_GETDN_AUTHCID ) {
168 if( sasl_external_x509dn_convert
169 && conn->c_sasl_bind_mech.bv_len == ext_bv.bv_len
170 && ( strcasecmp( ext_bv.bv_val, conn->c_sasl_bind_mech.bv_val ) == 0 )
173 /* check SASL external for X.509 style DN and */
174 /* convert to dn:<dn> form */
175 dn->bv_val = ldap_dcedn2dn( id );
176 dn->bv_len = strlen(dn->bv_val);
180 /* convert to u:<username> form */
181 ber_str2bv( id, len, 1, dn );
186 if( !strncasecmp( id, "u:", sizeof("u:")-1 )) {
188 ber_str2bv( id+2, len-2, 1, dn );
189 } else if ( !strncasecmp( id, "dn:", sizeof("dn:")-1) ) {
191 ber_str2bv( id+3, len-3, 1, dn );
195 /* An authzID must be properly prefixed */
196 if( (flags & FLAG_GETDN_AUTHZID) && !is_dn ) {
200 return( LDAP_INAPPROPRIATE_AUTH );
203 /* Username strings */
204 if( is_dn == SET_U ) {
206 len = dn->bv_len + sizeof("uid=")-1 + sizeof(",cn=auth")-1;
208 if( user_realm && *user_realm ) {
209 len += strlen( user_realm ) + sizeof(",cn=")-1;
212 if( conn->c_sasl_bind_mech.bv_len ) {
213 len += conn->c_sasl_bind_mech.bv_len + sizeof(",cn=")-1;
216 /* Build the new dn */
218 dn->bv_val = ch_malloc( len );
219 p = slap_strcopy( dn->bv_val, "uid=" );
220 p = slap_strcopy( p, c1 );
224 p = slap_strcopy( p, ",cn=" );
225 p = slap_strcopy( p, user_realm );
227 if( conn->c_sasl_bind_mech.bv_len ) {
228 p = slap_strcopy( p, ",cn=" );
229 p = slap_strcopy( p, conn->c_sasl_bind_mech.bv_val );
231 p = slap_strcopy( p, ",cn=auth" );
232 dn->bv_len = p - dn->bv_val;
236 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
237 "slap_sasl_getdn: u:id converted to %s.\n", dn->bv_val ));
239 Debug( LDAP_DEBUG_TRACE, "getdn: u:id converted to %s\n", dn->bv_val,0,0 );
243 /* DN strings that are a cn=auth identity to run through regexp */
244 if( is_dn == SET_DN && ( ( flags & FLAG_GETDN_FINAL ) == 0 ) )
246 slap_sasl2dn( dn, &dn2 );
248 ch_free( dn->bv_val );
251 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
252 "slap_sasl_getdn: dn:id converted to %s.\n", dn->bv_val ));
254 Debug( LDAP_DEBUG_TRACE, "getdn: dn:id converted to %s\n",
260 if( flags & FLAG_GETDN_FINAL ) {
261 /* omit "dn:" prefix */
264 rc = dnNormalize2( NULL, dn, &dn2 );
266 if ( rc != LDAP_SUCCESS ) {
273 /* Attach the "dn:" prefix if needed */
274 if ( is_dn == SET_DN ) {
275 c1 = ch_malloc( dn->bv_len + sizeof("dn:") );
277 strcpy( c1 + 3, dn->bv_val );
283 return( LDAP_SUCCESS );
286 #if SASL_VERSION_MAJOR >= 2
291 const char *username,
294 struct propctx *propctx)
296 Connection *conn = (Connection *)context;
297 struct berval dn, cred;
301 cred.bv_val = (char *)pass;
302 cred.bv_len = passlen;
304 /* XXX can we do both steps at once? */
305 rc = slap_sasl_getdn( conn, (char *)username, NULL, &dn,
306 FLAG_GETDN_AUTHCID | FLAG_GETDN_FINAL );
307 if ( rc != LDAP_SUCCESS ) {
308 sasl_seterror( sconn, 0, ldap_err2string( rc ) );
312 if ( dn.bv_len == 0 ) {
313 sasl_seterror( sconn, 0, "No password is associated with the Root DSE" );
314 if ( dn.bv_val != NULL ) {
315 ch_free( dn.bv_val );
320 rc = backend_attribute( NULL, NULL, NULL, NULL, &dn,
321 slap_schema.si_ad_userPassword, &vals);
322 if ( rc != LDAP_SUCCESS ) {
323 ch_free( dn.bv_val );
324 sasl_seterror( sconn, 0, ldap_err2string( rc ) );
325 return SASL_NOVERIFY;
330 if ( vals != NULL ) {
331 for ( bv = vals; bv->bv_val != NULL; bv++ ) {
332 if ( !lutil_passwd( bv, &cred, NULL ) ) {
337 ber_bvarray_free( vals );
340 if ( rc != SASL_OK ) {
341 sasl_seterror( sconn, 0,
342 ldap_err2string( LDAP_INVALID_CREDENTIALS ) );
345 ch_free( dn.bv_val );
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, (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",
421 const char *requested_user,
423 const char *auth_identity,
425 const char *def_realm,
427 struct propctx *propctx)
429 Connection *conn = (Connection *)context;
430 struct berval authcDN, authzDN;
433 authcDN.bv_val = (char *)auth_identity;
434 authcDN.bv_len = alen;
436 authzDN.bv_val = (char *)requested_user;
437 authzDN.bv_len = rlen;
440 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
441 "slap_sasl_authorize: conn %d authcDN=\"%s\" authzDN=\"%s\"\n",
442 conn ? conn->c_connid : -1, authcDN.bv_val, authzDN.bv_val));
444 Debug( LDAP_DEBUG_ARGS, "SASL Authorize [conn=%ld]: "
445 "authcDN=\"%s\" authzDN=\"%s\"\n",
446 conn ? conn->c_connid : -1, authcDN.bv_val, authzDN.bv_val );
449 rc = slap_sasl_authorized( &authcDN, &authzDN );
450 if ( rc != LDAP_SUCCESS ) {
452 LDAP_LOG(( "sasl", LDAP_LEVEL_INFO,
453 "slap_sasl_authorize: conn %ld authorization disallowed (%d)\n",
454 (long)(conn ? conn->c_connid : -1), rc ));
456 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
457 " authorization disallowed (%d)\n",
458 (long) (conn ? conn->c_connid : -1), rc, 0 );
461 sasl_seterror( sconn, 0, "not authorized" );
466 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
467 "slap_sasl_authorize: conn %d authorization allowed\n",
468 (long)(conn ? conn->c_connid : -1 ) ));
470 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
471 " authorization allowed\n",
472 (long) (conn ? conn->c_connid : -1), 0, 0 );
486 struct berval authcDN, authzDN;
488 Connection *conn = context;
494 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
495 "slap_sasl_authorize: conn %d authcid=\"%s\" authzid=\"%s\"\n",
496 conn ? conn->c_connid : -1,
497 authcid ? authcid : "<empty>",
498 authzid ? authzid : "<empty>" ));
500 Debug( LDAP_DEBUG_ARGS, "SASL Authorize [conn=%ld]: "
501 "authcid=\"%s\" authzid=\"%s\"\n",
502 (long) (conn ? conn->c_connid : -1),
503 authcid ? authcid : "<empty>",
504 authzid ? authzid : "<empty>" );
507 /* Convert the identities to DN's. If no authzid was given, client will
508 be bound as the DN matching their username */
509 rc = slap_sasl_getdn( conn, (char *)authcid, realm, &authcDN, FLAG_GETDN_AUTHCID );
510 if( rc != LDAP_SUCCESS ) {
511 *errstr = ldap_err2string( rc );
514 if( ( authzid == NULL ) || !strcmp( authcid,authzid ) ) {
516 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
517 "slap_sasl_authorize: conn %d Using authcDN=%s\n",
518 conn ? conn->c_connid : -1, authcDN.bv_val ));
520 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
521 "Using authcDN=%s\n", (long) (conn ? conn->c_connid : -1), authcDN.bv_val,0 );
524 *user = authcDN.bv_val;
528 rc = slap_sasl_getdn( conn, (char *)authzid, realm, &authzDN, FLAG_GETDN_AUTHZID );
529 if( rc != LDAP_SUCCESS ) {
530 ch_free( authcDN.bv_val );
531 *errstr = ldap_err2string( rc );
535 rc = slap_sasl_authorized( &authcDN, &authzDN );
538 LDAP_LOG(( "sasl", LDAP_LEVEL_INFO,
539 "slap_sasl_authorize: conn %ld authorization disallowed (%d)\n",
540 (long)(conn ? conn->c_connid : -1), rc ));
542 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
543 " authorization disallowed (%d)\n",
544 (long) (conn ? conn->c_connid : -1), rc, 0 );
547 *errstr = "not authorized";
548 ch_free( authcDN.bv_val );
549 ch_free( authzDN.bv_val );
554 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
555 "slap_sasl_authorize: conn %d authorization allowed\n",
556 (long)(conn ? conn->c_connid : -1 ) ));
558 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
559 " authorization allowed\n",
560 (long) (conn ? conn->c_connid : -1), 0, 0 );
564 ch_free( authcDN.bv_val );
565 *user = authzDN.bv_val;
569 #endif /* SASL_VERSION_MAJOR >= 2 */
572 slap_sasl_err2ldap( int saslerr )
578 rc = LDAP_SASL_BIND_IN_PROGRESS;
587 rc = LDAP_AUTH_METHOD_NOT_SUPPORTED;
590 rc = LDAP_INVALID_CREDENTIALS;
593 rc = LDAP_INSUFFICIENT_ACCESS;
597 rc = LDAP_INAPPROPRIATE_AUTH;
609 int slap_sasl_init( void )
611 #ifdef HAVE_CYRUS_SASL
613 static sasl_callback_t server_callbacks[] = {
614 { SASL_CB_LOG, &slap_sasl_log, NULL },
615 { SASL_CB_LIST_END, NULL, NULL }
625 ldap_pvt_sasl_mutex_new,
626 ldap_pvt_sasl_mutex_lock,
627 ldap_pvt_sasl_mutex_unlock,
628 ldap_pvt_sasl_mutex_dispose );
630 /* should provide callbacks for logging */
631 /* server name should be configurable */
632 rc = sasl_server_init( server_callbacks, "slapd" );
634 if( rc != SASL_OK ) {
636 LDAP_LOG(( "sasl", LDAP_LEVEL_INFO,
637 "slap_sasl_init: init failed.\n" ));
639 Debug( LDAP_DEBUG_ANY, "sasl_server_init failed\n",
647 LDAP_LOG(( "sasl", LDAP_LEVEL_INFO,
648 "slap_sasl_init: initialized!\n"));
650 Debug( LDAP_DEBUG_TRACE, "slap_sasl_init: initialized!\n",
655 /* default security properties */
656 memset( &sasl_secprops, '\0', sizeof(sasl_secprops) );
657 sasl_secprops.max_ssf = INT_MAX;
658 sasl_secprops.maxbufsize = 65536;
659 sasl_secprops.security_flags = SASL_SEC_NOPLAINTEXT|SASL_SEC_NOANONYMOUS;
665 int slap_sasl_destroy( void )
667 #ifdef HAVE_CYRUS_SASL
676 int slap_sasl_open( Connection *conn )
678 int sc = LDAP_SUCCESS;
679 #if SASL_VERSION_MAJOR >= 2
680 char *ipremoteport = NULL, *iplocalport = NULL;
683 #ifdef HAVE_CYRUS_SASL
684 sasl_conn_t *ctx = NULL;
685 sasl_callback_t *session_callbacks;
687 assert( conn->c_sasl_context == NULL );
688 assert( conn->c_sasl_extra == NULL );
690 conn->c_sasl_layers = 0;
693 #if SASL_VERSION_MAJOR >= 2
694 ch_calloc( 5, sizeof(sasl_callback_t));
696 ch_calloc( 3, sizeof(sasl_callback_t));
698 conn->c_sasl_extra = session_callbacks;
700 session_callbacks[0].id = SASL_CB_LOG;
701 session_callbacks[0].proc = &slap_sasl_log;
702 session_callbacks[0].context = conn;
704 session_callbacks[1].id = SASL_CB_PROXY_POLICY;
705 session_callbacks[1].proc = &slap_sasl_authorize;
706 session_callbacks[1].context = conn;
708 #if SASL_VERSION_MAJOR >= 2
709 session_callbacks[2].id = SASL_CB_CANON_USER;
710 session_callbacks[2].proc = &slap_sasl_canonicalize;
711 session_callbacks[2].context = conn;
713 session_callbacks[3].id = SASL_CB_SERVER_USERDB_CHECKPASS;
714 session_callbacks[3].proc = &slap_sasl_checkpass;
715 session_callbacks[3].context = conn;
717 session_callbacks[4].id = SASL_CB_LIST_END;
718 session_callbacks[4].proc = NULL;
719 session_callbacks[4].context = NULL;
721 session_callbacks[2].id = SASL_CB_LIST_END;
722 session_callbacks[2].proc = NULL;
723 session_callbacks[2].context = NULL;
726 if( global_host == NULL ) {
727 global_host = ldap_pvt_get_fqdn( NULL );
730 /* create new SASL context */
731 #if SASL_VERSION_MAJOR >= 2
732 if ( conn->c_sock_name.bv_len != 0 &&
733 strncmp( conn->c_sock_name.bv_val, "IP=", 3 ) == 0) {
736 iplocalport = ch_strdup( conn->c_sock_name.bv_val + 3 );
737 /* Convert IPv6 addresses to address;port syntax. */
738 p = strrchr( iplocalport, ' ' );
743 if ( conn->c_peer_name.bv_len != 0 &&
744 strncmp( conn->c_peer_name.bv_val, "IP=", 3 ) == 0) {
747 ipremoteport = ch_strdup( conn->c_peer_name.bv_val + 3 );
748 /* Convert IPv6 addresses to address;port syntax. */
749 p = strrchr( ipremoteport, ' ' );
754 sc = sasl_server_new( "ldap", global_host, global_realm,
755 iplocalport, ipremoteport, session_callbacks, 0, &ctx );
756 if ( iplocalport != NULL ) {
757 ch_free( iplocalport );
759 if ( ipremoteport != NULL ) {
760 ch_free( ipremoteport );
763 sc = sasl_server_new( "ldap", global_host, global_realm,
764 session_callbacks, SASL_SECURITY_LAYER, &ctx );
767 if( sc != SASL_OK ) {
769 LDAP_LOG(( "sasl", LDAP_LEVEL_ERR,
770 "slap_sasl_open: sasl_server_new failed: %d\n", sc ));
772 Debug( LDAP_DEBUG_ANY, "sasl_server_new failed: %d\n",
779 conn->c_sasl_context = ctx;
781 if( sc == SASL_OK ) {
782 sc = sasl_setprop( ctx,
783 SASL_SEC_PROPS, &sasl_secprops );
785 if( sc != SASL_OK ) {
787 LDAP_LOG(( "sasl", LDAP_LEVEL_ERR,
788 "slap_sasl_open: sasl_setprop failed: %d \n", sc ));
790 Debug( LDAP_DEBUG_ANY, "sasl_setprop failed: %d\n",
794 slap_sasl_close( conn );
799 sc = slap_sasl_err2ldap( sc );
804 int slap_sasl_external(
807 const char *auth_id )
809 #if SASL_VERSION_MAJOR >= 2
811 sasl_conn_t *ctx = conn->c_sasl_context;
814 return LDAP_UNAVAILABLE;
817 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL, &ssf );
819 if ( sc != SASL_OK ) {
823 sc = sasl_setprop( ctx, SASL_AUTH_EXTERNAL, auth_id );
825 if ( sc != SASL_OK ) {
828 #elif defined(HAVE_CYRUS_SASL)
830 sasl_conn_t *ctx = conn->c_sasl_context;
831 sasl_external_properties_t extprops;
834 return LDAP_UNAVAILABLE;
837 memset( &extprops, '\0', sizeof(extprops) );
839 extprops.auth_id = (char *) auth_id;
841 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL,
842 (void *) &extprops );
844 if ( sc != SASL_OK ) {
852 int slap_sasl_reset( Connection *conn )
854 #ifdef HAVE_CYRUS_SASL
855 sasl_conn_t *ctx = conn->c_sasl_context;
860 /* must return "anonymous" */
864 char ** slap_sasl_mechs( Connection *conn )
868 #ifdef HAVE_CYRUS_SASL
869 sasl_conn_t *ctx = conn->c_sasl_context;
873 SASL_CONST char *mechstr;
875 sc = sasl_listmech( ctx,
876 NULL, NULL, ",", NULL,
877 &mechstr, NULL, NULL );
879 if( sc != SASL_OK ) {
881 LDAP_LOG(( "sasl", LDAP_LEVEL_ERR,
882 "slap_sasl_mechs: sasl_listmech failed: %d\n", sc ));
884 Debug( LDAP_DEBUG_ANY, "slap_sasl_listmech failed: %d\n",
891 mechs = str2charray( mechstr, "," );
893 #if SASL_VERSION_MAJOR < 2
902 int slap_sasl_close( Connection *conn )
904 #ifdef HAVE_CYRUS_SASL
905 sasl_conn_t *ctx = conn->c_sasl_context;
908 sasl_dispose( &ctx );
911 conn->c_sasl_context = NULL;
913 free( conn->c_sasl_extra );
914 conn->c_sasl_extra = NULL;
931 #ifdef HAVE_CYRUS_SASL
932 sasl_conn_t *ctx = conn->c_sasl_context;
933 struct berval response;
935 const char *errstr = NULL;
939 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
940 "sasl_bind: conn %ld dn=\"%s\" mech=%s datalen=%ld\n",
942 dn->bv_len ? dn->bv_val : "",
943 conn->c_sasl_bind_in_progress ? "<continuing>" : conn->c_sasl_bind_mech.bv_val,
944 cred ? cred->bv_len : 0 ));
946 Debug(LDAP_DEBUG_ARGS,
947 "==> sasl_bind: dn=\"%s\" mech=%s datalen=%ld\n",
948 dn->bv_len ? dn->bv_val : "",
949 conn->c_sasl_bind_in_progress ? "<continuing>":conn->c_sasl_bind_mech.bv_val,
950 cred ? cred->bv_len : 0 );
955 send_ldap_result( conn, op, LDAP_UNAVAILABLE,
956 NULL, "SASL unavailable on this session", NULL, NULL );
960 #if SASL_VERSION_MAJOR >= 2
961 #define START( ctx, mech, cred, clen, resp, rlen, err ) \
962 sasl_server_start( ctx, mech, cred, clen, resp, rlen )
963 #define STEP( ctx, cred, clen, resp, rlen, err ) \
964 sasl_server_step( ctx, cred, clen, resp, rlen )
966 #define START( ctx, mech, cred, clen, resp, rlen, err ) \
967 sasl_server_start( ctx, mech, cred, clen, resp, rlen, err )
968 #define STEP( ctx, cred, clen, resp, rlen, err ) \
969 sasl_server_step( ctx, cred, clen, resp, rlen, err )
972 if ( !conn->c_sasl_bind_in_progress ) {
974 conn->c_sasl_bind_mech.bv_val,
975 cred->bv_len ? cred->bv_val : "",
977 (SASL_CONST char **)&response.bv_val, &reslen, &errstr );
981 cred->bv_val, cred->bv_len,
982 (SASL_CONST char **)&response.bv_val, &reslen, &errstr );
985 response.bv_len = reslen;
987 if ( sc == SASL_OK ) {
988 char *username = NULL;
991 #if SASL_VERSION_MAJOR >= 2
992 sc = sasl_getprop( ctx, SASL_DEFUSERREALM, (const void **)&realm );
994 sc = sasl_getprop( ctx, SASL_REALM, (void **)&realm );
996 sc = sasl_getprop( ctx,
997 SASL_USERNAME, (SASL_CONST void **)&username );
999 if ( sc != SASL_OK ) {
1001 LDAP_LOG(( "sasl", LDAP_LEVEL_ERR,
1002 "slap_sasl_bind: getprop(USERNAME) failed: %d\n", sc ));
1004 Debug(LDAP_DEBUG_TRACE,
1005 "slap_sasl_bind: getprop(USERNAME) failed!\n",
1010 send_ldap_result( conn, op, rc = slap_sasl_err2ldap( sc ),
1011 NULL, "no SASL username", NULL, NULL );
1014 rc = slap_sasl_getdn( conn, username, realm, edn, FLAG_GETDN_FINAL );
1016 if( rc == LDAP_SUCCESS ) {
1017 sasl_ssf_t *ssf = NULL;
1018 (void) sasl_getprop( ctx, SASL_SSF, (void *)&ssf );
1019 *ssfp = ssf ? *ssf : 0;
1022 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1023 conn->c_sasl_layers++;
1024 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1027 send_ldap_sasl( conn, op, rc,
1028 NULL, NULL, NULL, NULL,
1029 response.bv_len ? &response : NULL );
1032 send_ldap_result( conn, op, rc,
1033 NULL, errstr, NULL, NULL );
1037 } else if ( sc == SASL_CONTINUE ) {
1038 send_ldap_sasl( conn, op, rc = LDAP_SASL_BIND_IN_PROGRESS,
1039 NULL, NULL, NULL, NULL, &response );
1042 send_ldap_result( conn, op, rc = slap_sasl_err2ldap( sc ),
1043 NULL, errstr, NULL, NULL );
1046 #if SASL_VERSION_MAJOR < 2
1047 if( response.bv_len ) {
1048 ch_free( response.bv_val );
1053 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
1054 "slap_sasl_bind: rc=%d\n", rc ));
1056 Debug(LDAP_DEBUG_TRACE, "<== slap_sasl_bind: rc=%d\n", rc, 0, 0);
1061 send_ldap_result( conn, op, rc = LDAP_UNAVAILABLE,
1062 NULL, "SASL not supported", NULL, NULL );
1068 char* slap_sasl_secprops( const char *in )
1070 #ifdef HAVE_CYRUS_SASL
1071 int rc = ldap_pvt_sasl_secprops( in, &sasl_secprops );
1073 return rc == LDAP_SUCCESS ? NULL : "Invalid security properties";
1075 return "SASL not supported";