1
0
mirror of https://github.com/binary-kitchen/doorlockd synced 2024-12-23 02:34:27 +01:00
doorlockd-mirror/doorlockd/lib/logic.cpp

261 lines
6.7 KiB
C++
Raw Normal View History

2015-05-12 17:35:57 +02:00
#include <chrono>
2015-09-24 18:57:32 +02:00
#include <functional>
2015-05-11 00:18:22 +02:00
#include <cstdlib>
#include <json/json.h>
2015-05-11 20:40:26 +02:00
#define LDAP_DEPRECATED 1
#include <ldap.h>
2015-05-12 17:35:57 +02:00
#include <errno.h>
2015-05-11 00:18:22 +02:00
#include "util.h"
#include "logic.h"
using namespace std;
2015-05-13 16:40:30 +02:00
Logic::Logic(const chrono::seconds tokenTimeout,
2015-05-24 19:15:47 +02:00
const string &ldapUri,
2015-05-13 16:40:30 +02:00
const string &bindDN,
const string &webPrefix,
const string &serDev,
2015-09-23 15:33:27 +02:00
const unsigned int baudrate,
2015-09-24 18:15:05 +02:00
condition_variable &onClientUpdate) :
2015-05-11 00:18:22 +02:00
_logger(Logger::get()),
2015-09-23 15:33:27 +02:00
_door(serDev, baudrate),
2015-05-13 16:40:30 +02:00
_tokenTimeout(tokenTimeout),
2015-09-24 18:15:05 +02:00
_onClientUpdate(onClientUpdate),
2015-05-24 19:15:47 +02:00
_ldapUri(ldapUri),
2015-05-13 16:40:30 +02:00
_bindDN(bindDN),
2015-09-22 18:22:15 +02:00
_webPrefix(webPrefix)
2015-05-11 00:18:22 +02:00
{
srand(time(NULL));
2015-05-12 17:35:57 +02:00
_createNewToken(false);
2015-09-24 18:57:32 +02:00
_door.setDoorCallback(std::bind(&Logic::_doorCallback,
this,
std::placeholders::_1));
2015-05-12 17:35:57 +02:00
_tokenUpdater = thread([this] () {
while (_run)
{
unique_lock<mutex> l(_mutex);
2015-05-21 13:35:30 +02:00
_tokenCondition.wait_for(l, _tokenTimeout);
2015-05-12 17:35:57 +02:00
if (_run == false)
{
break;
} else {
_createNewToken(true);
}
}
});
2015-05-11 00:18:22 +02:00
}
Logic::~Logic()
{
2015-05-12 17:35:57 +02:00
_run = false;
2015-05-21 13:35:30 +02:00
_tokenCondition.notify_one();
2015-05-12 17:35:57 +02:00
_tokenUpdater.join();
2015-05-11 00:18:22 +02:00
}
Response Logic::parseRequest(const Json::Value &root)
2015-05-11 00:18:22 +02:00
{
2015-05-12 17:35:57 +02:00
unique_lock<mutex> l(_mutex);
2015-05-14 15:33:40 +02:00
_logger(LogLevel::info, "Incoming request...");
Response response;
2015-09-22 21:31:26 +02:00
string command, user, password, ip, token;
2015-05-11 00:18:22 +02:00
try {
2015-09-22 21:31:26 +02:00
command = getJsonOrFail<string>(root, "command");
2015-05-12 01:28:02 +02:00
ip = getJsonOrFail<string>(root, "ip");
user = getJsonOrFail<string>(root, "user");
password = getJsonOrFail<string>(root, "password");
token = getJsonOrFail<string>(root, "token");
2015-05-11 00:18:22 +02:00
}
catch (...)
{
_logger(LogLevel::warning, "Error parsing JSON");
response.code = Response::Code::JsonError;
response.message = "Error parsing JSON";
2015-05-12 01:28:02 +02:00
goto out;
2015-05-11 00:18:22 +02:00
}
2015-09-22 21:31:26 +02:00
_logger(" User : " + user, LogLevel::notice);
_logger(" IP : " + ip, LogLevel::notice);
_logger(" Token : " + token, LogLevel::notice);
2015-05-11 00:18:22 +02:00
if (_checkToken(token) == false)
2015-05-11 00:18:22 +02:00
{
_logger(LogLevel::error, "User provided invalid token");
response.code = Response::Code::InvalidToken;
response.message = "User provided invalid token";
goto out;
}
2015-05-11 00:18:22 +02:00
response = _checkLDAP(user,password);
if (!response)
{
_logger(LogLevel::error, "Ldap error");
goto out;
2015-05-11 00:18:22 +02:00
}
2015-09-22 21:31:26 +02:00
if (command == "lock")
2015-05-11 00:18:22 +02:00
{
response = _lock();
2015-09-22 21:31:26 +02:00
} else if (command == "unlock") {
response = _unlock();
2015-05-11 00:18:22 +02:00
} else {
2015-09-22 21:31:26 +02:00
response.code = Response::Code::UnknownCommand;
response.message = "Unknown Command: " + command;
_logger(response.message, LogLevel::error);
2015-05-11 00:18:22 +02:00
}
2015-05-12 01:28:02 +02:00
out:
return response;
2015-05-11 00:18:22 +02:00
}
Response Logic::_lock()
2015-05-11 00:18:22 +02:00
{
Response response;
if (_door.state() == Door::State::Locked)
2015-05-11 00:18:22 +02:00
{
response.code = Response::Code::AlreadyLocked;
response.message = "Unable to lock: already closed";
_logger(response.message, LogLevel::warning);
} else {
_door.lock();
_createNewToken(false);
response.code = Response::Code::Success;
2015-05-11 00:18:22 +02:00
}
2015-05-12 15:59:04 +02:00
return response;
2015-05-11 00:18:22 +02:00
}
Response Logic::_unlock()
2015-05-11 00:18:22 +02:00
{
Response response;
const auto oldState = _door.state();
_door.unlock();
_createNewToken(false);
if (oldState == Door::State::Unlocked)
{
response.code = Response::Code::AlreadyUnlocked;
response.message = "Unable to unlock: already unlocked";
_logger(response.message, LogLevel::warning);
} else {
response.code = Response::Code::Success;
}
return response;
2015-05-11 00:18:22 +02:00
}
bool Logic::_checkToken(const string &strToken)
{
try {
uint64_t token = toUint64(strToken);
if (token == _curToken || (_prevValid == true && token == _prevToken))
{
_logger(LogLevel::info, "Token check successful");
return true;
}
}
catch (const char* const &ex)
{
_logger(LogLevel::error, "Check Token failed for token \"%s\" (expected %s): %s", strToken.c_str(), toHexString(_curToken).c_str(), ex);
}
return false;
}
Response Logic::_checkLDAP(const string &user, const string &password)
2015-05-11 00:18:22 +02:00
{
2015-05-11 20:40:26 +02:00
constexpr int BUFFERSIZE = 1024;
char buffer[BUFFERSIZE];
Response retval;
2015-05-11 20:40:26 +02:00
int rc = -1;
LDAP* ld = nullptr;
unsigned long version = LDAP_VERSION3;
_logger(LogLevel::notice, "Trying to authenticate as user \"%s\"", user.c_str());
snprintf(buffer, BUFFERSIZE, _bindDN.c_str(), user.c_str());
2015-05-24 19:15:47 +02:00
rc = ldap_initialize(&ld, _ldapUri.c_str());
2015-05-11 20:40:26 +02:00
if(rc != LDAP_SUCCESS)
{
retval.message = (string)"LDAP initialize error: "
+ ldap_err2string(rc);
retval.code = Response::Code::LDAPInit;
_logger(retval.message, LogLevel::error);
2015-05-11 20:40:26 +02:00
goto out2;
}
rc = ldap_set_option(ld,
LDAP_OPT_PROTOCOL_VERSION,
(void*)&version);
if (rc != LDAP_SUCCESS)
{
retval.code = Response::Code::LDAPInit;
retval.message = "LDAP set version failed";
_logger(retval.message, LogLevel::error);
2015-05-11 20:40:26 +02:00
goto out;
}
rc = ldap_simple_bind_s(ld, buffer, password.c_str());
if (rc != LDAP_SUCCESS)
{
retval = Response::Code::InvalidCredentials;
retval.message = "Credential check for user \"" + user
+ "\" failed: " + ldap_err2string(rc);
_logger(retval.message, LogLevel::error);
2015-05-11 20:40:26 +02:00
goto out;
}
_logger(LogLevel::notice, "user \"%s\" successfully authenticated", user.c_str());
retval = Response::Code::Success;
2015-05-11 20:40:26 +02:00
out:
ldap_unbind(ld);
ld = nullptr;
out2:
return retval;
2015-05-11 00:18:22 +02:00
}
2015-05-12 17:35:57 +02:00
void Logic::_createNewToken(const bool stillValid)
2015-05-11 00:18:22 +02:00
{
// Todo Mutex einführen
2015-05-11 00:18:22 +02:00
_prevToken = _curToken;
_prevValid = stillValid;
_curToken = (((uint64_t)rand())<<32) | ((uint64_t)rand());
ostringstream message;
message << "New Token generated: " << toHexString(_curToken) << " old Token: " << toHexString(_prevToken) << " is " << (_prevValid?"still":"not") << " valid";
_logger(message, LogLevel::info);
2015-09-24 18:15:05 +02:00
_onClientUpdate.notify_all();
}
Clientmessage Logic::getClientMessage()
2015-09-24 18:57:32 +02:00
{
std::lock_guard<std::mutex> l(_mutex);
Clientmessage retval(_webPrefix + toHexString(_curToken),
_doormessage);
2015-09-24 18:57:32 +02:00
// Reset doormessage
_doormessage = Doormessage();
2015-09-24 18:57:32 +02:00
return retval;
2015-09-24 18:57:32 +02:00
}
void Logic::_doorCallback(Doormessage doormessage)
{
2015-09-24 18:57:32 +02:00
std::lock_guard<std::mutex> l(_mutex);
_doormessage = doormessage;
_onClientUpdate.notify_all();
2015-05-11 00:18:22 +02:00
}