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"
38 static char *newSuperior = NULL;
39 static int remove_old_RDN = 0;
47 int remove ); /* flag: remove old RDN */
53 "Rename LDAP entries\n\n"
54 "usage: %s [options] [dn rdn]\n"
55 " dn rdn: If given, rdn will replace the RDN of the entry specified by DN\n"
56 " If not given, the list of modifications is read from stdin or\n"
57 " from the file specified by \"-f file\" (see man page).\n"
59 " -r remove old RDN\n"
60 " -s newsup new superior entry\n"
67 const char options[] = "rs:"
68 "cCd:D:e:f:h:H:IkKMnO:p:P:QR:U:vVw:WxX:y:Y:Z";
71 handle_private_option( int i )
76 char *control, *cvalue;
77 case 'E': /* modrdn controls */
78 if( protocol == LDAP_VERSION2 ) {
79 fprintf( stderr, "%s: -E incompatible with LDAPv%d\n",
84 /* should be extended to support comma separated list of
85 * [!]key[=value] parameters, e.g. -E !foo,bar=567
90 if( optarg[0] == '!' ) {
95 control = strdup( optarg );
96 if ( (cvalue = strchr( control, '=' )) != NULL ) {
99 fprintf( stderr, "Invalid modrdn control name: %s\n", control );
103 case 'r': /* remove old RDN */
107 case 's': /* newSuperior */
108 if( protocol == LDAP_VERSION2 ) {
109 fprintf( stderr, "%s: -X incompatible with LDAPv%d\n",
111 exit( EXIT_FAILURE );
113 newSuperior = strdup( optarg );
114 protocol = LDAP_VERSION3;
125 main(int argc, char **argv)
127 char *entrydn = NULL, *rdn = NULL, buf[ 4096 ];
130 int rc, retval, havedn;
132 prog = lutil_progname( "ldapmodrdn", argc, argv );
134 tool_args( argc, argv );
137 if (argc - optind == 2) {
138 if (( rdn = strdup( argv[argc - 1] )) == NULL ) {
140 return( EXIT_FAILURE );
142 if (( entrydn = strdup( argv[argc - 2] )) == NULL ) {
144 return( EXIT_FAILURE );
147 } else if ( argc - optind != 0 ) {
148 fprintf( stderr, "%s: invalid number of arguments (%d), "
149 "only two allowed\n", prog, argc-optind );
153 if ( infile != NULL ) {
154 if (( fp = fopen( infile, "r" )) == NULL ) {
156 return( EXIT_FAILURE );
162 ld = tool_conn_setup( 0, 0 );
164 if ( pw_file || want_bindpw ) {
166 rc = lutil_get_filed_password( pw_file, &passwd );
167 if( rc ) return EXIT_FAILURE;
169 passwd.bv_val = getpassphrase( "Enter LDAP Password: " );
170 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
176 if ( authzid || manageDSAit || noop )
177 tool_server_controls( ld, NULL, 0 );
181 retval = domodrdn( ld, entrydn, rdn, newSuperior, remove_old_RDN );
182 else while ((rc == 0 || contoper) && fgets(buf, sizeof(buf), fp) != NULL) {
183 if ( *buf != '\0' ) { /* blank lines optional, skip */
184 buf[ strlen( buf ) - 1 ] = '\0'; /* remove nl */
186 if ( havedn ) { /* have DN, get RDN */
187 if (( rdn = strdup( buf )) == NULL ) {
189 return( EXIT_FAILURE );
191 rc = domodrdn(ld, entrydn, rdn, newSuperior, remove_old_RDN );
195 } else if ( !havedn ) { /* don't have DN yet */
196 if (( entrydn = strdup( buf )) == NULL ) {
198 return( EXIT_FAILURE );
215 int remove ) /* flag: remove old RDN */
218 char *matcheddn=NULL, *text=NULL, **refs=NULL;
222 printf( "Renaming \"%s\"\n", dn );
223 printf( "\tnew rdn=\"%s\" (%s old rdn)\n",
224 rdn, remove ? "delete" : "keep" );
225 if( newSuperior != NULL ) {
226 printf("\tnew parent=\"%s\"\n", newSuperior);
230 if( not ) return LDAP_SUCCESS;
232 rc = ldap_rename( ld, dn, rdn, newSuperior, remove,
235 if ( rc != LDAP_SUCCESS ) {
236 fprintf( stderr, "%s: ldap_rename: %s (%d)\n",
237 prog, ldap_err2string( rc ), rc );
241 rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, NULL, &res );
243 ldap_perror( ld, "ldapmodrdn: ldap_result" );
247 rc = ldap_parse_result( ld, res, &code, &matcheddn, &text, &refs, NULL, 1 );
249 if( rc != LDAP_SUCCESS ) {
250 fprintf( stderr, "%s: ldap_parse_result: %s (%d)\n",
251 prog, ldap_err2string( rc ), rc );
255 if( verbose || code != LDAP_SUCCESS ||
256 (matcheddn && *matcheddn) || (text && *text) || (refs && *refs) )
258 printf( "Rename Result: %s (%d)\n",
259 ldap_err2string( code ), code );
261 if( text && *text ) {
262 printf( "Additional info: %s\n", text );
265 if( matcheddn && *matcheddn ) {
266 printf( "Matched DN: %s\n", matcheddn );
271 for( i=0; refs[i]; i++ ) {
272 printf("Referral: %s\n", refs[i] );
278 ber_memfree( matcheddn );
279 ber_memvfree( (void **) refs );