2 * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
3 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5 /* ldapmodrdn.c - generic program to modify an entry's RDN using LDAP.
7 * Support for MODIFYDN REQUEST V3 (newSuperior) by:
9 * Copyright 1999, Juan C. Gomez, All rights reserved.
10 * This software is not subject to any license of Silicon Graphics
11 * Inc. or Purdue University.
13 * Redistribution and use in source and binary forms are permitted
14 * without restriction or fee of any kind as long as this notice
23 #include <ac/stdlib.h>
26 #include <ac/signal.h>
27 #include <ac/string.h>
28 #include <ac/unistd.h>
33 static char *binddn = NULL;
34 static char *passwd = NULL;
35 static char *ldaphost = NULL;
36 static int ldapport = 0;
37 static int not, verbose, contoper;
40 static int domodrdn LDAP_P((
44 int remove, /* flag: remove old RDN */
48 main(int argc, char **argv)
50 char *usage = "usage: %s [-nvkWc] [-M[M]] [-d debug-level] [-h ldaphost] [-P version] [-p ldapport] [-D binddn] [-w passwd] [ -f file | < entryfile | dn newrdn ] [-s newSuperior]\n";
51 char *myname,*infile, *entrydn, *rdn, buf[ 4096 ];
53 int rc, i, remove, havedn, authmethod, version, want_bindpw, debug, manageDSAit;
54 char *newSuperior=NULL;
57 not = contoper = verbose = remove = want_bindpw = debug = manageDSAit = 0;
58 authmethod = LDAP_AUTH_SIMPLE;
61 myname = (myname = strrchr(argv[0], '/')) == NULL ? argv[0] : ++myname;
63 while (( i = getopt( argc, argv, "WkKMcnvrh:P:p:D:w:d:f:s:" )) != EOF ) {
65 case 'k': /* kerberos bind */
67 authmethod = LDAP_AUTH_KRBV4;
69 fprintf (stderr, "%s was not compiled with Kerberos support\n", argv[0]);
70 return( EXIT_FAILURE );
73 case 'K': /* kerberos bind, part one only */
75 authmethod = LDAP_AUTH_KRBV41;
77 fprintf (stderr, "%s was not compiled with Kerberos support\n", argv[0]);
78 return( EXIT_FAILURE );
81 case 'c': /* continuous operation mode */
84 case 'h': /* ldap host */
85 ldaphost = strdup( optarg );
87 case 'D': /* bind DN */
88 binddn = strdup( optarg );
90 case 's': /* newSuperior */
91 newSuperior = strdup( optarg );
92 version = LDAP_VERSION3; /* This option => force V3 */
94 case 'w': /* password */
95 passwd = strdup( optarg );
99 for( p = optarg; *p == '\0'; p++ ) {
105 debug |= atoi( optarg );
107 case 'f': /* read from file */
108 infile = strdup( optarg );
111 ldapport = atoi( optarg );
113 case 'n': /* print adds, don't actually do them */
116 case 'v': /* verbose mode */
119 case 'r': /* remove old RDN */
123 /* enable Manage DSA IT */
130 switch( atoi(optarg) )
133 version = LDAP_VERSION2;
136 version = LDAP_VERSION3;
139 fprintf( stderr, "protocol version should be 2 or 3\n" );
140 fprintf( stderr, usage, argv[0] );
141 return( EXIT_FAILURE );
145 fprintf( stderr, usage, argv[0] );
146 return( EXIT_FAILURE );
150 if (newSuperior != NULL) {
151 if (version == LDAP_VERSION2) {
153 "%s: version conflict!, -s newSuperior requires LDAPv3\n",
155 fprintf( stderr, usage, argv[0] );
156 return( EXIT_FAILURE );
159 /* promote to LDAPv3 */
160 version = LDAP_VERSION3;
164 if (argc - optind == 2) {
165 if (( rdn = strdup( argv[argc - 1] )) == NULL ) {
167 return( EXIT_FAILURE );
169 if (( entrydn = strdup( argv[argc - 2] )) == NULL ) {
171 return( EXIT_FAILURE );
174 } else if ( argc - optind != 0 ) {
175 fprintf( stderr, "%s: invalid number of arguments, only two allowed\n", myname);
176 fprintf( stderr, usage, argv[0] );
177 return( EXIT_FAILURE );
180 if ( infile != NULL ) {
181 if (( fp = fopen( infile, "r" )) == NULL ) {
183 return( EXIT_FAILURE );
190 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
191 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
193 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
194 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
199 (void) SIGNAL( SIGPIPE, SIG_IGN );
202 if (( ld = ldap_init( ldaphost, ldapport )) == NULL ) {
203 perror( "ldap_init" );
204 return( EXIT_FAILURE );
207 /* this seems prudent */
209 int deref = LDAP_DEREF_NEVER;
210 ldap_set_option( ld, LDAP_OPT_DEREF, &deref);
214 passwd = getpass("Enter LDAP Password: ");
217 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version ) != LDAP_OPT_SUCCESS)
219 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n", version );
222 if ( ldap_bind_s( ld, binddn, passwd, authmethod ) != LDAP_SUCCESS ) {
223 ldap_perror( ld, "ldap_bind" );
224 return( EXIT_FAILURE );
230 LDAPControl *ctrls[2];
234 c.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
235 c.ldctl_value.bv_val = NULL;
236 c.ldctl_value.bv_len = 0;
237 c.ldctl_iscritical = manageDSAit > 1;
239 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, &ctrls );
241 if( err != LDAP_OPT_SUCCESS ) {
242 fprintf( stderr, "Could not set Manage DSA IT Control\n" );
243 if( c.ldctl_iscritical ) {
244 exit( EXIT_FAILURE );
251 rc = domodrdn(ld, entrydn, rdn, remove, newSuperior);
252 else while ((rc == 0 || contoper) && fgets(buf, sizeof(buf), fp) != NULL) {
253 if ( *buf != '\0' ) { /* blank lines optional, skip */
254 buf[ strlen( buf ) - 1 ] = '\0'; /* remove nl */
256 if ( havedn ) { /* have DN, get RDN */
257 if (( rdn = strdup( buf )) == NULL ) {
259 return( EXIT_FAILURE );
261 rc = domodrdn(ld, entrydn, rdn, remove, newSuperior);
263 } else if ( !havedn ) { /* don't have DN yet */
264 if (( entrydn = strdup( buf )) == NULL ) {
266 return( EXIT_FAILURE );
283 int remove, /* flag: remove old RDN */
289 printf( "Renaming \"%s\"\n", dn );
290 printf( "\tnew rdn=\"%s\" (%s old rdn)\n",
291 rdn, remove ? "delete" : "keep" );
292 if( newSuperior != NULL ) {
293 printf("\tnew parent=\"%s\"\n", newSuperior);
298 i = ldap_rename2_s( ld, dn, rdn, remove, newSuperior );
299 if ( i != LDAP_SUCCESS ) {
300 ldap_perror( ld, "ldap_rename2_s" );
301 } else if ( verbose ) {
302 printf( "modrdn complete\n" );