1 /* ldapdelete.c - simple program to delete an entry using LDAP */
10 #include <ac/unistd.h>
15 static char *binddn = NULL;
16 static char *passwd = NULL;
17 static char *base = NULL;
18 static char *ldaphost = NULL;
19 static int ldapport = 0;
20 static int not, verbose, contoper;
23 #define safe_realloc( ptr, size ) ( ptr == NULL ? malloc( size ) : \
26 static int dodelete LDAP_P((
31 main( int argc, char **argv )
33 char *usage = "usage: %s [-n] [-v] [-k] [-W] [-d debug-level] [-f file] [-h ldaphost] [-p ldapport] [-D binddn] [-w passwd] [dn]...\n";
36 int i, rc, authmethod, want_bindpw, debug;
38 not = verbose = contoper = want_bindpw = debug = 0;
40 authmethod = LDAP_AUTH_SIMPLE;
42 while (( i = getopt( argc, argv, "WnvkKch:p:D:w:d:f:" )) != EOF ) {
44 case 'k': /* kerberos bind */
46 authmethod = LDAP_AUTH_KRBV4;
48 fprintf (stderr, "%s was not compiled with Kerberos support\n", argv[0]);
51 case 'K': /* kerberos bind, part one only */
53 authmethod = LDAP_AUTH_KRBV41;
55 fprintf (stderr, "%s was not compiled with Kerberos support\n", argv[0]);
58 case 'c': /* continuous operation mode */
61 case 'h': /* ldap host */
62 ldaphost = strdup( optarg );
64 case 'D': /* bind DN */
65 binddn = strdup( optarg );
67 case 'w': /* password */
68 passwd = strdup( optarg );
70 case 'f': /* read DNs from a file */
71 if (( fp = fopen( optarg, "r" )) == NULL ) {
77 debug |= atoi( optarg );
80 ldapport = atoi( optarg );
82 case 'n': /* print deletes, don't actually do them */
85 case 'v': /* verbose mode */
92 fprintf( stderr, usage, argv[0] );
98 if ( optind >= argc ) {
104 lber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug );
105 ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug );
108 if (( ld = ldap_open( ldaphost, ldapport )) == NULL ) {
109 perror( "ldap_open" );
114 /* this seems prudent */
115 int deref = LDAP_DEREF_NEVER;
116 ldap_set_option( ld, LDAP_OPT_DEREF, &deref );
120 passwd = getpass("Enter LDAP Password: ");
122 if ( ldap_bind_s( ld, binddn, passwd, authmethod ) != LDAP_SUCCESS ) {
123 ldap_perror( ld, "ldap_bind" );
128 for ( ; optind < argc; ++optind ) {
129 rc = dodelete( ld, argv[ optind ] );
133 while ((rc == 0 || contoper) && fgets(buf, sizeof(buf), fp) != NULL) {
134 buf[ strlen( buf ) - 1 ] = '\0'; /* remove trailing newline */
135 if ( *buf != '\0' ) {
136 rc = dodelete( ld, buf );
157 printf( "%sdeleting entry %s\n", not ? "!" : "", dn );
162 if (( rc = ldap_delete_s( ld, dn )) != LDAP_SUCCESS ) {
163 ldap_perror( ld, "ldap_delete" );
164 } else if ( verbose ) {
165 printf( "entry removed\n" );