1
0
mirror of https://github.com/binary-kitchen/doorlockd synced 2024-11-18 04:59:13 +01:00
doorlockd-mirror/doorlockd/main.cpp

245 lines
7.1 KiB
C++
Raw Normal View History

2015-05-11 00:18:22 +02:00
#include <iostream>
#include <string>
2015-05-13 16:20:12 +02:00
#include <cstdlib>
#include <memory>
#include <utility>
2015-05-14 15:33:40 +02:00
#include <csignal>
2015-05-11 00:18:22 +02:00
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>
2015-05-14 15:33:40 +02:00
#include "daemon.h"
2015-05-12 03:49:26 +02:00
#include "config.h"
2015-05-11 00:18:22 +02:00
#include "logic.h"
#include "util.h"
2015-05-11 00:18:22 +02:00
2015-05-12 17:35:57 +02:00
namespace po = boost::program_options;
2015-05-13 16:20:12 +02:00
using boost::asio::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-23 15:50:26 +02:00
"doorlockd-" DOORLOCKD_VERSION;
2015-09-23 15:47:52 +02:00
const static std::string gitversion =
DOORLOCKD_GIT_BRANCH "-" DOORLOCKD_GIT_COMMIT_HASH;
// The receive buffer length of the TCP socket
const int constexpr SOCKET_BUFFERLENGTH = 2048;
2015-05-11 00:18:22 +02:00
const static Logger &l = Logger::get();
2015-09-22 17:47:42 +02:00
static std::unique_ptr<Logic> logic = nullptr;
static boost::asio::io_service io_service;
2015-05-13 16:20:12 +02:00
static std::condition_variable onTokenUpdate;
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();
}
static void session(tcp::socket &&sock)
2015-05-11 00:18:22 +02:00
{
boost::system::error_code error;
2015-09-22 21:53:27 +02:00
const auto remoteIP = sock.remote_endpoint().address();
std::vector<char> data;
data.resize(SOCKET_BUFFERLENGTH);
2015-05-12 03:49:26 +02:00
2015-09-22 21:53:27 +02:00
l("Incoming TCP connection from " + remoteIP.to_string(), LogLevel::notice);
try {
2015-09-22 17:47:42 +02:00
size_t length = sock.read_some(boost::asio::buffer(data),
error);
if (error == boost::asio::error::eof)
return;
else if (error)
throw boost::system::system_error(error);
2015-09-22 18:22:15 +02:00
const std::string request(data.begin(), data.begin()+length);
Json::Reader reader;
Json::Value root;
Response response;
2015-09-22 21:55:31 +02:00
std::string command;
2015-09-22 18:22:15 +02:00
if (reader.parse(request, root, false))
{
response.message = "Request is no valid JSON";
response.code = Response::Code::JsonError;
l(response.message, LogLevel::warning);
2015-09-22 21:55:31 +02:00
goto out;
}
try {
command = getJsonOrFail<std::string>(root, "command");
}
catch (...)
{
response.code = Response::Code::JsonError;
response.message = "Error parsing JSON";
l(response.message, LogLevel::warning);
goto out;
}
l(" Command: " + command, LogLevel::notice);
if (command == "lock" || command == "unlock") {
response = logic->parseRequest(root);
2015-09-22 18:22:15 +02:00
} else {
2015-09-22 21:55:31 +02:00
response.code = Response::Code::UnknownCommand;
response.message = "Received unknown command " + command;
l(response.message, LogLevel::warning);
2015-09-22 18:22:15 +02:00
}
out:
sock.write_some(boost::asio::buffer(response.toJson()),
2015-09-22 17:47:42 +02:00
error);
if (error == boost::asio::error::eof)
return;
else if (error)
throw boost::system::system_error(error);
2015-05-12 03:49:26 +02:00
}
catch (std::exception& e) {
std::cerr << "Exception in thread: " << e.what() << "\n";
2015-05-12 03:49:26 +02:00
}
}
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(boost::asio::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;
std::string logfile;
std::string serDev;
2015-09-23 15:33:27 +02:00
unsigned int baudrate;
2015-05-12 17:35:57 +02:00
2015-09-23 15:47:52 +02:00
l((std::string)"Hello, this is " + version + " built on " + gitversion,
LogLevel::info);
2015-05-13 16:50:44 +02:00
l(LogLevel::notice, "Starting doorlockd");
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")
("logfile,l",
po::value<std::string>(&logfile)->default_value(DEFAULT_LOG_FILE),
"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;
goto out;
}
po::notify(vm);
2015-09-23 15:58:28 +02:00
tokenTimeout = std::chrono::seconds(timeout);
2015-05-12 17:35:57 +02:00
}
catch(const std::exception &e)
{
std::cerr << "Error: " << e.what() << "\n";
goto out;
}
daemonize("/",
2015-05-14 15:33:40 +02:00
"/dev/null",
logfile,
logfile);
// Resend version string after redirection stdout
l((std::string)"Hello, this is " + version + " built on " + gitversion,
LogLevel::info);
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");
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,
serDev,
2015-09-23 15:33:27 +02:00
baudrate,
onTokenUpdate));
server(port);
2015-05-11 00:18:22 +02:00
}
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-05-13 16:20:12 +02:00
goto out;
2015-05-12 03:49:26 +02:00
}
2015-05-12 17:35:57 +02:00
retval = 0;
2015-05-11 00:18:22 +02:00
2015-05-12 03:49:26 +02:00
out:
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
}