1 /* ldapdelete.c - simple program to delete an entry using LDAP */
4 * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
12 #include <ac/stdlib.h>
14 #include <ac/string.h>
15 #include <ac/unistd.h>
19 #include "lutil_ldap.h"
20 #include "ldap_defaults.h"
28 static int dodelete LDAP_P((
32 static int deletechildren LDAP_P((
40 "Delete entries from an LDAP server\n\n"
41 "usage: %s [options] [dn]...\n"
42 " dn: list of DNs to delete. If not given, it will be readed from stdin\n"
43 " or from the file specified with \"-f file\".\n"
45 " -r delete recursively\n"
52 const char options[] = "r"
53 "cCd:D:e:f:h:H:IkKMnO:p:P:QR:U:vVw:WxX:y:Y:Z";
56 handle_private_option( int i )
61 char *control, *cvalue;
62 case 'E': /* delete controls */
63 if( protocol == LDAP_VERSION2 ) {
64 fprintf( stderr, "%s: -E incompatible with LDAPv%d\n",
69 /* should be extended to support comma separated list of
70 * [!]key[=value] parameters, e.g. -E !foo,bar=567
75 if( optarg[0] == '!' ) {
80 control = strdup( optarg );
81 if ( (cvalue = strchr( control, '=' )) != NULL ) {
84 fprintf( stderr, "Invalid delete control name: %s\n", control );
100 private_conn_setup( LDAP *ld )
102 /* this seems prudent for searches below */
103 int deref = LDAP_DEREF_NEVER;
104 ldap_set_option( ld, LDAP_OPT_DEREF, &deref );
109 main( int argc, char **argv )
118 prog = lutil_progname( "ldapdelete", argc, argv );
120 tool_args( argc, argv );
122 if ( infile != NULL ) {
123 if (( fp = fopen( infile, "r" )) == NULL ) {
125 exit( EXIT_FAILURE );
128 if ( optind >= argc ) {
133 ld = tool_conn_setup( 0, &private_conn_setup );
135 if ( pw_file || want_bindpw ) {
137 rc = lutil_get_filed_password( pw_file, &passwd );
138 if( rc ) return EXIT_FAILURE;
140 passwd.bv_val = getpassphrase( "Enter LDAP Password: " );
141 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
147 if ( authzid || manageDSAit || noop )
148 tool_server_controls( ld, NULL, 0 );
153 for ( ; optind < argc; ++optind ) {
154 rc = dodelete( ld, argv[ optind ] );
156 /* Stop on error and no -c option */
159 if( contoper == 0 ) break;
163 while ((rc == 0 || contoper) && fgets(buf, sizeof(buf), fp) != NULL) {
164 buf[ strlen( buf ) - 1 ] = '\0'; /* remove trailing newline */
166 if ( *buf != '\0' ) {
167 rc = dodelete( ld, buf );
186 char *matcheddn = NULL, *text = NULL, **refs = NULL;
190 printf( "%sdeleting entry \"%s\"\n",
191 (not ? "!" : ""), dn );
198 /* If prune is on, remove a whole subtree. Delete the children of the
199 * DN recursively, then the DN requested.
201 if ( prune ) deletechildren( ld, dn );
203 rc = ldap_delete_ext( ld, dn, NULL, NULL, &id );
204 if ( rc != LDAP_SUCCESS ) {
205 fprintf( stderr, "%s: ldap_delete_ext: %s (%d)\n",
206 prog, ldap_err2string( rc ), rc );
210 rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, NULL, &res );
212 ldap_perror( ld, "ldapdelete: ldap_result" );
216 rc = ldap_parse_result( ld, res, &code, &matcheddn, &text, &refs, NULL, 1 );
218 if( rc != LDAP_SUCCESS ) {
219 fprintf( stderr, "%s: ldap_parse_result: %s (%d)\n",
220 prog, ldap_err2string( rc ), rc );
224 if( verbose || code != LDAP_SUCCESS ||
225 (matcheddn && *matcheddn) || (text && *text) || (refs && *refs) )
227 printf( "Delete Result: %s (%d)\n", ldap_err2string( code ), code );
229 if( text && *text ) {
230 printf( "Additional info: %s\n", text );
233 if( matcheddn && *matcheddn ) {
234 printf( "Matched DN: %s\n", matcheddn );
239 for( i=0; refs[i]; i++ ) {
240 printf("Referral: %s\n", refs[i] );
246 ber_memfree( matcheddn );
247 ber_memvfree( (void **) refs );
253 * Delete all the children of an entry recursively until leaf nodes are reached.
256 static int deletechildren(
260 LDAPMessage *res, *e;
263 static char *attrs[] = { "1.1", NULL };
265 if ( verbose ) printf ( "deleting children of: %s\n", dn );
267 * Do a one level search at dn for children. For each, delete its children.
270 rc = ldap_search_ext_s( ld, dn, LDAP_SCOPE_ONELEVEL, NULL, attrs, 1,
271 NULL, NULL, NULL, -1, &res );
272 if ( rc != LDAP_SUCCESS ) {
273 ldap_perror( ld, "ldap_search" );
277 entries = ldap_count_entries( ld, res );
282 for (e = ldap_first_entry( ld, res ), i = 0; e != NULL;
283 e = ldap_next_entry( ld, e ), i++ )
285 char *dn = ldap_get_dn( ld, e );
288 ldap_perror( ld, "ldap_prune" );
289 ldap_get_option( ld, LDAP_OPT_ERROR_NUMBER, &rc );
294 rc = deletechildren( ld, dn );
296 ldap_perror( ld, "ldap_prune" );
302 printf( "\tremoving %s\n", dn );
305 rc = ldap_delete_s( ld, dn );
307 ldap_perror( ld, "ldap_delete" );
314 printf( "\t%s removed\n", dn );