mirror of
https://github.com/dkmstr/openuds.git
synced 2025-01-03 01:17:56 +03:00
Removing Java plugins related code
This commit is contained in:
parent
4ed20850ae
commit
3206390c9c
@ -1,6 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="src" path="src"/>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
|
||||
<classpathentry kind="output" path="bin"/>
|
||||
</classpath>
|
2
nxtransport/.gitignore
vendored
2
nxtransport/.gitignore
vendored
@ -1,2 +0,0 @@
|
||||
/bin/
|
||||
/jar/
|
@ -1,17 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>nxtransport</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
@ -1,19 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
||||
<jardesc>
|
||||
<jar path="nxtransport/jar/nxtransport.jar"/>
|
||||
<options buildIfNeeded="true" compress="true" descriptionLocation="/nxtransport/description.jardesc" exportErrors="true" exportWarnings="true" includeDirectoryEntries="false" overwrite="true" saveDescription="true" storeRefactorings="false" useSourceFolders="false"/>
|
||||
<storedRefactorings deprecationInfo="true" structuralOnly="false"/>
|
||||
<selectedProjects/>
|
||||
<manifest generateManifest="false" manifestLocation="/nxtransport/src/manifest" manifestVersion="1.0" reuseManifest="true" saveManifest="true" usesManifest="true">
|
||||
<sealing sealJar="true">
|
||||
<packagesToSeal>
|
||||
<package handleIdentifier="=rdp/src<com.virtualcable.rdp"/>
|
||||
<package handleIdentifier="=rdp/src<net.sourceforge.jdpapi"/>
|
||||
</packagesToSeal>
|
||||
<packagesToUnSeal/>
|
||||
</sealing>
|
||||
</manifest>
|
||||
<selectedElements exportClassFiles="true" exportJavaFiles="false" exportOutputFolder="false">
|
||||
<javaElement handleIdentifier="=nxtransport/src"/>
|
||||
</selectedElements>
|
||||
</jardesc>
|
@ -1,135 +0,0 @@
|
||||
|
||||
import java.applet.*;
|
||||
import java.awt.*;
|
||||
import java.security.AccessController;
|
||||
import java.security.PrivilegedAction;
|
||||
import java.util.Hashtable;
|
||||
|
||||
import es.virtualcable.nx.LinuxApplet;
|
||||
import es.virtualcable.nx.MacApplet;
|
||||
import es.virtualcable.nx.OsApplet;
|
||||
import es.virtualcable.nx.WindowsApplet;
|
||||
|
||||
public class NxTransportApplet extends Applet {
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static final long serialVersionUID = 6553108857320035827L;
|
||||
/**
|
||||
*
|
||||
*/
|
||||
//private int width;
|
||||
//private int height;
|
||||
private OsApplet applet;
|
||||
|
||||
public void init() {
|
||||
//width = getSize().width;
|
||||
//height = getSize().height;
|
||||
setBackground(Color.lightGray);
|
||||
|
||||
try {
|
||||
AccessController.doPrivileged(new PrivilegedAction<Object>() {
|
||||
public Object run() {
|
||||
try {
|
||||
String os = System.getProperty("os.name");
|
||||
if( os.startsWith("Windows") )
|
||||
applet = new WindowsApplet();
|
||||
else if( os.startsWith("Linux"))
|
||||
applet = new LinuxApplet();
|
||||
else if (os.startsWith("Mac"))
|
||||
applet = new MacApplet();
|
||||
else
|
||||
throw new Exception("Invalid os!!!");
|
||||
|
||||
Hashtable<String,String> params = parseParams(simpleUnscrambler(getParameter("data")));
|
||||
String baseUrl = getCodeBase().toString();
|
||||
|
||||
Dimension scrSize = Toolkit.getDefaultToolkit().getScreenSize();
|
||||
applet.setParameters(params, baseUrl, scrSize.width, scrSize.height);
|
||||
applet.init();
|
||||
|
||||
return null; // nothing to return
|
||||
} catch (Exception e) {
|
||||
System.out.println("Exception:" + e.getMessage());
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
});
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void start() {
|
||||
try {
|
||||
AccessController.doPrivileged(new PrivilegedAction<Object>() {
|
||||
public Object run() {
|
||||
try {
|
||||
applet.start();
|
||||
return null; // nothing to return
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
});
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void destroy() {
|
||||
try {
|
||||
AccessController.doPrivileged(new PrivilegedAction<Object>() {
|
||||
public Object run() {
|
||||
try {
|
||||
applet.destroy();
|
||||
return null; // nothing to return
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
});
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void paint(Graphics g) {
|
||||
g.setColor(Color.black);
|
||||
g.drawString("UDS NX connector", 8, 16);
|
||||
}
|
||||
|
||||
private Hashtable<String,String> parseParams(String params)
|
||||
{
|
||||
Hashtable<String,String> res = new Hashtable<String, String>();
|
||||
String[] parms = params.split("\t");
|
||||
for( int i = 0; i < parms.length; i++) {
|
||||
String[] val = parms[i].split(":");
|
||||
if( val.length == 1 )
|
||||
res.put(val[0], "");
|
||||
else
|
||||
res.put(val[0], val[1]);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
private String simpleUnscrambler(String in)
|
||||
{
|
||||
int len = in.length();
|
||||
StringBuilder res = new StringBuilder(len/2);
|
||||
int val = (int)'M';
|
||||
int pos = 0;
|
||||
for( int i = 0; i < len; i += 2, pos++) {
|
||||
String b = in.substring(i, i+2);
|
||||
int c = Integer.parseInt(b, 16)^val;
|
||||
val = (val ^ pos)&0xFF;
|
||||
res.append((char)c);
|
||||
}
|
||||
return res.toString();
|
||||
}
|
||||
|
||||
}
|
@ -1,107 +0,0 @@
|
||||
package es.virtualcable.nx;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Hashtable;
|
||||
import java.util.UUID;
|
||||
|
||||
public class LinuxApplet implements OsApplet {
|
||||
|
||||
private final String[] paths = { "/usr/NX/bin/", "/usr/local/bin/", "/usr/bin/" };
|
||||
private final String nxclient = "nxclient";
|
||||
|
||||
private Hashtable<String,String> params;
|
||||
private String tmpDir = "";
|
||||
private String baseUrl = "";
|
||||
private String nxFileName = "";
|
||||
private String scrWidth;
|
||||
private String scrHeight;
|
||||
|
||||
public void start() {
|
||||
tmpDir = System.getProperty("java.io.tmpdir") + File.separator;
|
||||
nxFileName = tmpDir + UUID.randomUUID().toString() + ".nxs";
|
||||
|
||||
String width = params.get("width");
|
||||
String height = params.get("height");
|
||||
boolean fullScreen = false;
|
||||
|
||||
// Notifies to broker the hostname/ip
|
||||
util.notifyHostname(baseUrl, params.get("is"));
|
||||
|
||||
if( width.equals("-1"))
|
||||
{
|
||||
width = scrWidth;
|
||||
height = scrHeight;
|
||||
fullScreen = true;
|
||||
}
|
||||
|
||||
NxFile nx = new NxFile(fullScreen, width, height);
|
||||
nx.host = params.get("ip");
|
||||
nx.username = params.get("user");
|
||||
nx.password = params.get("pass");
|
||||
nx.cachedisk = params.get("cacheDisk");
|
||||
nx.cachemem = params.get("cacheMem");
|
||||
nx.port = params.get("port");
|
||||
nx.desktop = params.get("session");
|
||||
nx.linkSpeed = params.get("connection");
|
||||
|
||||
try {
|
||||
nx.saveFile( nxFileName );
|
||||
} catch (IOException e) {
|
||||
javax.swing.JOptionPane.showMessageDialog(null, "Can't save nx temporal file: " + e.getMessage());
|
||||
e.printStackTrace();
|
||||
return;
|
||||
}
|
||||
|
||||
String execPath = "";
|
||||
|
||||
for(int i = 0; i < paths.length; i++ )
|
||||
{
|
||||
File f = new File(paths[i] + nxclient);
|
||||
if( f.exists() )
|
||||
{
|
||||
execPath = paths[i] + nxclient;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if( execPath.length() == 0 )
|
||||
{
|
||||
javax.swing.JOptionPane.showMessageDialog(null, "Can't find nxclient client.\nShould be at /usr/NX/bin, /usr/bin or /usr/local/bin\nPlease, install it");
|
||||
System.err.println("Can't find nxclient.");
|
||||
return;
|
||||
}
|
||||
|
||||
ArrayList<String> exec = new ArrayList<String>();
|
||||
exec.add(execPath);
|
||||
exec.add("--session");
|
||||
exec.add(nxFileName);
|
||||
|
||||
try {
|
||||
ProcessBuilder pb = new ProcessBuilder(exec);
|
||||
pb.start();
|
||||
} catch(Exception e) {
|
||||
javax.swing.JOptionPane.showMessageDialog(null,"Exception at applet:\n" + e.getMessage());
|
||||
e.printStackTrace();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
public void init() {
|
||||
}
|
||||
|
||||
public void destroy() {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
public void setParameters(Hashtable<String, String> parameters, String urlBase,
|
||||
int screenWidth, int screenHeight) {
|
||||
params = parameters;
|
||||
baseUrl = urlBase;
|
||||
scrWidth = Integer.toString(screenWidth);
|
||||
scrHeight = Integer.toString(screenHeight);
|
||||
}
|
||||
|
||||
}
|
@ -1,118 +0,0 @@
|
||||
package es.virtualcable.nx;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Hashtable;
|
||||
import java.util.UUID;
|
||||
|
||||
// Adapted to Mac OS X Lion by Virginio
|
||||
|
||||
public class MacApplet implements OsApplet {
|
||||
|
||||
private final String[] paths = { "/Applications/OpenNX/OpenNX.app/Contents/MacOS/" };
|
||||
private final String app = "OpenNXapp";
|
||||
|
||||
private Hashtable<String,String> params;
|
||||
private String tmpDir = "";
|
||||
private String baseUrl = "";
|
||||
private String nxFileName = "";
|
||||
private String scrWidth;
|
||||
private String scrHeight;
|
||||
private String msg;
|
||||
|
||||
public void start() {
|
||||
tmpDir = System.getProperty("java.io.tmpdir") + File.separator;
|
||||
nxFileName = tmpDir + UUID.randomUUID().toString() + ".nxs";
|
||||
|
||||
String width = params.get("width");
|
||||
String height = params.get("height");
|
||||
boolean fullScreen = false;
|
||||
|
||||
// Notifies to broker the hostname/ip
|
||||
util.notifyHostname(baseUrl, params.get("is"));
|
||||
|
||||
if( width.equals("-1"))
|
||||
{
|
||||
width = scrWidth;
|
||||
height = scrHeight;
|
||||
fullScreen = true;
|
||||
}
|
||||
|
||||
NxFile nx = new NxFile(fullScreen, width, height);
|
||||
nx.host = params.get("ip");
|
||||
nx.username = params.get("user");
|
||||
nx.password = params.get("pass");
|
||||
nx.cachedisk = params.get("cacheDisk");
|
||||
nx.cachemem = params.get("cacheMem");
|
||||
nx.port = params.get("port");
|
||||
nx.desktop = params.get("session");
|
||||
nx.linkSpeed = params.get("connection");
|
||||
|
||||
try {
|
||||
nx.saveFile( nxFileName );
|
||||
} catch (IOException e) {
|
||||
javax.swing.JOptionPane.showMessageDialog(null, "Can't save nx temporal file: " + e.getMessage());
|
||||
e.printStackTrace();
|
||||
msg = "DISK_WRITE_ERROR";
|
||||
return;
|
||||
}
|
||||
|
||||
String execPath = "";
|
||||
|
||||
for(int i = 0; i < paths.length; i++ )
|
||||
{
|
||||
File f = new File(paths[i] + app);
|
||||
if( f.exists() )
|
||||
{
|
||||
execPath = paths[i] + app;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if( execPath.length() == 0 )
|
||||
{
|
||||
javax.swing.JOptionPane.showMessageDialog(null, "Can't find OpenNX. Install it from http://opennx.net/");
|
||||
System.err.println("Can't find OpenNX executable");
|
||||
msg = "PROGRAM_NOT_FOUND";
|
||||
return;
|
||||
}
|
||||
|
||||
ArrayList<String> exec = new ArrayList<String>();
|
||||
exec.add(execPath);
|
||||
exec.add("--session=" + nxFileName);
|
||||
exec.add("--autologin");
|
||||
exec.add("--killerrors");
|
||||
|
||||
try {
|
||||
ProcessBuilder pb = new ProcessBuilder(exec);
|
||||
pb.start();
|
||||
} catch(Exception e) {
|
||||
javax.swing.JOptionPane.showMessageDialog(null,"Exception at applet:\n" + e.getMessage());
|
||||
e.printStackTrace();
|
||||
msg = "APPLET_ERROR";
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
public void init() {
|
||||
}
|
||||
|
||||
public void destroy() {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
public void setParameters(Hashtable<String, String> parameters, String urlBase,
|
||||
int screenWidth, int screenHeight) {
|
||||
params = parameters;
|
||||
baseUrl = urlBase;
|
||||
scrWidth = Integer.toString(screenWidth);
|
||||
scrHeight = Integer.toString(screenHeight);
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return msg;
|
||||
}
|
||||
|
||||
}
|
@ -1,122 +0,0 @@
|
||||
package es.virtualcable.nx;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
public class NXPassword {
|
||||
// Encoding method extracted from nomachine web site:
|
||||
// http://www.nomachine.com/ar/view.php?ar_id=AR01C00125
|
||||
|
||||
private static final int numValidCharList = 85;
|
||||
private static final String dummyString = "{{{{";
|
||||
private static final char[] validCharList = {
|
||||
'!', '#', '$', '%', '&', '(', ')', '*', '+', '-',
|
||||
'.', '0', '1', '2', '3', '4', '5', '6', '7', '8',
|
||||
'9', ':', ';', '<', '>', '?', '@', 'A', 'B', 'C',
|
||||
'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
|
||||
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
|
||||
'X', 'Y', 'Z', '[', ']', '_', 'a', 'b', 'c', 'd',
|
||||
'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
|
||||
'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
|
||||
'y', 'z', '{', '|', '}'
|
||||
};
|
||||
|
||||
private static StringBuilder encodePassword(String p)
|
||||
{
|
||||
StringBuilder sPass = new StringBuilder(":");
|
||||
|
||||
if (p.length() == 0)
|
||||
return new StringBuilder("");
|
||||
|
||||
for (int i = 0; i < p.length(); i++)
|
||||
{
|
||||
char c = (char)p.charAt(i);
|
||||
sPass.append(c+i+1).append(":");
|
||||
}
|
||||
|
||||
return sPass;
|
||||
}
|
||||
|
||||
private static int findCharInList(char c)
|
||||
{
|
||||
int i = -1;
|
||||
|
||||
for (int j = 0; j < numValidCharList; j++)
|
||||
{
|
||||
if (validCharList[j] == c)
|
||||
{
|
||||
i = j;
|
||||
return i;
|
||||
}
|
||||
}
|
||||
|
||||
return i;
|
||||
}
|
||||
|
||||
private static char getRandomValidCharFromList()
|
||||
{
|
||||
// int k = (int)(java.lang.System.currentTimeMillis() % 60);
|
||||
int k = 0;
|
||||
return validCharList[k];
|
||||
}
|
||||
|
||||
public static String scrambleString(String s)
|
||||
{
|
||||
StringBuilder pass = new StringBuilder();
|
||||
|
||||
if (s == null || s.length() == 0)
|
||||
return s;
|
||||
|
||||
StringBuilder str = encodePassword(s);
|
||||
|
||||
if (str.length() < 32)
|
||||
str.append(dummyString);
|
||||
|
||||
pass.append(str.reverse());
|
||||
|
||||
if (pass.length() < 32)
|
||||
pass.append(dummyString);
|
||||
|
||||
int k = getRandomValidCharFromList();
|
||||
int l = k + pass.length() - 2;
|
||||
|
||||
pass.insert(0, (char)k);
|
||||
|
||||
for (int i1 = 1; i1 < (int)pass.length(); i1++)
|
||||
{
|
||||
int j = findCharInList(pass.charAt(i1));
|
||||
|
||||
if (j == -1)
|
||||
return s;
|
||||
|
||||
int i = (j + l * (i1 + 1)) % numValidCharList;
|
||||
|
||||
pass.setCharAt(i1,validCharList[i]);
|
||||
}
|
||||
|
||||
char c = (char)(getRandomValidCharFromList() + 2);
|
||||
pass.append(c);
|
||||
|
||||
// Convert entities
|
||||
HashMap<Character, String> replacements = new HashMap<Character, String>();
|
||||
replacements.put('&', "&");
|
||||
replacements.put('<', "<");
|
||||
replacements.put('"', """);
|
||||
replacements.put('\'', "'");
|
||||
// And convert $ to \$
|
||||
replacements.put('$', "\\$");
|
||||
|
||||
StringBuilder result = new StringBuilder();
|
||||
for( int i = 0; i < pass.length(); i++ )
|
||||
{
|
||||
c = pass.charAt(i);
|
||||
if( replacements.containsKey(c) )
|
||||
result.append(replacements.get(c));
|
||||
else
|
||||
result.append(c);
|
||||
}
|
||||
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -1,192 +0,0 @@
|
||||
package es.virtualcable.nx;
|
||||
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
|
||||
public class NxFile {
|
||||
private static final String EMPTY_PASSWORD = "EMPTY_PASSWORD";
|
||||
|
||||
public boolean fullScreen;
|
||||
public String width;
|
||||
public String height;
|
||||
public String cachemem = "4";
|
||||
public String cachedisk = "32";
|
||||
public String keyboardLayout = "";
|
||||
public String linkSpeed = "wan";
|
||||
public String host = "";
|
||||
public String port = "";
|
||||
public String username = "";
|
||||
public String password = "";
|
||||
public String desktop = "gnome";
|
||||
|
||||
public NxFile(boolean fullScreen, String width, String height) {
|
||||
this.fullScreen = fullScreen;
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
}
|
||||
|
||||
public void saveFile(String fname) throws IOException {
|
||||
String rememberPass = "true";
|
||||
String pass = NXPassword.scrambleString(password);
|
||||
if( pass == "" )
|
||||
{
|
||||
rememberPass = "false";
|
||||
pass = EMPTY_PASSWORD;
|
||||
}
|
||||
String resolution = width + "x" + height;
|
||||
if( fullScreen )
|
||||
resolution = "fullscreen";
|
||||
|
||||
String save = NXTemplate.replaceAll("\\{CACHEMEM\\}", cachemem);
|
||||
save = save.replaceAll("\\{CACHEDISK\\}", cachedisk);
|
||||
save = save.replaceAll("\\{KEYLAYOUT\\}", keyboardLayout);
|
||||
save = save.replaceAll("\\{LINKSPEED\\}", linkSpeed);
|
||||
save = save.replaceAll("\\{REMEMBERPASS\\}", rememberPass);
|
||||
save = save.replaceAll("\\{RESOLUTION\\}", resolution);
|
||||
save = save.replaceAll("\\{WIDTH\\}", width);
|
||||
save = save.replaceAll("\\{HEIGHT\\}", height);
|
||||
save = save.replaceAll("\\{HOST\\}", host);
|
||||
save = save.replaceAll("\\{PORT\\}", port);
|
||||
save = save.replaceAll("\\{DESKTOP\\}", desktop);
|
||||
save = save.replaceAll("\\{USERNAME\\}", username);
|
||||
save = save.replaceAll("\\{PASSWORD\\}", pass);
|
||||
|
||||
FileWriter fstream = new FileWriter(fname);
|
||||
PrintWriter out = new PrintWriter(fstream);
|
||||
out.write(save);
|
||||
out.close();
|
||||
}
|
||||
|
||||
public static void main(String [] args)
|
||||
{
|
||||
NxFile nx = new NxFile(true, "1024", "768");
|
||||
try {
|
||||
nx.saveFile("");
|
||||
} catch( Exception e )
|
||||
{
|
||||
System.out.println(e);
|
||||
}
|
||||
}
|
||||
|
||||
// NX Template used to generate file
|
||||
private static final String NXTemplate =
|
||||
"<!DOCTYPE NXClientSettings>\n" +
|
||||
"<NXClientSettings application=\"nxclient\" version=\"1.3\" >\n" +
|
||||
" <group name=\"Advanced\" >\n" +
|
||||
" <option key=\"Cache size\" value=\"{CACHEMEM}\" />\n"+
|
||||
" <option key=\"Cache size on disk\" value=\"{CACHEDISK}\" />\n" +
|
||||
" <option key=\"Current keyboard\" value=\"true\" />\n" +
|
||||
" <option key=\"Custom keyboard layout\" value=\"{KEYLAYOUT}\" />\n" +
|
||||
" <option key=\"Disable ZLIB stream compression\" value=\"false\" />\n" +
|
||||
" <option key=\"Disable TCP no-delay\" value=\"false\" />\n"+
|
||||
" <option key=\"Disable deferred updates\" value=\"false\" />\n" +
|
||||
" <option key=\"Enable HTTP proxy\" value=\"false\" />\n" +
|
||||
" <option key=\"Enable SSL encryption\" value=\"true\" />\n" +
|
||||
" <option key=\"Enable response time optimisations\" value=\"true\" />\n" +
|
||||
" <option key=\"Grab keyboard\" value=\"false\" />\n" +
|
||||
" <option key=\"HTTP proxy host\" value=\"\" />\n" +
|
||||
" <option key=\"HTTP proxy port\" value=\"8080\" />\n" +
|
||||
" <option key=\"HTTP proxy username\" value=\"\" />\n" +
|
||||
" <option key=\"Remember HTTP proxy password\" value=\"false\" />\n" +
|
||||
" <option key=\"Restore cache\" value=\"true\" />\n" +
|
||||
" <option key=\"StreamCompression\" value=\"\" />\n" +
|
||||
" </group>\n" +
|
||||
" <group name=\"Environment\" >\n" +
|
||||
" <option key=\"Font server host\" value=\"\" />\n" +
|
||||
" <option key=\"Font server port\" value=\"7100\" />\n" +
|
||||
" <option key=\"Use font server\" value=\"false\" />\n" +
|
||||
" </group>\n" +
|
||||
" <group name=\"General\" >\n" +
|
||||
" <option key=\"Automatic reconnect\" value=\"true\" />\n" +
|
||||
" <option key=\"Disable SHM\" value=\"false\" />\n" +
|
||||
" <option key=\"Disable emulate shared pixmaps\" value=\"false\" />\n" +
|
||||
" <option key=\"Link speed\" value=\"{LINKSPEED}\" />\n" +
|
||||
" <option key=\"Remember password\" value=\"{REMEMBERPASS}\" />\n" +
|
||||
" <option key=\"Resolution\" value=\"{RESOLUTION}\" />\n" +
|
||||
" <option key=\"Resolution width\" value=\"{WIDTH}\" />\n" +
|
||||
" <option key=\"Resolution height\" value=\"{HEIGHT}\" />\n" +
|
||||
" <option key=\"Server host\" value=\"{HOST}\" />\n" +
|
||||
" <option key=\"Server port\" value=\"{PORT}\" />\n" +
|
||||
" <option key=\"Session\" value=\"unix\" />\n" +
|
||||
" <option key=\"Desktop\" value=\"{DESKTOP}\" />\n" +
|
||||
" <option key=\"Use default image encoding\" value=\"1\" />\n" +
|
||||
" <option key=\"Use render\" value=\"false\" />\n" +
|
||||
" <option key=\"Use taint\" value=\"true\" />\n" +
|
||||
" <option key=\"Virtual desktop\" value=\"false\" />\n" +
|
||||
" <option key=\"XAgent encoding\" value=\"true\" />\n" +
|
||||
" <option key=\"displaySaveOnExit\" value=\"true\" />\n" +
|
||||
" <option key=\"xdm broadcast port\" value=\"177\" />\n" +
|
||||
" <option key=\"xdm list host\" value=\"localhost\" />\n" +
|
||||
" <option key=\"xdm list port\" value=\"177\" />\n" +
|
||||
" <option key=\"xdm mode\" value=\"server decide\" />\n" +
|
||||
" <option key=\"xdm query host\" value=\"localhost\" />\n" +
|
||||
" <option key=\"xdm query port\" value=\"177\" />\n" +
|
||||
" </group>\n" +
|
||||
" <group name=\"Images\" >\n" +
|
||||
" <option key=\"Disable JPEG Compression\" value=\"0\" />\n" +
|
||||
" <option key=\"Disable all image optimisations\" value=\"false\" />\n" +
|
||||
" <option key=\"Disable backingstore\" value=\"false\" />\n" +
|
||||
" <option key=\"Disable composite\" value=\"false\" />\n" +
|
||||
" <option key=\"Image Compression Type\" value=\"3\" />\n" +
|
||||
" <option key=\"Image Encoding Type\" value=\"0\" />\n" +
|
||||
" <option key=\"Image JPEG Encoding\" value=\"false\" />\n" +
|
||||
" <option key=\"JPEG Quality\" value=\"6\" />\n" +
|
||||
" <option key=\"RDP Image Encoding\" value=\"3\" />\n" +
|
||||
" <option key=\"RDP JPEG Quality\" value=\"6\" />\n" +
|
||||
" <option key=\"RDP optimization for low-bandwidth link\" value=\"false\" />\n" +
|
||||
" <option key=\"Reduce colors to\" value=\"\" />\n" +
|
||||
" <option key=\"Use PNG Compression\" value=\"true\" />\n" +
|
||||
" <option key=\"VNC JPEG Quality\" value=\"6\" />\n" +
|
||||
" <option key=\"VNC images compression\" value=\"3\" />\n" +
|
||||
" </group>\n" +
|
||||
" <group name=\"Login\" >\n" +
|
||||
" <option key=\"User\" value=\"{USERNAME}\" />\n" +
|
||||
" <option key=\"Auth\" value=\"{PASSWORD}\" />\n" +
|
||||
" <option key=\"Guest Mode\" value=\"false\" />\n" +
|
||||
" <option key=\"Guest password\" value=\"\" />\n" +
|
||||
" <option key=\"Guest username\" value=\"\" />\n" +
|
||||
" <option key=\"Login Method\" value=\"nx\" />\n" +
|
||||
" <option key=\"Public Key\" value=\"-----BEGIN DSA PRIVATE KEY-----\n" +
|
||||
"MIIBuwIBAAKBgQCXv9AzQXjxvXWC1qu3CdEqskX9YomTfyG865gb4D02ZwWuRU/9\n" +
|
||||
"C3I9/bEWLdaWgJYXIcFJsMCIkmWjjeSZyTmeoypI1iLifTHUxn3b7WNWi8AzKcVF\n" +
|
||||
"aBsBGiljsop9NiD1mEpA0G+nHHrhvTXz7pUvYrsrXcdMyM6rxqn77nbbnwIVALCi\n" +
|
||||
"xFdHZADw5KAVZI7r6QatEkqLAoGBAI4L1TQGFkq5xQ/nIIciW8setAAIyrcWdK/z\n" +
|
||||
"5/ZPeELdq70KDJxoLf81NL/8uIc4PoNyTRJjtT3R4f8Az1TsZWeh2+ReCEJxDWgG\n" +
|
||||
"fbk2YhRqoQTtXPFsI4qvzBWct42WonWqyyb1bPBHk+JmXFscJu5yFQ+JUVNsENpY\n" +
|
||||
"+Gkz3HqTAoGANlgcCuA4wrC+3Cic9CFkqiwO/Rn1vk8dvGuEQqFJ6f6LVfPfRTfa\n" +
|
||||
"QU7TGVLk2CzY4dasrwxJ1f6FsT8DHTNGnxELPKRuLstGrFY/PR7KeafeFZDf+fJ3\n" +
|
||||
"mbX5nxrld3wi5titTnX+8s4IKv29HJguPvOK/SI7cjzA+SqNfD7qEo8CFDIm1xRf\n" +
|
||||
"8xAPsSKs6yZ6j1FNklfu\n" +
|
||||
"-----END DSA PRIVATE KEY-----\n" +
|
||||
"\" />\n" +
|
||||
" </group>\n" +
|
||||
" <group name=\"Services\" >\n" +
|
||||
" <option key=\"Audio\" value=\"true\" />\n" +
|
||||
" <option key=\"IPPPort\" value=\"631\" />\n" +
|
||||
" <option key=\"IPPPrinting\" value=\"false\" />\n" +
|
||||
" <option key=\"Shares\" value=\"false\" />\n" +
|
||||
" </group>\n" +
|
||||
" <group name=\"VNC Session\" >\n" +
|
||||
" <option key=\"Display\" value=\"0\" />\n" +
|
||||
" <option key=\"Remember\" value=\"false\" />\n" +
|
||||
" <option key=\"Server\" value=\"\" />\n" +
|
||||
" </group>\n" +
|
||||
" <group name=\"Windows Session\" >\n" +
|
||||
" <option key=\"Application\" value=\"\" />\n" +
|
||||
" <option key=\"Authentication\" value=\"2\" />\n" +
|
||||
" <option key=\"Color Depth\" value=\"16\" />\n" +
|
||||
" <option key=\"Domain\" value=\"\" />\n" +
|
||||
" <option key=\"Image Cache\" value=\"true\" />\n" +
|
||||
" <option key=\"Password\" value=\"EMPTY_PASSWORD\" />\n" +
|
||||
" <option key=\"Remember\" value=\"true\" />\n" +
|
||||
" <option key=\"Run application\" value=\"false\" />\n" +
|
||||
" <option key=\"Server\" value=\"\" />\n" +
|
||||
" <option key=\"User\" value=\"\" />\n" +
|
||||
" </group>\n" +
|
||||
" <group name=\"share chosen\" >\n" +
|
||||
" <option key=\"Share number\" value=\"0\" />\n" +
|
||||
" </group>\n" +
|
||||
"</NXClientSettings>";
|
||||
|
||||
}
|
@ -1,15 +0,0 @@
|
||||
package es.virtualcable.nx;
|
||||
|
||||
import java.util.Hashtable;
|
||||
|
||||
public interface OsApplet {
|
||||
|
||||
void setParameters(Hashtable<String,String> parameters, String urlBase, int screenWidth, int screenHeight);
|
||||
|
||||
void init();
|
||||
|
||||
void start();
|
||||
|
||||
void destroy();
|
||||
|
||||
}
|
@ -1,97 +0,0 @@
|
||||
package es.virtualcable.nx;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.Hashtable;
|
||||
import java.util.UUID;
|
||||
|
||||
import es.virtualcable.windows.WinRegistry;
|
||||
|
||||
public class WindowsApplet implements OsApplet {
|
||||
|
||||
private Hashtable<String,String> params;
|
||||
private String tmpDir = "";
|
||||
private String baseUrl = "";
|
||||
private String nxFileName = "";
|
||||
private String scrWidth;
|
||||
private String scrHeight;
|
||||
|
||||
public void setParameters(Hashtable<String, String> parameters, String urlBase,
|
||||
int screenWidth, int screenHeight) {
|
||||
params = parameters;
|
||||
baseUrl = urlBase;
|
||||
scrWidth = Integer.toString(screenWidth);
|
||||
scrHeight = Integer.toString(screenHeight);
|
||||
}
|
||||
|
||||
public void start() {
|
||||
try {
|
||||
tmpDir = System.getProperty("java.io.tmpdir") + File.separator;
|
||||
System.out.println(tmpDir);
|
||||
nxFileName = tmpDir + UUID.randomUUID().toString() + ".nxs";
|
||||
|
||||
String width = params.get("width");
|
||||
String height = params.get("height");
|
||||
boolean fullScreen = false;
|
||||
|
||||
// Notifies to broker the hostname/ip
|
||||
util.notifyHostname(baseUrl, params.get("is"));
|
||||
|
||||
if( width.equals("-1"))
|
||||
{
|
||||
width = scrWidth;
|
||||
height = scrHeight;
|
||||
fullScreen = true;
|
||||
}
|
||||
|
||||
NxFile nx = new NxFile(fullScreen, width, height);
|
||||
nx.host = params.get("ip");
|
||||
nx.username = params.get("user");
|
||||
nx.password = params.get("pass");
|
||||
nx.cachedisk = params.get("cacheDisk");
|
||||
nx.cachemem = params.get("cacheMem");
|
||||
nx.port = params.get("port");
|
||||
nx.desktop = params.get("session");
|
||||
nx.linkSpeed = params.get("connection");
|
||||
|
||||
try {
|
||||
nx.saveFile( nxFileName );
|
||||
} catch (IOException e) {
|
||||
javax.swing.JOptionPane.showMessageDialog(null, "Can't save nx temporal file: " + e.getMessage());
|
||||
e.printStackTrace();
|
||||
return;
|
||||
}
|
||||
String cmd = WinRegistry.readString(WinRegistry.HKEY_CURRENT_USER, "Software\\Classes\\NXClient.session\\shell\\open\\command", "");
|
||||
if ( null == cmd )
|
||||
{
|
||||
// "C:\Program Files (x86)\OpenNX\bin\opennx.exe" --session "%1"
|
||||
cmd = WinRegistry.readString(WinRegistry.HKEY_CURRENT_USER, "Software\\InnoviData\\OpenNX\\Config", "SystemNxDir");
|
||||
if( null != cmd )
|
||||
{
|
||||
cmd = "\"" + cmd + "\\bin\\opennx.exe\" --autologin --session \"%1\"";
|
||||
}
|
||||
}
|
||||
|
||||
if( null == cmd )
|
||||
javax.swing.JOptionPane.showMessageDialog(null, "Can't find neither OpenNX or Nomachine client. Please, install one of them.");
|
||||
else
|
||||
{
|
||||
nxFileName = nxFileName.replace("\\", "\\\\");
|
||||
cmd = cmd.replaceAll("%1", nxFileName);
|
||||
ProcessBuilder pb = new ProcessBuilder(util.splitCommandLine(cmd));
|
||||
pb.start();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
javax.swing.JOptionPane.showMessageDialog(null,"Exception at applet:\n" + e.getMessage());
|
||||
e.printStackTrace();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
public void init() {
|
||||
}
|
||||
|
||||
public void destroy() {
|
||||
}
|
||||
|
||||
}
|
@ -1,129 +0,0 @@
|
||||
package es.virtualcable.nx;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.URLEncoder;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class util {
|
||||
|
||||
public static boolean download(String baseUrl, String id, String outputFileName)
|
||||
{
|
||||
try {
|
||||
java.net.URL u = new java.net.URL(baseUrl + id);
|
||||
java.net.URLConnection uc = u.openConnection();
|
||||
String contentType = uc.getContentType();
|
||||
int contentLength = uc.getContentLength();
|
||||
if (contentType.startsWith("text/") || contentLength == -1) {
|
||||
throw new IOException("This is not a binary file.");
|
||||
}
|
||||
InputStream raw = uc.getInputStream();
|
||||
InputStream in = new BufferedInputStream(raw);
|
||||
byte[] data = new byte[contentLength];
|
||||
int bytesRead = 0;
|
||||
int offset = 0;
|
||||
while (offset < contentLength) {
|
||||
bytesRead = in.read(data, offset, data.length - offset);
|
||||
if (bytesRead == -1)
|
||||
break;
|
||||
offset += bytesRead;
|
||||
}
|
||||
in.close();
|
||||
|
||||
if (offset != contentLength) {
|
||||
throw new IOException("Only read " + offset + " bytes; Expected " + contentLength + " bytes");
|
||||
}
|
||||
|
||||
java.io.FileOutputStream out = new java.io.FileOutputStream(outputFileName);
|
||||
out.write(data);
|
||||
out.flush();
|
||||
out.close();
|
||||
|
||||
} catch(Exception e) {
|
||||
System.out.println("Unable to download file, already present or network error? " + e.getMessage());
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
public static String getUrl(String url) {
|
||||
try {
|
||||
java.net.URL u = new java.net.URL(url);
|
||||
BufferedReader in = new BufferedReader(new InputStreamReader(u.openStream()));
|
||||
StringBuilder data = new StringBuilder();
|
||||
|
||||
String inputLine;
|
||||
while ((inputLine = in.readLine()) != null) {
|
||||
data.append(inputLine);
|
||||
data.append("\n");
|
||||
}
|
||||
|
||||
in.close();
|
||||
return data.toString();
|
||||
|
||||
} catch(Exception e) {
|
||||
System.out.println("Unable to get url. Network error? " + e.getMessage());
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
public static void notifyHostname(String baseUrl, String serviceId) {
|
||||
String[] urlComponents = baseUrl.split("/");
|
||||
String hostname;
|
||||
String ip;
|
||||
String url="";
|
||||
|
||||
try {
|
||||
hostname = java.net.InetAddress.getLocalHost().getHostName();
|
||||
ip = java.net.InetAddress.getLocalHost().getHostAddress();
|
||||
} catch(Exception e) {
|
||||
hostname = "unknown";
|
||||
ip = "0.0.0.0";
|
||||
}
|
||||
|
||||
try {
|
||||
// An url is "http[s]://.....:/,
|
||||
url = urlComponents[0] + "//" + urlComponents[2] + "/sernotify/" + serviceId + "/hostname?hostname="+URLEncoder.encode(hostname)+"&ip="+URLEncoder.encode(ip);
|
||||
getUrl(url);
|
||||
} catch(Exception e) {
|
||||
System.out.println("Unable to get url? " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public static ArrayList<String> splitCommandLine(String cmd) {
|
||||
boolean inQuotes = false;
|
||||
ArrayList<String> res = new ArrayList<String>();
|
||||
String tmp = "";
|
||||
for( char c : cmd.toCharArray() ) {
|
||||
if( c == '"' ) {
|
||||
if( inQuotes ) {
|
||||
res.add(tmp);
|
||||
tmp = "";
|
||||
inQuotes = false;
|
||||
continue;
|
||||
}
|
||||
inQuotes = true;
|
||||
continue;
|
||||
}
|
||||
if( c == ' ' && inQuotes == false) {
|
||||
if( tmp.length() > 0 ) {
|
||||
res.add(tmp);
|
||||
tmp = "";
|
||||
continue;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
tmp += c;
|
||||
}
|
||||
if( tmp.length() > 0 )
|
||||
res.add(tmp);
|
||||
return res;
|
||||
}
|
||||
|
||||
}
|
@ -1,392 +0,0 @@
|
||||
package es.virtualcable.windows;
|
||||
|
||||
/* Code borrowed from http://stackoverflow.com/questions/62289/read-write-to-windows-registry-using-java. */
|
||||
/* Original author don't found, if someone found him, please note it here */
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.prefs.Preferences;
|
||||
|
||||
public class WinRegistry {
|
||||
public static final int HKEY_CURRENT_USER = 0x80000001;
|
||||
public static final int HKEY_LOCAL_MACHINE = 0x80000002;
|
||||
public static final int REG_SUCCESS = 0;
|
||||
public static final int REG_NOTFOUND = 2;
|
||||
public static final int REG_ACCESSDENIED = 5;
|
||||
|
||||
private static final int KEY_ALL_ACCESS = 0xf003f;
|
||||
private static final int KEY_READ = 0x20019;
|
||||
private static Preferences userRoot = Preferences.userRoot();
|
||||
private static Preferences systemRoot = Preferences.systemRoot();
|
||||
|
||||
private static Class<? extends Preferences> userClass = userRoot.getClass();
|
||||
private static Method regOpenKey = null;
|
||||
private static Method regCloseKey = null;
|
||||
private static Method regQueryValueEx = null;
|
||||
private static Method regEnumValue = null;
|
||||
private static Method regQueryInfoKey = null;
|
||||
private static Method regEnumKeyEx = null;
|
||||
private static Method regCreateKeyEx = null;
|
||||
private static Method regSetValueEx = null;
|
||||
private static Method regDeleteKey = null;
|
||||
private static Method regDeleteValue = null;
|
||||
|
||||
static {
|
||||
try {
|
||||
regOpenKey = userClass.getDeclaredMethod("WindowsRegOpenKey",
|
||||
new Class[] { int.class, byte[].class, int.class });
|
||||
regOpenKey.setAccessible(true);
|
||||
regCloseKey = userClass.getDeclaredMethod("WindowsRegCloseKey",
|
||||
new Class[] { int.class });
|
||||
regCloseKey.setAccessible(true);
|
||||
regQueryValueEx = userClass.getDeclaredMethod("WindowsRegQueryValueEx",
|
||||
new Class[] { int.class, byte[].class });
|
||||
regQueryValueEx.setAccessible(true);
|
||||
regEnumValue = userClass.getDeclaredMethod("WindowsRegEnumValue",
|
||||
new Class[] { int.class, int.class, int.class });
|
||||
regEnumValue.setAccessible(true);
|
||||
regQueryInfoKey = userClass.getDeclaredMethod("WindowsRegQueryInfoKey1",
|
||||
new Class[] { int.class });
|
||||
regQueryInfoKey.setAccessible(true);
|
||||
regEnumKeyEx = userClass.getDeclaredMethod(
|
||||
"WindowsRegEnumKeyEx", new Class[] { int.class, int.class,
|
||||
int.class });
|
||||
regEnumKeyEx.setAccessible(true);
|
||||
regCreateKeyEx = userClass.getDeclaredMethod(
|
||||
"WindowsRegCreateKeyEx", new Class[] { int.class,
|
||||
byte[].class });
|
||||
regCreateKeyEx.setAccessible(true);
|
||||
regSetValueEx = userClass.getDeclaredMethod(
|
||||
"WindowsRegSetValueEx", new Class[] { int.class,
|
||||
byte[].class, byte[].class });
|
||||
regSetValueEx.setAccessible(true);
|
||||
regDeleteValue = userClass.getDeclaredMethod(
|
||||
"WindowsRegDeleteValue", new Class[] { int.class,
|
||||
byte[].class });
|
||||
regDeleteValue.setAccessible(true);
|
||||
regDeleteKey = userClass.getDeclaredMethod(
|
||||
"WindowsRegDeleteKey", new Class[] { int.class,
|
||||
byte[].class });
|
||||
regDeleteKey.setAccessible(true);
|
||||
}
|
||||
catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private WinRegistry() { }
|
||||
|
||||
/**
|
||||
* Read a value from key and value name
|
||||
* @param hkey HKEY_CURRENT_USER/HKEY_LOCAL_MACHINE
|
||||
* @param key
|
||||
* @param valueName
|
||||
* @return the value
|
||||
* @throws IllegalArgumentException
|
||||
* @throws IllegalAccessException
|
||||
* @throws InvocationTargetException
|
||||
*/
|
||||
public static String readString(int hkey, String key, String valueName)
|
||||
throws IllegalArgumentException, IllegalAccessException,
|
||||
InvocationTargetException
|
||||
{
|
||||
if (hkey == HKEY_LOCAL_MACHINE) {
|
||||
return readString(systemRoot, hkey, key, valueName);
|
||||
}
|
||||
else if (hkey == HKEY_CURRENT_USER) {
|
||||
return readString(userRoot, hkey, key, valueName);
|
||||
}
|
||||
else {
|
||||
throw new IllegalArgumentException("hkey=" + hkey);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Read value(s) and value name(s) form given key
|
||||
* @param hkey HKEY_CURRENT_USER/HKEY_LOCAL_MACHINE
|
||||
* @param key
|
||||
* @return the value name(s) plus the value(s)
|
||||
* @throws IllegalArgumentException
|
||||
* @throws IllegalAccessException
|
||||
* @throws InvocationTargetException
|
||||
*/
|
||||
public static Map<String, String> readStringValues(int hkey, String key)
|
||||
throws IllegalArgumentException, IllegalAccessException,
|
||||
InvocationTargetException
|
||||
{
|
||||
if (hkey == HKEY_LOCAL_MACHINE) {
|
||||
return readStringValues(systemRoot, hkey, key);
|
||||
}
|
||||
else if (hkey == HKEY_CURRENT_USER) {
|
||||
return readStringValues(userRoot, hkey, key);
|
||||
}
|
||||
else {
|
||||
throw new IllegalArgumentException("hkey=" + hkey);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Read the value name(s) from a given key
|
||||
* @param hkey HKEY_CURRENT_USER/HKEY_LOCAL_MACHINE
|
||||
* @param key
|
||||
* @return the value name(s)
|
||||
* @throws IllegalArgumentException
|
||||
* @throws IllegalAccessException
|
||||
* @throws InvocationTargetException
|
||||
*/
|
||||
public static List<String> readStringSubKeys(int hkey, String key)
|
||||
throws IllegalArgumentException, IllegalAccessException,
|
||||
InvocationTargetException
|
||||
{
|
||||
if (hkey == HKEY_LOCAL_MACHINE) {
|
||||
return readStringSubKeys(systemRoot, hkey, key);
|
||||
}
|
||||
else if (hkey == HKEY_CURRENT_USER) {
|
||||
return readStringSubKeys(userRoot, hkey, key);
|
||||
}
|
||||
else {
|
||||
throw new IllegalArgumentException("hkey=" + hkey);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a key
|
||||
* @param hkey HKEY_CURRENT_USER/HKEY_LOCAL_MACHINE
|
||||
* @param key
|
||||
* @throws IllegalArgumentException
|
||||
* @throws IllegalAccessException
|
||||
* @throws InvocationTargetException
|
||||
*/
|
||||
public static void createKey(int hkey, String key)
|
||||
throws IllegalArgumentException, IllegalAccessException,
|
||||
InvocationTargetException
|
||||
{
|
||||
int [] ret;
|
||||
if (hkey == HKEY_LOCAL_MACHINE) {
|
||||
ret = createKey(systemRoot, hkey, key);
|
||||
regCloseKey.invoke(systemRoot, new Object[] { new Integer(ret[0]) });
|
||||
}
|
||||
else if (hkey == HKEY_CURRENT_USER) {
|
||||
ret = createKey(userRoot, hkey, key);
|
||||
regCloseKey.invoke(userRoot, new Object[] { new Integer(ret[0]) });
|
||||
}
|
||||
else {
|
||||
throw new IllegalArgumentException("hkey=" + hkey);
|
||||
}
|
||||
if (ret[1] != REG_SUCCESS) {
|
||||
throw new IllegalArgumentException("rc=" + ret[1] + " key=" + key);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Write a value in a given key/value name
|
||||
* @param hkey
|
||||
* @param key
|
||||
* @param valueName
|
||||
* @param value
|
||||
* @throws IllegalArgumentException
|
||||
* @throws IllegalAccessException
|
||||
* @throws InvocationTargetException
|
||||
*/
|
||||
public static void writeStringValue
|
||||
(int hkey, String key, String valueName, String value)
|
||||
throws IllegalArgumentException, IllegalAccessException,
|
||||
InvocationTargetException
|
||||
{
|
||||
if (hkey == HKEY_LOCAL_MACHINE) {
|
||||
writeStringValue(systemRoot, hkey, key, valueName, value);
|
||||
}
|
||||
else if (hkey == HKEY_CURRENT_USER) {
|
||||
writeStringValue(userRoot, hkey, key, valueName, value);
|
||||
}
|
||||
else {
|
||||
throw new IllegalArgumentException("hkey=" + hkey);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a given key
|
||||
* @param hkey
|
||||
* @param key
|
||||
* @throws IllegalArgumentException
|
||||
* @throws IllegalAccessException
|
||||
* @throws InvocationTargetException
|
||||
*/
|
||||
public static void deleteKey(int hkey, String key)
|
||||
throws IllegalArgumentException, IllegalAccessException,
|
||||
InvocationTargetException
|
||||
{
|
||||
int rc = -1;
|
||||
if (hkey == HKEY_LOCAL_MACHINE) {
|
||||
rc = deleteKey(systemRoot, hkey, key);
|
||||
}
|
||||
else if (hkey == HKEY_CURRENT_USER) {
|
||||
rc = deleteKey(userRoot, hkey, key);
|
||||
}
|
||||
if (rc != REG_SUCCESS) {
|
||||
throw new IllegalArgumentException("rc=" + rc + " key=" + key);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* delete a value from a given key/value name
|
||||
* @param hkey
|
||||
* @param key
|
||||
* @param value
|
||||
* @throws IllegalArgumentException
|
||||
* @throws IllegalAccessException
|
||||
* @throws InvocationTargetException
|
||||
*/
|
||||
public static void deleteValue(int hkey, String key, String value)
|
||||
throws IllegalArgumentException, IllegalAccessException,
|
||||
InvocationTargetException
|
||||
{
|
||||
int rc = -1;
|
||||
if (hkey == HKEY_LOCAL_MACHINE) {
|
||||
rc = deleteValue(systemRoot, hkey, key, value);
|
||||
}
|
||||
else if (hkey == HKEY_CURRENT_USER) {
|
||||
rc = deleteValue(userRoot, hkey, key, value);
|
||||
}
|
||||
if (rc != REG_SUCCESS) {
|
||||
throw new IllegalArgumentException("rc=" + rc + " key=" + key + " value=" + value);
|
||||
}
|
||||
}
|
||||
|
||||
// =====================
|
||||
|
||||
private static int deleteValue
|
||||
(Preferences root, int hkey, String key, String value)
|
||||
throws IllegalArgumentException, IllegalAccessException,
|
||||
InvocationTargetException
|
||||
{
|
||||
int[] handles = (int[]) regOpenKey.invoke(root, new Object[] {
|
||||
new Integer(hkey), toCstr(key), new Integer(KEY_ALL_ACCESS) });
|
||||
if (handles[1] != REG_SUCCESS) {
|
||||
return handles[1]; // can be REG_NOTFOUND, REG_ACCESSDENIED
|
||||
}
|
||||
int rc =((Integer) regDeleteValue.invoke(root,
|
||||
new Object[] {
|
||||
new Integer(handles[0]), toCstr(value)
|
||||
})).intValue();
|
||||
regCloseKey.invoke(root, new Object[] { new Integer(handles[0]) });
|
||||
return rc;
|
||||
}
|
||||
|
||||
private static int deleteKey(Preferences root, int hkey, String key)
|
||||
throws IllegalArgumentException, IllegalAccessException,
|
||||
InvocationTargetException
|
||||
{
|
||||
int rc =((Integer) regDeleteKey.invoke(root,
|
||||
new Object[] { new Integer(hkey), toCstr(key) })).intValue();
|
||||
return rc; // can REG_NOTFOUND, REG_ACCESSDENIED, REG_SUCCESS
|
||||
}
|
||||
|
||||
private static String readString(Preferences root, int hkey, String key, String value)
|
||||
throws IllegalArgumentException, IllegalAccessException,
|
||||
InvocationTargetException
|
||||
{
|
||||
int[] handles = (int[]) regOpenKey.invoke(root, new Object[] {
|
||||
new Integer(hkey), toCstr(key), new Integer(KEY_READ) });
|
||||
if (handles[1] != REG_SUCCESS) {
|
||||
return null;
|
||||
}
|
||||
byte[] valb = (byte[]) regQueryValueEx.invoke(root, new Object[] {
|
||||
new Integer(handles[0]), toCstr(value) });
|
||||
regCloseKey.invoke(root, new Object[] { new Integer(handles[0]) });
|
||||
return (valb != null ? new String(valb).trim() : null);
|
||||
}
|
||||
|
||||
private static Map<String,String> readStringValues
|
||||
(Preferences root, int hkey, String key)
|
||||
throws IllegalArgumentException, IllegalAccessException,
|
||||
InvocationTargetException
|
||||
{
|
||||
HashMap<String, String> results = new HashMap<String,String>();
|
||||
int[] handles = (int[]) regOpenKey.invoke(root, new Object[] {
|
||||
new Integer(hkey), toCstr(key), new Integer(KEY_READ) });
|
||||
if (handles[1] != REG_SUCCESS) {
|
||||
return null;
|
||||
}
|
||||
int[] info = (int[]) regQueryInfoKey.invoke(root,
|
||||
new Object[] { new Integer(handles[0]) });
|
||||
|
||||
int count = info[2]; // count
|
||||
int maxlen = info[3]; // value length max
|
||||
for(int index=0; index<count; index++) {
|
||||
byte[] name = (byte[]) regEnumValue.invoke(root, new Object[] {
|
||||
new Integer
|
||||
(handles[0]), new Integer(index), new Integer(maxlen + 1)});
|
||||
String value = readString(hkey, key, new String(name));
|
||||
results.put(new String(name).trim(), value);
|
||||
}
|
||||
regCloseKey.invoke(root, new Object[] { new Integer(handles[0]) });
|
||||
return results;
|
||||
}
|
||||
|
||||
private static List<String> readStringSubKeys
|
||||
(Preferences root, int hkey, String key)
|
||||
throws IllegalArgumentException, IllegalAccessException,
|
||||
InvocationTargetException
|
||||
{
|
||||
List<String> results = new ArrayList<String>();
|
||||
int[] handles = (int[]) regOpenKey.invoke(root, new Object[] {
|
||||
new Integer(hkey), toCstr(key), new Integer(KEY_READ)
|
||||
});
|
||||
if (handles[1] != REG_SUCCESS) {
|
||||
return null;
|
||||
}
|
||||
int[] info = (int[]) regQueryInfoKey.invoke(root,
|
||||
new Object[] { new Integer(handles[0]) });
|
||||
|
||||
int count = info[2]; // count
|
||||
int maxlen = info[3]; // value length max
|
||||
for(int index=0; index<count; index++) {
|
||||
byte[] name = (byte[]) regEnumKeyEx.invoke(root, new Object[] {
|
||||
new Integer
|
||||
(handles[0]), new Integer(index), new Integer(maxlen + 1)
|
||||
});
|
||||
results.add(new String(name).trim());
|
||||
}
|
||||
regCloseKey.invoke(root, new Object[] { new Integer(handles[0]) });
|
||||
return results;
|
||||
}
|
||||
|
||||
private static int [] createKey(Preferences root, int hkey, String key)
|
||||
throws IllegalArgumentException, IllegalAccessException,
|
||||
InvocationTargetException
|
||||
{
|
||||
return (int[]) regCreateKeyEx.invoke(root,
|
||||
new Object[] { new Integer(hkey), toCstr(key) });
|
||||
}
|
||||
|
||||
private static void writeStringValue
|
||||
(Preferences root, int hkey, String key, String valueName, String value)
|
||||
throws IllegalArgumentException, IllegalAccessException,
|
||||
InvocationTargetException
|
||||
{
|
||||
int[] handles = (int[]) regOpenKey.invoke(root, new Object[] {
|
||||
new Integer(hkey), toCstr(key), new Integer(KEY_ALL_ACCESS) });
|
||||
|
||||
regSetValueEx.invoke(root,
|
||||
new Object[] {
|
||||
new Integer(handles[0]), toCstr(valueName), toCstr(value)
|
||||
});
|
||||
regCloseKey.invoke(root, new Object[] { new Integer(handles[0]) });
|
||||
}
|
||||
|
||||
// utility
|
||||
private static byte[] toCstr(String str) {
|
||||
byte[] result = new byte[str.length() + 1];
|
||||
|
||||
for (int i = 0; i < str.length(); i++) {
|
||||
result[i] = (byte) str.charAt(i);
|
||||
}
|
||||
result[str.length()] = 0;
|
||||
return result;
|
||||
}
|
||||
}
|
@ -1,5 +0,0 @@
|
||||
Manifest-Version: 1.0
|
||||
Sealed: true
|
||||
Permissions: all-permissions
|
||||
Codebase: *
|
||||
Application-Name: UDS NX Connector
|
@ -1,6 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="src" path="src"/>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
|
||||
<classpathentry kind="output" path="bin"/>
|
||||
</classpath>
|
2
nxtuntransport/.gitignore
vendored
2
nxtuntransport/.gitignore
vendored
@ -1,2 +0,0 @@
|
||||
/bin/
|
||||
/jar/
|
@ -1,17 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>nxtuntransport</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
@ -1,19 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
||||
<jardesc>
|
||||
<jar path="nxtuntransport/jar/nxtuntransport.jar"/>
|
||||
<options buildIfNeeded="true" compress="true" descriptionLocation="/nxtuntransport/description.jardesc" exportErrors="true" exportWarnings="true" includeDirectoryEntries="false" overwrite="true" saveDescription="true" storeRefactorings="false" useSourceFolders="false"/>
|
||||
<storedRefactorings deprecationInfo="true" structuralOnly="false"/>
|
||||
<selectedProjects/>
|
||||
<manifest generateManifest="false" manifestLocation="/nxtuntransport/src/manifest" manifestVersion="1.0" reuseManifest="true" saveManifest="true" usesManifest="true">
|
||||
<sealing sealJar="true">
|
||||
<packagesToSeal>
|
||||
<package handleIdentifier="=rdp/src<com.virtualcable.rdp"/>
|
||||
<package handleIdentifier="=rdp/src<net.sourceforge.jdpapi"/>
|
||||
</packagesToSeal>
|
||||
<packagesToUnSeal/>
|
||||
</sealing>
|
||||
</manifest>
|
||||
<selectedElements exportClassFiles="true" exportJavaFiles="false" exportOutputFolder="false">
|
||||
<javaElement handleIdentifier="=nxtuntransport/src"/>
|
||||
</selectedElements>
|
||||
</jardesc>
|
@ -1,135 +0,0 @@
|
||||
|
||||
import java.applet.*;
|
||||
import java.awt.*;
|
||||
import java.security.AccessController;
|
||||
import java.security.PrivilegedAction;
|
||||
import java.util.Hashtable;
|
||||
|
||||
import es.virtualcable.nx.LinuxApplet;
|
||||
import es.virtualcable.nx.MacApplet;
|
||||
import es.virtualcable.nx.OsApplet;
|
||||
import es.virtualcable.nx.WindowsApplet;
|
||||
|
||||
public class NxTunTransportApplet extends Applet {
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static final long serialVersionUID = 6553108857320035827L;
|
||||
/**
|
||||
*
|
||||
*/
|
||||
//private int width;
|
||||
//private int height;
|
||||
private OsApplet applet;
|
||||
|
||||
public void init() {
|
||||
//width = getSize().width;
|
||||
//height = getSize().height;
|
||||
setBackground(Color.lightGray);
|
||||
|
||||
try {
|
||||
AccessController.doPrivileged(new PrivilegedAction<Object>() {
|
||||
public Object run() {
|
||||
try {
|
||||
String os = System.getProperty("os.name");
|
||||
if( os.startsWith("Windows") )
|
||||
applet = new WindowsApplet();
|
||||
else if( os.startsWith("Linux"))
|
||||
applet = new LinuxApplet();
|
||||
else if (os.startsWith("Mac"))
|
||||
applet = new MacApplet();
|
||||
else
|
||||
throw new Exception("Invalid os!!!");
|
||||
|
||||
Hashtable<String,String> params = parseParams(simpleUnscrambler(getParameter("data")));
|
||||
String baseUrl = getCodeBase().toString();
|
||||
|
||||
Dimension scrSize = Toolkit.getDefaultToolkit().getScreenSize();
|
||||
applet.setParameters(params, baseUrl, scrSize.width, scrSize.height);
|
||||
applet.init();
|
||||
|
||||
return null; // nothing to return
|
||||
} catch (Exception e) {
|
||||
System.out.println("Exception:" + e.getMessage());
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
});
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void start() {
|
||||
try {
|
||||
AccessController.doPrivileged(new PrivilegedAction<Object>() {
|
||||
public Object run() {
|
||||
try {
|
||||
applet.start();
|
||||
return null; // nothing to return
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
});
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void destroy() {
|
||||
try {
|
||||
AccessController.doPrivileged(new PrivilegedAction<Object>() {
|
||||
public Object run() {
|
||||
try {
|
||||
applet.destroy();
|
||||
return null; // nothing to return
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
});
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void paint(Graphics g) {
|
||||
g.setColor(Color.black);
|
||||
g.drawString("UDS NX Tunel Connector", 8, 16);
|
||||
}
|
||||
|
||||
private Hashtable<String,String> parseParams(String params)
|
||||
{
|
||||
Hashtable<String,String> res = new Hashtable<String, String>();
|
||||
String[] parms = params.split("\t");
|
||||
for( int i = 0; i < parms.length; i++) {
|
||||
String[] val = parms[i].split(":");
|
||||
if( val.length == 1 )
|
||||
res.put(val[0], "");
|
||||
else
|
||||
res.put(val[0], val[1]);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
private String simpleUnscrambler(String in)
|
||||
{
|
||||
int len = in.length();
|
||||
StringBuilder res = new StringBuilder(len/2);
|
||||
int val = (int)'M';
|
||||
int pos = 0;
|
||||
for( int i = 0; i < len; i += 2, pos++) {
|
||||
String b = in.substring(i, i+2);
|
||||
int c = Integer.parseInt(b, 16)^val;
|
||||
val = (val ^ pos)&0xFF;
|
||||
res.append((char)c);
|
||||
}
|
||||
return res.toString();
|
||||
}
|
||||
|
||||
}
|
@ -1,40 +0,0 @@
|
||||
package es.virtualcable.nx;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.*;
|
||||
import java.util.Random;
|
||||
|
||||
|
||||
public class FreePortFinder {
|
||||
|
||||
private final static int START_PORT = 35000;
|
||||
private final static int START_PORT_MAX = 44000;
|
||||
private final static int END_PORT = 45500;
|
||||
|
||||
public static boolean isFree(int port)
|
||||
{
|
||||
boolean free = true;
|
||||
try {
|
||||
InetAddress addr = InetAddress.getByName("localhost");
|
||||
SocketAddress sa = new InetSocketAddress(addr, port);
|
||||
ServerSocket socket = new ServerSocket();
|
||||
socket.bind(sa);
|
||||
socket.close();
|
||||
} catch (IOException e) {
|
||||
free = false;
|
||||
}
|
||||
return free;
|
||||
}
|
||||
|
||||
public static int findFreePort()
|
||||
{
|
||||
Random rnd = new Random();
|
||||
int startPort = START_PORT + rnd.nextInt(START_PORT_MAX-START_PORT);
|
||||
for( int port = startPort; port < END_PORT; port ++)
|
||||
{
|
||||
if( isFree(port) )
|
||||
return port;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
}
|
@ -1,131 +0,0 @@
|
||||
package es.virtualcable.nx;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.Hashtable;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
|
||||
public class LinuxApplet implements OsApplet {
|
||||
|
||||
private final String[] paths = { "/usr/NX/bin/", "/usr/local/bin/", "/usr/bin/" };
|
||||
private final String nxclient = "nxclient";
|
||||
|
||||
private Hashtable<String,String> params;
|
||||
private String tmpDir = "";
|
||||
private String baseUrl = "";
|
||||
private String nxFileName = "";
|
||||
private String jarFileName = "";
|
||||
private String scrWidth;
|
||||
private String scrHeight;
|
||||
private String tunPort = "";
|
||||
|
||||
public void start() {
|
||||
try {
|
||||
tmpDir = System.getProperty("java.io.tmpdir") + File.separator;
|
||||
nxFileName = tmpDir + UUID.randomUUID().toString() + ".nxs";
|
||||
jarFileName = tmpDir + UUID.randomUUID().toString() + ".jar";
|
||||
|
||||
// Notifies to broker the hostname/ip
|
||||
util.notifyHostname(baseUrl, params.get("is"));
|
||||
|
||||
String width = params.get("width");
|
||||
String height = params.get("height");
|
||||
boolean fullScreen = false;
|
||||
|
||||
if( width.equals("-1"))
|
||||
{
|
||||
width = scrWidth;
|
||||
height = scrHeight;
|
||||
fullScreen = true;
|
||||
}
|
||||
|
||||
if( downloadJar() == false)
|
||||
return;
|
||||
tunPort = Integer.toString(FreePortFinder.findFreePort());
|
||||
|
||||
NxFile nx = new NxFile(fullScreen, width, height);
|
||||
nx.host = "127.0.0.1";
|
||||
nx.port = tunPort;
|
||||
nx.username = params.get("user");
|
||||
nx.password = params.get("pass");
|
||||
nx.cachedisk = params.get("cacheDisk");
|
||||
nx.cachemem = params.get("cacheMem");
|
||||
nx.desktop = params.get("session");
|
||||
nx.linkSpeed = params.get("connection");
|
||||
|
||||
try {
|
||||
nx.saveFile( nxFileName );
|
||||
} catch (IOException e) {
|
||||
javax.swing.JOptionPane.showMessageDialog(null, "Can't save nx temporal file: " + e.getMessage());
|
||||
e.printStackTrace();
|
||||
return;
|
||||
}
|
||||
|
||||
String execPath = "";
|
||||
|
||||
for(int i = 0; i < paths.length; i++ )
|
||||
{
|
||||
File f = new File(paths[i] + nxclient);
|
||||
if( f.exists() )
|
||||
{
|
||||
execPath = paths[i] + nxclient;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if( execPath.length() == 0 )
|
||||
{
|
||||
javax.swing.JOptionPane.showMessageDialog(null, "Can't find nxclient client.\nShould be at /usr/NX/bin, /usr/bin or /usr/local/bin\nPlease, install it");
|
||||
System.err.println("Can't find nxclient.");
|
||||
return;
|
||||
}
|
||||
|
||||
executeTunnel(execPath);
|
||||
|
||||
} catch (Exception e) {
|
||||
javax.swing.JOptionPane.showMessageDialog(null,"Exception at applet:\n" + e.getMessage());
|
||||
e.printStackTrace();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
public void init() {
|
||||
}
|
||||
|
||||
public void destroy() {
|
||||
}
|
||||
|
||||
private void executeTunnel(String nxCmd) throws IOException
|
||||
{
|
||||
String java = System.getProperty("java.home") + File.separator + "bin" + File.separator + "java";
|
||||
String [] exec = { java, "-jar", jarFileName, tunPort, nxCmd, "--session", nxFileName };
|
||||
ProcessBuilder pb = new ProcessBuilder( exec );
|
||||
Map<String,String> env = pb.environment();
|
||||
env.put("TPARAMS", params.get("tun"));
|
||||
/*System.out.println("TPARAMS: " + params.get("tun"));
|
||||
for (String str : exec) {
|
||||
System.out.print(str + " ");
|
||||
}
|
||||
System.out.println();*/
|
||||
pb.start();
|
||||
|
||||
}
|
||||
|
||||
public void setParameters(Hashtable<String, String> parameters, String urlBase,
|
||||
int screenWidth, int screenHeight) {
|
||||
params = parameters;
|
||||
baseUrl = urlBase;
|
||||
scrWidth = Integer.toString(screenWidth);
|
||||
scrHeight = Integer.toString(screenHeight);
|
||||
|
||||
}
|
||||
|
||||
private boolean downloadJar()
|
||||
{
|
||||
return util.download(baseUrl, "2", jarFileName);
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -1,131 +0,0 @@
|
||||
package es.virtualcable.nx;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.Hashtable;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
|
||||
public class MacApplet implements OsApplet {
|
||||
|
||||
private final String[] paths = { "/Applications/OpenNX/OpenNX.app/Contents/MacOS/" };
|
||||
private final String app = "OpenNXapp";
|
||||
|
||||
private Hashtable<String,String> params;
|
||||
private String tmpDir = "";
|
||||
private String baseUrl = "";
|
||||
private String nxFileName = "";
|
||||
private String jarFileName = "";
|
||||
private String scrWidth;
|
||||
private String scrHeight;
|
||||
private String tunPort = "";
|
||||
|
||||
public void start() {
|
||||
try {
|
||||
tmpDir = System.getProperty("java.io.tmpdir") + File.separator;
|
||||
nxFileName = tmpDir + UUID.randomUUID().toString() + ".nxs";
|
||||
jarFileName = tmpDir + UUID.randomUUID().toString() + ".jar";
|
||||
|
||||
// Notifies to broker the hostname/ip
|
||||
util.notifyHostname(baseUrl, params.get("is"));
|
||||
|
||||
String width = params.get("width");
|
||||
String height = params.get("height");
|
||||
boolean fullScreen = false;
|
||||
|
||||
if( width.equals("-1"))
|
||||
{
|
||||
width = scrWidth;
|
||||
height = scrHeight;
|
||||
fullScreen = true;
|
||||
}
|
||||
|
||||
if( downloadJar() == false)
|
||||
return;
|
||||
tunPort = Integer.toString(FreePortFinder.findFreePort());
|
||||
|
||||
NxFile nx = new NxFile(fullScreen, width, height);
|
||||
nx.host = "127.0.0.1";
|
||||
nx.port = tunPort;
|
||||
nx.username = params.get("user");
|
||||
nx.password = params.get("pass");
|
||||
nx.cachedisk = params.get("cacheDisk");
|
||||
nx.cachemem = params.get("cacheMem");
|
||||
nx.desktop = params.get("session");
|
||||
nx.linkSpeed = params.get("connection");
|
||||
|
||||
try {
|
||||
nx.saveFile( nxFileName );
|
||||
} catch (IOException e) {
|
||||
javax.swing.JOptionPane.showMessageDialog(null, "Can't save nx temporal file: " + e.getMessage());
|
||||
e.printStackTrace();
|
||||
return;
|
||||
}
|
||||
|
||||
String execPath = "";
|
||||
|
||||
for(int i = 0; i < paths.length; i++ )
|
||||
{
|
||||
File f = new File(paths[i] + app);
|
||||
if( f.exists() )
|
||||
{
|
||||
execPath = paths[i] + app;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if( execPath.length() == 0 )
|
||||
{
|
||||
javax.swing.JOptionPane.showMessageDialog(null, "Can't find OpenNXapp client.\nShould be at /Applications/NX Client for OSX.app/Contents/MacOS/\nPlease, install it");
|
||||
System.err.println("Can't find nxclient.");
|
||||
return;
|
||||
}
|
||||
|
||||
executeTunnel(execPath);
|
||||
|
||||
} catch (Exception e) {
|
||||
javax.swing.JOptionPane.showMessageDialog(null,"Exception at applet:\n" + e.getMessage());
|
||||
e.printStackTrace();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
public void init() {
|
||||
}
|
||||
|
||||
public void destroy() {
|
||||
}
|
||||
|
||||
private void executeTunnel(String nxCmd) throws IOException
|
||||
{
|
||||
String java = System.getProperty("java.home") + File.separator + "bin" + File.separator + "java";
|
||||
String [] exec = { java, "-jar", jarFileName, tunPort, nxCmd, "--session", nxFileName, "--autologin", "--killerrors" };
|
||||
ProcessBuilder pb = new ProcessBuilder( exec );
|
||||
Map<String,String> env = pb.environment();
|
||||
env.put("TPARAMS", params.get("tun"));
|
||||
System.out.println("TPARAMS: " + params.get("tun"));
|
||||
for (String str : exec) {
|
||||
System.out.print(str + " ");
|
||||
}
|
||||
System.out.println();
|
||||
pb.start();
|
||||
|
||||
}
|
||||
|
||||
public void setParameters(Hashtable<String, String> parameters, String urlBase,
|
||||
int screenWidth, int screenHeight) {
|
||||
params = parameters;
|
||||
baseUrl = urlBase;
|
||||
scrWidth = Integer.toString(screenWidth);
|
||||
scrHeight = Integer.toString(screenHeight);
|
||||
|
||||
}
|
||||
|
||||
private boolean downloadJar()
|
||||
{
|
||||
return util.download(baseUrl, "2", jarFileName);
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -1,122 +0,0 @@
|
||||
package es.virtualcable.nx;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
public class NXPassword {
|
||||
// Encoding method extracted from nomachine web site:
|
||||
// http://www.nomachine.com/ar/view.php?ar_id=AR01C00125
|
||||
|
||||
private static final int numValidCharList = 85;
|
||||
private static final String dummyString = "{{{{";
|
||||
private static final char[] validCharList = {
|
||||
'!', '#', '$', '%', '&', '(', ')', '*', '+', '-',
|
||||
'.', '0', '1', '2', '3', '4', '5', '6', '7', '8',
|
||||
'9', ':', ';', '<', '>', '?', '@', 'A', 'B', 'C',
|
||||
'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
|
||||
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
|
||||
'X', 'Y', 'Z', '[', ']', '_', 'a', 'b', 'c', 'd',
|
||||
'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
|
||||
'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
|
||||
'y', 'z', '{', '|', '}'
|
||||
};
|
||||
|
||||
private static StringBuilder encodePassword(String p)
|
||||
{
|
||||
StringBuilder sPass = new StringBuilder(":");
|
||||
|
||||
if (p.length() == 0)
|
||||
return new StringBuilder("");
|
||||
|
||||
for (int i = 0; i < p.length(); i++)
|
||||
{
|
||||
char c = (char)p.charAt(i);
|
||||
sPass.append(c+i+1).append(":");
|
||||
}
|
||||
|
||||
return sPass;
|
||||
}
|
||||
|
||||
private static int findCharInList(char c)
|
||||
{
|
||||
int i = -1;
|
||||
|
||||
for (int j = 0; j < numValidCharList; j++)
|
||||
{
|
||||
if (validCharList[j] == c)
|
||||
{
|
||||
i = j;
|
||||
return i;
|
||||
}
|
||||
}
|
||||
|
||||
return i;
|
||||
}
|
||||
|
||||
private static char getRandomValidCharFromList()
|
||||
{
|
||||
// int k = (int)(java.lang.System.currentTimeMillis() % 60);
|
||||
int k = 0;
|
||||
return validCharList[k];
|
||||
}
|
||||
|
||||
public static String scrambleString(String s)
|
||||
{
|
||||
StringBuilder pass = new StringBuilder();
|
||||
|
||||
if (s == null || s.length() == 0)
|
||||
return s;
|
||||
|
||||
StringBuilder str = encodePassword(s);
|
||||
|
||||
if (str.length() < 32)
|
||||
str.append(dummyString);
|
||||
|
||||
pass.append(str.reverse());
|
||||
|
||||
if (pass.length() < 32)
|
||||
pass.append(dummyString);
|
||||
|
||||
int k = getRandomValidCharFromList();
|
||||
int l = k + pass.length() - 2;
|
||||
|
||||
pass.insert(0, (char)k);
|
||||
|
||||
for (int i1 = 1; i1 < (int)pass.length(); i1++)
|
||||
{
|
||||
int j = findCharInList(pass.charAt(i1));
|
||||
|
||||
if (j == -1)
|
||||
return s;
|
||||
|
||||
int i = (j + l * (i1 + 1)) % numValidCharList;
|
||||
|
||||
pass.setCharAt(i1,validCharList[i]);
|
||||
}
|
||||
|
||||
char c = (char)(getRandomValidCharFromList() + 2);
|
||||
pass.append(c);
|
||||
|
||||
// Convert entities
|
||||
HashMap<Character, String> replacements = new HashMap<Character, String>();
|
||||
replacements.put('&', "&");
|
||||
replacements.put('<', "<");
|
||||
replacements.put('"', """);
|
||||
replacements.put('\'', "'");
|
||||
// And convert $ to \$
|
||||
replacements.put('$', "\\$");
|
||||
|
||||
StringBuilder result = new StringBuilder();
|
||||
for( int i = 0; i < pass.length(); i++ )
|
||||
{
|
||||
c = pass.charAt(i);
|
||||
if( replacements.containsKey(c) )
|
||||
result.append(replacements.get(c));
|
||||
else
|
||||
result.append(c);
|
||||
}
|
||||
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -1,192 +0,0 @@
|
||||
package es.virtualcable.nx;
|
||||
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
|
||||
public class NxFile {
|
||||
private static final String EMPTY_PASSWORD = "EMPTY_PASSWORD";
|
||||
|
||||
public boolean fullScreen;
|
||||
public String width;
|
||||
public String height;
|
||||
public String cachemem = "4";
|
||||
public String cachedisk = "32";
|
||||
public String keyboardLayout = "";
|
||||
public String linkSpeed = "wan";
|
||||
public String host = "";
|
||||
public String port = "";
|
||||
public String username = "";
|
||||
public String password = "";
|
||||
public String desktop = "gnome";
|
||||
|
||||
public NxFile(boolean fullScreen, String width, String height) {
|
||||
this.fullScreen = fullScreen;
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
}
|
||||
|
||||
public void saveFile(String fname) throws IOException {
|
||||
String rememberPass = "true";
|
||||
String pass = NXPassword.scrambleString(password);
|
||||
if( pass == "" )
|
||||
{
|
||||
rememberPass = "false";
|
||||
pass = EMPTY_PASSWORD;
|
||||
}
|
||||
String resolution = width + "x" + height;
|
||||
if( fullScreen )
|
||||
resolution = "fullscreen";
|
||||
|
||||
String save = NXTemplate.replaceAll("\\{CACHEMEM\\}", cachemem);
|
||||
save = save.replaceAll("\\{CACHEDISK\\}", cachedisk);
|
||||
save = save.replaceAll("\\{KEYLAYOUT\\}", keyboardLayout);
|
||||
save = save.replaceAll("\\{LINKSPEED\\}", linkSpeed);
|
||||
save = save.replaceAll("\\{REMEMBERPASS\\}", rememberPass);
|
||||
save = save.replaceAll("\\{RESOLUTION\\}", resolution);
|
||||
save = save.replaceAll("\\{WIDTH\\}", width);
|
||||
save = save.replaceAll("\\{HEIGHT\\}", height);
|
||||
save = save.replaceAll("\\{HOST\\}", host);
|
||||
save = save.replaceAll("\\{PORT\\}", port);
|
||||
save = save.replaceAll("\\{DESKTOP\\}", desktop);
|
||||
save = save.replaceAll("\\{USERNAME\\}", username);
|
||||
save = save.replaceAll("\\{PASSWORD\\}", pass);
|
||||
|
||||
FileWriter fstream = new FileWriter(fname);
|
||||
PrintWriter out = new PrintWriter(fstream);
|
||||
out.write(save);
|
||||
out.close();
|
||||
}
|
||||
|
||||
public static void main(String [] args)
|
||||
{
|
||||
NxFile nx = new NxFile(true, "1024", "768");
|
||||
try {
|
||||
nx.saveFile("");
|
||||
} catch( Exception e )
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
// NX Template used to generate file
|
||||
private static final String NXTemplate =
|
||||
"<!DOCTYPE NXClientSettings>\n" +
|
||||
"<NXClientSettings application=\"nxclient\" version=\"1.3\" >\n" +
|
||||
" <group name=\"Advanced\" >\n" +
|
||||
" <option key=\"Cache size\" value=\"{CACHEMEM}\" />\n"+
|
||||
" <option key=\"Cache size on disk\" value=\"{CACHEDISK}\" />\n" +
|
||||
" <option key=\"Current keyboard\" value=\"true\" />\n" +
|
||||
" <option key=\"Custom keyboard layout\" value=\"{KEYLAYOUT}\" />\n" +
|
||||
" <option key=\"Disable ZLIB stream compression\" value=\"false\" />\n" +
|
||||
" <option key=\"Disable TCP no-delay\" value=\"false\" />\n"+
|
||||
" <option key=\"Disable deferred updates\" value=\"false\" />\n" +
|
||||
" <option key=\"Enable HTTP proxy\" value=\"false\" />\n" +
|
||||
" <option key=\"Enable SSL encryption\" value=\"true\" />\n" +
|
||||
" <option key=\"Enable response time optimisations\" value=\"true\" />\n" +
|
||||
" <option key=\"Grab keyboard\" value=\"false\" />\n" +
|
||||
" <option key=\"HTTP proxy host\" value=\"\" />\n" +
|
||||
" <option key=\"HTTP proxy port\" value=\"8080\" />\n" +
|
||||
" <option key=\"HTTP proxy username\" value=\"\" />\n" +
|
||||
" <option key=\"Remember HTTP proxy password\" value=\"false\" />\n" +
|
||||
" <option key=\"Restore cache\" value=\"true\" />\n" +
|
||||
" <option key=\"StreamCompression\" value=\"\" />\n" +
|
||||
" </group>\n" +
|
||||
" <group name=\"Environment\" >\n" +
|
||||
" <option key=\"Font server host\" value=\"\" />\n" +
|
||||
" <option key=\"Font server port\" value=\"7100\" />\n" +
|
||||
" <option key=\"Use font server\" value=\"false\" />\n" +
|
||||
" </group>\n" +
|
||||
" <group name=\"General\" >\n" +
|
||||
" <option key=\"Automatic reconnect\" value=\"true\" />\n" +
|
||||
" <option key=\"Disable SHM\" value=\"false\" />\n" +
|
||||
" <option key=\"Disable emulate shared pixmaps\" value=\"false\" />\n" +
|
||||
" <option key=\"Link speed\" value=\"{LINKSPEED}\" />\n" +
|
||||
" <option key=\"Remember password\" value=\"{REMEMBERPASS}\" />\n" +
|
||||
" <option key=\"Resolution\" value=\"{RESOLUTION}\" />\n" +
|
||||
" <option key=\"Resolution width\" value=\"{WIDTH}\" />\n" +
|
||||
" <option key=\"Resolution height\" value=\"{HEIGHT}\" />\n" +
|
||||
" <option key=\"Server host\" value=\"{HOST}\" />\n" +
|
||||
" <option key=\"Server port\" value=\"{PORT}\" />\n" +
|
||||
" <option key=\"Session\" value=\"unix\" />\n" +
|
||||
" <option key=\"Desktop\" value=\"{DESKTOP}\" />\n" +
|
||||
" <option key=\"Use default image encoding\" value=\"1\" />\n" +
|
||||
" <option key=\"Use render\" value=\"false\" />\n" +
|
||||
" <option key=\"Use taint\" value=\"true\" />\n" +
|
||||
" <option key=\"Virtual desktop\" value=\"false\" />\n" +
|
||||
" <option key=\"XAgent encoding\" value=\"true\" />\n" +
|
||||
" <option key=\"displaySaveOnExit\" value=\"true\" />\n" +
|
||||
" <option key=\"xdm broadcast port\" value=\"177\" />\n" +
|
||||
" <option key=\"xdm list host\" value=\"localhost\" />\n" +
|
||||
" <option key=\"xdm list port\" value=\"177\" />\n" +
|
||||
" <option key=\"xdm mode\" value=\"server decide\" />\n" +
|
||||
" <option key=\"xdm query host\" value=\"localhost\" />\n" +
|
||||
" <option key=\"xdm query port\" value=\"177\" />\n" +
|
||||
" </group>\n" +
|
||||
" <group name=\"Images\" >\n" +
|
||||
" <option key=\"Disable JPEG Compression\" value=\"0\" />\n" +
|
||||
" <option key=\"Disable all image optimisations\" value=\"false\" />\n" +
|
||||
" <option key=\"Disable backingstore\" value=\"false\" />\n" +
|
||||
" <option key=\"Disable composite\" value=\"false\" />\n" +
|
||||
" <option key=\"Image Compression Type\" value=\"3\" />\n" +
|
||||
" <option key=\"Image Encoding Type\" value=\"0\" />\n" +
|
||||
" <option key=\"Image JPEG Encoding\" value=\"false\" />\n" +
|
||||
" <option key=\"JPEG Quality\" value=\"6\" />\n" +
|
||||
" <option key=\"RDP Image Encoding\" value=\"3\" />\n" +
|
||||
" <option key=\"RDP JPEG Quality\" value=\"6\" />\n" +
|
||||
" <option key=\"RDP optimization for low-bandwidth link\" value=\"false\" />\n" +
|
||||
" <option key=\"Reduce colors to\" value=\"\" />\n" +
|
||||
" <option key=\"Use PNG Compression\" value=\"true\" />\n" +
|
||||
" <option key=\"VNC JPEG Quality\" value=\"6\" />\n" +
|
||||
" <option key=\"VNC images compression\" value=\"3\" />\n" +
|
||||
" </group>\n" +
|
||||
" <group name=\"Login\" >\n" +
|
||||
" <option key=\"User\" value=\"{USERNAME}\" />\n" +
|
||||
" <option key=\"Auth\" value=\"{PASSWORD}\" />\n" +
|
||||
" <option key=\"Guest Mode\" value=\"false\" />\n" +
|
||||
" <option key=\"Guest password\" value=\"\" />\n" +
|
||||
" <option key=\"Guest username\" value=\"\" />\n" +
|
||||
" <option key=\"Login Method\" value=\"nx\" />\n" +
|
||||
" <option key=\"Public Key\" value=\"-----BEGIN DSA PRIVATE KEY-----\n" +
|
||||
"MIIBuwIBAAKBgQCXv9AzQXjxvXWC1qu3CdEqskX9YomTfyG865gb4D02ZwWuRU/9\n" +
|
||||
"C3I9/bEWLdaWgJYXIcFJsMCIkmWjjeSZyTmeoypI1iLifTHUxn3b7WNWi8AzKcVF\n" +
|
||||
"aBsBGiljsop9NiD1mEpA0G+nHHrhvTXz7pUvYrsrXcdMyM6rxqn77nbbnwIVALCi\n" +
|
||||
"xFdHZADw5KAVZI7r6QatEkqLAoGBAI4L1TQGFkq5xQ/nIIciW8setAAIyrcWdK/z\n" +
|
||||
"5/ZPeELdq70KDJxoLf81NL/8uIc4PoNyTRJjtT3R4f8Az1TsZWeh2+ReCEJxDWgG\n" +
|
||||
"fbk2YhRqoQTtXPFsI4qvzBWct42WonWqyyb1bPBHk+JmXFscJu5yFQ+JUVNsENpY\n" +
|
||||
"+Gkz3HqTAoGANlgcCuA4wrC+3Cic9CFkqiwO/Rn1vk8dvGuEQqFJ6f6LVfPfRTfa\n" +
|
||||
"QU7TGVLk2CzY4dasrwxJ1f6FsT8DHTNGnxELPKRuLstGrFY/PR7KeafeFZDf+fJ3\n" +
|
||||
"mbX5nxrld3wi5titTnX+8s4IKv29HJguPvOK/SI7cjzA+SqNfD7qEo8CFDIm1xRf\n" +
|
||||
"8xAPsSKs6yZ6j1FNklfu\n" +
|
||||
"-----END DSA PRIVATE KEY-----\n" +
|
||||
"\" />\n" +
|
||||
" </group>\n" +
|
||||
" <group name=\"Services\" >\n" +
|
||||
" <option key=\"Audio\" value=\"true\" />\n" +
|
||||
" <option key=\"IPPPort\" value=\"631\" />\n" +
|
||||
" <option key=\"IPPPrinting\" value=\"false\" />\n" +
|
||||
" <option key=\"Shares\" value=\"false\" />\n" +
|
||||
" </group>\n" +
|
||||
" <group name=\"VNC Session\" >\n" +
|
||||
" <option key=\"Display\" value=\"0\" />\n" +
|
||||
" <option key=\"Remember\" value=\"false\" />\n" +
|
||||
" <option key=\"Server\" value=\"\" />\n" +
|
||||
" </group>\n" +
|
||||
" <group name=\"Windows Session\" >\n" +
|
||||
" <option key=\"Application\" value=\"\" />\n" +
|
||||
" <option key=\"Authentication\" value=\"2\" />\n" +
|
||||
" <option key=\"Color Depth\" value=\"16\" />\n" +
|
||||
" <option key=\"Domain\" value=\"\" />\n" +
|
||||
" <option key=\"Image Cache\" value=\"true\" />\n" +
|
||||
" <option key=\"Password\" value=\"EMPTY_PASSWORD\" />\n" +
|
||||
" <option key=\"Remember\" value=\"true\" />\n" +
|
||||
" <option key=\"Run application\" value=\"false\" />\n" +
|
||||
" <option key=\"Server\" value=\"\" />\n" +
|
||||
" <option key=\"User\" value=\"\" />\n" +
|
||||
" </group>\n" +
|
||||
" <group name=\"share chosen\" >\n" +
|
||||
" <option key=\"Share number\" value=\"0\" />\n" +
|
||||
" </group>\n" +
|
||||
"</NXClientSettings>";
|
||||
|
||||
}
|
@ -1,15 +0,0 @@
|
||||
package es.virtualcable.nx;
|
||||
|
||||
import java.util.Hashtable;
|
||||
|
||||
public interface OsApplet {
|
||||
|
||||
void setParameters(Hashtable<String,String> parameters, String urlBase, int screenWidth, int screenHeight);
|
||||
|
||||
void init();
|
||||
|
||||
void start();
|
||||
|
||||
void destroy();
|
||||
|
||||
}
|
@ -1,127 +0,0 @@
|
||||
package es.virtualcable.nx;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.Hashtable;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
import es.virtualcable.nx.FreePortFinder;
|
||||
import es.virtualcable.nx.util;
|
||||
import es.virtualcable.windows.WinRegistry;
|
||||
|
||||
public class WindowsApplet implements OsApplet {
|
||||
|
||||
private Hashtable<String,String> params;
|
||||
private String tmpDir = "";
|
||||
private String baseUrl = "";
|
||||
private String nxFileName = "";
|
||||
private String jarFileName = "";
|
||||
private String scrWidth;
|
||||
private String scrHeight;
|
||||
private String tunPort = "";
|
||||
|
||||
|
||||
public void setParameters(Hashtable<String, String> parameters, String urlBase,
|
||||
int screenWidth, int screenHeight) {
|
||||
params = parameters;
|
||||
baseUrl = urlBase;
|
||||
scrWidth = Integer.toString(screenWidth);
|
||||
scrHeight = Integer.toString(screenHeight);
|
||||
}
|
||||
|
||||
public void start() {
|
||||
try {
|
||||
tmpDir = System.getProperty("java.io.tmpdir") + File.separator;
|
||||
nxFileName = tmpDir + UUID.randomUUID().toString() + ".nxs";
|
||||
jarFileName = tmpDir + UUID.randomUUID().toString() + ".jar";
|
||||
|
||||
// Notifies to broker the hostname/ip
|
||||
util.notifyHostname(baseUrl, params.get("is"));
|
||||
|
||||
String width = params.get("width");
|
||||
String height = params.get("height");
|
||||
boolean fullScreen = false;
|
||||
|
||||
if( width.equals("-1"))
|
||||
{
|
||||
width = scrWidth;
|
||||
height = scrHeight;
|
||||
fullScreen = true;
|
||||
}
|
||||
|
||||
if( downloadJar() == false)
|
||||
return;
|
||||
tunPort = Integer.toString(FreePortFinder.findFreePort());
|
||||
|
||||
NxFile nx = new NxFile(fullScreen, width, height);
|
||||
nx.host = "127.0.0.1";
|
||||
nx.port = tunPort;
|
||||
nx.username = params.get("user");
|
||||
nx.password = params.get("pass");
|
||||
nx.cachedisk = params.get("cacheDisk");
|
||||
nx.cachemem = params.get("cacheMem");
|
||||
nx.desktop = params.get("session");
|
||||
nx.linkSpeed = params.get("connection");
|
||||
|
||||
try {
|
||||
nx.saveFile( nxFileName );
|
||||
} catch (IOException e) {
|
||||
javax.swing.JOptionPane.showMessageDialog(null, "Can't save nx temporal file: " + e.getMessage());
|
||||
e.printStackTrace();
|
||||
return;
|
||||
}
|
||||
|
||||
String cmd = WinRegistry.readString(WinRegistry.HKEY_CURRENT_USER, "Software\\Classes\\NXClient.session\\shell\\open\\command", "");
|
||||
if ( null == cmd )
|
||||
{
|
||||
// "C:\Program Files (x86)\OpenNX\bin\opennx.exe" --session "%1"
|
||||
cmd = WinRegistry.readString(WinRegistry.HKEY_CURRENT_USER, "Software\\InnoviData\\OpenNX\\Config", "SystemNxDir");
|
||||
if( null != cmd )
|
||||
{
|
||||
cmd = "\"" + cmd + "\\bin\\opennx.exe\" --autologin --session \"%1\"";
|
||||
}
|
||||
}
|
||||
|
||||
if ( null == cmd )
|
||||
javax.swing.JOptionPane.showMessageDialog(null, "Can't find Nomachine client. Please, install it");
|
||||
else
|
||||
{
|
||||
nxFileName = nxFileName.replace("\\", "\\\\");
|
||||
System.out.println(nxFileName);
|
||||
cmd = cmd.replaceAll("%1", nxFileName);
|
||||
executeTunnel(cmd);
|
||||
|
||||
}
|
||||
} catch (Exception e) {
|
||||
javax.swing.JOptionPane.showMessageDialog(null,"Exception at applet:\n" + e.getMessage());
|
||||
e.printStackTrace();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
public void init() {
|
||||
}
|
||||
|
||||
public void destroy() {
|
||||
}
|
||||
|
||||
private void executeTunnel(String nxCmd) throws IOException
|
||||
{
|
||||
String java = System.getProperty("java.home") + File.separator + "bin" + File.separator + "java.exe";
|
||||
String cmd = "\"" + java + "\" -jar " + jarFileName + " " + tunPort + " " + nxCmd;
|
||||
ProcessBuilder pb = new ProcessBuilder( util.splitCommandLine(cmd ));
|
||||
Map<String,String> env = pb.environment();
|
||||
env.put("TPARAMS", params.get("tun"));
|
||||
System.out.println("TPARAMS: " + params.get("tun"));
|
||||
System.out.println(cmd);
|
||||
pb.start();
|
||||
|
||||
}
|
||||
|
||||
private boolean downloadJar()
|
||||
{
|
||||
return util.download(baseUrl, "2", jarFileName);
|
||||
}
|
||||
|
||||
}
|
@ -1,130 +0,0 @@
|
||||
package es.virtualcable.nx;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.URLEncoder;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class util {
|
||||
|
||||
public static boolean download(String baseUrl, String id, String outputFileName)
|
||||
{
|
||||
try {
|
||||
java.net.URL u = new java.net.URL(baseUrl + id);
|
||||
java.net.URLConnection uc = u.openConnection();
|
||||
String contentType = uc.getContentType();
|
||||
int contentLength = uc.getContentLength();
|
||||
if (contentType.startsWith("text/") || contentLength == -1) {
|
||||
throw new IOException("This is not a binary file.");
|
||||
}
|
||||
InputStream raw = uc.getInputStream();
|
||||
InputStream in = new BufferedInputStream(raw);
|
||||
byte[] data = new byte[contentLength];
|
||||
int bytesRead = 0;
|
||||
int offset = 0;
|
||||
while (offset < contentLength) {
|
||||
bytesRead = in.read(data, offset, data.length - offset);
|
||||
if (bytesRead == -1)
|
||||
break;
|
||||
offset += bytesRead;
|
||||
}
|
||||
in.close();
|
||||
|
||||
if (offset != contentLength) {
|
||||
throw new IOException("Only read " + offset + " bytes; Expected " + contentLength + " bytes");
|
||||
}
|
||||
|
||||
java.io.FileOutputStream out = new java.io.FileOutputStream(outputFileName);
|
||||
out.write(data);
|
||||
out.flush();
|
||||
out.close();
|
||||
|
||||
} catch(Exception e) {
|
||||
System.out.println("Unable to download file, already present or network error? " + e.getMessage());
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
public static String getUrl(String url) {
|
||||
try {
|
||||
java.net.URL u = new java.net.URL(url);
|
||||
BufferedReader in = new BufferedReader(new InputStreamReader(u.openStream()));
|
||||
StringBuilder data = new StringBuilder();
|
||||
|
||||
String inputLine;
|
||||
while ((inputLine = in.readLine()) != null) {
|
||||
data.append(inputLine);
|
||||
data.append("\n");
|
||||
}
|
||||
|
||||
in.close();
|
||||
return data.toString();
|
||||
|
||||
} catch(Exception e) {
|
||||
System.out.println("Unable to get url. Network error? " + e.getMessage());
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
public static void notifyHostname(String baseUrl, String serviceId) {
|
||||
String[] urlComponents = baseUrl.split("/");
|
||||
String hostname;
|
||||
String ip;
|
||||
String url="";
|
||||
|
||||
try {
|
||||
hostname = java.net.InetAddress.getLocalHost().getHostName();
|
||||
ip = java.net.InetAddress.getLocalHost().getHostAddress();
|
||||
} catch(Exception e) {
|
||||
hostname = "unknown";
|
||||
ip = "0.0.0.0";
|
||||
}
|
||||
|
||||
try {
|
||||
// An url is "http[s]://.....:/,
|
||||
url = urlComponents[0] + "//" + urlComponents[2] + "/sernotify/" + serviceId + "/hostname?hostname="+URLEncoder.encode(hostname)+"&ip="+URLEncoder.encode(ip);
|
||||
getUrl(url);
|
||||
} catch(Exception e) {
|
||||
System.out.println("Unable to get url? " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static ArrayList<String> splitCommandLine(String cmd) {
|
||||
boolean inQuotes = false;
|
||||
ArrayList<String> res = new ArrayList<String>();
|
||||
String tmp = "";
|
||||
for( char c : cmd.toCharArray() ) {
|
||||
if( c == '"' ) {
|
||||
if( inQuotes ) {
|
||||
res.add(tmp);
|
||||
tmp = "";
|
||||
inQuotes = false;
|
||||
continue;
|
||||
}
|
||||
inQuotes = true;
|
||||
continue;
|
||||
}
|
||||
if( c == ' ' && inQuotes == false) {
|
||||
if( tmp.length() > 0 ) {
|
||||
res.add(tmp);
|
||||
tmp = "";
|
||||
continue;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
tmp += c;
|
||||
}
|
||||
if( tmp.length() > 0 )
|
||||
res.add(tmp);
|
||||
return res;
|
||||
}
|
||||
|
||||
}
|
@ -1,392 +0,0 @@
|
||||
package es.virtualcable.windows;
|
||||
|
||||
/* Code borrowed from http://stackoverflow.com/questions/62289/read-write-to-windows-registry-using-java. */
|
||||
/* Original author don't found, if someone found him, please note it here */
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.prefs.Preferences;
|
||||
|
||||
public class WinRegistry {
|
||||
public static final int HKEY_CURRENT_USER = 0x80000001;
|
||||
public static final int HKEY_LOCAL_MACHINE = 0x80000002;
|
||||
public static final int REG_SUCCESS = 0;
|
||||
public static final int REG_NOTFOUND = 2;
|
||||
public static final int REG_ACCESSDENIED = 5;
|
||||
|
||||
private static final int KEY_ALL_ACCESS = 0xf003f;
|
||||
private static final int KEY_READ = 0x20019;
|
||||
private static Preferences userRoot = Preferences.userRoot();
|
||||
private static Preferences systemRoot = Preferences.systemRoot();
|
||||
|
||||
private static Class<? extends Preferences> userClass = userRoot.getClass();
|
||||
private static Method regOpenKey = null;
|
||||
private static Method regCloseKey = null;
|
||||
private static Method regQueryValueEx = null;
|
||||
private static Method regEnumValue = null;
|
||||
private static Method regQueryInfoKey = null;
|
||||
private static Method regEnumKeyEx = null;
|
||||
private static Method regCreateKeyEx = null;
|
||||
private static Method regSetValueEx = null;
|
||||
private static Method regDeleteKey = null;
|
||||
private static Method regDeleteValue = null;
|
||||
|
||||
static {
|
||||
try {
|
||||
regOpenKey = userClass.getDeclaredMethod("WindowsRegOpenKey",
|
||||
new Class[] { int.class, byte[].class, int.class });
|
||||
regOpenKey.setAccessible(true);
|
||||
regCloseKey = userClass.getDeclaredMethod("WindowsRegCloseKey",
|
||||
new Class[] { int.class });
|
||||
regCloseKey.setAccessible(true);
|
||||
regQueryValueEx = userClass.getDeclaredMethod("WindowsRegQueryValueEx",
|
||||
new Class[] { int.class, byte[].class });
|
||||
regQueryValueEx.setAccessible(true);
|
||||
regEnumValue = userClass.getDeclaredMethod("WindowsRegEnumValue",
|
||||
new Class[] { int.class, int.class, int.class });
|
||||
regEnumValue.setAccessible(true);
|
||||
regQueryInfoKey = userClass.getDeclaredMethod("WindowsRegQueryInfoKey1",
|
||||
new Class[] { int.class });
|
||||
regQueryInfoKey.setAccessible(true);
|
||||
regEnumKeyEx = userClass.getDeclaredMethod(
|
||||
"WindowsRegEnumKeyEx", new Class[] { int.class, int.class,
|
||||
int.class });
|
||||
regEnumKeyEx.setAccessible(true);
|
||||
regCreateKeyEx = userClass.getDeclaredMethod(
|
||||
"WindowsRegCreateKeyEx", new Class[] { int.class,
|
||||
byte[].class });
|
||||
regCreateKeyEx.setAccessible(true);
|
||||
regSetValueEx = userClass.getDeclaredMethod(
|
||||
"WindowsRegSetValueEx", new Class[] { int.class,
|
||||
byte[].class, byte[].class });
|
||||
regSetValueEx.setAccessible(true);
|
||||
regDeleteValue = userClass.getDeclaredMethod(
|
||||
"WindowsRegDeleteValue", new Class[] { int.class,
|
||||
byte[].class });
|
||||
regDeleteValue.setAccessible(true);
|
||||
regDeleteKey = userClass.getDeclaredMethod(
|
||||
"WindowsRegDeleteKey", new Class[] { int.class,
|
||||
byte[].class });
|
||||
regDeleteKey.setAccessible(true);
|
||||
}
|
||||
catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private WinRegistry() { }
|
||||
|
||||
/**
|
||||
* Read a value from key and value name
|
||||
* @param hkey HKEY_CURRENT_USER/HKEY_LOCAL_MACHINE
|
||||
* @param key
|
||||
* @param valueName
|
||||
* @return the value
|
||||
* @throws IllegalArgumentException
|
||||
* @throws IllegalAccessException
|
||||
* @throws InvocationTargetException
|
||||
*/
|
||||
public static String readString(int hkey, String key, String valueName)
|
||||
throws IllegalArgumentException, IllegalAccessException,
|
||||
InvocationTargetException
|
||||
{
|
||||
if (hkey == HKEY_LOCAL_MACHINE) {
|
||||
return readString(systemRoot, hkey, key, valueName);
|
||||
}
|
||||
else if (hkey == HKEY_CURRENT_USER) {
|
||||
return readString(userRoot, hkey, key, valueName);
|
||||
}
|
||||
else {
|
||||
throw new IllegalArgumentException("hkey=" + hkey);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Read value(s) and value name(s) form given key
|
||||
* @param hkey HKEY_CURRENT_USER/HKEY_LOCAL_MACHINE
|
||||
* @param key
|
||||
* @return the value name(s) plus the value(s)
|
||||
* @throws IllegalArgumentException
|
||||
* @throws IllegalAccessException
|
||||
* @throws InvocationTargetException
|
||||
*/
|
||||
public static Map<String, String> readStringValues(int hkey, String key)
|
||||
throws IllegalArgumentException, IllegalAccessException,
|
||||
InvocationTargetException
|
||||
{
|
||||
if (hkey == HKEY_LOCAL_MACHINE) {
|
||||
return readStringValues(systemRoot, hkey, key);
|
||||
}
|
||||
else if (hkey == HKEY_CURRENT_USER) {
|
||||
return readStringValues(userRoot, hkey, key);
|
||||
}
|
||||
else {
|
||||
throw new IllegalArgumentException("hkey=" + hkey);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Read the value name(s) from a given key
|
||||
* @param hkey HKEY_CURRENT_USER/HKEY_LOCAL_MACHINE
|
||||
* @param key
|
||||
* @return the value name(s)
|
||||
* @throws IllegalArgumentException
|
||||
* @throws IllegalAccessException
|
||||
* @throws InvocationTargetException
|
||||
*/
|
||||
public static List<String> readStringSubKeys(int hkey, String key)
|
||||
throws IllegalArgumentException, IllegalAccessException,
|
||||
InvocationTargetException
|
||||
{
|
||||
if (hkey == HKEY_LOCAL_MACHINE) {
|
||||
return readStringSubKeys(systemRoot, hkey, key);
|
||||
}
|
||||
else if (hkey == HKEY_CURRENT_USER) {
|
||||
return readStringSubKeys(userRoot, hkey, key);
|
||||
}
|
||||
else {
|
||||
throw new IllegalArgumentException("hkey=" + hkey);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a key
|
||||
* @param hkey HKEY_CURRENT_USER/HKEY_LOCAL_MACHINE
|
||||
* @param key
|
||||
* @throws IllegalArgumentException
|
||||
* @throws IllegalAccessException
|
||||
* @throws InvocationTargetException
|
||||
*/
|
||||
public static void createKey(int hkey, String key)
|
||||
throws IllegalArgumentException, IllegalAccessException,
|
||||
InvocationTargetException
|
||||
{
|
||||
int [] ret;
|
||||
if (hkey == HKEY_LOCAL_MACHINE) {
|
||||
ret = createKey(systemRoot, hkey, key);
|
||||
regCloseKey.invoke(systemRoot, new Object[] { new Integer(ret[0]) });
|
||||
}
|
||||
else if (hkey == HKEY_CURRENT_USER) {
|
||||
ret = createKey(userRoot, hkey, key);
|
||||
regCloseKey.invoke(userRoot, new Object[] { new Integer(ret[0]) });
|
||||
}
|
||||
else {
|
||||
throw new IllegalArgumentException("hkey=" + hkey);
|
||||
}
|
||||
if (ret[1] != REG_SUCCESS) {
|
||||
throw new IllegalArgumentException("rc=" + ret[1] + " key=" + key);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Write a value in a given key/value name
|
||||
* @param hkey
|
||||
* @param key
|
||||
* @param valueName
|
||||
* @param value
|
||||
* @throws IllegalArgumentException
|
||||
* @throws IllegalAccessException
|
||||
* @throws InvocationTargetException
|
||||
*/
|
||||
public static void writeStringValue
|
||||
(int hkey, String key, String valueName, String value)
|
||||
throws IllegalArgumentException, IllegalAccessException,
|
||||
InvocationTargetException
|
||||
{
|
||||
if (hkey == HKEY_LOCAL_MACHINE) {
|
||||
writeStringValue(systemRoot, hkey, key, valueName, value);
|
||||
}
|
||||
else if (hkey == HKEY_CURRENT_USER) {
|
||||
writeStringValue(userRoot, hkey, key, valueName, value);
|
||||
}
|
||||
else {
|
||||
throw new IllegalArgumentException("hkey=" + hkey);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a given key
|
||||
* @param hkey
|
||||
* @param key
|
||||
* @throws IllegalArgumentException
|
||||
* @throws IllegalAccessException
|
||||
* @throws InvocationTargetException
|
||||
*/
|
||||
public static void deleteKey(int hkey, String key)
|
||||
throws IllegalArgumentException, IllegalAccessException,
|
||||
InvocationTargetException
|
||||
{
|
||||
int rc = -1;
|
||||
if (hkey == HKEY_LOCAL_MACHINE) {
|
||||
rc = deleteKey(systemRoot, hkey, key);
|
||||
}
|
||||
else if (hkey == HKEY_CURRENT_USER) {
|
||||
rc = deleteKey(userRoot, hkey, key);
|
||||
}
|
||||
if (rc != REG_SUCCESS) {
|
||||
throw new IllegalArgumentException("rc=" + rc + " key=" + key);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* delete a value from a given key/value name
|
||||
* @param hkey
|
||||
* @param key
|
||||
* @param value
|
||||
* @throws IllegalArgumentException
|
||||
* @throws IllegalAccessException
|
||||
* @throws InvocationTargetException
|
||||
*/
|
||||
public static void deleteValue(int hkey, String key, String value)
|
||||
throws IllegalArgumentException, IllegalAccessException,
|
||||
InvocationTargetException
|
||||
{
|
||||
int rc = -1;
|
||||
if (hkey == HKEY_LOCAL_MACHINE) {
|
||||
rc = deleteValue(systemRoot, hkey, key, value);
|
||||
}
|
||||
else if (hkey == HKEY_CURRENT_USER) {
|
||||
rc = deleteValue(userRoot, hkey, key, value);
|
||||
}
|
||||
if (rc != REG_SUCCESS) {
|
||||
throw new IllegalArgumentException("rc=" + rc + " key=" + key + " value=" + value);
|
||||
}
|
||||
}
|
||||
|
||||
// =====================
|
||||
|
||||
private static int deleteValue
|
||||
(Preferences root, int hkey, String key, String value)
|
||||
throws IllegalArgumentException, IllegalAccessException,
|
||||
InvocationTargetException
|
||||
{
|
||||
int[] handles = (int[]) regOpenKey.invoke(root, new Object[] {
|
||||
new Integer(hkey), toCstr(key), new Integer(KEY_ALL_ACCESS) });
|
||||
if (handles[1] != REG_SUCCESS) {
|
||||
return handles[1]; // can be REG_NOTFOUND, REG_ACCESSDENIED
|
||||
}
|
||||
int rc =((Integer) regDeleteValue.invoke(root,
|
||||
new Object[] {
|
||||
new Integer(handles[0]), toCstr(value)
|
||||
})).intValue();
|
||||
regCloseKey.invoke(root, new Object[] { new Integer(handles[0]) });
|
||||
return rc;
|
||||
}
|
||||
|
||||
private static int deleteKey(Preferences root, int hkey, String key)
|
||||
throws IllegalArgumentException, IllegalAccessException,
|
||||
InvocationTargetException
|
||||
{
|
||||
int rc =((Integer) regDeleteKey.invoke(root,
|
||||
new Object[] { new Integer(hkey), toCstr(key) })).intValue();
|
||||
return rc; // can REG_NOTFOUND, REG_ACCESSDENIED, REG_SUCCESS
|
||||
}
|
||||
|
||||
private static String readString(Preferences root, int hkey, String key, String value)
|
||||
throws IllegalArgumentException, IllegalAccessException,
|
||||
InvocationTargetException
|
||||
{
|
||||
int[] handles = (int[]) regOpenKey.invoke(root, new Object[] {
|
||||
new Integer(hkey), toCstr(key), new Integer(KEY_READ) });
|
||||
if (handles[1] != REG_SUCCESS) {
|
||||
return null;
|
||||
}
|
||||
byte[] valb = (byte[]) regQueryValueEx.invoke(root, new Object[] {
|
||||
new Integer(handles[0]), toCstr(value) });
|
||||
regCloseKey.invoke(root, new Object[] { new Integer(handles[0]) });
|
||||
return (valb != null ? new String(valb).trim() : null);
|
||||
}
|
||||
|
||||
private static Map<String,String> readStringValues
|
||||
(Preferences root, int hkey, String key)
|
||||
throws IllegalArgumentException, IllegalAccessException,
|
||||
InvocationTargetException
|
||||
{
|
||||
HashMap<String, String> results = new HashMap<String,String>();
|
||||
int[] handles = (int[]) regOpenKey.invoke(root, new Object[] {
|
||||
new Integer(hkey), toCstr(key), new Integer(KEY_READ) });
|
||||
if (handles[1] != REG_SUCCESS) {
|
||||
return null;
|
||||
}
|
||||
int[] info = (int[]) regQueryInfoKey.invoke(root,
|
||||
new Object[] { new Integer(handles[0]) });
|
||||
|
||||
int count = info[2]; // count
|
||||
int maxlen = info[3]; // value length max
|
||||
for(int index=0; index<count; index++) {
|
||||
byte[] name = (byte[]) regEnumValue.invoke(root, new Object[] {
|
||||
new Integer
|
||||
(handles[0]), new Integer(index), new Integer(maxlen + 1)});
|
||||
String value = readString(hkey, key, new String(name));
|
||||
results.put(new String(name).trim(), value);
|
||||
}
|
||||
regCloseKey.invoke(root, new Object[] { new Integer(handles[0]) });
|
||||
return results;
|
||||
}
|
||||
|
||||
private static List<String> readStringSubKeys
|
||||
(Preferences root, int hkey, String key)
|
||||
throws IllegalArgumentException, IllegalAccessException,
|
||||
InvocationTargetException
|
||||
{
|
||||
List<String> results = new ArrayList<String>();
|
||||
int[] handles = (int[]) regOpenKey.invoke(root, new Object[] {
|
||||
new Integer(hkey), toCstr(key), new Integer(KEY_READ)
|
||||
});
|
||||
if (handles[1] != REG_SUCCESS) {
|
||||
return null;
|
||||
}
|
||||
int[] info = (int[]) regQueryInfoKey.invoke(root,
|
||||
new Object[] { new Integer(handles[0]) });
|
||||
|
||||
int count = info[2]; // count
|
||||
int maxlen = info[3]; // value length max
|
||||
for(int index=0; index<count; index++) {
|
||||
byte[] name = (byte[]) regEnumKeyEx.invoke(root, new Object[] {
|
||||
new Integer
|
||||
(handles[0]), new Integer(index), new Integer(maxlen + 1)
|
||||
});
|
||||
results.add(new String(name).trim());
|
||||
}
|
||||
regCloseKey.invoke(root, new Object[] { new Integer(handles[0]) });
|
||||
return results;
|
||||
}
|
||||
|
||||
private static int [] createKey(Preferences root, int hkey, String key)
|
||||
throws IllegalArgumentException, IllegalAccessException,
|
||||
InvocationTargetException
|
||||
{
|
||||
return (int[]) regCreateKeyEx.invoke(root,
|
||||
new Object[] { new Integer(hkey), toCstr(key) });
|
||||
}
|
||||
|
||||
private static void writeStringValue
|
||||
(Preferences root, int hkey, String key, String valueName, String value)
|
||||
throws IllegalArgumentException, IllegalAccessException,
|
||||
InvocationTargetException
|
||||
{
|
||||
int[] handles = (int[]) regOpenKey.invoke(root, new Object[] {
|
||||
new Integer(hkey), toCstr(key), new Integer(KEY_ALL_ACCESS) });
|
||||
|
||||
regSetValueEx.invoke(root,
|
||||
new Object[] {
|
||||
new Integer(handles[0]), toCstr(valueName), toCstr(value)
|
||||
});
|
||||
regCloseKey.invoke(root, new Object[] { new Integer(handles[0]) });
|
||||
}
|
||||
|
||||
// utility
|
||||
private static byte[] toCstr(String str) {
|
||||
byte[] result = new byte[str.length() + 1];
|
||||
|
||||
for (int i = 0; i < str.length(); i++) {
|
||||
result[i] = (byte) str.charAt(i);
|
||||
}
|
||||
result[str.length()] = 0;
|
||||
return result;
|
||||
}
|
||||
}
|
@ -1,5 +0,0 @@
|
||||
Manifest-Version: 1.0
|
||||
Sealed: true
|
||||
Permissions: all-permissions
|
||||
Codebase: *
|
||||
Application-Name: UDS TunNX Connector
|
@ -1,6 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="src" path="src"/>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
|
||||
<classpathentry kind="output" path="bin"/>
|
||||
</classpath>
|
2
rdptransport/java/.gitignore
vendored
2
rdptransport/java/.gitignore
vendored
@ -1,2 +0,0 @@
|
||||
/bin/
|
||||
/jar/
|
@ -1,17 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>rdptransport</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
@ -1,21 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
||||
<jardesc>
|
||||
<jar path="rdptransport/jar/rdp.jar"/>
|
||||
<options buildIfNeeded="true" compress="true" descriptionLocation="/rdptransport/descrdp.jardesc" exportErrors="true" exportWarnings="true" includeDirectoryEntries="true" overwrite="true" saveDescription="true" storeRefactorings="false" useSourceFolders="false"/>
|
||||
<storedRefactorings deprecationInfo="true" structuralOnly="false"/>
|
||||
<selectedProjects/>
|
||||
<manifest generateManifest="false" manifestLocation="/rdptransport/src/manifest" manifestVersion="1.0" reuseManifest="true" saveManifest="true" usesManifest="true">
|
||||
<sealing sealJar="true">
|
||||
<packagesToSeal>
|
||||
<package handleIdentifier="=rdp/src<com.virtualcable.rdp"/>
|
||||
<package handleIdentifier="=rdp/src<net.sourceforge.jdpapi"/>
|
||||
</packagesToSeal>
|
||||
<packagesToUnSeal/>
|
||||
</sealing>
|
||||
</manifest>
|
||||
<selectedElements exportClassFiles="true" exportJavaFiles="false" exportOutputFolder="false">
|
||||
<javaElement handleIdentifier="=rdptransport/src<"/>
|
||||
<javaElement handleIdentifier="=rdptransport/src<net.sourceforge.jdpapi"/>
|
||||
<javaElement handleIdentifier="=rdptransport/src<es.virtualcable.rdp"/>
|
||||
</selectedElements>
|
||||
</jardesc>
|
@ -1,136 +0,0 @@
|
||||
|
||||
import java.applet.*;
|
||||
import java.awt.*;
|
||||
import java.security.AccessController;
|
||||
import java.security.PrivilegedAction;
|
||||
import java.util.Hashtable;
|
||||
import es.virtualcable.rdp.LinuxApplet;
|
||||
import es.virtualcable.rdp.OsApplet;
|
||||
import es.virtualcable.rdp.WindowsApplet;
|
||||
import es.virtualcable.rdp.MacApplet;
|
||||
|
||||
public class RdpApplet extends Applet {
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static final long serialVersionUID = -7930346560654300533L;
|
||||
//private int width;
|
||||
//private int height;
|
||||
private OsApplet applet;
|
||||
private String appletStr = "UDS RDP Connector";
|
||||
|
||||
public void init() {
|
||||
//width = getSize().width;
|
||||
//height = getSize().height;
|
||||
setBackground(Color.lightGray);
|
||||
|
||||
try {
|
||||
AccessController.doPrivileged(new PrivilegedAction<Object>() {
|
||||
public Object run() {
|
||||
try {
|
||||
String os = System.getProperty("os.name");
|
||||
if( os.startsWith("Windows") )
|
||||
applet = new WindowsApplet();
|
||||
else if( os.startsWith("Linux"))
|
||||
applet = new LinuxApplet();
|
||||
else if (os.startsWith("Mac"))
|
||||
applet = new MacApplet();
|
||||
else
|
||||
throw new Exception("Invalid os!!!");
|
||||
|
||||
Hashtable<String,String> params = parseParams(unscramble(getParameter("data")));
|
||||
String baseUrl = getCodeBase().toString();
|
||||
|
||||
if( params.get("tun") != null )
|
||||
appletStr = "UDS RDP Tunnel Connector";
|
||||
|
||||
Dimension scrSize = Toolkit.getDefaultToolkit().getScreenSize();
|
||||
|
||||
applet.setParameters(params, baseUrl, scrSize.width, scrSize.height);
|
||||
applet.init();
|
||||
|
||||
return null; // nothing to return
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
});
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void start() {
|
||||
try {
|
||||
AccessController.doPrivileged(new PrivilegedAction<Object>() {
|
||||
public Object run() {
|
||||
try {
|
||||
applet.start();
|
||||
return null; // nothing to return
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
});
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void destroy() {
|
||||
try {
|
||||
AccessController.doPrivileged(new PrivilegedAction<Object>() {
|
||||
public Object run() {
|
||||
try {
|
||||
applet.destroy();
|
||||
return null; // nothing to return
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
});
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void paint(Graphics g) {
|
||||
g.setColor(Color.black);
|
||||
g.drawString(appletStr, 8, 16);
|
||||
}
|
||||
|
||||
private Hashtable<String,String> parseParams(String params)
|
||||
{
|
||||
Hashtable<String,String> res = new Hashtable<String, String>();
|
||||
String[] parms = params.split("\t");
|
||||
for( int i = 0; i < parms.length; i++) {
|
||||
String[] val = parms[i].split(":");
|
||||
if( val.length == 1 )
|
||||
res.put(val[0], "");
|
||||
else
|
||||
res.put(val[0], val[1]);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
private String unscramble(String in)
|
||||
{
|
||||
int len = in.length();
|
||||
StringBuilder res = new StringBuilder(len/2);
|
||||
int val = 0x32;
|
||||
for( int i = 0; i < len; i += 2) {
|
||||
String b = in.substring(i, i+2);
|
||||
int c = Integer.parseInt(b, 16)^val;
|
||||
val = (val + c) & 0xFF;
|
||||
res.append((char)c);
|
||||
}
|
||||
|
||||
return res.reverse().toString();
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -1,40 +0,0 @@
|
||||
package es.virtualcable.rdp;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.*;
|
||||
import java.util.Random;
|
||||
|
||||
|
||||
public class FreePortFinder {
|
||||
|
||||
private final static int START_PORT = 35000;
|
||||
private final static int START_PORT_MAX = 44000;
|
||||
private final static int END_PORT = 45500;
|
||||
|
||||
public static boolean isFree(int port)
|
||||
{
|
||||
boolean free = true;
|
||||
try {
|
||||
InetAddress addr = InetAddress.getByName("localhost");
|
||||
SocketAddress sa = new InetSocketAddress(addr, port);
|
||||
ServerSocket socket = new ServerSocket();
|
||||
socket.bind(sa);
|
||||
socket.close();
|
||||
} catch (IOException e) {
|
||||
free = false;
|
||||
}
|
||||
return free;
|
||||
}
|
||||
|
||||
public static int findFreePort()
|
||||
{
|
||||
Random rnd = new Random();
|
||||
int startPort = START_PORT + rnd.nextInt(START_PORT_MAX-START_PORT);
|
||||
for( int port = startPort; port < END_PORT; port ++)
|
||||
{
|
||||
if( isFree(port) )
|
||||
return port;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
}
|
@ -1,206 +0,0 @@
|
||||
package es.virtualcable.rdp;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Hashtable;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
public class LinuxApplet implements OsApplet {
|
||||
private final String[] paths = { "/usr/local/bin/", "/usr/bin/" };
|
||||
private final String rdesktop = "rdesktop";
|
||||
|
||||
private Hashtable<String,String> params;
|
||||
private String jarFileName = "";
|
||||
private String tmpDir = "";
|
||||
private String baseUrl = "";
|
||||
private String tunPort = "";
|
||||
|
||||
private void addParameterIfNotEmpty(ArrayList<String> exec, String param, String value)
|
||||
{
|
||||
if( value.length() == 0 )
|
||||
return;
|
||||
if( param.indexOf(' ') != -1 )
|
||||
exec.add( param + "\"" + value + "\"");
|
||||
else
|
||||
exec.add(param + value);
|
||||
}
|
||||
|
||||
public void start() {
|
||||
try {
|
||||
String colors = params.get("c");
|
||||
String width = params.get("w");
|
||||
String height = params.get("h");
|
||||
String server = params.get("s"); // Server
|
||||
String user = params.get("u"); // User
|
||||
String password = params.get("p"); // password
|
||||
String domain = params.get("d"); // domain
|
||||
|
||||
tmpDir = System.getProperty("java.io.tmpdir") + File.separator;
|
||||
jarFileName = tmpDir + UUID.randomUUID().toString() + ".jar";
|
||||
|
||||
boolean redirectSmartcards = params.get("sc").equals("1");
|
||||
boolean redirectDrives = params.get("dr").equals("1");
|
||||
boolean redirectSerials = params.get("se").equals("1");
|
||||
boolean redirectPrinters = params.get("pr").equals("1");
|
||||
boolean redirectAudio = params.get("au").equals("1");
|
||||
boolean compression = params.get("cr").equals("1");
|
||||
boolean wallpaper = false;
|
||||
|
||||
if( params.get("sw") != null )
|
||||
{
|
||||
wallpaper = params.get("sw").equals("1");
|
||||
}
|
||||
|
||||
|
||||
// Notifies to broker the
|
||||
util.notifyHostname(baseUrl, params.get("is"));
|
||||
|
||||
String home = System.getProperty("user.home");
|
||||
|
||||
ArrayList<String> exec = new ArrayList<String>();
|
||||
|
||||
if(params.get("tun") != null)
|
||||
{
|
||||
if( downloadJar() == false)
|
||||
return;
|
||||
tunPort = Integer.toString(FreePortFinder.findFreePort());
|
||||
server = "localhost:" + tunPort;
|
||||
String java = System.getProperty("java.home") + File.separator + "bin" + File.separator + "java";
|
||||
exec.add(java); exec.add("-jar"); exec.add(jarFileName); exec.add(tunPort);
|
||||
}
|
||||
|
||||
String execPath = "";
|
||||
|
||||
for(int i = 0; i < paths.length; i++ )
|
||||
{
|
||||
File f = new File(paths[i] + rdesktop);
|
||||
if( f.exists() )
|
||||
{
|
||||
execPath = paths[i] + rdesktop;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if( execPath.length() == 0 )
|
||||
{
|
||||
javax.swing.JOptionPane.showMessageDialog(null, "Can't find rdesktop client.\nShould be at /usr/bin or /usr/local/bin\nPlease, install it");
|
||||
return;
|
||||
}
|
||||
|
||||
exec.add(execPath);
|
||||
|
||||
addParameterIfNotEmpty(exec, "-u", user);
|
||||
addParameterIfNotEmpty(exec, "-d", domain);
|
||||
if( password.length() != 0 )
|
||||
exec.add("-p-");
|
||||
addParameterIfNotEmpty(exec, "-a", colors);
|
||||
if( width.equals("-1") )
|
||||
exec.add("-f");
|
||||
else
|
||||
exec.add("-g" + width + "x" + height);
|
||||
|
||||
if (redirectSmartcards)
|
||||
exec.add("-rscard");
|
||||
|
||||
exec.add("-TUDS-RDP");
|
||||
exec.add("-P");
|
||||
|
||||
if( redirectSmartcards )
|
||||
{
|
||||
}
|
||||
|
||||
if( compression )
|
||||
{
|
||||
exec.add("-z");
|
||||
}
|
||||
|
||||
if( redirectDrives )
|
||||
{
|
||||
exec.add("-rdisk:home=" + home);
|
||||
exec.add("-rdisk:media=/media");
|
||||
}
|
||||
|
||||
if( redirectAudio )
|
||||
exec.add("-rsound:local");
|
||||
else
|
||||
exec.add("-rsound:off");
|
||||
|
||||
if( redirectSerials )
|
||||
exec.add("-rcomport:COM1=/dev/ttyS0");
|
||||
|
||||
if( wallpaper )
|
||||
exec.add("-xl"); // Adds "Lan" experience
|
||||
|
||||
if( redirectPrinters ) // Will have to look at local cups to find printer
|
||||
{
|
||||
}
|
||||
|
||||
exec.add(server);
|
||||
|
||||
/* Iterator<String> it = exec.iterator();
|
||||
while( it.hasNext()) {
|
||||
System.out.print(it.next() + " ");
|
||||
}
|
||||
System.out.println();
|
||||
*/
|
||||
Process p;
|
||||
try {
|
||||
ProcessBuilder pb = new ProcessBuilder(exec);
|
||||
if( params.get("tun") != null)
|
||||
{
|
||||
Map<String,String> env = pb.environment();
|
||||
env.put("TPARAMS", params.get("tun"));
|
||||
//System.out.println("TPARAMS: " + params.get("tun"));
|
||||
}
|
||||
p = pb.start();
|
||||
} catch(Exception e) {
|
||||
javax.swing.JOptionPane.showMessageDialog(null,"Exception launching " + execPath + ":\n" + e.getMessage());
|
||||
e.printStackTrace();
|
||||
return;
|
||||
}
|
||||
|
||||
if( password.length() != 0 )
|
||||
{
|
||||
try {
|
||||
p.getOutputStream().write(password.getBytes());
|
||||
p.getOutputStream().write( new byte[]{'\n'} );
|
||||
p.getOutputStream().flush();
|
||||
}
|
||||
catch( Exception e ) {
|
||||
javax.swing.JOptionPane.showMessageDialog(null,"Exception communicating with " + execPath + ":\n" + e.getMessage());
|
||||
return;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
javax.swing.JOptionPane.showMessageDialog(null,"Exception at applet:\n" + e.getMessage());
|
||||
e.printStackTrace();
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
public void init() {
|
||||
|
||||
}
|
||||
|
||||
public void destroy() {
|
||||
|
||||
}
|
||||
|
||||
public void setParameters(Hashtable<String, String> parameters,
|
||||
String urlBase, int screenWidth, int screenHeight) {
|
||||
params = parameters;
|
||||
baseUrl = urlBase;
|
||||
}
|
||||
|
||||
private boolean downloadJar()
|
||||
{
|
||||
return util.download(baseUrl, "3", jarFileName);
|
||||
}
|
||||
|
||||
}
|
@ -1,211 +0,0 @@
|
||||
package es.virtualcable.rdp;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Hashtable;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
public class MacApplet implements OsApplet {
|
||||
private final String[] paths = { "/usr/bin/" };
|
||||
private final String open = "open";
|
||||
private final String cordPath = "/Applications/CoRD.app/Contents/MacOS/CoRD";
|
||||
|
||||
private Hashtable<String,String> params;
|
||||
private String jarFileName = "";
|
||||
private String tmpDir = "";
|
||||
private String baseUrl = "";
|
||||
private String tunPort = "";
|
||||
|
||||
private String convSpaces(String value)
|
||||
{
|
||||
final String weird = "$#$_$#$";
|
||||
if( value == null || value.isEmpty())
|
||||
return "";
|
||||
String res = value.replaceAll(" ", "%20");
|
||||
res = res.replaceAll("@", "%40");
|
||||
res = res.replaceAll(":", "%3A");
|
||||
res = res.replaceAll("&", weird);
|
||||
return res.replaceAll(weird, "&");
|
||||
}
|
||||
|
||||
private void Cord(ArrayList<String> exec, String user, String password, String server,
|
||||
String domain, String colors, String width, String height, boolean redirectAudio,
|
||||
boolean redirectSmartcards, boolean redirectDrives,
|
||||
boolean redirectSerials, boolean redirectPrinters)
|
||||
{
|
||||
String url = "rdp://";
|
||||
|
||||
if(!user.isEmpty()) {
|
||||
url += user;
|
||||
if(!password.isEmpty())
|
||||
url += ":" + password;
|
||||
url += "@";
|
||||
}
|
||||
|
||||
url += server + "/";
|
||||
if(!domain.isEmpty())
|
||||
url += domain;
|
||||
url += "?screenDepth=" + colors + "&";
|
||||
if(width.equals("-1"))
|
||||
url += "fullscreen=true";
|
||||
else
|
||||
url += "screenWidth="+width+"&screenHeight="+height;
|
||||
|
||||
url += "&forwardAudio=";
|
||||
|
||||
if( redirectAudio )
|
||||
url += "0";
|
||||
else
|
||||
url += "1";
|
||||
|
||||
if( redirectSmartcards ) // Not supported by Cord
|
||||
{
|
||||
}
|
||||
|
||||
if( redirectDrives )
|
||||
url += "&forwardDisks=true";
|
||||
|
||||
if( redirectSerials )
|
||||
{
|
||||
}
|
||||
|
||||
if( redirectPrinters )
|
||||
url += "&forwardPrinters=true";
|
||||
|
||||
exec.add(url);
|
||||
|
||||
}
|
||||
|
||||
public void start() {
|
||||
try {
|
||||
String colors = params.get("c");
|
||||
String width = params.get("w");
|
||||
String height = params.get("h");
|
||||
String server = params.get("s"); // Server
|
||||
String user = convSpaces(params.get("u")); // User
|
||||
String password = convSpaces(params.get("p")); // password
|
||||
String domain = convSpaces(params.get("d")); // domain
|
||||
|
||||
tmpDir = System.getProperty("java.io.tmpdir") + File.separator;
|
||||
jarFileName = tmpDir + UUID.randomUUID().toString() + ".jar";
|
||||
|
||||
boolean redirectSmartcards = params.get("sc").equals("1");
|
||||
boolean redirectDrives = params.get("dr").equals("1");
|
||||
boolean redirectSerials = params.get("se").equals("1");
|
||||
boolean redirectPrinters = params.get("pr").equals("1");
|
||||
boolean redirectAudio = params.get("au").equals("1");
|
||||
//boolean compression = params.get("cr").equals("1");
|
||||
|
||||
//String home = System.getProperty("user.home");
|
||||
|
||||
// Notifies to broker the
|
||||
util.notifyHostname(baseUrl, params.get("is"));
|
||||
|
||||
ArrayList<String> exec = new ArrayList<String>();
|
||||
|
||||
if(params.get("tun") != null)
|
||||
{
|
||||
if( downloadJar() == false)
|
||||
return;
|
||||
tunPort = Integer.toString(FreePortFinder.findFreePort());
|
||||
server = "127.0.0.1:" + tunPort;
|
||||
String java = System.getProperty("java.home") + File.separator + "bin" + File.separator + "java";
|
||||
exec.add(java); exec.add("-jar"); exec.add(jarFileName); exec.add(tunPort);
|
||||
}
|
||||
|
||||
String execPath = "";
|
||||
|
||||
for(int i = 0; i < paths.length; i++ )
|
||||
{
|
||||
File f = new File(paths[i] + open);
|
||||
if( f.exists() )
|
||||
{
|
||||
execPath = paths[i] + open;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if(( new File(cordPath)).exists() == false )
|
||||
{
|
||||
javax.swing.JOptionPane.showMessageDialog(null, "Can't find CoRD client. Please, install it before proceeding.");
|
||||
return;
|
||||
}
|
||||
|
||||
if( execPath.length() == 0 )
|
||||
{
|
||||
javax.swing.JOptionPane.showMessageDialog(null, "Can't find rdesktop client.\nShould be at /usr/bin or /usr/local/bin\nPlease, install it");
|
||||
return;
|
||||
}
|
||||
|
||||
exec.add(execPath);
|
||||
|
||||
Cord(exec, user, password, server, domain, colors, width, height, redirectAudio, redirectSmartcards,
|
||||
redirectDrives, redirectSerials, redirectPrinters);
|
||||
|
||||
/* Iterator<String> it = exec.iterator();
|
||||
while( it.hasNext()) {
|
||||
System.out.print(it.next() + " ");
|
||||
}
|
||||
System.out.println();
|
||||
*/
|
||||
Process p;
|
||||
try {
|
||||
ProcessBuilder pb = new ProcessBuilder(exec);
|
||||
if( params.get("tun") != null)
|
||||
{
|
||||
Map<String,String> env = pb.environment();
|
||||
env.put("TPARAMS", params.get("tun"));
|
||||
//System.out.println("TPARAMS: " + params.get("tun"));
|
||||
}
|
||||
p = pb.start();
|
||||
} catch(Exception e) {
|
||||
javax.swing.JOptionPane.showMessageDialog(null,"Exception launching " + execPath + ":\n" + e.getMessage());
|
||||
e.printStackTrace();
|
||||
return;
|
||||
}
|
||||
|
||||
if( password.length() != 0 )
|
||||
{
|
||||
try {
|
||||
p.getOutputStream().write(password.getBytes());
|
||||
p.getOutputStream().write( new byte[]{'\n'} );
|
||||
p.getOutputStream().flush();
|
||||
}
|
||||
catch( Exception e ) {
|
||||
javax.swing.JOptionPane.showMessageDialog(null,"Exception communicating with " + execPath + ":\n" + e.getMessage());
|
||||
return;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
javax.swing.JOptionPane.showMessageDialog(null,"Exception at applet:\n" + e.getMessage());
|
||||
e.printStackTrace();
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
public void init() {
|
||||
|
||||
}
|
||||
|
||||
public void destroy() {
|
||||
|
||||
}
|
||||
|
||||
public void setParameters(Hashtable<String, String> parameters,
|
||||
String urlBase, int screenWidth, int screenHeight) {
|
||||
params = parameters;
|
||||
baseUrl = urlBase;
|
||||
}
|
||||
|
||||
private boolean downloadJar()
|
||||
{
|
||||
return util.download(baseUrl, "3", jarFileName);
|
||||
}
|
||||
|
||||
}
|
@ -1,15 +0,0 @@
|
||||
package es.virtualcable.rdp;
|
||||
|
||||
import java.util.Hashtable;
|
||||
|
||||
public interface OsApplet {
|
||||
|
||||
void setParameters(Hashtable<String,String> parameters, String urlBase, int screenWidth, int screenHeight);
|
||||
|
||||
void init();
|
||||
|
||||
void start();
|
||||
|
||||
void destroy();
|
||||
|
||||
}
|
@ -1,103 +0,0 @@
|
||||
package es.virtualcable.rdp;
|
||||
|
||||
// More info about RDP files: http://technet.microsoft.com/en-us/library/ff393699%28WS.10%29.aspx
|
||||
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import net.sourceforge.jdpapi.DataProtector;
|
||||
|
||||
public class WinRdpFile {
|
||||
public String width;
|
||||
public String height;
|
||||
public boolean fullScreen;
|
||||
public String bpp;
|
||||
public String address = "";
|
||||
public String username = "";
|
||||
public String domain = "";
|
||||
public String password = "";
|
||||
public boolean redirectSerials = false;
|
||||
public boolean redirectPrinters = false;
|
||||
public boolean redirectDrives = false;
|
||||
public boolean redirectSmartcards = false;
|
||||
public boolean redirectAudio = false;
|
||||
public boolean compression = false;
|
||||
public boolean displayConnectionBar = true;
|
||||
public boolean showWallpaper = false;
|
||||
public boolean multimon = false;
|
||||
|
||||
public WinRdpFile(boolean fullScreen, String width, String height, String bpp) {
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
this.bpp = bpp;
|
||||
this.fullScreen = fullScreen;
|
||||
}
|
||||
|
||||
public void saveFile(String fname) throws IOException {
|
||||
DataProtector d = new DataProtector(true);
|
||||
String pass = encode(d.protect(this.password));
|
||||
String screenMode = fullScreen ? "2" : "1";
|
||||
String audioMode = redirectAudio ? "0" : "2";
|
||||
String serials = redirectSerials ? "1" : "0";
|
||||
String drives = redirectDrives ? "1" : "0";
|
||||
String scards = redirectSmartcards ? "1" : "0";
|
||||
String printers = redirectPrinters ? "1" : "0";
|
||||
String compression = this.compression ? "1" : "0";
|
||||
String bar = displayConnectionBar ? "1" : "0";
|
||||
String disableWallpaper = showWallpaper ? "0" : "1";
|
||||
String useMultimon = multimon ? "0" : "1";
|
||||
|
||||
FileWriter fstream = new FileWriter(fname);
|
||||
PrintWriter out = new PrintWriter(fstream);
|
||||
out.println("screen mode id:i:" + screenMode);
|
||||
out.println("desktopwidth:i:"+this.width);
|
||||
out.println("desktopheight:i:"+this.height);
|
||||
out.println("session bpp:i:"+this.bpp);
|
||||
out.println("use multimon:i:"+useMultimon);
|
||||
out.println("auto connect:i:1");
|
||||
out.println("full address:s:"+this.address);
|
||||
out.println("compression:i:"+compression);
|
||||
out.println("keyboardhook:i:2");
|
||||
out.println("audiomode:i:"+audioMode);
|
||||
out.println("redirectdrives:i:" + drives);
|
||||
out.println("redirectprinters:i:" + printers);
|
||||
out.println("redirectcomports:i:" + serials);
|
||||
out.println("redirectsmartcards:i:" + scards);
|
||||
out.println("redirectclipboard:i:1");
|
||||
out.println("displayconnectionbar:i:"+bar);
|
||||
if( this.username.length() != 0) {
|
||||
out.println("username:s:"+this.username);
|
||||
out.println("domain:s:"+this.domain);
|
||||
out.println("password 51:b:"+pass);
|
||||
}
|
||||
out.println("alternate shell:s:");
|
||||
out.println("shell working directory:s:");
|
||||
out.println("disable wallpaper:i:"+disableWallpaper);
|
||||
out.println("disable full window drag:i:1");
|
||||
out.println("disable menu anims:i:"+disableWallpaper);
|
||||
out.println("disable themes:i:"+disableWallpaper);
|
||||
out.println("bitmapcachepersistenable:i:1");
|
||||
out.println("authentication level:i:0");
|
||||
out.println("enablecredsspsupport:i:1");
|
||||
out.println("prompt for credentials:i:0");
|
||||
out.println("negotiate security layer:i:1");
|
||||
out.close();
|
||||
}
|
||||
|
||||
protected static final byte[] Hexhars = {
|
||||
'0', '1', '2', '3', '4', '5', '6', '7',
|
||||
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
|
||||
|
||||
public static String encode(byte[] b) {
|
||||
|
||||
StringBuilder s = new StringBuilder(2 * b.length);
|
||||
for (int i = 0; i < b.length; i++) {
|
||||
int v = b[i] & 0xff;
|
||||
s.append((char) Hexhars[v >> 4]);
|
||||
s.append((char) Hexhars[v & 0xf]);
|
||||
}
|
||||
|
||||
return s.toString();
|
||||
}
|
||||
|
||||
}
|
@ -1,143 +0,0 @@
|
||||
package es.virtualcable.rdp;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.Hashtable;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
import es.virtualcable.rdp.util;
|
||||
|
||||
public class WindowsApplet implements OsApplet {
|
||||
|
||||
private static final String MSTSC_CMD_REL = "\\system32\\mstsc.exe";
|
||||
|
||||
private Hashtable<String,String> params;
|
||||
private String scrWidth;
|
||||
private String scrHeight;
|
||||
private String tmpDir = "";
|
||||
private String rdpFileName = "";
|
||||
private String dllFileName = "";
|
||||
private String jarFileName = "";
|
||||
private String baseUrl = "";
|
||||
private String tunPort = "";
|
||||
|
||||
public void setParameters(Hashtable<String, String> parameters, String urlBase,
|
||||
int screenWidth, int screenHeight) {
|
||||
params = parameters;
|
||||
baseUrl = urlBase;
|
||||
scrWidth = Integer.toString(screenWidth);
|
||||
scrHeight = Integer.toString(screenHeight);
|
||||
}
|
||||
|
||||
public void start() {
|
||||
try {
|
||||
tmpDir = System.getProperty("java.io.tmpdir") + File.separator;
|
||||
rdpFileName = tmpDir + UUID.randomUUID().toString() + ".tmp";
|
||||
dllFileName = tmpDir + UUID.randomUUID().toString() + ".tmp";
|
||||
jarFileName = tmpDir + UUID.randomUUID().toString() + ".jar";
|
||||
if( downloadDll() == false )
|
||||
return;
|
||||
|
||||
if(params.get("tun") != null)
|
||||
{
|
||||
if( downloadJar() == false)
|
||||
return;
|
||||
tunPort = Integer.toString(FreePortFinder.findFreePort());
|
||||
}
|
||||
|
||||
// Notifies to broker the hostname/ip
|
||||
util.notifyHostname(baseUrl, params.get("is"));
|
||||
|
||||
System.load(dllFileName);
|
||||
|
||||
String width = params.get("w");
|
||||
String height = params.get("h");
|
||||
boolean fullScreen = false;
|
||||
|
||||
if( width.equals("-1"))
|
||||
{
|
||||
width = scrWidth;
|
||||
height = scrHeight;
|
||||
fullScreen = true;
|
||||
}
|
||||
|
||||
WinRdpFile rdp = new WinRdpFile(fullScreen, width, height, params.get("c"));
|
||||
if( params.get("tun") != null )
|
||||
{
|
||||
rdp.address = "127.0.0.1:" + tunPort;
|
||||
}
|
||||
else
|
||||
rdp.address = params.get("s"); // Server
|
||||
rdp.username = params.get("u"); // User
|
||||
rdp.password = params.get("p"); // password
|
||||
rdp.domain = params.get("d"); // domain
|
||||
rdp.redirectSmartcards = params.get("sc").equals("1");
|
||||
rdp.redirectDrives = params.get("dr").equals("1");
|
||||
rdp.redirectSerials = params.get("se").equals("1");
|
||||
rdp.redirectPrinters = params.get("pr").equals("1");
|
||||
rdp.redirectAudio = params.get("au").equals("1");
|
||||
rdp.compression = params.get("cr").equals("1");
|
||||
rdp.multimon = params.get("mm").equals("1");
|
||||
if( params.get("sw") != null )
|
||||
{
|
||||
rdp.showWallpaper = params.get("sw").equals("1");
|
||||
}
|
||||
|
||||
try {
|
||||
rdp.saveFile( rdpFileName );
|
||||
if( params.get("tun") != null )
|
||||
executeTunnel();
|
||||
else
|
||||
executeDirect();
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return;
|
||||
}
|
||||
|
||||
// Process p =
|
||||
} catch (Exception e) {
|
||||
javax.swing.JOptionPane.showMessageDialog(null,"Exception at applet:\n" + e.getMessage());
|
||||
e.printStackTrace();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
public void init() {
|
||||
}
|
||||
|
||||
public void destroy() {
|
||||
}
|
||||
|
||||
private void executeTunnel() throws IOException
|
||||
{
|
||||
String java = System.getProperty("java.home") + File.separator + "bin" + File.separator + "java.exe";
|
||||
String MSTSC_CMD = System.getenv("SystemRoot") + MSTSC_CMD_REL;
|
||||
String [] cmd = { java, "-jar", jarFileName, tunPort, MSTSC_CMD, rdpFileName };
|
||||
ProcessBuilder pb = new ProcessBuilder( cmd );
|
||||
Map<String,String> env = pb.environment();
|
||||
env.put("TPARAMS", params.get("tun"));
|
||||
//System.out.println("TPARAMS: " + params.get("tun"));
|
||||
//System.out.println("java: " + java + " -jar " + jarFileName + " " + MSTSC_CMD + " " + rdpFileName);
|
||||
pb.start();
|
||||
|
||||
}
|
||||
|
||||
private void executeDirect() throws IOException
|
||||
{
|
||||
String MSTSC_CMD = System.getenv("SystemRoot") + MSTSC_CMD_REL;
|
||||
Runtime.getRuntime().exec( MSTSC_CMD + " \"" + rdpFileName + "\"" );
|
||||
}
|
||||
|
||||
private boolean downloadDll()
|
||||
{
|
||||
return util.download(baseUrl, "2", dllFileName);
|
||||
}
|
||||
|
||||
private boolean downloadJar()
|
||||
{
|
||||
return util.download(baseUrl, "3", jarFileName);
|
||||
}
|
||||
|
||||
}
|
@ -1,98 +0,0 @@
|
||||
package es.virtualcable.rdp;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.URLEncoder;
|
||||
|
||||
public class util {
|
||||
|
||||
public static boolean download(String baseUrl, String id, String outputFileName)
|
||||
{
|
||||
try {
|
||||
java.net.URL u = new java.net.URL(baseUrl + id);
|
||||
java.net.URLConnection uc = u.openConnection();
|
||||
String contentType = uc.getContentType();
|
||||
int contentLength = uc.getContentLength();
|
||||
if (contentType.startsWith("text/") || contentLength == -1) {
|
||||
throw new IOException("This is not a binary file.");
|
||||
}
|
||||
InputStream raw = uc.getInputStream();
|
||||
InputStream in = new BufferedInputStream(raw);
|
||||
byte[] data = new byte[contentLength];
|
||||
int bytesRead = 0;
|
||||
int offset = 0;
|
||||
while (offset < contentLength) {
|
||||
bytesRead = in.read(data, offset, data.length - offset);
|
||||
if (bytesRead == -1)
|
||||
break;
|
||||
offset += bytesRead;
|
||||
}
|
||||
in.close();
|
||||
|
||||
if (offset != contentLength) {
|
||||
throw new IOException("Only read " + offset + " bytes; Expected " + contentLength + " bytes");
|
||||
}
|
||||
|
||||
java.io.FileOutputStream out = new java.io.FileOutputStream(outputFileName);
|
||||
out.write(data);
|
||||
out.flush();
|
||||
out.close();
|
||||
|
||||
} catch(Exception e) {
|
||||
System.out.println("Unable to download file, already present or network error? " + e.getMessage());
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
public static String getUrl(String url) {
|
||||
try {
|
||||
java.net.URL u = new java.net.URL(url);
|
||||
BufferedReader in = new BufferedReader(new InputStreamReader(u.openStream()));
|
||||
StringBuilder data = new StringBuilder();
|
||||
|
||||
String inputLine;
|
||||
while ((inputLine = in.readLine()) != null) {
|
||||
data.append(inputLine);
|
||||
data.append("\n");
|
||||
}
|
||||
|
||||
in.close();
|
||||
return data.toString();
|
||||
|
||||
} catch(Exception e) {
|
||||
System.out.println("Unable to get url. Network error? " + e.getMessage());
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
public static void notifyHostname(String baseUrl, String serviceId) {
|
||||
String[] urlComponents = baseUrl.split("/");
|
||||
String hostname;
|
||||
String ip;
|
||||
String url="";
|
||||
|
||||
try {
|
||||
hostname = java.net.InetAddress.getLocalHost().getHostName();
|
||||
ip = java.net.InetAddress.getLocalHost().getHostAddress();
|
||||
} catch(Exception e) {
|
||||
hostname = "unknown";
|
||||
ip = "0.0.0.0";
|
||||
}
|
||||
|
||||
try {
|
||||
// An url is "http[s]://.....:/,
|
||||
url = urlComponents[0] + "//" + urlComponents[2] + "/sernotify/" + serviceId + "/hostname?hostname="+URLEncoder.encode(hostname)+"&ip="+URLEncoder.encode(ip);
|
||||
getUrl(url);
|
||||
} catch(Exception e) {
|
||||
System.out.println("Unable to get url? " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -1,7 +0,0 @@
|
||||
Manifest-Version: 1.0
|
||||
Sealed: true
|
||||
Permissions: all-permissions
|
||||
Codebase: *
|
||||
Application-Library-Allowable-Codebase: *
|
||||
Application-Name: UDS RDP Connector
|
||||
Created-By: Virtual Cable S.L.U.
|
@ -1,27 +0,0 @@
|
||||
package net.sourceforge.jdpapi;
|
||||
|
||||
|
||||
class DPAPI {
|
||||
|
||||
/**
|
||||
* See the <a href="http://msdn2.microsoft.com/en-us/library/aa380261.aspx">CryptProtectData</a>
|
||||
* documentation for more details
|
||||
*
|
||||
* @param input Plaintext to encrypt
|
||||
* @param entropy Additional entropy to include when encrypting (optional)
|
||||
* @param localMachine If true, allow all users on the machine to decrypt the return ciphertext
|
||||
* @return ciphertext
|
||||
*/
|
||||
static native byte [] CryptProtectData(String input, byte [] entropy, boolean localMachine);
|
||||
|
||||
/**
|
||||
* See the <a href="http://msdn2.microsoft.com/en-us/library/aa380882.aspx">CryptUnprotectData</a>
|
||||
* documentation for more details
|
||||
*
|
||||
* @param input Ciphertext
|
||||
* @param entropy Entropy that was included when {@code input} was encrypted
|
||||
* @return
|
||||
*/
|
||||
static native String CryptUnprotectData(byte [] input, byte [] entropy);
|
||||
|
||||
}
|
@ -1,28 +0,0 @@
|
||||
package net.sourceforge.jdpapi;
|
||||
|
||||
/**
|
||||
* Exception that can be thrown from the native DPAPI
|
||||
*
|
||||
* @author <a href="mailto:kevin.a.conaway@gmail.com">Kevin Conaway</a>
|
||||
*/
|
||||
public class DPAPIException extends RuntimeException {
|
||||
|
||||
private static final long serialVersionUID = 1l;
|
||||
|
||||
public DPAPIException() {
|
||||
super();
|
||||
}
|
||||
|
||||
public DPAPIException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
|
||||
public DPAPIException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public DPAPIException(Throwable cause) {
|
||||
super(cause);
|
||||
}
|
||||
|
||||
}
|
@ -1,75 +0,0 @@
|
||||
package net.sourceforge.jdpapi;
|
||||
|
||||
/**
|
||||
* <p>An interface to the Microsoft Data Protection API (DPAPI).</p>
|
||||
*
|
||||
* <p>See <a href="http://msdn2.microsoft.com/en-us/library/ms995355.aspx">MSDN</a> for more information</p>
|
||||
*
|
||||
* @author <a href="mailto:kevin.a.conaway@gmail.com">Kevin Conaway</a>
|
||||
*/
|
||||
public class DataProtector {
|
||||
|
||||
private final byte [] entropy;
|
||||
private final boolean localMachine;
|
||||
|
||||
/**
|
||||
* @param entropy Additional entropy to include when encrypting (optional)
|
||||
* @param localMachine If true, allow all users on the machine to decrypt a given ciphertext
|
||||
*
|
||||
*/
|
||||
public DataProtector(byte[] entropy, boolean localMachine) {
|
||||
this.entropy = entropy;
|
||||
this.localMachine = localMachine;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param entropy
|
||||
* @see #DataProtector(byte[], boolean)
|
||||
*/
|
||||
public DataProtector(byte[] entropy) {
|
||||
this(entropy, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param localMachine
|
||||
* @see #DataProtector(byte[], boolean)
|
||||
*/
|
||||
public DataProtector(boolean localMachine) {
|
||||
this(null, localMachine);
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes the protector with no additional entropy
|
||||
* and {@code localMachine} set to false
|
||||
* @see #DataProtector(byte[], boolean)
|
||||
*/
|
||||
public DataProtector() {
|
||||
this(false);
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Protect {@code input} using the Microsoft DPAPI CryptProtectData function.</p>
|
||||
*
|
||||
* <p>See the <a href="http://msdn2.microsoft.com/en-us/library/aa380261.aspx">CryptProtectData</a>
|
||||
* documentation for more details</p>
|
||||
*
|
||||
* @param input Plaintext to encrypt
|
||||
* @return ciphertext
|
||||
*/
|
||||
public byte[] protect(String input) {
|
||||
return DPAPI.CryptProtectData(input, entropy, localMachine);
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Unprotect {@code input} using the Microsoft DPAPI CryptUnprotectData function.</p>
|
||||
*
|
||||
* <p>See the <a href="http://msdn2.microsoft.com/en-us/library/aa380882.aspx">CryptUnprotectData</a>
|
||||
* documentation for more details</p>
|
||||
*
|
||||
* @param input Ciphertext
|
||||
* @return Plaintext
|
||||
*/
|
||||
public String unprotect(byte [] input) {
|
||||
return DPAPI.CryptUnprotectData(input, entropy);
|
||||
}
|
||||
}
|
@ -1,6 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="src" path="src"/>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
|
||||
<classpathentry kind="output" path="bin"/>
|
||||
</classpath>
|
2
ssh-tunnel/tunnelLaucher/.gitignore
vendored
2
ssh-tunnel/tunnelLaucher/.gitignore
vendored
@ -1,2 +0,0 @@
|
||||
/bin/
|
||||
/jar/
|
@ -1,17 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>tunnelLaucher</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
@ -1,19 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
||||
<jardesc>
|
||||
<jar path="tunnelLaucher/jar/launcher.jar"/>
|
||||
<options buildIfNeeded="true" compress="true" descriptionLocation="/tunnelLaucher/description.jardesc" exportErrors="true" exportWarnings="true" includeDirectoryEntries="false" overwrite="true" saveDescription="true" storeRefactorings="false" useSourceFolders="false"/>
|
||||
<storedRefactorings deprecationInfo="true" structuralOnly="false"/>
|
||||
<selectedProjects/>
|
||||
<manifest generateManifest="false" mainClassHandleIdentifier="=tunnelLaucher/src<es.virtualcable.sshtunnel{Laucher.java[Laucher" manifestLocation="/tunnelLaucher/src/manifest" manifestVersion="1.0" reuseManifest="true" saveManifest="true" usesManifest="true">
|
||||
<sealing sealJar="true">
|
||||
<packagesToSeal>
|
||||
<package handleIdentifier="=rdp/src<com.virtualcable.rdp"/>
|
||||
<package handleIdentifier="=rdp/src<net.sourceforge.jdpapi"/>
|
||||
</packagesToSeal>
|
||||
<packagesToUnSeal/>
|
||||
</sealing>
|
||||
</manifest>
|
||||
<selectedElements exportClassFiles="true" exportJavaFiles="false" exportOutputFolder="false">
|
||||
<javaElement handleIdentifier="=tunnelLaucher/src"/>
|
||||
</selectedElements>
|
||||
</jardesc>
|
@ -1,254 +0,0 @@
|
||||
/* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
|
||||
/*
|
||||
Copyright (c) 2002-2011 ymnk, JCraft,Inc. All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice,
|
||||
this list of conditions and the following disclaimer.
|
||||
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the distribution.
|
||||
|
||||
3. The names of the authors may not be used to endorse or promote products
|
||||
derived from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
|
||||
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
|
||||
INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
|
||||
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
||||
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
package com.jcraft.jsch;
|
||||
|
||||
public class Buffer{
|
||||
final byte[] tmp=new byte[4];
|
||||
byte[] buffer;
|
||||
int index;
|
||||
int s;
|
||||
public Buffer(int size){
|
||||
buffer=new byte[size];
|
||||
index=0;
|
||||
s=0;
|
||||
}
|
||||
public Buffer(byte[] buffer){
|
||||
this.buffer=buffer;
|
||||
index=0;
|
||||
s=0;
|
||||
}
|
||||
public Buffer(){ this(1024*10*2); }
|
||||
public void putByte(byte foo){
|
||||
buffer[index++]=foo;
|
||||
}
|
||||
public void putByte(byte[] foo) {
|
||||
putByte(foo, 0, foo.length);
|
||||
}
|
||||
public void putByte(byte[] foo, int begin, int length) {
|
||||
System.arraycopy(foo, begin, buffer, index, length);
|
||||
index+=length;
|
||||
}
|
||||
public void putString(byte[] foo){
|
||||
putString(foo, 0, foo.length);
|
||||
}
|
||||
public void putString(byte[] foo, int begin, int length) {
|
||||
putInt(length);
|
||||
putByte(foo, begin, length);
|
||||
}
|
||||
public void putInt(int val) {
|
||||
tmp[0]=(byte)(val >>> 24);
|
||||
tmp[1]=(byte)(val >>> 16);
|
||||
tmp[2]=(byte)(val >>> 8);
|
||||
tmp[3]=(byte)(val);
|
||||
System.arraycopy(tmp, 0, buffer, index, 4);
|
||||
index+=4;
|
||||
}
|
||||
public void putLong(long val) {
|
||||
tmp[0]=(byte)(val >>> 56);
|
||||
tmp[1]=(byte)(val >>> 48);
|
||||
tmp[2]=(byte)(val >>> 40);
|
||||
tmp[3]=(byte)(val >>> 32);
|
||||
System.arraycopy(tmp, 0, buffer, index, 4);
|
||||
tmp[0]=(byte)(val >>> 24);
|
||||
tmp[1]=(byte)(val >>> 16);
|
||||
tmp[2]=(byte)(val >>> 8);
|
||||
tmp[3]=(byte)(val);
|
||||
System.arraycopy(tmp, 0, buffer, index+4, 4);
|
||||
index+=8;
|
||||
}
|
||||
void skip(int n) {
|
||||
index+=n;
|
||||
}
|
||||
void putPad(int n) {
|
||||
while(n>0){
|
||||
buffer[index++]=(byte)0;
|
||||
n--;
|
||||
}
|
||||
}
|
||||
public void putMPInt(byte[] foo){
|
||||
int i=foo.length;
|
||||
if((foo[0]&0x80)!=0){
|
||||
i++;
|
||||
putInt(i);
|
||||
putByte((byte)0);
|
||||
}
|
||||
else{
|
||||
putInt(i);
|
||||
}
|
||||
putByte(foo);
|
||||
}
|
||||
public int getLength(){
|
||||
return index-s;
|
||||
}
|
||||
public int getOffSet(){
|
||||
return s;
|
||||
}
|
||||
public void setOffSet(int s){
|
||||
this.s=s;
|
||||
}
|
||||
public long getLong(){
|
||||
long foo = getInt()&0xffffffffL;
|
||||
foo = ((foo<<32)) | (getInt()&0xffffffffL);
|
||||
return foo;
|
||||
}
|
||||
public int getInt(){
|
||||
int foo = getShort();
|
||||
foo = ((foo<<16)&0xffff0000) | (getShort()&0xffff);
|
||||
return foo;
|
||||
}
|
||||
public long getUInt(){
|
||||
long foo = 0L;
|
||||
long bar = 0L;
|
||||
foo = getByte();
|
||||
foo = ((foo<<8)&0xff00)|(getByte()&0xff);
|
||||
bar = getByte();
|
||||
bar = ((bar<<8)&0xff00)|(getByte()&0xff);
|
||||
foo = ((foo<<16)&0xffff0000) | (bar&0xffff);
|
||||
return foo;
|
||||
}
|
||||
int getShort() {
|
||||
int foo = getByte();
|
||||
foo = ((foo<<8)&0xff00)|(getByte()&0xff);
|
||||
return foo;
|
||||
}
|
||||
public int getByte() {
|
||||
return (buffer[s++]&0xff);
|
||||
}
|
||||
public void getByte(byte[] foo) {
|
||||
getByte(foo, 0, foo.length);
|
||||
}
|
||||
void getByte(byte[] foo, int start, int len) {
|
||||
System.arraycopy(buffer, s, foo, start, len);
|
||||
s+=len;
|
||||
}
|
||||
public int getByte(int len) {
|
||||
int foo=s;
|
||||
s+=len;
|
||||
return foo;
|
||||
}
|
||||
public byte[] getMPInt() {
|
||||
int i=getInt(); // uint32
|
||||
if(i<0 || // bigger than 0x7fffffff
|
||||
i>8*1024){
|
||||
// TODO: an exception should be thrown.
|
||||
i = 8*1024; // the session will be broken, but working around OOME.
|
||||
}
|
||||
byte[] foo=new byte[i];
|
||||
getByte(foo, 0, i);
|
||||
return foo;
|
||||
}
|
||||
public byte[] getMPIntBits() {
|
||||
int bits=getInt();
|
||||
int bytes=(bits+7)/8;
|
||||
byte[] foo=new byte[bytes];
|
||||
getByte(foo, 0, bytes);
|
||||
if((foo[0]&0x80)!=0){
|
||||
byte[] bar=new byte[foo.length+1];
|
||||
bar[0]=0; // ??
|
||||
System.arraycopy(foo, 0, bar, 1, foo.length);
|
||||
foo=bar;
|
||||
}
|
||||
return foo;
|
||||
}
|
||||
public byte[] getString() {
|
||||
int i = getInt(); // uint32
|
||||
if(i<0 || // bigger than 0x7fffffff
|
||||
i>256*1024){
|
||||
// TODO: an exception should be thrown.
|
||||
i = 256*1024; // the session will be broken, but working around OOME.
|
||||
}
|
||||
byte[] foo=new byte[i];
|
||||
getByte(foo, 0, i);
|
||||
return foo;
|
||||
}
|
||||
byte[] getString(int[]start, int[]len) {
|
||||
int i=getInt();
|
||||
start[0]=getByte(i);
|
||||
len[0]=i;
|
||||
return buffer;
|
||||
}
|
||||
public void reset(){
|
||||
index=0;
|
||||
s=0;
|
||||
}
|
||||
public void shift(){
|
||||
if(s==0)return;
|
||||
System.arraycopy(buffer, s, buffer, 0, index-s);
|
||||
index=index-s;
|
||||
s=0;
|
||||
}
|
||||
void rewind(){
|
||||
s=0;
|
||||
}
|
||||
|
||||
byte getCommand(){
|
||||
return buffer[5];
|
||||
}
|
||||
|
||||
void checkFreeSize(int n){
|
||||
if(buffer.length<index+n){
|
||||
byte[] tmp = new byte[buffer.length*2];
|
||||
System.arraycopy(buffer, 0, tmp, 0, index);
|
||||
buffer = tmp;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
static String[] chars={
|
||||
"0","1","2","3","4","5","6","7","8","9", "a","b","c","d","e","f"
|
||||
};
|
||||
static void dump_buffer(){
|
||||
int foo;
|
||||
for(int i=0; i<tmp_buffer_index; i++){
|
||||
foo=tmp_buffer[i]&0xff;
|
||||
System.err.print(chars[(foo>>>4)&0xf]);
|
||||
System.err.print(chars[foo&0xf]);
|
||||
if(i%16==15){
|
||||
System.err.println("");
|
||||
continue;
|
||||
}
|
||||
if(i>0 && i%2==1){
|
||||
System.err.print(" ");
|
||||
}
|
||||
}
|
||||
System.err.println("");
|
||||
}
|
||||
static void dump(byte[] b){
|
||||
dump(b, 0, b.length);
|
||||
}
|
||||
static void dump(byte[] b, int s, int l){
|
||||
for(int i=s; i<s+l; i++){
|
||||
System.err.print(Integer.toHexString(b[i]&0xff)+":");
|
||||
}
|
||||
System.err.println("");
|
||||
}
|
||||
*/
|
||||
|
||||
}
|
@ -1,665 +0,0 @@
|
||||
/* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
|
||||
/*
|
||||
Copyright (c) 2002-2011 ymnk, JCraft,Inc. All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice,
|
||||
this list of conditions and the following disclaimer.
|
||||
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the distribution.
|
||||
|
||||
3. The names of the authors may not be used to endorse or promote products
|
||||
derived from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
|
||||
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
|
||||
INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
|
||||
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
||||
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
package com.jcraft.jsch;
|
||||
|
||||
import java.io.PipedInputStream;
|
||||
import java.io.PipedOutputStream;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
|
||||
public abstract class Channel implements Runnable{
|
||||
|
||||
static final int SSH_MSG_CHANNEL_OPEN_CONFIRMATION= 91;
|
||||
static final int SSH_MSG_CHANNEL_OPEN_FAILURE= 92;
|
||||
static final int SSH_MSG_CHANNEL_WINDOW_ADJUST= 93;
|
||||
|
||||
static final int SSH_OPEN_ADMINISTRATIVELY_PROHIBITED= 1;
|
||||
static final int SSH_OPEN_CONNECT_FAILED= 2;
|
||||
static final int SSH_OPEN_UNKNOWN_CHANNEL_TYPE= 3;
|
||||
static final int SSH_OPEN_RESOURCE_SHORTAGE= 4;
|
||||
|
||||
static int index=0;
|
||||
private static java.util.Vector pool=new java.util.Vector();
|
||||
static Channel getChannel(String type){
|
||||
if(type.equals("session")){
|
||||
return new ChannelSession();
|
||||
}
|
||||
if(type.equals("shell")){
|
||||
return new ChannelShell();
|
||||
}
|
||||
if(type.equals("exec")){
|
||||
return new ChannelExec();
|
||||
}
|
||||
if(type.equals("x11")){
|
||||
return new ChannelX11();
|
||||
}
|
||||
if(type.equals("auth-agent@openssh.com")){
|
||||
return new ChannelAgentForwarding();
|
||||
}
|
||||
if(type.equals("direct-tcpip")){
|
||||
return new ChannelDirectTCPIP();
|
||||
}
|
||||
if(type.equals("forwarded-tcpip")){
|
||||
return new ChannelForwardedTCPIP();
|
||||
}
|
||||
if(type.equals("sftp")){
|
||||
return new ChannelSftp();
|
||||
}
|
||||
if(type.equals("subsystem")){
|
||||
return new ChannelSubsystem();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
static Channel getChannel(int id, Session session){
|
||||
synchronized(pool){
|
||||
for(int i=0; i<pool.size(); i++){
|
||||
Channel c=(Channel)(pool.elementAt(i));
|
||||
if(c.id==id && c.session==session) return c;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
static void del(Channel c){
|
||||
synchronized(pool){
|
||||
pool.removeElement(c);
|
||||
}
|
||||
}
|
||||
|
||||
int id;
|
||||
volatile int recipient=-1;
|
||||
protected byte[] type=Util.str2byte("foo");
|
||||
volatile int lwsize_max=0x100000;
|
||||
volatile int lwsize=lwsize_max; // local initial window size
|
||||
volatile int lmpsize=0x4000; // local maximum packet size
|
||||
|
||||
volatile long rwsize=0; // remote initial window size
|
||||
volatile int rmpsize=0; // remote maximum packet size
|
||||
|
||||
IO io=null;
|
||||
Thread thread=null;
|
||||
|
||||
volatile boolean eof_local=false;
|
||||
volatile boolean eof_remote=false;
|
||||
|
||||
volatile boolean close=false;
|
||||
volatile boolean connected=false;
|
||||
volatile boolean open_confirmation=false;
|
||||
|
||||
volatile int exitstatus=-1;
|
||||
|
||||
volatile int reply=0;
|
||||
volatile int connectTimeout=0;
|
||||
|
||||
private Session session;
|
||||
|
||||
int notifyme=0;
|
||||
|
||||
Channel(){
|
||||
synchronized(pool){
|
||||
id=index++;
|
||||
pool.addElement(this);
|
||||
}
|
||||
}
|
||||
synchronized void setRecipient(int foo){
|
||||
this.recipient=foo;
|
||||
if(notifyme>0)
|
||||
notifyAll();
|
||||
}
|
||||
int getRecipient(){
|
||||
return recipient;
|
||||
}
|
||||
|
||||
void init() throws JSchException {
|
||||
}
|
||||
|
||||
public void connect() throws JSchException{
|
||||
connect(0);
|
||||
}
|
||||
|
||||
public void connect(int connectTimeout) throws JSchException{
|
||||
Session _session=getSession();
|
||||
if(!_session.isConnected()){
|
||||
throw new JSchException("session is down");
|
||||
}
|
||||
this.connectTimeout=connectTimeout;
|
||||
try{
|
||||
Buffer buf=new Buffer(100);
|
||||
Packet packet=new Packet(buf);
|
||||
// send
|
||||
// byte SSH_MSG_CHANNEL_OPEN(90)
|
||||
// string channel type //
|
||||
// uint32 sender channel // 0
|
||||
// uint32 initial window size // 0x100000(65536)
|
||||
// uint32 maxmum packet size // 0x4000(16384)
|
||||
packet.reset();
|
||||
buf.putByte((byte)90);
|
||||
buf.putString(this.type);
|
||||
buf.putInt(this.id);
|
||||
buf.putInt(this.lwsize);
|
||||
buf.putInt(this.lmpsize);
|
||||
_session.write(packet);
|
||||
int retry=10;
|
||||
long start=System.currentTimeMillis();
|
||||
long timeout=connectTimeout;
|
||||
if(timeout!=0L) retry = 1;
|
||||
synchronized(this){
|
||||
while(this.getRecipient()==-1 &&
|
||||
_session.isConnected() &&
|
||||
retry>0){
|
||||
if(timeout>0L){
|
||||
if((System.currentTimeMillis()-start)>timeout){
|
||||
retry=0;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
try{
|
||||
long t = timeout==0L ? 5000L : timeout;
|
||||
this.notifyme=1;
|
||||
wait(t);
|
||||
}
|
||||
catch(java.lang.InterruptedException e){
|
||||
}
|
||||
finally{
|
||||
this.notifyme=0;
|
||||
}
|
||||
retry--;
|
||||
}
|
||||
}
|
||||
if(!_session.isConnected()){
|
||||
throw new JSchException("session is down");
|
||||
}
|
||||
if(this.getRecipient()==-1){ // timeout
|
||||
throw new JSchException("channel is not opened.");
|
||||
}
|
||||
if(this.open_confirmation==false){ // SSH_MSG_CHANNEL_OPEN_FAILURE
|
||||
throw new JSchException("channel is not opened.");
|
||||
}
|
||||
|
||||
connected=true;
|
||||
start();
|
||||
}
|
||||
catch(Exception e){
|
||||
connected=false;
|
||||
disconnect();
|
||||
if(e instanceof JSchException)
|
||||
throw (JSchException)e;
|
||||
throw new JSchException(e.toString(), e);
|
||||
}
|
||||
}
|
||||
|
||||
public void setXForwarding(boolean foo){
|
||||
}
|
||||
|
||||
public void start() throws JSchException{}
|
||||
|
||||
public boolean isEOF() {return eof_remote;}
|
||||
|
||||
void getData(Buffer buf){
|
||||
setRecipient(buf.getInt());
|
||||
setRemoteWindowSize(buf.getUInt());
|
||||
setRemotePacketSize(buf.getInt());
|
||||
}
|
||||
|
||||
public void setInputStream(InputStream in){
|
||||
io.setInputStream(in, false);
|
||||
}
|
||||
public void setInputStream(InputStream in, boolean dontclose){
|
||||
io.setInputStream(in, dontclose);
|
||||
}
|
||||
public void setOutputStream(OutputStream out){
|
||||
io.setOutputStream(out, false);
|
||||
}
|
||||
public void setOutputStream(OutputStream out, boolean dontclose){
|
||||
io.setOutputStream(out, dontclose);
|
||||
}
|
||||
public void setExtOutputStream(OutputStream out){
|
||||
io.setExtOutputStream(out, false);
|
||||
}
|
||||
public void setExtOutputStream(OutputStream out, boolean dontclose){
|
||||
io.setExtOutputStream(out, dontclose);
|
||||
}
|
||||
public InputStream getInputStream() throws IOException {
|
||||
PipedInputStream in=
|
||||
new MyPipedInputStream(
|
||||
32*1024 // this value should be customizable.
|
||||
);
|
||||
io.setOutputStream(new PassiveOutputStream(in), false);
|
||||
return in;
|
||||
}
|
||||
public InputStream getExtInputStream() throws IOException {
|
||||
PipedInputStream in=
|
||||
new MyPipedInputStream(
|
||||
32*1024 // this value should be customizable.
|
||||
);
|
||||
io.setExtOutputStream(new PassiveOutputStream(in), false);
|
||||
return in;
|
||||
}
|
||||
public OutputStream getOutputStream() throws IOException {
|
||||
/*
|
||||
PipedOutputStream out=new PipedOutputStream();
|
||||
io.setInputStream(new PassiveInputStream(out
|
||||
, 32*1024
|
||||
), false);
|
||||
return out;
|
||||
*/
|
||||
|
||||
final Channel channel=this;
|
||||
OutputStream out=new OutputStream(){
|
||||
private int dataLen=0;
|
||||
private Buffer buffer=null;
|
||||
private Packet packet=null;
|
||||
private boolean closed=false;
|
||||
private synchronized void init() throws java.io.IOException{
|
||||
buffer=new Buffer(rmpsize);
|
||||
packet=new Packet(buffer);
|
||||
|
||||
byte[] _buf=buffer.buffer;
|
||||
if(_buf.length-(14+0)-Session.buffer_margin<=0){
|
||||
buffer=null;
|
||||
packet=null;
|
||||
throw new IOException("failed to initialize the channel.");
|
||||
}
|
||||
|
||||
}
|
||||
byte[] b=new byte[1];
|
||||
public void write(int w) throws java.io.IOException{
|
||||
b[0]=(byte)w;
|
||||
write(b, 0, 1);
|
||||
}
|
||||
public void write(byte[] buf, int s, int l) throws java.io.IOException{
|
||||
if(packet==null){
|
||||
init();
|
||||
}
|
||||
|
||||
if(closed){
|
||||
throw new java.io.IOException("Already closed");
|
||||
}
|
||||
|
||||
byte[] _buf=buffer.buffer;
|
||||
int _bufl=_buf.length;
|
||||
while(l>0){
|
||||
int _l=l;
|
||||
if(l>_bufl-(14+dataLen)-Session.buffer_margin){
|
||||
_l=_bufl-(14+dataLen)-Session.buffer_margin;
|
||||
}
|
||||
|
||||
if(_l<=0){
|
||||
flush();
|
||||
continue;
|
||||
}
|
||||
|
||||
System.arraycopy(buf, s, _buf, 14+dataLen, _l);
|
||||
dataLen+=_l;
|
||||
s+=_l;
|
||||
l-=_l;
|
||||
}
|
||||
}
|
||||
|
||||
public void flush() throws java.io.IOException{
|
||||
if(closed){
|
||||
throw new java.io.IOException("Already closed");
|
||||
}
|
||||
if(dataLen==0)
|
||||
return;
|
||||
packet.reset();
|
||||
buffer.putByte((byte)Session.SSH_MSG_CHANNEL_DATA);
|
||||
buffer.putInt(recipient);
|
||||
buffer.putInt(dataLen);
|
||||
buffer.skip(dataLen);
|
||||
try{
|
||||
int foo=dataLen;
|
||||
dataLen=0;
|
||||
getSession().write(packet, channel, foo);
|
||||
}
|
||||
catch(Exception e){
|
||||
close();
|
||||
throw new java.io.IOException(e.toString());
|
||||
}
|
||||
|
||||
}
|
||||
public void close() throws java.io.IOException{
|
||||
if(packet==null){
|
||||
try{
|
||||
init();
|
||||
}
|
||||
catch(java.io.IOException e){
|
||||
// close should be finished silently.
|
||||
return;
|
||||
}
|
||||
}
|
||||
if(closed){
|
||||
return;
|
||||
}
|
||||
if(dataLen>0){
|
||||
flush();
|
||||
}
|
||||
channel.eof();
|
||||
closed=true;
|
||||
}
|
||||
};
|
||||
return out;
|
||||
}
|
||||
|
||||
class MyPipedInputStream extends PipedInputStream{
|
||||
MyPipedInputStream() throws IOException{ super(); }
|
||||
MyPipedInputStream(int size) throws IOException{
|
||||
super();
|
||||
buffer=new byte[size];
|
||||
}
|
||||
MyPipedInputStream(PipedOutputStream out) throws IOException{ super(out); }
|
||||
MyPipedInputStream(PipedOutputStream out, int size) throws IOException{
|
||||
super(out);
|
||||
buffer=new byte[size];
|
||||
}
|
||||
|
||||
/*
|
||||
* TODO: We should have our own Piped[I/O]Stream implementation.
|
||||
* Before accepting data, JDK's PipedInputStream will check the existence of
|
||||
* reader thread, and if it is not alive, the stream will be closed.
|
||||
* That behavior may cause the problem if multiple threads make access to it.
|
||||
*/
|
||||
public synchronized void updateReadSide() throws IOException {
|
||||
if(available() != 0){ // not empty
|
||||
return;
|
||||
}
|
||||
in = 0;
|
||||
out = 0;
|
||||
buffer[in++] = 0;
|
||||
read();
|
||||
}
|
||||
}
|
||||
void setLocalWindowSizeMax(int foo){ this.lwsize_max=foo; }
|
||||
void setLocalWindowSize(int foo){ this.lwsize=foo; }
|
||||
void setLocalPacketSize(int foo){ this.lmpsize=foo; }
|
||||
synchronized void setRemoteWindowSize(long foo){ this.rwsize=foo; }
|
||||
synchronized void addRemoteWindowSize(int foo){
|
||||
this.rwsize+=foo;
|
||||
if(notifyme>0)
|
||||
notifyAll();
|
||||
}
|
||||
void setRemotePacketSize(int foo){ this.rmpsize=foo; }
|
||||
|
||||
public void run(){
|
||||
}
|
||||
|
||||
void write(byte[] foo) throws IOException {
|
||||
write(foo, 0, foo.length);
|
||||
}
|
||||
void write(byte[] foo, int s, int l) throws IOException {
|
||||
try{
|
||||
io.put(foo, s, l);
|
||||
}catch(NullPointerException e){}
|
||||
}
|
||||
void write_ext(byte[] foo, int s, int l) throws IOException {
|
||||
try{
|
||||
io.put_ext(foo, s, l);
|
||||
}catch(NullPointerException e){}
|
||||
}
|
||||
|
||||
void eof_remote(){
|
||||
eof_remote=true;
|
||||
try{
|
||||
io.out_close();
|
||||
}
|
||||
catch(NullPointerException e){}
|
||||
}
|
||||
|
||||
void eof(){
|
||||
if(eof_local)return;
|
||||
eof_local=true;
|
||||
|
||||
try{
|
||||
Buffer buf=new Buffer(100);
|
||||
Packet packet=new Packet(buf);
|
||||
packet.reset();
|
||||
buf.putByte((byte)Session.SSH_MSG_CHANNEL_EOF);
|
||||
buf.putInt(getRecipient());
|
||||
synchronized(this){
|
||||
if(!close)
|
||||
getSession().write(packet);
|
||||
}
|
||||
}
|
||||
catch(Exception e){
|
||||
//System.err.println("Channel.eof");
|
||||
//e.printStackTrace();
|
||||
}
|
||||
/*
|
||||
if(!isConnected()){ disconnect(); }
|
||||
*/
|
||||
}
|
||||
|
||||
/*
|
||||
http://www1.ietf.org/internet-drafts/draft-ietf-secsh-connect-24.txt
|
||||
|
||||
5.3 Closing a Channel
|
||||
When a party will no longer send more data to a channel, it SHOULD
|
||||
send SSH_MSG_CHANNEL_EOF.
|
||||
|
||||
byte SSH_MSG_CHANNEL_EOF
|
||||
uint32 recipient_channel
|
||||
|
||||
No explicit response is sent to this message. However, the
|
||||
application may send EOF to whatever is at the other end of the
|
||||
channel. Note that the channel remains open after this message, and
|
||||
more data may still be sent in the other direction. This message
|
||||
does not consume window space and can be sent even if no window space
|
||||
is available.
|
||||
|
||||
When either party wishes to terminate the channel, it sends
|
||||
SSH_MSG_CHANNEL_CLOSE. Upon receiving this message, a party MUST
|
||||
send back a SSH_MSG_CHANNEL_CLOSE unless it has already sent this
|
||||
message for the channel. The channel is considered closed for a
|
||||
party when it has both sent and received SSH_MSG_CHANNEL_CLOSE, and
|
||||
the party may then reuse the channel number. A party MAY send
|
||||
SSH_MSG_CHANNEL_CLOSE without having sent or received
|
||||
SSH_MSG_CHANNEL_EOF.
|
||||
|
||||
byte SSH_MSG_CHANNEL_CLOSE
|
||||
uint32 recipient_channel
|
||||
|
||||
This message does not consume window space and can be sent even if no
|
||||
window space is available.
|
||||
|
||||
It is recommended that any data sent before this message is delivered
|
||||
to the actual destination, if possible.
|
||||
*/
|
||||
|
||||
void close(){
|
||||
if(close)return;
|
||||
close=true;
|
||||
eof_local=eof_remote=true;
|
||||
|
||||
try{
|
||||
Buffer buf=new Buffer(100);
|
||||
Packet packet=new Packet(buf);
|
||||
packet.reset();
|
||||
buf.putByte((byte)Session.SSH_MSG_CHANNEL_CLOSE);
|
||||
buf.putInt(getRecipient());
|
||||
synchronized(this){
|
||||
getSession().write(packet);
|
||||
}
|
||||
}
|
||||
catch(Exception e){
|
||||
//e.printStackTrace();
|
||||
}
|
||||
}
|
||||
public boolean isClosed(){
|
||||
return close;
|
||||
}
|
||||
static void disconnect(Session session){
|
||||
Channel[] channels=null;
|
||||
int count=0;
|
||||
synchronized(pool){
|
||||
channels=new Channel[pool.size()];
|
||||
for(int i=0; i<pool.size(); i++){
|
||||
try{
|
||||
Channel c=((Channel)(pool.elementAt(i)));
|
||||
if(c.session==session){
|
||||
channels[count++]=c;
|
||||
}
|
||||
}
|
||||
catch(Exception e){
|
||||
}
|
||||
}
|
||||
}
|
||||
for(int i=0; i<count; i++){
|
||||
channels[i].disconnect();
|
||||
}
|
||||
}
|
||||
|
||||
public void disconnect(){
|
||||
//System.err.println(this+":disconnect "+io+" "+connected);
|
||||
//Thread.dumpStack();
|
||||
|
||||
try{
|
||||
|
||||
synchronized(this){
|
||||
if(!connected){
|
||||
return;
|
||||
}
|
||||
connected=false;
|
||||
}
|
||||
|
||||
close();
|
||||
|
||||
eof_remote=eof_local=true;
|
||||
|
||||
thread=null;
|
||||
|
||||
try{
|
||||
if(io!=null){
|
||||
io.close();
|
||||
}
|
||||
}
|
||||
catch(Exception e){
|
||||
//e.printStackTrace();
|
||||
}
|
||||
// io=null;
|
||||
}
|
||||
finally{
|
||||
Channel.del(this);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isConnected(){
|
||||
Session _session=this.session;
|
||||
if(_session!=null){
|
||||
return _session.isConnected() && connected;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public void sendSignal(String signal) throws Exception {
|
||||
RequestSignal request=new RequestSignal();
|
||||
request.setSignal(signal);
|
||||
request.request(getSession(), this);
|
||||
}
|
||||
|
||||
// public String toString(){
|
||||
// return "Channel: type="+new String(type)+",id="+id+",recipient="+recipient+",window_size="+window_size+",packet_size="+packet_size;
|
||||
// }
|
||||
|
||||
/*
|
||||
class OutputThread extends Thread{
|
||||
Channel c;
|
||||
OutputThread(Channel c){ this.c=c;}
|
||||
public void run(){c.output_thread();}
|
||||
}
|
||||
*/
|
||||
|
||||
class PassiveInputStream extends MyPipedInputStream{
|
||||
PipedOutputStream out;
|
||||
PassiveInputStream(PipedOutputStream out, int size) throws IOException{
|
||||
super(out, size);
|
||||
this.out=out;
|
||||
}
|
||||
PassiveInputStream(PipedOutputStream out) throws IOException{
|
||||
super(out);
|
||||
this.out=out;
|
||||
}
|
||||
public void close() throws IOException{
|
||||
if(out!=null){
|
||||
this.out.close();
|
||||
}
|
||||
out=null;
|
||||
}
|
||||
}
|
||||
class PassiveOutputStream extends PipedOutputStream{
|
||||
PassiveOutputStream(PipedInputStream in) throws IOException{
|
||||
super(in);
|
||||
}
|
||||
}
|
||||
|
||||
void setExitStatus(int status){ exitstatus=status; }
|
||||
public int getExitStatus(){ return exitstatus; }
|
||||
|
||||
void setSession(Session session){
|
||||
this.session=session;
|
||||
}
|
||||
|
||||
public Session getSession() throws JSchException{
|
||||
Session _session=session;
|
||||
if(_session==null){
|
||||
throw new JSchException("session is not available");
|
||||
}
|
||||
return _session;
|
||||
}
|
||||
public int getId(){ return id; }
|
||||
|
||||
protected void sendOpenConfirmation() throws Exception{
|
||||
Buffer buf=new Buffer(100);
|
||||
Packet packet=new Packet(buf);
|
||||
packet.reset();
|
||||
buf.putByte((byte)SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
|
||||
buf.putInt(getRecipient());
|
||||
buf.putInt(id);
|
||||
buf.putInt(lwsize);
|
||||
buf.putInt(lmpsize);
|
||||
getSession().write(packet);
|
||||
}
|
||||
|
||||
protected void sendOpenFailure(int reasoncode){
|
||||
try{
|
||||
Buffer buf=new Buffer(100);
|
||||
Packet packet=new Packet(buf);
|
||||
packet.reset();
|
||||
buf.putByte((byte)SSH_MSG_CHANNEL_OPEN_FAILURE);
|
||||
buf.putInt(getRecipient());
|
||||
buf.putInt(reasoncode);
|
||||
buf.putString(Util.str2byte("open failed"));
|
||||
buf.putString(Util.empty);
|
||||
getSession().write(packet);
|
||||
}
|
||||
catch(Exception e){
|
||||
}
|
||||
}
|
||||
}
|
@ -1,227 +0,0 @@
|
||||
/* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
|
||||
/*
|
||||
Copyright (c) 2006-2011 ymnk, JCraft,Inc. All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice,
|
||||
this list of conditions and the following disclaimer.
|
||||
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the distribution.
|
||||
|
||||
3. The names of the authors may not be used to endorse or promote products
|
||||
derived from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
|
||||
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
|
||||
INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
|
||||
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
||||
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
package com.jcraft.jsch;
|
||||
|
||||
import java.net.*;
|
||||
import java.util.Vector;
|
||||
|
||||
class ChannelAgentForwarding extends Channel{
|
||||
|
||||
static private final int LOCAL_WINDOW_SIZE_MAX=0x20000;
|
||||
static private final int LOCAL_MAXIMUM_PACKET_SIZE=0x4000;
|
||||
|
||||
private final int SSH2_AGENTC_REQUEST_IDENTITIES=11;
|
||||
private final int SSH2_AGENT_IDENTITIES_ANSWER=12;
|
||||
private final int SSH2_AGENTC_SIGN_REQUEST=13;
|
||||
private final int SSH2_AGENT_SIGN_RESPONSE=14;
|
||||
private final int SSH2_AGENTC_ADD_IDENTITY=17;
|
||||
private final int SSH2_AGENTC_REMOVE_IDENTITY=18;
|
||||
private final int SSH2_AGENTC_REMOVE_ALL_IDENTITIES=19;
|
||||
private final int SSH2_AGENT_FAILURE=30;
|
||||
|
||||
boolean init=true;
|
||||
|
||||
private Buffer rbuf=null;
|
||||
private Buffer wbuf=null;
|
||||
private Packet packet=null;
|
||||
private Buffer mbuf=null;
|
||||
|
||||
ChannelAgentForwarding(){
|
||||
super();
|
||||
|
||||
setLocalWindowSizeMax(LOCAL_WINDOW_SIZE_MAX);
|
||||
setLocalWindowSize(LOCAL_WINDOW_SIZE_MAX);
|
||||
setLocalPacketSize(LOCAL_MAXIMUM_PACKET_SIZE);
|
||||
|
||||
type=Util.str2byte("auth-agent@openssh.com");
|
||||
rbuf=new Buffer();
|
||||
rbuf.reset();
|
||||
//wbuf=new Buffer(rmpsize);
|
||||
//packet=new Packet(wbuf);
|
||||
mbuf=new Buffer();
|
||||
connected=true;
|
||||
}
|
||||
|
||||
public void run(){
|
||||
try{
|
||||
sendOpenConfirmation();
|
||||
}
|
||||
catch(Exception e){
|
||||
close=true;
|
||||
disconnect();
|
||||
}
|
||||
}
|
||||
|
||||
void write(byte[] foo, int s, int l) throws java.io.IOException {
|
||||
|
||||
if(packet==null){
|
||||
wbuf=new Buffer(rmpsize);
|
||||
packet=new Packet(wbuf);
|
||||
}
|
||||
|
||||
rbuf.shift();
|
||||
if(rbuf.buffer.length<rbuf.index+l){
|
||||
byte[] newbuf=new byte[rbuf.s+l];
|
||||
System.arraycopy(rbuf.buffer, 0, newbuf, 0, rbuf.buffer.length);
|
||||
rbuf.buffer=newbuf;
|
||||
}
|
||||
|
||||
rbuf.putByte(foo, s, l);
|
||||
|
||||
int mlen=rbuf.getInt();
|
||||
if(mlen>rbuf.getLength()){
|
||||
rbuf.s-=4;
|
||||
return;
|
||||
}
|
||||
|
||||
int typ=rbuf.getByte();
|
||||
|
||||
Session _session=null;
|
||||
try{
|
||||
_session=getSession();
|
||||
}
|
||||
catch(JSchException e){
|
||||
throw new java.io.IOException(e.toString());
|
||||
}
|
||||
|
||||
Vector identities=_session.jsch.identities;
|
||||
UserInfo userinfo=_session.getUserInfo();
|
||||
|
||||
if(typ==SSH2_AGENTC_REQUEST_IDENTITIES){
|
||||
mbuf.reset();
|
||||
mbuf.putByte((byte)SSH2_AGENT_IDENTITIES_ANSWER);
|
||||
synchronized(identities){
|
||||
int count=0;
|
||||
for(int i=0; i<identities.size(); i++){
|
||||
Identity identity=(Identity)(identities.elementAt(i));
|
||||
if(identity.getPublicKeyBlob()!=null)
|
||||
count++;
|
||||
}
|
||||
mbuf.putInt(count);
|
||||
for(int i=0; i<identities.size(); i++){
|
||||
Identity identity=(Identity)(identities.elementAt(i));
|
||||
byte[] pubkeyblob=identity.getPublicKeyBlob();
|
||||
if(pubkeyblob==null)
|
||||
continue;
|
||||
mbuf.putString(pubkeyblob);
|
||||
mbuf.putString(Util.empty);
|
||||
}
|
||||
}
|
||||
byte[] bar=new byte[mbuf.getLength()];
|
||||
mbuf.getByte(bar);
|
||||
|
||||
send(bar);
|
||||
}
|
||||
else if(typ==SSH2_AGENTC_SIGN_REQUEST){
|
||||
byte[] blob=rbuf.getString();
|
||||
byte[] data=rbuf.getString();
|
||||
int flags=rbuf.getInt();
|
||||
|
||||
// if((flags & 1)!=0){ //SSH_AGENT_OLD_SIGNATURE // old OpenSSH 2.0, 2.1
|
||||
// datafellows = SSH_BUG_SIGBLOB;
|
||||
// }
|
||||
|
||||
Identity identity=null;
|
||||
synchronized(identities){
|
||||
for(int i=0; i<identities.size(); i++){
|
||||
Identity _identity=(Identity)(identities.elementAt(i));
|
||||
if(_identity.getPublicKeyBlob()==null)
|
||||
continue;
|
||||
if(!Util.array_equals(blob, _identity.getPublicKeyBlob())){
|
||||
continue;
|
||||
}
|
||||
if(_identity.isEncrypted()){
|
||||
if(userinfo==null)
|
||||
continue;
|
||||
while(_identity.isEncrypted()){
|
||||
if(!userinfo.promptPassphrase("Passphrase for "+_identity.getName())){
|
||||
break;
|
||||
}
|
||||
|
||||
String _passphrase=userinfo.getPassphrase();
|
||||
if(_passphrase==null){
|
||||
break;
|
||||
}
|
||||
|
||||
byte[] passphrase=Util.str2byte(_passphrase);
|
||||
try{
|
||||
if(_identity.setPassphrase(passphrase)){
|
||||
break;
|
||||
}
|
||||
}
|
||||
catch(JSchException e){
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(!_identity.isEncrypted()){
|
||||
identity=_identity;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
byte[] signature=null;
|
||||
|
||||
if(identity!=null){
|
||||
signature=identity.getSignature(data);
|
||||
}
|
||||
|
||||
mbuf.reset();
|
||||
if(signature==null){
|
||||
mbuf.putByte((byte)SSH2_AGENT_FAILURE);
|
||||
}
|
||||
else{
|
||||
mbuf.putByte((byte)SSH2_AGENT_SIGN_RESPONSE);
|
||||
mbuf.putString(signature);
|
||||
}
|
||||
|
||||
byte[] bar=new byte[mbuf.getLength()];
|
||||
mbuf.getByte(bar);
|
||||
|
||||
send(bar);
|
||||
}
|
||||
}
|
||||
|
||||
private void send(byte[] message){
|
||||
packet.reset();
|
||||
wbuf.putByte((byte)Session.SSH_MSG_CHANNEL_DATA);
|
||||
wbuf.putInt(recipient);
|
||||
wbuf.putInt(4+message.length);
|
||||
wbuf.putString(message);
|
||||
|
||||
try{
|
||||
getSession().write(packet, this, 4+message.length);
|
||||
}
|
||||
catch(Exception e){
|
||||
}
|
||||
}
|
||||
}
|
@ -1,194 +0,0 @@
|
||||
/* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
|
||||
/*
|
||||
Copyright (c) 2002-2011 ymnk, JCraft,Inc. All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice,
|
||||
this list of conditions and the following disclaimer.
|
||||
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the distribution.
|
||||
|
||||
3. The names of the authors may not be used to endorse or promote products
|
||||
derived from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
|
||||
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
|
||||
INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
|
||||
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
||||
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
package com.jcraft.jsch;
|
||||
|
||||
import java.io.*;
|
||||
|
||||
public class ChannelDirectTCPIP extends Channel{
|
||||
|
||||
static private final int LOCAL_WINDOW_SIZE_MAX=0x20000;
|
||||
static private final int LOCAL_MAXIMUM_PACKET_SIZE=0x4000;
|
||||
|
||||
String host;
|
||||
int port;
|
||||
|
||||
String originator_IP_address="127.0.0.1";
|
||||
int originator_port=0;
|
||||
|
||||
ChannelDirectTCPIP(){
|
||||
super();
|
||||
setLocalWindowSizeMax(LOCAL_WINDOW_SIZE_MAX);
|
||||
setLocalWindowSize(LOCAL_WINDOW_SIZE_MAX);
|
||||
setLocalPacketSize(LOCAL_MAXIMUM_PACKET_SIZE);
|
||||
}
|
||||
|
||||
void init (){
|
||||
try{
|
||||
io=new IO();
|
||||
}
|
||||
catch(Exception e){
|
||||
System.err.println(e);
|
||||
}
|
||||
}
|
||||
|
||||
public void connect() throws JSchException{
|
||||
try{
|
||||
Session _session=getSession();
|
||||
if(!_session.isConnected()){
|
||||
throw new JSchException("session is down");
|
||||
}
|
||||
Buffer buf=new Buffer(150);
|
||||
Packet packet=new Packet(buf);
|
||||
// send
|
||||
// byte SSH_MSG_CHANNEL_OPEN(90)
|
||||
// string channel type //
|
||||
// uint32 sender channel // 0
|
||||
// uint32 initial window size // 0x100000(65536)
|
||||
// uint32 maxmum packet size // 0x4000(16384)
|
||||
|
||||
packet.reset();
|
||||
buf.putByte((byte)90);
|
||||
buf.putString(Util.str2byte("direct-tcpip"));
|
||||
buf.putInt(id);
|
||||
buf.putInt(lwsize);
|
||||
buf.putInt(lmpsize);
|
||||
buf.putString(Util.str2byte(host));
|
||||
buf.putInt(port);
|
||||
buf.putString(Util.str2byte(originator_IP_address));
|
||||
buf.putInt(originator_port);
|
||||
_session.write(packet);
|
||||
|
||||
int retry=10;
|
||||
long start=System.currentTimeMillis();
|
||||
long timeout=connectTimeout;
|
||||
if(timeout!=0L) retry = 1;
|
||||
synchronized(this){
|
||||
while(this.getRecipient()==-1 &&
|
||||
_session.isConnected() &&
|
||||
retry>0){
|
||||
if(timeout>0L){
|
||||
if((System.currentTimeMillis()-start)>timeout){
|
||||
retry=0;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
try{
|
||||
long t = timeout==0L ? 5000L : timeout;
|
||||
this.notifyme=1;
|
||||
wait(t);
|
||||
}
|
||||
catch(java.lang.InterruptedException e){
|
||||
}
|
||||
finally{
|
||||
this.notifyme=0;
|
||||
}
|
||||
retry--;
|
||||
}
|
||||
}
|
||||
if(!_session.isConnected()){
|
||||
throw new JSchException("session is down");
|
||||
}
|
||||
if(this.getRecipient()==-1){ // timeout
|
||||
throw new JSchException("channel is not opened.");
|
||||
}
|
||||
if(this.open_confirmation==false){ // SSH_MSG_CHANNEL_OPEN_FAILURE
|
||||
throw new JSchException("channel is not opened.");
|
||||
}
|
||||
|
||||
connected=true;
|
||||
|
||||
if(io.in!=null){
|
||||
thread=new Thread(this);
|
||||
thread.setName("DirectTCPIP thread "+_session.getHost());
|
||||
if(_session.daemon_thread){
|
||||
thread.setDaemon(_session.daemon_thread);
|
||||
}
|
||||
thread.start();
|
||||
}
|
||||
}
|
||||
catch(Exception e){
|
||||
io.close();
|
||||
io=null;
|
||||
Channel.del(this);
|
||||
if (e instanceof JSchException) {
|
||||
throw (JSchException) e;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void run(){
|
||||
|
||||
Buffer buf=new Buffer(rmpsize);
|
||||
Packet packet=new Packet(buf);
|
||||
int i=0;
|
||||
|
||||
try{
|
||||
Session _session=getSession();
|
||||
while(isConnected() &&
|
||||
thread!=null &&
|
||||
io!=null &&
|
||||
io.in!=null){
|
||||
i=io.in.read(buf.buffer,
|
||||
14,
|
||||
buf.buffer.length-14
|
||||
-Session.buffer_margin
|
||||
);
|
||||
|
||||
if(i<=0){
|
||||
eof();
|
||||
break;
|
||||
}
|
||||
if(close)break;
|
||||
packet.reset();
|
||||
buf.putByte((byte)Session.SSH_MSG_CHANNEL_DATA);
|
||||
buf.putInt(recipient);
|
||||
buf.putInt(i);
|
||||
buf.skip(i);
|
||||
_session.write(packet, this, i);
|
||||
}
|
||||
}
|
||||
catch(Exception e){
|
||||
}
|
||||
disconnect();
|
||||
//System.err.println("connect end");
|
||||
}
|
||||
|
||||
public void setInputStream(InputStream in){
|
||||
io.setInputStream(in);
|
||||
}
|
||||
public void setOutputStream(OutputStream out){
|
||||
io.setOutputStream(out);
|
||||
}
|
||||
|
||||
public void setHost(String host){this.host=host;}
|
||||
public void setPort(int port){this.port=port;}
|
||||
public void setOrgIPAddress(String foo){this.originator_IP_address=foo;}
|
||||
public void setOrgPort(int foo){this.originator_port=foo;}
|
||||
}
|
@ -1,83 +0,0 @@
|
||||
/* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
|
||||
/*
|
||||
Copyright (c) 2002-2011 ymnk, JCraft,Inc. All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice,
|
||||
this list of conditions and the following disclaimer.
|
||||
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the distribution.
|
||||
|
||||
3. The names of the authors may not be used to endorse or promote products
|
||||
derived from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
|
||||
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
|
||||
INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
|
||||
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
||||
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
package com.jcraft.jsch;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class ChannelExec extends ChannelSession{
|
||||
|
||||
byte[] command=new byte[0];
|
||||
|
||||
public void start() throws JSchException{
|
||||
Session _session=getSession();
|
||||
try{
|
||||
sendRequests();
|
||||
Request request=new RequestExec(command);
|
||||
request.request(_session, this);
|
||||
}
|
||||
catch(Exception e){
|
||||
if(e instanceof JSchException) throw (JSchException)e;
|
||||
if(e instanceof Throwable)
|
||||
throw new JSchException("ChannelExec", (Throwable)e);
|
||||
throw new JSchException("ChannelExec");
|
||||
}
|
||||
|
||||
if(io.in!=null){
|
||||
thread=new Thread(this);
|
||||
thread.setName("Exec thread "+_session.getHost());
|
||||
if(_session.daemon_thread){
|
||||
thread.setDaemon(_session.daemon_thread);
|
||||
}
|
||||
thread.start();
|
||||
}
|
||||
}
|
||||
|
||||
public void setCommand(String command){
|
||||
this.command=Util.str2byte(command);
|
||||
}
|
||||
public void setCommand(byte[] command){
|
||||
this.command=command;
|
||||
}
|
||||
|
||||
void init() throws JSchException {
|
||||
io.setInputStream(getSession().in);
|
||||
io.setOutputStream(getSession().out);
|
||||
}
|
||||
|
||||
public void setErrStream(java.io.OutputStream out){
|
||||
setExtOutputStream(out);
|
||||
}
|
||||
public void setErrStream(java.io.OutputStream out, boolean dontclose){
|
||||
setExtOutputStream(out, dontclose);
|
||||
}
|
||||
public java.io.InputStream getErrStream() throws java.io.IOException {
|
||||
return getExtInputStream();
|
||||
}
|
||||
}
|
@ -1,311 +0,0 @@
|
||||
/* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
|
||||
/*
|
||||
Copyright (c) 2002-2011 ymnk, JCraft,Inc. All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice,
|
||||
this list of conditions and the following disclaimer.
|
||||
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the distribution.
|
||||
|
||||
3. The names of the authors may not be used to endorse or promote products
|
||||
derived from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
|
||||
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
|
||||
INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
|
||||
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
||||
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
package com.jcraft.jsch;
|
||||
|
||||
import java.net.*;
|
||||
import java.io.*;
|
||||
|
||||
public class ChannelForwardedTCPIP extends Channel{
|
||||
|
||||
static java.util.Vector pool=new java.util.Vector();
|
||||
|
||||
static private final int LOCAL_WINDOW_SIZE_MAX=0x20000;
|
||||
//static private final int LOCAL_WINDOW_SIZE_MAX=0x100000;
|
||||
static private final int LOCAL_MAXIMUM_PACKET_SIZE=0x4000;
|
||||
|
||||
static private final int TIMEOUT=10*1000;
|
||||
|
||||
SocketFactory factory=null;
|
||||
private Socket socket=null;
|
||||
private ForwardedTCPIPDaemon daemon=null;
|
||||
String target;
|
||||
int lport;
|
||||
int rport;
|
||||
|
||||
ChannelForwardedTCPIP(){
|
||||
super();
|
||||
setLocalWindowSizeMax(LOCAL_WINDOW_SIZE_MAX);
|
||||
setLocalWindowSize(LOCAL_WINDOW_SIZE_MAX);
|
||||
setLocalPacketSize(LOCAL_MAXIMUM_PACKET_SIZE);
|
||||
io=new IO();
|
||||
connected=true;
|
||||
}
|
||||
|
||||
public void run(){
|
||||
try{
|
||||
if(lport==-1){
|
||||
Class c=Class.forName(target);
|
||||
daemon=(ForwardedTCPIPDaemon)c.newInstance();
|
||||
|
||||
PipedOutputStream out=new PipedOutputStream();
|
||||
io.setInputStream(new PassiveInputStream(out
|
||||
, 32*1024
|
||||
), false);
|
||||
|
||||
daemon.setChannel(this, getInputStream(), out);
|
||||
Object[] foo=getPort(getSession(), rport);
|
||||
daemon.setArg((Object[])foo[3]);
|
||||
|
||||
new Thread(daemon).start();
|
||||
}
|
||||
else{
|
||||
socket=(factory==null) ?
|
||||
Util.createSocket(target, lport, TIMEOUT) :
|
||||
factory.createSocket(target, lport);
|
||||
socket.setTcpNoDelay(true);
|
||||
io.setInputStream(socket.getInputStream());
|
||||
io.setOutputStream(socket.getOutputStream());
|
||||
}
|
||||
sendOpenConfirmation();
|
||||
}
|
||||
catch(Exception e){
|
||||
sendOpenFailure(SSH_OPEN_ADMINISTRATIVELY_PROHIBITED);
|
||||
close=true;
|
||||
disconnect();
|
||||
return;
|
||||
}
|
||||
|
||||
thread=Thread.currentThread();
|
||||
Buffer buf=new Buffer(rmpsize);
|
||||
Packet packet=new Packet(buf);
|
||||
int i=0;
|
||||
try{
|
||||
while(thread!=null &&
|
||||
io!=null &&
|
||||
io.in!=null){
|
||||
i=io.in.read(buf.buffer,
|
||||
14,
|
||||
buf.buffer.length-14
|
||||
-Session.buffer_margin
|
||||
);
|
||||
if(i<=0){
|
||||
eof();
|
||||
break;
|
||||
}
|
||||
packet.reset();
|
||||
if(close)break;
|
||||
buf.putByte((byte)Session.SSH_MSG_CHANNEL_DATA);
|
||||
buf.putInt(recipient);
|
||||
buf.putInt(i);
|
||||
buf.skip(i);
|
||||
getSession().write(packet, this, i);
|
||||
}
|
||||
}
|
||||
catch(Exception e){
|
||||
//System.err.println(e);
|
||||
}
|
||||
//thread=null;
|
||||
//eof();
|
||||
disconnect();
|
||||
}
|
||||
|
||||
void getData(Buffer buf){
|
||||
setRecipient(buf.getInt());
|
||||
setRemoteWindowSize(buf.getUInt());
|
||||
setRemotePacketSize(buf.getInt());
|
||||
byte[] addr=buf.getString();
|
||||
int port=buf.getInt();
|
||||
byte[] orgaddr=buf.getString();
|
||||
int orgport=buf.getInt();
|
||||
|
||||
/*
|
||||
System.err.println("addr: "+Util.byte2str(addr));
|
||||
System.err.println("port: "+port);
|
||||
System.err.println("orgaddr: "+Util.byte2str(orgaddr));
|
||||
System.err.println("orgport: "+orgport);
|
||||
*/
|
||||
|
||||
Session _session=null;
|
||||
try{
|
||||
_session=getSession();
|
||||
}
|
||||
catch(JSchException e){
|
||||
// session has been already down.
|
||||
}
|
||||
|
||||
synchronized(pool){
|
||||
for(int i=0; i<pool.size(); i++){
|
||||
Object[] foo=(Object[])(pool.elementAt(i));
|
||||
if(foo[0]!=_session) continue;
|
||||
if(((Integer)foo[1]).intValue()!=port) continue;
|
||||
this.rport=port;
|
||||
this.target=(String)foo[2];
|
||||
if(foo[3]==null || (foo[3] instanceof Object[])){ this.lport=-1; }
|
||||
else{ this.lport=((Integer)foo[3]).intValue(); }
|
||||
if(foo.length>=6){
|
||||
this.factory=((SocketFactory)foo[5]);
|
||||
}
|
||||
break;
|
||||
}
|
||||
if(target==null){
|
||||
//System.err.println("??");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static Object[] getPort(Session session, int rport){
|
||||
synchronized(pool){
|
||||
for(int i=0; i<pool.size(); i++){
|
||||
Object[] bar=(Object[])(pool.elementAt(i));
|
||||
if(bar[0]!=session) continue;
|
||||
if(((Integer)bar[1]).intValue()!=rport) continue;
|
||||
return bar;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
static String[] getPortForwarding(Session session){
|
||||
java.util.Vector foo=new java.util.Vector();
|
||||
synchronized(pool){
|
||||
for(int i=0; i<pool.size(); i++){
|
||||
Object[] bar=(Object[])(pool.elementAt(i));
|
||||
if(bar[0]!=session) continue;
|
||||
if(bar[3]==null){ foo.addElement(bar[1]+":"+bar[2]+":"); }
|
||||
else{ foo.addElement(bar[1]+":"+bar[2]+":"+bar[3]); }
|
||||
}
|
||||
}
|
||||
String[] bar=new String[foo.size()];
|
||||
for(int i=0; i<foo.size(); i++){
|
||||
bar[i]=(String)(foo.elementAt(i));
|
||||
}
|
||||
return bar;
|
||||
}
|
||||
|
||||
static String normalize(String address){
|
||||
if(address==null){ return "localhost"; }
|
||||
else if(address.length()==0 || address.equals("*")){ return ""; }
|
||||
else{ return address; }
|
||||
}
|
||||
|
||||
static void addPort(Session session, String _address_to_bind, int port, String target, int lport, SocketFactory factory) throws JSchException{
|
||||
String address_to_bind=normalize(_address_to_bind);
|
||||
synchronized(pool){
|
||||
if(getPort(session, port)!=null){
|
||||
throw new JSchException("PortForwardingR: remote port "+port+" is already registered.");
|
||||
}
|
||||
Object[] foo=new Object[6];
|
||||
foo[0]=session; foo[1]=new Integer(port);
|
||||
foo[2]=target; foo[3]=new Integer(lport);
|
||||
foo[4]=address_to_bind;
|
||||
foo[5]=factory;
|
||||
pool.addElement(foo);
|
||||
}
|
||||
}
|
||||
static void addPort(Session session, String _address_to_bind, int port, String daemon, Object[] arg) throws JSchException{
|
||||
String address_to_bind=normalize(_address_to_bind);
|
||||
synchronized(pool){
|
||||
if(getPort(session, port)!=null){
|
||||
throw new JSchException("PortForwardingR: remote port "+port+" is already registered.");
|
||||
}
|
||||
Object[] foo=new Object[5];
|
||||
foo[0]=session; foo[1]=new Integer(port);
|
||||
foo[2]=daemon; foo[3]=arg;
|
||||
foo[4]=address_to_bind;
|
||||
pool.addElement(foo);
|
||||
}
|
||||
}
|
||||
static void delPort(ChannelForwardedTCPIP c){
|
||||
Session _session=null;
|
||||
try{
|
||||
_session=c.getSession();
|
||||
}
|
||||
catch(JSchException e){
|
||||
// session has been already down.
|
||||
}
|
||||
if(_session!=null)
|
||||
delPort(_session, c.rport);
|
||||
}
|
||||
static void delPort(Session session, int rport){
|
||||
delPort(session, null, rport);
|
||||
}
|
||||
static void delPort(Session session, String address_to_bind, int rport){
|
||||
synchronized(pool){
|
||||
Object[] foo=null;
|
||||
for(int i=0; i<pool.size(); i++){
|
||||
Object[] bar=(Object[])(pool.elementAt(i));
|
||||
if(bar[0]!=session) continue;
|
||||
if(((Integer)bar[1]).intValue()!=rport) continue;
|
||||
foo=bar;
|
||||
break;
|
||||
}
|
||||
if(foo==null)return;
|
||||
pool.removeElement(foo);
|
||||
if(address_to_bind==null){
|
||||
address_to_bind=(String)foo[4];
|
||||
}
|
||||
if(address_to_bind==null){
|
||||
address_to_bind="0.0.0.0";
|
||||
}
|
||||
}
|
||||
|
||||
Buffer buf=new Buffer(100); // ??
|
||||
Packet packet=new Packet(buf);
|
||||
|
||||
try{
|
||||
// byte SSH_MSG_GLOBAL_REQUEST 80
|
||||
// string "cancel-tcpip-forward"
|
||||
// boolean want_reply
|
||||
// string address_to_bind (e.g. "127.0.0.1")
|
||||
// uint32 port number to bind
|
||||
packet.reset();
|
||||
buf.putByte((byte) 80/*SSH_MSG_GLOBAL_REQUEST*/);
|
||||
buf.putString(Util.str2byte("cancel-tcpip-forward"));
|
||||
buf.putByte((byte)0);
|
||||
buf.putString(Util.str2byte(address_to_bind));
|
||||
buf.putInt(rport);
|
||||
session.write(packet);
|
||||
}
|
||||
catch(Exception e){
|
||||
// throw new JSchException(e.toString());
|
||||
}
|
||||
}
|
||||
static void delPort(Session session){
|
||||
int[] rport=null;
|
||||
int count=0;
|
||||
synchronized(pool){
|
||||
rport=new int[pool.size()];
|
||||
for(int i=0; i<pool.size(); i++){
|
||||
Object[] bar=(Object[])(pool.elementAt(i));
|
||||
if(bar[0]==session) {
|
||||
rport[count++]=((Integer)bar[1]).intValue();
|
||||
}
|
||||
}
|
||||
}
|
||||
for(int i=0; i<count; i++){
|
||||
delPort(session, rport[i]);
|
||||
}
|
||||
}
|
||||
|
||||
public int getRemotePort(){return rport;}
|
||||
void setSocketFactory(SocketFactory factory){
|
||||
this.factory=factory;
|
||||
}
|
||||
}
|
@ -1,276 +0,0 @@
|
||||
/* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
|
||||
/*
|
||||
Copyright (c) 2002-2011 ymnk, JCraft,Inc. All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice,
|
||||
this list of conditions and the following disclaimer.
|
||||
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the distribution.
|
||||
|
||||
3. The names of the authors may not be used to endorse or promote products
|
||||
derived from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
|
||||
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
|
||||
INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
|
||||
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
||||
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
package com.jcraft.jsch;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
class ChannelSession extends Channel{
|
||||
private static byte[] _session=Util.str2byte("session");
|
||||
|
||||
protected boolean agent_forwarding=false;
|
||||
protected boolean xforwading=false;
|
||||
protected Hashtable env=null;
|
||||
|
||||
protected boolean pty=false;
|
||||
|
||||
protected String ttype="vt100";
|
||||
protected int tcol=80;
|
||||
protected int trow=24;
|
||||
protected int twp=640;
|
||||
protected int thp=480;
|
||||
protected byte[] terminal_mode=null;
|
||||
|
||||
ChannelSession(){
|
||||
super();
|
||||
type=_session;
|
||||
io=new IO();
|
||||
}
|
||||
|
||||
/**
|
||||
* Enable the agent forwarding.
|
||||
*
|
||||
* @param enable
|
||||
*/
|
||||
public void setAgentForwarding(boolean enable){
|
||||
agent_forwarding=enable;
|
||||
}
|
||||
|
||||
/**
|
||||
* Enable the X11 forwarding.
|
||||
*
|
||||
* @param enable
|
||||
* @see RFC4254 6.3.1. Requesting X11 Forwarding
|
||||
*/
|
||||
public void setXForwarding(boolean enable){
|
||||
xforwading=enable;
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated Use {@link #setEnv(String, String)} or {@link #setEnv(byte[], byte[])} instead.
|
||||
* @see #setEnv(String, String)
|
||||
* @see #setEnv(byte[], byte[])
|
||||
*/
|
||||
public void setEnv(Hashtable env){
|
||||
synchronized(this){
|
||||
this.env=env;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the environment variable.
|
||||
* If <code>name</code> and <code>value</code> are needed to be passed
|
||||
* to the remote in your faivorite encoding,use
|
||||
* {@link #setEnv(byte[], byte[])}.
|
||||
*
|
||||
* @param name A name for environment variable.
|
||||
* @param value A value for environment variable.
|
||||
* @see RFC4254 6.4 Environment Variable Passing
|
||||
*/
|
||||
public void setEnv(String name, String value){
|
||||
setEnv(Util.str2byte(name), Util.str2byte(value));
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the environment variable.
|
||||
*
|
||||
* @param name A name of environment variable.
|
||||
* @param value A value of environment variable.
|
||||
* @see #setEnv(String, String)
|
||||
* @see RFC4254 6.4 Environment Variable Passing
|
||||
*/
|
||||
public void setEnv(byte[] name, byte[] value){
|
||||
synchronized(this){
|
||||
getEnv().put(name, value);
|
||||
}
|
||||
}
|
||||
|
||||
private Hashtable getEnv(){
|
||||
if(env==null)
|
||||
env=new Hashtable();
|
||||
return env;
|
||||
}
|
||||
|
||||
/**
|
||||
* Allocate a Pseudo-Terminal.
|
||||
*
|
||||
* @param enable
|
||||
* @see RFC4254 6.2. Requesting a Pseudo-Terminal
|
||||
*/
|
||||
public void setPty(boolean enable){
|
||||
pty=enable;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the terminal mode.
|
||||
*
|
||||
* @param terminal_mode
|
||||
*/
|
||||
public void setTerminalMode(byte[] terminal_mode){
|
||||
this.terminal_mode=terminal_mode;
|
||||
}
|
||||
|
||||
/**
|
||||
* Change the window dimension interactively.
|
||||
*
|
||||
* @param col terminal width, columns
|
||||
* @param row terminal height, rows
|
||||
* @param wp terminal width, pixels
|
||||
* @param hp terminal height, pixels
|
||||
* @see RFC4254 6.7. Window Dimension Change Message
|
||||
*/
|
||||
public void setPtySize(int col, int row, int wp, int hp){
|
||||
setPtyType(this.ttype, col, row, wp, hp);
|
||||
if(!pty || !isConnected()){
|
||||
return;
|
||||
}
|
||||
try{
|
||||
RequestWindowChange request=new RequestWindowChange();
|
||||
request.setSize(col, row, wp, hp);
|
||||
request.request(getSession(), this);
|
||||
}
|
||||
catch(Exception e){
|
||||
//System.err.println("ChannelSessio.setPtySize: "+e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the terminal type.
|
||||
* This method is not effective after Channel#connect().
|
||||
*
|
||||
* @param ttype terminal type(for example, "vt100")
|
||||
* @see #setPtyType(String, int, int, int, int)
|
||||
*/
|
||||
public void setPtyType(String ttype){
|
||||
setPtyType(ttype, 80, 24, 640, 480);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the terminal type.
|
||||
* This method is not effective after Channel#connect().
|
||||
*
|
||||
* @param ttype terminal type(for example, "vt100")
|
||||
* @param col terminal width, columns
|
||||
* @param row terminal height, rows
|
||||
* @param wp terminal width, pixels
|
||||
* @param hp terminal height, pixels
|
||||
*/
|
||||
public void setPtyType(String ttype, int col, int row, int wp, int hp){
|
||||
this.ttype=ttype;
|
||||
this.tcol=col;
|
||||
this.trow=row;
|
||||
this.twp=wp;
|
||||
this.thp=hp;
|
||||
}
|
||||
|
||||
protected void sendRequests() throws Exception{
|
||||
Session _session=getSession();
|
||||
Request request;
|
||||
if(agent_forwarding){
|
||||
request=new RequestAgentForwarding();
|
||||
request.request(_session, this);
|
||||
}
|
||||
|
||||
if(xforwading){
|
||||
request=new RequestX11();
|
||||
request.request(_session, this);
|
||||
}
|
||||
|
||||
if(pty){
|
||||
request=new RequestPtyReq();
|
||||
((RequestPtyReq)request).setTType(ttype);
|
||||
((RequestPtyReq)request).setTSize(tcol, trow, twp, thp);
|
||||
if(terminal_mode!=null){
|
||||
((RequestPtyReq)request).setTerminalMode(terminal_mode);
|
||||
}
|
||||
request.request(_session, this);
|
||||
}
|
||||
|
||||
if(env!=null){
|
||||
for(Enumeration _env=env.keys(); _env.hasMoreElements();){
|
||||
Object name=_env.nextElement();
|
||||
Object value=env.get(name);
|
||||
request=new RequestEnv();
|
||||
((RequestEnv)request).setEnv(toByteArray(name),
|
||||
toByteArray(value));
|
||||
request.request(_session, this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private byte[] toByteArray(Object o){
|
||||
if(o instanceof String){
|
||||
return Util.str2byte((String)o);
|
||||
}
|
||||
return (byte[])o;
|
||||
}
|
||||
|
||||
public void run(){
|
||||
//System.err.println(this+":run >");
|
||||
|
||||
Buffer buf=new Buffer(rmpsize);
|
||||
Packet packet=new Packet(buf);
|
||||
int i=-1;
|
||||
try{
|
||||
while(isConnected() &&
|
||||
thread!=null &&
|
||||
io!=null &&
|
||||
io.in!=null){
|
||||
i=io.in.read(buf.buffer,
|
||||
14,
|
||||
buf.buffer.length-14
|
||||
-Session.buffer_margin
|
||||
);
|
||||
if(i==0)continue;
|
||||
if(i==-1){
|
||||
eof();
|
||||
break;
|
||||
}
|
||||
if(close)break;
|
||||
//System.out.println("write: "+i);
|
||||
packet.reset();
|
||||
buf.putByte((byte)Session.SSH_MSG_CHANNEL_DATA);
|
||||
buf.putInt(recipient);
|
||||
buf.putInt(i);
|
||||
buf.skip(i);
|
||||
getSession().write(packet, this, i);
|
||||
}
|
||||
}
|
||||
catch(Exception e){
|
||||
//System.err.println("# ChannelExec.run");
|
||||
//e.printStackTrace();
|
||||
}
|
||||
Thread _thread=thread;
|
||||
if(_thread!=null){
|
||||
synchronized(_thread){ _thread.notifyAll(); }
|
||||
}
|
||||
thread=null;
|
||||
//System.err.println(this+":run <");
|
||||
}
|
||||
}
|
File diff suppressed because it is too large
Load Diff
@ -1,70 +0,0 @@
|
||||
/* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
|
||||
/*
|
||||
Copyright (c) 2002-2011 ymnk, JCraft,Inc. All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice,
|
||||
this list of conditions and the following disclaimer.
|
||||
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the distribution.
|
||||
|
||||
3. The names of the authors may not be used to endorse or promote products
|
||||
derived from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
|
||||
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
|
||||
INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
|
||||
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
||||
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
package com.jcraft.jsch;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class ChannelShell extends ChannelSession{
|
||||
|
||||
ChannelShell(){
|
||||
super();
|
||||
pty=true;
|
||||
}
|
||||
|
||||
public void start() throws JSchException{
|
||||
Session _session=getSession();
|
||||
try{
|
||||
sendRequests();
|
||||
|
||||
Request request=new RequestShell();
|
||||
request.request(_session, this);
|
||||
}
|
||||
catch(Exception e){
|
||||
if(e instanceof JSchException) throw (JSchException)e;
|
||||
if(e instanceof Throwable)
|
||||
throw new JSchException("ChannelShell", (Throwable)e);
|
||||
throw new JSchException("ChannelShell");
|
||||
}
|
||||
|
||||
if(io.in!=null){
|
||||
thread=new Thread(this);
|
||||
thread.setName("Shell for "+_session.host);
|
||||
if(_session.daemon_thread){
|
||||
thread.setDaemon(_session.daemon_thread);
|
||||
}
|
||||
thread.start();
|
||||
}
|
||||
}
|
||||
|
||||
void init() throws JSchException {
|
||||
io.setInputStream(getSession().in);
|
||||
io.setOutputStream(getSession().out);
|
||||
}
|
||||
}
|
@ -1,83 +0,0 @@
|
||||
/* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
|
||||
/*
|
||||
Copyright (c) 2005-2011 ymnk, JCraft,Inc. All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice,
|
||||
this list of conditions and the following disclaimer.
|
||||
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the distribution.
|
||||
|
||||
3. The names of the authors may not be used to endorse or promote products
|
||||
derived from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
|
||||
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
|
||||
INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
|
||||
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
||||
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
package com.jcraft.jsch;
|
||||
|
||||
public class ChannelSubsystem extends ChannelSession{
|
||||
boolean xforwading=false;
|
||||
boolean pty=false;
|
||||
boolean want_reply=true;
|
||||
String subsystem="";
|
||||
public void setXForwarding(boolean foo){ xforwading=foo; }
|
||||
public void setPty(boolean foo){ pty=foo; }
|
||||
public void setWantReply(boolean foo){ want_reply=foo; }
|
||||
public void setSubsystem(String foo){ subsystem=foo; }
|
||||
public void start() throws JSchException{
|
||||
Session _session=getSession();
|
||||
try{
|
||||
Request request;
|
||||
if(xforwading){
|
||||
request=new RequestX11();
|
||||
request.request(_session, this);
|
||||
}
|
||||
if(pty){
|
||||
request=new RequestPtyReq();
|
||||
request.request(_session, this);
|
||||
}
|
||||
request=new RequestSubsystem();
|
||||
((RequestSubsystem)request).request(_session, this, subsystem, want_reply);
|
||||
}
|
||||
catch(Exception e){
|
||||
if(e instanceof JSchException){ throw (JSchException)e; }
|
||||
if(e instanceof Throwable)
|
||||
throw new JSchException("ChannelSubsystem", (Throwable)e);
|
||||
throw new JSchException("ChannelSubsystem");
|
||||
}
|
||||
if(io.in!=null){
|
||||
thread=new Thread(this);
|
||||
thread.setName("Subsystem for "+_session.host);
|
||||
if(_session.daemon_thread){
|
||||
thread.setDaemon(_session.daemon_thread);
|
||||
}
|
||||
thread.start();
|
||||
}
|
||||
}
|
||||
|
||||
void init() throws JSchException {
|
||||
io.setInputStream(getSession().in);
|
||||
io.setOutputStream(getSession().out);
|
||||
}
|
||||
|
||||
public void setErrStream(java.io.OutputStream out){
|
||||
setExtOutputStream(out);
|
||||
}
|
||||
public java.io.InputStream getErrStream() throws java.io.IOException {
|
||||
return getExtInputStream();
|
||||
}
|
||||
}
|
@ -1,273 +0,0 @@
|
||||
/* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
|
||||
/*
|
||||
Copyright (c) 2002-2011 ymnk, JCraft,Inc. All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice,
|
||||
this list of conditions and the following disclaimer.
|
||||
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the distribution.
|
||||
|
||||
3. The names of the authors may not be used to endorse or promote products
|
||||
derived from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
|
||||
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
|
||||
INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
|
||||
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
||||
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
package com.jcraft.jsch;
|
||||
|
||||
import java.net.*;
|
||||
|
||||
class ChannelX11 extends Channel{
|
||||
|
||||
static private final int LOCAL_WINDOW_SIZE_MAX=0x20000;
|
||||
static private final int LOCAL_MAXIMUM_PACKET_SIZE=0x4000;
|
||||
|
||||
static private final int TIMEOUT=10*1000;
|
||||
|
||||
private static String host="127.0.0.1";
|
||||
private static int port=6000;
|
||||
|
||||
private boolean init=true;
|
||||
|
||||
static byte[] cookie=null;
|
||||
private static byte[] cookie_hex=null;
|
||||
|
||||
private static java.util.Hashtable faked_cookie_pool=new java.util.Hashtable();
|
||||
private static java.util.Hashtable faked_cookie_hex_pool=new java.util.Hashtable();
|
||||
|
||||
private static byte[] table={0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,
|
||||
0x61,0x62,0x63,0x64,0x65,0x66};
|
||||
|
||||
private Socket socket = null;
|
||||
|
||||
static int revtable(byte foo){
|
||||
for(int i=0; i<table.length; i++){
|
||||
if(table[i]==foo)return i;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
static void setCookie(String foo){
|
||||
cookie_hex=Util.str2byte(foo);
|
||||
cookie=new byte[16];
|
||||
for(int i=0; i<16; i++){
|
||||
cookie[i]=(byte)(((revtable(cookie_hex[i*2])<<4)&0xf0) |
|
||||
((revtable(cookie_hex[i*2+1]))&0xf));
|
||||
}
|
||||
}
|
||||
static void setHost(String foo){ host=foo; }
|
||||
static void setPort(int foo){ port=foo; }
|
||||
static byte[] getFakedCookie(Session session){
|
||||
synchronized(faked_cookie_hex_pool){
|
||||
byte[] foo=(byte[])faked_cookie_hex_pool.get(session);
|
||||
if(foo==null){
|
||||
Random random=Session.random;
|
||||
foo=new byte[16];
|
||||
synchronized(random){
|
||||
random.fill(foo, 0, 16);
|
||||
}
|
||||
/*
|
||||
System.err.print("faked_cookie: ");
|
||||
for(int i=0; i<foo.length; i++){
|
||||
System.err.print(Integer.toHexString(foo[i]&0xff)+":");
|
||||
}
|
||||
System.err.println("");
|
||||
*/
|
||||
faked_cookie_pool.put(session, foo);
|
||||
byte[] bar=new byte[32];
|
||||
for(int i=0; i<16; i++){
|
||||
bar[2*i]=table[(foo[i]>>>4)&0xf];
|
||||
bar[2*i+1]=table[(foo[i])&0xf];
|
||||
}
|
||||
faked_cookie_hex_pool.put(session, bar);
|
||||
foo=bar;
|
||||
}
|
||||
return foo;
|
||||
}
|
||||
}
|
||||
|
||||
static void removeFakedCookie(Session session){
|
||||
synchronized(faked_cookie_hex_pool){
|
||||
faked_cookie_hex_pool.remove(session);
|
||||
faked_cookie_pool.remove(session);
|
||||
}
|
||||
}
|
||||
|
||||
ChannelX11(){
|
||||
super();
|
||||
|
||||
setLocalWindowSizeMax(LOCAL_WINDOW_SIZE_MAX);
|
||||
setLocalWindowSize(LOCAL_WINDOW_SIZE_MAX);
|
||||
setLocalPacketSize(LOCAL_MAXIMUM_PACKET_SIZE);
|
||||
|
||||
type=Util.str2byte("x11");
|
||||
|
||||
connected=true;
|
||||
/*
|
||||
try{
|
||||
socket=Util.createSocket(host, port, TIMEOUT);
|
||||
socket.setTcpNoDelay(true);
|
||||
io=new IO();
|
||||
io.setInputStream(socket.getInputStream());
|
||||
io.setOutputStream(socket.getOutputStream());
|
||||
}
|
||||
catch(Exception e){
|
||||
//System.err.println(e);
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
public void run(){
|
||||
|
||||
try{
|
||||
socket=Util.createSocket(host, port, TIMEOUT);
|
||||
socket.setTcpNoDelay(true);
|
||||
io=new IO();
|
||||
io.setInputStream(socket.getInputStream());
|
||||
io.setOutputStream(socket.getOutputStream());
|
||||
sendOpenConfirmation();
|
||||
}
|
||||
catch(Exception e){
|
||||
sendOpenFailure(SSH_OPEN_ADMINISTRATIVELY_PROHIBITED);
|
||||
close=true;
|
||||
disconnect();
|
||||
return;
|
||||
}
|
||||
|
||||
thread=Thread.currentThread();
|
||||
Buffer buf=new Buffer(rmpsize);
|
||||
Packet packet=new Packet(buf);
|
||||
int i=0;
|
||||
try{
|
||||
while(thread!=null &&
|
||||
io!=null &&
|
||||
io.in!=null){
|
||||
i=io.in.read(buf.buffer,
|
||||
14,
|
||||
buf.buffer.length-14-Session.buffer_margin);
|
||||
if(i<=0){
|
||||
eof();
|
||||
break;
|
||||
}
|
||||
if(close)break;
|
||||
packet.reset();
|
||||
buf.putByte((byte)Session.SSH_MSG_CHANNEL_DATA);
|
||||
buf.putInt(recipient);
|
||||
buf.putInt(i);
|
||||
buf.skip(i);
|
||||
getSession().write(packet, this, i);
|
||||
}
|
||||
}
|
||||
catch(Exception e){
|
||||
//System.err.println(e);
|
||||
}
|
||||
disconnect();
|
||||
}
|
||||
|
||||
private byte[] cache=new byte[0];
|
||||
private byte[] addCache(byte[] foo, int s, int l){
|
||||
byte[] bar=new byte[cache.length+l];
|
||||
System.arraycopy(foo, s, bar, cache.length, l);
|
||||
if(cache.length>0)
|
||||
System.arraycopy(cache, 0, bar, 0, cache.length);
|
||||
cache=bar;
|
||||
return cache;
|
||||
}
|
||||
|
||||
void write(byte[] foo, int s, int l) throws java.io.IOException {
|
||||
//if(eof_local)return;
|
||||
|
||||
if(init){
|
||||
|
||||
Session _session=null;
|
||||
try{
|
||||
_session=getSession();
|
||||
}
|
||||
catch(JSchException e){
|
||||
throw new java.io.IOException(e.toString());
|
||||
}
|
||||
|
||||
foo=addCache(foo, s, l);
|
||||
s=0;
|
||||
l=foo.length;
|
||||
|
||||
if(l<9)
|
||||
return;
|
||||
|
||||
int plen=(foo[s+6]&0xff)*256+(foo[s+7]&0xff);
|
||||
int dlen=(foo[s+8]&0xff)*256+(foo[s+9]&0xff);
|
||||
|
||||
if((foo[s]&0xff)==0x42){
|
||||
}
|
||||
else if((foo[s]&0xff)==0x6c){
|
||||
plen=((plen>>>8)&0xff)|((plen<<8)&0xff00);
|
||||
dlen=((dlen>>>8)&0xff)|((dlen<<8)&0xff00);
|
||||
}
|
||||
else{
|
||||
// ??
|
||||
}
|
||||
|
||||
if(l<12+plen+((-plen)&3)+dlen)
|
||||
return;
|
||||
|
||||
byte[] bar=new byte[dlen];
|
||||
System.arraycopy(foo, s+12+plen+((-plen)&3), bar, 0, dlen);
|
||||
byte[] faked_cookie=null;
|
||||
|
||||
synchronized(faked_cookie_pool){
|
||||
faked_cookie=(byte[])faked_cookie_pool.get(_session);
|
||||
}
|
||||
|
||||
/*
|
||||
System.err.print("faked_cookie: ");
|
||||
for(int i=0; i<faked_cookie.length; i++){
|
||||
System.err.print(Integer.toHexString(faked_cookie[i]&0xff)+":");
|
||||
}
|
||||
System.err.println("");
|
||||
System.err.print("bar: ");
|
||||
for(int i=0; i<bar.length; i++){
|
||||
System.err.print(Integer.toHexString(bar[i]&0xff)+":");
|
||||
}
|
||||
System.err.println("");
|
||||
*/
|
||||
|
||||
if(equals(bar, faked_cookie)){
|
||||
if(cookie!=null)
|
||||
System.arraycopy(cookie, 0, foo, s+12+plen+((-plen)&3), dlen);
|
||||
}
|
||||
else{
|
||||
//System.err.println("wrong cookie");
|
||||
thread=null;
|
||||
eof();
|
||||
io.close();
|
||||
disconnect();
|
||||
}
|
||||
init=false;
|
||||
io.put(foo, s, l);
|
||||
cache=null;
|
||||
return;
|
||||
}
|
||||
io.put(foo, s, l);
|
||||
}
|
||||
|
||||
private static boolean equals(byte[] foo, byte[] bar){
|
||||
if(foo.length!=bar.length)return false;
|
||||
for(int i=0; i<foo.length; i++){
|
||||
if(foo[i]!=bar[i])return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
@ -1,40 +0,0 @@
|
||||
/* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
|
||||
/*
|
||||
Copyright (c) 2002-2011 ymnk, JCraft,Inc. All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice,
|
||||
this list of conditions and the following disclaimer.
|
||||
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the distribution.
|
||||
|
||||
3. The names of the authors may not be used to endorse or promote products
|
||||
derived from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
|
||||
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
|
||||
INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
|
||||
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
||||
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
package com.jcraft.jsch;
|
||||
|
||||
public interface Cipher{
|
||||
static int ENCRYPT_MODE=0;
|
||||
static int DECRYPT_MODE=1;
|
||||
int getIVSize();
|
||||
int getBlockSize();
|
||||
void init(int mode, byte[] key, byte[] iv) throws Exception;
|
||||
void update(byte[] foo, int s1, int len, byte[] bar, int s2) throws Exception;
|
||||
boolean isCBC();
|
||||
}
|
@ -1,42 +0,0 @@
|
||||
/* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
|
||||
/*
|
||||
Copyright (c) 2002-2011 ymnk, JCraft,Inc. All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice,
|
||||
this list of conditions and the following disclaimer.
|
||||
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the distribution.
|
||||
|
||||
3. The names of the authors may not be used to endorse or promote products
|
||||
derived from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
|
||||
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
|
||||
INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
|
||||
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
||||
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
package com.jcraft.jsch;
|
||||
|
||||
public class CipherNone implements Cipher{
|
||||
private static final int ivsize=8;
|
||||
private static final int bsize=16;
|
||||
public int getIVSize(){return ivsize;}
|
||||
public int getBlockSize(){return bsize;}
|
||||
public void init(int mode, byte[] key, byte[] iv) throws Exception{
|
||||
}
|
||||
public void update(byte[] foo, int s1, int len, byte[] bar, int s2) throws Exception{
|
||||
}
|
||||
public boolean isCBC(){return false; }
|
||||
}
|
@ -1,38 +0,0 @@
|
||||
/* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
|
||||
/*
|
||||
Copyright (c) 2002-2011 ymnk, JCraft,Inc. All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice,
|
||||
this list of conditions and the following disclaimer.
|
||||
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the distribution.
|
||||
|
||||
3. The names of the authors may not be used to endorse or promote products
|
||||
derived from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
|
||||
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
|
||||
INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
|
||||
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
||||
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
package com.jcraft.jsch;
|
||||
|
||||
public interface Compression{
|
||||
static public final int INFLATER=0;
|
||||
static public final int DEFLATER=1;
|
||||
void init(int type, int level);
|
||||
byte[] compress(byte[] buf, int start, int[] len);
|
||||
byte[] uncompress(byte[] buf, int start, int[] len);
|
||||
}
|
@ -1,39 +0,0 @@
|
||||
/* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
|
||||
/*
|
||||
Copyright (c) 2002-2011 ymnk, JCraft,Inc. All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice,
|
||||
this list of conditions and the following disclaimer.
|
||||
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the distribution.
|
||||
|
||||
3. The names of the authors may not be used to endorse or promote products
|
||||
derived from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
|
||||
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
|
||||
INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
|
||||
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
||||
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
package com.jcraft.jsch;
|
||||
|
||||
public interface DH{
|
||||
void init() throws Exception;
|
||||
void setP(byte[] p);
|
||||
void setG(byte[] g);
|
||||
byte[] getE() throws Exception;
|
||||
void setF(byte[] f);
|
||||
byte[] getK() throws Exception;
|
||||
}
|
@ -1,310 +0,0 @@
|
||||
/* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
|
||||
/*
|
||||
Copyright (c) 2002-2011 ymnk, JCraft,Inc. All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice,
|
||||
this list of conditions and the following disclaimer.
|
||||
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the distribution.
|
||||
|
||||
3. The names of the authors may not be used to endorse or promote products
|
||||
derived from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
|
||||
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
|
||||
INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
|
||||
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
||||
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
package com.jcraft.jsch;
|
||||
|
||||
public class DHG1 extends KeyExchange{
|
||||
|
||||
static final byte[] g={ 2 };
|
||||
static final byte[] p={
|
||||
(byte)0x00,
|
||||
(byte)0xFF,(byte)0xFF,(byte)0xFF,(byte)0xFF,(byte)0xFF,(byte)0xFF,(byte)0xFF,(byte)0xFF,
|
||||
(byte)0xC9,(byte)0x0F,(byte)0xDA,(byte)0xA2,(byte)0x21,(byte)0x68,(byte)0xC2,(byte)0x34,
|
||||
(byte)0xC4,(byte)0xC6,(byte)0x62,(byte)0x8B,(byte)0x80,(byte)0xDC,(byte)0x1C,(byte)0xD1,
|
||||
(byte)0x29,(byte)0x02,(byte)0x4E,(byte)0x08,(byte)0x8A,(byte)0x67,(byte)0xCC,(byte)0x74,
|
||||
(byte)0x02,(byte)0x0B,(byte)0xBE,(byte)0xA6,(byte)0x3B,(byte)0x13,(byte)0x9B,(byte)0x22,
|
||||
(byte)0x51,(byte)0x4A,(byte)0x08,(byte)0x79,(byte)0x8E,(byte)0x34,(byte)0x04,(byte)0xDD,
|
||||
(byte)0xEF,(byte)0x95,(byte)0x19,(byte)0xB3,(byte)0xCD,(byte)0x3A,(byte)0x43,(byte)0x1B,
|
||||
(byte)0x30,(byte)0x2B,(byte)0x0A,(byte)0x6D,(byte)0xF2,(byte)0x5F,(byte)0x14,(byte)0x37,
|
||||
(byte)0x4F,(byte)0xE1,(byte)0x35,(byte)0x6D,(byte)0x6D,(byte)0x51,(byte)0xC2,(byte)0x45,
|
||||
(byte)0xE4,(byte)0x85,(byte)0xB5,(byte)0x76,(byte)0x62,(byte)0x5E,(byte)0x7E,(byte)0xC6,
|
||||
(byte)0xF4,(byte)0x4C,(byte)0x42,(byte)0xE9,(byte)0xA6,(byte)0x37,(byte)0xED,(byte)0x6B,
|
||||
(byte)0x0B,(byte)0xFF,(byte)0x5C,(byte)0xB6,(byte)0xF4,(byte)0x06,(byte)0xB7,(byte)0xED,
|
||||
(byte)0xEE,(byte)0x38,(byte)0x6B,(byte)0xFB,(byte)0x5A,(byte)0x89,(byte)0x9F,(byte)0xA5,
|
||||
(byte)0xAE,(byte)0x9F,(byte)0x24,(byte)0x11,(byte)0x7C,(byte)0x4B,(byte)0x1F,(byte)0xE6,
|
||||
(byte)0x49,(byte)0x28,(byte)0x66,(byte)0x51,(byte)0xEC,(byte)0xE6,(byte)0x53,(byte)0x81,
|
||||
(byte)0xFF,(byte)0xFF,(byte)0xFF,(byte)0xFF,(byte)0xFF,(byte)0xFF,(byte)0xFF,(byte)0xFF
|
||||
};
|
||||
|
||||
private static final int SSH_MSG_KEXDH_INIT= 30;
|
||||
private static final int SSH_MSG_KEXDH_REPLY= 31;
|
||||
|
||||
static final int RSA=0;
|
||||
static final int DSS=1;
|
||||
private int type=0;
|
||||
|
||||
private int state;
|
||||
|
||||
DH dh;
|
||||
// HASH sha;
|
||||
|
||||
// byte[] K;
|
||||
// byte[] H;
|
||||
|
||||
byte[] V_S;
|
||||
byte[] V_C;
|
||||
byte[] I_S;
|
||||
byte[] I_C;
|
||||
|
||||
// byte[] K_S;
|
||||
|
||||
byte[] e;
|
||||
|
||||
private Buffer buf;
|
||||
private Packet packet;
|
||||
|
||||
public void init(Session session,
|
||||
byte[] V_S, byte[] V_C, byte[] I_S, byte[] I_C) throws Exception{
|
||||
this.session=session;
|
||||
this.V_S=V_S;
|
||||
this.V_C=V_C;
|
||||
this.I_S=I_S;
|
||||
this.I_C=I_C;
|
||||
|
||||
// sha=new SHA1();
|
||||
// sha.init();
|
||||
try{
|
||||
Class c=Class.forName(session.getConfig("sha-1"));
|
||||
sha=(HASH)(c.newInstance());
|
||||
sha.init();
|
||||
}
|
||||
catch(Exception e){
|
||||
System.err.println(e);
|
||||
}
|
||||
|
||||
buf=new Buffer();
|
||||
packet=new Packet(buf);
|
||||
|
||||
try{
|
||||
Class c=Class.forName(session.getConfig("dh"));
|
||||
dh=(DH)(c.newInstance());
|
||||
dh.init();
|
||||
}
|
||||
catch(Exception e){
|
||||
//System.err.println(e);
|
||||
throw e;
|
||||
}
|
||||
|
||||
dh.setP(p);
|
||||
dh.setG(g);
|
||||
|
||||
// The client responds with:
|
||||
// byte SSH_MSG_KEXDH_INIT(30)
|
||||
// mpint e <- g^x mod p
|
||||
// x is a random number (1 < x < (p-1)/2)
|
||||
|
||||
e=dh.getE();
|
||||
|
||||
packet.reset();
|
||||
buf.putByte((byte)SSH_MSG_KEXDH_INIT);
|
||||
buf.putMPInt(e);
|
||||
session.write(packet);
|
||||
|
||||
if(JSch.getLogger().isEnabled(Logger.INFO)){
|
||||
JSch.getLogger().log(Logger.INFO,
|
||||
"SSH_MSG_KEXDH_INIT sent");
|
||||
JSch.getLogger().log(Logger.INFO,
|
||||
"expecting SSH_MSG_KEXDH_REPLY");
|
||||
}
|
||||
|
||||
state=SSH_MSG_KEXDH_REPLY;
|
||||
}
|
||||
|
||||
public boolean next(Buffer _buf) throws Exception{
|
||||
int i,j;
|
||||
|
||||
switch(state){
|
||||
case SSH_MSG_KEXDH_REPLY:
|
||||
// The server responds with:
|
||||
// byte SSH_MSG_KEXDH_REPLY(31)
|
||||
// string server public host key and certificates (K_S)
|
||||
// mpint f
|
||||
// string signature of H
|
||||
j=_buf.getInt();
|
||||
j=_buf.getByte();
|
||||
j=_buf.getByte();
|
||||
if(j!=31){
|
||||
System.err.println("type: must be 31 "+j);
|
||||
return false;
|
||||
}
|
||||
|
||||
K_S=_buf.getString();
|
||||
// K_S is server_key_blob, which includes ....
|
||||
// string ssh-dss
|
||||
// impint p of dsa
|
||||
// impint q of dsa
|
||||
// impint g of dsa
|
||||
// impint pub_key of dsa
|
||||
//System.err.print("K_S: "); //dump(K_S, 0, K_S.length);
|
||||
byte[] f=_buf.getMPInt();
|
||||
byte[] sig_of_H=_buf.getString();
|
||||
/*
|
||||
for(int ii=0; ii<sig_of_H.length;ii++){
|
||||
System.err.print(Integer.toHexString(sig_of_H[ii]&0xff));
|
||||
System.err.print(": ");
|
||||
}
|
||||
System.err.println("");
|
||||
*/
|
||||
|
||||
dh.setF(f);
|
||||
K=dh.getK();
|
||||
|
||||
//The hash H is computed as the HASH hash of the concatenation of the
|
||||
//following:
|
||||
// string V_C, the client's version string (CR and NL excluded)
|
||||
// string V_S, the server's version string (CR and NL excluded)
|
||||
// string I_C, the payload of the client's SSH_MSG_KEXINIT
|
||||
// string I_S, the payload of the server's SSH_MSG_KEXINIT
|
||||
// string K_S, the host key
|
||||
// mpint e, exchange value sent by the client
|
||||
// mpint f, exchange value sent by the server
|
||||
// mpint K, the shared secret
|
||||
// This value is called the exchange hash, and it is used to authenti-
|
||||
// cate the key exchange.
|
||||
buf.reset();
|
||||
buf.putString(V_C); buf.putString(V_S);
|
||||
buf.putString(I_C); buf.putString(I_S);
|
||||
buf.putString(K_S);
|
||||
buf.putMPInt(e); buf.putMPInt(f);
|
||||
buf.putMPInt(K);
|
||||
byte[] foo=new byte[buf.getLength()];
|
||||
buf.getByte(foo);
|
||||
sha.update(foo, 0, foo.length);
|
||||
H=sha.digest();
|
||||
//System.err.print("H -> "); //dump(H, 0, H.length);
|
||||
|
||||
i=0;
|
||||
j=0;
|
||||
j=((K_S[i++]<<24)&0xff000000)|((K_S[i++]<<16)&0x00ff0000)|
|
||||
((K_S[i++]<<8)&0x0000ff00)|((K_S[i++])&0x000000ff);
|
||||
String alg=Util.byte2str(K_S, i, j);
|
||||
i+=j;
|
||||
|
||||
boolean result=false;
|
||||
|
||||
if(alg.equals("ssh-rsa")){
|
||||
byte[] tmp;
|
||||
byte[] ee;
|
||||
byte[] n;
|
||||
|
||||
type=RSA;
|
||||
|
||||
j=((K_S[i++]<<24)&0xff000000)|((K_S[i++]<<16)&0x00ff0000)|
|
||||
((K_S[i++]<<8)&0x0000ff00)|((K_S[i++])&0x000000ff);
|
||||
tmp=new byte[j]; System.arraycopy(K_S, i, tmp, 0, j); i+=j;
|
||||
ee=tmp;
|
||||
j=((K_S[i++]<<24)&0xff000000)|((K_S[i++]<<16)&0x00ff0000)|
|
||||
((K_S[i++]<<8)&0x0000ff00)|((K_S[i++])&0x000000ff);
|
||||
tmp=new byte[j]; System.arraycopy(K_S, i, tmp, 0, j); i+=j;
|
||||
n=tmp;
|
||||
|
||||
// SignatureRSA sig=new SignatureRSA();
|
||||
// sig.init();
|
||||
|
||||
SignatureRSA sig=null;
|
||||
try{
|
||||
Class c=Class.forName(session.getConfig("signature.rsa"));
|
||||
sig=(SignatureRSA)(c.newInstance());
|
||||
sig.init();
|
||||
}
|
||||
catch(Exception e){
|
||||
System.err.println(e);
|
||||
}
|
||||
|
||||
sig.setPubKey(ee, n);
|
||||
sig.update(H);
|
||||
result=sig.verify(sig_of_H);
|
||||
|
||||
if(JSch.getLogger().isEnabled(Logger.INFO)){
|
||||
JSch.getLogger().log(Logger.INFO,
|
||||
"ssh_rsa_verify: signature "+result);
|
||||
}
|
||||
|
||||
}
|
||||
else if(alg.equals("ssh-dss")){
|
||||
byte[] q=null;
|
||||
byte[] tmp;
|
||||
byte[] p;
|
||||
byte[] g;
|
||||
|
||||
type=DSS;
|
||||
|
||||
j=((K_S[i++]<<24)&0xff000000)|((K_S[i++]<<16)&0x00ff0000)|
|
||||
((K_S[i++]<<8)&0x0000ff00)|((K_S[i++])&0x000000ff);
|
||||
tmp=new byte[j]; System.arraycopy(K_S, i, tmp, 0, j); i+=j;
|
||||
p=tmp;
|
||||
j=((K_S[i++]<<24)&0xff000000)|((K_S[i++]<<16)&0x00ff0000)|
|
||||
((K_S[i++]<<8)&0x0000ff00)|((K_S[i++])&0x000000ff);
|
||||
tmp=new byte[j]; System.arraycopy(K_S, i, tmp, 0, j); i+=j;
|
||||
q=tmp;
|
||||
j=((K_S[i++]<<24)&0xff000000)|((K_S[i++]<<16)&0x00ff0000)|
|
||||
((K_S[i++]<<8)&0x0000ff00)|((K_S[i++])&0x000000ff);
|
||||
tmp=new byte[j]; System.arraycopy(K_S, i, tmp, 0, j); i+=j;
|
||||
g=tmp;
|
||||
j=((K_S[i++]<<24)&0xff000000)|((K_S[i++]<<16)&0x00ff0000)|
|
||||
((K_S[i++]<<8)&0x0000ff00)|((K_S[i++])&0x000000ff);
|
||||
tmp=new byte[j]; System.arraycopy(K_S, i, tmp, 0, j); i+=j;
|
||||
f=tmp;
|
||||
// SignatureDSA sig=new SignatureDSA();
|
||||
// sig.init();
|
||||
SignatureDSA sig=null;
|
||||
try{
|
||||
Class c=Class.forName(session.getConfig("signature.dss"));
|
||||
sig=(SignatureDSA)(c.newInstance());
|
||||
sig.init();
|
||||
}
|
||||
catch(Exception e){
|
||||
System.err.println(e);
|
||||
}
|
||||
sig.setPubKey(f, p, q, g);
|
||||
sig.update(H);
|
||||
result=sig.verify(sig_of_H);
|
||||
|
||||
if(JSch.getLogger().isEnabled(Logger.INFO)){
|
||||
JSch.getLogger().log(Logger.INFO,
|
||||
"ssh_dss_verify: signature "+result);
|
||||
}
|
||||
|
||||
}
|
||||
else{
|
||||
System.err.println("unknown alg");
|
||||
}
|
||||
state=STATE_END;
|
||||
return result;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public String getKeyType(){
|
||||
if(type==DSS) return "DSA";
|
||||
return "RSA";
|
||||
}
|
||||
|
||||
public int getState(){return state; }
|
||||
}
|
@ -1,310 +0,0 @@
|
||||
/* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
|
||||
/*
|
||||
Copyright (c) 2002-2011 ymnk, JCraft,Inc. All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice,
|
||||
this list of conditions and the following disclaimer.
|
||||
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the distribution.
|
||||
|
||||
3. The names of the authors may not be used to endorse or promote products
|
||||
derived from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
|
||||
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
|
||||
INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
|
||||
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
||||
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
package com.jcraft.jsch;
|
||||
|
||||
public class DHG14 extends KeyExchange{
|
||||
|
||||
static final byte[] g={ 2 };
|
||||
static final byte[] p={
|
||||
(byte)0x00,
|
||||
(byte)0xFF,(byte)0xFF,(byte)0xFF,(byte)0xFF,(byte)0xFF,(byte)0xFF,(byte)0xFF,(byte)0xFF,
|
||||
(byte)0xC9,(byte)0x0F,(byte)0xDA,(byte)0xA2,(byte)0x21,(byte)0x68,(byte)0xC2,(byte)0x34,
|
||||
(byte)0xC4,(byte)0xC6,(byte)0x62,(byte)0x8B,(byte)0x80,(byte)0xDC,(byte)0x1C,(byte)0xD1,
|
||||
(byte)0x29,(byte)0x02,(byte)0x4E,(byte)0x08,(byte)0x8A,(byte)0x67,(byte)0xCC,(byte)0x74,
|
||||
(byte)0x02,(byte)0x0B,(byte)0xBE,(byte)0xA6,(byte)0x3B,(byte)0x13,(byte)0x9B,(byte)0x22,
|
||||
(byte)0x51,(byte)0x4A,(byte)0x08,(byte)0x79,(byte)0x8E,(byte)0x34,(byte)0x04,(byte)0xDD,
|
||||
(byte)0xEF,(byte)0x95,(byte)0x19,(byte)0xB3,(byte)0xCD,(byte)0x3A,(byte)0x43,(byte)0x1B,
|
||||
(byte)0x30,(byte)0x2B,(byte)0x0A,(byte)0x6D,(byte)0xF2,(byte)0x5F,(byte)0x14,(byte)0x37,
|
||||
(byte)0x4F,(byte)0xE1,(byte)0x35,(byte)0x6D,(byte)0x6D,(byte)0x51,(byte)0xC2,(byte)0x45,
|
||||
(byte)0xE4,(byte)0x85,(byte)0xB5,(byte)0x76,(byte)0x62,(byte)0x5E,(byte)0x7E,(byte)0xC6,
|
||||
(byte)0xF4,(byte)0x4C,(byte)0x42,(byte)0xE9,(byte)0xA6,(byte)0x37,(byte)0xED,(byte)0x6B,
|
||||
(byte)0x0B,(byte)0xFF,(byte)0x5C,(byte)0xB6,(byte)0xF4,(byte)0x06,(byte)0xB7,(byte)0xED,
|
||||
(byte)0xEE,(byte)0x38,(byte)0x6B,(byte)0xFB,(byte)0x5A,(byte)0x89,(byte)0x9F,(byte)0xA5,
|
||||
(byte)0xAE,(byte)0x9F,(byte)0x24,(byte)0x11,(byte)0x7C,(byte)0x4B,(byte)0x1F,(byte)0xE6,
|
||||
(byte)0x49,(byte)0x28,(byte)0x66,(byte)0x51,(byte)0xEC,(byte)0xE4,(byte)0x5B,(byte)0x3D,
|
||||
(byte)0xC2,(byte)0x00,(byte)0x7C,(byte)0xB8,(byte)0xA1,(byte)0x63,(byte)0xBF,(byte)0x05,
|
||||
(byte)0x98,(byte)0xDA,(byte)0x48,(byte)0x36,(byte)0x1C,(byte)0x55,(byte)0xD3,(byte)0x9A,
|
||||
(byte)0x69,(byte)0x16,(byte)0x3F,(byte)0xA8,(byte)0xFD,(byte)0x24,(byte)0xCF,(byte)0x5F,
|
||||
(byte)0x83,(byte)0x65,(byte)0x5D,(byte)0x23,(byte)0xDC,(byte)0xA3,(byte)0xAD,(byte)0x96,
|
||||
(byte)0x1C,(byte)0x62,(byte)0xF3,(byte)0x56,(byte)0x20,(byte)0x85,(byte)0x52,(byte)0xBB,
|
||||
(byte)0x9E,(byte)0xD5,(byte)0x29,(byte)0x07,(byte)0x70,(byte)0x96,(byte)0x96,(byte)0x6D,
|
||||
(byte)0x67,(byte)0x0C,(byte)0x35,(byte)0x4E,(byte)0x4A,(byte)0xBC,(byte)0x98,(byte)0x04,
|
||||
(byte)0xF1,(byte)0x74,(byte)0x6C,(byte)0x08,(byte)0xCA,(byte)0x18,(byte)0x21,(byte)0x7C,
|
||||
(byte)0x32,(byte)0x90,(byte)0x5E,(byte)0x46,(byte)0x2E,(byte)0x36,(byte)0xCE,(byte)0x3B,
|
||||
(byte)0xE3,(byte)0x9E,(byte)0x77,(byte)0x2C,(byte)0x18,(byte)0x0E,(byte)0x86,(byte)0x03,
|
||||
(byte)0x9B,(byte)0x27,(byte)0x83,(byte)0xA2,(byte)0xEC,(byte)0x07,(byte)0xA2,(byte)0x8F,
|
||||
(byte)0xB5,(byte)0xC5,(byte)0x5D,(byte)0xF0,(byte)0x6F,(byte)0x4C,(byte)0x52,(byte)0xC9,
|
||||
(byte)0xDE,(byte)0x2B,(byte)0xCB,(byte)0xF6,(byte)0x95,(byte)0x58,(byte)0x17,(byte)0x18,
|
||||
(byte)0x39,(byte)0x95,(byte)0x49,(byte)0x7C,(byte)0xEA,(byte)0x95,(byte)0x6A,(byte)0xE5,
|
||||
(byte)0x15,(byte)0xD2,(byte)0x26,(byte)0x18,(byte)0x98,(byte)0xFA,(byte)0x05,(byte)0x10,
|
||||
(byte)0x15,(byte)0x72,(byte)0x8E,(byte)0x5A,(byte)0x8A,(byte)0xAC,(byte)0xAA,(byte)0x68,
|
||||
(byte)0xFF,(byte)0xFF,(byte)0xFF,(byte)0xFF,(byte)0xFF,(byte)0xFF,(byte)0xFF,(byte)0xFF
|
||||
};
|
||||
|
||||
private static final int SSH_MSG_KEXDH_INIT= 30;
|
||||
private static final int SSH_MSG_KEXDH_REPLY= 31;
|
||||
|
||||
static final int RSA=0;
|
||||
static final int DSS=1;
|
||||
private int type=0;
|
||||
|
||||
private int state;
|
||||
|
||||
DH dh;
|
||||
|
||||
byte[] V_S;
|
||||
byte[] V_C;
|
||||
byte[] I_S;
|
||||
byte[] I_C;
|
||||
|
||||
byte[] e;
|
||||
|
||||
private Buffer buf;
|
||||
private Packet packet;
|
||||
|
||||
public void init(Session session,
|
||||
byte[] V_S, byte[] V_C, byte[] I_S, byte[] I_C) throws Exception{
|
||||
this.session=session;
|
||||
this.V_S=V_S;
|
||||
this.V_C=V_C;
|
||||
this.I_S=I_S;
|
||||
this.I_C=I_C;
|
||||
|
||||
try{
|
||||
Class c=Class.forName(session.getConfig("sha-1"));
|
||||
sha=(HASH)(c.newInstance());
|
||||
sha.init();
|
||||
}
|
||||
catch(Exception e){
|
||||
System.err.println(e);
|
||||
}
|
||||
|
||||
buf=new Buffer();
|
||||
packet=new Packet(buf);
|
||||
|
||||
try{
|
||||
Class c=Class.forName(session.getConfig("dh"));
|
||||
dh=(DH)(c.newInstance());
|
||||
dh.init();
|
||||
}
|
||||
catch(Exception e){
|
||||
//System.err.println(e);
|
||||
throw e;
|
||||
}
|
||||
|
||||
dh.setP(p);
|
||||
dh.setG(g);
|
||||
// The client responds with:
|
||||
// byte SSH_MSG_KEXDH_INIT(30)
|
||||
// mpint e <- g^x mod p
|
||||
// x is a random number (1 < x < (p-1)/2)
|
||||
|
||||
e=dh.getE();
|
||||
packet.reset();
|
||||
buf.putByte((byte)SSH_MSG_KEXDH_INIT);
|
||||
buf.putMPInt(e);
|
||||
|
||||
if(V_S==null){ // This is a really ugly hack for Session.checkKexes ;-(
|
||||
return;
|
||||
}
|
||||
|
||||
session.write(packet);
|
||||
|
||||
if(JSch.getLogger().isEnabled(Logger.INFO)){
|
||||
JSch.getLogger().log(Logger.INFO,
|
||||
"SSH_MSG_KEXDH_INIT sent");
|
||||
JSch.getLogger().log(Logger.INFO,
|
||||
"expecting SSH_MSG_KEXDH_REPLY");
|
||||
}
|
||||
|
||||
state=SSH_MSG_KEXDH_REPLY;
|
||||
}
|
||||
|
||||
public boolean next(Buffer _buf) throws Exception{
|
||||
int i,j;
|
||||
|
||||
switch(state){
|
||||
case SSH_MSG_KEXDH_REPLY:
|
||||
// The server responds with:
|
||||
// byte SSH_MSG_KEXDH_REPLY(31)
|
||||
// string server public host key and certificates (K_S)
|
||||
// mpint f
|
||||
// string signature of H
|
||||
j=_buf.getInt();
|
||||
j=_buf.getByte();
|
||||
j=_buf.getByte();
|
||||
if(j!=31){
|
||||
System.err.println("type: must be 31 "+j);
|
||||
return false;
|
||||
}
|
||||
|
||||
K_S=_buf.getString();
|
||||
// K_S is server_key_blob, which includes ....
|
||||
// string ssh-dss
|
||||
// impint p of dsa
|
||||
// impint q of dsa
|
||||
// impint g of dsa
|
||||
// impint pub_key of dsa
|
||||
//System.err.print("K_S: "); //dump(K_S, 0, K_S.length);
|
||||
byte[] f=_buf.getMPInt();
|
||||
byte[] sig_of_H=_buf.getString();
|
||||
|
||||
dh.setF(f);
|
||||
K=dh.getK();
|
||||
|
||||
//The hash H is computed as the HASH hash of the concatenation of the
|
||||
//following:
|
||||
// string V_C, the client's version string (CR and NL excluded)
|
||||
// string V_S, the server's version string (CR and NL excluded)
|
||||
// string I_C, the payload of the client's SSH_MSG_KEXINIT
|
||||
// string I_S, the payload of the server's SSH_MSG_KEXINIT
|
||||
// string K_S, the host key
|
||||
// mpint e, exchange value sent by the client
|
||||
// mpint f, exchange value sent by the server
|
||||
// mpint K, the shared secret
|
||||
// This value is called the exchange hash, and it is used to authenti-
|
||||
// cate the key exchange.
|
||||
buf.reset();
|
||||
buf.putString(V_C); buf.putString(V_S);
|
||||
buf.putString(I_C); buf.putString(I_S);
|
||||
buf.putString(K_S);
|
||||
buf.putMPInt(e); buf.putMPInt(f);
|
||||
buf.putMPInt(K);
|
||||
byte[] foo=new byte[buf.getLength()];
|
||||
buf.getByte(foo);
|
||||
sha.update(foo, 0, foo.length);
|
||||
H=sha.digest();
|
||||
//System.err.print("H -> "); //dump(H, 0, H.length);
|
||||
|
||||
i=0;
|
||||
j=0;
|
||||
j=((K_S[i++]<<24)&0xff000000)|((K_S[i++]<<16)&0x00ff0000)|
|
||||
((K_S[i++]<<8)&0x0000ff00)|((K_S[i++])&0x000000ff);
|
||||
String alg=Util.byte2str(K_S, i, j);
|
||||
i+=j;
|
||||
|
||||
boolean result=false;
|
||||
|
||||
if(alg.equals("ssh-rsa")){
|
||||
byte[] tmp;
|
||||
byte[] ee;
|
||||
byte[] n;
|
||||
|
||||
type=RSA;
|
||||
|
||||
j=((K_S[i++]<<24)&0xff000000)|((K_S[i++]<<16)&0x00ff0000)|
|
||||
((K_S[i++]<<8)&0x0000ff00)|((K_S[i++])&0x000000ff);
|
||||
tmp=new byte[j]; System.arraycopy(K_S, i, tmp, 0, j); i+=j;
|
||||
ee=tmp;
|
||||
j=((K_S[i++]<<24)&0xff000000)|((K_S[i++]<<16)&0x00ff0000)|
|
||||
((K_S[i++]<<8)&0x0000ff00)|((K_S[i++])&0x000000ff);
|
||||
tmp=new byte[j]; System.arraycopy(K_S, i, tmp, 0, j); i+=j;
|
||||
n=tmp;
|
||||
|
||||
SignatureRSA sig=null;
|
||||
try{
|
||||
Class c=Class.forName(session.getConfig("signature.rsa"));
|
||||
sig=(SignatureRSA)(c.newInstance());
|
||||
sig.init();
|
||||
}
|
||||
catch(Exception e){
|
||||
System.err.println(e);
|
||||
}
|
||||
|
||||
sig.setPubKey(ee, n);
|
||||
sig.update(H);
|
||||
result=sig.verify(sig_of_H);
|
||||
|
||||
if(JSch.getLogger().isEnabled(Logger.INFO)){
|
||||
JSch.getLogger().log(Logger.INFO,
|
||||
"ssh_rsa_verify: signature "+result);
|
||||
}
|
||||
|
||||
}
|
||||
else if(alg.equals("ssh-dss")){
|
||||
byte[] q=null;
|
||||
byte[] tmp;
|
||||
byte[] p;
|
||||
byte[] g;
|
||||
|
||||
type=DSS;
|
||||
|
||||
j=((K_S[i++]<<24)&0xff000000)|((K_S[i++]<<16)&0x00ff0000)|
|
||||
((K_S[i++]<<8)&0x0000ff00)|((K_S[i++])&0x000000ff);
|
||||
tmp=new byte[j]; System.arraycopy(K_S, i, tmp, 0, j); i+=j;
|
||||
p=tmp;
|
||||
j=((K_S[i++]<<24)&0xff000000)|((K_S[i++]<<16)&0x00ff0000)|
|
||||
((K_S[i++]<<8)&0x0000ff00)|((K_S[i++])&0x000000ff);
|
||||
tmp=new byte[j]; System.arraycopy(K_S, i, tmp, 0, j); i+=j;
|
||||
q=tmp;
|
||||
j=((K_S[i++]<<24)&0xff000000)|((K_S[i++]<<16)&0x00ff0000)|
|
||||
((K_S[i++]<<8)&0x0000ff00)|((K_S[i++])&0x000000ff);
|
||||
tmp=new byte[j]; System.arraycopy(K_S, i, tmp, 0, j); i+=j;
|
||||
g=tmp;
|
||||
j=((K_S[i++]<<24)&0xff000000)|((K_S[i++]<<16)&0x00ff0000)|
|
||||
((K_S[i++]<<8)&0x0000ff00)|((K_S[i++])&0x000000ff);
|
||||
tmp=new byte[j]; System.arraycopy(K_S, i, tmp, 0, j); i+=j;
|
||||
f=tmp;
|
||||
|
||||
SignatureDSA sig=null;
|
||||
try{
|
||||
Class c=Class.forName(session.getConfig("signature.dss"));
|
||||
sig=(SignatureDSA)(c.newInstance());
|
||||
sig.init();
|
||||
}
|
||||
catch(Exception e){
|
||||
System.err.println(e);
|
||||
}
|
||||
sig.setPubKey(f, p, q, g);
|
||||
sig.update(H);
|
||||
result=sig.verify(sig_of_H);
|
||||
|
||||
if(JSch.getLogger().isEnabled(Logger.INFO)){
|
||||
JSch.getLogger().log(Logger.INFO,
|
||||
"ssh_dss_verify: signature "+result);
|
||||
}
|
||||
|
||||
}
|
||||
else{
|
||||
System.err.println("unknown alg");
|
||||
}
|
||||
state=STATE_END;
|
||||
return result;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public String getKeyType(){
|
||||
if(type==DSS) return "DSA";
|
||||
return "RSA";
|
||||
}
|
||||
|
||||
public int getState(){return state; }
|
||||
}
|
@ -1,340 +0,0 @@
|
||||
/* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
|
||||
/*
|
||||
Copyright (c) 2002-2011 ymnk, JCraft,Inc. All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice,
|
||||
this list of conditions and the following disclaimer.
|
||||
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the distribution.
|
||||
|
||||
3. The names of the authors may not be used to endorse or promote products
|
||||
derived from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
|
||||
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
|
||||
INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
|
||||
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
||||
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
package com.jcraft.jsch;
|
||||
|
||||
public class DHGEX extends KeyExchange{
|
||||
|
||||
private static final int SSH_MSG_KEX_DH_GEX_GROUP= 31;
|
||||
private static final int SSH_MSG_KEX_DH_GEX_INIT= 32;
|
||||
private static final int SSH_MSG_KEX_DH_GEX_REPLY= 33;
|
||||
private static final int SSH_MSG_KEX_DH_GEX_REQUEST= 34;
|
||||
|
||||
static int min=1024;
|
||||
|
||||
// static int min=512;
|
||||
static int preferred=1024;
|
||||
static int max=1024;
|
||||
|
||||
// static int preferred=1024;
|
||||
// static int max=2000;
|
||||
|
||||
static final int RSA=0;
|
||||
static final int DSS=1;
|
||||
private int type=0;
|
||||
|
||||
private int state;
|
||||
|
||||
// com.jcraft.jsch.DH dh;
|
||||
DH dh;
|
||||
|
||||
byte[] V_S;
|
||||
byte[] V_C;
|
||||
byte[] I_S;
|
||||
byte[] I_C;
|
||||
|
||||
private Buffer buf;
|
||||
private Packet packet;
|
||||
|
||||
private byte[] p;
|
||||
private byte[] g;
|
||||
private byte[] e;
|
||||
//private byte[] f;
|
||||
|
||||
public void init(Session session,
|
||||
byte[] V_S, byte[] V_C, byte[] I_S, byte[] I_C) throws Exception{
|
||||
this.session=session;
|
||||
this.V_S=V_S;
|
||||
this.V_C=V_C;
|
||||
this.I_S=I_S;
|
||||
this.I_C=I_C;
|
||||
|
||||
try{
|
||||
Class c=Class.forName(session.getConfig("sha-1"));
|
||||
sha=(HASH)(c.newInstance());
|
||||
sha.init();
|
||||
}
|
||||
catch(Exception e){
|
||||
System.err.println(e);
|
||||
}
|
||||
|
||||
buf=new Buffer();
|
||||
packet=new Packet(buf);
|
||||
|
||||
try{
|
||||
Class c=Class.forName(session.getConfig("dh"));
|
||||
dh=(com.jcraft.jsch.DH)(c.newInstance());
|
||||
dh.init();
|
||||
}
|
||||
catch(Exception e){
|
||||
// System.err.println(e);
|
||||
throw e;
|
||||
}
|
||||
|
||||
packet.reset();
|
||||
buf.putByte((byte)SSH_MSG_KEX_DH_GEX_REQUEST);
|
||||
buf.putInt(min);
|
||||
buf.putInt(preferred);
|
||||
buf.putInt(max);
|
||||
session.write(packet);
|
||||
|
||||
if(JSch.getLogger().isEnabled(Logger.INFO)){
|
||||
JSch.getLogger().log(Logger.INFO,
|
||||
"SSH_MSG_KEX_DH_GEX_REQUEST("+min+"<"+preferred+"<"+max+") sent");
|
||||
JSch.getLogger().log(Logger.INFO,
|
||||
"expecting SSH_MSG_KEX_DH_GEX_GROUP");
|
||||
}
|
||||
|
||||
state=SSH_MSG_KEX_DH_GEX_GROUP;
|
||||
}
|
||||
|
||||
public boolean next(Buffer _buf) throws Exception{
|
||||
int i,j;
|
||||
switch(state){
|
||||
case SSH_MSG_KEX_DH_GEX_GROUP:
|
||||
// byte SSH_MSG_KEX_DH_GEX_GROUP(31)
|
||||
// mpint p, safe prime
|
||||
// mpint g, generator for subgroup in GF (p)
|
||||
_buf.getInt();
|
||||
_buf.getByte();
|
||||
j=_buf.getByte();
|
||||
if(j!=SSH_MSG_KEX_DH_GEX_GROUP){
|
||||
System.err.println("type: must be SSH_MSG_KEX_DH_GEX_GROUP "+j);
|
||||
return false;
|
||||
}
|
||||
|
||||
p=_buf.getMPInt();
|
||||
g=_buf.getMPInt();
|
||||
/*
|
||||
for(int iii=0; iii<p.length; iii++){
|
||||
System.err.println("0x"+Integer.toHexString(p[iii]&0xff)+",");
|
||||
}
|
||||
System.err.println("");
|
||||
for(int iii=0; iii<g.length; iii++){
|
||||
System.err.println("0x"+Integer.toHexString(g[iii]&0xff)+",");
|
||||
}
|
||||
*/
|
||||
dh.setP(p);
|
||||
dh.setG(g);
|
||||
|
||||
// The client responds with:
|
||||
// byte SSH_MSG_KEX_DH_GEX_INIT(32)
|
||||
// mpint e <- g^x mod p
|
||||
// x is a random number (1 < x < (p-1)/2)
|
||||
|
||||
e=dh.getE();
|
||||
|
||||
packet.reset();
|
||||
buf.putByte((byte)SSH_MSG_KEX_DH_GEX_INIT);
|
||||
buf.putMPInt(e);
|
||||
session.write(packet);
|
||||
|
||||
if(JSch.getLogger().isEnabled(Logger.INFO)){
|
||||
JSch.getLogger().log(Logger.INFO,
|
||||
"SSH_MSG_KEX_DH_GEX_INIT sent");
|
||||
JSch.getLogger().log(Logger.INFO,
|
||||
"expecting SSH_MSG_KEX_DH_GEX_REPLY");
|
||||
}
|
||||
|
||||
state=SSH_MSG_KEX_DH_GEX_REPLY;
|
||||
return true;
|
||||
//break;
|
||||
|
||||
case SSH_MSG_KEX_DH_GEX_REPLY:
|
||||
// The server responds with:
|
||||
// byte SSH_MSG_KEX_DH_GEX_REPLY(33)
|
||||
// string server public host key and certificates (K_S)
|
||||
// mpint f
|
||||
// string signature of H
|
||||
j=_buf.getInt();
|
||||
j=_buf.getByte();
|
||||
j=_buf.getByte();
|
||||
if(j!=SSH_MSG_KEX_DH_GEX_REPLY){
|
||||
System.err.println("type: must be SSH_MSG_KEX_DH_GEX_REPLY "+j);
|
||||
return false;
|
||||
}
|
||||
|
||||
K_S=_buf.getString();
|
||||
// K_S is server_key_blob, which includes ....
|
||||
// string ssh-dss
|
||||
// impint p of dsa
|
||||
// impint q of dsa
|
||||
// impint g of dsa
|
||||
// impint pub_key of dsa
|
||||
//System.err.print("K_S: "); dump(K_S, 0, K_S.length);
|
||||
|
||||
byte[] f=_buf.getMPInt();
|
||||
byte[] sig_of_H=_buf.getString();
|
||||
|
||||
dh.setF(f);
|
||||
K=dh.getK();
|
||||
|
||||
//The hash H is computed as the HASH hash of the concatenation of the
|
||||
//following:
|
||||
// string V_C, the client's version string (CR and NL excluded)
|
||||
// string V_S, the server's version string (CR and NL excluded)
|
||||
// string I_C, the payload of the client's SSH_MSG_KEXINIT
|
||||
// string I_S, the payload of the server's SSH_MSG_KEXINIT
|
||||
// string K_S, the host key
|
||||
// uint32 min, minimal size in bits of an acceptable group
|
||||
// uint32 n, preferred size in bits of the group the server should send
|
||||
// uint32 max, maximal size in bits of an acceptable group
|
||||
// mpint p, safe prime
|
||||
// mpint g, generator for subgroup
|
||||
// mpint e, exchange value sent by the client
|
||||
// mpint f, exchange value sent by the server
|
||||
// mpint K, the shared secret
|
||||
// This value is called the exchange hash, and it is used to authenti-
|
||||
// cate the key exchange.
|
||||
|
||||
buf.reset();
|
||||
buf.putString(V_C); buf.putString(V_S);
|
||||
buf.putString(I_C); buf.putString(I_S);
|
||||
buf.putString(K_S);
|
||||
buf.putInt(min); buf.putInt(preferred); buf.putInt(max);
|
||||
buf.putMPInt(p); buf.putMPInt(g); buf.putMPInt(e); buf.putMPInt(f);
|
||||
buf.putMPInt(K);
|
||||
|
||||
byte[] foo=new byte[buf.getLength()];
|
||||
buf.getByte(foo);
|
||||
sha.update(foo, 0, foo.length);
|
||||
|
||||
H=sha.digest();
|
||||
|
||||
// System.err.print("H -> "); dump(H, 0, H.length);
|
||||
|
||||
i=0;
|
||||
j=0;
|
||||
j=((K_S[i++]<<24)&0xff000000)|((K_S[i++]<<16)&0x00ff0000)|
|
||||
((K_S[i++]<<8)&0x0000ff00)|((K_S[i++])&0x000000ff);
|
||||
String alg=Util.byte2str(K_S, i, j);
|
||||
i+=j;
|
||||
|
||||
boolean result=false;
|
||||
if(alg.equals("ssh-rsa")){
|
||||
byte[] tmp;
|
||||
byte[] ee;
|
||||
byte[] n;
|
||||
|
||||
type=RSA;
|
||||
|
||||
j=((K_S[i++]<<24)&0xff000000)|((K_S[i++]<<16)&0x00ff0000)|
|
||||
((K_S[i++]<<8)&0x0000ff00)|((K_S[i++])&0x000000ff);
|
||||
tmp=new byte[j]; System.arraycopy(K_S, i, tmp, 0, j); i+=j;
|
||||
ee=tmp;
|
||||
j=((K_S[i++]<<24)&0xff000000)|((K_S[i++]<<16)&0x00ff0000)|
|
||||
((K_S[i++]<<8)&0x0000ff00)|((K_S[i++])&0x000000ff);
|
||||
tmp=new byte[j]; System.arraycopy(K_S, i, tmp, 0, j); i+=j;
|
||||
n=tmp;
|
||||
|
||||
// SignatureRSA sig=new SignatureRSA();
|
||||
// sig.init();
|
||||
|
||||
SignatureRSA sig=null;
|
||||
try{
|
||||
Class c=Class.forName(session.getConfig("signature.rsa"));
|
||||
sig=(SignatureRSA)(c.newInstance());
|
||||
sig.init();
|
||||
}
|
||||
catch(Exception e){
|
||||
System.err.println(e);
|
||||
}
|
||||
|
||||
sig.setPubKey(ee, n);
|
||||
sig.update(H);
|
||||
result=sig.verify(sig_of_H);
|
||||
|
||||
if(JSch.getLogger().isEnabled(Logger.INFO)){
|
||||
JSch.getLogger().log(Logger.INFO,
|
||||
"ssh_rsa_verify: signature "+result);
|
||||
}
|
||||
|
||||
}
|
||||
else if(alg.equals("ssh-dss")){
|
||||
byte[] q=null;
|
||||
byte[] tmp;
|
||||
|
||||
type=DSS;
|
||||
|
||||
j=((K_S[i++]<<24)&0xff000000)|((K_S[i++]<<16)&0x00ff0000)|
|
||||
((K_S[i++]<<8)&0x0000ff00)|((K_S[i++])&0x000000ff);
|
||||
tmp=new byte[j]; System.arraycopy(K_S, i, tmp, 0, j); i+=j;
|
||||
p=tmp;
|
||||
j=((K_S[i++]<<24)&0xff000000)|((K_S[i++]<<16)&0x00ff0000)|
|
||||
((K_S[i++]<<8)&0x0000ff00)|((K_S[i++])&0x000000ff);
|
||||
tmp=new byte[j]; System.arraycopy(K_S, i, tmp, 0, j); i+=j;
|
||||
q=tmp;
|
||||
j=((K_S[i++]<<24)&0xff000000)|((K_S[i++]<<16)&0x00ff0000)|
|
||||
((K_S[i++]<<8)&0x0000ff00)|((K_S[i++])&0x000000ff);
|
||||
tmp=new byte[j]; System.arraycopy(K_S, i, tmp, 0, j); i+=j;
|
||||
g=tmp;
|
||||
j=((K_S[i++]<<24)&0xff000000)|((K_S[i++]<<16)&0x00ff0000)|
|
||||
((K_S[i++]<<8)&0x0000ff00)|((K_S[i++])&0x000000ff);
|
||||
tmp=new byte[j]; System.arraycopy(K_S, i, tmp, 0, j); i+=j;
|
||||
f=tmp;
|
||||
|
||||
// SignatureDSA sig=new SignatureDSA();
|
||||
// sig.init();
|
||||
|
||||
SignatureDSA sig=null;
|
||||
try{
|
||||
Class c=Class.forName(session.getConfig("signature.dss"));
|
||||
sig=(SignatureDSA)(c.newInstance());
|
||||
sig.init();
|
||||
}
|
||||
catch(Exception e){
|
||||
System.err.println(e);
|
||||
}
|
||||
|
||||
sig.setPubKey(f, p, q, g);
|
||||
sig.update(H);
|
||||
result=sig.verify(sig_of_H);
|
||||
|
||||
if(JSch.getLogger().isEnabled(Logger.INFO)){
|
||||
JSch.getLogger().log(Logger.INFO,
|
||||
"ssh_dss_verify: signature "+result);
|
||||
}
|
||||
|
||||
}
|
||||
else{
|
||||
System.err.println("unknown alg");
|
||||
}
|
||||
state=STATE_END;
|
||||
return result;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public String getKeyType(){
|
||||
if(type==DSS) return "DSA";
|
||||
return "RSA";
|
||||
}
|
||||
|
||||
public int getState(){return state; }
|
||||
}
|
@ -1,36 +0,0 @@
|
||||
/* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
|
||||
/*
|
||||
Copyright (c) 2002-2011 ymnk, JCraft,Inc. All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice,
|
||||
this list of conditions and the following disclaimer.
|
||||
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the distribution.
|
||||
|
||||
3. The names of the authors may not be used to endorse or promote products
|
||||
derived from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
|
||||
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
|
||||
INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
|
||||
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
||||
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
package com.jcraft.jsch;
|
||||
import java.io.*;
|
||||
|
||||
public interface ForwardedTCPIPDaemon extends Runnable{
|
||||
void setChannel(ChannelForwardedTCPIP channel, InputStream in, OutputStream out);
|
||||
void setArg(Object[] arg);
|
||||
}
|
@ -1,38 +0,0 @@
|
||||
/* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
|
||||
/*
|
||||
Copyright (c) 2004-2011 ymnk, JCraft,Inc. All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice,
|
||||
this list of conditions and the following disclaimer.
|
||||
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the distribution.
|
||||
|
||||
3. The names of the authors may not be used to endorse or promote products
|
||||
derived from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
|
||||
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
|
||||
INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
|
||||
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
||||
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
package com.jcraft.jsch;
|
||||
|
||||
public interface GSSContext{
|
||||
public void create(String user, String host) throws JSchException;
|
||||
public boolean isEstablished();
|
||||
public byte[] init(byte[] token, int s, int l) throws JSchException;
|
||||
public byte[] getMIC(byte[] message, int s, int l);
|
||||
public void dispose();
|
||||
}
|
@ -1,37 +0,0 @@
|
||||
/* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
|
||||
/*
|
||||
Copyright (c) 2002-2011 ymnk, JCraft,Inc. All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice,
|
||||
this list of conditions and the following disclaimer.
|
||||
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the distribution.
|
||||
|
||||
3. The names of the authors may not be used to endorse or promote products
|
||||
derived from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
|
||||
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
|
||||
INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
|
||||
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
||||
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
package com.jcraft.jsch;
|
||||
|
||||
public interface HASH{
|
||||
void init() throws Exception;
|
||||
int getBlockSize();
|
||||
void update(byte[] foo, int start, int len) throws Exception;
|
||||
byte[] digest() throws Exception;
|
||||
}
|
@ -1,104 +0,0 @@
|
||||
/* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
|
||||
/*
|
||||
Copyright (c) 2002-2011 ymnk, JCraft,Inc. All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice,
|
||||
this list of conditions and the following disclaimer.
|
||||
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the distribution.
|
||||
|
||||
3. The names of the authors may not be used to endorse or promote products
|
||||
derived from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
|
||||
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
|
||||
INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
|
||||
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
||||
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
package com.jcraft.jsch;
|
||||
|
||||
public class HostKey{
|
||||
private static final byte[] sshdss=Util.str2byte("ssh-dss");
|
||||
private static final byte[] sshrsa=Util.str2byte("ssh-rsa");
|
||||
|
||||
protected static final int GUESS=0;
|
||||
public static final int SSHDSS=1;
|
||||
public static final int SSHRSA=2;
|
||||
static final int UNKNOWN=3;
|
||||
|
||||
protected String host;
|
||||
protected int type;
|
||||
protected byte[] key;
|
||||
|
||||
public HostKey(String host, byte[] key) throws JSchException {
|
||||
this(host, GUESS, key);
|
||||
}
|
||||
|
||||
public HostKey(String host, int type, byte[] key) throws JSchException {
|
||||
this.host=host;
|
||||
if(type==GUESS){
|
||||
if(key[8]=='d'){ this.type=SSHDSS; }
|
||||
else if(key[8]=='r'){ this.type=SSHRSA; }
|
||||
else { throw new JSchException("invalid key type");}
|
||||
}
|
||||
else{
|
||||
this.type=type;
|
||||
}
|
||||
this.key=key;
|
||||
}
|
||||
|
||||
public String getHost(){ return host; }
|
||||
public String getType(){
|
||||
if(type==SSHDSS){ return Util.byte2str(sshdss); }
|
||||
if(type==SSHRSA){ return Util.byte2str(sshrsa);}
|
||||
return "UNKNOWN";
|
||||
}
|
||||
public String getKey(){
|
||||
return Util.byte2str(Util.toBase64(key, 0, key.length));
|
||||
}
|
||||
public String getFingerPrint(JSch jsch){
|
||||
HASH hash=null;
|
||||
try{
|
||||
Class c=Class.forName(jsch.getConfig("md5"));
|
||||
hash=(HASH)(c.newInstance());
|
||||
}
|
||||
catch(Exception e){ System.err.println("getFingerPrint: "+e); }
|
||||
return Util.getFingerPrint(hash, key);
|
||||
}
|
||||
|
||||
boolean isMatched(String _host){
|
||||
return isIncluded(_host);
|
||||
}
|
||||
|
||||
private boolean isIncluded(String _host){
|
||||
int i=0;
|
||||
String hosts=this.host;
|
||||
int hostslen=hosts.length();
|
||||
int hostlen=_host.length();
|
||||
int j;
|
||||
while(i<hostslen){
|
||||
j=hosts.indexOf(',', i);
|
||||
if(j==-1){
|
||||
if(hostlen!=hostslen-i) return false;
|
||||
return hosts.regionMatches(true, i, _host, 0, hostlen);
|
||||
}
|
||||
if(hostlen==(j-i)){
|
||||
if(hosts.regionMatches(true, i, _host, 0, hostlen)) return true;
|
||||
}
|
||||
i=j+1;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
@ -1,44 +0,0 @@
|
||||
/* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
|
||||
/*
|
||||
Copyright (c) 2004-2011 ymnk, JCraft,Inc. All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice,
|
||||
this list of conditions and the following disclaimer.
|
||||
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the distribution.
|
||||
|
||||
3. The names of the authors may not be used to endorse or promote products
|
||||
derived from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
|
||||
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
|
||||
INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
|
||||
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
||||
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
package com.jcraft.jsch;
|
||||
|
||||
public interface HostKeyRepository{
|
||||
final int OK=0;
|
||||
final int NOT_INCLUDED=1;
|
||||
final int CHANGED=2;
|
||||
|
||||
int check(String host, byte[] key);
|
||||
void add(HostKey hostkey, UserInfo ui);
|
||||
void remove(String host, String type);
|
||||
void remove(String host, String type, byte[] key);
|
||||
String getKnownHostsRepositoryID();
|
||||
HostKey[] getHostKey();
|
||||
HostKey[] getHostKey(String host, String type);
|
||||
}
|
@ -1,132 +0,0 @@
|
||||
/* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
|
||||
/*
|
||||
Copyright (c) 2002-2011 ymnk, JCraft,Inc. All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice,
|
||||
this list of conditions and the following disclaimer.
|
||||
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the distribution.
|
||||
|
||||
3. The names of the authors may not be used to endorse or promote products
|
||||
derived from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
|
||||
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
|
||||
INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
|
||||
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
||||
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
package com.jcraft.jsch;
|
||||
|
||||
import java.io.*;
|
||||
|
||||
public class IO{
|
||||
InputStream in;
|
||||
OutputStream out;
|
||||
OutputStream out_ext;
|
||||
|
||||
private boolean in_dontclose=false;
|
||||
private boolean out_dontclose=false;
|
||||
private boolean out_ext_dontclose=false;
|
||||
|
||||
void setOutputStream(OutputStream out){ this.out=out; }
|
||||
void setOutputStream(OutputStream out, boolean dontclose){
|
||||
this.out_dontclose=dontclose;
|
||||
setOutputStream(out);
|
||||
}
|
||||
void setExtOutputStream(OutputStream out){ this.out_ext=out; }
|
||||
void setExtOutputStream(OutputStream out, boolean dontclose){
|
||||
this.out_ext_dontclose=dontclose;
|
||||
setExtOutputStream(out);
|
||||
}
|
||||
void setInputStream(InputStream in){ this.in=in; }
|
||||
void setInputStream(InputStream in, boolean dontclose){
|
||||
this.in_dontclose=dontclose;
|
||||
setInputStream(in);
|
||||
}
|
||||
|
||||
public void put(Packet p) throws IOException, java.net.SocketException {
|
||||
out.write(p.buffer.buffer, 0, p.buffer.index);
|
||||
out.flush();
|
||||
}
|
||||
void put(byte[] array, int begin, int length) throws IOException {
|
||||
out.write(array, begin, length);
|
||||
out.flush();
|
||||
}
|
||||
void put_ext(byte[] array, int begin, int length) throws IOException {
|
||||
out_ext.write(array, begin, length);
|
||||
out_ext.flush();
|
||||
}
|
||||
|
||||
int getByte() throws IOException {
|
||||
return in.read();
|
||||
}
|
||||
|
||||
void getByte(byte[] array) throws IOException {
|
||||
getByte(array, 0, array.length);
|
||||
}
|
||||
|
||||
void getByte(byte[] array, int begin, int length) throws IOException {
|
||||
do{
|
||||
int completed = in.read(array, begin, length);
|
||||
if(completed<0){
|
||||
throw new IOException("End of IO Stream Read");
|
||||
}
|
||||
begin+=completed;
|
||||
length-=completed;
|
||||
}
|
||||
while (length>0);
|
||||
}
|
||||
|
||||
void out_close(){
|
||||
try{
|
||||
if(out!=null && !out_dontclose) out.close();
|
||||
out=null;
|
||||
}
|
||||
catch(Exception ee){}
|
||||
}
|
||||
|
||||
public void close(){
|
||||
try{
|
||||
if(in!=null && !in_dontclose) in.close();
|
||||
in=null;
|
||||
}
|
||||
catch(Exception ee){}
|
||||
|
||||
out_close();
|
||||
|
||||
try{
|
||||
if(out_ext!=null && !out_ext_dontclose) out_ext.close();
|
||||
out_ext=null;
|
||||
}
|
||||
catch(Exception ee){}
|
||||
}
|
||||
|
||||
/*
|
||||
public void finalize() throws Throwable{
|
||||
try{
|
||||
if(in!=null) in.close();
|
||||
}
|
||||
catch(Exception ee){}
|
||||
try{
|
||||
if(out!=null) out.close();
|
||||
}
|
||||
catch(Exception ee){}
|
||||
try{
|
||||
if(out_ext!=null) out_ext.close();
|
||||
}
|
||||
catch(Exception ee){}
|
||||
}
|
||||
*/
|
||||
}
|
@ -1,41 +0,0 @@
|
||||
/* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
|
||||
/*
|
||||
Copyright (c) 2002-2011 ymnk, JCraft,Inc. All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice,
|
||||
this list of conditions and the following disclaimer.
|
||||
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the distribution.
|
||||
|
||||
3. The names of the authors may not be used to endorse or promote products
|
||||
derived from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
|
||||
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
|
||||
INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
|
||||
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
||||
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
package com.jcraft.jsch;
|
||||
|
||||
public interface Identity{
|
||||
public boolean setPassphrase(byte[] passphrase) throws JSchException;
|
||||
public byte[] getPublicKeyBlob();
|
||||
public byte[] getSignature(byte[] data);
|
||||
public boolean decrypt();
|
||||
public String getAlgName();
|
||||
public String getName();
|
||||
public boolean isEncrypted();
|
||||
public void clear();
|
||||
}
|
@ -1,918 +0,0 @@
|
||||
/* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
|
||||
/*
|
||||
Copyright (c) 2002-2011 ymnk, JCraft,Inc. All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice,
|
||||
this list of conditions and the following disclaimer.
|
||||
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the distribution.
|
||||
|
||||
3. The names of the authors may not be used to endorse or promote products
|
||||
derived from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
|
||||
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
|
||||
INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
|
||||
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
||||
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
package com.jcraft.jsch;
|
||||
|
||||
import java.io.*;
|
||||
|
||||
class IdentityFile implements Identity{
|
||||
String identity;
|
||||
byte[] key;
|
||||
byte[] iv;
|
||||
private JSch jsch;
|
||||
private HASH hash;
|
||||
private byte[] encoded_data;
|
||||
|
||||
private Cipher cipher;
|
||||
|
||||
// DSA
|
||||
private byte[] P_array;
|
||||
private byte[] Q_array;
|
||||
private byte[] G_array;
|
||||
private byte[] pub_array;
|
||||
private byte[] prv_array;
|
||||
|
||||
// RSA
|
||||
private byte[] n_array; // modulus
|
||||
private byte[] e_array; // public exponent
|
||||
private byte[] d_array; // private exponent
|
||||
|
||||
// private String algname="ssh-dss";
|
||||
private String algname="ssh-rsa";
|
||||
|
||||
private static final int ERROR=0;
|
||||
private static final int RSA=1;
|
||||
private static final int DSS=2;
|
||||
private static final int UNKNOWN=3;
|
||||
|
||||
private static final int OPENSSH=0;
|
||||
private static final int FSECURE=1;
|
||||
private static final int PUTTY=2;
|
||||
|
||||
private int type=ERROR;
|
||||
private int keytype=OPENSSH;
|
||||
|
||||
private byte[] publickeyblob=null;
|
||||
|
||||
private boolean encrypted=true;
|
||||
|
||||
static IdentityFile newInstance(String prvfile, String pubfile, JSch jsch) throws JSchException{
|
||||
byte[] prvkey=null;
|
||||
byte[] pubkey=null;
|
||||
|
||||
File file=null;
|
||||
FileInputStream fis=null;
|
||||
try{
|
||||
file=new File(prvfile);
|
||||
fis=new FileInputStream(prvfile);
|
||||
prvkey=new byte[(int)(file.length())];
|
||||
int len=0;
|
||||
while(true){
|
||||
int i=fis.read(prvkey, len, prvkey.length-len);
|
||||
if(i<=0)
|
||||
break;
|
||||
len+=i;
|
||||
}
|
||||
fis.close();
|
||||
}
|
||||
catch(Exception e){
|
||||
try{ if(fis!=null) fis.close();}
|
||||
catch(Exception ee){}
|
||||
if(e instanceof Throwable)
|
||||
throw new JSchException(e.toString(), (Throwable)e);
|
||||
throw new JSchException(e.toString());
|
||||
}
|
||||
|
||||
String _pubfile=pubfile;
|
||||
if(pubfile==null){
|
||||
_pubfile=prvfile+".pub";
|
||||
}
|
||||
|
||||
try{
|
||||
file=new File(_pubfile);
|
||||
fis = new FileInputStream(_pubfile);
|
||||
pubkey=new byte[(int)(file.length())];
|
||||
int len=0;
|
||||
while(true){
|
||||
int i=fis.read(pubkey, len, pubkey.length-len);
|
||||
if(i<=0)
|
||||
break;
|
||||
len+=i;
|
||||
}
|
||||
fis.close();
|
||||
}
|
||||
catch(Exception e){
|
||||
try{ if(fis!=null) fis.close();}
|
||||
catch(Exception ee){}
|
||||
if(pubfile!=null){
|
||||
// The pubfile is explicitry given, but not accessible.
|
||||
if(e instanceof Throwable)
|
||||
throw new JSchException(e.toString(), (Throwable)e);
|
||||
throw new JSchException(e.toString());
|
||||
}
|
||||
}
|
||||
return newInstance(prvfile, prvkey, pubkey, jsch);
|
||||
}
|
||||
|
||||
static IdentityFile newInstance(String name, byte[] prvkey, byte[] pubkey, JSch jsch) throws JSchException{
|
||||
try{
|
||||
return new IdentityFile(name, prvkey, pubkey, jsch);
|
||||
}
|
||||
finally{
|
||||
Util.bzero(prvkey);
|
||||
}
|
||||
}
|
||||
|
||||
private IdentityFile(String name, byte[] prvkey, byte[] pubkey, JSch jsch) throws JSchException{
|
||||
this.identity=name;
|
||||
this.jsch=jsch;
|
||||
|
||||
/* TODO: IdentityFile should use KeyPair.
|
||||
* The following logic exists also in KeyPair. It is redundant.
|
||||
*/
|
||||
try{
|
||||
Class c;
|
||||
c=Class.forName((String)jsch.getConfig("3des-cbc"));
|
||||
cipher=(Cipher)(c.newInstance());
|
||||
key=new byte[cipher.getBlockSize()]; // 24
|
||||
iv=new byte[cipher.getIVSize()]; // 8
|
||||
c=Class.forName((String)jsch.getConfig("md5"));
|
||||
hash=(HASH)(c.newInstance());
|
||||
hash.init();
|
||||
|
||||
byte[] buf=prvkey;
|
||||
int len=buf.length;
|
||||
|
||||
int i=0;
|
||||
|
||||
while(i<len){
|
||||
if(buf[i] == '-' && i+4<len &&
|
||||
buf[i+1] == '-' && buf[i+2] == '-' &&
|
||||
buf[i+3] == '-' && buf[i+4] == '-'){
|
||||
break;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
|
||||
while(i<len){
|
||||
if(buf[i]=='B'&& i+3<len && buf[i+1]=='E'&& buf[i+2]=='G'&& buf[i+3]=='I'){
|
||||
i+=6;
|
||||
if(buf[i]=='D'&& buf[i+1]=='S'&& buf[i+2]=='A'){ type=DSS; }
|
||||
else if(buf[i]=='R'&& buf[i+1]=='S'&& buf[i+2]=='A'){ type=RSA; }
|
||||
else if(buf[i]=='S'&& buf[i+1]=='S'&& buf[i+2]=='H'){ // FSecure
|
||||
type=UNKNOWN;
|
||||
keytype=FSECURE;
|
||||
}
|
||||
else{
|
||||
//System.err.println("invalid format: "+identity);
|
||||
throw new JSchException("invalid privatekey: "+identity);
|
||||
}
|
||||
i+=3;
|
||||
continue;
|
||||
}
|
||||
if(buf[i]=='A'&& i+7<len && buf[i+1]=='E'&& buf[i+2]=='S'&& buf[i+3]=='-' &&
|
||||
buf[i+4]=='2'&& buf[i+5]=='5'&& buf[i+6]=='6'&& buf[i+7]=='-'){
|
||||
i+=8;
|
||||
if(Session.checkCipher((String)jsch.getConfig("aes256-cbc"))){
|
||||
c=Class.forName((String)jsch.getConfig("aes256-cbc"));
|
||||
cipher=(Cipher)(c.newInstance());
|
||||
key=new byte[cipher.getBlockSize()];
|
||||
iv=new byte[cipher.getIVSize()];
|
||||
}
|
||||
else{
|
||||
throw new JSchException("privatekey: aes256-cbc is not available "+identity);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if(buf[i]=='A'&& i+7<len && buf[i+1]=='E'&& buf[i+2]=='S'&& buf[i+3]=='-' &&
|
||||
buf[i+4]=='1'&& buf[i+5]=='9'&& buf[i+6]=='2'&& buf[i+7]=='-'){
|
||||
i+=8;
|
||||
if(Session.checkCipher((String)jsch.getConfig("aes192-cbc"))){
|
||||
c=Class.forName((String)jsch.getConfig("aes192-cbc"));
|
||||
cipher=(Cipher)(c.newInstance());
|
||||
key=new byte[cipher.getBlockSize()];
|
||||
iv=new byte[cipher.getIVSize()];
|
||||
}
|
||||
else{
|
||||
throw new JSchException("privatekey: aes192-cbc is not available "+identity);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if(buf[i]=='A'&& i+7<len && buf[i+1]=='E'&& buf[i+2]=='S'&& buf[i+3]=='-' &&
|
||||
buf[i+4]=='1'&& buf[i+5]=='2'&& buf[i+6]=='8'&& buf[i+7]=='-'){
|
||||
i+=8;
|
||||
if(Session.checkCipher((String)jsch.getConfig("aes128-cbc"))){
|
||||
c=Class.forName((String)jsch.getConfig("aes128-cbc"));
|
||||
cipher=(Cipher)(c.newInstance());
|
||||
key=new byte[cipher.getBlockSize()];
|
||||
iv=new byte[cipher.getIVSize()];
|
||||
}
|
||||
else{
|
||||
throw new JSchException("privatekey: aes128-cbc is not available "+identity);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if(buf[i]=='C'&& i+3<len && buf[i+1]=='B'&& buf[i+2]=='C'&& buf[i+3]==','){
|
||||
i+=4;
|
||||
for(int ii=0; ii<iv.length; ii++){
|
||||
iv[ii]=(byte)(((a2b(buf[i++])<<4)&0xf0)+
|
||||
(a2b(buf[i++])&0xf));
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if(buf[i]==0x0d && i+1<len && buf[i+1]==0x0a){
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
if(buf[i]==0x0a && i+1<len){
|
||||
if(buf[i+1]==0x0a){ i+=2; break; }
|
||||
if(buf[i+1]==0x0d &&
|
||||
i+2<len && buf[i+2]==0x0a){
|
||||
i+=3; break;
|
||||
}
|
||||
boolean inheader=false;
|
||||
for(int j=i+1; j<len; j++){
|
||||
if(buf[j]==0x0a) break;
|
||||
//if(buf[j]==0x0d) break;
|
||||
if(buf[j]==':'){inheader=true; break;}
|
||||
}
|
||||
if(!inheader){
|
||||
i++;
|
||||
encrypted=false; // no passphrase
|
||||
break;
|
||||
}
|
||||
}
|
||||
i++;
|
||||
}
|
||||
|
||||
if(type==ERROR){
|
||||
throw new JSchException("invalid privatekey: "+identity);
|
||||
}
|
||||
|
||||
int start=i;
|
||||
while(i<len){
|
||||
if(buf[i]==0x0a){
|
||||
boolean xd=(buf[i-1]==0x0d);
|
||||
System.arraycopy(buf, i+1,
|
||||
buf,
|
||||
i-(xd ? 1 : 0),
|
||||
len-i-1-(xd ? 1 : 0)
|
||||
);
|
||||
if(xd)len--;
|
||||
len--;
|
||||
continue;
|
||||
}
|
||||
if(buf[i]=='-'){ break; }
|
||||
i++;
|
||||
}
|
||||
encoded_data=Util.fromBase64(buf, start, i-start);
|
||||
|
||||
if(encoded_data.length>4 && // FSecure
|
||||
encoded_data[0]==(byte)0x3f &&
|
||||
encoded_data[1]==(byte)0x6f &&
|
||||
encoded_data[2]==(byte)0xf9 &&
|
||||
encoded_data[3]==(byte)0xeb){
|
||||
|
||||
Buffer _buf=new Buffer(encoded_data);
|
||||
_buf.getInt(); // 0x3f6ff9be
|
||||
_buf.getInt();
|
||||
byte[]_type=_buf.getString();
|
||||
//System.err.println("type: "+new String(_type));
|
||||
byte[] _cipher=_buf.getString();
|
||||
String cipher=Util.byte2str(_cipher);
|
||||
//System.err.println("cipher: "+cipher);
|
||||
if(cipher.equals("3des-cbc")){
|
||||
_buf.getInt();
|
||||
byte[] foo=new byte[encoded_data.length-_buf.getOffSet()];
|
||||
_buf.getByte(foo);
|
||||
encoded_data=foo;
|
||||
encrypted=true;
|
||||
throw new JSchException("unknown privatekey format: "+identity);
|
||||
}
|
||||
else if(cipher.equals("none")){
|
||||
_buf.getInt();
|
||||
//_buf.getInt();
|
||||
|
||||
encrypted=false;
|
||||
|
||||
byte[] foo=new byte[encoded_data.length-_buf.getOffSet()];
|
||||
_buf.getByte(foo);
|
||||
encoded_data=foo;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if(pubkey==null){
|
||||
return;
|
||||
}
|
||||
|
||||
buf=pubkey;
|
||||
len=buf.length;
|
||||
|
||||
if(buf.length>4 && // FSecure's public key
|
||||
buf[0]=='-' && buf[1]=='-' && buf[2]=='-' && buf[3]=='-'){
|
||||
i=0;
|
||||
do{i++;}while(len>i && buf[i]!=0x0a);
|
||||
if(len<=i) return;
|
||||
while(i<len){
|
||||
if(buf[i]==0x0a){
|
||||
boolean inheader=false;
|
||||
for(int j=i+1; j<len; j++){
|
||||
if(buf[j]==0x0a) break;
|
||||
if(buf[j]==':'){inheader=true; break;}
|
||||
}
|
||||
if(!inheader){
|
||||
i++;
|
||||
break;
|
||||
}
|
||||
}
|
||||
i++;
|
||||
}
|
||||
if(len<=i) return;
|
||||
|
||||
start=i;
|
||||
while(i<len){
|
||||
if(buf[i]==0x0a){
|
||||
System.arraycopy(buf, i+1, buf, i, len-i-1);
|
||||
len--;
|
||||
continue;
|
||||
}
|
||||
if(buf[i]=='-'){ break; }
|
||||
i++;
|
||||
}
|
||||
publickeyblob=Util.fromBase64(buf, start, i-start);
|
||||
|
||||
if(type==UNKNOWN && publickeyblob.length>8){
|
||||
if(publickeyblob[8]=='d'){
|
||||
type=DSS;
|
||||
}
|
||||
else if(publickeyblob[8]=='r'){
|
||||
type=RSA;
|
||||
}
|
||||
}
|
||||
}
|
||||
else{
|
||||
if(buf[0]!='s'|| buf[1]!='s'|| buf[2]!='h'|| buf[3]!='-') return;
|
||||
i=0;
|
||||
while(i<len){ if(buf[i]==' ')break; i++;} i++;
|
||||
if(i>=len) return;
|
||||
start=i;
|
||||
while(i<len){ if(buf[i]==' ' || buf[i]=='\n')break; i++;}
|
||||
publickeyblob=Util.fromBase64(buf, start, i-start);
|
||||
if(publickeyblob.length<4+7){ // It must start with "ssh-XXX".
|
||||
if(JSch.getLogger().isEnabled(Logger.WARN)){
|
||||
JSch.getLogger().log(Logger.WARN,
|
||||
"failed to parse the public key");
|
||||
}
|
||||
publickeyblob=null;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch(Exception e){
|
||||
//System.err.println("IdentityFile: "+e);
|
||||
if(e instanceof JSchException) throw (JSchException)e;
|
||||
if(e instanceof Throwable)
|
||||
throw new JSchException(e.toString(), (Throwable)e);
|
||||
throw new JSchException(e.toString());
|
||||
}
|
||||
}
|
||||
|
||||
public String getAlgName(){
|
||||
if(type==RSA) return "ssh-rsa";
|
||||
return "ssh-dss";
|
||||
}
|
||||
|
||||
public boolean setPassphrase(byte[] _passphrase) throws JSchException{
|
||||
/*
|
||||
hash is MD5
|
||||
h(0) <- hash(passphrase, iv);
|
||||
h(n) <- hash(h(n-1), passphrase, iv);
|
||||
key <- (h(0),...,h(n))[0,..,key.length];
|
||||
*/
|
||||
try{
|
||||
if(encrypted){
|
||||
if(_passphrase==null) return false;
|
||||
byte[] passphrase=_passphrase;
|
||||
int hsize=hash.getBlockSize();
|
||||
byte[] hn=new byte[key.length/hsize*hsize+
|
||||
(key.length%hsize==0?0:hsize)];
|
||||
byte[] tmp=null;
|
||||
if(keytype==OPENSSH){
|
||||
for(int index=0; index+hsize<=hn.length;){
|
||||
if(tmp!=null){ hash.update(tmp, 0, tmp.length); }
|
||||
hash.update(passphrase, 0, passphrase.length);
|
||||
hash.update(iv, 0, iv.length > 8 ? 8: iv.length);
|
||||
tmp=hash.digest();
|
||||
System.arraycopy(tmp, 0, hn, index, tmp.length);
|
||||
index+=tmp.length;
|
||||
}
|
||||
System.arraycopy(hn, 0, key, 0, key.length);
|
||||
}
|
||||
else if(keytype==FSECURE){
|
||||
for(int index=0; index+hsize<=hn.length;){
|
||||
if(tmp!=null){ hash.update(tmp, 0, tmp.length); }
|
||||
hash.update(passphrase, 0, passphrase.length);
|
||||
tmp=hash.digest();
|
||||
System.arraycopy(tmp, 0, hn, index, tmp.length);
|
||||
index+=tmp.length;
|
||||
}
|
||||
System.arraycopy(hn, 0, key, 0, key.length);
|
||||
}
|
||||
Util.bzero(passphrase);
|
||||
}
|
||||
if(decrypt()){
|
||||
encrypted=false;
|
||||
return true;
|
||||
}
|
||||
P_array=Q_array=G_array=pub_array=prv_array=null;
|
||||
return false;
|
||||
}
|
||||
catch(Exception e){
|
||||
if(e instanceof JSchException) throw (JSchException)e;
|
||||
if(e instanceof Throwable)
|
||||
throw new JSchException(e.toString(), (Throwable)e);
|
||||
throw new JSchException(e.toString());
|
||||
}
|
||||
}
|
||||
|
||||
public byte[] getPublicKeyBlob(){
|
||||
if(publickeyblob!=null) return publickeyblob;
|
||||
if(type==RSA) return getPublicKeyBlob_rsa();
|
||||
return getPublicKeyBlob_dss();
|
||||
}
|
||||
|
||||
byte[] getPublicKeyBlob_rsa(){
|
||||
if(e_array==null) return null;
|
||||
Buffer buf=new Buffer("ssh-rsa".length()+4+
|
||||
e_array.length+4+
|
||||
n_array.length+4);
|
||||
buf.putString(Util.str2byte("ssh-rsa"));
|
||||
buf.putString(e_array);
|
||||
buf.putString(n_array);
|
||||
return buf.buffer;
|
||||
}
|
||||
|
||||
byte[] getPublicKeyBlob_dss(){
|
||||
if(P_array==null) return null;
|
||||
Buffer buf=new Buffer("ssh-dss".length()+4+
|
||||
P_array.length+4+
|
||||
Q_array.length+4+
|
||||
G_array.length+4+
|
||||
pub_array.length+4);
|
||||
buf.putString(Util.str2byte("ssh-dss"));
|
||||
buf.putString(P_array);
|
||||
buf.putString(Q_array);
|
||||
buf.putString(G_array);
|
||||
buf.putString(pub_array);
|
||||
return buf.buffer;
|
||||
}
|
||||
|
||||
public byte[] getSignature(byte[] data){
|
||||
if(type==RSA) return getSignature_rsa(data);
|
||||
return getSignature_dss(data);
|
||||
}
|
||||
|
||||
byte[] getSignature_rsa(byte[] data){
|
||||
try{
|
||||
Class c=Class.forName((String)jsch.getConfig("signature.rsa"));
|
||||
SignatureRSA rsa=(SignatureRSA)(c.newInstance());
|
||||
|
||||
rsa.init();
|
||||
rsa.setPrvKey(d_array, n_array);
|
||||
|
||||
rsa.update(data);
|
||||
byte[] sig = rsa.sign();
|
||||
Buffer buf=new Buffer("ssh-rsa".length()+4+
|
||||
sig.length+4);
|
||||
buf.putString(Util.str2byte("ssh-rsa"));
|
||||
buf.putString(sig);
|
||||
return buf.buffer;
|
||||
}
|
||||
catch(Exception e){
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
byte[] getSignature_dss(byte[] data){
|
||||
/*
|
||||
byte[] foo;
|
||||
int i;
|
||||
System.err.print("P ");
|
||||
foo=P_array;
|
||||
for(i=0; i<foo.length; i++){
|
||||
System.err.print(Integer.toHexString(foo[i]&0xff)+":");
|
||||
}
|
||||
System.err.println("");
|
||||
System.err.print("Q ");
|
||||
foo=Q_array;
|
||||
for(i=0; i<foo.length; i++){
|
||||
System.err.print(Integer.toHexString(foo[i]&0xff)+":");
|
||||
}
|
||||
System.err.println("");
|
||||
System.err.print("G ");
|
||||
foo=G_array;
|
||||
for(i=0; i<foo.length; i++){
|
||||
System.err.print(Integer.toHexString(foo[i]&0xff)+":");
|
||||
}
|
||||
System.err.println("");
|
||||
*/
|
||||
|
||||
try{
|
||||
Class c=Class.forName((String)jsch.getConfig("signature.dss"));
|
||||
SignatureDSA dsa=(SignatureDSA)(c.newInstance());
|
||||
dsa.init();
|
||||
dsa.setPrvKey(prv_array, P_array, Q_array, G_array);
|
||||
|
||||
dsa.update(data);
|
||||
byte[] sig = dsa.sign();
|
||||
Buffer buf=new Buffer("ssh-dss".length()+4+
|
||||
sig.length+4);
|
||||
buf.putString(Util.str2byte("ssh-dss"));
|
||||
buf.putString(sig);
|
||||
return buf.buffer;
|
||||
}
|
||||
catch(Exception e){
|
||||
//System.err.println("e "+e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public boolean decrypt(){
|
||||
if(type==RSA) return decrypt_rsa();
|
||||
return decrypt_dss();
|
||||
}
|
||||
|
||||
boolean decrypt_rsa(){
|
||||
byte[] p_array;
|
||||
byte[] q_array;
|
||||
byte[] dmp1_array;
|
||||
byte[] dmq1_array;
|
||||
byte[] iqmp_array;
|
||||
|
||||
try{
|
||||
byte[] plain;
|
||||
if(encrypted){
|
||||
if(keytype==OPENSSH){
|
||||
cipher.init(Cipher.DECRYPT_MODE, key, iv);
|
||||
plain=new byte[encoded_data.length];
|
||||
cipher.update(encoded_data, 0, encoded_data.length, plain, 0);
|
||||
}
|
||||
else if(keytype==FSECURE){
|
||||
for(int i=0; i<iv.length; i++)iv[i]=0;
|
||||
cipher.init(Cipher.DECRYPT_MODE, key, iv);
|
||||
plain=new byte[encoded_data.length];
|
||||
cipher.update(encoded_data, 0, encoded_data.length, plain, 0);
|
||||
}
|
||||
else{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else{
|
||||
if(n_array!=null) return true;
|
||||
plain=encoded_data;
|
||||
}
|
||||
|
||||
if(keytype==FSECURE){ // FSecure
|
||||
Buffer buf=new Buffer(plain);
|
||||
int foo=buf.getInt();
|
||||
if(plain.length!=foo+4){
|
||||
return false;
|
||||
}
|
||||
e_array=buf.getMPIntBits();
|
||||
d_array=buf.getMPIntBits();
|
||||
n_array=buf.getMPIntBits();
|
||||
byte[] u_array=buf.getMPIntBits();
|
||||
p_array=buf.getMPIntBits();
|
||||
q_array=buf.getMPIntBits();
|
||||
return true;
|
||||
}
|
||||
|
||||
int index=0;
|
||||
int length=0;
|
||||
|
||||
if(plain[index]!=0x30)return false;
|
||||
index++; // SEQUENCE
|
||||
length=plain[index++]&0xff;
|
||||
if((length&0x80)!=0){
|
||||
int foo=length&0x7f; length=0;
|
||||
while(foo-->0){ length=(length<<8)+(plain[index++]&0xff); }
|
||||
}
|
||||
|
||||
if(plain[index]!=0x02)return false;
|
||||
index++; // INTEGER
|
||||
length=plain[index++]&0xff;
|
||||
if((length&0x80)!=0){
|
||||
int foo=length&0x7f; length=0;
|
||||
while(foo-->0){ length=(length<<8)+(plain[index++]&0xff); }
|
||||
}
|
||||
index+=length;
|
||||
|
||||
//System.err.println("int: len="+length);
|
||||
//System.err.print(Integer.toHexString(plain[index-1]&0xff)+":");
|
||||
//System.err.println("");
|
||||
|
||||
index++;
|
||||
length=plain[index++]&0xff;
|
||||
if((length&0x80)!=0){
|
||||
int foo=length&0x7f; length=0;
|
||||
while(foo-->0){ length=(length<<8)+(plain[index++]&0xff); }
|
||||
}
|
||||
n_array=new byte[length];
|
||||
System.arraycopy(plain, index, n_array, 0, length);
|
||||
index+=length;
|
||||
/*
|
||||
System.err.println("int: N len="+length);
|
||||
for(int i=0; i<n_array.length; i++){
|
||||
System.err.print(Integer.toHexString(n_array[i]&0xff)+":");
|
||||
}
|
||||
System.err.println("");
|
||||
*/
|
||||
index++;
|
||||
length=plain[index++]&0xff;
|
||||
if((length&0x80)!=0){
|
||||
int foo=length&0x7f; length=0;
|
||||
while(foo-->0){ length=(length<<8)+(plain[index++]&0xff); }
|
||||
}
|
||||
e_array=new byte[length];
|
||||
System.arraycopy(plain, index, e_array, 0, length);
|
||||
index+=length;
|
||||
/*
|
||||
System.err.println("int: E len="+length);
|
||||
for(int i=0; i<e_array.length; i++){
|
||||
System.err.print(Integer.toHexString(e_array[i]&0xff)+":");
|
||||
}
|
||||
System.err.println("");
|
||||
*/
|
||||
index++;
|
||||
length=plain[index++]&0xff;
|
||||
if((length&0x80)!=0){
|
||||
int foo=length&0x7f; length=0;
|
||||
while(foo-->0){ length=(length<<8)+(plain[index++]&0xff); }
|
||||
}
|
||||
d_array=new byte[length];
|
||||
System.arraycopy(plain, index, d_array, 0, length);
|
||||
index+=length;
|
||||
/*
|
||||
System.err.println("int: D len="+length);
|
||||
for(int i=0; i<d_array.length; i++){
|
||||
System.err.print(Integer.toHexString(d_array[i]&0xff)+":");
|
||||
}
|
||||
System.err.println("");
|
||||
*/
|
||||
|
||||
index++;
|
||||
length=plain[index++]&0xff;
|
||||
if((length&0x80)!=0){
|
||||
int foo=length&0x7f; length=0;
|
||||
while(foo-->0){ length=(length<<8)+(plain[index++]&0xff); }
|
||||
}
|
||||
p_array=new byte[length];
|
||||
System.arraycopy(plain, index, p_array, 0, length);
|
||||
index+=length;
|
||||
/*
|
||||
System.err.println("int: P len="+length);
|
||||
for(int i=0; i<p_array.length; i++){
|
||||
System.err.print(Integer.toHexString(p_array[i]&0xff)+":");
|
||||
}
|
||||
System.err.println("");
|
||||
*/
|
||||
index++;
|
||||
length=plain[index++]&0xff;
|
||||
if((length&0x80)!=0){
|
||||
int foo=length&0x7f; length=0;
|
||||
while(foo-->0){ length=(length<<8)+(plain[index++]&0xff); }
|
||||
}
|
||||
q_array=new byte[length];
|
||||
System.arraycopy(plain, index, q_array, 0, length);
|
||||
index+=length;
|
||||
/*
|
||||
System.err.println("int: q len="+length);
|
||||
for(int i=0; i<q_array.length; i++){
|
||||
System.err.print(Integer.toHexString(q_array[i]&0xff)+":");
|
||||
}
|
||||
System.err.println("");
|
||||
*/
|
||||
index++;
|
||||
length=plain[index++]&0xff;
|
||||
if((length&0x80)!=0){
|
||||
int foo=length&0x7f; length=0;
|
||||
while(foo-->0){ length=(length<<8)+(plain[index++]&0xff); }
|
||||
}
|
||||
dmp1_array=new byte[length];
|
||||
System.arraycopy(plain, index, dmp1_array, 0, length);
|
||||
index+=length;
|
||||
/*
|
||||
System.err.println("int: dmp1 len="+length);
|
||||
for(int i=0; i<dmp1_array.length; i++){
|
||||
System.err.print(Integer.toHexString(dmp1_array[i]&0xff)+":");
|
||||
}
|
||||
System.err.println("");
|
||||
*/
|
||||
index++;
|
||||
length=plain[index++]&0xff;
|
||||
if((length&0x80)!=0){
|
||||
int foo=length&0x7f; length=0;
|
||||
while(foo-->0){ length=(length<<8)+(plain[index++]&0xff); }
|
||||
}
|
||||
dmq1_array=new byte[length];
|
||||
System.arraycopy(plain, index, dmq1_array, 0, length);
|
||||
index+=length;
|
||||
/*
|
||||
System.err.println("int: dmq1 len="+length);
|
||||
for(int i=0; i<dmq1_array.length; i++){
|
||||
System.err.print(Integer.toHexString(dmq1_array[i]&0xff)+":");
|
||||
}
|
||||
System.err.println("");
|
||||
*/
|
||||
index++;
|
||||
length=plain[index++]&0xff;
|
||||
if((length&0x80)!=0){
|
||||
int foo=length&0x7f; length=0;
|
||||
while(foo-->0){ length=(length<<8)+(plain[index++]&0xff); }
|
||||
}
|
||||
iqmp_array=new byte[length];
|
||||
System.arraycopy(plain, index, iqmp_array, 0, length);
|
||||
index+=length;
|
||||
/*
|
||||
System.err.println("int: iqmp len="+length);
|
||||
for(int i=0; i<iqmp_array.length; i++){
|
||||
System.err.print(Integer.toHexString(iqmp_array[i]&0xff)+":");
|
||||
}
|
||||
System.err.println("");
|
||||
*/
|
||||
}
|
||||
catch(Exception e){
|
||||
//System.err.println(e);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
boolean decrypt_dss(){
|
||||
try{
|
||||
byte[] plain;
|
||||
if(encrypted){
|
||||
if(keytype==OPENSSH){
|
||||
cipher.init(Cipher.DECRYPT_MODE, key, iv);
|
||||
plain=new byte[encoded_data.length];
|
||||
cipher.update(encoded_data, 0, encoded_data.length, plain, 0);
|
||||
/*
|
||||
for(int i=0; i<plain.length; i++){
|
||||
System.err.print(Integer.toHexString(plain[i]&0xff)+":");
|
||||
}
|
||||
System.err.println("");
|
||||
*/
|
||||
}
|
||||
else if(keytype==FSECURE){
|
||||
for(int i=0; i<iv.length; i++)iv[i]=0;
|
||||
cipher.init(Cipher.DECRYPT_MODE, key, iv);
|
||||
plain=new byte[encoded_data.length];
|
||||
cipher.update(encoded_data, 0, encoded_data.length, plain, 0);
|
||||
}
|
||||
else{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else{
|
||||
if(P_array!=null) return true;
|
||||
plain=encoded_data;
|
||||
}
|
||||
|
||||
if(keytype==FSECURE){ // FSecure
|
||||
Buffer buf=new Buffer(plain);
|
||||
int foo=buf.getInt();
|
||||
if(plain.length!=foo+4){
|
||||
return false;
|
||||
}
|
||||
P_array=buf.getMPIntBits();
|
||||
G_array=buf.getMPIntBits();
|
||||
Q_array=buf.getMPIntBits();
|
||||
pub_array=buf.getMPIntBits();
|
||||
prv_array=buf.getMPIntBits();
|
||||
return true;
|
||||
}
|
||||
|
||||
int index=0;
|
||||
int length=0;
|
||||
if(plain[index]!=0x30)return false;
|
||||
index++; // SEQUENCE
|
||||
length=plain[index++]&0xff;
|
||||
if((length&0x80)!=0){
|
||||
int foo=length&0x7f; length=0;
|
||||
while(foo-->0){ length=(length<<8)+(plain[index++]&0xff); }
|
||||
}
|
||||
if(plain[index]!=0x02)return false;
|
||||
index++; // INTEGER
|
||||
length=plain[index++]&0xff;
|
||||
if((length&0x80)!=0){
|
||||
int foo=length&0x7f; length=0;
|
||||
while(foo-->0){ length=(length<<8)+(plain[index++]&0xff); }
|
||||
}
|
||||
index+=length;
|
||||
|
||||
index++;
|
||||
length=plain[index++]&0xff;
|
||||
if((length&0x80)!=0){
|
||||
int foo=length&0x7f; length=0;
|
||||
while(foo-->0){ length=(length<<8)+(plain[index++]&0xff); }
|
||||
}
|
||||
P_array=new byte[length];
|
||||
System.arraycopy(plain, index, P_array, 0, length);
|
||||
index+=length;
|
||||
|
||||
index++;
|
||||
length=plain[index++]&0xff;
|
||||
if((length&0x80)!=0){
|
||||
int foo=length&0x7f; length=0;
|
||||
while(foo-->0){ length=(length<<8)+(plain[index++]&0xff); }
|
||||
}
|
||||
Q_array=new byte[length];
|
||||
System.arraycopy(plain, index, Q_array, 0, length);
|
||||
index+=length;
|
||||
|
||||
index++;
|
||||
length=plain[index++]&0xff;
|
||||
if((length&0x80)!=0){
|
||||
int foo=length&0x7f; length=0;
|
||||
while(foo-->0){ length=(length<<8)+(plain[index++]&0xff); }
|
||||
}
|
||||
G_array=new byte[length];
|
||||
System.arraycopy(plain, index, G_array, 0, length);
|
||||
index+=length;
|
||||
|
||||
index++;
|
||||
length=plain[index++]&0xff;
|
||||
if((length&0x80)!=0){
|
||||
int foo=length&0x7f; length=0;
|
||||
while(foo-->0){ length=(length<<8)+(plain[index++]&0xff); }
|
||||
}
|
||||
pub_array=new byte[length];
|
||||
System.arraycopy(plain, index, pub_array, 0, length);
|
||||
index+=length;
|
||||
|
||||
index++;
|
||||
length=plain[index++]&0xff;
|
||||
if((length&0x80)!=0){
|
||||
int foo=length&0x7f; length=0;
|
||||
while(foo-->0){ length=(length<<8)+(plain[index++]&0xff); }
|
||||
}
|
||||
prv_array=new byte[length];
|
||||
System.arraycopy(plain, index, prv_array, 0, length);
|
||||
index+=length;
|
||||
}
|
||||
catch(Exception e){
|
||||
//System.err.println(e);
|
||||
//e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean isEncrypted(){
|
||||
return encrypted;
|
||||
}
|
||||
|
||||
public String getName(){
|
||||
return identity;
|
||||
}
|
||||
|
||||
private byte a2b(byte c){
|
||||
if('0'<=c&&c<='9') return (byte)(c-'0');
|
||||
if('a'<=c&&c<='z') return (byte)(c-'a'+10);
|
||||
return (byte)(c-'A'+10);
|
||||
}
|
||||
|
||||
public boolean equals(Object o){
|
||||
if(!(o instanceof IdentityFile)) return super.equals(o);
|
||||
IdentityFile foo=(IdentityFile)o;
|
||||
return getName().equals(foo.getName());
|
||||
}
|
||||
|
||||
public void clear(){
|
||||
Util.bzero(encoded_data);
|
||||
Util.bzero(prv_array);
|
||||
Util.bzero(d_array);
|
||||
Util.bzero(key);
|
||||
Util.bzero(iv);
|
||||
}
|
||||
|
||||
public void finalize (){
|
||||
clear();
|
||||
}
|
||||
}
|
@ -1,301 +0,0 @@
|
||||
/* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
|
||||
/*
|
||||
Copyright (c) 2002-2011 ymnk, JCraft,Inc. All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice,
|
||||
this list of conditions and the following disclaimer.
|
||||
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the distribution.
|
||||
|
||||
3. The names of the authors may not be used to endorse or promote products
|
||||
derived from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
|
||||
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
|
||||
INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
|
||||
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
||||
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
package com.jcraft.jsch;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.util.Vector;
|
||||
|
||||
public class JSch{
|
||||
static java.util.Hashtable config=new java.util.Hashtable();
|
||||
static{
|
||||
// config.put("kex", "diffie-hellman-group-exchange-sha1");
|
||||
config.put("kex", "diffie-hellman-group1-sha1,diffie-hellman-group14-sha1,diffie-hellman-group-exchange-sha1");
|
||||
config.put("server_host_key", "ssh-rsa,ssh-dss");
|
||||
// config.put("server_host_key", "ssh-dss,ssh-rsa");
|
||||
|
||||
config.put("cipher.s2c",
|
||||
"aes128-ctr,aes128-cbc,3des-ctr,3des-cbc,blowfish-cbc,aes192-cbc,aes256-cbc");
|
||||
config.put("cipher.c2s",
|
||||
"aes128-ctr,aes128-cbc,3des-ctr,3des-cbc,blowfish-cbc,aes192-cbc,aes256-cbc");
|
||||
|
||||
config.put("mac.s2c", "hmac-md5,hmac-sha1,hmac-sha1-96,hmac-md5-96");
|
||||
config.put("mac.c2s", "hmac-md5,hmac-sha1,hmac-sha1-96,hmac-md5-96");
|
||||
config.put("compression.s2c", "none");
|
||||
// config.put("compression.s2c", "zlib@openssh.com,zlib,none");
|
||||
config.put("compression.c2s", "none");
|
||||
// config.put("compression.c2s", "zlib@openssh.com,zlib,none");
|
||||
|
||||
config.put("lang.s2c", "");
|
||||
config.put("lang.c2s", "");
|
||||
|
||||
config.put("compression_level", "6");
|
||||
|
||||
config.put("diffie-hellman-group-exchange-sha1",
|
||||
"com.jcraft.jsch.DHGEX");
|
||||
config.put("diffie-hellman-group1-sha1",
|
||||
"com.jcraft.jsch.DHG1");
|
||||
config.put("diffie-hellman-group14-sha1",
|
||||
"com.jcraft.jsch.DHG14");
|
||||
|
||||
config.put("dh", "com.jcraft.jsch.jce.DH");
|
||||
config.put("3des-cbc", "com.jcraft.jsch.jce.TripleDESCBC");
|
||||
config.put("blowfish-cbc", "com.jcraft.jsch.jce.BlowfishCBC");
|
||||
config.put("hmac-sha1", "com.jcraft.jsch.jce.HMACSHA1");
|
||||
config.put("hmac-sha1-96", "com.jcraft.jsch.jce.HMACSHA196");
|
||||
config.put("hmac-md5", "com.jcraft.jsch.jce.HMACMD5");
|
||||
config.put("hmac-md5-96", "com.jcraft.jsch.jce.HMACMD596");
|
||||
config.put("sha-1", "com.jcraft.jsch.jce.SHA1");
|
||||
config.put("md5", "com.jcraft.jsch.jce.MD5");
|
||||
config.put("signature.dss", "com.jcraft.jsch.jce.SignatureDSA");
|
||||
config.put("signature.rsa", "com.jcraft.jsch.jce.SignatureRSA");
|
||||
config.put("keypairgen.dsa", "com.jcraft.jsch.jce.KeyPairGenDSA");
|
||||
config.put("keypairgen.rsa", "com.jcraft.jsch.jce.KeyPairGenRSA");
|
||||
config.put("random", "com.jcraft.jsch.jce.Random");
|
||||
|
||||
config.put("none", "com.jcraft.jsch.CipherNone");
|
||||
|
||||
config.put("aes128-cbc", "com.jcraft.jsch.jce.AES128CBC");
|
||||
config.put("aes192-cbc", "com.jcraft.jsch.jce.AES192CBC");
|
||||
config.put("aes256-cbc", "com.jcraft.jsch.jce.AES256CBC");
|
||||
|
||||
config.put("aes128-ctr", "com.jcraft.jsch.jce.AES128CTR");
|
||||
config.put("aes192-ctr", "com.jcraft.jsch.jce.AES192CTR");
|
||||
config.put("aes256-ctr", "com.jcraft.jsch.jce.AES256CTR");
|
||||
config.put("3des-ctr", "com.jcraft.jsch.jce.TripleDESCTR");
|
||||
config.put("arcfour", "com.jcraft.jsch.jce.ARCFOUR");
|
||||
config.put("arcfour128", "com.jcraft.jsch.jce.ARCFOUR128");
|
||||
config.put("arcfour256", "com.jcraft.jsch.jce.ARCFOUR256");
|
||||
|
||||
config.put("userauth.none", "com.jcraft.jsch.UserAuthNone");
|
||||
config.put("userauth.password", "com.jcraft.jsch.UserAuthPassword");
|
||||
config.put("userauth.keyboard-interactive", "com.jcraft.jsch.UserAuthKeyboardInteractive");
|
||||
config.put("userauth.publickey", "com.jcraft.jsch.UserAuthPublicKey");
|
||||
config.put("userauth.gssapi-with-mic", "com.jcraft.jsch.UserAuthGSSAPIWithMIC");
|
||||
config.put("gssapi-with-mic.krb5", "com.jcraft.jsch.jgss.GSSContextKrb5");
|
||||
|
||||
config.put("zlib", "com.jcraft.jsch.jcraft.Compression");
|
||||
config.put("zlib@openssh.com", "com.jcraft.jsch.jcraft.Compression");
|
||||
|
||||
config.put("StrictHostKeyChecking", "ask");
|
||||
config.put("HashKnownHosts", "no");
|
||||
//config.put("HashKnownHosts", "yes");
|
||||
config.put("PreferredAuthentications", "gssapi-with-mic,publickey,keyboard-interactive,password");
|
||||
|
||||
config.put("CheckCiphers", "aes256-ctr,aes192-ctr,aes128-ctr,aes256-cbc,aes192-cbc,aes128-cbc,3des-ctr,arcfour,arcfour128,arcfour256");
|
||||
config.put("CheckKexes", "diffie-hellman-group14-sha1");
|
||||
}
|
||||
java.util.Vector pool=new java.util.Vector();
|
||||
java.util.Vector identities=new java.util.Vector();
|
||||
private HostKeyRepository known_hosts=null;
|
||||
|
||||
private static final Logger DEVNULL=new Logger(){
|
||||
public boolean isEnabled(int level){return false;}
|
||||
public void log(int level, String message){}
|
||||
};
|
||||
static Logger logger=DEVNULL;
|
||||
|
||||
public JSch(){
|
||||
|
||||
try{
|
||||
String osname=(String)(System.getProperties().get("os.name"));
|
||||
if(osname!=null && osname.equals("Mac OS X")){
|
||||
config.put("hmac-sha1", "com.jcraft.jsch.jcraft.HMACSHA1");
|
||||
config.put("hmac-md5", "com.jcraft.jsch.jcraft.HMACMD5");
|
||||
config.put("hmac-md5-96", "com.jcraft.jsch.jcraft.HMACMD596");
|
||||
config.put("hmac-sha1-96", "com.jcraft.jsch.jcraft.HMACSHA196");
|
||||
}
|
||||
}
|
||||
catch(Exception e){
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public Session getSession(String username, String host) throws JSchException { return getSession(username, host, 22); }
|
||||
public Session getSession(String username, String host, int port) throws JSchException {
|
||||
if(username==null){
|
||||
throw new JSchException("username must not be null.");
|
||||
}
|
||||
if(host==null){
|
||||
throw new JSchException("host must not be null.");
|
||||
}
|
||||
Session s=new Session(this);
|
||||
s.setUserName(username);
|
||||
s.setHost(host);
|
||||
s.setPort(port);
|
||||
//pool.addElement(s);
|
||||
return s;
|
||||
}
|
||||
|
||||
protected void addSession(Session session){
|
||||
synchronized(pool){
|
||||
pool.addElement(session);
|
||||
}
|
||||
}
|
||||
|
||||
protected boolean removeSession(Session session){
|
||||
synchronized(pool){
|
||||
return pool.remove(session);
|
||||
}
|
||||
}
|
||||
public void setHostKeyRepository(HostKeyRepository hkrepo){
|
||||
known_hosts=hkrepo;
|
||||
}
|
||||
|
||||
public void setKnownHosts(String filename) throws JSchException{
|
||||
if(known_hosts==null) known_hosts=new KnownHosts(this);
|
||||
if(known_hosts instanceof KnownHosts){
|
||||
synchronized(known_hosts){
|
||||
((KnownHosts)known_hosts).setKnownHosts(filename);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void setKnownHosts(InputStream stream) throws JSchException{
|
||||
if(known_hosts==null) known_hosts=new KnownHosts(this);
|
||||
if(known_hosts instanceof KnownHosts){
|
||||
synchronized(known_hosts){
|
||||
((KnownHosts)known_hosts).setKnownHosts(stream);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public HostKeyRepository getHostKeyRepository(){
|
||||
if(known_hosts==null) known_hosts=new KnownHosts(this);
|
||||
return known_hosts;
|
||||
}
|
||||
|
||||
public void addIdentity(String prvkey) throws JSchException{
|
||||
addIdentity(prvkey, (byte[])null);
|
||||
}
|
||||
|
||||
public void addIdentity(String prvkey, String passphrase) throws JSchException{
|
||||
byte[] _passphrase=null;
|
||||
if(passphrase!=null){
|
||||
_passphrase=Util.str2byte(passphrase);
|
||||
}
|
||||
addIdentity(prvkey, _passphrase);
|
||||
if(_passphrase!=null)
|
||||
Util.bzero(_passphrase);
|
||||
}
|
||||
|
||||
public void addIdentity(String prvkey, byte[] passphrase) throws JSchException{
|
||||
Identity identity=IdentityFile.newInstance(prvkey, null, this);
|
||||
addIdentity(identity, passphrase);
|
||||
}
|
||||
public void addIdentity(String prvkey, String pubkey, byte[] passphrase) throws JSchException{
|
||||
Identity identity=IdentityFile.newInstance(prvkey, pubkey, this);
|
||||
addIdentity(identity, passphrase);
|
||||
}
|
||||
|
||||
public void addIdentity(String name, byte[]prvkey, byte[]pubkey, byte[] passphrase) throws JSchException{
|
||||
Identity identity=IdentityFile.newInstance(name, prvkey, pubkey, this);
|
||||
addIdentity(identity, passphrase);
|
||||
}
|
||||
|
||||
public void addIdentity(Identity identity, byte[] passphrase) throws JSchException{
|
||||
if(passphrase!=null){
|
||||
try{
|
||||
byte[] goo=new byte[passphrase.length];
|
||||
System.arraycopy(passphrase, 0, goo, 0, passphrase.length);
|
||||
passphrase=goo;
|
||||
identity.setPassphrase(passphrase);
|
||||
}
|
||||
finally{
|
||||
Util.bzero(passphrase);
|
||||
}
|
||||
}
|
||||
synchronized(identities){
|
||||
if(!identities.contains(identity)){
|
||||
identities.addElement(identity);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void removeIdentity(String name) throws JSchException{
|
||||
synchronized(identities){
|
||||
for(int i=0; i<identities.size(); i++){
|
||||
Identity identity=(Identity)(identities.elementAt(i));
|
||||
if(!identity.getName().equals(name))
|
||||
continue;
|
||||
identities.removeElement(identity);
|
||||
identity.clear();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Vector getIdentityNames() throws JSchException{
|
||||
Vector foo=new Vector();
|
||||
synchronized(identities){
|
||||
for(int i=0; i<identities.size(); i++){
|
||||
Identity identity=(Identity)(identities.elementAt(i));
|
||||
foo.addElement(identity.getName());
|
||||
}
|
||||
}
|
||||
return foo;
|
||||
}
|
||||
|
||||
public void removeAllIdentity() throws JSchException{
|
||||
synchronized(identities){
|
||||
Vector foo=getIdentityNames();
|
||||
for(int i=0; i<foo.size(); i++){
|
||||
String name=((String)foo.elementAt(i));
|
||||
removeIdentity(name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static String getConfig(String key){
|
||||
synchronized(config){
|
||||
return (String)(config.get(key));
|
||||
}
|
||||
}
|
||||
|
||||
public static void setConfig(java.util.Hashtable newconf){
|
||||
synchronized(config){
|
||||
for(java.util.Enumeration e=newconf.keys() ; e.hasMoreElements() ;) {
|
||||
String key=(String)(e.nextElement());
|
||||
config.put(key, (String)(newconf.get(key)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void setConfig(String key, String value){
|
||||
config.put(key, value);
|
||||
}
|
||||
|
||||
public static void setLogger(Logger logger){
|
||||
if(logger==null) logger=DEVNULL;
|
||||
JSch.logger=logger;
|
||||
}
|
||||
static Logger getLogger(){
|
||||
return logger;
|
||||
}
|
||||
}
|
@ -1,45 +0,0 @@
|
||||
/* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
|
||||
/*
|
||||
Copyright (c) 2002-2011 ymnk, JCraft,Inc. All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice,
|
||||
this list of conditions and the following disclaimer.
|
||||
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the distribution.
|
||||
|
||||
3. The names of the authors may not be used to endorse or promote products
|
||||
derived from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
|
||||
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
|
||||
INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
|
||||
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
||||
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
package com.jcraft.jsch;
|
||||
|
||||
class JSchAuthCancelException extends JSchException{
|
||||
//private static final long serialVersionUID=3204965907117900987L;
|
||||
String method;
|
||||
JSchAuthCancelException () {
|
||||
super();
|
||||
}
|
||||
JSchAuthCancelException (String s) {
|
||||
super(s);
|
||||
this.method=s;
|
||||
}
|
||||
public String getMethod(){
|
||||
return method;
|
||||
}
|
||||
}
|
@ -1,48 +0,0 @@
|
||||
/* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
|
||||
/*
|
||||
Copyright (c) 2002-2011 ymnk, JCraft,Inc. All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice,
|
||||
this list of conditions and the following disclaimer.
|
||||
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the distribution.
|
||||
|
||||
3. The names of the authors may not be used to endorse or promote products
|
||||
derived from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
|
||||
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
|
||||
INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
|
||||
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
||||
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
package com.jcraft.jsch;
|
||||
|
||||
public class JSchException extends Exception{
|
||||
//private static final long serialVersionUID=-1319309923966731989L;
|
||||
private Throwable cause=null;
|
||||
public JSchException () {
|
||||
super();
|
||||
}
|
||||
public JSchException (String s) {
|
||||
super(s);
|
||||
}
|
||||
public JSchException (String s, Throwable e) {
|
||||
super(s);
|
||||
this.cause=e;
|
||||
}
|
||||
public Throwable getCause(){
|
||||
return this.cause;
|
||||
}
|
||||
}
|
@ -1,45 +0,0 @@
|
||||
/* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
|
||||
/*
|
||||
Copyright (c) 2002-2011 ymnk, JCraft,Inc. All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice,
|
||||
this list of conditions and the following disclaimer.
|
||||
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the distribution.
|
||||
|
||||
3. The names of the authors may not be used to endorse or promote products
|
||||
derived from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
|
||||
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
|
||||
INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
|
||||
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
||||
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
package com.jcraft.jsch;
|
||||
|
||||
class JSchPartialAuthException extends JSchException{
|
||||
//private static final long serialVersionUID=-378849862323360367L;
|
||||
String methods;
|
||||
public JSchPartialAuthException () {
|
||||
super();
|
||||
}
|
||||
public JSchPartialAuthException (String s) {
|
||||
super(s);
|
||||
this.methods=s;
|
||||
}
|
||||
public String getMethods(){
|
||||
return methods;
|
||||
}
|
||||
}
|
@ -1,159 +0,0 @@
|
||||
/* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
|
||||
/*
|
||||
Copyright (c) 2002-2011 ymnk, JCraft,Inc. All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice,
|
||||
this list of conditions and the following disclaimer.
|
||||
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the distribution.
|
||||
|
||||
3. The names of the authors may not be used to endorse or promote products
|
||||
derived from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
|
||||
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
|
||||
INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
|
||||
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
||||
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
package com.jcraft.jsch;
|
||||
|
||||
public abstract class KeyExchange{
|
||||
|
||||
static final int PROPOSAL_KEX_ALGS=0;
|
||||
static final int PROPOSAL_SERVER_HOST_KEY_ALGS=1;
|
||||
static final int PROPOSAL_ENC_ALGS_CTOS=2;
|
||||
static final int PROPOSAL_ENC_ALGS_STOC=3;
|
||||
static final int PROPOSAL_MAC_ALGS_CTOS=4;
|
||||
static final int PROPOSAL_MAC_ALGS_STOC=5;
|
||||
static final int PROPOSAL_COMP_ALGS_CTOS=6;
|
||||
static final int PROPOSAL_COMP_ALGS_STOC=7;
|
||||
static final int PROPOSAL_LANG_CTOS=8;
|
||||
static final int PROPOSAL_LANG_STOC=9;
|
||||
static final int PROPOSAL_MAX=10;
|
||||
|
||||
//static String kex_algs="diffie-hellman-group-exchange-sha1"+
|
||||
// ",diffie-hellman-group1-sha1";
|
||||
|
||||
//static String kex="diffie-hellman-group-exchange-sha1";
|
||||
static String kex="diffie-hellman-group1-sha1";
|
||||
static String server_host_key="ssh-rsa,ssh-dss";
|
||||
static String enc_c2s="blowfish-cbc";
|
||||
static String enc_s2c="blowfish-cbc";
|
||||
static String mac_c2s="hmac-md5"; // hmac-md5,hmac-sha1,hmac-ripemd160,
|
||||
// hmac-sha1-96,hmac-md5-96
|
||||
static String mac_s2c="hmac-md5";
|
||||
//static String comp_c2s="none"; // zlib
|
||||
//static String comp_s2c="none";
|
||||
static String lang_c2s="";
|
||||
static String lang_s2c="";
|
||||
|
||||
public static final int STATE_END=0;
|
||||
|
||||
protected Session session=null;
|
||||
protected HASH sha=null;
|
||||
protected byte[] K=null;
|
||||
protected byte[] H=null;
|
||||
protected byte[] K_S=null;
|
||||
|
||||
public abstract void init(Session session,
|
||||
byte[] V_S, byte[] V_C, byte[] I_S, byte[] I_C) throws Exception;
|
||||
public abstract boolean next(Buffer buf) throws Exception;
|
||||
public abstract String getKeyType();
|
||||
public abstract int getState();
|
||||
|
||||
/*
|
||||
void dump(byte[] foo){
|
||||
for(int i=0; i<foo.length; i++){
|
||||
if((foo[i]&0xf0)==0)System.err.print("0");
|
||||
System.err.print(Integer.toHexString(foo[i]&0xff));
|
||||
if(i%16==15){System.err.println(""); continue;}
|
||||
if(i%2==1)System.err.print(" ");
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
protected static String[] guess(byte[]I_S, byte[]I_C){
|
||||
String[] guess=new String[PROPOSAL_MAX];
|
||||
Buffer sb=new Buffer(I_S); sb.setOffSet(17);
|
||||
Buffer cb=new Buffer(I_C); cb.setOffSet(17);
|
||||
|
||||
for(int i=0; i<PROPOSAL_MAX; i++){
|
||||
byte[] sp=sb.getString(); // server proposal
|
||||
byte[] cp=cb.getString(); // client proposal
|
||||
int j=0;
|
||||
int k=0;
|
||||
|
||||
loop:
|
||||
while(j<cp.length){
|
||||
while(j<cp.length && cp[j]!=',')j++;
|
||||
if(k==j) return null;
|
||||
String algorithm=Util.byte2str(cp, k, j-k);
|
||||
int l=0;
|
||||
int m=0;
|
||||
while(l<sp.length){
|
||||
while(l<sp.length && sp[l]!=',')l++;
|
||||
if(m==l) return null;
|
||||
if(algorithm.equals(Util.byte2str(sp, m, l-m))){
|
||||
guess[i]=algorithm;
|
||||
break loop;
|
||||
}
|
||||
l++;
|
||||
m=l;
|
||||
}
|
||||
j++;
|
||||
k=j;
|
||||
}
|
||||
if(j==0){
|
||||
guess[i]="";
|
||||
}
|
||||
else if(guess[i]==null){
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
if(JSch.getLogger().isEnabled(Logger.INFO)){
|
||||
JSch.getLogger().log(Logger.INFO,
|
||||
"kex: server->client"+
|
||||
" "+guess[PROPOSAL_ENC_ALGS_STOC]+
|
||||
" "+guess[PROPOSAL_MAC_ALGS_STOC]+
|
||||
" "+guess[PROPOSAL_COMP_ALGS_STOC]);
|
||||
JSch.getLogger().log(Logger.INFO,
|
||||
"kex: client->server"+
|
||||
" "+guess[PROPOSAL_ENC_ALGS_CTOS]+
|
||||
" "+guess[PROPOSAL_MAC_ALGS_CTOS]+
|
||||
" "+guess[PROPOSAL_COMP_ALGS_CTOS]);
|
||||
}
|
||||
|
||||
// for(int i=0; i<PROPOSAL_MAX; i++){
|
||||
// System.err.println("guess: ["+guess[i]+"]");
|
||||
// }
|
||||
|
||||
return guess;
|
||||
}
|
||||
|
||||
public String getFingerPrint(){
|
||||
HASH hash=null;
|
||||
try{
|
||||
Class c=Class.forName(session.getConfig("md5"));
|
||||
hash=(HASH)(c.newInstance());
|
||||
}
|
||||
catch(Exception e){ System.err.println("getFingerPrint: "+e); }
|
||||
return Util.getFingerPrint(hash, getHostKey());
|
||||
}
|
||||
byte[] getK(){ return K; }
|
||||
byte[] getH(){ return H; }
|
||||
HASH getHash(){ return sha; }
|
||||
byte[] getHostKey(){ return K_S; }
|
||||
}
|
@ -1,729 +0,0 @@
|
||||
/* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
|
||||
/*
|
||||
Copyright (c) 2002-2011 ymnk, JCraft,Inc. All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice,
|
||||
this list of conditions and the following disclaimer.
|
||||
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the distribution.
|
||||
|
||||
3. The names of the authors may not be used to endorse or promote products
|
||||
derived from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
|
||||
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
|
||||
INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
|
||||
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
||||
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
package com.jcraft.jsch;
|
||||
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.File;
|
||||
|
||||
public abstract class KeyPair{
|
||||
public static final int ERROR=0;
|
||||
public static final int DSA=1;
|
||||
public static final int RSA=2;
|
||||
public static final int UNKNOWN=3;
|
||||
|
||||
static final int VENDOR_OPENSSH=0;
|
||||
static final int VENDOR_FSECURE=1;
|
||||
int vendor=VENDOR_OPENSSH;
|
||||
|
||||
private static final byte[] cr=Util.str2byte("\n");
|
||||
|
||||
public static KeyPair genKeyPair(JSch jsch, int type) throws JSchException{
|
||||
return genKeyPair(jsch, type, 1024);
|
||||
}
|
||||
public static KeyPair genKeyPair(JSch jsch, int type, int key_size) throws JSchException{
|
||||
KeyPair kpair=null;
|
||||
if(type==DSA){ kpair=new KeyPairDSA(jsch); }
|
||||
else if(type==RSA){ kpair=new KeyPairRSA(jsch); }
|
||||
if(kpair!=null){
|
||||
kpair.generate(key_size);
|
||||
}
|
||||
return kpair;
|
||||
}
|
||||
|
||||
abstract void generate(int key_size) throws JSchException;
|
||||
|
||||
abstract byte[] getBegin();
|
||||
abstract byte[] getEnd();
|
||||
abstract int getKeySize();
|
||||
|
||||
public String getPublicKeyComment(){
|
||||
return publicKeyComment;
|
||||
}
|
||||
private String publicKeyComment = "";
|
||||
|
||||
JSch jsch=null;
|
||||
private Cipher cipher;
|
||||
private HASH hash;
|
||||
private Random random;
|
||||
|
||||
private byte[] passphrase;
|
||||
|
||||
public KeyPair(JSch jsch){
|
||||
this.jsch=jsch;
|
||||
}
|
||||
|
||||
static byte[][] header={Util.str2byte("Proc-Type: 4,ENCRYPTED"),
|
||||
Util.str2byte("DEK-Info: DES-EDE3-CBC,")};
|
||||
|
||||
abstract byte[] getPrivateKey();
|
||||
|
||||
public void writePrivateKey(java.io.OutputStream out){
|
||||
byte[] plain=getPrivateKey();
|
||||
byte[][] _iv=new byte[1][];
|
||||
byte[] encoded=encrypt(plain, _iv);
|
||||
if(encoded!=plain)
|
||||
Util.bzero(plain);
|
||||
byte[] iv=_iv[0];
|
||||
byte[] prv=Util.toBase64(encoded, 0, encoded.length);
|
||||
|
||||
try{
|
||||
out.write(getBegin()); out.write(cr);
|
||||
if(passphrase!=null){
|
||||
out.write(header[0]); out.write(cr);
|
||||
out.write(header[1]);
|
||||
for(int i=0; i<iv.length; i++){
|
||||
out.write(b2a((byte)((iv[i]>>>4)&0x0f)));
|
||||
out.write(b2a((byte)(iv[i]&0x0f)));
|
||||
}
|
||||
out.write(cr);
|
||||
out.write(cr);
|
||||
}
|
||||
int i=0;
|
||||
while(i<prv.length){
|
||||
if(i+64<prv.length){
|
||||
out.write(prv, i, 64);
|
||||
out.write(cr);
|
||||
i+=64;
|
||||
continue;
|
||||
}
|
||||
out.write(prv, i, prv.length-i);
|
||||
out.write(cr);
|
||||
break;
|
||||
}
|
||||
out.write(getEnd()); out.write(cr);
|
||||
//out.close();
|
||||
}
|
||||
catch(Exception e){
|
||||
}
|
||||
}
|
||||
|
||||
private static byte[] space=Util.str2byte(" ");
|
||||
|
||||
abstract byte[] getKeyTypeName();
|
||||
public abstract int getKeyType();
|
||||
|
||||
public byte[] getPublicKeyBlob(){ return publickeyblob; }
|
||||
|
||||
public void writePublicKey(java.io.OutputStream out, String comment){
|
||||
byte[] pubblob=getPublicKeyBlob();
|
||||
byte[] pub=Util.toBase64(pubblob, 0, pubblob.length);
|
||||
try{
|
||||
out.write(getKeyTypeName()); out.write(space);
|
||||
out.write(pub, 0, pub.length); out.write(space);
|
||||
out.write(Util.str2byte(comment));
|
||||
out.write(cr);
|
||||
}
|
||||
catch(Exception e){
|
||||
}
|
||||
}
|
||||
|
||||
public void writePublicKey(String name, String comment) throws java.io.FileNotFoundException, java.io.IOException{
|
||||
FileOutputStream fos=new FileOutputStream(name);
|
||||
writePublicKey(fos, comment);
|
||||
fos.close();
|
||||
}
|
||||
|
||||
public void writeSECSHPublicKey(java.io.OutputStream out, String comment){
|
||||
byte[] pubblob=getPublicKeyBlob();
|
||||
byte[] pub=Util.toBase64(pubblob, 0, pubblob.length);
|
||||
try{
|
||||
out.write(Util.str2byte("---- BEGIN SSH2 PUBLIC KEY ----")); out.write(cr);
|
||||
out.write(Util.str2byte("Comment: \""+comment+"\"")); out.write(cr);
|
||||
int index=0;
|
||||
while(index<pub.length){
|
||||
int len=70;
|
||||
if((pub.length-index)<len)len=pub.length-index;
|
||||
out.write(pub, index, len); out.write(cr);
|
||||
index+=len;
|
||||
}
|
||||
out.write(Util.str2byte("---- END SSH2 PUBLIC KEY ----")); out.write(cr);
|
||||
}
|
||||
catch(Exception e){
|
||||
}
|
||||
}
|
||||
|
||||
public void writeSECSHPublicKey(String name, String comment) throws java.io.FileNotFoundException, java.io.IOException{
|
||||
FileOutputStream fos=new FileOutputStream(name);
|
||||
writeSECSHPublicKey(fos, comment);
|
||||
fos.close();
|
||||
}
|
||||
|
||||
|
||||
public void writePrivateKey(String name) throws java.io.FileNotFoundException, java.io.IOException{
|
||||
FileOutputStream fos=new FileOutputStream(name);
|
||||
writePrivateKey(fos);
|
||||
fos.close();
|
||||
}
|
||||
|
||||
public String getFingerPrint(){
|
||||
if(hash==null) hash=genHash();
|
||||
byte[] kblob=getPublicKeyBlob();
|
||||
if(kblob==null) return null;
|
||||
return getKeySize()+" "+Util.getFingerPrint(hash, kblob);
|
||||
}
|
||||
|
||||
private byte[] encrypt(byte[] plain, byte[][] _iv){
|
||||
if(passphrase==null) return plain;
|
||||
|
||||
if(cipher==null) cipher=genCipher();
|
||||
byte[] iv=_iv[0]=new byte[cipher.getIVSize()];
|
||||
|
||||
if(random==null) random=genRandom();
|
||||
random.fill(iv, 0, iv.length);
|
||||
|
||||
byte[] key=genKey(passphrase, iv);
|
||||
byte[] encoded=plain;
|
||||
|
||||
// PKCS#5Padding
|
||||
{
|
||||
//int bsize=cipher.getBlockSize();
|
||||
int bsize=cipher.getIVSize();
|
||||
byte[] foo=new byte[(encoded.length/bsize+1)*bsize];
|
||||
System.arraycopy(encoded, 0, foo, 0, encoded.length);
|
||||
int padding=bsize-encoded.length%bsize;
|
||||
for(int i=foo.length-1; (foo.length-padding)<=i; i--){
|
||||
foo[i]=(byte)padding;
|
||||
}
|
||||
encoded=foo;
|
||||
}
|
||||
|
||||
try{
|
||||
cipher.init(Cipher.ENCRYPT_MODE, key, iv);
|
||||
cipher.update(encoded, 0, encoded.length, encoded, 0);
|
||||
}
|
||||
catch(Exception e){
|
||||
//System.err.println(e);
|
||||
}
|
||||
Util.bzero(key);
|
||||
return encoded;
|
||||
}
|
||||
|
||||
abstract boolean parse(byte[] data);
|
||||
|
||||
private byte[] decrypt(byte[] data, byte[] passphrase, byte[] iv){
|
||||
/*
|
||||
if(iv==null){ // FSecure
|
||||
iv=new byte[8];
|
||||
for(int i=0; i<iv.length; i++)iv[i]=0;
|
||||
}
|
||||
*/
|
||||
try{
|
||||
byte[] key=genKey(passphrase, iv);
|
||||
cipher.init(Cipher.DECRYPT_MODE, key, iv);
|
||||
Util.bzero(key);
|
||||
byte[] plain=new byte[data.length];
|
||||
cipher.update(data, 0, data.length, plain, 0);
|
||||
return plain;
|
||||
}
|
||||
catch(Exception e){
|
||||
//System.err.println(e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
int writeSEQUENCE(byte[] buf, int index, int len){
|
||||
buf[index++]=0x30;
|
||||
index=writeLength(buf, index, len);
|
||||
return index;
|
||||
}
|
||||
int writeINTEGER(byte[] buf, int index, byte[] data){
|
||||
buf[index++]=0x02;
|
||||
index=writeLength(buf, index, data.length);
|
||||
System.arraycopy(data, 0, buf, index, data.length);
|
||||
index+=data.length;
|
||||
return index;
|
||||
}
|
||||
|
||||
int countLength(int len){
|
||||
int i=1;
|
||||
if(len<=0x7f) return i;
|
||||
while(len>0){
|
||||
len>>>=8;
|
||||
i++;
|
||||
}
|
||||
return i;
|
||||
}
|
||||
|
||||
int writeLength(byte[] data, int index, int len){
|
||||
int i=countLength(len)-1;
|
||||
if(i==0){
|
||||
data[index++]=(byte)len;
|
||||
return index;
|
||||
}
|
||||
data[index++]=(byte)(0x80|i);
|
||||
int j=index+i;
|
||||
while(i>0){
|
||||
data[index+i-1]=(byte)(len&0xff);
|
||||
len>>>=8;
|
||||
i--;
|
||||
}
|
||||
return j;
|
||||
}
|
||||
|
||||
private Random genRandom(){
|
||||
if(random==null){
|
||||
try{
|
||||
Class c=Class.forName(jsch.getConfig("random"));
|
||||
random=(Random)(c.newInstance());
|
||||
}
|
||||
catch(Exception e){ System.err.println("connect: random "+e); }
|
||||
}
|
||||
return random;
|
||||
}
|
||||
|
||||
private HASH genHash(){
|
||||
try{
|
||||
Class c=Class.forName(jsch.getConfig("md5"));
|
||||
hash=(HASH)(c.newInstance());
|
||||
hash.init();
|
||||
}
|
||||
catch(Exception e){
|
||||
}
|
||||
return hash;
|
||||
}
|
||||
private Cipher genCipher(){
|
||||
try{
|
||||
Class c;
|
||||
c=Class.forName(jsch.getConfig("3des-cbc"));
|
||||
cipher=(Cipher)(c.newInstance());
|
||||
}
|
||||
catch(Exception e){
|
||||
}
|
||||
return cipher;
|
||||
}
|
||||
|
||||
/*
|
||||
hash is MD5
|
||||
h(0) <- hash(passphrase, iv);
|
||||
h(n) <- hash(h(n-1), passphrase, iv);
|
||||
key <- (h(0),...,h(n))[0,..,key.length];
|
||||
*/
|
||||
synchronized byte[] genKey(byte[] passphrase, byte[] iv){
|
||||
if(cipher==null) cipher=genCipher();
|
||||
if(hash==null) hash=genHash();
|
||||
|
||||
byte[] key=new byte[cipher.getBlockSize()];
|
||||
int hsize=hash.getBlockSize();
|
||||
byte[] hn=new byte[key.length/hsize*hsize+
|
||||
(key.length%hsize==0?0:hsize)];
|
||||
try{
|
||||
byte[] tmp=null;
|
||||
if(vendor==VENDOR_OPENSSH){
|
||||
for(int index=0; index+hsize<=hn.length;){
|
||||
if(tmp!=null){ hash.update(tmp, 0, tmp.length); }
|
||||
hash.update(passphrase, 0, passphrase.length);
|
||||
hash.update(iv, 0, iv.length > 8 ? 8: iv.length);
|
||||
tmp=hash.digest();
|
||||
System.arraycopy(tmp, 0, hn, index, tmp.length);
|
||||
index+=tmp.length;
|
||||
}
|
||||
System.arraycopy(hn, 0, key, 0, key.length);
|
||||
}
|
||||
else if(vendor==VENDOR_FSECURE){
|
||||
for(int index=0; index+hsize<=hn.length;){
|
||||
if(tmp!=null){ hash.update(tmp, 0, tmp.length); }
|
||||
hash.update(passphrase, 0, passphrase.length);
|
||||
tmp=hash.digest();
|
||||
System.arraycopy(tmp, 0, hn, index, tmp.length);
|
||||
index+=tmp.length;
|
||||
}
|
||||
System.arraycopy(hn, 0, key, 0, key.length);
|
||||
}
|
||||
}
|
||||
catch(Exception e){
|
||||
System.err.println(e);
|
||||
}
|
||||
return key;
|
||||
}
|
||||
|
||||
public void setPassphrase(String passphrase){
|
||||
if(passphrase==null || passphrase.length()==0){
|
||||
setPassphrase((byte[])null);
|
||||
}
|
||||
else{
|
||||
setPassphrase(Util.str2byte(passphrase));
|
||||
}
|
||||
}
|
||||
public void setPassphrase(byte[] passphrase){
|
||||
if(passphrase!=null && passphrase.length==0)
|
||||
passphrase=null;
|
||||
this.passphrase=passphrase;
|
||||
}
|
||||
|
||||
private boolean encrypted=false;
|
||||
private byte[] data=null;
|
||||
private byte[] iv=null;
|
||||
private byte[] publickeyblob=null;
|
||||
|
||||
public boolean isEncrypted(){ return encrypted; }
|
||||
public boolean decrypt(String _passphrase){
|
||||
if(_passphrase==null || _passphrase.length()==0){
|
||||
return !encrypted;
|
||||
}
|
||||
return decrypt(Util.str2byte(_passphrase));
|
||||
}
|
||||
public boolean decrypt(byte[] _passphrase){
|
||||
if(!encrypted){
|
||||
return true;
|
||||
}
|
||||
if(_passphrase==null){
|
||||
return !encrypted;
|
||||
}
|
||||
byte[] bar=new byte[_passphrase.length];
|
||||
System.arraycopy(_passphrase, 0, bar, 0, bar.length);
|
||||
_passphrase=bar;
|
||||
byte[] foo=decrypt(data, _passphrase, iv);
|
||||
Util.bzero(_passphrase);
|
||||
if(parse(foo)){
|
||||
encrypted=false;
|
||||
}
|
||||
return !encrypted;
|
||||
}
|
||||
|
||||
public static KeyPair load(JSch jsch, String prvkey) throws JSchException{
|
||||
String pubkey=prvkey+".pub";
|
||||
if(!new File(pubkey).exists()){
|
||||
pubkey=null;
|
||||
}
|
||||
return load(jsch, prvkey, pubkey);
|
||||
}
|
||||
public static KeyPair load(JSch jsch, String prvkey, String pubkey) throws JSchException{
|
||||
|
||||
byte[] iv=new byte[8]; // 8
|
||||
boolean encrypted=true;
|
||||
byte[] data=null;
|
||||
|
||||
byte[] publickeyblob=null;
|
||||
|
||||
int type=ERROR;
|
||||
int vendor=VENDOR_OPENSSH;
|
||||
String publicKeyComment = "";
|
||||
Cipher cipher=null;
|
||||
|
||||
try{
|
||||
File file=new File(prvkey);
|
||||
FileInputStream fis=new FileInputStream(prvkey);
|
||||
byte[] buf=new byte[(int)(file.length())];
|
||||
int len=0;
|
||||
while(true){
|
||||
int i=fis.read(buf, len, buf.length-len);
|
||||
if(i<=0)
|
||||
break;
|
||||
len+=i;
|
||||
}
|
||||
fis.close();
|
||||
|
||||
int i=0;
|
||||
|
||||
while(i<len){
|
||||
if(buf[i] == '-' && i+4<len &&
|
||||
buf[i+1] == '-' && buf[i+2] == '-' &&
|
||||
buf[i+3] == '-' && buf[i+4] == '-'){
|
||||
break;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
|
||||
while(i<len){
|
||||
if(buf[i]=='B'&& i+3<len && buf[i+1]=='E'&& buf[i+2]=='G'&& buf[i+3]=='I'){
|
||||
i+=6;
|
||||
if(buf[i]=='D'&& buf[i+1]=='S'&& buf[i+2]=='A'){ type=DSA; }
|
||||
else if(buf[i]=='R'&& buf[i+1]=='S'&& buf[i+2]=='A'){ type=RSA; }
|
||||
else if(buf[i]=='S'&& buf[i+1]=='S'&& buf[i+2]=='H'){ // FSecure
|
||||
type=UNKNOWN;
|
||||
vendor=VENDOR_FSECURE;
|
||||
}
|
||||
else{
|
||||
throw new JSchException("invalid privatekey: "+prvkey);
|
||||
}
|
||||
i+=3;
|
||||
continue;
|
||||
}
|
||||
if(buf[i]=='A'&& i+7<len && buf[i+1]=='E'&& buf[i+2]=='S'&& buf[i+3]=='-' &&
|
||||
buf[i+4]=='2'&& buf[i+5]=='5'&& buf[i+6]=='6'&& buf[i+7]=='-'){
|
||||
i+=8;
|
||||
if(Session.checkCipher((String)jsch.getConfig("aes256-cbc"))){
|
||||
Class c=Class.forName((String)jsch.getConfig("aes256-cbc"));
|
||||
cipher=(Cipher)(c.newInstance());
|
||||
// key=new byte[cipher.getBlockSize()];
|
||||
iv=new byte[cipher.getIVSize()];
|
||||
}
|
||||
else{
|
||||
throw new JSchException("privatekey: aes256-cbc is not available "+prvkey);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if(buf[i]=='A'&& i+7<len && buf[i+1]=='E'&& buf[i+2]=='S'&& buf[i+3]=='-' &&
|
||||
buf[i+4]=='1'&& buf[i+5]=='9'&& buf[i+6]=='2'&& buf[i+7]=='-'){
|
||||
i+=8;
|
||||
if(Session.checkCipher((String)jsch.getConfig("aes192-cbc"))){
|
||||
Class c=Class.forName((String)jsch.getConfig("aes192-cbc"));
|
||||
cipher=(Cipher)(c.newInstance());
|
||||
// key=new byte[cipher.getBlockSize()];
|
||||
iv=new byte[cipher.getIVSize()];
|
||||
}
|
||||
else{
|
||||
throw new JSchException("privatekey: aes192-cbc is not available "+prvkey);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if(buf[i]=='A'&& i+7<len && buf[i+1]=='E'&& buf[i+2]=='S'&& buf[i+3]=='-' &&
|
||||
buf[i+4]=='1'&& buf[i+5]=='2'&& buf[i+6]=='8'&& buf[i+7]=='-'){
|
||||
i+=8;
|
||||
if(Session.checkCipher((String)jsch.getConfig("aes128-cbc"))){
|
||||
Class c=Class.forName((String)jsch.getConfig("aes128-cbc"));
|
||||
cipher=(Cipher)(c.newInstance());
|
||||
// key=new byte[cipher.getBlockSize()];
|
||||
iv=new byte[cipher.getIVSize()];
|
||||
}
|
||||
else{
|
||||
throw new JSchException("privatekey: aes128-cbc is not available "+prvkey);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if(buf[i]=='C'&& i+3<len && buf[i+1]=='B'&& buf[i+2]=='C'&& buf[i+3]==','){
|
||||
i+=4;
|
||||
for(int ii=0; ii<iv.length; ii++){
|
||||
iv[ii]=(byte)(((a2b(buf[i++])<<4)&0xf0)+(a2b(buf[i++])&0xf));
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if(buf[i]==0x0d && i+1<buf.length && buf[i+1]==0x0a){
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
if(buf[i]==0x0a && i+1<buf.length){
|
||||
if(buf[i+1]==0x0a){ i+=2; break; }
|
||||
if(buf[i+1]==0x0d &&
|
||||
i+2<buf.length && buf[i+2]==0x0a){
|
||||
i+=3; break;
|
||||
}
|
||||
boolean inheader=false;
|
||||
for(int j=i+1; j<buf.length; j++){
|
||||
if(buf[j]==0x0a) break;
|
||||
//if(buf[j]==0x0d) break;
|
||||
if(buf[j]==':'){inheader=true; break;}
|
||||
}
|
||||
if(!inheader){
|
||||
i++;
|
||||
encrypted=false; // no passphrase
|
||||
break;
|
||||
}
|
||||
}
|
||||
i++;
|
||||
}
|
||||
|
||||
if(type==ERROR){
|
||||
throw new JSchException("invalid privatekey: "+prvkey);
|
||||
}
|
||||
|
||||
int start=i;
|
||||
while(i<len){
|
||||
if(buf[i]==0x0a){
|
||||
boolean xd=(buf[i-1]==0x0d);
|
||||
System.arraycopy(buf, i+1,
|
||||
buf,
|
||||
i-(xd ? 1 : 0),
|
||||
len-i-1-(xd ? 1 : 0)
|
||||
);
|
||||
if(xd)len--;
|
||||
len--;
|
||||
continue;
|
||||
}
|
||||
if(buf[i]=='-'){ break; }
|
||||
i++;
|
||||
}
|
||||
data=Util.fromBase64(buf, start, i-start);
|
||||
|
||||
if(data.length>4 && // FSecure
|
||||
data[0]==(byte)0x3f &&
|
||||
data[1]==(byte)0x6f &&
|
||||
data[2]==(byte)0xf9 &&
|
||||
data[3]==(byte)0xeb){
|
||||
|
||||
Buffer _buf=new Buffer(data);
|
||||
_buf.getInt(); // 0x3f6ff9be
|
||||
_buf.getInt();
|
||||
byte[]_type=_buf.getString();
|
||||
//System.err.println("type: "+new String(_type));
|
||||
String _cipher=Util.byte2str(_buf.getString());
|
||||
//System.err.println("cipher: "+_cipher);
|
||||
if(_cipher.equals("3des-cbc")){
|
||||
_buf.getInt();
|
||||
byte[] foo=new byte[data.length-_buf.getOffSet()];
|
||||
_buf.getByte(foo);
|
||||
data=foo;
|
||||
encrypted=true;
|
||||
throw new JSchException("unknown privatekey format: "+prvkey);
|
||||
}
|
||||
else if(_cipher.equals("none")){
|
||||
_buf.getInt();
|
||||
_buf.getInt();
|
||||
|
||||
encrypted=false;
|
||||
|
||||
byte[] foo=new byte[data.length-_buf.getOffSet()];
|
||||
_buf.getByte(foo);
|
||||
data=foo;
|
||||
}
|
||||
}
|
||||
|
||||
if(pubkey!=null){
|
||||
try{
|
||||
file=new File(pubkey);
|
||||
fis=new FileInputStream(pubkey);
|
||||
buf=new byte[(int)(file.length())];
|
||||
len=0;
|
||||
while(true){
|
||||
i=fis.read(buf, len, buf.length-len);
|
||||
if(i<=0)
|
||||
break;
|
||||
len+=i;
|
||||
}
|
||||
fis.close();
|
||||
|
||||
if(buf.length>4 && // FSecure's public key
|
||||
buf[0]=='-' && buf[1]=='-' && buf[2]=='-' && buf[3]=='-'){
|
||||
|
||||
boolean valid=true;
|
||||
i=0;
|
||||
do{i++;}while(buf.length>i && buf[i]!=0x0a);
|
||||
if(buf.length<=i) {valid=false;}
|
||||
|
||||
while(valid){
|
||||
if(buf[i]==0x0a){
|
||||
boolean inheader=false;
|
||||
for(int j=i+1; j<buf.length; j++){
|
||||
if(buf[j]==0x0a) break;
|
||||
if(buf[j]==':'){inheader=true; break;}
|
||||
}
|
||||
if(!inheader){
|
||||
i++;
|
||||
break;
|
||||
}
|
||||
}
|
||||
i++;
|
||||
}
|
||||
if(buf.length<=i){valid=false;}
|
||||
|
||||
start=i;
|
||||
while(valid && i<len){
|
||||
if(buf[i]==0x0a){
|
||||
System.arraycopy(buf, i+1, buf, i, len-i-1);
|
||||
len--;
|
||||
continue;
|
||||
}
|
||||
if(buf[i]=='-'){ break; }
|
||||
i++;
|
||||
}
|
||||
if(valid){
|
||||
publickeyblob=Util.fromBase64(buf, start, i-start);
|
||||
if(type==UNKNOWN){
|
||||
if(publickeyblob[8]=='d'){ type=DSA; }
|
||||
else if(publickeyblob[8]=='r'){ type=RSA; }
|
||||
}
|
||||
}
|
||||
}
|
||||
else{
|
||||
if(buf[0]=='s'&& buf[1]=='s'&& buf[2]=='h' && buf[3]=='-'){
|
||||
i=0;
|
||||
while(i<len){ if(buf[i]==' ')break; i++;} i++;
|
||||
if(i<len){
|
||||
start=i;
|
||||
while(i<len){ if(buf[i]==' ')break; i++;}
|
||||
publickeyblob=Util.fromBase64(buf, start, i-start);
|
||||
}
|
||||
if(i++<len){
|
||||
int s=i;
|
||||
while(i<len){ if(buf[i]=='\n')break; i++;}
|
||||
if(i<len){
|
||||
publicKeyComment = new String(buf, s, i-s);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch(Exception ee){
|
||||
}
|
||||
}
|
||||
}
|
||||
catch(Exception e){
|
||||
if(e instanceof JSchException) throw (JSchException)e;
|
||||
if(e instanceof Throwable)
|
||||
throw new JSchException(e.toString(), (Throwable)e);
|
||||
throw new JSchException(e.toString());
|
||||
}
|
||||
|
||||
KeyPair kpair=null;
|
||||
if(type==DSA){ kpair=new KeyPairDSA(jsch); }
|
||||
else if(type==RSA){ kpair=new KeyPairRSA(jsch); }
|
||||
|
||||
if(kpair!=null){
|
||||
kpair.encrypted=encrypted;
|
||||
kpair.publickeyblob=publickeyblob;
|
||||
kpair.vendor=vendor;
|
||||
kpair.publicKeyComment=publicKeyComment;
|
||||
kpair.cipher=cipher;
|
||||
|
||||
if(encrypted){
|
||||
kpair.iv=iv;
|
||||
kpair.data=data;
|
||||
}
|
||||
else{
|
||||
if(kpair.parse(data)){
|
||||
return kpair;
|
||||
}
|
||||
else{
|
||||
throw new JSchException("invalid privatekey: "+prvkey);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return kpair;
|
||||
}
|
||||
|
||||
static private byte a2b(byte c){
|
||||
if('0'<=c&&c<='9') return (byte)(c-'0');
|
||||
return (byte)(c-'a'+10);
|
||||
}
|
||||
static private byte b2a(byte c){
|
||||
if(0<=c&&c<=9) return (byte)(c+'0');
|
||||
return (byte)(c-10+'A');
|
||||
}
|
||||
|
||||
public void dispose(){
|
||||
Util.bzero(passphrase);
|
||||
}
|
||||
|
||||
public void finalize (){
|
||||
dispose();
|
||||
}
|
||||
}
|
@ -1,221 +0,0 @@
|
||||
/* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
|
||||
/*
|
||||
Copyright (c) 2002-2011 ymnk, JCraft,Inc. All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice,
|
||||
this list of conditions and the following disclaimer.
|
||||
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the distribution.
|
||||
|
||||
3. The names of the authors may not be used to endorse or promote products
|
||||
derived from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
|
||||
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
|
||||
INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
|
||||
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
||||
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
package com.jcraft.jsch;
|
||||
|
||||
public class KeyPairDSA extends KeyPair{
|
||||
private byte[] P_array;
|
||||
private byte[] Q_array;
|
||||
private byte[] G_array;
|
||||
private byte[] pub_array;
|
||||
private byte[] prv_array;
|
||||
|
||||
//private int key_size=0;
|
||||
private int key_size=1024;
|
||||
|
||||
public KeyPairDSA(JSch jsch){
|
||||
super(jsch);
|
||||
}
|
||||
|
||||
void generate(int key_size) throws JSchException{
|
||||
this.key_size=key_size;
|
||||
try{
|
||||
Class c=Class.forName(jsch.getConfig("keypairgen.dsa"));
|
||||
KeyPairGenDSA keypairgen=(KeyPairGenDSA)(c.newInstance());
|
||||
keypairgen.init(key_size);
|
||||
P_array=keypairgen.getP();
|
||||
Q_array=keypairgen.getQ();
|
||||
G_array=keypairgen.getG();
|
||||
pub_array=keypairgen.getY();
|
||||
prv_array=keypairgen.getX();
|
||||
|
||||
keypairgen=null;
|
||||
}
|
||||
catch(Exception e){
|
||||
//System.err.println("KeyPairDSA: "+e);
|
||||
if(e instanceof Throwable)
|
||||
throw new JSchException(e.toString(), (Throwable)e);
|
||||
throw new JSchException(e.toString());
|
||||
}
|
||||
}
|
||||
|
||||
private static final byte[] begin=Util.str2byte("-----BEGIN DSA PRIVATE KEY-----");
|
||||
private static final byte[] end=Util.str2byte("-----END DSA PRIVATE KEY-----");
|
||||
|
||||
byte[] getBegin(){ return begin; }
|
||||
byte[] getEnd(){ return end; }
|
||||
|
||||
byte[] getPrivateKey(){
|
||||
int content=
|
||||
1+countLength(1) + 1 + // INTEGER
|
||||
1+countLength(P_array.length) + P_array.length + // INTEGER P
|
||||
1+countLength(Q_array.length) + Q_array.length + // INTEGER Q
|
||||
1+countLength(G_array.length) + G_array.length + // INTEGER G
|
||||
1+countLength(pub_array.length) + pub_array.length + // INTEGER pub
|
||||
1+countLength(prv_array.length) + prv_array.length; // INTEGER prv
|
||||
|
||||
int total=
|
||||
1+countLength(content)+content; // SEQUENCE
|
||||
|
||||
byte[] plain=new byte[total];
|
||||
int index=0;
|
||||
index=writeSEQUENCE(plain, index, content);
|
||||
index=writeINTEGER(plain, index, new byte[1]); // 0
|
||||
index=writeINTEGER(plain, index, P_array);
|
||||
index=writeINTEGER(plain, index, Q_array);
|
||||
index=writeINTEGER(plain, index, G_array);
|
||||
index=writeINTEGER(plain, index, pub_array);
|
||||
index=writeINTEGER(plain, index, prv_array);
|
||||
return plain;
|
||||
}
|
||||
|
||||
boolean parse(byte[] plain){
|
||||
try{
|
||||
|
||||
if(vendor==VENDOR_FSECURE){
|
||||
if(plain[0]!=0x30){ // FSecure
|
||||
Buffer buf=new Buffer(plain);
|
||||
buf.getInt();
|
||||
P_array=buf.getMPIntBits();
|
||||
G_array=buf.getMPIntBits();
|
||||
Q_array=buf.getMPIntBits();
|
||||
pub_array=buf.getMPIntBits();
|
||||
prv_array=buf.getMPIntBits();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
int index=0;
|
||||
int length=0;
|
||||
|
||||
if(plain[index]!=0x30)return false;
|
||||
index++; // SEQUENCE
|
||||
length=plain[index++]&0xff;
|
||||
if((length&0x80)!=0){
|
||||
int foo=length&0x7f; length=0;
|
||||
while(foo-->0){ length=(length<<8)+(plain[index++]&0xff); }
|
||||
}
|
||||
|
||||
if(plain[index]!=0x02)return false;
|
||||
index++; // INTEGER
|
||||
length=plain[index++]&0xff;
|
||||
if((length&0x80)!=0){
|
||||
int foo=length&0x7f; length=0;
|
||||
while(foo-->0){ length=(length<<8)+(plain[index++]&0xff); }
|
||||
}
|
||||
index+=length;
|
||||
|
||||
index++;
|
||||
length=plain[index++]&0xff;
|
||||
if((length&0x80)!=0){
|
||||
int foo=length&0x7f; length=0;
|
||||
while(foo-->0){ length=(length<<8)+(plain[index++]&0xff); }
|
||||
}
|
||||
P_array=new byte[length];
|
||||
System.arraycopy(plain, index, P_array, 0, length);
|
||||
index+=length;
|
||||
|
||||
index++;
|
||||
length=plain[index++]&0xff;
|
||||
if((length&0x80)!=0){
|
||||
int foo=length&0x7f; length=0;
|
||||
while(foo-->0){ length=(length<<8)+(plain[index++]&0xff); }
|
||||
}
|
||||
Q_array=new byte[length];
|
||||
System.arraycopy(plain, index, Q_array, 0, length);
|
||||
index+=length;
|
||||
|
||||
index++;
|
||||
length=plain[index++]&0xff;
|
||||
if((length&0x80)!=0){
|
||||
int foo=length&0x7f; length=0;
|
||||
while(foo-->0){ length=(length<<8)+(plain[index++]&0xff); }
|
||||
}
|
||||
G_array=new byte[length];
|
||||
System.arraycopy(plain, index, G_array, 0, length);
|
||||
index+=length;
|
||||
|
||||
index++;
|
||||
length=plain[index++]&0xff;
|
||||
if((length&0x80)!=0){
|
||||
int foo=length&0x7f; length=0;
|
||||
while(foo-->0){ length=(length<<8)+(plain[index++]&0xff); }
|
||||
}
|
||||
pub_array=new byte[length];
|
||||
System.arraycopy(plain, index, pub_array, 0, length);
|
||||
index+=length;
|
||||
|
||||
index++;
|
||||
length=plain[index++]&0xff;
|
||||
if((length&0x80)!=0){
|
||||
int foo=length&0x7f; length=0;
|
||||
while(foo-->0){ length=(length<<8)+(plain[index++]&0xff); }
|
||||
}
|
||||
prv_array=new byte[length];
|
||||
System.arraycopy(plain, index, prv_array, 0, length);
|
||||
index+=length;
|
||||
}
|
||||
catch(Exception e){
|
||||
//System.err.println(e);
|
||||
//e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public byte[] getPublicKeyBlob(){
|
||||
byte[] foo=super.getPublicKeyBlob();
|
||||
if(foo!=null) return foo;
|
||||
|
||||
if(P_array==null) return null;
|
||||
|
||||
Buffer buf=new Buffer(sshdss.length+4+
|
||||
P_array.length+4+
|
||||
Q_array.length+4+
|
||||
G_array.length+4+
|
||||
pub_array.length+4);
|
||||
buf.putString(sshdss);
|
||||
buf.putString(P_array);
|
||||
buf.putString(Q_array);
|
||||
buf.putString(G_array);
|
||||
buf.putString(pub_array);
|
||||
return buf.buffer;
|
||||
}
|
||||
|
||||
private static final byte[] sshdss=Util.str2byte("ssh-dss");
|
||||
byte[] getKeyTypeName(){return sshdss;}
|
||||
public int getKeyType(){return DSA;}
|
||||
|
||||
public int getKeySize(){return key_size; }
|
||||
public void dispose(){
|
||||
super.dispose();
|
||||
Util.bzero(prv_array);
|
||||
}
|
||||
}
|
@ -1,39 +0,0 @@
|
||||
/* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
|
||||
/*
|
||||
Copyright (c) 2002-2011 ymnk, JCraft,Inc. All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice,
|
||||
this list of conditions and the following disclaimer.
|
||||
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the distribution.
|
||||
|
||||
3. The names of the authors may not be used to endorse or promote products
|
||||
derived from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
|
||||
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
|
||||
INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
|
||||
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
||||
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
package com.jcraft.jsch;
|
||||
|
||||
public interface KeyPairGenDSA{
|
||||
void init(int key_size) throws Exception;
|
||||
byte[] getX();
|
||||
byte[] getY();
|
||||
byte[] getP();
|
||||
byte[] getQ();
|
||||
byte[] getG();
|
||||
}
|
@ -1,43 +0,0 @@
|
||||
/* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
|
||||
/*
|
||||
Copyright (c) 2002-2011 ymnk, JCraft,Inc. All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice,
|
||||
this list of conditions and the following disclaimer.
|
||||
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the distribution.
|
||||
|
||||
3. The names of the authors may not be used to endorse or promote products
|
||||
derived from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
|
||||
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
|
||||
INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
|
||||
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
||||
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
package com.jcraft.jsch;
|
||||
|
||||
public interface KeyPairGenRSA{
|
||||
void init(int key_size) throws Exception;
|
||||
byte[] getD();
|
||||
byte[] getE();
|
||||
byte[] getN();
|
||||
|
||||
byte[] getC();
|
||||
byte[] getEP();
|
||||
byte[] getEQ();
|
||||
byte[] getP();
|
||||
byte[] getQ();
|
||||
}
|
@ -1,320 +0,0 @@
|
||||
/* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
|
||||
/*
|
||||
Copyright (c) 2002-2011 ymnk, JCraft,Inc. All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice,
|
||||
this list of conditions and the following disclaimer.
|
||||
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the distribution.
|
||||
|
||||
3. The names of the authors may not be used to endorse or promote products
|
||||
derived from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
|
||||
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
|
||||
INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
|
||||
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
||||
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
package com.jcraft.jsch;
|
||||
|
||||
public class KeyPairRSA extends KeyPair{
|
||||
private byte[] prv_array;
|
||||
private byte[] pub_array;
|
||||
private byte[] n_array;
|
||||
|
||||
private byte[] p_array; // prime p
|
||||
private byte[] q_array; // prime q
|
||||
private byte[] ep_array; // prime exponent p
|
||||
private byte[] eq_array; // prime exponent q
|
||||
private byte[] c_array; // coefficient
|
||||
|
||||
//private int key_size=0;
|
||||
private int key_size=1024;
|
||||
|
||||
public KeyPairRSA(JSch jsch){
|
||||
super(jsch);
|
||||
}
|
||||
|
||||
void generate(int key_size) throws JSchException{
|
||||
this.key_size=key_size;
|
||||
try{
|
||||
Class c=Class.forName(jsch.getConfig("keypairgen.rsa"));
|
||||
KeyPairGenRSA keypairgen=(KeyPairGenRSA)(c.newInstance());
|
||||
keypairgen.init(key_size);
|
||||
pub_array=keypairgen.getE();
|
||||
prv_array=keypairgen.getD();
|
||||
n_array=keypairgen.getN();
|
||||
|
||||
p_array=keypairgen.getP();
|
||||
q_array=keypairgen.getQ();
|
||||
ep_array=keypairgen.getEP();
|
||||
eq_array=keypairgen.getEQ();
|
||||
c_array=keypairgen.getC();
|
||||
|
||||
keypairgen=null;
|
||||
}
|
||||
catch(Exception e){
|
||||
//System.err.println("KeyPairRSA: "+e);
|
||||
if(e instanceof Throwable)
|
||||
throw new JSchException(e.toString(), (Throwable)e);
|
||||
throw new JSchException(e.toString());
|
||||
}
|
||||
}
|
||||
|
||||
private static final byte[] begin=Util.str2byte("-----BEGIN RSA PRIVATE KEY-----");
|
||||
private static final byte[] end=Util.str2byte("-----END RSA PRIVATE KEY-----");
|
||||
|
||||
byte[] getBegin(){ return begin; }
|
||||
byte[] getEnd(){ return end; }
|
||||
|
||||
byte[] getPrivateKey(){
|
||||
int content=
|
||||
1+countLength(1) + 1 + // INTEGER
|
||||
1+countLength(n_array.length) + n_array.length + // INTEGER N
|
||||
1+countLength(pub_array.length) + pub_array.length + // INTEGER pub
|
||||
1+countLength(prv_array.length) + prv_array.length+ // INTEGER prv
|
||||
1+countLength(p_array.length) + p_array.length+ // INTEGER p
|
||||
1+countLength(q_array.length) + q_array.length+ // INTEGER q
|
||||
1+countLength(ep_array.length) + ep_array.length+ // INTEGER ep
|
||||
1+countLength(eq_array.length) + eq_array.length+ // INTEGER eq
|
||||
1+countLength(c_array.length) + c_array.length; // INTEGER c
|
||||
|
||||
int total=
|
||||
1+countLength(content)+content; // SEQUENCE
|
||||
|
||||
byte[] plain=new byte[total];
|
||||
int index=0;
|
||||
index=writeSEQUENCE(plain, index, content);
|
||||
index=writeINTEGER(plain, index, new byte[1]); // 0
|
||||
index=writeINTEGER(plain, index, n_array);
|
||||
index=writeINTEGER(plain, index, pub_array);
|
||||
index=writeINTEGER(plain, index, prv_array);
|
||||
index=writeINTEGER(plain, index, p_array);
|
||||
index=writeINTEGER(plain, index, q_array);
|
||||
index=writeINTEGER(plain, index, ep_array);
|
||||
index=writeINTEGER(plain, index, eq_array);
|
||||
index=writeINTEGER(plain, index, c_array);
|
||||
return plain;
|
||||
}
|
||||
|
||||
boolean parse(byte [] plain){
|
||||
/*
|
||||
byte[] p_array;
|
||||
byte[] q_array;
|
||||
byte[] dmp1_array;
|
||||
byte[] dmq1_array;
|
||||
byte[] iqmp_array;
|
||||
*/
|
||||
try{
|
||||
int index=0;
|
||||
int length=0;
|
||||
|
||||
if(vendor==VENDOR_FSECURE){
|
||||
if(plain[index]!=0x30){ // FSecure
|
||||
Buffer buf=new Buffer(plain);
|
||||
pub_array=buf.getMPIntBits();
|
||||
prv_array=buf.getMPIntBits();
|
||||
n_array=buf.getMPIntBits();
|
||||
byte[] u_array=buf.getMPIntBits();
|
||||
p_array=buf.getMPIntBits();
|
||||
q_array=buf.getMPIntBits();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
index++; // SEQUENCE
|
||||
length=plain[index++]&0xff;
|
||||
if((length&0x80)!=0){
|
||||
int foo=length&0x7f; length=0;
|
||||
while(foo-->0){ length=(length<<8)+(plain[index++]&0xff); }
|
||||
}
|
||||
|
||||
if(plain[index]!=0x02)return false;
|
||||
index++; // INTEGER
|
||||
length=plain[index++]&0xff;
|
||||
if((length&0x80)!=0){
|
||||
int foo=length&0x7f; length=0;
|
||||
while(foo-->0){ length=(length<<8)+(plain[index++]&0xff); }
|
||||
}
|
||||
index+=length;
|
||||
|
||||
//System.err.println("int: len="+length);
|
||||
//System.err.print(Integer.toHexString(plain[index-1]&0xff)+":");
|
||||
//System.err.println("");
|
||||
|
||||
index++;
|
||||
length=plain[index++]&0xff;
|
||||
if((length&0x80)!=0){
|
||||
int foo=length&0x7f; length=0;
|
||||
while(foo-->0){ length=(length<<8)+(plain[index++]&0xff); }
|
||||
}
|
||||
n_array=new byte[length];
|
||||
System.arraycopy(plain, index, n_array, 0, length);
|
||||
index+=length;
|
||||
/*
|
||||
System.err.println("int: N len="+length);
|
||||
for(int i=0; i<n_array.length; i++){
|
||||
System.err.print(Integer.toHexString(n_array[i]&0xff)+":");
|
||||
}
|
||||
System.err.println("");
|
||||
*/
|
||||
index++;
|
||||
length=plain[index++]&0xff;
|
||||
if((length&0x80)!=0){
|
||||
int foo=length&0x7f; length=0;
|
||||
while(foo-->0){ length=(length<<8)+(plain[index++]&0xff); }
|
||||
}
|
||||
pub_array=new byte[length];
|
||||
System.arraycopy(plain, index, pub_array, 0, length);
|
||||
index+=length;
|
||||
/*
|
||||
System.err.println("int: E len="+length);
|
||||
for(int i=0; i<pub_array.length; i++){
|
||||
System.err.print(Integer.toHexString(pub_array[i]&0xff)+":");
|
||||
}
|
||||
System.err.println("");
|
||||
*/
|
||||
index++;
|
||||
length=plain[index++]&0xff;
|
||||
if((length&0x80)!=0){
|
||||
int foo=length&0x7f; length=0;
|
||||
while(foo-->0){ length=(length<<8)+(plain[index++]&0xff); }
|
||||
}
|
||||
prv_array=new byte[length];
|
||||
System.arraycopy(plain, index, prv_array, 0, length);
|
||||
index+=length;
|
||||
/*
|
||||
System.err.println("int: prv len="+length);
|
||||
for(int i=0; i<prv_array.length; i++){
|
||||
System.err.print(Integer.toHexString(prv_array[i]&0xff)+":");
|
||||
}
|
||||
System.err.println("");
|
||||
*/
|
||||
|
||||
index++;
|
||||
length=plain[index++]&0xff;
|
||||
if((length&0x80)!=0){
|
||||
int foo=length&0x7f; length=0;
|
||||
while(foo-->0){ length=(length<<8)+(plain[index++]&0xff); }
|
||||
}
|
||||
p_array=new byte[length];
|
||||
System.arraycopy(plain, index, p_array, 0, length);
|
||||
index+=length;
|
||||
/*
|
||||
System.err.println("int: P len="+length);
|
||||
for(int i=0; i<p_array.length; i++){
|
||||
System.err.print(Integer.toHexString(p_array[i]&0xff)+":");
|
||||
}
|
||||
System.err.println("");
|
||||
*/
|
||||
index++;
|
||||
length=plain[index++]&0xff;
|
||||
if((length&0x80)!=0){
|
||||
int foo=length&0x7f; length=0;
|
||||
while(foo-->0){ length=(length<<8)+(plain[index++]&0xff); }
|
||||
}
|
||||
q_array=new byte[length];
|
||||
System.arraycopy(plain, index, q_array, 0, length);
|
||||
index+=length;
|
||||
/*
|
||||
System.err.println("int: q len="+length);
|
||||
for(int i=0; i<q_array.length; i++){
|
||||
System.err.print(Integer.toHexString(q_array[i]&0xff)+":");
|
||||
}
|
||||
System.err.println("");
|
||||
*/
|
||||
index++;
|
||||
length=plain[index++]&0xff;
|
||||
if((length&0x80)!=0){
|
||||
int foo=length&0x7f; length=0;
|
||||
while(foo-->0){ length=(length<<8)+(plain[index++]&0xff); }
|
||||
}
|
||||
ep_array=new byte[length];
|
||||
System.arraycopy(plain, index, ep_array, 0, length);
|
||||
index+=length;
|
||||
/*
|
||||
System.err.println("int: ep len="+length);
|
||||
for(int i=0; i<ep_array.length; i++){
|
||||
System.err.print(Integer.toHexString(ep_array[i]&0xff)+":");
|
||||
}
|
||||
System.err.println("");
|
||||
*/
|
||||
index++;
|
||||
length=plain[index++]&0xff;
|
||||
if((length&0x80)!=0){
|
||||
int foo=length&0x7f; length=0;
|
||||
while(foo-->0){ length=(length<<8)+(plain[index++]&0xff); }
|
||||
}
|
||||
eq_array=new byte[length];
|
||||
System.arraycopy(plain, index, eq_array, 0, length);
|
||||
index+=length;
|
||||
/*
|
||||
System.err.println("int: eq len="+length);
|
||||
for(int i=0; i<eq_array.length; i++){
|
||||
System.err.print(Integer.toHexString(eq_array[i]&0xff)+":");
|
||||
}
|
||||
System.err.println("");
|
||||
*/
|
||||
index++;
|
||||
length=plain[index++]&0xff;
|
||||
if((length&0x80)!=0){
|
||||
int foo=length&0x7f; length=0;
|
||||
while(foo-->0){ length=(length<<8)+(plain[index++]&0xff); }
|
||||
}
|
||||
c_array=new byte[length];
|
||||
System.arraycopy(plain, index, c_array, 0, length);
|
||||
index+=length;
|
||||
/*
|
||||
System.err.println("int: c len="+length);
|
||||
for(int i=0; i<c_array.length; i++){
|
||||
System.err.print(Integer.toHexString(c_array[i]&0xff)+":");
|
||||
}
|
||||
System.err.println("");
|
||||
*/
|
||||
}
|
||||
catch(Exception e){
|
||||
//System.err.println(e);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
public byte[] getPublicKeyBlob(){
|
||||
byte[] foo=super.getPublicKeyBlob();
|
||||
if(foo!=null) return foo;
|
||||
|
||||
if(pub_array==null) return null;
|
||||
|
||||
Buffer buf=new Buffer(sshrsa.length+4+
|
||||
pub_array.length+4+
|
||||
n_array.length+4);
|
||||
buf.putString(sshrsa);
|
||||
buf.putString(pub_array);
|
||||
buf.putString(n_array);
|
||||
return buf.buffer;
|
||||
}
|
||||
|
||||
private static final byte[] sshrsa=Util.str2byte("ssh-rsa");
|
||||
byte[] getKeyTypeName(){return sshrsa;}
|
||||
public int getKeyType(){return RSA;}
|
||||
|
||||
public int getKeySize(){return key_size; }
|
||||
public void dispose(){
|
||||
super.dispose();
|
||||
Util.bzero(prv_array);
|
||||
}
|
||||
}
|
@ -1,506 +0,0 @@
|
||||
/* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
|
||||
/*
|
||||
Copyright (c) 2002-2011 ymnk, JCraft,Inc. All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice,
|
||||
this list of conditions and the following disclaimer.
|
||||
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the distribution.
|
||||
|
||||
3. The names of the authors may not be used to endorse or promote products
|
||||
derived from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
|
||||
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
|
||||
INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
|
||||
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
||||
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
package com.jcraft.jsch;
|
||||
|
||||
import java.io.*;
|
||||
|
||||
public
|
||||
class KnownHosts implements HostKeyRepository{
|
||||
private static final String _known_hosts="known_hosts";
|
||||
|
||||
/*
|
||||
static final int SSHDSS=0;
|
||||
static final int SSHRSA=1;
|
||||
static final int UNKNOWN=2;
|
||||
*/
|
||||
|
||||
private JSch jsch=null;
|
||||
private String known_hosts=null;
|
||||
private java.util.Vector pool=null;
|
||||
|
||||
private MAC hmacsha1=null;
|
||||
|
||||
KnownHosts(JSch jsch){
|
||||
super();
|
||||
this.jsch=jsch;
|
||||
pool=new java.util.Vector();
|
||||
}
|
||||
|
||||
void setKnownHosts(String foo) throws JSchException{
|
||||
try{
|
||||
known_hosts=foo;
|
||||
FileInputStream fis=new FileInputStream(foo);
|
||||
setKnownHosts(fis);
|
||||
}
|
||||
catch(FileNotFoundException e){
|
||||
}
|
||||
}
|
||||
void setKnownHosts(InputStream foo) throws JSchException{
|
||||
pool.removeAllElements();
|
||||
StringBuffer sb=new StringBuffer();
|
||||
byte i;
|
||||
int j;
|
||||
boolean error=false;
|
||||
try{
|
||||
InputStream fis=foo;
|
||||
String host;
|
||||
String key=null;
|
||||
int type;
|
||||
byte[] buf=new byte[1024];
|
||||
int bufl=0;
|
||||
loop:
|
||||
while(true){
|
||||
bufl=0;
|
||||
while(true){
|
||||
j=fis.read();
|
||||
if(j==-1){
|
||||
if(bufl==0){ break loop; }
|
||||
else{ break; }
|
||||
}
|
||||
if(j==0x0d){ continue; }
|
||||
if(j==0x0a){ break; }
|
||||
if(buf.length<=bufl){
|
||||
if(bufl>1024*10) break; // too long...
|
||||
byte[] newbuf=new byte[buf.length*2];
|
||||
System.arraycopy(buf, 0, newbuf, 0, buf.length);
|
||||
buf=newbuf;
|
||||
}
|
||||
buf[bufl++]=(byte)j;
|
||||
}
|
||||
|
||||
j=0;
|
||||
while(j<bufl){
|
||||
i=buf[j];
|
||||
if(i==' '||i=='\t'){ j++; continue; }
|
||||
if(i=='#'){
|
||||
addInvalidLine(Util.byte2str(buf, 0, bufl));
|
||||
continue loop;
|
||||
}
|
||||
break;
|
||||
}
|
||||
if(j>=bufl){
|
||||
addInvalidLine(Util.byte2str(buf, 0, bufl));
|
||||
continue loop;
|
||||
}
|
||||
|
||||
sb.setLength(0);
|
||||
while(j<bufl){
|
||||
i=buf[j++];
|
||||
if(i==0x20 || i=='\t'){ break; }
|
||||
sb.append((char)i);
|
||||
}
|
||||
host=sb.toString();
|
||||
if(j>=bufl || host.length()==0){
|
||||
addInvalidLine(Util.byte2str(buf, 0, bufl));
|
||||
continue loop;
|
||||
}
|
||||
|
||||
sb.setLength(0);
|
||||
type=-1;
|
||||
while(j<bufl){
|
||||
i=buf[j++];
|
||||
if(i==0x20 || i=='\t'){ break; }
|
||||
sb.append((char)i);
|
||||
}
|
||||
if(sb.toString().equals("ssh-dss")){ type=HostKey.SSHDSS; }
|
||||
else if(sb.toString().equals("ssh-rsa")){ type=HostKey.SSHRSA; }
|
||||
else { j=bufl; }
|
||||
if(j>=bufl){
|
||||
addInvalidLine(Util.byte2str(buf, 0, bufl));
|
||||
continue loop;
|
||||
}
|
||||
|
||||
sb.setLength(0);
|
||||
while(j<bufl){
|
||||
i=buf[j++];
|
||||
if(i==0x0d){ continue; }
|
||||
if(i==0x0a){ break; }
|
||||
sb.append((char)i);
|
||||
}
|
||||
key=sb.toString();
|
||||
if(key.length()==0){
|
||||
addInvalidLine(Util.byte2str(buf, 0, bufl));
|
||||
continue loop;
|
||||
}
|
||||
|
||||
//System.err.println(host);
|
||||
//System.err.println("|"+key+"|");
|
||||
|
||||
HostKey hk = null;
|
||||
hk = new HashedHostKey(host, type,
|
||||
Util.fromBase64(Util.str2byte(key), 0,
|
||||
key.length()));
|
||||
pool.addElement(hk);
|
||||
}
|
||||
fis.close();
|
||||
if(error){
|
||||
throw new JSchException("KnownHosts: invalid format");
|
||||
}
|
||||
}
|
||||
catch(Exception e){
|
||||
if(e instanceof JSchException)
|
||||
throw (JSchException)e;
|
||||
if(e instanceof Throwable)
|
||||
throw new JSchException(e.toString(), (Throwable)e);
|
||||
throw new JSchException(e.toString());
|
||||
}
|
||||
}
|
||||
private void addInvalidLine(String line) throws JSchException {
|
||||
HostKey hk = new HostKey(line, HostKey.UNKNOWN, null);
|
||||
pool.addElement(hk);
|
||||
}
|
||||
String getKnownHostsFile(){ return known_hosts; }
|
||||
public String getKnownHostsRepositoryID(){ return known_hosts; }
|
||||
|
||||
public int check(String host, byte[] key){
|
||||
int result=NOT_INCLUDED;
|
||||
if(host==null){
|
||||
return result;
|
||||
}
|
||||
|
||||
int type=getType(key);
|
||||
HostKey hk;
|
||||
|
||||
synchronized(pool){
|
||||
for(int i=0; i<pool.size(); i++){
|
||||
hk=(HostKey)(pool.elementAt(i));
|
||||
if(hk.isMatched(host) && hk.type==type){
|
||||
if(Util.array_equals(hk.key, key)){
|
||||
return OK;
|
||||
}
|
||||
else{
|
||||
result=CHANGED;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(result==NOT_INCLUDED &&
|
||||
host.startsWith("[") &&
|
||||
host.indexOf("]:")>1
|
||||
){
|
||||
return check(host.substring(1, host.indexOf("]:")), key);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
public void add(HostKey hostkey, UserInfo userinfo){
|
||||
int type=hostkey.type;
|
||||
String host=hostkey.getHost();
|
||||
byte[] key=hostkey.key;
|
||||
|
||||
HostKey hk=null;
|
||||
synchronized(pool){
|
||||
for(int i=0; i<pool.size(); i++){
|
||||
hk=(HostKey)(pool.elementAt(i));
|
||||
if(hk.isMatched(host) && hk.type==type){
|
||||
/*
|
||||
if(Util.array_equals(hk.key, key)){ return; }
|
||||
if(hk.host.equals(host)){
|
||||
hk.key=key;
|
||||
return;
|
||||
}
|
||||
else{
|
||||
hk.host=deleteSubString(hk.host, host);
|
||||
break;
|
||||
}
|
||||
*/
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
hk=hostkey;
|
||||
|
||||
pool.addElement(hk);
|
||||
|
||||
String bar=getKnownHostsRepositoryID();
|
||||
if(bar!=null){
|
||||
boolean foo=true;
|
||||
File goo=new File(bar);
|
||||
if(!goo.exists()){
|
||||
foo=false;
|
||||
if(userinfo!=null){
|
||||
foo=userinfo.promptYesNo(bar+" does not exist.\n"+
|
||||
"Are you sure you want to create it?"
|
||||
);
|
||||
goo=goo.getParentFile();
|
||||
if(foo && goo!=null && !goo.exists()){
|
||||
foo=userinfo.promptYesNo("The parent directory "+goo+" does not exist.\n"+
|
||||
"Are you sure you want to create it?"
|
||||
);
|
||||
if(foo){
|
||||
if(!goo.mkdirs()){
|
||||
userinfo.showMessage(goo+" has not been created.");
|
||||
foo=false;
|
||||
}
|
||||
else{
|
||||
userinfo.showMessage(goo+" has been succesfully created.\nPlease check its access permission.");
|
||||
}
|
||||
}
|
||||
}
|
||||
if(goo==null)foo=false;
|
||||
}
|
||||
}
|
||||
if(foo){
|
||||
try{
|
||||
sync(bar);
|
||||
}
|
||||
catch(Exception e){ System.err.println("sync known_hosts: "+e); }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public HostKey[] getHostKey(){
|
||||
return getHostKey(null, null);
|
||||
}
|
||||
public HostKey[] getHostKey(String host, String type){
|
||||
synchronized(pool){
|
||||
int count=0;
|
||||
for(int i=0; i<pool.size(); i++){
|
||||
HostKey hk=(HostKey)pool.elementAt(i);
|
||||
if(hk.type==HostKey.UNKNOWN) continue;
|
||||
if(host==null ||
|
||||
(hk.isMatched(host) &&
|
||||
(type==null || hk.getType().equals(type)))){
|
||||
count++;
|
||||
}
|
||||
}
|
||||
if(count==0)return null;
|
||||
HostKey[] foo=new HostKey[count];
|
||||
int j=0;
|
||||
for(int i=0; i<pool.size(); i++){
|
||||
HostKey hk=(HostKey)pool.elementAt(i);
|
||||
if(hk.type==HostKey.UNKNOWN) continue;
|
||||
if(host==null ||
|
||||
(hk.isMatched(host) &&
|
||||
(type==null || hk.getType().equals(type)))){
|
||||
foo[j++]=hk;
|
||||
}
|
||||
}
|
||||
return foo;
|
||||
}
|
||||
}
|
||||
public void remove(String host, String type){
|
||||
remove(host, type, null);
|
||||
}
|
||||
public void remove(String host, String type, byte[] key){
|
||||
boolean sync=false;
|
||||
synchronized(pool){
|
||||
for(int i=0; i<pool.size(); i++){
|
||||
HostKey hk=(HostKey)(pool.elementAt(i));
|
||||
if(host==null ||
|
||||
(hk.isMatched(host) &&
|
||||
(type==null || (hk.getType().equals(type) &&
|
||||
(key==null || Util.array_equals(key, hk.key)))))){
|
||||
String hosts=hk.getHost();
|
||||
if(hosts.equals(host) ||
|
||||
((hk instanceof HashedHostKey) &&
|
||||
((HashedHostKey)hk).isHashed())){
|
||||
pool.removeElement(hk);
|
||||
}
|
||||
else{
|
||||
hk.host=deleteSubString(hosts, host);
|
||||
}
|
||||
sync=true;
|
||||
}
|
||||
}
|
||||
}
|
||||
if(sync){
|
||||
try{sync();}catch(Exception e){};
|
||||
}
|
||||
}
|
||||
|
||||
protected void sync() throws IOException {
|
||||
if(known_hosts!=null)
|
||||
sync(known_hosts);
|
||||
}
|
||||
protected synchronized void sync(String foo) throws IOException {
|
||||
if(foo==null) return;
|
||||
FileOutputStream fos=new FileOutputStream(foo);
|
||||
dump(fos);
|
||||
fos.close();
|
||||
}
|
||||
|
||||
private static final byte[] space={(byte)0x20};
|
||||
private static final byte[] cr=Util.str2byte("\n");
|
||||
void dump(OutputStream out) throws IOException {
|
||||
try{
|
||||
HostKey hk;
|
||||
synchronized(pool){
|
||||
for(int i=0; i<pool.size(); i++){
|
||||
hk=(HostKey)(pool.elementAt(i));
|
||||
//hk.dump(out);
|
||||
String host=hk.getHost();
|
||||
String type=hk.getType();
|
||||
if(type.equals("UNKNOWN")){
|
||||
out.write(Util.str2byte(host));
|
||||
out.write(cr);
|
||||
continue;
|
||||
}
|
||||
out.write(Util.str2byte(host));
|
||||
out.write(space);
|
||||
out.write(Util.str2byte(type));
|
||||
out.write(space);
|
||||
out.write(Util.str2byte(hk.getKey()));
|
||||
out.write(cr);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch(Exception e){
|
||||
System.err.println(e);
|
||||
}
|
||||
}
|
||||
private int getType(byte[] key){
|
||||
if(key[8]=='d') return HostKey.SSHDSS;
|
||||
if(key[8]=='r') return HostKey.SSHRSA;
|
||||
return HostKey.UNKNOWN;
|
||||
}
|
||||
private String deleteSubString(String hosts, String host){
|
||||
int i=0;
|
||||
int hostlen=host.length();
|
||||
int hostslen=hosts.length();
|
||||
int j;
|
||||
while(i<hostslen){
|
||||
j=hosts.indexOf(',', i);
|
||||
if(j==-1) break;
|
||||
if(!host.equals(hosts.substring(i, j))){
|
||||
i=j+1;
|
||||
continue;
|
||||
}
|
||||
return hosts.substring(0, i)+hosts.substring(j+1);
|
||||
}
|
||||
if(hosts.endsWith(host) && hostslen-i==hostlen){
|
||||
return hosts.substring(0, (hostlen==hostslen) ? 0 :hostslen-hostlen-1);
|
||||
}
|
||||
return hosts;
|
||||
}
|
||||
|
||||
private synchronized MAC getHMACSHA1(){
|
||||
if(hmacsha1==null){
|
||||
try{
|
||||
Class c=Class.forName(jsch.getConfig("hmac-sha1"));
|
||||
hmacsha1=(MAC)(c.newInstance());
|
||||
}
|
||||
catch(Exception e){
|
||||
System.err.println("hmacsha1: "+e);
|
||||
}
|
||||
}
|
||||
return hmacsha1;
|
||||
}
|
||||
|
||||
HostKey createHashedHostKey(String host, byte[]key) throws JSchException {
|
||||
HashedHostKey hhk=new HashedHostKey(host, key);
|
||||
hhk.hash();
|
||||
return hhk;
|
||||
}
|
||||
class HashedHostKey extends HostKey{
|
||||
private static final String HASH_MAGIC="|1|";
|
||||
private static final String HASH_DELIM="|";
|
||||
|
||||
private boolean hashed=false;
|
||||
byte[] salt=null;
|
||||
byte[] hash=null;
|
||||
|
||||
|
||||
HashedHostKey(String host, byte[] key) throws JSchException {
|
||||
this(host, GUESS, key);
|
||||
}
|
||||
HashedHostKey(String host, int type, byte[] key) throws JSchException {
|
||||
super(host, type, key);
|
||||
if(this.host.startsWith(HASH_MAGIC) &&
|
||||
this.host.substring(HASH_MAGIC.length()).indexOf(HASH_DELIM)>0){
|
||||
String data=this.host.substring(HASH_MAGIC.length());
|
||||
String _salt=data.substring(0, data.indexOf(HASH_DELIM));
|
||||
String _hash=data.substring(data.indexOf(HASH_DELIM)+1);
|
||||
salt=Util.fromBase64(Util.str2byte(_salt), 0, _salt.length());
|
||||
hash=Util.fromBase64(Util.str2byte(_hash), 0, _hash.length());
|
||||
if(salt.length!=20 || // block size of hmac-sha1
|
||||
hash.length!=20){
|
||||
salt=null;
|
||||
hash=null;
|
||||
return;
|
||||
}
|
||||
hashed=true;
|
||||
}
|
||||
}
|
||||
|
||||
boolean isMatched(String _host){
|
||||
if(!hashed){
|
||||
return super.isMatched(_host);
|
||||
}
|
||||
MAC macsha1=getHMACSHA1();
|
||||
try{
|
||||
synchronized(macsha1){
|
||||
macsha1.init(salt);
|
||||
byte[] foo=Util.str2byte(_host);
|
||||
macsha1.update(foo, 0, foo.length);
|
||||
byte[] bar=new byte[macsha1.getBlockSize()];
|
||||
macsha1.doFinal(bar, 0);
|
||||
return Util.array_equals(hash, bar);
|
||||
}
|
||||
}
|
||||
catch(Exception e){
|
||||
System.out.println(e);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
boolean isHashed(){
|
||||
return hashed;
|
||||
}
|
||||
|
||||
void hash(){
|
||||
if(hashed)
|
||||
return;
|
||||
MAC macsha1=getHMACSHA1();
|
||||
if(salt==null){
|
||||
Random random=Session.random;
|
||||
synchronized(random){
|
||||
salt=new byte[macsha1.getBlockSize()];
|
||||
random.fill(salt, 0, salt.length);
|
||||
}
|
||||
}
|
||||
try{
|
||||
synchronized(macsha1){
|
||||
macsha1.init(salt);
|
||||
byte[] foo=Util.str2byte(host);
|
||||
macsha1.update(foo, 0, foo.length);
|
||||
hash=new byte[macsha1.getBlockSize()];
|
||||
macsha1.doFinal(hash, 0);
|
||||
}
|
||||
}
|
||||
catch(Exception e){
|
||||
}
|
||||
host=HASH_MAGIC+Util.byte2str(Util.toBase64(salt, 0, salt.length))+
|
||||
HASH_DELIM+Util.byte2str(Util.toBase64(hash, 0, hash.length));
|
||||
hashed=true;
|
||||
}
|
||||
}
|
||||
}
|
@ -1,54 +0,0 @@
|
||||
/* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
|
||||
/*
|
||||
Copyright (c) 2006-2011 ymnk, JCraft,Inc. All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice,
|
||||
this list of conditions and the following disclaimer.
|
||||
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the distribution.
|
||||
|
||||
3. The names of the authors may not be used to endorse or promote products
|
||||
derived from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
|
||||
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
|
||||
INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
|
||||
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
||||
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
package com.jcraft.jsch;
|
||||
|
||||
public interface Logger{
|
||||
|
||||
public final int DEBUG=0;
|
||||
public final int INFO=1;
|
||||
public final int WARN=2;
|
||||
public final int ERROR=3;
|
||||
public final int FATAL=4;
|
||||
|
||||
public boolean isEnabled(int level);
|
||||
|
||||
public void log(int level, String message);
|
||||
|
||||
/*
|
||||
public final Logger SIMPLE_LOGGER=new Logger(){
|
||||
public boolean isEnabled(int level){return true;}
|
||||
public void log(int level, String message){System.err.println(message);}
|
||||
};
|
||||
final Logger DEVNULL=new Logger(){
|
||||
public boolean isEnabled(int level){return false;}
|
||||
public void log(int level, String message){}
|
||||
};
|
||||
*/
|
||||
}
|
@ -1,39 +0,0 @@
|
||||
/* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
|
||||
/*
|
||||
Copyright (c) 2002-2011 ymnk, JCraft,Inc. All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice,
|
||||
this list of conditions and the following disclaimer.
|
||||
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the distribution.
|
||||
|
||||
3. The names of the authors may not be used to endorse or promote products
|
||||
derived from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
|
||||
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
|
||||
INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
|
||||
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
||||
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
package com.jcraft.jsch;
|
||||
|
||||
public interface MAC{
|
||||
String getName();
|
||||
int getBlockSize();
|
||||
void init(byte[] key) throws Exception;
|
||||
void update(byte[] foo, int start, int len);
|
||||
void update(int foo);
|
||||
void doFinal(byte[] buf, int offset);
|
||||
}
|
@ -1,115 +0,0 @@
|
||||
/* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
|
||||
/*
|
||||
Copyright (c) 2002-2011 ymnk, JCraft,Inc. All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice,
|
||||
this list of conditions and the following disclaimer.
|
||||
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the distribution.
|
||||
|
||||
3. The names of the authors may not be used to endorse or promote products
|
||||
derived from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
|
||||
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
|
||||
INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
|
||||
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
||||
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
package com.jcraft.jsch;
|
||||
|
||||
public class Packet{
|
||||
|
||||
private static Random random=null;
|
||||
static void setRandom(Random foo){ random=foo;}
|
||||
|
||||
Buffer buffer;
|
||||
byte[] ba4=new byte[4];
|
||||
public Packet(Buffer buffer){
|
||||
this.buffer=buffer;
|
||||
}
|
||||
public void reset(){
|
||||
buffer.index=5;
|
||||
}
|
||||
void padding(int bsize){
|
||||
int len=buffer.index;
|
||||
int pad=(-len)&(bsize-1);
|
||||
if(pad<bsize){
|
||||
pad+=bsize;
|
||||
}
|
||||
len=len+pad-4;
|
||||
ba4[0]=(byte)(len>>>24);
|
||||
ba4[1]=(byte)(len>>>16);
|
||||
ba4[2]=(byte)(len>>>8);
|
||||
ba4[3]=(byte)(len);
|
||||
System.arraycopy(ba4, 0, buffer.buffer, 0, 4);
|
||||
buffer.buffer[4]=(byte)pad;
|
||||
synchronized(random){
|
||||
random.fill(buffer.buffer, buffer.index, pad);
|
||||
}
|
||||
buffer.skip(pad);
|
||||
//buffer.putPad(pad);
|
||||
/*
|
||||
for(int i=0; i<buffer.index; i++){
|
||||
System.err.print(Integer.toHexString(buffer.buffer[i]&0xff)+":");
|
||||
}
|
||||
System.err.println("");
|
||||
*/
|
||||
}
|
||||
|
||||
int shift(int len, int bsize, int mac){
|
||||
int s=len+5+9;
|
||||
int pad=(-s)&(bsize-1);
|
||||
if(pad<bsize)pad+=bsize;
|
||||
s+=pad;
|
||||
s+=mac;
|
||||
s+=32; // margin for deflater; deflater may inflate data
|
||||
|
||||
/**/
|
||||
if(buffer.buffer.length<s+buffer.index-5-9-len){
|
||||
byte[] foo=new byte[s+buffer.index-5-9-len];
|
||||
System.arraycopy(buffer.buffer, 0, foo, 0, buffer.buffer.length);
|
||||
buffer.buffer=foo;
|
||||
}
|
||||
/**/
|
||||
|
||||
//if(buffer.buffer.length<len+5+9)
|
||||
// System.err.println("buffer.buffer.length="+buffer.buffer.length+" len+5+9="+(len+5+9));
|
||||
|
||||
//if(buffer.buffer.length<s)
|
||||
// System.err.println("buffer.buffer.length="+buffer.buffer.length+" s="+(s));
|
||||
|
||||
System.arraycopy(buffer.buffer,
|
||||
len+5+9,
|
||||
buffer.buffer, s, buffer.index-5-9-len);
|
||||
|
||||
buffer.index=10;
|
||||
buffer.putInt(len);
|
||||
buffer.index=len+5+9;
|
||||
return s;
|
||||
}
|
||||
void unshift(byte command, int recipient, int s, int len){
|
||||
System.arraycopy(buffer.buffer,
|
||||
s,
|
||||
buffer.buffer, 5+9, len);
|
||||
buffer.buffer[5]=command;
|
||||
buffer.index=6;
|
||||
buffer.putInt(recipient);
|
||||
buffer.putInt(len);
|
||||
buffer.index=len+5+9;
|
||||
}
|
||||
Buffer getBuffer(){
|
||||
return buffer;
|
||||
}
|
||||
}
|
@ -1,194 +0,0 @@
|
||||
/* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
|
||||
/*
|
||||
Copyright (c) 2002-2011 ymnk, JCraft,Inc. All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice,
|
||||
this list of conditions and the following disclaimer.
|
||||
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the distribution.
|
||||
|
||||
3. The names of the authors may not be used to endorse or promote products
|
||||
derived from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
|
||||
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
|
||||
INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
|
||||
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
||||
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
package com.jcraft.jsch;
|
||||
|
||||
import java.net.*;
|
||||
import java.io.*;
|
||||
|
||||
class PortWatcher implements Runnable{
|
||||
private static java.util.Vector pool=new java.util.Vector();
|
||||
private static InetAddress anyLocalAddress=null;
|
||||
static{
|
||||
// 0.0.0.0
|
||||
/*
|
||||
try{ anyLocalAddress=InetAddress.getByAddress(new byte[4]); }
|
||||
catch(UnknownHostException e){
|
||||
}
|
||||
*/
|
||||
try{ anyLocalAddress=InetAddress.getByName("0.0.0.0"); }
|
||||
catch(UnknownHostException e){
|
||||
}
|
||||
}
|
||||
|
||||
Session session;
|
||||
int lport;
|
||||
int rport;
|
||||
String host;
|
||||
InetAddress boundaddress;
|
||||
Runnable thread;
|
||||
ServerSocket ss;
|
||||
|
||||
static String[] getPortForwarding(Session session){
|
||||
java.util.Vector foo=new java.util.Vector();
|
||||
synchronized(pool){
|
||||
for(int i=0; i<pool.size(); i++){
|
||||
PortWatcher p=(PortWatcher)(pool.elementAt(i));
|
||||
if(p.session==session){
|
||||
foo.addElement(p.lport+":"+p.host+":"+p.rport);
|
||||
}
|
||||
}
|
||||
}
|
||||
String[] bar=new String[foo.size()];
|
||||
for(int i=0; i<foo.size(); i++){
|
||||
bar[i]=(String)(foo.elementAt(i));
|
||||
}
|
||||
return bar;
|
||||
}
|
||||
static PortWatcher getPort(Session session, String address, int lport) throws JSchException{
|
||||
InetAddress addr;
|
||||
try{
|
||||
addr=InetAddress.getByName(address);
|
||||
}
|
||||
catch(UnknownHostException uhe){
|
||||
throw new JSchException("PortForwardingL: invalid address "+address+" specified.", uhe);
|
||||
}
|
||||
synchronized(pool){
|
||||
for(int i=0; i<pool.size(); i++){
|
||||
PortWatcher p=(PortWatcher)(pool.elementAt(i));
|
||||
if(p.session==session && p.lport==lport){
|
||||
if(/*p.boundaddress.isAnyLocalAddress() ||*/
|
||||
(anyLocalAddress!=null && p.boundaddress.equals(anyLocalAddress)) ||
|
||||
p.boundaddress.equals(addr))
|
||||
return p;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
static PortWatcher addPort(Session session, String address, int lport, String host, int rport, ServerSocketFactory ssf) throws JSchException{
|
||||
if(getPort(session, address, lport)!=null){
|
||||
throw new JSchException("PortForwardingL: local port "+ address+":"+lport+" is already registered.");
|
||||
}
|
||||
PortWatcher pw=new PortWatcher(session, address, lport, host, rport, ssf);
|
||||
pool.addElement(pw);
|
||||
return pw;
|
||||
}
|
||||
static void delPort(Session session, String address, int lport) throws JSchException{
|
||||
PortWatcher pw=getPort(session, address, lport);
|
||||
if(pw==null){
|
||||
throw new JSchException("PortForwardingL: local port "+address+":"+lport+" is not registered.");
|
||||
}
|
||||
pw.delete();
|
||||
pool.removeElement(pw);
|
||||
}
|
||||
static void delPort(Session session){
|
||||
synchronized(pool){
|
||||
PortWatcher[] foo=new PortWatcher[pool.size()];
|
||||
int count=0;
|
||||
for(int i=0; i<pool.size(); i++){
|
||||
PortWatcher p=(PortWatcher)(pool.elementAt(i));
|
||||
if(p.session==session) {
|
||||
p.delete();
|
||||
foo[count++]=p;
|
||||
}
|
||||
}
|
||||
for(int i=0; i<count; i++){
|
||||
PortWatcher p=foo[i];
|
||||
pool.removeElement(p);
|
||||
}
|
||||
}
|
||||
}
|
||||
PortWatcher(Session session,
|
||||
String address, int lport,
|
||||
String host, int rport,
|
||||
ServerSocketFactory factory) throws JSchException{
|
||||
this.session=session;
|
||||
this.lport=lport;
|
||||
this.host=host;
|
||||
this.rport=rport;
|
||||
try{
|
||||
boundaddress=InetAddress.getByName(address);
|
||||
ss=(factory==null) ?
|
||||
new ServerSocket(lport, 0, boundaddress) :
|
||||
factory.createServerSocket(lport, 0, boundaddress);
|
||||
}
|
||||
catch(Exception e){
|
||||
//System.err.println(e);
|
||||
String message="PortForwardingL: local port "+address+":"+lport+" cannot be bound.";
|
||||
if(e instanceof Throwable)
|
||||
throw new JSchException(message, (Throwable)e);
|
||||
throw new JSchException(message);
|
||||
}
|
||||
if(lport==0){
|
||||
int assigned=ss.getLocalPort();
|
||||
if(assigned!=-1)
|
||||
this.lport=assigned;
|
||||
}
|
||||
}
|
||||
|
||||
public void run(){
|
||||
thread=this;
|
||||
try{
|
||||
while(thread!=null){
|
||||
Socket socket=ss.accept();
|
||||
socket.setTcpNoDelay(true);
|
||||
InputStream in=socket.getInputStream();
|
||||
OutputStream out=socket.getOutputStream();
|
||||
ChannelDirectTCPIP channel=new ChannelDirectTCPIP();
|
||||
channel.init();
|
||||
channel.setInputStream(in);
|
||||
channel.setOutputStream(out);
|
||||
session.addChannel(channel);
|
||||
((ChannelDirectTCPIP)channel).setHost(host);
|
||||
((ChannelDirectTCPIP)channel).setPort(rport);
|
||||
((ChannelDirectTCPIP)channel).setOrgIPAddress(socket.getInetAddress().getHostAddress());
|
||||
((ChannelDirectTCPIP)channel).setOrgPort(socket.getPort());
|
||||
channel.connect();
|
||||
if(channel.exitstatus!=-1){
|
||||
}
|
||||
}
|
||||
}
|
||||
catch(Exception e){
|
||||
//System.err.println("! "+e);
|
||||
}
|
||||
|
||||
delete();
|
||||
}
|
||||
|
||||
void delete(){
|
||||
thread=null;
|
||||
try{
|
||||
if(ss!=null)ss.close();
|
||||
ss=null;
|
||||
}
|
||||
catch(Exception e){
|
||||
}
|
||||
}
|
||||
}
|
@ -1,40 +0,0 @@
|
||||
/* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
|
||||
/*
|
||||
Copyright (c) 2002-2011 ymnk, JCraft,Inc. All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice,
|
||||
this list of conditions and the following disclaimer.
|
||||
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the distribution.
|
||||
|
||||
3. The names of the authors may not be used to endorse or promote products
|
||||
derived from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
|
||||
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
|
||||
INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
|
||||
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
||||
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
package com.jcraft.jsch;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.Socket;
|
||||
public interface Proxy{
|
||||
void connect(SocketFactory socket_factory, String host, int port, int timeout) throws Exception;
|
||||
InputStream getInputStream();
|
||||
OutputStream getOutputStream();
|
||||
Socket getSocket();
|
||||
void close();
|
||||
}
|
@ -1,180 +0,0 @@
|
||||
/* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
|
||||
/*
|
||||
Copyright (c) 2002-2011 ymnk, JCraft,Inc. All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice,
|
||||
this list of conditions and the following disclaimer.
|
||||
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the distribution.
|
||||
|
||||
3. The names of the authors may not be used to endorse or promote products
|
||||
derived from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
|
||||
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
|
||||
INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
|
||||
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
||||
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
package com.jcraft.jsch;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
|
||||
public class ProxyHTTP implements Proxy{
|
||||
private static int DEFAULTPORT=80;
|
||||
private String proxy_host;
|
||||
private int proxy_port;
|
||||
private InputStream in;
|
||||
private OutputStream out;
|
||||
private Socket socket;
|
||||
|
||||
private String user;
|
||||
private String passwd;
|
||||
|
||||
public ProxyHTTP(String proxy_host){
|
||||
int port=DEFAULTPORT;
|
||||
String host=proxy_host;
|
||||
if(proxy_host.indexOf(':')!=-1){
|
||||
try{
|
||||
host=proxy_host.substring(0, proxy_host.indexOf(':'));
|
||||
port=Integer.parseInt(proxy_host.substring(proxy_host.indexOf(':')+1));
|
||||
}
|
||||
catch(Exception e){
|
||||
}
|
||||
}
|
||||
this.proxy_host=host;
|
||||
this.proxy_port=port;
|
||||
}
|
||||
public ProxyHTTP(String proxy_host, int proxy_port){
|
||||
this.proxy_host=proxy_host;
|
||||
this.proxy_port=proxy_port;
|
||||
}
|
||||
public void setUserPasswd(String user, String passwd){
|
||||
this.user=user;
|
||||
this.passwd=passwd;
|
||||
}
|
||||
public void connect(SocketFactory socket_factory, String host, int port, int timeout) throws JSchException{
|
||||
try{
|
||||
if(socket_factory==null){
|
||||
socket=Util.createSocket(proxy_host, proxy_port, timeout);
|
||||
in=socket.getInputStream();
|
||||
out=socket.getOutputStream();
|
||||
}
|
||||
else{
|
||||
socket=socket_factory.createSocket(proxy_host, proxy_port);
|
||||
in=socket_factory.getInputStream(socket);
|
||||
out=socket_factory.getOutputStream(socket);
|
||||
}
|
||||
if(timeout>0){
|
||||
socket.setSoTimeout(timeout);
|
||||
}
|
||||
socket.setTcpNoDelay(true);
|
||||
|
||||
out.write(Util.str2byte("CONNECT "+host+":"+port+" HTTP/1.0\r\n"));
|
||||
|
||||
if(user!=null && passwd!=null){
|
||||
byte[] code=Util.str2byte(user+":"+passwd);
|
||||
code=Util.toBase64(code, 0, code.length);
|
||||
out.write(Util.str2byte("Proxy-Authorization: Basic "));
|
||||
out.write(code);
|
||||
out.write(Util.str2byte("\r\n"));
|
||||
}
|
||||
|
||||
out.write(Util.str2byte("\r\n"));
|
||||
out.flush();
|
||||
|
||||
int foo=0;
|
||||
|
||||
StringBuffer sb=new StringBuffer();
|
||||
while(foo>=0){
|
||||
foo=in.read(); if(foo!=13){sb.append((char)foo); continue;}
|
||||
foo=in.read(); if(foo!=10){continue;}
|
||||
break;
|
||||
}
|
||||
if(foo<0){
|
||||
throw new IOException();
|
||||
}
|
||||
|
||||
String response=sb.toString();
|
||||
String reason="Unknow reason";
|
||||
int code=-1;
|
||||
try{
|
||||
foo=response.indexOf(' ');
|
||||
int bar=response.indexOf(' ', foo+1);
|
||||
code=Integer.parseInt(response.substring(foo+1, bar));
|
||||
reason=response.substring(bar+1);
|
||||
}
|
||||
catch(Exception e){
|
||||
}
|
||||
if(code!=200){
|
||||
throw new IOException("proxy error: "+reason);
|
||||
}
|
||||
|
||||
/*
|
||||
while(foo>=0){
|
||||
foo=in.read(); if(foo!=13) continue;
|
||||
foo=in.read(); if(foo!=10) continue;
|
||||
foo=in.read(); if(foo!=13) continue;
|
||||
foo=in.read(); if(foo!=10) continue;
|
||||
break;
|
||||
}
|
||||
*/
|
||||
|
||||
int count=0;
|
||||
while(true){
|
||||
count=0;
|
||||
while(foo>=0){
|
||||
foo=in.read(); if(foo!=13){count++; continue;}
|
||||
foo=in.read(); if(foo!=10){continue;}
|
||||
break;
|
||||
}
|
||||
if(foo<0){
|
||||
throw new IOException();
|
||||
}
|
||||
if(count==0)break;
|
||||
}
|
||||
}
|
||||
catch(RuntimeException e){
|
||||
throw e;
|
||||
}
|
||||
catch(Exception e){
|
||||
try{ if(socket!=null)socket.close(); }
|
||||
catch(Exception eee){
|
||||
}
|
||||
String message="ProxyHTTP: "+e.toString();
|
||||
if(e instanceof Throwable)
|
||||
throw new JSchException(message, (Throwable)e);
|
||||
throw new JSchException(message);
|
||||
}
|
||||
}
|
||||
public InputStream getInputStream(){ return in; }
|
||||
public OutputStream getOutputStream(){ return out; }
|
||||
public Socket getSocket(){ return socket; }
|
||||
public void close(){
|
||||
try{
|
||||
if(in!=null)in.close();
|
||||
if(out!=null)out.close();
|
||||
if(socket!=null)socket.close();
|
||||
}
|
||||
catch(Exception e){
|
||||
}
|
||||
in=null;
|
||||
out=null;
|
||||
socket=null;
|
||||
}
|
||||
public static int getDefaultPort(){
|
||||
return DEFAULTPORT;
|
||||
}
|
||||
}
|
@ -1,212 +0,0 @@
|
||||
/* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
|
||||
/*
|
||||
Copyright (c) 2006-2011 ymnk, JCraft,Inc. All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice,
|
||||
this list of conditions and the following disclaimer.
|
||||
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the distribution.
|
||||
|
||||
3. The names of the authors may not be used to endorse or promote products
|
||||
derived from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
|
||||
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
|
||||
INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
|
||||
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
||||
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
/*
|
||||
This file depends on following documents,
|
||||
- SOCKS: A protocol for TCP proxy across firewalls, Ying-Da Lee
|
||||
http://www.socks.nec.com/protocol/socks4.protocol
|
||||
*/
|
||||
|
||||
package com.jcraft.jsch;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
|
||||
public class ProxySOCKS4 implements Proxy{
|
||||
private static int DEFAULTPORT=1080;
|
||||
private String proxy_host;
|
||||
private int proxy_port;
|
||||
private InputStream in;
|
||||
private OutputStream out;
|
||||
private Socket socket;
|
||||
private String user;
|
||||
private String passwd;
|
||||
|
||||
public ProxySOCKS4(String proxy_host){
|
||||
int port=DEFAULTPORT;
|
||||
String host=proxy_host;
|
||||
if(proxy_host.indexOf(':')!=-1){
|
||||
try{
|
||||
host=proxy_host.substring(0, proxy_host.indexOf(':'));
|
||||
port=Integer.parseInt(proxy_host.substring(proxy_host.indexOf(':')+1));
|
||||
}
|
||||
catch(Exception e){
|
||||
}
|
||||
}
|
||||
this.proxy_host=host;
|
||||
this.proxy_port=port;
|
||||
}
|
||||
public ProxySOCKS4(String proxy_host, int proxy_port){
|
||||
this.proxy_host=proxy_host;
|
||||
this.proxy_port=proxy_port;
|
||||
}
|
||||
public void setUserPasswd(String user, String passwd){
|
||||
this.user=user;
|
||||
this.passwd=passwd;
|
||||
}
|
||||
public void connect(SocketFactory socket_factory, String host, int port, int timeout) throws JSchException{
|
||||
try{
|
||||
if(socket_factory==null){
|
||||
socket=Util.createSocket(proxy_host, proxy_port, timeout);
|
||||
//socket=new Socket(proxy_host, proxy_port);
|
||||
in=socket.getInputStream();
|
||||
out=socket.getOutputStream();
|
||||
}
|
||||
else{
|
||||
socket=socket_factory.createSocket(proxy_host, proxy_port);
|
||||
in=socket_factory.getInputStream(socket);
|
||||
out=socket_factory.getOutputStream(socket);
|
||||
}
|
||||
if(timeout>0){
|
||||
socket.setSoTimeout(timeout);
|
||||
}
|
||||
socket.setTcpNoDelay(true);
|
||||
|
||||
byte[] buf=new byte[1024];
|
||||
int index=0;
|
||||
|
||||
/*
|
||||
1) CONNECT
|
||||
|
||||
The client connects to the SOCKS server and sends a CONNECT request when
|
||||
it wants to establish a connection to an application server. The client
|
||||
includes in the request packet the IP address and the port number of the
|
||||
destination host, and userid, in the following format.
|
||||
|
||||
+----+----+----+----+----+----+----+----+----+----+....+----+
|
||||
| VN | CD | DSTPORT | DSTIP | USERID |NULL|
|
||||
+----+----+----+----+----+----+----+----+----+----+....+----+
|
||||
# of bytes: 1 1 2 4 variable 1
|
||||
|
||||
VN is the SOCKS protocol version number and should be 4. CD is the
|
||||
SOCKS command code and should be 1 for CONNECT request. NULL is a byte
|
||||
of all zero bits.
|
||||
*/
|
||||
|
||||
index=0;
|
||||
buf[index++]=4;
|
||||
buf[index++]=1;
|
||||
|
||||
buf[index++]=(byte)(port>>>8);
|
||||
buf[index++]=(byte)(port&0xff);
|
||||
|
||||
try{
|
||||
InetAddress addr=InetAddress.getByName(host);
|
||||
byte[] byteAddress = addr.getAddress();
|
||||
for (int i = 0; i < byteAddress.length; i++) {
|
||||
buf[index++]=byteAddress[i];
|
||||
}
|
||||
}
|
||||
catch(UnknownHostException uhe){
|
||||
throw new JSchException("ProxySOCKS4: "+uhe.toString(), uhe);
|
||||
}
|
||||
|
||||
if(user!=null){
|
||||
System.arraycopy(Util.str2byte(user), 0, buf, index, user.length());
|
||||
index+=user.length();
|
||||
}
|
||||
buf[index++]=0;
|
||||
out.write(buf, 0, index);
|
||||
|
||||
/*
|
||||
The SOCKS server checks to see whether such a request should be granted
|
||||
based on any combination of source IP address, destination IP address,
|
||||
destination port number, the userid, and information it may obtain by
|
||||
consulting IDENT, cf. RFC 1413. If the request is granted, the SOCKS
|
||||
server makes a connection to the specified port of the destination host.
|
||||
A reply packet is sent to the client when this connection is established,
|
||||
or when the request is rejected or the operation fails.
|
||||
|
||||
+----+----+----+----+----+----+----+----+
|
||||
| VN | CD | DSTPORT | DSTIP |
|
||||
+----+----+----+----+----+----+----+----+
|
||||
# of bytes: 1 1 2 4
|
||||
|
||||
VN is the version of the reply code and should be 0. CD is the result
|
||||
code with one of the following values:
|
||||
|
||||
90: request granted
|
||||
91: request rejected or failed
|
||||
92: request rejected becasue SOCKS server cannot connect to
|
||||
identd on the client
|
||||
93: request rejected because the client program and identd
|
||||
report different user-ids
|
||||
|
||||
The remaining fields are ignored.
|
||||
*/
|
||||
|
||||
int len=8;
|
||||
int s=0;
|
||||
while(s<len){
|
||||
int i=in.read(buf, s, len-s);
|
||||
if(i<=0){
|
||||
throw new JSchException("ProxySOCKS4: stream is closed");
|
||||
}
|
||||
s+=i;
|
||||
}
|
||||
if(buf[0]!=0){
|
||||
throw new JSchException("ProxySOCKS4: server returns VN "+buf[0]);
|
||||
}
|
||||
if(buf[1]!=90){
|
||||
try{ socket.close(); }
|
||||
catch(Exception eee){
|
||||
}
|
||||
String message="ProxySOCKS4: server returns CD "+buf[1];
|
||||
throw new JSchException(message);
|
||||
}
|
||||
}
|
||||
catch(RuntimeException e){
|
||||
throw e;
|
||||
}
|
||||
catch(Exception e){
|
||||
try{ if(socket!=null)socket.close(); }
|
||||
catch(Exception eee){
|
||||
}
|
||||
throw new JSchException("ProxySOCKS4: "+e.toString());
|
||||
}
|
||||
}
|
||||
public InputStream getInputStream(){ return in; }
|
||||
public OutputStream getOutputStream(){ return out; }
|
||||
public Socket getSocket(){ return socket; }
|
||||
public void close(){
|
||||
try{
|
||||
if(in!=null)in.close();
|
||||
if(out!=null)out.close();
|
||||
if(socket!=null)socket.close();
|
||||
}
|
||||
catch(Exception e){
|
||||
}
|
||||
in=null;
|
||||
out=null;
|
||||
socket=null;
|
||||
}
|
||||
public static int getDefaultPort(){
|
||||
return DEFAULTPORT;
|
||||
}
|
||||
}
|
@ -1,349 +0,0 @@
|
||||
/* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
|
||||
/*
|
||||
Copyright (c) 2002-2011 ymnk, JCraft,Inc. All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice,
|
||||
this list of conditions and the following disclaimer.
|
||||
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the distribution.
|
||||
|
||||
3. The names of the authors may not be used to endorse or promote products
|
||||
derived from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
|
||||
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
|
||||
INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
|
||||
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
||||
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
/*
|
||||
This file depends on following documents,
|
||||
- RFC 1928 SOCKS Protocol Verseion 5
|
||||
- RFC 1929 Username/Password Authentication for SOCKS V5.
|
||||
*/
|
||||
|
||||
package com.jcraft.jsch;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
|
||||
public class ProxySOCKS5 implements Proxy{
|
||||
private static int DEFAULTPORT=1080;
|
||||
private String proxy_host;
|
||||
private int proxy_port;
|
||||
private InputStream in;
|
||||
private OutputStream out;
|
||||
private Socket socket;
|
||||
private String user;
|
||||
private String passwd;
|
||||
|
||||
public ProxySOCKS5(String proxy_host){
|
||||
int port=DEFAULTPORT;
|
||||
String host=proxy_host;
|
||||
if(proxy_host.indexOf(':')!=-1){
|
||||
try{
|
||||
host=proxy_host.substring(0, proxy_host.indexOf(':'));
|
||||
port=Integer.parseInt(proxy_host.substring(proxy_host.indexOf(':')+1));
|
||||
}
|
||||
catch(Exception e){
|
||||
}
|
||||
}
|
||||
this.proxy_host=host;
|
||||
this.proxy_port=port;
|
||||
}
|
||||
public ProxySOCKS5(String proxy_host, int proxy_port){
|
||||
this.proxy_host=proxy_host;
|
||||
this.proxy_port=proxy_port;
|
||||
}
|
||||
public void setUserPasswd(String user, String passwd){
|
||||
this.user=user;
|
||||
this.passwd=passwd;
|
||||
}
|
||||
public void connect(SocketFactory socket_factory, String host, int port, int timeout) throws JSchException{
|
||||
try{
|
||||
if(socket_factory==null){
|
||||
socket=Util.createSocket(proxy_host, proxy_port, timeout);
|
||||
//socket=new Socket(proxy_host, proxy_port);
|
||||
in=socket.getInputStream();
|
||||
out=socket.getOutputStream();
|
||||
}
|
||||
else{
|
||||
socket=socket_factory.createSocket(proxy_host, proxy_port);
|
||||
in=socket_factory.getInputStream(socket);
|
||||
out=socket_factory.getOutputStream(socket);
|
||||
}
|
||||
if(timeout>0){
|
||||
socket.setSoTimeout(timeout);
|
||||
}
|
||||
socket.setTcpNoDelay(true);
|
||||
|
||||
byte[] buf=new byte[1024];
|
||||
int index=0;
|
||||
|
||||
/*
|
||||
+----+----------+----------+
|
||||
|VER | NMETHODS | METHODS |
|
||||
+----+----------+----------+
|
||||
| 1 | 1 | 1 to 255 |
|
||||
+----+----------+----------+
|
||||
|
||||
The VER field is set to X'05' for this version of the protocol. The
|
||||
NMETHODS field contains the number of method identifier octets that
|
||||
appear in the METHODS field.
|
||||
|
||||
The values currently defined for METHOD are:
|
||||
|
||||
o X'00' NO AUTHENTICATION REQUIRED
|
||||
o X'01' GSSAPI
|
||||
o X'02' USERNAME/PASSWORD
|
||||
o X'03' to X'7F' IANA ASSIGNED
|
||||
o X'80' to X'FE' RESERVED FOR PRIVATE METHODS
|
||||
o X'FF' NO ACCEPTABLE METHODS
|
||||
*/
|
||||
|
||||
buf[index++]=5;
|
||||
|
||||
buf[index++]=2;
|
||||
buf[index++]=0; // NO AUTHENTICATION REQUIRED
|
||||
buf[index++]=2; // USERNAME/PASSWORD
|
||||
|
||||
out.write(buf, 0, index);
|
||||
|
||||
/*
|
||||
The server selects from one of the methods given in METHODS, and
|
||||
sends a METHOD selection message:
|
||||
|
||||
+----+--------+
|
||||
|VER | METHOD |
|
||||
+----+--------+
|
||||
| 1 | 1 |
|
||||
+----+--------+
|
||||
*/
|
||||
//in.read(buf, 0, 2);
|
||||
fill(in, buf, 2);
|
||||
|
||||
boolean check=false;
|
||||
switch((buf[1])&0xff){
|
||||
case 0: // NO AUTHENTICATION REQUIRED
|
||||
check=true;
|
||||
break;
|
||||
case 2: // USERNAME/PASSWORD
|
||||
if(user==null || passwd==null)break;
|
||||
|
||||
/*
|
||||
Once the SOCKS V5 server has started, and the client has selected the
|
||||
Username/Password Authentication protocol, the Username/Password
|
||||
subnegotiation begins. This begins with the client producing a
|
||||
Username/Password request:
|
||||
|
||||
+----+------+----------+------+----------+
|
||||
|VER | ULEN | UNAME | PLEN | PASSWD |
|
||||
+----+------+----------+------+----------+
|
||||
| 1 | 1 | 1 to 255 | 1 | 1 to 255 |
|
||||
+----+------+----------+------+----------+
|
||||
|
||||
The VER field contains the current version of the subnegotiation,
|
||||
which is X'01'. The ULEN field contains the length of the UNAME field
|
||||
that follows. The UNAME field contains the username as known to the
|
||||
source operating system. The PLEN field contains the length of the
|
||||
PASSWD field that follows. The PASSWD field contains the password
|
||||
association with the given UNAME.
|
||||
*/
|
||||
index=0;
|
||||
buf[index++]=1;
|
||||
buf[index++]=(byte)(user.length());
|
||||
System.arraycopy(Util.str2byte(user), 0, buf, index, user.length());
|
||||
index+=user.length();
|
||||
buf[index++]=(byte)(passwd.length());
|
||||
System.arraycopy(Util.str2byte(passwd), 0, buf, index, passwd.length());
|
||||
index+=passwd.length();
|
||||
|
||||
out.write(buf, 0, index);
|
||||
|
||||
/*
|
||||
The server verifies the supplied UNAME and PASSWD, and sends the
|
||||
following response:
|
||||
|
||||
+----+--------+
|
||||
|VER | STATUS |
|
||||
+----+--------+
|
||||
| 1 | 1 |
|
||||
+----+--------+
|
||||
|
||||
A STATUS field of X'00' indicates success. If the server returns a
|
||||
`failure' (STATUS value other than X'00') status, it MUST close the
|
||||
connection.
|
||||
*/
|
||||
//in.read(buf, 0, 2);
|
||||
fill(in, buf, 2);
|
||||
if(buf[1]==0)
|
||||
check=true;
|
||||
break;
|
||||
default:
|
||||
}
|
||||
|
||||
if(!check){
|
||||
try{ socket.close(); }
|
||||
catch(Exception eee){
|
||||
}
|
||||
throw new JSchException("fail in SOCKS5 proxy");
|
||||
}
|
||||
|
||||
/*
|
||||
The SOCKS request is formed as follows:
|
||||
|
||||
+----+-----+-------+------+----------+----------+
|
||||
|VER | CMD | RSV | ATYP | DST.ADDR | DST.PORT |
|
||||
+----+-----+-------+------+----------+----------+
|
||||
| 1 | 1 | X'00' | 1 | Variable | 2 |
|
||||
+----+-----+-------+------+----------+----------+
|
||||
|
||||
Where:
|
||||
|
||||
o VER protocol version: X'05'
|
||||
o CMD
|
||||
o CONNECT X'01'
|
||||
o BIND X'02'
|
||||
o UDP ASSOCIATE X'03'
|
||||
o RSV RESERVED
|
||||
o ATYP address type of following address
|
||||
o IP V4 address: X'01'
|
||||
o DOMAINNAME: X'03'
|
||||
o IP V6 address: X'04'
|
||||
o DST.ADDR desired destination address
|
||||
o DST.PORT desired destination port in network octet
|
||||
order
|
||||
*/
|
||||
|
||||
index=0;
|
||||
buf[index++]=5;
|
||||
buf[index++]=1; // CONNECT
|
||||
buf[index++]=0;
|
||||
|
||||
byte[] hostb=Util.str2byte(host);
|
||||
int len=hostb.length;
|
||||
buf[index++]=3; // DOMAINNAME
|
||||
buf[index++]=(byte)(len);
|
||||
System.arraycopy(hostb, 0, buf, index, len);
|
||||
index+=len;
|
||||
buf[index++]=(byte)(port>>>8);
|
||||
buf[index++]=(byte)(port&0xff);
|
||||
|
||||
out.write(buf, 0, index);
|
||||
|
||||
/*
|
||||
The SOCKS request information is sent by the client as soon as it has
|
||||
established a connection to the SOCKS server, and completed the
|
||||
authentication negotiations. The server evaluates the request, and
|
||||
returns a reply formed as follows:
|
||||
|
||||
+----+-----+-------+------+----------+----------+
|
||||
|VER | REP | RSV | ATYP | BND.ADDR | BND.PORT |
|
||||
+----+-----+-------+------+----------+----------+
|
||||
| 1 | 1 | X'00' | 1 | Variable | 2 |
|
||||
+----+-----+-------+------+----------+----------+
|
||||
|
||||
Where:
|
||||
|
||||
o VER protocol version: X'05'
|
||||
o REP Reply field:
|
||||
o X'00' succeeded
|
||||
o X'01' general SOCKS server failure
|
||||
o X'02' connection not allowed by ruleset
|
||||
o X'03' Network unreachable
|
||||
o X'04' Host unreachable
|
||||
o X'05' Connection refused
|
||||
o X'06' TTL expired
|
||||
o X'07' Command not supported
|
||||
o X'08' Address type not supported
|
||||
o X'09' to X'FF' unassigned
|
||||
o RSV RESERVED
|
||||
o ATYP address type of following address
|
||||
o IP V4 address: X'01'
|
||||
o DOMAINNAME: X'03'
|
||||
o IP V6 address: X'04'
|
||||
o BND.ADDR server bound address
|
||||
o BND.PORT server bound port in network octet order
|
||||
*/
|
||||
|
||||
//in.read(buf, 0, 4);
|
||||
fill(in, buf, 4);
|
||||
|
||||
if(buf[1]!=0){
|
||||
try{ socket.close(); }
|
||||
catch(Exception eee){
|
||||
}
|
||||
throw new JSchException("ProxySOCKS5: server returns "+buf[1]);
|
||||
}
|
||||
|
||||
switch(buf[3]&0xff){
|
||||
case 1:
|
||||
//in.read(buf, 0, 6);
|
||||
fill(in, buf, 6);
|
||||
break;
|
||||
case 3:
|
||||
//in.read(buf, 0, 1);
|
||||
fill(in, buf, 1);
|
||||
//in.read(buf, 0, buf[0]+2);
|
||||
fill(in, buf, (buf[0]&0xff)+2);
|
||||
break;
|
||||
case 4:
|
||||
//in.read(buf, 0, 18);
|
||||
fill(in, buf, 18);
|
||||
break;
|
||||
default:
|
||||
}
|
||||
}
|
||||
catch(RuntimeException e){
|
||||
throw e;
|
||||
}
|
||||
catch(Exception e){
|
||||
try{ if(socket!=null)socket.close(); }
|
||||
catch(Exception eee){
|
||||
}
|
||||
String message="ProxySOCKS5: "+e.toString();
|
||||
if(e instanceof Throwable)
|
||||
throw new JSchException(message, (Throwable)e);
|
||||
throw new JSchException(message);
|
||||
}
|
||||
}
|
||||
public InputStream getInputStream(){ return in; }
|
||||
public OutputStream getOutputStream(){ return out; }
|
||||
public Socket getSocket(){ return socket; }
|
||||
public void close(){
|
||||
try{
|
||||
if(in!=null)in.close();
|
||||
if(out!=null)out.close();
|
||||
if(socket!=null)socket.close();
|
||||
}
|
||||
catch(Exception e){
|
||||
}
|
||||
in=null;
|
||||
out=null;
|
||||
socket=null;
|
||||
}
|
||||
public static int getDefaultPort(){
|
||||
return DEFAULTPORT;
|
||||
}
|
||||
private void fill(InputStream in, byte[] buf, int len) throws JSchException, IOException{
|
||||
int s=0;
|
||||
while(s<len){
|
||||
int i=in.read(buf, s, len-s);
|
||||
if(i<=0){
|
||||
throw new JSchException("ProxySOCKS5: stream is closed");
|
||||
}
|
||||
s+=i;
|
||||
}
|
||||
}
|
||||
}
|
@ -1,34 +0,0 @@
|
||||
/* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
|
||||
/*
|
||||
Copyright (c) 2002-2011 ymnk, JCraft,Inc. All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice,
|
||||
this list of conditions and the following disclaimer.
|
||||
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the distribution.
|
||||
|
||||
3. The names of the authors may not be used to endorse or promote products
|
||||
derived from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
|
||||
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
|
||||
INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
|
||||
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
||||
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
package com.jcraft.jsch;
|
||||
|
||||
public interface Random{
|
||||
void fill(byte[] foo, int start, int len);
|
||||
}
|
@ -1,69 +0,0 @@
|
||||
/* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
|
||||
/*
|
||||
Copyright (c) 2002-2011 ymnk, JCraft,Inc. All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice,
|
||||
this list of conditions and the following disclaimer.
|
||||
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the distribution.
|
||||
|
||||
3. The names of the authors may not be used to endorse or promote products
|
||||
derived from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
|
||||
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
|
||||
INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
|
||||
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
||||
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
package com.jcraft.jsch;
|
||||
|
||||
abstract class Request{
|
||||
private boolean reply=false;
|
||||
private Session session=null;
|
||||
private Channel channel=null;
|
||||
void request(Session session, Channel channel) throws Exception{
|
||||
this.session=session;
|
||||
this.channel=channel;
|
||||
if(channel.connectTimeout>0){
|
||||
setReply(true);
|
||||
}
|
||||
}
|
||||
boolean waitForReply(){ return reply; }
|
||||
void setReply(boolean reply){ this.reply=reply; }
|
||||
void write(Packet packet) throws Exception{
|
||||
if(reply){
|
||||
channel.reply=-1;
|
||||
}
|
||||
session.write(packet);
|
||||
if(reply){
|
||||
long start=System.currentTimeMillis();
|
||||
long timeout=channel.connectTimeout;
|
||||
while(channel.isConnected() && channel.reply==-1){
|
||||
try{Thread.sleep(10);}
|
||||
catch(Exception ee){
|
||||
}
|
||||
if(timeout>0L &&
|
||||
(System.currentTimeMillis()-start)>timeout){
|
||||
channel.reply=0;
|
||||
throw new JSchException("channel request: timeout");
|
||||
}
|
||||
}
|
||||
|
||||
if(channel.reply==0){
|
||||
throw new JSchException("failed to send channel request");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,53 +0,0 @@
|
||||
/* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
|
||||
/*
|
||||
Copyright (c) 2006-2011 ymnk, JCraft,Inc. All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice,
|
||||
this list of conditions and the following disclaimer.
|
||||
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the distribution.
|
||||
|
||||
3. The names of the authors may not be used to endorse or promote products
|
||||
derived from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
|
||||
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
|
||||
INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
|
||||
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
||||
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
package com.jcraft.jsch;
|
||||
|
||||
class RequestAgentForwarding extends Request{
|
||||
public void request(Session session, Channel channel) throws Exception{
|
||||
super.request(session, channel);
|
||||
|
||||
setReply(false);
|
||||
|
||||
Buffer buf=new Buffer();
|
||||
Packet packet=new Packet(buf);
|
||||
|
||||
// byte SSH_MSG_CHANNEL_REQUEST(98)
|
||||
// uint32 recipient channel
|
||||
// string request type // "auth-agent-req@openssh.com"
|
||||
// boolean want reply // 0
|
||||
packet.reset();
|
||||
buf.putByte((byte) Session.SSH_MSG_CHANNEL_REQUEST);
|
||||
buf.putInt(channel.getRecipient());
|
||||
buf.putString(Util.str2byte("auth-agent-req@openssh.com"));
|
||||
buf.putByte((byte)(waitForReply() ? 1 : 0));
|
||||
write(packet);
|
||||
session.agent_forwarding=true;
|
||||
}
|
||||
}
|
@ -1,54 +0,0 @@
|
||||
/* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
|
||||
/*
|
||||
Copyright (c) 2002-2011 ymnk, JCraft,Inc. All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice,
|
||||
this list of conditions and the following disclaimer.
|
||||
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the distribution.
|
||||
|
||||
3. The names of the authors may not be used to endorse or promote products
|
||||
derived from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
|
||||
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
|
||||
INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
|
||||
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
||||
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
package com.jcraft.jsch;
|
||||
|
||||
class RequestEnv extends Request{
|
||||
byte[] name=new byte[0];
|
||||
byte[] value=new byte[0];
|
||||
void setEnv(byte[] name, byte[] value){
|
||||
this.name=name;
|
||||
this.value=value;
|
||||
}
|
||||
public void request(Session session, Channel channel) throws Exception{
|
||||
super.request(session, channel);
|
||||
|
||||
Buffer buf=new Buffer();
|
||||
Packet packet=new Packet(buf);
|
||||
|
||||
packet.reset();
|
||||
buf.putByte((byte) Session.SSH_MSG_CHANNEL_REQUEST);
|
||||
buf.putInt(channel.getRecipient());
|
||||
buf.putString(Util.str2byte("env"));
|
||||
buf.putByte((byte)(waitForReply() ? 1 : 0));
|
||||
buf.putString(name);
|
||||
buf.putString(value);
|
||||
write(packet);
|
||||
}
|
||||
}
|
@ -1,58 +0,0 @@
|
||||
/* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
|
||||
/*
|
||||
Copyright (c) 2002-2011 ymnk, JCraft,Inc. All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice,
|
||||
this list of conditions and the following disclaimer.
|
||||
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the distribution.
|
||||
|
||||
3. The names of the authors may not be used to endorse or promote products
|
||||
derived from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
|
||||
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
|
||||
INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
|
||||
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
||||
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
package com.jcraft.jsch;
|
||||
|
||||
class RequestExec extends Request{
|
||||
private byte[] command=new byte[0];
|
||||
RequestExec(byte[] command){
|
||||
this.command=command;
|
||||
}
|
||||
public void request(Session session, Channel channel) throws Exception{
|
||||
super.request(session, channel);
|
||||
|
||||
Buffer buf=new Buffer();
|
||||
Packet packet=new Packet(buf);
|
||||
|
||||
// send
|
||||
// byte SSH_MSG_CHANNEL_REQUEST(98)
|
||||
// uint32 recipient channel
|
||||
// string request type // "exec"
|
||||
// boolean want reply // 0
|
||||
// string command
|
||||
packet.reset();
|
||||
buf.putByte((byte) Session.SSH_MSG_CHANNEL_REQUEST);
|
||||
buf.putInt(channel.getRecipient());
|
||||
buf.putString(Util.str2byte("exec"));
|
||||
buf.putByte((byte)(waitForReply() ? 1 : 0));
|
||||
buf.checkFreeSize(4+command.length);
|
||||
buf.putString(command);
|
||||
write(packet);
|
||||
}
|
||||
}
|
@ -1,78 +0,0 @@
|
||||
/* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
|
||||
/*
|
||||
Copyright (c) 2002-2011 ymnk, JCraft,Inc. All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice,
|
||||
this list of conditions and the following disclaimer.
|
||||
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the distribution.
|
||||
|
||||
3. The names of the authors may not be used to endorse or promote products
|
||||
derived from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
|
||||
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
|
||||
INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
|
||||
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
||||
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
package com.jcraft.jsch;
|
||||
|
||||
class RequestPtyReq extends Request{
|
||||
private String ttype="vt100";
|
||||
private int tcol=80;
|
||||
private int trow=24;
|
||||
private int twp=640;
|
||||
private int thp=480;
|
||||
|
||||
private byte[] terminal_mode=Util.empty;
|
||||
|
||||
void setCode(String cookie){
|
||||
}
|
||||
|
||||
void setTType(String ttype){
|
||||
this.ttype=ttype;
|
||||
}
|
||||
|
||||
void setTerminalMode(byte[] terminal_mode){
|
||||
this.terminal_mode=terminal_mode;
|
||||
}
|
||||
|
||||
void setTSize(int tcol, int trow, int twp, int thp){
|
||||
this.tcol=tcol;
|
||||
this.trow=trow;
|
||||
this.twp=twp;
|
||||
this.thp=thp;
|
||||
}
|
||||
|
||||
public void request(Session session, Channel channel) throws Exception{
|
||||
super.request(session, channel);
|
||||
|
||||
Buffer buf=new Buffer();
|
||||
Packet packet=new Packet(buf);
|
||||
|
||||
packet.reset();
|
||||
buf.putByte((byte) Session.SSH_MSG_CHANNEL_REQUEST);
|
||||
buf.putInt(channel.getRecipient());
|
||||
buf.putString(Util.str2byte("pty-req"));
|
||||
buf.putByte((byte)(waitForReply() ? 1 : 0));
|
||||
buf.putString(Util.str2byte(ttype));
|
||||
buf.putInt(tcol);
|
||||
buf.putInt(trow);
|
||||
buf.putInt(twp);
|
||||
buf.putInt(thp);
|
||||
buf.putString(terminal_mode);
|
||||
write(packet);
|
||||
}
|
||||
}
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue
Block a user