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