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