]> git.sur5r.net Git - openldap/blob - clients/tools/ldapdelete.c
694d7f8b613dab2180f1317c40b0bbdee8822757
[openldap] / clients / tools / ldapdelete.c
1 /* ldapdelete.c - simple program to delete an entry using LDAP */
2 /*
3  * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6
7 #include "portable.h"
8
9 #include <stdio.h>
10
11 #include <ac/stdlib.h>
12 #include <ac/ctype.h>
13
14 #include <ac/signal.h>
15 #include <ac/string.h>
16 #include <ac/unistd.h>
17
18 #include <lber.h>
19 #include <ldap.h>
20
21 static char     *binddn = NULL;
22 static char     *passwd = NULL;
23 static char     *ldaphost = NULL;
24 static int      ldapport = 0;
25 static int      not, verbose, contoper;
26 static LDAP     *ld;
27
28 static int dodelete LDAP_P((
29     LDAP        *ld,
30     char        *dn));
31
32 int
33 main( int argc, char **argv )
34 {
35         char            *usage = "usage: %s [-n] [-v] [-k] [-W] [-M[M]] [-d debug-level] [-f file] [-h ldaphost] [-P version] [-p ldapport] [-D binddn] [-w passwd] [dn]...\n";
36     char                buf[ 4096 ];
37     FILE                *fp;
38         int             i, rc, authmethod, want_bindpw, version, debug, manageDSAit;
39
40     not = verbose = contoper = want_bindpw = debug = manageDSAit = 0;
41     fp = NULL;
42     authmethod = LDAP_AUTH_SIMPLE;
43         version = -1;
44
45     while (( i = getopt( argc, argv, "WMnvkKch:P:p:D:w:d:f:" )) != EOF ) {
46         switch( i ) {
47         case 'k':       /* kerberos bind */
48 #ifdef HAVE_KERBEROS
49                 authmethod = LDAP_AUTH_KRBV4;
50 #else
51                 fprintf (stderr, "%s was not compiled with Kerberos support\n", argv[0]);
52                 fprintf( stderr, usage, argv[0] );
53                 return( EXIT_FAILURE );
54 #endif
55             break;
56         case 'K':       /* kerberos bind, part one only */
57 #ifdef HAVE_KERBEROS
58                 authmethod = LDAP_AUTH_KRBV41;
59 #else
60                 fprintf (stderr, "%s was not compiled with Kerberos support\n", argv[0]);
61                 fprintf( stderr, usage, argv[0] );
62                 return( EXIT_FAILURE );
63 #endif
64             break;
65         case 'c':       /* continuous operation mode */
66             ++contoper;
67             break;
68         case 'h':       /* ldap host */
69             ldaphost = strdup( optarg );
70             break;
71         case 'D':       /* bind DN */
72             binddn = strdup( optarg );
73             break;
74         case 'w':       /* password */
75             passwd = strdup( optarg );
76                 {
77                         char* p;
78
79                         for( p = optarg; *p == '\0'; p++ ) {
80                                 *p = '*';
81                         }
82                 }
83             break;
84         case 'f':       /* read DNs from a file */
85             if (( fp = fopen( optarg, "r" )) == NULL ) {
86                 perror( optarg );
87                 exit( EXIT_FAILURE );
88             }
89             break;
90         case 'd':
91             debug |= atoi( optarg );
92             break;
93         case 'p':
94             ldapport = atoi( optarg );
95             break;
96         case 'n':       /* print deletes, don't actually do them */
97             ++not;
98             break;
99         case 'v':       /* verbose mode */
100             verbose++;
101             break;
102         case 'M':
103                 /* enable Manage DSA IT */
104                 manageDSAit++;
105                 break;
106         case 'W':
107                 want_bindpw++;
108                 break;
109         case 'P':
110                 switch( atoi(optarg) )
111                 {
112                 case 2:
113                         version = LDAP_VERSION2;
114                         break;
115                 case 3:
116                         version = LDAP_VERSION3;
117                         break;
118                 default:
119                         fprintf( stderr, "protocol version should be 2 or 3\n" );
120                     fprintf( stderr, usage, argv[0] );
121                     return( EXIT_FAILURE );
122                 }
123                 break;
124         default:
125             fprintf( stderr, usage, argv[0] );
126             return( EXIT_FAILURE );
127         }
128     }
129
130     if ( fp == NULL ) {
131         if ( optind >= argc ) {
132             fp = stdin;
133         }
134     }
135
136         if ( debug ) {
137                 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
138                         fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
139                 }
140                 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
141                         fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
142                 }
143         }
144
145 #ifdef SIGPIPE
146         (void) SIGNAL( SIGPIPE, SIG_IGN );
147 #endif
148
149     if (( ld = ldap_init( ldaphost, ldapport )) == NULL ) {
150         perror( "ldap_init" );
151         return( EXIT_FAILURE );
152     }
153
154         {
155                 /* this seems prudent */
156                 int deref = LDAP_DEREF_NEVER;
157                 ldap_set_option( ld, LDAP_OPT_DEREF, &deref );
158         }
159
160         if (want_bindpw)
161                 passwd = getpass("Enter LDAP Password: ");
162
163         if (version != -1 &&
164                 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version ) != LDAP_OPT_SUCCESS)
165         {
166                 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n", version );
167         }
168
169     if ( ldap_bind_s( ld, binddn, passwd, authmethod ) != LDAP_SUCCESS ) {
170         ldap_perror( ld, "ldap_bind" );
171         return( EXIT_FAILURE );
172     }
173
174         if ( manageDSAit ) {
175                 int err;
176                 LDAPControl c;
177                 LDAPControl *ctrls[2];
178                 ctrls[0] = &c;
179                 ctrls[1] = NULL;
180
181                 c.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
182                 c.ldctl_value.bv_val = NULL;
183                 c.ldctl_value.bv_len = 0;
184                 c.ldctl_iscritical = manageDSAit > 1;
185
186                 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, &ctrls );
187
188                 if( err != LDAP_OPT_SUCCESS ) {
189                         fprintf( stderr, "Could not set Manage DSA IT Control\n" );
190                         if( c.ldctl_iscritical ) {
191                                 exit( EXIT_FAILURE );
192                         }
193                 }
194         }
195
196         rc = 0;
197     if ( fp == NULL ) {
198         for ( ; optind < argc; ++optind ) {
199             rc = dodelete( ld, argv[ optind ] );
200         }
201     } else {
202         while ((rc == 0 || contoper) && fgets(buf, sizeof(buf), fp) != NULL) {
203             buf[ strlen( buf ) - 1 ] = '\0';    /* remove trailing newline */
204             if ( *buf != '\0' ) {
205                 rc = dodelete( ld, buf );
206             }
207         }
208     }
209
210     ldap_unbind( ld );
211
212         return( rc );
213 }
214
215
216 static int dodelete(
217     LDAP        *ld,
218     char        *dn)
219 {
220     int rc;
221
222     if ( verbose ) {
223         printf( "%sdeleting entry \"%s\"\n",
224                 (not ? "!" : ""), dn );
225     }
226     if ( not ) {
227         rc = LDAP_SUCCESS;
228     } else {
229         if (( rc = ldap_delete_s( ld, dn )) != LDAP_SUCCESS ) {
230             ldap_perror( ld, "ldap_delete" );
231         } else if ( verbose ) {
232             printf( "\tremoved\n" );
233         }
234     }
235
236     return( rc );
237 }