]> git.sur5r.net Git - openldap/blob - clients/tools/ldapmodrdn.c
2e68fa816e42fcb4908a36a47021e3ae22ef88f3
[openldap] / clients / tools / ldapmodrdn.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6 /* ldapmodrdn.c - generic program to modify an entry's RDN using LDAP.
7  *
8  * Support for MODIFYDN REQUEST V3 (newSuperior) by:
9  * 
10  * Copyright 1999, Juan C. Gomez, All rights reserved.
11  * This software is not subject to any license of Silicon Graphics 
12  * Inc. or Purdue University.
13  *
14  * Redistribution and use in source and binary forms are permitted
15  * without restriction or fee of any kind as long as this notice
16  * is preserved.
17  *
18  */
19
20 #include "portable.h"
21
22 #include <stdio.h>
23
24 #include <ac/stdlib.h>
25
26 #include <ac/ctype.h>
27 #include <ac/signal.h>
28 #include <ac/string.h>
29 #include <ac/unistd.h>
30
31 #include <ldap.h>
32 #include "lutil_ldap.h"
33 #include "ldap_defaults.h"
34
35 static char *prog = NULL;
36 static char     *binddn = NULL;
37 static struct berval passwd = { 0, NULL };
38 static char     *ldapuri = NULL;
39 static char     *ldaphost = NULL;
40 static int      ldapport = 0;
41 #ifdef HAVE_CYRUS_SASL
42 static unsigned sasl_flags = LDAP_SASL_AUTOMATIC;
43 static char *sasl_realm = NULL;
44 static char     *sasl_authc_id = NULL;
45 static char     *sasl_authz_id = NULL;
46 static char     *sasl_mech = NULL;
47 static char     *sasl_secprops = NULL;
48 #endif
49 static int      use_tls = 0;
50 static int      not, verbose, contoper;
51 static LDAP     *ld = NULL;
52
53 static int domodrdn(
54     LDAP        *ld,
55     char        *dn,
56     char        *rdn,
57     char        *newSuperior,
58     int         remove );       /* flag: remove old RDN */
59
60 static void
61 usage( const char *s )
62 {
63         fprintf( stderr,
64 "Rename LDAP entries\n\n"
65 "usage: %s [options] [dn rdn]\n"
66 "       dn rdn: If given, rdn will replace the RDN of the entry specified by DN\n"
67 "               If not given, the list of modifications is read from stdin or\n"
68 "               from the file specified by \"-f file\" (see man page).\n"
69 "Rename options:\n"
70 "  -c         continuous operation mode (do not stop on errors)\n"
71 "  -f file    read operations from `file'\n"
72 "  -r         remove old RDN\n"
73 "  -s newsup  new superior entry\n"
74
75 "Common options:\n"
76 "  -d level   set LDAP debugging level to `level'\n"
77 "  -D binddn  bind DN\n"
78 "  -f file    read operations from `file'\n"
79 "  -h host    LDAP server\n"
80 "  -I         use SASL Interactive mode\n"
81 "  -k         use Kerberos authentication\n"
82 "  -K         like -k, but do only step 1 of the Kerberos bind\n"
83 "  -M         enable Manage DSA IT control (-MM to make critical)\n"
84 "  -n         show what would be done but don't actually search\n"
85 "  -O props   SASL security properties\n"
86 "  -p port    port on LDAP server\n"
87 "  -P version procotol version (default: 3)\n"
88 "  -Q         use SASL Quiet mode\n"
89 "  -R realm   SASL realm\n"
90 "  -U user    SASL authentication identity (username)\n"
91 "  -v         run in verbose mode (diagnostics to standard output)\n"
92 "  -w passwd  bind passwd (for simple authentication)\n"
93 "  -W         prompt for bind passwd\n"
94 "  -x         Simple authentication\n"
95 "  -X id      SASL authorization identity (\"dn:<dn>\" or \"u:<user>\")\n"
96 "  -Y mech    SASL mechanism\n"
97 "  -Z         Start TLS request (-ZZ to require successful response)\n"
98 ,               s );
99
100         exit( EXIT_FAILURE );
101 }
102
103 int
104 main(int argc, char **argv)
105 {
106     char                *infile, *entrydn = NULL, *rdn = NULL, buf[ 4096 ];
107     FILE                *fp;
108         int             rc, i, remove, havedn, authmethod, version, want_bindpw, debug, manageDSAit;
109         int             referrals;
110     char        *newSuperior=NULL;
111
112     infile = NULL;
113     not = contoper = verbose = remove = want_bindpw =
114                 debug = manageDSAit = referrals = 0;
115     authmethod = LDAP_AUTH_SIMPLE;
116         version = -1;
117
118     prog = (prog = strrchr(argv[0], *LDAP_DIRSEP)) == NULL ? argv[0] : prog + 1;
119
120     while (( i = getopt( argc, argv, "cf:rs:" "Cd:D:h:H:IkKMnO:p:P:QRU:vw:WxX:Y:Z" )) != EOF ) {
121         switch( i ) {
122         /* Modrdn Options */
123         case 'c':
124                 contoper++;
125                 break;
126         case 'f':       /* read from file */
127                 if( infile != NULL ) {
128                         fprintf( stderr, "%s: -f previously specified\n" );
129                         return EXIT_FAILURE;
130                 }
131             infile = strdup( optarg );
132             break;
133         case 'r':       /* remove old RDN */
134             remove++;
135             break;
136         case 's':       /* newSuperior */
137                 if( version == LDAP_VERSION2 ) {
138                         fprintf( stderr, "%s: -X incompatible with LDAPv%d\n",
139                                 prog, version );
140                         return EXIT_FAILURE;
141                 }
142             newSuperior = strdup( optarg );
143             version = LDAP_VERSION3;
144             break;
145
146         /* Common Options */
147         case 'C':
148                 referrals++;
149                 break;
150         case 'd':
151             debug |= atoi( optarg );
152             break;
153         case 'D':       /* bind DN */
154                 if( binddn != NULL ) {
155                         fprintf( stderr, "%s: -D previously specified\n" );
156                         return EXIT_FAILURE;
157                 }
158             binddn = strdup( optarg );
159             break;
160         case 'h':       /* ldap host */
161                 if( ldapuri != NULL ) {
162                         fprintf( stderr, "%s: -h incompatible with -H\n" );
163                         return EXIT_FAILURE;
164                 }
165                 if( ldaphost != NULL ) {
166                         fprintf( stderr, "%s: -h previously specified\n" );
167                         return EXIT_FAILURE;
168                 }
169             ldaphost = strdup( optarg );
170             break;
171         case 'H':       /* ldap URI */
172                 if( ldaphost != NULL ) {
173                         fprintf( stderr, "%s: -H incompatible with -h\n" );
174                         return EXIT_FAILURE;
175                 }
176                 if( ldapport ) {
177                         fprintf( stderr, "%s: -H incompatible with -p\n" );
178                         return EXIT_FAILURE;
179                 }
180                 if( ldapuri != NULL ) {
181                         fprintf( stderr, "%s: -H previously specified\n" );
182                         return EXIT_FAILURE;
183                 }
184             ldapuri = strdup( optarg );
185             break;
186         case 'I':
187 #ifdef HAVE_CYRUS_SASL
188                 if( version == LDAP_VERSION2 ) {
189                         fprintf( stderr, "%s: -I incompatible with version %d\n",
190                                 prog, version );
191                         return EXIT_FAILURE;
192                 }
193                 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
194                         fprintf( stderr, "%s: incompatible previous "
195                                 "authentication choice\n",
196                                 prog );
197                         return EXIT_FAILURE;
198                 }
199                 authmethod = LDAP_AUTH_SASL;
200                 version = LDAP_VERSION3;
201                 sasl_flags = LDAP_SASL_INTERACTIVE;
202                 break;
203 #else
204                 fprintf( stderr, "%s: was not compiled with SASL support\n",
205                         prog );
206                 return( EXIT_FAILURE );
207 #endif
208         case 'k':       /* kerberos bind */
209 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
210                 if( version > LDAP_VERSION2 ) {
211                         fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
212                                 prog, version );
213                         return EXIT_FAILURE;
214                 }
215
216                 if( authmethod != -1 ) {
217                         fprintf( stderr, "%s: -k incompatible with previous "
218                                 "authentication choice\n", prog );
219                         return EXIT_FAILURE;
220                 }
221                         
222                 authmethod = LDAP_AUTH_KRBV4;
223 #else
224                 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
225                 return EXIT_FAILURE;
226 #endif
227             break;
228         case 'K':       /* kerberos bind, part one only */
229 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
230                 if( version > LDAP_VERSION2 ) {
231                         fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
232                                 prog, version );
233                         return EXIT_FAILURE;
234                 }
235                 if( authmethod != -1 ) {
236                         fprintf( stderr, "%s: incompatible with previous "
237                                 "authentication choice\n", prog );
238                         return EXIT_FAILURE;
239                 }
240
241                 authmethod = LDAP_AUTH_KRBV41;
242 #else
243                 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
244                 return( EXIT_FAILURE );
245 #endif
246             break;
247         case 'M':
248                 /* enable Manage DSA IT */
249                 if( version == LDAP_VERSION2 ) {
250                         fprintf( stderr, "%s: -M incompatible with LDAPv%d\n",
251                                 prog, version );
252                         return EXIT_FAILURE;
253                 }
254                 manageDSAit++;
255                 version = LDAP_VERSION3;
256                 break;
257         case 'n':       /* print deletes, don't actually do them */
258             ++not;
259             break;
260         case 'O':
261 #ifdef HAVE_CYRUS_SASL
262                 if( sasl_secprops != NULL ) {
263                         fprintf( stderr, "%s: -O previously specified\n" );
264                         return EXIT_FAILURE;
265                 }
266                 if( version == LDAP_VERSION2 ) {
267                         fprintf( stderr, "%s: -O incompatible with LDAPv%d\n",
268                                 prog, version );
269                         return EXIT_FAILURE;
270                 }
271                 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
272                         fprintf( stderr, "%s: incompatible previous "
273                                 "authentication choice\n", prog );
274                         return EXIT_FAILURE;
275                 }
276                 authmethod = LDAP_AUTH_SASL;
277                 version = LDAP_VERSION3;
278                 sasl_secprops = strdup( optarg );
279 #else
280                 fprintf( stderr, "%s: not compiled with SASL support\n",
281                         prog );
282                 return( EXIT_FAILURE );
283 #endif
284                 break;
285         case 'p':
286                 if( ldapport ) {
287                         fprintf( stderr, "%s: -p previously specified\n" );
288                         return EXIT_FAILURE;
289                 }
290             ldapport = atoi( optarg );
291             break;
292         case 'P':
293                 switch( atoi(optarg) ) {
294                 case 2:
295                         if( version == LDAP_VERSION3 ) {
296                                 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
297                                         prog, version );
298                                 return EXIT_FAILURE;
299                         }
300                         version = LDAP_VERSION2;
301                         break;
302                 case 3:
303                         if( version == LDAP_VERSION2 ) {
304                                 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
305                                         prog, version );
306                                 return EXIT_FAILURE;
307                         }
308                         version = LDAP_VERSION3;
309                         break;
310                 default:
311                         fprintf( stderr, "%s: protocol version should be 2 or 3\n",
312                                 prog );
313                         usage( prog );
314                         return( EXIT_FAILURE );
315                 } break;
316         case 'Q':
317 #ifdef HAVE_CYRUS_SASL
318                 if( version == LDAP_VERSION2 ) {
319                         fprintf( stderr, "%s: -Q incompatible with version %d\n",
320                                 prog, version );
321                         return EXIT_FAILURE;
322                 }
323                 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
324                         fprintf( stderr, "%s: incompatible previous "
325                                 "authentication choice\n",
326                                 prog );
327                         return EXIT_FAILURE;
328                 }
329                 authmethod = LDAP_AUTH_SASL;
330                 version = LDAP_VERSION3;
331                 sasl_flags = LDAP_SASL_QUIET;
332                 break;
333 #else
334                 fprintf( stderr, "%s: not compiled with SASL support\n",
335                         prog );
336                 return( EXIT_FAILURE );
337 #endif
338         case 'R':
339 #ifdef HAVE_CYRUS_SASL
340                 if( sasl_realm != NULL ) {
341                         fprintf( stderr, "%s: -R previously specified\n" );
342                         return EXIT_FAILURE;
343                 }
344                 if( version == LDAP_VERSION2 ) {
345                         fprintf( stderr, "%s: -R incompatible with version %d\n",
346                                 prog, version );
347                         return EXIT_FAILURE;
348                 }
349                 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
350                         fprintf( stderr, "%s: incompatible previous "
351                                 "authentication choice\n",
352                                 prog );
353                         return EXIT_FAILURE;
354                 }
355                 authmethod = LDAP_AUTH_SASL;
356                 version = LDAP_VERSION3;
357                 sasl_realm = strdup( optarg );
358 #else
359                 fprintf( stderr, "%s: not compiled with SASL support\n",
360                         prog );
361                 return( EXIT_FAILURE );
362 #endif
363                 break;
364         case 'U':
365 #ifdef HAVE_CYRUS_SASL
366                 if( sasl_authc_id != NULL ) {
367                         fprintf( stderr, "%s: -U previously specified\n" );
368                         return EXIT_FAILURE;
369                 }
370                 if( version == LDAP_VERSION2 ) {
371                         fprintf( stderr, "%s: -U incompatible with version %d\n",
372                                 prog, version );
373                         return EXIT_FAILURE;
374                 }
375                 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
376                         fprintf( stderr, "%s: incompatible previous "
377                                 "authentication choice\n",
378                                 prog );
379                         return EXIT_FAILURE;
380                 }
381                 authmethod = LDAP_AUTH_SASL;
382                 version = LDAP_VERSION3;
383                 sasl_authc_id = strdup( optarg );
384 #else
385                 fprintf( stderr, "%s: not compiled with SASL support\n",
386                         prog );
387                 return( EXIT_FAILURE );
388 #endif
389                 break;
390         case 'v':       /* verbose mode */
391             verbose++;
392             break;
393         case 'w':       /* password */
394             passwd.bv_val = strdup( optarg );
395                 {
396                         char* p;
397
398                         for( p = optarg; *p == '\0'; p++ ) {
399                                 *p = '\0';
400                         }
401                 }
402                 passwd.bv_len = strlen( passwd.bv_val );
403             break;
404         case 'W':
405                 want_bindpw++;
406                 break;
407         case 'Y':
408 #ifdef HAVE_CYRUS_SASL
409                 if( sasl_mech != NULL ) {
410                         fprintf( stderr, "%s: -Y previously specified\n" );
411                         return EXIT_FAILURE;
412                 }
413                 if( version == LDAP_VERSION2 ) {
414                         fprintf( stderr, "%s: -Y incompatible with version %d\n",
415                                 prog, version );
416                         return EXIT_FAILURE;
417                 }
418                 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
419                         fprintf( stderr, "%s: incompatible with authentication choice\n", prog );
420                         return EXIT_FAILURE;
421                 }
422                 authmethod = LDAP_AUTH_SASL;
423                 version = LDAP_VERSION3;
424                 sasl_mech = strdup( optarg );
425 #else
426                 fprintf( stderr, "%s: not compiled with SASL support\n",
427                         prog );
428                 return( EXIT_FAILURE );
429 #endif
430                 break;
431         case 'x':
432                 if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
433                         fprintf( stderr, "%s: incompatible with previous "
434                                 "authentication choice\n", prog );
435                         return EXIT_FAILURE;
436                 }
437                 authmethod = LDAP_AUTH_SIMPLE;
438                 break;
439         case 'X':
440 #ifdef HAVE_CYRUS_SASL
441                 if( sasl_authz_id != NULL ) {
442                         fprintf( stderr, "%s: -X previously specified\n" );
443                         return EXIT_FAILURE;
444                 }
445                 if( version == LDAP_VERSION2 ) {
446                         fprintf( stderr, "%s: -X incompatible with LDAPv%d\n",
447                                 prog, version );
448                         return EXIT_FAILURE;
449                 }
450                 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
451                         fprintf( stderr, "%s: -X incompatible with "
452                                 "authentication choice\n", prog );
453                         return EXIT_FAILURE;
454                 }
455                 authmethod = LDAP_AUTH_SASL;
456                 version = LDAP_VERSION3;
457                 sasl_authz_id = strdup( optarg );
458 #else
459                 fprintf( stderr, "%s: not compiled with SASL support\n",
460                         prog );
461                 return( EXIT_FAILURE );
462 #endif
463                 break;
464         case 'Z':
465 #ifdef HAVE_TLS
466                 if( version == LDAP_VERSION2 ) {
467                         fprintf( stderr, "%s: -Z incompatible with version %d\n",
468                                 prog, version );
469                         return EXIT_FAILURE;
470                 }
471                 version = LDAP_VERSION3;
472                 use_tls++;
473 #else
474                 fprintf( stderr, "%s: not compiled with TLS support\n",
475                         prog );
476                 return( EXIT_FAILURE );
477 #endif
478                 break;
479         default:
480                 fprintf( stderr, "%s: unrecongized option -%c\n",
481                         prog, optopt );
482             usage( argv[0] );
483             return( EXIT_FAILURE );
484         }
485     }
486
487         if (version == -1) {
488                 version = LDAP_VERSION3;
489         }
490         if (authmethod == -1 && version > LDAP_VERSION2) {
491 #ifdef HAVE_CYRUS_SASL
492                 authmethod = LDAP_AUTH_SASL;
493 #else
494                 authmethod = LDAP_AUTH_SIMPLE;
495 #endif
496         }
497
498     havedn = 0;
499     if (argc - optind == 2) {
500         if (( rdn = strdup( argv[argc - 1] )) == NULL ) {
501             perror( "strdup" );
502             return( EXIT_FAILURE );
503         }
504         if (( entrydn = strdup( argv[argc - 2] )) == NULL ) {
505             perror( "strdup" );
506             return( EXIT_FAILURE );
507         }
508         ++havedn;
509     } else if ( argc - optind != 0 ) {
510         fprintf( stderr, "%s: invalid number of arguments (%d), "
511                 "only two allowed\n", prog, argc-optind );
512         usage( argv[0] );
513         return( EXIT_FAILURE );
514     }
515
516     if ( infile != NULL ) {
517         if (( fp = fopen( infile, "r" )) == NULL ) {
518             perror( infile );
519             return( EXIT_FAILURE );
520         }
521     } else {
522         fp = stdin;
523     }
524
525         if ( debug ) {
526                 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
527                         fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
528                 }
529                 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
530                         fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
531                 }
532         }
533
534 #ifdef SIGPIPE
535         (void) SIGNAL( SIGPIPE, SIG_IGN );
536 #endif
537
538         if( ( ldaphost != NULL || ldapport ) && ( ldapuri == NULL ) ) {
539                 if ( verbose ) {
540                         fprintf( stderr, "ldap_init( %s, %d )\n",
541                                 ldaphost != NULL ? ldaphost : "<DEFAULT>",
542                                 ldapport );
543                 }
544                 ld = ldap_init( ldaphost, ldapport );
545
546         } else {
547                 if ( verbose ) {
548                         fprintf( stderr, "ldap_initialize( %s )\n",
549                                 ldapuri != NULL ? ldapuri : "<DEFAULT>" );
550                 }
551                 (void) ldap_initialize( &ld, ldapuri );
552         }
553
554         if( ld == NULL ) {
555                 fprintf( stderr, "Could not create LDAP session handle (%d): %s\n",
556                         rc, ldap_err2string(rc) );
557                 return EXIT_FAILURE;
558         }
559
560         /* referrals */
561         if( ldap_set_option( ld, LDAP_OPT_REFERRALS,
562                 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
563         {
564                 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
565                         referrals ? "on" : "off" );
566                 return EXIT_FAILURE;
567         }
568
569         if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version )
570                 != LDAP_OPT_SUCCESS )
571         {
572                 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
573                         version );
574                 return EXIT_FAILURE;
575         }
576
577         if ( use_tls && ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS ) {
578                 if ( use_tls > 1 ) {
579                         ldap_perror( ld, "ldap_start_tls" );
580                         return( EXIT_FAILURE );
581                 }
582                 fprintf( stderr, "WARNING: could not start TLS\n" );
583         }
584
585         if (want_bindpw) {
586                 passwd.bv_val = getpassphrase("Enter LDAP Password: ");
587                 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
588         }
589
590         if ( authmethod == LDAP_AUTH_SASL ) {
591 #ifdef HAVE_CYRUS_SASL
592                 void *defaults;
593
594                 if( sasl_secprops != NULL ) {
595                         rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
596                                 (void *) sasl_secprops );
597                         
598                         if( rc != LDAP_OPT_SUCCESS ) {
599                                 fprintf( stderr,
600                                         "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
601                                         sasl_secprops );
602                                 return( EXIT_FAILURE );
603                         }
604                 }
605                 
606                 defaults = lutil_sasl_defaults( ld,
607                         sasl_mech,
608                         sasl_realm,
609                         sasl_authc_id,
610                         passwd.bv_val,
611                         sasl_authz_id );
612
613                 rc = ldap_sasl_interactive_bind_s( ld, binddn,
614                         sasl_mech, NULL, NULL,
615                         sasl_flags, lutil_sasl_interact, defaults );
616
617                 if( rc != LDAP_SUCCESS ) {
618                         ldap_perror( ld, "ldap_sasl_interactive_bind_s" );
619                         return( EXIT_FAILURE );
620                 }
621 #else
622                 fprintf( stderr, "%s: not compiled with SASL support\n",
623                         argv[0] );
624                 return( EXIT_FAILURE );
625 #endif
626         }
627         else {
628                 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
629                                 != LDAP_SUCCESS ) {
630                         ldap_perror( ld, "ldap_bind" );
631                         return( EXIT_FAILURE );
632                 }
633         }
634
635         if ( manageDSAit ) {
636                 int err;
637                 LDAPControl c;
638                 LDAPControl *ctrls[2];
639                 ctrls[0] = &c;
640                 ctrls[1] = NULL;
641
642                 c.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
643                 c.ldctl_value.bv_val = NULL;
644                 c.ldctl_value.bv_len = 0;
645                 c.ldctl_iscritical = manageDSAit > 1;
646
647                 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, ctrls );
648
649                 if( err != LDAP_OPT_SUCCESS ) {
650                         fprintf( stderr, "Could not set ManageDSAit %scontrol\n",
651                                 c.ldctl_iscritical ? "critical " : "" );
652                         if( c.ldctl_iscritical ) {
653                                 exit( EXIT_FAILURE );
654                         }
655                 }
656         }
657
658     rc = 0;
659     if (havedn)
660         rc = domodrdn( ld, entrydn, rdn, newSuperior, remove );
661     else while ((rc == 0 || contoper) && fgets(buf, sizeof(buf), fp) != NULL) {
662         if ( *buf != '\0' ) {   /* blank lines optional, skip */
663             buf[ strlen( buf ) - 1 ] = '\0';    /* remove nl */
664
665             if ( havedn ) {     /* have DN, get RDN */
666                 if (( rdn = strdup( buf )) == NULL ) {
667                     perror( "strdup" );
668                     return( EXIT_FAILURE );
669                 }
670                 rc = domodrdn(ld, entrydn, rdn, newSuperior, remove );
671                 havedn = 0;
672             } else if ( !havedn ) {     /* don't have DN yet */
673                 if (( entrydn = strdup( buf )) == NULL ) {
674                     perror( "strdup" );
675                     return( EXIT_FAILURE );
676                 }
677                 ++havedn;
678             }
679         }
680     }
681
682     ldap_unbind( ld );
683
684         /* UNREACHABLE */
685         return( rc );
686 }
687
688 static int domodrdn(
689     LDAP        *ld,
690     char        *dn,
691     char        *rdn,
692     char        *newSuperior,
693     int         remove ) /* flag: remove old RDN */
694 {
695         int rc, code, id;
696         char *matcheddn=NULL, *text=NULL, **refs=NULL;
697         LDAPMessage *res;
698
699     if ( verbose ) {
700                 printf( "Renaming \"%s\"\n", dn );
701                 printf( "\tnew rdn=\"%s\" (%s old rdn)\n",
702                         rdn, remove ? "delete" : "keep" );
703                 if( newSuperior != NULL ) {
704                         printf("\tnew parent=\"%s\"\n", newSuperior);
705                 }
706         }
707
708         if( not ) return LDAP_SUCCESS;
709
710         rc = ldap_rename( ld, dn, rdn, newSuperior, remove,
711                 NULL, NULL, &id );
712
713         if ( rc != LDAP_SUCCESS ) {
714                 fprintf( stderr, "%s: ldap_rename: %s (%d)\n",
715                         prog, ldap_err2string( rc ), rc );
716                 return rc;
717         }
718
719         rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, NULL, &res );
720         if ( rc < 0 ) {
721                 ldap_perror( ld, "ldapmodrdn: ldap_result" );
722                 return rc;
723         }
724
725         rc = ldap_parse_result( ld, res, &code, &matcheddn, &text, &refs, NULL, 1 );
726
727         if( rc != LDAP_SUCCESS ) {
728                 fprintf( stderr, "%s: ldap_parse_result: %s (%d)\n",
729                         prog, ldap_err2string( rc ), rc );
730                 return rc;
731         }
732
733         if( verbose || code != LDAP_SUCCESS ||
734                 (matcheddn && *matcheddn) || (text && *text) || (refs && *refs) )
735         {
736                 printf( "Rename Result: %s (%d)\n",
737                         ldap_err2string( code ), code );
738
739                 if( text && *text ) {
740                         printf( "Additional info: %s\n", text );
741                 }
742
743                 if( matcheddn && *matcheddn ) {
744                         printf( "Matched DN: %s\n", matcheddn );
745                 }
746
747                 if( refs ) {
748                         int i;
749                         for( i=0; refs[i]; i++ ) {
750                                 printf("Referral: %s\n", refs[i] );
751                         }
752                 }
753         }
754
755         ber_memfree( text );
756         ber_memfree( matcheddn );
757         ber_memvfree( (void **) refs );
758
759         return code;
760 }