1 /* ldapdelete.c - simple program to delete an entry using LDAP */
10 #include <ac/string.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((
40 char *usage = "usage: %s [-n] [-v] [-k] [-d debug-level] [-f file] [-h ldaphost] [-p ldapport] [-D binddn] [-w passwd] [dn]...\n";
43 int i, rc, kerberos, authmethod;
48 kerberos = not = verbose = contoper = 0;
51 while (( i = getopt( argc, argv, "nvkKch:p:D:w:d:f:" )) != EOF ) {
53 case 'k': /* kerberos bind */
56 case 'K': /* kerberos bind, part one only */
59 case 'c': /* continuous operation mode */
62 case 'h': /* ldap host */
63 ldaphost = strdup( optarg );
65 case 'D': /* bind DN */
66 binddn = strdup( optarg );
68 case 'w': /* password */
69 passwd = strdup( optarg );
71 case 'f': /* read DNs from a file */
72 if (( fp = fopen( optarg, "r" )) == NULL ) {
79 ldap_debug = lber_debug = atoi( optarg ); /* */
80 #else /* LDAP_DEBUG */
81 fprintf( stderr, "compile with -DLDAP_DEBUG for debugging\n" );
82 #endif /* LDAP_DEBUG */
85 ldapport = atoi( optarg );
87 case 'n': /* print deletes, don't actually do them */
90 case 'v': /* verbose mode */
94 fprintf( stderr, usage, argv[0] );
100 if ( optind >= argc ) {
105 if (( ld = ldap_open( ldaphost, ldapport )) == NULL ) {
106 perror( "ldap_open" );
110 ld->ld_deref = LDAP_DEREF_NEVER; /* prudent, but probably unnecessary */
113 authmethod = LDAP_AUTH_SIMPLE;
114 } else if ( kerberos == 1 ) {
115 authmethod = LDAP_AUTH_KRBV41;
117 authmethod = LDAP_AUTH_KRBV4;
119 if ( ldap_bind_s( ld, binddn, passwd, authmethod ) != LDAP_SUCCESS ) {
120 ldap_perror( ld, "ldap_bind" );
125 for ( ; optind < argc; ++optind ) {
126 rc = dodelete( ld, argv[ optind ] );
130 while ((rc == 0 || contoper) && fgets(buf, sizeof(buf), fp) != NULL) {
131 buf[ strlen( buf ) - 1 ] = '\0'; /* remove trailing newline */
132 if ( *buf != '\0' ) {
133 rc = dodelete( ld, buf );
151 printf( "%sdeleting entry %s\n", not ? "!" : "", dn );
156 if (( rc = ldap_delete_s( ld, dn )) != LDAP_SUCCESS ) {
157 ldap_perror( ld, "ldap_delete" );
158 } else if ( verbose ) {
159 printf( "entry removed\n" );