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;
47 int slap_sasl_config( int cargc, char **cargv, char *line,
48 const char *fname, int lineno )
50 /* set SASL proxy authorization policy */
51 if ( strcasecmp( cargv[0], "sasl-authz-policy" ) == 0 ) {
54 LDAP_LOG( CONFIG, CRIT,
55 "%s: line %d: missing policy in \"sasl-authz-policy <policy>\" line\n",
58 Debug( LDAP_DEBUG_ANY,
59 "%s: line %d: missing policy in \"sasl-authz-policy <policy>\" line\n",
65 if ( slap_sasl_setpolicy( cargv[1] ) ) {
67 LDAP_LOG( CONFIG, CRIT,
68 "%s: line %d: unable "
69 "to parse value \"%s\" "
70 "in \"sasl-authz-policy "
72 fname, lineno, cargv[1] );
74 Debug( LDAP_DEBUG_ANY,
75 "%s: line %d: unable "
76 "to parse value \"%s\" "
77 "in \"sasl-authz-policy "
79 fname, lineno, cargv[1] );
86 } else if ( strcasecmp( cargv[0], "sasl-host" ) == 0 ) {
89 LDAP_LOG( CONFIG, CRIT,
90 "%s: line %d: missing host in \"sasl-host <host>\" line\n",
93 Debug( LDAP_DEBUG_ANY,
94 "%s: line %d: missing host in \"sasl-host <host>\" line\n",
101 if ( global_host != NULL ) {
103 LDAP_LOG( CONFIG, CRIT,
104 "%s: line %d: already set sasl-host!\n",
107 Debug( LDAP_DEBUG_ANY,
108 "%s: line %d: already set sasl-host!\n",
115 global_host = ch_strdup( cargv[1] );
119 } else if ( strcasecmp( cargv[0], "sasl-realm" ) == 0 ) {
122 LDAP_LOG( CONFIG, CRIT,
123 "%s: line %d: missing realm in \"sasl-realm <realm>\" line.\n",
126 Debug( LDAP_DEBUG_ANY,
127 "%s: line %d: missing realm in \"sasl-realm <realm>\" line\n",
134 if ( global_realm != NULL ) {
136 LDAP_LOG( CONFIG, CRIT,
137 "%s: line %d: already set sasl-realm!\n",
140 Debug( LDAP_DEBUG_ANY,
141 "%s: line %d: already set sasl-realm!\n",
148 global_realm = ch_strdup( cargv[1] );
151 } else if ( !strcasecmp( cargv[0], "sasl-regexp" )
152 || !strcasecmp( cargv[0], "saslregexp" ) )
157 LDAP_LOG( CONFIG, CRIT,
158 "%s: line %d: need 2 args in "
159 "\"saslregexp <match> <replace>\"\n",
162 Debug( LDAP_DEBUG_ANY,
163 "%s: line %d: need 2 args in \"saslregexp <match> <replace>\"\n",
169 rc = slap_sasl_regexp_config( cargv[1], cargv[2] );
174 /* SASL security properties */
175 } else if ( strcasecmp( cargv[0], "sasl-secprops" ) == 0 ) {
180 LDAP_LOG( CONFIG, CRIT,
181 "%s: line %d: missing flags in "
182 "\"sasl-secprops <properties>\" line\n",
185 Debug( LDAP_DEBUG_ANY,
186 "%s: line %d: missing flags in \"sasl-secprops <properties>\" line\n",
193 txt = slap_sasl_secprops( cargv[1] );
196 LDAP_LOG( CONFIG, CRIT,
197 "%s: line %d sasl-secprops: %s\n",
198 fname, lineno, txt );
200 Debug( LDAP_DEBUG_ANY,
201 "%s: line %d: sasl-secprops: %s\n",
202 fname, lineno, txt );
218 Connection *conn = context;
222 if ( message == NULL ) {
223 return SASL_BADPARAM;
227 #if SASL_VERSION_MAJOR >= 2
229 level = LDAP_DEBUG_NONE;
233 level = LDAP_DEBUG_ANY;
237 level = LDAP_DEBUG_ANY;
241 level = LDAP_DEBUG_TRACE;
245 level = LDAP_DEBUG_TRACE;
249 level = LDAP_DEBUG_TRACE;
253 level = LDAP_DEBUG_TRACE;
257 level = LDAP_DEBUG_TRACE;
258 label = "Password Trace";
262 level = LDAP_DEBUG_ANY;
265 case SASL_LOG_WARNING:
266 level = LDAP_DEBUG_TRACE;
270 level = LDAP_DEBUG_TRACE;
275 return SASL_BADPARAM;
279 LDAP_LOG( TRANSPORT, ENTRY,
280 "SASL [conn=%ld] %s: %s\n", conn ? conn->c_connid : -1, label, message);
282 Debug( level, "SASL [conn=%ld] %s: %s\n",
283 conn ? conn->c_connid: -1,
292 /* Take any sort of identity string and return a DN with the "dn:" prefix. The
293 string returned in *dn is in its own allocated memory, and must be free'd
294 by the calling process.
295 -Mark Adamson, Carnegie Mellon
297 The "dn:" prefix is no longer used anywhere inside slapd. It is only used
298 on strings passed in directly from SASL.
299 -Howard Chu, Symas Corp.
305 static struct berval ext_bv = { sizeof("EXTERNAL")-1, "EXTERNAL" };
307 int slap_sasl_getdn( Connection *conn, char *id, int len,
308 char *user_realm, struct berval *dn, int flags )
311 int rc, is_dn = 0, do_norm = 1;
316 LDAP_LOG( TRANSPORT, ENTRY,
317 "slap_sasl_getdn: conn %d id=%s\n",
318 conn ? conn->c_connid : -1, id ? (*id ? id : "<empty>") : "NULL", 0 );
320 Debug( LDAP_DEBUG_ARGS, "slap_sasl_getdn: id=%s\n",
321 id?(*id?id:"<empty>"):"NULL",0,0 );
328 if ( len == 0 ) len = strlen( id );
330 /* Blatantly anonymous ID */
331 if ( len == sizeof("anonymous") - 1 &&
332 !strcasecmp( id, "anonymous" ) ) {
333 return( LDAP_SUCCESS );
339 ctx = conn->c_sasl_context;
341 /* An authcID needs to be converted to authzID form. Set the
342 * values directly into *dn; they will be normalized later. (and
343 * normalizing always makes a new copy.) An ID from a TLS certificate
344 * is already normalized, so copy it and skip normalization.
346 if( flags & FLAG_GETDN_AUTHCID ) {
348 if( conn->c_is_tls && conn->c_sasl_bind_mech.bv_len == ext_bv.bv_len
349 && ( strcasecmp( ext_bv.bv_val, conn->c_sasl_bind_mech.bv_val ) == 0 ) ) {
350 /* X.509 DN is already normalized */
353 ber_str2bv( id, len, 1, dn );
358 /* convert to u:<username> form */
365 if( !strncasecmp( id, "u:", sizeof("u:")-1 )) {
369 } else if ( !strncasecmp( id, "dn:", sizeof("dn:")-1) ) {
376 /* No other possibilities from here */
380 return( LDAP_INAPPROPRIATE_AUTH );
383 /* Username strings */
384 if( is_dn == SET_U ) {
386 len = dn->bv_len + sizeof("uid=")-1 + sizeof(",cn=auth")-1;
388 /* username may have embedded realm name */
389 if( realm = strchr( dn->bv_val, '@') ) {
391 len += sizeof(",cn=")-2;
392 } else if( user_realm && *user_realm ) {
393 len += strlen( user_realm ) + sizeof(",cn=")-1;
396 if( conn->c_sasl_bind_mech.bv_len ) {
397 len += conn->c_sasl_bind_mech.bv_len + sizeof(",cn=")-1;
400 /* Build the new dn */
402 dn->bv_val = ch_malloc( len+1 );
403 p = lutil_strcopy( dn->bv_val, "uid=" );
404 p = lutil_strncopy( p, c1, dn->bv_len );
407 int rlen = dn->bv_len - ( realm - c1 );
408 p = lutil_strcopy( p, ",cn=" );
409 p = lutil_strncopy( p, realm, rlen );
411 } else if( user_realm && *user_realm ) {
412 p = lutil_strcopy( p, ",cn=" );
413 p = lutil_strcopy( p, user_realm );
416 if( conn->c_sasl_bind_mech.bv_len ) {
417 p = lutil_strcopy( p, ",cn=" );
418 p = lutil_strcopy( p, conn->c_sasl_bind_mech.bv_val );
420 p = lutil_strcopy( p, ",cn=auth" );
421 dn->bv_len = p - dn->bv_val;
424 LDAP_LOG( TRANSPORT, ENTRY,
425 "slap_sasl_getdn: u:id converted to %s.\n", dn->bv_val, 0, 0 );
427 Debug( LDAP_DEBUG_TRACE, "getdn: u:id converted to %s\n", dn->bv_val,0,0 );
431 /* All strings are in DN form now. Normalize if needed. */
433 rc = dnNormalize2( NULL, dn, &dn2 );
435 /* User DNs were constructed above and must be freed now */
436 if ( is_dn == SET_U )
437 ch_free( dn->bv_val );
439 if ( rc != LDAP_SUCCESS ) {
447 /* Run thru regexp */
448 slap_sasl2dn( conn, dn, &dn2 );
450 ch_free( dn->bv_val );
453 LDAP_LOG( TRANSPORT, ENTRY,
454 "slap_sasl_getdn: dn:id converted to %s.\n", dn->bv_val, 0, 0 );
456 Debug( LDAP_DEBUG_TRACE, "getdn: dn:id converted to %s\n",
461 return( LDAP_SUCCESS );
464 #if SASL_VERSION_MAJOR >= 2
465 static const char *slap_propnames[] = { "*authcDN", "*authzDN", NULL };
470 sasl_server_params_t *sparams,
477 const struct propval *list;
479 AttributeDescription *ad;
482 list = sparams->utils->prop_get( sparams->propctx );
484 /* Find our DN first */
485 for( i = 0, last = 0; list[i].name; i++ ) {
486 if ( list[i].name[0] == '*' ) {
487 if ( (flags & SASL_AUXPROP_AUTHZID) &&
488 !strcmp( list[i].name, slap_propnames[1] ) ) {
489 if ( list[i].values && list[i].values[0] )
490 AC_MEMCPY( &dn, list[i].values[0], sizeof( dn ) );
491 if ( !last ) last = i;
494 if ( !strcmp( list[i].name, slap_propnames[0] ) ) {
495 if ( !last ) last = i;
496 if ( list[i].values && list[i].values[0] ) {
497 AC_MEMCPY( &dn, list[i].values[0], sizeof( dn ) );
498 if ( !(flags & SASL_AUXPROP_AUTHZID) )
505 /* Now fetch the rest */
506 for( i = 0; i < last; i++ ) {
507 const char *name = list[i].name;
509 if ( name[0] == '*' ) {
510 if ( flags & SASL_AUXPROP_AUTHZID ) continue;
512 } else if ( !(flags & SASL_AUXPROP_AUTHZID ) )
515 if ( list[i].values ) {
516 if ( !(flags & SASL_AUXPROP_OVERRIDE) ) continue;
517 sparams->utils->prop_erase( sparams->propctx, list[i].name );
520 rc = slap_str2ad( name, &ad, &text );
521 if ( rc != LDAP_SUCCESS ) {
523 LDAP_LOG( TRANSPORT, DETAIL1,
524 "slap_auxprop: str2ad(%s): %s\n", name, text, 0 );
526 Debug( LDAP_DEBUG_TRACE,
527 "slap_auxprop: str2ad(%s): %s\n", name, text, 0 );
529 rc = slap_str2undef_ad( name, &ad, &text );
530 if ( rc != LDAP_SUCCESS ) continue;
532 rc = backend_attribute( NULL,NULL,NULL,NULL, &dn, ad, &vals );
533 if ( rc != LDAP_SUCCESS ) continue;
534 for ( bv = vals; bv->bv_val; bv++ ) {
535 sparams->utils->prop_set( sparams->propctx, list[i].name,
536 bv->bv_val, bv->bv_len );
538 ber_bvarray_free( vals );
542 static sasl_auxprop_plug_t slap_auxprop_plugin = {
545 NULL, /* glob_context */
546 NULL, /* auxprop_free */
554 const sasl_utils_t *utils,
557 sasl_auxprop_plug_t **plug,
558 const char *plugname)
560 if ( !out_version | !plug ) return SASL_BADPARAM;
562 if ( max_version < SASL_AUXPROP_PLUG_VERSION ) return SASL_BADVERS;
564 *out_version = SASL_AUXPROP_PLUG_VERSION;
565 *plug = &slap_auxprop_plugin;
573 const char *username,
576 struct propctx *propctx)
578 Connection *conn = (Connection *)context;
579 struct berval dn, cred;
583 cred.bv_val = (char *)pass;
584 cred.bv_len = passlen;
586 /* SASL will fallback to its own mechanisms if we don't
587 * find an answer here.
590 rc = slap_sasl_getdn( conn, (char *)username, 0, NULL, &dn,
591 FLAG_GETDN_AUTHCID );
592 if ( rc != LDAP_SUCCESS ) {
593 sasl_seterror( sconn, 0, ldap_err2string( rc ) );
597 if ( dn.bv_len == 0 ) {
598 sasl_seterror( sconn, 0,
599 "No password is associated with the Root DSE" );
600 if ( dn.bv_val != NULL ) {
601 ch_free( dn.bv_val );
606 rc = backend_attribute( NULL, NULL, NULL, NULL, &dn,
607 slap_schema.si_ad_userPassword, &vals);
608 if ( rc != LDAP_SUCCESS ) {
609 ch_free( dn.bv_val );
610 sasl_seterror( sconn, 0, ldap_err2string( rc ) );
611 return SASL_NOVERIFY;
616 if ( vals != NULL ) {
617 for ( bv = vals; bv->bv_val != NULL; bv++ ) {
618 if ( !lutil_passwd( bv, &cred, NULL ) ) {
623 ber_bvarray_free( vals );
626 if ( rc != SASL_OK ) {
627 sasl_seterror( sconn, 0,
628 ldap_err2string( LDAP_INVALID_CREDENTIALS ) );
631 ch_free( dn.bv_val );
636 /* Convert a SASL authcid or authzid into a DN. Store the DN in an
637 * auxiliary property, so that we can refer to it in sasl_authorize
638 * without interfering with anything else. Also, the SASL username
639 * buffer is constrained to 256 characters, and our DNs could be
640 * much longer (totally arbitrary length)...
643 slap_sasl_canonicalize(
649 const char *user_realm,
654 Connection *conn = (Connection *)context;
655 struct propctx *props = sasl_auxprop_getctx( sconn );
656 struct propval auxvals[3];
659 const char *names[2];
664 LDAP_LOG( TRANSPORT, ENTRY,
665 "slap_sasl_canonicalize: conn %d %s=\"%s\"\n",
666 conn ? conn->c_connid : -1,
667 (flags & SASL_CU_AUTHID) ? "authcid" : "authzid", in ? in : "<empty>");
669 Debug( LDAP_DEBUG_ARGS, "SASL Canonicalize [conn=%ld]: "
671 conn ? conn->c_connid : -1,
672 (flags & SASL_CU_AUTHID) ? "authcid" : "authzid",
673 in ? in : "<empty>" );
676 /* If name is too big, just truncate. We don't care, we're
677 * using DNs, not the usernames.
679 if ( inlen > out_max )
682 /* See if we need to add request, can only do it once */
683 prop_getnames( props, slap_propnames, auxvals );
684 if ( !auxvals[0].name )
685 prop_request( props, slap_propnames );
687 if ( flags & SASL_CU_AUTHID )
692 /* Already been here? */
693 if ( auxvals[which].values )
696 if ( flags == SASL_CU_AUTHZID ) {
697 /* If we got unqualified authzid's, they probably came from SASL
698 * itself just passing the authcid to us. Look inside the oparams
699 * structure to see if that's true. (HACK: the out_len pointer is
700 * the address of a member of a sasl_out_params_t structure...)
702 sasl_out_params_t dummy;
703 int offset = (void *)&dummy.ulen - (void *)&dummy.authid;
704 char **authid = (void *)out_len - offset;
705 if ( *authid && !strcmp( in, *authid ) )
709 rc = slap_sasl_getdn( conn, (char *)in, inlen, (char *)user_realm, &dn,
710 (flags & SASL_CU_AUTHID) ? FLAG_GETDN_AUTHCID : FLAG_GETDN_AUTHZID );
711 if ( rc != LDAP_SUCCESS ) {
712 sasl_seterror( sconn, 0, ldap_err2string( rc ) );
716 names[0] = slap_propnames[which];
719 prop_set( props, names[0], (char *)&dn, sizeof( dn ) );
722 LDAP_LOG( TRANSPORT, ENTRY,
723 "slap_sasl_canonicalize: conn %d %s=\"%s\"\n",
724 conn ? conn->c_connid : -1, names[0]+1, dn.bv_val );
726 Debug( LDAP_DEBUG_ARGS, "SASL Canonicalize [conn=%ld]: "
728 conn ? conn->c_connid : -1,
729 names[0]+1, dn.bv_val );
731 done: AC_MEMCPY( out, in, inlen );
743 char *requested_user,
747 const char *def_realm,
749 struct propctx *props)
751 Connection *conn = (Connection *)context;
752 struct propval auxvals[3];
753 struct berval authcDN, authzDN;
757 LDAP_LOG( TRANSPORT, ENTRY,
758 "slap_sasl_authorize: conn %d authcid=\"%s\" authzid=\"%s\"\n",
759 conn ? conn->c_connid : -1, auth_identity, requested_user);
761 Debug( LDAP_DEBUG_ARGS, "SASL Authorize [conn=%ld]: "
762 "authcid=\"%s\" authzid=\"%s\"\n",
763 conn ? conn->c_connid : -1, auth_identity, requested_user );
765 if ( conn->c_sasl_dn.bv_val ) {
766 ch_free( conn->c_sasl_dn.bv_val );
767 conn->c_sasl_dn.bv_val = NULL;
768 conn->c_sasl_dn.bv_len = 0;
771 prop_getnames( props, slap_propnames, auxvals );
773 AC_MEMCPY( &authcDN, auxvals[0].values[0], sizeof(authcDN) );
775 /* Nothing to do if no authzID was given */
776 if ( !auxvals[1].name || !auxvals[1].values ) {
777 conn->c_sasl_dn = authcDN;
781 AC_MEMCPY( &authzDN, auxvals[1].values[0], sizeof(authzDN) );
783 rc = slap_sasl_authorized( conn, &authcDN, &authzDN );
784 ch_free( authcDN.bv_val );
785 if ( rc != LDAP_SUCCESS ) {
787 LDAP_LOG( TRANSPORT, INFO,
788 "slap_sasl_authorize: conn %ld authorization disallowed (%d)\n",
789 (long)(conn ? conn->c_connid : -1), rc, 0 );
791 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
792 " authorization disallowed (%d)\n",
793 (long) (conn ? conn->c_connid : -1), rc, 0 );
796 sasl_seterror( sconn, 0, "not authorized" );
797 ch_free( authzDN.bv_val );
801 conn->c_sasl_dn = authzDN;
804 LDAP_LOG( TRANSPORT, ENTRY,
805 "slap_sasl_authorize: conn %d authorization allowed\n",
806 (long)(conn ? conn->c_connid : -1), 0, 0 );
808 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
809 " authorization allowed\n",
810 (long) (conn ? conn->c_connid : -1), 0, 0 );
823 struct berval authcDN, authzDN;
825 Connection *conn = context;
829 if ( conn->c_sasl_dn.bv_val ) {
830 ch_free( conn->c_sasl_dn.bv_val );
831 conn->c_sasl_dn.bv_val = NULL;
832 conn->c_sasl_dn.bv_len = 0;
836 LDAP_LOG( TRANSPORT, ENTRY,
837 "slap_sasl_authorize: conn %d authcid=\"%s\" authzid=\"%s\"\n",
838 conn ? conn->c_connid : -1, authcid ? authcid : "<empty>",
839 authzid ? authzid : "<empty>" );
841 Debug( LDAP_DEBUG_ARGS, "SASL Authorize [conn=%ld]: "
842 "authcid=\"%s\" authzid=\"%s\"\n",
843 (long) (conn ? conn->c_connid : -1),
844 authcid ? authcid : "<empty>",
845 authzid ? authzid : "<empty>" );
848 /* Figure out how much data we have for the dn */
849 rc = sasl_getprop( conn->c_sasl_context, SASL_REALM, (void **)&realm );
850 if( rc != SASL_OK && rc != SASL_NOTDONE ) {
852 LDAP_LOG( TRANSPORT, ERR,
853 "slap_sasl_authorize: getprop(REALM) failed.\n", 0, 0, 0 );
855 Debug(LDAP_DEBUG_TRACE,
856 "authorize: getprop(REALM) failed!\n", 0,0,0);
858 *errstr = "Could not extract realm";
862 /* Convert the identities to DN's. If no authzid was given, client will
863 be bound as the DN matching their username */
864 rc = slap_sasl_getdn( conn, (char *)authcid, 0, realm, &authcDN, FLAG_GETDN_AUTHCID );
865 if( rc != LDAP_SUCCESS ) {
866 *errstr = ldap_err2string( rc );
869 if( ( authzid == NULL ) || !strcmp( authcid,authzid ) ) {
871 LDAP_LOG( TRANSPORT, ENTRY,
872 "slap_sasl_authorize: conn %d Using authcDN=%s\n",
873 conn ? conn->c_connid : -1, authcDN.bv_val, 0 );
875 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
876 "Using authcDN=%s\n", (long) (conn ? conn->c_connid : -1), authcDN.bv_val,0 );
879 conn->c_sasl_dn = authcDN;
883 rc = slap_sasl_getdn( conn, (char *)authzid, 0, realm, &authzDN, FLAG_GETDN_AUTHZID );
884 if( rc != LDAP_SUCCESS ) {
885 ch_free( authcDN.bv_val );
886 *errstr = ldap_err2string( rc );
890 rc = slap_sasl_authorized(conn, &authcDN, &authzDN );
891 ch_free( authcDN.bv_val );
894 LDAP_LOG( TRANSPORT, INFO,
895 "slap_sasl_authorize: conn %ld authorization disallowed (%d)\n",
896 (long)(conn ? conn->c_connid : -1), rc, 0 );
898 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
899 " authorization disallowed (%d)\n",
900 (long) (conn ? conn->c_connid : -1), rc, 0 );
903 *errstr = "not authorized";
904 ch_free( authzDN.bv_val );
909 LDAP_LOG( TRANSPORT, RESULTS,
910 "slap_sasl_authorize: conn %d authorization allowed\n",
911 (long)(conn ? conn->c_connid : -1 ), 0, 0 );
913 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
914 " authorization allowed\n",
915 (long) (conn ? conn->c_connid : -1), 0, 0 );
918 conn->c_sasl_dn = authzDN;
922 #endif /* SASL_VERSION_MAJOR >= 2 */
925 slap_sasl_err2ldap( int saslerr )
931 rc = LDAP_SASL_BIND_IN_PROGRESS;
940 rc = LDAP_AUTH_METHOD_NOT_SUPPORTED;
943 rc = LDAP_INVALID_CREDENTIALS;
946 rc = LDAP_INSUFFICIENT_ACCESS;
950 rc = LDAP_INAPPROPRIATE_AUTH;
962 int slap_sasl_init( void )
964 #ifdef HAVE_CYRUS_SASL
966 static sasl_callback_t server_callbacks[] = {
967 { SASL_CB_LOG, &slap_sasl_log, NULL },
968 { SASL_CB_LIST_END, NULL, NULL }
978 ldap_pvt_sasl_mutex_new,
979 ldap_pvt_sasl_mutex_lock,
980 ldap_pvt_sasl_mutex_unlock,
981 ldap_pvt_sasl_mutex_dispose );
983 #if SASL_VERSION_MAJOR >= 2
984 sasl_auxprop_add_plugin( "slapd", slap_auxprop_init );
986 /* should provide callbacks for logging */
987 /* server name should be configurable */
988 rc = sasl_server_init( server_callbacks, "slapd" );
990 if( rc != SASL_OK ) {
992 LDAP_LOG( TRANSPORT, INFO, "slap_sasl_init: init failed.\n", 0, 0, 0 );
994 Debug( LDAP_DEBUG_ANY, "sasl_server_init failed\n",
1002 LDAP_LOG( TRANSPORT, INFO, "slap_sasl_init: initialized!\n", 0, 0, 0 );
1004 Debug( LDAP_DEBUG_TRACE, "slap_sasl_init: initialized!\n",
1009 /* default security properties */
1010 memset( &sasl_secprops, '\0', sizeof(sasl_secprops) );
1011 sasl_secprops.max_ssf = INT_MAX;
1012 sasl_secprops.maxbufsize = 65536;
1013 sasl_secprops.security_flags = SASL_SEC_NOPLAINTEXT|SASL_SEC_NOANONYMOUS;
1019 int slap_sasl_destroy( void )
1021 #ifdef HAVE_CYRUS_SASL
1024 free( global_host );
1030 int slap_sasl_open( Connection *conn )
1032 int cb, sc = LDAP_SUCCESS;
1033 #if SASL_VERSION_MAJOR >= 2
1034 char *ipremoteport = NULL, *iplocalport = NULL;
1037 #ifdef HAVE_CYRUS_SASL
1038 sasl_conn_t *ctx = NULL;
1039 sasl_callback_t *session_callbacks;
1041 assert( conn->c_sasl_context == NULL );
1042 assert( conn->c_sasl_extra == NULL );
1044 conn->c_sasl_layers = 0;
1047 #if SASL_VERSION_MAJOR >= 2
1048 ch_calloc( 5, sizeof(sasl_callback_t));
1050 ch_calloc( 3, sizeof(sasl_callback_t));
1052 conn->c_sasl_extra = session_callbacks;
1054 session_callbacks[cb=0].id = SASL_CB_LOG;
1055 session_callbacks[cb].proc = &slap_sasl_log;
1056 session_callbacks[cb++].context = conn;
1058 session_callbacks[cb].id = SASL_CB_PROXY_POLICY;
1059 session_callbacks[cb].proc = &slap_sasl_authorize;
1060 session_callbacks[cb++].context = conn;
1062 #if SASL_VERSION_MAJOR >= 2
1063 session_callbacks[cb].id = SASL_CB_CANON_USER;
1064 session_callbacks[cb].proc = &slap_sasl_canonicalize;
1065 session_callbacks[cb++].context = conn;
1067 /* XXXX: this should be conditional */
1068 session_callbacks[cb].id = SASL_CB_SERVER_USERDB_CHECKPASS;
1069 session_callbacks[cb].proc = &slap_sasl_checkpass;
1070 session_callbacks[cb++].context = conn;
1073 session_callbacks[cb].id = SASL_CB_LIST_END;
1074 session_callbacks[cb].proc = NULL;
1075 session_callbacks[cb++].context = NULL;
1077 if( global_host == NULL ) {
1078 global_host = ldap_pvt_get_fqdn( NULL );
1081 /* create new SASL context */
1082 #if SASL_VERSION_MAJOR >= 2
1083 if ( conn->c_sock_name.bv_len != 0 &&
1084 strncmp( conn->c_sock_name.bv_val, "IP=", 3 ) == 0) {
1087 iplocalport = ch_strdup( conn->c_sock_name.bv_val + 3 );
1088 /* Convert IPv6 addresses to address;port syntax. */
1089 p = strrchr( iplocalport, ' ' );
1090 /* Convert IPv4 addresses to address;port syntax. */
1091 if ( p == NULL ) p = strchr( iplocalport, ':' );
1096 if ( conn->c_peer_name.bv_len != 0 &&
1097 strncmp( conn->c_peer_name.bv_val, "IP=", 3 ) == 0) {
1100 ipremoteport = ch_strdup( conn->c_peer_name.bv_val + 3 );
1101 /* Convert IPv6 addresses to address;port syntax. */
1102 p = strrchr( ipremoteport, ' ' );
1103 /* Convert IPv4 addresses to address;port syntax. */
1104 if ( p == NULL ) p = strchr( ipremoteport, ':' );
1109 sc = sasl_server_new( "ldap", global_host, global_realm,
1110 iplocalport, ipremoteport, session_callbacks, 0, &ctx );
1111 if ( iplocalport != NULL ) {
1112 ch_free( iplocalport );
1114 if ( ipremoteport != NULL ) {
1115 ch_free( ipremoteport );
1118 sc = sasl_server_new( "ldap", global_host, global_realm,
1119 session_callbacks, SASL_SECURITY_LAYER, &ctx );
1122 if( sc != SASL_OK ) {
1124 LDAP_LOG( TRANSPORT, ERR,
1125 "slap_sasl_open: sasl_server_new failed: %d\n", sc, 0, 0 );
1127 Debug( LDAP_DEBUG_ANY, "sasl_server_new failed: %d\n",
1134 conn->c_sasl_context = ctx;
1136 if( sc == SASL_OK ) {
1137 sc = sasl_setprop( ctx,
1138 SASL_SEC_PROPS, &sasl_secprops );
1140 if( sc != SASL_OK ) {
1142 LDAP_LOG( TRANSPORT, ERR,
1143 "slap_sasl_open: sasl_setprop failed: %d \n", sc, 0, 0 );
1145 Debug( LDAP_DEBUG_ANY, "sasl_setprop failed: %d\n",
1149 slap_sasl_close( conn );
1154 sc = slap_sasl_err2ldap( sc );
1159 int slap_sasl_external(
1162 const char *auth_id )
1164 #if SASL_VERSION_MAJOR >= 2
1166 sasl_conn_t *ctx = conn->c_sasl_context;
1168 if ( ctx == NULL ) {
1169 return LDAP_UNAVAILABLE;
1172 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL, &ssf );
1174 if ( sc != SASL_OK ) {
1178 sc = sasl_setprop( ctx, SASL_AUTH_EXTERNAL, auth_id );
1180 if ( sc != SASL_OK ) {
1184 #elif defined(HAVE_CYRUS_SASL)
1186 sasl_conn_t *ctx = conn->c_sasl_context;
1187 sasl_external_properties_t extprops;
1189 if ( ctx == NULL ) {
1190 return LDAP_UNAVAILABLE;
1193 memset( &extprops, '\0', sizeof(extprops) );
1195 extprops.auth_id = (char *) auth_id;
1197 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL,
1198 (void *) &extprops );
1200 if ( sc != SASL_OK ) {
1205 return LDAP_SUCCESS;
1208 int slap_sasl_reset( Connection *conn )
1210 #ifdef HAVE_CYRUS_SASL
1211 sasl_conn_t *ctx = conn->c_sasl_context;
1216 /* must return "anonymous" */
1217 return LDAP_SUCCESS;
1220 char ** slap_sasl_mechs( Connection *conn )
1222 char **mechs = NULL;
1224 #ifdef HAVE_CYRUS_SASL
1225 sasl_conn_t *ctx = conn->c_sasl_context;
1229 SASL_CONST char *mechstr;
1231 sc = sasl_listmech( ctx,
1232 NULL, NULL, ",", NULL,
1233 &mechstr, NULL, NULL );
1235 if( sc != SASL_OK ) {
1237 LDAP_LOG( TRANSPORT, ERR,
1238 "slap_sasl_mechs: sasl_listmech failed: %d\n", sc, 0, 0 );
1240 Debug( LDAP_DEBUG_ANY, "slap_sasl_listmech failed: %d\n",
1247 mechs = str2charray( mechstr, "," );
1249 #if SASL_VERSION_MAJOR < 2
1258 int slap_sasl_close( Connection *conn )
1260 #ifdef HAVE_CYRUS_SASL
1261 sasl_conn_t *ctx = conn->c_sasl_context;
1264 sasl_dispose( &ctx );
1267 conn->c_sasl_context = NULL;
1269 free( conn->c_sasl_extra );
1270 conn->c_sasl_extra = NULL;
1273 return LDAP_SUCCESS;
1281 struct berval *cred,
1287 #ifdef HAVE_CYRUS_SASL
1288 sasl_conn_t *ctx = conn->c_sasl_context;
1289 struct berval response;
1290 unsigned reslen = 0;
1291 const char *errstr = NULL;
1295 LDAP_LOG( TRANSPORT, ENTRY,
1296 "sasl_bind: dn=\"%s\" mech=%s datalen=%ld\n",
1297 dn->bv_len ? dn->bv_val : "",
1298 conn->c_sasl_bind_in_progress ? "<continuing>" :
1299 conn->c_sasl_bind_mech.bv_val,
1300 cred ? cred->bv_len : 0 );
1302 Debug(LDAP_DEBUG_ARGS,
1303 "==> sasl_bind: dn=\"%s\" mech=%s datalen=%ld\n",
1304 dn->bv_len ? dn->bv_val : "",
1305 conn->c_sasl_bind_in_progress ? "<continuing>":conn->c_sasl_bind_mech.bv_val,
1306 cred ? cred->bv_len : 0 );
1311 send_ldap_result( conn, op, LDAP_UNAVAILABLE,
1312 NULL, "SASL unavailable on this session", NULL, NULL );
1316 #if SASL_VERSION_MAJOR >= 2
1317 #define START( ctx, mech, cred, clen, resp, rlen, err ) \
1318 sasl_server_start( ctx, mech, cred, clen, resp, rlen )
1319 #define STEP( ctx, cred, clen, resp, rlen, err ) \
1320 sasl_server_step( ctx, cred, clen, resp, rlen )
1322 #define START( ctx, mech, cred, clen, resp, rlen, err ) \
1323 sasl_server_start( ctx, mech, cred, clen, resp, rlen, err )
1324 #define STEP( ctx, cred, clen, resp, rlen, err ) \
1325 sasl_server_step( ctx, cred, clen, resp, rlen, err )
1328 if ( !conn->c_sasl_bind_in_progress ) {
1330 conn->c_sasl_bind_mech.bv_val,
1331 cred->bv_len ? cred->bv_val : "",
1333 (SASL_CONST char **)&response.bv_val, &reslen, &errstr );
1337 cred->bv_val, cred->bv_len,
1338 (SASL_CONST char **)&response.bv_val, &reslen, &errstr );
1341 response.bv_len = reslen;
1343 if ( sc == SASL_OK ) {
1344 sasl_ssf_t *ssf = NULL;
1346 *edn = conn->c_sasl_dn;
1347 conn->c_sasl_dn.bv_val = NULL;
1348 conn->c_sasl_dn.bv_len = 0;
1352 (void) sasl_getprop( ctx, SASL_SSF, (void *)&ssf );
1353 *ssfp = ssf ? *ssf : 0;
1356 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1357 conn->c_sasl_layers++;
1358 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1361 send_ldap_sasl( conn, op, rc,
1362 NULL, NULL, NULL, NULL,
1363 response.bv_len ? &response : NULL );
1365 } else if ( sc == SASL_CONTINUE ) {
1366 send_ldap_sasl( conn, op, rc = LDAP_SASL_BIND_IN_PROGRESS,
1367 NULL, NULL, NULL, NULL, &response );
1370 #if SASL_VERSION_MAJOR >= 2
1371 errstr = sasl_errdetail( ctx );
1373 send_ldap_result( conn, op, rc = slap_sasl_err2ldap( sc ),
1374 NULL, errstr, NULL, NULL );
1377 #if SASL_VERSION_MAJOR < 2
1378 if( response.bv_len ) {
1379 ch_free( response.bv_val );
1384 LDAP_LOG( TRANSPORT, RESULTS, "slap_sasl_bind: rc=%d\n", rc, 0, 0 );
1386 Debug(LDAP_DEBUG_TRACE, "<== slap_sasl_bind: rc=%d\n", rc, 0, 0);
1391 send_ldap_result( conn, op, rc = LDAP_UNAVAILABLE,
1392 NULL, "SASL not supported", NULL, NULL );
1398 char* slap_sasl_secprops( const char *in )
1400 #ifdef HAVE_CYRUS_SASL
1401 int rc = ldap_pvt_sasl_secprops( in, &sasl_secprops );
1403 return rc == LDAP_SUCCESS ? NULL : "Invalid security properties";
1405 return "SASL not supported";
1409 #ifdef HAVE_CYRUS_SASL
1415 struct berval *reqdata,
1417 struct berval **rspdata,
1418 LDAPControl *** rspctrls,
1422 struct berval id = { 0, NULL }; /* needs to come from connection */
1423 struct berval new = { 0, NULL };
1424 struct berval old = { 0, NULL };
1426 assert( reqoid != NULL );
1427 assert( strcmp( LDAP_EXOP_MODIFY_PASSWD, reqoid ) == 0 );
1429 rc = sasl_getprop( conn->c_sasl_context, SASL_USERNAME,
1430 (SASL_CONST void **)&id.bv_val );
1432 if( rc != SASL_OK ) {
1433 *text = "unable to retrieve SASL username";
1439 LDAP_LOG( BACKEND, ENTRY,
1440 "slap_sasl_setpass: \"%s\"\n",
1441 id.bv_val ? id.bv_val : "", 0, 0);
1443 Debug( LDAP_DEBUG_ARGS, "==> slap_sasl_setpass: \"%s\"\n",
1444 id.bv_val ? id.bv_val : "", 0, 0 );
1447 rc = slap_passwd_parse( reqdata,
1448 NULL, &old, &new, text );
1450 if( rc != LDAP_SUCCESS ) {
1454 if( new.bv_len == 0 ) {
1455 slap_passwd_generate(&new);
1457 if( new.bv_len == 0 ) {
1458 *text = "password generation failed.";
1463 *rspdata = slap_passwd_return( &new );
1466 #if SASL_VERSION_MAJOR < 2
1467 rc = sasl_setpass( conn->c_sasl_context,
1468 id.bv_val, new.bv_val, new.bv_len, 0, text );
1470 rc = sasl_setpass( conn->c_sasl_context, id.bv_val,
1471 new.bv_val, new.bv_len, old.bv_val, old.bv_len, 0 );
1472 if( rc != SASL_OK ) {
1473 *text = sasl_errdetail( conn->c_sasl_context );