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 # include <sasl/saslplug.h>
29 # define SASL_CONST const
34 static sasl_security_properties_t sasl_secprops;
35 #endif /* HAVE_CYRUS_SASL */
41 int slap_sasl_config( int cargc, char **cargv, char *line,
42 const char *fname, int lineno )
44 /* set SASL proxy authorization policy */
45 if ( strcasecmp( cargv[0], "sasl-authz-policy" ) == 0 ) {
48 LDAP_LOG( CONFIG, CRIT,
49 "%s: line %d: missing policy in"
50 " \"sasl-authz-policy <policy>\" line\n",
53 Debug( LDAP_DEBUG_ANY,
54 "%s: line %d: missing policy in"
55 " \"sasl-authz-policy <policy>\" line\n",
61 if ( slap_sasl_setpolicy( cargv[1] ) ) {
63 LDAP_LOG( CONFIG, CRIT,
64 "%s: line %d: unable "
65 "to parse value \"%s\" "
66 "in \"sasl-authz-policy "
68 fname, lineno, cargv[1] );
70 Debug( LDAP_DEBUG_ANY,
71 "%s: line %d: unable "
72 "to parse value \"%s\" "
73 "in \"sasl-authz-policy "
75 fname, lineno, cargv[1] );
80 } else if ( !strcasecmp( cargv[0], "sasl-regexp" )
81 || !strcasecmp( cargv[0], "saslregexp" ) )
86 LDAP_LOG( CONFIG, CRIT,
87 "%s: line %d: need 2 args in "
88 "\"saslregexp <match> <replace>\"\n",
91 Debug( LDAP_DEBUG_ANY,
92 "%s: line %d: need 2 args in "
93 "\"saslregexp <match> <replace>\"\n",
99 rc = slap_sasl_regexp_config( cargv[1], cargv[2] );
104 #ifdef HAVE_CYRUS_SASL
106 } else if ( strcasecmp( cargv[0], "sasl-host" ) == 0 ) {
109 LDAP_LOG( CONFIG, CRIT,
110 "%s: line %d: missing host in \"sasl-host <host>\" line\n",
113 Debug( LDAP_DEBUG_ANY,
114 "%s: line %d: missing host in \"sasl-host <host>\" line\n",
121 if ( global_host != NULL ) {
123 LDAP_LOG( CONFIG, CRIT,
124 "%s: line %d: already set sasl-host!\n",
127 Debug( LDAP_DEBUG_ANY,
128 "%s: line %d: already set sasl-host!\n",
135 global_host = ch_strdup( cargv[1] );
139 } else if ( strcasecmp( cargv[0], "sasl-realm" ) == 0 ) {
142 LDAP_LOG( CONFIG, CRIT, "%s: line %d: "
143 "missing realm in \"sasl-realm <realm>\" line.\n",
146 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
147 "missing realm in \"sasl-realm <realm>\" line.\n",
154 if ( global_realm != NULL ) {
156 LDAP_LOG( CONFIG, CRIT,
157 "%s: line %d: already set sasl-realm!\n",
160 Debug( LDAP_DEBUG_ANY,
161 "%s: line %d: already set sasl-realm!\n",
168 global_realm = ch_strdup( cargv[1] );
171 /* SASL security properties */
172 } else if ( strcasecmp( cargv[0], "sasl-secprops" ) == 0 ) {
177 LDAP_LOG( CONFIG, CRIT, "%s: line %d: "
178 "missing flags in \"sasl-secprops <properties>\" line\n",
181 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
182 "missing flags in \"sasl-secprops <properties>\" line\n",
189 txt = slap_sasl_secprops( cargv[1] );
192 LDAP_LOG( CONFIG, CRIT,
193 "%s: line %d sasl-secprops: %s\n",
194 fname, lineno, txt );
196 Debug( LDAP_DEBUG_ANY,
197 "%s: line %d: sasl-secprops: %s\n",
198 fname, lineno, txt );
203 #endif /* HAVE_CYRUS_SASL */
209 #ifdef HAVE_CYRUS_SASL
217 Connection *conn = context;
221 if ( message == NULL ) {
222 return SASL_BADPARAM;
226 #if SASL_VERSION_MAJOR >= 2
228 level = LDAP_DEBUG_NONE;
232 level = LDAP_DEBUG_ANY;
236 level = LDAP_DEBUG_ANY;
240 level = LDAP_DEBUG_TRACE;
244 level = LDAP_DEBUG_TRACE;
248 level = LDAP_DEBUG_TRACE;
252 level = LDAP_DEBUG_TRACE;
256 level = LDAP_DEBUG_TRACE;
257 label = "Password Trace";
261 level = LDAP_DEBUG_ANY;
264 case SASL_LOG_WARNING:
265 level = LDAP_DEBUG_TRACE;
269 level = LDAP_DEBUG_TRACE;
274 return SASL_BADPARAM;
278 LDAP_LOG( TRANSPORT, ENTRY,
279 "SASL [conn=%ld] %s: %s\n", conn ? conn->c_connid : -1, label, message);
281 Debug( level, "SASL [conn=%ld] %s: %s\n",
282 conn ? conn->c_connid: -1,
291 #if SASL_VERSION_MAJOR >= 2
292 static const char *slap_propnames[] = {
293 "*slapConn", "*authcDN", "*authzDN", NULL };
295 static Filter *generic_filter;
301 typedef struct lookup_info {
304 const struct propval *list;
305 sasl_server_params_t *sparams;
316 LDAPControl **ctrls )
319 AttributeDescription *ad;
323 slap_callback *tmp = op->o_callback;
324 lookup_info *sl = tmp->sc_private;
326 for( i = 0; i < sl->last; i++ ) {
327 const char *name = sl->list[i].name;
329 if ( name[0] == '*' ) {
330 if ( sl->flags & SASL_AUXPROP_AUTHZID ) continue;
332 } else if ( !(sl->flags & SASL_AUXPROP_AUTHZID ) )
335 if ( sl->list[i].values ) {
336 if ( !(sl->flags & SASL_AUXPROP_OVERRIDE) ) continue;
339 rc = slap_str2ad( name, &ad, &text );
340 if ( rc != LDAP_SUCCESS ) {
342 LDAP_LOG( TRANSPORT, DETAIL1,
343 "slap_auxprop: str2ad(%s): %s\n", name, text, 0 );
345 Debug( LDAP_DEBUG_TRACE,
346 "slap_auxprop: str2ad(%s): %s\n", name, text, 0 );
350 a = attr_find( e->e_attrs, ad );
352 if ( ! access_allowed( be, conn, op, e, ad, NULL, ACL_AUTH, NULL ) )
354 if ( sl->list[i].values && ( sl->flags & SASL_AUXPROP_OVERRIDE ) )
355 sl->sparams->utils->prop_erase( sl->sparams->propctx, sl->list[i].name );
356 for ( bv = a->a_vals; bv->bv_val; bv++ ) {
357 sl->sparams->utils->prop_set( sl->sparams->propctx, sl->list[i].name,
358 bv->bv_val, bv->bv_len );
367 sasl_server_params_t *sparams,
374 Connection *conn = NULL;
377 sl.list = sparams->utils->prop_get( sparams->propctx );
378 sl.sparams = sparams;
381 /* Find our DN and conn first */
382 for( i = 0, sl.last = 0; sl.list[i].name; i++ ) {
383 if ( sl.list[i].name[0] == '*' ) {
384 if ( !strcmp( sl.list[i].name, slap_propnames[PROP_CONN] ) ) {
385 if ( sl.list[i].values && sl.list[i].values[0] )
386 AC_MEMCPY( &conn, sl.list[i].values[0], sizeof( conn ) );
387 if ( !sl.last ) sl.last = i;
389 if ( (flags & SASL_AUXPROP_AUTHZID) &&
390 !strcmp( sl.list[i].name, slap_propnames[PROP_AUTHZ] ) ) {
392 if ( sl.list[i].values && sl.list[i].values[0] )
393 AC_MEMCPY( &dn, sl.list[i].values[0], sizeof( dn ) );
394 if ( !sl.last ) sl.last = i;
397 if ( !strcmp( sl.list[i].name, slap_propnames[PROP_AUTHC] ) ) {
398 if ( !sl.last ) sl.last = i;
399 if ( sl.list[i].values && sl.list[i].values[0] ) {
400 AC_MEMCPY( &dn, sl.list[i].values[0], sizeof( dn ) );
401 if ( !(flags & SASL_AUXPROP_AUTHZID) )
408 /* Now see what else needs to be fetched */
409 for( i = 0; i < sl.last; i++ ) {
410 const char *name = sl.list[i].name;
412 if ( name[0] == '*' ) {
413 if ( flags & SASL_AUXPROP_AUTHZID ) continue;
415 } else if ( !(flags & SASL_AUXPROP_AUTHZID ) )
418 if ( sl.list[i].values ) {
419 if ( !(flags & SASL_AUXPROP_OVERRIDE) ) continue;
427 slap_callback cb = { slap_cb_null_response,
428 slap_cb_null_sresult, sasl_ap_lookup, NULL };
432 be = select_backend( &dn, 0, 1 );
434 if ( be && be->be_search ) {
435 op.o_tag = LDAP_REQ_SEARCH;
436 op.o_protocol = LDAP_VERSION3;
437 op.o_ndn = conn->c_ndn;
439 op.o_time = slap_get_time();
440 op.o_do_not_cache = 1;
441 op.o_threadctx = conn->c_sasl_bindop->o_threadctx;
443 (*be->be_search)( be, conn, &op, NULL, &dn,
444 LDAP_SCOPE_BASE, LDAP_DEREF_NEVER, 1, 0,
445 generic_filter, NULL, NULL, 0 );
450 static sasl_auxprop_plug_t slap_auxprop_plugin = {
453 NULL, /* glob_context */
454 NULL, /* auxprop_free */
462 const sasl_utils_t *utils,
465 sasl_auxprop_plug_t **plug,
466 const char *plugname)
468 if ( !out_version | !plug ) return SASL_BADPARAM;
470 if ( max_version < SASL_AUXPROP_PLUG_VERSION ) return SASL_BADVERS;
472 *out_version = SASL_AUXPROP_PLUG_VERSION;
473 *plug = &slap_auxprop_plugin;
477 typedef struct checkpass_info {
490 LDAPControl **ctrls )
492 slap_callback *tmp = op->o_callback;
493 checkpass_info *ci = tmp->sc_private;
497 ci->rc = SASL_NOVERIFY;
499 a = attr_find( e->e_attrs, slap_schema.si_ad_userPassword );
501 if ( ! access_allowed( be, conn, op, e, slap_schema.si_ad_userPassword,
502 NULL, ACL_AUTH, NULL ) ) return 0;
504 for ( bv = a->a_vals; bv->bv_val != NULL; bv++ ) {
505 if ( !lutil_passwd( bv, &ci->cred, NULL ) ) {
517 const char *username,
520 struct propctx *propctx)
522 Connection *conn = (Connection *)context;
530 /* SASL will fallback to its own mechanisms if we don't
531 * find an answer here.
534 rc = slap_sasl_getdn( conn, (char *)username, 0, NULL, &dn,
535 SLAP_GETDN_AUTHCID );
536 if ( rc != LDAP_SUCCESS ) {
537 sasl_seterror( sconn, 0, ldap_err2string( rc ) );
541 if ( dn.bv_len == 0 ) {
542 sasl_seterror( sconn, 0,
543 "No password is associated with the Root DSE" );
544 if ( dn.bv_val != NULL ) {
545 ch_free( dn.bv_val );
550 be = select_backend( &dn, 0, 1 );
551 if ( be && be->be_search ) {
553 slap_callback cb = { slap_cb_null_response,
554 slap_cb_null_sresult, sasl_cb_checkpass, NULL };
556 ci.cred.bv_val = (char *)pass;
557 ci.cred.bv_len = passlen;
560 op.o_tag = LDAP_REQ_SEARCH;
561 op.o_protocol = LDAP_VERSION3;
562 op.o_ndn = conn->c_ndn;
564 op.o_time = slap_get_time();
565 op.o_do_not_cache = 1;
566 op.o_threadctx = conn->c_sasl_bindop->o_threadctx;
568 (*be->be_search)( be, conn, &op, NULL, &dn,
569 LDAP_SCOPE_BASE, LDAP_DEREF_NEVER, 1, 0,
570 generic_filter, NULL, NULL, 0 );
572 if ( ci.rc != SASL_OK ) {
573 sasl_seterror( sconn, 0,
574 ldap_err2string( LDAP_INVALID_CREDENTIALS ) );
577 ch_free( dn.bv_val );
582 /* Convert a SASL authcid or authzid into a DN. Store the DN in an
583 * auxiliary property, so that we can refer to it in sasl_authorize
584 * without interfering with anything else. Also, the SASL username
585 * buffer is constrained to 256 characters, and our DNs could be
586 * much longer (totally arbitrary length)...
589 slap_sasl_canonicalize(
595 const char *user_realm,
600 Connection *conn = (Connection *)context;
601 struct propctx *props = sasl_auxprop_getctx( sconn );
602 struct propval auxvals[3];
605 const char *names[2];
610 LDAP_LOG( TRANSPORT, ENTRY,
611 "slap_sasl_canonicalize: conn %d %s=\"%s\"\n",
612 conn ? conn->c_connid : -1,
613 (flags & SASL_CU_AUTHID) ? "authcid" : "authzid", in ? in : "<empty>");
615 Debug( LDAP_DEBUG_ARGS, "SASL Canonicalize [conn=%ld]: "
617 conn ? conn->c_connid : -1,
618 (flags & SASL_CU_AUTHID) ? "authcid" : "authzid",
619 in ? in : "<empty>" );
622 /* If name is too big, just truncate. We don't care, we're
623 * using DNs, not the usernames.
625 if ( inlen > out_max )
628 /* See if we need to add request, can only do it once */
629 prop_getnames( props, slap_propnames, auxvals );
630 if ( !auxvals[0].name )
631 prop_request( props, slap_propnames );
633 if ( flags & SASL_CU_AUTHID )
638 /* Need to store the Connection for auxprop_lookup */
639 if ( !auxvals[PROP_CONN].values ) {
640 names[0] = slap_propnames[PROP_CONN];
642 prop_set( props, names[0], (char *)&conn, sizeof( conn ) );
645 /* Already been here? */
646 if ( auxvals[which].values )
649 /* Normally we require an authzID to have a u: or dn: prefix.
650 * However, SASL frequently gives us an authzID that is just
651 * an exact copy of the authcID, without a prefix. We need to
652 * detect and allow this condition. If SASL calls canonicalize
653 * with SASL_CU_AUTHID|SASL_CU_AUTHZID this is a no-brainer.
654 * But if it's broken into two calls, we need to remember the
655 * authcID so that we can compare the authzID later. We store
656 * the authcID temporarily in conn->c_sasl_dn. We necessarily
657 * finish Canonicalizing before Authorizing, so there is no
658 * conflict with slap_sasl_authorize's use of this temp var.
660 * The SASL EXTERNAL mech is backwards from all the other mechs,
661 * it does authzID before the authcID. If we see that authzID
662 * has already been done, don't do anything special with authcID.
664 if ( flags == SASL_CU_AUTHID && !auxvals[PROP_AUTHZ].values ) {
665 conn->c_sasl_dn.bv_val = (char *) in;
666 } else if ( flags == SASL_CU_AUTHZID && conn->c_sasl_dn.bv_val ) {
667 rc = strcmp( in, conn->c_sasl_dn.bv_val );
668 conn->c_sasl_dn.bv_val = NULL;
669 /* They were equal, no work needed */
670 if ( !rc ) goto done;
673 rc = slap_sasl_getdn( conn, (char *)in, inlen, (char *)user_realm, &dn,
674 (flags & SASL_CU_AUTHID) ? SLAP_GETDN_AUTHCID : SLAP_GETDN_AUTHZID );
675 if ( rc != LDAP_SUCCESS ) {
676 sasl_seterror( sconn, 0, ldap_err2string( rc ) );
680 names[0] = slap_propnames[which];
683 prop_set( props, names[0], (char *)&dn, sizeof( dn ) );
686 LDAP_LOG( TRANSPORT, ENTRY,
687 "slap_sasl_canonicalize: conn %d %s=\"%s\"\n",
688 conn ? conn->c_connid : -1, names[0]+1, dn.bv_val );
690 Debug( LDAP_DEBUG_ARGS, "SASL Canonicalize [conn=%ld]: "
692 conn ? conn->c_connid : -1,
693 names[0]+1, dn.bv_val );
695 done: AC_MEMCPY( out, in, inlen );
707 char *requested_user,
711 const char *def_realm,
713 struct propctx *props)
715 Connection *conn = (Connection *)context;
716 struct propval auxvals[3];
717 struct berval authcDN, authzDN;
721 LDAP_LOG( TRANSPORT, ENTRY,
722 "slap_sasl_authorize: conn %d authcid=\"%s\" authzid=\"%s\"\n",
723 conn ? conn->c_connid : -1, auth_identity, requested_user);
725 Debug( LDAP_DEBUG_ARGS, "SASL Authorize [conn=%ld]: "
726 "authcid=\"%s\" authzid=\"%s\"\n",
727 conn ? conn->c_connid : -1, auth_identity, requested_user );
729 if ( conn->c_sasl_dn.bv_val ) {
730 ch_free( conn->c_sasl_dn.bv_val );
731 conn->c_sasl_dn.bv_val = NULL;
732 conn->c_sasl_dn.bv_len = 0;
736 prop_getnames( props, slap_propnames+1, auxvals );
738 AC_MEMCPY( &authcDN, auxvals[0].values[0], sizeof(authcDN) );
740 /* Nothing to do if no authzID was given */
741 if ( !auxvals[1].name || !auxvals[1].values ) {
742 conn->c_sasl_dn = authcDN;
746 AC_MEMCPY( &authzDN, auxvals[1].values[0], sizeof(authzDN) );
748 rc = slap_sasl_authorized( conn, &authcDN, &authzDN );
749 ch_free( authcDN.bv_val );
750 if ( rc != LDAP_SUCCESS ) {
752 LDAP_LOG( TRANSPORT, INFO,
753 "slap_sasl_authorize: conn %ld authorization disallowed (%d)\n",
754 (long)(conn ? conn->c_connid : -1), rc, 0 );
756 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
757 " authorization disallowed (%d)\n",
758 (long) (conn ? conn->c_connid : -1), rc, 0 );
761 sasl_seterror( sconn, 0, "not authorized" );
762 ch_free( authzDN.bv_val );
766 conn->c_sasl_dn = authzDN;
768 if (conn->c_sasl_bindop) {
769 Statslog( LDAP_DEBUG_STATS,
770 "conn=%lu op=%lu BIND authcid=\"%s\"\n",
771 conn->c_connid, conn->c_sasl_bindop->o_opid,
772 auth_identity, 0, 0);
776 LDAP_LOG( TRANSPORT, ENTRY,
777 "slap_sasl_authorize: conn %d authorization allowed\n",
778 (long)(conn ? conn->c_connid : -1), 0, 0 );
780 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
781 " authorization allowed\n",
782 (long) (conn ? conn->c_connid : -1), 0, 0 );
795 struct berval authcDN, authzDN;
797 Connection *conn = context;
801 if ( conn->c_sasl_dn.bv_val ) {
802 ch_free( conn->c_sasl_dn.bv_val );
803 conn->c_sasl_dn.bv_val = NULL;
804 conn->c_sasl_dn.bv_len = 0;
808 LDAP_LOG( TRANSPORT, ENTRY,
809 "slap_sasl_authorize: conn %d authcid=\"%s\" authzid=\"%s\"\n",
810 conn ? conn->c_connid : -1, authcid ? authcid : "<empty>",
811 authzid ? authzid : "<empty>" );
813 Debug( LDAP_DEBUG_ARGS, "SASL Authorize [conn=%ld]: "
814 "authcid=\"%s\" authzid=\"%s\"\n",
815 (long) (conn ? conn->c_connid : -1),
816 authcid ? authcid : "<empty>",
817 authzid ? authzid : "<empty>" );
820 /* Figure out how much data we have for the dn */
821 rc = sasl_getprop( conn->c_sasl_context, SASL_REALM, (void **)&realm );
822 if( rc != SASL_OK && rc != SASL_NOTDONE ) {
824 LDAP_LOG( TRANSPORT, ERR,
825 "slap_sasl_authorize: getprop(REALM) failed.\n", 0, 0, 0 );
827 Debug(LDAP_DEBUG_TRACE,
828 "authorize: getprop(REALM) failed!\n", 0,0,0);
830 *errstr = "Could not extract realm";
834 /* Convert the identities to DN's. If no authzid was given, client will
835 be bound as the DN matching their username */
836 rc = slap_sasl_getdn( conn, (char *)authcid, 0, realm,
837 &authcDN, SLAP_GETDN_AUTHCID );
838 if( rc != LDAP_SUCCESS ) {
839 *errstr = ldap_err2string( rc );
842 if( ( authzid == NULL ) || !strcmp( authcid,authzid ) ) {
844 LDAP_LOG( TRANSPORT, ENTRY,
845 "slap_sasl_authorize: conn %d Using authcDN=%s\n",
846 conn ? conn->c_connid : -1, authcDN.bv_val, 0 );
848 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
849 "Using authcDN=%s\n", (long) (conn ? conn->c_connid : -1), authcDN.bv_val,0 );
852 conn->c_sasl_dn = authcDN;
855 rc = slap_sasl_getdn( conn, (char *)authzid, 0, realm,
856 &authzDN, SLAP_GETDN_AUTHZID );
857 if( rc != LDAP_SUCCESS ) {
858 ch_free( authcDN.bv_val );
859 *errstr = ldap_err2string( rc );
863 rc = slap_sasl_authorized(conn, &authcDN, &authzDN );
864 ch_free( authcDN.bv_val );
867 LDAP_LOG( TRANSPORT, INFO,
868 "slap_sasl_authorize: conn %ld authorization disallowed (%d)\n",
869 (long)(conn ? conn->c_connid : -1), rc, 0 );
871 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
872 " authorization disallowed (%d)\n",
873 (long) (conn ? conn->c_connid : -1), rc, 0 );
876 *errstr = "not authorized";
877 ch_free( authzDN.bv_val );
880 conn->c_sasl_dn = authzDN;
884 LDAP_LOG( TRANSPORT, RESULTS,
885 "slap_sasl_authorize: conn %d authorization allowed\n",
886 (long)(conn ? conn->c_connid : -1 ), 0, 0 );
888 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
889 " authorization allowed\n",
890 (long) (conn ? conn->c_connid : -1), 0, 0 );
893 if (conn->c_sasl_bindop) {
894 Statslog( LDAP_DEBUG_STATS,
895 "conn=%lu op=%lu BIND authcid=\"%s\"\n",
896 conn->c_connid, conn->c_sasl_bindop->o_opid,
903 #endif /* SASL_VERSION_MAJOR >= 2 */
906 slap_sasl_err2ldap( int saslerr )
912 rc = LDAP_SASL_BIND_IN_PROGRESS;
921 rc = LDAP_AUTH_METHOD_NOT_SUPPORTED;
924 rc = LDAP_INVALID_CREDENTIALS;
927 rc = LDAP_INSUFFICIENT_ACCESS;
931 rc = LDAP_INAPPROPRIATE_AUTH;
942 int slap_sasl_init( void )
944 #ifdef HAVE_CYRUS_SASL
946 static sasl_callback_t server_callbacks[] = {
947 { SASL_CB_LOG, &slap_sasl_log, NULL },
948 { SASL_CB_LIST_END, NULL, NULL }
951 #ifdef HAVE_SASL_VERSION
952 #define SASL_BUILD_VERSION ((SASL_VERSION_MAJOR << 24) |\
953 (SASL_VERSION_MINOR << 16) | SASL_VERSION_STEP)
955 sasl_version( NULL, &rc );
956 if ( ((rc >> 16) != ((SASL_VERSION_MAJOR << 8)|SASL_VERSION_MINOR)) ||
957 (rc & 0xffff) < SASL_VERSION_STEP) {
960 LDAP_LOG( TRANSPORT, INFO,
961 "slap_sasl_init: SASL version mismatch, got %x, wanted %x.\n",
962 rc, SASL_BUILD_VERSION, 0 );
964 Debug( LDAP_DEBUG_ANY,
965 "slap_sasl_init: SASL version mismatch, got %x, wanted %x.\n",
966 rc, SASL_BUILD_VERSION, 0 );
972 /* SASL 2 does its own memory management internally */
973 #if SASL_VERSION_MAJOR < 2
982 ldap_pvt_sasl_mutex_new,
983 ldap_pvt_sasl_mutex_lock,
984 ldap_pvt_sasl_mutex_unlock,
985 ldap_pvt_sasl_mutex_dispose );
987 #if SASL_VERSION_MAJOR >= 2
988 sasl_auxprop_add_plugin( "slapd", slap_auxprop_init );
990 /* should provide callbacks for logging */
991 /* server name should be configurable */
992 rc = sasl_server_init( server_callbacks, "slapd" );
994 if( rc != SASL_OK ) {
996 LDAP_LOG( TRANSPORT, INFO, "slap_sasl_init: init failed.\n", 0, 0, 0 );
998 Debug( LDAP_DEBUG_ANY, "sasl_server_init failed\n",
1001 #if SASL_VERSION_MAJOR < 2
1002 /* A no-op used to make sure we linked with Cyrus 1.5 */
1003 sasl_client_auth( NULL, NULL, NULL, 0, NULL, NULL );
1010 LDAP_LOG( TRANSPORT, INFO, "slap_sasl_init: initialized!\n", 0, 0, 0 );
1012 Debug( LDAP_DEBUG_TRACE, "slap_sasl_init: initialized!\n",
1017 /* default security properties */
1018 memset( &sasl_secprops, '\0', sizeof(sasl_secprops) );
1019 sasl_secprops.max_ssf = INT_MAX;
1020 sasl_secprops.maxbufsize = 65536;
1021 sasl_secprops.security_flags = SASL_SEC_NOPLAINTEXT|SASL_SEC_NOANONYMOUS;
1027 int slap_sasl_destroy( void )
1029 #ifdef HAVE_CYRUS_SASL
1032 #if SASL_VERSION_MAJOR >= 2
1033 filter_free( generic_filter );
1035 free( global_host );
1041 int slap_sasl_open( Connection *conn )
1043 int cb, sc = LDAP_SUCCESS;
1044 #if SASL_VERSION_MAJOR >= 2
1045 char *ipremoteport = NULL, *iplocalport = NULL;
1048 #ifdef HAVE_CYRUS_SASL
1049 sasl_conn_t *ctx = NULL;
1050 sasl_callback_t *session_callbacks;
1052 assert( conn->c_sasl_context == NULL );
1053 assert( conn->c_sasl_extra == NULL );
1055 conn->c_sasl_layers = 0;
1058 #if SASL_VERSION_MAJOR >= 2
1059 SLAP_CALLOC( 5, sizeof(sasl_callback_t));
1061 SLAP_CALLOC( 3, sizeof(sasl_callback_t));
1063 if( session_callbacks == NULL ) {
1065 LDAP_LOG( TRANSPORT, ERR,
1066 "slap_sasl_open: SLAP_MALLOC failed", 0, 0, 0 );
1068 Debug( LDAP_DEBUG_ANY,
1069 "slap_sasl_open: SLAP_MALLOC failed", 0, 0, 0 );
1073 conn->c_sasl_extra = session_callbacks;
1075 session_callbacks[cb=0].id = SASL_CB_LOG;
1076 session_callbacks[cb].proc = &slap_sasl_log;
1077 session_callbacks[cb++].context = conn;
1079 session_callbacks[cb].id = SASL_CB_PROXY_POLICY;
1080 session_callbacks[cb].proc = &slap_sasl_authorize;
1081 session_callbacks[cb++].context = conn;
1083 #if SASL_VERSION_MAJOR >= 2
1084 session_callbacks[cb].id = SASL_CB_CANON_USER;
1085 session_callbacks[cb].proc = &slap_sasl_canonicalize;
1086 session_callbacks[cb++].context = conn;
1088 /* XXXX: this should be conditional */
1089 session_callbacks[cb].id = SASL_CB_SERVER_USERDB_CHECKPASS;
1090 session_callbacks[cb].proc = &slap_sasl_checkpass;
1091 session_callbacks[cb++].context = conn;
1094 session_callbacks[cb].id = SASL_CB_LIST_END;
1095 session_callbacks[cb].proc = NULL;
1096 session_callbacks[cb++].context = NULL;
1098 if( global_host == NULL ) {
1099 global_host = ldap_pvt_get_fqdn( NULL );
1102 /* create new SASL context */
1103 #if SASL_VERSION_MAJOR >= 2
1104 if ( generic_filter == NULL ) {
1105 generic_filter = str2filter( "(objectclass=*)" );
1107 if ( conn->c_sock_name.bv_len != 0 &&
1108 strncmp( conn->c_sock_name.bv_val, "IP=", 3 ) == 0) {
1111 iplocalport = ch_strdup( conn->c_sock_name.bv_val + 3 );
1112 /* Convert IPv6 addresses to address;port syntax. */
1113 p = strrchr( iplocalport, ' ' );
1114 /* Convert IPv4 addresses to address;port syntax. */
1115 if ( p == NULL ) p = strchr( iplocalport, ':' );
1120 if ( conn->c_peer_name.bv_len != 0 &&
1121 strncmp( conn->c_peer_name.bv_val, "IP=", 3 ) == 0) {
1124 ipremoteport = ch_strdup( conn->c_peer_name.bv_val + 3 );
1125 /* Convert IPv6 addresses to address;port syntax. */
1126 p = strrchr( ipremoteport, ' ' );
1127 /* Convert IPv4 addresses to address;port syntax. */
1128 if ( p == NULL ) p = strchr( ipremoteport, ':' );
1133 sc = sasl_server_new( "ldap", global_host, global_realm,
1134 iplocalport, ipremoteport, session_callbacks, SASL_SUCCESS_DATA, &ctx );
1135 if ( iplocalport != NULL ) {
1136 ch_free( iplocalport );
1138 if ( ipremoteport != NULL ) {
1139 ch_free( ipremoteport );
1142 sc = sasl_server_new( "ldap", global_host, global_realm,
1143 session_callbacks, SASL_SECURITY_LAYER, &ctx );
1146 if( sc != SASL_OK ) {
1148 LDAP_LOG( TRANSPORT, ERR,
1149 "slap_sasl_open: sasl_server_new failed: %d\n", sc, 0, 0 );
1151 Debug( LDAP_DEBUG_ANY, "sasl_server_new failed: %d\n",
1158 conn->c_sasl_context = ctx;
1160 if( sc == SASL_OK ) {
1161 sc = sasl_setprop( ctx,
1162 SASL_SEC_PROPS, &sasl_secprops );
1164 if( sc != SASL_OK ) {
1166 LDAP_LOG( TRANSPORT, ERR,
1167 "slap_sasl_open: sasl_setprop failed: %d \n", sc, 0, 0 );
1169 Debug( LDAP_DEBUG_ANY, "sasl_setprop failed: %d\n",
1173 slap_sasl_close( conn );
1178 sc = slap_sasl_err2ldap( sc );
1183 int slap_sasl_external(
1186 const char *auth_id )
1188 #if SASL_VERSION_MAJOR >= 2
1190 sasl_conn_t *ctx = conn->c_sasl_context;
1192 if ( ctx == NULL ) {
1193 return LDAP_UNAVAILABLE;
1196 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL, &ssf );
1198 if ( sc != SASL_OK ) {
1202 sc = sasl_setprop( ctx, SASL_AUTH_EXTERNAL, auth_id );
1204 if ( sc != SASL_OK ) {
1208 #elif defined(HAVE_CYRUS_SASL)
1210 sasl_conn_t *ctx = conn->c_sasl_context;
1211 sasl_external_properties_t extprops;
1213 if ( ctx == NULL ) {
1214 return LDAP_UNAVAILABLE;
1217 memset( &extprops, '\0', sizeof(extprops) );
1219 extprops.auth_id = (char *) auth_id;
1221 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL,
1222 (void *) &extprops );
1224 if ( sc != SASL_OK ) {
1229 return LDAP_SUCCESS;
1232 int slap_sasl_reset( Connection *conn )
1234 #ifdef HAVE_CYRUS_SASL
1235 sasl_conn_t *ctx = conn->c_sasl_context;
1240 /* must return "anonymous" */
1241 return LDAP_SUCCESS;
1244 char ** slap_sasl_mechs( Connection *conn )
1246 char **mechs = NULL;
1248 #ifdef HAVE_CYRUS_SASL
1249 sasl_conn_t *ctx = conn->c_sasl_context;
1253 SASL_CONST char *mechstr;
1255 sc = sasl_listmech( ctx,
1256 NULL, NULL, ",", NULL,
1257 &mechstr, NULL, NULL );
1259 if( sc != SASL_OK ) {
1261 LDAP_LOG( TRANSPORT, ERR,
1262 "slap_sasl_mechs: sasl_listmech failed: %d\n", sc, 0, 0 );
1264 Debug( LDAP_DEBUG_ANY, "slap_sasl_listmech failed: %d\n",
1271 mechs = ldap_str2charray( mechstr, "," );
1273 #if SASL_VERSION_MAJOR < 2
1282 int slap_sasl_close( Connection *conn )
1284 #ifdef HAVE_CYRUS_SASL
1285 sasl_conn_t *ctx = conn->c_sasl_context;
1288 sasl_dispose( &ctx );
1291 conn->c_sasl_context = NULL;
1293 free( conn->c_sasl_extra );
1294 conn->c_sasl_extra = NULL;
1297 return LDAP_SUCCESS;
1305 struct berval *cred,
1311 #ifdef HAVE_CYRUS_SASL
1312 sasl_conn_t *ctx = conn->c_sasl_context;
1313 struct berval response;
1314 unsigned reslen = 0;
1315 const char *errstr = NULL;
1319 LDAP_LOG( TRANSPORT, ENTRY,
1320 "sasl_bind: dn=\"%s\" mech=%s datalen=%ld\n",
1321 dn->bv_len ? dn->bv_val : "",
1322 conn->c_sasl_bind_in_progress ? "<continuing>" :
1323 conn->c_sasl_bind_mech.bv_val,
1324 cred ? cred->bv_len : 0 );
1326 Debug(LDAP_DEBUG_ARGS,
1327 "==> sasl_bind: dn=\"%s\" mech=%s datalen=%ld\n",
1328 dn->bv_len ? dn->bv_val : "",
1329 conn->c_sasl_bind_in_progress ? "<continuing>":conn->c_sasl_bind_mech.bv_val,
1330 cred ? cred->bv_len : 0 );
1335 send_ldap_result( conn, op, LDAP_UNAVAILABLE,
1336 NULL, "SASL unavailable on this session", NULL, NULL );
1340 #if SASL_VERSION_MAJOR >= 2
1341 #define START( ctx, mech, cred, clen, resp, rlen, err ) \
1342 sasl_server_start( ctx, mech, cred, clen, resp, rlen )
1343 #define STEP( ctx, cred, clen, resp, rlen, err ) \
1344 sasl_server_step( ctx, cred, clen, resp, rlen )
1346 #define START( ctx, mech, cred, clen, resp, rlen, err ) \
1347 sasl_server_start( ctx, mech, cred, clen, resp, rlen, err )
1348 #define STEP( ctx, cred, clen, resp, rlen, err ) \
1349 sasl_server_step( ctx, cred, clen, resp, rlen, err )
1352 if ( !conn->c_sasl_bind_in_progress ) {
1354 conn->c_sasl_bind_mech.bv_val,
1355 cred->bv_val, cred->bv_len,
1356 (SASL_CONST char **)&response.bv_val, &reslen, &errstr );
1360 cred->bv_val, cred->bv_len,
1361 (SASL_CONST char **)&response.bv_val, &reslen, &errstr );
1364 response.bv_len = reslen;
1366 if ( sc == SASL_OK ) {
1367 sasl_ssf_t *ssf = NULL;
1369 *edn = conn->c_sasl_dn;
1370 conn->c_sasl_dn.bv_val = NULL;
1371 conn->c_sasl_dn.bv_len = 0;
1375 (void) sasl_getprop( ctx, SASL_SSF, (void *)&ssf );
1376 *ssfp = ssf ? *ssf : 0;
1379 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1380 conn->c_sasl_layers++;
1381 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1384 send_ldap_sasl( conn, op, rc,
1385 NULL, NULL, NULL, NULL,
1386 response.bv_len ? &response : NULL );
1388 } else if ( sc == SASL_CONTINUE ) {
1389 send_ldap_sasl( conn, op, rc = LDAP_SASL_BIND_IN_PROGRESS,
1390 NULL, NULL, NULL, NULL, &response );
1393 #if SASL_VERSION_MAJOR >= 2
1394 errstr = sasl_errdetail( ctx );
1396 send_ldap_result( conn, op, rc = slap_sasl_err2ldap( sc ),
1397 NULL, errstr, NULL, NULL );
1400 #if SASL_VERSION_MAJOR < 2
1401 if( response.bv_len ) {
1402 ch_free( response.bv_val );
1407 LDAP_LOG( TRANSPORT, RESULTS, "slap_sasl_bind: rc=%d\n", rc, 0, 0 );
1409 Debug(LDAP_DEBUG_TRACE, "<== slap_sasl_bind: rc=%d\n", rc, 0, 0);
1414 send_ldap_result( conn, op, rc = LDAP_UNAVAILABLE,
1415 NULL, "SASL not supported", NULL, NULL );
1421 char* slap_sasl_secprops( const char *in )
1423 #ifdef HAVE_CYRUS_SASL
1424 int rc = ldap_pvt_sasl_secprops( in, &sasl_secprops );
1426 return rc == LDAP_SUCCESS ? NULL : "Invalid security properties";
1428 return "SASL not supported";
1432 #ifdef HAVE_CYRUS_SASL
1438 struct berval *reqdata,
1440 struct berval **rspdata,
1441 LDAPControl *** rspctrls,
1445 struct berval id = { 0, NULL }; /* needs to come from connection */
1446 struct berval new = { 0, NULL };
1447 struct berval old = { 0, NULL };
1449 assert( reqoid != NULL );
1450 assert( strcmp( LDAP_EXOP_MODIFY_PASSWD, reqoid ) == 0 );
1452 rc = sasl_getprop( conn->c_sasl_context, SASL_USERNAME,
1453 (SASL_CONST void **)&id.bv_val );
1455 if( rc != SASL_OK ) {
1456 *text = "unable to retrieve SASL username";
1462 LDAP_LOG( BACKEND, ENTRY,
1463 "slap_sasl_setpass: \"%s\"\n",
1464 id.bv_val ? id.bv_val : "", 0, 0);
1466 Debug( LDAP_DEBUG_ARGS, "==> slap_sasl_setpass: \"%s\"\n",
1467 id.bv_val ? id.bv_val : "", 0, 0 );
1470 rc = slap_passwd_parse( reqdata,
1471 NULL, &old, &new, text );
1473 if( rc != LDAP_SUCCESS ) {
1477 if( new.bv_len == 0 ) {
1478 slap_passwd_generate(&new);
1480 if( new.bv_len == 0 ) {
1481 *text = "password generation failed.";
1486 *rspdata = slap_passwd_return( &new );
1489 #if SASL_VERSION_MAJOR < 2
1490 rc = sasl_setpass( conn->c_sasl_context,
1491 id.bv_val, new.bv_val, new.bv_len, 0, text );
1493 rc = sasl_setpass( conn->c_sasl_context, id.bv_val,
1494 new.bv_val, new.bv_len, old.bv_val, old.bv_len, 0 );
1495 if( rc != SASL_OK ) {
1496 *text = sasl_errdetail( conn->c_sasl_context );
1517 #endif /* HAVE_CYRUS_SASL */
1519 /* Take any sort of identity string and return a DN with the "dn:" prefix. The
1520 string returned in *dn is in its own allocated memory, and must be free'd
1521 by the calling process.
1522 -Mark Adamson, Carnegie Mellon
1524 The "dn:" prefix is no longer used anywhere inside slapd. It is only used
1525 on strings passed in directly from SASL.
1526 -Howard Chu, Symas Corp.
1532 static struct berval ext_bv = BER_BVC( "EXTERNAL" );
1534 int slap_sasl_getdn( Connection *conn, char *id, int len,
1535 char *user_realm, struct berval *dn, int flags )
1538 int rc, is_dn = 0, do_norm = 1;
1542 LDAP_LOG( TRANSPORT, ENTRY,
1543 "slap_sasl_getdn: conn %d id=%s\n",
1544 conn ? conn->c_connid : -1, id ? (*id ? id : "<empty>") : "NULL", 0 );
1546 Debug( LDAP_DEBUG_ARGS, "slap_sasl_getdn: id=%s\n",
1547 id?(*id?id:"<empty>"):"NULL",0,0 );
1554 if ( len == 0 ) len = strlen( id );
1556 /* Blatantly anonymous ID */
1557 if ( len == sizeof("anonymous") - 1 &&
1558 !strcasecmp( id, "anonymous" ) ) {
1559 return( LDAP_SUCCESS );
1565 /* An authcID needs to be converted to authzID form. Set the
1566 * values directly into *dn; they will be normalized later. (and
1567 * normalizing always makes a new copy.) An ID from a TLS certificate
1568 * is already normalized, so copy it and skip normalization.
1570 if( flags & SLAP_GETDN_AUTHCID ) {
1571 if( conn->c_sasl_bind_mech.bv_len == ext_bv.bv_len &&
1572 strcasecmp( ext_bv.bv_val, conn->c_sasl_bind_mech.bv_val ) == 0 )
1574 /* EXTERNAL DNs are already normalized */
1577 ber_str2bv( id, len, 1, dn );
1580 /* convert to u:<username> form */
1587 if( !strncasecmp( id, "u:", sizeof("u:")-1 )) {
1591 } else if ( !strncasecmp( id, "dn:", sizeof("dn:")-1) ) {
1598 /* No other possibilities from here */
1602 return( LDAP_INAPPROPRIATE_AUTH );
1605 /* Username strings */
1606 if( is_dn == SET_U ) {
1608 len = dn->bv_len + sizeof("uid=")-1 + sizeof(",cn=auth")-1;
1610 /* username may have embedded realm name */
1611 if( ( realm = strchr( dn->bv_val, '@') ) ) {
1613 len += sizeof(",cn=")-2;
1614 } else if( user_realm && *user_realm ) {
1615 len += strlen( user_realm ) + sizeof(",cn=")-1;
1618 if( conn->c_sasl_bind_mech.bv_len ) {
1619 len += conn->c_sasl_bind_mech.bv_len + sizeof(",cn=")-1;
1622 /* Build the new dn */
1624 dn->bv_val = SLAP_MALLOC( len+1 );
1625 if( dn->bv_val == NULL ) {
1627 LDAP_LOG( TRANSPORT, ERR,
1628 "slap_sasl_getdn: SLAP_MALLOC failed", 0, 0, 0 );
1630 Debug( LDAP_DEBUG_ANY,
1631 "slap_sasl_getdn: SLAP_MALLOC failed", 0, 0, 0 );
1635 p = lutil_strcopy( dn->bv_val, "uid=" );
1636 p = lutil_strncopy( p, c1, dn->bv_len );
1639 int rlen = dn->bv_len - ( realm - c1 );
1640 p = lutil_strcopy( p, ",cn=" );
1641 p = lutil_strncopy( p, realm, rlen );
1643 } else if( user_realm && *user_realm ) {
1644 p = lutil_strcopy( p, ",cn=" );
1645 p = lutil_strcopy( p, user_realm );
1648 if( conn->c_sasl_bind_mech.bv_len ) {
1649 p = lutil_strcopy( p, ",cn=" );
1650 p = lutil_strcopy( p, conn->c_sasl_bind_mech.bv_val );
1652 p = lutil_strcopy( p, ",cn=auth" );
1653 dn->bv_len = p - dn->bv_val;
1656 LDAP_LOG( TRANSPORT, ENTRY,
1657 "slap_sasl_getdn: u:id converted to %s.\n", dn->bv_val, 0, 0 );
1659 Debug( LDAP_DEBUG_TRACE, "getdn: u:id converted to %s\n", dn->bv_val,0,0 );
1663 /* All strings are in DN form now. Normalize if needed. */
1665 rc = dnNormalize2( NULL, dn, &dn2 );
1667 /* User DNs were constructed above and must be freed now */
1668 if ( is_dn == SET_U )
1669 ch_free( dn->bv_val );
1671 if ( rc != LDAP_SUCCESS ) {
1679 /* Run thru regexp */
1680 slap_sasl2dn( conn, dn, &dn2 );
1682 ch_free( dn->bv_val );
1685 LDAP_LOG( TRANSPORT, ENTRY,
1686 "slap_sasl_getdn: dn:id converted to %s.\n", dn->bv_val, 0, 0 );
1688 Debug( LDAP_DEBUG_TRACE, "getdn: dn:id converted to %s\n",
1693 return( LDAP_SUCCESS );