]> git.sur5r.net Git - openldap/blob - clients/tools/ldapdelete.c
Initial LDAP_API_FEATURE_X_OPENLDAP commit:
[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 #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 static int dodelete LDAP_P((
33     LDAP        *ld,
34     char        *dn));
35
36 int
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         {
112                 /* this seems prudent */
113                 int deref = LDAP_DEREF_NEVER;
114                 ldap_set_option( ld, LDAP_OPT_DEREF, &deref );
115         }
116
117     if ( !kerberos ) {
118         authmethod = LDAP_AUTH_SIMPLE;
119     } else if ( kerberos == 1 ) {
120         authmethod = LDAP_AUTH_KRBV41;
121     } else {
122         authmethod = LDAP_AUTH_KRBV4;
123     }
124     if ( ldap_bind_s( ld, binddn, passwd, authmethod ) != LDAP_SUCCESS ) {
125         ldap_perror( ld, "ldap_bind" );
126         exit( 1 );
127     }
128
129     if ( fp == NULL ) {
130         for ( ; optind < argc; ++optind ) {
131             rc = dodelete( ld, argv[ optind ] );
132         }
133     } else {
134         rc = 0;
135         while ((rc == 0 || contoper) && fgets(buf, sizeof(buf), fp) != NULL) {
136             buf[ strlen( buf ) - 1 ] = '\0';    /* remove trailing newline */
137             if ( *buf != '\0' ) {
138                 rc = dodelete( ld, buf );
139             }
140         }
141     }
142
143     ldap_unbind( ld );
144
145     exit( rc );
146
147         /* UNREACHABLE */
148         return(0);
149 }
150
151
152 static int dodelete(
153     LDAP        *ld,
154     char        *dn)
155 {
156     int rc;
157
158     if ( verbose ) {
159         printf( "%sdeleting entry %s\n", not ? "!" : "", dn );
160     }
161     if ( not ) {
162         rc = LDAP_SUCCESS;
163     } else {
164         if (( rc = ldap_delete_s( ld, dn )) != LDAP_SUCCESS ) {
165             ldap_perror( ld, "ldap_delete" );
166         } else if ( verbose ) {
167             printf( "entry removed\n" );
168         }
169     }
170
171     return( rc );
172 }