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, 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_write_cb(PRFileDesc *prfd, void *user_data1, void *user_data2)
  89. {
  90. /*
  91. * Poll write on listen socket -> fatal error
  92. */
  93. log(LOG_CRIT, "POLL_WRITE on listening socket");
  94. return (-1);
  95. }
  96. static int
  97. server_socket_poll_loop_err_cb(PRFileDesc *prfd, short revents, void *user_data1, void *user_data2)
  98. {
  99. if (revents != POLLNVAL) {
  100. /*
  101. * Poll ERR on listening socket is fatal error.
  102. * POLL_NVAL is used as a signal to quit poll loop.
  103. */
  104. log(LOG_CRIT, "POLL_ERR (%u) on listening socket", revents);
  105. } else {
  106. log(LOG_DEBUG, "Listening socket is closed");
  107. }
  108. return (-1);
  109. }
  110. static void
  111. signal_int_handler(int sig)
  112. {
  113. log(LOG_DEBUG, "SIGINT received - closing server IPC socket");
  114. qnetd_ipc_close(global_instance);
  115. }
  116. static void
  117. signal_term_handler(int sig)
  118. {
  119. log(LOG_DEBUG, "SIGTERM received - closing server IPC socket");
  120. qnetd_ipc_close(global_instance);
  121. }
  122. static void
  123. signal_handlers_register(void)
  124. {
  125. struct sigaction act;
  126. act.sa_handler = signal_int_handler;
  127. sigemptyset(&act.sa_mask);
  128. act.sa_flags = SA_RESTART;
  129. sigaction(SIGINT, &act, NULL);
  130. act.sa_handler = signal_term_handler;
  131. sigemptyset(&act.sa_mask);
  132. act.sa_flags = SA_RESTART;
  133. sigaction(SIGTERM, &act, NULL);
  134. }
  135. static void
  136. usage(void)
  137. {
  138. printf("usage: %s [-46dfhv] [-l listen_addr] [-p listen_port] [-s tls]\n", QNETD_PROGRAM_NAME);
  139. printf("%14s[-c client_cert_required] [-m max_clients] [-S option=value[,option2=value2,...]]\n", "");
  140. }
  141. static void
  142. display_version(void)
  143. {
  144. enum msg_type *supported_messages;
  145. size_t no_supported_messages;
  146. size_t zi;
  147. msg_get_supported_messages(&supported_messages, &no_supported_messages);
  148. printf("Corosync Qdevice Network Daemon, version '%s'\n\n", VERSION);
  149. printf("Supported algorithms: ");
  150. for (zi = 0; zi < QNETD_STATIC_SUPPORTED_DECISION_ALGORITHMS_SIZE; zi++) {
  151. if (zi != 0) {
  152. printf(", ");
  153. }
  154. printf("%s (%u)",
  155. tlv_decision_algorithm_type_to_str(qnetd_static_supported_decision_algorithms[zi]),
  156. qnetd_static_supported_decision_algorithms[zi]);
  157. }
  158. printf("\n");
  159. printf("Supported message types: ");
  160. for (zi = 0; zi < no_supported_messages; zi++) {
  161. if (zi != 0) {
  162. printf(", ");
  163. }
  164. printf("%s (%u)", msg_type_to_str(supported_messages[zi]), supported_messages[zi]);
  165. }
  166. printf("\n");
  167. }
  168. static void
  169. cli_parse_long_opt(struct qnetd_advanced_settings *advanced_settings, const char *long_opt)
  170. {
  171. struct dynar_getopt_lex lex;
  172. struct dynar dynar_long_opt;
  173. const char *opt;
  174. const char *val;
  175. int res;
  176. dynar_init(&dynar_long_opt, strlen(long_opt) + 1);
  177. if (dynar_str_cpy(&dynar_long_opt, long_opt) != 0) {
  178. errx(EXIT_FAILURE, "Can't alloc memory for long option");
  179. }
  180. dynar_getopt_lex_init(&lex, &dynar_long_opt);
  181. while (dynar_getopt_lex_token_next(&lex) == 0 && strcmp(dynar_data(&lex.option), "") != 0) {
  182. opt = dynar_data(&lex.option);
  183. val = dynar_data(&lex.value);
  184. res = qnetd_advanced_settings_set(advanced_settings, opt, val);
  185. switch (res) {
  186. case -1:
  187. errx(EXIT_FAILURE, "Unknown option '%s'", opt);
  188. break;
  189. case -2:
  190. errx(EXIT_FAILURE, "Invalid value '%s' for option '%s'", val, opt);
  191. break;
  192. }
  193. }
  194. dynar_getopt_lex_destroy(&lex);
  195. dynar_destroy(&dynar_long_opt);
  196. }
  197. static void
  198. cli_parse(int argc, char * const argv[], char **host_addr, uint16_t *host_port, int *foreground,
  199. int *debug_log, int *bump_log_priority, enum tlv_tls_supported *tls_supported,
  200. int *client_cert_required, size_t *max_clients, PRIntn *address_family,
  201. struct qnetd_advanced_settings *advanced_settings)
  202. {
  203. int ch;
  204. long long int tmpll;
  205. *host_addr = NULL;
  206. *host_port = QNETD_DEFAULT_HOST_PORT;
  207. *foreground = 0;
  208. *debug_log = 0;
  209. *bump_log_priority = 0;
  210. *tls_supported = QNETD_DEFAULT_TLS_SUPPORTED;
  211. *client_cert_required = QNETD_DEFAULT_TLS_CLIENT_CERT_REQUIRED;
  212. *max_clients = QNETD_DEFAULT_MAX_CLIENTS;
  213. *address_family = PR_AF_UNSPEC;
  214. while ((ch = getopt(argc, argv, "46dfhvc:l:m:p:S:s:")) != -1) {
  215. switch (ch) {
  216. case '4':
  217. *address_family = PR_AF_INET;
  218. break;
  219. case '6':
  220. *address_family = PR_AF_INET6;
  221. break;
  222. case 'f':
  223. *foreground = 1;
  224. break;
  225. case 'd':
  226. if (*debug_log) {
  227. *bump_log_priority = 1;
  228. }
  229. *debug_log = 1;
  230. break;
  231. case 'c':
  232. if ((*client_cert_required = utils_parse_bool_str(optarg)) == -1) {
  233. errx(EXIT_FAILURE, "client_cert_required should be on/yes/1, off/no/0");
  234. }
  235. break;
  236. case 'l':
  237. free(*host_addr);
  238. *host_addr = strdup(optarg);
  239. if (*host_addr == NULL) {
  240. errx(EXIT_FAILURE, "Can't alloc memory for host addr string");
  241. }
  242. break;
  243. case 'm':
  244. if (utils_strtonum(optarg, 0, LLONG_MAX, &tmpll) == -1) {
  245. errx(EXIT_FAILURE, "max clients value %s is invalid", optarg);
  246. }
  247. *max_clients = (size_t)tmpll;
  248. break;
  249. case 'p':
  250. if (utils_strtonum(optarg, 1, UINT16_MAX, &tmpll) == -1) {
  251. errx(EXIT_FAILURE, "host port must be in range 1-%u", UINT16_MAX);
  252. }
  253. *host_port = tmpll;
  254. break;
  255. case 'S':
  256. cli_parse_long_opt(advanced_settings, optarg);
  257. break;
  258. case 's':
  259. if (strcasecmp(optarg, "on") == 0) {
  260. *tls_supported = QNETD_DEFAULT_TLS_SUPPORTED;
  261. } else if (strcasecmp(optarg, "off") == 0) {
  262. *tls_supported = TLV_TLS_UNSUPPORTED;
  263. } else if (strcasecmp(optarg, "req") == 0) {
  264. *tls_supported = TLV_TLS_REQUIRED;
  265. } else {
  266. errx(EXIT_FAILURE, "tls must be one of on, off, req");
  267. }
  268. break;
  269. case 'v':
  270. display_version();
  271. exit(EXIT_FAILURE);
  272. break;
  273. case 'h':
  274. case '?':
  275. usage();
  276. exit(EXIT_FAILURE);
  277. break;
  278. }
  279. }
  280. }
  281. int
  282. main(int argc, char * const argv[])
  283. {
  284. struct qnetd_instance instance;
  285. struct qnetd_advanced_settings advanced_settings;
  286. char *host_addr;
  287. uint16_t host_port;
  288. int foreground;
  289. int debug_log;
  290. int bump_log_priority;
  291. enum tlv_tls_supported tls_supported;
  292. int client_cert_required;
  293. size_t max_clients;
  294. PRIntn address_family;
  295. int lock_file;
  296. int another_instance_running;
  297. int log_target;
  298. int poll_res;
  299. if (qnetd_advanced_settings_init(&advanced_settings) != 0) {
  300. errx(EXIT_FAILURE, "Can't alloc memory for advanced settings");
  301. }
  302. cli_parse(argc, argv, &host_addr, &host_port, &foreground, &debug_log, &bump_log_priority,
  303. &tls_supported, &client_cert_required, &max_clients, &address_family, &advanced_settings);
  304. log_target = LOG_TARGET_SYSLOG;
  305. if (foreground) {
  306. log_target |= LOG_TARGET_STDERR;
  307. }
  308. if (log_init(QNETD_PROGRAM_NAME, log_target, LOG_DAEMON) == -1) {
  309. errx(EXIT_FAILURE, "Can't initialize logging");
  310. }
  311. log_set_debug(debug_log);
  312. log_set_priority_bump(bump_log_priority);
  313. /*
  314. * Check that it's possible to open NSS dir if needed
  315. */
  316. if (nss_sock_check_db_dir((tls_supported != TLV_TLS_UNSUPPORTED ?
  317. advanced_settings.nss_db_dir : NULL)) != 0) {
  318. log_err(LOG_ERR, "Can't open NSS DB directory");
  319. return (EXIT_FAILURE);
  320. }
  321. /*
  322. * Daemonize
  323. */
  324. if (!foreground) {
  325. utils_tty_detach();
  326. }
  327. if ((lock_file = utils_flock(advanced_settings.lock_file, getpid(),
  328. &another_instance_running)) == -1) {
  329. if (another_instance_running) {
  330. log(LOG_ERR, "Another instance is running");
  331. } else {
  332. log_err(LOG_ERR, "Can't acquire lock");
  333. }
  334. return (EXIT_FAILURE);
  335. }
  336. log(LOG_DEBUG, "Initializing nss");
  337. if (nss_sock_init_nss((tls_supported != TLV_TLS_UNSUPPORTED ?
  338. advanced_settings.nss_db_dir : NULL)) != 0) {
  339. qnetd_err_nss();
  340. }
  341. if (SSL_ConfigServerSessionIDCache(0, 0, 0, NULL) != SECSuccess) {
  342. qnetd_err_nss();
  343. }
  344. if (qnetd_instance_init(&instance, tls_supported, client_cert_required,
  345. max_clients, &advanced_settings) == -1) {
  346. log(LOG_ERR, "Can't initialize qnetd");
  347. return (EXIT_FAILURE);
  348. }
  349. instance.host_addr = host_addr;
  350. instance.host_port = host_port;
  351. if (tls_supported != TLV_TLS_UNSUPPORTED && qnetd_instance_init_certs(&instance) == -1) {
  352. qnetd_err_nss();
  353. }
  354. log(LOG_DEBUG, "Initializing local socket");
  355. if (qnetd_ipc_init(&instance) != 0) {
  356. return (EXIT_FAILURE);
  357. }
  358. log(LOG_DEBUG, "Creating listening socket");
  359. instance.server.socket = nss_sock_create_listen_socket(instance.host_addr,
  360. instance.host_port, address_family);
  361. if (instance.server.socket == NULL) {
  362. qnetd_err_nss();
  363. }
  364. if (nss_sock_set_non_blocking(instance.server.socket) != 0) {
  365. qnetd_err_nss();
  366. }
  367. if (PR_Listen(instance.server.socket, instance.advanced_settings->listen_backlog) !=
  368. PR_SUCCESS) {
  369. qnetd_err_nss();
  370. }
  371. if (pr_poll_loop_add_prfd(&instance.main_poll_loop, instance.server.socket, POLLIN,
  372. NULL,
  373. server_socket_poll_loop_read_cb,
  374. server_socket_poll_loop_write_cb,
  375. server_socket_poll_loop_err_cb,
  376. &instance, NULL) != 0) {
  377. log(LOG_ERR, "Can't add server socket to main poll loop");
  378. return (EXIT_FAILURE);
  379. }
  380. global_instance = &instance;
  381. signal_handlers_register();
  382. log(LOG_DEBUG, "Registering algorithms");
  383. if (qnetd_algorithm_register_all() != 0) {
  384. return (EXIT_FAILURE);
  385. }
  386. log(LOG_DEBUG, "QNetd ready to provide service");
  387. #ifdef HAVE_LIBSYSTEMD
  388. sd_notify(0, "READY=1");
  389. #endif
  390. /*
  391. * MAIN LOOP
  392. */
  393. while ((poll_res = pr_poll_loop_exec(&instance.main_poll_loop)) == 0) {
  394. }
  395. if (poll_res == -2) {
  396. log(LOG_CRIT, "pr_poll_loop_exec returned -2 - internal error");
  397. return (EXIT_FAILURE);
  398. } else if (poll_res == -3) {
  399. log_nss(LOG_CRIT, "pr_poll_loop_exec returned -3 - PR_Poll error");
  400. return (EXIT_FAILURE);
  401. }
  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 (EXIT_SUCCESS);
  426. }