1 /* ldapdelete.c - simple program to delete an entry using LDAP */
4 * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
12 #include <ac/stdlib.h>
15 #include <ac/signal.h>
16 #include <ac/string.h>
17 #include <ac/unistd.h>
20 #include "lutil_ldap.h"
21 #include "ldap_defaults.h"
24 static char *binddn = NULL;
25 static struct berval passwd = { 0, NULL };
26 static char *ldapuri = NULL;
27 static char *ldaphost = NULL;
28 static int ldapport = 0;
30 #ifdef HAVE_CYRUS_SASL
31 static unsigned sasl_flags = LDAP_SASL_AUTOMATIC;
32 static char *sasl_mech = NULL;
33 static char *sasl_realm = NULL;
34 static char *sasl_authc_id = NULL;
35 static char *sasl_authz_id = NULL;
36 static char *sasl_secprops = NULL;
38 static int use_tls = 0;
39 static int not, verbose, contoper;
40 static LDAP *ld = NULL;
42 static int dodelete LDAP_P((
46 static int deletechildren LDAP_P((
51 usage( const char *s )
54 "Delete entries from an LDAP server\n\n"
55 "usage: %s [options] [dn]...\n"
56 " dn: list of DNs to delete. If not given, it will be readed from stdin\n"
57 " or from the file specified with \"-f file\".\n"
59 " -r delete recursively\n"
62 " -d level set LDAP debugging level to `level'\n"
63 " -D binddn bind DN\n"
64 " -f file read operations from `file'\n"
65 " -h host LDAP server\n"
66 " -H URI LDAP Uniform Resource Indentifier(s)\n"
67 " -I use SASL Interactive mode\n"
68 " -k use Kerberos authentication\n"
69 " -K like -k, but do only step 1 of the Kerberos bind\n"
70 " -M enable Manage DSA IT control (-MM to make critical)\n"
71 " -n show what would be done but don't actually search\n"
72 " -O props SASL security properties\n"
73 " -p port port on LDAP server\n"
74 " -P version procotol version (default: 3)\n"
75 " -Q use SASL Quiet mode\n"
76 " -R realm SASL realm\n"
77 " -U user SASL authentication identity (username)\n"
78 " -v run in verbose mode (diagnostics to standard output)\n"
79 " -w passwd bind passwd (for simple authentication)\n"
80 " -W prompt for bind passwd\n"
81 " -x Simple authentication\n"
82 " -X id SASL authorization identity (\"dn:<dn>\" or \"u:<user>\")\n"
83 " -Y mech SASL mechanism\n"
84 " -Z Start TLS request (-ZZ to require successful response)\n"
92 main( int argc, char **argv )
96 int i, rc, authmethod, referrals, want_bindpw, version, debug, manageDSAit;
98 not = verbose = contoper = want_bindpw = debug = manageDSAit = referrals = 0;
103 prog = (prog = strrchr(argv[0], *LDAP_DIRSEP)) == NULL ? argv[0] : prog + 1;
105 while (( i = getopt( argc, argv, "cf:r" "Cd:D:h:H:IkKMnO:p:P:QRU:vw:WxX:Y:Z" )) != EOF ) {
107 /* Delete Specific Options */
108 case 'c': /* continuous operation mode */
111 case 'f': /* read DNs from a file */
113 fprintf( stderr, "%s: -f previously specified\n" );
116 if (( fp = fopen( optarg, "r" )) == NULL ) {
118 exit( EXIT_FAILURE );
130 debug |= atoi( optarg );
132 case 'D': /* bind DN */
133 if( binddn != NULL ) {
134 fprintf( stderr, "%s: -D previously specified\n" );
137 binddn = strdup( optarg );
139 case 'h': /* ldap host */
140 if( ldapuri != NULL ) {
141 fprintf( stderr, "%s: -h incompatible with -H\n" );
144 if( ldaphost != NULL ) {
145 fprintf( stderr, "%s: -h previously specified\n" );
148 ldaphost = strdup( optarg );
150 case 'H': /* ldap URI */
151 if( ldaphost != NULL ) {
152 fprintf( stderr, "%s: -H incompatible with -h\n" );
156 fprintf( stderr, "%s: -H incompatible with -p\n" );
159 if( ldapuri != NULL ) {
160 fprintf( stderr, "%s: -H previously specified\n" );
163 ldapuri = strdup( optarg );
166 #ifdef HAVE_CYRUS_SASL
167 if( version == LDAP_VERSION2 ) {
168 fprintf( stderr, "%s: -I incompatible with version %d\n",
172 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
173 fprintf( stderr, "%s: incompatible previous "
174 "authentication choice\n",
178 authmethod = LDAP_AUTH_SASL;
179 version = LDAP_VERSION3;
180 sasl_flags = LDAP_SASL_INTERACTIVE;
183 fprintf( stderr, "%s: was not compiled with SASL support\n",
185 return( EXIT_FAILURE );
187 case 'k': /* kerberos bind */
188 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
189 if( version > LDAP_VERSION2 ) {
190 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
195 if( authmethod != -1 ) {
196 fprintf( stderr, "%s: -k incompatible with previous "
197 "authentication choice\n", prog );
201 authmethod = LDAP_AUTH_KRBV4;
203 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
207 case 'K': /* kerberos bind, part one only */
208 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
209 if( version > LDAP_VERSION2 ) {
210 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
214 if( authmethod != -1 ) {
215 fprintf( stderr, "%s: incompatible with previous "
216 "authentication choice\n", prog );
220 authmethod = LDAP_AUTH_KRBV41;
222 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
223 return( EXIT_FAILURE );
227 /* enable Manage DSA IT */
228 if( version == LDAP_VERSION2 ) {
229 fprintf( stderr, "%s: -M incompatible with LDAPv%d\n",
234 version = LDAP_VERSION3;
236 case 'n': /* print deletes, don't actually do them */
240 #ifdef HAVE_CYRUS_SASL
241 if( sasl_secprops != NULL ) {
242 fprintf( stderr, "%s: -O previously specified\n" );
245 if( version == LDAP_VERSION2 ) {
246 fprintf( stderr, "%s: -O incompatible with LDAPv%d\n",
250 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
251 fprintf( stderr, "%s: incompatible previous "
252 "authentication choice\n", prog );
255 authmethod = LDAP_AUTH_SASL;
256 version = LDAP_VERSION3;
257 sasl_secprops = strdup( optarg );
259 fprintf( stderr, "%s: not compiled with SASL support\n",
261 return( EXIT_FAILURE );
266 fprintf( stderr, "%s: -p previously specified\n" );
269 ldapport = atoi( optarg );
272 switch( atoi(optarg) ) {
274 if( version == LDAP_VERSION3 ) {
275 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
279 version = LDAP_VERSION2;
282 if( version == LDAP_VERSION2 ) {
283 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
287 version = LDAP_VERSION3;
290 fprintf( stderr, "%s: protocol version should be 2 or 3\n",
293 return( EXIT_FAILURE );
296 #ifdef HAVE_CYRUS_SASL
297 if( version == LDAP_VERSION2 ) {
298 fprintf( stderr, "%s: -Q incompatible with version %d\n",
302 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
303 fprintf( stderr, "%s: incompatible previous "
304 "authentication choice\n",
308 authmethod = LDAP_AUTH_SASL;
309 version = LDAP_VERSION3;
310 sasl_flags = LDAP_SASL_QUIET;
313 fprintf( stderr, "%s: not compiled with SASL support\n",
315 return( EXIT_FAILURE );
318 #ifdef HAVE_CYRUS_SASL
319 if( sasl_realm != NULL ) {
320 fprintf( stderr, "%s: -R previously specified\n" );
323 if( version == LDAP_VERSION2 ) {
324 fprintf( stderr, "%s: -R incompatible with version %d\n",
328 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
329 fprintf( stderr, "%s: incompatible previous "
330 "authentication choice\n",
334 authmethod = LDAP_AUTH_SASL;
335 version = LDAP_VERSION3;
336 sasl_realm = strdup( optarg );
338 fprintf( stderr, "%s: not compiled with SASL support\n",
340 return( EXIT_FAILURE );
344 #ifdef HAVE_CYRUS_SASL
345 if( sasl_authc_id != NULL ) {
346 fprintf( stderr, "%s: -U previously specified\n" );
349 if( version == LDAP_VERSION2 ) {
350 fprintf( stderr, "%s: -U incompatible with version %d\n",
354 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
355 fprintf( stderr, "%s: incompatible previous "
356 "authentication choice\n",
360 authmethod = LDAP_AUTH_SASL;
361 version = LDAP_VERSION3;
362 sasl_authc_id = strdup( optarg );
364 fprintf( stderr, "%s: not compiled with SASL support\n",
366 return( EXIT_FAILURE );
369 case 'v': /* verbose mode */
372 case 'w': /* password */
373 passwd.bv_val = strdup( optarg );
377 for( p = optarg; *p == '\0'; p++ ) {
381 passwd.bv_len = strlen( passwd.bv_val );
387 #ifdef HAVE_CYRUS_SASL
388 if( sasl_mech != NULL ) {
389 fprintf( stderr, "%s: -Y previously specified\n" );
392 if( version == LDAP_VERSION2 ) {
393 fprintf( stderr, "%s: -Y incompatible with version %d\n",
397 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
398 fprintf( stderr, "%s: incompatible with authentication choice\n", prog );
401 authmethod = LDAP_AUTH_SASL;
402 version = LDAP_VERSION3;
403 sasl_mech = strdup( optarg );
405 fprintf( stderr, "%s: not compiled with SASL support\n",
407 return( EXIT_FAILURE );
411 if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
412 fprintf( stderr, "%s: incompatible with previous "
413 "authentication choice\n", prog );
416 authmethod = LDAP_AUTH_SIMPLE;
419 #ifdef HAVE_CYRUS_SASL
420 if( sasl_authz_id != NULL ) {
421 fprintf( stderr, "%s: -X previously specified\n" );
424 if( version == LDAP_VERSION2 ) {
425 fprintf( stderr, "%s: -X incompatible with LDAPv%d\n",
429 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
430 fprintf( stderr, "%s: -X incompatible with "
431 "authentication choice\n", prog );
434 authmethod = LDAP_AUTH_SASL;
435 version = LDAP_VERSION3;
436 sasl_authz_id = strdup( optarg );
438 fprintf( stderr, "%s: not compiled with SASL support\n",
440 return( EXIT_FAILURE );
445 if( version == LDAP_VERSION2 ) {
446 fprintf( stderr, "%s: -Z incompatible with version %d\n",
450 version = LDAP_VERSION3;
453 fprintf( stderr, "%s: not compiled with TLS support\n",
455 return( EXIT_FAILURE );
459 fprintf( stderr, "%s: unrecongized option -%c\n",
462 return( EXIT_FAILURE );
467 version = LDAP_VERSION3;
469 if (authmethod == -1 && version > LDAP_VERSION2) {
470 #ifdef HAVE_CYRUS_SASL
471 authmethod = LDAP_AUTH_SASL;
473 authmethod = LDAP_AUTH_SIMPLE;
478 if ( optind >= argc ) {
484 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
485 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
487 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
488 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
493 (void) SIGNAL( SIGPIPE, SIG_IGN );
496 if( ( ldaphost != NULL || ldapport ) && ( ldapuri == NULL ) ) {
498 fprintf( stderr, "ldap_init( %s, %d )\n",
499 ldaphost != NULL ? ldaphost : "<DEFAULT>",
502 ld = ldap_init( ldaphost, ldapport );
506 fprintf( stderr, "ldap_initialize( %s )\n",
507 ldapuri != NULL ? ldapuri : "<DEFAULT>" );
509 (void) ldap_initialize( &ld, ldapuri );
513 fprintf( stderr, "Could not create LDAP session handle (%d): %s\n",
514 rc, ldap_err2string(rc) );
519 /* this seems prudent for searches below */
520 int deref = LDAP_DEREF_NEVER;
521 ldap_set_option( ld, LDAP_OPT_DEREF, &deref );
524 /* chase referrals */
525 if( ldap_set_option( ld, LDAP_OPT_REFERRALS,
526 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
528 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
529 referrals ? "on" : "off" );
533 if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version )
534 != LDAP_OPT_SUCCESS )
536 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
541 if ( use_tls && ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS ) {
543 ldap_perror( ld, "ldap_start_tls" );
546 fprintf( stderr, "WARNING: could not start TLS\n" );
550 passwd.bv_val = getpassphrase("Enter LDAP Password: ");
551 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
554 if ( authmethod == LDAP_AUTH_SASL ) {
555 #ifdef HAVE_CYRUS_SASL
558 if( sasl_secprops != NULL ) {
559 rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
560 (void *) sasl_secprops );
562 if( rc != LDAP_OPT_SUCCESS ) {
564 "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
566 return( EXIT_FAILURE );
570 defaults = lutil_sasl_defaults( ld,
577 rc = ldap_sasl_interactive_bind_s( ld, binddn,
578 sasl_mech, NULL, NULL,
579 sasl_flags, lutil_sasl_interact, defaults );
581 if( rc != LDAP_SUCCESS ) {
582 ldap_perror( ld, "ldap_sasl_interactive_bind_s" );
583 return( EXIT_FAILURE );
586 fprintf( stderr, "%s: not compiled with SASL support\n",
588 return( EXIT_FAILURE );
592 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
594 ldap_perror( ld, "ldap_bind" );
595 return( EXIT_FAILURE );
602 LDAPControl *ctrls[2];
606 c.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
607 c.ldctl_value.bv_val = NULL;
608 c.ldctl_value.bv_len = 0;
609 c.ldctl_iscritical = manageDSAit > 1;
611 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, ctrls );
613 if( err != LDAP_OPT_SUCCESS ) {
614 fprintf( stderr, "Could not set ManageDSAit %scontrol\n",
615 c.ldctl_iscritical ? "critical " : "" );
616 if( c.ldctl_iscritical ) {
617 exit( EXIT_FAILURE );
624 for ( ; optind < argc; ++optind ) {
625 rc = dodelete( ld, argv[ optind ] );
628 while ((rc == 0 || contoper) && fgets(buf, sizeof(buf), fp) != NULL) {
629 buf[ strlen( buf ) - 1 ] = '\0'; /* remove trailing newline */
630 if ( *buf != '\0' ) {
631 rc = dodelete( ld, buf );
648 char *matcheddn = NULL, *text = NULL, **refs = NULL;
652 printf( "%sdeleting entry \"%s\"\n",
653 (not ? "!" : ""), dn );
660 /* If prune is on, remove a whole subtree. Delete the children of the
661 * DN recursively, then the DN requested.
663 if ( prune ) deletechildren( ld, dn );
665 rc = ldap_delete_ext( ld, dn, NULL, NULL, &id );
666 if ( rc != LDAP_SUCCESS ) {
667 fprintf( stderr, "%s: ldap_delete_ext: %s (%d)\n",
668 prog, ldap_err2string( rc ), rc );
672 rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, NULL, &res );
674 ldap_perror( ld, "ldapdelete: ldap_result" );
678 rc = ldap_parse_result( ld, res, &code, &matcheddn, &text, &refs, NULL, 1 );
680 if( rc != LDAP_SUCCESS ) {
681 fprintf( stderr, "%s: ldap_parse_result: %s (%d)\n",
682 prog, ldap_err2string( rc ), rc );
686 if( verbose || code != LDAP_SUCCESS ||
687 (matcheddn && *matcheddn) || (text && *text) || (refs && *refs) )
689 printf( "Delete Result: %s (%d)\n", ldap_err2string( code ), code );
691 if( text && *text ) {
692 printf( "Additional info: %s\n", text );
695 if( matcheddn && *matcheddn ) {
696 printf( "Matched DN: %s\n", matcheddn );
701 for( i=0; refs[i]; i++ ) {
702 printf("Referral: %s\n", refs[i] );
708 ber_memfree( matcheddn );
709 ber_memvfree( (void **) refs );
715 * Delete all the children of an entry recursively until leaf nodes are reached.
718 static int deletechildren(
722 LDAPMessage *res, *e;
725 static char *attrs[] = { "1.1", NULL };
727 if ( verbose ) printf ( "deleting children of: %s\n", dn );
729 * Do a one level search at dn for children. For each, delete its children.
732 rc = ldap_search_ext_s( ld, dn, LDAP_SCOPE_ONELEVEL, NULL, attrs, 1,
733 NULL, NULL, NULL, -1, &res );
734 if ( rc != LDAP_SUCCESS ) {
735 ldap_perror( ld, "ldap_search" );
739 entries = ldap_count_entries( ld, res );
744 for (e = ldap_first_entry( ld, res ), i = 0; e != NULL;
745 e = ldap_next_entry( ld, e ), i++ )
747 char *dn = ldap_get_dn( ld, e );
750 ldap_perror( ld, "ldap_prune" );
751 ldap_get_option( ld, LDAP_OPT_ERROR_NUMBER, &rc );
756 rc = deletechildren( ld, dn );
758 ldap_perror( ld, "ldap_prune" );
764 printf( "\tremoving %s\n", dn );
767 rc = ldap_delete_s( ld, dn );
769 ldap_perror( ld, "ldap_delete" );
776 printf( "\t%s removed\n", dn );