]> git.sur5r.net Git - openldap/blob - clients/tools/ldapmodrdn.c
69d99e7f5a469a9f58a2b34ee677427a86148b3c
[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 <lber.h>
8 #include <ldap.h>
9
10 #include "ldapconfig.h"
11
12 static char     *binddn = LDAPMODRDN_BINDDN;
13 static char     *base = LDAPMODRDN_BASE;
14 static char     *passwd = NULL;
15 static char     *ldaphost = LDAPHOST;
16 static int      ldapport = LDAP_PORT;
17 static int      not, verbose, contoper;
18 static LDAP     *ld;
19
20 #ifdef LDAP_DEBUG
21 extern int ldap_debug, lber_debug;
22 #endif /* LDAP_DEBUG */
23
24 #define safe_realloc( ptr, size )       ( ptr == NULL ? malloc( size ) : \
25                                          realloc( ptr, size ))
26
27
28 main( argc, argv )
29     int         argc;
30     char        **argv;
31 {
32     char                *usage = "usage: %s [-nvkc] [-d debug-level] [-h ldaphost] [-p ldapport] [-D binddn] [-w passwd] [ -f file | < entryfile | dn newrdn ]\n";
33     char                *myname,*infile, *p, *entrydn, *rdn, buf[ 4096 ];
34     FILE                *fp;
35     int                 rc, i, kerberos, remove, havedn, authmethod;
36     LDAPMod             **pmods;
37
38     extern char *optarg;
39     extern int  optind;
40
41     infile = NULL;
42     kerberos = not = contoper = verbose = remove = 0;
43
44     myname = (myname = strrchr(argv[0], '/')) == NULL ? argv[0] : ++myname;
45
46     while (( i = getopt( argc, argv, "kKcnvrh:p:D:w:d:f:" )) != EOF ) {
47         switch( i ) {
48         case 'k':       /* kerberos bind */
49             kerberos = 2;
50             break;
51         case 'K':       /* kerberos bind, part one only */
52             kerberos = 1;
53             break;
54         case 'c':       /* continuous operation mode */
55             ++contoper;
56             break;
57         case 'h':       /* ldap host */
58             ldaphost = strdup( optarg );
59             break;
60         case 'D':       /* bind DN */
61             binddn = strdup( optarg );
62             break;
63         case 'w':       /* password */
64             passwd = strdup( optarg );
65             break;
66         case 'd':
67 #ifdef LDAP_DEBUG
68             ldap_debug = lber_debug = atoi( optarg );   /* */
69 #else /* LDAP_DEBUG */
70             fprintf( stderr, "compile with -DLDAP_DEBUG for debugging\n" );
71 #endif /* LDAP_DEBUG */
72             break;
73         case 'f':       /* read from file */
74             infile = strdup( optarg );
75             break;
76         case 'p':
77             ldapport = atoi( optarg );
78             break;
79         case 'n':       /* print adds, don't actually do them */
80             ++not;
81             break;
82         case 'v':       /* verbose mode */
83             verbose++;
84             break;
85         case 'r':       /* remove old RDN */
86             remove++;
87             break;
88         default:
89             fprintf( stderr, usage, argv[0] );
90             exit( 1 );
91         }
92     }
93
94     havedn = 0;
95     if (argc - optind == 2) {
96         if (( rdn = strdup( argv[argc - 1] )) == NULL ) {
97             perror( "strdup" );
98             exit( 1 );
99         }
100         if (( entrydn = strdup( argv[argc - 2] )) == NULL ) {
101             perror( "strdup" );
102             exit( 1 );
103         }
104         ++havedn;
105     } else if ( argc - optind != 0 ) {
106         fprintf( stderr, "%s: invalid number of arguments, only two allowed\n", myname);
107         fprintf( stderr, usage, argv[0] );
108         exit( 1 );
109     }
110
111     if ( infile != NULL ) {
112         if (( fp = fopen( infile, "r" )) == NULL ) {
113             perror( infile );
114             exit( 1 );
115         }
116     } else {
117         fp = stdin;
118     }
119
120     if (( ld = ldap_open( ldaphost, ldapport )) == NULL ) {
121         perror( "ldap_open" );
122         exit( 1 );
123     }
124
125     ld->ld_deref = LDAP_DEREF_NEVER;    /* this seems prudent */
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
168 domodrdn( ld, dn, rdn, remove )
169     LDAP        *ld;
170     char        *dn;
171     char        *rdn;
172     int         remove; /* flag: remove old RDN */
173 {
174     int i;
175
176     if ( verbose ) {
177         printf( "modrdn %s:\n\t%s\n", dn, rdn );
178         if (remove)
179             printf("removing old RDN\n");
180         else
181             printf("keeping old RDN\n");
182     }
183
184     if ( !not ) {
185         i = ldap_modrdn2_s( ld, dn, rdn, remove );
186         if ( i != LDAP_SUCCESS ) {
187             ldap_perror( ld, "ldap_modrdn2_s" );
188         } else if ( verbose ) {
189             printf( "modrdn complete\n" );
190         }
191     } else {
192         i = LDAP_SUCCESS;
193     }
194
195     return( i );
196 }