def wifi_form_cb(data): uci = Uci() wireless = Config("wireless") uci.add(wireless) iface = Section(data['iface_section'], "wifi-iface") wireless.add(iface) device = Section("radio0", "wifi-device") wireless.add(device) # we must toggle both wifi-iface and device iface.add(Option("disabled", not data['wifi_enabled'])) device.add(Option("disabled", not data['wifi_enabled'])) if data['wifi_enabled']: iface.add(Option("ssid", data['ssid'])) iface.add(Option("hidden", data['ssid_hidden'])) iface.add(Option("encryption", "psk2+tkip+aes")) iface.add(Option("key", data['key'])) if data.get('channel2g4'): channel = data['channel2g4'] elif data.get('channel5g'): channel = data['channel5g'] else: logger.critical("Saving form without Wi-Fi channel: %s", data) channel = "auto" hwmode = data.get('hwmode') if hwmode: # change hwmode only if we had the choice device.add(Option("hwmode", hwmode)) device.add(Option("htmode", data['htmode'])) # channel is in wifi-device section device.add(Option("channel", channel)) else: pass # wifi disabled return "edit_config", uci
def pw_form_cb(data): from beaker.crypto import pbkdf2 if self.change: # if changing password, check the old pw is right first uci_data = client.get(filter=filters.foris_config) password_hash = uci_data.find_child("uci.foris.auth.password") # allow changing the password if password_hash is empty if password_hash: password_hash = password_hash.value # crypt automatically extracts salt and iterations from formatted pw hash if password_hash != pbkdf2.crypt(data['old_password'], salt=password_hash): return "save_result", {'wrong_old_password': True} uci = Uci() foris = Config("foris") uci.add(foris) auth = Section("auth", "config") foris.add(auth) # use 48bit pseudo-random salt internally generated by pbkdf2 new_password_hash = pbkdf2.crypt(data['password'], iterations=1000) auth.add(Option("password", new_password_hash)) if data['set_system_pw'] is True: client.set_password("root", data['password']) return "edit_config", uci
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")) return "edit_config", uci
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: # TODO: workaround for Nuci bug #3984 - remove when fixed fakes_section = ucollect_form.nuci_config.find_child("uci.ucollect.fakes") if fakes_section: fakes.add_removal(disable) fakes.add(Option("log_credentials", data['log_credentials'])) return "edit_config", uci
def dns_form_cb(data): uci = Uci() unbound = Config("unbound") uci.add(unbound) server = Section("server", "unbound") unbound.add(server) server.add(Option("forward_upstream", data['forward_upstream'])) return "edit_config", uci
def test_build_option_uci_tree_correct_xml(): uci = Uci() updater = Config("updater") uci.add(updater) override = Section("override", "override") updater.add(override) override.add(Option("disable", True)) built_tree = build_option_uci_tree("updater.override.disable", "override", True) assert ET.tostring(built_tree.get_xml()) == ET.tostring(uci.get_xml())
def region_form_cb(data): uci = Uci() system = Config("system") uci.add(system) system_section = Section(data['system_name'], "system") system.add(system_section) zonename = data['zonename'] system_section.add(Option("zonename", zonename)) system_section.add( Option("timezone", tzinfo.get_zoneinfo_for_tz(zonename))) return "edit_config", uci
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
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
def package_lists_form_cb(data): uci = Uci() updater = Config("updater") uci.add(updater) pkglists = Section("pkglists", "pkglists") updater.add(pkglists) lists = List("lists") # create List with selected packages i = 0 for k, v in data.iteritems(): if v and k.startswith("install_"): lists.add(Value(i, k[8:])) i += 1 if i == 0: pkglists.add_removal(lists) else: pkglists.add_replace(lists) return "edit_config", uci
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
def form_cb(data): agreed = bool(int(data.get("agreed", "0"))) approval_status = data.get(UpdaterAutoUpdatesHandler.APPROVAL_NO, UpdaterAutoUpdatesHandler.APPROVAL_NO) auto_grant_seconds = int(data.get("approval_timeout", 24)) * 60 * 60 uci = Uci() updater = uci.add(Config("updater")) override = updater.add(Section("override", "override")) override.add(Option("disable", not agreed)) approvals = updater.add_replace(Section("approvals", "approvals")) if approval_status == UpdaterAutoUpdatesHandler.APPROVAL_NO: approvals.add(Option("need", "0")) elif approval_status == UpdaterAutoUpdatesHandler.APPROVAL_NEEDED: approvals.add(Option("need", "1")) elif approval_status == UpdaterAutoUpdatesHandler.APPROVAL_TIMEOUT: approvals.add(Option("need", "1")) approvals.add(Option("auto_grant_seconds", auto_grant_seconds)) return "edit_config", uci
def wifi_form_cb(data): uci = Uci() wireless = Config("wireless") uci.add(wireless) guest_wifi_enabled = False for radio in radios: if self._prepare_radio_cb(data, wireless, radio): guest_wifi_enabled = True guest_interfaces = ["guest_turris_%s" % e for e in sorted(radios)] # test whether it is required to pass update guest network current_data = client.get(filter=filters.wifi_filter()) current_enabled = preprocessors.guest_network_enabled(current_data) if guest_wifi_enabled and not current_enabled: # Guest network handling 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 LanHandler.prepare_guest_configs(uci, True, network, prefix, guest_interfaces) elif guest_wifi_enabled: # try to update guest interfaces if the differs stored = current_data.find_child( "uci.network.guest_turris.ifname") if not stored or set( stored.value.split(" ")) != set(guest_interfaces): network_conf = uci.add(Config("network")) interface_section = network_conf.add( Section("guest_turris", "interface")) interface_section.add( Option("ifname", " ".join(guest_interfaces))) return "edit_config", uci
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
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
def dns_form_cb(data): uci = Uci() resolver = Config("resolver") uci.add(resolver) server = Section("common", "resolver") resolver.add(server) server.add(Option("forward_upstream", data['forward_upstream'])) if not contract_valid(): server.add(Option("ignore_root_key", data['ignore_root_key'])) if 'dhcp_from_dns' in data: server.add(Option("dynamic_domains", data['dhcp_from_dns'])) if 'dhcp_dns_domain' in data: dhcp = uci.add(Config("dhcp")) dnsmasq_section = dns_form.nuci_config.find_child( "uci.dhcp.@dnsmasq[0]") dnsmasq = dhcp.add( Section(dnsmasq_section.name, "dnsmasq", anonymous=True)) dnsmasq.add( Option("local", "/%s/" % data["dhcp_dns_domain"].strip("/"))) return "edit_config", uci
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 = "eth2" if data['proto'] == WAN_PPPOE: wan.add(Option("username", data['username'])) wan.add(Option("password", data['password'])) wan.add(Option("ipv6", data['ppp_ipv6'])) 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_string = " ".join([data.get("dns1", ""), data.get("dns2", "")]).strip() wan.add(Option("dns", dns_string)) if data.get("static_ipv6") is True: wan.add(Option("ip6addr", data['ip6addr'])) wan.add(Option("ip6gw", data['ip6gw'])) wan.add(Option("ip6prefix", data['ip6prefix'])) else: wan.add_removal(Option("ip6addr", None)) wan.add_removal(Option("ip6gw", None)) wan.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_ifname = "eth2" eth2 = Section("eth2", "interface") smrtd.add(eth2) eth2.add(Option("name", "eth2")) if use_smrt: if not smrt_vlan: # "proprietary" number - and also a common VLAN ID in CZ smrt_vlan = "848" wan_ifname += ".%s" % smrt_vlan vpi, vci = data.get("smrt_vpi"), data.get("smrt_vci") connections = List("connections") if vpi and vci: eth2.add(connections) connections.add(Value(1, "%s %s %s" % (smrt_vlan, vpi, vci))) elif use_smrt: eth2.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", 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
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