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