package org.openslx.satellitedaemon;
import java.math.BigInteger;
import java.security.NoSuchAlgorithmException;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.util.Random;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;
import org.openslx.satellitedaemon.filetransfer.FileDownloadWorker;
import org.openslx.satellitedaemon.filetransfer.FileUploadWorker;
/***********************************************************************************************/
/**
* Main class for uploading images from the HS-Server to the Satellite Server.
*
*/
public class App
{
private static Logger log = Logger.getLogger( App.class );
public static void main( String[] args ) throws NoSuchAlgorithmException
{
BasicConfigurator.configure();
int i = 0;
String arg;
String organizationName;
BigInteger modulus, privExp, pubExp;
String ipAddress;
// Check if there are arguments available and if they start with "--".
if ( i < args.length && args[i].startsWith( "--" ) ) {
// Arguments available, take the first one.
arg = args[i++];
if ( arg.equals( "--checkconfig" ) ) {
if ( checkConfig() ) {
System.exit( 0 );
}
log.error( "Config not valid: existing modulus, private and public exponent no valid key pair." );
System.exit( 2 );
} else if ( arg.equals( "--genid" ) ) {
if ( i < args.length ) {
organizationName = args[i++];
if ( genId( organizationName ) ) {
System.exit( 0 );
}
else
System.exit( 2 );
} else {
log.error( "Illegal option: --genid requires <organizationName>" );
System.exit( 2 );
}
} else if ( arg.equals( "--import" ) ) {
if ( args.length != 5 ) {
log.error( "Illegal option: '--import' requires 4 arguments, <OrgName> <Modulus> <priv_Exponent> <pub_Exponent>" );
System.exit( 2 );
} else {
organizationName = args[i++];
modulus = new BigInteger( args[i++] );
privExp = new BigInteger( args[i++] );
pubExp = new BigInteger( args[i++] );
if ( importId( organizationName, modulus, privExp, pubExp ) ) {
System.exit( 0 );
} else
System.exit( 2 );
}
} else if ( arg.equals( "--submitkey" ) ) {
if ( i < args.length ) {
ipAddress = args[i++];
if ( submitKey( ipAddress ) )
System.exit( 0 );
else
System.exit( 2 );
} else {
log.error( "Illegal option: --submitkey requires <IPADDRESS>" );
System.exit( 2 );
}
} else if ( arg.equals( "--updateaddress" ) ) {
if ( i < args.length ) {
ipAddress = args[i++];
if ( updateAddress( ipAddress ) ) {
System.exit( 0 );
} else
System.exit( 2 );
} else {
log.error( "Illegal option: --updateaddress requires <IPADDRESS>" );
System.exit( 2 );
}
} else {
// no correct option was selected.
log.error( "Illegal option: given argument(s) not valid." );
log.info( "\nUsage: java -jar target/satellite-daemon-1.0-SNAPSHOT-jar-with-dependencies.jar [options]\n"
+ " options:\n"
+ " [--checkconfig] or\n"
+ " [--genid <ORGANIZANTIONNAME>] or\n"
+ " [--import <ORGANIZATIONNAME> <MODULUS> <PRIV_EXP> <PUB_EXP>] or\n"
+ " [--submitkey <IPADDRESS>] or\n"
+ " [--updateaddress <IPADDRESS>]");
System.exit( 2 );
}
} else if ( args.length == 0 ) {
// No Option choosed, try to load existing identity.
if ( !tryLoadIdentity() ) {
System.exit( 2 );
}
}
if ( !Globals.masterServerSslContextInit() ) {
log.error( "Problem with initializing the SSLContext" );
System.exit( 1 );
}
// Start Up- and Download.
Thread uploadWorker = new Thread( new FileUploadWorker() );
uploadWorker.start();
Thread downloadWorker = new Thread( new FileDownloadWorker() );
downloadWorker.start();
}
private static boolean checkConfig()
{
if ( Identity.getOrganizationName() == null ) {
log.error( "Checking config failed: no existing organization name." );
return false;
}
RSAPublicKey pub = (RSAPublicKey)Identity.getPublicKey();
RSAPrivateKey priv = (RSAPrivateKey)Identity.getPrivateKey();
assert ( pub.getModulus() == priv.getModulus() );
BigInteger modulus = pub.getModulus();
return Identity.isValidKeyPair(
modulus,
priv.getPrivateExponent(),
pub.getPublicExponent() );
}
private static boolean genId( String organizationName )
{
return Identity.generateIdentity( organizationName );
}
private static boolean importId( String organizationName, BigInteger modulus, BigInteger privExp, BigInteger pubExp )
{
if ( !Identity.isValidKeyPair( modulus, privExp, pubExp ) ) {
log.error( "Given arguments not valid: given modulus, private and public exponent no valid key pair." );
return false;
}
return Identity.importIdentity( organizationName, modulus, privExp, pubExp );
}
private static boolean submitKey( String ipAddress )
{
return Identity.submitKey( ipAddress );
}
private static boolean updateAddress( String ipAddress )
{
return Identity.updateAddress( ipAddress );
}
private static boolean tryLoadIdentity()
{
return checkConfig();
}
}