1 /* ldapdelete.c - simple program to delete an entry using LDAP */
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 ) : \
37 char *usage = "usage: %s [-n] [-v] [-k] [-d debug-level] [-f file] [-h ldaphost] [-p ldapport] [-D binddn] [-w passwd] [dn]...\n";
40 int i, rc, kerberos, linenum, authmethod;
45 kerberos = not = verbose = contoper = 0;
48 while (( i = getopt( argc, argv, "nvkKch:p:D:w:d:f:" )) != EOF ) {
50 case 'k': /* kerberos bind */
53 case 'K': /* kerberos bind, part one only */
56 case 'c': /* continuous operation mode */
59 case 'h': /* ldap host */
60 ldaphost = strdup( optarg );
62 case 'D': /* bind DN */
63 binddn = strdup( optarg );
65 case 'w': /* password */
66 passwd = strdup( optarg );
68 case 'f': /* read DNs from a file */
69 if (( fp = fopen( optarg, "r" )) == NULL ) {
76 ldap_debug = lber_debug = atoi( optarg ); /* */
77 #else /* LDAP_DEBUG */
78 fprintf( stderr, "compile with -DLDAP_DEBUG for debugging\n" );
79 #endif /* LDAP_DEBUG */
82 ldapport = atoi( optarg );
84 case 'n': /* print deletes, don't actually do them */
87 case 'v': /* verbose mode */
91 fprintf( stderr, usage, argv[0] );
97 if ( optind >= argc ) {
102 if (( ld = ldap_open( ldaphost, ldapport )) == NULL ) {
103 perror( "ldap_open" );
107 ld->ld_deref = LDAP_DEREF_NEVER; /* prudent, but probably unnecessary */
110 authmethod = LDAP_AUTH_SIMPLE;
111 } else if ( kerberos == 1 ) {
112 authmethod = LDAP_AUTH_KRBV41;
114 authmethod = LDAP_AUTH_KRBV4;
116 if ( ldap_bind_s( ld, binddn, passwd, authmethod ) != LDAP_SUCCESS ) {
117 ldap_perror( ld, "ldap_bind" );
122 for ( ; optind < argc; ++optind ) {
123 rc = dodelete( ld, argv[ optind ] );
127 while ((rc == 0 || contoper) && fgets(buf, sizeof(buf), fp) != NULL) {
128 buf[ strlen( buf ) - 1 ] = '\0'; /* remove trailing newline */
129 if ( *buf != '\0' ) {
130 rc = dodelete( ld, buf );
148 printf( "%sdeleting entry %s\n", not ? "!" : "", dn );
153 if (( rc = ldap_delete_s( ld, dn )) != LDAP_SUCCESS ) {
154 ldap_perror( ld, "ldap_delete" );
155 } else if ( verbose ) {
156 printf( "entry removed\n" );