1 /* ldapdelete.c - simple program to delete an entry using LDAP */
4 * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
12 #include <ac/stdlib.h>
15 #include <ac/signal.h>
16 #include <ac/string.h>
17 #include <ac/unistd.h>
22 static char *binddn = NULL;
23 static char *passwd = NULL;
24 static char *ldaphost = NULL;
25 static int ldapport = 0;
26 static int not, verbose, contoper;
29 static int dodelete LDAP_P((
34 main( int argc, char **argv )
36 char *usage = "usage: %s [-n] [-v] [-k] [-W] [-M[M]] [-d debug-level] [-f file] [-h ldaphost] [-P version] [-p ldapport] [-D binddn] [-w passwd] [dn]...\n";
39 int i, rc, authmethod, want_bindpw, version, debug, manageDSAit;
41 not = verbose = contoper = want_bindpw = debug = manageDSAit = 0;
43 authmethod = LDAP_AUTH_SIMPLE;
46 while (( i = getopt( argc, argv, "WMnvkKch:P:p:D:w:d:f:" )) != EOF ) {
48 case 'k': /* kerberos bind */
50 authmethod = LDAP_AUTH_KRBV4;
52 fprintf (stderr, "%s was not compiled with Kerberos support\n", argv[0]);
53 fprintf( stderr, usage, argv[0] );
54 return( EXIT_FAILURE );
57 case 'K': /* kerberos bind, part one only */
59 authmethod = LDAP_AUTH_KRBV41;
61 fprintf (stderr, "%s was not compiled with Kerberos support\n", argv[0]);
62 fprintf( stderr, usage, argv[0] );
63 return( EXIT_FAILURE );
66 case 'c': /* continuous operation mode */
69 case 'h': /* ldap host */
70 ldaphost = strdup( optarg );
72 case 'D': /* bind DN */
73 binddn = strdup( optarg );
75 case 'w': /* password */
76 passwd = strdup( optarg );
80 for( p = optarg; *p == '\0'; p++ ) {
85 case 'f': /* read DNs from a file */
86 if (( fp = fopen( optarg, "r" )) == NULL ) {
92 debug |= atoi( optarg );
95 ldapport = atoi( optarg );
97 case 'n': /* print deletes, don't actually do them */
100 case 'v': /* verbose mode */
104 /* enable Manage DSA IT */
111 switch( atoi(optarg) )
114 version = LDAP_VERSION2;
117 version = LDAP_VERSION3;
120 fprintf( stderr, "protocol version should be 2 or 3\n" );
121 fprintf( stderr, usage, argv[0] );
122 return( EXIT_FAILURE );
126 fprintf( stderr, usage, argv[0] );
127 return( EXIT_FAILURE );
132 if ( optind >= argc ) {
138 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
139 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
141 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
142 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
147 (void) SIGNAL( SIGPIPE, SIG_IGN );
150 if (( ld = ldap_init( ldaphost, ldapport )) == NULL ) {
151 perror( "ldap_init" );
152 return( EXIT_FAILURE );
156 /* this seems prudent */
157 int deref = LDAP_DEREF_NEVER;
158 ldap_set_option( ld, LDAP_OPT_DEREF, &deref );
161 /* don't chase referrals */
162 ldap_set_option( ld, LDAP_OPT_REFERRALS, LDAP_OPT_OFF );
165 passwd = getpass("Enter LDAP Password: ");
168 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version ) != LDAP_OPT_SUCCESS)
170 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n", version );
173 if ( ldap_bind_s( ld, binddn, passwd, authmethod ) != LDAP_SUCCESS ) {
174 ldap_perror( ld, "ldap_bind" );
175 return( EXIT_FAILURE );
181 LDAPControl *ctrls[2];
185 c.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
186 c.ldctl_value.bv_val = NULL;
187 c.ldctl_value.bv_len = 0;
188 c.ldctl_iscritical = manageDSAit > 1;
190 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, &ctrls );
192 if( err != LDAP_OPT_SUCCESS ) {
193 fprintf( stderr, "Could not set Manage DSA IT Control\n" );
194 if( c.ldctl_iscritical ) {
195 exit( EXIT_FAILURE );
202 for ( ; optind < argc; ++optind ) {
203 rc = dodelete( ld, argv[ optind ] );
206 while ((rc == 0 || contoper) && fgets(buf, sizeof(buf), fp) != NULL) {
207 buf[ strlen( buf ) - 1 ] = '\0'; /* remove trailing newline */
208 if ( *buf != '\0' ) {
209 rc = dodelete( ld, buf );
227 printf( "%sdeleting entry \"%s\"\n",
228 (not ? "!" : ""), dn );
233 if (( rc = ldap_delete_s( ld, dn )) != LDAP_SUCCESS ) {
234 ldap_perror( ld, "ldap_delete" );
235 } else if ( verbose ) {
236 printf( "\tremoved\n" );