aboutsummaryrefslogtreecommitdiff
path: root/hardinfo2/remote.c
blob: f0922d3ff5b08fe7538af524e7250f83466ea68a (plain)
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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
/*
 *    Remote Client
 *    HardInfo - Displays System Information
 *    Copyright (C) 2003-2009 Leandro A. F. Pereira <leandro@hardinfo.org>
 *
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, version 2.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program; if not, write to the Free Software
 *    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 */

#include <stdio.h>
#include <string.h>
#include <shell.h>
#include <iconcache.h>
#include <hardinfo.h>
#include <config.h>

#include "xmlrpc-client.h"

/*
 * TODO
 *
 * - Completely disable this feature if libsoup support isn't compiled in
 * - Add "Local Computer"
 * - Detect machines on local network that runs SSH
 *   - IP range scan
 *   - mDNS
 * - Allow the user to add/remove/edit a machine
 *   - Use ~/.ssh/known_hosts as a starting point?
 *   - Different icons for different machines?
 *   - Make sure SSH can do port forwarding
 *   - Make sure the remote host has HardInfo installed, with the correct
 *     version.
 * - Allow the user to choose/guess the SSH authentication method
 *   - Plain old username / password
 *   - Passwordless RSA keys
 *   - RSA Keys with passwords
 * - Make sure SSH can do port forwarding
 * - Make sure the remote host has HardInfo installed
 * - Implement XML-RPC server (use libsoup; there's a example with libsoup's sources)
 *   - Create abstractions for common module operations
 *   - Use function pointers to choose between local and remote modes
 *   - Generate a random username/password to be passed to the XML-RPC server; use
 *     that username/password on the client; this is just to make sure nobody on the
 *     machine will be allowed to obtain information that might be sensitive. This
 *     will be passed with base64, so it can be sniffed; this needs root access anyway,
 *     so not really a problem.
 * - Determine if we're gonna use GIOChannels or create a communication thread
 * - Use libsoup XML-RPC support to implement the remote mode
 * - Introduce a flag on the modules, stating their ability to be used locally/remotely
 *   (Benchmarks can't be used remotely; Displays won't work remotely [unless we use
 *    X forwarding, but that'll be local X11 info anyway]).
 */

typedef struct _RemoteDialog RemoteDialog;
struct _RemoteDialog {
  GtkWidget *dialog;
  GtkWidget *btn_connect, *btn_cancel;
};

static RemoteDialog *remote_dialog_new(GtkWidget *parent);

static gboolean remote_version_is_supported(void)
{
    shell_status_update("Obtaining remote server API version...");

    switch (xmlrpc_get_integer("http://localhost:4242/xmlrpc", "server.getAPIVersion", NULL)) {
    case -1:
        g_warning("Remote Host didn't respond.");
        break;
    case 1:
        return TRUE;
    default:
        g_warning("Remote Host has an unsupported API version.");
    }

    return FALSE;
}

static gchar *remote_module_entry_func()
{
    Shell *shell = shell_get_main_shell();
    
    return xmlrpc_get_string("http://localhost:4242/xmlrpc", "module.entryFunction",
                             "%s%i", shell->selected_module_name, shell->selected->number);
}

static gchar *remote_module_entry_scan_func()
{
    Shell *shell = shell_get_main_shell();
    
    return xmlrpc_get_string("http://localhost:4242/xmlrpc", "module.entryScan",
                             "%s%i", shell->selected_module_name, shell->selected->number);
}

static gchar *remote_module_entry_field_func(gchar *entry)
{
    Shell *shell = shell_get_main_shell();
    
    return xmlrpc_get_string("http://localhost:4242/xmlrpc", "module.entryGetField",
                             "%s%i%s", shell->selected_module_name, shell->selected->number, entry);
}

static gchar *remote_module_entry_more_func(gchar *entry)
{
    Shell *shell = shell_get_main_shell();
    
    return xmlrpc_get_string("http://localhost:4242/xmlrpc", "module.entryGetMoreInfo",
                             "%s%i%s", shell->selected_module_name, shell->selected->number, entry);
}

static gchar *remote_module_entry_note_func(gint entry)
{
    Shell *shell = shell_get_main_shell();
    
    return xmlrpc_get_string("http://localhost:4242/xmlrpc", "module.entryGetNote",
                             "%s%i", shell->selected_module_name, shell->selected->number);
}

static gboolean load_module_list()
{
    GValueArray *modules;
    int i = 0;
    
    shell_status_update("Obtaining remote server module list...");
    
    modules = xmlrpc_get_array("http://localhost:4242/xmlrpc", "module.getModuleList", NULL);
    if (!modules) {
        return FALSE;
    }
    
    shell_status_update("Unloading local modules...");
    module_unload_all();
    
    for (; i < modules->n_values; i++) {
        GValueArray *entries;
        const gchar *module = g_value_get_string(&modules->values[i]);
        int j = 0;
        
        DEBUG("%s", module);
        
        shell_status_pulse();
        entries = xmlrpc_get_array("http://localhost:4242/xmlrpc", "module.getEntryList", "%s", module);
        if (entries) {
            for (; j < entries->n_values; j++) {
                GValueArray *tuple = g_value_get_boxed(&entries->values[j]);
                
                DEBUG("--- %s [%s](%d)",
                      g_value_get_string(&tuple->values[0]),
                      g_value_get_string(&tuple->values[1]),
                      j);
            }
            
            g_value_array_free(entries);
        }
    }
    
    g_value_array_free(modules);
    
    return TRUE;
}

static void remote_connect(RemoteDialog *rd)
{
    xmlrpc_init();
    
    /* check API version */
    if (remote_version_is_supported()) {
        if (!load_module_list()) {
            g_warning("Remote module list couldn't be loaded.");
        }
    }
    
    shell_status_update("Done.");
}

void remote_dialog_show(GtkWidget *parent)
{
    gboolean success;
    RemoteDialog *rd = remote_dialog_new(parent);

    if (gtk_dialog_run(GTK_DIALOG(rd->dialog)) == GTK_RESPONSE_ACCEPT) {
	gtk_widget_hide(rd->dialog);
	shell_view_set_enabled(FALSE);
	shell_status_set_enabled(TRUE);
	
	remote_connect(rd);
	
	shell_status_set_enabled(FALSE);
    }

    gtk_widget_destroy(rd->dialog);
    g_free(rd);
}

static void populate_store(GtkListStore * store)
{
    GKeyFile *remote;
    GtkTreeIter iter;
    gchar *path;
    
    gtk_list_store_clear(store);

    gtk_list_store_append(store, &iter);
    gtk_list_store_set(store, &iter,
                       0, icon_cache_get_pixbuf("home.png"),
                       1, g_strdup("Local Computer"),
                       2, GINT_TO_POINTER(-1),
                       -1);

    remote = g_key_file_new();
    path = g_build_filename(g_get_home_dir(), ".hardinfo", "remote.conf", NULL);
    if (g_key_file_load_from_file(remote, path, 0, NULL)) {
        gint no_hosts, i;
        
        no_hosts = g_key_file_get_integer(remote, "$Global$", "no_hosts", NULL);
        for (i = 0; i < no_hosts; i++) {
            gchar *hostname;
            gchar *hostgroup;
            gchar *icon;
            
            hostgroup = g_strdup_printf("Host%d", i);
            
            hostname = g_key_file_get_string(remote, hostgroup, "name", NULL);
            icon = g_key_file_get_string(remote, hostgroup, "icon", NULL);
        
            gtk_list_store_append(store, &iter);
            gtk_list_store_set(store, &iter,
                               0, icon_cache_get_pixbuf(icon ? icon : "server.png"),
                               1, hostname,
                               2, GINT_TO_POINTER(i),
                               -1);
            
            g_free(hostgroup);
            g_free(icon);
        }
    }
    
    g_free(path);
    g_key_file_free(remote);
}

static GtkWidget *host_editor_dialog_new(GtkWidget *parent, gchar *title)
{
    GtkWidget *dialog, *dialog1_action_area, *button7, *button8;
    
    dialog = gtk_dialog_new();
    gtk_window_set_title(GTK_WINDOW(dialog), title);
    gtk_container_set_border_width(GTK_CONTAINER(dialog), 5);
    gtk_window_set_default_size(GTK_WINDOW(dialog), 420, 260);
    gtk_window_set_transient_for(GTK_WINDOW(dialog), GTK_WINDOW(parent));
    gtk_window_set_position(GTK_WINDOW(dialog),
			    GTK_WIN_POS_CENTER_ON_PARENT);
    gtk_window_set_type_hint(GTK_WINDOW(dialog),
			     GDK_WINDOW_TYPE_HINT_DIALOG);

    dialog1_action_area = GTK_DIALOG(dialog)->action_area;
    gtk_widget_show(dialog1_action_area);
    gtk_button_box_set_layout(GTK_BUTTON_BOX(dialog1_action_area),
			      GTK_BUTTONBOX_END);

    button8 = gtk_button_new_from_stock(GTK_STOCK_CANCEL);
    gtk_widget_show(button8);
    gtk_dialog_add_action_widget(GTK_DIALOG(dialog), button8,
				 GTK_RESPONSE_CANCEL);
    GTK_WIDGET_SET_FLAGS(button8, GTK_CAN_DEFAULT);

    button7 = gtk_button_new_from_stock(GTK_STOCK_SAVE);
    gtk_widget_show(button7);
    gtk_dialog_add_action_widget(GTK_DIALOG(dialog), button7,
				 GTK_RESPONSE_ACCEPT);
    GTK_WIDGET_SET_FLAGS(button7, GTK_CAN_DEFAULT);

    return dialog;
}

static void remote_dialog_add(GtkWidget *button, gpointer data)
{
    RemoteDialog *rd = (RemoteDialog *)data;
    GtkWidget *host_editor = host_editor_dialog_new(rd->dialog, "Add a host");

    if (gtk_dialog_run(GTK_DIALOG(host_editor)) == GTK_RESPONSE_ACCEPT) {
        DEBUG("saving");
    }

    gtk_widget_destroy(host_editor);
}

static void remote_dialog_edit(GtkWidget *button, gpointer data)
{
    RemoteDialog *rd = (RemoteDialog *)data;
    GtkWidget *host_editor = host_editor_dialog_new(rd->dialog, "Edit a host");

    if (gtk_dialog_run(GTK_DIALOG(host_editor)) == GTK_RESPONSE_ACCEPT) {
        DEBUG("saving");
    }

    gtk_widget_destroy(host_editor);
}

static void remote_dialog_remove(GtkWidget *button, gpointer data)
{
    RemoteDialog *rd = (RemoteDialog *)data;
    GtkWidget *dialog;

    dialog = gtk_message_dialog_new(GTK_WINDOW(rd->dialog),
                                    GTK_DIALOG_DESTROY_WITH_PARENT,
                                    GTK_MESSAGE_QUESTION,
                                    GTK_BUTTONS_NONE,
                                    "Remove the host <b>%s</b>?", "selected.host.name");

    gtk_dialog_add_buttons(GTK_DIALOG(dialog),
                           GTK_STOCK_NO, GTK_RESPONSE_REJECT,
                           GTK_STOCK_DELETE, GTK_RESPONSE_ACCEPT, NULL);

    if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
        DEBUG("removing");
    }

    gtk_widget_destroy(dialog);
}

static RemoteDialog *remote_dialog_new(GtkWidget *parent)
{
    RemoteDialog *rd;
    GtkWidget *dialog;
    GtkWidget *dialog1_vbox;
    GtkWidget *scrolledwindow2;
    GtkWidget *treeview2;
    GtkWidget *vbuttonbox3;
    GtkWidget *button3;
    GtkWidget *button6;
    GtkWidget *dialog1_action_area;
    GtkWidget *button8;
    GtkWidget *button7;
    GtkWidget *label;
    GtkWidget *hbox;

    GtkTreeViewColumn *column;
    GtkCellRenderer *cr_text, *cr_pbuf, *cr_toggle;
    GtkListStore *store;
    GtkTreeModel *model;

    rd = g_new0(RemoteDialog, 1);

    dialog = gtk_dialog_new();
    gtk_window_set_title(GTK_WINDOW(dialog), "Connect to Remote Computer");
    gtk_container_set_border_width(GTK_CONTAINER(dialog), 5);
    gtk_window_set_default_size(GTK_WINDOW(dialog), 420, 260);
    gtk_window_set_transient_for(GTK_WINDOW(dialog), GTK_WINDOW(parent));
    gtk_window_set_position(GTK_WINDOW(dialog),
			    GTK_WIN_POS_CENTER_ON_PARENT);
    gtk_window_set_type_hint(GTK_WINDOW(dialog),
			     GDK_WINDOW_TYPE_HINT_DIALOG);

    dialog1_vbox = GTK_DIALOG(dialog)->vbox;
    gtk_box_set_spacing(GTK_BOX(dialog1_vbox), 5);
    gtk_container_set_border_width(GTK_CONTAINER(dialog1_vbox), 4);
    gtk_widget_show(dialog1_vbox);

    hbox = gtk_hbox_new(FALSE, 5);
    gtk_box_pack_start(GTK_BOX(dialog1_vbox), hbox, FALSE, FALSE, 0);

    label = gtk_label_new("<big><b>Connect To</b></big>\n"
			  "Please choose the remote computer to connect to:");
    gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
    gtk_label_set_use_markup(GTK_LABEL(label), TRUE);
    gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);

    gtk_box_pack_start(GTK_BOX(hbox),
		       icon_cache_get_image("server-large.png"),
		       FALSE, FALSE, 0);
    gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, TRUE, 0);
    gtk_widget_show_all(hbox);
    
    hbox = gtk_hbox_new(FALSE, 5);
    gtk_box_pack_start(GTK_BOX(dialog1_vbox), hbox, TRUE, TRUE, 0);
    gtk_widget_show(hbox);

    scrolledwindow2 = gtk_scrolled_window_new(NULL, NULL);
    gtk_widget_show(scrolledwindow2);
    gtk_box_pack_start(GTK_BOX(hbox), scrolledwindow2, TRUE, TRUE,
		       0);
    gtk_widget_set_size_request(scrolledwindow2, -1, 200);
    gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwindow2),
				   GTK_POLICY_AUTOMATIC,
				   GTK_POLICY_AUTOMATIC);
    gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW
					(scrolledwindow2), GTK_SHADOW_IN);

    store = gtk_list_store_new(3, GDK_TYPE_PIXBUF, G_TYPE_STRING, G_TYPE_INT);
    model = GTK_TREE_MODEL(store);

    treeview2 = gtk_tree_view_new_with_model(model);
    gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(treeview2), FALSE);
    gtk_widget_show(treeview2);
    gtk_container_add(GTK_CONTAINER(scrolledwindow2), treeview2);

    column = gtk_tree_view_column_new();
    gtk_tree_view_append_column(GTK_TREE_VIEW(treeview2), column);

    cr_pbuf = gtk_cell_renderer_pixbuf_new();
    gtk_tree_view_column_pack_start(column, cr_pbuf, FALSE);
    gtk_tree_view_column_add_attribute(column, cr_pbuf, "pixbuf",
				       TREE_COL_PBUF);

    cr_text = gtk_cell_renderer_text_new();
    gtk_tree_view_column_pack_start(column, cr_text, TRUE);
    gtk_tree_view_column_add_attribute(column, cr_text, "markup",
				       TREE_COL_NAME);

    populate_store(store);
    
    vbuttonbox3 = gtk_vbutton_box_new();
    gtk_widget_show(vbuttonbox3);
    gtk_box_pack_start(GTK_BOX(hbox), vbuttonbox3, FALSE, TRUE, 0);
    gtk_box_set_spacing(GTK_BOX(vbuttonbox3), 5);
    gtk_button_box_set_layout(GTK_BUTTON_BOX(vbuttonbox3),
			      GTK_BUTTONBOX_START);

    button3 = gtk_button_new_with_mnemonic("_Add");
    gtk_widget_show(button3);
    gtk_container_add(GTK_CONTAINER(vbuttonbox3), button3);
    GTK_WIDGET_SET_FLAGS(button3, GTK_CAN_DEFAULT);
    g_signal_connect(button3, "clicked",
		     G_CALLBACK(remote_dialog_add), rd);

    button6 = gtk_button_new_with_mnemonic("_Edit");
    gtk_widget_show(button6);
    gtk_container_add(GTK_CONTAINER(vbuttonbox3), button6);
    GTK_WIDGET_SET_FLAGS(button6, GTK_CAN_DEFAULT);
    g_signal_connect(button6, "clicked", G_CALLBACK(remote_dialog_edit),
		     rd);

    button6 = gtk_button_new_with_mnemonic("_Remove");
    gtk_widget_show(button6);
    gtk_container_add(GTK_CONTAINER(vbuttonbox3), button6);
    GTK_WIDGET_SET_FLAGS(button6, GTK_CAN_DEFAULT);
    g_signal_connect(button6, "clicked", G_CALLBACK(remote_dialog_remove),
		     rd);

    dialog1_action_area = GTK_DIALOG(dialog)->action_area;
    gtk_widget_show(dialog1_action_area);
    gtk_button_box_set_layout(GTK_BUTTON_BOX(dialog1_action_area),
			      GTK_BUTTONBOX_END);

    button8 = gtk_button_new_from_stock(GTK_STOCK_CANCEL);
    gtk_widget_show(button8);
    gtk_dialog_add_action_widget(GTK_DIALOG(dialog), button8,
				 GTK_RESPONSE_CANCEL);
    GTK_WIDGET_SET_FLAGS(button8, GTK_CAN_DEFAULT);

    button7 = gtk_button_new_from_stock(GTK_STOCK_CONNECT);
    gtk_widget_show(button7);
    gtk_dialog_add_action_widget(GTK_DIALOG(dialog), button7,
				 GTK_RESPONSE_ACCEPT);
    GTK_WIDGET_SET_FLAGS(button7, GTK_CAN_DEFAULT);

    rd->dialog = dialog;
    rd->btn_cancel = button8;
    rd->btn_connect = button7;

    gtk_tree_view_collapse_all(GTK_TREE_VIEW(treeview2));

    return rd;
}