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 char *sasl_authc_id = NULL;
31 static char *sasl_authz_id = NULL;
32 static char *sasl_mech = NULL;
33 static char *sasl_secprops = NULL;
35 static int use_tls = 0;
36 static int not, verbose, contoper;
39 static int dodelete LDAP_P((
43 static int deletechildren LDAP_P((
48 usage( const char *s )
51 "Delete entries from an LDAP server\n\n"
52 "usage: %s [options] [dn]...\n"
53 " dn: list of DNs to delete. If not given, it will be readed from stdin\n"
54 " or from the file specified with \"-f file\".\n"
56 " -r\t\tdelete recursively\n"
58 " -c\t\tcontinuous operation mode (do not stop on errors)\n"
59 " -C\t\tchase referrals\n"
60 " -d level\tset LDAP debugging level to `level'\n"
61 " -D binddn\tbind DN\n"
62 " -f file\t\tread operations from `file'\n"
63 " -h host\t\tLDAP server\n"
64 " -k\t\tuse Kerberos authentication\n"
65 " -K\t\tlike -k, but do only step 1 of the Kerberos bind\n"
66 " -M\t\tenable Manage DSA IT control (-MM to make it critical)\n"
67 " -n\t\tshow what would be done but don't actually delete\n"
68 " -O secprops\tSASL security properties\n"
69 " -p port\t\tport on LDAP server\n"
70 " -P version\tprocotol version (default: 3)\n"
71 " -U user\t\tSASL authentication identity (username)\n"
72 " -v\t\trun in verbose mode (diagnostics to standard output)\n"
73 " -w passwd\tbind passwd (for simple authentication)\n"
74 " -W\t\tprompt for bind passwd\n"
75 " -x\t\tSimple authentication\n"
76 " -X id\t\tSASL authorization identity (\"dn:<dn>\" or \"u:<user>\")\n"
77 " -Y mech\t\tSASL mechanism\n"
78 " -Z\t\tissue Start TLS request (-ZZ to require successful response)\n"
86 main( int argc, char **argv )
90 int i, rc, authmethod, referrals, want_bindpw, version, debug, manageDSAit;
92 not = verbose = contoper = want_bindpw = debug = manageDSAit = referrals = 0;
97 prog = (prog = strrchr(argv[0], *LDAP_DIRSEP)) == NULL ? argv[0] : ++prog;
99 while (( i = getopt( argc, argv, "cf:r" "Cd:D:h:kKMnO:p:P:U:vw:WxX:Y:Z" )) != EOF ) {
101 /* Delete Specific Options */
102 case 'c': /* continuous operation mode */
105 case 'f': /* read DNs from a file */
106 if (( fp = fopen( optarg, "r" )) == NULL ) {
108 exit( EXIT_FAILURE );
120 debug |= atoi( optarg );
122 case 'D': /* bind DN */
123 binddn = strdup( optarg );
125 case 'h': /* ldap host */
126 ldaphost = strdup( optarg );
128 case 'k': /* kerberos bind */
129 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
130 if( version > LDAP_VERSION2 ) {
131 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
136 if( authmethod != -1 ) {
137 fprintf( stderr, "%s: -k incompatible with previous "
138 "authentication choice\n", prog );
142 authmethod = LDAP_AUTH_KRBV4;
144 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
148 case 'K': /* kerberos bind, part one only */
149 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
150 if( version > LDAP_VERSION2 ) {
151 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
155 if( authmethod != -1 ) {
156 fprintf( stderr, "%s: incompatible with previous "
157 "authentication choice\n", prog );
161 authmethod = LDAP_AUTH_KRBV41;
163 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
164 return( EXIT_FAILURE );
168 /* enable Manage DSA IT */
169 if( version == LDAP_VERSION2 ) {
170 fprintf( stderr, "%s: -M incompatible with LDAPv%d\n",
175 version = LDAP_VERSION3;
177 case 'n': /* print deletes, don't actually do them */
181 #ifdef HAVE_CYRUS_SASL
182 if( version == LDAP_VERSION2 ) {
183 fprintf( stderr, "%s -O incompatible with LDAPv%d\n",
187 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
188 fprintf( stderr, "%s: incompatible previous "
189 "authentication choice\n", prog );
192 sasl_secprops = strdup( optarg );
193 authmethod = LDAP_AUTH_SASL;
194 version = LDAP_VERSION3;
196 fprintf( stderr, "%s: not compiled with SASL support\n",
198 return( EXIT_FAILURE );
202 ldapport = atoi( optarg );
205 switch( atoi(optarg) ) {
207 if( version == LDAP_VERSION3 ) {
208 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
212 version = LDAP_VERSION2;
215 if( version == LDAP_VERSION2 ) {
216 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
220 version = LDAP_VERSION3;
223 fprintf( stderr, "%s: protocol version should be 2 or 3\n",
226 return( EXIT_FAILURE );
229 #ifdef HAVE_CYRUS_SASL
230 if( version == LDAP_VERSION2 ) {
231 fprintf( stderr, "%s: -U incompatible with version %d\n",
235 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
236 fprintf( stderr, "%s: incompatible previous "
237 "authentication choice\n",
241 authmethod = LDAP_AUTH_SASL;
242 version = LDAP_VERSION3;
244 sasl_authc_id = strdup( optarg );
245 authmethod = LDAP_AUTH_SASL;
247 fprintf( stderr, "%s: was not compiled with SASL support\n",
249 return( EXIT_FAILURE );
252 case 'v': /* verbose mode */
255 case 'w': /* password */
256 passwd.bv_val = strdup( optarg );
260 for( p = optarg; *p == '\0'; p++ ) {
264 passwd.bv_len = strlen( passwd.bv_val );
270 #ifdef HAVE_CYRUS_SASL
271 if( version == LDAP_VERSION2 ) {
272 fprintf( stderr, "%s: -Y incompatible with version %d\n",
276 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
277 fprintf( stderr, "%s: incompatible with authentication choice\n", prog );
281 authmethod = LDAP_AUTH_SASL;
282 version = LDAP_VERSION3;
284 fprintf( stderr, "%s: was not compiled with SASL support\n",
286 return( EXIT_FAILURE );
290 if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
291 fprintf( stderr, "%s: incompatible with previous "
292 "authentication choice\n", prog );
295 authmethod = LDAP_AUTH_SIMPLE;
298 #ifdef HAVE_CYRUS_SASL
299 if( version == LDAP_VERSION2 ) {
300 fprintf( stderr, "%s: -X incompatible with LDAPv%d\n",
304 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
305 fprintf( stderr, "%s: -X incompatible with "
306 "authentication choice\n", prog );
309 authmethod = LDAP_AUTH_SASL;
310 version = LDAP_VERSION3;
312 sasl_authz_id = strdup( optarg );
313 authmethod = LDAP_AUTH_SASL;
315 fprintf( stderr, "%s: not compiled with SASL support\n",
317 return( EXIT_FAILURE );
322 if( version == LDAP_VERSION2 ) {
323 fprintf( stderr, "%s -Z incompatible with version %d\n",
327 version = LDAP_VERSION3;
330 fprintf( stderr, "%s: not compiled with TLS support\n",
332 return( EXIT_FAILURE );
336 fprintf( stderr, "%s: unrecongized option -%c\n",
339 return( EXIT_FAILURE );
344 version = LDAP_VERSION3;
346 if (authmethod == -1 && version > LDAP_VERSION2) {
347 #ifdef HAVE_CYRUS_SASL
348 authmethod = LDAP_AUTH_SASL;
350 authmethod = LDAP_AUTH_SIMPLE;
355 if ( optind >= argc ) {
361 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
362 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
364 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
365 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
370 (void) SIGNAL( SIGPIPE, SIG_IGN );
373 if (( ld = ldap_init( ldaphost, ldapport )) == NULL ) {
374 perror( "ldap_init" );
375 return( EXIT_FAILURE );
379 /* this seems prudent for searches below */
380 int deref = LDAP_DEREF_NEVER;
381 ldap_set_option( ld, LDAP_OPT_DEREF, &deref );
384 /* chase referrals */
385 if( ldap_set_option( ld, LDAP_OPT_REFERRALS,
386 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
388 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
389 referrals ? "on" : "off" );
393 if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version )
394 != LDAP_OPT_SUCCESS )
396 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
401 if ( use_tls && ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS ) {
403 ldap_perror( ld, "ldap_start_tls" );
406 fprintf( stderr, "WARNING: could not start TLS\n" );
410 passwd.bv_val = getpassphrase("Enter LDAP Password: ");
411 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
414 if ( authmethod == LDAP_AUTH_SASL ) {
415 #ifdef HAVE_CYRUS_SASL
416 if( sasl_secprops != NULL ) {
417 rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
418 (void *) sasl_secprops );
420 if( rc != LDAP_OPT_SUCCESS ) {
422 "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
424 return( EXIT_FAILURE );
428 rc = ldap_sasl_interactive_bind_s( ld, binddn,
429 sasl_mech, NULL, NULL, lutil_sasl_interact );
431 if( rc != LDAP_SUCCESS ) {
432 ldap_perror( ld, "ldap_sasl_interactive_bind_s" );
433 return( EXIT_FAILURE );
436 fprintf( stderr, "%s was not compiled with SASL support\n",
438 return( EXIT_FAILURE );
442 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
444 ldap_perror( ld, "ldap_bind" );
445 return( EXIT_FAILURE );
452 LDAPControl *ctrls[2];
456 c.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
457 c.ldctl_value.bv_val = NULL;
458 c.ldctl_value.bv_len = 0;
459 c.ldctl_iscritical = manageDSAit > 1;
461 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, &ctrls );
463 if( err != LDAP_OPT_SUCCESS ) {
464 fprintf( stderr, "Could not set ManageDSAit %scontrol\n",
465 c.ldctl_iscritical ? "critical " : "" );
466 if( c.ldctl_iscritical ) {
467 exit( EXIT_FAILURE );
474 for ( ; optind < argc; ++optind ) {
475 rc = dodelete( ld, argv[ optind ] );
478 while ((rc == 0 || contoper) && fgets(buf, sizeof(buf), fp) != NULL) {
479 buf[ strlen( buf ) - 1 ] = '\0'; /* remove trailing newline */
480 if ( *buf != '\0' ) {
481 rc = dodelete( ld, buf );
498 char *matcheddn = NULL, *text = NULL, **refs = NULL;
502 printf( "%sdeleting entry \"%s\"\n",
503 (not ? "!" : ""), dn );
510 /* If prune is on, remove a whole subtree. Delete the children of the
511 * DN recursively, then the DN requested.
513 if ( prune ) deletechildren( ld, dn );
515 rc = ldap_delete_ext( ld, dn, NULL, NULL, &id );
516 if ( rc != LDAP_SUCCESS ) {
517 fprintf( stderr, "ldapdelete: ldap_delete_ext: %s (%d)\n",
518 ldap_err2string( rc ), rc );
522 rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, NULL, &res );
524 ldap_perror( ld, "ldapdelete: ldap_result" );
528 rc = ldap_parse_result( ld, res, &code, &matcheddn, &text, &refs, NULL, 1 );
530 if( rc != LDAP_SUCCESS ) {
531 fprintf( stderr, "ldapdelete: ldap_parse_result: %s (%d)\n",
532 ldap_err2string( rc ), rc );
536 if( verbose || code != LDAP_SUCCESS ||
537 (matcheddn && *matcheddn) || (text && *text) || (refs && *refs) )
539 printf( "Delete Result: %s (%d)\n", ldap_err2string( code ), code );
541 if( text && *text ) {
542 printf( "Additional info: %s\n", text );
545 if( matcheddn && *matcheddn ) {
546 printf( "Matched DN: %s\n", matcheddn );
551 for( i=0; refs[i]; i++ ) {
552 printf("Referral: %s\n", refs[i] );
558 ber_memfree( matcheddn );
559 ber_memvfree( (void **) refs );
565 * Delete all the children of an entry recursively until leaf nodes are reached.
568 static int deletechildren(
572 LDAPMessage *res, *e;
575 static char *attrs[] = { "1.1", NULL };
577 if ( verbose ) printf ( "deleting children of: %s\n", dn );
579 * Do a one level search at dn for children. For each, delete its children.
582 rc = ldap_search_ext_s( ld, dn, LDAP_SCOPE_ONELEVEL, NULL, attrs, 1,
583 NULL, NULL, NULL, -1, &res );
584 if ( rc != LDAP_SUCCESS ) {
585 ldap_perror( ld, "ldap_search" );
589 entries = ldap_count_entries( ld, res );
594 for (e = ldap_first_entry( ld, res ), i = 0; e != NULL;
595 e = ldap_next_entry( ld, e ), i++ )
597 char *dn = ldap_get_dn( ld, e );
600 ldap_perror( ld, "ldap_prune" );
601 ldap_get_option( ld, LDAP_OPT_ERROR_NUMBER, &rc );
606 rc = deletechildren( ld, dn );
608 ldap_perror( ld, "ldap_prune" );
614 printf( "\tremoving %s\n", dn );
617 rc = ldap_delete_s( ld, dn );
619 ldap_perror( ld, "ldap_delete" );
626 printf( "\t%s removed\n", dn );