Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 4317afc58248cc01db1ada37969e5d53bf1dec79 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
/*******************************************************************************
 * Copyright (c) 2009, 2010 Wind River Systems, Inc. and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Wind River Systems - initial API and implementation
 *******************************************************************************/
package org.eclipse.tcf.internal.debug.ui.launch.setup;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.InetAddress;
import java.net.URL;

import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.dialogs.IPageChangingListener;
import org.eclipse.jface.dialogs.PageChangingEvent;
import org.eclipse.jface.wizard.IWizardPage;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.tcf.internal.debug.ui.Activator;
import org.eclipse.tcf.core.Base64;
import org.eclipse.tcf.ssl.TCFSecurityManager;
import org.osgi.framework.Bundle;

class WizardLogPage extends WizardPage implements Runnable {

    private final SetupWizardDialog wizard;

    private Thread thread;
    private String protocol;
    private String host;
    private String user;
    private String user_password;
    private String root_password;

    private Text log_text;

    private Display display;
    private Shell parent;
    private IRemoteShell shell;

    WizardLogPage(SetupWizardDialog wizard) {
        super("LogPage");
        this.wizard = wizard;
        setTitle("Remote host login");
        setDescription("");
    }

    public void createControl(Composite parent) {
        display = parent.getDisplay();
        this.parent = parent.getShell();
        ((WizardDialog)getContainer()).addPageChangingListener(new IPageChangingListener() {
            public void handlePageChanging(PageChangingEvent event) {
                if (event.getCurrentPage() instanceof WizardLoginPage && event.getTargetPage() == WizardLogPage.this) {
                    runSetupJob((WizardLoginPage)event.getCurrentPage());
                }
            }
        });

        Composite composite =  new Composite(parent, SWT.NULL);
        GridLayout gl = new GridLayout();
        gl.numColumns = 1;
        composite.setLayout(gl);

        new Label(composite, SWT.WRAP).setText("Agent installation log:");

        log_text = new Text(composite, SWT.BORDER | SWT.MULTI | SWT.READ_ONLY | SWT.H_SCROLL | SWT.V_SCROLL);
        GridData gd = new GridData(GridData.FILL_BOTH);
        log_text.setLayoutData(gd);

        setControl(composite);
    }

    private void runSetupJob(WizardLoginPage login) {
        assert display != null;
        assert display.getThread() == Thread.currentThread();
        if (thread != null) return;
        setErrorMessage(null);
        protocol = login.protocol.getText();
        host = login.host.getText();
        user = login.user.getText();
        user_password = login.user_password.getText();
        root_password = login.root_password.getText();
        log_text.setText("Connect to " + host + "\n");
        thread = new Thread(this);
        thread.start();
    }

    public void run() {
        Throwable error = null;
        try {
            if ("Telnet".equals(protocol)) {
                shell = new TelnetClient(InetAddress.getByName(host), 23, user, user_password);
            }
            else if ("SSH".equals(protocol)) {
                shell = new SSHClient(parent, host, user, user_password);
            }
            else {
                throw new Exception("Invalid protocol name: " + protocol);
            }

            String s;

            if (!user.equals("root")) {
                send("su", true);
                expect("Password: ", true);
                send(root_password, false);
                s = waitPrompt();
                if (s.length() > 0) throw new Exception(s);
            }
            exec("cd /tmp");
            send("uname -o", true);
            String os = waitPrompt().replace('\n', ' ').trim();
            send("uname -m", true);
            String machine = waitPrompt().replace('\n', ' ').trim();
            String version = "1.0.0";
            Bundle bundle = Platform.getBundle(Activator.PLUGIN_ID);

            URL url = FileLocator.find(bundle, new Path("agent/get-os-tag"), null);
            if (url == null) throw new Exception("Cannot find get-os-tag script");
            send("cat >get-os-tag", true);
            InputStream inp = url.openStream();
            byte[] buf = new byte[0x100];
            for (;;) {
                int len = inp.read(buf);
                if (len < 0) break;
                shell.write(new String(buf, 0, len, "ASCII"));
                for (int i = 0; i < len; i++) {
                    if (buf[i] == '\n') shell.expect("\n");
                }
            }
            inp.close();
            shell.write("\004");
            s = waitPrompt();
            if (s.length() > 0) throw new Exception(s);

            exec("chmod u+x get-os-tag");

            send("./get-os-tag", true);
            String os_tag = waitPrompt().replace('\n', ' ').trim();

            exec("rm -f get-os-tag");

            url = null;
            String fnm = null;
            String machine0 = machine;
            for (;;) {
                for (int release = 16; url == null && release > 0; release--) {
                    fnm = "tcf-agent-" + version + "-" + release + "." + os_tag + "." + machine + ".rpm";
                    url = FileLocator.find(bundle, new Path("agent/" + os + "/" + machine + "/" + fnm), null);
                }
                if (url != null) break;
                if (machine.equals("i686")) machine = "i586";
                else if (machine.equals("i586")) machine = "i486";
                else if (machine.equals("i486")) machine = "i386";
                else {
                    machine = machine0;
                    if (os_tag.startsWith("fc")) {
                        int n = Integer.parseInt(os_tag.substring(2)) - 1;
                        if (n <= 0) break;
                        os_tag = "fc" + n;
                    }
                    else if (os_tag.startsWith("rh")) {
                        int n = Integer.parseInt(os_tag.substring(2)) - 1;
                        if (n <= 0) break;
                        os_tag = "rh" + n;
                    }
                    else break;
                }
            }
            if (url == null) throw new Exception("Unsupported target OS or CPU");

            inp = url.openStream();
            send("which base64", true);
            s = waitPrompt();
            if (s.indexOf(':') < 0) {
                send("base64 -di >" + fnm, true);
                sendBase64(inp);
                shell.write("\004");
                s = waitPrompt();
                if (s.length() > 0) throw new Exception(s);
            }
            else {
                send("which uudecode", true);
                s = waitPrompt();
                if (s.indexOf(':') < 0) {
                    send("uudecode", true);
                    send("begin-base64 644 " + fnm, true);
                    sendBase64(inp);
                    send("====", true);
                    s = waitPrompt();
                    if (s.length() > 0) throw new Exception(s);
                }
                else {
                    throw new Exception("No base64 or uudecode commands available");
                }
            }
            inp.close();

            send("rpm -e --quiet tcf-agent", true);
            waitPrompt();
            exec("rm -f /etc/init.d/tcf-agent*");
            exec("rpm -i --quiet " + fnm);
            exec("rm -f " + fnm);

            File certs = TCFSecurityManager.getCertificatesDirectory();

            File local_cert = new File(certs, "local.cert");
            File local_priv = new File(certs, "local.priv");

            if (!local_cert.exists() || !local_priv.exists()) {
                copyRemoteSecret("local.cert", local_cert);
                copyRemoteSecret("local.priv", local_priv);
                exec("/usr/sbin/tcf-agent -c");
            }

            copyRemoteSecret("local.cert", new File(certs, host + ".cert"));
            copyLocalSecret(local_cert, InetAddress.getLocalHost().getHostName() + ".cert");

            if (!user.equals("root")) {
                send("exit", true);
                waitPrompt();
            }
        }
        catch (Throwable x) {
            error = x;
        }
        if (shell != null) {
            try {
                shell.close();
                shell = null;
            }
            catch (Throwable x) {
                if (error != null) error = x;
            }
        }
        done(error);
    }

    private void sendBase64(InputStream inp) throws Exception {
        byte[] buf = new byte[0x100 * 3];
        for (;;) {
            int len = 0;
            while (len < buf.length) {
                int rd = inp.read(buf, len, buf.length - len);
                if (rd < 0) break;
                len += rd;
            }
            if (len == 0) break;
            send(new String(Base64.toBase64(buf, 0, len)), false);
        }
    }

    private void copyRemoteSecret(String from, File to) throws Exception {
        send("cat /etc/tcf/ssl/" + from, true);
        String s = waitPrompt();
        if (s.indexOf("-----BEGIN ") != 0) throw new Exception(s);
        BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(
                new FileOutputStream(to), "ASCII"));
        wr.write(s);
        wr.close();
    }

    private void copyLocalSecret(File from, String to) throws Exception {
        send("cat >/etc/tcf/ssl/" + to, true);
        BufferedReader rd = new BufferedReader(new InputStreamReader(
                new FileInputStream(from), "ASCII"));
        for (;;) {
            String s = rd.readLine();
            if (s == null) break;
            send(s, true);
        }
        shell.write("\004");
        String s = waitPrompt();
        if (s.length() > 0) throw new Exception(s);
    }

    private void send(final String s, boolean log) throws IOException {
        if (log) {
            display.asyncExec(new Runnable() {
                public void run() {
                    if (log_text.isDisposed()) return;
                    log_text.append("Send: ");
                    log_text.append(s);
                    if (!s.endsWith("\n")) log_text.append("\n");
                }
            });
        }
        shell.write(s);
        shell.write("\n");
        if (log) shell.expect(s);
        shell.expect("\n");
    }

    private void expect(final String s, boolean log) throws IOException {
        if (log) {
            display.asyncExec(new Runnable() {
                public void run() {
                    if (log_text.isDisposed()) return;
                    log_text.append("Expect: ");
                    log_text.append(s);
                    if (!s.endsWith("\n")) log_text.append("\n");
                }
            });
        }
        shell.expect(s);
    }

    private String waitPrompt() throws IOException {
        display.asyncExec(new Runnable() {
            public void run() {
                if (log_text.isDisposed()) return;
                log_text.append("Wait for shell prompt\n");
            }
        });
        final String s = shell.waitPrompt();
        if (s.length() > 0) {
            display.asyncExec(new Runnable() {
                public void run() {
                    if (log_text.isDisposed()) return;
                    log_text.append("Got: ");
                    log_text.append(s);
                    if (!s.endsWith("\n")) log_text.append("\n");
                }
            });
        }
        return s;
    }

    private void exec(String cmd) throws Exception {
        send(cmd, true);
        String s = waitPrompt();
        if (s.length() > 0) throw new Exception(s);
    }

    private void done(final Throwable error) {
        display.asyncExec(new Runnable() {
            public void run() {
                thread = null;
                protocol = null;
                host = null;
                user = null;
                user_password = null;
                root_password = null;
                if (log_text.isDisposed()) return;
                if (error != null) {
                    StringWriter buf = new StringWriter();
                    PrintWriter pwr = new PrintWriter(buf);
                    error.printStackTrace(pwr);
                    pwr.flush();
                    log_text.append(buf.toString());
                    setErrorMessage(error.getClass().getName() + ": " + error.getLocalizedMessage());
                }
                else {
                    log_text.append("Done\n");
                    setErrorMessage(null);
                }
                getContainer().updateButtons();
            }
        });
    }

    @Override
    public IWizardPage getPreviousPage() {
        if (thread != null) return null;
        return wizard.getPage("LoginPage");
    }

    @Override
    public IWizardPage getNextPage() {
        return null;
    }

    public boolean canFinish() {
        return thread == null && getErrorMessage() == null;
    }
}

Back to the top