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 *ldaphost = NULL;
39 static int ldapport = 0;
40 #ifdef HAVE_CYRUS_SASL
41 static unsigned sasl_flags = LDAP_SASL_AUTOMATIC;
42 static char *sasl_realm = NULL;
43 static char *sasl_authc_id = NULL;
44 static char *sasl_authz_id = NULL;
45 static char *sasl_mech = NULL;
46 static char *sasl_secprops = NULL;
48 static int use_tls = 0;
49 static int not, verbose, contoper;
57 int remove ); /* flag: remove old RDN */
60 usage( const char *s )
63 "Rename LDAP entries\n\n"
64 "usage: %s [options] [dn rdn]\n"
65 " dn rdn: If given, rdn will replace the RDN of the entry specified by DN\n"
66 " If not given, the list of modifications is read from stdin or\n"
67 " from the file specified by \"-f file\" (see man page).\n"
69 " -c continuous operation mode (do not stop on errors)\n"
70 " -f file read operations from `file'\n"
71 " -r remove old RDN\n"
72 " -s newsup new superior entry\n"
75 " -d level set LDAP debugging level to `level'\n"
76 " -D binddn bind DN\n"
77 " -f file read operations from `file'\n"
78 " -h host LDAP server\n"
79 " -I use SASL Interactive mode\n"
80 " -k use Kerberos authentication\n"
81 " -K like -k, but do only step 1 of the Kerberos bind\n"
82 " -M enable Manage DSA IT control (-MM to make critical)\n"
83 " -n show what would be done but don't actually search\n"
84 " -O props SASL security properties\n"
85 " -p port port on LDAP server\n"
86 " -P version procotol version (default: 3)\n"
87 " -Q use SASL Quiet mode\n"
88 " -R realm SASL realm\n"
89 " -U user SASL authentication identity (username)\n"
90 " -v run in verbose mode (diagnostics to standard output)\n"
91 " -w passwd bind passwd (for simple authentication)\n"
92 " -W prompt for bind passwd\n"
93 " -x Simple authentication\n"
94 " -X id SASL authorization identity (\"dn:<dn>\" or \"u:<user>\")\n"
95 " -Y mech SASL mechanism\n"
96 " -Z Start TLS request (-ZZ to require successful response)\n"
103 main(int argc, char **argv)
105 char *infile, *entrydn = NULL, *rdn = NULL, buf[ 4096 ];
107 int rc, i, remove, havedn, authmethod, version, want_bindpw, debug, manageDSAit;
109 char *newSuperior=NULL;
112 not = contoper = verbose = remove = want_bindpw =
113 debug = manageDSAit = referrals = 0;
114 authmethod = LDAP_AUTH_SIMPLE;
117 prog = (prog = strrchr(argv[0], *LDAP_DIRSEP)) == NULL ? argv[0] : ++prog;
119 while (( i = getopt( argc, argv, "cf:rs:" "Cd:D:h:IkKMnO:p:P:QRU:vw:WxX:Y:Z" )) != EOF ) {
125 case 'f': /* read from file */
126 if( infile != NULL ) {
127 fprintf( stderr, "%s: -f previously specified\n" );
130 infile = strdup( optarg );
132 case 'r': /* remove old RDN */
135 case 's': /* newSuperior */
136 if( version == LDAP_VERSION2 ) {
137 fprintf( stderr, "%s: -X incompatible with LDAPv%d\n",
141 newSuperior = strdup( optarg );
142 version = LDAP_VERSION3;
150 debug |= atoi( optarg );
152 case 'D': /* bind DN */
153 if( binddn != NULL ) {
154 fprintf( stderr, "%s: -D previously specified\n" );
157 binddn = strdup( optarg );
159 case 'h': /* ldap host */
160 if( ldaphost != NULL ) {
161 fprintf( stderr, "%s: -h previously specified\n" );
164 ldaphost = strdup( optarg );
167 #ifdef HAVE_CYRUS_SASL
168 if( version == LDAP_VERSION2 ) {
169 fprintf( stderr, "%s: -I incompatible with version %d\n",
173 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
174 fprintf( stderr, "%s: incompatible previous "
175 "authentication choice\n",
179 authmethod = LDAP_AUTH_SASL;
180 version = LDAP_VERSION3;
181 sasl_flags = LDAP_SASL_INTERACTIVE;
184 fprintf( stderr, "%s: was not compiled with SASL support\n",
186 return( EXIT_FAILURE );
188 case 'k': /* kerberos bind */
189 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
190 if( version > LDAP_VERSION2 ) {
191 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
196 if( authmethod != -1 ) {
197 fprintf( stderr, "%s: -k incompatible with previous "
198 "authentication choice\n", prog );
202 authmethod = LDAP_AUTH_KRBV4;
204 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
208 case 'K': /* kerberos bind, part one only */
209 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
210 if( version > LDAP_VERSION2 ) {
211 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
215 if( authmethod != -1 ) {
216 fprintf( stderr, "%s: incompatible with previous "
217 "authentication choice\n", prog );
221 authmethod = LDAP_AUTH_KRBV41;
223 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
224 return( EXIT_FAILURE );
228 /* enable Manage DSA IT */
229 if( version == LDAP_VERSION2 ) {
230 fprintf( stderr, "%s: -M incompatible with LDAPv%d\n",
235 version = LDAP_VERSION3;
237 case 'n': /* print deletes, don't actually do them */
241 #ifdef HAVE_CYRUS_SASL
242 if( sasl_secprops != NULL ) {
243 fprintf( stderr, "%s: -O previously specified\n" );
246 if( version == LDAP_VERSION2 ) {
247 fprintf( stderr, "%s: -O incompatible with LDAPv%d\n",
251 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
252 fprintf( stderr, "%s: incompatible previous "
253 "authentication choice\n", prog );
256 authmethod = LDAP_AUTH_SASL;
257 version = LDAP_VERSION3;
258 sasl_secprops = strdup( optarg );
260 fprintf( stderr, "%s: not compiled with SASL support\n",
262 return( EXIT_FAILURE );
267 fprintf( stderr, "%s: -p previously specified\n" );
270 ldapport = atoi( optarg );
273 switch( atoi(optarg) ) {
275 if( version == LDAP_VERSION3 ) {
276 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
280 version = LDAP_VERSION2;
283 if( version == LDAP_VERSION2 ) {
284 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
288 version = LDAP_VERSION3;
291 fprintf( stderr, "%s: protocol version should be 2 or 3\n",
294 return( EXIT_FAILURE );
297 #ifdef HAVE_CYRUS_SASL
298 if( version == LDAP_VERSION2 ) {
299 fprintf( stderr, "%s: -Q incompatible with version %d\n",
303 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
304 fprintf( stderr, "%s: incompatible previous "
305 "authentication choice\n",
309 authmethod = LDAP_AUTH_SASL;
310 version = LDAP_VERSION3;
311 sasl_flags = LDAP_SASL_QUIET;
314 fprintf( stderr, "%s: not compiled with SASL support\n",
316 return( EXIT_FAILURE );
319 #ifdef HAVE_CYRUS_SASL
320 if( sasl_realm != NULL ) {
321 fprintf( stderr, "%s: -R previously specified\n" );
324 if( version == LDAP_VERSION2 ) {
325 fprintf( stderr, "%s: -R 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_realm = strdup( optarg );
339 fprintf( stderr, "%s: not compiled with SASL support\n",
341 return( EXIT_FAILURE );
345 #ifdef HAVE_CYRUS_SASL
346 if( sasl_authc_id != NULL ) {
347 fprintf( stderr, "%s: -U previously specified\n" );
350 if( version == LDAP_VERSION2 ) {
351 fprintf( stderr, "%s: -U 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_authc_id = strdup( optarg );
365 fprintf( stderr, "%s: not compiled with SASL support\n",
367 return( EXIT_FAILURE );
370 case 'v': /* verbose mode */
373 case 'w': /* password */
374 passwd.bv_val = strdup( optarg );
378 for( p = optarg; *p == '\0'; p++ ) {
382 passwd.bv_len = strlen( passwd.bv_val );
388 #ifdef HAVE_CYRUS_SASL
389 if( sasl_mech != NULL ) {
390 fprintf( stderr, "%s: -Y previously specified\n" );
393 if( version == LDAP_VERSION2 ) {
394 fprintf( stderr, "%s: -Y incompatible with version %d\n",
398 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
399 fprintf( stderr, "%s: incompatible with authentication choice\n", prog );
402 authmethod = LDAP_AUTH_SASL;
403 version = LDAP_VERSION3;
404 sasl_mech = strdup( optarg );
406 fprintf( stderr, "%s: not compiled with SASL support\n",
408 return( EXIT_FAILURE );
412 if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
413 fprintf( stderr, "%s: incompatible with previous "
414 "authentication choice\n", prog );
417 authmethod = LDAP_AUTH_SIMPLE;
420 #ifdef HAVE_CYRUS_SASL
421 if( sasl_authz_id != NULL ) {
422 fprintf( stderr, "%s: -X previously specified\n" );
425 if( version == LDAP_VERSION2 ) {
426 fprintf( stderr, "%s: -X incompatible with LDAPv%d\n",
430 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
431 fprintf( stderr, "%s: -X incompatible with "
432 "authentication choice\n", prog );
435 authmethod = LDAP_AUTH_SASL;
436 version = LDAP_VERSION3;
437 sasl_authz_id = strdup( optarg );
439 fprintf( stderr, "%s: not compiled with SASL support\n",
441 return( EXIT_FAILURE );
446 if( version == LDAP_VERSION2 ) {
447 fprintf( stderr, "%s: -Z incompatible with version %d\n",
451 version = LDAP_VERSION3;
454 fprintf( stderr, "%s: not compiled with TLS support\n",
456 return( EXIT_FAILURE );
460 fprintf( stderr, "%s: unrecongized option -%c\n",
463 return( EXIT_FAILURE );
468 version = LDAP_VERSION3;
470 if (authmethod == -1 && version > LDAP_VERSION2) {
471 #ifdef HAVE_CYRUS_SASL
472 authmethod = LDAP_AUTH_SASL;
474 authmethod = LDAP_AUTH_SIMPLE;
479 if (argc - optind == 2) {
480 if (( rdn = strdup( argv[argc - 1] )) == NULL ) {
482 return( EXIT_FAILURE );
484 if (( entrydn = strdup( argv[argc - 2] )) == NULL ) {
486 return( EXIT_FAILURE );
489 } else if ( argc - optind != 0 ) {
490 fprintf( stderr, "%s: invalid number of arguments (%d), "
491 "only two allowed\n", prog, argc-optind );
493 return( EXIT_FAILURE );
496 if ( infile != NULL ) {
497 if (( fp = fopen( infile, "r" )) == NULL ) {
499 return( EXIT_FAILURE );
506 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
507 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
509 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
510 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
515 (void) SIGNAL( SIGPIPE, SIG_IGN );
518 if (( ld = ldap_init( ldaphost, ldapport )) == NULL ) {
519 perror( "ldap_init" );
520 return( EXIT_FAILURE );
524 if( ldap_set_option( ld, LDAP_OPT_REFERRALS,
525 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
527 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
528 referrals ? "on" : "off" );
532 if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version )
533 != LDAP_OPT_SUCCESS )
535 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
540 if ( use_tls && ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS ) {
542 ldap_perror( ld, "ldap_start_tls" );
543 return( EXIT_FAILURE );
545 fprintf( stderr, "WARNING: could not start TLS\n" );
549 passwd.bv_val = getpassphrase("Enter LDAP Password: ");
550 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
553 if ( authmethod == LDAP_AUTH_SASL ) {
554 #ifdef HAVE_CYRUS_SASL
557 if( sasl_secprops != NULL ) {
558 rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
559 (void *) sasl_secprops );
561 if( rc != LDAP_OPT_SUCCESS ) {
563 "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
565 return( EXIT_FAILURE );
569 defaults = lutil_sasl_defaults( ld,
576 rc = ldap_sasl_interactive_bind_s( ld, binddn,
577 sasl_mech, NULL, NULL,
578 sasl_flags, lutil_sasl_interact, defaults );
580 if( rc != LDAP_SUCCESS ) {
581 ldap_perror( ld, "ldap_sasl_interactive_bind_s" );
582 return( EXIT_FAILURE );
585 fprintf( stderr, "%s: not compiled with SASL support\n",
587 return( EXIT_FAILURE );
591 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
593 ldap_perror( ld, "ldap_bind" );
594 return( EXIT_FAILURE );
601 LDAPControl *ctrls[2];
605 c.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
606 c.ldctl_value.bv_val = NULL;
607 c.ldctl_value.bv_len = 0;
608 c.ldctl_iscritical = manageDSAit > 1;
610 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, &ctrls );
612 if( err != LDAP_OPT_SUCCESS ) {
613 fprintf( stderr, "Could not set ManageDSAit %scontrol\n",
614 c.ldctl_iscritical ? "critical " : "" );
615 if( c.ldctl_iscritical ) {
616 exit( EXIT_FAILURE );
623 rc = domodrdn( ld, entrydn, rdn, newSuperior, remove );
624 else while ((rc == 0 || contoper) && fgets(buf, sizeof(buf), fp) != NULL) {
625 if ( *buf != '\0' ) { /* blank lines optional, skip */
626 buf[ strlen( buf ) - 1 ] = '\0'; /* remove nl */
628 if ( havedn ) { /* have DN, get RDN */
629 if (( rdn = strdup( buf )) == NULL ) {
631 return( EXIT_FAILURE );
633 rc = domodrdn(ld, entrydn, rdn, newSuperior, remove );
635 } else if ( !havedn ) { /* don't have DN yet */
636 if (( entrydn = strdup( buf )) == NULL ) {
638 return( EXIT_FAILURE );
656 int remove ) /* flag: remove old RDN */
659 char *matcheddn=NULL, *text=NULL, **refs=NULL;
663 printf( "Renaming \"%s\"\n", dn );
664 printf( "\tnew rdn=\"%s\" (%s old rdn)\n",
665 rdn, remove ? "delete" : "keep" );
666 if( newSuperior != NULL ) {
667 printf("\tnew parent=\"%s\"\n", newSuperior);
671 if( not ) return LDAP_SUCCESS;
673 rc = ldap_rename( ld, dn, rdn, newSuperior, remove,
676 if ( rc != LDAP_SUCCESS ) {
677 fprintf( stderr, "%s: ldap_rename: %s (%d)\n",
678 prog, ldap_err2string( rc ), rc );
682 rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, NULL, &res );
684 ldap_perror( ld, "ldapmodrdn: ldap_result" );
688 rc = ldap_parse_result( ld, res, &code, &matcheddn, &text, &refs, NULL, 1 );
690 if( rc != LDAP_SUCCESS ) {
691 fprintf( stderr, "%s: ldap_parse_result: %s (%d)\n",
692 prog, ldap_err2string( rc ), rc );
696 if( verbose || code != LDAP_SUCCESS ||
697 (matcheddn && *matcheddn) || (text && *text) || (refs && *refs) )
699 printf( "Rename Result: %s (%d)\n",
700 ldap_err2string( code ), code );
702 if( text && *text ) {
703 printf( "Additional info: %s\n", text );
706 if( matcheddn && *matcheddn ) {
707 printf( "Matched DN: %s\n", matcheddn );
712 for( i=0; refs[i]; i++ ) {
713 printf("Referral: %s\n", refs[i] );
719 ber_memfree( matcheddn );
720 ber_memvfree( (void **) refs );