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 mech SASL mechanism\n"
99 " -Z Start TLS request (-ZZ to require successful response)\n"
102 exit( EXIT_FAILURE );
106 main(int argc, char **argv)
108 char *infile, *entrydn = NULL, *rdn = NULL, buf[ 4096 ];
110 int rc, i, remove, havedn, authmethod, version, want_bindpw, debug, manageDSAit;
112 char *newSuperior=NULL;
115 not = contoper = verbose = remove = want_bindpw =
116 debug = manageDSAit = referrals = 0;
120 prog = lutil_progname( "ldapmodrdn", argc, argv );
122 while (( i = getopt( argc, argv, "cf:rs:"
123 "Cd:D:h:H:IkKMnO:p:P:QR:U:vw:WxX:Y:Z" )) != EOF )
130 case 'f': /* read from file */
131 if( infile != NULL ) {
132 fprintf( stderr, "%s: -f previously specified\n", prog );
135 infile = strdup( optarg );
137 case 'r': /* remove old RDN */
140 case 's': /* newSuperior */
141 if( version == LDAP_VERSION2 ) {
142 fprintf( stderr, "%s: -X incompatible with LDAPv%d\n",
146 newSuperior = strdup( optarg );
147 version = LDAP_VERSION3;
155 debug |= atoi( optarg );
157 case 'D': /* bind DN */
158 if( binddn != NULL ) {
159 fprintf( stderr, "%s: -D previously specified\n", prog );
162 binddn = strdup( optarg );
164 case 'h': /* ldap host */
165 if( ldapuri != NULL ) {
166 fprintf( stderr, "%s: -h incompatible with -H\n", prog );
169 if( ldaphost != NULL ) {
170 fprintf( stderr, "%s: -h previously specified\n", prog );
173 ldaphost = strdup( optarg );
175 case 'H': /* ldap URI */
176 if( ldaphost != NULL ) {
177 fprintf( stderr, "%s: -H incompatible with -h\n", prog );
181 fprintf( stderr, "%s: -H incompatible with -p\n", prog );
184 if( ldapuri != NULL ) {
185 fprintf( stderr, "%s: -H previously specified\n", prog );
188 ldapuri = strdup( optarg );
191 #ifdef HAVE_CYRUS_SASL
192 if( version == LDAP_VERSION2 ) {
193 fprintf( stderr, "%s: -I incompatible with version %d\n",
197 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
198 fprintf( stderr, "%s: incompatible previous "
199 "authentication choice\n",
203 authmethod = LDAP_AUTH_SASL;
204 version = LDAP_VERSION3;
205 sasl_flags = LDAP_SASL_INTERACTIVE;
208 fprintf( stderr, "%s: was not compiled with SASL support\n",
210 return( EXIT_FAILURE );
212 case 'k': /* kerberos bind */
213 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
214 if( version > LDAP_VERSION2 ) {
215 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
220 if( authmethod != -1 ) {
221 fprintf( stderr, "%s: -k incompatible with previous "
222 "authentication choice\n", prog );
226 authmethod = LDAP_AUTH_KRBV4;
228 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
232 case 'K': /* kerberos bind, part one only */
233 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
234 if( version > LDAP_VERSION2 ) {
235 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
239 if( authmethod != -1 ) {
240 fprintf( stderr, "%s: incompatible with previous "
241 "authentication choice\n", prog );
245 authmethod = LDAP_AUTH_KRBV41;
247 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
248 return( EXIT_FAILURE );
252 /* enable Manage DSA IT */
253 if( version == LDAP_VERSION2 ) {
254 fprintf( stderr, "%s: -M incompatible with LDAPv%d\n",
259 version = LDAP_VERSION3;
261 case 'n': /* print deletes, don't actually do them */
265 #ifdef HAVE_CYRUS_SASL
266 if( sasl_secprops != NULL ) {
267 fprintf( stderr, "%s: -O previously specified\n", prog );
270 if( version == LDAP_VERSION2 ) {
271 fprintf( stderr, "%s: -O incompatible with LDAPv%d\n",
275 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
276 fprintf( stderr, "%s: incompatible previous "
277 "authentication choice\n", prog );
280 authmethod = LDAP_AUTH_SASL;
281 version = LDAP_VERSION3;
282 sasl_secprops = strdup( optarg );
284 fprintf( stderr, "%s: not compiled with SASL support\n",
286 return( EXIT_FAILURE );
291 fprintf( stderr, "%s: -p previously specified\n", prog );
294 ldapport = atoi( optarg );
297 switch( atoi(optarg) ) {
299 if( version == LDAP_VERSION3 ) {
300 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
304 version = LDAP_VERSION2;
307 if( version == LDAP_VERSION2 ) {
308 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
312 version = LDAP_VERSION3;
315 fprintf( stderr, "%s: protocol version should be 2 or 3\n",
318 return( EXIT_FAILURE );
321 #ifdef HAVE_CYRUS_SASL
322 if( version == LDAP_VERSION2 ) {
323 fprintf( stderr, "%s: -Q incompatible with version %d\n",
327 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
328 fprintf( stderr, "%s: incompatible previous "
329 "authentication choice\n",
333 authmethod = LDAP_AUTH_SASL;
334 version = LDAP_VERSION3;
335 sasl_flags = LDAP_SASL_QUIET;
338 fprintf( stderr, "%s: not compiled with SASL support\n",
340 return( EXIT_FAILURE );
343 #ifdef HAVE_CYRUS_SASL
344 if( sasl_realm != NULL ) {
345 fprintf( stderr, "%s: -R previously specified\n", prog );
348 if( version == LDAP_VERSION2 ) {
349 fprintf( stderr, "%s: -R incompatible with version %d\n",
353 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
354 fprintf( stderr, "%s: incompatible previous "
355 "authentication choice\n",
359 authmethod = LDAP_AUTH_SASL;
360 version = LDAP_VERSION3;
361 sasl_realm = strdup( optarg );
363 fprintf( stderr, "%s: not compiled with SASL support\n",
365 return( EXIT_FAILURE );
369 #ifdef HAVE_CYRUS_SASL
370 if( sasl_authc_id != NULL ) {
371 fprintf( stderr, "%s: -U previously specified\n", prog );
374 if( version == LDAP_VERSION2 ) {
375 fprintf( stderr, "%s: -U incompatible with version %d\n",
379 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
380 fprintf( stderr, "%s: incompatible previous "
381 "authentication choice\n",
385 authmethod = LDAP_AUTH_SASL;
386 version = LDAP_VERSION3;
387 sasl_authc_id = strdup( optarg );
389 fprintf( stderr, "%s: not compiled with SASL support\n",
391 return( EXIT_FAILURE );
394 case 'v': /* verbose mode */
397 case 'w': /* password */
398 passwd.bv_val = strdup( optarg );
402 for( p = optarg; *p != '\0'; p++ ) {
406 passwd.bv_len = strlen( passwd.bv_val );
412 #ifdef HAVE_CYRUS_SASL
413 if( sasl_mech != NULL ) {
414 fprintf( stderr, "%s: -Y previously specified\n", prog );
417 if( version == LDAP_VERSION2 ) {
418 fprintf( stderr, "%s: -Y incompatible with version %d\n",
422 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
423 fprintf( stderr, "%s: incompatible with authentication choice\n", prog );
426 authmethod = LDAP_AUTH_SASL;
427 version = LDAP_VERSION3;
428 sasl_mech = strdup( optarg );
430 fprintf( stderr, "%s: not compiled with SASL support\n",
432 return( EXIT_FAILURE );
436 if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
437 fprintf( stderr, "%s: incompatible with previous "
438 "authentication choice\n", prog );
441 authmethod = LDAP_AUTH_SIMPLE;
444 #ifdef HAVE_CYRUS_SASL
445 if( sasl_authz_id != NULL ) {
446 fprintf( stderr, "%s: -X previously specified\n", prog );
449 if( version == LDAP_VERSION2 ) {
450 fprintf( stderr, "%s: -X incompatible with LDAPv%d\n",
454 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
455 fprintf( stderr, "%s: -X incompatible with "
456 "authentication choice\n", prog );
459 authmethod = LDAP_AUTH_SASL;
460 version = LDAP_VERSION3;
461 sasl_authz_id = strdup( optarg );
463 fprintf( stderr, "%s: not compiled with SASL support\n",
465 return( EXIT_FAILURE );
470 if( version == LDAP_VERSION2 ) {
471 fprintf( stderr, "%s: -Z incompatible with version %d\n",
475 version = LDAP_VERSION3;
478 fprintf( stderr, "%s: not compiled with TLS support\n",
480 return( EXIT_FAILURE );
484 fprintf( stderr, "%s: unrecognized option -%c\n",
487 return( EXIT_FAILURE );
492 version = LDAP_VERSION3;
494 if (authmethod == -1 && version > LDAP_VERSION2) {
495 #ifdef HAVE_CYRUS_SASL
496 authmethod = LDAP_AUTH_SASL;
498 authmethod = LDAP_AUTH_SIMPLE;
503 if (argc - optind == 2) {
504 if (( rdn = strdup( argv[argc - 1] )) == NULL ) {
506 return( EXIT_FAILURE );
508 if (( entrydn = strdup( argv[argc - 2] )) == NULL ) {
510 return( EXIT_FAILURE );
513 } else if ( argc - optind != 0 ) {
514 fprintf( stderr, "%s: invalid number of arguments (%d), "
515 "only two allowed\n", prog, argc-optind );
517 return( EXIT_FAILURE );
520 if ( infile != NULL ) {
521 if (( fp = fopen( infile, "r" )) == NULL ) {
523 return( EXIT_FAILURE );
530 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
531 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
533 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
534 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
539 (void) SIGNAL( SIGPIPE, SIG_IGN );
542 if( ( ldaphost != NULL || ldapport ) && ( ldapuri == NULL ) ) {
544 fprintf( stderr, "ldap_init( %s, %d )\n",
545 ldaphost != NULL ? ldaphost : "<DEFAULT>",
549 ld = ldap_init( ldaphost, ldapport );
551 perror("ldapmodify: ldap_init");
557 fprintf( stderr, "ldap_initialize( %s )\n",
558 ldapuri != NULL ? ldapuri : "<DEFAULT>" );
561 rc = ldap_initialize( &ld, ldapuri );
562 if( rc != LDAP_SUCCESS ) {
563 fprintf( stderr, "Could not create LDAP session handle (%d): %s\n",
564 rc, ldap_err2string(rc) );
570 if( ldap_set_option( ld, LDAP_OPT_REFERRALS,
571 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
573 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
574 referrals ? "on" : "off" );
578 if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version )
579 != LDAP_OPT_SUCCESS )
581 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
586 if ( use_tls && ( ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS )) {
587 ldap_perror( ld, "ldap_start_tls" );
589 return( EXIT_FAILURE );
594 passwd.bv_val = getpassphrase("Enter LDAP Password: ");
595 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
598 if ( authmethod == LDAP_AUTH_SASL ) {
599 #ifdef HAVE_CYRUS_SASL
602 if( sasl_secprops != NULL ) {
603 rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
604 (void *) sasl_secprops );
606 if( rc != LDAP_OPT_SUCCESS ) {
608 "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
610 return( EXIT_FAILURE );
614 defaults = lutil_sasl_defaults( ld,
621 rc = ldap_sasl_interactive_bind_s( ld, binddn,
622 sasl_mech, NULL, NULL,
623 sasl_flags, lutil_sasl_interact, defaults );
625 if( rc != LDAP_SUCCESS ) {
626 ldap_perror( ld, "ldap_sasl_interactive_bind_s" );
627 return( EXIT_FAILURE );
630 fprintf( stderr, "%s: not compiled with SASL support\n",
632 return( EXIT_FAILURE );
636 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
638 ldap_perror( ld, "ldap_bind" );
639 return( EXIT_FAILURE );
646 LDAPControl *ctrls[2];
650 c.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
651 c.ldctl_value.bv_val = NULL;
652 c.ldctl_value.bv_len = 0;
653 c.ldctl_iscritical = manageDSAit > 1;
655 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, ctrls );
657 if( err != LDAP_OPT_SUCCESS ) {
658 fprintf( stderr, "Could not set ManageDSAit %scontrol\n",
659 c.ldctl_iscritical ? "critical " : "" );
660 if( c.ldctl_iscritical ) {
661 exit( EXIT_FAILURE );
668 rc = domodrdn( ld, entrydn, rdn, newSuperior, remove );
669 else while ((rc == 0 || contoper) && fgets(buf, sizeof(buf), fp) != NULL) {
670 if ( *buf != '\0' ) { /* blank lines optional, skip */
671 buf[ strlen( buf ) - 1 ] = '\0'; /* remove nl */
673 if ( havedn ) { /* have DN, get RDN */
674 if (( rdn = strdup( buf )) == NULL ) {
676 return( EXIT_FAILURE );
678 rc = domodrdn(ld, entrydn, rdn, newSuperior, remove );
680 } else if ( !havedn ) { /* don't have DN yet */
681 if (( entrydn = strdup( buf )) == NULL ) {
683 return( EXIT_FAILURE );
701 int remove ) /* flag: remove old RDN */
704 char *matcheddn=NULL, *text=NULL, **refs=NULL;
708 printf( "Renaming \"%s\"\n", dn );
709 printf( "\tnew rdn=\"%s\" (%s old rdn)\n",
710 rdn, remove ? "delete" : "keep" );
711 if( newSuperior != NULL ) {
712 printf("\tnew parent=\"%s\"\n", newSuperior);
716 if( not ) return LDAP_SUCCESS;
718 rc = ldap_rename( ld, dn, rdn, newSuperior, remove,
721 if ( rc != LDAP_SUCCESS ) {
722 fprintf( stderr, "%s: ldap_rename: %s (%d)\n",
723 prog, ldap_err2string( rc ), rc );
727 rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, NULL, &res );
729 ldap_perror( ld, "ldapmodrdn: ldap_result" );
733 rc = ldap_parse_result( ld, res, &code, &matcheddn, &text, &refs, NULL, 1 );
735 if( rc != LDAP_SUCCESS ) {
736 fprintf( stderr, "%s: ldap_parse_result: %s (%d)\n",
737 prog, ldap_err2string( rc ), rc );
741 if( verbose || code != LDAP_SUCCESS ||
742 (matcheddn && *matcheddn) || (text && *text) || (refs && *refs) )
744 printf( "Rename Result: %s (%d)\n",
745 ldap_err2string( code ), code );
747 if( text && *text ) {
748 printf( "Additional info: %s\n", text );
751 if( matcheddn && *matcheddn ) {
752 printf( "Matched DN: %s\n", matcheddn );
757 for( i=0; refs[i]; i++ ) {
758 printf("Referral: %s\n", refs[i] );
764 ber_memfree( matcheddn );
765 ber_memvfree( (void **) refs );