]> git.sur5r.net Git - openldap/blob - clients/tools/ldapmodrdn.c
ITS#6145
[openldap] / clients / tools / ldapmodrdn.c
1 /* ldapmodrdn.c - generic program to modify an entry's RDN using LDAP */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1998-2009 The OpenLDAP Foundation.
6  * Portions Copyright 1998-2003 Kurt D. Zeilenga.
7  * Portions Copyright 1998-2001 Net Boolean Incorporated.
8  * Portions Copyright 2001-2003 IBM Corporation.
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted only as authorized by the OpenLDAP
13  * Public License.
14  *
15  * A copy of this license is available in the file LICENSE in the
16  * top-level directory of the distribution or, alternatively, at
17  * <http://www.OpenLDAP.org/license.html>.
18  */
19 /* Portions Copyright 1999, Juan C. Gomez, All rights reserved.
20  * This software is not subject to any license of Silicon Graphics 
21  * Inc. or Purdue University.
22  *
23  * Redistribution and use in source and binary forms are permitted
24  * without restriction or fee of any kind as long as this notice
25  * is preserved.
26  */
27 /* Portions Copyright (c) 1992-1996 Regents of the University of Michigan.
28  * All rights reserved.
29  *
30  * Redistribution and use in source and binary forms are permitted
31  * provided that this notice is preserved and that due credit is given
32  * to the University of Michigan at Ann Arbor.  The name of the
33  * University may not be used to endorse or promote products derived
34  * from this software without specific prior written permission.  This
35  * software is provided ``as is'' without express or implied warranty.
36  */
37 /* ACKNOWLEDGEMENTS:
38  * This work was originally developed by the University of Michigan
39  * (as part of U-MICH LDAP).  Additional significant contributors
40  * include:
41  *      Kurt D. Zeilenga
42  *      Juan C Gomez
43  */
44
45
46 #include "portable.h"
47
48 #include <stdio.h>
49
50 #include <ac/stdlib.h>
51
52 #include <ac/ctype.h>
53 #include <ac/string.h>
54 #include <ac/unistd.h>
55 #include <ac/socket.h>
56 #include <ac/time.h>
57
58 #include <ldap.h>
59 #include "lutil.h"
60 #include "lutil_ldap.h"
61 #include "ldap_defaults.h"
62
63 #include "common.h"
64
65
66 static char     *newSuperior = NULL;
67 static int   remove_old_RDN = 0;
68
69
70 static int domodrdn(
71         LDAP    *ld,
72         char    *dn,
73         char    *rdn,
74         char    *newSuperior,
75         int             remove );       /* flag: remove old RDN */
76
77 void
78 usage( void )
79 {
80         fprintf( stderr, _("Rename LDAP entries\n\n"));
81         fprintf( stderr, _("usage: %s [options] [dn rdn]\n"), prog);
82         fprintf( stderr, _("    dn rdn: If given, rdn will replace the RDN of the entry specified by DN\n"));
83         fprintf( stderr, _("            If not given, the list of modifications is read from stdin or\n"));
84         fprintf( stderr, _("            from the file specified by \"-f file\" (see man page).\n"));
85         fprintf( stderr, _("Rename options:\n"));
86         fprintf( stderr, _("  -r                 remove old RDN\n"));
87         fprintf( stderr, _("  -s newsup  new superior entry\n"));
88         tool_common_usage();
89         exit( EXIT_FAILURE );
90 }
91
92
93 const char options[] = "rs:"
94         "cd:D:e:f:h:H:IMnNO:o:p:P:QR:U:vVw:WxX:y:Y:Z";
95
96 int
97 handle_private_option( int i )
98 {
99         switch ( i ) {
100 #if 0
101                 int crit;
102                 char *control, *cvalue;
103         case 'E': /* modrdn extensions */
104                 if( protocol == LDAP_VERSION2 ) {
105                         fprintf( stderr, _("%s: -E incompatible with LDAPv%d\n"),
106                                 prog, version );
107                         exit( EXIT_FAILURE );
108                 }
109
110                 /* should be extended to support comma separated list of
111                  *      [!]key[=value] parameters, e.g.  -E !foo,bar=567
112                  */
113
114                 crit = 0;
115                 cvalue = NULL;
116                 if( optarg[0] == '!' ) {
117                         crit = 1;
118                         optarg++;
119                 }
120
121                 control = strdup( optarg );
122                 if ( (cvalue = strchr( control, '=' )) != NULL ) {
123                         *cvalue++ = '\0';
124                 }
125                 fprintf( stderr, _("Invalid modrdn extension name: %s\n"), control );
126                 usage();
127 #endif
128
129         case 'r':       /* remove old RDN */
130                 remove_old_RDN++;
131                 break;
132
133         case 's':       /* newSuperior */
134                 if( protocol == LDAP_VERSION2 ) {
135                         fprintf( stderr, _("%s: -X incompatible with LDAPv%d\n"),
136                                 prog, protocol );
137                         exit( EXIT_FAILURE );
138                 }
139                 newSuperior = strdup( optarg );
140                 protocol = LDAP_VERSION3;
141                 break;
142
143         default:
144                 return 0;
145         }
146         return 1;
147 }
148
149
150 int
151 main(int argc, char **argv)
152 {
153         char            *entrydn = NULL, *rdn = NULL, buf[ 4096 ];
154         FILE            *fp = NULL;
155         LDAP            *ld;
156         int             rc, retval, havedn;
157
158         tool_init( TOOL_MODRDN );
159         prog = lutil_progname( "ldapmodrdn", argc, argv );
160
161         tool_args( argc, argv );
162
163         havedn = 0;
164         if (argc - optind == 2) {
165                 if (( rdn = strdup( argv[argc - 1] )) == NULL ) {
166                         perror( "strdup" );
167                         retval = EXIT_FAILURE;
168                         goto fail;
169                 }
170                 if (( entrydn = strdup( argv[argc - 2] )) == NULL ) {
171                         perror( "strdup" );
172                         retval = EXIT_FAILURE;
173                         goto fail;
174                 }
175                 ++havedn;
176         } else if ( argc - optind != 0 ) {
177                 fprintf( stderr, _("%s: invalid number of arguments (%d), only two allowed\n"), prog, argc-optind );
178                 usage();
179         }
180
181         if ( infile != NULL ) {
182                 if (( fp = fopen( infile, "r" )) == NULL ) {
183                         perror( infile );
184                         retval = EXIT_FAILURE;
185                         goto fail;
186                 }
187         } else {
188                 fp = stdin;
189         }
190
191         ld = tool_conn_setup( 0, 0 );
192
193         if ( pw_file || want_bindpw ) {
194                 if ( pw_file ) {
195                         rc = lutil_get_filed_password( pw_file, &passwd );
196                         if( rc ) {
197                                 retval = EXIT_FAILURE;
198                                 goto fail;
199                         }
200                 } else {
201                         passwd.bv_val = getpassphrase( _("Enter LDAP Password: ") );
202                         passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
203                 }
204         }
205
206         tool_bind( ld );
207
208         tool_server_controls( ld, NULL, 0 );
209
210         retval = rc = 0;
211         if (havedn)
212                 retval = domodrdn( ld, entrydn, rdn, newSuperior, remove_old_RDN );
213         else while ((rc == 0 || contoper) && fgets(buf, sizeof(buf), fp) != NULL) {
214                 if ( *buf != '\n' ) {   /* blank lines optional, skip */
215                         buf[ strlen( buf ) - 1 ] = '\0';        /* remove nl */
216
217                         if ( havedn ) { /* have DN, get RDN */
218                                 if (( rdn = strdup( buf )) == NULL ) {
219                                         perror( "strdup" );
220                                         retval = EXIT_FAILURE;
221                                         goto fail;
222                                 }
223                                 rc = domodrdn(ld, entrydn, rdn, newSuperior, remove_old_RDN );
224                                 if ( rc != 0 )
225                                         retval = rc;
226                                 havedn = 0;
227                                 free( rdn ); rdn = NULL;
228                                 free( entrydn ); entrydn = NULL;
229                         } else if ( !havedn ) { /* don't have DN yet */
230                                 if (( entrydn = strdup( buf )) == NULL ) {
231                                         retval = EXIT_FAILURE;
232                                         goto fail;
233                                 }
234                                 ++havedn;
235                         }
236                 }
237         }
238
239         tool_unbind( ld );
240         tool_destroy();
241 fail:
242         if ( fp && fp != stdin ) fclose( fp );
243         if ( entrydn ) free( entrydn );
244         if ( rdn ) free( rdn );
245         return( retval );
246 }
247
248 static int domodrdn(
249         LDAP    *ld,
250         char    *dn,
251         char    *rdn,
252         char    *newSuperior,
253         int             remove ) /* flag: remove old RDN */
254 {
255         int rc, code, id;
256         char *matcheddn=NULL, *text=NULL, **refs=NULL;
257         LDAPControl **ctrls = NULL;
258         LDAPMessage *res;
259
260         if ( verbose ) {
261                 printf( _("Renaming \"%s\"\n"), dn );
262                 printf( _("\tnew rdn=\"%s\" (%s old rdn)\n"),
263                         rdn, remove ? _("delete") : _("keep") );
264                 if( newSuperior != NULL ) {
265                         printf(_("\tnew parent=\"%s\"\n"), newSuperior);
266                 }
267         }
268
269         if( dont ) return LDAP_SUCCESS;
270
271         rc = ldap_rename( ld, dn, rdn, newSuperior, remove,
272                 NULL, NULL, &id );
273
274         if ( rc != LDAP_SUCCESS ) {
275                 fprintf( stderr, "%s: ldap_rename: %s (%d)\n",
276                         prog, ldap_err2string( rc ), rc );
277                 return rc;
278         }
279
280         for ( ; ; ) {
281                 struct timeval  tv = { 0, 0 };
282
283                 if ( tool_check_abandon( ld, id ) ) {
284                         return LDAP_CANCELLED;
285                 }
286
287                 tv.tv_sec = 0;
288                 tv.tv_usec = 100000;
289
290                 rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, &tv, &res );
291                 if ( rc < 0 ) {
292                         tool_perror( "ldap_result", rc, NULL, NULL, NULL, NULL );
293                         return rc;
294                 }
295
296                 if ( rc != 0 ) {
297                         break;
298                 }
299         }
300
301         rc = ldap_parse_result( ld, res, &code, &matcheddn, &text, &refs, &ctrls, 1 );
302
303         if( rc != LDAP_SUCCESS ) {
304                 fprintf( stderr, "%s: ldap_parse_result: %s (%d)\n",
305                         prog, ldap_err2string( rc ), rc );
306                 return rc;
307         }
308
309         if( verbose || code != LDAP_SUCCESS ||
310                 (matcheddn && *matcheddn) || (text && *text) || (refs && *refs) )
311         {
312                 printf( _("Rename Result: %s (%d)\n"),
313                         ldap_err2string( code ), code );
314
315                 if( text && *text ) {
316                         printf( _("Additional info: %s\n"), text );
317                 }
318
319                 if( matcheddn && *matcheddn ) {
320                         printf( _("Matched DN: %s\n"), matcheddn );
321                 }
322
323                 if( refs ) {
324                         int i;
325                         for( i=0; refs[i]; i++ ) {
326                                 printf(_("Referral: %s\n"), refs[i] );
327                         }
328                 }
329         }
330
331         if (ctrls) {
332                 tool_print_ctrls( ld, ctrls );
333                 ldap_controls_free( ctrls );
334         }
335
336         ber_memfree( text );
337         ber_memfree( matcheddn );
338         ber_memvfree( (void **) refs );
339
340         return code;
341 }