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. Set the
180 * values directly into *dn; they will be normalized later. (and
181 * normalizing always makes a new copy.) An ID from a TLS certificate
182 * is already normalized, so copy it and skip normalization.
184 if( flags & FLAG_GETDN_AUTHCID ) {
186 if( conn->c_is_tls && conn->c_sasl_bind_mech.bv_len == ext_bv.bv_len
187 && ( strcasecmp( ext_bv.bv_val, conn->c_sasl_bind_mech.bv_val ) == 0 ) ) {
188 /* X.509 DN is already normalized */
191 ber_str2bv( id, len, 1, dn );
196 /* convert to u:<username> form */
203 if( !strncasecmp( id, "u:", sizeof("u:")-1 )) {
207 } else if ( !strncasecmp( id, "dn:", sizeof("dn:")-1) ) {
214 /* No other possibilities from here */
218 return( LDAP_INAPPROPRIATE_AUTH );
221 /* Username strings */
222 if( is_dn == SET_U ) {
224 len = dn->bv_len + sizeof("uid=")-1 + sizeof(",cn=auth")-1;
226 /* username may have embedded realm name */
227 if( realm = strchr( dn->bv_val, '@') ) {
229 len += sizeof(",cn=")-2;
230 } else if( user_realm && *user_realm ) {
231 len += strlen( user_realm ) + sizeof(",cn=")-1;
234 if( conn->c_sasl_bind_mech.bv_len ) {
235 len += conn->c_sasl_bind_mech.bv_len + sizeof(",cn=")-1;
238 /* Build the new dn */
240 dn->bv_val = ch_malloc( len+1 );
241 p = slap_strcopy( dn->bv_val, "uid=" );
242 p = slap_strcopy( p, c1 );
245 p = slap_strcopy( p, ",cn=" );
246 p = slap_strcopy( p, realm );
248 } else if( user_realm && *user_realm ) {
249 p = slap_strcopy( p, ",cn=" );
250 p = slap_strcopy( p, user_realm );
253 if( conn->c_sasl_bind_mech.bv_len ) {
254 p = slap_strcopy( p, ",cn=" );
255 p = slap_strcopy( p, conn->c_sasl_bind_mech.bv_val );
257 p = slap_strcopy( p, ",cn=auth" );
258 dn->bv_len = p - dn->bv_val;
261 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
262 "slap_sasl_getdn: u:id converted to %s.\n", dn->bv_val ));
264 Debug( LDAP_DEBUG_TRACE, "getdn: u:id converted to %s\n", dn->bv_val,0,0 );
268 /* All strings are in DN form now. Normalize if needed. */
270 rc = dnNormalize2( NULL, dn, &dn2 );
272 /* User DNs were constructed above and must be freed now */
273 if ( is_dn == SET_U )
274 ch_free( dn->bv_val );
276 if ( rc != LDAP_SUCCESS ) {
284 /* Run thru regexp */
285 slap_sasl2dn( conn, dn, &dn2 );
287 ch_free( dn->bv_val );
290 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
291 "slap_sasl_getdn: dn:id converted to %s.\n", dn->bv_val ));
293 Debug( LDAP_DEBUG_TRACE, "getdn: dn:id converted to %s\n",
298 return( LDAP_SUCCESS );
301 #if SASL_VERSION_MAJOR >= 2
302 static const char *slap_propnames[] = { "*authcDN", "*authzDN", NULL };
307 sasl_server_params_t *sparams,
314 const struct propval *list, *cur;
316 AttributeDescription *ad;
319 list = sparams->utils->prop_get( sparams->propctx );
321 /* Find our DN first */
322 for( cur = list; cur->name; cur++ ) {
323 if ( cur->name[0] == '*' ) {
324 if ( (flags & SASL_AUXPROP_AUTHZID) &&
325 !strcmp( cur->name, slap_propnames[1] ) ) {
326 if ( cur->values && cur->values[0] )
327 AC_MEMCPY( &dn, cur->values[0], sizeof( dn ) );
330 if ( !strcmp( cur->name, slap_propnames[0] ) ) {
331 AC_MEMCPY( &dn, cur->values[0], sizeof( dn ) );
332 if ( !(flags & SASL_AUXPROP_AUTHZID) )
338 /* Now fetch the rest */
339 for( cur = list; cur->name; cur++ ) {
340 const char *name = cur->name;
342 if ( name[0] == '*' ) {
343 if ( flags & SASL_AUXPROP_AUTHZID ) continue;
345 } else if ( !(flags & SASL_AUXPROP_AUTHZID ) )
349 if ( !(flags & SASL_AUXPROP_OVERRIDE) ) continue;
350 sparams->utils->prop_erase( sparams->propctx, cur->name );
353 rc = slap_str2ad( name, &ad, &text );
354 if ( rc != LDAP_SUCCESS ) {
356 LDAP_LOG(( "sasl", LDAP_LEVEL_DETAIL1,
357 "slap_auxprop: str2ad(%s): %s\n", name, text ));
359 Debug( LDAP_DEBUG_TRACE,
360 "slap_auxprop: str2ad(%s): %s\n", name, text, 0 );
362 rc = slap_str2undef_ad( name, &ad, &text );
363 if ( rc != LDAP_SUCCESS ) continue;
365 rc = backend_attribute( NULL,NULL,NULL,NULL, &dn, ad, &vals );
366 if ( rc != LDAP_SUCCESS ) continue;
367 for ( bv = vals; bv->bv_val; bv++ ) {
368 sparams->utils->prop_set( sparams->propctx, cur->name,
369 bv->bv_val, bv->bv_len );
371 ber_bvarray_free( vals );
375 static sasl_auxprop_plug_t slap_auxprop_plugin = {
378 NULL, /* glob_context */
379 NULL, /* auxprop_free */
387 const sasl_utils_t *utils,
390 sasl_auxprop_plug_t **plug,
391 const char *plugname)
393 if ( !out_version | !plug ) return SASL_BADPARAM;
395 if ( max_version < SASL_AUXPROP_PLUG_VERSION ) return SASL_BADVERS;
397 *out_version = SASL_AUXPROP_PLUG_VERSION;
398 *plug = &slap_auxprop_plugin;
406 const char *username,
409 struct propctx *propctx)
411 Connection *conn = (Connection *)context;
412 struct berval dn, cred;
416 cred.bv_val = (char *)pass;
417 cred.bv_len = passlen;
419 /* SASL will fallback to its own mechanisms if we don't
420 * find an answer here.
423 rc = slap_sasl_getdn( conn, (char *)username, 0, NULL, &dn,
424 FLAG_GETDN_AUTHCID );
425 if ( rc != LDAP_SUCCESS ) {
426 sasl_seterror( sconn, 0, ldap_err2string( rc ) );
430 if ( dn.bv_len == 0 ) {
431 sasl_seterror( sconn, 0,
432 "No password is associated with the Root DSE" );
433 if ( dn.bv_val != NULL ) {
434 ch_free( dn.bv_val );
439 rc = backend_attribute( NULL, NULL, NULL, NULL, &dn,
440 slap_schema.si_ad_userPassword, &vals);
441 if ( rc != LDAP_SUCCESS ) {
442 ch_free( dn.bv_val );
443 sasl_seterror( sconn, 0, ldap_err2string( rc ) );
444 return SASL_NOVERIFY;
449 if ( vals != NULL ) {
450 for ( bv = vals; bv->bv_val != NULL; bv++ ) {
451 if ( !lutil_passwd( bv, &cred, NULL ) ) {
456 ber_bvarray_free( vals );
459 if ( rc != SASL_OK ) {
460 sasl_seterror( sconn, 0,
461 ldap_err2string( LDAP_INVALID_CREDENTIALS ) );
464 ch_free( dn.bv_val );
469 /* Convert a SASL authcid or authzid into a DN. Store the DN in an
470 * auxiliary property, so that we can refer to it in sasl_authorize
471 * without interfering with anything else. Also, the SASL username
472 * buffer is constrained to 256 characters, and our DNs could be
473 * much longer (totally arbitrary length)...
476 slap_sasl_canonicalize(
482 const char *user_realm,
487 Connection *conn = (Connection *)context;
488 struct propctx *props = sasl_auxprop_getctx( sconn );
491 const char *names[2];
496 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
497 "slap_sasl_canonicalize: conn %d %s=\"%s\"\n",
498 conn ? conn->c_connid : -1,
499 (flags & SASL_CU_AUTHID) ? "authcid" : "authzid",
500 in ? in : "<empty>" ));
502 Debug( LDAP_DEBUG_ARGS, "SASL Canonicalize [conn=%ld]: "
504 conn ? conn->c_connid : -1,
505 (flags & SASL_CU_AUTHID) ? "authcid" : "authzid",
506 in ? in : "<empty>" );
509 if ( inlen > out_max )
512 if ( flags == SASL_CU_AUTHZID ) {
513 /* If we got unqualified authzid's, they probably came from SASL
514 * itself just passing the authcid to us. Ignore it.
516 if (strncasecmp(in, "u:", 2) && strncasecmp(in, "dn:", 3)) {
517 AC_MEMCPY( out, in, inlen );
525 rc = slap_sasl_getdn( conn, (char *)in, inlen, (char *)user_realm, &dn,
526 (flags & SASL_CU_AUTHID) ? FLAG_GETDN_AUTHCID : FLAG_GETDN_AUTHZID );
527 if ( rc != LDAP_SUCCESS ) {
528 sasl_seterror( sconn, 0, ldap_err2string( rc ) );
532 AC_MEMCPY( out, in, inlen );
537 if ( flags & SASL_CU_AUTHID )
538 names[0] = slap_propnames[0];
540 names[0] = slap_propnames[1];
543 sasl_auxprop_request( sconn, names );
544 prop_set( props, names[0], (char *)&dn, sizeof( dn ) );
547 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
548 "slap_sasl_canonicalize: conn %d %s=\"%s\"\n",
549 conn ? conn->c_connid : -1,
550 (flags & SASL_CU_AUTHID) ? "authcDN" : "authzDN",
553 Debug( LDAP_DEBUG_ARGS, "SASL Canonicalize [conn=%ld]: "
555 conn ? conn->c_connid : -1,
556 (flags & SASL_CU_AUTHID) ? "authcDN" : "authzDN",
567 char *requested_user,
571 const char *def_realm,
573 struct propctx *props)
575 Connection *conn = (Connection *)context;
576 struct propval auxvals[3];
577 struct berval authcDN, authzDN;
581 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
582 "slap_sasl_authorize: conn %d authcid=\"%s\" authzid=\"%s\"\n",
583 conn ? conn->c_connid : -1, auth_identity, requested_user));
585 Debug( LDAP_DEBUG_ARGS, "SASL Authorize [conn=%ld]: "
586 "authcid=\"%s\" authzid=\"%s\"\n",
587 conn ? conn->c_connid : -1, auth_identity, requested_user );
590 prop_getnames( props, slap_propnames, auxvals );
592 /* Nothing to do if no authzID was given */
593 if ( !auxvals[1].name )
596 AC_MEMCPY( &authcDN, auxvals[0].values[0], sizeof(authcDN) );
597 AC_MEMCPY( &authzDN, auxvals[1].values[0], sizeof(authzDN) );
599 rc = slap_sasl_authorized( &authcDN, &authzDN );
600 if ( rc != LDAP_SUCCESS ) {
602 LDAP_LOG(( "sasl", LDAP_LEVEL_INFO,
603 "slap_sasl_authorize: conn %ld authorization disallowed (%d)\n",
604 (long)(conn ? conn->c_connid : -1), rc ));
606 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
607 " authorization disallowed (%d)\n",
608 (long) (conn ? conn->c_connid : -1), rc, 0 );
611 sasl_seterror( sconn, 0, "not authorized" );
616 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
617 "slap_sasl_authorize: conn %d authorization allowed\n",
618 (long)(conn ? conn->c_connid : -1 ) ));
620 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
621 " authorization allowed\n",
622 (long) (conn ? conn->c_connid : -1), 0, 0 );
635 struct berval authcDN, authzDN;
637 Connection *conn = context;
643 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
644 "slap_sasl_authorize: conn %d authcid=\"%s\" authzid=\"%s\"\n",
645 conn ? conn->c_connid : -1,
646 authcid ? authcid : "<empty>",
647 authzid ? authzid : "<empty>" ));
649 Debug( LDAP_DEBUG_ARGS, "SASL Authorize [conn=%ld]: "
650 "authcid=\"%s\" authzid=\"%s\"\n",
651 (long) (conn ? conn->c_connid : -1),
652 authcid ? authcid : "<empty>",
653 authzid ? authzid : "<empty>" );
656 /* Figure out how much data we have for the dn */
657 rc = sasl_getprop( conn->c_sasl_context, SASL_REALM, (void **)&realm );
658 if( rc != SASL_OK && rc != SASL_NOTDONE ) {
660 LDAP_LOG(( "sasl", LDAP_LEVEL_ERR,
661 "slap_sasl_authorize: getprop(REALM) failed.\n" ));
663 Debug(LDAP_DEBUG_TRACE,
664 "authorize: getprop(REALM) failed!\n", 0,0,0);
666 *errstr = "Could not extract realm";
670 /* Convert the identities to DN's. If no authzid was given, client will
671 be bound as the DN matching their username */
672 rc = slap_sasl_getdn( conn, (char *)authcid, 0, realm, &authcDN, FLAG_GETDN_AUTHCID );
673 if( rc != LDAP_SUCCESS ) {
674 *errstr = ldap_err2string( rc );
677 if( ( authzid == NULL ) || !strcmp( authcid,authzid ) ) {
679 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
680 "slap_sasl_authorize: conn %d Using authcDN=%s\n",
681 conn ? conn->c_connid : -1, authcDN.bv_val ));
683 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
684 "Using authcDN=%s\n", (long) (conn ? conn->c_connid : -1), authcDN.bv_val,0 );
687 *user = authcDN.bv_val;
691 rc = slap_sasl_getdn( conn, (char *)authzid, 0, realm, &authzDN, FLAG_GETDN_AUTHZID );
692 if( rc != LDAP_SUCCESS ) {
693 ch_free( authcDN.bv_val );
694 *errstr = ldap_err2string( rc );
698 rc = slap_sasl_authorized( &authcDN, &authzDN );
701 LDAP_LOG(( "sasl", LDAP_LEVEL_INFO,
702 "slap_sasl_authorize: conn %ld authorization disallowed (%d)\n",
703 (long)(conn ? conn->c_connid : -1), rc ));
705 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
706 " authorization disallowed (%d)\n",
707 (long) (conn ? conn->c_connid : -1), rc, 0 );
710 *errstr = "not authorized";
711 ch_free( authcDN.bv_val );
712 ch_free( authzDN.bv_val );
717 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
718 "slap_sasl_authorize: conn %d authorization allowed\n",
719 (long)(conn ? conn->c_connid : -1 ) ));
721 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
722 " authorization allowed\n",
723 (long) (conn ? conn->c_connid : -1), 0, 0 );
727 ch_free( authcDN.bv_val );
728 *user = authzDN.bv_val;
732 #endif /* SASL_VERSION_MAJOR >= 2 */
735 slap_sasl_err2ldap( int saslerr )
741 rc = LDAP_SASL_BIND_IN_PROGRESS;
750 rc = LDAP_AUTH_METHOD_NOT_SUPPORTED;
753 rc = LDAP_INVALID_CREDENTIALS;
756 rc = LDAP_INSUFFICIENT_ACCESS;
760 rc = LDAP_INAPPROPRIATE_AUTH;
772 int slap_sasl_init( void )
774 #ifdef HAVE_CYRUS_SASL
776 static sasl_callback_t server_callbacks[] = {
777 { SASL_CB_LOG, &slap_sasl_log, NULL },
778 { SASL_CB_LIST_END, NULL, NULL }
788 ldap_pvt_sasl_mutex_new,
789 ldap_pvt_sasl_mutex_lock,
790 ldap_pvt_sasl_mutex_unlock,
791 ldap_pvt_sasl_mutex_dispose );
793 #if SASL_VERSION_MAJOR >= 2
794 sasl_auxprop_add_plugin( "slapd", slap_auxprop_init );
796 /* should provide callbacks for logging */
797 /* server name should be configurable */
798 rc = sasl_server_init( server_callbacks, "slapd" );
800 if( rc != SASL_OK ) {
802 LDAP_LOG(( "sasl", LDAP_LEVEL_INFO,
803 "slap_sasl_init: init failed.\n" ));
805 Debug( LDAP_DEBUG_ANY, "sasl_server_init failed\n",
813 LDAP_LOG(( "sasl", LDAP_LEVEL_INFO,
814 "slap_sasl_init: initialized!\n"));
816 Debug( LDAP_DEBUG_TRACE, "slap_sasl_init: initialized!\n",
821 /* default security properties */
822 memset( &sasl_secprops, '\0', sizeof(sasl_secprops) );
823 sasl_secprops.max_ssf = INT_MAX;
824 sasl_secprops.maxbufsize = 65536;
825 sasl_secprops.security_flags = SASL_SEC_NOPLAINTEXT|SASL_SEC_NOANONYMOUS;
831 int slap_sasl_destroy( void )
833 #ifdef HAVE_CYRUS_SASL
842 int slap_sasl_open( Connection *conn )
844 int cb, sc = LDAP_SUCCESS;
845 #if SASL_VERSION_MAJOR >= 2
846 char *ipremoteport = NULL, *iplocalport = NULL;
849 #ifdef HAVE_CYRUS_SASL
850 sasl_conn_t *ctx = NULL;
851 sasl_callback_t *session_callbacks;
853 assert( conn->c_sasl_context == NULL );
854 assert( conn->c_sasl_extra == NULL );
856 conn->c_sasl_layers = 0;
859 #if SASL_VERSION_MAJOR >= 2
860 ch_calloc( 5, sizeof(sasl_callback_t));
862 ch_calloc( 3, sizeof(sasl_callback_t));
864 conn->c_sasl_extra = session_callbacks;
866 session_callbacks[cb=0].id = SASL_CB_LOG;
867 session_callbacks[cb].proc = &slap_sasl_log;
868 session_callbacks[cb++].context = conn;
870 session_callbacks[cb].id = SASL_CB_PROXY_POLICY;
871 session_callbacks[cb].proc = &slap_sasl_authorize;
872 session_callbacks[cb++].context = conn;
874 #if SASL_VERSION_MAJOR >= 2
875 session_callbacks[cb].id = SASL_CB_CANON_USER;
876 session_callbacks[cb].proc = &slap_sasl_canonicalize;
877 session_callbacks[cb++].context = conn;
879 /* XXXX: this should be conditional */
880 session_callbacks[cb].id = SASL_CB_SERVER_USERDB_CHECKPASS;
881 session_callbacks[cb].proc = &slap_sasl_checkpass;
882 session_callbacks[cb++].context = conn;
885 session_callbacks[cb].id = SASL_CB_LIST_END;
886 session_callbacks[cb].proc = NULL;
887 session_callbacks[cb++].context = NULL;
889 if( global_host == NULL ) {
890 global_host = ldap_pvt_get_fqdn( NULL );
893 /* create new SASL context */
894 #if SASL_VERSION_MAJOR >= 2
895 if ( conn->c_sock_name.bv_len != 0 &&
896 strncmp( conn->c_sock_name.bv_val, "IP=", 3 ) == 0) {
899 iplocalport = ch_strdup( conn->c_sock_name.bv_val + 3 );
900 /* Convert IPv6 addresses to address;port syntax. */
901 p = strrchr( iplocalport, ' ' );
902 /* Convert IPv4 addresses to address;port syntax. */
903 if ( p == NULL ) p = strchr( iplocalport, ':' );
908 if ( conn->c_peer_name.bv_len != 0 &&
909 strncmp( conn->c_peer_name.bv_val, "IP=", 3 ) == 0) {
912 ipremoteport = ch_strdup( conn->c_peer_name.bv_val + 3 );
913 /* Convert IPv6 addresses to address;port syntax. */
914 p = strrchr( ipremoteport, ' ' );
915 /* Convert IPv4 addresses to address;port syntax. */
916 if ( p == NULL ) p = strchr( ipremoteport, ':' );
921 sc = sasl_server_new( "ldap", global_host, global_realm,
922 iplocalport, ipremoteport, session_callbacks, 0, &ctx );
923 if ( iplocalport != NULL ) {
924 ch_free( iplocalport );
926 if ( ipremoteport != NULL ) {
927 ch_free( ipremoteport );
930 sc = sasl_server_new( "ldap", global_host, global_realm,
931 session_callbacks, SASL_SECURITY_LAYER, &ctx );
934 if( sc != SASL_OK ) {
936 LDAP_LOG(( "sasl", LDAP_LEVEL_ERR,
937 "slap_sasl_open: sasl_server_new failed: %d\n", sc ));
939 Debug( LDAP_DEBUG_ANY, "sasl_server_new failed: %d\n",
946 conn->c_sasl_context = ctx;
948 if( sc == SASL_OK ) {
949 sc = sasl_setprop( ctx,
950 SASL_SEC_PROPS, &sasl_secprops );
952 if( sc != SASL_OK ) {
954 LDAP_LOG(( "sasl", LDAP_LEVEL_ERR,
955 "slap_sasl_open: sasl_setprop failed: %d \n", sc ));
957 Debug( LDAP_DEBUG_ANY, "sasl_setprop failed: %d\n",
961 slap_sasl_close( conn );
966 sc = slap_sasl_err2ldap( sc );
971 int slap_sasl_external(
974 const char *auth_id )
976 #if SASL_VERSION_MAJOR >= 2
978 sasl_conn_t *ctx = conn->c_sasl_context;
981 return LDAP_UNAVAILABLE;
984 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL, &ssf );
986 if ( sc != SASL_OK ) {
990 sc = sasl_setprop( ctx, SASL_AUTH_EXTERNAL, auth_id );
992 if ( sc != SASL_OK ) {
996 #elif defined(HAVE_CYRUS_SASL)
998 sasl_conn_t *ctx = conn->c_sasl_context;
999 sasl_external_properties_t extprops;
1001 if ( ctx == NULL ) {
1002 return LDAP_UNAVAILABLE;
1005 memset( &extprops, '\0', sizeof(extprops) );
1007 extprops.auth_id = (char *) auth_id;
1009 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL,
1010 (void *) &extprops );
1012 if ( sc != SASL_OK ) {
1017 return LDAP_SUCCESS;
1020 int slap_sasl_reset( Connection *conn )
1022 #ifdef HAVE_CYRUS_SASL
1023 sasl_conn_t *ctx = conn->c_sasl_context;
1028 /* must return "anonymous" */
1029 return LDAP_SUCCESS;
1032 char ** slap_sasl_mechs( Connection *conn )
1034 char **mechs = NULL;
1036 #ifdef HAVE_CYRUS_SASL
1037 sasl_conn_t *ctx = conn->c_sasl_context;
1041 SASL_CONST char *mechstr;
1043 sc = sasl_listmech( ctx,
1044 NULL, NULL, ",", NULL,
1045 &mechstr, NULL, NULL );
1047 if( sc != SASL_OK ) {
1049 LDAP_LOG(( "sasl", LDAP_LEVEL_ERR,
1050 "slap_sasl_mechs: sasl_listmech failed: %d\n", sc ));
1052 Debug( LDAP_DEBUG_ANY, "slap_sasl_listmech failed: %d\n",
1059 mechs = str2charray( mechstr, "," );
1061 #if SASL_VERSION_MAJOR < 2
1070 int slap_sasl_close( Connection *conn )
1072 #ifdef HAVE_CYRUS_SASL
1073 sasl_conn_t *ctx = conn->c_sasl_context;
1076 sasl_dispose( &ctx );
1079 conn->c_sasl_context = NULL;
1081 free( conn->c_sasl_extra );
1082 conn->c_sasl_extra = NULL;
1085 return LDAP_SUCCESS;
1093 struct berval *cred,
1099 #ifdef HAVE_CYRUS_SASL
1100 sasl_conn_t *ctx = conn->c_sasl_context;
1101 struct berval response;
1102 unsigned reslen = 0;
1103 const char *errstr = NULL;
1107 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
1108 "sasl_bind: conn %ld dn=\"%s\" mech=%s datalen=%ld\n",
1110 dn->bv_len ? dn->bv_val : "",
1111 conn->c_sasl_bind_in_progress ? "<continuing>" : conn->c_sasl_bind_mech.bv_val,
1112 cred ? cred->bv_len : 0 ));
1114 Debug(LDAP_DEBUG_ARGS,
1115 "==> sasl_bind: dn=\"%s\" mech=%s datalen=%ld\n",
1116 dn->bv_len ? dn->bv_val : "",
1117 conn->c_sasl_bind_in_progress ? "<continuing>":conn->c_sasl_bind_mech.bv_val,
1118 cred ? cred->bv_len : 0 );
1123 send_ldap_result( conn, op, LDAP_UNAVAILABLE,
1124 NULL, "SASL unavailable on this session", NULL, NULL );
1128 #if SASL_VERSION_MAJOR >= 2
1129 #define START( ctx, mech, cred, clen, resp, rlen, err ) \
1130 sasl_server_start( ctx, mech, cred, clen, resp, rlen )
1131 #define STEP( ctx, cred, clen, resp, rlen, err ) \
1132 sasl_server_step( ctx, cred, clen, resp, rlen )
1134 #define START( ctx, mech, cred, clen, resp, rlen, err ) \
1135 sasl_server_start( ctx, mech, cred, clen, resp, rlen, err )
1136 #define STEP( ctx, cred, clen, resp, rlen, err ) \
1137 sasl_server_step( ctx, cred, clen, resp, rlen, err )
1140 if ( !conn->c_sasl_bind_in_progress ) {
1142 conn->c_sasl_bind_mech.bv_val,
1143 cred->bv_len ? cred->bv_val : "",
1145 (SASL_CONST char **)&response.bv_val, &reslen, &errstr );
1149 cred->bv_val, cred->bv_len,
1150 (SASL_CONST char **)&response.bv_val, &reslen, &errstr );
1153 response.bv_len = reslen;
1155 if ( sc == SASL_OK ) {
1156 #if SASL_VERSION_MAJOR >= 2
1157 struct propctx *props = sasl_auxprop_getctx( ctx );
1158 struct propval vals[3];
1159 sasl_ssf_t *ssf = NULL;
1161 prop_getnames( props, slap_propnames, vals );
1163 AC_MEMCPY( edn, vals[0].values[0], sizeof(*edn) );
1164 if ( vals[1].name ) {
1165 ch_free( edn->bv_val );
1166 AC_MEMCPY( edn, vals[1].values[0], sizeof(*edn) );
1171 (void) sasl_getprop( ctx, SASL_SSF, (void *)&ssf );
1172 *ssfp = ssf ? *ssf : 0;
1175 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1176 conn->c_sasl_layers++;
1177 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1180 send_ldap_sasl( conn, op, rc,
1181 NULL, NULL, NULL, NULL,
1182 response.bv_len ? &response : NULL );
1184 char *username = NULL;
1186 sc = sasl_getprop( ctx,
1187 SASL_USERNAME, (SASL_CONST void **)&username );
1189 if ( sc != SASL_OK ) {
1191 LDAP_LOG(( "sasl", LDAP_LEVEL_ERR,
1192 "slap_sasl_bind: getprop(USERNAME) failed: %d\n", sc ));
1194 Debug(LDAP_DEBUG_TRACE,
1195 "slap_sasl_bind: getprop(USERNAME) failed!\n",
1200 send_ldap_result( conn, op, rc = slap_sasl_err2ldap( sc ),
1201 NULL, "no SASL username", NULL, NULL );
1204 sasl_ssf_t *ssf = NULL;
1207 ber_str2bv( username, 0, 1, edn );
1209 (void) sasl_getprop( ctx, SASL_SSF, (void *)&ssf );
1210 *ssfp = ssf ? *ssf : 0;
1213 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1214 conn->c_sasl_layers++;
1215 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1218 send_ldap_sasl( conn, op, rc,
1219 NULL, NULL, NULL, NULL,
1220 response.bv_len ? &response : NULL );
1224 } else if ( sc == SASL_CONTINUE ) {
1225 send_ldap_sasl( conn, op, rc = LDAP_SASL_BIND_IN_PROGRESS,
1226 NULL, NULL, NULL, NULL, &response );
1229 #if SASL_VERSION_MAJOR >= 2
1230 errstr = sasl_errdetail( ctx );
1232 send_ldap_result( conn, op, rc = slap_sasl_err2ldap( sc ),
1233 NULL, errstr, NULL, NULL );
1236 #if SASL_VERSION_MAJOR < 2
1237 if( response.bv_len ) {
1238 ch_free( response.bv_val );
1243 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
1244 "slap_sasl_bind: rc=%d\n", rc ));
1246 Debug(LDAP_DEBUG_TRACE, "<== slap_sasl_bind: rc=%d\n", rc, 0, 0);
1251 send_ldap_result( conn, op, rc = LDAP_UNAVAILABLE,
1252 NULL, "SASL not supported", NULL, NULL );
1258 char* slap_sasl_secprops( const char *in )
1260 #ifdef HAVE_CYRUS_SASL
1261 int rc = ldap_pvt_sasl_secprops( in, &sasl_secprops );
1263 return rc == LDAP_SUCCESS ? NULL : "Invalid security properties";
1265 return "SASL not supported";