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