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
21 #include <ac/signal.h>
22 #include <ac/string.h>
23 #include <ac/unistd.h>
28 static char *binddn = NULL;
29 static char *passwd = NULL;
30 static char *base = NULL;
31 static char *ldaphost = NULL;
32 static int ldapport = 0;
33 static int not, verbose, contoper;
36 static int domodrdn LDAP_P((
40 int remove, /* flag: remove old RDN */
44 main(int argc, char **argv)
46 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";
47 char *myname,*infile, *entrydn, *rdn, buf[ 4096 ];
49 int rc, i, remove, havedn, authmethod, version, want_bindpw, debug;
50 char *newSuperior=NULL;
53 not = contoper = verbose = remove = want_bindpw = debug = 0;
54 authmethod = LDAP_AUTH_SIMPLE;
57 myname = (myname = strrchr(argv[0], '/')) == NULL ? argv[0] : ++myname;
59 while (( i = getopt( argc, argv, "WkKcnvrh:P:p:D:w:d:f:s:" )) != EOF ) {
61 case 'k': /* kerberos bind */
63 authmethod = LDAP_AUTH_KRBV4;
65 fprintf (stderr, "%s was not compiled with Kerberos support\n", argv[0]);
66 return( EXIT_FAILURE );
69 case 'K': /* kerberos bind, part one only */
71 authmethod = LDAP_AUTH_KRBV41;
73 fprintf (stderr, "%s was not compiled with Kerberos support\n", argv[0]);
74 return( EXIT_FAILURE );
77 case 'c': /* continuous operation mode */
80 case 'h': /* ldap host */
81 ldaphost = strdup( optarg );
83 case 'D': /* bind DN */
84 binddn = strdup( optarg );
86 case 's': /* newSuperior */
87 newSuperior = strdup( optarg );
88 version = LDAP_VERSION3; /* This option => force V3 */
90 case 'w': /* password */
91 passwd = strdup( optarg );
94 debug |= atoi( optarg );
96 case 'f': /* read from file */
97 infile = strdup( optarg );
100 ldapport = atoi( optarg );
102 case 'n': /* print adds, don't actually do them */
105 case 'v': /* verbose mode */
108 case 'r': /* remove old RDN */
115 switch( atoi(optarg) )
118 version = LDAP_VERSION2;
121 version = LDAP_VERSION3;
124 fprintf( stderr, "protocol version should be 2 or 3\n" );
125 fprintf( stderr, usage, argv[0] );
126 return( EXIT_FAILURE );
130 fprintf( stderr, usage, argv[0] );
131 return( EXIT_FAILURE );
135 if (newSuperior != NULL) {
136 if (version == LDAP_VERSION2) {
138 "%s: version conflict!, -s newSuperior requires LDAPv3\n",
140 fprintf( stderr, usage, argv[0] );
141 return( EXIT_FAILURE );
144 /* promote to LDAPv3 */
145 version = LDAP_VERSION3;
149 if (argc - optind == 2) {
150 if (( rdn = strdup( argv[argc - 1] )) == NULL ) {
152 return( EXIT_FAILURE );
154 if (( entrydn = strdup( argv[argc - 2] )) == NULL ) {
156 return( EXIT_FAILURE );
159 } else if ( argc - optind != 0 ) {
160 fprintf( stderr, "%s: invalid number of arguments, only two allowed\n", myname);
161 fprintf( stderr, usage, argv[0] );
162 return( EXIT_FAILURE );
165 if ( infile != NULL ) {
166 if (( fp = fopen( infile, "r" )) == NULL ) {
168 return( EXIT_FAILURE );
175 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_ERROR ) {
176 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
178 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_ERROR ) {
179 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
184 (void) SIGNAL( SIGPIPE, SIG_IGN );
187 if (( ld = ldap_init( ldaphost, ldapport )) == NULL ) {
188 perror( "ldap_init" );
189 return( EXIT_FAILURE );
192 /* this seems prudent */
194 int deref = LDAP_DEREF_NEVER;
195 ldap_set_option( ld, LDAP_OPT_DEREF, &deref);
199 passwd = getpass("Enter LDAP Password: ");
202 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version ) == LDAP_OPT_ERROR)
204 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n", version );
207 if ( ldap_bind_s( ld, binddn, passwd, authmethod ) != LDAP_SUCCESS ) {
208 ldap_perror( ld, "ldap_bind" );
209 return( EXIT_FAILURE );
214 rc = domodrdn(ld, entrydn, rdn, remove, newSuperior);
215 else while ((rc == 0 || contoper) && fgets(buf, sizeof(buf), fp) != NULL) {
216 if ( *buf != '\0' ) { /* blank lines optional, skip */
217 buf[ strlen( buf ) - 1 ] = '\0'; /* remove nl */
219 if ( havedn ) { /* have DN, get RDN */
220 if (( rdn = strdup( buf )) == NULL ) {
222 return( EXIT_FAILURE );
224 rc = domodrdn(ld, entrydn, rdn, remove, newSuperior);
226 } else if ( !havedn ) { /* don't have DN yet */
227 if (( entrydn = strdup( buf )) == NULL ) {
229 return( EXIT_FAILURE );
246 int remove, /* flag: remove old RDN */
252 printf( "modrdn %s:\n\t%s\n", dn, rdn );
254 printf("removing old RDN\n");
256 printf("keeping old RDN\n");
257 if(newSuperior!=NULL)
258 printf("placing node under a new parent = %s\n", newSuperior);
262 i = ldap_rename2_s( ld, dn, rdn, remove, newSuperior );
263 if ( i != LDAP_SUCCESS ) {
264 ldap_perror( ld, "ldap_rename2_s" );
265 } else if ( verbose ) {
266 printf( "modrdn complete\n" );