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\t\tdelete recursively\n"
60 " -c\t\tcontinuous operation mode (do not stop on errors)\n"
61 " -C\t\tchase referrals\n"
62 " -d level\tset LDAP debugging level to `level'\n"
63 " -D binddn\tbind DN\n"
64 " -f file\t\tread operations from `file'\n"
65 " -h host\t\tLDAP server\n"
66 " -I\t\tuse SASL Interactive mode\n"
67 " -k\t\tuse Kerberos authentication\n"
68 " -K\t\tlike -k, but do only step 1 of the Kerberos bind\n"
69 " -M\t\tenable Manage DSA IT control (-MM to make it critical)\n"
70 " -n\t\tshow what would be done but don't actually delete\n"
71 " -O secprops\tSASL security properties\n"
72 " -p port\t\tport on LDAP server\n"
73 " -P version\tprocotol version (default: 3)\n"
74 " -Q\t\tuse SASL Quiet mode\n"
75 " -R realm\tSASL realm\n"
76 " -U user\t\tSASL authentication identity (username)\n"
77 " -v\t\trun in verbose mode (diagnostics to standard output)\n"
78 " -w passwd\tbind passwd (for simple authentication)\n"
79 " -W\t\tprompt for bind passwd\n"
80 " -x\t\tSimple authentication\n"
81 " -X id\t\tSASL authorization identity (\"dn:<dn>\" or \"u:<user>\")\n"
82 " -Y mech\t\tSASL mechanism\n"
83 " -Z\t\tissue Start TLS request (-ZZ to require successful response)\n"
91 main( int argc, char **argv )
95 int i, rc, authmethod, referrals, want_bindpw, version, debug, manageDSAit;
97 not = verbose = contoper = want_bindpw = debug = manageDSAit = referrals = 0;
102 prog = (prog = strrchr(argv[0], *LDAP_DIRSEP)) == NULL ? argv[0] : ++prog;
104 while (( i = getopt( argc, argv, "cf:r" "Cd:D:h:IkKMnO:p:P:QRU:vw:WxX:Y:Z" )) != EOF ) {
106 /* Delete Specific Options */
107 case 'c': /* continuous operation mode */
110 case 'f': /* read DNs from a file */
112 fprintf( stderr, "%s: -f previously specified\n" );
115 if (( fp = fopen( optarg, "r" )) == NULL ) {
117 exit( EXIT_FAILURE );
129 debug |= atoi( optarg );
131 case 'D': /* bind DN */
132 if( binddn != NULL ) {
133 fprintf( stderr, "%s: -D previously specified\n" );
136 binddn = strdup( optarg );
138 case 'h': /* ldap host */
139 if( ldaphost != NULL ) {
140 fprintf( stderr, "%s: -h previously specified\n" );
143 ldaphost = strdup( optarg );
146 #ifdef HAVE_CYRUS_SASL
147 if( version == LDAP_VERSION2 ) {
148 fprintf( stderr, "%s: -I incompatible with version %d\n",
152 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
153 fprintf( stderr, "%s: incompatible previous "
154 "authentication choice\n",
158 authmethod = LDAP_AUTH_SASL;
159 version = LDAP_VERSION3;
160 sasl_flags = LDAP_SASL_INTERACTIVE;
163 fprintf( stderr, "%s: was not compiled with SASL support\n",
165 return( EXIT_FAILURE );
167 case 'k': /* kerberos bind */
168 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
169 if( version > LDAP_VERSION2 ) {
170 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
175 if( authmethod != -1 ) {
176 fprintf( stderr, "%s: -k incompatible with previous "
177 "authentication choice\n", prog );
181 authmethod = LDAP_AUTH_KRBV4;
183 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
187 case 'K': /* kerberos bind, part one only */
188 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
189 if( version > LDAP_VERSION2 ) {
190 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
194 if( authmethod != -1 ) {
195 fprintf( stderr, "%s: incompatible with previous "
196 "authentication choice\n", prog );
200 authmethod = LDAP_AUTH_KRBV41;
202 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
203 return( EXIT_FAILURE );
207 /* enable Manage DSA IT */
208 if( version == LDAP_VERSION2 ) {
209 fprintf( stderr, "%s: -M incompatible with LDAPv%d\n",
214 version = LDAP_VERSION3;
216 case 'n': /* print deletes, don't actually do them */
220 #ifdef HAVE_CYRUS_SASL
221 if( sasl_secprops != NULL ) {
222 fprintf( stderr, "%s: -O previously specified\n" );
225 if( version == LDAP_VERSION2 ) {
226 fprintf( stderr, "%s: -O incompatible with LDAPv%d\n",
230 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
231 fprintf( stderr, "%s: incompatible previous "
232 "authentication choice\n", prog );
235 authmethod = LDAP_AUTH_SASL;
236 version = LDAP_VERSION3;
237 sasl_secprops = strdup( optarg );
239 fprintf( stderr, "%s: not compiled with SASL support\n",
241 return( EXIT_FAILURE );
246 fprintf( stderr, "%s: -p previously specified\n" );
249 ldapport = atoi( optarg );
252 switch( atoi(optarg) ) {
254 if( version == LDAP_VERSION3 ) {
255 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
259 version = LDAP_VERSION2;
262 if( version == LDAP_VERSION2 ) {
263 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
267 version = LDAP_VERSION3;
270 fprintf( stderr, "%s: protocol version should be 2 or 3\n",
273 return( EXIT_FAILURE );
276 #ifdef HAVE_CYRUS_SASL
277 if( version == LDAP_VERSION2 ) {
278 fprintf( stderr, "%s: -Q incompatible with version %d\n",
282 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
283 fprintf( stderr, "%s: incompatible previous "
284 "authentication choice\n",
288 authmethod = LDAP_AUTH_SASL;
289 version = LDAP_VERSION3;
290 sasl_flags = LDAP_SASL_QUIET;
293 fprintf( stderr, "%s: was not compiled with SASL support\n",
295 return( EXIT_FAILURE );
298 #ifdef HAVE_CYRUS_SASL
299 if( sasl_realm != NULL ) {
300 fprintf( stderr, "%s: -R previously specified\n" );
303 if( version == LDAP_VERSION2 ) {
304 fprintf( stderr, "%s: -R 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_realm = strdup( optarg );
318 fprintf( stderr, "%s: was not compiled with SASL support\n",
320 return( EXIT_FAILURE );
324 #ifdef HAVE_CYRUS_SASL
325 if( sasl_authc_id != NULL ) {
326 fprintf( stderr, "%s: -U previously specified\n" );
329 if( version == LDAP_VERSION2 ) {
330 fprintf( stderr, "%s: -U 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_authc_id = strdup( optarg );
344 fprintf( stderr, "%s: was not compiled with SASL support\n",
346 return( EXIT_FAILURE );
349 case 'v': /* verbose mode */
352 case 'w': /* password */
353 passwd.bv_val = strdup( optarg );
357 for( p = optarg; *p == '\0'; p++ ) {
361 passwd.bv_len = strlen( passwd.bv_val );
367 #ifdef HAVE_CYRUS_SASL
368 if( sasl_mech != NULL ) {
369 fprintf( stderr, "%s: -Y previously specified\n" );
372 if( version == LDAP_VERSION2 ) {
373 fprintf( stderr, "%s: -Y incompatible with version %d\n",
377 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
378 fprintf( stderr, "%s: incompatible with authentication choice\n", prog );
381 authmethod = LDAP_AUTH_SASL;
382 version = LDAP_VERSION3;
383 sasl_mech = strdup( optarg );
385 fprintf( stderr, "%s: was not compiled with SASL support\n",
387 return( EXIT_FAILURE );
391 if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
392 fprintf( stderr, "%s: incompatible with previous "
393 "authentication choice\n", prog );
396 authmethod = LDAP_AUTH_SIMPLE;
399 #ifdef HAVE_CYRUS_SASL
400 if( sasl_authz_id != NULL ) {
401 fprintf( stderr, "%s: -X previously specified\n" );
404 if( version == LDAP_VERSION2 ) {
405 fprintf( stderr, "%s: -X incompatible with LDAPv%d\n",
409 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
410 fprintf( stderr, "%s: -X incompatible with "
411 "authentication choice\n", prog );
414 authmethod = LDAP_AUTH_SASL;
415 version = LDAP_VERSION3;
416 sasl_authz_id = strdup( optarg );
418 fprintf( stderr, "%s: not compiled with SASL support\n",
420 return( EXIT_FAILURE );
425 if( version == LDAP_VERSION2 ) {
426 fprintf( stderr, "%s -Z incompatible with version %d\n",
430 version = LDAP_VERSION3;
433 fprintf( stderr, "%s: not compiled with TLS support\n",
435 return( EXIT_FAILURE );
439 fprintf( stderr, "%s: unrecongized option -%c\n",
442 return( EXIT_FAILURE );
447 version = LDAP_VERSION3;
449 if (authmethod == -1 && version > LDAP_VERSION2) {
450 #ifdef HAVE_CYRUS_SASL
451 authmethod = LDAP_AUTH_SASL;
453 authmethod = LDAP_AUTH_SIMPLE;
458 if ( optind >= argc ) {
464 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
465 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
467 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
468 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
473 (void) SIGNAL( SIGPIPE, SIG_IGN );
476 if (( ld = ldap_init( ldaphost, ldapport )) == NULL ) {
477 perror( "ldap_init" );
478 return( EXIT_FAILURE );
482 /* this seems prudent for searches below */
483 int deref = LDAP_DEREF_NEVER;
484 ldap_set_option( ld, LDAP_OPT_DEREF, &deref );
487 /* chase referrals */
488 if( ldap_set_option( ld, LDAP_OPT_REFERRALS,
489 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
491 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
492 referrals ? "on" : "off" );
496 if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version )
497 != LDAP_OPT_SUCCESS )
499 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
504 if ( use_tls && ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS ) {
506 ldap_perror( ld, "ldap_start_tls" );
509 fprintf( stderr, "WARNING: could not start TLS\n" );
513 passwd.bv_val = getpassphrase("Enter LDAP Password: ");
514 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
517 if ( authmethod == LDAP_AUTH_SASL ) {
518 #ifdef HAVE_CYRUS_SASL
521 if( sasl_secprops != NULL ) {
522 rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
523 (void *) sasl_secprops );
525 if( rc != LDAP_OPT_SUCCESS ) {
527 "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
529 return( EXIT_FAILURE );
533 defaults = lutil_sasl_defaults( ld,
540 rc = ldap_sasl_interactive_bind_s( ld, binddn,
541 sasl_mech, NULL, NULL,
542 sasl_flags, lutil_sasl_interact, defaults );
544 if( rc != LDAP_SUCCESS ) {
545 ldap_perror( ld, "ldap_sasl_interactive_bind_s" );
546 return( EXIT_FAILURE );
549 fprintf( stderr, "%s was not compiled with SASL support\n",
551 return( EXIT_FAILURE );
555 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
557 ldap_perror( ld, "ldap_bind" );
558 return( EXIT_FAILURE );
565 LDAPControl *ctrls[2];
569 c.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
570 c.ldctl_value.bv_val = NULL;
571 c.ldctl_value.bv_len = 0;
572 c.ldctl_iscritical = manageDSAit > 1;
574 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, &ctrls );
576 if( err != LDAP_OPT_SUCCESS ) {
577 fprintf( stderr, "Could not set ManageDSAit %scontrol\n",
578 c.ldctl_iscritical ? "critical " : "" );
579 if( c.ldctl_iscritical ) {
580 exit( EXIT_FAILURE );
587 for ( ; optind < argc; ++optind ) {
588 rc = dodelete( ld, argv[ optind ] );
591 while ((rc == 0 || contoper) && fgets(buf, sizeof(buf), fp) != NULL) {
592 buf[ strlen( buf ) - 1 ] = '\0'; /* remove trailing newline */
593 if ( *buf != '\0' ) {
594 rc = dodelete( ld, buf );
611 char *matcheddn = NULL, *text = NULL, **refs = NULL;
615 printf( "%sdeleting entry \"%s\"\n",
616 (not ? "!" : ""), dn );
623 /* If prune is on, remove a whole subtree. Delete the children of the
624 * DN recursively, then the DN requested.
626 if ( prune ) deletechildren( ld, dn );
628 rc = ldap_delete_ext( ld, dn, NULL, NULL, &id );
629 if ( rc != LDAP_SUCCESS ) {
630 fprintf( stderr, "ldapdelete: ldap_delete_ext: %s (%d)\n",
631 ldap_err2string( rc ), rc );
635 rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, NULL, &res );
637 ldap_perror( ld, "ldapdelete: ldap_result" );
641 rc = ldap_parse_result( ld, res, &code, &matcheddn, &text, &refs, NULL, 1 );
643 if( rc != LDAP_SUCCESS ) {
644 fprintf( stderr, "ldapdelete: ldap_parse_result: %s (%d)\n",
645 ldap_err2string( rc ), rc );
649 if( verbose || code != LDAP_SUCCESS ||
650 (matcheddn && *matcheddn) || (text && *text) || (refs && *refs) )
652 printf( "Delete Result: %s (%d)\n", ldap_err2string( code ), code );
654 if( text && *text ) {
655 printf( "Additional info: %s\n", text );
658 if( matcheddn && *matcheddn ) {
659 printf( "Matched DN: %s\n", matcheddn );
664 for( i=0; refs[i]; i++ ) {
665 printf("Referral: %s\n", refs[i] );
671 ber_memfree( matcheddn );
672 ber_memvfree( (void **) refs );
678 * Delete all the children of an entry recursively until leaf nodes are reached.
681 static int deletechildren(
685 LDAPMessage *res, *e;
688 static char *attrs[] = { "1.1", NULL };
690 if ( verbose ) printf ( "deleting children of: %s\n", dn );
692 * Do a one level search at dn for children. For each, delete its children.
695 rc = ldap_search_ext_s( ld, dn, LDAP_SCOPE_ONELEVEL, NULL, attrs, 1,
696 NULL, NULL, NULL, -1, &res );
697 if ( rc != LDAP_SUCCESS ) {
698 ldap_perror( ld, "ldap_search" );
702 entries = ldap_count_entries( ld, res );
707 for (e = ldap_first_entry( ld, res ), i = 0; e != NULL;
708 e = ldap_next_entry( ld, e ), i++ )
710 char *dn = ldap_get_dn( ld, e );
713 ldap_perror( ld, "ldap_prune" );
714 ldap_get_option( ld, LDAP_OPT_ERROR_NUMBER, &rc );
719 rc = deletechildren( ld, dn );
721 ldap_perror( ld, "ldap_prune" );
727 printf( "\tremoving %s\n", dn );
730 rc = ldap_delete_s( ld, dn );
732 ldap_perror( ld, "ldap_delete" );
739 printf( "\t%s removed\n", dn );