]> git.sur5r.net Git - openldap/blob - clients/tools/ldapdelete.c
ignore SIGPIPE
[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/signal.h>
10 #include <ac/string.h>
11 #include <ac/unistd.h>
12
13 #include <lber.h>
14 #include <ldap.h>
15
16 static char     *binddn = NULL;
17 static char     *passwd = NULL;
18 static char     *base = NULL;
19 static char     *ldaphost = NULL;
20 static int      ldapport = 0;
21 static int      not, verbose, contoper;
22 static LDAP     *ld;
23
24 #define safe_realloc( ptr, size )       ( ptr == NULL ? malloc( size ) : \
25                                          realloc( ptr, size ))
26
27 static int dodelete LDAP_P((
28     LDAP        *ld,
29     char        *dn));
30
31 int
32 main( int argc, char **argv )
33 {
34         char            *usage = "usage: %s [-n] [-v] [-k] [-W] [-d debug-level] [-f file] [-h ldaphost] [-P version] [-p ldapport] [-D binddn] [-w passwd] [dn]...\n";
35     char                buf[ 4096 ];
36     FILE                *fp;
37         int             i, rc, authmethod, want_bindpw, version, debug;
38
39     not = verbose = contoper = want_bindpw = debug = 0;
40     fp = NULL;
41     authmethod = LDAP_AUTH_SIMPLE;
42         version = -1;
43
44     while (( i = getopt( argc, argv, "WnvkKch:P:p:D:w:d:f:" )) != EOF ) {
45         switch( i ) {
46         case 'k':       /* kerberos bind */
47 #ifdef HAVE_KERBEROS
48                 authmethod = LDAP_AUTH_KRBV4;
49 #else
50                 fprintf (stderr, "%s was not compiled with Kerberos support\n", argv[0]);
51 #endif
52             break;
53         case 'K':       /* kerberos bind, part one only */
54 #ifdef HAVE_KERBEROS
55                 authmethod = LDAP_AUTH_KRBV41;
56 #else
57                 fprintf (stderr, "%s was not compiled with Kerberos support\n", argv[0]);
58 #endif
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             debug |= atoi( optarg );
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         case 'W':
91                 want_bindpw++;
92                 break;
93         case 'P':
94                 switch(optarg[0])
95                 {
96                 case '2':
97                         version = LDAP_VERSION2;
98                         break;
99                 case '3':
100                         version = LDAP_VERSION3;
101                         break;
102                 }
103                 break;
104         default:
105             fprintf( stderr, usage, argv[0] );
106             exit( 1 );
107         }
108     }
109
110     if ( fp == NULL ) {
111         if ( optind >= argc ) {
112             fp = stdin;
113         }
114     }
115
116         if ( debug ) {
117                 lber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug );
118                 ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug );
119         }
120
121 #ifdef SIGPIPE
122         (void) SIGNAL( SIGPIPE, SIG_IGN );
123 #endif
124
125     if (( ld = ldap_open( ldaphost, ldapport )) == NULL ) {
126         perror( "ldap_open" );
127         exit( 1 );
128     }
129
130         {
131                 /* this seems prudent */
132                 int deref = LDAP_DEREF_NEVER;
133                 ldap_set_option( ld, LDAP_OPT_DEREF, &deref );
134         }
135
136         if (want_bindpw)
137                 passwd = getpass("Enter LDAP Password: ");
138
139         if( version != -1 ) {
140                 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version );
141         }
142
143     if ( ldap_bind_s( ld, binddn, passwd, authmethod ) != LDAP_SUCCESS ) {
144         ldap_perror( ld, "ldap_bind" );
145         exit( 1 );
146     }
147
148     if ( fp == NULL ) {
149         for ( ; optind < argc; ++optind ) {
150             rc = dodelete( ld, argv[ optind ] );
151         }
152     } else {
153         rc = 0;
154         while ((rc == 0 || contoper) && fgets(buf, sizeof(buf), fp) != NULL) {
155             buf[ strlen( buf ) - 1 ] = '\0';    /* remove trailing newline */
156             if ( *buf != '\0' ) {
157                 rc = dodelete( ld, buf );
158             }
159         }
160     }
161
162     ldap_unbind( ld );
163
164     exit( rc );
165
166         /* UNREACHABLE */
167         return(0);
168 }
169
170
171 static int dodelete(
172     LDAP        *ld,
173     char        *dn)
174 {
175     int rc;
176
177     if ( verbose ) {
178         printf( "%sdeleting entry \"%s\"\n",
179                 (not ? "!" : ""), dn );
180     }
181     if ( not ) {
182         rc = LDAP_SUCCESS;
183     } else {
184         if (( rc = ldap_delete_s( ld, dn )) != LDAP_SUCCESS ) {
185             ldap_perror( ld, "ldap_delete" );
186         } else if ( verbose ) {
187             printf( "\tremoved\n" );
188         }
189     }
190
191     return( rc );
192 }