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 );
358 #if 0 /* CANON isn't for what you think it does. */
360 slap_sasl_canonicalize(
366 const char *user_realm,
371 Connection *conn = (Connection *)context;
378 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
379 "slap_sasl_canonicalize: conn %d %s=\"%s\"\n",
380 conn ? conn->c_connid : -1,
381 (flags == SASL_CU_AUTHID) ? "authcid" : "authzid",
382 in ? in : "<empty>" ));
384 Debug( LDAP_DEBUG_ARGS, "SASL Canonicalize [conn=%ld]: "
386 conn ? conn->c_connid : -1,
387 (flags == SASL_CU_AUTHID) ? "authcid" : "authzid",
388 in ? in : "<empty>" );
391 rc = slap_sasl_getdn( conn, (char *)in, (char *)user_realm, &dn,
392 (flags == SASL_CU_AUTHID) ? FLAG_GETDN_AUTHCID : FLAG_GETDN_AUTHZID );
393 if ( rc != LDAP_SUCCESS ) {
394 sasl_seterror( sconn, 0, ldap_err2string( rc ) );
398 if ( out_max < dn.bv_len ) {
402 AC_MEMCPY( out, dn.bv_val, dn.bv_len );
403 out[dn.bv_len] = '\0';
405 *out_len = dn.bv_len;
407 ch_free( dn.bv_val );
410 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
411 "slap_sasl_canonicalize: conn %d %s=\"%s\"\n",
412 conn ? conn->c_connid : -1,
413 (flags == SASL_CU_AUTHID) ? "authcDN" : "authzDN",
416 Debug( LDAP_DEBUG_ARGS, "SASL Canonicalize [conn=%ld]: "
418 conn ? conn->c_connid : -1,
419 (flags == SASL_CU_AUTHID) ? "authcDN" : "authzDN",
427 #define CANON_BUF_SIZE 256 /* from saslint.h */
433 char *requested_user,
437 const char *def_realm,
439 struct propctx *propctx)
441 Connection *conn = (Connection *)context;
442 struct berval authcDN, authzDN;
447 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
448 "slap_sasl_authorize: conn %d authcid=\"%s\" authzid=\"%s\"\n",
449 conn ? conn->c_connid : -1, auth_identity, requested_user));
451 Debug( LDAP_DEBUG_ARGS, "SASL Authorize [conn=%ld]: "
452 "authcid=\"%s\" authzid=\"%s\"\n",
453 conn ? conn->c_connid : -1, auth_identity, requested_user );
456 if ( requested_user )
457 equal = !strcmp( auth_identity, requested_user );
459 realm = strchr( auth_identity, '@' );
463 rc = slap_sasl_getdn( conn, auth_identity, 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 );
483 realm = strchr( requested_user, '@' );
487 rc = slap_sasl_getdn( conn, requested_user, realm ? realm : (char *)def_realm,
488 &authzDN, FLAG_GETDN_AUTHZID );
492 if ( rc != LDAP_SUCCESS ) {
493 free( authcDN.bv_val );
494 sasl_seterror( sconn, 0, ldap_err2string( rc ) );
498 if (authzDN.bv_len > CANON_BUF_SIZE) {
499 free( authcDN.bv_val );
500 free( authzDN.bv_val );
504 rc = slap_sasl_authorized( &authcDN, &authzDN );
505 free( authcDN.bv_val );
506 if ( rc != LDAP_SUCCESS ) {
508 LDAP_LOG(( "sasl", LDAP_LEVEL_INFO,
509 "slap_sasl_authorize: conn %ld authorization disallowed (%d)\n",
510 (long)(conn ? conn->c_connid : -1), rc ));
512 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
513 " authorization disallowed (%d)\n",
514 (long) (conn ? conn->c_connid : -1), rc, 0 );
517 sasl_seterror( sconn, 0, "not authorized" );
518 free( authzDN.bv_val );
521 AC_MEMCPY( requested_user, authzDN.bv_val, authzDN.bv_len );
522 free( authzDN.bv_val );
525 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
526 "slap_sasl_authorize: conn %d authorization allowed\n",
527 (long)(conn ? conn->c_connid : -1 ) ));
529 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
530 " authorization allowed\n",
531 (long) (conn ? conn->c_connid : -1), 0, 0 );
545 struct berval authcDN, authzDN;
547 Connection *conn = context;
553 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
554 "slap_sasl_authorize: conn %d authcid=\"%s\" authzid=\"%s\"\n",
555 conn ? conn->c_connid : -1,
556 authcid ? authcid : "<empty>",
557 authzid ? authzid : "<empty>" ));
559 Debug( LDAP_DEBUG_ARGS, "SASL Authorize [conn=%ld]: "
560 "authcid=\"%s\" authzid=\"%s\"\n",
561 (long) (conn ? conn->c_connid : -1),
562 authcid ? authcid : "<empty>",
563 authzid ? authzid : "<empty>" );
566 /* Figure out how much data we have for the dn */
567 rc = sasl_getprop( conn->c_sasl_context, SASL_REALM, (void **)&realm );
568 if( rc != SASL_OK && rc != SASL_NOTDONE ) {
570 LDAP_LOG(( "sasl", LDAP_LEVEL_ERR,
571 "slap_sasl_authorize: getprop(REALM) failed.\n" ));
573 Debug(LDAP_DEBUG_TRACE,
574 "authorize: getprop(REALM) failed!\n", 0,0,0);
576 *errstr = "Could not extract realm";
580 /* Convert the identities to DN's. If no authzid was given, client will
581 be bound as the DN matching their username */
582 rc = slap_sasl_getdn( conn, (char *)authcid, realm, &authcDN, FLAG_GETDN_AUTHCID );
583 if( rc != LDAP_SUCCESS ) {
584 *errstr = ldap_err2string( rc );
587 if( ( authzid == NULL ) || !strcmp( authcid,authzid ) ) {
589 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
590 "slap_sasl_authorize: conn %d Using authcDN=%s\n",
591 conn ? conn->c_connid : -1, authcDN.bv_val ));
593 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
594 "Using authcDN=%s\n", (long) (conn ? conn->c_connid : -1), authcDN.bv_val,0 );
597 *user = authcDN.bv_val;
601 rc = slap_sasl_getdn( conn, (char *)authzid, realm, &authzDN, FLAG_GETDN_AUTHZID );
602 if( rc != LDAP_SUCCESS ) {
603 ch_free( authcDN.bv_val );
604 *errstr = ldap_err2string( rc );
608 rc = slap_sasl_authorized( &authcDN, &authzDN );
611 LDAP_LOG(( "sasl", LDAP_LEVEL_INFO,
612 "slap_sasl_authorize: conn %ld authorization disallowed (%d)\n",
613 (long)(conn ? conn->c_connid : -1), rc ));
615 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
616 " authorization disallowed (%d)\n",
617 (long) (conn ? conn->c_connid : -1), rc, 0 );
620 *errstr = "not authorized";
621 ch_free( authcDN.bv_val );
622 ch_free( authzDN.bv_val );
627 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
628 "slap_sasl_authorize: conn %d authorization allowed\n",
629 (long)(conn ? conn->c_connid : -1 ) ));
631 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
632 " authorization allowed\n",
633 (long) (conn ? conn->c_connid : -1), 0, 0 );
637 ch_free( authcDN.bv_val );
638 *user = authzDN.bv_val;
642 #endif /* SASL_VERSION_MAJOR >= 2 */
645 slap_sasl_err2ldap( int saslerr )
651 rc = LDAP_SASL_BIND_IN_PROGRESS;
660 rc = LDAP_AUTH_METHOD_NOT_SUPPORTED;
663 rc = LDAP_INVALID_CREDENTIALS;
666 rc = LDAP_INSUFFICIENT_ACCESS;
670 rc = LDAP_INAPPROPRIATE_AUTH;
682 int slap_sasl_init( void )
684 #ifdef HAVE_CYRUS_SASL
686 static sasl_callback_t server_callbacks[] = {
687 { SASL_CB_LOG, &slap_sasl_log, NULL },
688 { SASL_CB_LIST_END, NULL, NULL }
698 ldap_pvt_sasl_mutex_new,
699 ldap_pvt_sasl_mutex_lock,
700 ldap_pvt_sasl_mutex_unlock,
701 ldap_pvt_sasl_mutex_dispose );
703 /* should provide callbacks for logging */
704 /* server name should be configurable */
705 rc = sasl_server_init( server_callbacks, "slapd" );
707 if( rc != SASL_OK ) {
709 LDAP_LOG(( "sasl", LDAP_LEVEL_INFO,
710 "slap_sasl_init: init failed.\n" ));
712 Debug( LDAP_DEBUG_ANY, "sasl_server_init failed\n",
720 LDAP_LOG(( "sasl", LDAP_LEVEL_INFO,
721 "slap_sasl_init: initialized!\n"));
723 Debug( LDAP_DEBUG_TRACE, "slap_sasl_init: initialized!\n",
728 /* default security properties */
729 memset( &sasl_secprops, '\0', sizeof(sasl_secprops) );
730 sasl_secprops.max_ssf = INT_MAX;
731 sasl_secprops.maxbufsize = 65536;
732 sasl_secprops.security_flags = SASL_SEC_NOPLAINTEXT|SASL_SEC_NOANONYMOUS;
738 int slap_sasl_destroy( void )
740 #ifdef HAVE_CYRUS_SASL
749 int slap_sasl_open( Connection *conn )
751 int cb, sc = LDAP_SUCCESS;
752 #if SASL_VERSION_MAJOR >= 2
753 char *ipremoteport = NULL, *iplocalport = NULL;
756 #ifdef HAVE_CYRUS_SASL
757 sasl_conn_t *ctx = NULL;
758 sasl_callback_t *session_callbacks;
760 assert( conn->c_sasl_context == NULL );
761 assert( conn->c_sasl_extra == NULL );
763 conn->c_sasl_layers = 0;
766 #if SASL_VERSION_MAJOR >= 2
767 ch_calloc( 5, sizeof(sasl_callback_t));
769 ch_calloc( 3, sizeof(sasl_callback_t));
771 conn->c_sasl_extra = session_callbacks;
773 session_callbacks[cb=0].id = SASL_CB_LOG;
774 session_callbacks[cb].proc = &slap_sasl_log;
775 session_callbacks[cb++].context = conn;
777 session_callbacks[cb].id = SASL_CB_PROXY_POLICY;
778 session_callbacks[cb].proc = &slap_sasl_authorize;
779 session_callbacks[cb++].context = conn;
781 #if SASL_VERSION_MAJOR >= 2
782 #if 0 /* CANON isn't for what you think it does. */
783 session_callbacks[cb].id = SASL_CB_CANON_USER;
784 session_callbacks[cb].proc = &slap_sasl_canonicalize;
785 session_callbacks[cb++].context = conn;
788 /* XXXX: this should be conditional */
789 session_callbacks[cb].id = SASL_CB_SERVER_USERDB_CHECKPASS;
790 session_callbacks[cb].proc = &slap_sasl_checkpass;
791 session_callbacks[cb++].context = conn;
794 session_callbacks[cb].id = SASL_CB_LIST_END;
795 session_callbacks[cb].proc = NULL;
796 session_callbacks[cb++].context = NULL;
798 if( global_host == NULL ) {
799 global_host = ldap_pvt_get_fqdn( NULL );
802 /* create new SASL context */
803 #if SASL_VERSION_MAJOR >= 2
804 if ( conn->c_sock_name.bv_len != 0 &&
805 strncmp( conn->c_sock_name.bv_val, "IP=", 3 ) == 0) {
808 iplocalport = ch_strdup( conn->c_sock_name.bv_val + 3 );
809 /* Convert IPv6 addresses to address;port syntax. */
810 p = strrchr( iplocalport, ' ' );
811 /* Convert IPv4 addresses to address;port syntax. */
812 if ( p == NULL ) p = strchr( iplocalport, ':' );
817 if ( conn->c_peer_name.bv_len != 0 &&
818 strncmp( conn->c_peer_name.bv_val, "IP=", 3 ) == 0) {
821 ipremoteport = ch_strdup( conn->c_peer_name.bv_val + 3 );
822 /* Convert IPv6 addresses to address;port syntax. */
823 p = strrchr( ipremoteport, ' ' );
824 /* Convert IPv4 addresses to address;port syntax. */
825 if ( p == NULL ) p = strchr( ipremoteport, ':' );
830 sc = sasl_server_new( "ldap", global_host, global_realm,
831 iplocalport, ipremoteport, session_callbacks, 0, &ctx );
832 if ( iplocalport != NULL ) {
833 ch_free( iplocalport );
835 if ( ipremoteport != NULL ) {
836 ch_free( ipremoteport );
839 sc = sasl_server_new( "ldap", global_host, global_realm,
840 session_callbacks, SASL_SECURITY_LAYER, &ctx );
843 if( sc != SASL_OK ) {
845 LDAP_LOG(( "sasl", LDAP_LEVEL_ERR,
846 "slap_sasl_open: sasl_server_new failed: %d\n", sc ));
848 Debug( LDAP_DEBUG_ANY, "sasl_server_new failed: %d\n",
855 conn->c_sasl_context = ctx;
857 if( sc == SASL_OK ) {
858 sc = sasl_setprop( ctx,
859 SASL_SEC_PROPS, &sasl_secprops );
861 if( sc != SASL_OK ) {
863 LDAP_LOG(( "sasl", LDAP_LEVEL_ERR,
864 "slap_sasl_open: sasl_setprop failed: %d \n", sc ));
866 Debug( LDAP_DEBUG_ANY, "sasl_setprop failed: %d\n",
870 slap_sasl_close( conn );
875 sc = slap_sasl_err2ldap( sc );
880 int slap_sasl_external(
883 const char *auth_id )
885 #if SASL_VERSION_MAJOR >= 2
887 sasl_conn_t *ctx = conn->c_sasl_context;
890 return LDAP_UNAVAILABLE;
893 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL, &ssf );
895 if ( sc != SASL_OK ) {
899 sc = sasl_setprop( ctx, SASL_AUTH_EXTERNAL, auth_id );
901 if ( sc != SASL_OK ) {
905 #elif defined(HAVE_CYRUS_SASL)
907 sasl_conn_t *ctx = conn->c_sasl_context;
908 sasl_external_properties_t extprops;
911 return LDAP_UNAVAILABLE;
914 memset( &extprops, '\0', sizeof(extprops) );
916 extprops.auth_id = (char *) auth_id;
918 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL,
919 (void *) &extprops );
921 if ( sc != SASL_OK ) {
929 int slap_sasl_reset( Connection *conn )
931 #ifdef HAVE_CYRUS_SASL
932 sasl_conn_t *ctx = conn->c_sasl_context;
937 /* must return "anonymous" */
941 char ** slap_sasl_mechs( Connection *conn )
945 #ifdef HAVE_CYRUS_SASL
946 sasl_conn_t *ctx = conn->c_sasl_context;
950 SASL_CONST char *mechstr;
952 sc = sasl_listmech( ctx,
953 NULL, NULL, ",", NULL,
954 &mechstr, NULL, NULL );
956 if( sc != SASL_OK ) {
958 LDAP_LOG(( "sasl", LDAP_LEVEL_ERR,
959 "slap_sasl_mechs: sasl_listmech failed: %d\n", sc ));
961 Debug( LDAP_DEBUG_ANY, "slap_sasl_listmech failed: %d\n",
968 mechs = str2charray( mechstr, "," );
970 #if SASL_VERSION_MAJOR < 2
979 int slap_sasl_close( Connection *conn )
981 #ifdef HAVE_CYRUS_SASL
982 sasl_conn_t *ctx = conn->c_sasl_context;
985 sasl_dispose( &ctx );
988 conn->c_sasl_context = NULL;
990 free( conn->c_sasl_extra );
991 conn->c_sasl_extra = NULL;
1002 struct berval *cred,
1008 #ifdef HAVE_CYRUS_SASL
1009 sasl_conn_t *ctx = conn->c_sasl_context;
1010 struct berval response;
1011 unsigned reslen = 0;
1012 const char *errstr = NULL;
1016 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
1017 "sasl_bind: conn %ld dn=\"%s\" mech=%s datalen=%ld\n",
1019 dn->bv_len ? dn->bv_val : "",
1020 conn->c_sasl_bind_in_progress ? "<continuing>" : conn->c_sasl_bind_mech.bv_val,
1021 cred ? cred->bv_len : 0 ));
1023 Debug(LDAP_DEBUG_ARGS,
1024 "==> sasl_bind: dn=\"%s\" mech=%s datalen=%ld\n",
1025 dn->bv_len ? dn->bv_val : "",
1026 conn->c_sasl_bind_in_progress ? "<continuing>":conn->c_sasl_bind_mech.bv_val,
1027 cred ? cred->bv_len : 0 );
1032 send_ldap_result( conn, op, LDAP_UNAVAILABLE,
1033 NULL, "SASL unavailable on this session", NULL, NULL );
1037 #if SASL_VERSION_MAJOR >= 2
1038 #define START( ctx, mech, cred, clen, resp, rlen, err ) \
1039 sasl_server_start( ctx, mech, cred, clen, resp, rlen )
1040 #define STEP( ctx, cred, clen, resp, rlen, err ) \
1041 sasl_server_step( ctx, cred, clen, resp, rlen )
1043 #define START( ctx, mech, cred, clen, resp, rlen, err ) \
1044 sasl_server_start( ctx, mech, cred, clen, resp, rlen, err )
1045 #define STEP( ctx, cred, clen, resp, rlen, err ) \
1046 sasl_server_step( ctx, cred, clen, resp, rlen, err )
1049 if ( !conn->c_sasl_bind_in_progress ) {
1051 conn->c_sasl_bind_mech.bv_val,
1052 cred->bv_len ? cred->bv_val : "",
1054 (SASL_CONST char **)&response.bv_val, &reslen, &errstr );
1058 cred->bv_val, cred->bv_len,
1059 (SASL_CONST char **)&response.bv_val, &reslen, &errstr );
1062 response.bv_len = reslen;
1064 if ( sc == SASL_OK ) {
1065 char *username = NULL;
1068 #if SASL_VERSION_MAJOR >= 2
1069 sc = sasl_getprop( ctx, SASL_DEFUSERREALM, (const void **)&realm );
1071 sc = sasl_getprop( ctx, SASL_REALM, (void **)&realm );
1073 sc = sasl_getprop( ctx,
1074 SASL_USERNAME, (SASL_CONST void **)&username );
1076 if ( sc != SASL_OK ) {
1078 LDAP_LOG(( "sasl", LDAP_LEVEL_ERR,
1079 "slap_sasl_bind: getprop(USERNAME) failed: %d\n", sc ));
1081 Debug(LDAP_DEBUG_TRACE,
1082 "slap_sasl_bind: getprop(USERNAME) failed!\n",
1087 send_ldap_result( conn, op, rc = slap_sasl_err2ldap( sc ),
1088 NULL, "no SASL username", NULL, NULL );
1091 rc = slap_sasl_getdn( conn, username, realm, edn, FLAG_GETDN_FINAL );
1093 if( rc == LDAP_SUCCESS ) {
1094 sasl_ssf_t *ssf = NULL;
1095 (void) sasl_getprop( ctx, SASL_SSF, (void *)&ssf );
1096 *ssfp = ssf ? *ssf : 0;
1099 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1100 conn->c_sasl_layers++;
1101 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1104 send_ldap_sasl( conn, op, rc,
1105 NULL, NULL, NULL, NULL,
1106 response.bv_len ? &response : NULL );
1109 #if SASL_VERSION_MAJOR >= 2
1110 errstr = sasl_errdetail( ctx );
1112 send_ldap_result( conn, op, rc,
1113 NULL, errstr, NULL, 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";