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