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