]> git.sur5r.net Git - openldap/blob - clients/tools/ldapdelete.c
df1103cda15c00e09883ddb09ee9bb4a4418c1ee
[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] [-W] [-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, authmethod, want_bindpw, debug;
37
38     not = verbose = contoper = want_bindpw = debug = 0;
39     fp = NULL;
40     authmethod = LDAP_AUTH_SIMPLE;
41
42     while (( i = getopt( argc, argv, "WnvkKch:p:D:w:d:f:" )) != EOF ) {
43         switch( i ) {
44         case 'k':       /* kerberos bind */
45 #ifdef HAVE_KERBEROS
46                 authmethod = LDAP_AUTH_KRBV4;
47 #else
48                 fprintf (stderr, "%s was not compiled with Kerberos support\n", argv[0]);
49 #endif
50             break;
51         case 'K':       /* kerberos bind, part one only */
52 #ifdef HAVE_KERBEROS
53                 authmethod = LDAP_AUTH_KRBV41;
54 #else
55                 fprintf (stderr, "%s was not compiled with Kerberos support\n", argv[0]);
56 #endif
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             debug |= atoi( optarg );
78             break;
79         case 'p':
80             ldapport = atoi( optarg );
81             break;
82         case 'n':       /* print deletes, don't actually do them */
83             ++not;
84             break;
85         case 'v':       /* verbose mode */
86             verbose++;
87             break;
88         case 'W':
89                 want_bindpw++;
90                 break;
91         default:
92             fprintf( stderr, usage, argv[0] );
93             exit( 1 );
94         }
95     }
96
97     if ( fp == NULL ) {
98         if ( optind >= argc ) {
99             fp = stdin;
100         }
101     }
102
103         if ( debug ) {
104                 lber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug );
105                 ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug );
106         }
107
108     if (( ld = ldap_open( ldaphost, ldapport )) == NULL ) {
109         perror( "ldap_open" );
110         exit( 1 );
111     }
112
113         {
114                 /* this seems prudent */
115                 int deref = LDAP_DEREF_NEVER;
116                 ldap_set_option( ld, LDAP_OPT_DEREF, &deref );
117         }
118
119         if (want_bindpw)
120                 passwd = getpass("Enter LDAP Password: ");
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 }