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>
33 static char *binddn = NULL;
34 static struct berval passwd = { 0, NULL};
35 static char *ldaphost = NULL;
36 static int ldapport = 0;
37 #ifdef HAVE_CYRUS_SASL
38 static char *sasl_authc_id = NULL;
39 static char *sasl_authz_id = NULL;
40 static char *sasl_mech = NULL;
41 static int sasl_integrity = 0;
42 static int sasl_privacy = 0;
44 static int use_tls = 0;
45 static int not, verbose, contoper;
53 int remove ); /* flag: remove old RDN */
56 usage( const char *s )
59 "Rename LDAP entries\n\n"
60 "usage: %s [options] [dn rdn]\n"
61 " dn rdn: If given, rdn will replace the RDN of the entry specified by DN\n"
62 " If not given, the list of modifications is read from stdin or\n"
63 " from the file specified by \"-f file\" (see man page).\n"
65 " -c\t\tcontinuous operation mode (do not stop on errors)\n"
66 " -C\t\tchase referrals\n"
67 " -d level\tset LDAP debugging level to `level'\n"
68 " -D binddn\tbind DN\n"
69 " -E\t\trequest SASL privacy (-EE to make it critical)\n"
70 " -f file\t\tdo renames listed in `file'\n"
71 " -h host\t\tLDAP server\n"
72 " -I\t\trequest SASL integrity checking (-II to make it\n"
74 " -k\t\tuse Kerberos authentication\n"
75 " -K\t\tlike -k, but do only step 1 of the Kerberos bind\n"
76 " -M\t\tenable Manage DSA IT control (-MM to make it critical)\n"
77 " -n\t\tshow what would be done but don't actually do it\n"
78 " -p port\t\tport on LDAP server\n"
79 " -P version\tprocotol version (default: 3)\n"
80 " -r\t\tremove old RDN\n"
81 " -s newsuperior\tnew superior entry\n"
82 " -U user\t\tSASL authentication identity (username)\n"
83 " -v\t\trun in verbose mode (diagnostics to standard output)\n"
84 " -w passwd\tbind passwd (for simple authentication)\n"
85 " -W\t\tprompt for bind passwd\n"
86 " -X id\t\tSASL authorization identity (\"dn:<dn>\" or \"u:<user>\")\n"
87 " -Y mech\t\tSASL mechanism\n"
88 " -Z\t\tissue Start TLS request (-ZZ to require successful response)\n"
95 main(int argc, char **argv)
97 char *myname,*infile, *entrydn = NULL, *rdn = NULL, buf[ 4096 ];
99 int rc, i, remove, havedn, authmethod, version, want_bindpw, debug, manageDSAit;
101 char *newSuperior=NULL;
104 not = contoper = verbose = remove = want_bindpw =
105 debug = manageDSAit = referrals = 0;
106 authmethod = LDAP_AUTH_SIMPLE;
109 myname = (myname = strrchr(argv[0], '/')) == NULL ? argv[0] : ++myname;
111 while (( i = getopt( argc, argv, "cCD:d:Ef:h:IKkMnP:p:rs:U:vWw:X:Y:Z" )) != EOF ) {
113 case 'k': /* kerberos bind */
114 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
115 authmethod = LDAP_AUTH_KRBV4;
117 fprintf( stderr, "%s was not compiled with Kerberos support\n", argv[0] );
118 return( EXIT_FAILURE );
121 case 'K': /* kerberos bind, part one only */
122 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
123 authmethod = LDAP_AUTH_KRBV41;
125 fprintf( stderr, "%s was not compiled with Kerberos support\n", argv[0] );
126 return( EXIT_FAILURE );
129 case 'c': /* continuous operation mode */
135 case 'h': /* ldap host */
136 ldaphost = strdup( optarg );
138 case 'D': /* bind DN */
139 binddn = strdup( optarg );
141 case 's': /* newSuperior */
142 newSuperior = strdup( optarg );
143 version = LDAP_VERSION3; /* This option => force V3 */
145 case 'w': /* password */
146 passwd.bv_val = strdup( optarg );
150 for( p = optarg; *p == '\0'; p++ ) {
154 passwd.bv_len = strlen( passwd.bv_val );
157 debug |= atoi( optarg );
159 case 'f': /* read from file */
160 infile = strdup( optarg );
163 ldapport = atoi( optarg );
165 case 'n': /* print adds, don't actually do them */
168 case 'v': /* verbose mode */
171 case 'r': /* remove old RDN */
175 /* enable Manage DSA IT */
182 switch( atoi(optarg) )
185 version = LDAP_VERSION2;
188 version = LDAP_VERSION3;
191 fprintf( stderr, "protocol version should be 2 or 3\n" );
193 return( EXIT_FAILURE );
197 #ifdef HAVE_CYRUS_SASL
199 authmethod = LDAP_AUTH_SASL;
201 fprintf( stderr, "%s was not compiled with SASL support\n",
203 return( EXIT_FAILURE );
207 #ifdef HAVE_CYRUS_SASL
209 authmethod = LDAP_AUTH_SASL;
211 fprintf( stderr, "%s was not compiled with SASL support\n",
213 return( EXIT_FAILURE );
217 #ifdef HAVE_CYRUS_SASL
218 if ( strcasecmp( optarg, "any" ) && strcmp( optarg, "*" ) ) {
219 sasl_mech = strdup( optarg );
221 authmethod = LDAP_AUTH_SASL;
223 fprintf( stderr, "%s was not compiled with SASL support\n",
225 return( EXIT_FAILURE );
229 #ifdef HAVE_CYRUS_SASL
230 sasl_authc_id = strdup( optarg );
231 authmethod = LDAP_AUTH_SASL;
233 fprintf( stderr, "%s was not compiled with SASL support\n",
235 return( EXIT_FAILURE );
239 #ifdef HAVE_CYRUS_SASL
240 sasl_authz_id = strdup( optarg );
241 authmethod = LDAP_AUTH_SASL;
243 fprintf( stderr, "%s was not compiled with SASL support\n",
245 return( EXIT_FAILURE );
252 fprintf( stderr, "%s was not compiled with TLS support\n",
254 return( EXIT_FAILURE );
259 return( EXIT_FAILURE );
263 if ( ( authmethod == LDAP_AUTH_KRBV4 ) || ( authmethod ==
264 LDAP_AUTH_KRBV41 ) ) {
265 if( version > LDAP_VERSION2 ) {
266 fprintf( stderr, "Kerberos requires LDAPv2\n" );
267 return( EXIT_FAILURE );
269 version = LDAP_VERSION2;
271 else if ( authmethod == LDAP_AUTH_SASL ) {
272 if( version != -1 && version != LDAP_VERSION3 ) {
273 fprintf( stderr, "SASL requires LDAPv3\n" );
274 return( EXIT_FAILURE );
276 version = LDAP_VERSION3;
280 if( version != -1 && version != LDAP_VERSION3 ) {
281 fprintf(stderr, "manage DSA control requires LDAPv3\n");
284 version = LDAP_VERSION3;
288 if( version != -1 && version != LDAP_VERSION3 ) {
289 fprintf(stderr, "Start TLS requires LDAPv3\n");
292 version = LDAP_VERSION3;
295 if (newSuperior != NULL) {
296 if (version == LDAP_VERSION2) {
298 "%s: version conflict!, -s newSuperior requires LDAPv3\n",
301 return( EXIT_FAILURE );
303 version = LDAP_VERSION3;
307 if (argc - optind == 2) {
308 if (( rdn = strdup( argv[argc - 1] )) == NULL ) {
310 return( EXIT_FAILURE );
312 if (( entrydn = strdup( argv[argc - 2] )) == NULL ) {
314 return( EXIT_FAILURE );
317 } else if ( argc - optind != 0 ) {
318 fprintf( stderr, "%s: invalid number of arguments, only two allowed\n", myname);
320 return( EXIT_FAILURE );
323 if ( infile != NULL ) {
324 if (( fp = fopen( infile, "r" )) == NULL ) {
326 return( EXIT_FAILURE );
333 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
334 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
336 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
337 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
342 (void) SIGNAL( SIGPIPE, SIG_IGN );
345 if (( ld = ldap_init( ldaphost, ldapport )) == NULL ) {
346 perror( "ldap_init" );
347 return( EXIT_FAILURE );
351 if( ldap_set_option( ld, LDAP_OPT_REFERRALS,
352 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
354 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
355 referrals ? "on" : "off" );
359 if (version == -1 ) {
363 if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version )
364 != LDAP_OPT_SUCCESS )
366 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
371 if ( use_tls && ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS ) {
373 ldap_perror( ld, "ldap_start_tls" );
374 return( EXIT_FAILURE );
376 fprintf( stderr, "WARNING: could not start TLS\n" );
380 passwd.bv_val = getpassphrase("Enter LDAP Password: ");
381 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
384 if ( authmethod == LDAP_AUTH_SASL ) {
385 #ifdef HAVE_CYRUS_SASL
386 int minssf = 0, maxssf = 0;
388 if ( sasl_integrity > 0 )
390 if ( sasl_integrity > 1 )
392 if ( sasl_privacy > 0 )
393 maxssf = 100000; /* Something big value */
394 if ( sasl_privacy > 1 )
397 if ( ldap_set_option( ld, LDAP_OPT_X_SASL_MINSSF,
398 (void *)&minssf ) != LDAP_OPT_SUCCESS ) {
399 fprintf( stderr, "Could not set LDAP_OPT_X_SASL_MINSSF"
401 return( EXIT_FAILURE );
403 if ( ldap_set_option( ld, LDAP_OPT_X_SASL_MAXSSF,
404 (void *)&maxssf ) != LDAP_OPT_SUCCESS ) {
405 fprintf( stderr, "Could not set LDAP_OPT_X_SASL_MAXSSF"
407 return( EXIT_FAILURE );
410 rc = ldap_negotiated_sasl_bind_s( ld, binddn, sasl_authc_id,
411 sasl_authz_id, sasl_mech,
412 passwd.bv_len ? &passwd : NULL,
415 if( rc != LDAP_SUCCESS ) {
416 ldap_perror( ld, "ldap_negotiated_sasl_bind_s" );
417 return( EXIT_FAILURE );
420 fprintf( stderr, "%s was not compiled with SASL support\n",
422 return( EXIT_FAILURE );
426 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
428 ldap_perror( ld, "ldap_bind" );
429 return( EXIT_FAILURE );
436 LDAPControl *ctrls[2];
440 c.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
441 c.ldctl_value.bv_val = NULL;
442 c.ldctl_value.bv_len = 0;
443 c.ldctl_iscritical = manageDSAit > 1;
445 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, &ctrls );
447 if( err != LDAP_OPT_SUCCESS ) {
448 fprintf( stderr, "Could not set ManageDSAit %scontrol\n",
449 c.ldctl_iscritical ? "critical " : "" );
450 if( c.ldctl_iscritical ) {
451 exit( EXIT_FAILURE );
458 rc = domodrdn( ld, entrydn, rdn, newSuperior, remove );
459 else while ((rc == 0 || contoper) && fgets(buf, sizeof(buf), fp) != NULL) {
460 if ( *buf != '\0' ) { /* blank lines optional, skip */
461 buf[ strlen( buf ) - 1 ] = '\0'; /* remove nl */
463 if ( havedn ) { /* have DN, get RDN */
464 if (( rdn = strdup( buf )) == NULL ) {
466 return( EXIT_FAILURE );
468 rc = domodrdn(ld, entrydn, rdn, newSuperior, remove );
470 } else if ( !havedn ) { /* don't have DN yet */
471 if (( entrydn = strdup( buf )) == NULL ) {
473 return( EXIT_FAILURE );
491 int remove ) /* flag: remove old RDN */
494 char *matcheddn=NULL, *text=NULL, **refs=NULL;
498 printf( "Renaming \"%s\"\n", dn );
499 printf( "\tnew rdn=\"%s\" (%s old rdn)\n",
500 rdn, remove ? "delete" : "keep" );
501 if( newSuperior != NULL ) {
502 printf("\tnew parent=\"%s\"\n", newSuperior);
506 if( not ) return LDAP_SUCCESS;
508 rc = ldap_rename( ld, dn, rdn, newSuperior, remove,
511 if ( rc != LDAP_SUCCESS ) {
512 fprintf( stderr, "ldapmodrdn: ldap_rename: %s (%d)\n",
513 ldap_err2string( rc ), rc );
517 rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, NULL, &res );
518 if ( rc != LDAP_SUCCESS ) {
519 fprintf( stderr, "ldapmodrdn: ldap_result: %s (%d)\n",
520 ldap_err2string( rc ), rc );
524 rc = ldap_parse_result( ld, res, &code, &matcheddn, &text, &refs, NULL, 1 );
526 if( rc != LDAP_SUCCESS ) {
527 fprintf( stderr, "ldapmodrdn: ldap_parse_result: %s (%d)\n",
528 ldap_err2string( rc ), rc );
532 if( verbose || code != LDAP_SUCCESS ||
533 (matcheddn && *matcheddn) || (text && *text) || (refs && *refs) )
535 printf( "Rename Result: %s (%d)\n",
536 ldap_err2string( code ), code );
538 if( text && *text ) {
539 printf( "Additional info: %s\n", text );
542 if( matcheddn && *matcheddn ) {
543 printf( "Matched DN: %s\n", matcheddn );
548 for( i=0; refs[i]; i++ ) {
549 printf("Referral: %s\n", refs[i] );
555 ber_memfree( matcheddn );
556 ber_memvfree( (void **) refs );