]> git.sur5r.net Git - openldap/blob - clients/tools/ldapdelete.c
backout unintended commit commit
[openldap] / clients / tools / ldapdelete.c
1 /* ldapdelete.c - simple program to delete an entry using LDAP */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1998-2009 The OpenLDAP Foundation.
6  * Portions Copyright 1998-2003 Kurt D. Zeilenga.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted only as authorized by the OpenLDAP
11  * Public License.
12  *
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>.
16  */
17 /* Portions Copyright (c) 1992-1996 Regents of the University of Michigan.
18  * All rights reserved.
19  *
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.
26  */
27 /* ACKNOWLEDGEMENTS:
28  * This work was originally developed by the University of Michigan
29  * (as part of U-MICH LDAP).  Additional significant contributors
30  * include:
31  *   Kurt D. Zeilenga
32  */
33
34 #include "portable.h"
35
36 #include <stdio.h>
37
38 #include <ac/stdlib.h>
39 #include <ac/ctype.h>
40 #include <ac/string.h>
41 #include <ac/unistd.h>
42 #include <ac/socket.h>
43 #include <ac/time.h>
44
45 #include <ldap.h>
46 #include "lutil.h"
47 #include "lutil_ldap.h"
48 #include "ldap_defaults.h"
49
50 #include "common.h"
51
52
53 static int      prune = 0;
54 static int sizelimit = -1;
55
56
57 static int dodelete LDAP_P((
58     LDAP *ld,
59     const char *dn));
60
61 static int deletechildren LDAP_P((
62         LDAP *ld,
63         const char *dn,
64         int subentries ));
65
66 void
67 usage( void )
68 {
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"));
79         tool_common_usage();
80         exit( EXIT_FAILURE );
81 }
82
83
84 const char options[] = "r"
85         "cd:D:e:f:h:H:IMnNO:o:p:P:QR:U:vVw:WxX:y:Y:z:Z";
86
87 int
88 handle_private_option( int i )
89 {
90         int ival;
91         char *next;
92         switch ( i ) {
93 #if 0
94                 int crit;
95                 char *control, *cvalue;
96         case 'E': /* delete extensions */
97                 if( protocol == LDAP_VERSION2 ) {
98                         fprintf( stderr, _("%s: -E incompatible with LDAPv%d\n"),
99                                 prog, protocol );
100                         exit( EXIT_FAILURE );
101                 }
102
103                 /* should be extended to support comma separated list of
104                  *      [!]key[=value] parameters, e.g.  -E !foo,bar=567
105                  */
106
107                 crit = 0;
108                 cvalue = NULL;
109                 if( optarg[0] == '!' ) {
110                         crit = 1;
111                         optarg++;
112                 }
113
114                 control = strdup( optarg );
115                 if ( (cvalue = strchr( control, '=' )) != NULL ) {
116                         *cvalue++ = '\0';
117                 }
118                 fprintf( stderr, _("Invalid delete extension name: %s\n"), control );
119                 usage();
120 #endif
121
122         case 'r':
123                 prune = 1;
124                 break;
125
126         case 'z':       /* size limit */
127                 if ( strcasecmp( optarg, "none" ) == 0 ) {
128                         sizelimit = 0;
129
130                 } else if ( strcasecmp( optarg, "max" ) == 0 ) {
131                         sizelimit = LDAP_MAXINT;
132
133                 } else {
134                         ival = strtol( optarg, &next, 10 );
135                         if ( next == NULL || next[0] != '\0' ) {
136                                 fprintf( stderr,
137                                         _("Unable to parse size limit \"%s\"\n"), optarg );
138                                 exit( EXIT_FAILURE );
139                         }
140                         sizelimit = ival;
141                 }
142                 if( sizelimit < 0 || sizelimit > LDAP_MAXINT ) {
143                         fprintf( stderr, _("%s: invalid sizelimit (%d) specified\n"),
144                                 prog, sizelimit );
145                         exit( EXIT_FAILURE );
146                 }
147                 break;
148
149         default:
150                 return 0;
151         }
152         return 1;
153 }
154
155
156 static void
157 private_conn_setup( LDAP *ld )
158 {
159         /* this seems prudent for searches below */
160         int deref = LDAP_DEREF_NEVER;
161         ldap_set_option( ld, LDAP_OPT_DEREF, &deref );
162 }
163
164
165 int
166 main( int argc, char **argv )
167 {
168         char            buf[ 4096 ];
169         FILE            *fp = NULL;
170         LDAP            *ld;
171         int             rc, retval;
172
173         tool_init( TOOL_DELETE );
174     prog = lutil_progname( "ldapdelete", argc, argv );
175
176         tool_args( argc, argv );
177
178         if ( infile != NULL ) {
179                 if (( fp = fopen( infile, "r" )) == NULL ) {
180                         perror( optarg );
181                         exit( EXIT_FAILURE );
182             }
183         } else {
184                 if ( optind >= argc ) {
185                         fp = stdin;
186                 }
187     }
188
189         ld = tool_conn_setup( 0, &private_conn_setup );
190
191         if ( pw_file || want_bindpw ) {
192                 if ( pw_file ) {
193                         rc = lutil_get_filed_password( pw_file, &passwd );
194                         if( rc ) {
195                                 if ( fp && fp != stdin )
196                                         fclose( fp );
197                                 return EXIT_FAILURE;
198                         }
199                 } else {
200                         passwd.bv_val = getpassphrase( _("Enter LDAP Password: ") );
201                         passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
202                 }
203         }
204
205         tool_bind( ld );
206
207         tool_server_controls( ld, NULL, 0 );
208
209         retval = rc = 0;
210
211         if ( fp == NULL ) {
212                 for ( ; optind < argc; ++optind ) {
213                         rc = dodelete( ld, argv[ optind ] );
214
215                         /* Stop on error and no -c option */
216                         if( rc != 0 ) {
217                                 retval = rc;
218                                 if( contoper == 0 ) break;
219                         }
220                 }
221         } else {
222                 while ((rc == 0 || contoper) && fgets(buf, sizeof(buf), fp) != NULL) {
223                         buf[ strlen( buf ) - 1 ] = '\0'; /* remove trailing newline */
224
225                         if ( *buf != '\0' ) {
226                                 rc = dodelete( ld, buf );
227                                 if ( rc != 0 )
228                                         retval = rc;
229                         }
230                 }
231                 if ( fp != stdin )
232                         fclose( fp );
233         }
234
235         tool_unbind( ld );
236         tool_destroy();
237     return retval;
238 }
239
240
241 static int dodelete(
242     LDAP        *ld,
243     const char  *dn)
244 {
245         int id;
246         int     rc, code;
247         char *matcheddn = NULL, *text = NULL, **refs = NULL;
248         LDAPControl **ctrls = NULL;
249         LDAPMessage *res;
250         int subentries = 0;
251
252         if ( verbose ) {
253                 printf( _("%sdeleting entry \"%s\"\n"),
254                         (dont ? "!" : ""), dn );
255         }
256
257         if ( dont ) {
258                 return LDAP_SUCCESS;
259         }
260
261         /* If prune is on, remove a whole subtree.  Delete the children of the
262          * DN recursively, then the DN requested.
263          */
264         if ( prune ) {
265 retry:;
266                 deletechildren( ld, dn, subentries );
267         }
268
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 );
273                 return rc;
274         }
275
276         for ( ; ; ) {
277                 struct timeval tv;
278
279                 if ( tool_check_abandon( ld, id ) ) {
280                         return LDAP_CANCELLED;
281                 }
282
283                 tv.tv_sec = 0;
284                 tv.tv_usec = 100000;
285
286                 rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, &tv, &res );
287                 if ( rc < 0 ) {
288                         tool_perror( "ldap_result", rc, NULL, NULL, NULL, NULL );
289                         return rc;
290                 }
291
292                 if ( rc != 0 ) {
293                         break;
294                 }
295         }
296
297         rc = ldap_parse_result( ld, res, &code, &matcheddn, &text, &refs, &ctrls, 1 );
298
299         switch ( rc ) {
300         case LDAP_SUCCESS:
301                 break;
302
303         case LDAP_NOT_ALLOWED_ON_NONLEAF:
304                 if ( prune && !subentries ) {
305                         subentries = 1;
306                         goto retry;
307                 }
308                 /* fallthru */
309
310         default:
311                 fprintf( stderr, "%s: ldap_parse_result: %s (%d)\n",
312                         prog, ldap_err2string( rc ), rc );
313                 return rc;
314         }
315
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) ))
320         {
321                 printf( _("Delete Result: %s (%d)\n"),
322                         ldap_err2string( code ), code );
323
324                 if( text && *text ) {
325                         printf( _("Additional info: %s\n"), text );
326                 }
327
328                 if( matcheddn && *matcheddn ) {
329                         printf( _("Matched DN: %s\n"), matcheddn );
330                 }
331
332                 if( refs ) {
333                         int i;
334                         for( i=0; refs[i]; i++ ) {
335                                 printf(_("Referral: %s\n"), refs[i] );
336                         }
337                 }
338         }
339
340         if (ctrls) {
341                 tool_print_ctrls( ld, ctrls );
342                 ldap_controls_free( ctrls );
343         }
344
345         ber_memfree( text );
346         ber_memfree( matcheddn );
347         ber_memvfree( (void **) refs );
348
349         return code;
350 }
351
352 /*
353  * Delete all the children of an entry recursively until leaf nodes are reached.
354  */
355 static int deletechildren(
356         LDAP *ld,
357         const char *base,
358         int subentries )
359 {
360         LDAPMessage *res, *e;
361         int entries;
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;
366
367         if ( verbose ) printf ( _("deleting children of: %s\n"), base );
368
369         if ( subentries ) {
370                 /*
371                  * Do a one level search at base for subentry children.
372                  */
373
374                 if ((ber = ber_alloc_t(LBER_USE_DER)) == NULL) {
375                         return EXIT_FAILURE;
376                 }
377                 rc = ber_printf( ber, "b", 1 );
378                 if ( rc == -1 ) {
379                         ber_free( ber, 1 );
380                         fprintf( stderr, _("Subentries control encoding error!\n"));
381                         return EXIT_FAILURE;
382                 }
383                 if ( ber_flatten2( ber, &c.ldctl_value, 0 ) == -1 ) {
384                         return EXIT_FAILURE;
385                 }
386                 c.ldctl_oid = LDAP_CONTROL_SUBENTRIES;
387                 c.ldctl_iscritical = 1;
388                 ctrls[0] = &c;
389                 ctrls[1] = NULL;
390                 ctrlsp = ctrls;
391         }
392
393         /*
394          * Do a one level search at base for children.  For each, delete its children.
395          */
396 more:;
397         srch_rc = ldap_search_ext_s( ld, base, LDAP_SCOPE_ONELEVEL, NULL, attrs, 1,
398                 ctrlsp, NULL, NULL, sizelimit, &res );
399         switch ( srch_rc ) {
400         case LDAP_SUCCESS:
401         case LDAP_SIZELIMIT_EXCEEDED:
402                 break;
403         default:
404                 tool_perror( "ldap_search", srch_rc, NULL, NULL, NULL, NULL );
405                 return( srch_rc );
406         }
407
408         entries = ldap_count_entries( ld, res );
409
410         if ( entries > 0 ) {
411                 int i;
412
413                 for (e = ldap_first_entry( ld, res ), i = 0; e != NULL;
414                         e = ldap_next_entry( ld, e ), i++ )
415                 {
416                         char *dn = ldap_get_dn( ld, e );
417
418                         if( dn == NULL ) {
419                                 ldap_get_option( ld, LDAP_OPT_RESULT_CODE, &rc );
420                                 tool_perror( "ldap_prune", rc, NULL, NULL, NULL, NULL );
421                                 ber_memfree( dn );
422                                 return rc;
423                         }
424
425                         rc = deletechildren( ld, dn, 0 );
426                         if ( rc != LDAP_SUCCESS ) {
427                                 tool_perror( "ldap_prune", rc, NULL, NULL, NULL, NULL );
428                                 ber_memfree( dn );
429                                 return rc;
430                         }
431
432                         if ( verbose ) {
433                                 printf( _("\tremoving %s\n"), dn );
434                         }
435
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 );
439                                 ber_memfree( dn );
440                                 return rc;
441
442                         }
443                         
444                         if ( verbose ) {
445                                 printf( _("\t%s removed\n"), dn );
446                         }
447
448                         ber_memfree( dn );
449                 }
450         }
451
452         ldap_msgfree( res );
453
454         if ( srch_rc == LDAP_SIZELIMIT_EXCEEDED ) {
455                 goto more;
456         }
457
458         return rc;
459 }