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