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>
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 int sasl_integrity = 0;
43 static int sasl_privacy = 0;
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 " -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 (2 or 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;
100 char *newSuperior=NULL;
103 not = contoper = verbose = remove = want_bindpw = debug = manageDSAit = 0;
104 authmethod = LDAP_AUTH_SIMPLE;
107 myname = (myname = strrchr(argv[0], '/')) == NULL ? argv[0] : ++myname;
109 while (( i = getopt( argc, argv, "cD:d:Ef:h:IKkMnP: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 */
130 case 'h': /* ldap host */
131 ldaphost = strdup( optarg );
133 case 'D': /* bind DN */
134 binddn = strdup( optarg );
136 case 's': /* newSuperior */
137 newSuperior = strdup( optarg );
138 version = LDAP_VERSION3; /* This option => force V3 */
140 case 'w': /* password */
141 passwd.bv_val = strdup( optarg );
145 for( p = optarg; *p == '\0'; p++ ) {
149 passwd.bv_len = strlen( passwd.bv_val );
152 debug |= atoi( optarg );
154 case 'f': /* read from file */
155 infile = strdup( optarg );
158 ldapport = atoi( optarg );
160 case 'n': /* print adds, don't actually do them */
163 case 'v': /* verbose mode */
166 case 'r': /* remove old RDN */
170 /* enable Manage DSA IT */
177 switch( atoi(optarg) )
180 version = LDAP_VERSION2;
183 version = LDAP_VERSION3;
186 fprintf( stderr, "protocol version should be 2 or 3\n" );
188 return( EXIT_FAILURE );
192 #ifdef HAVE_CYRUS_SASL
194 authmethod = LDAP_AUTH_SASL;
196 fprintf( stderr, "%s was not compiled with SASL support\n",
198 return( EXIT_FAILURE );
202 #ifdef HAVE_CYRUS_SASL
204 authmethod = LDAP_AUTH_SASL;
206 fprintf( stderr, "%s was not compiled with SASL support\n",
208 return( EXIT_FAILURE );
212 #ifdef HAVE_CYRUS_SASL
213 if ( strcasecmp( optarg, "any" ) && strcmp( optarg, "*" ) ) {
214 sasl_mech = strdup( optarg );
216 authmethod = LDAP_AUTH_SASL;
218 fprintf( stderr, "%s was not compiled with SASL support\n",
220 return( EXIT_FAILURE );
224 #ifdef HAVE_CYRUS_SASL
225 sasl_authc_id = strdup( optarg );
226 authmethod = LDAP_AUTH_SASL;
228 fprintf( stderr, "%s was not compiled with SASL support\n",
230 return( EXIT_FAILURE );
234 #ifdef HAVE_CYRUS_SASL
235 sasl_authz_id = strdup( optarg );
236 authmethod = LDAP_AUTH_SASL;
238 fprintf( stderr, "%s was not compiled with SASL support\n",
240 return( EXIT_FAILURE );
247 fprintf( stderr, "%s was not compiled with TLS support\n",
249 return( EXIT_FAILURE );
254 return( EXIT_FAILURE );
258 if ( ( authmethod == LDAP_AUTH_KRBV4 ) || ( authmethod ==
259 LDAP_AUTH_KRBV41 ) ) {
260 if( version > LDAP_VERSION2 ) {
261 fprintf( stderr, "Kerberos requires LDAPv2\n" );
262 return( EXIT_FAILURE );
264 version = LDAP_VERSION2;
266 else if ( authmethod == LDAP_AUTH_SASL ) {
267 if( version != -1 && version != LDAP_VERSION3 ) {
268 fprintf( stderr, "SASL requires LDAPv3\n" );
269 return( EXIT_FAILURE );
271 version = LDAP_VERSION3;
275 if( version != -1 && version != LDAP_VERSION3 ) {
276 fprintf(stderr, "manage DSA control requires LDAPv3\n");
279 version = LDAP_VERSION3;
283 if( version != -1 && version != LDAP_VERSION3 ) {
284 fprintf(stderr, "Start TLS requires LDAPv3\n");
287 version = LDAP_VERSION3;
290 if (newSuperior != NULL) {
291 if (version == LDAP_VERSION2) {
293 "%s: version conflict!, -s newSuperior requires LDAPv3\n",
296 return( EXIT_FAILURE );
298 version = LDAP_VERSION3;
302 if (argc - optind == 2) {
303 if (( rdn = strdup( argv[argc - 1] )) == NULL ) {
305 return( EXIT_FAILURE );
307 if (( entrydn = strdup( argv[argc - 2] )) == NULL ) {
309 return( EXIT_FAILURE );
312 } else if ( argc - optind != 0 ) {
313 fprintf( stderr, "%s: invalid number of arguments, only two allowed\n", myname);
315 return( EXIT_FAILURE );
318 if ( infile != NULL ) {
319 if (( fp = fopen( infile, "r" )) == NULL ) {
321 return( EXIT_FAILURE );
328 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
329 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
331 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
332 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
337 (void) SIGNAL( SIGPIPE, SIG_IGN );
340 if (( ld = ldap_init( ldaphost, ldapport )) == NULL ) {
341 perror( "ldap_init" );
342 return( EXIT_FAILURE );
345 /* this seems prudent */
347 int deref = LDAP_DEREF_NEVER;
348 ldap_set_option( ld, LDAP_OPT_DEREF, &deref);
350 /* don't chase referrals */
351 ldap_set_option( ld, LDAP_OPT_REFERRALS, LDAP_OPT_OFF );
355 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version ) != LDAP_OPT_SUCCESS)
357 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n", version );
360 if ( use_tls && ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS ) {
362 ldap_perror( ld, "ldap_start_tls" );
363 return( EXIT_FAILURE );
368 passwd.bv_val = getpassphrase("Enter LDAP Password: ");
369 passwd.bv_len = strlen( passwd.bv_val );
372 if ( authmethod == LDAP_AUTH_SASL ) {
373 #ifdef HAVE_CYRUS_SASL
374 int minssf = 0, maxssf = 0;
376 if ( sasl_integrity > 0 )
378 if ( sasl_integrity > 1 )
380 if ( sasl_privacy > 0 )
381 maxssf = 100000; /* Something big value */
382 if ( sasl_privacy > 1 )
385 if ( ldap_set_option( ld, LDAP_OPT_X_SASL_MINSSF,
386 (void *)&minssf ) != LDAP_OPT_SUCCESS ) {
387 fprintf( stderr, "Could not set LDAP_OPT_X_SASL_MINSSF"
389 return( EXIT_FAILURE );
391 if ( ldap_set_option( ld, LDAP_OPT_X_SASL_MAXSSF,
392 (void *)&maxssf ) != LDAP_OPT_SUCCESS ) {
393 fprintf( stderr, "Could not set LDAP_OPT_X_SASL_MAXSSF"
395 return( EXIT_FAILURE );
398 rc = ldap_negotiated_sasl_bind_s( ld, binddn, sasl_authc_id,
399 sasl_authz_id, sasl_mech,
400 passwd.bv_len ? &passwd : NULL,
403 if( rc != LDAP_SUCCESS ) {
404 ldap_perror( ld, "ldap_negotiated_sasl_bind_s" );
405 return( EXIT_FAILURE );
408 fprintf( stderr, "%s was not compiled with SASL support\n",
410 return( EXIT_FAILURE );
414 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
416 ldap_perror( ld, "ldap_bind" );
417 return( EXIT_FAILURE );
424 LDAPControl *ctrls[2];
428 c.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
429 c.ldctl_value.bv_val = NULL;
430 c.ldctl_value.bv_len = 0;
431 c.ldctl_iscritical = manageDSAit > 1;
433 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, &ctrls );
435 if( err != LDAP_OPT_SUCCESS ) {
436 fprintf( stderr, "Could not set Manage DSA IT Control\n" );
437 if( c.ldctl_iscritical ) {
438 exit( EXIT_FAILURE );
445 rc = domodrdn( ld, entrydn, rdn, newSuperior, remove );
446 else while ((rc == 0 || contoper) && fgets(buf, sizeof(buf), fp) != NULL) {
447 if ( *buf != '\0' ) { /* blank lines optional, skip */
448 buf[ strlen( buf ) - 1 ] = '\0'; /* remove nl */
450 if ( havedn ) { /* have DN, get RDN */
451 if (( rdn = strdup( buf )) == NULL ) {
453 return( EXIT_FAILURE );
455 rc = domodrdn(ld, entrydn, rdn, newSuperior, remove );
457 } else if ( !havedn ) { /* don't have DN yet */
458 if (( entrydn = strdup( buf )) == NULL ) {
460 return( EXIT_FAILURE );
478 int remove ) /* flag: remove old RDN */
483 printf( "Renaming \"%s\"\n", dn );
484 printf( "\tnew rdn=\"%s\" (%s old rdn)\n",
485 rdn, remove ? "delete" : "keep" );
486 if( newSuperior != NULL ) {
487 printf("\tnew parent=\"%s\"\n", newSuperior);
492 i = ldap_rename2_s( ld, dn, rdn, newSuperior, remove );
493 if ( i != LDAP_SUCCESS ) {
494 ldap_perror( ld, "ldap_rename2_s" );
495 } else if ( verbose ) {
496 printf( "modrdn complete\n" );