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 );
605 if ( conn->c_sasl_dn.bv_val ) {
606 ch_free( conn->c_sasl_dn.bv_val );
607 conn->c_sasl_dn.bv_val = NULL;
608 conn->c_sasl_dn.bv_len = 0;
611 prop_getnames( props, slap_propnames, auxvals );
613 AC_MEMCPY( &authcDN, auxvals[0].values[0], sizeof(authcDN) );
615 /* Nothing to do if no authzID was given */
616 if ( !auxvals[1].name || !auxvals[1].values ) {
617 conn->c_sasl_dn = authcDN;
621 AC_MEMCPY( &authzDN, auxvals[1].values[0], sizeof(authzDN) );
623 rc = slap_sasl_authorized( &authcDN, &authzDN );
624 ch_free( authcDN.bv_val );
625 if ( rc != LDAP_SUCCESS ) {
627 LDAP_LOG(( "sasl", LDAP_LEVEL_INFO,
628 "slap_sasl_authorize: conn %ld authorization disallowed (%d)\n",
629 (long)(conn ? conn->c_connid : -1), rc ));
631 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
632 " authorization disallowed (%d)\n",
633 (long) (conn ? conn->c_connid : -1), rc, 0 );
636 sasl_seterror( sconn, 0, "not authorized" );
637 ch_free( authzDN.bv_val );
641 conn->c_sasl_dn = authzDN;
644 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
645 "slap_sasl_authorize: conn %d authorization allowed\n",
646 (long)(conn ? conn->c_connid : -1 ) ));
648 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
649 " authorization allowed\n",
650 (long) (conn ? conn->c_connid : -1), 0, 0 );
663 struct berval authcDN, authzDN;
665 Connection *conn = context;
669 if ( conn->c_sasl_dn.bv_val ) {
670 ch_free( conn->c_sasl_dn.bv_val );
671 conn->c_sasl_dn.bv_val = NULL;
672 conn->c_sasl_dn.bv_len = 0;
676 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
677 "slap_sasl_authorize: conn %d authcid=\"%s\" authzid=\"%s\"\n",
678 conn ? conn->c_connid : -1,
679 authcid ? authcid : "<empty>",
680 authzid ? authzid : "<empty>" ));
682 Debug( LDAP_DEBUG_ARGS, "SASL Authorize [conn=%ld]: "
683 "authcid=\"%s\" authzid=\"%s\"\n",
684 (long) (conn ? conn->c_connid : -1),
685 authcid ? authcid : "<empty>",
686 authzid ? authzid : "<empty>" );
689 /* Figure out how much data we have for the dn */
690 rc = sasl_getprop( conn->c_sasl_context, SASL_REALM, (void **)&realm );
691 if( rc != SASL_OK && rc != SASL_NOTDONE ) {
693 LDAP_LOG(( "sasl", LDAP_LEVEL_ERR,
694 "slap_sasl_authorize: getprop(REALM) failed.\n" ));
696 Debug(LDAP_DEBUG_TRACE,
697 "authorize: getprop(REALM) failed!\n", 0,0,0);
699 *errstr = "Could not extract realm";
703 /* Convert the identities to DN's. If no authzid was given, client will
704 be bound as the DN matching their username */
705 rc = slap_sasl_getdn( conn, (char *)authcid, 0, realm, &authcDN, FLAG_GETDN_AUTHCID );
706 if( rc != LDAP_SUCCESS ) {
707 *errstr = ldap_err2string( rc );
710 if( ( authzid == NULL ) || !strcmp( authcid,authzid ) ) {
712 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
713 "slap_sasl_authorize: conn %d Using authcDN=%s\n",
714 conn ? conn->c_connid : -1, authcDN.bv_val ));
716 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
717 "Using authcDN=%s\n", (long) (conn ? conn->c_connid : -1), authcDN.bv_val,0 );
720 conn->c_sasl_dn = authcDN;
724 rc = slap_sasl_getdn( conn, (char *)authzid, 0, realm, &authzDN, FLAG_GETDN_AUTHZID );
725 if( rc != LDAP_SUCCESS ) {
726 ch_free( authcDN.bv_val );
727 *errstr = ldap_err2string( rc );
731 rc = slap_sasl_authorized( &authcDN, &authzDN );
732 ch_free( authcDN.bv_val );
735 LDAP_LOG(( "sasl", LDAP_LEVEL_INFO,
736 "slap_sasl_authorize: conn %ld authorization disallowed (%d)\n",
737 (long)(conn ? conn->c_connid : -1), rc ));
739 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
740 " authorization disallowed (%d)\n",
741 (long) (conn ? conn->c_connid : -1), rc, 0 );
744 *errstr = "not authorized";
745 ch_free( authzDN.bv_val );
750 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
751 "slap_sasl_authorize: conn %d authorization allowed\n",
752 (long)(conn ? conn->c_connid : -1 ) ));
754 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
755 " authorization allowed\n",
756 (long) (conn ? conn->c_connid : -1), 0, 0 );
759 conn->c_sasl_dn = authzDN;
763 #endif /* SASL_VERSION_MAJOR >= 2 */
766 slap_sasl_err2ldap( int saslerr )
772 rc = LDAP_SASL_BIND_IN_PROGRESS;
781 rc = LDAP_AUTH_METHOD_NOT_SUPPORTED;
784 rc = LDAP_INVALID_CREDENTIALS;
787 rc = LDAP_INSUFFICIENT_ACCESS;
791 rc = LDAP_INAPPROPRIATE_AUTH;
803 int slap_sasl_init( void )
805 #ifdef HAVE_CYRUS_SASL
807 static sasl_callback_t server_callbacks[] = {
808 { SASL_CB_LOG, &slap_sasl_log, NULL },
809 { SASL_CB_LIST_END, NULL, NULL }
819 ldap_pvt_sasl_mutex_new,
820 ldap_pvt_sasl_mutex_lock,
821 ldap_pvt_sasl_mutex_unlock,
822 ldap_pvt_sasl_mutex_dispose );
824 #if SASL_VERSION_MAJOR >= 2
825 sasl_auxprop_add_plugin( "slapd", slap_auxprop_init );
827 /* should provide callbacks for logging */
828 /* server name should be configurable */
829 rc = sasl_server_init( server_callbacks, "slapd" );
831 if( rc != SASL_OK ) {
833 LDAP_LOG(( "sasl", LDAP_LEVEL_INFO,
834 "slap_sasl_init: init failed.\n" ));
836 Debug( LDAP_DEBUG_ANY, "sasl_server_init failed\n",
844 LDAP_LOG(( "sasl", LDAP_LEVEL_INFO,
845 "slap_sasl_init: initialized!\n"));
847 Debug( LDAP_DEBUG_TRACE, "slap_sasl_init: initialized!\n",
852 /* default security properties */
853 memset( &sasl_secprops, '\0', sizeof(sasl_secprops) );
854 sasl_secprops.max_ssf = INT_MAX;
855 sasl_secprops.maxbufsize = 65536;
856 sasl_secprops.security_flags = SASL_SEC_NOPLAINTEXT|SASL_SEC_NOANONYMOUS;
862 int slap_sasl_destroy( void )
864 #ifdef HAVE_CYRUS_SASL
873 int slap_sasl_open( Connection *conn )
875 int cb, sc = LDAP_SUCCESS;
876 #if SASL_VERSION_MAJOR >= 2
877 char *ipremoteport = NULL, *iplocalport = NULL;
880 #ifdef HAVE_CYRUS_SASL
881 sasl_conn_t *ctx = NULL;
882 sasl_callback_t *session_callbacks;
884 assert( conn->c_sasl_context == NULL );
885 assert( conn->c_sasl_extra == NULL );
887 conn->c_sasl_layers = 0;
890 #if SASL_VERSION_MAJOR >= 2
891 ch_calloc( 5, sizeof(sasl_callback_t));
893 ch_calloc( 3, sizeof(sasl_callback_t));
895 conn->c_sasl_extra = session_callbacks;
897 session_callbacks[cb=0].id = SASL_CB_LOG;
898 session_callbacks[cb].proc = &slap_sasl_log;
899 session_callbacks[cb++].context = conn;
901 session_callbacks[cb].id = SASL_CB_PROXY_POLICY;
902 session_callbacks[cb].proc = &slap_sasl_authorize;
903 session_callbacks[cb++].context = conn;
905 #if SASL_VERSION_MAJOR >= 2
906 session_callbacks[cb].id = SASL_CB_CANON_USER;
907 session_callbacks[cb].proc = &slap_sasl_canonicalize;
908 session_callbacks[cb++].context = conn;
910 /* XXXX: this should be conditional */
911 session_callbacks[cb].id = SASL_CB_SERVER_USERDB_CHECKPASS;
912 session_callbacks[cb].proc = &slap_sasl_checkpass;
913 session_callbacks[cb++].context = conn;
916 session_callbacks[cb].id = SASL_CB_LIST_END;
917 session_callbacks[cb].proc = NULL;
918 session_callbacks[cb++].context = NULL;
920 if( global_host == NULL ) {
921 global_host = ldap_pvt_get_fqdn( NULL );
924 /* create new SASL context */
925 #if SASL_VERSION_MAJOR >= 2
926 if ( conn->c_sock_name.bv_len != 0 &&
927 strncmp( conn->c_sock_name.bv_val, "IP=", 3 ) == 0) {
930 iplocalport = ch_strdup( conn->c_sock_name.bv_val + 3 );
931 /* Convert IPv6 addresses to address;port syntax. */
932 p = strrchr( iplocalport, ' ' );
933 /* Convert IPv4 addresses to address;port syntax. */
934 if ( p == NULL ) p = strchr( iplocalport, ':' );
939 if ( conn->c_peer_name.bv_len != 0 &&
940 strncmp( conn->c_peer_name.bv_val, "IP=", 3 ) == 0) {
943 ipremoteport = ch_strdup( conn->c_peer_name.bv_val + 3 );
944 /* Convert IPv6 addresses to address;port syntax. */
945 p = strrchr( ipremoteport, ' ' );
946 /* Convert IPv4 addresses to address;port syntax. */
947 if ( p == NULL ) p = strchr( ipremoteport, ':' );
952 sc = sasl_server_new( "ldap", global_host, global_realm,
953 iplocalport, ipremoteport, session_callbacks, 0, &ctx );
954 if ( iplocalport != NULL ) {
955 ch_free( iplocalport );
957 if ( ipremoteport != NULL ) {
958 ch_free( ipremoteport );
961 sc = sasl_server_new( "ldap", global_host, global_realm,
962 session_callbacks, SASL_SECURITY_LAYER, &ctx );
965 if( sc != SASL_OK ) {
967 LDAP_LOG(( "sasl", LDAP_LEVEL_ERR,
968 "slap_sasl_open: sasl_server_new failed: %d\n", sc ));
970 Debug( LDAP_DEBUG_ANY, "sasl_server_new failed: %d\n",
977 conn->c_sasl_context = ctx;
979 if( sc == SASL_OK ) {
980 sc = sasl_setprop( ctx,
981 SASL_SEC_PROPS, &sasl_secprops );
983 if( sc != SASL_OK ) {
985 LDAP_LOG(( "sasl", LDAP_LEVEL_ERR,
986 "slap_sasl_open: sasl_setprop failed: %d \n", sc ));
988 Debug( LDAP_DEBUG_ANY, "sasl_setprop failed: %d\n",
992 slap_sasl_close( conn );
997 sc = slap_sasl_err2ldap( sc );
1002 int slap_sasl_external(
1005 const char *auth_id )
1007 #if SASL_VERSION_MAJOR >= 2
1009 sasl_conn_t *ctx = conn->c_sasl_context;
1011 if ( ctx == NULL ) {
1012 return LDAP_UNAVAILABLE;
1015 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL, &ssf );
1017 if ( sc != SASL_OK ) {
1021 sc = sasl_setprop( ctx, SASL_AUTH_EXTERNAL, auth_id );
1023 if ( sc != SASL_OK ) {
1027 #elif defined(HAVE_CYRUS_SASL)
1029 sasl_conn_t *ctx = conn->c_sasl_context;
1030 sasl_external_properties_t extprops;
1032 if ( ctx == NULL ) {
1033 return LDAP_UNAVAILABLE;
1036 memset( &extprops, '\0', sizeof(extprops) );
1038 extprops.auth_id = (char *) auth_id;
1040 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL,
1041 (void *) &extprops );
1043 if ( sc != SASL_OK ) {
1048 return LDAP_SUCCESS;
1051 int slap_sasl_reset( Connection *conn )
1053 #ifdef HAVE_CYRUS_SASL
1054 sasl_conn_t *ctx = conn->c_sasl_context;
1059 /* must return "anonymous" */
1060 return LDAP_SUCCESS;
1063 char ** slap_sasl_mechs( Connection *conn )
1065 char **mechs = NULL;
1067 #ifdef HAVE_CYRUS_SASL
1068 sasl_conn_t *ctx = conn->c_sasl_context;
1072 SASL_CONST char *mechstr;
1074 sc = sasl_listmech( ctx,
1075 NULL, NULL, ",", NULL,
1076 &mechstr, NULL, NULL );
1078 if( sc != SASL_OK ) {
1080 LDAP_LOG(( "sasl", LDAP_LEVEL_ERR,
1081 "slap_sasl_mechs: sasl_listmech failed: %d\n", sc ));
1083 Debug( LDAP_DEBUG_ANY, "slap_sasl_listmech failed: %d\n",
1090 mechs = str2charray( mechstr, "," );
1092 #if SASL_VERSION_MAJOR < 2
1101 int slap_sasl_close( Connection *conn )
1103 #ifdef HAVE_CYRUS_SASL
1104 sasl_conn_t *ctx = conn->c_sasl_context;
1107 sasl_dispose( &ctx );
1110 conn->c_sasl_context = NULL;
1112 free( conn->c_sasl_extra );
1113 conn->c_sasl_extra = NULL;
1116 return LDAP_SUCCESS;
1124 struct berval *cred,
1130 #ifdef HAVE_CYRUS_SASL
1131 sasl_conn_t *ctx = conn->c_sasl_context;
1132 struct berval response;
1133 unsigned reslen = 0;
1134 const char *errstr = NULL;
1138 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
1139 "sasl_bind: conn %ld dn=\"%s\" mech=%s datalen=%ld\n",
1141 dn->bv_len ? dn->bv_val : "",
1142 conn->c_sasl_bind_in_progress ? "<continuing>" : conn->c_sasl_bind_mech.bv_val,
1143 cred ? cred->bv_len : 0 ));
1145 Debug(LDAP_DEBUG_ARGS,
1146 "==> sasl_bind: dn=\"%s\" mech=%s datalen=%ld\n",
1147 dn->bv_len ? dn->bv_val : "",
1148 conn->c_sasl_bind_in_progress ? "<continuing>":conn->c_sasl_bind_mech.bv_val,
1149 cred ? cred->bv_len : 0 );
1154 send_ldap_result( conn, op, LDAP_UNAVAILABLE,
1155 NULL, "SASL unavailable on this session", NULL, NULL );
1159 #if SASL_VERSION_MAJOR >= 2
1160 #define START( ctx, mech, cred, clen, resp, rlen, err ) \
1161 sasl_server_start( ctx, mech, cred, clen, resp, rlen )
1162 #define STEP( ctx, cred, clen, resp, rlen, err ) \
1163 sasl_server_step( ctx, cred, clen, resp, rlen )
1165 #define START( ctx, mech, cred, clen, resp, rlen, err ) \
1166 sasl_server_start( ctx, mech, cred, clen, resp, rlen, err )
1167 #define STEP( ctx, cred, clen, resp, rlen, err ) \
1168 sasl_server_step( ctx, cred, clen, resp, rlen, err )
1171 if ( !conn->c_sasl_bind_in_progress ) {
1173 conn->c_sasl_bind_mech.bv_val,
1174 cred->bv_len ? cred->bv_val : "",
1176 (SASL_CONST char **)&response.bv_val, &reslen, &errstr );
1180 cred->bv_val, cred->bv_len,
1181 (SASL_CONST char **)&response.bv_val, &reslen, &errstr );
1184 response.bv_len = reslen;
1186 if ( sc == SASL_OK ) {
1187 sasl_ssf_t *ssf = NULL;
1189 *edn = conn->c_sasl_dn;
1190 conn->c_sasl_dn.bv_val = NULL;
1191 conn->c_sasl_dn.bv_len = 0;
1195 (void) sasl_getprop( ctx, SASL_SSF, (void *)&ssf );
1196 *ssfp = ssf ? *ssf : 0;
1199 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1200 conn->c_sasl_layers++;
1201 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1204 send_ldap_sasl( conn, op, rc,
1205 NULL, NULL, NULL, NULL,
1206 response.bv_len ? &response : NULL );
1208 } else if ( sc == SASL_CONTINUE ) {
1209 send_ldap_sasl( conn, op, rc = LDAP_SASL_BIND_IN_PROGRESS,
1210 NULL, NULL, NULL, NULL, &response );
1213 #if SASL_VERSION_MAJOR >= 2
1214 errstr = sasl_errdetail( ctx );
1216 send_ldap_result( conn, op, rc = slap_sasl_err2ldap( sc ),
1217 NULL, errstr, NULL, NULL );
1220 #if SASL_VERSION_MAJOR < 2
1221 if( response.bv_len ) {
1222 ch_free( response.bv_val );
1227 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
1228 "slap_sasl_bind: rc=%d\n", rc ));
1230 Debug(LDAP_DEBUG_TRACE, "<== slap_sasl_bind: rc=%d\n", rc, 0, 0);
1235 send_ldap_result( conn, op, rc = LDAP_UNAVAILABLE,
1236 NULL, "SASL not supported", NULL, NULL );
1242 char* slap_sasl_secprops( const char *in )
1244 #ifdef HAVE_CYRUS_SASL
1245 int rc = ldap_pvt_sasl_secprops( in, &sasl_secprops );
1247 return rc == LDAP_SUCCESS ? NULL : "Invalid security properties";
1249 return "SASL not supported";
1253 #ifdef HAVE_CYRUS_SASL
1259 struct berval *reqdata,
1261 struct berval **rspdata,
1262 LDAPControl *** rspctrls,
1266 struct berval id = { 0, NULL }; /* needs to come from connection */
1267 struct berval new = { 0, NULL };
1268 struct berval old = { 0, NULL };
1270 assert( reqoid != NULL );
1271 assert( strcmp( LDAP_EXOP_MODIFY_PASSWD, reqoid ) == 0 );
1273 rc = sasl_getprop( conn->c_sasl_context, SASL_USERNAME,
1274 (SASL_CONST void **)&id.bv_val );
1276 if( rc != SASL_OK ) {
1277 *text = "unable to retrieve SASL username";
1283 LDAP_LOG(( "backend", LDAP_LEVEL_ENTRY,
1284 "slap_sasl_setpass: \"%s\"\n",
1285 id.bv_val ? id.bv_val : "" ));
1287 Debug( LDAP_DEBUG_ARGS, "==> ldbm_back_exop_passwd: \"%s\"\n",
1288 id.bv_val ? id.bv_val : "", 0, 0 );
1291 rc = slap_passwd_parse( reqdata,
1292 NULL, &old, &new, text );
1294 if( rc != LDAP_SUCCESS ) {
1298 if( new.bv_len == 0 ) {
1299 slap_passwd_generate(&new);
1301 if( new.bv_len == 0 ) {
1302 *text = "password generation failed.";
1307 *rspdata = slap_passwd_return( &new );
1310 #if SASL_VERSION_MAJOR < 2
1311 rc = sasl_setpass( conn->c_sasl_context,
1312 id.bv_val, new.bv_val, new.bv_len, 0, text );
1314 rc = sasl_setpass( conn->c_sasl_context, id.bv_val,
1315 old.bv_val, old.bv_len, new.bv_val, new.bv_len, 0 );
1316 if( rc != SASL_OK ) {
1317 *text = sasl_errdetail( conn->c_sasl_context );