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((
39 fprintf( stderr, _("Delete entries from an LDAP server\n\n"));
40 fprintf( stderr, _("usage: %s [options] [dn]...\n"), prog);
41 fprintf( stderr, _(" dn: list of DNs to delete. If not given, it will be readed from stdin\n"));
42 fprintf( stderr, _(" or from the file specified with \"-f file\".\n"));
43 fprintf( stderr, _("Delete Options:\n"));
44 fprintf( stderr, _(" -r delete recursively\n"));
50 const char options[] = "r"
51 "cCd:D:e:f:h:H:IkKMnO:p:P:QR:U:vVw:WxX:y:Y:Z";
54 handle_private_option( int i )
59 char *control, *cvalue;
60 case 'E': /* delete controls */
61 if( protocol == LDAP_VERSION2 ) {
62 fprintf( stderr, _("%s: -E incompatible with LDAPv%d\n"),
67 /* should be extended to support comma separated list of
68 * [!]key[=value] parameters, e.g. -E !foo,bar=567
73 if( optarg[0] == '!' ) {
78 control = strdup( optarg );
79 if ( (cvalue = strchr( control, '=' )) != NULL ) {
82 fprintf( stderr, _("Invalid delete control name: %s\n"), control );
98 private_conn_setup( LDAP *ld )
100 /* this seems prudent for searches below */
101 int deref = LDAP_DEREF_NEVER;
102 ldap_set_option( ld, LDAP_OPT_DEREF, &deref );
107 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 ( assertion || authzid || manageDSAit || noop ) {
147 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 );