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 " -f file read operations from `file'\n"
80 " -h host LDAP server\n"
81 " -H URI LDAP Uniform Resource Indentifier(s)\n"
82 " -I use SASL Interactive mode\n"
83 " -k use Kerberos authentication\n"
84 " -K like -k, but do only step 1 of the Kerberos bind\n"
85 " -M enable Manage DSA IT control (-MM to make critical)\n"
86 " -n show what would be done but don't actually update\n"
87 " -O props SASL security properties\n"
88 " -p port port on LDAP server\n"
89 " -P version procotol version (default: 3)\n"
90 " -Q use SASL Quiet mode\n"
91 " -R realm SASL realm\n"
92 " -U authzid SASL authentication identity\n"
93 " -v run in verbose mode (diagnostics to standard output)\n"
94 " -w passwd bind passwd (for simple authentication)\n"
95 " -W prompt for bind passwd\n"
96 " -x Simple authentication\n"
97 " -X authzid SASL authorization identity (\"dn:<dn>\" or \"u:<user>\")\n"
98 " -y file Read passwd from file\n"
99 " -Y mech SASL mechanism\n"
100 " -Z Start TLS request (-ZZ to require successful response)\n"
103 exit( EXIT_FAILURE );
107 main(int argc, char **argv)
109 char *infile, *entrydn = NULL, *rdn = NULL, buf[ 4096 ];
111 int rc, i, remove, havedn, authmethod, version, want_bindpw, debug, manageDSAit;
113 char *newSuperior=NULL;
114 char *pw_file = NULL;
117 not = contoper = verbose = remove = want_bindpw =
118 debug = manageDSAit = referrals = 0;
122 prog = lutil_progname( "ldapmodrdn", argc, argv );
124 while (( i = getopt( argc, argv, "cf:rs:"
125 "Cd:D:h:H:IkKMnO:p:P:QR:U:vw:WxX:y:Y:Z" )) != EOF )
132 case 'f': /* read from file */
133 if( infile != NULL ) {
134 fprintf( stderr, "%s: -f previously specified\n", prog );
137 infile = strdup( optarg );
139 case 'r': /* remove old RDN */
142 case 's': /* newSuperior */
143 if( version == LDAP_VERSION2 ) {
144 fprintf( stderr, "%s: -X incompatible with LDAPv%d\n",
148 newSuperior = strdup( optarg );
149 version = LDAP_VERSION3;
157 debug |= atoi( optarg );
159 case 'D': /* bind DN */
160 if( binddn != NULL ) {
161 fprintf( stderr, "%s: -D previously specified\n", prog );
164 binddn = strdup( optarg );
166 case 'h': /* ldap host */
167 if( ldapuri != NULL ) {
168 fprintf( stderr, "%s: -h incompatible with -H\n", prog );
171 if( ldaphost != NULL ) {
172 fprintf( stderr, "%s: -h previously specified\n", prog );
175 ldaphost = strdup( optarg );
177 case 'H': /* ldap URI */
178 if( ldaphost != NULL ) {
179 fprintf( stderr, "%s: -H incompatible with -h\n", prog );
183 fprintf( stderr, "%s: -H incompatible with -p\n", prog );
186 if( ldapuri != NULL ) {
187 fprintf( stderr, "%s: -H previously specified\n", prog );
190 ldapuri = strdup( optarg );
193 #ifdef HAVE_CYRUS_SASL
194 if( version == LDAP_VERSION2 ) {
195 fprintf( stderr, "%s: -I incompatible with version %d\n",
199 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
200 fprintf( stderr, "%s: incompatible previous "
201 "authentication choice\n",
205 authmethod = LDAP_AUTH_SASL;
206 version = LDAP_VERSION3;
207 sasl_flags = LDAP_SASL_INTERACTIVE;
210 fprintf( stderr, "%s: was not compiled with SASL support\n",
212 return( EXIT_FAILURE );
214 case 'k': /* kerberos bind */
215 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
216 if( version > LDAP_VERSION2 ) {
217 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
222 if( authmethod != -1 ) {
223 fprintf( stderr, "%s: -k incompatible with previous "
224 "authentication choice\n", prog );
228 authmethod = LDAP_AUTH_KRBV4;
230 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
234 case 'K': /* kerberos bind, part one only */
235 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
236 if( version > LDAP_VERSION2 ) {
237 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
241 if( authmethod != -1 ) {
242 fprintf( stderr, "%s: incompatible with previous "
243 "authentication choice\n", prog );
247 authmethod = LDAP_AUTH_KRBV41;
249 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
250 return( EXIT_FAILURE );
254 /* enable Manage DSA IT */
255 if( version == LDAP_VERSION2 ) {
256 fprintf( stderr, "%s: -M incompatible with LDAPv%d\n",
261 version = LDAP_VERSION3;
263 case 'n': /* print deletes, don't actually do them */
267 #ifdef HAVE_CYRUS_SASL
268 if( sasl_secprops != NULL ) {
269 fprintf( stderr, "%s: -O previously specified\n", prog );
272 if( version == LDAP_VERSION2 ) {
273 fprintf( stderr, "%s: -O incompatible with LDAPv%d\n",
277 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
278 fprintf( stderr, "%s: incompatible previous "
279 "authentication choice\n", prog );
282 authmethod = LDAP_AUTH_SASL;
283 version = LDAP_VERSION3;
284 sasl_secprops = strdup( optarg );
286 fprintf( stderr, "%s: not compiled with SASL support\n",
288 return( EXIT_FAILURE );
293 fprintf( stderr, "%s: -p previously specified\n", prog );
296 ldapport = atoi( optarg );
299 switch( atoi(optarg) ) {
301 if( version == LDAP_VERSION3 ) {
302 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
306 version = LDAP_VERSION2;
309 if( version == LDAP_VERSION2 ) {
310 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
314 version = LDAP_VERSION3;
317 fprintf( stderr, "%s: protocol version should be 2 or 3\n",
320 return( EXIT_FAILURE );
323 #ifdef HAVE_CYRUS_SASL
324 if( version == LDAP_VERSION2 ) {
325 fprintf( stderr, "%s: -Q incompatible with version %d\n",
329 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
330 fprintf( stderr, "%s: incompatible previous "
331 "authentication choice\n",
335 authmethod = LDAP_AUTH_SASL;
336 version = LDAP_VERSION3;
337 sasl_flags = LDAP_SASL_QUIET;
340 fprintf( stderr, "%s: not compiled with SASL support\n",
342 return( EXIT_FAILURE );
345 #ifdef HAVE_CYRUS_SASL
346 if( sasl_realm != NULL ) {
347 fprintf( stderr, "%s: -R previously specified\n", prog );
350 if( version == LDAP_VERSION2 ) {
351 fprintf( stderr, "%s: -R incompatible with version %d\n",
355 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
356 fprintf( stderr, "%s: incompatible previous "
357 "authentication choice\n",
361 authmethod = LDAP_AUTH_SASL;
362 version = LDAP_VERSION3;
363 sasl_realm = strdup( optarg );
365 fprintf( stderr, "%s: not compiled with SASL support\n",
367 return( EXIT_FAILURE );
371 #ifdef HAVE_CYRUS_SASL
372 if( sasl_authc_id != NULL ) {
373 fprintf( stderr, "%s: -U previously specified\n", prog );
376 if( version == LDAP_VERSION2 ) {
377 fprintf( stderr, "%s: -U incompatible with version %d\n",
381 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
382 fprintf( stderr, "%s: incompatible previous "
383 "authentication choice\n",
387 authmethod = LDAP_AUTH_SASL;
388 version = LDAP_VERSION3;
389 sasl_authc_id = strdup( optarg );
391 fprintf( stderr, "%s: not compiled with SASL support\n",
393 return( EXIT_FAILURE );
396 case 'v': /* verbose mode */
399 case 'w': /* password */
400 passwd.bv_val = strdup( optarg );
404 for( p = optarg; *p != '\0'; p++ ) {
408 passwd.bv_len = strlen( passwd.bv_val );
417 #ifdef HAVE_CYRUS_SASL
418 if( sasl_mech != NULL ) {
419 fprintf( stderr, "%s: -Y previously specified\n", prog );
422 if( version == LDAP_VERSION2 ) {
423 fprintf( stderr, "%s: -Y incompatible with version %d\n",
427 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
428 fprintf( stderr, "%s: incompatible with authentication choice\n", prog );
431 authmethod = LDAP_AUTH_SASL;
432 version = LDAP_VERSION3;
433 sasl_mech = strdup( optarg );
435 fprintf( stderr, "%s: not compiled with SASL support\n",
437 return( EXIT_FAILURE );
441 if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
442 fprintf( stderr, "%s: incompatible with previous "
443 "authentication choice\n", prog );
446 authmethod = LDAP_AUTH_SIMPLE;
449 #ifdef HAVE_CYRUS_SASL
450 if( sasl_authz_id != NULL ) {
451 fprintf( stderr, "%s: -X previously specified\n", prog );
454 if( version == LDAP_VERSION2 ) {
455 fprintf( stderr, "%s: -X incompatible with LDAPv%d\n",
459 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
460 fprintf( stderr, "%s: -X incompatible with "
461 "authentication choice\n", prog );
464 authmethod = LDAP_AUTH_SASL;
465 version = LDAP_VERSION3;
466 sasl_authz_id = strdup( optarg );
468 fprintf( stderr, "%s: not compiled with SASL support\n",
470 return( EXIT_FAILURE );
475 if( version == LDAP_VERSION2 ) {
476 fprintf( stderr, "%s: -Z incompatible with version %d\n",
480 version = LDAP_VERSION3;
483 fprintf( stderr, "%s: not compiled with TLS support\n",
485 return( EXIT_FAILURE );
489 fprintf( stderr, "%s: unrecognized option -%c\n",
492 return( EXIT_FAILURE );
497 version = LDAP_VERSION3;
499 if (authmethod == -1 && version > LDAP_VERSION2) {
500 #ifdef HAVE_CYRUS_SASL
501 authmethod = LDAP_AUTH_SASL;
503 authmethod = LDAP_AUTH_SIMPLE;
508 if (argc - optind == 2) {
509 if (( rdn = strdup( argv[argc - 1] )) == NULL ) {
511 return( EXIT_FAILURE );
513 if (( entrydn = strdup( argv[argc - 2] )) == NULL ) {
515 return( EXIT_FAILURE );
518 } else if ( argc - optind != 0 ) {
519 fprintf( stderr, "%s: invalid number of arguments (%d), "
520 "only two allowed\n", prog, argc-optind );
522 return( EXIT_FAILURE );
525 if ( infile != NULL ) {
526 if (( fp = fopen( infile, "r" )) == NULL ) {
528 return( EXIT_FAILURE );
535 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
536 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
538 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
539 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
544 (void) SIGNAL( SIGPIPE, SIG_IGN );
547 if( ( ldaphost != NULL || ldapport ) && ( ldapuri == NULL ) ) {
549 fprintf( stderr, "ldap_init( %s, %d )\n",
550 ldaphost != NULL ? ldaphost : "<DEFAULT>",
554 ld = ldap_init( ldaphost, ldapport );
556 perror("ldapmodify: ldap_init");
562 fprintf( stderr, "ldap_initialize( %s )\n",
563 ldapuri != NULL ? ldapuri : "<DEFAULT>" );
566 rc = ldap_initialize( &ld, ldapuri );
567 if( rc != LDAP_SUCCESS ) {
568 fprintf( stderr, "Could not create LDAP session handle (%d): %s\n",
569 rc, ldap_err2string(rc) );
575 if( ldap_set_option( ld, LDAP_OPT_REFERRALS,
576 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
578 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
579 referrals ? "on" : "off" );
583 if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version )
584 != LDAP_OPT_SUCCESS )
586 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
591 if ( use_tls && ( ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS )) {
592 ldap_perror( ld, "ldap_start_tls" );
594 return( EXIT_FAILURE );
598 if ( pw_file || want_bindpw ) {
600 rc = lutil_get_filed_password( pw_file, &passwd );
601 if( rc ) return EXIT_FAILURE;
603 passwd.bv_val = getpassphrase( "Enter LDAP Password: " );
604 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
608 if ( authmethod == LDAP_AUTH_SASL ) {
609 #ifdef HAVE_CYRUS_SASL
612 if( sasl_secprops != NULL ) {
613 rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
614 (void *) sasl_secprops );
616 if( rc != LDAP_OPT_SUCCESS ) {
618 "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
620 return( EXIT_FAILURE );
624 defaults = lutil_sasl_defaults( ld,
631 rc = ldap_sasl_interactive_bind_s( ld, binddn,
632 sasl_mech, NULL, NULL,
633 sasl_flags, lutil_sasl_interact, defaults );
635 if( rc != LDAP_SUCCESS ) {
636 ldap_perror( ld, "ldap_sasl_interactive_bind_s" );
637 return( EXIT_FAILURE );
640 fprintf( stderr, "%s: not compiled with SASL support\n",
642 return( EXIT_FAILURE );
646 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
648 ldap_perror( ld, "ldap_bind" );
649 return( EXIT_FAILURE );
656 LDAPControl *ctrls[2];
660 c.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
661 c.ldctl_value.bv_val = NULL;
662 c.ldctl_value.bv_len = 0;
663 c.ldctl_iscritical = manageDSAit > 1;
665 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, ctrls );
667 if( err != LDAP_OPT_SUCCESS ) {
668 fprintf( stderr, "Could not set ManageDSAit %scontrol\n",
669 c.ldctl_iscritical ? "critical " : "" );
670 if( c.ldctl_iscritical ) {
671 exit( EXIT_FAILURE );
678 rc = domodrdn( ld, entrydn, rdn, newSuperior, remove );
679 else while ((rc == 0 || contoper) && fgets(buf, sizeof(buf), fp) != NULL) {
680 if ( *buf != '\0' ) { /* blank lines optional, skip */
681 buf[ strlen( buf ) - 1 ] = '\0'; /* remove nl */
683 if ( havedn ) { /* have DN, get RDN */
684 if (( rdn = strdup( buf )) == NULL ) {
686 return( EXIT_FAILURE );
688 rc = domodrdn(ld, entrydn, rdn, newSuperior, remove );
690 } else if ( !havedn ) { /* don't have DN yet */
691 if (( entrydn = strdup( buf )) == NULL ) {
693 return( EXIT_FAILURE );
711 int remove ) /* flag: remove old RDN */
714 char *matcheddn=NULL, *text=NULL, **refs=NULL;
718 printf( "Renaming \"%s\"\n", dn );
719 printf( "\tnew rdn=\"%s\" (%s old rdn)\n",
720 rdn, remove ? "delete" : "keep" );
721 if( newSuperior != NULL ) {
722 printf("\tnew parent=\"%s\"\n", newSuperior);
726 if( not ) return LDAP_SUCCESS;
728 rc = ldap_rename( ld, dn, rdn, newSuperior, remove,
731 if ( rc != LDAP_SUCCESS ) {
732 fprintf( stderr, "%s: ldap_rename: %s (%d)\n",
733 prog, ldap_err2string( rc ), rc );
737 rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, NULL, &res );
739 ldap_perror( ld, "ldapmodrdn: ldap_result" );
743 rc = ldap_parse_result( ld, res, &code, &matcheddn, &text, &refs, NULL, 1 );
745 if( rc != LDAP_SUCCESS ) {
746 fprintf( stderr, "%s: ldap_parse_result: %s (%d)\n",
747 prog, ldap_err2string( rc ), rc );
751 if( verbose || code != LDAP_SUCCESS ||
752 (matcheddn && *matcheddn) || (text && *text) || (refs && *refs) )
754 printf( "Rename Result: %s (%d)\n",
755 ldap_err2string( code ), code );
757 if( text && *text ) {
758 printf( "Additional info: %s\n", text );
761 if( matcheddn && *matcheddn ) {
762 printf( "Matched DN: %s\n", matcheddn );
767 for( i=0; refs[i]; i++ ) {
768 printf("Referral: %s\n", refs[i] );
774 ber_memfree( matcheddn );
775 ber_memvfree( (void **) refs );