version 1.9, 2000/01/16 00:29:15
|
version 1.25, 2000/06/07 03:17:29
|
Line 1
|
Line 1
|
/* $OpenLDAP: pkg/ldap/libraries/libldap/sasl.c,v 1.8 2000/01/08 17:42:18 kdz Exp $ */ |
/* $OpenLDAP: pkg/ldap/libraries/libldap/sasl.c,v 1.24 2000/06/01 20:01:01 kurt Exp $ */ |
/* |
/* |
* Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved. |
* Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved. |
* COPYING RESTRICTIONS APPLY, see COPYRIGHT file |
* COPYING RESTRICTIONS APPLY, see COPYRIGHT file |
*/ |
*/ |
|
|
Line 32
|
Line 32
|
#include <ac/socket.h> |
#include <ac/socket.h> |
#include <ac/string.h> |
#include <ac/string.h> |
#include <ac/time.h> |
#include <ac/time.h> |
|
#include <ac/errno.h> |
|
|
#include "ldap-int.h" |
#include "ldap-int.h" |
|
|
|
|
/* |
/* |
* ldap_sasl_bind - bind to the ldap server (and X.500). The dn, mechanism, and |
* ldap_sasl_bind - bind to the ldap server (and X.500). |
* credentials of the entry to which to bind are supplied. The message id |
* The dn (usually NULL), mechanism, and credentials are provided. |
* of the request initiated is provided upon successful (LDAP_SUCCESS) return. |
* The message id of the request initiated is provided upon successful |
|
* (LDAP_SUCCESS) return. |
* |
* |
* Example: |
* Example: |
* ldap_sasl_bind( ld, "cn=manager, o=university of michigan, c=us", |
* ldap_sasl_bind( ld, NULL, "mechanism", |
* "mechanism", "secret", NULL, NULL, &msgid ) |
* cred, NULL, NULL, &msgid ) |
*/ |
*/ |
|
|
int |
int |
Line 190 ldap_sasl_bind_s(
|
Line 192 ldap_sasl_bind_s(
|
rc = ldap_parse_sasl_bind_result( ld, result, &scredp, 0 ); |
rc = ldap_parse_sasl_bind_result( ld, result, &scredp, 0 ); |
} |
} |
|
|
if( rc != LDAP_SUCCESS ) { |
if ( rc != LDAP_SUCCESS && rc != LDAP_SASL_BIND_IN_PROGRESS ) { |
ldap_msgfree( result ); |
ldap_msgfree( result ); |
return( rc ); |
return( rc ); |
} |
} |
|
|
rc = ldap_result2error( ld, result, 1 ); |
rc = ldap_result2error( ld, result, 1 ); |
|
|
if( rc == LDAP_SUCCESS ) { |
if ( rc == LDAP_SUCCESS || rc == LDAP_SASL_BIND_IN_PROGRESS ) { |
if( servercredp != NULL ) { |
if( servercredp != NULL ) { |
*servercredp = scredp; |
*servercredp = scredp; |
|
scredp = NULL; |
} |
} |
|
} |
|
|
} else if (scredp != NULL ) { |
if ( scredp != NULL ) { |
ber_bvfree(scredp); |
ber_bvfree(scredp); |
} |
} |
|
|
Line 346 ldap_parse_sasl_bind_result(
|
Line 350 ldap_parse_sasl_bind_result(
|
* Various Cyrus SASL related stuff. |
* Various Cyrus SASL related stuff. |
*/ |
*/ |
|
|
static int sasl_setup( Sockbuf *sb, void *arg ); |
#define MAX_BUFF_SIZE 65536 |
static int sasl_remove( Sockbuf *sb ); |
#define MIN_BUFF_SIZE 4096 |
static ber_slen_t sasl_read( Sockbuf *sb, void *buf, ber_len_t len ); |
|
static ber_slen_t sasl_write( Sockbuf *sb, void *buf, ber_len_t len ); |
|
static int sasl_close( Sockbuf *sb ); |
|
|
|
static Sockbuf_IO sasl_io= |
|
{ |
|
sasl_setup, |
|
sasl_remove, |
|
sasl_read, |
|
sasl_write, |
|
sasl_close |
|
}; |
|
|
|
#define HAS_SASL( sb ) ((sb)->sb_io==&sasl_io) |
|
|
|
static char * |
static char * |
array2str( char **a ) |
array2str( char **a ) |
Line 421 int ldap_pvt_sasl_init( void )
|
Line 411 int ldap_pvt_sasl_init( void )
|
return -1; |
return -1; |
} |
} |
|
|
int ldap_pvt_sasl_install( Sockbuf *sb, void *ctx_arg ) |
/* |
|
* SASL encryption support for LBER Sockbufs |
|
*/ |
|
|
|
struct sb_sasl_data { |
|
sasl_conn_t *sasl_context; |
|
Sockbuf_Buf sec_buf_in; |
|
Sockbuf_Buf buf_in; |
|
Sockbuf_Buf buf_out; |
|
}; |
|
|
|
static int |
|
sb_sasl_setup( Sockbuf_IO_Desc *sbiod, void *arg ) |
{ |
{ |
/* don't install the stuff unless security has been negotiated */ |
struct sb_sasl_data *p; |
|
|
|
assert( sbiod != NULL ); |
|
|
if ( !HAS_SASL( sb ) ) { |
p = LBER_MALLOC( sizeof( *p ) ); |
ber_pvt_sb_clear_io( sb ); |
if ( p == NULL ) |
ber_pvt_sb_set_io( sb, &sasl_io, ctx_arg ); |
return -1; |
|
p->sasl_context = (sasl_conn_t *)arg; |
|
ber_pvt_sb_buf_init( &p->sec_buf_in ); |
|
ber_pvt_sb_buf_init( &p->buf_in ); |
|
ber_pvt_sb_buf_init( &p->buf_out ); |
|
if ( ber_pvt_sb_grow_buffer( &p->sec_buf_in, MIN_BUFF_SIZE ) < 0 ) { |
|
errno = ENOMEM; |
|
return -1; |
} |
} |
|
|
|
sbiod->sbiod_pvt = p; |
|
|
return 0; |
return 0; |
} |
} |
|
|
static int sasl_setup( Sockbuf *sb, void *arg ) |
static int |
|
sb_sasl_remove( Sockbuf_IO_Desc *sbiod ) |
{ |
{ |
sb->sb_iodata = arg; |
struct sb_sasl_data *p; |
|
|
|
assert( sbiod != NULL ); |
|
|
|
p = (struct sb_sasl_data *)sbiod->sbiod_pvt; |
|
ber_pvt_sb_buf_destroy( &p->sec_buf_in ); |
|
ber_pvt_sb_buf_destroy( &p->buf_in ); |
|
ber_pvt_sb_buf_destroy( &p->buf_out ); |
|
LBER_FREE( p ); |
|
sbiod->sbiod_pvt = NULL; |
return 0; |
return 0; |
} |
} |
|
|
static int sasl_remove( Sockbuf *sb ) |
static ber_len_t |
|
sb_sasl_pkt_length( const char *buf, int debuglevel ) |
{ |
{ |
return 0; |
ber_len_t size; |
|
long tmp; |
|
|
|
assert( buf != NULL ); |
|
|
|
tmp = *((long *)buf); |
|
size = ntohl( tmp ); |
|
|
|
if ( size > MAX_BUFF_SIZE ) { |
|
/* somebody is trying to mess me up. */ |
|
ber_log_printf( LDAP_DEBUG_ANY, debuglevel, |
|
"sb_sasl_pkt_length: received illegal packet length " |
|
"of %lu bytes\n", (unsigned long)size ); |
|
size = 16; /* this should lead to an error. */ |
|
} |
|
|
|
return size + 4; /* include the size !!! */ |
} |
} |
|
|
static ber_slen_t sasl_read( Sockbuf *sb, void *buf, ber_len_t buflen ) |
/* Drop a processed packet from the input buffer */ |
|
static void |
|
sb_sasl_drop_packet ( Sockbuf_Buf *sec_buf_in, int debuglevel ) |
{ |
{ |
char *recv_tok; |
ber_slen_t len; |
unsigned recv_tok_len; |
|
sasl_conn_t *conn = (sasl_conn_t *)sb->sb_iodata; |
|
|
|
if ((ber_pvt_sb_io_tcp.sbi_read)( sb, buf, buflen ) != buflen ) { |
len = sec_buf_in->buf_ptr - sec_buf_in->buf_end; |
return -1; |
if ( len > 0 ) |
|
memmove( sec_buf_in->buf_base, sec_buf_in->buf_base + |
|
sec_buf_in->buf_end, len ); |
|
|
|
if ( len >= 4 ) { |
|
sec_buf_in->buf_end = sb_sasl_pkt_length( sec_buf_in->buf_base, |
|
debuglevel); |
|
} |
|
else { |
|
sec_buf_in->buf_end = 0; |
} |
} |
|
sec_buf_in->buf_ptr = len; |
|
} |
|
|
if ( sasl_decode( conn, buf, buflen, &recv_tok, &recv_tok_len ) != SASL_OK ) { |
static ber_slen_t |
|
sb_sasl_read( Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len) |
|
{ |
|
struct sb_sasl_data *p; |
|
ber_slen_t ret, bufptr; |
|
|
|
assert( sbiod != NULL ); |
|
assert( SOCKBUF_VALID( sbiod->sbiod_sb ) ); |
|
|
|
p = (struct sb_sasl_data *)sbiod->sbiod_pvt; |
|
|
|
/* Are there anything left in the buffer? */ |
|
ret = ber_pvt_sb_copy_out( &p->buf_in, buf, len ); |
|
bufptr = ret; |
|
len -= ret; |
|
|
|
if ( len == 0 ) |
|
return bufptr; |
|
|
|
ber_pvt_sb_buf_destroy( &p->buf_in ); |
|
|
|
/* Read the length of the packet */ |
|
while ( p->sec_buf_in.buf_ptr < 4 ) { |
|
ret = LBER_SBIOD_READ_NEXT( sbiod, p->sec_buf_in.buf_base, |
|
4 - p->sec_buf_in.buf_ptr ); |
|
#ifdef EINTR |
|
if ( ( ret < 0 ) && ( errno == EINTR ) ) |
|
continue; |
|
#endif |
|
if ( ret <= 0 ) |
|
return ret; |
|
|
|
p->sec_buf_in.buf_ptr += ret; |
|
} |
|
|
|
/* The new packet always starts at p->sec_buf_in.buf_base */ |
|
ret = sb_sasl_pkt_length( p->sec_buf_in.buf_base, |
|
sbiod->sbiod_sb->sb_debug ); |
|
|
|
/* Grow the packet buffer if neccessary */ |
|
if ( ( p->sec_buf_in.buf_size < ret ) && |
|
ber_pvt_sb_grow_buffer( &p->sec_buf_in, ret ) < 0 ) { |
|
errno = ENOMEM; |
return -1; |
return -1; |
} |
} |
|
p->sec_buf_in.buf_end = ret; |
|
|
|
/* Did we read the whole encrypted packet? */ |
|
while ( p->sec_buf_in.buf_ptr < p->sec_buf_in.buf_end ) { |
|
/* No, we have got only a part of it */ |
|
ret = p->sec_buf_in.buf_end - p->sec_buf_in.buf_ptr; |
|
|
|
ret = LBER_SBIOD_READ_NEXT( sbiod, p->sec_buf_in.buf_base + |
|
p->sec_buf_in.buf_ptr, ret ); |
|
#ifdef EINTR |
|
if ( ( ret < 0 ) && ( errno == EINTR ) ) |
|
continue; |
|
#endif |
|
if ( ret <= 0 ) |
|
return ret; |
|
|
if ( recv_tok_len > buflen ) { |
p->sec_buf_in.buf_ptr += ret; |
LDAP_FREE( recv_tok ); |
} |
|
|
|
/* Decode the packet */ |
|
ret = sasl_decode( p->sasl_context, p->sec_buf_in.buf_base, |
|
p->sec_buf_in.buf_end, &p->buf_in.buf_base, |
|
(unsigned *)&p->buf_in.buf_end ); |
|
if ( ret != SASL_OK ) { |
|
ber_log_printf( LDAP_DEBUG_ANY, sbiod->sbiod_sb->sb_debug, |
|
"sb_sasl_read: failed to decode packet: %s\n", |
|
sasl_errstring( ret, NULL, NULL ) ); |
|
sb_sasl_drop_packet( &p->sec_buf_in, |
|
sbiod->sbiod_sb->sb_debug ); |
|
errno = EIO; |
return -1; |
return -1; |
} |
} |
|
|
|
/* Drop the packet from the input buffer */ |
|
sb_sasl_drop_packet( &p->sec_buf_in, sbiod->sbiod_sb->sb_debug ); |
|
|
memcpy( buf, recv_tok, recv_tok_len ); |
p->buf_in.buf_size = p->buf_in.buf_end; |
|
|
LDAP_FREE( recv_tok ); |
bufptr += ber_pvt_sb_copy_out( &p->buf_in, (char*) buf + bufptr, len ); |
|
|
return recv_tok_len; |
return bufptr; |
} |
} |
|
|
static ber_slen_t sasl_write( Sockbuf *sb, void *buf, ber_len_t len ) |
static ber_slen_t |
|
sb_sasl_write( Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len) |
{ |
{ |
char *wrapped_tok; |
struct sb_sasl_data *p; |
unsigned wrapped_tok_len; |
int ret; |
sasl_conn_t *conn = (sasl_conn_t *)sb->sb_iodata; |
|
|
|
if ( sasl_encode( conn, (const char *)buf, len, |
assert( sbiod != NULL ); |
&wrapped_tok, &wrapped_tok_len ) != SASL_OK ) { |
assert( SOCKBUF_VALID( sbiod->sbiod_sb ) ); |
return -1; |
|
} |
p = (struct sb_sasl_data *)sbiod->sbiod_pvt; |
|
|
if ((ber_pvt_sb_io_tcp.sbi_write)( sb, wrapped_tok, wrapped_tok_len ) != wrapped_tok_len ) { |
/* Are there anything left in the buffer? */ |
LDAP_FREE( wrapped_tok ); |
if ( p->buf_out.buf_ptr != p->buf_out.buf_end ) { |
|
ret = ber_pvt_sb_do_write( sbiod, &p->buf_out ); |
|
if ( ret <= 0 ) |
|
return ret; |
|
} |
|
|
|
/* now encode the next packet. */ |
|
ber_pvt_sb_buf_destroy( &p->buf_out ); |
|
ret = sasl_encode( p->sasl_context, buf, len, &p->buf_out.buf_base, |
|
(unsigned *)&p->buf_out.buf_size ); |
|
if ( ret != SASL_OK ) { |
|
ber_log_printf( LDAP_DEBUG_ANY, sbiod->sbiod_sb->sb_debug, |
|
"sb_sasl_write: failed to encode packet: %s\n", |
|
sasl_errstring( ret, NULL, NULL ) ); |
return -1; |
return -1; |
} |
} |
|
p->buf_out.buf_end = p->buf_out.buf_size; |
|
|
LDAP_FREE( wrapped_tok ); |
ret = ber_pvt_sb_do_write( sbiod, &p->buf_out ); |
|
if ( ret <= 0 ) |
|
return ret; |
return len; |
return len; |
} |
} |
|
|
static int sasl_close( Sockbuf *sb ) |
static int |
|
sb_sasl_ctrl( Sockbuf_IO_Desc *sbiod, int opt, void *arg ) |
{ |
{ |
(ber_pvt_sb_io_tcp.sbi_close)( sb ); |
struct sb_sasl_data *p; |
|
|
|
p = (struct sb_sasl_data *)sbiod->sbiod_pvt; |
|
|
|
if ( opt == LBER_SB_OPT_DATA_READY ) { |
|
if ( p->buf_in.buf_ptr != p->buf_in.buf_end ) |
|
return 1; |
|
} |
|
|
|
return LBER_SBIOD_CTRL_NEXT( sbiod, opt, arg ); |
} |
} |
|
|
int |
Sockbuf_IO ldap_pvt_sockbuf_io_sasl = |
ldap_pvt_sasl_err2ldap( int saslerr ) |
{ |
|
sb_sasl_setup, /* sbi_setup */ |
|
sb_sasl_remove, /* sbi_remove */ |
|
sb_sasl_ctrl, /* sbi_ctrl */ |
|
sb_sasl_read, /* sbi_read */ |
|
sb_sasl_write, /* sbi_write */ |
|
NULL /* sbi_close */ |
|
}; |
|
|
|
int ldap_pvt_sasl_install( Sockbuf *sb, void *ctx_arg ) |
|
{ |
|
/* don't install the stuff unless security has been negotiated */ |
|
|
|
if ( !ber_sockbuf_ctrl( sb, LBER_SB_OPT_HAS_IO, |
|
&ldap_pvt_sockbuf_io_sasl ) ) |
|
ber_sockbuf_add_io( sb, &ldap_pvt_sockbuf_io_sasl, |
|
LBER_SBIOD_LEVEL_APPLICATION, ctx_arg ); |
|
|
|
return LDAP_SUCCESS; |
|
} |
|
|
|
static int |
|
sasl_err2ldap( int saslerr ) |
{ |
{ |
int rc; |
int rc; |
|
|
switch (saslerr) { |
switch (saslerr) { |
case SASL_CONTINUE: |
case SASL_CONTINUE: |
rc = LDAP_SASL_BIND_IN_PROGRESS; |
rc = LDAP_MORE_RESULTS_TO_RETURN; |
break; |
break; |
case SASL_OK: |
case SASL_OK: |
rc = LDAP_SUCCESS; |
rc = LDAP_SUCCESS; |
break; |
break; |
case SASL_FAIL: |
case SASL_FAIL: |
rc = LDAP_OPERATIONS_ERROR; |
rc = LDAP_LOCAL_ERROR; |
break; |
break; |
case SASL_NOMEM: |
case SASL_NOMEM: |
rc = LDAP_NO_MEMORY; |
rc = LDAP_NO_MEMORY; |
break; |
break; |
case SASL_NOMECH: |
case SASL_NOMECH: |
rc = LDAP_AUTH_METHOD_NOT_SUPPORTED; |
rc = LDAP_AUTH_UNKNOWN; |
break; |
break; |
case SASL_BADAUTH: |
case SASL_BADAUTH: |
rc = LDAP_INVALID_CREDENTIALS; |
rc = LDAP_AUTH_UNKNOWN; |
break; |
break; |
case SASL_NOAUTHZ: |
case SASL_NOAUTHZ: |
rc = LDAP_INSUFFICIENT_ACCESS; |
rc = LDAP_PARAM_ERROR; |
break; |
break; |
case SASL_TOOWEAK: |
case SASL_TOOWEAK: |
case SASL_ENCRYPT: |
case SASL_ENCRYPT: |
rc = LDAP_INAPPROPRIATE_AUTH; |
rc = LDAP_AUTH_UNKNOWN; |
break; |
break; |
default: |
default: |
rc = LDAP_OPERATIONS_ERROR; |
rc = LDAP_LOCAL_ERROR; |
break; |
break; |
} |
} |
|
|
|
assert( rc == LDAP_SUCCESS || LDAP_API_ERROR( rc ) ); |
return rc; |
return rc; |
} |
} |
|
|
int |
int |
ldap_pvt_sasl_getmechs ( LDAP *ld, LDAP_CONST char *desired, char **pmechlist ) |
ldap_pvt_sasl_getmechs ( LDAP *ld, char **pmechlist ) |
{ |
{ |
/* we need to query the server for supported mechs anyway */ |
/* we need to query the server for supported mechs anyway */ |
LDAPMessage *res, *e; |
LDAPMessage *res, *e; |
char *attrs[] = { "supportedSASLMechanisms", NULL }; |
char *attrs[] = { "supportedSASLMechanisms", NULL }; |
char **values, *mechlist, **p; |
char **values, *mechlist; |
int rc; |
int rc; |
|
|
|
Debug( LDAP_DEBUG_TRACE, "ldap_pvt_sasl_getmech\n", 0, 0, 0 ); |
|
|
rc = ldap_search_s( ld, NULL, LDAP_SCOPE_BASE, |
rc = ldap_search_s( ld, NULL, LDAP_SCOPE_BASE, |
"(objectclass=*)", attrs, 0, &res ); |
NULL, attrs, 0, &res ); |
|
|
if ( rc != LDAP_SUCCESS ) { |
if ( rc != LDAP_SUCCESS ) { |
return ld->ld_errno; |
return ld->ld_errno; |
Line 566 ldap_pvt_sasl_getmechs ( LDAP *ld, LDAP_
|
Line 739 ldap_pvt_sasl_getmechs ( LDAP *ld, LDAP_
|
return ld->ld_errno; |
return ld->ld_errno; |
} |
} |
|
|
if ( desired != NULL ) { |
|
rc = LDAP_INAPPROPRIATE_AUTH; |
|
|
|
for ( p = values; *p != NULL; p++ ) { |
|
if ( !strcmp( *p, desired ) == 0 ) { |
|
rc = LDAP_SUCCESS; |
|
break; |
|
} |
|
} |
|
|
|
if ( rc == LDAP_SUCCESS ) { |
|
/* just return this */ |
|
*pmechlist = LDAP_STRDUP( desired ); |
|
return LDAP_SUCCESS; |
|
} else { |
|
/* couldn't find it */ |
|
ld->ld_errno = LDAP_INAPPROPRIATE_AUTH; |
|
return ld->ld_errno; |
|
} |
|
} |
|
|
|
mechlist = array2str( values ); |
mechlist = array2str( values ); |
if ( mechlist == NULL ) { |
if ( mechlist == NULL ) { |
ld->ld_errno = LDAP_NO_MEMORY; |
ld->ld_errno = LDAP_NO_MEMORY; |
ldap_value_free( values ); |
LDAP_VFREE( values ); |
ldap_msgfree( res ); |
ldap_msgfree( res ); |
return ld->ld_errno; |
return ld->ld_errno; |
} |
} |
|
|
ldap_value_free( values ); |
LDAP_VFREE( values ); |
ldap_msgfree( res ); |
ldap_msgfree( res ); |
|
|
*pmechlist = mechlist; |
*pmechlist = mechlist; |
Line 607 int
|
Line 759 int
|
ldap_pvt_sasl_bind( |
ldap_pvt_sasl_bind( |
LDAP *ld, |
LDAP *ld, |
LDAP_CONST char *dn, |
LDAP_CONST char *dn, |
LDAP_CONST char *mechanism, |
LDAP_CONST char *mechs, |
LDAP_CONST sasl_callback_t *callbacks, |
LDAP_CONST sasl_callback_t *callbacks, |
LDAPControl **sctrls, |
LDAPControl **sctrls, |
LDAPControl **cctrls ) |
LDAPControl **cctrls ) |
{ |
{ |
int saslrc, rc, msgid, ssf = 0; |
const char *mech; |
|
int saslrc, rc; |
|
sasl_ssf_t *ssf = NULL; |
|
unsigned credlen; |
struct berval ccred, *scred; |
struct berval ccred, *scred; |
char *mechlist = NULL; |
|
char *host; |
char *host; |
sasl_interact_t *client_interact = NULL; |
sasl_interact_t *client_interact = NULL; |
|
struct sockaddr_in sin; |
|
socklen_t len; |
|
sasl_security_properties_t secprops; |
|
ber_socket_t sd; |
|
|
Debug( LDAP_DEBUG_TRACE, "ldap_pvt_sasl_bind\n", 0, 0, 0 ); |
Debug( LDAP_DEBUG_TRACE, "ldap_pvt_sasl_bind\n", 0, 0, 0 ); |
|
|
Line 626 ldap_pvt_sasl_bind(
|
Line 784 ldap_pvt_sasl_bind(
|
return ld->ld_errno; |
return ld->ld_errno; |
} |
} |
|
|
/* |
ber_sockbuf_ctrl( ld->ld_sb, LBER_SB_OPT_GET_FD, &sd ); |
* This connects to the host, side effect being that |
|
* ldap_host_connected_to() works. |
if ( sd == AC_SOCKET_INVALID ) { |
*/ |
/* not connected yet */ |
rc = ldap_pvt_sasl_getmechs( ld, mechanism, &mechlist ); |
int rc = ldap_open_defconn( ld ); |
if ( rc != LDAP_SUCCESS ) { |
|
return ld->ld_errno; |
if( rc < 0 ) return ld->ld_errno; |
} |
ber_sockbuf_ctrl( ld->ld_sb, LBER_SB_OPT_GET_FD, &sd ); |
|
} |
|
|
/* XXX this doesn't work with PF_LOCAL hosts */ |
/* XXX this doesn't work with PF_LOCAL hosts */ |
host = ldap_host_connected_to( &ld->ld_sb ); |
host = ldap_host_connected_to( ld->ld_sb ); |
|
|
if ( host == NULL ) { |
if ( host == NULL ) { |
LDAP_FREE( mechlist ); |
|
ld->ld_errno = LDAP_UNAVAILABLE; |
ld->ld_errno = LDAP_UNAVAILABLE; |
return ld->ld_errno; |
return ld->ld_errno; |
} |
} |
|
|
if ( ld->ld_sasl_context != NULL ) { |
if ( ld->ld_sasl_context != NULL ) { |
LDAP_FREE( mechlist ); |
|
sasl_dispose( &ld->ld_sasl_context ); |
sasl_dispose( &ld->ld_sasl_context ); |
} |
} |
|
|
saslrc = sasl_client_new( "ldap", host, callbacks, 0, &ld->ld_sasl_context ); |
saslrc = sasl_client_new( "ldap", host, callbacks, SASL_SECURITY_LAYER, |
|
&ld->ld_sasl_context ); |
|
|
LDAP_FREE( host ); |
LDAP_FREE( host ); |
|
|
if ( (saslrc != SASL_OK) && (saslrc != SASL_CONTINUE) ) { |
if ( (saslrc != SASL_OK) && (saslrc != SASL_CONTINUE) ) { |
LDAP_FREE( mechlist ); |
ld->ld_errno = sasl_err2ldap( saslrc ); |
ld->ld_errno = ldap_pvt_sasl_err2ldap( rc ); |
|
sasl_dispose( &ld->ld_sasl_context ); |
sasl_dispose( &ld->ld_sasl_context ); |
return ld->ld_errno; |
return ld->ld_errno; |
} |
} |
|
|
|
len = sizeof( sin ); |
|
if ( getpeername( sd, (struct sockaddr *)&sin, &len ) == -1 ) { |
|
Debug( LDAP_DEBUG_ANY, "SASL: can't query remote IP.\n", |
|
0, 0, 0 ); |
|
ld->ld_errno = LDAP_OPERATIONS_ERROR; |
|
return ld->ld_errno; |
|
} |
|
sasl_setprop( ld->ld_sasl_context, SASL_IP_REMOTE, &sin ); |
|
|
|
len = sizeof( sin ); |
|
if ( getsockname( sd, (struct sockaddr *)&sin, &len ) == -1 ) { |
|
Debug( LDAP_DEBUG_ANY, "SASL: can't query local IP.\n", |
|
0, 0, 0 ); |
|
ld->ld_errno = LDAP_OPERATIONS_ERROR; |
|
return ld->ld_errno; |
|
} |
|
sasl_setprop( ld->ld_sasl_context, SASL_IP_LOCAL, &sin ); |
|
|
|
memset( &secprops, 0, sizeof( secprops ) ); |
|
secprops.min_ssf = ld->ld_options.ldo_sasl_minssf; |
|
secprops.max_ssf = ld->ld_options.ldo_sasl_maxssf; |
|
secprops.security_flags = SASL_SECURITY_LAYER; |
|
secprops.maxbufsize = 65536; |
|
sasl_setprop( ld->ld_sasl_context, SASL_SEC_PROPS, &secprops ); |
|
|
ccred.bv_val = NULL; |
ccred.bv_val = NULL; |
ccred.bv_len = 0; |
ccred.bv_len = 0; |
|
|
saslrc = sasl_client_start( ld->ld_sasl_context, |
saslrc = sasl_client_start( ld->ld_sasl_context, |
mechlist, |
mechs, |
NULL, |
NULL, |
&client_interact, |
&client_interact, |
&ccred.bv_val, |
&ccred.bv_val, |
(unsigned int *)&ccred.bv_len, |
&credlen, |
&mechanism ); |
&mech ); |
|
|
LDAP_FREE( mechlist ); |
ccred.bv_len = credlen; |
|
|
if ( (saslrc != SASL_OK) && (saslrc != SASL_CONTINUE) ) { |
if ( (saslrc != SASL_OK) && (saslrc != SASL_CONTINUE) ) { |
ld->ld_errno = ldap_pvt_sasl_err2ldap( saslrc ); |
ld->ld_errno = sasl_err2ldap( saslrc ); |
sasl_dispose( &ld->ld_sasl_context ); |
sasl_dispose( &ld->ld_sasl_context ); |
return ld->ld_errno; |
return ld->ld_errno; |
} |
} |
Line 682 ldap_pvt_sasl_bind(
|
Line 864 ldap_pvt_sasl_bind(
|
scred = NULL; |
scred = NULL; |
|
|
do { |
do { |
|
unsigned credlen; |
sasl_interact_t *client_interact = NULL; |
sasl_interact_t *client_interact = NULL; |
|
|
rc = ldap_sasl_bind_s( ld, dn, mechanism, &ccred, sctrls, cctrls, &scred ); |
rc = ldap_sasl_bind_s( ld, dn, mech, &ccred, sctrls, cctrls, &scred ); |
if ( rc == LDAP_SUCCESS ) { |
if ( rc == LDAP_SUCCESS ) { |
break; |
break; |
} else if ( rc != LDAP_SASL_BIND_IN_PROGRESS ) { |
} else if ( rc != LDAP_SASL_BIND_IN_PROGRESS ) { |
Line 705 ldap_pvt_sasl_bind(
|
Line 888 ldap_pvt_sasl_bind(
|
(scred == NULL) ? 0 : scred->bv_len, |
(scred == NULL) ? 0 : scred->bv_len, |
&client_interact, |
&client_interact, |
&ccred.bv_val, |
&ccred.bv_val, |
(unsigned int *)&ccred.bv_len ); |
&credlen ); |
|
|
|
ccred.bv_len = credlen; |
ber_bvfree( scred ); |
ber_bvfree( scred ); |
|
|
if ( (saslrc != SASL_OK) && (saslrc != SASL_CONTINUE) ) { |
if ( (saslrc != SASL_OK) && (saslrc != SASL_CONTINUE) ) { |
ld->ld_errno = ldap_pvt_sasl_err2ldap( saslrc ); |
ld->ld_errno = sasl_err2ldap( saslrc ); |
sasl_dispose( &ld->ld_sasl_context ); |
sasl_dispose( &ld->ld_sasl_context ); |
return ld->ld_errno; |
return ld->ld_errno; |
} |
} |
Line 719 ldap_pvt_sasl_bind(
|
Line 903 ldap_pvt_sasl_bind(
|
assert ( rc == LDAP_SUCCESS ); |
assert ( rc == LDAP_SUCCESS ); |
|
|
if ( sasl_getprop( ld->ld_sasl_context, SASL_SSF, (void **)&ssf ) |
if ( sasl_getprop( ld->ld_sasl_context, SASL_SSF, (void **)&ssf ) |
== SASL_OK && ssf ) { |
== SASL_OK && ssf && *ssf ) { |
ldap_pvt_sasl_install( &ld->ld_sb, ld->ld_sasl_context ); |
ldap_pvt_sasl_install( ld->ld_sb, ld->ld_sasl_context ); |
} |
} |
|
|
return rc; |
return rc; |
Line 728 ldap_pvt_sasl_bind(
|
Line 912 ldap_pvt_sasl_bind(
|
|
|
/* based on sample/sample-client.c */ |
/* based on sample/sample-client.c */ |
static int |
static int |
ldap_pvt_sasl_getsecret(sasl_conn_t *conn, void *context, int id, sasl_secret_t **psecret) |
ldap_pvt_sasl_getsecret(sasl_conn_t *conn, |
|
void *context, int id, sasl_secret_t **psecret) |
{ |
{ |
struct berval *passphrase = (struct berval *)context; |
struct berval *passphrase = (struct berval *)context; |
size_t len; |
size_t len; |
Line 781 ldap_pvt_sasl_getsimple(void *context, i
|
Line 966 ldap_pvt_sasl_getsimple(void *context, i
|
return SASL_OK; |
return SASL_OK; |
} |
} |
|
|
|
int |
|
ldap_pvt_sasl_get_option( LDAP *ld, int option, void *arg ) |
|
{ |
|
sasl_ssf_t *ssf; |
|
|
|
if ( ld == NULL ) |
|
return -1; |
|
|
|
switch ( option ) { |
|
case LDAP_OPT_X_SASL_MINSSF: |
|
*(int *)arg = ld->ld_options.ldo_sasl_minssf; |
|
break; |
|
case LDAP_OPT_X_SASL_MAXSSF: |
|
*(int *)arg = ld->ld_options.ldo_sasl_maxssf; |
|
break; |
|
case LDAP_OPT_X_SASL_ACTSSF: |
|
if ( ld->ld_sasl_context == NULL ) { |
|
*(int *)arg = -1; |
|
break; |
|
} |
|
if ( sasl_getprop( ld->ld_sasl_context, SASL_SSF, |
|
(void **) &ssf ) != SASL_OK ) |
|
{ |
|
return -1; |
|
} |
|
*(int *)arg = *ssf; |
|
break; |
|
default: |
|
return -1; |
|
} |
|
return 0; |
|
} |
|
|
|
int |
|
ldap_pvt_sasl_set_option( LDAP *ld, int option, void *arg ) |
|
{ |
|
if ( ld == NULL ) |
|
return -1; |
|
|
|
switch ( option ) { |
|
case LDAP_OPT_X_SASL_MINSSF: |
|
ld->ld_options.ldo_sasl_minssf = *(int *)arg; |
|
break; |
|
case LDAP_OPT_X_SASL_MAXSSF: |
|
ld->ld_options.ldo_sasl_maxssf = *(int *)arg; |
|
break; |
|
case LDAP_OPT_X_SASL_ACTSSF: |
|
/* This option is read-only */ |
|
default: |
|
return -1; |
|
} |
|
return 0; |
|
} |
|
|
/* |
/* |
* ldap_negotiated_sasl_bind_s - bind to the ldap server (and X.500) using SASL |
* ldap_negotiated_sasl_bind_s - bind to the ldap server (and X.500) |
* authentication. The dn and password of the entry to which to bind are |
* using SASL authentication. |
* supplied. LDAP_SUCCESS is returned upon success, the ldap error code |
* |
|
* This routine attempts to authenticate the user referred by the |
|
* authentication id using the provided password. An optional |
|
* authorization identity may be provided. An DN is generally not |
|
* provided [see AuthMethod]. |
|
* |
|
* If the mechanism negotiated does not require a password, the |
|
* passwd field is ignored. [A callback mechanism should really |
|
* be used]. |
|
* |
|
* LDAP_SUCCESS is returned upon success, the ldap error code |
* otherwise. |
* otherwise. |
* |
* |
* Example: |
* Examples: |
|
* ldap_negotiated_sasl_bind_s( ld, NULL, |
|
* NULL, NULL, NULL, |
|
* NULL, NULL, NULL, NULL ); |
|
* |
|
* ldap_negotiated_sasl_bind_s( ld, NULL, |
|
* "user@OPENLDAP.ORG", NULL, NULL, |
|
* "GSSAPI", NULL, NULL, NULL ); |
|
* |
* ldap_negotiated_sasl_bind_s( ld, NULL, |
* ldap_negotiated_sasl_bind_s( ld, NULL, |
* "dn:cn=manager", NULL, "GSSAPI", NULL, NULL, NULL ); |
* "manager", "dn:cn=user,dc=openldap,dc=org", NULL, |
|
* "DIGEST-MD5", NULL, NULL, NULL ); |
|
* |
|
* ldap_negotiated_sasl_bind_s( ld, NULL, |
|
* "root@OPENLDAP.ORG", "u:user@OPENLDAP.ORG", NULL, |
|
* "GSSAPI", NULL, NULL, NULL ); |
|
* |
|
* ldap_negotiated_sasl_bind_s( ld, NULL, |
|
* "manager", "dn:cn=user,dc=openldap,dc=org", NULL, |
|
* "DIGEST-MD5", NULL, NULL, NULL ); |
*/ |
*/ |
int |
int |
ldap_negotiated_sasl_bind_s( |
ldap_negotiated_sasl_bind_s( |
LDAP *ld, |
LDAP *ld, |
LDAP_CONST char *dn, /* usually NULL */ |
LDAP_CONST char *dn, /* usually NULL */ |
LDAP_CONST char *authorizationId, |
LDAP_CONST char *authenticationId, |
LDAP_CONST char *authenticationId, |
LDAP_CONST char *authorizationId, /* commonly NULL */ |
LDAP_CONST char *saslMechanism, |
LDAP_CONST char *saslMechanism, |
struct berval *passPhrase, |
struct berval *passPhrase, |
LDAPControl **serverControls, |
LDAPControl **serverControls, |
LDAPControl **clientControls) |
LDAPControl **clientControls) |
{ |
{ |
|
int n; |
sasl_callback_t callbacks[4]; |
sasl_callback_t callbacks[4]; |
int rc; |
int rc; |
|
|
callbacks[0].id = SASL_CB_USER; |
Debug( LDAP_DEBUG_TRACE, "ldap_negotiated_sasl_bind_s\n", 0, 0, 0 ); |
callbacks[0].proc = ldap_pvt_sasl_getsimple; |
|
callbacks[0].context = (void *)authorizationId; |
if( saslMechanism == NULL || *saslMechanism == '\0' ) { |
callbacks[1].id = SASL_CB_AUTHNAME; |
char *mechs; |
callbacks[1].proc = ldap_pvt_sasl_getsimple; |
rc = ldap_pvt_sasl_getmechs( ld, &mechs ); |
callbacks[1].context = (void *)authenticationId; |
|
callbacks[2].id = SASL_CB_PASS; |
if( rc != LDAP_SUCCESS ) { |
callbacks[2].proc = ldap_pvt_sasl_getsecret; |
return rc; |
callbacks[2].context = (void *)passPhrase; |
} |
callbacks[3].id = SASL_CB_LIST_END; |
|
callbacks[3].proc = NULL; |
saslMechanism = mechs; |
callbacks[3].context = NULL; |
} |
|
|
|
/* SASL Authentication Identity */ |
|
callbacks[n=0].id = SASL_CB_AUTHNAME; |
|
callbacks[n].proc = ldap_pvt_sasl_getsimple; |
|
callbacks[n].context = (void *)authenticationId; |
|
|
|
/* SASL Authorization Identity (userid) */ |
|
if( authorizationId != NULL ) { |
|
callbacks[++n].id = SASL_CB_USER; |
|
callbacks[n].proc = ldap_pvt_sasl_getsimple; |
|
callbacks[n].context = (void *)authorizationId; |
|
} |
|
|
|
callbacks[++n].id = SASL_CB_PASS; |
|
callbacks[n].proc = ldap_pvt_sasl_getsecret; |
|
callbacks[n].context = (void *)passPhrase; |
|
|
|
callbacks[++n].id = SASL_CB_LIST_END; |
|
callbacks[n].proc = NULL; |
|
callbacks[n].context = NULL; |
|
|
|
assert( n * sizeof(sasl_callback_t) < sizeof(callbacks) ); |
|
|
rc = ldap_pvt_sasl_bind(ld, dn, saslMechanism, callbacks, serverControls, clientControls); |
rc = ldap_pvt_sasl_bind(ld, dn, saslMechanism, callbacks, |
|
serverControls, clientControls); |
|
|
return rc; |
return rc; |
} |
} |