]> git.sur5r.net Git - openldap/blob - clients/tools/ldapmodrdn.c
bbfead056c5934cb2e0433c4a5993cdc577d603a
[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         /* this seems prudent */
135         ldap_set_option( ld, LDAP_OPT_DEREF, LDAP_DEREF_NEVER);
136
137     if ( !kerberos ) {
138         authmethod = LDAP_AUTH_SIMPLE;
139     } else if ( kerberos == 1 ) {
140         authmethod = LDAP_AUTH_KRBV41;
141     } else {
142         authmethod = LDAP_AUTH_KRBV4;
143     }
144     if ( ldap_bind_s( ld, binddn, passwd, authmethod ) != LDAP_SUCCESS ) {
145         ldap_perror( ld, "ldap_bind" );
146         exit( 1 );
147     }
148
149     rc = 0;
150     if (havedn)
151         rc = domodrdn(ld, entrydn, rdn, remove);
152     else while ((rc == 0 || contoper) && fgets(buf, sizeof(buf), fp) != NULL) {
153         if ( *buf != '\0' ) {   /* blank lines optional, skip */
154             buf[ strlen( buf ) - 1 ] = '\0';    /* remove nl */
155
156             if ( havedn ) {     /* have DN, get RDN */
157                 if (( rdn = strdup( buf )) == NULL ) {
158                     perror( "strdup" );
159                     exit( 1 );
160                 }
161                 rc = domodrdn(ld, entrydn, rdn, remove);
162                 havedn = 0;
163             } else if ( !havedn ) {     /* don't have DN yet */
164                 if (( entrydn = strdup( buf )) == NULL ) {
165                     perror( "strdup" );
166                     exit( 1 );
167                 }
168                 ++havedn;
169             }
170         }
171     }
172
173     ldap_unbind( ld );
174
175     exit( rc );
176 }
177
178 static int domodrdn(
179     LDAP        *ld,
180     char        *dn,
181     char        *rdn,
182     int         remove) /* flag: remove old RDN */
183 {
184     int i;
185
186     if ( verbose ) {
187         printf( "modrdn %s:\n\t%s\n", dn, rdn );
188         if (remove)
189             printf("removing old RDN\n");
190         else
191             printf("keeping old RDN\n");
192     }
193
194     if ( !not ) {
195         i = ldap_modrdn2_s( ld, dn, rdn, remove );
196         if ( i != LDAP_SUCCESS ) {
197             ldap_perror( ld, "ldap_modrdn2_s" );
198         } else if ( verbose ) {
199             printf( "modrdn complete\n" );
200         }
201     } else {
202         i = LDAP_SUCCESS;
203     }
204
205     return( i );
206 }