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