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