]> git.sur5r.net Git - openldap/blob - clients/tools/ldapdelete.c
Protoized, moved extern definitions to .h files, fixed related bugs.
[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 extern char *strdup (const char *);
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 #define safe_realloc( ptr, size )       ( ptr == NULL ? malloc( size ) : \
25                                          realloc( ptr, size ))
26
27 static int dodelete LDAP_P((
28     LDAP        *ld,
29     char        *dn));
30
31 int
32 main( int argc, char **argv )
33 {
34     char                *usage = "usage: %s [-n] [-v] [-k] [-d debug-level] [-f file] [-h ldaphost] [-p ldapport] [-D binddn] [-w passwd] [dn]...\n";
35     char                buf[ 4096 ];
36     FILE                *fp;
37     int                 i, rc, kerberos, authmethod;
38
39     extern char *optarg;
40     extern int  optind;
41
42     kerberos = not = verbose = contoper = 0;
43     fp = NULL;
44
45     while (( i = getopt( argc, argv, "nvkKch:p:D:w:d:f:" )) != EOF ) {
46         switch( i ) {
47         case 'k':       /* kerberos bind */
48             kerberos = 2;
49             break;
50         case 'K':       /* kerberos bind, part one only */
51             kerberos = 1;
52             break;
53         case 'c':       /* continuous operation mode */
54             ++contoper;
55             break;
56         case 'h':       /* ldap host */
57             ldaphost = strdup( optarg );
58             break;
59         case 'D':       /* bind DN */
60             binddn = strdup( optarg );
61             break;
62         case 'w':       /* password */
63             passwd = strdup( optarg );
64             break;
65         case 'f':       /* read DNs from a file */
66             if (( fp = fopen( optarg, "r" )) == NULL ) {
67                 perror( optarg );
68                 exit( 1 );
69             }
70             break;
71         case 'd':
72 #ifdef LDAP_DEBUG
73             ldap_debug = lber_debug = atoi( optarg );   /* */
74 #else /* LDAP_DEBUG */
75             fprintf( stderr, "compile with -DLDAP_DEBUG for debugging\n" );
76 #endif /* LDAP_DEBUG */
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         default:
88             fprintf( stderr, usage, argv[0] );
89             exit( 1 );
90         }
91     }
92
93     if ( fp == NULL ) {
94         if ( optind >= argc ) {
95             fp = stdin;
96         }
97     }
98
99     if (( ld = ldap_open( ldaphost, ldapport )) == NULL ) {
100         perror( "ldap_open" );
101         exit( 1 );
102     }
103
104         {
105                 /* this seems prudent */
106                 int deref = LDAP_DEREF_NEVER;
107                 ldap_set_option( ld, LDAP_OPT_DEREF, &deref );
108         }
109
110     if ( !kerberos ) {
111         authmethod = LDAP_AUTH_SIMPLE;
112     } else if ( kerberos == 1 ) {
113         authmethod = LDAP_AUTH_KRBV41;
114     } else {
115         authmethod = LDAP_AUTH_KRBV4;
116     }
117     if ( ldap_bind_s( ld, binddn, passwd, authmethod ) != LDAP_SUCCESS ) {
118         ldap_perror( ld, "ldap_bind" );
119         exit( 1 );
120     }
121
122     if ( fp == NULL ) {
123         for ( ; optind < argc; ++optind ) {
124             rc = dodelete( ld, argv[ optind ] );
125         }
126     } else {
127         rc = 0;
128         while ((rc == 0 || contoper) && fgets(buf, sizeof(buf), fp) != NULL) {
129             buf[ strlen( buf ) - 1 ] = '\0';    /* remove trailing newline */
130             if ( *buf != '\0' ) {
131                 rc = dodelete( ld, buf );
132             }
133         }
134     }
135
136     ldap_unbind( ld );
137
138     exit( rc );
139
140         /* UNREACHABLE */
141         return(0);
142 }
143
144
145 static int dodelete(
146     LDAP        *ld,
147     char        *dn)
148 {
149     int rc;
150
151     if ( verbose ) {
152         printf( "%sdeleting entry %s\n", not ? "!" : "", dn );
153     }
154     if ( not ) {
155         rc = LDAP_SUCCESS;
156     } else {
157         if (( rc = ldap_delete_s( ld, dn )) != LDAP_SUCCESS ) {
158             ldap_perror( ld, "ldap_delete" );
159         } else if ( verbose ) {
160             printf( "entry removed\n" );
161         }
162     }
163
164     return( rc );
165 }