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 */
52 fprintf( stderr, _("Rename LDAP entries\n\n"));
53 fprintf( stderr, _("usage: %s [options] [dn rdn]\n"), prog);
54 fprintf( stderr, _(" dn rdn: If given, rdn will replace the RDN of the entry specified by DN\n"));
55 fprintf( stderr, _(" If not given, the list of modifications is read from stdin or\n"));
56 fprintf( stderr, _(" from the file specified by \"-f file\" (see man page).\n"));
57 fprintf( stderr, _("Rename options:\n"));
58 fprintf( stderr, _(" -r remove old RDN\n"));
59 fprintf( stderr, _(" -s newsup new superior entry\n"));
65 const char options[] = "rs:"
66 "cCd:D:e:f:h:H:IkKMnO:p:P:QR:U:vVw:WxX:y:Y:Z";
69 handle_private_option( int i )
74 char *control, *cvalue;
75 case 'E': /* modrdn controls */
76 if( protocol == LDAP_VERSION2 ) {
77 fprintf( stderr, _("%s: -E incompatible with LDAPv%d\n"),
82 /* should be extended to support comma separated list of
83 * [!]key[=value] parameters, e.g. -E !foo,bar=567
88 if( optarg[0] == '!' ) {
93 control = strdup( optarg );
94 if ( (cvalue = strchr( control, '=' )) != NULL ) {
97 fprintf( stderr, _("Invalid modrdn control name: %s\n"), control );
101 case 'r': /* remove old RDN */
105 case 's': /* newSuperior */
106 if( protocol == LDAP_VERSION2 ) {
107 fprintf( stderr, _("%s: -X incompatible with LDAPv%d\n"),
109 exit( EXIT_FAILURE );
111 newSuperior = strdup( optarg );
112 protocol = LDAP_VERSION3;
123 main(int argc, char **argv)
125 char *entrydn = NULL, *rdn = NULL, buf[ 4096 ];
128 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), only two allowed\n"), prog, argc-optind );
151 if ( infile != NULL ) {
152 if (( fp = fopen( infile, "r" )) == NULL ) {
154 return( EXIT_FAILURE );
160 ld = tool_conn_setup( 0, 0 );
162 if ( pw_file || want_bindpw ) {
164 rc = lutil_get_filed_password( pw_file, &passwd );
165 if( rc ) return EXIT_FAILURE;
167 passwd.bv_val = getpassphrase( _("Enter LDAP Password: ") );
168 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
174 if ( assertion || authzid || manageDSAit || noop ) {
175 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 );