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 " -e [!]<ctrl>[=<ctrlparam>] general controls (! indicates criticality)\n"
66 " [!]authzid=<authzid> (\"dn:<dn>\" or \"u:<user>\")\n"
67 " [!]manageDSAit (alternate form, see -M)\n"
69 " -f file read operations from `file'\n"
70 " -h host LDAP server\n"
71 " -H URI LDAP Uniform Resource Indentifier(s)\n"
72 " -I use SASL Interactive mode\n"
73 " -k use Kerberos authentication\n"
74 " -K like -k, but do only step 1 of the Kerberos bind\n"
75 " -M enable Manage DSA IT control (-MM to make critical)\n"
76 " -n show what would be done but don't actually do it\n"
77 " -O props SASL security properties\n"
78 " -p port port on LDAP server\n"
79 " -P version procotol version (default: 3)\n"
80 " -Q use SASL Quiet mode\n"
81 " -R realm SASL realm\n"
82 " -U authcid SASL authentication identity\n"
83 " -v run in verbose mode (diagnostics to standard output)\n"
84 " -w passwd bind passwd (for simple authentication)\n"
85 " -W prompt for bind passwd\n"
86 " -x Simple authentication\n"
87 " -X authzid SASL authorization identity (\"dn:<dn>\" or \"u:<user>\")\n"
88 " -y file Read passwd from file\n"
89 " -Y mech SASL mechanism\n"
90 " -Z Start TLS request (-ZZ to require successful response)\n"
98 main( int argc, char **argv )
102 int i, rc, authmethod, referrals, want_bindpw, version, debug, manageDSAit, noop, crit;
104 char *control, *cvalue;
105 char *authzid = NULL;
107 not = verbose = contoper = want_bindpw = debug
108 = manageDSAit = noop = referrals = 0;
114 prog = lutil_progname( "ldapdelete", argc, argv );
116 while (( i = getopt( argc, argv, "cf:r"
117 "Cd:D:e:h:H:IkKMnO:p:P:QR:U:vw:WxX:y:Y:Z" )) != EOF )
120 /* Delete Specific Options */
121 case 'c': /* continuous operation mode */
124 case 'E': /* delete controls */
125 if( version == LDAP_VERSION2 ) {
126 fprintf( stderr, "%s: -E incompatible with LDAPv%d\n",
131 /* should be extended to support comma separated list of
132 * [!]key[=value] parameters, e.g. -E !foo,bar=567
137 if( optarg[0] == '!' ) {
142 control = strdup( optarg );
143 if ( (cvalue = strchr( control, '=' )) != NULL ) {
146 fprintf( stderr, "Invalid delete control name: %s\n", control );
149 case 'f': /* read DNs from a file */
151 fprintf( stderr, "%s: -f previously specified\n", prog );
154 if (( fp = fopen( optarg, "r" )) == NULL ) {
156 exit( EXIT_FAILURE );
168 debug |= atoi( optarg );
170 case 'D': /* bind DN */
171 if( binddn != NULL ) {
172 fprintf( stderr, "%s: -D previously specified\n", prog );
175 binddn = strdup( optarg );
177 case 'e': /* general controls */
178 if( version == LDAP_VERSION2 ) {
179 fprintf( stderr, "%s: -e incompatible with LDAPv%d\n",
184 /* should be extended to support comma separated list of
185 * [!]key[=value] parameters, e.g. -e !foo,bar=567
190 if( optarg[0] == '!' ) {
195 control = strdup( optarg );
196 if ( (cvalue = strchr( control, '=' )) != NULL ) {
200 if ( strcasecmp( control, "manageDSAit" ) == 0 ) {
202 fprintf( stderr, "manageDSAit control previously specified");
205 if( cvalue != NULL ) {
206 fprintf( stderr, "manageDSAit: no control value expected" );
211 manageDSAit = 1 + crit;
214 } else if ( strcasecmp( control, "noop" ) == 0 ) {
216 fprintf( stderr, "noop control previously specified");
219 if( cvalue != NULL ) {
220 fprintf( stderr, "noop: no control value expected" );
229 fprintf( stderr, "Invalid general control name: %s\n", control );
233 case 'h': /* ldap host */
234 if( ldapuri != NULL ) {
235 fprintf( stderr, "%s: -h incompatible with -H\n", prog );
238 if( ldaphost != NULL ) {
239 fprintf( stderr, "%s: -h previously specified\n", prog );
242 ldaphost = strdup( optarg );
244 case 'H': /* ldap URI */
245 if( ldaphost != NULL ) {
246 fprintf( stderr, "%s: -H incompatible with -h\n", prog );
250 fprintf( stderr, "%s: -H incompatible with -p\n", prog );
253 if( ldapuri != NULL ) {
254 fprintf( stderr, "%s: -H previously specified\n", prog );
257 ldapuri = strdup( optarg );
260 #ifdef HAVE_CYRUS_SASL
261 if( version == LDAP_VERSION2 ) {
262 fprintf( stderr, "%s: -I incompatible with version %d\n",
266 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
267 fprintf( stderr, "%s: incompatible previous "
268 "authentication choice\n",
272 authmethod = LDAP_AUTH_SASL;
273 version = LDAP_VERSION3;
274 sasl_flags = LDAP_SASL_INTERACTIVE;
277 fprintf( stderr, "%s: was not compiled with SASL support\n",
279 return( EXIT_FAILURE );
281 case 'k': /* kerberos bind */
282 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
283 if( version > LDAP_VERSION2 ) {
284 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
289 if( authmethod != -1 ) {
290 fprintf( stderr, "%s: -k incompatible with previous "
291 "authentication choice\n", prog );
295 authmethod = LDAP_AUTH_KRBV4;
297 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
301 case 'K': /* kerberos bind, part one only */
302 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
303 if( version > LDAP_VERSION2 ) {
304 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
308 if( authmethod != -1 ) {
309 fprintf( stderr, "%s: incompatible with previous "
310 "authentication choice\n", prog );
314 authmethod = LDAP_AUTH_KRBV41;
316 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
317 return( EXIT_FAILURE );
321 /* enable Manage DSA IT */
322 if( version == LDAP_VERSION2 ) {
323 fprintf( stderr, "%s: -M incompatible with LDAPv%d\n",
328 version = LDAP_VERSION3;
330 case 'n': /* print deletes, don't actually do them */
334 #ifdef HAVE_CYRUS_SASL
335 if( sasl_secprops != NULL ) {
336 fprintf( stderr, "%s: -O previously specified\n", prog );
339 if( version == LDAP_VERSION2 ) {
340 fprintf( stderr, "%s: -O incompatible with LDAPv%d\n",
344 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
345 fprintf( stderr, "%s: incompatible previous "
346 "authentication choice\n", prog );
349 authmethod = LDAP_AUTH_SASL;
350 version = LDAP_VERSION3;
351 sasl_secprops = strdup( optarg );
353 fprintf( stderr, "%s: not compiled with SASL support\n",
355 return( EXIT_FAILURE );
360 fprintf( stderr, "%s: -p previously specified\n", prog );
363 ldapport = atoi( optarg );
366 switch( atoi(optarg) ) {
368 if( version == LDAP_VERSION3 ) {
369 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
373 version = LDAP_VERSION2;
376 if( version == LDAP_VERSION2 ) {
377 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
381 version = LDAP_VERSION3;
384 fprintf( stderr, "%s: protocol version should be 2 or 3\n",
387 return( EXIT_FAILURE );
390 #ifdef HAVE_CYRUS_SASL
391 if( version == LDAP_VERSION2 ) {
392 fprintf( stderr, "%s: -Q incompatible with version %d\n",
396 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
397 fprintf( stderr, "%s: incompatible previous "
398 "authentication choice\n",
402 authmethod = LDAP_AUTH_SASL;
403 version = LDAP_VERSION3;
404 sasl_flags = LDAP_SASL_QUIET;
407 fprintf( stderr, "%s: not compiled with SASL support\n",
409 return( EXIT_FAILURE );
412 #ifdef HAVE_CYRUS_SASL
413 if( sasl_realm != NULL ) {
414 fprintf( stderr, "%s: -R previously specified\n", prog );
417 if( version == LDAP_VERSION2 ) {
418 fprintf( stderr, "%s: -R incompatible with version %d\n",
422 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
423 fprintf( stderr, "%s: incompatible previous "
424 "authentication choice\n",
428 authmethod = LDAP_AUTH_SASL;
429 version = LDAP_VERSION3;
430 sasl_realm = strdup( optarg );
432 fprintf( stderr, "%s: not compiled with SASL support\n",
434 return( EXIT_FAILURE );
438 #ifdef HAVE_CYRUS_SASL
439 if( sasl_authc_id != NULL ) {
440 fprintf( stderr, "%s: -U previously specified\n", prog );
443 if( version == LDAP_VERSION2 ) {
444 fprintf( stderr, "%s: -U incompatible with version %d\n",
448 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
449 fprintf( stderr, "%s: incompatible previous "
450 "authentication choice\n",
454 authmethod = LDAP_AUTH_SASL;
455 version = LDAP_VERSION3;
456 sasl_authc_id = strdup( optarg );
458 fprintf( stderr, "%s: not compiled with SASL support\n",
460 return( EXIT_FAILURE );
463 case 'v': /* verbose mode */
466 case 'w': /* password */
467 passwd.bv_val = strdup( optarg );
471 for( p = optarg; *p != '\0'; p++ ) {
475 passwd.bv_len = strlen( passwd.bv_val );
484 #ifdef HAVE_CYRUS_SASL
485 if( sasl_mech != NULL ) {
486 fprintf( stderr, "%s: -Y previously specified\n", prog );
489 if( version == LDAP_VERSION2 ) {
490 fprintf( stderr, "%s: -Y incompatible with version %d\n",
494 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
495 fprintf( stderr, "%s: incompatible with authentication choice\n", prog );
498 authmethod = LDAP_AUTH_SASL;
499 version = LDAP_VERSION3;
500 sasl_mech = strdup( optarg );
502 fprintf( stderr, "%s: not compiled with SASL support\n",
504 return( EXIT_FAILURE );
508 if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
509 fprintf( stderr, "%s: incompatible with previous "
510 "authentication choice\n", prog );
513 authmethod = LDAP_AUTH_SIMPLE;
516 #ifdef HAVE_CYRUS_SASL
517 if( sasl_authz_id != NULL ) {
518 fprintf( stderr, "%s: -X previously specified\n", prog );
521 if( version == LDAP_VERSION2 ) {
522 fprintf( stderr, "%s: -X incompatible with LDAPv%d\n",
526 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
527 fprintf( stderr, "%s: -X incompatible with "
528 "authentication choice\n", prog );
531 authmethod = LDAP_AUTH_SASL;
532 version = LDAP_VERSION3;
533 sasl_authz_id = strdup( optarg );
535 fprintf( stderr, "%s: not compiled with SASL support\n",
537 return( EXIT_FAILURE );
542 if( version == LDAP_VERSION2 ) {
543 fprintf( stderr, "%s: -Z incompatible with version %d\n",
547 version = LDAP_VERSION3;
550 fprintf( stderr, "%s: not compiled with TLS support\n",
552 return( EXIT_FAILURE );
556 fprintf( stderr, "%s: unrecognized option -%c\n",
559 return( EXIT_FAILURE );
564 version = LDAP_VERSION3;
566 if (authmethod == -1 && version > LDAP_VERSION2) {
567 #ifdef HAVE_CYRUS_SASL
568 authmethod = LDAP_AUTH_SASL;
570 authmethod = LDAP_AUTH_SIMPLE;
575 if ( optind >= argc ) {
581 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
582 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
584 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
585 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
590 (void) SIGNAL( SIGPIPE, SIG_IGN );
593 if( ( ldaphost != NULL || ldapport ) && ( ldapuri == NULL ) ) {
595 fprintf( stderr, "ldap_init( %s, %d )\n",
596 ldaphost != NULL ? ldaphost : "<DEFAULT>",
600 ld = ldap_init( ldaphost, ldapport );
602 perror("ldapdelete: ldap_init");
608 fprintf( stderr, "ldap_initialize( %s )\n",
609 ldapuri != NULL ? ldapuri : "<DEFAULT>" );
612 rc = ldap_initialize( &ld, ldapuri );
613 if( rc != LDAP_SUCCESS ) {
614 fprintf( stderr, "Could not create LDAP session handle (%d): %s\n",
615 rc, ldap_err2string(rc) );
621 /* this seems prudent for searches below */
622 int deref = LDAP_DEREF_NEVER;
623 ldap_set_option( ld, LDAP_OPT_DEREF, &deref );
626 /* chase referrals */
627 if( ldap_set_option( ld, LDAP_OPT_REFERRALS,
628 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
630 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
631 referrals ? "on" : "off" );
635 if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version )
636 != LDAP_OPT_SUCCESS )
638 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
643 if ( use_tls && ( ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS )) {
644 ldap_perror( ld, "ldap_start_tls" );
650 if ( pw_file || want_bindpw ) {
652 rc = lutil_get_filed_password( pw_file, &passwd );
653 if( rc ) return EXIT_FAILURE;
655 passwd.bv_val = getpassphrase( "Enter LDAP Password: " );
656 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
660 if ( authmethod == LDAP_AUTH_SASL ) {
661 #ifdef HAVE_CYRUS_SASL
664 if( sasl_secprops != NULL ) {
665 rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
666 (void *) sasl_secprops );
668 if( rc != LDAP_OPT_SUCCESS ) {
670 "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
672 return( EXIT_FAILURE );
676 defaults = lutil_sasl_defaults( ld,
683 rc = ldap_sasl_interactive_bind_s( ld, binddn,
684 sasl_mech, NULL, NULL,
685 sasl_flags, lutil_sasl_interact, defaults );
687 if( rc != LDAP_SUCCESS ) {
688 ldap_perror( ld, "ldap_sasl_interactive_bind_s" );
689 return( EXIT_FAILURE );
692 fprintf( stderr, "%s: not compiled with SASL support\n",
694 return( EXIT_FAILURE );
698 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
700 ldap_perror( ld, "ldap_bind" );
701 return( EXIT_FAILURE );
705 if ( authzid || manageDSAit || noop ) {
706 int err, crit=0, i=0;
708 LDAPControl *ctrls[4];
711 c[i].ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
712 c[i].ldctl_value.bv_val = authzid;
713 c[i].ldctl_value.bv_len = strlen( authzid );
714 c[i].ldctl_iscritical = 1;
716 if( c[i].ldctl_iscritical ) crit++;
722 c[i].ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
723 c[i].ldctl_value.bv_val = NULL;
724 c[i].ldctl_value.bv_len = 0;
725 c[i].ldctl_iscritical = manageDSAit > 1;
727 if( c[i].ldctl_iscritical ) crit++;
733 c[i].ldctl_oid = LDAP_CONTROL_NOOP;
734 c[i].ldctl_value.bv_val = NULL;
735 c[i].ldctl_value.bv_len = 0;
736 c[i].ldctl_iscritical = noop > 1;
738 if( c[i].ldctl_iscritical ) crit++;
743 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, ctrls );
745 if( err != LDAP_OPT_SUCCESS ) {
746 fprintf( stderr, "Could not set %scontrols\n",
747 crit ? "critical " : "" );
757 for ( ; optind < argc; ++optind ) {
758 rc = dodelete( ld, argv[ optind ] );
760 /* Stop on error and no -c option */
761 if( rc != 0 && contoper == 0) break;
764 while ((rc == 0 || contoper) && fgets(buf, sizeof(buf), fp) != NULL) {
765 buf[ strlen( buf ) - 1 ] = '\0'; /* remove trailing newline */
767 if ( *buf != '\0' ) {
768 rc = dodelete( ld, buf );
785 char *matcheddn = NULL, *text = NULL, **refs = NULL;
789 printf( "%sdeleting entry \"%s\"\n",
790 (not ? "!" : ""), dn );
797 /* If prune is on, remove a whole subtree. Delete the children of the
798 * DN recursively, then the DN requested.
800 if ( prune ) deletechildren( ld, dn );
802 rc = ldap_delete_ext( ld, dn, NULL, NULL, &id );
803 if ( rc != LDAP_SUCCESS ) {
804 fprintf( stderr, "%s: ldap_delete_ext: %s (%d)\n",
805 prog, ldap_err2string( rc ), rc );
809 rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, NULL, &res );
811 ldap_perror( ld, "ldapdelete: ldap_result" );
815 rc = ldap_parse_result( ld, res, &code, &matcheddn, &text, &refs, NULL, 1 );
817 if( rc != LDAP_SUCCESS ) {
818 fprintf( stderr, "%s: ldap_parse_result: %s (%d)\n",
819 prog, ldap_err2string( rc ), rc );
823 if( verbose || code != LDAP_SUCCESS ||
824 (matcheddn && *matcheddn) || (text && *text) || (refs && *refs) )
826 printf( "Delete Result: %s (%d)\n", ldap_err2string( code ), code );
828 if( text && *text ) {
829 printf( "Additional info: %s\n", text );
832 if( matcheddn && *matcheddn ) {
833 printf( "Matched DN: %s\n", matcheddn );
838 for( i=0; refs[i]; i++ ) {
839 printf("Referral: %s\n", refs[i] );
845 ber_memfree( matcheddn );
846 ber_memvfree( (void **) refs );
852 * Delete all the children of an entry recursively until leaf nodes are reached.
855 static int deletechildren(
859 LDAPMessage *res, *e;
862 static char *attrs[] = { "1.1", NULL };
864 if ( verbose ) printf ( "deleting children of: %s\n", dn );
866 * Do a one level search at dn for children. For each, delete its children.
869 rc = ldap_search_ext_s( ld, dn, LDAP_SCOPE_ONELEVEL, NULL, attrs, 1,
870 NULL, NULL, NULL, -1, &res );
871 if ( rc != LDAP_SUCCESS ) {
872 ldap_perror( ld, "ldap_search" );
876 entries = ldap_count_entries( ld, res );
881 for (e = ldap_first_entry( ld, res ), i = 0; e != NULL;
882 e = ldap_next_entry( ld, e ), i++ )
884 char *dn = ldap_get_dn( ld, e );
887 ldap_perror( ld, "ldap_prune" );
888 ldap_get_option( ld, LDAP_OPT_ERROR_NUMBER, &rc );
893 rc = deletechildren( ld, dn );
895 ldap_perror( ld, "ldap_prune" );
901 printf( "\tremoving %s\n", dn );
904 rc = ldap_delete_s( ld, dn );
906 ldap_perror( ld, "ldap_delete" );
913 printf( "\t%s removed\n", dn );