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