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 #define safe_realloc( ptr, size ) ( ptr == NULL ? malloc( size ) : \
39 static int domodrdn LDAP_P((
43 int remove, /* flag: remove old RDN */
47 main(int argc, char **argv)
49 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";
50 char *myname,*infile, *entrydn, *rdn, buf[ 4096 ];
52 int rc, i, remove, havedn, authmethod, version, want_bindpw, debug;
53 char *newSuperior=NULL;
56 not = contoper = verbose = remove = want_bindpw = debug = 0;
57 authmethod = LDAP_AUTH_SIMPLE;
60 myname = (myname = strrchr(argv[0], '/')) == NULL ? argv[0] : ++myname;
62 while (( i = getopt( argc, argv, "WkKcnvrh:P:p:D:w:d:f:s:" )) != EOF ) {
64 case 'k': /* kerberos bind */
66 authmethod = LDAP_AUTH_KRBV4;
68 fprintf (stderr, "%s was not compiled with Kerberos support\n", argv[0]);
71 case 'K': /* kerberos bind, part one only */
73 authmethod = LDAP_AUTH_KRBV41;
75 fprintf (stderr, "%s was not compiled with Kerberos support\n", argv[0]);
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 */
119 version = LDAP_VERSION2;
122 version = LDAP_VERSION3;
127 fprintf( stderr, usage, argv[0] );
132 if ((newSuperior != NULL) && (version != LDAP_VERSION3))
135 "%s: version conflict!, -s newSuperior requires LDAP v3\n",\
137 fprintf( stderr, usage, argv[0] );
142 if (argc - optind == 2) {
143 if (( rdn = strdup( argv[argc - 1] )) == NULL ) {
147 if (( entrydn = strdup( argv[argc - 2] )) == NULL ) {
152 } else if ( argc - optind != 0 ) {
153 fprintf( stderr, "%s: invalid number of arguments, only two allowed\n", myname);
154 fprintf( stderr, usage, argv[0] );
158 if ( infile != NULL ) {
159 if (( fp = fopen( infile, "r" )) == NULL ) {
168 lber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug );
169 ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug );
173 (void) SIGNAL( SIGPIPE, SIG_IGN );
176 if (( ld = ldap_init( ldaphost, ldapport )) == NULL ) {
177 perror( "ldap_init" );
181 /* this seems prudent */
183 int deref = LDAP_DEREF_NEVER;
184 ldap_set_option( ld, LDAP_OPT_DEREF, &deref);
188 passwd = getpass("Enter LDAP Password: ");
191 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version );
194 if ( ldap_bind_s( ld, binddn, passwd, authmethod ) != LDAP_SUCCESS ) {
195 ldap_perror( ld, "ldap_bind" );
201 rc = domodrdn(ld, entrydn, rdn, remove, newSuperior);
202 else while ((rc == 0 || contoper) && fgets(buf, sizeof(buf), fp) != NULL) {
203 if ( *buf != '\0' ) { /* blank lines optional, skip */
204 buf[ strlen( buf ) - 1 ] = '\0'; /* remove nl */
206 if ( havedn ) { /* have DN, get RDN */
207 if (( rdn = strdup( buf )) == NULL ) {
211 rc = domodrdn(ld, entrydn, rdn, remove, newSuperior);
213 } else if ( !havedn ) { /* don't have DN yet */
214 if (( entrydn = strdup( buf )) == NULL ) {
235 int remove, /* flag: remove old RDN */
241 printf( "modrdn %s:\n\t%s\n", dn, rdn );
243 printf("removing old RDN\n");
245 printf("keeping old RDN\n");
246 if(newSuperior!=NULL)
247 printf("placing node under a new parent = %s\n", newSuperior);
251 i = ldap_rename2_s( ld, dn, rdn, remove, newSuperior );
252 if ( i != LDAP_SUCCESS ) {
253 ldap_perror( ld, "ldap_rename2_s" );
254 } else if ( verbose ) {
255 printf( "modrdn complete\n" );