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"
24 #define _OLV_APP "ldapdelete"
26 #include "ol_version.h"
32 static int dodelete LDAP_P((
36 static int deletechildren LDAP_P((
44 "Delete entries from an LDAP server\n\n"
45 "usage: %s [options] [dn]...\n"
46 " dn: list of DNs to delete. If not given, it will be readed from stdin\n"
47 " or from the file specified with \"-f file\".\n"
49 " -r delete recursively\n"
56 const char options[] = "r"
57 "cCd:D:e:f:h:H:IkKMnO:p:P:QR:U:vVw:WxX:y:Y:Z";
60 handle_private_option( int i )
65 char *control, *cvalue;
66 case 'E': /* delete controls */
67 if( protocol == LDAP_VERSION2 ) {
68 fprintf( stderr, "%s: -E incompatible with LDAPv%d\n",
73 /* should be extended to support comma separated list of
74 * [!]key[=value] parameters, e.g. -E !foo,bar=567
79 if( optarg[0] == '!' ) {
84 control = strdup( optarg );
85 if ( (cvalue = strchr( control, '=' )) != NULL ) {
88 fprintf( stderr, "Invalid delete control name: %s\n", control );
104 private_conn_setup( LDAP *ld )
106 /* this seems prudent for searches below */
107 int deref = LDAP_DEREF_NEVER;
108 ldap_set_option( ld, LDAP_OPT_DEREF, &deref );
113 main( int argc, char **argv )
122 prog = lutil_progname( "ldapdelete", argc, argv );
124 tool_args( argc, argv );
126 if ( infile != NULL ) {
127 if (( fp = fopen( infile, "r" )) == NULL ) {
129 exit( EXIT_FAILURE );
132 if ( optind >= argc ) {
137 ld = tool_conn_setup( 0, &private_conn_setup );
139 if ( pw_file || want_bindpw ) {
141 rc = lutil_get_filed_password( pw_file, &passwd );
142 if( rc ) return EXIT_FAILURE;
144 passwd.bv_val = getpassphrase( "Enter LDAP Password: " );
145 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
151 if ( authzid || manageDSAit || noop )
152 tool_server_controls( ld, NULL, 0 );
157 for ( ; optind < argc; ++optind ) {
158 rc = dodelete( ld, argv[ optind ] );
160 /* Stop on error and no -c option */
163 if( contoper == 0 ) break;
167 while ((rc == 0 || contoper) && fgets(buf, sizeof(buf), fp) != NULL) {
168 buf[ strlen( buf ) - 1 ] = '\0'; /* remove trailing newline */
170 if ( *buf != '\0' ) {
171 rc = dodelete( ld, buf );
190 char *matcheddn = NULL, *text = NULL, **refs = NULL;
194 printf( "%sdeleting entry \"%s\"\n",
195 (not ? "!" : ""), dn );
202 /* If prune is on, remove a whole subtree. Delete the children of the
203 * DN recursively, then the DN requested.
205 if ( prune ) deletechildren( ld, dn );
207 rc = ldap_delete_ext( ld, dn, NULL, NULL, &id );
208 if ( rc != LDAP_SUCCESS ) {
209 fprintf( stderr, "%s: ldap_delete_ext: %s (%d)\n",
210 prog, ldap_err2string( rc ), rc );
214 rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, NULL, &res );
216 ldap_perror( ld, "ldapdelete: ldap_result" );
220 rc = ldap_parse_result( ld, res, &code, &matcheddn, &text, &refs, NULL, 1 );
222 if( rc != LDAP_SUCCESS ) {
223 fprintf( stderr, "%s: ldap_parse_result: %s (%d)\n",
224 prog, ldap_err2string( rc ), rc );
228 if( verbose || code != LDAP_SUCCESS ||
229 (matcheddn && *matcheddn) || (text && *text) || (refs && *refs) )
231 printf( "Delete Result: %s (%d)\n", ldap_err2string( code ), code );
233 if( text && *text ) {
234 printf( "Additional info: %s\n", text );
237 if( matcheddn && *matcheddn ) {
238 printf( "Matched DN: %s\n", matcheddn );
243 for( i=0; refs[i]; i++ ) {
244 printf("Referral: %s\n", refs[i] );
250 ber_memfree( matcheddn );
251 ber_memvfree( (void **) refs );
257 * Delete all the children of an entry recursively until leaf nodes are reached.
260 static int deletechildren(
264 LDAPMessage *res, *e;
267 static char *attrs[] = { "1.1", NULL };
269 if ( verbose ) printf ( "deleting children of: %s\n", dn );
271 * Do a one level search at dn for children. For each, delete its children.
274 rc = ldap_search_ext_s( ld, dn, LDAP_SCOPE_ONELEVEL, NULL, attrs, 1,
275 NULL, NULL, NULL, -1, &res );
276 if ( rc != LDAP_SUCCESS ) {
277 ldap_perror( ld, "ldap_search" );
281 entries = ldap_count_entries( ld, res );
286 for (e = ldap_first_entry( ld, res ), i = 0; e != NULL;
287 e = ldap_next_entry( ld, e ), i++ )
289 char *dn = ldap_get_dn( ld, e );
292 ldap_perror( ld, "ldap_prune" );
293 ldap_get_option( ld, LDAP_OPT_ERROR_NUMBER, &rc );
298 rc = deletechildren( ld, dn );
300 ldap_perror( ld, "ldap_prune" );
306 printf( "\tremoving %s\n", dn );
309 rc = ldap_delete_s( ld, dn );
311 ldap_perror( ld, "ldap_delete" );
318 printf( "\t%s removed\n", dn );