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-2003 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>
45 #include "lutil_ldap.h"
46 #include "ldap_defaults.h"
54 static int dodelete LDAP_P((
58 static int deletechildren LDAP_P((
65 fprintf( stderr, _("Delete entries from an LDAP server\n\n"));
66 fprintf( stderr, _("usage: %s [options] [dn]...\n"), prog);
67 fprintf( stderr, _(" dn: list of DNs to delete. If not given, it will be readed from stdin\n"));
68 fprintf( stderr, _(" or from the file specified with \"-f file\".\n"));
69 fprintf( stderr, _("Delete Options:\n"));
70 fprintf( stderr, _(" -r delete recursively\n"));
76 const char options[] = "r"
77 "cd:D:e:f:h:H:IkKMnO:p:P:QR:U:vVw:WxX:y:Y:Z";
80 handle_private_option( int i )
85 char *control, *cvalue;
86 case 'E': /* delete controls */
87 if( protocol == LDAP_VERSION2 ) {
88 fprintf( stderr, _("%s: -E incompatible with LDAPv%d\n"),
93 /* should be extended to support comma separated list of
94 * [!]key[=value] parameters, e.g. -E !foo,bar=567
99 if( optarg[0] == '!' ) {
104 control = strdup( optarg );
105 if ( (cvalue = strchr( control, '=' )) != NULL ) {
108 fprintf( stderr, _("Invalid delete control name: %s\n"), control );
124 private_conn_setup( LDAP *ld )
126 /* this seems prudent for searches below */
127 int deref = LDAP_DEREF_NEVER;
128 ldap_set_option( ld, LDAP_OPT_DEREF, &deref );
133 main( int argc, char **argv )
143 prog = lutil_progname( "ldapdelete", argc, argv );
145 tool_args( argc, argv );
147 if ( infile != NULL ) {
148 if (( fp = fopen( infile, "r" )) == NULL ) {
150 exit( EXIT_FAILURE );
153 if ( optind >= argc ) {
158 ld = tool_conn_setup( 0, &private_conn_setup );
160 if ( pw_file || want_bindpw ) {
162 rc = lutil_get_filed_password( pw_file, &passwd );
163 if( rc ) return EXIT_FAILURE;
165 passwd.bv_val = getpassphrase( _("Enter LDAP Password: ") );
166 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
172 if ( assertion || authzid || manageDSAit || noop ) {
173 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 );
200 ldap_unbind_ext( ld, NULL, NULL );
212 char *matcheddn = NULL, *text = NULL, **refs = NULL;
216 printf( _("%sdeleting entry \"%s\"\n"),
217 (not ? "!" : ""), dn );
224 /* If prune is on, remove a whole subtree. Delete the children of the
225 * DN recursively, then the DN requested.
227 if ( prune ) deletechildren( ld, dn );
229 rc = ldap_delete_ext( ld, dn, NULL, NULL, &id );
230 if ( rc != LDAP_SUCCESS ) {
231 fprintf( stderr, "%s: ldap_delete_ext: %s (%d)\n",
232 prog, ldap_err2string( rc ), rc );
236 rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, NULL, &res );
238 ldap_perror( ld, "ldapdelete: ldap_result" );
242 rc = ldap_parse_result( ld, res, &code, &matcheddn, &text, &refs, NULL, 1 );
244 if( rc != LDAP_SUCCESS ) {
245 fprintf( stderr, "%s: ldap_parse_result: %s (%d)\n",
246 prog, ldap_err2string( rc ), rc );
250 if( verbose || code != LDAP_SUCCESS ||
251 (matcheddn && *matcheddn) || (text && *text) || (refs && *refs) )
253 printf( _("Delete Result: %s (%d)\n"), ldap_err2string( code ), code );
255 if( text && *text ) {
256 printf( _("Additional info: %s\n"), text );
259 if( matcheddn && *matcheddn ) {
260 printf( _("Matched DN: %s\n"), matcheddn );
265 for( i=0; refs[i]; i++ ) {
266 printf(_("Referral: %s\n"), refs[i] );
272 ber_memfree( matcheddn );
273 ber_memvfree( (void **) refs );
279 * Delete all the children of an entry recursively until leaf nodes are reached.
281 static int deletechildren(
285 LDAPMessage *res, *e;
288 static char *attrs[] = { LDAP_NO_ATTRS, NULL };
290 if ( verbose ) printf ( _("deleting children of: %s\n"), dn );
292 * Do a one level search at dn for children. For each, delete its children.
295 rc = ldap_search_ext_s( ld, dn, LDAP_SCOPE_ONELEVEL, NULL, attrs, 1,
296 NULL, NULL, NULL, -1, &res );
297 if ( rc != LDAP_SUCCESS ) {
298 ldap_perror( ld, "ldap_search" );
302 entries = ldap_count_entries( ld, res );
307 for (e = ldap_first_entry( ld, res ), i = 0; e != NULL;
308 e = ldap_next_entry( ld, e ), i++ )
310 char *dn = ldap_get_dn( ld, e );
313 ldap_perror( ld, "ldap_prune" );
314 ldap_get_option( ld, LDAP_OPT_ERROR_NUMBER, &rc );
319 rc = deletechildren( ld, dn );
321 ldap_perror( ld, "ldap_prune" );
327 printf( _("\tremoving %s\n"), dn );
330 rc = ldap_delete_s( ld, dn );
332 ldap_perror( ld, "ldap_delete" );
339 printf( _("\t%s removed\n"), dn );