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