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] [-M[M]] [-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, manageDSAit;
51 char *newSuperior=NULL;
54 not = contoper = verbose = remove = want_bindpw = debug = manageDSAit = 0;
55 authmethod = LDAP_AUTH_SIMPLE;
58 myname = (myname = strrchr(argv[0], '/')) == NULL ? argv[0] : ++myname;
60 while (( i = getopt( argc, argv, "WkKMcnvrh: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 == '\0'; 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 */
120 /* enable Manage DSA IT */
127 switch( atoi(optarg) )
130 version = LDAP_VERSION2;
133 version = LDAP_VERSION3;
136 fprintf( stderr, "protocol version should be 2 or 3\n" );
137 fprintf( stderr, usage, argv[0] );
138 return( EXIT_FAILURE );
142 fprintf( stderr, usage, argv[0] );
143 return( EXIT_FAILURE );
147 if (newSuperior != NULL) {
148 if (version == LDAP_VERSION2) {
150 "%s: version conflict!, -s newSuperior requires LDAPv3\n",
152 fprintf( stderr, usage, argv[0] );
153 return( EXIT_FAILURE );
156 /* promote to LDAPv3 */
157 version = LDAP_VERSION3;
161 if (argc - optind == 2) {
162 if (( rdn = strdup( argv[argc - 1] )) == NULL ) {
164 return( EXIT_FAILURE );
166 if (( entrydn = strdup( argv[argc - 2] )) == NULL ) {
168 return( EXIT_FAILURE );
171 } else if ( argc - optind != 0 ) {
172 fprintf( stderr, "%s: invalid number of arguments, only two allowed\n", myname);
173 fprintf( stderr, usage, argv[0] );
174 return( EXIT_FAILURE );
177 if ( infile != NULL ) {
178 if (( fp = fopen( infile, "r" )) == NULL ) {
180 return( EXIT_FAILURE );
187 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
188 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
190 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
191 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
196 (void) SIGNAL( SIGPIPE, SIG_IGN );
199 if (( ld = ldap_init( ldaphost, ldapport )) == NULL ) {
200 perror( "ldap_init" );
201 return( EXIT_FAILURE );
204 /* this seems prudent */
206 int deref = LDAP_DEREF_NEVER;
207 ldap_set_option( ld, LDAP_OPT_DEREF, &deref);
211 passwd = getpass("Enter LDAP Password: ");
214 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version ) != LDAP_OPT_SUCCESS)
216 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n", version );
219 if ( ldap_bind_s( ld, binddn, passwd, authmethod ) != LDAP_SUCCESS ) {
220 ldap_perror( ld, "ldap_bind" );
221 return( EXIT_FAILURE );
227 LDAPControl *ctrls[2];
231 c.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
232 c.ldctl_value.bv_val = NULL;
233 c.ldctl_value.bv_len = 0;
234 c.ldctl_iscritical = manageDSAit > 1;
236 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, &ctrls );
238 if( err != LDAP_OPT_SUCCESS ) {
239 fprintf( stderr, "Could not set Manage DSA IT Control\n" );
240 if( c.ldctl_iscritical ) {
241 exit( EXIT_FAILURE );
248 rc = domodrdn(ld, entrydn, rdn, remove, newSuperior);
249 else while ((rc == 0 || contoper) && fgets(buf, sizeof(buf), fp) != NULL) {
250 if ( *buf != '\0' ) { /* blank lines optional, skip */
251 buf[ strlen( buf ) - 1 ] = '\0'; /* remove nl */
253 if ( havedn ) { /* have DN, get RDN */
254 if (( rdn = strdup( buf )) == NULL ) {
256 return( EXIT_FAILURE );
258 rc = domodrdn(ld, entrydn, rdn, remove, newSuperior);
260 } else if ( !havedn ) { /* don't have DN yet */
261 if (( entrydn = strdup( buf )) == NULL ) {
263 return( EXIT_FAILURE );
280 int remove, /* flag: remove old RDN */
286 printf( "modrdn %s:\n\t%s\n", dn, rdn );
288 printf("removing old RDN\n");
290 printf("keeping old RDN\n");
291 if(newSuperior!=NULL)
292 printf("placing node under a new parent = %s\n", newSuperior);
296 i = ldap_rename2_s( ld, dn, rdn, remove, newSuperior );
297 if ( i != LDAP_SUCCESS ) {
298 ldap_perror( ld, "ldap_rename2_s" );
299 } else if ( verbose ) {
300 printf( "modrdn complete\n" );