]> git.sur5r.net Git - openldap/blob - clients/tools/ldapdelete.c
ITS#5294
[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-2007 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
65 void
66 usage( void )
67 {
68         fprintf( stderr, _("Delete entries from an LDAP server\n\n"));
69         fprintf( stderr, _("usage: %s [options] [dn]...\n"), prog);
70         fprintf( stderr, _("    dn: list of DNs to delete. If not given, it will be readed from stdin\n"));
71         fprintf( stderr, _("        or from the file specified with \"-f file\".\n"));
72         fprintf( stderr, _("Delete Options:\n"));
73         fprintf( stderr, _("  -r         delete recursively\n"));
74         tool_common_usage();
75         exit( EXIT_FAILURE );
76 }
77
78
79 const char options[] = "r"
80         "cd:D:e:f:h:H:IMnO:o:p:P:QR:U:vVw:WxX:y:Y:z:Z";
81
82 int
83 handle_private_option( int i )
84 {
85         int ival;
86         char *next;
87         switch ( i ) {
88 #if 0
89                 int crit;
90                 char *control, *cvalue;
91         case 'E': /* delete extensions */
92                 if( protocol == LDAP_VERSION2 ) {
93                         fprintf( stderr, _("%s: -E incompatible with LDAPv%d\n"),
94                                 prog, protocol );
95                         exit( EXIT_FAILURE );
96                 }
97
98                 /* should be extended to support comma separated list of
99                  *      [!]key[=value] parameters, e.g.  -E !foo,bar=567
100                  */
101
102                 crit = 0;
103                 cvalue = NULL;
104                 if( optarg[0] == '!' ) {
105                         crit = 1;
106                         optarg++;
107                 }
108
109                 control = strdup( optarg );
110                 if ( (cvalue = strchr( control, '=' )) != NULL ) {
111                         *cvalue++ = '\0';
112                 }
113                 fprintf( stderr, _("Invalid delete extension name: %s\n"), control );
114                 usage();
115 #endif
116
117         case 'r':
118                 prune = 1;
119                 break;
120
121         case 'z':       /* size limit */
122                 if ( strcasecmp( optarg, "none" ) == 0 ) {
123                         sizelimit = 0;
124
125                 } else if ( strcasecmp( optarg, "max" ) == 0 ) {
126                         sizelimit = LDAP_MAXINT;
127
128                 } else {
129                         ival = strtol( optarg, &next, 10 );
130                         if ( next == NULL || next[0] != '\0' ) {
131                                 fprintf( stderr,
132                                         _("Unable to parse size limit \"%s\"\n"), optarg );
133                                 exit( EXIT_FAILURE );
134                         }
135                         sizelimit = ival;
136                 }
137                 if( sizelimit < 0 || sizelimit > LDAP_MAXINT ) {
138                         fprintf( stderr, _("%s: invalid sizelimit (%d) specified\n"),
139                                 prog, sizelimit );
140                         exit( EXIT_FAILURE );
141                 }
142                 break;
143
144         default:
145                 return 0;
146         }
147         return 1;
148 }
149
150
151 static void
152 private_conn_setup( LDAP *ld )
153 {
154         /* this seems prudent for searches below */
155         int deref = LDAP_DEREF_NEVER;
156         ldap_set_option( ld, LDAP_OPT_DEREF, &deref );
157 }
158
159
160 int
161 main( int argc, char **argv )
162 {
163         char            buf[ 4096 ];
164         FILE            *fp;
165         LDAP            *ld;
166         int             rc, retval;
167
168     fp = NULL;
169
170         tool_init( TOOL_DELETE );
171     prog = lutil_progname( "ldapdelete", argc, argv );
172
173         tool_args( argc, argv );
174
175         if ( infile != NULL ) {
176                 if (( fp = fopen( infile, "r" )) == NULL ) {
177                         perror( optarg );
178                         exit( EXIT_FAILURE );
179             }
180         } else {
181         if ( optind >= argc ) {
182             fp = stdin;
183         }
184     }
185
186         ld = tool_conn_setup( 0, &private_conn_setup );
187
188         if ( pw_file || want_bindpw ) {
189                 if ( pw_file ) {
190                         rc = lutil_get_filed_password( pw_file, &passwd );
191                         if( rc ) return EXIT_FAILURE;
192                 } else {
193                         passwd.bv_val = getpassphrase( _("Enter LDAP Password: ") );
194                         passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
195                 }
196         }
197
198         tool_bind( ld );
199
200         tool_server_controls( ld, NULL, 0 );
201
202         retval = rc = 0;
203
204         if ( fp == NULL ) {
205                 for ( ; optind < argc; ++optind ) {
206                         rc = dodelete( ld, argv[ optind ] );
207
208                         /* Stop on error and no -c option */
209                         if( rc != 0 ) {
210                                 retval = rc;
211                                 if( contoper == 0 ) break;
212                         }
213                 }
214         } else {
215                 while ((rc == 0 || contoper) && fgets(buf, sizeof(buf), fp) != NULL) {
216                         buf[ strlen( buf ) - 1 ] = '\0'; /* remove trailing newline */
217
218                         if ( *buf != '\0' ) {
219                                 rc = dodelete( ld, buf );
220                                 if ( rc != 0 )
221                                         retval = rc;
222                         }
223                 }
224         }
225
226         tool_unbind( ld );
227         tool_destroy();
228     return retval;
229 }
230
231
232 static int dodelete(
233     LDAP        *ld,
234     const char  *dn)
235 {
236         int id;
237         int     rc, code;
238         char *matcheddn = NULL, *text = NULL, **refs = NULL;
239         LDAPControl **ctrls = NULL;
240         LDAPMessage *res;
241
242         if ( verbose ) {
243                 printf( _("%sdeleting entry \"%s\"\n"),
244                         (dont ? "!" : ""), dn );
245         }
246
247         if ( dont ) {
248                 return LDAP_SUCCESS;
249         }
250
251         /* If prune is on, remove a whole subtree.  Delete the children of the
252          * DN recursively, then the DN requested.
253          */
254         if ( prune ) deletechildren( ld, dn );
255
256         rc = ldap_delete_ext( ld, dn, NULL, NULL, &id );
257         if ( rc != LDAP_SUCCESS ) {
258                 fprintf( stderr, "%s: ldap_delete_ext: %s (%d)\n",
259                         prog, ldap_err2string( rc ), rc );
260                 return rc;
261         }
262
263         for ( ; ; ) {
264                 struct timeval tv;
265
266                 if ( tool_check_abandon( ld, id ) ) {
267                         return LDAP_CANCELLED;
268                 }
269
270                 tv.tv_sec = 0;
271                 tv.tv_usec = 100000;
272
273                 rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, &tv, &res );
274                 if ( rc < 0 ) {
275                         tool_perror( "ldap_result", rc, NULL, NULL, NULL, NULL );
276                         return rc;
277                 }
278
279                 if ( rc != 0 ) {
280                         break;
281                 }
282         }
283
284         rc = ldap_parse_result( ld, res, &code, &matcheddn, &text, &refs, &ctrls, 1 );
285
286         if( rc != LDAP_SUCCESS ) {
287                 fprintf( stderr, "%s: ldap_parse_result: %s (%d)\n",
288                         prog, ldap_err2string( rc ), rc );
289                 return rc;
290         }
291
292         if( code != LDAP_SUCCESS ) {
293                 tool_perror( "ldap_delete", code, NULL, matcheddn, text, refs );
294         } else if ( verbose && 
295                 ((matcheddn && *matcheddn) || (text && *text) || (refs && *refs) ))
296         {
297                 printf( _("Delete Result: %s (%d)\n"),
298                         ldap_err2string( code ), code );
299
300                 if( text && *text ) {
301                         printf( _("Additional info: %s\n"), text );
302                 }
303
304                 if( matcheddn && *matcheddn ) {
305                         printf( _("Matched DN: %s\n"), matcheddn );
306                 }
307
308                 if( refs ) {
309                         int i;
310                         for( i=0; refs[i]; i++ ) {
311                                 printf(_("Referral: %s\n"), refs[i] );
312                         }
313                 }
314         }
315
316         if (ctrls) {
317                 tool_print_ctrls( ld, ctrls );
318                 ldap_controls_free( ctrls );
319     }
320
321         ber_memfree( text );
322         ber_memfree( matcheddn );
323         ber_memvfree( (void **) refs );
324
325         return code;
326 }
327
328 /*
329  * Delete all the children of an entry recursively until leaf nodes are reached.
330  */
331 static int deletechildren(
332         LDAP *ld,
333         const char *base )
334 {
335         LDAPMessage *res, *e;
336         int entries;
337         int rc, srch_rc;
338         static char *attrs[] = { LDAP_NO_ATTRS, NULL };
339         LDAPControl c, *ctrls[2];
340         BerElement *ber = NULL;
341         LDAPMessage *res_se;
342
343         if ( verbose ) printf ( _("deleting children of: %s\n"), base );
344
345         /*
346          * Do a one level search at base for children.  For each, delete its children.
347          */
348 more:;
349         srch_rc = ldap_search_ext_s( ld, base, LDAP_SCOPE_ONELEVEL, NULL, attrs, 1,
350                 NULL, NULL, NULL, sizelimit, &res );
351         switch ( srch_rc ) {
352         case LDAP_SUCCESS:
353         case LDAP_SIZELIMIT_EXCEEDED:
354                 break;
355         default:
356                 tool_perror( "ldap_search", srch_rc, NULL, NULL, NULL, NULL );
357                 return( srch_rc );
358         }
359
360         entries = ldap_count_entries( ld, res );
361
362         if ( entries > 0 ) {
363                 int i;
364
365                 for (e = ldap_first_entry( ld, res ), i = 0; e != NULL;
366                         e = ldap_next_entry( ld, e ), i++ )
367                 {
368                         char *dn = ldap_get_dn( ld, e );
369
370                         if( dn == NULL ) {
371                                 ldap_get_option( ld, LDAP_OPT_RESULT_CODE, &rc );
372                                 tool_perror( "ldap_prune", rc, NULL, NULL, NULL, NULL );
373                                 ber_memfree( dn );
374                                 return rc;
375                         }
376
377                         rc = deletechildren( ld, dn );
378                         if ( rc == -1 ) {
379                                 tool_perror( "ldap_prune", rc, NULL, NULL, NULL, NULL );
380                                 ber_memfree( dn );
381                                 return rc;
382                         }
383
384                         if ( verbose ) {
385                                 printf( _("\tremoving %s\n"), dn );
386                         }
387
388                         rc = ldap_delete_ext_s( ld, dn, NULL, NULL );
389                         if ( rc == -1 ) {
390                                 tool_perror( "ldap_delete", rc, NULL, NULL, NULL, NULL );
391                                 ber_memfree( dn );
392                                 return rc;
393
394                         }
395                         
396                         if ( verbose ) {
397                                 printf( _("\t%s removed\n"), dn );
398                         }
399
400                         ber_memfree( dn );
401                 }
402         }
403
404         ldap_msgfree( res );
405
406         if ( srch_rc == LDAP_SIZELIMIT_EXCEEDED ) {
407                 goto more;
408         }
409
410         /*
411          * Do a one level search at base for subentry children.
412          */
413
414         if ((ber = ber_alloc_t(LBER_USE_DER)) == NULL) {
415                 return EXIT_FAILURE;
416         }
417         rc = ber_printf( ber, "b", 1 );
418         if ( rc == -1 ) {
419                 ber_free( ber, 1 );
420                 fprintf( stderr, _("Subentries control encoding error!\n"));
421                 return EXIT_FAILURE;
422         }
423         if ( ber_flatten2( ber, &c.ldctl_value, 0 ) == -1 ) {
424                 return EXIT_FAILURE;
425         }
426         c.ldctl_oid = LDAP_CONTROL_SUBENTRIES;
427         c.ldctl_iscritical = 1;
428         ctrls[0] = &c;
429         ctrls[1] = NULL;
430
431 more2:;
432         srch_rc = ldap_search_ext_s( ld, base, LDAP_SCOPE_ONELEVEL, NULL, attrs, 1,
433                 ctrls, NULL, NULL, sizelimit, &res_se );
434         switch ( srch_rc ) {
435         case LDAP_SUCCESS:
436         case LDAP_SIZELIMIT_EXCEEDED:
437                 break;
438         default:
439                 tool_perror( "ldap_search", srch_rc, NULL, NULL, NULL, NULL );
440                 return( srch_rc );
441         }
442         ber_free( ber, 1 );
443
444         entries = ldap_count_entries( ld, res_se );
445
446         if ( entries > 0 ) {
447                 int i;
448
449                 for (e = ldap_first_entry( ld, res_se ), i = 0; e != NULL;
450                         e = ldap_next_entry( ld, e ), i++ )
451                 {
452                         char *dn = ldap_get_dn( ld, e );
453
454                         if( dn == NULL ) {
455                                 ldap_get_option( ld, LDAP_OPT_RESULT_CODE, &rc );
456                                 tool_perror( "ldap_prune", rc, NULL, NULL, NULL, NULL );
457                                 ber_memfree( dn );
458                                 return rc;
459                         }
460
461                         if ( verbose ) {
462                                 printf( _("\tremoving %s\n"), dn );
463                         }
464
465                         rc = ldap_delete_ext_s( ld, dn, NULL, NULL );
466                         if ( rc == -1 ) {
467                                 tool_perror( "ldap_delete", rc, NULL, NULL, NULL, NULL );
468                                 ber_memfree( dn );
469                                 return rc;
470
471                         }
472                         
473                         if ( verbose ) {
474                                 printf( _("\t%s removed\n"), dn );
475                         }
476
477                         ber_memfree( dn );
478                 }
479         }
480
481         ldap_msgfree( res_se );
482
483         if ( srch_rc == LDAP_SIZELIMIT_EXCEEDED ) {
484                 goto more2;
485         }
486
487         return rc;
488 }