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 );
337 return( EXIT_FAILURE );
342 version = LDAP_VERSION3;
344 if (authmethod == -1 && version > LDAP_VERSION2) {
345 #ifdef HAVE_CYRUS_SASL
346 authmethod = LDAP_AUTH_SASL;
348 authmethod = LDAP_AUTH_SIMPLE;
353 if ( optind >= argc ) {
359 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
360 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
362 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
363 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
368 (void) SIGNAL( SIGPIPE, SIG_IGN );
371 if (( ld = ldap_init( ldaphost, ldapport )) == NULL ) {
372 perror( "ldap_init" );
373 return( EXIT_FAILURE );
377 /* this seems prudent for searches below */
378 int deref = LDAP_DEREF_NEVER;
379 ldap_set_option( ld, LDAP_OPT_DEREF, &deref );
382 /* chase referrals */
383 if( ldap_set_option( ld, LDAP_OPT_REFERRALS,
384 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
386 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
387 referrals ? "on" : "off" );
391 if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version )
392 != LDAP_OPT_SUCCESS )
394 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
399 if ( use_tls && ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS ) {
401 ldap_perror( ld, "ldap_start_tls" );
404 fprintf( stderr, "WARNING: could not start TLS\n" );
408 passwd.bv_val = getpassphrase("Enter LDAP Password: ");
409 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
412 if ( authmethod == LDAP_AUTH_SASL ) {
413 #ifdef HAVE_CYRUS_SASL
414 if( sasl_secprops != NULL ) {
415 rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
416 (void *) sasl_secprops );
418 if( rc != LDAP_OPT_SUCCESS ) {
420 "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
422 return( EXIT_FAILURE );
426 rc = ldap_sasl_interactive_bind_s( ld, binddn,
427 sasl_mech, NULL, NULL, lutil_sasl_interact );
429 if( rc != LDAP_SUCCESS ) {
430 ldap_perror( ld, "ldap_sasl_interactive_bind_s" );
431 return( EXIT_FAILURE );
434 fprintf( stderr, "%s was not compiled with SASL support\n",
436 return( EXIT_FAILURE );
440 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
442 ldap_perror( ld, "ldap_bind" );
443 return( EXIT_FAILURE );
450 LDAPControl *ctrls[2];
454 c.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
455 c.ldctl_value.bv_val = NULL;
456 c.ldctl_value.bv_len = 0;
457 c.ldctl_iscritical = manageDSAit > 1;
459 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, &ctrls );
461 if( err != LDAP_OPT_SUCCESS ) {
462 fprintf( stderr, "Could not set ManageDSAit %scontrol\n",
463 c.ldctl_iscritical ? "critical " : "" );
464 if( c.ldctl_iscritical ) {
465 exit( EXIT_FAILURE );
472 for ( ; optind < argc; ++optind ) {
473 rc = dodelete( ld, argv[ optind ] );
476 while ((rc == 0 || contoper) && fgets(buf, sizeof(buf), fp) != NULL) {
477 buf[ strlen( buf ) - 1 ] = '\0'; /* remove trailing newline */
478 if ( *buf != '\0' ) {
479 rc = dodelete( ld, buf );
496 char *matcheddn = NULL, *text = NULL, **refs = NULL;
500 printf( "%sdeleting entry \"%s\"\n",
501 (not ? "!" : ""), dn );
508 /* If prune is on, remove a whole subtree. Delete the children of the
509 * DN recursively, then the DN requested.
511 if ( prune ) deletechildren( ld, dn );
513 rc = ldap_delete_ext( ld, dn, NULL, NULL, &id );
514 if ( rc != LDAP_SUCCESS ) {
515 fprintf( stderr, "ldapdelete: ldap_delete_ext: %s (%d)\n",
516 ldap_err2string( rc ), rc );
520 rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, NULL, &res );
522 ldap_perror( ld, "ldapdelete: ldap_result" );
526 rc = ldap_parse_result( ld, res, &code, &matcheddn, &text, &refs, NULL, 1 );
528 if( rc != LDAP_SUCCESS ) {
529 fprintf( stderr, "ldapdelete: ldap_parse_result: %s (%d)\n",
530 ldap_err2string( rc ), rc );
534 if( verbose || code != LDAP_SUCCESS ||
535 (matcheddn && *matcheddn) || (text && *text) || (refs && *refs) )
537 printf( "Delete Result: %s (%d)\n", ldap_err2string( code ), code );
539 if( text && *text ) {
540 printf( "Additional info: %s\n", text );
543 if( matcheddn && *matcheddn ) {
544 printf( "Matched DN: %s\n", matcheddn );
549 for( i=0; refs[i]; i++ ) {
550 printf("Referral: %s\n", refs[i] );
556 ber_memfree( matcheddn );
557 ber_memvfree( (void **) refs );
563 * Delete all the children of an entry recursively until leaf nodes are reached.
566 static int deletechildren(
570 LDAPMessage *res, *e;
573 static char *attrs[] = { "1.1", NULL };
575 if ( verbose ) printf ( "deleting children of: %s\n", dn );
577 * Do a one level search at dn for children. For each, delete its children.
580 rc = ldap_search_ext_s( ld, dn, LDAP_SCOPE_ONELEVEL, NULL, attrs, 1,
581 NULL, NULL, NULL, -1, &res );
582 if ( rc != LDAP_SUCCESS ) {
583 ldap_perror( ld, "ldap_search" );
587 entries = ldap_count_entries( ld, res );
592 for (e = ldap_first_entry( ld, res ), i = 0; e != NULL;
593 e = ldap_next_entry( ld, e ), i++ )
595 char *dn = ldap_get_dn( ld, e );
598 ldap_perror( ld, "ldap_prune" );
599 ldap_get_option( ld, LDAP_OPT_ERROR_NUMBER, &rc );
604 rc = deletechildren( ld, dn );
606 ldap_perror( ld, "ldap_prune" );
612 printf( "\tremoving %s\n", dn );
615 rc = ldap_delete_s( ld, dn );
617 ldap_perror( ld, "ldap_delete" );
624 printf( "\t%s removed\n", dn );