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>
21 static char *binddn = NULL;
22 static struct berval passwd = { 0, NULL};
23 static char *ldaphost = NULL;
24 static int ldapport = 0;
26 #ifdef HAVE_CYRUS_SASL
27 static char *sasl_authc_id = NULL;
28 static char *sasl_authz_id = NULL;
29 static char *sasl_mech = NULL;
30 static int sasl_integrity = 0;
31 static int sasl_privacy = 0;
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 " -E\t\trequest SASL privacy (-EE to make it critical)\n"
59 " -f file\t\tdelete DNs listed in `file'\n"
60 " -h host\t\tLDAP server\n"
61 " -I\t\trequest SASL integrity checking (-II to make it\n"
63 " -k\t\tuse Kerberos authentication\n"
64 " -K\t\tlike -k, but do only step 1 of the Kerberos bind\n"
65 " -M\t\tenable Manage DSA IT control (-MM to make it critical)\n"
66 " -n\t\tshow what would be done but don't actually delete\n"
67 " -p port\t\tport on LDAP server\n"
68 " -P version\tprocotol version (default: 3)\n"
69 " -r\t\tdelete recursively\n"
70 " -U user\t\tSASL authentication identity (username)\n"
71 " -v\t\trun in verbose mode (diagnostics to standard output)\n"
72 " -w passwd\tbind passwd (for simple authentication)\n"
73 " -W\t\tprompt for bind passwd\n"
74 " -X id\t\tSASL authorization identity (\"dn:<dn>\" or \"u:<user>\")\n"
75 " -Y mech\t\tSASL mechanism\n"
76 " -Z\t\tissue Start TLS request (-ZZ to require successful response)\n"
84 main( int argc, char **argv )
88 int i, rc, authmethod, referrals, want_bindpw, version, debug, manageDSAit;
90 not = verbose = contoper = want_bindpw = debug = manageDSAit = referrals = 0;
92 authmethod = LDAP_AUTH_SIMPLE;
95 while (( i = getopt( argc, argv, "cCD:d:Ef:h:IKMnP:p:rU:vWw:X:Y:Z" )) != EOF ) {
97 case 'k': /* kerberos bind */
98 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
99 authmethod = LDAP_AUTH_KRBV4;
101 fprintf( stderr, "%s was not compiled with Kerberos support\n", argv[0] );
102 return( EXIT_FAILURE );
105 case 'K': /* kerberos bind, part one only */
106 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
107 authmethod = LDAP_AUTH_KRBV41;
109 fprintf( stderr, "%s was not compiled with Kerberos support\n", argv[0] );
110 return( EXIT_FAILURE );
113 case 'c': /* continuous operation mode */
119 case 'h': /* ldap host */
120 ldaphost = strdup( optarg );
122 case 'D': /* bind DN */
123 binddn = strdup( optarg );
125 case 'w': /* password */
126 passwd.bv_val = strdup( optarg );
130 for( p = optarg; *p == '\0'; p++ ) {
134 passwd.bv_len = strlen( passwd.bv_val );
136 case 'f': /* read DNs from a file */
137 if (( fp = fopen( optarg, "r" )) == NULL ) {
139 exit( EXIT_FAILURE );
143 debug |= atoi( optarg );
146 ldapport = atoi( optarg );
148 case 'n': /* print deletes, don't actually do them */
154 case 'v': /* verbose mode */
158 /* enable Manage DSA IT */
165 switch( atoi(optarg) )
168 version = LDAP_VERSION2;
171 version = LDAP_VERSION3;
174 fprintf( stderr, "protocol version should be 2 or 3\n" );
176 return( EXIT_FAILURE );
180 #ifdef HAVE_CYRUS_SASL
182 authmethod = LDAP_AUTH_SASL;
184 fprintf( stderr, "%s was not compiled with SASL support\n",
186 return( EXIT_FAILURE );
190 #ifdef HAVE_CYRUS_SASL
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 if ( strcasecmp( optarg, "any" ) && strcmp( optarg, "*" ) ) {
202 sasl_mech = strdup( optarg );
204 authmethod = LDAP_AUTH_SASL;
206 fprintf( stderr, "%s was not compiled with SASL support\n",
208 return( EXIT_FAILURE );
212 #ifdef HAVE_CYRUS_SASL
213 sasl_authc_id = strdup( optarg );
214 authmethod = LDAP_AUTH_SASL;
216 fprintf( stderr, "%s was not compiled with SASL support\n",
218 return( EXIT_FAILURE );
222 #ifdef HAVE_CYRUS_SASL
223 sasl_authz_id = strdup( optarg );
224 authmethod = LDAP_AUTH_SASL;
226 fprintf( stderr, "%s was not compiled with SASL support\n",
228 return( EXIT_FAILURE );
235 fprintf( stderr, "%s was not compiled with TLS support\n",
237 return( EXIT_FAILURE );
242 return( EXIT_FAILURE );
246 if ( ( authmethod == LDAP_AUTH_KRBV4 ) || ( authmethod ==
247 LDAP_AUTH_KRBV41 ) ) {
248 if( version > LDAP_VERSION2 ) {
249 fprintf( stderr, "Kerberos requires LDAPv2\n" );
250 return( EXIT_FAILURE );
252 version = LDAP_VERSION2;
254 else if ( authmethod == LDAP_AUTH_SASL ) {
255 if( version != -1 && version != LDAP_VERSION3 ) {
256 fprintf( stderr, "SASL requires LDAPv3\n" );
257 return( EXIT_FAILURE );
259 version = LDAP_VERSION3;
263 if( version != -1 && version != LDAP_VERSION3 ) {
264 fprintf(stderr, "manage DSA control requires LDAPv3\n");
267 version = LDAP_VERSION3;
271 if( version != -1 && version != LDAP_VERSION3 ) {
272 fprintf(stderr, "Start TLS requires LDAPv3\n");
275 version = LDAP_VERSION3;
279 if ( optind >= argc ) {
285 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
286 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
288 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
289 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
294 (void) SIGNAL( SIGPIPE, SIG_IGN );
297 if (( ld = ldap_init( ldaphost, ldapport )) == NULL ) {
298 perror( "ldap_init" );
299 return( EXIT_FAILURE );
303 /* this seems prudent for searches below */
304 int deref = LDAP_DEREF_NEVER;
305 ldap_set_option( ld, LDAP_OPT_DEREF, &deref );
308 /* chase referrals */
309 if( ldap_set_option( ld, LDAP_OPT_REFERRALS,
310 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
312 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
313 referrals ? "on" : "off" );
317 if (version == -1 ) {
321 if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version )
322 != LDAP_OPT_SUCCESS )
324 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
329 if ( use_tls && ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS ) {
331 ldap_perror( ld, "ldap_start_tls" );
334 fprintf( stderr, "WARNING: could not start TLS\n" );
338 passwd.bv_val = getpassphrase("Enter LDAP Password: ");
339 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
342 if ( authmethod == LDAP_AUTH_SASL ) {
343 #ifdef HAVE_CYRUS_SASL
344 int minssf = 0, maxssf = 0;
346 if ( sasl_integrity > 0 )
348 if ( sasl_integrity > 1 )
350 if ( sasl_privacy > 0 )
351 maxssf = 100000; /* Something big value */
352 if ( sasl_privacy > 1 )
355 if ( ldap_set_option( ld, LDAP_OPT_X_SASL_MINSSF,
356 (void *)&minssf ) != LDAP_OPT_SUCCESS ) {
357 fprintf( stderr, "Could not set LDAP_OPT_X_SASL_MINSSF"
359 return( EXIT_FAILURE );
361 if ( ldap_set_option( ld, LDAP_OPT_X_SASL_MAXSSF,
362 (void *)&maxssf ) != LDAP_OPT_SUCCESS ) {
363 fprintf( stderr, "Could not set LDAP_OPT_X_SASL_MAXSSF"
365 return( EXIT_FAILURE );
368 rc = ldap_negotiated_sasl_bind_s( ld, binddn, sasl_authc_id,
369 sasl_authz_id, sasl_mech,
370 passwd.bv_len ? &passwd : NULL,
373 if( rc != LDAP_SUCCESS ) {
374 ldap_perror( ld, "ldap_negotiated_sasl_bind_s" );
375 return( EXIT_FAILURE );
378 fprintf( stderr, "%s was not compiled with SASL support\n",
380 return( EXIT_FAILURE );
384 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
386 ldap_perror( ld, "ldap_bind" );
387 return( EXIT_FAILURE );
394 LDAPControl *ctrls[2];
398 c.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
399 c.ldctl_value.bv_val = NULL;
400 c.ldctl_value.bv_len = 0;
401 c.ldctl_iscritical = manageDSAit > 1;
403 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, &ctrls );
405 if( err != LDAP_OPT_SUCCESS ) {
406 fprintf( stderr, "Could not set ManageDSAit %scontrol\n",
407 c.ldctl_iscritical ? "critical " : "" );
408 if( c.ldctl_iscritical ) {
409 exit( EXIT_FAILURE );
416 for ( ; optind < argc; ++optind ) {
417 rc = dodelete( ld, argv[ optind ] );
420 while ((rc == 0 || contoper) && fgets(buf, sizeof(buf), fp) != NULL) {
421 buf[ strlen( buf ) - 1 ] = '\0'; /* remove trailing newline */
422 if ( *buf != '\0' ) {
423 rc = dodelete( ld, buf );
440 char *matcheddn = NULL, *text = NULL, **refs = NULL;
444 printf( "%sdeleting entry \"%s\"\n",
445 (not ? "!" : ""), dn );
452 /* If prune is on, remove a whole subtree. Delete the children of the
453 * DN recursively, then the DN requested.
455 if ( prune ) deletechildren( ld, dn );
457 rc = ldap_delete_ext( ld, dn, NULL, NULL, &id );
458 if ( rc != LDAP_SUCCESS ) {
459 fprintf( stderr, "ldapdelete: ldap_delete_ext: %s (%d)\n",
460 ldap_err2string( rc ), rc );
464 rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, NULL, &res );
465 if ( rc != LDAP_SUCCESS ) {
466 fprintf( stderr, "ldapdelete: ldap_result: %s (%d)\n",
467 ldap_err2string( rc ), rc );
471 rc = ldap_parse_result( ld, res, &code, &matcheddn, &text, &refs, NULL, 1 );
473 if( rc != LDAP_SUCCESS ) {
474 fprintf( stderr, "ldapdelete: ldap_parse_result: %s (%d)\n",
475 ldap_err2string( rc ), rc );
479 if( verbose || code != LDAP_SUCCESS ||
480 (matcheddn && *matcheddn) || (text && *text) || (refs && *refs) )
482 printf( "Delete Result: %s (%d)\n", ldap_err2string( code ), code );
484 if( text && *text ) {
485 printf( "Additional info: %s\n", text );
488 if( matcheddn && *matcheddn ) {
489 printf( "Matched DN: %s\n", matcheddn );
494 for( i=0; refs[i]; i++ ) {
495 printf("Referral: %s\n", refs[i] );
501 ber_memfree( matcheddn );
502 ber_memvfree( (void **) refs );
508 * Delete all the children of an entry recursively until leaf nodes are reached.
511 static int deletechildren(
515 LDAPMessage *res, *e;
518 static char *attrs[] = { "1.1", NULL };
520 if ( verbose ) printf ( "deleting children of: %s\n", dn );
522 * Do a one level search at dn for children. For each, delete its children.
525 rc = ldap_search_ext_s( ld, dn, LDAP_SCOPE_ONELEVEL, NULL, attrs, 1,
526 NULL, NULL, NULL, -1, &res );
527 if ( rc != LDAP_SUCCESS ) {
528 ldap_perror( ld, "ldap_search" );
532 entries = ldap_count_entries( ld, res );
537 for (e = ldap_first_entry( ld, res ), i = 0; e != NULL;
538 e = ldap_next_entry( ld, e ), i++ )
540 char *dn = ldap_get_dn( ld, e );
543 ldap_perror( ld, "ldap_prune" );
544 ldap_get_option( ld, LDAP_OPT_ERROR_NUMBER, &rc );
549 rc = deletechildren( ld, dn );
551 ldap_perror( ld, "ldap_prune" );
557 printf( "\tremoving %s\n", dn );
560 rc = ldap_delete_s( ld, dn );
562 ldap_perror( ld, "ldap_delete" );
569 printf( "\t%s removed\n", dn );