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