]> git.sur5r.net Git - openldap/blob - clients/tools/ldapmodrdn.c
Fix minor value match bug
[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 mech    SASL mechanism\n"
99 "  -Z         Start TLS request (-ZZ to require successful response)\n"
100 ,               s );
101
102         exit( EXIT_FAILURE );
103 }
104
105 int
106 main(int argc, char **argv)
107 {
108     char                *infile, *entrydn = NULL, *rdn = NULL, buf[ 4096 ];
109     FILE                *fp;
110         int             rc, i, remove, havedn, authmethod, version, want_bindpw, debug, manageDSAit;
111         int             referrals;
112     char        *newSuperior=NULL;
113
114     infile = NULL;
115     not = contoper = verbose = remove = want_bindpw =
116                 debug = manageDSAit = referrals = 0;
117     authmethod = -1;
118         version = -1;
119
120     prog = lutil_progname( "ldapmodrdn", argc, argv );
121
122     while (( i = getopt( argc, argv, "cf:rs:"
123                 "Cd:D:h:H:IkKMnO:p:P:QR:U:vw:WxX:Y:Z" )) != EOF )
124         {
125         switch( i ) {
126         /* Modrdn Options */
127         case 'c':
128                 contoper++;
129                 break;
130         case 'f':       /* read from file */
131                 if( infile != NULL ) {
132                         fprintf( stderr, "%s: -f previously specified\n", prog );
133                         return EXIT_FAILURE;
134                 }
135             infile = strdup( optarg );
136             break;
137         case 'r':       /* remove old RDN */
138             remove++;
139             break;
140         case 's':       /* newSuperior */
141                 if( version == LDAP_VERSION2 ) {
142                         fprintf( stderr, "%s: -X incompatible with LDAPv%d\n",
143                                 prog, version );
144                         return EXIT_FAILURE;
145                 }
146             newSuperior = strdup( optarg );
147             version = LDAP_VERSION3;
148             break;
149
150         /* Common Options */
151         case 'C':
152                 referrals++;
153                 break;
154         case 'd':
155             debug |= atoi( optarg );
156             break;
157         case 'D':       /* bind DN */
158                 if( binddn != NULL ) {
159                         fprintf( stderr, "%s: -D previously specified\n", prog );
160                         return EXIT_FAILURE;
161                 }
162             binddn = strdup( optarg );
163             break;
164         case 'h':       /* ldap host */
165                 if( ldapuri != NULL ) {
166                         fprintf( stderr, "%s: -h incompatible with -H\n", prog );
167                         return EXIT_FAILURE;
168                 }
169                 if( ldaphost != NULL ) {
170                         fprintf( stderr, "%s: -h previously specified\n", prog );
171                         return EXIT_FAILURE;
172                 }
173             ldaphost = strdup( optarg );
174             break;
175         case 'H':       /* ldap URI */
176                 if( ldaphost != NULL ) {
177                         fprintf( stderr, "%s: -H incompatible with -h\n", prog );
178                         return EXIT_FAILURE;
179                 }
180                 if( ldapport ) {
181                         fprintf( stderr, "%s: -H incompatible with -p\n", prog );
182                         return EXIT_FAILURE;
183                 }
184                 if( ldapuri != NULL ) {
185                         fprintf( stderr, "%s: -H previously specified\n", prog );
186                         return EXIT_FAILURE;
187                 }
188             ldapuri = strdup( optarg );
189             break;
190         case 'I':
191 #ifdef HAVE_CYRUS_SASL
192                 if( version == LDAP_VERSION2 ) {
193                         fprintf( stderr, "%s: -I incompatible with version %d\n",
194                                 prog, version );
195                         return EXIT_FAILURE;
196                 }
197                 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
198                         fprintf( stderr, "%s: incompatible previous "
199                                 "authentication choice\n",
200                                 prog );
201                         return EXIT_FAILURE;
202                 }
203                 authmethod = LDAP_AUTH_SASL;
204                 version = LDAP_VERSION3;
205                 sasl_flags = LDAP_SASL_INTERACTIVE;
206                 break;
207 #else
208                 fprintf( stderr, "%s: was not compiled with SASL support\n",
209                         prog );
210                 return( EXIT_FAILURE );
211 #endif
212         case 'k':       /* kerberos bind */
213 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
214                 if( version > LDAP_VERSION2 ) {
215                         fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
216                                 prog, version );
217                         return EXIT_FAILURE;
218                 }
219
220                 if( authmethod != -1 ) {
221                         fprintf( stderr, "%s: -k incompatible with previous "
222                                 "authentication choice\n", prog );
223                         return EXIT_FAILURE;
224                 }
225                         
226                 authmethod = LDAP_AUTH_KRBV4;
227 #else
228                 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
229                 return EXIT_FAILURE;
230 #endif
231             break;
232         case 'K':       /* kerberos bind, part one only */
233 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
234                 if( version > LDAP_VERSION2 ) {
235                         fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
236                                 prog, version );
237                         return EXIT_FAILURE;
238                 }
239                 if( authmethod != -1 ) {
240                         fprintf( stderr, "%s: incompatible with previous "
241                                 "authentication choice\n", prog );
242                         return EXIT_FAILURE;
243                 }
244
245                 authmethod = LDAP_AUTH_KRBV41;
246 #else
247                 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
248                 return( EXIT_FAILURE );
249 #endif
250             break;
251         case 'M':
252                 /* enable Manage DSA IT */
253                 if( version == LDAP_VERSION2 ) {
254                         fprintf( stderr, "%s: -M incompatible with LDAPv%d\n",
255                                 prog, version );
256                         return EXIT_FAILURE;
257                 }
258                 manageDSAit++;
259                 version = LDAP_VERSION3;
260                 break;
261         case 'n':       /* print deletes, don't actually do them */
262             ++not;
263             break;
264         case 'O':
265 #ifdef HAVE_CYRUS_SASL
266                 if( sasl_secprops != NULL ) {
267                         fprintf( stderr, "%s: -O previously specified\n", prog );
268                         return EXIT_FAILURE;
269                 }
270                 if( version == LDAP_VERSION2 ) {
271                         fprintf( stderr, "%s: -O incompatible with LDAPv%d\n",
272                                 prog, version );
273                         return EXIT_FAILURE;
274                 }
275                 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
276                         fprintf( stderr, "%s: incompatible previous "
277                                 "authentication choice\n", prog );
278                         return EXIT_FAILURE;
279                 }
280                 authmethod = LDAP_AUTH_SASL;
281                 version = LDAP_VERSION3;
282                 sasl_secprops = strdup( optarg );
283 #else
284                 fprintf( stderr, "%s: not compiled with SASL support\n",
285                         prog );
286                 return( EXIT_FAILURE );
287 #endif
288                 break;
289         case 'p':
290                 if( ldapport ) {
291                         fprintf( stderr, "%s: -p previously specified\n", prog );
292                         return EXIT_FAILURE;
293                 }
294             ldapport = atoi( optarg );
295             break;
296         case 'P':
297                 switch( atoi(optarg) ) {
298                 case 2:
299                         if( version == LDAP_VERSION3 ) {
300                                 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
301                                         prog, version );
302                                 return EXIT_FAILURE;
303                         }
304                         version = LDAP_VERSION2;
305                         break;
306                 case 3:
307                         if( version == LDAP_VERSION2 ) {
308                                 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
309                                         prog, version );
310                                 return EXIT_FAILURE;
311                         }
312                         version = LDAP_VERSION3;
313                         break;
314                 default:
315                         fprintf( stderr, "%s: protocol version should be 2 or 3\n",
316                                 prog );
317                         usage( prog );
318                         return( EXIT_FAILURE );
319                 } break;
320         case 'Q':
321 #ifdef HAVE_CYRUS_SASL
322                 if( version == LDAP_VERSION2 ) {
323                         fprintf( stderr, "%s: -Q incompatible with version %d\n",
324                                 prog, version );
325                         return EXIT_FAILURE;
326                 }
327                 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
328                         fprintf( stderr, "%s: incompatible previous "
329                                 "authentication choice\n",
330                                 prog );
331                         return EXIT_FAILURE;
332                 }
333                 authmethod = LDAP_AUTH_SASL;
334                 version = LDAP_VERSION3;
335                 sasl_flags = LDAP_SASL_QUIET;
336                 break;
337 #else
338                 fprintf( stderr, "%s: not compiled with SASL support\n",
339                         prog );
340                 return( EXIT_FAILURE );
341 #endif
342         case 'R':
343 #ifdef HAVE_CYRUS_SASL
344                 if( sasl_realm != NULL ) {
345                         fprintf( stderr, "%s: -R previously specified\n", prog );
346                         return EXIT_FAILURE;
347                 }
348                 if( version == LDAP_VERSION2 ) {
349                         fprintf( stderr, "%s: -R incompatible with version %d\n",
350                                 prog, version );
351                         return EXIT_FAILURE;
352                 }
353                 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
354                         fprintf( stderr, "%s: incompatible previous "
355                                 "authentication choice\n",
356                                 prog );
357                         return EXIT_FAILURE;
358                 }
359                 authmethod = LDAP_AUTH_SASL;
360                 version = LDAP_VERSION3;
361                 sasl_realm = strdup( optarg );
362 #else
363                 fprintf( stderr, "%s: not compiled with SASL support\n",
364                         prog );
365                 return( EXIT_FAILURE );
366 #endif
367                 break;
368         case 'U':
369 #ifdef HAVE_CYRUS_SASL
370                 if( sasl_authc_id != NULL ) {
371                         fprintf( stderr, "%s: -U previously specified\n", prog );
372                         return EXIT_FAILURE;
373                 }
374                 if( version == LDAP_VERSION2 ) {
375                         fprintf( stderr, "%s: -U incompatible with version %d\n",
376                                 prog, version );
377                         return EXIT_FAILURE;
378                 }
379                 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
380                         fprintf( stderr, "%s: incompatible previous "
381                                 "authentication choice\n",
382                                 prog );
383                         return EXIT_FAILURE;
384                 }
385                 authmethod = LDAP_AUTH_SASL;
386                 version = LDAP_VERSION3;
387                 sasl_authc_id = strdup( optarg );
388 #else
389                 fprintf( stderr, "%s: not compiled with SASL support\n",
390                         prog );
391                 return( EXIT_FAILURE );
392 #endif
393                 break;
394         case 'v':       /* verbose mode */
395             verbose++;
396             break;
397         case 'w':       /* password */
398             passwd.bv_val = strdup( optarg );
399                 {
400                         char* p;
401
402                         for( p = optarg; *p != '\0'; p++ ) {
403                                 *p = '\0';
404                         }
405                 }
406                 passwd.bv_len = strlen( passwd.bv_val );
407             break;
408         case 'W':
409                 want_bindpw++;
410                 break;
411         case 'Y':
412 #ifdef HAVE_CYRUS_SASL
413                 if( sasl_mech != NULL ) {
414                         fprintf( stderr, "%s: -Y previously specified\n", prog );
415                         return EXIT_FAILURE;
416                 }
417                 if( version == LDAP_VERSION2 ) {
418                         fprintf( stderr, "%s: -Y incompatible with version %d\n",
419                                 prog, version );
420                         return EXIT_FAILURE;
421                 }
422                 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
423                         fprintf( stderr, "%s: incompatible with authentication choice\n", prog );
424                         return EXIT_FAILURE;
425                 }
426                 authmethod = LDAP_AUTH_SASL;
427                 version = LDAP_VERSION3;
428                 sasl_mech = strdup( optarg );
429 #else
430                 fprintf( stderr, "%s: not compiled with SASL support\n",
431                         prog );
432                 return( EXIT_FAILURE );
433 #endif
434                 break;
435         case 'x':
436                 if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
437                         fprintf( stderr, "%s: incompatible with previous "
438                                 "authentication choice\n", prog );
439                         return EXIT_FAILURE;
440                 }
441                 authmethod = LDAP_AUTH_SIMPLE;
442                 break;
443         case 'X':
444 #ifdef HAVE_CYRUS_SASL
445                 if( sasl_authz_id != NULL ) {
446                         fprintf( stderr, "%s: -X previously specified\n", prog );
447                         return EXIT_FAILURE;
448                 }
449                 if( version == LDAP_VERSION2 ) {
450                         fprintf( stderr, "%s: -X incompatible with LDAPv%d\n",
451                                 prog, version );
452                         return EXIT_FAILURE;
453                 }
454                 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
455                         fprintf( stderr, "%s: -X incompatible with "
456                                 "authentication choice\n", prog );
457                         return EXIT_FAILURE;
458                 }
459                 authmethod = LDAP_AUTH_SASL;
460                 version = LDAP_VERSION3;
461                 sasl_authz_id = strdup( optarg );
462 #else
463                 fprintf( stderr, "%s: not compiled with SASL support\n",
464                         prog );
465                 return( EXIT_FAILURE );
466 #endif
467                 break;
468         case 'Z':
469 #ifdef HAVE_TLS
470                 if( version == LDAP_VERSION2 ) {
471                         fprintf( stderr, "%s: -Z incompatible with version %d\n",
472                                 prog, version );
473                         return EXIT_FAILURE;
474                 }
475                 version = LDAP_VERSION3;
476                 use_tls++;
477 #else
478                 fprintf( stderr, "%s: not compiled with TLS support\n",
479                         prog );
480                 return( EXIT_FAILURE );
481 #endif
482                 break;
483         default:
484                 fprintf( stderr, "%s: unrecognized option -%c\n",
485                         prog, optopt );
486             usage( prog );
487             return( EXIT_FAILURE );
488         }
489     }
490
491         if (version == -1) {
492                 version = LDAP_VERSION3;
493         }
494         if (authmethod == -1 && version > LDAP_VERSION2) {
495 #ifdef HAVE_CYRUS_SASL
496                 authmethod = LDAP_AUTH_SASL;
497 #else
498                 authmethod = LDAP_AUTH_SIMPLE;
499 #endif
500         }
501
502     havedn = 0;
503     if (argc - optind == 2) {
504         if (( rdn = strdup( argv[argc - 1] )) == NULL ) {
505             perror( "strdup" );
506             return( EXIT_FAILURE );
507         }
508         if (( entrydn = strdup( argv[argc - 2] )) == NULL ) {
509             perror( "strdup" );
510             return( EXIT_FAILURE );
511         }
512         ++havedn;
513     } else if ( argc - optind != 0 ) {
514         fprintf( stderr, "%s: invalid number of arguments (%d), "
515                 "only two allowed\n", prog, argc-optind );
516         usage( prog );
517         return( EXIT_FAILURE );
518     }
519
520     if ( infile != NULL ) {
521         if (( fp = fopen( infile, "r" )) == NULL ) {
522             perror( infile );
523             return( EXIT_FAILURE );
524         }
525     } else {
526         fp = stdin;
527     }
528
529         if ( debug ) {
530                 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
531                         fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
532                 }
533                 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
534                         fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
535                 }
536         }
537
538 #ifdef SIGPIPE
539         (void) SIGNAL( SIGPIPE, SIG_IGN );
540 #endif
541
542         if( ( ldaphost != NULL || ldapport ) && ( ldapuri == NULL ) ) {
543                 if ( verbose ) {
544                         fprintf( stderr, "ldap_init( %s, %d )\n",
545                                 ldaphost != NULL ? ldaphost : "<DEFAULT>",
546                                 ldapport );
547                 }
548
549                 ld = ldap_init( ldaphost, ldapport );
550                 if( ld == NULL ) {
551                         perror("ldapmodify: ldap_init");
552                         return EXIT_FAILURE;
553                 }
554
555         } else {
556                 if ( verbose ) {
557                         fprintf( stderr, "ldap_initialize( %s )\n",
558                                 ldapuri != NULL ? ldapuri : "<DEFAULT>" );
559                 }
560
561                 rc = ldap_initialize( &ld, ldapuri );
562                 if( rc != LDAP_SUCCESS ) {
563                         fprintf( stderr, "Could not create LDAP session handle (%d): %s\n",
564                                 rc, ldap_err2string(rc) );
565                         return EXIT_FAILURE;
566                 }
567         }
568
569         /* referrals */
570         if( ldap_set_option( ld, LDAP_OPT_REFERRALS,
571                 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
572         {
573                 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
574                         referrals ? "on" : "off" );
575                 return EXIT_FAILURE;
576         }
577
578         if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version )
579                 != LDAP_OPT_SUCCESS )
580         {
581                 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
582                         version );
583                 return EXIT_FAILURE;
584         }
585
586         if ( use_tls && ( ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS )) {
587                 ldap_perror( ld, "ldap_start_tls" );
588                 if ( use_tls > 1 ) {
589                         return( EXIT_FAILURE );
590                 }
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                         prog );
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 }