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