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