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 ) {
280 return( LDAP_SUCCESS );
283 #if SASL_VERSION_MAJOR >= 2
284 static const char *slap_propnames[] = { "*authcDN", "*authzDN", NULL };
289 sasl_server_params_t *sparams,
296 const struct propval *list, *cur;
298 AttributeDescription *ad;
301 list = sparams->utils->prop_get( sparams->propctx );
303 /* Find our DN first */
304 for( cur = list; cur->name; cur++ ) {
305 if ( cur->name[0] == '*' ) {
306 if ( (flags & SASL_AUXPROP_AUTHZID) &&
307 !strcmp( cur->name, slap_propnames[1] ) ) {
308 if ( cur->values && cur->values[0] )
309 AC_MEMCPY( &dn, cur->values[0], sizeof( dn ) );
312 if ( !strcmp( cur->name, slap_propnames[0] ) ) {
313 AC_MEMCPY( &dn, cur->values[0], sizeof( dn ) );
314 if ( !(flags & SASL_AUXPROP_AUTHZID) )
320 /* Now fetch the rest */
321 for( cur = list; cur->name; cur++ ) {
322 const char *name = cur->name;
324 if ( name[0] == '*' ) {
325 if ( flags & SASL_AUXPROP_AUTHZID ) continue;
327 } else if ( !(flags & SASL_AUXPROP_AUTHZID ) )
331 if ( !(flags & SASL_AUXPROP_OVERRIDE) ) continue;
332 sparams->utils->prop_erase( sparams->propctx, cur->name );
335 rc = slap_str2ad( name, &ad, &text );
336 if ( rc != LDAP_SUCCESS ) {
338 LDAP_LOG(( "sasl", LDAP_LEVEL_DETAIL1,
339 "slap_auxprop: str2ad(%s): %s\n", name, text ));
341 Debug( LDAP_DEBUG_TRACE,
342 "slap_auxprop: str2ad(%s): %s\n", name, text, 0 );
344 rc = slap_str2undef_ad( name, &ad, &text );
345 if ( rc != LDAP_SUCCESS ) continue;
347 rc = backend_attribute( NULL,NULL,NULL,NULL, &dn, ad, &vals );
348 if ( rc != LDAP_SUCCESS ) continue;
349 for ( bv = vals; bv->bv_val; bv++ ) {
350 sparams->utils->prop_set( sparams->propctx, cur->name,
351 bv->bv_val, bv->bv_len );
353 ber_bvarray_free( vals );
357 static sasl_auxprop_plug_t slap_auxprop_plugin = {
360 NULL, /* glob_context */
361 NULL, /* auxprop_free */
369 const sasl_utils_t *utils,
372 sasl_auxprop_plug_t **plug,
373 const char *plugname)
375 if ( !out_version | !plug ) return SASL_BADPARAM;
377 if ( max_version < SASL_AUXPROP_PLUG_VERSION ) return SASL_BADVERS;
379 *out_version = SASL_AUXPROP_PLUG_VERSION;
380 *plug = &slap_auxprop_plugin;
388 const char *username,
391 struct propctx *propctx)
393 Connection *conn = (Connection *)context;
394 struct berval dn, cred;
398 cred.bv_val = (char *)pass;
399 cred.bv_len = passlen;
401 /* SASL will fallback to its own mechanisms if we don't
402 * find an answer here.
405 rc = slap_sasl_getdn( conn, (char *)username, 0, NULL, &dn,
406 FLAG_GETDN_AUTHCID );
407 if ( rc != LDAP_SUCCESS ) {
408 sasl_seterror( sconn, 0, ldap_err2string( rc ) );
412 if ( dn.bv_len == 0 ) {
413 sasl_seterror( sconn, 0,
414 "No password is associated with the Root DSE" );
415 if ( dn.bv_val != NULL ) {
416 ch_free( dn.bv_val );
421 rc = backend_attribute( NULL, NULL, NULL, NULL, &dn,
422 slap_schema.si_ad_userPassword, &vals);
423 if ( rc != LDAP_SUCCESS ) {
424 ch_free( dn.bv_val );
425 sasl_seterror( sconn, 0, ldap_err2string( rc ) );
426 return SASL_NOVERIFY;
431 if ( vals != NULL ) {
432 for ( bv = vals; bv->bv_val != NULL; bv++ ) {
433 if ( !lutil_passwd( bv, &cred, NULL ) ) {
438 ber_bvarray_free( vals );
441 if ( rc != SASL_OK ) {
442 sasl_seterror( sconn, 0,
443 ldap_err2string( LDAP_INVALID_CREDENTIALS ) );
446 ch_free( dn.bv_val );
451 /* Convert a SASL authcid or authzid into a DN. Store the DN in an
452 * auxiliary property, so that we can refer to it in sasl_authorize
453 * without interfering with anything else. Also, the SASL username
454 * buffer is constrained to 256 characters, and our DNs could be
455 * much longer (totally arbitrary length)...
458 slap_sasl_canonicalize(
464 const char *user_realm,
469 Connection *conn = (Connection *)context;
470 struct propctx *props = sasl_auxprop_getctx( sconn );
474 const char *names[2];
479 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
480 "slap_sasl_canonicalize: conn %d %s=\"%s\"\n",
481 conn ? conn->c_connid : -1,
482 (flags & SASL_CU_AUTHID) ? "authcid" : "authzid",
483 in ? in : "<empty>" ));
485 Debug( LDAP_DEBUG_ARGS, "SASL Canonicalize [conn=%ld]: "
487 conn ? conn->c_connid : -1,
488 (flags & SASL_CU_AUTHID) ? "authcid" : "authzid",
489 in ? in : "<empty>" );
492 if ( inlen > out_max )
495 if ( flags == SASL_CU_AUTHZID ) {
496 /* If we got unqualified authzid's, they probably came from SASL
497 * itself just passing the authcid to us. Ignore it.
499 if (strncasecmp(in, "u:", 2) && strncasecmp(in, "dn:", 3)) {
500 AC_MEMCPY( out, in, inlen );
508 /* If using SASL-EXTERNAL, don't modify the ID in any way */
509 if ( conn->c_is_tls && conn->c_sasl_bind_mech.bv_len == ext_bv.bv_len
510 && ( strcasecmp( ext_bv.bv_val, conn->c_sasl_bind_mech.bv_val ) == 0 ) ) {
514 /* Else look for an embedded realm in the name */
515 realm = strchr( in, '@' );
516 if ( realm ) *realm++ = '\0';
518 rc = slap_sasl_getdn( conn, (char *)in, inlen, realm ? realm : (char *)user_realm, &dn,
519 (flags & SASL_CU_AUTHID) ? FLAG_GETDN_AUTHCID : FLAG_GETDN_AUTHZID );
522 if ( rc != LDAP_SUCCESS ) {
523 sasl_seterror( sconn, 0, ldap_err2string( rc ) );
527 AC_MEMCPY( out, in, inlen );
532 if ( flags & SASL_CU_AUTHID )
533 names[0] = slap_propnames[0];
535 names[0] = slap_propnames[1];
538 sasl_auxprop_request( sconn, names );
539 prop_set( props, names[0], (char *)&dn, sizeof( dn ) );
542 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
543 "slap_sasl_canonicalize: conn %d %s=\"%s\"\n",
544 conn ? conn->c_connid : -1,
545 (flags & SASL_CU_AUTHID) ? "authcDN" : "authzDN",
548 Debug( LDAP_DEBUG_ARGS, "SASL Canonicalize [conn=%ld]: "
550 conn ? conn->c_connid : -1,
551 (flags & SASL_CU_AUTHID) ? "authcDN" : "authzDN",
562 char *requested_user,
566 const char *def_realm,
568 struct propctx *props)
570 Connection *conn = (Connection *)context;
571 struct propval auxvals[3];
572 struct berval authcDN, authzDN;
576 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
577 "slap_sasl_authorize: conn %d authcid=\"%s\" authzid=\"%s\"\n",
578 conn ? conn->c_connid : -1, auth_identity, requested_user));
580 Debug( LDAP_DEBUG_ARGS, "SASL Authorize [conn=%ld]: "
581 "authcid=\"%s\" authzid=\"%s\"\n",
582 conn ? conn->c_connid : -1, auth_identity, requested_user );
585 prop_getnames( props, slap_propnames, auxvals );
587 /* Nothing to do if no authzID was given */
588 if ( !auxvals[1].name )
591 AC_MEMCPY( &authcDN, auxvals[0].values[0], sizeof(authcDN) );
592 AC_MEMCPY( &authzDN, auxvals[1].values[0], sizeof(authzDN) );
594 rc = slap_sasl_authorized( &authcDN, &authzDN );
595 if ( rc != LDAP_SUCCESS ) {
597 LDAP_LOG(( "sasl", LDAP_LEVEL_INFO,
598 "slap_sasl_authorize: conn %ld authorization disallowed (%d)\n",
599 (long)(conn ? conn->c_connid : -1), rc ));
601 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
602 " authorization disallowed (%d)\n",
603 (long) (conn ? conn->c_connid : -1), rc, 0 );
606 sasl_seterror( sconn, 0, "not authorized" );
611 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
612 "slap_sasl_authorize: conn %d authorization allowed\n",
613 (long)(conn ? conn->c_connid : -1 ) ));
615 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
616 " authorization allowed\n",
617 (long) (conn ? conn->c_connid : -1), 0, 0 );
630 struct berval authcDN, authzDN;
632 Connection *conn = context;
633 char *realm, *xrealm;
638 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
639 "slap_sasl_authorize: conn %d authcid=\"%s\" authzid=\"%s\"\n",
640 conn ? conn->c_connid : -1,
641 authcid ? authcid : "<empty>",
642 authzid ? authzid : "<empty>" ));
644 Debug( LDAP_DEBUG_ARGS, "SASL Authorize [conn=%ld]: "
645 "authcid=\"%s\" authzid=\"%s\"\n",
646 (long) (conn ? conn->c_connid : -1),
647 authcid ? authcid : "<empty>",
648 authzid ? authzid : "<empty>" );
651 /* Figure out how much data we have for the dn */
652 rc = sasl_getprop( conn->c_sasl_context, SASL_REALM, (void **)&realm );
653 if( rc != SASL_OK && rc != SASL_NOTDONE ) {
655 LDAP_LOG(( "sasl", LDAP_LEVEL_ERR,
656 "slap_sasl_authorize: getprop(REALM) failed.\n" ));
658 Debug(LDAP_DEBUG_TRACE,
659 "authorize: getprop(REALM) failed!\n", 0,0,0);
661 *errstr = "Could not extract realm";
665 /* Convert the identities to DN's. If no authzid was given, client will
666 be bound as the DN matching their username */
667 if ( conn->c_is_tls && conn->c_sasl_bind_mech.bv_len == ext_bv.bv_len
668 && ( strcasecmp( ext_bv.bv_val, conn->c_sasl_bind_mech.bv_val ) == 0 ) ) {
672 xrealm = strchr( authcid, '@' );
673 if ( xrealm ) *xrealm++ = '\0';
675 rc = slap_sasl_getdn( conn, (char *)authcid, 0, xrealm ? xrealm : realm, &authcDN, FLAG_GETDN_AUTHCID );
676 if ( xrealm ) xrealm[-1] = '@';
677 if( rc != LDAP_SUCCESS ) {
678 *errstr = ldap_err2string( rc );
681 if( ( authzid == NULL ) || !strcmp( authcid,authzid ) ) {
683 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
684 "slap_sasl_authorize: conn %d Using authcDN=%s\n",
685 conn ? conn->c_connid : -1, authcDN.bv_val ));
687 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
688 "Using authcDN=%s\n", (long) (conn ? conn->c_connid : -1), authcDN.bv_val,0 );
691 *user = authcDN.bv_val;
698 xrealm = strchr( authzid, '@' );
699 if ( xrealm ) *xrealm++ = '\0';
701 rc = slap_sasl_getdn( conn, (char *)authzid, 0, xrealm ? xrealm : realm, &authzDN, FLAG_GETDN_AUTHZID );
702 if ( xrealm ) xrealm[-1] = '@';
703 if( rc != LDAP_SUCCESS ) {
704 ch_free( authcDN.bv_val );
705 *errstr = ldap_err2string( rc );
709 rc = slap_sasl_authorized( &authcDN, &authzDN );
712 LDAP_LOG(( "sasl", LDAP_LEVEL_INFO,
713 "slap_sasl_authorize: conn %ld authorization disallowed (%d)\n",
714 (long)(conn ? conn->c_connid : -1), rc ));
716 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
717 " authorization disallowed (%d)\n",
718 (long) (conn ? conn->c_connid : -1), rc, 0 );
721 *errstr = "not authorized";
722 ch_free( authcDN.bv_val );
723 ch_free( authzDN.bv_val );
728 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
729 "slap_sasl_authorize: conn %d authorization allowed\n",
730 (long)(conn ? conn->c_connid : -1 ) ));
732 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
733 " authorization allowed\n",
734 (long) (conn ? conn->c_connid : -1), 0, 0 );
738 ch_free( authcDN.bv_val );
739 *user = authzDN.bv_val;
743 #endif /* SASL_VERSION_MAJOR >= 2 */
746 slap_sasl_err2ldap( int saslerr )
752 rc = LDAP_SASL_BIND_IN_PROGRESS;
761 rc = LDAP_AUTH_METHOD_NOT_SUPPORTED;
764 rc = LDAP_INVALID_CREDENTIALS;
767 rc = LDAP_INSUFFICIENT_ACCESS;
771 rc = LDAP_INAPPROPRIATE_AUTH;
783 int slap_sasl_init( void )
785 #ifdef HAVE_CYRUS_SASL
787 static sasl_callback_t server_callbacks[] = {
788 { SASL_CB_LOG, &slap_sasl_log, NULL },
789 { SASL_CB_LIST_END, NULL, NULL }
799 ldap_pvt_sasl_mutex_new,
800 ldap_pvt_sasl_mutex_lock,
801 ldap_pvt_sasl_mutex_unlock,
802 ldap_pvt_sasl_mutex_dispose );
804 #if SASL_VERSION_MAJOR >= 2
805 sasl_auxprop_add_plugin( "slapd", slap_auxprop_init );
807 /* should provide callbacks for logging */
808 /* server name should be configurable */
809 rc = sasl_server_init( server_callbacks, "slapd" );
811 if( rc != SASL_OK ) {
813 LDAP_LOG(( "sasl", LDAP_LEVEL_INFO,
814 "slap_sasl_init: init failed.\n" ));
816 Debug( LDAP_DEBUG_ANY, "sasl_server_init failed\n",
824 LDAP_LOG(( "sasl", LDAP_LEVEL_INFO,
825 "slap_sasl_init: initialized!\n"));
827 Debug( LDAP_DEBUG_TRACE, "slap_sasl_init: initialized!\n",
832 /* default security properties */
833 memset( &sasl_secprops, '\0', sizeof(sasl_secprops) );
834 sasl_secprops.max_ssf = INT_MAX;
835 sasl_secprops.maxbufsize = 65536;
836 sasl_secprops.security_flags = SASL_SEC_NOPLAINTEXT|SASL_SEC_NOANONYMOUS;
842 int slap_sasl_destroy( void )
844 #ifdef HAVE_CYRUS_SASL
853 int slap_sasl_open( Connection *conn )
855 int cb, sc = LDAP_SUCCESS;
856 #if SASL_VERSION_MAJOR >= 2
857 char *ipremoteport = NULL, *iplocalport = NULL;
860 #ifdef HAVE_CYRUS_SASL
861 sasl_conn_t *ctx = NULL;
862 sasl_callback_t *session_callbacks;
864 assert( conn->c_sasl_context == NULL );
865 assert( conn->c_sasl_extra == NULL );
867 conn->c_sasl_layers = 0;
870 #if SASL_VERSION_MAJOR >= 2
871 ch_calloc( 5, sizeof(sasl_callback_t));
873 ch_calloc( 3, sizeof(sasl_callback_t));
875 conn->c_sasl_extra = session_callbacks;
877 session_callbacks[cb=0].id = SASL_CB_LOG;
878 session_callbacks[cb].proc = &slap_sasl_log;
879 session_callbacks[cb++].context = conn;
881 session_callbacks[cb].id = SASL_CB_PROXY_POLICY;
882 session_callbacks[cb].proc = &slap_sasl_authorize;
883 session_callbacks[cb++].context = conn;
885 #if SASL_VERSION_MAJOR >= 2
886 session_callbacks[cb].id = SASL_CB_CANON_USER;
887 session_callbacks[cb].proc = &slap_sasl_canonicalize;
888 session_callbacks[cb++].context = conn;
890 /* XXXX: this should be conditional */
891 session_callbacks[cb].id = SASL_CB_SERVER_USERDB_CHECKPASS;
892 session_callbacks[cb].proc = &slap_sasl_checkpass;
893 session_callbacks[cb++].context = conn;
896 session_callbacks[cb].id = SASL_CB_LIST_END;
897 session_callbacks[cb].proc = NULL;
898 session_callbacks[cb++].context = NULL;
900 if( global_host == NULL ) {
901 global_host = ldap_pvt_get_fqdn( NULL );
904 /* create new SASL context */
905 #if SASL_VERSION_MAJOR >= 2
906 if ( conn->c_sock_name.bv_len != 0 &&
907 strncmp( conn->c_sock_name.bv_val, "IP=", 3 ) == 0) {
910 iplocalport = ch_strdup( conn->c_sock_name.bv_val + 3 );
911 /* Convert IPv6 addresses to address;port syntax. */
912 p = strrchr( iplocalport, ' ' );
913 /* Convert IPv4 addresses to address;port syntax. */
914 if ( p == NULL ) p = strchr( iplocalport, ':' );
919 if ( conn->c_peer_name.bv_len != 0 &&
920 strncmp( conn->c_peer_name.bv_val, "IP=", 3 ) == 0) {
923 ipremoteport = ch_strdup( conn->c_peer_name.bv_val + 3 );
924 /* Convert IPv6 addresses to address;port syntax. */
925 p = strrchr( ipremoteport, ' ' );
926 /* Convert IPv4 addresses to address;port syntax. */
927 if ( p == NULL ) p = strchr( ipremoteport, ':' );
932 sc = sasl_server_new( "ldap", global_host, global_realm,
933 iplocalport, ipremoteport, session_callbacks, 0, &ctx );
934 if ( iplocalport != NULL ) {
935 ch_free( iplocalport );
937 if ( ipremoteport != NULL ) {
938 ch_free( ipremoteport );
941 sc = sasl_server_new( "ldap", global_host, global_realm,
942 session_callbacks, SASL_SECURITY_LAYER, &ctx );
945 if( sc != SASL_OK ) {
947 LDAP_LOG(( "sasl", LDAP_LEVEL_ERR,
948 "slap_sasl_open: sasl_server_new failed: %d\n", sc ));
950 Debug( LDAP_DEBUG_ANY, "sasl_server_new failed: %d\n",
957 conn->c_sasl_context = ctx;
959 if( sc == SASL_OK ) {
960 sc = sasl_setprop( ctx,
961 SASL_SEC_PROPS, &sasl_secprops );
963 if( sc != SASL_OK ) {
965 LDAP_LOG(( "sasl", LDAP_LEVEL_ERR,
966 "slap_sasl_open: sasl_setprop failed: %d \n", sc ));
968 Debug( LDAP_DEBUG_ANY, "sasl_setprop failed: %d\n",
972 slap_sasl_close( conn );
977 sc = slap_sasl_err2ldap( sc );
982 int slap_sasl_external(
985 const char *auth_id )
987 #if SASL_VERSION_MAJOR >= 2
989 sasl_conn_t *ctx = conn->c_sasl_context;
992 return LDAP_UNAVAILABLE;
995 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL, &ssf );
997 if ( sc != SASL_OK ) {
1001 sc = sasl_setprop( ctx, SASL_AUTH_EXTERNAL, auth_id );
1003 if ( sc != SASL_OK ) {
1007 #elif defined(HAVE_CYRUS_SASL)
1009 sasl_conn_t *ctx = conn->c_sasl_context;
1010 sasl_external_properties_t extprops;
1012 if ( ctx == NULL ) {
1013 return LDAP_UNAVAILABLE;
1016 memset( &extprops, '\0', sizeof(extprops) );
1018 extprops.auth_id = (char *) auth_id;
1020 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL,
1021 (void *) &extprops );
1023 if ( sc != SASL_OK ) {
1028 return LDAP_SUCCESS;
1031 int slap_sasl_reset( Connection *conn )
1033 #ifdef HAVE_CYRUS_SASL
1034 sasl_conn_t *ctx = conn->c_sasl_context;
1039 /* must return "anonymous" */
1040 return LDAP_SUCCESS;
1043 char ** slap_sasl_mechs( Connection *conn )
1045 char **mechs = NULL;
1047 #ifdef HAVE_CYRUS_SASL
1048 sasl_conn_t *ctx = conn->c_sasl_context;
1052 SASL_CONST char *mechstr;
1054 sc = sasl_listmech( ctx,
1055 NULL, NULL, ",", NULL,
1056 &mechstr, NULL, NULL );
1058 if( sc != SASL_OK ) {
1060 LDAP_LOG(( "sasl", LDAP_LEVEL_ERR,
1061 "slap_sasl_mechs: sasl_listmech failed: %d\n", sc ));
1063 Debug( LDAP_DEBUG_ANY, "slap_sasl_listmech failed: %d\n",
1070 mechs = str2charray( mechstr, "," );
1072 #if SASL_VERSION_MAJOR < 2
1081 int slap_sasl_close( Connection *conn )
1083 #ifdef HAVE_CYRUS_SASL
1084 sasl_conn_t *ctx = conn->c_sasl_context;
1087 sasl_dispose( &ctx );
1090 conn->c_sasl_context = NULL;
1092 free( conn->c_sasl_extra );
1093 conn->c_sasl_extra = NULL;
1096 return LDAP_SUCCESS;
1104 struct berval *cred,
1110 #ifdef HAVE_CYRUS_SASL
1111 sasl_conn_t *ctx = conn->c_sasl_context;
1112 struct berval response;
1113 unsigned reslen = 0;
1114 const char *errstr = NULL;
1118 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
1119 "sasl_bind: conn %ld dn=\"%s\" mech=%s datalen=%ld\n",
1121 dn->bv_len ? dn->bv_val : "",
1122 conn->c_sasl_bind_in_progress ? "<continuing>" : conn->c_sasl_bind_mech.bv_val,
1123 cred ? cred->bv_len : 0 ));
1125 Debug(LDAP_DEBUG_ARGS,
1126 "==> sasl_bind: dn=\"%s\" mech=%s datalen=%ld\n",
1127 dn->bv_len ? dn->bv_val : "",
1128 conn->c_sasl_bind_in_progress ? "<continuing>":conn->c_sasl_bind_mech.bv_val,
1129 cred ? cred->bv_len : 0 );
1134 send_ldap_result( conn, op, LDAP_UNAVAILABLE,
1135 NULL, "SASL unavailable on this session", NULL, NULL );
1139 #if SASL_VERSION_MAJOR >= 2
1140 #define START( ctx, mech, cred, clen, resp, rlen, err ) \
1141 sasl_server_start( ctx, mech, cred, clen, resp, rlen )
1142 #define STEP( ctx, cred, clen, resp, rlen, err ) \
1143 sasl_server_step( ctx, cred, clen, resp, rlen )
1145 #define START( ctx, mech, cred, clen, resp, rlen, err ) \
1146 sasl_server_start( ctx, mech, cred, clen, resp, rlen, err )
1147 #define STEP( ctx, cred, clen, resp, rlen, err ) \
1148 sasl_server_step( ctx, cred, clen, resp, rlen, err )
1151 if ( !conn->c_sasl_bind_in_progress ) {
1153 conn->c_sasl_bind_mech.bv_val,
1154 cred->bv_len ? cred->bv_val : "",
1156 (SASL_CONST char **)&response.bv_val, &reslen, &errstr );
1160 cred->bv_val, cred->bv_len,
1161 (SASL_CONST char **)&response.bv_val, &reslen, &errstr );
1164 response.bv_len = reslen;
1166 if ( sc == SASL_OK ) {
1167 #if SASL_VERSION_MAJOR >= 2
1168 struct propctx *props = sasl_auxprop_getctx( ctx );
1169 struct propval vals[3];
1170 sasl_ssf_t *ssf = NULL;
1172 prop_getnames( props, slap_propnames, vals );
1174 AC_MEMCPY( edn, vals[0].values[0], sizeof(*edn) );
1175 if ( vals[1].name ) {
1176 ch_free( edn->bv_val );
1177 AC_MEMCPY( edn, vals[1].values[0], sizeof(*edn) );
1182 (void) sasl_getprop( ctx, SASL_SSF, (void *)&ssf );
1183 *ssfp = ssf ? *ssf : 0;
1186 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1187 conn->c_sasl_layers++;
1188 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1191 send_ldap_sasl( conn, op, rc,
1192 NULL, NULL, NULL, NULL,
1193 response.bv_len ? &response : NULL );
1195 char *username = NULL;
1197 sc = sasl_getprop( ctx,
1198 SASL_USERNAME, (SASL_CONST void **)&username );
1200 if ( sc != SASL_OK ) {
1202 LDAP_LOG(( "sasl", LDAP_LEVEL_ERR,
1203 "slap_sasl_bind: getprop(USERNAME) failed: %d\n", sc ));
1205 Debug(LDAP_DEBUG_TRACE,
1206 "slap_sasl_bind: getprop(USERNAME) failed!\n",
1211 send_ldap_result( conn, op, rc = slap_sasl_err2ldap( sc ),
1212 NULL, "no SASL username", NULL, NULL );
1215 sasl_ssf_t *ssf = NULL;
1218 ber_str2bv( username, 0, 1, edn );
1220 (void) sasl_getprop( ctx, SASL_SSF, (void *)&ssf );
1221 *ssfp = ssf ? *ssf : 0;
1224 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1225 conn->c_sasl_layers++;
1226 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1229 send_ldap_sasl( conn, op, rc,
1230 NULL, NULL, NULL, NULL,
1231 response.bv_len ? &response : NULL );
1235 } else if ( sc == SASL_CONTINUE ) {
1236 send_ldap_sasl( conn, op, rc = LDAP_SASL_BIND_IN_PROGRESS,
1237 NULL, NULL, NULL, NULL, &response );
1240 #if SASL_VERSION_MAJOR >= 2
1241 errstr = sasl_errdetail( ctx );
1243 send_ldap_result( conn, op, rc = slap_sasl_err2ldap( sc ),
1244 NULL, errstr, NULL, NULL );
1247 #if SASL_VERSION_MAJOR < 2
1248 if( response.bv_len ) {
1249 ch_free( response.bv_val );
1254 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
1255 "slap_sasl_bind: rc=%d\n", rc ));
1257 Debug(LDAP_DEBUG_TRACE, "<== slap_sasl_bind: rc=%d\n", rc, 0, 0);
1262 send_ldap_result( conn, op, rc = LDAP_UNAVAILABLE,
1263 NULL, "SASL not supported", NULL, NULL );
1269 char* slap_sasl_secprops( const char *in )
1271 #ifdef HAVE_CYRUS_SASL
1272 int rc = ldap_pvt_sasl_secprops( in, &sasl_secprops );
1274 return rc == LDAP_SUCCESS ? NULL : "Invalid security properties";
1276 return "SASL not supported";