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 " -c\t\tcontinuous operation mode (do not stop on errors)\n"
67 " -f file\t\tread operations from `file'\n"
68 " -r\t\tremove old RDN\n"
69 " -s newsuperior\tnew superior entry\n"
72 " -C\t\tchase referrals\n"
73 " -d level\tset LDAP debugging level to `level'\n"
74 " -D binddn\tbind DN\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, "cf:rs:" "Cd:D:h:kKMnO:p:P:U:vw:WxX:Y:Z" )) != EOF ) {
118 case 's': /* newSuperior */
119 if( version == LDAP_VERSION2 ) {
120 fprintf( stderr, "%s: -X incompatible with LDAPv%d\n",
124 newSuperior = strdup( optarg );
125 version = LDAP_VERSION3;
127 case 'r': /* remove old RDN */
136 debug |= atoi( optarg );
138 case 'D': /* bind DN */
139 binddn = strdup( optarg );
141 case 'h': /* ldap host */
142 ldaphost = strdup( optarg );
144 case 'k': /* kerberos bind */
145 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
146 if( version > LDAP_VERSION2 ) {
147 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
152 if( authmethod != -1 ) {
153 fprintf( stderr, "%s: -k incompatible with previous "
154 "authentication choice\n", prog );
158 authmethod = LDAP_AUTH_KRBV4;
160 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
164 case 'K': /* kerberos bind, part one only */
165 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
166 if( version > LDAP_VERSION2 ) {
167 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
171 if( authmethod != -1 ) {
172 fprintf( stderr, "%s: incompatible with previous "
173 "authentication choice\n", prog );
177 authmethod = LDAP_AUTH_KRBV41;
179 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
180 return( EXIT_FAILURE );
184 /* enable Manage DSA IT */
185 if( version == LDAP_VERSION2 ) {
186 fprintf( stderr, "%s: -M incompatible with LDAPv%d\n",
191 version = LDAP_VERSION3;
193 case 'n': /* print deletes, don't actually do them */
197 #ifdef HAVE_CYRUS_SASL
198 if( version == LDAP_VERSION2 ) {
199 fprintf( stderr, "%s -O incompatible with LDAPv%d\n",
203 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
204 fprintf( stderr, "%s: incompatible previous "
205 "authentication choice\n", prog );
208 sasl_secprops = strdup( optarg );
209 authmethod = LDAP_AUTH_SASL;
210 version = LDAP_VERSION3;
212 fprintf( stderr, "%s: not compiled with SASL support\n",
214 return( EXIT_FAILURE );
218 ldapport = atoi( optarg );
221 switch( atoi(optarg) ) {
223 if( version == LDAP_VERSION3 ) {
224 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
228 version = LDAP_VERSION2;
231 if( version == LDAP_VERSION2 ) {
232 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
236 version = LDAP_VERSION3;
239 fprintf( stderr, "%s: protocol version should be 2 or 3\n",
242 return( EXIT_FAILURE );
245 #ifdef HAVE_CYRUS_SASL
246 if( version == LDAP_VERSION2 ) {
247 fprintf( stderr, "%s: -U incompatible with version %d\n",
251 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
252 fprintf( stderr, "%s: incompatible previous "
253 "authentication choice\n",
257 authmethod = LDAP_AUTH_SASL;
258 version = LDAP_VERSION3;
260 sasl_authc_id = strdup( optarg );
261 authmethod = LDAP_AUTH_SASL;
263 fprintf( stderr, "%s: was not compiled with SASL support\n",
265 return( EXIT_FAILURE );
268 case 'v': /* verbose mode */
271 case 'w': /* password */
272 passwd.bv_val = strdup( optarg );
276 for( p = optarg; *p == '\0'; p++ ) {
280 passwd.bv_len = strlen( passwd.bv_val );
286 #ifdef HAVE_CYRUS_SASL
287 if( version == LDAP_VERSION2 ) {
288 fprintf( stderr, "%s: -Y incompatible with version %d\n",
292 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
293 fprintf( stderr, "%s: incompatible with authentication choice\n", prog );
297 authmethod = LDAP_AUTH_SASL;
298 version = LDAP_VERSION3;
300 fprintf( stderr, "%s: was not compiled with SASL support\n",
302 return( EXIT_FAILURE );
306 if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
307 fprintf( stderr, "%s: incompatible with previous "
308 "authentication choice\n", prog );
311 authmethod = LDAP_AUTH_SIMPLE;
314 #ifdef HAVE_CYRUS_SASL
315 if( version == LDAP_VERSION2 ) {
316 fprintf( stderr, "%s: -X incompatible with LDAPv%d\n",
320 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
321 fprintf( stderr, "%s: -X incompatible with "
322 "authentication choice\n", prog );
325 authmethod = LDAP_AUTH_SASL;
326 version = LDAP_VERSION3;
328 sasl_authz_id = strdup( optarg );
329 authmethod = LDAP_AUTH_SASL;
331 fprintf( stderr, "%s: not compiled with SASL support\n",
333 return( EXIT_FAILURE );
338 if( version == LDAP_VERSION2 ) {
339 fprintf( stderr, "%s -Z incompatible with version %d\n",
343 version = LDAP_VERSION3;
346 fprintf( stderr, "%s: not compiled with TLS support\n",
348 return( EXIT_FAILURE );
352 fprintf( stderr, "%s: unrecongized option -%c\n",
355 return( EXIT_FAILURE );
360 version = LDAP_VERSION3;
362 if (authmethod == -1 && version > LDAP_VERSION2) {
363 #ifdef HAVE_CYRUS_SASL
364 authmethod = LDAP_AUTH_SASL;
366 authmethod = LDAP_AUTH_SIMPLE;
371 if (argc - optind == 2) {
372 if (( rdn = strdup( argv[argc - 1] )) == NULL ) {
374 return( EXIT_FAILURE );
376 if (( entrydn = strdup( argv[argc - 2] )) == NULL ) {
378 return( EXIT_FAILURE );
381 } else if ( argc - optind != 0 ) {
382 fprintf( stderr, "%s: invalid number of arguments (%d), "
383 "only two allowed\n", prog, argc-optind );
385 return( EXIT_FAILURE );
388 if ( infile != NULL ) {
389 if (( fp = fopen( infile, "r" )) == NULL ) {
391 return( EXIT_FAILURE );
398 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
399 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
401 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
402 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
407 (void) SIGNAL( SIGPIPE, SIG_IGN );
410 if (( ld = ldap_init( ldaphost, ldapport )) == NULL ) {
411 perror( "ldap_init" );
412 return( EXIT_FAILURE );
416 if( ldap_set_option( ld, LDAP_OPT_REFERRALS,
417 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
419 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
420 referrals ? "on" : "off" );
424 if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version )
425 != LDAP_OPT_SUCCESS )
427 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
432 if ( use_tls && ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS ) {
434 ldap_perror( ld, "ldap_start_tls" );
435 return( EXIT_FAILURE );
437 fprintf( stderr, "WARNING: could not start TLS\n" );
441 passwd.bv_val = getpassphrase("Enter LDAP Password: ");
442 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
445 if ( authmethod == LDAP_AUTH_SASL ) {
446 #ifdef HAVE_CYRUS_SASL
447 if( sasl_secprops != NULL ) {
448 rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
449 (void *) sasl_secprops );
451 if( rc != LDAP_OPT_SUCCESS ) {
453 "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
455 return( EXIT_FAILURE );
459 rc = ldap_sasl_interactive_bind_s( ld, binddn,
460 sasl_mech, NULL, NULL, lutil_sasl_interact );
462 if( rc != LDAP_SUCCESS ) {
463 ldap_perror( ld, "ldap_sasl_interactive_bind_s" );
464 return( EXIT_FAILURE );
467 fprintf( stderr, "%s was not compiled with SASL support\n",
469 return( EXIT_FAILURE );
473 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
475 ldap_perror( ld, "ldap_bind" );
476 return( EXIT_FAILURE );
483 LDAPControl *ctrls[2];
487 c.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
488 c.ldctl_value.bv_val = NULL;
489 c.ldctl_value.bv_len = 0;
490 c.ldctl_iscritical = manageDSAit > 1;
492 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, &ctrls );
494 if( err != LDAP_OPT_SUCCESS ) {
495 fprintf( stderr, "Could not set ManageDSAit %scontrol\n",
496 c.ldctl_iscritical ? "critical " : "" );
497 if( c.ldctl_iscritical ) {
498 exit( EXIT_FAILURE );
505 rc = domodrdn( ld, entrydn, rdn, newSuperior, remove );
506 else while ((rc == 0 || contoper) && fgets(buf, sizeof(buf), fp) != NULL) {
507 if ( *buf != '\0' ) { /* blank lines optional, skip */
508 buf[ strlen( buf ) - 1 ] = '\0'; /* remove nl */
510 if ( havedn ) { /* have DN, get RDN */
511 if (( rdn = strdup( buf )) == NULL ) {
513 return( EXIT_FAILURE );
515 rc = domodrdn(ld, entrydn, rdn, newSuperior, remove );
517 } else if ( !havedn ) { /* don't have DN yet */
518 if (( entrydn = strdup( buf )) == NULL ) {
520 return( EXIT_FAILURE );
538 int remove ) /* flag: remove old RDN */
541 char *matcheddn=NULL, *text=NULL, **refs=NULL;
545 printf( "Renaming \"%s\"\n", dn );
546 printf( "\tnew rdn=\"%s\" (%s old rdn)\n",
547 rdn, remove ? "delete" : "keep" );
548 if( newSuperior != NULL ) {
549 printf("\tnew parent=\"%s\"\n", newSuperior);
553 if( not ) return LDAP_SUCCESS;
555 rc = ldap_rename( ld, dn, rdn, newSuperior, remove,
558 if ( rc != LDAP_SUCCESS ) {
559 fprintf( stderr, "ldapmodrdn: ldap_rename: %s (%d)\n",
560 ldap_err2string( rc ), rc );
564 rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, NULL, &res );
566 ldap_perror( ld, "ldapmodrdn: ldap_result" );
570 rc = ldap_parse_result( ld, res, &code, &matcheddn, &text, &refs, NULL, 1 );
572 if( rc != LDAP_SUCCESS ) {
573 fprintf( stderr, "ldapmodrdn: ldap_parse_result: %s (%d)\n",
574 ldap_err2string( rc ), rc );
578 if( verbose || code != LDAP_SUCCESS ||
579 (matcheddn && *matcheddn) || (text && *text) || (refs && *refs) )
581 printf( "Rename Result: %s (%d)\n",
582 ldap_err2string( code ), code );
584 if( text && *text ) {
585 printf( "Additional info: %s\n", text );
588 if( matcheddn && *matcheddn ) {
589 printf( "Matched DN: %s\n", matcheddn );
594 for( i=0; refs[i]; i++ ) {
595 printf("Referral: %s\n", refs[i] );
601 ber_memfree( matcheddn );
602 ber_memvfree( (void **) refs );