diff --git a/CMakeLists.txt b/CMakeLists.txt old mode 100755 new mode 100644 index cc9103f..7bb1ce4 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -29,6 +29,7 @@ door.cpp epaper.cpp logic.cpp util.cpp +daemon.cpp epaper/Display_COG_Process.c epaper/Display_Controller.c epaper/Display_Hardware_Driver.c @@ -38,9 +39,10 @@ epaper/bsp.c add_executable(doorlockd ${SRCS}) target_link_libraries(doorlockd wiringPi jsoncpp ldap ${Boost_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT}) -install(TARGETS doorlockd DESTINATION sbin) -install(FILES img/template.png DESTINATION share/doorlockd) -install(DIRECTORY scripts/ DESTINATION share/doorlockd +install(TARGETS doorlockd DESTINATION sbin/) +install(FILES img/template.png DESTINATION share/doorlockd/) +install(DIRECTORY scripts/ DESTINATION share/doorlockd/ FILES_MATCHING PATTERN "scripts/doorlockd-*" PERMISSIONS WORLD_EXECUTE WORLD_READ OWNER_READ OWNER_EXECUTE GROUP_READ GROUP_EXECUTE OWNER_WRITE) -install(FILES scripts/doorlockd.service DESTINATION /etc/systemd/system) +install(FILES scripts/doorlockd.service DESTINATION /etc/systemd/system/) +install(FILES scripts/doorlockd DESTINATION /etc/sysconfig/) diff --git a/config.h.in b/config.h.in old mode 100755 new mode 100644 index c66fc8d..4ffc0d3 --- a/config.h.in +++ b/config.h.in @@ -14,6 +14,8 @@ #define DEFAULT_WEB_PREFIX "https://lock.binary.kitchen/" #define DEFAULT_LDAP_SERVER "ldaps://ldap.binary.kitchen" #define DEFAULT_BINDDN "cn=%s,ou=Users,dc=binary-kitchen,dc=de" +#define DEFAULT_LOG_FILE "/var/log/doorlockd.log" #define DEFAULT_ALLOWED_IP_PREFIX "172.23.3." +#define DEFAULT_PID_FILE "/var/run/doorlockd.pid" #endif diff --git a/logic.cpp b/logic.cpp old mode 100755 new mode 100644 index 9a8d691..b8cb8ee --- a/logic.cpp +++ b/logic.cpp @@ -56,7 +56,7 @@ Logic::Response Logic::parseRequest(const string &str) { unique_lock l(_mutex); - _logger("Parsing request..."); + _logger(LogLevel::info, "Incoming request..."); Json::Reader reader; Json::Value root; Response retval = Fail; @@ -66,7 +66,7 @@ Logic::Response Logic::parseRequest(const string &str) bool suc = reader.parse(str, root, false); if (!suc) { - _logger(LogLevel::error, "Request ist not valid JSON!"); + _logger(LogLevel::warning, "Request ist not valid JSON!"); retval = NotJson; goto out; } @@ -89,8 +89,10 @@ Logic::Response Logic::parseRequest(const string &str) goto out; } - printf("Action: %s\nAuthenticate: %d\nIP: %s\n",action.c_str(), authenticate, ip.c_str()); - printf("User: %s\nPassword: XXXXXXXXXX\nToken: %s\n",user.c_str(), token.c_str()); + _logger(" Action: " + action, LogLevel::notice); + _logger(" User : " + user, LogLevel::notice); + _logger(" IP : " + ip, LogLevel::notice); + _logger(" Token : " + token, LogLevel::notice); if (authenticate == true) { diff --git a/logic.h b/logic.h old mode 100755 new mode 100644 diff --git a/main.cpp b/main.cpp old mode 100755 new mode 100644 index b975b8b..89fcad2 --- a/main.cpp +++ b/main.cpp @@ -3,10 +3,12 @@ #include #include #include +#include #include #include +#include "daemon.h" #include "config.h" #include "logic.h" @@ -19,6 +21,15 @@ const static Logger &l = Logger::get(); static unique_ptr logic = nullptr; +boost::asio::io_service io_service; + +void signal_handler(int signum) +{ + (void)signum; + io_service.stop(); + logic.reset(); +} + class session : public std::enable_shared_from_this { @@ -96,6 +107,9 @@ int main(int argc, char** argv) string bindDN; string lockPagePrefix; string allowedIpPrefix; + string logfile; + string pidFile; + bool foreground = false; l(LogLevel::notice, "Starting doorlockd"); @@ -106,10 +120,13 @@ int main(int argc, char** argv) ("help,h", "print help") ("tokentimeout,t", po::value(&timeout)->default_value(DEFAULT_TOKEN_TIMEOUT), "Token timeout in seconds") ("port,p", po::value(&port)->default_value(DEFAULT_PORT), "Port") - ("ldap,l", po::value(&ldapServer)->default_value(DEFAULT_LDAP_SERVER), "Ldap Server") + ("ldap,s", po::value(&ldapServer)->default_value(DEFAULT_LDAP_SERVER), "Ldap Server") ("bidndn,b", po::value(&bindDN)->default_value(DEFAULT_BINDDN), "Bind DN, %s means username") ("web,w", po::value(&lockPagePrefix)->default_value(DEFAULT_WEB_PREFIX), "Prefix of the webpage") - ("ip,i", po::value(&allowedIpPrefix)->default_value(DEFAULT_ALLOWED_IP_PREFIX), "Default allowed IP Prefix"); + ("ip,i", po::value(&allowedIpPrefix)->default_value(DEFAULT_ALLOWED_IP_PREFIX), "Default allowed IP Prefix") + ("foreground,f", po::bool_switch(&foreground)->default_value(false), "Run in foreground") + ("logfile,l", po::value(&logfile)->default_value(DEFAULT_LOG_FILE), "Log file") + ("pid,z", po::value(&pidFile)->default_value(DEFAULT_PID_FILE), "PID file"); po::variables_map vm; po::store(po::command_line_parser(argc, argv).options(desc).run(), vm); @@ -131,6 +148,19 @@ int main(int argc, char** argv) goto out; } + daemonize(!foreground, + "/", + "/dev/null", + logfile, + logfile, + pidFile); + + signal(SIGINT, signal_handler); + signal(SIGKILL, signal_handler); + signal(SIGTERM, signal_handler); + signal(SIGUSR1, signal_handler); + signal(SIGUSR2, signal_handler); + logic = unique_ptr(new Logic(tokenTimeout, ldapServer, bindDN, @@ -138,11 +168,8 @@ int main(int argc, char** argv) allowedIpPrefix)); try { - boost::asio::io_service io_service; server s(io_service, port); io_service.run(); - - retval = 0; } catch (const char* const &ex) { ostringstream str;