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