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 static int not, verbose, contoper;
46 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);
213 /* don't chase referrals */
214 ldap_set_option( ld, LDAP_OPT_REFERRALS, LDAP_OPT_OFF );
218 passwd = getpass("Enter LDAP Password: ");
221 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version ) != LDAP_OPT_SUCCESS)
223 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n", version );
226 if ( ldap_bind_s( ld, binddn, passwd, authmethod ) != LDAP_SUCCESS ) {
227 ldap_perror( ld, "ldap_bind" );
228 return( EXIT_FAILURE );
234 LDAPControl *ctrls[2];
238 c.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
239 c.ldctl_value.bv_val = NULL;
240 c.ldctl_value.bv_len = 0;
241 c.ldctl_iscritical = manageDSAit > 1;
243 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, &ctrls );
245 if( err != LDAP_OPT_SUCCESS ) {
246 fprintf( stderr, "Could not set Manage DSA IT Control\n" );
247 if( c.ldctl_iscritical ) {
248 exit( EXIT_FAILURE );
255 rc = domodrdn( ld, entrydn, rdn, newSuperior, remove );
256 else while ((rc == 0 || contoper) && fgets(buf, sizeof(buf), fp) != NULL) {
257 if ( *buf != '\0' ) { /* blank lines optional, skip */
258 buf[ strlen( buf ) - 1 ] = '\0'; /* remove nl */
260 if ( havedn ) { /* have DN, get RDN */
261 if (( rdn = strdup( buf )) == NULL ) {
263 return( EXIT_FAILURE );
265 rc = domodrdn(ld, entrydn, rdn, newSuperior, remove );
267 } else if ( !havedn ) { /* don't have DN yet */
268 if (( entrydn = strdup( buf )) == NULL ) {
270 return( EXIT_FAILURE );
288 int remove ) /* flag: remove old RDN */
293 printf( "Renaming \"%s\"\n", dn );
294 printf( "\tnew rdn=\"%s\" (%s old rdn)\n",
295 rdn, remove ? "delete" : "keep" );
296 if( newSuperior != NULL ) {
297 printf("\tnew parent=\"%s\"\n", newSuperior);
302 i = ldap_rename2_s( ld, dn, rdn, newSuperior, remove );
303 if ( i != LDAP_SUCCESS ) {
304 ldap_perror( ld, "ldap_rename2_s" );
305 } else if ( verbose ) {
306 printf( "modrdn complete\n" );