]> git.sur5r.net Git - openldap/blob - clients/tools/ldapdelete.c
29a372df594dc7886ff01fc65cc70119c7997cb0
[openldap] / clients / tools / ldapdelete.c
1 /* ldapdelete.c - simple program to delete an entry using LDAP */
2
3 #include <stdio.h>
4 #include <string.h>
5 #include <stdlib.h>
6 #include <ctype.h>
7 #include <time.h>
8
9 #include <lber.h>
10 #include <ldap.h>
11
12 #include "ldapconfig.h"
13
14 static char     *binddn = LDAPDELETE_BINDDN;
15 static char     *passwd = LDAPDELETE_BIND_CRED;
16 static char     *base = LDAPDELETE_BASE;
17 static char     *ldaphost = LDAPHOST;
18 static int      ldapport = LDAP_PORT;
19 static int      not, verbose, contoper;
20 static LDAP     *ld;
21
22 #ifdef LDAP_DEBUG
23 extern int ldap_debug, lber_debug;
24 #endif /* LDAP_DEBUG */
25
26 #define safe_realloc( ptr, size )       ( ptr == NULL ? malloc( size ) : \
27                                          realloc( ptr, size ))
28
29
30 main( argc, argv )
31     int         argc;
32     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                *p, buf[ 4096 ];
36     FILE                *fp;
37     int                 i, rc, kerberos, linenum, 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     ld->ld_deref = LDAP_DEREF_NEVER;    /* prudent, but probably unnecessary */
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
137
138 dodelete( ld, dn )
139     LDAP        *ld;
140     char        *dn;
141 {
142     int rc;
143
144     if ( verbose ) {
145         printf( "%sdeleting entry %s\n", not ? "!" : "", dn );
146     }
147     if ( not ) {
148         rc = LDAP_SUCCESS;
149     } else {
150         if (( rc = ldap_delete_s( ld, dn )) != LDAP_SUCCESS ) {
151             ldap_perror( ld, "ldap_delete" );
152         } else if ( verbose ) {
153             printf( "entry removed\n" );
154         }
155     }
156
157     return( rc );
158 }