summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openslx/satellitedaemon/App.java
blob: 4a6bb7bfc7e3290b5186d302d4bb55bc8faeb887 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
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();
	}
}