1999-09-09 03:06:24 +08:00
|
|
|
/* $OpenLDAP$ */
|
1999-08-07 07:07:46 +08:00
|
|
|
/*
|
2000-05-13 10:47:56 +08:00
|
|
|
* Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
|
1999-08-07 07:07:46 +08:00
|
|
|
* COPYING RESTRICTIONS APPLY, see COPYRIGHT file
|
|
|
|
*/
|
|
|
|
|
1999-08-03 10:37:42 +08:00
|
|
|
#include "portable.h"
|
|
|
|
|
1999-08-04 02:14:24 +08:00
|
|
|
#include <ac/stdlib.h>
|
1999-08-03 10:37:42 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include "slap.h"
|
|
|
|
#include "proto-slap.h"
|
|
|
|
|
|
|
|
#include <lber.h>
|
|
|
|
#include <ldap_log.h>
|
|
|
|
|
1999-08-04 07:23:05 +08:00
|
|
|
char **supportedSASLMechanisms = NULL;
|
2000-04-26 00:03:17 +08:00
|
|
|
char *sasl_host = NULL;
|
1999-08-03 10:37:42 +08:00
|
|
|
|
1999-08-04 07:23:05 +08:00
|
|
|
#ifdef HAVE_CYRUS_SASL
|
2000-05-10 12:29:51 +08:00
|
|
|
|
|
|
|
#ifdef SLAPD_SPASSWD
|
|
|
|
#include <lutil.h>
|
|
|
|
#endif
|
|
|
|
|
2000-05-04 02:59:58 +08:00
|
|
|
static void *slap_sasl_mutex_new(void)
|
2000-01-02 09:21:25 +08:00
|
|
|
{
|
|
|
|
ldap_pvt_thread_mutex_t *mutex;
|
|
|
|
|
2000-05-04 02:59:58 +08:00
|
|
|
mutex = (ldap_pvt_thread_mutex_t *) ch_malloc( sizeof(ldap_pvt_thread_mutex_t) );
|
2000-01-02 09:21:25 +08:00
|
|
|
if ( ldap_pvt_thread_mutex_init( mutex ) == 0 ) {
|
|
|
|
return mutex;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2000-05-04 02:59:58 +08:00
|
|
|
static int slap_sasl_mutex_lock(void *mutex)
|
2000-01-02 09:21:25 +08:00
|
|
|
{
|
|
|
|
return ldap_pvt_thread_mutex_lock( (ldap_pvt_thread_mutex_t *)mutex );
|
|
|
|
}
|
|
|
|
|
2000-05-04 02:59:58 +08:00
|
|
|
static int slap_sasl_mutex_unlock(void *mutex)
|
2000-01-02 09:21:25 +08:00
|
|
|
{
|
|
|
|
return ldap_pvt_thread_mutex_unlock( (ldap_pvt_thread_mutex_t *)mutex );
|
|
|
|
}
|
|
|
|
|
2000-05-04 02:59:58 +08:00
|
|
|
static void slap_sasl_mutex_dispose(void *mutex)
|
2000-01-02 09:21:25 +08:00
|
|
|
{
|
|
|
|
(void) ldap_pvt_thread_mutex_destroy( (ldap_pvt_thread_mutex_t *)mutex );
|
|
|
|
free( mutex );
|
|
|
|
}
|
1999-08-03 10:37:42 +08:00
|
|
|
|
2000-05-04 02:59:58 +08:00
|
|
|
static int
|
|
|
|
slap_sasl_err2ldap( int saslerr )
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
switch (saslerr) {
|
|
|
|
case SASL_CONTINUE:
|
|
|
|
rc = LDAP_SASL_BIND_IN_PROGRESS;
|
|
|
|
break;
|
|
|
|
case SASL_OK:
|
|
|
|
rc = LDAP_SUCCESS;
|
|
|
|
break;
|
|
|
|
case SASL_FAIL:
|
|
|
|
rc = LDAP_OTHER;
|
|
|
|
break;
|
|
|
|
case SASL_NOMEM:
|
|
|
|
rc = LDAP_OTHER;
|
|
|
|
break;
|
|
|
|
case SASL_NOMECH:
|
|
|
|
rc = LDAP_AUTH_METHOD_NOT_SUPPORTED;
|
|
|
|
break;
|
|
|
|
case SASL_BADAUTH:
|
|
|
|
rc = LDAP_INVALID_CREDENTIALS;
|
|
|
|
break;
|
|
|
|
case SASL_NOAUTHZ:
|
|
|
|
rc = LDAP_INSUFFICIENT_ACCESS;
|
|
|
|
break;
|
|
|
|
case SASL_TOOWEAK:
|
|
|
|
case SASL_ENCRYPT:
|
|
|
|
rc = LDAP_INAPPROPRIATE_AUTH;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
rc = LDAP_OTHER;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-08-03 10:37:42 +08:00
|
|
|
int sasl_init( void )
|
|
|
|
{
|
|
|
|
int rc;
|
1999-08-04 08:11:22 +08:00
|
|
|
char *mechs;
|
1999-08-04 07:23:05 +08:00
|
|
|
sasl_conn_t *server = NULL;
|
1999-08-03 10:37:42 +08:00
|
|
|
|
2000-01-02 09:21:25 +08:00
|
|
|
sasl_set_alloc( ch_malloc, ch_calloc, ch_realloc, ch_free );
|
|
|
|
|
2000-05-04 02:59:58 +08:00
|
|
|
sasl_set_mutex(
|
|
|
|
slap_sasl_mutex_new,
|
|
|
|
slap_sasl_mutex_lock,
|
|
|
|
slap_sasl_mutex_unlock,
|
|
|
|
slap_sasl_mutex_dispose );
|
2000-01-02 09:21:25 +08:00
|
|
|
|
|
|
|
rc = sasl_server_init( NULL, "slapd" );
|
1999-08-03 10:37:42 +08:00
|
|
|
|
|
|
|
if( rc != SASL_OK ) {
|
|
|
|
Debug( LDAP_DEBUG_ANY, "sasl_server_init failed\n",
|
|
|
|
0, 0, 0 );
|
1999-08-04 07:23:05 +08:00
|
|
|
return -1;
|
1999-08-03 10:37:42 +08:00
|
|
|
}
|
|
|
|
|
2000-04-26 00:03:17 +08:00
|
|
|
if( sasl_host == NULL ) {
|
2000-05-11 10:41:34 +08:00
|
|
|
static char hostname[MAXHOSTNAMELEN+1];
|
2000-04-26 00:03:17 +08:00
|
|
|
|
|
|
|
if( gethostname( hostname, MAXHOSTNAMELEN ) == 0 ) {
|
|
|
|
hostname[MAXHOSTNAMELEN] = '\0';
|
|
|
|
sasl_host = hostname;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = sasl_server_new( "ldap", sasl_host, NULL, NULL,
|
1999-08-03 10:37:42 +08:00
|
|
|
SASL_SECURITY_LAYER,
|
|
|
|
&server );
|
|
|
|
|
|
|
|
if( rc != SASL_OK ) {
|
|
|
|
Debug( LDAP_DEBUG_ANY, "sasl_server_new failed\n",
|
|
|
|
0, 0, 0 );
|
1999-08-04 07:23:05 +08:00
|
|
|
return -1;
|
1999-08-03 10:37:42 +08:00
|
|
|
}
|
|
|
|
|
1999-08-04 07:23:05 +08:00
|
|
|
#ifdef RESTRICT_SASL
|
|
|
|
{
|
|
|
|
sasl_security_properties_t secprops;
|
|
|
|
memset(&secprops, 0, sizeof(secprops));
|
|
|
|
secprops.security_flags = SASL_SEC_NOPLAINTEXT | SASL_SEC_NOANONYMOUS;
|
|
|
|
secprops.property_names = NULL;
|
|
|
|
secprops.property_values = NULL;
|
1999-08-03 10:37:42 +08:00
|
|
|
|
1999-08-04 07:23:05 +08:00
|
|
|
rc = sasl_setprop( server, SASL_SEC_PROPS, &secprops );
|
1999-08-03 10:37:42 +08:00
|
|
|
|
1999-08-04 07:23:05 +08:00
|
|
|
if( rc != SASL_OK ) {
|
|
|
|
Debug( LDAP_DEBUG_ANY, "sasl_setprop failed\n",
|
|
|
|
0, 0, 0 );
|
|
|
|
return -1;
|
|
|
|
}
|
1999-08-03 10:37:42 +08:00
|
|
|
}
|
1999-08-04 07:23:05 +08:00
|
|
|
#endif
|
1999-08-03 10:37:42 +08:00
|
|
|
|
|
|
|
rc = sasl_listmech( server, NULL, NULL, ",", NULL,
|
1999-08-04 08:11:22 +08:00
|
|
|
&mechs, NULL, NULL);
|
1999-08-03 10:37:42 +08:00
|
|
|
|
|
|
|
if( rc != SASL_OK ) {
|
|
|
|
Debug( LDAP_DEBUG_ANY, "sasl_listmech failed: %d\n",
|
|
|
|
rc, 0, 0 );
|
1999-08-04 07:23:05 +08:00
|
|
|
return -1;
|
1999-08-03 10:37:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Debug( LDAP_DEBUG_TRACE, "SASL mechanisms: %s\n",
|
1999-08-04 08:11:22 +08:00
|
|
|
mechs, 0, 0 );
|
1999-08-03 10:37:42 +08:00
|
|
|
|
1999-08-04 08:11:22 +08:00
|
|
|
supportedSASLMechanisms = str2charray( mechs, "," );
|
2000-05-10 12:29:51 +08:00
|
|
|
|
|
|
|
#ifdef SLAPD_SPASSWD
|
|
|
|
lutil_passwd_sasl_conn = server;
|
|
|
|
#else
|
1999-08-04 07:23:05 +08:00
|
|
|
sasl_dispose( &server );
|
2000-05-10 12:29:51 +08:00
|
|
|
#endif
|
1999-08-03 10:37:42 +08:00
|
|
|
|
1999-08-04 07:23:05 +08:00
|
|
|
return 0;
|
1999-08-03 10:37:42 +08:00
|
|
|
}
|
|
|
|
|
1999-08-04 07:23:05 +08:00
|
|
|
int sasl_destroy( void )
|
1999-08-03 10:37:42 +08:00
|
|
|
{
|
2000-05-10 12:29:51 +08:00
|
|
|
#ifdef SLAPD_SPASSWD
|
|
|
|
sasl_dispose( &lutil_passwd_sasl_conn );
|
|
|
|
#endif
|
1999-08-04 07:23:05 +08:00
|
|
|
charray_free( supportedSASLMechanisms );
|
|
|
|
return 0;
|
1999-08-03 10:37:42 +08:00
|
|
|
}
|
1999-08-04 07:23:05 +08:00
|
|
|
|
2000-01-02 09:21:25 +08:00
|
|
|
#ifdef HAVE_CYRUS_SASL
|
|
|
|
int sasl_bind(
|
|
|
|
Connection *conn,
|
|
|
|
Operation *op,
|
|
|
|
char *dn,
|
|
|
|
char *ndn,
|
|
|
|
char *mech,
|
|
|
|
struct berval *cred,
|
2000-04-26 01:23:54 +08:00
|
|
|
char **edn )
|
2000-01-02 09:21:25 +08:00
|
|
|
{
|
|
|
|
struct berval response;
|
|
|
|
const char *errstr;
|
|
|
|
int sc;
|
|
|
|
int rc = 1;
|
|
|
|
|
2000-05-17 00:22:52 +08:00
|
|
|
Debug(LDAP_DEBUG_ARGS,
|
|
|
|
"==> sasl_bind: dn=\"%s\" mech=%s cred->bv_len=%d\n",
|
2000-01-02 09:21:25 +08:00
|
|
|
dn, mech, cred ? cred->bv_len : 0 );
|
|
|
|
|
2000-04-26 02:02:50 +08:00
|
|
|
if ( conn->c_sasl_bind_context == NULL ) {
|
2000-01-02 09:21:25 +08:00
|
|
|
sasl_callback_t callbacks[4];
|
|
|
|
int cbnum = 0;
|
|
|
|
|
2000-04-26 00:03:17 +08:00
|
|
|
#if 0
|
2000-01-02 09:21:25 +08:00
|
|
|
if (be->be_sasl_authorize) {
|
|
|
|
callbacks[cbnum].id = SASL_CB_PROXY_POLICY;
|
|
|
|
callbacks[cbnum].proc = be->be_sasl_authorize;
|
|
|
|
callbacks[cbnum].context = be;
|
|
|
|
++cbnum;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (be->be_sasl_getsecret) {
|
|
|
|
callbacks[cbnum].id = SASL_CB_SERVER_GETSECRET;
|
|
|
|
callbacks[cbnum].proc = be->be_sasl_getsecret;
|
|
|
|
callbacks[cbnum].context = be;
|
|
|
|
++cbnum;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (be->be_sasl_putsecret) {
|
|
|
|
callbacks[cbnum].id = SASL_CB_SERVER_PUTSECRET;
|
|
|
|
callbacks[cbnum].proc = be->be_sasl_putsecret;
|
|
|
|
callbacks[cbnum].context = be;
|
|
|
|
++cbnum;
|
|
|
|
}
|
2000-04-26 00:03:17 +08:00
|
|
|
#endif
|
|
|
|
|
2000-01-02 09:21:25 +08:00
|
|
|
callbacks[cbnum].id = SASL_CB_LIST_END;
|
|
|
|
callbacks[cbnum].proc = NULL;
|
|
|
|
callbacks[cbnum].context = NULL;
|
2000-04-25 21:21:42 +08:00
|
|
|
|
|
|
|
/* create new SASL context */
|
2000-04-26 00:03:17 +08:00
|
|
|
sc = sasl_server_new( "ldap", sasl_host, global_realm,
|
|
|
|
callbacks, SASL_SECURITY_LAYER, &conn->c_sasl_bind_context );
|
|
|
|
|
|
|
|
if( sc != SASL_OK ) {
|
2000-04-26 01:23:54 +08:00
|
|
|
send_ldap_result( conn, op, rc = LDAP_AUTH_METHOD_NOT_SUPPORTED,
|
2000-01-02 09:21:25 +08:00
|
|
|
NULL, NULL, NULL, NULL );
|
|
|
|
} else {
|
2000-05-16 12:52:37 +08:00
|
|
|
unsigned reslen;
|
2000-01-02 09:21:25 +08:00
|
|
|
conn->c_authmech = ch_strdup( mech );
|
2000-05-16 12:52:37 +08:00
|
|
|
|
2000-04-26 00:03:17 +08:00
|
|
|
sc = sasl_server_start( conn->c_sasl_bind_context, conn->c_authmech,
|
2000-05-16 12:52:37 +08:00
|
|
|
cred->bv_val, cred->bv_len,
|
|
|
|
(char **)&response.bv_val, &reslen, &errstr );
|
|
|
|
|
|
|
|
response.bv_len = reslen;
|
|
|
|
|
2000-01-02 09:21:25 +08:00
|
|
|
if ( (sc != SASL_OK) && (sc != SASL_CONTINUE) ) {
|
2000-05-04 02:59:58 +08:00
|
|
|
send_ldap_result( conn, op, rc = slap_sasl_err2ldap( sc ),
|
2000-01-02 09:21:25 +08:00
|
|
|
NULL, errstr, NULL, NULL );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2000-05-16 12:52:37 +08:00
|
|
|
unsigned reslen;
|
2000-04-26 00:03:17 +08:00
|
|
|
sc = sasl_server_step( conn->c_sasl_bind_context, cred->bv_val, cred->bv_len,
|
2000-05-16 12:52:37 +08:00
|
|
|
(char **)&response.bv_val, &reslen, &errstr );
|
|
|
|
|
|
|
|
response.bv_len = reslen;
|
|
|
|
|
2000-01-02 09:21:25 +08:00
|
|
|
if ( (sc != SASL_OK) && (sc != SASL_CONTINUE) ) {
|
2000-05-04 02:59:58 +08:00
|
|
|
send_ldap_result( conn, op, rc = slap_sasl_err2ldap( sc ),
|
2000-01-02 09:21:25 +08:00
|
|
|
NULL, errstr, NULL, NULL );
|
|
|
|
}
|
|
|
|
}
|
2000-04-25 21:21:42 +08:00
|
|
|
|
2000-01-02 09:21:25 +08:00
|
|
|
if ( sc == SASL_OK ) {
|
|
|
|
char *authzid;
|
|
|
|
|
2000-04-26 00:03:17 +08:00
|
|
|
if ( ( sc = sasl_getprop( conn->c_sasl_bind_context, SASL_USERNAME,
|
2000-01-02 09:21:25 +08:00
|
|
|
(void **)&authzid ) ) != SASL_OK ) {
|
2000-05-04 02:59:58 +08:00
|
|
|
send_ldap_result( conn, op, rc = slap_sasl_err2ldap( sc ),
|
2000-01-02 09:21:25 +08:00
|
|
|
NULL, NULL, NULL, NULL );
|
2000-04-26 01:23:54 +08:00
|
|
|
|
2000-01-02 09:21:25 +08:00
|
|
|
} else {
|
2000-04-26 02:02:50 +08:00
|
|
|
Debug(LDAP_DEBUG_TRACE, "<== sasl_bind: username=%s\n",
|
|
|
|
authzid, 0, 0);
|
|
|
|
|
2000-04-26 01:23:54 +08:00
|
|
|
if( strncasecmp( authzid, "anonymous", sizeof("anonyous")-1 ) &&
|
|
|
|
( ( authzid[sizeof("anonymous")] == '\0' ) ||
|
|
|
|
( authzid[sizeof("anonymous")] == '@' ) ) )
|
|
|
|
{
|
2000-04-25 21:21:42 +08:00
|
|
|
*edn = ch_malloc( sizeof( "authzid=" ) + strlen( authzid ) );
|
2000-01-02 09:21:25 +08:00
|
|
|
strcpy( *edn, "authzid=" );
|
|
|
|
strcat( *edn, authzid );
|
|
|
|
}
|
2000-04-26 01:23:54 +08:00
|
|
|
|
|
|
|
send_ldap_result( conn, op, rc = LDAP_SUCCESS,
|
|
|
|
NULL, NULL, NULL, NULL );
|
2000-01-02 09:21:25 +08:00
|
|
|
}
|
2000-04-26 01:23:54 +08:00
|
|
|
|
2000-01-02 09:21:25 +08:00
|
|
|
} else if ( sc == SASL_CONTINUE ) {
|
2000-04-26 01:23:54 +08:00
|
|
|
send_ldap_sasl( conn, op, rc = LDAP_SASL_BIND_IN_PROGRESS,
|
2000-04-25 21:21:42 +08:00
|
|
|
NULL, NULL, NULL, NULL, &response );
|
2000-01-02 09:21:25 +08:00
|
|
|
}
|
|
|
|
|
2000-04-26 02:02:50 +08:00
|
|
|
if ( sc != SASL_CONTINUE && conn->c_sasl_bind_context != NULL ) {
|
|
|
|
sasl_dispose( &conn->c_sasl_bind_context );
|
|
|
|
conn->c_sasl_bind_context = NULL;
|
|
|
|
}
|
|
|
|
|
2000-01-02 09:21:25 +08:00
|
|
|
Debug(LDAP_DEBUG_TRACE, "<== sasl_bind: rc=%d\n", rc, 0, 0);
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
#endif /* HAVE_CYRUS_SASL */
|
|
|
|
|
1999-08-04 07:23:05 +08:00
|
|
|
#else
|
|
|
|
/* no SASL support */
|
2000-04-26 03:28:00 +08:00
|
|
|
int sasl_bind(
|
|
|
|
Connection *conn,
|
|
|
|
Operation *op,
|
|
|
|
char *dn,
|
|
|
|
char *ndn,
|
|
|
|
char *mech,
|
|
|
|
struct berval *cred,
|
|
|
|
char **edn )
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
|
|
|
|
NULL, "SASL unavailable", NULL, NULL );
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
1999-08-04 07:23:05 +08:00
|
|
|
int sasl_init( void ) { return 0; }
|
|
|
|
int sasl_destroy( void ) { return 0; }
|
1999-08-03 10:37:42 +08:00
|
|
|
#endif
|