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