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 *base = NULL;
36 static char *ldaphost = NULL;
37 static int ldapport = 0;
38 static int not, verbose, contoper;
41 static int domodrdn LDAP_P((
45 int remove, /* flag: remove old RDN */
49 main(int argc, char **argv)
51 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";
52 char *myname,*infile, *entrydn, *rdn, buf[ 4096 ];
54 int rc, i, remove, havedn, authmethod, version, want_bindpw, debug, manageDSAit;
55 char *newSuperior=NULL;
58 not = contoper = verbose = remove = want_bindpw = debug = manageDSAit = 0;
59 authmethod = LDAP_AUTH_SIMPLE;
62 myname = (myname = strrchr(argv[0], '/')) == NULL ? argv[0] : ++myname;
64 while (( i = getopt( argc, argv, "WkKMcnvrh:P:p:D:w:d:f:s:" )) != EOF ) {
66 case 'k': /* kerberos bind */
68 authmethod = LDAP_AUTH_KRBV4;
70 fprintf (stderr, "%s was not compiled with Kerberos support\n", argv[0]);
71 return( EXIT_FAILURE );
74 case 'K': /* kerberos bind, part one only */
76 authmethod = LDAP_AUTH_KRBV41;
78 fprintf (stderr, "%s was not compiled with Kerberos support\n", argv[0]);
79 return( EXIT_FAILURE );
82 case 'c': /* continuous operation mode */
85 case 'h': /* ldap host */
86 ldaphost = strdup( optarg );
88 case 'D': /* bind DN */
89 binddn = strdup( optarg );
91 case 's': /* newSuperior */
92 newSuperior = strdup( optarg );
93 version = LDAP_VERSION3; /* This option => force V3 */
95 case 'w': /* password */
96 passwd = strdup( optarg );
100 for( p = optarg; *p == '\0'; p++ ) {
106 debug |= atoi( optarg );
108 case 'f': /* read from file */
109 infile = strdup( optarg );
112 ldapport = atoi( optarg );
114 case 'n': /* print adds, don't actually do them */
117 case 'v': /* verbose mode */
120 case 'r': /* remove old RDN */
124 /* enable Manage DSA IT */
131 switch( atoi(optarg) )
134 version = LDAP_VERSION2;
137 version = LDAP_VERSION3;
140 fprintf( stderr, "protocol version should be 2 or 3\n" );
141 fprintf( stderr, usage, argv[0] );
142 return( EXIT_FAILURE );
146 fprintf( stderr, usage, argv[0] );
147 return( EXIT_FAILURE );
151 if (newSuperior != NULL) {
152 if (version == LDAP_VERSION2) {
154 "%s: version conflict!, -s newSuperior requires LDAPv3\n",
156 fprintf( stderr, usage, argv[0] );
157 return( EXIT_FAILURE );
160 /* promote to LDAPv3 */
161 version = LDAP_VERSION3;
165 if (argc - optind == 2) {
166 if (( rdn = strdup( argv[argc - 1] )) == NULL ) {
168 return( EXIT_FAILURE );
170 if (( entrydn = strdup( argv[argc - 2] )) == NULL ) {
172 return( EXIT_FAILURE );
175 } else if ( argc - optind != 0 ) {
176 fprintf( stderr, "%s: invalid number of arguments, only two allowed\n", myname);
177 fprintf( stderr, usage, argv[0] );
178 return( EXIT_FAILURE );
181 if ( infile != NULL ) {
182 if (( fp = fopen( infile, "r" )) == NULL ) {
184 return( EXIT_FAILURE );
191 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
192 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
194 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
195 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
200 (void) SIGNAL( SIGPIPE, SIG_IGN );
203 if (( ld = ldap_init( ldaphost, ldapport )) == NULL ) {
204 perror( "ldap_init" );
205 return( EXIT_FAILURE );
208 /* this seems prudent */
210 int deref = LDAP_DEREF_NEVER;
211 ldap_set_option( ld, LDAP_OPT_DEREF, &deref);
215 passwd = getpass("Enter LDAP Password: ");
218 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version ) != LDAP_OPT_SUCCESS)
220 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n", version );
223 if ( ldap_bind_s( ld, binddn, passwd, authmethod ) != LDAP_SUCCESS ) {
224 ldap_perror( ld, "ldap_bind" );
225 return( EXIT_FAILURE );
231 LDAPControl *ctrls[2];
235 c.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
236 c.ldctl_value.bv_val = NULL;
237 c.ldctl_value.bv_len = 0;
238 c.ldctl_iscritical = manageDSAit > 1;
240 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, &ctrls );
242 if( err != LDAP_OPT_SUCCESS ) {
243 fprintf( stderr, "Could not set Manage DSA IT Control\n" );
244 if( c.ldctl_iscritical ) {
245 exit( EXIT_FAILURE );
252 rc = domodrdn(ld, entrydn, rdn, remove, newSuperior);
253 else while ((rc == 0 || contoper) && fgets(buf, sizeof(buf), fp) != NULL) {
254 if ( *buf != '\0' ) { /* blank lines optional, skip */
255 buf[ strlen( buf ) - 1 ] = '\0'; /* remove nl */
257 if ( havedn ) { /* have DN, get RDN */
258 if (( rdn = strdup( buf )) == NULL ) {
260 return( EXIT_FAILURE );
262 rc = domodrdn(ld, entrydn, rdn, remove, newSuperior);
264 } else if ( !havedn ) { /* don't have DN yet */
265 if (( entrydn = strdup( buf )) == NULL ) {
267 return( EXIT_FAILURE );
284 int remove, /* flag: remove old RDN */
290 printf( "Renaming \"%s\"\n", dn );
291 printf( "\tnew rdn=\"%s\" (%s old rdn)\n",
292 rdn, remove ? "delete" : "keep" );
293 if( newSuperior != NULL ) {
294 printf("\tnew parent=\"%s\"\n", newSuperior);
299 i = ldap_rename2_s( ld, dn, rdn, remove, newSuperior );
300 if ( i != LDAP_SUCCESS ) {
301 ldap_perror( ld, "ldap_rename2_s" );
302 } else if ( verbose ) {
303 printf( "modrdn complete\n" );