]> git.sur5r.net Git - openldap/blob - clients/tools/ldapdelete.c
Modified build environment to correctly support bin,sbin,libexec,etc
[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 <lber.h>
8 #include <ldap.h>
9
10 #include "ldapconfig.h"
11
12 static char     *binddn = LDAPDELETE_BINDDN;
13 static char     *base = LDAPDELETE_BASE;
14 static char     *passwd = NULL;
15 static char     *ldaphost = LDAPHOST;
16 static int      ldapport = LDAP_PORT;
17 static int      not, verbose, contoper;
18 static LDAP     *ld;
19
20 #ifdef LDAP_DEBUG
21 extern int ldap_debug, lber_debug;
22 #endif /* LDAP_DEBUG */
23
24 #define safe_realloc( ptr, size )       ( ptr == NULL ? malloc( size ) : \
25                                          realloc( ptr, size ))
26
27
28 main( argc, argv )
29     int         argc;
30     char        **argv;
31 {
32     char                *usage = "usage: %s [-n] [-v] [-k] [-d debug-level] [-f file] [-h ldaphost] [-p ldapport] [-D binddn] [-w passwd] [dn]...\n";
33     char                *p, buf[ 4096 ];
34     FILE                *fp;
35     int                 i, rc, kerberos, linenum, authmethod;
36
37     extern char *optarg;
38     extern int  optind;
39
40     kerberos = not = verbose = contoper = 0;
41     fp = NULL;
42
43     while (( i = getopt( argc, argv, "nvkKch:p:D:w:d:f:" )) != EOF ) {
44         switch( i ) {
45         case 'k':       /* kerberos bind */
46             kerberos = 2;
47             break;
48         case 'K':       /* kerberos bind, part one only */
49             kerberos = 1;
50             break;
51         case 'c':       /* continuous operation mode */
52             ++contoper;
53             break;
54         case 'h':       /* ldap host */
55             ldaphost = strdup( optarg );
56             break;
57         case 'D':       /* bind DN */
58             binddn = strdup( optarg );
59             break;
60         case 'w':       /* password */
61             passwd = strdup( optarg );
62             break;
63         case 'f':       /* read DNs from a file */
64             if (( fp = fopen( optarg, "r" )) == NULL ) {
65                 perror( optarg );
66                 exit( 1 );
67             }
68             break;
69         case 'd':
70 #ifdef LDAP_DEBUG
71             ldap_debug = lber_debug = atoi( optarg );   /* */
72 #else /* LDAP_DEBUG */
73             fprintf( stderr, "compile with -DLDAP_DEBUG for debugging\n" );
74 #endif /* LDAP_DEBUG */
75             break;
76         case 'p':
77             ldapport = atoi( optarg );
78             break;
79         case 'n':       /* print deletes, don't actually do them */
80             ++not;
81             break;
82         case 'v':       /* verbose mode */
83             verbose++;
84             break;
85         default:
86             fprintf( stderr, usage, argv[0] );
87             exit( 1 );
88         }
89     }
90
91     if ( fp == NULL ) {
92         if ( optind >= argc ) {
93             fp = stdin;
94         }
95     }
96
97     if (( ld = ldap_open( ldaphost, ldapport )) == NULL ) {
98         perror( "ldap_open" );
99         exit( 1 );
100     }
101
102     ld->ld_deref = LDAP_DEREF_NEVER;    /* prudent, but probably unnecessary */
103
104     if ( !kerberos ) {
105         authmethod = LDAP_AUTH_SIMPLE;
106     } else if ( kerberos == 1 ) {
107         authmethod = LDAP_AUTH_KRBV41;
108     } else {
109         authmethod = LDAP_AUTH_KRBV4;
110     }
111     if ( ldap_bind_s( ld, binddn, passwd, authmethod ) != LDAP_SUCCESS ) {
112         ldap_perror( ld, "ldap_bind" );
113         exit( 1 );
114     }
115
116     if ( fp == NULL ) {
117         for ( ; optind < argc; ++optind ) {
118             rc = dodelete( ld, argv[ optind ] );
119         }
120     } else {
121         rc = 0;
122         while ((rc == 0 || contoper) && fgets(buf, sizeof(buf), fp) != NULL) {
123             buf[ strlen( buf ) - 1 ] = '\0';    /* remove trailing newline */
124             if ( *buf != '\0' ) {
125                 rc = dodelete( ld, buf );
126             }
127         }
128     }
129
130     ldap_unbind( ld );
131
132     exit( rc );
133 }
134
135
136 dodelete( ld, dn )
137     LDAP        *ld;
138     char        *dn;
139 {
140     int rc;
141
142     if ( verbose ) {
143         printf( "%sdeleting entry %s\n", not ? "!" : "", dn );
144     }
145     if ( not ) {
146         rc = LDAP_SUCCESS;
147     } else {
148         if (( rc = ldap_delete_s( ld, dn )) != LDAP_SUCCESS ) {
149             ldap_perror( ld, "ldap_delete" );
150         } else if ( verbose ) {
151             printf( "entry removed\n" );
152         }
153     }
154
155     return( rc );
156 }