1 /* ldapdelete.c - simple program to delete an entry using LDAP */
3 * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
11 #include <ac/stdlib.h>
14 #include <ac/signal.h>
15 #include <ac/string.h>
16 #include <ac/unistd.h>
21 static char *binddn = NULL;
22 static char *passwd = NULL;
23 static char *ldaphost = NULL;
24 static int ldapport = 0;
25 static int not, verbose, contoper;
28 static int dodelete LDAP_P((
33 main( int argc, char **argv )
35 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";
38 int i, rc, authmethod, want_bindpw, version, debug, manageDSAit;
40 not = verbose = contoper = want_bindpw = debug = manageDSAit = 0;
42 authmethod = LDAP_AUTH_SIMPLE;
45 while (( i = getopt( argc, argv, "WMnvkKch:P:p:D:w:d:f:" )) != EOF ) {
47 case 'k': /* kerberos bind */
49 authmethod = LDAP_AUTH_KRBV4;
51 fprintf (stderr, "%s was not compiled with Kerberos support\n", argv[0]);
52 fprintf( stderr, usage, argv[0] );
53 return( EXIT_FAILURE );
56 case 'K': /* kerberos bind, part one only */
58 authmethod = LDAP_AUTH_KRBV41;
60 fprintf (stderr, "%s was not compiled with Kerberos support\n", argv[0]);
61 fprintf( stderr, usage, argv[0] );
62 return( EXIT_FAILURE );
65 case 'c': /* continuous operation mode */
68 case 'h': /* ldap host */
69 ldaphost = strdup( optarg );
71 case 'D': /* bind DN */
72 binddn = strdup( optarg );
74 case 'w': /* password */
75 passwd = strdup( optarg );
79 for( p = optarg; *p == '\0'; p++ ) {
84 case 'f': /* read DNs from a file */
85 if (( fp = fopen( optarg, "r" )) == NULL ) {
91 debug |= atoi( optarg );
94 ldapport = atoi( optarg );
96 case 'n': /* print deletes, don't actually do them */
99 case 'v': /* verbose mode */
103 /* enable Manage DSA IT */
110 switch( atoi(optarg) )
113 version = LDAP_VERSION2;
116 version = LDAP_VERSION3;
119 fprintf( stderr, "protocol version should be 2 or 3\n" );
120 fprintf( stderr, usage, argv[0] );
121 return( EXIT_FAILURE );
125 fprintf( stderr, usage, argv[0] );
126 return( EXIT_FAILURE );
131 if ( optind >= argc ) {
137 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
138 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
140 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
141 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
146 (void) SIGNAL( SIGPIPE, SIG_IGN );
149 if (( ld = ldap_init( ldaphost, ldapport )) == NULL ) {
150 perror( "ldap_init" );
151 return( EXIT_FAILURE );
155 /* this seems prudent */
156 int deref = LDAP_DEREF_NEVER;
157 ldap_set_option( ld, LDAP_OPT_DEREF, &deref );
160 /* don't chase referrals */
161 ldap_set_option( ld, LDAP_OPT_REFERRALS, LDAP_OPT_OFF );
164 passwd = getpass("Enter LDAP Password: ");
167 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version ) != LDAP_OPT_SUCCESS)
169 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n", version );
172 if ( ldap_bind_s( ld, binddn, passwd, authmethod ) != LDAP_SUCCESS ) {
173 ldap_perror( ld, "ldap_bind" );
174 return( EXIT_FAILURE );
180 LDAPControl *ctrls[2];
184 c.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
185 c.ldctl_value.bv_val = NULL;
186 c.ldctl_value.bv_len = 0;
187 c.ldctl_iscritical = manageDSAit > 1;
189 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, &ctrls );
191 if( err != LDAP_OPT_SUCCESS ) {
192 fprintf( stderr, "Could not set Manage DSA IT Control\n" );
193 if( c.ldctl_iscritical ) {
194 exit( EXIT_FAILURE );
201 for ( ; optind < argc; ++optind ) {
202 rc = dodelete( ld, argv[ optind ] );
205 while ((rc == 0 || contoper) && fgets(buf, sizeof(buf), fp) != NULL) {
206 buf[ strlen( buf ) - 1 ] = '\0'; /* remove trailing newline */
207 if ( *buf != '\0' ) {
208 rc = dodelete( ld, buf );
226 printf( "%sdeleting entry \"%s\"\n",
227 (not ? "!" : ""), dn );
232 if (( rc = ldap_delete_s( ld, dn )) != LDAP_SUCCESS ) {
233 ldap_perror( ld, "ldap_delete" );
234 } else if ( verbose ) {
235 printf( "\tremoved\n" );