]> git.sur5r.net Git - openldap/blob - clients/tools/ldapdelete.c
Fix getpassword bug
[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_s( 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 = getpassphrase("Enter LDAP Password: ");
322                 passwd.bv_len = strlen( passwd.bv_val );
323         }
324
325         if ( authmethod == LDAP_AUTH_SASL ) {
326 #ifdef HAVE_CYRUS_SASL
327                 int     minssf = 0, maxssf = 0;
328
329                 if ( sasl_integrity > 0 )
330                         maxssf = 1;
331                 if ( sasl_integrity > 1 )
332                         minssf = 1;
333                 if ( sasl_privacy > 0 )
334                         maxssf = 100000; /* Something big value */
335                 if ( sasl_privacy > 1 )
336                         minssf = 56;
337                 
338                 if ( ldap_set_option( ld, LDAP_OPT_X_SASL_MINSSF,
339                                 (void *)&minssf ) != LDAP_OPT_SUCCESS ) {
340                         fprintf( stderr, "Could not set LDAP_OPT_X_SASL_MINSSF"
341                                 "%d\n", minssf);
342                         return( EXIT_FAILURE );
343                 }
344                 if ( ldap_set_option( ld, LDAP_OPT_X_SASL_MAXSSF,
345                                 (void *)&maxssf ) != LDAP_OPT_SUCCESS ) {
346                         fprintf( stderr, "Could not set LDAP_OPT_X_SASL_MAXSSF"
347                                 "%d\n", maxssf);
348                         return( EXIT_FAILURE );
349                 }
350                 
351                 rc = ldap_negotiated_sasl_bind_s( ld, binddn, sasl_authc_id,
352                                 sasl_authz_id, sasl_mech,
353                                 passwd.bv_len ? &passwd : NULL,
354                                 NULL, NULL );
355
356                 if( rc != LDAP_SUCCESS ) {
357                         ldap_perror( ld, "ldap_negotiated_sasl_bind_s" );
358                         return( EXIT_FAILURE );
359                 }
360 #else
361                 fprintf( stderr, "%s was not compiled with SASL support\n",
362                         argv[0] );
363                 return( EXIT_FAILURE );
364 #endif
365         }
366         else {
367                 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
368                                 != LDAP_SUCCESS ) {
369                         ldap_perror( ld, "ldap_bind" );
370                         return( EXIT_FAILURE );
371                 }
372         }
373
374         if ( manageDSAit ) {
375                 int err;
376                 LDAPControl c;
377                 LDAPControl *ctrls[2];
378                 ctrls[0] = &c;
379                 ctrls[1] = NULL;
380
381                 c.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
382                 c.ldctl_value.bv_val = NULL;
383                 c.ldctl_value.bv_len = 0;
384                 c.ldctl_iscritical = manageDSAit > 1;
385
386                 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, &ctrls );
387
388                 if( err != LDAP_OPT_SUCCESS ) {
389                         fprintf( stderr, "Could not set Manage DSA IT Control\n" );
390                         if( c.ldctl_iscritical ) {
391                                 exit( EXIT_FAILURE );
392                         }
393                 }
394         }
395
396         rc = 0;
397     if ( fp == NULL ) {
398         for ( ; optind < argc; ++optind ) {
399             rc = dodelete( ld, argv[ optind ] );
400         }
401     } else {
402         while ((rc == 0 || contoper) && fgets(buf, sizeof(buf), fp) != NULL) {
403             buf[ strlen( buf ) - 1 ] = '\0';    /* remove trailing newline */
404             if ( *buf != '\0' ) {
405                 rc = dodelete( ld, buf );
406             }
407         }
408     }
409
410     ldap_unbind( ld );
411
412         return( rc );
413 }
414
415
416 static int dodelete(
417     LDAP        *ld,
418     char        *dn)
419 {
420     int rc;
421
422     if ( verbose ) {
423         printf( "%sdeleting entry \"%s\"\n",
424                 (not ? "!" : ""), dn );
425     }
426     if ( not ) {
427         rc = LDAP_SUCCESS;
428     } else {
429                 /* If prune is on, remove a whole subtree.  Delete the children of the
430                  * DN recursively, then the DN requested.
431                  */
432                 if ( prune ) deletechildren( ld, dn );
433                 if (( rc = ldap_delete_s( ld, dn )) != LDAP_SUCCESS ) {
434                         ldap_perror( ld, "ldap_delete" );
435         } else if ( verbose ) {
436             printf( "\tremoved\n" );
437         }
438     }
439
440     return( rc );
441 }
442
443 /*
444  * Delete all the children of an entry recursively until leaf nodes are reached.
445  *
446  */
447 static int deletechildren( LDAP *ld,
448                            char *dn )
449 {
450     LDAPMessage *res, *e;
451     int entries;
452     int rc;
453         int timeout = 30 * 10000;
454
455     ldap_set_option( ld, LDAP_OPT_TIMEOUT, &timeout );
456     if ( verbose ) printf ( "deleting children of: %s\n", dn );
457     /*
458      * Do a one level search at dn for children.  For each, delete its children.
459      */
460     if ( ldap_search_s( ld, dn, LDAP_SCOPE_ONELEVEL, NULL, NULL, 0, &res ) == -1 )
461     {
462         ldap_perror( ld, "ldap_search" );
463                 ldap_get_option( ld, LDAP_OPT_ERROR_NUMBER, &rc );
464         return( rc );
465     }
466
467     entries = ldap_count_entries( ld, res );
468     if ( entries > 0 )
469     {
470         int i;
471
472         for (e = ldap_first_entry( ld, res ), i = 0; e != NULL;
473              e = ldap_next_entry( ld, e ), i++ )
474         {
475             if ( (rc = deletechildren( ld, ldap_get_dn( ld, e) )) == -1 )
476             {
477                 ldap_perror( ld, "ldap_prune" );
478                 return rc;
479             }
480             if ( verbose )
481             {
482                 printf( "\tremoving %s\n", ldap_get_dn( ld, e ) );
483             }
484             if ( ( rc = ldap_delete_s( ld, ldap_get_dn( ld, e ) ) ) == -1 )
485             {
486                 ldap_perror( ld, "ldap_delete" );
487                 return rc;
488             }
489             else if ( verbose )
490             {
491                 printf( "\t%s removed\n", ldap_get_dn( ld, e ) );
492             }
493         }
494     }
495     ldap_msgfree( res );
496     return rc;
497 }