1 common = require("cjdns/common")
7 --- Return the configuration defaults as a table suitable for JSON output
9 -- Mostly taken from cjdroute --genconf
10 -- @return table with configuration defaults
11 function UCI.defaults()
14 { setuser = "nobody", keepNetAdmin = 1 },
15 { chroot = "/var/run/" },
22 ipTunnel = { outgoingConnections = {}, allowedConnections = {} },
23 interface = { type = "TUNInterface" }
25 interfaces = { UDPInterface = {}, ETHInterface = {} },
26 authorizedPasswords = {},
27 logging = { logTo = "stdout" }
31 --- Return the cjdns configuration as a table suitable for JSON output
33 -- Iterates over cjdns, eth_interface, udp_interface, eth_peer, udp_peer,
34 -- and password sections. Doesn't include IPTunnel related options yet.
35 -- @return table with cjdns configuration
37 local obj = UCI.defaults()
39 local cursor = uci.cursor()
41 local config = cursor:get_all("cjdns", "cjdns")
42 if not config then return obj end
44 obj.ipv6 = config.ipv6
45 obj.publicKey = config.public_key
46 obj.privateKey = config.private_key
48 bind = config.admin_address .. ":" .. config.admin_port,
49 password = config.admin_password }
51 if config.tun_device and string.len(config.tun_device) > 0 then
52 obj.router.interface.tunDevice = config.tun_device
55 for i,section in pairs(obj.security) do
56 if type(section.seccomp) == "number" then
57 obj.security[i].seccomp = tonumber(config.seccomp)
61 cursor:foreach("cjdns", "iptunnel_outgoing", function(outgoing)
62 table.insert(obj.router.ipTunnel.outgoingConnections, outgoing.public_key)
65 cursor:foreach("cjdns", "iptunnel_allowed", function(allowed)
66 entry = { publicKey = allowed.public_key }
68 entry["ip4Address"] = allowed.ipv4
71 entry["ip6Address"] = allowed.ipv6
73 table.insert(obj.router.ipTunnel.allowedConnections, entry)
76 cursor:foreach("cjdns", "eth_interface", function(eth_interface)
77 table.insert(obj.interfaces.ETHInterface, {
78 bind = eth_interface.bind,
79 beacon = tonumber(eth_interface.beacon),
84 cursor:foreach("cjdns", "udp_interface", function(udp_interface)
85 table.insert(obj.interfaces.UDPInterface, {
86 bind = udp_interface.address .. ":" .. udp_interface.port,
91 cursor:foreach("cjdns", "eth_peer", function(eth_peer)
92 if not eth_peer.address == "" then
93 local i = tonumber(eth_peer.interface)
94 obj.interfaces.ETHInterface[i].connectTo[eth_peer.address] = {
95 publicKey = eth_peer.public_key,
96 password = eth_peer.password
101 cursor:foreach("cjdns", "udp_peer", function(udp_peer)
102 local bind = udp_peer.address .. ":" .. udp_peer.port
103 local i = tonumber(udp_peer.interface)
104 obj.interfaces.UDPInterface[i].connectTo[bind] = {
105 user = udp_peer.user,
106 publicKey = udp_peer.public_key,
107 password = udp_peer.password
111 cursor:foreach("cjdns", "password", function(password)
112 table.insert(obj.authorizedPasswords, {
113 password = password.password,
114 user = password.user,
115 contact = password.contact
122 --- Parse and save updated configuration from JSON input
124 -- Transforms general settings, ETHInterface, UDPInterface, connectTo, and
125 -- authorizedPasswords fields into UCI sections, and replaces the UCI config's
126 -- contents with them.
127 -- @param table JSON input
128 -- @return Boolean whether saving succeeded
129 function UCI.set(obj)
130 local cursor = uci.cursor()
132 for i, section in pairs(cursor:get_all("cjdns")) do
133 cursor:delete("cjdns", section[".name"])
136 local admin_address, admin_port = string.match(obj.admin.bind, "^(.*):(.*)$")
137 UCI.cursor_section(cursor, "cjdns", "cjdns", "cjdns", {
139 public_key = obj.publicKey,
140 private_key = obj.privateKey,
141 admin_password = obj.admin.password,
142 admin_address = admin_address,
143 admin_port = admin_port
146 if obj.router.interface.tunDevice then
147 UCI.cursor_section(cursor, "cjdns", "cjdns", "cjdns", {
148 tun_device = tostring(obj.router.interface.tunDevice)
153 for i,section in pairs(obj.security) do
154 for key,value in pairs(section) do
155 if key == "seccomp" then
156 UCI.cursor_section(cursor, "cjdns", "cjdns", "cjdns", {
157 seccomp = tonumber(value)
164 if obj.router.ipTunnel.outgoingConnections then
165 for i,public_key in pairs(obj.router.ipTunnel.outgoingConnections) do
166 UCI.cursor_section(cursor, "cjdns", "iptunnel_outgoing", nil, {
167 public_key = public_key
172 if obj.router.ipTunnel.allowedConnections then
173 for i,allowed in pairs(obj.router.ipTunnel.allowedConnections) do
174 entry = { public_key = allowed.publicKey }
175 if allowed.ip4Address then
176 entry["ipv4"] = allowed.ip4Address
178 if allowed.ip6Address then
179 entry["ipv6"] = allowed.ip6Address
182 UCI.cursor_section(cursor, "cjdns", "iptunnel_allowed", nil, entry)
186 if obj.interfaces.ETHInterface then
187 for i,interface in pairs(obj.interfaces.ETHInterface) do
188 UCI.cursor_section(cursor, "cjdns", "eth_interface", nil, {
189 bind = interface.bind,
190 beacon = tostring(interface.beacon)
193 if interface.connectTo then
194 for peer_address,peer in pairs(interface.connectTo) do
195 UCI.cursor_section(cursor, "cjdns", "eth_peer", nil, {
197 address = peer_address,
198 public_key = peer.publicKey,
199 password = peer.password
206 if obj.interfaces.UDPInterface then
207 for i,interface in pairs(obj.interfaces.UDPInterface) do
208 local address, port = string.match(interface.bind, "^(.*):(.*)$")
209 UCI.cursor_section(cursor, "cjdns", "udp_interface", nil, {
214 if interface.connectTo then
215 for peer_bind,peer in pairs(interface.connectTo) do
216 local peer_address, peer_port = string.match(peer_bind, "^(.*):(.*)$")
217 UCI.cursor_section(cursor, "cjdns", "udp_peer", nil, {
219 address = peer_address,
222 public_key = peer.publicKey,
223 password = peer.password
230 if obj.authorizedPasswords then
231 for i,password in pairs(obj.authorizedPasswords) do
232 local user = password.user
233 if not user or string.len(user) == 0 then
234 user = "user-" .. UCI.random_string(6)
237 UCI.cursor_section(cursor, "cjdns", "password", nil, {
238 password = password.password,
240 contact = password.contact
245 return cursor:save("cjdns")
248 --- Simple backport of Cursor:section from luci.model.uci
250 -- Backport reason: we don't wanna depend on LuCI.
251 -- @param Cursor the UCI cursor to operate on
252 -- @param string name of the config
253 -- @param string type of the section
254 -- @param string name of the section (optional)
255 -- @param table config values
256 function UCI.cursor_section(cursor, config, type, section, values)
258 cursor:set(config, section, type)
260 section = cursor:add("cjdns", type)
263 for k,v in pairs(values) do
264 cursor:set(config, section, k, v)
268 function UCI.makeInterface()
269 local cursor = uci.cursor()
271 local config = cursor:get_all("cjdns", "cjdns")
272 if not config then return nil end
274 return common.AdminInterface.new({
275 host = config.admin_address,
276 port = config.admin_port,
277 password = config.admin_password,
283 function UCI.random_string(length)
284 -- tr -cd 'A-Za-z0-9' < /dev/urandom
285 local urandom = io.popen("tr -cd 'A-Za-z0-9' 2> /dev/null < /dev/urandom", "r")
286 local string = urandom:read(length)