corosync-qnetd.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. /*
  2. * Copyright (c) 2015 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 &
  126. (PR_POLL_ERR|PR_POLL_NVAL|PR_POLL_HUP|PR_POLL_EXCEPT)) {
  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 [-df] [-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)
  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. while ((ch = getopt(argc, argv, "fdc:l:m:p:s:")) != -1) {
  203. switch (ch) {
  204. case 'f':
  205. *foreground = 1;
  206. break;
  207. case 'd':
  208. if (*debug_log) {
  209. *bump_log_priority = 1;
  210. }
  211. *debug_log = 1;
  212. break;
  213. case 'c':
  214. if ((*client_cert_required = utils_parse_bool_str(optarg)) == -1) {
  215. errx(1, "client_cert_required should be on/yes/1, off/no/0");
  216. }
  217. break;
  218. case 'l':
  219. *host_addr = strdup(optarg);
  220. break;
  221. case 'm':
  222. errno = 0;
  223. tmpll = strtoll(optarg, &ep, 10);
  224. if (tmpll < 0 || errno != 0 || *ep != '\0') {
  225. errx(1, "max clients value %s is invalid", optarg);
  226. }
  227. *max_clients = (size_t)tmpll;
  228. break;
  229. case 'p':
  230. *host_port = strtol(optarg, &ep, 10);
  231. if (*host_port <= 0 || *host_port > ((uint16_t)~0) || *ep != '\0') {
  232. errx(1, "host port must be in range 0-65535");
  233. }
  234. break;
  235. case 's':
  236. if (strcasecmp(optarg, "on") == 0) {
  237. *tls_supported = QNETD_DEFAULT_TLS_SUPPORTED;
  238. } else if (strcasecmp(optarg, "off") == 0) {
  239. *tls_supported = TLV_TLS_UNSUPPORTED;
  240. } else if (strcasecmp(optarg, "req") == 0) {
  241. *tls_supported = TLV_TLS_REQUIRED;
  242. } else {
  243. errx(1, "tls must be one of on, off, req");
  244. }
  245. break;
  246. case '?':
  247. usage();
  248. exit(1);
  249. break;
  250. }
  251. }
  252. }
  253. int
  254. main(int argc, char *argv[])
  255. {
  256. struct qnetd_instance instance;
  257. char *host_addr;
  258. uint16_t host_port;
  259. int foreground;
  260. int debug_log;
  261. int bump_log_priority;
  262. enum tlv_tls_supported tls_supported;
  263. int client_cert_required;
  264. size_t max_clients;
  265. cli_parse(argc, argv, &host_addr, &host_port, &foreground, &debug_log, &bump_log_priority,
  266. &tls_supported, &client_cert_required, &max_clients);
  267. if (foreground) {
  268. qnetd_log_init(QNETD_LOG_TARGET_STDERR);
  269. } else {
  270. qnetd_log_init(QNETD_LOG_TARGET_SYSLOG);
  271. }
  272. qnetd_log_set_debug(debug_log);
  273. qnetd_log_set_priority_bump(bump_log_priority);
  274. /*
  275. * Daemonize
  276. */
  277. if (!foreground) {
  278. utils_tty_detach();
  279. }
  280. if (utils_flock(QNETD_LOCK_FILE, getpid(), qnetd_log_printf) != 0) {
  281. exit(1);
  282. }
  283. qnetd_log_printf(LOG_DEBUG, "Initializing nss");
  284. if (nss_sock_init_nss((tls_supported != TLV_TLS_UNSUPPORTED ?
  285. (char *)QNETD_NSS_DB_DIR : NULL)) != 0) {
  286. qnetd_err_nss();
  287. }
  288. if (SSL_ConfigServerSessionIDCache(0, 0, 0, NULL) != SECSuccess) {
  289. qnetd_err_nss();
  290. }
  291. if (qnetd_instance_init(&instance, QNETD_MAX_CLIENT_RECEIVE_SIZE,
  292. QNETD_MAX_CLIENT_SEND_BUFFERS, QNETD_MAX_CLIENT_SEND_SIZE,
  293. tls_supported, client_cert_required, max_clients) == -1) {
  294. qnetd_log(LOG_ERR, "Can't initialize qnetd");
  295. exit(1);
  296. }
  297. instance.host_addr = host_addr;
  298. instance.host_port = host_port;
  299. if (qnetd_instance_init_certs(&instance) == -1) {
  300. qnetd_err_nss();
  301. }
  302. qnetd_log_printf(LOG_DEBUG, "Creating listening socket");
  303. instance.server.socket = nss_sock_create_listen_socket(instance.host_addr,
  304. instance.host_port, PR_AF_INET6);
  305. if (instance.server.socket == NULL) {
  306. qnetd_err_nss();
  307. }
  308. if (nss_sock_set_nonblocking(instance.server.socket) != 0) {
  309. qnetd_err_nss();
  310. }
  311. if (PR_Listen(instance.server.socket, QNETD_LISTEN_BACKLOG) != PR_SUCCESS) {
  312. qnetd_err_nss();
  313. }
  314. global_server_socket = instance.server.socket;
  315. signal_handlers_register();
  316. qnetd_log_printf(LOG_DEBUG, "Registering algorithms");
  317. algorithms_register();
  318. qnetd_log_printf(LOG_DEBUG, "QNetd ready to provide service");
  319. /*
  320. * MAIN LOOP
  321. */
  322. while (qnetd_poll(&instance) == 0) {
  323. }
  324. /*
  325. * Cleanup
  326. */
  327. CERT_DestroyCertificate(instance.server.cert);
  328. SECKEY_DestroyPrivateKey(instance.server.private_key);
  329. SSL_ClearSessionCache();
  330. SSL_ShutdownServerSessionIDCache();
  331. qnetd_instance_destroy(&instance);
  332. if (NSS_Shutdown() != SECSuccess) {
  333. qnetd_warn_nss();
  334. }
  335. if (PR_Cleanup() != PR_SUCCESS) {
  336. qnetd_warn_nss();
  337. }
  338. qnetd_log_close();
  339. return (0);
  340. }