1 /* ldapdelete.c - simple program to delete an entry using LDAP */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2007 The OpenLDAP Foundation.
6 * Portions Copyright 1998-2003 Kurt D. Zeilenga.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted only as authorized by the OpenLDAP
13 * A copy of this license is available in the file LICENSE in the
14 * top-level directory of the distribution or, alternatively, at
15 * <http://www.OpenLDAP.org/license.html>.
17 /* Portions Copyright (c) 1992-1996 Regents of the University of Michigan.
18 * All rights reserved.
20 * Redistribution and use in source and binary forms are permitted
21 * provided that this notice is preserved and that due credit is given
22 * to the University of Michigan at Ann Arbor. The name of the
23 * University may not be used to endorse or promote products derived
24 * from this software without specific prior written permission. This
25 * software is provided ``as is'' without express or implied warranty.
28 * This work was originally developed by the University of Michigan
29 * (as part of U-MICH LDAP). Additional significant contributors
38 #include <ac/stdlib.h>
40 #include <ac/string.h>
41 #include <ac/unistd.h>
42 #include <ac/socket.h>
47 #include "lutil_ldap.h"
48 #include "ldap_defaults.h"
56 static int dodelete LDAP_P((
60 static int deletechildren LDAP_P((
67 fprintf( stderr, _("Delete entries from an LDAP server\n\n"));
68 fprintf( stderr, _("usage: %s [options] [dn]...\n"), prog);
69 fprintf( stderr, _(" dn: list of DNs to delete. If not given, it will be readed from stdin\n"));
70 fprintf( stderr, _(" or from the file specified with \"-f file\".\n"));
71 fprintf( stderr, _("Delete Options:\n"));
72 fprintf( stderr, _(" -r delete recursively\n"));
78 const char options[] = "r"
79 "cd:D:e:f:h:H:IMnO:o:p:P:QR:U:vVw:WxX:y:Y:Z";
82 handle_private_option( int i )
87 char *control, *cvalue;
88 case 'E': /* delete extensions */
89 if( protocol == LDAP_VERSION2 ) {
90 fprintf( stderr, _("%s: -E incompatible with LDAPv%d\n"),
95 /* should be extended to support comma separated list of
96 * [!]key[=value] parameters, e.g. -E !foo,bar=567
101 if( optarg[0] == '!' ) {
106 control = strdup( optarg );
107 if ( (cvalue = strchr( control, '=' )) != NULL ) {
110 fprintf( stderr, _("Invalid delete extension name: %s\n"), control );
126 private_conn_setup( LDAP *ld )
128 /* this seems prudent for searches below */
129 int deref = LDAP_DEREF_NEVER;
130 ldap_set_option( ld, LDAP_OPT_DEREF, &deref );
135 main( int argc, char **argv )
144 tool_init( TOOL_DELETE );
145 prog = lutil_progname( "ldapdelete", argc, argv );
147 tool_args( argc, argv );
149 if ( infile != NULL ) {
150 if (( fp = fopen( infile, "r" )) == NULL ) {
152 exit( EXIT_FAILURE );
155 if ( optind >= argc ) {
160 ld = tool_conn_setup( 0, &private_conn_setup );
162 if ( pw_file || want_bindpw ) {
164 rc = lutil_get_filed_password( pw_file, &passwd );
165 if( rc ) return EXIT_FAILURE;
167 passwd.bv_val = getpassphrase( _("Enter LDAP Password: ") );
168 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
174 tool_server_controls( ld, NULL, 0 );
179 for ( ; optind < argc; ++optind ) {
180 rc = dodelete( ld, argv[ optind ] );
182 /* Stop on error and no -c option */
185 if( contoper == 0 ) break;
189 while ((rc == 0 || contoper) && fgets(buf, sizeof(buf), fp) != NULL) {
190 buf[ strlen( buf ) - 1 ] = '\0'; /* remove trailing newline */
192 if ( *buf != '\0' ) {
193 rc = dodelete( ld, buf );
212 char *matcheddn = NULL, *text = NULL, **refs = NULL;
213 LDAPControl **ctrls = NULL;
217 printf( _("%sdeleting entry \"%s\"\n"),
218 (dont ? "!" : ""), dn );
225 /* If prune is on, remove a whole subtree. Delete the children of the
226 * DN recursively, then the DN requested.
228 if ( prune ) deletechildren( ld, dn );
230 rc = ldap_delete_ext( ld, dn, NULL, NULL, &id );
231 if ( rc != LDAP_SUCCESS ) {
232 fprintf( stderr, "%s: ldap_delete_ext: %s (%d)\n",
233 prog, ldap_err2string( rc ), rc );
240 if ( tool_check_abandon( ld, id ) ) {
241 return LDAP_CANCELLED;
247 rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, &tv, &res );
249 tool_perror( "ldap_result", rc, NULL, NULL, NULL, NULL );
258 rc = ldap_parse_result( ld, res, &code, &matcheddn, &text, &refs, &ctrls, 1 );
260 if( rc != LDAP_SUCCESS ) {
261 fprintf( stderr, "%s: ldap_parse_result: %s (%d)\n",
262 prog, ldap_err2string( rc ), rc );
266 if( code != LDAP_SUCCESS ) {
267 tool_perror( "ldap_delete", code, NULL, matcheddn, text, refs );
268 } else if ( verbose &&
269 ((matcheddn && *matcheddn) || (text && *text) || (refs && *refs) ))
271 printf( _("Delete Result: %s (%d)\n"),
272 ldap_err2string( code ), code );
274 if( text && *text ) {
275 printf( _("Additional info: %s\n"), text );
278 if( matcheddn && *matcheddn ) {
279 printf( _("Matched DN: %s\n"), matcheddn );
284 for( i=0; refs[i]; i++ ) {
285 printf(_("Referral: %s\n"), refs[i] );
291 tool_print_ctrls( ld, ctrls );
292 ldap_controls_free( ctrls );
296 ber_memfree( matcheddn );
297 ber_memvfree( (void **) refs );
303 * Delete all the children of an entry recursively until leaf nodes are reached.
305 static int deletechildren(
309 LDAPMessage *res, *e;
312 static char *attrs[] = { LDAP_NO_ATTRS, NULL };
313 LDAPControl c, *ctrls[2];
314 BerElement *ber = NULL;
317 if ( verbose ) printf ( _("deleting children of: %s\n"), dn );
320 * Do a one level search at dn for children. For each, delete its children.
323 rc = ldap_search_ext_s( ld, dn, LDAP_SCOPE_ONELEVEL, NULL, attrs, 1,
324 NULL, NULL, NULL, -1, &res );
325 if ( rc != LDAP_SUCCESS ) {
326 tool_perror( "ldap_search", rc, NULL, NULL, NULL, NULL );
330 entries = ldap_count_entries( ld, res );
335 for (e = ldap_first_entry( ld, res ), i = 0; e != NULL;
336 e = ldap_next_entry( ld, e ), i++ )
338 char *dn = ldap_get_dn( ld, e );
341 ldap_get_option( ld, LDAP_OPT_RESULT_CODE, &rc );
342 tool_perror( "ldap_prune", rc, NULL, NULL, NULL, NULL );
347 rc = deletechildren( ld, dn );
349 tool_perror( "ldap_prune", rc, NULL, NULL, NULL, NULL );
355 printf( _("\tremoving %s\n"), dn );
358 rc = ldap_delete_ext_s( ld, dn, NULL, NULL );
360 tool_perror( "ldap_delete", rc, NULL, NULL, NULL, NULL );
367 printf( _("\t%s removed\n"), dn );
377 * Do a one level search at dn for subentry children.
380 if ((ber = ber_alloc_t(LBER_USE_DER)) == NULL) {
383 rc = ber_printf( ber, "b", 1 );
386 fprintf( stderr, _("Subentries control encoding error!\n"));
389 if ( ber_flatten2( ber, &c.ldctl_value, 0 ) == -1 ) {
392 c.ldctl_oid = LDAP_CONTROL_SUBENTRIES;
393 c.ldctl_iscritical = 1;
397 rc = ldap_search_ext_s( ld, dn, LDAP_SCOPE_ONELEVEL, NULL, attrs, 1,
398 ctrls, NULL, NULL, -1, &res_se );
399 if ( rc != LDAP_SUCCESS ) {
400 tool_perror( "ldap_search", rc, NULL, NULL, NULL, NULL );
405 entries = ldap_count_entries( ld, res_se );
410 for (e = ldap_first_entry( ld, res_se ), i = 0; e != NULL;
411 e = ldap_next_entry( ld, e ), i++ )
413 char *dn = ldap_get_dn( ld, e );
416 ldap_get_option( ld, LDAP_OPT_RESULT_CODE, &rc );
417 tool_perror( "ldap_prune", rc, NULL, NULL, NULL, NULL );
423 printf( _("\tremoving %s\n"), dn );
426 rc = ldap_delete_ext_s( ld, dn, NULL, NULL );
428 tool_perror( "ldap_delete", rc, NULL, NULL, NULL, NULL );
435 printf( _("\t%s removed\n"), dn );
442 ldap_msgfree( res_se );