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_strncopy( p, c1, dn->bv_len );
245 int rlen = dn->bv_len - ( realm - c1 );
246 p = slap_strcopy( p, ",cn=" );
247 p = slap_strncopy( p, realm, rlen );
249 } else if( user_realm && *user_realm ) {
250 p = slap_strcopy( p, ",cn=" );
251 p = slap_strcopy( p, user_realm );
254 if( conn->c_sasl_bind_mech.bv_len ) {
255 p = slap_strcopy( p, ",cn=" );
256 p = slap_strcopy( p, conn->c_sasl_bind_mech.bv_val );
258 p = slap_strcopy( p, ",cn=auth" );
259 dn->bv_len = p - dn->bv_val;
262 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
263 "slap_sasl_getdn: u:id converted to %s.\n", dn->bv_val ));
265 Debug( LDAP_DEBUG_TRACE, "getdn: u:id converted to %s\n", dn->bv_val,0,0 );
269 /* All strings are in DN form now. Normalize if needed. */
271 rc = dnNormalize2( NULL, dn, &dn2 );
273 /* User DNs were constructed above and must be freed now */
274 if ( is_dn == SET_U )
275 ch_free( dn->bv_val );
277 if ( rc != LDAP_SUCCESS ) {
285 /* Run thru regexp */
286 slap_sasl2dn( conn, dn, &dn2 );
288 ch_free( dn->bv_val );
291 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
292 "slap_sasl_getdn: dn:id converted to %s.\n", dn->bv_val ));
294 Debug( LDAP_DEBUG_TRACE, "getdn: dn:id converted to %s\n",
299 return( LDAP_SUCCESS );
302 #if SASL_VERSION_MAJOR >= 2
303 static const char *slap_propnames[] = { "*authcDN", "*authzDN", NULL };
308 sasl_server_params_t *sparams,
315 const struct propval *list;
317 AttributeDescription *ad;
320 list = sparams->utils->prop_get( sparams->propctx );
322 /* Find our DN first */
323 for( i = 0, last = 0; list[i].name; i++ ) {
324 if ( list[i].name[0] == '*' ) {
325 if ( (flags & SASL_AUXPROP_AUTHZID) &&
326 !strcmp( list[i].name, slap_propnames[1] ) ) {
327 if ( list[i].values && list[i].values[0] )
328 AC_MEMCPY( &dn, list[i].values[0], sizeof( dn ) );
329 if ( !last ) last = i;
332 if ( !strcmp( list[i].name, slap_propnames[0] ) ) {
333 if ( !last ) last = i;
334 if ( list[i].values && list[i].values[0] ) {
335 AC_MEMCPY( &dn, list[i].values[0], sizeof( dn ) );
336 if ( !(flags & SASL_AUXPROP_AUTHZID) )
343 /* Now fetch the rest */
344 for( i = 0; i < last; i++ ) {
345 const char *name = list[i].name;
347 if ( name[0] == '*' ) {
348 if ( flags & SASL_AUXPROP_AUTHZID ) continue;
350 } else if ( !(flags & SASL_AUXPROP_AUTHZID ) )
353 if ( list[i].values ) {
354 if ( !(flags & SASL_AUXPROP_OVERRIDE) ) continue;
355 sparams->utils->prop_erase( sparams->propctx, list[i].name );
358 rc = slap_str2ad( name, &ad, &text );
359 if ( rc != LDAP_SUCCESS ) {
361 LDAP_LOG(( "sasl", LDAP_LEVEL_DETAIL1,
362 "slap_auxprop: str2ad(%s): %s\n", name, text ));
364 Debug( LDAP_DEBUG_TRACE,
365 "slap_auxprop: str2ad(%s): %s\n", name, text, 0 );
367 rc = slap_str2undef_ad( name, &ad, &text );
368 if ( rc != LDAP_SUCCESS ) continue;
370 rc = backend_attribute( NULL,NULL,NULL,NULL, &dn, ad, &vals );
371 if ( rc != LDAP_SUCCESS ) continue;
372 for ( bv = vals; bv->bv_val; bv++ ) {
373 sparams->utils->prop_set( sparams->propctx, list[i].name,
374 bv->bv_val, bv->bv_len );
376 ber_bvarray_free( vals );
380 static sasl_auxprop_plug_t slap_auxprop_plugin = {
383 NULL, /* glob_context */
384 NULL, /* auxprop_free */
392 const sasl_utils_t *utils,
395 sasl_auxprop_plug_t **plug,
396 const char *plugname)
398 if ( !out_version | !plug ) return SASL_BADPARAM;
400 if ( max_version < SASL_AUXPROP_PLUG_VERSION ) return SASL_BADVERS;
402 *out_version = SASL_AUXPROP_PLUG_VERSION;
403 *plug = &slap_auxprop_plugin;
411 const char *username,
414 struct propctx *propctx)
416 Connection *conn = (Connection *)context;
417 struct berval dn, cred;
421 cred.bv_val = (char *)pass;
422 cred.bv_len = passlen;
424 /* SASL will fallback to its own mechanisms if we don't
425 * find an answer here.
428 rc = slap_sasl_getdn( conn, (char *)username, 0, NULL, &dn,
429 FLAG_GETDN_AUTHCID );
430 if ( rc != LDAP_SUCCESS ) {
431 sasl_seterror( sconn, 0, ldap_err2string( rc ) );
435 if ( dn.bv_len == 0 ) {
436 sasl_seterror( sconn, 0,
437 "No password is associated with the Root DSE" );
438 if ( dn.bv_val != NULL ) {
439 ch_free( dn.bv_val );
444 rc = backend_attribute( NULL, NULL, NULL, NULL, &dn,
445 slap_schema.si_ad_userPassword, &vals);
446 if ( rc != LDAP_SUCCESS ) {
447 ch_free( dn.bv_val );
448 sasl_seterror( sconn, 0, ldap_err2string( rc ) );
449 return SASL_NOVERIFY;
454 if ( vals != NULL ) {
455 for ( bv = vals; bv->bv_val != NULL; bv++ ) {
456 if ( !lutil_passwd( bv, &cred, NULL ) ) {
461 ber_bvarray_free( vals );
464 if ( rc != SASL_OK ) {
465 sasl_seterror( sconn, 0,
466 ldap_err2string( LDAP_INVALID_CREDENTIALS ) );
469 ch_free( dn.bv_val );
474 /* Convert a SASL authcid or authzid into a DN. Store the DN in an
475 * auxiliary property, so that we can refer to it in sasl_authorize
476 * without interfering with anything else. Also, the SASL username
477 * buffer is constrained to 256 characters, and our DNs could be
478 * much longer (totally arbitrary length)...
481 slap_sasl_canonicalize(
487 const char *user_realm,
492 Connection *conn = (Connection *)context;
493 struct propctx *props = sasl_auxprop_getctx( sconn );
494 struct propval auxvals[3];
497 const char *names[2];
502 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
503 "slap_sasl_canonicalize: conn %d %s=\"%s\"\n",
504 conn ? conn->c_connid : -1,
505 (flags & SASL_CU_AUTHID) ? "authcid" : "authzid",
506 in ? in : "<empty>" ));
508 Debug( LDAP_DEBUG_ARGS, "SASL Canonicalize [conn=%ld]: "
510 conn ? conn->c_connid : -1,
511 (flags & SASL_CU_AUTHID) ? "authcid" : "authzid",
512 in ? in : "<empty>" );
515 /* If name is too big, just truncate. We don't care, we're
516 * using DNs, not the usernames.
518 if ( inlen > out_max )
521 /* See if we need to add request, can only do it once */
522 prop_getnames( props, slap_propnames, auxvals );
523 if ( !auxvals[0].name )
524 prop_request( props, slap_propnames );
526 if ( flags & SASL_CU_AUTHID )
531 /* Already been here? */
532 if ( auxvals[which].values )
535 if ( flags == SASL_CU_AUTHZID ) {
536 /* If we got unqualified authzid's, they probably came from SASL
537 * itself just passing the authcid to us. Look inside the oparams
538 * structure to see if that's true. (HACK: the out_len pointer is
539 * the address of a member of a sasl_out_params_t structure...)
541 sasl_out_params_t dummy;
542 int offset = (void *)&dummy.ulen - (void *)&dummy.authid;
543 char **authid = (void *)out_len - offset;
544 if ( *authid && !strcmp( in, *authid ) )
548 rc = slap_sasl_getdn( conn, (char *)in, inlen, (char *)user_realm, &dn,
549 (flags & SASL_CU_AUTHID) ? FLAG_GETDN_AUTHCID : FLAG_GETDN_AUTHZID );
550 if ( rc != LDAP_SUCCESS ) {
551 sasl_seterror( sconn, 0, ldap_err2string( rc ) );
555 names[0] = slap_propnames[which];
558 prop_set( props, names[0], (char *)&dn, sizeof( dn ) );
561 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
562 "slap_sasl_canonicalize: conn %d %s=\"%s\"\n",
563 conn ? conn->c_connid : -1,
564 names[0]+1, dn.bv_val ));
566 Debug( LDAP_DEBUG_ARGS, "SASL Canonicalize [conn=%ld]: "
568 conn ? conn->c_connid : -1,
569 names[0]+1, dn.bv_val );
571 done: AC_MEMCPY( out, in, inlen );
583 char *requested_user,
587 const char *def_realm,
589 struct propctx *props)
591 Connection *conn = (Connection *)context;
592 struct propval auxvals[3];
593 struct berval authcDN, authzDN;
597 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
598 "slap_sasl_authorize: conn %d authcid=\"%s\" authzid=\"%s\"\n",
599 conn ? conn->c_connid : -1, auth_identity, requested_user));
601 Debug( LDAP_DEBUG_ARGS, "SASL Authorize [conn=%ld]: "
602 "authcid=\"%s\" authzid=\"%s\"\n",
603 conn ? conn->c_connid : -1, auth_identity, requested_user );
606 prop_getnames( props, slap_propnames, auxvals );
608 /* Nothing to do if no authzID was given */
609 if ( !auxvals[1].name || !auxvals[1].values )
612 AC_MEMCPY( &authcDN, auxvals[0].values[0], sizeof(authcDN) );
613 AC_MEMCPY( &authzDN, auxvals[1].values[0], sizeof(authzDN) );
615 rc = slap_sasl_authorized( &authcDN, &authzDN );
616 if ( rc != LDAP_SUCCESS ) {
618 LDAP_LOG(( "sasl", LDAP_LEVEL_INFO,
619 "slap_sasl_authorize: conn %ld authorization disallowed (%d)\n",
620 (long)(conn ? conn->c_connid : -1), rc ));
622 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
623 " authorization disallowed (%d)\n",
624 (long) (conn ? conn->c_connid : -1), rc, 0 );
627 sasl_seterror( sconn, 0, "not authorized" );
632 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
633 "slap_sasl_authorize: conn %d authorization allowed\n",
634 (long)(conn ? conn->c_connid : -1 ) ));
636 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
637 " authorization allowed\n",
638 (long) (conn ? conn->c_connid : -1), 0, 0 );
651 struct berval authcDN, authzDN;
653 Connection *conn = context;
659 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
660 "slap_sasl_authorize: conn %d authcid=\"%s\" authzid=\"%s\"\n",
661 conn ? conn->c_connid : -1,
662 authcid ? authcid : "<empty>",
663 authzid ? authzid : "<empty>" ));
665 Debug( LDAP_DEBUG_ARGS, "SASL Authorize [conn=%ld]: "
666 "authcid=\"%s\" authzid=\"%s\"\n",
667 (long) (conn ? conn->c_connid : -1),
668 authcid ? authcid : "<empty>",
669 authzid ? authzid : "<empty>" );
672 /* Figure out how much data we have for the dn */
673 rc = sasl_getprop( conn->c_sasl_context, SASL_REALM, (void **)&realm );
674 if( rc != SASL_OK && rc != SASL_NOTDONE ) {
676 LDAP_LOG(( "sasl", LDAP_LEVEL_ERR,
677 "slap_sasl_authorize: getprop(REALM) failed.\n" ));
679 Debug(LDAP_DEBUG_TRACE,
680 "authorize: getprop(REALM) failed!\n", 0,0,0);
682 *errstr = "Could not extract realm";
686 /* Convert the identities to DN's. If no authzid was given, client will
687 be bound as the DN matching their username */
688 rc = slap_sasl_getdn( conn, (char *)authcid, 0, realm, &authcDN, FLAG_GETDN_AUTHCID );
689 if( rc != LDAP_SUCCESS ) {
690 *errstr = ldap_err2string( rc );
693 if( ( authzid == NULL ) || !strcmp( authcid,authzid ) ) {
695 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
696 "slap_sasl_authorize: conn %d Using authcDN=%s\n",
697 conn ? conn->c_connid : -1, authcDN.bv_val ));
699 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
700 "Using authcDN=%s\n", (long) (conn ? conn->c_connid : -1), authcDN.bv_val,0 );
703 *user = authcDN.bv_val;
707 rc = slap_sasl_getdn( conn, (char *)authzid, 0, realm, &authzDN, FLAG_GETDN_AUTHZID );
708 if( rc != LDAP_SUCCESS ) {
709 ch_free( authcDN.bv_val );
710 *errstr = ldap_err2string( rc );
714 rc = slap_sasl_authorized( &authcDN, &authzDN );
717 LDAP_LOG(( "sasl", LDAP_LEVEL_INFO,
718 "slap_sasl_authorize: conn %ld authorization disallowed (%d)\n",
719 (long)(conn ? conn->c_connid : -1), rc ));
721 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
722 " authorization disallowed (%d)\n",
723 (long) (conn ? conn->c_connid : -1), rc, 0 );
726 *errstr = "not authorized";
727 ch_free( authcDN.bv_val );
728 ch_free( authzDN.bv_val );
733 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
734 "slap_sasl_authorize: conn %d authorization allowed\n",
735 (long)(conn ? conn->c_connid : -1 ) ));
737 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
738 " authorization allowed\n",
739 (long) (conn ? conn->c_connid : -1), 0, 0 );
743 ch_free( authcDN.bv_val );
744 *user = authzDN.bv_val;
748 #endif /* SASL_VERSION_MAJOR >= 2 */
751 slap_sasl_err2ldap( int saslerr )
757 rc = LDAP_SASL_BIND_IN_PROGRESS;
766 rc = LDAP_AUTH_METHOD_NOT_SUPPORTED;
769 rc = LDAP_INVALID_CREDENTIALS;
772 rc = LDAP_INSUFFICIENT_ACCESS;
776 rc = LDAP_INAPPROPRIATE_AUTH;
788 int slap_sasl_init( void )
790 #ifdef HAVE_CYRUS_SASL
792 static sasl_callback_t server_callbacks[] = {
793 { SASL_CB_LOG, &slap_sasl_log, NULL },
794 { SASL_CB_LIST_END, NULL, NULL }
804 ldap_pvt_sasl_mutex_new,
805 ldap_pvt_sasl_mutex_lock,
806 ldap_pvt_sasl_mutex_unlock,
807 ldap_pvt_sasl_mutex_dispose );
809 #if SASL_VERSION_MAJOR >= 2
810 sasl_auxprop_add_plugin( "slapd", slap_auxprop_init );
812 /* should provide callbacks for logging */
813 /* server name should be configurable */
814 rc = sasl_server_init( server_callbacks, "slapd" );
816 if( rc != SASL_OK ) {
818 LDAP_LOG(( "sasl", LDAP_LEVEL_INFO,
819 "slap_sasl_init: init failed.\n" ));
821 Debug( LDAP_DEBUG_ANY, "sasl_server_init failed\n",
829 LDAP_LOG(( "sasl", LDAP_LEVEL_INFO,
830 "slap_sasl_init: initialized!\n"));
832 Debug( LDAP_DEBUG_TRACE, "slap_sasl_init: initialized!\n",
837 /* default security properties */
838 memset( &sasl_secprops, '\0', sizeof(sasl_secprops) );
839 sasl_secprops.max_ssf = INT_MAX;
840 sasl_secprops.maxbufsize = 65536;
841 sasl_secprops.security_flags = SASL_SEC_NOPLAINTEXT|SASL_SEC_NOANONYMOUS;
847 int slap_sasl_destroy( void )
849 #ifdef HAVE_CYRUS_SASL
858 int slap_sasl_open( Connection *conn )
860 int cb, sc = LDAP_SUCCESS;
861 #if SASL_VERSION_MAJOR >= 2
862 char *ipremoteport = NULL, *iplocalport = NULL;
865 #ifdef HAVE_CYRUS_SASL
866 sasl_conn_t *ctx = NULL;
867 sasl_callback_t *session_callbacks;
869 assert( conn->c_sasl_context == NULL );
870 assert( conn->c_sasl_extra == NULL );
872 conn->c_sasl_layers = 0;
875 #if SASL_VERSION_MAJOR >= 2
876 ch_calloc( 5, sizeof(sasl_callback_t));
878 ch_calloc( 3, sizeof(sasl_callback_t));
880 conn->c_sasl_extra = session_callbacks;
882 session_callbacks[cb=0].id = SASL_CB_LOG;
883 session_callbacks[cb].proc = &slap_sasl_log;
884 session_callbacks[cb++].context = conn;
886 session_callbacks[cb].id = SASL_CB_PROXY_POLICY;
887 session_callbacks[cb].proc = &slap_sasl_authorize;
888 session_callbacks[cb++].context = conn;
890 #if SASL_VERSION_MAJOR >= 2
891 session_callbacks[cb].id = SASL_CB_CANON_USER;
892 session_callbacks[cb].proc = &slap_sasl_canonicalize;
893 session_callbacks[cb++].context = conn;
895 /* XXXX: this should be conditional */
896 session_callbacks[cb].id = SASL_CB_SERVER_USERDB_CHECKPASS;
897 session_callbacks[cb].proc = &slap_sasl_checkpass;
898 session_callbacks[cb++].context = conn;
901 session_callbacks[cb].id = SASL_CB_LIST_END;
902 session_callbacks[cb].proc = NULL;
903 session_callbacks[cb++].context = NULL;
905 if( global_host == NULL ) {
906 global_host = ldap_pvt_get_fqdn( NULL );
909 /* create new SASL context */
910 #if SASL_VERSION_MAJOR >= 2
911 if ( conn->c_sock_name.bv_len != 0 &&
912 strncmp( conn->c_sock_name.bv_val, "IP=", 3 ) == 0) {
915 iplocalport = ch_strdup( conn->c_sock_name.bv_val + 3 );
916 /* Convert IPv6 addresses to address;port syntax. */
917 p = strrchr( iplocalport, ' ' );
918 /* Convert IPv4 addresses to address;port syntax. */
919 if ( p == NULL ) p = strchr( iplocalport, ':' );
924 if ( conn->c_peer_name.bv_len != 0 &&
925 strncmp( conn->c_peer_name.bv_val, "IP=", 3 ) == 0) {
928 ipremoteport = ch_strdup( conn->c_peer_name.bv_val + 3 );
929 /* Convert IPv6 addresses to address;port syntax. */
930 p = strrchr( ipremoteport, ' ' );
931 /* Convert IPv4 addresses to address;port syntax. */
932 if ( p == NULL ) p = strchr( ipremoteport, ':' );
937 sc = sasl_server_new( "ldap", global_host, global_realm,
938 iplocalport, ipremoteport, session_callbacks, 0, &ctx );
939 if ( iplocalport != NULL ) {
940 ch_free( iplocalport );
942 if ( ipremoteport != NULL ) {
943 ch_free( ipremoteport );
946 sc = sasl_server_new( "ldap", global_host, global_realm,
947 session_callbacks, SASL_SECURITY_LAYER, &ctx );
950 if( sc != SASL_OK ) {
952 LDAP_LOG(( "sasl", LDAP_LEVEL_ERR,
953 "slap_sasl_open: sasl_server_new failed: %d\n", sc ));
955 Debug( LDAP_DEBUG_ANY, "sasl_server_new failed: %d\n",
962 conn->c_sasl_context = ctx;
964 if( sc == SASL_OK ) {
965 sc = sasl_setprop( ctx,
966 SASL_SEC_PROPS, &sasl_secprops );
968 if( sc != SASL_OK ) {
970 LDAP_LOG(( "sasl", LDAP_LEVEL_ERR,
971 "slap_sasl_open: sasl_setprop failed: %d \n", sc ));
973 Debug( LDAP_DEBUG_ANY, "sasl_setprop failed: %d\n",
977 slap_sasl_close( conn );
982 sc = slap_sasl_err2ldap( sc );
987 int slap_sasl_external(
990 const char *auth_id )
992 #if SASL_VERSION_MAJOR >= 2
994 sasl_conn_t *ctx = conn->c_sasl_context;
997 return LDAP_UNAVAILABLE;
1000 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL, &ssf );
1002 if ( sc != SASL_OK ) {
1006 sc = sasl_setprop( ctx, SASL_AUTH_EXTERNAL, auth_id );
1008 if ( sc != SASL_OK ) {
1012 #elif defined(HAVE_CYRUS_SASL)
1014 sasl_conn_t *ctx = conn->c_sasl_context;
1015 sasl_external_properties_t extprops;
1017 if ( ctx == NULL ) {
1018 return LDAP_UNAVAILABLE;
1021 memset( &extprops, '\0', sizeof(extprops) );
1023 extprops.auth_id = (char *) auth_id;
1025 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL,
1026 (void *) &extprops );
1028 if ( sc != SASL_OK ) {
1033 return LDAP_SUCCESS;
1036 int slap_sasl_reset( Connection *conn )
1038 #ifdef HAVE_CYRUS_SASL
1039 sasl_conn_t *ctx = conn->c_sasl_context;
1044 /* must return "anonymous" */
1045 return LDAP_SUCCESS;
1048 char ** slap_sasl_mechs( Connection *conn )
1050 char **mechs = NULL;
1052 #ifdef HAVE_CYRUS_SASL
1053 sasl_conn_t *ctx = conn->c_sasl_context;
1057 SASL_CONST char *mechstr;
1059 sc = sasl_listmech( ctx,
1060 NULL, NULL, ",", NULL,
1061 &mechstr, NULL, NULL );
1063 if( sc != SASL_OK ) {
1065 LDAP_LOG(( "sasl", LDAP_LEVEL_ERR,
1066 "slap_sasl_mechs: sasl_listmech failed: %d\n", sc ));
1068 Debug( LDAP_DEBUG_ANY, "slap_sasl_listmech failed: %d\n",
1075 mechs = str2charray( mechstr, "," );
1077 #if SASL_VERSION_MAJOR < 2
1086 int slap_sasl_close( Connection *conn )
1088 #ifdef HAVE_CYRUS_SASL
1089 sasl_conn_t *ctx = conn->c_sasl_context;
1092 sasl_dispose( &ctx );
1095 conn->c_sasl_context = NULL;
1097 free( conn->c_sasl_extra );
1098 conn->c_sasl_extra = NULL;
1101 return LDAP_SUCCESS;
1109 struct berval *cred,
1115 #ifdef HAVE_CYRUS_SASL
1116 sasl_conn_t *ctx = conn->c_sasl_context;
1117 struct berval response;
1118 unsigned reslen = 0;
1119 const char *errstr = NULL;
1123 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
1124 "sasl_bind: conn %ld dn=\"%s\" mech=%s datalen=%ld\n",
1126 dn->bv_len ? dn->bv_val : "",
1127 conn->c_sasl_bind_in_progress ? "<continuing>" : conn->c_sasl_bind_mech.bv_val,
1128 cred ? cred->bv_len : 0 ));
1130 Debug(LDAP_DEBUG_ARGS,
1131 "==> sasl_bind: dn=\"%s\" mech=%s datalen=%ld\n",
1132 dn->bv_len ? dn->bv_val : "",
1133 conn->c_sasl_bind_in_progress ? "<continuing>":conn->c_sasl_bind_mech.bv_val,
1134 cred ? cred->bv_len : 0 );
1139 send_ldap_result( conn, op, LDAP_UNAVAILABLE,
1140 NULL, "SASL unavailable on this session", NULL, NULL );
1144 #if SASL_VERSION_MAJOR >= 2
1145 #define START( ctx, mech, cred, clen, resp, rlen, err ) \
1146 sasl_server_start( ctx, mech, cred, clen, resp, rlen )
1147 #define STEP( ctx, cred, clen, resp, rlen, err ) \
1148 sasl_server_step( ctx, cred, clen, resp, rlen )
1150 #define START( ctx, mech, cred, clen, resp, rlen, err ) \
1151 sasl_server_start( ctx, mech, cred, clen, resp, rlen, err )
1152 #define STEP( ctx, cred, clen, resp, rlen, err ) \
1153 sasl_server_step( ctx, cred, clen, resp, rlen, err )
1156 if ( !conn->c_sasl_bind_in_progress ) {
1158 conn->c_sasl_bind_mech.bv_val,
1159 cred->bv_len ? cred->bv_val : "",
1161 (SASL_CONST char **)&response.bv_val, &reslen, &errstr );
1165 cred->bv_val, cred->bv_len,
1166 (SASL_CONST char **)&response.bv_val, &reslen, &errstr );
1169 response.bv_len = reslen;
1171 if ( sc == SASL_OK ) {
1172 #if SASL_VERSION_MAJOR >= 2
1173 struct propctx *props = sasl_auxprop_getctx( ctx );
1174 struct propval vals[3];
1175 sasl_ssf_t *ssf = NULL;
1177 prop_getnames( props, slap_propnames, vals );
1179 AC_MEMCPY( edn, vals[0].values[0], sizeof(*edn) );
1180 if ( vals[1].name && vals[1].values ) {
1181 ch_free( edn->bv_val );
1182 AC_MEMCPY( edn, vals[1].values[0], sizeof(*edn) );
1187 (void) sasl_getprop( ctx, SASL_SSF, (void *)&ssf );
1188 *ssfp = ssf ? *ssf : 0;
1191 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1192 conn->c_sasl_layers++;
1193 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1196 send_ldap_sasl( conn, op, rc,
1197 NULL, NULL, NULL, NULL,
1198 response.bv_len ? &response : NULL );
1200 char *username = NULL;
1202 sc = sasl_getprop( ctx,
1203 SASL_USERNAME, (SASL_CONST void **)&username );
1205 if ( sc != SASL_OK ) {
1207 LDAP_LOG(( "sasl", LDAP_LEVEL_ERR,
1208 "slap_sasl_bind: getprop(USERNAME) failed: %d\n", sc ));
1210 Debug(LDAP_DEBUG_TRACE,
1211 "slap_sasl_bind: getprop(USERNAME) failed!\n",
1216 send_ldap_result( conn, op, rc = slap_sasl_err2ldap( sc ),
1217 NULL, "no SASL username", NULL, NULL );
1220 sasl_ssf_t *ssf = NULL;
1223 ber_str2bv( username, 0, 1, edn );
1225 (void) sasl_getprop( ctx, SASL_SSF, (void *)&ssf );
1226 *ssfp = ssf ? *ssf : 0;
1229 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1230 conn->c_sasl_layers++;
1231 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1234 send_ldap_sasl( conn, op, rc,
1235 NULL, NULL, NULL, NULL,
1236 response.bv_len ? &response : NULL );
1240 } else if ( sc == SASL_CONTINUE ) {
1241 send_ldap_sasl( conn, op, rc = LDAP_SASL_BIND_IN_PROGRESS,
1242 NULL, NULL, NULL, NULL, &response );
1245 #if SASL_VERSION_MAJOR >= 2
1246 errstr = sasl_errdetail( ctx );
1248 send_ldap_result( conn, op, rc = slap_sasl_err2ldap( sc ),
1249 NULL, errstr, NULL, NULL );
1252 #if SASL_VERSION_MAJOR < 2
1253 if( response.bv_len ) {
1254 ch_free( response.bv_val );
1259 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
1260 "slap_sasl_bind: rc=%d\n", rc ));
1262 Debug(LDAP_DEBUG_TRACE, "<== slap_sasl_bind: rc=%d\n", rc, 0, 0);
1267 send_ldap_result( conn, op, rc = LDAP_UNAVAILABLE,
1268 NULL, "SASL not supported", NULL, NULL );
1274 char* slap_sasl_secprops( const char *in )
1276 #ifdef HAVE_CYRUS_SASL
1277 int rc = ldap_pvt_sasl_secprops( in, &sasl_secprops );
1279 return rc == LDAP_SUCCESS ? NULL : "Invalid security properties";
1281 return "SASL not supported";