1 | /******************************************************************************
|
---|
2 | *
|
---|
3 | * Copyright (c) 1998,99 by Mindbright Technology AB, Stockholm, Sweden.
|
---|
4 | * www.mindbright.se, [email protected]
|
---|
5 | *
|
---|
6 | * This program is free software; you can redistribute it and/or modify
|
---|
7 | * it under the terms of the GNU General Public License as published by
|
---|
8 | * the Free Software Foundation; either version 2 of the License, or
|
---|
9 | * (at your option) any later version.
|
---|
10 | *
|
---|
11 | * This program is distributed in the hope that it will be useful,
|
---|
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
14 | * GNU General Public License for more details.
|
---|
15 | *
|
---|
16 | *****************************************************************************
|
---|
17 | * $Author: mats $
|
---|
18 | * $Date: 2000/03/02 19:52:23 $
|
---|
19 | * $Name: rel1-2-1 $
|
---|
20 | *****************************************************************************/
|
---|
21 | package mindbright.ssh;
|
---|
22 |
|
---|
23 | import java.net.*;
|
---|
24 | import java.io.*;
|
---|
25 | import java.util.Hashtable;
|
---|
26 | import java.util.Vector;
|
---|
27 |
|
---|
28 | public class SSHConnectChannel extends SSHTxChannel {
|
---|
29 | SSHChannelController controller;
|
---|
30 |
|
---|
31 | Hashtable hostMap;
|
---|
32 |
|
---|
33 | public SSHConnectChannel(SSHChannelController controller) {
|
---|
34 | super(null, SSH.CONNECT_CHAN_NUM);
|
---|
35 | this.controller = controller;
|
---|
36 | this.hostMap = new Hashtable();
|
---|
37 | }
|
---|
38 |
|
---|
39 | public synchronized void addHostMapPermanent(String fromHost, String toHost, int toPort) {
|
---|
40 | Vector hostPortPair = new Vector();
|
---|
41 | hostPortPair.addElement(toHost);
|
---|
42 | hostPortPair.addElement(new Integer(toPort));
|
---|
43 | hostPortPair.addElement(new Boolean(true));
|
---|
44 | hostMap.put(fromHost, hostPortPair);
|
---|
45 | }
|
---|
46 | public synchronized void addHostMapTemporary(String fromHost, String toHost, int toPort) {
|
---|
47 | Vector hostPortPair = new Vector();
|
---|
48 | hostPortPair.addElement(toHost);
|
---|
49 | hostPortPair.addElement(new Integer(toPort));
|
---|
50 | hostPortPair.addElement(new Boolean(false));
|
---|
51 | hostMap.put(fromHost, hostPortPair);
|
---|
52 | }
|
---|
53 |
|
---|
54 | public synchronized void delHostMap(String fromHost) {
|
---|
55 | hostMap.remove(fromHost);
|
---|
56 | }
|
---|
57 |
|
---|
58 | public synchronized Vector getHostMap(String fromHost) {
|
---|
59 | Vector hostPortPair = (Vector)hostMap.get(fromHost);
|
---|
60 | if(hostPortPair != null && !(((Boolean)hostPortPair.elementAt(2)).booleanValue())) {
|
---|
61 | delHostMap(fromHost);
|
---|
62 | }
|
---|
63 | return hostPortPair;
|
---|
64 | }
|
---|
65 |
|
---|
66 | int displayNumber(String display) {
|
---|
67 | int hostEnd;
|
---|
68 | int dispEnd;
|
---|
69 | int displayNum;
|
---|
70 | if(display == null || display.equals("") ||
|
---|
71 | (hostEnd = display.indexOf(':')) == -1)
|
---|
72 | return 0;
|
---|
73 |
|
---|
74 | if((dispEnd = display.indexOf('.', hostEnd)) == -1)
|
---|
75 | dispEnd = display.length();
|
---|
76 |
|
---|
77 | try {
|
---|
78 | return Integer.parseInt(display.substring(hostEnd + 1, dispEnd));
|
---|
79 | } catch (Exception e) {
|
---|
80 | // !!!
|
---|
81 | displayNum = 0;
|
---|
82 | }
|
---|
83 | return displayNum;
|
---|
84 | }
|
---|
85 |
|
---|
86 | String displayHost(String display) {
|
---|
87 | int hostEnd;
|
---|
88 | if(display == null || display.equals("") ||
|
---|
89 | display.charAt(0) == ':' || display.indexOf("unix:") == 0 ||
|
---|
90 | (hostEnd = display.indexOf(':')) == -1)
|
---|
91 | return "localhost";
|
---|
92 | return display.substring(0, hostEnd);
|
---|
93 | }
|
---|
94 |
|
---|
95 | public void serviceLoop() throws Exception {
|
---|
96 | SSHPduInputStream inPdu;
|
---|
97 | int remoteChannel;
|
---|
98 | int port;
|
---|
99 | String host;
|
---|
100 | String origin;
|
---|
101 | Socket fwdSocket;
|
---|
102 |
|
---|
103 | for(;;) {
|
---|
104 | inPdu = (SSHPduInputStream) queue.getFirst();
|
---|
105 | remoteChannel = inPdu.readInt();
|
---|
106 |
|
---|
107 | if(inPdu.type == SSH.SMSG_X11_OPEN) {
|
---|
108 | if(!controller.sshAsClient().user.wantX11Forward()) {
|
---|
109 | controller.alert("Something is fishy with the server, unsolicited X11 forward!");
|
---|
110 | throw new Exception("Something is fishy with the server, unsolicited X11 forward!");
|
---|
111 | }
|
---|
112 | String display = controller.sshAsClient().user.getDisplay();
|
---|
113 | host = displayHost(display);
|
---|
114 | port = 6000 + displayNumber(display);
|
---|
115 | } else {
|
---|
116 | host = inPdu.readString();
|
---|
117 | port = inPdu.readInt();
|
---|
118 | }
|
---|
119 |
|
---|
120 | if(controller.haveHostInFwdOpen())
|
---|
121 | origin = inPdu.readString();
|
---|
122 | else
|
---|
123 | origin = "unknown (origin-option not used)";
|
---|
124 |
|
---|
125 | // See if there is a translation entry for this host
|
---|
126 | //
|
---|
127 | Vector hostPortPair = getHostMap(host);
|
---|
128 | if(hostPortPair != null) {
|
---|
129 | host = (String)hostPortPair.elementAt(0);
|
---|
130 | port = ((Integer)hostPortPair.elementAt(1)).intValue();
|
---|
131 | }
|
---|
132 |
|
---|
133 | SSHPduOutputStream respPdu;
|
---|
134 |
|
---|
135 | try {
|
---|
136 | fwdSocket = new Socket(host, port);
|
---|
137 | int newChan = controller.newChannelId();
|
---|
138 | SSHTunnel tunnel = new SSHTunnel(fwdSocket, newChan, remoteChannel, controller);
|
---|
139 | controller.addTunnel(tunnel);
|
---|
140 | tunnel.setRemoteDesc(origin);
|
---|
141 |
|
---|
142 | respPdu = new SSHPduOutputStream(SSH.MSG_CHANNEL_OPEN_CONFIRMATION, controller.sndCipher);
|
---|
143 | respPdu.writeInt(remoteChannel);
|
---|
144 | respPdu.writeInt(newChan);
|
---|
145 |
|
---|
146 | SSH.log("Port open (" + origin + ") : " + host + ": " + port +
|
---|
147 | " (#" + remoteChannel + ")" + " new: " + newChan);
|
---|
148 |
|
---|
149 | controller.transmit(respPdu);
|
---|
150 |
|
---|
151 | // We must wait until after we have put the response in the
|
---|
152 | // controllers tx-queue with starting the tunnel
|
---|
153 | // (to avoid data reaching the server before the response)
|
---|
154 | //
|
---|
155 | tunnel.start();
|
---|
156 |
|
---|
157 | } catch (IOException e) {
|
---|
158 | respPdu = new SSHPduOutputStream(SSH.MSG_CHANNEL_OPEN_FAILURE, controller.sndCipher);
|
---|
159 | respPdu.writeInt(remoteChannel);
|
---|
160 |
|
---|
161 | controller.alert("Failed port open (" + origin + ") : " + host + ": " + port +
|
---|
162 | " (#" + remoteChannel + ")");
|
---|
163 |
|
---|
164 | controller.transmit(respPdu);
|
---|
165 | }
|
---|
166 |
|
---|
167 | }
|
---|
168 | }
|
---|
169 |
|
---|
170 | }
|
---|