corosync-qnetd.c 10 KB

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