Example #1
0
File: lan.py Project: chlordk/foris
        def lan_form_cb(data):
            uci = Uci()
            config = Config("dhcp")
            uci.add(config)

            dhcp = Section("lan", "dhcp")
            config.add(dhcp)
            # FIXME: this would overwrite any unrelated DHCP options the user might have set.
            # Maybe we should get the current values, scan them and remove selectively the ones
            # with 6 in front of them? Or have some support for higher level of stuff in nuci.
            options = List("dhcp_option")
            options.add(Value(0, "6," + data['lan_ipaddr']))
            dhcp.add_replace(options)
            network = Config("network")
            uci.add(network)
            interface = Section("lan", "interface")
            network.add(interface)
            interface.add(Option("ipaddr", data['lan_ipaddr']))
            if data['dhcp_enabled']:
                dhcp.add(Option("ignore", "0"))
                dhcp.add(Option("start", data['dhcp_min']))
                dhcp.add(Option("limit", data['dhcp_max']))
            else:
                dhcp.add(Option("ignore", "1"))

            # qos data
            qos = {'enabled': False}
            if 'guest_network_shapping' in data and data['guest_network_shapping']:
                qos['enabled'] = True
                qos['download'] = data['guest_network_download']
                qos['upload'] = data['guest_network_upload']

            # update guest network configs
            guest_enabled = data.get("guest_network_enabled")
            guest_network_subnet = data.get("guest_network_subnet")
            if guest_network_subnet:
                network, prefix = data.get("guest_network_subnet").split("/")
            else:
                network, prefix = DEFAULT_GUEST_NETWORK, DEFAULT_GUEST_PREFIX

            # disable guest wifi when guest network is not enabled
            data = client.get(filter=wifi_filter())
            card_count = 0
            while data.find_child("uci.wireless.@wifi-device[%d]" % card_count):
                card_count += 1
            if not guest_enabled and card_count > 0:
                wireless = uci.add(Config("wireless"))
                for i in range(card_count):
                    guest_iface = wireless.add(Section("guest_iface_%d" % i, "wifi-iface"))
                    guest_iface.add(Option("disabled", "1"))

            guest_interfaces = ["guest_turris_%d" % e for e in range(card_count)]

            LanHandler.prepare_guest_configs(
                uci, guest_enabled, network, prefix, guest_interfaces, qos)

            return "edit_config", uci
Example #2
0
        def package_lists_form_cb(data):
            uci = Uci()
            updater = Config("updater")
            uci.add(updater)

            pkglists = Section("pkglists", "pkglists")
            updater.add(pkglists)
            new_package_list = List("lists")

            langlists = Section("l10n", "l10n")
            updater.add(langlists)
            new_language_list = List("langs")

            # create List with selected packages
            i = 0
            for k, v in [(k, v) for k, v in data.iteritems()
                         if v and k.startswith("install_")]:
                new_package_list.add(Value(i, k[8:]))
                i += 1
            # If user agreed with data collection, add i_agree_datacollect list
            if agreed_collect:
                new_package_list.add(Value(i, "i_agree_datacollect"))
                i += 1
            if i == 0:
                pkglists.add_removal(new_package_list)
            else:
                pkglists.add_replace(new_package_list)

            # create language list
            i = 0
            for k, v in [(k, v) for k, v in data.iteritems()
                         if v and k.startswith("language_")]:
                new_language_list.add(Value(i, k[9:]))
                i += 1
            if i == 0:
                langlists.add_removal(new_language_list)
            else:
                langlists.add_replace(new_language_list)

            return "edit_config", uci
Example #3
0
        def adjust_lists_cb(data):
            uci = Uci()
            # All enabled lists
            enabled_lists = map(
                lambda x: x.content,
                form.nuci_config.find_child(
                    "uci.updater.pkglists.lists").children)
            # Lists that do not need agreement
            enabled_no_agree = filter(lambda x: not x.startswith("i_agree_"),
                                      enabled_lists)
            # Lists that need agreement
            enabled_i_agree = filter(lambda x: x.startswith("i_agree_"),
                                     enabled_lists)

            # Always install lists that do not need agreement - create a copy of the list
            installed_lists = enabled_no_agree[:]
            logger.warning("no agree: %s", enabled_no_agree)
            logger.warning("installed: %s", installed_lists)
            if data.get("enable", False):
                # Include i_agree lists if user agreed with EULA
                installed_lists.extend(enabled_i_agree)
                # Add main data collection list if it's not present
                logger.warning(installed_lists)
                logger.warning("i_agree_datacollect" not in installed_lists)
                logger.warning("i_agree_datacollect" in installed_lists)
                if "i_agree_datacollect" not in installed_lists:
                    logger.warning("appending")
                    installed_lists.append("i_agree_datacollect")
            logger.warning("saving %s", installed_lists)
            # Reconstruct list of package lists
            updater = uci.add(Config("updater"))
            pkglists = updater.add(Section("pkglists", "pkglists"))
            lists = List("lists")
            for i, name in enumerate(installed_lists):
                lists.add(Value(i, name))

            # If there's anything to add, replace the list, otherwise remove it completely
            if len(installed_lists) > 0:
                pkglists.add_replace(lists)
            else:
                pkglists.add_removal(lists)

            return "edit_config", uci
Example #4
0
        def notifications_form_cb(data):
            uci = Uci()
            user_notify = Config("user_notify")
            uci.add(user_notify)

            smtp = Section("smtp", "smtp")
            user_notify.add(smtp)
            smtp.add(Option("enable", data['enable_smtp']))

            reboot = Section("reboot", "reboot")
            user_notify.add(reboot)
            reboot.add(Option("time", data['reboot_time']))
            reboot.add(Option("delay", data['delay']))

            if data['enable_smtp']:
                smtp.add(Option("use_turris_smtp", data['use_turris_smtp']))
                if data['use_turris_smtp'] == "0":
                    smtp.add(Option("server", data['server']))
                    smtp.add(Option("port", data['port']))
                    smtp.add(Option("username", data['username']))
                    smtp.add(Option("password", data['password']))
                    smtp.add(Option("security", data['security']))
                    smtp.add(Option("from", data['from']))
                else:
                    smtp.add(Option("sender_name", data['sender_name']))
                to = List("to")
                for i, to_item in enumerate(data['to'].split(" ")):
                    if to_item:
                        to.add(Value(i, to_item))
                smtp.add_replace(to)
                # notifications section
                notifications = Section("notifications", "notifications")
                user_notify.add(notifications)
                notifications.add(Option("severity", data['severity']))
                notifications.add(Option("news", data['news']))

            return "edit_config", uci
Example #5
0
        def ucollect_form_cb(data):
            uci = Uci()
            ucollect = Config("ucollect")
            uci.add(ucollect)

            fakes = Section("fakes", "fakes")
            ucollect.add(fakes)

            disable = List("disable")

            disabled_services = [
                x[0] for x in SERVICES_OPTIONS if x[0] not in data['services']
            ]
            for i, service in enumerate(disabled_services):
                disable.add(Value(i, service))

            if len(disabled_services):
                fakes.add_replace(disable)
            else:
                fakes.add_removal(disable)

            fakes.add(Option("log_credentials", data['log_credentials']))

            return "edit_config", uci
Example #6
0
        def wan_form_cb(data):
            uci = Uci()
            network = Config("network")
            uci.add(network)

            wan = Section("wan", "interface")
            network.add(wan)

            wan.add(Option("proto", data['proto']))
            if data['custom_mac'] is True:
                wan.add(Option("macaddr", data['macaddr']))
            else:
                wan.add_removal(Option("macaddr", None))

            ucollect_ifname = self.wan_ifname

            if data['proto'] == WAN_PPPOE:
                wan.add(Option("username", data['username']))
                wan.add(Option("password", data['password']))
                wan.add(Option("ipv6",
                               data.get("wan6_proto") is not WAN6_NONE))
                ucollect_ifname = "pppoe-wan"
            elif data['proto'] == WAN_STATIC:
                wan.add(Option("ipaddr", data['ipaddr']))
                wan.add(Option("netmask", data['netmask']))
                wan.add(Option("gateway", data['gateway']))
                dns_list = List("dns")
                dns2 = data.get("dns2", None)
                if dns2:
                    dns_list.add(Value(0, dns2))
                dns1 = data.get("dns1", None)
                if dns1:
                    dns_list.add(Value(
                        1,
                        dns1))  # dns with higher priority should be added last
                if not dns_list.children:
                    wan.add_removal(dns_list)
                else:
                    wan.add_replace(dns_list)

            # IPv6 configuration
            wan6 = Section("wan6", "interface")
            network.add(wan6)
            wan6.add(Option("ifname", "@wan"))
            wan6.add(Option("proto", data['wan6_proto']))

            if data.get("wan6_proto") == WAN6_STATIC:
                wan6.add(Option("ip6addr", data['ip6addr']))
                wan6.add(Option("ip6gw", data['ip6gw']))
                wan6.add(Option("ip6prefix", data['ip6prefix']))
            else:
                wan6.add_removal(Option("ip6addr", None))
                wan6.add_removal(Option("ip6gw", None))
                wan6.add_removal(Option("ip6prefix", None))

            if has_smrtd:
                smrtd = Config("smrtd")
                uci.add(smrtd)

                smrt_vlan = data.get("smrt_vlan")
                use_smrt = data.get("use_smrt", False)

                wan_if = Section(self.wan_ifname, "interface")
                smrtd.add(wan_if)
                wan_if.add(Option("name", self.wan_ifname))

                if use_smrt:
                    if not smrt_vlan:
                        # "proprietary" number - and also a common VLAN ID in CZ
                        smrt_vlan = "848"
                        self.wan_ifname += ".%s" % smrt_vlan

                vpi, vci = data.get("smrt_vpi"), data.get("smrt_vci")
                connections = List("connections")
                if vpi and vci:
                    wan_if.add(connections)
                    connections.add(
                        Value(1, "%s %s %s" % (smrt_vlan, vpi, vci)))
                elif use_smrt:
                    wan_if.add_removal(connections)

                smrtd_global = Section("global", "global")
                smrtd.add(smrtd_global)
                smrtd_global.add(Option("enabled", use_smrt))

                # set correct ifname for WAN - must be changed when disabling SMRT
                wan.add(Option("ifname", self.wan_ifname))

            # set interface for ucollect to listen on
            interface_if_name = None
            ucollect_interface0 = wan_form.nuci_config.find_child(
                "uci.ucollect.@interface[0]")
            if ucollect_interface0:
                interface_if_name = ucollect_interface0.name

            ucollect = Config("ucollect")
            uci.add(ucollect)
            interface = Section(interface_if_name, "interface", True)
            ucollect.add(interface)
            interface.add(Option("ifname", ucollect_ifname))

            return "edit_config", uci
Example #7
0
File: lan.py Project: chlordk/foris
    def prepare_guest_configs(uci, enabled, network, prefix, interfaces=[], qos={}):
        ignore = "0" if enabled else "1"
        enabled = "1" if enabled else "0"

        # parse router ip address (192.168.1.0 -> 192.168.1.1)
        router_ip = ip_num_to_str_4(ip_str_to_num_4(network) + 1)
        netmask = prefix_to_mask_4(int(prefix))

        # update network interface list
        network_conf = uci.find_child("network") or Config("network")
        uci.add(network_conf)
        interface_section = Section("guest_turris", "interface")
        network_conf.add_replace(interface_section)
        interface_section.add(Option("enabled", enabled))
        interface_section.add(Option("type", "bridge"))
        if interfaces:
            interface_section.add(Option("ifname", " ".join(interfaces)))
        interface_section.add(Option("proto", "static"))
        interface_section.add(Option("ipaddr", router_ip))
        interface_section.add(Option("netmask", netmask))
        interface_section.add(Option("bridge_empty", "1"))

        # update firewall config
        firewall_conf = uci.find_child("firewall") or Config("firewall")
        uci.add(firewall_conf)

        zone_section = Section("guest_turris", "zone")
        firewall_conf.add_replace(zone_section)
        zone_section.add(Option("enabled", enabled))
        zone_section.add(Option("name", "guest_turris"))
        network_list = List("network")
        network_list.add(Value(0, "guest_turris"))
        zone_section.add(network_list)
        zone_section.add(Option("input", "REJECT"))
        zone_section.add(Option("forward", "REJECT"))
        zone_section.add(Option("output", "ACCEPT"))

        wan_forwarding_section = Section("guest_turris_forward_wan", "forwarding")
        firewall_conf.add_replace(wan_forwarding_section)
        wan_forwarding_section.add(Option("enabled", enabled))
        wan_forwarding_section.add(Option("name", "guest to wan forward"))
        wan_forwarding_section.add(Option("src", "guest_turris"))
        wan_forwarding_section.add(Option("dest", "wan"))

        dns_rule_section = Section("guest_turris_dns_rule", "rule")
        firewall_conf.add_replace(dns_rule_section)
        dns_rule_section.add(Option("enabled", enabled))
        dns_rule_section.add(Option("name", "guest dns rule"))
        dns_rule_section.add(Option("src", "guest_turris"))
        dns_rule_section.add(Option("proto", "tcpudp"))
        dns_rule_section.add(Option("dest_port", 53))
        dns_rule_section.add(Option("target", "ACCEPT"))

        dhcp_rule_section = Section("guest_turris_dhcp_rule", "rule")
        firewall_conf.add_replace(dhcp_rule_section)
        dhcp_rule_section.add(Option("enabled", enabled))
        dhcp_rule_section.add(Option("name", "guest dhcp rule"))
        dhcp_rule_section.add(Option("src", "guest_turris"))
        dhcp_rule_section.add(Option("proto", "udp"))
        dhcp_rule_section.add(Option("src_port", "67-68"))
        dhcp_rule_section.add(Option("dest_port", "67-68"))
        dhcp_rule_section.add(Option("target", "ACCEPT"))

        # update dhcp config
        dhcp_conf = uci.find_child("dhcp") or Config("dhcp")
        uci.add(dhcp_conf)

        dhcp_section = Section("guest_turris", "dhcp")
        dhcp_conf.add_replace(dhcp_section)
        dhcp_section.add(Option("interface", "guest_turris"))
        dhcp_section.add(Option("start", "200"))
        dhcp_section.add(Option("limit", "50"))
        dhcp_section.add(Option("leasetime", "1h"))
        dhcp_section.add(Option("ignore", ignore))
        dhcp_option_list = List("dhcp_option")
        dhcp_option_list.add(Value(0, "6,%s" % router_ip))
        dhcp_section.add(dhcp_option_list)

        # update qos part
        if qos:
            qos_conf = uci.find_child("sqm") or Config("sqm")
            uci.add(qos_conf)

            queue_section = qos_conf.add_replace(Section("guest_limit_turris", "queue"))
            queue_section.add(Option("enabled", qos["enabled"]))
            if qos["enabled"]:
                queue_section.add(Option("interface", "br-guest_turris"))
                queue_section.add(Option("qdisc", "fq_codel"))
                queue_section.add(Option("script", "simple.qos"))
                queue_section.add(Option("link_layer", "none"))
                queue_section.add(Option("verbosity", "5"))
                queue_section.add(Option("debug_logging", "1"))
                # We need to swap dowload and upload
                # "upload" means upload to the guest network
                # "download" means dowload from the guest network
                # so it would be confusing for a client who tries to run some speedtest
                queue_section.add(Option("download", qos["upload"]))
                queue_section.add(Option("upload", qos["download"]))