Leosac  0.8.0
Open Source Access Control
kernel.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014-2016 Leosac
3 
4  This file is part of Leosac.
5 
6  Leosac is free software: you can redistribute it and/or modify
7  it under the terms of the GNU Affero General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  Leosac is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU Affero General Public License for more details.
15 
16  You should have received a copy of the GNU Affero General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #include "kernel.hpp"
23 #include "core/auth/Group.hpp"
24 #include "core/auth/User.hpp"
25 #include "core/auth/User_odb.h"
28 #include "core/credentials/RFIDCard_odb.h"
35 #include "tools/GenGuid.h"
36 #include "tools/Mail.hpp"
37 #include "tools/Schedule.hpp"
38 #include "tools/ScheduleMapping_odb.h"
39 #include "tools/Schedule_odb.h"
41 #include "tools/db/PGSQLTracer.hpp"
42 #include "tools/db/database.hpp"
43 #include "tools/log.hpp"
45 #include "tools/scrypt/Random.hpp"
47 #include "tools/signalhandler.hpp"
48 #include "tools/unixfs.hpp"
50 #include <boost/algorithm/string/join.hpp>
51 #include <boost/archive/binary_iarchive.hpp>
52 #include <boost/archive/binary_oarchive.hpp>
53 #include <boost/archive/text_oarchive.hpp>
54 #include <boost/property_tree/ptree_serialization.hpp>
55 #include <fstream>
56 #include <odb/pgsql/database.hxx>
57 #include <odb/sqlite/database.hxx>
58 
59 using boost::property_tree::ptree;
60 using boost::property_tree::ptree_error;
61 using namespace Leosac::Tools;
62 using namespace Leosac;
63 
64 Kernel *Kernel::instance_ = nullptr;
65 
66 Kernel::Kernel(const boost::property_tree::ptree &config, bool strict)
67  : utils_(std::make_shared<CoreUtils>(this, std::make_shared<Scheduler>(this),
68  std::make_shared<ConfigChecker>(), strict))
69  , config_manager_(config)
70  , ctx_()
71  , bus_(ctx_)
72  , control_(ctx_, zmqpp::socket_type::rep)
73  , bus_push_(ctx_, zmqpp::socket_type::push)
74  , is_running_(true)
75  , want_restart_(false)
76  , module_manager_(ctx_, *this)
77  , network_config_(nullptr)
78  , remote_controller_(nullptr)
79  , send_sighup_(false)
80  , autosave_(false)
81  , start_time_(std::chrono::steady_clock::now())
82  , xmlnne_(config_file_path())
83 {
88 
89  if (config.get_child_optional("network"))
90  {
91  network_config_ = std::unique_ptr<NetworkConfig>(
92  new NetworkConfig(*this, config.get_child("network")));
93  }
94  else
95  {
96  network_config_ = std::unique_ptr<NetworkConfig>(
97  new NetworkConfig(*this, boost::property_tree::ptree()));
98  }
99 
100  if (config.get_child_optional("remote"))
101  {
102  remote_controller_ = std::unique_ptr<RemoteControl>(
103  new RemoteControl(ctx_, *this, config.get_child("remote")));
104  }
105 
106  if (auto child = config.get_child_optional("autosave"))
107  {
108  autosave_ = (*child).get<bool>("");
109  }
110 
111  control_.bind("inproc://leosac-kernel");
112  bus_push_.connect("inproc://zmq-bus-pull");
113  network_config_->reload();
114  instance_ = this;
115 }
116 
118 {
119  // Was done automatically by the destructor,
120  // but we need modules to be stopped before unregistering services.
121  // A more elegant workaround would be to register core services
122  // through a RAII object.
125  instance_ = nullptr;
126 }
127 
128 boost::property_tree::ptree Kernel::make_config(const RuntimeOptions &opt)
129 {
130  boost::property_tree::ptree cfg;
131  std::string filename = opt.get_param("kernel-cfg");
132 
133  if (filename.empty())
134  throw CoreException("Invalid command line parameter. No kernel "
135  "configuration file specified.");
136 
137  try
138  {
139  cfg = propertyTreeFromXmlFile(filename);
140  // store the path the config file.
141  cfg.get_child("kernel").add("kernel-cfg", filename);
142  return cfg.get_child("kernel"); // kernel is the root node.
143  }
144  catch (ptree_error &e)
145  {
146  std::throw_with_nested(
147  ConfigException(filename, "Invalid main configuration"));
148  }
149 }
150 
152 {
155 
156  // At this point all module should have properly initialized.
157  bus_push_.send(zmqpp::message() << "KERNEL"
158  << "SYSTEM_READY");
159 
160  reactor_.add(control_, std::bind(&Kernel::handle_control_request, this));
161  if (remote_controller_)
162  reactor_.add(
163  remote_controller_->socket_,
164  std::bind(&RemoteControl::handle_msg, remote_controller_.get()));
165 
166  while (is_running_)
167  {
168  reactor_.poll(25); // this is good enough. May be improved later tho.
169  utils_->scheduler().update(TargetThread::MAIN);
170  if (send_sighup_)
171  {
172  bus_push_.send(zmqpp::message() << "KERNEL"
173  << "SIGHUP");
174  send_sighup_ = false;
175  }
176  }
177 
178  INFO("KERNEL JUST EXITED MAIN LOOP");
179  shutdown();
180 
181  if (autosave_)
182  save_config();
183  return want_restart_;
184 }
185 
187 {
188  try
189  {
190  ptree plugin_dirs =
191  config_manager_.kconfig().get_child("plugin_directories");
192 
193  for (const auto &plugin_dir : plugin_dirs)
194  {
195  std::string pname = plugin_dir.first;
196  std::string pvalue = plugin_dir.second.data();
197 
198  xmlnne_("plugindir", pname);
199  DEBUG("Adding {" << pvalue << "} in library path");
200  module_manager_.addToPath(pvalue);
201  }
202 
203  for (const auto &module : config_manager_.kconfig().get_child("modules"))
204  {
205  std::string pname = module.first;
206  xmlnne_("module", pname);
207 
208  ptree module_conf = module.second;
209  std::string module_file = module_conf.get_child("file").data();
210  std::string module_name = module_conf.get_child("name").data();
211 
212  // we store the conf in our ConfigManager object, the ModuleManager will
213  // use it later.
214  config_manager_.store_config(module_name, module_conf);
215 
216  if (!module_manager_.loadModule(module_name))
217  {
218  std::string search_path;
219 
220  search_path = boost::algorithm::join(
221  module_manager().get_module_path(), "\n\t -> ");
222  throw LEOSACException(
223  "Cannot load modules. Search path was: \n\t -> " + search_path);
224  }
225  }
226  }
227  catch (ptree_error &e)
228  {
229  ERROR("Invalid configuration file: " << e.what());
230  std::throw_with_nested(LEOSACException("Cannot load modules."));
231  }
233 }
234 
236 {
237  zmqpp::message msg;
238  std::string req;
239 
240  control_.receive(msg);
241  msg >> req;
242  INFO("Receive request: " << req);
243 
244  if (req == "RESTART")
245  {
246  is_running_ = false;
247  want_restart_ = true;
248  control_.send("OK");
249  }
250  else if (req == "RESET")
251  {
252  is_running_ = false;
253  want_restart_ = true;
254  factory_reset();
255  }
256  else if (req == "GET_NETCONFIG")
257  {
258  get_netconfig();
259  }
260  else if (req == "SET_NETCONFIG")
261  {
262  set_netconfig(&msg);
263  }
264  else if (req == "SCRIPTS_DIR")
265  {
266  control_.send(script_directory());
267  }
268  else if (req == "FACTORY_CONF_DIR")
269  {
271  }
272  else
273  {
274  ASSERT_LOG(0, "Unsupported message: " + req);
275  }
276 }
277 
279 {
280  // we need to restore factory config file.
281  UnixShellScript script("cp -f");
282 
283  std::string kernel_config_file =
284  config_manager_.kconfig().get_child("kernel-cfg").data();
285  INFO("Kernel config file path = " << kernel_config_file);
286  INFO("RESTORING FACTORY CONFIG");
287 
288  if (script.run(UnixShellScript::toCmdLine(
289  factory_config_directory() + "/kernel.xml", kernel_config_file)) != 0)
290  {
291  ERROR("Error restoring factory configuration...");
292  }
293 }
294 
296 {
297  std::ostringstream oss;
298  boost::archive::binary_oarchive archive(oss);
299  auto network_config = config_manager_.kconfig().get_child("network");
300 
301  zmqpp::message response;
302  boost::property_tree::save(archive, network_config, 1);
303  response << oss.str();
304  control_.send(response);
305 }
306 
307 void Kernel::set_netconfig(zmqpp::message *msg)
308 {
309  std::string serialized_config;
310  *msg >> serialized_config;
311  std::istringstream iss(serialized_config);
312  boost::archive::binary_iarchive archive(iss);
313 
314  boost::property_tree::ptree network_config;
315  boost::property_tree::load(archive, network_config, 1);
316 
317  config_manager_.kconfig().erase("network");
318  config_manager_.kconfig().add_child("network", network_config);
319 
320  // we need to add the root node to write config;
321  boost::property_tree::ptree to_save;
322 
323  to_save.add_child("kernel", config_manager_.kconfig());
324  // remove path to config file.
325  to_save.get_child("kernel").erase("kernel-cfg");
326  try
327  {
329  to_save, config_manager_.kconfig().get_child("kernel-cfg").data());
330  }
331  catch (std::exception &e)
332  {
333  ERROR("Exception: " << e.what());
334  control_.send("KO");
335  return;
336  }
337  control_.send("OK");
338 }
339 
341 {
342  if (char *str = getenv("LEOSAC_FACTORY_CONFIG_DIR"))
343  {
344  INFO("Using FACTORY_CONFIG_DIR: " << str);
345  environ_[EnvironVar::FACTORY_CONFIG_DIR] = std::string(str);
346  }
347  if (char *str = getenv("LEOSAC_SCRIPTS_DIR"))
348  {
349  INFO("Using SCRIPTS_DIR: " << str);
350  environ_[EnvironVar::SCRIPTS_DIR] = std::string(str);
351  }
352 }
353 
354 std::string Kernel::script_directory() const
355 {
357  return environ_.at(EnvironVar::SCRIPTS_DIR) + "/";
358  return Leosac::Tools::UnixFs::getCWD() + "/scripts/";
359 }
360 
362 {
365  return Leosac::Tools::UnixFs::getCWD() + "/share/leosac/cfg/factory/";
366 }
367 
369 {
370  bool use_syslog = true;
371  bool use_database = false;
372  std::string syslog_min_level = "WARNING";
373  std::shared_ptr<spdlog::logger> console;
374 
375  // Drop existing logger, if any. (This is for the case of a "in process" restart)
376  spdlog::drop("syslog");
377  spdlog::drop("console");
378 
379  auto log_cfg_node = config_manager_.kconfig().get_child_optional("log");
380  if (log_cfg_node)
381  {
382  use_syslog = log_cfg_node->get<bool>("enable_syslog", true);
383  use_database = log_cfg_node->get<bool>("enable_database", false);
384  syslog_min_level = log_cfg_node->get<std::string>("min_syslog", "WARNING");
385  }
386  if (use_syslog)
387  {
388  auto syslog = spdlog::create(
389  "syslog", {std::make_shared<spdlog::sinks::syslog_sink>()});
390  syslog->set_level(static_cast<spdlog::level::level_enum>(
391  LogHelper::log_level_from_string(syslog_min_level)));
392  }
393  if (use_database)
394  {
395  console = spdlog::create(
396  "console", {std::make_shared<spdlog::sinks::stdout_sink_mt>(),
397  std::make_shared<Tools::DatabaseLogSink>(database_)});
398  }
399  else
400  console = spdlog::create(
401  "console", {std::make_shared<spdlog::sinks::stdout_sink_mt>()});
402  console->set_level(spdlog::level::debug);
403 }
404 
406 {
407  return module_manager_;
408 }
409 
411 {
412  return module_manager_;
413 }
414 
416 {
417  INFO("Saving current configuration to disk.");
418  std::string full_config =
420  std::string cfg_file_path =
421  config_manager_.kconfig().get<std::string>("kernel-cfg");
422 
423  DEBUG("Will overwrite " << cfg_file_path << " in order to save configuration.");
424  std::ofstream cfg_file(cfg_file_path);
425 
426  if (cfg_file << full_config)
427  return true;
428  return false;
429 }
430 
431 zmqpp::context &Kernel::zmqpp_context()
432 {
433  return ctx_;
434 }
435 
437 {
438  return config_manager_;
439 }
440 
442 {
443  want_restart_ = true;
444  is_running_ = false;
445 }
446 
448 {
449  return utils_;
450 }
451 
452 const std::chrono::steady_clock::time_point Kernel::start_time() const
453 {
454  return start_time_;
455 }
456 
458 {
459  // Request modules shutdown.
460  module_manager().stopModules(true);
461 
462  INFO("DONE SOFT STOP");
463 
464  // Still process tasks and message for 5s.
465  // Note that this a workaround. The shutdown process
466  // should be improved. This may require important changes
467  // to the module subsystem.
469  while (etc.elapsed() < 5000)
470  {
471  reactor_.poll(25);
472  utils_->scheduler().update(TargetThread::MAIN);
473  }
474 }
475 
477 {
478  auto db_cfg_node = config_manager_.kconfig().get_child_optional("database");
479  if (db_cfg_node)
480  {
481  ElapsedTimeCounter etc;
482  int wait_time = 1;
483  while (etc.elapsed() <
484  db_cfg_node->get<uint64_t>("startup_abort_time", 60 * 5) * 1000)
485  {
486  try
487  {
488  connect_to_db(*db_cfg_node);
489  ASSERT_LOG(database_, "Database pointer is null");
491  return;
492  }
493  catch (odb::unknown_schema &ex)
494  {
495  INFO("Database schema unknown: "
496  << ex.what() << ". Leosac will attempt to create the schema "
497  "and populate the database.");
499  }
500  catch (const odb::exception &e)
501  {
502  if (utils_->is_strict())
503  throw;
504  WARN("Cannot connect to or initialize database at this point. "
505  "Leosac will not start until it can reach the database. "
506  "Error was: "
507  << e.what());
508  INFO("Will now wait " << wait_time << " seconds.");
509  std::this_thread::sleep_for(std::chrono::seconds(wait_time));
510  wait_time = std::min(wait_time * 2, 60);
511  }
512  }
513  throw LEOSACException("Startup failed. Couldn't correctly "
514  "connect to / initialize the database");
515  }
516 }
517 
519 {
520  return database_;
521 }
522 
523 std::string Kernel::config_file_path() const
524 {
525  return config_manager_.kconfig().get<std::string>("kernel-cfg");
526 }
527 
529 {
530  ASSERT_LOG(!service_registry_, "ServiceRegistry is already created.");
531  service_registry_ = std::make_unique<ServiceRegistry>();
532  {
533  // Database service
534  if (database_)
535  {
536  service_registry_->register_service<DBService>(
537  std::make_unique<DBService>(database_));
538  }
539 
540  // Audit serializers
541  {
543  std::make_unique<Audit::Serializer::JSONService>());
544  }
545 
546  // AccessPoint
547  {
548  // Register the AccessPoint service. Also register default serializer
549  // for simple AccessPoint object.
550  auto aps = std::make_unique<Auth::AccessPointService>();
551  aps->register_serializer<Auth::AccessPoint>(
553  service_registry_->register_service<Auth::AccessPointService>(
554  std::move(aps));
555  }
556 
557  // Update
558  {
559  auto update_srv = std::make_unique<update::UpdateService>();
560  update_srv->register_serializer<Auth::AccessPointUpdate>(
562  service_registry_->register_service<update::UpdateService>(
563  std::move(update_srv));
564  }
565 
566  // Hardware service (required database service)
567  if (database_)
568  {
569  auto hardware_srv = std::make_unique<Hardware::HardwareService>(
570  service_registry_->get_service<DBService>());
571  service_registry_->register_service(std::move(hardware_srv));
572  }
573  }
574 }
575 
577 {
578  // Todo: Shall we unregister in the reverse order of serialization,
579  // similarly to ctor/dtor mechanism ?
580 
581  // Audit serializers
582  {
583  bool ret =
584  service_registry_->unregister_service<Audit::Serializer::JSONService>();
585  ASSERT_LOG(ret, "Failed to unregister AuditSerializerService.");
586  if (database_)
587  {
588  ret = service_registry_->unregister_service<DBService>();
589  ASSERT_LOG(ret, "Failed to unregister DBService");
590  }
591  }
592 
593  // Access Point service
594  {
595  {
596  auto aps = service_registry_->get_service<Auth::AccessPointService>();
598  }
599  bool ret = service_registry_->unregister_service<Auth::AccessPointService>();
600  ASSERT_LOG(ret, "Failed to unregister Auth::AccessPointService");
601  }
602 
603  // Update service
604  {
605  bool ret = service_registry_->unregister_service<update::UpdateService>();
606  ASSERT_LOG(ret, "Failed to unregister update::UpdateService");
607  }
608 
609  // Hardware service
610  {
611  bool ret =
612  service_registry_->unregister_service<Hardware::HardwareService>();
613  ASSERT_LOG(ret, "Failed to unregister HardwareService");
614  }
615 }
616 
618 {
619  ASSERT_LOG(service_registry_, "Service registry is null.");
620  return *service_registry_;
621 }
622 
624 {
625  using namespace odb;
626  using namespace odb::core;
627 
628  Auth::UserPtr admin;
629  Auth::GroupPtr users;
630  Cred::RFIDCardPtr card;
631 
632  // Default users / groups
633  {
634  transaction t(database_->begin());
635 
636  admin = std::make_shared<Auth::User>();
637  admin->firstname("Admin");
638  admin->lastname("ADMIN");
639  admin->username("admin");
640  admin->password("admin");
641  admin->rank(Auth::UserRank::ADMIN);
642  database_->persist(admin);
643 
644  Auth::UserPtr demo = std::make_shared<Auth::User>();
645  demo->firstname("Demo");
646  demo->lastname("Demo");
647  demo->username("demo");
648  demo->password("demo");
649  database_->persist(demo);
650 
651  Auth::GroupPtr administrators = std::make_shared<Auth::Group>();
652  administrators->name("Administrator");
653  administrators->member_add(admin);
654  database_->persist(administrators);
655 
656  users = std::make_shared<Auth::Group>();
657  users->name("Users");
658  users->member_add(demo);
659  users->member_add(admin);
660  database_->persist(users);
661  t.commit();
662  }
663 
664  // Credentials stuff
665  {
666  transaction t(database_->begin());
667 
668  card = std::make_shared<Cred::RFIDCard>();
669  card->owner(admin);
670  card->alias(std::string("BestCardEver"));
671  card->card_id("00:11:22:33");
672  card->nb_bits(32);
673  database_->persist(card);
674 
675  Cred::RFIDCard card2;
676  card2.alias(std::string("Ownerless"));
677  card2.card_id("aa:bb:cc:dd");
678  card2.nb_bits(32);
679  database_->persist(card2);
680  t.commit();
681  }
682 
683  // Schedules stuff
684  {
685  transaction t(database_->begin());
686 
687  Tools::SchedulePtr sched = std::make_shared<Tools::Schedule>();
688  sched->name("DummySchedule");
689  sched->description("A test schedule, with mapping.");
690  Tools::ScheduleMappingPtr map0 = std::make_shared<ScheduleMapping>();
691  map0->add_user(admin);
692  map0->add_group(users);
693  map0->add_credential(card);
694  map0->alias("My first mapping");
695  database_->persist(map0);
696  sched->add_mapping(map0);
697  database_->persist(sched);
698 
699  t.commit();
700  }
701 }
702 
703 void Kernel::connect_to_db(const boost::property_tree::ptree &db_cfg_node)
704 {
705  std::string db_type = db_cfg_node.get<std::string>("type", "");
706  if (db_type == "sqlite")
707  {
708  std::string db_path = db_cfg_node.get<std::string>("path");
709  database_ = std::make_shared<odb::sqlite::database>(
710  db_path, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE);
711  }
712  else if (db_type == "pgsql")
713  {
714  std::string db_user = db_cfg_node.get<std::string>("username");
715  std::string db_pw = db_cfg_node.get<std::string>("password");
716  std::string db_dbname = db_cfg_node.get<std::string>("dbname");
717  std::string db_host = db_cfg_node.get<std::string>("host", "");
718  uint16_t db_port = db_cfg_node.get<uint16_t>("port", 0);
719 
720  INFO("Connecting to PGSQL database.");
721  auto pg_db = std::make_shared<odb::pgsql::database>(
722  db_user, db_pw, db_dbname, db_host, db_port);
723  // todo: care about leak
724  pg_db->tracer(new db::PGSQLTracer(true));
725  database_ = pg_db;
726  }
727  else
728  {
729  throw ConfigException(config_file_path(), "Unsupported database type: " +
730  Colorize::underline(db_type));
731  }
732 }
733 
735 {
736  SignalHandler::registerCallback(Signal::SigInt, [this](Signal) {
737  if (!this->is_running_)
738  {
739  std::cerr << "SIGINT received a second time. Exiting abruptly."
740  << std::endl;
741  _exit(-1);
742  }
743  else
744  {
745  this->is_running_ = false;
746  }
747  });
748 
749  SignalHandler::registerCallback(Signal::SigTerm,
750  [this](Signal) { this->is_running_ = false; });
751 
752  SignalHandler::registerCallback(Signal::SigHup,
753  [this](Signal) { this->send_sighup_ = true; });
754 }
755 
757 {
758  ASSERT_LOG(database_, "Database pointer is null");
759 
760  odb::schema_version v = database_->schema_version("core");
761  odb::schema_version cv(odb::schema_catalog::current_version(*database_, "core"));
762 
763  DEBUG("Database schema version: " << v);
764  if (v == 0)
765  {
766  {
767  odb::transaction t(database_->begin());
768  odb::schema_catalog::create_schema(*database_, "core");
769  t.commit();
770  }
772  }
773  else if (v < cv)
774  {
775  INFO("Leosac performing database migration. Going from version "
776  << v << " to version " << cv);
777  odb::transaction t(database_->begin());
778  odb::schema_catalog::migrate(*database_, cv, "core");
779  t.commit();
780  }
781 }
Leosac::Kernel::reactor_
zmqpp::reactor reactor_
Watch for message on the control_ socket.
Definition: kernel.hpp:295
Leosac::Kernel::network_config_
std::unique_ptr< NetworkConfig > network_config_
Object that handle networking configuration.
Definition: kernel.hpp:316
AccessPointSerializer.hpp
Leosac::Kernel::factory_config_directory
std::string factory_config_directory() const
Return the path to factory config directory Uses environment variable if available,...
Definition: kernel.cpp:361
Leosac::Kernel::EnvironVar::FACTORY_CONFIG_DIR
@ FACTORY_CONFIG_DIR
Leosac::Tools::ElapsedTimeCounter::elapsed
size_t elapsed() const
Returns the elapsed time, in milliseconds, since the creation of the ElapsedTimeCounter object.
Definition: ElapsedTimeCounter.cpp:30
GlobalRegistry.hpp
Leosac::Kernel::restart_later
void restart_later()
Set the running_ and want_restart flag so that leosac will restart in the next main loop iteration.
Definition: kernel.cpp:441
unixshellscript.hpp
UnixShellScript class declaration.
WARN
@ WARN
Definition: log.hpp:33
Leosac::Hardware::HardwareService
Database aware Hardware Service.
Definition: HardwareService.hpp:39
Leosac::Kernel::~Kernel
~Kernel()
Implemented in .cpp for unique_ptr to work.
Definition: kernel.cpp:117
zmqpp
Definition: CoreUtils.hpp:27
database.hpp
Leosac::ConfigManager
That class helps manage the configuration for the application and its module.
Definition: ConfigManager.hpp:45
UpdateService.hpp
Leosac::Kernel::xmlnne_
Tools::XmlNodeNameEnforcer xmlnne_
Definition: kernel.hpp:351
ERROR
@ ERROR
Definition: log.hpp:32
DEBUG
@ DEBUG
Definition: log.hpp:35
AccessPointService.hpp
Leosac::Cred::RFIDCard::nb_bits
virtual int nb_bits() const override
Definition: RFIDCard.cpp:39
ServiceRegistry.hpp
Leosac::Kernel::set_netconfig
void set_netconfig(zmqpp::message *msg)
Handle SET_NETCONFIG command and update the configuration file directly.
Definition: kernel.cpp:307
Leosac::Cred::RFIDCardPtr
std::shared_ptr< RFIDCard > RFIDCardPtr
Definition: CredentialFwd.hpp:43
signalhandler.hpp
signal handler to provide a C++ interface for UNIX sigaction()
AccessPointUpdateSerializer.hpp
ElapsedTimeCounter.hpp
ASSERT_LOG
#define ASSERT_LOG(cond, msg)
Definition: log.hpp:190
Leosac::Auth::AccessPointUpdate
Definition: AccessPointUpdate.hpp:29
Leosac::Auth::UserPtr
std::shared_ptr< User > UserPtr
Definition: AuthFwd.hpp:31
Leosac::Tools::propertyTreeFromXmlFile
boost::property_tree::ptree propertyTreeFromXmlFile(const std::string &path)
Build a property tree from a xml file.
Definition: XmlPropertyTree.cpp:39
RFIDCard.hpp
User.hpp
Leosac::Kernel::module_manager_
ModuleManager module_manager_
Manages the different libraries (.so) we load, path to those libraries, modules instantiation.
Definition: kernel.hpp:311
Leosac::Kernel::autosave_
bool autosave_
Autosave configuration on shutdown.
Definition: kernel.hpp:339
Leosac::Kernel::run
bool run()
Main loop of the main thread.
Definition: kernel.cpp:151
ModuleManager
A second module manager that loads "ZMQ aware" module – modules that talks to the application through...
Definition: module_manager.hpp:54
Leosac::Kernel::connect_to_db
void connect_to_db(const boost::property_tree::ptree &db_cfg_node)
Definition: kernel.cpp:703
INFO
@ INFO
Definition: log.hpp:34
Leosac::Tools::ElapsedTimeCounter
This class provide a simple to get the elapsed time since it's creation.
Definition: ElapsedTimeCounter.hpp:36
Leosac::DBPtr
std::shared_ptr< odb::database > DBPtr
Definition: db_fwd.hpp:31
Leosac::Kernel::environ_
std::map< EnvironVar, std::string > environ_
Definition: kernel.hpp:329
Leosac::Tools::UnixFs::getCWD
static std::string getCWD()
get current working directory without trailing slash
Definition: unixfs.cpp:40
odb
Provide ODB magic to be able to store an Leosac::Audit::EventType (FlagSet) object.
Definition: AuditEventMaskODB.hpp:31
PGSQLTracer.hpp
Leosac::Kernel::service_registry_
ServiceRegistryUPtr service_registry_
Definition: kernel.hpp:353
Leosac::Cred::RFIDCard
An RFID card credential.
Definition: RFIDCard.hpp:33
Leosac::Kernel::database_
DBPtr database_
A pointer to the database used by Leosac, if any.
Definition: kernel.hpp:349
ExceptionsTools.hpp
Leosac::Tools::ScheduleMappingPtr
std::shared_ptr< ScheduleMapping > ScheduleMappingPtr
Definition: ToolsFwd.hpp:41
ModuleManager::initModules
void initModules()
Actually call the init_module() function of each library we loaded.
Definition: module_manager.cpp:69
Leosac::RemoteControl::handle_msg
void handle_msg()
Register by core and called when message arrives.
Definition: RemoteControl.cpp:94
Leosac::Tools::UnixShellScript::run
int run(const std::string &args=std::string())
Definition: unixshellscript.cpp:46
Leosac::DBService
Provides various database-related services to consumer.
Definition: DBService.hpp:34
Leosac::Colorize::underline
std::string underline(const T &in)
Definition: Colorize.hpp:64
Leosac::Tools::SchedulePtr
std::shared_ptr< Schedule > SchedulePtr
Definition: ToolsFwd.hpp:32
CoreException
Definition: coreexception.hpp:33
Leosac::ConfigManager::kconfig
const boost::property_tree::ptree & kconfig() const
Return const & on the general config tree.
Definition: ConfigManager.cpp:166
Leosac::ConfigManager::store_config
bool store_config(const std::string &module, const boost::property_tree::ptree &cfg)
Store the configuration tree for a module.
Definition: ConfigManager.cpp:117
unixfs.hpp
unix filesystem helper functions
Mail.hpp
Leosac::Kernel::populate_default_db
void populate_default_db()
Definition: kernel.cpp:623
DatabaseLogSink.hpp
Leosac::Kernel::extract_environ
void extract_environ()
Definition: kernel.cpp:340
Leosac::Tools::propertyTreeToXmlFile
void propertyTreeToXmlFile(const boost::property_tree::ptree &tree, const std::string &path)
Write a property tree to an xml file.
Definition: XmlPropertyTree.cpp:58
Leosac
This is the header file for a generated source file, GitSHA1.cpp.
Definition: APIStatusCode.hpp:22
Leosac::Kernel::Kernel
Kernel(const boost::property_tree::ptree &config, bool strict_mode=false)
Construct a Kernel object.
Definition: kernel.cpp:66
Leosac::Kernel::configure_logger
void configure_logger()
Definition: kernel.cpp:368
kernel.hpp
Leosac::ConfigChecker
This class is here to help check the validity of the configuration.
Definition: ConfigChecker.hpp:46
Leosac::Kernel::start_time
const std::chrono::steady_clock::time_point start_time() const
Return the time point at which Leosac started.
Definition: kernel.cpp:452
Leosac::Kernel::unregister_core_services
void unregister_core_services()
Unregister the service that were registered into register_core_services().
Definition: kernel.cpp:576
Leosac::Kernel::factory_reset
void factory_reset()
Reset Leosac configuration.
Definition: kernel.cpp:278
Leosac::Kernel::want_restart_
bool want_restart_
Should leosac restart ?
Definition: kernel.hpp:305
Leosac::NetworkConfig
Class that helps configuring the network.
Definition: networkconfig.hpp:40
ConfigException
Definition: configexception.hpp:87
JSONService.hpp
Group.hpp
Leosac::Kernel::get_netconfig
void get_netconfig()
Handle GET_NETCONFIG command.
Definition: kernel.cpp:295
Leosac::Scheduler
This is a scheduler that is used internally to schedule asynchronous / long running tasks.
Definition: Scheduler.hpp:47
Leosac::update::AccessPointUpdateJSONSerializer::serialize
static json serialize(const Auth::AccessPointUpdate &in, const SecurityContext &sc)
Definition: AccessPointUpdateSerializer.cpp:29
LEOSACException
A base class for Leosac specific exception.
Definition: leosacexception.hpp:40
Leosac::Kernel::ctx_
zmqpp::context ctx_
The application ZMQ context.
Definition: kernel.hpp:275
Leosac::Kernel::save_config
bool save_config()
Save the current configuration to its original file if autosave is enabled.
Definition: kernel.cpp:415
Random.hpp
Leosac::ServiceRegistry
A class that manages services.
Definition: ServiceRegistry.hpp:110
Leosac::Auth::AccessPointService
This service lets various AccessPoint backend register and provide implementation to use by the Acces...
Definition: AccessPointService.hpp:69
Leosac::Auth::GroupPtr
std::shared_ptr< Group > GroupPtr
Definition: AuthFwd.hpp:37
Leosac::AccessPointJSONSerializer::serialize
static json serialize(const Auth::IAccessPoint &ap, const SecurityContext &sc)
Definition: AccessPointSerializer.cpp:28
GenGuid.h
Leosac::Kernel::shutdown
void shutdown()
Definition: kernel.cpp:457
Leosac::CoreUtils
This class is part of Leosac::Kernel, but it only exposes thread-safe functionalities that may be use...
Definition: CoreUtils.hpp:43
Leosac::Kernel::send_sighup_
bool send_sighup_
Should we broadcast "SIGHUP" in the next main loop iteration ?
Definition: kernel.hpp:334
XmlPropertyTree.hpp
Leosac::Kernel::is_running_
bool is_running_
Controls core main loop.
Definition: kernel.hpp:300
Leosac::Kernel::zmqpp_context
zmqpp::context & zmqpp_context()
Returns a reference to the zmqpp context created for the application.
Definition: kernel.cpp:431
Leosac::Kernel::module_manager_init
void module_manager_init()
Init the module manager by feeding it paths to library file, loading module, etc.
Definition: kernel.cpp:186
Leosac::Cred::RFIDCard::card_id
virtual const std::string & card_id() const override
Definition: RFIDCard.cpp:34
Leosac::Kernel::configure_signal_handler
void configure_signal_handler()
Setup signal handling for the process.
Definition: kernel.cpp:734
Leosac::Kernel::make_config
static boost::property_tree::ptree make_config(const Leosac::Tools::RuntimeOptions &opt)
Build a property tree from a runtime object object.
Definition: kernel.cpp:128
HardwareService.hpp
Leosac::Kernel::instance_
static Kernel * instance_
A global pointer to the Kernel instance.
Definition: kernel.hpp:358
Leosac::Kernel
Core of Leosac.
Definition: kernel.hpp:73
Leosac::db::PGSQLTracer
An implementation of odb::tracer that use the logging infrastructure of Leosac.
Definition: PGSQLTracer.hpp:35
Leosac::Kernel::utils_
CoreUtilsPtr utils_
Definition: kernel.hpp:268
Leosac::ExtensibleSerializer::unregister_serializer
void unregister_serializer()
Definition: ExtensibleSerializer.hpp:112
Leosac::TargetThread::MAIN
@ MAIN
Leosac::Cred::Credential::alias
virtual std::string alias() const override
An alias for the credential.
Definition: Credential.cpp:43
Leosac::Kernel::config_manager_
ConfigManager config_manager_
Definition: kernel.hpp:270
Leosac::Kernel::script_directory
std::string script_directory() const
Return the path to the scripts directory.
Definition: kernel.cpp:354
Leosac::Kernel::create_update_schema
void create_update_schema()
Create and/or update the database schema.
Definition: kernel.cpp:756
Leosac::Tools::RuntimeOptions::get_param
const std::string & get_param(const std::string &key) const
Definition: runtimeoptions.cpp:41
Leosac::Auth::UserRank::ADMIN
@ ADMIN
Site administrator.
log.hpp
LogHelper::log_level_from_string
LogLevel log_level_from_string(const std::string &level)
Definition: log.cpp:100
ModuleManager::stopModules
void stopModules(bool soft=false)
Opposite of init module.
Definition: module_manager.cpp:198
Leosac::Tools::propertyTreeToXml
std::string propertyTreeToXml(const boost::property_tree::ptree &tree)
Convert a property tree to an xml formatted string.
Definition: XmlPropertyTree.cpp:67
Leosac::update::UpdateService
This service provides various update management utilities.
Definition: UpdateService.hpp:126
Leosac::Kernel::handle_control_request
void handle_control_request()
A request has arrived on the control_ socket.
Definition: kernel.cpp:235
ModuleManager::addToPath
void addToPath(const std::string &dir)
Add a directory to a path.
Definition: module_manager.cpp:148
Leosac::Kernel::remote_controller_
std::unique_ptr< RemoteControl > remote_controller_
Object that expose leosac to the world.
Definition: kernel.hpp:321
Leosac::Kernel::register_core_services
void register_core_services()
Register some important services to the service registry.
Definition: kernel.cpp:528
Leosac::Kernel::config_file_path
std::string config_file_path() const
Return the path to the kernel configuration file.
Definition: kernel.cpp:523
Leosac::Kernel::control_
zmqpp::socket control_
A REP socket to send request to the kernel.
Definition: kernel.hpp:285
Leosac::Tools::RuntimeOptions
Holds informations about runtime options, such as "is this a verbose run" or path to configurations f...
Definition: runtimeoptions.hpp:42
ModuleManager::loadModule
bool loadModule(const std::string &module_name)
Search the path and load a module based on a property tree for this module.
Definition: module_manager.cpp:154
Leosac::Tools::UnixShellScript
Definition: unixshellscript.hpp:36
Leosac::Kernel::service_registry
ServiceRegistry & service_registry()
Retrieve a reference to the service registry.
Definition: kernel.cpp:617
Leosac::Kernel::configure_database
void configure_database()
Definition: kernel.cpp:476
Leosac::Auth::AccessPoint
Definition: AccessPoint.hpp:30
Schedule.hpp
Leosac::Kernel::core_utils
CoreUtilsPtr core_utils()
Returns a (smart) pointer to the core utils: some thread-safe utilities.
Definition: kernel.cpp:447
Leosac::CoreUtilsPtr
std::shared_ptr< CoreUtils > CoreUtilsPtr
Definition: LeosacFwd.hpp:35
Leosac::Kernel::module_manager
const ModuleManager & module_manager() const
Returns a reference to the module manager object (const version).
Definition: kernel.cpp:405
Leosac::Tools::Signal
Signal
Definition: signalhandler.hpp:39
Leosac::ConfigManager::get_application_config
boost::property_tree::ptree get_application_config()
Retrieve the (current, running) configuration from the application and its modules.
Definition: ConfigManager.cpp:41
Leosac::Tools
Definition: DatabaseLogSink.hpp:27
Leosac::Kernel::EnvironVar::SCRIPTS_DIR
@ SCRIPTS_DIR
Leosac::Kernel::config_manager
ConfigManager & config_manager()
Retrieve a reference to the configuration manager object.
Definition: kernel.cpp:436
Leosac::Audit::Serializer::JSONService
This service manages runtime registered serializer that target AuditEntry object.
Definition: JSONService.hpp:42
Leosac::Kernel::start_time_
const std::chrono::steady_clock::time_point start_time_
Time-point when Leosac started to run.
Definition: kernel.hpp:344
Leosac::RemoteControl
This class handle the remote control of leosac.
Definition: RemoteControl.hpp:40
Leosac::Kernel::database
DBPtr database()
Retrieve a pointer to the database, if any.
Definition: kernel.cpp:518
Leosac::Kernel::bus_push_
zmqpp::socket bus_push_
A PUSH socket to write on the bus.
Definition: kernel.hpp:290