]> git.sur5r.net Git - openldap/blob - clients/tools/ldapdelete.c
72d550df2ff628d7d5b8e91836f2eda2ee340c9a
[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] [-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;
36
37     not = verbose = contoper = want_bindpw = debug = 0;
38     fp = NULL;
39     authmethod = LDAP_AUTH_SIMPLE;
40         version = -1;
41
42     while (( i = getopt( argc, argv, "WnvkKch: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( 1 );
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 'W':
100                 want_bindpw++;
101                 break;
102         case 'P':
103                 switch( atoi(optarg) )
104                 {
105                 case 2:
106                         version = LDAP_VERSION2;
107                         break;
108                 case 3:
109                         version = LDAP_VERSION3;
110                         break;
111                 default:
112                         fprintf( stderr, "protocol version should be 2 or 3\n" );
113                     fprintf( stderr, usage, argv[0] );
114                     return( EXIT_FAILURE );
115                 }
116                 break;
117         default:
118             fprintf( stderr, usage, argv[0] );
119             return( EXIT_FAILURE );
120         }
121     }
122
123     if ( fp == NULL ) {
124         if ( optind >= argc ) {
125             fp = stdin;
126         }
127     }
128
129         if ( debug ) {
130                 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
131                         fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
132                 }
133                 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
134                         fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
135                 }
136         }
137
138 #ifdef SIGPIPE
139         (void) SIGNAL( SIGPIPE, SIG_IGN );
140 #endif
141
142     if (( ld = ldap_init( ldaphost, ldapport )) == NULL ) {
143         perror( "ldap_init" );
144         return( EXIT_FAILURE );
145     }
146
147         {
148                 /* this seems prudent */
149                 int deref = LDAP_DEREF_NEVER;
150                 ldap_set_option( ld, LDAP_OPT_DEREF, &deref );
151         }
152
153         if (want_bindpw)
154                 passwd = getpass("Enter LDAP Password: ");
155
156         if (version != -1 &&
157                 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version ) != LDAP_OPT_SUCCESS)
158         {
159                 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n", version );
160         }
161
162     if ( ldap_bind_s( ld, binddn, passwd, authmethod ) != LDAP_SUCCESS ) {
163         ldap_perror( ld, "ldap_bind" );
164         return( EXIT_FAILURE );
165     }
166
167     if ( fp == NULL ) {
168         for ( ; optind < argc; ++optind ) {
169             rc = dodelete( ld, argv[ optind ] );
170         }
171     } else {
172         rc = 0;
173         while ((rc == 0 || contoper) && fgets(buf, sizeof(buf), fp) != NULL) {
174             buf[ strlen( buf ) - 1 ] = '\0';    /* remove trailing newline */
175             if ( *buf != '\0' ) {
176                 rc = dodelete( ld, buf );
177             }
178         }
179     }
180
181     ldap_unbind( ld );
182
183         return( rc );
184 }
185
186
187 static int dodelete(
188     LDAP        *ld,
189     char        *dn)
190 {
191     int rc;
192
193     if ( verbose ) {
194         printf( "%sdeleting entry \"%s\"\n",
195                 (not ? "!" : ""), dn );
196     }
197     if ( not ) {
198         rc = LDAP_SUCCESS;
199     } else {
200         if (( rc = ldap_delete_s( ld, dn )) != LDAP_SUCCESS ) {
201             ldap_perror( ld, "ldap_delete" );
202         } else if ( verbose ) {
203             printf( "\tremoved\n" );
204         }
205     }
206
207     return( rc );
208 }