3 * Copyright 1998-2002 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>
33 #include "lutil_ldap.h"
34 #include "ldap_defaults.h"
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;
50 static int use_tls = 0;
51 static int not, verbose, contoper;
52 static LDAP *ld = NULL;
59 int remove ); /* flag: remove old RDN */
62 usage( const char *s )
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"
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"
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"
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"
106 exit( EXIT_FAILURE );
110 main(int argc, char **argv)
112 char *infile, *entrydn = NULL, *rdn = NULL, buf[ 4096 ];
114 int rc, i, remove, havedn, authmethod, version, want_bindpw, debug, manageDSAit, noop, crit;
116 char *newSuperior=NULL;
117 char *pw_file = NULL;
118 char *control, *cvalue;
121 not = contoper = verbose = remove = want_bindpw =
122 debug = manageDSAit = noop = referrals = 0;
126 prog = lutil_progname( "ldapmodrdn", argc, argv );
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 )
136 case 'E': /* modrdn controls */
137 if( version == LDAP_VERSION2 ) {
138 fprintf( stderr, "%s: -E incompatible with LDAPv%d\n",
143 /* should be extended to support comma separated list of
144 * [!]key[=value] parameters, e.g. -E !foo,bar=567
149 if( optarg[0] == '!' ) {
154 control = strdup( optarg );
155 if ( (cvalue = strchr( control, '=' )) != NULL ) {
158 fprintf( stderr, "Invalid modrdn control name: %s\n", control );
161 case 'f': /* read from file */
162 if( infile != NULL ) {
163 fprintf( stderr, "%s: -f previously specified\n", prog );
166 infile = strdup( optarg );
168 case 'r': /* remove old RDN */
171 case 's': /* newSuperior */
172 if( version == LDAP_VERSION2 ) {
173 fprintf( stderr, "%s: -X incompatible with LDAPv%d\n",
177 newSuperior = strdup( optarg );
178 version = LDAP_VERSION3;
186 debug |= atoi( optarg );
188 case 'D': /* bind DN */
189 if( binddn != NULL ) {
190 fprintf( stderr, "%s: -D previously specified\n", prog );
193 binddn = strdup( optarg );
195 case 'e': /* general controls */
196 if( version == LDAP_VERSION2 ) {
197 fprintf( stderr, "%s: -e incompatible with LDAPv%d\n",
202 /* should be extended to support comma separated list of
203 * [!]key[=value] parameters, e.g. -e !foo,bar=567
208 if( optarg[0] == '!' ) {
213 control = strdup( optarg );
214 if ( (cvalue = strchr( control, '=' )) != NULL ) {
218 if ( strcasecmp( control, "manageDSAit" ) == 0 ) {
219 if( cvalue != NULL ) {
220 fprintf( stderr, "manageDSAit: no control value expected" );
225 manageDSAit = 1 + crit;
229 } else if ( strcasecmp( control, "noop" ) == 0 ) {
230 if( cvalue != NULL ) {
231 fprintf( stderr, "noop: no control value expected" );
241 fprintf( stderr, "Invalid general control name: %s\n", control );
245 case 'h': /* ldap host */
246 if( ldapuri != NULL ) {
247 fprintf( stderr, "%s: -h incompatible with -H\n", prog );
250 if( ldaphost != NULL ) {
251 fprintf( stderr, "%s: -h previously specified\n", prog );
254 ldaphost = strdup( optarg );
256 case 'H': /* ldap URI */
257 if( ldaphost != NULL ) {
258 fprintf( stderr, "%s: -H incompatible with -h\n", prog );
262 fprintf( stderr, "%s: -H incompatible with -p\n", prog );
265 if( ldapuri != NULL ) {
266 fprintf( stderr, "%s: -H previously specified\n", prog );
269 ldapuri = strdup( optarg );
272 #ifdef HAVE_CYRUS_SASL
273 if( version == LDAP_VERSION2 ) {
274 fprintf( stderr, "%s: -I incompatible with version %d\n",
278 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
279 fprintf( stderr, "%s: incompatible previous "
280 "authentication choice\n",
284 authmethod = LDAP_AUTH_SASL;
285 version = LDAP_VERSION3;
286 sasl_flags = LDAP_SASL_INTERACTIVE;
289 fprintf( stderr, "%s: was not compiled with SASL support\n",
291 return( EXIT_FAILURE );
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",
301 if( authmethod != -1 ) {
302 fprintf( stderr, "%s: -k incompatible with previous "
303 "authentication choice\n", prog );
307 authmethod = LDAP_AUTH_KRBV4;
309 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
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",
320 if( authmethod != -1 ) {
321 fprintf( stderr, "%s: incompatible with previous "
322 "authentication choice\n", prog );
326 authmethod = LDAP_AUTH_KRBV41;
328 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
329 return( EXIT_FAILURE );
333 /* enable Manage DSA IT */
334 if( version == LDAP_VERSION2 ) {
335 fprintf( stderr, "%s: -M incompatible with LDAPv%d\n",
340 version = LDAP_VERSION3;
342 case 'n': /* print deletes, don't actually do them */
346 #ifdef HAVE_CYRUS_SASL
347 if( sasl_secprops != NULL ) {
348 fprintf( stderr, "%s: -O previously specified\n", prog );
351 if( version == LDAP_VERSION2 ) {
352 fprintf( stderr, "%s: -O incompatible with LDAPv%d\n",
356 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
357 fprintf( stderr, "%s: incompatible previous "
358 "authentication choice\n", prog );
361 authmethod = LDAP_AUTH_SASL;
362 version = LDAP_VERSION3;
363 sasl_secprops = strdup( optarg );
365 fprintf( stderr, "%s: not compiled with SASL support\n",
367 return( EXIT_FAILURE );
372 fprintf( stderr, "%s: -p previously specified\n", prog );
375 ldapport = atoi( optarg );
378 switch( atoi(optarg) ) {
380 if( version == LDAP_VERSION3 ) {
381 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
385 version = LDAP_VERSION2;
388 if( version == LDAP_VERSION2 ) {
389 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
393 version = LDAP_VERSION3;
396 fprintf( stderr, "%s: protocol version should be 2 or 3\n",
399 return( EXIT_FAILURE );
402 #ifdef HAVE_CYRUS_SASL
403 if( version == LDAP_VERSION2 ) {
404 fprintf( stderr, "%s: -Q incompatible with version %d\n",
408 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
409 fprintf( stderr, "%s: incompatible previous "
410 "authentication choice\n",
414 authmethod = LDAP_AUTH_SASL;
415 version = LDAP_VERSION3;
416 sasl_flags = LDAP_SASL_QUIET;
419 fprintf( stderr, "%s: not compiled with SASL support\n",
421 return( EXIT_FAILURE );
424 #ifdef HAVE_CYRUS_SASL
425 if( sasl_realm != NULL ) {
426 fprintf( stderr, "%s: -R previously specified\n", prog );
429 if( version == LDAP_VERSION2 ) {
430 fprintf( stderr, "%s: -R incompatible with version %d\n",
434 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
435 fprintf( stderr, "%s: incompatible previous "
436 "authentication choice\n",
440 authmethod = LDAP_AUTH_SASL;
441 version = LDAP_VERSION3;
442 sasl_realm = strdup( optarg );
444 fprintf( stderr, "%s: not compiled with SASL support\n",
446 return( EXIT_FAILURE );
450 #ifdef HAVE_CYRUS_SASL
451 if( sasl_authc_id != NULL ) {
452 fprintf( stderr, "%s: -U previously specified\n", prog );
455 if( version == LDAP_VERSION2 ) {
456 fprintf( stderr, "%s: -U incompatible with version %d\n",
460 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
461 fprintf( stderr, "%s: incompatible previous "
462 "authentication choice\n",
466 authmethod = LDAP_AUTH_SASL;
467 version = LDAP_VERSION3;
468 sasl_authc_id = strdup( optarg );
470 fprintf( stderr, "%s: not compiled with SASL support\n",
472 return( EXIT_FAILURE );
475 case 'v': /* verbose mode */
478 case 'w': /* password */
479 passwd.bv_val = strdup( optarg );
483 for( p = optarg; *p != '\0'; p++ ) {
487 passwd.bv_len = strlen( passwd.bv_val );
496 #ifdef HAVE_CYRUS_SASL
497 if( sasl_mech != NULL ) {
498 fprintf( stderr, "%s: -Y previously specified\n", prog );
501 if( version == LDAP_VERSION2 ) {
502 fprintf( stderr, "%s: -Y incompatible with version %d\n",
506 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
507 fprintf( stderr, "%s: incompatible with authentication choice\n", prog );
510 authmethod = LDAP_AUTH_SASL;
511 version = LDAP_VERSION3;
512 sasl_mech = strdup( optarg );
514 fprintf( stderr, "%s: not compiled with SASL support\n",
516 return( EXIT_FAILURE );
520 if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
521 fprintf( stderr, "%s: incompatible with previous "
522 "authentication choice\n", prog );
525 authmethod = LDAP_AUTH_SIMPLE;
528 #ifdef HAVE_CYRUS_SASL
529 if( sasl_authz_id != NULL ) {
530 fprintf( stderr, "%s: -X previously specified\n", prog );
533 if( version == LDAP_VERSION2 ) {
534 fprintf( stderr, "%s: -X incompatible with LDAPv%d\n",
538 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
539 fprintf( stderr, "%s: -X incompatible with "
540 "authentication choice\n", prog );
543 authmethod = LDAP_AUTH_SASL;
544 version = LDAP_VERSION3;
545 sasl_authz_id = strdup( optarg );
547 fprintf( stderr, "%s: not compiled with SASL support\n",
549 return( EXIT_FAILURE );
554 if( version == LDAP_VERSION2 ) {
555 fprintf( stderr, "%s: -Z incompatible with version %d\n",
559 version = LDAP_VERSION3;
562 fprintf( stderr, "%s: not compiled with TLS support\n",
564 return( EXIT_FAILURE );
568 fprintf( stderr, "%s: unrecognized option -%c\n",
571 return( EXIT_FAILURE );
576 version = LDAP_VERSION3;
578 if (authmethod == -1 && version > LDAP_VERSION2) {
579 #ifdef HAVE_CYRUS_SASL
580 authmethod = LDAP_AUTH_SASL;
582 authmethod = LDAP_AUTH_SIMPLE;
587 if (argc - optind == 2) {
588 if (( rdn = strdup( argv[argc - 1] )) == NULL ) {
590 return( EXIT_FAILURE );
592 if (( entrydn = strdup( argv[argc - 2] )) == NULL ) {
594 return( EXIT_FAILURE );
597 } else if ( argc - optind != 0 ) {
598 fprintf( stderr, "%s: invalid number of arguments (%d), "
599 "only two allowed\n", prog, argc-optind );
601 return( EXIT_FAILURE );
604 if ( infile != NULL ) {
605 if (( fp = fopen( infile, "r" )) == NULL ) {
607 return( EXIT_FAILURE );
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 );
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 );
623 (void) SIGNAL( SIGPIPE, SIG_IGN );
626 if( ( ldaphost != NULL || ldapport ) && ( ldapuri == NULL ) ) {
628 fprintf( stderr, "ldap_init( %s, %d )\n",
629 ldaphost != NULL ? ldaphost : "<DEFAULT>",
633 ld = ldap_init( ldaphost, ldapport );
635 perror("ldapmodify: ldap_init");
641 fprintf( stderr, "ldap_initialize( %s )\n",
642 ldapuri != NULL ? ldapuri : "<DEFAULT>" );
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) );
654 if( ldap_set_option( ld, LDAP_OPT_REFERRALS,
655 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
657 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
658 referrals ? "on" : "off" );
662 if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version )
663 != LDAP_OPT_SUCCESS )
665 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
670 if ( use_tls && ( ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS )) {
671 ldap_perror( ld, "ldap_start_tls" );
673 return( EXIT_FAILURE );
677 if ( pw_file || want_bindpw ) {
679 rc = lutil_get_filed_password( pw_file, &passwd );
680 if( rc ) return EXIT_FAILURE;
682 passwd.bv_val = getpassphrase( "Enter LDAP Password: " );
683 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
687 if ( authmethod == LDAP_AUTH_SASL ) {
688 #ifdef HAVE_CYRUS_SASL
691 if( sasl_secprops != NULL ) {
692 rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
693 (void *) sasl_secprops );
695 if( rc != LDAP_OPT_SUCCESS ) {
697 "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
699 return( EXIT_FAILURE );
703 defaults = lutil_sasl_defaults( ld,
710 rc = ldap_sasl_interactive_bind_s( ld, binddn,
711 sasl_mech, NULL, NULL,
712 sasl_flags, lutil_sasl_interact, defaults );
714 if( rc != LDAP_SUCCESS ) {
715 ldap_perror( ld, "ldap_sasl_interactive_bind_s" );
716 return( EXIT_FAILURE );
719 fprintf( stderr, "%s: not compiled with SASL support\n",
721 return( EXIT_FAILURE );
725 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
727 ldap_perror( ld, "ldap_bind" );
728 return( EXIT_FAILURE );
735 LDAPControl *ctrls[2];
739 c.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
740 c.ldctl_value.bv_val = NULL;
741 c.ldctl_value.bv_len = 0;
742 c.ldctl_iscritical = manageDSAit > 1;
744 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, ctrls );
746 if( err != LDAP_OPT_SUCCESS ) {
747 fprintf( stderr, "Could not set ManageDSAit %scontrol\n",
748 c.ldctl_iscritical ? "critical " : "" );
749 if( c.ldctl_iscritical ) {
750 exit( EXIT_FAILURE );
757 rc = domodrdn( ld, entrydn, rdn, newSuperior, remove );
758 else while ((rc == 0 || contoper) && fgets(buf, sizeof(buf), fp) != NULL) {
759 if ( *buf != '\0' ) { /* blank lines optional, skip */
760 buf[ strlen( buf ) - 1 ] = '\0'; /* remove nl */
762 if ( havedn ) { /* have DN, get RDN */
763 if (( rdn = strdup( buf )) == NULL ) {
765 return( EXIT_FAILURE );
767 rc = domodrdn(ld, entrydn, rdn, newSuperior, remove );
769 } else if ( !havedn ) { /* don't have DN yet */
770 if (( entrydn = strdup( buf )) == NULL ) {
772 return( EXIT_FAILURE );
790 int remove ) /* flag: remove old RDN */
793 char *matcheddn=NULL, *text=NULL, **refs=NULL;
797 printf( "Renaming \"%s\"\n", dn );
798 printf( "\tnew rdn=\"%s\" (%s old rdn)\n",
799 rdn, remove ? "delete" : "keep" );
800 if( newSuperior != NULL ) {
801 printf("\tnew parent=\"%s\"\n", newSuperior);
805 if( not ) return LDAP_SUCCESS;
807 rc = ldap_rename( ld, dn, rdn, newSuperior, remove,
810 if ( rc != LDAP_SUCCESS ) {
811 fprintf( stderr, "%s: ldap_rename: %s (%d)\n",
812 prog, ldap_err2string( rc ), rc );
816 rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, NULL, &res );
818 ldap_perror( ld, "ldapmodrdn: ldap_result" );
822 rc = ldap_parse_result( ld, res, &code, &matcheddn, &text, &refs, NULL, 1 );
824 if( rc != LDAP_SUCCESS ) {
825 fprintf( stderr, "%s: ldap_parse_result: %s (%d)\n",
826 prog, ldap_err2string( rc ), rc );
830 if( verbose || code != LDAP_SUCCESS ||
831 (matcheddn && *matcheddn) || (text && *text) || (refs && *refs) )
833 printf( "Rename Result: %s (%d)\n",
834 ldap_err2string( code ), code );
836 if( text && *text ) {
837 printf( "Additional info: %s\n", text );
840 if( matcheddn && *matcheddn ) {
841 printf( "Matched DN: %s\n", matcheddn );
846 for( i=0; refs[i]; i++ ) {
847 printf("Referral: %s\n", refs[i] );
853 ber_memfree( matcheddn );
854 ber_memvfree( (void **) refs );