changeset 8:3b760b39962a tn5250

adding tn5250 files
author Carl Byington <carl@five-ten-sg.com>
date Thu, 22 May 2014 15:41:35 -0700
parents 3248b46f3973
children 731e70088af0
files res/values/strings.xml src/com/five_ten_sg/connectbot/transport/SSH.java src/org/tn5250j/framework/Tn5250jListener.java src/org/tn5250j/framework/tn5250/tnvt.java src/org/tn5250j/framework/transport/SSL/SSLImplementation.java src/org/tn5250j/framework/transport/SocketConnector.java
diffstat 6 files changed, 62 insertions(+), 192 deletions(-) [+]
line wrap: on
line diff
--- a/res/values/strings.xml	Thu May 22 13:41:05 2014 -0700
+++ b/res/values/strings.xml	Thu May 22 15:41:35 2014 -0700
@@ -119,6 +119,11 @@
 	<!-- First field is encryption algorithm. Second is the actual fingerprint in hex digits -->
 	<string name="host_fingerprint">"Host %1$s key fingerprint is %2$s"</string>
 
+    <!-- tn5250 certificate strings -->
+	<string name="host_certificate">"The certificate is %1$s"</string>
+	<string name="prompt_accept_certificate">"Unknown Certificate - Do you accept it?"</string>
+	<string name="prompt_save_certificate">"Do you want to save this certificate?"</string>
+
 	<string name="alert_passwords_do_not_match_msg">"Passwords do not match!"</string>
 	<string name="alert_wrong_password_msg">"Wrong password!"</string>
 	<string name="alert_key_corrupted_msg">"Private key appears corrupt!"</string>
--- a/src/com/five_ten_sg/connectbot/transport/SSH.java	Thu May 22 13:41:05 2014 -0700
+++ b/src/com/five_ten_sg/connectbot/transport/SSH.java	Thu May 22 15:41:35 2014 -0700
@@ -87,8 +87,9 @@
     }
 
     /**
+     * @param host
      * @param bridge
-     * @param db
+     * @param manager
      */
     public SSH(HostBean host, TerminalBridge bridge, TerminalManager manager) {
         super(host, bridge, manager);
--- a/src/org/tn5250j/framework/Tn5250jListener.java	Thu May 22 13:41:05 2014 -0700
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,38 +0,0 @@
-/**Copyright (C) 2004 Seagull Software
-*
-* This library is free software; you can redistribute it and/or
-* modify it under the terms of the GNU Lesser General Public
-* License as published by the Free Software Foundation; either
-* version 2.1 of the License, or (at your option) any later version.
-*
-* This library is distributed in the hope that it will be useful,
-* but WITHOUT ANY WARRANTY; without even the implied warranty of
-* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-* Lesser General Public License for more details.
-*
-* You should have received a copy of the GNU Lesser General Public
-* License along with this library; if not, write to the Free Software
-* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
-*
-*@author bvansomeren (bvansomeren@seagull.nl)
-*/
-package org.tn5250j.framework;
-
-import java.io.File;
-import java.util.Properties;
-
-public abstract class Tn5250jListener {
-	public abstract void actionPerformed(Tn5250jEvent event);
-	
-	public abstract void init(File fileDir, Properties config);
-	
-	public abstract void run();
-	
-	public abstract void destroy();
-	
-	public abstract String getName();
-	
-	public abstract void setController(Tn5250jController control);
-	
-	public abstract void sessionCreated(Tn5250jSession session);
-}
--- a/src/org/tn5250j/framework/tn5250/tnvt.java	Thu May 22 13:41:05 2014 -0700
+++ b/src/org/tn5250j/framework/tn5250/tnvt.java	Thu May 22 15:41:35 2014 -0700
@@ -80,6 +80,9 @@
 
 import javax.net.ssl.SSLSocket;
 
+import com.five_ten_sg.connectbot.service.TerminalBridge;
+import com.five_ten_sg.connectbot.service.TerminalManager;
+
 import org.tn5250j.Session5250;
 import org.tn5250j.TN5250jConstants;
 import org.tn5250j.encoding.CharMappings;
@@ -89,8 +92,6 @@
 import org.tn5250j.tools.logging.TN5250jLogger;
 
 public final class tnvt implements Runnable {
-
-
 	// negotiating commands
 	private static final byte IAC = (byte) -1; // 255 FF
 	private static final byte DONT = (byte) -2; //254 FE
@@ -131,7 +132,6 @@
 	private Thread pthread;
 	private int readType;
 	private boolean enhanced = true;
-	private Session5250 controller;
 	private boolean cursorOn = false;
 	private String session = "";
 	private int port = 23;
@@ -160,6 +160,8 @@
 	private boolean firstScreen;
 	private String sslType;
 	private WTDSFParser sfParser;
+    private TerminalBridge bridge;
+    private TerminalManager manager;
 
 	private final TN5250jLogger log = TN5250jLogFactory.getLogger(this.getClass());
 
@@ -169,17 +171,16 @@
 	 * @param type
 	 * @param support132
 	 */
-	public tnvt(Session5250 session, Screen5250 screen52, boolean type, boolean support132) {
-
-		controller = session;
+	public tnvt(Screen5250 screen52, boolean enhanced, boolean support132, TerminalBridge bridge, TerminalManager manager) {
 		if (log.isInfoEnabled()) {
 			log.info(" new session -> " + controller.getSessionName());
 		}
-
-		enhanced = type;
+		this.screen52   = screen52;
 		this.support132 = support132;
+		this.enhanced   = enhanced;
+        this.bridge     = bridge;
+        this.manager    = manager;
 		setCodePage("37");
-		this.screen52 = screen52;
 		dataIncluded = new boolean[24];
 
 		if (System.getProperties().containsKey("SESSION_CONNECT_USER")) {
@@ -295,9 +296,8 @@
 			//         sock = new Socket(s, port);
 			//smk - For SSL compability
 			SocketConnector sc = new SocketConnector();
-			if (sslType != null)
-				sc.setSSLType(sslType);
-			sock = sc.createSocket(s, port);
+			if (sslType != null) sc.setSSLType(sslType);
+			sock = sc.createSocket(s, port, bridge, manager);
 
 			if (sock == null) {
 				log.warn("I did not get a socket");
@@ -384,11 +384,9 @@
 				log.info("Closing socket");
 				sock.close();
 			}
-			if (bin != null)
-				bin.close();
-			if (bout != null)
-				bout.close();
-			connected = false;
+			if (bin != null) bin.close();
+			if (bout != null) bout.close();
+			connected   = false;
 			firstScreen = false;
 
 			// WVL - LDC : TR.000345 : properly disconnect and clear screen
@@ -398,9 +396,6 @@
 			screen52.setCursorActive(false);
 			screen52.clearAll();
 			screen52.restoreScreen();
-
-			controller.fireSessionChanged(TN5250jConstants.STATE_DISCONNECTED);
-
 		} catch (Exception exception) {
 			log.warn(exception.getMessage());
 			connected = false;
@@ -1165,95 +1160,8 @@
 
 	}
 
-	//      private final void execCmd() {
-	//         String name = "";
-	//         String argString = "";
-	//
-	//         StringBuffer sb = new StringBuffer();
-	//         sb.append(screen52.screen[0][3].getChar());
-	//         sb.append(screen52.screen[0][4].getChar());
-	//         sb.append(screen52.screen[0][5].getChar());
-	//         sb.append(screen52.screen[0][6].getChar());
-	//
-	//         System.out.println("command = " + sb);
-	//         int x = 8;
-	//         sb.setLength(0);
-	//         while (screen52.screen[0][x].getChar() > ' ') {
-	//            sb.append(screen52.screen[0][x].getChar());
-	//            x++;
-	//         }
-	//         name = sb.toString();
-	//         System.out.println("name = " + name);
-	//
-	//         sb.setLength(0);
-	//         x++;
-	//         while (screen52.screen[0][x].getChar() >= ' ') {
-	//            sb.append(screen52.screen[0][x].getChar());
-	//            x++;
-	//         }
-	//         argString = sb.toString();
-	//         System.out.println("args = " + argString);
-	//
-	//         sendAidKey(AID_ENTER);
-	//
-	//         try {
-	//
-	//            Class c = Class.forName(name);
-	//            String args1[] = {argString};
-	//            String args2[] = {};
-	//
-	//            Method m = c.getMethod("main",
-	//            new Class[] { args1.getClass() });
-	//            m.setAccessible(true);
-	//            int mods = m.getModifiers();
-	//            if (m.getReturnType() !=
-	//                   void.class || !Modifier.isStatic(mods) ||
-	//                  !Modifier.isPublic(mods)) {
-	//
-	//                     throw new NoSuchMethodException("main");
-	//                  }
-	//            try {
-	//               if (argString.length() > 0)
-	//                  m.invoke(null, new Object[] { args1 });
-	//               else
-	//                  m.invoke(null, new Object[] { args2 });
-	//            }
-	//            catch (IllegalAccessException e) {
-	//                 // This should not happen, as we have
-	//                 // disabled access checks
-	//                  System.out.println("iae " + e.getMessage());
-	//
-	//            }
-	//         }
-	//         catch (ClassNotFoundException cnfe) {
-	//            System.out.println("cnfe " + cnfe.getMessage());
-	//         }
-	//         catch (NoSuchMethodException nsmf) {
-	//            System.out.println("nsmf " + nsmf.getMessage());
-	//         }
-	//         catch (InvocationTargetException ite) {
-	//            System.out.println("ite " + ite.getMessage());
-	//         }
-	//   // catch (IllegalAccessException iae) {
-	//   // System.out.println("iae " + iae.getMessage());
-	//   // }
-	//   // catch (InstantiationException ie) {
-	//   // System.out.println("ie " + ie.getMessage());
-	//   // }
-	//   // try {
-	//   //
-	//   // Runtime rt = Runtime.getRuntime();
-	//   // Process proc = rt.exec("notepad");
-	//   // int exitVal = proc.exitValue();
-	//   // }
-	//   // catch (Throwable t) {
-	//   //
-	//   // t.printStackTrace();
-	//   // }
-	//      }
 
 	private final void readScreen() throws IOException {
-
 		int rows = screen52.getRows();
 		int cols = screen52.getColumns();
 		byte abyte0[] = new byte[rows * cols];
@@ -1263,7 +1171,6 @@
 	}
 
 	private final void fillScreenArray(byte[] sa, int rows, int cols) {
-
 		int la = 32;
 		int sac = 0;
 		int len = rows * cols;
@@ -1980,12 +1887,9 @@
 					break;
 
 				case 21: // WTDSF - Write To Display Structured Field order
-					log
-					.debug("WTDSF - Write To Display Structured Field order");
+					log.debug("WTDSF - Write To Display Structured Field order");
 					byte[] seg = bk.getSegment();
 					error = sfParser.parseWriteToDisplayStructuredField(seg);
-
-					//                  error = writeToDisplayStructuredField();
 					break;
 
 				case 29: // SF - Start of Field
@@ -2002,8 +1906,7 @@
 						// control word
 
 						// check if the first fcw1 is an 0x81 if it is then get
-						// the
-						// next pair for checking
+						// the next pair for checking
 						if (fcw1 == 0x81) {
 							bk.getNextByte();
 							fcw1 = bk.getNextByte() & 0xff; // check for field
@@ -2023,9 +1926,7 @@
 										+ " "
 										+ Integer.toHexString(bk
 												.getNextByte() & 0xff));
-								//                           bk.getNextByte();
-								attr = bk.getNextByte() & 0xff; // attribute
-								// field
+								attr = bk.getNextByte() & 0xff; // attribute field
 							}
 						} else {
 							attr = fcw1; // attribute of field
@@ -2133,10 +2034,8 @@
 			screen52.clearTable();
 
 			// well that is the first time I have seen this. This fixes a
-			// problem
-			// with S/36 command line. Finally got it.
-			if (l <= 3)
-				return false;
+			// problem with S/36 command line. Finally got it.
+			if (l <= 3) return false;
 
 			screen52.setErrorLine(bk.getNextByte()); // error row
 
--- a/src/org/tn5250j/framework/transport/SSL/SSLImplementation.java	Thu May 22 13:41:05 2014 -0700
+++ b/src/org/tn5250j/framework/transport/SSL/SSLImplementation.java	Thu May 22 15:41:35 2014 -0700
@@ -37,7 +37,9 @@
 import javax.net.ssl.TrustManager;
 import javax.net.ssl.TrustManagerFactory;
 import javax.net.ssl.X509TrustManager;
-import javax.swing.JOptionPane;
+
+import com.five_ten_sg.connectbot.service.TerminalBridge;
+import com.five_ten_sg.connectbot.service.TerminalManager;
 
 import org.tn5250j.GlobalConfigure;
 import org.tn5250j.framework.transport.SSLInterface;
@@ -49,9 +51,9 @@
  * This class implements the SSLInterface and is used to create SSL socket
  * instances.
  * </p>
- * 
+ *
  * @author Stephen M. Kennedy <skennedy@tenthpowertech.com>
- * 
+ *
  */
 public class SSLImplementation implements SSLInterface, X509TrustManager {
 
@@ -61,6 +63,10 @@
 	private String userKsPath;
 	private char[] userksPassword = "changeit".toCharArray();
 
+    TerminalBridge  bridge = null;
+    TerminalManager manager = null;
+    String          target  = null;     // destination:port
+
 	KeyManagerFactory userkmf = null;
 
 	TrustManagerFactory usertmf = null;
@@ -71,7 +77,9 @@
 
 	TN5250jLogger logger;
 
-	public SSLImplementation() {
+	public SSLImplementation(TerminalBridge bridge, TerminalManager manager) {
+        this.bridge  = bridge;
+        this.manager = manager;
 		logger = TN5250jLogFactory.getLogger(getClass());
 	}
 
@@ -107,6 +115,7 @@
 			throw new IllegalStateException("SSL Context Not Initialized");
 		SSLSocket socket = null;
 		try {
+            target = destination + ":" + String.valueOf(port);
 			socket = (SSLSocket) sslContext.getSocketFactory().createSocket(
 					destination, port);
 		} catch (Exception e) {
@@ -119,7 +128,7 @@
 
 	/*
 	 * (non-Javadoc)
-	 * 
+	 *
 	 * @see javax.net.ssl.X509TrustManager#getAcceptedIssuers()
 	 */
 	public X509Certificate[] getAcceptedIssuers() {
@@ -128,7 +137,7 @@
 
 	/*
 	 * (non-Javadoc)
-	 * 
+	 *
 	 * @see
 	 * javax.net.ssl.X509TrustManager#checkClientTrusted(java.security.cert.
 	 * X509Certificate[], java.lang.String)
@@ -141,7 +150,7 @@
 
 	/*
 	 * (non-Javadoc)
-	 * 
+	 *
 	 * @see
 	 * javax.net.ssl.X509TrustManager#checkServerTrusted(java.security.cert.
 	 * X509Certificate[], java.lang.String)
@@ -181,19 +190,16 @@
 			certInfo = certInfo.concat("Public Key: "
 					+ cert.getPublicKey().getFormat() + "\n");
 
-			int accept = JOptionPane
-					.showConfirmDialog(null, certInfo, "Unknown Certificate - Do you accept it?",
-							javax.swing.JOptionPane.YES_NO_OPTION);
-			if (accept != JOptionPane.YES_OPTION) {
+            bridge.outputLine(manager.res.getString(R.string.host_authenticity_warning, target));
+            bridge.outputLine(manager.res.getString(R.string.host_certificate, certInfo));
+            Boolean result = bridge.promptHelper.requestBooleanPrompt(null, manager.res.getString(R.string.prompt_accept_certificate));
+            if ((result == null) || (!result.booleanValue()) {
 				throw new java.security.cert.CertificateException(
 						"Certificate Rejected");
 			}
 
-			int save = JOptionPane.showConfirmDialog(null,
-					"Remember this certificate?", "Save Certificate",
-					javax.swing.JOptionPane.YES_NO_OPTION);
-
-			if (save == JOptionPane.YES_OPTION) {
+            result = bridge.promptHelper.requestBooleanPrompt(null, manager.res.getString(R.string.prompt_save_certificate));
+            if ((result != null) && (result.booleanValue()) {
 				try {
 					userks.setCertificateEntry(cert.getSubjectDN().getName(),
 							cert);
@@ -206,6 +212,5 @@
 				}
 			}
 		}
-
 	}
 }
\ No newline at end of file
--- a/src/org/tn5250j/framework/transport/SocketConnector.java	Thu May 22 13:41:05 2014 -0700
+++ b/src/org/tn5250j/framework/transport/SocketConnector.java	Thu May 22 15:41:35 2014 -0700
@@ -25,6 +25,7 @@
 
 import java.net.Socket;
 
+import org.tn5250j.framework.transport.SSL.SSLImplementation;
 import org.tn5250j.TN5250jConstants;
 import org.tn5250j.tools.logging.TN5250jLogFactory;
 import org.tn5250j.tools.logging.TN5250jLogger;
@@ -44,7 +45,7 @@
 
   /**
    * Set the type of SSL connection to use.  Specify null or an empty string
-   * to use a plain socket. 
+   * to use a plain socket.
    * @param type The SSL connection type
    * @see org.tn5250j.framework.transport.SSLConstants
    */
@@ -55,18 +56,18 @@
   /**
    * Create a new client Socket to the given destination and port.  If an SSL
    * socket type has not been specified <i>(by setSSLType(String))</i>, then
-   * a plain socket will be created.  Otherwise, a new SSL socket of the 
+   * a plain socket will be created.  Otherwise, a new SSL socket of the
    * specified type will be created.
    * @param destination
    * @param port
-   * @return a new client socket, or null if  
+   * @return a new client socket, or null if
    */
   public Socket createSocket(String destination, int port) {
 
   	Socket socket = null;
   	Exception ex = null;
-  	
-      if (sslType == null || sslType.trim().length() == 0 || 
+
+      if (sslType == null || sslType.trim().length() == 0 ||
       		sslType.toUpperCase().equals(TN5250jConstants.SSL_TYPE_NONE)) {
         	logger.info("Creating Plain Socket");
         try {
@@ -77,20 +78,17 @@
 		}
       } else {  //SSL SOCKET
 
-   		logger.info("Creating SSL ["+sslType+"] Socket");      
-      
+   		logger.info("Creating SSL ["+sslType+"] Socket");
+
       	SSLInterface sslIf = null;
-      	
-      	String sslImplClassName = 
-      		"org.tn5250j.framework.transport.SSL.SSLImplementation";  
+
 		try {
-			Class<?> c = Class.forName(sslImplClassName);
-			sslIf = (SSLInterface)c.newInstance();
+			sslIf = (SSLInterface) new SSLImplementation();
 		} catch (Exception e) {
 			ex = new Exception("Failed to create SSLInterface Instance. " +
 					"Message is ["+e.getMessage()+"]");
 		}
-		
+
       	if (sslIf != null) {
       		sslIf.init(sslType);
       		socket = sslIf.createSSLSocket(destination,port);
@@ -105,6 +103,6 @@
       }
       return socket;
   }
-      
-      
+
+
 }
\ No newline at end of file