#include "portable.h"
#include <stdio.h>
-#include <stdlib.h>
-#include <ctype.h>
+#include <ac/stdlib.h>
+#include <ac/ctype.h>
+
+#include <ac/signal.h>
#include <ac/string.h>
#include <ac/unistd.h>
static int not, verbose, contoper;
static LDAP *ld;
-#define safe_realloc( ptr, size ) ( ptr == NULL ? malloc( size ) : \
- realloc( ptr, size ))
-
static int dodelete LDAP_P((
LDAP *ld,
char *dn));
not = verbose = contoper = want_bindpw = debug = 0;
fp = NULL;
authmethod = LDAP_AUTH_SIMPLE;
- version = LDAP_VERSION2;
+ version = -1;
while (( i = getopt( argc, argv, "WnvkKch:P:p:D:w:d:f:" )) != EOF ) {
switch( i ) {
authmethod = LDAP_AUTH_KRBV4;
#else
fprintf (stderr, "%s was not compiled with Kerberos support\n", argv[0]);
+ fprintf( stderr, usage, argv[0] );
+ return( EXIT_FAILURE );
#endif
break;
case 'K': /* kerberos bind, part one only */
authmethod = LDAP_AUTH_KRBV41;
#else
fprintf (stderr, "%s was not compiled with Kerberos support\n", argv[0]);
+ fprintf( stderr, usage, argv[0] );
+ return( EXIT_FAILURE );
#endif
break;
case 'c': /* continuous operation mode */
want_bindpw++;
break;
case 'P':
- switch(optarg[0])
+ switch( atoi(optarg) )
{
- case '2':
+ case 2:
version = LDAP_VERSION2;
break;
- case '3':
+ case 3:
version = LDAP_VERSION3;
break;
+ default:
+ fprintf( stderr, "protocol version should be 2 or 3\n" );
+ fprintf( stderr, usage, argv[0] );
+ return( EXIT_FAILURE );
}
break;
default:
fprintf( stderr, usage, argv[0] );
- exit( 1 );
+ return( EXIT_FAILURE );
}
}
}
if ( debug ) {
- lber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug );
- ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug );
+ if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_ERROR ) {
+ fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
+ }
+ if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_ERROR ) {
+ fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
+ }
}
- if (( ld = ldap_open( ldaphost, ldapport )) == NULL ) {
- perror( "ldap_open" );
- exit( 1 );
+#ifdef SIGPIPE
+ (void) SIGNAL( SIGPIPE, SIG_IGN );
+#endif
+
+ if (( ld = ldap_init( ldaphost, ldapport )) == NULL ) {
+ perror( "ldap_init" );
+ return( EXIT_FAILURE );
}
{
if (want_bindpw)
passwd = getpass("Enter LDAP Password: ");
- ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version );
+ if (version != -1 &&
+ ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version ) == LDAP_OPT_ERROR)
+ {
+ fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n", version );
+ }
if ( ldap_bind_s( ld, binddn, passwd, authmethod ) != LDAP_SUCCESS ) {
ldap_perror( ld, "ldap_bind" );
- exit( 1 );
+ return( EXIT_FAILURE );
}
if ( fp == NULL ) {
ldap_unbind( ld );
- exit( rc );
-
- /* UNREACHABLE */
- return(0);
+ return( rc );
}
int rc;
if ( verbose ) {
- printf( "%sdeleting entry %s\n", not ? "!" : "", dn );
+ printf( "%sdeleting entry \"%s\"\n",
+ (not ? "!" : ""), dn );
}
if ( not ) {
rc = LDAP_SUCCESS;
if (( rc = ldap_delete_s( ld, dn )) != LDAP_SUCCESS ) {
ldap_perror( ld, "ldap_delete" );
} else if ( verbose ) {
- printf( "entry removed\n" );
+ printf( "\tremoved\n" );
}
}