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 );
76 for( p = optarg; *p == '\0'; p++ ) {
81 case 'f': /* read DNs from a file */
82 if (( fp = fopen( optarg, "r" )) == NULL ) {
88 debug |= atoi( optarg );
91 ldapport = atoi( optarg );
93 case 'n': /* print deletes, don't actually do them */
96 case 'v': /* verbose mode */
103 switch( atoi(optarg) )
106 version = LDAP_VERSION2;
109 version = LDAP_VERSION3;
112 fprintf( stderr, "protocol version should be 2 or 3\n" );
113 fprintf( stderr, usage, argv[0] );
114 return( EXIT_FAILURE );
118 fprintf( stderr, usage, argv[0] );
119 return( EXIT_FAILURE );
124 if ( optind >= argc ) {
130 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
131 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
133 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
134 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
139 (void) SIGNAL( SIGPIPE, SIG_IGN );
142 if (( ld = ldap_init( ldaphost, ldapport )) == NULL ) {
143 perror( "ldap_init" );
144 return( EXIT_FAILURE );
148 /* this seems prudent */
149 int deref = LDAP_DEREF_NEVER;
150 ldap_set_option( ld, LDAP_OPT_DEREF, &deref );
154 passwd = getpass("Enter LDAP Password: ");
157 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version ) != LDAP_OPT_SUCCESS)
159 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n", version );
162 if ( ldap_bind_s( ld, binddn, passwd, authmethod ) != LDAP_SUCCESS ) {
163 ldap_perror( ld, "ldap_bind" );
164 return( EXIT_FAILURE );
168 for ( ; optind < argc; ++optind ) {
169 rc = dodelete( ld, argv[ optind ] );
173 while ((rc == 0 || contoper) && fgets(buf, sizeof(buf), fp) != NULL) {
174 buf[ strlen( buf ) - 1 ] = '\0'; /* remove trailing newline */
175 if ( *buf != '\0' ) {
176 rc = dodelete( ld, buf );
194 printf( "%sdeleting entry \"%s\"\n",
195 (not ? "!" : ""), dn );
200 if (( rc = ldap_delete_s( ld, dn )) != LDAP_SUCCESS ) {
201 ldap_perror( ld, "ldap_delete" );
202 } else if ( verbose ) {
203 printf( "\tremoved\n" );