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[] = { "*authcDN", "*authzDN", NULL };
466 sasl_server_params_t *sparams,
473 const struct propval *list;
475 AttributeDescription *ad;
478 list = sparams->utils->prop_get( sparams->propctx );
480 /* Find our DN first */
481 for( i = 0, last = 0; list[i].name; i++ ) {
482 if ( list[i].name[0] == '*' ) {
483 if ( (flags & SASL_AUXPROP_AUTHZID) &&
484 !strcmp( list[i].name, slap_propnames[1] ) ) {
485 if ( list[i].values && list[i].values[0] )
486 AC_MEMCPY( &dn, list[i].values[0], sizeof( dn ) );
487 if ( !last ) last = i;
490 if ( !strcmp( list[i].name, slap_propnames[0] ) ) {
491 if ( !last ) last = i;
492 if ( list[i].values && list[i].values[0] ) {
493 AC_MEMCPY( &dn, list[i].values[0], sizeof( dn ) );
494 if ( !(flags & SASL_AUXPROP_AUTHZID) )
501 /* Now fetch the rest */
502 for( i = 0; i < last; i++ ) {
503 const char *name = list[i].name;
505 if ( name[0] == '*' ) {
506 if ( flags & SASL_AUXPROP_AUTHZID ) continue;
508 } else if ( !(flags & SASL_AUXPROP_AUTHZID ) )
511 if ( list[i].values ) {
512 if ( !(flags & SASL_AUXPROP_OVERRIDE) ) continue;
513 sparams->utils->prop_erase( sparams->propctx, list[i].name );
516 rc = slap_str2ad( name, &ad, &text );
517 if ( rc != LDAP_SUCCESS ) {
519 LDAP_LOG( TRANSPORT, DETAIL1,
520 "slap_auxprop: str2ad(%s): %s\n", name, text, 0 );
522 Debug( LDAP_DEBUG_TRACE,
523 "slap_auxprop: str2ad(%s): %s\n", name, text, 0 );
525 rc = slap_str2undef_ad( name, &ad, &text );
526 if ( rc != LDAP_SUCCESS ) continue;
528 rc = backend_attribute( NULL,NULL,NULL,NULL, &dn, ad, &vals );
529 if ( rc != LDAP_SUCCESS ) continue;
530 for ( bv = vals; bv->bv_val; bv++ ) {
531 sparams->utils->prop_set( sparams->propctx, list[i].name,
532 bv->bv_val, bv->bv_len );
534 ber_bvarray_free( vals );
538 static sasl_auxprop_plug_t slap_auxprop_plugin = {
541 NULL, /* glob_context */
542 NULL, /* auxprop_free */
550 const sasl_utils_t *utils,
553 sasl_auxprop_plug_t **plug,
554 const char *plugname)
556 if ( !out_version | !plug ) return SASL_BADPARAM;
558 if ( max_version < SASL_AUXPROP_PLUG_VERSION ) return SASL_BADVERS;
560 *out_version = SASL_AUXPROP_PLUG_VERSION;
561 *plug = &slap_auxprop_plugin;
569 const char *username,
572 struct propctx *propctx)
574 Connection *conn = (Connection *)context;
575 struct berval dn, cred;
579 cred.bv_val = (char *)pass;
580 cred.bv_len = passlen;
582 /* SASL will fallback to its own mechanisms if we don't
583 * find an answer here.
586 rc = slap_sasl_getdn( conn, (char *)username, 0, NULL, &dn,
587 FLAG_GETDN_AUTHCID );
588 if ( rc != LDAP_SUCCESS ) {
589 sasl_seterror( sconn, 0, ldap_err2string( rc ) );
593 if ( dn.bv_len == 0 ) {
594 sasl_seterror( sconn, 0,
595 "No password is associated with the Root DSE" );
596 if ( dn.bv_val != NULL ) {
597 ch_free( dn.bv_val );
602 rc = backend_attribute( NULL, NULL, NULL, NULL, &dn,
603 slap_schema.si_ad_userPassword, &vals);
604 if ( rc != LDAP_SUCCESS ) {
605 ch_free( dn.bv_val );
606 sasl_seterror( sconn, 0, ldap_err2string( rc ) );
607 return SASL_NOVERIFY;
612 if ( vals != NULL ) {
613 for ( bv = vals; bv->bv_val != NULL; bv++ ) {
614 if ( !lutil_passwd( bv, &cred, NULL ) ) {
619 ber_bvarray_free( vals );
622 if ( rc != SASL_OK ) {
623 sasl_seterror( sconn, 0,
624 ldap_err2string( LDAP_INVALID_CREDENTIALS ) );
627 ch_free( dn.bv_val );
632 /* Convert a SASL authcid or authzid into a DN. Store the DN in an
633 * auxiliary property, so that we can refer to it in sasl_authorize
634 * without interfering with anything else. Also, the SASL username
635 * buffer is constrained to 256 characters, and our DNs could be
636 * much longer (totally arbitrary length)...
639 slap_sasl_canonicalize(
645 const char *user_realm,
650 Connection *conn = (Connection *)context;
651 struct propctx *props = sasl_auxprop_getctx( sconn );
652 struct propval auxvals[3];
655 const char *names[2];
660 LDAP_LOG( TRANSPORT, ENTRY,
661 "slap_sasl_canonicalize: conn %d %s=\"%s\"\n",
662 conn ? conn->c_connid : -1,
663 (flags & SASL_CU_AUTHID) ? "authcid" : "authzid", in ? in : "<empty>");
665 Debug( LDAP_DEBUG_ARGS, "SASL Canonicalize [conn=%ld]: "
667 conn ? conn->c_connid : -1,
668 (flags & SASL_CU_AUTHID) ? "authcid" : "authzid",
669 in ? in : "<empty>" );
672 /* If name is too big, just truncate. We don't care, we're
673 * using DNs, not the usernames.
675 if ( inlen > out_max )
678 /* See if we need to add request, can only do it once */
679 prop_getnames( props, slap_propnames, auxvals );
680 if ( !auxvals[0].name )
681 prop_request( props, slap_propnames );
683 if ( flags & SASL_CU_AUTHID )
688 /* Already been here? */
689 if ( auxvals[which].values )
692 if ( flags == SASL_CU_AUTHZID ) {
693 /* If we got unqualified authzid's, they probably came from SASL
694 * itself just passing the authcid to us. Look inside the oparams
695 * structure to see if that's true. (HACK: the out_len pointer is
696 * the address of a member of a sasl_out_params_t structure...)
698 sasl_out_params_t dummy;
699 int offset = (void *)&dummy.ulen - (void *)&dummy.authid;
700 char **authid = (void *)out_len - offset;
701 if ( *authid && !strcmp( in, *authid ) )
705 rc = slap_sasl_getdn( conn, (char *)in, inlen, (char *)user_realm, &dn,
706 (flags & SASL_CU_AUTHID) ? FLAG_GETDN_AUTHCID : FLAG_GETDN_AUTHZID );
707 if ( rc != LDAP_SUCCESS ) {
708 sasl_seterror( sconn, 0, ldap_err2string( rc ) );
712 names[0] = slap_propnames[which];
715 prop_set( props, names[0], (char *)&dn, sizeof( dn ) );
718 LDAP_LOG( TRANSPORT, ENTRY,
719 "slap_sasl_canonicalize: conn %d %s=\"%s\"\n",
720 conn ? conn->c_connid : -1, names[0]+1, dn.bv_val );
722 Debug( LDAP_DEBUG_ARGS, "SASL Canonicalize [conn=%ld]: "
724 conn ? conn->c_connid : -1,
725 names[0]+1, dn.bv_val );
727 done: AC_MEMCPY( out, in, inlen );
739 char *requested_user,
743 const char *def_realm,
745 struct propctx *props)
747 Connection *conn = (Connection *)context;
748 struct propval auxvals[3];
749 struct berval authcDN, authzDN;
753 LDAP_LOG( TRANSPORT, ENTRY,
754 "slap_sasl_authorize: conn %d authcid=\"%s\" authzid=\"%s\"\n",
755 conn ? conn->c_connid : -1, auth_identity, requested_user);
757 Debug( LDAP_DEBUG_ARGS, "SASL Authorize [conn=%ld]: "
758 "authcid=\"%s\" authzid=\"%s\"\n",
759 conn ? conn->c_connid : -1, auth_identity, requested_user );
761 if ( conn->c_sasl_dn.bv_val ) {
762 ch_free( conn->c_sasl_dn.bv_val );
763 conn->c_sasl_dn.bv_val = NULL;
764 conn->c_sasl_dn.bv_len = 0;
767 prop_getnames( props, slap_propnames, auxvals );
769 AC_MEMCPY( &authcDN, auxvals[0].values[0], sizeof(authcDN) );
771 /* Nothing to do if no authzID was given */
772 if ( !auxvals[1].name || !auxvals[1].values ) {
773 conn->c_sasl_dn = authcDN;
777 AC_MEMCPY( &authzDN, auxvals[1].values[0], sizeof(authzDN) );
779 rc = slap_sasl_authorized( conn, &authcDN, &authzDN );
780 ch_free( authcDN.bv_val );
781 if ( rc != LDAP_SUCCESS ) {
783 LDAP_LOG( TRANSPORT, INFO,
784 "slap_sasl_authorize: conn %ld authorization disallowed (%d)\n",
785 (long)(conn ? conn->c_connid : -1), rc, 0 );
787 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
788 " authorization disallowed (%d)\n",
789 (long) (conn ? conn->c_connid : -1), rc, 0 );
792 sasl_seterror( sconn, 0, "not authorized" );
793 ch_free( authzDN.bv_val );
797 conn->c_sasl_dn = authzDN;
800 LDAP_LOG( TRANSPORT, ENTRY,
801 "slap_sasl_authorize: conn %d authorization allowed\n",
802 (long)(conn ? conn->c_connid : -1), 0, 0 );
804 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
805 " authorization allowed\n",
806 (long) (conn ? conn->c_connid : -1), 0, 0 );
819 struct berval authcDN, authzDN;
821 Connection *conn = context;
825 if ( conn->c_sasl_dn.bv_val ) {
826 ch_free( conn->c_sasl_dn.bv_val );
827 conn->c_sasl_dn.bv_val = NULL;
828 conn->c_sasl_dn.bv_len = 0;
832 LDAP_LOG( TRANSPORT, ENTRY,
833 "slap_sasl_authorize: conn %d authcid=\"%s\" authzid=\"%s\"\n",
834 conn ? conn->c_connid : -1, authcid ? authcid : "<empty>",
835 authzid ? authzid : "<empty>" );
837 Debug( LDAP_DEBUG_ARGS, "SASL Authorize [conn=%ld]: "
838 "authcid=\"%s\" authzid=\"%s\"\n",
839 (long) (conn ? conn->c_connid : -1),
840 authcid ? authcid : "<empty>",
841 authzid ? authzid : "<empty>" );
844 /* Figure out how much data we have for the dn */
845 rc = sasl_getprop( conn->c_sasl_context, SASL_REALM, (void **)&realm );
846 if( rc != SASL_OK && rc != SASL_NOTDONE ) {
848 LDAP_LOG( TRANSPORT, ERR,
849 "slap_sasl_authorize: getprop(REALM) failed.\n", 0, 0, 0 );
851 Debug(LDAP_DEBUG_TRACE,
852 "authorize: getprop(REALM) failed!\n", 0,0,0);
854 *errstr = "Could not extract realm";
858 /* Convert the identities to DN's. If no authzid was given, client will
859 be bound as the DN matching their username */
860 rc = slap_sasl_getdn( conn, (char *)authcid, 0, realm, &authcDN, FLAG_GETDN_AUTHCID );
861 if( rc != LDAP_SUCCESS ) {
862 *errstr = ldap_err2string( rc );
865 if( ( authzid == NULL ) || !strcmp( authcid,authzid ) ) {
867 LDAP_LOG( TRANSPORT, ENTRY,
868 "slap_sasl_authorize: conn %d Using authcDN=%s\n",
869 conn ? conn->c_connid : -1, authcDN.bv_val, 0 );
871 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
872 "Using authcDN=%s\n", (long) (conn ? conn->c_connid : -1), authcDN.bv_val,0 );
875 conn->c_sasl_dn = authcDN;
879 rc = slap_sasl_getdn( conn, (char *)authzid, 0, realm, &authzDN, FLAG_GETDN_AUTHZID );
880 if( rc != LDAP_SUCCESS ) {
881 ch_free( authcDN.bv_val );
882 *errstr = ldap_err2string( rc );
886 rc = slap_sasl_authorized(conn, &authcDN, &authzDN );
887 ch_free( authcDN.bv_val );
890 LDAP_LOG( TRANSPORT, INFO,
891 "slap_sasl_authorize: conn %ld authorization disallowed (%d)\n",
892 (long)(conn ? conn->c_connid : -1), rc, 0 );
894 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
895 " authorization disallowed (%d)\n",
896 (long) (conn ? conn->c_connid : -1), rc, 0 );
899 *errstr = "not authorized";
900 ch_free( authzDN.bv_val );
905 LDAP_LOG( TRANSPORT, RESULTS,
906 "slap_sasl_authorize: conn %d authorization allowed\n",
907 (long)(conn ? conn->c_connid : -1 ), 0, 0 );
909 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
910 " authorization allowed\n",
911 (long) (conn ? conn->c_connid : -1), 0, 0 );
914 conn->c_sasl_dn = authzDN;
918 #endif /* SASL_VERSION_MAJOR >= 2 */
921 slap_sasl_err2ldap( int saslerr )
927 rc = LDAP_SASL_BIND_IN_PROGRESS;
936 rc = LDAP_AUTH_METHOD_NOT_SUPPORTED;
939 rc = LDAP_INVALID_CREDENTIALS;
942 rc = LDAP_INSUFFICIENT_ACCESS;
946 rc = LDAP_INAPPROPRIATE_AUTH;
958 int slap_sasl_init( void )
960 #ifdef HAVE_CYRUS_SASL
962 static sasl_callback_t server_callbacks[] = {
963 { SASL_CB_LOG, &slap_sasl_log, NULL },
964 { SASL_CB_LIST_END, NULL, NULL }
974 ldap_pvt_sasl_mutex_new,
975 ldap_pvt_sasl_mutex_lock,
976 ldap_pvt_sasl_mutex_unlock,
977 ldap_pvt_sasl_mutex_dispose );
979 #if SASL_VERSION_MAJOR >= 2
980 sasl_auxprop_add_plugin( "slapd", slap_auxprop_init );
982 /* should provide callbacks for logging */
983 /* server name should be configurable */
984 rc = sasl_server_init( server_callbacks, "slapd" );
986 if( rc != SASL_OK ) {
988 LDAP_LOG( TRANSPORT, INFO, "slap_sasl_init: init failed.\n", 0, 0, 0 );
990 Debug( LDAP_DEBUG_ANY, "sasl_server_init failed\n",
998 LDAP_LOG( TRANSPORT, INFO, "slap_sasl_init: initialized!\n", 0, 0, 0 );
1000 Debug( LDAP_DEBUG_TRACE, "slap_sasl_init: initialized!\n",
1005 /* default security properties */
1006 memset( &sasl_secprops, '\0', sizeof(sasl_secprops) );
1007 sasl_secprops.max_ssf = INT_MAX;
1008 sasl_secprops.maxbufsize = 65536;
1009 sasl_secprops.security_flags = SASL_SEC_NOPLAINTEXT|SASL_SEC_NOANONYMOUS;
1015 int slap_sasl_destroy( void )
1017 #ifdef HAVE_CYRUS_SASL
1020 free( global_host );
1026 int slap_sasl_open( Connection *conn )
1028 int cb, sc = LDAP_SUCCESS;
1029 #if SASL_VERSION_MAJOR >= 2
1030 char *ipremoteport = NULL, *iplocalport = NULL;
1033 #ifdef HAVE_CYRUS_SASL
1034 sasl_conn_t *ctx = NULL;
1035 sasl_callback_t *session_callbacks;
1037 assert( conn->c_sasl_context == NULL );
1038 assert( conn->c_sasl_extra == NULL );
1040 conn->c_sasl_layers = 0;
1043 #if SASL_VERSION_MAJOR >= 2
1044 ch_calloc( 5, sizeof(sasl_callback_t));
1046 ch_calloc( 3, sizeof(sasl_callback_t));
1048 conn->c_sasl_extra = session_callbacks;
1050 session_callbacks[cb=0].id = SASL_CB_LOG;
1051 session_callbacks[cb].proc = &slap_sasl_log;
1052 session_callbacks[cb++].context = conn;
1054 session_callbacks[cb].id = SASL_CB_PROXY_POLICY;
1055 session_callbacks[cb].proc = &slap_sasl_authorize;
1056 session_callbacks[cb++].context = conn;
1058 #if SASL_VERSION_MAJOR >= 2
1059 session_callbacks[cb].id = SASL_CB_CANON_USER;
1060 session_callbacks[cb].proc = &slap_sasl_canonicalize;
1061 session_callbacks[cb++].context = conn;
1063 /* XXXX: this should be conditional */
1064 session_callbacks[cb].id = SASL_CB_SERVER_USERDB_CHECKPASS;
1065 session_callbacks[cb].proc = &slap_sasl_checkpass;
1066 session_callbacks[cb++].context = conn;
1069 session_callbacks[cb].id = SASL_CB_LIST_END;
1070 session_callbacks[cb].proc = NULL;
1071 session_callbacks[cb++].context = NULL;
1073 if( global_host == NULL ) {
1074 global_host = ldap_pvt_get_fqdn( NULL );
1077 /* create new SASL context */
1078 #if SASL_VERSION_MAJOR >= 2
1079 if ( conn->c_sock_name.bv_len != 0 &&
1080 strncmp( conn->c_sock_name.bv_val, "IP=", 3 ) == 0) {
1083 iplocalport = ch_strdup( conn->c_sock_name.bv_val + 3 );
1084 /* Convert IPv6 addresses to address;port syntax. */
1085 p = strrchr( iplocalport, ' ' );
1086 /* Convert IPv4 addresses to address;port syntax. */
1087 if ( p == NULL ) p = strchr( iplocalport, ':' );
1092 if ( conn->c_peer_name.bv_len != 0 &&
1093 strncmp( conn->c_peer_name.bv_val, "IP=", 3 ) == 0) {
1096 ipremoteport = ch_strdup( conn->c_peer_name.bv_val + 3 );
1097 /* Convert IPv6 addresses to address;port syntax. */
1098 p = strrchr( ipremoteport, ' ' );
1099 /* Convert IPv4 addresses to address;port syntax. */
1100 if ( p == NULL ) p = strchr( ipremoteport, ':' );
1105 sc = sasl_server_new( "ldap", global_host, global_realm,
1106 iplocalport, ipremoteport, session_callbacks, 0, &ctx );
1107 if ( iplocalport != NULL ) {
1108 ch_free( iplocalport );
1110 if ( ipremoteport != NULL ) {
1111 ch_free( ipremoteport );
1114 sc = sasl_server_new( "ldap", global_host, global_realm,
1115 session_callbacks, SASL_SECURITY_LAYER, &ctx );
1118 if( sc != SASL_OK ) {
1120 LDAP_LOG( TRANSPORT, ERR,
1121 "slap_sasl_open: sasl_server_new failed: %d\n", sc, 0, 0 );
1123 Debug( LDAP_DEBUG_ANY, "sasl_server_new failed: %d\n",
1130 conn->c_sasl_context = ctx;
1132 if( sc == SASL_OK ) {
1133 sc = sasl_setprop( ctx,
1134 SASL_SEC_PROPS, &sasl_secprops );
1136 if( sc != SASL_OK ) {
1138 LDAP_LOG( TRANSPORT, ERR,
1139 "slap_sasl_open: sasl_setprop failed: %d \n", sc, 0, 0 );
1141 Debug( LDAP_DEBUG_ANY, "sasl_setprop failed: %d\n",
1145 slap_sasl_close( conn );
1150 sc = slap_sasl_err2ldap( sc );
1155 int slap_sasl_external(
1158 const char *auth_id )
1160 #if SASL_VERSION_MAJOR >= 2
1162 sasl_conn_t *ctx = conn->c_sasl_context;
1164 if ( ctx == NULL ) {
1165 return LDAP_UNAVAILABLE;
1168 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL, &ssf );
1170 if ( sc != SASL_OK ) {
1174 sc = sasl_setprop( ctx, SASL_AUTH_EXTERNAL, auth_id );
1176 if ( sc != SASL_OK ) {
1180 #elif defined(HAVE_CYRUS_SASL)
1182 sasl_conn_t *ctx = conn->c_sasl_context;
1183 sasl_external_properties_t extprops;
1185 if ( ctx == NULL ) {
1186 return LDAP_UNAVAILABLE;
1189 memset( &extprops, '\0', sizeof(extprops) );
1191 extprops.auth_id = (char *) auth_id;
1193 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL,
1194 (void *) &extprops );
1196 if ( sc != SASL_OK ) {
1201 return LDAP_SUCCESS;
1204 int slap_sasl_reset( Connection *conn )
1206 #ifdef HAVE_CYRUS_SASL
1207 sasl_conn_t *ctx = conn->c_sasl_context;
1212 /* must return "anonymous" */
1213 return LDAP_SUCCESS;
1216 char ** slap_sasl_mechs( Connection *conn )
1218 char **mechs = NULL;
1220 #ifdef HAVE_CYRUS_SASL
1221 sasl_conn_t *ctx = conn->c_sasl_context;
1225 SASL_CONST char *mechstr;
1227 sc = sasl_listmech( ctx,
1228 NULL, NULL, ",", NULL,
1229 &mechstr, NULL, NULL );
1231 if( sc != SASL_OK ) {
1233 LDAP_LOG( TRANSPORT, ERR,
1234 "slap_sasl_mechs: sasl_listmech failed: %d\n", sc, 0, 0 );
1236 Debug( LDAP_DEBUG_ANY, "slap_sasl_listmech failed: %d\n",
1243 mechs = str2charray( mechstr, "," );
1245 #if SASL_VERSION_MAJOR < 2
1254 int slap_sasl_close( Connection *conn )
1256 #ifdef HAVE_CYRUS_SASL
1257 sasl_conn_t *ctx = conn->c_sasl_context;
1260 sasl_dispose( &ctx );
1263 conn->c_sasl_context = NULL;
1265 free( conn->c_sasl_extra );
1266 conn->c_sasl_extra = NULL;
1269 return LDAP_SUCCESS;
1277 struct berval *cred,
1283 #ifdef HAVE_CYRUS_SASL
1284 sasl_conn_t *ctx = conn->c_sasl_context;
1285 struct berval response;
1286 unsigned reslen = 0;
1287 const char *errstr = NULL;
1291 LDAP_LOG( TRANSPORT, ENTRY,
1292 "sasl_bind: dn=\"%s\" mech=%s datalen=%ld\n",
1293 dn->bv_len ? dn->bv_val : "",
1294 conn->c_sasl_bind_in_progress ? "<continuing>" :
1295 conn->c_sasl_bind_mech.bv_val,
1296 cred ? cred->bv_len : 0 );
1298 Debug(LDAP_DEBUG_ARGS,
1299 "==> sasl_bind: dn=\"%s\" mech=%s datalen=%ld\n",
1300 dn->bv_len ? dn->bv_val : "",
1301 conn->c_sasl_bind_in_progress ? "<continuing>":conn->c_sasl_bind_mech.bv_val,
1302 cred ? cred->bv_len : 0 );
1307 send_ldap_result( conn, op, LDAP_UNAVAILABLE,
1308 NULL, "SASL unavailable on this session", NULL, NULL );
1312 #if SASL_VERSION_MAJOR >= 2
1313 #define START( ctx, mech, cred, clen, resp, rlen, err ) \
1314 sasl_server_start( ctx, mech, cred, clen, resp, rlen )
1315 #define STEP( ctx, cred, clen, resp, rlen, err ) \
1316 sasl_server_step( ctx, cred, clen, resp, rlen )
1318 #define START( ctx, mech, cred, clen, resp, rlen, err ) \
1319 sasl_server_start( ctx, mech, cred, clen, resp, rlen, err )
1320 #define STEP( ctx, cred, clen, resp, rlen, err ) \
1321 sasl_server_step( ctx, cred, clen, resp, rlen, err )
1324 if ( !conn->c_sasl_bind_in_progress ) {
1326 conn->c_sasl_bind_mech.bv_val,
1327 cred->bv_len ? cred->bv_val : "",
1329 (SASL_CONST char **)&response.bv_val, &reslen, &errstr );
1333 cred->bv_val, cred->bv_len,
1334 (SASL_CONST char **)&response.bv_val, &reslen, &errstr );
1337 response.bv_len = reslen;
1339 if ( sc == SASL_OK ) {
1340 sasl_ssf_t *ssf = NULL;
1342 *edn = conn->c_sasl_dn;
1343 conn->c_sasl_dn.bv_val = NULL;
1344 conn->c_sasl_dn.bv_len = 0;
1348 (void) sasl_getprop( ctx, SASL_SSF, (void *)&ssf );
1349 *ssfp = ssf ? *ssf : 0;
1352 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1353 conn->c_sasl_layers++;
1354 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1357 send_ldap_sasl( conn, op, rc,
1358 NULL, NULL, NULL, NULL,
1359 response.bv_len ? &response : NULL );
1361 } else if ( sc == SASL_CONTINUE ) {
1362 send_ldap_sasl( conn, op, rc = LDAP_SASL_BIND_IN_PROGRESS,
1363 NULL, NULL, NULL, NULL, &response );
1366 #if SASL_VERSION_MAJOR >= 2
1367 errstr = sasl_errdetail( ctx );
1369 send_ldap_result( conn, op, rc = slap_sasl_err2ldap( sc ),
1370 NULL, errstr, NULL, NULL );
1373 #if SASL_VERSION_MAJOR < 2
1374 if( response.bv_len ) {
1375 ch_free( response.bv_val );
1380 LDAP_LOG( TRANSPORT, RESULTS, "slap_sasl_bind: rc=%d\n", rc, 0, 0 );
1382 Debug(LDAP_DEBUG_TRACE, "<== slap_sasl_bind: rc=%d\n", rc, 0, 0);
1387 send_ldap_result( conn, op, rc = LDAP_UNAVAILABLE,
1388 NULL, "SASL not supported", NULL, NULL );
1394 char* slap_sasl_secprops( const char *in )
1396 #ifdef HAVE_CYRUS_SASL
1397 int rc = ldap_pvt_sasl_secprops( in, &sasl_secprops );
1399 return rc == LDAP_SUCCESS ? NULL : "Invalid security properties";
1401 return "SASL not supported";
1405 #ifdef HAVE_CYRUS_SASL
1411 struct berval *reqdata,
1413 struct berval **rspdata,
1414 LDAPControl *** rspctrls,
1418 struct berval id = { 0, NULL }; /* needs to come from connection */
1419 struct berval new = { 0, NULL };
1420 struct berval old = { 0, NULL };
1422 assert( reqoid != NULL );
1423 assert( strcmp( LDAP_EXOP_MODIFY_PASSWD, reqoid ) == 0 );
1425 rc = sasl_getprop( conn->c_sasl_context, SASL_USERNAME,
1426 (SASL_CONST void **)&id.bv_val );
1428 if( rc != SASL_OK ) {
1429 *text = "unable to retrieve SASL username";
1435 LDAP_LOG( BACKEND, ENTRY,
1436 "slap_sasl_setpass: \"%s\"\n",
1437 id.bv_val ? id.bv_val : "", 0, 0);
1439 Debug( LDAP_DEBUG_ARGS, "==> slap_sasl_setpass: \"%s\"\n",
1440 id.bv_val ? id.bv_val : "", 0, 0 );
1443 rc = slap_passwd_parse( reqdata,
1444 NULL, &old, &new, text );
1446 if( rc != LDAP_SUCCESS ) {
1450 if( new.bv_len == 0 ) {
1451 slap_passwd_generate(&new);
1453 if( new.bv_len == 0 ) {
1454 *text = "password generation failed.";
1459 *rspdata = slap_passwd_return( &new );
1462 #if SASL_VERSION_MAJOR < 2
1463 rc = sasl_setpass( conn->c_sasl_context,
1464 id.bv_val, new.bv_val, new.bv_len, 0, text );
1466 rc = sasl_setpass( conn->c_sasl_context, id.bv_val,
1467 new.bv_val, new.bv_len, old.bv_val, old.bv_len, 0 );
1468 if( rc != SASL_OK ) {
1469 *text = sasl_errdetail( conn->c_sasl_context );