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"
22 static char *binddn = NULL;
23 static struct berval passwd = { 0, NULL };
24 static char *ldaphost = NULL;
25 static int ldapport = 0;
27 #ifdef HAVE_CYRUS_SASL
28 static char *sasl_authc_id = NULL;
29 static char *sasl_authz_id = NULL;
30 static char *sasl_mech = NULL;
31 static char *sasl_secprops = NULL;
33 static int use_tls = 0;
34 static int not, verbose, contoper;
37 static int dodelete LDAP_P((
41 static int deletechildren LDAP_P((
46 usage( const char *s )
49 "Delete entries from an LDAP server\n\n"
50 "usage: %s [options] [dn]...\n"
51 " dn: list of DNs to delete. If not given, it will be readed from stdin\n"
52 " or from the file specified with \"-f file\".\n"
54 " -c\t\tcontinuous operation mode (do not stop on errors)\n"
55 " -C\t\tchase referrals\n"
56 " -d level\tset LDAP debugging level to `level'\n"
57 " -D binddn\tbind DN\n"
58 " -f file\t\tdelete DNs listed in `file'\n"
59 " -h host\t\tLDAP server\n"
60 " -k\t\tuse Kerberos authentication\n"
61 " -K\t\tlike -k, but do only step 1 of the Kerberos bind\n"
62 " -M\t\tenable Manage DSA IT control (-MM to make it critical)\n"
63 " -n\t\tshow what would be done but don't actually delete\n"
64 " -O secprops\tSASL security properties\n"
65 " -p port\t\tport on LDAP server\n"
66 " -P version\tprocotol version (default: 3)\n"
67 " -r\t\tdelete recursively\n"
68 " -U user\t\tSASL authentication identity (username)\n"
69 " -v\t\trun in verbose mode (diagnostics to standard output)\n"
70 " -w passwd\tbind passwd (for simple authentication)\n"
71 " -W\t\tprompt for bind passwd\n"
72 " -X id\t\tSASL authorization identity (\"dn:<dn>\" or \"u:<user>\")\n"
73 " -Y mech\t\tSASL mechanism\n"
74 " -Z\t\tissue Start TLS request (-ZZ to require successful response)\n"
82 main( int argc, char **argv )
86 int i, rc, authmethod, referrals, want_bindpw, version, debug, manageDSAit;
88 not = verbose = contoper = want_bindpw = debug = manageDSAit = referrals = 0;
90 authmethod = LDAP_AUTH_SIMPLE;
93 while (( i = getopt( argc, argv, "cCD:d:f:h:KMnO:P:p:rU:vWw:X:Y:Z" )) != EOF ) {
95 case 'k': /* kerberos bind */
96 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
97 authmethod = LDAP_AUTH_KRBV4;
99 fprintf( stderr, "%s was not compiled with Kerberos support\n", argv[0] );
100 return( EXIT_FAILURE );
103 case 'K': /* kerberos bind, part one only */
104 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
105 authmethod = LDAP_AUTH_KRBV41;
107 fprintf( stderr, "%s was not compiled with Kerberos support\n", argv[0] );
108 return( EXIT_FAILURE );
111 case 'c': /* continuous operation mode */
117 case 'h': /* ldap host */
118 ldaphost = strdup( optarg );
120 case 'D': /* bind DN */
121 binddn = strdup( optarg );
123 case 'w': /* password */
124 passwd.bv_val = strdup( optarg );
128 for( p = optarg; *p == '\0'; p++ ) {
132 passwd.bv_len = strlen( passwd.bv_val );
134 case 'f': /* read DNs from a file */
135 if (( fp = fopen( optarg, "r" )) == NULL ) {
137 exit( EXIT_FAILURE );
141 debug |= atoi( optarg );
144 ldapport = atoi( optarg );
146 case 'n': /* print deletes, don't actually do them */
152 case 'v': /* verbose mode */
156 /* enable Manage DSA IT */
163 switch( atoi(optarg) )
166 version = LDAP_VERSION2;
169 version = LDAP_VERSION3;
172 fprintf( stderr, "protocol version should be 2 or 3\n" );
174 return( EXIT_FAILURE );
178 #ifdef HAVE_CYRUS_SASL
179 sasl_secprops = strdup( optarg );
180 authmethod = LDAP_AUTH_SASL;
182 fprintf( stderr, "%s was not compiled with SASL support\n",
184 return( EXIT_FAILURE );
188 #ifdef HAVE_CYRUS_SASL
189 if ( strcasecmp( optarg, "any" ) && strcmp( optarg, "*" ) ) {
190 sasl_mech = strdup( optarg );
192 authmethod = LDAP_AUTH_SASL;
194 fprintf( stderr, "%s was not compiled with SASL support\n",
196 return( EXIT_FAILURE );
200 #ifdef HAVE_CYRUS_SASL
201 sasl_authc_id = strdup( optarg );
202 authmethod = LDAP_AUTH_SASL;
204 fprintf( stderr, "%s was not compiled with SASL support\n",
206 return( EXIT_FAILURE );
210 #ifdef HAVE_CYRUS_SASL
211 sasl_authz_id = strdup( optarg );
212 authmethod = LDAP_AUTH_SASL;
214 fprintf( stderr, "%s was not compiled with SASL support\n",
216 return( EXIT_FAILURE );
223 fprintf( stderr, "%s was not compiled with TLS support\n",
225 return( EXIT_FAILURE );
230 return( EXIT_FAILURE );
234 if ( ( authmethod == LDAP_AUTH_KRBV4 ) || ( authmethod ==
235 LDAP_AUTH_KRBV41 ) ) {
236 if( version > LDAP_VERSION2 ) {
237 fprintf( stderr, "Kerberos requires LDAPv2\n" );
238 return( EXIT_FAILURE );
240 version = LDAP_VERSION2;
242 else if ( authmethod == LDAP_AUTH_SASL ) {
243 if( version != -1 && version != LDAP_VERSION3 ) {
244 fprintf( stderr, "SASL requires LDAPv3\n" );
245 return( EXIT_FAILURE );
247 version = LDAP_VERSION3;
251 if( version != -1 && version != LDAP_VERSION3 ) {
252 fprintf(stderr, "manage DSA control requires LDAPv3\n");
255 version = LDAP_VERSION3;
259 if( version != -1 && version != LDAP_VERSION3 ) {
260 fprintf(stderr, "Start TLS requires LDAPv3\n");
263 version = LDAP_VERSION3;
267 if ( optind >= argc ) {
273 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
274 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
276 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
277 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
282 (void) SIGNAL( SIGPIPE, SIG_IGN );
285 if (( ld = ldap_init( ldaphost, ldapport )) == NULL ) {
286 perror( "ldap_init" );
287 return( EXIT_FAILURE );
291 /* this seems prudent for searches below */
292 int deref = LDAP_DEREF_NEVER;
293 ldap_set_option( ld, LDAP_OPT_DEREF, &deref );
296 /* chase referrals */
297 if( ldap_set_option( ld, LDAP_OPT_REFERRALS,
298 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
300 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
301 referrals ? "on" : "off" );
305 if (version == -1 ) {
309 if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version )
310 != LDAP_OPT_SUCCESS )
312 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
317 if ( use_tls && ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS ) {
319 ldap_perror( ld, "ldap_start_tls" );
322 fprintf( stderr, "WARNING: could not start TLS\n" );
326 passwd.bv_val = getpassphrase("Enter LDAP Password: ");
327 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
330 if ( authmethod == LDAP_AUTH_SASL ) {
331 #ifdef HAVE_CYRUS_SASL
332 ldap_set_sasl_interact_proc( ld, lutil_sasl_interact );
334 if( sasl_secprops != NULL ) {
335 rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
336 (void *) sasl_secprops );
338 if( rc != LDAP_OPT_SUCCESS ) {
340 "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
342 return( EXIT_FAILURE );
346 rc = ldap_sasl_interactive_bind_s( ld, binddn,
347 sasl_mech, NULL, NULL );
349 if( rc != LDAP_SUCCESS ) {
350 ldap_perror( ld, "ldap_sasl_interactive_bind_s" );
351 return( EXIT_FAILURE );
354 fprintf( stderr, "%s was not compiled with SASL support\n",
356 return( EXIT_FAILURE );
360 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
362 ldap_perror( ld, "ldap_bind" );
363 return( EXIT_FAILURE );
370 LDAPControl *ctrls[2];
374 c.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
375 c.ldctl_value.bv_val = NULL;
376 c.ldctl_value.bv_len = 0;
377 c.ldctl_iscritical = manageDSAit > 1;
379 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, &ctrls );
381 if( err != LDAP_OPT_SUCCESS ) {
382 fprintf( stderr, "Could not set ManageDSAit %scontrol\n",
383 c.ldctl_iscritical ? "critical " : "" );
384 if( c.ldctl_iscritical ) {
385 exit( EXIT_FAILURE );
392 for ( ; optind < argc; ++optind ) {
393 rc = dodelete( ld, argv[ optind ] );
396 while ((rc == 0 || contoper) && fgets(buf, sizeof(buf), fp) != NULL) {
397 buf[ strlen( buf ) - 1 ] = '\0'; /* remove trailing newline */
398 if ( *buf != '\0' ) {
399 rc = dodelete( ld, buf );
416 char *matcheddn = NULL, *text = NULL, **refs = NULL;
420 printf( "%sdeleting entry \"%s\"\n",
421 (not ? "!" : ""), dn );
428 /* If prune is on, remove a whole subtree. Delete the children of the
429 * DN recursively, then the DN requested.
431 if ( prune ) deletechildren( ld, dn );
433 rc = ldap_delete_ext( ld, dn, NULL, NULL, &id );
434 if ( rc != LDAP_SUCCESS ) {
435 fprintf( stderr, "ldapdelete: ldap_delete_ext: %s (%d)\n",
436 ldap_err2string( rc ), rc );
440 rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, NULL, &res );
442 ldap_perror( ld, "ldapdelete: ldap_result" );
446 rc = ldap_parse_result( ld, res, &code, &matcheddn, &text, &refs, NULL, 1 );
448 if( rc != LDAP_SUCCESS ) {
449 fprintf( stderr, "ldapdelete: ldap_parse_result: %s (%d)\n",
450 ldap_err2string( rc ), rc );
454 if( verbose || code != LDAP_SUCCESS ||
455 (matcheddn && *matcheddn) || (text && *text) || (refs && *refs) )
457 printf( "Delete Result: %s (%d)\n", ldap_err2string( code ), code );
459 if( text && *text ) {
460 printf( "Additional info: %s\n", text );
463 if( matcheddn && *matcheddn ) {
464 printf( "Matched DN: %s\n", matcheddn );
469 for( i=0; refs[i]; i++ ) {
470 printf("Referral: %s\n", refs[i] );
476 ber_memfree( matcheddn );
477 ber_memvfree( (void **) refs );
483 * Delete all the children of an entry recursively until leaf nodes are reached.
486 static int deletechildren(
490 LDAPMessage *res, *e;
493 static char *attrs[] = { "1.1", NULL };
495 if ( verbose ) printf ( "deleting children of: %s\n", dn );
497 * Do a one level search at dn for children. For each, delete its children.
500 rc = ldap_search_ext_s( ld, dn, LDAP_SCOPE_ONELEVEL, NULL, attrs, 1,
501 NULL, NULL, NULL, -1, &res );
502 if ( rc != LDAP_SUCCESS ) {
503 ldap_perror( ld, "ldap_search" );
507 entries = ldap_count_entries( ld, res );
512 for (e = ldap_first_entry( ld, res ), i = 0; e != NULL;
513 e = ldap_next_entry( ld, e ), i++ )
515 char *dn = ldap_get_dn( ld, e );
518 ldap_perror( ld, "ldap_prune" );
519 ldap_get_option( ld, LDAP_OPT_ERROR_NUMBER, &rc );
524 rc = deletechildren( ld, dn );
526 ldap_perror( ld, "ldap_prune" );
532 printf( "\tremoving %s\n", dn );
535 rc = ldap_delete_s( ld, dn );
537 ldap_perror( ld, "ldap_delete" );
544 printf( "\t%s removed\n", dn );