corosync-qnetd.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. /*
  2. * Copyright (c) 2015-2020 Red Hat, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Jan Friesse (jfriesse@redhat.com)
  7. *
  8. * This software licensed under BSD license, the text of which follows:
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions are met:
  12. *
  13. * - Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. * - Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * - Neither the name of the Red Hat, Inc. nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  26. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  32. * THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #include <err.h>
  35. #include <errno.h>
  36. #include <getopt.h>
  37. #include <limits.h>
  38. #include <signal.h>
  39. #include <unistd.h>
  40. #include "qnet-config.h"
  41. #include "dynar.h"
  42. #include "dynar-str.h"
  43. #include "dynar-getopt-lex.h"
  44. #include "log.h"
  45. #include "nss-sock.h"
  46. #include "pr-poll-array.h"
  47. #include "qnetd-advanced-settings.h"
  48. #include "qnetd-algorithm.h"
  49. #include "qnetd-instance.h"
  50. #include "qnetd-ipc.h"
  51. #include "qnetd-client-net.h"
  52. #include "qnetd-client-msg-received.h"
  53. #include "utils.h"
  54. #include "msg.h"
  55. #ifdef HAVE_LIBSYSTEMD
  56. #include <systemd/sd-daemon.h>
  57. #endif
  58. /*
  59. * This is global variable used for comunication with main loop and signal (calls close)
  60. */
  61. struct qnetd_instance *global_instance;
  62. enum tlv_decision_algorithm_type
  63. qnetd_static_supported_decision_algorithms[QNETD_STATIC_SUPPORTED_DECISION_ALGORITHMS_SIZE] = {
  64. TLV_DECISION_ALGORITHM_TYPE_TEST,
  65. TLV_DECISION_ALGORITHM_TYPE_FFSPLIT,
  66. TLV_DECISION_ALGORITHM_TYPE_2NODELMS,
  67. TLV_DECISION_ALGORITHM_TYPE_LMS,
  68. };
  69. static void
  70. qnetd_err_nss(void)
  71. {
  72. log_nss(LOG_CRIT, "NSS error");
  73. exit(EXIT_FAILURE);
  74. }
  75. static void
  76. qnetd_warn_nss(void)
  77. {
  78. log_nss(LOG_WARNING, "NSS warning");
  79. }
  80. static int
  81. server_socket_poll_loop_read_cb(PRFileDesc *prfd, const PRPollDesc *pd, void *user_data1, void *user_data2)
  82. {
  83. struct qnetd_instance *instance = (struct qnetd_instance *)user_data1;
  84. qnetd_client_net_accept(instance);
  85. return (0);
  86. }
  87. static int
  88. server_socket_poll_loop_err_cb(PRFileDesc *prfd, short revents, const PRPollDesc *pd,
  89. void *user_data1, void *user_data2)
  90. {
  91. if (revents != POLLNVAL) {
  92. /*
  93. * Poll ERR on listening socket is fatal error.
  94. * POLL_NVAL is used as a signal to quit poll loop.
  95. */
  96. log(LOG_CRIT, "POLL_ERR (%u) on listening socket", revents);
  97. } else {
  98. log(LOG_DEBUG, "Listening socket is closed");
  99. }
  100. return (-1);
  101. }
  102. static void
  103. signal_int_handler(int sig)
  104. {
  105. log(LOG_DEBUG, "SIGINT received - closing server IPC socket");
  106. qnetd_ipc_close(global_instance);
  107. }
  108. static void
  109. signal_term_handler(int sig)
  110. {
  111. log(LOG_DEBUG, "SIGTERM received - closing server IPC socket");
  112. qnetd_ipc_close(global_instance);
  113. }
  114. static void
  115. signal_handlers_register(void)
  116. {
  117. struct sigaction act;
  118. act.sa_handler = signal_int_handler;
  119. sigemptyset(&act.sa_mask);
  120. act.sa_flags = SA_RESTART;
  121. sigaction(SIGINT, &act, NULL);
  122. act.sa_handler = signal_term_handler;
  123. sigemptyset(&act.sa_mask);
  124. act.sa_flags = SA_RESTART;
  125. sigaction(SIGTERM, &act, NULL);
  126. }
  127. static int
  128. qnetd_run_main_loop(struct qnetd_instance *instance)
  129. {
  130. int poll_res;
  131. while ((poll_res = pr_poll_loop_exec(&instance->main_poll_loop)) == 0) {
  132. }
  133. if (poll_res == -2) {
  134. log(LOG_CRIT, "pr_poll_loop_exec returned -2 - internal error");
  135. return (-1);
  136. } else if (poll_res == -3) {
  137. log_nss(LOG_CRIT, "pr_poll_loop_exec returned -3 - PR_Poll error");
  138. return (-1);
  139. }
  140. return (qnetd_ipc_is_closed(instance) ? 0 : -1);
  141. }
  142. static void
  143. usage(void)
  144. {
  145. printf("usage: %s [-46dfhv] [-l listen_addr] [-p listen_port] [-s tls]\n", QNETD_PROGRAM_NAME);
  146. printf("%14s[-c client_cert_required] [-m max_clients] [-S option=value[,option2=value2,...]]\n", "");
  147. }
  148. static void
  149. display_version(void)
  150. {
  151. enum msg_type *supported_messages;
  152. size_t no_supported_messages;
  153. size_t zi;
  154. msg_get_supported_messages(&supported_messages, &no_supported_messages);
  155. printf("Corosync Qdevice Network Daemon, version '%s'\n\n", VERSION);
  156. printf("Supported algorithms: ");
  157. for (zi = 0; zi < QNETD_STATIC_SUPPORTED_DECISION_ALGORITHMS_SIZE; zi++) {
  158. if (zi != 0) {
  159. printf(", ");
  160. }
  161. printf("%s (%u)",
  162. tlv_decision_algorithm_type_to_str(qnetd_static_supported_decision_algorithms[zi]),
  163. qnetd_static_supported_decision_algorithms[zi]);
  164. }
  165. printf("\n");
  166. printf("Supported message types: ");
  167. for (zi = 0; zi < no_supported_messages; zi++) {
  168. if (zi != 0) {
  169. printf(", ");
  170. }
  171. printf("%s (%u)", msg_type_to_str(supported_messages[zi]), supported_messages[zi]);
  172. }
  173. printf("\n");
  174. }
  175. static void
  176. cli_parse_long_opt(struct qnetd_advanced_settings *advanced_settings, const char *long_opt)
  177. {
  178. struct dynar_getopt_lex lex;
  179. struct dynar dynar_long_opt;
  180. const char *opt;
  181. const char *val;
  182. int res;
  183. dynar_init(&dynar_long_opt, strlen(long_opt) + 1);
  184. if (dynar_str_cpy(&dynar_long_opt, long_opt) != 0) {
  185. errx(EXIT_FAILURE, "Can't alloc memory for long option");
  186. }
  187. dynar_getopt_lex_init(&lex, &dynar_long_opt);
  188. while (dynar_getopt_lex_token_next(&lex) == 0 && strcmp(dynar_data(&lex.option), "") != 0) {
  189. opt = dynar_data(&lex.option);
  190. val = dynar_data(&lex.value);
  191. res = qnetd_advanced_settings_set(advanced_settings, opt, val);
  192. switch (res) {
  193. case -1:
  194. errx(EXIT_FAILURE, "Unknown option '%s'", opt);
  195. break;
  196. case -2:
  197. errx(EXIT_FAILURE, "Invalid value '%s' for option '%s'", val, opt);
  198. break;
  199. case -3:
  200. warnx("Option '%s' is deprecated and has no effect anymore", opt);
  201. break;
  202. }
  203. }
  204. dynar_getopt_lex_destroy(&lex);
  205. dynar_destroy(&dynar_long_opt);
  206. }
  207. static void
  208. cli_parse(int argc, char * const argv[], char **host_addr, uint16_t *host_port, int *foreground,
  209. int *debug_log, int *bump_log_priority, enum tlv_tls_supported *tls_supported,
  210. int *client_cert_required, size_t *max_clients, PRIntn *address_family,
  211. struct qnetd_advanced_settings *advanced_settings)
  212. {
  213. int ch;
  214. long long int tmpll;
  215. *host_addr = NULL;
  216. *host_port = QNETD_DEFAULT_HOST_PORT;
  217. *foreground = 0;
  218. *debug_log = 0;
  219. *bump_log_priority = 0;
  220. *tls_supported = QNETD_DEFAULT_TLS_SUPPORTED;
  221. *client_cert_required = QNETD_DEFAULT_TLS_CLIENT_CERT_REQUIRED;
  222. *max_clients = QNETD_DEFAULT_MAX_CLIENTS;
  223. *address_family = PR_AF_UNSPEC;
  224. while ((ch = getopt(argc, argv, "46dfhvc:l:m:p:S:s:")) != -1) {
  225. switch (ch) {
  226. case '4':
  227. *address_family = PR_AF_INET;
  228. break;
  229. case '6':
  230. *address_family = PR_AF_INET6;
  231. break;
  232. case 'f':
  233. *foreground = 1;
  234. break;
  235. case 'd':
  236. if (*debug_log) {
  237. *bump_log_priority = 1;
  238. }
  239. *debug_log = 1;
  240. break;
  241. case 'c':
  242. if ((*client_cert_required = utils_parse_bool_str(optarg)) == -1) {
  243. errx(EXIT_FAILURE, "client_cert_required should be on/yes/1, off/no/0");
  244. }
  245. break;
  246. case 'l':
  247. *host_addr = optarg;
  248. break;
  249. case 'm':
  250. if (utils_strtonum(optarg, 0, LLONG_MAX, &tmpll) == -1) {
  251. errx(EXIT_FAILURE, "max clients value %s is invalid", optarg);
  252. }
  253. *max_clients = (size_t)tmpll;
  254. break;
  255. case 'p':
  256. if (utils_strtonum(optarg, 1, UINT16_MAX, &tmpll) == -1) {
  257. errx(EXIT_FAILURE, "host port must be in range 1-%u", UINT16_MAX);
  258. }
  259. *host_port = tmpll;
  260. break;
  261. case 'S':
  262. cli_parse_long_opt(advanced_settings, optarg);
  263. break;
  264. case 's':
  265. if (strcasecmp(optarg, "on") == 0) {
  266. *tls_supported = QNETD_DEFAULT_TLS_SUPPORTED;
  267. } else if (strcasecmp(optarg, "off") == 0) {
  268. *tls_supported = TLV_TLS_UNSUPPORTED;
  269. } else if (strcasecmp(optarg, "req") == 0) {
  270. *tls_supported = TLV_TLS_REQUIRED;
  271. } else {
  272. errx(EXIT_FAILURE, "tls must be one of on, off, req");
  273. }
  274. break;
  275. case 'v':
  276. display_version();
  277. exit(EXIT_FAILURE);
  278. break;
  279. case 'h':
  280. case '?':
  281. usage();
  282. exit(EXIT_FAILURE);
  283. break;
  284. }
  285. }
  286. }
  287. int
  288. main(int argc, char * const argv[])
  289. {
  290. struct qnetd_instance instance;
  291. struct qnetd_advanced_settings advanced_settings;
  292. char *host_addr;
  293. uint16_t host_port;
  294. int foreground;
  295. int debug_log;
  296. int bump_log_priority;
  297. enum tlv_tls_supported tls_supported;
  298. int client_cert_required;
  299. size_t max_clients;
  300. PRIntn address_family;
  301. int lock_file;
  302. int another_instance_running;
  303. int log_target;
  304. int main_loop_res;
  305. if (qnetd_advanced_settings_init(&advanced_settings) != 0) {
  306. errx(EXIT_FAILURE, "Can't alloc memory for advanced settings");
  307. }
  308. cli_parse(argc, argv, &host_addr, &host_port, &foreground, &debug_log, &bump_log_priority,
  309. &tls_supported, &client_cert_required, &max_clients, &address_family, &advanced_settings);
  310. log_target = LOG_TARGET_SYSLOG;
  311. if (foreground) {
  312. log_target |= LOG_TARGET_STDERR;
  313. }
  314. if (log_init(QNETD_PROGRAM_NAME, log_target, LOG_DAEMON) == -1) {
  315. errx(EXIT_FAILURE, "Can't initialize logging");
  316. }
  317. log_set_debug(debug_log);
  318. log_set_priority_bump(bump_log_priority);
  319. /*
  320. * Check that it's possible to open NSS dir if needed
  321. */
  322. if (nss_sock_check_db_dir((tls_supported != TLV_TLS_UNSUPPORTED ?
  323. advanced_settings.nss_db_dir : NULL)) != 0) {
  324. log_err(LOG_ERR, "Can't open NSS DB directory");
  325. return (EXIT_FAILURE);
  326. }
  327. /*
  328. * Daemonize
  329. */
  330. if (!foreground) {
  331. utils_tty_detach();
  332. }
  333. if ((lock_file = utils_flock(advanced_settings.lock_file, getpid(),
  334. &another_instance_running)) == -1) {
  335. if (another_instance_running) {
  336. log(LOG_ERR, "Another instance is running");
  337. } else {
  338. log_err(LOG_ERR, "Can't acquire lock");
  339. }
  340. return (EXIT_FAILURE);
  341. }
  342. log(LOG_DEBUG, "Initializing nss");
  343. if (nss_sock_init_nss((tls_supported != TLV_TLS_UNSUPPORTED ?
  344. advanced_settings.nss_db_dir : NULL)) != 0) {
  345. qnetd_err_nss();
  346. }
  347. if (SSL_ConfigServerSessionIDCache(0, 0, 0, NULL) != SECSuccess) {
  348. qnetd_err_nss();
  349. }
  350. if (qnetd_instance_init(&instance, tls_supported, client_cert_required,
  351. max_clients, &advanced_settings) == -1) {
  352. log(LOG_ERR, "Can't initialize qnetd");
  353. return (EXIT_FAILURE);
  354. }
  355. instance.host_addr = host_addr;
  356. instance.host_port = host_port;
  357. if (tls_supported != TLV_TLS_UNSUPPORTED && qnetd_instance_init_certs(&instance) == -1) {
  358. qnetd_err_nss();
  359. }
  360. log(LOG_DEBUG, "Initializing local socket");
  361. if (qnetd_ipc_init(&instance) != 0) {
  362. return (EXIT_FAILURE);
  363. }
  364. log(LOG_DEBUG, "Creating listening socket");
  365. instance.server.socket = nss_sock_create_listen_socket(instance.host_addr,
  366. instance.host_port, address_family);
  367. if (instance.server.socket == NULL) {
  368. qnetd_err_nss();
  369. }
  370. if (nss_sock_set_non_blocking(instance.server.socket) != 0) {
  371. qnetd_err_nss();
  372. }
  373. if (PR_Listen(instance.server.socket, instance.advanced_settings->listen_backlog) !=
  374. PR_SUCCESS) {
  375. qnetd_err_nss();
  376. }
  377. if (pr_poll_loop_add_prfd(&instance.main_poll_loop, instance.server.socket, POLLIN,
  378. NULL,
  379. server_socket_poll_loop_read_cb,
  380. NULL,
  381. server_socket_poll_loop_err_cb,
  382. &instance, NULL) != 0) {
  383. log(LOG_ERR, "Can't add server socket to main poll loop");
  384. return (EXIT_FAILURE);
  385. }
  386. global_instance = &instance;
  387. signal_handlers_register();
  388. log(LOG_DEBUG, "Registering algorithms");
  389. if (qnetd_algorithm_register_all() != 0) {
  390. return (EXIT_FAILURE);
  391. }
  392. log(LOG_DEBUG, "QNetd ready to provide service");
  393. #ifdef HAVE_LIBSYSTEMD
  394. sd_notify(0, "READY=1");
  395. #endif
  396. log(LOG_DEBUG, "Running QNetd main loop");
  397. main_loop_res = qnetd_run_main_loop(&instance);
  398. /*
  399. * Cleanup
  400. */
  401. log(LOG_DEBUG, "Destroying qnetd ipc");
  402. qnetd_ipc_destroy(&instance);
  403. log(LOG_DEBUG, "Closing server socket");
  404. if (PR_Close(instance.server.socket) != PR_SUCCESS) {
  405. qnetd_warn_nss();
  406. }
  407. CERT_DestroyCertificate(instance.server.cert);
  408. SECKEY_DestroyPrivateKey(instance.server.private_key);
  409. SSL_ClearSessionCache();
  410. SSL_ShutdownServerSessionIDCache();
  411. qnetd_instance_destroy(&instance);
  412. qnetd_advanced_settings_destroy(&advanced_settings);
  413. if (NSS_Shutdown() != SECSuccess) {
  414. qnetd_warn_nss();
  415. }
  416. if (PR_Cleanup() != PR_SUCCESS) {
  417. qnetd_warn_nss();
  418. }
  419. log(LOG_DEBUG, "Closing log");
  420. log_close();
  421. return (main_loop_res == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
  422. }