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 ) {
182 if( conn->c_is_tls && conn->c_sasl_bind_mech.bv_len == ext_bv.bv_len
183 && ( strcasecmp( ext_bv.bv_val, conn->c_sasl_bind_mech.bv_val ) == 0 ) ) {
184 /* X.509 DN is already normalized */
191 /* convert to u:<username> form */
194 ber_str2bv( id, len, 1, dn );
197 if( !strncasecmp( id, "u:", sizeof("u:")-1 )) {
199 ber_str2bv( id+2, len-2, 1, dn );
200 } else if ( !strncasecmp( id, "dn:", sizeof("dn:")-1) ) {
202 ber_str2bv( id+3, len-3, 1, dn );
206 /* An authzID must be properly prefixed */
207 if( (flags & FLAG_GETDN_AUTHZID) && !is_dn ) {
211 return( LDAP_INAPPROPRIATE_AUTH );
214 /* Username strings */
215 if( is_dn == SET_U ) {
217 len = dn->bv_len + sizeof("uid=")-1 + sizeof(",cn=auth")-1;
219 /* username may have embedded realm name */
220 if( realm = strchr( dn->bv_val, '@') ) {
222 len += sizeof(",cn=")-2;
223 } else if( user_realm && *user_realm ) {
224 len += strlen( user_realm ) + sizeof(",cn=")-1;
227 if( conn->c_sasl_bind_mech.bv_len ) {
228 len += conn->c_sasl_bind_mech.bv_len + sizeof(",cn=")-1;
231 /* Build the new dn */
233 dn->bv_val = ch_malloc( len+1 );
234 p = slap_strcopy( dn->bv_val, "uid=" );
235 p = slap_strcopy( p, c1 );
238 p = slap_strcopy( p, ",cn=" );
239 p = slap_strcopy( p, realm );
240 } else if( user_realm && *user_realm ) {
241 p = slap_strcopy( p, ",cn=" );
242 p = slap_strcopy( p, user_realm );
246 if( conn->c_sasl_bind_mech.bv_len ) {
247 p = slap_strcopy( p, ",cn=" );
248 p = slap_strcopy( p, conn->c_sasl_bind_mech.bv_val );
250 p = slap_strcopy( p, ",cn=auth" );
251 dn->bv_len = p - dn->bv_val;
255 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
256 "slap_sasl_getdn: u:id converted to %s.\n", dn->bv_val ));
258 Debug( LDAP_DEBUG_TRACE, "getdn: u:id converted to %s\n", dn->bv_val,0,0 );
262 /* DN strings that are a cn=auth identity to run through regexp */
263 if( is_dn == SET_DN )
265 slap_sasl2dn( conn, dn, &dn2 );
267 ch_free( dn->bv_val );
269 do_norm = 0; /* slap_sasl2dn normalizes */
271 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
272 "slap_sasl_getdn: dn:id converted to %s.\n", dn->bv_val ));
274 Debug( LDAP_DEBUG_TRACE, "getdn: dn:id converted to %s\n",
281 rc = dnNormalize2( NULL, dn, &dn2 );
283 if ( rc != LDAP_SUCCESS ) {
291 return( LDAP_SUCCESS );
294 #if SASL_VERSION_MAJOR >= 2
295 static const char *slap_propnames[] = { "*authcDN", "*authzDN", NULL };
300 sasl_server_params_t *sparams,
307 const struct propval *list, *cur;
309 AttributeDescription *ad;
312 list = sparams->utils->prop_get( sparams->propctx );
314 /* Find our DN first */
315 for( cur = list; cur->name; cur++ ) {
316 if ( cur->name[0] == '*' ) {
317 if ( (flags & SASL_AUXPROP_AUTHZID) &&
318 !strcmp( cur->name, slap_propnames[1] ) ) {
319 if ( cur->values && cur->values[0] )
320 AC_MEMCPY( &dn, cur->values[0], sizeof( dn ) );
323 if ( !strcmp( cur->name, slap_propnames[0] ) ) {
324 AC_MEMCPY( &dn, cur->values[0], sizeof( dn ) );
325 if ( !(flags & SASL_AUXPROP_AUTHZID) )
331 /* Now fetch the rest */
332 for( cur = list; cur->name; cur++ ) {
333 const char *name = cur->name;
335 if ( name[0] == '*' ) {
336 if ( flags & SASL_AUXPROP_AUTHZID ) continue;
338 } else if ( !(flags & SASL_AUXPROP_AUTHZID ) )
342 if ( !(flags & SASL_AUXPROP_OVERRIDE) ) continue;
343 sparams->utils->prop_erase( sparams->propctx, cur->name );
346 rc = slap_str2ad( name, &ad, &text );
347 if ( rc != LDAP_SUCCESS ) {
349 LDAP_LOG(( "sasl", LDAP_LEVEL_DETAIL1,
350 "slap_auxprop: str2ad(%s): %s\n", name, text ));
352 Debug( LDAP_DEBUG_TRACE,
353 "slap_auxprop: str2ad(%s): %s\n", name, text, 0 );
355 rc = slap_str2undef_ad( name, &ad, &text );
356 if ( rc != LDAP_SUCCESS ) continue;
358 rc = backend_attribute( NULL,NULL,NULL,NULL, &dn, ad, &vals );
359 if ( rc != LDAP_SUCCESS ) continue;
360 for ( bv = vals; bv->bv_val; bv++ ) {
361 sparams->utils->prop_set( sparams->propctx, cur->name,
362 bv->bv_val, bv->bv_len );
364 ber_bvarray_free( vals );
368 static sasl_auxprop_plug_t slap_auxprop_plugin = {
371 NULL, /* glob_context */
372 NULL, /* auxprop_free */
380 const sasl_utils_t *utils,
383 sasl_auxprop_plug_t **plug,
384 const char *plugname)
386 if ( !out_version | !plug ) return SASL_BADPARAM;
388 if ( max_version < SASL_AUXPROP_PLUG_VERSION ) return SASL_BADVERS;
390 *out_version = SASL_AUXPROP_PLUG_VERSION;
391 *plug = &slap_auxprop_plugin;
399 const char *username,
402 struct propctx *propctx)
404 Connection *conn = (Connection *)context;
405 struct berval dn, cred;
409 cred.bv_val = (char *)pass;
410 cred.bv_len = passlen;
412 /* SASL will fallback to its own mechanisms if we don't
413 * find an answer here.
416 rc = slap_sasl_getdn( conn, (char *)username, 0, NULL, &dn,
417 FLAG_GETDN_AUTHCID );
418 if ( rc != LDAP_SUCCESS ) {
419 sasl_seterror( sconn, 0, ldap_err2string( rc ) );
423 if ( dn.bv_len == 0 ) {
424 sasl_seterror( sconn, 0,
425 "No password is associated with the Root DSE" );
426 if ( dn.bv_val != NULL ) {
427 ch_free( dn.bv_val );
432 rc = backend_attribute( NULL, NULL, NULL, NULL, &dn,
433 slap_schema.si_ad_userPassword, &vals);
434 if ( rc != LDAP_SUCCESS ) {
435 ch_free( dn.bv_val );
436 sasl_seterror( sconn, 0, ldap_err2string( rc ) );
437 return SASL_NOVERIFY;
442 if ( vals != NULL ) {
443 for ( bv = vals; bv->bv_val != NULL; bv++ ) {
444 if ( !lutil_passwd( bv, &cred, NULL ) ) {
449 ber_bvarray_free( vals );
452 if ( rc != SASL_OK ) {
453 sasl_seterror( sconn, 0,
454 ldap_err2string( LDAP_INVALID_CREDENTIALS ) );
457 ch_free( dn.bv_val );
462 /* Convert a SASL authcid or authzid into a DN. Store the DN in an
463 * auxiliary property, so that we can refer to it in sasl_authorize
464 * without interfering with anything else. Also, the SASL username
465 * buffer is constrained to 256 characters, and our DNs could be
466 * much longer (totally arbitrary length)...
469 slap_sasl_canonicalize(
475 const char *user_realm,
480 Connection *conn = (Connection *)context;
481 struct propctx *props = sasl_auxprop_getctx( sconn );
484 const char *names[2];
489 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
490 "slap_sasl_canonicalize: conn %d %s=\"%s\"\n",
491 conn ? conn->c_connid : -1,
492 (flags & SASL_CU_AUTHID) ? "authcid" : "authzid",
493 in ? in : "<empty>" ));
495 Debug( LDAP_DEBUG_ARGS, "SASL Canonicalize [conn=%ld]: "
497 conn ? conn->c_connid : -1,
498 (flags & SASL_CU_AUTHID) ? "authcid" : "authzid",
499 in ? in : "<empty>" );
502 if ( inlen > out_max )
505 if ( flags == SASL_CU_AUTHZID ) {
506 /* If we got unqualified authzid's, they probably came from SASL
507 * itself just passing the authcid to us. Ignore it.
509 if (strncasecmp(in, "u:", 2) && strncasecmp(in, "dn:", 3)) {
510 AC_MEMCPY( out, in, inlen );
518 rc = slap_sasl_getdn( conn, (char *)in, inlen, (char *)user_realm, &dn,
519 (flags & SASL_CU_AUTHID) ? FLAG_GETDN_AUTHCID : FLAG_GETDN_AUTHZID );
520 if ( rc != LDAP_SUCCESS ) {
521 sasl_seterror( sconn, 0, ldap_err2string( rc ) );
525 AC_MEMCPY( out, in, inlen );
530 if ( flags & SASL_CU_AUTHID )
531 names[0] = slap_propnames[0];
533 names[0] = slap_propnames[1];
536 sasl_auxprop_request( sconn, names );
537 prop_set( props, names[0], (char *)&dn, sizeof( dn ) );
540 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
541 "slap_sasl_canonicalize: conn %d %s=\"%s\"\n",
542 conn ? conn->c_connid : -1,
543 (flags & SASL_CU_AUTHID) ? "authcDN" : "authzDN",
546 Debug( LDAP_DEBUG_ARGS, "SASL Canonicalize [conn=%ld]: "
548 conn ? conn->c_connid : -1,
549 (flags & SASL_CU_AUTHID) ? "authcDN" : "authzDN",
560 char *requested_user,
564 const char *def_realm,
566 struct propctx *props)
568 Connection *conn = (Connection *)context;
569 struct propval auxvals[3];
570 struct berval authcDN, authzDN;
574 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
575 "slap_sasl_authorize: conn %d authcid=\"%s\" authzid=\"%s\"\n",
576 conn ? conn->c_connid : -1, auth_identity, requested_user));
578 Debug( LDAP_DEBUG_ARGS, "SASL Authorize [conn=%ld]: "
579 "authcid=\"%s\" authzid=\"%s\"\n",
580 conn ? conn->c_connid : -1, auth_identity, requested_user );
583 prop_getnames( props, slap_propnames, auxvals );
585 /* Nothing to do if no authzID was given */
586 if ( !auxvals[1].name )
589 AC_MEMCPY( &authcDN, auxvals[0].values[0], sizeof(authcDN) );
590 AC_MEMCPY( &authzDN, auxvals[1].values[0], sizeof(authzDN) );
592 rc = slap_sasl_authorized( &authcDN, &authzDN );
593 if ( rc != LDAP_SUCCESS ) {
595 LDAP_LOG(( "sasl", LDAP_LEVEL_INFO,
596 "slap_sasl_authorize: conn %ld authorization disallowed (%d)\n",
597 (long)(conn ? conn->c_connid : -1), rc ));
599 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
600 " authorization disallowed (%d)\n",
601 (long) (conn ? conn->c_connid : -1), rc, 0 );
604 sasl_seterror( sconn, 0, "not authorized" );
609 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
610 "slap_sasl_authorize: conn %d authorization allowed\n",
611 (long)(conn ? conn->c_connid : -1 ) ));
613 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
614 " authorization allowed\n",
615 (long) (conn ? conn->c_connid : -1), 0, 0 );
628 struct berval authcDN, authzDN;
630 Connection *conn = context;
636 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
637 "slap_sasl_authorize: conn %d authcid=\"%s\" authzid=\"%s\"\n",
638 conn ? conn->c_connid : -1,
639 authcid ? authcid : "<empty>",
640 authzid ? authzid : "<empty>" ));
642 Debug( LDAP_DEBUG_ARGS, "SASL Authorize [conn=%ld]: "
643 "authcid=\"%s\" authzid=\"%s\"\n",
644 (long) (conn ? conn->c_connid : -1),
645 authcid ? authcid : "<empty>",
646 authzid ? authzid : "<empty>" );
649 /* Figure out how much data we have for the dn */
650 rc = sasl_getprop( conn->c_sasl_context, SASL_REALM, (void **)&realm );
651 if( rc != SASL_OK && rc != SASL_NOTDONE ) {
653 LDAP_LOG(( "sasl", LDAP_LEVEL_ERR,
654 "slap_sasl_authorize: getprop(REALM) failed.\n" ));
656 Debug(LDAP_DEBUG_TRACE,
657 "authorize: getprop(REALM) failed!\n", 0,0,0);
659 *errstr = "Could not extract realm";
663 /* Convert the identities to DN's. If no authzid was given, client will
664 be bound as the DN matching their username */
665 rc = slap_sasl_getdn( conn, (char *)authcid, 0, realm, &authcDN, FLAG_GETDN_AUTHCID );
666 if( rc != LDAP_SUCCESS ) {
667 *errstr = ldap_err2string( rc );
670 if( ( authzid == NULL ) || !strcmp( authcid,authzid ) ) {
672 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
673 "slap_sasl_authorize: conn %d Using authcDN=%s\n",
674 conn ? conn->c_connid : -1, authcDN.bv_val ));
676 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
677 "Using authcDN=%s\n", (long) (conn ? conn->c_connid : -1), authcDN.bv_val,0 );
680 *user = authcDN.bv_val;
684 rc = slap_sasl_getdn( conn, (char *)authzid, 0, realm, &authzDN, FLAG_GETDN_AUTHZID );
685 if( rc != LDAP_SUCCESS ) {
686 ch_free( authcDN.bv_val );
687 *errstr = ldap_err2string( rc );
691 rc = slap_sasl_authorized( &authcDN, &authzDN );
694 LDAP_LOG(( "sasl", LDAP_LEVEL_INFO,
695 "slap_sasl_authorize: conn %ld authorization disallowed (%d)\n",
696 (long)(conn ? conn->c_connid : -1), rc ));
698 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
699 " authorization disallowed (%d)\n",
700 (long) (conn ? conn->c_connid : -1), rc, 0 );
703 *errstr = "not authorized";
704 ch_free( authcDN.bv_val );
705 ch_free( authzDN.bv_val );
710 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
711 "slap_sasl_authorize: conn %d authorization allowed\n",
712 (long)(conn ? conn->c_connid : -1 ) ));
714 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
715 " authorization allowed\n",
716 (long) (conn ? conn->c_connid : -1), 0, 0 );
720 ch_free( authcDN.bv_val );
721 *user = authzDN.bv_val;
725 #endif /* SASL_VERSION_MAJOR >= 2 */
728 slap_sasl_err2ldap( int saslerr )
734 rc = LDAP_SASL_BIND_IN_PROGRESS;
743 rc = LDAP_AUTH_METHOD_NOT_SUPPORTED;
746 rc = LDAP_INVALID_CREDENTIALS;
749 rc = LDAP_INSUFFICIENT_ACCESS;
753 rc = LDAP_INAPPROPRIATE_AUTH;
765 int slap_sasl_init( void )
767 #ifdef HAVE_CYRUS_SASL
769 static sasl_callback_t server_callbacks[] = {
770 { SASL_CB_LOG, &slap_sasl_log, NULL },
771 { SASL_CB_LIST_END, NULL, NULL }
781 ldap_pvt_sasl_mutex_new,
782 ldap_pvt_sasl_mutex_lock,
783 ldap_pvt_sasl_mutex_unlock,
784 ldap_pvt_sasl_mutex_dispose );
786 #if SASL_VERSION_MAJOR >= 2
787 sasl_auxprop_add_plugin( "slapd", slap_auxprop_init );
789 /* should provide callbacks for logging */
790 /* server name should be configurable */
791 rc = sasl_server_init( server_callbacks, "slapd" );
793 if( rc != SASL_OK ) {
795 LDAP_LOG(( "sasl", LDAP_LEVEL_INFO,
796 "slap_sasl_init: init failed.\n" ));
798 Debug( LDAP_DEBUG_ANY, "sasl_server_init failed\n",
806 LDAP_LOG(( "sasl", LDAP_LEVEL_INFO,
807 "slap_sasl_init: initialized!\n"));
809 Debug( LDAP_DEBUG_TRACE, "slap_sasl_init: initialized!\n",
814 /* default security properties */
815 memset( &sasl_secprops, '\0', sizeof(sasl_secprops) );
816 sasl_secprops.max_ssf = INT_MAX;
817 sasl_secprops.maxbufsize = 65536;
818 sasl_secprops.security_flags = SASL_SEC_NOPLAINTEXT|SASL_SEC_NOANONYMOUS;
824 int slap_sasl_destroy( void )
826 #ifdef HAVE_CYRUS_SASL
835 int slap_sasl_open( Connection *conn )
837 int cb, sc = LDAP_SUCCESS;
838 #if SASL_VERSION_MAJOR >= 2
839 char *ipremoteport = NULL, *iplocalport = NULL;
842 #ifdef HAVE_CYRUS_SASL
843 sasl_conn_t *ctx = NULL;
844 sasl_callback_t *session_callbacks;
846 assert( conn->c_sasl_context == NULL );
847 assert( conn->c_sasl_extra == NULL );
849 conn->c_sasl_layers = 0;
852 #if SASL_VERSION_MAJOR >= 2
853 ch_calloc( 5, sizeof(sasl_callback_t));
855 ch_calloc( 3, sizeof(sasl_callback_t));
857 conn->c_sasl_extra = session_callbacks;
859 session_callbacks[cb=0].id = SASL_CB_LOG;
860 session_callbacks[cb].proc = &slap_sasl_log;
861 session_callbacks[cb++].context = conn;
863 session_callbacks[cb].id = SASL_CB_PROXY_POLICY;
864 session_callbacks[cb].proc = &slap_sasl_authorize;
865 session_callbacks[cb++].context = conn;
867 #if SASL_VERSION_MAJOR >= 2
868 session_callbacks[cb].id = SASL_CB_CANON_USER;
869 session_callbacks[cb].proc = &slap_sasl_canonicalize;
870 session_callbacks[cb++].context = conn;
872 /* XXXX: this should be conditional */
873 session_callbacks[cb].id = SASL_CB_SERVER_USERDB_CHECKPASS;
874 session_callbacks[cb].proc = &slap_sasl_checkpass;
875 session_callbacks[cb++].context = conn;
878 session_callbacks[cb].id = SASL_CB_LIST_END;
879 session_callbacks[cb].proc = NULL;
880 session_callbacks[cb++].context = NULL;
882 if( global_host == NULL ) {
883 global_host = ldap_pvt_get_fqdn( NULL );
886 /* create new SASL context */
887 #if SASL_VERSION_MAJOR >= 2
888 if ( conn->c_sock_name.bv_len != 0 &&
889 strncmp( conn->c_sock_name.bv_val, "IP=", 3 ) == 0) {
892 iplocalport = ch_strdup( conn->c_sock_name.bv_val + 3 );
893 /* Convert IPv6 addresses to address;port syntax. */
894 p = strrchr( iplocalport, ' ' );
895 /* Convert IPv4 addresses to address;port syntax. */
896 if ( p == NULL ) p = strchr( iplocalport, ':' );
901 if ( conn->c_peer_name.bv_len != 0 &&
902 strncmp( conn->c_peer_name.bv_val, "IP=", 3 ) == 0) {
905 ipremoteport = ch_strdup( conn->c_peer_name.bv_val + 3 );
906 /* Convert IPv6 addresses to address;port syntax. */
907 p = strrchr( ipremoteport, ' ' );
908 /* Convert IPv4 addresses to address;port syntax. */
909 if ( p == NULL ) p = strchr( ipremoteport, ':' );
914 sc = sasl_server_new( "ldap", global_host, global_realm,
915 iplocalport, ipremoteport, session_callbacks, 0, &ctx );
916 if ( iplocalport != NULL ) {
917 ch_free( iplocalport );
919 if ( ipremoteport != NULL ) {
920 ch_free( ipremoteport );
923 sc = sasl_server_new( "ldap", global_host, global_realm,
924 session_callbacks, SASL_SECURITY_LAYER, &ctx );
927 if( sc != SASL_OK ) {
929 LDAP_LOG(( "sasl", LDAP_LEVEL_ERR,
930 "slap_sasl_open: sasl_server_new failed: %d\n", sc ));
932 Debug( LDAP_DEBUG_ANY, "sasl_server_new failed: %d\n",
939 conn->c_sasl_context = ctx;
941 if( sc == SASL_OK ) {
942 sc = sasl_setprop( ctx,
943 SASL_SEC_PROPS, &sasl_secprops );
945 if( sc != SASL_OK ) {
947 LDAP_LOG(( "sasl", LDAP_LEVEL_ERR,
948 "slap_sasl_open: sasl_setprop failed: %d \n", sc ));
950 Debug( LDAP_DEBUG_ANY, "sasl_setprop failed: %d\n",
954 slap_sasl_close( conn );
959 sc = slap_sasl_err2ldap( sc );
964 int slap_sasl_external(
967 const char *auth_id )
969 #if SASL_VERSION_MAJOR >= 2
971 sasl_conn_t *ctx = conn->c_sasl_context;
974 return LDAP_UNAVAILABLE;
977 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL, &ssf );
979 if ( sc != SASL_OK ) {
983 sc = sasl_setprop( ctx, SASL_AUTH_EXTERNAL, auth_id );
985 if ( sc != SASL_OK ) {
989 #elif defined(HAVE_CYRUS_SASL)
991 sasl_conn_t *ctx = conn->c_sasl_context;
992 sasl_external_properties_t extprops;
995 return LDAP_UNAVAILABLE;
998 memset( &extprops, '\0', sizeof(extprops) );
1000 extprops.auth_id = (char *) auth_id;
1002 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL,
1003 (void *) &extprops );
1005 if ( sc != SASL_OK ) {
1010 return LDAP_SUCCESS;
1013 int slap_sasl_reset( Connection *conn )
1015 #ifdef HAVE_CYRUS_SASL
1016 sasl_conn_t *ctx = conn->c_sasl_context;
1021 /* must return "anonymous" */
1022 return LDAP_SUCCESS;
1025 char ** slap_sasl_mechs( Connection *conn )
1027 char **mechs = NULL;
1029 #ifdef HAVE_CYRUS_SASL
1030 sasl_conn_t *ctx = conn->c_sasl_context;
1034 SASL_CONST char *mechstr;
1036 sc = sasl_listmech( ctx,
1037 NULL, NULL, ",", NULL,
1038 &mechstr, NULL, NULL );
1040 if( sc != SASL_OK ) {
1042 LDAP_LOG(( "sasl", LDAP_LEVEL_ERR,
1043 "slap_sasl_mechs: sasl_listmech failed: %d\n", sc ));
1045 Debug( LDAP_DEBUG_ANY, "slap_sasl_listmech failed: %d\n",
1052 mechs = str2charray( mechstr, "," );
1054 #if SASL_VERSION_MAJOR < 2
1063 int slap_sasl_close( Connection *conn )
1065 #ifdef HAVE_CYRUS_SASL
1066 sasl_conn_t *ctx = conn->c_sasl_context;
1069 sasl_dispose( &ctx );
1072 conn->c_sasl_context = NULL;
1074 free( conn->c_sasl_extra );
1075 conn->c_sasl_extra = NULL;
1078 return LDAP_SUCCESS;
1086 struct berval *cred,
1092 #ifdef HAVE_CYRUS_SASL
1093 sasl_conn_t *ctx = conn->c_sasl_context;
1094 struct berval response;
1095 unsigned reslen = 0;
1096 const char *errstr = NULL;
1100 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
1101 "sasl_bind: conn %ld dn=\"%s\" mech=%s datalen=%ld\n",
1103 dn->bv_len ? dn->bv_val : "",
1104 conn->c_sasl_bind_in_progress ? "<continuing>" : conn->c_sasl_bind_mech.bv_val,
1105 cred ? cred->bv_len : 0 ));
1107 Debug(LDAP_DEBUG_ARGS,
1108 "==> sasl_bind: dn=\"%s\" mech=%s datalen=%ld\n",
1109 dn->bv_len ? dn->bv_val : "",
1110 conn->c_sasl_bind_in_progress ? "<continuing>":conn->c_sasl_bind_mech.bv_val,
1111 cred ? cred->bv_len : 0 );
1116 send_ldap_result( conn, op, LDAP_UNAVAILABLE,
1117 NULL, "SASL unavailable on this session", NULL, NULL );
1121 #if SASL_VERSION_MAJOR >= 2
1122 #define START( ctx, mech, cred, clen, resp, rlen, err ) \
1123 sasl_server_start( ctx, mech, cred, clen, resp, rlen )
1124 #define STEP( ctx, cred, clen, resp, rlen, err ) \
1125 sasl_server_step( ctx, cred, clen, resp, rlen )
1127 #define START( ctx, mech, cred, clen, resp, rlen, err ) \
1128 sasl_server_start( ctx, mech, cred, clen, resp, rlen, err )
1129 #define STEP( ctx, cred, clen, resp, rlen, err ) \
1130 sasl_server_step( ctx, cred, clen, resp, rlen, err )
1133 if ( !conn->c_sasl_bind_in_progress ) {
1135 conn->c_sasl_bind_mech.bv_val,
1136 cred->bv_len ? cred->bv_val : "",
1138 (SASL_CONST char **)&response.bv_val, &reslen, &errstr );
1142 cred->bv_val, cred->bv_len,
1143 (SASL_CONST char **)&response.bv_val, &reslen, &errstr );
1146 response.bv_len = reslen;
1148 if ( sc == SASL_OK ) {
1149 #if SASL_VERSION_MAJOR >= 2
1150 struct propctx *props = sasl_auxprop_getctx( ctx );
1151 struct propval vals[3];
1152 sasl_ssf_t *ssf = NULL;
1154 prop_getnames( props, slap_propnames, vals );
1156 AC_MEMCPY( edn, vals[0].values[0], sizeof(*edn) );
1157 if ( vals[1].name ) {
1158 ch_free( edn->bv_val );
1159 AC_MEMCPY( edn, vals[1].values[0], sizeof(*edn) );
1164 (void) sasl_getprop( ctx, SASL_SSF, (void *)&ssf );
1165 *ssfp = ssf ? *ssf : 0;
1168 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1169 conn->c_sasl_layers++;
1170 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1173 send_ldap_sasl( conn, op, rc,
1174 NULL, NULL, NULL, NULL,
1175 response.bv_len ? &response : NULL );
1177 char *username = NULL;
1179 sc = sasl_getprop( ctx,
1180 SASL_USERNAME, (SASL_CONST void **)&username );
1182 if ( sc != SASL_OK ) {
1184 LDAP_LOG(( "sasl", LDAP_LEVEL_ERR,
1185 "slap_sasl_bind: getprop(USERNAME) failed: %d\n", sc ));
1187 Debug(LDAP_DEBUG_TRACE,
1188 "slap_sasl_bind: getprop(USERNAME) failed!\n",
1193 send_ldap_result( conn, op, rc = slap_sasl_err2ldap( sc ),
1194 NULL, "no SASL username", NULL, NULL );
1197 sasl_ssf_t *ssf = NULL;
1200 ber_str2bv( username, 0, 1, edn );
1202 (void) sasl_getprop( ctx, SASL_SSF, (void *)&ssf );
1203 *ssfp = ssf ? *ssf : 0;
1206 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1207 conn->c_sasl_layers++;
1208 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1211 send_ldap_sasl( conn, op, rc,
1212 NULL, NULL, NULL, NULL,
1213 response.bv_len ? &response : NULL );
1217 } else if ( sc == SASL_CONTINUE ) {
1218 send_ldap_sasl( conn, op, rc = LDAP_SASL_BIND_IN_PROGRESS,
1219 NULL, NULL, NULL, NULL, &response );
1222 #if SASL_VERSION_MAJOR >= 2
1223 errstr = sasl_errdetail( ctx );
1225 send_ldap_result( conn, op, rc = slap_sasl_err2ldap( sc ),
1226 NULL, errstr, NULL, NULL );
1229 #if SASL_VERSION_MAJOR < 2
1230 if( response.bv_len ) {
1231 ch_free( response.bv_val );
1236 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
1237 "slap_sasl_bind: rc=%d\n", rc ));
1239 Debug(LDAP_DEBUG_TRACE, "<== slap_sasl_bind: rc=%d\n", rc, 0, 0);
1244 send_ldap_result( conn, op, rc = LDAP_UNAVAILABLE,
1245 NULL, "SASL not supported", NULL, NULL );
1251 char* slap_sasl_secprops( const char *in )
1253 #ifdef HAVE_CYRUS_SASL
1254 int rc = ldap_pvt_sasl_secprops( in, &sasl_secprops );
1256 return rc == LDAP_SUCCESS ? NULL : "Invalid security properties";
1258 return "SASL not supported";