swanctl.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * Copyright (C) 2018 Tobias Brunner
  3. * HSR Hochschule fuer Technik Rapperswil
  4. *
  5. * Copyright (C) 2014 Martin Willi
  6. * Copyright (C) 2014 revosec AG
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2 of the License, or (at your
  11. * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  15. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  16. * for more details.
  17. */
  18. #include "swanctl.h"
  19. #include "command.h"
  20. #include <unistd.h>
  21. #include <library.h>
  22. /*
  23. * Described in header
  24. */
  25. char *swanctl_dir;
  26. /*
  27. * Described in header
  28. */
  29. settings_t *load_swanctl_conf(char *file)
  30. {
  31. settings_t *cfg;
  32. char buf[PATH_MAX];
  33. if (!file)
  34. {
  35. if (!strlen(swanctl_dir))
  36. {
  37. free(swanctl_dir);
  38. swanctl_dir = strdup(getcwd(buf, sizeof(buf)));
  39. }
  40. file = buf;
  41. snprintf(buf, sizeof(buf), "%s%s%s", swanctl_dir,
  42. DIRECTORY_SEPARATOR, SWANCTL_CONF);
  43. }
  44. cfg = settings_create(file);
  45. if (!cfg)
  46. {
  47. fprintf(stderr, "parsing '%s' failed\n", file);
  48. return NULL;
  49. }
  50. free(swanctl_dir);
  51. swanctl_dir = path_dirname(file);
  52. return cfg;
  53. }
  54. /**
  55. * Cleanup library atexit()
  56. */
  57. static void cleanup()
  58. {
  59. free(swanctl_dir);
  60. lib->processor->cancel(lib->processor);
  61. library_deinit();
  62. }
  63. /**
  64. * Library initialization and operation parsing
  65. */
  66. int main(int argc, char *argv[])
  67. {
  68. atexit(cleanup);
  69. if (!library_init(NULL, "swanctl"))
  70. {
  71. exit(SS_RC_LIBSTRONGSWAN_INTEGRITY);
  72. }
  73. if (lib->integrity &&
  74. !lib->integrity->check_file(lib->integrity, "swanctl", argv[0]))
  75. {
  76. fprintf(stderr, "integrity check of swanctl failed\n");
  77. exit(SS_RC_DAEMON_INTEGRITY);
  78. }
  79. if (!lib->plugins->load(lib->plugins,
  80. lib->settings->get_str(lib->settings, "swanctl.load", PLUGINS)))
  81. {
  82. exit(SS_RC_INITIALIZATION_FAILED);
  83. }
  84. swanctl_dir = strdup(getenv("SWANCTL_DIR") ?: SWANCTLDIR);
  85. dbg_default_set_level(0);
  86. lib->processor->set_threads(lib->processor, 4);
  87. dbg_default_set_level(1);
  88. return command_dispatch(argc, argv);
  89. }