3 * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
10 #include <ac/stdlib.h>
11 #include <ac/string.h>
20 #ifdef HAVE_CYRUS_SASL
21 # ifdef HAVE_SASL_SASL_H
22 # include <sasl/sasl.h>
27 # if SASL_VERSION_MAJOR >= 2
28 # ifdef HAVE_SASL_SASL_H
29 # include <sasl/saslplug.h>
31 # include <saslplug.h>
33 # define SASL_CONST const
38 static sasl_security_properties_t sasl_secprops;
39 #endif /* HAVE_CYRUS_SASL */
45 int slap_sasl_config( int cargc, char **cargv, char *line,
46 const char *fname, int lineno )
48 /* set SASL proxy authorization policy */
49 if ( strcasecmp( cargv[0], "sasl-authz-policy" ) == 0 ) {
52 LDAP_LOG( CONFIG, CRIT,
53 "%s: line %d: missing policy in"
54 " \"sasl-authz-policy <policy>\" line\n",
57 Debug( LDAP_DEBUG_ANY,
58 "%s: line %d: missing policy in"
59 " \"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] );
84 } else if ( !strcasecmp( cargv[0], "sasl-regexp" )
85 || !strcasecmp( cargv[0], "saslregexp" ) )
90 LDAP_LOG( CONFIG, CRIT,
91 "%s: line %d: need 2 args in "
92 "\"saslregexp <match> <replace>\"\n",
95 Debug( LDAP_DEBUG_ANY,
96 "%s: line %d: need 2 args in "
97 "\"saslregexp <match> <replace>\"\n",
103 rc = slap_sasl_regexp_config( cargv[1], cargv[2] );
108 #ifdef HAVE_CYRUS_SASL
110 } else if ( strcasecmp( cargv[0], "sasl-host" ) == 0 ) {
113 LDAP_LOG( CONFIG, CRIT,
114 "%s: line %d: missing host in \"sasl-host <host>\" line\n",
117 Debug( LDAP_DEBUG_ANY,
118 "%s: line %d: missing host in \"sasl-host <host>\" line\n",
125 if ( global_host != NULL ) {
127 LDAP_LOG( CONFIG, CRIT,
128 "%s: line %d: already set sasl-host!\n",
131 Debug( LDAP_DEBUG_ANY,
132 "%s: line %d: already set sasl-host!\n",
139 global_host = ch_strdup( cargv[1] );
143 } else if ( strcasecmp( cargv[0], "sasl-realm" ) == 0 ) {
146 LDAP_LOG( CONFIG, CRIT, "%s: line %d: "
147 "missing realm in \"sasl-realm <realm>\" line.\n",
150 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
151 "missing realm in \"sasl-realm <realm>\" line.\n",
158 if ( global_realm != NULL ) {
160 LDAP_LOG( CONFIG, CRIT,
161 "%s: line %d: already set sasl-realm!\n",
164 Debug( LDAP_DEBUG_ANY,
165 "%s: line %d: already set sasl-realm!\n",
172 global_realm = ch_strdup( cargv[1] );
175 /* SASL security properties */
176 } else if ( strcasecmp( cargv[0], "sasl-secprops" ) == 0 ) {
181 LDAP_LOG( CONFIG, CRIT, "%s: line %d: "
182 "missing flags in \"sasl-secprops <properties>\" line\n",
185 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
186 "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 );
207 #endif /* HAVE_CYRUS_SASL */
213 #ifdef HAVE_CYRUS_SASL
221 Connection *conn = context;
225 if ( message == NULL ) {
226 return SASL_BADPARAM;
230 #if SASL_VERSION_MAJOR >= 2
232 level = LDAP_DEBUG_NONE;
236 level = LDAP_DEBUG_ANY;
240 level = LDAP_DEBUG_ANY;
244 level = LDAP_DEBUG_TRACE;
248 level = LDAP_DEBUG_TRACE;
252 level = LDAP_DEBUG_TRACE;
256 level = LDAP_DEBUG_TRACE;
260 level = LDAP_DEBUG_TRACE;
261 label = "Password Trace";
265 level = LDAP_DEBUG_ANY;
268 case SASL_LOG_WARNING:
269 level = LDAP_DEBUG_TRACE;
273 level = LDAP_DEBUG_TRACE;
278 return SASL_BADPARAM;
282 LDAP_LOG( TRANSPORT, ENTRY,
283 "SASL [conn=%ld] %s: %s\n", conn ? conn->c_connid : -1, label, message);
285 Debug( level, "SASL [conn=%ld] %s: %s\n",
286 conn ? conn->c_connid: -1,
295 #if SASL_VERSION_MAJOR >= 2
296 static const char *slap_propnames[] = {
297 "*slapConn", "*authcDN", "*authzDN", NULL };
299 static Filter *generic_filter;
300 static struct berval generic_filterstr = BER_BVC("(objectclass=*)");
306 typedef struct lookup_info {
309 const struct propval *list;
310 sasl_server_params_t *sparams;
321 LDAPControl **ctrls )
324 AttributeDescription *ad;
328 slap_callback *tmp = op->o_callback;
329 lookup_info *sl = tmp->sc_private;
331 for( i = 0; i < sl->last; i++ ) {
332 const char *name = sl->list[i].name;
334 if ( name[0] == '*' ) {
335 if ( sl->flags & SASL_AUXPROP_AUTHZID ) continue;
337 } else if ( !(sl->flags & SASL_AUXPROP_AUTHZID ) )
340 if ( sl->list[i].values ) {
341 if ( !(sl->flags & SASL_AUXPROP_OVERRIDE) ) continue;
344 rc = slap_str2ad( name, &ad, &text );
345 if ( rc != LDAP_SUCCESS ) {
347 LDAP_LOG( TRANSPORT, DETAIL1,
348 "slap_auxprop: str2ad(%s): %s\n", name, text, 0 );
350 Debug( LDAP_DEBUG_TRACE,
351 "slap_auxprop: str2ad(%s): %s\n", name, text, 0 );
355 a = attr_find( e->e_attrs, ad );
357 if ( ! access_allowed( be, conn, op, e, ad, NULL, ACL_AUTH, NULL ) ) {
360 if ( sl->list[i].values && ( sl->flags & SASL_AUXPROP_OVERRIDE ) ) {
361 sl->sparams->utils->prop_erase( sl->sparams->propctx,
364 for ( bv = a->a_vals; bv->bv_val; bv++ ) {
365 sl->sparams->utils->prop_set( sl->sparams->propctx,
366 sl->list[i].name, bv->bv_val, bv->bv_len );
375 sasl_server_params_t *sparams,
382 Connection *conn = NULL;
385 sl.list = sparams->utils->prop_get( sparams->propctx );
386 sl.sparams = sparams;
389 /* Find our DN and conn first */
390 for( i = 0, sl.last = 0; sl.list[i].name; i++ ) {
391 if ( sl.list[i].name[0] == '*' ) {
392 if ( !strcmp( sl.list[i].name, slap_propnames[PROP_CONN] ) ) {
393 if ( sl.list[i].values && sl.list[i].values[0] )
394 AC_MEMCPY( &conn, sl.list[i].values[0], sizeof( conn ) );
395 if ( !sl.last ) sl.last = i;
397 if ( (flags & SASL_AUXPROP_AUTHZID) &&
398 !strcmp( sl.list[i].name, slap_propnames[PROP_AUTHZ] ) ) {
400 if ( sl.list[i].values && sl.list[i].values[0] )
401 AC_MEMCPY( &dn, sl.list[i].values[0], sizeof( dn ) );
402 if ( !sl.last ) sl.last = i;
405 if ( !strcmp( sl.list[i].name, slap_propnames[PROP_AUTHC] ) ) {
406 if ( !sl.last ) sl.last = i;
407 if ( sl.list[i].values && sl.list[i].values[0] ) {
408 AC_MEMCPY( &dn, sl.list[i].values[0], sizeof( dn ) );
409 if ( !(flags & SASL_AUXPROP_AUTHZID) )
416 /* Now see what else needs to be fetched */
417 for( i = 0; i < sl.last; i++ ) {
418 const char *name = sl.list[i].name;
420 if ( name[0] == '*' ) {
421 if ( flags & SASL_AUXPROP_AUTHZID ) continue;
423 } else if ( !(flags & SASL_AUXPROP_AUTHZID ) )
426 if ( sl.list[i].values ) {
427 if ( !(flags & SASL_AUXPROP_OVERRIDE) ) continue;
435 slap_callback cb = { slap_cb_null_response,
436 slap_cb_null_sresult, sasl_ap_lookup, slap_cb_null_sreference, NULL };
440 be = select_backend( &dn, 0, 1 );
442 if ( be && be->be_search ) {
443 op.o_tag = LDAP_REQ_SEARCH;
444 op.o_protocol = LDAP_VERSION3;
445 op.o_ndn = conn->c_ndn;
447 op.o_time = slap_get_time();
448 op.o_do_not_cache = 1;
449 op.o_is_auth_check = 1;
450 op.o_threadctx = conn->c_sasl_bindop->o_threadctx;
452 (*be->be_search)( be, conn, &op, &dn, &dn,
453 LDAP_SCOPE_BASE, LDAP_DEREF_NEVER, 1, 0,
454 generic_filter, &generic_filterstr, NULL, 0 );
459 static sasl_auxprop_plug_t slap_auxprop_plugin = {
462 NULL, /* glob_context */
463 NULL, /* auxprop_free */
471 const sasl_utils_t *utils,
474 sasl_auxprop_plug_t **plug,
475 const char *plugname)
477 if ( !out_version || !plug ) return SASL_BADPARAM;
479 if ( max_version < SASL_AUXPROP_PLUG_VERSION ) return SASL_BADVERS;
481 *out_version = SASL_AUXPROP_PLUG_VERSION;
482 *plug = &slap_auxprop_plugin;
486 typedef struct checkpass_info {
499 LDAPControl **ctrls )
501 slap_callback *tmp = op->o_callback;
502 checkpass_info *ci = tmp->sc_private;
507 ci->rc = SASL_NOVERIFY;
509 a = attr_find( e->e_attrs, slap_schema.si_ad_userPassword );
511 if ( ! access_allowed( be, conn, op, e, slap_schema.si_ad_userPassword,
512 NULL, ACL_AUTH, NULL ) ) return 0;
514 for ( bv = a->a_vals; bv->bv_val != NULL; bv++ ) {
515 if ( !lutil_passwd( bv, &ci->cred, NULL, &text ) ) {
527 const char *username,
530 struct propctx *propctx)
532 Connection *conn = (Connection *)context;
540 /* SASL will fallback to its own mechanisms if we don't
541 * find an answer here.
544 rc = slap_sasl_getdn( conn, (char *)username, 0, NULL, &dn,
545 SLAP_GETDN_AUTHCID );
546 if ( rc != LDAP_SUCCESS ) {
547 sasl_seterror( sconn, 0, ldap_err2string( rc ) );
551 if ( dn.bv_len == 0 ) {
552 sasl_seterror( sconn, 0,
553 "No password is associated with the Root DSE" );
554 if ( dn.bv_val != NULL ) {
555 ch_free( dn.bv_val );
560 be = select_backend( &dn, 0, 1 );
561 if ( be && be->be_search ) {
563 slap_callback cb = { slap_cb_null_response,
564 slap_cb_null_sresult, sasl_cb_checkpass, slap_cb_null_sreference, NULL };
566 ci.cred.bv_val = (char *)pass;
567 ci.cred.bv_len = passlen;
570 op.o_tag = LDAP_REQ_SEARCH;
571 op.o_protocol = LDAP_VERSION3;
572 op.o_ndn = conn->c_ndn;
574 op.o_time = slap_get_time();
575 op.o_do_not_cache = 1;
576 op.o_is_auth_check = 1;
577 op.o_threadctx = conn->c_sasl_bindop->o_threadctx;
579 (*be->be_search)( be, conn, &op, &dn, &dn,
580 LDAP_SCOPE_BASE, LDAP_DEREF_NEVER, 1, 0,
581 generic_filter, &generic_filterstr, NULL, 0 );
583 if ( ci.rc != SASL_OK ) {
584 sasl_seterror( sconn, 0,
585 ldap_err2string( LDAP_INVALID_CREDENTIALS ) );
588 ch_free( dn.bv_val );
593 /* Convert a SASL authcid or authzid into a DN. Store the DN in an
594 * auxiliary property, so that we can refer to it in sasl_authorize
595 * without interfering with anything else. Also, the SASL username
596 * buffer is constrained to 256 characters, and our DNs could be
597 * much longer (totally arbitrary length)...
600 slap_sasl_canonicalize(
606 const char *user_realm,
611 Connection *conn = (Connection *)context;
612 struct propctx *props = sasl_auxprop_getctx( sconn );
613 struct propval auxvals[3];
616 const char *names[2];
621 LDAP_LOG( TRANSPORT, ENTRY,
622 "slap_sasl_canonicalize: conn %d %s=\"%s\"\n",
623 conn ? conn->c_connid : -1,
624 (flags & SASL_CU_AUTHID) ? "authcid" : "authzid",
625 in ? in : "<empty>");
627 Debug( LDAP_DEBUG_ARGS, "SASL Canonicalize [conn=%ld]: %s=\"%s\"\n",
628 conn ? conn->c_connid : -1,
629 (flags & SASL_CU_AUTHID) ? "authcid" : "authzid",
630 in ? in : "<empty>");
633 /* If name is too big, just truncate. We don't care, we're
634 * using DNs, not the usernames.
636 if ( inlen > out_max )
639 /* See if we need to add request, can only do it once */
640 prop_getnames( props, slap_propnames, auxvals );
641 if ( !auxvals[0].name )
642 prop_request( props, slap_propnames );
644 if ( flags & SASL_CU_AUTHID )
649 /* Need to store the Connection for auxprop_lookup */
650 if ( !auxvals[PROP_CONN].values ) {
651 names[0] = slap_propnames[PROP_CONN];
653 prop_set( props, names[0], (char *)&conn, sizeof( conn ) );
656 /* Already been here? */
657 if ( auxvals[which].values )
660 /* Normally we require an authzID to have a u: or dn: prefix.
661 * However, SASL frequently gives us an authzID that is just
662 * an exact copy of the authcID, without a prefix. We need to
663 * detect and allow this condition. If SASL calls canonicalize
664 * with SASL_CU_AUTHID|SASL_CU_AUTHZID this is a no-brainer.
665 * But if it's broken into two calls, we need to remember the
666 * authcID so that we can compare the authzID later. We store
667 * the authcID temporarily in conn->c_sasl_dn. We necessarily
668 * finish Canonicalizing before Authorizing, so there is no
669 * conflict with slap_sasl_authorize's use of this temp var.
671 * The SASL EXTERNAL mech is backwards from all the other mechs,
672 * it does authzID before the authcID. If we see that authzID
673 * has already been done, don't do anything special with authcID.
675 if ( flags == SASL_CU_AUTHID && !auxvals[PROP_AUTHZ].values ) {
676 conn->c_sasl_dn.bv_val = (char *) in;
677 } else if ( flags == SASL_CU_AUTHZID && conn->c_sasl_dn.bv_val ) {
678 rc = strcmp( in, conn->c_sasl_dn.bv_val );
679 conn->c_sasl_dn.bv_val = NULL;
680 /* They were equal, no work needed */
681 if ( !rc ) goto done;
684 rc = slap_sasl_getdn( conn, (char *)in, inlen, (char *)user_realm, &dn,
685 (flags & SASL_CU_AUTHID) ? SLAP_GETDN_AUTHCID : SLAP_GETDN_AUTHZID );
686 if ( rc != LDAP_SUCCESS ) {
687 sasl_seterror( sconn, 0, ldap_err2string( rc ) );
691 names[0] = slap_propnames[which];
694 prop_set( props, names[0], (char *)&dn, sizeof( dn ) );
697 LDAP_LOG( TRANSPORT, ENTRY,
698 "slap_sasl_canonicalize: conn %d %s=\"%s\"\n",
699 conn ? conn->c_connid : -1, names[0]+1,
700 dn.bv_val ? dn.bv_val : "<EMPTY>" );
702 Debug( LDAP_DEBUG_ARGS, "SASL Canonicalize [conn=%ld]: %s=\"%s\"\n",
703 conn ? conn->c_connid : -1, names[0]+1,
704 dn.bv_val ? dn.bv_val : "<EMPTY>" );
708 AC_MEMCPY( out, in, inlen );
720 char *requested_user,
724 const char *def_realm,
726 struct propctx *props)
728 Connection *conn = (Connection *)context;
729 struct propval auxvals[3];
730 struct berval authcDN, authzDN;
734 LDAP_LOG( TRANSPORT, ENTRY,
735 "slap_sasl_authorize: conn %d authcid=\"%s\" authzid=\"%s\"\n",
736 conn ? conn->c_connid : -1, auth_identity, requested_user);
738 Debug( LDAP_DEBUG_ARGS, "SASL Authorize [conn=%ld]: "
739 "authcid=\"%s\" authzid=\"%s\"\n",
740 conn ? conn->c_connid : -1, auth_identity, requested_user );
742 if ( conn->c_sasl_dn.bv_val ) {
743 ch_free( conn->c_sasl_dn.bv_val );
744 conn->c_sasl_dn.bv_val = NULL;
745 conn->c_sasl_dn.bv_len = 0;
749 prop_getnames( props, slap_propnames+1, auxvals );
751 AC_MEMCPY( &authcDN, auxvals[0].values[0], sizeof(authcDN) );
753 /* Nothing to do if no authzID was given */
754 if ( !auxvals[1].name || !auxvals[1].values ) {
755 conn->c_sasl_dn = authcDN;
759 AC_MEMCPY( &authzDN, auxvals[1].values[0], sizeof(authzDN) );
761 rc = slap_sasl_authorized( conn, &authcDN, &authzDN );
762 ch_free( authcDN.bv_val );
763 if ( rc != LDAP_SUCCESS ) {
765 LDAP_LOG( TRANSPORT, INFO,
766 "slap_sasl_authorize: conn %ld authorization disallowed (%d)\n",
767 (long)(conn ? conn->c_connid : -1), rc, 0 );
769 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
770 " authorization disallowed (%d)\n",
771 (long) (conn ? conn->c_connid : -1), rc, 0 );
774 sasl_seterror( sconn, 0, "not authorized" );
775 ch_free( authzDN.bv_val );
779 conn->c_sasl_dn = authzDN;
781 if (conn->c_sasl_bindop) {
782 Statslog( LDAP_DEBUG_STATS,
783 "conn=%lu op=%lu BIND authcid=\"%s\"\n",
784 conn->c_connid, conn->c_sasl_bindop->o_opid,
785 auth_identity, 0, 0);
789 LDAP_LOG( TRANSPORT, ENTRY,
790 "slap_sasl_authorize: conn %d authorization allowed\n",
791 (long)(conn ? conn->c_connid : -1), 0, 0 );
793 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
794 " authorization allowed\n",
795 (long) (conn ? conn->c_connid : -1), 0, 0 );
808 struct berval authcDN, authzDN;
810 Connection *conn = context;
814 if ( conn->c_sasl_dn.bv_val ) {
815 ch_free( conn->c_sasl_dn.bv_val );
816 conn->c_sasl_dn.bv_val = NULL;
817 conn->c_sasl_dn.bv_len = 0;
821 LDAP_LOG( TRANSPORT, ENTRY,
822 "slap_sasl_authorize: conn %d authcid=\"%s\" authzid=\"%s\"\n",
823 conn ? conn->c_connid : -1, authcid ? authcid : "<empty>",
824 authzid ? authzid : "<empty>" );
826 Debug( LDAP_DEBUG_ARGS, "SASL Authorize [conn=%ld]: "
827 "authcid=\"%s\" authzid=\"%s\"\n",
828 (long) (conn ? conn->c_connid : -1),
829 authcid ? authcid : "<empty>",
830 authzid ? authzid : "<empty>" );
833 /* Figure out how much data we have for the dn */
834 rc = sasl_getprop( conn->c_sasl_context, SASL_REALM, (void **)&realm );
835 if( rc != SASL_OK && rc != SASL_NOTDONE ) {
837 LDAP_LOG( TRANSPORT, ERR,
838 "slap_sasl_authorize: getprop(REALM) failed.\n", 0, 0, 0 );
840 Debug(LDAP_DEBUG_TRACE,
841 "authorize: getprop(REALM) failed!\n", 0,0,0);
843 *errstr = "Could not extract realm";
847 /* Convert the identities to DN's. If no authzid was given, client will
848 be bound as the DN matching their username */
849 rc = slap_sasl_getdn( conn, (char *)authcid, 0, realm,
850 &authcDN, SLAP_GETDN_AUTHCID );
851 if( rc != LDAP_SUCCESS ) {
852 *errstr = ldap_err2string( rc );
855 if( ( authzid == NULL ) || !strcmp( authcid,authzid ) ) {
857 LDAP_LOG( TRANSPORT, ENTRY,
858 "slap_sasl_authorize: conn %d Using authcDN=%s\n",
859 conn ? conn->c_connid : -1, authcDN.bv_val, 0 );
861 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
862 "Using authcDN=%s\n", (long) (conn ? conn->c_connid : -1), authcDN.bv_val,0 );
865 conn->c_sasl_dn = authcDN;
868 rc = slap_sasl_getdn( conn, (char *)authzid, 0, realm,
869 &authzDN, SLAP_GETDN_AUTHZID );
870 if( rc != LDAP_SUCCESS ) {
871 ch_free( authcDN.bv_val );
872 *errstr = ldap_err2string( rc );
876 rc = slap_sasl_authorized(conn, &authcDN, &authzDN );
877 ch_free( authcDN.bv_val );
880 LDAP_LOG( TRANSPORT, INFO,
881 "slap_sasl_authorize: conn %ld authorization disallowed (%d)\n",
882 (long)(conn ? conn->c_connid : -1), rc, 0 );
884 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
885 " authorization disallowed (%d)\n",
886 (long) (conn ? conn->c_connid : -1), rc, 0 );
889 *errstr = "not authorized";
890 ch_free( authzDN.bv_val );
893 conn->c_sasl_dn = authzDN;
897 LDAP_LOG( TRANSPORT, RESULTS,
898 "slap_sasl_authorize: conn %d authorization allowed\n",
899 (long)(conn ? conn->c_connid : -1 ), 0, 0 );
901 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
902 " authorization allowed\n",
903 (long) (conn ? conn->c_connid : -1), 0, 0 );
906 if (conn->c_sasl_bindop) {
907 Statslog( LDAP_DEBUG_STATS,
908 "conn=%lu op=%lu BIND authcid=\"%s\"\n",
909 conn->c_connid, conn->c_sasl_bindop->o_opid,
916 #endif /* SASL_VERSION_MAJOR >= 2 */
919 slap_sasl_err2ldap( int saslerr )
925 rc = LDAP_SASL_BIND_IN_PROGRESS;
934 rc = LDAP_AUTH_METHOD_NOT_SUPPORTED;
937 rc = LDAP_INVALID_CREDENTIALS;
940 rc = LDAP_INSUFFICIENT_ACCESS;
944 rc = LDAP_INAPPROPRIATE_AUTH;
955 int slap_sasl_init( void )
957 #ifdef HAVE_CYRUS_SASL
959 static sasl_callback_t server_callbacks[] = {
960 { SASL_CB_LOG, &slap_sasl_log, NULL },
961 { SASL_CB_LIST_END, NULL, NULL }
964 #ifdef HAVE_SASL_VERSION
965 #define SASL_BUILD_VERSION ((SASL_VERSION_MAJOR << 24) |\
966 (SASL_VERSION_MINOR << 16) | SASL_VERSION_STEP)
968 sasl_version( NULL, &rc );
969 if ( ((rc >> 16) != ((SASL_VERSION_MAJOR << 8)|SASL_VERSION_MINOR)) ||
970 (rc & 0xffff) < SASL_VERSION_STEP) {
973 LDAP_LOG( TRANSPORT, INFO,
974 "slap_sasl_init: SASL version mismatch, got %x, wanted %x.\n",
975 rc, SASL_BUILD_VERSION, 0 );
977 Debug( LDAP_DEBUG_ANY,
978 "slap_sasl_init: SASL version mismatch, got %x, wanted %x.\n",
979 rc, SASL_BUILD_VERSION, 0 );
985 /* SASL 2 does its own memory management internally */
986 #if SASL_VERSION_MAJOR < 2
995 ldap_pvt_sasl_mutex_new,
996 ldap_pvt_sasl_mutex_lock,
997 ldap_pvt_sasl_mutex_unlock,
998 ldap_pvt_sasl_mutex_dispose );
1000 #if SASL_VERSION_MAJOR >= 2
1001 sasl_auxprop_add_plugin( "slapd", slap_auxprop_init );
1003 /* should provide callbacks for logging */
1004 /* server name should be configurable */
1005 rc = sasl_server_init( server_callbacks, "slapd" );
1007 if( rc != SASL_OK ) {
1009 LDAP_LOG( TRANSPORT, INFO, "slap_sasl_init: init failed.\n", 0, 0, 0 );
1011 Debug( LDAP_DEBUG_ANY, "sasl_server_init failed\n",
1014 #if SASL_VERSION_MAJOR < 2
1015 /* A no-op used to make sure we linked with Cyrus 1.5 */
1016 sasl_client_auth( NULL, NULL, NULL, 0, NULL, NULL );
1023 LDAP_LOG( TRANSPORT, INFO, "slap_sasl_init: initialized!\n", 0, 0, 0 );
1025 Debug( LDAP_DEBUG_TRACE, "slap_sasl_init: initialized!\n",
1030 /* default security properties */
1031 memset( &sasl_secprops, '\0', sizeof(sasl_secprops) );
1032 sasl_secprops.max_ssf = INT_MAX;
1033 sasl_secprops.maxbufsize = 65536;
1034 sasl_secprops.security_flags = SASL_SEC_NOPLAINTEXT|SASL_SEC_NOANONYMOUS;
1040 int slap_sasl_destroy( void )
1042 #ifdef HAVE_CYRUS_SASL
1045 #if SASL_VERSION_MAJOR >= 2
1046 filter_free( generic_filter );
1048 free( global_host );
1054 int slap_sasl_open( Connection *conn )
1056 int cb, sc = LDAP_SUCCESS;
1057 #if SASL_VERSION_MAJOR >= 2
1058 char *ipremoteport = NULL, *iplocalport = NULL;
1061 #ifdef HAVE_CYRUS_SASL
1062 sasl_conn_t *ctx = NULL;
1063 sasl_callback_t *session_callbacks;
1065 assert( conn->c_sasl_context == NULL );
1066 assert( conn->c_sasl_extra == NULL );
1068 conn->c_sasl_layers = 0;
1071 #if SASL_VERSION_MAJOR >= 2
1072 SLAP_CALLOC( 5, sizeof(sasl_callback_t));
1074 SLAP_CALLOC( 3, sizeof(sasl_callback_t));
1076 if( session_callbacks == NULL ) {
1078 LDAP_LOG( TRANSPORT, ERR,
1079 "slap_sasl_open: SLAP_MALLOC failed", 0, 0, 0 );
1081 Debug( LDAP_DEBUG_ANY,
1082 "slap_sasl_open: SLAP_MALLOC failed", 0, 0, 0 );
1086 conn->c_sasl_extra = session_callbacks;
1088 session_callbacks[cb=0].id = SASL_CB_LOG;
1089 session_callbacks[cb].proc = &slap_sasl_log;
1090 session_callbacks[cb++].context = conn;
1092 session_callbacks[cb].id = SASL_CB_PROXY_POLICY;
1093 session_callbacks[cb].proc = &slap_sasl_authorize;
1094 session_callbacks[cb++].context = conn;
1096 #if SASL_VERSION_MAJOR >= 2
1097 session_callbacks[cb].id = SASL_CB_CANON_USER;
1098 session_callbacks[cb].proc = &slap_sasl_canonicalize;
1099 session_callbacks[cb++].context = conn;
1101 /* XXXX: this should be conditional */
1102 session_callbacks[cb].id = SASL_CB_SERVER_USERDB_CHECKPASS;
1103 session_callbacks[cb].proc = &slap_sasl_checkpass;
1104 session_callbacks[cb++].context = conn;
1107 session_callbacks[cb].id = SASL_CB_LIST_END;
1108 session_callbacks[cb].proc = NULL;
1109 session_callbacks[cb++].context = NULL;
1111 if( global_host == NULL ) {
1112 global_host = ldap_pvt_get_fqdn( NULL );
1115 /* create new SASL context */
1116 #if SASL_VERSION_MAJOR >= 2
1117 if ( generic_filter == NULL ) {
1118 generic_filter = str2filter( generic_filterstr.bv_val );
1120 if ( conn->c_sock_name.bv_len != 0 &&
1121 strncmp( conn->c_sock_name.bv_val, "IP=", 3 ) == 0) {
1124 iplocalport = ch_strdup( conn->c_sock_name.bv_val + 3 );
1125 /* Convert IPv6 addresses to address;port syntax. */
1126 p = strrchr( iplocalport, ' ' );
1127 /* Convert IPv4 addresses to address;port syntax. */
1128 if ( p == NULL ) p = strchr( iplocalport, ':' );
1133 if ( conn->c_peer_name.bv_len != 0 &&
1134 strncmp( conn->c_peer_name.bv_val, "IP=", 3 ) == 0) {
1137 ipremoteport = ch_strdup( conn->c_peer_name.bv_val + 3 );
1138 /* Convert IPv6 addresses to address;port syntax. */
1139 p = strrchr( ipremoteport, ' ' );
1140 /* Convert IPv4 addresses to address;port syntax. */
1141 if ( p == NULL ) p = strchr( ipremoteport, ':' );
1146 sc = sasl_server_new( "ldap", global_host, global_realm,
1147 iplocalport, ipremoteport, session_callbacks, SASL_SUCCESS_DATA, &ctx );
1148 if ( iplocalport != NULL ) {
1149 ch_free( iplocalport );
1151 if ( ipremoteport != NULL ) {
1152 ch_free( ipremoteport );
1155 sc = sasl_server_new( "ldap", global_host, global_realm,
1156 session_callbacks, SASL_SECURITY_LAYER, &ctx );
1159 if( sc != SASL_OK ) {
1161 LDAP_LOG( TRANSPORT, ERR,
1162 "slap_sasl_open: sasl_server_new failed: %d\n", sc, 0, 0 );
1164 Debug( LDAP_DEBUG_ANY, "sasl_server_new failed: %d\n",
1171 conn->c_sasl_context = ctx;
1173 if( sc == SASL_OK ) {
1174 sc = sasl_setprop( ctx,
1175 SASL_SEC_PROPS, &sasl_secprops );
1177 if( sc != SASL_OK ) {
1179 LDAP_LOG( TRANSPORT, ERR,
1180 "slap_sasl_open: sasl_setprop failed: %d \n", sc, 0, 0 );
1182 Debug( LDAP_DEBUG_ANY, "sasl_setprop failed: %d\n",
1186 slap_sasl_close( conn );
1191 sc = slap_sasl_err2ldap( sc );
1196 int slap_sasl_external(
1199 const char *auth_id )
1201 #if SASL_VERSION_MAJOR >= 2
1203 sasl_conn_t *ctx = conn->c_sasl_context;
1205 if ( ctx == NULL ) {
1206 return LDAP_UNAVAILABLE;
1209 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL, &ssf );
1211 if ( sc != SASL_OK ) {
1215 sc = sasl_setprop( ctx, SASL_AUTH_EXTERNAL, auth_id );
1217 if ( sc != SASL_OK ) {
1221 #elif defined(HAVE_CYRUS_SASL)
1223 sasl_conn_t *ctx = conn->c_sasl_context;
1224 sasl_external_properties_t extprops;
1226 if ( ctx == NULL ) {
1227 return LDAP_UNAVAILABLE;
1230 memset( &extprops, '\0', sizeof(extprops) );
1232 extprops.auth_id = (char *) auth_id;
1234 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL,
1235 (void *) &extprops );
1237 if ( sc != SASL_OK ) {
1242 return LDAP_SUCCESS;
1245 int slap_sasl_reset( Connection *conn )
1247 #ifdef HAVE_CYRUS_SASL
1248 sasl_conn_t *ctx = conn->c_sasl_context;
1253 /* must return "anonymous" */
1254 return LDAP_SUCCESS;
1257 char ** slap_sasl_mechs( Connection *conn )
1259 char **mechs = NULL;
1261 #ifdef HAVE_CYRUS_SASL
1262 sasl_conn_t *ctx = conn->c_sasl_context;
1266 SASL_CONST char *mechstr;
1268 sc = sasl_listmech( ctx,
1269 NULL, NULL, ",", NULL,
1270 &mechstr, NULL, NULL );
1272 if( sc != SASL_OK ) {
1274 LDAP_LOG( TRANSPORT, ERR,
1275 "slap_sasl_mechs: sasl_listmech failed: %d\n", sc, 0, 0 );
1277 Debug( LDAP_DEBUG_ANY, "slap_sasl_listmech failed: %d\n",
1284 mechs = ldap_str2charray( mechstr, "," );
1286 #if SASL_VERSION_MAJOR < 2
1295 int slap_sasl_close( Connection *conn )
1297 #ifdef HAVE_CYRUS_SASL
1298 sasl_conn_t *ctx = conn->c_sasl_context;
1301 sasl_dispose( &ctx );
1304 conn->c_sasl_context = NULL;
1306 free( conn->c_sasl_extra );
1307 conn->c_sasl_extra = NULL;
1310 return LDAP_SUCCESS;
1318 struct berval *cred,
1324 #ifdef HAVE_CYRUS_SASL
1325 sasl_conn_t *ctx = conn->c_sasl_context;
1326 struct berval response;
1327 unsigned reslen = 0;
1328 const char *errstr = NULL;
1332 LDAP_LOG( TRANSPORT, ENTRY,
1333 "sasl_bind: dn=\"%s\" mech=%s datalen=%ld\n",
1334 dn->bv_len ? dn->bv_val : "",
1335 conn->c_sasl_bind_in_progress ? "<continuing>" :
1336 conn->c_sasl_bind_mech.bv_val,
1337 cred ? cred->bv_len : 0 );
1339 Debug(LDAP_DEBUG_ARGS,
1340 "==> sasl_bind: dn=\"%s\" mech=%s datalen=%ld\n",
1341 dn->bv_len ? dn->bv_val : "",
1342 conn->c_sasl_bind_in_progress ? "<continuing>":conn->c_sasl_bind_mech.bv_val,
1343 cred ? cred->bv_len : 0 );
1348 send_ldap_result( conn, op, LDAP_UNAVAILABLE,
1349 NULL, "SASL unavailable on this session", NULL, NULL );
1353 #if SASL_VERSION_MAJOR >= 2
1354 #define START( ctx, mech, cred, clen, resp, rlen, err ) \
1355 sasl_server_start( ctx, mech, cred, clen, resp, rlen )
1356 #define STEP( ctx, cred, clen, resp, rlen, err ) \
1357 sasl_server_step( ctx, cred, clen, resp, rlen )
1359 #define START( ctx, mech, cred, clen, resp, rlen, err ) \
1360 sasl_server_start( ctx, mech, cred, clen, resp, rlen, err )
1361 #define STEP( ctx, cred, clen, resp, rlen, err ) \
1362 sasl_server_step( ctx, cred, clen, resp, rlen, err )
1365 if ( !conn->c_sasl_bind_in_progress ) {
1367 conn->c_sasl_bind_mech.bv_val,
1368 cred->bv_val, cred->bv_len,
1369 (SASL_CONST char **)&response.bv_val, &reslen, &errstr );
1373 cred->bv_val, cred->bv_len,
1374 (SASL_CONST char **)&response.bv_val, &reslen, &errstr );
1377 response.bv_len = reslen;
1379 if ( sc == SASL_OK ) {
1380 sasl_ssf_t *ssf = NULL;
1382 *edn = conn->c_sasl_dn;
1383 conn->c_sasl_dn.bv_val = NULL;
1384 conn->c_sasl_dn.bv_len = 0;
1388 (void) sasl_getprop( ctx, SASL_SSF, (void *)&ssf );
1389 *ssfp = ssf ? *ssf : 0;
1392 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1393 conn->c_sasl_layers++;
1394 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1397 send_ldap_sasl( conn, op, rc,
1398 NULL, NULL, NULL, NULL,
1399 response.bv_len ? &response : NULL );
1401 } else if ( sc == SASL_CONTINUE ) {
1402 send_ldap_sasl( conn, op, rc = LDAP_SASL_BIND_IN_PROGRESS,
1403 NULL, NULL, NULL, NULL, &response );
1406 #if SASL_VERSION_MAJOR >= 2
1407 errstr = sasl_errdetail( ctx );
1409 send_ldap_result( conn, op, rc = slap_sasl_err2ldap( sc ),
1410 NULL, errstr, NULL, NULL );
1413 #if SASL_VERSION_MAJOR < 2
1414 if( response.bv_len ) {
1415 ch_free( response.bv_val );
1420 LDAP_LOG( TRANSPORT, RESULTS, "slap_sasl_bind: rc=%d\n", rc, 0, 0 );
1422 Debug(LDAP_DEBUG_TRACE, "<== slap_sasl_bind: rc=%d\n", rc, 0, 0);
1427 send_ldap_result( conn, op, rc = LDAP_UNAVAILABLE,
1428 NULL, "SASL not supported", NULL, NULL );
1434 char* slap_sasl_secprops( const char *in )
1436 #ifdef HAVE_CYRUS_SASL
1437 int rc = ldap_pvt_sasl_secprops( in, &sasl_secprops );
1439 return rc == LDAP_SUCCESS ? NULL : "Invalid security properties";
1441 return "SASL not supported";
1445 #ifdef HAVE_CYRUS_SASL
1451 struct berval *reqdata,
1453 struct berval **rspdata,
1454 LDAPControl *** rspctrls,
1458 struct berval id = { 0, NULL }; /* needs to come from connection */
1459 struct berval new = { 0, NULL };
1460 struct berval old = { 0, NULL };
1462 assert( reqoid != NULL );
1463 assert( strcmp( LDAP_EXOP_MODIFY_PASSWD, reqoid ) == 0 );
1465 rc = sasl_getprop( conn->c_sasl_context, SASL_USERNAME,
1466 (SASL_CONST void **)&id.bv_val );
1468 if( rc != SASL_OK ) {
1469 *text = "unable to retrieve SASL username";
1475 LDAP_LOG( BACKEND, ENTRY,
1476 "slap_sasl_setpass: \"%s\"\n",
1477 id.bv_val ? id.bv_val : "", 0, 0);
1479 Debug( LDAP_DEBUG_ARGS, "==> slap_sasl_setpass: \"%s\"\n",
1480 id.bv_val ? id.bv_val : "", 0, 0 );
1483 rc = slap_passwd_parse( reqdata,
1484 NULL, &old, &new, text );
1486 if( rc != LDAP_SUCCESS ) {
1490 if( new.bv_len == 0 ) {
1491 slap_passwd_generate(&new);
1493 if( new.bv_len == 0 ) {
1494 *text = "password generation failed.";
1499 *rspdata = slap_passwd_return( &new );
1502 #if SASL_VERSION_MAJOR < 2
1503 rc = sasl_setpass( conn->c_sasl_context,
1504 id.bv_val, new.bv_val, new.bv_len, 0, text );
1506 rc = sasl_setpass( conn->c_sasl_context, id.bv_val,
1507 new.bv_val, new.bv_len, old.bv_val, old.bv_len, 0 );
1508 if( rc != SASL_OK ) {
1509 *text = sasl_errdetail( conn->c_sasl_context );
1530 #endif /* HAVE_CYRUS_SASL */
1532 /* Take any sort of identity string and return a DN with the "dn:" prefix. The
1533 string returned in *dn is in its own allocated memory, and must be free'd
1534 by the calling process.
1535 -Mark Adamson, Carnegie Mellon
1537 The "dn:" prefix is no longer used anywhere inside slapd. It is only used
1538 on strings passed in directly from SASL.
1539 -Howard Chu, Symas Corp.
1546 static struct berval ext_bv = BER_BVC( "EXTERNAL" );
1548 int slap_sasl_getdn( Connection *conn, char *id, int len,
1549 char *user_realm, struct berval *dn, int flags )
1551 int rc, is_dn = SET_NONE, do_norm = 1;
1552 struct berval dn2, *mech;
1555 LDAP_LOG( TRANSPORT, ENTRY,
1556 "slap_sasl_getdn: conn %d id=%s [len=%d]\n",
1557 conn ? conn->c_connid : -1, id ? (*id ? id : "<empty>") : "NULL", len );
1559 Debug( LDAP_DEBUG_ARGS, "slap_sasl_getdn: id=%s [len=%d]\n",
1560 id ? ( *id ? id : "<empty>" ) : "NULL", len, 0 );
1567 if ( len == 0 ) len = strlen( id );
1569 /* Blatantly anonymous ID */
1570 if ( len == sizeof("anonymous") - 1 &&
1571 !strcasecmp( id, "anonymous" ) ) {
1572 return( LDAP_SUCCESS );
1578 if ( conn->c_sasl_bind_mech.bv_len ) {
1579 mech = &conn->c_sasl_bind_mech;
1581 mech = &conn->c_authmech;
1584 /* An authcID needs to be converted to authzID form. Set the
1585 * values directly into *dn; they will be normalized later. (and
1586 * normalizing always makes a new copy.) An ID from a TLS certificate
1587 * is already normalized, so copy it and skip normalization.
1589 if( flags & SLAP_GETDN_AUTHCID ) {
1590 if( mech->bv_len == ext_bv.bv_len &&
1591 strcasecmp( ext_bv.bv_val, mech->bv_val ) == 0 )
1593 /* EXTERNAL DNs are already normalized */
1596 ber_str2bv( id, len, 1, dn );
1599 /* convert to u:<username> form */
1605 if( is_dn == SET_NONE ) {
1606 if( !strncasecmp( id, "u:", sizeof("u:")-1 )) {
1610 } else if ( !strncasecmp( id, "dn:", sizeof("dn:")-1) ) {
1617 /* No other possibilities from here */
1618 if( is_dn == SET_NONE ) {
1621 return( LDAP_INAPPROPRIATE_AUTH );
1624 /* Username strings */
1625 if( is_dn == SET_U ) {
1627 struct berval realm = { 0, NULL }, c1 = *dn;
1628 len = dn->bv_len + sizeof("uid=")-1 + sizeof(",cn=auth")-1;
1631 /* userids can legally have embedded '@' chars;
1632 * the realm should be set by those mechanisms
1633 * that support it by means of the user_realm
1636 if( ( realm = strchr( dn->bv_val, '@') ) ) {
1638 len += sizeof(",cn=")-2;
1641 if( user_realm && *user_realm ) {
1642 realm.bv_val = user_realm;
1643 realm.bv_len = strlen( user_realm );
1644 len += realm.bv_len + sizeof(",cn=")-1;
1647 if( mech->bv_len ) {
1648 len += mech->bv_len + sizeof(",cn=")-1;
1651 /* Build the new dn */
1652 dn->bv_val = SLAP_MALLOC( len+1 );
1653 if( dn->bv_val == NULL ) {
1655 LDAP_LOG( TRANSPORT, ERR,
1656 "slap_sasl_getdn: SLAP_MALLOC failed", 0, 0, 0 );
1658 Debug( LDAP_DEBUG_ANY,
1659 "slap_sasl_getdn: SLAP_MALLOC failed", 0, 0, 0 );
1663 p = lutil_strcopy( dn->bv_val, "uid=" );
1664 p = lutil_strncopy( p, c1.bv_val, dn->bv_len );
1666 if( realm.bv_len ) {
1667 p = lutil_strcopy( p, ",cn=" );
1668 p = lutil_strncopy( p, realm.bv_val, realm.bv_len );
1671 if( mech->bv_len ) {
1672 p = lutil_strcopy( p, ",cn=" );
1673 p = lutil_strcopy( p, mech->bv_val );
1675 p = lutil_strcopy( p, ",cn=auth" );
1676 dn->bv_len = p - dn->bv_val;
1679 LDAP_LOG( TRANSPORT, ENTRY,
1680 "slap_sasl_getdn: u:id converted to %s.\n", dn->bv_val, 0, 0 );
1682 Debug( LDAP_DEBUG_TRACE, "slap_sasl_getdn: u:id converted to %s\n", dn->bv_val,0,0 );
1686 /* Dup the DN in any case, so we don't risk
1687 * leaks or dangling pointers later,
1688 * and the DN value is '\0' terminated */
1689 ber_dupbv( &dn2, dn );
1690 dn->bv_val = dn2.bv_val;
1693 /* All strings are in DN form now. Normalize if needed. */
1695 rc = dnNormalize2( NULL, dn, &dn2 );
1697 /* User DNs were constructed above and must be freed now */
1698 ch_free( dn->bv_val );
1700 if ( rc != LDAP_SUCCESS ) {
1708 /* Run thru regexp */
1709 slap_sasl2dn( conn, dn, &dn2 );
1711 ch_free( dn->bv_val );
1714 LDAP_LOG( TRANSPORT, ENTRY,
1715 "slap_sasl_getdn: dn:id converted to %s.\n", dn->bv_val, 0, 0 );
1717 Debug( LDAP_DEBUG_TRACE, "getdn: dn:id converted to %s\n",
1722 return( LDAP_SUCCESS );