]> git.sur5r.net Git - openldap/blob - clients/tools/ldapdelete.c
Add LBER_ and LDAP_ memory allocators/deallocators for internal
[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 #endif
49             break;
50         case 'K':       /* kerberos bind, part one only */
51 #ifdef HAVE_KERBEROS
52                 authmethod = LDAP_AUTH_KRBV41;
53 #else
54                 fprintf (stderr, "%s was not compiled with Kerberos support\n", argv[0]);
55 #endif
56             break;
57         case 'c':       /* continuous operation mode */
58             ++contoper;
59             break;
60         case 'h':       /* ldap host */
61             ldaphost = strdup( optarg );
62             break;
63         case 'D':       /* bind DN */
64             binddn = strdup( optarg );
65             break;
66         case 'w':       /* password */
67             passwd = strdup( optarg );
68             break;
69         case 'f':       /* read DNs from a file */
70             if (( fp = fopen( optarg, "r" )) == NULL ) {
71                 perror( optarg );
72                 exit( 1 );
73             }
74             break;
75         case 'd':
76             debug |= atoi( optarg );
77             break;
78         case 'p':
79             ldapport = atoi( optarg );
80             break;
81         case 'n':       /* print deletes, don't actually do them */
82             ++not;
83             break;
84         case 'v':       /* verbose mode */
85             verbose++;
86             break;
87         case 'W':
88                 want_bindpw++;
89                 break;
90         case 'P':
91                 switch(optarg[0])
92                 {
93                 case '2':
94                         version = LDAP_VERSION2;
95                         break;
96                 case '3':
97                         version = LDAP_VERSION3;
98                         break;
99                 }
100                 break;
101         default:
102             fprintf( stderr, usage, argv[0] );
103             exit( 1 );
104         }
105     }
106
107     if ( fp == NULL ) {
108         if ( optind >= argc ) {
109             fp = stdin;
110         }
111     }
112
113         if ( debug ) {
114                 ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug );
115                 ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug );
116         }
117
118 #ifdef SIGPIPE
119         (void) SIGNAL( SIGPIPE, SIG_IGN );
120 #endif
121
122     if (( ld = ldap_init( ldaphost, ldapport )) == NULL ) {
123         perror( "ldap_init" );
124         exit( 1 );
125     }
126
127         {
128                 /* this seems prudent */
129                 int deref = LDAP_DEREF_NEVER;
130                 ldap_set_option( ld, LDAP_OPT_DEREF, &deref );
131         }
132
133         if (want_bindpw)
134                 passwd = getpass("Enter LDAP Password: ");
135
136         if( version != -1 ) {
137                 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version );
138         }
139
140     if ( ldap_bind_s( ld, binddn, passwd, authmethod ) != LDAP_SUCCESS ) {
141         ldap_perror( ld, "ldap_bind" );
142         exit( 1 );
143     }
144
145     if ( fp == NULL ) {
146         for ( ; optind < argc; ++optind ) {
147             rc = dodelete( ld, argv[ optind ] );
148         }
149     } else {
150         rc = 0;
151         while ((rc == 0 || contoper) && fgets(buf, sizeof(buf), fp) != NULL) {
152             buf[ strlen( buf ) - 1 ] = '\0';    /* remove trailing newline */
153             if ( *buf != '\0' ) {
154                 rc = dodelete( ld, buf );
155             }
156         }
157     }
158
159     ldap_unbind( ld );
160
161     exit( rc );
162
163         /* UNREACHABLE */
164         return(0);
165 }
166
167
168 static int dodelete(
169     LDAP        *ld,
170     char        *dn)
171 {
172     int rc;
173
174     if ( verbose ) {
175         printf( "%sdeleting entry \"%s\"\n",
176                 (not ? "!" : ""), dn );
177     }
178     if ( not ) {
179         rc = LDAP_SUCCESS;
180     } else {
181         if (( rc = ldap_delete_s( ld, dn )) != LDAP_SUCCESS ) {
182             ldap_perror( ld, "ldap_delete" );
183         } else if ( verbose ) {
184             printf( "\tremoved\n" );
185         }
186     }
187
188     return( rc );
189 }