1 /* ldapdelete.c - simple program to delete an entry using LDAP */
4 * Copyright 1998-2002 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" "cCd:D:e:f:h:H:IkKMnO:p:P:QR:U:vw:WxX:y:Y:Z";
55 handle_private_option( int i )
60 char *control, *cvalue;
61 case 'E': /* delete controls */
62 if( version == LDAP_VERSION2 ) {
63 fprintf( stderr, "%s: -E incompatible with LDAPv%d\n",
68 /* should be extended to support comma separated list of
69 * [!]key[=value] parameters, e.g. -E !foo,bar=567
74 if( optarg[0] == '!' ) {
79 control = strdup( optarg );
80 if ( (cvalue = strchr( control, '=' )) != NULL ) {
83 fprintf( stderr, "Invalid delete control name: %s\n", control );
99 private_conn_setup( LDAP *ld )
101 /* this seems prudent for searches below */
102 int deref = LDAP_DEREF_NEVER;
103 ldap_set_option( ld, LDAP_OPT_DEREF, &deref );
108 main( int argc, char **argv )
117 prog = lutil_progname( "ldapdelete", argc, argv );
119 tool_args( argc, argv );
121 if ( infile != NULL ) {
122 if (( fp = fopen( infile, "r" )) == NULL ) {
124 exit( EXIT_FAILURE );
127 if ( optind >= argc ) {
132 ld = tool_conn_setup( 0, &private_conn_setup );
134 if ( pw_file || want_bindpw ) {
136 rc = lutil_get_filed_password( pw_file, &passwd );
137 if( rc ) return EXIT_FAILURE;
139 passwd.bv_val = getpassphrase( "Enter LDAP Password: " );
140 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
146 if ( authzid || manageDSAit || noop )
147 tool_server_controls( ld, NULL, 0 );
152 for ( ; optind < argc; ++optind ) {
153 rc = dodelete( ld, argv[ optind ] );
155 /* Stop on error and no -c option */
158 if( contoper == 0 ) break;
162 while ((rc == 0 || contoper) && fgets(buf, sizeof(buf), fp) != NULL) {
163 buf[ strlen( buf ) - 1 ] = '\0'; /* remove trailing newline */
165 if ( *buf != '\0' ) {
166 rc = dodelete( ld, buf );
185 char *matcheddn = NULL, *text = NULL, **refs = NULL;
189 printf( "%sdeleting entry \"%s\"\n",
190 (not ? "!" : ""), dn );
197 /* If prune is on, remove a whole subtree. Delete the children of the
198 * DN recursively, then the DN requested.
200 if ( prune ) deletechildren( ld, dn );
202 rc = ldap_delete_ext( ld, dn, NULL, NULL, &id );
203 if ( rc != LDAP_SUCCESS ) {
204 fprintf( stderr, "%s: ldap_delete_ext: %s (%d)\n",
205 prog, ldap_err2string( rc ), rc );
209 rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, NULL, &res );
211 ldap_perror( ld, "ldapdelete: ldap_result" );
215 rc = ldap_parse_result( ld, res, &code, &matcheddn, &text, &refs, NULL, 1 );
217 if( rc != LDAP_SUCCESS ) {
218 fprintf( stderr, "%s: ldap_parse_result: %s (%d)\n",
219 prog, ldap_err2string( rc ), rc );
223 if( verbose || code != LDAP_SUCCESS ||
224 (matcheddn && *matcheddn) || (text && *text) || (refs && *refs) )
226 printf( "Delete Result: %s (%d)\n", ldap_err2string( code ), code );
228 if( text && *text ) {
229 printf( "Additional info: %s\n", text );
232 if( matcheddn && *matcheddn ) {
233 printf( "Matched DN: %s\n", matcheddn );
238 for( i=0; refs[i]; i++ ) {
239 printf("Referral: %s\n", refs[i] );
245 ber_memfree( matcheddn );
246 ber_memvfree( (void **) refs );
252 * Delete all the children of an entry recursively until leaf nodes are reached.
255 static int deletechildren(
259 LDAPMessage *res, *e;
262 static char *attrs[] = { "1.1", NULL };
264 if ( verbose ) printf ( "deleting children of: %s\n", dn );
266 * Do a one level search at dn for children. For each, delete its children.
269 rc = ldap_search_ext_s( ld, dn, LDAP_SCOPE_ONELEVEL, NULL, attrs, 1,
270 NULL, NULL, NULL, -1, &res );
271 if ( rc != LDAP_SUCCESS ) {
272 ldap_perror( ld, "ldap_search" );
276 entries = ldap_count_entries( ld, res );
281 for (e = ldap_first_entry( ld, res ), i = 0; e != NULL;
282 e = ldap_next_entry( ld, e ), i++ )
284 char *dn = ldap_get_dn( ld, e );
287 ldap_perror( ld, "ldap_prune" );
288 ldap_get_option( ld, LDAP_OPT_ERROR_NUMBER, &rc );
293 rc = deletechildren( ld, dn );
295 ldap_perror( ld, "ldap_prune" );
301 printf( "\tremoving %s\n", dn );
304 rc = ldap_delete_s( ld, dn );
306 ldap_perror( ld, "ldap_delete" );
313 printf( "\t%s removed\n", dn );