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"
81 #ifdef LDAP_CONTROL_NOOP
84 " -f file read operations from `file'\n"
85 " -h host LDAP server\n"
86 " -H URI LDAP Uniform Resource Indentifier(s)\n"
87 " -I use SASL Interactive mode\n"
88 " -k use Kerberos authentication\n"
89 " -K like -k, but do only step 1 of the Kerberos bind\n"
90 " -M enable Manage DSA IT control (-MM to make critical)\n"
91 " -n show what would be done but don't actually update\n"
92 " -O props SASL security properties\n"
93 " -p port port on LDAP server\n"
94 " -P version procotol version (default: 3)\n"
95 " -Q use SASL Quiet mode\n"
96 " -R realm SASL realm\n"
97 " -U authzid SASL authentication identity\n"
98 " -v run in verbose mode (diagnostics to standard output)\n"
99 " -w passwd bind passwd (for simple authentication)\n"
100 " -W prompt for bind passwd\n"
101 " -x Simple authentication\n"
102 " -X authzid SASL authorization identity (\"dn:<dn>\" or \"u:<user>\")\n"
103 " -y file Read passwd from file\n"
104 " -Y mech SASL mechanism\n"
105 " -Z Start TLS request (-ZZ to require successful response)\n"
108 exit( EXIT_FAILURE );
112 main(int argc, char **argv)
114 char *infile, *entrydn = NULL, *rdn = NULL, buf[ 4096 ];
116 int rc, i, remove, havedn, authmethod, version, want_bindpw, debug, manageDSAit, crit;
118 char *newSuperior=NULL;
119 char *pw_file = NULL;
120 char *control, *cvalue;
121 #ifdef LDAP_CONTROL_NOOP
126 not = contoper = verbose = remove = want_bindpw =
127 debug = manageDSAit = referrals = 0;
131 prog = lutil_progname( "ldapmodrdn", argc, argv );
133 while (( i = getopt( argc, argv, "cf:rs:"
134 "Cd:D:e:h:H:IkKMnO:p:P:QR:U:vw:WxX:y:Y:Z" )) != EOF )
141 case 'E': /* modrdn controls */
142 if( version == LDAP_VERSION2 ) {
143 fprintf( stderr, "%s: -E incompatible with LDAPv%d\n",
148 /* should be extended to support comma separated list of
149 * [!]key[=value] parameters, e.g. -E !foo,bar=567
154 if( optarg[0] == '!' ) {
159 control = strdup( optarg );
160 if ( (cvalue = strchr( control, '=' )) != NULL ) {
163 fprintf( stderr, "Invalid modrdn control name: %s\n", control );
166 case 'f': /* read from file */
167 if( infile != NULL ) {
168 fprintf( stderr, "%s: -f previously specified\n", prog );
171 infile = strdup( optarg );
173 case 'r': /* remove old RDN */
176 case 's': /* newSuperior */
177 if( version == LDAP_VERSION2 ) {
178 fprintf( stderr, "%s: -X incompatible with LDAPv%d\n",
182 newSuperior = strdup( optarg );
183 version = LDAP_VERSION3;
191 debug |= atoi( optarg );
193 case 'D': /* bind DN */
194 if( binddn != NULL ) {
195 fprintf( stderr, "%s: -D previously specified\n", prog );
198 binddn = strdup( optarg );
200 case 'e': /* general controls */
201 if( version == LDAP_VERSION2 ) {
202 fprintf( stderr, "%s: -e incompatible with LDAPv%d\n",
207 /* should be extended to support comma separated list of
208 * [!]key[=value] parameters, e.g. -e !foo,bar=567
213 if( optarg[0] == '!' ) {
218 control = strdup( optarg );
219 if ( (cvalue = strchr( control, '=' )) != NULL ) {
223 if ( strcasecmp( control, "manageDSAit" ) == 0 ) {
224 if( cvalue != NULL ) {
225 fprintf( stderr, "manageDSAit: no control value expected" );
230 manageDSAit = 1 + crit;
234 #ifdef LDAP_CONTROL_NOOP
235 } else if ( strcasecmp( control, "noop" ) == 0 ) {
236 if( cvalue != NULL ) {
237 fprintf( stderr, "noop: no control value expected" );
247 fprintf( stderr, "Invalid general control name: %s\n", control );
251 case 'h': /* ldap host */
252 if( ldapuri != NULL ) {
253 fprintf( stderr, "%s: -h incompatible with -H\n", prog );
256 if( ldaphost != NULL ) {
257 fprintf( stderr, "%s: -h previously specified\n", prog );
260 ldaphost = strdup( optarg );
262 case 'H': /* ldap URI */
263 if( ldaphost != NULL ) {
264 fprintf( stderr, "%s: -H incompatible with -h\n", prog );
268 fprintf( stderr, "%s: -H incompatible with -p\n", prog );
271 if( ldapuri != NULL ) {
272 fprintf( stderr, "%s: -H previously specified\n", prog );
275 ldapuri = strdup( optarg );
278 #ifdef HAVE_CYRUS_SASL
279 if( version == LDAP_VERSION2 ) {
280 fprintf( stderr, "%s: -I incompatible with version %d\n",
284 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
285 fprintf( stderr, "%s: incompatible previous "
286 "authentication choice\n",
290 authmethod = LDAP_AUTH_SASL;
291 version = LDAP_VERSION3;
292 sasl_flags = LDAP_SASL_INTERACTIVE;
295 fprintf( stderr, "%s: was not compiled with SASL support\n",
297 return( EXIT_FAILURE );
299 case 'k': /* kerberos bind */
300 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
301 if( version > LDAP_VERSION2 ) {
302 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
307 if( authmethod != -1 ) {
308 fprintf( stderr, "%s: -k incompatible with previous "
309 "authentication choice\n", prog );
313 authmethod = LDAP_AUTH_KRBV4;
315 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
319 case 'K': /* kerberos bind, part one only */
320 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
321 if( version > LDAP_VERSION2 ) {
322 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
326 if( authmethod != -1 ) {
327 fprintf( stderr, "%s: incompatible with previous "
328 "authentication choice\n", prog );
332 authmethod = LDAP_AUTH_KRBV41;
334 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
335 return( EXIT_FAILURE );
339 /* enable Manage DSA IT */
340 if( version == LDAP_VERSION2 ) {
341 fprintf( stderr, "%s: -M incompatible with LDAPv%d\n",
346 version = LDAP_VERSION3;
348 case 'n': /* print deletes, don't actually do them */
352 #ifdef HAVE_CYRUS_SASL
353 if( sasl_secprops != NULL ) {
354 fprintf( stderr, "%s: -O previously specified\n", prog );
357 if( version == LDAP_VERSION2 ) {
358 fprintf( stderr, "%s: -O incompatible with LDAPv%d\n",
362 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
363 fprintf( stderr, "%s: incompatible previous "
364 "authentication choice\n", prog );
367 authmethod = LDAP_AUTH_SASL;
368 version = LDAP_VERSION3;
369 sasl_secprops = strdup( optarg );
371 fprintf( stderr, "%s: not compiled with SASL support\n",
373 return( EXIT_FAILURE );
378 fprintf( stderr, "%s: -p previously specified\n", prog );
381 ldapport = atoi( optarg );
384 switch( atoi(optarg) ) {
386 if( version == LDAP_VERSION3 ) {
387 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
391 version = LDAP_VERSION2;
394 if( version == LDAP_VERSION2 ) {
395 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
399 version = LDAP_VERSION3;
402 fprintf( stderr, "%s: protocol version should be 2 or 3\n",
405 return( EXIT_FAILURE );
408 #ifdef HAVE_CYRUS_SASL
409 if( version == LDAP_VERSION2 ) {
410 fprintf( stderr, "%s: -Q incompatible with version %d\n",
414 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
415 fprintf( stderr, "%s: incompatible previous "
416 "authentication choice\n",
420 authmethod = LDAP_AUTH_SASL;
421 version = LDAP_VERSION3;
422 sasl_flags = LDAP_SASL_QUIET;
425 fprintf( stderr, "%s: not compiled with SASL support\n",
427 return( EXIT_FAILURE );
430 #ifdef HAVE_CYRUS_SASL
431 if( sasl_realm != NULL ) {
432 fprintf( stderr, "%s: -R previously specified\n", prog );
435 if( version == LDAP_VERSION2 ) {
436 fprintf( stderr, "%s: -R incompatible with version %d\n",
440 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
441 fprintf( stderr, "%s: incompatible previous "
442 "authentication choice\n",
446 authmethod = LDAP_AUTH_SASL;
447 version = LDAP_VERSION3;
448 sasl_realm = strdup( optarg );
450 fprintf( stderr, "%s: not compiled with SASL support\n",
452 return( EXIT_FAILURE );
456 #ifdef HAVE_CYRUS_SASL
457 if( sasl_authc_id != NULL ) {
458 fprintf( stderr, "%s: -U previously specified\n", prog );
461 if( version == LDAP_VERSION2 ) {
462 fprintf( stderr, "%s: -U incompatible with version %d\n",
466 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
467 fprintf( stderr, "%s: incompatible previous "
468 "authentication choice\n",
472 authmethod = LDAP_AUTH_SASL;
473 version = LDAP_VERSION3;
474 sasl_authc_id = strdup( optarg );
476 fprintf( stderr, "%s: not compiled with SASL support\n",
478 return( EXIT_FAILURE );
481 case 'v': /* verbose mode */
484 case 'w': /* password */
485 passwd.bv_val = strdup( optarg );
489 for( p = optarg; *p != '\0'; p++ ) {
493 passwd.bv_len = strlen( passwd.bv_val );
502 #ifdef HAVE_CYRUS_SASL
503 if( sasl_mech != NULL ) {
504 fprintf( stderr, "%s: -Y previously specified\n", prog );
507 if( version == LDAP_VERSION2 ) {
508 fprintf( stderr, "%s: -Y incompatible with version %d\n",
512 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
513 fprintf( stderr, "%s: incompatible with authentication choice\n", prog );
516 authmethod = LDAP_AUTH_SASL;
517 version = LDAP_VERSION3;
518 sasl_mech = strdup( optarg );
520 fprintf( stderr, "%s: not compiled with SASL support\n",
522 return( EXIT_FAILURE );
526 if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
527 fprintf( stderr, "%s: incompatible with previous "
528 "authentication choice\n", prog );
531 authmethod = LDAP_AUTH_SIMPLE;
534 #ifdef HAVE_CYRUS_SASL
535 if( sasl_authz_id != NULL ) {
536 fprintf( stderr, "%s: -X previously specified\n", prog );
539 if( version == LDAP_VERSION2 ) {
540 fprintf( stderr, "%s: -X incompatible with LDAPv%d\n",
544 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
545 fprintf( stderr, "%s: -X incompatible with "
546 "authentication choice\n", prog );
549 authmethod = LDAP_AUTH_SASL;
550 version = LDAP_VERSION3;
551 sasl_authz_id = strdup( optarg );
553 fprintf( stderr, "%s: not compiled with SASL support\n",
555 return( EXIT_FAILURE );
560 if( version == LDAP_VERSION2 ) {
561 fprintf( stderr, "%s: -Z incompatible with version %d\n",
565 version = LDAP_VERSION3;
568 fprintf( stderr, "%s: not compiled with TLS support\n",
570 return( EXIT_FAILURE );
574 fprintf( stderr, "%s: unrecognized option -%c\n",
577 return( EXIT_FAILURE );
582 version = LDAP_VERSION3;
584 if (authmethod == -1 && version > LDAP_VERSION2) {
585 #ifdef HAVE_CYRUS_SASL
586 authmethod = LDAP_AUTH_SASL;
588 authmethod = LDAP_AUTH_SIMPLE;
593 if (argc - optind == 2) {
594 if (( rdn = strdup( argv[argc - 1] )) == NULL ) {
596 return( EXIT_FAILURE );
598 if (( entrydn = strdup( argv[argc - 2] )) == NULL ) {
600 return( EXIT_FAILURE );
603 } else if ( argc - optind != 0 ) {
604 fprintf( stderr, "%s: invalid number of arguments (%d), "
605 "only two allowed\n", prog, argc-optind );
607 return( EXIT_FAILURE );
610 if ( infile != NULL ) {
611 if (( fp = fopen( infile, "r" )) == NULL ) {
613 return( EXIT_FAILURE );
620 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
621 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
623 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
624 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
629 (void) SIGNAL( SIGPIPE, SIG_IGN );
632 if( ( ldaphost != NULL || ldapport ) && ( ldapuri == NULL ) ) {
634 fprintf( stderr, "ldap_init( %s, %d )\n",
635 ldaphost != NULL ? ldaphost : "<DEFAULT>",
639 ld = ldap_init( ldaphost, ldapport );
641 perror("ldapmodify: ldap_init");
647 fprintf( stderr, "ldap_initialize( %s )\n",
648 ldapuri != NULL ? ldapuri : "<DEFAULT>" );
651 rc = ldap_initialize( &ld, ldapuri );
652 if( rc != LDAP_SUCCESS ) {
653 fprintf( stderr, "Could not create LDAP session handle (%d): %s\n",
654 rc, ldap_err2string(rc) );
660 if( ldap_set_option( ld, LDAP_OPT_REFERRALS,
661 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
663 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
664 referrals ? "on" : "off" );
668 if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version )
669 != LDAP_OPT_SUCCESS )
671 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
676 if ( use_tls && ( ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS )) {
677 ldap_perror( ld, "ldap_start_tls" );
679 return( EXIT_FAILURE );
683 if ( pw_file || want_bindpw ) {
685 rc = lutil_get_filed_password( pw_file, &passwd );
686 if( rc ) return EXIT_FAILURE;
688 passwd.bv_val = getpassphrase( "Enter LDAP Password: " );
689 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
693 if ( authmethod == LDAP_AUTH_SASL ) {
694 #ifdef HAVE_CYRUS_SASL
697 if( sasl_secprops != NULL ) {
698 rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
699 (void *) sasl_secprops );
701 if( rc != LDAP_OPT_SUCCESS ) {
703 "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
705 return( EXIT_FAILURE );
709 defaults = lutil_sasl_defaults( ld,
716 rc = ldap_sasl_interactive_bind_s( ld, binddn,
717 sasl_mech, NULL, NULL,
718 sasl_flags, lutil_sasl_interact, defaults );
720 if( rc != LDAP_SUCCESS ) {
721 ldap_perror( ld, "ldap_sasl_interactive_bind_s" );
722 return( EXIT_FAILURE );
725 fprintf( stderr, "%s: not compiled with SASL support\n",
727 return( EXIT_FAILURE );
731 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
733 ldap_perror( ld, "ldap_bind" );
734 return( EXIT_FAILURE );
739 #ifdef LDAP_CONTROL_NOOP
745 LDAPControl *ctrls[3];
750 c1.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
751 c1.ldctl_value.bv_val = NULL;
752 c1.ldctl_value.bv_len = 0;
753 c1.ldctl_iscritical = manageDSAit > 1;
756 #ifdef LDAP_CONTROL_NOOP
761 c2.ldctl_oid = LDAP_CONTROL_NOOP;
762 c2.ldctl_value.bv_val = NULL;
763 c2.ldctl_value.bv_len = 0;
764 c2.ldctl_iscritical = noop > 1;
768 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, ctrls );
770 if( err != LDAP_OPT_SUCCESS ) {
771 fprintf( stderr, "Could not set %scontrols\n",
772 (c1.ldctl_iscritical || c2.ldctl_iscritical)
773 ? "critical " : "" );
774 if ( c1.ldctl_iscritical && c2.ldctl_iscritical ) {
782 rc = domodrdn( ld, entrydn, rdn, newSuperior, remove );
783 else while ((rc == 0 || contoper) && fgets(buf, sizeof(buf), fp) != NULL) {
784 if ( *buf != '\0' ) { /* blank lines optional, skip */
785 buf[ strlen( buf ) - 1 ] = '\0'; /* remove nl */
787 if ( havedn ) { /* have DN, get RDN */
788 if (( rdn = strdup( buf )) == NULL ) {
790 return( EXIT_FAILURE );
792 rc = domodrdn(ld, entrydn, rdn, newSuperior, remove );
794 } else if ( !havedn ) { /* don't have DN yet */
795 if (( entrydn = strdup( buf )) == NULL ) {
797 return( EXIT_FAILURE );
815 int remove ) /* flag: remove old RDN */
818 char *matcheddn=NULL, *text=NULL, **refs=NULL;
822 printf( "Renaming \"%s\"\n", dn );
823 printf( "\tnew rdn=\"%s\" (%s old rdn)\n",
824 rdn, remove ? "delete" : "keep" );
825 if( newSuperior != NULL ) {
826 printf("\tnew parent=\"%s\"\n", newSuperior);
830 if( not ) return LDAP_SUCCESS;
832 rc = ldap_rename( ld, dn, rdn, newSuperior, remove,
835 if ( rc != LDAP_SUCCESS ) {
836 fprintf( stderr, "%s: ldap_rename: %s (%d)\n",
837 prog, ldap_err2string( rc ), rc );
841 rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, NULL, &res );
843 ldap_perror( ld, "ldapmodrdn: ldap_result" );
847 rc = ldap_parse_result( ld, res, &code, &matcheddn, &text, &refs, NULL, 1 );
849 if( rc != LDAP_SUCCESS ) {
850 fprintf( stderr, "%s: ldap_parse_result: %s (%d)\n",
851 prog, ldap_err2string( rc ), rc );
855 if( verbose || code != LDAP_SUCCESS ||
856 (matcheddn && *matcheddn) || (text && *text) || (refs && *refs) )
858 printf( "Rename Result: %s (%d)\n",
859 ldap_err2string( code ), code );
861 if( text && *text ) {
862 printf( "Additional info: %s\n", text );
865 if( matcheddn && *matcheddn ) {
866 printf( "Matched DN: %s\n", matcheddn );
871 for( i=0; refs[i]; i++ ) {
872 printf("Referral: %s\n", refs[i] );
878 ber_memfree( matcheddn );
879 ber_memvfree( (void **) refs );