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