]> git.sur5r.net Git - openldap/blob - clients/tools/ldapmodrdn.c
e293fc530e86dd8d589f88a50ebf52f039ea1238
[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 #ifdef LDAP_DEBUG
24 extern int ldap_debug, lber_debug;
25 #endif /* LDAP_DEBUG */
26
27 #define safe_realloc( ptr, size )       ( ptr == NULL ? malloc( size ) : \
28                                          realloc( ptr, size ))
29
30 static int domodrdn LDAP_P((
31     LDAP        *ld,
32     char        *dn,
33     char        *rdn,
34     int         remove));       /* flag: remove old RDN */
35
36 int
37 main( argc, argv )
38     int         argc;
39     char        **argv;
40 {
41     char                *usage = "usage: %s [-nvkc] [-d debug-level] [-h ldaphost] [-p ldapport] [-D binddn] [-w passwd] [ -f file | < entryfile | dn newrdn ]\n";
42     char                *myname,*infile, *entrydn, *rdn, buf[ 4096 ];
43     FILE                *fp;
44     int                 rc, i, kerberos, remove, havedn, authmethod;
45
46     extern char *optarg;
47     extern int  optind;
48
49     infile = NULL;
50     kerberos = not = contoper = verbose = remove = 0;
51
52     myname = (myname = strrchr(argv[0], '/')) == NULL ? argv[0] : ++myname;
53
54     while (( i = getopt( argc, argv, "kKcnvrh:p:D:w:d:f:" )) != EOF ) {
55         switch( i ) {
56         case 'k':       /* kerberos bind */
57             kerberos = 2;
58             break;
59         case 'K':       /* kerberos bind, part one only */
60             kerberos = 1;
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         default:
97             fprintf( stderr, usage, argv[0] );
98             exit( 1 );
99         }
100     }
101
102     havedn = 0;
103     if (argc - optind == 2) {
104         if (( rdn = strdup( argv[argc - 1] )) == NULL ) {
105             perror( "strdup" );
106             exit( 1 );
107         }
108         if (( entrydn = strdup( argv[argc - 2] )) == NULL ) {
109             perror( "strdup" );
110             exit( 1 );
111         }
112         ++havedn;
113     } else if ( argc - optind != 0 ) {
114         fprintf( stderr, "%s: invalid number of arguments, only two allowed\n", myname);
115         fprintf( stderr, usage, argv[0] );
116         exit( 1 );
117     }
118
119     if ( infile != NULL ) {
120         if (( fp = fopen( infile, "r" )) == NULL ) {
121             perror( infile );
122             exit( 1 );
123         }
124     } else {
125         fp = stdin;
126     }
127
128     if (( ld = ldap_open( ldaphost, ldapport )) == NULL ) {
129         perror( "ldap_open" );
130         exit( 1 );
131     }
132
133         /* this seems prudent */
134         ldap_set_option( ld, LDAP_OPT_DEREF, LDAP_DEREF_NEVER);
135
136     if ( !kerberos ) {
137         authmethod = LDAP_AUTH_SIMPLE;
138     } else if ( kerberos == 1 ) {
139         authmethod = LDAP_AUTH_KRBV41;
140     } else {
141         authmethod = LDAP_AUTH_KRBV4;
142     }
143     if ( ldap_bind_s( ld, binddn, passwd, authmethod ) != LDAP_SUCCESS ) {
144         ldap_perror( ld, "ldap_bind" );
145         exit( 1 );
146     }
147
148     rc = 0;
149     if (havedn)
150         rc = domodrdn(ld, entrydn, rdn, remove);
151     else while ((rc == 0 || contoper) && fgets(buf, sizeof(buf), fp) != NULL) {
152         if ( *buf != '\0' ) {   /* blank lines optional, skip */
153             buf[ strlen( buf ) - 1 ] = '\0';    /* remove nl */
154
155             if ( havedn ) {     /* have DN, get RDN */
156                 if (( rdn = strdup( buf )) == NULL ) {
157                     perror( "strdup" );
158                     exit( 1 );
159                 }
160                 rc = domodrdn(ld, entrydn, rdn, remove);
161                 havedn = 0;
162             } else if ( !havedn ) {     /* don't have DN yet */
163                 if (( entrydn = strdup( buf )) == NULL ) {
164                     perror( "strdup" );
165                     exit( 1 );
166                 }
167                 ++havedn;
168             }
169         }
170     }
171
172     ldap_unbind( ld );
173
174     exit( rc );
175
176         /* UNREACHABLE */
177         return(0);
178 }
179
180 static int domodrdn(
181     LDAP        *ld,
182     char        *dn,
183     char        *rdn,
184     int         remove) /* flag: remove old RDN */
185 {
186     int i;
187
188     if ( verbose ) {
189         printf( "modrdn %s:\n\t%s\n", dn, rdn );
190         if (remove)
191             printf("removing old RDN\n");
192         else
193             printf("keeping old RDN\n");
194     }
195
196     if ( !not ) {
197         i = ldap_modrdn2_s( ld, dn, rdn, remove );
198         if ( i != LDAP_SUCCESS ) {
199             ldap_perror( ld, "ldap_modrdn2_s" );
200         } else if ( verbose ) {
201             printf( "modrdn complete\n" );
202         }
203     } else {
204         i = LDAP_SUCCESS;
205     }
206
207     return( i );
208 }