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 = LUTIL_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 );
145 case 'k': /* kerberos bind */
146 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
147 if( version > LDAP_VERSION2 ) {
148 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
153 if( authmethod != -1 ) {
154 fprintf( stderr, "%s: -k incompatible with previous "
155 "authentication choice\n", prog );
159 authmethod = LDAP_AUTH_KRBV4;
161 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
165 case 'K': /* kerberos bind, part one only */
166 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
167 if( version > LDAP_VERSION2 ) {
168 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
172 if( authmethod != -1 ) {
173 fprintf( stderr, "%s: incompatible with previous "
174 "authentication choice\n", prog );
178 authmethod = LDAP_AUTH_KRBV41;
180 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
181 return( EXIT_FAILURE );
185 /* enable Manage DSA IT */
186 if( version == LDAP_VERSION2 ) {
187 fprintf( stderr, "%s: -M incompatible with LDAPv%d\n",
192 version = LDAP_VERSION3;
194 case 'n': /* print deletes, don't actually do them */
198 #ifdef HAVE_CYRUS_SASL
199 if( sasl_secprops != NULL ) {
200 fprintf( stderr, "%s: -O previously specified\n" );
203 if( version == LDAP_VERSION2 ) {
204 fprintf( stderr, "%s: -O incompatible with LDAPv%d\n",
208 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
209 fprintf( stderr, "%s: incompatible previous "
210 "authentication choice\n", prog );
213 authmethod = LDAP_AUTH_SASL;
214 version = LDAP_VERSION3;
215 sasl_secprops = strdup( optarg );
217 fprintf( stderr, "%s: not compiled with SASL support\n",
219 return( EXIT_FAILURE );
224 fprintf( stderr, "%s: -p previously specified\n" );
227 ldapport = atoi( optarg );
230 switch( atoi(optarg) ) {
232 if( version == LDAP_VERSION3 ) {
233 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
237 version = LDAP_VERSION2;
240 if( version == LDAP_VERSION2 ) {
241 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
245 version = LDAP_VERSION3;
248 fprintf( stderr, "%s: protocol version should be 2 or 3\n",
251 return( EXIT_FAILURE );
254 #ifdef HAVE_CYRUS_SASL
255 if( version == LDAP_VERSION2 ) {
256 fprintf( stderr, "%s: -Q incompatible with version %d\n",
260 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
261 fprintf( stderr, "%s: incompatible previous "
262 "authentication choice\n",
266 authmethod = LDAP_AUTH_SASL;
267 version = LDAP_VERSION3;
268 sasl_flags = LUTIL_SASL_QUIET;
270 fprintf( stderr, "%s: was not compiled with SASL support\n",
272 return( EXIT_FAILURE );
275 #ifdef HAVE_CYRUS_SASL
276 if( sasl_realm != NULL ) {
277 fprintf( stderr, "%s: -R previously specified\n" );
280 if( version == LDAP_VERSION2 ) {
281 fprintf( stderr, "%s: -R incompatible with version %d\n",
285 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
286 fprintf( stderr, "%s: incompatible previous "
287 "authentication choice\n",
291 authmethod = LDAP_AUTH_SASL;
292 version = LDAP_VERSION3;
293 sasl_realm = strdup( optarg );
295 fprintf( stderr, "%s: was not compiled with SASL support\n",
297 return( EXIT_FAILURE );
301 #ifdef HAVE_CYRUS_SASL
302 if( sasl_authc_id != NULL ) {
303 fprintf( stderr, "%s: -U previously specified\n" );
306 if( version == LDAP_VERSION2 ) {
307 fprintf( stderr, "%s: -U incompatible with version %d\n",
311 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
312 fprintf( stderr, "%s: incompatible previous "
313 "authentication choice\n",
317 authmethod = LDAP_AUTH_SASL;
318 version = LDAP_VERSION3;
319 sasl_authc_id = strdup( optarg );
321 fprintf( stderr, "%s: was not compiled with SASL support\n",
323 return( EXIT_FAILURE );
326 case 'v': /* verbose mode */
329 case 'w': /* password */
330 passwd.bv_val = strdup( optarg );
334 for( p = optarg; *p == '\0'; p++ ) {
338 passwd.bv_len = strlen( passwd.bv_val );
344 #ifdef HAVE_CYRUS_SASL
345 if( sasl_mech != NULL ) {
346 fprintf( stderr, "%s: -Y previously specified\n" );
349 if( version == LDAP_VERSION2 ) {
350 fprintf( stderr, "%s: -Y incompatible with version %d\n",
354 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
355 fprintf( stderr, "%s: incompatible with authentication choice\n", prog );
358 authmethod = LDAP_AUTH_SASL;
359 version = LDAP_VERSION3;
360 sasl_mech = strdup( optarg );
362 fprintf( stderr, "%s: was not compiled with SASL support\n",
364 return( EXIT_FAILURE );
368 if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
369 fprintf( stderr, "%s: incompatible with previous "
370 "authentication choice\n", prog );
373 authmethod = LDAP_AUTH_SIMPLE;
376 #ifdef HAVE_CYRUS_SASL
377 if( sasl_authz_id != NULL ) {
378 fprintf( stderr, "%s: -X previously specified\n" );
381 if( version == LDAP_VERSION2 ) {
382 fprintf( stderr, "%s: -X incompatible with LDAPv%d\n",
386 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
387 fprintf( stderr, "%s: -X incompatible with "
388 "authentication choice\n", prog );
391 authmethod = LDAP_AUTH_SASL;
392 version = LDAP_VERSION3;
393 sasl_authz_id = strdup( optarg );
395 fprintf( stderr, "%s: not compiled with SASL support\n",
397 return( EXIT_FAILURE );
402 if( version == LDAP_VERSION2 ) {
403 fprintf( stderr, "%s -Z incompatible with version %d\n",
407 version = LDAP_VERSION3;
410 fprintf( stderr, "%s: not compiled with TLS support\n",
412 return( EXIT_FAILURE );
416 fprintf( stderr, "%s: unrecongized option -%c\n",
419 return( EXIT_FAILURE );
424 version = LDAP_VERSION3;
426 if (authmethod == -1 && version > LDAP_VERSION2) {
427 #ifdef HAVE_CYRUS_SASL
428 authmethod = LDAP_AUTH_SASL;
430 authmethod = LDAP_AUTH_SIMPLE;
435 if ( optind >= argc ) {
441 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
442 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
444 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
445 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
450 (void) SIGNAL( SIGPIPE, SIG_IGN );
453 if (( ld = ldap_init( ldaphost, ldapport )) == NULL ) {
454 perror( "ldap_init" );
455 return( EXIT_FAILURE );
459 /* this seems prudent for searches below */
460 int deref = LDAP_DEREF_NEVER;
461 ldap_set_option( ld, LDAP_OPT_DEREF, &deref );
464 /* chase referrals */
465 if( ldap_set_option( ld, LDAP_OPT_REFERRALS,
466 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
468 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
469 referrals ? "on" : "off" );
473 if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version )
474 != LDAP_OPT_SUCCESS )
476 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
481 if ( use_tls && ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS ) {
483 ldap_perror( ld, "ldap_start_tls" );
486 fprintf( stderr, "WARNING: could not start TLS\n" );
490 passwd.bv_val = getpassphrase("Enter LDAP Password: ");
491 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
494 if ( authmethod == LDAP_AUTH_SASL ) {
495 #ifdef HAVE_CYRUS_SASL
498 if( sasl_secprops != NULL ) {
499 rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
500 (void *) sasl_secprops );
502 if( rc != LDAP_OPT_SUCCESS ) {
504 "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
506 return( EXIT_FAILURE );
510 defaults = lutil_sasl_defaults( ld, sasl_flags,
517 rc = ldap_sasl_interactive_bind_s( ld, binddn,
518 sasl_mech, NULL, NULL,
519 lutil_sasl_interact, defaults );
521 if( rc != LDAP_SUCCESS ) {
522 ldap_perror( ld, "ldap_sasl_interactive_bind_s" );
523 return( EXIT_FAILURE );
526 fprintf( stderr, "%s was not compiled with SASL support\n",
528 return( EXIT_FAILURE );
532 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
534 ldap_perror( ld, "ldap_bind" );
535 return( EXIT_FAILURE );
542 LDAPControl *ctrls[2];
546 c.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
547 c.ldctl_value.bv_val = NULL;
548 c.ldctl_value.bv_len = 0;
549 c.ldctl_iscritical = manageDSAit > 1;
551 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, &ctrls );
553 if( err != LDAP_OPT_SUCCESS ) {
554 fprintf( stderr, "Could not set ManageDSAit %scontrol\n",
555 c.ldctl_iscritical ? "critical " : "" );
556 if( c.ldctl_iscritical ) {
557 exit( EXIT_FAILURE );
564 for ( ; optind < argc; ++optind ) {
565 rc = dodelete( ld, argv[ optind ] );
568 while ((rc == 0 || contoper) && fgets(buf, sizeof(buf), fp) != NULL) {
569 buf[ strlen( buf ) - 1 ] = '\0'; /* remove trailing newline */
570 if ( *buf != '\0' ) {
571 rc = dodelete( ld, buf );
588 char *matcheddn = NULL, *text = NULL, **refs = NULL;
592 printf( "%sdeleting entry \"%s\"\n",
593 (not ? "!" : ""), dn );
600 /* If prune is on, remove a whole subtree. Delete the children of the
601 * DN recursively, then the DN requested.
603 if ( prune ) deletechildren( ld, dn );
605 rc = ldap_delete_ext( ld, dn, NULL, NULL, &id );
606 if ( rc != LDAP_SUCCESS ) {
607 fprintf( stderr, "ldapdelete: ldap_delete_ext: %s (%d)\n",
608 ldap_err2string( rc ), rc );
612 rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, NULL, &res );
614 ldap_perror( ld, "ldapdelete: ldap_result" );
618 rc = ldap_parse_result( ld, res, &code, &matcheddn, &text, &refs, NULL, 1 );
620 if( rc != LDAP_SUCCESS ) {
621 fprintf( stderr, "ldapdelete: ldap_parse_result: %s (%d)\n",
622 ldap_err2string( rc ), rc );
626 if( verbose || code != LDAP_SUCCESS ||
627 (matcheddn && *matcheddn) || (text && *text) || (refs && *refs) )
629 printf( "Delete Result: %s (%d)\n", ldap_err2string( code ), code );
631 if( text && *text ) {
632 printf( "Additional info: %s\n", text );
635 if( matcheddn && *matcheddn ) {
636 printf( "Matched DN: %s\n", matcheddn );
641 for( i=0; refs[i]; i++ ) {
642 printf("Referral: %s\n", refs[i] );
648 ber_memfree( matcheddn );
649 ber_memvfree( (void **) refs );
655 * Delete all the children of an entry recursively until leaf nodes are reached.
658 static int deletechildren(
662 LDAPMessage *res, *e;
665 static char *attrs[] = { "1.1", NULL };
667 if ( verbose ) printf ( "deleting children of: %s\n", dn );
669 * Do a one level search at dn for children. For each, delete its children.
672 rc = ldap_search_ext_s( ld, dn, LDAP_SCOPE_ONELEVEL, NULL, attrs, 1,
673 NULL, NULL, NULL, -1, &res );
674 if ( rc != LDAP_SUCCESS ) {
675 ldap_perror( ld, "ldap_search" );
679 entries = ldap_count_entries( ld, res );
684 for (e = ldap_first_entry( ld, res ), i = 0; e != NULL;
685 e = ldap_next_entry( ld, e ), i++ )
687 char *dn = ldap_get_dn( ld, e );
690 ldap_perror( ld, "ldap_prune" );
691 ldap_get_option( ld, LDAP_OPT_ERROR_NUMBER, &rc );
696 rc = deletechildren( ld, dn );
698 ldap_perror( ld, "ldap_prune" );
704 printf( "\tremoving %s\n", dn );
707 rc = ldap_delete_s( ld, dn );
709 ldap_perror( ld, "ldap_delete" );
716 printf( "\t%s removed\n", dn );