3 * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6 /* ldapmodrdn.c - generic program to modify an entry's RDN using LDAP.
8 * Support for MODIFYDN REQUEST V3 (newSuperior) by:
10 * Copyright 1999, Juan C. Gomez, All rights reserved.
11 * This software is not subject to any license of Silicon Graphics
12 * Inc. or Purdue University.
14 * Redistribution and use in source and binary forms are permitted
15 * without restriction or fee of any kind as long as this notice
24 #include <ac/stdlib.h>
27 #include <ac/string.h>
28 #include <ac/unistd.h>
32 #include "lutil_ldap.h"
33 #include "ldap_defaults.h"
37 #define _OLV_APP "ldapmodrdn"
39 #include "ol_version.h"
42 static char *newSuperior = NULL;
43 static int remove_old_RDN = 0;
51 int remove ); /* flag: remove old RDN */
57 "Rename LDAP entries\n\n"
58 "usage: %s [options] [dn rdn]\n"
59 " dn rdn: If given, rdn will replace the RDN of the entry specified by DN\n"
60 " If not given, the list of modifications is read from stdin or\n"
61 " from the file specified by \"-f file\" (see man page).\n"
63 " -r remove old RDN\n"
64 " -s newsup new superior entry\n"
71 const char options[] = "rs:"
72 "cCd:D:e:f:h:H:IkKMnO:p:P:QR:U:vVw:WxX:y:Y:Z";
75 handle_private_option( int i )
80 char *control, *cvalue;
81 case 'E': /* modrdn controls */
82 if( protocol == LDAP_VERSION2 ) {
83 fprintf( stderr, "%s: -E incompatible with LDAPv%d\n",
88 /* should be extended to support comma separated list of
89 * [!]key[=value] parameters, e.g. -E !foo,bar=567
94 if( optarg[0] == '!' ) {
99 control = strdup( optarg );
100 if ( (cvalue = strchr( control, '=' )) != NULL ) {
103 fprintf( stderr, "Invalid modrdn control name: %s\n", control );
107 case 'r': /* remove old RDN */
111 case 's': /* newSuperior */
112 if( protocol == LDAP_VERSION2 ) {
113 fprintf( stderr, "%s: -X incompatible with LDAPv%d\n",
115 exit( EXIT_FAILURE );
117 newSuperior = strdup( optarg );
118 protocol = LDAP_VERSION3;
129 main(int argc, char **argv)
131 char *entrydn = NULL, *rdn = NULL, buf[ 4096 ];
134 int rc, retval, havedn;
136 prog = lutil_progname( "ldapmodrdn", argc, argv );
138 tool_args( argc, argv );
141 if (argc - optind == 2) {
142 if (( rdn = strdup( argv[argc - 1] )) == NULL ) {
144 return( EXIT_FAILURE );
146 if (( entrydn = strdup( argv[argc - 2] )) == NULL ) {
148 return( EXIT_FAILURE );
151 } else if ( argc - optind != 0 ) {
152 fprintf( stderr, "%s: invalid number of arguments (%d), "
153 "only two allowed\n", prog, argc-optind );
157 if ( infile != NULL ) {
158 if (( fp = fopen( infile, "r" )) == NULL ) {
160 return( EXIT_FAILURE );
166 ld = tool_conn_setup( 0, 0 );
168 if ( pw_file || want_bindpw ) {
170 rc = lutil_get_filed_password( pw_file, &passwd );
171 if( rc ) return EXIT_FAILURE;
173 passwd.bv_val = getpassphrase( "Enter LDAP Password: " );
174 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
180 if ( authzid || manageDSAit || noop )
181 tool_server_controls( ld, NULL, 0 );
185 retval = domodrdn( ld, entrydn, rdn, newSuperior, remove_old_RDN );
186 else while ((rc == 0 || contoper) && fgets(buf, sizeof(buf), fp) != NULL) {
187 if ( *buf != '\0' ) { /* blank lines optional, skip */
188 buf[ strlen( buf ) - 1 ] = '\0'; /* remove nl */
190 if ( havedn ) { /* have DN, get RDN */
191 if (( rdn = strdup( buf )) == NULL ) {
193 return( EXIT_FAILURE );
195 rc = domodrdn(ld, entrydn, rdn, newSuperior, remove_old_RDN );
199 } else if ( !havedn ) { /* don't have DN yet */
200 if (( entrydn = strdup( buf )) == NULL ) {
202 return( EXIT_FAILURE );
219 int remove ) /* flag: remove old RDN */
222 char *matcheddn=NULL, *text=NULL, **refs=NULL;
226 printf( "Renaming \"%s\"\n", dn );
227 printf( "\tnew rdn=\"%s\" (%s old rdn)\n",
228 rdn, remove ? "delete" : "keep" );
229 if( newSuperior != NULL ) {
230 printf("\tnew parent=\"%s\"\n", newSuperior);
234 if( not ) return LDAP_SUCCESS;
236 rc = ldap_rename( ld, dn, rdn, newSuperior, remove,
239 if ( rc != LDAP_SUCCESS ) {
240 fprintf( stderr, "%s: ldap_rename: %s (%d)\n",
241 prog, ldap_err2string( rc ), rc );
245 rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, NULL, &res );
247 ldap_perror( ld, "ldapmodrdn: ldap_result" );
251 rc = ldap_parse_result( ld, res, &code, &matcheddn, &text, &refs, NULL, 1 );
253 if( rc != LDAP_SUCCESS ) {
254 fprintf( stderr, "%s: ldap_parse_result: %s (%d)\n",
255 prog, ldap_err2string( rc ), rc );
259 if( verbose || code != LDAP_SUCCESS ||
260 (matcheddn && *matcheddn) || (text && *text) || (refs && *refs) )
262 printf( "Rename Result: %s (%d)\n",
263 ldap_err2string( code ), code );
265 if( text && *text ) {
266 printf( "Additional info: %s\n", text );
269 if( matcheddn && *matcheddn ) {
270 printf( "Matched DN: %s\n", matcheddn );
275 for( i=0; refs[i]; i++ ) {
276 printf("Referral: %s\n", refs[i] );
282 ber_memfree( matcheddn );
283 ber_memvfree( (void **) refs );