]> git.sur5r.net Git - openldap/blob - clients/tools/ldapmodrdn.c
5b7a4036218fc888c88323e8c65aedeec8fde830
[openldap] / clients / tools / ldapmodrdn.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6 /* ldapmodrdn.c - generic program to modify an entry's RDN using LDAP.
7  *
8  * Support for MODIFYDN REQUEST V3 (newSuperior) by:
9  * 
10  * Copyright 1999, Juan C. Gomez, All rights reserved.
11  * This software is not subject to any license of Silicon Graphics 
12  * Inc. or Purdue University.
13  *
14  * Redistribution and use in source and binary forms are permitted
15  * without restriction or fee of any kind as long as this notice
16  * is preserved.
17  *
18  */
19
20 #include "portable.h"
21
22 #include <stdio.h>
23
24 #include <ac/stdlib.h>
25
26 #include <ac/ctype.h>
27 #include <ac/signal.h>
28 #include <ac/string.h>
29 #include <ac/unistd.h>
30
31 #include <lber.h>
32 #include <ldap.h>
33
34 static char     *binddn = NULL;
35 static struct berval passwd = { 0, NULL};
36 static char     *ldaphost = NULL;
37 static int      ldapport = 0;
38 #ifdef HAVE_CYRUS_SASL
39 static char     *sasl_authc_id = NULL;
40 static char     *sasl_authz_id = NULL;
41 static char     *sasl_mech = NULL;
42 static int      sasl_integrity = 0;
43 static int      sasl_privacy = 0;
44 #endif
45 static int      use_tls = 0;
46 static int      not, verbose, contoper;
47 static LDAP     *ld;
48
49 static int domodrdn(
50     LDAP        *ld,
51     char        *dn,
52     char        *rdn,
53     char        *newSuperior,
54     int         remove );       /* flag: remove old RDN */
55
56 static void
57 usage( const char *s )
58 {
59         fprintf( stderr,
60 "Rename LDAP entries\n\n"
61 "usage: %s [options] [dn rdn]\n"
62 "       dn rdn: If given, rdn will replace the RDN of the entry specified by DN\n"
63 "               If not given, the list of modifications is read from stdin or\n"
64 "               from the file specified by \"-f file\" (see man page).\n"
65 "options:\n"
66 "       -c\t\tcontinuous operation mode (do not stop on errors)\n"
67 "       -d level\tset LDAP debugging level to `level'\n"
68 "       -D binddn\tbind DN\n"
69 "       -E\t\trequest SASL privacy (-EE to make it critical)\n"
70 "       -f file\t\tdo renames listed in `file'\n"
71 "       -h host\t\tLDAP server\n"
72 "       -I\t\trequest SASL integrity checking (-II to make it\n"
73 "               \tcritical)\n"
74 "       -k\t\tuse Kerberos authentication\n"
75 "       -K\t\tlike -k, but do only step 1 of the Kerberos bind\n"
76 "       -M\t\tenable Manage DSA IT control (-MM to make it critical)\n"
77 "       -n\t\tshow what would be done but don't actually do it\n"
78 "       -p port\t\tport on LDAP server\n"
79 "       -P version\tprocotol version (2 or 3)\n"
80 "       -r\t\tremove old RDN\n"
81 "       -s newsuperior\tnew superior entry\n"
82 "       -U user\t\tSASL authentication identity (username)\n"
83 "       -v\t\trun in verbose mode (diagnostics to standard output)\n"
84 "       -w passwd\tbind passwd (for simple authentication)\n"
85 "       -W\t\tprompt for bind passwd\n"
86 "       -X id\t\tSASL authorization identity (\"dn:<dn>\" or \"u:<user>\")\n"
87 "       -Y mech\t\tSASL mechanism\n"
88 "       -Z\t\trequest the use of TLS (-ZZ to make it critical)\n"
89 ,               s );
90
91         exit( EXIT_FAILURE );
92 }
93
94 int
95 main(int argc, char **argv)
96 {
97     char                *myname,*infile, *entrydn = NULL, *rdn = NULL, buf[ 4096 ];
98     FILE                *fp;
99         int             rc, i, remove, havedn, authmethod, version, want_bindpw, debug, manageDSAit;
100     char        *newSuperior=NULL;
101
102     infile = NULL;
103     not = contoper = verbose = remove = want_bindpw = debug = manageDSAit = 0;
104     authmethod = LDAP_AUTH_SIMPLE;
105         version = -1;
106
107     myname = (myname = strrchr(argv[0], '/')) == NULL ? argv[0] : ++myname;
108
109     while (( i = getopt( argc, argv, "cD:d:Ef:h:IKkMnP:p:rs:U:vWw:X:Y:Z" )) != EOF ) {
110         switch( i ) {
111         case 'k':       /* kerberos bind */
112 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
113                 authmethod = LDAP_AUTH_KRBV4;
114 #else
115                 fprintf( stderr, "%s was not compiled with Kerberos support\n", argv[0] );
116                 return( EXIT_FAILURE );
117 #endif
118                 break;
119         case 'K':       /* kerberos bind, part one only */
120 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
121                 authmethod = LDAP_AUTH_KRBV41;
122 #else
123                 fprintf( stderr, "%s was not compiled with Kerberos support\n", argv[0] );
124                 return( EXIT_FAILURE );
125 #endif
126                 break;
127         case 'c':       /* continuous operation mode */
128             ++contoper;
129             break;
130         case 'h':       /* ldap host */
131             ldaphost = strdup( optarg );
132             break;
133         case 'D':       /* bind DN */
134             binddn = strdup( optarg );
135             break;
136         case 's':       /* newSuperior */
137             newSuperior = strdup( optarg );
138             version = LDAP_VERSION3;    /* This option => force V3 */
139             break;
140         case 'w':       /* password */
141             passwd.bv_val = strdup( optarg );
142                 {
143                         char* p;
144
145                         for( p = optarg; *p == '\0'; p++ ) {
146                                 *p = '*';
147                         }
148                 }
149                 passwd.bv_len = strlen( passwd.bv_val );
150             break;
151         case 'd':
152             debug |= atoi( optarg );
153             break;
154         case 'f':       /* read from file */
155             infile = strdup( optarg );
156             break;
157         case 'p':
158             ldapport = atoi( optarg );
159             break;
160         case 'n':       /* print adds, don't actually do them */
161             ++not;
162             break;
163         case 'v':       /* verbose mode */
164             verbose++;
165             break;
166         case 'r':       /* remove old RDN */
167             remove++;
168             break;
169         case 'M':
170                 /* enable Manage DSA IT */
171                 manageDSAit++;
172                 break;
173         case 'W':
174                 want_bindpw++;
175                 break;
176         case 'P':
177                 switch( atoi(optarg) )
178                 {
179                 case 2:
180                         version = LDAP_VERSION2;
181                         break;
182                 case 3:
183                         version = LDAP_VERSION3;
184                         break;
185                 default:
186                         fprintf( stderr, "protocol version should be 2 or 3\n" );
187                         usage( argv[0] );
188                         return( EXIT_FAILURE );
189                 }
190                 break;
191         case 'I':
192 #ifdef HAVE_CYRUS_SASL
193                 sasl_integrity++;
194                 authmethod = LDAP_AUTH_SASL;
195 #else
196                 fprintf( stderr, "%s was not compiled with SASL support\n",
197                         argv[0] );
198                 return( EXIT_FAILURE );
199 #endif
200                 break;
201         case 'E':
202 #ifdef HAVE_CYRUS_SASL
203                 sasl_privacy++;
204                 authmethod = LDAP_AUTH_SASL;
205 #else
206                 fprintf( stderr, "%s was not compiled with SASL support\n",
207                         argv[0] );
208                 return( EXIT_FAILURE );
209 #endif
210                 break;
211         case 'Y':
212 #ifdef HAVE_CYRUS_SASL
213                 if ( strcasecmp( optarg, "any" ) && strcmp( optarg, "*" ) ) {
214                         sasl_mech = strdup( optarg );
215                 }
216                 authmethod = LDAP_AUTH_SASL;
217 #else
218                 fprintf( stderr, "%s was not compiled with SASL support\n",
219                         argv[0] );
220                 return( EXIT_FAILURE );
221 #endif
222                 break;
223         case 'U':
224 #ifdef HAVE_CYRUS_SASL
225                 sasl_authc_id = strdup( optarg );
226                 authmethod = LDAP_AUTH_SASL;
227 #else
228                 fprintf( stderr, "%s was not compiled with SASL support\n",
229                         argv[0] );
230                 return( EXIT_FAILURE );
231 #endif
232                 break;
233         case 'X':
234 #ifdef HAVE_CYRUS_SASL
235                 sasl_authz_id = strdup( optarg );
236                 authmethod = LDAP_AUTH_SASL;
237 #else
238                 fprintf( stderr, "%s was not compiled with SASL support\n",
239                         argv[0] );
240                 return( EXIT_FAILURE );
241 #endif
242                 break;
243         case 'Z':
244 #ifdef HAVE_TLS
245                 use_tls++;
246 #else
247                 fprintf( stderr, "%s was not compiled with TLS support\n",
248                         argv[0] );
249                 return( EXIT_FAILURE );
250 #endif
251                 break;
252         default:
253             usage( argv[0] );
254             return( EXIT_FAILURE );
255         }
256     }
257
258         if ( ( authmethod == LDAP_AUTH_KRBV4 ) || ( authmethod ==
259                         LDAP_AUTH_KRBV41 ) ) {
260                 if( version > LDAP_VERSION2 ) {
261                         fprintf( stderr, "Kerberos requires LDAPv2\n" );
262                         return( EXIT_FAILURE );
263                 }
264                 version = LDAP_VERSION2;
265         }
266         else if ( authmethod == LDAP_AUTH_SASL ) {
267                 if( version != -1 && version != LDAP_VERSION3 ) {
268                         fprintf( stderr, "SASL requires LDAPv3\n" );
269                         return( EXIT_FAILURE );
270                 }
271                 version = LDAP_VERSION3;
272         }
273
274         if( manageDSAit ) {
275                 if( version != -1 && version != LDAP_VERSION3 ) {
276                         fprintf(stderr, "manage DSA control requires LDAPv3\n");
277                         return EXIT_FAILURE;
278                 }
279                 version = LDAP_VERSION3;
280         }
281
282         if( use_tls ) {
283                 if( version != -1 && version != LDAP_VERSION3 ) {
284                         fprintf(stderr, "Start TLS requires LDAPv3\n");
285                         return EXIT_FAILURE;
286                 }
287                 version = LDAP_VERSION3;
288         }
289
290     if (newSuperior != NULL) {
291                 if (version == LDAP_VERSION2) {
292                         fprintf( stderr,
293                                 "%s: version conflict!, -s newSuperior requires LDAPv3\n",
294                                 myname);
295                         usage( argv[0] );
296                         return( EXIT_FAILURE );
297                 }
298                 version = LDAP_VERSION3;
299     }
300     
301     havedn = 0;
302     if (argc - optind == 2) {
303         if (( rdn = strdup( argv[argc - 1] )) == NULL ) {
304             perror( "strdup" );
305             return( EXIT_FAILURE );
306         }
307         if (( entrydn = strdup( argv[argc - 2] )) == NULL ) {
308             perror( "strdup" );
309             return( EXIT_FAILURE );
310         }
311         ++havedn;
312     } else if ( argc - optind != 0 ) {
313         fprintf( stderr, "%s: invalid number of arguments, only two allowed\n", myname);
314         usage( argv[0] );
315         return( EXIT_FAILURE );
316     }
317
318     if ( infile != NULL ) {
319         if (( fp = fopen( infile, "r" )) == NULL ) {
320             perror( infile );
321             return( EXIT_FAILURE );
322         }
323     } else {
324         fp = stdin;
325     }
326
327         if ( debug ) {
328                 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
329                         fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
330                 }
331                 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
332                         fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
333                 }
334         }
335
336 #ifdef SIGPIPE
337         (void) SIGNAL( SIGPIPE, SIG_IGN );
338 #endif
339
340     if (( ld = ldap_init( ldaphost, ldapport )) == NULL ) {
341         perror( "ldap_init" );
342         return( EXIT_FAILURE );
343     }
344
345         /* this seems prudent */
346         {
347                 int deref = LDAP_DEREF_NEVER;
348                 ldap_set_option( ld, LDAP_OPT_DEREF, &deref);
349         }
350         /* don't chase referrals */
351         ldap_set_option( ld, LDAP_OPT_REFERRALS, LDAP_OPT_OFF );
352
353
354         if (version != -1 &&
355                 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version ) != LDAP_OPT_SUCCESS)
356         {
357                 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n", version );
358         }
359
360         if ( use_tls && ldap_start_tls( ld, NULL, NULL ) != LDAP_SUCCESS ) {
361                 if ( use_tls > 1 ) {
362                         ldap_perror( ld, "ldap_start_tls" );
363                         return( EXIT_FAILURE );
364                 }
365         }
366
367         if (want_bindpw)
368                 passwd.bv_val = getpass("Enter LDAP Password: ");
369                 passwd.bv_len = strlen( passwd.bv_val );
370
371         if ( authmethod == LDAP_AUTH_SASL ) {
372 #ifdef HAVE_CYRUS_SASL
373                 int     minssf = 0, maxssf = 0;
374
375                 if ( sasl_integrity > 0 )
376                         maxssf = 1;
377                 if ( sasl_integrity > 1 )
378                         minssf = 1;
379                 if ( sasl_privacy > 0 )
380                         maxssf = 100000; /* Something big value */
381                 if ( sasl_privacy > 1 )
382                         minssf = 56;
383                 
384                 if ( ldap_set_option( ld, LDAP_OPT_X_SASL_MINSSF,
385                         (void *)&minssf ) != LDAP_OPT_SUCCESS ) {
386                         fprintf( stderr, "Could not set LDAP_OPT_X_SASL_MINSSF"
387                                 "%d\n", minssf);
388                         return( EXIT_FAILURE );
389                 }
390                 if ( ldap_set_option( ld, LDAP_OPT_X_SASL_MAXSSF,
391                         (void *)&maxssf ) != LDAP_OPT_SUCCESS ) {
392                         fprintf( stderr, "Could not set LDAP_OPT_X_SASL_MAXSSF"
393                                 "%d\n", maxssf);
394                         return( EXIT_FAILURE );
395                 }
396                 
397                 rc = ldap_negotiated_sasl_bind_s( ld, binddn, sasl_authc_id,
398                                 sasl_authz_id, sasl_mech,
399                                 passwd.bv_len ? &passwd : NULL,
400                                 NULL, NULL );
401
402                 if( rc != LDAP_SUCCESS ) {
403                         ldap_perror( ld, "ldap_negotiated_sasl_bind_s" );
404                         return( EXIT_FAILURE );
405                 }
406 #else
407                 fprintf( stderr, "%s was not compiled with SASL support\n",
408                         argv[0] );
409                 return( EXIT_FAILURE );
410 #endif
411         }
412         else {
413                 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
414                                 != LDAP_SUCCESS ) {
415                         ldap_perror( ld, "ldap_bind" );
416                         return( EXIT_FAILURE );
417                 }
418         }
419
420         if ( manageDSAit ) {
421                 int err;
422                 LDAPControl c;
423                 LDAPControl *ctrls[2];
424                 ctrls[0] = &c;
425                 ctrls[1] = NULL;
426
427                 c.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
428                 c.ldctl_value.bv_val = NULL;
429                 c.ldctl_value.bv_len = 0;
430                 c.ldctl_iscritical = manageDSAit > 1;
431
432                 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, &ctrls );
433
434                 if( err != LDAP_OPT_SUCCESS ) {
435                         fprintf( stderr, "Could not set Manage DSA IT Control\n" );
436                         if( c.ldctl_iscritical ) {
437                                 exit( EXIT_FAILURE );
438                         }
439                 }
440         }
441
442     rc = 0;
443     if (havedn)
444         rc = domodrdn( ld, entrydn, rdn, newSuperior, remove );
445     else while ((rc == 0 || contoper) && fgets(buf, sizeof(buf), fp) != NULL) {
446         if ( *buf != '\0' ) {   /* blank lines optional, skip */
447             buf[ strlen( buf ) - 1 ] = '\0';    /* remove nl */
448
449             if ( havedn ) {     /* have DN, get RDN */
450                 if (( rdn = strdup( buf )) == NULL ) {
451                     perror( "strdup" );
452                     return( EXIT_FAILURE );
453                 }
454                 rc = domodrdn(ld, entrydn, rdn, newSuperior, remove );
455                 havedn = 0;
456             } else if ( !havedn ) {     /* don't have DN yet */
457                 if (( entrydn = strdup( buf )) == NULL ) {
458                     perror( "strdup" );
459                     return( EXIT_FAILURE );
460                 }
461                 ++havedn;
462             }
463         }
464     }
465
466     ldap_unbind( ld );
467
468         /* UNREACHABLE */
469         return( rc );
470 }
471
472 static int domodrdn(
473     LDAP        *ld,
474     char        *dn,
475     char        *rdn,
476     char        *newSuperior,
477     int         remove ) /* flag: remove old RDN */
478 {
479     int i;
480
481     if ( verbose ) {
482                 printf( "Renaming \"%s\"\n", dn );
483                 printf( "\tnew rdn=\"%s\" (%s old rdn)\n",
484                         rdn, remove ? "delete" : "keep" );
485                 if( newSuperior != NULL ) {
486                         printf("\tnew parent=\"%s\"\n", newSuperior);
487                 }
488         }
489
490     if ( !not ) {
491         i = ldap_rename2_s( ld, dn, rdn, newSuperior, remove );
492         if ( i != LDAP_SUCCESS ) {
493             ldap_perror( ld, "ldap_rename2_s" );
494         } else if ( verbose ) {
495             printf( "modrdn complete\n" );
496         }
497     } else {
498         i = LDAP_SUCCESS;
499     }
500
501     return( i );
502 }