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_FINAL 1
42 #define FLAG_GETDN_AUTHCID 2
43 #define FLAG_GETDN_AUTHZID 4
45 static sasl_security_properties_t sasl_secprops;
53 Connection *conn = context;
57 if ( message == NULL ) {
62 #if SASL_VERSION_MAJOR >= 2
64 level = LDAP_DEBUG_NONE;
68 level = LDAP_DEBUG_ANY;
72 level = LDAP_DEBUG_ANY;
76 level = LDAP_DEBUG_TRACE;
80 level = LDAP_DEBUG_TRACE;
84 level = LDAP_DEBUG_TRACE;
88 level = LDAP_DEBUG_TRACE;
92 level = LDAP_DEBUG_TRACE;
93 label = "Password Trace";
97 level = LDAP_DEBUG_ANY;
100 case SASL_LOG_WARNING:
101 level = LDAP_DEBUG_TRACE;
105 level = LDAP_DEBUG_TRACE;
110 return SASL_BADPARAM;
114 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
115 "SASL [conn=%ld] %s: %s\n",
116 conn ? conn->c_connid : -1,
119 Debug( level, "SASL [conn=%ld] %s: %s\n",
120 conn ? conn->c_connid: -1,
129 /* Take any sort of identity string and return a DN with the "dn:" prefix. The
130 string returned in *dn is in its own allocated memory, and must be free'd
131 by the calling process.
132 -Mark Adamson, Carnegie Mellon
138 static struct berval ext_bv = { sizeof("EXTERNAL")-1, "EXTERNAL" };
140 int slap_sasl_getdn( Connection *conn, char *id,
141 char *user_realm, struct berval *dn, int flags )
144 int rc, len, is_dn = 0;
149 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
150 "slap_sasl_getdn: conn %d id=%s\n",
151 conn ? conn->c_connid : -1,
152 id ? (*id ? id : "<empty>") : "NULL" ));
154 Debug( LDAP_DEBUG_ARGS, "slap_sasl_getdn: id=%s\n",
155 id?(*id?id:"<empty>"):"NULL",0,0 );
161 /* Blatantly anonymous ID */
163 ( id[sizeof( "anonymous" )-1] == '\0'
164 || id[sizeof( "anonymous" )-1] == '@' ) &&
165 !strncasecmp( id, "anonymous", sizeof( "anonymous" )-1) ) {
166 return( LDAP_SUCCESS );
168 ctx = conn->c_sasl_context;
171 /* An authcID needs to be converted to authzID form */
172 if( flags & FLAG_GETDN_AUTHCID ) {
173 if( sasl_external_x509dn_convert
174 && conn->c_sasl_bind_mech.bv_len == ext_bv.bv_len
175 && ( strcasecmp( ext_bv.bv_val, conn->c_sasl_bind_mech.bv_val ) == 0 )
178 /* check SASL external for X.509 style DN and */
179 /* convert to dn:<dn> form */
180 dn->bv_val = ldap_dcedn2dn( id );
181 dn->bv_len = strlen(dn->bv_val);
185 /* convert to u:<username> form */
186 ber_str2bv( id, len, 1, dn );
191 if( !strncasecmp( id, "u:", sizeof("u:")-1 )) {
193 ber_str2bv( id+2, len-2, 1, dn );
194 } else if ( !strncasecmp( id, "dn:", sizeof("dn:")-1) ) {
196 ber_str2bv( id+3, len-3, 1, dn );
200 /* An authzID must be properly prefixed */
201 if( (flags & FLAG_GETDN_AUTHZID) && !is_dn ) {
205 return( LDAP_INAPPROPRIATE_AUTH );
208 /* Username strings */
209 if( is_dn == SET_U ) {
211 len = dn->bv_len + sizeof("uid=")-1 + sizeof(",cn=auth")-1;
213 if( user_realm && *user_realm ) {
214 len += strlen( user_realm ) + sizeof(",cn=")-1;
217 if( conn->c_sasl_bind_mech.bv_len ) {
218 len += conn->c_sasl_bind_mech.bv_len + sizeof(",cn=")-1;
221 /* Build the new dn */
223 dn->bv_val = ch_malloc( len+1 );
224 p = slap_strcopy( dn->bv_val, "uid=" );
225 p = slap_strcopy( p, c1 );
228 if( user_realm && *user_realm ) {
229 p = slap_strcopy( p, ",cn=" );
230 p = slap_strcopy( p, user_realm );
232 if( conn->c_sasl_bind_mech.bv_len ) {
233 p = slap_strcopy( p, ",cn=" );
234 p = slap_strcopy( p, conn->c_sasl_bind_mech.bv_val );
236 p = slap_strcopy( p, ",cn=auth" );
237 dn->bv_len = p - dn->bv_val;
241 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
242 "slap_sasl_getdn: u:id converted to %s.\n", dn->bv_val ));
244 Debug( LDAP_DEBUG_TRACE, "getdn: u:id converted to %s\n", dn->bv_val,0,0 );
248 /* DN strings that are a cn=auth identity to run through regexp */
249 if( is_dn == SET_DN && ( ( flags & FLAG_GETDN_FINAL ) == 0 ) )
251 slap_sasl2dn( dn, &dn2 );
253 ch_free( dn->bv_val );
256 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
257 "slap_sasl_getdn: dn:id converted to %s.\n", dn->bv_val ));
259 Debug( LDAP_DEBUG_TRACE, "getdn: dn:id converted to %s\n",
265 if( flags & FLAG_GETDN_FINAL ) {
266 /* omit "dn:" prefix */
269 rc = dnNormalize2( NULL, dn, &dn2 );
271 if ( rc != LDAP_SUCCESS ) {
278 /* Attach the "dn:" prefix if needed */
279 if ( is_dn == SET_DN ) {
280 c1 = ch_malloc( dn->bv_len + sizeof("dn:") );
282 strcpy( c1 + 3, dn->bv_val );
288 return( LDAP_SUCCESS );
291 #if SASL_VERSION_MAJOR >= 2
296 const char *username,
299 struct propctx *propctx)
301 Connection *conn = (Connection *)context;
302 struct berval dn, cred;
306 cred.bv_val = (char *)pass;
307 cred.bv_len = passlen;
309 /* XXX do we need to check sasldb as well? */
311 /* XXX can we do both steps at once? */
312 rc = slap_sasl_getdn( conn, (char *)username, NULL, &dn,
313 FLAG_GETDN_AUTHCID | FLAG_GETDN_FINAL );
314 if ( rc != LDAP_SUCCESS ) {
315 sasl_seterror( sconn, 0, ldap_err2string( rc ) );
319 if ( dn.bv_len == 0 ) {
320 sasl_seterror( sconn, 0,
321 "No password is associated with the Root DSE" );
322 if ( dn.bv_val != NULL ) {
323 ch_free( dn.bv_val );
328 rc = backend_attribute( NULL, NULL, NULL, NULL, &dn,
329 slap_schema.si_ad_userPassword, &vals);
330 if ( rc != LDAP_SUCCESS ) {
331 ch_free( dn.bv_val );
332 sasl_seterror( sconn, 0, ldap_err2string( rc ) );
333 return SASL_NOVERIFY;
338 if ( vals != NULL ) {
339 for ( bv = vals; bv->bv_val != NULL; bv++ ) {
340 if ( !lutil_passwd( bv, &cred, NULL ) ) {
345 ber_bvarray_free( vals );
348 if ( rc != SASL_OK ) {
349 sasl_seterror( sconn, 0,
350 ldap_err2string( LDAP_INVALID_CREDENTIALS ) );
353 ch_free( dn.bv_val );
359 slap_sasl_canonicalize(
365 const char *user_realm,
370 Connection *conn = (Connection *)context;
377 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
378 "slap_sasl_canonicalize: conn %d %s=\"%s\"\n",
379 conn ? conn->c_connid : -1,
380 (flags == SASL_CU_AUTHID) ? "authcid" : "authzid",
381 in ? in : "<empty>" ));
383 Debug( LDAP_DEBUG_ARGS, "SASL Canonicalize [conn=%ld]: "
385 conn ? conn->c_connid : -1,
386 (flags == SASL_CU_AUTHID) ? "authcid" : "authzid",
387 in ? in : "<empty>" );
390 rc = slap_sasl_getdn( conn, (char *)in, (char *)user_realm, &dn,
391 (flags == SASL_CU_AUTHID) ? FLAG_GETDN_AUTHCID : FLAG_GETDN_AUTHZID );
392 if ( rc != LDAP_SUCCESS ) {
393 sasl_seterror( sconn, 0, ldap_err2string( rc ) );
397 if ( out_max < dn.bv_len ) {
401 AC_MEMCPY( out, dn.bv_val, dn.bv_len );
402 out[dn.bv_len] = '\0';
404 *out_len = dn.bv_len;
406 ch_free( dn.bv_val );
409 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
410 "slap_sasl_canonicalize: conn %d %s=\"%s\"\n",
411 conn ? conn->c_connid : -1,
412 (flags == SASL_CU_AUTHID) ? "authcDN" : "authzDN",
415 Debug( LDAP_DEBUG_ARGS, "SASL Canonicalize [conn=%ld]: "
417 conn ? conn->c_connid : -1,
418 (flags == SASL_CU_AUTHID) ? "authcDN" : "authzDN",
429 const char *requested_user,
431 const char *auth_identity,
433 const char *def_realm,
435 struct propctx *propctx)
437 Connection *conn = (Connection *)context;
438 struct berval authcDN, authzDN;
441 authcDN.bv_val = (char *)auth_identity;
442 authcDN.bv_len = alen;
444 authzDN.bv_val = (char *)requested_user;
445 authzDN.bv_len = rlen;
448 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
449 "slap_sasl_authorize: conn %d authcDN=\"%s\" authzDN=\"%s\"\n",
450 conn ? conn->c_connid : -1, authcDN.bv_val, authzDN.bv_val));
452 Debug( LDAP_DEBUG_ARGS, "SASL Authorize [conn=%ld]: "
453 "authcDN=\"%s\" authzDN=\"%s\"\n",
454 conn ? conn->c_connid : -1, authcDN.bv_val, authzDN.bv_val );
457 rc = slap_sasl_authorized( &authcDN, &authzDN );
458 if ( rc != LDAP_SUCCESS ) {
460 LDAP_LOG(( "sasl", LDAP_LEVEL_INFO,
461 "slap_sasl_authorize: conn %ld authorization disallowed (%d)\n",
462 (long)(conn ? conn->c_connid : -1), rc ));
464 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
465 " authorization disallowed (%d)\n",
466 (long) (conn ? conn->c_connid : -1), rc, 0 );
469 sasl_seterror( sconn, 0, "not authorized" );
474 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
475 "slap_sasl_authorize: conn %d authorization allowed\n",
476 (long)(conn ? conn->c_connid : -1 ) ));
478 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
479 " authorization allowed\n",
480 (long) (conn ? conn->c_connid : -1), 0, 0 );
494 struct berval authcDN, authzDN;
496 Connection *conn = context;
502 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
503 "slap_sasl_authorize: conn %d authcid=\"%s\" authzid=\"%s\"\n",
504 conn ? conn->c_connid : -1,
505 authcid ? authcid : "<empty>",
506 authzid ? authzid : "<empty>" ));
508 Debug( LDAP_DEBUG_ARGS, "SASL Authorize [conn=%ld]: "
509 "authcid=\"%s\" authzid=\"%s\"\n",
510 (long) (conn ? conn->c_connid : -1),
511 authcid ? authcid : "<empty>",
512 authzid ? authzid : "<empty>" );
515 /* Figure out how much data we have for the dn */
516 rc = sasl_getprop( conn->c_sasl_context, SASL_REALM, (void **)&realm );
517 if( rc != SASL_OK && rc != SASL_NOTDONE ) {
519 LDAP_LOG(( "sasl", LDAP_LEVEL_ERR,
520 "slap_sasl_authorize: getprop(REALM) failed.\n" ));
522 Debug(LDAP_DEBUG_TRACE,
523 "authorize: getprop(REALM) failed!\n", 0,0,0);
525 *errstr = "Could not extract realm";
529 /* Convert the identities to DN's. If no authzid was given, client will
530 be bound as the DN matching their username */
531 rc = slap_sasl_getdn( conn, (char *)authcid, realm, &authcDN, FLAG_GETDN_AUTHCID );
532 if( rc != LDAP_SUCCESS ) {
533 *errstr = ldap_err2string( rc );
536 if( ( authzid == NULL ) || !strcmp( authcid,authzid ) ) {
538 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
539 "slap_sasl_authorize: conn %d Using authcDN=%s\n",
540 conn ? conn->c_connid : -1, authcDN.bv_val ));
542 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
543 "Using authcDN=%s\n", (long) (conn ? conn->c_connid : -1), authcDN.bv_val,0 );
546 *user = authcDN.bv_val;
550 rc = slap_sasl_getdn( conn, (char *)authzid, realm, &authzDN, FLAG_GETDN_AUTHZID );
551 if( rc != LDAP_SUCCESS ) {
552 ch_free( authcDN.bv_val );
553 *errstr = ldap_err2string( rc );
557 rc = slap_sasl_authorized( &authcDN, &authzDN );
560 LDAP_LOG(( "sasl", LDAP_LEVEL_INFO,
561 "slap_sasl_authorize: conn %ld authorization disallowed (%d)\n",
562 (long)(conn ? conn->c_connid : -1), rc ));
564 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
565 " authorization disallowed (%d)\n",
566 (long) (conn ? conn->c_connid : -1), rc, 0 );
569 *errstr = "not authorized";
570 ch_free( authcDN.bv_val );
571 ch_free( authzDN.bv_val );
576 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
577 "slap_sasl_authorize: conn %d authorization allowed\n",
578 (long)(conn ? conn->c_connid : -1 ) ));
580 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
581 " authorization allowed\n",
582 (long) (conn ? conn->c_connid : -1), 0, 0 );
586 ch_free( authcDN.bv_val );
587 *user = authzDN.bv_val;
591 #endif /* SASL_VERSION_MAJOR >= 2 */
594 slap_sasl_err2ldap( int saslerr )
600 rc = LDAP_SASL_BIND_IN_PROGRESS;
609 rc = LDAP_AUTH_METHOD_NOT_SUPPORTED;
612 rc = LDAP_INVALID_CREDENTIALS;
615 rc = LDAP_INSUFFICIENT_ACCESS;
619 rc = LDAP_INAPPROPRIATE_AUTH;
631 int slap_sasl_init( void )
633 #ifdef HAVE_CYRUS_SASL
635 static sasl_callback_t server_callbacks[] = {
636 { SASL_CB_LOG, &slap_sasl_log, NULL },
637 { SASL_CB_LIST_END, NULL, NULL }
647 ldap_pvt_sasl_mutex_new,
648 ldap_pvt_sasl_mutex_lock,
649 ldap_pvt_sasl_mutex_unlock,
650 ldap_pvt_sasl_mutex_dispose );
652 /* should provide callbacks for logging */
653 /* server name should be configurable */
654 rc = sasl_server_init( server_callbacks, "slapd" );
656 if( rc != SASL_OK ) {
658 LDAP_LOG(( "sasl", LDAP_LEVEL_INFO,
659 "slap_sasl_init: init failed.\n" ));
661 Debug( LDAP_DEBUG_ANY, "sasl_server_init failed\n",
669 LDAP_LOG(( "sasl", LDAP_LEVEL_INFO,
670 "slap_sasl_init: initialized!\n"));
672 Debug( LDAP_DEBUG_TRACE, "slap_sasl_init: initialized!\n",
677 /* default security properties */
678 memset( &sasl_secprops, '\0', sizeof(sasl_secprops) );
679 sasl_secprops.max_ssf = INT_MAX;
680 sasl_secprops.maxbufsize = 65536;
681 sasl_secprops.security_flags = SASL_SEC_NOPLAINTEXT|SASL_SEC_NOANONYMOUS;
687 int slap_sasl_destroy( void )
689 #ifdef HAVE_CYRUS_SASL
698 int slap_sasl_open( Connection *conn )
700 int cb, sc = LDAP_SUCCESS;
701 #if SASL_VERSION_MAJOR >= 2
702 char *ipremoteport = NULL, *iplocalport = NULL;
705 #ifdef HAVE_CYRUS_SASL
706 sasl_conn_t *ctx = NULL;
707 sasl_callback_t *session_callbacks;
709 assert( conn->c_sasl_context == NULL );
710 assert( conn->c_sasl_extra == NULL );
712 conn->c_sasl_layers = 0;
715 #if SASL_VERSION_MAJOR >= 2
716 ch_calloc( 5, sizeof(sasl_callback_t));
718 ch_calloc( 3, sizeof(sasl_callback_t));
720 conn->c_sasl_extra = session_callbacks;
722 session_callbacks[cb=0].id = SASL_CB_LOG;
723 session_callbacks[cb].proc = &slap_sasl_log;
724 session_callbacks[cb++].context = conn;
726 session_callbacks[cb].id = SASL_CB_PROXY_POLICY;
727 session_callbacks[cb].proc = &slap_sasl_authorize;
728 session_callbacks[cb++].context = conn;
730 #if SASL_VERSION_MAJOR >= 2
731 session_callbacks[cb].id = SASL_CB_CANON_USER;
732 session_callbacks[cb].proc = &slap_sasl_canonicalize;
733 session_callbacks[cb++].context = conn;
735 /* XXXX: this should be conditional */
736 session_callbacks[cb].id = SASL_CB_SERVER_USERDB_CHECKPASS;
737 session_callbacks[cb].proc = &slap_sasl_checkpass;
738 session_callbacks[cb++].context = conn;
741 session_callbacks[cb].id = SASL_CB_LIST_END;
742 session_callbacks[cb].proc = NULL;
743 session_callbacks[cb++].context = NULL;
745 if( global_host == NULL ) {
746 global_host = ldap_pvt_get_fqdn( NULL );
749 /* create new SASL context */
750 #if SASL_VERSION_MAJOR >= 2
751 if ( conn->c_sock_name.bv_len != 0 &&
752 strncmp( conn->c_sock_name.bv_val, "IP=", 3 ) == 0) {
755 iplocalport = ch_strdup( conn->c_sock_name.bv_val + 3 );
756 /* Convert IPv6 addresses to address;port syntax. */
757 p = strrchr( iplocalport, ' ' );
758 /* Convert IPv4 addresses to address;port syntax. */
759 if ( p == NULL ) p = strchr( iplocalport, ':' );
764 if ( conn->c_peer_name.bv_len != 0 &&
765 strncmp( conn->c_peer_name.bv_val, "IP=", 3 ) == 0) {
768 ipremoteport = ch_strdup( conn->c_peer_name.bv_val + 3 );
769 /* Convert IPv6 addresses to address;port syntax. */
770 p = strrchr( ipremoteport, ' ' );
771 /* Convert IPv4 addresses to address;port syntax. */
772 if ( p == NULL ) p = strchr( ipremoteport, ':' );
777 sc = sasl_server_new( "ldap", global_host, global_realm,
778 iplocalport, ipremoteport, session_callbacks, 0, &ctx );
779 if ( iplocalport != NULL ) {
780 ch_free( iplocalport );
782 if ( ipremoteport != NULL ) {
783 ch_free( ipremoteport );
786 sc = sasl_server_new( "ldap", global_host, global_realm,
787 session_callbacks, SASL_SECURITY_LAYER, &ctx );
790 if( sc != SASL_OK ) {
792 LDAP_LOG(( "sasl", LDAP_LEVEL_ERR,
793 "slap_sasl_open: sasl_server_new failed: %d\n", sc ));
795 Debug( LDAP_DEBUG_ANY, "sasl_server_new failed: %d\n",
802 conn->c_sasl_context = ctx;
804 if( sc == SASL_OK ) {
805 sc = sasl_setprop( ctx,
806 SASL_SEC_PROPS, &sasl_secprops );
808 if( sc != SASL_OK ) {
810 LDAP_LOG(( "sasl", LDAP_LEVEL_ERR,
811 "slap_sasl_open: sasl_setprop failed: %d \n", sc ));
813 Debug( LDAP_DEBUG_ANY, "sasl_setprop failed: %d\n",
817 slap_sasl_close( conn );
822 sc = slap_sasl_err2ldap( sc );
827 int slap_sasl_external(
830 const char *auth_id )
832 #if SASL_VERSION_MAJOR >= 2
834 sasl_conn_t *ctx = conn->c_sasl_context;
837 return LDAP_UNAVAILABLE;
840 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL, &ssf );
842 if ( sc != SASL_OK ) {
846 sc = sasl_setprop( ctx, SASL_AUTH_EXTERNAL, auth_id );
848 if ( sc != SASL_OK ) {
852 #elif defined(HAVE_CYRUS_SASL)
854 sasl_conn_t *ctx = conn->c_sasl_context;
855 sasl_external_properties_t extprops;
858 return LDAP_UNAVAILABLE;
861 memset( &extprops, '\0', sizeof(extprops) );
863 extprops.auth_id = (char *) auth_id;
865 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL,
866 (void *) &extprops );
868 if ( sc != SASL_OK ) {
876 int slap_sasl_reset( Connection *conn )
878 #ifdef HAVE_CYRUS_SASL
879 sasl_conn_t *ctx = conn->c_sasl_context;
884 /* must return "anonymous" */
888 char ** slap_sasl_mechs( Connection *conn )
892 #ifdef HAVE_CYRUS_SASL
893 sasl_conn_t *ctx = conn->c_sasl_context;
897 SASL_CONST char *mechstr;
899 sc = sasl_listmech( ctx,
900 NULL, NULL, ",", NULL,
901 &mechstr, NULL, NULL );
903 if( sc != SASL_OK ) {
905 LDAP_LOG(( "sasl", LDAP_LEVEL_ERR,
906 "slap_sasl_mechs: sasl_listmech failed: %d\n", sc ));
908 Debug( LDAP_DEBUG_ANY, "slap_sasl_listmech failed: %d\n",
915 mechs = str2charray( mechstr, "," );
917 #if SASL_VERSION_MAJOR < 2
926 int slap_sasl_close( Connection *conn )
928 #ifdef HAVE_CYRUS_SASL
929 sasl_conn_t *ctx = conn->c_sasl_context;
932 sasl_dispose( &ctx );
935 conn->c_sasl_context = NULL;
937 free( conn->c_sasl_extra );
938 conn->c_sasl_extra = NULL;
955 #ifdef HAVE_CYRUS_SASL
956 sasl_conn_t *ctx = conn->c_sasl_context;
957 struct berval response;
959 const char *errstr = NULL;
963 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
964 "sasl_bind: conn %ld dn=\"%s\" mech=%s datalen=%ld\n",
966 dn->bv_len ? dn->bv_val : "",
967 conn->c_sasl_bind_in_progress ? "<continuing>" : conn->c_sasl_bind_mech.bv_val,
968 cred ? cred->bv_len : 0 ));
970 Debug(LDAP_DEBUG_ARGS,
971 "==> sasl_bind: dn=\"%s\" mech=%s datalen=%ld\n",
972 dn->bv_len ? dn->bv_val : "",
973 conn->c_sasl_bind_in_progress ? "<continuing>":conn->c_sasl_bind_mech.bv_val,
974 cred ? cred->bv_len : 0 );
979 send_ldap_result( conn, op, LDAP_UNAVAILABLE,
980 NULL, "SASL unavailable on this session", NULL, NULL );
984 #if SASL_VERSION_MAJOR >= 2
985 #define START( ctx, mech, cred, clen, resp, rlen, err ) \
986 sasl_server_start( ctx, mech, cred, clen, resp, rlen )
987 #define STEP( ctx, cred, clen, resp, rlen, err ) \
988 sasl_server_step( ctx, cred, clen, resp, rlen )
990 #define START( ctx, mech, cred, clen, resp, rlen, err ) \
991 sasl_server_start( ctx, mech, cred, clen, resp, rlen, err )
992 #define STEP( ctx, cred, clen, resp, rlen, err ) \
993 sasl_server_step( ctx, cred, clen, resp, rlen, err )
996 if ( !conn->c_sasl_bind_in_progress ) {
998 conn->c_sasl_bind_mech.bv_val,
999 cred->bv_len ? cred->bv_val : "",
1001 (SASL_CONST char **)&response.bv_val, &reslen, &errstr );
1005 cred->bv_val, cred->bv_len,
1006 (SASL_CONST char **)&response.bv_val, &reslen, &errstr );
1009 response.bv_len = reslen;
1011 if ( sc == SASL_OK ) {
1012 char *username = NULL;
1015 #if SASL_VERSION_MAJOR >= 2
1016 sc = sasl_getprop( ctx, SASL_DEFUSERREALM, (const void **)&realm );
1018 sc = sasl_getprop( ctx, SASL_REALM, (void **)&realm );
1020 sc = sasl_getprop( ctx,
1021 SASL_USERNAME, (SASL_CONST void **)&username );
1023 if ( sc != SASL_OK ) {
1025 LDAP_LOG(( "sasl", LDAP_LEVEL_ERR,
1026 "slap_sasl_bind: getprop(USERNAME) failed: %d\n", sc ));
1028 Debug(LDAP_DEBUG_TRACE,
1029 "slap_sasl_bind: getprop(USERNAME) failed!\n",
1034 send_ldap_result( conn, op, rc = slap_sasl_err2ldap( sc ),
1035 NULL, "no SASL username", NULL, NULL );
1038 rc = slap_sasl_getdn( conn, username, realm, edn, FLAG_GETDN_FINAL );
1040 if( rc == LDAP_SUCCESS ) {
1041 sasl_ssf_t *ssf = NULL;
1042 (void) sasl_getprop( ctx, SASL_SSF, (void *)&ssf );
1043 *ssfp = ssf ? *ssf : 0;
1046 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1047 conn->c_sasl_layers++;
1048 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1051 send_ldap_sasl( conn, op, rc,
1052 NULL, NULL, NULL, NULL,
1053 response.bv_len ? &response : NULL );
1056 send_ldap_result( conn, op, rc,
1057 NULL, errstr, NULL, NULL );
1061 } else if ( sc == SASL_CONTINUE ) {
1062 send_ldap_sasl( conn, op, rc = LDAP_SASL_BIND_IN_PROGRESS,
1063 NULL, NULL, NULL, NULL, &response );
1066 send_ldap_result( conn, op, rc = slap_sasl_err2ldap( sc ),
1067 NULL, errstr, NULL, NULL );
1070 #if SASL_VERSION_MAJOR < 2
1071 if( response.bv_len ) {
1072 ch_free( response.bv_val );
1077 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
1078 "slap_sasl_bind: rc=%d\n", rc ));
1080 Debug(LDAP_DEBUG_TRACE, "<== slap_sasl_bind: rc=%d\n", rc, 0, 0);
1085 send_ldap_result( conn, op, rc = LDAP_UNAVAILABLE,
1086 NULL, "SASL not supported", NULL, NULL );
1092 char* slap_sasl_secprops( const char *in )
1094 #ifdef HAVE_CYRUS_SASL
1095 int rc = ldap_pvt_sasl_secprops( in, &sasl_secprops );
1097 return rc == LDAP_SUCCESS ? NULL : "Invalid security properties";
1099 return "SASL not supported";