class SchedSchemaEditor(HIGWindow):
    """
    Scheduler Schemas Editor
    """

    def __init__(self, daddy=None):
        HIGWindow.__init__(self)

        self.daddy = daddy
        self.wtitle = _("Scan Scheduler Editor")

        # header
        self.title_markup = "<span size='16500' weight='heavy'>%s</span>"
        self.ttitle = HIGEntryLabel("")
        self.ttitle.set_line_wrap(False)
        self.ttitle.set_markup(self.title_markup % self.wtitle)
        self.umit_logo = gtk.Image()
        self.umit_logo.set_from_file(logo)
        # schemas name
        self.schema_name_lbl = HIGEntryLabel(_("Schema Name"))
        self.schema_name = gtk.combo_box_entry_new_text()
        self.schema_name.connect("changed", self._check_schema)
        # target and scan profiles
        # self.target_lbl = HIGEntryLabel(_("Target"))
        # self.target = gtk.Entry()
        self.scan_name_lbl = HIGEntryLabel(_("Scan Profile"))
        self.scan_name = ProfileCombo()
        self.scan_name.update()
        self.scan_name.set_active(0)
        self.scan_name.connect("changed", self._set_scan_command)
        # scan command
        self.scan_command_lbl = HIGEntryLabel(_("Command"))
        self.scan_command = gtk.Entry()
        # scheduling profile
        self.sched_name_lbl = HIGEntryLabel(_("Scheduling Profile"))
        self.sched_name = gtk.combo_box_new_text()
        self.sched_name_edit = gtk.Button(stock=gtk.STOCK_EDIT)
        blbl = self.sched_name_edit.get_children()[0].get_children()[0].get_children()[1]
        blbl.set_text(_("Edit Profiles"))
        self.sched_name_edit.connect("clicked", self._edit_schedprofiles)
        # schema settings
        self.schema_sett_frame = HIGFrame()
        self.setting_saveto = gtk.CheckButton(_("Save outputs to"))
        self.setting_saveto_entry = gtk.Entry()
        self.setting_saveto_browse = gtk.Button(_("..."))
        self.setting_saveto_browse.connect("clicked", self._select_saveto)
        self.setting_mailto = gtk.CheckButton(_("Send output to email"))
        self.setting_mailto_entry = gtk.Entry()
        self.setting_smtp_lbl = HIGEntryLabel(_("SMTP Schema"))
        self.setting_smtp = gtk.combo_box_new_text()
        self.setting_addtoinv = gtk.CheckButton(_("Add to the Inventory"))
        self.setting_enabled = gtk.CheckButton(_("Enabled"))
        # bottom buttons
        self.help = HIGButton(stock=gtk.STOCK_HELP)
        self.help.connect("clicked", self._show_help)
        self.apply = HIGButton(stock=gtk.STOCK_APPLY)
        self.apply.connect("clicked", self._save_schema)
        self.cancel = HIGButton(stock=gtk.STOCK_CANCEL)
        self.cancel.connect("clicked", self._exit)
        self.ok = HIGButton(stock=gtk.STOCK_OK)
        self.ok.connect("clicked", self._save_schema_and_leave)

        self.load_smtp_schemas()
        self._set_scan_command(None)
        self.profile_running = None  # no SchedProfileEditor instance is running.
        self._load_pscheds()
        self.load_schemas()

        self.__set_props()
        self.__do_layout()

        self.connect("destroy", self._exit)

    def load_smtp_schemas(self):
        """
        Load smtp profiles.
        """
        schemas = ConfigParser()
        schemas.read(Path.smtp_schemas)

        self.smtp_sections = []
        self.setting_smtp.get_model().clear()
        for section in schemas.sections():
            self.smtp_sections.append(section)
            self.setting_smtp.append_text(section)

        self.setting_smtp.set_active(0)

    def load_schemas(self):
        """
        Load schemas profiles.
        """
        schemas = ConfigParser()
        schemas.read(Path.sched_schemas)

        self.sections = []
        self.schema_name.get_model().clear()
        for section in schemas.sections():
            self.sections.append(section)
            self.schema_name.append_text(section)

        self.schema_name.set_active(0)
        self._check_schema(None)

    def _load_schema(self):
        """
        Load current set schedule schema.
        """
        schema = ConfigParser()
        schema.read(Path.sched_schemas)

        values = {
            "command": self.scan_command.set_text,
            "saveto": self.setting_saveto_entry.set_text,
            "mailto": self.setting_mailto_entry.set_text,
        }
        enable = {"saveto": self.setting_saveto.set_active, "mailto": self.setting_mailto.set_active}

        for item in schema.items(self.schema_name.get_active_text()):
            if item[0] == "addtoinv":
                self.setting_addtoinv.set_active(int(item[1]))
                if item[1] == "2":
                    self.apply.set_sensitive(False)
                    self.ok.set_sensitive(False)
                else:
                    self.apply.set_sensitive(True)
                    self.ok.set_sensitive(True)
            elif item[0] == "enabled":
                self.setting_enabled.set_active(int(item[1]))
            elif item[0] == "profile":
                pindx = self.profiles.index(item[1])
                self.sched_name.set_active(pindx)
            elif item[0] == "smtp":
                if item[1]:
                    pindx = self.smtp_sections.index(item[1])
                    self.setting_smtp.set_active(pindx)
            else:
                values[item[0]](item[1])
                if item[0] in ("saveto", "mailto"):
                    if len(item[1]):
                        enable[item[0]](True)
                    else:
                        enable[item[0]](False)

    def _check_schema(self, event):
        """
        Check if current text in schema_name combobox is a schema name.
        """
        if self.schema_name.get_active_text() in self.sections:
            # load schema
            self._load_schema()
        else:
            # reset to default values
            self.apply.set_sensitive(True)
            self.ok.set_sensitive(True)
            self.setting_addtoinv.set_active(False)
            self.setting_enabled.set_active(False)
            self.setting_mailto.set_active(False)
            self.setting_mailto_entry.set_text("")
            self.setting_saveto.set_active(False)
            self.setting_saveto_entry.set_text("")

        self.schema_sett_frame._set_label(self.schema_name.get_active_text() + " - Settings")

    def _set_scan_command(self, event):
        """
        Set scan command based on chosen profile.
        """
        profile = self.scan_name.get_selected_profile()
        cmd_profile = CommandProfile()
        command = cmd_profile.get_command(profile)
        self.scan_command.set_text(command % "<target>")

    def _load_pscheds(self):
        """
        Load scheduling profiles.
        """
        pscheds = ConfigParser()
        pscheds.read(Path.sched_profiles)

        self.profiles = []
        self.sched_name.get_model().clear()
        for section in pscheds.sections():
            self.sched_name.append_text(section)
            self.profiles.append(section)

        self.sched_name.set_active(0)

    def _edit_schedprofiles(self, event):
        """
        Open Scheduling Profiles Editor.
        """
        if self.profile_running:
            return

        win = SchedProfileEditor(self, self.sched_name.get_active_text())
        win.show_all()
        self.profile_running = win

    def _select_saveto(self, event):
        """
        Select directory to save file.
        """
        dir_chooser = DirectoryChooserDialog(_("Select a directory"))

        dir_chooser.run()
        dir_chosen = dir_chooser.get_filename()
        dir_chooser.destroy()
        self.setting_saveto_entry.set_text(dir_chosen)

    def _save_schema(self, event):
        """
        Save current schema.
        """
        schema = self.schema_name.get_active_text()
        command = self.scan_command.get_text()
        schedule = self.sched_name.get_active_text()
        mailto = self.setting_mailto.get_active()

        if not schema or not schedule or not command or "<target>" in command:
            dlg = HIGAlertDialog(
                self,
                message_format=_(
                    "Scheduling Schema - Error\
 while saving."
                ),
                secondary_text=_(
                    'There is some error in at \
least one of the following fields: "Schema name", "Command" or "Scheduling\
 Profile"\n\nCheck if "Schema name" is not empty.\nCheck if "Command" does\
 contain "<target>" on it.\nCheck if there is some "Scheduling Profile" \
selected.'
                ),
            )

            dlg.run()
            dlg.destroy()
            return

        if mailto and not self.setting_smtp.get_active_text():
            dlg = HIGAlertDialog(
                self,
                message_format=_(
                    "Scheduling Schema - Error\
 while saving."
                ),
                secondary_text=_(
                    "You need to create a \
a SMTP Schema for sending email."
                ),
            )

            dlg.run()
            dlg.destroy()
            return

        # check for output existance
        if self.setting_saveto.get_active() and not os.path.isdir(self.setting_saveto_entry.get_text()):

            dlg = HIGAlertDialog(
                self,
                message_format=_(
                    "Scheduling Schema - Error\
 while saving."
                ),
                secondary_text=_(
                    "You especified an invalid \
directory to save scans output."
                ),
            )

            dlg.run()
            dlg.destroy()
            return

        # write schema to file
        s_cfg = ConfigParser()
        s_cfg.read(Path.sched_schemas)

        if not s_cfg.has_section(schema):
            new_sec = True
            s_cfg.add_section(schema)
        else:
            new_sec = False

        s_cfg.set(schema, "profile", schedule)
        s_cfg.set(schema, "command", command)
        if self.setting_enabled.get_active():
            s_cfg.set(schema, "enabled", "1")
        else:
            s_cfg.set(schema, "enabled", "0")
        if self.setting_addtoinv.get_active():
            s_cfg.set(schema, "addtoinv", "1")
        else:
            s_cfg.set(schema, "addtoinv", "0")
        if self.setting_saveto.get_active():
            s_cfg.set(schema, "saveto", self.setting_saveto_entry.get_text())
        else:
            s_cfg.set(schema, "saveto", "")
        if mailto:
            s_cfg.set(schema, "mailto", self.setting_mailto_entry.get_text())
            s_cfg.set(schema, "smtp", self.setting_smtp.get_active_text())
        else:
            s_cfg.set(schema, "mailto", "")
            s_cfg.set(schema, "smtp", "")

        s_cfg.write(open(Path.sched_schemas, "w"))

        if new_sec:
            self.load_schemas()

        if self.daddy:
            self.daddy.load_schemas()

    def _save_schema_and_leave(self, event):
        """
        Save current schema and close editor.
        """
        self._save_schema(None)
        self._exit(None)

    def _show_help(self, event):
        """
        Show help for Scan Scheduler Editor.
        """
        show_help(self, "scheduler.html#setting-up-a-schedule")

    def __set_props(self):
        """
        Set window properties.
        """
        self.set_title(self.wtitle)
        self.set_default_size(440, -1)

    def __do_layout(self):
        """
        Layout widgets in window.
        """
        main_vbox = HIGVBox()
        main_vbox.set_border_width(5)
        main_vbox.set_spacing(12)
        header_hbox = HIGHBox()
        schema_table = HIGTable()
        schedsn_hbox = HIGHBox()
        sett_table = HIGTable()
        btns_hbox = HIGHBox()

        header_hbox._pack_expand_fill(self.ttitle)
        header_hbox._pack_noexpand_nofill(self.umit_logo)

        # schema name
        schema_table.attach_label(self.schema_name_lbl, 0, 1, 0, 1)
        schema_table.attach_entry(self.schema_name, 1, 2, 0, 1)

        # target and scan profile
        schema_table.attach_label(self.scan_name_lbl, 0, 1, 1, 2)
        schema_table.attach_entry(self.scan_name, 1, 2, 1, 2)

        # scan command
        schema_table.attach_label(self.scan_command_lbl, 0, 1, 2, 3)
        schema_table.attach_label(self.scan_command, 1, 2, 2, 3)

        # scheduling profile
        schedsn_hbox._pack_expand_fill(self.sched_name)
        schedsn_hbox._pack_noexpand_nofill(self.sched_name_edit)

        schema_table.attach_label(self.sched_name_lbl, 0, 1, 3, 4)
        schema_table.attach_entry(schedsn_hbox, 1, 2, 3, 4)

        # settings frame
        settings_align = gtk.Alignment(0.5, 0.5, 1, 1)
        settings_align.set_padding(6, 0, 12, 0)
        schemasett_hbox = HIGVBox()

        # saveto
        sett_hbox = HIGHBox()
        sett_hbox._pack_expand_fill(self.setting_saveto_entry)
        sett_hbox._pack_noexpand_nofill(self.setting_saveto_browse)
        sett_table.attach_label(self.setting_saveto, 0, 1, 0, 1)
        sett_table.attach_entry(sett_hbox, 1, 2, 0, 1)

        # mailto, smtp
        sett_hbox = HIGHBox()
        sett_hbox._pack_expand_fill(self.setting_mailto_entry)
        sett_hbox._pack_noexpand_nofill(self.setting_smtp_lbl)
        sett_hbox._pack_expand_fill(self.setting_smtp)
        sett_table.attach_label(self.setting_mailto, 0, 1, 1, 2)
        sett_table.attach_entry(sett_hbox, 1, 2, 1, 2)
        schemasett_hbox._pack_noexpand_nofill(sett_table)

        # add to inventory
        sett_hbox = HIGHBox()
        sett_hbox._pack_noexpand_nofill(self.setting_addtoinv)
        schemasett_hbox._pack_noexpand_nofill(sett_hbox)

        # enabled/disabled
        sett_hbox = HIGHBox()
        sett_hbox._pack_noexpand_nofill(self.setting_enabled)
        schemasett_hbox._pack_noexpand_nofill(sett_hbox)
        settings_align.add(schemasett_hbox)

        self.schema_sett_frame.add(settings_align)

        # bottom buttons
        btns_hbox.set_homogeneous(True)
        btns_hbox._pack_expand_fill(self.help)
        btns_hbox._pack_expand_fill(hig_box_space_holder())
        btns_hbox._pack_expand_fill(self.apply)
        btns_hbox._pack_expand_fill(self.cancel)
        btns_hbox._pack_expand_fill(self.ok)

        main_vbox._pack_noexpand_nofill(header_hbox)
        main_vbox._pack_noexpand_nofill(gtk.HSeparator())
        main_vbox._pack_noexpand_nofill(schema_table)
        main_vbox._pack_noexpand_nofill(gtk.HSeparator())
        main_vbox._pack_noexpand_nofill(self.schema_sett_frame)
        main_vbox.pack_end(btns_hbox, False, False, 0)

        self.add(main_vbox)

    def _exit(self, event):
        """
        Close current and window and profile editor if it is running.
        """
        if self.profile_running:
            self.profile_running._exit(None)

        if self.daddy:
            self.daddy.schemawin = None

        self.destroy()

    def _get_profile_running(self):
        """
        Get profile editor running instance.
        """
        return self.__profilerunning

    def _set_profile_running(self, running):
        """
        Set profile editor instance.
        """
        self.__profilerunning = running

    # Properties
    profile_running = property(_get_profile_running, _set_profile_running)
Esempio n. 2
0
class NewInventory(HIGWindow):
    """
    A window for creating new or editing existing Inventories.
    """

    def __init__(self, inventory=None, edit_mode=False):
        """
        If you want to load an inventory at startup, pass it to inventory.
        If you want to run this in edit mode, set to True edit_mode.
        """
        HIGWindow.__init__(self)

        self.schemawin = None
        self.discoverywin = None
        self.edit_mode = edit_mode
        if edit_mode:
            self.wtitle = _("Editing Inventory")
        else:
            self.wtitle = _("New Inventory")

        # header
        self.title_markup = "<span size='16500' weight='heavy'>%s</span>"
        self.ttitle = HIGEntryLabel("")
        self.ttitle.set_line_wrap(False)
        self.ttitle.set_markup(self.title_markup % self.wtitle)
        self.umit_logo = gtk.Image()
        self.umit_logo.set_from_file(logo)
        # inventory
        self.invname_lbl = HIGEntryLabel(_("Inventory's name"))
        self.invname = gtk.Entry()
        self.invname.connect('changed', self._check_invname)
        self.invname_inuse = HIGEntryLabel(_("in use"))
        self.invname_inuse.set_sensitive(False)
        self.invenabled = gtk.CheckButton(_("Enabled"))
        self.invenabled.set_active(True)
        # scan command
        self.scandefault = gtk.CheckButton(_("Use default scan options"))
        img_info = gtk.Image()
        img_info.set_from_stock(gtk.STOCK_INFO, gtk.ICON_SIZE_MENU)
        self.scandefault_tip = gtk.EventBox()
        self.scandefault_tip.add(img_info)

        self.scanadv = gtk.Expander(_("Use advanced scan options"))
        self.scan_name_lbl = HIGEntryLabel(_("Scan Profile"))
        self.scan_name = ProfileCombo()
        self.scan_name.update()
        self.scan_name.set_active(0)
        self.scan_name.connect('changed', self._set_scan_command)
        self.cmd_wizard = gtk.Button(stock=gtk.STOCK_CONVERT)
        blbl = self.cmd_wizard.get_children(
            )[0].get_children()[0].get_children()[1]
        blbl.set_text(_("Command Wizard"))
        self.cmd_wizard.connect('clicked', self._open_cmd_wizard)

        self.scan_command_lbl = HIGEntryLabel(_("Command"))
        self.scan_command = gtk.Entry()
        self.scan_command.connect('changed', self._set_scan_type)
        self._set_scan_command(None)
        self.scandefault.set_active(True)
        # scan target
        self.scantarget_lbl = HIGEntryLabel(_("Scan target"))
        self.scantarget = gtk.Entry()
        self.scantarget_discovery = HIGButton(_("Use host discovery"))
        self.scantarget_discovery.connect('clicked', self._open_host_discovery)
        # scheduling profiles
        self.sched_name_lbl = HIGEntryLabel(_("Scheduling Profile"))
        self.sched_name = gtk.combo_box_new_text()
        self.sched_name_edit = gtk.Button(stock=gtk.STOCK_EDIT)
        blbl = self.sched_name_edit.get_children(
            )[0].get_children()[0].get_children()[1]
        blbl.set_text(_("Edit Profiles"))
        self.sched_name_edit.connect('clicked', self._edit_schedprofiles)
        # bottom buttons
        self.help = HIGButton(stock=gtk.STOCK_HELP)
        self.help.connect('clicked', self._show_help)
        self.cancel = HIGButton(stock=gtk.STOCK_CANCEL)
        self.cancel.connect('clicked', self._exit)
        self.ok = HIGButton(stock=gtk.STOCK_OK)
        self.ok.connect('clicked', self._save_inventory_and_leave)

        self.tooltips = gtk.Tooltips()
        self.tooltips.set_tip(self.scandefault_tip,
            _("nmap -T Aggressive -sV -n -O -v target"))

        # disable controls if edit_mode=True
        if edit_mode:
            self.invname.set_sensitive(False)
            self.scandefault.set_sensitive(False)
            self.scandefault_tip.set_sensitive(False)
            self.scanadv.set_sensitive(False)
            self.scan_name.set_sensitive(False)
            self.scan_command.set_sensitive(False)
            self.scantarget_lbl.set_sensitive(False)
            self.scantarget.set_sensitive(False)
            self.scantarget_discovery.set_sensitive(False)

        self.connect('destroy', self._exit)
        self.profile_running = None # no SchedProfileEditor instance is running.
        self.load_schemas()
        self._load_pscheds()

        # load an inventory if especified
        self.loaded_command = None
        if inventory:
            self.load_inventory(inventory)

        self.__set_props()
        self.__do_layout()


    def load_inventory(self, inventory):
        """
        Load inventory.
        """
        inv = ConfigParser()
        inv.read(Path.sched_schemas)

        if not inv.has_section(inventory):
            dlg = NoScheduleDlg()
            dlg.run()
            dlg.destroy()
            raise NoInventory(inventory)

        self.invname.set_text(inventory)
        for item in inv.items(inventory):
            if item[0] == 'profile':
                pindx = self.profiles.index(item[1])
                self.sched_name.set_active(pindx)
            if item[0] == 'enabled':
                self.invenabled.set_active(int(item[1]))
            if item[0] == 'command':
                self.loaded_command = item[1]


    def load_schemas(self):
        """
        Load scheduler schemas profiles.
        """
        schemas = ConfigParser()
        schemas.read(Path.sched_schemas)

        self.sections = [ ]
        for section in schemas.sections():
            self.sections.append(section)


    def _load_pscheds(self):
        """
        Load scheduling profiles.
        """
        pscheds = ConfigParser()
        pscheds.read(Path.sched_profiles)

        self.profiles = [ ]
        self.sched_name.get_model().clear()
        for section in pscheds.sections():
            self.sched_name.append_text(section)
            self.profiles.append(section)

        self.sched_name.set_active(0)


    def _check_invname(self, event):
        """
        Check if Inventory's name isn't in use.
        """
        if self.invname.get_text() and \
           (self.invname.get_text() in self.sections) and \
            not self.edit_mode:
            self.invname_inuse.set_sensitive(True)
        else:
            self.invname_inuse.set_sensitive(False)


    def _edit_schedprofiles(self, event):
        """
        Open Scheduling Profiles Editor.
        """
        if self.profile_running:
            return

        win = SchedProfileEditor(self, self.sched_name.get_active_text())
        win.show_all()
        self.profile_running = win


    def _set_scan_type(self, event):
        """
        When scan command is changed, unset "Default scan options" if it is
        selected.
        """
        if self.scandefault.get_active():
            self.scandefault.set_active(False)


    def _open_cmd_wizard(self, event):
        """
        Run command wizard window and update combobox when it finishes.
        """
        def update_scan_profiles(wwin):
            self.scan_name.update()

        w = Wizard()
        w.show_all()
        w.connect('destroy', update_scan_profiles)


    def _open_host_discovery(self, event):
        """
        Open host discovery window.
        """
        if self.discoverywin:
            return

        w = HostDiscovery(self)
        w.show_all()

        self.discoverywin = w


    def get_discoverywin(self):
        """
        Get HostDiscovery running instance.
        """
        return self.__discoverywin


    def set_discoverywin(self, win):
        """
        Set HostDiscovery instance.
        """
        self.__discoverywin = win


    def get_schemawin(self):
        """
        Get scheduelr schemas editor running instance.
        """
        return self.__schemawin


    def set_schemawin(self, win):
        """
        Set scheduler schemas editor instance.
        """
        self.__schemawin = win


    def get_profile_running(self):
        """
        Get profile editor running instance.
        """
        return self.__profilerunning


    def set_profile_running(self, running):
        """
        Set profile editor instance.
        """
        self.__profilerunning = running


    def _save_inventory(self, event):
        """
        Save inventory.
        """
        target = self.scantarget.get_text()
        invname = self.invname.get_text()
        notinuse = (self.invname_inuse.state == gtk.STATE_INSENSITIVE)
        command_adv = self.scan_command.get_text()
        
        # checking for errors
        if not notinuse or not len(invname) and not self.edit_mode:
            dlg = HIGAlertDialog(self,
                message_format=_("New Inventory - Error while creating."),
                secondary_text=_("You tried to use an existing Inventory "
                    "name or you didn't specify one."))

            dlg.run()
            dlg.destroy()
            return 0

        if not len(target) and not self.edit_mode:
            dlg = HIGAlertDialog(self,
                message_format=_("New Inventory - Error  while creating."),
                    secondary_text=_("You didn't specify any target."))

            dlg.run()
            dlg.destroy()
            return 0
            
       
        if not self.edit_mode:
        	if Address_Checker(target) == "IPV4" :
        		option = ""
        	elif Address_Checker(target) == "IPV6":
        		option = "-6 "
        	elif Address_Checker(target) == "MAC":
        		option = ""
        	else:
        		option = ""
        		dlg = HIGAlertDialog(self,
        			message_format=_("New Inentory - Error While Creating."),
        			secondary_text=_("You need to enter correct address either"
        				"IPV4 or IPv6 or MAC address"))
        		dlg.run()
        		dlg.destroy()
        		return 0


        if not len(command_adv) and not self.scandefault.get_active() \
            and not self.edit_mode:
            dlg = HIGAlertDialog(self,
                message_format=_("New Inventory - Error while creating."),
                secondary_text=_("You need to toggle \"Use default scan "
                    "options\" or specify a command."))

            dlg.run()
            dlg.destroy()
            return 0
        # end error checking

        if self.scandefault.get_active() and not self.edit_mode:
            command = "nmap -T Aggressive -sV -n -O -v " + option +target
        elif not self.edit_mode:
            target_cmd = "<target>"
            target_pos = command_adv.find(target_cmd)
            if target_pos != -1:
                start = target_pos
                end = target_pos + len(target_cmd)
                command = command_adv[:start] + option +target + command_adv[end:]
            else:
                dlg = HIGAlertDialog(self,
                    message_format=_("New Inventory - Error while creating."),
                    secondary_text=_("It seems you removed <target> from the "
                        "Scan command entry, you need to leave it somewhere "
                        "there."))

                dlg.run()
                dlg.destroy()
                return 0

        schedule = self.sched_name.get_active_text()
        enabled = int(self.invenabled.get_active())
        # write inventory to schema's file
        s_cfg = ConfigParser()
        s_cfg.read(Path.sched_schemas)

        if not s_cfg.has_section(invname):
            new_sec = True
            s_cfg.add_section(invname)
        elif self.edit_mode:
            new_sec = False
        else:
            print "How the hell did we get here?!"
            print "Report as BUG"
            return 0

        if new_sec:
            # New Section
            s_cfg.set(invname, 'profile', schedule)
            s_cfg.set(invname, 'command', command)
            s_cfg.set(invname, 'enabled', enabled)
            s_cfg.set(invname, 'addtoinv', '2')
            s_cfg.set(invname, 'saveto', '')
            s_cfg.set(invname, 'mailto', '')
            s_cfg.set(invname, 'smtp', '')
        else:
            # Edit Mode
            s_cfg.set(invname, 'profile', schedule)
            s_cfg.set(invname, 'enabled', enabled)
            #here i have to put check for scan target field
            command_text = self.cmd_entry.get_text()
            if command_text.find("nmap") == -1:
            	dlg = HIGAlertDialog(self,
            		message_format=_("Edit Inventory - Error while creating."),
            		secondary_text=_("It seems you have not entered namp in "
            			"command field. enter correct command with target."))
            	dlg.run()
            	dlg.destroy()
            	return 0
            	
            s_cfg.set(invname, 'command', command_text)

        s_cfg.write(open(Path.sched_schemas, 'w'))

        self.load_schemas()

        return 1


    def _save_inventory_and_leave(self, event):
        """
        Save Inventory and close window.
        """
        close_win = self._save_inventory(None)
        if close_win:
            self._exit(None)


    def _set_scan_command(self, event):
        """
        Set scan command based on chosen profile.
        """
        profile = self.scan_name.get_selected_profile()
        cmd_profile = CommandProfile()
        command = cmd_profile.get_command(profile)
        self.scan_command.set_text(command % '<target>')


    def _show_help(self, event):
        """
        Show help for creating a New Inventory.
        """
        pass


    def _exit(self, event):
        """
        Close window.
        """
        if self.schemawin:
            self.schemawin._exit(None)

        if self.profile_running:
            self.profile_running._exit(None)

        self.destroy()


    def __set_props(self):
        """
        Set window properties.
        """
        self.set_title(self.wtitle)


    def __do_layout(self):
        """
        Layout widgets.
        """
        main_vbox = HIGVBox()
        main_vbox.set_border_width(5)
        main_vbox.set_spacing(12)
        header_hbox = HIGHBox()
        invname_hbox = HIGHBox()
        scan_hbox = HIGHBox()
        scanadv_hbox = HIGHBox()
        scantarget_hbox = HIGHBox()
        sched_box = HIGHBox()
        btns_hbox = HIGHBox()

        # header
        header_hbox._pack_expand_fill(self.ttitle)
        header_hbox._pack_noexpand_nofill(self.umit_logo)
        # inventory's name
        invname_hbox._pack_noexpand_nofill(self.invname_lbl)
        invname_hbox._pack_expand_fill(self.invname)
        invname_hbox._pack_noexpand_nofill(self.invname_inuse)
        invname_hbox._pack_noexpand_nofill(self.invenabled)
        # scan command
        scan_hbox._pack_noexpand_nofill(self.scandefault)
        scan_hbox._pack_noexpand_nofill(self.scandefault_tip)
        scanadv_hbox._pack_expand_fill(self.scanadv)

        adv_box = HIGVBox()
        scanadv_align = gtk.Alignment(0.5, 0.5, 1, 1)
        scanadv_align.set_padding(6, 0, 12, 0)
        scanname_box = HIGHBox()
        scanname_box._pack_noexpand_nofill(self.scan_name_lbl)
        scanname_box._pack_expand_fill(self.scan_name)
        scanname_box._pack_noexpand_nofill(self.cmd_wizard)
        adv_box.add(scanname_box)
        scancmd_box = HIGHBox()
        scancmd_box._pack_noexpand_nofill(self.scan_command_lbl)
        scancmd_box._pack_expand_fill(self.scan_command)
        adv_box.add(scancmd_box)

        scanadv_align.add(adv_box)
        self.scanadv.add(scanadv_align)
        # scan target
        scantarget_hbox._pack_noexpand_nofill(self.scantarget_lbl)
        scantarget_hbox._pack_expand_fill(self.scantarget)
        scantarget_hbox._pack_noexpand_nofill(self.scantarget_discovery)
        # scheduling profiles
        sched_box._pack_noexpand_nofill(self.sched_name_lbl)
        sched_box._pack_expand_fill(self.sched_name)
        sched_box._pack_noexpand_nofill(self.sched_name_edit)
        # bottom buttons
        btns_hbox.set_homogeneous(True)
        btns_hbox._pack_expand_fill(self.help)
        btns_hbox._pack_expand_fill(hig_box_space_holder())
        btns_hbox._pack_expand_fill(self.cancel)
        btns_hbox._pack_expand_fill(self.ok)


        main_vbox._pack_noexpand_nofill(header_hbox)
        main_vbox._pack_noexpand_nofill(gtk.HSeparator())
        main_vbox._pack_noexpand_nofill(invname_hbox)
        main_vbox._pack_noexpand_nofill(scan_hbox)
        main_vbox._pack_noexpand_nofill(scanadv_hbox)
        main_vbox._pack_noexpand_nofill(scantarget_hbox)
        
        if self.loaded_command and self.edit_mode:
            view_cmd_box = HIGHBox()
            view_cmd_box._pack_noexpand_nofill(gtk.Label(_("Command")))
            # XXX Why don't reuse scan_command?
            self.cmd_entry = gtk.Entry()
            self.cmd_entry.set_text(self.loaded_command)
            view_cmd_box._pack_expand_fill(self.cmd_entry)
            img_info = gtk.Image()
            img_info.set_from_stock(gtk.STOCK_INFO, gtk.ICON_SIZE_MENU)
            eb = gtk.EventBox()
            eb.add(img_info)
            self.tooltips.set_tip(eb, _("Changes in command won't be saved!"))
            view_cmd_box.pack_end(eb, False, False, 0)
            main_vbox._pack_noexpand_nofill(view_cmd_box)

        main_vbox._pack_noexpand_nofill(sched_box)
        main_vbox.pack_end(btns_hbox, False, False, 0)
        main_vbox.pack_end(gtk.HSeparator(), False, False, 0)

        self.add(main_vbox)


    # Properties
    schemawin = property(get_schemawin, set_schemawin)
    discoverywin = property(get_discoverywin, set_discoverywin)
    profile_running = property(get_profile_running, set_profile_running)
Esempio n. 3
0
class NewInventory(HIGWindow):
    """
    A window for creating new or editing existing Inventories.
    """
    def __init__(self, inventory=None, edit_mode=False):
        """
        If you want to load an inventory at startup, pass it to inventory.
        If you want to run this in edit mode, set to True edit_mode.
        """
        HIGWindow.__init__(self)

        self.schemawin = None
        self.discoverywin = None
        self.edit_mode = edit_mode
        if edit_mode:
            self.wtitle = _("Editing Inventory")
        else:
            self.wtitle = _("New Inventory")

        # header
        self.title_markup = "<span size='16500' weight='heavy'>%s</span>"
        self.ttitle = HIGEntryLabel("")
        self.ttitle.set_line_wrap(False)
        self.ttitle.set_markup(self.title_markup % self.wtitle)
        self.umit_logo = gtk.Image()
        self.umit_logo.set_from_file(logo)
        # inventory
        self.invname_lbl = HIGEntryLabel(_("Inventory's name"))
        self.invname = gtk.Entry()
        self.invname.connect('changed', self._check_invname)
        self.invname_inuse = HIGEntryLabel(_("in use"))
        self.invname_inuse.set_sensitive(False)
        self.invenabled = gtk.CheckButton(_("Enabled"))
        self.invenabled.set_active(True)
        # scan command
        self.scandefault = gtk.CheckButton(_("Use default scan options"))
        img_info = gtk.Image()
        img_info.set_from_stock(gtk.STOCK_INFO, gtk.ICON_SIZE_MENU)
        self.scandefault_tip = gtk.EventBox()
        self.scandefault_tip.add(img_info)

        self.scanadv = gtk.Expander(_("Use advanced scan options"))
        self.scan_name_lbl = HIGEntryLabel(_("Scan Profile"))
        self.scan_name = ProfileCombo()
        self.scan_name.update()
        self.scan_name.set_active(0)
        self.scan_name.connect('changed', self._set_scan_command)
        self.cmd_wizard = gtk.Button(stock=gtk.STOCK_CONVERT)
        blbl = self.cmd_wizard.get_children()[0].get_children(
        )[0].get_children()[1]
        blbl.set_text(_("Command Wizard"))
        self.cmd_wizard.connect('clicked', self._open_cmd_wizard)

        self.scan_command_lbl = HIGEntryLabel(_("Command"))
        self.scan_command = gtk.Entry()
        self.scan_command.connect('changed', self._set_scan_type)
        self._set_scan_command(None)
        self.scandefault.set_active(True)
        # scan target
        self.scantarget_lbl = HIGEntryLabel(_("Scan target"))
        self.scantarget = gtk.Entry()
        self.scantarget_discovery = HIGButton(_("Use host discovery"))
        self.scantarget_discovery.connect('clicked', self._open_host_discovery)
        # scheduling profiles
        self.sched_name_lbl = HIGEntryLabel(_("Scheduling Profile"))
        self.sched_name = gtk.combo_box_new_text()
        self.sched_name_edit = gtk.Button(stock=gtk.STOCK_EDIT)
        blbl = self.sched_name_edit.get_children()[0].get_children(
        )[0].get_children()[1]
        blbl.set_text(_("Edit Profiles"))
        self.sched_name_edit.connect('clicked', self._edit_schedprofiles)
        # bottom buttons
        self.help = HIGButton(stock=gtk.STOCK_HELP)
        self.help.connect('clicked', self._show_help)
        self.cancel = HIGButton(stock=gtk.STOCK_CANCEL)
        self.cancel.connect('clicked', self._exit)
        self.ok = HIGButton(stock=gtk.STOCK_OK)
        self.ok.connect('clicked', self._save_inventory_and_leave)

        self.tooltips = gtk.Tooltips()
        self.tooltips.set_tip(self.scandefault_tip,
                              _("nmap -T Aggressive -sV -n -O -v target"))

        # disable controls if edit_mode=True
        if edit_mode:
            self.invname.set_sensitive(False)
            self.scandefault.set_sensitive(False)
            self.scandefault_tip.set_sensitive(False)
            self.scanadv.set_sensitive(False)
            self.scan_name.set_sensitive(False)
            self.scan_command.set_sensitive(False)
            self.scantarget_lbl.set_sensitive(False)
            self.scantarget.set_sensitive(False)
            self.scantarget_discovery.set_sensitive(False)

        self.connect('destroy', self._exit)
        self.profile_running = None  # no SchedProfileEditor instance is running.
        self.load_schemas()
        self._load_pscheds()

        # load an inventory if especified
        self.loaded_command = None
        if inventory:
            self.load_inventory(inventory)

        self.__set_props()
        self.__do_layout()

    def load_inventory(self, inventory):
        """
        Load inventory.
        """
        inv = ConfigParser()
        inv.read(Path.sched_schemas)

        if not inv.has_section(inventory):
            dlg = NoScheduleDlg()
            dlg.run()
            dlg.destroy()
            raise NoInventory(inventory)

        self.invname.set_text(inventory)
        for item in inv.items(inventory):
            if item[0] == 'profile':
                pindx = self.profiles.index(item[1])
                self.sched_name.set_active(pindx)
            if item[0] == 'enabled':
                self.invenabled.set_active(int(item[1]))
            if item[0] == 'command':
                self.loaded_command = item[1]

    def load_schemas(self):
        """
        Load scheduler schemas profiles.
        """
        schemas = ConfigParser()
        schemas.read(Path.sched_schemas)

        self.sections = []
        for section in schemas.sections():
            self.sections.append(section)

    def _load_pscheds(self):
        """
        Load scheduling profiles.
        """
        pscheds = ConfigParser()
        pscheds.read(Path.sched_profiles)

        self.profiles = []
        self.sched_name.get_model().clear()
        for section in pscheds.sections():
            self.sched_name.append_text(section)
            self.profiles.append(section)

        self.sched_name.set_active(0)

    def _check_invname(self, event):
        """
        Check if Inventory's name isn't in use.
        """
        if self.invname.get_text() and \
           (self.invname.get_text() in self.sections) and \
            not self.edit_mode:
            self.invname_inuse.set_sensitive(True)
        else:
            self.invname_inuse.set_sensitive(False)

    def _edit_schedprofiles(self, event):
        """
        Open Scheduling Profiles Editor.
        """
        if self.profile_running:
            return

        win = SchedProfileEditor(self, self.sched_name.get_active_text())
        win.show_all()
        self.profile_running = win

    def _set_scan_type(self, event):
        """
        When scan command is changed, unset "Default scan options" if it is
        selected.
        """
        if self.scandefault.get_active():
            self.scandefault.set_active(False)

    def _open_cmd_wizard(self, event):
        """
        Run command wizard window and update combobox when it finishes.
        """
        def update_scan_profiles(wwin):
            self.scan_name.update()

        w = Wizard()
        w.show_all()
        w.connect('destroy', update_scan_profiles)

    def _open_host_discovery(self, event):
        """
        Open host discovery window.
        """
        if self.discoverywin:
            return

        w = HostDiscovery(self)
        w.show_all()

        self.discoverywin = w

    def get_discoverywin(self):
        """
        Get HostDiscovery running instance.
        """
        return self.__discoverywin

    def set_discoverywin(self, win):
        """
        Set HostDiscovery instance.
        """
        self.__discoverywin = win

    def get_schemawin(self):
        """
        Get scheduelr schemas editor running instance.
        """
        return self.__schemawin

    def set_schemawin(self, win):
        """
        Set scheduler schemas editor instance.
        """
        self.__schemawin = win

    def get_profile_running(self):
        """
        Get profile editor running instance.
        """
        return self.__profilerunning

    def set_profile_running(self, running):
        """
        Set profile editor instance.
        """
        self.__profilerunning = running

    def _save_inventory(self, event):
        """
        Save inventory.
        """
        target = self.scantarget.get_text()
        invname = self.invname.get_text()
        notinuse = (self.invname_inuse.state == gtk.STATE_INSENSITIVE)
        command_adv = self.scan_command.get_text()

        # checking for errors
        if not notinuse or not len(invname) and not self.edit_mode:
            dlg = HIGAlertDialog(
                self,
                message_format=_("New Inventory - Error while creating."),
                secondary_text=_("You tried to use an existing Inventory "
                                 "name or you didn't specify one."))

            dlg.run()
            dlg.destroy()
            return 0

        if not len(target) and not self.edit_mode:
            dlg = HIGAlertDialog(
                self,
                message_format=_("New Inventory - Error  while creating."),
                secondary_text=_("You didn't specify any target."))

            dlg.run()
            dlg.destroy()
            return 0

        if not self.edit_mode:
            if Address_Checker(target) == "IPV4":
                option = ""
            elif Address_Checker(target) == "IPV6":
                option = "-6 "
            elif Address_Checker(target) == "MAC":
                option = ""
            else:
                option = ""
                dlg = HIGAlertDialog(
                    self,
                    message_format=_("New Inentory - Error While Creating."),
                    secondary_text=_("You need to enter correct address either"
                                     "IPV4 or IPv6 or MAC address"))
                dlg.run()
                dlg.destroy()
                return 0


        if not len(command_adv) and not self.scandefault.get_active() \
            and not self.edit_mode:
            dlg = HIGAlertDialog(
                self,
                message_format=_("New Inventory - Error while creating."),
                secondary_text=_("You need to toggle \"Use default scan "
                                 "options\" or specify a command."))

            dlg.run()
            dlg.destroy()
            return 0
        # end error checking

        if self.scandefault.get_active() and not self.edit_mode:
            command = "nmap -T Aggressive -sV -n -O -v " + option + target
        elif not self.edit_mode:
            target_cmd = "<target>"
            target_pos = command_adv.find(target_cmd)
            if target_pos != -1:
                start = target_pos
                end = target_pos + len(target_cmd)
                command = command_adv[:start] + option + target + command_adv[
                    end:]
            else:
                dlg = HIGAlertDialog(
                    self,
                    message_format=_("New Inventory - Error while creating."),
                    secondary_text=_(
                        "It seems you removed <target> from the "
                        "Scan command entry, you need to leave it somewhere "
                        "there."))

                dlg.run()
                dlg.destroy()
                return 0

        schedule = self.sched_name.get_active_text()
        enabled = int(self.invenabled.get_active())
        # write inventory to schema's file
        s_cfg = ConfigParser()
        s_cfg.read(Path.sched_schemas)

        if not s_cfg.has_section(invname):
            new_sec = True
            s_cfg.add_section(invname)
        elif self.edit_mode:
            new_sec = False
        else:
            print "How the hell did we get here?!"
            print "Report as BUG"
            return 0

        if new_sec:
            # New Section
            s_cfg.set(invname, 'profile', schedule)
            s_cfg.set(invname, 'command', command)
            s_cfg.set(invname, 'enabled', enabled)
            s_cfg.set(invname, 'addtoinv', '2')
            s_cfg.set(invname, 'saveto', '')
            s_cfg.set(invname, 'mailto', '')
            s_cfg.set(invname, 'smtp', '')
        else:
            # Edit Mode
            s_cfg.set(invname, 'profile', schedule)
            s_cfg.set(invname, 'enabled', enabled)
            #here i have to put check for scan target field
            command_text = self.cmd_entry.get_text()
            if command_text.find("nmap") == -1:
                dlg = HIGAlertDialog(
                    self,
                    message_format=_("Edit Inventory - Error while creating."),
                    secondary_text=_(
                        "It seems you have not entered namp in "
                        "command field. enter correct command with target."))
                dlg.run()
                dlg.destroy()
                return 0

            s_cfg.set(invname, 'command', command_text)

        s_cfg.write(open(Path.sched_schemas, 'w'))

        self.load_schemas()

        return 1

    def _save_inventory_and_leave(self, event):
        """
        Save Inventory and close window.
        """
        close_win = self._save_inventory(None)
        if close_win:
            self._exit(None)

    def _set_scan_command(self, event):
        """
        Set scan command based on chosen profile.
        """
        profile = self.scan_name.get_selected_profile()
        cmd_profile = CommandProfile()
        command = cmd_profile.get_command(profile)
        self.scan_command.set_text(command % '<target>')

    def _show_help(self, event):
        """
        Show help for creating a New Inventory.
        """
        pass

    def _exit(self, event):
        """
        Close window.
        """
        if self.schemawin:
            self.schemawin._exit(None)

        if self.profile_running:
            self.profile_running._exit(None)

        self.destroy()

    def __set_props(self):
        """
        Set window properties.
        """
        self.set_title(self.wtitle)

    def __do_layout(self):
        """
        Layout widgets.
        """
        main_vbox = HIGVBox()
        main_vbox.set_border_width(5)
        main_vbox.set_spacing(12)
        header_hbox = HIGHBox()
        invname_hbox = HIGHBox()
        scan_hbox = HIGHBox()
        scanadv_hbox = HIGHBox()
        scantarget_hbox = HIGHBox()
        sched_box = HIGHBox()
        btns_hbox = HIGHBox()

        # header
        header_hbox._pack_expand_fill(self.ttitle)
        header_hbox._pack_noexpand_nofill(self.umit_logo)
        # inventory's name
        invname_hbox._pack_noexpand_nofill(self.invname_lbl)
        invname_hbox._pack_expand_fill(self.invname)
        invname_hbox._pack_noexpand_nofill(self.invname_inuse)
        invname_hbox._pack_noexpand_nofill(self.invenabled)
        # scan command
        scan_hbox._pack_noexpand_nofill(self.scandefault)
        scan_hbox._pack_noexpand_nofill(self.scandefault_tip)
        scanadv_hbox._pack_expand_fill(self.scanadv)

        adv_box = HIGVBox()
        scanadv_align = gtk.Alignment(0.5, 0.5, 1, 1)
        scanadv_align.set_padding(6, 0, 12, 0)
        scanname_box = HIGHBox()
        scanname_box._pack_noexpand_nofill(self.scan_name_lbl)
        scanname_box._pack_expand_fill(self.scan_name)
        scanname_box._pack_noexpand_nofill(self.cmd_wizard)
        adv_box.add(scanname_box)
        scancmd_box = HIGHBox()
        scancmd_box._pack_noexpand_nofill(self.scan_command_lbl)
        scancmd_box._pack_expand_fill(self.scan_command)
        adv_box.add(scancmd_box)

        scanadv_align.add(adv_box)
        self.scanadv.add(scanadv_align)
        # scan target
        scantarget_hbox._pack_noexpand_nofill(self.scantarget_lbl)
        scantarget_hbox._pack_expand_fill(self.scantarget)
        scantarget_hbox._pack_noexpand_nofill(self.scantarget_discovery)
        # scheduling profiles
        sched_box._pack_noexpand_nofill(self.sched_name_lbl)
        sched_box._pack_expand_fill(self.sched_name)
        sched_box._pack_noexpand_nofill(self.sched_name_edit)
        # bottom buttons
        btns_hbox.set_homogeneous(True)
        btns_hbox._pack_expand_fill(self.help)
        btns_hbox._pack_expand_fill(hig_box_space_holder())
        btns_hbox._pack_expand_fill(self.cancel)
        btns_hbox._pack_expand_fill(self.ok)

        main_vbox._pack_noexpand_nofill(header_hbox)
        main_vbox._pack_noexpand_nofill(gtk.HSeparator())
        main_vbox._pack_noexpand_nofill(invname_hbox)
        main_vbox._pack_noexpand_nofill(scan_hbox)
        main_vbox._pack_noexpand_nofill(scanadv_hbox)
        main_vbox._pack_noexpand_nofill(scantarget_hbox)

        if self.loaded_command and self.edit_mode:
            view_cmd_box = HIGHBox()
            view_cmd_box._pack_noexpand_nofill(gtk.Label(_("Command")))
            # XXX Why don't reuse scan_command?
            self.cmd_entry = gtk.Entry()
            self.cmd_entry.set_text(self.loaded_command)
            view_cmd_box._pack_expand_fill(self.cmd_entry)
            img_info = gtk.Image()
            img_info.set_from_stock(gtk.STOCK_INFO, gtk.ICON_SIZE_MENU)
            eb = gtk.EventBox()
            eb.add(img_info)
            self.tooltips.set_tip(eb, _("Changes in command won't be saved!"))
            view_cmd_box.pack_end(eb, False, False, 0)
            main_vbox._pack_noexpand_nofill(view_cmd_box)

        main_vbox._pack_noexpand_nofill(sched_box)
        main_vbox.pack_end(btns_hbox, False, False, 0)
        main_vbox.pack_end(gtk.HSeparator(), False, False, 0)

        self.add(main_vbox)

    # Properties
    schemawin = property(get_schemawin, set_schemawin)
    discoverywin = property(get_discoverywin, set_discoverywin)
    profile_running = property(get_profile_running, set_profile_running)
class SchedSchemaEditor(HIGWindow):
    """
    Scheduler Schemas Editor
    """
    def __init__(self, daddy=None):
        HIGWindow.__init__(self)

        self.daddy = daddy
        self.wtitle = _("Scan Scheduler Editor")

        # header
        self.title_markup = "<span size='16500' weight='heavy'>%s</span>"
        self.ttitle = HIGEntryLabel("")
        self.ttitle.set_line_wrap(False)
        self.ttitle.set_markup(self.title_markup % self.wtitle)
        self.umit_logo = gtk.Image()
        self.umit_logo.set_from_file(logo)
        # schemas name
        self.schema_name_lbl = HIGEntryLabel(_("Schema Name"))
        self.schema_name = gtk.combo_box_entry_new_text()
        self.schema_name.connect('changed', self._check_schema)
        # target and scan profiles
        #self.target_lbl = HIGEntryLabel(_("Target"))
        #self.target = gtk.Entry()
        self.scan_name_lbl = HIGEntryLabel(_("Scan Profile"))
        self.scan_name = ProfileCombo()
        self.scan_name.update()
        self.scan_name.set_active(0)
        self.scan_name.connect('changed', self._set_scan_command)
        # scan command
        self.scan_command_lbl = HIGEntryLabel(_("Command"))
        self.scan_command = gtk.Entry()
        # scheduling profile
        self.sched_name_lbl = HIGEntryLabel(_("Scheduling Profile"))
        self.sched_name = gtk.combo_box_new_text()
        self.sched_name_edit = gtk.Button(stock=gtk.STOCK_EDIT)
        blbl = self.sched_name_edit.get_children()[0].get_children(
        )[0].get_children()[1]
        blbl.set_text(_("Edit Profiles"))
        self.sched_name_edit.connect('clicked', self._edit_schedprofiles)
        # schema settings
        self.schema_sett_frame = HIGFrame()
        self.setting_saveto = gtk.CheckButton(_("Save outputs to"))
        self.setting_saveto_entry = gtk.Entry()
        self.setting_saveto_browse = gtk.Button(_("..."))
        self.setting_saveto_browse.connect('clicked', self._select_saveto)
        self.setting_mailto = gtk.CheckButton(_("Send output to email"))
        self.setting_mailto_entry = gtk.Entry()
        self.setting_smtp_lbl = HIGEntryLabel(_("SMTP Schema"))
        self.setting_smtp = gtk.combo_box_new_text()
        self.setting_addtoinv = gtk.CheckButton(_("Add to the Inventory"))
        self.setting_enabled = gtk.CheckButton(_("Enabled"))
        # bottom buttons
        self.help = HIGButton(stock=gtk.STOCK_HELP)
        self.help.connect('clicked', self._show_help)
        self.apply = HIGButton(stock=gtk.STOCK_APPLY)
        self.apply.connect('clicked', self._save_schema)
        self.cancel = HIGButton(stock=gtk.STOCK_CANCEL)
        self.cancel.connect('clicked', self._exit)
        self.ok = HIGButton(stock=gtk.STOCK_OK)
        self.ok.connect('clicked', self._save_schema_and_leave)

        self.load_smtp_schemas()
        self._set_scan_command(None)
        self.profile_running = None  # no SchedProfileEditor instance is running.
        self._load_pscheds()
        self.load_schemas()

        self.__set_props()
        self.__do_layout()

        self.connect('destroy', self._exit)

    def load_smtp_schemas(self):
        """
        Load smtp profiles.
        """
        schemas = ConfigParser()
        schemas.read(Path.smtp_schemas)

        self.smtp_sections = []
        self.setting_smtp.get_model().clear()
        for section in schemas.sections():
            self.smtp_sections.append(section)
            self.setting_smtp.append_text(section)

        self.setting_smtp.set_active(0)

    def load_schemas(self):
        """
        Load schemas profiles.
        """
        schemas = ConfigParser()
        schemas.read(Path.sched_schemas)

        self.sections = []
        self.schema_name.get_model().clear()
        for section in schemas.sections():
            self.sections.append(section)
            self.schema_name.append_text(section)

        self.schema_name.set_active(0)
        self._check_schema(None)

    def _load_schema(self):
        """
        Load current set schedule schema.
        """
        schema = ConfigParser()
        schema.read(Path.sched_schemas)

        values = {
            'command': self.scan_command.set_text,
            'saveto': self.setting_saveto_entry.set_text,
            'mailto': self.setting_mailto_entry.set_text
        }
        enable = {
            'saveto': self.setting_saveto.set_active,
            'mailto': self.setting_mailto.set_active
        }

        for item in schema.items(self.schema_name.get_active_text()):
            if item[0] == 'addtoinv':
                self.setting_addtoinv.set_active(int(item[1]))
                if item[1] == '2':
                    self.apply.set_sensitive(False)
                    self.ok.set_sensitive(False)
                else:
                    self.apply.set_sensitive(True)
                    self.ok.set_sensitive(True)
            elif item[0] == 'enabled':
                self.setting_enabled.set_active(int(item[1]))
            elif item[0] == 'profile':
                pindx = self.profiles.index(item[1])
                self.sched_name.set_active(pindx)
            elif item[0] == 'smtp':
                if item[1]:
                    pindx = self.smtp_sections.index(item[1])
                    self.setting_smtp.set_active(pindx)
            else:
                values[item[0]](item[1])
                if item[0] in ('saveto', 'mailto'):
                    if len(item[1]):
                        enable[item[0]](True)
                    else:
                        enable[item[0]](False)

    def _check_schema(self, event):
        """
        Check if current text in schema_name combobox is a schema name.
        """
        if self.schema_name.get_active_text() in self.sections:
            # load schema
            self._load_schema()
        else:
            # reset to default values
            self.apply.set_sensitive(True)
            self.ok.set_sensitive(True)
            self.setting_addtoinv.set_active(False)
            self.setting_enabled.set_active(False)
            self.setting_mailto.set_active(False)
            self.setting_mailto_entry.set_text('')
            self.setting_saveto.set_active(False)
            self.setting_saveto_entry.set_text('')

        self.schema_sett_frame._set_label(self.schema_name.get_active_text() \
+ " - Settings")

    def _set_scan_command(self, event):
        """
        Set scan command based on chosen profile.
        """
        profile = self.scan_name.get_selected_profile()
        cmd_profile = CommandProfile()
        command = cmd_profile.get_command(profile)
        self.scan_command.set_text(command % '<target>')

    def _load_pscheds(self):
        """
        Load scheduling profiles.
        """
        pscheds = ConfigParser()
        pscheds.read(Path.sched_profiles)

        self.profiles = []
        self.sched_name.get_model().clear()
        for section in pscheds.sections():
            self.sched_name.append_text(section)
            self.profiles.append(section)

        self.sched_name.set_active(0)

    def _edit_schedprofiles(self, event):
        """
        Open Scheduling Profiles Editor.
        """
        if self.profile_running:
            return

        win = SchedProfileEditor(self, self.sched_name.get_active_text())
        win.show_all()
        self.profile_running = win

    def _select_saveto(self, event):
        """
        Select directory to save file.
        """
        dir_chooser = DirectoryChooserDialog(_("Select a directory"))

        dir_chooser.run()
        dir_chosen = dir_chooser.get_filename()
        dir_chooser.destroy()
        self.setting_saveto_entry.set_text(dir_chosen)

    def _save_schema(self, event):
        """
        Save current schema.
        """
        schema = self.schema_name.get_active_text()
        command = self.scan_command.get_text()
        schedule = self.sched_name.get_active_text()
        mailto = self.setting_mailto.get_active()

        if not schema or not schedule or not command or '<target>' in command:
            dlg = HIGAlertDialog(self,
                                 message_format=_('Scheduling Schema - Error\
 while saving.'),
                                 secondary_text=_("There is some error in at \
least one of the following fields: \"Schema name\", \"Command\" or \"Scheduling\
 Profile\"\n\nCheck if \"Schema name\" is not empty.\nCheck if \"Command\" does\
 contain \"<target>\" on it.\nCheck if there is some \"Scheduling Profile\" \
selected."))

            dlg.run()
            dlg.destroy()
            return

        if mailto and not self.setting_smtp.get_active_text():
            dlg = HIGAlertDialog(self,
                                 message_format=_('Scheduling Schema - Error\
 while saving.'),
                                 secondary_text=_("You need to create a \
a SMTP Schema for sending email."))

            dlg.run()
            dlg.destroy()
            return

        # check for output existance
        if self.setting_saveto.get_active() and \
           not os.path.isdir(self.setting_saveto_entry.get_text()):

            dlg = HIGAlertDialog(self,
                                 message_format=_('Scheduling Schema - Error\
 while saving.'),
                                 secondary_text=_("You especified an invalid \
directory to save scans output."))

            dlg.run()
            dlg.destroy()
            return

        # write schema to file
        s_cfg = ConfigParser()
        s_cfg.read(Path.sched_schemas)

        if not s_cfg.has_section(schema):
            new_sec = True
            s_cfg.add_section(schema)
        else:
            new_sec = False

        s_cfg.set(schema, 'profile', schedule)
        s_cfg.set(schema, 'command', command)
        if self.setting_enabled.get_active():
            s_cfg.set(schema, 'enabled', '1')
        else:
            s_cfg.set(schema, 'enabled', '0')
        if self.setting_addtoinv.get_active():
            s_cfg.set(schema, 'addtoinv', '1')
        else:
            s_cfg.set(schema, 'addtoinv', '0')
        if self.setting_saveto.get_active():
            s_cfg.set(schema, 'saveto', self.setting_saveto_entry.get_text())
        else:
            s_cfg.set(schema, 'saveto', '')
        if mailto:
            s_cfg.set(schema, 'mailto', self.setting_mailto_entry.get_text())
            s_cfg.set(schema, 'smtp', self.setting_smtp.get_active_text())
        else:
            s_cfg.set(schema, 'mailto', '')
            s_cfg.set(schema, 'smtp', '')

        s_cfg.write(open(Path.sched_schemas, 'w'))

        if new_sec:
            self.load_schemas()

        if self.daddy:
            self.daddy.load_schemas()

    def _save_schema_and_leave(self, event):
        """
        Save current schema and close editor.
        """
        self._save_schema(None)
        self._exit(None)

    def _show_help(self, event):
        """
        Show help for Scan Scheduler Editor.
        """
        show_help(self, "scheduler.html#setting-up-a-schedule")

    def __set_props(self):
        """
        Set window properties.
        """
        self.set_title(self.wtitle)
        self.set_default_size(440, -1)

    def __do_layout(self):
        """
        Layout widgets in window.
        """
        main_vbox = HIGVBox()
        main_vbox.set_border_width(5)
        main_vbox.set_spacing(12)
        header_hbox = HIGHBox()
        schema_table = HIGTable()
        schedsn_hbox = HIGHBox()
        sett_table = HIGTable()
        btns_hbox = HIGHBox()

        header_hbox._pack_expand_fill(self.ttitle)
        header_hbox._pack_noexpand_nofill(self.umit_logo)

        # schema name
        schema_table.attach_label(self.schema_name_lbl, 0, 1, 0, 1)
        schema_table.attach_entry(self.schema_name, 1, 2, 0, 1)

        # target and scan profile
        schema_table.attach_label(self.scan_name_lbl, 0, 1, 1, 2)
        schema_table.attach_entry(self.scan_name, 1, 2, 1, 2)

        # scan command
        schema_table.attach_label(self.scan_command_lbl, 0, 1, 2, 3)
        schema_table.attach_label(self.scan_command, 1, 2, 2, 3)

        # scheduling profile
        schedsn_hbox._pack_expand_fill(self.sched_name)
        schedsn_hbox._pack_noexpand_nofill(self.sched_name_edit)

        schema_table.attach_label(self.sched_name_lbl, 0, 1, 3, 4)
        schema_table.attach_entry(schedsn_hbox, 1, 2, 3, 4)

        # settings frame
        settings_align = gtk.Alignment(0.5, 0.5, 1, 1)
        settings_align.set_padding(6, 0, 12, 0)
        schemasett_hbox = HIGVBox()

        # saveto
        sett_hbox = HIGHBox()
        sett_hbox._pack_expand_fill(self.setting_saveto_entry)
        sett_hbox._pack_noexpand_nofill(self.setting_saveto_browse)
        sett_table.attach_label(self.setting_saveto, 0, 1, 0, 1)
        sett_table.attach_entry(sett_hbox, 1, 2, 0, 1)

        # mailto, smtp
        sett_hbox = HIGHBox()
        sett_hbox._pack_expand_fill(self.setting_mailto_entry)
        sett_hbox._pack_noexpand_nofill(self.setting_smtp_lbl)
        sett_hbox._pack_expand_fill(self.setting_smtp)
        sett_table.attach_label(self.setting_mailto, 0, 1, 1, 2)
        sett_table.attach_entry(sett_hbox, 1, 2, 1, 2)
        schemasett_hbox._pack_noexpand_nofill(sett_table)

        # add to inventory
        sett_hbox = HIGHBox()
        sett_hbox._pack_noexpand_nofill(self.setting_addtoinv)
        schemasett_hbox._pack_noexpand_nofill(sett_hbox)

        # enabled/disabled
        sett_hbox = HIGHBox()
        sett_hbox._pack_noexpand_nofill(self.setting_enabled)
        schemasett_hbox._pack_noexpand_nofill(sett_hbox)
        settings_align.add(schemasett_hbox)

        self.schema_sett_frame.add(settings_align)

        # bottom buttons
        btns_hbox.set_homogeneous(True)
        btns_hbox._pack_expand_fill(self.help)
        btns_hbox._pack_expand_fill(hig_box_space_holder())
        btns_hbox._pack_expand_fill(self.apply)
        btns_hbox._pack_expand_fill(self.cancel)
        btns_hbox._pack_expand_fill(self.ok)

        main_vbox._pack_noexpand_nofill(header_hbox)
        main_vbox._pack_noexpand_nofill(gtk.HSeparator())
        main_vbox._pack_noexpand_nofill(schema_table)
        main_vbox._pack_noexpand_nofill(gtk.HSeparator())
        main_vbox._pack_noexpand_nofill(self.schema_sett_frame)
        main_vbox.pack_end(btns_hbox, False, False, 0)

        self.add(main_vbox)

    def _exit(self, event):
        """
        Close current and window and profile editor if it is running.
        """
        if self.profile_running:
            self.profile_running._exit(None)

        if self.daddy:
            self.daddy.schemawin = None

        self.destroy()

    def _get_profile_running(self):
        """
        Get profile editor running instance.
        """
        return self.__profilerunning

    def _set_profile_running(self, running):
        """
        Set profile editor instance.
        """
        self.__profilerunning = running

    # Properties
    profile_running = property(_get_profile_running, _set_profile_running)