1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153
|
--
-- (C) 2020 - ntop.org
--
local dirs = ntop.getDirs()
local json = require("dkjson")
local os_utils = require "os_utils"
local sys_utils = require "sys_utils"
local ipv4_utils = require "ipv4_utils"
local tz_utils = require "tz_utils"
-- ##############################################
local system_config = {}
-- ##############################################
-- Configuration file
local CONF_FILE = os_utils.fixPath(dirs.workingdir.."/system.config")
local CONF_FILE_EDITED = CONF_FILE..".edited"
local CONF_FILE_RELOAD = CONF_FILE..".reload" -- to force first start
local STOCK_CONF_FILE = "/etc/ntopng/system.config"
-- ##############################################
system_config.readonly = true
-- ##############################################
function system_config:create(args)
local this = args or {key = "base"}
setmetatable(this, self)
self.__index = self
return this
end
-- ##############################################
-- @brief Writes temporary configuration
-- @param fname File name
-- @param config Configuration
-- @return true on success, false otherwise
local function dumpConfig(fname, config)
local f = io.open(fname, "w")
if f and config then
f:write(json.encode(config, {indent = true}))
f:close()
return true
end
return false
end
-- ##############################################
function system_config.configFile()
return CONF_FILE
end
-- ##############################################
function system_config.configChanged()
return ntop.exists(CONF_FILE_EDITED)
end
-- ##############################################
-- Loads temporary configuration
function system_config:_load_config(fname, guess)
local dirs = ntop.getDirs()
local config
local f = io.open(fname, "r")
if f ~= nil then
config = json.decode((f:read "*a"))
if not config then
traceError(TRACE_ERROR,
TRACE_CONSOLE,
"Error while parsing configuration file " .. fname)
end
f:close()
elseif guess then
-- Load stock configuration if it exists
if ntop.exists(STOCK_CONF_FILE) then
traceError(TRACE_WARNING, TRACE_CONSOLE, "Installing stock configuration file " .. STOCK_CONF_FILE)
sys_utils.execShellCmd("mkdir -p " .. dirs.workingdir)
sys_utils.execShellCmd("cp " .. STOCK_CONF_FILE .. " " .. CONF_FILE)
return self:_load_config(fname, false)
else
config = self:_guess_config()
if dumpConfig(CONF_FILE, config) then
traceError(TRACE_NORMAL, TRACE_CONSOLE,
"Cannot open system configuration file " .. fname .. ", populating with defaults")
end
end
end
sys_utils.setRealExec(config.globals and not config.globals.fake_exec)
return config
end
-- ##############################################
function system_config:readable()
self.readonly = true
self.config = self:_load_config(CONF_FILE, true)
self.conf_handler = self:getConfHandler()
end
-- ##############################################
function system_config:editable()
self.readonly = false
if ntop.exists(CONF_FILE_EDITED) then
self.config = self:_load_config(CONF_FILE_EDITED)
else
self.config = self:_load_config(CONF_FILE, true)
end
self.conf_handler = self:getConfHandler()
end
-- ##############################################
function system_config:getConfHandler()
package.path = dirs.installdir .. "/scripts/lua/modules/conf_handlers/?.lua;" .. package.path
local info = ntop.getInfo(false)
local os = info.OS
local config_target = "network_ifaces"
if string.find(os, "Ubuntu 18%.") ~= nil or
string.find(os, "Ubuntu 20%.") ~= nil then
config_target = "netplan"
end
return require(config_target)
end
-- ##############################################
-- Discards the temporary config and loads the persistent one
function system_config:discard()
if not self.readonly and isAdministrator() then
os.remove(CONF_FILE_EDITED)
self.config = self:_load_config(CONF_FILE, true)
else
traceError(TRACE_ERROR, TRACE_CONSOLE, "Unable to discard a readonly configuration.")
end
end
-- ##############################################
function system_config:_apply_operating_mode_settings(config)
if config.globals.operating_mode == "bridging" then
-- We currently force DHCP off on bridge mode
if config.dhcp_server then
config.dhcp_server.enabled = false
end
end
end
-- ##############################################
-- Set the current interface mode. mode is one of:
-- routing
-- bridging
-- single_port_router
--
function system_config:setOperatingMode(mode)
if not self.config.globals.available_modes[mode] then
return false
end
self.config.globals.operating_mode = mode
self:_apply_operating_mode_settings(self.config)
-- Set DHCP on nedge
if self.config.dhcp_server then
self:setDhcpFromLan()
end
return true
end
-- ##############################################
-- Returns the available modes
function system_config:getAvailableModes()
return self.config.globals.available_modes
end
-- ##############################################
-- Returns the current operating mode
-- nf_config and appliance_config override this
function system_config:getOperatingMode()
return self.config.globals.operating_mode
end
-- ##############################################
-- NOTE: do not use this, use the getOperatingMode instead
function system_config:_getConfiguredOperatingMode()
return self.config.globals.operating_mode
end
-- ##############################################
function system_config:getManagementAccessConfig()
local default_config = {bind_to = "lan"}
if self:isBridgeOverVLANTrunkEnabled() then
-- on VLAN trunk mode, there is no LAN interface, so we bind to "any"
return {bind_to = "any"}
end
if not self.config.globals.management_access then
return default_config
else
return self.config.globals.management_access
end
end
function system_config:setManagementAccessConfig(management_access)
self.config.globals.management_access = management_access
end
-- ##############################################
function system_config.isFirstStart()
return ntop.exists(CONF_FILE_RELOAD) or ntop.getPref("ntopng.prefs.nedge_start") ~= "1"
end
function system_config.setFirstStart(first_start)
ntop.setPref("ntopng.prefs.nedge_start", ternary(first_start, "0", "1"))
if not first_start and ntop.exists(CONF_FILE_RELOAD) then
os.remove(CONF_FILE_RELOAD)
end
end
-- ##############################################
-- Gets the date and time configuration
function system_config:getDateTimeConfig()
return self.config.date_time
end
-- Setup the date and time configuration
function system_config:setDateTimeConfig(config)
self.config.date_time = config
end
-- ##############################################
function system_config:getUnusedInterfaces()
local mode = self:getOperatingMode()
if mode then
return self.config.globals.available_modes[mode].interfaces.unused or {}
else
return {}
end
end
-- Get the LAN interface, based on the current operating mode
function system_config:getLanInterface()
local mode = self:getOperatingMode()
if mode == "bridging" then
return self.config.globals.available_modes.bridging.name
else
return self.config.globals.available_modes[mode].interfaces.lan
end
end
-- You should only call this in single port routing mode
function system_config:getWanInterface()
local mode = self:getOperatingMode()
if mode == "single_port_router" then
return self.config.globals.available_modes.single_port_router.interfaces.wan
end
return nil
end
-- Get all the interfaces, along with their roles
function system_config:getAllInterfaces()
local ifaces = {}
for _, iface in pairs(self:getPhysicalLanInterfaces()) do
ifaces[iface] = "lan"
end
for _, iface in pairs(self:getPhysicalWanInterfaces()) do
ifaces[iface] = "wan"
end
for _, iface in pairs(self:getUnusedInterfaces()) do
ifaces[iface] = "unused"
end
return ifaces
end
function system_config:getBridgeInterfaceName()
if self.config.globals.available_modes["bridging"] then
return self.config.globals.available_modes["bridging"].name
end
return nil
end
-- Set all the interfaces roles
function system_config:setLanWanIfaces(lan_ifaces, wan_ifaces)
local mode = self:getOperatingMode()
local unused = self:getAllInterfaces()
for _, iface in pairs(lan_ifaces) do
unused[iface] = nil
end
for _, iface in pairs(wan_ifaces) do
unused[iface] = nil
end
local unused_ifaces = {}
for iface, _ in pairs(unused) do
unused_ifaces[#unused_ifaces + 1] = iface
end
if mode == "bridging" then
self.config.globals.available_modes.bridging.interfaces.lan = lan_ifaces
else
self.config.globals.available_modes[mode].interfaces.lan = lan_ifaces[1]
end
self.config.globals.available_modes[mode].interfaces.wan = wan_ifaces
self.config.globals.available_modes[mode].interfaces.unused = unused_ifaces
end
local function isInterfaceUp(ifname)
local res = sys_utils.execShellCmd("ip link show dev ".. ifname .." | grep ' state UP '")
return not isEmptyString(res)
end
-- returns all the IP addresses associated to one interface
function system_config.getAllInterfaceAddresses(ifname)
local res = sys_utils.execShellCmd("ip addr show ".. ifname .." | grep -Po 'inet \\K[\\d.]+/[\\d]+'")
local rv = {}
if not isEmptyString(res) then
local lines = string.split(res, "\n")
for _, line in ipairs(lines) do
local ip, netmask = ipv4_utils.cidr_2_addr(line)
if (ip ~= nil) and (netmask ~= nil) then
rv[#rv + 1] = {ip=ip, netmask=netmask}
end
end
end
return rv
end
-- returns a single IP address of an interface.
-- Since an interface can have multiple IPs, this returns the first
-- available excluding the recovery IP
function system_config:getInterfaceAddress(ifname)
local recovery_conf = self:getLanRecoveryIpConfig()
local addresses = system_config.getAllInterfaceAddresses(ifname)
for _, addr in ipairs(addresses) do
if addr.ip ~= recovery_conf.ip then
return addr.ip, addr.netmask
end
end
return nil
end
-- Get the LAN address, based on the current operating mode
function system_config:getLocalIpv4Address()
local lan_iface = self:getLanInterface()
local lan_address = self:getInterfaceAddress(lan_iface)
if isEmptyString(lan_address) then
if not self:isBridgeOverVLANTrunkEnabled() then
traceError(TRACE_WARNING, TRACE_CONSOLE, "Cannot get LAN interface " .. lan_iface .. " address")
end
-- This is possibly wrong (e.g. in transparent bridge)
lan_address = self.config.interfaces.configuration[lan_iface].network.ip or "192.168.1.1"
end
return lan_address
end
-- ##############################################
-- Get the physical LAN interfaces, based on the current operating mode
-- nf_config and appliance_config override this
function system_config:getPhysicalLanInterfaces()
local interfaces = {}
return interfaces
end
-- Get the physical WAN interfaces, based on the current operating mode
-- nf_config and appliance_config override this
function system_config:getPhysicalWanInterfaces()
local interfaces = {}
return interfaces
end
-- ##############################################
local function configDiffers(a_conf, b_conf)
return not table.compare(a_conf, b_conf)
end
-- ##############################################
function system_config:_get_changed_sections(new_config, force_all_changes)
local orig_config = self:_load_config(CONF_FILE, false)
local changed = {}
-- Check for new / changed sections
for section in pairs(new_config) do
if force_all_changes or (orig_config[section] == nil) or configDiffers(new_config[section], orig_config[section]) then
changed[section] = 1
end
end
-- Check for removed sections
for section in pairs(orig_config) do
if new_config[section] == nil then
changed[section] = 1
end
end
return changed
end
-- ##############################################
local function isRebootRequired(changed_sections)
-- Always reboot on first start
if system_config.isFirstStart() then
return true
end
local non_reboot_sections = {
dhcp_server = 1,
date_time = 1,
gateways = 1,
static_routes = 1,
routing = 1,
disabled_wans = 1,
shapers = 1,
port_forwarding = 1,
}
for section in pairs(changed_sections) do
if non_reboot_sections[section] == nil then
return true
end
end
return false
end
-- ##############################################
function system_config:needsReboot()
return isRebootRequired(self:_get_changed_sections(self.config))
end
-- ##############################################
local function isSelfRestartRequired(changed_sections)
local self_restart_sections = {
static_routes = 1,
}
for section in pairs(changed_sections) do
if self_restart_sections[section] then
return true
end
end
return false
end
-- ##############################################
function system_config:needsSelfRestart()
return isSelfRestartRequired(self:_get_changed_sections(self.config))
end
-- ##############################################
-- Save the configuration changes as a temporary config
function system_config:save()
if not self.readonly and isAdministrator() then
local orig_config = self:_load_config(CONF_FILE, false)
if configDiffers(self.config, orig_config) then
dumpConfig(CONF_FILE_EDITED, self.config)
else
os.remove(CONF_FILE_EDITED)
end
else
traceError(TRACE_ERROR, TRACE_CONSOLE, "Unable to save a readonly configuration.")
end
end
-- Save the current temporary config as persistent
function system_config:makePermanent(force_write)
if (not self.readonly or force_write) and isAdministrator() then
dumpConfig(CONF_FILE, self.config)
os.remove(CONF_FILE_EDITED)
else
traceError(TRACE_ERROR, TRACE_CONSOLE, "Unable to make a readonly configuration permanent.")
end
end
-- ##############################################
-- This functions handles configuration changes which do not need a reboot
function system_config:_handleChangedCommonSections(changed_sections, is_rebooting)
if changed_sections["date_time"] then
-- drift accounts for the time between the user clicked 'save' and when it actually clicked 'apply'
-- only when it is requested to set a custom date
-- drift calculation must go before timezone/ntp changes as they will change the time making it invalid
local drift
if self.config.date_time.custom_date_set_req then
drift = os.time(os.date("!*t", os.time())) - (self.config.date_time.custom_date_set_req or 0)
self.config.date_time.custom_date_set_req = nil
end
if self.config.date_time.timezone then
local system_timezone = tz_utils.TimeZone()
if self.config.date_time.timezone ~= system_timezone then
sys_utils.execCmd("timedatectl set-timezone "..self.config.date_time.timezone)
ntop.tzset()
end
end
if self.config.date_time.ntp_sync.enabled then
sys_utils.execCmd("timedatectl set-ntp yes")
else
sys_utils.execCmd("timedatectl set-ntp no")
if self.config.date_time.custom_date then
-- do not specify any timezone here as it is safe to take the one set for the system
local custom_epoch = makeTimeStamp(self.config.date_time.custom_date)
if drift then
custom_epoch = custom_epoch + drift
end
-- use a format that timedatectl likes
local timedatectl_fmt = os.date("%y-%m-%d %X", tonumber(custom_epoch))
if timedatectl_fmt then
sys_utils.execCmd('timedatectl set-time "'..timedatectl_fmt..'"')
end
end
self.config.date_time.custom_date = nil
end
end
end
function system_config:_handleChangedSections(changed_sections, is_rebooting)
self:_handleChangedCommonSections(changed_sections, is_rebooting)
end
function system_config:applyChanges()
local changed_sections = self:_get_changed_sections(self.config, system_config.isFirstStart())
local is_rebooting = isRebootRequired(changed_sections)
local is_self_restarting = isSelfRestartRequired(changed_sections)
self:_handleChangedSections(changed_sections, is_rebooting)
self:makePermanent()
if is_rebooting then
self:writeSystemFiles()
sys_utils.rebootSystem()
elseif is_self_restarting then
sys_utils.restartSelf()
end
end
-- ##############################################
function system_config:_writeNetworkInterfaceConfig(f, iface, network_conf, bridge_ifaces)
local dns_config = self:getDnsConfig()
self.conf_handler.writeNetworkInterfaceConfig(f, iface, network_conf, dns_config, bridge_ifaces)
end
function system_config:_writePassiveModeNetworkConfig(f)
local network_config = self.config.interfaces.configuration
local mode_config = self.config.globals.available_modes["passive"]
local lan_config = network_config[mode_config.interfaces.lan]
-- Lan interface
self:_writeNetworkInterfaceConfig(f, mode_config.interfaces.lan, lan_config.network)
end
function system_config:_writeBridgeModeNetworkConfig(f)
local network_config = self.config.interfaces.configuration
local mode_config = self.config.globals.available_modes["bridging"]
if ntop.isIoTBridge() then
local br_name = mode_config.name
local br_config = network_config[br_name]
package.path = dirs.installdir .. "/scripts/lua/modules/conf_handlers/?.lua;" .. package.path
local wireless = require("wireless")
-- Bridge mode on IoT bridge, setting up Wireless
if (self.config.wireless.enabled) then
-- WiFi Access Point (creates a br0)
wireless.configureWiFiAccessPoint(f,
self.config.wireless.ssid,
self.config.wireless.passphrase,
br_config.network)
else
wireless.disableWiFi()
end
-- Bridge interface
self:_writeNetworkInterfaceConfig(f, br_name, br_config.network, bridge_ifaces)
else
local bridge_ifaces = {}
-- Lan interfaces
for _, iface in ipairs(mode_config.interfaces.lan) do
self:_writeNetworkInterfaceConfig(f, iface, { mode="manual" })
bridge_ifaces[#bridge_ifaces + 1] = iface
end
-- Wan interfaces
for _, iface in ipairs(mode_config.interfaces.wan) do
self:_writeNetworkInterfaceConfig(f, iface, { mode="manual" })
bridge_ifaces[#bridge_ifaces + 1] = iface
end
-- Bridge interface
local br_name = mode_config.name
local br_config = network_config[br_name]
self:_writeNetworkInterfaceConfig(f, br_name, br_config.network, bridge_ifaces)
end
end
function system_config:_writeRoutingModeNetworkConfig(f)
local network_config = self.config.interfaces.configuration
local mode_config = self.config.globals.available_modes["routing"].interfaces
-- Lan interface
self:_writeNetworkInterfaceConfig(f, mode_config.lan, network_config[mode_config.lan].network)
-- Wan interfaces
for _, iface in ipairs(mode_config.wan) do
self:_writeNetworkInterfaceConfig(f, iface, network_config[iface].network)
end
end
function system_config:_writeSinglePortModeInterfaces(f)
local network_config = self.config.interfaces.configuration
local mode_config = self.config.globals.available_modes["single_port_router"]
local lan_iface = self:getLanInterface()
local wan_iface = self:getWanInterface()
-- Lan interface
self:_writeNetworkInterfaceConfig(f, lan_iface, network_config[lan_iface].network)
-- Wan interface
self:_writeNetworkInterfaceConfig(f, wan_iface, network_config[wan_iface].network)
end
function system_config:_getRecoveryInterface()
return self:getLanInterface() .. ":2"
end
function system_config:_writeNetworkInterfaces()
local mode = self:getOperatingMode()
local f = self.conf_handler.openNetworkInterfacesConfigFile()
local recovery_conf = self:getLanRecoveryIpConfig()
local recovery_iface = self:_getRecoveryInterface()
local is_configured, fnames = self.conf_handler.isConfiguredInterface("lo")
if not is_configured then
self:_writeNetworkInterfaceConfig(f, "lo", {mode="loopback"})
end
-- Configure interfaces depending on working mode
if mode == "passive" then
self:_writePassiveModeNetworkConfig(f)
elseif mode == "bridging" then
self:_writeBridgeModeNetworkConfig(f)
elseif mode == "routing" then
self:_writeRoutingModeNetworkConfig(f)
elseif mode == "single_port_router" then
self:_writeSinglePortModeInterfaces(f)
end
-- Configure backup interface
self:_writeNetworkInterfaceConfig(f, recovery_iface, { mode="static", ip=recovery_conf.ip, netmask=recovery_conf.netmask })
self.conf_handler.closeNetworkInterfacesConfigFile(f)
end
function system_config:writeSystemFiles()
if system_config.isFirstStart() then
self:verifyNetworkInterfaces()
end
self:_writeNetworkInterfaces()
system_config.setFirstStart(false)
end
-- ##############################################
function system_config:isBridgeOverVLANTrunkEnabled()
local mode = self:getOperatingMode()
if mode == "bridging" then
local bridge = self.config.globals.available_modes.bridging.name
if self:getInterfaceMode(bridge) == "vlan_trunk" then
return true
end
end
return false
end
-- ##############################################
function system_config.gatewayGetInterface(gateway)
-- Useful to find the interface which would route traffic to some address
local res = sys_utils.execShellCmd("ip route get " .. gateway)
if not isEmptyString(res) then
return res:gmatch(" dev ([^ ]*)")()
end
end
-- TODO use more reliable information
function system_config.ifaceGetNetwork(iface)
local res = sys_utils.execShellCmd("ip route show | grep \"scope link\" | grep \"proto kernel\" | grep \"" .. iface .. "\"")
if not isEmptyString(res) then
return split(res, " ")[1]
end
end
-- ##############################################
function system_config:getStaticLanNetwork()
local lan_iface = self:getLanInterface()
local lan_config = self.config.interfaces.configuration[lan_iface].network
local lan_network = ipv4_utils.addressToNetwork(lan_config.ip, lan_config.netmask)
return {
iface = lan_iface,
network = lan_network,
cidr = lan_network,
ip = lan_config.ip,
netmask = lan_config.netmask,
}
end
-- Returns true if the interface is currently up and running
local function isInterfaceLinkUp(ifname)
local opstatus = sys_utils.execShellCmd("cat /sys/class/net/" .. ifname .. "/operstate 2>/dev/null")
return starts(opstatus, "up")
end
-- ##############################################
function system_config:getDisabledWans(check_wans_with_linkdown)
if check_wans_with_linkdown == nil then check_wans_with_linkdown = false end
-- table.clone needed to modify some parameters while keeping the original unchanged
local disabled = table.clone(self.config.disabled_wans)
if check_wans_with_linkdown then
local roles = self:getAllInterfaces()
for iface, role in pairs(roles) do
if role == "wan" then
if (disabled[iface] ~= true) and (not isInterfaceLinkUp(iface)) then
disabled[iface] = true
end
end
end
end
return disabled
end
function system_config:setDisabledWans(disabled_wans)
self.config.disabled_wans = disabled_wans
end
-- ##############################################
function system_config:_checkDisabledInterfaces()
local ifaces = self:getAllInterfaces()
local disabled_wans = self.config.disabled_wans
for iface, role in pairs(ifaces) do
if role == "wan" then
local is_disabled = (disabled_wans[iface] == true)
local currently_disabled = not isInterfaceUp(iface)
if is_disabled ~= currently_disabled then
if is_disabled then
traceError(TRACE_NORMAL, TRACE_CONSOLE, "Disable interface " .. iface)
sys_utils.execCmd("ip link set dev " .. iface .. " down")
else
traceError(TRACE_NORMAL, TRACE_CONSOLE, "Enable interface " .. iface)
sys_utils.execCmd("ip link set dev " .. iface .. " up")
end
end
end
end
end
-- ##############################################
function system_config:getInterfacesConfiguration()
return self.config.interfaces.configuration or {}
end
function system_config:setInterfacesConfiguration(config)
self.config.interfaces.configuration = config
end
-- ##############################################
function system_config:setInterfaceMode(iface, mode)
local net_config = self.config.interfaces.configuration[iface]
if net_config ~= nil then
net_config.network.mode = mode
if mode == "static" and (isEmptyString(net_config.network.ip) or isEmptyString(net_config.network.netmask)) then
net_config.network.ip = "192.168.1.1"
net_config.network.netmask = "255.255.255.0"
end
return true
end
return false
end
function system_config:getInterfaceMode(iface)
local net_config = self.config.interfaces.configuration[iface]
if net_config ~= nil and net_config.network then
return net_config.network.mode
end
end
-- ##############################################
function system_config:_enableDisableDhcpService()
if self:isDhcpServerEnabled() then
sys_utils.enableService("isc-dhcp-server")
else
sys_utils.disableService("isc-dhcp-server")
end
end
-- ##############################################
function system_config.findDnsPreset(preset_name)
require("prefs_utils")
for _, preset in pairs(DNS_PRESETS) do
if preset.id == preset_name then
return preset
end
end
return nil
end
function system_config:_get_default_global_dns_preset()
return system_config.findDnsPreset("google")
end
-- ##############################################
function system_config:getDnsConfig()
return self.config.globals.dns
end
function system_config:setDnsConfig(config)
self.config.globals.dns = config
end
-- ##############################################
function system_config:isGlobalDnsForgingEnabled()
return self.config.globals.dns.forge_global
end
function system_config:setGlobalDnsForgingEnabled(enabled)
self.config.globals.dns.forge_global = ternary(enabled, true, false)
end
-- ##############################################
function system_config:getLanRecoveryIpConfig()
return self.config.globals.lan_recovery_ip
end
function system_config:setLanRecoveryIpConfig(config)
self.config.globals.lan_recovery_ip = config
end
-- ##############################################
-- NOTE: can't rely on the main routing table when having multiple gateways!
function system_config._interface_get_default_gateway(iface)
local res = sys_utils.execShellCmd("ip route show | grep \"^default via\" | grep \"" .. iface .. "\"")
if not isEmptyString(res) then
return split(res, " ")[3]
end
end
-- ##############################################
local allowed_interfaces
-- @brief Use the logic in ntopng to list available interfaces
-- and avoid interface name with characters that could
-- lead to injections
local function allowedDevName(devname)
if isEmptyString(devname) then
return false
end
-- Do some caching
if not allowed_interfaces then
allowed_interfaces = ntop.listInterfaces()
end
-- Interface is allowed if it appears in the list retrieved from C
return allowed_interfaces[devname]
end
-- ##############################################
function system_config._split_dev_names(c, delimiter)
local ret = {}
local cmd = sys_utils.execShellCmd(c)
if((cmd ~= "") and (cmd ~= nil)) then
local devs = split(cmd, "\n")
if(delimiter == nil) then
local rv = {}
for idx, dev in pairs(devs) do
if not isEmptyString(dev) and allowedDevName(dev) then
rv[#rv + 1] = dev
end
end
return rv
end
for _,a in pairs(devs) do
local p = split(a, delimiter)
if(p ~= nil) then
local name = p[1]
local addr = p[2]
if(addr and name) then
name = trimSpace(name)
addr = trimSpace(addr)
if allowedDevName(name) then
ret[name] = addr:gsub("%s+", "")
end
end
end
end
end
return ret
end
-- ##############################################
-- Check TUN/dummy/lo interface
function system_config.is_virtual_interface(name)
return (name == "lo" or
starts(name, "dummy") or
ntop.exists('/sys/class/net/'.. name ..'/tun_flags') or
string.contains(name, ":"))
end
-- ##############################################
-- Find all interfaces
function system_config.get_all_interfaces()
local devs = system_config._split_dev_names('cat /proc/net/dev', ":")
return devs
end
-- ##############################################
-- Find all bridge interfaces
function system_config.get_bridge_interfaces()
local devs = system_config.get_all_interfaces()
local bridge_devs = {}
for name, _ in pairsByKeys(devs) do
if not system_config.is_virtual_interface(name) then
if(ntop.exists('/sys/class/net/'.. name ..'/bridge')) then
local devs = system_config._split_dev_names('ls /sys/class/net/'.. name ..'/brif/', nil)
bridge_devs[name] = { ["ports"] = devs }
end
end
end
return bridge_devs
end
-- ##############################################
-- Find WiFi interfaces
function system_config.get_wifi_interfaces()
local wifi_devs = system_config._split_dev_names('cat /proc/net/wireless', ":")
-- Necessary for inactive wifi devices
local devs = system_config.get_all_interfaces()
for dev in pairs(devs) do
local rv = sys_utils.execShellCmd("iwconfig 2>/dev/null | grep \"" .. dev .. "\" | grep \"IEEE 802.11\"")
if not isEmptyString(rv) then
wifi_devs[dev] = {}
end
end
return wifi_devs
end
-- ##############################################
-- Find interfaces with IP configured
function system_config.get_ip_interfaces()
local ip_devs = system_config._split_dev_names('ip addr | awk \'$1 == "inet" && $7 { split( $2, addr, "/" ); print $7, addr[1] }\'', " ")
return ip_devs
end
-- ##############################################
-- Find DHCP interface
function system_config.get_dhcp_interfaces()
local dhcp_ifaces = {}
local res = sys_utils.execShellCmd('ps aux | grep dhclient')
if not isEmptyString(res) then
local devs = system_config.get_all_interfaces()
for _, line in pairs(split(res, "\n")) do
local name = line:gmatch(".([^.]+).leases")()
if devs[name] ~= nil then
dhcp_ifaces[name] = {}
end
end
end
return dhcp_ifaces
end
-- ##############################################
-- Find the wan interface reading the default gw
function system_config.get_default_gw_interface()
local dev = sys_utils.execShellCmd("ip route show | grep \"default via\" | awk '{printf \"%s\", $5}'")
if not isEmptyString(dev) then
return dev
end
return nil
end
-- ##############################################
-- nf_config and appliance_config override this
function system_config:_guess_config()
local config = {}
return config
end
-- ##############################################
-- Verify that we are the only to manage the network interfaces
function system_config:verifyNetworkInterfaces()
local lan_ifaces = self:getPhysicalLanInterfaces()
local wan_ifaces = self:getPhysicalWanInterfaces()
local lan_iface = self:getLanInterface()
local ifaces = {[lan_iface] = 1}
for _, iface in pairs(lan_ifaces) do
ifaces[iface] = 1
end
for _, iface in pairs(wan_ifaces) do
ifaces[iface] = 1
end
local has_error = false
local to_backup = {}
for iface in pairs(ifaces) do
local is_configured, conf_files = self.conf_handler.isConfiguredInterface(iface)
if is_configured then
traceError(TRACE_WARNING, TRACE_CONSOLE, "Network interface " .. iface .. " must be managed by ntopng")
has_error = true
if conf_files ~= nil then
to_backup = table.merge(to_backup, conf_files)
end
end
end
if has_error then
self.conf_handler.backupNetworkInterfacesFiles(to_backup)
return true
end
return true
end
-- ##############################################
return system_config
|