]> git.sur5r.net Git - openldap/blob - clients/tools/ldapmodrdn.c
6e98c7d32f0b91da3542418a50f92aced834cd47
[openldap] / clients / tools / ldapmodrdn.c
1 /* ldapmodrdn.c - generic program to modify an entry's RDN using LDAP.
2  *
3  * Support for MODIFYDN REQUEST V3 (newSuperior) by:
4  * 
5  * Copyright 1999, Juan C. Gomez, All rights reserved.
6  * This software is not subject to any license of Silicon Graphics 
7  * Inc. or Purdue University.
8  *
9  * Redistribution and use in source and binary forms are permitted
10  * without restriction or fee of any kind as long as this notice
11  * is preserved.
12  *
13  */
14
15 #include "portable.h"
16
17 #include <stdio.h>
18
19 #include <ac/stdlib.h>
20
21 #include <ac/ctype.h>
22 #include <ac/signal.h>
23 #include <ac/string.h>
24 #include <ac/unistd.h>
25
26 #include <lber.h>
27 #include <ldap.h>
28
29 static char     *binddn = NULL;
30 static char     *passwd = NULL;
31 static char     *base = NULL;
32 static char     *ldaphost = NULL;
33 static int      ldapport = 0;
34 static int      not, verbose, contoper;
35 static LDAP     *ld;
36
37 static int domodrdn LDAP_P((
38     LDAP        *ld,
39     char        *dn,
40     char        *rdn,
41     int         remove,         /* flag: remove old RDN */
42     char        *newSuperior));
43
44 int
45 main(int argc, char **argv)
46 {
47         char            *usage = "usage: %s [-nvkWc] [-d debug-level] [-h ldaphost] [-P version] [-p ldapport] [-D binddn] [-w passwd] [ -f file | < entryfile | dn newrdn ] [-s newSuperior]\n";
48     char                *myname,*infile, *entrydn, *rdn, buf[ 4096 ];
49     FILE                *fp;
50         int             rc, i, remove, havedn, authmethod, version, want_bindpw, debug;
51     char        *newSuperior=NULL;
52
53     infile = NULL;
54     not = contoper = verbose = remove = want_bindpw = debug = 0;
55     authmethod = LDAP_AUTH_SIMPLE;
56         version = -1;
57
58     myname = (myname = strrchr(argv[0], '/')) == NULL ? argv[0] : ++myname;
59
60     while (( i = getopt( argc, argv, "WkKcnvrh:P:p:D:w:d:f:s:" )) != EOF ) {
61         switch( i ) {
62         case 'k':       /* kerberos bind */
63 #ifdef HAVE_KERBEROS
64                 authmethod = LDAP_AUTH_KRBV4;
65 #else
66                 fprintf (stderr, "%s was not compiled with Kerberos support\n", argv[0]);
67                 return( EXIT_FAILURE );
68 #endif
69             break;
70         case 'K':       /* kerberos bind, part one only */
71 #ifdef HAVE_KERBEROS
72                 authmethod = LDAP_AUTH_KRBV41;
73 #else
74                 fprintf (stderr, "%s was not compiled with Kerberos support\n", argv[0]);
75                 return( EXIT_FAILURE );
76 #endif
77             break;
78         case 'c':       /* continuous operation mode */
79             ++contoper;
80             break;
81         case 'h':       /* ldap host */
82             ldaphost = strdup( optarg );
83             break;
84         case 'D':       /* bind DN */
85             binddn = strdup( optarg );
86             break;
87         case 's':       /* newSuperior */
88             newSuperior = strdup( optarg );
89             version = LDAP_VERSION3;    /* This option => force V3 */
90             break;
91         case 'w':       /* password */
92             passwd = strdup( optarg );
93             break;
94         case 'd':
95             debug |= atoi( optarg );
96             break;
97         case 'f':       /* read from file */
98             infile = strdup( optarg );
99             break;
100         case 'p':
101             ldapport = atoi( optarg );
102             break;
103         case 'n':       /* print adds, don't actually do them */
104             ++not;
105             break;
106         case 'v':       /* verbose mode */
107             verbose++;
108             break;
109         case 'r':       /* remove old RDN */
110             remove++;
111             break;
112         case 'W':
113                 want_bindpw++;
114                 break;
115         case 'P':
116                 switch( atoi(optarg) )
117                 {
118                 case 2:
119                         version = LDAP_VERSION2;
120                         break;
121                 case 3:
122                         version = LDAP_VERSION3;
123                         break;
124                 default:
125                         fprintf( stderr, "protocol version should be 2 or 3\n" );
126                     fprintf( stderr, usage, argv[0] );
127                     return( EXIT_FAILURE );
128                 }
129                 break;
130         default:
131             fprintf( stderr, usage, argv[0] );
132             return( EXIT_FAILURE );
133         }
134     }
135
136     if (newSuperior != NULL) {
137                 if (version == LDAP_VERSION2) {
138                         fprintf( stderr,
139                                 "%s: version conflict!, -s newSuperior requires LDAPv3\n",
140                                 myname);
141                         fprintf( stderr, usage, argv[0] );
142                         return( EXIT_FAILURE );
143                 }
144
145                 /* promote to LDAPv3 */
146                 version = LDAP_VERSION3;
147     }
148     
149     havedn = 0;
150     if (argc - optind == 2) {
151         if (( rdn = strdup( argv[argc - 1] )) == NULL ) {
152             perror( "strdup" );
153             return( EXIT_FAILURE );
154         }
155         if (( entrydn = strdup( argv[argc - 2] )) == NULL ) {
156             perror( "strdup" );
157             return( EXIT_FAILURE );
158         }
159         ++havedn;
160     } else if ( argc - optind != 0 ) {
161         fprintf( stderr, "%s: invalid number of arguments, only two allowed\n", myname);
162         fprintf( stderr, usage, argv[0] );
163         return( EXIT_FAILURE );
164     }
165
166     if ( infile != NULL ) {
167         if (( fp = fopen( infile, "r" )) == NULL ) {
168             perror( infile );
169             return( EXIT_FAILURE );
170         }
171     } else {
172         fp = stdin;
173     }
174
175         if ( debug ) {
176                 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_ERROR ) {
177                         fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
178                 }
179                 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_ERROR ) {
180                         fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
181                 }
182         }
183
184 #ifdef SIGPIPE
185         (void) SIGNAL( SIGPIPE, SIG_IGN );
186 #endif
187
188     if (( ld = ldap_init( ldaphost, ldapport )) == NULL ) {
189         perror( "ldap_init" );
190         return( EXIT_FAILURE );
191     }
192
193         /* this seems prudent */
194         {
195                 int deref = LDAP_DEREF_NEVER;
196                 ldap_set_option( ld, LDAP_OPT_DEREF, &deref);
197         }
198
199         if (want_bindpw)
200                 passwd = getpass("Enter LDAP Password: ");
201
202         if (version != -1 &&
203                 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version ) == LDAP_OPT_ERROR)
204         {
205                 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n", version );
206         }
207
208     if ( ldap_bind_s( ld, binddn, passwd, authmethod ) != LDAP_SUCCESS ) {
209         ldap_perror( ld, "ldap_bind" );
210         return( EXIT_FAILURE );
211     }
212
213     rc = 0;
214     if (havedn)
215         rc = domodrdn(ld, entrydn, rdn, remove, newSuperior);
216     else while ((rc == 0 || contoper) && fgets(buf, sizeof(buf), fp) != NULL) {
217         if ( *buf != '\0' ) {   /* blank lines optional, skip */
218             buf[ strlen( buf ) - 1 ] = '\0';    /* remove nl */
219
220             if ( havedn ) {     /* have DN, get RDN */
221                 if (( rdn = strdup( buf )) == NULL ) {
222                     perror( "strdup" );
223                     return( EXIT_FAILURE );
224                 }
225                 rc = domodrdn(ld, entrydn, rdn, remove, newSuperior);
226                 havedn = 0;
227             } else if ( !havedn ) {     /* don't have DN yet */
228                 if (( entrydn = strdup( buf )) == NULL ) {
229                     perror( "strdup" );
230                     return( EXIT_FAILURE );
231                 }
232                 ++havedn;
233             }
234         }
235     }
236
237     ldap_unbind( ld );
238
239         /* UNREACHABLE */
240         return( rc );
241 }
242
243 static int domodrdn(
244     LDAP        *ld,
245     char        *dn,
246     char        *rdn,
247     int         remove,         /* flag: remove old RDN */
248     char        *newSuperior)
249 {
250     int i;
251
252     if ( verbose ) {
253         printf( "modrdn %s:\n\t%s\n", dn, rdn );
254         if (remove)
255             printf("removing old RDN\n");
256         else
257             printf("keeping old RDN\n");
258         if(newSuperior!=NULL)
259             printf("placing node under a new parent = %s\n", newSuperior);
260     }
261
262     if ( !not ) {
263         i = ldap_rename2_s( ld, dn, rdn, remove, newSuperior );
264         if ( i != LDAP_SUCCESS ) {
265             ldap_perror( ld, "ldap_rename2_s" );
266         } else if ( verbose ) {
267             printf( "modrdn complete\n" );
268         }
269     } else {
270         i = LDAP_SUCCESS;
271     }
272
273     return( i );
274 }