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