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 ( 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 );