]> git.sur5r.net Git - openldap/blob - clients/tools/ldapmodrdn.c
Happy new year! (belated)
[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-2008 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/time.h>
56
57 #include <ldap.h>
58 #include "lutil.h"
59 #include "lutil_ldap.h"
60 #include "ldap_defaults.h"
61
62 #include "common.h"
63
64
65 static char     *newSuperior = NULL;
66 static int   remove_old_RDN = 0;
67
68
69 static int domodrdn(
70     LDAP        *ld,
71     char        *dn,
72     char        *rdn,
73     char        *newSuperior,
74     int         remove );       /* flag: remove old RDN */
75
76 void
77 usage( void )
78 {
79         fprintf( stderr, _("Rename LDAP entries\n\n"));
80         fprintf( stderr, _("usage: %s [options] [dn rdn]\n"), prog);
81         fprintf( stderr, _("    dn rdn: If given, rdn will replace the RDN of the entry specified by DN\n"));
82         fprintf( stderr, _("            If not given, the list of modifications is read from stdin or\n"));
83         fprintf( stderr, _("            from the file specified by \"-f file\" (see man page).\n"));
84         fprintf( stderr, _("Rename options:\n"));
85         fprintf( stderr, _("  -r         remove old RDN\n"));
86         fprintf( stderr, _("  -s newsup  new superior entry\n"));
87         tool_common_usage();
88         exit( EXIT_FAILURE );
89 }
90
91
92 const char options[] = "rs:"
93         "cd:D:e:f:h:H:IkKMnO:p:P:QR:U:vVw:WxX:y:Y:Z";
94
95 int
96 handle_private_option( int i )
97 {
98         switch ( i ) {
99 #if 0
100                 int crit;
101                 char *control, *cvalue;
102         case 'E': /* modrdn extensions */
103                 if( protocol == LDAP_VERSION2 ) {
104                         fprintf( stderr, _("%s: -E incompatible with LDAPv%d\n"),
105                                 prog, version );
106                         exit( EXIT_FAILURE );
107                 }
108
109                 /* should be extended to support comma separated list of
110                  *      [!]key[=value] parameters, e.g.  -E !foo,bar=567
111                  */
112
113                 crit = 0;
114                 cvalue = NULL;
115                 if( optarg[0] == '!' ) {
116                         crit = 1;
117                         optarg++;
118                 }
119
120                 control = strdup( optarg );
121                 if ( (cvalue = strchr( control, '=' )) != NULL ) {
122                         *cvalue++ = '\0';
123                 }
124                 fprintf( stderr, _("Invalid modrdn extension name: %s\n"), control );
125                 usage();
126 #endif
127
128         case 'r':       /* remove old RDN */
129             remove_old_RDN++;
130             break;
131
132         case 's':       /* newSuperior */
133                 if( protocol == LDAP_VERSION2 ) {
134                         fprintf( stderr, _("%s: -X incompatible with LDAPv%d\n"),
135                                 prog, protocol );
136                         exit( EXIT_FAILURE );
137                 }
138             newSuperior = strdup( optarg );
139             protocol = LDAP_VERSION3;
140             break;
141
142         default:
143                 return 0;
144         }
145         return 1;
146 }
147
148
149 int
150 main(int argc, char **argv)
151 {
152     char                *entrydn = NULL, *rdn = NULL, buf[ 4096 ];
153     FILE                *fp;
154     LDAP                *ld;
155         int             rc, retval, havedn;
156
157     tool_init();
158     prog = lutil_progname( "ldapmodrdn", argc, argv );
159
160         tool_args( argc, argv );
161
162     havedn = 0;
163     if (argc - optind == 2) {
164         if (( rdn = strdup( argv[argc - 1] )) == NULL ) {
165             perror( "strdup" );
166             return( EXIT_FAILURE );
167         }
168         if (( entrydn = strdup( argv[argc - 2] )) == NULL ) {
169             perror( "strdup" );
170             return( EXIT_FAILURE );
171         }
172         ++havedn;
173     } else if ( argc - optind != 0 ) {
174         fprintf( stderr, _("%s: invalid number of arguments (%d), only two allowed\n"), prog, argc-optind );
175         usage();
176     }
177
178     if ( infile != NULL ) {
179         if (( fp = fopen( infile, "r" )) == NULL ) {
180             perror( infile );
181             return( EXIT_FAILURE );
182         }
183     } else {
184         fp = stdin;
185     }
186
187         ld = tool_conn_setup( 0, 0 );
188
189         if ( pw_file || want_bindpw ) {
190                 if ( pw_file ) {
191                         rc = lutil_get_filed_password( pw_file, &passwd );
192                         if( rc ) return EXIT_FAILURE;
193                 } else {
194                         passwd.bv_val = getpassphrase( _("Enter LDAP Password: ") );
195                         passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
196                 }
197         }
198
199         tool_bind( ld );
200
201         if ( assertion || authzid || manageDSAit || noop ) {
202                 tool_server_controls( ld, NULL, 0 );
203         }
204
205     retval = rc = 0;
206     if (havedn)
207         retval = domodrdn( ld, entrydn, rdn, newSuperior, remove_old_RDN );
208     else while ((rc == 0 || contoper) && fgets(buf, sizeof(buf), fp) != NULL) {
209         if ( *buf != '\n' ) {   /* blank lines optional, skip */
210             buf[ strlen( buf ) - 1 ] = '\0';    /* remove nl */
211
212             if ( havedn ) {     /* have DN, get RDN */
213                 if (( rdn = strdup( buf )) == NULL ) {
214                     perror( "strdup" );
215                     return( EXIT_FAILURE );
216                 }
217                 rc = domodrdn(ld, entrydn, rdn, newSuperior, remove_old_RDN );
218                 if ( rc != 0 )
219                         retval = rc;
220                 havedn = 0;
221             } else if ( !havedn ) {     /* don't have DN yet */
222                 if (( entrydn = strdup( buf )) == NULL ) {
223                     perror( "strdup" );
224                     return( EXIT_FAILURE );
225                 }
226                 ++havedn;
227             }
228         }
229     }
230
231         tool_unbind( ld );
232         tool_destroy();
233     return( retval );
234 }
235
236 static int domodrdn(
237     LDAP        *ld,
238     char        *dn,
239     char        *rdn,
240     char        *newSuperior,
241     int         remove ) /* flag: remove old RDN */
242 {
243         int rc, code, id;
244         char *matcheddn=NULL, *text=NULL, **refs=NULL;
245         LDAPControl **ctrls = NULL;
246         LDAPMessage *res;
247
248     if ( verbose ) {
249                 printf( _("Renaming \"%s\"\n"), dn );
250                 printf( _("\tnew rdn=\"%s\" (%s old rdn)\n"),
251                         rdn, remove ? _("delete") : _("keep") );
252                 if( newSuperior != NULL ) {
253                         printf(_("\tnew parent=\"%s\"\n"), newSuperior);
254                 }
255         }
256
257         if( not ) return LDAP_SUCCESS;
258
259         rc = ldap_rename( ld, dn, rdn, newSuperior, remove,
260                 NULL, NULL, &id );
261
262         if ( rc != LDAP_SUCCESS ) {
263                 fprintf( stderr, "%s: ldap_rename: %s (%d)\n",
264                         prog, ldap_err2string( rc ), rc );
265                 return rc;
266         }
267
268         for ( ; ; ) {
269                 struct timeval  tv = { 0, 0 };
270
271                 if ( tool_check_abandon( ld, id ) ) {
272                         return LDAP_CANCELLED;
273                 }
274
275                 tv.tv_sec = 0;
276                 tv.tv_usec = 100000;
277
278                 rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, &tv, &res );
279                 if ( rc < 0 ) {
280                         ldap_perror( ld, "ldapmodrdn: ldap_result" );
281                         return rc;
282                 }
283
284                 if ( rc != 0 ) {
285                         break;
286                 }
287         }
288
289         rc = ldap_parse_result( ld, res, &code, &matcheddn, &text, &refs, &ctrls, 1 );
290
291         if( rc != LDAP_SUCCESS ) {
292                 fprintf( stderr, "%s: ldap_parse_result: %s (%d)\n",
293                         prog, ldap_err2string( rc ), rc );
294                 return rc;
295         }
296
297         if( verbose || code != LDAP_SUCCESS ||
298                 (matcheddn && *matcheddn) || (text && *text) || (refs && *refs) )
299         {
300                 printf( _("Rename Result: %s (%d)\n"),
301                         ldap_err2string( code ), code );
302
303                 if( text && *text ) {
304                         printf( _("Additional info: %s\n"), text );
305                 }
306
307                 if( matcheddn && *matcheddn ) {
308                         printf( _("Matched DN: %s\n"), matcheddn );
309                 }
310
311                 if( refs ) {
312                         int i;
313                         for( i=0; refs[i]; i++ ) {
314                                 printf(_("Referral: %s\n"), refs[i] );
315                         }
316                 }
317         }
318
319         if (ctrls) {
320                 tool_print_ctrls( ld, ctrls );
321                 ldap_controls_free( ctrls );
322     }
323
324         ber_memfree( text );
325         ber_memfree( matcheddn );
326         ber_memvfree( (void **) refs );
327
328         return code;
329 }