Beispiel #1
0
class SMTPSetup(HIGWindow):
    """
    SMTP editor.
    """
    
    def __init__(self):
        HIGWindow.__init__(self)
        
        self.wtitle = _("SMTP Account 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)
        # smtp server
        self.smtp_server_lbl = HIGEntryLabel(_("Server"))
        self.smtp_server = gtk.Entry()
        self.smtp_port_lbl = HIGEntryLabel(_("Port"))
        self.smtp_port = gtk.Entry()
        # sending mail..
        self.smtp_mailfrom_lbl = HIGEntryLabel(_("Mail from"))
        self.smtp_mailfrom = gtk.Entry()
        # smtp auth
        self.smtp_need_auth = gtk.CheckButton(_("Servers requires authentication"))
        self.smtp_need_auth.connect('toggled', self._auth_need)
        self.smtp_login_lbl = HIGEntryLabel(_("Username"))
        self.smtp_login = gtk.Entry()
        self.smtp_passwd_lbl = HIGEntryLabel(_("Password"))
        self.smtp_passwd = gtk.Entry()
        self.smtp_passwd.set_visibility(False)
        self._auth_need(None)
        # smtp encryption
        self.smtp_encrypt_tls = gtk.CheckButton(_("Use TLS Encryption"))

        """
        Missing: SSL encryption,
                 Other authentication methods.
        """ 

        # 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_schemas()
        
        self.__set_props()
        self.__do_layout()

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

    def load_schemas(self):
        """
        Load schemas profiles.
        """
        schemas = ConfigParser()
        schemas.read(Path.smtp_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.smtp_schemas)
        
        enable = {'tls':self.smtp_encrypt_tls.set_active,
                  'auth':self.smtp_need_auth.set_active}
        values = {'user':self.smtp_login.set_text,
                  'pass':self.smtp_passwd.set_text,
                  'server':self.smtp_server.set_text,
                  'port':self.smtp_port.set_text,
                  'mailfrom':self.smtp_mailfrom.set_text}
        
        for item in schema.items(self.schema_name.get_active_text()):
            if item[0] in ('tls', 'auth'):
                enable[item[0]](int(item[1]))
            else:
                values[item[0]](item[1])
                

    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.smtp_mailfrom.set_text('')
            self.smtp_server.set_text('')
            self.smtp_port.set_text('')
            self.smtp_encrypt_tls.set_active(False)
            self.smtp_login.set_text('')
            self.smtp_passwd.set_text('')
            self.smtp_need_auth.set_active(False)
            self._auth_need(None)
            
    
    def _auth_need(self, event):
        """
        SMTP Authentication toggled.
        """
        status = self.smtp_need_auth.get_active()
        self.smtp_login.set_sensitive(status)
        self.smtp_passwd.set_sensitive(status)
        self.smtp_login_lbl.set_sensitive(status)
        self.smtp_passwd_lbl.set_sensitive(status)
    
    
    def _save_schema(self, event):
        """
        Save current schema.
        """
        schema = self.schema_name.get_active_text()
        server = self.smtp_server.get_text()
        port = self.smtp_port.get_text()
        auth = self.smtp_need_auth.get_active()
        mailfrom = self.smtp_mailfrom.get_text()
        
        if auth:
            user = self.smtp_login.get_text()
            passwd = self.smtp_passwd.get_text()
        
        if auth and not (user and passwd):
            dlg = HIGAlertDialog(self, 
                                 message_format=_('SMTP Schema - Error\
 while saving.'),
                                 secondary_text=_("You need to specify an \
username and password for this SMTP Schema."))
            
            dlg.run()
            dlg.destroy()
            return
        
        if not schema or not server or not port or not mailfrom:
            dlg = HIGAlertDialog(self, 
                                 message_format=_('SMTP Schema - Error\
 while saving.'),
                                 secondary_text=_("The following fields \
need to be filled: Schema Name, Server, Port and Mail from."))
            
            dlg.run()
            dlg.destroy()
            return
        
        # write schema to file
        s_cfg = ConfigParser()
        s_cfg.read(Path.smtp_schemas)
        
        if not s_cfg.has_section(schema):
            new_sec = True
            s_cfg.add_section(schema)
        else:
            new_sec = False
        
        if auth:
            s_cfg.set(schema, 'auth', '1')
        else:
            s_cfg.set(schema, 'auth', '0')
            user = ''
            passwd = ''
            
        s_cfg.set(schema, 'port', port)
        s_cfg.set(schema, 'server', server)
        s_cfg.set(schema, 'user', user)
        s_cfg.set(schema, 'pass', passwd)
        s_cfg.set(schema, 'mailfrom', mailfrom)
        
        if self.smtp_encrypt_tls.get_active():
            s_cfg.set(schema, 'tls', '1')
        else:
            s_cfg.set(schema, 'tls', '0')
            
        s_cfg.write(open(Path.smtp_schemas, 'w'))
        
        if new_sec:
            self.load_schemas()
        
        
    def _save_schema_and_leave(self, event):
        """
        Save current schema and close editor.
        """
        self._save_schema(None)
        self._exit(None)
        
        
    def __set_props(self):
        """
        Set window properties.
        """
        self.set_title(self.wtitle)
       
    
    def _show_help(self, event):
        """
        Open SMTP Setup help
        """
        show_help(self, "smtpsetup.html")


    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()
        auth_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)
        
        # smtp server
        schema_table.attach_label(self.smtp_server_lbl, 0, 1, 1, 2)
        schema_table.attach_entry(self.smtp_server, 1, 2, 1, 2)

        # smtp server port
        schema_table.attach_label(self.smtp_port_lbl, 0, 1, 2, 3)
        schema_table.attach_entry(self.smtp_port, 1, 2, 2, 3)
        
        # smtp mail from
        schema_table.attach_label(self.smtp_mailfrom_lbl, 0, 1, 3, 4)
        schema_table.attach_entry(self.smtp_mailfrom, 1, 2, 3, 4)
        
        # smtp user
        auth_table.attach_label(self.smtp_login_lbl, 0, 1, 0, 1)
        auth_table.attach_entry(self.smtp_login, 1, 2, 0, 1)
        
        # smtp passwd
        auth_table.attach_label(self.smtp_passwd_lbl, 0, 1, 1, 2)
        auth_table.attach_label(self.smtp_passwd, 1, 2, 1, 2)
        
        # 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.smtp_need_auth)
        main_vbox._pack_noexpand_nofill(auth_table)
        main_vbox._pack_noexpand_nofill(gtk.HSeparator())
        main_vbox._pack_noexpand_nofill(self.smtp_encrypt_tls)
        main_vbox.pack_end(btns_hbox, False, False, 0)
        
        self.add(main_vbox)


    def _exit(self, event):
        """
        Close current window.
        """
        self.destroy()
Beispiel #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)
Beispiel #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 SMTPSetup(HIGWindow):
    """
    SMTP editor.
    """
    def __init__(self):
        HIGWindow.__init__(self)

        self.wtitle = _("SMTP Account 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)
        # smtp server
        self.smtp_server_lbl = HIGEntryLabel(_("Server"))
        self.smtp_server = gtk.Entry()
        self.smtp_port_lbl = HIGEntryLabel(_("Port"))
        self.smtp_port = gtk.Entry()
        # sending mail..
        self.smtp_mailfrom_lbl = HIGEntryLabel(_("Mail from"))
        self.smtp_mailfrom = gtk.Entry()
        # smtp auth
        self.smtp_need_auth = gtk.CheckButton(
            _("Servers requires authentication"))
        self.smtp_need_auth.connect('toggled', self._auth_need)
        self.smtp_login_lbl = HIGEntryLabel(_("Username"))
        self.smtp_login = gtk.Entry()
        self.smtp_passwd_lbl = HIGEntryLabel(_("Password"))
        self.smtp_passwd = gtk.Entry()
        self.smtp_passwd.set_visibility(False)
        self._auth_need(None)
        # smtp encryption
        self.smtp_encrypt_tls = gtk.CheckButton(_("Use TLS Encryption"))
        """
        Missing: SSL encryption,
                 Other authentication methods.
        """

        # 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_schemas()

        self.__set_props()
        self.__do_layout()

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

    def load_schemas(self):
        """
        Load schemas profiles.
        """
        schemas = ConfigParser()
        schemas.read(Path.smtp_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.smtp_schemas)

        enable = {
            'tls': self.smtp_encrypt_tls.set_active,
            'auth': self.smtp_need_auth.set_active
        }
        values = {
            'user': self.smtp_login.set_text,
            'pass': self.smtp_passwd.set_text,
            'server': self.smtp_server.set_text,
            'port': self.smtp_port.set_text,
            'mailfrom': self.smtp_mailfrom.set_text
        }

        for item in schema.items(self.schema_name.get_active_text()):
            if item[0] in ('tls', 'auth'):
                enable[item[0]](int(item[1]))
            else:
                values[item[0]](item[1])

    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.smtp_mailfrom.set_text('')
            self.smtp_server.set_text('')
            self.smtp_port.set_text('')
            self.smtp_encrypt_tls.set_active(False)
            self.smtp_login.set_text('')
            self.smtp_passwd.set_text('')
            self.smtp_need_auth.set_active(False)
            self._auth_need(None)

    def _auth_need(self, event):
        """
        SMTP Authentication toggled.
        """
        status = self.smtp_need_auth.get_active()
        self.smtp_login.set_sensitive(status)
        self.smtp_passwd.set_sensitive(status)
        self.smtp_login_lbl.set_sensitive(status)
        self.smtp_passwd_lbl.set_sensitive(status)

    def _save_schema(self, event):
        """
        Save current schema.
        """
        schema = self.schema_name.get_active_text()
        server = self.smtp_server.get_text()
        port = self.smtp_port.get_text()
        auth = self.smtp_need_auth.get_active()
        mailfrom = self.smtp_mailfrom.get_text()

        if auth:
            user = self.smtp_login.get_text()
            passwd = self.smtp_passwd.get_text()

        if auth and not (user and passwd):
            dlg = HIGAlertDialog(self,
                                 message_format=_('SMTP Schema - Error\
 while saving.'),
                                 secondary_text=_("You need to specify an \
username and password for this SMTP Schema."))

            dlg.run()
            dlg.destroy()
            return

        if not schema or not server or not port or not mailfrom:
            dlg = HIGAlertDialog(self,
                                 message_format=_('SMTP Schema - Error\
 while saving.'),
                                 secondary_text=_("The following fields \
need to be filled: Schema Name, Server, Port and Mail from."))

            dlg.run()
            dlg.destroy()
            return

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

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

        if auth:
            s_cfg.set(schema, 'auth', '1')
        else:
            s_cfg.set(schema, 'auth', '0')
            user = ''
            passwd = ''

        s_cfg.set(schema, 'port', port)
        s_cfg.set(schema, 'server', server)
        s_cfg.set(schema, 'user', user)
        s_cfg.set(schema, 'pass', passwd)
        s_cfg.set(schema, 'mailfrom', mailfrom)

        if self.smtp_encrypt_tls.get_active():
            s_cfg.set(schema, 'tls', '1')
        else:
            s_cfg.set(schema, 'tls', '0')

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

        if new_sec:
            self.load_schemas()

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

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

    def _show_help(self, event):
        """
        Open SMTP Setup help
        """
        show_help(self, "smtpsetup.html")

    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()
        auth_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)

        # smtp server
        schema_table.attach_label(self.smtp_server_lbl, 0, 1, 1, 2)
        schema_table.attach_entry(self.smtp_server, 1, 2, 1, 2)

        # smtp server port
        schema_table.attach_label(self.smtp_port_lbl, 0, 1, 2, 3)
        schema_table.attach_entry(self.smtp_port, 1, 2, 2, 3)

        # smtp mail from
        schema_table.attach_label(self.smtp_mailfrom_lbl, 0, 1, 3, 4)
        schema_table.attach_entry(self.smtp_mailfrom, 1, 2, 3, 4)

        # smtp user
        auth_table.attach_label(self.smtp_login_lbl, 0, 1, 0, 1)
        auth_table.attach_entry(self.smtp_login, 1, 2, 0, 1)

        # smtp passwd
        auth_table.attach_label(self.smtp_passwd_lbl, 0, 1, 1, 2)
        auth_table.attach_label(self.smtp_passwd, 1, 2, 1, 2)

        # 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.smtp_need_auth)
        main_vbox._pack_noexpand_nofill(auth_table)
        main_vbox._pack_noexpand_nofill(gtk.HSeparator())
        main_vbox._pack_noexpand_nofill(self.smtp_encrypt_tls)
        main_vbox.pack_end(btns_hbox, False, False, 0)

        self.add(main_vbox)

    def _exit(self, event):
        """
        Close current window.
        """
        self.destroy()
Beispiel #5
0
class GeneralSettings(TabBox, object):
    """
    General Settings
    - Splash enable/disable
    - Warnings
    - Erros, log, configure bugreport
    - Autosave: clean up, define folder etc
    - Define Nmap Command
    """

    def __init__(self, name):
        TabBox.__init__(self, name)


    def _create_widgets(self):
        """
        Design all
        """
        # Create general widgets
        self._create_widgets_common()
        self._create_widgets_autosave()
        self._create_widgets_error()
        # Packing main section
        self.pack_start(self._box_common, False, False)
        self.pack_start(self._box_error_frame, False, False)
        self.pack_start(self._box_save_frame, False, False)
        self._box_error_frame.set_shadow_type(gtk.SHADOW_NONE)
        self._box_error_frame.set_shadow_type(gtk.SHADOW_NONE)


        # Settings Values

        self.splash = general_settings.splash
        self.warnings_extensions = general_settings.warnings_extensions
        self.silent_root = general_settings.silent_root
        self.crash_report = general_settings.crash_report
        self.log = general_settings.log
        self.log_file = general_settings.log_file
        self.warnings_save = general_settings.warnings_save

        self._connect_events()
    def _create_widgets_common(self):
        """ generally tab """
        self._box_common = HIGVBox() # pack main section
        self.__check_splash = gtk.CheckButton(_('Enable Splash on start'))
        self.__check_silent_root = gtk.CheckButton(\
            _('Silent Warning Non-Root'))
        self.__check_warning_extensions = gtk.CheckButton(\
            _('Set/Check extensions - Windows only'))
        self._box_common.pack_start(self.__check_splash, False, False)
        #self._box_common.pack_start(self.__check_warning_extensions, False, \
        #                            False)
        self._box_common.pack_start(self.__check_silent_root, False, False)
        self.__label_nmap = HIGEntryLabel(_('Nmap Command'))
        self.__entry_nmap = HIGTextEntry()
        # Files usr saved on predefined directory:
        self.__label_path = HIGEntryLabel(_('Nmap Command'))
        self.__entry_path = HIGTextEntry()
        self.__button_path = HIGButton(_('Choose'))
        self.__box_path = HIGHBox()
        self.__box_path.pack_start(self.__label_path, False, False)
        self.__box_path.pack_end(self.__button_path, False, False)

        self._box_common.set_border_width(0)




    def _create_widgets_error(self):

        # Create Widgets
        self._box_error_frame = HIGFrame('Error')

        self._box_error = HIGTable(5, 2)
        self._box_error.set_border_width(10)
        self._box_error_frame.add(self._box_error)
        self.__crash_report = gtk.CheckButton(_('Enable Crash Report'))
        # Radio Button List
        self.__log_no = gtk.RadioButton(None, _('No log'))
        self.__log_terminal = gtk.RadioButton(self.__log_no, _('Enable log in terminal'))
        self.__log_file = gtk.RadioButton(self.__log_terminal,\
                                         _('Enable log file'))

        self.__log_file_label = HIGEntryLabel(_('Log file'))
        self.__log_file_entry = HIGTextEntry()
        self.__log_file_entry.set_editable(False)
        # FIXME: Do default file ~/.umit/umit.log
        self.__log_file_browser = HIGButton(_('Browse file'), \
                                            gtk.STOCK_DIRECTORY)

        tmpbox = HIGHBox()
        tmpbox.pack_start(self.__log_file_entry, False, False)
        tmpbox.pack_start(self.__log_file_browser, False, False)


        # attach table
        self._box_error.attach(self.__crash_report, 0,1,0,1,\
                               gtk.FILL|gtk.EXPAND| gtk.SHRINK, gtk.FILL)
        self._box_error.attach(self.__log_no, 0,1,1,2,\
                               gtk.FILL|gtk.EXPAND| gtk.SHRINK, gtk.FILL)
        self._box_error.attach(self.__log_terminal, 0,1,2,3,\
                               gtk.FILL|gtk.EXPAND| gtk.SHRINK, gtk.FILL)
        self._box_error.attach(self.__log_file, 0,1,3,4,\
                               gtk.FILL|gtk.EXPAND| gtk.SHRINK, gtk.FILL)
        self._box_error.attach(self.__log_file_label, 0,1,4,5,\
                               gtk.FILL|gtk.EXPAND| gtk.SHRINK, gtk.FILL)
        self._box_error.attach(tmpbox, 1,2,4,5,\
                               gtk.FILL|gtk.EXPAND| gtk.SHRINK, gtk.FILL)
    def _create_widgets_autosave(self):

        # Create Widgets
        self._box_save_frame = HIGFrame('Auto-save')

        self._box_save = HIGVBox()
        self._box_save_frame.add(self._box_save)
        self._box_save.set_border_width(10)

        self._warnings_save = gtk.CheckButton(
            _('Enable warnings without saving'))
        self._clean_b = HIGButton(_('Delete saved files'), gtk.STOCK_CLEAR)
        self._clean_b_box = HIGHBox()
        self._clean_b_box.pack_start(self._clean_b, False, True)

        # pack

        self._box_save.pack_start(self._warnings_save, False, False)
        self._box_save.pack_start(self._clean_b_box, False, False)
    def _connect_events(self):
        """
        connect call backs
        """

        self.__check_splash.connect('toggled', self.update_splash)
        self.__check_silent_root.connect('toggled', self.update_silent_root)
        self.__crash_report.connect('toggled', self.update_crash_report)


        self.__log_file.connect('toggled', self.update_log)
        self.__log_file.connect('toggled', self.update_log_file)
        self.__log_terminal.connect('toggled', self.update_log)
        self.__log_no.connect('toggled', self.update_log)

        self.__log_file_browser.connect('clicked', self.read_file)
        self.__log_file_entry.connect('changed', self.update_log_file_text)

        self._warnings_save.connect('toggled', self.update_save_warn)
        
        self._clean_b.connect('clicked', self.delete_files)
        
        self.update_log_file(None)
    # Callbacks
    def delete_files(self, widget):
        # NOTE: This method should be adapted in umit.core.UserConf
        # Creating an empty recent_scans file
        
        log.debug('>>> Reset store files:  -  %s' % base_paths['user_dir'])

        reset_user_dir(base_paths['user_dir'])
        
    
    def update_save_warn(self, widget):
        general_settings.warnings_save = self.warnings_save

    def read_file(self, widget):
        dialog = gtk.FileChooserDialog("Open..",
                                       None,
                                       gtk.FILE_CHOOSER_ACTION_SAVE,
                                       (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
                                        gtk.STOCK_SAVE_AS, gtk.RESPONSE_OK))
        dialog.set_default_response(gtk.RESPONSE_OK)
        response = dialog.run()
        if response == gtk.RESPONSE_OK:
            # Get name of file
            self.__log_file_entry.set_text(dialog.get_filename())
        dialog.destroy()

    def update_crash_report(self, widget):
        general_settings.crash_report = self.crash_report
    def update_splash(self, widget):
        general_settings.splash = self.splash
    def update_silent_root(self, widget):
        general_settings.silent_root = self.silent_root
    def update_log_file_text(self, widget):
        general_settings.log_file = self.log_file
    def update_log_file(self, widget):
        if not self.__log_file.get_active():
            self.__log_file_active(False)
        else:
            self.__log_file_active(True)
    def update_log(self, widget):
        """ Verify if is enable, if it is not enable nothing to do """
        if not widget.get_active():
            return
        # Widget is enable.
        ## But which is enable?

        # Setting kind of log
        if widget == self.__log_no:
            general_settings.log = "None"
        elif widget == self.__log_terminal:
            general_settings.log = "Debug"
        elif widget == self.__log_file:
            general_settings.log = "File"


    # API
    def get_splash(self):
        return self.__check_splash.get_active()
    def set_splash(self, splash):
        self.__check_splash.set_active(splash)

    def set_warnings_extensions(self, extensions):
        self.__check_warning_extensions.set_active(extensions)
    def get_warnings_extensions(self):
        return self.__check_warning_extensions.get_active()
    def set_silent_root(self, root):
        self.__check_silent_root.set_active(root)
    def get_silent_root(self):
        return self.__check_silent_root.get_active()


    def set_crash_report(self, crash):
        self.__crash_report.set_active(crash)
    def get_crash_report(self):
        return self.__crash_report.get_active()

    def get_log(self):
        """
        filter a str with None, Debug or File
        return: int (means 0 - No debug, 1 - debug, 2 - debug file)
        """
        if self.__log_no.get_active():
            return "None"
        elif self.__log_terminal.get_active():
            return "Debug"
        elif self.__log_file.get_active():
            return "File"
        else:
            raise LogException()



    def set_log(self, log):
        if log == "None":
            self.__log_no.set_active(True)
        elif log == "Debug":
            self.__log_terminal.set_active(True)
        elif log == "File":
            self.__log_file.set_active(True)


    def __log_file_active(self, bool):
        self.__log_file_label.set_sensitive(bool)
        self.__log_file_entry.set_sensitive(bool)
        self.__log_file_browser.set_sensitive(bool)

    def set_log_file(self, filename):
        self.__log_file_entry.set_text(filename)
    def get_log_file(self):
        return self.__log_file_entry.get_text()

    def set_warnings_save(self, save):
        self._warnings_save.set_active(save)
    def get_warnings_save(self):
        return self._warnings_save.get_active()

    # Propertys
    splash = property(get_splash, set_splash)
    warnings_extensions = property(get_warnings_extensions, \
                                   set_warnings_extensions)
    silent_root = property(get_silent_root, set_silent_root)
    crash_report = property(get_crash_report, set_crash_report)
    log = property(get_log, set_log)
    log_file = property(get_log_file, set_log_file)
    warnings_save = property(get_warnings_save, set_warnings_save)