1 /* ldapdelete.c - simple program to delete an entry using LDAP */
12 #include "ldapconfig.h"
14 static char *binddn = LDAPDELETE_BINDDN;
15 static char *base = LDAPDELETE_BASE;
16 static char *passwd = NULL;
17 static char *ldaphost = LDAPHOST;
18 static int ldapport = LDAP_PORT;
19 static int not, verbose, contoper;
23 extern int ldap_debug, lber_debug;
24 #endif /* LDAP_DEBUG */
26 #define safe_realloc( ptr, size ) ( ptr == NULL ? malloc( size ) : \
34 char *usage = "usage: %s [-n] [-v] [-k] [-d debug-level] [-f file] [-h ldaphost] [-p ldapport] [-D binddn] [-w passwd] [dn]...\n";
37 int i, rc, kerberos, linenum, authmethod;
42 kerberos = not = verbose = contoper = 0;
45 while (( i = getopt( argc, argv, "nvkKch:p:D:w:d:f:" )) != EOF ) {
47 case 'k': /* kerberos bind */
50 case 'K': /* kerberos bind, part one only */
53 case 'c': /* continuous operation mode */
56 case 'h': /* ldap host */
57 ldaphost = strdup( optarg );
59 case 'D': /* bind DN */
60 binddn = strdup( optarg );
62 case 'w': /* password */
63 passwd = strdup( optarg );
65 case 'f': /* read DNs from a file */
66 if (( fp = fopen( optarg, "r" )) == NULL ) {
73 ldap_debug = lber_debug = atoi( optarg ); /* */
74 #else /* LDAP_DEBUG */
75 fprintf( stderr, "compile with -DLDAP_DEBUG for debugging\n" );
76 #endif /* LDAP_DEBUG */
79 ldapport = atoi( optarg );
81 case 'n': /* print deletes, don't actually do them */
84 case 'v': /* verbose mode */
88 fprintf( stderr, usage, argv[0] );
94 if ( optind >= argc ) {
99 if (( ld = ldap_open( ldaphost, ldapport )) == NULL ) {
100 perror( "ldap_open" );
104 ld->ld_deref = LDAP_DEREF_NEVER; /* prudent, but probably unnecessary */
107 authmethod = LDAP_AUTH_SIMPLE;
108 } else if ( kerberos == 1 ) {
109 authmethod = LDAP_AUTH_KRBV41;
111 authmethod = LDAP_AUTH_KRBV4;
113 if ( ldap_bind_s( ld, binddn, passwd, authmethod ) != LDAP_SUCCESS ) {
114 ldap_perror( ld, "ldap_bind" );
119 for ( ; optind < argc; ++optind ) {
120 rc = dodelete( ld, argv[ optind ] );
124 while ((rc == 0 || contoper) && fgets(buf, sizeof(buf), fp) != NULL) {
125 buf[ strlen( buf ) - 1 ] = '\0'; /* remove trailing newline */
126 if ( *buf != '\0' ) {
127 rc = dodelete( ld, buf );
145 printf( "%sdeleting entry %s\n", not ? "!" : "", dn );
150 if (( rc = ldap_delete_s( ld, dn )) != LDAP_SUCCESS ) {
151 ldap_perror( ld, "ldap_delete" );
152 } else if ( verbose ) {
153 printf( "entry removed\n" );