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 *binddn = NULL;
36 static struct berval passwd = { 0, NULL };
37 static char *ldaphost = NULL;
38 static int ldapport = 0;
39 #ifdef HAVE_CYRUS_SASL
40 static char *sasl_authc_id = NULL;
41 static char *sasl_authz_id = NULL;
42 static char *sasl_mech = NULL;
43 static char *sasl_secprops = NULL;
45 static int use_tls = 0;
46 static int not, verbose, contoper;
54 int remove ); /* flag: remove old RDN */
57 usage( const char *s )
60 "Rename LDAP entries\n\n"
61 "usage: %s [options] [dn rdn]\n"
62 " dn rdn: If given, rdn will replace the RDN of the entry specified by DN\n"
63 " If not given, the list of modifications is read from stdin or\n"
64 " from the file specified by \"-f file\" (see man page).\n"
66 " -r\t\tremove old RDN\n"
67 " -s newsuperior\tnew superior entry\n"
70 " -c\t\tcontinuous operation mode (do not stop on errors)\n"
71 " -C\t\tchase referrals\n"
72 " -d level\tset LDAP debugging level to `level'\n"
73 " -D binddn\tbind DN\n"
74 " -f file\t\tread operations from `file'\n"
75 " -h host\t\tLDAP server\n"
76 " -k\t\tuse Kerberos authentication\n"
77 " -K\t\tlike -k, but do only step 1 of the Kerberos bind\n"
78 " -M\t\tenable Manage DSA IT control (-MM to make it critical)\n"
79 " -n\t\tshow what would be done but don't actually do it\n"
80 " -O secprops\tSASL security properties\n"
81 " -p port\t\tport on LDAP server\n"
82 " -P version\tprocotol version (default: 3)\n"
83 " -U user\t\tSASL authentication identity (username)\n"
84 " -v\t\trun in verbose mode (diagnostics to standard output)\n"
85 " -w passwd\tbind passwd (for simple authentication)\n"
86 " -W\t\tprompt for bind passwd\n"
87 " -X id\t\tSASL authorization identity (\"dn:<dn>\" or \"u:<user>\")\n"
88 " -Y mech\t\tSASL mechanism\n"
89 " -Z\t\tissue Start TLS request (-ZZ to require successful response)\n"
96 main(int argc, char **argv)
98 char *prog,*infile, *entrydn = NULL, *rdn = NULL, buf[ 4096 ];
100 int rc, i, remove, havedn, authmethod, version, want_bindpw, debug, manageDSAit;
102 char *newSuperior=NULL;
105 not = contoper = verbose = remove = want_bindpw =
106 debug = manageDSAit = referrals = 0;
107 authmethod = LDAP_AUTH_SIMPLE;
110 prog = (prog = strrchr(argv[0], *LDAP_DIRSEP)) == NULL ? argv[0] : ++prog;
112 while (( i = getopt( argc, argv, "rs:" "cCd:D:f:h:kKMnO:p:P:U:vw:WxX:Y:Z" )) != EOF ) {
115 case 's': /* newSuperior */
116 if( version == LDAP_VERSION2 ) {
117 fprintf( stderr, "%s: -X incompatible with LDAPv%d\n",
121 newSuperior = strdup( optarg );
122 version = LDAP_VERSION3;
124 case 'r': /* remove old RDN */
133 debug |= atoi( optarg );
135 case 'D': /* bind DN */
136 binddn = strdup( optarg );
138 case 'h': /* ldap host */
139 ldaphost = strdup( optarg );
141 case 'k': /* kerberos bind */
142 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
143 if( version > LDAP_VERSION2 ) {
144 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
149 if( authmethod != -1 ) {
150 fprintf( stderr, "%s: -k incompatible with previous "
151 "authentication choice\n", prog );
155 authmethod = LDAP_AUTH_KRBV4;
157 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
161 case 'K': /* kerberos bind, part one only */
162 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
163 if( version > LDAP_VERSION2 ) {
164 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
168 if( authmethod != -1 ) {
169 fprintf( stderr, "%s: incompatible with previous "
170 "authentication choice\n", prog );
174 authmethod = LDAP_AUTH_KRBV41;
176 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
177 return( EXIT_FAILURE );
181 /* enable Manage DSA IT */
182 if( version == LDAP_VERSION2 ) {
183 fprintf( stderr, "%s: -M incompatible with LDAPv%d\n",
188 version = LDAP_VERSION3;
190 case 'n': /* print deletes, don't actually do them */
194 #ifdef HAVE_CYRUS_SASL
195 if( version == LDAP_VERSION2 ) {
196 fprintf( stderr, "%s -O incompatible with LDAPv%d\n",
200 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
201 fprintf( stderr, "%s: incompatible previous "
202 "authentication choice\n", prog );
205 sasl_secprops = strdup( optarg );
206 authmethod = LDAP_AUTH_SASL;
207 version = LDAP_VERSION3;
209 fprintf( stderr, "%s: not compiled with SASL support\n",
211 return( EXIT_FAILURE );
215 ldapport = atoi( optarg );
218 switch( atoi(optarg) ) {
220 if( version == LDAP_VERSION3 ) {
221 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
225 version = LDAP_VERSION2;
228 if( version == LDAP_VERSION2 ) {
229 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
233 version = LDAP_VERSION3;
236 fprintf( stderr, "%s: protocol version should be 2 or 3\n",
239 return( EXIT_FAILURE );
242 #ifdef HAVE_CYRUS_SASL
243 if( version == LDAP_VERSION2 ) {
244 fprintf( stderr, "%s: -U incompatible with version %d\n",
248 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
249 fprintf( stderr, "%s: incompatible previous "
250 "authentication choice\n",
254 authmethod = LDAP_AUTH_SASL;
255 version = LDAP_VERSION3;
257 sasl_authc_id = strdup( optarg );
258 authmethod = LDAP_AUTH_SASL;
260 fprintf( stderr, "%s: was not compiled with SASL support\n",
262 return( EXIT_FAILURE );
265 case 'v': /* verbose mode */
268 case 'w': /* password */
269 passwd.bv_val = strdup( optarg );
273 for( p = optarg; *p == '\0'; p++ ) {
277 passwd.bv_len = strlen( passwd.bv_val );
283 #ifdef HAVE_CYRUS_SASL
284 if( version == LDAP_VERSION2 ) {
285 fprintf( stderr, "%s: -Y incompatible with version %d\n",
289 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
290 fprintf( stderr, "%s: incompatible with authentication choice\n", prog );
294 authmethod = LDAP_AUTH_SASL;
295 version = LDAP_VERSION3;
297 fprintf( stderr, "%s: was not compiled with SASL support\n",
299 return( EXIT_FAILURE );
303 if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
304 fprintf( stderr, "%s: incompatible with previous "
305 "authentication choice\n", prog );
308 authmethod = LDAP_AUTH_SIMPLE;
311 #ifdef HAVE_CYRUS_SASL
312 if( version == LDAP_VERSION2 ) {
313 fprintf( stderr, "%s: -X incompatible with LDAPv%d\n",
317 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
318 fprintf( stderr, "%s: -X incompatible with "
319 "authentication choice\n", prog );
322 authmethod = LDAP_AUTH_SASL;
323 version = LDAP_VERSION3;
325 sasl_authz_id = strdup( optarg );
326 authmethod = LDAP_AUTH_SASL;
328 fprintf( stderr, "%s: not compiled with SASL support\n",
330 return( EXIT_FAILURE );
335 if( version == LDAP_VERSION2 ) {
336 fprintf( stderr, "%s -Z incompatible with version %d\n",
340 version = LDAP_VERSION3;
343 fprintf( stderr, "%s: not compiled with TLS support\n",
345 return( EXIT_FAILURE );
350 return( EXIT_FAILURE );
355 version = LDAP_VERSION3;
357 if (authmethod == -1 && version > LDAP_VERSION2) {
358 authmethod = LDAP_AUTH_SASL;
362 if (argc - optind == 2) {
363 if (( rdn = strdup( argv[argc - 1] )) == NULL ) {
365 return( EXIT_FAILURE );
367 if (( entrydn = strdup( argv[argc - 2] )) == NULL ) {
369 return( EXIT_FAILURE );
372 } else if ( argc - optind != 0 ) {
373 fprintf( stderr, "%s: invalid number of arguments, only two allowed\n", prog);
375 return( EXIT_FAILURE );
378 if ( infile != NULL ) {
379 if (( fp = fopen( infile, "r" )) == NULL ) {
381 return( EXIT_FAILURE );
388 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
389 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
391 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
392 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
397 (void) SIGNAL( SIGPIPE, SIG_IGN );
400 if (( ld = ldap_init( ldaphost, ldapport )) == NULL ) {
401 perror( "ldap_init" );
402 return( EXIT_FAILURE );
406 if( ldap_set_option( ld, LDAP_OPT_REFERRALS,
407 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
409 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
410 referrals ? "on" : "off" );
414 if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version )
415 != LDAP_OPT_SUCCESS )
417 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
422 if ( use_tls && ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS ) {
424 ldap_perror( ld, "ldap_start_tls" );
425 return( EXIT_FAILURE );
427 fprintf( stderr, "WARNING: could not start TLS\n" );
431 passwd.bv_val = getpassphrase("Enter LDAP Password: ");
432 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
435 if ( authmethod == LDAP_AUTH_SASL ) {
436 #ifdef HAVE_CYRUS_SASL
437 ldap_set_sasl_interact_proc( ld, lutil_sasl_interact );
439 if( sasl_secprops != NULL ) {
440 rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
441 (void *) sasl_secprops );
443 if( rc != LDAP_OPT_SUCCESS ) {
445 "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
447 return( EXIT_FAILURE );
451 rc = ldap_sasl_interactive_bind_s( ld, binddn,
452 sasl_mech, NULL, NULL );
454 if( rc != LDAP_SUCCESS ) {
455 ldap_perror( ld, "ldap_sasl_interactive_bind_s" );
456 return( EXIT_FAILURE );
459 fprintf( stderr, "%s was not compiled with SASL support\n",
461 return( EXIT_FAILURE );
465 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
467 ldap_perror( ld, "ldap_bind" );
468 return( EXIT_FAILURE );
475 LDAPControl *ctrls[2];
479 c.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
480 c.ldctl_value.bv_val = NULL;
481 c.ldctl_value.bv_len = 0;
482 c.ldctl_iscritical = manageDSAit > 1;
484 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, &ctrls );
486 if( err != LDAP_OPT_SUCCESS ) {
487 fprintf( stderr, "Could not set ManageDSAit %scontrol\n",
488 c.ldctl_iscritical ? "critical " : "" );
489 if( c.ldctl_iscritical ) {
490 exit( EXIT_FAILURE );
497 rc = domodrdn( ld, entrydn, rdn, newSuperior, remove );
498 else while ((rc == 0 || contoper) && fgets(buf, sizeof(buf), fp) != NULL) {
499 if ( *buf != '\0' ) { /* blank lines optional, skip */
500 buf[ strlen( buf ) - 1 ] = '\0'; /* remove nl */
502 if ( havedn ) { /* have DN, get RDN */
503 if (( rdn = strdup( buf )) == NULL ) {
505 return( EXIT_FAILURE );
507 rc = domodrdn(ld, entrydn, rdn, newSuperior, remove );
509 } else if ( !havedn ) { /* don't have DN yet */
510 if (( entrydn = strdup( buf )) == NULL ) {
512 return( EXIT_FAILURE );
530 int remove ) /* flag: remove old RDN */
533 char *matcheddn=NULL, *text=NULL, **refs=NULL;
537 printf( "Renaming \"%s\"\n", dn );
538 printf( "\tnew rdn=\"%s\" (%s old rdn)\n",
539 rdn, remove ? "delete" : "keep" );
540 if( newSuperior != NULL ) {
541 printf("\tnew parent=\"%s\"\n", newSuperior);
545 if( not ) return LDAP_SUCCESS;
547 rc = ldap_rename( ld, dn, rdn, newSuperior, remove,
550 if ( rc != LDAP_SUCCESS ) {
551 fprintf( stderr, "ldapmodrdn: ldap_rename: %s (%d)\n",
552 ldap_err2string( rc ), rc );
556 rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, NULL, &res );
558 ldap_perror( ld, "ldapmodrdn: ldap_result" );
562 rc = ldap_parse_result( ld, res, &code, &matcheddn, &text, &refs, NULL, 1 );
564 if( rc != LDAP_SUCCESS ) {
565 fprintf( stderr, "ldapmodrdn: ldap_parse_result: %s (%d)\n",
566 ldap_err2string( rc ), rc );
570 if( verbose || code != LDAP_SUCCESS ||
571 (matcheddn && *matcheddn) || (text && *text) || (refs && *refs) )
573 printf( "Rename Result: %s (%d)\n",
574 ldap_err2string( code ), code );
576 if( text && *text ) {
577 printf( "Additional info: %s\n", text );
580 if( matcheddn && *matcheddn ) {
581 printf( "Matched DN: %s\n", matcheddn );
586 for( i=0; refs[i]; i++ ) {
587 printf("Referral: %s\n", refs[i] );
593 ber_memfree( matcheddn );
594 ber_memvfree( (void **) refs );