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/27 07:53:54 $
|
---|
19 | * $Name: rel1-2-1 $
|
---|
20 | *****************************************************************************/
|
---|
21 | package mindbright.ssh;
|
---|
22 |
|
---|
23 | import java.io.*;
|
---|
24 | import java.net.Socket;
|
---|
25 |
|
---|
26 | import mindbright.terminal.Terminal;
|
---|
27 | import mindbright.security.Cipher;
|
---|
28 |
|
---|
29 | public class SSHExec extends SSHClientUserAdaptor implements SSHConsole {
|
---|
30 |
|
---|
31 | SSHClient client;
|
---|
32 | SSHInteractor interactor = null;
|
---|
33 | SSHClientUser proxyUser = null;
|
---|
34 | SSHExecIndicator indicator = null;
|
---|
35 | SSHAuthenticator authenticator = null;
|
---|
36 |
|
---|
37 | SSHInteractor ourInteractAdapter = null;
|
---|
38 |
|
---|
39 | File cwd;
|
---|
40 | boolean recursive;
|
---|
41 | boolean verbose;
|
---|
42 |
|
---|
43 | String sshHost;
|
---|
44 |
|
---|
45 | PipedInputStream inTop;
|
---|
46 | PipedOutputStream inBottom;
|
---|
47 |
|
---|
48 | public SSHExec(String sshHost, int port, SSHAuthenticator authenticator,
|
---|
49 | boolean verbose) throws IOException {
|
---|
50 | super(sshHost, port);
|
---|
51 |
|
---|
52 | // OUCH: Note must be set before constructing SSHClient since
|
---|
53 | // its constructor calls getInteractor to fetch this
|
---|
54 | //
|
---|
55 | ourInteractAdapter = new SSHInteractorAdapter() {
|
---|
56 | public void open(SSHClient client) {
|
---|
57 | SSHExec.this.open(client);
|
---|
58 | }
|
---|
59 | public void disconnected(SSHClient client, boolean graceful) {
|
---|
60 | SSHExec.this.disconnected(client, graceful);
|
---|
61 | }
|
---|
62 | public void alert(String msg) {
|
---|
63 | SSHExec.this.alert(msg);
|
---|
64 | }
|
---|
65 | };
|
---|
66 |
|
---|
67 | this.authenticator = authenticator;
|
---|
68 | client = new SSHClient(authenticator, this);
|
---|
69 | inTop = new PipedInputStream();
|
---|
70 | inBottom = new PipedOutputStream(inTop);
|
---|
71 | this.cwd = cwd;
|
---|
72 | this.verbose = verbose;
|
---|
73 | this.sshHost = sshHost;
|
---|
74 |
|
---|
75 | client.setConsole(this);
|
---|
76 | client.activateTunnels = false;
|
---|
77 | }
|
---|
78 |
|
---|
79 | public void setInteractor(SSHInteractor interactor) {
|
---|
80 | this.interactor = interactor;
|
---|
81 | }
|
---|
82 |
|
---|
83 | public void setClientUser(SSHClientUser proxyUser) {
|
---|
84 | this.proxyUser = proxyUser;
|
---|
85 | }
|
---|
86 |
|
---|
87 | public void setIndicator(SSHExecIndicator indicator) {
|
---|
88 | this.indicator = indicator;
|
---|
89 | }
|
---|
90 |
|
---|
91 | public void abort() {
|
---|
92 | interactor = null;
|
---|
93 | indicator = null;
|
---|
94 | client.forcedDisconnect();
|
---|
95 | }
|
---|
96 |
|
---|
97 | public void exec(String command) throws IOException {
|
---|
98 | //String cmd = "scp -t " + (verbose ? "-v " : "") + authenticator.getUsername(this) + "@" + sshHost + " \"" + command + "\"";
|
---|
99 | String cmd = command;
|
---|
100 | // System.out.println(cmd);
|
---|
101 | client.doSingleCommand(cmd, true, 0);
|
---|
102 | // readResponse("After starting remote ssh exec");
|
---|
103 | client.forcedDisconnect();
|
---|
104 |
|
---|
105 | }
|
---|
106 |
|
---|
107 | int readByte() throws IOException {
|
---|
108 | return inTop.read();
|
---|
109 | }
|
---|
110 |
|
---|
111 | String readString() throws IOException {
|
---|
112 | byte[] buf = new byte[2048];
|
---|
113 | int ch, i = 0;
|
---|
114 | while(((ch = readByte()) != ((int)'\n')) && ch >= 0) {
|
---|
115 | buf[i++] = (byte)ch;
|
---|
116 | }
|
---|
117 | if(ch == -1) {
|
---|
118 | throw new EOFException();
|
---|
119 | }
|
---|
120 | if(buf[0] == (byte)'\n')
|
---|
121 | throw new IOException("Unexpected <NL>");
|
---|
122 | if(buf[0] == (byte)'\02' || buf[0] == (byte)'\01') {
|
---|
123 | // !!!
|
---|
124 | String errMsg = new String(buf, 1, i - 1);
|
---|
125 | if(buf[0] == (byte)'\02')
|
---|
126 | throw new IOException(errMsg);
|
---|
127 | alert(errMsg);
|
---|
128 | return null;
|
---|
129 | }
|
---|
130 | return new String(buf, 0, i);
|
---|
131 | }
|
---|
132 |
|
---|
133 | void readResponse(String where) throws IOException {
|
---|
134 | int r = readByte();
|
---|
135 | if(r == 0) {
|
---|
136 | // All is well, no error
|
---|
137 | return;
|
---|
138 | }
|
---|
139 | String errMsg = readString();
|
---|
140 | if(r == (byte)'\02')
|
---|
141 | throw new IOException(errMsg);
|
---|
142 | alert(errMsg);
|
---|
143 | }
|
---|
144 |
|
---|
145 | public void stderrWriteString(byte[] str) {
|
---|
146 | if(verbose) alert("Remote warning/error: " + new String(str));
|
---|
147 | }
|
---|
148 |
|
---|
149 | public void stdoutWriteString(byte[] str) {
|
---|
150 | try {
|
---|
151 | inBottom.write(str);
|
---|
152 | } catch(IOException e) {
|
---|
153 | try {
|
---|
154 | inBottom.close();
|
---|
155 | } catch (IOException ee) {
|
---|
156 | // !!!
|
---|
157 | }
|
---|
158 | alert("Error writing data to stdout-pipe");
|
---|
159 | }
|
---|
160 | }
|
---|
161 |
|
---|
162 | public Terminal getTerminal() {
|
---|
163 | return null;
|
---|
164 | }
|
---|
165 | public void print(String str) {
|
---|
166 | }
|
---|
167 | public void println(String str) {
|
---|
168 | }
|
---|
169 | public void serverConnect(SSHChannelController controller, Cipher sndCipher) {
|
---|
170 | }
|
---|
171 | public void serverDisconnect(String reason) {
|
---|
172 | }
|
---|
173 | public boolean wantPTY() {
|
---|
174 | return false;
|
---|
175 | }
|
---|
176 | public void open(SSHClient client) {
|
---|
177 | if(indicator != null)
|
---|
178 | indicator.connected(sshHost);
|
---|
179 | }
|
---|
180 | public void disconnected(SSHClient client, boolean graceful) {
|
---|
181 | try {
|
---|
182 | inBottom.close();
|
---|
183 | } catch (IOException e) {
|
---|
184 | // !!!
|
---|
185 | }
|
---|
186 | }
|
---|
187 | public void alert(String msg) {
|
---|
188 | if(interactor != null) {
|
---|
189 | interactor.alert(msg);
|
---|
190 | }
|
---|
191 | }
|
---|
192 |
|
---|
193 | public Socket getProxyConnection() throws IOException {
|
---|
194 | if(proxyUser != null) {
|
---|
195 | return proxyUser.getProxyConnection();
|
---|
196 | }
|
---|
197 | return null;
|
---|
198 | }
|
---|
199 |
|
---|
200 | public SSHInteractor getInteractor() {
|
---|
201 | return ourInteractAdapter;
|
---|
202 | }
|
---|
203 |
|
---|
204 | }
|
---|