1 /* ldapdelete.c - simple program to delete an entry using LDAP */
10 #include <ac/unistd.h>
15 #include "ldapconfig.h"
17 static char *binddn = LDAPDELETE_BINDDN;
18 static char *passwd = LDAPDELETE_BIND_CRED;
19 static char *base = LDAPDELETE_BASE;
20 static char *ldaphost = LDAPHOST;
21 static int ldapport = LDAP_PORT;
22 static int not, verbose, contoper;
26 extern int ldap_debug, lber_debug;
27 #endif /* LDAP_DEBUG */
29 #define safe_realloc( ptr, size ) ( ptr == NULL ? malloc( size ) : \
32 static int dodelete LDAP_P((
41 char *usage = "usage: %s [-n] [-v] [-k] [-d debug-level] [-f file] [-h ldaphost] [-p ldapport] [-D binddn] [-w passwd] [dn]...\n";
44 int i, rc, kerberos, authmethod;
49 kerberos = not = verbose = contoper = 0;
52 while (( i = getopt( argc, argv, "nvkKch:p:D:w:d:f:" )) != EOF ) {
54 case 'k': /* kerberos bind */
57 case 'K': /* kerberos bind, part one only */
60 case 'c': /* continuous operation mode */
63 case 'h': /* ldap host */
64 ldaphost = strdup( optarg );
66 case 'D': /* bind DN */
67 binddn = strdup( optarg );
69 case 'w': /* password */
70 passwd = strdup( optarg );
72 case 'f': /* read DNs from a file */
73 if (( fp = fopen( optarg, "r" )) == NULL ) {
80 ldap_debug = lber_debug = atoi( optarg ); /* */
81 #else /* LDAP_DEBUG */
82 fprintf( stderr, "compile with -DLDAP_DEBUG for debugging\n" );
83 #endif /* LDAP_DEBUG */
86 ldapport = atoi( optarg );
88 case 'n': /* print deletes, don't actually do them */
91 case 'v': /* verbose mode */
95 fprintf( stderr, usage, argv[0] );
101 if ( optind >= argc ) {
106 if (( ld = ldap_open( ldaphost, ldapport )) == NULL ) {
107 perror( "ldap_open" );
111 #if LDAP_VERSION > LDAP_VERSION2
112 /* this seems prudent */
113 ldap_set_option( LDAP_OPT_DEREF, LDAP_DEREF_NEVER);
117 authmethod = LDAP_AUTH_SIMPLE;
118 } else if ( kerberos == 1 ) {
119 authmethod = LDAP_AUTH_KRBV41;
121 authmethod = LDAP_AUTH_KRBV4;
123 if ( ldap_bind_s( ld, binddn, passwd, authmethod ) != LDAP_SUCCESS ) {
124 ldap_perror( ld, "ldap_bind" );
129 for ( ; optind < argc; ++optind ) {
130 rc = dodelete( ld, argv[ optind ] );
134 while ((rc == 0 || contoper) && fgets(buf, sizeof(buf), fp) != NULL) {
135 buf[ strlen( buf ) - 1 ] = '\0'; /* remove trailing newline */
136 if ( *buf != '\0' ) {
137 rc = dodelete( ld, buf );
155 printf( "%sdeleting entry %s\n", not ? "!" : "", dn );
160 if (( rc = ldap_delete_s( ld, dn )) != LDAP_SUCCESS ) {
161 ldap_perror( ld, "ldap_delete" );
162 } else if ( verbose ) {
163 printf( "entry removed\n" );