]> git.sur5r.net Git - openldap/blobdiff - clients/tools/ldapdelete.c
Fix target directories.
[openldap] / clients / tools / ldapdelete.c
index 20a905998ba39acd5f64f3c94fc6302f30bb78c9..7290fc3368d376f57c747c3a550139aec7db9b97 100644 (file)
@@ -3,9 +3,11 @@
 #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>
 
@@ -20,9 +22,6 @@ static int    ldapport = 0;
 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));
@@ -38,7 +37,7 @@ main( int argc, char **argv )
     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 ) {
@@ -47,6 +46,8 @@ main( int argc, char **argv )
                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 */
@@ -54,6 +55,8 @@ main( int argc, char **argv )
                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 */
@@ -90,19 +93,23 @@ main( int argc, char **argv )
                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 );
        }
     }
 
@@ -113,13 +120,21 @@ main( int argc, char **argv )
     }
 
        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 );
     }
 
        {
@@ -131,11 +146,15 @@ main( int argc, char **argv )
        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 ) {
@@ -154,10 +173,7 @@ main( int argc, char **argv )
 
     ldap_unbind( ld );
 
-    exit( rc );
-
-       /* UNREACHABLE */
-       return(0);
+       return( rc );
 }
 
 
@@ -168,7 +184,8 @@ static int dodelete(
     int        rc;
 
     if ( verbose ) {
-       printf( "%sdeleting entry %s\n", not ? "!" : "", dn );
+       printf( "%sdeleting entry \"%s\"\n",
+               (not ? "!" : ""), dn );
     }
     if ( not ) {
        rc = LDAP_SUCCESS;
@@ -176,7 +193,7 @@ static int dodelete(
        if (( rc = ldap_delete_s( ld, dn )) != LDAP_SUCCESS ) {
            ldap_perror( ld, "ldap_delete" );
        } else if ( verbose ) {
-           printf( "entry removed\n" );
+           printf( "\tremoved\n" );
        }
     }