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