]> git.sur5r.net Git - openldap/blob - clients/tools/ldapdelete.c
Fix target directories.
[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             break;
74         case 'f':       /* read DNs from a file */
75             if (( fp = fopen( optarg, "r" )) == NULL ) {
76                 perror( optarg );
77                 exit( 1 );
78             }
79             break;
80         case 'd':
81             debug |= atoi( optarg );
82             break;
83         case 'p':
84             ldapport = atoi( optarg );
85             break;
86         case 'n':       /* print deletes, don't actually do them */
87             ++not;
88             break;
89         case 'v':       /* verbose mode */
90             verbose++;
91             break;
92         case 'W':
93                 want_bindpw++;
94                 break;
95         case 'P':
96                 switch( atoi(optarg) )
97                 {
98                 case 2:
99                         version = LDAP_VERSION2;
100                         break;
101                 case 3:
102                         version = LDAP_VERSION3;
103                         break;
104                 default:
105                         fprintf( stderr, "protocol version should be 2 or 3\n" );
106                     fprintf( stderr, usage, argv[0] );
107                     return( EXIT_FAILURE );
108                 }
109                 break;
110         default:
111             fprintf( stderr, usage, argv[0] );
112             return( EXIT_FAILURE );
113         }
114     }
115
116     if ( fp == NULL ) {
117         if ( optind >= argc ) {
118             fp = stdin;
119         }
120     }
121
122         if ( debug ) {
123                 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_ERROR ) {
124                         fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
125                 }
126                 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_ERROR ) {
127                         fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
128                 }
129         }
130
131 #ifdef SIGPIPE
132         (void) SIGNAL( SIGPIPE, SIG_IGN );
133 #endif
134
135     if (( ld = ldap_init( ldaphost, ldapport )) == NULL ) {
136         perror( "ldap_init" );
137         return( EXIT_FAILURE );
138     }
139
140         {
141                 /* this seems prudent */
142                 int deref = LDAP_DEREF_NEVER;
143                 ldap_set_option( ld, LDAP_OPT_DEREF, &deref );
144         }
145
146         if (want_bindpw)
147                 passwd = getpass("Enter LDAP Password: ");
148
149         if (version != -1 &&
150                 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version ) == LDAP_OPT_ERROR)
151         {
152                 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n", version );
153         }
154
155     if ( ldap_bind_s( ld, binddn, passwd, authmethod ) != LDAP_SUCCESS ) {
156         ldap_perror( ld, "ldap_bind" );
157         return( EXIT_FAILURE );
158     }
159
160     if ( fp == NULL ) {
161         for ( ; optind < argc; ++optind ) {
162             rc = dodelete( ld, argv[ optind ] );
163         }
164     } else {
165         rc = 0;
166         while ((rc == 0 || contoper) && fgets(buf, sizeof(buf), fp) != NULL) {
167             buf[ strlen( buf ) - 1 ] = '\0';    /* remove trailing newline */
168             if ( *buf != '\0' ) {
169                 rc = dodelete( ld, buf );
170             }
171         }
172     }
173
174     ldap_unbind( ld );
175
176         return( rc );
177 }
178
179
180 static int dodelete(
181     LDAP        *ld,
182     char        *dn)
183 {
184     int rc;
185
186     if ( verbose ) {
187         printf( "%sdeleting entry \"%s\"\n",
188                 (not ? "!" : ""), dn );
189     }
190     if ( not ) {
191         rc = LDAP_SUCCESS;
192     } else {
193         if (( rc = ldap_delete_s( ld, dn )) != LDAP_SUCCESS ) {
194             ldap_perror( ld, "ldap_delete" );
195         } else if ( verbose ) {
196             printf( "\tremoved\n" );
197         }
198     }
199
200     return( rc );
201 }