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 " [!]authzid=<authzid> (\"dn:<dn>\" or \"u:<user>\")\n"
81 " [!]manageDSAit (alternate form, see -M)\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"
107 exit( EXIT_FAILURE );
111 main(int argc, char **argv)
113 char *infile, *entrydn = NULL, *rdn = NULL, buf[ 4096 ];
115 int rc, retval, i, remove, havedn, authmethod, version;
116 int want_bindpw, debug, manageDSAit, noop, crit;
118 char *newSuperior=NULL;
119 char *pw_file = NULL;
120 char *control, *cvalue;
121 char *authzid = NULL;
124 not = contoper = verbose = remove = want_bindpw =
125 debug = manageDSAit = noop = referrals = 0;
129 prog = lutil_progname( "ldapmodrdn", argc, argv );
131 while (( i = getopt( argc, argv, "cf:rs:"
132 "Cd:D:e:h:H:IkKMnO:p:P:QR:U:vw:WxX:y:Y:Z" )) != EOF )
139 case 'E': /* modrdn controls */
140 if( version == LDAP_VERSION2 ) {
141 fprintf( stderr, "%s: -E incompatible with LDAPv%d\n",
146 /* should be extended to support comma separated list of
147 * [!]key[=value] parameters, e.g. -E !foo,bar=567
152 if( optarg[0] == '!' ) {
157 control = strdup( optarg );
158 if ( (cvalue = strchr( control, '=' )) != NULL ) {
161 fprintf( stderr, "Invalid modrdn control name: %s\n", control );
164 case 'f': /* read from file */
165 if( infile != NULL ) {
166 fprintf( stderr, "%s: -f previously specified\n", prog );
169 infile = strdup( optarg );
171 case 'r': /* remove old RDN */
174 case 's': /* newSuperior */
175 if( version == LDAP_VERSION2 ) {
176 fprintf( stderr, "%s: -X incompatible with LDAPv%d\n",
180 newSuperior = strdup( optarg );
181 version = LDAP_VERSION3;
189 debug |= atoi( optarg );
191 case 'D': /* bind DN */
192 if( binddn != NULL ) {
193 fprintf( stderr, "%s: -D previously specified\n", prog );
196 binddn = strdup( optarg );
198 case 'e': /* general controls */
199 if( version == LDAP_VERSION2 ) {
200 fprintf( stderr, "%s: -e incompatible with LDAPv%d\n",
205 /* should be extended to support comma separated list of
206 * [!]key[=value] parameters, e.g. -e !foo,bar=567
211 if( optarg[0] == '!' ) {
216 control = strdup( optarg );
217 if ( (cvalue = strchr( control, '=' )) != NULL ) {
221 if ( strcasecmp( control, "authzid" ) == 0 ) {
222 if( authzid != NULL ) {
223 fprintf( stderr, "authzid control previously specified\n");
226 if( cvalue == NULL ) {
227 fprintf( stderr, "authzid: control value expected\n" );
232 fprintf( stderr, "authzid: must be marked critical\n" );
237 assert( authzid == NULL );
240 } else if ( strcasecmp( control, "manageDSAit" ) == 0 ) {
242 fprintf( stderr, "manageDSAit control previously specified\n");
245 if( cvalue != NULL ) {
246 fprintf( stderr, "manageDSAit: no control value expected\n" );
251 manageDSAit = 1 + crit;
253 } else if ( strcasecmp( control, "noop" ) == 0 ) {
255 fprintf( stderr, "noop control previously specified\n");
258 if( cvalue != NULL ) {
259 fprintf( stderr, "noop: no control value expected\n" );
267 fprintf( stderr, "Invalid general control name: %s\n", control );
272 case 'h': /* ldap host */
273 if( ldapuri != NULL ) {
274 fprintf( stderr, "%s: -h incompatible with -H\n", prog );
277 if( ldaphost != NULL ) {
278 fprintf( stderr, "%s: -h previously specified\n", prog );
281 ldaphost = strdup( optarg );
283 case 'H': /* ldap URI */
284 if( ldaphost != NULL ) {
285 fprintf( stderr, "%s: -H incompatible with -h\n", prog );
289 fprintf( stderr, "%s: -H incompatible with -p\n", prog );
292 if( ldapuri != NULL ) {
293 fprintf( stderr, "%s: -H previously specified\n", prog );
296 ldapuri = strdup( optarg );
299 #ifdef HAVE_CYRUS_SASL
300 if( version == LDAP_VERSION2 ) {
301 fprintf( stderr, "%s: -I incompatible with version %d\n",
305 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
306 fprintf( stderr, "%s: incompatible previous "
307 "authentication choice\n",
311 authmethod = LDAP_AUTH_SASL;
312 version = LDAP_VERSION3;
313 sasl_flags = LDAP_SASL_INTERACTIVE;
316 fprintf( stderr, "%s: was not compiled with SASL support\n",
318 return( EXIT_FAILURE );
320 case 'k': /* kerberos bind */
321 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
322 if( version > LDAP_VERSION2 ) {
323 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
328 if( authmethod != -1 ) {
329 fprintf( stderr, "%s: -k incompatible with previous "
330 "authentication choice\n", prog );
334 authmethod = LDAP_AUTH_KRBV4;
336 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
340 case 'K': /* kerberos bind, part one only */
341 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
342 if( version > LDAP_VERSION2 ) {
343 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
347 if( authmethod != -1 ) {
348 fprintf( stderr, "%s: incompatible with previous "
349 "authentication choice\n", prog );
353 authmethod = LDAP_AUTH_KRBV41;
355 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
356 return( EXIT_FAILURE );
360 /* enable Manage DSA IT */
361 if( version == LDAP_VERSION2 ) {
362 fprintf( stderr, "%s: -M incompatible with LDAPv%d\n",
367 version = LDAP_VERSION3;
369 case 'n': /* print deletes, don't actually do them */
373 #ifdef HAVE_CYRUS_SASL
374 if( sasl_secprops != NULL ) {
375 fprintf( stderr, "%s: -O previously specified\n", prog );
378 if( version == LDAP_VERSION2 ) {
379 fprintf( stderr, "%s: -O incompatible with LDAPv%d\n",
383 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
384 fprintf( stderr, "%s: incompatible previous "
385 "authentication choice\n", prog );
388 authmethod = LDAP_AUTH_SASL;
389 version = LDAP_VERSION3;
390 sasl_secprops = strdup( optarg );
392 fprintf( stderr, "%s: not compiled with SASL support\n",
394 return( EXIT_FAILURE );
399 fprintf( stderr, "%s: -p previously specified\n", prog );
402 ldapport = atoi( optarg );
405 switch( atoi(optarg) ) {
407 if( version == LDAP_VERSION3 ) {
408 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
412 version = LDAP_VERSION2;
415 if( version == LDAP_VERSION2 ) {
416 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
420 version = LDAP_VERSION3;
423 fprintf( stderr, "%s: protocol version should be 2 or 3\n",
426 return( EXIT_FAILURE );
430 #ifdef HAVE_CYRUS_SASL
431 if( version == LDAP_VERSION2 ) {
432 fprintf( stderr, "%s: -Q incompatible with version %d\n",
436 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
437 fprintf( stderr, "%s: incompatible previous "
438 "authentication choice\n",
442 authmethod = LDAP_AUTH_SASL;
443 version = LDAP_VERSION3;
444 sasl_flags = LDAP_SASL_QUIET;
447 fprintf( stderr, "%s: not compiled with SASL support\n",
449 return( EXIT_FAILURE );
452 #ifdef HAVE_CYRUS_SASL
453 if( sasl_realm != NULL ) {
454 fprintf( stderr, "%s: -R previously specified\n", prog );
457 if( version == LDAP_VERSION2 ) {
458 fprintf( stderr, "%s: -R incompatible with version %d\n",
462 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
463 fprintf( stderr, "%s: incompatible previous "
464 "authentication choice\n",
468 authmethod = LDAP_AUTH_SASL;
469 version = LDAP_VERSION3;
470 sasl_realm = strdup( optarg );
472 fprintf( stderr, "%s: not compiled with SASL support\n",
474 return( EXIT_FAILURE );
478 #ifdef HAVE_CYRUS_SASL
479 if( sasl_authc_id != NULL ) {
480 fprintf( stderr, "%s: -U previously specified\n", prog );
483 if( version == LDAP_VERSION2 ) {
484 fprintf( stderr, "%s: -U incompatible with version %d\n",
488 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
489 fprintf( stderr, "%s: incompatible previous "
490 "authentication choice\n",
494 authmethod = LDAP_AUTH_SASL;
495 version = LDAP_VERSION3;
496 sasl_authc_id = strdup( optarg );
498 fprintf( stderr, "%s: not compiled with SASL support\n",
500 return( EXIT_FAILURE );
503 case 'v': /* verbose mode */
506 case 'w': /* password */
507 passwd.bv_val = strdup( optarg );
511 for( p = optarg; *p != '\0'; p++ ) {
515 passwd.bv_len = strlen( passwd.bv_val );
524 #ifdef HAVE_CYRUS_SASL
525 if( sasl_mech != NULL ) {
526 fprintf( stderr, "%s: -Y previously specified\n", prog );
529 if( version == LDAP_VERSION2 ) {
530 fprintf( stderr, "%s: -Y incompatible with version %d\n",
534 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
535 fprintf( stderr, "%s: incompatible with authentication choice\n", prog );
538 authmethod = LDAP_AUTH_SASL;
539 version = LDAP_VERSION3;
540 sasl_mech = strdup( optarg );
542 fprintf( stderr, "%s: not compiled with SASL support\n",
544 return( EXIT_FAILURE );
548 if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
549 fprintf( stderr, "%s: incompatible with previous "
550 "authentication choice\n", prog );
553 authmethod = LDAP_AUTH_SIMPLE;
556 #ifdef HAVE_CYRUS_SASL
557 if( sasl_authz_id != NULL ) {
558 fprintf( stderr, "%s: -X previously specified\n", prog );
561 if( version == LDAP_VERSION2 ) {
562 fprintf( stderr, "%s: -X incompatible with LDAPv%d\n",
566 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
567 fprintf( stderr, "%s: -X incompatible with "
568 "authentication choice\n", prog );
571 authmethod = LDAP_AUTH_SASL;
572 version = LDAP_VERSION3;
573 sasl_authz_id = strdup( optarg );
575 fprintf( stderr, "%s: not compiled with SASL support\n",
577 return( EXIT_FAILURE );
582 if( version == LDAP_VERSION2 ) {
583 fprintf( stderr, "%s: -Z incompatible with version %d\n",
587 version = LDAP_VERSION3;
590 fprintf( stderr, "%s: not compiled with TLS support\n",
592 return( EXIT_FAILURE );
596 fprintf( stderr, "%s: unrecognized option -%c\n",
599 return( EXIT_FAILURE );
604 version = LDAP_VERSION3;
606 if (authmethod == -1 && version > LDAP_VERSION2) {
607 #ifdef HAVE_CYRUS_SASL
608 authmethod = LDAP_AUTH_SASL;
610 authmethod = LDAP_AUTH_SIMPLE;
615 if (argc - optind == 2) {
616 if (( rdn = strdup( argv[argc - 1] )) == NULL ) {
618 return( EXIT_FAILURE );
620 if (( entrydn = strdup( argv[argc - 2] )) == NULL ) {
622 return( EXIT_FAILURE );
625 } else if ( argc - optind != 0 ) {
626 fprintf( stderr, "%s: invalid number of arguments (%d), "
627 "only two allowed\n", prog, argc-optind );
629 return( EXIT_FAILURE );
632 if ( infile != NULL ) {
633 if (( fp = fopen( infile, "r" )) == NULL ) {
635 return( EXIT_FAILURE );
642 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
643 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
645 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
646 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
651 (void) SIGNAL( SIGPIPE, SIG_IGN );
654 if( ( ldaphost != NULL || ldapport ) && ( ldapuri == NULL ) ) {
656 fprintf( stderr, "ldap_init( %s, %d )\n",
657 ldaphost != NULL ? ldaphost : "<DEFAULT>",
661 ld = ldap_init( ldaphost, ldapport );
663 perror("ldapmodify: ldap_init");
669 fprintf( stderr, "ldap_initialize( %s )\n",
670 ldapuri != NULL ? ldapuri : "<DEFAULT>" );
673 rc = ldap_initialize( &ld, ldapuri );
674 if( rc != LDAP_SUCCESS ) {
675 fprintf( stderr, "Could not create LDAP session handle (%d): %s\n",
676 rc, ldap_err2string(rc) );
682 if( ldap_set_option( ld, LDAP_OPT_REFERRALS,
683 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
685 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
686 referrals ? "on" : "off" );
690 if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version )
691 != LDAP_OPT_SUCCESS )
693 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
698 if ( use_tls && ( ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS )) {
699 ldap_perror( ld, "ldap_start_tls" );
701 return( EXIT_FAILURE );
705 if ( pw_file || want_bindpw ) {
707 rc = lutil_get_filed_password( pw_file, &passwd );
708 if( rc ) return EXIT_FAILURE;
710 passwd.bv_val = getpassphrase( "Enter LDAP Password: " );
711 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
715 if ( authmethod == LDAP_AUTH_SASL ) {
716 #ifdef HAVE_CYRUS_SASL
719 if( sasl_secprops != NULL ) {
720 rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
721 (void *) sasl_secprops );
723 if( rc != LDAP_OPT_SUCCESS ) {
725 "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
727 return( EXIT_FAILURE );
731 defaults = lutil_sasl_defaults( ld,
738 rc = ldap_sasl_interactive_bind_s( ld, binddn,
739 sasl_mech, NULL, NULL,
740 sasl_flags, lutil_sasl_interact, defaults );
742 if( rc != LDAP_SUCCESS ) {
743 ldap_perror( ld, "ldap_sasl_interactive_bind_s" );
744 return( EXIT_FAILURE );
747 fprintf( stderr, "%s: not compiled with SASL support\n",
749 return( EXIT_FAILURE );
753 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
755 ldap_perror( ld, "ldap_bind" );
756 return( EXIT_FAILURE );
760 if ( authzid || manageDSAit || noop ) {
761 int err, crit=0, i=0;
763 LDAPControl *ctrls[4];
766 c[i].ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
767 c[i].ldctl_value.bv_val = authzid;
768 c[i].ldctl_value.bv_len = strlen( authzid );
769 c[i].ldctl_iscritical = 1;
771 if( c[i].ldctl_iscritical ) crit++;
777 c[i].ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
778 c[i].ldctl_value.bv_val = NULL;
779 c[i].ldctl_value.bv_len = 0;
780 c[i].ldctl_iscritical = manageDSAit > 1;
782 if( c[i].ldctl_iscritical ) crit++;
788 c[i].ldctl_oid = LDAP_CONTROL_NOOP;
789 c[i].ldctl_value.bv_val = NULL;
790 c[i].ldctl_value.bv_len = 0;
791 c[i].ldctl_iscritical = noop > 1;
793 if( c[i].ldctl_iscritical ) crit++;
798 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, ctrls );
800 if( err != LDAP_OPT_SUCCESS ) {
801 fprintf( stderr, "Could not set %scontrols\n",
802 crit ? "critical " : "" );
811 retval = domodrdn( ld, entrydn, rdn, newSuperior, remove );
812 else while ((rc == 0 || contoper) && fgets(buf, sizeof(buf), fp) != NULL) {
813 if ( *buf != '\0' ) { /* blank lines optional, skip */
814 buf[ strlen( buf ) - 1 ] = '\0'; /* remove nl */
816 if ( havedn ) { /* have DN, get RDN */
817 if (( rdn = strdup( buf )) == NULL ) {
819 return( EXIT_FAILURE );
821 rc = domodrdn(ld, entrydn, rdn, newSuperior, remove );
825 } else if ( !havedn ) { /* don't have DN yet */
826 if (( entrydn = strdup( buf )) == NULL ) {
828 return( EXIT_FAILURE );
845 int remove ) /* flag: remove old RDN */
848 char *matcheddn=NULL, *text=NULL, **refs=NULL;
852 printf( "Renaming \"%s\"\n", dn );
853 printf( "\tnew rdn=\"%s\" (%s old rdn)\n",
854 rdn, remove ? "delete" : "keep" );
855 if( newSuperior != NULL ) {
856 printf("\tnew parent=\"%s\"\n", newSuperior);
860 if( not ) return LDAP_SUCCESS;
862 rc = ldap_rename( ld, dn, rdn, newSuperior, remove,
865 if ( rc != LDAP_SUCCESS ) {
866 fprintf( stderr, "%s: ldap_rename: %s (%d)\n",
867 prog, ldap_err2string( rc ), rc );
871 rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, NULL, &res );
873 ldap_perror( ld, "ldapmodrdn: ldap_result" );
877 rc = ldap_parse_result( ld, res, &code, &matcheddn, &text, &refs, NULL, 1 );
879 if( rc != LDAP_SUCCESS ) {
880 fprintf( stderr, "%s: ldap_parse_result: %s (%d)\n",
881 prog, ldap_err2string( rc ), rc );
885 if( verbose || code != LDAP_SUCCESS ||
886 (matcheddn && *matcheddn) || (text && *text) || (refs && *refs) )
888 printf( "Rename Result: %s (%d)\n",
889 ldap_err2string( code ), code );
891 if( text && *text ) {
892 printf( "Additional info: %s\n", text );
895 if( matcheddn && *matcheddn ) {
896 printf( "Matched DN: %s\n", matcheddn );
901 for( i=0; refs[i]; i++ ) {
902 printf("Referral: %s\n", refs[i] );
908 ber_memfree( matcheddn );
909 ber_memvfree( (void **) refs );