import com.novell.ldap.LDAPAttribute;
import com.novell.ldap.LDAPAttributeSet;
import com.novell.ldap.LDAPAuthHandler;
import com.novell.ldap.LDAPAuthProvider;
import com.novell.ldap.LDAPBindHandler;
import com.novell.ldap.LDAPConnection;
import com.novell.ldap.LDAPControl;
import com.novell.ldap.LDAPEntry;
import com.novell.ldap.LDAPException;
import com.novell.ldap.LDAPJSSEStartTLSFactory;
import com.novell.ldap.LDAPJSSESecureSocketFactory;
import com.novell.ldap.LDAPReferralException;
import com.novell.ldap.LDAPReferralHandler;
import com.novell.ldap.LDAPSearchConstraints;
import com.novell.ldap.LDAPSearchResults;
import com.novell.ldap.LDAPSocketFactory;
import com.novell.ldap.LDAPUrl;
import com.novell.ldap.controls.LDAPPersistSearchControl;
import com.novell.ldap.controls.LDAPSortKey;
import com.novell.ldap.client.Debug;
import com.novell.ldap.util.LDAPWriter;
import com.novell.ldap.util.LDIFWriter;
import com.novell.ldap.util.DSMLWriter;
import arguments.*;
import java.io.BufferedReader;
import java.io.FileDescriptor;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.security.Security;
import java.util.Hashtable;
import java.util.Enumeration;
import java.text.ParseException;
public class SearchUtil extends Thread
{
public final int BIND_TIMEOUT = 20000;
public final int FORMAT_LDIF = 9;
public final int FORMAT_DSML = -9;
public final int FORMAT_TEXT = 0;
public final String DSAIT_OID = "2.16.840.1.113730.3.4.2";
public final String PSEARCH_OID = "2.16.840.1.113730.3.4.3";
public final String SORT_OID = "1.2.840.113556.1.4.473";
private LDAPSearchResults searchResults;
private LDAPConnection conn;
private SearchUtil()
{
return;
}
public
static void main( String[] args )
{
System.exit(new SearchUtil().runSearch( args));
}
private
int runSearch( String[] args)
{
int exfmt = FORMAT_TEXT;
LDAPWriter writer = null;
Options options = null;
try {
options = new Options();
options.parse(args);
} catch( ParseException e) {
String errorMessage = e.getMessage();
if( errorMessage == null) {
errorMessage = e.toString();
}
errorMessage = options.usage(errorMessage);
displayArguments( options, args);
System.err.println( errorMessage);
return 1;
} catch( Exception e) {
System.err.println("Exception: " + e.toString());
e.printStackTrace();
return 1;
}
if( options.debug()) {
Debug.setTraceStream( System.err);
Debug.setTrace( "TraceAll", true);
}
LDAPSearchConstraints cons = new LDAPSearchConstraints(
options.getTimeout(),
options.getServerTimeLimit(),
options.getAliasDereference(),
options.getMaxSearchResults(),
options.getReferralFollowing(),
options.getBatchSize(),
options.getReferralHandler(),
options.getReferralHopLimit());
String keyPath = null;
String type = options.getConnectionType();
if( (keyPath = options.getKeystore()) != null) {
LDAPSocketFactory ssf;
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
if( options.getPrintVerbose()) {
System.err.println("Java key store is \"" + keyPath + "\"");
}
System.setProperty("javax.net.ssl.trustStore", keyPath);
if( type.equalsIgnoreCase("TLS")) {
if( options.getPrintVerbose()) {
System.err.println("Setting factory for a TLS connection");
}
ssf = new LDAPJSSEStartTLSFactory();
} else {
if( options.getPrintVerbose()) {
System.err.println("Setting factory for a SSL connection");
}
ssf = new LDAPJSSESecureSocketFactory();
}
LDAPConnection.setSocketFactory(ssf);
}
conn = new LDAPConnection();
conn.setConstraints( cons );
int entryCount = 0;
try {
if( options.getPrintVerbose()) {
System.err.println("Connecting to host \"" + options.getHostPort() + "\"");
}
conn.connect(options.getHostPort(), 0);
if( (keyPath != null) && (type.equalsIgnoreCase("TLS")) ) {
conn.startTLS();
}
String loginDN = options.getLoginDN();
if( loginDN.length() != 0) {
cons.setTimeLimit( BIND_TIMEOUT);
conn.bind(3, loginDN,
options.getLoginPasswd().getBytes("UTF8"), cons);
}
cons = conn.getSearchConstraints();
cons.setControls( options.getControls());
conn.setConstraints( cons);
displaySearchConstraints( options, cons);
cons = null;
options.clearLoginDN();
options.clearLoginPassword();
displayAuthentication( options, conn);
displaySearchParameters( options);
if( options.getPersistentSearch()) {
Thread r = new Thread(this);
r.setDaemon(true);
r.start();
System.err.println("\nMonitoring changes. Enter a 'STOP' to quit: ");
}
searchResults = conn.search(
options.getSearchBase(),
options.getSearchScope(),
options.getSearchFilter(),
options.getAttributeNames(),
options.getTypesOnly());
FileOutputStream fos = new FileOutputStream(FileDescriptor.out);
exfmt = options.getExportFormat();
if( exfmt == FORMAT_LDIF) {
writer = new LDIFWriter(fos);
} else
if( exfmt == FORMAT_DSML) {
fos = new FileOutputStream(FileDescriptor.out);
writer = new DSMLWriter(fos);
((DSMLWriter)writer).setIndent(3);
((DSMLWriter)writer).useIndent(true);
}
LDAPEntry entry;
while ( searchResults.hasMore()) {
try {
entry = searchResults.next();
entryCount++;
}
catch(LDAPException e) {
displayException( options, e);
continue;
}
if( exfmt != FORMAT_TEXT) {
writer.writeEntry(entry);
continue;
}
if( options.getPrintDN() || options.getPrintAttrs()) {
String dn = entry.getDN();
if( dn.length() == 0) {
dn = "\"\"";
}
System.out.println("\n" + dn);
}
if( options.getPrintAttrs()) {
System.out.println("\tAttributes: ");
LDAPAttributeSet attributeSet = entry.getAttributeSet();
java.util.Iterator allAttributes = attributeSet.iterator();
while( allAttributes.hasNext()) {
LDAPAttribute attribute =
(LDAPAttribute)allAttributes.next();
String attributeName = attribute.getName();
System.out.println("\t\t" + attributeName);
Enumeration allValues = attribute.getStringValues();
if( allValues != null) {
while(allValues.hasMoreElements()) {
String Value = (String) allValues.nextElement();
System.out.println("\t\t\t" + Value);
}
}
}
}
}
} catch( LDAPException e ) {
displayException( options, e);
} catch( Exception ex) {
displayException( options, ex);
} finally {
if( exfmt != FORMAT_TEXT) {
try {
writer.finish();
} catch( Exception ex) {
;
}
}
}
System.err.println( entryCount + " Entries found");
if ( conn.isConnected() ) {
try {
conn.disconnect();
} catch ( LDAPException e ) {
System.err.println( "Disconnect Error: " + e.toString() );
}
}
return 0;
}
public void run()
{
BufferedReader in
= new BufferedReader(new InputStreamReader(System.in));
try {
String input;
while (true) {
input = in.readLine();
if ( ! input.equalsIgnoreCase("STOP")) {
System.err.println(
"\nSearchUtil: input ignored during persistent search: "
+ input);
continue;
}
break;
}
}
catch (Exception e) {
System.out.println(e.getMessage());
}
System.err.println("\nSearchUtil: stopping persistent search");
try {
if( searchResults == null) {
conn.disconnect();
} else {
conn.abandon(searchResults);
}
}
catch( LDAPException e ) {
System.out.println();
System.out.println( "Error: " + e.toString() );
}
return;
}
private class AuthImpl implements LDAPAuthHandler
{
private String dn;
private byte[] passwd;
private boolean printVerbose;
private AuthImpl( Options options)
{
dn = options.getLoginDN();
printVerbose = options.getPrintVerbose();
try {
passwd = options.getLoginPasswd().getBytes("UTF8");
} catch ( UnsupportedEncodingException ex) {
System.out.println( "Unable to encode password to UTF-8: " +
ex.toString());
System.exit(1);
}
return;
}
public LDAPAuthProvider getAuthProvider( String host, int port)
{
if( printVerbose) {
System.err.println("\nReBind to host " + host + ":" + port);
}
return new LDAPAuthProvider( dn, passwd);
}
}
private class BindImpl implements LDAPBindHandler
{
private Hashtable connections;
private String dn;
private byte[] passwd;
private boolean printVerbose;
private BindImpl( Options options)
{
connections = new Hashtable();
dn = options.getLoginDN();
printVerbose = options.getPrintVerbose();
try {
passwd = options.getLoginPasswd().getBytes("UTF8");
} catch ( UnsupportedEncodingException ex) {
System.out.println( "Unable to encode password to UTF-8: " +
ex.toString());
System.exit(1);
}
return;
}
public LDAPConnection bind( String[] urls, LDAPConnection conn)
throws LDAPReferralException
{
LDAPReferralException ex = null;
LDAPConnection newconn = null;
LDAPUrl url;
String host;
host = conn.getHost() + ":" + conn.getPort();
if( (LDAPConnection)connections.get( host) == null) {
connections.put( host, conn);
}
if( printVerbose) {
System.err.println("\nBind: Referral contains " + urls.length +
" URLs");
for( int i = 0; i < urls.length; i++) {
System.err.println(" " + urls[i]);
}
System.err.println("Bind: Original host is " + host);
}
for( int i = 0; i < urls.length; i++) {
try {
url = new LDAPUrl( urls[i]);
host = url.getHost() + ":" + conn.getPort();
if((newconn=(LDAPConnection)connections.get( host))==null) {
continue;
} else
if( printVerbose) {
System.err.println(
"Bind: Using existing connection to host " + host);
}
ex = null;
break;
} catch( Throwable e) {
newconn = null;
}
}
if( newconn == null) {
newconn = new LDAPConnection( conn.getSocketFactory());
LDAPSearchConstraints cons = conn.getSearchConstraints();
newconn.setConstraints( cons);
cons.setReferralFollowing(false);
cons.setTimeLimit(BIND_TIMEOUT);
for( int i = 0; i < urls.length; i++) {
try {
url = new LDAPUrl( urls[i]);
} catch( MalformedURLException e) {
continue;
}
host = url.getHost() + ":" + url.getPort();
try {
newconn.connect( host,0);
if( conn.isTLS()) {
newconn.startTLS();
}
if( conn.isBound()) {
String method = conn.getAuthenticationMethod();
if( method.equals("simple")) {
newconn.bind( conn.getProtocolVersion(),
dn, passwd, cons);
} else
if( method.equals("sasl")) {
throw new LDAPException( "Bind: " + method +
" not implemented",
LDAPException.AUTH_UNKNOWN,(String)null);
} else {
throw new LDAPException( "Bind: " + method +
" unknown",
LDAPException.AUTH_UNKNOWN,(String)null);
}
if( printVerbose) {
System.err.println(
"Bind: Successful " + method +
" Bind to host " + host);
}
} else {
if( printVerbose) {
System.err.println(
"Bind: Anonymous connection to host " + host);
}
}
connections.put( host, newconn);
ex = null;
break;
} catch( Throwable e) {
try {
newconn.disconnect();
} catch( LDAPException dex) {
;
}
System.err.println(
"Bind: Exception on Bind to host " +
host + " - " + e.toString());
if( e instanceof LDAPReferralException) {
ex = (LDAPReferralException)e;
} else {
ex = new LDAPReferralException(
"Bind: Could not follow referrals", e);
ex.setFailedReferral(urls[i]);
if( printVerbose) {
if( ! (e instanceof LDAPException)) {
e.printStackTrace();
}
}
}
}
}
}
if( ex != null) {
newconn = null;
if( printVerbose) {
System.err.println(
"Bind: Throwing LDAPReferralException " + ex.toString());
}
throw ex;
}
return newconn;
}
}
private
void displayArguments( Options options, String[] args)
{
if( options.getPrintVerbose()) {
System.err.println("\nCommand line arguments");
for( int i=0; i < args.length; i++) {
System.err.println(" " + i + ": " + args[i]);
}
}
return;
}
private
void displaySearchConstraints( Options options, LDAPSearchConstraints cons)
{
if( options.getPrintVerbose()) {
System.err.println("Constraints Values");
System.err.print(" Search alias dereferencing is ");
switch( cons.getDereference())
{
case LDAPSearchConstraints.DEREF_NEVER:
System.err.println("DEREF_NEVER");
break;
case LDAPSearchConstraints.DEREF_FINDING:
System.err.println("DEREF_FINDING");
break;
case LDAPSearchConstraints.DEREF_SEARCHING:
System.err.println("DEREF_SEARCHING");
break;
case LDAPSearchConstraints.DEREF_ALWAYS:
System.err.println("DEREF_ALWAYS");
break;
}
System.err.println(" Search batch size is " +
cons.getBatchSize());
System.err.println(" Search max results is " +
cons.getMaxResults());
System.err.println(" Search server time limit is " +
cons.getServerTimeLimit() + " seconds");
System.err.println(" API request time limit is " +
cons.getTimeLimit() + " milliseconds");
System.err.println(" Referral following is " +
cons.getReferralFollowing());
if( cons.getReferralFollowing()) {
LDAPReferralHandler rh = options.getReferralHandler();
if( rh == null) {
System.err.println(" Referral handler not set");
} else {
System.err.print(" Referral handler is ");
if( rh instanceof LDAPBindHandler) {
System.err.println("LDAPBindHandler");
} else {
System.err.println("LDAPAuthHandler");
}
System.err.println(" Referral hop limit set to " +
cons.getHopLimit());
}
}
if( cons.getControls() != null) {
LDAPControl[] controls = cons.getControls();
StringBuffer msg = new StringBuffer("Controls:\n");
for(int i=0; i < controls.length; i++) {
String oid = controls[i].getID();
if( oid.equals(DSAIT_OID)) {
msg.append( " ManageDsaIT\n");
} else
if( oid.equals(PSEARCH_OID)) {
msg.append( " Persistent Search\n");
} else {
msg.append( " " + controls[i].getID() + "\n");
}
}
System.err.print(msg.toString());
}
}
return;
}
private
void displayAuthentication( Options options, LDAPConnection conn)
{
if( options.getPrintVerbose()) {
if( conn.isBound()) {
System.err.println(
"Application is authenticated to server as " +
conn.getAuthenticationDN());
} else {
System.err.println(
"Application authentication is anonymous");
}
}
return;
}
private
void displaySearchParameters( Options options)
{
if( options.getPrintVerbose()) {
System.err.println("Search Parameters");
System.err.println(" Search Base: \"" +
options.getSearchBase() + "\"");
System.err.print(" Search Scope: ");
int scope = options.getSearchScope();
switch( scope) {
case LDAPConnection.SCOPE_SUB:
System.err.println("SUB_TREE");
break;
case LDAPConnection.SCOPE_BASE:
System.err.println("BASE");
break;
case LDAPConnection.SCOPE_ONE:
System.err.println("ONE_LEVEL");
break;
case LDAPConnection.SCOPE_SUBORDINATESUBTREE:
System.err.println("SUBORDINATE_SUBTREE");
break;
}
System.err.println(" Search Filter: \"" +
options.getSearchFilter() + "\"");
String[] attrs = options.getAttributeNames();
if( attrs.length == 1) {
System.err.println(" Attribute Name: \"" +
attrs[0] + "\"");
} else {
System.err.println(" Attribute Names:");
for( int i = 0; i < attrs.length; i++) {
System.err.println(" \"" + attrs[i] + "\"");
}
}
System.err.println(" Typesonly: " + options.getTypesOnly());
}
return;
}
private
void displayException( Options options, Exception ex)
{
if( options.debug()) {
ex.printStackTrace();
} else
if( ex instanceof RuntimeException) {
ex.printStackTrace();
System.exit(1);
} else {
System.err.println("\n" + ex.toString());
}
return;
}
private
class Options
{
private ApplicationArguments options;
private Options()
throws Exception
{
super();
options = new ApplicationArguments("java SearchUtil", 25);
options.add( new Argument(
'a',
"alias",
"Alias dereferencing\n" +
" NEVER - do not dereference aliases in \n" +
" searching or in locating the base\n" +
" object of the search\n" +
" SEARCHING - dereference aliases in subordinates\n" +
" of the base object in searching, \n" +
" but not in locating the base\n" +
" object of the search\n" +
" FINDING - dereference aliases in locating the\n" +
" base object of the search, but not\n" +
" when searching subordinates of the\n" +
" base object\n" +
" ALWAYS - dereference aliases both in\n" +
" searching and in locating the base\n" +
" object of the search.",
"NEVER",
Argument.SINGLE_VALUED));
options.add( new Argument(
'A',
"retrieve attribute names only (no values)",
false));
options.add( new Argument(
'b',
"baseDN",
"the base DN for the search",
"",
Argument.SINGLE_VALUED));
options.add( new Argument(
'C',
"referralHandling",
"Follow refeerals, OFF | ON | AUTH | BIND\n" +
" OFF - Referral following turned off\n" +
" NO - Follow with NO referral handler (anonymous)\n" +
" AUTH - Follow with AUTH referral handler\n" +
" BIND - Follow with BIND referral handler",
"OFF",
Argument.SINGLE_VALUED));
options.add( new Argument(
'd',
"enable API debug output",
Argument.SINGLE_VALUED));
options.add( new Argument(
'D',
"bind DN",
"the DN of the object used for authentication"
"",
Argument.SINGLE_VALUED));
options.add( new Argument(
'e',
"keystore",
"Path to a Java Keystore. A valid certificate in the keystore enables\n" +
" an encrypted TLS connection. See also the -Z option.",
"",
Argument.SINGLE_VALUED));
options.add( new Argument(
'h',
"host",
"host name or IP address. A port can be specified with the\n" +
" host name as hostname:port, i.e. myhost:389. See also \n" +
" the -p option",
"localhost",
Argument.SINGLE_VALUED));
options.add( new Argument(
'l',
"server timeout",
"server time limit in seconds to complete the search\n" +
" 0 = no limit",
0,
Argument.SINGLE_VALUED));
options.add( new Argument(
'L',
"export format",
"Export format - LDIF|DSML\n" +
" DSML - output in DSML V2.0 format\n" +
" LDIF - output in LDIF V1.0 format\n" +
" TEXT - output in descriptive text formt\n" +
" Note: selecting DSML or LDIF forces the\n" +
" -a and -c options to be set",
"TEXT",
Argument.SINGLE_VALUED));
options.add( new Argument(
'm',
"max results",
"maximum number of entries for server to return",
1000,
Argument.SINGLE_VALUED));
options.add( new Argument(
'M',
"control",
"a control to include with the search. Any control specified\n" +
" will be sent as critical, i.e., the operation will fail\n" +
" if the server is unwilling to perform the control with the\n" +
" search operation.\n" +
" ManageDsaIT - Causes Directory-specific entries,\n" +
" regardless of type, to be treated as\n" +
" normal entries\n" +
" PSearch - A simple change notification mechanism.\n" +
" When set, changed entries are returned\n" +
" until the application is interrupted,\n" +
" or STOP is entered from the standard input\n" +
" stream\n" +
" You specify multiple controls by preceding each control with\n" +
" the -M option. A \"+\" sign on the front of the control name\n" +
" designates it as critical, i.e. \"-M +Sort\". Psearch is\n" +
" is always considered critical.",
null,
Argument.MULTI_VALUED));
options.add( new Argument(
'p',
"port",
"host IP port number. See also the -h option",
LDAPConnection.DEFAULT_PORT,
Argument.SINGLE_VALUED));
options.add( new Argument(
'q',
"queue size",
"number of entries to queue before the search\n" +
" results are displayed",
1,
Argument.SINGLE_VALUED));
options.add( new Argument(
'r',
"referral hop limit",
"the maximum number of referral hops allowed before\n" +
" the operation fails",
10,
Argument.SINGLE_VALUED));
options.add( new Argument(
's',
"scope",
"Search Scope - BASE|ONE|SUB\n" +
" BASE - Base level search\n" +
" ONE - One level search\n" +
" SUB - Subtree search",
"ONE",
Argument.SINGLE_VALUED));
options.add( new Argument(
't',
"client timeout",
"client message timeout in milliseconds\n" +
" 0 = no timeout",
0,
Argument.SINGLE_VALUED));
options.add( new Argument(
'v',
"enable verbose output",
false));
options.add( new Argument(
'w',
"password",
"the password value used for authentication",
"",
Argument.SINGLE_VALUED));
options.add( new Argument(
'Z',
"encrypted connection",
"sets the type of encrypted connection. A Keystore must be specified\n" +
" with the -e option to enable an encrypted connection.\n" +
" SSL - Establishes an encrypted connection using \n" +
" SSL. The default port is 636\n" +
" TLS - Establishes an encrypted connection using \n" +
" TLS. The default port is 389",
"TLS",
Argument.SINGLE_VALUED));
options.add( new Argument(
"filter",
"an LDAP search filter",
"(objectclass=*)",
Argument.SINGLE_VALUED));
options.add( new Argument(
"attributes",
"only the the named attributes will be displayed in the results.\n" +
" You specify multiple attributes by separating each attribute\n" +
" description with a space. An attribute description may include:\n" +
" 1.1 - no attributes\n" +
" * - all user attributes\n" +
" + - all informational attribute\n" +
" Note: You must specify a filter if you specify any attribute\n" +
" descriptions",
"*",
Argument.MULTI_VALUED));
return;
}
void parse( String[] args)
throws Exception
{
options.parse(args);
return;
}
String getHostPort()
{
String host;
int port;
try {
host=(String)(options.getArgument('h').getValue());
Argument arg = options.getArgument('p');
int cnt = arg.getValueCount();
if( cnt == 0) {
port=((Integer)arg.getValue()).intValue();
String type = getConnectionType();
if( type.equalsIgnoreCase("SSL")) {
port = LDAPConnection.DEFAULT_SSL_PORT;
}
} else {
port=((Integer)arg.getValue()).intValue();
}
} catch( NoSuchFieldException e) {
throw new RuntimeException(e.toString());
}
if( host.indexOf(":") == -1) {
host = host + ":" + port;
}
return host;
}
int getTimeout()
{
try {
return ((Integer)
(options.getArgument('t').getValue())).intValue();
} catch( NoSuchFieldException e) {
throw new RuntimeException(e.toString());
}
}
String getSearchBase()
{
try {
return (String)(options.getArgument('b').getValue());
} catch( NoSuchFieldException e) {
throw new RuntimeException(e.toString());
}
}
boolean getTypesOnly()
{
return (getPrintAttrs() == false) &&
(getAttrValues() == false) &&
(getExportFormat() == 0);
}
String getSearchFilter()
{
try {
return (String)(options.getArgument(1).getValue());
} catch( NoSuchFieldException e) {
throw new RuntimeException(e.toString());
}
}
String getConnectionType()
{
String type = null;
try {
type = (String)(options.getArgument('Z').getValue());
if( type.equalsIgnoreCase("SSL") ) {
;
} else
if( type.equalsIgnoreCase("TLS") ) {
;
} else {
throw new NoSuchFieldException(
"Invalid connection type specified: " + type);
}
} catch( NoSuchFieldException e) {
throw new RuntimeException(e.toString());
}
return type;
}
boolean getPersistentSearch()
{
try {
Argument arg = options.getArgument('M');
Enumeration e = arg.getValues();
while( e.hasMoreElements()) {
if( "PSearch".equalsIgnoreCase((String)e.nextElement())) {
return true;
}
}
return false;
} catch( NoSuchFieldException e) {
throw new RuntimeException(e.toString());
}
}
LDAPControl[] getControls()
{
try {
Argument arg = options.getArgument('M');
Enumeration e = arg.getValues();
int size = arg.getValueCount();
if( size == 0) {
return null;
}
LDAPControl[] controls = new LDAPControl[size];
int i = 0;
while( e.hasMoreElements()) {
String name = (String)e.nextElement();
if( name.equalsIgnoreCase("ManageDsaIT") ) {
controls[i++] = new LDAPControl( DSAIT_OID, false, null);
} else
if( name.equalsIgnoreCase("+ManageDsaIT") ) {
controls[i++] = new LDAPControl( DSAIT_OID, true, null);
} else
if( name.equalsIgnoreCase("PSearch") ) {
controls[i++] = new LDAPPersistSearchControl();
} else
if( name.equalsIgnoreCase("+PSearch") ) {
controls[i++] = new LDAPPersistSearchControl();
} else {
throw new NoSuchFieldException(
"Invalid Control specified: " + name);
}
}
return controls;
} catch( NoSuchFieldException e) {
throw new RuntimeException(e.toString());
}
}
String getKeystore()
{
try {
Argument arg = options.getArgument('e');
int size = arg.getValueCount();
if( size == 0)
return null;
return (String)arg.getValue();
} catch( NoSuchFieldException e) {
throw new RuntimeException(e.toString());
}
}
String[] getAttributeNames()
{
try {
String[] attrs;
Argument arg;
arg = options.getArgument(2);
int cnt = arg.getValueCount();
if( cnt == 0) {
cnt =1;
}
attrs = new String[cnt];
Enumeration values = arg.getValues();
int i=0;
while(values.hasMoreElements()) {
attrs[i++] = (String)values.nextElement();
}
return attrs;
} catch( NoSuchFieldException e) {
throw new RuntimeException(e.toString());
}
}
int getSearchScope()
{
int sscope = 0;
try {
String scope = (String)(options.getArgument('s').getValue());
if( scope.equalsIgnoreCase("SUB") ) {
sscope = LDAPConnection.SCOPE_SUB;
} else
if( scope.equalsIgnoreCase("BASE") ) {
sscope = LDAPConnection.SCOPE_BASE;
} else
if( scope.equalsIgnoreCase("ONE") ) {
sscope = LDAPConnection.SCOPE_ONE;
} else {
throw new NoSuchFieldException("Invalid Search Scope Argument");
}
} catch( NoSuchFieldException e) {
throw new RuntimeException(e.toString());
}
return sscope;
}
int getExportFormat()
{
int fmt = FORMAT_TEXT;
try {
Argument arg = options.getArgument('L');
if( arg.getValueCount() == 0) {
return fmt;
}
String format = (String)(arg.getValue());
if( format.equalsIgnoreCase("DSML") ) {
fmt = FORMAT_DSML;
} else
if( format.equalsIgnoreCase("LDIF") ) {
fmt = FORMAT_LDIF;
} else
if( format.equalsIgnoreCase("TEXT") ) {
fmt = FORMAT_TEXT;
} else {
throw new NoSuchFieldException("Invalid Export Format Argument");
}
} catch( NoSuchFieldException e) {
throw new RuntimeException(e.toString());
}
return fmt;
}
LDAPReferralHandler getReferralHandler( )
{
LDAPReferralHandler rh = null;
try {
String referString = (String)
(options.getArgument('C').getValue());
if( referString.equalsIgnoreCase("NO") ) {
;
} else
if( referString.equalsIgnoreCase("ON") ) {
;
} else
if( referString.equalsIgnoreCase("BIND") ) {
rh = new BindImpl( this);
} else
if( referString.equalsIgnoreCase("AUTH") ) {
rh = new AuthImpl( this);
} else
if( referString.equalsIgnoreCase("REBIND") ) {
rh = new AuthImpl( this);
} else
if( referString.equalsIgnoreCase("OFF") ) {
;
} else {
throw new NoSuchFieldException(
"Invalid Referral Following Argument: \"" +
referString + "\"");
}
} catch( NoSuchFieldException e) {
throw new RuntimeException(e.toString());
}
return rh;
}
boolean getReferralFollowing( )
{
boolean follow = false;
try {
String referString = (String)
(options.getArgument('C').getValue());
if( referString.equalsIgnoreCase("ON") ) {
follow = true;
} else
if( referString.equalsIgnoreCase("BIND") ) {
follow = true;
} else
if( referString.equalsIgnoreCase("AUTH") ) {
follow = true;
} else
if( referString.equalsIgnoreCase("REBIND") ) {
follow = true;
} else
if( referString.equalsIgnoreCase("OFF") ) {
;
} else {
throw new NoSuchFieldException(
"Invalid Referral Following Argument: \"" +
referString + "\"");
}
} catch( NoSuchFieldException e) {
throw new RuntimeException(e.toString());
}
return follow;
}
int getAliasDereference( )
{
int val = 0;
try {
String deref = (String)(options.getArgument('a').getValue());
if( deref.equalsIgnoreCase("NEVER")) {
val = LDAPSearchConstraints.DEREF_NEVER;
} else
if( deref.equalsIgnoreCase("FINDING")) {
val = LDAPSearchConstraints.DEREF_FINDING;
} else
if( deref.equalsIgnoreCase("SEARCHING")) {
val = LDAPSearchConstraints.DEREF_SEARCHING;
} else
if( deref.equalsIgnoreCase("ALWAYS")) {
val = LDAPSearchConstraints.DEREF_ALWAYS;
} else {
throw new NoSuchFieldException(
"Invalid Alias Dereference Argument: \"" +
deref + "\"");
}
} catch( NoSuchFieldException e) {
throw new RuntimeException(e.toString());
}
return val;
}
int getBatchSize()
{
try {
return ((Integer)
(options.getArgument('q').getValue())).intValue();
} catch( NoSuchFieldException e) {
throw new RuntimeException(e.toString());
}
}
int getReferralHopLimit()
{
try {
return ((Integer)
(options.getArgument('r').getValue())).intValue();
} catch( NoSuchFieldException e) {
throw new RuntimeException(e.toString());
}
}
int getMaxSearchResults()
{
try {
return ((Integer)
(options.getArgument('m').getValue())).intValue();
} catch( NoSuchFieldException e) {
throw new RuntimeException(e.toString());
}
}
int getServerTimeLimit()
{
try {
return ((Integer)
(options.getArgument('l').getValue())).intValue();
} catch( NoSuchFieldException e) {
throw new RuntimeException(e.toString());
}
}
String getLoginDN()
{
try {
return (String)(options.getArgument('D').getValue());
} catch( NoSuchFieldException e) {
throw new RuntimeException(e.toString());
}
}
void clearLoginDN()
{
try {
options.getArgument('D').clearValues();
return;
} catch( NoSuchFieldException e) {
throw new RuntimeException(e.toString());
}
}
String getLoginPasswd()
{
try {
return (String)(options.getArgument('w').getValue());
} catch( NoSuchFieldException e) {
throw new RuntimeException(e.toString());
}
}
void clearLoginPassword()
{
try {
options.getArgument('w').clearValues();
return;
} catch( NoSuchFieldException e) {
throw new RuntimeException(e.toString());
}
}
boolean debug()
{
try {
return ((Boolean)
(options.getArgument('d').getValue())).booleanValue();
} catch( NoSuchFieldException e) {
throw new RuntimeException(e.toString());
}
}
boolean getPrintAttrs()
{
return getAttrValues();
}
boolean getAttrValues()
{
try {
return ! ((Boolean)
(options.getArgument('A').getValue())).booleanValue();
} catch( NoSuchFieldException e) {
throw new RuntimeException(e.toString());
}
}
boolean getPrintDN()
{
return true;
}
boolean getPrintVerbose()
{
try {
return ((Boolean)
(options.getArgument('v').getValue())).booleanValue();
} catch( NoSuchFieldException e) {
throw new RuntimeException(e.toString());
}
}
String usage( String msg)
{
return options.usage( msg);
}
}
}