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