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 file Read passwd from file\n"
85 " -Y mech SASL mechanism\n"
86 " -Z Start TLS request (-ZZ to require successful response)\n"
94 main( int argc, char **argv )
98 int i, rc, authmethod, referrals, want_bindpw, version, debug, manageDSAit;
101 not = verbose = contoper = want_bindpw = debug = manageDSAit = referrals = 0;
107 prog = lutil_progname( "ldapdelete", argc, argv );
109 while (( i = getopt( argc, argv, "cf:r"
110 "Cd:D:h:H:IkKMnO:p:P:QR:U:vw:WxX:y:Y:Z" )) != EOF )
113 /* Delete Specific Options */
114 case 'c': /* continuous operation mode */
117 case 'f': /* read DNs from a file */
119 fprintf( stderr, "%s: -f previously specified\n", prog );
122 if (( fp = fopen( optarg, "r" )) == NULL ) {
124 exit( EXIT_FAILURE );
136 debug |= atoi( optarg );
138 case 'D': /* bind DN */
139 if( binddn != NULL ) {
140 fprintf( stderr, "%s: -D previously specified\n", prog );
143 binddn = strdup( optarg );
145 case 'h': /* ldap host */
146 if( ldapuri != NULL ) {
147 fprintf( stderr, "%s: -h incompatible with -H\n", prog );
150 if( ldaphost != NULL ) {
151 fprintf( stderr, "%s: -h previously specified\n", prog );
154 ldaphost = strdup( optarg );
156 case 'H': /* ldap URI */
157 if( ldaphost != NULL ) {
158 fprintf( stderr, "%s: -H incompatible with -h\n", prog );
162 fprintf( stderr, "%s: -H incompatible with -p\n", prog );
165 if( ldapuri != NULL ) {
166 fprintf( stderr, "%s: -H previously specified\n", prog );
169 ldapuri = strdup( optarg );
172 #ifdef HAVE_CYRUS_SASL
173 if( version == LDAP_VERSION2 ) {
174 fprintf( stderr, "%s: -I incompatible with version %d\n",
178 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
179 fprintf( stderr, "%s: incompatible previous "
180 "authentication choice\n",
184 authmethod = LDAP_AUTH_SASL;
185 version = LDAP_VERSION3;
186 sasl_flags = LDAP_SASL_INTERACTIVE;
189 fprintf( stderr, "%s: was not compiled with SASL support\n",
191 return( EXIT_FAILURE );
193 case 'k': /* kerberos bind */
194 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
195 if( version > LDAP_VERSION2 ) {
196 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
201 if( authmethod != -1 ) {
202 fprintf( stderr, "%s: -k incompatible with previous "
203 "authentication choice\n", prog );
207 authmethod = LDAP_AUTH_KRBV4;
209 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
213 case 'K': /* kerberos bind, part one only */
214 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
215 if( version > LDAP_VERSION2 ) {
216 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
220 if( authmethod != -1 ) {
221 fprintf( stderr, "%s: incompatible with previous "
222 "authentication choice\n", prog );
226 authmethod = LDAP_AUTH_KRBV41;
228 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
229 return( EXIT_FAILURE );
233 /* enable Manage DSA IT */
234 if( version == LDAP_VERSION2 ) {
235 fprintf( stderr, "%s: -M incompatible with LDAPv%d\n",
240 version = LDAP_VERSION3;
242 case 'n': /* print deletes, don't actually do them */
246 #ifdef HAVE_CYRUS_SASL
247 if( sasl_secprops != NULL ) {
248 fprintf( stderr, "%s: -O previously specified\n", prog );
251 if( version == LDAP_VERSION2 ) {
252 fprintf( stderr, "%s: -O incompatible with LDAPv%d\n",
256 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
257 fprintf( stderr, "%s: incompatible previous "
258 "authentication choice\n", prog );
261 authmethod = LDAP_AUTH_SASL;
262 version = LDAP_VERSION3;
263 sasl_secprops = strdup( optarg );
265 fprintf( stderr, "%s: not compiled with SASL support\n",
267 return( EXIT_FAILURE );
272 fprintf( stderr, "%s: -p previously specified\n", prog );
275 ldapport = atoi( optarg );
278 switch( atoi(optarg) ) {
280 if( version == LDAP_VERSION3 ) {
281 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
285 version = LDAP_VERSION2;
288 if( version == LDAP_VERSION2 ) {
289 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
293 version = LDAP_VERSION3;
296 fprintf( stderr, "%s: protocol version should be 2 or 3\n",
299 return( EXIT_FAILURE );
302 #ifdef HAVE_CYRUS_SASL
303 if( version == LDAP_VERSION2 ) {
304 fprintf( stderr, "%s: -Q incompatible with version %d\n",
308 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
309 fprintf( stderr, "%s: incompatible previous "
310 "authentication choice\n",
314 authmethod = LDAP_AUTH_SASL;
315 version = LDAP_VERSION3;
316 sasl_flags = LDAP_SASL_QUIET;
319 fprintf( stderr, "%s: not compiled with SASL support\n",
321 return( EXIT_FAILURE );
324 #ifdef HAVE_CYRUS_SASL
325 if( sasl_realm != NULL ) {
326 fprintf( stderr, "%s: -R previously specified\n", prog );
329 if( version == LDAP_VERSION2 ) {
330 fprintf( stderr, "%s: -R incompatible with version %d\n",
334 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
335 fprintf( stderr, "%s: incompatible previous "
336 "authentication choice\n",
340 authmethod = LDAP_AUTH_SASL;
341 version = LDAP_VERSION3;
342 sasl_realm = strdup( optarg );
344 fprintf( stderr, "%s: not compiled with SASL support\n",
346 return( EXIT_FAILURE );
350 #ifdef HAVE_CYRUS_SASL
351 if( sasl_authc_id != NULL ) {
352 fprintf( stderr, "%s: -U previously specified\n", prog );
355 if( version == LDAP_VERSION2 ) {
356 fprintf( stderr, "%s: -U incompatible with version %d\n",
360 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
361 fprintf( stderr, "%s: incompatible previous "
362 "authentication choice\n",
366 authmethod = LDAP_AUTH_SASL;
367 version = LDAP_VERSION3;
368 sasl_authc_id = strdup( optarg );
370 fprintf( stderr, "%s: not compiled with SASL support\n",
372 return( EXIT_FAILURE );
375 case 'v': /* verbose mode */
378 case 'w': /* password */
379 passwd.bv_val = strdup( optarg );
383 for( p = optarg; *p != '\0'; p++ ) {
387 passwd.bv_len = strlen( passwd.bv_val );
396 #ifdef HAVE_CYRUS_SASL
397 if( sasl_mech != NULL ) {
398 fprintf( stderr, "%s: -Y previously specified\n", prog );
401 if( version == LDAP_VERSION2 ) {
402 fprintf( stderr, "%s: -Y incompatible with version %d\n",
406 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
407 fprintf( stderr, "%s: incompatible with authentication choice\n", prog );
410 authmethod = LDAP_AUTH_SASL;
411 version = LDAP_VERSION3;
412 sasl_mech = strdup( optarg );
414 fprintf( stderr, "%s: not compiled with SASL support\n",
416 return( EXIT_FAILURE );
420 if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
421 fprintf( stderr, "%s: incompatible with previous "
422 "authentication choice\n", prog );
425 authmethod = LDAP_AUTH_SIMPLE;
428 #ifdef HAVE_CYRUS_SASL
429 if( sasl_authz_id != NULL ) {
430 fprintf( stderr, "%s: -X previously specified\n", prog );
433 if( version == LDAP_VERSION2 ) {
434 fprintf( stderr, "%s: -X incompatible with LDAPv%d\n",
438 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
439 fprintf( stderr, "%s: -X incompatible with "
440 "authentication choice\n", prog );
443 authmethod = LDAP_AUTH_SASL;
444 version = LDAP_VERSION3;
445 sasl_authz_id = strdup( optarg );
447 fprintf( stderr, "%s: not compiled with SASL support\n",
449 return( EXIT_FAILURE );
454 if( version == LDAP_VERSION2 ) {
455 fprintf( stderr, "%s: -Z incompatible with version %d\n",
459 version = LDAP_VERSION3;
462 fprintf( stderr, "%s: not compiled with TLS support\n",
464 return( EXIT_FAILURE );
468 fprintf( stderr, "%s: unrecognized option -%c\n",
471 return( EXIT_FAILURE );
476 version = LDAP_VERSION3;
478 if (authmethod == -1 && version > LDAP_VERSION2) {
479 #ifdef HAVE_CYRUS_SASL
480 authmethod = LDAP_AUTH_SASL;
482 authmethod = LDAP_AUTH_SIMPLE;
487 if ( optind >= argc ) {
493 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
494 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
496 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
497 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
502 (void) SIGNAL( SIGPIPE, SIG_IGN );
505 if( ( ldaphost != NULL || ldapport ) && ( ldapuri == NULL ) ) {
507 fprintf( stderr, "ldap_init( %s, %d )\n",
508 ldaphost != NULL ? ldaphost : "<DEFAULT>",
512 ld = ldap_init( ldaphost, ldapport );
514 perror("ldapdelete: ldap_init");
520 fprintf( stderr, "ldap_initialize( %s )\n",
521 ldapuri != NULL ? ldapuri : "<DEFAULT>" );
524 rc = ldap_initialize( &ld, ldapuri );
525 if( rc != LDAP_SUCCESS ) {
526 fprintf( stderr, "Could not create LDAP session handle (%d): %s\n",
527 rc, ldap_err2string(rc) );
533 /* this seems prudent for searches below */
534 int deref = LDAP_DEREF_NEVER;
535 ldap_set_option( ld, LDAP_OPT_DEREF, &deref );
538 /* chase referrals */
539 if( ldap_set_option( ld, LDAP_OPT_REFERRALS,
540 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
542 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
543 referrals ? "on" : "off" );
547 if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version )
548 != LDAP_OPT_SUCCESS )
550 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
555 if ( use_tls && ( ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS )) {
556 ldap_perror( ld, "ldap_start_tls" );
562 if ( pw_file || want_bindpw ) {
564 rc = lutil_get_filed_password( pw_file, &passwd );
565 if( rc ) return EXIT_FAILURE;
567 passwd.bv_val = getpassphrase( "Enter LDAP Password: " );
568 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
572 if ( authmethod == LDAP_AUTH_SASL ) {
573 #ifdef HAVE_CYRUS_SASL
576 if( sasl_secprops != NULL ) {
577 rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
578 (void *) sasl_secprops );
580 if( rc != LDAP_OPT_SUCCESS ) {
582 "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
584 return( EXIT_FAILURE );
588 defaults = lutil_sasl_defaults( ld,
595 rc = ldap_sasl_interactive_bind_s( ld, binddn,
596 sasl_mech, NULL, NULL,
597 sasl_flags, lutil_sasl_interact, defaults );
599 if( rc != LDAP_SUCCESS ) {
600 ldap_perror( ld, "ldap_sasl_interactive_bind_s" );
601 return( EXIT_FAILURE );
604 fprintf( stderr, "%s: not compiled with SASL support\n",
606 return( EXIT_FAILURE );
610 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
612 ldap_perror( ld, "ldap_bind" );
613 return( EXIT_FAILURE );
620 LDAPControl *ctrls[2];
624 c.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
625 c.ldctl_value.bv_val = NULL;
626 c.ldctl_value.bv_len = 0;
627 c.ldctl_iscritical = manageDSAit > 1;
629 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, ctrls );
631 if( err != LDAP_OPT_SUCCESS ) {
632 fprintf( stderr, "Could not set ManageDSAit %scontrol\n",
633 c.ldctl_iscritical ? "critical " : "" );
634 if( c.ldctl_iscritical ) {
635 exit( EXIT_FAILURE );
643 for ( ; optind < argc; ++optind ) {
644 rc = dodelete( ld, argv[ optind ] );
646 /* Stop on error and no -c option */
647 if( rc != 0 && contoper == 0) break;
650 while ((rc == 0 || contoper) && fgets(buf, sizeof(buf), fp) != NULL) {
651 buf[ strlen( buf ) - 1 ] = '\0'; /* remove trailing newline */
653 if ( *buf != '\0' ) {
654 rc = dodelete( ld, buf );
671 char *matcheddn = NULL, *text = NULL, **refs = NULL;
675 printf( "%sdeleting entry \"%s\"\n",
676 (not ? "!" : ""), dn );
683 /* If prune is on, remove a whole subtree. Delete the children of the
684 * DN recursively, then the DN requested.
686 if ( prune ) deletechildren( ld, dn );
688 rc = ldap_delete_ext( ld, dn, NULL, NULL, &id );
689 if ( rc != LDAP_SUCCESS ) {
690 fprintf( stderr, "%s: ldap_delete_ext: %s (%d)\n",
691 prog, ldap_err2string( rc ), rc );
695 rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, NULL, &res );
697 ldap_perror( ld, "ldapdelete: ldap_result" );
701 rc = ldap_parse_result( ld, res, &code, &matcheddn, &text, &refs, NULL, 1 );
703 if( rc != LDAP_SUCCESS ) {
704 fprintf( stderr, "%s: ldap_parse_result: %s (%d)\n",
705 prog, ldap_err2string( rc ), rc );
709 if( verbose || code != LDAP_SUCCESS ||
710 (matcheddn && *matcheddn) || (text && *text) || (refs && *refs) )
712 printf( "Delete Result: %s (%d)\n", ldap_err2string( code ), code );
714 if( text && *text ) {
715 printf( "Additional info: %s\n", text );
718 if( matcheddn && *matcheddn ) {
719 printf( "Matched DN: %s\n", matcheddn );
724 for( i=0; refs[i]; i++ ) {
725 printf("Referral: %s\n", refs[i] );
731 ber_memfree( matcheddn );
732 ber_memvfree( (void **) refs );
738 * Delete all the children of an entry recursively until leaf nodes are reached.
741 static int deletechildren(
745 LDAPMessage *res, *e;
748 static char *attrs[] = { "1.1", NULL };
750 if ( verbose ) printf ( "deleting children of: %s\n", dn );
752 * Do a one level search at dn for children. For each, delete its children.
755 rc = ldap_search_ext_s( ld, dn, LDAP_SCOPE_ONELEVEL, NULL, attrs, 1,
756 NULL, NULL, NULL, -1, &res );
757 if ( rc != LDAP_SUCCESS ) {
758 ldap_perror( ld, "ldap_search" );
762 entries = ldap_count_entries( ld, res );
767 for (e = ldap_first_entry( ld, res ), i = 0; e != NULL;
768 e = ldap_next_entry( ld, e ), i++ )
770 char *dn = ldap_get_dn( ld, e );
773 ldap_perror( ld, "ldap_prune" );
774 ldap_get_option( ld, LDAP_OPT_ERROR_NUMBER, &rc );
779 rc = deletechildren( ld, dn );
781 ldap_perror( ld, "ldap_prune" );
787 printf( "\tremoving %s\n", dn );
790 rc = ldap_delete_s( ld, dn );
792 ldap_perror( ld, "ldap_delete" );
799 printf( "\t%s removed\n", dn );