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(( LDAP *ld,
45 usage( const char *s )
48 "Delete entries from an LDAP server\n\n"
49 "usage: %s [options] [dn]...\n"
50 " dn: list of DNs to delete. If not given, it will be readed from stdin\n"
51 " or from the file specified with \"-f file\".\n"
53 " -c\t\tcontinuous operation mode (do not stop on errors)\n"
54 " -d level\tset LDAP debugging level to `level'\n"
55 " -D binddn\tbind DN\n"
56 " -E\t\trequest SASL privacy (-EE to make it critical)\n"
57 " -f file\t\tdelete DNs listed in `file'\n"
58 " -h host\t\tLDAP server\n"
59 " -I\t\trequest SASL integrity checking (-II to make it\n"
61 " -k\t\tuse Kerberos authentication\n"
62 " -K\t\tlike -k, but do only step 1 of the Kerberos bind\n"
63 " -M\t\tenable Manage DSA IT control (-MM to make it critical)\n"
64 " -n\t\tshow what would be done but don't actually delete\n"
65 " -p port\t\tport on LDAP server\n"
66 " -P version\tprocotol version (2 or 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, want_bindpw, version, debug, manageDSAit;
88 not = verbose = contoper = want_bindpw = debug = manageDSAit = 0;
90 authmethod = LDAP_AUTH_SIMPLE;
93 while (( i = getopt( argc, argv, "cD:d:Ef:h:IKkMnP: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 */
114 case 'h': /* ldap host */
115 ldaphost = strdup( optarg );
117 case 'D': /* bind DN */
118 binddn = strdup( optarg );
120 case 'w': /* password */
121 passwd.bv_val = strdup( optarg );
125 for( p = optarg; *p == '\0'; p++ ) {
129 passwd.bv_len = strlen( passwd.bv_val );
131 case 'f': /* read DNs from a file */
132 if (( fp = fopen( optarg, "r" )) == NULL ) {
134 exit( EXIT_FAILURE );
138 debug |= atoi( optarg );
141 ldapport = atoi( optarg );
143 case 'n': /* print deletes, don't actually do them */
149 case 'v': /* verbose mode */
153 /* enable Manage DSA IT */
160 switch( atoi(optarg) )
163 version = LDAP_VERSION2;
166 version = LDAP_VERSION3;
169 fprintf( stderr, "protocol version should be 2 or 3\n" );
171 return( EXIT_FAILURE );
175 #ifdef HAVE_CYRUS_SASL
177 authmethod = LDAP_AUTH_SASL;
179 fprintf( stderr, "%s was not compiled with SASL support\n",
181 return( EXIT_FAILURE );
185 #ifdef HAVE_CYRUS_SASL
187 authmethod = LDAP_AUTH_SASL;
189 fprintf( stderr, "%s was not compiled with SASL support\n",
191 return( EXIT_FAILURE );
195 #ifdef HAVE_CYRUS_SASL
196 if ( strcasecmp( optarg, "any" ) && strcmp( optarg, "*" ) ) {
197 sasl_mech = strdup( optarg );
199 authmethod = LDAP_AUTH_SASL;
201 fprintf( stderr, "%s was not compiled with SASL support\n",
203 return( EXIT_FAILURE );
207 #ifdef HAVE_CYRUS_SASL
208 sasl_authc_id = strdup( optarg );
209 authmethod = LDAP_AUTH_SASL;
211 fprintf( stderr, "%s was not compiled with SASL support\n",
213 return( EXIT_FAILURE );
217 #ifdef HAVE_CYRUS_SASL
218 sasl_authz_id = strdup( optarg );
219 authmethod = LDAP_AUTH_SASL;
221 fprintf( stderr, "%s was not compiled with SASL support\n",
223 return( EXIT_FAILURE );
230 fprintf( stderr, "%s was not compiled with TLS support\n",
232 return( EXIT_FAILURE );
237 return( EXIT_FAILURE );
241 if ( ( authmethod == LDAP_AUTH_KRBV4 ) || ( authmethod ==
242 LDAP_AUTH_KRBV41 ) ) {
243 if( version > LDAP_VERSION2 ) {
244 fprintf( stderr, "Kerberos requires LDAPv2\n" );
245 return( EXIT_FAILURE );
247 version = LDAP_VERSION2;
249 else if ( authmethod == LDAP_AUTH_SASL ) {
250 if( version != -1 && version != LDAP_VERSION3 ) {
251 fprintf( stderr, "SASL requires LDAPv3\n" );
252 return( EXIT_FAILURE );
254 version = LDAP_VERSION3;
258 if( version != -1 && version != LDAP_VERSION3 ) {
259 fprintf(stderr, "manage DSA control requires LDAPv3\n");
262 version = LDAP_VERSION3;
266 if( version != -1 && version != LDAP_VERSION3 ) {
267 fprintf(stderr, "Start TLS requires LDAPv3\n");
270 version = LDAP_VERSION3;
274 if ( optind >= argc ) {
280 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
281 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
283 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
284 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
289 (void) SIGNAL( SIGPIPE, SIG_IGN );
292 if (( ld = ldap_init( ldaphost, ldapport )) == NULL ) {
293 perror( "ldap_init" );
294 return( EXIT_FAILURE );
298 /* this seems prudent */
299 int deref = LDAP_DEREF_NEVER;
300 ldap_set_option( ld, LDAP_OPT_DEREF, &deref );
303 /* don't chase referrals */
304 ldap_set_option( ld, LDAP_OPT_REFERRALS, LDAP_OPT_OFF );
307 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version ) != LDAP_OPT_SUCCESS)
309 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n", version );
312 if ( use_tls && ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS ) {
314 ldap_perror( ld, "ldap_start_tls" );
315 return( EXIT_FAILURE );
320 passwd.bv_val = getpassphrase("Enter LDAP Password: ");
321 passwd.bv_len = strlen( passwd.bv_val );
324 if ( authmethod == LDAP_AUTH_SASL ) {
325 #ifdef HAVE_CYRUS_SASL
326 int minssf = 0, maxssf = 0;
328 if ( sasl_integrity > 0 )
330 if ( sasl_integrity > 1 )
332 if ( sasl_privacy > 0 )
333 maxssf = 100000; /* Something big value */
334 if ( sasl_privacy > 1 )
337 if ( ldap_set_option( ld, LDAP_OPT_X_SASL_MINSSF,
338 (void *)&minssf ) != LDAP_OPT_SUCCESS ) {
339 fprintf( stderr, "Could not set LDAP_OPT_X_SASL_MINSSF"
341 return( EXIT_FAILURE );
343 if ( ldap_set_option( ld, LDAP_OPT_X_SASL_MAXSSF,
344 (void *)&maxssf ) != LDAP_OPT_SUCCESS ) {
345 fprintf( stderr, "Could not set LDAP_OPT_X_SASL_MAXSSF"
347 return( EXIT_FAILURE );
350 rc = ldap_negotiated_sasl_bind_s( ld, binddn, sasl_authc_id,
351 sasl_authz_id, sasl_mech,
352 passwd.bv_len ? &passwd : NULL,
355 if( rc != LDAP_SUCCESS ) {
356 ldap_perror( ld, "ldap_negotiated_sasl_bind_s" );
357 return( EXIT_FAILURE );
360 fprintf( stderr, "%s was not compiled with SASL support\n",
362 return( EXIT_FAILURE );
366 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
368 ldap_perror( ld, "ldap_bind" );
369 return( EXIT_FAILURE );
376 LDAPControl *ctrls[2];
380 c.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
381 c.ldctl_value.bv_val = NULL;
382 c.ldctl_value.bv_len = 0;
383 c.ldctl_iscritical = manageDSAit > 1;
385 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, &ctrls );
387 if( err != LDAP_OPT_SUCCESS ) {
388 fprintf( stderr, "Could not set Manage DSA IT Control\n" );
389 if( c.ldctl_iscritical ) {
390 exit( EXIT_FAILURE );
397 for ( ; optind < argc; ++optind ) {
398 rc = dodelete( ld, argv[ optind ] );
401 while ((rc == 0 || contoper) && fgets(buf, sizeof(buf), fp) != NULL) {
402 buf[ strlen( buf ) - 1 ] = '\0'; /* remove trailing newline */
403 if ( *buf != '\0' ) {
404 rc = dodelete( ld, buf );
422 printf( "%sdeleting entry \"%s\"\n",
423 (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 );
432 if (( rc = ldap_delete_s( ld, dn )) != LDAP_SUCCESS ) {
433 ldap_perror( ld, "ldap_delete" );
434 } else if ( verbose ) {
435 printf( "\tremoved\n" );
443 * Delete all the children of an entry recursively until leaf nodes are reached.
446 static int deletechildren( LDAP *ld,
449 LDAPMessage *res, *e;
452 int timeout = 30 * 10000;
454 ldap_set_option( ld, LDAP_OPT_TIMEOUT, &timeout );
455 if ( verbose ) printf ( "deleting children of: %s\n", dn );
457 * Do a one level search at dn for children. For each, delete its children.
459 if ( ldap_search_s( ld, dn, LDAP_SCOPE_ONELEVEL, NULL, NULL, 0, &res ) == -1 )
461 ldap_perror( ld, "ldap_search" );
462 ldap_get_option( ld, LDAP_OPT_ERROR_NUMBER, &rc );
466 entries = ldap_count_entries( ld, res );
471 for (e = ldap_first_entry( ld, res ), i = 0; e != NULL;
472 e = ldap_next_entry( ld, e ), i++ )
474 if ( (rc = deletechildren( ld, ldap_get_dn( ld, e) )) == -1 )
476 ldap_perror( ld, "ldap_prune" );
481 printf( "\tremoving %s\n", ldap_get_dn( ld, e ) );
483 if ( ( rc = ldap_delete_s( ld, ldap_get_dn( ld, e ) ) ) == -1 )
485 ldap_perror( ld, "ldap_delete" );
490 printf( "\t%s removed\n", ldap_get_dn( ld, e ) );