]> git.sur5r.net Git - openldap/blob - clients/tools/ldapdelete.c
b4beafab0b6d70675182b82f72b6f0133b11a61f
[openldap] / clients / tools / ldapdelete.c
1 /* ldapdelete.c - simple program to delete an entry using LDAP */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
5  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6  */
7
8 #include "portable.h"
9
10 #include <stdio.h>
11
12 #include <ac/stdlib.h>
13 #include <ac/ctype.h>
14
15 #include <ac/signal.h>
16 #include <ac/string.h>
17 #include <ac/unistd.h>
18
19 #include <ldap.h>
20 #include "lutil_ldap.h"
21 #include "ldap_defaults.h"
22
23 static char     *prog;
24 static char     *binddn = NULL;
25 static struct berval passwd = { 0, NULL };
26 static char *ldapuri = NULL;
27 static char     *ldaphost = NULL;
28 static int      ldapport = 0;
29 static int      prune = 0;
30 #ifdef HAVE_CYRUS_SASL
31 static unsigned sasl_flags = LDAP_SASL_AUTOMATIC;
32 static char     *sasl_mech = NULL;
33 static char *sasl_realm = NULL;
34 static char     *sasl_authc_id = NULL;
35 static char     *sasl_authz_id = NULL;
36 static char     *sasl_secprops = NULL;
37 #endif
38 static int      use_tls = 0;
39 static int      not, verbose, contoper;
40 static LDAP     *ld = NULL;
41
42 static int dodelete LDAP_P((
43     LDAP *ld,
44     const char *dn));
45
46 static int deletechildren LDAP_P((
47         LDAP *ld,
48         const char *dn ));
49
50 static void
51 usage( const char *s )
52 {
53         fprintf( stderr,
54 "Delete entries from an LDAP server\n\n"
55 "usage: %s [options] [dn]...\n"
56 "       dn: list of DNs to delete. If not given, it will be readed from stdin\n"
57 "           or from the file specified with \"-f file\".\n"
58 "Delete Options:\n"
59 "  -r         delete recursively\n"
60
61 "Common options:\n"
62 "  -d level   set LDAP debugging level to `level'\n"
63 "  -D binddn  bind DN\n"
64 "  -f file    read operations from `file'\n"
65 "  -h host    LDAP server\n"
66 "  -H URI     LDAP Uniform Resource Indentifier(s)\n"
67 "  -I         use SASL Interactive mode\n"
68 "  -k         use Kerberos authentication\n"
69 "  -K         like -k, but do only step 1 of the Kerberos bind\n"
70 "  -M         enable Manage DSA IT control (-MM to make critical)\n"
71 "  -n         show what would be done but don't actually search\n"
72 "  -O props   SASL security properties\n"
73 "  -p port    port on LDAP server\n"
74 "  -P version procotol version (default: 3)\n"
75 "  -Q         use SASL Quiet mode\n"
76 "  -R realm   SASL realm\n"
77 "  -U user    SASL authentication identity (username)\n"
78 "  -v         run in verbose mode (diagnostics to standard output)\n"
79 "  -w passwd  bind passwd (for simple authentication)\n"
80 "  -W         prompt for bind passwd\n"
81 "  -x         Simple authentication\n"
82 "  -X id      SASL authorization identity (\"dn:<dn>\" or \"u:<user>\")\n"
83 "  -Y mech    SASL mechanism\n"
84 "  -Z         Start TLS request (-ZZ to require successful response)\n"
85 ,               s );
86
87         exit( EXIT_FAILURE );
88 }
89
90
91 int
92 main( int argc, char **argv )
93 {
94         char            buf[ 4096 ];
95         FILE            *fp;
96         int             i, rc, authmethod, referrals, want_bindpw, version, debug, manageDSAit;
97
98     not = verbose = contoper = want_bindpw = debug = manageDSAit = referrals = 0;
99     fp = NULL;
100     authmethod = -1;
101         version = -1;
102
103     prog = (prog = strrchr(argv[0], *LDAP_DIRSEP)) == NULL ? argv[0] : prog + 1;
104
105     while (( i = getopt( argc, argv, "cf:r" "Cd:D:h:H:IkKMnO:p:P:QRU:vw:WxX:Y:Z" )) != EOF ) {
106         switch( i ) {
107         /* Delete Specific Options */
108         case 'c':       /* continuous operation mode */
109             ++contoper;
110             break;
111         case 'f':       /* read DNs from a file */
112                 if( fp != NULL ) {
113                         fprintf( stderr, "%s: -f previously specified\n" );
114                         return EXIT_FAILURE;
115                 }
116             if (( fp = fopen( optarg, "r" )) == NULL ) {
117                 perror( optarg );
118                 exit( EXIT_FAILURE );
119             }
120             break;
121         case 'r':
122                 prune = 1;
123                 break;
124
125         /* Common Options */
126         case 'C':
127                 referrals++;
128                 break;
129         case 'd':
130             debug |= atoi( optarg );
131             break;
132         case 'D':       /* bind DN */
133                 if( binddn != NULL ) {
134                         fprintf( stderr, "%s: -D previously specified\n" );
135                         return EXIT_FAILURE;
136                 }
137             binddn = strdup( optarg );
138             break;
139         case 'h':       /* ldap host */
140                 if( ldapuri != NULL ) {
141                         fprintf( stderr, "%s: -h incompatible with -H\n" );
142                         return EXIT_FAILURE;
143                 }
144                 if( ldaphost != NULL ) {
145                         fprintf( stderr, "%s: -h previously specified\n" );
146                         return EXIT_FAILURE;
147                 }
148             ldaphost = strdup( optarg );
149             break;
150         case 'H':       /* ldap URI */
151                 if( ldaphost != NULL ) {
152                         fprintf( stderr, "%s: -H incompatible with -h\n" );
153                         return EXIT_FAILURE;
154                 }
155                 if( ldapport ) {
156                         fprintf( stderr, "%s: -H incompatible with -p\n" );
157                         return EXIT_FAILURE;
158                 }
159                 if( ldapuri != NULL ) {
160                         fprintf( stderr, "%s: -H previously specified\n" );
161                         return EXIT_FAILURE;
162                 }
163             ldapuri = strdup( optarg );
164             break;
165         case 'I':
166 #ifdef HAVE_CYRUS_SASL
167                 if( version == LDAP_VERSION2 ) {
168                         fprintf( stderr, "%s: -I incompatible with version %d\n",
169                                 prog, version );
170                         return EXIT_FAILURE;
171                 }
172                 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
173                         fprintf( stderr, "%s: incompatible previous "
174                                 "authentication choice\n",
175                                 prog );
176                         return EXIT_FAILURE;
177                 }
178                 authmethod = LDAP_AUTH_SASL;
179                 version = LDAP_VERSION3;
180                 sasl_flags = LDAP_SASL_INTERACTIVE;
181                 break;
182 #else
183                 fprintf( stderr, "%s: was not compiled with SASL support\n",
184                         prog );
185                 return( EXIT_FAILURE );
186 #endif
187         case 'k':       /* kerberos bind */
188 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
189                 if( version > LDAP_VERSION2 ) {
190                         fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
191                                 prog, version );
192                         return EXIT_FAILURE;
193                 }
194
195                 if( authmethod != -1 ) {
196                         fprintf( stderr, "%s: -k incompatible with previous "
197                                 "authentication choice\n", prog );
198                         return EXIT_FAILURE;
199                 }
200                         
201                 authmethod = LDAP_AUTH_KRBV4;
202 #else
203                 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
204                 return EXIT_FAILURE;
205 #endif
206             break;
207         case 'K':       /* kerberos bind, part one only */
208 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
209                 if( version > LDAP_VERSION2 ) {
210                         fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
211                                 prog, version );
212                         return EXIT_FAILURE;
213                 }
214                 if( authmethod != -1 ) {
215                         fprintf( stderr, "%s: incompatible with previous "
216                                 "authentication choice\n", prog );
217                         return EXIT_FAILURE;
218                 }
219
220                 authmethod = LDAP_AUTH_KRBV41;
221 #else
222                 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
223                 return( EXIT_FAILURE );
224 #endif
225             break;
226         case 'M':
227                 /* enable Manage DSA IT */
228                 if( version == LDAP_VERSION2 ) {
229                         fprintf( stderr, "%s: -M incompatible with LDAPv%d\n",
230                                 prog, version );
231                         return EXIT_FAILURE;
232                 }
233                 manageDSAit++;
234                 version = LDAP_VERSION3;
235                 break;
236         case 'n':       /* print deletes, don't actually do them */
237             ++not;
238             break;
239         case 'O':
240 #ifdef HAVE_CYRUS_SASL
241                 if( sasl_secprops != NULL ) {
242                         fprintf( stderr, "%s: -O previously specified\n" );
243                         return EXIT_FAILURE;
244                 }
245                 if( version == LDAP_VERSION2 ) {
246                         fprintf( stderr, "%s: -O incompatible with LDAPv%d\n",
247                                 prog, version );
248                         return EXIT_FAILURE;
249                 }
250                 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
251                         fprintf( stderr, "%s: incompatible previous "
252                                 "authentication choice\n", prog );
253                         return EXIT_FAILURE;
254                 }
255                 authmethod = LDAP_AUTH_SASL;
256                 version = LDAP_VERSION3;
257                 sasl_secprops = strdup( optarg );
258 #else
259                 fprintf( stderr, "%s: not compiled with SASL support\n",
260                         prog );
261                 return( EXIT_FAILURE );
262 #endif
263                 break;
264         case 'p':
265                 if( ldapport ) {
266                         fprintf( stderr, "%s: -p previously specified\n" );
267                         return EXIT_FAILURE;
268                 }
269             ldapport = atoi( optarg );
270             break;
271         case 'P':
272                 switch( atoi(optarg) ) {
273                 case 2:
274                         if( version == LDAP_VERSION3 ) {
275                                 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
276                                         prog, version );
277                                 return EXIT_FAILURE;
278                         }
279                         version = LDAP_VERSION2;
280                         break;
281                 case 3:
282                         if( version == LDAP_VERSION2 ) {
283                                 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
284                                         prog, version );
285                                 return EXIT_FAILURE;
286                         }
287                         version = LDAP_VERSION3;
288                         break;
289                 default:
290                         fprintf( stderr, "%s: protocol version should be 2 or 3\n",
291                                 prog );
292                         usage( prog );
293                         return( EXIT_FAILURE );
294                 } break;
295         case 'Q':
296 #ifdef HAVE_CYRUS_SASL
297                 if( version == LDAP_VERSION2 ) {
298                         fprintf( stderr, "%s: -Q incompatible with version %d\n",
299                                 prog, version );
300                         return EXIT_FAILURE;
301                 }
302                 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
303                         fprintf( stderr, "%s: incompatible previous "
304                                 "authentication choice\n",
305                                 prog );
306                         return EXIT_FAILURE;
307                 }
308                 authmethod = LDAP_AUTH_SASL;
309                 version = LDAP_VERSION3;
310                 sasl_flags = LDAP_SASL_QUIET;
311                 break;
312 #else
313                 fprintf( stderr, "%s: not compiled with SASL support\n",
314                         prog );
315                 return( EXIT_FAILURE );
316 #endif
317         case 'R':
318 #ifdef HAVE_CYRUS_SASL
319                 if( sasl_realm != NULL ) {
320                         fprintf( stderr, "%s: -R previously specified\n" );
321                         return EXIT_FAILURE;
322                 }
323                 if( version == LDAP_VERSION2 ) {
324                         fprintf( stderr, "%s: -R incompatible with version %d\n",
325                                 prog, version );
326                         return EXIT_FAILURE;
327                 }
328                 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
329                         fprintf( stderr, "%s: incompatible previous "
330                                 "authentication choice\n",
331                                 prog );
332                         return EXIT_FAILURE;
333                 }
334                 authmethod = LDAP_AUTH_SASL;
335                 version = LDAP_VERSION3;
336                 sasl_realm = strdup( optarg );
337 #else
338                 fprintf( stderr, "%s: not compiled with SASL support\n",
339                         prog );
340                 return( EXIT_FAILURE );
341 #endif
342                 break;
343         case 'U':
344 #ifdef HAVE_CYRUS_SASL
345                 if( sasl_authc_id != NULL ) {
346                         fprintf( stderr, "%s: -U previously specified\n" );
347                         return EXIT_FAILURE;
348                 }
349                 if( version == LDAP_VERSION2 ) {
350                         fprintf( stderr, "%s: -U incompatible with version %d\n",
351                                 prog, version );
352                         return EXIT_FAILURE;
353                 }
354                 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
355                         fprintf( stderr, "%s: incompatible previous "
356                                 "authentication choice\n",
357                                 prog );
358                         return EXIT_FAILURE;
359                 }
360                 authmethod = LDAP_AUTH_SASL;
361                 version = LDAP_VERSION3;
362                 sasl_authc_id = strdup( optarg );
363 #else
364                 fprintf( stderr, "%s: not compiled with SASL support\n",
365                         prog );
366                 return( EXIT_FAILURE );
367 #endif
368                 break;
369         case 'v':       /* verbose mode */
370             verbose++;
371             break;
372         case 'w':       /* password */
373             passwd.bv_val = strdup( optarg );
374                 {
375                         char* p;
376
377                         for( p = optarg; *p == '\0'; p++ ) {
378                                 *p = '\0';
379                         }
380                 }
381                 passwd.bv_len = strlen( passwd.bv_val );
382             break;
383         case 'W':
384                 want_bindpw++;
385                 break;
386         case 'Y':
387 #ifdef HAVE_CYRUS_SASL
388                 if( sasl_mech != NULL ) {
389                         fprintf( stderr, "%s: -Y previously specified\n" );
390                         return EXIT_FAILURE;
391                 }
392                 if( version == LDAP_VERSION2 ) {
393                         fprintf( stderr, "%s: -Y incompatible with version %d\n",
394                                 prog, version );
395                         return EXIT_FAILURE;
396                 }
397                 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
398                         fprintf( stderr, "%s: incompatible with authentication choice\n", prog );
399                         return EXIT_FAILURE;
400                 }
401                 authmethod = LDAP_AUTH_SASL;
402                 version = LDAP_VERSION3;
403                 sasl_mech = strdup( optarg );
404 #else
405                 fprintf( stderr, "%s: not compiled with SASL support\n",
406                         prog );
407                 return( EXIT_FAILURE );
408 #endif
409                 break;
410         case 'x':
411                 if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
412                         fprintf( stderr, "%s: incompatible with previous "
413                                 "authentication choice\n", prog );
414                         return EXIT_FAILURE;
415                 }
416                 authmethod = LDAP_AUTH_SIMPLE;
417                 break;
418         case 'X':
419 #ifdef HAVE_CYRUS_SASL
420                 if( sasl_authz_id != NULL ) {
421                         fprintf( stderr, "%s: -X previously specified\n" );
422                         return EXIT_FAILURE;
423                 }
424                 if( version == LDAP_VERSION2 ) {
425                         fprintf( stderr, "%s: -X incompatible with LDAPv%d\n",
426                                 prog, version );
427                         return EXIT_FAILURE;
428                 }
429                 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
430                         fprintf( stderr, "%s: -X incompatible with "
431                                 "authentication choice\n", prog );
432                         return EXIT_FAILURE;
433                 }
434                 authmethod = LDAP_AUTH_SASL;
435                 version = LDAP_VERSION3;
436                 sasl_authz_id = strdup( optarg );
437 #else
438                 fprintf( stderr, "%s: not compiled with SASL support\n",
439                         prog );
440                 return( EXIT_FAILURE );
441 #endif
442                 break;
443         case 'Z':
444 #ifdef HAVE_TLS
445                 if( version == LDAP_VERSION2 ) {
446                         fprintf( stderr, "%s: -Z incompatible with version %d\n",
447                                 prog, version );
448                         return EXIT_FAILURE;
449                 }
450                 version = LDAP_VERSION3;
451                 use_tls++;
452 #else
453                 fprintf( stderr, "%s: not compiled with TLS support\n",
454                         prog );
455                 return( EXIT_FAILURE );
456 #endif
457                 break;
458         default:
459                 fprintf( stderr, "%s: unrecongized option -%c\n",
460                         prog, optopt );
461                 usage( prog );
462                 return( EXIT_FAILURE );
463         }
464     }
465
466         if (version == -1) {
467                 version = LDAP_VERSION3;
468         }
469         if (authmethod == -1 && version > LDAP_VERSION2) {
470 #ifdef HAVE_CYRUS_SASL
471                 authmethod = LDAP_AUTH_SASL;
472 #else
473                 authmethod = LDAP_AUTH_SIMPLE;
474 #endif
475         }
476
477     if ( fp == NULL ) {
478         if ( optind >= argc ) {
479             fp = stdin;
480         }
481     }
482
483         if ( debug ) {
484                 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
485                         fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
486                 }
487                 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
488                         fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
489                 }
490         }
491
492 #ifdef SIGPIPE
493         (void) SIGNAL( SIGPIPE, SIG_IGN );
494 #endif
495
496         if( ( ldaphost != NULL || ldapport ) && ( ldapuri == NULL ) ) {
497                 if ( verbose ) {
498                         fprintf( stderr, "ldap_init( %s, %d )\n",
499                                 ldaphost != NULL ? ldaphost : "<DEFAULT>",
500                                 ldapport );
501                 }
502                 ld = ldap_init( ldaphost, ldapport );
503
504         } else {
505                 if ( verbose ) {
506                         fprintf( stderr, "ldap_initialize( %s )\n",
507                                 ldapuri != NULL ? ldapuri : "<DEFAULT>" );
508                 }
509                 (void) ldap_initialize( &ld, ldapuri );
510         }
511
512         if( ld == NULL ) {
513                 fprintf( stderr, "Could not create LDAP session handle (%d): %s\n",
514                         rc, ldap_err2string(rc) );
515                 return EXIT_FAILURE;
516         }
517
518         {
519                 /* this seems prudent for searches below */
520                 int deref = LDAP_DEREF_NEVER;
521                 ldap_set_option( ld, LDAP_OPT_DEREF, &deref );
522         }
523
524         /* chase referrals */
525         if( ldap_set_option( ld, LDAP_OPT_REFERRALS,
526                 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
527         {
528                 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
529                         referrals ? "on" : "off" );
530                 return EXIT_FAILURE;
531         }
532
533         if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version )
534                 != LDAP_OPT_SUCCESS )
535         {
536                 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
537                         version );
538                 return EXIT_FAILURE;
539         }
540
541         if ( use_tls && ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS ) {
542                 if ( use_tls > 1 ) {
543                         ldap_perror( ld, "ldap_start_tls" );
544                         return EXIT_FAILURE;
545                 }
546                 fprintf( stderr, "WARNING: could not start TLS\n" );
547         }
548
549         if (want_bindpw) {
550                 passwd.bv_val = getpassphrase("Enter LDAP Password: ");
551                 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
552         }
553
554         if ( authmethod == LDAP_AUTH_SASL ) {
555 #ifdef HAVE_CYRUS_SASL
556                 void *defaults;
557
558                 if( sasl_secprops != NULL ) {
559                         rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
560                                 (void *) sasl_secprops );
561                         
562                         if( rc != LDAP_OPT_SUCCESS ) {
563                                 fprintf( stderr,
564                                         "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
565                                         sasl_secprops );
566                                 return( EXIT_FAILURE );
567                         }
568                 }
569                 
570                 defaults = lutil_sasl_defaults( ld,
571                         sasl_mech,
572                         sasl_realm,
573                         sasl_authc_id,
574                         passwd.bv_val,
575                         sasl_authz_id );
576
577                 rc = ldap_sasl_interactive_bind_s( ld, binddn,
578                         sasl_mech, NULL, NULL,
579                         sasl_flags, lutil_sasl_interact, defaults );
580
581                 if( rc != LDAP_SUCCESS ) {
582                         ldap_perror( ld, "ldap_sasl_interactive_bind_s" );
583                         return( EXIT_FAILURE );
584                 }
585 #else
586                 fprintf( stderr, "%s: not compiled with SASL support\n",
587                         argv[0] );
588                 return( EXIT_FAILURE );
589 #endif
590         }
591         else {
592                 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
593                                 != LDAP_SUCCESS ) {
594                         ldap_perror( ld, "ldap_bind" );
595                         return( EXIT_FAILURE );
596                 }
597         }
598
599         if ( manageDSAit ) {
600                 int err;
601                 LDAPControl c;
602                 LDAPControl *ctrls[2];
603                 ctrls[0] = &c;
604                 ctrls[1] = NULL;
605
606                 c.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
607                 c.ldctl_value.bv_val = NULL;
608                 c.ldctl_value.bv_len = 0;
609                 c.ldctl_iscritical = manageDSAit > 1;
610
611                 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, ctrls );
612
613                 if( err != LDAP_OPT_SUCCESS ) {
614                         fprintf( stderr, "Could not set ManageDSAit %scontrol\n",
615                                 c.ldctl_iscritical ? "critical " : "" );
616                         if( c.ldctl_iscritical ) {
617                                 exit( EXIT_FAILURE );
618                         }
619                 }
620         }
621
622         rc = 0;
623     if ( fp == NULL ) {
624         for ( ; optind < argc; ++optind ) {
625             rc = dodelete( ld, argv[ optind ] );
626         }
627     } else {
628         while ((rc == 0 || contoper) && fgets(buf, sizeof(buf), fp) != NULL) {
629             buf[ strlen( buf ) - 1 ] = '\0';    /* remove trailing newline */
630             if ( *buf != '\0' ) {
631                 rc = dodelete( ld, buf );
632             }
633         }
634     }
635
636     ldap_unbind( ld );
637
638         return( rc );
639 }
640
641
642 static int dodelete(
643     LDAP        *ld,
644     const char  *dn)
645 {
646         int id;
647         int     rc, code;
648         char *matcheddn = NULL, *text = NULL, **refs = NULL;
649         LDAPMessage *res;
650
651         if ( verbose ) {
652                 printf( "%sdeleting entry \"%s\"\n",
653                         (not ? "!" : ""), dn );
654         }
655
656         if ( not ) {
657                 return LDAP_SUCCESS;
658         }
659
660         /* If prune is on, remove a whole subtree.  Delete the children of the
661          * DN recursively, then the DN requested.
662          */
663         if ( prune ) deletechildren( ld, dn );
664
665         rc = ldap_delete_ext( ld, dn, NULL, NULL, &id );
666         if ( rc != LDAP_SUCCESS ) {
667                 fprintf( stderr, "%s: ldap_delete_ext: %s (%d)\n",
668                         prog, ldap_err2string( rc ), rc );
669                 return rc;
670         }
671
672         rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, NULL, &res );
673         if ( rc < 0 ) {
674                 ldap_perror( ld, "ldapdelete: ldap_result" );
675                 return rc;
676         }
677
678         rc = ldap_parse_result( ld, res, &code, &matcheddn, &text, &refs, NULL, 1 );
679
680         if( rc != LDAP_SUCCESS ) {
681                 fprintf( stderr, "%s: ldap_parse_result: %s (%d)\n",
682                         prog, ldap_err2string( rc ), rc );
683                 return rc;
684         }
685
686         if( verbose || code != LDAP_SUCCESS ||
687                 (matcheddn && *matcheddn) || (text && *text) || (refs && *refs) )
688         {
689                 printf( "Delete Result: %s (%d)\n", ldap_err2string( code ), code );
690
691                 if( text && *text ) {
692                         printf( "Additional info: %s\n", text );
693                 }
694
695                 if( matcheddn && *matcheddn ) {
696                         printf( "Matched DN: %s\n", matcheddn );
697                 }
698
699                 if( refs ) {
700                         int i;
701                         for( i=0; refs[i]; i++ ) {
702                                 printf("Referral: %s\n", refs[i] );
703                         }
704                 }
705         }
706
707         ber_memfree( text );
708         ber_memfree( matcheddn );
709         ber_memvfree( (void **) refs );
710
711         return code;
712 }
713
714 /*
715  * Delete all the children of an entry recursively until leaf nodes are reached.
716  *
717  */
718 static int deletechildren(
719         LDAP *ld,
720         const char *dn )
721 {
722         LDAPMessage *res, *e;
723         int entries;
724         int rc;
725         static char *attrs[] = { "1.1", NULL };
726
727         if ( verbose ) printf ( "deleting children of: %s\n", dn );
728         /*
729          * Do a one level search at dn for children.  For each, delete its children.
730          */
731
732         rc = ldap_search_ext_s( ld, dn, LDAP_SCOPE_ONELEVEL, NULL, attrs, 1,
733                 NULL, NULL, NULL, -1, &res );
734         if ( rc != LDAP_SUCCESS ) {
735                 ldap_perror( ld, "ldap_search" );
736                 return( rc );
737         }
738
739         entries = ldap_count_entries( ld, res );
740
741         if ( entries > 0 ) {
742                 int i;
743
744                 for (e = ldap_first_entry( ld, res ), i = 0; e != NULL;
745                         e = ldap_next_entry( ld, e ), i++ )
746                 {
747                         char *dn = ldap_get_dn( ld, e );
748
749                         if( dn == NULL ) {
750                                 ldap_perror( ld, "ldap_prune" );
751                                 ldap_get_option( ld, LDAP_OPT_ERROR_NUMBER, &rc );
752                                 ber_memfree( dn );
753                                 return rc;
754                         }
755
756                         rc = deletechildren( ld, dn );
757                         if ( rc == -1 ) {
758                                 ldap_perror( ld, "ldap_prune" );
759                                 ber_memfree( dn );
760                                 return rc;
761                         }
762
763                         if ( verbose ) {
764                                 printf( "\tremoving %s\n", dn );
765                         }
766
767                         rc = ldap_delete_s( ld, dn );
768                         if ( rc == -1 ) {
769                                 ldap_perror( ld, "ldap_delete" );
770                                 ber_memfree( dn );
771                                 return rc;
772
773                         }
774                         
775                         if ( verbose ) {
776                                 printf( "\t%s removed\n", dn );
777                         }
778
779                         ber_memfree( dn );
780                 }
781         }
782
783         ldap_msgfree( res );
784         return rc;
785 }