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-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. 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#include <ac/socket.h> 56#include <ac/time.h> 57 58#include <ldap.h> 59#include "lutil.h" 60#include "lutil_ldap.h" 61#include "ldap_defaults.h" 62 63#include "common.h" 64 65 66static char *newSuperior = NULL; 67static int remove_old_RDN = 0; 68 69 70static int domodrdn( 71 LDAP *ld, 72 char *dn, 73 char *rdn, 74 char *newSuperior, 75 int remove ); /* flag: remove old RDN */ 76 77void 78usage( void ) 79{ 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")); 92 tool_common_usage(); 93 exit( EXIT_FAILURE ); 94} 95 96 97const char options[] = "rs:" 98 "cd:D:e:f:h:H:IMnNO:o:p:P:QR:U:vVw:WxX:y:Y:Z"; 99 100int 101handle_private_option( int i ) 102{ 103 switch ( i ) { 104#if 0 105 int crit; 106 char *control, *cvalue; 107 case 'E': /* modrdn extensions */ 108 if( protocol == LDAP_VERSION2 ) { 109 fprintf( stderr, _("%s: -E incompatible with LDAPv%d\n"), 110 prog, version ); 111 exit( EXIT_FAILURE ); 112 } 113 114 /* should be extended to support comma separated list of 115 * [!]key[=value] parameters, e.g. -E !foo,bar=567 116 */ 117 118 crit = 0; 119 cvalue = NULL; 120 if( optarg[0] == '!' ) { 121 crit = 1; 122 optarg++; 123 } 124 125 control = strdup( optarg ); 126 if ( (cvalue = strchr( control, '=' )) != NULL ) { 127 *cvalue++ = '\0'; 128 } 129 fprintf( stderr, _("Invalid modrdn extension name: %s\n"), control ); 130 usage(); 131#endif 132 133 case 'r': /* remove old RDN */ 134 remove_old_RDN++; 135 break; 136 137 case 's': /* newSuperior */ 138 if( protocol == LDAP_VERSION2 ) { 139 fprintf( stderr, _("%s: -X incompatible with LDAPv%d\n"), 140 prog, protocol ); 141 exit( EXIT_FAILURE ); 142 } 143 newSuperior = strdup( optarg ); 144 protocol = LDAP_VERSION3; 145 break; 146 147 default: 148 return 0; 149 } 150 return 1; 151} 152 153 154int 155main(int argc, char **argv) 156{ 157 char *entrydn = NULL, *rdn = NULL, buf[ 4096 ]; 158 FILE *fp = NULL; 159 LDAP *ld = NULL; 160 int rc, retval, havedn; 161 162 tool_init( TOOL_MODRDN ); 163 prog = lutil_progname( "ldapmodrdn", argc, argv ); 164 165 tool_args( argc, argv ); 166 167 havedn = 0; 168 if (argc - optind == 2) { 169 if (( rdn = strdup( argv[argc - 1] )) == NULL ) { 170 perror( "strdup" ); 171 retval = EXIT_FAILURE; 172 goto fail; 173 } 174 if (( entrydn = strdup( argv[argc - 2] )) == NULL ) { 175 perror( "strdup" ); 176 retval = EXIT_FAILURE; 177 goto fail; 178 } 179 ++havedn; 180 } else if ( argc - optind != 0 ) { 181 fprintf( stderr, _("%s: invalid number of arguments (%d), only two allowed\n"), prog, argc-optind ); 182 usage(); 183 } 184 185 if ( infile != NULL ) { 186 if (( fp = fopen( infile, "r" )) == NULL ) { 187 perror( infile ); 188 retval = EXIT_FAILURE; 189 goto fail; 190 } 191 } else { 192 fp = stdin; 193 } 194 195 ld = tool_conn_setup( 0, 0 ); 196 197 tool_bind( ld ); 198 199 tool_server_controls( ld, NULL, 0 ); 200 201 retval = rc = 0; 202 if (havedn) 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 */ 207 208 if ( havedn ) { /* have DN, get RDN */ 209 if (( rdn = strdup( buf )) == NULL ) { 210 perror( "strdup" ); 211 retval = EXIT_FAILURE; 212 goto fail; 213 } 214 rc = domodrdn(ld, entrydn, rdn, newSuperior, remove_old_RDN ); 215 if ( rc != 0 ) 216 retval = rc; 217 havedn = 0; 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; 223 goto fail; 224 } 225 ++havedn; 226 } 227 } 228 } 229 230fail: 231 if ( fp && fp != stdin ) fclose( fp ); 232 if ( entrydn ) free( entrydn ); 233 if ( rdn ) free( rdn ); 234 tool_exit( ld, retval ); 235} 236 237static int domodrdn( 238 LDAP *ld, 239 char *dn, 240 char *rdn, 241 char *newSuperior, 242 int remove ) /* flag: remove old RDN */ 243{ 244 int rc, code, id; 245 char *matcheddn=NULL, *text=NULL, **refs=NULL; 246 LDAPControl **ctrls = NULL; 247 LDAPMessage *res; 248 249 if ( verbose ) { 250 printf( _("Renaming \"%s\"\n"), dn ); 251 printf( _("\tnew rdn=\"%s\" (%s old rdn)\n"), 252 rdn, remove ? _("delete") : _("keep") ); 253 if( newSuperior != NULL ) { 254 printf(_("\tnew parent=\"%s\"\n"), newSuperior); 255 } 256 } 257 258 if( dont ) return LDAP_SUCCESS; 259 260 rc = ldap_rename( ld, dn, rdn, newSuperior, remove, 261 NULL, NULL, &id ); 262 263 if ( rc != LDAP_SUCCESS ) { 264 fprintf( stderr, "%s: ldap_rename: %s (%d)\n", 265 prog, ldap_err2string( rc ), rc ); 266 return rc; 267 } 268 269 for ( ; ; ) { 270 struct timeval tv = { 0, 0 }; 271 272 if ( tool_check_abandon( ld, id ) ) { 273 return LDAP_CANCELLED; 274 } 275 276 tv.tv_sec = 0; 277 tv.tv_usec = 100000; 278 279 rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, &tv, &res ); 280 if ( rc < 0 ) { 281 tool_perror( "ldap_result", rc, NULL, NULL, NULL, NULL ); 282 return rc; 283 } 284 285 if ( rc != 0 ) { 286 break; 287 } 288 } 289 290 rc = ldap_parse_result( ld, res, &code, &matcheddn, &text, &refs, &ctrls, 1 ); 291 292 if( rc != LDAP_SUCCESS ) { 293 fprintf( stderr, "%s: ldap_parse_result: %s (%d)\n", 294 prog, ldap_err2string( rc ), rc ); 295 return rc; 296 } 297 298 if( verbose || code != LDAP_SUCCESS || 299 (matcheddn && *matcheddn) || (text && *text) || (refs && *refs) ) 300 { 301 printf( _("Rename Result: %s (%d)\n"), 302 ldap_err2string( code ), code ); 303 304 if( text && *text ) { 305 printf( _("Additional info: %s\n"), text ); 306 } 307 308 if( matcheddn && *matcheddn ) { 309 printf( _("Matched DN: %s\n"), matcheddn ); 310 } 311 312 if( refs ) { 313 int i; 314 for( i=0; refs[i]; i++ ) { 315 printf(_("Referral: %s\n"), refs[i] ); 316 } 317 } 318 } 319 320 if (ctrls) { 321 tool_print_ctrls( ld, ctrls ); 322 ldap_controls_free( ctrls ); 323 } 324 325 ber_memfree( text ); 326 ber_memfree( matcheddn ); 327 ber_memvfree( (void **) refs ); 328 329 return code; 330} 331