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