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;
116 authmethod = LDAP_AUTH_SIMPLE;
119 prog = (prog = strrchr(argv[0], *LDAP_DIRSEP)) == NULL ? argv[0] : prog + 1;
121 while (( i = getopt( argc, argv, "cf:rs:" "Cd:D:h:H:IkKMnO:p:P:QRU:vw:WxX:Y:Z" )) != EOF ) {
127 case 'f': /* read from file */
128 if( infile != NULL ) {
129 fprintf( stderr, "%s: -f previously specified\n" );
132 infile = strdup( optarg );
134 case 'r': /* remove old RDN */
137 case 's': /* newSuperior */
138 if( version == LDAP_VERSION2 ) {
139 fprintf( stderr, "%s: -X incompatible with LDAPv%d\n",
143 newSuperior = strdup( optarg );
144 version = LDAP_VERSION3;
152 debug |= atoi( optarg );
154 case 'D': /* bind DN */
155 if( binddn != NULL ) {
156 fprintf( stderr, "%s: -D previously specified\n" );
159 binddn = strdup( optarg );
161 case 'h': /* ldap host */
162 if( ldapuri != NULL ) {
163 fprintf( stderr, "%s: -h incompatible with -H\n" );
166 if( ldaphost != NULL ) {
167 fprintf( stderr, "%s: -h previously specified\n" );
170 ldaphost = strdup( optarg );
172 case 'H': /* ldap URI */
173 if( ldaphost != NULL ) {
174 fprintf( stderr, "%s: -H incompatible with -h\n" );
178 fprintf( stderr, "%s: -H incompatible with -p\n" );
181 if( ldapuri != NULL ) {
182 fprintf( stderr, "%s: -H previously specified\n" );
185 ldapuri = strdup( optarg );
188 #ifdef HAVE_CYRUS_SASL
189 if( version == LDAP_VERSION2 ) {
190 fprintf( stderr, "%s: -I incompatible with version %d\n",
194 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
195 fprintf( stderr, "%s: incompatible previous "
196 "authentication choice\n",
200 authmethod = LDAP_AUTH_SASL;
201 version = LDAP_VERSION3;
202 sasl_flags = LDAP_SASL_INTERACTIVE;
205 fprintf( stderr, "%s: was not compiled with SASL support\n",
207 return( EXIT_FAILURE );
209 case 'k': /* kerberos bind */
210 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
211 if( version > LDAP_VERSION2 ) {
212 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
217 if( authmethod != -1 ) {
218 fprintf( stderr, "%s: -k incompatible with previous "
219 "authentication choice\n", prog );
223 authmethod = LDAP_AUTH_KRBV4;
225 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
229 case 'K': /* kerberos bind, part one only */
230 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
231 if( version > LDAP_VERSION2 ) {
232 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
236 if( authmethod != -1 ) {
237 fprintf( stderr, "%s: incompatible with previous "
238 "authentication choice\n", prog );
242 authmethod = LDAP_AUTH_KRBV41;
244 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
245 return( EXIT_FAILURE );
249 /* enable Manage DSA IT */
250 if( version == LDAP_VERSION2 ) {
251 fprintf( stderr, "%s: -M incompatible with LDAPv%d\n",
256 version = LDAP_VERSION3;
258 case 'n': /* print deletes, don't actually do them */
262 #ifdef HAVE_CYRUS_SASL
263 if( sasl_secprops != NULL ) {
264 fprintf( stderr, "%s: -O previously specified\n" );
267 if( version == LDAP_VERSION2 ) {
268 fprintf( stderr, "%s: -O incompatible with LDAPv%d\n",
272 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
273 fprintf( stderr, "%s: incompatible previous "
274 "authentication choice\n", prog );
277 authmethod = LDAP_AUTH_SASL;
278 version = LDAP_VERSION3;
279 sasl_secprops = strdup( optarg );
281 fprintf( stderr, "%s: not compiled with SASL support\n",
283 return( EXIT_FAILURE );
288 fprintf( stderr, "%s: -p previously specified\n" );
291 ldapport = atoi( optarg );
294 switch( atoi(optarg) ) {
296 if( version == LDAP_VERSION3 ) {
297 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
301 version = LDAP_VERSION2;
304 if( version == LDAP_VERSION2 ) {
305 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
309 version = LDAP_VERSION3;
312 fprintf( stderr, "%s: protocol version should be 2 or 3\n",
315 return( EXIT_FAILURE );
318 #ifdef HAVE_CYRUS_SASL
319 if( version == LDAP_VERSION2 ) {
320 fprintf( stderr, "%s: -Q incompatible with version %d\n",
324 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
325 fprintf( stderr, "%s: incompatible previous "
326 "authentication choice\n",
330 authmethod = LDAP_AUTH_SASL;
331 version = LDAP_VERSION3;
332 sasl_flags = LDAP_SASL_QUIET;
335 fprintf( stderr, "%s: not compiled with SASL support\n",
337 return( EXIT_FAILURE );
340 #ifdef HAVE_CYRUS_SASL
341 if( sasl_realm != NULL ) {
342 fprintf( stderr, "%s: -R previously specified\n" );
345 if( version == LDAP_VERSION2 ) {
346 fprintf( stderr, "%s: -R incompatible with version %d\n",
350 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
351 fprintf( stderr, "%s: incompatible previous "
352 "authentication choice\n",
356 authmethod = LDAP_AUTH_SASL;
357 version = LDAP_VERSION3;
358 sasl_realm = strdup( optarg );
360 fprintf( stderr, "%s: not compiled with SASL support\n",
362 return( EXIT_FAILURE );
366 #ifdef HAVE_CYRUS_SASL
367 if( sasl_authc_id != NULL ) {
368 fprintf( stderr, "%s: -U previously specified\n" );
371 if( version == LDAP_VERSION2 ) {
372 fprintf( stderr, "%s: -U incompatible with version %d\n",
376 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
377 fprintf( stderr, "%s: incompatible previous "
378 "authentication choice\n",
382 authmethod = LDAP_AUTH_SASL;
383 version = LDAP_VERSION3;
384 sasl_authc_id = strdup( optarg );
386 fprintf( stderr, "%s: not compiled with SASL support\n",
388 return( EXIT_FAILURE );
391 case 'v': /* verbose mode */
394 case 'w': /* password */
395 passwd.bv_val = strdup( optarg );
399 for( p = optarg; *p == '\0'; p++ ) {
403 passwd.bv_len = strlen( passwd.bv_val );
409 #ifdef HAVE_CYRUS_SASL
410 if( sasl_mech != NULL ) {
411 fprintf( stderr, "%s: -Y previously specified\n" );
414 if( version == LDAP_VERSION2 ) {
415 fprintf( stderr, "%s: -Y incompatible with version %d\n",
419 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
420 fprintf( stderr, "%s: incompatible with authentication choice\n", prog );
423 authmethod = LDAP_AUTH_SASL;
424 version = LDAP_VERSION3;
425 sasl_mech = strdup( optarg );
427 fprintf( stderr, "%s: not compiled with SASL support\n",
429 return( EXIT_FAILURE );
433 if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
434 fprintf( stderr, "%s: incompatible with previous "
435 "authentication choice\n", prog );
438 authmethod = LDAP_AUTH_SIMPLE;
441 #ifdef HAVE_CYRUS_SASL
442 if( sasl_authz_id != NULL ) {
443 fprintf( stderr, "%s: -X previously specified\n" );
446 if( version == LDAP_VERSION2 ) {
447 fprintf( stderr, "%s: -X incompatible with LDAPv%d\n",
451 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
452 fprintf( stderr, "%s: -X incompatible with "
453 "authentication choice\n", prog );
456 authmethod = LDAP_AUTH_SASL;
457 version = LDAP_VERSION3;
458 sasl_authz_id = strdup( optarg );
460 fprintf( stderr, "%s: not compiled with SASL support\n",
462 return( EXIT_FAILURE );
467 if( version == LDAP_VERSION2 ) {
468 fprintf( stderr, "%s: -Z incompatible with version %d\n",
472 version = LDAP_VERSION3;
475 fprintf( stderr, "%s: not compiled with TLS support\n",
477 return( EXIT_FAILURE );
481 fprintf( stderr, "%s: unrecongized option -%c\n",
484 return( EXIT_FAILURE );
489 version = LDAP_VERSION3;
491 if (authmethod == -1 && version > LDAP_VERSION2) {
492 #ifdef HAVE_CYRUS_SASL
493 authmethod = LDAP_AUTH_SASL;
495 authmethod = LDAP_AUTH_SIMPLE;
500 if (argc - optind == 2) {
501 if (( rdn = strdup( argv[argc - 1] )) == NULL ) {
503 return( EXIT_FAILURE );
505 if (( entrydn = strdup( argv[argc - 2] )) == NULL ) {
507 return( EXIT_FAILURE );
510 } else if ( argc - optind != 0 ) {
511 fprintf( stderr, "%s: invalid number of arguments (%d), "
512 "only two allowed\n", prog, argc-optind );
514 return( EXIT_FAILURE );
517 if ( infile != NULL ) {
518 if (( fp = fopen( infile, "r" )) == NULL ) {
520 return( EXIT_FAILURE );
527 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
528 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
530 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
531 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
536 (void) SIGNAL( SIGPIPE, SIG_IGN );
539 if( ( ldaphost != NULL || ldapport ) && ( ldapuri == NULL ) ) {
541 fprintf( stderr, "ldap_init( %s, %d )\n",
542 ldaphost != NULL ? ldaphost : "<DEFAULT>",
545 ld = ldap_init( ldaphost, ldapport );
549 fprintf( stderr, "ldap_initialize( %s )\n",
550 ldapuri != NULL ? ldapuri : "<DEFAULT>" );
552 (void) ldap_initialize( &ld, ldapuri );
556 fprintf( stderr, "Could not create LDAP session handle (%d): %s\n",
557 rc, ldap_err2string(rc) );
562 if( ldap_set_option( ld, LDAP_OPT_REFERRALS,
563 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
565 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
566 referrals ? "on" : "off" );
570 if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version )
571 != LDAP_OPT_SUCCESS )
573 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
578 if ( use_tls && ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS ) {
580 ldap_perror( ld, "ldap_start_tls" );
581 return( EXIT_FAILURE );
583 fprintf( stderr, "WARNING: could not start TLS\n" );
587 passwd.bv_val = getpassphrase("Enter LDAP Password: ");
588 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
591 if ( authmethod == LDAP_AUTH_SASL ) {
592 #ifdef HAVE_CYRUS_SASL
595 if( sasl_secprops != NULL ) {
596 rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
597 (void *) sasl_secprops );
599 if( rc != LDAP_OPT_SUCCESS ) {
601 "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
603 return( EXIT_FAILURE );
607 defaults = lutil_sasl_defaults( ld,
614 rc = ldap_sasl_interactive_bind_s( ld, binddn,
615 sasl_mech, NULL, NULL,
616 sasl_flags, lutil_sasl_interact, defaults );
618 if( rc != LDAP_SUCCESS ) {
619 ldap_perror( ld, "ldap_sasl_interactive_bind_s" );
620 return( EXIT_FAILURE );
623 fprintf( stderr, "%s: not compiled with SASL support\n",
625 return( EXIT_FAILURE );
629 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
631 ldap_perror( ld, "ldap_bind" );
632 return( EXIT_FAILURE );
639 LDAPControl *ctrls[2];
643 c.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
644 c.ldctl_value.bv_val = NULL;
645 c.ldctl_value.bv_len = 0;
646 c.ldctl_iscritical = manageDSAit > 1;
648 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, ctrls );
650 if( err != LDAP_OPT_SUCCESS ) {
651 fprintf( stderr, "Could not set ManageDSAit %scontrol\n",
652 c.ldctl_iscritical ? "critical " : "" );
653 if( c.ldctl_iscritical ) {
654 exit( EXIT_FAILURE );
661 rc = domodrdn( ld, entrydn, rdn, newSuperior, remove );
662 else while ((rc == 0 || contoper) && fgets(buf, sizeof(buf), fp) != NULL) {
663 if ( *buf != '\0' ) { /* blank lines optional, skip */
664 buf[ strlen( buf ) - 1 ] = '\0'; /* remove nl */
666 if ( havedn ) { /* have DN, get RDN */
667 if (( rdn = strdup( buf )) == NULL ) {
669 return( EXIT_FAILURE );
671 rc = domodrdn(ld, entrydn, rdn, newSuperior, remove );
673 } else if ( !havedn ) { /* don't have DN yet */
674 if (( entrydn = strdup( buf )) == NULL ) {
676 return( EXIT_FAILURE );
694 int remove ) /* flag: remove old RDN */
697 char *matcheddn=NULL, *text=NULL, **refs=NULL;
701 printf( "Renaming \"%s\"\n", dn );
702 printf( "\tnew rdn=\"%s\" (%s old rdn)\n",
703 rdn, remove ? "delete" : "keep" );
704 if( newSuperior != NULL ) {
705 printf("\tnew parent=\"%s\"\n", newSuperior);
709 if( not ) return LDAP_SUCCESS;
711 rc = ldap_rename( ld, dn, rdn, newSuperior, remove,
714 if ( rc != LDAP_SUCCESS ) {
715 fprintf( stderr, "%s: ldap_rename: %s (%d)\n",
716 prog, ldap_err2string( rc ), rc );
720 rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, NULL, &res );
722 ldap_perror( ld, "ldapmodrdn: ldap_result" );
726 rc = ldap_parse_result( ld, res, &code, &matcheddn, &text, &refs, NULL, 1 );
728 if( rc != LDAP_SUCCESS ) {
729 fprintf( stderr, "%s: ldap_parse_result: %s (%d)\n",
730 prog, ldap_err2string( rc ), rc );
734 if( verbose || code != LDAP_SUCCESS ||
735 (matcheddn && *matcheddn) || (text && *text) || (refs && *refs) )
737 printf( "Rename Result: %s (%d)\n",
738 ldap_err2string( code ), code );
740 if( text && *text ) {
741 printf( "Additional info: %s\n", text );
744 if( matcheddn && *matcheddn ) {
745 printf( "Matched DN: %s\n", matcheddn );
750 for( i=0; refs[i]; i++ ) {
751 printf("Referral: %s\n", refs[i] );
757 ber_memfree( matcheddn );
758 ber_memvfree( (void **) refs );