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