]> git.sur5r.net Git - openldap/blob - clients/tools/ldapmodrdn.c
First-cut proxy authorization support.
[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, "authzid" ) == 0 ) {
221                         if( authzid != NULL ) {
222                                 fprintf( stderr, "authzid control previously specified\n");
223                                 return EXIT_FAILURE;
224                         }
225                         if( cvalue == NULL ) {
226                                 fprintf( stderr, "authzid: control value expected\n" );
227                                 usage(prog);
228                                 return EXIT_FAILURE;
229                         }
230                         if( !crit ) {
231                                 fprintf( stderr, "authzid: must be marked critical\n" );
232                                 usage(prog);
233                                 return EXIT_FAILURE;
234                         }
235
236                         assert( authzid == NULL );
237                         authzid = cvalue;
238
239                 } else if ( strcasecmp( control, "manageDSAit" ) == 0 ) {
240                         if( manageDSAit ) {
241                                 fprintf( stderr, "manageDSAit control previously specified\n");
242                                 return EXIT_FAILURE;
243                         }
244                         if( cvalue != NULL ) {
245                                 fprintf( stderr, "manageDSAit: no control value expected\n" );
246                                 usage(prog);
247                                 return EXIT_FAILURE;
248                         }
249
250                         manageDSAit = 1 + crit;
251
252                 } else if ( strcasecmp( control, "noop" ) == 0 ) {
253                         if( noop ) {
254                                 fprintf( stderr, "noop control previously specified\n");
255                                 return EXIT_FAILURE;
256                         }
257                         if( cvalue != NULL ) {
258                                 fprintf( stderr, "noop: no control value expected\n" );
259                                 usage(prog);
260                                 return EXIT_FAILURE;
261                         }
262
263                         noop = 1 + crit;
264
265                 } else {
266                         fprintf( stderr, "Invalid general control name: %s\n", control );
267                         usage(prog);
268                         return EXIT_FAILURE;
269                 }
270                 break;
271         case 'h':       /* ldap host */
272                 if( ldapuri != NULL ) {
273                         fprintf( stderr, "%s: -h incompatible with -H\n", prog );
274                         return EXIT_FAILURE;
275                 }
276                 if( ldaphost != NULL ) {
277                         fprintf( stderr, "%s: -h previously specified\n", prog );
278                         return EXIT_FAILURE;
279                 }
280             ldaphost = strdup( optarg );
281             break;
282         case 'H':       /* ldap URI */
283                 if( ldaphost != NULL ) {
284                         fprintf( stderr, "%s: -H incompatible with -h\n", prog );
285                         return EXIT_FAILURE;
286                 }
287                 if( ldapport ) {
288                         fprintf( stderr, "%s: -H incompatible with -p\n", prog );
289                         return EXIT_FAILURE;
290                 }
291                 if( ldapuri != NULL ) {
292                         fprintf( stderr, "%s: -H previously specified\n", prog );
293                         return EXIT_FAILURE;
294                 }
295             ldapuri = strdup( optarg );
296             break;
297         case 'I':
298 #ifdef HAVE_CYRUS_SASL
299                 if( version == LDAP_VERSION2 ) {
300                         fprintf( stderr, "%s: -I incompatible with version %d\n",
301                                 prog, version );
302                         return EXIT_FAILURE;
303                 }
304                 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
305                         fprintf( stderr, "%s: incompatible previous "
306                                 "authentication choice\n",
307                                 prog );
308                         return EXIT_FAILURE;
309                 }
310                 authmethod = LDAP_AUTH_SASL;
311                 version = LDAP_VERSION3;
312                 sasl_flags = LDAP_SASL_INTERACTIVE;
313                 break;
314 #else
315                 fprintf( stderr, "%s: was not compiled with SASL support\n",
316                         prog );
317                 return( EXIT_FAILURE );
318 #endif
319         case 'k':       /* kerberos bind */
320 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
321                 if( version > LDAP_VERSION2 ) {
322                         fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
323                                 prog, version );
324                         return EXIT_FAILURE;
325                 }
326
327                 if( authmethod != -1 ) {
328                         fprintf( stderr, "%s: -k incompatible with previous "
329                                 "authentication choice\n", prog );
330                         return EXIT_FAILURE;
331                 }
332                         
333                 authmethod = LDAP_AUTH_KRBV4;
334 #else
335                 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
336                 return EXIT_FAILURE;
337 #endif
338             break;
339         case 'K':       /* kerberos bind, part one only */
340 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
341                 if( version > LDAP_VERSION2 ) {
342                         fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
343                                 prog, version );
344                         return EXIT_FAILURE;
345                 }
346                 if( authmethod != -1 ) {
347                         fprintf( stderr, "%s: incompatible with previous "
348                                 "authentication choice\n", prog );
349                         return EXIT_FAILURE;
350                 }
351
352                 authmethod = LDAP_AUTH_KRBV41;
353 #else
354                 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
355                 return( EXIT_FAILURE );
356 #endif
357             break;
358         case 'M':
359                 /* enable Manage DSA IT */
360                 if( version == LDAP_VERSION2 ) {
361                         fprintf( stderr, "%s: -M incompatible with LDAPv%d\n",
362                                 prog, version );
363                         return EXIT_FAILURE;
364                 }
365                 manageDSAit++;
366                 version = LDAP_VERSION3;
367                 break;
368         case 'n':       /* print deletes, don't actually do them */
369             ++not;
370             break;
371         case 'O':
372 #ifdef HAVE_CYRUS_SASL
373                 if( sasl_secprops != NULL ) {
374                         fprintf( stderr, "%s: -O previously specified\n", prog );
375                         return EXIT_FAILURE;
376                 }
377                 if( version == LDAP_VERSION2 ) {
378                         fprintf( stderr, "%s: -O incompatible with LDAPv%d\n",
379                                 prog, version );
380                         return EXIT_FAILURE;
381                 }
382                 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
383                         fprintf( stderr, "%s: incompatible previous "
384                                 "authentication choice\n", prog );
385                         return EXIT_FAILURE;
386                 }
387                 authmethod = LDAP_AUTH_SASL;
388                 version = LDAP_VERSION3;
389                 sasl_secprops = strdup( optarg );
390 #else
391                 fprintf( stderr, "%s: not compiled with SASL support\n",
392                         prog );
393                 return( EXIT_FAILURE );
394 #endif
395                 break;
396         case 'p':
397                 if( ldapport ) {
398                         fprintf( stderr, "%s: -p previously specified\n", prog );
399                         return EXIT_FAILURE;
400                 }
401             ldapport = atoi( optarg );
402             break;
403         case 'P':
404                 switch( atoi(optarg) ) {
405                 case 2:
406                         if( version == LDAP_VERSION3 ) {
407                                 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
408                                         prog, version );
409                                 return EXIT_FAILURE;
410                         }
411                         version = LDAP_VERSION2;
412                         break;
413                 case 3:
414                         if( version == LDAP_VERSION2 ) {
415                                 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
416                                         prog, version );
417                                 return EXIT_FAILURE;
418                         }
419                         version = LDAP_VERSION3;
420                         break;
421                 default:
422                         fprintf( stderr, "%s: protocol version should be 2 or 3\n",
423                                 prog );
424                         usage( prog );
425                         return( EXIT_FAILURE );
426                 }
427                 break;
428         case 'Q':
429 #ifdef HAVE_CYRUS_SASL
430                 if( version == LDAP_VERSION2 ) {
431                         fprintf( stderr, "%s: -Q incompatible with version %d\n",
432                                 prog, version );
433                         return EXIT_FAILURE;
434                 }
435                 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
436                         fprintf( stderr, "%s: incompatible previous "
437                                 "authentication choice\n",
438                                 prog );
439                         return EXIT_FAILURE;
440                 }
441                 authmethod = LDAP_AUTH_SASL;
442                 version = LDAP_VERSION3;
443                 sasl_flags = LDAP_SASL_QUIET;
444                 break;
445 #else
446                 fprintf( stderr, "%s: not compiled with SASL support\n",
447                         prog );
448                 return( EXIT_FAILURE );
449 #endif
450         case 'R':
451 #ifdef HAVE_CYRUS_SASL
452                 if( sasl_realm != NULL ) {
453                         fprintf( stderr, "%s: -R previously specified\n", prog );
454                         return EXIT_FAILURE;
455                 }
456                 if( version == LDAP_VERSION2 ) {
457                         fprintf( stderr, "%s: -R incompatible with version %d\n",
458                                 prog, version );
459                         return EXIT_FAILURE;
460                 }
461                 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
462                         fprintf( stderr, "%s: incompatible previous "
463                                 "authentication choice\n",
464                                 prog );
465                         return EXIT_FAILURE;
466                 }
467                 authmethod = LDAP_AUTH_SASL;
468                 version = LDAP_VERSION3;
469                 sasl_realm = strdup( optarg );
470 #else
471                 fprintf( stderr, "%s: not compiled with SASL support\n",
472                         prog );
473                 return( EXIT_FAILURE );
474 #endif
475                 break;
476         case 'U':
477 #ifdef HAVE_CYRUS_SASL
478                 if( sasl_authc_id != NULL ) {
479                         fprintf( stderr, "%s: -U previously specified\n", prog );
480                         return EXIT_FAILURE;
481                 }
482                 if( version == LDAP_VERSION2 ) {
483                         fprintf( stderr, "%s: -U incompatible with version %d\n",
484                                 prog, version );
485                         return EXIT_FAILURE;
486                 }
487                 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
488                         fprintf( stderr, "%s: incompatible previous "
489                                 "authentication choice\n",
490                                 prog );
491                         return EXIT_FAILURE;
492                 }
493                 authmethod = LDAP_AUTH_SASL;
494                 version = LDAP_VERSION3;
495                 sasl_authc_id = strdup( optarg );
496 #else
497                 fprintf( stderr, "%s: not compiled with SASL support\n",
498                         prog );
499                 return( EXIT_FAILURE );
500 #endif
501                 break;
502         case 'v':       /* verbose mode */
503             verbose++;
504             break;
505         case 'w':       /* password */
506             passwd.bv_val = strdup( optarg );
507                 {
508                         char* p;
509
510                         for( p = optarg; *p != '\0'; p++ ) {
511                                 *p = '\0';
512                         }
513                 }
514                 passwd.bv_len = strlen( passwd.bv_val );
515             break;
516         case 'W':
517                 want_bindpw++;
518                 break;
519         case 'y':
520                 pw_file = optarg;
521                 break;
522         case 'Y':
523 #ifdef HAVE_CYRUS_SASL
524                 if( sasl_mech != NULL ) {
525                         fprintf( stderr, "%s: -Y previously specified\n", prog );
526                         return EXIT_FAILURE;
527                 }
528                 if( version == LDAP_VERSION2 ) {
529                         fprintf( stderr, "%s: -Y incompatible with version %d\n",
530                                 prog, version );
531                         return EXIT_FAILURE;
532                 }
533                 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
534                         fprintf( stderr, "%s: incompatible with authentication choice\n", prog );
535                         return EXIT_FAILURE;
536                 }
537                 authmethod = LDAP_AUTH_SASL;
538                 version = LDAP_VERSION3;
539                 sasl_mech = strdup( optarg );
540 #else
541                 fprintf( stderr, "%s: not compiled with SASL support\n",
542                         prog );
543                 return( EXIT_FAILURE );
544 #endif
545                 break;
546         case 'x':
547                 if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
548                         fprintf( stderr, "%s: incompatible with previous "
549                                 "authentication choice\n", prog );
550                         return EXIT_FAILURE;
551                 }
552                 authmethod = LDAP_AUTH_SIMPLE;
553                 break;
554         case 'X':
555 #ifdef HAVE_CYRUS_SASL
556                 if( sasl_authz_id != NULL ) {
557                         fprintf( stderr, "%s: -X previously specified\n", prog );
558                         return EXIT_FAILURE;
559                 }
560                 if( version == LDAP_VERSION2 ) {
561                         fprintf( stderr, "%s: -X incompatible with LDAPv%d\n",
562                                 prog, version );
563                         return EXIT_FAILURE;
564                 }
565                 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
566                         fprintf( stderr, "%s: -X incompatible with "
567                                 "authentication choice\n", prog );
568                         return EXIT_FAILURE;
569                 }
570                 authmethod = LDAP_AUTH_SASL;
571                 version = LDAP_VERSION3;
572                 sasl_authz_id = strdup( optarg );
573 #else
574                 fprintf( stderr, "%s: not compiled with SASL support\n",
575                         prog );
576                 return( EXIT_FAILURE );
577 #endif
578                 break;
579         case 'Z':
580 #ifdef HAVE_TLS
581                 if( version == LDAP_VERSION2 ) {
582                         fprintf( stderr, "%s: -Z incompatible with version %d\n",
583                                 prog, version );
584                         return EXIT_FAILURE;
585                 }
586                 version = LDAP_VERSION3;
587                 use_tls++;
588 #else
589                 fprintf( stderr, "%s: not compiled with TLS support\n",
590                         prog );
591                 return( EXIT_FAILURE );
592 #endif
593                 break;
594         default:
595                 fprintf( stderr, "%s: unrecognized option -%c\n",
596                         prog, optopt );
597             usage( prog );
598             return( EXIT_FAILURE );
599         }
600     }
601
602         if (version == -1) {
603                 version = LDAP_VERSION3;
604         }
605         if (authmethod == -1 && version > LDAP_VERSION2) {
606 #ifdef HAVE_CYRUS_SASL
607                 authmethod = LDAP_AUTH_SASL;
608 #else
609                 authmethod = LDAP_AUTH_SIMPLE;
610 #endif
611         }
612
613     havedn = 0;
614     if (argc - optind == 2) {
615         if (( rdn = strdup( argv[argc - 1] )) == NULL ) {
616             perror( "strdup" );
617             return( EXIT_FAILURE );
618         }
619         if (( entrydn = strdup( argv[argc - 2] )) == NULL ) {
620             perror( "strdup" );
621             return( EXIT_FAILURE );
622         }
623         ++havedn;
624     } else if ( argc - optind != 0 ) {
625         fprintf( stderr, "%s: invalid number of arguments (%d), "
626                 "only two allowed\n", prog, argc-optind );
627         usage( prog );
628         return( EXIT_FAILURE );
629     }
630
631     if ( infile != NULL ) {
632         if (( fp = fopen( infile, "r" )) == NULL ) {
633             perror( infile );
634             return( EXIT_FAILURE );
635         }
636     } else {
637         fp = stdin;
638     }
639
640         if ( debug ) {
641                 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
642                         fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
643                 }
644                 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
645                         fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
646                 }
647         }
648
649 #ifdef SIGPIPE
650         (void) SIGNAL( SIGPIPE, SIG_IGN );
651 #endif
652
653         if( ( ldaphost != NULL || ldapport ) && ( ldapuri == NULL ) ) {
654                 if ( verbose ) {
655                         fprintf( stderr, "ldap_init( %s, %d )\n",
656                                 ldaphost != NULL ? ldaphost : "<DEFAULT>",
657                                 ldapport );
658                 }
659
660                 ld = ldap_init( ldaphost, ldapport );
661                 if( ld == NULL ) {
662                         perror("ldapmodify: ldap_init");
663                         return EXIT_FAILURE;
664                 }
665
666         } else {
667                 if ( verbose ) {
668                         fprintf( stderr, "ldap_initialize( %s )\n",
669                                 ldapuri != NULL ? ldapuri : "<DEFAULT>" );
670                 }
671
672                 rc = ldap_initialize( &ld, ldapuri );
673                 if( rc != LDAP_SUCCESS ) {
674                         fprintf( stderr, "Could not create LDAP session handle (%d): %s\n",
675                                 rc, ldap_err2string(rc) );
676                         return EXIT_FAILURE;
677                 }
678         }
679
680         /* referrals */
681         if( ldap_set_option( ld, LDAP_OPT_REFERRALS,
682                 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
683         {
684                 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
685                         referrals ? "on" : "off" );
686                 return EXIT_FAILURE;
687         }
688
689         if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version )
690                 != LDAP_OPT_SUCCESS )
691         {
692                 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
693                         version );
694                 return EXIT_FAILURE;
695         }
696
697         if ( use_tls && ( ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS )) {
698                 ldap_perror( ld, "ldap_start_tls" );
699                 if ( use_tls > 1 ) {
700                         return( EXIT_FAILURE );
701                 }
702         }
703
704         if ( pw_file || want_bindpw ) {
705                 if ( pw_file ) {
706                         rc = lutil_get_filed_password( pw_file, &passwd );
707                         if( rc ) return EXIT_FAILURE;
708                 } else {
709                         passwd.bv_val = getpassphrase( "Enter LDAP Password: " );
710                         passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
711                 }
712         }
713
714         if ( authmethod == LDAP_AUTH_SASL ) {
715 #ifdef HAVE_CYRUS_SASL
716                 void *defaults;
717
718                 if( sasl_secprops != NULL ) {
719                         rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
720                                 (void *) sasl_secprops );
721                         
722                         if( rc != LDAP_OPT_SUCCESS ) {
723                                 fprintf( stderr,
724                                         "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
725                                         sasl_secprops );
726                                 return( EXIT_FAILURE );
727                         }
728                 }
729                 
730                 defaults = lutil_sasl_defaults( ld,
731                         sasl_mech,
732                         sasl_realm,
733                         sasl_authc_id,
734                         passwd.bv_val,
735                         sasl_authz_id );
736
737                 rc = ldap_sasl_interactive_bind_s( ld, binddn,
738                         sasl_mech, NULL, NULL,
739                         sasl_flags, lutil_sasl_interact, defaults );
740
741                 if( rc != LDAP_SUCCESS ) {
742                         ldap_perror( ld, "ldap_sasl_interactive_bind_s" );
743                         return( EXIT_FAILURE );
744                 }
745 #else
746                 fprintf( stderr, "%s: not compiled with SASL support\n",
747                         prog );
748                 return( EXIT_FAILURE );
749 #endif
750         }
751         else {
752                 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
753                                 != LDAP_SUCCESS ) {
754                         ldap_perror( ld, "ldap_bind" );
755                         return( EXIT_FAILURE );
756                 }
757         }
758
759         if ( authzid || manageDSAit || noop ) {
760                 int err, crit=0, i=0;
761                 LDAPControl c[3];
762                 LDAPControl *ctrls[4];
763
764                 if ( authzid ) {
765                         c[i].ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
766                         c[i].ldctl_value.bv_val = authzid;
767                         c[i].ldctl_value.bv_len = strlen( authzid );
768                         c[i].ldctl_iscritical = 1;
769
770                         if( c[i].ldctl_iscritical ) crit++;
771                         ctrls[i] = &c[i];
772                         ctrls[++i] = NULL;
773                 }
774
775                 if ( manageDSAit ) {
776                         c[i].ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
777                         c[i].ldctl_value.bv_val = NULL;
778                         c[i].ldctl_value.bv_len = 0;
779                         c[i].ldctl_iscritical = manageDSAit > 1;
780
781                         if( c[i].ldctl_iscritical ) crit++;
782                         ctrls[i] = &c[i];
783                         ctrls[++i] = NULL;
784                 }
785
786                 if ( noop ) {
787                         c[i].ldctl_oid = LDAP_CONTROL_NOOP;
788                         c[i].ldctl_value.bv_val = NULL;
789                         c[i].ldctl_value.bv_len = 0;
790                         c[i].ldctl_iscritical = noop > 1;
791
792                         if( c[i].ldctl_iscritical ) crit++;
793                         ctrls[i] = &c[i];
794                         ctrls[++i] = NULL;
795                 }
796         
797                 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, ctrls );
798
799                 if( err != LDAP_OPT_SUCCESS ) {
800                         fprintf( stderr, "Could not set %scontrols\n",
801                                 crit ? "critical " : "" );
802                         if ( crit ) {
803                                 return EXIT_FAILURE;
804                         }
805                 }
806         }
807
808     rc = 0;
809     if (havedn)
810         rc = domodrdn( ld, entrydn, rdn, newSuperior, remove );
811     else while ((rc == 0 || contoper) && fgets(buf, sizeof(buf), fp) != NULL) {
812         if ( *buf != '\0' ) {   /* blank lines optional, skip */
813             buf[ strlen( buf ) - 1 ] = '\0';    /* remove nl */
814
815             if ( havedn ) {     /* have DN, get RDN */
816                 if (( rdn = strdup( buf )) == NULL ) {
817                     perror( "strdup" );
818                     return( EXIT_FAILURE );
819                 }
820                 rc = domodrdn(ld, entrydn, rdn, newSuperior, remove );
821                 havedn = 0;
822             } else if ( !havedn ) {     /* don't have DN yet */
823                 if (( entrydn = strdup( buf )) == NULL ) {
824                     perror( "strdup" );
825                     return( EXIT_FAILURE );
826                 }
827                 ++havedn;
828             }
829         }
830     }
831
832     ldap_unbind( ld );
833
834         /* UNREACHABLE */
835         return( rc );
836 }
837
838 static int domodrdn(
839     LDAP        *ld,
840     char        *dn,
841     char        *rdn,
842     char        *newSuperior,
843     int         remove ) /* flag: remove old RDN */
844 {
845         int rc, code, id;
846         char *matcheddn=NULL, *text=NULL, **refs=NULL;
847         LDAPMessage *res;
848
849     if ( verbose ) {
850                 printf( "Renaming \"%s\"\n", dn );
851                 printf( "\tnew rdn=\"%s\" (%s old rdn)\n",
852                         rdn, remove ? "delete" : "keep" );
853                 if( newSuperior != NULL ) {
854                         printf("\tnew parent=\"%s\"\n", newSuperior);
855                 }
856         }
857
858         if( not ) return LDAP_SUCCESS;
859
860         rc = ldap_rename( ld, dn, rdn, newSuperior, remove,
861                 NULL, NULL, &id );
862
863         if ( rc != LDAP_SUCCESS ) {
864                 fprintf( stderr, "%s: ldap_rename: %s (%d)\n",
865                         prog, ldap_err2string( rc ), rc );
866                 return rc;
867         }
868
869         rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, NULL, &res );
870         if ( rc < 0 ) {
871                 ldap_perror( ld, "ldapmodrdn: ldap_result" );
872                 return rc;
873         }
874
875         rc = ldap_parse_result( ld, res, &code, &matcheddn, &text, &refs, NULL, 1 );
876
877         if( rc != LDAP_SUCCESS ) {
878                 fprintf( stderr, "%s: ldap_parse_result: %s (%d)\n",
879                         prog, ldap_err2string( rc ), rc );
880                 return rc;
881         }
882
883         if( verbose || code != LDAP_SUCCESS ||
884                 (matcheddn && *matcheddn) || (text && *text) || (refs && *refs) )
885         {
886                 printf( "Rename Result: %s (%d)\n",
887                         ldap_err2string( code ), code );
888
889                 if( text && *text ) {
890                         printf( "Additional info: %s\n", text );
891                 }
892
893                 if( matcheddn && *matcheddn ) {
894                         printf( "Matched DN: %s\n", matcheddn );
895                 }
896
897                 if( refs ) {
898                         int i;
899                         for( i=0; refs[i]; i++ ) {
900                                 printf("Referral: %s\n", refs[i] );
901                         }
902                 }
903         }
904
905         ber_memfree( text );
906         ber_memfree( matcheddn );
907         ber_memvfree( (void **) refs );
908
909         return code;
910 }