Diff for /libraries/libldap/sasl.c between versions 1.1.4.2 and 1.9

version 1.1.4.2, 2000/06/13 17:57:20 version 1.9, 2000/01/16 00:29:15
Line 1 Line 1
 /* $OpenLDAP: pkg/ldap/libraries/libldap/sasl.c,v 1.26 2000/06/10 20:39:31 kurt Exp $ */  /* $OpenLDAP: pkg/ldap/libraries/libldap/sasl.c,v 1.8 2000/01/08 17:42:18 kdz Exp $ */
 /*  /*
  * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.   * Copyright 1998-1999 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).   * ldap_sasl_bind - bind to the ldap server (and X.500).  The dn, mechanism, and
  * The dn (usually NULL), mechanism, and credentials are provided.   * credentials of the entry to which to bind are supplied.  The message id
  * The message id of the request initiated is provided upon successful   * of the request initiated is provided upon successful (LDAP_SUCCESS) return.
  * (LDAP_SUCCESS) return.  
  *   *
  * Example:   * Example:
  *      ldap_sasl_bind( ld, NULL, "mechanism",   *      ldap_sasl_bind( ld, "cn=manager, o=university of michigan, c=us",
  *              cred, NULL, NULL, &msgid )   *          "mechanism", "secret", NULL, NULL, &msgid )
  */   */
   
 int  int
Line 192  ldap_sasl_bind_s( Line 190  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 && rc != LDAP_SASL_BIND_IN_PROGRESS ) {          if( rc != LDAP_SUCCESS ) {
                 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 || rc == LDAP_SASL_BIND_IN_PROGRESS ) {          if( rc == LDAP_SUCCESS ) {
                 if( servercredp != NULL ) {                  if( servercredp != NULL ) {
                         *servercredp = scredp;                          *servercredp = scredp;
                         scredp = NULL;  
                 }                  }
         }  
   
         if ( scredp != NULL ) {          } else if (scredp != NULL ) {
                 ber_bvfree(scredp);                  ber_bvfree(scredp);
         }          }
   
Line 350  ldap_parse_sasl_bind_result( Line 346  ldap_parse_sasl_bind_result(
 * Various Cyrus SASL related stuff.  * Various Cyrus SASL related stuff.
 */  */
   
 #define MAX_BUFF_SIZE   65536  static int sasl_setup( Sockbuf *sb, void *arg );
 #define MIN_BUFF_SIZE   4096  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)
   
 static char *  static char *
 array2str( char **a )  array2str( char **a )
Line 411  int ldap_pvt_sasl_init( void ) Line 421  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 )  
 {  {
         struct sb_sasl_data     *p;          /* don't install the stuff unless security has been negotiated */
   
         assert( sbiod != NULL );  
   
         p = LBER_MALLOC( sizeof( *p ) );          if ( !HAS_SASL( sb ) ) {
         if ( p == NULL )                  ber_pvt_sb_clear_io( sb );
                 return -1;                  ber_pvt_sb_set_io( sb, &sasl_io, ctx_arg );
         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  static int sasl_setup( Sockbuf *sb, void *arg )
 sb_sasl_remove( Sockbuf_IO_Desc *sbiod )  
 {  {
         struct sb_sasl_data     *p;          sb->sb_iodata = arg;
   
         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 ber_len_t  static int sasl_remove( Sockbuf *sb )
 sb_sasl_pkt_length( const char *buf, int debuglevel )  
 {  {
         ber_len_t               size;          return 0;
         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 !!! */  
 }  
   
 /* Drop a processed packet from the input buffer */  
 static void  
 sb_sasl_drop_packet ( Sockbuf_Buf *sec_buf_in, int debuglevel )  
 {  
         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;  
 }  }
   
 static ber_slen_t  static ber_slen_t sasl_read( Sockbuf *sb, void *buf, ber_len_t buflen )
 sb_sasl_read( Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len)  
 {  {
         struct sb_sasl_data     *p;          char *recv_tok;
         ber_slen_t              ret, bufptr;          unsigned recv_tok_len;
              sasl_conn_t *conn = (sasl_conn_t *)sb->sb_iodata;
         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 ((ber_pvt_sb_io_tcp.sbi_read)( sb, buf, buflen ) != buflen ) {
                   return -1;
         }          }
   
         /* The new packet always starts at p->sec_buf_in.buf_base */          if ( sasl_decode( conn, buf, buflen, &recv_tok, &recv_tok_len ) != SASL_OK ) {
         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;  
   
                 p->sec_buf_in.buf_ptr += ret;          if ( recv_tok_len > buflen ) {
         }                  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 );  
   
         p->buf_in.buf_size = p->buf_in.buf_end;          memcpy( buf, recv_tok, recv_tok_len );  
   
         bufptr += ber_pvt_sb_copy_out( &p->buf_in, (char*) buf + bufptr, len );          LDAP_FREE( recv_tok );
   
         return bufptr;          return recv_tok_len;
 }  }
   
 static ber_slen_t  static ber_slen_t sasl_write( Sockbuf *sb, void *buf, ber_len_t len )
 sb_sasl_write( Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len)  
 {  {
         struct sb_sasl_data     *p;          char *wrapped_tok;
         int                     ret;          unsigned wrapped_tok_len;
           sasl_conn_t *conn = (sasl_conn_t *)sb->sb_iodata;
         assert( sbiod != NULL );  
         assert( SOCKBUF_VALID( sbiod->sbiod_sb ) );  
   
         p = (struct sb_sasl_data *)sbiod->sbiod_pvt;          if ( sasl_encode( conn, (const char *)buf, len,
                   &wrapped_tok, &wrapped_tok_len ) != SASL_OK ) {
         /* 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;  
         }  
   
         /* 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;  
   
         ret = ber_pvt_sb_do_write( sbiod, &p->buf_out );  
         if ( ret <= 0 )  
                 return ret;  
         return len;  
 }  
   
 static int          if ((ber_pvt_sb_io_tcp.sbi_write)( sb, wrapped_tok, wrapped_tok_len ) != wrapped_tok_len ) {
 sb_sasl_ctrl( Sockbuf_IO_Desc *sbiod, int opt, void *arg )                  LDAP_FREE( wrapped_tok );
 {                  return -1;
         struct sb_sasl_data     *p;          }
   
         p = (struct sb_sasl_data *)sbiod->sbiod_pvt;          LDAP_FREE( wrapped_tok );
   
         if ( opt == LBER_SB_OPT_DATA_READY ) {          return len;
                 if ( p->buf_in.buf_ptr != p->buf_in.buf_end )  
                         return 1;  
         }  
           
         return LBER_SBIOD_CTRL_NEXT( sbiod, opt, arg );  
 }  }
   
 Sockbuf_IO ldap_pvt_sockbuf_io_sasl =  static int sasl_close( Sockbuf *sb )
 {  
         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 */          (ber_pvt_sb_io_tcp.sbi_close)( sb );
   
         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  int
 sasl_err2ldap( int saslerr )  ldap_pvt_sasl_err2ldap( int saslerr )
 {  {
         int rc;          int rc;
   
         switch (saslerr) {          switch (saslerr) {
                 case SASL_CONTINUE:                  case SASL_CONTINUE:
                         rc = LDAP_MORE_RESULTS_TO_RETURN;                          rc = LDAP_SASL_BIND_IN_PROGRESS;
                         break;                          break;
                 case SASL_OK:                  case SASL_OK:
                         rc = LDAP_SUCCESS;                          rc = LDAP_SUCCESS;
                         break;                          break;
                 case SASL_FAIL:                  case SASL_FAIL:
                         rc = LDAP_LOCAL_ERROR;                          rc = LDAP_OPERATIONS_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_UNKNOWN;                          rc = LDAP_AUTH_METHOD_NOT_SUPPORTED;
                         break;                          break;
                 case SASL_BADAUTH:                  case SASL_BADAUTH:
                         rc = LDAP_AUTH_UNKNOWN;                          rc = LDAP_INVALID_CREDENTIALS;
                         break;                          break;
                 case SASL_NOAUTHZ:                  case SASL_NOAUTHZ:
                         rc = LDAP_PARAM_ERROR;                          rc = LDAP_INSUFFICIENT_ACCESS;
                         break;                          break;
                 case SASL_TOOWEAK:                  case SASL_TOOWEAK:
                 case SASL_ENCRYPT:                  case SASL_ENCRYPT:
                         rc = LDAP_AUTH_UNKNOWN;                          rc = LDAP_INAPPROPRIATE_AUTH;
                         break;                          break;
                 default:                  default:
                         rc = LDAP_LOCAL_ERROR;                          rc = LDAP_OPERATIONS_ERROR;
                         break;                          break;
         }          }
   
         assert( rc == LDAP_SUCCESS || LDAP_API_ERROR( rc ) );  
         return rc;          return rc;
 }  }
   
 int  int
 ldap_pvt_sasl_getmechs ( LDAP *ld, char **pmechlist )  ldap_pvt_sasl_getmechs ( LDAP *ld, LDAP_CONST char *desired, 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;          char **values, *mechlist, **p;
         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,
                 NULL, attrs, 0, &res );                  "(objectclass=*)", attrs, 0, &res );
   
         if ( rc != LDAP_SUCCESS ) {          if ( rc != LDAP_SUCCESS ) {
                 return ld->ld_errno;                  return ld->ld_errno;
Line 739  ldap_pvt_sasl_getmechs ( LDAP *ld, char Line 566  ldap_pvt_sasl_getmechs ( LDAP *ld, char
                 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_VFREE( values );                  ldap_value_free( values );
                 ldap_msgfree( res );                  ldap_msgfree( res );
                 return ld->ld_errno;                  return ld->ld_errno;
         }           } 
   
         LDAP_VFREE( values );          ldap_value_free( values );
         ldap_msgfree( res );          ldap_msgfree( res );
   
         *pmechlist = mechlist;          *pmechlist = mechlist;
Line 759  int Line 607  int
 ldap_pvt_sasl_bind(  ldap_pvt_sasl_bind(
         LDAP                    *ld,          LDAP                    *ld,
         LDAP_CONST char         *dn,          LDAP_CONST char         *dn,
         LDAP_CONST char         *mechs,          LDAP_CONST char         *mechanism,
         LDAP_CONST sasl_callback_t      *callbacks,          LDAP_CONST sasl_callback_t      *callbacks,
         LDAPControl             **sctrls,          LDAPControl             **sctrls,
         LDAPControl             **cctrls )          LDAPControl             **cctrls )
 {  {
         const char *mech;          int     saslrc, rc, msgid, ssf = 0;
         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 784  ldap_pvt_sasl_bind( Line 626  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
         if ( sd == AC_SOCKET_INVALID ) {           * ldap_host_connected_to() works.
                 /* not connected yet */           */
                 int rc = ldap_open_defconn( ld );          rc = ldap_pvt_sasl_getmechs( ld, mechanism, &mechlist );
             if ( rc != LDAP_SUCCESS ) {
                 if( rc < 0 ) return ld->ld_errno;                  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, SASL_SECURITY_LAYER,          saslrc = sasl_client_new( "ldap", host, callbacks, 0, &ld->ld_sasl_context );
                 &ld->ld_sasl_context );  
   
         LDAP_FREE( host );          LDAP_FREE( host );
   
         if ( (saslrc != SASL_OK) && (saslrc != SASL_CONTINUE) ) {          if ( (saslrc != SASL_OK) && (saslrc != SASL_CONTINUE) ) {
                 ld->ld_errno = sasl_err2ldap( saslrc );                  LDAP_FREE( mechlist );
                   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,
                 mechs,                  mechlist,
                 NULL,                  NULL,
                 &client_interact,                  &client_interact,
                 &ccred.bv_val,                  &ccred.bv_val,
                 &credlen,                  (unsigned int *)&ccred.bv_len,
                 &mech );                  &mechanism );
   
         ccred.bv_len = credlen;          LDAP_FREE( mechlist );
   
         if ( (saslrc != SASL_OK) && (saslrc != SASL_CONTINUE) ) {          if ( (saslrc != SASL_OK) && (saslrc != SASL_CONTINUE) ) {
                 ld->ld_errno = sasl_err2ldap( saslrc );                  ld->ld_errno = ldap_pvt_sasl_err2ldap( saslrc );
                 sasl_dispose( &ld->ld_sasl_context );                  sasl_dispose( &ld->ld_sasl_context );
                 return ld->ld_errno;                  return ld->ld_errno;
         }          }
Line 864  ldap_pvt_sasl_bind( Line 682  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, mech, &ccred, sctrls, cctrls, &scred );                  rc = ldap_sasl_bind_s( ld, dn, mechanism, &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 888  ldap_pvt_sasl_bind( Line 705  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,
                         &credlen );                          (unsigned int *)&ccred.bv_len );
   
                 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 = sasl_err2ldap( saslrc );                          ld->ld_errno = ldap_pvt_sasl_err2ldap( saslrc );
                         sasl_dispose( &ld->ld_sasl_context );                          sasl_dispose( &ld->ld_sasl_context );
                         return ld->ld_errno;                          return ld->ld_errno;
                 }                  }
Line 903  ldap_pvt_sasl_bind( Line 719  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 && *ssf ) {                  == SASL_OK && 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 912  ldap_pvt_sasl_bind( Line 728  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,  ldap_pvt_sasl_getsecret(sasl_conn_t *conn, void *context, int id, sasl_secret_t **psecret)
         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 966  ldap_pvt_sasl_getsimple(void *context, i Line 781  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)   * ldap_negotiated_sasl_bind_s - bind to the ldap server (and X.500) using SASL
  * using SASL authentication.   * authentication.  The dn and password of the entry to which to bind are
  *   * 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.
  *   *
  * Examples:   * Example:
  *      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,   *      ldap_negotiated_sasl_bind_s( ld, NULL,
  *          "manager", "dn:cn=user,dc=openldap,dc=org", NULL,   *          "dn:cn=manager", NULL, "GSSAPI", NULL, NULL, 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 *authenticationId,          LDAP_CONST char *authorizationId,
         LDAP_CONST char *authorizationId, /* commonly NULL */          LDAP_CONST char *authenticationId,  
         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;
   
         Debug( LDAP_DEBUG_TRACE, "ldap_negotiated_sasl_bind_s\n", 0, 0, 0 );          callbacks[0].id = SASL_CB_USER;
           callbacks[0].proc = ldap_pvt_sasl_getsimple;
         if( saslMechanism == NULL || *saslMechanism == '\0' ) {          callbacks[0].context = (void *)authorizationId;
                 char *mechs;          callbacks[1].id = SASL_CB_AUTHNAME;
                 rc = ldap_pvt_sasl_getmechs( ld, &mechs );          callbacks[1].proc = ldap_pvt_sasl_getsimple;
           callbacks[1].context = (void *)authenticationId;
                 if( rc != LDAP_SUCCESS ) {          callbacks[2].id = SASL_CB_PASS;
                         return rc;          callbacks[2].proc = ldap_pvt_sasl_getsecret;
                 }          callbacks[2].context = (void *)passPhrase;
           callbacks[3].id = SASL_CB_LIST_END;
                 saslMechanism = mechs;          callbacks[3].proc = NULL;
         }          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,          rc = ldap_pvt_sasl_bind(ld, dn, saslMechanism, callbacks, serverControls, clientControls);
                 serverControls, clientControls);  
   
         return rc;          return rc;
 }  }

Removed from v.1.1.4.2  
changed lines
  Added in v.1.9


______________
© Copyright 1998-2020, OpenLDAP Foundation, info@OpenLDAP.org