]> git.sur5r.net Git - openldap/blob - clients/tools/ldapdelete.c
Streamlined Kerberos Code.
[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] [-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;
37
38     not = verbose = contoper = 0;
39     fp = NULL;
40     authmethod = LDAP_AUTH_SIMPLE;
41
42     while (( i = getopt( argc, argv, "nvkKch: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 #ifdef LDAP_DEBUG
78             ldap_debug = lber_debug = atoi( optarg );   /* */
79 #else /* LDAP_DEBUG */
80             fprintf( stderr, "compile with -DLDAP_DEBUG for debugging\n" );
81 #endif /* LDAP_DEBUG */
82             break;
83         case 'p':
84             ldapport = atoi( optarg );
85             break;
86         case 'n':       /* print deletes, don't actually do them */
87             ++not;
88             break;
89         case 'v':       /* verbose mode */
90             verbose++;
91             break;
92         default:
93             fprintf( stderr, usage, argv[0] );
94             exit( 1 );
95         }
96     }
97
98     if ( fp == NULL ) {
99         if ( optind >= argc ) {
100             fp = stdin;
101         }
102     }
103
104     if (( ld = ldap_open( ldaphost, ldapport )) == NULL ) {
105         perror( "ldap_open" );
106         exit( 1 );
107     }
108
109         {
110                 /* this seems prudent */
111                 int deref = LDAP_DEREF_NEVER;
112                 ldap_set_option( ld, LDAP_OPT_DEREF, &deref );
113         }
114
115     if ( ldap_bind_s( ld, binddn, passwd, authmethod ) != LDAP_SUCCESS ) {
116         ldap_perror( ld, "ldap_bind" );
117         exit( 1 );
118     }
119
120     if ( fp == NULL ) {
121         for ( ; optind < argc; ++optind ) {
122             rc = dodelete( ld, argv[ optind ] );
123         }
124     } else {
125         rc = 0;
126         while ((rc == 0 || contoper) && fgets(buf, sizeof(buf), fp) != NULL) {
127             buf[ strlen( buf ) - 1 ] = '\0';    /* remove trailing newline */
128             if ( *buf != '\0' ) {
129                 rc = dodelete( ld, buf );
130             }
131         }
132     }
133
134     ldap_unbind( ld );
135
136     exit( rc );
137
138         /* UNREACHABLE */
139         return(0);
140 }
141
142
143 static int dodelete(
144     LDAP        *ld,
145     char        *dn)
146 {
147     int rc;
148
149     if ( verbose ) {
150         printf( "%sdeleting entry %s\n", not ? "!" : "", dn );
151     }
152     if ( not ) {
153         rc = LDAP_SUCCESS;
154     } else {
155         if (( rc = ldap_delete_s( ld, dn )) != LDAP_SUCCESS ) {
156             ldap_perror( ld, "ldap_delete" );
157         } else if ( verbose ) {
158             printf( "entry removed\n" );
159         }
160     }
161
162     return( rc );
163 }