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