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