]> git.sur5r.net Git - openldap/blob - clients/tools/ldapdelete.c
merged with autoconf branch
[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/socket.h>
10 #include <ac/string.h>
11
12 #include <lber.h>
13 #include <ldap.h>
14
15 #include "ldapconfig.h"
16
17 static char     *binddn = LDAPDELETE_BINDDN;
18 static char     *passwd = LDAPDELETE_BIND_CRED;
19 static char     *base = LDAPDELETE_BASE;
20 static char     *ldaphost = LDAPHOST;
21 static int      ldapport = LDAP_PORT;
22 static int      not, verbose, contoper;
23 static LDAP     *ld;
24
25 #ifdef LDAP_DEBUG
26 extern int ldap_debug, lber_debug;
27 #endif /* LDAP_DEBUG */
28
29 #define safe_realloc( ptr, size )       ( ptr == NULL ? malloc( size ) : \
30                                          realloc( ptr, size ))
31
32 static int dodelete LDAP_P((
33     LDAP        *ld,
34     char        *dn));
35
36 main( argc, argv )
37     int         argc;
38     char        **argv;
39 {
40     char                *usage = "usage: %s [-n] [-v] [-k] [-d debug-level] [-f file] [-h ldaphost] [-p ldapport] [-D binddn] [-w passwd] [dn]...\n";
41     char                buf[ 4096 ];
42     FILE                *fp;
43     int                 i, rc, kerberos, authmethod;
44
45     extern char *optarg;
46     extern int  optind;
47
48     kerberos = not = verbose = contoper = 0;
49     fp = NULL;
50
51     while (( i = getopt( argc, argv, "nvkKch:p:D:w:d:f:" )) != EOF ) {
52         switch( i ) {
53         case 'k':       /* kerberos bind */
54             kerberos = 2;
55             break;
56         case 'K':       /* kerberos bind, part one only */
57             kerberos = 1;
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 #ifdef LDAP_DEBUG
79             ldap_debug = lber_debug = atoi( optarg );   /* */
80 #else /* LDAP_DEBUG */
81             fprintf( stderr, "compile with -DLDAP_DEBUG for debugging\n" );
82 #endif /* LDAP_DEBUG */
83             break;
84         case 'p':
85             ldapport = atoi( optarg );
86             break;
87         case 'n':       /* print deletes, don't actually do them */
88             ++not;
89             break;
90         case 'v':       /* verbose mode */
91             verbose++;
92             break;
93         default:
94             fprintf( stderr, usage, argv[0] );
95             exit( 1 );
96         }
97     }
98
99     if ( fp == NULL ) {
100         if ( optind >= argc ) {
101             fp = stdin;
102         }
103     }
104
105     if (( ld = ldap_open( ldaphost, ldapport )) == NULL ) {
106         perror( "ldap_open" );
107         exit( 1 );
108     }
109
110     ld->ld_deref = LDAP_DEREF_NEVER;    /* prudent, but probably unnecessary */
111
112     if ( !kerberos ) {
113         authmethod = LDAP_AUTH_SIMPLE;
114     } else if ( kerberos == 1 ) {
115         authmethod = LDAP_AUTH_KRBV41;
116     } else {
117         authmethod = LDAP_AUTH_KRBV4;
118     }
119     if ( ldap_bind_s( ld, binddn, passwd, authmethod ) != LDAP_SUCCESS ) {
120         ldap_perror( ld, "ldap_bind" );
121         exit( 1 );
122     }
123
124     if ( fp == NULL ) {
125         for ( ; optind < argc; ++optind ) {
126             rc = dodelete( ld, argv[ optind ] );
127         }
128     } else {
129         rc = 0;
130         while ((rc == 0 || contoper) && fgets(buf, sizeof(buf), fp) != NULL) {
131             buf[ strlen( buf ) - 1 ] = '\0';    /* remove trailing newline */
132             if ( *buf != '\0' ) {
133                 rc = dodelete( ld, buf );
134             }
135         }
136     }
137
138     ldap_unbind( ld );
139
140     exit( rc );
141 }
142
143
144 static int dodelete(
145     LDAP        *ld,
146     char        *dn)
147 {
148     int rc;
149
150     if ( verbose ) {
151         printf( "%sdeleting entry %s\n", not ? "!" : "", dn );
152     }
153     if ( not ) {
154         rc = LDAP_SUCCESS;
155     } else {
156         if (( rc = ldap_delete_s( ld, dn )) != LDAP_SUCCESS ) {
157             ldap_perror( ld, "ldap_delete" );
158         } else if ( verbose ) {
159             printf( "entry removed\n" );
160         }
161     }
162
163     return( rc );
164 }