]> git.sur5r.net Git - openldap/blob - clients/tools/ldapdelete.c
229a63b5fd90d8e9422ae5b92c38981ccbc243ee
[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     char        *dn));
40
41 static int deletechildren LDAP_P(( LDAP *ld,
42                                    char *dn ));
43
44 static void
45 usage( const char *s )
46 {
47         fprintf( stderr,
48 "Delete entries from an LDAP server\n\n"
49 "usage: %s [options] [dn]...\n"
50 "       dn: list of DNs to delete. If not given, it will be readed from stdin\n"
51 "           or from the file specified with \"-f file\".\n"
52 "options:\n"
53 "       -c\t\tcontinuous operation mode (do not stop on errors)\n"
54 "       -d level\tset LDAP debugging level to `level'\n"
55 "       -D binddn\tbind DN\n"
56 "       -E\t\trequest SASL privacy (-EE to make it critical)\n"
57 "       -f file\t\tdelete DNs listed in `file'\n"
58 "       -h host\t\tLDAP server\n"
59 "       -I\t\trequest SASL integrity checking (-II to make it\n"
60 "               \tcritical)\n"
61 "       -k\t\tuse Kerberos authentication\n"
62 "       -K\t\tlike -k, but do only step 1 of the Kerberos bind\n"
63 "       -M\t\tenable Manage DSA IT control (-MM to make it critical)\n"
64 "       -n\t\tshow what would be done but don't actually delete\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, want_bindpw, version, debug, manageDSAit;
87
88     not = verbose = contoper = want_bindpw = debug = manageDSAit = 0;
89     fp = NULL;
90     authmethod = LDAP_AUTH_SIMPLE;
91         version = -1;
92
93     while (( i = getopt( argc, argv, "cD:d:Ef:h:IKkMnP: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 'h':       /* ldap host */
115             ldaphost = strdup( optarg );
116             break;
117         case 'D':       /* bind DN */
118             binddn = strdup( optarg );
119             break;
120         case 'w':       /* password */
121             passwd.bv_val = strdup( optarg );
122                 {
123                         char* p;
124
125                         for( p = optarg; *p == '\0'; p++ ) {
126                                 *p = '*';
127                         }
128                 }
129                 passwd.bv_len = strlen( passwd.bv_val );
130             break;
131         case 'f':       /* read DNs from a file */
132             if (( fp = fopen( optarg, "r" )) == NULL ) {
133                 perror( optarg );
134                 exit( EXIT_FAILURE );
135             }
136             break;
137         case 'd':
138             debug |= atoi( optarg );
139             break;
140         case 'p':
141             ldapport = atoi( optarg );
142             break;
143         case 'n':       /* print deletes, don't actually do them */
144             ++not;
145             break;
146         case 'r':
147                 prune = 1;
148                 break;
149         case 'v':       /* verbose mode */
150             verbose++;
151             break;
152         case 'M':
153                 /* enable Manage DSA IT */
154                 manageDSAit++;
155                 break;
156         case 'W':
157                 want_bindpw++;
158                 break;
159         case 'P':
160                 switch( atoi(optarg) )
161                 {
162                 case 2:
163                         version = LDAP_VERSION2;
164                         break;
165                 case 3:
166                         version = LDAP_VERSION3;
167                         break;
168                 default:
169                         fprintf( stderr, "protocol version should be 2 or 3\n" );
170                         usage( argv[0] );
171                         return( EXIT_FAILURE );
172                 }
173                 break;
174         case 'I':
175 #ifdef HAVE_CYRUS_SASL
176                 sasl_integrity++;
177                 authmethod = LDAP_AUTH_SASL;
178 #else
179                 fprintf( stderr, "%s was not compiled with SASL support\n",
180                         argv[0] );
181                 return( EXIT_FAILURE );
182 #endif
183                 break;
184         case 'E':
185 #ifdef HAVE_CYRUS_SASL
186                 sasl_privacy++;
187                 authmethod = LDAP_AUTH_SASL;
188 #else
189                 fprintf( stderr, "%s was not compiled with SASL support\n",
190                         argv[0] );
191                 return( EXIT_FAILURE );
192 #endif
193                 break;
194         case 'Y':
195 #ifdef HAVE_CYRUS_SASL
196                 if ( strcasecmp( optarg, "any" ) && strcmp( optarg, "*" ) ) {
197                         sasl_mech = strdup( optarg );
198                 }
199                 authmethod = LDAP_AUTH_SASL;
200 #else
201                 fprintf( stderr, "%s was not compiled with SASL support\n",
202                         argv[0] );
203                 return( EXIT_FAILURE );
204 #endif
205                 break;
206         case 'U':
207 #ifdef HAVE_CYRUS_SASL
208                 sasl_authc_id = strdup( optarg );
209                 authmethod = LDAP_AUTH_SASL;
210 #else
211                 fprintf( stderr, "%s was not compiled with SASL support\n",
212                         argv[0] );
213                 return( EXIT_FAILURE );
214 #endif
215                 break;
216         case 'X':
217 #ifdef HAVE_CYRUS_SASL
218                 sasl_authz_id = strdup( optarg );
219                 authmethod = LDAP_AUTH_SASL;
220 #else
221                 fprintf( stderr, "%s was not compiled with SASL support\n",
222                         argv[0] );
223                 return( EXIT_FAILURE );
224 #endif
225                 break;
226         case 'Z':
227 #ifdef HAVE_TLS
228                 use_tls++;
229 #else
230                 fprintf( stderr, "%s was not compiled with TLS support\n",
231                         argv[0] );
232                 return( EXIT_FAILURE );
233 #endif
234                 break;
235         default:
236                 usage( argv[0] );
237                 return( EXIT_FAILURE );
238         }
239     }
240
241         if ( ( authmethod == LDAP_AUTH_KRBV4 ) || ( authmethod ==
242                         LDAP_AUTH_KRBV41 ) ) {
243                 if( version > LDAP_VERSION2 ) {
244                         fprintf( stderr, "Kerberos requires LDAPv2\n" );
245                         return( EXIT_FAILURE );
246                 }
247                 version = LDAP_VERSION2;
248         }
249         else if ( authmethod == LDAP_AUTH_SASL ) {
250                 if( version != -1 && version != LDAP_VERSION3 ) {
251                         fprintf( stderr, "SASL requires LDAPv3\n" );
252                         return( EXIT_FAILURE );
253                 }
254                 version = LDAP_VERSION3;
255         }
256
257         if( manageDSAit ) {
258                 if( version != -1 && version != LDAP_VERSION3 ) {
259                         fprintf(stderr, "manage DSA control requires LDAPv3\n");
260                         return EXIT_FAILURE;
261                 }
262                 version = LDAP_VERSION3;
263         }
264
265         if( use_tls ) {
266                 if( version != -1 && version != LDAP_VERSION3 ) {
267                         fprintf(stderr, "Start TLS requires LDAPv3\n");
268                         return EXIT_FAILURE;
269                 }
270                 version = LDAP_VERSION3;
271         }
272
273     if ( fp == NULL ) {
274         if ( optind >= argc ) {
275             fp = stdin;
276         }
277     }
278
279         if ( debug ) {
280                 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
281                         fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
282                 }
283                 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
284                         fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
285                 }
286         }
287
288 #ifdef SIGPIPE
289         (void) SIGNAL( SIGPIPE, SIG_IGN );
290 #endif
291
292     if (( ld = ldap_init( ldaphost, ldapport )) == NULL ) {
293         perror( "ldap_init" );
294         return( EXIT_FAILURE );
295     }
296
297         {
298                 /* this seems prudent */
299                 int deref = LDAP_DEREF_NEVER;
300                 ldap_set_option( ld, LDAP_OPT_DEREF, &deref );
301         }
302
303         /* don't chase referrals */
304         ldap_set_option( ld, LDAP_OPT_REFERRALS, LDAP_OPT_OFF );
305
306         if (version == -1 ) {
307                 version = 3;
308         }
309
310         if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version )
311                 != LDAP_OPT_SUCCESS )
312         {
313                 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
314                         version );
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         }
323
324         if (want_bindpw) {
325                 passwd.bv_val = getpassphrase("Enter LDAP Password: ");
326                 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
327         }
328
329         if ( authmethod == LDAP_AUTH_SASL ) {
330 #ifdef HAVE_CYRUS_SASL
331                 int     minssf = 0, maxssf = 0;
332
333                 if ( sasl_integrity > 0 )
334                         maxssf = 1;
335                 if ( sasl_integrity > 1 )
336                         minssf = 1;
337                 if ( sasl_privacy > 0 )
338                         maxssf = 100000; /* Something big value */
339                 if ( sasl_privacy > 1 )
340                         minssf = 56;
341                 
342                 if ( ldap_set_option( ld, LDAP_OPT_X_SASL_MINSSF,
343                                 (void *)&minssf ) != LDAP_OPT_SUCCESS ) {
344                         fprintf( stderr, "Could not set LDAP_OPT_X_SASL_MINSSF"
345                                 "%d\n", minssf);
346                         return( EXIT_FAILURE );
347                 }
348                 if ( ldap_set_option( ld, LDAP_OPT_X_SASL_MAXSSF,
349                                 (void *)&maxssf ) != LDAP_OPT_SUCCESS ) {
350                         fprintf( stderr, "Could not set LDAP_OPT_X_SASL_MAXSSF"
351                                 "%d\n", maxssf);
352                         return( EXIT_FAILURE );
353                 }
354                 
355                 rc = ldap_negotiated_sasl_bind_s( ld, binddn, sasl_authc_id,
356                                 sasl_authz_id, sasl_mech,
357                                 passwd.bv_len ? &passwd : NULL,
358                                 NULL, NULL );
359
360                 if( rc != LDAP_SUCCESS ) {
361                         ldap_perror( ld, "ldap_negotiated_sasl_bind_s" );
362                         return( EXIT_FAILURE );
363                 }
364 #else
365                 fprintf( stderr, "%s was not compiled with SASL support\n",
366                         argv[0] );
367                 return( EXIT_FAILURE );
368 #endif
369         }
370         else {
371                 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
372                                 != LDAP_SUCCESS ) {
373                         ldap_perror( ld, "ldap_bind" );
374                         return( EXIT_FAILURE );
375                 }
376         }
377
378         if ( manageDSAit ) {
379                 int err;
380                 LDAPControl c;
381                 LDAPControl *ctrls[2];
382                 ctrls[0] = &c;
383                 ctrls[1] = NULL;
384
385                 c.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
386                 c.ldctl_value.bv_val = NULL;
387                 c.ldctl_value.bv_len = 0;
388                 c.ldctl_iscritical = manageDSAit > 1;
389
390                 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, &ctrls );
391
392                 if( err != LDAP_OPT_SUCCESS ) {
393                         fprintf( stderr, "Could not set Manage DSA IT Control\n" );
394                         if( c.ldctl_iscritical ) {
395                                 exit( EXIT_FAILURE );
396                         }
397                 }
398         }
399
400         rc = 0;
401     if ( fp == NULL ) {
402         for ( ; optind < argc; ++optind ) {
403             rc = dodelete( ld, argv[ optind ] );
404         }
405     } else {
406         while ((rc == 0 || contoper) && fgets(buf, sizeof(buf), fp) != NULL) {
407             buf[ strlen( buf ) - 1 ] = '\0';    /* remove trailing newline */
408             if ( *buf != '\0' ) {
409                 rc = dodelete( ld, buf );
410             }
411         }
412     }
413
414     ldap_unbind( ld );
415
416         return( rc );
417 }
418
419
420 static int dodelete(
421     LDAP        *ld,
422     char        *dn)
423 {
424     int rc;
425
426     if ( verbose ) {
427         printf( "%sdeleting entry \"%s\"\n",
428                 (not ? "!" : ""), dn );
429     }
430     if ( not ) {
431         rc = LDAP_SUCCESS;
432     } else {
433                 /* If prune is on, remove a whole subtree.  Delete the children of the
434                  * DN recursively, then the DN requested.
435                  */
436                 if ( prune ) deletechildren( ld, dn );
437                 if (( rc = ldap_delete_s( ld, dn )) != LDAP_SUCCESS ) {
438                         ldap_perror( ld, "ldap_delete" );
439         } else if ( verbose ) {
440             printf( "\tremoved\n" );
441         }
442     }
443
444     return( rc );
445 }
446
447 /*
448  * Delete all the children of an entry recursively until leaf nodes are reached.
449  *
450  */
451 static int deletechildren( LDAP *ld,
452                            char *dn )
453 {
454     LDAPMessage *res, *e;
455     int entries;
456     int rc;
457         int timeout = 30 * 10000;
458
459     ldap_set_option( ld, LDAP_OPT_TIMEOUT, &timeout );
460     if ( verbose ) printf ( "deleting children of: %s\n", dn );
461     /*
462      * Do a one level search at dn for children.  For each, delete its children.
463      */
464     if ( ldap_search_s( ld, dn, LDAP_SCOPE_ONELEVEL, NULL, NULL, 0, &res ) == -1 )
465     {
466         ldap_perror( ld, "ldap_search" );
467                 ldap_get_option( ld, LDAP_OPT_ERROR_NUMBER, &rc );
468         return( rc );
469     }
470
471     entries = ldap_count_entries( ld, res );
472     if ( entries > 0 )
473     {
474         int i;
475
476         for (e = ldap_first_entry( ld, res ), i = 0; e != NULL;
477              e = ldap_next_entry( ld, e ), i++ )
478         {
479             if ( (rc = deletechildren( ld, ldap_get_dn( ld, e) )) == -1 )
480             {
481                 ldap_perror( ld, "ldap_prune" );
482                 return rc;
483             }
484             if ( verbose )
485             {
486                 printf( "\tremoving %s\n", ldap_get_dn( ld, e ) );
487             }
488             if ( ( rc = ldap_delete_s( ld, ldap_get_dn( ld, e ) ) ) == -1 )
489             {
490                 ldap_perror( ld, "ldap_delete" );
491                 return rc;
492             }
493             else if ( verbose )
494             {
495                 printf( "\t%s removed\n", ldap_get_dn( ld, e ) );
496             }
497         }
498     }
499     ldap_msgfree( res );
500     return rc;
501 }