Пример #1
0
def test_uciutils():
    """
    Test UCI utility functions
    """
    from paradrop.lib.config import uciutils

    # Test appendListItem function
    options = dict()
    uciutils.appendListItem(options, "name", "value1")
    uciutils.appendListItem(options, "name", "value2")
    assert options['list']['name'] == ["value1", "value2"]

    # Test setList function
    options = dict()
    uciutils.setList(options, "name", ["value1", "value2"])
    assert options['list']['name'] == ["value1", "value2"]
Пример #2
0
def getVirtDHCPSettings(update):
    """
    Looks at the runtime rules the developer defined to see if they want a dhcp
    server.  If so it generates the data and stores it into the chute cache
    key:virtDHCPSettings.
    """

    interfaces = update.new.getCache('networkInterfaces')
    if interfaces is None:
        return

    dhcpSettings = list()

    for iface in interfaces:
        # Only look at interfaces with DHCP server requested.
        if 'dhcp' not in iface:
            continue
        dhcp = iface['dhcp']

        # Check for required fields.
        res = pdutils.check(dhcp, dict, ['lease', 'start', 'limit'])
        if(res):
            out.warn('DHCP server definition {}\n'.format(res))
            raise Exception("DHCP server definition missing field(s)")

        # NOTE: Having one dnsmasq section for each interface deviates from how
        # OpenWRT does things, where they assume a single instance of dnsmasq
        # will be handling all DHCP and DNS needs.
        config = {'type': 'dnsmasq'}
        options = {}
        uciutils.setList(options, 'interface', [iface['externalIntf']])

        # Optional: developer can pass in a list of DNS nameservers to use
        # instead of the system default.
        #
        # This path -> clients query our dnsmasq server; dnsmasq uses the
        # specified nameservers and caches the results.
        if DNSMASQ_CACHE_ENABLED and 'dns' in dhcp:
            options['noresolv'] = '1'
            uciutils.setList(options, 'server', dhcp['dns'])

        dhcpSettings.append((config, options))

        config = {'type': 'dhcp', 'name': iface['externalIntf']}
        options = {
            'interface': iface['externalIntf'],
            'start': dhcp['start'],
            'limit': dhcp['limit'],
            'leasetime': dhcp['lease'],
        }

        # This option tells clients that the router is the interface inside the
        # chute not the one in the host.
        uciutils.setList(options, 'dhcp_option', ["option:router,{}".format(
                                                    iface['internalIpaddr'])])

        # Optional: developer can pass in a list of DNS nameservers to use
        # instead of the system default.
        #
        # This path -> clients receive the list of DNS servers and query them
        # directly.
        if not DNSMASQ_CACHE_ENABLED and 'dns' in dhcp:
            uciutils.appendListItem(options, 'dhcp_option',
                    ",".join(["option:dns-server"] + dhcp['dns']))

        dhcpSettings.append((config, options))

    update.new.setCache('virtDHCPSettings', dhcpSettings)
Пример #3
0
def setSystemDevices(update):
    """
    Initialize system configuration files.

    Creates basic sections that all chutes require such as the "wan" interface.
    """
    hostConfig = update.new.getCache('hostConfig')

    dhcpSections = list()
    networkSections = list()
    firewallSections = list()
    wirelessSections = list()

    if 'wan' in hostConfig:
        config = {"type": "interface", "name": "wan"}

        options = dict()
        options['ifname'] = hostConfig['wan']['interface']
        options['proto'] = "dhcp"

        networkSections.append((config, options))

        config = {"type": "zone"}
        options = {
            "name": "wan",
            "masq": "1",
            "input": "ACCEPT",
            "forward": "REJECT",
            "output": "ACCEPT",
            "network": "wan"
        }
        firewallSections.append((config, options))

    if 'lan' in hostConfig:
        config = {"type": "interface", "name": "lan"}

        options = dict()
        options['type'] = "bridge"

        options['proto'] = 'static'
        options['ipaddr'] = hostConfig['lan']['ipaddr']
        options['netmask'] = hostConfig['lan']['netmask']
        uciutils.setList(options, 'ifname', hostConfig['lan']['interfaces'])

        networkSections.append((config, options))

        if 'dhcp' in hostConfig['lan']:
            dhcp = hostConfig['lan']['dhcp']

            config = {'type': 'dnsmasq'}
            options = {'interface': 'lan'}

            dhcpSections.append((config, options))

            config = {'type': 'dhcp', 'name': 'lan'}
            options = {
                'interface': 'lan',
                'start': dhcp['start'],
                'limit': dhcp['limit'],
                'leasetime': dhcp['leasetime']
            }

            dhcpSections.append((config, options))

    if 'wifi' in hostConfig:
        for dev in hostConfig['wifi']:
            config = {"type": "wifi-device", "name": dev['interface']}
            options = {
                'type': 'auto',
                'channel': dev['channel']
            }

            wirelessSections.append((config, options))

    if 'wifi-interfaces' in hostConfig:
        for iface in hostConfig['wifi-interfaces']:
            config = {"type": "wifi-iface"}
            wirelessSections.append((config, iface))

    setConfig(settings.RESERVED_CHUTE, dhcpSections,
              uci.getSystemPath("dhcp"))
    setConfig(settings.RESERVED_CHUTE, networkSections,
              uci.getSystemPath("network"))
    setConfig(settings.RESERVED_CHUTE, firewallSections,
              uci.getSystemPath("firewall"))
    setConfig(settings.RESERVED_CHUTE, wirelessSections,
              uci.getSystemPath("wireless"))