1 /* ldapdelete.c - simple program to delete an entry using LDAP */
4 * Copyright 1998-2000 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>
20 #include "lutil_ldap.h"
21 #include "ldap_defaults.h"
24 static char *binddn = NULL;
25 static struct berval passwd = { 0, NULL };
26 static char *ldaphost = NULL;
27 static int ldapport = 0;
29 #ifdef HAVE_CYRUS_SASL
30 static unsigned sasl_flags = LDAP_SASL_AUTOMATIC;
31 static char *sasl_mech = NULL;
32 static char *sasl_realm = NULL;
33 static char *sasl_authc_id = NULL;
34 static char *sasl_authz_id = NULL;
35 static char *sasl_secprops = NULL;
37 static int use_tls = 0;
38 static int not, verbose, contoper;
41 static int dodelete LDAP_P((
45 static int deletechildren LDAP_P((
50 usage( const char *s )
53 "Delete entries from an LDAP server\n\n"
54 "usage: %s [options] [dn]...\n"
55 " dn: list of DNs to delete. If not given, it will be readed from stdin\n"
56 " or from the file specified with \"-f file\".\n"
58 " -r delete recursively\n"
61 " -d level set LDAP debugging level to `level'\n"
62 " -D binddn bind DN\n"
63 " -f file read operations from `file'\n"
64 " -h host LDAP server\n"
65 " -I use SASL Interactive mode\n"
66 " -k use Kerberos authentication\n"
67 " -K like -k, but do only step 1 of the Kerberos bind\n"
68 " -M enable Manage DSA IT control (-MM to make critical)\n"
69 " -n show what would be done but don't actually search\n"
70 " -O props SASL security properties\n"
71 " -p port port on LDAP server\n"
72 " -P version procotol version (default: 3)\n"
73 " -Q use SASL Quiet mode\n"
74 " -R realm SASL realm\n"
75 " -U user SASL authentication identity (username)\n"
76 " -v run in verbose mode (diagnostics to standard output)\n"
77 " -w passwd bind passwd (for simple authentication)\n"
78 " -W prompt for bind passwd\n"
79 " -x Simple authentication\n"
80 " -X id SASL authorization identity (\"dn:<dn>\" or \"u:<user>\")\n"
81 " -Y mech SASL mechanism\n"
82 " -Z Start TLS request (-ZZ to require successful response)\n"
90 main( int argc, char **argv )
94 int i, rc, authmethod, referrals, want_bindpw, version, debug, manageDSAit;
96 not = verbose = contoper = want_bindpw = debug = manageDSAit = referrals = 0;
101 prog = (prog = strrchr(argv[0], *LDAP_DIRSEP)) == NULL ? argv[0] : ++prog;
103 while (( i = getopt( argc, argv, "cf:r" "Cd:D:h:IkKMnO:p:P:QRU:vw:WxX:Y:Z" )) != EOF ) {
105 /* Delete Specific Options */
106 case 'c': /* continuous operation mode */
109 case 'f': /* read DNs from a file */
111 fprintf( stderr, "%s: -f previously specified\n" );
114 if (( fp = fopen( optarg, "r" )) == NULL ) {
116 exit( EXIT_FAILURE );
128 debug |= atoi( optarg );
130 case 'D': /* bind DN */
131 if( binddn != NULL ) {
132 fprintf( stderr, "%s: -D previously specified\n" );
135 binddn = strdup( optarg );
137 case 'h': /* ldap host */
138 if( ldaphost != NULL ) {
139 fprintf( stderr, "%s: -h previously specified\n" );
142 ldaphost = strdup( optarg );
145 #ifdef HAVE_CYRUS_SASL
146 if( version == LDAP_VERSION2 ) {
147 fprintf( stderr, "%s: -I incompatible with version %d\n",
151 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
152 fprintf( stderr, "%s: incompatible previous "
153 "authentication choice\n",
157 authmethod = LDAP_AUTH_SASL;
158 version = LDAP_VERSION3;
159 sasl_flags = LDAP_SASL_INTERACTIVE;
162 fprintf( stderr, "%s: was not compiled with SASL support\n",
164 return( EXIT_FAILURE );
166 case 'k': /* kerberos bind */
167 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
168 if( version > LDAP_VERSION2 ) {
169 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
174 if( authmethod != -1 ) {
175 fprintf( stderr, "%s: -k incompatible with previous "
176 "authentication choice\n", prog );
180 authmethod = LDAP_AUTH_KRBV4;
182 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
186 case 'K': /* kerberos bind, part one only */
187 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
188 if( version > LDAP_VERSION2 ) {
189 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
193 if( authmethod != -1 ) {
194 fprintf( stderr, "%s: incompatible with previous "
195 "authentication choice\n", prog );
199 authmethod = LDAP_AUTH_KRBV41;
201 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
202 return( EXIT_FAILURE );
206 /* enable Manage DSA IT */
207 if( version == LDAP_VERSION2 ) {
208 fprintf( stderr, "%s: -M incompatible with LDAPv%d\n",
213 version = LDAP_VERSION3;
215 case 'n': /* print deletes, don't actually do them */
219 #ifdef HAVE_CYRUS_SASL
220 if( sasl_secprops != NULL ) {
221 fprintf( stderr, "%s: -O previously specified\n" );
224 if( version == LDAP_VERSION2 ) {
225 fprintf( stderr, "%s: -O incompatible with LDAPv%d\n",
229 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
230 fprintf( stderr, "%s: incompatible previous "
231 "authentication choice\n", prog );
234 authmethod = LDAP_AUTH_SASL;
235 version = LDAP_VERSION3;
236 sasl_secprops = strdup( optarg );
238 fprintf( stderr, "%s: not compiled with SASL support\n",
240 return( EXIT_FAILURE );
245 fprintf( stderr, "%s: -p previously specified\n" );
248 ldapport = atoi( optarg );
251 switch( atoi(optarg) ) {
253 if( version == LDAP_VERSION3 ) {
254 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
258 version = LDAP_VERSION2;
261 if( version == LDAP_VERSION2 ) {
262 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
266 version = LDAP_VERSION3;
269 fprintf( stderr, "%s: protocol version should be 2 or 3\n",
272 return( EXIT_FAILURE );
275 #ifdef HAVE_CYRUS_SASL
276 if( version == LDAP_VERSION2 ) {
277 fprintf( stderr, "%s: -Q incompatible with version %d\n",
281 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
282 fprintf( stderr, "%s: incompatible previous "
283 "authentication choice\n",
287 authmethod = LDAP_AUTH_SASL;
288 version = LDAP_VERSION3;
289 sasl_flags = LDAP_SASL_QUIET;
292 fprintf( stderr, "%s: not compiled with SASL support\n",
294 return( EXIT_FAILURE );
297 #ifdef HAVE_CYRUS_SASL
298 if( sasl_realm != NULL ) {
299 fprintf( stderr, "%s: -R previously specified\n" );
302 if( version == LDAP_VERSION2 ) {
303 fprintf( stderr, "%s: -R incompatible with version %d\n",
307 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
308 fprintf( stderr, "%s: incompatible previous "
309 "authentication choice\n",
313 authmethod = LDAP_AUTH_SASL;
314 version = LDAP_VERSION3;
315 sasl_realm = strdup( optarg );
317 fprintf( stderr, "%s: not compiled with SASL support\n",
319 return( EXIT_FAILURE );
323 #ifdef HAVE_CYRUS_SASL
324 if( sasl_authc_id != NULL ) {
325 fprintf( stderr, "%s: -U previously specified\n" );
328 if( version == LDAP_VERSION2 ) {
329 fprintf( stderr, "%s: -U incompatible with version %d\n",
333 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
334 fprintf( stderr, "%s: incompatible previous "
335 "authentication choice\n",
339 authmethod = LDAP_AUTH_SASL;
340 version = LDAP_VERSION3;
341 sasl_authc_id = strdup( optarg );
343 fprintf( stderr, "%s: not compiled with SASL support\n",
345 return( EXIT_FAILURE );
348 case 'v': /* verbose mode */
351 case 'w': /* password */
352 passwd.bv_val = strdup( optarg );
356 for( p = optarg; *p == '\0'; p++ ) {
360 passwd.bv_len = strlen( passwd.bv_val );
366 #ifdef HAVE_CYRUS_SASL
367 if( sasl_mech != NULL ) {
368 fprintf( stderr, "%s: -Y previously specified\n" );
371 if( version == LDAP_VERSION2 ) {
372 fprintf( stderr, "%s: -Y incompatible with version %d\n",
376 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
377 fprintf( stderr, "%s: incompatible with authentication choice\n", prog );
380 authmethod = LDAP_AUTH_SASL;
381 version = LDAP_VERSION3;
382 sasl_mech = strdup( optarg );
384 fprintf( stderr, "%s: not compiled with SASL support\n",
386 return( EXIT_FAILURE );
390 if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
391 fprintf( stderr, "%s: incompatible with previous "
392 "authentication choice\n", prog );
395 authmethod = LDAP_AUTH_SIMPLE;
398 #ifdef HAVE_CYRUS_SASL
399 if( sasl_authz_id != NULL ) {
400 fprintf( stderr, "%s: -X previously specified\n" );
403 if( version == LDAP_VERSION2 ) {
404 fprintf( stderr, "%s: -X incompatible with LDAPv%d\n",
408 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
409 fprintf( stderr, "%s: -X incompatible with "
410 "authentication choice\n", prog );
413 authmethod = LDAP_AUTH_SASL;
414 version = LDAP_VERSION3;
415 sasl_authz_id = strdup( optarg );
417 fprintf( stderr, "%s: not compiled with SASL support\n",
419 return( EXIT_FAILURE );
424 if( version == LDAP_VERSION2 ) {
425 fprintf( stderr, "%s: -Z incompatible with version %d\n",
429 version = LDAP_VERSION3;
432 fprintf( stderr, "%s: not compiled with TLS support\n",
434 return( EXIT_FAILURE );
438 fprintf( stderr, "%s: unrecongized option -%c\n",
441 return( EXIT_FAILURE );
446 version = LDAP_VERSION3;
448 if (authmethod == -1 && version > LDAP_VERSION2) {
449 #ifdef HAVE_CYRUS_SASL
450 authmethod = LDAP_AUTH_SASL;
452 authmethod = LDAP_AUTH_SIMPLE;
457 if ( optind >= argc ) {
463 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
464 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
466 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
467 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
472 (void) SIGNAL( SIGPIPE, SIG_IGN );
475 if (( ld = ldap_init( ldaphost, ldapport )) == NULL ) {
476 perror( "ldap_init" );
477 return( EXIT_FAILURE );
481 /* this seems prudent for searches below */
482 int deref = LDAP_DEREF_NEVER;
483 ldap_set_option( ld, LDAP_OPT_DEREF, &deref );
486 /* chase referrals */
487 if( ldap_set_option( ld, LDAP_OPT_REFERRALS,
488 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
490 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
491 referrals ? "on" : "off" );
495 if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version )
496 != LDAP_OPT_SUCCESS )
498 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
503 if ( use_tls && ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS ) {
505 ldap_perror( ld, "ldap_start_tls" );
508 fprintf( stderr, "WARNING: could not start TLS\n" );
512 passwd.bv_val = getpassphrase("Enter LDAP Password: ");
513 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
516 if ( authmethod == LDAP_AUTH_SASL ) {
517 #ifdef HAVE_CYRUS_SASL
520 if( sasl_secprops != NULL ) {
521 rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
522 (void *) sasl_secprops );
524 if( rc != LDAP_OPT_SUCCESS ) {
526 "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
528 return( EXIT_FAILURE );
532 defaults = lutil_sasl_defaults( ld,
539 rc = ldap_sasl_interactive_bind_s( ld, binddn,
540 sasl_mech, NULL, NULL,
541 sasl_flags, lutil_sasl_interact, defaults );
543 if( rc != LDAP_SUCCESS ) {
544 ldap_perror( ld, "ldap_sasl_interactive_bind_s" );
545 return( EXIT_FAILURE );
548 fprintf( stderr, "%s: not compiled with SASL support\n",
550 return( EXIT_FAILURE );
554 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
556 ldap_perror( ld, "ldap_bind" );
557 return( EXIT_FAILURE );
564 LDAPControl *ctrls[2];
568 c.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
569 c.ldctl_value.bv_val = NULL;
570 c.ldctl_value.bv_len = 0;
571 c.ldctl_iscritical = manageDSAit > 1;
573 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, &ctrls );
575 if( err != LDAP_OPT_SUCCESS ) {
576 fprintf( stderr, "Could not set ManageDSAit %scontrol\n",
577 c.ldctl_iscritical ? "critical " : "" );
578 if( c.ldctl_iscritical ) {
579 exit( EXIT_FAILURE );
586 for ( ; optind < argc; ++optind ) {
587 rc = dodelete( ld, argv[ optind ] );
590 while ((rc == 0 || contoper) && fgets(buf, sizeof(buf), fp) != NULL) {
591 buf[ strlen( buf ) - 1 ] = '\0'; /* remove trailing newline */
592 if ( *buf != '\0' ) {
593 rc = dodelete( ld, buf );
610 char *matcheddn = NULL, *text = NULL, **refs = NULL;
614 printf( "%sdeleting entry \"%s\"\n",
615 (not ? "!" : ""), dn );
622 /* If prune is on, remove a whole subtree. Delete the children of the
623 * DN recursively, then the DN requested.
625 if ( prune ) deletechildren( ld, dn );
627 rc = ldap_delete_ext( ld, dn, NULL, NULL, &id );
628 if ( rc != LDAP_SUCCESS ) {
629 fprintf( stderr, "%s: ldap_delete_ext: %s (%d)\n",
630 prog, ldap_err2string( rc ), rc );
634 rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, NULL, &res );
636 ldap_perror( ld, "ldapdelete: ldap_result" );
640 rc = ldap_parse_result( ld, res, &code, &matcheddn, &text, &refs, NULL, 1 );
642 if( rc != LDAP_SUCCESS ) {
643 fprintf( stderr, "%s: ldap_parse_result: %s (%d)\n",
644 prog, ldap_err2string( rc ), rc );
648 if( verbose || code != LDAP_SUCCESS ||
649 (matcheddn && *matcheddn) || (text && *text) || (refs && *refs) )
651 printf( "Delete Result: %s (%d)\n", ldap_err2string( code ), code );
653 if( text && *text ) {
654 printf( "Additional info: %s\n", text );
657 if( matcheddn && *matcheddn ) {
658 printf( "Matched DN: %s\n", matcheddn );
663 for( i=0; refs[i]; i++ ) {
664 printf("Referral: %s\n", refs[i] );
670 ber_memfree( matcheddn );
671 ber_memvfree( (void **) refs );
677 * Delete all the children of an entry recursively until leaf nodes are reached.
680 static int deletechildren(
684 LDAPMessage *res, *e;
687 static char *attrs[] = { "1.1", NULL };
689 if ( verbose ) printf ( "deleting children of: %s\n", dn );
691 * Do a one level search at dn for children. For each, delete its children.
694 rc = ldap_search_ext_s( ld, dn, LDAP_SCOPE_ONELEVEL, NULL, attrs, 1,
695 NULL, NULL, NULL, -1, &res );
696 if ( rc != LDAP_SUCCESS ) {
697 ldap_perror( ld, "ldap_search" );
701 entries = ldap_count_entries( ld, res );
706 for (e = ldap_first_entry( ld, res ), i = 0; e != NULL;
707 e = ldap_next_entry( ld, e ), i++ )
709 char *dn = ldap_get_dn( ld, e );
712 ldap_perror( ld, "ldap_prune" );
713 ldap_get_option( ld, LDAP_OPT_ERROR_NUMBER, &rc );
718 rc = deletechildren( ld, dn );
720 ldap_perror( ld, "ldap_prune" );
726 printf( "\tremoving %s\n", dn );
729 rc = ldap_delete_s( ld, dn );
731 ldap_perror( ld, "ldap_delete" );
738 printf( "\t%s removed\n", dn );