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, "authzid" ) == 0 ) {
201 if( authzid != NULL ) {
202 fprintf( stderr, "authzid control previously specified");
205 if( cvalue == NULL ) {
206 fprintf( stderr, "authzid: control value expected" );
211 fprintf( stderr, "authzid: must be marked critical" );
216 assert( authzid == NULL );
220 } else if ( strcasecmp( control, "manageDSAit" ) == 0 ) {
222 fprintf( stderr, "manageDSAit control previously specified");
225 if( cvalue != NULL ) {
226 fprintf( stderr, "manageDSAit: no control value expected" );
231 manageDSAit = 1 + crit;
234 } else if ( strcasecmp( control, "noop" ) == 0 ) {
236 fprintf( stderr, "noop control previously specified");
239 if( cvalue != NULL ) {
240 fprintf( stderr, "noop: no control value expected" );
249 fprintf( stderr, "Invalid general control name: %s\n", control );
253 case 'h': /* ldap host */
254 if( ldapuri != NULL ) {
255 fprintf( stderr, "%s: -h incompatible with -H\n", prog );
258 if( ldaphost != NULL ) {
259 fprintf( stderr, "%s: -h previously specified\n", prog );
262 ldaphost = strdup( optarg );
264 case 'H': /* ldap URI */
265 if( ldaphost != NULL ) {
266 fprintf( stderr, "%s: -H incompatible with -h\n", prog );
270 fprintf( stderr, "%s: -H incompatible with -p\n", prog );
273 if( ldapuri != NULL ) {
274 fprintf( stderr, "%s: -H previously specified\n", prog );
277 ldapuri = strdup( optarg );
280 #ifdef HAVE_CYRUS_SASL
281 if( version == LDAP_VERSION2 ) {
282 fprintf( stderr, "%s: -I incompatible with version %d\n",
286 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
287 fprintf( stderr, "%s: incompatible previous "
288 "authentication choice\n",
292 authmethod = LDAP_AUTH_SASL;
293 version = LDAP_VERSION3;
294 sasl_flags = LDAP_SASL_INTERACTIVE;
297 fprintf( stderr, "%s: was not compiled with SASL support\n",
299 return( EXIT_FAILURE );
301 case 'k': /* kerberos bind */
302 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
303 if( version > LDAP_VERSION2 ) {
304 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
309 if( authmethod != -1 ) {
310 fprintf( stderr, "%s: -k incompatible with previous "
311 "authentication choice\n", prog );
315 authmethod = LDAP_AUTH_KRBV4;
317 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
321 case 'K': /* kerberos bind, part one only */
322 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
323 if( version > LDAP_VERSION2 ) {
324 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
328 if( authmethod != -1 ) {
329 fprintf( stderr, "%s: incompatible with previous "
330 "authentication choice\n", prog );
334 authmethod = LDAP_AUTH_KRBV41;
336 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
337 return( EXIT_FAILURE );
341 /* enable Manage DSA IT */
342 if( version == LDAP_VERSION2 ) {
343 fprintf( stderr, "%s: -M incompatible with LDAPv%d\n",
348 version = LDAP_VERSION3;
350 case 'n': /* print deletes, don't actually do them */
354 #ifdef HAVE_CYRUS_SASL
355 if( sasl_secprops != NULL ) {
356 fprintf( stderr, "%s: -O previously specified\n", prog );
359 if( version == LDAP_VERSION2 ) {
360 fprintf( stderr, "%s: -O incompatible with LDAPv%d\n",
364 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
365 fprintf( stderr, "%s: incompatible previous "
366 "authentication choice\n", prog );
369 authmethod = LDAP_AUTH_SASL;
370 version = LDAP_VERSION3;
371 sasl_secprops = strdup( optarg );
373 fprintf( stderr, "%s: not compiled with SASL support\n",
375 return( EXIT_FAILURE );
380 fprintf( stderr, "%s: -p previously specified\n", prog );
383 ldapport = atoi( optarg );
386 switch( atoi(optarg) ) {
388 if( version == LDAP_VERSION3 ) {
389 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
393 version = LDAP_VERSION2;
396 if( version == LDAP_VERSION2 ) {
397 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
401 version = LDAP_VERSION3;
404 fprintf( stderr, "%s: protocol version should be 2 or 3\n",
407 return( EXIT_FAILURE );
410 #ifdef HAVE_CYRUS_SASL
411 if( version == LDAP_VERSION2 ) {
412 fprintf( stderr, "%s: -Q incompatible with version %d\n",
416 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
417 fprintf( stderr, "%s: incompatible previous "
418 "authentication choice\n",
422 authmethod = LDAP_AUTH_SASL;
423 version = LDAP_VERSION3;
424 sasl_flags = LDAP_SASL_QUIET;
427 fprintf( stderr, "%s: not compiled with SASL support\n",
429 return( EXIT_FAILURE );
432 #ifdef HAVE_CYRUS_SASL
433 if( sasl_realm != NULL ) {
434 fprintf( stderr, "%s: -R previously specified\n", prog );
437 if( version == LDAP_VERSION2 ) {
438 fprintf( stderr, "%s: -R incompatible with version %d\n",
442 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
443 fprintf( stderr, "%s: incompatible previous "
444 "authentication choice\n",
448 authmethod = LDAP_AUTH_SASL;
449 version = LDAP_VERSION3;
450 sasl_realm = strdup( optarg );
452 fprintf( stderr, "%s: not compiled with SASL support\n",
454 return( EXIT_FAILURE );
458 #ifdef HAVE_CYRUS_SASL
459 if( sasl_authc_id != NULL ) {
460 fprintf( stderr, "%s: -U previously specified\n", prog );
463 if( version == LDAP_VERSION2 ) {
464 fprintf( stderr, "%s: -U incompatible with version %d\n",
468 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
469 fprintf( stderr, "%s: incompatible previous "
470 "authentication choice\n",
474 authmethod = LDAP_AUTH_SASL;
475 version = LDAP_VERSION3;
476 sasl_authc_id = strdup( optarg );
478 fprintf( stderr, "%s: not compiled with SASL support\n",
480 return( EXIT_FAILURE );
483 case 'v': /* verbose mode */
486 case 'w': /* password */
487 passwd.bv_val = strdup( optarg );
491 for( p = optarg; *p != '\0'; p++ ) {
495 passwd.bv_len = strlen( passwd.bv_val );
504 #ifdef HAVE_CYRUS_SASL
505 if( sasl_mech != NULL ) {
506 fprintf( stderr, "%s: -Y previously specified\n", prog );
509 if( version == LDAP_VERSION2 ) {
510 fprintf( stderr, "%s: -Y incompatible with version %d\n",
514 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
515 fprintf( stderr, "%s: incompatible with authentication choice\n", prog );
518 authmethod = LDAP_AUTH_SASL;
519 version = LDAP_VERSION3;
520 sasl_mech = strdup( optarg );
522 fprintf( stderr, "%s: not compiled with SASL support\n",
524 return( EXIT_FAILURE );
528 if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
529 fprintf( stderr, "%s: incompatible with previous "
530 "authentication choice\n", prog );
533 authmethod = LDAP_AUTH_SIMPLE;
536 #ifdef HAVE_CYRUS_SASL
537 if( sasl_authz_id != NULL ) {
538 fprintf( stderr, "%s: -X previously specified\n", prog );
541 if( version == LDAP_VERSION2 ) {
542 fprintf( stderr, "%s: -X incompatible with LDAPv%d\n",
546 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
547 fprintf( stderr, "%s: -X incompatible with "
548 "authentication choice\n", prog );
551 authmethod = LDAP_AUTH_SASL;
552 version = LDAP_VERSION3;
553 sasl_authz_id = strdup( optarg );
555 fprintf( stderr, "%s: not compiled with SASL support\n",
557 return( EXIT_FAILURE );
562 if( version == LDAP_VERSION2 ) {
563 fprintf( stderr, "%s: -Z incompatible with version %d\n",
567 version = LDAP_VERSION3;
570 fprintf( stderr, "%s: not compiled with TLS support\n",
572 return( EXIT_FAILURE );
576 fprintf( stderr, "%s: unrecognized option -%c\n",
579 return( EXIT_FAILURE );
584 version = LDAP_VERSION3;
586 if (authmethod == -1 && version > LDAP_VERSION2) {
587 #ifdef HAVE_CYRUS_SASL
588 authmethod = LDAP_AUTH_SASL;
590 authmethod = LDAP_AUTH_SIMPLE;
595 if ( optind >= argc ) {
601 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
602 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
604 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
605 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
610 (void) SIGNAL( SIGPIPE, SIG_IGN );
613 if( ( ldaphost != NULL || ldapport ) && ( ldapuri == NULL ) ) {
615 fprintf( stderr, "ldap_init( %s, %d )\n",
616 ldaphost != NULL ? ldaphost : "<DEFAULT>",
620 ld = ldap_init( ldaphost, ldapport );
622 perror("ldapdelete: ldap_init");
628 fprintf( stderr, "ldap_initialize( %s )\n",
629 ldapuri != NULL ? ldapuri : "<DEFAULT>" );
632 rc = ldap_initialize( &ld, ldapuri );
633 if( rc != LDAP_SUCCESS ) {
634 fprintf( stderr, "Could not create LDAP session handle (%d): %s\n",
635 rc, ldap_err2string(rc) );
641 /* this seems prudent for searches below */
642 int deref = LDAP_DEREF_NEVER;
643 ldap_set_option( ld, LDAP_OPT_DEREF, &deref );
646 /* chase referrals */
647 if( ldap_set_option( ld, LDAP_OPT_REFERRALS,
648 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
650 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
651 referrals ? "on" : "off" );
655 if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version )
656 != LDAP_OPT_SUCCESS )
658 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
663 if ( use_tls && ( ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS )) {
664 ldap_perror( ld, "ldap_start_tls" );
670 if ( pw_file || want_bindpw ) {
672 rc = lutil_get_filed_password( pw_file, &passwd );
673 if( rc ) return EXIT_FAILURE;
675 passwd.bv_val = getpassphrase( "Enter LDAP Password: " );
676 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
680 if ( authmethod == LDAP_AUTH_SASL ) {
681 #ifdef HAVE_CYRUS_SASL
684 if( sasl_secprops != NULL ) {
685 rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
686 (void *) sasl_secprops );
688 if( rc != LDAP_OPT_SUCCESS ) {
690 "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
692 return( EXIT_FAILURE );
696 defaults = lutil_sasl_defaults( ld,
703 rc = ldap_sasl_interactive_bind_s( ld, binddn,
704 sasl_mech, NULL, NULL,
705 sasl_flags, lutil_sasl_interact, defaults );
707 if( rc != LDAP_SUCCESS ) {
708 ldap_perror( ld, "ldap_sasl_interactive_bind_s" );
709 return( EXIT_FAILURE );
712 fprintf( stderr, "%s: not compiled with SASL support\n",
714 return( EXIT_FAILURE );
718 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
720 ldap_perror( ld, "ldap_bind" );
721 return( EXIT_FAILURE );
725 if ( authzid || manageDSAit || noop ) {
726 int err, crit=0, i=0;
728 LDAPControl *ctrls[4];
731 c[i].ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
732 c[i].ldctl_value.bv_val = authzid;
733 c[i].ldctl_value.bv_len = strlen( authzid );
734 c[i].ldctl_iscritical = 1;
736 if( c[i].ldctl_iscritical ) crit++;
742 c[i].ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
743 c[i].ldctl_value.bv_val = NULL;
744 c[i].ldctl_value.bv_len = 0;
745 c[i].ldctl_iscritical = manageDSAit > 1;
747 if( c[i].ldctl_iscritical ) crit++;
753 c[i].ldctl_oid = LDAP_CONTROL_NOOP;
754 c[i].ldctl_value.bv_val = NULL;
755 c[i].ldctl_value.bv_len = 0;
756 c[i].ldctl_iscritical = noop > 1;
758 if( c[i].ldctl_iscritical ) crit++;
763 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, ctrls );
765 if( err != LDAP_OPT_SUCCESS ) {
766 fprintf( stderr, "Could not set %scontrols\n",
767 crit ? "critical " : "" );
777 for ( ; optind < argc; ++optind ) {
778 rc = dodelete( ld, argv[ optind ] );
780 /* Stop on error and no -c option */
781 if( rc != 0 && contoper == 0) break;
784 while ((rc == 0 || contoper) && fgets(buf, sizeof(buf), fp) != NULL) {
785 buf[ strlen( buf ) - 1 ] = '\0'; /* remove trailing newline */
787 if ( *buf != '\0' ) {
788 rc = dodelete( ld, buf );
805 char *matcheddn = NULL, *text = NULL, **refs = NULL;
809 printf( "%sdeleting entry \"%s\"\n",
810 (not ? "!" : ""), dn );
817 /* If prune is on, remove a whole subtree. Delete the children of the
818 * DN recursively, then the DN requested.
820 if ( prune ) deletechildren( ld, dn );
822 rc = ldap_delete_ext( ld, dn, NULL, NULL, &id );
823 if ( rc != LDAP_SUCCESS ) {
824 fprintf( stderr, "%s: ldap_delete_ext: %s (%d)\n",
825 prog, ldap_err2string( rc ), rc );
829 rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, NULL, &res );
831 ldap_perror( ld, "ldapdelete: ldap_result" );
835 rc = ldap_parse_result( ld, res, &code, &matcheddn, &text, &refs, NULL, 1 );
837 if( rc != LDAP_SUCCESS ) {
838 fprintf( stderr, "%s: ldap_parse_result: %s (%d)\n",
839 prog, ldap_err2string( rc ), rc );
843 if( verbose || code != LDAP_SUCCESS ||
844 (matcheddn && *matcheddn) || (text && *text) || (refs && *refs) )
846 printf( "Delete Result: %s (%d)\n", ldap_err2string( code ), code );
848 if( text && *text ) {
849 printf( "Additional info: %s\n", text );
852 if( matcheddn && *matcheddn ) {
853 printf( "Matched DN: %s\n", matcheddn );
858 for( i=0; refs[i]; i++ ) {
859 printf("Referral: %s\n", refs[i] );
865 ber_memfree( matcheddn );
866 ber_memvfree( (void **) refs );
872 * Delete all the children of an entry recursively until leaf nodes are reached.
875 static int deletechildren(
879 LDAPMessage *res, *e;
882 static char *attrs[] = { "1.1", NULL };
884 if ( verbose ) printf ( "deleting children of: %s\n", dn );
886 * Do a one level search at dn for children. For each, delete its children.
889 rc = ldap_search_ext_s( ld, dn, LDAP_SCOPE_ONELEVEL, NULL, attrs, 1,
890 NULL, NULL, NULL, -1, &res );
891 if ( rc != LDAP_SUCCESS ) {
892 ldap_perror( ld, "ldap_search" );
896 entries = ldap_count_entries( ld, res );
901 for (e = ldap_first_entry( ld, res ), i = 0; e != NULL;
902 e = ldap_next_entry( ld, e ), i++ )
904 char *dn = ldap_get_dn( ld, e );
907 ldap_perror( ld, "ldap_prune" );
908 ldap_get_option( ld, LDAP_OPT_ERROR_NUMBER, &rc );
913 rc = deletechildren( ld, dn );
915 ldap_perror( ld, "ldap_prune" );
921 printf( "\tremoving %s\n", dn );
924 rc = ldap_delete_s( ld, dn );
926 ldap_perror( ld, "ldap_delete" );
933 printf( "\t%s removed\n", dn );