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]);
50 case 'K': /* kerberos bind, part one only */
52 authmethod = LDAP_AUTH_KRBV41;
54 fprintf (stderr, "%s was not compiled with Kerberos support\n", argv[0]);
57 case 'c': /* continuous operation mode */
60 case 'h': /* ldap host */
61 ldaphost = strdup( optarg );
63 case 'D': /* bind DN */
64 binddn = strdup( optarg );
66 case 'w': /* password */
67 passwd = strdup( optarg );
69 case 'f': /* read DNs from a file */
70 if (( fp = fopen( optarg, "r" )) == NULL ) {
76 debug |= atoi( optarg );
79 ldapport = atoi( optarg );
81 case 'n': /* print deletes, don't actually do them */
84 case 'v': /* verbose mode */
94 version = LDAP_VERSION2;
97 version = LDAP_VERSION3;
102 fprintf( stderr, usage, argv[0] );
108 if ( optind >= argc ) {
114 ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug );
115 ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug );
119 (void) SIGNAL( SIGPIPE, SIG_IGN );
122 if (( ld = ldap_init( ldaphost, ldapport )) == NULL ) {
123 perror( "ldap_init" );
128 /* this seems prudent */
129 int deref = LDAP_DEREF_NEVER;
130 ldap_set_option( ld, LDAP_OPT_DEREF, &deref );
134 passwd = getpass("Enter LDAP Password: ");
136 if( version != -1 ) {
137 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version );
140 if ( ldap_bind_s( ld, binddn, passwd, authmethod ) != LDAP_SUCCESS ) {
141 ldap_perror( ld, "ldap_bind" );
146 for ( ; optind < argc; ++optind ) {
147 rc = dodelete( ld, argv[ optind ] );
151 while ((rc == 0 || contoper) && fgets(buf, sizeof(buf), fp) != NULL) {
152 buf[ strlen( buf ) - 1 ] = '\0'; /* remove trailing newline */
153 if ( *buf != '\0' ) {
154 rc = dodelete( ld, buf );
175 printf( "%sdeleting entry \"%s\"\n",
176 (not ? "!" : ""), dn );
181 if (( rc = ldap_delete_s( ld, dn )) != LDAP_SUCCESS ) {
182 ldap_perror( ld, "ldap_delete" );
183 } else if ( verbose ) {
184 printf( "\tremoved\n" );