]> git.sur5r.net Git - openldap/blob - clients/tools/ldapmodrdn.c
d2450103b947345129f04cf0bf7a4a0e83f46cec
[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 #include <ctype.h>
8
9 #include <ac/string.h>
10 #include <ac/unistd.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 static int domodrdn LDAP_P((
33     LDAP        *ld,
34     char        *dn,
35     char        *rdn,
36     int         remove));       /* flag: remove old RDN */
37
38 main( argc, argv )
39     int         argc;
40     char        **argv;
41 {
42     char                *usage = "usage: %s [-nvkc] [-d debug-level] [-h ldaphost] [-p ldapport] [-D binddn] [-w passwd] [ -f file | < entryfile | dn newrdn ]\n";
43     char                *myname,*infile, *entrydn, *rdn, buf[ 4096 ];
44     FILE                *fp;
45     int                 rc, i, kerberos, remove, havedn, authmethod;
46
47     extern char *optarg;
48     extern int  optind;
49
50     infile = NULL;
51     kerberos = not = contoper = verbose = remove = 0;
52
53     myname = (myname = strrchr(argv[0], '/')) == NULL ? argv[0] : ++myname;
54
55     while (( i = getopt( argc, argv, "kKcnvrh:p:D:w:d:f:" )) != EOF ) {
56         switch( i ) {
57         case 'k':       /* kerberos bind */
58             kerberos = 2;
59             break;
60         case 'K':       /* kerberos bind, part one only */
61             kerberos = 1;
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 #ifdef LDAP_DEBUG
77             ldap_debug = lber_debug = atoi( optarg );   /* */
78 #else /* LDAP_DEBUG */
79             fprintf( stderr, "compile with -DLDAP_DEBUG for debugging\n" );
80 #endif /* LDAP_DEBUG */
81             break;
82         case 'f':       /* read from file */
83             infile = strdup( optarg );
84             break;
85         case 'p':
86             ldapport = atoi( optarg );
87             break;
88         case 'n':       /* print adds, don't actually do them */
89             ++not;
90             break;
91         case 'v':       /* verbose mode */
92             verbose++;
93             break;
94         case 'r':       /* remove old RDN */
95             remove++;
96             break;
97         default:
98             fprintf( stderr, usage, argv[0] );
99             exit( 1 );
100         }
101     }
102
103     havedn = 0;
104     if (argc - optind == 2) {
105         if (( rdn = strdup( argv[argc - 1] )) == NULL ) {
106             perror( "strdup" );
107             exit( 1 );
108         }
109         if (( entrydn = strdup( argv[argc - 2] )) == NULL ) {
110             perror( "strdup" );
111             exit( 1 );
112         }
113         ++havedn;
114     } else if ( argc - optind != 0 ) {
115         fprintf( stderr, "%s: invalid number of arguments, only two allowed\n", myname);
116         fprintf( stderr, usage, argv[0] );
117         exit( 1 );
118     }
119
120     if ( infile != NULL ) {
121         if (( fp = fopen( infile, "r" )) == NULL ) {
122             perror( infile );
123             exit( 1 );
124         }
125     } else {
126         fp = stdin;
127     }
128
129     if (( ld = ldap_open( ldaphost, ldapport )) == NULL ) {
130         perror( "ldap_open" );
131         exit( 1 );
132     }
133
134 #if LDAP_VERSION > LDAP_VERSION2
135         /* this seems prudent */
136         ldap_set_option( LDAP_OPT_DEREF, LDAP_DEREF_NEVER);
137 #endif
138
139     if ( !kerberos ) {
140         authmethod = LDAP_AUTH_SIMPLE;
141     } else if ( kerberos == 1 ) {
142         authmethod = LDAP_AUTH_KRBV41;
143     } else {
144         authmethod = LDAP_AUTH_KRBV4;
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
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 }