2 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
3 * Use is subject to license terms.
6 #pragma ident "%Z%%M% %I% %E% SMI"
8 /* ldapmodrdn.c - generic program to modify an entry's RDN using LDAP */
20 static int contoper
, remove_oldrdn
;
23 static int domodrdn( LDAP
*ld
, char *dn
, char *rdn
, char *newsuperior
,
25 static void options_callback( int option
, char *optarg
);
27 static void usage( void )
29 fprintf(stderr
, gettext("usage: %s [options] [dn newrdn [newsuperior]]\n"), ldaptool_progname
);
30 fprintf( stderr
, gettext("options:\n"));
31 ldaptool_common_usage( 0 );
32 fprintf( stderr
, gettext(" -c\t\tcontinuous mode\n") );
33 fprintf( stderr
, gettext(" -r\t\tremove old RDN from the entries\n"));
34 fprintf( stderr
, gettext(" -f file\tread changes from `file'\n") );
35 exit(LDAP_PARAM_ERROR
);
39 main(int argc
, char **argv
)
41 char *myname
, *entrydn
, *rdn
, buf
[ 4096 ];
42 int rc
, havedn
, deref
, optind
;
43 char * L_newParent
= NULL
;
46 int L_protoVersion
= LDAP_VERSION3
;
48 char *locale
= setlocale(LC_ALL
, "");
49 textdomain(TEXT_DOMAIN
);
53 contoper
= remove_oldrdn
= 0;
55 if ((myname
= strrchr(argv
[0], '/')) == NULL
)
60 optind
= ldaptool_process_args( argc
, argv
, "cr", 0, options_callback
);
66 if ( ldaptool_fp
== NULL
) {
71 if (argc
- optind
== 3) /* accept as arguments: dn rdn newsuperior */
73 if (( L_newParent
= strdup( argv
[argc
- 1] )) == NULL
)
76 exit( LDAP_NO_MEMORY
);
79 if (( rdn
= strdup( argv
[argc
- 2] )) == NULL
)
82 exit( LDAP_NO_MEMORY
);
85 if (( entrydn
= strdup( argv
[argc
- 3] )) == NULL
)
88 exit( LDAP_NO_MEMORY
);
92 else if (argc
- optind
== 2) /* accept as arguments: dn rdn */
94 if (( rdn
= strdup( argv
[argc
- 1] )) == NULL
)
97 exit( LDAP_NO_MEMORY
);
100 if (( entrydn
= strdup( argv
[argc
- 2] )) == NULL
)
107 else if ( argc
- optind
!= 0 )
109 fprintf( stderr
, gettext("%s: invalid number of arguments, only two or three allowed\n"), myname
);
114 ld
= ldaptool_ldap_init (0);
116 if ( !ldaptool_not
) {
117 deref
= LDAP_DEREF_NEVER
; /* this seems prudent */
118 ldap_set_option( ld
, LDAP_OPT_DEREF
, &deref
);
126 rc
= domodrdn(ld
, entrydn
, rdn
, L_newParent
, remove_oldrdn
);
128 else while ( (rc
== 0 || contoper
) &&
129 (fgets(buf
, sizeof(buf
), ldaptool_fp
) != NULL
) )
133 * The format of the file is one of the following:
142 * both types of sequences can be found in the file
145 if ( (strlen(buf
) == 1) && (ldaptool_fp
== stdin
) )
148 buf
[ strlen( buf
) - 1 ] = '\0'; /* remove nl */
149 if ( *buf
!= '\0' ) /* blank lines optional, skip */
151 if ( haverdn
) /* first type of sequence */
153 if (( L_newParent
= strdup( buf
)) == NULL
)
156 exit( LDAP_NO_MEMORY
);
158 if ( L_newParent
&& (L_protoVersion
== LDAP_VERSION
) )
160 printf( gettext("LDAP Server is V2: <newsuperior> argument is ignored...\n") );
163 rc
= domodrdn(ld
, entrydn
, rdn
, L_newParent
, remove_oldrdn
);
166 else if ( havedn
) /* have DN, get RDN */
168 if (( rdn
= strdup( buf
)) == NULL
)
171 exit( LDAP_NO_MEMORY
);
176 else if ( !havedn
) /* don't have DN yet */
178 if (( entrydn
= strdup( buf
)) == NULL
)
181 exit( LDAP_NO_MEMORY
);
188 printf(gettext("kex: new line %d\n"), rc
);
189 if ( haverdn
) /* second type of sequence */
191 rc
= domodrdn(ld
, entrydn
, rdn
, NULL
, remove_oldrdn
);
196 if ( (rc
== 0 || contoper
) && haverdn
) /* second type of sequence */
198 rc
= domodrdn(ld
, entrydn
, rdn
, NULL
, remove_oldrdn
);
202 ldaptool_cleanup( ld
);
208 options_callback( int option
, char *optarg
)
211 case 'c': /* continuous operation mode */
214 case 'r': /* remove old RDN */
223 domodrdn( LDAP
*ld
, char *dn
, char *rdn
, char *newsuperior
, int remove_oldrdn
)
225 int rc
= LDAP_SUCCESS
;
227 if ( ldaptool_verbose
)
228 printf( gettext("new RDN: %1$s (%2$skeep existing values)\n"),
229 rdn
, remove_oldrdn
? "do not " : "" );
231 printf( gettext("%1$srenaming entry %2$s\n"),
232 ldaptool_not
? "!" : "", dn
);
236 rc
= ldap_rename_s( ld
, dn
, rdn
, newsuperior
, remove_oldrdn
, NULL
, NULL
);
237 if ( rc
!= LDAP_SUCCESS
)
238 fprintf(stderr
, gettext("ldap_rename_s: %s\n"), ldap_err2string(rc
));
239 else if ( ldaptool_verbose
)
240 printf( gettext("rename completed\n") );