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 );
95 debug |= atoi( optarg );
97 case 'f': /* read from file */
98 infile = strdup( optarg );
101 ldapport = atoi( optarg );
103 case 'n': /* print adds, don't actually do them */
106 case 'v': /* verbose mode */
109 case 'r': /* remove old RDN */
116 switch( atoi(optarg) )
119 version = LDAP_VERSION2;
122 version = LDAP_VERSION3;
125 fprintf( stderr, "protocol version should be 2 or 3\n" );
126 fprintf( stderr, usage, argv[0] );
127 return( EXIT_FAILURE );
131 fprintf( stderr, usage, argv[0] );
132 return( EXIT_FAILURE );
136 if (newSuperior != NULL) {
137 if (version == LDAP_VERSION2) {
139 "%s: version conflict!, -s newSuperior requires LDAPv3\n",
141 fprintf( stderr, usage, argv[0] );
142 return( EXIT_FAILURE );
145 /* promote to LDAPv3 */
146 version = LDAP_VERSION3;
150 if (argc - optind == 2) {
151 if (( rdn = strdup( argv[argc - 1] )) == NULL ) {
153 return( EXIT_FAILURE );
155 if (( entrydn = strdup( argv[argc - 2] )) == NULL ) {
157 return( EXIT_FAILURE );
160 } else if ( argc - optind != 0 ) {
161 fprintf( stderr, "%s: invalid number of arguments, only two allowed\n", myname);
162 fprintf( stderr, usage, argv[0] );
163 return( EXIT_FAILURE );
166 if ( infile != NULL ) {
167 if (( fp = fopen( infile, "r" )) == NULL ) {
169 return( EXIT_FAILURE );
176 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_ERROR ) {
177 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
179 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_ERROR ) {
180 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
185 (void) SIGNAL( SIGPIPE, SIG_IGN );
188 if (( ld = ldap_init( ldaphost, ldapport )) == NULL ) {
189 perror( "ldap_init" );
190 return( EXIT_FAILURE );
193 /* this seems prudent */
195 int deref = LDAP_DEREF_NEVER;
196 ldap_set_option( ld, LDAP_OPT_DEREF, &deref);
200 passwd = getpass("Enter LDAP Password: ");
203 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version ) == LDAP_OPT_ERROR)
205 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n", version );
208 if ( ldap_bind_s( ld, binddn, passwd, authmethod ) != LDAP_SUCCESS ) {
209 ldap_perror( ld, "ldap_bind" );
210 return( EXIT_FAILURE );
215 rc = domodrdn(ld, entrydn, rdn, remove, newSuperior);
216 else while ((rc == 0 || contoper) && fgets(buf, sizeof(buf), fp) != NULL) {
217 if ( *buf != '\0' ) { /* blank lines optional, skip */
218 buf[ strlen( buf ) - 1 ] = '\0'; /* remove nl */
220 if ( havedn ) { /* have DN, get RDN */
221 if (( rdn = strdup( buf )) == NULL ) {
223 return( EXIT_FAILURE );
225 rc = domodrdn(ld, entrydn, rdn, remove, newSuperior);
227 } else if ( !havedn ) { /* don't have DN yet */
228 if (( entrydn = strdup( buf )) == NULL ) {
230 return( EXIT_FAILURE );
247 int remove, /* flag: remove old RDN */
253 printf( "modrdn %s:\n\t%s\n", dn, rdn );
255 printf("removing old RDN\n");
257 printf("keeping old RDN\n");
258 if(newSuperior!=NULL)
259 printf("placing node under a new parent = %s\n", newSuperior);
263 i = ldap_rename2_s( ld, dn, rdn, remove, newSuperior );
264 if ( i != LDAP_SUCCESS ) {
265 ldap_perror( ld, "ldap_rename2_s" );
266 } else if ( verbose ) {
267 printf( "modrdn complete\n" );