3 * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6 /* ldapmodrdn.c - generic program to modify an entry's RDN using LDAP.
8 * Support for MODIFYDN REQUEST V3 (newSuperior) by:
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.
14 * Redistribution and use in source and binary forms are permitted
15 * without restriction or fee of any kind as long as this notice
24 #include <ac/stdlib.h>
27 #include <ac/signal.h>
28 #include <ac/string.h>
29 #include <ac/unistd.h>
32 #include "lutil_ldap.h"
33 #include "ldap_defaults.h"
35 static char *prog = NULL;
36 static char *binddn = NULL;
37 static struct berval passwd = { 0, NULL };
38 static char *ldapuri = NULL;
39 static char *ldaphost = NULL;
40 static int ldapport = 0;
41 #ifdef HAVE_CYRUS_SASL
42 static unsigned sasl_flags = LDAP_SASL_AUTOMATIC;
43 static char *sasl_realm = NULL;
44 static char *sasl_authc_id = NULL;
45 static char *sasl_authz_id = NULL;
46 static char *sasl_mech = NULL;
47 static char *sasl_secprops = NULL;
49 static int use_tls = 0;
50 static int not, verbose, contoper;
51 static LDAP *ld = NULL;
58 int remove ); /* flag: remove old RDN */
61 usage( const char *s )
64 "Rename LDAP entries\n\n"
65 "usage: %s [options] [dn rdn]\n"
66 " dn rdn: If given, rdn will replace the RDN of the entry specified by DN\n"
67 " If not given, the list of modifications is read from stdin or\n"
68 " from the file specified by \"-f file\" (see man page).\n"
70 " -c continuous operation mode (do not stop on errors)\n"
71 " -f file read operations from `file'\n"
72 " -r remove old RDN\n"
73 " -s newsup new superior entry\n"
76 " -d level set LDAP debugging level to `level'\n"
77 " -D binddn bind DN\n"
78 " -f file read operations from `file'\n"
79 " -h host LDAP server\n"
80 " -I use SASL Interactive mode\n"
81 " -k use Kerberos authentication\n"
82 " -K like -k, but do only step 1 of the Kerberos bind\n"
83 " -M enable Manage DSA IT control (-MM to make critical)\n"
84 " -n show what would be done but don't actually search\n"
85 " -O props SASL security properties\n"
86 " -p port port on LDAP server\n"
87 " -P version procotol version (default: 3)\n"
88 " -Q use SASL Quiet mode\n"
89 " -R realm SASL realm\n"
90 " -U user SASL authentication identity (username)\n"
91 " -v run in verbose mode (diagnostics to standard output)\n"
92 " -w passwd bind passwd (for simple authentication)\n"
93 " -W prompt for bind passwd\n"
94 " -x Simple authentication\n"
95 " -X id SASL authorization identity (\"dn:<dn>\" or \"u:<user>\")\n"
96 " -Y mech SASL mechanism\n"
97 " -Z Start TLS request (-ZZ to require successful response)\n"
100 exit( EXIT_FAILURE );
104 main(int argc, char **argv)
106 char *infile, *entrydn = NULL, *rdn = NULL, buf[ 4096 ];
108 int rc, i, remove, havedn, authmethod, version, want_bindpw, debug, manageDSAit;
110 char *newSuperior=NULL;
113 not = contoper = verbose = remove = want_bindpw =
114 debug = manageDSAit = referrals = 0;
115 authmethod = LDAP_AUTH_SIMPLE;
118 prog = (prog = strrchr(argv[0], *LDAP_DIRSEP)) == NULL ? argv[0] : prog + 1;
120 while (( i = getopt( argc, argv, "cf:rs:" "Cd:D:h:H:IkKMnO:p:P:QRU:vw:WxX:Y:Z" )) != EOF ) {
126 case 'f': /* read from file */
127 if( infile != NULL ) {
128 fprintf( stderr, "%s: -f previously specified\n" );
131 infile = strdup( optarg );
133 case 'r': /* remove old RDN */
136 case 's': /* newSuperior */
137 if( version == LDAP_VERSION2 ) {
138 fprintf( stderr, "%s: -X incompatible with LDAPv%d\n",
142 newSuperior = strdup( optarg );
143 version = LDAP_VERSION3;
151 debug |= atoi( optarg );
153 case 'D': /* bind DN */
154 if( binddn != NULL ) {
155 fprintf( stderr, "%s: -D previously specified\n" );
158 binddn = strdup( optarg );
160 case 'h': /* ldap host */
161 if( ldapuri != NULL ) {
162 fprintf( stderr, "%s: -h incompatible with -H\n" );
165 if( ldaphost != NULL ) {
166 fprintf( stderr, "%s: -h previously specified\n" );
169 ldaphost = strdup( optarg );
171 case 'H': /* ldap URI */
172 if( ldaphost != NULL ) {
173 fprintf( stderr, "%s: -H incompatible with -h\n" );
177 fprintf( stderr, "%s: -H incompatible with -p\n" );
180 if( ldapuri != NULL ) {
181 fprintf( stderr, "%s: -H previously specified\n" );
184 ldapuri = strdup( optarg );
187 #ifdef HAVE_CYRUS_SASL
188 if( version == LDAP_VERSION2 ) {
189 fprintf( stderr, "%s: -I incompatible with version %d\n",
193 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
194 fprintf( stderr, "%s: incompatible previous "
195 "authentication choice\n",
199 authmethod = LDAP_AUTH_SASL;
200 version = LDAP_VERSION3;
201 sasl_flags = LDAP_SASL_INTERACTIVE;
204 fprintf( stderr, "%s: was not compiled with SASL support\n",
206 return( EXIT_FAILURE );
208 case 'k': /* kerberos bind */
209 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
210 if( version > LDAP_VERSION2 ) {
211 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
216 if( authmethod != -1 ) {
217 fprintf( stderr, "%s: -k incompatible with previous "
218 "authentication choice\n", prog );
222 authmethod = LDAP_AUTH_KRBV4;
224 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
228 case 'K': /* kerberos bind, part one only */
229 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
230 if( version > LDAP_VERSION2 ) {
231 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
235 if( authmethod != -1 ) {
236 fprintf( stderr, "%s: incompatible with previous "
237 "authentication choice\n", prog );
241 authmethod = LDAP_AUTH_KRBV41;
243 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
244 return( EXIT_FAILURE );
248 /* enable Manage DSA IT */
249 if( version == LDAP_VERSION2 ) {
250 fprintf( stderr, "%s: -M incompatible with LDAPv%d\n",
255 version = LDAP_VERSION3;
257 case 'n': /* print deletes, don't actually do them */
261 #ifdef HAVE_CYRUS_SASL
262 if( sasl_secprops != NULL ) {
263 fprintf( stderr, "%s: -O previously specified\n" );
266 if( version == LDAP_VERSION2 ) {
267 fprintf( stderr, "%s: -O incompatible with LDAPv%d\n",
271 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
272 fprintf( stderr, "%s: incompatible previous "
273 "authentication choice\n", prog );
276 authmethod = LDAP_AUTH_SASL;
277 version = LDAP_VERSION3;
278 sasl_secprops = strdup( optarg );
280 fprintf( stderr, "%s: not compiled with SASL support\n",
282 return( EXIT_FAILURE );
287 fprintf( stderr, "%s: -p previously specified\n" );
290 ldapport = atoi( optarg );
293 switch( atoi(optarg) ) {
295 if( version == LDAP_VERSION3 ) {
296 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
300 version = LDAP_VERSION2;
303 if( version == LDAP_VERSION2 ) {
304 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
308 version = LDAP_VERSION3;
311 fprintf( stderr, "%s: protocol version should be 2 or 3\n",
314 return( EXIT_FAILURE );
317 #ifdef HAVE_CYRUS_SASL
318 if( version == LDAP_VERSION2 ) {
319 fprintf( stderr, "%s: -Q incompatible with version %d\n",
323 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
324 fprintf( stderr, "%s: incompatible previous "
325 "authentication choice\n",
329 authmethod = LDAP_AUTH_SASL;
330 version = LDAP_VERSION3;
331 sasl_flags = LDAP_SASL_QUIET;
334 fprintf( stderr, "%s: not compiled with SASL support\n",
336 return( EXIT_FAILURE );
339 #ifdef HAVE_CYRUS_SASL
340 if( sasl_realm != NULL ) {
341 fprintf( stderr, "%s: -R previously specified\n" );
344 if( version == LDAP_VERSION2 ) {
345 fprintf( stderr, "%s: -R incompatible with version %d\n",
349 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
350 fprintf( stderr, "%s: incompatible previous "
351 "authentication choice\n",
355 authmethod = LDAP_AUTH_SASL;
356 version = LDAP_VERSION3;
357 sasl_realm = strdup( optarg );
359 fprintf( stderr, "%s: not compiled with SASL support\n",
361 return( EXIT_FAILURE );
365 #ifdef HAVE_CYRUS_SASL
366 if( sasl_authc_id != NULL ) {
367 fprintf( stderr, "%s: -U previously specified\n" );
370 if( version == LDAP_VERSION2 ) {
371 fprintf( stderr, "%s: -U incompatible with version %d\n",
375 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
376 fprintf( stderr, "%s: incompatible previous "
377 "authentication choice\n",
381 authmethod = LDAP_AUTH_SASL;
382 version = LDAP_VERSION3;
383 sasl_authc_id = strdup( optarg );
385 fprintf( stderr, "%s: not compiled with SASL support\n",
387 return( EXIT_FAILURE );
390 case 'v': /* verbose mode */
393 case 'w': /* password */
394 passwd.bv_val = strdup( optarg );
398 for( p = optarg; *p == '\0'; p++ ) {
402 passwd.bv_len = strlen( passwd.bv_val );
408 #ifdef HAVE_CYRUS_SASL
409 if( sasl_mech != NULL ) {
410 fprintf( stderr, "%s: -Y previously specified\n" );
413 if( version == LDAP_VERSION2 ) {
414 fprintf( stderr, "%s: -Y incompatible with version %d\n",
418 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
419 fprintf( stderr, "%s: incompatible with authentication choice\n", prog );
422 authmethod = LDAP_AUTH_SASL;
423 version = LDAP_VERSION3;
424 sasl_mech = strdup( optarg );
426 fprintf( stderr, "%s: not compiled with SASL support\n",
428 return( EXIT_FAILURE );
432 if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
433 fprintf( stderr, "%s: incompatible with previous "
434 "authentication choice\n", prog );
437 authmethod = LDAP_AUTH_SIMPLE;
440 #ifdef HAVE_CYRUS_SASL
441 if( sasl_authz_id != NULL ) {
442 fprintf( stderr, "%s: -X previously specified\n" );
445 if( version == LDAP_VERSION2 ) {
446 fprintf( stderr, "%s: -X incompatible with LDAPv%d\n",
450 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
451 fprintf( stderr, "%s: -X incompatible with "
452 "authentication choice\n", prog );
455 authmethod = LDAP_AUTH_SASL;
456 version = LDAP_VERSION3;
457 sasl_authz_id = strdup( optarg );
459 fprintf( stderr, "%s: not compiled with SASL support\n",
461 return( EXIT_FAILURE );
466 if( version == LDAP_VERSION2 ) {
467 fprintf( stderr, "%s: -Z incompatible with version %d\n",
471 version = LDAP_VERSION3;
474 fprintf( stderr, "%s: not compiled with TLS support\n",
476 return( EXIT_FAILURE );
480 fprintf( stderr, "%s: unrecongized option -%c\n",
483 return( EXIT_FAILURE );
488 version = LDAP_VERSION3;
490 if (authmethod == -1 && version > LDAP_VERSION2) {
491 #ifdef HAVE_CYRUS_SASL
492 authmethod = LDAP_AUTH_SASL;
494 authmethod = LDAP_AUTH_SIMPLE;
499 if (argc - optind == 2) {
500 if (( rdn = strdup( argv[argc - 1] )) == NULL ) {
502 return( EXIT_FAILURE );
504 if (( entrydn = strdup( argv[argc - 2] )) == NULL ) {
506 return( EXIT_FAILURE );
509 } else if ( argc - optind != 0 ) {
510 fprintf( stderr, "%s: invalid number of arguments (%d), "
511 "only two allowed\n", prog, argc-optind );
513 return( EXIT_FAILURE );
516 if ( infile != NULL ) {
517 if (( fp = fopen( infile, "r" )) == NULL ) {
519 return( EXIT_FAILURE );
526 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
527 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
529 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
530 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
535 (void) SIGNAL( SIGPIPE, SIG_IGN );
538 if( ( ldaphost != NULL || ldapport ) && ( ldapuri == NULL ) ) {
540 fprintf( stderr, "ldap_init( %s, %d )\n",
541 ldaphost != NULL ? ldaphost : "<DEFAULT>",
544 ld = ldap_init( ldaphost, ldapport );
548 fprintf( stderr, "ldap_initialize( %s )\n",
549 ldapuri != NULL ? ldapuri : "<DEFAULT>" );
551 (void) ldap_initialize( &ld, ldapuri );
555 fprintf( stderr, "Could not create LDAP session handle (%d): %s\n", rc );
560 if( ldap_set_option( ld, LDAP_OPT_REFERRALS,
561 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
563 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
564 referrals ? "on" : "off" );
568 if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version )
569 != LDAP_OPT_SUCCESS )
571 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
576 if ( use_tls && ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS ) {
578 ldap_perror( ld, "ldap_start_tls" );
579 return( EXIT_FAILURE );
581 fprintf( stderr, "WARNING: could not start TLS\n" );
585 passwd.bv_val = getpassphrase("Enter LDAP Password: ");
586 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
589 if ( authmethod == LDAP_AUTH_SASL ) {
590 #ifdef HAVE_CYRUS_SASL
593 if( sasl_secprops != NULL ) {
594 rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
595 (void *) sasl_secprops );
597 if( rc != LDAP_OPT_SUCCESS ) {
599 "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
601 return( EXIT_FAILURE );
605 defaults = lutil_sasl_defaults( ld,
612 rc = ldap_sasl_interactive_bind_s( ld, binddn,
613 sasl_mech, NULL, NULL,
614 sasl_flags, lutil_sasl_interact, defaults );
616 if( rc != LDAP_SUCCESS ) {
617 ldap_perror( ld, "ldap_sasl_interactive_bind_s" );
618 return( EXIT_FAILURE );
621 fprintf( stderr, "%s: not compiled with SASL support\n",
623 return( EXIT_FAILURE );
627 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
629 ldap_perror( ld, "ldap_bind" );
630 return( EXIT_FAILURE );
637 LDAPControl *ctrls[2];
641 c.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
642 c.ldctl_value.bv_val = NULL;
643 c.ldctl_value.bv_len = 0;
644 c.ldctl_iscritical = manageDSAit > 1;
646 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, ctrls );
648 if( err != LDAP_OPT_SUCCESS ) {
649 fprintf( stderr, "Could not set ManageDSAit %scontrol\n",
650 c.ldctl_iscritical ? "critical " : "" );
651 if( c.ldctl_iscritical ) {
652 exit( EXIT_FAILURE );
659 rc = domodrdn( ld, entrydn, rdn, newSuperior, remove );
660 else while ((rc == 0 || contoper) && fgets(buf, sizeof(buf), fp) != NULL) {
661 if ( *buf != '\0' ) { /* blank lines optional, skip */
662 buf[ strlen( buf ) - 1 ] = '\0'; /* remove nl */
664 if ( havedn ) { /* have DN, get RDN */
665 if (( rdn = strdup( buf )) == NULL ) {
667 return( EXIT_FAILURE );
669 rc = domodrdn(ld, entrydn, rdn, newSuperior, remove );
671 } else if ( !havedn ) { /* don't have DN yet */
672 if (( entrydn = strdup( buf )) == NULL ) {
674 return( EXIT_FAILURE );
692 int remove ) /* flag: remove old RDN */
695 char *matcheddn=NULL, *text=NULL, **refs=NULL;
699 printf( "Renaming \"%s\"\n", dn );
700 printf( "\tnew rdn=\"%s\" (%s old rdn)\n",
701 rdn, remove ? "delete" : "keep" );
702 if( newSuperior != NULL ) {
703 printf("\tnew parent=\"%s\"\n", newSuperior);
707 if( not ) return LDAP_SUCCESS;
709 rc = ldap_rename( ld, dn, rdn, newSuperior, remove,
712 if ( rc != LDAP_SUCCESS ) {
713 fprintf( stderr, "%s: ldap_rename: %s (%d)\n",
714 prog, ldap_err2string( rc ), rc );
718 rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, NULL, &res );
720 ldap_perror( ld, "ldapmodrdn: ldap_result" );
724 rc = ldap_parse_result( ld, res, &code, &matcheddn, &text, &refs, NULL, 1 );
726 if( rc != LDAP_SUCCESS ) {
727 fprintf( stderr, "%s: ldap_parse_result: %s (%d)\n",
728 prog, ldap_err2string( rc ), rc );
732 if( verbose || code != LDAP_SUCCESS ||
733 (matcheddn && *matcheddn) || (text && *text) || (refs && *refs) )
735 printf( "Rename Result: %s (%d)\n",
736 ldap_err2string( code ), code );
738 if( text && *text ) {
739 printf( "Additional info: %s\n", text );
742 if( matcheddn && *matcheddn ) {
743 printf( "Matched DN: %s\n", matcheddn );
748 for( i=0; refs[i]; i++ ) {
749 printf("Referral: %s\n", refs[i] );
755 ber_memfree( matcheddn );
756 ber_memvfree( (void **) refs );