1 /* ldapdelete.c - simple program to delete an entry using LDAP */
10 #include "ldapconfig.h"
12 static char *binddn = LDAPDELETE_BINDDN;
13 static char *base = LDAPDELETE_BASE;
14 static char *passwd = NULL;
15 static char *ldaphost = LDAPHOST;
16 static int ldapport = LDAP_PORT;
17 static int not, verbose, contoper;
21 extern int ldap_debug, lber_debug;
22 #endif /* LDAP_DEBUG */
24 #define safe_realloc( ptr, size ) ( ptr == NULL ? malloc( size ) : \
32 char *usage = "usage: %s [-n] [-v] [-k] [-d debug-level] [-f file] [-h ldaphost] [-p ldapport] [-D binddn] [-w passwd] [dn]...\n";
35 int i, rc, kerberos, linenum, authmethod;
40 kerberos = not = verbose = contoper = 0;
43 while (( i = getopt( argc, argv, "nvkKch:p:D:w:d:f:" )) != EOF ) {
45 case 'k': /* kerberos bind */
48 case 'K': /* kerberos bind, part one only */
51 case 'c': /* continuous operation mode */
54 case 'h': /* ldap host */
55 ldaphost = strdup( optarg );
57 case 'D': /* bind DN */
58 binddn = strdup( optarg );
60 case 'w': /* password */
61 passwd = strdup( optarg );
63 case 'f': /* read DNs from a file */
64 if (( fp = fopen( optarg, "r" )) == NULL ) {
71 ldap_debug = lber_debug = atoi( optarg ); /* */
72 #else /* LDAP_DEBUG */
73 fprintf( stderr, "compile with -DLDAP_DEBUG for debugging\n" );
74 #endif /* LDAP_DEBUG */
77 ldapport = atoi( optarg );
79 case 'n': /* print deletes, don't actually do them */
82 case 'v': /* verbose mode */
86 fprintf( stderr, usage, argv[0] );
92 if ( optind >= argc ) {
97 if (( ld = ldap_open( ldaphost, ldapport )) == NULL ) {
98 perror( "ldap_open" );
102 ld->ld_deref = LDAP_DEREF_NEVER; /* prudent, but probably unnecessary */
105 authmethod = LDAP_AUTH_SIMPLE;
106 } else if ( kerberos == 1 ) {
107 authmethod = LDAP_AUTH_KRBV41;
109 authmethod = LDAP_AUTH_KRBV4;
111 if ( ldap_bind_s( ld, binddn, passwd, authmethod ) != LDAP_SUCCESS ) {
112 ldap_perror( ld, "ldap_bind" );
117 for ( ; optind < argc; ++optind ) {
118 rc = dodelete( ld, argv[ optind ] );
122 while ((rc == 0 || contoper) && fgets(buf, sizeof(buf), fp) != NULL) {
123 buf[ strlen( buf ) - 1 ] = '\0'; /* remove trailing newline */
124 if ( *buf != '\0' ) {
125 rc = dodelete( ld, buf );
143 printf( "%sdeleting entry %s\n", not ? "!" : "", dn );
148 if (( rc = ldap_delete_s( ld, dn )) != LDAP_SUCCESS ) {
149 ldap_perror( ld, "ldap_delete" );
150 } else if ( verbose ) {
151 printf( "entry removed\n" );