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 *base = 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 );
162 passwd = getpass("Enter LDAP Password: ");
165 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version ) != LDAP_OPT_SUCCESS)
167 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n", version );
170 if ( ldap_bind_s( ld, binddn, passwd, authmethod ) != LDAP_SUCCESS ) {
171 ldap_perror( ld, "ldap_bind" );
172 return( EXIT_FAILURE );
178 LDAPControl *ctrls[2];
182 c.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
183 c.ldctl_value.bv_val = NULL;
184 c.ldctl_value.bv_len = 0;
185 c.ldctl_iscritical = manageDSAit > 1;
187 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, &ctrls );
189 if( err != LDAP_OPT_SUCCESS ) {
190 fprintf( stderr, "Could not set Manage DSA IT Control\n" );
191 if( c.ldctl_iscritical ) {
192 exit( EXIT_FAILURE );
199 for ( ; optind < argc; ++optind ) {
200 rc = dodelete( ld, argv[ optind ] );
203 while ((rc == 0 || contoper) && fgets(buf, sizeof(buf), fp) != NULL) {
204 buf[ strlen( buf ) - 1 ] = '\0'; /* remove trailing newline */
205 if ( *buf != '\0' ) {
206 rc = dodelete( ld, buf );
224 printf( "%sdeleting entry \"%s\"\n",
225 (not ? "!" : ""), dn );
230 if (( rc = ldap_delete_s( ld, dn )) != LDAP_SUCCESS ) {
231 ldap_perror( ld, "ldap_delete" );
232 } else if ( verbose ) {
233 printf( "\tremoved\n" );