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