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