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 if ( flags == SASL_CU_AUTHZID ) {
822 /* If we got unqualified authzid's, they probably came from SASL
823 * itself just passing the authcid to us. Look inside the oparams
824 * structure to see if that's true. (HACK: the out_len pointer is
825 * the address of a member of a sasl_out_params_t structure...)
827 sasl_out_params_t dummy;
828 int offset = (void *)&dummy.ulen - (void *)&dummy.authid;
829 char **authid = (void *)out_len - offset;
830 if ( *authid && !strcmp( in, *authid ) )
834 rc = slap_sasl_getdn( conn, (char *)in, inlen, (char *)user_realm, &dn,
835 (flags & SASL_CU_AUTHID) ? FLAG_GETDN_AUTHCID : FLAG_GETDN_AUTHZID );
836 if ( rc != LDAP_SUCCESS ) {
837 sasl_seterror( sconn, 0, ldap_err2string( rc ) );
841 names[0] = slap_propnames[which];
844 prop_set( props, names[0], (char *)&dn, sizeof( dn ) );
847 LDAP_LOG( TRANSPORT, ENTRY,
848 "slap_sasl_canonicalize: conn %d %s=\"%s\"\n",
849 conn ? conn->c_connid : -1, names[0]+1, dn.bv_val );
851 Debug( LDAP_DEBUG_ARGS, "SASL Canonicalize [conn=%ld]: "
853 conn ? conn->c_connid : -1,
854 names[0]+1, dn.bv_val );
856 done: AC_MEMCPY( out, in, inlen );
868 char *requested_user,
872 const char *def_realm,
874 struct propctx *props)
876 Connection *conn = (Connection *)context;
877 struct propval auxvals[3];
878 struct berval authcDN, authzDN;
882 LDAP_LOG( TRANSPORT, ENTRY,
883 "slap_sasl_authorize: conn %d authcid=\"%s\" authzid=\"%s\"\n",
884 conn ? conn->c_connid : -1, auth_identity, requested_user);
886 Debug( LDAP_DEBUG_ARGS, "SASL Authorize [conn=%ld]: "
887 "authcid=\"%s\" authzid=\"%s\"\n",
888 conn ? conn->c_connid : -1, auth_identity, requested_user );
890 if ( conn->c_sasl_dn.bv_val ) {
891 ch_free( conn->c_sasl_dn.bv_val );
892 conn->c_sasl_dn.bv_val = NULL;
893 conn->c_sasl_dn.bv_len = 0;
897 prop_getnames( props, slap_propnames+1, auxvals );
899 AC_MEMCPY( &authcDN, auxvals[0].values[0], sizeof(authcDN) );
901 /* Nothing to do if no authzID was given */
902 if ( !auxvals[1].name || !auxvals[1].values ) {
903 conn->c_sasl_dn = authcDN;
907 AC_MEMCPY( &authzDN, auxvals[1].values[0], sizeof(authzDN) );
909 rc = slap_sasl_authorized( conn, &authcDN, &authzDN );
910 ch_free( authcDN.bv_val );
911 if ( rc != LDAP_SUCCESS ) {
913 LDAP_LOG( TRANSPORT, INFO,
914 "slap_sasl_authorize: conn %ld authorization disallowed (%d)\n",
915 (long)(conn ? conn->c_connid : -1), rc, 0 );
917 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
918 " authorization disallowed (%d)\n",
919 (long) (conn ? conn->c_connid : -1), rc, 0 );
922 sasl_seterror( sconn, 0, "not authorized" );
923 ch_free( authzDN.bv_val );
927 conn->c_sasl_dn = authzDN;
930 LDAP_LOG( TRANSPORT, ENTRY,
931 "slap_sasl_authorize: conn %d authorization allowed\n",
932 (long)(conn ? conn->c_connid : -1), 0, 0 );
934 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
935 " authorization allowed\n",
936 (long) (conn ? conn->c_connid : -1), 0, 0 );
949 struct berval authcDN, authzDN;
951 Connection *conn = context;
955 if ( conn->c_sasl_dn.bv_val ) {
956 ch_free( conn->c_sasl_dn.bv_val );
957 conn->c_sasl_dn.bv_val = NULL;
958 conn->c_sasl_dn.bv_len = 0;
962 LDAP_LOG( TRANSPORT, ENTRY,
963 "slap_sasl_authorize: conn %d authcid=\"%s\" authzid=\"%s\"\n",
964 conn ? conn->c_connid : -1, authcid ? authcid : "<empty>",
965 authzid ? authzid : "<empty>" );
967 Debug( LDAP_DEBUG_ARGS, "SASL Authorize [conn=%ld]: "
968 "authcid=\"%s\" authzid=\"%s\"\n",
969 (long) (conn ? conn->c_connid : -1),
970 authcid ? authcid : "<empty>",
971 authzid ? authzid : "<empty>" );
974 /* Figure out how much data we have for the dn */
975 rc = sasl_getprop( conn->c_sasl_context, SASL_REALM, (void **)&realm );
976 if( rc != SASL_OK && rc != SASL_NOTDONE ) {
978 LDAP_LOG( TRANSPORT, ERR,
979 "slap_sasl_authorize: getprop(REALM) failed.\n", 0, 0, 0 );
981 Debug(LDAP_DEBUG_TRACE,
982 "authorize: getprop(REALM) failed!\n", 0,0,0);
984 *errstr = "Could not extract realm";
988 /* Convert the identities to DN's. If no authzid was given, client will
989 be bound as the DN matching their username */
990 rc = slap_sasl_getdn( conn, (char *)authcid, 0, realm, &authcDN, FLAG_GETDN_AUTHCID );
991 if( rc != LDAP_SUCCESS ) {
992 *errstr = ldap_err2string( rc );
995 if( ( authzid == NULL ) || !strcmp( authcid,authzid ) ) {
997 LDAP_LOG( TRANSPORT, ENTRY,
998 "slap_sasl_authorize: conn %d Using authcDN=%s\n",
999 conn ? conn->c_connid : -1, authcDN.bv_val, 0 );
1001 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
1002 "Using authcDN=%s\n", (long) (conn ? conn->c_connid : -1), authcDN.bv_val,0 );
1005 conn->c_sasl_dn = authcDN;
1009 rc = slap_sasl_getdn( conn, (char *)authzid, 0, realm, &authzDN, FLAG_GETDN_AUTHZID );
1010 if( rc != LDAP_SUCCESS ) {
1011 ch_free( authcDN.bv_val );
1012 *errstr = ldap_err2string( rc );
1013 return SASL_NOAUTHZ;
1016 rc = slap_sasl_authorized(conn, &authcDN, &authzDN );
1017 ch_free( authcDN.bv_val );
1020 LDAP_LOG( TRANSPORT, INFO,
1021 "slap_sasl_authorize: conn %ld authorization disallowed (%d)\n",
1022 (long)(conn ? conn->c_connid : -1), rc, 0 );
1024 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
1025 " authorization disallowed (%d)\n",
1026 (long) (conn ? conn->c_connid : -1), rc, 0 );
1029 *errstr = "not authorized";
1030 ch_free( authzDN.bv_val );
1031 return SASL_NOAUTHZ;
1035 LDAP_LOG( TRANSPORT, RESULTS,
1036 "slap_sasl_authorize: conn %d authorization allowed\n",
1037 (long)(conn ? conn->c_connid : -1 ), 0, 0 );
1039 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
1040 " authorization allowed\n",
1041 (long) (conn ? conn->c_connid : -1), 0, 0 );
1044 conn->c_sasl_dn = authzDN;
1048 #endif /* SASL_VERSION_MAJOR >= 2 */
1051 slap_sasl_err2ldap( int saslerr )
1057 rc = LDAP_SASL_BIND_IN_PROGRESS;
1066 rc = LDAP_AUTH_METHOD_NOT_SUPPORTED;
1069 rc = LDAP_INVALID_CREDENTIALS;
1072 rc = LDAP_INSUFFICIENT_ACCESS;
1076 rc = LDAP_INAPPROPRIATE_AUTH;
1088 int slap_sasl_init( void )
1090 #ifdef HAVE_CYRUS_SASL
1092 static sasl_callback_t server_callbacks[] = {
1093 { SASL_CB_LOG, &slap_sasl_log, NULL },
1094 { SASL_CB_LIST_END, NULL, NULL }
1097 #ifdef HAVE_SASL_VERSION
1098 #define SASL_BUILD_VERSION ((SASL_VERSION_MAJOR << 24) |\
1099 (SASL_VERSION_MINOR << 16) | SASL_VERSION_STEP)
1101 sasl_version( NULL, &rc );
1102 if ( ((rc >> 16) != ((SASL_VERSION_MAJOR << 8)|SASL_VERSION_MINOR)) ||
1103 (rc & 0xffff) < SASL_VERSION_STEP) {
1106 LDAP_LOG( TRANSPORT, INFO,
1107 "slap_sasl_init: SASL version mismatch, got %x, wanted %x.\n",
1108 rc, SASL_BUILD_VERSION, 0 );
1110 Debug( LDAP_DEBUG_ANY,
1111 "slap_sasl_init: SASL version mismatch, got %x, wanted %x.\n",
1112 rc, SASL_BUILD_VERSION, 0 );
1118 /* SASL 2 does its own memory management internally */
1119 #if SASL_VERSION_MAJOR < 2
1128 ldap_pvt_sasl_mutex_new,
1129 ldap_pvt_sasl_mutex_lock,
1130 ldap_pvt_sasl_mutex_unlock,
1131 ldap_pvt_sasl_mutex_dispose );
1133 #if SASL_VERSION_MAJOR >= 2
1134 sasl_auxprop_add_plugin( "slapd", slap_auxprop_init );
1136 /* should provide callbacks for logging */
1137 /* server name should be configurable */
1138 rc = sasl_server_init( server_callbacks, "slapd" );
1140 if( rc != SASL_OK ) {
1142 LDAP_LOG( TRANSPORT, INFO, "slap_sasl_init: init failed.\n", 0, 0, 0 );
1144 Debug( LDAP_DEBUG_ANY, "sasl_server_init failed\n",
1147 #if SASL_VERSION_MAJOR < 2
1148 /* A no-op used to make sure we linked with Cyrus 1.5 */
1149 sasl_client_auth( NULL, NULL, NULL, 0, NULL, NULL );
1156 LDAP_LOG( TRANSPORT, INFO, "slap_sasl_init: initialized!\n", 0, 0, 0 );
1158 Debug( LDAP_DEBUG_TRACE, "slap_sasl_init: initialized!\n",
1163 /* default security properties */
1164 memset( &sasl_secprops, '\0', sizeof(sasl_secprops) );
1165 sasl_secprops.max_ssf = INT_MAX;
1166 sasl_secprops.maxbufsize = 65536;
1167 sasl_secprops.security_flags = SASL_SEC_NOPLAINTEXT|SASL_SEC_NOANONYMOUS;
1173 int slap_sasl_destroy( void )
1175 #ifdef HAVE_CYRUS_SASL
1178 #if SASL_VERSION_MAJOR >= 2
1179 filter_free( generic_filter );
1181 free( global_host );
1187 int slap_sasl_open( Connection *conn )
1189 int cb, sc = LDAP_SUCCESS;
1190 #if SASL_VERSION_MAJOR >= 2
1191 char *ipremoteport = NULL, *iplocalport = NULL;
1194 #ifdef HAVE_CYRUS_SASL
1195 sasl_conn_t *ctx = NULL;
1196 sasl_callback_t *session_callbacks;
1198 assert( conn->c_sasl_context == NULL );
1199 assert( conn->c_sasl_extra == NULL );
1201 conn->c_sasl_layers = 0;
1204 #if SASL_VERSION_MAJOR >= 2
1205 ch_calloc( 5, sizeof(sasl_callback_t));
1207 ch_calloc( 3, sizeof(sasl_callback_t));
1209 conn->c_sasl_extra = session_callbacks;
1211 session_callbacks[cb=0].id = SASL_CB_LOG;
1212 session_callbacks[cb].proc = &slap_sasl_log;
1213 session_callbacks[cb++].context = conn;
1215 session_callbacks[cb].id = SASL_CB_PROXY_POLICY;
1216 session_callbacks[cb].proc = &slap_sasl_authorize;
1217 session_callbacks[cb++].context = conn;
1219 #if SASL_VERSION_MAJOR >= 2
1220 session_callbacks[cb].id = SASL_CB_CANON_USER;
1221 session_callbacks[cb].proc = &slap_sasl_canonicalize;
1222 session_callbacks[cb++].context = conn;
1224 /* XXXX: this should be conditional */
1225 session_callbacks[cb].id = SASL_CB_SERVER_USERDB_CHECKPASS;
1226 session_callbacks[cb].proc = &slap_sasl_checkpass;
1227 session_callbacks[cb++].context = conn;
1230 session_callbacks[cb].id = SASL_CB_LIST_END;
1231 session_callbacks[cb].proc = NULL;
1232 session_callbacks[cb++].context = NULL;
1234 if( global_host == NULL ) {
1235 global_host = ldap_pvt_get_fqdn( NULL );
1238 /* create new SASL context */
1239 #if SASL_VERSION_MAJOR >= 2
1240 if ( generic_filter == NULL ) {
1241 generic_filter = str2filter( "(objectclass=*)" );
1243 if ( conn->c_sock_name.bv_len != 0 &&
1244 strncmp( conn->c_sock_name.bv_val, "IP=", 3 ) == 0) {
1247 iplocalport = ch_strdup( conn->c_sock_name.bv_val + 3 );
1248 /* Convert IPv6 addresses to address;port syntax. */
1249 p = strrchr( iplocalport, ' ' );
1250 /* Convert IPv4 addresses to address;port syntax. */
1251 if ( p == NULL ) p = strchr( iplocalport, ':' );
1256 if ( conn->c_peer_name.bv_len != 0 &&
1257 strncmp( conn->c_peer_name.bv_val, "IP=", 3 ) == 0) {
1260 ipremoteport = ch_strdup( conn->c_peer_name.bv_val + 3 );
1261 /* Convert IPv6 addresses to address;port syntax. */
1262 p = strrchr( ipremoteport, ' ' );
1263 /* Convert IPv4 addresses to address;port syntax. */
1264 if ( p == NULL ) p = strchr( ipremoteport, ':' );
1269 sc = sasl_server_new( "ldap", global_host, global_realm,
1270 iplocalport, ipremoteport, session_callbacks, 0, &ctx );
1271 if ( iplocalport != NULL ) {
1272 ch_free( iplocalport );
1274 if ( ipremoteport != NULL ) {
1275 ch_free( ipremoteport );
1278 sc = sasl_server_new( "ldap", global_host, global_realm,
1279 session_callbacks, SASL_SECURITY_LAYER, &ctx );
1282 if( sc != SASL_OK ) {
1284 LDAP_LOG( TRANSPORT, ERR,
1285 "slap_sasl_open: sasl_server_new failed: %d\n", sc, 0, 0 );
1287 Debug( LDAP_DEBUG_ANY, "sasl_server_new failed: %d\n",
1294 conn->c_sasl_context = ctx;
1296 if( sc == SASL_OK ) {
1297 sc = sasl_setprop( ctx,
1298 SASL_SEC_PROPS, &sasl_secprops );
1300 if( sc != SASL_OK ) {
1302 LDAP_LOG( TRANSPORT, ERR,
1303 "slap_sasl_open: sasl_setprop failed: %d \n", sc, 0, 0 );
1305 Debug( LDAP_DEBUG_ANY, "sasl_setprop failed: %d\n",
1309 slap_sasl_close( conn );
1314 sc = slap_sasl_err2ldap( sc );
1319 int slap_sasl_external(
1322 const char *auth_id )
1324 #if SASL_VERSION_MAJOR >= 2
1326 sasl_conn_t *ctx = conn->c_sasl_context;
1328 if ( ctx == NULL ) {
1329 return LDAP_UNAVAILABLE;
1332 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL, &ssf );
1334 if ( sc != SASL_OK ) {
1338 sc = sasl_setprop( ctx, SASL_AUTH_EXTERNAL, auth_id );
1340 if ( sc != SASL_OK ) {
1344 #elif defined(HAVE_CYRUS_SASL)
1346 sasl_conn_t *ctx = conn->c_sasl_context;
1347 sasl_external_properties_t extprops;
1349 if ( ctx == NULL ) {
1350 return LDAP_UNAVAILABLE;
1353 memset( &extprops, '\0', sizeof(extprops) );
1355 extprops.auth_id = (char *) auth_id;
1357 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL,
1358 (void *) &extprops );
1360 if ( sc != SASL_OK ) {
1365 return LDAP_SUCCESS;
1368 int slap_sasl_reset( Connection *conn )
1370 #ifdef HAVE_CYRUS_SASL
1371 sasl_conn_t *ctx = conn->c_sasl_context;
1376 /* must return "anonymous" */
1377 return LDAP_SUCCESS;
1380 char ** slap_sasl_mechs( Connection *conn )
1382 char **mechs = NULL;
1384 #ifdef HAVE_CYRUS_SASL
1385 sasl_conn_t *ctx = conn->c_sasl_context;
1389 SASL_CONST char *mechstr;
1391 sc = sasl_listmech( ctx,
1392 NULL, NULL, ",", NULL,
1393 &mechstr, NULL, NULL );
1395 if( sc != SASL_OK ) {
1397 LDAP_LOG( TRANSPORT, ERR,
1398 "slap_sasl_mechs: sasl_listmech failed: %d\n", sc, 0, 0 );
1400 Debug( LDAP_DEBUG_ANY, "slap_sasl_listmech failed: %d\n",
1407 mechs = ldap_str2charray( mechstr, "," );
1409 #if SASL_VERSION_MAJOR < 2
1418 int slap_sasl_close( Connection *conn )
1420 #ifdef HAVE_CYRUS_SASL
1421 sasl_conn_t *ctx = conn->c_sasl_context;
1424 sasl_dispose( &ctx );
1427 conn->c_sasl_context = NULL;
1429 free( conn->c_sasl_extra );
1430 conn->c_sasl_extra = NULL;
1433 return LDAP_SUCCESS;
1441 struct berval *cred,
1447 #ifdef HAVE_CYRUS_SASL
1448 sasl_conn_t *ctx = conn->c_sasl_context;
1449 struct berval response;
1450 unsigned reslen = 0;
1451 const char *errstr = NULL;
1455 LDAP_LOG( TRANSPORT, ENTRY,
1456 "sasl_bind: dn=\"%s\" mech=%s datalen=%ld\n",
1457 dn->bv_len ? dn->bv_val : "",
1458 conn->c_sasl_bind_in_progress ? "<continuing>" :
1459 conn->c_sasl_bind_mech.bv_val,
1460 cred ? cred->bv_len : 0 );
1462 Debug(LDAP_DEBUG_ARGS,
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>":conn->c_sasl_bind_mech.bv_val,
1466 cred ? cred->bv_len : 0 );
1471 send_ldap_result( conn, op, LDAP_UNAVAILABLE,
1472 NULL, "SASL unavailable on this session", NULL, NULL );
1476 #if SASL_VERSION_MAJOR >= 2
1477 #define START( ctx, mech, cred, clen, resp, rlen, err ) \
1478 sasl_server_start( ctx, mech, cred, clen, resp, rlen )
1479 #define STEP( ctx, cred, clen, resp, rlen, err ) \
1480 sasl_server_step( ctx, cred, clen, resp, rlen )
1482 #define START( ctx, mech, cred, clen, resp, rlen, err ) \
1483 sasl_server_start( ctx, mech, cred, clen, resp, rlen, err )
1484 #define STEP( ctx, cred, clen, resp, rlen, err ) \
1485 sasl_server_step( ctx, cred, clen, resp, rlen, err )
1488 if ( !conn->c_sasl_bind_in_progress ) {
1490 conn->c_sasl_bind_mech.bv_val,
1491 cred->bv_len ? cred->bv_val : "",
1493 (SASL_CONST char **)&response.bv_val, &reslen, &errstr );
1497 cred->bv_val, cred->bv_len,
1498 (SASL_CONST char **)&response.bv_val, &reslen, &errstr );
1501 response.bv_len = reslen;
1503 if ( sc == SASL_OK ) {
1504 sasl_ssf_t *ssf = NULL;
1506 *edn = conn->c_sasl_dn;
1507 conn->c_sasl_dn.bv_val = NULL;
1508 conn->c_sasl_dn.bv_len = 0;
1512 (void) sasl_getprop( ctx, SASL_SSF, (void *)&ssf );
1513 *ssfp = ssf ? *ssf : 0;
1516 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1517 conn->c_sasl_layers++;
1518 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1521 send_ldap_sasl( conn, op, rc,
1522 NULL, NULL, NULL, NULL,
1523 response.bv_len ? &response : NULL );
1525 } else if ( sc == SASL_CONTINUE ) {
1526 send_ldap_sasl( conn, op, rc = LDAP_SASL_BIND_IN_PROGRESS,
1527 NULL, NULL, NULL, NULL, &response );
1530 #if SASL_VERSION_MAJOR >= 2
1531 errstr = sasl_errdetail( ctx );
1533 send_ldap_result( conn, op, rc = slap_sasl_err2ldap( sc ),
1534 NULL, errstr, NULL, NULL );
1537 #if SASL_VERSION_MAJOR < 2
1538 if( response.bv_len ) {
1539 ch_free( response.bv_val );
1544 LDAP_LOG( TRANSPORT, RESULTS, "slap_sasl_bind: rc=%d\n", rc, 0, 0 );
1546 Debug(LDAP_DEBUG_TRACE, "<== slap_sasl_bind: rc=%d\n", rc, 0, 0);
1551 send_ldap_result( conn, op, rc = LDAP_UNAVAILABLE,
1552 NULL, "SASL not supported", NULL, NULL );
1558 char* slap_sasl_secprops( const char *in )
1560 #ifdef HAVE_CYRUS_SASL
1561 int rc = ldap_pvt_sasl_secprops( in, &sasl_secprops );
1563 return rc == LDAP_SUCCESS ? NULL : "Invalid security properties";
1565 return "SASL not supported";
1569 #ifdef HAVE_CYRUS_SASL
1575 struct berval *reqdata,
1577 struct berval **rspdata,
1578 LDAPControl *** rspctrls,
1582 struct berval id = { 0, NULL }; /* needs to come from connection */
1583 struct berval new = { 0, NULL };
1584 struct berval old = { 0, NULL };
1586 assert( reqoid != NULL );
1587 assert( strcmp( LDAP_EXOP_MODIFY_PASSWD, reqoid ) == 0 );
1589 rc = sasl_getprop( conn->c_sasl_context, SASL_USERNAME,
1590 (SASL_CONST void **)&id.bv_val );
1592 if( rc != SASL_OK ) {
1593 *text = "unable to retrieve SASL username";
1599 LDAP_LOG( BACKEND, ENTRY,
1600 "slap_sasl_setpass: \"%s\"\n",
1601 id.bv_val ? id.bv_val : "", 0, 0);
1603 Debug( LDAP_DEBUG_ARGS, "==> slap_sasl_setpass: \"%s\"\n",
1604 id.bv_val ? id.bv_val : "", 0, 0 );
1607 rc = slap_passwd_parse( reqdata,
1608 NULL, &old, &new, text );
1610 if( rc != LDAP_SUCCESS ) {
1614 if( new.bv_len == 0 ) {
1615 slap_passwd_generate(&new);
1617 if( new.bv_len == 0 ) {
1618 *text = "password generation failed.";
1623 *rspdata = slap_passwd_return( &new );
1626 #if SASL_VERSION_MAJOR < 2
1627 rc = sasl_setpass( conn->c_sasl_context,
1628 id.bv_val, new.bv_val, new.bv_len, 0, text );
1630 rc = sasl_setpass( conn->c_sasl_context, id.bv_val,
1631 new.bv_val, new.bv_len, old.bv_val, old.bv_len, 0 );
1632 if( rc != SASL_OK ) {
1633 *text = sasl_errdetail( conn->c_sasl_context );