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 if ( flags == SASL_CU_AUTHID ) {
843 conn->c_sasl_dn.bv_val = (char *) in;
844 } else if ( flags == SASL_CU_AUTHZID && conn->c_sasl_dn.bv_val ) {
845 rc = strcmp( in, conn->c_sasl_dn.bv_val );
846 conn->c_sasl_dn.bv_val = NULL;
847 /* They were equal, no work needed */
848 if ( !rc ) goto done;
851 rc = slap_sasl_getdn( conn, (char *)in, inlen, (char *)user_realm, &dn,
852 (flags & SASL_CU_AUTHID) ? FLAG_GETDN_AUTHCID : FLAG_GETDN_AUTHZID );
853 if ( rc != LDAP_SUCCESS ) {
854 sasl_seterror( sconn, 0, ldap_err2string( rc ) );
858 names[0] = slap_propnames[which];
861 prop_set( props, names[0], (char *)&dn, sizeof( dn ) );
864 LDAP_LOG( TRANSPORT, ENTRY,
865 "slap_sasl_canonicalize: conn %d %s=\"%s\"\n",
866 conn ? conn->c_connid : -1, names[0]+1, dn.bv_val );
868 Debug( LDAP_DEBUG_ARGS, "SASL Canonicalize [conn=%ld]: "
870 conn ? conn->c_connid : -1,
871 names[0]+1, dn.bv_val );
873 done: AC_MEMCPY( out, in, inlen );
885 char *requested_user,
889 const char *def_realm,
891 struct propctx *props)
893 Connection *conn = (Connection *)context;
894 struct propval auxvals[3];
895 struct berval authcDN, authzDN;
899 LDAP_LOG( TRANSPORT, ENTRY,
900 "slap_sasl_authorize: conn %d authcid=\"%s\" authzid=\"%s\"\n",
901 conn ? conn->c_connid : -1, auth_identity, requested_user);
903 Debug( LDAP_DEBUG_ARGS, "SASL Authorize [conn=%ld]: "
904 "authcid=\"%s\" authzid=\"%s\"\n",
905 conn ? conn->c_connid : -1, auth_identity, requested_user );
907 if ( conn->c_sasl_dn.bv_val ) {
908 ch_free( conn->c_sasl_dn.bv_val );
909 conn->c_sasl_dn.bv_val = NULL;
910 conn->c_sasl_dn.bv_len = 0;
914 prop_getnames( props, slap_propnames+1, auxvals );
916 AC_MEMCPY( &authcDN, auxvals[0].values[0], sizeof(authcDN) );
918 /* Nothing to do if no authzID was given */
919 if ( !auxvals[1].name || !auxvals[1].values ) {
920 conn->c_sasl_dn = authcDN;
924 AC_MEMCPY( &authzDN, auxvals[1].values[0], sizeof(authzDN) );
926 rc = slap_sasl_authorized( conn, &authcDN, &authzDN );
927 ch_free( authcDN.bv_val );
928 if ( rc != LDAP_SUCCESS ) {
930 LDAP_LOG( TRANSPORT, INFO,
931 "slap_sasl_authorize: conn %ld authorization disallowed (%d)\n",
932 (long)(conn ? conn->c_connid : -1), rc, 0 );
934 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
935 " authorization disallowed (%d)\n",
936 (long) (conn ? conn->c_connid : -1), rc, 0 );
939 sasl_seterror( sconn, 0, "not authorized" );
940 ch_free( authzDN.bv_val );
944 conn->c_sasl_dn = authzDN;
947 LDAP_LOG( TRANSPORT, ENTRY,
948 "slap_sasl_authorize: conn %d authorization allowed\n",
949 (long)(conn ? conn->c_connid : -1), 0, 0 );
951 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
952 " authorization allowed\n",
953 (long) (conn ? conn->c_connid : -1), 0, 0 );
966 struct berval authcDN, authzDN;
968 Connection *conn = context;
972 if ( conn->c_sasl_dn.bv_val ) {
973 ch_free( conn->c_sasl_dn.bv_val );
974 conn->c_sasl_dn.bv_val = NULL;
975 conn->c_sasl_dn.bv_len = 0;
979 LDAP_LOG( TRANSPORT, ENTRY,
980 "slap_sasl_authorize: conn %d authcid=\"%s\" authzid=\"%s\"\n",
981 conn ? conn->c_connid : -1, authcid ? authcid : "<empty>",
982 authzid ? authzid : "<empty>" );
984 Debug( LDAP_DEBUG_ARGS, "SASL Authorize [conn=%ld]: "
985 "authcid=\"%s\" authzid=\"%s\"\n",
986 (long) (conn ? conn->c_connid : -1),
987 authcid ? authcid : "<empty>",
988 authzid ? authzid : "<empty>" );
991 /* Figure out how much data we have for the dn */
992 rc = sasl_getprop( conn->c_sasl_context, SASL_REALM, (void **)&realm );
993 if( rc != SASL_OK && rc != SASL_NOTDONE ) {
995 LDAP_LOG( TRANSPORT, ERR,
996 "slap_sasl_authorize: getprop(REALM) failed.\n", 0, 0, 0 );
998 Debug(LDAP_DEBUG_TRACE,
999 "authorize: getprop(REALM) failed!\n", 0,0,0);
1001 *errstr = "Could not extract realm";
1002 return SASL_NOAUTHZ;
1005 /* Convert the identities to DN's. If no authzid was given, client will
1006 be bound as the DN matching their username */
1007 rc = slap_sasl_getdn( conn, (char *)authcid, 0, realm, &authcDN, FLAG_GETDN_AUTHCID );
1008 if( rc != LDAP_SUCCESS ) {
1009 *errstr = ldap_err2string( rc );
1010 return SASL_NOAUTHZ;
1012 if( ( authzid == NULL ) || !strcmp( authcid,authzid ) ) {
1014 LDAP_LOG( TRANSPORT, ENTRY,
1015 "slap_sasl_authorize: conn %d Using authcDN=%s\n",
1016 conn ? conn->c_connid : -1, authcDN.bv_val, 0 );
1018 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
1019 "Using authcDN=%s\n", (long) (conn ? conn->c_connid : -1), authcDN.bv_val,0 );
1022 conn->c_sasl_dn = authcDN;
1026 rc = slap_sasl_getdn( conn, (char *)authzid, 0, realm, &authzDN, FLAG_GETDN_AUTHZID );
1027 if( rc != LDAP_SUCCESS ) {
1028 ch_free( authcDN.bv_val );
1029 *errstr = ldap_err2string( rc );
1030 return SASL_NOAUTHZ;
1033 rc = slap_sasl_authorized(conn, &authcDN, &authzDN );
1034 ch_free( authcDN.bv_val );
1037 LDAP_LOG( TRANSPORT, INFO,
1038 "slap_sasl_authorize: conn %ld authorization disallowed (%d)\n",
1039 (long)(conn ? conn->c_connid : -1), rc, 0 );
1041 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
1042 " authorization disallowed (%d)\n",
1043 (long) (conn ? conn->c_connid : -1), rc, 0 );
1046 *errstr = "not authorized";
1047 ch_free( authzDN.bv_val );
1048 return SASL_NOAUTHZ;
1052 LDAP_LOG( TRANSPORT, RESULTS,
1053 "slap_sasl_authorize: conn %d authorization allowed\n",
1054 (long)(conn ? conn->c_connid : -1 ), 0, 0 );
1056 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
1057 " authorization allowed\n",
1058 (long) (conn ? conn->c_connid : -1), 0, 0 );
1061 conn->c_sasl_dn = authzDN;
1065 #endif /* SASL_VERSION_MAJOR >= 2 */
1068 slap_sasl_err2ldap( int saslerr )
1074 rc = LDAP_SASL_BIND_IN_PROGRESS;
1083 rc = LDAP_AUTH_METHOD_NOT_SUPPORTED;
1086 rc = LDAP_INVALID_CREDENTIALS;
1089 rc = LDAP_INSUFFICIENT_ACCESS;
1093 rc = LDAP_INAPPROPRIATE_AUTH;
1105 int slap_sasl_init( void )
1107 #ifdef HAVE_CYRUS_SASL
1109 static sasl_callback_t server_callbacks[] = {
1110 { SASL_CB_LOG, &slap_sasl_log, NULL },
1111 { SASL_CB_LIST_END, NULL, NULL }
1114 #ifdef HAVE_SASL_VERSION
1115 #define SASL_BUILD_VERSION ((SASL_VERSION_MAJOR << 24) |\
1116 (SASL_VERSION_MINOR << 16) | SASL_VERSION_STEP)
1118 sasl_version( NULL, &rc );
1119 if ( ((rc >> 16) != ((SASL_VERSION_MAJOR << 8)|SASL_VERSION_MINOR)) ||
1120 (rc & 0xffff) < SASL_VERSION_STEP) {
1123 LDAP_LOG( TRANSPORT, INFO,
1124 "slap_sasl_init: SASL version mismatch, got %x, wanted %x.\n",
1125 rc, SASL_BUILD_VERSION, 0 );
1127 Debug( LDAP_DEBUG_ANY,
1128 "slap_sasl_init: SASL version mismatch, got %x, wanted %x.\n",
1129 rc, SASL_BUILD_VERSION, 0 );
1135 /* SASL 2 does its own memory management internally */
1136 #if SASL_VERSION_MAJOR < 2
1145 ldap_pvt_sasl_mutex_new,
1146 ldap_pvt_sasl_mutex_lock,
1147 ldap_pvt_sasl_mutex_unlock,
1148 ldap_pvt_sasl_mutex_dispose );
1150 #if SASL_VERSION_MAJOR >= 2
1151 sasl_auxprop_add_plugin( "slapd", slap_auxprop_init );
1153 /* should provide callbacks for logging */
1154 /* server name should be configurable */
1155 rc = sasl_server_init( server_callbacks, "slapd" );
1157 if( rc != SASL_OK ) {
1159 LDAP_LOG( TRANSPORT, INFO, "slap_sasl_init: init failed.\n", 0, 0, 0 );
1161 Debug( LDAP_DEBUG_ANY, "sasl_server_init failed\n",
1164 #if SASL_VERSION_MAJOR < 2
1165 /* A no-op used to make sure we linked with Cyrus 1.5 */
1166 sasl_client_auth( NULL, NULL, NULL, 0, NULL, NULL );
1173 LDAP_LOG( TRANSPORT, INFO, "slap_sasl_init: initialized!\n", 0, 0, 0 );
1175 Debug( LDAP_DEBUG_TRACE, "slap_sasl_init: initialized!\n",
1180 /* default security properties */
1181 memset( &sasl_secprops, '\0', sizeof(sasl_secprops) );
1182 sasl_secprops.max_ssf = INT_MAX;
1183 sasl_secprops.maxbufsize = 65536;
1184 sasl_secprops.security_flags = SASL_SEC_NOPLAINTEXT|SASL_SEC_NOANONYMOUS;
1190 int slap_sasl_destroy( void )
1192 #ifdef HAVE_CYRUS_SASL
1195 #if SASL_VERSION_MAJOR >= 2
1196 filter_free( generic_filter );
1198 free( global_host );
1204 int slap_sasl_open( Connection *conn )
1206 int cb, sc = LDAP_SUCCESS;
1207 #if SASL_VERSION_MAJOR >= 2
1208 char *ipremoteport = NULL, *iplocalport = NULL;
1211 #ifdef HAVE_CYRUS_SASL
1212 sasl_conn_t *ctx = NULL;
1213 sasl_callback_t *session_callbacks;
1215 assert( conn->c_sasl_context == NULL );
1216 assert( conn->c_sasl_extra == NULL );
1218 conn->c_sasl_layers = 0;
1221 #if SASL_VERSION_MAJOR >= 2
1222 SLAP_CALLOC( 5, sizeof(sasl_callback_t));
1224 SLAP_CALLOC( 3, sizeof(sasl_callback_t));
1226 if( session_callbacks == NULL ) {
1228 LDAP_LOG( TRANSPORT, ERR,
1229 "slap_sasl_open: SLAP_MALLOC failed", 0, 0, 0 );
1231 Debug( LDAP_DEBUG_ANY,
1232 "slap_sasl_open: SLAP_MALLOC failed", 0, 0, 0 );
1236 conn->c_sasl_extra = session_callbacks;
1238 session_callbacks[cb=0].id = SASL_CB_LOG;
1239 session_callbacks[cb].proc = &slap_sasl_log;
1240 session_callbacks[cb++].context = conn;
1242 session_callbacks[cb].id = SASL_CB_PROXY_POLICY;
1243 session_callbacks[cb].proc = &slap_sasl_authorize;
1244 session_callbacks[cb++].context = conn;
1246 #if SASL_VERSION_MAJOR >= 2
1247 session_callbacks[cb].id = SASL_CB_CANON_USER;
1248 session_callbacks[cb].proc = &slap_sasl_canonicalize;
1249 session_callbacks[cb++].context = conn;
1251 /* XXXX: this should be conditional */
1252 session_callbacks[cb].id = SASL_CB_SERVER_USERDB_CHECKPASS;
1253 session_callbacks[cb].proc = &slap_sasl_checkpass;
1254 session_callbacks[cb++].context = conn;
1257 session_callbacks[cb].id = SASL_CB_LIST_END;
1258 session_callbacks[cb].proc = NULL;
1259 session_callbacks[cb++].context = NULL;
1261 if( global_host == NULL ) {
1262 global_host = ldap_pvt_get_fqdn( NULL );
1265 /* create new SASL context */
1266 #if SASL_VERSION_MAJOR >= 2
1267 if ( generic_filter == NULL ) {
1268 generic_filter = str2filter( "(objectclass=*)" );
1270 if ( conn->c_sock_name.bv_len != 0 &&
1271 strncmp( conn->c_sock_name.bv_val, "IP=", 3 ) == 0) {
1274 iplocalport = ch_strdup( conn->c_sock_name.bv_val + 3 );
1275 /* Convert IPv6 addresses to address;port syntax. */
1276 p = strrchr( iplocalport, ' ' );
1277 /* Convert IPv4 addresses to address;port syntax. */
1278 if ( p == NULL ) p = strchr( iplocalport, ':' );
1283 if ( conn->c_peer_name.bv_len != 0 &&
1284 strncmp( conn->c_peer_name.bv_val, "IP=", 3 ) == 0) {
1287 ipremoteport = ch_strdup( conn->c_peer_name.bv_val + 3 );
1288 /* Convert IPv6 addresses to address;port syntax. */
1289 p = strrchr( ipremoteport, ' ' );
1290 /* Convert IPv4 addresses to address;port syntax. */
1291 if ( p == NULL ) p = strchr( ipremoteport, ':' );
1296 sc = sasl_server_new( "ldap", global_host, global_realm,
1297 iplocalport, ipremoteport, session_callbacks, 0, &ctx );
1298 if ( iplocalport != NULL ) {
1299 ch_free( iplocalport );
1301 if ( ipremoteport != NULL ) {
1302 ch_free( ipremoteport );
1305 sc = sasl_server_new( "ldap", global_host, global_realm,
1306 session_callbacks, SASL_SECURITY_LAYER, &ctx );
1309 if( sc != SASL_OK ) {
1311 LDAP_LOG( TRANSPORT, ERR,
1312 "slap_sasl_open: sasl_server_new failed: %d\n", sc, 0, 0 );
1314 Debug( LDAP_DEBUG_ANY, "sasl_server_new failed: %d\n",
1321 conn->c_sasl_context = ctx;
1323 if( sc == SASL_OK ) {
1324 sc = sasl_setprop( ctx,
1325 SASL_SEC_PROPS, &sasl_secprops );
1327 if( sc != SASL_OK ) {
1329 LDAP_LOG( TRANSPORT, ERR,
1330 "slap_sasl_open: sasl_setprop failed: %d \n", sc, 0, 0 );
1332 Debug( LDAP_DEBUG_ANY, "sasl_setprop failed: %d\n",
1336 slap_sasl_close( conn );
1341 sc = slap_sasl_err2ldap( sc );
1346 int slap_sasl_external(
1349 const char *auth_id )
1351 #if SASL_VERSION_MAJOR >= 2
1353 sasl_conn_t *ctx = conn->c_sasl_context;
1355 if ( ctx == NULL ) {
1356 return LDAP_UNAVAILABLE;
1359 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL, &ssf );
1361 if ( sc != SASL_OK ) {
1365 sc = sasl_setprop( ctx, SASL_AUTH_EXTERNAL, auth_id );
1367 if ( sc != SASL_OK ) {
1371 #elif defined(HAVE_CYRUS_SASL)
1373 sasl_conn_t *ctx = conn->c_sasl_context;
1374 sasl_external_properties_t extprops;
1376 if ( ctx == NULL ) {
1377 return LDAP_UNAVAILABLE;
1380 memset( &extprops, '\0', sizeof(extprops) );
1382 extprops.auth_id = (char *) auth_id;
1384 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL,
1385 (void *) &extprops );
1387 if ( sc != SASL_OK ) {
1392 return LDAP_SUCCESS;
1395 int slap_sasl_reset( Connection *conn )
1397 #ifdef HAVE_CYRUS_SASL
1398 sasl_conn_t *ctx = conn->c_sasl_context;
1403 /* must return "anonymous" */
1404 return LDAP_SUCCESS;
1407 char ** slap_sasl_mechs( Connection *conn )
1409 char **mechs = NULL;
1411 #ifdef HAVE_CYRUS_SASL
1412 sasl_conn_t *ctx = conn->c_sasl_context;
1416 SASL_CONST char *mechstr;
1418 sc = sasl_listmech( ctx,
1419 NULL, NULL, ",", NULL,
1420 &mechstr, NULL, NULL );
1422 if( sc != SASL_OK ) {
1424 LDAP_LOG( TRANSPORT, ERR,
1425 "slap_sasl_mechs: sasl_listmech failed: %d\n", sc, 0, 0 );
1427 Debug( LDAP_DEBUG_ANY, "slap_sasl_listmech failed: %d\n",
1434 mechs = ldap_str2charray( mechstr, "," );
1436 #if SASL_VERSION_MAJOR < 2
1445 int slap_sasl_close( Connection *conn )
1447 #ifdef HAVE_CYRUS_SASL
1448 sasl_conn_t *ctx = conn->c_sasl_context;
1451 sasl_dispose( &ctx );
1454 conn->c_sasl_context = NULL;
1456 free( conn->c_sasl_extra );
1457 conn->c_sasl_extra = NULL;
1460 return LDAP_SUCCESS;
1468 struct berval *cred,
1474 #ifdef HAVE_CYRUS_SASL
1475 sasl_conn_t *ctx = conn->c_sasl_context;
1476 struct berval response;
1477 unsigned reslen = 0;
1478 const char *errstr = NULL;
1482 LDAP_LOG( TRANSPORT, ENTRY,
1483 "sasl_bind: dn=\"%s\" mech=%s datalen=%ld\n",
1484 dn->bv_len ? dn->bv_val : "",
1485 conn->c_sasl_bind_in_progress ? "<continuing>" :
1486 conn->c_sasl_bind_mech.bv_val,
1487 cred ? cred->bv_len : 0 );
1489 Debug(LDAP_DEBUG_ARGS,
1490 "==> sasl_bind: dn=\"%s\" mech=%s datalen=%ld\n",
1491 dn->bv_len ? dn->bv_val : "",
1492 conn->c_sasl_bind_in_progress ? "<continuing>":conn->c_sasl_bind_mech.bv_val,
1493 cred ? cred->bv_len : 0 );
1498 send_ldap_result( conn, op, LDAP_UNAVAILABLE,
1499 NULL, "SASL unavailable on this session", NULL, NULL );
1503 #if SASL_VERSION_MAJOR >= 2
1504 #define START( ctx, mech, cred, clen, resp, rlen, err ) \
1505 sasl_server_start( ctx, mech, cred, clen, resp, rlen )
1506 #define STEP( ctx, cred, clen, resp, rlen, err ) \
1507 sasl_server_step( ctx, cred, clen, resp, rlen )
1509 #define START( ctx, mech, cred, clen, resp, rlen, err ) \
1510 sasl_server_start( ctx, mech, cred, clen, resp, rlen, err )
1511 #define STEP( ctx, cred, clen, resp, rlen, err ) \
1512 sasl_server_step( ctx, cred, clen, resp, rlen, err )
1515 if ( !conn->c_sasl_bind_in_progress ) {
1517 conn->c_sasl_bind_mech.bv_val,
1518 cred->bv_len ? cred->bv_val : NULL,
1520 (SASL_CONST char **)&response.bv_val, &reslen, &errstr );
1524 cred->bv_val, cred->bv_len,
1525 (SASL_CONST char **)&response.bv_val, &reslen, &errstr );
1528 response.bv_len = reslen;
1530 if ( sc == SASL_OK ) {
1531 sasl_ssf_t *ssf = NULL;
1533 *edn = conn->c_sasl_dn;
1534 conn->c_sasl_dn.bv_val = NULL;
1535 conn->c_sasl_dn.bv_len = 0;
1539 (void) sasl_getprop( ctx, SASL_SSF, (void *)&ssf );
1540 *ssfp = ssf ? *ssf : 0;
1543 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1544 conn->c_sasl_layers++;
1545 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1548 send_ldap_sasl( conn, op, rc,
1549 NULL, NULL, NULL, NULL,
1550 response.bv_len ? &response : NULL );
1552 } else if ( sc == SASL_CONTINUE ) {
1553 send_ldap_sasl( conn, op, rc = LDAP_SASL_BIND_IN_PROGRESS,
1554 NULL, NULL, NULL, NULL, &response );
1557 #if SASL_VERSION_MAJOR >= 2
1558 errstr = sasl_errdetail( ctx );
1560 send_ldap_result( conn, op, rc = slap_sasl_err2ldap( sc ),
1561 NULL, errstr, NULL, NULL );
1564 #if SASL_VERSION_MAJOR < 2
1565 if( response.bv_len ) {
1566 ch_free( response.bv_val );
1571 LDAP_LOG( TRANSPORT, RESULTS, "slap_sasl_bind: rc=%d\n", rc, 0, 0 );
1573 Debug(LDAP_DEBUG_TRACE, "<== slap_sasl_bind: rc=%d\n", rc, 0, 0);
1578 send_ldap_result( conn, op, rc = LDAP_UNAVAILABLE,
1579 NULL, "SASL not supported", NULL, NULL );
1585 char* slap_sasl_secprops( const char *in )
1587 #ifdef HAVE_CYRUS_SASL
1588 int rc = ldap_pvt_sasl_secprops( in, &sasl_secprops );
1590 return rc == LDAP_SUCCESS ? NULL : "Invalid security properties";
1592 return "SASL not supported";
1596 #ifdef HAVE_CYRUS_SASL
1602 struct berval *reqdata,
1604 struct berval **rspdata,
1605 LDAPControl *** rspctrls,
1609 struct berval id = { 0, NULL }; /* needs to come from connection */
1610 struct berval new = { 0, NULL };
1611 struct berval old = { 0, NULL };
1613 assert( reqoid != NULL );
1614 assert( strcmp( LDAP_EXOP_MODIFY_PASSWD, reqoid ) == 0 );
1616 rc = sasl_getprop( conn->c_sasl_context, SASL_USERNAME,
1617 (SASL_CONST void **)&id.bv_val );
1619 if( rc != SASL_OK ) {
1620 *text = "unable to retrieve SASL username";
1626 LDAP_LOG( BACKEND, ENTRY,
1627 "slap_sasl_setpass: \"%s\"\n",
1628 id.bv_val ? id.bv_val : "", 0, 0);
1630 Debug( LDAP_DEBUG_ARGS, "==> slap_sasl_setpass: \"%s\"\n",
1631 id.bv_val ? id.bv_val : "", 0, 0 );
1634 rc = slap_passwd_parse( reqdata,
1635 NULL, &old, &new, text );
1637 if( rc != LDAP_SUCCESS ) {
1641 if( new.bv_len == 0 ) {
1642 slap_passwd_generate(&new);
1644 if( new.bv_len == 0 ) {
1645 *text = "password generation failed.";
1650 *rspdata = slap_passwd_return( &new );
1653 #if SASL_VERSION_MAJOR < 2
1654 rc = sasl_setpass( conn->c_sasl_context,
1655 id.bv_val, new.bv_val, new.bv_len, 0, text );
1657 rc = sasl_setpass( conn->c_sasl_context, id.bv_val,
1658 new.bv_val, new.bv_len, old.bv_val, old.bv_len, 0 );
1659 if( rc != SASL_OK ) {
1660 *text = sasl_errdetail( conn->c_sasl_context );