]> git.sur5r.net Git - openldap/blob - clients/tools/ldapmodrdn.c
4a7b5cfc3482e38fd881f0a5f307bd022cb5b8a8
[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 #include "lutil_ldap.h"
33 #include "ldap_defaults.h"
34
35 static char     *binddn = NULL;
36 static struct berval passwd = { 0, NULL };
37 static char     *ldaphost = NULL;
38 static int      ldapport = 0;
39 #ifdef HAVE_CYRUS_SASL
40 static char     *sasl_authc_id = NULL;
41 static char     *sasl_authz_id = NULL;
42 static char     *sasl_mech = NULL;
43 static char     *sasl_secprops = NULL;
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 "Rename options:\n"
66 "       -r\t\tremove old RDN\n"
67 "       -s newsuperior\tnew superior entry\n"
68
69 "common options:\n"
70 "       -c\t\tcontinuous operation mode (do not stop on errors)\n"
71 "       -C\t\tchase referrals\n"
72 "       -d level\tset LDAP debugging level to `level'\n"
73 "       -D binddn\tbind DN\n"
74 "       -f file\t\tread operations from `file'\n"
75 "       -h host\t\tLDAP server\n"
76 "       -k\t\tuse Kerberos authentication\n"
77 "       -K\t\tlike -k, but do only step 1 of the Kerberos bind\n"
78 "       -M\t\tenable Manage DSA IT control (-MM to make it critical)\n"
79 "       -n\t\tshow what would be done but don't actually do it\n"
80 "       -O secprops\tSASL security properties\n"
81 "       -p port\t\tport on LDAP server\n"
82 "       -P version\tprocotol version (default: 3)\n"
83 "       -U user\t\tSASL authentication identity (username)\n"
84 "       -v\t\trun in verbose mode (diagnostics to standard output)\n"
85 "       -w passwd\tbind passwd (for simple authentication)\n"
86 "       -W\t\tprompt for bind passwd\n"
87 "       -X id\t\tSASL authorization identity (\"dn:<dn>\" or \"u:<user>\")\n"
88 "       -Y mech\t\tSASL mechanism\n"
89 "       -Z\t\tissue Start TLS request (-ZZ to require successful response)\n"
90 ,               s );
91
92         exit( EXIT_FAILURE );
93 }
94
95 int
96 main(int argc, char **argv)
97 {
98     char                *prog,*infile, *entrydn = NULL, *rdn = NULL, buf[ 4096 ];
99     FILE                *fp;
100         int             rc, i, remove, havedn, authmethod, version, want_bindpw, debug, manageDSAit;
101         int             referrals;
102     char        *newSuperior=NULL;
103
104     infile = NULL;
105     not = contoper = verbose = remove = want_bindpw =
106                 debug = manageDSAit = referrals = 0;
107     authmethod = LDAP_AUTH_SIMPLE;
108         version = -1;
109
110     prog = (prog = strrchr(argv[0], *LDAP_DIRSEP)) == NULL ? argv[0] : ++prog;
111
112     while (( i = getopt( argc, argv, "rs:" "cCd:D:f:h:kKMnO:p:P:U:vw:WxX:Y:Z" )) != EOF ) {
113         switch( i ) {
114         /* Modrdn Options */
115         case 's':       /* newSuperior */
116                 if( version == LDAP_VERSION2 ) {
117                         fprintf( stderr, "%s: -X incompatible with LDAPv%d\n",
118                                 prog, version );
119                         return EXIT_FAILURE;
120                 }
121             newSuperior = strdup( optarg );
122             version = LDAP_VERSION3;
123             break;
124         case 'r':       /* remove old RDN */
125             remove++;
126             break;
127
128         /* Common Options */
129         case 'C':
130                 referrals++;
131                 break;
132         case 'd':
133             debug |= atoi( optarg );
134             break;
135         case 'D':       /* bind DN */
136             binddn = strdup( optarg );
137             break;
138         case 'h':       /* ldap host */
139             ldaphost = strdup( optarg );
140             break;
141         case 'k':       /* kerberos bind */
142 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
143                 if( version > LDAP_VERSION2 ) {
144                         fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
145                                 prog, version );
146                         return EXIT_FAILURE;
147                 }
148
149                 if( authmethod != -1 ) {
150                         fprintf( stderr, "%s: -k incompatible with previous "
151                                 "authentication choice\n", prog );
152                         return EXIT_FAILURE;
153                 }
154                         
155                 authmethod = LDAP_AUTH_KRBV4;
156 #else
157                 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
158                 return EXIT_FAILURE;
159 #endif
160             break;
161         case 'K':       /* kerberos bind, part one only */
162 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
163                 if( version > LDAP_VERSION2 ) {
164                         fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
165                                 prog, version );
166                         return EXIT_FAILURE;
167                 }
168                 if( authmethod != -1 ) {
169                         fprintf( stderr, "%s: incompatible with previous "
170                                 "authentication choice\n", prog );
171                         return EXIT_FAILURE;
172                 }
173
174                 authmethod = LDAP_AUTH_KRBV41;
175 #else
176                 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
177                 return( EXIT_FAILURE );
178 #endif
179             break;
180         case 'M':
181                 /* enable Manage DSA IT */
182                 if( version == LDAP_VERSION2 ) {
183                         fprintf( stderr, "%s: -M incompatible with LDAPv%d\n",
184                                 prog, version );
185                         return EXIT_FAILURE;
186                 }
187                 manageDSAit++;
188                 version = LDAP_VERSION3;
189                 break;
190         case 'n':       /* print deletes, don't actually do them */
191             ++not;
192             break;
193         case 'O':
194 #ifdef HAVE_CYRUS_SASL
195                 if( version == LDAP_VERSION2 ) {
196                         fprintf( stderr, "%s -O incompatible with LDAPv%d\n",
197                                 prog, version );
198                         return EXIT_FAILURE;
199                 }
200                 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
201                         fprintf( stderr, "%s: incompatible previous "
202                                 "authentication choice\n", prog );
203                         return EXIT_FAILURE;
204                 }
205                 sasl_secprops = strdup( optarg );
206                 authmethod = LDAP_AUTH_SASL;
207                 version = LDAP_VERSION3;
208 #else
209                 fprintf( stderr, "%s: not compiled with SASL support\n",
210                         prog );
211                 return( EXIT_FAILURE );
212 #endif
213                 break;
214         case 'p':
215             ldapport = atoi( optarg );
216             break;
217         case 'P':
218                 switch( atoi(optarg) ) {
219                 case 2:
220                         if( version == LDAP_VERSION3 ) {
221                                 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
222                                         prog, version );
223                                 return EXIT_FAILURE;
224                         }
225                         version = LDAP_VERSION2;
226                         break;
227                 case 3:
228                         if( version == LDAP_VERSION2 ) {
229                                 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
230                                         prog, version );
231                                 return EXIT_FAILURE;
232                         }
233                         version = LDAP_VERSION3;
234                         break;
235                 default:
236                         fprintf( stderr, "%s: protocol version should be 2 or 3\n",
237                                 prog );
238                         usage( prog );
239                         return( EXIT_FAILURE );
240                 } break;
241         case 'U':
242 #ifdef HAVE_CYRUS_SASL
243                 if( version == LDAP_VERSION2 ) {
244                         fprintf( stderr, "%s: -U incompatible with version %d\n",
245                                 prog, version );
246                         return EXIT_FAILURE;
247                 }
248                 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
249                         fprintf( stderr, "%s: incompatible previous "
250                                 "authentication choice\n",
251                                 prog );
252                         return EXIT_FAILURE;
253                 }
254                 authmethod = LDAP_AUTH_SASL;
255                 version = LDAP_VERSION3;
256
257                 sasl_authc_id = strdup( optarg );
258                 authmethod = LDAP_AUTH_SASL;
259 #else
260                 fprintf( stderr, "%s: was not compiled with SASL support\n",
261                         prog );
262                 return( EXIT_FAILURE );
263 #endif
264                 break;
265         case 'v':       /* verbose mode */
266             verbose++;
267             break;
268         case 'w':       /* password */
269             passwd.bv_val = strdup( optarg );
270                 {
271                         char* p;
272
273                         for( p = optarg; *p == '\0'; p++ ) {
274                                 *p = '*';
275                         }
276                 }
277                 passwd.bv_len = strlen( passwd.bv_val );
278             break;
279         case 'W':
280                 want_bindpw++;
281                 break;
282         case 'Y':
283 #ifdef HAVE_CYRUS_SASL
284                 if( version == LDAP_VERSION2 ) {
285                         fprintf( stderr, "%s: -Y incompatible with version %d\n",
286                                 prog, version );
287                         return EXIT_FAILURE;
288                 }
289                 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
290                         fprintf( stderr, "%s: incompatible with authentication choice\n", prog );
291                         return EXIT_FAILURE;
292                 }
293
294                 authmethod = LDAP_AUTH_SASL;
295                 version = LDAP_VERSION3;
296 #else
297                 fprintf( stderr, "%s: was not compiled with SASL support\n",
298                         prog );
299                 return( EXIT_FAILURE );
300 #endif
301                 break;
302         case 'x':
303                 if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
304                         fprintf( stderr, "%s: incompatible with previous "
305                                 "authentication choice\n", prog );
306                         return EXIT_FAILURE;
307                 }
308                 authmethod = LDAP_AUTH_SIMPLE;
309                 break;
310         case 'X':
311 #ifdef HAVE_CYRUS_SASL
312                 if( version == LDAP_VERSION2 ) {
313                         fprintf( stderr, "%s: -X incompatible with LDAPv%d\n",
314                                 prog, version );
315                         return EXIT_FAILURE;
316                 }
317                 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
318                         fprintf( stderr, "%s: -X incompatible with "
319                                 "authentication choice\n", prog );
320                         return EXIT_FAILURE;
321                 }
322                 authmethod = LDAP_AUTH_SASL;
323                 version = LDAP_VERSION3;
324
325                 sasl_authz_id = strdup( optarg );
326                 authmethod = LDAP_AUTH_SASL;
327 #else
328                 fprintf( stderr, "%s: not compiled with SASL support\n",
329                         prog );
330                 return( EXIT_FAILURE );
331 #endif
332                 break;
333         case 'Z':
334 #ifdef HAVE_TLS
335                 if( version == LDAP_VERSION2 ) {
336                         fprintf( stderr, "%s -Z incompatible with version %d\n",
337                                 prog, version );
338                         return EXIT_FAILURE;
339                 }
340                 version = LDAP_VERSION3;
341                 use_tls++;
342 #else
343                 fprintf( stderr, "%s: not compiled with TLS support\n",
344                         prog );
345                 return( EXIT_FAILURE );
346 #endif
347                 break;
348         default:
349             usage( argv[0] );
350             return( EXIT_FAILURE );
351         }
352     }
353
354         if (version == -1) {
355                 version = LDAP_VERSION3;
356         }
357         if (authmethod == -1 && version > LDAP_VERSION2) {
358                 authmethod = LDAP_AUTH_SASL;
359         }
360
361     havedn = 0;
362     if (argc - optind == 2) {
363         if (( rdn = strdup( argv[argc - 1] )) == NULL ) {
364             perror( "strdup" );
365             return( EXIT_FAILURE );
366         }
367         if (( entrydn = strdup( argv[argc - 2] )) == NULL ) {
368             perror( "strdup" );
369             return( EXIT_FAILURE );
370         }
371         ++havedn;
372     } else if ( argc - optind != 0 ) {
373         fprintf( stderr, "%s: invalid number of arguments, only two allowed\n", prog);
374         usage( argv[0] );
375         return( EXIT_FAILURE );
376     }
377
378     if ( infile != NULL ) {
379         if (( fp = fopen( infile, "r" )) == NULL ) {
380             perror( infile );
381             return( EXIT_FAILURE );
382         }
383     } else {
384         fp = stdin;
385     }
386
387         if ( debug ) {
388                 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
389                         fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
390                 }
391                 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
392                         fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
393                 }
394         }
395
396 #ifdef SIGPIPE
397         (void) SIGNAL( SIGPIPE, SIG_IGN );
398 #endif
399
400     if (( ld = ldap_init( ldaphost, ldapport )) == NULL ) {
401         perror( "ldap_init" );
402         return( EXIT_FAILURE );
403     }
404
405         /* referrals */
406         if( ldap_set_option( ld, LDAP_OPT_REFERRALS,
407                 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
408         {
409                 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
410                         referrals ? "on" : "off" );
411                 return EXIT_FAILURE;
412         }
413
414         if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version )
415                 != LDAP_OPT_SUCCESS )
416         {
417                 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
418                         version );
419                 return EXIT_FAILURE;
420         }
421
422         if ( use_tls && ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS ) {
423                 if ( use_tls > 1 ) {
424                         ldap_perror( ld, "ldap_start_tls" );
425                         return( EXIT_FAILURE );
426                 }
427                 fprintf( stderr, "WARNING: could not start TLS\n" );
428         }
429
430         if (want_bindpw) {
431                 passwd.bv_val = getpassphrase("Enter LDAP Password: ");
432                 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
433         }
434
435         if ( authmethod == LDAP_AUTH_SASL ) {
436 #ifdef HAVE_CYRUS_SASL
437                 ldap_set_sasl_interact_proc( ld, lutil_sasl_interact );
438
439                 if( sasl_secprops != NULL ) {
440                         rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
441                                 (void *) sasl_secprops );
442                         
443                         if( rc != LDAP_OPT_SUCCESS ) {
444                                 fprintf( stderr,
445                                         "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
446                                         sasl_secprops );
447                                 return( EXIT_FAILURE );
448                         }
449                 }
450                 
451                 rc = ldap_sasl_interactive_bind_s( ld, binddn,
452                                 sasl_mech, NULL, NULL );
453
454                 if( rc != LDAP_SUCCESS ) {
455                         ldap_perror( ld, "ldap_sasl_interactive_bind_s" );
456                         return( EXIT_FAILURE );
457                 }
458 #else
459                 fprintf( stderr, "%s was not compiled with SASL support\n",
460                         argv[0] );
461                 return( EXIT_FAILURE );
462 #endif
463         }
464         else {
465                 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
466                                 != LDAP_SUCCESS ) {
467                         ldap_perror( ld, "ldap_bind" );
468                         return( EXIT_FAILURE );
469                 }
470         }
471
472         if ( manageDSAit ) {
473                 int err;
474                 LDAPControl c;
475                 LDAPControl *ctrls[2];
476                 ctrls[0] = &c;
477                 ctrls[1] = NULL;
478
479                 c.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
480                 c.ldctl_value.bv_val = NULL;
481                 c.ldctl_value.bv_len = 0;
482                 c.ldctl_iscritical = manageDSAit > 1;
483
484                 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, &ctrls );
485
486                 if( err != LDAP_OPT_SUCCESS ) {
487                         fprintf( stderr, "Could not set ManageDSAit %scontrol\n",
488                                 c.ldctl_iscritical ? "critical " : "" );
489                         if( c.ldctl_iscritical ) {
490                                 exit( EXIT_FAILURE );
491                         }
492                 }
493         }
494
495     rc = 0;
496     if (havedn)
497         rc = domodrdn( ld, entrydn, rdn, newSuperior, remove );
498     else while ((rc == 0 || contoper) && fgets(buf, sizeof(buf), fp) != NULL) {
499         if ( *buf != '\0' ) {   /* blank lines optional, skip */
500             buf[ strlen( buf ) - 1 ] = '\0';    /* remove nl */
501
502             if ( havedn ) {     /* have DN, get RDN */
503                 if (( rdn = strdup( buf )) == NULL ) {
504                     perror( "strdup" );
505                     return( EXIT_FAILURE );
506                 }
507                 rc = domodrdn(ld, entrydn, rdn, newSuperior, remove );
508                 havedn = 0;
509             } else if ( !havedn ) {     /* don't have DN yet */
510                 if (( entrydn = strdup( buf )) == NULL ) {
511                     perror( "strdup" );
512                     return( EXIT_FAILURE );
513                 }
514                 ++havedn;
515             }
516         }
517     }
518
519     ldap_unbind( ld );
520
521         /* UNREACHABLE */
522         return( rc );
523 }
524
525 static int domodrdn(
526     LDAP        *ld,
527     char        *dn,
528     char        *rdn,
529     char        *newSuperior,
530     int         remove ) /* flag: remove old RDN */
531 {
532         int rc, code, id;
533         char *matcheddn=NULL, *text=NULL, **refs=NULL;
534         LDAPMessage *res;
535
536     if ( verbose ) {
537                 printf( "Renaming \"%s\"\n", dn );
538                 printf( "\tnew rdn=\"%s\" (%s old rdn)\n",
539                         rdn, remove ? "delete" : "keep" );
540                 if( newSuperior != NULL ) {
541                         printf("\tnew parent=\"%s\"\n", newSuperior);
542                 }
543         }
544
545         if( not ) return LDAP_SUCCESS;
546
547         rc = ldap_rename( ld, dn, rdn, newSuperior, remove,
548                 NULL, NULL, &id );
549
550         if ( rc != LDAP_SUCCESS ) {
551                 fprintf( stderr, "ldapmodrdn: ldap_rename: %s (%d)\n",
552                         ldap_err2string( rc ), rc );
553                 return rc;
554         }
555
556         rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, NULL, &res );
557         if ( rc < 0 ) {
558                 ldap_perror( ld, "ldapmodrdn: ldap_result" );
559                 return rc;
560         }
561
562         rc = ldap_parse_result( ld, res, &code, &matcheddn, &text, &refs, NULL, 1 );
563
564         if( rc != LDAP_SUCCESS ) {
565                 fprintf( stderr, "ldapmodrdn: ldap_parse_result: %s (%d)\n",
566                         ldap_err2string( rc ), rc );
567                 return rc;
568         }
569
570         if( verbose || code != LDAP_SUCCESS ||
571                 (matcheddn && *matcheddn) || (text && *text) || (refs && *refs) )
572         {
573                 printf( "Rename Result: %s (%d)\n",
574                         ldap_err2string( code ), code );
575
576                 if( text && *text ) {
577                         printf( "Additional info: %s\n", text );
578                 }
579
580                 if( matcheddn && *matcheddn ) {
581                         printf( "Matched DN: %s\n", matcheddn );
582                 }
583
584                 if( refs ) {
585                         int i;
586                         for( i=0; refs[i]; i++ ) {
587                                 printf("Referral: %s\n", refs[i] );
588                         }
589                 }
590         }
591
592         ber_memfree( text );
593         ber_memfree( matcheddn );
594         ber_memvfree( (void **) refs );
595
596         return code;
597 }