1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3
4# ***********************IMPORTANT NMAP LICENSE TERMS************************
5# *                                                                         *
6# * The Nmap Security Scanner is (C) 1996-2020 Insecure.Com LLC ("The Nmap  *
7# * Project"). Nmap is also a registered trademark of the Nmap Project.     *
8# *                                                                         *
9# * This program is distributed under the terms of the Nmap Public Source   *
10# * License (NPSL). The exact license text applying to a particular Nmap    *
11# * release or source code control revision is contained in the LICENSE     *
12# * file distributed with that version of Nmap or source code control       *
13# * revision. More Nmap copyright/legal information is available from       *
14# * https://nmap.org/book/man-legal.html, and further information on the    *
15# * NPSL license itself can be found at https://nmap.org/npsl. This header  *
16# * summarizes some key points from the Nmap license, but is no substitute  *
17# * for the actual license text.                                            *
18# *                                                                         *
19# * Nmap is generally free for end users to download and use themselves,    *
20# * including commercial use. It is available from https://nmap.org.        *
21# *                                                                         *
22# * The Nmap license generally prohibits companies from using and           *
23# * redistributing Nmap in commercial products, but we sell a special Nmap  *
24# * OEM Edition with a more permissive license and special features for     *
25# * this purpose. See https://nmap.org/oem                                  *
26# *                                                                         *
27# * If you have received a written Nmap license agreement or contract       *
28# * stating terms other than these (such as an Nmap OEM license), you may   *
29# * choose to use and redistribute Nmap under those terms instead.          *
30# *                                                                         *
31# * The official Nmap Windows builds include the Npcap software             *
32# * (https://npcap.org) for packet capture and transmission. It is under    *
33# * separate license terms which forbid redistribution without special      *
34# * permission. So the official Nmap Windows builds may not be              *
35# * redistributed without special permission (such as an Nmap OEM           *
36# * license).                                                               *
37# *                                                                         *
38# * Source is provided to this software because we believe users have a     *
39# * right to know exactly what a program is going to do before they run it. *
40# * This also allows you to audit the software for security holes.          *
41# *                                                                         *
42# * Source code also allows you to port Nmap to new platforms, fix bugs,    *
43# * and add new features.  You are highly encouraged to submit your         *
44# * changes as a Github PR or by email to the dev@nmap.org mailing list     *
45# * for possible incorporation into the main distribution. Unless you       *
46# * specify otherwise, it is understood that you are offering us very       *
47# * broad rights to use your submissions as described in the Nmap Public    *
48# * Source License Contributor Agreement. This is important because we      *
49# * fund the project by selling licenses with various terms, and also       *
50# * because the inability to relicense code has caused devastating          *
51# * problems for other Free Software projects (such as KDE and NASM).       *
52# *                                                                         *
53# * The free version of Nmap is distributed in the hope that it will be     *
54# * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of  *
55# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. Warranties,        *
56# * indemnification and commercial support are all available through the    *
57# * Npcap OEM program--see https://nmap.org/oem.                            *
58# *                                                                         *
59# ***************************************************************************/
60
61import gtk
62
63from zenmapGUI.SearchGUI import SearchGUI
64
65import zenmapCore.I18N  # lgtm[py/unused-import]
66from zenmapCore.UmitConf import is_maemo
67
68from zenmapGUI.higwidgets.higboxes import HIGVBox
69from zenmapGUI.higwidgets.higbuttons import HIGButton
70
71BaseSearchWindow = None
72hildon = None
73
74if is_maemo():
75    import hildon
76
77    class BaseSearchWindow(hildon.Window):
78        def __init__(self):
79            hildon.Window.__init__(self)
80
81        def _pack_widgets(self):
82            pass
83else:
84    class BaseSearchWindow(gtk.Window):
85        def __init__(self):
86            gtk.Window.__init__(self)
87            self.set_title(_("Search Scans"))
88            self.set_position(gtk.WIN_POS_CENTER)
89
90        def _pack_widgets(self):
91            self.vbox.set_border_width(4)
92
93
94class SearchWindow(BaseSearchWindow, object):
95    def __init__(self, load_method, append_method):
96        BaseSearchWindow.__init__(self)
97
98        self.set_default_size(600, 400)
99
100        self.load_method = load_method
101        self.append_method = append_method
102
103        self._create_widgets()
104        self._pack_widgets()
105        self._connect_widgets()
106
107    def _create_widgets(self):
108        self.vbox = HIGVBox()
109
110        self.bottom_hbox = gtk.HBox()
111        self.bottom_label = gtk.Label()
112        self.btn_box = gtk.HButtonBox()
113        self.btn_open = HIGButton(stock=gtk.STOCK_OPEN)
114        self.btn_append = HIGButton(_("Append"), gtk.STOCK_ADD)
115        self.btn_close = HIGButton(stock=gtk.STOCK_CLOSE)
116
117        self.search_gui = SearchGUI(self)
118
119    def _pack_widgets(self):
120        BaseSearchWindow._pack_widgets(self)
121
122        self.btn_box.set_layout(gtk.BUTTONBOX_END)
123        self.btn_box.set_spacing(4)
124        self.btn_box.pack_start(self.btn_close)
125        self.btn_box.pack_start(self.btn_append)
126        self.btn_box.pack_start(self.btn_open)
127
128        self.bottom_label.set_alignment(0.0, 0.5)
129        self.bottom_label.set_use_markup(True)
130
131        self.bottom_hbox.set_spacing(4)
132        self.bottom_hbox.pack_start(self.bottom_label, True)
133        self.bottom_hbox.pack_start(self.btn_box, False)
134
135        self.vbox.set_spacing(4)
136        self.vbox.pack_start(self.search_gui, True, True)
137        self.vbox.pack_start(self.bottom_hbox, False)
138
139        self.add(self.vbox)
140
141    def _connect_widgets(self):
142        # Double click on result, opens it
143        self.search_gui.result_view.connect(
144                "row-activated", self.open_selected)
145
146        self.btn_open.connect("clicked", self.open_selected)
147        self.btn_append.connect("clicked", self.append_selected)
148        self.btn_close.connect("clicked", self.close)
149        self.connect("delete-event", self.close)
150
151    def close(self, widget=None, event=None):
152        self.search_gui.close()
153        self.destroy()
154
155    def set_label_text(self, text):
156        self.bottom_label.set_label(text)
157
158    def open_selected(self, widget=None, path=None, view_column=None,
159            extra=None):
160        # Open selected results
161        self.load_method(self.results)
162
163        # Close Search Window
164        self.close()
165
166    def append_selected(self, widget=None, path=None, view_column=None,
167            extra=None):
168        # Append selected results
169        self.append_method(self.results)
170
171        # Close Search Window
172        self.close()
173
174    def get_results(self):
175        # Return list with parsed objects from result list store
176        return self.search_gui.selected_results
177
178    results = property(get_results)
179
180
181if __name__ == "__main__":
182    search = SearchWindow(lambda x: gtk.main_quit(), lambda x: gtk.main_quit())
183    search.show_all()
184    gtk.main()
185