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, kerberos, authmethod;
38 kerberos = not = verbose = contoper = 0;
41 while (( i = getopt( argc, argv, "nvkKch:p:D:w:d:f:" )) != EOF ) {
43 case 'k': /* kerberos bind */
46 case 'K': /* kerberos bind, part one only */
49 case 'c': /* continuous operation mode */
52 case 'h': /* ldap host */
53 ldaphost = strdup( optarg );
55 case 'D': /* bind DN */
56 binddn = strdup( optarg );
58 case 'w': /* password */
59 passwd = strdup( optarg );
61 case 'f': /* read DNs from a file */
62 if (( fp = fopen( optarg, "r" )) == NULL ) {
69 ldap_debug = lber_debug = atoi( optarg ); /* */
70 #else /* LDAP_DEBUG */
71 fprintf( stderr, "compile with -DLDAP_DEBUG for debugging\n" );
72 #endif /* LDAP_DEBUG */
75 ldapport = atoi( optarg );
77 case 'n': /* print deletes, don't actually do them */
80 case 'v': /* verbose mode */
84 fprintf( stderr, usage, argv[0] );
90 if ( optind >= argc ) {
95 if (( ld = ldap_open( ldaphost, ldapport )) == NULL ) {
96 perror( "ldap_open" );
101 /* this seems prudent */
102 int deref = LDAP_DEREF_NEVER;
103 ldap_set_option( ld, LDAP_OPT_DEREF, &deref );
107 authmethod = LDAP_AUTH_SIMPLE;
108 } else if ( kerberos == 1 ) {
109 authmethod = LDAP_AUTH_KRBV41;
111 authmethod = LDAP_AUTH_KRBV4;
113 if ( ldap_bind_s( ld, binddn, passwd, authmethod ) != LDAP_SUCCESS ) {
114 ldap_perror( ld, "ldap_bind" );
119 for ( ; optind < argc; ++optind ) {
120 rc = dodelete( ld, argv[ optind ] );
124 while ((rc == 0 || contoper) && fgets(buf, sizeof(buf), fp) != NULL) {
125 buf[ strlen( buf ) - 1 ] = '\0'; /* remove trailing newline */
126 if ( *buf != '\0' ) {
127 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" );