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 version] [-p ldapport] [-D binddn] [-w passwd] [dn]...\n";
36 int i, rc, authmethod, want_bindpw, version, debug;
38 not = verbose = contoper = want_bindpw = debug = 0;
40 authmethod = LDAP_AUTH_SIMPLE;
41 version = LDAP_VERSION2;
43 while (( i = getopt( argc, argv, "WnvkKch:P:p:D:w:d:f:" )) != EOF ) {
45 case 'k': /* kerberos bind */
47 authmethod = LDAP_AUTH_KRBV4;
49 fprintf (stderr, "%s was not compiled with Kerberos support\n", argv[0]);
52 case 'K': /* kerberos bind, part one only */
54 authmethod = LDAP_AUTH_KRBV41;
56 fprintf (stderr, "%s was not compiled with Kerberos support\n", argv[0]);
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 ) {
78 debug |= atoi( optarg );
81 ldapport = atoi( optarg );
83 case 'n': /* print deletes, don't actually do them */
86 case 'v': /* verbose mode */
96 version = LDAP_VERSION2;
99 version = LDAP_VERSION3;
104 fprintf( stderr, usage, argv[0] );
110 if ( optind >= argc ) {
116 lber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug );
117 ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug );
120 if (( ld = ldap_open( ldaphost, ldapport )) == NULL ) {
121 perror( "ldap_open" );
126 /* this seems prudent */
127 int deref = LDAP_DEREF_NEVER;
128 ldap_set_option( ld, LDAP_OPT_DEREF, &deref );
132 passwd = getpass("Enter LDAP Password: ");
134 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version );
136 if ( ldap_bind_s( ld, binddn, passwd, authmethod ) != LDAP_SUCCESS ) {
137 ldap_perror( ld, "ldap_bind" );
142 for ( ; optind < argc; ++optind ) {
143 rc = dodelete( ld, argv[ optind ] );
147 while ((rc == 0 || contoper) && fgets(buf, sizeof(buf), fp) != NULL) {
148 buf[ strlen( buf ) - 1 ] = '\0'; /* remove trailing newline */
149 if ( *buf != '\0' ) {
150 rc = dodelete( ld, buf );
171 printf( "%sdeleting entry %s\n", not ? "!" : "", dn );
176 if (( rc = ldap_delete_s( ld, dn )) != LDAP_SUCCESS ) {
177 ldap_perror( ld, "ldap_delete" );
178 } else if ( verbose ) {
179 printf( "entry removed\n" );