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
37 /* Flags for telling slap_sasl_getdn() what type of identity is being passed */
38 #define FLAG_GETDN_AUTHCID 2
39 #define FLAG_GETDN_AUTHZID 4
41 static sasl_security_properties_t sasl_secprops;
43 int slap_sasl_config( int cargc, char **cargv, char *line,
44 const char *fname, int lineno )
46 /* set SASL proxy authorization policy */
47 if ( strcasecmp( cargv[0], "sasl-authz-policy" ) == 0 ) {
50 LDAP_LOG( CONFIG, CRIT,
51 "%s: line %d: missing policy in \"sasl-authz-policy <policy>\" line\n",
54 Debug( LDAP_DEBUG_ANY,
55 "%s: line %d: missing policy in \"sasl-authz-policy <policy>\" line\n",
61 if ( slap_sasl_setpolicy( cargv[1] ) ) {
63 LDAP_LOG( CONFIG, CRIT,
64 "%s: line %d: unable "
65 "to parse value \"%s\" "
66 "in \"sasl-authz-policy "
68 fname, lineno, cargv[1] );
70 Debug( LDAP_DEBUG_ANY,
71 "%s: line %d: unable "
72 "to parse value \"%s\" "
73 "in \"sasl-authz-policy "
75 fname, lineno, cargv[1] );
82 } else if ( strcasecmp( cargv[0], "sasl-host" ) == 0 ) {
85 LDAP_LOG( CONFIG, CRIT,
86 "%s: line %d: missing host in \"sasl-host <host>\" line\n",
89 Debug( LDAP_DEBUG_ANY,
90 "%s: line %d: missing host in \"sasl-host <host>\" line\n",
97 if ( global_host != NULL ) {
99 LDAP_LOG( CONFIG, CRIT,
100 "%s: line %d: already set sasl-host!\n",
103 Debug( LDAP_DEBUG_ANY,
104 "%s: line %d: already set sasl-host!\n",
111 global_host = ch_strdup( cargv[1] );
115 } else if ( strcasecmp( cargv[0], "sasl-realm" ) == 0 ) {
118 LDAP_LOG( CONFIG, CRIT,
119 "%s: line %d: missing realm in \"sasl-realm <realm>\" line.\n",
122 Debug( LDAP_DEBUG_ANY,
123 "%s: line %d: missing realm in \"sasl-realm <realm>\" line\n",
130 if ( global_realm != NULL ) {
132 LDAP_LOG( CONFIG, CRIT,
133 "%s: line %d: already set sasl-realm!\n",
136 Debug( LDAP_DEBUG_ANY,
137 "%s: line %d: already set sasl-realm!\n",
144 global_realm = ch_strdup( cargv[1] );
147 } else if ( !strcasecmp( cargv[0], "sasl-regexp" )
148 || !strcasecmp( cargv[0], "saslregexp" ) )
153 LDAP_LOG( CONFIG, CRIT,
154 "%s: line %d: need 2 args in "
155 "\"saslregexp <match> <replace>\"\n",
158 Debug( LDAP_DEBUG_ANY,
159 "%s: line %d: need 2 args in \"saslregexp <match> <replace>\"\n",
165 rc = slap_sasl_regexp_config( cargv[1], cargv[2] );
170 /* SASL security properties */
171 } else if ( strcasecmp( cargv[0], "sasl-secprops" ) == 0 ) {
176 LDAP_LOG( CONFIG, CRIT,
177 "%s: line %d: missing flags in "
178 "\"sasl-secprops <properties>\" line\n",
181 Debug( LDAP_DEBUG_ANY,
182 "%s: line %d: missing flags in \"sasl-secprops <properties>\" line\n",
189 txt = slap_sasl_secprops( cargv[1] );
192 LDAP_LOG( CONFIG, CRIT,
193 "%s: line %d sasl-secprops: %s\n",
194 fname, lineno, txt );
196 Debug( LDAP_DEBUG_ANY,
197 "%s: line %d: sasl-secprops: %s\n",
198 fname, lineno, txt );
214 Connection *conn = context;
218 if ( message == NULL ) {
219 return SASL_BADPARAM;
223 #if SASL_VERSION_MAJOR >= 2
225 level = LDAP_DEBUG_NONE;
229 level = LDAP_DEBUG_ANY;
233 level = LDAP_DEBUG_ANY;
237 level = LDAP_DEBUG_TRACE;
241 level = LDAP_DEBUG_TRACE;
245 level = LDAP_DEBUG_TRACE;
249 level = LDAP_DEBUG_TRACE;
253 level = LDAP_DEBUG_TRACE;
254 label = "Password Trace";
258 level = LDAP_DEBUG_ANY;
261 case SASL_LOG_WARNING:
262 level = LDAP_DEBUG_TRACE;
266 level = LDAP_DEBUG_TRACE;
271 return SASL_BADPARAM;
275 LDAP_LOG( TRANSPORT, ENTRY,
276 "SASL [conn=%ld] %s: %s\n", conn ? conn->c_connid : -1, label, message);
278 Debug( level, "SASL [conn=%ld] %s: %s\n",
279 conn ? conn->c_connid: -1,
288 /* Take any sort of identity string and return a DN with the "dn:" prefix. The
289 string returned in *dn is in its own allocated memory, and must be free'd
290 by the calling process.
291 -Mark Adamson, Carnegie Mellon
293 The "dn:" prefix is no longer used anywhere inside slapd. It is only used
294 on strings passed in directly from SASL.
295 -Howard Chu, Symas Corp.
301 static struct berval ext_bv = { sizeof("EXTERNAL")-1, "EXTERNAL" };
303 int slap_sasl_getdn( Connection *conn, char *id, int len,
304 char *user_realm, struct berval *dn, int flags )
307 int rc, is_dn = 0, do_norm = 1;
312 LDAP_LOG( TRANSPORT, ENTRY,
313 "slap_sasl_getdn: conn %d id=%s\n",
314 conn ? conn->c_connid : -1, id ? (*id ? id : "<empty>") : "NULL", 0 );
316 Debug( LDAP_DEBUG_ARGS, "slap_sasl_getdn: id=%s\n",
317 id?(*id?id:"<empty>"):"NULL",0,0 );
324 if ( len == 0 ) len = strlen( id );
326 /* Blatantly anonymous ID */
327 if ( len == sizeof("anonymous") - 1 &&
328 !strcasecmp( id, "anonymous" ) ) {
329 return( LDAP_SUCCESS );
335 ctx = conn->c_sasl_context;
337 /* An authcID needs to be converted to authzID form. Set the
338 * values directly into *dn; they will be normalized later. (and
339 * normalizing always makes a new copy.) An ID from a TLS certificate
340 * is already normalized, so copy it and skip normalization.
342 if( flags & FLAG_GETDN_AUTHCID ) {
344 if( conn->c_is_tls && conn->c_sasl_bind_mech.bv_len == ext_bv.bv_len
345 && ( strcasecmp( ext_bv.bv_val, conn->c_sasl_bind_mech.bv_val ) == 0 ) ) {
346 /* X.509 DN is already normalized */
349 ber_str2bv( id, len, 1, dn );
354 /* convert to u:<username> form */
361 if( !strncasecmp( id, "u:", sizeof("u:")-1 )) {
365 } else if ( !strncasecmp( id, "dn:", sizeof("dn:")-1) ) {
372 /* No other possibilities from here */
376 return( LDAP_INAPPROPRIATE_AUTH );
379 /* Username strings */
380 if( is_dn == SET_U ) {
382 len = dn->bv_len + sizeof("uid=")-1 + sizeof(",cn=auth")-1;
384 /* username may have embedded realm name */
385 if( ( realm = strchr( dn->bv_val, '@') ) ) {
387 len += sizeof(",cn=")-2;
388 } else if( user_realm && *user_realm ) {
389 len += strlen( user_realm ) + sizeof(",cn=")-1;
392 if( conn->c_sasl_bind_mech.bv_len ) {
393 len += conn->c_sasl_bind_mech.bv_len + sizeof(",cn=")-1;
396 /* Build the new dn */
398 dn->bv_val = ch_malloc( len+1 );
399 p = lutil_strcopy( dn->bv_val, "uid=" );
400 p = lutil_strncopy( p, c1, dn->bv_len );
403 int rlen = dn->bv_len - ( realm - c1 );
404 p = lutil_strcopy( p, ",cn=" );
405 p = lutil_strncopy( p, realm, rlen );
407 } else if( user_realm && *user_realm ) {
408 p = lutil_strcopy( p, ",cn=" );
409 p = lutil_strcopy( p, user_realm );
412 if( conn->c_sasl_bind_mech.bv_len ) {
413 p = lutil_strcopy( p, ",cn=" );
414 p = lutil_strcopy( p, conn->c_sasl_bind_mech.bv_val );
416 p = lutil_strcopy( p, ",cn=auth" );
417 dn->bv_len = p - dn->bv_val;
420 LDAP_LOG( TRANSPORT, ENTRY,
421 "slap_sasl_getdn: u:id converted to %s.\n", dn->bv_val, 0, 0 );
423 Debug( LDAP_DEBUG_TRACE, "getdn: u:id converted to %s\n", dn->bv_val,0,0 );
427 /* All strings are in DN form now. Normalize if needed. */
429 rc = dnNormalize2( NULL, dn, &dn2 );
431 /* User DNs were constructed above and must be freed now */
432 if ( is_dn == SET_U )
433 ch_free( dn->bv_val );
435 if ( rc != LDAP_SUCCESS ) {
443 /* Run thru regexp */
444 slap_sasl2dn( conn, dn, &dn2 );
446 ch_free( dn->bv_val );
449 LDAP_LOG( TRANSPORT, ENTRY,
450 "slap_sasl_getdn: dn:id converted to %s.\n", dn->bv_val, 0, 0 );
452 Debug( LDAP_DEBUG_TRACE, "getdn: dn:id converted to %s\n",
457 return( LDAP_SUCCESS );
460 #if SASL_VERSION_MAJOR >= 2
461 static const char *slap_propnames[] = {
462 "*slapConn", "*authcDN", "*authzDN", NULL };
464 static Filter *generic_filter;
470 typedef struct lookup_info {
473 const struct propval *list;
474 sasl_server_params_t *sparams;
485 LDAPControl **ctrls )
488 AttributeDescription *ad;
492 slap_callback *tmp = op->o_callback;
493 lookup_info *sl = tmp->sc_private;
495 for( i = 0; i < sl->last; i++ ) {
496 const char *name = sl->list[i].name;
498 if ( name[0] == '*' ) {
499 if ( sl->flags & SASL_AUXPROP_AUTHZID ) continue;
501 } else if ( !(sl->flags & SASL_AUXPROP_AUTHZID ) )
504 if ( sl->list[i].values ) {
505 if ( !(sl->flags & SASL_AUXPROP_OVERRIDE) ) continue;
508 rc = slap_str2ad( name, &ad, &text );
509 if ( rc != LDAP_SUCCESS ) {
511 LDAP_LOG( TRANSPORT, DETAIL1,
512 "slap_auxprop: str2ad(%s): %s\n", name, text, 0 );
514 Debug( LDAP_DEBUG_TRACE,
515 "slap_auxprop: str2ad(%s): %s\n", name, text, 0 );
519 a = attr_find( e->e_attrs, ad );
521 if ( ! access_allowed( be, conn, op, e, ad, NULL, ACL_AUTH, NULL ) )
523 if ( sl->list[i].values && ( sl->flags & SASL_AUXPROP_OVERRIDE ) )
524 sl->sparams->utils->prop_erase( sl->sparams->propctx, sl->list[i].name );
525 for ( bv = a->a_vals; bv->bv_val; bv++ ) {
526 sl->sparams->utils->prop_set( sl->sparams->propctx, sl->list[i].name,
527 bv->bv_val, bv->bv_len );
536 sasl_server_params_t *sparams,
543 Connection *conn = NULL;
546 sl.list = sparams->utils->prop_get( sparams->propctx );
547 sl.sparams = sparams;
550 /* Find our DN and conn first */
551 for( i = 0, sl.last = 0; sl.list[i].name; i++ ) {
552 if ( sl.list[i].name[0] == '*' ) {
553 if ( !strcmp( sl.list[i].name, slap_propnames[PROP_CONN] ) ) {
554 if ( sl.list[i].values && sl.list[i].values[0] )
555 AC_MEMCPY( &conn, sl.list[i].values[0], sizeof( conn ) );
556 if ( !sl.last ) sl.last = i;
558 if ( (flags & SASL_AUXPROP_AUTHZID) &&
559 !strcmp( sl.list[i].name, slap_propnames[PROP_AUTHZ] ) ) {
561 if ( sl.list[i].values && sl.list[i].values[0] )
562 AC_MEMCPY( &dn, sl.list[i].values[0], sizeof( dn ) );
563 if ( !sl.last ) sl.last = i;
566 if ( !strcmp( sl.list[i].name, slap_propnames[PROP_AUTHC] ) ) {
567 if ( !sl.last ) sl.last = i;
568 if ( sl.list[i].values && sl.list[i].values[0] ) {
569 AC_MEMCPY( &dn, sl.list[i].values[0], sizeof( dn ) );
570 if ( !(flags & SASL_AUXPROP_AUTHZID) )
577 /* Now see what else needs to be fetched */
578 for( i = 0; i < sl.last; i++ ) {
579 const char *name = sl.list[i].name;
581 if ( name[0] == '*' ) {
582 if ( flags & SASL_AUXPROP_AUTHZID ) continue;
584 } else if ( !(flags & SASL_AUXPROP_AUTHZID ) )
587 if ( sl.list[i].values ) {
588 if ( !(flags & SASL_AUXPROP_OVERRIDE) ) continue;
596 slap_callback cb = { slap_cb_null_response,
597 slap_cb_null_sresult, sasl_ap_lookup, NULL };
601 be = select_backend( &dn, 0, 1 );
603 if ( be && be->be_search ) {
604 op.o_tag = LDAP_REQ_SEARCH;
605 op.o_protocol = LDAP_VERSION3;
606 op.o_ndn = conn->c_ndn;
608 op.o_time = slap_get_time();
609 op.o_do_not_cache = 1;
611 (*be->be_search)( be, conn, &op, NULL, &dn,
612 LDAP_SCOPE_BASE, LDAP_DEREF_NEVER, 1, 0,
613 generic_filter, NULL, NULL, 0 );
618 static sasl_auxprop_plug_t slap_auxprop_plugin = {
621 NULL, /* glob_context */
622 NULL, /* auxprop_free */
630 const sasl_utils_t *utils,
633 sasl_auxprop_plug_t **plug,
634 const char *plugname)
636 if ( !out_version | !plug ) return SASL_BADPARAM;
638 if ( max_version < SASL_AUXPROP_PLUG_VERSION ) return SASL_BADVERS;
640 *out_version = SASL_AUXPROP_PLUG_VERSION;
641 *plug = &slap_auxprop_plugin;
645 typedef struct checkpass_info {
658 LDAPControl **ctrls )
660 slap_callback *tmp = op->o_callback;
661 checkpass_info *ci = tmp->sc_private;
665 ci->rc = SASL_NOVERIFY;
667 a = attr_find( e->e_attrs, slap_schema.si_ad_userPassword );
669 if ( ! access_allowed( be, conn, op, e, slap_schema.si_ad_userPassword,
670 NULL, ACL_AUTH, NULL ) ) return 0;
672 for ( bv = a->a_vals; bv->bv_val != NULL; bv++ ) {
673 if ( !lutil_passwd( bv, &ci->cred, NULL ) ) {
685 const char *username,
688 struct propctx *propctx)
690 Connection *conn = (Connection *)context;
698 /* SASL will fallback to its own mechanisms if we don't
699 * find an answer here.
702 rc = slap_sasl_getdn( conn, (char *)username, 0, NULL, &dn,
703 FLAG_GETDN_AUTHCID );
704 if ( rc != LDAP_SUCCESS ) {
705 sasl_seterror( sconn, 0, ldap_err2string( rc ) );
709 if ( dn.bv_len == 0 ) {
710 sasl_seterror( sconn, 0,
711 "No password is associated with the Root DSE" );
712 if ( dn.bv_val != NULL ) {
713 ch_free( dn.bv_val );
718 be = select_backend( &dn, 0, 1 );
719 if ( be && be->be_search ) {
721 slap_callback cb = { slap_cb_null_response,
722 slap_cb_null_sresult, sasl_cb_checkpass, NULL };
724 ci.cred.bv_val = (char *)pass;
725 ci.cred.bv_len = passlen;
728 op.o_tag = LDAP_REQ_SEARCH;
729 op.o_protocol = LDAP_VERSION3;
730 op.o_ndn = conn->c_ndn;
732 op.o_time = slap_get_time();
733 op.o_do_not_cache = 1;
735 (*be->be_search)( be, conn, &op, NULL, &dn,
736 LDAP_SCOPE_BASE, LDAP_DEREF_NEVER, 1, 0,
737 generic_filter, NULL, NULL, 0 );
739 if ( ci.rc != SASL_OK ) {
740 sasl_seterror( sconn, 0,
741 ldap_err2string( LDAP_INVALID_CREDENTIALS ) );
744 ch_free( dn.bv_val );
749 /* Convert a SASL authcid or authzid into a DN. Store the DN in an
750 * auxiliary property, so that we can refer to it in sasl_authorize
751 * without interfering with anything else. Also, the SASL username
752 * buffer is constrained to 256 characters, and our DNs could be
753 * much longer (totally arbitrary length)...
756 slap_sasl_canonicalize(
762 const char *user_realm,
767 Connection *conn = (Connection *)context;
768 struct propctx *props = sasl_auxprop_getctx( sconn );
769 struct propval auxvals[3];
772 const char *names[2];
777 LDAP_LOG( TRANSPORT, ENTRY,
778 "slap_sasl_canonicalize: conn %d %s=\"%s\"\n",
779 conn ? conn->c_connid : -1,
780 (flags & SASL_CU_AUTHID) ? "authcid" : "authzid", in ? in : "<empty>");
782 Debug( LDAP_DEBUG_ARGS, "SASL Canonicalize [conn=%ld]: "
784 conn ? conn->c_connid : -1,
785 (flags & SASL_CU_AUTHID) ? "authcid" : "authzid",
786 in ? in : "<empty>" );
789 /* If name is too big, just truncate. We don't care, we're
790 * using DNs, not the usernames.
792 if ( inlen > out_max )
795 /* See if we need to add request, can only do it once */
796 prop_getnames( props, slap_propnames, auxvals );
797 if ( !auxvals[0].name )
798 prop_request( props, slap_propnames );
800 if ( flags & SASL_CU_AUTHID )
805 /* Need to store the Connection for auxprop_lookup */
806 if ( !auxvals[PROP_CONN].values ) {
807 names[0] = slap_propnames[PROP_CONN];
809 prop_set( props, names[0], (char *)&conn, sizeof( conn ) );
812 /* Already been here? */
813 if ( auxvals[which].values )
816 if ( flags == SASL_CU_AUTHZID ) {
817 /* If we got unqualified authzid's, they probably came from SASL
818 * itself just passing the authcid to us. Look inside the oparams
819 * structure to see if that's true. (HACK: the out_len pointer is
820 * the address of a member of a sasl_out_params_t structure...)
822 sasl_out_params_t dummy;
823 int offset = (void *)&dummy.ulen - (void *)&dummy.authid;
824 char **authid = (void *)out_len - offset;
825 if ( *authid && !strcmp( in, *authid ) )
829 rc = slap_sasl_getdn( conn, (char *)in, inlen, (char *)user_realm, &dn,
830 (flags & SASL_CU_AUTHID) ? FLAG_GETDN_AUTHCID : FLAG_GETDN_AUTHZID );
831 if ( rc != LDAP_SUCCESS ) {
832 sasl_seterror( sconn, 0, ldap_err2string( rc ) );
836 names[0] = slap_propnames[which];
839 prop_set( props, names[0], (char *)&dn, sizeof( dn ) );
842 LDAP_LOG( TRANSPORT, ENTRY,
843 "slap_sasl_canonicalize: conn %d %s=\"%s\"\n",
844 conn ? conn->c_connid : -1, names[0]+1, dn.bv_val );
846 Debug( LDAP_DEBUG_ARGS, "SASL Canonicalize [conn=%ld]: "
848 conn ? conn->c_connid : -1,
849 names[0]+1, dn.bv_val );
851 done: AC_MEMCPY( out, in, inlen );
863 char *requested_user,
867 const char *def_realm,
869 struct propctx *props)
871 Connection *conn = (Connection *)context;
872 struct propval auxvals[3];
873 struct berval authcDN, authzDN;
877 LDAP_LOG( TRANSPORT, ENTRY,
878 "slap_sasl_authorize: conn %d authcid=\"%s\" authzid=\"%s\"\n",
879 conn ? conn->c_connid : -1, auth_identity, requested_user);
881 Debug( LDAP_DEBUG_ARGS, "SASL Authorize [conn=%ld]: "
882 "authcid=\"%s\" authzid=\"%s\"\n",
883 conn ? conn->c_connid : -1, auth_identity, requested_user );
885 if ( conn->c_sasl_dn.bv_val ) {
886 ch_free( conn->c_sasl_dn.bv_val );
887 conn->c_sasl_dn.bv_val = NULL;
888 conn->c_sasl_dn.bv_len = 0;
892 prop_getnames( props, slap_propnames+1, auxvals );
894 AC_MEMCPY( &authcDN, auxvals[0].values[0], sizeof(authcDN) );
896 /* Nothing to do if no authzID was given */
897 if ( !auxvals[1].name || !auxvals[1].values ) {
898 conn->c_sasl_dn = authcDN;
902 AC_MEMCPY( &authzDN, auxvals[1].values[0], sizeof(authzDN) );
904 rc = slap_sasl_authorized( conn, &authcDN, &authzDN );
905 ch_free( authcDN.bv_val );
906 if ( rc != LDAP_SUCCESS ) {
908 LDAP_LOG( TRANSPORT, INFO,
909 "slap_sasl_authorize: conn %ld authorization disallowed (%d)\n",
910 (long)(conn ? conn->c_connid : -1), rc, 0 );
912 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
913 " authorization disallowed (%d)\n",
914 (long) (conn ? conn->c_connid : -1), rc, 0 );
917 sasl_seterror( sconn, 0, "not authorized" );
918 ch_free( authzDN.bv_val );
922 conn->c_sasl_dn = authzDN;
925 LDAP_LOG( TRANSPORT, ENTRY,
926 "slap_sasl_authorize: conn %d authorization allowed\n",
927 (long)(conn ? conn->c_connid : -1), 0, 0 );
929 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
930 " authorization allowed\n",
931 (long) (conn ? conn->c_connid : -1), 0, 0 );
944 struct berval authcDN, authzDN;
946 Connection *conn = context;
950 if ( conn->c_sasl_dn.bv_val ) {
951 ch_free( conn->c_sasl_dn.bv_val );
952 conn->c_sasl_dn.bv_val = NULL;
953 conn->c_sasl_dn.bv_len = 0;
957 LDAP_LOG( TRANSPORT, ENTRY,
958 "slap_sasl_authorize: conn %d authcid=\"%s\" authzid=\"%s\"\n",
959 conn ? conn->c_connid : -1, authcid ? authcid : "<empty>",
960 authzid ? authzid : "<empty>" );
962 Debug( LDAP_DEBUG_ARGS, "SASL Authorize [conn=%ld]: "
963 "authcid=\"%s\" authzid=\"%s\"\n",
964 (long) (conn ? conn->c_connid : -1),
965 authcid ? authcid : "<empty>",
966 authzid ? authzid : "<empty>" );
969 /* Figure out how much data we have for the dn */
970 rc = sasl_getprop( conn->c_sasl_context, SASL_REALM, (void **)&realm );
971 if( rc != SASL_OK && rc != SASL_NOTDONE ) {
973 LDAP_LOG( TRANSPORT, ERR,
974 "slap_sasl_authorize: getprop(REALM) failed.\n", 0, 0, 0 );
976 Debug(LDAP_DEBUG_TRACE,
977 "authorize: getprop(REALM) failed!\n", 0,0,0);
979 *errstr = "Could not extract realm";
983 /* Convert the identities to DN's. If no authzid was given, client will
984 be bound as the DN matching their username */
985 rc = slap_sasl_getdn( conn, (char *)authcid, 0, realm, &authcDN, FLAG_GETDN_AUTHCID );
986 if( rc != LDAP_SUCCESS ) {
987 *errstr = ldap_err2string( rc );
990 if( ( authzid == NULL ) || !strcmp( authcid,authzid ) ) {
992 LDAP_LOG( TRANSPORT, ENTRY,
993 "slap_sasl_authorize: conn %d Using authcDN=%s\n",
994 conn ? conn->c_connid : -1, authcDN.bv_val, 0 );
996 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
997 "Using authcDN=%s\n", (long) (conn ? conn->c_connid : -1), authcDN.bv_val,0 );
1000 conn->c_sasl_dn = authcDN;
1004 rc = slap_sasl_getdn( conn, (char *)authzid, 0, realm, &authzDN, FLAG_GETDN_AUTHZID );
1005 if( rc != LDAP_SUCCESS ) {
1006 ch_free( authcDN.bv_val );
1007 *errstr = ldap_err2string( rc );
1008 return SASL_NOAUTHZ;
1011 rc = slap_sasl_authorized(conn, &authcDN, &authzDN );
1012 ch_free( authcDN.bv_val );
1015 LDAP_LOG( TRANSPORT, INFO,
1016 "slap_sasl_authorize: conn %ld authorization disallowed (%d)\n",
1017 (long)(conn ? conn->c_connid : -1), rc, 0 );
1019 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
1020 " authorization disallowed (%d)\n",
1021 (long) (conn ? conn->c_connid : -1), rc, 0 );
1024 *errstr = "not authorized";
1025 ch_free( authzDN.bv_val );
1026 return SASL_NOAUTHZ;
1030 LDAP_LOG( TRANSPORT, RESULTS,
1031 "slap_sasl_authorize: conn %d authorization allowed\n",
1032 (long)(conn ? conn->c_connid : -1 ), 0, 0 );
1034 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
1035 " authorization allowed\n",
1036 (long) (conn ? conn->c_connid : -1), 0, 0 );
1039 conn->c_sasl_dn = authzDN;
1043 #endif /* SASL_VERSION_MAJOR >= 2 */
1046 slap_sasl_err2ldap( int saslerr )
1052 rc = LDAP_SASL_BIND_IN_PROGRESS;
1061 rc = LDAP_AUTH_METHOD_NOT_SUPPORTED;
1064 rc = LDAP_INVALID_CREDENTIALS;
1067 rc = LDAP_INSUFFICIENT_ACCESS;
1071 rc = LDAP_INAPPROPRIATE_AUTH;
1083 int slap_sasl_init( void )
1085 #ifdef HAVE_CYRUS_SASL
1087 static sasl_callback_t server_callbacks[] = {
1088 { SASL_CB_LOG, &slap_sasl_log, NULL },
1089 { SASL_CB_LIST_END, NULL, NULL }
1099 ldap_pvt_sasl_mutex_new,
1100 ldap_pvt_sasl_mutex_lock,
1101 ldap_pvt_sasl_mutex_unlock,
1102 ldap_pvt_sasl_mutex_dispose );
1104 #if SASL_VERSION_MAJOR >= 2
1105 sasl_auxprop_add_plugin( "slapd", slap_auxprop_init );
1107 /* should provide callbacks for logging */
1108 /* server name should be configurable */
1109 rc = sasl_server_init( server_callbacks, "slapd" );
1111 if( rc != SASL_OK ) {
1113 LDAP_LOG( TRANSPORT, INFO, "slap_sasl_init: init failed.\n", 0, 0, 0 );
1115 Debug( LDAP_DEBUG_ANY, "sasl_server_init failed\n",
1123 LDAP_LOG( TRANSPORT, INFO, "slap_sasl_init: initialized!\n", 0, 0, 0 );
1125 Debug( LDAP_DEBUG_TRACE, "slap_sasl_init: initialized!\n",
1130 /* default security properties */
1131 memset( &sasl_secprops, '\0', sizeof(sasl_secprops) );
1132 sasl_secprops.max_ssf = INT_MAX;
1133 sasl_secprops.maxbufsize = 65536;
1134 sasl_secprops.security_flags = SASL_SEC_NOPLAINTEXT|SASL_SEC_NOANONYMOUS;
1140 int slap_sasl_destroy( void )
1142 #ifdef HAVE_CYRUS_SASL
1145 #if SASL_VERSION_MAJOR >= 2
1146 filter_free( generic_filter );
1148 free( global_host );
1154 int slap_sasl_open( Connection *conn )
1156 int cb, sc = LDAP_SUCCESS;
1157 #if SASL_VERSION_MAJOR >= 2
1158 char *ipremoteport = NULL, *iplocalport = NULL;
1161 #ifdef HAVE_CYRUS_SASL
1162 sasl_conn_t *ctx = NULL;
1163 sasl_callback_t *session_callbacks;
1165 assert( conn->c_sasl_context == NULL );
1166 assert( conn->c_sasl_extra == NULL );
1168 conn->c_sasl_layers = 0;
1171 #if SASL_VERSION_MAJOR >= 2
1172 ch_calloc( 5, sizeof(sasl_callback_t));
1174 ch_calloc( 3, sizeof(sasl_callback_t));
1176 conn->c_sasl_extra = session_callbacks;
1178 session_callbacks[cb=0].id = SASL_CB_LOG;
1179 session_callbacks[cb].proc = &slap_sasl_log;
1180 session_callbacks[cb++].context = conn;
1182 session_callbacks[cb].id = SASL_CB_PROXY_POLICY;
1183 session_callbacks[cb].proc = &slap_sasl_authorize;
1184 session_callbacks[cb++].context = conn;
1186 #if SASL_VERSION_MAJOR >= 2
1187 session_callbacks[cb].id = SASL_CB_CANON_USER;
1188 session_callbacks[cb].proc = &slap_sasl_canonicalize;
1189 session_callbacks[cb++].context = conn;
1191 /* XXXX: this should be conditional */
1192 session_callbacks[cb].id = SASL_CB_SERVER_USERDB_CHECKPASS;
1193 session_callbacks[cb].proc = &slap_sasl_checkpass;
1194 session_callbacks[cb++].context = conn;
1197 session_callbacks[cb].id = SASL_CB_LIST_END;
1198 session_callbacks[cb].proc = NULL;
1199 session_callbacks[cb++].context = NULL;
1201 if( global_host == NULL ) {
1202 global_host = ldap_pvt_get_fqdn( NULL );
1205 /* create new SASL context */
1206 #if SASL_VERSION_MAJOR >= 2
1207 if ( generic_filter == NULL ) {
1208 generic_filter = str2filter( "(objectclass=*)" );
1210 if ( conn->c_sock_name.bv_len != 0 &&
1211 strncmp( conn->c_sock_name.bv_val, "IP=", 3 ) == 0) {
1214 iplocalport = ch_strdup( conn->c_sock_name.bv_val + 3 );
1215 /* Convert IPv6 addresses to address;port syntax. */
1216 p = strrchr( iplocalport, ' ' );
1217 /* Convert IPv4 addresses to address;port syntax. */
1218 if ( p == NULL ) p = strchr( iplocalport, ':' );
1223 if ( conn->c_peer_name.bv_len != 0 &&
1224 strncmp( conn->c_peer_name.bv_val, "IP=", 3 ) == 0) {
1227 ipremoteport = ch_strdup( conn->c_peer_name.bv_val + 3 );
1228 /* Convert IPv6 addresses to address;port syntax. */
1229 p = strrchr( ipremoteport, ' ' );
1230 /* Convert IPv4 addresses to address;port syntax. */
1231 if ( p == NULL ) p = strchr( ipremoteport, ':' );
1236 sc = sasl_server_new( "ldap", global_host, global_realm,
1237 iplocalport, ipremoteport, session_callbacks, 0, &ctx );
1238 if ( iplocalport != NULL ) {
1239 ch_free( iplocalport );
1241 if ( ipremoteport != NULL ) {
1242 ch_free( ipremoteport );
1245 sc = sasl_server_new( "ldap", global_host, global_realm,
1246 session_callbacks, SASL_SECURITY_LAYER, &ctx );
1249 if( sc != SASL_OK ) {
1251 LDAP_LOG( TRANSPORT, ERR,
1252 "slap_sasl_open: sasl_server_new failed: %d\n", sc, 0, 0 );
1254 Debug( LDAP_DEBUG_ANY, "sasl_server_new failed: %d\n",
1261 conn->c_sasl_context = ctx;
1263 if( sc == SASL_OK ) {
1264 sc = sasl_setprop( ctx,
1265 SASL_SEC_PROPS, &sasl_secprops );
1267 if( sc != SASL_OK ) {
1269 LDAP_LOG( TRANSPORT, ERR,
1270 "slap_sasl_open: sasl_setprop failed: %d \n", sc, 0, 0 );
1272 Debug( LDAP_DEBUG_ANY, "sasl_setprop failed: %d\n",
1276 slap_sasl_close( conn );
1281 sc = slap_sasl_err2ldap( sc );
1286 int slap_sasl_external(
1289 const char *auth_id )
1291 #if SASL_VERSION_MAJOR >= 2
1293 sasl_conn_t *ctx = conn->c_sasl_context;
1295 if ( ctx == NULL ) {
1296 return LDAP_UNAVAILABLE;
1299 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL, &ssf );
1301 if ( sc != SASL_OK ) {
1305 sc = sasl_setprop( ctx, SASL_AUTH_EXTERNAL, auth_id );
1307 if ( sc != SASL_OK ) {
1311 #elif defined(HAVE_CYRUS_SASL)
1313 sasl_conn_t *ctx = conn->c_sasl_context;
1314 sasl_external_properties_t extprops;
1316 if ( ctx == NULL ) {
1317 return LDAP_UNAVAILABLE;
1320 memset( &extprops, '\0', sizeof(extprops) );
1322 extprops.auth_id = (char *) auth_id;
1324 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL,
1325 (void *) &extprops );
1327 if ( sc != SASL_OK ) {
1332 return LDAP_SUCCESS;
1335 int slap_sasl_reset( Connection *conn )
1337 #ifdef HAVE_CYRUS_SASL
1338 sasl_conn_t *ctx = conn->c_sasl_context;
1343 /* must return "anonymous" */
1344 return LDAP_SUCCESS;
1347 char ** slap_sasl_mechs( Connection *conn )
1349 char **mechs = NULL;
1351 #ifdef HAVE_CYRUS_SASL
1352 sasl_conn_t *ctx = conn->c_sasl_context;
1356 SASL_CONST char *mechstr;
1358 sc = sasl_listmech( ctx,
1359 NULL, NULL, ",", NULL,
1360 &mechstr, NULL, NULL );
1362 if( sc != SASL_OK ) {
1364 LDAP_LOG( TRANSPORT, ERR,
1365 "slap_sasl_mechs: sasl_listmech failed: %d\n", sc, 0, 0 );
1367 Debug( LDAP_DEBUG_ANY, "slap_sasl_listmech failed: %d\n",
1374 mechs = ldap_str2charray( mechstr, "," );
1376 #if SASL_VERSION_MAJOR < 2
1385 int slap_sasl_close( Connection *conn )
1387 #ifdef HAVE_CYRUS_SASL
1388 sasl_conn_t *ctx = conn->c_sasl_context;
1391 sasl_dispose( &ctx );
1394 conn->c_sasl_context = NULL;
1396 free( conn->c_sasl_extra );
1397 conn->c_sasl_extra = NULL;
1400 return LDAP_SUCCESS;
1408 struct berval *cred,
1414 #ifdef HAVE_CYRUS_SASL
1415 sasl_conn_t *ctx = conn->c_sasl_context;
1416 struct berval response;
1417 unsigned reslen = 0;
1418 const char *errstr = NULL;
1422 LDAP_LOG( TRANSPORT, ENTRY,
1423 "sasl_bind: dn=\"%s\" mech=%s datalen=%ld\n",
1424 dn->bv_len ? dn->bv_val : "",
1425 conn->c_sasl_bind_in_progress ? "<continuing>" :
1426 conn->c_sasl_bind_mech.bv_val,
1427 cred ? cred->bv_len : 0 );
1429 Debug(LDAP_DEBUG_ARGS,
1430 "==> sasl_bind: dn=\"%s\" mech=%s datalen=%ld\n",
1431 dn->bv_len ? dn->bv_val : "",
1432 conn->c_sasl_bind_in_progress ? "<continuing>":conn->c_sasl_bind_mech.bv_val,
1433 cred ? cred->bv_len : 0 );
1438 send_ldap_result( conn, op, LDAP_UNAVAILABLE,
1439 NULL, "SASL unavailable on this session", NULL, NULL );
1443 #if SASL_VERSION_MAJOR >= 2
1444 #define START( ctx, mech, cred, clen, resp, rlen, err ) \
1445 sasl_server_start( ctx, mech, cred, clen, resp, rlen )
1446 #define STEP( ctx, cred, clen, resp, rlen, err ) \
1447 sasl_server_step( ctx, cred, clen, resp, rlen )
1449 #define START( ctx, mech, cred, clen, resp, rlen, err ) \
1450 sasl_server_start( ctx, mech, cred, clen, resp, rlen, err )
1451 #define STEP( ctx, cred, clen, resp, rlen, err ) \
1452 sasl_server_step( ctx, cred, clen, resp, rlen, err )
1455 if ( !conn->c_sasl_bind_in_progress ) {
1457 conn->c_sasl_bind_mech.bv_val,
1458 cred->bv_len ? cred->bv_val : "",
1460 (SASL_CONST char **)&response.bv_val, &reslen, &errstr );
1464 cred->bv_val, cred->bv_len,
1465 (SASL_CONST char **)&response.bv_val, &reslen, &errstr );
1468 response.bv_len = reslen;
1470 if ( sc == SASL_OK ) {
1471 sasl_ssf_t *ssf = NULL;
1473 *edn = conn->c_sasl_dn;
1474 conn->c_sasl_dn.bv_val = NULL;
1475 conn->c_sasl_dn.bv_len = 0;
1479 (void) sasl_getprop( ctx, SASL_SSF, (void *)&ssf );
1480 *ssfp = ssf ? *ssf : 0;
1483 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1484 conn->c_sasl_layers++;
1485 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1488 send_ldap_sasl( conn, op, rc,
1489 NULL, NULL, NULL, NULL,
1490 response.bv_len ? &response : NULL );
1492 } else if ( sc == SASL_CONTINUE ) {
1493 send_ldap_sasl( conn, op, rc = LDAP_SASL_BIND_IN_PROGRESS,
1494 NULL, NULL, NULL, NULL, &response );
1497 #if SASL_VERSION_MAJOR >= 2
1498 errstr = sasl_errdetail( ctx );
1500 send_ldap_result( conn, op, rc = slap_sasl_err2ldap( sc ),
1501 NULL, errstr, NULL, NULL );
1504 #if SASL_VERSION_MAJOR < 2
1505 if( response.bv_len ) {
1506 ch_free( response.bv_val );
1511 LDAP_LOG( TRANSPORT, RESULTS, "slap_sasl_bind: rc=%d\n", rc, 0, 0 );
1513 Debug(LDAP_DEBUG_TRACE, "<== slap_sasl_bind: rc=%d\n", rc, 0, 0);
1518 send_ldap_result( conn, op, rc = LDAP_UNAVAILABLE,
1519 NULL, "SASL not supported", NULL, NULL );
1525 char* slap_sasl_secprops( const char *in )
1527 #ifdef HAVE_CYRUS_SASL
1528 int rc = ldap_pvt_sasl_secprops( in, &sasl_secprops );
1530 return rc == LDAP_SUCCESS ? NULL : "Invalid security properties";
1532 return "SASL not supported";
1536 #ifdef HAVE_CYRUS_SASL
1542 struct berval *reqdata,
1544 struct berval **rspdata,
1545 LDAPControl *** rspctrls,
1549 struct berval id = { 0, NULL }; /* needs to come from connection */
1550 struct berval new = { 0, NULL };
1551 struct berval old = { 0, NULL };
1553 assert( reqoid != NULL );
1554 assert( strcmp( LDAP_EXOP_MODIFY_PASSWD, reqoid ) == 0 );
1556 rc = sasl_getprop( conn->c_sasl_context, SASL_USERNAME,
1557 (SASL_CONST void **)&id.bv_val );
1559 if( rc != SASL_OK ) {
1560 *text = "unable to retrieve SASL username";
1566 LDAP_LOG( BACKEND, ENTRY,
1567 "slap_sasl_setpass: \"%s\"\n",
1568 id.bv_val ? id.bv_val : "", 0, 0);
1570 Debug( LDAP_DEBUG_ARGS, "==> slap_sasl_setpass: \"%s\"\n",
1571 id.bv_val ? id.bv_val : "", 0, 0 );
1574 rc = slap_passwd_parse( reqdata,
1575 NULL, &old, &new, text );
1577 if( rc != LDAP_SUCCESS ) {
1581 if( new.bv_len == 0 ) {
1582 slap_passwd_generate(&new);
1584 if( new.bv_len == 0 ) {
1585 *text = "password generation failed.";
1590 *rspdata = slap_passwd_return( &new );
1593 #if SASL_VERSION_MAJOR < 2
1594 rc = sasl_setpass( conn->c_sasl_context,
1595 id.bv_val, new.bv_val, new.bv_len, 0, text );
1597 rc = sasl_setpass( conn->c_sasl_context, id.bv_val,
1598 new.bv_val, new.bv_len, old.bv_val, old.bv_len, 0 );
1599 if( rc != SASL_OK ) {
1600 *text = sasl_errdetail( conn->c_sasl_context );