2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2004 The OpenLDAP Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
11 * A copy of this license is available in the file LICENSE in the
12 * top-level directory of the distribution or, alternatively, at
13 * <http://www.OpenLDAP.org/license.html>.
19 #include <ac/stdlib.h>
20 #include <ac/string.h>
29 #ifdef HAVE_CYRUS_SASL
30 # ifdef HAVE_SASL_SASL_H
31 # include <sasl/sasl.h>
36 # if SASL_VERSION_MAJOR >= 2
37 # ifdef HAVE_SASL_SASL_H
38 # include <sasl/saslplug.h>
40 # include <saslplug.h>
42 # define SASL_CONST const
47 #define SASL_VERSION_FULL ((SASL_VERSION_MAJOR << 16) |\
48 (SASL_VERSION_MINOR << 8) | SASL_VERSION_STEP)
50 static sasl_security_properties_t sasl_secprops;
51 #elif defined( SLAP_BUILTIN_SASL )
53 * built-in SASL implementation
54 * only supports EXTERNAL
56 typedef struct sasl_ctx {
57 slap_ssf_t sc_external_ssf;
58 struct berval sc_external_id;
67 static struct berval ext_bv = BER_BVC( "EXTERNAL" );
69 int slap_sasl_config( int cargc, char **cargv, char *line,
70 const char *fname, int lineno )
72 /* set SASL proxy authorization policy */
73 if ( strcasecmp( cargv[0], "sasl-authz-policy" ) == 0 ) {
76 LDAP_LOG( CONFIG, CRIT,
77 "%s: line %d: missing policy in"
78 " \"sasl-authz-policy <policy>\" line\n",
81 Debug( LDAP_DEBUG_ANY,
82 "%s: line %d: missing policy in"
83 " \"sasl-authz-policy <policy>\" line\n",
89 if ( slap_sasl_setpolicy( cargv[1] ) ) {
91 LDAP_LOG( CONFIG, CRIT,
92 "%s: line %d: unable "
93 "to parse value \"%s\" "
94 "in \"sasl-authz-policy "
96 fname, lineno, cargv[1] );
98 Debug( LDAP_DEBUG_ANY,
99 "%s: line %d: unable "
100 "to parse value \"%s\" "
101 "in \"sasl-authz-policy "
103 fname, lineno, cargv[1] );
108 } else if ( !strcasecmp( cargv[0], "sasl-regexp" )
109 || !strcasecmp( cargv[0], "saslregexp" ) )
114 LDAP_LOG( CONFIG, CRIT,
115 "%s: line %d: need 2 args in "
116 "\"saslregexp <match> <replace>\"\n",
119 Debug( LDAP_DEBUG_ANY,
120 "%s: line %d: need 2 args in "
121 "\"saslregexp <match> <replace>\"\n",
127 rc = slap_sasl_regexp_config( cargv[1], cargv[2] );
132 #ifdef HAVE_CYRUS_SASL
134 } else if ( strcasecmp( cargv[0], "sasl-host" ) == 0 ) {
137 LDAP_LOG( CONFIG, CRIT,
138 "%s: line %d: missing host in \"sasl-host <host>\" line\n",
141 Debug( LDAP_DEBUG_ANY,
142 "%s: line %d: missing host in \"sasl-host <host>\" line\n",
149 if ( global_host != NULL ) {
151 LDAP_LOG( CONFIG, CRIT,
152 "%s: line %d: already set sasl-host!\n",
155 Debug( LDAP_DEBUG_ANY,
156 "%s: line %d: already set sasl-host!\n",
163 global_host = ch_strdup( cargv[1] );
167 } else if ( strcasecmp( cargv[0], "sasl-realm" ) == 0 ) {
170 LDAP_LOG( CONFIG, CRIT, "%s: line %d: "
171 "missing realm in \"sasl-realm <realm>\" line.\n",
174 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
175 "missing realm in \"sasl-realm <realm>\" line.\n",
182 if ( global_realm != NULL ) {
184 LDAP_LOG( CONFIG, CRIT,
185 "%s: line %d: already set sasl-realm!\n",
188 Debug( LDAP_DEBUG_ANY,
189 "%s: line %d: already set sasl-realm!\n",
196 global_realm = ch_strdup( cargv[1] );
199 /* SASL security properties */
200 } else if ( strcasecmp( cargv[0], "sasl-secprops" ) == 0 ) {
205 LDAP_LOG( CONFIG, CRIT, "%s: line %d: "
206 "missing flags in \"sasl-secprops <properties>\" line\n",
209 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
210 "missing flags in \"sasl-secprops <properties>\" line\n",
217 txt = slap_sasl_secprops( cargv[1] );
220 LDAP_LOG( CONFIG, CRIT,
221 "%s: line %d sasl-secprops: %s\n",
222 fname, lineno, txt );
224 Debug( LDAP_DEBUG_ANY,
225 "%s: line %d: sasl-secprops: %s\n",
226 fname, lineno, txt );
231 #endif /* HAVE_CYRUS_SASL */
237 #ifdef HAVE_CYRUS_SASL
245 Connection *conn = context;
249 if ( message == NULL ) {
250 return SASL_BADPARAM;
254 #if SASL_VERSION_MAJOR >= 2
256 level = LDAP_DEBUG_NONE;
260 level = LDAP_DEBUG_ANY;
264 level = LDAP_DEBUG_ANY;
268 level = LDAP_DEBUG_TRACE;
272 level = LDAP_DEBUG_TRACE;
276 level = LDAP_DEBUG_TRACE;
280 level = LDAP_DEBUG_TRACE;
284 level = LDAP_DEBUG_TRACE;
285 label = "Password Trace";
289 level = LDAP_DEBUG_ANY;
292 case SASL_LOG_WARNING:
293 level = LDAP_DEBUG_TRACE;
297 level = LDAP_DEBUG_TRACE;
302 return SASL_BADPARAM;
306 LDAP_LOG( TRANSPORT, ENTRY,
307 "SASL [conn=%ld] %s: %s\n", conn ? conn->c_connid : -1, label, message);
309 Debug( level, "SASL [conn=%ld] %s: %s\n",
310 conn ? conn->c_connid: -1,
319 #if SASL_VERSION_MAJOR >= 2
320 static const char *slap_propnames[] = {
321 "*slapConn", "*authcDN", "*authzDN", NULL };
323 static Filter generic_filter = { LDAP_FILTER_PRESENT };
324 static struct berval generic_filterstr = BER_BVC("(objectclass=*)");
330 typedef struct lookup_info {
333 const struct propval *list;
334 sasl_server_params_t *sparams;
337 static slap_response sasl_ap_lookup, sasl_cb_checkpass;
340 sasl_ap_lookup( Operation *op, SlapReply *rs )
343 AttributeDescription *ad;
347 slap_callback *tmp = op->o_callback;
348 lookup_info *sl = tmp->sc_private;
350 if (rs->sr_type != REP_SEARCH) return 0;
352 for( i = 0; i < sl->last; i++ ) {
353 const char *name = sl->list[i].name;
355 if ( name[0] == '*' ) {
356 if ( sl->flags & SASL_AUXPROP_AUTHZID ) continue;
358 } else if ( !(sl->flags & SASL_AUXPROP_AUTHZID ) )
361 if ( sl->list[i].values ) {
362 if ( !(sl->flags & SASL_AUXPROP_OVERRIDE) ) continue;
365 rc = slap_str2ad( name, &ad, &text );
366 if ( rc != LDAP_SUCCESS ) {
368 LDAP_LOG( TRANSPORT, DETAIL1,
369 "slap_auxprop: str2ad(%s): %s\n", name, text, 0 );
371 Debug( LDAP_DEBUG_TRACE,
372 "slap_auxprop: str2ad(%s): %s\n", name, text, 0 );
376 a = attr_find( rs->sr_entry->e_attrs, ad );
378 if ( ! access_allowed( op, rs->sr_entry, ad, NULL, ACL_AUTH, NULL ) ) {
381 if ( sl->list[i].values && ( sl->flags & SASL_AUXPROP_OVERRIDE ) ) {
382 sl->sparams->utils->prop_erase( sl->sparams->propctx,
385 for ( bv = a->a_vals; bv->bv_val; bv++ ) {
386 sl->sparams->utils->prop_set( sl->sparams->propctx,
387 sl->list[i].name, bv->bv_val, bv->bv_len );
396 sasl_server_params_t *sparams,
403 Connection *conn = NULL;
406 sl.list = sparams->utils->prop_get( sparams->propctx );
407 sl.sparams = sparams;
410 /* Find our DN and conn first */
411 for( i = 0, sl.last = 0; sl.list[i].name; i++ ) {
412 if ( sl.list[i].name[0] == '*' ) {
413 if ( !strcmp( sl.list[i].name, slap_propnames[PROP_CONN] ) ) {
414 if ( sl.list[i].values && sl.list[i].values[0] )
415 AC_MEMCPY( &conn, sl.list[i].values[0], sizeof( conn ) );
416 if ( !sl.last ) sl.last = i;
418 if ( (flags & SASL_AUXPROP_AUTHZID) &&
419 !strcmp( sl.list[i].name, slap_propnames[PROP_AUTHZ] ) ) {
421 if ( sl.list[i].values && sl.list[i].values[0] )
422 AC_MEMCPY( &op.o_req_ndn, sl.list[i].values[0], sizeof( struct berval ) );
423 if ( !sl.last ) sl.last = i;
426 if ( !strcmp( sl.list[i].name, slap_propnames[PROP_AUTHC] ) ) {
427 if ( !sl.last ) sl.last = i;
428 if ( sl.list[i].values && sl.list[i].values[0] ) {
429 AC_MEMCPY( &op.o_req_ndn, sl.list[i].values[0], sizeof( struct berval ) );
430 if ( !(flags & SASL_AUXPROP_AUTHZID) )
437 /* Now see what else needs to be fetched */
438 for( i = 0; i < sl.last; i++ ) {
439 const char *name = sl.list[i].name;
441 if ( name[0] == '*' ) {
442 if ( flags & SASL_AUXPROP_AUTHZID ) continue;
444 } else if ( !(flags & SASL_AUXPROP_AUTHZID ) )
447 if ( sl.list[i].values ) {
448 if ( !(flags & SASL_AUXPROP_OVERRIDE) ) continue;
454 slap_callback cb = { NULL, sasl_ap_lookup, NULL, NULL };
458 op.o_bd = select_backend( &op.o_req_ndn, 0, 1 );
460 if ( op.o_bd && op.o_bd->be_search ) {
461 SlapReply rs = {REP_RESULT};
462 op.o_tag = LDAP_REQ_SEARCH;
463 op.o_protocol = LDAP_VERSION3;
464 op.o_ndn = conn->c_ndn;
466 op.o_time = slap_get_time();
467 op.o_do_not_cache = 1;
468 op.o_is_auth_check = 1;
469 op.o_threadctx = conn->c_sasl_bindop->o_threadctx;
470 op.o_tmpmemctx = conn->c_sasl_bindop->o_tmpmemctx;
471 op.o_tmpmfuncs = conn->c_sasl_bindop->o_tmpmfuncs;
473 op.o_connid = conn->c_connid;
474 op.o_req_dn = op.o_req_ndn;
475 op.ors_scope = LDAP_SCOPE_BASE;
476 op.ors_deref = LDAP_DEREF_NEVER;
478 op.ors_filter = &generic_filter;
479 op.ors_filterstr = generic_filterstr;
481 op.o_bd->be_search( &op, &rs );
486 #if SASL_VERSION_FULL >= 0x020110
490 sasl_server_params_t *sparams,
491 struct propctx *prctx,
496 SlapReply rs = {REP_RESULT};
498 Connection *conn = NULL;
499 const struct propval *pr;
500 Modifications *modlist = NULL, **modtail = &modlist, *mod;
501 slap_callback cb = { NULL, slap_null_cb, NULL, NULL };
502 char textbuf[SLAP_TEXT_BUFLEN];
504 size_t textlen = sizeof(textbuf);
506 /* just checking if we are enabled */
507 if (!prctx) return SASL_OK;
509 if (!sparams || !user) return SASL_BADPARAM;
511 pr = sparams->utils->prop_get( sparams->propctx );
513 /* Find our DN and conn first */
514 for( i = 0; pr[i].name; i++ ) {
515 if ( pr[i].name[0] == '*' ) {
516 if ( !strcmp( pr[i].name, slap_propnames[PROP_CONN] ) ) {
517 if ( pr[i].values && pr[i].values[0] )
518 AC_MEMCPY( &conn, pr[i].values[0], sizeof( conn ) );
521 if ( !strcmp( pr[i].name, slap_propnames[PROP_AUTHC] ) ) {
522 if ( pr[i].values && pr[i].values[0] ) {
523 AC_MEMCPY( &op.o_req_ndn, pr[i].values[0], sizeof( struct berval ) );
528 if (!conn || !op.o_req_ndn.bv_val) return SASL_BADPARAM;
530 op.o_bd = select_backend( &op.o_req_ndn, 0, 1 );
532 if ( !op.o_bd || !op.o_bd->be_modify ) return SASL_FAIL;
534 pr = sparams->utils->prop_get( prctx );
535 if (!pr) return SASL_BADPARAM;
537 for (i=0; pr[i].name; i++);
538 if (!i) return SASL_BADPARAM;
540 for (i=0; pr[i].name; i++) {
541 mod = (Modifications *)ch_malloc( sizeof(Modifications) );
542 mod->sml_op = LDAP_MOD_REPLACE;
543 ber_str2bv( pr[i].name, 0, 0, &mod->sml_type );
544 mod->sml_values = (struct berval *)ch_malloc( (pr[i].nvalues + 1) *
545 sizeof(struct berval));
546 for (j=0; j<pr[i].nvalues; j++) {
547 ber_str2bv( pr[i].values[j], 0, 1, &mod->sml_values[j]);
549 mod->sml_values[j].bv_val = NULL;
550 mod->sml_values[j].bv_len = 0;
551 mod->sml_nvalues = NULL;
552 mod->sml_desc = NULL;
554 modtail = &mod->sml_next;
558 rc = slap_mods_check( modlist, 0, &text, textbuf, textlen, NULL );
560 if ( rc == LDAP_SUCCESS ) {
561 rc = slap_mods_opattrs( &op, modlist, modtail, &text, textbuf,
565 if ( rc == LDAP_SUCCESS ) {
566 op.o_tag = LDAP_REQ_MODIFY;
567 op.o_protocol = LDAP_VERSION3;
568 op.o_ndn = op.o_req_ndn;
570 op.o_time = slap_get_time();
571 op.o_do_not_cache = 1;
572 op.o_is_auth_check = 1;
573 op.o_threadctx = conn->c_sasl_bindop->o_threadctx;
574 op.o_tmpmemctx = conn->c_sasl_bindop->o_tmpmemctx;
575 op.o_tmpmfuncs = conn->c_sasl_bindop->o_tmpmfuncs;
577 op.o_connid = conn->c_connid;
578 op.o_req_dn = op.o_req_ndn;
579 op.orm_modlist = modlist;
581 rc = op.o_bd->be_modify( &op, &rs );
583 slap_mods_free( modlist );
584 return rc ? SASL_FAIL : SASL_OK;
586 #endif /* SASL_VERSION_FULL >= 2.1.16 */
588 static sasl_auxprop_plug_t slap_auxprop_plugin = {
591 NULL, /* glob_context */
592 NULL, /* auxprop_free */
595 #if SASL_VERSION_FULL >= 0x020110
596 slap_auxprop_store /* the declaration of this member changed
597 * in cyrus SASL from 2.1.15 to 2.1.16 */
605 const sasl_utils_t *utils,
608 sasl_auxprop_plug_t **plug,
609 const char *plugname)
611 if ( !out_version || !plug ) return SASL_BADPARAM;
613 if ( max_version < SASL_AUXPROP_PLUG_VERSION ) return SASL_BADVERS;
615 *out_version = SASL_AUXPROP_PLUG_VERSION;
616 *plug = &slap_auxprop_plugin;
620 typedef struct checkpass_info {
626 sasl_cb_checkpass( Operation *op, SlapReply *rs )
628 slap_callback *tmp = op->o_callback;
629 checkpass_info *ci = tmp->sc_private;
633 if (rs->sr_type != REP_SEARCH) return 0;
635 ci->rc = SASL_NOVERIFY;
637 a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_userPassword );
639 if ( ! access_allowed( op, rs->sr_entry, slap_schema.si_ad_userPassword,
640 NULL, ACL_AUTH, NULL ) )
645 for ( bv = a->a_vals; bv->bv_val != NULL; bv++ ) {
646 if ( !lutil_passwd( bv, &ci->cred, NULL, &rs->sr_text ) ) {
658 const char *username,
661 struct propctx *propctx)
663 Connection *conn = (Connection *)context;
670 /* SASL will fallback to its own mechanisms if we don't
671 * find an answer here.
674 rc = slap_sasl_getdn( conn, NULL, (char *)username, 0, NULL, &op.o_req_ndn,
675 SLAP_GETDN_AUTHCID );
676 if ( rc != LDAP_SUCCESS ) {
677 sasl_seterror( sconn, 0, ldap_err2string( rc ) );
681 if ( op.o_req_ndn.bv_len == 0 ) {
682 sasl_seterror( sconn, 0,
683 "No password is associated with the Root DSE" );
684 if ( op.o_req_ndn.bv_val != NULL ) {
685 ch_free( op.o_req_ndn.bv_val );
690 op.o_bd = select_backend( &op.o_req_ndn, 0, 1 );
691 if ( op.o_bd && op.o_bd->be_search ) {
692 slap_callback cb = { NULL, sasl_cb_checkpass, NULL, NULL };
693 SlapReply rs = {REP_RESULT};
695 ci.cred.bv_val = (char *)pass;
696 ci.cred.bv_len = passlen;
699 op.o_tag = LDAP_REQ_SEARCH;
700 op.o_protocol = LDAP_VERSION3;
701 op.o_ndn = conn->c_ndn;
703 op.o_time = slap_get_time();
704 op.o_do_not_cache = 1;
705 op.o_is_auth_check = 1;
706 op.o_threadctx = conn->c_sasl_bindop->o_threadctx;
707 op.o_tmpmemctx = conn->c_sasl_bindop->o_tmpmemctx;
708 op.o_tmpmfuncs = conn->c_sasl_bindop->o_tmpmfuncs;
710 op.o_connid = conn->c_connid;
711 op.o_req_dn = op.o_req_ndn;
712 op.ors_scope = LDAP_SCOPE_BASE;
713 op.ors_deref = LDAP_DEREF_NEVER;
715 op.ors_filter = &generic_filter;
716 op.ors_filterstr = generic_filterstr;
718 op.o_bd->be_search( &op, &rs );
720 if ( ci.rc != SASL_OK ) {
721 sasl_seterror( sconn, 0,
722 ldap_err2string( LDAP_INVALID_CREDENTIALS ) );
725 ch_free( op.o_req_ndn.bv_val );
730 /* Convert a SASL authcid or authzid into a DN. Store the DN in an
731 * auxiliary property, so that we can refer to it in sasl_authorize
732 * without interfering with anything else. Also, the SASL username
733 * buffer is constrained to 256 characters, and our DNs could be
734 * much longer (SLAP_LDAPDN_MAXLEN, currently set to 8192)
737 slap_sasl_canonicalize(
743 const char *user_realm,
748 Connection *conn = (Connection *)context;
749 struct propctx *props = sasl_auxprop_getctx( sconn );
750 struct propval auxvals[3];
753 const char *names[2];
758 LDAP_LOG( TRANSPORT, ENTRY,
759 "slap_sasl_canonicalize: conn %d %s=\"%s\"\n",
760 conn ? conn->c_connid : -1,
761 (flags & SASL_CU_AUTHID) ? "authcid" : "authzid",
762 in ? in : "<empty>");
764 Debug( LDAP_DEBUG_ARGS, "SASL Canonicalize [conn=%ld]: %s=\"%s\"\n",
765 conn ? conn->c_connid : -1,
766 (flags & SASL_CU_AUTHID) ? "authcid" : "authzid",
767 in ? in : "<empty>");
770 /* If name is too big, just truncate. We don't care, we're
771 * using DNs, not the usernames.
773 if ( inlen > out_max )
776 /* This is a Simple Bind using SPASSWD. That means the in-directory
777 * userPassword of the Binding user already points at SASL, so it
778 * cannot be used to actually satisfy a password comparison. Just
779 * ignore it, some other mech will process it.
781 if ( !conn->c_sasl_bindop ||
782 conn->c_sasl_bindop->orb_method != LDAP_AUTH_SASL ) goto done;
784 /* See if we need to add request, can only do it once */
785 prop_getnames( props, slap_propnames, auxvals );
786 if ( !auxvals[0].name )
787 prop_request( props, slap_propnames );
789 if ( flags & SASL_CU_AUTHID )
794 /* Need to store the Connection for auxprop_lookup */
795 if ( !auxvals[PROP_CONN].values ) {
796 names[0] = slap_propnames[PROP_CONN];
798 prop_set( props, names[0], (char *)&conn, sizeof( conn ) );
801 /* Already been here? */
802 if ( auxvals[which].values )
805 /* Normally we require an authzID to have a u: or dn: prefix.
806 * However, SASL frequently gives us an authzID that is just
807 * an exact copy of the authcID, without a prefix. We need to
808 * detect and allow this condition. If SASL calls canonicalize
809 * with SASL_CU_AUTHID|SASL_CU_AUTHZID this is a no-brainer.
810 * But if it's broken into two calls, we need to remember the
811 * authcID so that we can compare the authzID later. We store
812 * the authcID temporarily in conn->c_sasl_dn. We necessarily
813 * finish Canonicalizing before Authorizing, so there is no
814 * conflict with slap_sasl_authorize's use of this temp var.
816 * The SASL EXTERNAL mech is backwards from all the other mechs,
817 * it does authzID before the authcID. If we see that authzID
818 * has already been done, don't do anything special with authcID.
820 if ( flags == SASL_CU_AUTHID && !auxvals[PROP_AUTHZ].values ) {
821 conn->c_sasl_dn.bv_val = (char *) in;
822 } else if ( flags == SASL_CU_AUTHZID && conn->c_sasl_dn.bv_val ) {
823 rc = strcmp( in, conn->c_sasl_dn.bv_val );
824 conn->c_sasl_dn.bv_val = NULL;
825 /* They were equal, no work needed */
826 if ( !rc ) goto done;
829 rc = slap_sasl_getdn( conn, NULL, (char *)in, inlen, (char *)user_realm, &dn,
830 (flags & SASL_CU_AUTHID) ? SLAP_GETDN_AUTHCID : SLAP_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,
845 dn.bv_val ? dn.bv_val : "<EMPTY>" );
847 Debug( LDAP_DEBUG_ARGS, "SASL Canonicalize [conn=%ld]: %s=\"%s\"\n",
848 conn ? conn->c_connid : -1, names[0]+1,
849 dn.bv_val ? dn.bv_val : "<EMPTY>" );
853 AC_MEMCPY( out, in, inlen );
865 char *requested_user,
869 const char *def_realm,
871 struct propctx *props)
873 Connection *conn = (Connection *)context;
874 struct propval auxvals[3];
875 struct berval authcDN, authzDN;
878 /* Simple Binds don't support proxy authorization, ignore it */
879 if ( !conn->c_sasl_bindop ||
880 conn->c_sasl_bindop->orb_method != LDAP_AUTH_SASL ) return SASL_OK;
883 LDAP_LOG( TRANSPORT, ENTRY,
884 "slap_sasl_authorize: conn %d authcid=\"%s\" authzid=\"%s\"\n",
885 conn ? conn->c_connid : -1, auth_identity, requested_user);
887 Debug( LDAP_DEBUG_ARGS, "SASL proxy authorize [conn=%ld]: "
888 "authcid=\"%s\" authzid=\"%s\"\n",
889 conn ? conn->c_connid : -1, auth_identity, requested_user );
891 if ( conn->c_sasl_dn.bv_val ) {
892 ch_free( conn->c_sasl_dn.bv_val );
893 conn->c_sasl_dn.bv_val = NULL;
894 conn->c_sasl_dn.bv_len = 0;
898 prop_getnames( props, slap_propnames+1, auxvals );
900 AC_MEMCPY( &authcDN, auxvals[0].values[0], sizeof(authcDN) );
902 /* Nothing to do if no authzID was given */
903 if ( !auxvals[1].name || !auxvals[1].values ) {
904 conn->c_sasl_dn = authcDN;
908 AC_MEMCPY( &authzDN, auxvals[1].values[0], sizeof(authzDN) );
910 rc = slap_sasl_authorized( conn->c_sasl_bindop, &authcDN, &authzDN );
911 ch_free( authcDN.bv_val );
912 if ( rc != LDAP_SUCCESS ) {
914 LDAP_LOG( TRANSPORT, INFO,
915 "slap_sasl_authorize: conn %ld "
916 "proxy authorization disallowed (%d)\n",
917 (long)(conn ? conn->c_connid : -1), rc, 0 );
919 Debug( LDAP_DEBUG_TRACE, "SASL Proxy Authorize [conn=%ld]: "
920 "proxy authorization disallowed (%d)\n",
921 (long) (conn ? conn->c_connid : -1), rc, 0 );
924 sasl_seterror( sconn, 0, "not authorized" );
925 ch_free( authzDN.bv_val );
929 conn->c_sasl_dn = authzDN;
931 if (conn->c_sasl_bindop) {
932 Statslog( LDAP_DEBUG_STATS,
933 "conn=%lu op=%lu BIND authcid=\"%s\"\n",
934 conn->c_connid, conn->c_sasl_bindop->o_opid,
935 auth_identity, 0, 0);
939 LDAP_LOG( TRANSPORT, ENTRY,
940 "slap_sasl_authorize: conn %d proxy authorization allowed\n",
941 (long)(conn ? conn->c_connid : -1), 0, 0 );
943 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
944 " proxy authorization allowed\n",
945 (long) (conn ? conn->c_connid : -1), 0, 0 );
958 struct berval authcDN, authzDN;
960 Connection *conn = context;
964 if ( conn->c_sasl_dn.bv_val ) {
965 ch_free( conn->c_sasl_dn.bv_val );
966 conn->c_sasl_dn.bv_val = NULL;
967 conn->c_sasl_dn.bv_len = 0;
971 LDAP_LOG( TRANSPORT, ENTRY,
972 "slap_sasl_authorize: conn %d authcid=\"%s\" authzid=\"%s\"\n",
973 conn ? conn->c_connid : -1, authcid ? authcid : "<empty>",
974 authzid ? authzid : "<empty>" );
976 Debug( LDAP_DEBUG_ARGS, "SASL Authorize [conn=%ld]: "
977 "authcid=\"%s\" authzid=\"%s\"\n",
978 (long) (conn ? conn->c_connid : -1),
979 authcid ? authcid : "<empty>",
980 authzid ? authzid : "<empty>" );
983 /* Figure out how much data we have for the dn */
984 rc = sasl_getprop( conn->c_sasl_authctx, SASL_REALM, (void **)&realm );
985 if( rc != SASL_OK && rc != SASL_NOTDONE ) {
987 LDAP_LOG( TRANSPORT, ERR,
988 "slap_sasl_authorize: getprop(REALM) failed.\n", 0, 0, 0 );
990 Debug(LDAP_DEBUG_TRACE,
991 "authorize: getprop(REALM) failed!\n", 0,0,0);
993 *errstr = "Could not extract realm";
997 /* Convert the identities to DN's. If no authzid was given, client will
998 be bound as the DN matching their username */
999 rc = slap_sasl_getdn( conn, NULL, (char *)authcid, 0, realm,
1000 &authcDN, SLAP_GETDN_AUTHCID );
1001 if( rc != LDAP_SUCCESS ) {
1002 *errstr = ldap_err2string( rc );
1003 return SASL_NOAUTHZ;
1005 if( ( authzid == NULL ) || !strcmp( authcid,authzid ) ) {
1007 LDAP_LOG( TRANSPORT, ENTRY,
1008 "slap_sasl_authorize: conn %d Using authcDN=%s\n",
1009 conn ? conn->c_connid : -1, authcDN.bv_val, 0 );
1011 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
1012 "Using authcDN=%s\n", (long) (conn ? conn->c_connid : -1), authcDN.bv_val,0 );
1015 conn->c_sasl_dn = authcDN;
1018 rc = slap_sasl_getdn( conn, NULL, (char *)authzid, 0, realm,
1019 &authzDN, SLAP_GETDN_AUTHZID );
1020 if( rc != LDAP_SUCCESS ) {
1021 ch_free( authcDN.bv_val );
1022 *errstr = ldap_err2string( rc );
1023 return SASL_NOAUTHZ;
1026 rc = slap_sasl_authorized(conn->c_sasl_bindop, &authcDN, &authzDN );
1027 ch_free( authcDN.bv_val );
1030 LDAP_LOG( TRANSPORT, INFO,
1031 "slap_sasl_authorize: conn %ld "
1032 "proxy authorization disallowed (%d)\n",
1033 (long)(conn ? conn->c_connid : -1), rc, 0 );
1035 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
1036 "proxy authorization disallowed (%d)\n",
1037 (long) (conn ? conn->c_connid : -1), rc, 0 );
1040 *errstr = "not authorized";
1041 ch_free( authzDN.bv_val );
1042 return SASL_NOAUTHZ;
1044 conn->c_sasl_dn = authzDN;
1048 LDAP_LOG( TRANSPORT, RESULTS,
1049 "slap_sasl_authorize: conn %d proxy authorization allowed\n",
1050 (long)(conn ? conn->c_connid : -1 ), 0, 0 );
1052 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
1053 " authorization allowed\n",
1054 (long) (conn ? conn->c_connid : -1), 0, 0 );
1057 if (conn->c_sasl_bindop) {
1058 Statslog( LDAP_DEBUG_STATS,
1059 "conn=%lu op=%lu BIND authcid=\"%s\"\n",
1060 conn->c_connid, conn->c_sasl_bindop->o_opid,
1067 #endif /* SASL_VERSION_MAJOR >= 2 */
1070 slap_sasl_err2ldap( int saslerr )
1079 rc = LDAP_SASL_BIND_IN_PROGRESS;
1088 rc = LDAP_AUTH_METHOD_NOT_SUPPORTED;
1091 rc = LDAP_INVALID_CREDENTIALS;
1094 rc = LDAP_INSUFFICIENT_ACCESS;
1098 rc = LDAP_INAPPROPRIATE_AUTH;
1109 int slap_sasl_init( void )
1111 #ifdef HAVE_CYRUS_SASL
1113 static sasl_callback_t server_callbacks[] = {
1114 { SASL_CB_LOG, &slap_sasl_log, NULL },
1115 { SASL_CB_LIST_END, NULL, NULL }
1118 #ifdef HAVE_SASL_VERSION
1119 /* stringify the version number, sasl.h doesn't do it for us */
1120 #define VSTR0(maj, min, pat) #maj "." #min "." #pat
1121 #define VSTR(maj, min, pat) VSTR0(maj, min, pat)
1122 #define SASL_VERSION_STRING VSTR(SASL_VERSION_MAJOR, SASL_VERSION_MINOR, \
1125 sasl_version( NULL, &rc );
1126 if ( ((rc >> 16) != ((SASL_VERSION_MAJOR << 8)|SASL_VERSION_MINOR)) ||
1127 (rc & 0xffff) < SASL_VERSION_STEP)
1129 char version[sizeof("xxx.xxx.xxxxx")];
1130 sprintf( version, "%u.%d.%d", (unsigned)rc >> 24, (rc >> 16) & 0xff,
1133 LDAP_LOG( TRANSPORT, INFO,
1134 "slap_sasl_init: SASL library version mismatch:"
1135 " expected " SASL_VERSION_STRING ","
1136 " got %s\n", version, 0, 0 );
1138 Debug( LDAP_DEBUG_ANY,
1139 "slap_sasl_init: SASL library version mismatch:"
1140 " expected " SASL_VERSION_STRING ","
1141 " got %s\n", version, 0, 0 );
1147 /* SASL 2 does its own memory management internally */
1148 #if SASL_VERSION_MAJOR < 2
1157 ldap_pvt_sasl_mutex_new,
1158 ldap_pvt_sasl_mutex_lock,
1159 ldap_pvt_sasl_mutex_unlock,
1160 ldap_pvt_sasl_mutex_dispose );
1162 #if SASL_VERSION_MAJOR >= 2
1163 generic_filter.f_desc = slap_schema.si_ad_objectClass;
1165 sasl_auxprop_add_plugin( "slapd", slap_auxprop_init );
1167 /* should provide callbacks for logging */
1168 /* server name should be configurable */
1169 rc = sasl_server_init( server_callbacks, "slapd" );
1171 if( rc != SASL_OK ) {
1173 LDAP_LOG( TRANSPORT, INFO, "slap_sasl_init: init failed.\n", 0, 0, 0 );
1175 Debug( LDAP_DEBUG_ANY, "sasl_server_init failed\n",
1178 #if SASL_VERSION_MAJOR < 2
1179 /* A no-op used to make sure we linked with Cyrus 1.5 */
1180 sasl_client_auth( NULL, NULL, NULL, 0, NULL, NULL );
1187 LDAP_LOG( TRANSPORT, INFO, "slap_sasl_init: initialized!\n", 0, 0, 0 );
1189 Debug( LDAP_DEBUG_TRACE, "slap_sasl_init: initialized!\n",
1193 /* default security properties */
1194 memset( &sasl_secprops, '\0', sizeof(sasl_secprops) );
1195 sasl_secprops.max_ssf = INT_MAX;
1196 sasl_secprops.maxbufsize = 65536;
1197 sasl_secprops.security_flags = SASL_SEC_NOPLAINTEXT|SASL_SEC_NOANONYMOUS;
1203 int slap_sasl_destroy( void )
1205 #ifdef HAVE_CYRUS_SASL
1208 free( global_host );
1214 int slap_sasl_open( Connection *conn, int reopen )
1216 int sc = LDAP_SUCCESS;
1217 #ifdef HAVE_CYRUS_SASL
1220 sasl_conn_t *ctx = NULL;
1221 sasl_callback_t *session_callbacks;
1223 #if SASL_VERSION_MAJOR >= 2
1224 char *ipremoteport = NULL, *iplocalport = NULL;
1227 assert( conn->c_sasl_authctx == NULL );
1230 assert( conn->c_sasl_extra == NULL );
1233 #if SASL_VERSION_MAJOR >= 2
1234 SLAP_CALLOC( 5, sizeof(sasl_callback_t));
1236 SLAP_CALLOC( 3, sizeof(sasl_callback_t));
1238 if( session_callbacks == NULL ) {
1240 LDAP_LOG( TRANSPORT, ERR,
1241 "slap_sasl_open: SLAP_MALLOC failed", 0, 0, 0 );
1243 Debug( LDAP_DEBUG_ANY,
1244 "slap_sasl_open: SLAP_MALLOC failed", 0, 0, 0 );
1248 conn->c_sasl_extra = session_callbacks;
1250 session_callbacks[cb=0].id = SASL_CB_LOG;
1251 session_callbacks[cb].proc = &slap_sasl_log;
1252 session_callbacks[cb++].context = conn;
1254 session_callbacks[cb].id = SASL_CB_PROXY_POLICY;
1255 session_callbacks[cb].proc = &slap_sasl_authorize;
1256 session_callbacks[cb++].context = conn;
1258 #if SASL_VERSION_MAJOR >= 2
1259 session_callbacks[cb].id = SASL_CB_CANON_USER;
1260 session_callbacks[cb].proc = &slap_sasl_canonicalize;
1261 session_callbacks[cb++].context = conn;
1263 /* XXXX: this should be conditional */
1264 session_callbacks[cb].id = SASL_CB_SERVER_USERDB_CHECKPASS;
1265 session_callbacks[cb].proc = &slap_sasl_checkpass;
1266 session_callbacks[cb++].context = conn;
1269 session_callbacks[cb].id = SASL_CB_LIST_END;
1270 session_callbacks[cb].proc = NULL;
1271 session_callbacks[cb++].context = NULL;
1273 session_callbacks = conn->c_sasl_extra;
1276 conn->c_sasl_layers = 0;
1278 if( global_host == NULL ) {
1279 global_host = ldap_pvt_get_fqdn( NULL );
1282 /* create new SASL context */
1283 #if SASL_VERSION_MAJOR >= 2
1284 if ( conn->c_sock_name.bv_len != 0 &&
1285 strncmp( conn->c_sock_name.bv_val, "IP=", 3 ) == 0) {
1288 iplocalport = ch_strdup( conn->c_sock_name.bv_val + 3 );
1289 /* Convert IPv6 addresses to address;port syntax. */
1290 p = strrchr( iplocalport, ' ' );
1291 /* Convert IPv4 addresses to address;port syntax. */
1292 if ( p == NULL ) p = strchr( iplocalport, ':' );
1297 if ( conn->c_peer_name.bv_len != 0 &&
1298 strncmp( conn->c_peer_name.bv_val, "IP=", 3 ) == 0) {
1301 ipremoteport = ch_strdup( conn->c_peer_name.bv_val + 3 );
1302 /* Convert IPv6 addresses to address;port syntax. */
1303 p = strrchr( ipremoteport, ' ' );
1304 /* Convert IPv4 addresses to address;port syntax. */
1305 if ( p == NULL ) p = strchr( ipremoteport, ':' );
1310 sc = sasl_server_new( "ldap", global_host, global_realm,
1311 iplocalport, ipremoteport, session_callbacks, SASL_SUCCESS_DATA, &ctx );
1312 if ( iplocalport != NULL ) {
1313 ch_free( iplocalport );
1315 if ( ipremoteport != NULL ) {
1316 ch_free( ipremoteport );
1319 sc = sasl_server_new( "ldap", global_host, global_realm,
1320 session_callbacks, SASL_SECURITY_LAYER, &ctx );
1323 if( sc != SASL_OK ) {
1325 LDAP_LOG( TRANSPORT, ERR,
1326 "slap_sasl_open: sasl_server_new failed: %d\n", sc, 0, 0 );
1328 Debug( LDAP_DEBUG_ANY, "sasl_server_new failed: %d\n",
1335 conn->c_sasl_authctx = ctx;
1337 if( sc == SASL_OK ) {
1338 sc = sasl_setprop( ctx,
1339 SASL_SEC_PROPS, &sasl_secprops );
1341 if( sc != SASL_OK ) {
1343 LDAP_LOG( TRANSPORT, ERR,
1344 "slap_sasl_open: sasl_setprop failed: %d \n", sc, 0, 0 );
1346 Debug( LDAP_DEBUG_ANY, "sasl_setprop failed: %d\n",
1350 slap_sasl_close( conn );
1355 sc = slap_sasl_err2ldap( sc );
1357 #elif defined(SLAP_BUILTIN_SASL)
1358 /* built-in SASL implementation */
1359 SASL_CTX *ctx = (SASL_CTX *) SLAP_MALLOC(sizeof(SASL_CTX));
1360 if( ctx == NULL ) return -1;
1362 ctx->sc_external_ssf = 0;
1363 ctx->sc_external_id.bv_len = 0;
1364 ctx->sc_external_id.bv_val = NULL;
1366 conn->c_sasl_authctx = ctx;
1372 int slap_sasl_external(
1375 struct berval *auth_id )
1377 #if SASL_VERSION_MAJOR >= 2
1379 sasl_conn_t *ctx = conn->c_sasl_authctx;
1381 if ( ctx == NULL ) {
1382 return LDAP_UNAVAILABLE;
1385 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL, &ssf );
1387 if ( sc != SASL_OK ) {
1391 sc = sasl_setprop( ctx, SASL_AUTH_EXTERNAL,
1392 auth_id ? auth_id->bv_val : NULL );
1394 if ( sc != SASL_OK ) {
1398 #elif defined(HAVE_CYRUS_SASL)
1400 sasl_conn_t *ctx = conn->c_sasl_authctx;
1401 sasl_external_properties_t extprops;
1403 if ( ctx == NULL ) {
1404 return LDAP_UNAVAILABLE;
1407 memset( &extprops, '\0', sizeof(extprops) );
1409 extprops.auth_id = auth_id ? auth_id->bv_val : NULL;
1411 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL,
1412 (void *) &extprops );
1414 if ( sc != SASL_OK ) {
1417 #elif defined(SLAP_BUILTIN_SASL)
1418 /* built-in SASL implementation */
1419 SASL_CTX *ctx = conn->c_sasl_authctx;
1420 if ( ctx == NULL ) return LDAP_UNAVAILABLE;
1422 ctx->sc_external_ssf = ssf;
1424 ctx->sc_external_id = *auth_id;
1425 auth_id->bv_len = 0;
1426 auth_id->bv_val = NULL;
1428 ctx->sc_external_id.bv_len = 0;
1429 ctx->sc_external_id.bv_val = NULL;
1433 return LDAP_SUCCESS;
1436 int slap_sasl_reset( Connection *conn )
1438 return LDAP_SUCCESS;
1441 char ** slap_sasl_mechs( Connection *conn )
1443 char **mechs = NULL;
1445 #ifdef HAVE_CYRUS_SASL
1446 sasl_conn_t *ctx = conn->c_sasl_authctx;
1448 if( ctx == NULL ) ctx = conn->c_sasl_sockctx;
1452 SASL_CONST char *mechstr;
1454 sc = sasl_listmech( ctx,
1455 NULL, NULL, ",", NULL,
1456 &mechstr, NULL, NULL );
1458 if( sc != SASL_OK ) {
1460 LDAP_LOG( TRANSPORT, ERR,
1461 "slap_sasl_mechs: sasl_listmech failed: %d\n", sc, 0, 0 );
1463 Debug( LDAP_DEBUG_ANY, "slap_sasl_listmech failed: %d\n",
1470 mechs = ldap_str2charray( mechstr, "," );
1472 #if SASL_VERSION_MAJOR < 2
1476 #elif defined(SLAP_BUILTIN_SASL)
1477 /* builtin SASL implementation */
1478 SASL_CTX *ctx = conn->c_sasl_authctx;
1479 if ( ctx != NULL && ctx->sc_external_id.bv_val ) {
1480 /* should check ssf */
1481 mechs = ldap_str2charray( "EXTERNAL", "," );
1488 int slap_sasl_close( Connection *conn )
1490 #ifdef HAVE_CYRUS_SASL
1491 sasl_conn_t *ctx = conn->c_sasl_authctx;
1494 sasl_dispose( &ctx );
1496 if ( conn->c_sasl_sockctx &&
1497 conn->c_sasl_authctx != conn->c_sasl_sockctx )
1499 ctx = conn->c_sasl_sockctx;
1500 sasl_dispose( &ctx );
1503 conn->c_sasl_authctx = NULL;
1504 conn->c_sasl_sockctx = NULL;
1505 conn->c_sasl_done = 0;
1507 free( conn->c_sasl_extra );
1508 conn->c_sasl_extra = NULL;
1510 #elif defined(SLAP_BUILTIN_SASL)
1511 SASL_CTX *ctx = conn->c_sasl_authctx;
1513 if( ctx->sc_external_id.bv_val ) {
1514 free( ctx->sc_external_id.bv_val );
1515 ctx->sc_external_id.bv_val = NULL;
1518 conn->c_sasl_authctx = NULL;
1522 return LDAP_SUCCESS;
1525 int slap_sasl_bind( Operation *op, SlapReply *rs )
1527 #ifdef HAVE_CYRUS_SASL
1528 sasl_conn_t *ctx = op->o_conn->c_sasl_authctx;
1529 struct berval response;
1530 unsigned reslen = 0;
1534 LDAP_LOG( TRANSPORT, ENTRY,
1535 "sasl_bind: dn=\"%s\" mech=%s datalen=%ld\n",
1536 op->o_req_dn.bv_len ? op->o_req_dn.bv_val : "",
1537 op->o_conn->c_sasl_bind_in_progress ? "<continuing>" :
1538 op->o_conn->c_sasl_bind_mech.bv_val,
1539 op->orb_cred.bv_len );
1541 Debug(LDAP_DEBUG_ARGS,
1542 "==> sasl_bind: dn=\"%s\" mech=%s datalen=%ld\n",
1543 op->o_req_dn.bv_len ? op->o_req_dn.bv_val : "",
1544 op->o_conn->c_sasl_bind_in_progress ? "<continuing>" :
1545 op->o_conn->c_sasl_bind_mech.bv_val,
1546 op->orb_cred.bv_len );
1550 send_ldap_error( op, rs, LDAP_UNAVAILABLE,
1551 "SASL unavailable on this session" );
1555 #if SASL_VERSION_MAJOR >= 2
1556 #define START( ctx, mech, cred, clen, resp, rlen, err ) \
1557 sasl_server_start( ctx, mech, cred, clen, resp, rlen )
1558 #define STEP( ctx, cred, clen, resp, rlen, err ) \
1559 sasl_server_step( ctx, cred, clen, resp, rlen )
1561 #define START( ctx, mech, cred, clen, resp, rlen, err ) \
1562 sasl_server_start( ctx, mech, cred, clen, resp, rlen, err )
1563 #define STEP( ctx, cred, clen, resp, rlen, err ) \
1564 sasl_server_step( ctx, cred, clen, resp, rlen, err )
1567 if ( !op->o_conn->c_sasl_bind_in_progress ) {
1568 /* If we already authenticated once, must use a new context */
1569 if ( op->o_conn->c_sasl_done ) {
1571 const char *authid = NULL;
1572 #if SASL_VERSION_MAJOR >= 2
1573 sasl_getprop( ctx, SASL_SSF_EXTERNAL, (void *)&ssf );
1574 sasl_getprop( ctx, SASL_AUTH_EXTERNAL, (void *)&authid );
1575 if ( authid ) authid = ch_strdup( authid );
1577 if ( ctx != op->o_conn->c_sasl_sockctx ) {
1578 sasl_dispose( &ctx );
1580 op->o_conn->c_sasl_authctx = NULL;
1582 slap_sasl_open( op->o_conn, 1 );
1583 ctx = op->o_conn->c_sasl_authctx;
1584 #if SASL_VERSION_MAJOR >= 2
1586 sasl_setprop( ctx, SASL_SSF_EXTERNAL, &ssf );
1587 sasl_setprop( ctx, SASL_AUTH_EXTERNAL, authid );
1588 ch_free( (char *)authid );
1593 op->o_conn->c_sasl_bind_mech.bv_val,
1594 op->orb_cred.bv_val, op->orb_cred.bv_len,
1595 (SASL_CONST char **)&response.bv_val, &reslen, &rs->sr_text );
1599 op->orb_cred.bv_val, op->orb_cred.bv_len,
1600 (SASL_CONST char **)&response.bv_val, &reslen, &rs->sr_text );
1603 response.bv_len = reslen;
1605 if ( sc == SASL_OK ) {
1606 sasl_ssf_t *ssf = NULL;
1608 op->orb_edn = op->o_conn->c_sasl_dn;
1609 op->o_conn->c_sasl_dn.bv_val = NULL;
1610 op->o_conn->c_sasl_dn.bv_len = 0;
1611 op->o_conn->c_sasl_done = 1;
1613 rs->sr_err = LDAP_SUCCESS;
1615 (void) sasl_getprop( ctx, SASL_SSF, (void *)&ssf );
1616 op->orb_ssf = ssf ? *ssf : 0;
1620 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1621 op->o_conn->c_sasl_layers++;
1623 /* If there's an old layer, set sockctx to NULL to
1624 * tell connection_read() to wait for us to finish.
1625 * Otherwise there is a race condition: we have to
1626 * send the Bind response using the old security
1627 * context and then remove it before reading any
1630 if ( op->o_conn->c_sasl_sockctx ) {
1631 ctx = op->o_conn->c_sasl_sockctx;
1632 op->o_conn->c_sasl_sockctx = NULL;
1634 op->o_conn->c_sasl_sockctx = op->o_conn->c_sasl_authctx;
1636 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1639 /* Must send response using old security layer */
1640 if (response.bv_len) rs->sr_sasldata = &response;
1641 send_ldap_sasl( op, rs );
1643 /* Now dispose of the old security layer.
1646 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1647 ldap_pvt_sasl_remove( op->o_conn->c_sb );
1648 op->o_conn->c_sasl_sockctx = op->o_conn->c_sasl_authctx;
1649 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1650 sasl_dispose( &ctx );
1652 } else if ( sc == SASL_CONTINUE ) {
1653 rs->sr_err = LDAP_SASL_BIND_IN_PROGRESS,
1654 rs->sr_sasldata = &response;
1655 send_ldap_sasl( op, rs );
1658 #if SASL_VERSION_MAJOR >= 2
1659 rs->sr_text = sasl_errdetail( ctx );
1661 rs->sr_err = slap_sasl_err2ldap( sc ),
1662 send_ldap_result( op, rs );
1665 #if SASL_VERSION_MAJOR < 2
1666 if( response.bv_len ) {
1667 ch_free( response.bv_val );
1672 LDAP_LOG( TRANSPORT, RESULTS, "slap_sasl_bind: rc=%d\n", rs->sr_err, 0, 0 );
1674 Debug(LDAP_DEBUG_TRACE, "<== slap_sasl_bind: rc=%d\n", rs->sr_err, 0, 0);
1677 #elif defined(SLAP_BUILTIN_SASL)
1678 /* built-in SASL implementation */
1679 SASL_CTX *ctx = op->o_conn->c_sasl_authctx;
1681 if ( ctx == NULL ) {
1682 send_ldap_error( op, rs, LDAP_OTHER,
1683 "Internal SASL Error" );
1685 } else if ( bvmatch( &ext_bv, &op->o_conn->c_sasl_bind_mech ) ) {
1688 if( op->orb_cred.bv_len ) {
1689 rs->sr_text = "proxy authorization not support";
1690 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1691 send_ldap_result( op, rs );
1694 op->orb_edn = ctx->sc_external_id;
1695 rs->sr_err = LDAP_SUCCESS;
1696 rs->sr_sasldata = NULL;
1697 send_ldap_sasl( op, rs );
1701 send_ldap_error( op, rs, LDAP_AUTH_METHOD_NOT_SUPPORTED,
1702 "requested SASL mechanism not supported" );
1705 send_ldap_error( op, rs, LDAP_AUTH_METHOD_NOT_SUPPORTED,
1706 "SASL not supported" );
1712 char* slap_sasl_secprops( const char *in )
1714 #ifdef HAVE_CYRUS_SASL
1715 int rc = ldap_pvt_sasl_secprops( in, &sasl_secprops );
1717 return rc == LDAP_SUCCESS ? NULL : "Invalid security properties";
1719 return "SASL not supported";
1723 #ifdef HAVE_CYRUS_SASL
1725 slap_sasl_setpass( Operation *op, SlapReply *rs )
1727 struct berval id = { 0, NULL }; /* needs to come from connection */
1728 struct berval new = { 0, NULL };
1729 struct berval old = { 0, NULL };
1731 assert( ber_bvcmp( &slap_EXOP_MODIFY_PASSWD, &op->ore_reqoid ) == 0 );
1733 rs->sr_err = sasl_getprop( op->o_conn->c_sasl_authctx, SASL_USERNAME,
1734 (SASL_CONST void **)&id.bv_val );
1736 if( rs->sr_err != SASL_OK ) {
1737 rs->sr_text = "unable to retrieve SASL username";
1738 rs->sr_err = LDAP_OTHER;
1743 LDAP_LOG( BACKEND, ENTRY,
1744 "slap_sasl_setpass: \"%s\"\n",
1745 id.bv_val ? id.bv_val : "", 0, 0);
1747 Debug( LDAP_DEBUG_ARGS, "==> slap_sasl_setpass: \"%s\"\n",
1748 id.bv_val ? id.bv_val : "", 0, 0 );
1751 rs->sr_err = slap_passwd_parse( op->ore_reqdata,
1752 NULL, &old, &new, &rs->sr_text );
1754 if( rs->sr_err != LDAP_SUCCESS ) {
1758 if( new.bv_len == 0 ) {
1759 slap_passwd_generate(&new);
1761 if( new.bv_len == 0 ) {
1762 rs->sr_text = "password generation failed.";
1763 rs->sr_err = LDAP_OTHER;
1767 rs->sr_rspdata = slap_passwd_return( &new );
1770 #if SASL_VERSION_MAJOR < 2
1771 rs->sr_err = sasl_setpass( op->o_conn->c_sasl_authctx,
1772 id.bv_val, new.bv_val, new.bv_len, 0, &rs->sr_text );
1774 rs->sr_err = sasl_setpass( op->o_conn->c_sasl_authctx, id.bv_val,
1775 new.bv_val, new.bv_len, old.bv_val, old.bv_len, 0 );
1776 if( rs->sr_err != SASL_OK ) {
1777 rs->sr_text = sasl_errdetail( op->o_conn->c_sasl_authctx );
1780 switch(rs->sr_err) {
1782 rs->sr_err = LDAP_SUCCESS;
1792 rs->sr_err = LDAP_OTHER;
1798 #endif /* HAVE_CYRUS_SASL */
1800 /* Take any sort of identity string and return a DN with the "dn:" prefix. The
1801 * string returned in *dn is in its own allocated memory, and must be free'd
1802 * by the calling process. -Mark Adamson, Carnegie Mellon
1804 * The "dn:" prefix is no longer used anywhere inside slapd. It is only used
1805 * on strings passed in directly from SASL. -Howard Chu, Symas Corp.
1812 int slap_sasl_getdn( Connection *conn, Operation *op, char *id, int len,
1813 char *user_realm, struct berval *dn, int flags )
1815 int rc, is_dn = SET_NONE, do_norm = 1;
1816 struct berval dn2, *mech;
1821 LDAP_LOG( TRANSPORT, ENTRY,
1822 "slap_sasl_getdn: conn %d id=%s [len=%d]\n",
1823 conn->c_connid, id ? (*id ? id : "<empty>") : "NULL", len );
1825 Debug( LDAP_DEBUG_ARGS, "slap_sasl_getdn: id=%s [len=%d]\n",
1826 id ? ( *id ? id : "<empty>" ) : "NULL", len, 0 );
1830 op = conn->c_sasl_bindop;
1837 if ( len == 0 ) len = strlen( id );
1839 /* Blatantly anonymous ID */
1840 if ( len == sizeof("anonymous") - 1 &&
1841 !strcasecmp( id, "anonymous" ) ) {
1842 return( LDAP_SUCCESS );
1848 if ( conn->c_sasl_bind_mech.bv_len ) {
1849 mech = &conn->c_sasl_bind_mech;
1851 mech = &conn->c_authmech;
1854 /* An authcID needs to be converted to authzID form. Set the
1855 * values directly into *dn; they will be normalized later. (and
1856 * normalizing always makes a new copy.) An ID from a TLS certificate
1857 * is already normalized, so copy it and skip normalization.
1859 if( flags & SLAP_GETDN_AUTHCID ) {
1860 if( bvmatch( mech, &ext_bv )) {
1861 /* EXTERNAL DNs are already normalized */
1864 ber_str2bv_x( id, len, 1, dn, op->o_tmpmemctx );
1867 /* convert to u:<username> form */
1873 if( is_dn == SET_NONE ) {
1874 if( !strncasecmp( id, "u:", sizeof("u:")-1 )) {
1878 } else if ( !strncasecmp( id, "dn:", sizeof("dn:")-1) ) {
1885 /* No other possibilities from here */
1886 if( is_dn == SET_NONE ) {
1889 return( LDAP_INAPPROPRIATE_AUTH );
1892 /* Username strings */
1893 if( is_dn == SET_U ) {
1895 struct berval realm = { 0, NULL }, c1 = *dn;
1897 len = dn->bv_len + sizeof("uid=")-1 + sizeof(",cn=auth")-1;
1899 if( user_realm && *user_realm ) {
1900 realm.bv_val = user_realm;
1901 realm.bv_len = strlen( user_realm );
1902 len += realm.bv_len + sizeof(",cn=") - 1;
1905 if( mech->bv_len ) {
1906 len += mech->bv_len + sizeof(",cn=")-1;
1909 /* Build the new dn */
1910 dn->bv_val = sl_malloc( len+1, op->o_tmpmemctx );
1911 if( dn->bv_val == NULL ) {
1913 LDAP_LOG( TRANSPORT, ERR,
1914 "slap_sasl_getdn: SLAP_MALLOC failed", 0, 0, 0 );
1916 Debug( LDAP_DEBUG_ANY,
1917 "slap_sasl_getdn: SLAP_MALLOC failed", 0, 0, 0 );
1921 p = lutil_strcopy( dn->bv_val, "uid=" );
1922 p = lutil_strncopy( p, c1.bv_val, c1.bv_len );
1924 if( realm.bv_len ) {
1925 p = lutil_strcopy( p, ",cn=" );
1926 p = lutil_strncopy( p, realm.bv_val, realm.bv_len );
1929 if( mech->bv_len ) {
1930 p = lutil_strcopy( p, ",cn=" );
1931 p = lutil_strcopy( p, mech->bv_val );
1933 p = lutil_strcopy( p, ",cn=auth" );
1934 dn->bv_len = p - dn->bv_val;
1937 LDAP_LOG( TRANSPORT, ENTRY,
1938 "slap_sasl_getdn: u:id converted to %s.\n", dn->bv_val, 0, 0 );
1940 Debug( LDAP_DEBUG_TRACE, "slap_sasl_getdn: u:id converted to %s\n", dn->bv_val,0,0 );
1944 /* Dup the DN in any case, so we don't risk
1945 * leaks or dangling pointers later,
1946 * and the DN value is '\0' terminated */
1947 ber_dupbv_x( &dn2, dn, op->o_tmpmemctx );
1948 dn->bv_val = dn2.bv_val;
1951 /* All strings are in DN form now. Normalize if needed. */
1953 rc = dnNormalize( 0, NULL, NULL, dn, &dn2, op->o_tmpmemctx );
1955 /* User DNs were constructed above and must be freed now */
1956 sl_free( dn->bv_val, op->o_tmpmemctx );
1958 if ( rc != LDAP_SUCCESS ) {
1966 /* Run thru regexp */
1967 slap_sasl2dn( op, dn, &dn2, flags );
1969 sl_free( dn->bv_val, op->o_tmpmemctx );
1972 LDAP_LOG( TRANSPORT, ENTRY,
1973 "slap_sasl_getdn: dn:id converted to %s.\n", dn->bv_val, 0, 0 );
1975 Debug( LDAP_DEBUG_TRACE, "getdn: dn:id converted to %s\n",
1980 return( LDAP_SUCCESS );