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