3 * Copyright 1998-2000 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>
32 #include "lutil_ldap.h"
33 #include "ldap_defaults.h"
35 static char *prog = NULL;
36 static char *binddn = NULL;
37 static struct berval passwd = { 0, NULL };
38 static char *ldapuri = NULL;
39 static char *ldaphost = NULL;
40 static int ldapport = 0;
41 #ifdef HAVE_CYRUS_SASL
42 static unsigned sasl_flags = LDAP_SASL_AUTOMATIC;
43 static char *sasl_realm = NULL;
44 static char *sasl_authc_id = NULL;
45 static char *sasl_authz_id = NULL;
46 static char *sasl_mech = NULL;
47 static char *sasl_secprops = NULL;
49 static int use_tls = 0;
50 static int not, verbose, contoper;
51 static LDAP *ld = NULL;
58 int remove ); /* flag: remove old RDN */
61 usage( const char *s )
64 "Rename LDAP entries\n\n"
65 "usage: %s [options] [dn rdn]\n"
66 " dn rdn: If given, rdn will replace the RDN of the entry specified by DN\n"
67 " If not given, the list of modifications is read from stdin or\n"
68 " from the file specified by \"-f file\" (see man page).\n"
70 " -c continuous operation mode (do not stop on errors)\n"
71 " -f file read operations from `file'\n"
72 " -r remove old RDN\n"
73 " -s newsup new superior entry\n"
76 " -d level set LDAP debugging level to `level'\n"
77 " -D binddn bind DN\n"
78 " -f file read operations from `file'\n"
79 " -h host LDAP server\n"
80 " -H URI LDAP Uniform Resource Indentifier(s)\n"
81 " -I use SASL Interactive mode\n"
82 " -k use Kerberos authentication\n"
83 " -K like -k, but do only step 1 of the Kerberos bind\n"
84 " -M enable Manage DSA IT control (-MM to make critical)\n"
85 " -n show what would be done but don't actually search\n"
86 " -O props SASL security properties\n"
87 " -p port port on LDAP server\n"
88 " -P version procotol version (default: 3)\n"
89 " -Q use SASL Quiet mode\n"
90 " -R realm SASL realm\n"
91 " -U user SASL authentication identity (username)\n"
92 " -v run in verbose mode (diagnostics to standard output)\n"
93 " -w passwd bind passwd (for simple authentication)\n"
94 " -W prompt for bind passwd\n"
95 " -x Simple authentication\n"
96 " -X id SASL authorization identity (\"dn:<dn>\" or \"u:<user>\")\n"
97 " -Y mech SASL mechanism\n"
98 " -Z Start TLS request (-ZZ to require successful response)\n"
101 exit( EXIT_FAILURE );
105 main(int argc, char **argv)
107 char *infile, *entrydn = NULL, *rdn = NULL, buf[ 4096 ];
109 int rc, i, remove, havedn, authmethod, version, want_bindpw, debug, manageDSAit;
111 char *newSuperior=NULL;
114 not = contoper = verbose = remove = want_bindpw =
115 debug = manageDSAit = referrals = 0;
119 prog = (prog = strrchr(argv[0], *LDAP_DIRSEP)) == NULL ? argv[0] : prog + 1;
121 while (( i = getopt( argc, argv, "cf:rs:"
122 "Cd:D:h:H:IkKMnO:p:P:QR:U:vw:WxX:Y:Z" )) != EOF )
129 case 'f': /* read from file */
130 if( infile != NULL ) {
131 fprintf( stderr, "%s: -f previously specified\n", prog );
134 infile = strdup( optarg );
136 case 'r': /* remove old RDN */
139 case 's': /* newSuperior */
140 if( version == LDAP_VERSION2 ) {
141 fprintf( stderr, "%s: -X incompatible with LDAPv%d\n",
145 newSuperior = strdup( optarg );
146 version = LDAP_VERSION3;
154 debug |= atoi( optarg );
156 case 'D': /* bind DN */
157 if( binddn != NULL ) {
158 fprintf( stderr, "%s: -D previously specified\n", prog );
161 binddn = strdup( optarg );
163 case 'h': /* ldap host */
164 if( ldapuri != NULL ) {
165 fprintf( stderr, "%s: -h incompatible with -H\n", prog );
168 if( ldaphost != NULL ) {
169 fprintf( stderr, "%s: -h previously specified\n", prog );
172 ldaphost = strdup( optarg );
174 case 'H': /* ldap URI */
175 if( ldaphost != NULL ) {
176 fprintf( stderr, "%s: -H incompatible with -h\n", prog );
180 fprintf( stderr, "%s: -H incompatible with -p\n", prog );
183 if( ldapuri != NULL ) {
184 fprintf( stderr, "%s: -H previously specified\n", prog );
187 ldapuri = strdup( optarg );
190 #ifdef HAVE_CYRUS_SASL
191 if( version == LDAP_VERSION2 ) {
192 fprintf( stderr, "%s: -I incompatible with version %d\n",
196 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
197 fprintf( stderr, "%s: incompatible previous "
198 "authentication choice\n",
202 authmethod = LDAP_AUTH_SASL;
203 version = LDAP_VERSION3;
204 sasl_flags = LDAP_SASL_INTERACTIVE;
207 fprintf( stderr, "%s: was not compiled with SASL support\n",
209 return( EXIT_FAILURE );
211 case 'k': /* kerberos bind */
212 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
213 if( version > LDAP_VERSION2 ) {
214 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
219 if( authmethod != -1 ) {
220 fprintf( stderr, "%s: -k incompatible with previous "
221 "authentication choice\n", prog );
225 authmethod = LDAP_AUTH_KRBV4;
227 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
231 case 'K': /* kerberos bind, part one only */
232 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
233 if( version > LDAP_VERSION2 ) {
234 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
238 if( authmethod != -1 ) {
239 fprintf( stderr, "%s: incompatible with previous "
240 "authentication choice\n", prog );
244 authmethod = LDAP_AUTH_KRBV41;
246 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
247 return( EXIT_FAILURE );
251 /* enable Manage DSA IT */
252 if( version == LDAP_VERSION2 ) {
253 fprintf( stderr, "%s: -M incompatible with LDAPv%d\n",
258 version = LDAP_VERSION3;
260 case 'n': /* print deletes, don't actually do them */
264 #ifdef HAVE_CYRUS_SASL
265 if( sasl_secprops != NULL ) {
266 fprintf( stderr, "%s: -O previously specified\n", prog );
269 if( version == LDAP_VERSION2 ) {
270 fprintf( stderr, "%s: -O incompatible with LDAPv%d\n",
274 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
275 fprintf( stderr, "%s: incompatible previous "
276 "authentication choice\n", prog );
279 authmethod = LDAP_AUTH_SASL;
280 version = LDAP_VERSION3;
281 sasl_secprops = strdup( optarg );
283 fprintf( stderr, "%s: not compiled with SASL support\n",
285 return( EXIT_FAILURE );
290 fprintf( stderr, "%s: -p previously specified\n", prog );
293 ldapport = atoi( optarg );
296 switch( atoi(optarg) ) {
298 if( version == LDAP_VERSION3 ) {
299 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
303 version = LDAP_VERSION2;
306 if( version == LDAP_VERSION2 ) {
307 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
311 version = LDAP_VERSION3;
314 fprintf( stderr, "%s: protocol version should be 2 or 3\n",
317 return( EXIT_FAILURE );
320 #ifdef HAVE_CYRUS_SASL
321 if( version == LDAP_VERSION2 ) {
322 fprintf( stderr, "%s: -Q incompatible with version %d\n",
326 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
327 fprintf( stderr, "%s: incompatible previous "
328 "authentication choice\n",
332 authmethod = LDAP_AUTH_SASL;
333 version = LDAP_VERSION3;
334 sasl_flags = LDAP_SASL_QUIET;
337 fprintf( stderr, "%s: not compiled with SASL support\n",
339 return( EXIT_FAILURE );
342 #ifdef HAVE_CYRUS_SASL
343 if( sasl_realm != NULL ) {
344 fprintf( stderr, "%s: -R previously specified\n", prog );
347 if( version == LDAP_VERSION2 ) {
348 fprintf( stderr, "%s: -R incompatible with version %d\n",
352 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
353 fprintf( stderr, "%s: incompatible previous "
354 "authentication choice\n",
358 authmethod = LDAP_AUTH_SASL;
359 version = LDAP_VERSION3;
360 sasl_realm = strdup( optarg );
362 fprintf( stderr, "%s: not compiled with SASL support\n",
364 return( EXIT_FAILURE );
368 #ifdef HAVE_CYRUS_SASL
369 if( sasl_authc_id != NULL ) {
370 fprintf( stderr, "%s: -U previously specified\n", prog );
373 if( version == LDAP_VERSION2 ) {
374 fprintf( stderr, "%s: -U incompatible with version %d\n",
378 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
379 fprintf( stderr, "%s: incompatible previous "
380 "authentication choice\n",
384 authmethod = LDAP_AUTH_SASL;
385 version = LDAP_VERSION3;
386 sasl_authc_id = strdup( optarg );
388 fprintf( stderr, "%s: not compiled with SASL support\n",
390 return( EXIT_FAILURE );
393 case 'v': /* verbose mode */
396 case 'w': /* password */
397 passwd.bv_val = strdup( optarg );
401 for( p = optarg; *p != '\0'; p++ ) {
405 passwd.bv_len = strlen( passwd.bv_val );
411 #ifdef HAVE_CYRUS_SASL
412 if( sasl_mech != NULL ) {
413 fprintf( stderr, "%s: -Y previously specified\n", prog );
416 if( version == LDAP_VERSION2 ) {
417 fprintf( stderr, "%s: -Y incompatible with version %d\n",
421 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
422 fprintf( stderr, "%s: incompatible with authentication choice\n", prog );
425 authmethod = LDAP_AUTH_SASL;
426 version = LDAP_VERSION3;
427 sasl_mech = strdup( optarg );
429 fprintf( stderr, "%s: not compiled with SASL support\n",
431 return( EXIT_FAILURE );
435 if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
436 fprintf( stderr, "%s: incompatible with previous "
437 "authentication choice\n", prog );
440 authmethod = LDAP_AUTH_SIMPLE;
443 #ifdef HAVE_CYRUS_SASL
444 if( sasl_authz_id != NULL ) {
445 fprintf( stderr, "%s: -X previously specified\n", prog );
448 if( version == LDAP_VERSION2 ) {
449 fprintf( stderr, "%s: -X incompatible with LDAPv%d\n",
453 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
454 fprintf( stderr, "%s: -X incompatible with "
455 "authentication choice\n", prog );
458 authmethod = LDAP_AUTH_SASL;
459 version = LDAP_VERSION3;
460 sasl_authz_id = strdup( optarg );
462 fprintf( stderr, "%s: not compiled with SASL support\n",
464 return( EXIT_FAILURE );
469 if( version == LDAP_VERSION2 ) {
470 fprintf( stderr, "%s: -Z incompatible with version %d\n",
474 version = LDAP_VERSION3;
477 fprintf( stderr, "%s: not compiled with TLS support\n",
479 return( EXIT_FAILURE );
483 fprintf( stderr, "%s: unrecognized option -%c\n",
486 return( EXIT_FAILURE );
491 version = LDAP_VERSION3;
493 if (authmethod == -1 && version > LDAP_VERSION2) {
494 #ifdef HAVE_CYRUS_SASL
495 authmethod = LDAP_AUTH_SASL;
497 authmethod = LDAP_AUTH_SIMPLE;
502 if (argc - optind == 2) {
503 if (( rdn = strdup( argv[argc - 1] )) == NULL ) {
505 return( EXIT_FAILURE );
507 if (( entrydn = strdup( argv[argc - 2] )) == NULL ) {
509 return( EXIT_FAILURE );
512 } else if ( argc - optind != 0 ) {
513 fprintf( stderr, "%s: invalid number of arguments (%d), "
514 "only two allowed\n", prog, argc-optind );
516 return( EXIT_FAILURE );
519 if ( infile != NULL ) {
520 if (( fp = fopen( infile, "r" )) == NULL ) {
522 return( EXIT_FAILURE );
529 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
530 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
532 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
533 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
538 (void) SIGNAL( SIGPIPE, SIG_IGN );
541 if( ( ldaphost != NULL || ldapport ) && ( ldapuri == NULL ) ) {
543 fprintf( stderr, "ldap_init( %s, %d )\n",
544 ldaphost != NULL ? ldaphost : "<DEFAULT>",
548 ld = ldap_init( ldaphost, ldapport );
550 perror("ldapsearch: ldap_init");
556 fprintf( stderr, "ldap_initialize( %s )\n",
557 ldapuri != NULL ? ldapuri : "<DEFAULT>" );
560 rc = ldap_initialize( &ld, ldapuri );
561 if( rc != LDAP_SUCCESS ) {
562 fprintf( stderr, "Could not create LDAP session handle (%d): %s\n",
563 rc, ldap_err2string(rc) );
569 if( ldap_set_option( ld, LDAP_OPT_REFERRALS,
570 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
572 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
573 referrals ? "on" : "off" );
577 if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version )
578 != LDAP_OPT_SUCCESS )
580 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
585 if ( use_tls && ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS ) {
586 ldap_perror( ld, "ldap_start_tls" );
588 return( EXIT_FAILURE );
593 passwd.bv_val = getpassphrase("Enter LDAP Password: ");
594 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
597 if ( authmethod == LDAP_AUTH_SASL ) {
598 #ifdef HAVE_CYRUS_SASL
601 if( sasl_secprops != NULL ) {
602 rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
603 (void *) sasl_secprops );
605 if( rc != LDAP_OPT_SUCCESS ) {
607 "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
609 return( EXIT_FAILURE );
613 defaults = lutil_sasl_defaults( ld,
620 rc = ldap_sasl_interactive_bind_s( ld, binddn,
621 sasl_mech, NULL, NULL,
622 sasl_flags, lutil_sasl_interact, defaults );
624 if( rc != LDAP_SUCCESS ) {
625 ldap_perror( ld, "ldap_sasl_interactive_bind_s" );
626 return( EXIT_FAILURE );
629 fprintf( stderr, "%s: not compiled with SASL support\n",
631 return( EXIT_FAILURE );
635 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
637 ldap_perror( ld, "ldap_bind" );
638 return( EXIT_FAILURE );
645 LDAPControl *ctrls[2];
649 c.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
650 c.ldctl_value.bv_val = NULL;
651 c.ldctl_value.bv_len = 0;
652 c.ldctl_iscritical = manageDSAit > 1;
654 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, ctrls );
656 if( err != LDAP_OPT_SUCCESS ) {
657 fprintf( stderr, "Could not set ManageDSAit %scontrol\n",
658 c.ldctl_iscritical ? "critical " : "" );
659 if( c.ldctl_iscritical ) {
660 exit( EXIT_FAILURE );
667 rc = domodrdn( ld, entrydn, rdn, newSuperior, remove );
668 else while ((rc == 0 || contoper) && fgets(buf, sizeof(buf), fp) != NULL) {
669 if ( *buf != '\0' ) { /* blank lines optional, skip */
670 buf[ strlen( buf ) - 1 ] = '\0'; /* remove nl */
672 if ( havedn ) { /* have DN, get RDN */
673 if (( rdn = strdup( buf )) == NULL ) {
675 return( EXIT_FAILURE );
677 rc = domodrdn(ld, entrydn, rdn, newSuperior, remove );
679 } else if ( !havedn ) { /* don't have DN yet */
680 if (( entrydn = strdup( buf )) == NULL ) {
682 return( EXIT_FAILURE );
700 int remove ) /* flag: remove old RDN */
703 char *matcheddn=NULL, *text=NULL, **refs=NULL;
707 printf( "Renaming \"%s\"\n", dn );
708 printf( "\tnew rdn=\"%s\" (%s old rdn)\n",
709 rdn, remove ? "delete" : "keep" );
710 if( newSuperior != NULL ) {
711 printf("\tnew parent=\"%s\"\n", newSuperior);
715 if( not ) return LDAP_SUCCESS;
717 rc = ldap_rename( ld, dn, rdn, newSuperior, remove,
720 if ( rc != LDAP_SUCCESS ) {
721 fprintf( stderr, "%s: ldap_rename: %s (%d)\n",
722 prog, ldap_err2string( rc ), rc );
726 rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, NULL, &res );
728 ldap_perror( ld, "ldapmodrdn: ldap_result" );
732 rc = ldap_parse_result( ld, res, &code, &matcheddn, &text, &refs, NULL, 1 );
734 if( rc != LDAP_SUCCESS ) {
735 fprintf( stderr, "%s: ldap_parse_result: %s (%d)\n",
736 prog, ldap_err2string( rc ), rc );
740 if( verbose || code != LDAP_SUCCESS ||
741 (matcheddn && *matcheddn) || (text && *text) || (refs && *refs) )
743 printf( "Rename Result: %s (%d)\n",
744 ldap_err2string( code ), code );
746 if( text && *text ) {
747 printf( "Additional info: %s\n", text );
750 if( matcheddn && *matcheddn ) {
751 printf( "Matched DN: %s\n", matcheddn );
756 for( i=0; refs[i]; i++ ) {
757 printf("Referral: %s\n", refs[i] );
763 ber_memfree( matcheddn );
764 ber_memvfree( (void **) refs );