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