| 1 | --[[ |
|---|
| 2 | LuCI - Lua Configuration Interface |
|---|
| 3 | |
|---|
| 4 | Copyright 2008 Steven Barth <steven@midlink.org> |
|---|
| 5 | |
|---|
| 6 | Licensed under the Apache License, Version 2.0 (the "License"); |
|---|
| 7 | you may not use this file except in compliance with the License. |
|---|
| 8 | You may obtain a copy of the License at |
|---|
| 9 | |
|---|
| 10 | http://www.apache.org/licenses/LICENSE-2.0 |
|---|
| 11 | |
|---|
| 12 | $Id$ |
|---|
| 13 | ]]-- |
|---|
| 14 | |
|---|
| 15 | require("luci.fs") |
|---|
| 16 | require("luci.ip") |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | if arg[1] then |
|---|
| 20 | mp = Map("olsrd", translate("olsrd_plugins", "OLSR - Plugins")) |
|---|
| 21 | |
|---|
| 22 | p = mp:section(TypedSection, "LoadPlugin") |
|---|
| 23 | p:depends("library", arg[1]) |
|---|
| 24 | p.anonymous = true |
|---|
| 25 | |
|---|
| 26 | ign = p:option(Flag, "ignore", "Enable") |
|---|
| 27 | ign.enabled = "0" |
|---|
| 28 | ign.disabled = "1" |
|---|
| 29 | ign.rmempty = false |
|---|
| 30 | function ign.cfgvalue(self, section) |
|---|
| 31 | return Flag.cfgvalue(self, section) or "0" |
|---|
| 32 | end |
|---|
| 33 | |
|---|
| 34 | lib = p:option(DummyValue, "library", translate("library")) |
|---|
| 35 | lib.default = arg[1] |
|---|
| 36 | |
|---|
| 37 | local function Range(x,y) |
|---|
| 38 | local t = {} |
|---|
| 39 | for i = x, y do t[#t+1] = i end |
|---|
| 40 | return t |
|---|
| 41 | end |
|---|
| 42 | |
|---|
| 43 | local function Cidr2IpMask(val) |
|---|
| 44 | if val then |
|---|
| 45 | for i = 1, #val do |
|---|
| 46 | local cidr = luci.ip.IPv4(val[i]) or luci.ip.IPv6(val[i]) |
|---|
| 47 | if cidr then |
|---|
| 48 | val[i] = cidr:network():string() .. " " .. cidr:mask():string() |
|---|
| 49 | end |
|---|
| 50 | end |
|---|
| 51 | return val |
|---|
| 52 | end |
|---|
| 53 | end |
|---|
| 54 | |
|---|
| 55 | local function IpMask2Cidr(val) |
|---|
| 56 | if val then |
|---|
| 57 | for i = 1, #val do |
|---|
| 58 | local ip, mask = val[i]:gmatch("([^%s+])%s+([^%s+])")() |
|---|
| 59 | local cidr |
|---|
| 60 | if ip and mask and ip:match(":") then |
|---|
| 61 | cidr = luci.ip.IPv6(ip, mask) |
|---|
| 62 | elseif ip and mask then |
|---|
| 63 | cidr = luci.ip.IPv4(ip, mask) |
|---|
| 64 | end |
|---|
| 65 | |
|---|
| 66 | if cidr then |
|---|
| 67 | val[i] = cidr:string() |
|---|
| 68 | end |
|---|
| 69 | end |
|---|
| 70 | return val |
|---|
| 71 | end |
|---|
| 72 | end |
|---|
| 73 | |
|---|
| 74 | |
|---|
| 75 | local knownPlParams = { |
|---|
| 76 | ["olsrd_bmf.so.1.5.3"] = { |
|---|
| 77 | { Value, "BmfInterface", "bmf0" }, |
|---|
| 78 | { Value, "BmfInterfaceIp", "10.10.10.234/24" }, |
|---|
| 79 | { Flag, "DoLocalBroadcast", "no" }, |
|---|
| 80 | { Flag, "CapturePacketsOnOlsrInterfaces", "yes" }, |
|---|
| 81 | { ListValue, "BmfMechanism", { "UnicastPromiscuous", "Broadcast" } }, |
|---|
| 82 | { Value, "BroadcastRetransmitCount", "2" }, |
|---|
| 83 | { Value, "FanOutLimit", "4" }, |
|---|
| 84 | { DynamicList, "NonOlsrIf", "eth1" } |
|---|
| 85 | }, |
|---|
| 86 | |
|---|
| 87 | ["olsrd_dyn_gw.so.0.4"] = { |
|---|
| 88 | { Value, "Interval", "40" }, |
|---|
| 89 | { DynamicList, "Ping", "141.1.1.1" }, |
|---|
| 90 | { DynamicList, "HNA", "192.168.80.0/24", IpMask2Cidr, Cidr2IpMask } |
|---|
| 91 | }, |
|---|
| 92 | |
|---|
| 93 | ["olsrd_httpinfo.so.0.1"] = { |
|---|
| 94 | { Value, "port", "80" }, |
|---|
| 95 | { DynamicList, "Host", "163.24.87.3" }, |
|---|
| 96 | { DynamicList, "Net", "0.0.0.0/0", IpMask2Cidr, Cidr2IpMask } |
|---|
| 97 | }, |
|---|
| 98 | |
|---|
| 99 | ["olsrd_nameservice.so.0.3"] = { |
|---|
| 100 | { DynamicList, "name", "my-name.mesh" }, |
|---|
| 101 | { DynamicList, "hosts", "1.2.3.4 name-for-other-interface.mesh" }, |
|---|
| 102 | { Value, "suffix", ".olsr" }, |
|---|
| 103 | { Value, "hosts_file", "/path/to/hosts_file" }, |
|---|
| 104 | { Value, "add_hosts", "/path/to/file" }, |
|---|
| 105 | { Value, "dns_server", "141.1.1.1" }, |
|---|
| 106 | { Value, "resolv_file", "/path/to/resolv.conf" }, |
|---|
| 107 | { Value, "interval", "120" }, |
|---|
| 108 | { Value, "timeout", "240" }, |
|---|
| 109 | { Value, "lat", "12.123" }, |
|---|
| 110 | { Value, "lon", "12.123" }, |
|---|
| 111 | { Value, "latlon_file", "/var/run/latlon.js" }, |
|---|
| 112 | { Value, "latlon_infile", "/var/run/gps.txt" }, |
|---|
| 113 | { Value, "sighup_pid_file", "/var/run/dnsmasq.pid" }, |
|---|
| 114 | { Value, "name_change_script", "/usr/local/bin/announce_new_hosts.sh" }, |
|---|
| 115 | { Value, "services_change_script", "/usr/local/bin/announce_new_services.sh" } |
|---|
| 116 | }, |
|---|
| 117 | |
|---|
| 118 | ["olsrd_quagga.so.0.2.2"] = { |
|---|
| 119 | { StaticList, "redistribute", { |
|---|
| 120 | "system", "kernel", "connect", "static", "rip", "ripng", "ospf", |
|---|
| 121 | "ospf6", "isis", "bgp", "hsls" |
|---|
| 122 | } }, |
|---|
| 123 | { ListValue, "ExportRoutes", { "only", "both" } }, |
|---|
| 124 | { Flag, "LocalPref", "true" }, |
|---|
| 125 | { Value, "Distance", Range(0,255) } |
|---|
| 126 | }, |
|---|
| 127 | |
|---|
| 128 | ["olsrd_secure.so.0.5"] = { |
|---|
| 129 | { Value, "Keyfile", "/etc/private-olsr.key" } |
|---|
| 130 | }, |
|---|
| 131 | |
|---|
| 132 | ["olsrd_txtinfo.so.0.1"] = { |
|---|
| 133 | { Value, "accept", "10.247.200.4" } |
|---|
| 134 | }, |
|---|
| 135 | |
|---|
| 136 | ["olsrd_arprefresh.so.0.1"] = {}, |
|---|
| 137 | ["olsrd_dot_draw.so.0.3"] = {}, |
|---|
| 138 | ["olsrd_dyn_gw_plain.so.0.4"] = {}, |
|---|
| 139 | ["olsrd_pgraph.so.1.1"] = {}, |
|---|
| 140 | ["olsrd_tas.so.0.1"] = {} |
|---|
| 141 | } |
|---|
| 142 | |
|---|
| 143 | |
|---|
| 144 | -- build plugin options with dependencies |
|---|
| 145 | if knownPlParams[arg[1]] then |
|---|
| 146 | for _, option in ipairs(knownPlParams[arg[1]]) do |
|---|
| 147 | local otype, name, default, uci2cbi, cbi2uci = unpack(option) |
|---|
| 148 | local values |
|---|
| 149 | |
|---|
| 150 | if type(default) == "table" then |
|---|
| 151 | values = default |
|---|
| 152 | default = default[1] |
|---|
| 153 | end |
|---|
| 154 | |
|---|
| 155 | if otype == Flag then |
|---|
| 156 | local bool = p:option( Flag, name ) |
|---|
| 157 | if default == "yes" or default == "no" then |
|---|
| 158 | bool.enabled = "yes" |
|---|
| 159 | bool.disabled = "no" |
|---|
| 160 | elseif default == "on" or default == "off" then |
|---|
| 161 | bool.enabled = "on" |
|---|
| 162 | bool.disabled = "off" |
|---|
| 163 | elseif default == "1" or default == "0" then |
|---|
| 164 | bool.enabled = "1" |
|---|
| 165 | bool.disabled = "0" |
|---|
| 166 | else |
|---|
| 167 | bool.enabled = "true" |
|---|
| 168 | bool.disabled = "false" |
|---|
| 169 | end |
|---|
| 170 | bool.optional = true |
|---|
| 171 | bool.default = default |
|---|
| 172 | bool:depends({ library = plugin }) |
|---|
| 173 | else |
|---|
| 174 | local field = p:option( otype, name ) |
|---|
| 175 | if values then |
|---|
| 176 | for _, value in ipairs(values) do |
|---|
| 177 | field:value( value ) |
|---|
| 178 | end |
|---|
| 179 | end |
|---|
| 180 | if type(uci2cbi) == "function" then |
|---|
| 181 | function field.cfgvalue(self, section) |
|---|
| 182 | return uci2cbi(otype.cfgvalue(self, section)) |
|---|
| 183 | end |
|---|
| 184 | end |
|---|
| 185 | if type(cbi2uci) == "function" then |
|---|
| 186 | function field.formvalue(self, section) |
|---|
| 187 | return cbi2uci(otype.formvalue(self, section)) |
|---|
| 188 | end |
|---|
| 189 | end |
|---|
| 190 | field.optional = true |
|---|
| 191 | field.default = default |
|---|
| 192 | --field:depends({ library = arg[1] }) |
|---|
| 193 | end |
|---|
| 194 | end |
|---|
| 195 | end |
|---|
| 196 | |
|---|
| 197 | return mp |
|---|
| 198 | |
|---|
| 199 | else |
|---|
| 200 | |
|---|
| 201 | mpi = Map("olsrd", "OLSR - Plugins") |
|---|
| 202 | |
|---|
| 203 | local plugins = {} |
|---|
| 204 | mpi.uci:foreach("olsrd", "LoadPlugin", |
|---|
| 205 | function(section) |
|---|
| 206 | if section.library and not plugins[section.library] then |
|---|
| 207 | plugins[section.library] = true |
|---|
| 208 | end |
|---|
| 209 | end |
|---|
| 210 | ) |
|---|
| 211 | |
|---|
| 212 | -- create a loadplugin section for each found plugin |
|---|
| 213 | for k, v in pairs(luci.fs.dir("/usr/lib")) do |
|---|
| 214 | if v:sub(1, 6) == "olsrd_" then |
|---|
| 215 | if not plugins[v] then |
|---|
| 216 | mpi.uci:section( |
|---|
| 217 | "olsrd", "LoadPlugin", nil, |
|---|
| 218 | { library = v, ignore = 1 } |
|---|
| 219 | ) |
|---|
| 220 | end |
|---|
| 221 | end |
|---|
| 222 | end |
|---|
| 223 | |
|---|
| 224 | t = mpi:section( TypedSection, "LoadPlugin", "Plugins" ) |
|---|
| 225 | t.anonymous = true |
|---|
| 226 | t.template = "cbi/tblsection" |
|---|
| 227 | t.override_scheme = true |
|---|
| 228 | function t.extedit(self, section) |
|---|
| 229 | local lib = self.map:get(section, "library") or "" |
|---|
| 230 | return luci.dispatcher.build_url("admin", "services", "olsrd", "plugins") .. "/" .. lib |
|---|
| 231 | end |
|---|
| 232 | |
|---|
| 233 | ign = t:option( Flag, "ignore", "Enabled" ) |
|---|
| 234 | ign.enabled = "0" |
|---|
| 235 | ign.disabled = "1" |
|---|
| 236 | ign.rmempty = false |
|---|
| 237 | function ign.cfgvalue(self, section) |
|---|
| 238 | return Flag.cfgvalue(self, section) or "0" |
|---|
| 239 | end |
|---|
| 240 | |
|---|
| 241 | t:option( DummyValue, "library", "Library" ) |
|---|
| 242 | |
|---|
| 243 | return mpi |
|---|
| 244 | end |
|---|