]> git.sur5r.net Git - openldap/blob - clients/tools/ldapdelete.c
Cleanup previous commit
[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                 usage( prog );
337                 return( EXIT_FAILURE );
338         }
339     }
340
341         if (version == -1) {
342                 version = LDAP_VERSION3;
343         }
344         if (authmethod == -1 && version > LDAP_VERSION2) {
345 #ifdef HAVE_CYRUS_SASL
346                 authmethod = LDAP_AUTH_SASL;
347 #else
348                 authmethod = LDAP_AUTH_SIMPLE;
349 #endif
350         }
351
352     if ( fp == NULL ) {
353         if ( optind >= argc ) {
354             fp = stdin;
355         }
356     }
357
358         if ( debug ) {
359                 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
360                         fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
361                 }
362                 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
363                         fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
364                 }
365         }
366
367 #ifdef SIGPIPE
368         (void) SIGNAL( SIGPIPE, SIG_IGN );
369 #endif
370
371     if (( ld = ldap_init( ldaphost, ldapport )) == NULL ) {
372                 perror( "ldap_init" );
373                 return( EXIT_FAILURE );
374     }
375
376         {
377                 /* this seems prudent for searches below */
378                 int deref = LDAP_DEREF_NEVER;
379                 ldap_set_option( ld, LDAP_OPT_DEREF, &deref );
380         }
381
382         /* chase referrals */
383         if( ldap_set_option( ld, LDAP_OPT_REFERRALS,
384                 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
385         {
386                 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
387                         referrals ? "on" : "off" );
388                 return EXIT_FAILURE;
389         }
390
391         if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version )
392                 != LDAP_OPT_SUCCESS )
393         {
394                 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
395                         version );
396                 return EXIT_FAILURE;
397         }
398
399         if ( use_tls && ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS ) {
400                 if ( use_tls > 1 ) {
401                         ldap_perror( ld, "ldap_start_tls" );
402                         return EXIT_FAILURE;
403                 }
404                 fprintf( stderr, "WARNING: could not start TLS\n" );
405         }
406
407         if (want_bindpw) {
408                 passwd.bv_val = getpassphrase("Enter LDAP Password: ");
409                 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
410         }
411
412         if ( authmethod == LDAP_AUTH_SASL ) {
413 #ifdef HAVE_CYRUS_SASL
414                 if( sasl_secprops != NULL ) {
415                         rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
416                                 (void *) sasl_secprops );
417                         
418                         if( rc != LDAP_OPT_SUCCESS ) {
419                                 fprintf( stderr,
420                                         "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
421                                         sasl_secprops );
422                                 return( EXIT_FAILURE );
423                         }
424                 }
425                 
426                 rc = ldap_sasl_interactive_bind_s( ld, binddn,
427                         sasl_mech, NULL, NULL, lutil_sasl_interact );
428
429                 if( rc != LDAP_SUCCESS ) {
430                         ldap_perror( ld, "ldap_sasl_interactive_bind_s" );
431                         return( EXIT_FAILURE );
432                 }
433 #else
434                 fprintf( stderr, "%s was not compiled with SASL support\n",
435                         argv[0] );
436                 return( EXIT_FAILURE );
437 #endif
438         }
439         else {
440                 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
441                                 != LDAP_SUCCESS ) {
442                         ldap_perror( ld, "ldap_bind" );
443                         return( EXIT_FAILURE );
444                 }
445         }
446
447         if ( manageDSAit ) {
448                 int err;
449                 LDAPControl c;
450                 LDAPControl *ctrls[2];
451                 ctrls[0] = &c;
452                 ctrls[1] = NULL;
453
454                 c.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
455                 c.ldctl_value.bv_val = NULL;
456                 c.ldctl_value.bv_len = 0;
457                 c.ldctl_iscritical = manageDSAit > 1;
458
459                 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, &ctrls );
460
461                 if( err != LDAP_OPT_SUCCESS ) {
462                         fprintf( stderr, "Could not set ManageDSAit %scontrol\n",
463                                 c.ldctl_iscritical ? "critical " : "" );
464                         if( c.ldctl_iscritical ) {
465                                 exit( EXIT_FAILURE );
466                         }
467                 }
468         }
469
470         rc = 0;
471     if ( fp == NULL ) {
472         for ( ; optind < argc; ++optind ) {
473             rc = dodelete( ld, argv[ optind ] );
474         }
475     } else {
476         while ((rc == 0 || contoper) && fgets(buf, sizeof(buf), fp) != NULL) {
477             buf[ strlen( buf ) - 1 ] = '\0';    /* remove trailing newline */
478             if ( *buf != '\0' ) {
479                 rc = dodelete( ld, buf );
480             }
481         }
482     }
483
484     ldap_unbind( ld );
485
486         return( rc );
487 }
488
489
490 static int dodelete(
491     LDAP        *ld,
492     const char  *dn)
493 {
494         int id;
495         int     rc, code;
496         char *matcheddn = NULL, *text = NULL, **refs = NULL;
497         LDAPMessage *res;
498
499         if ( verbose ) {
500                 printf( "%sdeleting entry \"%s\"\n",
501                         (not ? "!" : ""), dn );
502         }
503
504         if ( not ) {
505                 return LDAP_SUCCESS;
506         }
507
508         /* If prune is on, remove a whole subtree.  Delete the children of the
509          * DN recursively, then the DN requested.
510          */
511         if ( prune ) deletechildren( ld, dn );
512
513         rc = ldap_delete_ext( ld, dn, NULL, NULL, &id );
514         if ( rc != LDAP_SUCCESS ) {
515                 fprintf( stderr, "ldapdelete: ldap_delete_ext: %s (%d)\n",
516                         ldap_err2string( rc ), rc );
517                 return rc;
518         }
519
520         rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, NULL, &res );
521         if ( rc < 0 ) {
522                 ldap_perror( ld, "ldapdelete: ldap_result" );
523                 return rc;
524         }
525
526         rc = ldap_parse_result( ld, res, &code, &matcheddn, &text, &refs, NULL, 1 );
527
528         if( rc != LDAP_SUCCESS ) {
529                 fprintf( stderr, "ldapdelete: ldap_parse_result: %s (%d)\n",
530                         ldap_err2string( rc ), rc );
531                 return rc;
532         }
533
534         if( verbose || code != LDAP_SUCCESS ||
535                 (matcheddn && *matcheddn) || (text && *text) || (refs && *refs) )
536         {
537                 printf( "Delete Result: %s (%d)\n", ldap_err2string( code ), code );
538
539                 if( text && *text ) {
540                         printf( "Additional info: %s\n", text );
541                 }
542
543                 if( matcheddn && *matcheddn ) {
544                         printf( "Matched DN: %s\n", matcheddn );
545                 }
546
547                 if( refs ) {
548                         int i;
549                         for( i=0; refs[i]; i++ ) {
550                                 printf("Referral: %s\n", refs[i] );
551                         }
552                 }
553         }
554
555         ber_memfree( text );
556         ber_memfree( matcheddn );
557         ber_memvfree( (void **) refs );
558
559         return code;
560 }
561
562 /*
563  * Delete all the children of an entry recursively until leaf nodes are reached.
564  *
565  */
566 static int deletechildren(
567         LDAP *ld,
568         const char *dn )
569 {
570         LDAPMessage *res, *e;
571         int entries;
572         int rc;
573         static char *attrs[] = { "1.1", NULL };
574
575         if ( verbose ) printf ( "deleting children of: %s\n", dn );
576         /*
577          * Do a one level search at dn for children.  For each, delete its children.
578          */
579
580         rc = ldap_search_ext_s( ld, dn, LDAP_SCOPE_ONELEVEL, NULL, attrs, 1,
581                 NULL, NULL, NULL, -1, &res );
582         if ( rc != LDAP_SUCCESS ) {
583                 ldap_perror( ld, "ldap_search" );
584                 return( rc );
585         }
586
587         entries = ldap_count_entries( ld, res );
588
589         if ( entries > 0 ) {
590                 int i;
591
592                 for (e = ldap_first_entry( ld, res ), i = 0; e != NULL;
593                         e = ldap_next_entry( ld, e ), i++ )
594                 {
595                         char *dn = ldap_get_dn( ld, e );
596
597                         if( dn == NULL ) {
598                                 ldap_perror( ld, "ldap_prune" );
599                                 ldap_get_option( ld, LDAP_OPT_ERROR_NUMBER, &rc );
600                                 ber_memfree( dn );
601                                 return rc;
602                         }
603
604                         rc = deletechildren( ld, dn );
605                         if ( rc == -1 ) {
606                                 ldap_perror( ld, "ldap_prune" );
607                                 ber_memfree( dn );
608                                 return rc;
609                         }
610
611                         if ( verbose ) {
612                                 printf( "\tremoving %s\n", dn );
613                         }
614
615                         rc = ldap_delete_s( ld, dn );
616                         if ( rc == -1 ) {
617                                 ldap_perror( ld, "ldap_delete" );
618                                 ber_memfree( dn );
619                                 return rc;
620
621                         }
622                         
623                         if ( verbose ) {
624                                 printf( "\t%s removed\n", dn );
625                         }
626
627                         ber_memfree( dn );
628                 }
629         }
630
631         ldap_msgfree( res );
632         return rc;
633 }