charon-systemd.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. /*
  2. * Copyright (C) 2006-2018 Tobias Brunner
  3. * Copyright (C) 2005-2014 Martin Willi
  4. * Copyright (C) 2006 Daniel Roethlisberger
  5. * Copyright (C) 2005 Jan Hutter
  6. * HSR Hochschule fuer Technik Rapperswil
  7. * Copyright (C) 2014 revosec AG
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License as published by the
  11. * Free Software Foundation; either version 2 of the License, or (at your
  12. * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  13. *
  14. * This program is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  16. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  17. * for more details.
  18. */
  19. #include <signal.h>
  20. #include <stdio.h>
  21. #include <pthread.h>
  22. #include <sys/stat.h>
  23. #include <sys/types.h>
  24. #include <sys/utsname.h>
  25. #include <unistd.h>
  26. #include <errno.h>
  27. /* won't make sense from our logging hook */
  28. #define SD_JOURNAL_SUPPRESS_LOCATION
  29. #include <systemd/sd-daemon.h>
  30. #include <systemd/sd-journal.h>
  31. #include <daemon.h>
  32. #include <library.h>
  33. #include <utils/backtrace.h>
  34. #include <threading/thread.h>
  35. #include <threading/rwlock.h>
  36. /**
  37. * Default user and group
  38. */
  39. #ifndef IPSEC_USER
  40. #define IPSEC_USER NULL
  41. #endif
  42. #ifndef IPSEC_GROUP
  43. #define IPSEC_GROUP NULL
  44. #endif
  45. /**
  46. * hook in library for debugging messages
  47. */
  48. extern void (*dbg) (debug_t group, level_t level, char *fmt, ...);
  49. /**
  50. * Logging hook for library logs, using stderr output
  51. */
  52. static void dbg_stderr(debug_t group, level_t level, char *fmt, ...)
  53. {
  54. va_list args;
  55. if (level <= 1)
  56. {
  57. va_start(args, fmt);
  58. fprintf(stderr, "00[%N] ", debug_names, group);
  59. vfprintf(stderr, fmt, args);
  60. fprintf(stderr, "\n");
  61. va_end(args);
  62. }
  63. }
  64. typedef struct journal_logger_t journal_logger_t;
  65. /**
  66. * Logger implementation using systemd-journal
  67. */
  68. struct journal_logger_t {
  69. /**
  70. * Public interface
  71. */
  72. custom_logger_t public;
  73. /**
  74. * Configured loglevels
  75. */
  76. level_t levels[DBG_MAX];
  77. /**
  78. * Lock for levels
  79. */
  80. rwlock_t *lock;
  81. };
  82. METHOD(logger_t, vlog, void,
  83. journal_logger_t *this, debug_t group, level_t level, int thread,
  84. ike_sa_t *ike_sa, const char *fmt, va_list args)
  85. {
  86. char buf[4096], *msg = buf;
  87. ssize_t len;
  88. va_list copy;
  89. va_copy(copy, args);
  90. len = vsnprintf(msg, sizeof(buf), fmt, copy);
  91. va_end(copy);
  92. if (len >= sizeof(buf))
  93. {
  94. len++;
  95. msg = malloc(len);
  96. va_copy(copy, args);
  97. len = vsnprintf(msg, len, fmt, copy);
  98. va_end(copy);
  99. }
  100. if (len > 0)
  101. {
  102. char unique[64] = "", name[256] = "";
  103. int priority;
  104. if (ike_sa)
  105. {
  106. snprintf(unique, sizeof(unique), "IKE_SA_UNIQUE_ID=%u",
  107. ike_sa->get_unique_id(ike_sa));
  108. if (ike_sa->get_peer_cfg(ike_sa))
  109. {
  110. snprintf(name, sizeof(name), "IKE_SA_NAME=%s",
  111. ike_sa->get_name(ike_sa));
  112. }
  113. }
  114. switch (level)
  115. {
  116. case LEVEL_AUDIT:
  117. priority = LOG_NOTICE;
  118. break;
  119. case LEVEL_CTRL:
  120. priority = LOG_INFO;
  121. break;
  122. default:
  123. priority = LOG_DEBUG;
  124. break;
  125. }
  126. sd_journal_send(
  127. "MESSAGE=%s", msg,
  128. "MESSAGE_ID=57d2708c-d607-43bd-8c39-66bf%.8x",
  129. chunk_hash_static(chunk_from_str((char*)fmt)),
  130. "PRIORITY=%d", priority,
  131. "GROUP=%N", debug_names, group,
  132. "LEVEL=%d", level,
  133. "THREAD=%d", thread,
  134. unique[0] ? unique : NULL,
  135. name[0] ? name : NULL,
  136. NULL);
  137. }
  138. if (msg != buf)
  139. {
  140. free(msg);
  141. }
  142. }
  143. METHOD(logger_t, get_level, level_t,
  144. journal_logger_t *this, debug_t group)
  145. {
  146. level_t level;
  147. this->lock->read_lock(this->lock);
  148. level = this->levels[group];
  149. this->lock->unlock(this->lock);
  150. return level;
  151. }
  152. METHOD(custom_logger_t, set_level, void,
  153. journal_logger_t *this, debug_t group, level_t level)
  154. {
  155. this->lock->write_lock(this->lock);
  156. this->levels[group] = level;
  157. this->lock->unlock(this->lock);
  158. }
  159. METHOD(custom_logger_t, logger_destroy, void,
  160. journal_logger_t *this)
  161. {
  162. this->lock->destroy(this->lock);
  163. free(this);
  164. }
  165. static custom_logger_t *journal_logger_create(const char *name)
  166. {
  167. journal_logger_t *this;
  168. INIT(this,
  169. .public = {
  170. .logger = {
  171. .vlog = _vlog,
  172. .get_level = _get_level,
  173. },
  174. .set_level = _set_level,
  175. .destroy = _logger_destroy,
  176. },
  177. .lock = rwlock_create(RWLOCK_TYPE_DEFAULT),
  178. );
  179. return &this->public;
  180. }
  181. /**
  182. * Run the daemon and handle unix signals
  183. */
  184. static int run()
  185. {
  186. sigset_t set;
  187. sigemptyset(&set);
  188. sigaddset(&set, SIGHUP);
  189. sigaddset(&set, SIGTERM);
  190. sigprocmask(SIG_BLOCK, &set, NULL);
  191. sd_notify(0, "READY=1\n");
  192. while (TRUE)
  193. {
  194. int sig;
  195. sig = sigwaitinfo(&set, NULL);
  196. if (sig == -1)
  197. {
  198. if (errno == EINTR)
  199. { /* ignore signals we didn't wait for */
  200. continue;
  201. }
  202. DBG1(DBG_DMN, "waiting for signal failed: %s", strerror(errno));
  203. return SS_RC_INITIALIZATION_FAILED;
  204. }
  205. switch (sig)
  206. {
  207. case SIGHUP:
  208. {
  209. DBG1(DBG_DMN, "signal of type SIGHUP received. Reloading "
  210. "configuration");
  211. if (lib->settings->load_files(lib->settings, lib->conf, FALSE))
  212. {
  213. charon->load_loggers(charon);
  214. lib->plugins->reload(lib->plugins, NULL);
  215. }
  216. else
  217. {
  218. DBG1(DBG_DMN, "reloading config failed, keeping old");
  219. }
  220. break;
  221. }
  222. case SIGTERM:
  223. {
  224. DBG1(DBG_DMN, "SIGTERM received, shutting down");
  225. charon->bus->alert(charon->bus, ALERT_SHUTDOWN_SIGNAL, sig);
  226. return 0;
  227. }
  228. }
  229. }
  230. }
  231. /**
  232. * lookup UID and GID
  233. */
  234. static bool lookup_uid_gid()
  235. {
  236. char *name;
  237. name = lib->settings->get_str(lib->settings, "%s.user", IPSEC_USER,
  238. lib->ns);
  239. if (name && !lib->caps->resolve_uid(lib->caps, name))
  240. {
  241. return FALSE;
  242. }
  243. name = lib->settings->get_str(lib->settings, "%s.group", IPSEC_GROUP,
  244. lib->ns);
  245. if (name && !lib->caps->resolve_gid(lib->caps, name))
  246. {
  247. return FALSE;
  248. }
  249. return TRUE;
  250. }
  251. #ifndef DISABLE_SIGNAL_HANDLER
  252. /**
  253. * Handle SIGSEGV/SIGILL signals raised by threads
  254. */
  255. static void segv_handler(int signal)
  256. {
  257. backtrace_t *backtrace;
  258. DBG1(DBG_DMN, "thread %u received %d", thread_current_id(), signal);
  259. backtrace = backtrace_create(2);
  260. backtrace->log(backtrace, NULL, TRUE);
  261. backtrace->log(backtrace, stderr, TRUE);
  262. backtrace->destroy(backtrace);
  263. DBG1(DBG_DMN, "killing ourself, received critical signal");
  264. abort();
  265. }
  266. #endif /* DISABLE_SIGNAL_HANDLER */
  267. /**
  268. * Add namespace alias
  269. */
  270. static void __attribute__ ((constructor))register_namespace()
  271. {
  272. /* inherit settings from charon */
  273. library_add_namespace("charon");
  274. }
  275. /**
  276. * Register journal logger
  277. */
  278. static void __attribute__ ((constructor))register_logger()
  279. {
  280. register_custom_logger("journal", journal_logger_create);
  281. }
  282. /**
  283. * Main function, starts the daemon.
  284. */
  285. int main(int argc, char *argv[])
  286. {
  287. struct sigaction action;
  288. struct utsname utsname;
  289. int status = SS_RC_INITIALIZATION_FAILED;
  290. dbg = dbg_stderr;
  291. if (uname(&utsname) != 0)
  292. {
  293. memset(&utsname, 0, sizeof(utsname));
  294. }
  295. sd_notifyf(0, "STATUS=Starting charon-systemd, strongSwan %s, %s %s, %s",
  296. VERSION, utsname.sysname, utsname.release, utsname.machine);
  297. atexit(library_deinit);
  298. if (!library_init(NULL, "charon-systemd"))
  299. {
  300. sd_notifyf(0, "STATUS=libstrongswan initialization failed");
  301. return SS_RC_INITIALIZATION_FAILED;
  302. }
  303. if (lib->integrity &&
  304. !lib->integrity->check_file(lib->integrity, "charon-systemd", argv[0]))
  305. {
  306. sd_notifyf(0, "STATUS=integrity check of charon-systemd failed");
  307. return SS_RC_INITIALIZATION_FAILED;
  308. }
  309. if (!libcharon_init())
  310. {
  311. sd_notifyf(0, "STATUS=libcharon initialization failed");
  312. goto error;
  313. }
  314. if (!lookup_uid_gid())
  315. {
  316. sd_notifyf(0, "STATUS=unknown uid/gid");
  317. goto error;
  318. }
  319. /* we registered the journal logger as custom logger, which gets its
  320. * settings from <ns>.customlog.journal, let it fallback to <ns>.journal */
  321. lib->settings->add_fallback(lib->settings, "%s.customlog.journal",
  322. "%s.journal", lib->ns);
  323. /* load the journal logger by default */
  324. lib->settings->set_default_str(lib->settings, "%s.journal.default", "1",
  325. lib->ns);
  326. charon->load_loggers(charon);
  327. if (!charon->initialize(charon,
  328. lib->settings->get_str(lib->settings, "%s.load", PLUGINS, lib->ns)))
  329. {
  330. sd_notifyf(0, "STATUS=charon initialization failed");
  331. goto error;
  332. }
  333. lib->plugins->status(lib->plugins, LEVEL_CTRL);
  334. if (!lib->caps->drop(lib->caps))
  335. {
  336. sd_notifyf(0, "STATUS=dropping capabilities failed");
  337. goto error;
  338. }
  339. /* add handler for fatal signals,
  340. * INT, TERM and HUP are handled by sigwaitinfo() in run() */
  341. action.sa_flags = 0;
  342. sigemptyset(&action.sa_mask);
  343. sigaddset(&action.sa_mask, SIGINT);
  344. sigaddset(&action.sa_mask, SIGTERM);
  345. sigaddset(&action.sa_mask, SIGHUP);
  346. /* optionally let the external system handle fatal signals */
  347. #ifndef DISABLE_SIGNAL_HANDLER
  348. action.sa_handler = segv_handler;
  349. sigaction(SIGSEGV, &action, NULL);
  350. sigaction(SIGILL, &action, NULL);
  351. sigaction(SIGBUS, &action, NULL);
  352. #endif /* DISABLE_SIGNAL_HANDLER */
  353. action.sa_handler = SIG_IGN;
  354. sigaction(SIGPIPE, &action, NULL);
  355. pthread_sigmask(SIG_SETMASK, &action.sa_mask, NULL);
  356. charon->start(charon);
  357. sd_notifyf(0, "STATUS=charon-systemd running, strongSwan %s, %s %s, %s",
  358. VERSION, utsname.sysname, utsname.release, utsname.machine);
  359. status = run();
  360. error:
  361. libcharon_deinit();
  362. return status;
  363. }