2 LuCI - Lua Development Framework
4 Copyright 2009 Steven Barth <steven@midlink.org>
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
10 http://www.apache.org/licenses/LICENSE-2.0
15 local os = require "os"
16 local fs = require "nixio.fs"
17 local nixio = require "nixio"
18 local lucid = require "luci.lucid"
20 local ipairs, type, require, setmetatable = ipairs, type, require, setmetatable
21 local pairs, print, tostring, unpack = pairs, print, tostring, unpack
24 module "luci.lucid.tcpserver"
26 local cursor = lucid.cursor
27 local UCINAME = lucid.UCINAME
32 function prepare_daemon(config, server)
33 nixio.syslog("info", "Preparing TCP-Daemon " .. config[".name"])
34 if type(config.address) ~= "table" then
35 config.address = {config.address}
38 local sockets, socket, code, err = {}
39 local sopts = {reuseaddr = 1}
40 for _, addr in ipairs(config.address) do
41 local host, port = addr:match("(.-):?([^:]*)")
43 nixio.syslog("err", "Invalid address: " .. addr)
44 return nil, -5, "invalid address format"
45 elseif #host == 0 then
48 socket, code, err = prepare_socket(config.family, host, port, sopts)
50 sockets[#sockets+1] = socket
54 nixio.syslog("info", "Sockets bound for " .. config[".name"])
57 return nil, -6, "no sockets bound"
60 nixio.syslog("info", "Preparing publishers for " .. config[".name"])
63 for k, pname in ipairs(config.publisher) do
64 local pdata = cursor:get_all(UCINAME, pname)
66 publisher[#publisher+1] = pdata
68 nixio.syslog("err", "Publisher " .. pname .. " not found")
72 nixio.syslog("info", "Preparing TLS for " .. config[".name"])
74 local tls = prepare_tls(config.tls)
75 if not tls and config.encryption == "enable" then
76 for _, s in ipairs(sockets) do
79 return nil, -4, "Encryption requested, but no TLS context given"
82 nixio.syslog("info", "Invoking daemon factory for " .. config[".name"])
83 local handler, err = config.slave.module.factory(publisher, config)
85 for _, s in ipairs(sockets) do
90 local pollin = nixio.poll_flags("in")
91 for _, s in ipairs(sockets) do
92 server.register_pollfd({
99 publisher = publisher,
107 function accept(polle)
108 local socket, host, port = polle.fd:accept()
110 return nixio.syslog("warn", "accept() failed: " .. port)
113 socket:setblocking(true)
115 local function thread()
116 lucid.close_pollfds()
117 local inst = setmetatable({
118 host = host, port = port, interfaces = lucid.get_interfaces()
119 }, {__index = polle})
120 if polle.config.encryption then
121 socket = polle.tls:create(socket)
122 if not socket:accept() then
124 return nixio.syslog("warning", "TLS handshake failed: " .. host)
128 return polle.accept(socket, inst)
131 local stat = {lucid.create_process(thread)}
136 function prepare_socket(family, host, port, opts, backlog)
137 nixio.syslog("info", "Preparing socket for port " .. port)
138 backlog = backlog or 1024
139 family = family or "inetany"
142 local inetany = family == "inetany"
143 family = inetany and "inet6" or family
145 local socket, code, err = nixio.socket(family, "stream")
146 if not socket and inetany then
148 socket, code, err = nixio.socket(family, "stream")
152 return nil, code, err
155 for k, v in pairs(opts) do
156 socket:setsockopt("socket", k, v)
159 local stat, code, err = socket:bind(host, port)
161 return nil, code, err
164 stat, code, err = socket:listen(backlog)
166 return nil, code, err
169 socket:setblocking(false)
171 return socket, family
174 function prepare_tls(tlskey)
176 if tlskey and cursor:get(UCINAME, tlskey) then
177 tls = nixio.tls("server")
179 local make = cursor:get(UCINAME, tlskey, "generate") == "1"
180 local key = cursor:get(UCINAME, tlskey, "key")
181 local xtype = make and "asn1" or cursor:get(UCINAME, tlskey, "type")
182 local cert = cursor:get(UCINAME, tlskey, "cert")
183 local ciphers = cursor:get(UCINAME, tlskey, "ciphers")
185 if make and (not fs.access(key) or not fs.access(cert)) then
186 local CN = cursor:get(UCINAME, tlskey, "CN")
187 local O = cursor:get(UCINAME, tlskey, "O")
191 CN = CN or nixio.uname().nodename,
192 O = not O and "LuCId Keymaster" or #O > 0 and O
195 local stat, px5g = pcall(require, "px5g")
197 return nixio.syslog("err", "Unable to load PX5G Keymaster")
200 nixio.syslog("info", "PX5G: Generating " .. bits .. "b private key")
201 local rk = px5g.genkey(bits)
202 local keyfile = nixio.open(key, "w", 600)
203 if not rk or not keyfile or not keyfile:writeall(rk:asn1()) then
204 return nixio.syslog("err", "Unable to generate private key")
208 nixio.syslog("info", "PX5G: Generating self-signed certificate")
209 if not fs.writefile(cert, rk:create_selfsigned(data,
210 os.time(), os.time() + 3600 * 24 * 366 * 15)) then
211 return nixio.syslog("err", "Unable to generate certificate")
216 if not tls:set_cert(cert, xtype) then
217 nixio.syslog("err", "Unable to load certificate: " .. cert)
221 if not tls:set_key(key, xtype) then
222 nixio.syslog("err", "Unable to load private key: " .. key)
227 if type(ciphers) == "table" then
228 ciphers = table.concat(ciphers, ":")
230 tls:set_ciphers(ciphers)