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(( "sasl", LDAP_LEVEL_ENTRY,
280 "SASL [conn=%ld] %s: %s\n",
281 conn ? conn->c_connid : -1,
284 Debug( level, "SASL [conn=%ld] %s: %s\n",
285 conn ? conn->c_connid: -1,
294 /* Take any sort of identity string and return a DN with the "dn:" prefix. The
295 string returned in *dn is in its own allocated memory, and must be free'd
296 by the calling process.
297 -Mark Adamson, Carnegie Mellon
299 The "dn:" prefix is no longer used anywhere inside slapd. It is only used
300 on strings passed in directly from SASL.
301 -Howard Chu, Symas Corp.
307 static struct berval ext_bv = { sizeof("EXTERNAL")-1, "EXTERNAL" };
309 int slap_sasl_getdn( Connection *conn, char *id, int len,
310 char *user_realm, struct berval *dn, int flags )
313 int rc, is_dn = 0, do_norm = 1;
318 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
319 "slap_sasl_getdn: conn %d id=%s\n",
320 conn ? conn->c_connid : -1,
321 id ? (*id ? id : "<empty>") : "NULL" ));
323 Debug( LDAP_DEBUG_ARGS, "slap_sasl_getdn: id=%s\n",
324 id?(*id?id:"<empty>"):"NULL",0,0 );
331 if ( len == 0 ) len = strlen( id );
333 /* Blatantly anonymous ID */
334 if ( len == sizeof("anonymous") - 1 &&
335 !strcasecmp( id, "anonymous" ) ) {
336 return( LDAP_SUCCESS );
342 ctx = conn->c_sasl_context;
344 /* An authcID needs to be converted to authzID form. Set the
345 * values directly into *dn; they will be normalized later. (and
346 * normalizing always makes a new copy.) An ID from a TLS certificate
347 * is already normalized, so copy it and skip normalization.
349 if( flags & FLAG_GETDN_AUTHCID ) {
351 if( conn->c_is_tls && conn->c_sasl_bind_mech.bv_len == ext_bv.bv_len
352 && ( strcasecmp( ext_bv.bv_val, conn->c_sasl_bind_mech.bv_val ) == 0 ) ) {
353 /* X.509 DN is already normalized */
356 ber_str2bv( id, len, 1, dn );
361 /* convert to u:<username> form */
368 if( !strncasecmp( id, "u:", sizeof("u:")-1 )) {
372 } else if ( !strncasecmp( id, "dn:", sizeof("dn:")-1) ) {
379 /* No other possibilities from here */
383 return( LDAP_INAPPROPRIATE_AUTH );
386 /* Username strings */
387 if( is_dn == SET_U ) {
389 len = dn->bv_len + sizeof("uid=")-1 + sizeof(",cn=auth")-1;
391 /* username may have embedded realm name */
392 if( realm = strchr( dn->bv_val, '@') ) {
394 len += sizeof(",cn=")-2;
395 } else if( user_realm && *user_realm ) {
396 len += strlen( user_realm ) + sizeof(",cn=")-1;
399 if( conn->c_sasl_bind_mech.bv_len ) {
400 len += conn->c_sasl_bind_mech.bv_len + sizeof(",cn=")-1;
403 /* Build the new dn */
405 dn->bv_val = ch_malloc( len+1 );
406 p = slap_strcopy( dn->bv_val, "uid=" );
407 p = slap_strncopy( p, c1, dn->bv_len );
410 int rlen = dn->bv_len - ( realm - c1 );
411 p = slap_strcopy( p, ",cn=" );
412 p = slap_strncopy( p, realm, rlen );
414 } else if( user_realm && *user_realm ) {
415 p = slap_strcopy( p, ",cn=" );
416 p = slap_strcopy( p, user_realm );
419 if( conn->c_sasl_bind_mech.bv_len ) {
420 p = slap_strcopy( p, ",cn=" );
421 p = slap_strcopy( p, conn->c_sasl_bind_mech.bv_val );
423 p = slap_strcopy( p, ",cn=auth" );
424 dn->bv_len = p - dn->bv_val;
427 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
428 "slap_sasl_getdn: u:id converted to %s.\n", dn->bv_val ));
430 Debug( LDAP_DEBUG_TRACE, "getdn: u:id converted to %s\n", dn->bv_val,0,0 );
434 /* All strings are in DN form now. Normalize if needed. */
436 rc = dnNormalize2( NULL, dn, &dn2 );
438 /* User DNs were constructed above and must be freed now */
439 if ( is_dn == SET_U )
440 ch_free( dn->bv_val );
442 if ( rc != LDAP_SUCCESS ) {
450 /* Run thru regexp */
451 slap_sasl2dn( conn, dn, &dn2 );
453 ch_free( dn->bv_val );
456 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
457 "slap_sasl_getdn: dn:id converted to %s.\n", dn->bv_val ));
459 Debug( LDAP_DEBUG_TRACE, "getdn: dn:id converted to %s\n",
464 return( LDAP_SUCCESS );
467 #if SASL_VERSION_MAJOR >= 2
468 static const char *slap_propnames[] = { "*authcDN", "*authzDN", NULL };
473 sasl_server_params_t *sparams,
480 const struct propval *list;
482 AttributeDescription *ad;
485 list = sparams->utils->prop_get( sparams->propctx );
487 /* Find our DN first */
488 for( i = 0, last = 0; list[i].name; i++ ) {
489 if ( list[i].name[0] == '*' ) {
490 if ( (flags & SASL_AUXPROP_AUTHZID) &&
491 !strcmp( list[i].name, slap_propnames[1] ) ) {
492 if ( list[i].values && list[i].values[0] )
493 AC_MEMCPY( &dn, list[i].values[0], sizeof( dn ) );
494 if ( !last ) last = i;
497 if ( !strcmp( list[i].name, slap_propnames[0] ) ) {
498 if ( !last ) last = i;
499 if ( list[i].values && list[i].values[0] ) {
500 AC_MEMCPY( &dn, list[i].values[0], sizeof( dn ) );
501 if ( !(flags & SASL_AUXPROP_AUTHZID) )
508 /* Now fetch the rest */
509 for( i = 0; i < last; i++ ) {
510 const char *name = list[i].name;
512 if ( name[0] == '*' ) {
513 if ( flags & SASL_AUXPROP_AUTHZID ) continue;
515 } else if ( !(flags & SASL_AUXPROP_AUTHZID ) )
518 if ( list[i].values ) {
519 if ( !(flags & SASL_AUXPROP_OVERRIDE) ) continue;
520 sparams->utils->prop_erase( sparams->propctx, list[i].name );
523 rc = slap_str2ad( name, &ad, &text );
524 if ( rc != LDAP_SUCCESS ) {
526 LDAP_LOG(( "sasl", LDAP_LEVEL_DETAIL1,
527 "slap_auxprop: str2ad(%s): %s\n", name, text ));
529 Debug( LDAP_DEBUG_TRACE,
530 "slap_auxprop: str2ad(%s): %s\n", name, text, 0 );
532 rc = slap_str2undef_ad( name, &ad, &text );
533 if ( rc != LDAP_SUCCESS ) continue;
535 rc = backend_attribute( NULL,NULL,NULL,NULL, &dn, ad, &vals );
536 if ( rc != LDAP_SUCCESS ) continue;
537 for ( bv = vals; bv->bv_val; bv++ ) {
538 sparams->utils->prop_set( sparams->propctx, list[i].name,
539 bv->bv_val, bv->bv_len );
541 ber_bvarray_free( vals );
545 static sasl_auxprop_plug_t slap_auxprop_plugin = {
548 NULL, /* glob_context */
549 NULL, /* auxprop_free */
557 const sasl_utils_t *utils,
560 sasl_auxprop_plug_t **plug,
561 const char *plugname)
563 if ( !out_version | !plug ) return SASL_BADPARAM;
565 if ( max_version < SASL_AUXPROP_PLUG_VERSION ) return SASL_BADVERS;
567 *out_version = SASL_AUXPROP_PLUG_VERSION;
568 *plug = &slap_auxprop_plugin;
576 const char *username,
579 struct propctx *propctx)
581 Connection *conn = (Connection *)context;
582 struct berval dn, cred;
586 cred.bv_val = (char *)pass;
587 cred.bv_len = passlen;
589 /* SASL will fallback to its own mechanisms if we don't
590 * find an answer here.
593 rc = slap_sasl_getdn( conn, (char *)username, 0, NULL, &dn,
594 FLAG_GETDN_AUTHCID );
595 if ( rc != LDAP_SUCCESS ) {
596 sasl_seterror( sconn, 0, ldap_err2string( rc ) );
600 if ( dn.bv_len == 0 ) {
601 sasl_seterror( sconn, 0,
602 "No password is associated with the Root DSE" );
603 if ( dn.bv_val != NULL ) {
604 ch_free( dn.bv_val );
609 rc = backend_attribute( NULL, NULL, NULL, NULL, &dn,
610 slap_schema.si_ad_userPassword, &vals);
611 if ( rc != LDAP_SUCCESS ) {
612 ch_free( dn.bv_val );
613 sasl_seterror( sconn, 0, ldap_err2string( rc ) );
614 return SASL_NOVERIFY;
619 if ( vals != NULL ) {
620 for ( bv = vals; bv->bv_val != NULL; bv++ ) {
621 if ( !lutil_passwd( bv, &cred, NULL ) ) {
626 ber_bvarray_free( vals );
629 if ( rc != SASL_OK ) {
630 sasl_seterror( sconn, 0,
631 ldap_err2string( LDAP_INVALID_CREDENTIALS ) );
634 ch_free( dn.bv_val );
639 /* Convert a SASL authcid or authzid into a DN. Store the DN in an
640 * auxiliary property, so that we can refer to it in sasl_authorize
641 * without interfering with anything else. Also, the SASL username
642 * buffer is constrained to 256 characters, and our DNs could be
643 * much longer (totally arbitrary length)...
646 slap_sasl_canonicalize(
652 const char *user_realm,
657 Connection *conn = (Connection *)context;
658 struct propctx *props = sasl_auxprop_getctx( sconn );
659 struct propval auxvals[3];
662 const char *names[2];
667 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
668 "slap_sasl_canonicalize: conn %d %s=\"%s\"\n",
669 conn ? conn->c_connid : -1,
670 (flags & SASL_CU_AUTHID) ? "authcid" : "authzid",
671 in ? in : "<empty>" ));
673 Debug( LDAP_DEBUG_ARGS, "SASL Canonicalize [conn=%ld]: "
675 conn ? conn->c_connid : -1,
676 (flags & SASL_CU_AUTHID) ? "authcid" : "authzid",
677 in ? in : "<empty>" );
680 /* If name is too big, just truncate. We don't care, we're
681 * using DNs, not the usernames.
683 if ( inlen > out_max )
686 /* See if we need to add request, can only do it once */
687 prop_getnames( props, slap_propnames, auxvals );
688 if ( !auxvals[0].name )
689 prop_request( props, slap_propnames );
691 if ( flags & SASL_CU_AUTHID )
696 /* Already been here? */
697 if ( auxvals[which].values )
700 if ( flags == SASL_CU_AUTHZID ) {
701 /* If we got unqualified authzid's, they probably came from SASL
702 * itself just passing the authcid to us. Look inside the oparams
703 * structure to see if that's true. (HACK: the out_len pointer is
704 * the address of a member of a sasl_out_params_t structure...)
706 sasl_out_params_t dummy;
707 int offset = (void *)&dummy.ulen - (void *)&dummy.authid;
708 char **authid = (void *)out_len - offset;
709 if ( *authid && !strcmp( in, *authid ) )
713 rc = slap_sasl_getdn( conn, (char *)in, inlen, (char *)user_realm, &dn,
714 (flags & SASL_CU_AUTHID) ? FLAG_GETDN_AUTHCID : FLAG_GETDN_AUTHZID );
715 if ( rc != LDAP_SUCCESS ) {
716 sasl_seterror( sconn, 0, ldap_err2string( rc ) );
720 names[0] = slap_propnames[which];
723 prop_set( props, names[0], (char *)&dn, sizeof( dn ) );
726 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
727 "slap_sasl_canonicalize: conn %d %s=\"%s\"\n",
728 conn ? conn->c_connid : -1,
729 names[0]+1, dn.bv_val ));
731 Debug( LDAP_DEBUG_ARGS, "SASL Canonicalize [conn=%ld]: "
733 conn ? conn->c_connid : -1,
734 names[0]+1, dn.bv_val );
736 done: AC_MEMCPY( out, in, inlen );
748 char *requested_user,
752 const char *def_realm,
754 struct propctx *props)
756 Connection *conn = (Connection *)context;
757 struct propval auxvals[3];
758 struct berval authcDN, authzDN;
762 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
763 "slap_sasl_authorize: conn %d authcid=\"%s\" authzid=\"%s\"\n",
764 conn ? conn->c_connid : -1, auth_identity, requested_user));
766 Debug( LDAP_DEBUG_ARGS, "SASL Authorize [conn=%ld]: "
767 "authcid=\"%s\" authzid=\"%s\"\n",
768 conn ? conn->c_connid : -1, auth_identity, requested_user );
770 if ( conn->c_sasl_dn.bv_val ) {
771 ch_free( conn->c_sasl_dn.bv_val );
772 conn->c_sasl_dn.bv_val = NULL;
773 conn->c_sasl_dn.bv_len = 0;
776 prop_getnames( props, slap_propnames, auxvals );
778 AC_MEMCPY( &authcDN, auxvals[0].values[0], sizeof(authcDN) );
780 /* Nothing to do if no authzID was given */
781 if ( !auxvals[1].name || !auxvals[1].values ) {
782 conn->c_sasl_dn = authcDN;
786 AC_MEMCPY( &authzDN, auxvals[1].values[0], sizeof(authzDN) );
788 rc = slap_sasl_authorized( &authcDN, &authzDN );
789 ch_free( authcDN.bv_val );
790 if ( rc != LDAP_SUCCESS ) {
792 LDAP_LOG(( "sasl", LDAP_LEVEL_INFO,
793 "slap_sasl_authorize: conn %ld authorization disallowed (%d)\n",
794 (long)(conn ? conn->c_connid : -1), rc ));
796 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
797 " authorization disallowed (%d)\n",
798 (long) (conn ? conn->c_connid : -1), rc, 0 );
801 sasl_seterror( sconn, 0, "not authorized" );
802 ch_free( authzDN.bv_val );
806 conn->c_sasl_dn = authzDN;
809 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
810 "slap_sasl_authorize: conn %d authorization allowed\n",
811 (long)(conn ? conn->c_connid : -1 ) ));
813 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
814 " authorization allowed\n",
815 (long) (conn ? conn->c_connid : -1), 0, 0 );
828 struct berval authcDN, authzDN;
830 Connection *conn = context;
834 if ( conn->c_sasl_dn.bv_val ) {
835 ch_free( conn->c_sasl_dn.bv_val );
836 conn->c_sasl_dn.bv_val = NULL;
837 conn->c_sasl_dn.bv_len = 0;
841 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
842 "slap_sasl_authorize: conn %d authcid=\"%s\" authzid=\"%s\"\n",
843 conn ? conn->c_connid : -1,
844 authcid ? authcid : "<empty>",
845 authzid ? authzid : "<empty>" ));
847 Debug( LDAP_DEBUG_ARGS, "SASL Authorize [conn=%ld]: "
848 "authcid=\"%s\" authzid=\"%s\"\n",
849 (long) (conn ? conn->c_connid : -1),
850 authcid ? authcid : "<empty>",
851 authzid ? authzid : "<empty>" );
854 /* Figure out how much data we have for the dn */
855 rc = sasl_getprop( conn->c_sasl_context, SASL_REALM, (void **)&realm );
856 if( rc != SASL_OK && rc != SASL_NOTDONE ) {
858 LDAP_LOG(( "sasl", LDAP_LEVEL_ERR,
859 "slap_sasl_authorize: getprop(REALM) failed.\n" ));
861 Debug(LDAP_DEBUG_TRACE,
862 "authorize: getprop(REALM) failed!\n", 0,0,0);
864 *errstr = "Could not extract realm";
868 /* Convert the identities to DN's. If no authzid was given, client will
869 be bound as the DN matching their username */
870 rc = slap_sasl_getdn( conn, (char *)authcid, 0, realm, &authcDN, FLAG_GETDN_AUTHCID );
871 if( rc != LDAP_SUCCESS ) {
872 *errstr = ldap_err2string( rc );
875 if( ( authzid == NULL ) || !strcmp( authcid,authzid ) ) {
877 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
878 "slap_sasl_authorize: conn %d Using authcDN=%s\n",
879 conn ? conn->c_connid : -1, authcDN.bv_val ));
881 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
882 "Using authcDN=%s\n", (long) (conn ? conn->c_connid : -1), authcDN.bv_val,0 );
885 conn->c_sasl_dn = authcDN;
889 rc = slap_sasl_getdn( conn, (char *)authzid, 0, realm, &authzDN, FLAG_GETDN_AUTHZID );
890 if( rc != LDAP_SUCCESS ) {
891 ch_free( authcDN.bv_val );
892 *errstr = ldap_err2string( rc );
896 rc = slap_sasl_authorized( &authcDN, &authzDN );
897 ch_free( authcDN.bv_val );
900 LDAP_LOG(( "sasl", LDAP_LEVEL_INFO,
901 "slap_sasl_authorize: conn %ld authorization disallowed (%d)\n",
902 (long)(conn ? conn->c_connid : -1), rc ));
904 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
905 " authorization disallowed (%d)\n",
906 (long) (conn ? conn->c_connid : -1), rc, 0 );
909 *errstr = "not authorized";
910 ch_free( authzDN.bv_val );
915 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
916 "slap_sasl_authorize: conn %d authorization allowed\n",
917 (long)(conn ? conn->c_connid : -1 ) ));
919 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
920 " authorization allowed\n",
921 (long) (conn ? conn->c_connid : -1), 0, 0 );
924 conn->c_sasl_dn = authzDN;
928 #endif /* SASL_VERSION_MAJOR >= 2 */
931 slap_sasl_err2ldap( int saslerr )
937 rc = LDAP_SASL_BIND_IN_PROGRESS;
946 rc = LDAP_AUTH_METHOD_NOT_SUPPORTED;
949 rc = LDAP_INVALID_CREDENTIALS;
952 rc = LDAP_INSUFFICIENT_ACCESS;
956 rc = LDAP_INAPPROPRIATE_AUTH;
968 int slap_sasl_init( void )
970 #ifdef HAVE_CYRUS_SASL
972 static sasl_callback_t server_callbacks[] = {
973 { SASL_CB_LOG, &slap_sasl_log, NULL },
974 { SASL_CB_LIST_END, NULL, NULL }
984 ldap_pvt_sasl_mutex_new,
985 ldap_pvt_sasl_mutex_lock,
986 ldap_pvt_sasl_mutex_unlock,
987 ldap_pvt_sasl_mutex_dispose );
989 #if SASL_VERSION_MAJOR >= 2
990 sasl_auxprop_add_plugin( "slapd", slap_auxprop_init );
992 /* should provide callbacks for logging */
993 /* server name should be configurable */
994 rc = sasl_server_init( server_callbacks, "slapd" );
996 if( rc != SASL_OK ) {
998 LDAP_LOG(( "sasl", LDAP_LEVEL_INFO,
999 "slap_sasl_init: init failed.\n" ));
1001 Debug( LDAP_DEBUG_ANY, "sasl_server_init failed\n",
1009 LDAP_LOG(( "sasl", LDAP_LEVEL_INFO,
1010 "slap_sasl_init: initialized!\n"));
1012 Debug( LDAP_DEBUG_TRACE, "slap_sasl_init: initialized!\n",
1017 /* default security properties */
1018 memset( &sasl_secprops, '\0', sizeof(sasl_secprops) );
1019 sasl_secprops.max_ssf = INT_MAX;
1020 sasl_secprops.maxbufsize = 65536;
1021 sasl_secprops.security_flags = SASL_SEC_NOPLAINTEXT|SASL_SEC_NOANONYMOUS;
1027 int slap_sasl_destroy( void )
1029 #ifdef HAVE_CYRUS_SASL
1032 free( global_host );
1038 int slap_sasl_open( Connection *conn )
1040 int cb, sc = LDAP_SUCCESS;
1041 #if SASL_VERSION_MAJOR >= 2
1042 char *ipremoteport = NULL, *iplocalport = NULL;
1045 #ifdef HAVE_CYRUS_SASL
1046 sasl_conn_t *ctx = NULL;
1047 sasl_callback_t *session_callbacks;
1049 assert( conn->c_sasl_context == NULL );
1050 assert( conn->c_sasl_extra == NULL );
1052 conn->c_sasl_layers = 0;
1055 #if SASL_VERSION_MAJOR >= 2
1056 ch_calloc( 5, sizeof(sasl_callback_t));
1058 ch_calloc( 3, sizeof(sasl_callback_t));
1060 conn->c_sasl_extra = session_callbacks;
1062 session_callbacks[cb=0].id = SASL_CB_LOG;
1063 session_callbacks[cb].proc = &slap_sasl_log;
1064 session_callbacks[cb++].context = conn;
1066 session_callbacks[cb].id = SASL_CB_PROXY_POLICY;
1067 session_callbacks[cb].proc = &slap_sasl_authorize;
1068 session_callbacks[cb++].context = conn;
1070 #if SASL_VERSION_MAJOR >= 2
1071 session_callbacks[cb].id = SASL_CB_CANON_USER;
1072 session_callbacks[cb].proc = &slap_sasl_canonicalize;
1073 session_callbacks[cb++].context = conn;
1075 /* XXXX: this should be conditional */
1076 session_callbacks[cb].id = SASL_CB_SERVER_USERDB_CHECKPASS;
1077 session_callbacks[cb].proc = &slap_sasl_checkpass;
1078 session_callbacks[cb++].context = conn;
1081 session_callbacks[cb].id = SASL_CB_LIST_END;
1082 session_callbacks[cb].proc = NULL;
1083 session_callbacks[cb++].context = NULL;
1085 if( global_host == NULL ) {
1086 global_host = ldap_pvt_get_fqdn( NULL );
1089 /* create new SASL context */
1090 #if SASL_VERSION_MAJOR >= 2
1091 if ( conn->c_sock_name.bv_len != 0 &&
1092 strncmp( conn->c_sock_name.bv_val, "IP=", 3 ) == 0) {
1095 iplocalport = ch_strdup( conn->c_sock_name.bv_val + 3 );
1096 /* Convert IPv6 addresses to address;port syntax. */
1097 p = strrchr( iplocalport, ' ' );
1098 /* Convert IPv4 addresses to address;port syntax. */
1099 if ( p == NULL ) p = strchr( iplocalport, ':' );
1104 if ( conn->c_peer_name.bv_len != 0 &&
1105 strncmp( conn->c_peer_name.bv_val, "IP=", 3 ) == 0) {
1108 ipremoteport = ch_strdup( conn->c_peer_name.bv_val + 3 );
1109 /* Convert IPv6 addresses to address;port syntax. */
1110 p = strrchr( ipremoteport, ' ' );
1111 /* Convert IPv4 addresses to address;port syntax. */
1112 if ( p == NULL ) p = strchr( ipremoteport, ':' );
1117 sc = sasl_server_new( "ldap", global_host, global_realm,
1118 iplocalport, ipremoteport, session_callbacks, 0, &ctx );
1119 if ( iplocalport != NULL ) {
1120 ch_free( iplocalport );
1122 if ( ipremoteport != NULL ) {
1123 ch_free( ipremoteport );
1126 sc = sasl_server_new( "ldap", global_host, global_realm,
1127 session_callbacks, SASL_SECURITY_LAYER, &ctx );
1130 if( sc != SASL_OK ) {
1132 LDAP_LOG(( "sasl", LDAP_LEVEL_ERR,
1133 "slap_sasl_open: sasl_server_new failed: %d\n", sc ));
1135 Debug( LDAP_DEBUG_ANY, "sasl_server_new failed: %d\n",
1142 conn->c_sasl_context = ctx;
1144 if( sc == SASL_OK ) {
1145 sc = sasl_setprop( ctx,
1146 SASL_SEC_PROPS, &sasl_secprops );
1148 if( sc != SASL_OK ) {
1150 LDAP_LOG(( "sasl", LDAP_LEVEL_ERR,
1151 "slap_sasl_open: sasl_setprop failed: %d \n", sc ));
1153 Debug( LDAP_DEBUG_ANY, "sasl_setprop failed: %d\n",
1157 slap_sasl_close( conn );
1162 sc = slap_sasl_err2ldap( sc );
1167 int slap_sasl_external(
1170 const char *auth_id )
1172 #if SASL_VERSION_MAJOR >= 2
1174 sasl_conn_t *ctx = conn->c_sasl_context;
1176 if ( ctx == NULL ) {
1177 return LDAP_UNAVAILABLE;
1180 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL, &ssf );
1182 if ( sc != SASL_OK ) {
1186 sc = sasl_setprop( ctx, SASL_AUTH_EXTERNAL, auth_id );
1188 if ( sc != SASL_OK ) {
1192 #elif defined(HAVE_CYRUS_SASL)
1194 sasl_conn_t *ctx = conn->c_sasl_context;
1195 sasl_external_properties_t extprops;
1197 if ( ctx == NULL ) {
1198 return LDAP_UNAVAILABLE;
1201 memset( &extprops, '\0', sizeof(extprops) );
1203 extprops.auth_id = (char *) auth_id;
1205 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL,
1206 (void *) &extprops );
1208 if ( sc != SASL_OK ) {
1213 return LDAP_SUCCESS;
1216 int slap_sasl_reset( Connection *conn )
1218 #ifdef HAVE_CYRUS_SASL
1219 sasl_conn_t *ctx = conn->c_sasl_context;
1224 /* must return "anonymous" */
1225 return LDAP_SUCCESS;
1228 char ** slap_sasl_mechs( Connection *conn )
1230 char **mechs = NULL;
1232 #ifdef HAVE_CYRUS_SASL
1233 sasl_conn_t *ctx = conn->c_sasl_context;
1237 SASL_CONST char *mechstr;
1239 sc = sasl_listmech( ctx,
1240 NULL, NULL, ",", NULL,
1241 &mechstr, NULL, NULL );
1243 if( sc != SASL_OK ) {
1245 LDAP_LOG(( "sasl", LDAP_LEVEL_ERR,
1246 "slap_sasl_mechs: sasl_listmech failed: %d\n", sc ));
1248 Debug( LDAP_DEBUG_ANY, "slap_sasl_listmech failed: %d\n",
1255 mechs = str2charray( mechstr, "," );
1257 #if SASL_VERSION_MAJOR < 2
1266 int slap_sasl_close( Connection *conn )
1268 #ifdef HAVE_CYRUS_SASL
1269 sasl_conn_t *ctx = conn->c_sasl_context;
1272 sasl_dispose( &ctx );
1275 conn->c_sasl_context = NULL;
1277 free( conn->c_sasl_extra );
1278 conn->c_sasl_extra = NULL;
1281 return LDAP_SUCCESS;
1289 struct berval *cred,
1295 #ifdef HAVE_CYRUS_SASL
1296 sasl_conn_t *ctx = conn->c_sasl_context;
1297 struct berval response;
1298 unsigned reslen = 0;
1299 const char *errstr = NULL;
1303 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
1304 "sasl_bind: conn %ld dn=\"%s\" mech=%s datalen=%ld\n",
1306 dn->bv_len ? dn->bv_val : "",
1307 conn->c_sasl_bind_in_progress ? "<continuing>" : conn->c_sasl_bind_mech.bv_val,
1308 cred ? cred->bv_len : 0 ));
1310 Debug(LDAP_DEBUG_ARGS,
1311 "==> sasl_bind: dn=\"%s\" mech=%s datalen=%ld\n",
1312 dn->bv_len ? dn->bv_val : "",
1313 conn->c_sasl_bind_in_progress ? "<continuing>":conn->c_sasl_bind_mech.bv_val,
1314 cred ? cred->bv_len : 0 );
1319 send_ldap_result( conn, op, LDAP_UNAVAILABLE,
1320 NULL, "SASL unavailable on this session", NULL, NULL );
1324 #if SASL_VERSION_MAJOR >= 2
1325 #define START( ctx, mech, cred, clen, resp, rlen, err ) \
1326 sasl_server_start( ctx, mech, cred, clen, resp, rlen )
1327 #define STEP( ctx, cred, clen, resp, rlen, err ) \
1328 sasl_server_step( ctx, cred, clen, resp, rlen )
1330 #define START( ctx, mech, cred, clen, resp, rlen, err ) \
1331 sasl_server_start( ctx, mech, cred, clen, resp, rlen, err )
1332 #define STEP( ctx, cred, clen, resp, rlen, err ) \
1333 sasl_server_step( ctx, cred, clen, resp, rlen, err )
1336 if ( !conn->c_sasl_bind_in_progress ) {
1338 conn->c_sasl_bind_mech.bv_val,
1339 cred->bv_len ? cred->bv_val : "",
1341 (SASL_CONST char **)&response.bv_val, &reslen, &errstr );
1345 cred->bv_val, cred->bv_len,
1346 (SASL_CONST char **)&response.bv_val, &reslen, &errstr );
1349 response.bv_len = reslen;
1351 if ( sc == SASL_OK ) {
1352 sasl_ssf_t *ssf = NULL;
1354 *edn = conn->c_sasl_dn;
1355 conn->c_sasl_dn.bv_val = NULL;
1356 conn->c_sasl_dn.bv_len = 0;
1360 (void) sasl_getprop( ctx, SASL_SSF, (void *)&ssf );
1361 *ssfp = ssf ? *ssf : 0;
1364 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1365 conn->c_sasl_layers++;
1366 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1369 send_ldap_sasl( conn, op, rc,
1370 NULL, NULL, NULL, NULL,
1371 response.bv_len ? &response : NULL );
1373 } else if ( sc == SASL_CONTINUE ) {
1374 send_ldap_sasl( conn, op, rc = LDAP_SASL_BIND_IN_PROGRESS,
1375 NULL, NULL, NULL, NULL, &response );
1378 #if SASL_VERSION_MAJOR >= 2
1379 errstr = sasl_errdetail( ctx );
1381 send_ldap_result( conn, op, rc = slap_sasl_err2ldap( sc ),
1382 NULL, errstr, NULL, NULL );
1385 #if SASL_VERSION_MAJOR < 2
1386 if( response.bv_len ) {
1387 ch_free( response.bv_val );
1392 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
1393 "slap_sasl_bind: rc=%d\n", rc ));
1395 Debug(LDAP_DEBUG_TRACE, "<== slap_sasl_bind: rc=%d\n", rc, 0, 0);
1400 send_ldap_result( conn, op, rc = LDAP_UNAVAILABLE,
1401 NULL, "SASL not supported", NULL, NULL );
1407 char* slap_sasl_secprops( const char *in )
1409 #ifdef HAVE_CYRUS_SASL
1410 int rc = ldap_pvt_sasl_secprops( in, &sasl_secprops );
1412 return rc == LDAP_SUCCESS ? NULL : "Invalid security properties";
1414 return "SASL not supported";
1418 #ifdef HAVE_CYRUS_SASL
1424 struct berval *reqdata,
1426 struct berval **rspdata,
1427 LDAPControl *** rspctrls,
1431 struct berval id = { 0, NULL }; /* needs to come from connection */
1432 struct berval new = { 0, NULL };
1433 struct berval old = { 0, NULL };
1435 assert( reqoid != NULL );
1436 assert( strcmp( LDAP_EXOP_MODIFY_PASSWD, reqoid ) == 0 );
1438 rc = sasl_getprop( conn->c_sasl_context, SASL_USERNAME,
1439 (SASL_CONST void **)&id.bv_val );
1441 if( rc != SASL_OK ) {
1442 *text = "unable to retrieve SASL username";
1448 LDAP_LOG(( "backend", LDAP_LEVEL_ENTRY,
1449 "slap_sasl_setpass: \"%s\"\n",
1450 id.bv_val ? id.bv_val : "" ));
1452 Debug( LDAP_DEBUG_ARGS, "==> ldbm_back_exop_passwd: \"%s\"\n",
1453 id.bv_val ? id.bv_val : "", 0, 0 );
1456 rc = slap_passwd_parse( reqdata,
1457 NULL, &old, &new, text );
1459 if( rc != LDAP_SUCCESS ) {
1463 if( new.bv_len == 0 ) {
1464 slap_passwd_generate(&new);
1466 if( new.bv_len == 0 ) {
1467 *text = "password generation failed.";
1472 *rspdata = slap_passwd_return( &new );
1475 #if SASL_VERSION_MAJOR < 2
1476 rc = sasl_setpass( conn->c_sasl_context,
1477 id.bv_val, new.bv_val, new.bv_len, 0, text );
1479 rc = sasl_setpass( conn->c_sasl_context, id.bv_val,
1480 old.bv_val, old.bv_len, new.bv_val, new.bv_len, 0 );
1481 if( rc != SASL_OK ) {
1482 *text = sasl_errdetail( conn->c_sasl_context );