import java.security.Security;
import java.text.ParseException;
import java.util.Enumeration;
import java.util.Iterator;
import java.io.UnsupportedEncodingException;
import com.novell.ldap.*;
import com.novell.ldap.client.Debug;
import arguments.*;
public class GetDSE implements LDAPOIDs{
private int len = NamesAndOIDs.length;
public final int BIND_TIMEOUT = 20000;
public static void main( String[] args )
{
System.exit( new GetDSE().runDSE(args));
}
private int runDSE( String[] args)
{
String returnedAttributes[] = {"*", "+"};
String oid, value;
boolean attributeOnly = false;
LDAPConnection lc;
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);
}
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);
}
lc = new LDAPConnection();
try {
if( options.getPrintVerbose()) {
System.err.println("Connecting to host \"" + options.getHostPort() + "\"");
}
lc.connect(options.getHostPort(), 0);
if( (keyPath != null) && (type.equalsIgnoreCase("TLS")) ) {
lc.startTLS();
}
String loginDN = options.getLoginDN();
if( loginDN.length() != 0) {
LDAPConstraints cons = new LDAPConstraints();
cons.setTimeLimit( BIND_TIMEOUT);
lc.bind(3, loginDN,
options.getLoginPasswd().getBytes("UTF8"), cons);
}
LDAPSearchResults searchResults = lc.search(
"",
LDAPConnection.SCOPE_BASE,
"(objectclass=*)",
returnedAttributes,
attributeOnly );
LDAPEntry entry = null;
try {
entry = searchResults.next();
}
catch(LDAPException e) {
System.out.println("Error: " + e.toString());
}
System.out.println("\n" + entry.getDN());
System.out.println(" Attributes: ");
LDAPAttributeSet attributeSet = entry.getAttributeSet();
Iterator allAttributes = attributeSet.iterator();
while(allAttributes.hasNext()) {
LDAPAttribute attribute = (LDAPAttribute)allAttributes.next();
String attrName = attribute.getName();
System.out.println(" " + attrName);
Enumeration allValues = attribute.getStringValues();
if( allValues != null) {
while(allValues.hasMoreElements()) {
if ( (attrName.equalsIgnoreCase("supportedExtension"))
||(attrName.equalsIgnoreCase("supportedControl"))) {
oid = (String) allValues.nextElement();
System.out.print(" " + oid);
getOidInfo(oid);
}
else {
value = (String) allValues.nextElement();
System.out.println(" " + value);
}
}
}
}
}
catch( LDAPException e ) {
System.out.println( "Error: " + e.toString() );
}
catch( UnsupportedEncodingException e ) {
System.out.println( "Error: " + e.toString() );
}
try {
lc.disconnect();
}
catch( Exception e ) {
System.out.println( "Error: " + e.toString() );
System.exit(1);
}
return 0;
}
public void getOidInfo( String value ) {
String descriptiveName="";
for ( int i = 0; i < len; i++ ) {
if ( value.equalsIgnoreCase(NamesAndOIDs[i][OIDValueIndex]) ) {
descriptiveName = NamesAndOIDs[i][OIDDescrIndex];
if (descriptiveName.length() != 0) {
System.out.print( " (" + descriptiveName + ")\n" );
break;
}
else
System.out.println("");
}
}
if (descriptiveName.length() == 0) {
System.out.print("\n");
}
}
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
class Options
{
private ApplicationArguments options;
private Options()
throws Exception
{
super();
options = new ApplicationArguments("java GetDSE", 25);
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 to bind"
"",
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(
'p',
"port",
"host IP port number. See also the -h option",
LDAPConnection.DEFAULT_PORT,
Argument.SINGLE_VALUED));
options.add( new Argument(
'w',
"password",
"the password value used for authentication",
"",
Argument.SINGLE_VALUED));
options.add( new Argument(
'v',
"enable verbose output",
false));
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));
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;
}
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;
}
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 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 getPrintVerbose()
{
try {
return ((Boolean)
(options.getArgument('v').getValue())).booleanValue();
} catch( NoSuchFieldException e) {
throw new RuntimeException(e.toString());
}
}
boolean debug()
{
try {
return( options.hasArgument('d'));
} catch( NoSuchFieldException e) {
throw new RuntimeException(e.toString());
}
}
String usage( String msg)
{
return options.usage( msg);
}
}
}