1 /* ldapdelete.c - simple program to delete an entry using LDAP */
10 #include <ac/signal.h>
11 #include <ac/string.h>
12 #include <ac/unistd.h>
17 static char *binddn = NULL;
18 static char *passwd = NULL;
19 static char *base = NULL;
20 static char *ldaphost = NULL;
21 static int ldapport = 0;
22 static int not, verbose, contoper;
25 static int dodelete LDAP_P((
30 main( int argc, char **argv )
32 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";
35 int i, rc, authmethod, want_bindpw, version, debug;
37 not = verbose = contoper = want_bindpw = debug = 0;
39 authmethod = LDAP_AUTH_SIMPLE;
42 while (( i = getopt( argc, argv, "WnvkKch:P: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]);
49 fprintf( stderr, usage, argv[0] );
50 return( EXIT_FAILURE );
53 case 'K': /* kerberos bind, part one only */
55 authmethod = LDAP_AUTH_KRBV41;
57 fprintf (stderr, "%s was not compiled with Kerberos support\n", argv[0]);
58 fprintf( stderr, usage, argv[0] );
59 return( EXIT_FAILURE );
62 case 'c': /* continuous operation mode */
65 case 'h': /* ldap host */
66 ldaphost = strdup( optarg );
68 case 'D': /* bind DN */
69 binddn = strdup( optarg );
71 case 'w': /* password */
72 passwd = strdup( optarg );
74 case 'f': /* read DNs from a file */
75 if (( fp = fopen( optarg, "r" )) == NULL ) {
81 debug |= atoi( optarg );
84 ldapport = atoi( optarg );
86 case 'n': /* print deletes, don't actually do them */
89 case 'v': /* verbose mode */
96 switch( atoi(optarg) )
99 version = LDAP_VERSION2;
102 version = LDAP_VERSION3;
105 fprintf( stderr, "protocol version should be 2 or 3\n" );
106 fprintf( stderr, usage, argv[0] );
107 return( EXIT_FAILURE );
111 fprintf( stderr, usage, argv[0] );
112 return( EXIT_FAILURE );
117 if ( optind >= argc ) {
123 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
124 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
126 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
127 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
132 (void) SIGNAL( SIGPIPE, SIG_IGN );
135 if (( ld = ldap_init( ldaphost, ldapport )) == NULL ) {
136 perror( "ldap_init" );
137 return( EXIT_FAILURE );
141 /* this seems prudent */
142 int deref = LDAP_DEREF_NEVER;
143 ldap_set_option( ld, LDAP_OPT_DEREF, &deref );
147 passwd = getpass("Enter LDAP Password: ");
150 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version ) != LDAP_OPT_SUCCESS)
152 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n", version );
155 if ( ldap_bind_s( ld, binddn, passwd, authmethod ) != LDAP_SUCCESS ) {
156 ldap_perror( ld, "ldap_bind" );
157 return( EXIT_FAILURE );
161 for ( ; optind < argc; ++optind ) {
162 rc = dodelete( ld, argv[ optind ] );
166 while ((rc == 0 || contoper) && fgets(buf, sizeof(buf), fp) != NULL) {
167 buf[ strlen( buf ) - 1 ] = '\0'; /* remove trailing newline */
168 if ( *buf != '\0' ) {
169 rc = dodelete( ld, buf );
187 printf( "%sdeleting entry \"%s\"\n",
188 (not ? "!" : ""), dn );
193 if (( rc = ldap_delete_s( ld, dn )) != LDAP_SUCCESS ) {
194 ldap_perror( ld, "ldap_delete" );
195 } else if ( verbose ) {
196 printf( "\tremoved\n" );