]> git.sur5r.net Git - openldap/blob - clients/tools/ldapmodrdn.c
cc8cc724faa65b9fc33611d6bfc7244a81c20c48
[openldap] / clients / tools / ldapmodrdn.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2000 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_ldap.h"
33 #include "ldap_defaults.h"
34
35 static char     *binddn = NULL;
36 static struct berval passwd = { 0, NULL };
37 static char     *ldaphost = NULL;
38 static int      ldapport = 0;
39 #ifdef HAVE_CYRUS_SASL
40 static char     *sasl_authc_id = NULL;
41 static char     *sasl_authz_id = NULL;
42 static char     *sasl_mech = NULL;
43 static char     *sasl_secprops = NULL;
44 #endif
45 static int      use_tls = 0;
46 static int      not, verbose, contoper;
47 static LDAP     *ld;
48
49 static int domodrdn(
50     LDAP        *ld,
51     char        *dn,
52     char        *rdn,
53     char        *newSuperior,
54     int         remove );       /* flag: remove old RDN */
55
56 static void
57 usage( const char *s )
58 {
59         fprintf( stderr,
60 "Rename LDAP entries\n\n"
61 "usage: %s [options] [dn rdn]\n"
62 "       dn rdn: If given, rdn will replace the RDN of the entry specified by DN\n"
63 "               If not given, the list of modifications is read from stdin or\n"
64 "               from the file specified by \"-f file\" (see man page).\n"
65 "Rename options:\n"
66 "       -c\t\tcontinuous operation mode (do not stop on errors)\n"
67 "       -f file\t\tread operations from `file'\n"
68 "       -r\t\tremove old RDN\n"
69 "       -s newsuperior\tnew superior entry\n"
70
71 "common options:\n"
72 "       -C\t\tchase referrals\n"
73 "       -d level\tset LDAP debugging level to `level'\n"
74 "       -D binddn\tbind DN\n"
75 "       -h host\t\tLDAP server\n"
76 "       -k\t\tuse Kerberos authentication\n"
77 "       -K\t\tlike -k, but do only step 1 of the Kerberos bind\n"
78 "       -M\t\tenable Manage DSA IT control (-MM to make it critical)\n"
79 "       -n\t\tshow what would be done but don't actually do it\n"
80 "       -O secprops\tSASL security properties\n"
81 "       -p port\t\tport on LDAP server\n"
82 "       -P version\tprocotol version (default: 3)\n"
83 "       -U user\t\tSASL authentication identity (username)\n"
84 "       -v\t\trun in verbose mode (diagnostics to standard output)\n"
85 "       -w passwd\tbind passwd (for simple authentication)\n"
86 "       -W\t\tprompt for bind passwd\n"
87 "       -X id\t\tSASL authorization identity (\"dn:<dn>\" or \"u:<user>\")\n"
88 "       -Y mech\t\tSASL mechanism\n"
89 "       -Z\t\tissue Start TLS request (-ZZ to require successful response)\n"
90 ,               s );
91
92         exit( EXIT_FAILURE );
93 }
94
95 int
96 main(int argc, char **argv)
97 {
98     char                *prog,*infile, *entrydn = NULL, *rdn = NULL, buf[ 4096 ];
99     FILE                *fp;
100         int             rc, i, remove, havedn, authmethod, version, want_bindpw, debug, manageDSAit;
101         int             referrals;
102     char        *newSuperior=NULL;
103
104     infile = NULL;
105     not = contoper = verbose = remove = want_bindpw =
106                 debug = manageDSAit = referrals = 0;
107     authmethod = LDAP_AUTH_SIMPLE;
108         version = -1;
109
110     prog = (prog = strrchr(argv[0], *LDAP_DIRSEP)) == NULL ? argv[0] : ++prog;
111
112     while (( i = getopt( argc, argv, "cf:rs:" "Cd:D:h:kKMnO:p:P:U:vw:WxX:Y:Z" )) != EOF ) {
113         switch( i ) {
114         /* Modrdn Options */
115         case 'c':
116                 contoper++;
117                 break;
118         case 's':       /* newSuperior */
119                 if( version == LDAP_VERSION2 ) {
120                         fprintf( stderr, "%s: -X incompatible with LDAPv%d\n",
121                                 prog, version );
122                         return EXIT_FAILURE;
123                 }
124             newSuperior = strdup( optarg );
125             version = LDAP_VERSION3;
126             break;
127         case 'r':       /* remove old RDN */
128             remove++;
129             break;
130
131         /* Common Options */
132         case 'C':
133                 referrals++;
134                 break;
135         case 'd':
136             debug |= atoi( optarg );
137             break;
138         case 'D':       /* bind DN */
139             binddn = strdup( optarg );
140             break;
141         case 'h':       /* ldap host */
142             ldaphost = strdup( optarg );
143             break;
144         case 'k':       /* kerberos bind */
145 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
146                 if( version > LDAP_VERSION2 ) {
147                         fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
148                                 prog, version );
149                         return EXIT_FAILURE;
150                 }
151
152                 if( authmethod != -1 ) {
153                         fprintf( stderr, "%s: -k incompatible with previous "
154                                 "authentication choice\n", prog );
155                         return EXIT_FAILURE;
156                 }
157                         
158                 authmethod = LDAP_AUTH_KRBV4;
159 #else
160                 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
161                 return EXIT_FAILURE;
162 #endif
163             break;
164         case 'K':       /* kerberos bind, part one only */
165 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
166                 if( version > LDAP_VERSION2 ) {
167                         fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
168                                 prog, version );
169                         return EXIT_FAILURE;
170                 }
171                 if( authmethod != -1 ) {
172                         fprintf( stderr, "%s: incompatible with previous "
173                                 "authentication choice\n", prog );
174                         return EXIT_FAILURE;
175                 }
176
177                 authmethod = LDAP_AUTH_KRBV41;
178 #else
179                 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
180                 return( EXIT_FAILURE );
181 #endif
182             break;
183         case 'M':
184                 /* enable Manage DSA IT */
185                 if( version == LDAP_VERSION2 ) {
186                         fprintf( stderr, "%s: -M incompatible with LDAPv%d\n",
187                                 prog, version );
188                         return EXIT_FAILURE;
189                 }
190                 manageDSAit++;
191                 version = LDAP_VERSION3;
192                 break;
193         case 'n':       /* print deletes, don't actually do them */
194             ++not;
195             break;
196         case 'O':
197 #ifdef HAVE_CYRUS_SASL
198                 if( version == LDAP_VERSION2 ) {
199                         fprintf( stderr, "%s -O incompatible with LDAPv%d\n",
200                                 prog, version );
201                         return EXIT_FAILURE;
202                 }
203                 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
204                         fprintf( stderr, "%s: incompatible previous "
205                                 "authentication choice\n", prog );
206                         return EXIT_FAILURE;
207                 }
208                 sasl_secprops = strdup( optarg );
209                 authmethod = LDAP_AUTH_SASL;
210                 version = LDAP_VERSION3;
211 #else
212                 fprintf( stderr, "%s: not compiled with SASL support\n",
213                         prog );
214                 return( EXIT_FAILURE );
215 #endif
216                 break;
217         case 'p':
218             ldapport = atoi( optarg );
219             break;
220         case 'P':
221                 switch( atoi(optarg) ) {
222                 case 2:
223                         if( version == LDAP_VERSION3 ) {
224                                 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
225                                         prog, version );
226                                 return EXIT_FAILURE;
227                         }
228                         version = LDAP_VERSION2;
229                         break;
230                 case 3:
231                         if( version == LDAP_VERSION2 ) {
232                                 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
233                                         prog, version );
234                                 return EXIT_FAILURE;
235                         }
236                         version = LDAP_VERSION3;
237                         break;
238                 default:
239                         fprintf( stderr, "%s: protocol version should be 2 or 3\n",
240                                 prog );
241                         usage( prog );
242                         return( EXIT_FAILURE );
243                 } break;
244         case 'U':
245 #ifdef HAVE_CYRUS_SASL
246                 if( version == LDAP_VERSION2 ) {
247                         fprintf( stderr, "%s: -U incompatible with version %d\n",
248                                 prog, version );
249                         return EXIT_FAILURE;
250                 }
251                 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
252                         fprintf( stderr, "%s: incompatible previous "
253                                 "authentication choice\n",
254                                 prog );
255                         return EXIT_FAILURE;
256                 }
257                 authmethod = LDAP_AUTH_SASL;
258                 version = LDAP_VERSION3;
259
260                 sasl_authc_id = strdup( optarg );
261                 authmethod = LDAP_AUTH_SASL;
262 #else
263                 fprintf( stderr, "%s: was not compiled with SASL support\n",
264                         prog );
265                 return( EXIT_FAILURE );
266 #endif
267                 break;
268         case 'v':       /* verbose mode */
269             verbose++;
270             break;
271         case 'w':       /* password */
272             passwd.bv_val = strdup( optarg );
273                 {
274                         char* p;
275
276                         for( p = optarg; *p == '\0'; p++ ) {
277                                 *p = '*';
278                         }
279                 }
280                 passwd.bv_len = strlen( passwd.bv_val );
281             break;
282         case 'W':
283                 want_bindpw++;
284                 break;
285         case 'Y':
286 #ifdef HAVE_CYRUS_SASL
287                 if( version == LDAP_VERSION2 ) {
288                         fprintf( stderr, "%s: -Y incompatible with version %d\n",
289                                 prog, version );
290                         return EXIT_FAILURE;
291                 }
292                 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
293                         fprintf( stderr, "%s: incompatible with authentication choice\n", prog );
294                         return EXIT_FAILURE;
295                 }
296
297                 authmethod = LDAP_AUTH_SASL;
298                 version = LDAP_VERSION3;
299 #else
300                 fprintf( stderr, "%s: was not compiled with SASL support\n",
301                         prog );
302                 return( EXIT_FAILURE );
303 #endif
304                 break;
305         case 'x':
306                 if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
307                         fprintf( stderr, "%s: incompatible with previous "
308                                 "authentication choice\n", prog );
309                         return EXIT_FAILURE;
310                 }
311                 authmethod = LDAP_AUTH_SIMPLE;
312                 break;
313         case 'X':
314 #ifdef HAVE_CYRUS_SASL
315                 if( version == LDAP_VERSION2 ) {
316                         fprintf( stderr, "%s: -X incompatible with LDAPv%d\n",
317                                 prog, version );
318                         return EXIT_FAILURE;
319                 }
320                 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
321                         fprintf( stderr, "%s: -X incompatible with "
322                                 "authentication choice\n", prog );
323                         return EXIT_FAILURE;
324                 }
325                 authmethod = LDAP_AUTH_SASL;
326                 version = LDAP_VERSION3;
327
328                 sasl_authz_id = strdup( optarg );
329                 authmethod = LDAP_AUTH_SASL;
330 #else
331                 fprintf( stderr, "%s: not compiled with SASL support\n",
332                         prog );
333                 return( EXIT_FAILURE );
334 #endif
335                 break;
336         case 'Z':
337 #ifdef HAVE_TLS
338                 if( version == LDAP_VERSION2 ) {
339                         fprintf( stderr, "%s -Z incompatible with version %d\n",
340                                 prog, version );
341                         return EXIT_FAILURE;
342                 }
343                 version = LDAP_VERSION3;
344                 use_tls++;
345 #else
346                 fprintf( stderr, "%s: not compiled with TLS support\n",
347                         prog );
348                 return( EXIT_FAILURE );
349 #endif
350                 break;
351         default:
352                 fprintf( stderr, "%s: unrecongized option -%c\n",
353                         prog, optopt );
354             usage( argv[0] );
355             return( EXIT_FAILURE );
356         }
357     }
358
359         if (version == -1) {
360                 version = LDAP_VERSION3;
361         }
362         if (authmethod == -1 && version > LDAP_VERSION2) {
363 #ifdef HAVE_CYRUS_SASL
364                 authmethod = LDAP_AUTH_SASL;
365 #else
366                 authmethod = LDAP_AUTH_SIMPLE;
367 #endif
368         }
369
370     havedn = 0;
371     if (argc - optind == 2) {
372         if (( rdn = strdup( argv[argc - 1] )) == NULL ) {
373             perror( "strdup" );
374             return( EXIT_FAILURE );
375         }
376         if (( entrydn = strdup( argv[argc - 2] )) == NULL ) {
377             perror( "strdup" );
378             return( EXIT_FAILURE );
379         }
380         ++havedn;
381     } else if ( argc - optind != 0 ) {
382         fprintf( stderr, "%s: invalid number of arguments (%d), "
383                 "only two allowed\n", prog, argc-optind );
384         usage( argv[0] );
385         return( EXIT_FAILURE );
386     }
387
388     if ( infile != NULL ) {
389         if (( fp = fopen( infile, "r" )) == NULL ) {
390             perror( infile );
391             return( EXIT_FAILURE );
392         }
393     } else {
394         fp = stdin;
395     }
396
397         if ( debug ) {
398                 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
399                         fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
400                 }
401                 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
402                         fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
403                 }
404         }
405
406 #ifdef SIGPIPE
407         (void) SIGNAL( SIGPIPE, SIG_IGN );
408 #endif
409
410     if (( ld = ldap_init( ldaphost, ldapport )) == NULL ) {
411         perror( "ldap_init" );
412         return( EXIT_FAILURE );
413     }
414
415         /* referrals */
416         if( ldap_set_option( ld, LDAP_OPT_REFERRALS,
417                 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
418         {
419                 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
420                         referrals ? "on" : "off" );
421                 return EXIT_FAILURE;
422         }
423
424         if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version )
425                 != LDAP_OPT_SUCCESS )
426         {
427                 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
428                         version );
429                 return EXIT_FAILURE;
430         }
431
432         if ( use_tls && ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS ) {
433                 if ( use_tls > 1 ) {
434                         ldap_perror( ld, "ldap_start_tls" );
435                         return( EXIT_FAILURE );
436                 }
437                 fprintf( stderr, "WARNING: could not start TLS\n" );
438         }
439
440         if (want_bindpw) {
441                 passwd.bv_val = getpassphrase("Enter LDAP Password: ");
442                 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
443         }
444
445         if ( authmethod == LDAP_AUTH_SASL ) {
446 #ifdef HAVE_CYRUS_SASL
447                 if( sasl_secprops != NULL ) {
448                         rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
449                                 (void *) sasl_secprops );
450                         
451                         if( rc != LDAP_OPT_SUCCESS ) {
452                                 fprintf( stderr,
453                                         "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
454                                         sasl_secprops );
455                                 return( EXIT_FAILURE );
456                         }
457                 }
458                 
459                 rc = ldap_sasl_interactive_bind_s( ld, binddn,
460                         sasl_mech, NULL, NULL, lutil_sasl_interact );
461
462                 if( rc != LDAP_SUCCESS ) {
463                         ldap_perror( ld, "ldap_sasl_interactive_bind_s" );
464                         return( EXIT_FAILURE );
465                 }
466 #else
467                 fprintf( stderr, "%s was not compiled with SASL support\n",
468                         argv[0] );
469                 return( EXIT_FAILURE );
470 #endif
471         }
472         else {
473                 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
474                                 != LDAP_SUCCESS ) {
475                         ldap_perror( ld, "ldap_bind" );
476                         return( EXIT_FAILURE );
477                 }
478         }
479
480         if ( manageDSAit ) {
481                 int err;
482                 LDAPControl c;
483                 LDAPControl *ctrls[2];
484                 ctrls[0] = &c;
485                 ctrls[1] = NULL;
486
487                 c.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
488                 c.ldctl_value.bv_val = NULL;
489                 c.ldctl_value.bv_len = 0;
490                 c.ldctl_iscritical = manageDSAit > 1;
491
492                 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, &ctrls );
493
494                 if( err != LDAP_OPT_SUCCESS ) {
495                         fprintf( stderr, "Could not set ManageDSAit %scontrol\n",
496                                 c.ldctl_iscritical ? "critical " : "" );
497                         if( c.ldctl_iscritical ) {
498                                 exit( EXIT_FAILURE );
499                         }
500                 }
501         }
502
503     rc = 0;
504     if (havedn)
505         rc = domodrdn( ld, entrydn, rdn, newSuperior, remove );
506     else while ((rc == 0 || contoper) && fgets(buf, sizeof(buf), fp) != NULL) {
507         if ( *buf != '\0' ) {   /* blank lines optional, skip */
508             buf[ strlen( buf ) - 1 ] = '\0';    /* remove nl */
509
510             if ( havedn ) {     /* have DN, get RDN */
511                 if (( rdn = strdup( buf )) == NULL ) {
512                     perror( "strdup" );
513                     return( EXIT_FAILURE );
514                 }
515                 rc = domodrdn(ld, entrydn, rdn, newSuperior, remove );
516                 havedn = 0;
517             } else if ( !havedn ) {     /* don't have DN yet */
518                 if (( entrydn = strdup( buf )) == NULL ) {
519                     perror( "strdup" );
520                     return( EXIT_FAILURE );
521                 }
522                 ++havedn;
523             }
524         }
525     }
526
527     ldap_unbind( ld );
528
529         /* UNREACHABLE */
530         return( rc );
531 }
532
533 static int domodrdn(
534     LDAP        *ld,
535     char        *dn,
536     char        *rdn,
537     char        *newSuperior,
538     int         remove ) /* flag: remove old RDN */
539 {
540         int rc, code, id;
541         char *matcheddn=NULL, *text=NULL, **refs=NULL;
542         LDAPMessage *res;
543
544     if ( verbose ) {
545                 printf( "Renaming \"%s\"\n", dn );
546                 printf( "\tnew rdn=\"%s\" (%s old rdn)\n",
547                         rdn, remove ? "delete" : "keep" );
548                 if( newSuperior != NULL ) {
549                         printf("\tnew parent=\"%s\"\n", newSuperior);
550                 }
551         }
552
553         if( not ) return LDAP_SUCCESS;
554
555         rc = ldap_rename( ld, dn, rdn, newSuperior, remove,
556                 NULL, NULL, &id );
557
558         if ( rc != LDAP_SUCCESS ) {
559                 fprintf( stderr, "ldapmodrdn: ldap_rename: %s (%d)\n",
560                         ldap_err2string( rc ), rc );
561                 return rc;
562         }
563
564         rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, NULL, &res );
565         if ( rc < 0 ) {
566                 ldap_perror( ld, "ldapmodrdn: ldap_result" );
567                 return rc;
568         }
569
570         rc = ldap_parse_result( ld, res, &code, &matcheddn, &text, &refs, NULL, 1 );
571
572         if( rc != LDAP_SUCCESS ) {
573                 fprintf( stderr, "ldapmodrdn: ldap_parse_result: %s (%d)\n",
574                         ldap_err2string( rc ), rc );
575                 return rc;
576         }
577
578         if( verbose || code != LDAP_SUCCESS ||
579                 (matcheddn && *matcheddn) || (text && *text) || (refs && *refs) )
580         {
581                 printf( "Rename Result: %s (%d)\n",
582                         ldap_err2string( code ), code );
583
584                 if( text && *text ) {
585                         printf( "Additional info: %s\n", text );
586                 }
587
588                 if( matcheddn && *matcheddn ) {
589                         printf( "Matched DN: %s\n", matcheddn );
590                 }
591
592                 if( refs ) {
593                         int i;
594                         for( i=0; refs[i]; i++ ) {
595                                 printf("Referral: %s\n", refs[i] );
596                         }
597                 }
598         }
599
600         ber_memfree( text );
601         ber_memfree( matcheddn );
602         ber_memvfree( (void **) refs );
603
604         return code;
605 }