]> git.sur5r.net Git - openldap/blob - clients/tools/ldapmodrdn.c
afbf959d5f94f8564d8801982c6783d48797455e
[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 <ldap.h>
32
33 static char     *binddn = NULL;
34 static struct berval passwd = { 0, NULL};
35 static char     *ldaphost = NULL;
36 static int      ldapport = 0;
37 #ifdef HAVE_CYRUS_SASL
38 static char     *sasl_authc_id = NULL;
39 static char     *sasl_authz_id = NULL;
40 static char     *sasl_mech = NULL;
41 static int      sasl_integrity = 0;
42 static int      sasl_privacy = 0;
43 #endif
44 static int      use_tls = 0;
45 static int      not, verbose, contoper;
46 static LDAP     *ld;
47
48 static int domodrdn(
49     LDAP        *ld,
50     char        *dn,
51     char        *rdn,
52     char        *newSuperior,
53     int         remove );       /* flag: remove old RDN */
54
55 static void
56 usage( const char *s )
57 {
58         fprintf( stderr,
59 "Rename LDAP entries\n\n"
60 "usage: %s [options] [dn rdn]\n"
61 "       dn rdn: If given, rdn will replace the RDN of the entry specified by DN\n"
62 "               If not given, the list of modifications is read from stdin or\n"
63 "               from the file specified by \"-f file\" (see man page).\n"
64 "options:\n"
65 "       -c\t\tcontinuous operation mode (do not stop on errors)\n"
66 "       -d level\tset LDAP debugging level to `level'\n"
67 "       -D binddn\tbind DN\n"
68 "       -E\t\trequest SASL privacy (-EE to make it critical)\n"
69 "       -f file\t\tdo renames listed in `file'\n"
70 "       -h host\t\tLDAP server\n"
71 "       -I\t\trequest SASL integrity checking (-II to make it\n"
72 "               \tcritical)\n"
73 "       -k\t\tuse Kerberos authentication\n"
74 "       -K\t\tlike -k, but do only step 1 of the Kerberos bind\n"
75 "       -M\t\tenable Manage DSA IT control (-MM to make it critical)\n"
76 "       -n\t\tshow what would be done but don't actually do it\n"
77 "       -p port\t\tport on LDAP server\n"
78 "       -P version\tprocotol version (2 or 3)\n"
79 "       -r\t\tremove old RDN\n"
80 "       -s newsuperior\tnew superior entry\n"
81 "       -U user\t\tSASL authentication identity (username)\n"
82 "       -v\t\trun in verbose mode (diagnostics to standard output)\n"
83 "       -w passwd\tbind passwd (for simple authentication)\n"
84 "       -W\t\tprompt for bind passwd\n"
85 "       -X id\t\tSASL authorization identity (\"dn:<dn>\" or \"u:<user>\")\n"
86 "       -Y mech\t\tSASL mechanism\n"
87 "       -Z\t\tissue Start TLS request (-ZZ to require successful response)\n"
88 ,               s );
89
90         exit( EXIT_FAILURE );
91 }
92
93 int
94 main(int argc, char **argv)
95 {
96     char                *myname,*infile, *entrydn = NULL, *rdn = NULL, buf[ 4096 ];
97     FILE                *fp;
98         int             rc, i, remove, havedn, authmethod, version, want_bindpw, debug, manageDSAit;
99     char        *newSuperior=NULL;
100
101     infile = NULL;
102     not = contoper = verbose = remove = want_bindpw = debug = manageDSAit = 0;
103     authmethod = LDAP_AUTH_SIMPLE;
104         version = -1;
105
106     myname = (myname = strrchr(argv[0], '/')) == NULL ? argv[0] : ++myname;
107
108     while (( i = getopt( argc, argv, "cD:d:Ef:h:IKkMnP:p:rs:U:vWw:X:Y:Z" )) != EOF ) {
109         switch( i ) {
110         case 'k':       /* kerberos bind */
111 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
112                 authmethod = LDAP_AUTH_KRBV4;
113 #else
114                 fprintf( stderr, "%s was not compiled with Kerberos support\n", argv[0] );
115                 return( EXIT_FAILURE );
116 #endif
117                 break;
118         case 'K':       /* kerberos bind, part one only */
119 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
120                 authmethod = LDAP_AUTH_KRBV41;
121 #else
122                 fprintf( stderr, "%s was not compiled with Kerberos support\n", argv[0] );
123                 return( EXIT_FAILURE );
124 #endif
125                 break;
126         case 'c':       /* continuous operation mode */
127             ++contoper;
128             break;
129         case 'h':       /* ldap host */
130             ldaphost = strdup( optarg );
131             break;
132         case 'D':       /* bind DN */
133             binddn = strdup( optarg );
134             break;
135         case 's':       /* newSuperior */
136             newSuperior = strdup( optarg );
137             version = LDAP_VERSION3;    /* This option => force V3 */
138             break;
139         case 'w':       /* password */
140             passwd.bv_val = strdup( optarg );
141                 {
142                         char* p;
143
144                         for( p = optarg; *p == '\0'; p++ ) {
145                                 *p = '*';
146                         }
147                 }
148                 passwd.bv_len = strlen( passwd.bv_val );
149             break;
150         case 'd':
151             debug |= atoi( optarg );
152             break;
153         case 'f':       /* read from file */
154             infile = strdup( optarg );
155             break;
156         case 'p':
157             ldapport = atoi( optarg );
158             break;
159         case 'n':       /* print adds, don't actually do them */
160             ++not;
161             break;
162         case 'v':       /* verbose mode */
163             verbose++;
164             break;
165         case 'r':       /* remove old RDN */
166             remove++;
167             break;
168         case 'M':
169                 /* enable Manage DSA IT */
170                 manageDSAit++;
171                 break;
172         case 'W':
173                 want_bindpw++;
174                 break;
175         case 'P':
176                 switch( atoi(optarg) )
177                 {
178                 case 2:
179                         version = LDAP_VERSION2;
180                         break;
181                 case 3:
182                         version = LDAP_VERSION3;
183                         break;
184                 default:
185                         fprintf( stderr, "protocol version should be 2 or 3\n" );
186                         usage( argv[0] );
187                         return( EXIT_FAILURE );
188                 }
189                 break;
190         case 'I':
191 #ifdef HAVE_CYRUS_SASL
192                 sasl_integrity++;
193                 authmethod = LDAP_AUTH_SASL;
194 #else
195                 fprintf( stderr, "%s was not compiled with SASL support\n",
196                         argv[0] );
197                 return( EXIT_FAILURE );
198 #endif
199                 break;
200         case 'E':
201 #ifdef HAVE_CYRUS_SASL
202                 sasl_privacy++;
203                 authmethod = LDAP_AUTH_SASL;
204 #else
205                 fprintf( stderr, "%s was not compiled with SASL support\n",
206                         argv[0] );
207                 return( EXIT_FAILURE );
208 #endif
209                 break;
210         case 'Y':
211 #ifdef HAVE_CYRUS_SASL
212                 if ( strcasecmp( optarg, "any" ) && strcmp( optarg, "*" ) ) {
213                         sasl_mech = strdup( optarg );
214                 }
215                 authmethod = LDAP_AUTH_SASL;
216 #else
217                 fprintf( stderr, "%s was not compiled with SASL support\n",
218                         argv[0] );
219                 return( EXIT_FAILURE );
220 #endif
221                 break;
222         case 'U':
223 #ifdef HAVE_CYRUS_SASL
224                 sasl_authc_id = strdup( optarg );
225                 authmethod = LDAP_AUTH_SASL;
226 #else
227                 fprintf( stderr, "%s was not compiled with SASL support\n",
228                         argv[0] );
229                 return( EXIT_FAILURE );
230 #endif
231                 break;
232         case 'X':
233 #ifdef HAVE_CYRUS_SASL
234                 sasl_authz_id = strdup( optarg );
235                 authmethod = LDAP_AUTH_SASL;
236 #else
237                 fprintf( stderr, "%s was not compiled with SASL support\n",
238                         argv[0] );
239                 return( EXIT_FAILURE );
240 #endif
241                 break;
242         case 'Z':
243 #ifdef HAVE_TLS
244                 use_tls++;
245 #else
246                 fprintf( stderr, "%s was not compiled with TLS support\n",
247                         argv[0] );
248                 return( EXIT_FAILURE );
249 #endif
250                 break;
251         default:
252             usage( argv[0] );
253             return( EXIT_FAILURE );
254         }
255     }
256
257         if ( ( authmethod == LDAP_AUTH_KRBV4 ) || ( authmethod ==
258                         LDAP_AUTH_KRBV41 ) ) {
259                 if( version > LDAP_VERSION2 ) {
260                         fprintf( stderr, "Kerberos requires LDAPv2\n" );
261                         return( EXIT_FAILURE );
262                 }
263                 version = LDAP_VERSION2;
264         }
265         else if ( authmethod == LDAP_AUTH_SASL ) {
266                 if( version != -1 && version != LDAP_VERSION3 ) {
267                         fprintf( stderr, "SASL requires LDAPv3\n" );
268                         return( EXIT_FAILURE );
269                 }
270                 version = LDAP_VERSION3;
271         }
272
273         if( manageDSAit ) {
274                 if( version != -1 && version != LDAP_VERSION3 ) {
275                         fprintf(stderr, "manage DSA control requires LDAPv3\n");
276                         return EXIT_FAILURE;
277                 }
278                 version = LDAP_VERSION3;
279         }
280
281         if( use_tls ) {
282                 if( version != -1 && version != LDAP_VERSION3 ) {
283                         fprintf(stderr, "Start TLS requires LDAPv3\n");
284                         return EXIT_FAILURE;
285                 }
286                 version = LDAP_VERSION3;
287         }
288
289     if (newSuperior != NULL) {
290                 if (version == LDAP_VERSION2) {
291                         fprintf( stderr,
292                                 "%s: version conflict!, -s newSuperior requires LDAPv3\n",
293                                 myname);
294                         usage( argv[0] );
295                         return( EXIT_FAILURE );
296                 }
297                 version = LDAP_VERSION3;
298     }
299     
300     havedn = 0;
301     if (argc - optind == 2) {
302         if (( rdn = strdup( argv[argc - 1] )) == NULL ) {
303             perror( "strdup" );
304             return( EXIT_FAILURE );
305         }
306         if (( entrydn = strdup( argv[argc - 2] )) == NULL ) {
307             perror( "strdup" );
308             return( EXIT_FAILURE );
309         }
310         ++havedn;
311     } else if ( argc - optind != 0 ) {
312         fprintf( stderr, "%s: invalid number of arguments, only two allowed\n", myname);
313         usage( argv[0] );
314         return( EXIT_FAILURE );
315     }
316
317     if ( infile != NULL ) {
318         if (( fp = fopen( infile, "r" )) == NULL ) {
319             perror( infile );
320             return( EXIT_FAILURE );
321         }
322     } else {
323         fp = stdin;
324     }
325
326         if ( debug ) {
327                 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
328                         fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
329                 }
330                 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
331                         fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
332                 }
333         }
334
335 #ifdef SIGPIPE
336         (void) SIGNAL( SIGPIPE, SIG_IGN );
337 #endif
338
339     if (( ld = ldap_init( ldaphost, ldapport )) == NULL ) {
340         perror( "ldap_init" );
341         return( EXIT_FAILURE );
342     }
343
344         /* this seems prudent */
345         {
346                 int deref = LDAP_DEREF_NEVER;
347                 ldap_set_option( ld, LDAP_OPT_DEREF, &deref);
348         }
349         /* don't chase referrals */
350         ldap_set_option( ld, LDAP_OPT_REFERRALS, LDAP_OPT_OFF );
351
352
353         if (version != -1 &&
354                 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version ) != LDAP_OPT_SUCCESS)
355         {
356                 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n", version );
357         }
358
359         if ( use_tls && ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS ) {
360                 if ( use_tls > 1 ) {
361                         ldap_perror( ld, "ldap_start_tls" );
362                         return( EXIT_FAILURE );
363                 }
364         }
365
366         if (want_bindpw) {
367                 passwd.bv_val = getpassphrase("Enter LDAP Password: ");
368                 passwd.bv_len = strlen( passwd.bv_val );
369         }
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 }