]> git.sur5r.net Git - openldap/blob - clients/tools/ldapdelete.c
bd2a61f1bf1c8876bbd9db401281f0f95e146684
[openldap] / clients / tools / ldapdelete.c
1 /* ldapdelete.c - simple program to delete an entry using LDAP */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-1999 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 char     *passwd = 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 = strdup( optarg );
123                 {
124                         char* p;
125
126                         for( p = optarg; *p == '\0'; p++ ) {
127                                 *p = '*';
128                         }
129                 }
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         }
248         else if ( authmethod == LDAP_AUTH_SASL ) {
249                 if( version != LDAP_VERSION3 ) {
250                         fprintf( stderr, "SASL requires LDAPv3\n" );
251                         return( EXIT_FAILURE );
252                 }
253         }
254
255         if( manageDSAit ) {
256                 if( version != LDAP_VERSION3 ) {
257                         fprintf(stderr, "manage DSA control requires LDAPv3\n");
258                         return EXIT_FAILURE;
259                 }
260         }
261
262     if ( fp == NULL ) {
263         if ( optind >= argc ) {
264             fp = stdin;
265         }
266     }
267
268         if ( debug ) {
269                 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
270                         fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
271                 }
272                 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
273                         fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
274                 }
275         }
276
277 #ifdef SIGPIPE
278         (void) SIGNAL( SIGPIPE, SIG_IGN );
279 #endif
280
281     if (( ld = ldap_init( ldaphost, ldapport )) == NULL ) {
282         perror( "ldap_init" );
283         return( EXIT_FAILURE );
284     }
285
286         {
287                 /* this seems prudent */
288                 int deref = LDAP_DEREF_NEVER;
289                 ldap_set_option( ld, LDAP_OPT_DEREF, &deref );
290         }
291
292         /* don't chase referrals */
293         ldap_set_option( ld, LDAP_OPT_REFERRALS, LDAP_OPT_OFF );
294
295         if (version != -1 &&
296                 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version ) != LDAP_OPT_SUCCESS)
297         {
298                 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n", version );
299         }
300
301         if ( use_tls && ldap_start_tls( ld, NULL, NULL ) != LDAP_SUCCESS ) {
302                 if ( use_tls > 1 ) {
303                         ldap_perror( ld, "ldap_start_tls" );
304                         return( EXIT_FAILURE );
305                 }
306         }
307
308         if (want_bindpw)
309                 passwd = getpass("Enter LDAP Password: ");
310
311         if ( authmethod == LDAP_AUTH_SASL ) {
312 #ifdef HAVE_CYRUS_SASL
313                 int     minssf = 0, maxssf = 0;
314
315                 if ( sasl_integrity > 0 )
316                         maxssf = 1;
317                 if ( sasl_integrity > 1 )
318                         minssf = 1;
319                 if ( sasl_privacy > 0 )
320                         maxssf = 100000; /* Something big value */
321                 if ( sasl_privacy > 1 )
322                         minssf = 56;
323                 
324                 if ( ldap_set_option( ld, LDAP_OPT_X_SASL_MINSSF,
325                                 (void *)&minssf ) != LDAP_OPT_SUCCESS ) {
326                         fprintf( stderr, "Could not set LDAP_OPT_X_SASL_MINSSF"
327                                 "%d\n", minssf);
328                         return( EXIT_FAILURE );
329                 }
330                 if ( ldap_set_option( ld, LDAP_OPT_X_SASL_MAXSSF,
331                                 (void *)&maxssf ) != LDAP_OPT_SUCCESS ) {
332                         fprintf( stderr, "Could not set LDAP_OPT_X_SASL_MAXSSF"
333                                 "%d\n", maxssf);
334                         return( EXIT_FAILURE );
335                 }
336                 
337                 if ( ldap_negotiated_sasl_bind_s( ld, binddn, sasl_authc_id,
338                                 sasl_authz_id, sasl_mech, NULL, NULL, NULL )
339                                         != LDAP_SUCCESS ) {
340                         ldap_perror( ld, "ldap_sasl_bind" );
341                         return( EXIT_FAILURE );
342                 }
343 #else
344                 fprintf( stderr, "%s was not compiled with SASL support\n",
345                         argv[0] );
346                 return( EXIT_FAILURE );
347 #endif
348         }
349         else {
350                 if ( ldap_bind_s( ld, binddn, passwd, authmethod )
351                                 != LDAP_SUCCESS ) {
352                         ldap_perror( ld, "ldap_bind" );
353                         return( EXIT_FAILURE );
354                 }
355         }
356
357         if ( manageDSAit ) {
358                 int err;
359                 LDAPControl c;
360                 LDAPControl *ctrls[2];
361                 ctrls[0] = &c;
362                 ctrls[1] = NULL;
363
364                 c.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
365                 c.ldctl_value.bv_val = NULL;
366                 c.ldctl_value.bv_len = 0;
367                 c.ldctl_iscritical = manageDSAit > 1;
368
369                 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, &ctrls );
370
371                 if( err != LDAP_OPT_SUCCESS ) {
372                         fprintf( stderr, "Could not set Manage DSA IT Control\n" );
373                         if( c.ldctl_iscritical ) {
374                                 exit( EXIT_FAILURE );
375                         }
376                 }
377         }
378
379         rc = 0;
380     if ( fp == NULL ) {
381         for ( ; optind < argc; ++optind ) {
382             rc = dodelete( ld, argv[ optind ] );
383         }
384     } else {
385         while ((rc == 0 || contoper) && fgets(buf, sizeof(buf), fp) != NULL) {
386             buf[ strlen( buf ) - 1 ] = '\0';    /* remove trailing newline */
387             if ( *buf != '\0' ) {
388                 rc = dodelete( ld, buf );
389             }
390         }
391     }
392
393     ldap_unbind( ld );
394
395         return( rc );
396 }
397
398
399 static int dodelete(
400     LDAP        *ld,
401     char        *dn)
402 {
403     int rc;
404
405     if ( verbose ) {
406         printf( "%sdeleting entry \"%s\"\n",
407                 (not ? "!" : ""), dn );
408     }
409     if ( not ) {
410         rc = LDAP_SUCCESS;
411     } else {
412                 /* If prune is on, remove a whole subtree.  Delete the children of the
413                  * DN recursively, then the DN requested.
414                  */
415                 if ( prune ) deletechildren( ld, dn );
416                 if (( rc = ldap_delete_s( ld, dn )) != LDAP_SUCCESS ) {
417                         ldap_perror( ld, "ldap_delete" );
418         } else if ( verbose ) {
419             printf( "\tremoved\n" );
420         }
421     }
422
423     return( rc );
424 }
425
426 /*
427  * Delete all the children of an entry recursively until leaf nodes are reached.
428  *
429  */
430 static int deletechildren( LDAP *ld,
431                            char *dn )
432 {
433     LDAPMessage *res, *e;
434     int entries;
435     int rc;
436         int timeout = 30 * 10000;
437
438     ldap_set_option( ld, LDAP_OPT_TIMEOUT, &timeout );
439     if ( verbose ) printf ( "deleting children of: %s\n", dn );
440     /*
441      * Do a one level search at dn for children.  For each, delete its children.
442      */
443     if ( ldap_search_s( ld, dn, LDAP_SCOPE_ONELEVEL, NULL, NULL, 0, &res ) == -1 )
444     {
445         ldap_perror( ld, "ldap_search" );
446                 ldap_get_option( ld, LDAP_OPT_ERROR_NUMBER, &rc );
447         return( rc );
448     }
449
450     entries = ldap_count_entries( ld, res );
451     if ( entries > 0 )
452     {
453         int i;
454
455         for (e = ldap_first_entry( ld, res ), i = 0; e != NULL;
456              e = ldap_next_entry( ld, e ), i++ )
457         {
458             if ( (rc = deletechildren( ld, ldap_get_dn( ld, e) )) == -1 )
459             {
460                 ldap_perror( ld, "ldap_prune" );
461                 return rc;
462             }
463             if ( verbose )
464             {
465                 printf( "\tremoving %s\n", ldap_get_dn( ld, e ) );
466             }
467             if ( ( rc = ldap_delete_s( ld, ldap_get_dn( ld, e ) ) ) == -1 )
468             {
469                 ldap_perror( ld, "ldap_delete" );
470                 return rc;
471             }
472             else if ( verbose )
473             {
474                 printf( "\t%s removed\n", ldap_get_dn( ld, e ) );
475             }
476         }
477     }
478     ldap_msgfree( res );
479     return rc;
480 }