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( sasl_external_x509dn_convert
177 && conn->c_sasl_bind_mech.bv_len == ext_bv.bv_len
178 && ( strcasecmp( ext_bv.bv_val, conn->c_sasl_bind_mech.bv_val ) == 0 )
181 /* check SASL external for X.509 style DN and */
182 /* convert to dn:<dn> form, result is normalized */
183 dnDCEnormalize( id, dn );
188 /* convert to u:<username> form */
189 ber_str2bv( id, len, 1, dn );
194 if( !strncasecmp( id, "u:", sizeof("u:")-1 )) {
196 ber_str2bv( id+2, len-2, 1, dn );
197 } else if ( !strncasecmp( id, "dn:", sizeof("dn:")-1) ) {
199 ber_str2bv( id+3, len-3, 1, dn );
203 /* An authzID must be properly prefixed */
204 if( (flags & FLAG_GETDN_AUTHZID) && !is_dn ) {
208 return( LDAP_INAPPROPRIATE_AUTH );
211 /* Username strings */
212 if( is_dn == SET_U ) {
214 len = dn->bv_len + sizeof("uid=")-1 + sizeof(",cn=auth")-1;
216 if( user_realm && *user_realm ) {
217 len += strlen( user_realm ) + sizeof(",cn=")-1;
220 if( conn->c_sasl_bind_mech.bv_len ) {
221 len += conn->c_sasl_bind_mech.bv_len + sizeof(",cn=")-1;
224 /* Build the new dn */
226 dn->bv_val = ch_malloc( len+1 );
227 p = slap_strcopy( dn->bv_val, "uid=" );
228 p = slap_strcopy( p, c1 );
231 if( user_realm && *user_realm ) {
232 p = slap_strcopy( p, ",cn=" );
233 p = slap_strcopy( p, user_realm );
235 if( conn->c_sasl_bind_mech.bv_len ) {
236 p = slap_strcopy( p, ",cn=" );
237 p = slap_strcopy( p, conn->c_sasl_bind_mech.bv_val );
239 p = slap_strcopy( p, ",cn=auth" );
240 dn->bv_len = p - dn->bv_val;
244 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
245 "slap_sasl_getdn: u:id converted to %s.\n", dn->bv_val ));
247 Debug( LDAP_DEBUG_TRACE, "getdn: u:id converted to %s\n", dn->bv_val,0,0 );
251 /* DN strings that are a cn=auth identity to run through regexp */
252 if( is_dn == SET_DN )
254 slap_sasl2dn( dn, &dn2 );
256 ch_free( dn->bv_val );
258 do_norm = 0; /* slap_sasl2dn normalizes */
260 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
261 "slap_sasl_getdn: dn:id converted to %s.\n", dn->bv_val ));
263 Debug( LDAP_DEBUG_TRACE, "getdn: dn:id converted to %s\n",
270 rc = dnNormalize2( NULL, dn, &dn2 );
272 if ( rc != LDAP_SUCCESS ) {
279 return( LDAP_SUCCESS );
282 #if SASL_VERSION_MAJOR >= 2
287 const char *username,
290 struct propctx *propctx)
292 Connection *conn = (Connection *)context;
293 struct berval dn, cred;
297 cred.bv_val = (char *)pass;
298 cred.bv_len = passlen;
300 /* SASL will fallback to its own mechanisms if we don't
301 * find an answer here.
304 rc = slap_sasl_getdn( conn, (char *)username, 0, NULL, &dn,
305 FLAG_GETDN_AUTHCID );
306 if ( rc != LDAP_SUCCESS ) {
307 sasl_seterror( sconn, 0, ldap_err2string( rc ) );
311 if ( dn.bv_len == 0 ) {
312 sasl_seterror( sconn, 0,
313 "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 );
350 #if 0 /* CANON isn't for what you think it does. */
352 slap_sasl_canonicalize(
358 const char *user_realm,
363 Connection *conn = (Connection *)context;
370 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
371 "slap_sasl_canonicalize: conn %d %s=\"%s\"\n",
372 conn ? conn->c_connid : -1,
373 (flags == SASL_CU_AUTHID) ? "authcid" : "authzid",
374 in ? in : "<empty>" ));
376 Debug( LDAP_DEBUG_ARGS, "SASL Canonicalize [conn=%ld]: "
378 conn ? conn->c_connid : -1,
379 (flags == SASL_CU_AUTHID) ? "authcid" : "authzid",
380 in ? in : "<empty>" );
383 rc = slap_sasl_getdn( conn, (char *)in, inlen, (char *)user_realm, &dn,
384 (flags == SASL_CU_AUTHID) ? FLAG_GETDN_AUTHCID : FLAG_GETDN_AUTHZID );
385 if ( rc != LDAP_SUCCESS ) {
386 sasl_seterror( sconn, 0, ldap_err2string( rc ) );
390 if ( out_max < dn.bv_len ) {
394 AC_MEMCPY( out, dn.bv_val, dn.bv_len );
395 out[dn.bv_len] = '\0';
397 *out_len = dn.bv_len;
399 ch_free( dn.bv_val );
402 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
403 "slap_sasl_canonicalize: conn %d %s=\"%s\"\n",
404 conn ? conn->c_connid : -1,
405 (flags == SASL_CU_AUTHID) ? "authcDN" : "authzDN",
408 Debug( LDAP_DEBUG_ARGS, "SASL Canonicalize [conn=%ld]: "
410 conn ? conn->c_connid : -1,
411 (flags == SASL_CU_AUTHID) ? "authcDN" : "authzDN",
419 #define CANON_BUF_SIZE 256 /* from saslint.h */
425 char *requested_user,
429 const char *def_realm,
431 struct propctx *propctx)
433 Connection *conn = (Connection *)context;
434 struct berval authcDN, authzDN;
436 int rc, equal = 1, ext = 0;
439 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
440 "slap_sasl_authorize: conn %d authcid=\"%s\" authzid=\"%s\"\n",
441 conn ? conn->c_connid : -1, auth_identity, requested_user));
443 Debug( LDAP_DEBUG_ARGS, "SASL Authorize [conn=%ld]: "
444 "authcid=\"%s\" authzid=\"%s\"\n",
445 conn ? conn->c_connid : -1, auth_identity, requested_user );
448 if ( requested_user )
449 equal = !strcmp( auth_identity, requested_user );
451 /* If using SASL-EXTERNAL, don't modify the ID in any way */
452 if ( conn->c_sasl_bind_mech.bv_len == ext_bv.bv_len
453 && ( strcasecmp( ext_bv.bv_val, conn->c_sasl_bind_mech.bv_val ) == 0 )
454 && auth_identity[0] == '/' ) {
458 /* Else look for an embedded realm in the name */
459 realm = strchr( auth_identity, '@' );
460 if ( realm ) *realm++ = '\0';
463 rc = slap_sasl_getdn( conn, auth_identity, alen, realm ? realm : (char *)def_realm,
464 &authcDN, FLAG_GETDN_AUTHCID );
468 if ( rc != LDAP_SUCCESS ) {
469 sasl_seterror( sconn, 0, ldap_err2string( rc ) );
474 if ( authcDN.bv_len > CANON_BUF_SIZE ) {
475 free( authcDN.bv_val );
478 AC_MEMCPY( requested_user, authcDN.bv_val, authcDN.bv_len );
486 realm = strchr( requested_user, '@' );
487 if ( realm ) *realm++ = '\0';
490 rc = slap_sasl_getdn( conn, requested_user, rlen, realm ? realm : (char *)def_realm,
491 &authzDN, FLAG_GETDN_AUTHZID );
495 if ( rc != LDAP_SUCCESS ) {
496 free( authcDN.bv_val );
497 sasl_seterror( sconn, 0, ldap_err2string( rc ) );
501 if (authzDN.bv_len > CANON_BUF_SIZE) {
502 free( authcDN.bv_val );
503 free( authzDN.bv_val );
507 rc = slap_sasl_authorized( &authcDN, &authzDN );
508 free( authcDN.bv_val );
509 if ( rc != LDAP_SUCCESS ) {
511 LDAP_LOG(( "sasl", LDAP_LEVEL_INFO,
512 "slap_sasl_authorize: conn %ld authorization disallowed (%d)\n",
513 (long)(conn ? conn->c_connid : -1), rc ));
515 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
516 " authorization disallowed (%d)\n",
517 (long) (conn ? conn->c_connid : -1), rc, 0 );
520 sasl_seterror( sconn, 0, "not authorized" );
521 free( authzDN.bv_val );
524 AC_MEMCPY( requested_user, authzDN.bv_val, authzDN.bv_len );
525 free( authzDN.bv_val );
528 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
529 "slap_sasl_authorize: conn %d authorization allowed\n",
530 (long)(conn ? conn->c_connid : -1 ) ));
532 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
533 " authorization allowed\n",
534 (long) (conn ? conn->c_connid : -1), 0, 0 );
548 struct berval authcDN, authzDN;
550 Connection *conn = context;
551 char *realm, *xrealm;
556 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
557 "slap_sasl_authorize: conn %d authcid=\"%s\" authzid=\"%s\"\n",
558 conn ? conn->c_connid : -1,
559 authcid ? authcid : "<empty>",
560 authzid ? authzid : "<empty>" ));
562 Debug( LDAP_DEBUG_ARGS, "SASL Authorize [conn=%ld]: "
563 "authcid=\"%s\" authzid=\"%s\"\n",
564 (long) (conn ? conn->c_connid : -1),
565 authcid ? authcid : "<empty>",
566 authzid ? authzid : "<empty>" );
569 /* Figure out how much data we have for the dn */
570 rc = sasl_getprop( conn->c_sasl_context, SASL_REALM, (void **)&realm );
571 if( rc != SASL_OK && rc != SASL_NOTDONE ) {
573 LDAP_LOG(( "sasl", LDAP_LEVEL_ERR,
574 "slap_sasl_authorize: getprop(REALM) failed.\n" ));
576 Debug(LDAP_DEBUG_TRACE,
577 "authorize: getprop(REALM) failed!\n", 0,0,0);
579 *errstr = "Could not extract realm";
583 /* Convert the identities to DN's. If no authzid was given, client will
584 be bound as the DN matching their username */
585 if ( conn->c_sasl_bind_mech.bv_len == ext_bv.bv_len
586 && ( strcasecmp( ext_bv.bv_val, conn->c_sasl_bind_mech.bv_val ) == 0 )
587 && authcid[0] == '/' ) {
591 xrealm = strchr( authcid, '@' );
592 if ( xrealm ) *xrealm++ = '\0';
594 rc = slap_sasl_getdn( conn, (char *)authcid, 0, xrealm ? xrealm : realm, &authcDN, FLAG_GETDN_AUTHCID );
595 if ( xrealm ) xrealm[-1] = '@';
596 if( rc != LDAP_SUCCESS ) {
597 *errstr = ldap_err2string( rc );
600 if( ( authzid == NULL ) || !strcmp( authcid,authzid ) ) {
602 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
603 "slap_sasl_authorize: conn %d Using authcDN=%s\n",
604 conn ? conn->c_connid : -1, authcDN.bv_val ));
606 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
607 "Using authcDN=%s\n", (long) (conn ? conn->c_connid : -1), authcDN.bv_val,0 );
610 *user = authcDN.bv_val;
617 xrealm = strchr( authzid, '@' );
618 if ( xrealm ) *xrealm++ = '\0';
620 rc = slap_sasl_getdn( conn, (char *)authzid, 0, xrealm ? xrealm : realm, &authzDN, FLAG_GETDN_AUTHZID );
621 if ( xrealm ) xrealm[-1] = '@';
622 if( rc != LDAP_SUCCESS ) {
623 ch_free( authcDN.bv_val );
624 *errstr = ldap_err2string( rc );
628 rc = slap_sasl_authorized( &authcDN, &authzDN );
631 LDAP_LOG(( "sasl", LDAP_LEVEL_INFO,
632 "slap_sasl_authorize: conn %ld authorization disallowed (%d)\n",
633 (long)(conn ? conn->c_connid : -1), rc ));
635 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
636 " authorization disallowed (%d)\n",
637 (long) (conn ? conn->c_connid : -1), rc, 0 );
640 *errstr = "not authorized";
641 ch_free( authcDN.bv_val );
642 ch_free( authzDN.bv_val );
647 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
648 "slap_sasl_authorize: conn %d authorization allowed\n",
649 (long)(conn ? conn->c_connid : -1 ) ));
651 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
652 " authorization allowed\n",
653 (long) (conn ? conn->c_connid : -1), 0, 0 );
657 ch_free( authcDN.bv_val );
658 *user = authzDN.bv_val;
662 #endif /* SASL_VERSION_MAJOR >= 2 */
665 slap_sasl_err2ldap( int saslerr )
671 rc = LDAP_SASL_BIND_IN_PROGRESS;
680 rc = LDAP_AUTH_METHOD_NOT_SUPPORTED;
683 rc = LDAP_INVALID_CREDENTIALS;
686 rc = LDAP_INSUFFICIENT_ACCESS;
690 rc = LDAP_INAPPROPRIATE_AUTH;
702 int slap_sasl_init( void )
704 #ifdef HAVE_CYRUS_SASL
706 static sasl_callback_t server_callbacks[] = {
707 { SASL_CB_LOG, &slap_sasl_log, NULL },
708 { SASL_CB_LIST_END, NULL, NULL }
718 ldap_pvt_sasl_mutex_new,
719 ldap_pvt_sasl_mutex_lock,
720 ldap_pvt_sasl_mutex_unlock,
721 ldap_pvt_sasl_mutex_dispose );
723 /* should provide callbacks for logging */
724 /* server name should be configurable */
725 rc = sasl_server_init( server_callbacks, "slapd" );
727 if( rc != SASL_OK ) {
729 LDAP_LOG(( "sasl", LDAP_LEVEL_INFO,
730 "slap_sasl_init: init failed.\n" ));
732 Debug( LDAP_DEBUG_ANY, "sasl_server_init failed\n",
740 LDAP_LOG(( "sasl", LDAP_LEVEL_INFO,
741 "slap_sasl_init: initialized!\n"));
743 Debug( LDAP_DEBUG_TRACE, "slap_sasl_init: initialized!\n",
748 /* default security properties */
749 memset( &sasl_secprops, '\0', sizeof(sasl_secprops) );
750 sasl_secprops.max_ssf = INT_MAX;
751 sasl_secprops.maxbufsize = 65536;
752 sasl_secprops.security_flags = SASL_SEC_NOPLAINTEXT|SASL_SEC_NOANONYMOUS;
758 int slap_sasl_destroy( void )
760 #ifdef HAVE_CYRUS_SASL
769 int slap_sasl_open( Connection *conn )
771 int cb, sc = LDAP_SUCCESS;
772 #if SASL_VERSION_MAJOR >= 2
773 char *ipremoteport = NULL, *iplocalport = NULL;
776 #ifdef HAVE_CYRUS_SASL
777 sasl_conn_t *ctx = NULL;
778 sasl_callback_t *session_callbacks;
780 assert( conn->c_sasl_context == NULL );
781 assert( conn->c_sasl_extra == NULL );
783 conn->c_sasl_layers = 0;
786 #if SASL_VERSION_MAJOR >= 2
787 ch_calloc( 5, sizeof(sasl_callback_t));
789 ch_calloc( 3, sizeof(sasl_callback_t));
791 conn->c_sasl_extra = session_callbacks;
793 session_callbacks[cb=0].id = SASL_CB_LOG;
794 session_callbacks[cb].proc = &slap_sasl_log;
795 session_callbacks[cb++].context = conn;
797 session_callbacks[cb].id = SASL_CB_PROXY_POLICY;
798 session_callbacks[cb].proc = &slap_sasl_authorize;
799 session_callbacks[cb++].context = conn;
801 #if SASL_VERSION_MAJOR >= 2
802 #if 0 /* CANON isn't for what you think it does. */
803 session_callbacks[cb].id = SASL_CB_CANON_USER;
804 session_callbacks[cb].proc = &slap_sasl_canonicalize;
805 session_callbacks[cb++].context = conn;
808 /* XXXX: this should be conditional */
809 session_callbacks[cb].id = SASL_CB_SERVER_USERDB_CHECKPASS;
810 session_callbacks[cb].proc = &slap_sasl_checkpass;
811 session_callbacks[cb++].context = conn;
814 session_callbacks[cb].id = SASL_CB_LIST_END;
815 session_callbacks[cb].proc = NULL;
816 session_callbacks[cb++].context = NULL;
818 if( global_host == NULL ) {
819 global_host = ldap_pvt_get_fqdn( NULL );
822 /* create new SASL context */
823 #if SASL_VERSION_MAJOR >= 2
824 if ( conn->c_sock_name.bv_len != 0 &&
825 strncmp( conn->c_sock_name.bv_val, "IP=", 3 ) == 0) {
828 iplocalport = ch_strdup( conn->c_sock_name.bv_val + 3 );
829 /* Convert IPv6 addresses to address;port syntax. */
830 p = strrchr( iplocalport, ' ' );
831 /* Convert IPv4 addresses to address;port syntax. */
832 if ( p == NULL ) p = strchr( iplocalport, ':' );
837 if ( conn->c_peer_name.bv_len != 0 &&
838 strncmp( conn->c_peer_name.bv_val, "IP=", 3 ) == 0) {
841 ipremoteport = ch_strdup( conn->c_peer_name.bv_val + 3 );
842 /* Convert IPv6 addresses to address;port syntax. */
843 p = strrchr( ipremoteport, ' ' );
844 /* Convert IPv4 addresses to address;port syntax. */
845 if ( p == NULL ) p = strchr( ipremoteport, ':' );
850 sc = sasl_server_new( "ldap", global_host, global_realm,
851 iplocalport, ipremoteport, session_callbacks, 0, &ctx );
852 if ( iplocalport != NULL ) {
853 ch_free( iplocalport );
855 if ( ipremoteport != NULL ) {
856 ch_free( ipremoteport );
859 sc = sasl_server_new( "ldap", global_host, global_realm,
860 session_callbacks, SASL_SECURITY_LAYER, &ctx );
863 if( sc != SASL_OK ) {
865 LDAP_LOG(( "sasl", LDAP_LEVEL_ERR,
866 "slap_sasl_open: sasl_server_new failed: %d\n", sc ));
868 Debug( LDAP_DEBUG_ANY, "sasl_server_new failed: %d\n",
875 conn->c_sasl_context = ctx;
877 if( sc == SASL_OK ) {
878 sc = sasl_setprop( ctx,
879 SASL_SEC_PROPS, &sasl_secprops );
881 if( sc != SASL_OK ) {
883 LDAP_LOG(( "sasl", LDAP_LEVEL_ERR,
884 "slap_sasl_open: sasl_setprop failed: %d \n", sc ));
886 Debug( LDAP_DEBUG_ANY, "sasl_setprop failed: %d\n",
890 slap_sasl_close( conn );
895 sc = slap_sasl_err2ldap( sc );
900 int slap_sasl_external(
903 const char *auth_id )
905 #if SASL_VERSION_MAJOR >= 2
907 sasl_conn_t *ctx = conn->c_sasl_context;
910 return LDAP_UNAVAILABLE;
913 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL, &ssf );
915 if ( sc != SASL_OK ) {
919 sc = sasl_setprop( ctx, SASL_AUTH_EXTERNAL, auth_id );
921 if ( sc != SASL_OK ) {
925 #elif defined(HAVE_CYRUS_SASL)
927 sasl_conn_t *ctx = conn->c_sasl_context;
928 sasl_external_properties_t extprops;
931 return LDAP_UNAVAILABLE;
934 memset( &extprops, '\0', sizeof(extprops) );
936 extprops.auth_id = (char *) auth_id;
938 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL,
939 (void *) &extprops );
941 if ( sc != SASL_OK ) {
949 int slap_sasl_reset( Connection *conn )
951 #ifdef HAVE_CYRUS_SASL
952 sasl_conn_t *ctx = conn->c_sasl_context;
957 /* must return "anonymous" */
961 char ** slap_sasl_mechs( Connection *conn )
965 #ifdef HAVE_CYRUS_SASL
966 sasl_conn_t *ctx = conn->c_sasl_context;
970 SASL_CONST char *mechstr;
972 sc = sasl_listmech( ctx,
973 NULL, NULL, ",", NULL,
974 &mechstr, NULL, NULL );
976 if( sc != SASL_OK ) {
978 LDAP_LOG(( "sasl", LDAP_LEVEL_ERR,
979 "slap_sasl_mechs: sasl_listmech failed: %d\n", sc ));
981 Debug( LDAP_DEBUG_ANY, "slap_sasl_listmech failed: %d\n",
988 mechs = str2charray( mechstr, "," );
990 #if SASL_VERSION_MAJOR < 2
999 int slap_sasl_close( Connection *conn )
1001 #ifdef HAVE_CYRUS_SASL
1002 sasl_conn_t *ctx = conn->c_sasl_context;
1005 sasl_dispose( &ctx );
1008 conn->c_sasl_context = NULL;
1010 free( conn->c_sasl_extra );
1011 conn->c_sasl_extra = NULL;
1014 return LDAP_SUCCESS;
1022 struct berval *cred,
1028 #ifdef HAVE_CYRUS_SASL
1029 sasl_conn_t *ctx = conn->c_sasl_context;
1030 struct berval response;
1031 unsigned reslen = 0;
1032 const char *errstr = NULL;
1036 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
1037 "sasl_bind: conn %ld dn=\"%s\" mech=%s datalen=%ld\n",
1039 dn->bv_len ? dn->bv_val : "",
1040 conn->c_sasl_bind_in_progress ? "<continuing>" : conn->c_sasl_bind_mech.bv_val,
1041 cred ? cred->bv_len : 0 ));
1043 Debug(LDAP_DEBUG_ARGS,
1044 "==> sasl_bind: dn=\"%s\" mech=%s datalen=%ld\n",
1045 dn->bv_len ? dn->bv_val : "",
1046 conn->c_sasl_bind_in_progress ? "<continuing>":conn->c_sasl_bind_mech.bv_val,
1047 cred ? cred->bv_len : 0 );
1052 send_ldap_result( conn, op, LDAP_UNAVAILABLE,
1053 NULL, "SASL unavailable on this session", NULL, NULL );
1057 #if SASL_VERSION_MAJOR >= 2
1058 #define START( ctx, mech, cred, clen, resp, rlen, err ) \
1059 sasl_server_start( ctx, mech, cred, clen, resp, rlen )
1060 #define STEP( ctx, cred, clen, resp, rlen, err ) \
1061 sasl_server_step( ctx, cred, clen, resp, rlen )
1063 #define START( ctx, mech, cred, clen, resp, rlen, err ) \
1064 sasl_server_start( ctx, mech, cred, clen, resp, rlen, err )
1065 #define STEP( ctx, cred, clen, resp, rlen, err ) \
1066 sasl_server_step( ctx, cred, clen, resp, rlen, err )
1069 if ( !conn->c_sasl_bind_in_progress ) {
1071 conn->c_sasl_bind_mech.bv_val,
1072 cred->bv_len ? cred->bv_val : "",
1074 (SASL_CONST char **)&response.bv_val, &reslen, &errstr );
1078 cred->bv_val, cred->bv_len,
1079 (SASL_CONST char **)&response.bv_val, &reslen, &errstr );
1082 response.bv_len = reslen;
1084 if ( sc == SASL_OK ) {
1085 char *username = NULL;
1088 #if SASL_VERSION_MAJOR >= 2
1089 sc = sasl_getprop( ctx, SASL_DEFUSERREALM, (const void **)&realm );
1091 sc = sasl_getprop( ctx, SASL_REALM, (void **)&realm );
1093 sc = sasl_getprop( ctx,
1094 SASL_USERNAME, (SASL_CONST void **)&username );
1096 if ( sc != SASL_OK ) {
1098 LDAP_LOG(( "sasl", LDAP_LEVEL_ERR,
1099 "slap_sasl_bind: getprop(USERNAME) failed: %d\n", sc ));
1101 Debug(LDAP_DEBUG_TRACE,
1102 "slap_sasl_bind: getprop(USERNAME) failed!\n",
1107 send_ldap_result( conn, op, rc = slap_sasl_err2ldap( sc ),
1108 NULL, "no SASL username", NULL, NULL );
1111 sasl_ssf_t *ssf = NULL;
1114 ber_str2bv( username, 0, 1, edn );
1116 (void) sasl_getprop( ctx, SASL_SSF, (void *)&ssf );
1117 *ssfp = ssf ? *ssf : 0;
1120 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1121 conn->c_sasl_layers++;
1122 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1125 send_ldap_sasl( conn, op, rc,
1126 NULL, NULL, NULL, NULL,
1127 response.bv_len ? &response : NULL );
1130 } else if ( sc == SASL_CONTINUE ) {
1131 send_ldap_sasl( conn, op, rc = LDAP_SASL_BIND_IN_PROGRESS,
1132 NULL, NULL, NULL, NULL, &response );
1135 #if SASL_VERSION_MAJOR >= 2
1136 errstr = sasl_errdetail( ctx );
1138 send_ldap_result( conn, op, rc = slap_sasl_err2ldap( sc ),
1139 NULL, errstr, NULL, NULL );
1142 #if SASL_VERSION_MAJOR < 2
1143 if( response.bv_len ) {
1144 ch_free( response.bv_val );
1149 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
1150 "slap_sasl_bind: rc=%d\n", rc ));
1152 Debug(LDAP_DEBUG_TRACE, "<== slap_sasl_bind: rc=%d\n", rc, 0, 0);
1157 send_ldap_result( conn, op, rc = LDAP_UNAVAILABLE,
1158 NULL, "SASL not supported", NULL, NULL );
1164 char* slap_sasl_secprops( const char *in )
1166 #ifdef HAVE_CYRUS_SASL
1167 int rc = ldap_pvt_sasl_secprops( in, &sasl_secprops );
1169 return rc == LDAP_SUCCESS ? NULL : "Invalid security properties";
1171 return "SASL not supported";