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