]> git.sur5r.net Git - openldap/blob - clients/tools/ldapdelete.c
Notice/Acknowledge updates
[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-2003 The OpenLDAP Foundation.
6  * Portions Copyright 1998-2003 Kurt D. Zeilenga.
7  * Portions Copyright 1998-2001 Net Boolean Incorporated.
8  * Portions Copyright 2001-2003 IBM Corporation.
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted only as authorized by the OpenLDAP
13  * Public License.
14  *
15  * A copy of this license is available in the file LICENSE in the
16  * top-level directory of the distribution or, alternatively, at
17  * <http://www.OpenLDAP.org/license.html>.
18  */
19 /* Portions Copyright (c) 1992-1996 Regents of the University of Michigan.
20  * All rights reserved.
21  *
22  * Redistribution and use in source and binary forms are permitted
23  * provided that this notice is preserved and that due credit is given
24  * to the University of Michigan at Ann Arbor.  The name of the
25  * University may not be used to endorse or promote products derived
26  * from this software without specific prior written permission.  This
27  * software is provided ``as is'' without express or implied warranty.
28  */
29 /* ACKNOWLEDGEMENTS:
30  * This work was originally developed by the University of Michigan
31  * (as part of U-MICH LDAP).  Additional significant contributors
32  * include:
33  *   Kurt D. Zeilenga
34  */
35
36 #include "portable.h"
37
38 #include <stdio.h>
39
40 #include <ac/stdlib.h>
41 #include <ac/ctype.h>
42 #include <ac/string.h>
43 #include <ac/unistd.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         "cCd:D:e:f:h:H:IkKMnO: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 controls */
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 control 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();
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         if ( assertion || authzid || manageDSAit || noop ) {
175                 tool_server_controls( ld, NULL, 0 );
176         }
177
178         retval = rc = 0;
179
180     if ( fp == NULL ) {
181                 for ( ; optind < argc; ++optind ) {
182                         rc = dodelete( ld, argv[ optind ] );
183
184                         /* Stop on error and no -c option */
185                         if( rc != 0 ) {
186                                 retval = rc;
187                                 if( contoper == 0 ) break;
188                         }
189                 }
190         } else {
191                 while ((rc == 0 || contoper) && fgets(buf, sizeof(buf), fp) != NULL) {
192                         buf[ strlen( buf ) - 1 ] = '\0'; /* remove trailing newline */
193
194                         if ( *buf != '\0' ) {
195                                 rc = dodelete( ld, buf );
196                                 if ( rc != 0 )
197                                         retval = rc;
198                         }
199                 }
200         }
201
202     ldap_unbind( ld );
203
204     return( retval );
205 }
206
207
208 static int dodelete(
209     LDAP        *ld,
210     const char  *dn)
211 {
212         int id;
213         int     rc, code;
214         char *matcheddn = NULL, *text = NULL, **refs = NULL;
215         LDAPMessage *res;
216
217         if ( verbose ) {
218                 printf( _("%sdeleting entry \"%s\"\n"),
219                         (not ? "!" : ""), dn );
220         }
221
222         if ( not ) {
223                 return LDAP_SUCCESS;
224         }
225
226         /* If prune is on, remove a whole subtree.  Delete the children of the
227          * DN recursively, then the DN requested.
228          */
229         if ( prune ) deletechildren( ld, dn );
230
231         rc = ldap_delete_ext( ld, dn, NULL, NULL, &id );
232         if ( rc != LDAP_SUCCESS ) {
233                 fprintf( stderr, "%s: ldap_delete_ext: %s (%d)\n",
234                         prog, ldap_err2string( rc ), rc );
235                 return rc;
236         }
237
238         rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, NULL, &res );
239         if ( rc < 0 ) {
240                 ldap_perror( ld, "ldapdelete: ldap_result" );
241                 return rc;
242         }
243
244         rc = ldap_parse_result( ld, res, &code, &matcheddn, &text, &refs, NULL, 1 );
245
246         if( rc != LDAP_SUCCESS ) {
247                 fprintf( stderr, "%s: ldap_parse_result: %s (%d)\n",
248                         prog, ldap_err2string( rc ), rc );
249                 return rc;
250         }
251
252         if( verbose || code != LDAP_SUCCESS ||
253                 (matcheddn && *matcheddn) || (text && *text) || (refs && *refs) )
254         {
255                 printf( _("Delete Result: %s (%d)\n"), ldap_err2string( code ), code );
256
257                 if( text && *text ) {
258                         printf( _("Additional info: %s\n"), text );
259                 }
260
261                 if( matcheddn && *matcheddn ) {
262                         printf( _("Matched DN: %s\n"), matcheddn );
263                 }
264
265                 if( refs ) {
266                         int i;
267                         for( i=0; refs[i]; i++ ) {
268                                 printf(_("Referral: %s\n"), refs[i] );
269                         }
270                 }
271         }
272
273         ber_memfree( text );
274         ber_memfree( matcheddn );
275         ber_memvfree( (void **) refs );
276
277         return code;
278 }
279
280 /*
281  * Delete all the children of an entry recursively until leaf nodes are reached.
282  *
283  */
284 static int deletechildren(
285         LDAP *ld,
286         const char *dn )
287 {
288         LDAPMessage *res, *e;
289         int entries;
290         int rc;
291         static char *attrs[] = { "1.1", NULL };
292
293         if ( verbose ) printf ( _("deleting children of: %s\n"), dn );
294         /*
295          * Do a one level search at dn for children.  For each, delete its children.
296          */
297
298         rc = ldap_search_ext_s( ld, dn, LDAP_SCOPE_ONELEVEL, NULL, attrs, 1,
299                 NULL, NULL, NULL, -1, &res );
300         if ( rc != LDAP_SUCCESS ) {
301                 ldap_perror( ld, "ldap_search" );
302                 return( rc );
303         }
304
305         entries = ldap_count_entries( ld, res );
306
307         if ( entries > 0 ) {
308                 int i;
309
310                 for (e = ldap_first_entry( ld, res ), i = 0; e != NULL;
311                         e = ldap_next_entry( ld, e ), i++ )
312                 {
313                         char *dn = ldap_get_dn( ld, e );
314
315                         if( dn == NULL ) {
316                                 ldap_perror( ld, "ldap_prune" );
317                                 ldap_get_option( ld, LDAP_OPT_ERROR_NUMBER, &rc );
318                                 ber_memfree( dn );
319                                 return rc;
320                         }
321
322                         rc = deletechildren( ld, dn );
323                         if ( rc == -1 ) {
324                                 ldap_perror( ld, "ldap_prune" );
325                                 ber_memfree( dn );
326                                 return rc;
327                         }
328
329                         if ( verbose ) {
330                                 printf( _("\tremoving %s\n"), dn );
331                         }
332
333                         rc = ldap_delete_s( ld, dn );
334                         if ( rc == -1 ) {
335                                 ldap_perror( ld, "ldap_delete" );
336                                 ber_memfree( dn );
337                                 return rc;
338
339                         }
340                         
341                         if ( verbose ) {
342                                 printf( _("\t%s removed\n"), dn );
343                         }
344
345                         ber_memfree( dn );
346                 }
347         }
348
349         ldap_msgfree( res );
350         return rc;
351 }