]> git.sur5r.net Git - openldap/blob - clients/tools/ldapmodrdn.c
d5f578f5944d095407cd095722ea9c740b7b31e0
[openldap] / clients / tools / ldapmodrdn.c
1 /* ldapmodrdn.c - generic program to modify an entry's RDN using LDAP */
2
3 #define DISABLE_BRIDGE
4 #include "portable.h"
5
6 #include <stdio.h>
7 #include <ac/string.h>
8 #include <stdlib.h>
9 #include <ctype.h>
10 #include <ac/time.h>
11
12 #include <lber.h>
13 #include <ldap.h>
14
15 #include "ldapconfig.h"
16
17 static char     *binddn = LDAPMODRDN_BINDDN;
18 static char     *passwd = LDAPMODRDN_BIND_CRED;
19 static char     *base = LDAPMODRDN_BASE;
20 static char     *ldaphost = LDAPHOST;
21 static int      ldapport = LDAP_PORT;
22 static int      not, verbose, contoper;
23 static LDAP     *ld;
24
25 #ifdef LDAP_DEBUG
26 extern int ldap_debug, lber_debug;
27 #endif /* LDAP_DEBUG */
28
29 #define safe_realloc( ptr, size )       ( ptr == NULL ? malloc( size ) : \
30                                          realloc( ptr, size ))
31
32
33 main( argc, argv )
34     int         argc;
35     char        **argv;
36 {
37     char                *usage = "usage: %s [-nvkc] [-d debug-level] [-h ldaphost] [-p ldapport] [-D binddn] [-w passwd] [ -f file | < entryfile | dn newrdn ]\n";
38     char                *myname,*infile, *p, *entrydn, *rdn, buf[ 4096 ];
39     FILE                *fp;
40     int                 rc, i, kerberos, remove, havedn, authmethod;
41     LDAPMod             **pmods;
42
43     extern char *optarg;
44     extern int  optind;
45
46     infile = NULL;
47     kerberos = not = contoper = verbose = remove = 0;
48
49     myname = (myname = strrchr(argv[0], '/')) == NULL ? argv[0] : ++myname;
50
51     while (( i = getopt( argc, argv, "kKcnvrh:p:D:w:d:f:" )) != EOF ) {
52         switch( i ) {
53         case 'k':       /* kerberos bind */
54             kerberos = 2;
55             break;
56         case 'K':       /* kerberos bind, part one only */
57             kerberos = 1;
58             break;
59         case 'c':       /* continuous operation mode */
60             ++contoper;
61             break;
62         case 'h':       /* ldap host */
63             ldaphost = strdup( optarg );
64             break;
65         case 'D':       /* bind DN */
66             binddn = strdup( optarg );
67             break;
68         case 'w':       /* password */
69             passwd = strdup( optarg );
70             break;
71         case 'd':
72 #ifdef LDAP_DEBUG
73             ldap_debug = lber_debug = atoi( optarg );   /* */
74 #else /* LDAP_DEBUG */
75             fprintf( stderr, "compile with -DLDAP_DEBUG for debugging\n" );
76 #endif /* LDAP_DEBUG */
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         default:
94             fprintf( stderr, usage, argv[0] );
95             exit( 1 );
96         }
97     }
98
99     havedn = 0;
100     if (argc - optind == 2) {
101         if (( rdn = strdup( argv[argc - 1] )) == NULL ) {
102             perror( "strdup" );
103             exit( 1 );
104         }
105         if (( entrydn = strdup( argv[argc - 2] )) == NULL ) {
106             perror( "strdup" );
107             exit( 1 );
108         }
109         ++havedn;
110     } else if ( argc - optind != 0 ) {
111         fprintf( stderr, "%s: invalid number of arguments, only two allowed\n", myname);
112         fprintf( stderr, usage, argv[0] );
113         exit( 1 );
114     }
115
116     if ( infile != NULL ) {
117         if (( fp = fopen( infile, "r" )) == NULL ) {
118             perror( infile );
119             exit( 1 );
120         }
121     } else {
122         fp = stdin;
123     }
124
125     if (( ld = ldap_open( ldaphost, ldapport )) == NULL ) {
126         perror( "ldap_open" );
127         exit( 1 );
128     }
129
130     ld->ld_deref = LDAP_DEREF_NEVER;    /* this seems prudent */
131
132     if ( !kerberos ) {
133         authmethod = LDAP_AUTH_SIMPLE;
134     } else if ( kerberos == 1 ) {
135         authmethod = LDAP_AUTH_KRBV41;
136     } else {
137         authmethod = LDAP_AUTH_KRBV4;
138     }
139     if ( ldap_bind_s( ld, binddn, passwd, authmethod ) != LDAP_SUCCESS ) {
140         ldap_perror( ld, "ldap_bind" );
141         exit( 1 );
142     }
143
144     rc = 0;
145     if (havedn)
146         rc = domodrdn(ld, entrydn, rdn, remove);
147     else while ((rc == 0 || contoper) && fgets(buf, sizeof(buf), fp) != NULL) {
148         if ( *buf != '\0' ) {   /* blank lines optional, skip */
149             buf[ strlen( buf ) - 1 ] = '\0';    /* remove nl */
150
151             if ( havedn ) {     /* have DN, get RDN */
152                 if (( rdn = strdup( buf )) == NULL ) {
153                     perror( "strdup" );
154                     exit( 1 );
155                 }
156                 rc = domodrdn(ld, entrydn, rdn, remove);
157                 havedn = 0;
158             } else if ( !havedn ) {     /* don't have DN yet */
159                 if (( entrydn = strdup( buf )) == NULL ) {
160                     perror( "strdup" );
161                     exit( 1 );
162                 }
163                 ++havedn;
164             }
165         }
166     }
167
168     ldap_unbind( ld );
169
170     exit( rc );
171 }
172
173 domodrdn( ld, dn, rdn, remove )
174     LDAP        *ld;
175     char        *dn;
176     char        *rdn;
177     int         remove; /* flag: remove old RDN */
178 {
179     int i;
180
181     if ( verbose ) {
182         printf( "modrdn %s:\n\t%s\n", dn, rdn );
183         if (remove)
184             printf("removing old RDN\n");
185         else
186             printf("keeping old RDN\n");
187     }
188
189     if ( !not ) {
190         i = ldap_modrdn2_s( ld, dn, rdn, remove );
191         if ( i != LDAP_SUCCESS ) {
192             ldap_perror( ld, "ldap_modrdn2_s" );
193         } else if ( verbose ) {
194             printf( "modrdn complete\n" );
195         }
196     } else {
197         i = LDAP_SUCCESS;
198     }
199
200     return( i );
201 }