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