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] [-d debug-level] [-f file] [-h ldaphost] [-p ldapport] [-D binddn] [-w passwd] [dn]...\n";
36 int i, rc, authmethod;
38 not = verbose = contoper = 0;
40 authmethod = LDAP_AUTH_SIMPLE;
42 while (( i = getopt( argc, argv, "nvkKch: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 ) {
78 ldap_debug = lber_debug = atoi( optarg ); /* */
79 #else /* LDAP_DEBUG */
80 fprintf( stderr, "compile with -DLDAP_DEBUG for debugging\n" );
81 #endif /* LDAP_DEBUG */
84 ldapport = atoi( optarg );
86 case 'n': /* print deletes, don't actually do them */
89 case 'v': /* verbose mode */
93 fprintf( stderr, usage, argv[0] );
99 if ( optind >= argc ) {
104 if (( ld = ldap_open( ldaphost, ldapport )) == NULL ) {
105 perror( "ldap_open" );
110 /* this seems prudent */
111 int deref = LDAP_DEREF_NEVER;
112 ldap_set_option( ld, LDAP_OPT_DEREF, &deref );
115 if ( ldap_bind_s( ld, binddn, passwd, authmethod ) != LDAP_SUCCESS ) {
116 ldap_perror( ld, "ldap_bind" );
121 for ( ; optind < argc; ++optind ) {
122 rc = dodelete( ld, argv[ optind ] );
126 while ((rc == 0 || contoper) && fgets(buf, sizeof(buf), fp) != NULL) {
127 buf[ strlen( buf ) - 1 ] = '\0'; /* remove trailing newline */
128 if ( *buf != '\0' ) {
129 rc = dodelete( ld, buf );
150 printf( "%sdeleting entry %s\n", not ? "!" : "", dn );
155 if (( rc = ldap_delete_s( ld, dn )) != LDAP_SUCCESS ) {
156 ldap_perror( ld, "ldap_delete" );
157 } else if ( verbose ) {
158 printf( "entry removed\n" );