corosync-qnetd.c 13 KB

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