1 /* ldapdelete.c - simple program to delete an entry using LDAP */
4 * Copyright 1998-2002 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>
21 #include "lutil_ldap.h"
22 #include "ldap_defaults.h"
25 static char *binddn = NULL;
26 static struct berval passwd = { 0, NULL };
27 static char *ldapuri = NULL;
28 static char *ldaphost = NULL;
29 static int ldapport = 0;
31 #ifdef HAVE_CYRUS_SASL
32 static unsigned sasl_flags = LDAP_SASL_AUTOMATIC;
33 static char *sasl_mech = NULL;
34 static char *sasl_realm = NULL;
35 static char *sasl_authc_id = NULL;
36 static char *sasl_authz_id = NULL;
37 static char *sasl_secprops = NULL;
39 static int use_tls = 0;
40 static int not, verbose, contoper;
41 static LDAP *ld = NULL;
43 static int dodelete LDAP_P((
47 static int deletechildren LDAP_P((
52 usage( const char *s )
55 "Delete entries from an LDAP server\n\n"
56 "usage: %s [options] [dn]...\n"
57 " dn: list of DNs to delete. If not given, it will be readed from stdin\n"
58 " or from the file specified with \"-f file\".\n"
60 " -r delete recursively\n"
63 " -d level set LDAP debugging level to `level'\n"
64 " -D binddn bind DN\n"
65 " -f file read operations from `file'\n"
66 " -h host LDAP server\n"
67 " -H URI LDAP Uniform Resource Indentifier(s)\n"
68 " -I use SASL Interactive mode\n"
69 " -k use Kerberos authentication\n"
70 " -K like -k, but do only step 1 of the Kerberos bind\n"
71 " -M enable Manage DSA IT control (-MM to make critical)\n"
72 " -n show what would be done but don't actually do it\n"
73 " -O props SASL security properties\n"
74 " -p port port on LDAP server\n"
75 " -P version procotol version (default: 3)\n"
76 " -Q use SASL Quiet mode\n"
77 " -R realm SASL realm\n"
78 " -U authcid SASL authentication identity\n"
79 " -v run in verbose mode (diagnostics to standard output)\n"
80 " -w passwd bind passwd (for simple authentication)\n"
81 " -W prompt for bind passwd\n"
82 " -x Simple authentication\n"
83 " -X authzid SASL authorization identity (\"dn:<dn>\" or \"u:<user>\")\n"
84 " -Y mech SASL mechanism\n"
85 " -Z Start TLS request (-ZZ to require successful response)\n"
93 main( int argc, char **argv )
97 int i, rc, authmethod, referrals, want_bindpw, version, debug, manageDSAit;
99 not = verbose = contoper = want_bindpw = debug = manageDSAit = referrals = 0;
104 prog = lutil_progname( "ldapdelete", argc, argv );
106 while (( i = getopt( argc, argv, "cf:r"
107 "Cd:D:h:H:IkKMnO:p:P:QR:U:vw:WxX:Y:Z" )) != EOF )
110 /* Delete Specific Options */
111 case 'c': /* continuous operation mode */
114 case 'f': /* read DNs from a file */
116 fprintf( stderr, "%s: -f previously specified\n", prog );
119 if (( fp = fopen( optarg, "r" )) == NULL ) {
121 exit( EXIT_FAILURE );
133 debug |= atoi( optarg );
135 case 'D': /* bind DN */
136 if( binddn != NULL ) {
137 fprintf( stderr, "%s: -D previously specified\n", prog );
140 binddn = strdup( optarg );
142 case 'h': /* ldap host */
143 if( ldapuri != NULL ) {
144 fprintf( stderr, "%s: -h incompatible with -H\n", prog );
147 if( ldaphost != NULL ) {
148 fprintf( stderr, "%s: -h previously specified\n", prog );
151 ldaphost = strdup( optarg );
153 case 'H': /* ldap URI */
154 if( ldaphost != NULL ) {
155 fprintf( stderr, "%s: -H incompatible with -h\n", prog );
159 fprintf( stderr, "%s: -H incompatible with -p\n", prog );
162 if( ldapuri != NULL ) {
163 fprintf( stderr, "%s: -H previously specified\n", prog );
166 ldapuri = strdup( optarg );
169 #ifdef HAVE_CYRUS_SASL
170 if( version == LDAP_VERSION2 ) {
171 fprintf( stderr, "%s: -I incompatible with version %d\n",
175 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
176 fprintf( stderr, "%s: incompatible previous "
177 "authentication choice\n",
181 authmethod = LDAP_AUTH_SASL;
182 version = LDAP_VERSION3;
183 sasl_flags = LDAP_SASL_INTERACTIVE;
186 fprintf( stderr, "%s: was not compiled with SASL support\n",
188 return( EXIT_FAILURE );
190 case 'k': /* kerberos bind */
191 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
192 if( version > LDAP_VERSION2 ) {
193 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
198 if( authmethod != -1 ) {
199 fprintf( stderr, "%s: -k incompatible with previous "
200 "authentication choice\n", prog );
204 authmethod = LDAP_AUTH_KRBV4;
206 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
210 case 'K': /* kerberos bind, part one only */
211 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
212 if( version > LDAP_VERSION2 ) {
213 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
217 if( authmethod != -1 ) {
218 fprintf( stderr, "%s: incompatible with previous "
219 "authentication choice\n", prog );
223 authmethod = LDAP_AUTH_KRBV41;
225 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
226 return( EXIT_FAILURE );
230 /* enable Manage DSA IT */
231 if( version == LDAP_VERSION2 ) {
232 fprintf( stderr, "%s: -M incompatible with LDAPv%d\n",
237 version = LDAP_VERSION3;
239 case 'n': /* print deletes, don't actually do them */
243 #ifdef HAVE_CYRUS_SASL
244 if( sasl_secprops != NULL ) {
245 fprintf( stderr, "%s: -O previously specified\n", prog );
248 if( version == LDAP_VERSION2 ) {
249 fprintf( stderr, "%s: -O incompatible with LDAPv%d\n",
253 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
254 fprintf( stderr, "%s: incompatible previous "
255 "authentication choice\n", prog );
258 authmethod = LDAP_AUTH_SASL;
259 version = LDAP_VERSION3;
260 sasl_secprops = strdup( optarg );
262 fprintf( stderr, "%s: not compiled with SASL support\n",
264 return( EXIT_FAILURE );
269 fprintf( stderr, "%s: -p previously specified\n", prog );
272 ldapport = atoi( optarg );
275 switch( atoi(optarg) ) {
277 if( version == LDAP_VERSION3 ) {
278 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
282 version = LDAP_VERSION2;
285 if( version == LDAP_VERSION2 ) {
286 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
290 version = LDAP_VERSION3;
293 fprintf( stderr, "%s: protocol version should be 2 or 3\n",
296 return( EXIT_FAILURE );
299 #ifdef HAVE_CYRUS_SASL
300 if( version == LDAP_VERSION2 ) {
301 fprintf( stderr, "%s: -Q incompatible with version %d\n",
305 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
306 fprintf( stderr, "%s: incompatible previous "
307 "authentication choice\n",
311 authmethod = LDAP_AUTH_SASL;
312 version = LDAP_VERSION3;
313 sasl_flags = LDAP_SASL_QUIET;
316 fprintf( stderr, "%s: not compiled with SASL support\n",
318 return( EXIT_FAILURE );
321 #ifdef HAVE_CYRUS_SASL
322 if( sasl_realm != NULL ) {
323 fprintf( stderr, "%s: -R previously specified\n", prog );
326 if( version == LDAP_VERSION2 ) {
327 fprintf( stderr, "%s: -R incompatible with version %d\n",
331 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
332 fprintf( stderr, "%s: incompatible previous "
333 "authentication choice\n",
337 authmethod = LDAP_AUTH_SASL;
338 version = LDAP_VERSION3;
339 sasl_realm = strdup( optarg );
341 fprintf( stderr, "%s: not compiled with SASL support\n",
343 return( EXIT_FAILURE );
347 #ifdef HAVE_CYRUS_SASL
348 if( sasl_authc_id != NULL ) {
349 fprintf( stderr, "%s: -U previously specified\n", prog );
352 if( version == LDAP_VERSION2 ) {
353 fprintf( stderr, "%s: -U incompatible with version %d\n",
357 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
358 fprintf( stderr, "%s: incompatible previous "
359 "authentication choice\n",
363 authmethod = LDAP_AUTH_SASL;
364 version = LDAP_VERSION3;
365 sasl_authc_id = strdup( optarg );
367 fprintf( stderr, "%s: not compiled with SASL support\n",
369 return( EXIT_FAILURE );
372 case 'v': /* verbose mode */
375 case 'w': /* password */
376 passwd.bv_val = strdup( optarg );
380 for( p = optarg; *p != '\0'; p++ ) {
384 passwd.bv_len = strlen( passwd.bv_val );
390 #ifdef HAVE_CYRUS_SASL
391 if( sasl_mech != NULL ) {
392 fprintf( stderr, "%s: -Y previously specified\n", prog );
395 if( version == LDAP_VERSION2 ) {
396 fprintf( stderr, "%s: -Y incompatible with version %d\n",
400 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
401 fprintf( stderr, "%s: incompatible with authentication choice\n", prog );
404 authmethod = LDAP_AUTH_SASL;
405 version = LDAP_VERSION3;
406 sasl_mech = strdup( optarg );
408 fprintf( stderr, "%s: not compiled with SASL support\n",
410 return( EXIT_FAILURE );
414 if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
415 fprintf( stderr, "%s: incompatible with previous "
416 "authentication choice\n", prog );
419 authmethod = LDAP_AUTH_SIMPLE;
422 #ifdef HAVE_CYRUS_SASL
423 if( sasl_authz_id != NULL ) {
424 fprintf( stderr, "%s: -X previously specified\n", prog );
427 if( version == LDAP_VERSION2 ) {
428 fprintf( stderr, "%s: -X incompatible with LDAPv%d\n",
432 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
433 fprintf( stderr, "%s: -X incompatible with "
434 "authentication choice\n", prog );
437 authmethod = LDAP_AUTH_SASL;
438 version = LDAP_VERSION3;
439 sasl_authz_id = strdup( optarg );
441 fprintf( stderr, "%s: not compiled with SASL support\n",
443 return( EXIT_FAILURE );
448 if( version == LDAP_VERSION2 ) {
449 fprintf( stderr, "%s: -Z incompatible with version %d\n",
453 version = LDAP_VERSION3;
456 fprintf( stderr, "%s: not compiled with TLS support\n",
458 return( EXIT_FAILURE );
462 fprintf( stderr, "%s: unrecognized option -%c\n",
465 return( EXIT_FAILURE );
470 version = LDAP_VERSION3;
472 if (authmethod == -1 && version > LDAP_VERSION2) {
473 #ifdef HAVE_CYRUS_SASL
474 authmethod = LDAP_AUTH_SASL;
476 authmethod = LDAP_AUTH_SIMPLE;
481 if ( optind >= argc ) {
487 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
488 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
490 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
491 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
496 (void) SIGNAL( SIGPIPE, SIG_IGN );
499 if( ( ldaphost != NULL || ldapport ) && ( ldapuri == NULL ) ) {
501 fprintf( stderr, "ldap_init( %s, %d )\n",
502 ldaphost != NULL ? ldaphost : "<DEFAULT>",
506 ld = ldap_init( ldaphost, ldapport );
508 perror("ldapdelete: ldap_init");
514 fprintf( stderr, "ldap_initialize( %s )\n",
515 ldapuri != NULL ? ldapuri : "<DEFAULT>" );
518 rc = ldap_initialize( &ld, ldapuri );
519 if( rc != LDAP_SUCCESS ) {
520 fprintf( stderr, "Could not create LDAP session handle (%d): %s\n",
521 rc, ldap_err2string(rc) );
527 /* this seems prudent for searches below */
528 int deref = LDAP_DEREF_NEVER;
529 ldap_set_option( ld, LDAP_OPT_DEREF, &deref );
532 /* chase referrals */
533 if( ldap_set_option( ld, LDAP_OPT_REFERRALS,
534 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
536 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
537 referrals ? "on" : "off" );
541 if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version )
542 != LDAP_OPT_SUCCESS )
544 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
549 if ( use_tls && ( ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS )) {
550 ldap_perror( ld, "ldap_start_tls" );
557 passwd.bv_val = getpassphrase("Enter LDAP Password: ");
558 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
561 if ( authmethod == LDAP_AUTH_SASL ) {
562 #ifdef HAVE_CYRUS_SASL
565 if( sasl_secprops != NULL ) {
566 rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
567 (void *) sasl_secprops );
569 if( rc != LDAP_OPT_SUCCESS ) {
571 "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
573 return( EXIT_FAILURE );
577 defaults = lutil_sasl_defaults( ld,
584 rc = ldap_sasl_interactive_bind_s( ld, binddn,
585 sasl_mech, NULL, NULL,
586 sasl_flags, lutil_sasl_interact, defaults );
588 if( rc != LDAP_SUCCESS ) {
589 ldap_perror( ld, "ldap_sasl_interactive_bind_s" );
590 return( EXIT_FAILURE );
593 fprintf( stderr, "%s: not compiled with SASL support\n",
595 return( EXIT_FAILURE );
599 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
601 ldap_perror( ld, "ldap_bind" );
602 return( EXIT_FAILURE );
609 LDAPControl *ctrls[2];
613 c.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
614 c.ldctl_value.bv_val = NULL;
615 c.ldctl_value.bv_len = 0;
616 c.ldctl_iscritical = manageDSAit > 1;
618 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, ctrls );
620 if( err != LDAP_OPT_SUCCESS ) {
621 fprintf( stderr, "Could not set ManageDSAit %scontrol\n",
622 c.ldctl_iscritical ? "critical " : "" );
623 if( c.ldctl_iscritical ) {
624 exit( EXIT_FAILURE );
632 for ( ; optind < argc; ++optind ) {
633 rc = dodelete( ld, argv[ optind ] );
635 /* Stop on error and no -c option */
636 if( rc != 0 && contoper == 0) break;
639 while ((rc == 0 || contoper) && fgets(buf, sizeof(buf), fp) != NULL) {
640 buf[ strlen( buf ) - 1 ] = '\0'; /* remove trailing newline */
642 if ( *buf != '\0' ) {
643 rc = dodelete( ld, buf );
660 char *matcheddn = NULL, *text = NULL, **refs = NULL;
664 printf( "%sdeleting entry \"%s\"\n",
665 (not ? "!" : ""), dn );
672 /* If prune is on, remove a whole subtree. Delete the children of the
673 * DN recursively, then the DN requested.
675 if ( prune ) deletechildren( ld, dn );
677 rc = ldap_delete_ext( ld, dn, NULL, NULL, &id );
678 if ( rc != LDAP_SUCCESS ) {
679 fprintf( stderr, "%s: ldap_delete_ext: %s (%d)\n",
680 prog, ldap_err2string( rc ), rc );
684 rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, NULL, &res );
686 ldap_perror( ld, "ldapdelete: ldap_result" );
690 rc = ldap_parse_result( ld, res, &code, &matcheddn, &text, &refs, NULL, 1 );
692 if( rc != LDAP_SUCCESS ) {
693 fprintf( stderr, "%s: ldap_parse_result: %s (%d)\n",
694 prog, ldap_err2string( rc ), rc );
698 if( verbose || code != LDAP_SUCCESS ||
699 (matcheddn && *matcheddn) || (text && *text) || (refs && *refs) )
701 printf( "Delete Result: %s (%d)\n", ldap_err2string( code ), code );
703 if( text && *text ) {
704 printf( "Additional info: %s\n", text );
707 if( matcheddn && *matcheddn ) {
708 printf( "Matched DN: %s\n", matcheddn );
713 for( i=0; refs[i]; i++ ) {
714 printf("Referral: %s\n", refs[i] );
720 ber_memfree( matcheddn );
721 ber_memvfree( (void **) refs );
727 * Delete all the children of an entry recursively until leaf nodes are reached.
730 static int deletechildren(
734 LDAPMessage *res, *e;
737 static char *attrs[] = { "1.1", NULL };
739 if ( verbose ) printf ( "deleting children of: %s\n", dn );
741 * Do a one level search at dn for children. For each, delete its children.
744 rc = ldap_search_ext_s( ld, dn, LDAP_SCOPE_ONELEVEL, NULL, attrs, 1,
745 NULL, NULL, NULL, -1, &res );
746 if ( rc != LDAP_SUCCESS ) {
747 ldap_perror( ld, "ldap_search" );
751 entries = ldap_count_entries( ld, res );
756 for (e = ldap_first_entry( ld, res ), i = 0; e != NULL;
757 e = ldap_next_entry( ld, e ), i++ )
759 char *dn = ldap_get_dn( ld, e );
762 ldap_perror( ld, "ldap_prune" );
763 ldap_get_option( ld, LDAP_OPT_ERROR_NUMBER, &rc );
768 rc = deletechildren( ld, dn );
770 ldap_perror( ld, "ldap_prune" );
776 printf( "\tremoving %s\n", dn );
779 rc = ldap_delete_s( ld, dn );
781 ldap_perror( ld, "ldap_delete" );
788 printf( "\t%s removed\n", dn );