Example #1
0
def _check_forward_port(option, opt, value):
    result = { }
    error = None
    splits = value.split(":", 1)
    while len(splits) > 0:
        key_val = splits[0].split("=")
        if len(key_val) != 2:
            error = _("Invalid argument %s") % splits[0]
            break
        (key, val) = key_val
        if (key == "if" and checkInterface(val)) or \
                (key == "proto" and val in [ "tcp", "udp" ]) or \
                (key == "toaddr" and checkIP(val)):
            result[key] = val
        elif (key == "port" or key == "toport") and getPortRange(val) > 0:
            result[key] = getPortRange(val)
        else:
            error = _("Invalid argument %s") % splits[0]
            break
        if len(splits) > 1:
            if splits[1].count("=") == 1:
                # last element
                splits = [ splits[1] ]
            else:
                splits = splits[1].split(":", 1)
        else:
            # finish
            splits.pop()

    if error:
        dict = { "option": opt, "value": value, "error": error }
        raise OptionError(_("option %(option)s: invalid forward_port "
                                 "'%(value)s': %(error)s.") % dict, opt)

    error = False
    for key in [ "if", "port", "proto" ]:
        if key not in result.keys():
            error = True
    if not "toport" in result.keys() and not "toaddr" in result.keys():
        error = True
    if error:
        dict = { "option": opt, "value": value }
        raise OptionError(_("option %(option)s: invalid forward_port "
                                 "'%(value)s'.") % dict, opt)

    return result
Example #2
0
    def forward_port_selection(self, interface=None, protocol=None, port=None,
                               to_address=None, to_port=None):
        _interface = ( interface if interface else "" )
        _protocol = ( protocol if protocol else "" )
        _port = ( self.__simplePortStr(port) if port else "" )
        _to_address = ( to_address if to_address else "" )
        _to_port = ( self.__simplePortStr(to_port) if to_port else "" )
        while 1:
            dialog = GridForm(self.screen, _("Port Forwarding"), 1, 6)
            tr = TextboxReflowed(40, _("Please select the source and "
                                       "destination options according "
                                       "to your needs."))
            dialog.add(tr, 0, 0, padding=(0,0,0,1), growx=1)

            dialog.add(TextboxReflowed(40, _("Source (all needed)")), 0, 1,
                       padding=(0,0,0,0), growx=1, anchorLeft=1)

            grid = Grid(2, 3)

            grid.setField(Label(_("Interface:")), 0, 0,
                          padding=(0,0,1,0), anchorLeft=1)
            dialog.interface = Entry(20, text=_interface)
            grid.setField(dialog.interface, 1, 0, padding=(0,0,1,0),
                          anchorLeft=1)

            grid.setField(Label(_("Protocol:")), 0, 1,
                          padding=(0,0,1,0), anchorLeft=1)
            dialog.protocol = Entry(20, text=_protocol)
            grid.setField(dialog.protocol, 1, 1, padding=(0,0,1,0),
                          anchorLeft=1)

            grid.setField(Label(_("Port / Port Range:")), 0, 2,
                          padding=(0,0,1,0), anchorLeft=1)
            dialog.port = Entry(20, text=_port)
            grid.setField(dialog.port, 1, 2, padding=(0,0,1,0),
                          anchorLeft=1)

            dialog.add(grid, 0, 2, padding=(0,0,0,1))

            dialog.add(TextboxReflowed(40, _("Destination (at least one "
                                             "needed)")), 0, 3,
                       padding=(0,0,0,0), growx=1, anchorLeft=1)

            grid = None
            grid = Grid(2, 2)

            grid.setField(Label(_("IP address:")), 0, 0,
                          padding=(0,0,1,0), anchorLeft=1)
            dialog.to_address = Entry(20, text=_to_address)
            grid.setField(dialog.to_address, 1, 0, padding=(0,0,1,0),
                          anchorLeft=1)

            grid.setField(Label(_("Port / Port Range:")), 0, 1,
                          padding=(0,0,1,0), anchorLeft=1)
            dialog.to_port = Entry(20, text=_to_port)
            grid.setField(dialog.to_port, 1, 1, padding=(0,0,1,0),
                          anchorLeft=1)

            dialog.add(grid, 0, 4, padding=(0,0,0,1))
            dialog.bb = ButtonBar(self.screen,
                                  ((_("OK"), "ok"), (_("Cancel"), "cancel")))
            dialog.add(dialog.bb, 0, 5, growx=1)
            res = dialog.bb.buttonPressed(dialog.runPopup())
            self.screen.popWindow()
            values = (dialog.interface.value(), dialog.protocol.value(),
                      dialog.port.value(), dialog.to_address.value(),
                      dialog.to_port.value())

            if res == 'ok':
                error = False
                # interface
                _interface = values[0].strip()
                if not len(_interface) > 0 or not checkInterface(_interface):
                    self.error(_("Interface '%s' is not valid.") % _interface)
                    error = True
                else:
                    interface = _interface
                # protocol
                _protocol = values[1].strip()
                if not _protocol in [ "tcp", "udp" ]:
                    self.protocol_error(_protocol)
                    error = True
                else:
                    protocol = _protocol
                # port
                _port = values[2].strip()
                port = getPortRange(_port)
                if not (isinstance(port, types.ListType) or \
                            isinstance(port, types.TupleType)):
                    self.port_error(_port)
                    error = True
                    port = None
                # to_address
                _to_address = values[3].strip()
                if len(_to_address) > 0 and not checkIP(_to_address):
                    self.error(_("Address '%s' is not valid.") % _to_address)
                    error = True
                    to_address = None
                else:
                    to_address = _to_address
                # to_port
                _to_port = values[4].strip()
                if len(_to_port) > 0:
                    to_port = getPortRange(_to_port)
                    if not (isinstance(to_port, types.ListType) or \
                                isinstance(to_port, types.TupleType)):
                        self.port_error(_to_port)
                        error = True
                        to_port = None

                if error:
                    continue
                if not interface or not protocol or not port:
                    continue
                if not to_address and not to_port:
                    continue

                return (interface, protocol, port, to_address, to_port)
            elif res == 'cancel':
                return None
Example #3
0
def _check_interface(option, opt, value):
    if not checkInterface(value):
        raise OptionError(_("invalid interface '%s'.") % value, opt)
    return value