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\n");
205 if( cvalue == NULL ) {
206 fprintf( stderr, "authzid: control value expected\n" );
211 fprintf( stderr, "authzid: must be marked critical\n" );
216 assert( authzid == NULL );
219 } else if ( strcasecmp( control, "manageDSAit" ) == 0 ) {
221 fprintf( stderr, "manageDSAit control previously specified\n");
224 if( cvalue != NULL ) {
225 fprintf( stderr, "manageDSAit: no control value expected\n" );
230 manageDSAit = 1 + crit;
232 } else if ( strcasecmp( control, "noop" ) == 0 ) {
234 fprintf( stderr, "noop control previously specified\n");
237 if( cvalue != NULL ) {
238 fprintf( stderr, "noop: no control value expected\n" );
246 fprintf( stderr, "Invalid general control name: %s\n", control );
251 case 'h': /* ldap host */
252 if( ldapuri != NULL ) {
253 fprintf( stderr, "%s: -h incompatible with -H\n", prog );
256 if( ldaphost != NULL ) {
257 fprintf( stderr, "%s: -h previously specified\n", prog );
260 ldaphost = strdup( optarg );
262 case 'H': /* ldap URI */
263 if( ldaphost != NULL ) {
264 fprintf( stderr, "%s: -H incompatible with -h\n", prog );
268 fprintf( stderr, "%s: -H incompatible with -p\n", prog );
271 if( ldapuri != NULL ) {
272 fprintf( stderr, "%s: -H previously specified\n", prog );
275 ldapuri = strdup( optarg );
278 #ifdef HAVE_CYRUS_SASL
279 if( version == LDAP_VERSION2 ) {
280 fprintf( stderr, "%s: -I incompatible with version %d\n",
284 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
285 fprintf( stderr, "%s: incompatible previous "
286 "authentication choice\n",
290 authmethod = LDAP_AUTH_SASL;
291 version = LDAP_VERSION3;
292 sasl_flags = LDAP_SASL_INTERACTIVE;
295 fprintf( stderr, "%s: was not compiled with SASL support\n",
297 return( EXIT_FAILURE );
299 case 'k': /* kerberos bind */
300 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
301 if( version > LDAP_VERSION2 ) {
302 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
307 if( authmethod != -1 ) {
308 fprintf( stderr, "%s: -k incompatible with previous "
309 "authentication choice\n", prog );
313 authmethod = LDAP_AUTH_KRBV4;
315 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
319 case 'K': /* kerberos bind, part one only */
320 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
321 if( version > LDAP_VERSION2 ) {
322 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
326 if( authmethod != -1 ) {
327 fprintf( stderr, "%s: incompatible with previous "
328 "authentication choice\n", prog );
332 authmethod = LDAP_AUTH_KRBV41;
334 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
335 return( EXIT_FAILURE );
339 /* enable Manage DSA IT */
340 if( version == LDAP_VERSION2 ) {
341 fprintf( stderr, "%s: -M incompatible with LDAPv%d\n",
346 version = LDAP_VERSION3;
348 case 'n': /* print deletes, don't actually do them */
352 #ifdef HAVE_CYRUS_SASL
353 if( sasl_secprops != NULL ) {
354 fprintf( stderr, "%s: -O previously specified\n", prog );
357 if( version == LDAP_VERSION2 ) {
358 fprintf( stderr, "%s: -O incompatible with LDAPv%d\n",
362 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
363 fprintf( stderr, "%s: incompatible previous "
364 "authentication choice\n", prog );
367 authmethod = LDAP_AUTH_SASL;
368 version = LDAP_VERSION3;
369 sasl_secprops = strdup( optarg );
371 fprintf( stderr, "%s: not compiled with SASL support\n",
373 return( EXIT_FAILURE );
378 fprintf( stderr, "%s: -p previously specified\n", prog );
381 ldapport = atoi( optarg );
384 switch( atoi(optarg) ) {
386 if( version == LDAP_VERSION3 ) {
387 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
391 version = LDAP_VERSION2;
394 if( version == LDAP_VERSION2 ) {
395 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
399 version = LDAP_VERSION3;
402 fprintf( stderr, "%s: protocol version should be 2 or 3\n",
405 return( EXIT_FAILURE );
409 #ifdef HAVE_CYRUS_SASL
410 if( version == LDAP_VERSION2 ) {
411 fprintf( stderr, "%s: -Q incompatible with version %d\n",
415 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
416 fprintf( stderr, "%s: incompatible previous "
417 "authentication choice\n",
421 authmethod = LDAP_AUTH_SASL;
422 version = LDAP_VERSION3;
423 sasl_flags = LDAP_SASL_QUIET;
426 fprintf( stderr, "%s: not compiled with SASL support\n",
428 return( EXIT_FAILURE );
431 #ifdef HAVE_CYRUS_SASL
432 if( sasl_realm != NULL ) {
433 fprintf( stderr, "%s: -R previously specified\n", prog );
436 if( version == LDAP_VERSION2 ) {
437 fprintf( stderr, "%s: -R incompatible with version %d\n",
441 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
442 fprintf( stderr, "%s: incompatible previous "
443 "authentication choice\n",
447 authmethod = LDAP_AUTH_SASL;
448 version = LDAP_VERSION3;
449 sasl_realm = strdup( optarg );
451 fprintf( stderr, "%s: not compiled with SASL support\n",
453 return( EXIT_FAILURE );
457 #ifdef HAVE_CYRUS_SASL
458 if( sasl_authc_id != NULL ) {
459 fprintf( stderr, "%s: -U previously specified\n", prog );
462 if( version == LDAP_VERSION2 ) {
463 fprintf( stderr, "%s: -U incompatible with version %d\n",
467 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
468 fprintf( stderr, "%s: incompatible previous "
469 "authentication choice\n",
473 authmethod = LDAP_AUTH_SASL;
474 version = LDAP_VERSION3;
475 sasl_authc_id = strdup( optarg );
477 fprintf( stderr, "%s: not compiled with SASL support\n",
479 return( EXIT_FAILURE );
482 case 'v': /* verbose mode */
485 case 'w': /* password */
486 passwd.bv_val = strdup( optarg );
490 for( p = optarg; *p != '\0'; p++ ) {
494 passwd.bv_len = strlen( passwd.bv_val );
503 #ifdef HAVE_CYRUS_SASL
504 if( sasl_mech != NULL ) {
505 fprintf( stderr, "%s: -Y previously specified\n", prog );
508 if( version == LDAP_VERSION2 ) {
509 fprintf( stderr, "%s: -Y incompatible with version %d\n",
513 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
514 fprintf( stderr, "%s: incompatible with authentication choice\n", prog );
517 authmethod = LDAP_AUTH_SASL;
518 version = LDAP_VERSION3;
519 sasl_mech = strdup( optarg );
521 fprintf( stderr, "%s: not compiled with SASL support\n",
523 return( EXIT_FAILURE );
527 if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
528 fprintf( stderr, "%s: incompatible with previous "
529 "authentication choice\n", prog );
532 authmethod = LDAP_AUTH_SIMPLE;
535 #ifdef HAVE_CYRUS_SASL
536 if( sasl_authz_id != NULL ) {
537 fprintf( stderr, "%s: -X previously specified\n", prog );
540 if( version == LDAP_VERSION2 ) {
541 fprintf( stderr, "%s: -X incompatible with LDAPv%d\n",
545 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
546 fprintf( stderr, "%s: -X incompatible with "
547 "authentication choice\n", prog );
550 authmethod = LDAP_AUTH_SASL;
551 version = LDAP_VERSION3;
552 sasl_authz_id = strdup( optarg );
554 fprintf( stderr, "%s: not compiled with SASL support\n",
556 return( EXIT_FAILURE );
561 if( version == LDAP_VERSION2 ) {
562 fprintf( stderr, "%s: -Z incompatible with version %d\n",
566 version = LDAP_VERSION3;
569 fprintf( stderr, "%s: not compiled with TLS support\n",
571 return( EXIT_FAILURE );
575 fprintf( stderr, "%s: unrecognized option -%c\n",
578 return( EXIT_FAILURE );
583 version = LDAP_VERSION3;
585 if (authmethod == -1 && version > LDAP_VERSION2) {
586 #ifdef HAVE_CYRUS_SASL
587 authmethod = LDAP_AUTH_SASL;
589 authmethod = LDAP_AUTH_SIMPLE;
594 if ( optind >= argc ) {
600 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
601 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
603 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
604 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
609 (void) SIGNAL( SIGPIPE, SIG_IGN );
612 if( ( ldaphost != NULL || ldapport ) && ( ldapuri == NULL ) ) {
614 fprintf( stderr, "ldap_init( %s, %d )\n",
615 ldaphost != NULL ? ldaphost : "<DEFAULT>",
619 ld = ldap_init( ldaphost, ldapport );
621 perror("ldapdelete: ldap_init");
627 fprintf( stderr, "ldap_initialize( %s )\n",
628 ldapuri != NULL ? ldapuri : "<DEFAULT>" );
631 rc = ldap_initialize( &ld, ldapuri );
632 if( rc != LDAP_SUCCESS ) {
633 fprintf( stderr, "Could not create LDAP session handle (%d): %s\n",
634 rc, ldap_err2string(rc) );
640 /* this seems prudent for searches below */
641 int deref = LDAP_DEREF_NEVER;
642 ldap_set_option( ld, LDAP_OPT_DEREF, &deref );
645 /* chase referrals */
646 if( ldap_set_option( ld, LDAP_OPT_REFERRALS,
647 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
649 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
650 referrals ? "on" : "off" );
654 if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version )
655 != LDAP_OPT_SUCCESS )
657 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
662 if ( use_tls && ( ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS )) {
663 ldap_perror( ld, "ldap_start_tls" );
669 if ( pw_file || want_bindpw ) {
671 rc = lutil_get_filed_password( pw_file, &passwd );
672 if( rc ) return EXIT_FAILURE;
674 passwd.bv_val = getpassphrase( "Enter LDAP Password: " );
675 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
679 if ( authmethod == LDAP_AUTH_SASL ) {
680 #ifdef HAVE_CYRUS_SASL
683 if( sasl_secprops != NULL ) {
684 rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
685 (void *) sasl_secprops );
687 if( rc != LDAP_OPT_SUCCESS ) {
689 "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
691 return( EXIT_FAILURE );
695 defaults = lutil_sasl_defaults( ld,
702 rc = ldap_sasl_interactive_bind_s( ld, binddn,
703 sasl_mech, NULL, NULL,
704 sasl_flags, lutil_sasl_interact, defaults );
706 if( rc != LDAP_SUCCESS ) {
707 ldap_perror( ld, "ldap_sasl_interactive_bind_s" );
708 return( EXIT_FAILURE );
711 fprintf( stderr, "%s: not compiled with SASL support\n",
713 return( EXIT_FAILURE );
717 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
719 ldap_perror( ld, "ldap_bind" );
720 return( EXIT_FAILURE );
724 if ( authzid || manageDSAit || noop ) {
725 int err, crit=0, i=0;
727 LDAPControl *ctrls[4];
730 c[i].ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
731 c[i].ldctl_value.bv_val = authzid;
732 c[i].ldctl_value.bv_len = strlen( authzid );
733 c[i].ldctl_iscritical = 1;
735 if( c[i].ldctl_iscritical ) crit++;
741 c[i].ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
742 c[i].ldctl_value.bv_val = NULL;
743 c[i].ldctl_value.bv_len = 0;
744 c[i].ldctl_iscritical = manageDSAit > 1;
746 if( c[i].ldctl_iscritical ) crit++;
752 c[i].ldctl_oid = LDAP_CONTROL_NOOP;
753 c[i].ldctl_value.bv_val = NULL;
754 c[i].ldctl_value.bv_len = 0;
755 c[i].ldctl_iscritical = noop > 1;
757 if( c[i].ldctl_iscritical ) crit++;
762 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, ctrls );
764 if( err != LDAP_OPT_SUCCESS ) {
765 fprintf( stderr, "Could not set %scontrols\n",
766 crit ? "critical " : "" );
776 for ( ; optind < argc; ++optind ) {
777 rc = dodelete( ld, argv[ optind ] );
779 /* Stop on error and no -c option */
780 if( rc != 0 && contoper == 0) break;
783 while ((rc == 0 || contoper) && fgets(buf, sizeof(buf), fp) != NULL) {
784 buf[ strlen( buf ) - 1 ] = '\0'; /* remove trailing newline */
786 if ( *buf != '\0' ) {
787 rc = dodelete( ld, buf );
804 char *matcheddn = NULL, *text = NULL, **refs = NULL;
808 printf( "%sdeleting entry \"%s\"\n",
809 (not ? "!" : ""), dn );
816 /* If prune is on, remove a whole subtree. Delete the children of the
817 * DN recursively, then the DN requested.
819 if ( prune ) deletechildren( ld, dn );
821 rc = ldap_delete_ext( ld, dn, NULL, NULL, &id );
822 if ( rc != LDAP_SUCCESS ) {
823 fprintf( stderr, "%s: ldap_delete_ext: %s (%d)\n",
824 prog, ldap_err2string( rc ), rc );
828 rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, NULL, &res );
830 ldap_perror( ld, "ldapdelete: ldap_result" );
834 rc = ldap_parse_result( ld, res, &code, &matcheddn, &text, &refs, NULL, 1 );
836 if( rc != LDAP_SUCCESS ) {
837 fprintf( stderr, "%s: ldap_parse_result: %s (%d)\n",
838 prog, ldap_err2string( rc ), rc );
842 if( verbose || code != LDAP_SUCCESS ||
843 (matcheddn && *matcheddn) || (text && *text) || (refs && *refs) )
845 printf( "Delete Result: %s (%d)\n", ldap_err2string( code ), code );
847 if( text && *text ) {
848 printf( "Additional info: %s\n", text );
851 if( matcheddn && *matcheddn ) {
852 printf( "Matched DN: %s\n", matcheddn );
857 for( i=0; refs[i]; i++ ) {
858 printf("Referral: %s\n", refs[i] );
864 ber_memfree( matcheddn );
865 ber_memvfree( (void **) refs );
871 * Delete all the children of an entry recursively until leaf nodes are reached.
874 static int deletechildren(
878 LDAPMessage *res, *e;
881 static char *attrs[] = { "1.1", NULL };
883 if ( verbose ) printf ( "deleting children of: %s\n", dn );
885 * Do a one level search at dn for children. For each, delete its children.
888 rc = ldap_search_ext_s( ld, dn, LDAP_SCOPE_ONELEVEL, NULL, attrs, 1,
889 NULL, NULL, NULL, -1, &res );
890 if ( rc != LDAP_SUCCESS ) {
891 ldap_perror( ld, "ldap_search" );
895 entries = ldap_count_entries( ld, res );
900 for (e = ldap_first_entry( ld, res ), i = 0; e != NULL;
901 e = ldap_next_entry( ld, e ), i++ )
903 char *dn = ldap_get_dn( ld, e );
906 ldap_perror( ld, "ldap_prune" );
907 ldap_get_option( ld, LDAP_OPT_ERROR_NUMBER, &rc );
912 rc = deletechildren( ld, dn );
914 ldap_perror( ld, "ldap_prune" );
920 printf( "\tremoving %s\n", dn );
923 rc = ldap_delete_s( ld, dn );
925 ldap_perror( ld, "ldap_delete" );
932 printf( "\t%s removed\n", dn );