]> git.sur5r.net Git - openldap/blob - clients/tools/ldapdelete.c
686f9ff764078f01bdb1ab97294e282505b25b74
[openldap] / clients / tools / ldapdelete.c
1 /* ldapdelete.c - simple program to delete an entry using LDAP */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
5  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6  */
7
8 #include "portable.h"
9
10 #include <stdio.h>
11
12 #include <ac/stdlib.h>
13 #include <ac/ctype.h>
14
15 #include <ac/signal.h>
16 #include <ac/string.h>
17 #include <ac/unistd.h>
18
19 #include <ldap.h>
20 #include "lutil_ldap.h"
21 #include "ldap_defaults.h"
22
23 static char     *prog;
24 static char     *binddn = NULL;
25 static struct berval passwd = { 0, NULL };
26 static char     *ldaphost = NULL;
27 static int      ldapport = 0;
28 static int      prune = 0;
29 #ifdef HAVE_CYRUS_SASL
30 static char     *sasl_authc_id = NULL;
31 static char     *sasl_authz_id = NULL;
32 static char     *sasl_mech = NULL;
33 static char     *sasl_secprops = NULL;
34 #endif
35 static int      use_tls = 0;
36 static int      not, verbose, contoper;
37 static LDAP     *ld;
38
39 static int dodelete LDAP_P((
40     LDAP *ld,
41     const char *dn));
42
43 static int deletechildren LDAP_P((
44         LDAP *ld,
45         const char *dn ));
46
47 static void
48 usage( const char *s )
49 {
50         fprintf( stderr,
51 "Delete entries from an LDAP server\n\n"
52 "usage: %s [options] [dn]...\n"
53 "       dn: list of DNs to delete. If not given, it will be readed from stdin\n"
54 "           or from the file specified with \"-f file\".\n"
55 "Delete Options:\n"
56 "       -r\t\tdelete recursively\n"
57 "Common options:\n"
58 "       -c\t\tcontinuous operation mode (do not stop on errors)\n"
59 "       -C\t\tchase referrals\n"
60 "       -d level\tset LDAP debugging level to `level'\n"
61 "       -D binddn\tbind DN\n"
62 "       -f file\t\tread operations from `file'\n"
63 "       -h host\t\tLDAP server\n"
64 "       -k\t\tuse Kerberos authentication\n"
65 "       -K\t\tlike -k, but do only step 1 of the Kerberos bind\n"
66 "       -M\t\tenable Manage DSA IT control (-MM to make it critical)\n"
67 "       -n\t\tshow what would be done but don't actually delete\n"
68 "       -O secprops\tSASL security properties\n"
69 "       -p port\t\tport on LDAP server\n"
70 "       -P version\tprocotol version (default: 3)\n"
71 "       -U user\t\tSASL authentication identity (username)\n"
72 "       -v\t\trun in verbose mode (diagnostics to standard output)\n"
73 "       -w passwd\tbind passwd (for simple authentication)\n"
74 "       -W\t\tprompt for bind passwd\n"
75 "       -x\t\tSimple authentication\n"
76 "       -X id\t\tSASL authorization identity (\"dn:<dn>\" or \"u:<user>\")\n"
77 "       -Y mech\t\tSASL mechanism\n"
78 "       -Z\t\tissue Start TLS request (-ZZ to require successful response)\n"
79 ,               s );
80
81         exit( EXIT_FAILURE );
82 }
83
84
85 int
86 main( int argc, char **argv )
87 {
88         char            buf[ 4096 ];
89         FILE            *fp;
90         int             i, rc, authmethod, referrals, want_bindpw, version, debug, manageDSAit;
91
92     not = verbose = contoper = want_bindpw = debug = manageDSAit = referrals = 0;
93     fp = NULL;
94     authmethod = -1;
95         version = -1;
96
97     prog = (prog = strrchr(argv[0], *LDAP_DIRSEP)) == NULL ? argv[0] : ++prog;
98
99     while (( i = getopt( argc, argv, "cf:r" "Cd:D:h:kKMnO:p:P:U:vw:WxX:Y:Z" )) != EOF ) {
100         switch( i ) {
101         /* Delete Specific Options */
102         case 'c':       /* continuous operation mode */
103             ++contoper;
104             break;
105         case 'f':       /* read DNs from a file */
106             if (( fp = fopen( optarg, "r" )) == NULL ) {
107                 perror( optarg );
108                 exit( EXIT_FAILURE );
109             }
110             break;
111         case 'r':
112                 prune = 1;
113                 break;
114
115         /* Common Options */
116         case 'C':
117                 referrals++;
118                 break;
119         case 'd':
120             debug |= atoi( optarg );
121             break;
122         case 'D':       /* bind DN */
123             binddn = strdup( optarg );
124             break;
125         case 'h':       /* ldap host */
126             ldaphost = strdup( optarg );
127             break;
128         case 'k':       /* kerberos bind */
129 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
130                 if( version > LDAP_VERSION2 ) {
131                         fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
132                                 prog, version );
133                         return EXIT_FAILURE;
134                 }
135
136                 if( authmethod != -1 ) {
137                         fprintf( stderr, "%s: -k incompatible with previous "
138                                 "authentication choice\n", prog );
139                         return EXIT_FAILURE;
140                 }
141                         
142                 authmethod = LDAP_AUTH_KRBV4;
143 #else
144                 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
145                 return EXIT_FAILURE;
146 #endif
147             break;
148         case 'K':       /* kerberos bind, part one only */
149 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
150                 if( version > LDAP_VERSION2 ) {
151                         fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
152                                 prog, version );
153                         return EXIT_FAILURE;
154                 }
155                 if( authmethod != -1 ) {
156                         fprintf( stderr, "%s: incompatible with previous "
157                                 "authentication choice\n", prog );
158                         return EXIT_FAILURE;
159                 }
160
161                 authmethod = LDAP_AUTH_KRBV41;
162 #else
163                 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
164                 return( EXIT_FAILURE );
165 #endif
166             break;
167         case 'M':
168                 /* enable Manage DSA IT */
169                 if( version == LDAP_VERSION2 ) {
170                         fprintf( stderr, "%s: -M incompatible with LDAPv%d\n",
171                                 prog, version );
172                         return EXIT_FAILURE;
173                 }
174                 manageDSAit++;
175                 version = LDAP_VERSION3;
176                 break;
177         case 'n':       /* print deletes, don't actually do them */
178             ++not;
179             break;
180         case 'O':
181 #ifdef HAVE_CYRUS_SASL
182                 if( version == LDAP_VERSION2 ) {
183                         fprintf( stderr, "%s -O incompatible with LDAPv%d\n",
184                                 prog, version );
185                         return EXIT_FAILURE;
186                 }
187                 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
188                         fprintf( stderr, "%s: incompatible previous "
189                                 "authentication choice\n", prog );
190                         return EXIT_FAILURE;
191                 }
192                 sasl_secprops = strdup( optarg );
193                 authmethod = LDAP_AUTH_SASL;
194                 version = LDAP_VERSION3;
195 #else
196                 fprintf( stderr, "%s: not compiled with SASL support\n",
197                         prog );
198                 return( EXIT_FAILURE );
199 #endif
200                 break;
201         case 'p':
202             ldapport = atoi( optarg );
203             break;
204         case 'P':
205                 switch( atoi(optarg) ) {
206                 case 2:
207                         if( version == LDAP_VERSION3 ) {
208                                 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
209                                         prog, version );
210                                 return EXIT_FAILURE;
211                         }
212                         version = LDAP_VERSION2;
213                         break;
214                 case 3:
215                         if( version == LDAP_VERSION2 ) {
216                                 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
217                                         prog, version );
218                                 return EXIT_FAILURE;
219                         }
220                         version = LDAP_VERSION3;
221                         break;
222                 default:
223                         fprintf( stderr, "%s: protocol version should be 2 or 3\n",
224                                 prog );
225                         usage( prog );
226                         return( EXIT_FAILURE );
227                 } break;
228         case 'U':
229 #ifdef HAVE_CYRUS_SASL
230                 if( version == LDAP_VERSION2 ) {
231                         fprintf( stderr, "%s: -U incompatible with version %d\n",
232                                 prog, version );
233                         return EXIT_FAILURE;
234                 }
235                 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
236                         fprintf( stderr, "%s: incompatible previous "
237                                 "authentication choice\n",
238                                 prog );
239                         return EXIT_FAILURE;
240                 }
241                 authmethod = LDAP_AUTH_SASL;
242                 version = LDAP_VERSION3;
243
244                 sasl_authc_id = strdup( optarg );
245                 authmethod = LDAP_AUTH_SASL;
246 #else
247                 fprintf( stderr, "%s: was not compiled with SASL support\n",
248                         prog );
249                 return( EXIT_FAILURE );
250 #endif
251                 break;
252         case 'v':       /* verbose mode */
253             verbose++;
254             break;
255         case 'w':       /* password */
256             passwd.bv_val = strdup( optarg );
257                 {
258                         char* p;
259
260                         for( p = optarg; *p == '\0'; p++ ) {
261                                 *p = '*';
262                         }
263                 }
264                 passwd.bv_len = strlen( passwd.bv_val );
265             break;
266         case 'W':
267                 want_bindpw++;
268                 break;
269         case 'Y':
270 #ifdef HAVE_CYRUS_SASL
271                 if( version == LDAP_VERSION2 ) {
272                         fprintf( stderr, "%s: -Y incompatible with version %d\n",
273                                 prog, version );
274                         return EXIT_FAILURE;
275                 }
276                 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
277                         fprintf( stderr, "%s: incompatible with authentication choice\n", prog );
278                         return EXIT_FAILURE;
279                 }
280
281                 authmethod = LDAP_AUTH_SASL;
282                 version = LDAP_VERSION3;
283 #else
284                 fprintf( stderr, "%s: was not compiled with SASL support\n",
285                         prog );
286                 return( EXIT_FAILURE );
287 #endif
288                 break;
289         case 'x':
290                 if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
291                         fprintf( stderr, "%s: incompatible with previous "
292                                 "authentication choice\n", prog );
293                         return EXIT_FAILURE;
294                 }
295                 authmethod = LDAP_AUTH_SIMPLE;
296                 break;
297         case 'X':
298 #ifdef HAVE_CYRUS_SASL
299                 if( version == LDAP_VERSION2 ) {
300                         fprintf( stderr, "%s: -X incompatible with LDAPv%d\n",
301                                 prog, version );
302                         return EXIT_FAILURE;
303                 }
304                 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
305                         fprintf( stderr, "%s: -X incompatible with "
306                                 "authentication choice\n", prog );
307                         return EXIT_FAILURE;
308                 }
309                 authmethod = LDAP_AUTH_SASL;
310                 version = LDAP_VERSION3;
311
312                 sasl_authz_id = strdup( optarg );
313                 authmethod = LDAP_AUTH_SASL;
314 #else
315                 fprintf( stderr, "%s: not compiled with SASL support\n",
316                         prog );
317                 return( EXIT_FAILURE );
318 #endif
319                 break;
320         case 'Z':
321 #ifdef HAVE_TLS
322                 if( version == LDAP_VERSION2 ) {
323                         fprintf( stderr, "%s -Z incompatible with version %d\n",
324                                 prog, version );
325                         return EXIT_FAILURE;
326                 }
327                 version = LDAP_VERSION3;
328                 use_tls++;
329 #else
330                 fprintf( stderr, "%s: not compiled with TLS support\n",
331                         prog );
332                 return( EXIT_FAILURE );
333 #endif
334                 break;
335         default:
336                 fprintf( stderr, "%s: unrecongized option -%c\n",
337                         prog, optopt );
338                 usage( prog );
339                 return( EXIT_FAILURE );
340         }
341     }
342
343         if (version == -1) {
344                 version = LDAP_VERSION3;
345         }
346         if (authmethod == -1 && version > LDAP_VERSION2) {
347 #ifdef HAVE_CYRUS_SASL
348                 authmethod = LDAP_AUTH_SASL;
349 #else
350                 authmethod = LDAP_AUTH_SIMPLE;
351 #endif
352         }
353
354     if ( fp == NULL ) {
355         if ( optind >= argc ) {
356             fp = stdin;
357         }
358     }
359
360         if ( debug ) {
361                 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
362                         fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
363                 }
364                 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
365                         fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
366                 }
367         }
368
369 #ifdef SIGPIPE
370         (void) SIGNAL( SIGPIPE, SIG_IGN );
371 #endif
372
373     if (( ld = ldap_init( ldaphost, ldapport )) == NULL ) {
374                 perror( "ldap_init" );
375                 return( EXIT_FAILURE );
376     }
377
378         {
379                 /* this seems prudent for searches below */
380                 int deref = LDAP_DEREF_NEVER;
381                 ldap_set_option( ld, LDAP_OPT_DEREF, &deref );
382         }
383
384         /* chase referrals */
385         if( ldap_set_option( ld, LDAP_OPT_REFERRALS,
386                 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
387         {
388                 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
389                         referrals ? "on" : "off" );
390                 return EXIT_FAILURE;
391         }
392
393         if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version )
394                 != LDAP_OPT_SUCCESS )
395         {
396                 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
397                         version );
398                 return EXIT_FAILURE;
399         }
400
401         if ( use_tls && ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS ) {
402                 if ( use_tls > 1 ) {
403                         ldap_perror( ld, "ldap_start_tls" );
404                         return EXIT_FAILURE;
405                 }
406                 fprintf( stderr, "WARNING: could not start TLS\n" );
407         }
408
409         if (want_bindpw) {
410                 passwd.bv_val = getpassphrase("Enter LDAP Password: ");
411                 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
412         }
413
414         if ( authmethod == LDAP_AUTH_SASL ) {
415 #ifdef HAVE_CYRUS_SASL
416                 if( sasl_secprops != NULL ) {
417                         rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
418                                 (void *) sasl_secprops );
419                         
420                         if( rc != LDAP_OPT_SUCCESS ) {
421                                 fprintf( stderr,
422                                         "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
423                                         sasl_secprops );
424                                 return( EXIT_FAILURE );
425                         }
426                 }
427                 
428                 rc = ldap_sasl_interactive_bind_s( ld, binddn,
429                         sasl_mech, NULL, NULL, lutil_sasl_interact );
430
431                 if( rc != LDAP_SUCCESS ) {
432                         ldap_perror( ld, "ldap_sasl_interactive_bind_s" );
433                         return( EXIT_FAILURE );
434                 }
435 #else
436                 fprintf( stderr, "%s was not compiled with SASL support\n",
437                         argv[0] );
438                 return( EXIT_FAILURE );
439 #endif
440         }
441         else {
442                 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
443                                 != LDAP_SUCCESS ) {
444                         ldap_perror( ld, "ldap_bind" );
445                         return( EXIT_FAILURE );
446                 }
447         }
448
449         if ( manageDSAit ) {
450                 int err;
451                 LDAPControl c;
452                 LDAPControl *ctrls[2];
453                 ctrls[0] = &c;
454                 ctrls[1] = NULL;
455
456                 c.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
457                 c.ldctl_value.bv_val = NULL;
458                 c.ldctl_value.bv_len = 0;
459                 c.ldctl_iscritical = manageDSAit > 1;
460
461                 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, &ctrls );
462
463                 if( err != LDAP_OPT_SUCCESS ) {
464                         fprintf( stderr, "Could not set ManageDSAit %scontrol\n",
465                                 c.ldctl_iscritical ? "critical " : "" );
466                         if( c.ldctl_iscritical ) {
467                                 exit( EXIT_FAILURE );
468                         }
469                 }
470         }
471
472         rc = 0;
473     if ( fp == NULL ) {
474         for ( ; optind < argc; ++optind ) {
475             rc = dodelete( ld, argv[ optind ] );
476         }
477     } else {
478         while ((rc == 0 || contoper) && fgets(buf, sizeof(buf), fp) != NULL) {
479             buf[ strlen( buf ) - 1 ] = '\0';    /* remove trailing newline */
480             if ( *buf != '\0' ) {
481                 rc = dodelete( ld, buf );
482             }
483         }
484     }
485
486     ldap_unbind( ld );
487
488         return( rc );
489 }
490
491
492 static int dodelete(
493     LDAP        *ld,
494     const char  *dn)
495 {
496         int id;
497         int     rc, code;
498         char *matcheddn = NULL, *text = NULL, **refs = NULL;
499         LDAPMessage *res;
500
501         if ( verbose ) {
502                 printf( "%sdeleting entry \"%s\"\n",
503                         (not ? "!" : ""), dn );
504         }
505
506         if ( not ) {
507                 return LDAP_SUCCESS;
508         }
509
510         /* If prune is on, remove a whole subtree.  Delete the children of the
511          * DN recursively, then the DN requested.
512          */
513         if ( prune ) deletechildren( ld, dn );
514
515         rc = ldap_delete_ext( ld, dn, NULL, NULL, &id );
516         if ( rc != LDAP_SUCCESS ) {
517                 fprintf( stderr, "ldapdelete: ldap_delete_ext: %s (%d)\n",
518                         ldap_err2string( rc ), rc );
519                 return rc;
520         }
521
522         rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, NULL, &res );
523         if ( rc < 0 ) {
524                 ldap_perror( ld, "ldapdelete: ldap_result" );
525                 return rc;
526         }
527
528         rc = ldap_parse_result( ld, res, &code, &matcheddn, &text, &refs, NULL, 1 );
529
530         if( rc != LDAP_SUCCESS ) {
531                 fprintf( stderr, "ldapdelete: ldap_parse_result: %s (%d)\n",
532                         ldap_err2string( rc ), rc );
533                 return rc;
534         }
535
536         if( verbose || code != LDAP_SUCCESS ||
537                 (matcheddn && *matcheddn) || (text && *text) || (refs && *refs) )
538         {
539                 printf( "Delete Result: %s (%d)\n", ldap_err2string( code ), code );
540
541                 if( text && *text ) {
542                         printf( "Additional info: %s\n", text );
543                 }
544
545                 if( matcheddn && *matcheddn ) {
546                         printf( "Matched DN: %s\n", matcheddn );
547                 }
548
549                 if( refs ) {
550                         int i;
551                         for( i=0; refs[i]; i++ ) {
552                                 printf("Referral: %s\n", refs[i] );
553                         }
554                 }
555         }
556
557         ber_memfree( text );
558         ber_memfree( matcheddn );
559         ber_memvfree( (void **) refs );
560
561         return code;
562 }
563
564 /*
565  * Delete all the children of an entry recursively until leaf nodes are reached.
566  *
567  */
568 static int deletechildren(
569         LDAP *ld,
570         const char *dn )
571 {
572         LDAPMessage *res, *e;
573         int entries;
574         int rc;
575         static char *attrs[] = { "1.1", NULL };
576
577         if ( verbose ) printf ( "deleting children of: %s\n", dn );
578         /*
579          * Do a one level search at dn for children.  For each, delete its children.
580          */
581
582         rc = ldap_search_ext_s( ld, dn, LDAP_SCOPE_ONELEVEL, NULL, attrs, 1,
583                 NULL, NULL, NULL, -1, &res );
584         if ( rc != LDAP_SUCCESS ) {
585                 ldap_perror( ld, "ldap_search" );
586                 return( rc );
587         }
588
589         entries = ldap_count_entries( ld, res );
590
591         if ( entries > 0 ) {
592                 int i;
593
594                 for (e = ldap_first_entry( ld, res ), i = 0; e != NULL;
595                         e = ldap_next_entry( ld, e ), i++ )
596                 {
597                         char *dn = ldap_get_dn( ld, e );
598
599                         if( dn == NULL ) {
600                                 ldap_perror( ld, "ldap_prune" );
601                                 ldap_get_option( ld, LDAP_OPT_ERROR_NUMBER, &rc );
602                                 ber_memfree( dn );
603                                 return rc;
604                         }
605
606                         rc = deletechildren( ld, dn );
607                         if ( rc == -1 ) {
608                                 ldap_perror( ld, "ldap_prune" );
609                                 ber_memfree( dn );
610                                 return rc;
611                         }
612
613                         if ( verbose ) {
614                                 printf( "\tremoving %s\n", dn );
615                         }
616
617                         rc = ldap_delete_s( ld, dn );
618                         if ( rc == -1 ) {
619                                 ldap_perror( ld, "ldap_delete" );
620                                 ber_memfree( dn );
621                                 return rc;
622
623                         }
624                         
625                         if ( verbose ) {
626                                 printf( "\t%s removed\n", dn );
627                         }
628
629                         ber_memfree( dn );
630                 }
631         }
632
633         ldap_msgfree( res );
634         return rc;
635 }