]> git.sur5r.net Git - openldap/blob - clients/tools/ldapmodrdn.c
03d6adfcbf337aeefaee95c836e4e74c96ce4345
[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;
39
40     infile = NULL;
41     not = contoper = verbose = remove = want_bindpw = 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 #ifdef LDAP_DEBUG
76             ldap_debug = lber_debug = atoi( optarg );   /* */
77 #else /* LDAP_DEBUG */
78             fprintf( stderr, "compile with -DLDAP_DEBUG for debugging\n" );
79 #endif /* LDAP_DEBUG */
80             break;
81         case 'f':       /* read from file */
82             infile = strdup( optarg );
83             break;
84         case 'p':
85             ldapport = atoi( optarg );
86             break;
87         case 'n':       /* print adds, don't actually do them */
88             ++not;
89             break;
90         case 'v':       /* verbose mode */
91             verbose++;
92             break;
93         case 'r':       /* remove old RDN */
94             remove++;
95             break;
96         case 'W':
97                 want_bindpw++;
98                 break;
99         default:
100             fprintf( stderr, usage, argv[0] );
101             exit( 1 );
102         }
103     }
104
105     havedn = 0;
106     if (argc - optind == 2) {
107         if (( rdn = strdup( argv[argc - 1] )) == NULL ) {
108             perror( "strdup" );
109             exit( 1 );
110         }
111         if (( entrydn = strdup( argv[argc - 2] )) == NULL ) {
112             perror( "strdup" );
113             exit( 1 );
114         }
115         ++havedn;
116     } else if ( argc - optind != 0 ) {
117         fprintf( stderr, "%s: invalid number of arguments, only two allowed\n", myname);
118         fprintf( stderr, usage, argv[0] );
119         exit( 1 );
120     }
121
122     if ( infile != NULL ) {
123         if (( fp = fopen( infile, "r" )) == NULL ) {
124             perror( infile );
125             exit( 1 );
126         }
127     } else {
128         fp = stdin;
129     }
130
131     if (( ld = ldap_open( ldaphost, ldapport )) == NULL ) {
132         perror( "ldap_open" );
133         exit( 1 );
134     }
135
136         /* this seems prudent */
137         ldap_set_option( ld, LDAP_OPT_DEREF, LDAP_DEREF_NEVER);
138
139         if (want_bindpw)
140                 passwd = getpass("Enter LDAP Password: ");
141
142     if ( ldap_bind_s( ld, binddn, passwd, authmethod ) != LDAP_SUCCESS ) {
143         ldap_perror( ld, "ldap_bind" );
144         exit( 1 );
145     }
146
147     rc = 0;
148     if (havedn)
149         rc = domodrdn(ld, entrydn, rdn, remove);
150     else while ((rc == 0 || contoper) && fgets(buf, sizeof(buf), fp) != NULL) {
151         if ( *buf != '\0' ) {   /* blank lines optional, skip */
152             buf[ strlen( buf ) - 1 ] = '\0';    /* remove nl */
153
154             if ( havedn ) {     /* have DN, get RDN */
155                 if (( rdn = strdup( buf )) == NULL ) {
156                     perror( "strdup" );
157                     exit( 1 );
158                 }
159                 rc = domodrdn(ld, entrydn, rdn, remove);
160                 havedn = 0;
161             } else if ( !havedn ) {     /* don't have DN yet */
162                 if (( entrydn = strdup( buf )) == NULL ) {
163                     perror( "strdup" );
164                     exit( 1 );
165                 }
166                 ++havedn;
167             }
168         }
169     }
170
171     ldap_unbind( ld );
172
173     exit( rc );
174
175         /* UNREACHABLE */
176         return(0);
177 }
178
179 static int domodrdn(
180     LDAP        *ld,
181     char        *dn,
182     char        *rdn,
183     int         remove) /* flag: remove old RDN */
184 {
185     int i;
186
187     if ( verbose ) {
188         printf( "modrdn %s:\n\t%s\n", dn, rdn );
189         if (remove)
190             printf("removing old RDN\n");
191         else
192             printf("keeping old RDN\n");
193     }
194
195     if ( !not ) {
196         i = ldap_modrdn2_s( ld, dn, rdn, remove );
197         if ( i != LDAP_SUCCESS ) {
198             ldap_perror( ld, "ldap_modrdn2_s" );
199         } else if ( verbose ) {
200             printf( "modrdn complete\n" );
201         }
202     } else {
203         i = LDAP_SUCCESS;
204     }
205
206     return( i );
207 }