]> git.sur5r.net Git - openldap/blob - clients/tools/ldapmodify.c
(ITS#6194) Patch - Enhancement - provide LDIF support as libldif
[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-2009 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                         return( EXIT_FAILURE );
243                 }
244         } else {
245                 rejfp = NULL;
246         }
247
248         if ( infile != NULL ) {
249                 if (( ldiffp = ldif_open( infile, "r" )) == NULL ) {
250                         perror( infile );
251                         return( EXIT_FAILURE );
252                 }
253         } else {
254                 ldifdummy.fp = stdin;
255                 ldiffp = &ldifdummy;
256         }
257
258         if ( debug ) ldif_debug = debug;
259
260         ld = tool_conn_setup( dont, 0 );
261
262         if ( !dont ) {
263                 if ( pw_file || want_bindpw ) {
264                         if ( pw_file ) {
265                                 rc = lutil_get_filed_password( pw_file, &passwd );
266                                 if( rc ) return EXIT_FAILURE;
267                         } else {
268                                 passwd.bv_val = getpassphrase( _("Enter LDAP Password: ") );
269                                 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
270                         }
271                 }
272                 tool_bind( ld );
273         }
274
275 #ifdef LDAP_X_TXN
276         if( txn ) {
277                 /* start transaction */
278                 rc = ldap_txn_start_s( ld, NULL, NULL, &txn_id );
279                 if( rc != LDAP_SUCCESS ) {
280                         tool_perror( "ldap_txn_start_s", rc, NULL, NULL, NULL, NULL );
281                         if( txn > 1 ) return EXIT_FAILURE;
282                         txn = 0;
283                 }
284         }
285 #endif
286
287         if ( 0
288 #ifdef LDAP_X_TXN
289                 || txn
290 #endif
291                 )
292         {
293 #ifdef LDAP_X_TXN
294                 if( txn ) {
295                         c[i].ldctl_oid = LDAP_CONTROL_X_TXN_SPEC;
296                         c[i].ldctl_value = *txn_id;
297                         c[i].ldctl_iscritical = 1;
298                         i++;
299                 }
300 #endif
301         }
302
303         tool_server_controls( ld, c, i );
304
305         rc = 0;
306         retval = 0;
307         lineno = 1;
308         while (( rc == 0 || contoper ) && ( ldifrc = ldif_read_record( ldiffp, &nextline,
309                 &rbuf, &lmax )) > 0 )
310         {
311                 if ( rejfp ) {
312                         len = strlen( rbuf );
313                         if (( rejbuf = (char *)ber_memalloc( len+1 )) == NULL ) {
314                                 perror( "malloc" );
315                                 exit( EXIT_FAILURE );
316                         }
317                         memcpy( rejbuf, rbuf, len+1 );
318                 }
319
320                 rc = process_ldif_rec( rbuf, lineno );
321                 lineno = nextline+1;
322
323                 if ( rc ) retval = rc;
324                 if ( rc && rejfp ) {
325                         fprintf(rejfp, _("# Error: %s (%d)"), ldap_err2string(rc), rc);
326
327                         matched_msg = NULL;
328                         ldap_get_option(ld, LDAP_OPT_MATCHED_DN, &matched_msg);
329                         if ( matched_msg != NULL ) {
330                                 if ( *matched_msg != '\0' ) {
331                                         fprintf( rejfp, _(", matched DN: %s"), matched_msg );
332                                 }
333                                 ldap_memfree( matched_msg );
334                         }
335
336                         error_msg = NULL;
337                         ldap_get_option(ld, LDAP_OPT_DIAGNOSTIC_MESSAGE, &error_msg);
338                         if ( error_msg != NULL ) {
339                                 if ( *error_msg != '\0' ) {
340                                         fprintf( rejfp, _(", additional info: %s"), error_msg );
341                                 }
342                                 ldap_memfree( error_msg );
343                         }
344                         fprintf( rejfp, "\n%s\n", rejbuf );
345                 }
346
347                 if (rejfp) ber_memfree( rejbuf );
348         }
349         ber_memfree( rbuf );
350
351         if ( ldifrc < 0 )
352                 retval = LDAP_OTHER;
353
354 #ifdef LDAP_X_TXN
355         if( retval == 0 && txn ) {
356                 rc = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, NULL );
357                 if ( rc != LDAP_OPT_SUCCESS ) {
358                         fprintf( stderr, "Could not unset controls for ldap_txn_end\n");
359                 }
360
361                 /* create transaction */
362                 rc = ldap_txn_end_s( ld, !txnabort, txn_id, NULL, NULL, NULL );
363                 if( rc != LDAP_SUCCESS ) {
364                         tool_perror( "ldap_txn_end_s", rc, NULL, NULL, NULL, NULL );
365                         retval = rc;
366                 }
367         }
368 #endif
369
370         if ( !dont ) {
371                 tool_unbind( ld );
372         }
373
374         if ( rejfp != NULL ) {
375                 fclose( rejfp );
376         }
377
378         tool_destroy();
379         return( retval );
380 }
381
382
383 static int
384 process_ldif_rec( char *rbuf, int linenum )
385 {
386         LDIFRecord lr;
387         int lrflags = ldapadd ? LDIF_DEFAULT_ADD : 0;
388         int rc;
389         struct berval rbuf_bv;
390
391 #ifdef TEST_LDIF_API
392         if ( getenv( "LDIF_ENTRIES_ONLY" ) ) {
393                 lrflags |= LDIF_ENTRIES_ONLY;
394         }
395         if ( getenv( "LDIF_NO_CONTROLS" ) ) {
396                 lrflags |= LDIF_NO_CONTROLS;
397         }
398 #endif /* TEST_LDIF_API */
399
400         rbuf_bv.bv_val = rbuf;
401         rbuf_bv.bv_len = 0; /* not used */
402         rc = ldap_parse_ldif_record( &rbuf_bv, linenum, &lr, prog, lrflags );
403
404         /* If default controls are set (as with -M option) and controls are
405            specified in the LDIF file, we must add the default controls to
406            the list of controls sent with the ldap operation.
407         */
408         if ( rc == 0 ) {
409                 if (lr.lr_ctrls) {
410                         LDAPControl **defctrls = NULL;   /* Default server controls */
411                         LDAPControl **newctrls = NULL;
412                         ldap_get_option(ld, LDAP_OPT_SERVER_CONTROLS, &defctrls);
413                         if (defctrls) {
414                                 int npc=0;                       /* Num of LDIF controls */
415                                 int ndefc=0;                     /* Num of default controls */
416                                 while (lr.lr_ctrls[npc]) npc++;       /* Count LDIF controls */
417                                 while (defctrls[ndefc]) ndefc++; /* Count default controls */
418                                 newctrls = ber_memrealloc(lr.lr_ctrls,
419                                         (npc+ndefc+1)*sizeof(LDAPControl*));
420
421                                 if (newctrls == NULL) {
422                                         rc = LDAP_NO_MEMORY;
423                                 } else {
424                                         int i;
425                                         lr.lr_ctrls = newctrls;
426                                         for (i=npc; i<npc+ndefc; i++) {
427                                                 lr.lr_ctrls[i] = ldap_control_dup(defctrls[i-npc]);
428                                                 if (lr.lr_ctrls[i] == NULL) {
429                                                         rc = LDAP_NO_MEMORY;
430                                                         break;
431                                                 }
432                                         }
433                                         lr.lr_ctrls[npc+ndefc] = NULL;
434                                 }
435                                 ldap_controls_free(defctrls);  /* Must be freed by library */
436                         }
437                 }
438         }
439
440         if ( rc == 0 ) {
441                 if ( LDAP_REQ_DELETE == lr.lr_op ) {
442                         rc = dodelete( &lr.lr_dn, lr.lr_ctrls );
443                 } else if ( LDAP_REQ_RENAME == lr.lr_op ) {
444                         rc = dorename( &lr.lr_dn, &lr.lr_newrdn, &lr.lr_newsuperior, lr.lr_deleteoldrdn, lr.lr_ctrls );
445                 } else if ( ( LDAP_REQ_ADD == lr.lr_op ) || ( LDAP_REQ_MODIFY == lr.lr_op ) ) {
446                         rc = domodify( &lr.lr_dn, lr.lr_mods, lr.lr_ctrls, LDAP_REQ_ADD == lr.lr_op );
447                 } else {
448                         /* record skipped e.g. version: or comment or something we don't handle yet */
449                 }
450
451                 if ( rc == LDAP_SUCCESS ) {
452                         rc = 0;
453                 }
454         }
455
456         ldap_ldif_record_done( &lr );
457
458         return( rc );
459 }
460
461
462 static int
463 domodify(
464         const struct berval *dn,
465         LDAPMod **pmods,
466         LDAPControl **pctrls,
467         int newentry )
468 {
469         int                     rc, i, j, k, notascii, op;
470         struct berval   *bvp;
471
472         if ( ( dn == NULL ) || ( dn->bv_val == NULL ) ) {
473                 fprintf( stderr, _("%s: no DN specified\n"), prog );
474                 return( LDAP_PARAM_ERROR );
475         }
476
477         if ( pmods == NULL ) {
478                 /* implement "touch" (empty sequence)
479                  * modify operation (note that there
480                  * is no symmetry with the UNIX command,
481                  * since \"touch\" on a non-existent entry
482                  * will fail)*/
483                 printf( "warning: no attributes to %sadd (entry=\"%s\")\n",
484                         newentry ? "" : "change or ", dn->bv_val );
485
486         } else {
487                 for ( i = 0; pmods[ i ] != NULL; ++i ) {
488                         op = pmods[ i ]->mod_op & ~LDAP_MOD_BVALUES;
489                         if( op == LDAP_MOD_ADD && ( pmods[i]->mod_bvalues == NULL )) {
490                                 fprintf( stderr,
491                                         _("%s: attribute \"%s\" has no values (entry=\"%s\")\n"),
492                                         prog, pmods[i]->mod_type, dn->bv_val );
493                                 return LDAP_PARAM_ERROR;
494                         }
495                 }
496
497                 if ( verbose ) {
498                         for ( i = 0; pmods[ i ] != NULL; ++i ) {
499                                 op = pmods[ i ]->mod_op & ~LDAP_MOD_BVALUES;
500                                 printf( "%s %s:\n",
501                                         op == LDAP_MOD_REPLACE ? _("replace") :
502                                                 op == LDAP_MOD_ADD ?  _("add") :
503                                                         op == LDAP_MOD_INCREMENT ?  _("increment") :
504                                                                 op == LDAP_MOD_DELETE ?  _("delete") :
505                                                                         _("unknown"),
506                                         pmods[ i ]->mod_type );
507         
508                                 if ( pmods[ i ]->mod_bvalues != NULL ) {
509                                         for ( j = 0; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
510                                                 bvp = pmods[ i ]->mod_bvalues[ j ];
511                                                 notascii = 0;
512                                                 for ( k = 0; (unsigned long) k < bvp->bv_len; ++k ) {
513                                                         if ( !isascii( bvp->bv_val[ k ] )) {
514                                                                 notascii = 1;
515                                                                 break;
516                                                         }
517                                                 }
518                                                 if ( notascii ) {
519                                                         printf( _("\tNOT ASCII (%ld bytes)\n"), bvp->bv_len );
520                                                 } else {
521                                                         printf( "\t%s\n", bvp->bv_val );
522                                                 }
523                                         }
524                                 }
525                         }
526                 }
527         }
528
529         if ( newentry ) {
530                 printf( "%sadding new entry \"%s\"\n", dont ? "!" : "", dn->bv_val );
531         } else {
532                 printf( "%smodifying entry \"%s\"\n", dont ? "!" : "", dn->bv_val );
533         }
534
535         if ( !dont ) {
536                 int     msgid;
537                 if ( newentry ) {
538                         rc = ldap_add_ext( ld, dn->bv_val, pmods, pctrls, NULL, &msgid );
539                 } else {
540                         rc = ldap_modify_ext( ld, dn->bv_val, pmods, pctrls, NULL, &msgid );
541                 }
542
543                 if ( rc != LDAP_SUCCESS ) {
544                         /* print error message about failed update including DN */
545                         fprintf( stderr, _("%s: update failed: %s\n"), prog, dn->bv_val );
546                         tool_perror( newentry ? "ldap_add" : "ldap_modify",
547                                 rc, NULL, NULL, NULL, NULL );
548                         goto done;
549                 }
550                 rc = process_response( ld, msgid,
551                         newentry ? LDAP_RES_ADD : LDAP_RES_MODIFY, dn );
552
553                 if ( verbose && rc == LDAP_SUCCESS ) {
554                         printf( _("modify complete\n") );
555                 }
556
557         } else {
558                 rc = LDAP_SUCCESS;
559         }
560
561 done:
562         putchar( '\n' );
563         return rc;
564 }
565
566
567 static int
568 dodelete(
569         const struct berval *dn,
570         LDAPControl **pctrls )
571 {
572         int     rc;
573         int msgid;
574
575         assert( dn != NULL );
576         assert( dn->bv_val != NULL );
577         printf( _("%sdeleting entry \"%s\"\n"), dont ? "!" : "", dn->bv_val );
578         if ( !dont ) {
579                 rc = ldap_delete_ext( ld, dn->bv_val, pctrls, NULL, &msgid );
580                 if ( rc != LDAP_SUCCESS ) {
581                         fprintf( stderr, _("%s: delete failed: %s\n"), prog, dn->bv_val );
582                         tool_perror( "ldap_delete", rc, NULL, NULL, NULL, NULL );
583                         goto done;
584                 }
585                 rc = process_response( ld, msgid, LDAP_RES_DELETE, dn );
586
587                 if ( verbose && rc == LDAP_SUCCESS ) {
588                         printf( _("delete complete\n") );
589                 }
590         } else {
591                 rc = LDAP_SUCCESS;
592         }
593
594 done:
595         putchar( '\n' );
596         return( rc );
597 }
598
599
600 static int
601 dorename(
602         const struct berval *dn,
603         const struct berval *newrdn,
604         const struct berval *newsup,
605         int deleteoldrdn,
606         LDAPControl **pctrls )
607 {
608         int     rc;
609         int msgid;
610
611         assert( dn != NULL );
612         assert( dn->bv_val != NULL );
613         assert( newrdn != NULL );
614         assert( newrdn->bv_val != NULL );
615         printf( _("%smodifying rdn of entry \"%s\"\n"), dont ? "!" : "", dn->bv_val );
616         if ( verbose ) {
617                 printf( _("\tnew RDN: \"%s\" (%skeep existing values)\n"),
618                         newrdn->bv_val, deleteoldrdn ? _("do not ") : "" );
619         }
620         if ( !dont ) {
621                 rc = ldap_rename( ld, dn->bv_val, newrdn->bv_val,
622                                                   ( newsup && newsup->bv_val ) ? newsup->bv_val : NULL,
623                                                   deleteoldrdn, pctrls, NULL, &msgid );
624                 if ( rc != LDAP_SUCCESS ) {
625                         fprintf( stderr, _("%s: rename failed: %s\n"), prog, dn->bv_val );
626                         tool_perror( "ldap_rename", rc, NULL, NULL, NULL, NULL );
627                         goto done;
628                 }
629                 rc = process_response( ld, msgid, LDAP_RES_RENAME, dn );
630
631                 if ( verbose && rc == LDAP_SUCCESS ) {
632                         printf( _("rename complete\n") );
633                 }
634         } else {
635                 rc = LDAP_SUCCESS;
636         }
637
638 done:
639         putchar( '\n' );
640         return( rc );
641 }
642
643 static const char *
644 res2str( int res ) {
645         switch ( res ) {
646         case LDAP_RES_ADD:
647                 return "ldap_add";
648         case LDAP_RES_DELETE:
649                 return "ldap_delete";
650         case LDAP_RES_MODIFY:
651                 return "ldap_modify";
652         case LDAP_RES_MODRDN:
653                 return "ldap_rename";
654         default:
655                 assert( 0 );
656         }
657
658         return "ldap_unknown";
659 }
660
661 static int process_response(
662         LDAP *ld,
663         int msgid,
664         int op,
665         const struct berval *dn )
666 {
667         LDAPMessage     *res;
668         int             rc = LDAP_OTHER, msgtype;
669         struct timeval  tv = { 0, 0 };
670         int             err;
671         char            *text = NULL, *matched = NULL, **refs = NULL;
672         LDAPControl     **ctrls = NULL;
673
674         assert( dn != NULL );
675         for ( ; ; ) {
676                 tv.tv_sec = 0;
677                 tv.tv_usec = 100000;
678
679                 rc = ldap_result( ld, msgid, LDAP_MSG_ALL, &tv, &res );
680                 if ( tool_check_abandon( ld, msgid ) ) {
681                         return LDAP_CANCELLED;
682                 }
683
684                 if ( rc == -1 ) {
685                         ldap_get_option( ld, LDAP_OPT_RESULT_CODE, &rc );
686                         tool_perror( "ldap_result", rc, NULL, NULL, NULL, NULL );
687                         return rc;
688                 }
689
690                 if ( rc != 0 ) {
691                         break;
692                 }
693         }
694
695         msgtype = ldap_msgtype( res );
696
697         rc = ldap_parse_result( ld, res, &err, &matched, &text, &refs, &ctrls, 1 );
698         if ( rc == LDAP_SUCCESS ) rc = err;
699
700 #ifdef LDAP_X_TXN
701         if ( rc == LDAP_X_TXN_SPECIFY_OKAY ) {
702                 rc = LDAP_SUCCESS;
703         } else
704 #endif
705         if ( rc != LDAP_SUCCESS ) {
706                 tool_perror( res2str( op ), rc, NULL, matched, text, refs );
707         } else if ( msgtype != op ) {
708                 fprintf( stderr, "%s: msgtype: expected %d got %d\n",
709                         res2str( op ), op, msgtype );
710                 rc = LDAP_OTHER;
711         }
712
713         if ( text ) ldap_memfree( text );
714         if ( matched ) ldap_memfree( matched );
715         if ( text ) ber_memvfree( (void **)refs );
716
717         if ( ctrls ) {
718                 tool_print_ctrls( ld, ctrls );
719                 ldap_controls_free( ctrls );
720         }
721
722         return rc;
723 }