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>
28 #if SASL_VERSION_MAJOR >= 2
29 #include <sasl/saslplug.h>
30 #define SASL_CONST const
38 /* Flags for telling slap_sasl_getdn() what type of identity is being passed */
39 #define FLAG_GETDN_AUTHCID 2
40 #define FLAG_GETDN_AUTHZID 4
42 static sasl_security_properties_t sasl_secprops;
44 int slap_sasl_config( int cargc, char **cargv, char *line,
45 const char *fname, int lineno )
47 /* set SASL proxy authorization policy */
48 if ( strcasecmp( cargv[0], "sasl-authz-policy" ) == 0 ) {
51 LDAP_LOG( CONFIG, CRIT,
52 "%s: line %d: missing policy in \"sasl-authz-policy <policy>\" line\n",
55 Debug( LDAP_DEBUG_ANY,
56 "%s: line %d: missing policy in \"sasl-authz-policy <policy>\" line\n",
62 if ( slap_sasl_setpolicy( cargv[1] ) ) {
64 LDAP_LOG( CONFIG, CRIT,
65 "%s: line %d: unable "
66 "to parse value \"%s\" "
67 "in \"sasl-authz-policy "
69 fname, lineno, cargv[1] );
71 Debug( LDAP_DEBUG_ANY,
72 "%s: line %d: unable "
73 "to parse value \"%s\" "
74 "in \"sasl-authz-policy "
76 fname, lineno, cargv[1] );
83 } else if ( strcasecmp( cargv[0], "sasl-host" ) == 0 ) {
86 LDAP_LOG( CONFIG, CRIT,
87 "%s: line %d: missing host in \"sasl-host <host>\" line\n",
90 Debug( LDAP_DEBUG_ANY,
91 "%s: line %d: missing host in \"sasl-host <host>\" line\n",
98 if ( global_host != NULL ) {
100 LDAP_LOG( CONFIG, CRIT,
101 "%s: line %d: already set sasl-host!\n",
104 Debug( LDAP_DEBUG_ANY,
105 "%s: line %d: already set sasl-host!\n",
112 global_host = ch_strdup( cargv[1] );
116 } else if ( strcasecmp( cargv[0], "sasl-realm" ) == 0 ) {
119 LDAP_LOG( CONFIG, CRIT,
120 "%s: line %d: missing realm in \"sasl-realm <realm>\" line.\n",
123 Debug( LDAP_DEBUG_ANY,
124 "%s: line %d: missing realm in \"sasl-realm <realm>\" line\n",
131 if ( global_realm != NULL ) {
133 LDAP_LOG( CONFIG, CRIT,
134 "%s: line %d: already set sasl-realm!\n",
137 Debug( LDAP_DEBUG_ANY,
138 "%s: line %d: already set sasl-realm!\n",
145 global_realm = ch_strdup( cargv[1] );
148 } else if ( !strcasecmp( cargv[0], "sasl-regexp" )
149 || !strcasecmp( cargv[0], "saslregexp" ) )
154 LDAP_LOG( CONFIG, CRIT,
155 "%s: line %d: need 2 args in "
156 "\"saslregexp <match> <replace>\"\n",
159 Debug( LDAP_DEBUG_ANY,
160 "%s: line %d: need 2 args in \"saslregexp <match> <replace>\"\n",
166 rc = slap_sasl_regexp_config( cargv[1], cargv[2] );
171 /* SASL security properties */
172 } else if ( strcasecmp( cargv[0], "sasl-secprops" ) == 0 ) {
177 LDAP_LOG( CONFIG, CRIT,
178 "%s: line %d: missing flags in "
179 "\"sasl-secprops <properties>\" line\n",
182 Debug( LDAP_DEBUG_ANY,
183 "%s: line %d: missing flags in \"sasl-secprops <properties>\" line\n",
190 txt = slap_sasl_secprops( cargv[1] );
193 LDAP_LOG( CONFIG, CRIT,
194 "%s: line %d sasl-secprops: %s\n",
195 fname, lineno, txt );
197 Debug( LDAP_DEBUG_ANY,
198 "%s: line %d: sasl-secprops: %s\n",
199 fname, lineno, txt );
215 Connection *conn = context;
219 if ( message == NULL ) {
220 return SASL_BADPARAM;
224 #if SASL_VERSION_MAJOR >= 2
226 level = LDAP_DEBUG_NONE;
230 level = LDAP_DEBUG_ANY;
234 level = LDAP_DEBUG_ANY;
238 level = LDAP_DEBUG_TRACE;
242 level = LDAP_DEBUG_TRACE;
246 level = LDAP_DEBUG_TRACE;
250 level = LDAP_DEBUG_TRACE;
254 level = LDAP_DEBUG_TRACE;
255 label = "Password Trace";
259 level = LDAP_DEBUG_ANY;
262 case SASL_LOG_WARNING:
263 level = LDAP_DEBUG_TRACE;
267 level = LDAP_DEBUG_TRACE;
272 return SASL_BADPARAM;
276 LDAP_LOG( TRANSPORT, ENTRY,
277 "SASL [conn=%ld] %s: %s\n", conn ? conn->c_connid : -1, label, message);
279 Debug( level, "SASL [conn=%ld] %s: %s\n",
280 conn ? conn->c_connid: -1,
289 /* Take any sort of identity string and return a DN with the "dn:" prefix. The
290 string returned in *dn is in its own allocated memory, and must be free'd
291 by the calling process.
292 -Mark Adamson, Carnegie Mellon
294 The "dn:" prefix is no longer used anywhere inside slapd. It is only used
295 on strings passed in directly from SASL.
296 -Howard Chu, Symas Corp.
302 static struct berval ext_bv = BER_BVC( "EXTERNAL" );
304 int slap_sasl_getdn( Connection *conn, char *id, int len,
305 char *user_realm, struct berval *dn, int flags )
308 int rc, is_dn = 0, do_norm = 1;
313 LDAP_LOG( TRANSPORT, ENTRY,
314 "slap_sasl_getdn: conn %d id=%s\n",
315 conn ? conn->c_connid : -1, id ? (*id ? id : "<empty>") : "NULL", 0 );
317 Debug( LDAP_DEBUG_ARGS, "slap_sasl_getdn: id=%s\n",
318 id?(*id?id:"<empty>"):"NULL",0,0 );
325 if ( len == 0 ) len = strlen( id );
327 /* Blatantly anonymous ID */
328 if ( len == sizeof("anonymous") - 1 &&
329 !strcasecmp( id, "anonymous" ) ) {
330 return( LDAP_SUCCESS );
336 ctx = conn->c_sasl_context;
338 /* An authcID needs to be converted to authzID form. Set the
339 * values directly into *dn; they will be normalized later. (and
340 * normalizing always makes a new copy.) An ID from a TLS certificate
341 * is already normalized, so copy it and skip normalization.
343 if( flags & FLAG_GETDN_AUTHCID ) {
345 if( conn->c_is_tls &&
346 conn->c_sasl_bind_mech.bv_len == ext_bv.bv_len &&
347 strcasecmp( ext_bv.bv_val, conn->c_sasl_bind_mech.bv_val ) == 0 )
349 /* X.509 DN is already normalized */
352 ber_str2bv( id, len, 1, dn );
357 /* convert to u:<username> form */
364 if( !strncasecmp( id, "u:", sizeof("u:")-1 )) {
368 } else if ( !strncasecmp( id, "dn:", sizeof("dn:")-1) ) {
375 /* No other possibilities from here */
379 return( LDAP_INAPPROPRIATE_AUTH );
382 /* Username strings */
383 if( is_dn == SET_U ) {
385 len = dn->bv_len + sizeof("uid=")-1 + sizeof(",cn=auth")-1;
387 /* username may have embedded realm name */
388 if( ( realm = strchr( dn->bv_val, '@') ) ) {
390 len += sizeof(",cn=")-2;
391 } else if( user_realm && *user_realm ) {
392 len += strlen( user_realm ) + sizeof(",cn=")-1;
395 if( conn->c_sasl_bind_mech.bv_len ) {
396 len += conn->c_sasl_bind_mech.bv_len + sizeof(",cn=")-1;
399 /* Build the new dn */
401 dn->bv_val = SLAP_MALLOC( len+1 );
402 if( dn->bv_val == NULL ) {
404 LDAP_LOG( TRANSPORT, ERR,
405 "slap_sasl_getdn: SLAP_MALLOC failed", 0, 0, 0 );
407 Debug( LDAP_DEBUG_ANY,
408 "slap_sasl_getdn: SLAP_MALLOC failed", 0, 0, 0 );
412 p = lutil_strcopy( dn->bv_val, "uid=" );
413 p = lutil_strncopy( p, c1, dn->bv_len );
416 int rlen = dn->bv_len - ( realm - c1 );
417 p = lutil_strcopy( p, ",cn=" );
418 p = lutil_strncopy( p, realm, rlen );
420 } else if( user_realm && *user_realm ) {
421 p = lutil_strcopy( p, ",cn=" );
422 p = lutil_strcopy( p, user_realm );
425 if( conn->c_sasl_bind_mech.bv_len ) {
426 p = lutil_strcopy( p, ",cn=" );
427 p = lutil_strcopy( p, conn->c_sasl_bind_mech.bv_val );
429 p = lutil_strcopy( p, ",cn=auth" );
430 dn->bv_len = p - dn->bv_val;
433 LDAP_LOG( TRANSPORT, ENTRY,
434 "slap_sasl_getdn: u:id converted to %s.\n", dn->bv_val, 0, 0 );
436 Debug( LDAP_DEBUG_TRACE, "getdn: u:id converted to %s\n", dn->bv_val,0,0 );
440 /* All strings are in DN form now. Normalize if needed. */
442 rc = dnNormalize2( NULL, dn, &dn2 );
444 /* User DNs were constructed above and must be freed now */
445 if ( is_dn == SET_U )
446 ch_free( dn->bv_val );
448 if ( rc != LDAP_SUCCESS ) {
456 /* Run thru regexp */
457 slap_sasl2dn( conn, dn, &dn2 );
459 ch_free( dn->bv_val );
462 LDAP_LOG( TRANSPORT, ENTRY,
463 "slap_sasl_getdn: dn:id converted to %s.\n", dn->bv_val, 0, 0 );
465 Debug( LDAP_DEBUG_TRACE, "getdn: dn:id converted to %s\n",
470 return( LDAP_SUCCESS );
473 #if SASL_VERSION_MAJOR >= 2
474 static const char *slap_propnames[] = {
475 "*slapConn", "*authcDN", "*authzDN", NULL };
477 static Filter *generic_filter;
483 typedef struct lookup_info {
486 const struct propval *list;
487 sasl_server_params_t *sparams;
498 LDAPControl **ctrls )
501 AttributeDescription *ad;
505 slap_callback *tmp = op->o_callback;
506 lookup_info *sl = tmp->sc_private;
508 for( i = 0; i < sl->last; i++ ) {
509 const char *name = sl->list[i].name;
511 if ( name[0] == '*' ) {
512 if ( sl->flags & SASL_AUXPROP_AUTHZID ) continue;
514 } else if ( !(sl->flags & SASL_AUXPROP_AUTHZID ) )
517 if ( sl->list[i].values ) {
518 if ( !(sl->flags & SASL_AUXPROP_OVERRIDE) ) continue;
521 rc = slap_str2ad( name, &ad, &text );
522 if ( rc != LDAP_SUCCESS ) {
524 LDAP_LOG( TRANSPORT, DETAIL1,
525 "slap_auxprop: str2ad(%s): %s\n", name, text, 0 );
527 Debug( LDAP_DEBUG_TRACE,
528 "slap_auxprop: str2ad(%s): %s\n", name, text, 0 );
532 a = attr_find( e->e_attrs, ad );
534 if ( ! access_allowed( be, conn, op, e, ad, NULL, ACL_AUTH, NULL ) )
536 if ( sl->list[i].values && ( sl->flags & SASL_AUXPROP_OVERRIDE ) )
537 sl->sparams->utils->prop_erase( sl->sparams->propctx, sl->list[i].name );
538 for ( bv = a->a_vals; bv->bv_val; bv++ ) {
539 sl->sparams->utils->prop_set( sl->sparams->propctx, sl->list[i].name,
540 bv->bv_val, bv->bv_len );
549 sasl_server_params_t *sparams,
556 Connection *conn = NULL;
559 sl.list = sparams->utils->prop_get( sparams->propctx );
560 sl.sparams = sparams;
563 /* Find our DN and conn first */
564 for( i = 0, sl.last = 0; sl.list[i].name; i++ ) {
565 if ( sl.list[i].name[0] == '*' ) {
566 if ( !strcmp( sl.list[i].name, slap_propnames[PROP_CONN] ) ) {
567 if ( sl.list[i].values && sl.list[i].values[0] )
568 AC_MEMCPY( &conn, sl.list[i].values[0], sizeof( conn ) );
569 if ( !sl.last ) sl.last = i;
571 if ( (flags & SASL_AUXPROP_AUTHZID) &&
572 !strcmp( sl.list[i].name, slap_propnames[PROP_AUTHZ] ) ) {
574 if ( sl.list[i].values && sl.list[i].values[0] )
575 AC_MEMCPY( &dn, sl.list[i].values[0], sizeof( dn ) );
576 if ( !sl.last ) sl.last = i;
579 if ( !strcmp( sl.list[i].name, slap_propnames[PROP_AUTHC] ) ) {
580 if ( !sl.last ) sl.last = i;
581 if ( sl.list[i].values && sl.list[i].values[0] ) {
582 AC_MEMCPY( &dn, sl.list[i].values[0], sizeof( dn ) );
583 if ( !(flags & SASL_AUXPROP_AUTHZID) )
590 /* Now see what else needs to be fetched */
591 for( i = 0; i < sl.last; i++ ) {
592 const char *name = sl.list[i].name;
594 if ( name[0] == '*' ) {
595 if ( flags & SASL_AUXPROP_AUTHZID ) continue;
597 } else if ( !(flags & SASL_AUXPROP_AUTHZID ) )
600 if ( sl.list[i].values ) {
601 if ( !(flags & SASL_AUXPROP_OVERRIDE) ) continue;
609 slap_callback cb = { slap_cb_null_response,
610 slap_cb_null_sresult, sasl_ap_lookup, NULL };
614 be = select_backend( &dn, 0, 1 );
616 if ( be && be->be_search ) {
617 op.o_tag = LDAP_REQ_SEARCH;
618 op.o_protocol = LDAP_VERSION3;
619 op.o_ndn = conn->c_ndn;
621 op.o_time = slap_get_time();
622 op.o_do_not_cache = 1;
623 op.o_threadctx = conn->c_sasl_bindop->o_threadctx;
625 (*be->be_search)( be, conn, &op, NULL, &dn,
626 LDAP_SCOPE_BASE, LDAP_DEREF_NEVER, 1, 0,
627 generic_filter, NULL, NULL, 0 );
632 static sasl_auxprop_plug_t slap_auxprop_plugin = {
635 NULL, /* glob_context */
636 NULL, /* auxprop_free */
644 const sasl_utils_t *utils,
647 sasl_auxprop_plug_t **plug,
648 const char *plugname)
650 if ( !out_version | !plug ) return SASL_BADPARAM;
652 if ( max_version < SASL_AUXPROP_PLUG_VERSION ) return SASL_BADVERS;
654 *out_version = SASL_AUXPROP_PLUG_VERSION;
655 *plug = &slap_auxprop_plugin;
659 typedef struct checkpass_info {
672 LDAPControl **ctrls )
674 slap_callback *tmp = op->o_callback;
675 checkpass_info *ci = tmp->sc_private;
679 ci->rc = SASL_NOVERIFY;
681 a = attr_find( e->e_attrs, slap_schema.si_ad_userPassword );
683 if ( ! access_allowed( be, conn, op, e, slap_schema.si_ad_userPassword,
684 NULL, ACL_AUTH, NULL ) ) return 0;
686 for ( bv = a->a_vals; bv->bv_val != NULL; bv++ ) {
687 if ( !lutil_passwd( bv, &ci->cred, NULL ) ) {
699 const char *username,
702 struct propctx *propctx)
704 Connection *conn = (Connection *)context;
712 /* SASL will fallback to its own mechanisms if we don't
713 * find an answer here.
716 rc = slap_sasl_getdn( conn, (char *)username, 0, NULL, &dn,
717 FLAG_GETDN_AUTHCID );
718 if ( rc != LDAP_SUCCESS ) {
719 sasl_seterror( sconn, 0, ldap_err2string( rc ) );
723 if ( dn.bv_len == 0 ) {
724 sasl_seterror( sconn, 0,
725 "No password is associated with the Root DSE" );
726 if ( dn.bv_val != NULL ) {
727 ch_free( dn.bv_val );
732 be = select_backend( &dn, 0, 1 );
733 if ( be && be->be_search ) {
735 slap_callback cb = { slap_cb_null_response,
736 slap_cb_null_sresult, sasl_cb_checkpass, NULL };
738 ci.cred.bv_val = (char *)pass;
739 ci.cred.bv_len = passlen;
742 op.o_tag = LDAP_REQ_SEARCH;
743 op.o_protocol = LDAP_VERSION3;
744 op.o_ndn = conn->c_ndn;
746 op.o_time = slap_get_time();
747 op.o_do_not_cache = 1;
748 op.o_threadctx = conn->c_sasl_bindop->o_threadctx;
750 (*be->be_search)( be, conn, &op, NULL, &dn,
751 LDAP_SCOPE_BASE, LDAP_DEREF_NEVER, 1, 0,
752 generic_filter, NULL, NULL, 0 );
754 if ( ci.rc != SASL_OK ) {
755 sasl_seterror( sconn, 0,
756 ldap_err2string( LDAP_INVALID_CREDENTIALS ) );
759 ch_free( dn.bv_val );
764 /* Convert a SASL authcid or authzid into a DN. Store the DN in an
765 * auxiliary property, so that we can refer to it in sasl_authorize
766 * without interfering with anything else. Also, the SASL username
767 * buffer is constrained to 256 characters, and our DNs could be
768 * much longer (totally arbitrary length)...
771 slap_sasl_canonicalize(
777 const char *user_realm,
782 Connection *conn = (Connection *)context;
783 struct propctx *props = sasl_auxprop_getctx( sconn );
784 struct propval auxvals[3];
787 const char *names[2];
792 LDAP_LOG( TRANSPORT, ENTRY,
793 "slap_sasl_canonicalize: conn %d %s=\"%s\"\n",
794 conn ? conn->c_connid : -1,
795 (flags & SASL_CU_AUTHID) ? "authcid" : "authzid", in ? in : "<empty>");
797 Debug( LDAP_DEBUG_ARGS, "SASL Canonicalize [conn=%ld]: "
799 conn ? conn->c_connid : -1,
800 (flags & SASL_CU_AUTHID) ? "authcid" : "authzid",
801 in ? in : "<empty>" );
804 /* If name is too big, just truncate. We don't care, we're
805 * using DNs, not the usernames.
807 if ( inlen > out_max )
810 /* See if we need to add request, can only do it once */
811 prop_getnames( props, slap_propnames, auxvals );
812 if ( !auxvals[0].name )
813 prop_request( props, slap_propnames );
815 if ( flags & SASL_CU_AUTHID )
820 /* Need to store the Connection for auxprop_lookup */
821 if ( !auxvals[PROP_CONN].values ) {
822 names[0] = slap_propnames[PROP_CONN];
824 prop_set( props, names[0], (char *)&conn, sizeof( conn ) );
827 /* Already been here? */
828 if ( auxvals[which].values )
831 /* Normally we require an authzID to have a u: or dn: prefix.
832 * However, SASL frequently gives us an authzID that is just
833 * an exact copy of the authcID, without a prefix. We need to
834 * detect and allow this condition. If SASL calls canonicalize
835 * with SASL_CU_AUTHID|SASL_CU_AUTHZID this is a no-brainer.
836 * But if it's broken into two calls, we need to remember the
837 * authcID so that we can compare the authzID later. We store
838 * the authcID temporarily in conn->c_sasl_dn. We necessarily
839 * finish Canonicalizing before Authorizing, so there is no
840 * conflict with slap_sasl_authorize's use of this temp var.
842 * The SASL EXTERNAL mech is backwards from all the other mechs,
843 * it does authzID before the authcID. If we see that authzID
844 * has already been done, don't do anything special with authcID.
846 if ( flags == SASL_CU_AUTHID && !auxvals[PROP_AUTHZ].values ) {
847 conn->c_sasl_dn.bv_val = (char *) in;
848 } else if ( flags == SASL_CU_AUTHZID && conn->c_sasl_dn.bv_val ) {
849 rc = strcmp( in, conn->c_sasl_dn.bv_val );
850 conn->c_sasl_dn.bv_val = NULL;
851 /* They were equal, no work needed */
852 if ( !rc ) goto done;
855 rc = slap_sasl_getdn( conn, (char *)in, inlen, (char *)user_realm, &dn,
856 (flags & SASL_CU_AUTHID) ? FLAG_GETDN_AUTHCID : FLAG_GETDN_AUTHZID );
857 if ( rc != LDAP_SUCCESS ) {
858 sasl_seterror( sconn, 0, ldap_err2string( rc ) );
862 names[0] = slap_propnames[which];
865 prop_set( props, names[0], (char *)&dn, sizeof( dn ) );
868 LDAP_LOG( TRANSPORT, ENTRY,
869 "slap_sasl_canonicalize: conn %d %s=\"%s\"\n",
870 conn ? conn->c_connid : -1, names[0]+1, dn.bv_val );
872 Debug( LDAP_DEBUG_ARGS, "SASL Canonicalize [conn=%ld]: "
874 conn ? conn->c_connid : -1,
875 names[0]+1, dn.bv_val );
877 done: AC_MEMCPY( out, in, inlen );
889 char *requested_user,
893 const char *def_realm,
895 struct propctx *props)
897 Connection *conn = (Connection *)context;
898 struct propval auxvals[3];
899 struct berval authcDN, authzDN;
903 LDAP_LOG( TRANSPORT, ENTRY,
904 "slap_sasl_authorize: conn %d authcid=\"%s\" authzid=\"%s\"\n",
905 conn ? conn->c_connid : -1, auth_identity, requested_user);
907 Debug( LDAP_DEBUG_ARGS, "SASL Authorize [conn=%ld]: "
908 "authcid=\"%s\" authzid=\"%s\"\n",
909 conn ? conn->c_connid : -1, auth_identity, requested_user );
911 if ( conn->c_sasl_dn.bv_val ) {
912 ch_free( conn->c_sasl_dn.bv_val );
913 conn->c_sasl_dn.bv_val = NULL;
914 conn->c_sasl_dn.bv_len = 0;
918 prop_getnames( props, slap_propnames+1, auxvals );
920 AC_MEMCPY( &authcDN, auxvals[0].values[0], sizeof(authcDN) );
922 /* Nothing to do if no authzID was given */
923 if ( !auxvals[1].name || !auxvals[1].values ) {
924 conn->c_sasl_dn = authcDN;
928 AC_MEMCPY( &authzDN, auxvals[1].values[0], sizeof(authzDN) );
930 rc = slap_sasl_authorized( conn, &authcDN, &authzDN );
931 ch_free( authcDN.bv_val );
932 if ( rc != LDAP_SUCCESS ) {
934 LDAP_LOG( TRANSPORT, INFO,
935 "slap_sasl_authorize: conn %ld authorization disallowed (%d)\n",
936 (long)(conn ? conn->c_connid : -1), rc, 0 );
938 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
939 " authorization disallowed (%d)\n",
940 (long) (conn ? conn->c_connid : -1), rc, 0 );
943 sasl_seterror( sconn, 0, "not authorized" );
944 ch_free( authzDN.bv_val );
948 conn->c_sasl_dn = authzDN;
950 if (conn->c_sasl_bindop) {
951 Statslog( LDAP_DEBUG_STATS,
952 "conn=%lu op=%lu BIND authcid=\"%s\"\n",
953 conn->c_connid, conn->c_sasl_bindop->o_opid,
954 auth_identity, 0, 0);
958 LDAP_LOG( TRANSPORT, ENTRY,
959 "slap_sasl_authorize: conn %d authorization allowed\n",
960 (long)(conn ? conn->c_connid : -1), 0, 0 );
962 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
963 " authorization allowed\n",
964 (long) (conn ? conn->c_connid : -1), 0, 0 );
977 struct berval authcDN, authzDN;
979 Connection *conn = context;
983 if ( conn->c_sasl_dn.bv_val ) {
984 ch_free( conn->c_sasl_dn.bv_val );
985 conn->c_sasl_dn.bv_val = NULL;
986 conn->c_sasl_dn.bv_len = 0;
990 LDAP_LOG( TRANSPORT, ENTRY,
991 "slap_sasl_authorize: conn %d authcid=\"%s\" authzid=\"%s\"\n",
992 conn ? conn->c_connid : -1, authcid ? authcid : "<empty>",
993 authzid ? authzid : "<empty>" );
995 Debug( LDAP_DEBUG_ARGS, "SASL Authorize [conn=%ld]: "
996 "authcid=\"%s\" authzid=\"%s\"\n",
997 (long) (conn ? conn->c_connid : -1),
998 authcid ? authcid : "<empty>",
999 authzid ? authzid : "<empty>" );
1002 /* Figure out how much data we have for the dn */
1003 rc = sasl_getprop( conn->c_sasl_context, SASL_REALM, (void **)&realm );
1004 if( rc != SASL_OK && rc != SASL_NOTDONE ) {
1006 LDAP_LOG( TRANSPORT, ERR,
1007 "slap_sasl_authorize: getprop(REALM) failed.\n", 0, 0, 0 );
1009 Debug(LDAP_DEBUG_TRACE,
1010 "authorize: getprop(REALM) failed!\n", 0,0,0);
1012 *errstr = "Could not extract realm";
1013 return SASL_NOAUTHZ;
1016 /* Convert the identities to DN's. If no authzid was given, client will
1017 be bound as the DN matching their username */
1018 rc = slap_sasl_getdn( conn, (char *)authcid, 0, realm, &authcDN, FLAG_GETDN_AUTHCID );
1019 if( rc != LDAP_SUCCESS ) {
1020 *errstr = ldap_err2string( rc );
1021 return SASL_NOAUTHZ;
1023 if( ( authzid == NULL ) || !strcmp( authcid,authzid ) ) {
1025 LDAP_LOG( TRANSPORT, ENTRY,
1026 "slap_sasl_authorize: conn %d Using authcDN=%s\n",
1027 conn ? conn->c_connid : -1, authcDN.bv_val, 0 );
1029 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
1030 "Using authcDN=%s\n", (long) (conn ? conn->c_connid : -1), authcDN.bv_val,0 );
1033 conn->c_sasl_dn = authcDN;
1036 rc = slap_sasl_getdn( conn, (char *)authzid, 0, realm, &authzDN, FLAG_GETDN_AUTHZID );
1037 if( rc != LDAP_SUCCESS ) {
1038 ch_free( authcDN.bv_val );
1039 *errstr = ldap_err2string( rc );
1040 return SASL_NOAUTHZ;
1043 rc = slap_sasl_authorized(conn, &authcDN, &authzDN );
1044 ch_free( authcDN.bv_val );
1047 LDAP_LOG( TRANSPORT, INFO,
1048 "slap_sasl_authorize: conn %ld authorization disallowed (%d)\n",
1049 (long)(conn ? conn->c_connid : -1), rc, 0 );
1051 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
1052 " authorization disallowed (%d)\n",
1053 (long) (conn ? conn->c_connid : -1), rc, 0 );
1056 *errstr = "not authorized";
1057 ch_free( authzDN.bv_val );
1058 return SASL_NOAUTHZ;
1060 conn->c_sasl_dn = authzDN;
1064 LDAP_LOG( TRANSPORT, RESULTS,
1065 "slap_sasl_authorize: conn %d authorization allowed\n",
1066 (long)(conn ? conn->c_connid : -1 ), 0, 0 );
1068 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
1069 " authorization allowed\n",
1070 (long) (conn ? conn->c_connid : -1), 0, 0 );
1073 if (conn->c_sasl_bindop) {
1074 Statslog( LDAP_DEBUG_STATS,
1075 "conn=%lu op=%lu BIND authcid=\"%s\"\n",
1076 conn->c_connid, conn->c_sasl_bindop->o_opid,
1083 #endif /* SASL_VERSION_MAJOR >= 2 */
1086 slap_sasl_err2ldap( int saslerr )
1092 rc = LDAP_SASL_BIND_IN_PROGRESS;
1101 rc = LDAP_AUTH_METHOD_NOT_SUPPORTED;
1104 rc = LDAP_INVALID_CREDENTIALS;
1107 rc = LDAP_INSUFFICIENT_ACCESS;
1111 rc = LDAP_INAPPROPRIATE_AUTH;
1123 int slap_sasl_init( void )
1125 #ifdef HAVE_CYRUS_SASL
1127 static sasl_callback_t server_callbacks[] = {
1128 { SASL_CB_LOG, &slap_sasl_log, NULL },
1129 { SASL_CB_LIST_END, NULL, NULL }
1132 #ifdef HAVE_SASL_VERSION
1133 #define SASL_BUILD_VERSION ((SASL_VERSION_MAJOR << 24) |\
1134 (SASL_VERSION_MINOR << 16) | SASL_VERSION_STEP)
1136 sasl_version( NULL, &rc );
1137 if ( ((rc >> 16) != ((SASL_VERSION_MAJOR << 8)|SASL_VERSION_MINOR)) ||
1138 (rc & 0xffff) < SASL_VERSION_STEP) {
1141 LDAP_LOG( TRANSPORT, INFO,
1142 "slap_sasl_init: SASL version mismatch, got %x, wanted %x.\n",
1143 rc, SASL_BUILD_VERSION, 0 );
1145 Debug( LDAP_DEBUG_ANY,
1146 "slap_sasl_init: SASL version mismatch, got %x, wanted %x.\n",
1147 rc, SASL_BUILD_VERSION, 0 );
1153 /* SASL 2 does its own memory management internally */
1154 #if SASL_VERSION_MAJOR < 2
1163 ldap_pvt_sasl_mutex_new,
1164 ldap_pvt_sasl_mutex_lock,
1165 ldap_pvt_sasl_mutex_unlock,
1166 ldap_pvt_sasl_mutex_dispose );
1168 #if SASL_VERSION_MAJOR >= 2
1169 sasl_auxprop_add_plugin( "slapd", slap_auxprop_init );
1171 /* should provide callbacks for logging */
1172 /* server name should be configurable */
1173 rc = sasl_server_init( server_callbacks, "slapd" );
1175 if( rc != SASL_OK ) {
1177 LDAP_LOG( TRANSPORT, INFO, "slap_sasl_init: init failed.\n", 0, 0, 0 );
1179 Debug( LDAP_DEBUG_ANY, "sasl_server_init failed\n",
1182 #if SASL_VERSION_MAJOR < 2
1183 /* A no-op used to make sure we linked with Cyrus 1.5 */
1184 sasl_client_auth( NULL, NULL, NULL, 0, NULL, NULL );
1191 LDAP_LOG( TRANSPORT, INFO, "slap_sasl_init: initialized!\n", 0, 0, 0 );
1193 Debug( LDAP_DEBUG_TRACE, "slap_sasl_init: initialized!\n",
1198 /* default security properties */
1199 memset( &sasl_secprops, '\0', sizeof(sasl_secprops) );
1200 sasl_secprops.max_ssf = INT_MAX;
1201 sasl_secprops.maxbufsize = 65536;
1202 sasl_secprops.security_flags = SASL_SEC_NOPLAINTEXT|SASL_SEC_NOANONYMOUS;
1208 int slap_sasl_destroy( void )
1210 #ifdef HAVE_CYRUS_SASL
1213 #if SASL_VERSION_MAJOR >= 2
1214 filter_free( generic_filter );
1216 free( global_host );
1222 int slap_sasl_open( Connection *conn )
1224 int cb, sc = LDAP_SUCCESS;
1225 #if SASL_VERSION_MAJOR >= 2
1226 char *ipremoteport = NULL, *iplocalport = NULL;
1229 #ifdef HAVE_CYRUS_SASL
1230 sasl_conn_t *ctx = NULL;
1231 sasl_callback_t *session_callbacks;
1233 assert( conn->c_sasl_context == NULL );
1234 assert( conn->c_sasl_extra == NULL );
1236 conn->c_sasl_layers = 0;
1239 #if SASL_VERSION_MAJOR >= 2
1240 SLAP_CALLOC( 5, sizeof(sasl_callback_t));
1242 SLAP_CALLOC( 3, sizeof(sasl_callback_t));
1244 if( session_callbacks == NULL ) {
1246 LDAP_LOG( TRANSPORT, ERR,
1247 "slap_sasl_open: SLAP_MALLOC failed", 0, 0, 0 );
1249 Debug( LDAP_DEBUG_ANY,
1250 "slap_sasl_open: SLAP_MALLOC failed", 0, 0, 0 );
1254 conn->c_sasl_extra = session_callbacks;
1256 session_callbacks[cb=0].id = SASL_CB_LOG;
1257 session_callbacks[cb].proc = &slap_sasl_log;
1258 session_callbacks[cb++].context = conn;
1260 session_callbacks[cb].id = SASL_CB_PROXY_POLICY;
1261 session_callbacks[cb].proc = &slap_sasl_authorize;
1262 session_callbacks[cb++].context = conn;
1264 #if SASL_VERSION_MAJOR >= 2
1265 session_callbacks[cb].id = SASL_CB_CANON_USER;
1266 session_callbacks[cb].proc = &slap_sasl_canonicalize;
1267 session_callbacks[cb++].context = conn;
1269 /* XXXX: this should be conditional */
1270 session_callbacks[cb].id = SASL_CB_SERVER_USERDB_CHECKPASS;
1271 session_callbacks[cb].proc = &slap_sasl_checkpass;
1272 session_callbacks[cb++].context = conn;
1275 session_callbacks[cb].id = SASL_CB_LIST_END;
1276 session_callbacks[cb].proc = NULL;
1277 session_callbacks[cb++].context = NULL;
1279 if( global_host == NULL ) {
1280 global_host = ldap_pvt_get_fqdn( NULL );
1283 /* create new SASL context */
1284 #if SASL_VERSION_MAJOR >= 2
1285 if ( generic_filter == NULL ) {
1286 generic_filter = str2filter( "(objectclass=*)" );
1288 if ( conn->c_sock_name.bv_len != 0 &&
1289 strncmp( conn->c_sock_name.bv_val, "IP=", 3 ) == 0) {
1292 iplocalport = ch_strdup( conn->c_sock_name.bv_val + 3 );
1293 /* Convert IPv6 addresses to address;port syntax. */
1294 p = strrchr( iplocalport, ' ' );
1295 /* Convert IPv4 addresses to address;port syntax. */
1296 if ( p == NULL ) p = strchr( iplocalport, ':' );
1301 if ( conn->c_peer_name.bv_len != 0 &&
1302 strncmp( conn->c_peer_name.bv_val, "IP=", 3 ) == 0) {
1305 ipremoteport = ch_strdup( conn->c_peer_name.bv_val + 3 );
1306 /* Convert IPv6 addresses to address;port syntax. */
1307 p = strrchr( ipremoteport, ' ' );
1308 /* Convert IPv4 addresses to address;port syntax. */
1309 if ( p == NULL ) p = strchr( ipremoteport, ':' );
1314 sc = sasl_server_new( "ldap", global_host, global_realm,
1315 iplocalport, ipremoteport, session_callbacks, SASL_SUCCESS_DATA, &ctx );
1316 if ( iplocalport != NULL ) {
1317 ch_free( iplocalport );
1319 if ( ipremoteport != NULL ) {
1320 ch_free( ipremoteport );
1323 sc = sasl_server_new( "ldap", global_host, global_realm,
1324 session_callbacks, SASL_SECURITY_LAYER, &ctx );
1327 if( sc != SASL_OK ) {
1329 LDAP_LOG( TRANSPORT, ERR,
1330 "slap_sasl_open: sasl_server_new failed: %d\n", sc, 0, 0 );
1332 Debug( LDAP_DEBUG_ANY, "sasl_server_new failed: %d\n",
1339 conn->c_sasl_context = ctx;
1341 if( sc == SASL_OK ) {
1342 sc = sasl_setprop( ctx,
1343 SASL_SEC_PROPS, &sasl_secprops );
1345 if( sc != SASL_OK ) {
1347 LDAP_LOG( TRANSPORT, ERR,
1348 "slap_sasl_open: sasl_setprop failed: %d \n", sc, 0, 0 );
1350 Debug( LDAP_DEBUG_ANY, "sasl_setprop failed: %d\n",
1354 slap_sasl_close( conn );
1359 sc = slap_sasl_err2ldap( sc );
1364 int slap_sasl_external(
1367 const char *auth_id )
1369 #if SASL_VERSION_MAJOR >= 2
1371 sasl_conn_t *ctx = conn->c_sasl_context;
1373 if ( ctx == NULL ) {
1374 return LDAP_UNAVAILABLE;
1377 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL, &ssf );
1379 if ( sc != SASL_OK ) {
1383 sc = sasl_setprop( ctx, SASL_AUTH_EXTERNAL, auth_id );
1385 if ( sc != SASL_OK ) {
1389 #elif defined(HAVE_CYRUS_SASL)
1391 sasl_conn_t *ctx = conn->c_sasl_context;
1392 sasl_external_properties_t extprops;
1394 if ( ctx == NULL ) {
1395 return LDAP_UNAVAILABLE;
1398 memset( &extprops, '\0', sizeof(extprops) );
1400 extprops.auth_id = (char *) auth_id;
1402 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL,
1403 (void *) &extprops );
1405 if ( sc != SASL_OK ) {
1410 return LDAP_SUCCESS;
1413 int slap_sasl_reset( Connection *conn )
1415 #ifdef HAVE_CYRUS_SASL
1416 sasl_conn_t *ctx = conn->c_sasl_context;
1421 /* must return "anonymous" */
1422 return LDAP_SUCCESS;
1425 char ** slap_sasl_mechs( Connection *conn )
1427 char **mechs = NULL;
1429 #ifdef HAVE_CYRUS_SASL
1430 sasl_conn_t *ctx = conn->c_sasl_context;
1434 SASL_CONST char *mechstr;
1436 sc = sasl_listmech( ctx,
1437 NULL, NULL, ",", NULL,
1438 &mechstr, NULL, NULL );
1440 if( sc != SASL_OK ) {
1442 LDAP_LOG( TRANSPORT, ERR,
1443 "slap_sasl_mechs: sasl_listmech failed: %d\n", sc, 0, 0 );
1445 Debug( LDAP_DEBUG_ANY, "slap_sasl_listmech failed: %d\n",
1452 mechs = ldap_str2charray( mechstr, "," );
1454 #if SASL_VERSION_MAJOR < 2
1463 int slap_sasl_close( Connection *conn )
1465 #ifdef HAVE_CYRUS_SASL
1466 sasl_conn_t *ctx = conn->c_sasl_context;
1469 sasl_dispose( &ctx );
1472 conn->c_sasl_context = NULL;
1474 free( conn->c_sasl_extra );
1475 conn->c_sasl_extra = NULL;
1478 return LDAP_SUCCESS;
1486 struct berval *cred,
1492 #ifdef HAVE_CYRUS_SASL
1493 sasl_conn_t *ctx = conn->c_sasl_context;
1494 struct berval response;
1495 unsigned reslen = 0;
1496 const char *errstr = NULL;
1500 LDAP_LOG( TRANSPORT, ENTRY,
1501 "sasl_bind: dn=\"%s\" mech=%s datalen=%ld\n",
1502 dn->bv_len ? dn->bv_val : "",
1503 conn->c_sasl_bind_in_progress ? "<continuing>" :
1504 conn->c_sasl_bind_mech.bv_val,
1505 cred ? cred->bv_len : 0 );
1507 Debug(LDAP_DEBUG_ARGS,
1508 "==> sasl_bind: dn=\"%s\" mech=%s datalen=%ld\n",
1509 dn->bv_len ? dn->bv_val : "",
1510 conn->c_sasl_bind_in_progress ? "<continuing>":conn->c_sasl_bind_mech.bv_val,
1511 cred ? cred->bv_len : 0 );
1516 send_ldap_result( conn, op, LDAP_UNAVAILABLE,
1517 NULL, "SASL unavailable on this session", NULL, NULL );
1521 #if SASL_VERSION_MAJOR >= 2
1522 #define START( ctx, mech, cred, clen, resp, rlen, err ) \
1523 sasl_server_start( ctx, mech, cred, clen, resp, rlen )
1524 #define STEP( ctx, cred, clen, resp, rlen, err ) \
1525 sasl_server_step( ctx, cred, clen, resp, rlen )
1527 #define START( ctx, mech, cred, clen, resp, rlen, err ) \
1528 sasl_server_start( ctx, mech, cred, clen, resp, rlen, err )
1529 #define STEP( ctx, cred, clen, resp, rlen, err ) \
1530 sasl_server_step( ctx, cred, clen, resp, rlen, err )
1533 if ( !conn->c_sasl_bind_in_progress ) {
1535 conn->c_sasl_bind_mech.bv_val,
1536 cred->bv_val, cred->bv_len,
1537 (SASL_CONST char **)&response.bv_val, &reslen, &errstr );
1541 cred->bv_val, cred->bv_len,
1542 (SASL_CONST char **)&response.bv_val, &reslen, &errstr );
1545 response.bv_len = reslen;
1547 if ( sc == SASL_OK ) {
1548 sasl_ssf_t *ssf = NULL;
1550 *edn = conn->c_sasl_dn;
1551 conn->c_sasl_dn.bv_val = NULL;
1552 conn->c_sasl_dn.bv_len = 0;
1556 (void) sasl_getprop( ctx, SASL_SSF, (void *)&ssf );
1557 *ssfp = ssf ? *ssf : 0;
1560 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1561 conn->c_sasl_layers++;
1562 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1565 send_ldap_sasl( conn, op, rc,
1566 NULL, NULL, NULL, NULL,
1567 response.bv_len ? &response : NULL );
1569 } else if ( sc == SASL_CONTINUE ) {
1570 send_ldap_sasl( conn, op, rc = LDAP_SASL_BIND_IN_PROGRESS,
1571 NULL, NULL, NULL, NULL, &response );
1574 #if SASL_VERSION_MAJOR >= 2
1575 errstr = sasl_errdetail( ctx );
1577 send_ldap_result( conn, op, rc = slap_sasl_err2ldap( sc ),
1578 NULL, errstr, NULL, NULL );
1581 #if SASL_VERSION_MAJOR < 2
1582 if( response.bv_len ) {
1583 ch_free( response.bv_val );
1588 LDAP_LOG( TRANSPORT, RESULTS, "slap_sasl_bind: rc=%d\n", rc, 0, 0 );
1590 Debug(LDAP_DEBUG_TRACE, "<== slap_sasl_bind: rc=%d\n", rc, 0, 0);
1595 send_ldap_result( conn, op, rc = LDAP_UNAVAILABLE,
1596 NULL, "SASL not supported", NULL, NULL );
1602 char* slap_sasl_secprops( const char *in )
1604 #ifdef HAVE_CYRUS_SASL
1605 int rc = ldap_pvt_sasl_secprops( in, &sasl_secprops );
1607 return rc == LDAP_SUCCESS ? NULL : "Invalid security properties";
1609 return "SASL not supported";
1613 #ifdef HAVE_CYRUS_SASL
1619 struct berval *reqdata,
1621 struct berval **rspdata,
1622 LDAPControl *** rspctrls,
1626 struct berval id = { 0, NULL }; /* needs to come from connection */
1627 struct berval new = { 0, NULL };
1628 struct berval old = { 0, NULL };
1630 assert( reqoid != NULL );
1631 assert( strcmp( LDAP_EXOP_MODIFY_PASSWD, reqoid ) == 0 );
1633 rc = sasl_getprop( conn->c_sasl_context, SASL_USERNAME,
1634 (SASL_CONST void **)&id.bv_val );
1636 if( rc != SASL_OK ) {
1637 *text = "unable to retrieve SASL username";
1643 LDAP_LOG( BACKEND, ENTRY,
1644 "slap_sasl_setpass: \"%s\"\n",
1645 id.bv_val ? id.bv_val : "", 0, 0);
1647 Debug( LDAP_DEBUG_ARGS, "==> slap_sasl_setpass: \"%s\"\n",
1648 id.bv_val ? id.bv_val : "", 0, 0 );
1651 rc = slap_passwd_parse( reqdata,
1652 NULL, &old, &new, text );
1654 if( rc != LDAP_SUCCESS ) {
1658 if( new.bv_len == 0 ) {
1659 slap_passwd_generate(&new);
1661 if( new.bv_len == 0 ) {
1662 *text = "password generation failed.";
1667 *rspdata = slap_passwd_return( &new );
1670 #if SASL_VERSION_MAJOR < 2
1671 rc = sasl_setpass( conn->c_sasl_context,
1672 id.bv_val, new.bv_val, new.bv_len, 0, text );
1674 rc = sasl_setpass( conn->c_sasl_context, id.bv_val,
1675 new.bv_val, new.bv_len, old.bv_val, old.bv_len, 0 );
1676 if( rc != SASL_OK ) {
1677 *text = sasl_errdetail( conn->c_sasl_context );