Back to index

openldap  2.4.31
ldapmodify.c
Go to the documentation of this file.
00001 /* ldapmodify.c - generic program to modify or add entries using LDAP */
00002 /* $OpenLDAP$ */
00003 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
00004  *
00005  * Copyright 1998-2012 The OpenLDAP Foundation.
00006  * Portions Copyright 2006 Howard Chu.
00007  * Portions Copyright 1998-2003 Kurt D. Zeilenga.
00008  * Portions Copyright 1998-2001 Net Boolean Incorporated.
00009  * Portions Copyright 2001-2003 IBM Corporation.
00010  * All rights reserved.
00011  *
00012  * Redistribution and use in source and binary forms, with or without
00013  * modification, are permitted only as authorized by the OpenLDAP
00014  * Public License.
00015  *
00016  * A copy of this license is available in the file LICENSE in the
00017  * top-level directory of the distribution or, alternatively, at
00018  * <http://www.OpenLDAP.org/license.html>.
00019  */
00020 /* Portions Copyright (c) 1992-1996 Regents of the University of Michigan.
00021  * All rights reserved.
00022  *
00023  * Redistribution and use in source and binary forms are permitted
00024  * provided that this notice is preserved and that due credit is given
00025  * to the University of Michigan at Ann Arbor.  The name of the
00026  * University may not be used to endorse or promote products derived
00027  * from this software without specific prior written permission.  This
00028  * software is provided ``as is'' without express or implied warranty.
00029  */
00030 /* ACKNOWLEDGEMENTS:
00031  * This work was originally developed by the University of Michigan
00032  * (as part of U-MICH LDAP).  Additional significant contributors
00033  * include:
00034  *   Kurt D. Zeilenga
00035  *   Norbert Klasen
00036  *   Howard Chu
00037  */
00038 
00039 #include "portable.h"
00040 
00041 #include <stdio.h>
00042 
00043 #include <ac/stdlib.h>
00044 #include <ac/ctype.h>
00045 #include <ac/string.h>
00046 #include <ac/unistd.h>
00047 #include <ac/socket.h>
00048 #include <ac/time.h>
00049 
00050 #ifdef HAVE_SYS_STAT_H
00051 #include <sys/stat.h>
00052 #endif
00053 
00054 #ifdef HAVE_SYS_FILE_H
00055 #include <sys/file.h>
00056 #endif
00057 #ifdef HAVE_FCNTL_H
00058 #include <fcntl.h>
00059 #endif
00060 
00061 #include <ldap.h>
00062 
00063 #include "lutil.h"
00064 #include "lutil_ldap.h"
00065 #include "ldif.h"
00066 #include "ldap_defaults.h"
00067 #include "ldap_pvt.h"
00068 #include "lber_pvt.h"
00069 
00070 #include "common.h"
00071 
00072 static int    ldapadd;
00073 static char *rejfile = NULL;
00074 static LDAP   *ld = NULL;
00075 
00076 #define       M_SEP  0x7f
00077 
00078 /* strings found in LDIF entries */
00079 static struct berval BV_VERSION = BER_BVC("version");
00080 static struct berval BV_DN = BER_BVC("dn");
00081 static struct berval BV_CONTROL = BER_BVC("control");
00082 static struct berval BV_CHANGETYPE = BER_BVC("changetype");
00083 static struct berval BV_ADDCT = BER_BVC("add");
00084 static struct berval BV_MODIFYCT = BER_BVC("modify");
00085 static struct berval BV_DELETECT = BER_BVC("delete");
00086 static struct berval BV_MODRDNCT = BER_BVC("modrdn");
00087 static struct berval BV_MODDNCT = BER_BVC("moddn");
00088 static struct berval BV_RENAMECT = BER_BVC("rename");
00089 static struct berval BV_MODOPADD = BER_BVC("add");
00090 static struct berval BV_MODOPREPLACE = BER_BVC("replace");
00091 static struct berval BV_MODOPDELETE = BER_BVC("delete");
00092 static struct berval BV_MODOPINCREMENT = BER_BVC("increment");
00093 static struct berval BV_NEWRDN = BER_BVC("newrdn");
00094 static struct berval BV_DELETEOLDRDN = BER_BVC("deleteoldrdn");
00095 static struct berval BV_NEWSUP = BER_BVC("newsuperior");
00096 
00097 #define       BV_CASEMATCH(a, b) \
00098        ((a)->bv_len == (b)->bv_len && 0 == strcasecmp((a)->bv_val, (b)->bv_val))
00099 
00100 static int process_ldif_rec LDAP_P(( char *rbuf, int lineno ));
00101 static int parse_ldif_control LDAP_P(( struct berval *val, LDAPControl ***pctrls ));
00102 static int domodify LDAP_P((
00103        const char *dn,
00104        LDAPMod **pmods,
00105        LDAPControl **pctrls,
00106        int newentry ));
00107 static int dodelete LDAP_P((
00108        const char *dn,
00109        LDAPControl **pctrls ));
00110 static int dorename LDAP_P((
00111        const char *dn,
00112        const char *newrdn,
00113        const char *newsup,
00114        int deleteoldrdn,
00115        LDAPControl **pctrls ));
00116 static int process_response(
00117        LDAP *ld,
00118        int msgid,
00119        int res,
00120        const char *dn );
00121 
00122 #ifdef LDAP_X_TXN
00123 static int txn = 0;
00124 static int txnabort = 0;
00125 struct berval *txn_id = NULL;
00126 #endif
00127 
00128 void
00129 usage( void )
00130 {
00131        fprintf( stderr, _("Add or modify entries from an LDAP server\n\n"));
00132        fprintf( stderr, _("usage: %s [options]\n"), prog);
00133        fprintf( stderr, _(" The list of desired operations are read from stdin"
00134               " or from the file\n"));
00135        fprintf( stderr, _(" specified by \"-f file\".\n"));
00136        fprintf( stderr, _("Add or modify options:\n"));
00137        fprintf( stderr, _("  -a         add values (%s)\n"),
00138               (ldapadd ? _("default") : _("default is to replace")));
00139        fprintf( stderr, _("  -c         continuous operation mode (do not stop on errors)\n"));
00140        fprintf( stderr, _("  -E [!]ext=extparam  modify extensions"
00141               " (! indicate s criticality)\n"));
00142        fprintf( stderr, _("  -f file    read operations from `file'\n"));
00143        fprintf( stderr, _("  -M         enable Manage DSA IT control (-MM to make critical)\n"));
00144        fprintf( stderr, _("  -P version protocol version (default: 3)\n"));
00145 #ifdef LDAP_X_TXN
00146        fprintf( stderr,
00147               _("             [!]txn=<commit|abort>         (transaction)\n"));
00148 #endif
00149        fprintf( stderr, _("  -S file    write skipped modifications to `file'\n"));
00150 
00151        tool_common_usage();
00152        exit( EXIT_FAILURE );
00153 }
00154 
00155 
00156 const char options[] = "aE:rS:"
00157        "cd:D:e:f:h:H:IMnNO:o:p:P:QR:U:vVw:WxX:y:Y:Z";
00158 
00159 int
00160 handle_private_option( int i )
00161 {
00162        char   *control, *cvalue;
00163        int           crit;
00164 
00165        switch ( i ) {
00166        case 'E': /* modify extensions */
00167               if( protocol == LDAP_VERSION2 ) {
00168                      fprintf( stderr, _("%s: -E incompatible with LDAPv%d\n"),
00169                             prog, protocol );
00170                      exit( EXIT_FAILURE );
00171               }
00172 
00173               /* should be extended to support comma separated list of
00174                *     [!]key[=value] parameters, e.g.  -E !foo,bar=567
00175                */
00176 
00177               crit = 0;
00178               cvalue = NULL;
00179               if( optarg[0] == '!' ) {
00180                      crit = 1;
00181                      optarg++;
00182               }
00183 
00184               control = ber_strdup( optarg );
00185               if ( (cvalue = strchr( control, '=' )) != NULL ) {
00186                      *cvalue++ = '\0';
00187               }
00188 
00189 #ifdef LDAP_X_TXN
00190               if( strcasecmp( control, "txn" ) == 0 ) {
00191                      /* Transaction */
00192                      if( txn ) {
00193                             fprintf( stderr,
00194                                    _("txn control previously specified\n"));
00195                             exit( EXIT_FAILURE );
00196                      }
00197                      if( cvalue != NULL ) {
00198                             if( strcasecmp( cvalue, "abort" ) == 0 ) {
00199                                    txnabort=1;
00200                             } else if( strcasecmp( cvalue, "commit" ) != 0 ) {
00201                                    fprintf( stderr, _("Invalid value for txn control, %s\n"),
00202                                           cvalue );
00203                                    exit( EXIT_FAILURE );
00204                             }
00205                      }
00206 
00207                      txn = 1 + crit;
00208               } else
00209 #endif
00210               {
00211                      fprintf( stderr, _("Invalid modify extension name: %s\n"),
00212                             control );
00213                      usage();
00214               }
00215               break;
00216 
00217        case 'a':     /* add */
00218               ldapadd = 1;
00219               break;
00220 
00221        case 'r':     /* replace (obsolete) */
00222               break;
00223 
00224        case 'S':     /* skipped modifications to file */
00225               if( rejfile != NULL ) {
00226                      fprintf( stderr, _("%s: -S previously specified\n"), prog );
00227                      exit( EXIT_FAILURE );
00228               }
00229               rejfile = ber_strdup( optarg );
00230               break;
00231 
00232        default:
00233               return 0;
00234        }
00235        return 1;
00236 }
00237 
00238 
00239 int
00240 main( int argc, char **argv )
00241 {
00242        char          *rbuf = NULL, *rejbuf = NULL;
00243        FILE          *rejfp;
00244        struct LDIFFP *ldiffp = NULL, ldifdummy = {0};
00245        char          *matched_msg, *error_msg;
00246        int           rc, retval, ldifrc;
00247        int           len;
00248        int           i = 0;
00249        int           lineno, nextline = 0, lmax = 0;
00250        LDAPControl   c[1];
00251 
00252        prog = lutil_progname( "ldapmodify", argc, argv );
00253 
00254        /* strncmp instead of strcmp since NT binaries carry .exe extension */
00255        ldapadd = ( strncasecmp( prog, "ldapadd", sizeof("ldapadd")-1 ) == 0 );
00256 
00257        tool_init( ldapadd ? TOOL_ADD : TOOL_MODIFY );
00258 
00259        tool_args( argc, argv );
00260 
00261        if ( argc != optind ) usage();
00262 
00263        if ( rejfile != NULL ) {
00264               if (( rejfp = fopen( rejfile, "w" )) == NULL ) {
00265                      perror( rejfile );
00266                      retval = EXIT_FAILURE;
00267                      goto fail;
00268               }
00269        } else {
00270               rejfp = NULL;
00271        }
00272 
00273        if ( infile != NULL ) {
00274               if (( ldiffp = ldif_open( infile, "r" )) == NULL ) {
00275                      perror( infile );
00276                      retval = EXIT_FAILURE;
00277                      goto fail;
00278               }
00279        } else {
00280               ldifdummy.fp = stdin;
00281               ldiffp = &ldifdummy;
00282        }
00283 
00284        if ( debug ) ldif_debug = debug;
00285 
00286        ld = tool_conn_setup( dont, 0 );
00287 
00288        if ( !dont ) {
00289               tool_bind( ld );
00290        }
00291 
00292 #ifdef LDAP_X_TXN
00293        if( txn ) {
00294               /* start transaction */
00295               rc = ldap_txn_start_s( ld, NULL, NULL, &txn_id );
00296               if( rc != LDAP_SUCCESS ) {
00297                      tool_perror( "ldap_txn_start_s", rc, NULL, NULL, NULL, NULL );
00298                      if( txn > 1 ) {
00299                             retval = EXIT_FAILURE;
00300                             goto fail;
00301                      }
00302                      txn = 0;
00303               }
00304        }
00305 #endif
00306 
00307        if ( 0
00308 #ifdef LDAP_X_TXN
00309               || txn
00310 #endif
00311               )
00312        {
00313 #ifdef LDAP_X_TXN
00314               if( txn ) {
00315                      c[i].ldctl_oid = LDAP_CONTROL_X_TXN_SPEC;
00316                      c[i].ldctl_value = *txn_id;
00317                      c[i].ldctl_iscritical = 1;
00318                      i++;
00319               }
00320 #endif
00321        }
00322 
00323        tool_server_controls( ld, c, i );
00324 
00325        rc = 0;
00326        retval = 0;
00327        lineno = 1;
00328        while (( rc == 0 || contoper ) && ( ldifrc = ldif_read_record( ldiffp, &nextline,
00329               &rbuf, &lmax )) > 0 )
00330        {
00331               if ( rejfp ) {
00332                      len = strlen( rbuf );
00333                      if (( rejbuf = (char *)ber_memalloc( len+1 )) == NULL ) {
00334                             perror( "malloc" );
00335                             retval = EXIT_FAILURE;
00336                             goto fail;
00337                      }
00338                      memcpy( rejbuf, rbuf, len+1 );
00339               }
00340 
00341               rc = process_ldif_rec( rbuf, lineno );
00342               lineno = nextline+1;
00343 
00344               if ( rc ) retval = rc;
00345               if ( rc && rejfp ) {
00346                      fprintf(rejfp, _("# Error: %s (%d)"), ldap_err2string(rc), rc);
00347 
00348                      matched_msg = NULL;
00349                      ldap_get_option(ld, LDAP_OPT_MATCHED_DN, &matched_msg);
00350                      if ( matched_msg != NULL ) {
00351                             if ( *matched_msg != '\0' ) {
00352                                    fprintf( rejfp, _(", matched DN: %s"), matched_msg );
00353                             }
00354                             ldap_memfree( matched_msg );
00355                      }
00356 
00357                      error_msg = NULL;
00358                      ldap_get_option(ld, LDAP_OPT_DIAGNOSTIC_MESSAGE, &error_msg);
00359                      if ( error_msg != NULL ) {
00360                             if ( *error_msg != '\0' ) {
00361                                    fprintf( rejfp, _(", additional info: %s"), error_msg );
00362                             }
00363                             ldap_memfree( error_msg );
00364                      }
00365                      fprintf( rejfp, "\n%s\n", rejbuf );
00366               }
00367 
00368               if (rejfp) ber_memfree( rejbuf );
00369        }
00370        ber_memfree( rbuf );
00371 
00372        if ( ldifrc < 0 )
00373               retval = LDAP_OTHER;
00374 
00375 #ifdef LDAP_X_TXN
00376        if( retval == 0 && txn ) {
00377               rc = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, NULL );
00378               if ( rc != LDAP_OPT_SUCCESS ) {
00379                      fprintf( stderr, "Could not unset controls for ldap_txn_end\n");
00380               }
00381 
00382               /* create transaction */
00383               rc = ldap_txn_end_s( ld, !txnabort, txn_id, NULL, NULL, NULL );
00384               if( rc != LDAP_SUCCESS ) {
00385                      tool_perror( "ldap_txn_end_s", rc, NULL, NULL, NULL, NULL );
00386                      retval = rc;
00387               }
00388        }
00389 #endif
00390 
00391 fail:;
00392        if ( rejfp != NULL ) {
00393               fclose( rejfp );
00394        }
00395 
00396        if ( ldiffp != NULL && ldiffp != &ldifdummy ) {
00397               ldif_close( ldiffp );
00398        }
00399 
00400        tool_exit( ld, retval );
00401 }
00402 
00403 
00404 static int
00405 process_ldif_rec( char *rbuf, int linenum )
00406 {
00407        char   *line, *dn, *newrdn, *newsup;
00408        int           rc, modop;
00409        int           expect_modop, expect_sep;
00410        int           deleteoldrdn;
00411        int           new_entry, delete_entry, got_all;
00412        LDAPMod       **pmods, *lm = NULL;
00413        int version;
00414        LDAPControl **pctrls;
00415        int i, j, k, lines, idn, nmods;
00416        struct berval *btype, *vals, **bvl, bv;
00417        char *freeval;
00418        unsigned char *mops = NULL;
00419 
00420        new_entry = ldapadd;
00421 
00422        rc = got_all = delete_entry = modop = expect_modop = 0;
00423        expect_sep = 0;
00424        version = 0;
00425        deleteoldrdn = 1;
00426        pmods = NULL;
00427        pctrls = NULL;
00428        dn = newrdn = newsup = NULL;
00429 
00430        lines = ldif_countlines( rbuf );
00431        btype = ber_memcalloc( 1, (lines+1)*2*sizeof(struct berval)+lines );
00432        if ( !btype )
00433               return LDAP_NO_MEMORY;
00434 
00435        vals = btype+lines+1;
00436        freeval = (char *)(vals+lines+1);
00437        i = -1;
00438 
00439        while ( rc == 0 && ( line = ldif_getline( &rbuf )) != NULL ) {
00440               int freev;
00441 
00442               if ( *line == '\n' || *line == '\0' ) {
00443                      break;
00444               }
00445 
00446               ++i;
00447 
00448               if ( line[0] == '-' && !line[1] ) {
00449                      BER_BVZERO( btype+i );
00450                      freeval[i] = 0;
00451                      continue;
00452               }
00453        
00454               if ( ( rc = ldif_parse_line2( line, btype+i, vals+i, &freev ) ) < 0 ) {
00455                      fprintf( stderr, _("%s: invalid format (line %d) entry: \"%s\"\n"),
00456                             prog, linenum+i, dn == NULL ? "" : dn );
00457                      rc = LDAP_PARAM_ERROR;
00458                      goto leave;
00459               }
00460               freeval[i] = freev;
00461 
00462               if ( dn == NULL ) {
00463                      if ( linenum+i == 1 && BV_CASEMATCH( btype+i, &BV_VERSION )) {
00464                             int    v;
00465                             if( vals[i].bv_len == 0 || lutil_atoi( &v, vals[i].bv_val) != 0 || v != 1 ) {
00466                                    fprintf( stderr,
00467                                           _("%s: invalid version %s, line %d (ignored)\n"),
00468                                           prog, vals[i].bv_val, linenum );
00469                             }
00470                             version++;
00471 
00472                      } else if ( BV_CASEMATCH( btype+i, &BV_DN )) {
00473                             dn = vals[i].bv_val;
00474                             idn = i;
00475                      }
00476                      /* skip all lines until we see "dn:" */
00477               }
00478        }
00479 
00480        /* check to make sure there was a dn: line */
00481        if ( !dn ) {
00482               rc = 0;
00483               goto leave;
00484        }
00485 
00486        lines = i+1;
00487 
00488        if( lines == 0 ) {
00489               rc = 0;
00490               goto leave;
00491        }
00492 
00493        if( version && lines == 1 ) {
00494               rc = 0;
00495               goto leave;
00496        }
00497 
00498        i = idn+1;
00499        /* Check for "control" tag after dn and before changetype. */
00500        if ( BV_CASEMATCH( btype+i, &BV_CONTROL )) {
00501               /* Parse and add it to the list of controls */
00502               rc = parse_ldif_control( vals+i, &pctrls );
00503               if (rc != 0) {
00504                      fprintf( stderr,
00505                             _("%s: Error processing %s line, line %d: %s\n"),
00506                             prog, BV_CONTROL.bv_val, linenum+i, ldap_err2string(rc) );
00507               }
00508               i++;
00509               if ( i>= lines ) {
00510 short_input:
00511                      fprintf( stderr,
00512                             _("%s: Expecting more input after %s line, line %d\n"),
00513                             prog, btype[i-1].bv_val, linenum+i );
00514                      
00515                      rc = LDAP_PARAM_ERROR;
00516                      goto leave;
00517               }
00518        }
00519 
00520        /* Check for changetype */
00521        if ( BV_CASEMATCH( btype+i, &BV_CHANGETYPE )) {
00522 #ifdef LIBERAL_CHANGETYPE_MODOP
00523               /* trim trailing spaces (and log warning ...) */
00524               int icnt;
00525               for ( icnt = vals[i].bv_len; --icnt > 0; ) {
00526                      if ( !isspace( (unsigned char) vals[i].bv_val[icnt] ) ) {
00527                             break;
00528                      }
00529               }
00530 
00531               if ( ++icnt != vals[i].bv_len ) {
00532                      fprintf( stderr, _("%s: illegal trailing space after"
00533                             " \"%s: %s\" trimmed (line %d, entry \"%s\")\n"),
00534                             prog, BV_CHANGETYPE.bv_val, vals[i].bv_val, linenum+i, dn );
00535                      vals[i].bv_val[icnt] = '\0';
00536               }
00537 #endif /* LIBERAL_CHANGETYPE_MODOP */
00538 
00539               if ( BV_CASEMATCH( vals+i, &BV_MODIFYCT )) {
00540                      new_entry = 0;
00541                      expect_modop = 1;
00542               } else if ( BV_CASEMATCH( vals+i, &BV_ADDCT )) {
00543                      new_entry = 1;
00544                      modop = LDAP_MOD_ADD;
00545               } else if ( BV_CASEMATCH( vals+i, &BV_MODRDNCT )
00546                      || BV_CASEMATCH( vals+i, &BV_MODDNCT )
00547                      || BV_CASEMATCH( vals+i, &BV_RENAMECT ))
00548               {
00549                      i++;
00550                      if ( i >= lines )
00551                             goto short_input;
00552                      if ( !BV_CASEMATCH( btype+i, &BV_NEWRDN )) {
00553                             fprintf( stderr, _("%s: expecting \"%s:\" but saw"
00554                                    " \"%s:\" (line %d, entry \"%s\")\n"),
00555                                    prog, BV_NEWRDN.bv_val, btype[i].bv_val, linenum+i, dn );
00556                             rc = LDAP_PARAM_ERROR;
00557                             goto leave;
00558                      }
00559                      newrdn = vals[i].bv_val;
00560                      i++;
00561                      if ( i >= lines )
00562                             goto short_input;
00563                      if ( !BV_CASEMATCH( btype+i, &BV_DELETEOLDRDN )) {
00564                             fprintf( stderr, _("%s: expecting \"%s:\" but saw"
00565                                    " \"%s:\" (line %d, entry \"%s\")\n"),
00566                                    prog, BV_DELETEOLDRDN.bv_val, btype[i].bv_val, linenum+i, dn );
00567                             rc = LDAP_PARAM_ERROR;
00568                             goto leave;
00569                      }
00570                      deleteoldrdn = ( vals[i].bv_val[0] == '0' ) ? 0 : 1;
00571                      i++;
00572                      if ( i < lines ) {
00573                             if ( !BV_CASEMATCH( btype+i, &BV_NEWSUP )) {
00574                                    fprintf( stderr, _("%s: expecting \"%s:\" but saw"
00575                                           " \"%s:\" (line %d, entry \"%s\")\n"),
00576                                           prog, BV_NEWSUP.bv_val, btype[i].bv_val, linenum+i, dn );
00577                                    rc = LDAP_PARAM_ERROR;
00578                                    goto leave;
00579                             }
00580                             newsup = vals[i].bv_val;
00581                             i++;
00582                      }
00583                      got_all = 1;
00584               } else if ( BV_CASEMATCH( vals+i, &BV_DELETECT )) {
00585                      got_all = delete_entry = 1;
00586               } else {
00587                      fprintf( stderr,
00588                             _("%s:  unknown %s \"%s\" (line %d, entry \"%s\")\n"),
00589                             prog, BV_CHANGETYPE.bv_val, vals[i].bv_val, linenum+i, dn );
00590                      rc = LDAP_PARAM_ERROR;
00591                      goto leave;
00592               }
00593               i++;
00594        } else if ( ldapadd ) {            /*  missing changetype => add */
00595               new_entry = 1;
00596               modop = LDAP_MOD_ADD;
00597        } else {
00598               expect_modop = 1;    /* missing changetype => modify */
00599        }
00600 
00601        if ( got_all ) {
00602               if ( i < lines ) {
00603                      fprintf( stderr,
00604                             _("%s: extra lines at end (line %d, entry \"%s\")\n"),
00605                             prog, linenum+i, dn );
00606                      rc = LDAP_PARAM_ERROR;
00607                      goto leave;
00608               }
00609               goto doit;
00610        }
00611 
00612        nmods = lines - i;
00613        idn = i;
00614 
00615        if ( new_entry ) {
00616               int fv;
00617 
00618               /* Make sure all attributes with multiple values are contiguous */
00619               for (; i<lines; i++) {
00620                      for (j=i+1; j<lines; j++) {
00621                             if ( BV_CASEMATCH( btype+i, btype+j )) {
00622                                    nmods--;
00623                                    /* out of order, move intervening attributes down */
00624                                    if ( j != i+1 ) {
00625                                           bv = vals[j];
00626                                           fv = freeval[j];
00627                                           for (k=j; k>i; k--) {
00628                                                  btype[k] = btype[k-1];
00629                                                  vals[k] = vals[k-1];
00630                                                  freeval[k] = freeval[k-1];
00631                                           }
00632                                           k++;
00633                                           btype[k] = btype[i];
00634                                           vals[k] = bv;
00635                                           freeval[k] = fv;
00636                                    }
00637                                    i++;
00638                             }
00639                      }
00640               }
00641               /* Allocate space for array of mods, array of pointers to mods,
00642                * and array of pointers to values, allowing for NULL terminators
00643                * for the pointer arrays...
00644                */
00645               lm = ber_memalloc( nmods * sizeof(LDAPMod) +
00646                      (nmods+1) * sizeof(LDAPMod*) +
00647                      (lines + nmods - idn) * sizeof(struct berval *));
00648               pmods = (LDAPMod **)(lm+nmods);
00649               bvl = (struct berval **)(pmods+nmods+1);
00650 
00651               j = 0;
00652               k = -1;
00653               BER_BVZERO(&bv);
00654               for (i=idn; i<lines; i++) {
00655                      if ( BV_CASEMATCH( btype+i, &BV_DN )) {
00656                             fprintf( stderr, _("%s: attributeDescription \"%s\":"
00657                                    " (possible missing newline"
00658                                           " after line %d, entry \"%s\"?)\n"),
00659                                    prog, btype[i].bv_val, linenum+i - 1, dn );
00660                      }
00661                      if ( !BV_CASEMATCH( btype+i, &bv )) {
00662                             bvl[k++] = NULL;
00663                             bv = btype[i];
00664                             lm[j].mod_op = LDAP_MOD_ADD | LDAP_MOD_BVALUES;
00665                             lm[j].mod_type = bv.bv_val;
00666                             lm[j].mod_bvalues = bvl+k;
00667                             pmods[j] = lm+j;
00668                             j++;
00669                      }
00670                      bvl[k++] = vals+i;
00671               }
00672               bvl[k] = NULL;
00673               pmods[j] = NULL;
00674               goto doit;
00675        }
00676 
00677        mops = ber_memalloc( lines+1 );
00678        mops[lines] = M_SEP;
00679        mops[i-1] = M_SEP;
00680 
00681        for ( ; i<lines; i++ ) {
00682               if ( expect_modop ) {
00683 #ifdef LIBERAL_CHANGETYPE_MODOP
00684                      /* trim trailing spaces (and log warning ...) */
00685                   int icnt;
00686                   for ( icnt = vals[i].bv_len; --icnt > 0; ) {
00687                             if ( !isspace( (unsigned char) vals[i].bv_val[icnt] ) ) break;
00688                      }
00689     
00690                      if ( ++icnt != vals[i].bv_len ) {
00691                             fprintf( stderr, _("%s: illegal trailing space after"
00692                                    " \"%s: %s\" trimmed (line %d, entry \"%s\")\n"),
00693                                    prog, type, vals[i].bv_val, linenum+i, dn );
00694                             vals[i].bv_val[icnt] = '\0';
00695                      }
00696 #endif /* LIBERAL_CHANGETYPE_MODOP */
00697 
00698                      expect_modop = 0;
00699                      expect_sep = 1;
00700                      if ( BV_CASEMATCH( btype+i, &BV_MODOPADD )) {
00701                             modop = LDAP_MOD_ADD;
00702                             mops[i] = M_SEP;
00703                             nmods--;
00704                      } else if ( BV_CASEMATCH( btype+i, &BV_MODOPREPLACE )) {
00705                      /* defer handling these since they might have no values.
00706                       * Use the BVALUES flag to signal that these were
00707                       * deferred. If values are provided later, this
00708                       * flag will be switched off.
00709                       */
00710                             modop = LDAP_MOD_REPLACE;
00711                             mops[i] = modop | LDAP_MOD_BVALUES;
00712                             btype[i] = vals[i];
00713                      } else if ( BV_CASEMATCH( btype+i, &BV_MODOPDELETE )) {
00714                             modop = LDAP_MOD_DELETE;
00715                             mops[i] = modop | LDAP_MOD_BVALUES;
00716                             btype[i] = vals[i];
00717                      } else if ( BV_CASEMATCH( btype+i, &BV_MODOPINCREMENT )) {
00718                             modop = LDAP_MOD_INCREMENT;
00719                             mops[i] = M_SEP;
00720                             nmods--;
00721                      } else {      /* no modify op: invalid LDIF */
00722                             fprintf( stderr, _("%s: modify operation type is missing at"
00723                                    " line %d, entry \"%s\"\n"),
00724                                    prog, linenum+i, dn );
00725                             rc = LDAP_PARAM_ERROR;
00726                             goto leave;
00727                      }
00728                      bv = vals[i];
00729               } else if ( expect_sep && BER_BVISEMPTY( btype+i )) {
00730                      mops[i] = M_SEP;
00731                      expect_sep = 0;
00732                      expect_modop = 1;
00733                      nmods--;
00734               } else {
00735                      if ( !BV_CASEMATCH( btype+i, &bv )) {
00736                             fprintf( stderr, _("%s: wrong attributeType at"
00737                                    " line %d, entry \"%s\"\n"),
00738                                    prog, linenum+i, dn );
00739                             rc = LDAP_PARAM_ERROR;
00740                             goto leave;
00741                      }
00742                      mops[i] = modop;
00743                      /* If prev op was deferred and matches this type,
00744                       * clear the flag
00745                       */
00746                      if ( (mops[i-1] & LDAP_MOD_BVALUES)
00747                             && BV_CASEMATCH( btype+i, btype+i-1 ))
00748                      {
00749                             mops[i-1] = M_SEP;
00750                             nmods--;
00751                      }
00752               }
00753        }
00754 
00755 #if 0  /* we should faithfully encode the LDIF, not combine */
00756        /* Make sure all modops with multiple values are contiguous */
00757        for (i=idn; i<lines; i++) {
00758               if ( mops[i] == M_SEP )
00759                      continue;
00760               for (j=i+1; j<lines; j++) {
00761                      if ( mops[j] == M_SEP || mops[i] != mops[j] )
00762                             continue;
00763                      if ( BV_CASEMATCH( btype+i, btype+j )) {
00764                             nmods--;
00765                             /* out of order, move intervening attributes down */
00766                             if ( j != i+1 ) {
00767                                    int c;
00768                                    struct berval bv;
00769                                    char fv;
00770 
00771                                    c = mops[j];
00772                                    bv = vals[j];
00773                                    fv = freeval[j];
00774                                    for (k=j; k>i; k--) {
00775                                           btype[k] = btype[k-1];
00776                                           vals[k] = vals[k-1];
00777                                           freeval[k] = freeval[k-1];
00778                                           mops[k] = mops[k-1];
00779                                    }
00780                                    k++;
00781                                    btype[k] = btype[i];
00782                                    vals[k] = bv;
00783                                    freeval[k] = fv;
00784                                    mops[k] = c;
00785                             }
00786                             i++;
00787                      }
00788               }
00789        }
00790 #endif
00791 
00792        /* Allocate space for array of mods, array of pointers to mods,
00793         * and array of pointers to values, allowing for NULL terminators
00794         * for the pointer arrays...
00795         */
00796        lm = ber_memalloc( nmods * sizeof(LDAPMod) +
00797               (nmods+1) * sizeof(LDAPMod*) +
00798               (lines + nmods - idn) * sizeof(struct berval *));
00799        pmods = (LDAPMod **)(lm+nmods);
00800        bvl = (struct berval **)(pmods+nmods+1);
00801 
00802        j = 0;
00803        k = -1;
00804        BER_BVZERO(&bv);
00805        mops[idn-1] = M_SEP;
00806        for (i=idn; i<lines; i++) {
00807               if ( mops[i] == M_SEP )
00808                      continue;
00809               if ( mops[i] != mops[i-1] || !BV_CASEMATCH( btype+i, &bv )) {
00810                      bvl[k++] = NULL;
00811                      bv = btype[i];
00812                      lm[j].mod_op = mops[i] | LDAP_MOD_BVALUES;
00813                      lm[j].mod_type = bv.bv_val;
00814                      if ( mops[i] & LDAP_MOD_BVALUES ) {
00815                             lm[j].mod_bvalues = NULL;
00816                      } else {
00817                             lm[j].mod_bvalues = bvl+k;
00818                      }
00819                      pmods[j] = lm+j;
00820                      j++;
00821               }
00822               bvl[k++] = vals+i;
00823        }
00824        bvl[k] = NULL;
00825        pmods[j] = NULL;
00826 
00827 doit:
00828        /* If default controls are set (as with -M option) and controls are
00829           specified in the LDIF file, we must add the default controls to
00830           the list of controls sent with the ldap operation.
00831        */
00832        if ( rc == 0 ) {
00833               if (pctrls) {
00834                      LDAPControl **defctrls = NULL;   /* Default server controls */
00835                      LDAPControl **newctrls = NULL;
00836                      ldap_get_option(ld, LDAP_OPT_SERVER_CONTROLS, &defctrls);
00837                      if (defctrls) {
00838                             int npc=0;                       /* Num of LDIF controls */
00839                             int ndefc=0;                     /* Num of default controls */
00840                             while (pctrls[npc]) npc++;       /* Count LDIF controls */
00841                             while (defctrls[ndefc]) ndefc++; /* Count default controls */
00842                             newctrls = ber_memrealloc(pctrls,
00843                                    (npc+ndefc+1)*sizeof(LDAPControl*));
00844 
00845                             if (newctrls == NULL) {
00846                                    rc = LDAP_NO_MEMORY;
00847                             } else {
00848                                    int i;
00849                                    pctrls = newctrls;
00850                                    for (i=npc; i<npc+ndefc; i++) {
00851                                           pctrls[i] = ldap_control_dup(defctrls[i-npc]);
00852                                           if (pctrls[i] == NULL) {
00853                                                  rc = LDAP_NO_MEMORY;
00854                                                  break;
00855                                           }
00856                                    }
00857                                    pctrls[npc+ndefc] = NULL;
00858                             }
00859                             ldap_controls_free(defctrls);  /* Must be freed by library */
00860                      }
00861               }
00862        }
00863 
00864        if ( rc == 0 ) {
00865               if ( delete_entry ) {
00866                      rc = dodelete( dn, pctrls );
00867               } else if ( newrdn != NULL ) {
00868                      rc = dorename( dn, newrdn, newsup, deleteoldrdn, pctrls );
00869               } else {
00870                      rc = domodify( dn, pmods, pctrls, new_entry );
00871               }
00872 
00873               if ( rc == LDAP_SUCCESS ) {
00874                      rc = 0;
00875               }
00876        }
00877 
00878 leave:
00879     if (pctrls != NULL) {
00880        ldap_controls_free( pctrls );
00881        }
00882        if ( lm != NULL ) {
00883               ber_memfree( lm );
00884        }
00885        if ( mops != NULL ) {
00886               ber_memfree( mops );
00887        }
00888        for (i=lines-1; i>=0; i--)
00889               if ( freeval[i] ) ber_memfree( vals[i].bv_val );
00890        ber_memfree( btype );
00891 
00892        return( rc );
00893 }
00894 
00895 /* Parse an LDIF control line of the form
00896       control:  oid  [true/false]  [: value]              or
00897       control:  oid  [true/false]  [:: base64-value]      or
00898       control:  oid  [true/false]  [:< url]
00899    The control is added to the list of controls in *ppctrls.
00900 */      
00901 static int
00902 parse_ldif_control(
00903        struct berval *bval,
00904        LDAPControl ***ppctrls )
00905 {
00906        char *oid = NULL;
00907        int criticality = 0;   /* Default is false if not present */
00908        int i, rc=0;
00909        char *s, *oidStart;
00910        LDAPControl *newctrl = NULL;
00911        LDAPControl **pctrls = NULL;
00912        struct berval type, bv = BER_BVNULL;
00913        int freeval = 0;
00914 
00915        if (ppctrls) pctrls = *ppctrls;
00916        /* OID should come first. Validate and extract it. */
00917        s = bval->bv_val;
00918        if (*s == 0) return ( LDAP_PARAM_ERROR );
00919        oidStart = s;
00920        while (isdigit((unsigned char)*s) || *s == '.') {
00921               s++;                           /* OID should be digits or . */
00922        }
00923        if (s == oidStart) { 
00924               return ( LDAP_PARAM_ERROR );   /* OID was not present */
00925        }
00926        if (*s) {                          /* End of OID should be space or NULL */
00927               if (!isspace((unsigned char)*s)) {
00928                      return ( LDAP_PARAM_ERROR ); /* else OID contained invalid chars */
00929               }
00930               *s++ = 0;                    /* Replace space with null to terminate */
00931        }
00932 
00933        oid = ber_strdup(oidStart);
00934        if (oid == NULL) return ( LDAP_NO_MEMORY );
00935 
00936        /* Optional Criticality field is next. */
00937        while (*s && isspace((unsigned char)*s)) {
00938               s++;                         /* Skip white space before criticality */
00939        }
00940        if (strncasecmp(s, "true", 4) == 0) {
00941               criticality = 1;
00942               s += 4;
00943        } 
00944        else if (strncasecmp(s, "false", 5) == 0) {
00945               criticality = 0;
00946               s += 5;
00947        }
00948 
00949        /* Optional value field is next */
00950        while (*s && isspace((unsigned char)*s)) {
00951               s++;                         /* Skip white space before value */
00952        }
00953        if (*s) {
00954               if (*s != ':') {           /* If value is present, must start with : */
00955                      rc = LDAP_PARAM_ERROR;
00956                      goto cleanup;
00957               }
00958 
00959               /* Back up so value is in the form
00960                    a: value
00961                    a:: base64-value
00962                    a:< url
00963                  Then we can use ldif_parse_line2 to extract and decode the value
00964               */
00965               s--;
00966               *s = 'a';
00967 
00968               rc = ldif_parse_line2(s, &type, &bv, &freeval);
00969               if (rc < 0) {
00970                      rc = LDAP_PARAM_ERROR;
00971                      goto cleanup;
00972               }
00973     }
00974 
00975        /* Create a new LDAPControl structure. */
00976        newctrl = (LDAPControl *)ber_memalloc(sizeof(LDAPControl));
00977        if ( newctrl == NULL ) {
00978               rc = LDAP_NO_MEMORY;
00979               goto cleanup;
00980        }
00981        newctrl->ldctl_oid = oid;
00982        oid = NULL;
00983        newctrl->ldctl_iscritical = criticality;
00984        if ( freeval )
00985               newctrl->ldctl_value = bv;
00986        else
00987               ber_dupbv( &newctrl->ldctl_value, &bv );
00988 
00989        /* Add the new control to the passed-in list of controls. */
00990        i = 0;
00991        if (pctrls) {
00992               while ( pctrls[i] ) {    /* Count the # of controls passed in */
00993                      i++;
00994               }
00995        }
00996        /* Allocate 1 more slot for the new control and 1 for the NULL. */
00997        pctrls = (LDAPControl **) ber_memrealloc(pctrls,
00998               (i+2)*(sizeof(LDAPControl *)));
00999        if (pctrls == NULL) {
01000               rc = LDAP_NO_MEMORY;
01001               goto cleanup;
01002        }
01003        pctrls[i] = newctrl;
01004        newctrl = NULL;
01005        pctrls[i+1] = NULL;
01006        *ppctrls = pctrls;
01007 
01008 cleanup:
01009        if (newctrl) {
01010               if (newctrl->ldctl_oid) ber_memfree(newctrl->ldctl_oid);
01011               if (newctrl->ldctl_value.bv_val) {
01012                      ber_memfree(newctrl->ldctl_value.bv_val);
01013               }
01014               ber_memfree(newctrl);
01015        }
01016        if (oid) ber_memfree(oid);
01017 
01018        return( rc );
01019 }
01020 
01021 
01022 static int
01023 domodify(
01024        const char *dn,
01025        LDAPMod **pmods,
01026        LDAPControl **pctrls,
01027        int newentry )
01028 {
01029        int                  rc, i, j, k, notascii, op;
01030        struct berval *bvp;
01031 
01032        if ( dn == NULL ) {
01033               fprintf( stderr, _("%s: no DN specified\n"), prog );
01034               return( LDAP_PARAM_ERROR );
01035        }
01036 
01037        if ( pmods == NULL ) {
01038               /* implement "touch" (empty sequence)
01039                * modify operation (note that there
01040                * is no symmetry with the UNIX command,
01041                * since \"touch\" on a non-existent entry
01042                * will fail)*/
01043               printf( "warning: no attributes to %sadd (entry=\"%s\")\n",
01044                      newentry ? "" : "change or ", dn );
01045 
01046        } else {
01047               for ( i = 0; pmods[ i ] != NULL; ++i ) {
01048                      op = pmods[ i ]->mod_op & ~LDAP_MOD_BVALUES;
01049                      if( op == LDAP_MOD_ADD && ( pmods[i]->mod_bvalues == NULL )) {
01050                             fprintf( stderr,
01051                                    _("%s: attribute \"%s\" has no values (entry=\"%s\")\n"),
01052                                    prog, pmods[i]->mod_type, dn );
01053                             return LDAP_PARAM_ERROR;
01054                      }
01055               }
01056 
01057               if ( verbose ) {
01058                      for ( i = 0; pmods[ i ] != NULL; ++i ) {
01059                             op = pmods[ i ]->mod_op & ~LDAP_MOD_BVALUES;
01060                             printf( "%s %s:\n",
01061                                    op == LDAP_MOD_REPLACE ? _("replace") :
01062                                           op == LDAP_MOD_ADD ?  _("add") :
01063                                                  op == LDAP_MOD_INCREMENT ?  _("increment") :
01064                                                         op == LDAP_MOD_DELETE ?  _("delete") :
01065                                                                _("unknown"),
01066                                    pmods[ i ]->mod_type );
01067        
01068                             if ( pmods[ i ]->mod_bvalues != NULL ) {
01069                                    for ( j = 0; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
01070                                           bvp = pmods[ i ]->mod_bvalues[ j ];
01071                                           notascii = 0;
01072                                           for ( k = 0; (unsigned long) k < bvp->bv_len; ++k ) {
01073                                                  if ( !isascii( bvp->bv_val[ k ] )) {
01074                                                         notascii = 1;
01075                                                         break;
01076                                                  }
01077                                           }
01078                                           if ( notascii ) {
01079                                                  printf( _("\tNOT ASCII (%ld bytes)\n"), bvp->bv_len );
01080                                           } else {
01081                                                  printf( "\t%s\n", bvp->bv_val );
01082                                           }
01083                                    }
01084                             }
01085                      }
01086               }
01087        }
01088 
01089        if ( newentry ) {
01090               printf( "%sadding new entry \"%s\"\n", dont ? "!" : "", dn );
01091        } else {
01092               printf( "%smodifying entry \"%s\"\n", dont ? "!" : "", dn );
01093        }
01094 
01095        if ( !dont ) {
01096               int    msgid;
01097               if ( newentry ) {
01098                      rc = ldap_add_ext( ld, dn, pmods, pctrls, NULL, &msgid );
01099               } else {
01100                      rc = ldap_modify_ext( ld, dn, pmods, pctrls, NULL, &msgid );
01101               }
01102 
01103               if ( rc != LDAP_SUCCESS ) {
01104                      /* print error message about failed update including DN */
01105                      fprintf( stderr, _("%s: update failed: %s\n"), prog, dn );
01106                      tool_perror( newentry ? "ldap_add" : "ldap_modify",
01107                             rc, NULL, NULL, NULL, NULL );
01108                      goto done;
01109               }
01110               rc = process_response( ld, msgid,
01111                      newentry ? LDAP_RES_ADD : LDAP_RES_MODIFY, dn );
01112 
01113               if ( verbose && rc == LDAP_SUCCESS ) {
01114                      printf( _("modify complete\n") );
01115               }
01116 
01117        } else {
01118               rc = LDAP_SUCCESS;
01119        }
01120 
01121 done:
01122        putchar( '\n' );
01123        return rc;
01124 }
01125 
01126 
01127 static int
01128 dodelete(
01129        const char *dn,
01130        LDAPControl **pctrls )
01131 {
01132        int    rc;
01133        int msgid;
01134 
01135        printf( _("%sdeleting entry \"%s\"\n"), dont ? "!" : "", dn );
01136        if ( !dont ) {
01137               rc = ldap_delete_ext( ld, dn, pctrls, NULL, &msgid );
01138               if ( rc != LDAP_SUCCESS ) {
01139                      fprintf( stderr, _("%s: delete failed: %s\n"), prog, dn );
01140                      tool_perror( "ldap_delete", rc, NULL, NULL, NULL, NULL );
01141                      goto done;
01142               }
01143               rc = process_response( ld, msgid, LDAP_RES_DELETE, dn );
01144 
01145               if ( verbose && rc == LDAP_SUCCESS ) {
01146                      printf( _("delete complete\n") );
01147               }
01148        } else {
01149               rc = LDAP_SUCCESS;
01150        }
01151 
01152 done:
01153        putchar( '\n' );
01154        return( rc );
01155 }
01156 
01157 
01158 static int
01159 dorename(
01160        const char *dn,
01161        const char *newrdn,
01162        const char* newsup,
01163        int deleteoldrdn,
01164        LDAPControl **pctrls )
01165 {
01166        int    rc;
01167        int msgid;
01168 
01169        printf( _("%smodifying rdn of entry \"%s\"\n"), dont ? "!" : "", dn );
01170        if ( verbose ) {
01171               printf( _("\tnew RDN: \"%s\" (%skeep existing values)\n"),
01172                      newrdn, deleteoldrdn ? _("do not ") : "" );
01173        }
01174        if ( !dont ) {
01175               rc = ldap_rename( ld, dn, newrdn, newsup, deleteoldrdn,
01176                      pctrls, NULL, &msgid );
01177               if ( rc != LDAP_SUCCESS ) {
01178                      fprintf( stderr, _("%s: rename failed: %s\n"), prog, dn );
01179                      tool_perror( "ldap_rename", rc, NULL, NULL, NULL, NULL );
01180                      goto done;
01181               }
01182               rc = process_response( ld, msgid, LDAP_RES_RENAME, dn );
01183 
01184               if ( verbose && rc == LDAP_SUCCESS ) {
01185                      printf( _("rename complete\n") );
01186               }
01187        } else {
01188               rc = LDAP_SUCCESS;
01189        }
01190 
01191 done:
01192        putchar( '\n' );
01193        return( rc );
01194 }
01195 
01196 static const char *
01197 res2str( int res ) {
01198        switch ( res ) {
01199        case LDAP_RES_ADD:
01200               return "ldap_add";
01201        case LDAP_RES_DELETE:
01202               return "ldap_delete";
01203        case LDAP_RES_MODIFY:
01204               return "ldap_modify";
01205        case LDAP_RES_MODRDN:
01206               return "ldap_rename";
01207        default:
01208               assert( 0 );
01209        }
01210 
01211        return "ldap_unknown";
01212 }
01213 
01214 static int process_response(
01215        LDAP *ld,
01216        int msgid,
01217        int op,
01218        const char *dn )
01219 {
01220        LDAPMessage   *res;
01221        int           rc = LDAP_OTHER, msgtype;
01222        struct timeval       tv = { 0, 0 };
01223        int           err;
01224        char          *text = NULL, *matched = NULL, **refs = NULL;
01225        LDAPControl   **ctrls = NULL;
01226 
01227        for ( ; ; ) {
01228               tv.tv_sec = 0;
01229               tv.tv_usec = 100000;
01230 
01231               rc = ldap_result( ld, msgid, LDAP_MSG_ALL, &tv, &res );
01232               if ( tool_check_abandon( ld, msgid ) ) {
01233                      return LDAP_CANCELLED;
01234               }
01235 
01236               if ( rc == -1 ) {
01237                      ldap_get_option( ld, LDAP_OPT_RESULT_CODE, &rc );
01238                      tool_perror( "ldap_result", rc, NULL, NULL, NULL, NULL );
01239                      return rc;
01240               }
01241 
01242               if ( rc != 0 ) {
01243                      break;
01244               }
01245        }
01246 
01247        msgtype = ldap_msgtype( res );
01248 
01249        rc = ldap_parse_result( ld, res, &err, &matched, &text, &refs, &ctrls, 1 );
01250        if ( rc == LDAP_SUCCESS ) rc = err;
01251 
01252 #ifdef LDAP_X_TXN
01253        if ( rc == LDAP_X_TXN_SPECIFY_OKAY ) {
01254               rc = LDAP_SUCCESS;
01255        } else
01256 #endif
01257        if ( rc != LDAP_SUCCESS ) {
01258               tool_perror( res2str( op ), rc, NULL, matched, text, refs );
01259        } else if ( msgtype != op ) {
01260               fprintf( stderr, "%s: msgtype: expected %d got %d\n",
01261                      res2str( op ), op, msgtype );
01262               rc = LDAP_OTHER;
01263        }
01264 
01265        if ( text ) ldap_memfree( text );
01266        if ( matched ) ldap_memfree( matched );
01267        if ( refs ) ber_memvfree( (void **)refs );
01268 
01269        if ( ctrls ) {
01270               tool_print_ctrls( ld, ctrls );
01271               ldap_controls_free( ctrls );
01272        }
01273 
01274        return rc;
01275 }