]> git.sur5r.net Git - openldap/blobdiff - libraries/libldap/sasl.c
Fix up NT port after thread changes
[openldap] / libraries / libldap / sasl.c
index 030ff976e6e93ef221235302223f9aacb086a79a..1243454d76a92aaff48ebfa9714afb133f48c465 100644 (file)
@@ -1,6 +1,6 @@
 /* $OpenLDAP$ */
 /*
- * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
+ * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
  */
 
 #include <ac/socket.h>
 #include <ac/string.h>
 #include <ac/time.h>
+#include <ac/errno.h>
 
 #include "ldap-int.h"
 
 
 /*
- * ldap_sasl_bind - bind to the ldap server (and X.500).  The dn, mechanism, and
- * credentials of the entry to which to bind are supplied.  The message id
- * of the request initiated is provided upon successful (LDAP_SUCCESS) return.
+ * ldap_sasl_bind - bind to the ldap server (and X.500).
+ * The dn (usually NULL), mechanism, and credentials are provided.
+ * The message id of the request initiated is provided upon successful
+ * (LDAP_SUCCESS) return.
  *
  * Example:
- *     ldap_sasl_bind( ld, "cn=manager, o=university of michigan, c=us",
- *         "mechanism", "secret", NULL, NULL, &msgid )
+ *     ldap_sasl_bind( ld, NULL, "mechanism",
+ *             cred, NULL, NULL, &msgid )
  */
 
 int
@@ -190,19 +192,21 @@ ldap_sasl_bind_s(
                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 );
                return( rc );
        }
 
        rc = ldap_result2error( ld, result, 1 );
 
-       if( rc == LDAP_SUCCESS ) {
+       if ( rc == LDAP_SUCCESS || rc == LDAP_SASL_BIND_IN_PROGRESS ) {
                if( servercredp != NULL ) {
                        *servercredp = scredp;
+                       scredp = NULL;
                }
+       }
 
-       } else if (scredp != NULL ) {
+       if ( scredp != NULL ) {
                ber_bvfree(scredp);
        }
 
@@ -346,22 +350,8 @@ ldap_parse_sasl_bind_result(
 * Various Cyrus SASL related stuff.
 */
 
-static int sasl_setup( Sockbuf *sb, void *arg );
-static int sasl_remove( Sockbuf *sb );
-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)
+#define MAX_BUFF_SIZE  65536
+#define MIN_BUFF_SIZE  4096
 
 static char *
 array2str( char **a )
@@ -407,7 +397,7 @@ int ldap_pvt_sasl_init( void )
        static int sasl_initialized = 0;
 
        if ( sasl_initialized ) {
-               return -1;
+               return 0;
        }
 #ifndef CSRIMALLOC
        sasl_set_alloc( ber_memalloc, ber_memcalloc, ber_memrealloc, ber_memfree );
@@ -421,131 +411,314 @@ int ldap_pvt_sasl_init( void )
        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 ) ) {
-               ber_pvt_sb_clear_io( sb );
-               ber_pvt_sb_set_io( sb, &sasl_io, ctx_arg );
+       p = LBER_MALLOC( sizeof( *p ) );
+       if ( p == NULL )
+               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;
 }
 
-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;
 }
 
-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;
-       unsigned recv_tok_len;
-       sasl_conn_t *conn = (sasl_conn_t *)sb->sb_iodata;
+       ber_slen_t                      len;
+
+       len = sec_buf_in->buf_ptr - sec_buf_in->buf_end;
+       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 ((ber_pvt_sb_io_tcp.sbi_read)( sb, buf, buflen ) != buflen ) {
-               return -1;
+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;
        }
 
-       if ( sasl_decode( conn, buf, buflen, &recv_tok, &recv_tok_len ) != SASL_OK ) {
+       /* 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;
        }
-
-       if ( recv_tok_len > buflen ) {
-               LDAP_FREE( recv_tok );
+       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;
+
+               p->sec_buf_in.buf_ptr += ret;
+       }
+
+       /* 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;
        }
+       
+       /* 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;
-       unsigned wrapped_tok_len;
-       sasl_conn_t *conn = (sasl_conn_t *)sb->sb_iodata;
+       struct sb_sasl_data     *p;
+       int                     ret;
 
-       if ( sasl_encode( conn, (const char *)buf, len,
-               &wrapped_tok, &wrapped_tok_len ) != SASL_OK ) {
-               return -1;
+       assert( sbiod != NULL );
+       assert( SOCKBUF_VALID( sbiod->sbiod_sb ) );
+
+       p = (struct sb_sasl_data *)sbiod->sbiod_pvt;
+
+       /* Are there anything left in the buffer? */
+       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;
        }
 
-       if ((ber_pvt_sb_io_tcp.sbi_write)( sb, wrapped_tok, wrapped_tok_len ) != wrapped_tok_len ) {
-               LDAP_FREE( wrapped_tok );
+       /* 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;
        }
+       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;
 }
 
-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
-ldap_pvt_sasl_err2ldap( int saslerr )
+Sockbuf_IO ldap_pvt_sockbuf_io_sasl =
+{
+       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;
 
        switch (saslerr) {
                case SASL_CONTINUE:
-                       rc = LDAP_SASL_BIND_IN_PROGRESS;
+                       rc = LDAP_MORE_RESULTS_TO_RETURN;
                        break;
                case SASL_OK:
                        rc = LDAP_SUCCESS;
                        break;
                case SASL_FAIL:
-                       rc = LDAP_OPERATIONS_ERROR;
+                       rc = LDAP_LOCAL_ERROR;
                        break;
                case SASL_NOMEM:
                        rc = LDAP_NO_MEMORY;
                        break;
                case SASL_NOMECH:
-                       rc = LDAP_AUTH_METHOD_NOT_SUPPORTED;
+                       rc = LDAP_AUTH_UNKNOWN;
                        break;
                case SASL_BADAUTH:
-                       rc = LDAP_INVALID_CREDENTIALS;
+                       rc = LDAP_AUTH_UNKNOWN;
                        break;
                case SASL_NOAUTHZ:
-                       rc = LDAP_INSUFFICIENT_ACCESS;
+                       rc = LDAP_PARAM_ERROR;
                        break;
                case SASL_TOOWEAK:
                case SASL_ENCRYPT:
-                       rc = LDAP_INAPPROPRIATE_AUTH;
+                       rc = LDAP_AUTH_UNKNOWN;
                        break;
                default:
-                       rc = LDAP_OPERATIONS_ERROR;
+                       rc = LDAP_LOCAL_ERROR;
                        break;
        }
 
+       assert( rc == LDAP_SUCCESS || LDAP_API_ERROR( rc ) );
        return rc;
 }
 
 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 */
        LDAPMessage *res, *e;
        char *attrs[] = { "supportedSASLMechanisms", NULL };
-       char **values, *mechlist, **p;
+       char **values, *mechlist;
        int rc;
 
+       Debug( LDAP_DEBUG_TRACE, "ldap_pvt_sasl_getmech\n", 0, 0, 0 );
+
        rc = ldap_search_s( ld, NULL, LDAP_SCOPE_BASE,
-               "(objectclass=*)", attrs, 0, &res );
+               NULL, attrs, 0, &res );
 
        if ( rc != LDAP_SUCCESS ) {
                return ld->ld_errno;
@@ -566,36 +739,15 @@ ldap_pvt_sasl_getmechs ( LDAP *ld, LDAP_CONST char *desired, char **pmechlist )
                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 );
        if ( mechlist == NULL ) {
                ld->ld_errno = LDAP_NO_MEMORY;
-               ldap_value_free( values );
+               LDAP_VFREE( values );
                ldap_msgfree( res );
                return ld->ld_errno;
        } 
 
-       ldap_value_free( values );
+       LDAP_VFREE( values );
        ldap_msgfree( res );
 
        *pmechlist = mechlist;
@@ -607,16 +759,22 @@ int
 ldap_pvt_sasl_bind(
        LDAP                    *ld,
        LDAP_CONST char         *dn,
-       LDAP_CONST char         *mechanism,
+       LDAP_CONST char         *mechs,
        LDAP_CONST sasl_callback_t      *callbacks,
        LDAPControl             **sctrls,
        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;
-       char *mechlist = NULL;
        char *host;
        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 );
 
@@ -626,55 +784,79 @@ ldap_pvt_sasl_bind(
                return ld->ld_errno;
        }
 
-       /*
-        * This connects to the host, side effect being that
-        * ldap_host_connected_to() works.
-        */
-       rc = ldap_pvt_sasl_getmechs( ld, mechanism, &mechlist );
-       if ( rc != LDAP_SUCCESS ) {
-               return ld->ld_errno;
-       }
+       ber_sockbuf_ctrl( ld->ld_sb, LBER_SB_OPT_GET_FD, &sd );
+
+       if ( sd == AC_SOCKET_INVALID ) {
+               /* not connected yet */
+               int rc = ldap_open_defconn( ld );
+  
+               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 */
-       host = ldap_host_connected_to( &ld->ld_sb );
+       host = ldap_host_connected_to( ld->ld_sb );
 
        if ( host == NULL ) {
-               LDAP_FREE( mechlist );
                ld->ld_errno = LDAP_UNAVAILABLE;
                return ld->ld_errno;
        }
 
        if ( ld->ld_sasl_context != NULL ) {
-               LDAP_FREE( mechlist );
                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 );
 
        if ( (saslrc != SASL_OK) && (saslrc != SASL_CONTINUE) ) {
-               LDAP_FREE( mechlist );
-               ld->ld_errno = ldap_pvt_sasl_err2ldap( rc );
+               ld->ld_errno = sasl_err2ldap( saslrc );
                sasl_dispose( &ld->ld_sasl_context );
                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_len = 0;
 
        saslrc = sasl_client_start( ld->ld_sasl_context,
-               mechlist,
+               mechs,
                NULL,
                &client_interact,
                &ccred.bv_val,
-               (unsigned int *)&ccred.bv_len,
-               &mechanism );
+               &credlen,
+               &mech );
 
-       LDAP_FREE( mechlist );
+       ccred.bv_len = credlen;
 
        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 );
                return ld->ld_errno;
        }
@@ -682,9 +864,10 @@ ldap_pvt_sasl_bind(
        scred = NULL;
 
        do {
+               unsigned credlen;
                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 ) {
                        break;
                } else if ( rc != LDAP_SASL_BIND_IN_PROGRESS ) {
@@ -705,12 +888,13 @@ ldap_pvt_sasl_bind(
                        (scred == NULL) ? 0 : scred->bv_len,
                        &client_interact,
                        &ccred.bv_val,
-                       (unsigned int *)&ccred.bv_len );
+                       &credlen );
 
+               ccred.bv_len = credlen;
                ber_bvfree( scred );
 
                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 );
                        return ld->ld_errno;
                }
@@ -719,8 +903,8 @@ ldap_pvt_sasl_bind(
        assert ( rc == LDAP_SUCCESS );
 
        if ( sasl_getprop( ld->ld_sasl_context, SASL_SSF, (void **)&ssf )
-               == SASL_OK && ssf ) {
-               ldap_pvt_sasl_install( &ld->ld_sb, ld->ld_sasl_context );
+               == SASL_OK && ssf && *ssf ) {
+               ldap_pvt_sasl_install( ld->ld_sb, ld->ld_sasl_context );
        }
 
        return rc;
@@ -728,7 +912,8 @@ ldap_pvt_sasl_bind(
 
 /* based on sample/sample-client.c */
 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;
        size_t len;           
@@ -781,44 +966,149 @@ ldap_pvt_sasl_getsimple(void *context, int id, const char **result, int *len)
        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
- * authentication.  The dn and password of the entry to which to bind are
- * supplied.  LDAP_SUCCESS is returned upon success, the ldap error code
+ * ldap_negotiated_sasl_bind_s - bind to the ldap server (and X.500)
+ * using SASL authentication.
+ *
+ * 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.
  *
- * 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,
+ *         "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,
- *         "dn:cn=manager", NULL, "GSSAPI", NULL, NULL, NULL );
+ *         "manager", "dn:cn=user,dc=openldap,dc=org", NULL,
+ *             "DIGEST-MD5", NULL, NULL, NULL );
  */
 int
 ldap_negotiated_sasl_bind_s(
-        LDAP *ld,
+       LDAP *ld,
        LDAP_CONST char *dn, /* usually NULL */
-        LDAP_CONST char *authorizationId,
-        LDAP_CONST char *authenticationId,  
-        LDAP_CONST char *saslMechanism,     
-        struct berval *passPhrase,        
-        LDAPControl **serverControls,
-        LDAPControl **clientControls)
+       LDAP_CONST char *authenticationId,
+       LDAP_CONST char *authorizationId, /* commonly NULL */
+       LDAP_CONST char *saslMechanism,
+       struct berval *passPhrase,
+       LDAPControl **serverControls,
+       LDAPControl **clientControls)
 {
+       int n;
        sasl_callback_t callbacks[4];
        int rc;
 
-       callbacks[0].id = SASL_CB_USER;
-       callbacks[0].proc = ldap_pvt_sasl_getsimple;
-       callbacks[0].context = (void *)authorizationId;
-       callbacks[1].id = SASL_CB_AUTHNAME;
-       callbacks[1].proc = ldap_pvt_sasl_getsimple;
-       callbacks[1].context = (void *)authenticationId;
-       callbacks[2].id = SASL_CB_PASS;
-       callbacks[2].proc = ldap_pvt_sasl_getsecret;
-       callbacks[2].context = (void *)passPhrase;
-       callbacks[3].id = SASL_CB_LIST_END;
-       callbacks[3].proc = NULL;
-       callbacks[3].context = NULL;
-
-       rc = ldap_pvt_sasl_bind(ld, dn, saslMechanism, callbacks, serverControls, clientControls);
+       Debug( LDAP_DEBUG_TRACE, "ldap_negotiated_sasl_bind_s\n", 0, 0, 0 );
+
+       if( saslMechanism == NULL || *saslMechanism == '\0' ) {
+               char *mechs;
+               rc = ldap_pvt_sasl_getmechs( ld, &mechs );
+
+               if( rc != LDAP_SUCCESS ) {
+                       return rc;
+               }
+
+               saslMechanism = mechs;
+       }
+
+       /* 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);
 
        return rc;
 }