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