1 /* ldapdelete.c - simple program to delete an entry using LDAP */
10 #include <ac/signal.h>
11 #include <ac/string.h>
12 #include <ac/unistd.h>
17 static char *binddn = NULL;
18 static char *passwd = NULL;
19 static char *base = NULL;
20 static char *ldaphost = NULL;
21 static int ldapport = 0;
22 static int not, verbose, contoper;
25 static int dodelete LDAP_P((
30 main( int argc, char **argv )
32 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";
35 int i, rc, authmethod, want_bindpw, version, debug, manageDSAit;
37 not = verbose = contoper = want_bindpw = debug = manageDSAit = 0;
39 authmethod = LDAP_AUTH_SIMPLE;
42 while (( i = getopt( argc, argv, "WMnvkKch:P:p:D:w:d:f:" )) != EOF ) {
44 case 'k': /* kerberos bind */
46 authmethod = LDAP_AUTH_KRBV4;
48 fprintf (stderr, "%s was not compiled with Kerberos support\n", argv[0]);
49 fprintf( stderr, usage, argv[0] );
50 return( EXIT_FAILURE );
53 case 'K': /* kerberos bind, part one only */
55 authmethod = LDAP_AUTH_KRBV41;
57 fprintf (stderr, "%s was not compiled with Kerberos support\n", argv[0]);
58 fprintf( stderr, usage, argv[0] );
59 return( EXIT_FAILURE );
62 case 'c': /* continuous operation mode */
65 case 'h': /* ldap host */
66 ldaphost = strdup( optarg );
68 case 'D': /* bind DN */
69 binddn = strdup( optarg );
71 case 'w': /* password */
72 passwd = strdup( optarg );
76 for( p = optarg; *p == '\0'; p++ ) {
81 case 'f': /* read DNs from a file */
82 if (( fp = fopen( optarg, "r" )) == NULL ) {
88 debug |= atoi( optarg );
91 ldapport = atoi( optarg );
93 case 'n': /* print deletes, don't actually do them */
96 case 'v': /* verbose mode */
100 /* enable Manage DSA IT */
107 switch( atoi(optarg) )
110 version = LDAP_VERSION2;
113 version = LDAP_VERSION3;
116 fprintf( stderr, "protocol version should be 2 or 3\n" );
117 fprintf( stderr, usage, argv[0] );
118 return( EXIT_FAILURE );
122 fprintf( stderr, usage, argv[0] );
123 return( EXIT_FAILURE );
128 if ( optind >= argc ) {
134 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
135 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
137 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
138 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
143 (void) SIGNAL( SIGPIPE, SIG_IGN );
146 if (( ld = ldap_init( ldaphost, ldapport )) == NULL ) {
147 perror( "ldap_init" );
148 return( EXIT_FAILURE );
152 /* this seems prudent */
153 int deref = LDAP_DEREF_NEVER;
154 ldap_set_option( ld, LDAP_OPT_DEREF, &deref );
158 passwd = getpass("Enter LDAP Password: ");
161 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version ) != LDAP_OPT_SUCCESS)
163 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n", version );
166 if ( ldap_bind_s( ld, binddn, passwd, authmethod ) != LDAP_SUCCESS ) {
167 ldap_perror( ld, "ldap_bind" );
168 return( EXIT_FAILURE );
174 LDAPControl *ctrls[2];
178 c.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
179 c.ldctl_value.bv_val = NULL;
180 c.ldctl_value.bv_len = 0;
181 c.ldctl_iscritical = manageDSAit > 1;
183 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, &ctrls );
185 if( err != LDAP_OPT_SUCCESS ) {
186 fprintf( stderr, "Could not set Manage DSA IT Control\n" );
187 if( c.ldctl_iscritical ) {
188 exit( EXIT_FAILURE );
194 for ( ; optind < argc; ++optind ) {
195 rc = dodelete( ld, argv[ optind ] );
199 while ((rc == 0 || contoper) && fgets(buf, sizeof(buf), fp) != NULL) {
200 buf[ strlen( buf ) - 1 ] = '\0'; /* remove trailing newline */
201 if ( *buf != '\0' ) {
202 rc = dodelete( ld, buf );
220 printf( "%sdeleting entry \"%s\"\n",
221 (not ? "!" : ""), dn );
226 if (( rc = ldap_delete_s( ld, dn )) != LDAP_SUCCESS ) {
227 ldap_perror( ld, "ldap_delete" );
228 } else if ( verbose ) {
229 printf( "\tremoved\n" );