1 /* ldapdelete.c - simple program to delete an entry using LDAP */
10 #include <ac/string.h>
11 #include <ac/unistd.h>
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 static int dodelete LDAP_P((
29 main( int argc, char **argv )
31 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";
34 int i, rc, authmethod, want_bindpw, version, debug;
36 not = verbose = contoper = want_bindpw = debug = 0;
38 authmethod = LDAP_AUTH_SIMPLE;
41 while (( i = getopt( argc, argv, "WnvkKch:P:p:D:w:d:f:" )) != EOF ) {
43 case 'k': /* kerberos bind */
45 authmethod = LDAP_AUTH_KRBV4;
47 fprintf (stderr, "%s was not compiled with Kerberos support\n", argv[0]);
48 fprintf( stderr, usage, argv[0] );
49 return( EXIT_FAILURE );
52 case 'K': /* kerberos bind, part one only */
54 authmethod = LDAP_AUTH_KRBV41;
56 fprintf (stderr, "%s was not compiled with Kerberos support\n", argv[0]);
57 fprintf( stderr, usage, argv[0] );
58 return( EXIT_FAILURE );
61 case 'c': /* continuous operation mode */
64 case 'h': /* ldap host */
65 ldaphost = strdup( optarg );
67 case 'D': /* bind DN */
68 binddn = strdup( optarg );
70 case 'w': /* password */
71 passwd = strdup( optarg );
73 case 'f': /* read DNs from a file */
74 if (( fp = fopen( optarg, "r" )) == NULL ) {
80 debug |= atoi( optarg );
83 ldapport = atoi( optarg );
85 case 'n': /* print deletes, don't actually do them */
88 case 'v': /* verbose mode */
95 switch( atoi(optarg) )
98 version = LDAP_VERSION2;
101 version = LDAP_VERSION3;
104 fprintf( stderr, "protocol version should be 2 or 3\n" );
105 fprintf( stderr, usage, argv[0] );
106 return( EXIT_FAILURE );
110 fprintf( stderr, usage, argv[0] );
111 return( EXIT_FAILURE );
116 if ( optind >= argc ) {
122 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_ERROR ) {
123 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
125 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_ERROR ) {
126 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
131 (void) SIGNAL( SIGPIPE, SIG_IGN );
134 if (( ld = ldap_init( ldaphost, ldapport )) == NULL ) {
135 perror( "ldap_init" );
136 return( EXIT_FAILURE );
140 /* this seems prudent */
141 int deref = LDAP_DEREF_NEVER;
142 ldap_set_option( ld, LDAP_OPT_DEREF, &deref );
146 passwd = getpass("Enter LDAP Password: ");
149 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version ) == LDAP_OPT_ERROR)
151 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n", version );
154 if ( ldap_bind_s( ld, binddn, passwd, authmethod ) != LDAP_SUCCESS ) {
155 ldap_perror( ld, "ldap_bind" );
156 return( EXIT_FAILURE );
160 for ( ; optind < argc; ++optind ) {
161 rc = dodelete( ld, argv[ optind ] );
165 while ((rc == 0 || contoper) && fgets(buf, sizeof(buf), fp) != NULL) {
166 buf[ strlen( buf ) - 1 ] = '\0'; /* remove trailing newline */
167 if ( *buf != '\0' ) {
168 rc = dodelete( ld, buf );
186 printf( "%sdeleting entry \"%s\"\n",
187 (not ? "!" : ""), dn );
192 if (( rc = ldap_delete_s( ld, dn )) != LDAP_SUCCESS ) {
193 ldap_perror( ld, "ldap_delete" );
194 } else if ( verbose ) {
195 printf( "\tremoved\n" );