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 " -I use SASL Interactive mode\n"
67 " -k use Kerberos authentication\n"
68 " -K like -k, but do only step 1 of the Kerberos bind\n"
69 " -M enable Manage DSA IT control (-MM to make critical)\n"
70 " -n show what would be done but don't actually search\n"
71 " -O props SASL security properties\n"
72 " -p port port on LDAP server\n"
73 " -P version procotol version (default: 3)\n"
74 " -Q use SASL Quiet mode\n"
75 " -R realm SASL realm\n"
76 " -U user SASL authentication identity (username)\n"
77 " -v run in verbose mode (diagnostics to standard output)\n"
78 " -w passwd bind passwd (for simple authentication)\n"
79 " -W prompt for bind passwd\n"
80 " -x Simple authentication\n"
81 " -X id SASL authorization identity (\"dn:<dn>\" or \"u:<user>\")\n"
82 " -Y mech SASL mechanism\n"
83 " -Z Start TLS request (-ZZ to require successful response)\n"
91 main( int argc, char **argv )
95 int i, rc, authmethod, referrals, want_bindpw, version, debug, manageDSAit;
97 not = verbose = contoper = want_bindpw = debug = manageDSAit = referrals = 0;
102 prog = (prog = strrchr(argv[0], *LDAP_DIRSEP)) == NULL ? argv[0] : prog + 1;
104 while (( i = getopt( argc, argv, "cf:r" "Cd:D:h:H:IkKMnO:p:P:QRU:vw:WxX:Y:Z" )) != EOF ) {
106 /* Delete Specific Options */
107 case 'c': /* continuous operation mode */
110 case 'f': /* read DNs from a file */
112 fprintf( stderr, "%s: -f previously specified\n" );
115 if (( fp = fopen( optarg, "r" )) == NULL ) {
117 exit( EXIT_FAILURE );
129 debug |= atoi( optarg );
131 case 'D': /* bind DN */
132 if( binddn != NULL ) {
133 fprintf( stderr, "%s: -D previously specified\n" );
136 binddn = strdup( optarg );
138 case 'h': /* ldap host */
139 if( ldapuri != NULL ) {
140 fprintf( stderr, "%s: -h incompatible with -H\n" );
143 if( ldaphost != NULL ) {
144 fprintf( stderr, "%s: -h previously specified\n" );
147 ldaphost = strdup( optarg );
149 case 'H': /* ldap URI */
150 if( ldaphost != NULL ) {
151 fprintf( stderr, "%s: -H incompatible with -h\n" );
155 fprintf( stderr, "%s: -H incompatible with -p\n" );
158 if( ldapuri != NULL ) {
159 fprintf( stderr, "%s: -H previously specified\n" );
162 ldapuri = strdup( optarg );
165 #ifdef HAVE_CYRUS_SASL
166 if( version == LDAP_VERSION2 ) {
167 fprintf( stderr, "%s: -I incompatible with version %d\n",
171 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
172 fprintf( stderr, "%s: incompatible previous "
173 "authentication choice\n",
177 authmethod = LDAP_AUTH_SASL;
178 version = LDAP_VERSION3;
179 sasl_flags = LDAP_SASL_INTERACTIVE;
182 fprintf( stderr, "%s: was not compiled with SASL support\n",
184 return( EXIT_FAILURE );
186 case 'k': /* kerberos bind */
187 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
188 if( version > LDAP_VERSION2 ) {
189 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
194 if( authmethod != -1 ) {
195 fprintf( stderr, "%s: -k incompatible with previous "
196 "authentication choice\n", prog );
200 authmethod = LDAP_AUTH_KRBV4;
202 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
206 case 'K': /* kerberos bind, part one only */
207 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
208 if( version > LDAP_VERSION2 ) {
209 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
213 if( authmethod != -1 ) {
214 fprintf( stderr, "%s: incompatible with previous "
215 "authentication choice\n", prog );
219 authmethod = LDAP_AUTH_KRBV41;
221 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
222 return( EXIT_FAILURE );
226 /* enable Manage DSA IT */
227 if( version == LDAP_VERSION2 ) {
228 fprintf( stderr, "%s: -M incompatible with LDAPv%d\n",
233 version = LDAP_VERSION3;
235 case 'n': /* print deletes, don't actually do them */
239 #ifdef HAVE_CYRUS_SASL
240 if( sasl_secprops != NULL ) {
241 fprintf( stderr, "%s: -O previously specified\n" );
244 if( version == LDAP_VERSION2 ) {
245 fprintf( stderr, "%s: -O incompatible with LDAPv%d\n",
249 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
250 fprintf( stderr, "%s: incompatible previous "
251 "authentication choice\n", prog );
254 authmethod = LDAP_AUTH_SASL;
255 version = LDAP_VERSION3;
256 sasl_secprops = strdup( optarg );
258 fprintf( stderr, "%s: not compiled with SASL support\n",
260 return( EXIT_FAILURE );
265 fprintf( stderr, "%s: -p previously specified\n" );
268 ldapport = atoi( optarg );
271 switch( atoi(optarg) ) {
273 if( version == LDAP_VERSION3 ) {
274 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
278 version = LDAP_VERSION2;
281 if( version == LDAP_VERSION2 ) {
282 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
286 version = LDAP_VERSION3;
289 fprintf( stderr, "%s: protocol version should be 2 or 3\n",
292 return( EXIT_FAILURE );
295 #ifdef HAVE_CYRUS_SASL
296 if( version == LDAP_VERSION2 ) {
297 fprintf( stderr, "%s: -Q incompatible with version %d\n",
301 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
302 fprintf( stderr, "%s: incompatible previous "
303 "authentication choice\n",
307 authmethod = LDAP_AUTH_SASL;
308 version = LDAP_VERSION3;
309 sasl_flags = LDAP_SASL_QUIET;
312 fprintf( stderr, "%s: not compiled with SASL support\n",
314 return( EXIT_FAILURE );
317 #ifdef HAVE_CYRUS_SASL
318 if( sasl_realm != NULL ) {
319 fprintf( stderr, "%s: -R previously specified\n" );
322 if( version == LDAP_VERSION2 ) {
323 fprintf( stderr, "%s: -R incompatible with version %d\n",
327 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
328 fprintf( stderr, "%s: incompatible previous "
329 "authentication choice\n",
333 authmethod = LDAP_AUTH_SASL;
334 version = LDAP_VERSION3;
335 sasl_realm = strdup( optarg );
337 fprintf( stderr, "%s: not compiled with SASL support\n",
339 return( EXIT_FAILURE );
343 #ifdef HAVE_CYRUS_SASL
344 if( sasl_authc_id != NULL ) {
345 fprintf( stderr, "%s: -U previously specified\n" );
348 if( version == LDAP_VERSION2 ) {
349 fprintf( stderr, "%s: -U incompatible with version %d\n",
353 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
354 fprintf( stderr, "%s: incompatible previous "
355 "authentication choice\n",
359 authmethod = LDAP_AUTH_SASL;
360 version = LDAP_VERSION3;
361 sasl_authc_id = strdup( optarg );
363 fprintf( stderr, "%s: not compiled with SASL support\n",
365 return( EXIT_FAILURE );
368 case 'v': /* verbose mode */
371 case 'w': /* password */
372 passwd.bv_val = strdup( optarg );
376 for( p = optarg; *p == '\0'; p++ ) {
380 passwd.bv_len = strlen( passwd.bv_val );
386 #ifdef HAVE_CYRUS_SASL
387 if( sasl_mech != NULL ) {
388 fprintf( stderr, "%s: -Y previously specified\n" );
391 if( version == LDAP_VERSION2 ) {
392 fprintf( stderr, "%s: -Y incompatible with version %d\n",
396 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
397 fprintf( stderr, "%s: incompatible with authentication choice\n", prog );
400 authmethod = LDAP_AUTH_SASL;
401 version = LDAP_VERSION3;
402 sasl_mech = strdup( optarg );
404 fprintf( stderr, "%s: not compiled with SASL support\n",
406 return( EXIT_FAILURE );
410 if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
411 fprintf( stderr, "%s: incompatible with previous "
412 "authentication choice\n", prog );
415 authmethod = LDAP_AUTH_SIMPLE;
418 #ifdef HAVE_CYRUS_SASL
419 if( sasl_authz_id != NULL ) {
420 fprintf( stderr, "%s: -X previously specified\n" );
423 if( version == LDAP_VERSION2 ) {
424 fprintf( stderr, "%s: -X incompatible with LDAPv%d\n",
428 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
429 fprintf( stderr, "%s: -X incompatible with "
430 "authentication choice\n", prog );
433 authmethod = LDAP_AUTH_SASL;
434 version = LDAP_VERSION3;
435 sasl_authz_id = strdup( optarg );
437 fprintf( stderr, "%s: not compiled with SASL support\n",
439 return( EXIT_FAILURE );
444 if( version == LDAP_VERSION2 ) {
445 fprintf( stderr, "%s: -Z incompatible with version %d\n",
449 version = LDAP_VERSION3;
452 fprintf( stderr, "%s: not compiled with TLS support\n",
454 return( EXIT_FAILURE );
458 fprintf( stderr, "%s: unrecongized option -%c\n",
461 return( EXIT_FAILURE );
466 version = LDAP_VERSION3;
468 if (authmethod == -1 && version > LDAP_VERSION2) {
469 #ifdef HAVE_CYRUS_SASL
470 authmethod = LDAP_AUTH_SASL;
472 authmethod = LDAP_AUTH_SIMPLE;
477 if ( optind >= argc ) {
483 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
484 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
486 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
487 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
492 (void) SIGNAL( SIGPIPE, SIG_IGN );
495 if( ( ldaphost != NULL || ldapport ) && ( ldapuri == NULL ) ) {
497 fprintf( stderr, "ldap_init( %s, %d )\n",
498 ldaphost != NULL ? ldaphost : "<DEFAULT>",
501 ld = ldap_init( ldaphost, ldapport );
505 fprintf( stderr, "ldap_initialize( %s )\n",
506 ldapuri != NULL ? ldapuri : "<DEFAULT>" );
508 (void) ldap_initialize( &ld, ldapuri );
512 fprintf( stderr, "Could not create LDAP session handle (%d): %s\n",
513 rc, ldap_err2string(rc) );
518 /* this seems prudent for searches below */
519 int deref = LDAP_DEREF_NEVER;
520 ldap_set_option( ld, LDAP_OPT_DEREF, &deref );
523 /* chase referrals */
524 if( ldap_set_option( ld, LDAP_OPT_REFERRALS,
525 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
527 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
528 referrals ? "on" : "off" );
532 if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version )
533 != LDAP_OPT_SUCCESS )
535 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
540 if ( use_tls && ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS ) {
542 ldap_perror( ld, "ldap_start_tls" );
545 fprintf( stderr, "WARNING: could not start TLS\n" );
549 passwd.bv_val = getpassphrase("Enter LDAP Password: ");
550 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
553 if ( authmethod == LDAP_AUTH_SASL ) {
554 #ifdef HAVE_CYRUS_SASL
557 if( sasl_secprops != NULL ) {
558 rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
559 (void *) sasl_secprops );
561 if( rc != LDAP_OPT_SUCCESS ) {
563 "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
565 return( EXIT_FAILURE );
569 defaults = lutil_sasl_defaults( ld,
576 rc = ldap_sasl_interactive_bind_s( ld, binddn,
577 sasl_mech, NULL, NULL,
578 sasl_flags, lutil_sasl_interact, defaults );
580 if( rc != LDAP_SUCCESS ) {
581 ldap_perror( ld, "ldap_sasl_interactive_bind_s" );
582 return( EXIT_FAILURE );
585 fprintf( stderr, "%s: not compiled with SASL support\n",
587 return( EXIT_FAILURE );
591 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
593 ldap_perror( ld, "ldap_bind" );
594 return( EXIT_FAILURE );
601 LDAPControl *ctrls[2];
605 c.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
606 c.ldctl_value.bv_val = NULL;
607 c.ldctl_value.bv_len = 0;
608 c.ldctl_iscritical = manageDSAit > 1;
610 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, ctrls );
612 if( err != LDAP_OPT_SUCCESS ) {
613 fprintf( stderr, "Could not set ManageDSAit %scontrol\n",
614 c.ldctl_iscritical ? "critical " : "" );
615 if( c.ldctl_iscritical ) {
616 exit( EXIT_FAILURE );
623 for ( ; optind < argc; ++optind ) {
624 rc = dodelete( ld, argv[ optind ] );
627 while ((rc == 0 || contoper) && fgets(buf, sizeof(buf), fp) != NULL) {
628 buf[ strlen( buf ) - 1 ] = '\0'; /* remove trailing newline */
629 if ( *buf != '\0' ) {
630 rc = dodelete( ld, buf );
647 char *matcheddn = NULL, *text = NULL, **refs = NULL;
651 printf( "%sdeleting entry \"%s\"\n",
652 (not ? "!" : ""), dn );
659 /* If prune is on, remove a whole subtree. Delete the children of the
660 * DN recursively, then the DN requested.
662 if ( prune ) deletechildren( ld, dn );
664 rc = ldap_delete_ext( ld, dn, NULL, NULL, &id );
665 if ( rc != LDAP_SUCCESS ) {
666 fprintf( stderr, "%s: ldap_delete_ext: %s (%d)\n",
667 prog, ldap_err2string( rc ), rc );
671 rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, NULL, &res );
673 ldap_perror( ld, "ldapdelete: ldap_result" );
677 rc = ldap_parse_result( ld, res, &code, &matcheddn, &text, &refs, NULL, 1 );
679 if( rc != LDAP_SUCCESS ) {
680 fprintf( stderr, "%s: ldap_parse_result: %s (%d)\n",
681 prog, ldap_err2string( rc ), rc );
685 if( verbose || code != LDAP_SUCCESS ||
686 (matcheddn && *matcheddn) || (text && *text) || (refs && *refs) )
688 printf( "Delete Result: %s (%d)\n", ldap_err2string( code ), code );
690 if( text && *text ) {
691 printf( "Additional info: %s\n", text );
694 if( matcheddn && *matcheddn ) {
695 printf( "Matched DN: %s\n", matcheddn );
700 for( i=0; refs[i]; i++ ) {
701 printf("Referral: %s\n", refs[i] );
707 ber_memfree( matcheddn );
708 ber_memvfree( (void **) refs );
714 * Delete all the children of an entry recursively until leaf nodes are reached.
717 static int deletechildren(
721 LDAPMessage *res, *e;
724 static char *attrs[] = { "1.1", NULL };
726 if ( verbose ) printf ( "deleting children of: %s\n", dn );
728 * Do a one level search at dn for children. For each, delete its children.
731 rc = ldap_search_ext_s( ld, dn, LDAP_SCOPE_ONELEVEL, NULL, attrs, 1,
732 NULL, NULL, NULL, -1, &res );
733 if ( rc != LDAP_SUCCESS ) {
734 ldap_perror( ld, "ldap_search" );
738 entries = ldap_count_entries( ld, res );
743 for (e = ldap_first_entry( ld, res ), i = 0; e != NULL;
744 e = ldap_next_entry( ld, e ), i++ )
746 char *dn = ldap_get_dn( ld, e );
749 ldap_perror( ld, "ldap_prune" );
750 ldap_get_option( ld, LDAP_OPT_ERROR_NUMBER, &rc );
755 rc = deletechildren( ld, dn );
757 ldap_perror( ld, "ldap_prune" );
763 printf( "\tremoving %s\n", dn );
766 rc = ldap_delete_s( ld, dn );
768 ldap_perror( ld, "ldap_delete" );
775 printf( "\t%s removed\n", dn );