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