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 #define safe_realloc( ptr, size ) ( ptr == NULL ? malloc( size ) : \
27 static int dodelete LDAP_P((
32 main( int argc, char **argv )
34 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";
37 int i, rc, authmethod, want_bindpw, version, debug;
39 not = verbose = contoper = want_bindpw = debug = 0;
41 authmethod = LDAP_AUTH_SIMPLE;
44 while (( i = getopt( argc, argv, "WnvkKch:P:p:D:w:d:f:" )) != EOF ) {
46 case 'k': /* kerberos bind */
48 authmethod = LDAP_AUTH_KRBV4;
50 fprintf (stderr, "%s was not compiled with Kerberos support\n", argv[0]);
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]);
60 case 'c': /* continuous operation mode */
63 case 'h': /* ldap host */
64 ldaphost = strdup( optarg );
66 case 'D': /* bind DN */
67 binddn = strdup( optarg );
69 case 'w': /* password */
70 passwd = strdup( optarg );
72 case 'f': /* read DNs from a file */
73 if (( fp = fopen( optarg, "r" )) == NULL ) {
79 debug |= atoi( optarg );
82 ldapport = atoi( optarg );
84 case 'n': /* print deletes, don't actually do them */
87 case 'v': /* verbose mode */
97 version = LDAP_VERSION2;
100 version = LDAP_VERSION3;
105 fprintf( stderr, usage, argv[0] );
111 if ( optind >= argc ) {
117 lber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug );
118 ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug );
122 (void) SIGNAL( SIGPIPE, SIG_IGN );
125 if (( ld = ldap_open( ldaphost, ldapport )) == NULL ) {
126 perror( "ldap_open" );
131 /* this seems prudent */
132 int deref = LDAP_DEREF_NEVER;
133 ldap_set_option( ld, LDAP_OPT_DEREF, &deref );
137 passwd = getpass("Enter LDAP Password: ");
139 if( version != -1 ) {
140 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version );
143 if ( ldap_bind_s( ld, binddn, passwd, authmethod ) != LDAP_SUCCESS ) {
144 ldap_perror( ld, "ldap_bind" );
149 for ( ; optind < argc; ++optind ) {
150 rc = dodelete( ld, argv[ optind ] );
154 while ((rc == 0 || contoper) && fgets(buf, sizeof(buf), fp) != NULL) {
155 buf[ strlen( buf ) - 1 ] = '\0'; /* remove trailing newline */
156 if ( *buf != '\0' ) {
157 rc = dodelete( ld, buf );
178 printf( "%sdeleting entry \"%s\"\n",
179 (not ? "!" : ""), dn );
184 if (( rc = ldap_delete_s( ld, dn )) != LDAP_SUCCESS ) {
185 ldap_perror( ld, "ldap_delete" );
186 } else if ( verbose ) {
187 printf( "\tremoved\n" );