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-2014 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"
54 static int sizelimit = -1;
57 static int dodelete LDAP_P((
61 static int deletechildren LDAP_P((
69 fprintf( stderr, _("Delete entries from an LDAP server\n\n"));
70 fprintf( stderr, _("usage: %s [options] [dn]...\n"), prog);
71 fprintf( stderr, _(" dn: list of DNs to delete. If not given, it will be readed from stdin\n"));
72 fprintf( stderr, _(" or from the file specified with \"-f file\".\n"));
73 fprintf( stderr, _("Delete Options:\n"));
74 fprintf( stderr, _(" -c continuous operation mode (do not stop on errors)\n"));
75 fprintf( stderr, _(" -f file read operations from `file'\n"));
76 fprintf( stderr, _(" -M enable Manage DSA IT control (-MM to make critical)\n"));
77 fprintf( stderr, _(" -P version protocol version (default: 3)\n"));
78 fprintf( stderr, _(" -r delete recursively\n"));
84 const char options[] = "r"
85 "cd:D:e:f:h:H:IMnNO:o:p:P:QR:U:vVw:WxX:y:Y:z:Z";
88 handle_private_option( int i )
95 char *control, *cvalue;
96 case 'E': /* delete extensions */
97 if( protocol == LDAP_VERSION2 ) {
98 fprintf( stderr, _("%s: -E incompatible with LDAPv%d\n"),
100 exit( EXIT_FAILURE );
103 /* should be extended to support comma separated list of
104 * [!]key[=value] parameters, e.g. -E !foo,bar=567
109 if( optarg[0] == '!' ) {
114 control = strdup( optarg );
115 if ( (cvalue = strchr( control, '=' )) != NULL ) {
118 fprintf( stderr, _("Invalid delete extension name: %s\n"), control );
126 case 'z': /* size limit */
127 if ( strcasecmp( optarg, "none" ) == 0 ) {
130 } else if ( strcasecmp( optarg, "max" ) == 0 ) {
131 sizelimit = LDAP_MAXINT;
134 ival = strtol( optarg, &next, 10 );
135 if ( next == NULL || next[0] != '\0' ) {
137 _("Unable to parse size limit \"%s\"\n"), optarg );
138 exit( EXIT_FAILURE );
142 if( sizelimit < 0 || sizelimit > LDAP_MAXINT ) {
143 fprintf( stderr, _("%s: invalid sizelimit (%d) specified\n"),
145 exit( EXIT_FAILURE );
157 private_conn_setup( LDAP *ld )
159 /* this seems prudent for searches below */
160 int deref = LDAP_DEREF_NEVER;
161 ldap_set_option( ld, LDAP_OPT_DEREF, &deref );
166 main( int argc, char **argv )
173 tool_init( TOOL_DELETE );
174 prog = lutil_progname( "ldapdelete", argc, argv );
176 tool_args( argc, argv );
178 if ( infile != NULL ) {
179 if (( fp = fopen( infile, "r" )) == NULL ) {
181 exit( EXIT_FAILURE );
184 if ( optind >= argc ) {
189 ld = tool_conn_setup( 0, &private_conn_setup );
193 tool_server_controls( ld, NULL, 0 );
198 for ( ; optind < argc; ++optind ) {
199 rc = dodelete( ld, argv[ optind ] );
201 /* Stop on error and no -c option */
204 if( contoper == 0 ) break;
208 while ((rc == 0 || contoper) && fgets(buf, sizeof(buf), fp) != NULL) {
209 buf[ strlen( buf ) - 1 ] = '\0'; /* remove trailing newline */
211 if ( *buf != '\0' ) {
212 rc = dodelete( ld, buf );
221 tool_exit( ld, retval );
231 char *matcheddn = NULL, *text = NULL, **refs = NULL;
232 LDAPControl **ctrls = NULL;
237 printf( _("%sdeleting entry \"%s\"\n"),
238 (dont ? "!" : ""), dn );
245 /* If prune is on, remove a whole subtree. Delete the children of the
246 * DN recursively, then the DN requested.
250 deletechildren( ld, dn, subentries );
253 rc = ldap_delete_ext( ld, dn, NULL, NULL, &id );
254 if ( rc != LDAP_SUCCESS ) {
255 fprintf( stderr, "%s: ldap_delete_ext: %s (%d)\n",
256 prog, ldap_err2string( rc ), rc );
263 if ( tool_check_abandon( ld, id ) ) {
264 return LDAP_CANCELLED;
270 rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, &tv, &res );
272 tool_perror( "ldap_result", rc, NULL, NULL, NULL, NULL );
281 rc = ldap_parse_result( ld, res, &code, &matcheddn, &text, &refs, &ctrls, 1 );
287 case LDAP_NOT_ALLOWED_ON_NONLEAF:
288 if ( prune && !subentries ) {
295 fprintf( stderr, "%s: ldap_parse_result: %s (%d)\n",
296 prog, ldap_err2string( rc ), rc );
300 if( code != LDAP_SUCCESS ) {
301 tool_perror( "ldap_delete", code, NULL, matcheddn, text, refs );
302 } else if ( verbose &&
303 ((matcheddn && *matcheddn) || (text && *text) || (refs && *refs) ))
305 printf( _("Delete Result: %s (%d)\n"),
306 ldap_err2string( code ), code );
308 if( text && *text ) {
309 printf( _("Additional info: %s\n"), text );
312 if( matcheddn && *matcheddn ) {
313 printf( _("Matched DN: %s\n"), matcheddn );
318 for( i=0; refs[i]; i++ ) {
319 printf(_("Referral: %s\n"), refs[i] );
325 tool_print_ctrls( ld, ctrls );
326 ldap_controls_free( ctrls );
330 ber_memfree( matcheddn );
331 ber_memvfree( (void **) refs );
337 * Delete all the children of an entry recursively until leaf nodes are reached.
339 static int deletechildren(
344 LDAPMessage *res, *e;
346 int rc = LDAP_SUCCESS, srch_rc;
347 static char *attrs[] = { LDAP_NO_ATTRS, NULL };
348 LDAPControl c, *ctrls[2], **ctrlsp = NULL;
349 BerElement *ber = NULL;
351 if ( verbose ) printf ( _("deleting children of: %s\n"), base );
355 * Do a one level search at base for subentry children.
358 if ((ber = ber_alloc_t(LBER_USE_DER)) == NULL) {
361 rc = ber_printf( ber, "b", 1 );
364 fprintf( stderr, _("Subentries control encoding error!\n"));
367 if ( ber_flatten2( ber, &c.ldctl_value, 0 ) == -1 ) {
370 c.ldctl_oid = LDAP_CONTROL_SUBENTRIES;
371 c.ldctl_iscritical = 1;
378 * Do a one level search at base for children. For each, delete its children.
381 srch_rc = ldap_search_ext_s( ld, base, LDAP_SCOPE_ONELEVEL, NULL, attrs, 1,
382 ctrlsp, NULL, NULL, sizelimit, &res );
385 case LDAP_SIZELIMIT_EXCEEDED:
388 tool_perror( "ldap_search", srch_rc, NULL, NULL, NULL, NULL );
392 entries = ldap_count_entries( ld, res );
397 for (e = ldap_first_entry( ld, res ), i = 0; e != NULL;
398 e = ldap_next_entry( ld, e ), i++ )
400 char *dn = ldap_get_dn( ld, e );
403 ldap_get_option( ld, LDAP_OPT_RESULT_CODE, &rc );
404 tool_perror( "ldap_prune", rc, NULL, NULL, NULL, NULL );
409 rc = deletechildren( ld, dn, 0 );
410 if ( rc != LDAP_SUCCESS ) {
411 tool_perror( "ldap_prune", rc, NULL, NULL, NULL, NULL );
417 printf( _("\tremoving %s\n"), dn );
420 rc = ldap_delete_ext_s( ld, dn, NULL, NULL );
421 if ( rc != LDAP_SUCCESS ) {
422 tool_perror( "ldap_delete", rc, NULL, NULL, NULL, NULL );
429 printf( _("\t%s removed\n"), dn );
438 if ( srch_rc == LDAP_SIZELIMIT_EXCEEDED ) {