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"
34 static char *binddn = NULL;
35 static struct berval passwd = { 0, NULL };
36 static char *ldaphost = NULL;
37 static int ldapport = 0;
38 #ifdef HAVE_CYRUS_SASL
39 static char *sasl_authc_id = NULL;
40 static char *sasl_authz_id = NULL;
41 static char *sasl_mech = NULL;
42 static char *sasl_secprops = NULL;
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 " -f file\t\tdo renames listed in `file'\n"
70 " -h host\t\tLDAP server\n"
71 " -k\t\tuse Kerberos authentication\n"
72 " -K\t\tlike -k, but do only step 1 of the Kerberos bind\n"
73 " -M\t\tenable Manage DSA IT control (-MM to make it critical)\n"
74 " -n\t\tshow what would be done but don't actually do it\n"
75 " -O secprops\tSASL security properties\n"
76 " -p port\t\tport on LDAP server\n"
77 " -P version\tprocotol version (default: 3)\n"
78 " -r\t\tremove old RDN\n"
79 " -s newsuperior\tnew superior entry\n"
80 " -U user\t\tSASL authentication identity (username)\n"
81 " -v\t\trun in verbose mode (diagnostics to standard output)\n"
82 " -w passwd\tbind passwd (for simple authentication)\n"
83 " -W\t\tprompt for bind passwd\n"
84 " -X id\t\tSASL authorization identity (\"dn:<dn>\" or \"u:<user>\")\n"
85 " -Y mech\t\tSASL mechanism\n"
86 " -Z\t\tissue Start TLS request (-ZZ to require successful response)\n"
93 main(int argc, char **argv)
95 char *myname,*infile, *entrydn = NULL, *rdn = NULL, buf[ 4096 ];
97 int rc, i, remove, havedn, authmethod, version, want_bindpw, debug, manageDSAit;
99 char *newSuperior=NULL;
102 not = contoper = verbose = remove = want_bindpw =
103 debug = manageDSAit = referrals = 0;
104 authmethod = LDAP_AUTH_SIMPLE;
107 myname = (myname = strrchr(argv[0], '/')) == NULL ? argv[0] : ++myname;
109 while (( i = getopt( argc, argv, "cCD:d:f:h:KkMnO:P:p:rs:U:vWw:X:Y:Z" )) != EOF ) {
111 case 'k': /* kerberos bind */
112 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
113 authmethod = LDAP_AUTH_KRBV4;
115 fprintf( stderr, "%s was not compiled with Kerberos support\n", argv[0] );
116 return( EXIT_FAILURE );
119 case 'K': /* kerberos bind, part one only */
120 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
121 authmethod = LDAP_AUTH_KRBV41;
123 fprintf( stderr, "%s was not compiled with Kerberos support\n", argv[0] );
124 return( EXIT_FAILURE );
127 case 'c': /* continuous operation mode */
133 case 'h': /* ldap host */
134 ldaphost = strdup( optarg );
136 case 'D': /* bind DN */
137 binddn = strdup( optarg );
139 case 's': /* newSuperior */
140 newSuperior = strdup( optarg );
141 version = LDAP_VERSION3; /* This option => force V3 */
143 case 'w': /* password */
144 passwd.bv_val = strdup( optarg );
148 for( p = optarg; *p == '\0'; p++ ) {
152 passwd.bv_len = strlen( passwd.bv_val );
155 debug |= atoi( optarg );
157 case 'f': /* read from file */
158 infile = strdup( optarg );
161 ldapport = atoi( optarg );
163 case 'n': /* print adds, don't actually do them */
166 case 'v': /* verbose mode */
169 case 'r': /* remove old RDN */
173 /* enable Manage DSA IT */
180 switch( atoi(optarg) )
183 version = LDAP_VERSION2;
186 version = LDAP_VERSION3;
189 fprintf( stderr, "protocol version should be 2 or 3\n" );
191 return( EXIT_FAILURE );
195 #ifdef HAVE_CYRUS_SASL
196 sasl_secprops = strdup( optarg );
197 authmethod = LDAP_AUTH_SASL;
199 fprintf( stderr, "%s was not compiled with SASL support\n",
201 return( EXIT_FAILURE );
205 #ifdef HAVE_CYRUS_SASL
206 if ( strcasecmp( optarg, "any" ) && strcmp( optarg, "*" ) ) {
207 sasl_mech = strdup( optarg );
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 sasl_authc_id = strdup( optarg );
219 authmethod = LDAP_AUTH_SASL;
221 fprintf( stderr, "%s was not compiled with SASL support\n",
223 return( EXIT_FAILURE );
227 #ifdef HAVE_CYRUS_SASL
228 sasl_authz_id = strdup( optarg );
229 authmethod = LDAP_AUTH_SASL;
231 fprintf( stderr, "%s was not compiled with SASL support\n",
233 return( EXIT_FAILURE );
240 fprintf( stderr, "%s was not compiled with TLS support\n",
242 return( EXIT_FAILURE );
247 return( EXIT_FAILURE );
251 if ( ( authmethod == LDAP_AUTH_KRBV4 ) || ( authmethod ==
252 LDAP_AUTH_KRBV41 ) ) {
253 if( version > LDAP_VERSION2 ) {
254 fprintf( stderr, "Kerberos requires LDAPv2\n" );
255 return( EXIT_FAILURE );
257 version = LDAP_VERSION2;
259 else if ( authmethod == LDAP_AUTH_SASL ) {
260 if( version != -1 && version != LDAP_VERSION3 ) {
261 fprintf( stderr, "SASL requires LDAPv3\n" );
262 return( EXIT_FAILURE );
264 version = LDAP_VERSION3;
268 if( version != -1 && version != LDAP_VERSION3 ) {
269 fprintf(stderr, "manage DSA control requires LDAPv3\n");
272 version = LDAP_VERSION3;
276 if( version != -1 && version != LDAP_VERSION3 ) {
277 fprintf(stderr, "Start TLS requires LDAPv3\n");
280 version = LDAP_VERSION3;
283 if (newSuperior != NULL) {
284 if (version == LDAP_VERSION2) {
286 "%s: version conflict!, -s newSuperior requires LDAPv3\n",
289 return( EXIT_FAILURE );
291 version = LDAP_VERSION3;
295 if (argc - optind == 2) {
296 if (( rdn = strdup( argv[argc - 1] )) == NULL ) {
298 return( EXIT_FAILURE );
300 if (( entrydn = strdup( argv[argc - 2] )) == NULL ) {
302 return( EXIT_FAILURE );
305 } else if ( argc - optind != 0 ) {
306 fprintf( stderr, "%s: invalid number of arguments, only two allowed\n", myname);
308 return( EXIT_FAILURE );
311 if ( infile != NULL ) {
312 if (( fp = fopen( infile, "r" )) == NULL ) {
314 return( EXIT_FAILURE );
321 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
322 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
324 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
325 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
330 (void) SIGNAL( SIGPIPE, SIG_IGN );
333 if (( ld = ldap_init( ldaphost, ldapport )) == NULL ) {
334 perror( "ldap_init" );
335 return( EXIT_FAILURE );
339 if( ldap_set_option( ld, LDAP_OPT_REFERRALS,
340 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
342 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
343 referrals ? "on" : "off" );
347 if (version == -1 ) {
351 if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version )
352 != LDAP_OPT_SUCCESS )
354 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
359 if ( use_tls && ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS ) {
361 ldap_perror( ld, "ldap_start_tls" );
362 return( EXIT_FAILURE );
364 fprintf( stderr, "WARNING: could not start TLS\n" );
368 passwd.bv_val = getpassphrase("Enter LDAP Password: ");
369 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
372 if ( authmethod == LDAP_AUTH_SASL ) {
373 #ifdef HAVE_CYRUS_SASL
374 ldap_set_sasl_interact_proc( ld, lutil_sasl_interact );
376 if( sasl_secprops != NULL ) {
377 rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
378 (void *) sasl_secprops );
380 if( rc != LDAP_OPT_SUCCESS ) {
382 "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
384 return( EXIT_FAILURE );
388 rc = ldap_sasl_interactive_bind_s( ld, binddn,
389 sasl_mech, NULL, NULL );
391 if( rc != LDAP_SUCCESS ) {
392 ldap_perror( ld, "ldap_sasl_interactive_bind_s" );
393 return( EXIT_FAILURE );
396 fprintf( stderr, "%s was not compiled with SASL support\n",
398 return( EXIT_FAILURE );
402 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
404 ldap_perror( ld, "ldap_bind" );
405 return( EXIT_FAILURE );
412 LDAPControl *ctrls[2];
416 c.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
417 c.ldctl_value.bv_val = NULL;
418 c.ldctl_value.bv_len = 0;
419 c.ldctl_iscritical = manageDSAit > 1;
421 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, &ctrls );
423 if( err != LDAP_OPT_SUCCESS ) {
424 fprintf( stderr, "Could not set ManageDSAit %scontrol\n",
425 c.ldctl_iscritical ? "critical " : "" );
426 if( c.ldctl_iscritical ) {
427 exit( EXIT_FAILURE );
434 rc = domodrdn( ld, entrydn, rdn, newSuperior, remove );
435 else while ((rc == 0 || contoper) && fgets(buf, sizeof(buf), fp) != NULL) {
436 if ( *buf != '\0' ) { /* blank lines optional, skip */
437 buf[ strlen( buf ) - 1 ] = '\0'; /* remove nl */
439 if ( havedn ) { /* have DN, get RDN */
440 if (( rdn = strdup( buf )) == NULL ) {
442 return( EXIT_FAILURE );
444 rc = domodrdn(ld, entrydn, rdn, newSuperior, remove );
446 } else if ( !havedn ) { /* don't have DN yet */
447 if (( entrydn = strdup( buf )) == NULL ) {
449 return( EXIT_FAILURE );
467 int remove ) /* flag: remove old RDN */
470 char *matcheddn=NULL, *text=NULL, **refs=NULL;
474 printf( "Renaming \"%s\"\n", dn );
475 printf( "\tnew rdn=\"%s\" (%s old rdn)\n",
476 rdn, remove ? "delete" : "keep" );
477 if( newSuperior != NULL ) {
478 printf("\tnew parent=\"%s\"\n", newSuperior);
482 if( not ) return LDAP_SUCCESS;
484 rc = ldap_rename( ld, dn, rdn, newSuperior, remove,
487 if ( rc != LDAP_SUCCESS ) {
488 fprintf( stderr, "ldapmodrdn: ldap_rename: %s (%d)\n",
489 ldap_err2string( rc ), rc );
493 rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, NULL, &res );
495 ldap_perror( ld, "ldapmodrdn: ldap_result" );
499 rc = ldap_parse_result( ld, res, &code, &matcheddn, &text, &refs, NULL, 1 );
501 if( rc != LDAP_SUCCESS ) {
502 fprintf( stderr, "ldapmodrdn: ldap_parse_result: %s (%d)\n",
503 ldap_err2string( rc ), rc );
507 if( verbose || code != LDAP_SUCCESS ||
508 (matcheddn && *matcheddn) || (text && *text) || (refs && *refs) )
510 printf( "Rename Result: %s (%d)\n",
511 ldap_err2string( code ), code );
513 if( text && *text ) {
514 printf( "Additional info: %s\n", text );
517 if( matcheddn && *matcheddn ) {
518 printf( "Matched DN: %s\n", matcheddn );
523 for( i=0; refs[i]; i++ ) {
524 printf("Referral: %s\n", refs[i] );
530 ber_memfree( matcheddn );
531 ber_memvfree( (void **) refs );