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,
137 char *user_realm, struct berval *dn, int flags )
140 int rc, len, is_dn = 0;
145 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
146 "slap_sasl_getdn: conn %d id=%s\n",
147 conn ? conn->c_connid : -1,
148 id ? (*id ? id : "<empty>") : "NULL" ));
150 Debug( LDAP_DEBUG_ARGS, "slap_sasl_getdn: id=%s\n",
151 id?(*id?id:"<empty>"):"NULL",0,0 );
157 /* Blatantly anonymous ID */
159 ( id[sizeof( "anonymous" )-1] == '\0'
160 || id[sizeof( "anonymous" )-1] == '@' ) &&
161 !strncasecmp( id, "anonymous", sizeof( "anonymous" )-1) ) {
162 return( LDAP_SUCCESS );
164 ctx = conn->c_sasl_context;
167 /* An authcID needs to be converted to authzID form */
168 if( flags & FLAG_GETDN_AUTHCID ) {
169 if( sasl_external_x509dn_convert
170 && conn->c_sasl_bind_mech.bv_len == ext_bv.bv_len
171 && ( strcasecmp( ext_bv.bv_val, conn->c_sasl_bind_mech.bv_val ) == 0 )
174 /* check SASL external for X.509 style DN and */
175 /* convert to dn:<dn> form */
176 dn->bv_val = ldap_dcedn2dn( id );
177 dn->bv_len = strlen(dn->bv_val);
181 /* convert to u:<username> form */
182 ber_str2bv( id, len, 1, dn );
187 if( !strncasecmp( id, "u:", sizeof("u:")-1 )) {
189 ber_str2bv( id+2, len-2, 1, dn );
190 } else if ( !strncasecmp( id, "dn:", sizeof("dn:")-1) ) {
192 ber_str2bv( id+3, len-3, 1, dn );
196 /* An authzID must be properly prefixed */
197 if( (flags & FLAG_GETDN_AUTHZID) && !is_dn ) {
201 return( LDAP_INAPPROPRIATE_AUTH );
204 /* Username strings */
205 if( is_dn == SET_U ) {
207 len = dn->bv_len + sizeof("uid=")-1 + sizeof(",cn=auth")-1;
209 if( user_realm && *user_realm ) {
210 len += strlen( user_realm ) + sizeof(",cn=")-1;
213 if( conn->c_sasl_bind_mech.bv_len ) {
214 len += conn->c_sasl_bind_mech.bv_len + sizeof(",cn=")-1;
217 /* Build the new dn */
219 dn->bv_val = ch_malloc( len );
220 p = slap_strcopy( dn->bv_val, "uid=" );
221 p = slap_strcopy( p, c1 );
225 p = slap_strcopy( p, ",cn=" );
226 p = slap_strcopy( p, user_realm );
228 if( conn->c_sasl_bind_mech.bv_len ) {
229 p = slap_strcopy( p, ",cn=" );
230 p = slap_strcopy( p, conn->c_sasl_bind_mech.bv_val );
232 p = slap_strcopy( p, ",cn=auth" );
233 dn->bv_len = p - dn->bv_val;
237 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
238 "slap_sasl_getdn: u:id converted to %s.\n", dn->bv_val ));
240 Debug( LDAP_DEBUG_TRACE, "getdn: u:id converted to %s\n", dn->bv_val,0,0 );
244 /* DN strings that are a cn=auth identity to run through regexp */
245 if( is_dn == SET_DN && ( ( flags & FLAG_GETDN_FINAL ) == 0 ) )
247 slap_sasl2dn( dn, &dn2 );
249 ch_free( dn->bv_val );
252 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
253 "slap_sasl_getdn: dn:id converted to %s.\n", dn->bv_val ));
255 Debug( LDAP_DEBUG_TRACE, "getdn: dn:id converted to %s\n",
261 if( flags & FLAG_GETDN_FINAL ) {
262 /* omit "dn:" prefix */
265 rc = dnNormalize2( NULL, dn, &dn2 );
267 if ( rc != LDAP_SUCCESS ) {
274 /* Attach the "dn:" prefix if needed */
275 if ( is_dn == SET_DN ) {
276 c1 = ch_malloc( dn->bv_len + sizeof("dn:") );
278 strcpy( c1 + 3, dn->bv_val );
284 return( LDAP_SUCCESS );
287 #if SASL_VERSION_MAJOR >= 2
292 const char *username,
295 struct propctx *propctx)
297 Connection *conn = (Connection *)context;
298 struct berval dn, cred;
302 cred.bv_val = (char *)pass;
303 cred.bv_len = passlen;
305 /* XXX can we do both steps at once? */
306 rc = slap_sasl_getdn( conn, (char *)username, NULL, &dn,
307 FLAG_GETDN_AUTHCID | FLAG_GETDN_FINAL );
308 if ( rc != LDAP_SUCCESS ) {
309 sasl_seterror( sconn, 0, ldap_err2string( rc ) );
313 if ( dn.bv_len == 0 ) {
314 sasl_seterror( sconn, 0,
315 "No password is associated with the Root DSE" );
316 if ( dn.bv_val != NULL ) {
317 ch_free( dn.bv_val );
322 rc = backend_attribute( NULL, NULL, NULL, NULL, &dn,
323 slap_schema.si_ad_userPassword, &vals);
324 if ( rc != LDAP_SUCCESS ) {
325 ch_free( dn.bv_val );
326 sasl_seterror( sconn, 0, ldap_err2string( rc ) );
327 return SASL_NOVERIFY;
332 if ( vals != NULL ) {
333 for ( bv = vals; bv->bv_val != NULL; bv++ ) {
334 if ( !lutil_passwd( bv, &cred, NULL ) ) {
339 ber_bvarray_free( vals );
342 if ( rc != SASL_OK ) {
343 sasl_seterror( sconn, 0,
344 ldap_err2string( LDAP_INVALID_CREDENTIALS ) );
347 ch_free( dn.bv_val );
353 slap_sasl_canonicalize(
359 const char *user_realm,
364 Connection *conn = (Connection *)context;
371 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
372 "slap_sasl_canonicalize: conn %d %s=\"%s\"\n",
373 conn ? conn->c_connid : -1,
374 (flags == SASL_CU_AUTHID) ? "authcid" : "authzid",
375 in ? in : "<empty>" ));
377 Debug( LDAP_DEBUG_ARGS, "SASL Canonicalize [conn=%ld]: "
379 conn ? conn->c_connid : -1,
380 (flags == SASL_CU_AUTHID) ? "authcid" : "authzid",
381 in ? in : "<empty>" );
384 rc = slap_sasl_getdn( conn, (char *)in, (char *)user_realm, &dn,
385 (flags == SASL_CU_AUTHID) ? FLAG_GETDN_AUTHCID : FLAG_GETDN_AUTHZID );
386 if ( rc != LDAP_SUCCESS ) {
387 sasl_seterror( sconn, 0, ldap_err2string( rc ) );
391 if ( out_max < dn.bv_len ) {
395 AC_MEMCPY( out, dn.bv_val, dn.bv_len );
396 out[dn.bv_len] = '\0';
398 *out_len = dn.bv_len;
400 ch_free( dn.bv_val );
403 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
404 "slap_sasl_canonicalize: conn %d %s=\"%s\"\n",
405 conn ? conn->c_connid : -1,
406 (flags == SASL_CU_AUTHID) ? "authcDN" : "authzDN",
409 Debug( LDAP_DEBUG_ARGS, "SASL Canonicalize [conn=%ld]: "
411 conn ? conn->c_connid : -1,
412 (flags == SASL_CU_AUTHID) ? "authcDN" : "authzDN",
423 const char *requested_user,
425 const char *auth_identity,
427 const char *def_realm,
429 struct propctx *propctx)
431 Connection *conn = (Connection *)context;
432 struct berval authcDN, authzDN;
435 authcDN.bv_val = (char *)auth_identity;
436 authcDN.bv_len = alen;
438 authzDN.bv_val = (char *)requested_user;
439 authzDN.bv_len = rlen;
442 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
443 "slap_sasl_authorize: conn %d authcDN=\"%s\" authzDN=\"%s\"\n",
444 conn ? conn->c_connid : -1, authcDN.bv_val, authzDN.bv_val));
446 Debug( LDAP_DEBUG_ARGS, "SASL Authorize [conn=%ld]: "
447 "authcDN=\"%s\" authzDN=\"%s\"\n",
448 conn ? conn->c_connid : -1, authcDN.bv_val, authzDN.bv_val );
451 rc = slap_sasl_authorized( &authcDN, &authzDN );
452 if ( rc != LDAP_SUCCESS ) {
454 LDAP_LOG(( "sasl", LDAP_LEVEL_INFO,
455 "slap_sasl_authorize: conn %ld authorization disallowed (%d)\n",
456 (long)(conn ? conn->c_connid : -1), rc ));
458 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
459 " authorization disallowed (%d)\n",
460 (long) (conn ? conn->c_connid : -1), rc, 0 );
463 sasl_seterror( sconn, 0, "not authorized" );
468 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
469 "slap_sasl_authorize: conn %d authorization allowed\n",
470 (long)(conn ? conn->c_connid : -1 ) ));
472 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
473 " authorization allowed\n",
474 (long) (conn ? conn->c_connid : -1), 0, 0 );
488 struct berval authcDN, authzDN;
490 Connection *conn = context;
496 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
497 "slap_sasl_authorize: conn %d authcid=\"%s\" authzid=\"%s\"\n",
498 conn ? conn->c_connid : -1,
499 authcid ? authcid : "<empty>",
500 authzid ? authzid : "<empty>" ));
502 Debug( LDAP_DEBUG_ARGS, "SASL Authorize [conn=%ld]: "
503 "authcid=\"%s\" authzid=\"%s\"\n",
504 (long) (conn ? conn->c_connid : -1),
505 authcid ? authcid : "<empty>",
506 authzid ? authzid : "<empty>" );
509 /* Figure out how much data we have for the dn */
510 rc = sasl_getprop( conn->c_sasl_context, SASL_REALM, (void **)&realm );
511 if( rc != SASL_OK && rc != SASL_NOTDONE ) {
513 LDAP_LOG(( "sasl", LDAP_LEVEL_ERR,
514 "slap_sasl_authorize: getprop(REALM) failed.\n" ));
516 Debug(LDAP_DEBUG_TRACE,
517 "authorize: getprop(REALM) failed!\n", 0,0,0);
519 *errstr = "Could not extract realm";
523 /* Convert the identities to DN's. If no authzid was given, client will
524 be bound as the DN matching their username */
525 rc = slap_sasl_getdn( conn, (char *)authcid, realm, &authcDN, FLAG_GETDN_AUTHCID );
526 if( rc != LDAP_SUCCESS ) {
527 *errstr = ldap_err2string( rc );
530 if( ( authzid == NULL ) || !strcmp( authcid,authzid ) ) {
532 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
533 "slap_sasl_authorize: conn %d Using authcDN=%s\n",
534 conn ? conn->c_connid : -1, authcDN.bv_val ));
536 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
537 "Using authcDN=%s\n", (long) (conn ? conn->c_connid : -1), authcDN.bv_val,0 );
540 *user = authcDN.bv_val;
544 rc = slap_sasl_getdn( conn, (char *)authzid, realm, &authzDN, FLAG_GETDN_AUTHZID );
545 if( rc != LDAP_SUCCESS ) {
546 ch_free( authcDN.bv_val );
547 *errstr = ldap_err2string( rc );
551 rc = slap_sasl_authorized( &authcDN, &authzDN );
554 LDAP_LOG(( "sasl", LDAP_LEVEL_INFO,
555 "slap_sasl_authorize: conn %ld authorization disallowed (%d)\n",
556 (long)(conn ? conn->c_connid : -1), rc ));
558 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
559 " authorization disallowed (%d)\n",
560 (long) (conn ? conn->c_connid : -1), rc, 0 );
563 *errstr = "not authorized";
564 ch_free( authcDN.bv_val );
565 ch_free( authzDN.bv_val );
570 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
571 "slap_sasl_authorize: conn %d authorization allowed\n",
572 (long)(conn ? conn->c_connid : -1 ) ));
574 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
575 " authorization allowed\n",
576 (long) (conn ? conn->c_connid : -1), 0, 0 );
580 ch_free( authcDN.bv_val );
581 *user = authzDN.bv_val;
585 #endif /* SASL_VERSION_MAJOR >= 2 */
588 slap_sasl_err2ldap( int saslerr )
594 rc = LDAP_SASL_BIND_IN_PROGRESS;
603 rc = LDAP_AUTH_METHOD_NOT_SUPPORTED;
606 rc = LDAP_INVALID_CREDENTIALS;
609 rc = LDAP_INSUFFICIENT_ACCESS;
613 rc = LDAP_INAPPROPRIATE_AUTH;
625 int slap_sasl_init( void )
627 #ifdef HAVE_CYRUS_SASL
629 static sasl_callback_t server_callbacks[] = {
630 { SASL_CB_LOG, &slap_sasl_log, NULL },
631 { SASL_CB_LIST_END, NULL, NULL }
641 ldap_pvt_sasl_mutex_new,
642 ldap_pvt_sasl_mutex_lock,
643 ldap_pvt_sasl_mutex_unlock,
644 ldap_pvt_sasl_mutex_dispose );
646 /* should provide callbacks for logging */
647 /* server name should be configurable */
648 rc = sasl_server_init( server_callbacks, "slapd" );
650 if( rc != SASL_OK ) {
652 LDAP_LOG(( "sasl", LDAP_LEVEL_INFO,
653 "slap_sasl_init: init failed.\n" ));
655 Debug( LDAP_DEBUG_ANY, "sasl_server_init failed\n",
663 LDAP_LOG(( "sasl", LDAP_LEVEL_INFO,
664 "slap_sasl_init: initialized!\n"));
666 Debug( LDAP_DEBUG_TRACE, "slap_sasl_init: initialized!\n",
671 /* default security properties */
672 memset( &sasl_secprops, '\0', sizeof(sasl_secprops) );
673 sasl_secprops.max_ssf = INT_MAX;
674 sasl_secprops.maxbufsize = 65536;
675 sasl_secprops.security_flags = SASL_SEC_NOPLAINTEXT|SASL_SEC_NOANONYMOUS;
681 int slap_sasl_destroy( void )
683 #ifdef HAVE_CYRUS_SASL
692 int slap_sasl_open( Connection *conn )
694 int cb, sc = LDAP_SUCCESS;
695 #if SASL_VERSION_MAJOR >= 2
696 char *ipremoteport = NULL, *iplocalport = NULL;
699 #ifdef HAVE_CYRUS_SASL
700 sasl_conn_t *ctx = NULL;
701 sasl_callback_t *session_callbacks;
703 assert( conn->c_sasl_context == NULL );
704 assert( conn->c_sasl_extra == NULL );
706 conn->c_sasl_layers = 0;
709 #if SASL_VERSION_MAJOR >= 2
710 ch_calloc( 5, sizeof(sasl_callback_t));
712 ch_calloc( 3, sizeof(sasl_callback_t));
714 conn->c_sasl_extra = session_callbacks;
716 session_callbacks[cb=0].id = SASL_CB_LOG;
717 session_callbacks[cb].proc = &slap_sasl_log;
718 session_callbacks[cb++].context = conn;
720 session_callbacks[cb].id = SASL_CB_PROXY_POLICY;
721 session_callbacks[cb].proc = &slap_sasl_authorize;
722 session_callbacks[cb++].context = conn;
724 #if SASL_VERSION_MAJOR >= 2
725 session_callbacks[cb].id = SASL_CB_CANON_USER;
726 session_callbacks[cb].proc = &slap_sasl_canonicalize;
727 session_callbacks[cb++].context = conn;
729 /* XXXX: this should be conditional */
730 session_callbacks[cb].id = SASL_CB_SERVER_USERDB_CHECKPASS;
731 session_callbacks[cb].proc = &slap_sasl_checkpass;
732 session_callbacks[cb++].context = conn;
735 session_callbacks[cb].id = SASL_CB_LIST_END;
736 session_callbacks[cb].proc = NULL;
737 session_callbacks[cb++].context = NULL;
739 if( global_host == NULL ) {
740 global_host = ldap_pvt_get_fqdn( NULL );
743 /* create new SASL context */
744 #if SASL_VERSION_MAJOR >= 2
745 if ( conn->c_sock_name.bv_len != 0 &&
746 strncmp( conn->c_sock_name.bv_val, "IP=", 3 ) == 0) {
749 iplocalport = ch_strdup( conn->c_sock_name.bv_val + 3 );
750 /* Convert IPv6 addresses to address;port syntax. */
751 p = strrchr( iplocalport, ' ' );
756 if ( conn->c_peer_name.bv_len != 0 &&
757 strncmp( conn->c_peer_name.bv_val, "IP=", 3 ) == 0) {
760 ipremoteport = ch_strdup( conn->c_peer_name.bv_val + 3 );
761 /* Convert IPv6 addresses to address;port syntax. */
762 p = strrchr( ipremoteport, ' ' );
767 sc = sasl_server_new( "ldap", global_host, global_realm,
768 iplocalport, ipremoteport, session_callbacks, 0, &ctx );
769 if ( iplocalport != NULL ) {
770 ch_free( iplocalport );
772 if ( ipremoteport != NULL ) {
773 ch_free( ipremoteport );
776 sc = sasl_server_new( "ldap", global_host, global_realm,
777 session_callbacks, SASL_SECURITY_LAYER, &ctx );
780 if( sc != SASL_OK ) {
782 LDAP_LOG(( "sasl", LDAP_LEVEL_ERR,
783 "slap_sasl_open: sasl_server_new failed: %d\n", sc ));
785 Debug( LDAP_DEBUG_ANY, "sasl_server_new failed: %d\n",
792 conn->c_sasl_context = ctx;
794 if( sc == SASL_OK ) {
795 sc = sasl_setprop( ctx,
796 SASL_SEC_PROPS, &sasl_secprops );
798 if( sc != SASL_OK ) {
800 LDAP_LOG(( "sasl", LDAP_LEVEL_ERR,
801 "slap_sasl_open: sasl_setprop failed: %d \n", sc ));
803 Debug( LDAP_DEBUG_ANY, "sasl_setprop failed: %d\n",
807 slap_sasl_close( conn );
812 sc = slap_sasl_err2ldap( sc );
817 int slap_sasl_external(
820 const char *auth_id )
822 #if SASL_VERSION_MAJOR >= 2
824 sasl_conn_t *ctx = conn->c_sasl_context;
827 return LDAP_UNAVAILABLE;
830 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL, &ssf );
832 if ( sc != SASL_OK ) {
836 sc = sasl_setprop( ctx, SASL_AUTH_EXTERNAL, auth_id );
838 if ( sc != SASL_OK ) {
842 #elif defined(HAVE_CYRUS_SASL)
844 sasl_conn_t *ctx = conn->c_sasl_context;
845 sasl_external_properties_t extprops;
848 return LDAP_UNAVAILABLE;
851 memset( &extprops, '\0', sizeof(extprops) );
853 extprops.auth_id = (char *) auth_id;
855 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL,
856 (void *) &extprops );
858 if ( sc != SASL_OK ) {
866 int slap_sasl_reset( Connection *conn )
868 #ifdef HAVE_CYRUS_SASL
869 sasl_conn_t *ctx = conn->c_sasl_context;
874 /* must return "anonymous" */
878 char ** slap_sasl_mechs( Connection *conn )
882 #ifdef HAVE_CYRUS_SASL
883 sasl_conn_t *ctx = conn->c_sasl_context;
887 SASL_CONST char *mechstr;
889 sc = sasl_listmech( ctx,
890 NULL, NULL, ",", NULL,
891 &mechstr, NULL, NULL );
893 if( sc != SASL_OK ) {
895 LDAP_LOG(( "sasl", LDAP_LEVEL_ERR,
896 "slap_sasl_mechs: sasl_listmech failed: %d\n", sc ));
898 Debug( LDAP_DEBUG_ANY, "slap_sasl_listmech failed: %d\n",
905 mechs = str2charray( mechstr, "," );
907 #if SASL_VERSION_MAJOR < 2
916 int slap_sasl_close( Connection *conn )
918 #ifdef HAVE_CYRUS_SASL
919 sasl_conn_t *ctx = conn->c_sasl_context;
922 sasl_dispose( &ctx );
925 conn->c_sasl_context = NULL;
927 free( conn->c_sasl_extra );
928 conn->c_sasl_extra = NULL;
945 #ifdef HAVE_CYRUS_SASL
946 sasl_conn_t *ctx = conn->c_sasl_context;
947 struct berval response;
949 const char *errstr = NULL;
953 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
954 "sasl_bind: conn %ld dn=\"%s\" mech=%s datalen=%ld\n",
956 dn->bv_len ? dn->bv_val : "",
957 conn->c_sasl_bind_in_progress ? "<continuing>" : conn->c_sasl_bind_mech.bv_val,
958 cred ? cred->bv_len : 0 ));
960 Debug(LDAP_DEBUG_ARGS,
961 "==> sasl_bind: dn=\"%s\" mech=%s datalen=%ld\n",
962 dn->bv_len ? dn->bv_val : "",
963 conn->c_sasl_bind_in_progress ? "<continuing>":conn->c_sasl_bind_mech.bv_val,
964 cred ? cred->bv_len : 0 );
969 send_ldap_result( conn, op, LDAP_UNAVAILABLE,
970 NULL, "SASL unavailable on this session", NULL, NULL );
974 #if SASL_VERSION_MAJOR >= 2
975 #define START( ctx, mech, cred, clen, resp, rlen, err ) \
976 sasl_server_start( ctx, mech, cred, clen, resp, rlen )
977 #define STEP( ctx, cred, clen, resp, rlen, err ) \
978 sasl_server_step( ctx, cred, clen, resp, rlen )
980 #define START( ctx, mech, cred, clen, resp, rlen, err ) \
981 sasl_server_start( ctx, mech, cred, clen, resp, rlen, err )
982 #define STEP( ctx, cred, clen, resp, rlen, err ) \
983 sasl_server_step( ctx, cred, clen, resp, rlen, err )
986 if ( !conn->c_sasl_bind_in_progress ) {
988 conn->c_sasl_bind_mech.bv_val,
989 cred->bv_len ? cred->bv_val : "",
991 (SASL_CONST char **)&response.bv_val, &reslen, &errstr );
995 cred->bv_val, cred->bv_len,
996 (SASL_CONST char **)&response.bv_val, &reslen, &errstr );
999 response.bv_len = reslen;
1001 if ( sc == SASL_OK ) {
1002 char *username = NULL;
1005 #if SASL_VERSION_MAJOR >= 2
1006 sc = sasl_getprop( ctx, SASL_DEFUSERREALM, (const void **)&realm );
1008 sc = sasl_getprop( ctx, SASL_REALM, (void **)&realm );
1010 sc = sasl_getprop( ctx,
1011 SASL_USERNAME, (SASL_CONST void **)&username );
1013 if ( sc != SASL_OK ) {
1015 LDAP_LOG(( "sasl", LDAP_LEVEL_ERR,
1016 "slap_sasl_bind: getprop(USERNAME) failed: %d\n", sc ));
1018 Debug(LDAP_DEBUG_TRACE,
1019 "slap_sasl_bind: getprop(USERNAME) failed!\n",
1024 send_ldap_result( conn, op, rc = slap_sasl_err2ldap( sc ),
1025 NULL, "no SASL username", NULL, NULL );
1028 rc = slap_sasl_getdn( conn, username, realm, edn, FLAG_GETDN_FINAL );
1030 if( rc == LDAP_SUCCESS ) {
1031 sasl_ssf_t *ssf = NULL;
1032 (void) sasl_getprop( ctx, SASL_SSF, (void *)&ssf );
1033 *ssfp = ssf ? *ssf : 0;
1036 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1037 conn->c_sasl_layers++;
1038 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1041 send_ldap_sasl( conn, op, rc,
1042 NULL, NULL, NULL, NULL,
1043 response.bv_len ? &response : NULL );
1046 send_ldap_result( conn, op, rc,
1047 NULL, errstr, NULL, NULL );
1051 } else if ( sc == SASL_CONTINUE ) {
1052 send_ldap_sasl( conn, op, rc = LDAP_SASL_BIND_IN_PROGRESS,
1053 NULL, NULL, NULL, NULL, &response );
1056 send_ldap_result( conn, op, rc = slap_sasl_err2ldap( sc ),
1057 NULL, errstr, NULL, NULL );
1060 #if SASL_VERSION_MAJOR < 2
1061 if( response.bv_len ) {
1062 ch_free( response.bv_val );
1067 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
1068 "slap_sasl_bind: rc=%d\n", rc ));
1070 Debug(LDAP_DEBUG_TRACE, "<== slap_sasl_bind: rc=%d\n", rc, 0, 0);
1075 send_ldap_result( conn, op, rc = LDAP_UNAVAILABLE,
1076 NULL, "SASL not supported", NULL, NULL );
1082 char* slap_sasl_secprops( const char *in )
1084 #ifdef HAVE_CYRUS_SASL
1085 int rc = ldap_pvt_sasl_secprops( in, &sasl_secprops );
1087 return rc == LDAP_SUCCESS ? NULL : "Invalid security properties";
1089 return "SASL not supported";