corosync-qnetd.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. /*
  2. * Copyright (c) 2015-2016 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 <config.h>
  35. #include <err.h>
  36. #include <errno.h>
  37. #include <getopt.h>
  38. #include <signal.h>
  39. #include <unistd.h>
  40. #include "nss-sock.h"
  41. #include "qnetd-algorithm.h"
  42. #include "qnetd-instance.h"
  43. #include "qnetd-log.h"
  44. #include "qnetd-client-net.h"
  45. #include "qnetd-client-msg-received.h"
  46. #include "utils.h"
  47. /*
  48. * This is global variable used for comunication with main loop and signal (calls close)
  49. */
  50. PRFileDesc *global_server_socket;
  51. enum tlv_decision_algorithm_type
  52. qnetd_static_supported_decision_algorithms[QNETD_STATIC_SUPPORTED_DECISION_ALGORITHMS_SIZE] = {
  53. TLV_DECISION_ALGORITHM_TYPE_TEST,
  54. TLV_DECISION_ALGORITHM_TYPE_FFSPLIT,
  55. TLV_DECISION_ALGORITHM_TYPE_2NODELMS,
  56. TLV_DECISION_ALGORITHM_TYPE_LMS,
  57. };
  58. static void
  59. qnetd_err_nss(void) {
  60. qnetd_log_nss(LOG_CRIT, "NSS error");
  61. exit(1);
  62. }
  63. static void
  64. qnetd_warn_nss(void) {
  65. qnetd_log_nss(LOG_WARNING, "NSS warning");
  66. }
  67. static int
  68. qnetd_poll(struct qnetd_instance *instance)
  69. {
  70. struct qnetd_client *client;
  71. struct qnetd_client *client_next;
  72. PRPollDesc *pfds;
  73. PRInt32 poll_res;
  74. int i;
  75. int client_disconnect;
  76. client = NULL;
  77. client_disconnect = 0;
  78. pfds = qnetd_poll_array_create_from_client_list(&instance->poll_array,
  79. &instance->clients, instance->server.socket, PR_POLL_READ);
  80. if (pfds == NULL) {
  81. return (-1);
  82. }
  83. if ((poll_res = PR_Poll(pfds, qnetd_poll_array_size(&instance->poll_array),
  84. timer_list_time_to_expire(&instance->main_timer_list))) >= 0) {
  85. timer_list_expire(&instance->main_timer_list);
  86. /*
  87. * Walk thru pfds array and process events
  88. */
  89. for (i = 0; i < qnetd_poll_array_size(&instance->poll_array); i++) {
  90. /*
  91. * Also traverse clients list
  92. */
  93. if (i > 0) {
  94. if (i == 1) {
  95. client = TAILQ_FIRST(&instance->clients);
  96. client_next = TAILQ_NEXT(client, entries);
  97. } else {
  98. client = client_next;
  99. client_next = TAILQ_NEXT(client, entries);
  100. }
  101. client_disconnect = client->schedule_disconnect;
  102. } else {
  103. client_disconnect = 0;
  104. }
  105. if (!client_disconnect && poll_res > 0 &&
  106. pfds[i].out_flags & PR_POLL_READ) {
  107. if (i == 0) {
  108. qnetd_client_net_accept(instance);
  109. } else {
  110. if (qnetd_client_net_read(instance, client) == -1) {
  111. client_disconnect = 1;
  112. }
  113. }
  114. }
  115. if (!client_disconnect && poll_res > 0 &&
  116. pfds[i].out_flags & PR_POLL_WRITE) {
  117. if (i == 0) {
  118. /*
  119. * Poll write on listen socket -> fatal error
  120. */
  121. qnetd_log(LOG_CRIT, "POLL_WRITE on listening socket");
  122. return (-1);
  123. } else {
  124. if (qnetd_client_net_write(instance, client) == -1) {
  125. client_disconnect = 1;
  126. }
  127. }
  128. }
  129. if (!client_disconnect && poll_res > 0 &&
  130. (pfds[i].out_flags & (PR_POLL_ERR|PR_POLL_NVAL|PR_POLL_HUP|PR_POLL_EXCEPT)) &&
  131. !(pfds[i].out_flags & (PR_POLL_READ|PR_POLL_WRITE))) {
  132. if (i == 0) {
  133. if (pfds[i].out_flags != PR_POLL_NVAL) {
  134. /*
  135. * Poll ERR on listening socket is fatal error.
  136. * POLL_NVAL is used as a signal to quit poll loop.
  137. */
  138. qnetd_log(LOG_CRIT, "POLL_ERR (%u) on listening "
  139. "socket", pfds[i].out_flags);
  140. } else {
  141. qnetd_log(LOG_DEBUG, "Listening socket is closed");
  142. }
  143. return (-1);
  144. } else {
  145. qnetd_log(LOG_DEBUG, "POLL_ERR (%u) on client socket. "
  146. "Disconnecting.", pfds[i].out_flags);
  147. client_disconnect = 1;
  148. }
  149. }
  150. /*
  151. * If client is scheduled for disconnect, disconnect it
  152. */
  153. if (client_disconnect) {
  154. qnetd_instance_client_disconnect(instance, client, 0);
  155. }
  156. }
  157. }
  158. return (0);
  159. }
  160. static void
  161. signal_int_handler(int sig)
  162. {
  163. qnetd_log(LOG_DEBUG, "SIGINT received - closing server socket");
  164. PR_Close(global_server_socket);
  165. }
  166. static void
  167. signal_term_handler(int sig)
  168. {
  169. qnetd_log(LOG_DEBUG, "SIGTERM received - closing server socket");
  170. PR_Close(global_server_socket);
  171. }
  172. static void
  173. signal_handlers_register(void)
  174. {
  175. struct sigaction act;
  176. act.sa_handler = signal_int_handler;
  177. sigemptyset(&act.sa_mask);
  178. act.sa_flags = SA_RESTART;
  179. sigaction(SIGINT, &act, NULL);
  180. act.sa_handler = signal_term_handler;
  181. sigemptyset(&act.sa_mask);
  182. act.sa_flags = SA_RESTART;
  183. sigaction(SIGTERM, &act, NULL);
  184. }
  185. static void
  186. usage(void)
  187. {
  188. printf("usage: %s [-46df] [-l listen_addr] [-p listen_port] [-s tls]\n", QNETD_PROGRAM_NAME);
  189. printf("%14s[-c client_cert_required] [-m max_clients]\n", "");
  190. }
  191. static void
  192. cli_parse(int argc, char * const argv[], char **host_addr, uint16_t *host_port, int *foreground,
  193. int *debug_log, int *bump_log_priority, enum tlv_tls_supported *tls_supported,
  194. int *client_cert_required, size_t *max_clients, PRIntn *address_family)
  195. {
  196. int ch;
  197. char *ep;
  198. long long int tmpll;
  199. *host_addr = NULL;
  200. *host_port = QNETD_DEFAULT_HOST_PORT;
  201. *foreground = 0;
  202. *debug_log = 0;
  203. *bump_log_priority = 0;
  204. *tls_supported = QNETD_DEFAULT_TLS_SUPPORTED;
  205. *client_cert_required = QNETD_DEFAULT_TLS_CLIENT_CERT_REQUIRED;
  206. *max_clients = QNETD_DEFAULT_MAX_CLIENTS;
  207. *address_family = PR_AF_UNSPEC;
  208. while ((ch = getopt(argc, argv, "46fdc:l:m:p:s:")) != -1) {
  209. switch (ch) {
  210. case '4':
  211. *address_family = PR_AF_INET;
  212. break;
  213. case '6':
  214. *address_family = PR_AF_INET6;
  215. break;
  216. case 'f':
  217. *foreground = 1;
  218. break;
  219. case 'd':
  220. if (*debug_log) {
  221. *bump_log_priority = 1;
  222. }
  223. *debug_log = 1;
  224. break;
  225. case 'c':
  226. if ((*client_cert_required = utils_parse_bool_str(optarg)) == -1) {
  227. errx(1, "client_cert_required should be on/yes/1, off/no/0");
  228. }
  229. break;
  230. case 'l':
  231. *host_addr = strdup(optarg);
  232. break;
  233. case 'm':
  234. errno = 0;
  235. tmpll = strtoll(optarg, &ep, 10);
  236. if (tmpll < 0 || errno != 0 || *ep != '\0') {
  237. errx(1, "max clients value %s is invalid", optarg);
  238. }
  239. *max_clients = (size_t)tmpll;
  240. break;
  241. case 'p':
  242. *host_port = strtol(optarg, &ep, 10);
  243. if (*host_port <= 0 || *host_port > ((uint16_t)~0) || *ep != '\0') {
  244. errx(1, "host port must be in range 0-65535");
  245. }
  246. break;
  247. case 's':
  248. if (strcasecmp(optarg, "on") == 0) {
  249. *tls_supported = QNETD_DEFAULT_TLS_SUPPORTED;
  250. } else if (strcasecmp(optarg, "off") == 0) {
  251. *tls_supported = TLV_TLS_UNSUPPORTED;
  252. } else if (strcasecmp(optarg, "req") == 0) {
  253. *tls_supported = TLV_TLS_REQUIRED;
  254. } else {
  255. errx(1, "tls must be one of on, off, req");
  256. }
  257. break;
  258. case '?':
  259. usage();
  260. exit(1);
  261. break;
  262. }
  263. }
  264. }
  265. int
  266. main(int argc, char *argv[])
  267. {
  268. struct qnetd_instance instance;
  269. char *host_addr;
  270. uint16_t host_port;
  271. int foreground;
  272. int debug_log;
  273. int bump_log_priority;
  274. enum tlv_tls_supported tls_supported;
  275. int client_cert_required;
  276. size_t max_clients;
  277. PRIntn address_family;
  278. int lock_file;
  279. cli_parse(argc, argv, &host_addr, &host_port, &foreground, &debug_log, &bump_log_priority,
  280. &tls_supported, &client_cert_required, &max_clients, &address_family);
  281. if (foreground) {
  282. qnetd_log_init(QNETD_LOG_TARGET_STDERR);
  283. } else {
  284. qnetd_log_init(QNETD_LOG_TARGET_SYSLOG);
  285. }
  286. qnetd_log_set_debug(debug_log);
  287. qnetd_log_set_priority_bump(bump_log_priority);
  288. /*
  289. * Daemonize
  290. */
  291. if (!foreground) {
  292. utils_tty_detach();
  293. }
  294. if ((lock_file = utils_flock(QNETD_LOCK_FILE, getpid(), qnetd_log_printf)) == -1) {
  295. exit(1);
  296. }
  297. qnetd_log(LOG_DEBUG, "Initializing nss");
  298. if (nss_sock_init_nss((tls_supported != TLV_TLS_UNSUPPORTED ?
  299. (char *)QNETD_NSS_DB_DIR : NULL)) != 0) {
  300. qnetd_err_nss();
  301. }
  302. if (SSL_ConfigServerSessionIDCache(0, 0, 0, NULL) != SECSuccess) {
  303. qnetd_err_nss();
  304. }
  305. if (qnetd_instance_init(&instance, QNETD_MAX_CLIENT_RECEIVE_SIZE,
  306. QNETD_MAX_CLIENT_SEND_BUFFERS, QNETD_MAX_CLIENT_SEND_SIZE,
  307. tls_supported, client_cert_required, max_clients) == -1) {
  308. qnetd_log(LOG_ERR, "Can't initialize qnetd");
  309. exit(1);
  310. }
  311. instance.host_addr = host_addr;
  312. instance.host_port = host_port;
  313. if (qnetd_instance_init_certs(&instance) == -1) {
  314. qnetd_err_nss();
  315. }
  316. qnetd_log(LOG_DEBUG, "Creating listening socket");
  317. instance.server.socket = nss_sock_create_listen_socket(instance.host_addr,
  318. instance.host_port, address_family);
  319. if (instance.server.socket == NULL) {
  320. qnetd_err_nss();
  321. }
  322. if (nss_sock_set_non_blocking(instance.server.socket) != 0) {
  323. qnetd_err_nss();
  324. }
  325. if (PR_Listen(instance.server.socket, QNETD_LISTEN_BACKLOG) != PR_SUCCESS) {
  326. qnetd_err_nss();
  327. }
  328. global_server_socket = instance.server.socket;
  329. signal_handlers_register();
  330. qnetd_log(LOG_DEBUG, "Registering algorithms");
  331. if (qnetd_algorithm_register_all() != 0) {
  332. exit(1);
  333. }
  334. qnetd_log(LOG_DEBUG, "QNetd ready to provide service");
  335. /*
  336. * MAIN LOOP
  337. */
  338. while (qnetd_poll(&instance) == 0) {
  339. }
  340. /*
  341. * Cleanup
  342. */
  343. CERT_DestroyCertificate(instance.server.cert);
  344. SECKEY_DestroyPrivateKey(instance.server.private_key);
  345. SSL_ClearSessionCache();
  346. SSL_ShutdownServerSessionIDCache();
  347. qnetd_instance_destroy(&instance);
  348. if (NSS_Shutdown() != SECSuccess) {
  349. qnetd_warn_nss();
  350. }
  351. if (PR_Cleanup() != PR_SUCCESS) {
  352. qnetd_warn_nss();
  353. }
  354. qnetd_log_close();
  355. return (0);
  356. }