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>
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 int sasl_integrity = 0;
32 static int sasl_privacy = 0;
34 static int use_tls = 0;
35 static int not, verbose, contoper;
38 static int dodelete LDAP_P((
42 static int deletechildren LDAP_P(( LDAP *ld,
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 " -d level\tset LDAP debugging level to `level'\n"
56 " -D binddn\tbind DN\n"
57 " -E\t\trequest SASL privacy (-EE to make it critical)\n"
58 " -f file\t\tdelete DNs listed in `file'\n"
59 " -h host\t\tLDAP server\n"
60 " -I\t\trequest SASL integrity checking (-II to make it\n"
62 " -k\t\tuse Kerberos authentication\n"
63 " -K\t\tlike -k, but do only step 1 of the Kerberos bind\n"
64 " -M\t\tenable Manage DSA IT control (-MM to make it critical)\n"
65 " -n\t\tshow what would be done but don't actually delete\n"
66 " -p port\t\tport on LDAP server\n"
67 " -P version\tprocotol version (2 or 3)\n"
68 " -r\t\tdelete recursively\n"
69 " -U user\t\tSASL authentication identity (username)\n"
70 " -v\t\trun in verbose mode (diagnostics to standard output)\n"
71 " -w passwd\tbind passwd (for simple authentication)\n"
72 " -W\t\tprompt for bind passwd\n"
73 " -X id\t\tSASL authorization identity (\"dn:<dn>\" or \"u:<user>\")\n"
74 " -Y mech\t\tSASL mechanism\n"
75 " -Z\t\tissue Start TLS request (-ZZ to require successful response)\n"
83 main( int argc, char **argv )
87 int i, rc, authmethod, want_bindpw, version, debug, manageDSAit;
89 not = verbose = contoper = want_bindpw = debug = manageDSAit = 0;
91 authmethod = LDAP_AUTH_SIMPLE;
94 while (( i = getopt( argc, argv, "cD:d:Ef:h:IKkMnP:p:rU:vWw:X:Y:Z" )) != EOF ) {
96 case 'k': /* kerberos bind */
97 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
98 authmethod = LDAP_AUTH_KRBV4;
100 fprintf( stderr, "%s was not compiled with Kerberos support\n", argv[0] );
101 return( EXIT_FAILURE );
104 case 'K': /* kerberos bind, part one only */
105 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
106 authmethod = LDAP_AUTH_KRBV41;
108 fprintf( stderr, "%s was not compiled with Kerberos support\n", argv[0] );
109 return( EXIT_FAILURE );
112 case 'c': /* continuous operation mode */
115 case 'h': /* ldap host */
116 ldaphost = strdup( optarg );
118 case 'D': /* bind DN */
119 binddn = strdup( optarg );
121 case 'w': /* password */
122 passwd.bv_val = strdup( optarg );
126 for( p = optarg; *p == '\0'; p++ ) {
130 passwd.bv_len = strlen( passwd.bv_val );
132 case 'f': /* read DNs from a file */
133 if (( fp = fopen( optarg, "r" )) == NULL ) {
135 exit( EXIT_FAILURE );
139 debug |= atoi( optarg );
142 ldapport = atoi( optarg );
144 case 'n': /* print deletes, don't actually do them */
150 case 'v': /* verbose mode */
154 /* enable Manage DSA IT */
161 switch( atoi(optarg) )
164 version = LDAP_VERSION2;
167 version = LDAP_VERSION3;
170 fprintf( stderr, "protocol version should be 2 or 3\n" );
172 return( EXIT_FAILURE );
176 #ifdef HAVE_CYRUS_SASL
178 authmethod = LDAP_AUTH_SASL;
180 fprintf( stderr, "%s was not compiled with SASL support\n",
182 return( EXIT_FAILURE );
186 #ifdef HAVE_CYRUS_SASL
188 authmethod = LDAP_AUTH_SASL;
190 fprintf( stderr, "%s was not compiled with SASL support\n",
192 return( EXIT_FAILURE );
196 #ifdef HAVE_CYRUS_SASL
197 if ( strcasecmp( optarg, "any" ) && strcmp( optarg, "*" ) ) {
198 sasl_mech = strdup( optarg );
200 authmethod = LDAP_AUTH_SASL;
202 fprintf( stderr, "%s was not compiled with SASL support\n",
204 return( EXIT_FAILURE );
208 #ifdef HAVE_CYRUS_SASL
209 sasl_authc_id = strdup( optarg );
210 authmethod = LDAP_AUTH_SASL;
212 fprintf( stderr, "%s was not compiled with SASL support\n",
214 return( EXIT_FAILURE );
218 #ifdef HAVE_CYRUS_SASL
219 sasl_authz_id = strdup( optarg );
220 authmethod = LDAP_AUTH_SASL;
222 fprintf( stderr, "%s was not compiled with SASL support\n",
224 return( EXIT_FAILURE );
231 fprintf( stderr, "%s was not compiled with TLS support\n",
233 return( EXIT_FAILURE );
238 return( EXIT_FAILURE );
242 if ( ( authmethod == LDAP_AUTH_KRBV4 ) || ( authmethod ==
243 LDAP_AUTH_KRBV41 ) ) {
244 if( version > LDAP_VERSION2 ) {
245 fprintf( stderr, "Kerberos requires LDAPv2\n" );
246 return( EXIT_FAILURE );
248 version = LDAP_VERSION2;
250 else if ( authmethod == LDAP_AUTH_SASL ) {
251 if( version != -1 && version != LDAP_VERSION3 ) {
252 fprintf( stderr, "SASL requires LDAPv3\n" );
253 return( EXIT_FAILURE );
255 version = LDAP_VERSION3;
259 if( version != -1 && version != LDAP_VERSION3 ) {
260 fprintf(stderr, "manage DSA control requires LDAPv3\n");
263 version = LDAP_VERSION3;
267 if( version != -1 && version != LDAP_VERSION3 ) {
268 fprintf(stderr, "Start TLS requires LDAPv3\n");
271 version = LDAP_VERSION3;
275 if ( optind >= argc ) {
281 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
282 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
284 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
285 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
290 (void) SIGNAL( SIGPIPE, SIG_IGN );
293 if (( ld = ldap_init( ldaphost, ldapport )) == NULL ) {
294 perror( "ldap_init" );
295 return( EXIT_FAILURE );
299 /* this seems prudent */
300 int deref = LDAP_DEREF_NEVER;
301 ldap_set_option( ld, LDAP_OPT_DEREF, &deref );
304 /* don't chase referrals */
305 ldap_set_option( ld, LDAP_OPT_REFERRALS, LDAP_OPT_OFF );
308 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version ) != LDAP_OPT_SUCCESS)
310 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n", version );
313 if ( use_tls && ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS ) {
315 ldap_perror( ld, "ldap_start_tls" );
316 return( EXIT_FAILURE );
321 passwd.bv_val = getpassphrase("Enter LDAP Password: ");
322 passwd.bv_len = strlen( passwd.bv_val );
325 if ( authmethod == LDAP_AUTH_SASL ) {
326 #ifdef HAVE_CYRUS_SASL
327 int minssf = 0, maxssf = 0;
329 if ( sasl_integrity > 0 )
331 if ( sasl_integrity > 1 )
333 if ( sasl_privacy > 0 )
334 maxssf = 100000; /* Something big value */
335 if ( sasl_privacy > 1 )
338 if ( ldap_set_option( ld, LDAP_OPT_X_SASL_MINSSF,
339 (void *)&minssf ) != LDAP_OPT_SUCCESS ) {
340 fprintf( stderr, "Could not set LDAP_OPT_X_SASL_MINSSF"
342 return( EXIT_FAILURE );
344 if ( ldap_set_option( ld, LDAP_OPT_X_SASL_MAXSSF,
345 (void *)&maxssf ) != LDAP_OPT_SUCCESS ) {
346 fprintf( stderr, "Could not set LDAP_OPT_X_SASL_MAXSSF"
348 return( EXIT_FAILURE );
351 rc = ldap_negotiated_sasl_bind_s( ld, binddn, sasl_authc_id,
352 sasl_authz_id, sasl_mech,
353 passwd.bv_len ? &passwd : NULL,
356 if( rc != LDAP_SUCCESS ) {
357 ldap_perror( ld, "ldap_negotiated_sasl_bind_s" );
358 return( EXIT_FAILURE );
361 fprintf( stderr, "%s was not compiled with SASL support\n",
363 return( EXIT_FAILURE );
367 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
369 ldap_perror( ld, "ldap_bind" );
370 return( EXIT_FAILURE );
377 LDAPControl *ctrls[2];
381 c.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
382 c.ldctl_value.bv_val = NULL;
383 c.ldctl_value.bv_len = 0;
384 c.ldctl_iscritical = manageDSAit > 1;
386 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, &ctrls );
388 if( err != LDAP_OPT_SUCCESS ) {
389 fprintf( stderr, "Could not set Manage DSA IT Control\n" );
390 if( c.ldctl_iscritical ) {
391 exit( EXIT_FAILURE );
398 for ( ; optind < argc; ++optind ) {
399 rc = dodelete( ld, argv[ optind ] );
402 while ((rc == 0 || contoper) && fgets(buf, sizeof(buf), fp) != NULL) {
403 buf[ strlen( buf ) - 1 ] = '\0'; /* remove trailing newline */
404 if ( *buf != '\0' ) {
405 rc = dodelete( ld, buf );
423 printf( "%sdeleting entry \"%s\"\n",
424 (not ? "!" : ""), dn );
429 /* If prune is on, remove a whole subtree. Delete the children of the
430 * DN recursively, then the DN requested.
432 if ( prune ) deletechildren( ld, dn );
433 if (( rc = ldap_delete_s( ld, dn )) != LDAP_SUCCESS ) {
434 ldap_perror( ld, "ldap_delete" );
435 } else if ( verbose ) {
436 printf( "\tremoved\n" );
444 * Delete all the children of an entry recursively until leaf nodes are reached.
447 static int deletechildren( LDAP *ld,
450 LDAPMessage *res, *e;
453 int timeout = 30 * 10000;
455 ldap_set_option( ld, LDAP_OPT_TIMEOUT, &timeout );
456 if ( verbose ) printf ( "deleting children of: %s\n", dn );
458 * Do a one level search at dn for children. For each, delete its children.
460 if ( ldap_search_s( ld, dn, LDAP_SCOPE_ONELEVEL, NULL, NULL, 0, &res ) == -1 )
462 ldap_perror( ld, "ldap_search" );
463 ldap_get_option( ld, LDAP_OPT_ERROR_NUMBER, &rc );
467 entries = ldap_count_entries( ld, res );
472 for (e = ldap_first_entry( ld, res ), i = 0; e != NULL;
473 e = ldap_next_entry( ld, e ), i++ )
475 if ( (rc = deletechildren( ld, ldap_get_dn( ld, e) )) == -1 )
477 ldap_perror( ld, "ldap_prune" );
482 printf( "\tremoving %s\n", ldap_get_dn( ld, e ) );
484 if ( ( rc = ldap_delete_s( ld, ldap_get_dn( ld, e ) ) ) == -1 )
486 ldap_perror( ld, "ldap_delete" );
491 printf( "\t%s removed\n", ldap_get_dn( ld, e ) );