summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openslx/satellitedaemon/App.java
blob: 3fe143aeee639707c1bb871b28325477d8296a82 (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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package org.openslx.satellitedaemon;

import java.math.BigInteger;
import java.net.Authenticator;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.ProxySelector;
import java.security.NoSuchAlgorithmException;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;

import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;
import org.openslx.network.StaticProxyAuthenticator;
import org.openslx.network.StaticProxySelector;
import org.openslx.satellitedaemon.filetransfer.FileDownloadWorker;
import org.openslx.satellitedaemon.filetransfer.FileUploadWorker;

import com.btr.proxy.search.wpad.WpadProxySearchStrategy;
import com.btr.proxy.util.ProxyException;

/***********************************************************************************************/
/**
 * 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 );
		}

		log.info( "Configure proxy settings ..." );
		// Configuring proxy settings. First read options from config file.
		String proxyConfiguration = Globals.getProxyConf();
		if ( ( proxyConfiguration.equals( "AUTO" ) ) || ( proxyConfiguration.equals( "" ) ) ) {
			log.info( "Configuring proxy settings automatically..." );
			// Configuring proxy settings automatically.
			WpadProxySearchStrategy wPSS = new WpadProxySearchStrategy();
			try {
				ProxySelector pS = wPSS.getProxySelector();
				ProxySelector.setDefault( pS );
			} catch ( ProxyException e ) {
				log.error( "Setting proxy configuration automatically failed.", e );
			}
		} else if ( proxyConfiguration.equals( "YES" ) ) {
			// Take the proxy settings from config file.
			// First check if one of the following necessary options might not be set.
			if ( Globals.checkProxySettings() ) {
				String proxyAddress = Globals.getProxyAddress();
				int proxyPort = Globals.getProxyPort();

				// Configure proxy.
				Proxy proxy = new Proxy( Proxy.Type.SOCKS, new InetSocketAddress( proxyAddress, proxyPort ) );
				StaticProxySelector sPS = new StaticProxySelector( proxy );
				ProxySelector.setDefault( sPS );

				if ( ! ( Globals.getProxyUsername().equals( "" ) ) && ! ( Globals.getProxyPassword().equals( "" ) ) ) {
					log.info( "Configuring proxy settings manually WITH authentication..." );
					// use Proxy with authentication.
					String proxyUname = Globals.getProxyUsername();
					String proxyPass = Globals.getProxyPassword();

					// Set authentication.
					StaticProxyAuthenticator sPA = new StaticProxyAuthenticator( proxyUname, proxyPass );
					Authenticator.setDefault( sPA );
				}
			}
		}
		log.info( "... proxy settings are done." );

		// 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();
	}
}