]> git.sur5r.net Git - openldap/blobdiff - clients/tools/ldapmodify.c
Import back-mdb
[openldap] / clients / tools / ldapmodify.c
index 18ee3f84015a15fb7b044fe70d0e0f92265121ae..70d231926590d350e43cc5aa09fa4250c44a2214 100644 (file)
@@ -2,7 +2,7 @@
 /* $OpenLDAP$ */
 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  *
- * Copyright 1998-2008 The OpenLDAP Foundation.
+ * Copyright 1998-2011 The OpenLDAP Foundation.
  * Portions Copyright 2006 Howard Chu.
  * Portions Copyright 1998-2003 Kurt D. Zeilenga.
  * Portions Copyright 1998-2001 Net Boolean Incorporated.
 #include "lutil_ldap.h"
 #include "ldif.h"
 #include "ldap_defaults.h"
-#include "ldap_log.h"
 #include "ldap_pvt.h"
 #include "lber_pvt.h"
 
 #include "common.h"
 
-static int     ldapadd, force = 0;
+static int     ldapadd;
 static char *rejfile = NULL;
 static LDAP    *ld = NULL;
 
-#define        M_SEP   0x7f
-
-/* strings found in replog/LDIF entries (mostly lifted from slurpd/slurp.h) */
-static struct berval BV_VERSION = BER_BVC("version");
-static struct berval BV_REPLICA = BER_BVC("replica");
-static struct berval BV_DN = BER_BVC("dn");
-static struct berval BV_CONTROL = BER_BVC("control");
-static struct berval BV_CHANGETYPE = BER_BVC("changetype");
-static struct berval BV_ADDCT = BER_BVC("add");
-static struct berval BV_MODIFYCT = BER_BVC("modify");
-static struct berval BV_DELETECT = BER_BVC("delete");
-static struct berval BV_MODRDNCT = BER_BVC("modrdn");
-static struct berval BV_MODDNCT = BER_BVC("moddn");
-static struct berval BV_RENAMECT = BER_BVC("rename");
-static struct berval BV_MODOPADD = BER_BVC("add");
-static struct berval BV_MODOPREPLACE = BER_BVC("replace");
-static struct berval BV_MODOPDELETE = BER_BVC("delete");
-static struct berval BV_MODOPINCREMENT = BER_BVC("increment");
-static struct berval BV_NEWRDN = BER_BVC("newrdn");
-static struct berval BV_DELETEOLDRDN = BER_BVC("deleteoldrdn");
-static struct berval BV_NEWSUP = BER_BVC("newsuperior");
-
-#define        BVICMP(a,b)     ((a)->bv_len != (b)->bv_len ? \
-       (a)->bv_len - (b)->bv_len : strcasecmp((a)->bv_val, (b)->bv_val))
-
 static int process_ldif_rec LDAP_P(( char *rbuf, int lineno ));
-static int parse_ldif_control LDAP_P(( struct berval *val, LDAPControl ***pctrls ));
 static int domodify LDAP_P((
-       const char *dn,
+       const struct berval *dn,
        LDAPMod **pmods,
        LDAPControl **pctrls,
        int newentry ));
 static int dodelete LDAP_P((
-       const char *dn,
+       const struct berval *dn,
        LDAPControl **pctrls ));
 static int dorename LDAP_P((
-       const char *dn,
-       const char *newrdn,
-       const char *newsup,
+       const struct berval *dn,
+       const struct berval *newrdn,
+       const struct berval *newsup,
        int deleteoldrdn,
        LDAPControl **pctrls ));
 static int process_response(
        LDAP *ld,
        int msgid,
        int res,
-       const char *dn );
+       const struct berval *dn );
 
 #ifdef LDAP_X_TXN
 static int txn = 0;
@@ -138,13 +111,16 @@ usage( void )
        fprintf( stderr, _("Add or modify options:\n"));
        fprintf( stderr, _("  -a         add values (%s)\n"),
                (ldapadd ? _("default") : _("default is to replace")));
+       fprintf( stderr, _("  -c         continuous operation mode (do not stop on errors)\n"));
        fprintf( stderr, _("  -E [!]ext=extparam        modify extensions"
                " (! indicate s criticality)\n"));
+       fprintf( stderr, _("  -f file    read operations from `file'\n"));
+       fprintf( stderr, _("  -M         enable Manage DSA IT control (-MM to make critical)\n"));
+       fprintf( stderr, _("  -P version protocol version (default: 3)\n"));
 #ifdef LDAP_X_TXN
        fprintf( stderr,
                _("             [!]txn=<commit|abort>         (transaction)\n"));
 #endif
-       fprintf( stderr, _("  -F         force all changes records to be used\n"));
        fprintf( stderr, _("  -S file    write skipped modifications to `file'\n"));
 
        tool_common_usage();
@@ -152,8 +128,8 @@ usage( void )
 }
 
 
-const char options[] = "aE:FrS:"
-       "cd:D:e:f:h:H:IMnO:o:p:P:QR:U:vVw:WxX:y:Y:Z";
+const char options[] = "aE:rS:"
+       "cd:D:e:f:h:H:IMnNO:o:p:P:QR:U:vVw:WxX:y:Y:Z";
 
 int
 handle_private_option( int i )
@@ -217,10 +193,6 @@ handle_private_option( int i )
                ldapadd = 1;
                break;
 
-       case 'F':       /* force all changes records to be used */
-               force = 1;
-               break;
-
        case 'r':       /* replace (obsolete) */
                break;
 
@@ -246,7 +218,7 @@ main( int argc, char **argv )
        FILE            *rejfp;
        struct LDIFFP *ldiffp, ldifdummy = {0};
        char            *matched_msg, *error_msg;
-       int             rc, retval;
+       int             rc, retval, ldifrc;
        int             len;
        int             i = 0;
        int             lineno, nextline = 0, lmax = 0;
@@ -266,7 +238,8 @@ main( int argc, char **argv )
        if ( rejfile != NULL ) {
                if (( rejfp = fopen( rejfile, "w" )) == NULL ) {
                        perror( rejfile );
-                       return( EXIT_FAILURE );
+                       retval = EXIT_FAILURE;
+                       goto fail;
                }
        } else {
                rejfp = NULL;
@@ -275,7 +248,8 @@ main( int argc, char **argv )
        if ( infile != NULL ) {
                if (( ldiffp = ldif_open( infile, "r" )) == NULL ) {
                        perror( infile );
-                       return( EXIT_FAILURE );
+                       retval = EXIT_FAILURE;
+                       goto fail;
                }
        } else {
                ldifdummy.fp = stdin;
@@ -287,15 +261,6 @@ main( int argc, char **argv )
        ld = tool_conn_setup( dont, 0 );
 
        if ( !dont ) {
-               if ( pw_file || want_bindpw ) {
-                       if ( pw_file ) {
-                               rc = lutil_get_filed_password( pw_file, &passwd );
-                               if( rc ) return EXIT_FAILURE;
-                       } else {
-                               passwd.bv_val = getpassphrase( _("Enter LDAP Password: ") );
-                               passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
-                       }
-               }
                tool_bind( ld );
        }
 
@@ -305,7 +270,10 @@ main( int argc, char **argv )
                rc = ldap_txn_start_s( ld, NULL, NULL, &txn_id );
                if( rc != LDAP_SUCCESS ) {
                        tool_perror( "ldap_txn_start_s", rc, NULL, NULL, NULL, NULL );
-                       if( txn > 1 ) return EXIT_FAILURE;
+                       if( txn > 1 ) {
+                               retval = EXIT_FAILURE;
+                               goto fail;
+                       }
                        txn = 0;
                }
        }
@@ -332,14 +300,15 @@ main( int argc, char **argv )
        rc = 0;
        retval = 0;
        lineno = 1;
-       while (( rc == 0 || contoper ) && ldif_read_record( ldiffp, &nextline,
-               &rbuf, &lmax ))
+       while (( rc == 0 || contoper ) && ( ldifrc = ldif_read_record( ldiffp, &nextline,
+               &rbuf, &lmax )) > 0 )
        {
                if ( rejfp ) {
                        len = strlen( rbuf );
                        if (( rejbuf = (char *)ber_memalloc( len+1 )) == NULL ) {
                                perror( "malloc" );
-                               exit( EXIT_FAILURE );
+                               retval = EXIT_FAILURE;
+                               goto fail;
                        }
                        memcpy( rejbuf, rbuf, len+1 );
                }
@@ -375,6 +344,9 @@ main( int argc, char **argv )
        }
        ber_memfree( rbuf );
 
+       if ( ldifrc < 0 )
+               retval = LDAP_OTHER;
+
 #ifdef LDAP_X_TXN
        if( retval == 0 && txn ) {
                rc = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, NULL );
@@ -391,15 +363,21 @@ main( int argc, char **argv )
        }
 #endif
 
-       if ( !dont ) {
+       if ( !dont && ld != NULL ) {
                tool_unbind( ld );
        }
 
+       tool_destroy();
+
+fail:;
        if ( rejfp != NULL ) {
                fclose( rejfp );
        }
 
-       tool_destroy();
+       if ( ldiffp != NULL && ldiffp != &ldifdummy ) {
+               ldif_close( ldiffp );
+       }
+
        return( retval );
 }
 
@@ -407,484 +385,54 @@ main( int argc, char **argv )
 static int
 process_ldif_rec( char *rbuf, int linenum )
 {
-       char    *line, *dn, *newrdn, *newsup;
-       int             rc, modop, replicaport;
-       int             expect_modop, expect_sep;
-       int             deleteoldrdn;
-       int             saw_replica, use_record, new_entry, delete_entry, got_all;
-       LDAPMod **pmods, *lm = NULL;
-       int version;
-       LDAPControl **pctrls;
-       int i, j, k, lines, idn, nmods;
-       struct berval *btype, *vals, **bvl, bv;
-       char *freeval;
-       unsigned char *mops = NULL;
-
-       new_entry = ldapadd;
-
-       rc = got_all = saw_replica = delete_entry = modop = expect_modop = 0;
-       expect_sep = 0;
-       version = 0;
-       deleteoldrdn = 1;
-       use_record = force;
-       pmods = NULL;
-       pctrls = NULL;
-       dn = newrdn = newsup = NULL;
-
-       lines = ldif_countlines( rbuf );
-       btype = ber_memcalloc( 1, (lines+1)*2*sizeof(struct berval)+lines );
-       if ( !btype )
-               return LDAP_NO_MEMORY;
-
-       vals = btype+lines+1;
-       freeval = (char *)(vals+lines+1);
-       i = -1;
-
-       while ( rc == 0 && ( line = ldif_getline( &rbuf )) != NULL ) {
-               int freev;
-
-               if ( *line == '\n' || *line == '\0' ) {
-                       break;
-               }
-
-               ++i;
-
-               if ( line[0] == '-' && !line[1] ) {
-                       BER_BVZERO( btype+i );
-                       freeval[i] = 0;
-                       continue;
-               }
-       
-               if ( ( rc = ldif_parse_line2( line, btype+i, vals+i, &freev ) ) < 0 ) {
-                       fprintf( stderr, _("%s: invalid format (line %d) entry: \"%s\"\n"),
-                               prog, linenum+i, dn == NULL ? "" : dn );
-                       rc = LDAP_PARAM_ERROR;
-                       break;
-               }
-               freeval[i] = freev;
-
-               if ( dn == NULL ) {
-                       if ( !use_record && !BVICMP( btype+i, &BV_REPLICA )) {
-                               char *p;
-                               ++saw_replica;
-                               if (( p = strchr( vals[i].bv_val, ':' )) == NULL ) {
-                                       replicaport = 0;
-                               } else {
-                                       *p++ = '\0';
-                                       if ( lutil_atoi( &replicaport, p ) != 0 ) {
-                                               fprintf( stderr, _("%s: unable to parse replica port \"%s\" (line %d) entry: \"%s\"\n"),
-                                                       prog, p, linenum+i, dn == NULL ? "" : dn );
-                                               rc = LDAP_PARAM_ERROR;
-                                               break;
-                                       }
-                               }
-                               if ( ldaphost != NULL &&
-                                       strcasecmp( vals[i].bv_val, ldaphost ) == 0 &&
-                                       replicaport == ldapport )
-                               {
-                                       use_record = 1;
-                               }
-                       } else if ( linenum+i == 1 && !BVICMP( btype+i, &BV_VERSION )) {
-                               int     v;
-                               if( vals[i].bv_len == 0 || lutil_atoi( &v, vals[i].bv_val) != 0 || v != 1 ) {
-                                       fprintf( stderr,
-                                               _("%s: invalid version %s, line %d (ignored)\n"),
-                                               prog, vals[i].bv_val, linenum );
-                               }
-                               version++;
-
-                       } else if ( !BVICMP( btype+i, &BV_DN )) {
-                               dn = vals[i].bv_val;
-                               idn = i;
-                               if ( !use_record && saw_replica ) {
-                                       printf(_("%s: skipping change record for entry: %s at line %d\n"),
-                                               prog, dn, linenum+i);
-                                       printf(_("\t(LDAP host/port does not match replica: lines)\n"));
-                                       rc = 0;
-                                       goto leave;
-                               }
-                       }
-                       /* skip all lines until we see "dn:" */
-               }
-       }
-
-       /* check to make sure there was a dn: line */
-       if ( !dn ) {
-               rc = 0;
-               goto leave;
-       }
-
-       lines = i+1;
+       LDIFRecord lr;
+       int lrflags = ldapadd ? LDIF_DEFAULT_ADD : 0;
+       int rc;
+       struct berval rbuf_bv;
 
-       if( lines == 0 ) {
-               rc = 0;
-               goto leave;
+#ifdef TEST_LDIF_API
+       if ( getenv( "LDIF_ENTRIES_ONLY" ) ) {
+               lrflags |= LDIF_ENTRIES_ONLY;
        }
-
-       if( version && lines == 1 ) {
-               rc = 0;
-               goto leave;
-       }
-
-       i = idn+1;
-       /* Check for "control" tag after dn and before changetype. */
-       if (!BVICMP( btype+i, &BV_CONTROL)) {
-               /* Parse and add it to the list of controls */
-               rc = parse_ldif_control( vals+i, &pctrls );
-               if (rc != 0) {
-                       fprintf( stderr,
-                               _("%s: Error processing %s line, line %d: %s\n"),
-                               prog, BV_CONTROL.bv_val, linenum+i, ldap_err2string(rc) );
-               }
-               i++;
-               if ( i>= lines ) {
-short_input:
-                       fprintf( stderr,
-                               _("%s: Expecting more input after %s line, line %d\n"),
-                               prog, btype[i-1].bv_val, linenum+i );
-                       
-                       rc = LDAP_PARAM_ERROR;
-                       goto leave;
-               }
-       }
-
-       /* Check for changetype */
-       if ( !BVICMP( btype+i, &BV_CHANGETYPE )) {
-#ifdef LIBERAL_CHANGETYPE_MODOP
-               /* trim trailing spaces (and log warning ...) */
-               int icnt;
-               for ( icnt = vals[i].bv_len; --icnt > 0; ) {
-                       if ( !isspace( (unsigned char) vals[i].bv_val[icnt] ) ) {
-                               break;
-                       }
-               }
-
-               if ( ++icnt != vals[i].bv_len ) {
-                       fprintf( stderr, _("%s: illegal trailing space after"
-                               " \"%s: %s\" trimmed (line %d, entry \"%s\")\n"),
-                               prog, BV_CHANGETYPE.bv_val, vals[i].bv_val, linenum+i, dn );
-                       vals[i].bv_val[icnt] = '\0';
-               }
-#endif /* LIBERAL_CHANGETYPE_MODOP */
-
-               if ( BVICMP( vals+i, &BV_MODIFYCT ) == 0 ) {
-                       new_entry = 0;
-                       expect_modop = 1;
-               } else if ( BVICMP( vals+i, &BV_ADDCT ) == 0 ) {
-                       new_entry = 1;
-                       modop = LDAP_MOD_ADD;
-               } else if ( BVICMP( vals+i, &BV_MODRDNCT ) == 0
-                       || BVICMP( vals+i, &BV_MODDNCT ) == 0
-                       || BVICMP( vals+i, &BV_RENAMECT ) == 0)
-               {
-                       i++;
-                       if ( i >= lines )
-                               goto short_input;
-                       if ( BVICMP( btype+i, &BV_NEWRDN )) {
-                               fprintf( stderr, _("%s: expecting \"%s:\" but saw"
-                                       " \"%s:\" (line %d, entry \"%s\")\n"),
-                                       prog, BV_NEWRDN.bv_val, btype[i].bv_val, linenum+i, dn );
-                               rc = LDAP_PARAM_ERROR;
-                               goto leave;
-                       }
-                       newrdn = vals[i].bv_val;
-                       i++;
-                       if ( i >= lines )
-                               goto short_input;
-                       if ( BVICMP( btype+i, &BV_DELETEOLDRDN )) {
-                               fprintf( stderr, _("%s: expecting \"%s:\" but saw"
-                                       " \"%s:\" (line %d, entry \"%s\")\n"),
-                                       prog, BV_DELETEOLDRDN.bv_val, btype[i].bv_val, linenum+i, dn );
-                               rc = LDAP_PARAM_ERROR;
-                               goto leave;
-                       }
-                       deleteoldrdn = ( vals[i].bv_val[0] == '0' ) ? 0 : 1;
-                       i++;
-                       if ( i < lines ) {
-                               if ( BVICMP( btype+i, &BV_NEWSUP )) {
-                                       fprintf( stderr, _("%s: expecting \"%s:\" but saw"
-                                               " \"%s:\" (line %d, entry \"%s\")\n"),
-                                               prog, BV_NEWSUP.bv_val, btype[i].bv_val, linenum+i, dn );
-                                       rc = LDAP_PARAM_ERROR;
-                                       goto leave;
-                               }
-                               newsup = vals[i].bv_val;
-                               i++;
-                       }
-                       got_all = 1;
-               } else if ( BVICMP( vals+i, &BV_DELETECT ) == 0 ) {
-                       got_all = delete_entry = 1;
-               } else {
-                       fprintf( stderr,
-                               _("%s:  unknown %s \"%s\" (line %d, entry \"%s\")\n"),
-                               prog, BV_CHANGETYPE.bv_val, vals[i].bv_val, linenum+i, dn );
-                       rc = LDAP_PARAM_ERROR;
-                       goto leave;
-               }
-               i++;
-       } else if ( ldapadd ) {         /*  missing changetype => add */
-               new_entry = 1;
-               modop = LDAP_MOD_ADD;
-       } else {
-               expect_modop = 1;       /* missing changetype => modify */
+       if ( getenv( "LDIF_NO_CONTROLS" ) ) {
+               lrflags |= LDIF_NO_CONTROLS;
        }
+#endif /* TEST_LDIF_API */
 
-       if ( got_all ) {
-               if ( i < lines ) {
-                       fprintf( stderr,
-                               _("%s: extra lines at end (line %d, entry \"%s\")\n"),
-                               prog, linenum+i, dn );
-                       rc = LDAP_PARAM_ERROR;
-                       goto leave;
-               }
-               goto doit;
-       }
+       rbuf_bv.bv_val = rbuf;
+       rbuf_bv.bv_len = 0; /* not used */
+       rc = ldap_parse_ldif_record( &rbuf_bv, linenum, &lr, prog, lrflags );
 
-       nmods = lines - i;
-       idn = i;
-
-       if ( new_entry ) {
-               int fv;
-
-               /* Make sure all attributes with multiple values are contiguous */
-               for (; i<lines; i++) {
-                       for (j=i+1; j<lines; j++) {
-                               if ( !BVICMP( btype+i, btype+j )) {
-                                       nmods--;
-                                       /* out of order, move intervening attributes down */
-                                       if ( j != i+1 ) {
-                                               bv = vals[j];
-                                               fv = freeval[j];
-                                               for (k=j; k>i; k--) {
-                                                       btype[k] = btype[k-1];
-                                                       vals[k] = vals[k-1];
-                                                       freeval[k] = freeval[k-1];
-                                               }
-                                               k++;
-                                               btype[k] = btype[i];
-                                               vals[k] = bv;
-                                               freeval[k] = fv;
-                                       }
-                                       i++;
-                               }
-                       }
-               }
-               /* Allocate space for array of mods, array of pointers to mods,
-                * and array of pointers to values, allowing for NULL terminators
-                * for the pointer arrays...
-                */
-               lm = ber_memalloc( nmods * sizeof(LDAPMod) +
-                       (nmods+1) * sizeof(LDAPMod*) +
-                       (lines + nmods - idn) * sizeof(struct berval *));
-               pmods = (LDAPMod **)(lm+nmods);
-               bvl = (struct berval **)(pmods+nmods+1);
-
-               j = 0;
-               k = -1;
-               BER_BVZERO(&bv);
-               for (i=idn; i<lines; i++) {
-                       if ( !BVICMP( btype+i, &BV_DN )) {
-                               fprintf( stderr, _("%s: attributeDescription \"%s\":"
-                                       " (possible missing newline"
-                                               " after line %d, entry \"%s\"?)\n"),
-                                       prog, btype[i].bv_val, linenum+i - 1, dn );
-                       }
-                       if ( BVICMP(btype+i,&bv)) {
-                               bvl[k++] = NULL;
-                               bv = btype[i];
-                               lm[j].mod_op = LDAP_MOD_ADD | LDAP_MOD_BVALUES;
-                               lm[j].mod_type = bv.bv_val;
-                               lm[j].mod_bvalues = bvl+k;
-                               pmods[j] = lm+j;
-                               j++;
-                       }
-                       bvl[k++] = vals+i;
-               }
-               bvl[k] = NULL;
-               pmods[j] = NULL;
-               goto doit;
-       }
-
-       mops = ber_memalloc( lines+1 );
-       mops[lines] = M_SEP;
-       mops[i-1] = M_SEP;
-
-       for ( ; i<lines; i++ ) {
-               if ( expect_modop ) {
-#ifdef LIBERAL_CHANGETYPE_MODOP
-                       /* trim trailing spaces (and log warning ...) */
-                   int icnt;
-                   for ( icnt = vals[i].bv_len; --icnt > 0; ) {
-                               if ( !isspace( (unsigned char) vals[i].bv_val[icnt] ) ) break;
-                       }
-    
-                       if ( ++icnt != vals[i].bv_len ) {
-                               fprintf( stderr, _("%s: illegal trailing space after"
-                                       " \"%s: %s\" trimmed (line %d, entry \"%s\")\n"),
-                                       prog, type, vals[i].bv_val, linenum+i, dn );
-                               vals[i].bv_val[icnt] = '\0';
-                       }
-#endif /* LIBERAL_CHANGETYPE_MODOP */
-
-                       expect_modop = 0;
-                       expect_sep = 1;
-                       if ( BVICMP( btype+i, &BV_MODOPADD ) == 0 ) {
-                               modop = LDAP_MOD_ADD;
-                               mops[i] = M_SEP;
-                               nmods--;
-                       } else if ( BVICMP( btype+i, &BV_MODOPREPLACE ) == 0 ) {
-                       /* defer handling these since they might have no values.
-                        * Use the BVALUES flag to signal that these were
-                        * deferred. If values are provided later, this
-                        * flag will be switched off.
-                        */
-                               modop = LDAP_MOD_REPLACE;
-                               mops[i] = modop | LDAP_MOD_BVALUES;
-                               btype[i] = vals[i];
-                       } else if ( BVICMP( btype+i, &BV_MODOPDELETE ) == 0 ) {
-                               modop = LDAP_MOD_DELETE;
-                               mops[i] = modop | LDAP_MOD_BVALUES;
-                               btype[i] = vals[i];
-                       } else if ( BVICMP( btype+i, &BV_MODOPINCREMENT ) == 0 ) {
-                               modop = LDAP_MOD_INCREMENT;
-                               mops[i] = M_SEP;
-                               nmods--;
-                       } else {        /* no modify op: invalid LDIF */
-                               fprintf( stderr, _("%s: modify operation type is missing at"
-                                       " line %d, entry \"%s\"\n"),
-                                       prog, linenum+i, dn );
-                               rc = LDAP_PARAM_ERROR;
-                               goto leave;
-                       }
-                       bv = vals[i];
-               } else if ( expect_sep && BER_BVISEMPTY( btype+i )) {
-                       mops[i] = M_SEP;
-                       expect_sep = 0;
-                       expect_modop = 1;
-                       nmods--;
-               } else {
-                       if ( BVICMP( btype+i, &bv )) {
-                               fprintf( stderr, _("%s: wrong attributeType at"
-                                       " line %d, entry \"%s\"\n"),
-                                       prog, linenum+i, dn );
-                               rc = LDAP_PARAM_ERROR;
-                               goto leave;
-                       }
-                       mops[i] = modop;
-                       /* If prev op was deferred and matches this type,
-                        * clear the flag
-                        */
-                       if ( (mops[i-1]&LDAP_MOD_BVALUES) && !BVICMP(btype+i,
-                               btype+i-1)) {
-                               mops[i-1] = M_SEP;
-                               nmods--;
-                       }
-               }
-       }
-
-#if 0  /* we should faithfully encode the LDIF, not combine */
-       /* Make sure all modops with multiple values are contiguous */
-       for (i=idn; i<lines; i++) {
-               if ( mops[i] == M_SEP )
-                       continue;
-               for (j=i+1; j<lines; j++) {
-                       if ( mops[j] == M_SEP || mops[i] != mops[j] )
-                               continue;
-                       if ( !BVICMP( btype+i, btype+j )) {
-                               nmods--;
-                               /* out of order, move intervening attributes down */
-                               if ( j != i+1 ) {
-                                       int c;
-                                       struct berval bv;
-                                       char fv;
-
-                                       c = mops[j];
-                                       bv = vals[j];
-                                       fv = freeval[j];
-                                       for (k=j; k>i; k--) {
-                                               btype[k] = btype[k-1];
-                                               vals[k] = vals[k-1];
-                                               freeval[k] = freeval[k-1];
-                                               mops[k] = mops[k-1];
-                                       }
-                                       k++;
-                                       btype[k] = btype[i];
-                                       vals[k] = bv;
-                                       freeval[k] = fv;
-                                       mops[k] = c;
-                               }
-                               i++;
-                       }
-               }
-       }
-#endif
-
-       /* Allocate space for array of mods, array of pointers to mods,
-        * and array of pointers to values, allowing for NULL terminators
-        * for the pointer arrays...
-        */
-       lm = ber_memalloc( nmods * sizeof(LDAPMod) +
-               (nmods+1) * sizeof(LDAPMod*) +
-               (lines + nmods - idn) * sizeof(struct berval *));
-       pmods = (LDAPMod **)(lm+nmods);
-       bvl = (struct berval **)(pmods+nmods+1);
-
-       j = 0;
-       k = -1;
-       BER_BVZERO(&bv);
-       mops[idn-1] = M_SEP;
-       for (i=idn; i<lines; i++) {
-               if ( mops[i] == M_SEP )
-                       continue;
-               if ( mops[i] != mops[i-1] || BVICMP(btype+i,&bv)) {
-                       bvl[k++] = NULL;
-                       bv = btype[i];
-                       lm[j].mod_op = mops[i] | LDAP_MOD_BVALUES;
-                       lm[j].mod_type = bv.bv_val;
-                       if ( mops[i] & LDAP_MOD_BVALUES ) {
-                               lm[j].mod_bvalues = NULL;
-                       } else {
-                               lm[j].mod_bvalues = bvl+k;
-                       }
-                       pmods[j] = lm+j;
-                       j++;
-               }
-               bvl[k++] = vals+i;
-       }
-       bvl[k] = NULL;
-       pmods[j] = NULL;
-
-doit:
        /* If default controls are set (as with -M option) and controls are
           specified in the LDIF file, we must add the default controls to
           the list of controls sent with the ldap operation.
        */
        if ( rc == 0 ) {
-               if (pctrls) {
+               if (lr.lr_ctrls) {
                        LDAPControl **defctrls = NULL;   /* Default server controls */
                        LDAPControl **newctrls = NULL;
                        ldap_get_option(ld, LDAP_OPT_SERVER_CONTROLS, &defctrls);
                        if (defctrls) {
                                int npc=0;                       /* Num of LDIF controls */
                                int ndefc=0;                     /* Num of default controls */
-                               while (pctrls[npc]) npc++;       /* Count LDIF controls */
+                               while (lr.lr_ctrls[npc]) npc++;       /* Count LDIF controls */
                                while (defctrls[ndefc]) ndefc++; /* Count default controls */
-                               newctrls = ber_memrealloc(pctrls,
+                               newctrls = ber_memrealloc(lr.lr_ctrls,
                                        (npc+ndefc+1)*sizeof(LDAPControl*));
 
                                if (newctrls == NULL) {
                                        rc = LDAP_NO_MEMORY;
                                } else {
                                        int i;
-                                       pctrls = newctrls;
+                                       lr.lr_ctrls = newctrls;
                                        for (i=npc; i<npc+ndefc; i++) {
-                                               pctrls[i] = ldap_control_dup(defctrls[i-npc]);
-                                               if (pctrls[i] == NULL) {
+                                               lr.lr_ctrls[i] = ldap_control_dup(defctrls[i-npc]);
+                                               if (lr.lr_ctrls[i] == NULL) {
                                                        rc = LDAP_NO_MEMORY;
                                                        break;
                                                }
                                        }
-                                       pctrls[npc+ndefc] = NULL;
+                                       lr.lr_ctrls[npc+ndefc] = NULL;
                                }
                                ldap_controls_free(defctrls);  /* Must be freed by library */
                        }
@@ -892,12 +440,14 @@ doit:
        }
 
        if ( rc == 0 ) {
-               if ( delete_entry ) {
-                       rc = dodelete( dn, pctrls );
-               } else if ( newrdn != NULL ) {
-                       rc = dorename( dn, newrdn, newsup, deleteoldrdn, pctrls );
+               if ( LDAP_REQ_DELETE == lr.lr_op ) {
+                       rc = dodelete( &lr.lr_dn, lr.lr_ctrls );
+               } else if ( LDAP_REQ_RENAME == lr.lr_op ) {
+                       rc = dorename( &lr.lr_dn, &lr.lrop_newrdn, &lr.lrop_newsup, lr.lrop_delold, lr.lr_ctrls );
+               } else if ( ( LDAP_REQ_ADD == lr.lr_op ) || ( LDAP_REQ_MODIFY == lr.lr_op ) ) {
+                       rc = domodify( &lr.lr_dn, lr.lrop_mods, lr.lr_ctrls, LDAP_REQ_ADD == lr.lr_op );
                } else {
-                       rc = domodify( dn, pmods, pctrls, new_entry );
+                       /* record skipped e.g. version: or comment or something we don't handle yet */
                }
 
                if ( rc == LDAP_SUCCESS ) {
@@ -905,145 +455,7 @@ doit:
                }
        }
 
-leave:
-    if (pctrls != NULL) {
-       ldap_controls_free( pctrls );
-       }
-       if ( lm != NULL ) {
-               ber_memfree( lm );
-       }
-       if ( mops != NULL ) {
-               ber_memfree( mops );
-       }
-       for (i=lines-1; i>=0; i--)
-               if ( freeval[i] ) ber_memfree( vals[i].bv_val );
-       ber_memfree( btype );
-
-       return( rc );
-}
-
-/* Parse an LDIF control line of the form
-      control:  oid  [true/false]  [: value]              or
-      control:  oid  [true/false]  [:: base64-value]      or
-      control:  oid  [true/false]  [:< url]
-   The control is added to the list of controls in *ppctrls.
-*/      
-static int
-parse_ldif_control(
-       struct berval *bval,
-       LDAPControl ***ppctrls )
-{
-       char *oid = NULL;
-       int criticality = 0;   /* Default is false if not present */
-       int i, rc=0;
-       char *s, *oidStart;
-       LDAPControl *newctrl = NULL;
-       LDAPControl **pctrls = NULL;
-       struct berval type, bv;
-       int freeval;
-
-       if (ppctrls) pctrls = *ppctrls;
-       /* OID should come first. Validate and extract it. */
-       s = bval->bv_val;
-       if (*s == 0) return ( LDAP_PARAM_ERROR );
-       oidStart = s;
-       while (isdigit((unsigned char)*s) || *s == '.') {
-               s++;                           /* OID should be digits or . */
-       }
-       if (s == oidStart) { 
-               return ( LDAP_PARAM_ERROR );   /* OID was not present */
-       }
-       if (*s) {                          /* End of OID should be space or NULL */
-               if (!isspace((unsigned char)*s)) {
-                       return ( LDAP_PARAM_ERROR ); /* else OID contained invalid chars */
-               }
-               *s++ = 0;                    /* Replace space with null to terminate */
-       }
-
-       oid = ber_strdup(oidStart);
-       if (oid == NULL) return ( LDAP_NO_MEMORY );
-
-       /* Optional Criticality field is next. */
-       while (*s && isspace((unsigned char)*s)) {
-               s++;                         /* Skip white space before criticality */
-       }
-       if (strncasecmp(s, "true", 4) == 0) {
-               criticality = 1;
-               s += 4;
-       } 
-       else if (strncasecmp(s, "false", 5) == 0) {
-               criticality = 0;
-               s += 5;
-       }
-
-       /* Optional value field is next */
-       while (*s && isspace((unsigned char)*s)) {
-               s++;                         /* Skip white space before value */
-       }
-       if (*s) {
-               if (*s != ':') {           /* If value is present, must start with : */
-                       rc = LDAP_PARAM_ERROR;
-                       goto cleanup;
-               }
-
-               /* Back up so value is in the form
-                    a: value
-                    a:: base64-value
-                    a:< url
-                  Then we can use ldif_parse_line2 to extract and decode the value
-               */
-               s--;
-               *s = 'a';
-
-               rc = ldif_parse_line2(s, &type, &bv, &freeval);
-               if (rc < 0) {
-                       rc = LDAP_PARAM_ERROR;
-                       goto cleanup;
-               }
-    }
-
-       /* Create a new LDAPControl structure. */
-       newctrl = (LDAPControl *)ber_memalloc(sizeof(LDAPControl));
-       if ( newctrl == NULL ) {
-               rc = LDAP_NO_MEMORY;
-               goto cleanup;
-       }
-       newctrl->ldctl_oid = oid;
-       oid = NULL;
-       newctrl->ldctl_iscritical = criticality;
-       if ( freeval )
-               newctrl->ldctl_value = bv;
-       else
-               ber_dupbv( &newctrl->ldctl_value, &bv );
-
-       /* Add the new control to the passed-in list of controls. */
-       i = 0;
-       if (pctrls) {
-               while ( pctrls[i] ) {    /* Count the # of controls passed in */
-                       i++;
-               }
-       }
-       /* Allocate 1 more slot for the new control and 1 for the NULL. */
-       pctrls = (LDAPControl **) ber_memrealloc(pctrls,
-               (i+2)*(sizeof(LDAPControl *)));
-       if (pctrls == NULL) {
-               rc = LDAP_NO_MEMORY;
-               goto cleanup;
-       }
-       pctrls[i] = newctrl;
-       newctrl = NULL;
-       pctrls[i+1] = NULL;
-       *ppctrls = pctrls;
-
-cleanup:
-       if (newctrl) {
-               if (newctrl->ldctl_oid) ber_memfree(newctrl->ldctl_oid);
-               if (newctrl->ldctl_value.bv_val) {
-                       ber_memfree(newctrl->ldctl_value.bv_val);
-               }
-               ber_memfree(newctrl);
-       }
-       if (oid) ber_memfree(oid);
+       ldap_ldif_record_done( &lr );
 
        return( rc );
 }
@@ -1051,7 +463,7 @@ cleanup:
 
 static int
 domodify(
-       const char *dn,
+       const struct berval *dn,
        LDAPMod **pmods,
        LDAPControl **pctrls,
        int newentry )
@@ -1059,7 +471,7 @@ domodify(
        int                     rc, i, j, k, notascii, op;
        struct berval   *bvp;
 
-       if ( dn == NULL ) {
+       if ( ( dn == NULL ) || ( dn->bv_val == NULL ) ) {
                fprintf( stderr, _("%s: no DN specified\n"), prog );
                return( LDAP_PARAM_ERROR );
        }
@@ -1071,7 +483,7 @@ domodify(
                 * since \"touch\" on a non-existent entry
                 * will fail)*/
                printf( "warning: no attributes to %sadd (entry=\"%s\")\n",
-                       newentry ? "" : "change or ", dn );
+                       newentry ? "" : "change or ", dn->bv_val );
 
        } else {
                for ( i = 0; pmods[ i ] != NULL; ++i ) {
@@ -1079,7 +491,7 @@ domodify(
                        if( op == LDAP_MOD_ADD && ( pmods[i]->mod_bvalues == NULL )) {
                                fprintf( stderr,
                                        _("%s: attribute \"%s\" has no values (entry=\"%s\")\n"),
-                                       prog, pmods[i]->mod_type, dn );
+                                       prog, pmods[i]->mod_type, dn->bv_val );
                                return LDAP_PARAM_ERROR;
                        }
                }
@@ -1117,22 +529,22 @@ domodify(
        }
 
        if ( newentry ) {
-               printf( "%sadding new entry \"%s\"\n", dont ? "!" : "", dn );
+               printf( "%sadding new entry \"%s\"\n", dont ? "!" : "", dn->bv_val );
        } else {
-               printf( "%smodifying entry \"%s\"\n", dont ? "!" : "", dn );
+               printf( "%smodifying entry \"%s\"\n", dont ? "!" : "", dn->bv_val );
        }
 
        if ( !dont ) {
                int     msgid;
                if ( newentry ) {
-                       rc = ldap_add_ext( ld, dn, pmods, pctrls, NULL, &msgid );
+                       rc = ldap_add_ext( ld, dn->bv_val, pmods, pctrls, NULL, &msgid );
                } else {
-                       rc = ldap_modify_ext( ld, dn, pmods, pctrls, NULL, &msgid );
+                       rc = ldap_modify_ext( ld, dn->bv_val, pmods, pctrls, NULL, &msgid );
                }
 
                if ( rc != LDAP_SUCCESS ) {
                        /* print error message about failed update including DN */
-                       fprintf( stderr, _("%s: update failed: %s\n"), prog, dn );
+                       fprintf( stderr, _("%s: update failed: %s\n"), prog, dn->bv_val );
                        tool_perror( newentry ? "ldap_add" : "ldap_modify",
                                rc, NULL, NULL, NULL, NULL );
                        goto done;
@@ -1156,17 +568,19 @@ done:
 
 static int
 dodelete(
-       const char *dn,
+       const struct berval *dn,
        LDAPControl **pctrls )
 {
        int     rc;
        int msgid;
 
-       printf( _("%sdeleting entry \"%s\"\n"), dont ? "!" : "", dn );
+       assert( dn != NULL );
+       assert( dn->bv_val != NULL );
+       printf( _("%sdeleting entry \"%s\"\n"), dont ? "!" : "", dn->bv_val );
        if ( !dont ) {
-               rc = ldap_delete_ext( ld, dn, pctrls, NULL, &msgid );
+               rc = ldap_delete_ext( ld, dn->bv_val, pctrls, NULL, &msgid );
                if ( rc != LDAP_SUCCESS ) {
-                       fprintf( stderr, _("%s: delete failed: %s\n"), prog, dn );
+                       fprintf( stderr, _("%s: delete failed: %s\n"), prog, dn->bv_val );
                        tool_perror( "ldap_delete", rc, NULL, NULL, NULL, NULL );
                        goto done;
                }
@@ -1187,25 +601,30 @@ done:
 
 static int
 dorename(
-       const char *dn,
-       const char *newrdn,
-       const char* newsup,
+       const struct berval *dn,
+       const struct berval *newrdn,
+       const struct berval *newsup,
        int deleteoldrdn,
        LDAPControl **pctrls )
 {
        int     rc;
        int msgid;
 
-       printf( _("%smodifying rdn of entry \"%s\"\n"), dont ? "!" : "", dn );
+       assert( dn != NULL );
+       assert( dn->bv_val != NULL );
+       assert( newrdn != NULL );
+       assert( newrdn->bv_val != NULL );
+       printf( _("%smodifying rdn of entry \"%s\"\n"), dont ? "!" : "", dn->bv_val );
        if ( verbose ) {
                printf( _("\tnew RDN: \"%s\" (%skeep existing values)\n"),
-                       newrdn, deleteoldrdn ? _("do not ") : "" );
+                       newrdn->bv_val, deleteoldrdn ? _("do not ") : "" );
        }
        if ( !dont ) {
-               rc = ldap_rename( ld, dn, newrdn, newsup, deleteoldrdn,
-                       pctrls, NULL, &msgid );
+               rc = ldap_rename( ld, dn->bv_val, newrdn->bv_val,
+                                                 ( newsup && newsup->bv_val ) ? newsup->bv_val : NULL,
+                                                 deleteoldrdn, pctrls, NULL, &msgid );
                if ( rc != LDAP_SUCCESS ) {
-                       fprintf( stderr, _("%s: rename failed: %s\n"), prog, dn );
+                       fprintf( stderr, _("%s: rename failed: %s\n"), prog, dn->bv_val );
                        tool_perror( "ldap_rename", rc, NULL, NULL, NULL, NULL );
                        goto done;
                }
@@ -1245,7 +664,7 @@ static int process_response(
        LDAP *ld,
        int msgid,
        int op,
-       const char *dn )
+       const struct berval *dn )
 {
        LDAPMessage     *res;
        int             rc = LDAP_OTHER, msgtype;
@@ -1254,6 +673,7 @@ static int process_response(
        char            *text = NULL, *matched = NULL, **refs = NULL;
        LDAPControl     **ctrls = NULL;
 
+       assert( dn != NULL );
        for ( ; ; ) {
                tv.tv_sec = 0;
                tv.tv_usec = 100000;
@@ -1294,7 +714,7 @@ static int process_response(
 
        if ( text ) ldap_memfree( text );
        if ( matched ) ldap_memfree( matched );
-       if ( text ) ber_memvfree( (void **)refs );
+       if ( refs ) ber_memvfree( (void **)refs );
 
        if ( ctrls ) {
                tool_print_ctrls( ld, ctrls );