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, i, remove, havedn, authmethod, version, want_bindpw, debug, manageDSAit, noop, crit;
117 char *newSuperior=NULL;
118 char *pw_file = NULL;
119 char *control, *cvalue;
120 char *authzid = NULL;
123 not = contoper = verbose = remove = want_bindpw =
124 debug = manageDSAit = noop = referrals = 0;
128 prog = lutil_progname( "ldapmodrdn", argc, argv );
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 )
138 case 'E': /* modrdn controls */
139 if( version == LDAP_VERSION2 ) {
140 fprintf( stderr, "%s: -E incompatible with LDAPv%d\n",
145 /* should be extended to support comma separated list of
146 * [!]key[=value] parameters, e.g. -E !foo,bar=567
151 if( optarg[0] == '!' ) {
156 control = strdup( optarg );
157 if ( (cvalue = strchr( control, '=' )) != NULL ) {
160 fprintf( stderr, "Invalid modrdn control name: %s\n", control );
163 case 'f': /* read from file */
164 if( infile != NULL ) {
165 fprintf( stderr, "%s: -f previously specified\n", prog );
168 infile = strdup( optarg );
170 case 'r': /* remove old RDN */
173 case 's': /* newSuperior */
174 if( version == LDAP_VERSION2 ) {
175 fprintf( stderr, "%s: -X incompatible with LDAPv%d\n",
179 newSuperior = strdup( optarg );
180 version = LDAP_VERSION3;
188 debug |= atoi( optarg );
190 case 'D': /* bind DN */
191 if( binddn != NULL ) {
192 fprintf( stderr, "%s: -D previously specified\n", prog );
195 binddn = strdup( optarg );
197 case 'e': /* general controls */
198 if( version == LDAP_VERSION2 ) {
199 fprintf( stderr, "%s: -e incompatible with LDAPv%d\n",
204 /* should be extended to support comma separated list of
205 * [!]key[=value] parameters, e.g. -e !foo,bar=567
210 if( optarg[0] == '!' ) {
215 control = strdup( optarg );
216 if ( (cvalue = strchr( control, '=' )) != NULL ) {
220 if ( strcasecmp( control, "authzid" ) == 0 ) {
221 if( authzid != NULL ) {
222 fprintf( stderr, "authzid control previously specified\n");
225 if( cvalue == NULL ) {
226 fprintf( stderr, "authzid: control value expected\n" );
231 fprintf( stderr, "authzid: must be marked critical\n" );
236 assert( authzid == NULL );
239 } else if ( strcasecmp( control, "manageDSAit" ) == 0 ) {
241 fprintf( stderr, "manageDSAit control previously specified\n");
244 if( cvalue != NULL ) {
245 fprintf( stderr, "manageDSAit: no control value expected\n" );
250 manageDSAit = 1 + crit;
252 } else if ( strcasecmp( control, "noop" ) == 0 ) {
254 fprintf( stderr, "noop control previously specified\n");
257 if( cvalue != NULL ) {
258 fprintf( stderr, "noop: no control value expected\n" );
266 fprintf( stderr, "Invalid general control name: %s\n", control );
271 case 'h': /* ldap host */
272 if( ldapuri != NULL ) {
273 fprintf( stderr, "%s: -h incompatible with -H\n", prog );
276 if( ldaphost != NULL ) {
277 fprintf( stderr, "%s: -h previously specified\n", prog );
280 ldaphost = strdup( optarg );
282 case 'H': /* ldap URI */
283 if( ldaphost != NULL ) {
284 fprintf( stderr, "%s: -H incompatible with -h\n", prog );
288 fprintf( stderr, "%s: -H incompatible with -p\n", prog );
291 if( ldapuri != NULL ) {
292 fprintf( stderr, "%s: -H previously specified\n", prog );
295 ldapuri = strdup( optarg );
298 #ifdef HAVE_CYRUS_SASL
299 if( version == LDAP_VERSION2 ) {
300 fprintf( stderr, "%s: -I incompatible with version %d\n",
304 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
305 fprintf( stderr, "%s: incompatible previous "
306 "authentication choice\n",
310 authmethod = LDAP_AUTH_SASL;
311 version = LDAP_VERSION3;
312 sasl_flags = LDAP_SASL_INTERACTIVE;
315 fprintf( stderr, "%s: was not compiled with SASL support\n",
317 return( EXIT_FAILURE );
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",
327 if( authmethod != -1 ) {
328 fprintf( stderr, "%s: -k incompatible with previous "
329 "authentication choice\n", prog );
333 authmethod = LDAP_AUTH_KRBV4;
335 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
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",
346 if( authmethod != -1 ) {
347 fprintf( stderr, "%s: incompatible with previous "
348 "authentication choice\n", prog );
352 authmethod = LDAP_AUTH_KRBV41;
354 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
355 return( EXIT_FAILURE );
359 /* enable Manage DSA IT */
360 if( version == LDAP_VERSION2 ) {
361 fprintf( stderr, "%s: -M incompatible with LDAPv%d\n",
366 version = LDAP_VERSION3;
368 case 'n': /* print deletes, don't actually do them */
372 #ifdef HAVE_CYRUS_SASL
373 if( sasl_secprops != NULL ) {
374 fprintf( stderr, "%s: -O previously specified\n", prog );
377 if( version == LDAP_VERSION2 ) {
378 fprintf( stderr, "%s: -O incompatible with LDAPv%d\n",
382 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
383 fprintf( stderr, "%s: incompatible previous "
384 "authentication choice\n", prog );
387 authmethod = LDAP_AUTH_SASL;
388 version = LDAP_VERSION3;
389 sasl_secprops = strdup( optarg );
391 fprintf( stderr, "%s: not compiled with SASL support\n",
393 return( EXIT_FAILURE );
398 fprintf( stderr, "%s: -p previously specified\n", prog );
401 ldapport = atoi( optarg );
404 switch( atoi(optarg) ) {
406 if( version == LDAP_VERSION3 ) {
407 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
411 version = LDAP_VERSION2;
414 if( version == LDAP_VERSION2 ) {
415 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
419 version = LDAP_VERSION3;
422 fprintf( stderr, "%s: protocol version should be 2 or 3\n",
425 return( EXIT_FAILURE );
429 #ifdef HAVE_CYRUS_SASL
430 if( version == LDAP_VERSION2 ) {
431 fprintf( stderr, "%s: -Q incompatible with version %d\n",
435 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
436 fprintf( stderr, "%s: incompatible previous "
437 "authentication choice\n",
441 authmethod = LDAP_AUTH_SASL;
442 version = LDAP_VERSION3;
443 sasl_flags = LDAP_SASL_QUIET;
446 fprintf( stderr, "%s: not compiled with SASL support\n",
448 return( EXIT_FAILURE );
451 #ifdef HAVE_CYRUS_SASL
452 if( sasl_realm != NULL ) {
453 fprintf( stderr, "%s: -R previously specified\n", prog );
456 if( version == LDAP_VERSION2 ) {
457 fprintf( stderr, "%s: -R incompatible with version %d\n",
461 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
462 fprintf( stderr, "%s: incompatible previous "
463 "authentication choice\n",
467 authmethod = LDAP_AUTH_SASL;
468 version = LDAP_VERSION3;
469 sasl_realm = strdup( optarg );
471 fprintf( stderr, "%s: not compiled with SASL support\n",
473 return( EXIT_FAILURE );
477 #ifdef HAVE_CYRUS_SASL
478 if( sasl_authc_id != NULL ) {
479 fprintf( stderr, "%s: -U previously specified\n", prog );
482 if( version == LDAP_VERSION2 ) {
483 fprintf( stderr, "%s: -U incompatible with version %d\n",
487 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
488 fprintf( stderr, "%s: incompatible previous "
489 "authentication choice\n",
493 authmethod = LDAP_AUTH_SASL;
494 version = LDAP_VERSION3;
495 sasl_authc_id = strdup( optarg );
497 fprintf( stderr, "%s: not compiled with SASL support\n",
499 return( EXIT_FAILURE );
502 case 'v': /* verbose mode */
505 case 'w': /* password */
506 passwd.bv_val = strdup( optarg );
510 for( p = optarg; *p != '\0'; p++ ) {
514 passwd.bv_len = strlen( passwd.bv_val );
523 #ifdef HAVE_CYRUS_SASL
524 if( sasl_mech != NULL ) {
525 fprintf( stderr, "%s: -Y previously specified\n", prog );
528 if( version == LDAP_VERSION2 ) {
529 fprintf( stderr, "%s: -Y incompatible with version %d\n",
533 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
534 fprintf( stderr, "%s: incompatible with authentication choice\n", prog );
537 authmethod = LDAP_AUTH_SASL;
538 version = LDAP_VERSION3;
539 sasl_mech = strdup( optarg );
541 fprintf( stderr, "%s: not compiled with SASL support\n",
543 return( EXIT_FAILURE );
547 if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
548 fprintf( stderr, "%s: incompatible with previous "
549 "authentication choice\n", prog );
552 authmethod = LDAP_AUTH_SIMPLE;
555 #ifdef HAVE_CYRUS_SASL
556 if( sasl_authz_id != NULL ) {
557 fprintf( stderr, "%s: -X previously specified\n", prog );
560 if( version == LDAP_VERSION2 ) {
561 fprintf( stderr, "%s: -X incompatible with LDAPv%d\n",
565 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
566 fprintf( stderr, "%s: -X incompatible with "
567 "authentication choice\n", prog );
570 authmethod = LDAP_AUTH_SASL;
571 version = LDAP_VERSION3;
572 sasl_authz_id = strdup( optarg );
574 fprintf( stderr, "%s: not compiled with SASL support\n",
576 return( EXIT_FAILURE );
581 if( version == LDAP_VERSION2 ) {
582 fprintf( stderr, "%s: -Z incompatible with version %d\n",
586 version = LDAP_VERSION3;
589 fprintf( stderr, "%s: not compiled with TLS support\n",
591 return( EXIT_FAILURE );
595 fprintf( stderr, "%s: unrecognized option -%c\n",
598 return( EXIT_FAILURE );
603 version = LDAP_VERSION3;
605 if (authmethod == -1 && version > LDAP_VERSION2) {
606 #ifdef HAVE_CYRUS_SASL
607 authmethod = LDAP_AUTH_SASL;
609 authmethod = LDAP_AUTH_SIMPLE;
614 if (argc - optind == 2) {
615 if (( rdn = strdup( argv[argc - 1] )) == NULL ) {
617 return( EXIT_FAILURE );
619 if (( entrydn = strdup( argv[argc - 2] )) == NULL ) {
621 return( EXIT_FAILURE );
624 } else if ( argc - optind != 0 ) {
625 fprintf( stderr, "%s: invalid number of arguments (%d), "
626 "only two allowed\n", prog, argc-optind );
628 return( EXIT_FAILURE );
631 if ( infile != NULL ) {
632 if (( fp = fopen( infile, "r" )) == NULL ) {
634 return( EXIT_FAILURE );
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 );
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 );
650 (void) SIGNAL( SIGPIPE, SIG_IGN );
653 if( ( ldaphost != NULL || ldapport ) && ( ldapuri == NULL ) ) {
655 fprintf( stderr, "ldap_init( %s, %d )\n",
656 ldaphost != NULL ? ldaphost : "<DEFAULT>",
660 ld = ldap_init( ldaphost, ldapport );
662 perror("ldapmodify: ldap_init");
668 fprintf( stderr, "ldap_initialize( %s )\n",
669 ldapuri != NULL ? ldapuri : "<DEFAULT>" );
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) );
681 if( ldap_set_option( ld, LDAP_OPT_REFERRALS,
682 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
684 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
685 referrals ? "on" : "off" );
689 if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version )
690 != LDAP_OPT_SUCCESS )
692 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
697 if ( use_tls && ( ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS )) {
698 ldap_perror( ld, "ldap_start_tls" );
700 return( EXIT_FAILURE );
704 if ( pw_file || want_bindpw ) {
706 rc = lutil_get_filed_password( pw_file, &passwd );
707 if( rc ) return EXIT_FAILURE;
709 passwd.bv_val = getpassphrase( "Enter LDAP Password: " );
710 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
714 if ( authmethod == LDAP_AUTH_SASL ) {
715 #ifdef HAVE_CYRUS_SASL
718 if( sasl_secprops != NULL ) {
719 rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
720 (void *) sasl_secprops );
722 if( rc != LDAP_OPT_SUCCESS ) {
724 "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
726 return( EXIT_FAILURE );
730 defaults = lutil_sasl_defaults( ld,
737 rc = ldap_sasl_interactive_bind_s( ld, binddn,
738 sasl_mech, NULL, NULL,
739 sasl_flags, lutil_sasl_interact, defaults );
741 if( rc != LDAP_SUCCESS ) {
742 ldap_perror( ld, "ldap_sasl_interactive_bind_s" );
743 return( EXIT_FAILURE );
746 fprintf( stderr, "%s: not compiled with SASL support\n",
748 return( EXIT_FAILURE );
752 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
754 ldap_perror( ld, "ldap_bind" );
755 return( EXIT_FAILURE );
759 if ( authzid || manageDSAit || noop ) {
760 int err, crit=0, i=0;
762 LDAPControl *ctrls[4];
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;
770 if( c[i].ldctl_iscritical ) crit++;
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;
781 if( c[i].ldctl_iscritical ) crit++;
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;
792 if( c[i].ldctl_iscritical ) crit++;
797 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, ctrls );
799 if( err != LDAP_OPT_SUCCESS ) {
800 fprintf( stderr, "Could not set %scontrols\n",
801 crit ? "critical " : "" );
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 */
815 if ( havedn ) { /* have DN, get RDN */
816 if (( rdn = strdup( buf )) == NULL ) {
818 return( EXIT_FAILURE );
820 rc = domodrdn(ld, entrydn, rdn, newSuperior, remove );
822 } else if ( !havedn ) { /* don't have DN yet */
823 if (( entrydn = strdup( buf )) == NULL ) {
825 return( EXIT_FAILURE );
843 int remove ) /* flag: remove old RDN */
846 char *matcheddn=NULL, *text=NULL, **refs=NULL;
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);
858 if( not ) return LDAP_SUCCESS;
860 rc = ldap_rename( ld, dn, rdn, newSuperior, remove,
863 if ( rc != LDAP_SUCCESS ) {
864 fprintf( stderr, "%s: ldap_rename: %s (%d)\n",
865 prog, ldap_err2string( rc ), rc );
869 rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, NULL, &res );
871 ldap_perror( ld, "ldapmodrdn: ldap_result" );
875 rc = ldap_parse_result( ld, res, &code, &matcheddn, &text, &refs, NULL, 1 );
877 if( rc != LDAP_SUCCESS ) {
878 fprintf( stderr, "%s: ldap_parse_result: %s (%d)\n",
879 prog, ldap_err2string( rc ), rc );
883 if( verbose || code != LDAP_SUCCESS ||
884 (matcheddn && *matcheddn) || (text && *text) || (refs && *refs) )
886 printf( "Rename Result: %s (%d)\n",
887 ldap_err2string( code ), code );
889 if( text && *text ) {
890 printf( "Additional info: %s\n", text );
893 if( matcheddn && *matcheddn ) {
894 printf( "Matched DN: %s\n", matcheddn );
899 for( i=0; refs[i]; i++ ) {
900 printf("Referral: %s\n", refs[i] );
906 ber_memfree( matcheddn );
907 ber_memvfree( (void **) refs );