]> git.sur5r.net Git - openldap/blob - clients/tools/ldapmodify.c
happy belated New Year
[openldap] / clients / tools / ldapmodify.c
1 /* ldapmodify.c - generic program to modify or add entries using LDAP */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1998-2010 The OpenLDAP Foundation.
6  * Portions Copyright 2006 Howard Chu.
7  * Portions Copyright 1998-2003 Kurt D. Zeilenga.
8  * Portions Copyright 1998-2001 Net Boolean Incorporated.
9  * Portions Copyright 2001-2003 IBM Corporation.
10  * All rights reserved.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted only as authorized by the OpenLDAP
14  * Public License.
15  *
16  * A copy of this license is available in the file LICENSE in the
17  * top-level directory of the distribution or, alternatively, at
18  * <http://www.OpenLDAP.org/license.html>.
19  */
20 /* Portions Copyright (c) 1992-1996 Regents of the University of Michigan.
21  * All rights reserved.
22  *
23  * Redistribution and use in source and binary forms are permitted
24  * provided that this notice is preserved and that due credit is given
25  * to the University of Michigan at Ann Arbor.  The name of the
26  * University may not be used to endorse or promote products derived
27  * from this software without specific prior written permission.  This
28  * software is provided ``as is'' without express or implied warranty.
29  */
30 /* ACKNOWLEDGEMENTS:
31  * This work was originally developed by the University of Michigan
32  * (as part of U-MICH LDAP).  Additional significant contributors
33  * include:
34  *   Kurt D. Zeilenga
35  *   Norbert Klasen
36  *   Howard Chu
37  */
38
39 #include "portable.h"
40
41 #include <stdio.h>
42
43 #include <ac/stdlib.h>
44 #include <ac/ctype.h>
45 #include <ac/string.h>
46 #include <ac/unistd.h>
47 #include <ac/socket.h>
48 #include <ac/time.h>
49
50 #ifdef HAVE_SYS_STAT_H
51 #include <sys/stat.h>
52 #endif
53
54 #ifdef HAVE_SYS_FILE_H
55 #include <sys/file.h>
56 #endif
57 #ifdef HAVE_FCNTL_H
58 #include <fcntl.h>
59 #endif
60
61 #include <ldap.h>
62
63 #include "lutil.h"
64 #include "lutil_ldap.h"
65 #include "ldif.h"
66 #include "ldap_defaults.h"
67 #include "ldap_log.h"
68 #include "ldap_pvt.h"
69 #include "lber_pvt.h"
70
71 #include "common.h"
72
73 static int      ldapadd;
74 static char *rejfile = NULL;
75 static LDAP     *ld = NULL;
76
77 static int process_ldif_rec LDAP_P(( char *rbuf, int lineno ));
78 static int domodify LDAP_P((
79         const struct berval *dn,
80         LDAPMod **pmods,
81         LDAPControl **pctrls,
82         int newentry ));
83 static int dodelete LDAP_P((
84         const struct berval *dn,
85         LDAPControl **pctrls ));
86 static int dorename LDAP_P((
87         const struct berval *dn,
88         const struct berval *newrdn,
89         const struct berval *newsup,
90         int deleteoldrdn,
91         LDAPControl **pctrls ));
92 static int process_response(
93         LDAP *ld,
94         int msgid,
95         int res,
96         const struct berval *dn );
97
98 #ifdef LDAP_X_TXN
99 static int txn = 0;
100 static int txnabort = 0;
101 struct berval *txn_id = NULL;
102 #endif
103
104 void
105 usage( void )
106 {
107         fprintf( stderr, _("Add or modify entries from an LDAP server\n\n"));
108         fprintf( stderr, _("usage: %s [options]\n"), prog);
109         fprintf( stderr, _("    The list of desired operations are read from stdin"
110                 " or from the file\n"));
111         fprintf( stderr, _("    specified by \"-f file\".\n"));
112         fprintf( stderr, _("Add or modify options:\n"));
113         fprintf( stderr, _("  -a         add values (%s)\n"),
114                 (ldapadd ? _("default") : _("default is to replace")));
115         fprintf( stderr, _("  -c         continuous operation mode (do not stop on errors)\n"));
116         fprintf( stderr, _("  -E [!]ext=extparam        modify extensions"
117                 " (! indicate s criticality)\n"));
118         fprintf( stderr, _("  -f file    read operations from `file'\n"));
119         fprintf( stderr, _("  -M         enable Manage DSA IT control (-MM to make critical)\n"));
120         fprintf( stderr, _("  -P version protocol version (default: 3)\n"));
121 #ifdef LDAP_X_TXN
122         fprintf( stderr,
123                 _("             [!]txn=<commit|abort>         (transaction)\n"));
124 #endif
125         fprintf( stderr, _("  -S file    write skipped modifications to `file'\n"));
126
127         tool_common_usage();
128         exit( EXIT_FAILURE );
129 }
130
131
132 const char options[] = "aE:rS:"
133         "cd:D:e:f:h:H:IMnNO:o:p:P:QR:U:vVw:WxX:y:Y:Z";
134
135 int
136 handle_private_option( int i )
137 {
138         char    *control, *cvalue;
139         int             crit;
140
141         switch ( i ) {
142         case 'E': /* modify extensions */
143                 if( protocol == LDAP_VERSION2 ) {
144                         fprintf( stderr, _("%s: -E incompatible with LDAPv%d\n"),
145                                 prog, protocol );
146                         exit( EXIT_FAILURE );
147                 }
148
149                 /* should be extended to support comma separated list of
150                  *      [!]key[=value] parameters, e.g.  -E !foo,bar=567
151                  */
152
153                 crit = 0;
154                 cvalue = NULL;
155                 if( optarg[0] == '!' ) {
156                         crit = 1;
157                         optarg++;
158                 }
159
160                 control = ber_strdup( optarg );
161                 if ( (cvalue = strchr( control, '=' )) != NULL ) {
162                         *cvalue++ = '\0';
163                 }
164
165 #ifdef LDAP_X_TXN
166                 if( strcasecmp( control, "txn" ) == 0 ) {
167                         /* Transaction */
168                         if( txn ) {
169                                 fprintf( stderr,
170                                         _("txn control previously specified\n"));
171                                 exit( EXIT_FAILURE );
172                         }
173                         if( cvalue != NULL ) {
174                                 if( strcasecmp( cvalue, "abort" ) == 0 ) {
175                                         txnabort=1;
176                                 } else if( strcasecmp( cvalue, "commit" ) != 0 ) {
177                                         fprintf( stderr, _("Invalid value for txn control, %s\n"),
178                                                 cvalue );
179                                         exit( EXIT_FAILURE );
180                                 }
181                         }
182
183                         txn = 1 + crit;
184                 } else
185 #endif
186                 {
187                         fprintf( stderr, _("Invalid modify extension name: %s\n"),
188                                 control );
189                         usage();
190                 }
191                 break;
192
193         case 'a':       /* add */
194                 ldapadd = 1;
195                 break;
196
197         case 'r':       /* replace (obsolete) */
198                 break;
199
200         case 'S':       /* skipped modifications to file */
201                 if( rejfile != NULL ) {
202                         fprintf( stderr, _("%s: -S previously specified\n"), prog );
203                         exit( EXIT_FAILURE );
204                 }
205                 rejfile = ber_strdup( optarg );
206                 break;
207
208         default:
209                 return 0;
210         }
211         return 1;
212 }
213
214
215 int
216 main( int argc, char **argv )
217 {
218         char            *rbuf = NULL, *rejbuf = NULL;
219         FILE            *rejfp;
220         struct LDIFFP *ldiffp, ldifdummy = {0};
221         char            *matched_msg, *error_msg;
222         int             rc, retval, ldifrc;
223         int             len;
224         int             i = 0;
225         int             lineno, nextline = 0, lmax = 0;
226         LDAPControl     c[1];
227
228         prog = lutil_progname( "ldapmodify", argc, argv );
229
230         /* strncmp instead of strcmp since NT binaries carry .exe extension */
231         ldapadd = ( strncasecmp( prog, "ldapadd", sizeof("ldapadd")-1 ) == 0 );
232
233         tool_init( ldapadd ? TOOL_ADD : TOOL_MODIFY );
234
235         tool_args( argc, argv );
236
237         if ( argc != optind ) usage();
238
239         if ( rejfile != NULL ) {
240                 if (( rejfp = fopen( rejfile, "w" )) == NULL ) {
241                         perror( rejfile );
242                         retval = EXIT_FAILURE;
243                         goto fail;
244                 }
245         } else {
246                 rejfp = NULL;
247         }
248
249         if ( infile != NULL ) {
250                 if (( ldiffp = ldif_open( infile, "r" )) == NULL ) {
251                         perror( infile );
252                         retval = EXIT_FAILURE;
253                         goto fail;
254                 }
255         } else {
256                 ldifdummy.fp = stdin;
257                 ldiffp = &ldifdummy;
258         }
259
260         if ( debug ) ldif_debug = debug;
261
262         ld = tool_conn_setup( dont, 0 );
263
264         if ( !dont ) {
265                 tool_bind( ld );
266         }
267
268 #ifdef LDAP_X_TXN
269         if( txn ) {
270                 /* start transaction */
271                 rc = ldap_txn_start_s( ld, NULL, NULL, &txn_id );
272                 if( rc != LDAP_SUCCESS ) {
273                         tool_perror( "ldap_txn_start_s", rc, NULL, NULL, NULL, NULL );
274                         if( txn > 1 ) {
275                                 retval = EXIT_FAILURE;
276                                 goto fail;
277                         }
278                         txn = 0;
279                 }
280         }
281 #endif
282
283         if ( 0
284 #ifdef LDAP_X_TXN
285                 || txn
286 #endif
287                 )
288         {
289 #ifdef LDAP_X_TXN
290                 if( txn ) {
291                         c[i].ldctl_oid = LDAP_CONTROL_X_TXN_SPEC;
292                         c[i].ldctl_value = *txn_id;
293                         c[i].ldctl_iscritical = 1;
294                         i++;
295                 }
296 #endif
297         }
298
299         tool_server_controls( ld, c, i );
300
301         rc = 0;
302         retval = 0;
303         lineno = 1;
304         while (( rc == 0 || contoper ) && ( ldifrc = ldif_read_record( ldiffp, &nextline,
305                 &rbuf, &lmax )) > 0 )
306         {
307                 if ( rejfp ) {
308                         len = strlen( rbuf );
309                         if (( rejbuf = (char *)ber_memalloc( len+1 )) == NULL ) {
310                                 perror( "malloc" );
311                                 retval = EXIT_FAILURE;
312                                 goto fail;
313                         }
314                         memcpy( rejbuf, rbuf, len+1 );
315                 }
316
317                 rc = process_ldif_rec( rbuf, lineno );
318                 lineno = nextline+1;
319
320                 if ( rc ) retval = rc;
321                 if ( rc && rejfp ) {
322                         fprintf(rejfp, _("# Error: %s (%d)"), ldap_err2string(rc), rc);
323
324                         matched_msg = NULL;
325                         ldap_get_option(ld, LDAP_OPT_MATCHED_DN, &matched_msg);
326                         if ( matched_msg != NULL ) {
327                                 if ( *matched_msg != '\0' ) {
328                                         fprintf( rejfp, _(", matched DN: %s"), matched_msg );
329                                 }
330                                 ldap_memfree( matched_msg );
331                         }
332
333                         error_msg = NULL;
334                         ldap_get_option(ld, LDAP_OPT_DIAGNOSTIC_MESSAGE, &error_msg);
335                         if ( error_msg != NULL ) {
336                                 if ( *error_msg != '\0' ) {
337                                         fprintf( rejfp, _(", additional info: %s"), error_msg );
338                                 }
339                                 ldap_memfree( error_msg );
340                         }
341                         fprintf( rejfp, "\n%s\n", rejbuf );
342                 }
343
344                 if (rejfp) ber_memfree( rejbuf );
345         }
346         ber_memfree( rbuf );
347
348         if ( ldifrc < 0 )
349                 retval = LDAP_OTHER;
350
351 #ifdef LDAP_X_TXN
352         if( retval == 0 && txn ) {
353                 rc = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, NULL );
354                 if ( rc != LDAP_OPT_SUCCESS ) {
355                         fprintf( stderr, "Could not unset controls for ldap_txn_end\n");
356                 }
357
358                 /* create transaction */
359                 rc = ldap_txn_end_s( ld, !txnabort, txn_id, NULL, NULL, NULL );
360                 if( rc != LDAP_SUCCESS ) {
361                         tool_perror( "ldap_txn_end_s", rc, NULL, NULL, NULL, NULL );
362                         retval = rc;
363                 }
364         }
365 #endif
366
367         if ( !dont && ld != NULL ) {
368                 tool_unbind( ld );
369         }
370
371         tool_destroy();
372
373 fail:;
374         if ( rejfp != NULL ) {
375                 fclose( rejfp );
376         }
377
378         if ( ldiffp != NULL && ldiffp != &ldifdummy ) {
379                 ldif_close( ldiffp );
380         }
381
382         return( retval );
383 }
384
385
386 static int
387 process_ldif_rec( char *rbuf, int linenum )
388 {
389         LDIFRecord lr;
390         int lrflags = ldapadd ? LDIF_DEFAULT_ADD : 0;
391         int rc;
392         struct berval rbuf_bv;
393
394 #ifdef TEST_LDIF_API
395         if ( getenv( "LDIF_ENTRIES_ONLY" ) ) {
396                 lrflags |= LDIF_ENTRIES_ONLY;
397         }
398         if ( getenv( "LDIF_NO_CONTROLS" ) ) {
399                 lrflags |= LDIF_NO_CONTROLS;
400         }
401 #endif /* TEST_LDIF_API */
402
403         rbuf_bv.bv_val = rbuf;
404         rbuf_bv.bv_len = 0; /* not used */
405         rc = ldap_parse_ldif_record( &rbuf_bv, linenum, &lr, prog, lrflags );
406
407         /* If default controls are set (as with -M option) and controls are
408            specified in the LDIF file, we must add the default controls to
409            the list of controls sent with the ldap operation.
410         */
411         if ( rc == 0 ) {
412                 if (lr.lr_ctrls) {
413                         LDAPControl **defctrls = NULL;   /* Default server controls */
414                         LDAPControl **newctrls = NULL;
415                         ldap_get_option(ld, LDAP_OPT_SERVER_CONTROLS, &defctrls);
416                         if (defctrls) {
417                                 int npc=0;                       /* Num of LDIF controls */
418                                 int ndefc=0;                     /* Num of default controls */
419                                 while (lr.lr_ctrls[npc]) npc++;       /* Count LDIF controls */
420                                 while (defctrls[ndefc]) ndefc++; /* Count default controls */
421                                 newctrls = ber_memrealloc(lr.lr_ctrls,
422                                         (npc+ndefc+1)*sizeof(LDAPControl*));
423
424                                 if (newctrls == NULL) {
425                                         rc = LDAP_NO_MEMORY;
426                                 } else {
427                                         int i;
428                                         lr.lr_ctrls = newctrls;
429                                         for (i=npc; i<npc+ndefc; i++) {
430                                                 lr.lr_ctrls[i] = ldap_control_dup(defctrls[i-npc]);
431                                                 if (lr.lr_ctrls[i] == NULL) {
432                                                         rc = LDAP_NO_MEMORY;
433                                                         break;
434                                                 }
435                                         }
436                                         lr.lr_ctrls[npc+ndefc] = NULL;
437                                 }
438                                 ldap_controls_free(defctrls);  /* Must be freed by library */
439                         }
440                 }
441         }
442
443         if ( rc == 0 ) {
444                 if ( LDAP_REQ_DELETE == lr.lr_op ) {
445                         rc = dodelete( &lr.lr_dn, lr.lr_ctrls );
446                 } else if ( LDAP_REQ_RENAME == lr.lr_op ) {
447                         rc = dorename( &lr.lr_dn, &lr.lrop_newrdn, &lr.lrop_newsup, lr.lrop_delold, lr.lr_ctrls );
448                 } else if ( ( LDAP_REQ_ADD == lr.lr_op ) || ( LDAP_REQ_MODIFY == lr.lr_op ) ) {
449                         rc = domodify( &lr.lr_dn, lr.lrop_mods, lr.lr_ctrls, LDAP_REQ_ADD == lr.lr_op );
450                 } else {
451                         /* record skipped e.g. version: or comment or something we don't handle yet */
452                 }
453
454                 if ( rc == LDAP_SUCCESS ) {
455                         rc = 0;
456                 }
457         }
458
459         ldap_ldif_record_done( &lr );
460
461         return( rc );
462 }
463
464
465 static int
466 domodify(
467         const struct berval *dn,
468         LDAPMod **pmods,
469         LDAPControl **pctrls,
470         int newentry )
471 {
472         int                     rc, i, j, k, notascii, op;
473         struct berval   *bvp;
474
475         if ( ( dn == NULL ) || ( dn->bv_val == NULL ) ) {
476                 fprintf( stderr, _("%s: no DN specified\n"), prog );
477                 return( LDAP_PARAM_ERROR );
478         }
479
480         if ( pmods == NULL ) {
481                 /* implement "touch" (empty sequence)
482                  * modify operation (note that there
483                  * is no symmetry with the UNIX command,
484                  * since \"touch\" on a non-existent entry
485                  * will fail)*/
486                 printf( "warning: no attributes to %sadd (entry=\"%s\")\n",
487                         newentry ? "" : "change or ", dn->bv_val );
488
489         } else {
490                 for ( i = 0; pmods[ i ] != NULL; ++i ) {
491                         op = pmods[ i ]->mod_op & ~LDAP_MOD_BVALUES;
492                         if( op == LDAP_MOD_ADD && ( pmods[i]->mod_bvalues == NULL )) {
493                                 fprintf( stderr,
494                                         _("%s: attribute \"%s\" has no values (entry=\"%s\")\n"),
495                                         prog, pmods[i]->mod_type, dn->bv_val );
496                                 return LDAP_PARAM_ERROR;
497                         }
498                 }
499
500                 if ( verbose ) {
501                         for ( i = 0; pmods[ i ] != NULL; ++i ) {
502                                 op = pmods[ i ]->mod_op & ~LDAP_MOD_BVALUES;
503                                 printf( "%s %s:\n",
504                                         op == LDAP_MOD_REPLACE ? _("replace") :
505                                                 op == LDAP_MOD_ADD ?  _("add") :
506                                                         op == LDAP_MOD_INCREMENT ?  _("increment") :
507                                                                 op == LDAP_MOD_DELETE ?  _("delete") :
508                                                                         _("unknown"),
509                                         pmods[ i ]->mod_type );
510         
511                                 if ( pmods[ i ]->mod_bvalues != NULL ) {
512                                         for ( j = 0; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
513                                                 bvp = pmods[ i ]->mod_bvalues[ j ];
514                                                 notascii = 0;
515                                                 for ( k = 0; (unsigned long) k < bvp->bv_len; ++k ) {
516                                                         if ( !isascii( bvp->bv_val[ k ] )) {
517                                                                 notascii = 1;
518                                                                 break;
519                                                         }
520                                                 }
521                                                 if ( notascii ) {
522                                                         printf( _("\tNOT ASCII (%ld bytes)\n"), bvp->bv_len );
523                                                 } else {
524                                                         printf( "\t%s\n", bvp->bv_val );
525                                                 }
526                                         }
527                                 }
528                         }
529                 }
530         }
531
532         if ( newentry ) {
533                 printf( "%sadding new entry \"%s\"\n", dont ? "!" : "", dn->bv_val );
534         } else {
535                 printf( "%smodifying entry \"%s\"\n", dont ? "!" : "", dn->bv_val );
536         }
537
538         if ( !dont ) {
539                 int     msgid;
540                 if ( newentry ) {
541                         rc = ldap_add_ext( ld, dn->bv_val, pmods, pctrls, NULL, &msgid );
542                 } else {
543                         rc = ldap_modify_ext( ld, dn->bv_val, pmods, pctrls, NULL, &msgid );
544                 }
545
546                 if ( rc != LDAP_SUCCESS ) {
547                         /* print error message about failed update including DN */
548                         fprintf( stderr, _("%s: update failed: %s\n"), prog, dn->bv_val );
549                         tool_perror( newentry ? "ldap_add" : "ldap_modify",
550                                 rc, NULL, NULL, NULL, NULL );
551                         goto done;
552                 }
553                 rc = process_response( ld, msgid,
554                         newentry ? LDAP_RES_ADD : LDAP_RES_MODIFY, dn );
555
556                 if ( verbose && rc == LDAP_SUCCESS ) {
557                         printf( _("modify complete\n") );
558                 }
559
560         } else {
561                 rc = LDAP_SUCCESS;
562         }
563
564 done:
565         putchar( '\n' );
566         return rc;
567 }
568
569
570 static int
571 dodelete(
572         const struct berval *dn,
573         LDAPControl **pctrls )
574 {
575         int     rc;
576         int msgid;
577
578         assert( dn != NULL );
579         assert( dn->bv_val != NULL );
580         printf( _("%sdeleting entry \"%s\"\n"), dont ? "!" : "", dn->bv_val );
581         if ( !dont ) {
582                 rc = ldap_delete_ext( ld, dn->bv_val, pctrls, NULL, &msgid );
583                 if ( rc != LDAP_SUCCESS ) {
584                         fprintf( stderr, _("%s: delete failed: %s\n"), prog, dn->bv_val );
585                         tool_perror( "ldap_delete", rc, NULL, NULL, NULL, NULL );
586                         goto done;
587                 }
588                 rc = process_response( ld, msgid, LDAP_RES_DELETE, dn );
589
590                 if ( verbose && rc == LDAP_SUCCESS ) {
591                         printf( _("delete complete\n") );
592                 }
593         } else {
594                 rc = LDAP_SUCCESS;
595         }
596
597 done:
598         putchar( '\n' );
599         return( rc );
600 }
601
602
603 static int
604 dorename(
605         const struct berval *dn,
606         const struct berval *newrdn,
607         const struct berval *newsup,
608         int deleteoldrdn,
609         LDAPControl **pctrls )
610 {
611         int     rc;
612         int msgid;
613
614         assert( dn != NULL );
615         assert( dn->bv_val != NULL );
616         assert( newrdn != NULL );
617         assert( newrdn->bv_val != NULL );
618         printf( _("%smodifying rdn of entry \"%s\"\n"), dont ? "!" : "", dn->bv_val );
619         if ( verbose ) {
620                 printf( _("\tnew RDN: \"%s\" (%skeep existing values)\n"),
621                         newrdn->bv_val, deleteoldrdn ? _("do not ") : "" );
622         }
623         if ( !dont ) {
624                 rc = ldap_rename( ld, dn->bv_val, newrdn->bv_val,
625                                                   ( newsup && newsup->bv_val ) ? newsup->bv_val : NULL,
626                                                   deleteoldrdn, pctrls, NULL, &msgid );
627                 if ( rc != LDAP_SUCCESS ) {
628                         fprintf( stderr, _("%s: rename failed: %s\n"), prog, dn->bv_val );
629                         tool_perror( "ldap_rename", rc, NULL, NULL, NULL, NULL );
630                         goto done;
631                 }
632                 rc = process_response( ld, msgid, LDAP_RES_RENAME, dn );
633
634                 if ( verbose && rc == LDAP_SUCCESS ) {
635                         printf( _("rename complete\n") );
636                 }
637         } else {
638                 rc = LDAP_SUCCESS;
639         }
640
641 done:
642         putchar( '\n' );
643         return( rc );
644 }
645
646 static const char *
647 res2str( int res ) {
648         switch ( res ) {
649         case LDAP_RES_ADD:
650                 return "ldap_add";
651         case LDAP_RES_DELETE:
652                 return "ldap_delete";
653         case LDAP_RES_MODIFY:
654                 return "ldap_modify";
655         case LDAP_RES_MODRDN:
656                 return "ldap_rename";
657         default:
658                 assert( 0 );
659         }
660
661         return "ldap_unknown";
662 }
663
664 static int process_response(
665         LDAP *ld,
666         int msgid,
667         int op,
668         const struct berval *dn )
669 {
670         LDAPMessage     *res;
671         int             rc = LDAP_OTHER, msgtype;
672         struct timeval  tv = { 0, 0 };
673         int             err;
674         char            *text = NULL, *matched = NULL, **refs = NULL;
675         LDAPControl     **ctrls = NULL;
676
677         assert( dn != NULL );
678         for ( ; ; ) {
679                 tv.tv_sec = 0;
680                 tv.tv_usec = 100000;
681
682                 rc = ldap_result( ld, msgid, LDAP_MSG_ALL, &tv, &res );
683                 if ( tool_check_abandon( ld, msgid ) ) {
684                         return LDAP_CANCELLED;
685                 }
686
687                 if ( rc == -1 ) {
688                         ldap_get_option( ld, LDAP_OPT_RESULT_CODE, &rc );
689                         tool_perror( "ldap_result", rc, NULL, NULL, NULL, NULL );
690                         return rc;
691                 }
692
693                 if ( rc != 0 ) {
694                         break;
695                 }
696         }
697
698         msgtype = ldap_msgtype( res );
699
700         rc = ldap_parse_result( ld, res, &err, &matched, &text, &refs, &ctrls, 1 );
701         if ( rc == LDAP_SUCCESS ) rc = err;
702
703 #ifdef LDAP_X_TXN
704         if ( rc == LDAP_X_TXN_SPECIFY_OKAY ) {
705                 rc = LDAP_SUCCESS;
706         } else
707 #endif
708         if ( rc != LDAP_SUCCESS ) {
709                 tool_perror( res2str( op ), rc, NULL, matched, text, refs );
710         } else if ( msgtype != op ) {
711                 fprintf( stderr, "%s: msgtype: expected %d got %d\n",
712                         res2str( op ), op, msgtype );
713                 rc = LDAP_OTHER;
714         }
715
716         if ( text ) ldap_memfree( text );
717         if ( matched ) ldap_memfree( matched );
718         if ( text ) ber_memvfree( (void **)refs );
719
720         if ( ctrls ) {
721                 tool_print_ctrls( ld, ctrls );
722                 ldap_controls_free( ctrls );
723         }
724
725         return rc;
726 }