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