001: /*
002: * GNetWatch
003: * Copyright 2006, 2007 Alexandre Fenyo
004: * gnetwatch@fenyo.net
005: *
006: * This file is part of GNetWatch.
007: *
008: * GNetWatch is free software; you can redistribute it and/or modify
009: * it under the terms of the GNU General Public License as published by
010: * the Free Software Foundation; either version 2 of the License, or
011: * (at your option) any later version.
012: *
013: * GNetWatch is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: * GNU General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public License
019: * along with GNetWatch; if not, write to the Free Software
020: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
021: */
022:
023: package net.fenyo.gnetwatch.actions;
024:
025: import net.fenyo.gnetwatch.*;
026: import net.fenyo.gnetwatch.GUI.GUI;
027: import net.fenyo.gnetwatch.actions.Action.InterruptCause;
028: import net.fenyo.gnetwatch.activities.Background;
029: import net.fenyo.gnetwatch.data.*;
030: import net.fenyo.gnetwatch.targets.*;
031:
032: import java.io.*;
033: import java.util.regex.Matcher;
034: import java.util.regex.Pattern;
035:
036: import org.apache.commons.logging.Log;
037: import org.apache.commons.logging.LogFactory;
038: import org.eclipse.swt.SWT;
039: import org.eclipse.swt.widgets.MessageBox;
040:
041: /**
042: * Instances of this action class use NMAP to explore their target
043: * and create events of type EventReachable.
044: * @author Alexandre Fenyo
045: * @version $Id: ActionNmap.java,v 1.2 2007/03/12 05:04:14 fenyo Exp $
046: */
047:
048: // on devrait utiliser l'option -oX file pour traiter l'output en XML...
049: public class ActionNmap extends Action {
050: private static Log log = LogFactory.getLog(ActionNmap.class);
051:
052: // main, Background & Queue threads
053: // supports any thread
054: private ExternalCommand cmd_nmap = null;
055:
056: private String address = "";
057: private boolean invoked = false;
058:
059: /**
060: * Constructor.
061: * @param target target this action works on.
062: * @param background queue manager by which this action will add events.
063: */
064: // GUI thread
065: // supports any thread
066: public ActionNmap(final Target target, final Background background) {
067: super (target, background);
068: setItem("nmap");
069: }
070:
071: /**
072: * Constructor.
073: * @param none.
074: */
075: // GUI thread
076: // supports any thread
077: public ActionNmap() {
078: setItem("nmap");
079: }
080:
081: /**
082: * Called to inform about the current GUI.
083: * @param gui current GUI instance.
084: * @return void.
085: */
086: protected void initialize(final GUI gui) {
087: super .initialize(gui);
088: }
089:
090: /**
091: * Returns the associated target.
092: * @param none.
093: * @return Target associated target.
094: */
095: // any thread
096: public String getQueueName() {
097: return "nmap";
098: }
099:
100: /**
101: * Returns the timeout associated with this action.
102: * @param none.
103: * @return long timeout.
104: */
105: // any thread
106: public long getMaxDelay() {
107: return new Long(getGUI().getConfig()
108: .getProperty("nmap.timeout"));
109: }
110:
111: /**
112: * Asks this action to stop rapidely.
113: * @param cause cause.
114: * @return void.
115: * @throws IOException IO exception.
116: */
117: // main & Background threads
118: // supports any thread
119: public void interrupt(final InterruptCause cause)
120: throws IOException {
121: if (cmd_nmap != null) {
122: cmd_nmap.end();
123: if (cause == InterruptCause.timeout)
124: getGUI().appendConsole(
125: getGUI().getConfig().getPattern(
126: "nmap_interrupted", address)
127: + "<BR/>");
128: }
129: }
130:
131: /**
132: * Computes the signature of the target.
133: * @param none.
134: * @return void.
135: * @throws IOException IO exception.
136: * @throws InterruptedException exception.
137: */
138: // Queue thread
139: // supports any thread
140: public void invoke() throws IOException, InterruptedException {
141: if (isDisposed() == true)
142: return;
143:
144: super .invoke();
145:
146: if (invoked == true)
147: return;
148: invoked = true;
149:
150: try {
151: final String[] cmdLine;
152:
153: if (TargetIPv4.class.isInstance(getTarget())) {
154: final TargetIPv4 target = (TargetIPv4) getTarget();
155: address = target.getAddress().toString();
156: address = address.substring(1 + address.indexOf('/'));
157: cmdLine = new String[] { "nmap", "-A", address };
158: } else if (TargetIPv6.class.isInstance(getTarget())) {
159: final TargetIPv6 target = (TargetIPv6) getTarget();
160: address = target.getAddress().toString();
161: address = address.substring(1 + address.indexOf('/'));
162: cmdLine = new String[] { "nmap", "-6", "-A", address };
163: } else
164: return;
165:
166: String cmd_line = "";
167: for (final String part : cmdLine)
168: cmd_line += part + " ";
169: getGUI().setStatus(
170: getGUI().getConfig()
171: .getPattern("forking", cmd_line));
172:
173: cmd_nmap = new ExternalCommand(cmdLine, true);
174: try {
175: cmd_nmap.fork();
176: } catch (final IOException ex) {
177: getGUI().asyncExecIfNeeded(new Runnable() {
178: public void run() {
179: final MessageBox dialog = new MessageBox(
180: getGUI().getShell(), SWT.ICON_ERROR
181: | SWT.OK);
182: dialog.setText(getGUI().getConfig().getString(
183: "nmap_error"));
184: dialog.setMessage(getGUI().getConfig()
185: .getString("nmap_error_long"));
186: dialog.open();
187: }
188: });
189:
190: throw ex;
191: }
192:
193: final String cmd_output = cmd_nmap.readStdout();
194: cmd_nmap.end();
195:
196: final Matcher match = Pattern.compile(
197: "(\r|\n)Running: (.*)").matcher(cmd_output);
198: if (match.find() == true)
199: if (match.group(2) != null)
200: getParents().get(0).setType(match.group(2));
201: getTarget().addEvent(new EventNmap(cmd_output));
202:
203: } finally {
204: getGUI().asyncExecIfNeeded(new Runnable() {
205: public void run() {
206: synchronized (getGUI().sync_tree) {
207: removeVisualElements(getParents().get(0));
208: }
209: }
210: });
211: }
212: }
213:
214: /**
215: * Called when this element is being removed.
216: * @param none.
217: * @return void.
218: */
219: protected void disposed() {
220: // remove us from the action queue
221: super .disposed();
222:
223: // interrupt if currently running
224: try {
225: interrupt(InterruptCause.removed);
226: } catch (final IOException ex) {
227: log.error("Exception", ex);
228: }
229: }
230: }
|