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