corosync-qnetd.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  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. free(*host_addr);
  248. *host_addr = strdup(optarg);
  249. if (*host_addr == NULL) {
  250. errx(EXIT_FAILURE, "Can't alloc memory for host addr string");
  251. }
  252. break;
  253. case 'm':
  254. if (utils_strtonum(optarg, 0, LLONG_MAX, &tmpll) == -1) {
  255. errx(EXIT_FAILURE, "max clients value %s is invalid", optarg);
  256. }
  257. *max_clients = (size_t)tmpll;
  258. break;
  259. case 'p':
  260. if (utils_strtonum(optarg, 1, UINT16_MAX, &tmpll) == -1) {
  261. errx(EXIT_FAILURE, "host port must be in range 1-%u", UINT16_MAX);
  262. }
  263. *host_port = tmpll;
  264. break;
  265. case 'S':
  266. cli_parse_long_opt(advanced_settings, optarg);
  267. break;
  268. case 's':
  269. if (strcasecmp(optarg, "on") == 0) {
  270. *tls_supported = QNETD_DEFAULT_TLS_SUPPORTED;
  271. } else if (strcasecmp(optarg, "off") == 0) {
  272. *tls_supported = TLV_TLS_UNSUPPORTED;
  273. } else if (strcasecmp(optarg, "req") == 0) {
  274. *tls_supported = TLV_TLS_REQUIRED;
  275. } else {
  276. errx(EXIT_FAILURE, "tls must be one of on, off, req");
  277. }
  278. break;
  279. case 'v':
  280. display_version();
  281. exit(EXIT_FAILURE);
  282. break;
  283. case 'h':
  284. case '?':
  285. usage();
  286. exit(EXIT_FAILURE);
  287. break;
  288. }
  289. }
  290. }
  291. int
  292. main(int argc, char * const argv[])
  293. {
  294. struct qnetd_instance instance;
  295. struct qnetd_advanced_settings advanced_settings;
  296. char *host_addr;
  297. uint16_t host_port;
  298. int foreground;
  299. int debug_log;
  300. int bump_log_priority;
  301. enum tlv_tls_supported tls_supported;
  302. int client_cert_required;
  303. size_t max_clients;
  304. PRIntn address_family;
  305. int lock_file;
  306. int another_instance_running;
  307. int log_target;
  308. int main_loop_res;
  309. if (qnetd_advanced_settings_init(&advanced_settings) != 0) {
  310. errx(EXIT_FAILURE, "Can't alloc memory for advanced settings");
  311. }
  312. cli_parse(argc, argv, &host_addr, &host_port, &foreground, &debug_log, &bump_log_priority,
  313. &tls_supported, &client_cert_required, &max_clients, &address_family, &advanced_settings);
  314. log_target = LOG_TARGET_SYSLOG;
  315. if (foreground) {
  316. log_target |= LOG_TARGET_STDERR;
  317. }
  318. if (log_init(QNETD_PROGRAM_NAME, log_target, LOG_DAEMON) == -1) {
  319. errx(EXIT_FAILURE, "Can't initialize logging");
  320. }
  321. log_set_debug(debug_log);
  322. log_set_priority_bump(bump_log_priority);
  323. /*
  324. * Check that it's possible to open NSS dir if needed
  325. */
  326. if (nss_sock_check_db_dir((tls_supported != TLV_TLS_UNSUPPORTED ?
  327. advanced_settings.nss_db_dir : NULL)) != 0) {
  328. log_err(LOG_ERR, "Can't open NSS DB directory");
  329. return (EXIT_FAILURE);
  330. }
  331. /*
  332. * Daemonize
  333. */
  334. if (!foreground) {
  335. utils_tty_detach();
  336. }
  337. if ((lock_file = utils_flock(advanced_settings.lock_file, getpid(),
  338. &another_instance_running)) == -1) {
  339. if (another_instance_running) {
  340. log(LOG_ERR, "Another instance is running");
  341. } else {
  342. log_err(LOG_ERR, "Can't acquire lock");
  343. }
  344. return (EXIT_FAILURE);
  345. }
  346. log(LOG_DEBUG, "Initializing nss");
  347. if (nss_sock_init_nss((tls_supported != TLV_TLS_UNSUPPORTED ?
  348. advanced_settings.nss_db_dir : NULL)) != 0) {
  349. qnetd_err_nss();
  350. }
  351. if (SSL_ConfigServerSessionIDCache(0, 0, 0, NULL) != SECSuccess) {
  352. qnetd_err_nss();
  353. }
  354. if (qnetd_instance_init(&instance, tls_supported, client_cert_required,
  355. max_clients, &advanced_settings) == -1) {
  356. log(LOG_ERR, "Can't initialize qnetd");
  357. return (EXIT_FAILURE);
  358. }
  359. instance.host_addr = host_addr;
  360. instance.host_port = host_port;
  361. if (tls_supported != TLV_TLS_UNSUPPORTED && qnetd_instance_init_certs(&instance) == -1) {
  362. qnetd_err_nss();
  363. }
  364. log(LOG_DEBUG, "Initializing local socket");
  365. if (qnetd_ipc_init(&instance) != 0) {
  366. return (EXIT_FAILURE);
  367. }
  368. log(LOG_DEBUG, "Creating listening socket");
  369. instance.server.socket = nss_sock_create_listen_socket(instance.host_addr,
  370. instance.host_port, address_family);
  371. if (instance.server.socket == NULL) {
  372. qnetd_err_nss();
  373. }
  374. if (nss_sock_set_non_blocking(instance.server.socket) != 0) {
  375. qnetd_err_nss();
  376. }
  377. if (PR_Listen(instance.server.socket, instance.advanced_settings->listen_backlog) !=
  378. PR_SUCCESS) {
  379. qnetd_err_nss();
  380. }
  381. if (pr_poll_loop_add_prfd(&instance.main_poll_loop, instance.server.socket, POLLIN,
  382. NULL,
  383. server_socket_poll_loop_read_cb,
  384. NULL,
  385. server_socket_poll_loop_err_cb,
  386. &instance, NULL) != 0) {
  387. log(LOG_ERR, "Can't add server socket to main poll loop");
  388. return (EXIT_FAILURE);
  389. }
  390. global_instance = &instance;
  391. signal_handlers_register();
  392. log(LOG_DEBUG, "Registering algorithms");
  393. if (qnetd_algorithm_register_all() != 0) {
  394. return (EXIT_FAILURE);
  395. }
  396. log(LOG_DEBUG, "QNetd ready to provide service");
  397. #ifdef HAVE_LIBSYSTEMD
  398. sd_notify(0, "READY=1");
  399. #endif
  400. log(LOG_DEBUG, "Running QNetd main loop");
  401. main_loop_res = qnetd_run_main_loop(&instance);
  402. /*
  403. * Cleanup
  404. */
  405. log(LOG_DEBUG, "Destroying qnetd ipc");
  406. qnetd_ipc_destroy(&instance);
  407. log(LOG_DEBUG, "Closing server socket");
  408. if (PR_Close(instance.server.socket) != PR_SUCCESS) {
  409. qnetd_warn_nss();
  410. }
  411. CERT_DestroyCertificate(instance.server.cert);
  412. SECKEY_DestroyPrivateKey(instance.server.private_key);
  413. SSL_ClearSessionCache();
  414. SSL_ShutdownServerSessionIDCache();
  415. qnetd_instance_destroy(&instance);
  416. qnetd_advanced_settings_destroy(&advanced_settings);
  417. if (NSS_Shutdown() != SECSuccess) {
  418. qnetd_warn_nss();
  419. }
  420. if (PR_Cleanup() != PR_SUCCESS) {
  421. qnetd_warn_nss();
  422. }
  423. log(LOG_DEBUG, "Closing log");
  424. log_close();
  425. return (main_loop_res == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
  426. }