3 * Copyright 1998-2002 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:" "cCd:D:e:f:h:H:IkKMnO:p:P:QR:U:vw:WxX:y:Y:Z";
70 handle_private_option( int i )
75 char *control, *cvalue;
76 case 'E': /* modrdn controls */
77 if( version == LDAP_VERSION2 ) {
78 fprintf( stderr, "%s: -E incompatible with LDAPv%d\n",
83 /* should be extended to support comma separated list of
84 * [!]key[=value] parameters, e.g. -E !foo,bar=567
89 if( optarg[0] == '!' ) {
94 control = strdup( optarg );
95 if ( (cvalue = strchr( control, '=' )) != NULL ) {
98 fprintf( stderr, "Invalid modrdn control name: %s\n", control );
102 case 'r': /* remove old RDN */
106 case 's': /* newSuperior */
107 if( version == LDAP_VERSION2 ) {
108 fprintf( stderr, "%s: -X incompatible with LDAPv%d\n",
110 exit( EXIT_FAILURE );
112 newSuperior = strdup( optarg );
113 version = LDAP_VERSION3;
124 main(int argc, char **argv)
126 char *entrydn = NULL, *rdn = NULL, buf[ 4096 ];
129 int rc, retval, havedn;
131 prog = lutil_progname( "ldapmodrdn", argc, argv );
133 tool_args( argc, argv );
136 if (argc - optind == 2) {
137 if (( rdn = strdup( argv[argc - 1] )) == NULL ) {
139 return( EXIT_FAILURE );
141 if (( entrydn = strdup( argv[argc - 2] )) == NULL ) {
143 return( EXIT_FAILURE );
146 } else if ( argc - optind != 0 ) {
147 fprintf( stderr, "%s: invalid number of arguments (%d), "
148 "only two allowed\n", prog, argc-optind );
152 if ( infile != NULL ) {
153 if (( fp = fopen( infile, "r" )) == NULL ) {
155 return( EXIT_FAILURE );
161 ld = tool_conn_setup( 0, 0 );
163 if ( pw_file || want_bindpw ) {
165 rc = lutil_get_filed_password( pw_file, &passwd );
166 if( rc ) return EXIT_FAILURE;
168 passwd.bv_val = getpassphrase( "Enter LDAP Password: " );
169 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
175 if ( authzid || manageDSAit || noop )
176 tool_server_controls( ld, NULL, 0 );
180 retval = domodrdn( ld, entrydn, rdn, newSuperior, remove_old_RDN );
181 else while ((rc == 0 || contoper) && fgets(buf, sizeof(buf), fp) != NULL) {
182 if ( *buf != '\0' ) { /* blank lines optional, skip */
183 buf[ strlen( buf ) - 1 ] = '\0'; /* remove nl */
185 if ( havedn ) { /* have DN, get RDN */
186 if (( rdn = strdup( buf )) == NULL ) {
188 return( EXIT_FAILURE );
190 rc = domodrdn(ld, entrydn, rdn, newSuperior, remove_old_RDN );
194 } else if ( !havedn ) { /* don't have DN yet */
195 if (( entrydn = strdup( buf )) == NULL ) {
197 return( EXIT_FAILURE );
214 int remove ) /* flag: remove old RDN */
217 char *matcheddn=NULL, *text=NULL, **refs=NULL;
221 printf( "Renaming \"%s\"\n", dn );
222 printf( "\tnew rdn=\"%s\" (%s old rdn)\n",
223 rdn, remove ? "delete" : "keep" );
224 if( newSuperior != NULL ) {
225 printf("\tnew parent=\"%s\"\n", newSuperior);
229 if( not ) return LDAP_SUCCESS;
231 rc = ldap_rename( ld, dn, rdn, newSuperior, remove,
234 if ( rc != LDAP_SUCCESS ) {
235 fprintf( stderr, "%s: ldap_rename: %s (%d)\n",
236 prog, ldap_err2string( rc ), rc );
240 rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, NULL, &res );
242 ldap_perror( ld, "ldapmodrdn: ldap_result" );
246 rc = ldap_parse_result( ld, res, &code, &matcheddn, &text, &refs, NULL, 1 );
248 if( rc != LDAP_SUCCESS ) {
249 fprintf( stderr, "%s: ldap_parse_result: %s (%d)\n",
250 prog, ldap_err2string( rc ), rc );
254 if( verbose || code != LDAP_SUCCESS ||
255 (matcheddn && *matcheddn) || (text && *text) || (refs && *refs) )
257 printf( "Rename Result: %s (%d)\n",
258 ldap_err2string( code ), code );
260 if( text && *text ) {
261 printf( "Additional info: %s\n", text );
264 if( matcheddn && *matcheddn ) {
265 printf( "Matched DN: %s\n", matcheddn );
270 for( i=0; refs[i]; i++ ) {
271 printf("Referral: %s\n", refs[i] );
277 ber_memfree( matcheddn );
278 ber_memvfree( (void **) refs );