1
0
mirror of https://github.com/binary-kitchen/doorlockd synced 2024-11-01 06:57:04 +01:00
doorlockd-mirror/doorlockd/daemon/doorlockd.cpp

279 lines
8.3 KiB
C++
Raw Normal View History

2015-05-14 15:33:40 +02:00
#include <csignal>
#include <iostream>
2015-05-11 00:18:22 +02:00
#include <boost/filesystem.hpp>
2015-05-12 17:35:57 +02:00
#include <boost/program_options.hpp>
2015-05-13 16:20:12 +02:00
#include <boost/asio.hpp>
2015-05-11 00:18:22 +02:00
2015-09-22 18:22:15 +02:00
#include <json/json.h>
#include "../lib/logic.h"
2015-05-12 03:49:26 +02:00
#include "config.h"
2015-05-11 00:18:22 +02:00
2015-05-12 17:35:57 +02:00
namespace po = boost::program_options;
namespace ba = boost::asio;
namespace fs = boost::filesystem;
using ba::ip::tcp;
2015-05-12 17:35:57 +02:00
2015-09-23 15:47:52 +02:00
// Info about doorlockd version
const static std::string version =
2015-09-24 21:59:20 +02:00
"doorlockd-" DOORLOCK_VERSION;
2015-09-23 15:47:52 +02:00
const static std::string gitversion =
2015-09-24 21:59:20 +02:00
DOORLOCK_GIT_BRANCH "-" DOORLOCK_GIT_COMMIT_HASH;
2015-09-23 15:47:52 +02:00
// The receive buffer length of the TCP socket
const int constexpr SOCKET_BUFFERLENGTH = 2048;
static Logger &l = Logger::get();
2015-05-11 00:18:22 +02:00
2015-09-22 17:47:42 +02:00
static std::unique_ptr<Logic> logic = nullptr;
static ba::io_service io_service;
2015-05-13 16:20:12 +02:00
2015-09-24 19:26:31 +02:00
static std::mutex mutex;
static std::condition_variable onClientMessage;
static volatile bool run = true;
static void signal_handler(int signum)
2015-05-14 15:33:40 +02:00
{
l((std::string)"Received Signal " + std::to_string(signum),
LogLevel::warning);
2015-05-14 15:33:40 +02:00
io_service.stop();
2015-09-24 19:26:31 +02:00
run = false;
onClientMessage.notify_all();
2015-05-14 15:33:40 +02:00
}
static Response subscribe(tcp::socket &sock)
{
sock.write_some(ba::buffer(logic->getClientMessage().toJson()));
while (run) {
std::unique_lock<std::mutex> lock(mutex);
onClientMessage.wait(lock);
if (run) {
sock.write_some(ba::buffer(logic->getClientMessage().toJson()));
}
};
return Response(Response::Code::Success);
}
static void session(tcp::socket &&sock)
2015-05-11 00:18:22 +02:00
{
ba::ip::address remoteAddress;
2015-09-29 15:03:43 +02:00
unsigned short remotePort = 0;
Response response;
2015-09-22 21:53:27 +02:00
try {
std::vector<char> data;
data.resize(SOCKET_BUFFERLENGTH);
remoteAddress = sock.remote_endpoint().address();
remotePort = sock.remote_endpoint().port();
l("Incoming TCP connection from " + remoteAddress.to_string() + "("
+ std::to_string(remotePort) + ")",
LogLevel::notice);
size_t length = sock.read_some(ba::buffer(data));
// Get Request
const std::string requestString(data.begin(), data.begin()+length);
l(" Parsing request...", LogLevel::info);
Request request = Request::fromString(requestString);
switch (request.command) {
case Request::Command::Lock:
case Request::Command::Unlock:
response = logic->request(request);
break;
case Request::Command::Subscribe:
if (remoteAddress.is_loopback() == false) {
response.code = Response::Code::AccessDenied;
response.message = "Subscriptions are only allowed from localhost";
} else {
response = subscribe(sock);
}
break;
2015-09-22 21:55:31 +02:00
case Request::Command::Unknown:
default:
response.code = Response::Code::UnknownCommand;
response.message = "Received unknown command ";
break;
2015-09-22 21:55:31 +02:00
}
2015-09-24 19:26:31 +02:00
throw response;
}
catch (const Response &err) {
response = err;
}
catch (const std::exception &err) {
response.code = Response::Code::Fail;
response.message = "Exception in session " + remoteAddress.to_string()
+ ":" + std::to_string(remotePort) + " : " + err.what();
}
catch (...) {
response.code = Response::Code::Fail;
response.message = "Unhandled doorlockd error";
}
2015-09-22 18:22:15 +02:00
if (!response) {
l(response.message, LogLevel::warning);
2015-05-12 03:49:26 +02:00
}
if (sock.is_open()) {
boost::system::error_code ec;
sock.write_some(ba::buffer(response.toJson()), ec);
2015-05-12 03:49:26 +02:00
}
l("Closing TCP connection from " + remoteAddress.to_string(), LogLevel::notice);
}
2015-05-12 03:49:26 +02:00
static void server(unsigned short port)
2015-05-12 17:35:57 +02:00
{
l(LogLevel::info, "Starting TCP Server");
2015-05-12 17:35:57 +02:00
const auto endpoint = tcp::endpoint(ba::ip::address::from_string("127.0.0.1"), port);
tcp::acceptor a(io_service, endpoint);
2015-05-12 17:35:57 +02:00
tcp::socket sock(io_service);
2015-05-13 16:20:12 +02:00
std::function<void(void)> accept_connection = [&] () {
a.async_accept(sock,
[&] (boost::system::error_code ec) {
if (ec)
{
return;
}
std::thread(session, std::move(sock)).detach();
accept_connection();
});
};
2015-05-13 16:20:12 +02:00
accept_connection();
2015-05-12 17:35:57 +02:00
io_service.run();
l(LogLevel::info, "Stopped TCP Server");
}
2015-05-12 17:35:57 +02:00
int main(int argc, char** argv)
{
int retval = -1;
2015-05-13 16:20:12 +02:00
short port;
2015-05-12 17:35:57 +02:00
std::chrono::seconds tokenTimeout;
2015-09-22 17:47:42 +02:00
std::string ldapUri;
std::string bindDN;
std::string lockPagePrefix;
fs::path logdir;
2015-09-22 17:47:42 +02:00
std::string logfile;
std::string logfile_scripts;
unsigned int tokenLength;
2015-09-22 17:47:42 +02:00
std::string serDev;
2015-09-23 15:33:27 +02:00
unsigned int baudrate;
2015-05-12 17:35:57 +02:00
try {
unsigned int timeout;
2015-09-23 15:47:52 +02:00
po::options_description desc("doorlockd (" + version + " built on " + gitversion + ")");
2015-05-12 17:35:57 +02:00
desc.add_options()
2015-09-22 17:47:42 +02:00
("help,h",
"print help")
("tokentimeout,t",
po::value<unsigned int>(&timeout)->default_value(DEFAULT_TOKEN_TIMEOUT),
"Token timeout in seconds")
("port,p",
po::value<short>(&port)->default_value(DEFAULT_PORT),
"Port")
("ldap,s",
po::value<std::string>(&ldapUri)->default_value(DEFAULT_LDAP_URI),
"Ldap Server")
("bidndn,b",
po::value<std::string>(&bindDN)->default_value(DEFAULT_BINDDN),
"Bind DN, %s means username")
("web,w",
po::value<std::string>(&lockPagePrefix)->default_value(DEFAULT_WEB_PREFIX),
"Prefix of the webpage")
("tokenLength,t",
po::value<unsigned int>(&tokenLength)->default_value(DEFAULT_TOKEN_LENGTH),
"Token length")
("logdir,l",
po::value<fs::path>(&logdir)->default_value(DEFAULT_LOG_DIR),
2015-09-22 17:47:42 +02:00
"Log file")
("serial,i",
po::value<std::string>(&serDev)->default_value(DEFAULT_SERIAL_DEVICE),
2015-09-23 15:33:27 +02:00
"Serial port")
("baud,r",
po::value<unsigned int>(&baudrate)->default_value((DEFAULT_SERIAL_BAUDRATE)),
"Serial baudrate");
2015-05-12 17:35:57 +02:00
po::variables_map vm;
po::store(po::command_line_parser(argc, argv).options(desc).run(), vm);
if (vm.count("help"))
{
std::cout << desc << std::endl;
retval = 0;
exit(0);
2015-05-12 17:35:57 +02:00
}
po::notify(vm);
2015-09-23 15:58:28 +02:00
tokenTimeout = std::chrono::seconds(timeout);
logfile = (logdir / LOG_FILENAME).string();
logfile_scripts = (logdir / LOG_SCRIPTS_FILENAME).string();
l.setLogFile(logfile);
l.logFile(true);
2015-05-12 17:35:57 +02:00
}
catch(const std::exception &e)
{
std::cerr << "Error: " << e.what() << "\n";
exit(-1);
2015-05-12 17:35:57 +02:00
}
l((std::string)"Hello, this is " + version + " built on " + gitversion,
LogLevel::info);
l(LogLevel::notice, "Starting doorlockd");
2015-05-14 15:33:40 +02:00
signal(SIGINT, signal_handler);
signal(SIGKILL, signal_handler);
signal(SIGTERM, signal_handler);
signal(SIGUSR1, signal_handler);
signal(SIGUSR2, signal_handler);
2015-09-22 17:43:15 +02:00
l(LogLevel::info, "Starting Doorlock Logic");
retval = 0;
2015-05-11 00:18:22 +02:00
try {
2015-09-22 17:47:42 +02:00
logic = std::unique_ptr<Logic>(new Logic(tokenTimeout,
ldapUri,
bindDN,
lockPagePrefix,
tokenLength,
serDev,
2015-09-23 15:33:27 +02:00
baudrate,
logfile_scripts,
2015-09-24 19:26:31 +02:00
onClientMessage));
server(port);
2015-05-11 00:18:22 +02:00
}
catch (const boost::system::system_error &ex) {
l(LogLevel::error, "Fatal error: %s", ex.what());
retval = -1;
}
catch (const std::exception &ex) {
l(LogLevel::error, "Fatal error: %s", ex.what());
retval = -1;
}
2015-09-22 17:43:15 +02:00
catch (...) {
l(LogLevel::error, "Fatal error, shutting down");
2015-05-12 03:49:26 +02:00
retval = -1;
}
2015-09-22 17:48:30 +02:00
if (logic) {
l(LogLevel::info, "Stopping Doorlock Logic");
logic.reset();
}
2015-05-11 00:18:22 +02:00
l(LogLevel::notice, "Doorlockd stopped");
2015-05-12 03:49:26 +02:00
return retval;
2015-05-11 00:18:22 +02:00
}