3 * Copyright 1998-1999 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 char *passwd = 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\trequest the use of TLS (-ZZ to make it critical)\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 = strdup( optarg );
145 for( p = optarg; *p == '\0'; p++ ) {
151 debug |= atoi( optarg );
153 case 'f': /* read from file */
154 infile = strdup( optarg );
157 ldapport = atoi( optarg );
159 case 'n': /* print adds, don't actually do them */
162 case 'v': /* verbose mode */
165 case 'r': /* remove old RDN */
169 /* enable Manage DSA IT */
176 switch( atoi(optarg) )
179 version = LDAP_VERSION2;
182 version = LDAP_VERSION3;
185 fprintf( stderr, "protocol version should be 2 or 3\n" );
187 return( EXIT_FAILURE );
191 #ifdef HAVE_CYRUS_SASL
193 authmethod = LDAP_AUTH_SASL;
195 fprintf( stderr, "%s was not compiled with SASL support\n",
197 return( EXIT_FAILURE );
201 #ifdef HAVE_CYRUS_SASL
203 authmethod = LDAP_AUTH_SASL;
205 fprintf( stderr, "%s was not compiled with SASL support\n",
207 return( EXIT_FAILURE );
211 #ifdef HAVE_CYRUS_SASL
212 if ( strcasecmp( optarg, "any" ) && strcmp( optarg, "*" ) ) {
213 sasl_mech = strdup( optarg );
215 authmethod = LDAP_AUTH_SASL;
217 fprintf( stderr, "%s was not compiled with SASL support\n",
219 return( EXIT_FAILURE );
223 #ifdef HAVE_CYRUS_SASL
224 sasl_authc_id = strdup( optarg );
225 authmethod = LDAP_AUTH_SASL;
227 fprintf( stderr, "%s was not compiled with SASL support\n",
229 return( EXIT_FAILURE );
233 #ifdef HAVE_CYRUS_SASL
234 sasl_authz_id = strdup( optarg );
235 authmethod = LDAP_AUTH_SASL;
237 fprintf( stderr, "%s was not compiled with SASL support\n",
239 return( EXIT_FAILURE );
246 fprintf( stderr, "%s was not compiled with TLS support\n",
248 return( EXIT_FAILURE );
253 return( EXIT_FAILURE );
257 if ( ( authmethod == LDAP_AUTH_KRBV4 ) || ( authmethod ==
258 LDAP_AUTH_KRBV41 ) ) {
259 if( version > LDAP_VERSION2 ) {
260 fprintf( stderr, "Kerberos requires LDAPv2\n" );
261 return( EXIT_FAILURE );
263 version = LDAP_VERSION2;
265 else if ( authmethod == LDAP_AUTH_SASL ) {
266 if( version != -1 || version != LDAP_VERSION3 ) {
267 fprintf( stderr, "SASL requires LDAPv3\n" );
268 return( EXIT_FAILURE );
270 version = LDAP_VERSION3;
274 if( version != -1 || version != LDAP_VERSION3 ) {
275 fprintf(stderr, "manage DSA control requires LDAPv3\n");
278 version = LDAP_VERSION3;
282 if( version != -1 || version != LDAP_VERSION3 ) {
283 fprintf(stderr, "Start TLS requires LDAPv3\n");
286 version = LDAP_VERSION3;
289 if (newSuperior != NULL) {
290 if (version == LDAP_VERSION2) {
292 "%s: version conflict!, -s newSuperior requires LDAPv3\n",
295 return( EXIT_FAILURE );
297 version = LDAP_VERSION3;
301 if (argc - optind == 2) {
302 if (( rdn = strdup( argv[argc - 1] )) == NULL ) {
304 return( EXIT_FAILURE );
306 if (( entrydn = strdup( argv[argc - 2] )) == NULL ) {
308 return( EXIT_FAILURE );
311 } else if ( argc - optind != 0 ) {
312 fprintf( stderr, "%s: invalid number of arguments, only two allowed\n", myname);
314 return( EXIT_FAILURE );
317 if ( infile != NULL ) {
318 if (( fp = fopen( infile, "r" )) == NULL ) {
320 return( EXIT_FAILURE );
327 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
328 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
330 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
331 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
336 (void) SIGNAL( SIGPIPE, SIG_IGN );
339 if (( ld = ldap_init( ldaphost, ldapport )) == NULL ) {
340 perror( "ldap_init" );
341 return( EXIT_FAILURE );
344 /* this seems prudent */
346 int deref = LDAP_DEREF_NEVER;
347 ldap_set_option( ld, LDAP_OPT_DEREF, &deref);
349 /* don't chase referrals */
350 ldap_set_option( ld, LDAP_OPT_REFERRALS, LDAP_OPT_OFF );
354 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version ) != LDAP_OPT_SUCCESS)
356 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n", version );
359 if ( use_tls && ldap_start_tls( ld, NULL, NULL ) != LDAP_SUCCESS ) {
361 ldap_perror( ld, "ldap_start_tls" );
362 return( EXIT_FAILURE );
367 passwd = getpass("Enter LDAP Password: ");
369 if ( authmethod == LDAP_AUTH_SASL ) {
370 #ifdef HAVE_CYRUS_SASL
371 int minssf = 0, maxssf = 0;
373 if ( sasl_integrity > 0 )
375 if ( sasl_integrity > 1 )
377 if ( sasl_privacy > 0 )
378 maxssf = 100000; /* Something big value */
379 if ( sasl_privacy > 1 )
382 if ( ldap_set_option( ld, LDAP_OPT_X_SASL_MINSSF,
383 (void *)&minssf ) != LDAP_OPT_SUCCESS ) {
384 fprintf( stderr, "Could not set LDAP_OPT_X_SASL_MINSSF"
386 return( EXIT_FAILURE );
388 if ( ldap_set_option( ld, LDAP_OPT_X_SASL_MAXSSF,
389 (void *)&maxssf ) != LDAP_OPT_SUCCESS ) {
390 fprintf( stderr, "Could not set LDAP_OPT_X_SASL_MAXSSF"
392 return( EXIT_FAILURE );
395 if ( ldap_negotiated_sasl_bind_s( ld, binddn, sasl_authc_id,
396 sasl_authz_id, sasl_mech, NULL, NULL, NULL )
398 ldap_perror( ld, "ldap_sasl_bind" );
399 return( EXIT_FAILURE );
402 fprintf( stderr, "%s was not compiled with SASL support\n",
404 return( EXIT_FAILURE );
408 if ( ldap_bind_s( ld, binddn, passwd, authmethod )
410 ldap_perror( ld, "ldap_bind" );
411 return( EXIT_FAILURE );
418 LDAPControl *ctrls[2];
422 c.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
423 c.ldctl_value.bv_val = NULL;
424 c.ldctl_value.bv_len = 0;
425 c.ldctl_iscritical = manageDSAit > 1;
427 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, &ctrls );
429 if( err != LDAP_OPT_SUCCESS ) {
430 fprintf( stderr, "Could not set Manage DSA IT Control\n" );
431 if( c.ldctl_iscritical ) {
432 exit( EXIT_FAILURE );
439 rc = domodrdn( ld, entrydn, rdn, newSuperior, remove );
440 else while ((rc == 0 || contoper) && fgets(buf, sizeof(buf), fp) != NULL) {
441 if ( *buf != '\0' ) { /* blank lines optional, skip */
442 buf[ strlen( buf ) - 1 ] = '\0'; /* remove nl */
444 if ( havedn ) { /* have DN, get RDN */
445 if (( rdn = strdup( buf )) == NULL ) {
447 return( EXIT_FAILURE );
449 rc = domodrdn(ld, entrydn, rdn, newSuperior, remove );
451 } else if ( !havedn ) { /* don't have DN yet */
452 if (( entrydn = strdup( buf )) == NULL ) {
454 return( EXIT_FAILURE );
472 int remove ) /* flag: remove old RDN */
477 printf( "Renaming \"%s\"\n", dn );
478 printf( "\tnew rdn=\"%s\" (%s old rdn)\n",
479 rdn, remove ? "delete" : "keep" );
480 if( newSuperior != NULL ) {
481 printf("\tnew parent=\"%s\"\n", newSuperior);
486 i = ldap_rename2_s( ld, dn, rdn, newSuperior, remove );
487 if ( i != LDAP_SUCCESS ) {
488 ldap_perror( ld, "ldap_rename2_s" );
489 } else if ( verbose ) {
490 printf( "modrdn complete\n" );