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 );
164 /* Blatantly anonymous ID */
166 ( id[sizeof( "anonymous" )-1] == '\0'
167 || id[sizeof( "anonymous" )-1] == '@' ) &&
168 !strncasecmp( id, "anonymous", sizeof( "anonymous" )-1) ) {
169 return( LDAP_SUCCESS );
171 ctx = conn->c_sasl_context;
172 if ( len == 0 ) len = strlen( id );
174 /* An authcID needs to be converted to authzID form */
175 if( flags & FLAG_GETDN_AUTHCID ) {
176 if( conn->c_is_tls && conn->c_sasl_bind_mech.bv_len == ext_bv.bv_len
177 && ( strcasecmp( ext_bv.bv_val, conn->c_sasl_bind_mech.bv_val ) == 0 ) ) {
178 /* X.509 DN is already normalized */
183 /* convert to u:<username> form */
186 ber_str2bv( id, len, 1, dn );
189 if( !strncasecmp( id, "u:", sizeof("u:")-1 )) {
191 ber_str2bv( id+2, len-2, 1, dn );
192 } else if ( !strncasecmp( id, "dn:", sizeof("dn:")-1) ) {
194 ber_str2bv( id+3, len-3, 1, dn );
198 /* An authzID must be properly prefixed */
199 if( (flags & FLAG_GETDN_AUTHZID) && !is_dn ) {
203 return( LDAP_INAPPROPRIATE_AUTH );
206 /* Username strings */
207 if( is_dn == SET_U ) {
209 len = dn->bv_len + sizeof("uid=")-1 + sizeof(",cn=auth")-1;
211 if( user_realm && *user_realm ) {
212 len += strlen( user_realm ) + sizeof(",cn=")-1;
215 if( conn->c_sasl_bind_mech.bv_len ) {
216 len += conn->c_sasl_bind_mech.bv_len + sizeof(",cn=")-1;
219 /* Build the new dn */
221 dn->bv_val = ch_malloc( len+1 );
222 p = slap_strcopy( dn->bv_val, "uid=" );
223 p = slap_strcopy( p, c1 );
226 if( user_realm && *user_realm ) {
227 p = slap_strcopy( p, ",cn=" );
228 p = slap_strcopy( p, user_realm );
230 if( conn->c_sasl_bind_mech.bv_len ) {
231 p = slap_strcopy( p, ",cn=" );
232 p = slap_strcopy( p, conn->c_sasl_bind_mech.bv_val );
234 p = slap_strcopy( p, ",cn=auth" );
235 dn->bv_len = p - dn->bv_val;
239 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
240 "slap_sasl_getdn: u:id converted to %s.\n", dn->bv_val ));
242 Debug( LDAP_DEBUG_TRACE, "getdn: u:id converted to %s\n", dn->bv_val,0,0 );
246 /* DN strings that are a cn=auth identity to run through regexp */
247 if( is_dn == SET_DN )
249 slap_sasl2dn( dn, &dn2 );
251 ch_free( dn->bv_val );
253 do_norm = 0; /* slap_sasl2dn normalizes */
255 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
256 "slap_sasl_getdn: dn:id converted to %s.\n", dn->bv_val ));
258 Debug( LDAP_DEBUG_TRACE, "getdn: dn:id converted to %s\n",
265 rc = dnNormalize2( NULL, dn, &dn2 );
267 if ( rc != LDAP_SUCCESS ) {
274 return( LDAP_SUCCESS );
277 #if SASL_VERSION_MAJOR >= 2
282 const char *username,
285 struct propctx *propctx)
287 Connection *conn = (Connection *)context;
288 struct berval dn, cred;
292 cred.bv_val = (char *)pass;
293 cred.bv_len = passlen;
295 /* SASL will fallback to its own mechanisms if we don't
296 * find an answer here.
299 rc = slap_sasl_getdn( conn, (char *)username, 0, NULL, &dn,
300 FLAG_GETDN_AUTHCID );
301 if ( rc != LDAP_SUCCESS ) {
302 sasl_seterror( sconn, 0, ldap_err2string( rc ) );
306 if ( dn.bv_len == 0 ) {
307 sasl_seterror( sconn, 0,
308 "No password is associated with the Root DSE" );
309 if ( dn.bv_val != NULL ) {
310 ch_free( dn.bv_val );
315 rc = backend_attribute( NULL, NULL, NULL, NULL, &dn,
316 slap_schema.si_ad_userPassword, &vals);
317 if ( rc != LDAP_SUCCESS ) {
318 ch_free( dn.bv_val );
319 sasl_seterror( sconn, 0, ldap_err2string( rc ) );
320 return SASL_NOVERIFY;
325 if ( vals != NULL ) {
326 for ( bv = vals; bv->bv_val != NULL; bv++ ) {
327 if ( !lutil_passwd( bv, &cred, NULL ) ) {
332 ber_bvarray_free( vals );
335 if ( rc != SASL_OK ) {
336 sasl_seterror( sconn, 0,
337 ldap_err2string( LDAP_INVALID_CREDENTIALS ) );
340 ch_free( dn.bv_val );
345 #if 0 /* CANON isn't for what you think it does. */
347 slap_sasl_canonicalize(
353 const char *user_realm,
358 Connection *conn = (Connection *)context;
365 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
366 "slap_sasl_canonicalize: conn %d %s=\"%s\"\n",
367 conn ? conn->c_connid : -1,
368 (flags == SASL_CU_AUTHID) ? "authcid" : "authzid",
369 in ? in : "<empty>" ));
371 Debug( LDAP_DEBUG_ARGS, "SASL Canonicalize [conn=%ld]: "
373 conn ? conn->c_connid : -1,
374 (flags == SASL_CU_AUTHID) ? "authcid" : "authzid",
375 in ? in : "<empty>" );
378 rc = slap_sasl_getdn( conn, (char *)in, inlen, (char *)user_realm, &dn,
379 (flags == SASL_CU_AUTHID) ? FLAG_GETDN_AUTHCID : FLAG_GETDN_AUTHZID );
380 if ( rc != LDAP_SUCCESS ) {
381 sasl_seterror( sconn, 0, ldap_err2string( rc ) );
385 if ( out_max < dn.bv_len ) {
389 AC_MEMCPY( out, dn.bv_val, dn.bv_len );
390 out[dn.bv_len] = '\0';
392 *out_len = dn.bv_len;
394 ch_free( dn.bv_val );
397 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
398 "slap_sasl_canonicalize: conn %d %s=\"%s\"\n",
399 conn ? conn->c_connid : -1,
400 (flags == SASL_CU_AUTHID) ? "authcDN" : "authzDN",
403 Debug( LDAP_DEBUG_ARGS, "SASL Canonicalize [conn=%ld]: "
405 conn ? conn->c_connid : -1,
406 (flags == SASL_CU_AUTHID) ? "authcDN" : "authzDN",
414 #define CANON_BUF_SIZE 256 /* from saslint.h */
420 char *requested_user,
424 const char *def_realm,
426 struct propctx *propctx)
428 Connection *conn = (Connection *)context;
429 struct berval authcDN, authzDN;
431 int rc, equal = 1, ext = 0;
434 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
435 "slap_sasl_authorize: conn %d authcid=\"%s\" authzid=\"%s\"\n",
436 conn ? conn->c_connid : -1, auth_identity, requested_user));
438 Debug( LDAP_DEBUG_ARGS, "SASL Authorize [conn=%ld]: "
439 "authcid=\"%s\" authzid=\"%s\"\n",
440 conn ? conn->c_connid : -1, auth_identity, requested_user );
443 if ( requested_user )
444 equal = !strcmp( auth_identity, requested_user );
446 /* If using SASL-EXTERNAL, don't modify the ID in any way */
447 if ( conn->c_is_tls && conn->c_sasl_bind_mech.bv_len == ext_bv.bv_len
448 && ( strcasecmp( ext_bv.bv_val, conn->c_sasl_bind_mech.bv_val ) == 0 ) ) {
452 /* Else look for an embedded realm in the name */
453 realm = strchr( auth_identity, '@' );
454 if ( realm ) *realm++ = '\0';
457 rc = slap_sasl_getdn( conn, auth_identity, alen, realm ? realm : (char *)def_realm,
458 &authcDN, FLAG_GETDN_AUTHCID );
462 if ( rc != LDAP_SUCCESS ) {
463 sasl_seterror( sconn, 0, ldap_err2string( rc ) );
468 if ( authcDN.bv_len > CANON_BUF_SIZE ) {
469 free( authcDN.bv_val );
472 AC_MEMCPY( requested_user, authcDN.bv_val, authcDN.bv_len );
480 realm = strchr( requested_user, '@' );
481 if ( realm ) *realm++ = '\0';
484 rc = slap_sasl_getdn( conn, requested_user, rlen, realm ? realm : (char *)def_realm,
485 &authzDN, FLAG_GETDN_AUTHZID );
489 if ( rc != LDAP_SUCCESS ) {
490 free( authcDN.bv_val );
491 sasl_seterror( sconn, 0, ldap_err2string( rc ) );
495 if (authzDN.bv_len > CANON_BUF_SIZE) {
496 free( authcDN.bv_val );
497 free( authzDN.bv_val );
501 rc = slap_sasl_authorized( &authcDN, &authzDN );
502 free( authcDN.bv_val );
503 if ( rc != LDAP_SUCCESS ) {
505 LDAP_LOG(( "sasl", LDAP_LEVEL_INFO,
506 "slap_sasl_authorize: conn %ld authorization disallowed (%d)\n",
507 (long)(conn ? conn->c_connid : -1), rc ));
509 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
510 " authorization disallowed (%d)\n",
511 (long) (conn ? conn->c_connid : -1), rc, 0 );
514 sasl_seterror( sconn, 0, "not authorized" );
515 free( authzDN.bv_val );
518 AC_MEMCPY( requested_user, authzDN.bv_val, authzDN.bv_len );
519 free( authzDN.bv_val );
522 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
523 "slap_sasl_authorize: conn %d authorization allowed\n",
524 (long)(conn ? conn->c_connid : -1 ) ));
526 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
527 " authorization allowed\n",
528 (long) (conn ? conn->c_connid : -1), 0, 0 );
542 struct berval authcDN, authzDN;
544 Connection *conn = context;
545 char *realm, *xrealm;
550 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
551 "slap_sasl_authorize: conn %d authcid=\"%s\" authzid=\"%s\"\n",
552 conn ? conn->c_connid : -1,
553 authcid ? authcid : "<empty>",
554 authzid ? authzid : "<empty>" ));
556 Debug( LDAP_DEBUG_ARGS, "SASL Authorize [conn=%ld]: "
557 "authcid=\"%s\" authzid=\"%s\"\n",
558 (long) (conn ? conn->c_connid : -1),
559 authcid ? authcid : "<empty>",
560 authzid ? authzid : "<empty>" );
563 /* Figure out how much data we have for the dn */
564 rc = sasl_getprop( conn->c_sasl_context, SASL_REALM, (void **)&realm );
565 if( rc != SASL_OK && rc != SASL_NOTDONE ) {
567 LDAP_LOG(( "sasl", LDAP_LEVEL_ERR,
568 "slap_sasl_authorize: getprop(REALM) failed.\n" ));
570 Debug(LDAP_DEBUG_TRACE,
571 "authorize: getprop(REALM) failed!\n", 0,0,0);
573 *errstr = "Could not extract realm";
577 /* Convert the identities to DN's. If no authzid was given, client will
578 be bound as the DN matching their username */
579 if ( conn->c_is_tls && conn->c_sasl_bind_mech.bv_len == ext_bv.bv_len
580 && ( strcasecmp( ext_bv.bv_val, conn->c_sasl_bind_mech.bv_val ) == 0 ) ) {
584 xrealm = strchr( authcid, '@' );
585 if ( xrealm ) *xrealm++ = '\0';
587 rc = slap_sasl_getdn( conn, (char *)authcid, 0, xrealm ? xrealm : realm, &authcDN, FLAG_GETDN_AUTHCID );
588 if ( xrealm ) xrealm[-1] = '@';
589 if( rc != LDAP_SUCCESS ) {
590 *errstr = ldap_err2string( rc );
593 if( ( authzid == NULL ) || !strcmp( authcid,authzid ) ) {
595 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
596 "slap_sasl_authorize: conn %d Using authcDN=%s\n",
597 conn ? conn->c_connid : -1, authcDN.bv_val ));
599 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
600 "Using authcDN=%s\n", (long) (conn ? conn->c_connid : -1), authcDN.bv_val,0 );
603 *user = authcDN.bv_val;
610 xrealm = strchr( authzid, '@' );
611 if ( xrealm ) *xrealm++ = '\0';
613 rc = slap_sasl_getdn( conn, (char *)authzid, 0, xrealm ? xrealm : realm, &authzDN, FLAG_GETDN_AUTHZID );
614 if ( xrealm ) xrealm[-1] = '@';
615 if( rc != LDAP_SUCCESS ) {
616 ch_free( authcDN.bv_val );
617 *errstr = ldap_err2string( rc );
621 rc = slap_sasl_authorized( &authcDN, &authzDN );
624 LDAP_LOG(( "sasl", LDAP_LEVEL_INFO,
625 "slap_sasl_authorize: conn %ld authorization disallowed (%d)\n",
626 (long)(conn ? conn->c_connid : -1), rc ));
628 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
629 " authorization disallowed (%d)\n",
630 (long) (conn ? conn->c_connid : -1), rc, 0 );
633 *errstr = "not authorized";
634 ch_free( authcDN.bv_val );
635 ch_free( authzDN.bv_val );
640 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
641 "slap_sasl_authorize: conn %d authorization allowed\n",
642 (long)(conn ? conn->c_connid : -1 ) ));
644 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
645 " authorization allowed\n",
646 (long) (conn ? conn->c_connid : -1), 0, 0 );
650 ch_free( authcDN.bv_val );
651 *user = authzDN.bv_val;
655 #endif /* SASL_VERSION_MAJOR >= 2 */
658 slap_sasl_err2ldap( int saslerr )
664 rc = LDAP_SASL_BIND_IN_PROGRESS;
673 rc = LDAP_AUTH_METHOD_NOT_SUPPORTED;
676 rc = LDAP_INVALID_CREDENTIALS;
679 rc = LDAP_INSUFFICIENT_ACCESS;
683 rc = LDAP_INAPPROPRIATE_AUTH;
695 int slap_sasl_init( void )
697 #ifdef HAVE_CYRUS_SASL
699 static sasl_callback_t server_callbacks[] = {
700 { SASL_CB_LOG, &slap_sasl_log, NULL },
701 { SASL_CB_LIST_END, NULL, NULL }
711 ldap_pvt_sasl_mutex_new,
712 ldap_pvt_sasl_mutex_lock,
713 ldap_pvt_sasl_mutex_unlock,
714 ldap_pvt_sasl_mutex_dispose );
716 /* should provide callbacks for logging */
717 /* server name should be configurable */
718 rc = sasl_server_init( server_callbacks, "slapd" );
720 if( rc != SASL_OK ) {
722 LDAP_LOG(( "sasl", LDAP_LEVEL_INFO,
723 "slap_sasl_init: init failed.\n" ));
725 Debug( LDAP_DEBUG_ANY, "sasl_server_init failed\n",
733 LDAP_LOG(( "sasl", LDAP_LEVEL_INFO,
734 "slap_sasl_init: initialized!\n"));
736 Debug( LDAP_DEBUG_TRACE, "slap_sasl_init: initialized!\n",
741 /* default security properties */
742 memset( &sasl_secprops, '\0', sizeof(sasl_secprops) );
743 sasl_secprops.max_ssf = INT_MAX;
744 sasl_secprops.maxbufsize = 65536;
745 sasl_secprops.security_flags = SASL_SEC_NOPLAINTEXT|SASL_SEC_NOANONYMOUS;
751 int slap_sasl_destroy( void )
753 #ifdef HAVE_CYRUS_SASL
762 int slap_sasl_open( Connection *conn )
764 int cb, sc = LDAP_SUCCESS;
765 #if SASL_VERSION_MAJOR >= 2
766 char *ipremoteport = NULL, *iplocalport = NULL;
769 #ifdef HAVE_CYRUS_SASL
770 sasl_conn_t *ctx = NULL;
771 sasl_callback_t *session_callbacks;
773 assert( conn->c_sasl_context == NULL );
774 assert( conn->c_sasl_extra == NULL );
776 conn->c_sasl_layers = 0;
779 #if SASL_VERSION_MAJOR >= 2
780 ch_calloc( 5, sizeof(sasl_callback_t));
782 ch_calloc( 3, sizeof(sasl_callback_t));
784 conn->c_sasl_extra = session_callbacks;
786 session_callbacks[cb=0].id = SASL_CB_LOG;
787 session_callbacks[cb].proc = &slap_sasl_log;
788 session_callbacks[cb++].context = conn;
790 session_callbacks[cb].id = SASL_CB_PROXY_POLICY;
791 session_callbacks[cb].proc = &slap_sasl_authorize;
792 session_callbacks[cb++].context = conn;
794 #if SASL_VERSION_MAJOR >= 2
795 #if 0 /* CANON isn't for what you think it does. */
796 session_callbacks[cb].id = SASL_CB_CANON_USER;
797 session_callbacks[cb].proc = &slap_sasl_canonicalize;
798 session_callbacks[cb++].context = conn;
801 /* XXXX: this should be conditional */
802 session_callbacks[cb].id = SASL_CB_SERVER_USERDB_CHECKPASS;
803 session_callbacks[cb].proc = &slap_sasl_checkpass;
804 session_callbacks[cb++].context = conn;
807 session_callbacks[cb].id = SASL_CB_LIST_END;
808 session_callbacks[cb].proc = NULL;
809 session_callbacks[cb++].context = NULL;
811 if( global_host == NULL ) {
812 global_host = ldap_pvt_get_fqdn( NULL );
815 /* create new SASL context */
816 #if SASL_VERSION_MAJOR >= 2
817 if ( conn->c_sock_name.bv_len != 0 &&
818 strncmp( conn->c_sock_name.bv_val, "IP=", 3 ) == 0) {
821 iplocalport = ch_strdup( conn->c_sock_name.bv_val + 3 );
822 /* Convert IPv6 addresses to address;port syntax. */
823 p = strrchr( iplocalport, ' ' );
824 /* Convert IPv4 addresses to address;port syntax. */
825 if ( p == NULL ) p = strchr( iplocalport, ':' );
830 if ( conn->c_peer_name.bv_len != 0 &&
831 strncmp( conn->c_peer_name.bv_val, "IP=", 3 ) == 0) {
834 ipremoteport = ch_strdup( conn->c_peer_name.bv_val + 3 );
835 /* Convert IPv6 addresses to address;port syntax. */
836 p = strrchr( ipremoteport, ' ' );
837 /* Convert IPv4 addresses to address;port syntax. */
838 if ( p == NULL ) p = strchr( ipremoteport, ':' );
843 sc = sasl_server_new( "ldap", global_host, global_realm,
844 iplocalport, ipremoteport, session_callbacks, 0, &ctx );
845 if ( iplocalport != NULL ) {
846 ch_free( iplocalport );
848 if ( ipremoteport != NULL ) {
849 ch_free( ipremoteport );
852 sc = sasl_server_new( "ldap", global_host, global_realm,
853 session_callbacks, SASL_SECURITY_LAYER, &ctx );
856 if( sc != SASL_OK ) {
858 LDAP_LOG(( "sasl", LDAP_LEVEL_ERR,
859 "slap_sasl_open: sasl_server_new failed: %d\n", sc ));
861 Debug( LDAP_DEBUG_ANY, "sasl_server_new failed: %d\n",
868 conn->c_sasl_context = ctx;
870 if( sc == SASL_OK ) {
871 sc = sasl_setprop( ctx,
872 SASL_SEC_PROPS, &sasl_secprops );
874 if( sc != SASL_OK ) {
876 LDAP_LOG(( "sasl", LDAP_LEVEL_ERR,
877 "slap_sasl_open: sasl_setprop failed: %d \n", sc ));
879 Debug( LDAP_DEBUG_ANY, "sasl_setprop failed: %d\n",
883 slap_sasl_close( conn );
888 sc = slap_sasl_err2ldap( sc );
893 int slap_sasl_external(
896 const char *auth_id )
898 #if SASL_VERSION_MAJOR >= 2
900 sasl_conn_t *ctx = conn->c_sasl_context;
903 return LDAP_UNAVAILABLE;
906 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL, &ssf );
908 if ( sc != SASL_OK ) {
912 sc = sasl_setprop( ctx, SASL_AUTH_EXTERNAL, auth_id );
914 if ( sc != SASL_OK ) {
918 #elif defined(HAVE_CYRUS_SASL)
920 sasl_conn_t *ctx = conn->c_sasl_context;
921 sasl_external_properties_t extprops;
924 return LDAP_UNAVAILABLE;
927 memset( &extprops, '\0', sizeof(extprops) );
929 extprops.auth_id = (char *) auth_id;
931 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL,
932 (void *) &extprops );
934 if ( sc != SASL_OK ) {
942 int slap_sasl_reset( Connection *conn )
944 #ifdef HAVE_CYRUS_SASL
945 sasl_conn_t *ctx = conn->c_sasl_context;
950 /* must return "anonymous" */
954 char ** slap_sasl_mechs( Connection *conn )
958 #ifdef HAVE_CYRUS_SASL
959 sasl_conn_t *ctx = conn->c_sasl_context;
963 SASL_CONST char *mechstr;
965 sc = sasl_listmech( ctx,
966 NULL, NULL, ",", NULL,
967 &mechstr, NULL, NULL );
969 if( sc != SASL_OK ) {
971 LDAP_LOG(( "sasl", LDAP_LEVEL_ERR,
972 "slap_sasl_mechs: sasl_listmech failed: %d\n", sc ));
974 Debug( LDAP_DEBUG_ANY, "slap_sasl_listmech failed: %d\n",
981 mechs = str2charray( mechstr, "," );
983 #if SASL_VERSION_MAJOR < 2
992 int slap_sasl_close( Connection *conn )
994 #ifdef HAVE_CYRUS_SASL
995 sasl_conn_t *ctx = conn->c_sasl_context;
998 sasl_dispose( &ctx );
1001 conn->c_sasl_context = NULL;
1003 free( conn->c_sasl_extra );
1004 conn->c_sasl_extra = NULL;
1007 return LDAP_SUCCESS;
1015 struct berval *cred,
1021 #ifdef HAVE_CYRUS_SASL
1022 sasl_conn_t *ctx = conn->c_sasl_context;
1023 struct berval response;
1024 unsigned reslen = 0;
1025 const char *errstr = NULL;
1029 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
1030 "sasl_bind: conn %ld dn=\"%s\" mech=%s datalen=%ld\n",
1032 dn->bv_len ? dn->bv_val : "",
1033 conn->c_sasl_bind_in_progress ? "<continuing>" : conn->c_sasl_bind_mech.bv_val,
1034 cred ? cred->bv_len : 0 ));
1036 Debug(LDAP_DEBUG_ARGS,
1037 "==> sasl_bind: dn=\"%s\" mech=%s datalen=%ld\n",
1038 dn->bv_len ? dn->bv_val : "",
1039 conn->c_sasl_bind_in_progress ? "<continuing>":conn->c_sasl_bind_mech.bv_val,
1040 cred ? cred->bv_len : 0 );
1045 send_ldap_result( conn, op, LDAP_UNAVAILABLE,
1046 NULL, "SASL unavailable on this session", NULL, NULL );
1050 #if SASL_VERSION_MAJOR >= 2
1051 #define START( ctx, mech, cred, clen, resp, rlen, err ) \
1052 sasl_server_start( ctx, mech, cred, clen, resp, rlen )
1053 #define STEP( ctx, cred, clen, resp, rlen, err ) \
1054 sasl_server_step( ctx, cred, clen, resp, rlen )
1056 #define START( ctx, mech, cred, clen, resp, rlen, err ) \
1057 sasl_server_start( ctx, mech, cred, clen, resp, rlen, err )
1058 #define STEP( ctx, cred, clen, resp, rlen, err ) \
1059 sasl_server_step( ctx, cred, clen, resp, rlen, err )
1062 if ( !conn->c_sasl_bind_in_progress ) {
1064 conn->c_sasl_bind_mech.bv_val,
1065 cred->bv_len ? cred->bv_val : "",
1067 (SASL_CONST char **)&response.bv_val, &reslen, &errstr );
1071 cred->bv_val, cred->bv_len,
1072 (SASL_CONST char **)&response.bv_val, &reslen, &errstr );
1075 response.bv_len = reslen;
1077 if ( sc == SASL_OK ) {
1078 char *username = NULL;
1080 sc = sasl_getprop( ctx,
1081 SASL_USERNAME, (SASL_CONST void **)&username );
1083 if ( sc != SASL_OK ) {
1085 LDAP_LOG(( "sasl", LDAP_LEVEL_ERR,
1086 "slap_sasl_bind: getprop(USERNAME) failed: %d\n", sc ));
1088 Debug(LDAP_DEBUG_TRACE,
1089 "slap_sasl_bind: getprop(USERNAME) failed!\n",
1094 send_ldap_result( conn, op, rc = slap_sasl_err2ldap( sc ),
1095 NULL, "no SASL username", NULL, NULL );
1098 sasl_ssf_t *ssf = NULL;
1101 ber_str2bv( username, 0, 1, edn );
1103 (void) sasl_getprop( ctx, SASL_SSF, (void *)&ssf );
1104 *ssfp = ssf ? *ssf : 0;
1107 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1108 conn->c_sasl_layers++;
1109 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1112 send_ldap_sasl( conn, op, rc,
1113 NULL, NULL, NULL, NULL,
1114 response.bv_len ? &response : NULL );
1117 } else if ( sc == SASL_CONTINUE ) {
1118 send_ldap_sasl( conn, op, rc = LDAP_SASL_BIND_IN_PROGRESS,
1119 NULL, NULL, NULL, NULL, &response );
1122 #if SASL_VERSION_MAJOR >= 2
1123 errstr = sasl_errdetail( ctx );
1125 send_ldap_result( conn, op, rc = slap_sasl_err2ldap( sc ),
1126 NULL, errstr, NULL, NULL );
1129 #if SASL_VERSION_MAJOR < 2
1130 if( response.bv_len ) {
1131 ch_free( response.bv_val );
1136 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
1137 "slap_sasl_bind: rc=%d\n", rc ));
1139 Debug(LDAP_DEBUG_TRACE, "<== slap_sasl_bind: rc=%d\n", rc, 0, 0);
1144 send_ldap_result( conn, op, rc = LDAP_UNAVAILABLE,
1145 NULL, "SASL not supported", NULL, NULL );
1151 char* slap_sasl_secprops( const char *in )
1153 #ifdef HAVE_CYRUS_SASL
1154 int rc = ldap_pvt_sasl_secprops( in, &sasl_secprops );
1156 return rc == LDAP_SUCCESS ? NULL : "Invalid security properties";
1158 return "SASL not supported";