1 /* ldapmodrdn.c - generic program to modify an entry's RDN using LDAP.
3 * Support for MODIFYDN REQUEST V3 (newSuperior) by:
5 * Copyright 1999, Juan C. Gomez, All rights reserved.
6 * This software is not subject to any license of Silicon Graphics
7 * Inc. or Purdue University.
9 * Redistribution and use in source and binary forms are permitted
10 * without restriction or fee of any kind as long as this notice
19 #include <ac/stdlib.h>
22 #include <ac/signal.h>
23 #include <ac/string.h>
24 #include <ac/unistd.h>
29 static char *binddn = NULL;
30 static char *passwd = NULL;
31 static char *base = NULL;
32 static char *ldaphost = NULL;
33 static int ldapport = 0;
34 static int not, verbose, contoper;
37 static int domodrdn LDAP_P((
41 int remove, /* flag: remove old RDN */
45 main(int argc, char **argv)
47 char *usage = "usage: %s [-nvkWc] [-d debug-level] [-h ldaphost] [-P version] [-p ldapport] [-D binddn] [-w passwd] [ -f file | < entryfile | dn newrdn ] [-s newSuperior]\n";
48 char *myname,*infile, *entrydn, *rdn, buf[ 4096 ];
50 int rc, i, remove, havedn, authmethod, version, want_bindpw, debug;
51 char *newSuperior=NULL;
54 not = contoper = verbose = remove = want_bindpw = debug = 0;
55 authmethod = LDAP_AUTH_SIMPLE;
58 myname = (myname = strrchr(argv[0], '/')) == NULL ? argv[0] : ++myname;
60 while (( i = getopt( argc, argv, "WkKcnvrh:P:p:D:w:d:f:s:" )) != EOF ) {
62 case 'k': /* kerberos bind */
64 authmethod = LDAP_AUTH_KRBV4;
66 fprintf (stderr, "%s was not compiled with Kerberos support\n", argv[0]);
67 return( EXIT_FAILURE );
70 case 'K': /* kerberos bind, part one only */
72 authmethod = LDAP_AUTH_KRBV41;
74 fprintf (stderr, "%s was not compiled with Kerberos support\n", argv[0]);
75 return( EXIT_FAILURE );
78 case 'c': /* continuous operation mode */
81 case 'h': /* ldap host */
82 ldaphost = strdup( optarg );
84 case 'D': /* bind DN */
85 binddn = strdup( optarg );
87 case 's': /* newSuperior */
88 newSuperior = strdup( optarg );
89 version = LDAP_VERSION3; /* This option => force V3 */
91 case 'w': /* password */
92 passwd = strdup( optarg );
96 for( p = optarg; *p == NULL; p++ ) {
102 debug |= atoi( optarg );
104 case 'f': /* read from file */
105 infile = strdup( optarg );
108 ldapport = atoi( optarg );
110 case 'n': /* print adds, don't actually do them */
113 case 'v': /* verbose mode */
116 case 'r': /* remove old RDN */
123 switch( atoi(optarg) )
126 version = LDAP_VERSION2;
129 version = LDAP_VERSION3;
132 fprintf( stderr, "protocol version should be 2 or 3\n" );
133 fprintf( stderr, usage, argv[0] );
134 return( EXIT_FAILURE );
138 fprintf( stderr, usage, argv[0] );
139 return( EXIT_FAILURE );
143 if (newSuperior != NULL) {
144 if (version == LDAP_VERSION2) {
146 "%s: version conflict!, -s newSuperior requires LDAPv3\n",
148 fprintf( stderr, usage, argv[0] );
149 return( EXIT_FAILURE );
152 /* promote to LDAPv3 */
153 version = LDAP_VERSION3;
157 if (argc - optind == 2) {
158 if (( rdn = strdup( argv[argc - 1] )) == NULL ) {
160 return( EXIT_FAILURE );
162 if (( entrydn = strdup( argv[argc - 2] )) == NULL ) {
164 return( EXIT_FAILURE );
167 } else if ( argc - optind != 0 ) {
168 fprintf( stderr, "%s: invalid number of arguments, only two allowed\n", myname);
169 fprintf( stderr, usage, argv[0] );
170 return( EXIT_FAILURE );
173 if ( infile != NULL ) {
174 if (( fp = fopen( infile, "r" )) == NULL ) {
176 return( EXIT_FAILURE );
183 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
184 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
186 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
187 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
192 (void) SIGNAL( SIGPIPE, SIG_IGN );
195 if (( ld = ldap_init( ldaphost, ldapport )) == NULL ) {
196 perror( "ldap_init" );
197 return( EXIT_FAILURE );
200 /* this seems prudent */
202 int deref = LDAP_DEREF_NEVER;
203 ldap_set_option( ld, LDAP_OPT_DEREF, &deref);
207 passwd = getpass("Enter LDAP Password: ");
210 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version ) != LDAP_OPT_SUCCESS)
212 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n", version );
215 if ( ldap_bind_s( ld, binddn, passwd, authmethod ) != LDAP_SUCCESS ) {
216 ldap_perror( ld, "ldap_bind" );
217 return( EXIT_FAILURE );
222 rc = domodrdn(ld, entrydn, rdn, remove, newSuperior);
223 else while ((rc == 0 || contoper) && fgets(buf, sizeof(buf), fp) != NULL) {
224 if ( *buf != '\0' ) { /* blank lines optional, skip */
225 buf[ strlen( buf ) - 1 ] = '\0'; /* remove nl */
227 if ( havedn ) { /* have DN, get RDN */
228 if (( rdn = strdup( buf )) == NULL ) {
230 return( EXIT_FAILURE );
232 rc = domodrdn(ld, entrydn, rdn, remove, newSuperior);
234 } else if ( !havedn ) { /* don't have DN yet */
235 if (( entrydn = strdup( buf )) == NULL ) {
237 return( EXIT_FAILURE );
254 int remove, /* flag: remove old RDN */
260 printf( "modrdn %s:\n\t%s\n", dn, rdn );
262 printf("removing old RDN\n");
264 printf("keeping old RDN\n");
265 if(newSuperior!=NULL)
266 printf("placing node under a new parent = %s\n", newSuperior);
270 i = ldap_rename2_s( ld, dn, rdn, remove, newSuperior );
271 if ( i != LDAP_SUCCESS ) {
272 ldap_perror( ld, "ldap_rename2_s" );
273 } else if ( verbose ) {
274 printf( "modrdn complete\n" );