]> git.sur5r.net Git - openldap/blob - clients/tools/ldapdelete.c
49c1763e12190dabb237f2da83c834922ba4622f
[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 #include <ac/time.h>
12
13 #include <lber.h>
14 #include <ldap.h>
15
16 #include "ldapconfig.h"
17
18 static char     *binddn = LDAPDELETE_BINDDN;
19 static char     *passwd = LDAPDELETE_BIND_CRED;
20 static char     *base = LDAPDELETE_BASE;
21 static char     *ldaphost = LDAPHOST;
22 static int      ldapport = LDAP_PORT;
23 static int      not, verbose, contoper;
24 static LDAP     *ld;
25
26 #ifdef LDAP_DEBUG
27 extern int ldap_debug, lber_debug;
28 #endif /* LDAP_DEBUG */
29
30 #define safe_realloc( ptr, size )       ( ptr == NULL ? malloc( size ) : \
31                                          realloc( ptr, size ))
32
33 static int dodelete LDAP_P((
34     LDAP        *ld,
35     char        *dn));
36
37 main( argc, argv )
38     int         argc;
39     char        **argv;
40 {
41     char                *usage = "usage: %s [-n] [-v] [-k] [-d debug-level] [-f file] [-h ldaphost] [-p ldapport] [-D binddn] [-w passwd] [dn]...\n";
42     char                buf[ 4096 ];
43     FILE                *fp;
44     int                 i, rc, kerberos, authmethod;
45
46     extern char *optarg;
47     extern int  optind;
48
49     kerberos = not = verbose = contoper = 0;
50     fp = NULL;
51
52     while (( i = getopt( argc, argv, "nvkKch:p:D:w:d:f:" )) != EOF ) {
53         switch( i ) {
54         case 'k':       /* kerberos bind */
55             kerberos = 2;
56             break;
57         case 'K':       /* kerberos bind, part one only */
58             kerberos = 1;
59             break;
60         case 'c':       /* continuous operation mode */
61             ++contoper;
62             break;
63         case 'h':       /* ldap host */
64             ldaphost = strdup( optarg );
65             break;
66         case 'D':       /* bind DN */
67             binddn = strdup( optarg );
68             break;
69         case 'w':       /* password */
70             passwd = strdup( optarg );
71             break;
72         case 'f':       /* read DNs from a file */
73             if (( fp = fopen( optarg, "r" )) == NULL ) {
74                 perror( optarg );
75                 exit( 1 );
76             }
77             break;
78         case 'd':
79 #ifdef LDAP_DEBUG
80             ldap_debug = lber_debug = atoi( optarg );   /* */
81 #else /* LDAP_DEBUG */
82             fprintf( stderr, "compile with -DLDAP_DEBUG for debugging\n" );
83 #endif /* LDAP_DEBUG */
84             break;
85         case 'p':
86             ldapport = atoi( optarg );
87             break;
88         case 'n':       /* print deletes, don't actually do them */
89             ++not;
90             break;
91         case 'v':       /* verbose mode */
92             verbose++;
93             break;
94         default:
95             fprintf( stderr, usage, argv[0] );
96             exit( 1 );
97         }
98     }
99
100     if ( fp == NULL ) {
101         if ( optind >= argc ) {
102             fp = stdin;
103         }
104     }
105
106     if (( ld = ldap_open( ldaphost, ldapport )) == NULL ) {
107         perror( "ldap_open" );
108         exit( 1 );
109     }
110
111     ld->ld_deref = LDAP_DEREF_NEVER;    /* prudent, but probably unnecessary */
112
113     if ( !kerberos ) {
114         authmethod = LDAP_AUTH_SIMPLE;
115     } else if ( kerberos == 1 ) {
116         authmethod = LDAP_AUTH_KRBV41;
117     } else {
118         authmethod = LDAP_AUTH_KRBV4;
119     }
120     if ( ldap_bind_s( ld, binddn, passwd, authmethod ) != LDAP_SUCCESS ) {
121         ldap_perror( ld, "ldap_bind" );
122         exit( 1 );
123     }
124
125     if ( fp == NULL ) {
126         for ( ; optind < argc; ++optind ) {
127             rc = dodelete( ld, argv[ optind ] );
128         }
129     } else {
130         rc = 0;
131         while ((rc == 0 || contoper) && fgets(buf, sizeof(buf), fp) != NULL) {
132             buf[ strlen( buf ) - 1 ] = '\0';    /* remove trailing newline */
133             if ( *buf != '\0' ) {
134                 rc = dodelete( ld, buf );
135             }
136         }
137     }
138
139     ldap_unbind( ld );
140
141     exit( rc );
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 }