]> git.sur5r.net Git - openldap/blobdiff - servers/slurpd/ldap_op.c
ITS#4384 rework entryCSN handling, retrieve from incoming request
[openldap] / servers / slurpd / ldap_op.c
index 08253921cd891a768349e7c94b8242acc29f85bf..2f5f870f03070fff844ae9c4f36505137b5f9c34 100644 (file)
@@ -1,5 +1,19 @@
-/*
- * Copyright (c) 1996 Regents of the University of Michigan.
+/* $OpenLDAP$ */
+/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
+ *
+ * Copyright 1998-2006 The OpenLDAP Foundation.
+ * Portions Copyright 2003 Mark Benson.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted only as authorized by the OpenLDAP
+ * Public License.
+ *
+ * A copy of this license is available in file LICENSE in the
+ * top-level directory of the distribution or, alternatively, at
+ * <http://www.OpenLDAP.org/license.html>.
+ */
+/* Portions Copyright (c) 1996 Regents of the University of Michigan.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms are permitted
  * software without specific prior written permission. This software
  * is provided ``as is'' without express or implied warranty.
  */
+/* ACKNOWLEDGEMENTS:
+ * This work was originally developed by the University of Michigan
+ * (as part of U-MICH LDAP).  Additional significant contributors
+ * include:
+ *     Mark Benson
+ */
 
 /*
  * ldap_op.c - routines to perform LDAP operations
  */
 
+#include "portable.h"
+
 #include <stdio.h>
-#include <string.h>
-#include <sys/types.h>
-#include <sys/time.h>
 
-#ifdef KERBEROS
-#include <krb.h>
-#endif /* KERBEROS */
+#include <ac/stdlib.h>
 
-#include <lber.h>
-#include <ldap.h>
+#include <ac/errno.h>
+#include <ac/string.h>
+#include <ac/ctype.h>
+#include <ac/time.h>
+#include <ac/unistd.h>
 
-#include "portable.h"
+#define LDAP_DEPRECATED 1
+#include <ldap.h>
+#include "lutil_ldap.h"
 #include "slurp.h"
 
 /* Forward references */
-static int get_changetype( char * );
-static struct berval **make_singlevalued_berval( char  *, int );
-static int op_ldap_add( Ri *, Re *, char ** );
-static int op_ldap_modify( Ri *, Re *, char ** );
-static int op_ldap_delete( Ri *, Re *, char ** );
-static int op_ldap_modrdn( Ri *, Re *, char ** );
-static LDAPMod *alloc_ldapmod();
-static void free_ldapmod( LDAPMod * );
-static void free_ldmarr( LDAPMod ** );
-static int getmodtype( char * );
-static void dump_ldm_array( LDAPMod ** );
-static char **read_krbnames( Ri * );
-static void upcase( char * );
-static int do_bind( Ri *, int * );
-static int do_unbind( Ri * );
-
-
-/* External references */
-#ifndef SYSERRLIST_IN_STDIO
-extern char *sys_errlist[];
-#endif /* SYSERRLIST_IN_STDIO */
-
-extern char *ch_malloc( unsigned long );
-
-static char *kattrs[] = {"kerberosName", NULL };
-static struct timeval kst = {30L, 0L};
-
+static struct berval **make_singlevalued_berval LDAP_P(( char  *, int ));
+static int op_ldap_add LDAP_P(( Ri *, Re *, char **, int * ));
+static int op_ldap_modify LDAP_P(( Ri *, Re *, char **, int * ));
+static int op_ldap_delete LDAP_P(( Ri *, Re *, char **, int * ));
+static int op_ldap_modrdn LDAP_P(( Ri *, Re *, char **, int * ));
+static LDAPMod *alloc_ldapmod LDAP_P(( void ));
+static void free_ldapmod LDAP_P(( LDAPMod * ));
+static void free_ldmarr LDAP_P(( LDAPMod ** ));
+static int getmodtype LDAP_P(( char * ));
+#ifdef SLAPD_UNUSED
+static void dump_ldm_array LDAP_P(( LDAPMod ** ));
+#endif
+static int do_bind LDAP_P(( Ri *, int * ));
+static int do_unbind LDAP_P(( Ri * ));
 
 
 /*
  * Determine the type of ldap operation being performed and call the
  * appropriate routine.
- * - If successful, returns ERR_DO_LDAP_OK
+ * - If successful, returns DO_LDAP_OK
  * - If a retryable error occurs, ERR_DO_LDAP_RETRYABLE is returned.
  *   The caller should wait a while and retry the operation.
  * - If a fatal error occurs, ERR_DO_LDAP_FATAL is returned.  The caller
@@ -71,91 +80,97 @@ static struct timeval kst = {30L, 0L};
  */
 int
 do_ldap(
-    Ri         *ri,
-    Re         *re,
-    char       **errmsg
+       Ri              *ri,
+       Re              *re,
+       char    **errmsg,
+       int     *errfree
 )
 {
-    int        rc = 0;
-    int        lderr = LDAP_SUCCESS;
-    int        retry = 2;
-    char *msg;
-
-    *errmsg = NULL;
-
-    while ( retry > 0 ) {
-       if ( ri->ri_ldp == NULL ) {
-           rc = do_bind( ri, &lderr );
-           if ( rc != BIND_OK ) {
-               return DO_LDAP_ERR_RETRYABLE;
-           }
-       }
-
-       switch ( re->re_changetype ) {
-       case T_ADDCT:
-           lderr = op_ldap_add( ri, re, errmsg );
-           if ( lderr != LDAP_SUCCESS ) {
-               Debug( LDAP_DEBUG_ANY,
-                       "Error: ldap_add_s failed adding \"%s\": %s\n",
-                       *errmsg ? *errmsg : ldap_err2string( lderr ),
-                       re->re_dn, 0 );
-           }
-           break;
-       case T_MODIFYCT:
-           lderr = op_ldap_modify( ri, re, errmsg );
-           if ( lderr != LDAP_SUCCESS ) {
-               Debug( LDAP_DEBUG_ANY,
-                       "Error: ldap_modify_s failed modifying \"%s\": %s\n",
-                       *errmsg ? *errmsg : ldap_err2string( lderr ),
-                       re->re_dn, 0 );
-           }
-           break;
-       case T_DELETECT:
-           lderr = op_ldap_delete( ri, re, errmsg );
-           if ( lderr != LDAP_SUCCESS ) {
-               Debug( LDAP_DEBUG_ANY,
-                       "Error: ldap_delete_s failed deleting \"%s\": %s\n",
-                       *errmsg ? *errmsg : ldap_err2string( lderr ),
-                       re->re_dn, 0 );
-           }
-           break;
-       case T_MODRDNCT:
-           lderr = op_ldap_modrdn( ri, re, errmsg );
-           if ( lderr != LDAP_SUCCESS ) {
-               Debug( LDAP_DEBUG_ANY,
-                       "Error: ldap_modrdn_s failed modifying %s: %s\n",
-                       *errmsg ? *errmsg : ldap_err2string( lderr ),
-                       re->re_dn, 0 );
-           }
-           break;
-       default:
-           Debug( LDAP_DEBUG_ANY,
-                   "Error: do_ldap: bad op \"%d\", dn = \"%s\"\n",
-                   re->re_changetype, re->re_dn, 0 );
-           return DO_LDAP_ERR_FATAL;
-       }
-
-       /*
-        * Analyze return code.  If ok, just return.  If LDAP_SERVER_DOWN,
-        * we may have been idle long enough that the remote slapd timed
-        * us out.  Rebind and try again.
-        */
-       if ( lderr == LDAP_SUCCESS ) {
-           return DO_LDAP_OK;
-       } else if ( lderr == LDAP_SERVER_DOWN ) {
-           /* The LDAP server may have timed us out - rebind and try again */
-           (void) do_unbind( ri );
-           retry--;
-       } else {
-           return DO_LDAP_ERR_FATAL;
-       }
-    }
-    return DO_LDAP_ERR_FATAL;
+       int     retry = 2;
+       *errmsg = NULL;
+       *errfree = 0;
+
+       do {
+               int lderr;
+               if ( ri->ri_ldp == NULL ) {
+                       lderr = do_bind( ri, &lderr );
+
+                       if ( lderr != BIND_OK ) {
+                               return DO_LDAP_ERR_RETRYABLE;
+                       }
+               }
+
+               switch ( re->re_changetype ) {
+               case T_ADDCT:
+                       lderr = op_ldap_add( ri, re, errmsg, errfree );
+                       if ( lderr != LDAP_SUCCESS ) {
+                               Debug( LDAP_DEBUG_ANY,
+                                       "Error: ldap_add_s failed adding DN \"%s\": %s\n",
+                                       re->re_dn, *errmsg && (*errmsg)[0] ?
+                                       *errmsg : ldap_err2string( lderr ), 0 );
+                       }
+                       break;
+
+               case T_MODIFYCT:
+                       lderr = op_ldap_modify( ri, re, errmsg, errfree );
+                       if ( lderr != LDAP_SUCCESS ) {
+                               Debug( LDAP_DEBUG_ANY,
+                                       "Error: ldap_modify_s failed modifying DN \"%s\": %s\n",
+                                       re->re_dn, *errmsg && (*errmsg)[0] ?
+                                       *errmsg : ldap_err2string( lderr ), 0 );
+                       }
+                       break;
+
+               case T_DELETECT:
+                       lderr = op_ldap_delete( ri, re, errmsg, errfree );
+                       if ( lderr != LDAP_SUCCESS ) {
+                               Debug( LDAP_DEBUG_ANY,
+                                       "Error: ldap_delete_s failed deleting DN \"%s\": %s\n",
+                                       re->re_dn, *errmsg && (*errmsg)[0] ?
+                                       *errmsg : ldap_err2string( lderr ), 0 );
+                       }
+                       break;
+
+               case T_MODRDNCT:
+                       lderr = op_ldap_modrdn( ri, re, errmsg, errfree );
+                       if ( lderr != LDAP_SUCCESS ) {
+                               Debug( LDAP_DEBUG_ANY,
+                                       "Error: ldap_modrdn_s failed modifying DN \"%s\": %s\n",
+                                       re->re_dn, *errmsg && (*errmsg)[0] ?
+                                       *errmsg : ldap_err2string( lderr ), 0 );
+                       }
+                       break;
+
+               default:
+                       Debug( LDAP_DEBUG_ANY,
+                               "Error: do_ldap: bad op \"%d\", DN \"%s\"\n",
+                               re->re_changetype, re->re_dn, 0 );
+                       return DO_LDAP_ERR_FATAL;
+               }
+
+               /*
+                * Analyze return code. If ok, just return. If LDAP_SERVER_DOWN,
+                * we may have been idle long enough that the remote slapd timed
+                * us out. Rebind and try again.
+                */
+               switch( lderr ) {
+               case LDAP_SUCCESS:
+                       return DO_LDAP_OK;
+       
+               default:
+                       return DO_LDAP_ERR_FATAL;
+
+               case LDAP_SERVER_DOWN: /* server went down */
+                       (void) do_unbind( ri );
+                       retry--;
+               }
+       } while ( retry > 0 );
+
+       return DO_LDAP_ERR_RETRYABLE;
 }
 
 
 
-
 /*
  * Perform an ldap add operation.
  */
@@ -163,7 +178,8 @@ static int
 op_ldap_add(
     Ri         *ri,
     Re         *re,
-    char       **errmsg
+    char       **errmsg,
+    int                *errfree
 )
 {
     Mi         *mi;
@@ -198,11 +214,15 @@ op_ldap_add(
        Debug( LDAP_DEBUG_ARGS, "replica %s:%d - add dn \"%s\"\n",
                ri->ri_hostname, ri->ri_port, re->re_dn );
        rc = ldap_add_s( ri->ri_ldp, re->re_dn, ldmarr );
-       lderr = ri->ri_ldp->ld_errno;
+
+       ldap_get_option( ri->ri_ldp, LDAP_OPT_ERROR_NUMBER, &lderr);
+       ldap_get_option( ri->ri_ldp, LDAP_OPT_ERROR_STRING, errmsg);
+       *errfree = 1;
+
     } else {
        *errmsg = "No modifications to do";
        Debug( LDAP_DEBUG_ANY,
-               "Error: op_ldap_add: no mods to do (%s)!", re->re_dn, 0, 0 );
+              "Error: op_ldap_add: no mods to do (%s)!\n", re->re_dn, 0, 0 );
     }
     free_ldmarr( ldmarr );
     return( lderr ); 
@@ -219,14 +239,15 @@ static int
 op_ldap_modify(
     Ri         *ri,
     Re         *re,
-    char       **errmsg
+    char       **errmsg,
+    int                *errfree
 )
 {
     Mi         *mi;
     int                state;  /* This code is a simple-minded state machine */
     int                nvals;  /* Number of values we're modifying */
     int                nops;   /* Number of LDAPMod structs in ldmarr */
-    LDAPMod    *ldm, *nldm, **ldmarr;
+    LDAPMod    *ldm = NULL, **ldmarr;
     int                i, len;
     char       *type, *value;
     int                rc = 0;
@@ -284,6 +305,16 @@ op_ldap_modify(
            nvals = 0;
            nops++;
            break;
+       case T_MODOPINCREMENT:
+           state = T_MODOPINCREMENT;
+           ldmarr = ( LDAPMod ** )
+                   ch_realloc(ldmarr, (( nops + 2 ) * ( sizeof( LDAPMod * ))));
+           ldmarr[ nops ] = ldm = alloc_ldapmod();
+           ldm->mod_op = LDAP_MOD_INCREMENT | LDAP_MOD_BVALUES;
+           ldm->mod_type = value;
+           nvals = 0;
+           nops++;
+           break;
        default:
            if ( state == AWAITING_OP ) {
                Debug( LDAP_DEBUG_ANY,
@@ -292,6 +323,8 @@ op_ldap_modify(
                continue;
            }
 
+           assert( ldm != NULL );
+
            /*
             * We should have an attribute: value pair here.
             * Construct the mod_bvalues part of the ldapmod struct.
@@ -320,6 +353,8 @@ op_ldap_modify(
        Debug( LDAP_DEBUG_ARGS, "replica %s:%d - modify dn \"%s\"\n",
                ri->ri_hostname, ri->ri_port, re->re_dn );
        rc = ldap_modify_s( ri->ri_ldp, re->re_dn, ldmarr );
+       ldap_get_option( ri->ri_ldp, LDAP_OPT_ERROR_STRING, errmsg);
+       *errfree = 1;
     }
     free_ldmarr( ldmarr );
     return( rc );
@@ -335,7 +370,8 @@ static int
 op_ldap_delete(
     Ri         *ri,
     Re         *re,
-    char       **errmsg
+    char       **errmsg,
+    int                *errfree
 )
 {
     int                rc;
@@ -343,6 +379,8 @@ op_ldap_delete(
     Debug( LDAP_DEBUG_ARGS, "replica %s:%d - delete dn \"%s\"\n",
            ri->ri_hostname, ri->ri_port, re->re_dn );
     rc = ldap_delete_s( ri->ri_ldp, re->re_dn );
+    ldap_get_option( ri->ri_ldp, LDAP_OPT_ERROR_STRING, errmsg);
+    *errfree = 1;
 
     return( rc );
 }
@@ -353,22 +391,28 @@ op_ldap_delete(
 /*
  * Perform an ldap modrdn operation.
  */
-#define        GOT_NEWRDN              1
-#define        GOT_DRDNFLAGSTR         2
-#define        GOT_ALLNEWRDNFLAGS      ( GOT_NEWRDN | GOT_DRDNFLAGSTR )
+#define        GOT_NEWRDN              0x1
+#define        GOT_DELOLDRDN   0x2
+#define GOT_NEWSUP             0x4
+
+#define GOT_MODDN_REQ  (GOT_NEWRDN|GOT_DELOLDRDN)
+#define        GOT_ALL_MODDN(f)        (((f) & GOT_MODDN_REQ) == GOT_MODDN_REQ)
 static int
 op_ldap_modrdn(
     Ri         *ri,
     Re         *re,
-    char       **errmsg
+    char       **errmsg,
+    int                *errfree
 )
 {
     int                rc = 0;
     Mi         *mi;
     int                i;
+       int             lderr = 0;
     int                state = 0;
     int                drdnflag = -1;
-    char       *newrdn;
+    char       *newrdn = NULL;
+       char    *newsup = NULL;
 
     if ( re->re_mods == NULL ) {
        *errmsg = "No arguments given";
@@ -382,10 +426,27 @@ op_ldap_modrdn(
      */
     for ( mi = re->re_mods, i = 0; mi[ i ].mi_type != NULL; i++ ) {
        if ( !strcmp( mi[ i ].mi_type, T_NEWRDNSTR )) {
+               if( state & GOT_NEWRDN ) {
+               Debug( LDAP_DEBUG_ANY,
+                       "Error: op_ldap_modrdn: multiple newrdn arg \"%s\"\n",
+                       mi[ i ].mi_val, 0, 0 );
+               *errmsg = "Multiple newrdn argument";
+               return -1;
+               }
+
            newrdn = mi[ i ].mi_val;
            state |= GOT_NEWRDN;
-       } else if ( !strcmp( mi[ i ].mi_type, T_DRDNFLAGSTR )) {
-           state |= GOT_DRDNFLAGSTR;
+
+       } else if ( !strcmp( mi[ i ].mi_type, T_DELOLDRDNSTR )) {
+               if( state & GOT_DELOLDRDN ) {
+               Debug( LDAP_DEBUG_ANY,
+                       "Error: op_ldap_modrdn: multiple deleteoldrdn arg \"%s\"\n",
+                       mi[ i ].mi_val, 0, 0 );
+               *errmsg = "Multiple newrdn argument";
+               return -1;
+               }
+
+           state |= GOT_DELOLDRDN;
            if ( !strcmp( mi[ i ].mi_val, "0" )) {
                drdnflag = 0;
            } else if ( !strcmp( mi[ i ].mi_val, "1" )) {
@@ -397,6 +458,19 @@ op_ldap_modrdn(
                *errmsg = "Incorrect argument to deleteoldrdn";
                return -1;
            }
+
+       } else if ( !strcmp( mi[ i ].mi_type, T_NEWSUPSTR )) {
+               if( state & GOT_NEWSUP ) {
+               Debug( LDAP_DEBUG_ANY,
+                       "Error: op_ldap_modrdn: multiple newsuperior arg \"%s\"\n",
+                       mi[ i ].mi_val, 0, 0 );
+               *errmsg = "Multiple newsuperior argument";
+               return -1;
+               }
+
+               newsup = mi[ i ].mi_val;
+           state |= GOT_NEWSUP;
+
        } else {
            Debug( LDAP_DEBUG_ANY, "Error: op_ldap_modrdn: bad type \"%s\"\n",
                    mi[ i ].mi_type, 0, 0 );
@@ -408,7 +482,7 @@ op_ldap_modrdn(
     /*
      * Punt if we don't have all the args.
      */
-    if ( state != GOT_ALLNEWRDNFLAGS ) {
+    if ( !GOT_ALL_MODDN(state) ) {
        Debug( LDAP_DEBUG_ANY, "Error: op_ldap_modrdn: missing arguments\n",
                0, 0, 0 );
        *errmsg = "Missing argument: requires \"newrdn\" and \"deleteoldrdn\"";
@@ -419,10 +493,13 @@ op_ldap_modrdn(
     if ( ldap_debug & LDAP_DEBUG_ARGS ) {
        char buf[ 256 ];
        char *buf2;
-       sprintf( buf, "%s:%d", ri->ri_hostname, ri->ri_port );
-       buf2 = (char *) ch_malloc( strlen( re->re_dn ) + strlen( mi->mi_val )
-               + 10 );
-       sprintf( buf2, "(\"%s\" -> \"%s\")", re->re_dn, mi->mi_val );
+       int buf2len = strlen( re->re_dn ) + strlen( mi->mi_val ) + 11;
+
+       snprintf( buf, sizeof(buf), "%s:%d", ri->ri_hostname, ri->ri_port );
+
+       buf2 = (char *) ch_malloc( buf2len );
+       snprintf( buf2, buf2len, "(\"%s\" -> \"%s\")", re->re_dn, mi->mi_val );
+
        Debug( LDAP_DEBUG_ARGS,
                "replica %s - modify rdn %s (flag: %d)\n",
                buf, buf2, drdnflag );
@@ -430,10 +507,15 @@ op_ldap_modrdn(
     }
 #endif /* LDAP_DEBUG */
 
+    assert( newrdn != NULL );
+
     /* Do the modrdn */
-    rc = ldap_modrdn2_s( ri->ri_ldp, re->re_dn, mi->mi_val, drdnflag );
+    rc = ldap_rename2_s( ri->ri_ldp, re->re_dn, newrdn, newsup, drdnflag );
 
-    return( ri->ri_ldp->ld_errno );
+       ldap_get_option( ri->ri_ldp, LDAP_OPT_ERROR_NUMBER, &lderr);
+       ldap_get_option( ri->ri_ldp, LDAP_OPT_ERROR_STRING, errmsg);
+       *errfree = 1;
+    return( lderr );
 }
 
 
@@ -442,7 +524,7 @@ op_ldap_modrdn(
  * Allocate and initialize an ldapmod struct.
  */
 static LDAPMod *
-alloc_ldapmod()
+alloc_ldapmod( void )
 {
     LDAPMod    *ldm;
 
@@ -536,6 +618,9 @@ char *type )
     if ( !strcmp( type, T_MODOPDELETESTR )) {
        return( T_MODOPDELETE );
     }
+    if ( !strcmp( type, T_MODOPINCREMENTSTR )) {
+       return( T_MODOPINCREMENT );
+    }
     return( T_ERR );
 }
 
@@ -557,7 +642,7 @@ do_unbind(
        if ( rc != LDAP_SUCCESS ) {
            Debug( LDAP_DEBUG_ANY,
                    "Error: do_unbind: ldap_unbind failed for %s:%d: %s\n",
-                   ldap_err2string( rc ), ri->ri_hostname, ri->ri_port );
+                   ri->ri_hostname, ri->ri_port, ldap_err2string( rc ) );
        }
        ri->ri_ldp = NULL;
     }
@@ -583,18 +668,8 @@ do_bind(
     int        *lderr
 )
 {
-    int                rc;
     int                ldrc;
-    char       msgbuf[ 1024];
-#ifdef KERBEROS
-    int retval = 0;
-    int kni, got_tgt;
-    char **krbnames;
-    char *skrbnames[ 2 ];
-    char realm[ REALM_SZ ];
-    char name[ ANAME_SZ ];
-    char instance[ INST_SZ ];
-#endif /* KERBEROS */
+    int                do_tls = ri->ri_tls;
 
     *lderr = 0;
 
@@ -603,6 +678,7 @@ do_bind(
        return( BIND_ERR_BADRI );
     }
 
+retry:
     if ( ri->ri_ldp != NULL ) {
        ldrc = ldap_unbind( ri->ri_ldp );
        if ( ldrc != LDAP_SUCCESS ) {
@@ -612,110 +688,87 @@ do_bind(
        }
        ri->ri_ldp = NULL;
     }
-
-    Debug( LDAP_DEBUG_ARGS, "Open connection to %s:%d\n",
+    
+       if ( ri->ri_uri != NULL ) { /* new URI style */
+           Debug( LDAP_DEBUG_ARGS, "Initializing session to %s\n",
+                   ri->ri_uri, 0, 0 );
+
+               ldrc = ldap_initialize( &(ri->ri_ldp), ri->ri_uri);
+
+               if (ldrc != LDAP_SUCCESS) {
+               Debug( LDAP_DEBUG_ANY, "Error: ldap_initialize(0, %s) failed: %s\n",
+                       ri->ri_uri, ldap_err2string(ldrc), 0 );
+               return( BIND_ERR_OPEN );                
+               }
+       } else { /* old HOST style */
+    Debug( LDAP_DEBUG_ARGS, "Initializing session to %s:%d\n",
            ri->ri_hostname, ri->ri_port, 0 );
-    ri->ri_ldp = ldap_open( ri->ri_hostname, ri->ri_port );
+
+    ri->ri_ldp = ldap_init( ri->ri_hostname, ri->ri_port );
     if ( ri->ri_ldp == NULL ) {
-       Debug( LDAP_DEBUG_ANY, "Error: ldap_open(%s, %d) failed: %s\n",
-               ri->ri_hostname, ri->ri_port, sys_errlist[ errno ] );
-       return( BIND_ERR_OPEN );
+               Debug( LDAP_DEBUG_ANY, "Error: ldap_init(%s, %d) failed: %s\n",
+                       ri->ri_hostname, ri->ri_port, sys_errlist[ errno ] );
+               return( BIND_ERR_OPEN );
     }
+    }
+
+       {       /* set version 3 */
+               int err, version = LDAP_VERSION3;
+               err = ldap_set_option(ri->ri_ldp,
+                       LDAP_OPT_PROTOCOL_VERSION, &version);
+
+               if( err != LDAP_OPT_SUCCESS ) {
+                       Debug( LDAP_DEBUG_ANY,
+                               "Error: ldap_set_option(%s, LDAP_OPT_VERSION, 3) failed!\n",
+                               ri->ri_hostname, NULL, NULL );
+
+                       ldap_unbind( ri->ri_ldp );
+                       ri->ri_ldp = NULL;
+                       return BIND_ERR_VERSION;
+               }
+       }
 
     /*
      * Set ldap library options to (1) not follow referrals, and 
      * (2) restart the select() system call.
      */
-#ifdef LDAP_REFERRALS
-    ri->ri_ldp->ld_options &= ~LDAP_OPT_REFERRALS;
-#endif /* LDAP_REFERRALS */
-    ri->ri_ldp->ld_options |= LDAP_OPT_RESTART;
-
-    switch ( ri->ri_bind_method ) {
-    case AUTH_KERBEROS:
-#ifndef KERBEROS
-       Debug( LDAP_DEBUG_ANY,
-           "Error: Kerberos bind for %s:%d, but not compiled w/kerberos\n",
-           ri->ri_hostname, ri->ri_port, 0 );
-       return( BIND_ERR_KERBEROS_FAILED );
-#else /* KERBEROS */
-       /*
-        * Bind using kerberos.
-        * If "bindprincipal" was given in the config file, then attempt
-        * to get a TGT for that principal (via the srvtab file).  If only
-        * a binddn was given, then we need to read that entry to get
-        * the kerberosName attributes, and try to get a TGT for one
-        * of them.  All are tried.  The first one which succeeds is
-        * returned.  XXX It might be a good idea to just require a
-        * bindprincipal.  Reading the entry every time might be a significant
-        * amount of overhead, if the connection is closed between most
-        * updates.
-        */
-
-       if ( ri->ri_principal != NULL ) {
-           skrbnames[ 0 ] = ri->ri_principal;
-           skrbnames[ 1 ] = NULL;
-           krbnames = skrbnames;
-       } else {
-           krbnames = read_krbnames( ri );
-       }       
-           
-       if (( krbnames == NULL ) || ( krbnames[ 0 ] == NULL )) {
-           Debug( LDAP_DEBUG_ANY,
-                   "Error: Can't find krbname for binddn \"%s\"\n",
-                   ri->ri_bind_dn, 0, 0 );
-           retval = BIND_ERR_KERBEROS_FAILED;
-           goto kexit;
+       {
+               int err;
+               err = ldap_set_option(ri->ri_ldp, LDAP_OPT_REFERRALS, LDAP_OPT_OFF);
+
+               if( err != LDAP_OPT_SUCCESS ) {
+                       Debug( LDAP_DEBUG_ANY,
+                               "Error: ldap_set_option(%s,REFERRALS, OFF) failed!\n",
+                               ri->ri_hostname, NULL, NULL );
+                       ldap_unbind( ri->ri_ldp );
+                       ri->ri_ldp = NULL;
+                       return BIND_ERR_REFERRALS;
+               }
        }
-       /*
-        * Now we've got one or more kerberos principals.  See if any
-        * of them are in the srvtab file.
-        */
-       got_tgt = 0;
-       for ( kni = 0; krbnames[ kni ] != NULL; kni++ ) {
-           rc = kname_parse( name, instance, realm, krbnames[ kni ]);
-           if ( rc != KSUCCESS ) {
-               continue;
-           }
-           upcase( realm );
-           rc = krb_get_svc_in_tkt( name, instance, realm, "krbtgt", realm,
-                   1, ri->ri_srvtab );
-           if ( rc != KSUCCESS) {
-               Debug( LDAP_DEBUG_ANY, "Error: Can't get TGT for %s: %s\n",
-                       krbnames[ kni ], krb_err_txt[ rc ], 0 );
-           } else {
-               got_tgt = 1;
-               break;
-           }
+       ldap_set_option(ri->ri_ldp, LDAP_OPT_RESTART, LDAP_OPT_ON);
+
+       if( do_tls ) {
+               int err = ldap_start_tls_s(ri->ri_ldp, NULL, NULL);
+
+               if( err != LDAP_SUCCESS ) {
+                       Debug( LDAP_DEBUG_ANY,
+                               "%s: ldap_start_tls failed: %s (%d)\n",
+                               ri->ri_tls == TLS_CRITICAL ? "Error" : "Warning",
+                               ldap_err2string( err ), err );
+
+                       if( ri->ri_tls == TLS_CRITICAL ) {
+                               *lderr = err;
+                               ldap_unbind( ri->ri_ldp );
+                               ri->ri_ldp = NULL;
+                               return BIND_ERR_TLS_FAILED;
+                       }
+                       do_tls = TLS_OFF;
+                       goto retry;
+               }
        }
-       if (!got_tgt) {
-           Debug( LDAP_DEBUG_ANY,
-                   "Error: Could not obtain TGT for DN \"%s\"\n", 
-                   ri->ri_bind_dn, 0, 0 );
-           retval = BIND_ERR_KERBEROS_FAILED;
-           goto kexit;
-       }
-       /*
-        * We've got a TGT.  Do a kerberos bind.
-        */
-       Debug( LDAP_DEBUG_ARGS, "bind to %s:%d as %s (kerberos)\n",
-               ri->ri_hostname, ri->ri_port, ri->ri_bind_dn );
-       ldrc = ldap_kerberos_bind_s( ri->ri_ldp, ri->ri_bind_dn );
-       ri->ri_principal = strdup( krbnames[ kni ] );
-       if ( ldrc != LDAP_SUCCESS ) {
-           Debug( LDAP_DEBUG_ANY, "Error: kerberos bind for %s:%dfailed: %s\n",
-               ri->ri_hostname, ri->ri_port, ldap_err2string( ldrc ));
-           *lderr = ldrc;
-           retval = BIND_ERR_KERBEROS_FAILED;
-           goto kexit;
-       }
-kexit: if ( krbnames != NULL ) {
-           ldap_value_free( krbnames );
-       }
-       return( retval);
-       break;
-#endif /* KERBEROS */
-    case AUTH_SIMPLE:
+
+    switch ( ri->ri_bind_method ) {
+    case LDAP_AUTH_SIMPLE:
        /*
         * Bind with a plaintext password.
         */
@@ -728,17 +781,95 @@ kexit:    if ( krbnames != NULL ) {
                    "Error: ldap_simple_bind_s for %s:%d failed: %s\n",
                    ri->ri_hostname, ri->ri_port, ldap_err2string( ldrc ));
            *lderr = ldrc;
+               ldap_unbind( ri->ri_ldp );
+               ri->ri_ldp = NULL;
            return( BIND_ERR_SIMPLE_FAILED );
-       } else {
-           return( BIND_OK );
        }
        break;
+
+       case LDAP_AUTH_SASL:
+       Debug( LDAP_DEBUG_ARGS, "bind to %s as %s via %s (SASL)\n",
+               ri->ri_hostname,
+               ri->ri_authcId ? ri->ri_authcId : "-",
+               ri->ri_saslmech );
+
+#ifdef HAVE_CYRUS_SASL
+       if( ri->ri_secprops != NULL ) {
+               int err = ldap_set_option(ri->ri_ldp,
+                       LDAP_OPT_X_SASL_SECPROPS, ri->ri_secprops);
+
+               if( err != LDAP_OPT_SUCCESS ) {
+                       Debug( LDAP_DEBUG_ANY,
+                               "Error: ldap_set_option(%s,SECPROPS,\"%s\") failed!\n",
+                               ri->ri_hostname, ri->ri_secprops, NULL );
+                       ldap_unbind( ri->ri_ldp );
+                       ri->ri_ldp = NULL;
+                       return BIND_ERR_SASL_FAILED;
+               }
+       }
+
+       {
+               void *defaults = lutil_sasl_defaults( ri->ri_ldp, ri->ri_saslmech,
+                   ri->ri_realm, ri->ri_authcId, ri->ri_password, ri->ri_authzId );
+
+               ldrc = ldap_sasl_interactive_bind_s( ri->ri_ldp, ri->ri_bind_dn,
+                   ri->ri_saslmech, NULL, NULL,
+                   LDAP_SASL_QUIET, lutil_sasl_interact, defaults );
+
+               lutil_sasl_freedefs( defaults );
+               if ( ldrc != LDAP_SUCCESS ) {
+                       Debug( LDAP_DEBUG_ANY, "Error: LDAP SASL for %s:%d failed: %s\n",
+                           ri->ri_hostname, ri->ri_port, ldap_err2string( ldrc ));
+                       *lderr = ldrc;
+                       ldap_unbind( ri->ri_ldp );
+                       ri->ri_ldp = NULL;
+                       return( BIND_ERR_SASL_FAILED );
+               }
+       }
+       break;
+#else
+       Debug( LDAP_DEBUG_ANY,
+               "Error: do_bind: SASL not supported %s:%d\n",
+                ri->ri_hostname, ri->ri_port, NULL );
+       ldap_unbind( ri->ri_ldp );
+       ri->ri_ldp = NULL;
+       return( BIND_ERR_BAD_ATYPE );
+#endif
+
     default:
        Debug(  LDAP_DEBUG_ANY,
                "Error: do_bind: unknown auth type \"%d\" for %s:%d\n",
                ri->ri_bind_method, ri->ri_hostname, ri->ri_port );
+       ldap_unbind( ri->ri_ldp );
+       ri->ri_ldp = NULL;
        return( BIND_ERR_BAD_ATYPE );
     }
+
+       {
+               int err;
+               LDAPControl c;
+               LDAPControl *ctrls[2];
+               ctrls[0] = &c;
+               ctrls[1] = NULL;
+
+               c.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
+               c.ldctl_value.bv_val = NULL;
+               c.ldctl_value.bv_len = 0;
+               c.ldctl_iscritical = 0;
+
+               err = ldap_set_option(ri->ri_ldp, LDAP_OPT_SERVER_CONTROLS, &ctrls);
+
+               if( err != LDAP_OPT_SUCCESS ) {
+                       Debug( LDAP_DEBUG_ANY, "Error: "
+                               "ldap_set_option(%s, SERVER_CONTROLS, ManageDSAit) failed!\n",
+                               ri->ri_hostname, NULL, NULL );
+                       ldap_unbind( ri->ri_ldp );
+                       ri->ri_ldp = NULL;
+                       return BIND_ERR_MANAGEDSAIT;
+               }
+       }
+
+       return( BIND_OK );
 }
 
 
@@ -748,9 +879,11 @@ kexit:     if ( krbnames != NULL ) {
 /*
  * For debugging.  Print the contents of an ldmarr array.
  */
+#ifdef SLAPD_UNUSED
 static void
 dump_ldm_array(
-LDAPMod **ldmarr )
+    LDAPMod **ldmarr
+)
 {
     int                         i, j;
     LDAPMod            *ldm;
@@ -760,99 +893,24 @@ LDAPMod **ldmarr )
     for ( i = 0; ldmarr[ i ] != NULL; i++ ) {
        ldm = ldmarr[ i ];
        Debug( LDAP_DEBUG_TRACE,
-               "Trace (%d): *** ldmarr[ %d ] contents:\n",
-               getpid(), i, 0 );
+               "Trace (%ld): *** ldmarr[ %d ] contents:\n",
+               (long) getpid(), i, 0 );
        Debug( LDAP_DEBUG_TRACE,
-               "Trace (%d): *** ldm->mod_op: %d\n",
-               getpid(), ldm->mod_op, 0 );
+               "Trace (%ld): *** ldm->mod_op: %d\n",
+               (long) getpid(), ldm->mod_op, 0 );
        Debug( LDAP_DEBUG_TRACE,
-               "Trace (%d): *** ldm->mod_type: %s\n",
-               getpid(), ldm->mod_type, 0 );
+               "Trace (%ld): *** ldm->mod_type: %s\n",
+               (long) getpid(), ldm->mod_type, 0 );
        if ( ldm->mod_bvalues != NULL ) {
            for ( j = 0; ( b = ldm->mod_bvalues[ j ] ) != NULL; j++ ) {
                msgbuf = ch_malloc( b->bv_len + 512 );
-               sprintf( msgbuf, "***** bv[ %d ] len = %d, val = <%s>",
+               sprintf( msgbuf, "***** bv[ %d ] len = %ld, val = <%s>",
                        j, b->bv_len, b->bv_val );
                Debug( LDAP_DEBUG_TRACE,
-                       "Trace (%d):%s\n", getpid(), msgbuf, 0 );
+                       "Trace (%ld):%s\n", (long) getpid(), msgbuf, 0 );
                free( msgbuf );
            }
        }
     }
 }
-
-
-/*
- * Get the kerberos names from the binddn for "replica" via an ldap search.
- * Returns a null-terminated array of char *, or NULL if the entry could
- * not be found or there were no kerberosName attributes.  The caller is
- * responsible for freeing the returned array and strings it points to.
- */
-static char **
-read_krbnames(
-    Ri *ri
-)
-{
-    int rc;
-    char **krbnames;
-    int ne;
-    LDAPMessage *result, *entry;
-
-    /* First need to bind as NULL */
-    rc = ldap_simple_bind_s( ri->ri_ldp, NULL, NULL );
-    if ( rc != LDAP_SUCCESS ) {
-       Debug( LDAP_DEBUG_ANY,
-               "Error: null bind failed getting krbnames for %s:%d: %s\n",
-               ri->ri_hostname, ri->ri_port, ldap_err2string( rc ));
-       return( NULL );
-    }
-    rc = ldap_search_st( ri->ri_ldp, ri->ri_bind_dn, LDAP_SCOPE_BASE,
-           "objectclass=*", kattrs, 0, &kst, &result );
-    if ( rc != LDAP_SUCCESS ) {
-       Debug( LDAP_DEBUG_ANY,
-               "Error: search failed getting krbnames for %s:%d: %s\n",
-               ri->ri_hostname, ri->ri_port, ldap_err2string( rc ));
-       return( NULL );
-    }
-    ne = ldap_count_entries( ri->ri_ldp, result );
-    if ( ne == 0 ) {
-       Debug( LDAP_DEBUG_ANY,
-               "Error: Can't find entry \"%s\" for %s:%d kerberos bind\n",
-               ri->ri_bind_dn, ri->ri_hostname, ri->ri_port );
-           return( NULL );
-    }
-    if ( ne > 1 ) {
-       Debug( LDAP_DEBUG_ANY,
-               "Error: Kerberos binddn \"%s\" for %s:%dis ambiguous\n",
-               ri->ri_bind_dn, ri->ri_hostname, ri->ri_port );
-           return( NULL );
-    }
-    entry = ldap_first_entry( ri->ri_ldp, result );
-    if ( entry == NULL ) {
-       Debug( LDAP_DEBUG_ANY,
-               "Error: Can't find \"%s\" for kerberos binddn for %s:%d\n",
-                   ri->ri_bind_dn, ri->ri_hostname, ri->ri_port );
-       return( NULL );
-    }
-    krbnames = ldap_get_values( ri->ri_ldp, entry, "kerberosName" );
-    ldap_msgfree( result );
-    return( krbnames );
-}
-
-
-
-/*
- * upcase a string
- */
-static void
-upcase(
-char *s )
-{
-    char *p;
-
-    for ( p = s; ( p != NULL ) && ( *p != '\0' ); p++ ) {
-       if ( islower( *p )) {
-           *p = toupper( *p );
-       }
-    }
-}
+#endif