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