root/branch/k0p/umitGUI/ProfileEditor.py @ 861

Revision 861, 17.4 kB (checked in by kop-labs, 6 years ago)

delete button

Line 
1#!/usr/bin/env python
2# Copyright (C) 2005 Insecure.Com LLC.
3#
4# Copyright (C) : Adriano Monteiro Marques <py.adriano@gmail.com>
5# Author: Luis Antonio Bastiao Silva <luis.kop@gmail.com>
6#
7# This program is free software; you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation; either version 2 of the License, or
10# (at your option) any later version.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with this program; if not, write to the Free Software
19# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20#!/usr/bin/env python
21# Copyright (C) 2005 Insecure.Com LLC.
22#
23# Author: Adriano Monteiro Marques <py.adriano@gmail.com>
24#
25# This program is free software; you can redistribute it and/or modify
26# it under the terms of the GNU General Public License as published by
27# the Free Software Foundation; either version 2 of the License, or
28# (at your option) any later version.
29#
30# This program is distributed in the hope that it will be useful,
31# but WITHOUT ANY WARRANTY; without even the implied warranty of
32# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33# GNU General Public License for more details.
34#
35# You should have received a copy of the GNU General Public License
36# along with this program; if not, write to the Free Software
37# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
38
39import gtk
40
41from higwidgets.higwindows import HIGWindow
42from higwidgets.higboxes import HIGVBox, HIGHBox, HIGSpacer, hig_box_space_holder
43from higwidgets.higexpanders import HIGExpander
44from higwidgets.higlabels import HIGSectionLabel, HIGEntryLabel
45from higwidgets.higscrollers import HIGScrolledWindow
46from higwidgets.higtextviewers import HIGTextView
47from higwidgets.higbuttons import HIGButton
48from higwidgets.higtables import HIGTable
49from higwidgets.higdialogs import HIGAlertDialog, HIGDialog
50from umitGUI.ScanToolbar import ScanCommandToolbar, ScanToolbar
51from umitGUI.ProfileCombo import ProfileCombo
52
53
54
55
56from umitGUI.OptionBuilder import *
57
58from umitCore.Paths import Path
59from umitCore.NmapCommand import CommandConstructor
60from umitCore.UmitConf import Profile, CommandProfile
61from umitCore.Logging import log
62from umitCore.I18N import _
63
64# Testing at devel
65from os.path import split, join
66
67from umitCore.Paths import Path
68Path.set_umit_conf(join(split(__file__)[0], 'config', 'umit.conf'))
69#END DEV TEST
70
71
72
73profile_editor = Path.profile_editor
74
75
76class ProfileEditor(HIGWindow):
77    def __init__(self, profile_name=None, delete=True):
78        HIGWindow.__init__(self)
79        self.set_title(_('Profile Editor'))
80        self.set_position(gtk.WIN_POS_CENTER)
81       
82        self.__create_widgets()
83        self.__pack_widgets()
84       
85        self.options = OptionBuilder(profile_editor)
86         
87        log.debug("Option groups: %s" % str(self.options.groups))
88        log.debug("Option section names: %s" % str(self.options.section_names))
89        #log.debug("Option tabs: %s" % str(self.options.tabs))
90       
91        self.constructor = CommandConstructor()
92        self.profile = CommandProfile()
93       
94        self.deleted = False
95        self.options_used = []
96       
97        if profile_name:
98            log.debug("Showing profile %s" % profile_name)
99            prof = self.profile.get_profile(profile_name)
100            self.options_used = prof['options']
101
102            # Interface settings
103            self.profile_name_entry.set_text(profile_name)
104            self.profile_hint_entry.set_text(prof['hint'])
105            self.profile_description_text.get_buffer().set_text(prof['description'])
106            self.profile_annotation_text.get_buffer().set_text(prof['annotation'])
107
108            if delete:
109                # Removing profile. It must be saved again
110                self.profile.remove_profile(profile_name)
111                self.deleted = True
112       
113        for tab in self.options.groups:
114            self.__create_tab(tab, self.options.section_names[tab], self.options.tabs[tab])
115       
116        self.update_command()
117   
118    def update_command(self):
119        """Regenerate command with target '<target>' and set the value for the command entry"""
120        self.command_entry.set_text(self.constructor.get_command('<target>'))
121
122    def help(self, widget):
123        d = HIGAlertDialog(parent=self,
124                           message_format=_("Help not implemented"),
125                           secondary_text=_("Umit help is not implemented yet."))
126        d.run()
127        d.destroy()
128   
129    def __create_widgets(self):
130        self.main_vbox = HIGVBox()
131        self.choose_profile =ProfileChosse()
132       
133       
134        self.command_expander = HIGExpander('<b>'+_('Command')+'</b>')
135        self.command_expander.set_expanded(True)
136        self.command_entry = gtk.Entry()
137       
138        self.widgets = {}
139       
140        self.notebook = gtk.Notebook()
141       
142        # Profile info page
143        self.profile_info_vbox = HIGVBox()
144        self.profile_info_label = HIGSectionLabel(_('Profile Information'))
145        self.profile_name_label = HIGEntryLabel(_('Profile name'))
146        self.profile_name_entry = gtk.Entry()
147        self.profile_hint_label = HIGEntryLabel(_('Hint'))
148        self.profile_hint_entry = gtk.Entry()
149        self.profile_description_label = HIGEntryLabel(_('Description'))
150        self.profile_description_scroll = HIGScrolledWindow()
151        self.profile_description_text = HIGTextView()
152        self.profile_annotation_label = HIGEntryLabel(_('Annotation'))
153        self.profile_annotation_scroll = HIGScrolledWindow()
154        self.profile_annotation_text = HIGTextView()
155       
156        # Buttons
157        self.buttons_hbox = HIGHBox()
158       
159        self.help_button = HIGButton(stock=gtk.STOCK_HELP)
160        self.help_button.connect('clicked', self.help)
161       
162        self.delete_button = HIGButton(stock=gtk.STOCK_DELETE)
163        self.delete_button.connect('clicked', self.delete_profile)
164       
165        self.cancel_button = HIGButton(stock=gtk.STOCK_CANCEL)
166        self.cancel_button.connect('clicked', self.quit)
167       
168        self.ok_button = HIGButton(stock=gtk.STOCK_OK)
169        self.ok_button.connect('clicked', self.save_profile)
170   
171    def __pack_widgets(self):
172        self.add(self.main_vbox)
173       
174        # Packing widgets to main_vbox
175        self.main_vbox._pack_expand_fill(self.choose_profile)
176        self.main_vbox._pack_noexpand_nofill(self.command_expander)
177        self.main_vbox._pack_expand_fill(self.notebook)
178        self.main_vbox._pack_noexpand_nofill(self.buttons_hbox)
179       
180        # Packing command_entry on command_expander
181        self.command_expander.hbox.pack_start(self.command_entry)
182       
183        # Packing profile information tab on notebook
184        self.notebook.append_page(self.profile_info_vbox, gtk.Label(_('Profile')))
185        self.profile_info_vbox.set_border_width(5)
186        table = HIGTable()
187        self.profile_info_vbox._pack_noexpand_nofill(self.profile_info_label)
188        self.profile_info_vbox._pack_noexpand_nofill(HIGSpacer(table))
189       
190        self.profile_annotation_scroll.add(self.profile_annotation_text)
191        self.profile_description_scroll.add(self.profile_description_text)
192       
193        vbox_desc = HIGVBox()
194        vbox_desc._pack_noexpand_nofill(self.profile_description_label)
195        vbox_desc._pack_expand_fill(hig_box_space_holder())
196       
197        vbox_ann = HIGVBox()
198        vbox_ann._pack_noexpand_nofill(self.profile_annotation_label)
199        vbox_ann._pack_expand_fill(hig_box_space_holder())
200       
201        table.attach(self.profile_name_label,0,1,0,1,xoptions=0)
202        table.attach(self.profile_name_entry,1,2,0,1)
203        table.attach(self.profile_hint_label,0,1,1,2,xoptions=0)
204        table.attach(self.profile_hint_entry,1,2,1,2)
205        table.attach(vbox_desc,0,1,2,3,xoptions=0)
206        table.attach(self.profile_description_scroll,1,2,2,3)
207        table.attach(vbox_ann,0,1,3,4,xoptions=0)
208        table.attach(self.profile_annotation_scroll,1,2,3,4)
209       
210        # Packing buttons on button_hbox
211        self.buttons_hbox.pack_start(self.help_button)
212        self.buttons_hbox.pack_start(self.delete_button)
213        self.buttons_hbox.pack_start(self.cancel_button)
214        self.buttons_hbox.pack_start(self.ok_button)
215       
216        self.buttons_hbox.set_border_width(5)
217        self.buttons_hbox.set_spacing(6)
218
219    def __create_tab(self, tab_name, section_name, tab_widgets):
220        vbox = HIGVBox()
221        table = HIGTable()
222        section = HIGSectionLabel(section_name)
223        log.debug(">>> Tab name: %s" % tab_name)
224        log.debug(">>>Creating profile editor section: %s" % section_name)
225       
226        vbox._pack_noexpand_nofill(section)
227        vbox._pack_noexpand_nofill(HIGSpacer(table))
228       
229        vbox.set_border_width(5)
230       
231        self.widgets[tab_name] = tab_widgets
232       
233        y1 = 0
234        y2 = 1
235       
236        for widget in tab_widgets:
237            log.debug(">>> Widget: %s" % str(widget))
238            if widget[1] == None:
239                table.attach(widget[0],0,2,y1,y2)
240            else:
241                table.attach(widget[0],0,1,y1,y2)
242                table.attach(widget[1],1,2,y1,y2)
243           
244            if type(widget[0]) == type(OptionCheck()):
245                widget[0].connect('toggled', self.update_check, widget[1])
246               
247                if widget[0].option['name'] in self.options_used:
248                    widget[0].set_active(True)
249           
250            te = type(widget[1])
251           
252            if te == type(OptionList()):
253                widget[1].connect('changed',self.update_list_option)
254               
255                for wid in range(widget[1].list.__len__()):
256                    if widget[1].list[wid][0] in self.options_used:
257                        widget[1].set_active(wid)
258            elif te == type(OptionIntSpin()) or\
259                 te == type(OptionFloatSpin()) or\
260                 te == type(OptionEntry()):
261                widget[1].connect('changed', self.update_entry, widget[0])
262            elif te == type(OptionLevelSpin()):
263                widget[1].connect('changed', self.update_level, widget[0])
264            elif te == type(OptionFile()):
265                widget[1].entry.connect('changed', self.update_entry, widget[0])
266           
267            y1+=1;y2+=1
268       
269        self.notebook.append_page(vbox, gtk.Label(tab_name))
270   
271    def update_entry(self, widget, check):
272        if not check.get_active():
273            check.set_active(True)
274       
275        self.constructor.remove_option(check.option['name'])
276        self.constructor.add_option(check.option['name'], widget.get_text())
277        self.update_command()
278   
279    def update_level(self, widget, check):
280        if not check.get_active():
281            check.set_active(True)
282       
283        try:
284            self.constructor.remove_option(check.option['name'])
285            if int(widget.get_text()) == 0:
286                check.set_active(False)
287            else:
288                self.constructor.add_option(check.option['name'],\
289                                        level=int(widget.get_text()))
290        except:pass
291        self.update_command()
292   
293    def save_profile(self, widget):
294        profile_name = self.profile_name_entry.get_text()
295        if profile_name == '':
296            alert = HIGAlertDialog(message_format=_('Unnamed profile'),\
297                                   secondary_text=_('You must provide a name \
298for this profile.'))
299            alert.run()
300            alert.destroy()
301           
302            self.notebook.set_current_page(0)
303            self.profile_name_entry.grab_focus()
304           
305            return None
306       
307        command = self.constructor.get_command('%s')
308        hint = self.profile_hint_entry.get_text()
309       
310        buf = self.profile_description_text.get_buffer()
311        description = buf.get_text(buf.get_start_iter(),\
312                                      buf.get_end_iter())
313       
314        buf = self.profile_annotation_text.get_buffer()
315        annotation = buf.get_text(buf.get_start_iter(),\
316                                      buf.get_end_iter())
317       
318        self.profile.add_profile(profile_name,\
319                                 command=command,\
320                                 hint=hint,\
321                                 description=description,\
322                                 annotation=annotation,\
323                                 options=','.join(self.options_used))
324       
325        self.deleted = False
326        self.quit()
327   
328    def clean_profile_info(self):
329        self.profile_name_entry.set_text('')
330        self.profile_hint_entry.set_text('')
331        self.profile_description_text.get_buffer().set_text('')
332        self.profile_annotation_text.get_buffer().set_text('')
333   
334    def update_list_option(self, widget):
335        try:widget.last_selected
336        except:pass
337        else:
338            self.constructor.remove_option(widget.last_selected)
339            try:del(self.options_used[self.options_used.index\
340                                      (widget.last_selected)])
341            except:pass
342       
343        option_name = widget.options[widget.get_active()]['name']
344       
345        self.constructor.add_option(option_name)
346        self.options_used.append(option_name)
347        widget.last_selected = option_name
348       
349        self.update_command()
350   
351    def update_check(self, check, extra):
352        if check.get_active():
353            te = type(extra)
354            if te == type(OptionEntry()) or\
355               te == type(OptionIntSpin()) or\
356               te == type(OptionFloatSpin()):
357                self.update_entry(extra, check)
358            elif te == type(OptionLevelSpin()):
359                self.update_level(extra, check)
360            elif te == type(OptionFile()):
361                self.update_entry(extra.entry, check)
362            elif te == type(OptionInterface()):
363                self.update_entry(extra.child, check)
364            else:
365                self.constructor.add_option(check.option['name'])
366                self.update_command()
367           
368            self.options_used.append(check.option['name'])
369        else:
370            self.constructor.remove_option(check.option['name'])
371            self.update_command()
372            try:del(self.options_used[self.options_used.index\
373                                      (check.option['name'])])
374            except:pass
375       
376        #print self.options_used
377   
378    def set_notebook(self, notebook):
379        self.scan_notebook = notebook
380   
381    def quit(self, widget=None, extra=None):
382        if self.deleted:
383            dialog = HIGDialog(buttons=(gtk.STOCK_OK, gtk.RESPONSE_OK,
384                                        gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL))
385            alert = HIGEntryLabel('<b>'+_("Deleting Profile")+'</b>')
386            text = HIGEntryLabel(_('Your profile is going to be deleted! Click\
387 Ok to continue, or Cancel to go back to Profile Editor.'))
388            hbox = HIGHBox()
389            hbox.set_border_width(5)
390            hbox.set_spacing(12)
391           
392            vbox = HIGVBox()
393            vbox.set_border_width(5)
394            vbox.set_spacing(12)
395           
396            image = gtk.Image()
397            image.set_from_stock(gtk.STOCK_DIALOG_WARNING, gtk.ICON_SIZE_DIALOG)
398           
399            vbox.pack_start(alert)
400            vbox.pack_start(text)
401            hbox.pack_start(image)
402            hbox.pack_start(vbox)
403           
404            dialog.vbox.pack_start(hbox)
405            dialog.vbox.show_all()
406           
407            response = dialog.run()
408            dialog.destroy()
409           
410            if response == gtk.RESPONSE_CANCEL:
411                return None
412       
413        self.destroy()
414       
415       
416        for i in xrange(self.scan_notebook.get_n_pages()):
417            page = self.scan_notebook.get_nth_page(i)
418            page.toolbar.profile_entry.update()
419       
420        #page.toolbar.scan_profile.profile_entry.child.\
421        #    set_text(self.profile_name_entry.get_text())
422    def delete_profile(self):
423        """
424        delete profile
425        """
426       
427    def update_all_configs(self):
428        """
429        Update all configurations when change ProfileChoose
430        """
431   
432   
433       
434       
435class ProfileChosse(HIGHBox):
436    def __init__(self):
437        HIGHBox.__init__(self)       
438        self._create_profile()
439       
440    #def _create_profile(self):
441    def __init__(self):
442        HIGHBox.__init__(self)
443
444        self._create_profile()
445       
446        self.change_button = gtk.Button(_("Change"))
447       
448        self._pack_noexpand_nofill(self.profile_label)
449        self._pack_expand_fill(self.profile_entry)
450       
451        self._pack_noexpand_nofill(self.change_button)
452
453        self.profile_entry.set_focus_child(self.profile_entry.child)
454
455        # Events
456        self.profile_entry.child.connect('activate',
457                        lambda x: self.change_button.clicked())
458
459    def _create_profile(self):
460        self.profile_label = HIGEntryLabel(_('Profile:'))
461        self.profile_entry = ProfileCombo()
462        self.update()
463    def update(self):
464        self.update_profile()
465    def update_profile(self):
466        self.profile_entry.update()
467
468if __name__ == '__main__':
469    p = ProfileEditor()
470    p.show_all()
471   
472    gtk.main()
473
Note: See TracBrowser for help on using the browser.