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-2009 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 );
191 if ( pw_file || want_bindpw ) {
193 rc = lutil_get_filed_password( pw_file, &passwd );
195 if ( fp && fp != stdin )
200 passwd.bv_val = getpassphrase( _("Enter LDAP Password: ") );
201 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
207 tool_server_controls( ld, NULL, 0 );
212 for ( ; optind < argc; ++optind ) {
213 rc = dodelete( ld, argv[ optind ] );
215 /* Stop on error and no -c option */
218 if( contoper == 0 ) break;
222 while ((rc == 0 || contoper) && fgets(buf, sizeof(buf), fp) != NULL) {
223 buf[ strlen( buf ) - 1 ] = '\0'; /* remove trailing newline */
225 if ( *buf != '\0' ) {
226 rc = dodelete( ld, buf );
247 char *matcheddn = NULL, *text = NULL, **refs = NULL;
248 LDAPControl **ctrls = NULL;
253 printf( _("%sdeleting entry \"%s\"\n"),
254 (dont ? "!" : ""), dn );
261 /* If prune is on, remove a whole subtree. Delete the children of the
262 * DN recursively, then the DN requested.
266 deletechildren( ld, dn, subentries );
269 rc = ldap_delete_ext( ld, dn, NULL, NULL, &id );
270 if ( rc != LDAP_SUCCESS ) {
271 fprintf( stderr, "%s: ldap_delete_ext: %s (%d)\n",
272 prog, ldap_err2string( rc ), rc );
279 if ( tool_check_abandon( ld, id ) ) {
280 return LDAP_CANCELLED;
286 rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, &tv, &res );
288 tool_perror( "ldap_result", rc, NULL, NULL, NULL, NULL );
297 rc = ldap_parse_result( ld, res, &code, &matcheddn, &text, &refs, &ctrls, 1 );
303 case LDAP_NOT_ALLOWED_ON_NONLEAF:
304 if ( prune && !subentries ) {
311 fprintf( stderr, "%s: ldap_parse_result: %s (%d)\n",
312 prog, ldap_err2string( rc ), rc );
316 if( code != LDAP_SUCCESS ) {
317 tool_perror( "ldap_delete", code, NULL, matcheddn, text, refs );
318 } else if ( verbose &&
319 ((matcheddn && *matcheddn) || (text && *text) || (refs && *refs) ))
321 printf( _("Delete Result: %s (%d)\n"),
322 ldap_err2string( code ), code );
324 if( text && *text ) {
325 printf( _("Additional info: %s\n"), text );
328 if( matcheddn && *matcheddn ) {
329 printf( _("Matched DN: %s\n"), matcheddn );
334 for( i=0; refs[i]; i++ ) {
335 printf(_("Referral: %s\n"), refs[i] );
341 tool_print_ctrls( ld, ctrls );
342 ldap_controls_free( ctrls );
346 ber_memfree( matcheddn );
347 ber_memvfree( (void **) refs );
353 * Delete all the children of an entry recursively until leaf nodes are reached.
355 static int deletechildren(
360 LDAPMessage *res, *e;
362 int rc = LDAP_SUCCESS, srch_rc;
363 static char *attrs[] = { LDAP_NO_ATTRS, NULL };
364 LDAPControl c, *ctrls[2], **ctrlsp = NULL;
365 BerElement *ber = NULL;
367 if ( verbose ) printf ( _("deleting children of: %s\n"), base );
371 * Do a one level search at base for subentry children.
374 if ((ber = ber_alloc_t(LBER_USE_DER)) == NULL) {
377 rc = ber_printf( ber, "b", 1 );
380 fprintf( stderr, _("Subentries control encoding error!\n"));
383 if ( ber_flatten2( ber, &c.ldctl_value, 0 ) == -1 ) {
386 c.ldctl_oid = LDAP_CONTROL_SUBENTRIES;
387 c.ldctl_iscritical = 1;
394 * Do a one level search at base for children. For each, delete its children.
397 srch_rc = ldap_search_ext_s( ld, base, LDAP_SCOPE_ONELEVEL, NULL, attrs, 1,
398 ctrlsp, NULL, NULL, sizelimit, &res );
401 case LDAP_SIZELIMIT_EXCEEDED:
404 tool_perror( "ldap_search", srch_rc, NULL, NULL, NULL, NULL );
408 entries = ldap_count_entries( ld, res );
413 for (e = ldap_first_entry( ld, res ), i = 0; e != NULL;
414 e = ldap_next_entry( ld, e ), i++ )
416 char *dn = ldap_get_dn( ld, e );
419 ldap_get_option( ld, LDAP_OPT_RESULT_CODE, &rc );
420 tool_perror( "ldap_prune", rc, NULL, NULL, NULL, NULL );
425 rc = deletechildren( ld, dn, 0 );
426 if ( rc != LDAP_SUCCESS ) {
427 tool_perror( "ldap_prune", rc, NULL, NULL, NULL, NULL );
433 printf( _("\tremoving %s\n"), dn );
436 rc = ldap_delete_ext_s( ld, dn, NULL, NULL );
437 if ( rc != LDAP_SUCCESS ) {
438 tool_perror( "ldap_delete", rc, NULL, NULL, NULL, NULL );
445 printf( _("\t%s removed\n"), dn );
454 if ( srch_rc == LDAP_SIZELIMIT_EXCEEDED ) {