001: // %Z%%M% %I% "%W% %E% Sun Microsystems"
002:
003: package com.sun.portal.netlet.client.common;
004:
005: import com.sun.portal.netlet.econnection.*;
006:
007: import java.net.Socket;
008: import java.io.*;
009: import java.awt.event.ActionListener;
010: import java.awt.event.ActionEvent;
011: import java.awt.*;
012:
013: class RWGroupCrypt extends ReaderWriterLock implements KeyConstants,
014: ActionListener {
015:
016: volatile boolean done = false;
017: private int srcPort;
018: private String serverPort;
019: private String serverHost;
020: private Socket in_s;
021: private Socket out_s;
022: // Added for Lihue Pluggable Cipher
023: private String cipherName;
024: // End
025: private ReaderWriterEncrypt src_to_dst;
026: private ReaderWriterDecrypt dst_to_src;
027:
028: private ProxySConn sconn;
029: private Thread s2dThread;
030: private Thread d2sThread;
031: private SClient sc;
032: public String newFtpAddrPort;
033: private NetletErrorDialog ned;
034:
035: RWGroupCrypt(Socket in, int connectionType, int sport, int srvport,
036: String srvhost, SClientMgr scm, SClient sclient,
037: String ciphername, int keylength) {
038: this (in, connectionType, sport, new String("" + srvport),
039: srvhost, scm, sclient, ciphername, keylength);
040: }
041:
042: /*
043: * Constructor modified for RFE - 4645450 ( Lihue Netlet PRD 2.2 )
044: */
045:
046: RWGroupCrypt(Socket in, int connectionType, int sport,
047: String srvport, String srvhost, SClientMgr scm,
048: SClient sclient, String ciphername, int keylength) {
049: in_s = in;
050: srcPort = sport;
051: serverPort = srvport;
052: serverHost = srvhost;
053: sc = sclient;
054: cipherName = ciphername;
055: ned = new NetletErrorDialog(new Frame(), this , null, null);
056:
057: System.out.println("============================");
058: System.out.println("RWGroupCrypt sport " + sport);
059: System.out.println("RWGroupCrypt srvport " + srvport);
060: System.out.println("RWGroupCrypt srvhost " + srvhost);
061: System.out.println("RWGroupCrypt ciphername " + ciphername);
062: System.out.println("============================");
063:
064: if (ClientConfig.isProxyMode()) {
065: String fail = null;
066: try {
067: // try regular connection
068: sconn = new ProxySConn(ClientConfig.getProxyPort(),
069: ClientConfig.getProxyHost(), ClientConfig
070: .getDestPort(), ClientConfig
071: .getDestHost(), ciphername);
072: } catch (ProxyAuthNeededException e) {
073: // try again with proxy auth
074: try {
075: sconn = new ProxySConn(ClientConfig.getProxyPort(),
076: ClientConfig.getProxyHost(), ClientConfig
077: .getDestPort(), ClientConfig
078: .getDestHost(), true, ciphername);
079: } catch (ProxyAuthNeededException ee) {
080: fail = ee.getMessage();
081: } catch (ProxyAuthFailedException ee) {
082: fail = ee.getMessage();
083: }
084: } catch (ProxyAuthFailedException e) {
085: fail = e.getMessage();
086: }
087:
088: // redundant exception handler
089: if (fail != null) {
090: System.out
091: .println("Netlet unable to authenticate to proxy server: "
092: + fail);
093: // Unable to connect to security proxy server:
094: ned.setErrorMessage(ResourceProperties
095: .getString("rwgroup.1")
096: + " "
097: + ClientConfig.getProxyHost()
098: + ":"
099: + ClientConfig.getProxyPort());
100: ned.setVisible(true);
101: notifyparent();
102: return;
103: }
104: } else {
105: sconn = new ProxySConn(ClientConfig.getDestPort(),
106: ClientConfig.getDestHost(), ciphername);
107: }
108: // out_s :: Connection with gateway
109: out_s = sconn.getconn();
110:
111: /**
112: * Create the SSL stream for communication.
113: */
114: InputStream fromServer = null;
115: OutputStream toServer = null;
116: try {
117: fromServer = out_s.getInputStream();
118: toServer = out_s.getOutputStream();
119: } catch (Exception ex) {
120: ex.printStackTrace();
121: earlyExit();
122: return;
123: }
124:
125: if (out_s == null) {
126: if (ClientConfig.isProxyMode()) {
127: System.out
128: .println("Netlet (RWGC-a) unable to make connection to proxy: "
129: + ClientConfig.getProxyHost());
130: ned.setErrorMessage(ResourceProperties
131: .getString("rwgroup.1")
132: + " "
133: + ClientConfig.getProxyHost()
134: + ":"
135: + ClientConfig.getProxyPort());
136: } else {
137: System.out
138: .println("Netlet (RWGC-a) unable to make connection to gateway: "
139: + ClientConfig.getDestHost());
140: // Unable to connect to Gateway:
141: ned.setErrorMessage(ResourceProperties
142: .getString("rwgroup.2")
143: + " "
144: + ClientConfig.getDestHost()
145: + ":"
146: + ClientConfig.getDestPort());
147: }
148: ned.setVisible(true);
149: notifyparent();
150: return;
151: }
152:
153: ESessionMsg eMsg = new ESessionMsg(("RP1.0").getBytes());
154: if (eMsg.writeMsg(new DataOutputStream(toServer)) != 0) {
155: earlyExit();
156: return;
157: }
158:
159: // tell erproxy that it's netlet...
160: if (encryptSessionId(ClientConfig.getSessionID().getBytes(),
161: ClientConfig.getSessionID().getBytes().length,
162: new DataOutputStream(toServer)) != 0) {
163: earlyExit();
164: return;
165: }
166:
167: // didn't use to encrypt proxy conf info...so hack it in
168: ProxyCipherMsg pMsg = new ProxyCipherMsg(srcPort, serverPort
169: .getBytes(), serverPort.getBytes().length, serverHost
170: .getBytes(), serverHost.getBytes().length);
171: if (pMsg.writeMsg(new DataOutputStream(toServer)) != 0) {
172: earlyExit();
173: return;
174: }
175:
176: pMsg = null;
177:
178: if (srvport.equals("-1")) {
179:
180: try {
181: DataInputStream dis = new DataInputStream(fromServer);
182: newFtpAddrPort = dis.readInt() + "," + dis.readInt()
183: + "," + dis.readInt() + "," + dis.readInt()
184: + "," + dis.readInt() + "," + dis.readInt();
185: System.out.println("newFtpAddrPort " + newFtpAddrPort);
186: } catch (Exception ex) {
187: earlyExit();
188: return;
189: }
190: }
191:
192: // in_s :: ServerSocket b/w client & server
193: InputStream fromClient = null;
194: OutputStream toClient = null;
195: try {
196: fromClient = in_s.getInputStream();
197: toClient = in_s.getOutputStream();
198: } catch (IOException ex) {
199: System.out
200: .println("Unable to get the input/output stream from the client");
201: ex.printStackTrace();
202: }
203:
204: // Needed to support FTP client active mode using the PORT command
205: //if( sport == 30021) {
206: if (connectionType == ClientConfig.FTP) {
207: src_to_dst = new ReaderWriterEncryptFtp(this , fromClient,
208: toServer, 0, scm);
209: } else {
210: src_to_dst = new ReaderWriterEncrypt(this , fromClient,
211: toServer,
212: /* ClientConfig.netletKeepAliveInterval */0);
213: }
214:
215: if (connectionType == ClientConfig.EXCHANGE) {
216: dst_to_src = new ReaderWriterDecryptExchange(this ,
217: fromServer, toClient, srvhost, scm);
218: } else if (connectionType == ClientConfig.FTP) {
219: dst_to_src = new ReaderWriterDecryptFtp(this , fromServer,
220: toClient, scm);
221: } else {
222: dst_to_src = new ReaderWriterDecrypt(this , fromServer,
223: toClient);
224: }
225:
226: s2dThread = new Thread(src_to_dst);
227: // s2dThread.setDaemon(true);
228: s2dThread.start();
229:
230: d2sThread = new Thread(dst_to_src);
231: // d2sThread.setDaemon(true);
232: d2sThread.start();
233: }
234:
235: public void notifyFinished(ReaderWriter obj) {
236: if (obj == src_to_dst) {
237: // s2dThread must be set to null right away so that when
238: // notifyFinished is called again, it doesn't get killed while
239: // cleaning up
240: s2dThread = null;
241:
242: // Save the thread variable to avoid being set to null by the other
243: // ReaderWriter thread when this method is invoked again.
244: Thread d2sTemp = d2sThread;
245: if (d2sTemp != null) {
246: try {
247: dst_to_src.stop();
248: d2sTemp.stop();
249: d2sTemp.join();
250: } catch (InterruptedException e) {
251: }
252: }
253: d2sThread = null;
254: } else if (obj == dst_to_src) {
255: // d2sThread must be set to null right away so that when
256: // notifyFinished is called again, it doesn't get killed while
257: // cleaning up
258: d2sThread = null;
259:
260: // Save the thread variable to avoid being set to null by the other
261: // ReaderWriter thread when this method is invoked again.
262: Thread s2dTemp = s2dThread;
263: if (s2dTemp != null) {
264: try {
265: src_to_dst.stop();
266: s2dTemp.stop();
267: s2dTemp.join();
268: } catch (InterruptedException e) {
269: }
270: }
271: s2dThread = null;
272: }
273: cleanup();
274: }
275:
276: public synchronized void stopAll() {
277: if (!done) {
278: if (dst_to_src != null) {
279: // dst_to_src.stop();
280: dst_to_src.netletstop();
281: }
282: if (src_to_dst != null) {
283: // src_to_dst.stop();
284: src_to_dst.netletstop();
285: }
286: cleanup();
287: }
288: }
289:
290: void earlyExit() {
291: /*try {
292: if (sslSocket != null) {
293: sslSocket.close();
294: }
295: } catch (IOException e) {
296: }*/
297: notifyparent();
298: }
299:
300: void cleanup() {
301: if (in_s != null) {
302: try {
303: in_s.close();
304: in_s = null;
305: } catch (IOException e) {
306: }
307: }
308:
309: if (out_s != null) {
310: try {
311: out_s.close();
312: out_s = null;
313: } catch (IOException e) {
314: }
315: }
316:
317: src_to_dst = null;
318: dst_to_src = null;
319: sconn = null;
320: if (sc != null) {
321: if (sc.isTrans()) {
322: sc.stopped = true;
323: }
324: }
325: notifyparent();
326: }
327:
328: void notifyparent() {
329: done = true;
330: }
331:
332: private int encryptSessionId(byte[] id, int id_len,
333: DataOutputStream out) {
334: int rc = 0;
335:
336: /*
337: * Just a temp hack for 6.2. This is not getting used anywhere. Remove cipher Name
338: * as the part of protocol
339: */
340:
341: if (cipherName == null) {
342: cipherName = "SSL_RSA_WITH_RC4_128_MD5";
343: }
344: // Sending the Dynamic Algorithm choosen by the user as a part of the header.
345: id = new StringBuffer(new String(id)).append("|").append(
346: cipherName).toString().getBytes();
347: id_len += cipherName.length() + 1;
348: SessionCipherMsg sMsg = new SessionCipherMsg(id, id_len);
349:
350: if (sMsg.writeMsg(out) != 0) {
351: System.out
352: .println("Netlet (RWGC-a) unable to process session id");
353: rc = -1;
354: }
355: return (rc);
356: }
357:
358: /*
359: * Added for RFE 4492648
360: */
361: public long getLastActivityTime() {
362: return 0;
363: }
364:
365: /*
366: * Added for Netlet PRD 1.8.1 for Lihue
367: * dummy implementation
368: */
369:
370: public int getAppletSrcPort() {
371: return 0;
372: }
373:
374: public long getStartTime() {
375: return 0;
376: }
377:
378: public void actionPerformed(ActionEvent evt) {
379: if ("OK".equals(evt.getActionCommand())) {
380: Object obj = evt.getSource();
381: if (obj == ned.okButton) {
382: ned.setVisible(false);
383: }
384: }
385: }
386: }
|