scan.c 6.24 KB
Newer Older
Rodrigo Moya's avatar
Rodrigo Moya committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/* -*- Mode: C; indent-tabs-mode: t; tab-width: 8; c-basic-offset: 8 -*- */

/* gnome-netinfo - A GUI Interface for network utilities
 * Copyright (C) 2003 by German Poo-Caaman~o
 * Copyright (C) 2003 by William Jon McCann
 *
 * 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; either version 2 of the License, or
 * (at your option) any later version.
 *
 * 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., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

22 23 24 25
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

26 27
#include <gtk/gtk.h>
#include <glib/gi18n.h>
28
#include <glib/gprintf.h>
Rodrigo Moya's avatar
Rodrigo Moya committed
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48

#include "scan.h"
#include "utils.h"

static GtkTreeModel *scan_create_model (GtkTreeView *widget);
static gint strip_line (gchar * line, scan_data * data);

void
scan_stop (Netinfo * netinfo)
{
	g_return_if_fail (netinfo != NULL);

	netinfo_stop_process_command (netinfo);
}

void
scan_do (Netinfo * netinfo)
{
	const gchar *host = NULL;
	GtkTreeModel *model;
49 50 51 52
	gint ip_version;
	gchar *program = NULL;
	gchar *command = NULL;
	GtkWidget *parent;
Rodrigo Moya's avatar
Rodrigo Moya committed
53 54 55

	g_return_if_fail (netinfo != NULL);

German Poo-Caaman~o's avatar
German Poo-Caaman~o committed
56 57 58 59
	/* Because of the delay, we can't check twice for a hostname/IP.
	 * It was made before this function was called.  Anyway, we
	 * check at least if we have a text as hostname */
	if (netinfo_validate_domain (netinfo) == FALSE) {
Rodrigo Moya's avatar
Rodrigo Moya committed
60 61 62 63 64 65
		netinfo_stop_process_command (netinfo);
		return;
	}

	host = netinfo_get_host (netinfo);

66 67 68 69
	if (netinfo->stbar_text)
		g_free (netinfo->stbar_text);
	netinfo->stbar_text = g_strdup_printf (_("Scanning %s for open ports"), host);

Rodrigo Moya's avatar
Rodrigo Moya committed
70 71 72 73 74 75
	/* Clear the current output */
	model = gtk_tree_view_get_model (GTK_TREE_VIEW (netinfo->output));
	if (GTK_IS_LIST_STORE (model)) {
		gtk_list_store_clear (GTK_LIST_STORE (model));
	}

76 77
	parent = gtk_widget_get_toplevel (netinfo->output);
	program = util_find_program_dialog ("nmap", parent);
Rodrigo Moya's avatar
Rodrigo Moya committed
78

79 80
	if (program != NULL) {
		ip_version = netinfo_get_ip_version (netinfo);
Rodrigo Moya's avatar
Rodrigo Moya committed
81

82 83 84 85 86 87
		if (ip_version == IPV6) {
			command = g_strdup_printf ("%s %s %s %s", program,
						   "nmap", "-6", host);
		} else {
			command = g_strdup_printf ("%s %s %s", program,
						   "nmap", host);
Rodrigo Moya's avatar
Rodrigo Moya committed
88
		}
89 90
	
		g_strfreev (netinfo->command_line);
91
		netinfo->command_line = g_strsplit (command, " ", -1);
Rodrigo Moya's avatar
Rodrigo Moya committed
92
	
93 94
		netinfo_process_command (netinfo);
	}
Rodrigo Moya's avatar
Rodrigo Moya committed
95

96 97
	g_free (command);
	g_free (program);
98 99
}

Rodrigo Moya's avatar
Rodrigo Moya committed
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
/* Process each line from ping command */
void
scan_foreach (Netinfo * netinfo, gchar * line, gint len,
	      gpointer user_data)
{
	scan_data data;
	gint count;

	g_return_if_fail (netinfo != NULL);
	g_return_if_fail (line != NULL);

	count = strip_line (line, &data);
	if (count == 3) {
		scan_define_model (netinfo, data);
	}
}

static gint
strip_line (gchar * line, scan_data * data)
{
	gint count;

122 123 124 125
	if (! g_regex_match_simple ("^\\d+/\\w", line, 0, 0)) {
		return 0;
	}

Rodrigo Moya's avatar
Rodrigo Moya committed
126
	count = sscanf (line, SCAN_FORMAT,
127
			data->port, data->state, data->service);
Rodrigo Moya's avatar
Rodrigo Moya committed
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

	return count;
}

void
scan_define_model (Netinfo * netinfo, scan_data data)
{
	GtkTreeIter iter, sibling;
	GList *columns;
	GtkTreePath *path;
	GtkTreeModel *model;
	GtkTreeView *widget;

	g_return_if_fail (netinfo != NULL);

	widget = (GTK_TREE_VIEW (netinfo->output));

	/* Creation of GtkTreeView */
	gtk_tree_view_set_rules_hint (GTK_TREE_VIEW
					  (widget), TRUE);
	columns =
		gtk_tree_view_get_columns (GTK_TREE_VIEW
					   (widget));

	if (g_list_length (columns) == 0) {

		model = scan_create_model (widget);
		gtk_tree_view_set_model (GTK_TREE_VIEW
					 (widget), model);
	}
	g_list_free (columns);

	model =
		gtk_tree_view_get_model (GTK_TREE_VIEW
					 (widget));
	
	gtk_tree_view_get_cursor (GTK_TREE_VIEW (widget),
				  &path, NULL);

	if (path != NULL) {
		gtk_tree_model_get_iter (model, &sibling,
					 path);
		gtk_list_store_insert_after (GTK_LIST_STORE
						 (model),
						 &iter,
						 &sibling);
	} else {
		gtk_list_store_append (GTK_LIST_STORE
					   (model), &iter);
	}

	gtk_list_store_set (GTK_LIST_STORE (model), &iter,
				0, data.port,
				1, data.state,
				2, data.service, -1);

	gtk_tree_view_set_model (GTK_TREE_VIEW (widget),
				 model);
	path = gtk_tree_model_get_path (model, &iter);
	gtk_tree_view_set_cursor (GTK_TREE_VIEW (widget),
				  path, NULL, FALSE);
	gtk_tree_path_free (path);
}



static GtkTreeModel *
scan_create_model (GtkTreeView *widget)
{
	GtkCellRenderer *renderer;
	static GtkTreeViewColumn *column;
	GtkTreeModel *model;

	renderer = gtk_cell_renderer_text_new ();
	/* Number of bytes received in a ping reply */
	column =
	    gtk_tree_view_column_new_with_attributes
	    (_("Port"), renderer, "text", 0, NULL);
206
	g_object_set (G_OBJECT (renderer), "xalign", 1.0, NULL);
Rodrigo Moya's avatar
Rodrigo Moya committed
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
	gtk_tree_view_append_column (widget, column);

	renderer = gtk_cell_renderer_text_new ();
	/* IP address that reply a ping */
	column =
	    gtk_tree_view_column_new_with_attributes
	    (_("State"), renderer, "text", 1, NULL);
	gtk_tree_view_column_set_alignment (column, 0.5);
	gtk_tree_view_append_column (widget, column);

	renderer = gtk_cell_renderer_text_new ();
	/* Number of sequence of a ICMP request (ping) */
	column =
	    gtk_tree_view_column_new_with_attributes
	    (_("Service"), renderer, "text", 2, NULL);
	gtk_tree_view_append_column (widget, column);

	model = GTK_TREE_MODEL (gtk_list_store_new
				(3,
226
				 G_TYPE_STRING,
Rodrigo Moya's avatar
Rodrigo Moya committed
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
				 G_TYPE_STRING,
				 G_TYPE_STRING));
	return model;
}

/* Copy on clipboard */
void
scan_copy_to_clipboard (Netinfo * netinfo, gpointer user_data)
{
	GString *result, *content;

	g_return_if_fail (netinfo != NULL);

	/* The portscan output in text format:
	   Port, State, Service.
	   It's a tabular output, and these belongs to the column titles */
	result = g_string_new (_("Port\tState\tService\n"));
	
	content = util_tree_model_to_string (GTK_TREE_VIEW (netinfo->output));
	
	g_string_append_printf (result, "%s", content->str);
	
	gtk_clipboard_set_text (gtk_clipboard_get (GDK_NONE), result->str,
				result->len);

	g_string_free (content, TRUE);
	g_string_free (result, TRUE);
}