]> git.sur5r.net Git - openldap/blob - clients/tools/ldapdelete.c
c69bf02d86f79dd9bc07d85c22611ea9c4857cc6
[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         /* don't chase referrals */
161         ldap_set_option( ld, LDAP_OPT_REFERRALS, LDAP_OPT_OFF );
162
163         if (want_bindpw)
164                 passwd = getpass("Enter LDAP Password: ");
165
166         if (version != -1 &&
167                 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version ) != LDAP_OPT_SUCCESS)
168         {
169                 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n", version );
170         }
171
172     if ( ldap_bind_s( ld, binddn, passwd, authmethod ) != LDAP_SUCCESS ) {
173         ldap_perror( ld, "ldap_bind" );
174         return( EXIT_FAILURE );
175     }
176
177         if ( manageDSAit ) {
178                 int err;
179                 LDAPControl c;
180                 LDAPControl *ctrls[2];
181                 ctrls[0] = &c;
182                 ctrls[1] = NULL;
183
184                 c.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
185                 c.ldctl_value.bv_val = NULL;
186                 c.ldctl_value.bv_len = 0;
187                 c.ldctl_iscritical = manageDSAit > 1;
188
189                 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, &ctrls );
190
191                 if( err != LDAP_OPT_SUCCESS ) {
192                         fprintf( stderr, "Could not set Manage DSA IT Control\n" );
193                         if( c.ldctl_iscritical ) {
194                                 exit( EXIT_FAILURE );
195                         }
196                 }
197         }
198
199         rc = 0;
200     if ( fp == NULL ) {
201         for ( ; optind < argc; ++optind ) {
202             rc = dodelete( ld, argv[ optind ] );
203         }
204     } else {
205         while ((rc == 0 || contoper) && fgets(buf, sizeof(buf), fp) != NULL) {
206             buf[ strlen( buf ) - 1 ] = '\0';    /* remove trailing newline */
207             if ( *buf != '\0' ) {
208                 rc = dodelete( ld, buf );
209             }
210         }
211     }
212
213     ldap_unbind( ld );
214
215         return( rc );
216 }
217
218
219 static int dodelete(
220     LDAP        *ld,
221     char        *dn)
222 {
223     int rc;
224
225     if ( verbose ) {
226         printf( "%sdeleting entry \"%s\"\n",
227                 (not ? "!" : ""), dn );
228     }
229     if ( not ) {
230         rc = LDAP_SUCCESS;
231     } else {
232         if (( rc = ldap_delete_s( ld, dn )) != LDAP_SUCCESS ) {
233             ldap_perror( ld, "ldap_delete" );
234         } else if ( verbose ) {
235             printf( "\tremoved\n" );
236         }
237     }
238
239     return( rc );
240 }