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"
106 "Cd:D:h:H:IkKMnO:p:P:QR:U:vw:WxX:Y:Z" )) != EOF )
109 /* Delete Specific Options */
110 case 'c': /* continuous operation mode */
113 case 'f': /* read DNs from a file */
115 fprintf( stderr, "%s: -f previously specified\n", prog );
118 if (( fp = fopen( optarg, "r" )) == NULL ) {
120 exit( EXIT_FAILURE );
132 debug |= atoi( optarg );
134 case 'D': /* bind DN */
135 if( binddn != NULL ) {
136 fprintf( stderr, "%s: -D previously specified\n", prog );
139 binddn = strdup( optarg );
141 case 'h': /* ldap host */
142 if( ldapuri != NULL ) {
143 fprintf( stderr, "%s: -h incompatible with -H\n", prog );
146 if( ldaphost != NULL ) {
147 fprintf( stderr, "%s: -h previously specified\n", prog );
150 ldaphost = strdup( optarg );
152 case 'H': /* ldap URI */
153 if( ldaphost != NULL ) {
154 fprintf( stderr, "%s: -H incompatible with -h\n", prog );
158 fprintf( stderr, "%s: -H incompatible with -p\n", prog );
161 if( ldapuri != NULL ) {
162 fprintf( stderr, "%s: -H previously specified\n", prog );
165 ldapuri = strdup( optarg );
168 #ifdef HAVE_CYRUS_SASL
169 if( version == LDAP_VERSION2 ) {
170 fprintf( stderr, "%s: -I incompatible with version %d\n",
174 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
175 fprintf( stderr, "%s: incompatible previous "
176 "authentication choice\n",
180 authmethod = LDAP_AUTH_SASL;
181 version = LDAP_VERSION3;
182 sasl_flags = LDAP_SASL_INTERACTIVE;
185 fprintf( stderr, "%s: was not compiled with SASL support\n",
187 return( EXIT_FAILURE );
189 case 'k': /* kerberos bind */
190 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
191 if( version > LDAP_VERSION2 ) {
192 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
197 if( authmethod != -1 ) {
198 fprintf( stderr, "%s: -k incompatible with previous "
199 "authentication choice\n", prog );
203 authmethod = LDAP_AUTH_KRBV4;
205 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
209 case 'K': /* kerberos bind, part one only */
210 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
211 if( version > LDAP_VERSION2 ) {
212 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
216 if( authmethod != -1 ) {
217 fprintf( stderr, "%s: incompatible with previous "
218 "authentication choice\n", prog );
222 authmethod = LDAP_AUTH_KRBV41;
224 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
225 return( EXIT_FAILURE );
229 /* enable Manage DSA IT */
230 if( version == LDAP_VERSION2 ) {
231 fprintf( stderr, "%s: -M incompatible with LDAPv%d\n",
236 version = LDAP_VERSION3;
238 case 'n': /* print deletes, don't actually do them */
242 #ifdef HAVE_CYRUS_SASL
243 if( sasl_secprops != NULL ) {
244 fprintf( stderr, "%s: -O previously specified\n", prog );
247 if( version == LDAP_VERSION2 ) {
248 fprintf( stderr, "%s: -O incompatible with LDAPv%d\n",
252 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
253 fprintf( stderr, "%s: incompatible previous "
254 "authentication choice\n", prog );
257 authmethod = LDAP_AUTH_SASL;
258 version = LDAP_VERSION3;
259 sasl_secprops = strdup( optarg );
261 fprintf( stderr, "%s: not compiled with SASL support\n",
263 return( EXIT_FAILURE );
268 fprintf( stderr, "%s: -p previously specified\n", prog );
271 ldapport = atoi( optarg );
274 switch( atoi(optarg) ) {
276 if( version == LDAP_VERSION3 ) {
277 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
281 version = LDAP_VERSION2;
284 if( version == LDAP_VERSION2 ) {
285 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
289 version = LDAP_VERSION3;
292 fprintf( stderr, "%s: protocol version should be 2 or 3\n",
295 return( EXIT_FAILURE );
298 #ifdef HAVE_CYRUS_SASL
299 if( version == LDAP_VERSION2 ) {
300 fprintf( stderr, "%s: -Q incompatible with version %d\n",
304 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
305 fprintf( stderr, "%s: incompatible previous "
306 "authentication choice\n",
310 authmethod = LDAP_AUTH_SASL;
311 version = LDAP_VERSION3;
312 sasl_flags = LDAP_SASL_QUIET;
315 fprintf( stderr, "%s: not compiled with SASL support\n",
317 return( EXIT_FAILURE );
320 #ifdef HAVE_CYRUS_SASL
321 if( sasl_realm != NULL ) {
322 fprintf( stderr, "%s: -R previously specified\n", prog );
325 if( version == LDAP_VERSION2 ) {
326 fprintf( stderr, "%s: -R incompatible with version %d\n",
330 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
331 fprintf( stderr, "%s: incompatible previous "
332 "authentication choice\n",
336 authmethod = LDAP_AUTH_SASL;
337 version = LDAP_VERSION3;
338 sasl_realm = strdup( optarg );
340 fprintf( stderr, "%s: not compiled with SASL support\n",
342 return( EXIT_FAILURE );
346 #ifdef HAVE_CYRUS_SASL
347 if( sasl_authc_id != NULL ) {
348 fprintf( stderr, "%s: -U previously specified\n", prog );
351 if( version == LDAP_VERSION2 ) {
352 fprintf( stderr, "%s: -U incompatible with version %d\n",
356 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
357 fprintf( stderr, "%s: incompatible previous "
358 "authentication choice\n",
362 authmethod = LDAP_AUTH_SASL;
363 version = LDAP_VERSION3;
364 sasl_authc_id = strdup( optarg );
366 fprintf( stderr, "%s: not compiled with SASL support\n",
368 return( EXIT_FAILURE );
371 case 'v': /* verbose mode */
374 case 'w': /* password */
375 passwd.bv_val = strdup( optarg );
379 for( p = optarg; *p != '\0'; p++ ) {
383 passwd.bv_len = strlen( passwd.bv_val );
389 #ifdef HAVE_CYRUS_SASL
390 if( sasl_mech != NULL ) {
391 fprintf( stderr, "%s: -Y previously specified\n", prog );
394 if( version == LDAP_VERSION2 ) {
395 fprintf( stderr, "%s: -Y incompatible with version %d\n",
399 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
400 fprintf( stderr, "%s: incompatible with authentication choice\n", prog );
403 authmethod = LDAP_AUTH_SASL;
404 version = LDAP_VERSION3;
405 sasl_mech = strdup( optarg );
407 fprintf( stderr, "%s: not compiled with SASL support\n",
409 return( EXIT_FAILURE );
413 if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
414 fprintf( stderr, "%s: incompatible with previous "
415 "authentication choice\n", prog );
418 authmethod = LDAP_AUTH_SIMPLE;
421 #ifdef HAVE_CYRUS_SASL
422 if( sasl_authz_id != NULL ) {
423 fprintf( stderr, "%s: -X previously specified\n", prog );
426 if( version == LDAP_VERSION2 ) {
427 fprintf( stderr, "%s: -X incompatible with LDAPv%d\n",
431 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
432 fprintf( stderr, "%s: -X incompatible with "
433 "authentication choice\n", prog );
436 authmethod = LDAP_AUTH_SASL;
437 version = LDAP_VERSION3;
438 sasl_authz_id = strdup( optarg );
440 fprintf( stderr, "%s: not compiled with SASL support\n",
442 return( EXIT_FAILURE );
447 if( version == LDAP_VERSION2 ) {
448 fprintf( stderr, "%s: -Z incompatible with version %d\n",
452 version = LDAP_VERSION3;
455 fprintf( stderr, "%s: not compiled with TLS support\n",
457 return( EXIT_FAILURE );
461 fprintf( stderr, "%s: unrecognized option -%c\n",
464 return( EXIT_FAILURE );
469 version = LDAP_VERSION3;
471 if (authmethod == -1 && version > LDAP_VERSION2) {
472 #ifdef HAVE_CYRUS_SASL
473 authmethod = LDAP_AUTH_SASL;
475 authmethod = LDAP_AUTH_SIMPLE;
480 if ( optind >= argc ) {
486 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
487 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
489 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
490 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
495 (void) SIGNAL( SIGPIPE, SIG_IGN );
498 if( ( ldaphost != NULL || ldapport ) && ( ldapuri == NULL ) ) {
500 fprintf( stderr, "ldap_init( %s, %d )\n",
501 ldaphost != NULL ? ldaphost : "<DEFAULT>",
505 ld = ldap_init( ldaphost, ldapport );
507 perror("ldapsearch: ldap_init");
513 fprintf( stderr, "ldap_initialize( %s )\n",
514 ldapuri != NULL ? ldapuri : "<DEFAULT>" );
517 rc = ldap_initialize( &ld, ldapuri );
518 if( rc != LDAP_SUCCESS ) {
519 fprintf( stderr, "Could not create LDAP session handle (%d): %s\n",
520 rc, ldap_err2string(rc) );
526 /* this seems prudent for searches below */
527 int deref = LDAP_DEREF_NEVER;
528 ldap_set_option( ld, LDAP_OPT_DEREF, &deref );
531 /* chase referrals */
532 if( ldap_set_option( ld, LDAP_OPT_REFERRALS,
533 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
535 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
536 referrals ? "on" : "off" );
540 if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version )
541 != LDAP_OPT_SUCCESS )
543 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
548 if ( use_tls && ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS ) {
549 ldap_perror( ld, "ldap_start_tls" );
556 passwd.bv_val = getpassphrase("Enter LDAP Password: ");
557 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
560 if ( authmethod == LDAP_AUTH_SASL ) {
561 #ifdef HAVE_CYRUS_SASL
564 if( sasl_secprops != NULL ) {
565 rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
566 (void *) sasl_secprops );
568 if( rc != LDAP_OPT_SUCCESS ) {
570 "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
572 return( EXIT_FAILURE );
576 defaults = lutil_sasl_defaults( ld,
583 rc = ldap_sasl_interactive_bind_s( ld, binddn,
584 sasl_mech, NULL, NULL,
585 sasl_flags, lutil_sasl_interact, defaults );
587 if( rc != LDAP_SUCCESS ) {
588 ldap_perror( ld, "ldap_sasl_interactive_bind_s" );
589 return( EXIT_FAILURE );
592 fprintf( stderr, "%s: not compiled with SASL support\n",
594 return( EXIT_FAILURE );
598 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
600 ldap_perror( ld, "ldap_bind" );
601 return( EXIT_FAILURE );
608 LDAPControl *ctrls[2];
612 c.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
613 c.ldctl_value.bv_val = NULL;
614 c.ldctl_value.bv_len = 0;
615 c.ldctl_iscritical = manageDSAit > 1;
617 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, ctrls );
619 if( err != LDAP_OPT_SUCCESS ) {
620 fprintf( stderr, "Could not set ManageDSAit %scontrol\n",
621 c.ldctl_iscritical ? "critical " : "" );
622 if( c.ldctl_iscritical ) {
623 exit( EXIT_FAILURE );
630 for ( ; optind < argc; ++optind ) {
631 rc = dodelete( ld, argv[ optind ] );
634 while ((rc == 0 || contoper) && fgets(buf, sizeof(buf), fp) != NULL) {
635 buf[ strlen( buf ) - 1 ] = '\0'; /* remove trailing newline */
636 if ( *buf != '\0' ) {
637 rc = dodelete( ld, buf );
654 char *matcheddn = NULL, *text = NULL, **refs = NULL;
658 printf( "%sdeleting entry \"%s\"\n",
659 (not ? "!" : ""), dn );
666 /* If prune is on, remove a whole subtree. Delete the children of the
667 * DN recursively, then the DN requested.
669 if ( prune ) deletechildren( ld, dn );
671 rc = ldap_delete_ext( ld, dn, NULL, NULL, &id );
672 if ( rc != LDAP_SUCCESS ) {
673 fprintf( stderr, "%s: ldap_delete_ext: %s (%d)\n",
674 prog, ldap_err2string( rc ), rc );
678 rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, NULL, &res );
680 ldap_perror( ld, "ldapdelete: ldap_result" );
684 rc = ldap_parse_result( ld, res, &code, &matcheddn, &text, &refs, NULL, 1 );
686 if( rc != LDAP_SUCCESS ) {
687 fprintf( stderr, "%s: ldap_parse_result: %s (%d)\n",
688 prog, ldap_err2string( rc ), rc );
692 if( verbose || code != LDAP_SUCCESS ||
693 (matcheddn && *matcheddn) || (text && *text) || (refs && *refs) )
695 printf( "Delete Result: %s (%d)\n", ldap_err2string( code ), code );
697 if( text && *text ) {
698 printf( "Additional info: %s\n", text );
701 if( matcheddn && *matcheddn ) {
702 printf( "Matched DN: %s\n", matcheddn );
707 for( i=0; refs[i]; i++ ) {
708 printf("Referral: %s\n", refs[i] );
714 ber_memfree( matcheddn );
715 ber_memvfree( (void **) refs );
721 * Delete all the children of an entry recursively until leaf nodes are reached.
724 static int deletechildren(
728 LDAPMessage *res, *e;
731 static char *attrs[] = { "1.1", NULL };
733 if ( verbose ) printf ( "deleting children of: %s\n", dn );
735 * Do a one level search at dn for children. For each, delete its children.
738 rc = ldap_search_ext_s( ld, dn, LDAP_SCOPE_ONELEVEL, NULL, attrs, 1,
739 NULL, NULL, NULL, -1, &res );
740 if ( rc != LDAP_SUCCESS ) {
741 ldap_perror( ld, "ldap_search" );
745 entries = ldap_count_entries( ld, res );
750 for (e = ldap_first_entry( ld, res ), i = 0; e != NULL;
751 e = ldap_next_entry( ld, e ), i++ )
753 char *dn = ldap_get_dn( ld, e );
756 ldap_perror( ld, "ldap_prune" );
757 ldap_get_option( ld, LDAP_OPT_ERROR_NUMBER, &rc );
762 rc = deletechildren( ld, dn );
764 ldap_perror( ld, "ldap_prune" );
770 printf( "\tremoving %s\n", dn );
773 rc = ldap_delete_s( ld, dn );
775 ldap_perror( ld, "ldap_delete" );
782 printf( "\t%s removed\n", dn );