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 #include <sasl/saslplug.h>
30 #define SASL_CONST const
41 /* Flags for telling slap_sasl_getdn() what type of identity is being passed */
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
134 The "dn:" prefix is no longer used anywhere inside slapd. It is only used
135 on strings passed in directly from SASL.
136 -Howard Chu, Symas Corp.
142 static struct berval ext_bv = { sizeof("EXTERNAL")-1, "EXTERNAL" };
144 int slap_sasl_getdn( Connection *conn, char *id, int len,
145 char *user_realm, struct berval *dn, int flags )
148 int rc, is_dn = 0, do_norm = 1;
153 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
154 "slap_sasl_getdn: conn %d id=%s\n",
155 conn ? conn->c_connid : -1,
156 id ? (*id ? id : "<empty>") : "NULL" ));
158 Debug( LDAP_DEBUG_ARGS, "slap_sasl_getdn: id=%s\n",
159 id?(*id?id:"<empty>"):"NULL",0,0 );
166 if ( len == 0 ) len = strlen( id );
168 /* Blatantly anonymous ID */
169 if ( len == sizeof("anonymous") - 1 &&
170 !strcasecmp( id, "anonymous" ) ) {
171 return( LDAP_SUCCESS );
177 ctx = conn->c_sasl_context;
179 /* An authcID needs to be converted to authzID form */
180 if( flags & FLAG_GETDN_AUTHCID ) {
181 if( conn->c_is_tls && conn->c_sasl_bind_mech.bv_len == ext_bv.bv_len
182 && ( strcasecmp( ext_bv.bv_val, conn->c_sasl_bind_mech.bv_val ) == 0 ) ) {
183 /* X.509 DN is already normalized */
188 /* convert to u:<username> form */
191 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( conn, 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
283 static const char *slap_propnames[] = { "*authcDN", "*authzDN", NULL };
288 sasl_server_params_t *sparams,
295 const struct propval *list, *cur;
297 AttributeDescription *ad;
300 list = sparams->utils->prop_get( sparams->propctx );
302 /* Find our DN first */
303 for( cur = list; cur->name; cur++ ) {
304 if ( cur->name[0] == '*' ) {
305 if ( (flags & SASL_AUXPROP_AUTHZID) &&
306 !strcmp( cur->name, slap_propnames[1] ) ) {
307 if ( cur->values && cur->values[0] )
308 AC_MEMCPY( &dn, cur->values[0], sizeof( dn ) );
311 if ( !strcmp( cur->name, slap_propnames[0] ) ) {
312 AC_MEMCPY( &dn, cur->values[0], sizeof( dn ) );
313 if ( !(flags & SASL_AUXPROP_AUTHZID) )
319 /* Now fetch the rest */
320 for( cur = list; cur->name; cur++ ) {
321 const char *name = cur->name;
323 if ( name[0] == '*' ) {
324 if ( flags & SASL_AUXPROP_AUTHZID ) continue;
326 } else if ( !(flags & SASL_AUXPROP_AUTHZID ) )
330 if ( !(flags & SASL_AUXPROP_OVERRIDE) ) continue;
331 sparams->utils->prop_erase( sparams->propctx, cur->name );
334 rc = slap_str2ad( name, &ad, &text );
335 if ( rc != LDAP_SUCCESS ) {
337 LDAP_LOG(( "sasl", LDAP_LEVEL_DETAIL1,
338 "slap_auxprop: str2ad(%s): %s\n", name, text ));
340 Debug( LDAP_DEBUG_TRACE,
341 "slap_auxprop: str2ad(%s): %s\n", name, text, 0 );
343 rc = slap_str2undef_ad( name, &ad, &text );
344 if ( rc != LDAP_SUCCESS ) continue;
346 rc = backend_attribute( NULL,NULL,NULL,NULL, &dn, ad, &vals );
347 if ( rc != LDAP_SUCCESS ) continue;
348 for ( bv = vals; bv->bv_val; bv++ ) {
349 sparams->utils->prop_set( sparams->propctx, cur->name,
350 bv->bv_val, bv->bv_len );
352 ber_bvarray_free( vals );
356 static sasl_auxprop_plug_t slap_auxprop_plugin = {
359 NULL, /* glob_context */
360 NULL, /* auxprop_free */
368 const sasl_utils_t *utils,
371 sasl_auxprop_plug_t **plug,
372 const char *plugname)
374 if ( !out_version | !plug ) return SASL_BADPARAM;
376 if ( max_version < SASL_AUXPROP_PLUG_VERSION ) return SASL_BADVERS;
378 *out_version = SASL_AUXPROP_PLUG_VERSION;
379 *plug = &slap_auxprop_plugin;
387 const char *username,
390 struct propctx *propctx)
392 Connection *conn = (Connection *)context;
393 struct berval dn, cred;
397 cred.bv_val = (char *)pass;
398 cred.bv_len = passlen;
400 /* SASL will fallback to its own mechanisms if we don't
401 * find an answer here.
404 rc = slap_sasl_getdn( conn, (char *)username, 0, NULL, &dn,
405 FLAG_GETDN_AUTHCID );
406 if ( rc != LDAP_SUCCESS ) {
407 sasl_seterror( sconn, 0, ldap_err2string( rc ) );
411 if ( dn.bv_len == 0 ) {
412 sasl_seterror( sconn, 0,
413 "No password is associated with the Root DSE" );
414 if ( dn.bv_val != NULL ) {
415 ch_free( dn.bv_val );
420 rc = backend_attribute( NULL, NULL, NULL, NULL, &dn,
421 slap_schema.si_ad_userPassword, &vals);
422 if ( rc != LDAP_SUCCESS ) {
423 ch_free( dn.bv_val );
424 sasl_seterror( sconn, 0, ldap_err2string( rc ) );
425 return SASL_NOVERIFY;
430 if ( vals != NULL ) {
431 for ( bv = vals; bv->bv_val != NULL; bv++ ) {
432 if ( !lutil_passwd( bv, &cred, NULL ) ) {
437 ber_bvarray_free( vals );
440 if ( rc != SASL_OK ) {
441 sasl_seterror( sconn, 0,
442 ldap_err2string( LDAP_INVALID_CREDENTIALS ) );
445 ch_free( dn.bv_val );
450 /* Convert a SASL authcid or authzid into a DN. Store the DN in an
451 * auxiliary property, so that we can refer to it in sasl_authorize
452 * without interfering with anything else. Also, the SASL username
453 * buffer is constrained to 256 characters, and our DNs could be
454 * much longer (totally arbitrary length)...
457 slap_sasl_canonicalize(
463 const char *user_realm,
468 Connection *conn = (Connection *)context;
469 struct propctx *props = sasl_auxprop_getctx( sconn );
473 const char *names[2];
478 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
479 "slap_sasl_canonicalize: conn %d %s=\"%s\"\n",
480 conn ? conn->c_connid : -1,
481 (flags & SASL_CU_AUTHID) ? "authcid" : "authzid",
482 in ? in : "<empty>" ));
484 Debug( LDAP_DEBUG_ARGS, "SASL Canonicalize [conn=%ld]: "
486 conn ? conn->c_connid : -1,
487 (flags & SASL_CU_AUTHID) ? "authcid" : "authzid",
488 in ? in : "<empty>" );
491 if ( inlen > out_max )
494 if ( flags == SASL_CU_AUTHZID ) {
495 /* If we got unqualified authzid's, they probably came from SASL
496 * itself just passing the authcid to us. Ignore it.
498 if (strncasecmp(in, "u:", 2) && strncasecmp(in, "dn:", 3)) {
499 AC_MEMCPY( out, in, inlen );
507 /* If using SASL-EXTERNAL, don't modify the ID in any way */
508 if ( conn->c_is_tls && conn->c_sasl_bind_mech.bv_len == ext_bv.bv_len
509 && ( strcasecmp( ext_bv.bv_val, conn->c_sasl_bind_mech.bv_val ) == 0 ) ) {
513 /* Else look for an embedded realm in the name */
514 realm = strchr( in, '@' );
515 if ( realm ) *realm++ = '\0';
517 rc = slap_sasl_getdn( conn, (char *)in, inlen, realm ? realm : (char *)user_realm, &dn,
518 (flags & SASL_CU_AUTHID) ? FLAG_GETDN_AUTHCID : FLAG_GETDN_AUTHZID );
521 if ( rc != LDAP_SUCCESS ) {
522 sasl_seterror( sconn, 0, ldap_err2string( rc ) );
526 AC_MEMCPY( out, in, inlen );
531 if ( flags & SASL_CU_AUTHID )
532 names[0] = slap_propnames[0];
534 names[0] = slap_propnames[1];
537 sasl_auxprop_request( sconn, names );
538 prop_set( props, names[0], (char *)&dn, sizeof( dn ) );
541 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
542 "slap_sasl_canonicalize: conn %d %s=\"%s\"\n",
543 conn ? conn->c_connid : -1,
544 (flags & SASL_CU_AUTHID) ? "authcDN" : "authzDN",
547 Debug( LDAP_DEBUG_ARGS, "SASL Canonicalize [conn=%ld]: "
549 conn ? conn->c_connid : -1,
550 (flags & SASL_CU_AUTHID) ? "authcDN" : "authzDN",
557 #define CANON_BUF_SIZE 256 /* from saslint.h */
563 char *requested_user,
567 const char *def_realm,
569 struct propctx *props)
571 Connection *conn = (Connection *)context;
572 struct propval auxvals[3];
573 struct berval authcDN, authzDN;
577 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
578 "slap_sasl_authorize: conn %d authcid=\"%s\" authzid=\"%s\"\n",
579 conn ? conn->c_connid : -1, auth_identity, requested_user));
581 Debug( LDAP_DEBUG_ARGS, "SASL Authorize [conn=%ld]: "
582 "authcid=\"%s\" authzid=\"%s\"\n",
583 conn ? conn->c_connid : -1, auth_identity, requested_user );
586 prop_getnames( props, slap_propnames, auxvals );
588 /* Nothing to do if no authzID was given */
589 if ( !auxvals[1].name )
592 AC_MEMCPY( &authcDN, auxvals[0].values[0], sizeof(authcDN) );
593 AC_MEMCPY( &authzDN, auxvals[1].values[0], sizeof(authzDN) );
595 rc = slap_sasl_authorized( &authcDN, &authzDN );
596 if ( rc != LDAP_SUCCESS ) {
598 LDAP_LOG(( "sasl", LDAP_LEVEL_INFO,
599 "slap_sasl_authorize: conn %ld authorization disallowed (%d)\n",
600 (long)(conn ? conn->c_connid : -1), rc ));
602 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
603 " authorization disallowed (%d)\n",
604 (long) (conn ? conn->c_connid : -1), rc, 0 );
607 sasl_seterror( sconn, 0, "not authorized" );
612 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
613 "slap_sasl_authorize: conn %d authorization allowed\n",
614 (long)(conn ? conn->c_connid : -1 ) ));
616 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
617 " authorization allowed\n",
618 (long) (conn ? conn->c_connid : -1), 0, 0 );
631 struct berval authcDN, authzDN;
633 Connection *conn = context;
634 char *realm, *xrealm;
639 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
640 "slap_sasl_authorize: conn %d authcid=\"%s\" authzid=\"%s\"\n",
641 conn ? conn->c_connid : -1,
642 authcid ? authcid : "<empty>",
643 authzid ? authzid : "<empty>" ));
645 Debug( LDAP_DEBUG_ARGS, "SASL Authorize [conn=%ld]: "
646 "authcid=\"%s\" authzid=\"%s\"\n",
647 (long) (conn ? conn->c_connid : -1),
648 authcid ? authcid : "<empty>",
649 authzid ? authzid : "<empty>" );
652 /* Figure out how much data we have for the dn */
653 rc = sasl_getprop( conn->c_sasl_context, SASL_REALM, (void **)&realm );
654 if( rc != SASL_OK && rc != SASL_NOTDONE ) {
656 LDAP_LOG(( "sasl", LDAP_LEVEL_ERR,
657 "slap_sasl_authorize: getprop(REALM) failed.\n" ));
659 Debug(LDAP_DEBUG_TRACE,
660 "authorize: getprop(REALM) failed!\n", 0,0,0);
662 *errstr = "Could not extract realm";
666 /* Convert the identities to DN's. If no authzid was given, client will
667 be bound as the DN matching their username */
668 if ( conn->c_is_tls && conn->c_sasl_bind_mech.bv_len == ext_bv.bv_len
669 && ( strcasecmp( ext_bv.bv_val, conn->c_sasl_bind_mech.bv_val ) == 0 ) ) {
673 xrealm = strchr( authcid, '@' );
674 if ( xrealm ) *xrealm++ = '\0';
676 rc = slap_sasl_getdn( conn, (char *)authcid, 0, xrealm ? xrealm : realm, &authcDN, FLAG_GETDN_AUTHCID );
677 if ( xrealm ) xrealm[-1] = '@';
678 if( rc != LDAP_SUCCESS ) {
679 *errstr = ldap_err2string( rc );
682 if( ( authzid == NULL ) || !strcmp( authcid,authzid ) ) {
684 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
685 "slap_sasl_authorize: conn %d Using authcDN=%s\n",
686 conn ? conn->c_connid : -1, authcDN.bv_val ));
688 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
689 "Using authcDN=%s\n", (long) (conn ? conn->c_connid : -1), authcDN.bv_val,0 );
692 *user = authcDN.bv_val;
699 xrealm = strchr( authzid, '@' );
700 if ( xrealm ) *xrealm++ = '\0';
702 rc = slap_sasl_getdn( conn, (char *)authzid, 0, xrealm ? xrealm : realm, &authzDN, FLAG_GETDN_AUTHZID );
703 if ( xrealm ) xrealm[-1] = '@';
704 if( rc != LDAP_SUCCESS ) {
705 ch_free( authcDN.bv_val );
706 *errstr = ldap_err2string( rc );
710 rc = slap_sasl_authorized( &authcDN, &authzDN );
713 LDAP_LOG(( "sasl", LDAP_LEVEL_INFO,
714 "slap_sasl_authorize: conn %ld authorization disallowed (%d)\n",
715 (long)(conn ? conn->c_connid : -1), rc ));
717 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
718 " authorization disallowed (%d)\n",
719 (long) (conn ? conn->c_connid : -1), rc, 0 );
722 *errstr = "not authorized";
723 ch_free( authcDN.bv_val );
724 ch_free( authzDN.bv_val );
729 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
730 "slap_sasl_authorize: conn %d authorization allowed\n",
731 (long)(conn ? conn->c_connid : -1 ) ));
733 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
734 " authorization allowed\n",
735 (long) (conn ? conn->c_connid : -1), 0, 0 );
739 ch_free( authcDN.bv_val );
740 *user = authzDN.bv_val;
744 #endif /* SASL_VERSION_MAJOR >= 2 */
747 slap_sasl_err2ldap( int saslerr )
753 rc = LDAP_SASL_BIND_IN_PROGRESS;
762 rc = LDAP_AUTH_METHOD_NOT_SUPPORTED;
765 rc = LDAP_INVALID_CREDENTIALS;
768 rc = LDAP_INSUFFICIENT_ACCESS;
772 rc = LDAP_INAPPROPRIATE_AUTH;
784 int slap_sasl_init( void )
786 #ifdef HAVE_CYRUS_SASL
788 static sasl_callback_t server_callbacks[] = {
789 { SASL_CB_LOG, &slap_sasl_log, NULL },
790 { SASL_CB_LIST_END, NULL, NULL }
800 ldap_pvt_sasl_mutex_new,
801 ldap_pvt_sasl_mutex_lock,
802 ldap_pvt_sasl_mutex_unlock,
803 ldap_pvt_sasl_mutex_dispose );
805 #if SASL_VERSION_MAJOR >= 2
806 sasl_auxprop_add_plugin( "slapd", slap_auxprop_init );
808 /* should provide callbacks for logging */
809 /* server name should be configurable */
810 rc = sasl_server_init( server_callbacks, "slapd" );
812 if( rc != SASL_OK ) {
814 LDAP_LOG(( "sasl", LDAP_LEVEL_INFO,
815 "slap_sasl_init: init failed.\n" ));
817 Debug( LDAP_DEBUG_ANY, "sasl_server_init failed\n",
825 LDAP_LOG(( "sasl", LDAP_LEVEL_INFO,
826 "slap_sasl_init: initialized!\n"));
828 Debug( LDAP_DEBUG_TRACE, "slap_sasl_init: initialized!\n",
833 /* default security properties */
834 memset( &sasl_secprops, '\0', sizeof(sasl_secprops) );
835 sasl_secprops.max_ssf = INT_MAX;
836 sasl_secprops.maxbufsize = 65536;
837 sasl_secprops.security_flags = SASL_SEC_NOPLAINTEXT|SASL_SEC_NOANONYMOUS;
843 int slap_sasl_destroy( void )
845 #ifdef HAVE_CYRUS_SASL
854 int slap_sasl_open( Connection *conn )
856 int cb, sc = LDAP_SUCCESS;
857 #if SASL_VERSION_MAJOR >= 2
858 char *ipremoteport = NULL, *iplocalport = NULL;
861 #ifdef HAVE_CYRUS_SASL
862 sasl_conn_t *ctx = NULL;
863 sasl_callback_t *session_callbacks;
865 assert( conn->c_sasl_context == NULL );
866 assert( conn->c_sasl_extra == NULL );
868 conn->c_sasl_layers = 0;
871 #if SASL_VERSION_MAJOR >= 2
872 ch_calloc( 5, sizeof(sasl_callback_t));
874 ch_calloc( 3, sizeof(sasl_callback_t));
876 conn->c_sasl_extra = session_callbacks;
878 session_callbacks[cb=0].id = SASL_CB_LOG;
879 session_callbacks[cb].proc = &slap_sasl_log;
880 session_callbacks[cb++].context = conn;
882 session_callbacks[cb].id = SASL_CB_PROXY_POLICY;
883 session_callbacks[cb].proc = &slap_sasl_authorize;
884 session_callbacks[cb++].context = conn;
886 #if SASL_VERSION_MAJOR >= 2
887 session_callbacks[cb].id = SASL_CB_CANON_USER;
888 session_callbacks[cb].proc = &slap_sasl_canonicalize;
889 session_callbacks[cb++].context = conn;
891 /* XXXX: this should be conditional */
892 session_callbacks[cb].id = SASL_CB_SERVER_USERDB_CHECKPASS;
893 session_callbacks[cb].proc = &slap_sasl_checkpass;
894 session_callbacks[cb++].context = conn;
897 session_callbacks[cb].id = SASL_CB_LIST_END;
898 session_callbacks[cb].proc = NULL;
899 session_callbacks[cb++].context = NULL;
901 if( global_host == NULL ) {
902 global_host = ldap_pvt_get_fqdn( NULL );
905 /* create new SASL context */
906 #if SASL_VERSION_MAJOR >= 2
907 if ( conn->c_sock_name.bv_len != 0 &&
908 strncmp( conn->c_sock_name.bv_val, "IP=", 3 ) == 0) {
911 iplocalport = ch_strdup( conn->c_sock_name.bv_val + 3 );
912 /* Convert IPv6 addresses to address;port syntax. */
913 p = strrchr( iplocalport, ' ' );
914 /* Convert IPv4 addresses to address;port syntax. */
915 if ( p == NULL ) p = strchr( iplocalport, ':' );
920 if ( conn->c_peer_name.bv_len != 0 &&
921 strncmp( conn->c_peer_name.bv_val, "IP=", 3 ) == 0) {
924 ipremoteport = ch_strdup( conn->c_peer_name.bv_val + 3 );
925 /* Convert IPv6 addresses to address;port syntax. */
926 p = strrchr( ipremoteport, ' ' );
927 /* Convert IPv4 addresses to address;port syntax. */
928 if ( p == NULL ) p = strchr( ipremoteport, ':' );
933 sc = sasl_server_new( "ldap", global_host, global_realm,
934 iplocalport, ipremoteport, session_callbacks, 0, &ctx );
935 if ( iplocalport != NULL ) {
936 ch_free( iplocalport );
938 if ( ipremoteport != NULL ) {
939 ch_free( ipremoteport );
942 sc = sasl_server_new( "ldap", global_host, global_realm,
943 session_callbacks, SASL_SECURITY_LAYER, &ctx );
946 if( sc != SASL_OK ) {
948 LDAP_LOG(( "sasl", LDAP_LEVEL_ERR,
949 "slap_sasl_open: sasl_server_new failed: %d\n", sc ));
951 Debug( LDAP_DEBUG_ANY, "sasl_server_new failed: %d\n",
958 conn->c_sasl_context = ctx;
960 if( sc == SASL_OK ) {
961 sc = sasl_setprop( ctx,
962 SASL_SEC_PROPS, &sasl_secprops );
964 if( sc != SASL_OK ) {
966 LDAP_LOG(( "sasl", LDAP_LEVEL_ERR,
967 "slap_sasl_open: sasl_setprop failed: %d \n", sc ));
969 Debug( LDAP_DEBUG_ANY, "sasl_setprop failed: %d\n",
973 slap_sasl_close( conn );
978 sc = slap_sasl_err2ldap( sc );
983 int slap_sasl_external(
986 const char *auth_id )
988 #if SASL_VERSION_MAJOR >= 2
990 sasl_conn_t *ctx = conn->c_sasl_context;
993 return LDAP_UNAVAILABLE;
996 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL, &ssf );
998 if ( sc != SASL_OK ) {
1002 sc = sasl_setprop( ctx, SASL_AUTH_EXTERNAL, auth_id );
1004 if ( sc != SASL_OK ) {
1008 #elif defined(HAVE_CYRUS_SASL)
1010 sasl_conn_t *ctx = conn->c_sasl_context;
1011 sasl_external_properties_t extprops;
1013 if ( ctx == NULL ) {
1014 return LDAP_UNAVAILABLE;
1017 memset( &extprops, '\0', sizeof(extprops) );
1019 extprops.auth_id = (char *) auth_id;
1021 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL,
1022 (void *) &extprops );
1024 if ( sc != SASL_OK ) {
1029 return LDAP_SUCCESS;
1032 int slap_sasl_reset( Connection *conn )
1034 #ifdef HAVE_CYRUS_SASL
1035 sasl_conn_t *ctx = conn->c_sasl_context;
1040 /* must return "anonymous" */
1041 return LDAP_SUCCESS;
1044 char ** slap_sasl_mechs( Connection *conn )
1046 char **mechs = NULL;
1048 #ifdef HAVE_CYRUS_SASL
1049 sasl_conn_t *ctx = conn->c_sasl_context;
1053 SASL_CONST char *mechstr;
1055 sc = sasl_listmech( ctx,
1056 NULL, NULL, ",", NULL,
1057 &mechstr, NULL, NULL );
1059 if( sc != SASL_OK ) {
1061 LDAP_LOG(( "sasl", LDAP_LEVEL_ERR,
1062 "slap_sasl_mechs: sasl_listmech failed: %d\n", sc ));
1064 Debug( LDAP_DEBUG_ANY, "slap_sasl_listmech failed: %d\n",
1071 mechs = str2charray( mechstr, "," );
1073 #if SASL_VERSION_MAJOR < 2
1082 int slap_sasl_close( Connection *conn )
1084 #ifdef HAVE_CYRUS_SASL
1085 sasl_conn_t *ctx = conn->c_sasl_context;
1088 sasl_dispose( &ctx );
1091 conn->c_sasl_context = NULL;
1093 free( conn->c_sasl_extra );
1094 conn->c_sasl_extra = NULL;
1097 return LDAP_SUCCESS;
1105 struct berval *cred,
1111 #ifdef HAVE_CYRUS_SASL
1112 sasl_conn_t *ctx = conn->c_sasl_context;
1113 struct berval response;
1114 unsigned reslen = 0;
1115 const char *errstr = NULL;
1119 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
1120 "sasl_bind: conn %ld dn=\"%s\" mech=%s datalen=%ld\n",
1122 dn->bv_len ? dn->bv_val : "",
1123 conn->c_sasl_bind_in_progress ? "<continuing>" : conn->c_sasl_bind_mech.bv_val,
1124 cred ? cred->bv_len : 0 ));
1126 Debug(LDAP_DEBUG_ARGS,
1127 "==> sasl_bind: dn=\"%s\" mech=%s datalen=%ld\n",
1128 dn->bv_len ? dn->bv_val : "",
1129 conn->c_sasl_bind_in_progress ? "<continuing>":conn->c_sasl_bind_mech.bv_val,
1130 cred ? cred->bv_len : 0 );
1135 send_ldap_result( conn, op, LDAP_UNAVAILABLE,
1136 NULL, "SASL unavailable on this session", NULL, NULL );
1140 #if SASL_VERSION_MAJOR >= 2
1141 #define START( ctx, mech, cred, clen, resp, rlen, err ) \
1142 sasl_server_start( ctx, mech, cred, clen, resp, rlen )
1143 #define STEP( ctx, cred, clen, resp, rlen, err ) \
1144 sasl_server_step( ctx, cred, clen, resp, rlen )
1146 #define START( ctx, mech, cred, clen, resp, rlen, err ) \
1147 sasl_server_start( ctx, mech, cred, clen, resp, rlen, err )
1148 #define STEP( ctx, cred, clen, resp, rlen, err ) \
1149 sasl_server_step( ctx, cred, clen, resp, rlen, err )
1152 if ( !conn->c_sasl_bind_in_progress ) {
1154 conn->c_sasl_bind_mech.bv_val,
1155 cred->bv_len ? cred->bv_val : "",
1157 (SASL_CONST char **)&response.bv_val, &reslen, &errstr );
1161 cred->bv_val, cred->bv_len,
1162 (SASL_CONST char **)&response.bv_val, &reslen, &errstr );
1165 response.bv_len = reslen;
1167 if ( sc == SASL_OK ) {
1168 #if SASL_VERSION_MAJOR >= 2
1169 struct propctx *props = sasl_auxprop_getctx( ctx );
1170 struct propval vals[3];
1171 sasl_ssf_t *ssf = NULL;
1173 prop_getnames( props, slap_propnames, vals );
1175 AC_MEMCPY( edn, vals[0].values[0], sizeof(*edn) );
1176 if ( vals[1].name ) {
1177 ch_free( edn->bv_val );
1178 AC_MEMCPY( edn, vals[1].values[0], sizeof(*edn) );
1183 (void) sasl_getprop( ctx, SASL_SSF, (void *)&ssf );
1184 *ssfp = ssf ? *ssf : 0;
1187 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1188 conn->c_sasl_layers++;
1189 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1192 send_ldap_sasl( conn, op, rc,
1193 NULL, NULL, NULL, NULL,
1194 response.bv_len ? &response : NULL );
1196 char *username = NULL;
1198 sc = sasl_getprop( ctx,
1199 SASL_USERNAME, (SASL_CONST void **)&username );
1201 if ( sc != SASL_OK ) {
1203 LDAP_LOG(( "sasl", LDAP_LEVEL_ERR,
1204 "slap_sasl_bind: getprop(USERNAME) failed: %d\n", sc ));
1206 Debug(LDAP_DEBUG_TRACE,
1207 "slap_sasl_bind: getprop(USERNAME) failed!\n",
1212 send_ldap_result( conn, op, rc = slap_sasl_err2ldap( sc ),
1213 NULL, "no SASL username", NULL, NULL );
1216 sasl_ssf_t *ssf = NULL;
1219 ber_str2bv( username, 0, 1, edn );
1221 (void) sasl_getprop( ctx, SASL_SSF, (void *)&ssf );
1222 *ssfp = ssf ? *ssf : 0;
1225 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1226 conn->c_sasl_layers++;
1227 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1230 send_ldap_sasl( conn, op, rc,
1231 NULL, NULL, NULL, NULL,
1232 response.bv_len ? &response : NULL );
1236 } else if ( sc == SASL_CONTINUE ) {
1237 send_ldap_sasl( conn, op, rc = LDAP_SASL_BIND_IN_PROGRESS,
1238 NULL, NULL, NULL, NULL, &response );
1241 #if SASL_VERSION_MAJOR >= 2
1242 errstr = sasl_errdetail( ctx );
1244 send_ldap_result( conn, op, rc = slap_sasl_err2ldap( sc ),
1245 NULL, errstr, NULL, NULL );
1248 #if SASL_VERSION_MAJOR < 2
1249 if( response.bv_len ) {
1250 ch_free( response.bv_val );
1255 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
1256 "slap_sasl_bind: rc=%d\n", rc ));
1258 Debug(LDAP_DEBUG_TRACE, "<== slap_sasl_bind: rc=%d\n", rc, 0, 0);
1263 send_ldap_result( conn, op, rc = LDAP_UNAVAILABLE,
1264 NULL, "SASL not supported", NULL, NULL );
1270 char* slap_sasl_secprops( const char *in )
1272 #ifdef HAVE_CYRUS_SASL
1273 int rc = ldap_pvt_sasl_secprops( in, &sasl_secprops );
1275 return rc == LDAP_SUCCESS ? NULL : "Invalid security properties";
1277 return "SASL not supported";