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>
27 #if SASL_VERSION_MAJOR >= 2
29 #include <sasl/saslplug.h>
30 #define SASL_CONST const
41 /* Flags for telling slap_sasl_getdn() what type of identity is being passed */
42 #define FLAG_GETDN_AUTHCID 2
43 #define FLAG_GETDN_AUTHZID 4
45 static sasl_security_properties_t sasl_secprops;
47 int slap_sasl_config( int cargc, char **cargv, char *line,
48 const char *fname, int lineno )
50 /* set SASL proxy authorization policy */
51 if ( strcasecmp( cargv[0], "sasl-authz-policy" ) == 0 ) {
54 LDAP_LOG(( "config", LDAP_LEVEL_CRIT,
55 "%s: line %d: missing policy in \"sasl-authz-policy <policy>\" line\n",
58 Debug( LDAP_DEBUG_ANY,
59 "%s: line %d: missing policy in \"sasl-authz-policy <policy>\" line\n",
65 if ( slap_sasl_setpolicy( cargv[1] ) ) {
67 LDAP_LOG(( "config", LDAP_LEVEL_CRIT,
68 "%s: line %d: unable "
69 "to parse value \"%s\" "
70 "in \"sasl-authz-policy "
72 fname, lineno, cargv[1] ));
74 Debug( LDAP_DEBUG_ANY,
75 "%s: line %d: unable "
76 "to parse value \"%s\" "
77 "in \"sasl-authz-policy "
79 fname, lineno, cargv[1] );
86 } else if ( strcasecmp( cargv[0], "sasl-host" ) == 0 ) {
89 LDAP_LOG(( "config", LDAP_LEVEL_CRIT,
90 "%s: line %d: missing host in \"sasl-host <host>\" line\n",
93 Debug( LDAP_DEBUG_ANY,
94 "%s: line %d: missing host in \"sasl-host <host>\" line\n",
101 if ( global_host != NULL ) {
103 LDAP_LOG(( "config", LDAP_LEVEL_CRIT,
104 "%s: line %d: already set sasl-host!\n",
107 Debug( LDAP_DEBUG_ANY,
108 "%s: line %d: already set sasl-host!\n",
115 global_host = ch_strdup( cargv[1] );
119 } else if ( strcasecmp( cargv[0], "sasl-realm" ) == 0 ) {
122 LDAP_LOG(( "config", LDAP_LEVEL_CRIT,
123 "%s: line %d: missing realm in \"sasl-realm <realm>\" line.\n",
126 Debug( LDAP_DEBUG_ANY,
127 "%s: line %d: missing realm in \"sasl-realm <realm>\" line\n",
134 if ( global_realm != NULL ) {
136 LDAP_LOG(( "config", LDAP_LEVEL_CRIT,
137 "%s: line %d: already set sasl-realm!\n",
140 Debug( LDAP_DEBUG_ANY,
141 "%s: line %d: already set sasl-realm!\n",
148 global_realm = ch_strdup( cargv[1] );
151 } else if ( !strcasecmp( cargv[0], "sasl-regexp" )
152 || !strcasecmp( cargv[0], "saslregexp" ) )
157 LDAP_LOG(( "config", LDAP_LEVEL_CRIT,
158 "%s: line %d: need 2 args in "
159 "\"saslregexp <match> <replace>\"\n",
162 Debug( LDAP_DEBUG_ANY,
163 "%s: line %d: need 2 args in \"saslregexp <match> <replace>\"\n",
169 rc = slap_sasl_regexp_config( cargv[1], cargv[2] );
174 /* SASL security properties */
175 } else if ( strcasecmp( cargv[0], "sasl-secprops" ) == 0 ) {
180 LDAP_LOG(( "config", LDAP_LEVEL_CRIT,
181 "%s: line %d: missing flags in "
182 "\"sasl-secprops <properties>\" line\n",
185 Debug( LDAP_DEBUG_ANY,
186 "%s: line %d: missing flags in \"sasl-secprops <properties>\" line\n",
193 txt = slap_sasl_secprops( cargv[1] );
196 LDAP_LOG(( "config", LDAP_LEVEL_CRIT,
197 "%s: line %d sasl-secprops: %s\n",
198 fname, lineno, txt ));
200 Debug( LDAP_DEBUG_ANY,
201 "%s: line %d: sasl-secprops: %s\n",
202 fname, lineno, txt );
218 Connection *conn = context;
222 if ( message == NULL ) {
223 return SASL_BADPARAM;
227 #if SASL_VERSION_MAJOR >= 2
229 level = LDAP_DEBUG_NONE;
233 level = LDAP_DEBUG_ANY;
237 level = LDAP_DEBUG_ANY;
241 level = LDAP_DEBUG_TRACE;
245 level = LDAP_DEBUG_TRACE;
249 level = LDAP_DEBUG_TRACE;
253 level = LDAP_DEBUG_TRACE;
257 level = LDAP_DEBUG_TRACE;
258 label = "Password Trace";
262 level = LDAP_DEBUG_ANY;
265 case SASL_LOG_WARNING:
266 level = LDAP_DEBUG_TRACE;
270 level = LDAP_DEBUG_TRACE;
275 return SASL_BADPARAM;
279 LDAP_LOG( TRANSPORT, ENTRY,
280 "SASL [conn=%ld] %s: %s\n", conn ? conn->c_connid : -1, label, message);
282 Debug( level, "SASL [conn=%ld] %s: %s\n",
283 conn ? conn->c_connid: -1,
292 /* Take any sort of identity string and return a DN with the "dn:" prefix. The
293 string returned in *dn is in its own allocated memory, and must be free'd
294 by the calling process.
295 -Mark Adamson, Carnegie Mellon
297 The "dn:" prefix is no longer used anywhere inside slapd. It is only used
298 on strings passed in directly from SASL.
299 -Howard Chu, Symas Corp.
305 static struct berval ext_bv = { sizeof("EXTERNAL")-1, "EXTERNAL" };
307 int slap_sasl_getdn( Connection *conn, char *id, int len,
308 char *user_realm, struct berval *dn, int flags )
311 int rc, is_dn = 0, do_norm = 1;
316 LDAP_LOG( TRANSPORT, ENTRY,
317 "slap_sasl_getdn: conn %d id=%s\n",
318 conn ? conn->c_connid : -1, id ? (*id ? id : "<empty>") : "NULL" );
320 Debug( LDAP_DEBUG_ARGS, "slap_sasl_getdn: id=%s\n",
321 id?(*id?id:"<empty>"):"NULL",0,0 );
328 if ( len == 0 ) len = strlen( id );
330 /* Blatantly anonymous ID */
331 if ( len == sizeof("anonymous") - 1 &&
332 !strcasecmp( id, "anonymous" ) ) {
333 return( LDAP_SUCCESS );
339 ctx = conn->c_sasl_context;
341 /* An authcID needs to be converted to authzID form. Set the
342 * values directly into *dn; they will be normalized later. (and
343 * normalizing always makes a new copy.) An ID from a TLS certificate
344 * is already normalized, so copy it and skip normalization.
346 if( flags & FLAG_GETDN_AUTHCID ) {
348 if( conn->c_is_tls && conn->c_sasl_bind_mech.bv_len == ext_bv.bv_len
349 && ( strcasecmp( ext_bv.bv_val, conn->c_sasl_bind_mech.bv_val ) == 0 ) ) {
350 /* X.509 DN is already normalized */
353 ber_str2bv( id, len, 1, dn );
358 /* convert to u:<username> form */
365 if( !strncasecmp( id, "u:", sizeof("u:")-1 )) {
369 } else if ( !strncasecmp( id, "dn:", sizeof("dn:")-1) ) {
376 /* No other possibilities from here */
380 return( LDAP_INAPPROPRIATE_AUTH );
383 /* Username strings */
384 if( is_dn == SET_U ) {
386 len = dn->bv_len + sizeof("uid=")-1 + sizeof(",cn=auth")-1;
388 /* username may have embedded realm name */
389 if( realm = strchr( dn->bv_val, '@') ) {
391 len += sizeof(",cn=")-2;
392 } else if( user_realm && *user_realm ) {
393 len += strlen( user_realm ) + sizeof(",cn=")-1;
396 if( conn->c_sasl_bind_mech.bv_len ) {
397 len += conn->c_sasl_bind_mech.bv_len + sizeof(",cn=")-1;
400 /* Build the new dn */
402 dn->bv_val = ch_malloc( len+1 );
403 p = slap_strcopy( dn->bv_val, "uid=" );
404 p = slap_strncopy( p, c1, dn->bv_len );
407 int rlen = dn->bv_len - ( realm - c1 );
408 p = slap_strcopy( p, ",cn=" );
409 p = slap_strncopy( p, realm, rlen );
411 } else if( user_realm && *user_realm ) {
412 p = slap_strcopy( p, ",cn=" );
413 p = slap_strcopy( p, user_realm );
416 if( conn->c_sasl_bind_mech.bv_len ) {
417 p = slap_strcopy( p, ",cn=" );
418 p = slap_strcopy( p, conn->c_sasl_bind_mech.bv_val );
420 p = slap_strcopy( p, ",cn=auth" );
421 dn->bv_len = p - dn->bv_val;
424 LDAP_LOG( TRANSPORT, ENTRY,
425 "slap_sasl_getdn: u:id converted to %s.\n", dn->bv_val, 0, 0 );
427 Debug( LDAP_DEBUG_TRACE, "getdn: u:id converted to %s\n", dn->bv_val,0,0 );
431 /* All strings are in DN form now. Normalize if needed. */
433 rc = dnNormalize2( NULL, dn, &dn2 );
435 /* User DNs were constructed above and must be freed now */
436 if ( is_dn == SET_U )
437 ch_free( dn->bv_val );
439 if ( rc != LDAP_SUCCESS ) {
447 /* Run thru regexp */
448 slap_sasl2dn( conn, dn, &dn2 );
450 ch_free( dn->bv_val );
453 LDAP_LOG( TRANSPORT, ENTRY,
454 "slap_sasl_getdn: dn:id converted to %s.\n", dn->bv_val, 0, 0 );
456 Debug( LDAP_DEBUG_TRACE, "getdn: dn:id converted to %s\n",
461 return( LDAP_SUCCESS );
464 #if SASL_VERSION_MAJOR >= 2
465 static const char *slap_propnames[] = { "*authcDN", "*authzDN", NULL };
470 sasl_server_params_t *sparams,
477 const struct propval *list;
479 AttributeDescription *ad;
482 list = sparams->utils->prop_get( sparams->propctx );
484 /* Find our DN first */
485 for( i = 0, last = 0; list[i].name; i++ ) {
486 if ( list[i].name[0] == '*' ) {
487 if ( (flags & SASL_AUXPROP_AUTHZID) &&
488 !strcmp( list[i].name, slap_propnames[1] ) ) {
489 if ( list[i].values && list[i].values[0] )
490 AC_MEMCPY( &dn, list[i].values[0], sizeof( dn ) );
491 if ( !last ) last = i;
494 if ( !strcmp( list[i].name, slap_propnames[0] ) ) {
495 if ( !last ) last = i;
496 if ( list[i].values && list[i].values[0] ) {
497 AC_MEMCPY( &dn, list[i].values[0], sizeof( dn ) );
498 if ( !(flags & SASL_AUXPROP_AUTHZID) )
505 /* Now fetch the rest */
506 for( i = 0; i < last; i++ ) {
507 const char *name = list[i].name;
509 if ( name[0] == '*' ) {
510 if ( flags & SASL_AUXPROP_AUTHZID ) continue;
512 } else if ( !(flags & SASL_AUXPROP_AUTHZID ) )
515 if ( list[i].values ) {
516 if ( !(flags & SASL_AUXPROP_OVERRIDE) ) continue;
517 sparams->utils->prop_erase( sparams->propctx, list[i].name );
520 rc = slap_str2ad( name, &ad, &text );
521 if ( rc != LDAP_SUCCESS ) {
523 LDAP_LOG( TRANSPORT, DETAIL1,
524 "slap_auxprop: str2ad(%s): %s\n", name, text, 0 );
526 Debug( LDAP_DEBUG_TRACE,
527 "slap_auxprop: str2ad(%s): %s\n", name, text, 0 );
529 rc = slap_str2undef_ad( name, &ad, &text );
530 if ( rc != LDAP_SUCCESS ) continue;
532 rc = backend_attribute( NULL,NULL,NULL,NULL, &dn, ad, &vals );
533 if ( rc != LDAP_SUCCESS ) continue;
534 for ( bv = vals; bv->bv_val; bv++ ) {
535 sparams->utils->prop_set( sparams->propctx, list[i].name,
536 bv->bv_val, bv->bv_len );
538 ber_bvarray_free( vals );
542 static sasl_auxprop_plug_t slap_auxprop_plugin = {
545 NULL, /* glob_context */
546 NULL, /* auxprop_free */
554 const sasl_utils_t *utils,
557 sasl_auxprop_plug_t **plug,
558 const char *plugname)
560 if ( !out_version | !plug ) return SASL_BADPARAM;
562 if ( max_version < SASL_AUXPROP_PLUG_VERSION ) return SASL_BADVERS;
564 *out_version = SASL_AUXPROP_PLUG_VERSION;
565 *plug = &slap_auxprop_plugin;
573 const char *username,
576 struct propctx *propctx)
578 Connection *conn = (Connection *)context;
579 struct berval dn, cred;
583 cred.bv_val = (char *)pass;
584 cred.bv_len = passlen;
586 /* SASL will fallback to its own mechanisms if we don't
587 * find an answer here.
590 rc = slap_sasl_getdn( conn, (char *)username, 0, NULL, &dn,
591 FLAG_GETDN_AUTHCID );
592 if ( rc != LDAP_SUCCESS ) {
593 sasl_seterror( sconn, 0, ldap_err2string( rc ) );
597 if ( dn.bv_len == 0 ) {
598 sasl_seterror( sconn, 0,
599 "No password is associated with the Root DSE" );
600 if ( dn.bv_val != NULL ) {
601 ch_free( dn.bv_val );
606 rc = backend_attribute( NULL, NULL, NULL, NULL, &dn,
607 slap_schema.si_ad_userPassword, &vals);
608 if ( rc != LDAP_SUCCESS ) {
609 ch_free( dn.bv_val );
610 sasl_seterror( sconn, 0, ldap_err2string( rc ) );
611 return SASL_NOVERIFY;
616 if ( vals != NULL ) {
617 for ( bv = vals; bv->bv_val != NULL; bv++ ) {
618 if ( !lutil_passwd( bv, &cred, NULL ) ) {
623 ber_bvarray_free( vals );
626 if ( rc != SASL_OK ) {
627 sasl_seterror( sconn, 0,
628 ldap_err2string( LDAP_INVALID_CREDENTIALS ) );
631 ch_free( dn.bv_val );
637 slap_sasl_cb_setpass(
640 const char *username,
643 struct propctx *propctx,
646 Connection *conn = (Connection *)context;
649 /* Convert a SASL authcid or authzid into a DN. Store the DN in an
650 * auxiliary property, so that we can refer to it in sasl_authorize
651 * without interfering with anything else. Also, the SASL username
652 * buffer is constrained to 256 characters, and our DNs could be
653 * much longer (totally arbitrary length)...
656 slap_sasl_canonicalize(
662 const char *user_realm,
667 Connection *conn = (Connection *)context;
668 struct propctx *props = sasl_auxprop_getctx( sconn );
669 struct propval auxvals[3];
672 const char *names[2];
677 LDAP_LOG( TRANSPORT, ENTRY,
678 "slap_sasl_canonicalize: conn %d %s=\"%s\"\n",
679 conn ? conn->c_connid : -1,
680 (flags & SASL_CU_AUTHID) ? "authcid" : "authzid", in ? in : "<empty>");
682 Debug( LDAP_DEBUG_ARGS, "SASL Canonicalize [conn=%ld]: "
684 conn ? conn->c_connid : -1,
685 (flags & SASL_CU_AUTHID) ? "authcid" : "authzid",
686 in ? in : "<empty>" );
689 /* If name is too big, just truncate. We don't care, we're
690 * using DNs, not the usernames.
692 if ( inlen > out_max )
695 /* See if we need to add request, can only do it once */
696 prop_getnames( props, slap_propnames, auxvals );
697 if ( !auxvals[0].name )
698 prop_request( props, slap_propnames );
700 if ( flags & SASL_CU_AUTHID )
705 /* Already been here? */
706 if ( auxvals[which].values )
709 if ( flags == SASL_CU_AUTHZID ) {
710 /* If we got unqualified authzid's, they probably came from SASL
711 * itself just passing the authcid to us. Look inside the oparams
712 * structure to see if that's true. (HACK: the out_len pointer is
713 * the address of a member of a sasl_out_params_t structure...)
715 sasl_out_params_t dummy;
716 int offset = (void *)&dummy.ulen - (void *)&dummy.authid;
717 char **authid = (void *)out_len - offset;
718 if ( *authid && !strcmp( in, *authid ) )
722 rc = slap_sasl_getdn( conn, (char *)in, inlen, (char *)user_realm, &dn,
723 (flags & SASL_CU_AUTHID) ? FLAG_GETDN_AUTHCID : FLAG_GETDN_AUTHZID );
724 if ( rc != LDAP_SUCCESS ) {
725 sasl_seterror( sconn, 0, ldap_err2string( rc ) );
729 names[0] = slap_propnames[which];
732 prop_set( props, names[0], (char *)&dn, sizeof( dn ) );
735 LDAP_LOG( TRANSPORT, ENTRY,
736 "slap_sasl_canonicalize: conn %d %s=\"%s\"\n",
737 conn ? conn->c_connid : -1, names[0]+1, dn.bv_val );
739 Debug( LDAP_DEBUG_ARGS, "SASL Canonicalize [conn=%ld]: "
741 conn ? conn->c_connid : -1,
742 names[0]+1, dn.bv_val );
744 done: AC_MEMCPY( out, in, inlen );
756 char *requested_user,
760 const char *def_realm,
762 struct propctx *props)
764 Connection *conn = (Connection *)context;
765 struct propval auxvals[3];
766 struct berval authcDN, authzDN;
770 LDAP_LOG( TRANSPORT, ENTRY,
771 "slap_sasl_authorize: conn %d authcid=\"%s\" authzid=\"%s\"\n",
772 conn ? conn->c_connid : -1, auth_identity, requested_user);
774 Debug( LDAP_DEBUG_ARGS, "SASL Authorize [conn=%ld]: "
775 "authcid=\"%s\" authzid=\"%s\"\n",
776 conn ? conn->c_connid : -1, auth_identity, requested_user );
778 if ( conn->c_sasl_dn.bv_val ) {
779 ch_free( conn->c_sasl_dn.bv_val );
780 conn->c_sasl_dn.bv_val = NULL;
781 conn->c_sasl_dn.bv_len = 0;
784 prop_getnames( props, slap_propnames, auxvals );
786 AC_MEMCPY( &authcDN, auxvals[0].values[0], sizeof(authcDN) );
788 /* Nothing to do if no authzID was given */
789 if ( !auxvals[1].name || !auxvals[1].values ) {
790 conn->c_sasl_dn = authcDN;
794 AC_MEMCPY( &authzDN, auxvals[1].values[0], sizeof(authzDN) );
796 rc = slap_sasl_authorized( &authcDN, &authzDN );
797 ch_free( authcDN.bv_val );
798 if ( rc != LDAP_SUCCESS ) {
800 LDAP_LOG( TRANSPORT, INFO,
801 "slap_sasl_authorize: conn %ld authorization disallowed (%d)\n",
802 (long)(conn ? conn->c_connid : -1), rc, 0 );
804 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
805 " authorization disallowed (%d)\n",
806 (long) (conn ? conn->c_connid : -1), rc, 0 );
809 sasl_seterror( sconn, 0, "not authorized" );
810 ch_free( authzDN.bv_val );
814 conn->c_sasl_dn = authzDN;
817 LDAP_LOG( TRANSPORT, ENTRY,
818 "slap_sasl_authorize: conn %d authorization allowed\n",
819 (long)(conn ? conn->c_connid : -1), 0, 0 );
821 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
822 " authorization allowed\n",
823 (long) (conn ? conn->c_connid : -1), 0, 0 );
836 struct berval authcDN, authzDN;
838 Connection *conn = context;
842 if ( conn->c_sasl_dn.bv_val ) {
843 ch_free( conn->c_sasl_dn.bv_val );
844 conn->c_sasl_dn.bv_val = NULL;
845 conn->c_sasl_dn.bv_len = 0;
849 LDAP_LOG( TRANSPORT, ENTRY,
850 "slap_sasl_authorize: conn %d authcid=\"%s\" authzid=\"%s\"\n",
851 conn ? conn->c_connid : -1, authcid ? authcid : "<empty>",
852 authzid ? authzid : "<empty>" );
854 Debug( LDAP_DEBUG_ARGS, "SASL Authorize [conn=%ld]: "
855 "authcid=\"%s\" authzid=\"%s\"\n",
856 (long) (conn ? conn->c_connid : -1),
857 authcid ? authcid : "<empty>",
858 authzid ? authzid : "<empty>" );
861 /* Figure out how much data we have for the dn */
862 rc = sasl_getprop( conn->c_sasl_context, SASL_REALM, (void **)&realm );
863 if( rc != SASL_OK && rc != SASL_NOTDONE ) {
865 LDAP_LOG( TRANSPORT, ERR,
866 "slap_sasl_authorize: getprop(REALM) failed.\n", 0, 0, 0 );
868 Debug(LDAP_DEBUG_TRACE,
869 "authorize: getprop(REALM) failed!\n", 0,0,0);
871 *errstr = "Could not extract realm";
875 /* Convert the identities to DN's. If no authzid was given, client will
876 be bound as the DN matching their username */
877 rc = slap_sasl_getdn( conn, (char *)authcid, 0, realm, &authcDN, FLAG_GETDN_AUTHCID );
878 if( rc != LDAP_SUCCESS ) {
879 *errstr = ldap_err2string( rc );
882 if( ( authzid == NULL ) || !strcmp( authcid,authzid ) ) {
884 LDAP_LOG( TRANSPORT, ENTRY,
885 "slap_sasl_authorize: conn %d Using authcDN=%s\n",
886 conn ? conn->c_connid : -1, authcDN.bv_val, 0 );
888 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
889 "Using authcDN=%s\n", (long) (conn ? conn->c_connid : -1), authcDN.bv_val,0 );
892 conn->c_sasl_dn = authcDN;
896 rc = slap_sasl_getdn( conn, (char *)authzid, 0, realm, &authzDN, FLAG_GETDN_AUTHZID );
897 if( rc != LDAP_SUCCESS ) {
898 ch_free( authcDN.bv_val );
899 *errstr = ldap_err2string( rc );
903 rc = slap_sasl_authorized( &authcDN, &authzDN );
904 ch_free( authcDN.bv_val );
907 LDAP_LOG( TRANSPORT, INFO,
908 "slap_sasl_authorize: conn %ld authorization disallowed (%d)\n",
909 (long)(conn ? conn->c_connid : -1), rc, 0 );
911 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
912 " authorization disallowed (%d)\n",
913 (long) (conn ? conn->c_connid : -1), rc, 0 );
916 *errstr = "not authorized";
917 ch_free( authzDN.bv_val );
922 LDAP_LOG( TRANSPORT, RESULTS,
923 "slap_sasl_authorize: conn %d authorization allowed\n",
924 (long)(conn ? conn->c_connid : -1 ), 0, 0 );
926 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
927 " authorization allowed\n",
928 (long) (conn ? conn->c_connid : -1), 0, 0 );
931 conn->c_sasl_dn = authzDN;
935 #endif /* SASL_VERSION_MAJOR >= 2 */
938 slap_sasl_err2ldap( int saslerr )
944 rc = LDAP_SASL_BIND_IN_PROGRESS;
953 rc = LDAP_AUTH_METHOD_NOT_SUPPORTED;
956 rc = LDAP_INVALID_CREDENTIALS;
959 rc = LDAP_INSUFFICIENT_ACCESS;
963 rc = LDAP_INAPPROPRIATE_AUTH;
975 int slap_sasl_init( void )
977 #ifdef HAVE_CYRUS_SASL
979 static sasl_callback_t server_callbacks[] = {
980 { SASL_CB_LOG, &slap_sasl_log, NULL },
981 { SASL_CB_LIST_END, NULL, NULL }
991 ldap_pvt_sasl_mutex_new,
992 ldap_pvt_sasl_mutex_lock,
993 ldap_pvt_sasl_mutex_unlock,
994 ldap_pvt_sasl_mutex_dispose );
996 #if SASL_VERSION_MAJOR >= 2
997 sasl_auxprop_add_plugin( "slapd", slap_auxprop_init );
999 /* should provide callbacks for logging */
1000 /* server name should be configurable */
1001 rc = sasl_server_init( server_callbacks, "slapd" );
1003 if( rc != SASL_OK ) {
1005 LDAP_LOG( TRANSPORT, INFO, "slap_sasl_init: init failed.\n", 0, 0, 0 );
1007 Debug( LDAP_DEBUG_ANY, "sasl_server_init failed\n",
1015 LDAP_LOG( TRANSPORT, INFO, "slap_sasl_init: initialized!\n", 0, 0, 0 );
1017 Debug( LDAP_DEBUG_TRACE, "slap_sasl_init: initialized!\n",
1022 /* default security properties */
1023 memset( &sasl_secprops, '\0', sizeof(sasl_secprops) );
1024 sasl_secprops.max_ssf = INT_MAX;
1025 sasl_secprops.maxbufsize = 65536;
1026 sasl_secprops.security_flags = SASL_SEC_NOPLAINTEXT|SASL_SEC_NOANONYMOUS;
1032 int slap_sasl_destroy( void )
1034 #ifdef HAVE_CYRUS_SASL
1037 free( global_host );
1043 int slap_sasl_open( Connection *conn )
1045 int cb, sc = LDAP_SUCCESS;
1046 #if SASL_VERSION_MAJOR >= 2
1047 char *ipremoteport = NULL, *iplocalport = NULL;
1050 #ifdef HAVE_CYRUS_SASL
1051 sasl_conn_t *ctx = NULL;
1052 sasl_callback_t *session_callbacks;
1054 assert( conn->c_sasl_context == NULL );
1055 assert( conn->c_sasl_extra == NULL );
1057 conn->c_sasl_layers = 0;
1060 #if SASL_VERSION_MAJOR >= 2
1061 ch_calloc( 5, sizeof(sasl_callback_t));
1063 ch_calloc( 3, sizeof(sasl_callback_t));
1065 conn->c_sasl_extra = session_callbacks;
1067 session_callbacks[cb=0].id = SASL_CB_LOG;
1068 session_callbacks[cb].proc = &slap_sasl_log;
1069 session_callbacks[cb++].context = conn;
1071 session_callbacks[cb].id = SASL_CB_PROXY_POLICY;
1072 session_callbacks[cb].proc = &slap_sasl_authorize;
1073 session_callbacks[cb++].context = conn;
1075 #if SASL_VERSION_MAJOR >= 2
1076 session_callbacks[cb].id = SASL_CB_CANON_USER;
1077 session_callbacks[cb].proc = &slap_sasl_canonicalize;
1078 session_callbacks[cb++].context = conn;
1080 /* XXXX: this should be conditional */
1081 session_callbacks[cb].id = SASL_CB_SERVER_USERDB_CHECKPASS;
1082 session_callbacks[cb].proc = &slap_sasl_checkpass;
1083 session_callbacks[cb++].context = conn;
1085 session_callbacks[cb].id = SASL_CB_SERVER_USERDB_SETPASS;
1086 session_callbacks[cb].proc = &slap_sasl_cb_setpass;
1087 session_callbacks[cb++].context = conn;
1090 session_callbacks[cb].id = SASL_CB_LIST_END;
1091 session_callbacks[cb].proc = NULL;
1092 session_callbacks[cb++].context = NULL;
1094 if( global_host == NULL ) {
1095 global_host = ldap_pvt_get_fqdn( NULL );
1098 /* create new SASL context */
1099 #if SASL_VERSION_MAJOR >= 2
1100 if ( conn->c_sock_name.bv_len != 0 &&
1101 strncmp( conn->c_sock_name.bv_val, "IP=", 3 ) == 0) {
1104 iplocalport = ch_strdup( conn->c_sock_name.bv_val + 3 );
1105 /* Convert IPv6 addresses to address;port syntax. */
1106 p = strrchr( iplocalport, ' ' );
1107 /* Convert IPv4 addresses to address;port syntax. */
1108 if ( p == NULL ) p = strchr( iplocalport, ':' );
1113 if ( conn->c_peer_name.bv_len != 0 &&
1114 strncmp( conn->c_peer_name.bv_val, "IP=", 3 ) == 0) {
1117 ipremoteport = ch_strdup( conn->c_peer_name.bv_val + 3 );
1118 /* Convert IPv6 addresses to address;port syntax. */
1119 p = strrchr( ipremoteport, ' ' );
1120 /* Convert IPv4 addresses to address;port syntax. */
1121 if ( p == NULL ) p = strchr( ipremoteport, ':' );
1126 sc = sasl_server_new( "ldap", global_host, global_realm,
1127 iplocalport, ipremoteport, session_callbacks, 0, &ctx );
1128 if ( iplocalport != NULL ) {
1129 ch_free( iplocalport );
1131 if ( ipremoteport != NULL ) {
1132 ch_free( ipremoteport );
1135 sc = sasl_server_new( "ldap", global_host, global_realm,
1136 session_callbacks, SASL_SECURITY_LAYER, &ctx );
1139 if( sc != SASL_OK ) {
1141 LDAP_LOG( TRANSPORT, ERR,
1142 "slap_sasl_open: sasl_server_new failed: %d\n", sc, 0, 0 );
1144 Debug( LDAP_DEBUG_ANY, "sasl_server_new failed: %d\n",
1151 conn->c_sasl_context = ctx;
1153 if( sc == SASL_OK ) {
1154 sc = sasl_setprop( ctx,
1155 SASL_SEC_PROPS, &sasl_secprops );
1157 if( sc != SASL_OK ) {
1159 LDAP_LOG( TRANSPORT, ERR,
1160 "slap_sasl_open: sasl_setprop failed: %d \n", sc, 0, 0 );
1162 Debug( LDAP_DEBUG_ANY, "sasl_setprop failed: %d\n",
1166 slap_sasl_close( conn );
1171 sc = slap_sasl_err2ldap( sc );
1176 int slap_sasl_external(
1179 const char *auth_id )
1181 #if SASL_VERSION_MAJOR >= 2
1183 sasl_conn_t *ctx = conn->c_sasl_context;
1185 if ( ctx == NULL ) {
1186 return LDAP_UNAVAILABLE;
1189 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL, &ssf );
1191 if ( sc != SASL_OK ) {
1195 sc = sasl_setprop( ctx, SASL_AUTH_EXTERNAL, auth_id );
1197 if ( sc != SASL_OK ) {
1201 #elif defined(HAVE_CYRUS_SASL)
1203 sasl_conn_t *ctx = conn->c_sasl_context;
1204 sasl_external_properties_t extprops;
1206 if ( ctx == NULL ) {
1207 return LDAP_UNAVAILABLE;
1210 memset( &extprops, '\0', sizeof(extprops) );
1212 extprops.auth_id = (char *) auth_id;
1214 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL,
1215 (void *) &extprops );
1217 if ( sc != SASL_OK ) {
1222 return LDAP_SUCCESS;
1225 int slap_sasl_reset( Connection *conn )
1227 #ifdef HAVE_CYRUS_SASL
1228 sasl_conn_t *ctx = conn->c_sasl_context;
1233 /* must return "anonymous" */
1234 return LDAP_SUCCESS;
1237 char ** slap_sasl_mechs( Connection *conn )
1239 char **mechs = NULL;
1241 #ifdef HAVE_CYRUS_SASL
1242 sasl_conn_t *ctx = conn->c_sasl_context;
1246 SASL_CONST char *mechstr;
1248 sc = sasl_listmech( ctx,
1249 NULL, NULL, ",", NULL,
1250 &mechstr, NULL, NULL );
1252 if( sc != SASL_OK ) {
1254 LDAP_LOG( TRANSPORT, ERR,
1255 "slap_sasl_mechs: sasl_listmech failed: %d\n", sc, 0, 0 );
1257 Debug( LDAP_DEBUG_ANY, "slap_sasl_listmech failed: %d\n",
1264 mechs = str2charray( mechstr, "," );
1266 #if SASL_VERSION_MAJOR < 2
1275 int slap_sasl_close( Connection *conn )
1277 #ifdef HAVE_CYRUS_SASL
1278 sasl_conn_t *ctx = conn->c_sasl_context;
1281 sasl_dispose( &ctx );
1284 conn->c_sasl_context = NULL;
1286 free( conn->c_sasl_extra );
1287 conn->c_sasl_extra = NULL;
1290 return LDAP_SUCCESS;
1298 struct berval *cred,
1304 #ifdef HAVE_CYRUS_SASL
1305 sasl_conn_t *ctx = conn->c_sasl_context;
1306 struct berval response;
1307 unsigned reslen = 0;
1308 const char *errstr = NULL;
1312 LDAP_LOG( TRANSPORT, ENTRY,
1313 "sasl_bind: dn=\"%s\" mech=%s datalen=%ld\n",
1314 dn->bv_len ? dn->bv_val : "",
1315 conn->c_sasl_bind_in_progress ? "<continuing>" :
1316 conn->c_sasl_bind_mech.bv_val,
1317 cred ? cred->bv_len : 0 );
1319 Debug(LDAP_DEBUG_ARGS,
1320 "==> sasl_bind: dn=\"%s\" mech=%s datalen=%ld\n",
1321 dn->bv_len ? dn->bv_val : "",
1322 conn->c_sasl_bind_in_progress ? "<continuing>":conn->c_sasl_bind_mech.bv_val,
1323 cred ? cred->bv_len : 0 );
1328 send_ldap_result( conn, op, LDAP_UNAVAILABLE,
1329 NULL, "SASL unavailable on this session", NULL, NULL );
1333 #if SASL_VERSION_MAJOR >= 2
1334 #define START( ctx, mech, cred, clen, resp, rlen, err ) \
1335 sasl_server_start( ctx, mech, cred, clen, resp, rlen )
1336 #define STEP( ctx, cred, clen, resp, rlen, err ) \
1337 sasl_server_step( ctx, cred, clen, resp, rlen )
1339 #define START( ctx, mech, cred, clen, resp, rlen, err ) \
1340 sasl_server_start( ctx, mech, cred, clen, resp, rlen, err )
1341 #define STEP( ctx, cred, clen, resp, rlen, err ) \
1342 sasl_server_step( ctx, cred, clen, resp, rlen, err )
1345 if ( !conn->c_sasl_bind_in_progress ) {
1347 conn->c_sasl_bind_mech.bv_val,
1348 cred->bv_len ? cred->bv_val : "",
1350 (SASL_CONST char **)&response.bv_val, &reslen, &errstr );
1354 cred->bv_val, cred->bv_len,
1355 (SASL_CONST char **)&response.bv_val, &reslen, &errstr );
1358 response.bv_len = reslen;
1360 if ( sc == SASL_OK ) {
1361 sasl_ssf_t *ssf = NULL;
1363 *edn = conn->c_sasl_dn;
1364 conn->c_sasl_dn.bv_val = NULL;
1365 conn->c_sasl_dn.bv_len = 0;
1369 (void) sasl_getprop( ctx, SASL_SSF, (void *)&ssf );
1370 *ssfp = ssf ? *ssf : 0;
1373 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1374 conn->c_sasl_layers++;
1375 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1378 send_ldap_sasl( conn, op, rc,
1379 NULL, NULL, NULL, NULL,
1380 response.bv_len ? &response : NULL );
1382 } else if ( sc == SASL_CONTINUE ) {
1383 send_ldap_sasl( conn, op, rc = LDAP_SASL_BIND_IN_PROGRESS,
1384 NULL, NULL, NULL, NULL, &response );
1387 #if SASL_VERSION_MAJOR >= 2
1388 errstr = sasl_errdetail( ctx );
1390 send_ldap_result( conn, op, rc = slap_sasl_err2ldap( sc ),
1391 NULL, errstr, NULL, NULL );
1394 #if SASL_VERSION_MAJOR < 2
1395 if( response.bv_len ) {
1396 ch_free( response.bv_val );
1401 LDAP_LOG( TRANSPORT, RESULTS, "slap_sasl_bind: rc=%d\n", rc, 0, 0 );
1403 Debug(LDAP_DEBUG_TRACE, "<== slap_sasl_bind: rc=%d\n", rc, 0, 0);
1408 send_ldap_result( conn, op, rc = LDAP_UNAVAILABLE,
1409 NULL, "SASL not supported", NULL, NULL );
1415 char* slap_sasl_secprops( const char *in )
1417 #ifdef HAVE_CYRUS_SASL
1418 int rc = ldap_pvt_sasl_secprops( in, &sasl_secprops );
1420 return rc == LDAP_SUCCESS ? NULL : "Invalid security properties";
1422 return "SASL not supported";
1426 #ifdef HAVE_CYRUS_SASL
1432 struct berval *reqdata,
1434 struct berval **rspdata,
1435 LDAPControl *** rspctrls,
1439 struct berval id = { 0, NULL }; /* needs to come from connection */
1440 struct berval new = { 0, NULL };
1441 struct berval old = { 0, NULL };
1443 assert( reqoid != NULL );
1444 assert( strcmp( LDAP_EXOP_MODIFY_PASSWD, reqoid ) == 0 );
1446 rc = sasl_getprop( conn->c_sasl_context, SASL_USERNAME,
1447 (SASL_CONST void **)&id.bv_val );
1449 if( rc != SASL_OK ) {
1450 *text = "unable to retrieve SASL username";
1456 LDAP_LOG(( "backend", LDAP_LEVEL_ENTRY,
1457 "slap_sasl_setpass: \"%s\"\n",
1458 id.bv_val ? id.bv_val : "" ));
1460 Debug( LDAP_DEBUG_ARGS, "==> ldbm_back_exop_passwd: \"%s\"\n",
1461 id.bv_val ? id.bv_val : "", 0, 0 );
1464 rc = slap_passwd_parse( reqdata,
1465 NULL, &old, &new, text );
1467 if( rc != LDAP_SUCCESS ) {
1471 if( new.bv_len == 0 ) {
1472 slap_passwd_generate(&new);
1474 if( new.bv_len == 0 ) {
1475 *text = "password generation failed.";
1480 *rspdata = slap_passwd_return( &new );
1483 #if SASL_VERSION_MAJOR < 2
1484 rc = sasl_setpass( conn->c_sasl_context,
1485 id.bv_val, new.bv_val, new.bv_len, 0, text );
1487 rc = sasl_setpass( conn->c_sasl_context, id.bv_val,
1488 new.bv_val, new.bv_len, old.bv_val, old.bv_len, 0 );
1489 if( rc != SASL_OK ) {
1490 *text = sasl_errdetail( conn->c_sasl_context );