]> git.sur5r.net Git - openldap/blob - clients/tools/ldapmodrdn.c
Fix ldap_send_initial_request() to open connection if not already
[openldap] / clients / tools / ldapmodrdn.c
1 /* ldapmodrdn.c - generic program to modify an entry's RDN using LDAP */
2
3 #include "portable.h"
4
5 #include <stdio.h>
6 #include <stdlib.h>
7
8 #include <ac/ctype.h>
9 #include <ac/signal.h>
10 #include <ac/string.h>
11 #include <ac/unistd.h>
12
13 #include <lber.h>
14 #include <ldap.h>
15
16 static char     *binddn = NULL;
17 static char     *passwd = NULL;
18 static char     *base = NULL;
19 static char     *ldaphost = NULL;
20 static int      ldapport = 0;
21 static int      not, verbose, contoper;
22 static LDAP     *ld;
23
24 #define safe_realloc( ptr, size )       ( ptr == NULL ? malloc( size ) : \
25                                          realloc( ptr, size ))
26
27 static int domodrdn LDAP_P((
28     LDAP        *ld,
29     char        *dn,
30     char        *rdn,
31     int         remove));       /* flag: remove old RDN */
32
33 int
34 main(int argc, char **argv)
35 {
36         char            *usage = "usage: %s [-nvkWc] [-d debug-level] [-h ldaphost] [-P version] [-p ldapport] [-D binddn] [-w passwd] [ -f file | < entryfile | dn newrdn ]\n";
37     char                *myname,*infile, *entrydn, *rdn, buf[ 4096 ];
38     FILE                *fp;
39         int             rc, i, remove, havedn, authmethod, version, want_bindpw, debug;
40
41     infile = NULL;
42     not = contoper = verbose = remove = want_bindpw = debug = 0;
43     authmethod = LDAP_AUTH_SIMPLE;
44         version = -1;
45
46     myname = (myname = strrchr(argv[0], '/')) == NULL ? argv[0] : ++myname;
47
48     while (( i = getopt( argc, argv, "WkKcnvrh:P:p:D:w:d:f:" )) != EOF ) {
49         switch( i ) {
50         case 'k':       /* kerberos bind */
51 #ifdef HAVE_KERBEROS
52                 authmethod = LDAP_AUTH_KRBV4;
53 #else
54                 fprintf (stderr, "%s was not compiled with Kerberos support\n", argv[0]);
55 #endif
56             break;
57         case 'K':       /* kerberos bind, part one only */
58 #ifdef HAVE_KERBEROS
59                 authmethod = LDAP_AUTH_KRBV41;
60 #else
61                 fprintf (stderr, "%s was not compiled with Kerberos support\n", argv[0]);
62 #endif
63             break;
64         case 'c':       /* continuous operation mode */
65             ++contoper;
66             break;
67         case 'h':       /* ldap host */
68             ldaphost = strdup( optarg );
69             break;
70         case 'D':       /* bind DN */
71             binddn = strdup( optarg );
72             break;
73         case 'w':       /* password */
74             passwd = strdup( optarg );
75             break;
76         case 'd':
77             debug |= atoi( optarg );
78             break;
79         case 'f':       /* read from file */
80             infile = strdup( optarg );
81             break;
82         case 'p':
83             ldapport = atoi( optarg );
84             break;
85         case 'n':       /* print adds, don't actually do them */
86             ++not;
87             break;
88         case 'v':       /* verbose mode */
89             verbose++;
90             break;
91         case 'r':       /* remove old RDN */
92             remove++;
93             break;
94         case 'W':
95                 want_bindpw++;
96                 break;
97         case 'P':
98                 switch(optarg[0])
99                 {
100                 case '2':
101                         version = LDAP_VERSION2;
102                         break;
103                 case '3':
104                         version = LDAP_VERSION3;
105                         break;
106                 }
107                 break;
108         default:
109             fprintf( stderr, usage, argv[0] );
110             exit( 1 );
111         }
112     }
113
114     havedn = 0;
115     if (argc - optind == 2) {
116         if (( rdn = strdup( argv[argc - 1] )) == NULL ) {
117             perror( "strdup" );
118             exit( 1 );
119         }
120         if (( entrydn = strdup( argv[argc - 2] )) == NULL ) {
121             perror( "strdup" );
122             exit( 1 );
123         }
124         ++havedn;
125     } else if ( argc - optind != 0 ) {
126         fprintf( stderr, "%s: invalid number of arguments, only two allowed\n", myname);
127         fprintf( stderr, usage, argv[0] );
128         exit( 1 );
129     }
130
131     if ( infile != NULL ) {
132         if (( fp = fopen( infile, "r" )) == NULL ) {
133             perror( infile );
134             exit( 1 );
135         }
136     } else {
137         fp = stdin;
138     }
139
140         if ( debug ) {
141                 lber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug );
142                 ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug );
143         }
144
145 #ifdef SIGPIPE
146         (void) SIGNAL( SIGPIPE, SIG_IGN );
147 #endif
148
149     if (( ld = ldap_init( ldaphost, ldapport )) == NULL ) {
150         perror( "ldap_init" );
151         exit( 1 );
152     }
153
154         /* this seems prudent */
155         {
156                 int deref = LDAP_DEREF_NEVER;
157                 ldap_set_option( ld, LDAP_OPT_DEREF, &deref);
158         }
159
160         if (want_bindpw)
161                 passwd = getpass("Enter LDAP Password: ");
162
163         if( version != -1) {
164                 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version );
165         }
166
167     if ( ldap_bind_s( ld, binddn, passwd, authmethod ) != LDAP_SUCCESS ) {
168         ldap_perror( ld, "ldap_bind" );
169         exit( 1 );
170     }
171
172     rc = 0;
173     if (havedn)
174         rc = domodrdn(ld, entrydn, rdn, remove);
175     else while ((rc == 0 || contoper) && fgets(buf, sizeof(buf), fp) != NULL) {
176         if ( *buf != '\0' ) {   /* blank lines optional, skip */
177             buf[ strlen( buf ) - 1 ] = '\0';    /* remove nl */
178
179             if ( havedn ) {     /* have DN, get RDN */
180                 if (( rdn = strdup( buf )) == NULL ) {
181                     perror( "strdup" );
182                     exit( 1 );
183                 }
184                 rc = domodrdn(ld, entrydn, rdn, remove);
185                 havedn = 0;
186             } else if ( !havedn ) {     /* don't have DN yet */
187                 if (( entrydn = strdup( buf )) == NULL ) {
188                     perror( "strdup" );
189                     exit( 1 );
190                 }
191                 ++havedn;
192             }
193         }
194     }
195
196     ldap_unbind( ld );
197
198     exit( rc );
199
200         /* UNREACHABLE */
201         return(0);
202 }
203
204 static int domodrdn(
205     LDAP        *ld,
206     char        *dn,
207     char        *rdn,
208     int         remove) /* flag: remove old RDN */
209 {
210     int i;
211
212     if ( verbose ) {
213         printf( "modrdn %s:\n\t%s\n", dn, rdn );
214         if (remove)
215             printf("removing old RDN\n");
216         else
217             printf("keeping old RDN\n");
218     }
219
220     if ( !not ) {
221         i = ldap_modrdn2_s( ld, dn, rdn, remove );
222         if ( i != LDAP_SUCCESS ) {
223             ldap_perror( ld, "ldap_modrdn2_s" );
224         } else if ( verbose ) {
225             printf( "modrdn complete\n" );
226         }
227     } else {
228         i = LDAP_SUCCESS;
229     }
230
231     return( i );
232 }