]> git.sur5r.net Git - openldap/blob - clients/tools/ldapmodrdn.c
3bc2e3140df15087121fee3bdf7e95597c4cfa44
[openldap] / clients / tools / ldapmodrdn.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6 /* ldapmodrdn.c - generic program to modify an entry's RDN using LDAP.
7  *
8  * Support for MODIFYDN REQUEST V3 (newSuperior) by:
9  * 
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.
13  *
14  * Redistribution and use in source and binary forms are permitted
15  * without restriction or fee of any kind as long as this notice
16  * is preserved.
17  *
18  */
19
20 #include "portable.h"
21
22 #include <stdio.h>
23
24 #include <ac/stdlib.h>
25
26 #include <ac/ctype.h>
27 #include <ac/signal.h>
28 #include <ac/string.h>
29 #include <ac/unistd.h>
30
31 #include <lber.h>
32 #include <ldap.h>
33
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;
39 static LDAP     *ld;
40
41 static int domodrdn LDAP_P((
42     LDAP        *ld,
43     char        *dn,
44     char        *rdn,
45     int         remove,         /* flag: remove old RDN */
46     char        *newSuperior));
47
48 int
49 main(int argc, char **argv)
50 {
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 ];
53     FILE                *fp;
54         int             rc, i, remove, havedn, authmethod, version, want_bindpw, debug, manageDSAit;
55     char        *newSuperior=NULL;
56
57     infile = NULL;
58     not = contoper = verbose = remove = want_bindpw = debug = manageDSAit = 0;
59     authmethod = LDAP_AUTH_SIMPLE;
60         version = -1;
61
62     myname = (myname = strrchr(argv[0], '/')) == NULL ? argv[0] : ++myname;
63
64     while (( i = getopt( argc, argv, "WkKMcnvrh:P:p:D:w:d:f:s:" )) != EOF ) {
65         switch( i ) {
66         case 'k':       /* kerberos bind */
67 #ifdef HAVE_KERBEROS
68                 authmethod = LDAP_AUTH_KRBV4;
69 #else
70                 fprintf (stderr, "%s was not compiled with Kerberos support\n", argv[0]);
71                 return( EXIT_FAILURE );
72 #endif
73             break;
74         case 'K':       /* kerberos bind, part one only */
75 #ifdef HAVE_KERBEROS
76                 authmethod = LDAP_AUTH_KRBV41;
77 #else
78                 fprintf (stderr, "%s was not compiled with Kerberos support\n", argv[0]);
79                 return( EXIT_FAILURE );
80 #endif
81             break;
82         case 'c':       /* continuous operation mode */
83             ++contoper;
84             break;
85         case 'h':       /* ldap host */
86             ldaphost = strdup( optarg );
87             break;
88         case 'D':       /* bind DN */
89             binddn = strdup( optarg );
90             break;
91         case 's':       /* newSuperior */
92             newSuperior = strdup( optarg );
93             version = LDAP_VERSION3;    /* This option => force V3 */
94             break;
95         case 'w':       /* password */
96             passwd = strdup( optarg );
97                 {
98                         char* p;
99
100                         for( p = optarg; *p == '\0'; p++ ) {
101                                 *p = '*';
102                         }
103                 }
104             break;
105         case 'd':
106             debug |= atoi( optarg );
107             break;
108         case 'f':       /* read from file */
109             infile = strdup( optarg );
110             break;
111         case 'p':
112             ldapport = atoi( optarg );
113             break;
114         case 'n':       /* print adds, don't actually do them */
115             ++not;
116             break;
117         case 'v':       /* verbose mode */
118             verbose++;
119             break;
120         case 'r':       /* remove old RDN */
121             remove++;
122             break;
123         case 'M':
124                 /* enable Manage DSA IT */
125                 manageDSAit++;
126                 break;
127         case 'W':
128                 want_bindpw++;
129                 break;
130         case 'P':
131                 switch( atoi(optarg) )
132                 {
133                 case 2:
134                         version = LDAP_VERSION2;
135                         break;
136                 case 3:
137                         version = LDAP_VERSION3;
138                         break;
139                 default:
140                         fprintf( stderr, "protocol version should be 2 or 3\n" );
141                     fprintf( stderr, usage, argv[0] );
142                     return( EXIT_FAILURE );
143                 }
144                 break;
145         default:
146             fprintf( stderr, usage, argv[0] );
147             return( EXIT_FAILURE );
148         }
149     }
150
151     if (newSuperior != NULL) {
152                 if (version == LDAP_VERSION2) {
153                         fprintf( stderr,
154                                 "%s: version conflict!, -s newSuperior requires LDAPv3\n",
155                                 myname);
156                         fprintf( stderr, usage, argv[0] );
157                         return( EXIT_FAILURE );
158                 }
159
160                 /* promote to LDAPv3 */
161                 version = LDAP_VERSION3;
162     }
163     
164     havedn = 0;
165     if (argc - optind == 2) {
166         if (( rdn = strdup( argv[argc - 1] )) == NULL ) {
167             perror( "strdup" );
168             return( EXIT_FAILURE );
169         }
170         if (( entrydn = strdup( argv[argc - 2] )) == NULL ) {
171             perror( "strdup" );
172             return( EXIT_FAILURE );
173         }
174         ++havedn;
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 );
179     }
180
181     if ( infile != NULL ) {
182         if (( fp = fopen( infile, "r" )) == NULL ) {
183             perror( infile );
184             return( EXIT_FAILURE );
185         }
186     } else {
187         fp = stdin;
188     }
189
190         if ( debug ) {
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 );
193                 }
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 );
196                 }
197         }
198
199 #ifdef SIGPIPE
200         (void) SIGNAL( SIGPIPE, SIG_IGN );
201 #endif
202
203     if (( ld = ldap_init( ldaphost, ldapport )) == NULL ) {
204         perror( "ldap_init" );
205         return( EXIT_FAILURE );
206     }
207
208         /* this seems prudent */
209         {
210                 int deref = LDAP_DEREF_NEVER;
211                 ldap_set_option( ld, LDAP_OPT_DEREF, &deref);
212         }
213         /* don't chase referrals */
214         ldap_set_option( ld, LDAP_OPT_REFERRALS, LDAP_OPT_OFF );
215
216
217         if (want_bindpw)
218                 passwd = getpass("Enter LDAP Password: ");
219
220         if (version != -1 &&
221                 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version ) != LDAP_OPT_SUCCESS)
222         {
223                 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n", version );
224         }
225
226     if ( ldap_bind_s( ld, binddn, passwd, authmethod ) != LDAP_SUCCESS ) {
227         ldap_perror( ld, "ldap_bind" );
228         return( EXIT_FAILURE );
229     }
230
231         if ( manageDSAit ) {
232                 int err;
233                 LDAPControl c;
234                 LDAPControl *ctrls[2];
235                 ctrls[0] = &c;
236                 ctrls[1] = NULL;
237
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;
242
243                 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, &ctrls );
244
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 );
249                         }
250                 }
251         }
252
253     rc = 0;
254     if (havedn)
255         rc = domodrdn(ld, entrydn, rdn, remove, newSuperior);
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 */
259
260             if ( havedn ) {     /* have DN, get RDN */
261                 if (( rdn = strdup( buf )) == NULL ) {
262                     perror( "strdup" );
263                     return( EXIT_FAILURE );
264                 }
265                 rc = domodrdn(ld, entrydn, rdn, remove, newSuperior);
266                 havedn = 0;
267             } else if ( !havedn ) {     /* don't have DN yet */
268                 if (( entrydn = strdup( buf )) == NULL ) {
269                     perror( "strdup" );
270                     return( EXIT_FAILURE );
271                 }
272                 ++havedn;
273             }
274         }
275     }
276
277     ldap_unbind( ld );
278
279         /* UNREACHABLE */
280         return( rc );
281 }
282
283 static int domodrdn(
284     LDAP        *ld,
285     char        *dn,
286     char        *rdn,
287     int         remove,         /* flag: remove old RDN */
288     char        *newSuperior)
289 {
290     int i;
291
292     if ( verbose ) {
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);
298                 }
299         }
300
301     if ( !not ) {
302         i = ldap_rename2_s( ld, dn, rdn, remove, newSuperior );
303         if ( i != LDAP_SUCCESS ) {
304             ldap_perror( ld, "ldap_rename2_s" );
305         } else if ( verbose ) {
306             printf( "modrdn complete\n" );
307         }
308     } else {
309         i = LDAP_SUCCESS;
310     }
311
312     return( i );
313 }