1 /* ldapmodrdn.c - generic program to modify an entry's RDN using LDAP */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2011 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.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted only as authorized by the OpenLDAP
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>.
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.
23 * Redistribution and use in source and binary forms are permitted
24 * without restriction or fee of any kind as long as this notice
27 /* Portions Copyright (c) 1992-1996 Regents of the University of Michigan.
28 * All rights reserved.
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.
38 * This work was originally developed by the University of Michigan
39 * (as part of U-MICH LDAP). Additional significant contributors
50 #include <ac/stdlib.h>
53 #include <ac/string.h>
54 #include <ac/unistd.h>
55 #include <ac/socket.h>
60 #include "lutil_ldap.h"
61 #include "ldap_defaults.h"
66 static char *newSuperior = NULL;
67 static int remove_old_RDN = 0;
75 int remove ); /* flag: remove old RDN */
80 fprintf( stderr, _("Rename LDAP entries\n\n"));
81 fprintf( stderr, _("usage: %s [options] [dn rdn]\n"), prog);
82 fprintf( stderr, _(" dn rdn: If given, rdn will replace the RDN of the entry specified by DN\n"));
83 fprintf( stderr, _(" If not given, the list of modifications is read from stdin or\n"));
84 fprintf( stderr, _(" from the file specified by \"-f file\" (see man page).\n"));
85 fprintf( stderr, _("Rename options:\n"));
86 fprintf( stderr, _(" -c continuous operation mode (do not stop on errors)\n"));
87 fprintf( stderr, _(" -f file read operations from `file'\n"));
88 fprintf( stderr, _(" -M enable Manage DSA IT control (-MM to make critical)\n"));
89 fprintf( stderr, _(" -P version protocol version (default: 3)\n"));
90 fprintf( stderr, _(" -r remove old RDN\n"));
91 fprintf( stderr, _(" -s newsup new superior entry\n"));
97 const char options[] = "rs:"
98 "cd:D:e:f:h:H:IMnNO:o:p:P:QR:U:vVw:WxX:y:Y:Z";
101 handle_private_option( int i )
106 char *control, *cvalue;
107 case 'E': /* modrdn extensions */
108 if( protocol == LDAP_VERSION2 ) {
109 fprintf( stderr, _("%s: -E incompatible with LDAPv%d\n"),
111 exit( EXIT_FAILURE );
114 /* should be extended to support comma separated list of
115 * [!]key[=value] parameters, e.g. -E !foo,bar=567
120 if( optarg[0] == '!' ) {
125 control = strdup( optarg );
126 if ( (cvalue = strchr( control, '=' )) != NULL ) {
129 fprintf( stderr, _("Invalid modrdn extension name: %s\n"), control );
133 case 'r': /* remove old RDN */
137 case 's': /* newSuperior */
138 if( protocol == LDAP_VERSION2 ) {
139 fprintf( stderr, _("%s: -X incompatible with LDAPv%d\n"),
141 exit( EXIT_FAILURE );
143 newSuperior = strdup( optarg );
144 protocol = LDAP_VERSION3;
155 main(int argc, char **argv)
157 char *entrydn = NULL, *rdn = NULL, buf[ 4096 ];
160 int rc, retval, havedn;
162 tool_init( TOOL_MODRDN );
163 prog = lutil_progname( "ldapmodrdn", argc, argv );
165 tool_args( argc, argv );
168 if (argc - optind == 2) {
169 if (( rdn = strdup( argv[argc - 1] )) == NULL ) {
171 retval = EXIT_FAILURE;
174 if (( entrydn = strdup( argv[argc - 2] )) == NULL ) {
176 retval = EXIT_FAILURE;
180 } else if ( argc - optind != 0 ) {
181 fprintf( stderr, _("%s: invalid number of arguments (%d), only two allowed\n"), prog, argc-optind );
185 if ( infile != NULL ) {
186 if (( fp = fopen( infile, "r" )) == NULL ) {
188 retval = EXIT_FAILURE;
195 ld = tool_conn_setup( 0, 0 );
199 tool_server_controls( ld, NULL, 0 );
203 retval = domodrdn( ld, entrydn, rdn, newSuperior, remove_old_RDN );
204 else while ((rc == 0 || contoper) && fgets(buf, sizeof(buf), fp) != NULL) {
205 if ( *buf != '\n' ) { /* blank lines optional, skip */
206 buf[ strlen( buf ) - 1 ] = '\0'; /* remove nl */
208 if ( havedn ) { /* have DN, get RDN */
209 if (( rdn = strdup( buf )) == NULL ) {
211 retval = EXIT_FAILURE;
214 rc = domodrdn(ld, entrydn, rdn, newSuperior, remove_old_RDN );
218 free( rdn ); rdn = NULL;
219 free( entrydn ); entrydn = NULL;
220 } else if ( !havedn ) { /* don't have DN yet */
221 if (( entrydn = strdup( buf )) == NULL ) {
222 retval = EXIT_FAILURE;
233 if ( fp && fp != stdin ) fclose( fp );
234 if ( entrydn ) free( entrydn );
235 if ( rdn ) free( rdn );
244 int remove ) /* flag: remove old RDN */
247 char *matcheddn=NULL, *text=NULL, **refs=NULL;
248 LDAPControl **ctrls = NULL;
252 printf( _("Renaming \"%s\"\n"), dn );
253 printf( _("\tnew rdn=\"%s\" (%s old rdn)\n"),
254 rdn, remove ? _("delete") : _("keep") );
255 if( newSuperior != NULL ) {
256 printf(_("\tnew parent=\"%s\"\n"), newSuperior);
260 if( dont ) return LDAP_SUCCESS;
262 rc = ldap_rename( ld, dn, rdn, newSuperior, remove,
265 if ( rc != LDAP_SUCCESS ) {
266 fprintf( stderr, "%s: ldap_rename: %s (%d)\n",
267 prog, ldap_err2string( rc ), rc );
272 struct timeval tv = { 0, 0 };
274 if ( tool_check_abandon( ld, id ) ) {
275 return LDAP_CANCELLED;
281 rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, &tv, &res );
283 tool_perror( "ldap_result", rc, NULL, NULL, NULL, NULL );
292 rc = ldap_parse_result( ld, res, &code, &matcheddn, &text, &refs, &ctrls, 1 );
294 if( rc != LDAP_SUCCESS ) {
295 fprintf( stderr, "%s: ldap_parse_result: %s (%d)\n",
296 prog, ldap_err2string( rc ), rc );
300 if( verbose || code != LDAP_SUCCESS ||
301 (matcheddn && *matcheddn) || (text && *text) || (refs && *refs) )
303 printf( _("Rename Result: %s (%d)\n"),
304 ldap_err2string( code ), code );
306 if( text && *text ) {
307 printf( _("Additional info: %s\n"), text );
310 if( matcheddn && *matcheddn ) {
311 printf( _("Matched DN: %s\n"), matcheddn );
316 for( i=0; refs[i]; i++ ) {
317 printf(_("Referral: %s\n"), refs[i] );
323 tool_print_ctrls( ld, ctrls );
324 ldap_controls_free( ctrls );
328 ber_memfree( matcheddn );
329 ber_memvfree( (void **) refs );