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 = ch_malloc( len+1 );
402 p = lutil_strcopy( dn->bv_val, "uid=" );
403 p = lutil_strncopy( p, c1, dn->bv_len );
406 int rlen = dn->bv_len - ( realm - c1 );
407 p = lutil_strcopy( p, ",cn=" );
408 p = lutil_strncopy( p, realm, rlen );
410 } else if( user_realm && *user_realm ) {
411 p = lutil_strcopy( p, ",cn=" );
412 p = lutil_strcopy( p, user_realm );
415 if( conn->c_sasl_bind_mech.bv_len ) {
416 p = lutil_strcopy( p, ",cn=" );
417 p = lutil_strcopy( p, conn->c_sasl_bind_mech.bv_val );
419 p = lutil_strcopy( p, ",cn=auth" );
420 dn->bv_len = p - dn->bv_val;
423 LDAP_LOG( TRANSPORT, ENTRY,
424 "slap_sasl_getdn: u:id converted to %s.\n", dn->bv_val, 0, 0 );
426 Debug( LDAP_DEBUG_TRACE, "getdn: u:id converted to %s\n", dn->bv_val,0,0 );
430 /* All strings are in DN form now. Normalize if needed. */
432 rc = dnNormalize2( NULL, dn, &dn2 );
434 /* User DNs were constructed above and must be freed now */
435 if ( is_dn == SET_U )
436 ch_free( dn->bv_val );
438 if ( rc != LDAP_SUCCESS ) {
446 /* Run thru regexp */
447 slap_sasl2dn( conn, dn, &dn2 );
449 ch_free( dn->bv_val );
452 LDAP_LOG( TRANSPORT, ENTRY,
453 "slap_sasl_getdn: dn:id converted to %s.\n", dn->bv_val, 0, 0 );
455 Debug( LDAP_DEBUG_TRACE, "getdn: dn:id converted to %s\n",
460 return( LDAP_SUCCESS );
463 #if SASL_VERSION_MAJOR >= 2
464 static const char *slap_propnames[] = {
465 "*slapConn", "*authcDN", "*authzDN", NULL };
467 static Filter *generic_filter;
473 typedef struct lookup_info {
476 const struct propval *list;
477 sasl_server_params_t *sparams;
488 LDAPControl **ctrls )
491 AttributeDescription *ad;
495 slap_callback *tmp = op->o_callback;
496 lookup_info *sl = tmp->sc_private;
498 for( i = 0; i < sl->last; i++ ) {
499 const char *name = sl->list[i].name;
501 if ( name[0] == '*' ) {
502 if ( sl->flags & SASL_AUXPROP_AUTHZID ) continue;
504 } else if ( !(sl->flags & SASL_AUXPROP_AUTHZID ) )
507 if ( sl->list[i].values ) {
508 if ( !(sl->flags & SASL_AUXPROP_OVERRIDE) ) continue;
511 rc = slap_str2ad( name, &ad, &text );
512 if ( rc != LDAP_SUCCESS ) {
514 LDAP_LOG( TRANSPORT, DETAIL1,
515 "slap_auxprop: str2ad(%s): %s\n", name, text, 0 );
517 Debug( LDAP_DEBUG_TRACE,
518 "slap_auxprop: str2ad(%s): %s\n", name, text, 0 );
522 a = attr_find( e->e_attrs, ad );
524 if ( ! access_allowed( be, conn, op, e, ad, NULL, ACL_AUTH, NULL ) )
526 if ( sl->list[i].values && ( sl->flags & SASL_AUXPROP_OVERRIDE ) )
527 sl->sparams->utils->prop_erase( sl->sparams->propctx, sl->list[i].name );
528 for ( bv = a->a_vals; bv->bv_val; bv++ ) {
529 sl->sparams->utils->prop_set( sl->sparams->propctx, sl->list[i].name,
530 bv->bv_val, bv->bv_len );
539 sasl_server_params_t *sparams,
546 Connection *conn = NULL;
549 sl.list = sparams->utils->prop_get( sparams->propctx );
550 sl.sparams = sparams;
553 /* Find our DN and conn first */
554 for( i = 0, sl.last = 0; sl.list[i].name; i++ ) {
555 if ( sl.list[i].name[0] == '*' ) {
556 if ( !strcmp( sl.list[i].name, slap_propnames[PROP_CONN] ) ) {
557 if ( sl.list[i].values && sl.list[i].values[0] )
558 AC_MEMCPY( &conn, sl.list[i].values[0], sizeof( conn ) );
559 if ( !sl.last ) sl.last = i;
561 if ( (flags & SASL_AUXPROP_AUTHZID) &&
562 !strcmp( sl.list[i].name, slap_propnames[PROP_AUTHZ] ) ) {
564 if ( sl.list[i].values && sl.list[i].values[0] )
565 AC_MEMCPY( &dn, sl.list[i].values[0], sizeof( dn ) );
566 if ( !sl.last ) sl.last = i;
569 if ( !strcmp( sl.list[i].name, slap_propnames[PROP_AUTHC] ) ) {
570 if ( !sl.last ) sl.last = i;
571 if ( sl.list[i].values && sl.list[i].values[0] ) {
572 AC_MEMCPY( &dn, sl.list[i].values[0], sizeof( dn ) );
573 if ( !(flags & SASL_AUXPROP_AUTHZID) )
580 /* Now see what else needs to be fetched */
581 for( i = 0; i < sl.last; i++ ) {
582 const char *name = sl.list[i].name;
584 if ( name[0] == '*' ) {
585 if ( flags & SASL_AUXPROP_AUTHZID ) continue;
587 } else if ( !(flags & SASL_AUXPROP_AUTHZID ) )
590 if ( sl.list[i].values ) {
591 if ( !(flags & SASL_AUXPROP_OVERRIDE) ) continue;
599 slap_callback cb = { slap_cb_null_response,
600 slap_cb_null_sresult, sasl_ap_lookup, NULL };
604 be = select_backend( &dn, 0, 1 );
606 if ( be && be->be_search ) {
607 op.o_tag = LDAP_REQ_SEARCH;
608 op.o_protocol = LDAP_VERSION3;
609 op.o_ndn = conn->c_ndn;
611 op.o_time = slap_get_time();
612 op.o_do_not_cache = 1;
613 op.o_threadctx = conn->c_sasl_bindop->o_threadctx;
615 (*be->be_search)( be, conn, &op, NULL, &dn,
616 LDAP_SCOPE_BASE, LDAP_DEREF_NEVER, 1, 0,
617 generic_filter, NULL, NULL, 0 );
622 static sasl_auxprop_plug_t slap_auxprop_plugin = {
625 NULL, /* glob_context */
626 NULL, /* auxprop_free */
634 const sasl_utils_t *utils,
637 sasl_auxprop_plug_t **plug,
638 const char *plugname)
640 if ( !out_version | !plug ) return SASL_BADPARAM;
642 if ( max_version < SASL_AUXPROP_PLUG_VERSION ) return SASL_BADVERS;
644 *out_version = SASL_AUXPROP_PLUG_VERSION;
645 *plug = &slap_auxprop_plugin;
649 typedef struct checkpass_info {
662 LDAPControl **ctrls )
664 slap_callback *tmp = op->o_callback;
665 checkpass_info *ci = tmp->sc_private;
669 ci->rc = SASL_NOVERIFY;
671 a = attr_find( e->e_attrs, slap_schema.si_ad_userPassword );
673 if ( ! access_allowed( be, conn, op, e, slap_schema.si_ad_userPassword,
674 NULL, ACL_AUTH, NULL ) ) return 0;
676 for ( bv = a->a_vals; bv->bv_val != NULL; bv++ ) {
677 if ( !lutil_passwd( bv, &ci->cred, NULL ) ) {
689 const char *username,
692 struct propctx *propctx)
694 Connection *conn = (Connection *)context;
702 /* SASL will fallback to its own mechanisms if we don't
703 * find an answer here.
706 rc = slap_sasl_getdn( conn, (char *)username, 0, NULL, &dn,
707 FLAG_GETDN_AUTHCID );
708 if ( rc != LDAP_SUCCESS ) {
709 sasl_seterror( sconn, 0, ldap_err2string( rc ) );
713 if ( dn.bv_len == 0 ) {
714 sasl_seterror( sconn, 0,
715 "No password is associated with the Root DSE" );
716 if ( dn.bv_val != NULL ) {
717 ch_free( dn.bv_val );
722 be = select_backend( &dn, 0, 1 );
723 if ( be && be->be_search ) {
725 slap_callback cb = { slap_cb_null_response,
726 slap_cb_null_sresult, sasl_cb_checkpass, NULL };
728 ci.cred.bv_val = (char *)pass;
729 ci.cred.bv_len = passlen;
732 op.o_tag = LDAP_REQ_SEARCH;
733 op.o_protocol = LDAP_VERSION3;
734 op.o_ndn = conn->c_ndn;
736 op.o_time = slap_get_time();
737 op.o_do_not_cache = 1;
738 op.o_threadctx = conn->c_sasl_bindop->o_threadctx;
740 (*be->be_search)( be, conn, &op, NULL, &dn,
741 LDAP_SCOPE_BASE, LDAP_DEREF_NEVER, 1, 0,
742 generic_filter, NULL, NULL, 0 );
744 if ( ci.rc != SASL_OK ) {
745 sasl_seterror( sconn, 0,
746 ldap_err2string( LDAP_INVALID_CREDENTIALS ) );
749 ch_free( dn.bv_val );
754 /* Convert a SASL authcid or authzid into a DN. Store the DN in an
755 * auxiliary property, so that we can refer to it in sasl_authorize
756 * without interfering with anything else. Also, the SASL username
757 * buffer is constrained to 256 characters, and our DNs could be
758 * much longer (totally arbitrary length)...
761 slap_sasl_canonicalize(
767 const char *user_realm,
772 Connection *conn = (Connection *)context;
773 struct propctx *props = sasl_auxprop_getctx( sconn );
774 struct propval auxvals[3];
777 const char *names[2];
782 LDAP_LOG( TRANSPORT, ENTRY,
783 "slap_sasl_canonicalize: conn %d %s=\"%s\"\n",
784 conn ? conn->c_connid : -1,
785 (flags & SASL_CU_AUTHID) ? "authcid" : "authzid", in ? in : "<empty>");
787 Debug( LDAP_DEBUG_ARGS, "SASL Canonicalize [conn=%ld]: "
789 conn ? conn->c_connid : -1,
790 (flags & SASL_CU_AUTHID) ? "authcid" : "authzid",
791 in ? in : "<empty>" );
794 /* If name is too big, just truncate. We don't care, we're
795 * using DNs, not the usernames.
797 if ( inlen > out_max )
800 /* See if we need to add request, can only do it once */
801 prop_getnames( props, slap_propnames, auxvals );
802 if ( !auxvals[0].name )
803 prop_request( props, slap_propnames );
805 if ( flags & SASL_CU_AUTHID )
810 /* Need to store the Connection for auxprop_lookup */
811 if ( !auxvals[PROP_CONN].values ) {
812 names[0] = slap_propnames[PROP_CONN];
814 prop_set( props, names[0], (char *)&conn, sizeof( conn ) );
817 /* Already been here? */
818 if ( auxvals[which].values )
821 /* Normally we require an authzID to have a u: or dn: prefix.
822 * However, SASL frequently gives us an authzID that is just
823 * an exact copy of the authcID, without a prefix. We need to
824 * detect and allow this condition. If SASL calls canonicalize
825 * with SASL_CU_AUTHID|SASL_CU_AUTHZID this is a no-brainer.
826 * But if it's broken into two calls, we need to remember the
827 * authcID so that we can compare the authzID later. We store
828 * the authcID temporarily in conn->c_sasl_dn. We necessarily
829 * finish Canonicalizing before Authorizing, so there is no
830 * conflict with slap_sasl_authorize's use of this temp var.
832 if ( flags == SASL_CU_AUTHID ) {
833 conn->c_sasl_dn.bv_val = in;
834 } else if ( flags == SASL_CU_AUTHZID && conn->c_sasl_dn.bv_val ) {
835 rc = strcmp( in, conn->c_sasl_dn.bv_val );
836 conn->c_sasl_dn.bv_val = NULL;
837 /* They were equal, no work needed */
838 if ( !rc ) goto done;
841 rc = slap_sasl_getdn( conn, (char *)in, inlen, (char *)user_realm, &dn,
842 (flags & SASL_CU_AUTHID) ? FLAG_GETDN_AUTHCID : FLAG_GETDN_AUTHZID );
843 if ( rc != LDAP_SUCCESS ) {
844 sasl_seterror( sconn, 0, ldap_err2string( rc ) );
848 names[0] = slap_propnames[which];
851 prop_set( props, names[0], (char *)&dn, sizeof( dn ) );
854 LDAP_LOG( TRANSPORT, ENTRY,
855 "slap_sasl_canonicalize: conn %d %s=\"%s\"\n",
856 conn ? conn->c_connid : -1, names[0]+1, dn.bv_val );
858 Debug( LDAP_DEBUG_ARGS, "SASL Canonicalize [conn=%ld]: "
860 conn ? conn->c_connid : -1,
861 names[0]+1, dn.bv_val );
863 done: AC_MEMCPY( out, in, inlen );
875 char *requested_user,
879 const char *def_realm,
881 struct propctx *props)
883 Connection *conn = (Connection *)context;
884 struct propval auxvals[3];
885 struct berval authcDN, authzDN;
889 LDAP_LOG( TRANSPORT, ENTRY,
890 "slap_sasl_authorize: conn %d authcid=\"%s\" authzid=\"%s\"\n",
891 conn ? conn->c_connid : -1, auth_identity, requested_user);
893 Debug( LDAP_DEBUG_ARGS, "SASL Authorize [conn=%ld]: "
894 "authcid=\"%s\" authzid=\"%s\"\n",
895 conn ? conn->c_connid : -1, auth_identity, requested_user );
897 if ( conn->c_sasl_dn.bv_val ) {
898 ch_free( conn->c_sasl_dn.bv_val );
899 conn->c_sasl_dn.bv_val = NULL;
900 conn->c_sasl_dn.bv_len = 0;
904 prop_getnames( props, slap_propnames+1, auxvals );
906 AC_MEMCPY( &authcDN, auxvals[0].values[0], sizeof(authcDN) );
908 /* Nothing to do if no authzID was given */
909 if ( !auxvals[1].name || !auxvals[1].values ) {
910 conn->c_sasl_dn = authcDN;
914 AC_MEMCPY( &authzDN, auxvals[1].values[0], sizeof(authzDN) );
916 rc = slap_sasl_authorized( conn, &authcDN, &authzDN );
917 ch_free( authcDN.bv_val );
918 if ( rc != LDAP_SUCCESS ) {
920 LDAP_LOG( TRANSPORT, INFO,
921 "slap_sasl_authorize: conn %ld authorization disallowed (%d)\n",
922 (long)(conn ? conn->c_connid : -1), rc, 0 );
924 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
925 " authorization disallowed (%d)\n",
926 (long) (conn ? conn->c_connid : -1), rc, 0 );
929 sasl_seterror( sconn, 0, "not authorized" );
930 ch_free( authzDN.bv_val );
934 conn->c_sasl_dn = authzDN;
937 LDAP_LOG( TRANSPORT, ENTRY,
938 "slap_sasl_authorize: conn %d authorization allowed\n",
939 (long)(conn ? conn->c_connid : -1), 0, 0 );
941 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
942 " authorization allowed\n",
943 (long) (conn ? conn->c_connid : -1), 0, 0 );
956 struct berval authcDN, authzDN;
958 Connection *conn = context;
962 if ( conn->c_sasl_dn.bv_val ) {
963 ch_free( conn->c_sasl_dn.bv_val );
964 conn->c_sasl_dn.bv_val = NULL;
965 conn->c_sasl_dn.bv_len = 0;
969 LDAP_LOG( TRANSPORT, ENTRY,
970 "slap_sasl_authorize: conn %d authcid=\"%s\" authzid=\"%s\"\n",
971 conn ? conn->c_connid : -1, authcid ? authcid : "<empty>",
972 authzid ? authzid : "<empty>" );
974 Debug( LDAP_DEBUG_ARGS, "SASL Authorize [conn=%ld]: "
975 "authcid=\"%s\" authzid=\"%s\"\n",
976 (long) (conn ? conn->c_connid : -1),
977 authcid ? authcid : "<empty>",
978 authzid ? authzid : "<empty>" );
981 /* Figure out how much data we have for the dn */
982 rc = sasl_getprop( conn->c_sasl_context, SASL_REALM, (void **)&realm );
983 if( rc != SASL_OK && rc != SASL_NOTDONE ) {
985 LDAP_LOG( TRANSPORT, ERR,
986 "slap_sasl_authorize: getprop(REALM) failed.\n", 0, 0, 0 );
988 Debug(LDAP_DEBUG_TRACE,
989 "authorize: getprop(REALM) failed!\n", 0,0,0);
991 *errstr = "Could not extract realm";
995 /* Convert the identities to DN's. If no authzid was given, client will
996 be bound as the DN matching their username */
997 rc = slap_sasl_getdn( conn, (char *)authcid, 0, realm, &authcDN, FLAG_GETDN_AUTHCID );
998 if( rc != LDAP_SUCCESS ) {
999 *errstr = ldap_err2string( rc );
1000 return SASL_NOAUTHZ;
1002 if( ( authzid == NULL ) || !strcmp( authcid,authzid ) ) {
1004 LDAP_LOG( TRANSPORT, ENTRY,
1005 "slap_sasl_authorize: conn %d Using authcDN=%s\n",
1006 conn ? conn->c_connid : -1, authcDN.bv_val, 0 );
1008 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
1009 "Using authcDN=%s\n", (long) (conn ? conn->c_connid : -1), authcDN.bv_val,0 );
1012 conn->c_sasl_dn = authcDN;
1016 rc = slap_sasl_getdn( conn, (char *)authzid, 0, realm, &authzDN, FLAG_GETDN_AUTHZID );
1017 if( rc != LDAP_SUCCESS ) {
1018 ch_free( authcDN.bv_val );
1019 *errstr = ldap_err2string( rc );
1020 return SASL_NOAUTHZ;
1023 rc = slap_sasl_authorized(conn, &authcDN, &authzDN );
1024 ch_free( authcDN.bv_val );
1027 LDAP_LOG( TRANSPORT, INFO,
1028 "slap_sasl_authorize: conn %ld authorization disallowed (%d)\n",
1029 (long)(conn ? conn->c_connid : -1), rc, 0 );
1031 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
1032 " authorization disallowed (%d)\n",
1033 (long) (conn ? conn->c_connid : -1), rc, 0 );
1036 *errstr = "not authorized";
1037 ch_free( authzDN.bv_val );
1038 return SASL_NOAUTHZ;
1042 LDAP_LOG( TRANSPORT, RESULTS,
1043 "slap_sasl_authorize: conn %d authorization allowed\n",
1044 (long)(conn ? conn->c_connid : -1 ), 0, 0 );
1046 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
1047 " authorization allowed\n",
1048 (long) (conn ? conn->c_connid : -1), 0, 0 );
1051 conn->c_sasl_dn = authzDN;
1055 #endif /* SASL_VERSION_MAJOR >= 2 */
1058 slap_sasl_err2ldap( int saslerr )
1064 rc = LDAP_SASL_BIND_IN_PROGRESS;
1073 rc = LDAP_AUTH_METHOD_NOT_SUPPORTED;
1076 rc = LDAP_INVALID_CREDENTIALS;
1079 rc = LDAP_INSUFFICIENT_ACCESS;
1083 rc = LDAP_INAPPROPRIATE_AUTH;
1095 int slap_sasl_init( void )
1097 #ifdef HAVE_CYRUS_SASL
1099 static sasl_callback_t server_callbacks[] = {
1100 { SASL_CB_LOG, &slap_sasl_log, NULL },
1101 { SASL_CB_LIST_END, NULL, NULL }
1104 #ifdef HAVE_SASL_VERSION
1105 #define SASL_BUILD_VERSION ((SASL_VERSION_MAJOR << 24) |\
1106 (SASL_VERSION_MINOR << 16) | SASL_VERSION_STEP)
1108 sasl_version( NULL, &rc );
1109 if ( ((rc >> 16) != ((SASL_VERSION_MAJOR << 8)|SASL_VERSION_MINOR)) ||
1110 (rc & 0xffff) < SASL_VERSION_STEP) {
1113 LDAP_LOG( TRANSPORT, INFO,
1114 "slap_sasl_init: SASL version mismatch, got %x, wanted %x.\n",
1115 rc, SASL_BUILD_VERSION, 0 );
1117 Debug( LDAP_DEBUG_ANY,
1118 "slap_sasl_init: SASL version mismatch, got %x, wanted %x.\n",
1119 rc, SASL_BUILD_VERSION, 0 );
1125 /* SASL 2 does its own memory management internally */
1126 #if SASL_VERSION_MAJOR < 2
1135 ldap_pvt_sasl_mutex_new,
1136 ldap_pvt_sasl_mutex_lock,
1137 ldap_pvt_sasl_mutex_unlock,
1138 ldap_pvt_sasl_mutex_dispose );
1140 #if SASL_VERSION_MAJOR >= 2
1141 sasl_auxprop_add_plugin( "slapd", slap_auxprop_init );
1143 /* should provide callbacks for logging */
1144 /* server name should be configurable */
1145 rc = sasl_server_init( server_callbacks, "slapd" );
1147 if( rc != SASL_OK ) {
1149 LDAP_LOG( TRANSPORT, INFO, "slap_sasl_init: init failed.\n", 0, 0, 0 );
1151 Debug( LDAP_DEBUG_ANY, "sasl_server_init failed\n",
1154 #if SASL_VERSION_MAJOR < 2
1155 /* A no-op used to make sure we linked with Cyrus 1.5 */
1156 sasl_client_auth( NULL, NULL, NULL, 0, NULL, NULL );
1163 LDAP_LOG( TRANSPORT, INFO, "slap_sasl_init: initialized!\n", 0, 0, 0 );
1165 Debug( LDAP_DEBUG_TRACE, "slap_sasl_init: initialized!\n",
1170 /* default security properties */
1171 memset( &sasl_secprops, '\0', sizeof(sasl_secprops) );
1172 sasl_secprops.max_ssf = INT_MAX;
1173 sasl_secprops.maxbufsize = 65536;
1174 sasl_secprops.security_flags = SASL_SEC_NOPLAINTEXT|SASL_SEC_NOANONYMOUS;
1180 int slap_sasl_destroy( void )
1182 #ifdef HAVE_CYRUS_SASL
1185 #if SASL_VERSION_MAJOR >= 2
1186 filter_free( generic_filter );
1188 free( global_host );
1194 int slap_sasl_open( Connection *conn )
1196 int cb, sc = LDAP_SUCCESS;
1197 #if SASL_VERSION_MAJOR >= 2
1198 char *ipremoteport = NULL, *iplocalport = NULL;
1201 #ifdef HAVE_CYRUS_SASL
1202 sasl_conn_t *ctx = NULL;
1203 sasl_callback_t *session_callbacks;
1205 assert( conn->c_sasl_context == NULL );
1206 assert( conn->c_sasl_extra == NULL );
1208 conn->c_sasl_layers = 0;
1211 #if SASL_VERSION_MAJOR >= 2
1212 ch_calloc( 5, sizeof(sasl_callback_t));
1214 ch_calloc( 3, sizeof(sasl_callback_t));
1216 conn->c_sasl_extra = session_callbacks;
1218 session_callbacks[cb=0].id = SASL_CB_LOG;
1219 session_callbacks[cb].proc = &slap_sasl_log;
1220 session_callbacks[cb++].context = conn;
1222 session_callbacks[cb].id = SASL_CB_PROXY_POLICY;
1223 session_callbacks[cb].proc = &slap_sasl_authorize;
1224 session_callbacks[cb++].context = conn;
1226 #if SASL_VERSION_MAJOR >= 2
1227 session_callbacks[cb].id = SASL_CB_CANON_USER;
1228 session_callbacks[cb].proc = &slap_sasl_canonicalize;
1229 session_callbacks[cb++].context = conn;
1231 /* XXXX: this should be conditional */
1232 session_callbacks[cb].id = SASL_CB_SERVER_USERDB_CHECKPASS;
1233 session_callbacks[cb].proc = &slap_sasl_checkpass;
1234 session_callbacks[cb++].context = conn;
1237 session_callbacks[cb].id = SASL_CB_LIST_END;
1238 session_callbacks[cb].proc = NULL;
1239 session_callbacks[cb++].context = NULL;
1241 if( global_host == NULL ) {
1242 global_host = ldap_pvt_get_fqdn( NULL );
1245 /* create new SASL context */
1246 #if SASL_VERSION_MAJOR >= 2
1247 if ( generic_filter == NULL ) {
1248 generic_filter = str2filter( "(objectclass=*)" );
1250 if ( conn->c_sock_name.bv_len != 0 &&
1251 strncmp( conn->c_sock_name.bv_val, "IP=", 3 ) == 0) {
1254 iplocalport = ch_strdup( conn->c_sock_name.bv_val + 3 );
1255 /* Convert IPv6 addresses to address;port syntax. */
1256 p = strrchr( iplocalport, ' ' );
1257 /* Convert IPv4 addresses to address;port syntax. */
1258 if ( p == NULL ) p = strchr( iplocalport, ':' );
1263 if ( conn->c_peer_name.bv_len != 0 &&
1264 strncmp( conn->c_peer_name.bv_val, "IP=", 3 ) == 0) {
1267 ipremoteport = ch_strdup( conn->c_peer_name.bv_val + 3 );
1268 /* Convert IPv6 addresses to address;port syntax. */
1269 p = strrchr( ipremoteport, ' ' );
1270 /* Convert IPv4 addresses to address;port syntax. */
1271 if ( p == NULL ) p = strchr( ipremoteport, ':' );
1276 sc = sasl_server_new( "ldap", global_host, global_realm,
1277 iplocalport, ipremoteport, session_callbacks, 0, &ctx );
1278 if ( iplocalport != NULL ) {
1279 ch_free( iplocalport );
1281 if ( ipremoteport != NULL ) {
1282 ch_free( ipremoteport );
1285 sc = sasl_server_new( "ldap", global_host, global_realm,
1286 session_callbacks, SASL_SECURITY_LAYER, &ctx );
1289 if( sc != SASL_OK ) {
1291 LDAP_LOG( TRANSPORT, ERR,
1292 "slap_sasl_open: sasl_server_new failed: %d\n", sc, 0, 0 );
1294 Debug( LDAP_DEBUG_ANY, "sasl_server_new failed: %d\n",
1301 conn->c_sasl_context = ctx;
1303 if( sc == SASL_OK ) {
1304 sc = sasl_setprop( ctx,
1305 SASL_SEC_PROPS, &sasl_secprops );
1307 if( sc != SASL_OK ) {
1309 LDAP_LOG( TRANSPORT, ERR,
1310 "slap_sasl_open: sasl_setprop failed: %d \n", sc, 0, 0 );
1312 Debug( LDAP_DEBUG_ANY, "sasl_setprop failed: %d\n",
1316 slap_sasl_close( conn );
1321 sc = slap_sasl_err2ldap( sc );
1326 int slap_sasl_external(
1329 const char *auth_id )
1331 #if SASL_VERSION_MAJOR >= 2
1333 sasl_conn_t *ctx = conn->c_sasl_context;
1335 if ( ctx == NULL ) {
1336 return LDAP_UNAVAILABLE;
1339 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL, &ssf );
1341 if ( sc != SASL_OK ) {
1345 sc = sasl_setprop( ctx, SASL_AUTH_EXTERNAL, auth_id );
1347 if ( sc != SASL_OK ) {
1351 #elif defined(HAVE_CYRUS_SASL)
1353 sasl_conn_t *ctx = conn->c_sasl_context;
1354 sasl_external_properties_t extprops;
1356 if ( ctx == NULL ) {
1357 return LDAP_UNAVAILABLE;
1360 memset( &extprops, '\0', sizeof(extprops) );
1362 extprops.auth_id = (char *) auth_id;
1364 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL,
1365 (void *) &extprops );
1367 if ( sc != SASL_OK ) {
1372 return LDAP_SUCCESS;
1375 int slap_sasl_reset( Connection *conn )
1377 #ifdef HAVE_CYRUS_SASL
1378 sasl_conn_t *ctx = conn->c_sasl_context;
1383 /* must return "anonymous" */
1384 return LDAP_SUCCESS;
1387 char ** slap_sasl_mechs( Connection *conn )
1389 char **mechs = NULL;
1391 #ifdef HAVE_CYRUS_SASL
1392 sasl_conn_t *ctx = conn->c_sasl_context;
1396 SASL_CONST char *mechstr;
1398 sc = sasl_listmech( ctx,
1399 NULL, NULL, ",", NULL,
1400 &mechstr, NULL, NULL );
1402 if( sc != SASL_OK ) {
1404 LDAP_LOG( TRANSPORT, ERR,
1405 "slap_sasl_mechs: sasl_listmech failed: %d\n", sc, 0, 0 );
1407 Debug( LDAP_DEBUG_ANY, "slap_sasl_listmech failed: %d\n",
1414 mechs = ldap_str2charray( mechstr, "," );
1416 #if SASL_VERSION_MAJOR < 2
1425 int slap_sasl_close( Connection *conn )
1427 #ifdef HAVE_CYRUS_SASL
1428 sasl_conn_t *ctx = conn->c_sasl_context;
1431 sasl_dispose( &ctx );
1434 conn->c_sasl_context = NULL;
1436 free( conn->c_sasl_extra );
1437 conn->c_sasl_extra = NULL;
1440 return LDAP_SUCCESS;
1448 struct berval *cred,
1454 #ifdef HAVE_CYRUS_SASL
1455 sasl_conn_t *ctx = conn->c_sasl_context;
1456 struct berval response;
1457 unsigned reslen = 0;
1458 const char *errstr = NULL;
1462 LDAP_LOG( TRANSPORT, ENTRY,
1463 "sasl_bind: dn=\"%s\" mech=%s datalen=%ld\n",
1464 dn->bv_len ? dn->bv_val : "",
1465 conn->c_sasl_bind_in_progress ? "<continuing>" :
1466 conn->c_sasl_bind_mech.bv_val,
1467 cred ? cred->bv_len : 0 );
1469 Debug(LDAP_DEBUG_ARGS,
1470 "==> sasl_bind: dn=\"%s\" mech=%s datalen=%ld\n",
1471 dn->bv_len ? dn->bv_val : "",
1472 conn->c_sasl_bind_in_progress ? "<continuing>":conn->c_sasl_bind_mech.bv_val,
1473 cred ? cred->bv_len : 0 );
1478 send_ldap_result( conn, op, LDAP_UNAVAILABLE,
1479 NULL, "SASL unavailable on this session", NULL, NULL );
1483 #if SASL_VERSION_MAJOR >= 2
1484 #define START( ctx, mech, cred, clen, resp, rlen, err ) \
1485 sasl_server_start( ctx, mech, cred, clen, resp, rlen )
1486 #define STEP( ctx, cred, clen, resp, rlen, err ) \
1487 sasl_server_step( ctx, cred, clen, resp, rlen )
1489 #define START( ctx, mech, cred, clen, resp, rlen, err ) \
1490 sasl_server_start( ctx, mech, cred, clen, resp, rlen, err )
1491 #define STEP( ctx, cred, clen, resp, rlen, err ) \
1492 sasl_server_step( ctx, cred, clen, resp, rlen, err )
1495 if ( !conn->c_sasl_bind_in_progress ) {
1497 conn->c_sasl_bind_mech.bv_val,
1498 cred->bv_len ? cred->bv_val : "",
1500 (SASL_CONST char **)&response.bv_val, &reslen, &errstr );
1504 cred->bv_val, cred->bv_len,
1505 (SASL_CONST char **)&response.bv_val, &reslen, &errstr );
1508 response.bv_len = reslen;
1510 if ( sc == SASL_OK ) {
1511 sasl_ssf_t *ssf = NULL;
1513 *edn = conn->c_sasl_dn;
1514 conn->c_sasl_dn.bv_val = NULL;
1515 conn->c_sasl_dn.bv_len = 0;
1519 (void) sasl_getprop( ctx, SASL_SSF, (void *)&ssf );
1520 *ssfp = ssf ? *ssf : 0;
1523 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1524 conn->c_sasl_layers++;
1525 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1528 send_ldap_sasl( conn, op, rc,
1529 NULL, NULL, NULL, NULL,
1530 response.bv_len ? &response : NULL );
1532 } else if ( sc == SASL_CONTINUE ) {
1533 send_ldap_sasl( conn, op, rc = LDAP_SASL_BIND_IN_PROGRESS,
1534 NULL, NULL, NULL, NULL, &response );
1537 #if SASL_VERSION_MAJOR >= 2
1538 errstr = sasl_errdetail( ctx );
1540 send_ldap_result( conn, op, rc = slap_sasl_err2ldap( sc ),
1541 NULL, errstr, NULL, NULL );
1544 #if SASL_VERSION_MAJOR < 2
1545 if( response.bv_len ) {
1546 ch_free( response.bv_val );
1551 LDAP_LOG( TRANSPORT, RESULTS, "slap_sasl_bind: rc=%d\n", rc, 0, 0 );
1553 Debug(LDAP_DEBUG_TRACE, "<== slap_sasl_bind: rc=%d\n", rc, 0, 0);
1558 send_ldap_result( conn, op, rc = LDAP_UNAVAILABLE,
1559 NULL, "SASL not supported", NULL, NULL );
1565 char* slap_sasl_secprops( const char *in )
1567 #ifdef HAVE_CYRUS_SASL
1568 int rc = ldap_pvt_sasl_secprops( in, &sasl_secprops );
1570 return rc == LDAP_SUCCESS ? NULL : "Invalid security properties";
1572 return "SASL not supported";
1576 #ifdef HAVE_CYRUS_SASL
1582 struct berval *reqdata,
1584 struct berval **rspdata,
1585 LDAPControl *** rspctrls,
1589 struct berval id = { 0, NULL }; /* needs to come from connection */
1590 struct berval new = { 0, NULL };
1591 struct berval old = { 0, NULL };
1593 assert( reqoid != NULL );
1594 assert( strcmp( LDAP_EXOP_MODIFY_PASSWD, reqoid ) == 0 );
1596 rc = sasl_getprop( conn->c_sasl_context, SASL_USERNAME,
1597 (SASL_CONST void **)&id.bv_val );
1599 if( rc != SASL_OK ) {
1600 *text = "unable to retrieve SASL username";
1606 LDAP_LOG( BACKEND, ENTRY,
1607 "slap_sasl_setpass: \"%s\"\n",
1608 id.bv_val ? id.bv_val : "", 0, 0);
1610 Debug( LDAP_DEBUG_ARGS, "==> slap_sasl_setpass: \"%s\"\n",
1611 id.bv_val ? id.bv_val : "", 0, 0 );
1614 rc = slap_passwd_parse( reqdata,
1615 NULL, &old, &new, text );
1617 if( rc != LDAP_SUCCESS ) {
1621 if( new.bv_len == 0 ) {
1622 slap_passwd_generate(&new);
1624 if( new.bv_len == 0 ) {
1625 *text = "password generation failed.";
1630 *rspdata = slap_passwd_return( &new );
1633 #if SASL_VERSION_MAJOR < 2
1634 rc = sasl_setpass( conn->c_sasl_context,
1635 id.bv_val, new.bv_val, new.bv_len, 0, text );
1637 rc = sasl_setpass( conn->c_sasl_context, id.bv_val,
1638 new.bv_val, new.bv_len, old.bv_val, old.bv_len, 0 );
1639 if( rc != SASL_OK ) {
1640 *text = sasl_errdetail( conn->c_sasl_context );