1 /* ldapdelete.c - simple program to delete an entry using LDAP */
10 #include <ac/unistd.h>
11 extern char *strdup (const char *);
16 static char *binddn = NULL;
17 static char *passwd = NULL;
18 static char *base = NULL;
19 static char *ldaphost = NULL;
20 static int ldapport = 0;
21 static int not, verbose, contoper;
24 #define safe_realloc( ptr, size ) ( ptr == NULL ? malloc( size ) : \
27 static int dodelete LDAP_P((
32 main( int argc, char **argv )
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, 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" );
105 /* this seems prudent */
106 int deref = LDAP_DEREF_NEVER;
107 ldap_set_option( ld, LDAP_OPT_DEREF, &deref );
111 authmethod = LDAP_AUTH_SIMPLE;
112 } else if ( kerberos == 1 ) {
113 authmethod = LDAP_AUTH_KRBV41;
115 authmethod = LDAP_AUTH_KRBV4;
117 if ( ldap_bind_s( ld, binddn, passwd, authmethod ) != LDAP_SUCCESS ) {
118 ldap_perror( ld, "ldap_bind" );
123 for ( ; optind < argc; ++optind ) {
124 rc = dodelete( ld, argv[ optind ] );
128 while ((rc == 0 || contoper) && fgets(buf, sizeof(buf), fp) != NULL) {
129 buf[ strlen( buf ) - 1 ] = '\0'; /* remove trailing newline */
130 if ( *buf != '\0' ) {
131 rc = dodelete( ld, buf );
152 printf( "%sdeleting entry %s\n", not ? "!" : "", dn );
157 if (( rc = ldap_delete_s( ld, dn )) != LDAP_SUCCESS ) {
158 ldap_perror( ld, "ldap_delete" );
159 } else if ( verbose ) {
160 printf( "entry removed\n" );