corosync-qnetd.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512
  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 <stdio.h>
  36. #include <nss.h>
  37. #include <pk11func.h>
  38. #include <certt.h>
  39. #include <ssl.h>
  40. #include <prio.h>
  41. #include <prnetdb.h>
  42. #include <prerror.h>
  43. #include <prinit.h>
  44. #include <getopt.h>
  45. #include <err.h>
  46. #include <keyhi.h>
  47. #include <syslog.h>
  48. #include <signal.h>
  49. #include "qnet-config.h"
  50. #include "msg.h"
  51. #include "msgio.h"
  52. #include "tlv.h"
  53. #include "nss-sock.h"
  54. #include "qnetd-client.h"
  55. #include "qnetd-client-list.h"
  56. #include "qnetd-poll-array.h"
  57. #include "qnetd-log.h"
  58. #include "dynar.h"
  59. #include "timer-list.h"
  60. #include "qnetd-algorithm.h"
  61. #include "qnetd-cluster-list.h"
  62. #include "qnetd-client-send.h"
  63. struct qnetd_instance {
  64. struct {
  65. PRFileDesc *socket;
  66. CERTCertificate *cert;
  67. SECKEYPrivateKey *private_key;
  68. } server;
  69. size_t max_client_receive_size;
  70. size_t max_client_send_buffers;
  71. size_t max_client_send_size;
  72. struct qnetd_client_list clients;
  73. struct qnetd_cluster_list clusters;
  74. struct qnetd_poll_array poll_array;
  75. enum tlv_tls_supported tls_supported;
  76. int tls_client_cert_required;
  77. const char *host_addr;
  78. uint16_t host_port;
  79. };
  80. /*
  81. * This is global variable used for comunication with main loop and signal (calls close)
  82. */
  83. PRFileDesc *global_server_socket;
  84. /*
  85. * Decision algorithms supported in this server
  86. */
  87. #define QNETD_STATIC_SUPPORTED_DECISION_ALGORITHMS_SIZE 4
  88. enum tlv_decision_algorithm_type
  89. qnetd_static_supported_decision_algorithms[QNETD_STATIC_SUPPORTED_DECISION_ALGORITHMS_SIZE] = {
  90. TLV_DECISION_ALGORITHM_TYPE_TEST,
  91. TLV_DECISION_ALGORITHM_TYPE_FFSPLIT,
  92. TLV_DECISION_ALGORITHM_TYPE_2NODELMS,
  93. TLV_DECISION_ALGORITHM_TYPE_LMS,
  94. };
  95. static void
  96. qnetd_err_nss(void) {
  97. qnetd_log_nss(LOG_CRIT, "NSS error");
  98. exit(1);
  99. }
  100. static void
  101. qnetd_warn_nss(void) {
  102. qnetd_log_nss(LOG_WARNING, "NSS warning");
  103. }
  104. static void
  105. qnetd_client_log_msg_decode_error(int ret)
  106. {
  107. switch (ret) {
  108. case -1:
  109. qnetd_log(LOG_WARNING, "Received message with option with invalid length");
  110. break;
  111. case -2:
  112. qnetd_log(LOG_CRIT, "Can't allocate memory");
  113. break;
  114. case -3:
  115. qnetd_log(LOG_WARNING, "Received inconsistent msg (tlv len > msg size)");
  116. break;
  117. case -4:
  118. qnetd_log(LOG_WARNING, "Received message with option with invalid value");
  119. break;
  120. default:
  121. qnetd_log(LOG_ERR, "Unknown error occured when decoding message");
  122. break;
  123. }
  124. }
  125. static int
  126. qnetd_client_msg_received_preinit(struct qnetd_instance *instance, struct qnetd_client *client,
  127. const struct msg_decoded *msg)
  128. {
  129. struct send_buffer_list_entry *send_buffer;
  130. if (msg->cluster_name == NULL) {
  131. qnetd_log(LOG_ERR, "Received preinit message without cluster name. "
  132. "Sending error reply.");
  133. if (qnetd_client_send_err(client, msg->seq_number_set, msg->seq_number,
  134. TLV_REPLY_ERROR_CODE_DOESNT_CONTAIN_REQUIRED_OPTION) != 0) {
  135. return (-1);
  136. }
  137. return (0);
  138. }
  139. client->cluster_name = malloc(msg->cluster_name_len + 1);
  140. if (client->cluster_name == NULL) {
  141. qnetd_log(LOG_ERR, "Can't allocate cluster name. Sending error reply.");
  142. if (qnetd_client_send_err(client, msg->seq_number_set, msg->seq_number,
  143. TLV_REPLY_ERROR_CODE_INTERNAL_ERROR) != 0) {
  144. return (-1);
  145. }
  146. return (0);
  147. }
  148. memset(client->cluster_name, 0, msg->cluster_name_len + 1);
  149. memcpy(client->cluster_name, msg->cluster_name, msg->cluster_name_len);
  150. client->cluster_name_len = msg->cluster_name_len;
  151. client->preinit_received = 1;
  152. send_buffer = send_buffer_list_get_new(&client->send_buffer_list);
  153. if (send_buffer == NULL) {
  154. qnetd_log(LOG_ERR, "Can't alloc preinit reply msg from list. "
  155. "Disconnecting client connection.");
  156. return (-1);
  157. }
  158. if (msg_create_preinit_reply(&send_buffer->buffer, msg->seq_number_set, msg->seq_number,
  159. instance->tls_supported, instance->tls_client_cert_required) == 0) {
  160. qnetd_log(LOG_ERR, "Can't alloc preinit reply msg. "
  161. "Disconnecting client connection.");
  162. return (-1);
  163. };
  164. send_buffer_list_put(&client->send_buffer_list, send_buffer);
  165. return (0);
  166. }
  167. static int
  168. qnetd_client_msg_received_unexpected_msg(struct qnetd_client *client,
  169. const struct msg_decoded *msg, const char *msg_str)
  170. {
  171. qnetd_log(LOG_ERR, "Received %s message. Sending back error message", msg_str);
  172. if (qnetd_client_send_err(client, msg->seq_number_set, msg->seq_number,
  173. TLV_REPLY_ERROR_CODE_UNEXPECTED_MESSAGE) != 0) {
  174. return (-1);
  175. }
  176. return (0);
  177. }
  178. static int
  179. qnetd_client_msg_received_preinit_reply(struct qnetd_instance *instance,
  180. struct qnetd_client *client, const struct msg_decoded *msg)
  181. {
  182. return (qnetd_client_msg_received_unexpected_msg(client, msg, "preinit reply"));
  183. }
  184. static int
  185. qnetd_client_msg_received_starttls(struct qnetd_instance *instance, struct qnetd_client *client,
  186. const struct msg_decoded *msg)
  187. {
  188. PRFileDesc *new_pr_fd;
  189. if (!client->preinit_received) {
  190. qnetd_log(LOG_ERR, "Received starttls before preinit message. "
  191. "Sending error reply.");
  192. if (qnetd_client_send_err(client, msg->seq_number_set, msg->seq_number,
  193. TLV_REPLY_ERROR_CODE_PREINIT_REQUIRED) != 0) {
  194. return (-1);
  195. }
  196. return (0);
  197. }
  198. if ((new_pr_fd = nss_sock_start_ssl_as_server(client->socket, instance->server.cert,
  199. instance->server.private_key, instance->tls_client_cert_required, 0, NULL)) == NULL) {
  200. qnetd_log_nss(LOG_ERR, "Can't start TLS. Disconnecting client.");
  201. return (-1);
  202. }
  203. client->tls_started = 1;
  204. client->tls_peer_certificate_verified = 0;
  205. client->socket = new_pr_fd;
  206. return (0);
  207. }
  208. static int
  209. qnetd_client_msg_received_server_error(struct qnetd_instance *instance, struct qnetd_client *client,
  210. const struct msg_decoded *msg)
  211. {
  212. return (qnetd_client_msg_received_unexpected_msg(client, msg, "server error"));
  213. }
  214. /*
  215. * 0 - Success
  216. * -1 - Disconnect client
  217. * -2 - Error reply sent, but no need to disconnect client
  218. */
  219. static int
  220. qnetd_client_check_tls(struct qnetd_instance *instance, struct qnetd_client *client,
  221. const struct msg_decoded *msg)
  222. {
  223. int check_certificate;
  224. int tls_required;
  225. CERTCertificate *peer_cert;
  226. check_certificate = 0;
  227. tls_required = 0;
  228. switch (instance->tls_supported) {
  229. case TLV_TLS_UNSUPPORTED:
  230. tls_required = 0;
  231. check_certificate = 0;
  232. break;
  233. case TLV_TLS_SUPPORTED:
  234. tls_required = 0;
  235. if (client->tls_started && instance->tls_client_cert_required &&
  236. !client->tls_peer_certificate_verified) {
  237. check_certificate = 1;
  238. }
  239. break;
  240. case TLV_TLS_REQUIRED:
  241. tls_required = 1;
  242. if (instance->tls_client_cert_required && !client->tls_peer_certificate_verified) {
  243. check_certificate = 1;
  244. }
  245. break;
  246. default:
  247. errx(1, "Unhandled instance tls supported %u\n", instance->tls_supported);
  248. break;
  249. }
  250. if (tls_required && !client->tls_started) {
  251. qnetd_log(LOG_ERR, "TLS is required but doesn't started yet. "
  252. "Sending back error message");
  253. if (qnetd_client_send_err(client, msg->seq_number_set, msg->seq_number,
  254. TLV_REPLY_ERROR_CODE_TLS_REQUIRED) != 0) {
  255. return (-1);
  256. }
  257. return (-2);
  258. }
  259. if (check_certificate) {
  260. peer_cert = SSL_PeerCertificate(client->socket);
  261. if (peer_cert == NULL) {
  262. qnetd_log(LOG_ERR, "Client doesn't sent valid certificate. "
  263. "Disconnecting client");
  264. return (-1);
  265. }
  266. if (CERT_VerifyCertName(peer_cert, client->cluster_name) != SECSuccess) {
  267. qnetd_log(LOG_ERR, "Client doesn't sent certificate with valid CN. "
  268. "Disconnecting client");
  269. CERT_DestroyCertificate(peer_cert);
  270. return (-1);
  271. }
  272. CERT_DestroyCertificate(peer_cert);
  273. client->tls_peer_certificate_verified = 1;
  274. }
  275. return (0);
  276. }
  277. static int
  278. qnetd_client_msg_received_init(struct qnetd_instance *instance, struct qnetd_client *client,
  279. const struct msg_decoded *msg)
  280. {
  281. int res;
  282. size_t zi;
  283. enum msg_type *supported_msgs;
  284. size_t no_supported_msgs;
  285. enum tlv_opt_type *supported_opts;
  286. size_t no_supported_opts;
  287. struct send_buffer_list_entry *send_buffer;
  288. enum tlv_reply_error_code reply_error_code;
  289. struct qnetd_cluster *cluster;
  290. supported_msgs = NULL;
  291. supported_opts = NULL;
  292. no_supported_msgs = 0;
  293. no_supported_opts = 0;
  294. reply_error_code = TLV_REPLY_ERROR_CODE_NO_ERROR;
  295. if ((res = qnetd_client_check_tls(instance, client, msg)) != 0) {
  296. return (res == -1 ? -1 : 0);
  297. }
  298. if (!client->preinit_received) {
  299. qnetd_log(LOG_ERR, "Received init before preinit message. Sending error reply.");
  300. reply_error_code = TLV_REPLY_ERROR_CODE_PREINIT_REQUIRED;
  301. }
  302. if (reply_error_code == TLV_REPLY_ERROR_CODE_NO_ERROR && !msg->node_id_set) {
  303. qnetd_log(LOG_ERR, "Received init message without node id set. "
  304. "Sending error reply.");
  305. reply_error_code = TLV_REPLY_ERROR_CODE_DOESNT_CONTAIN_REQUIRED_OPTION;
  306. } else {
  307. client->node_id_set = 1;
  308. client->node_id = msg->node_id;
  309. }
  310. if (msg->supported_messages != NULL) {
  311. /*
  312. * Client sent supported messages. For now this is ignored but in the future
  313. * this may be used to ensure backward compatibility.
  314. */
  315. /*
  316. for (i = 0; i < msg->no_supported_messages; i++) {
  317. qnetd_log(LOG_DEBUG, "Client supports %u message",
  318. (int)msg->supported_messages[i]);
  319. }
  320. */
  321. /*
  322. * Sent back supported messages
  323. */
  324. msg_get_supported_messages(&supported_msgs, &no_supported_msgs);
  325. }
  326. if (msg->supported_options != NULL) {
  327. /*
  328. * Client sent supported options. For now this is ignored but in the future
  329. * this may be used to ensure backward compatibility.
  330. */
  331. /*
  332. for (i = 0; i < msg->no_supported_options; i++) {
  333. qnetd_log(LOG_DEBUG, "Client supports %u option",
  334. (int)msg->supported_messages[i]);
  335. }
  336. */
  337. /*
  338. * Send back supported options
  339. */
  340. tlv_get_supported_options(&supported_opts, &no_supported_opts);
  341. }
  342. if (reply_error_code == TLV_REPLY_ERROR_CODE_NO_ERROR && !msg->decision_algorithm_set) {
  343. qnetd_log(LOG_ERR, "Received init message without decision algorithm. "
  344. "Sending error reply.");
  345. reply_error_code = TLV_REPLY_ERROR_CODE_DOESNT_CONTAIN_REQUIRED_OPTION;
  346. } else {
  347. /*
  348. * Check if decision algorithm requested by client is supported
  349. */
  350. res = 0;
  351. for (zi = 0; zi < QNETD_STATIC_SUPPORTED_DECISION_ALGORITHMS_SIZE && !res; zi++) {
  352. if (qnetd_static_supported_decision_algorithms[zi] ==
  353. msg->decision_algorithm) {
  354. res = 1;
  355. }
  356. }
  357. if (!res) {
  358. qnetd_log(LOG_ERR, "Client requested unsupported decision algorithm %u. "
  359. "Sending error reply.", msg->decision_algorithm);
  360. reply_error_code = TLV_REPLY_ERROR_CODE_UNSUPPORTED_DECISION_ALGORITHM;
  361. }
  362. client->decision_algorithm = msg->decision_algorithm;
  363. }
  364. if (reply_error_code == TLV_REPLY_ERROR_CODE_NO_ERROR) {
  365. cluster = qnetd_cluster_list_add_client(&instance->clusters, client);
  366. if (cluster == NULL) {
  367. qnetd_log(LOG_ERR, "Can't add client to cluster list. "
  368. "Sending error reply.");
  369. reply_error_code = TLV_REPLY_ERROR_CODE_INTERNAL_ERROR;
  370. } else {
  371. client->cluster = cluster;
  372. }
  373. }
  374. if (reply_error_code == TLV_REPLY_ERROR_CODE_NO_ERROR) {
  375. reply_error_code = qnetd_algorithm_client_init(client);
  376. }
  377. if (reply_error_code == TLV_REPLY_ERROR_CODE_NO_ERROR) {
  378. /*
  379. * Correct init received
  380. */
  381. client->init_received = 1;
  382. }
  383. send_buffer = send_buffer_list_get_new(&client->send_buffer_list);
  384. if (send_buffer == NULL) {
  385. qnetd_log(LOG_ERR, "Can't alloc init reply msg from list. "
  386. "Disconnecting client connection.");
  387. return (-1);
  388. }
  389. if (msg_create_init_reply(&send_buffer->buffer, msg->seq_number_set, msg->seq_number,
  390. reply_error_code,
  391. supported_msgs, no_supported_msgs, supported_opts, no_supported_opts,
  392. instance->max_client_receive_size, instance->max_client_send_size,
  393. qnetd_static_supported_decision_algorithms,
  394. QNETD_STATIC_SUPPORTED_DECISION_ALGORITHMS_SIZE) == -1) {
  395. qnetd_log(LOG_ERR, "Can't alloc init reply msg. Disconnecting client connection.");
  396. return (-1);
  397. }
  398. send_buffer_list_put(&client->send_buffer_list, send_buffer);
  399. return (0);
  400. }
  401. static int
  402. qnetd_client_msg_received_init_reply(struct qnetd_instance *instance, struct qnetd_client *client,
  403. const struct msg_decoded *msg)
  404. {
  405. return (qnetd_client_msg_received_unexpected_msg(client, msg, "init reply"));
  406. }
  407. static int
  408. qnetd_client_msg_received_set_option_reply(struct qnetd_instance *instance,
  409. struct qnetd_client *client, const struct msg_decoded *msg)
  410. {
  411. return (qnetd_client_msg_received_unexpected_msg(client, msg, "set option reply"));
  412. }
  413. static int
  414. qnetd_client_msg_received_set_option(struct qnetd_instance *instance, struct qnetd_client *client,
  415. const struct msg_decoded *msg)
  416. {
  417. int res;
  418. struct send_buffer_list_entry *send_buffer;
  419. if ((res = qnetd_client_check_tls(instance, client, msg)) != 0) {
  420. return (res == -1 ? -1 : 0);
  421. }
  422. if (!client->init_received) {
  423. qnetd_log(LOG_ERR, "Received set option message before init message. "
  424. "Sending error reply.");
  425. if (qnetd_client_send_err(client, msg->seq_number_set, msg->seq_number,
  426. TLV_REPLY_ERROR_CODE_INIT_REQUIRED) != 0) {
  427. return (-1);
  428. }
  429. return (0);
  430. }
  431. if (msg->heartbeat_interval_set) {
  432. /*
  433. * Check if heartbeat interval is valid
  434. */
  435. if (msg->heartbeat_interval != 0 &&
  436. (msg->heartbeat_interval < QNETD_HEARTBEAT_INTERVAL_MIN ||
  437. msg->heartbeat_interval > QNETD_HEARTBEAT_INTERVAL_MAX)) {
  438. qnetd_log(LOG_ERR, "Client requested invalid heartbeat interval %u. "
  439. "Sending error reply.", msg->heartbeat_interval);
  440. if (qnetd_client_send_err(client, msg->seq_number_set, msg->seq_number,
  441. TLV_REPLY_ERROR_CODE_INVALID_HEARTBEAT_INTERVAL) != 0) {
  442. return (-1);
  443. }
  444. return (0);
  445. }
  446. client->heartbeat_interval = msg->heartbeat_interval;
  447. }
  448. send_buffer = send_buffer_list_get_new(&client->send_buffer_list);
  449. if (send_buffer == NULL) {
  450. qnetd_log(LOG_ERR, "Can't alloc set option reply msg from list. "
  451. "Disconnecting client connection.");
  452. return (-1);
  453. }
  454. if (msg_create_set_option_reply(&send_buffer->buffer, msg->seq_number_set, msg->seq_number,
  455. client->decision_algorithm, client->heartbeat_interval) == -1) {
  456. qnetd_log(LOG_ERR, "Can't alloc set option reply msg. "
  457. "Disconnecting client connection.");
  458. return (-1);
  459. }
  460. send_buffer_list_put(&client->send_buffer_list, send_buffer);
  461. return (0);
  462. }
  463. static int
  464. qnetd_client_msg_received_echo_reply(struct qnetd_instance *instance, struct qnetd_client *client,
  465. const struct msg_decoded *msg)
  466. {
  467. return (qnetd_client_msg_received_unexpected_msg(client, msg, "echo reply"));
  468. }
  469. static int
  470. qnetd_client_msg_received_echo_request(struct qnetd_instance *instance, struct qnetd_client *client,
  471. const struct msg_decoded *msg, const struct dynar *msg_orig)
  472. {
  473. int res;
  474. struct send_buffer_list_entry *send_buffer;
  475. if ((res = qnetd_client_check_tls(instance, client, msg)) != 0) {
  476. return (res == -1 ? -1 : 0);
  477. }
  478. if (!client->init_received) {
  479. qnetd_log(LOG_ERR, "Received echo request before init message. "
  480. "Sending error reply.");
  481. if (qnetd_client_send_err(client, msg->seq_number_set, msg->seq_number,
  482. TLV_REPLY_ERROR_CODE_INIT_REQUIRED) != 0) {
  483. return (-1);
  484. }
  485. return (0);
  486. }
  487. send_buffer = send_buffer_list_get_new(&client->send_buffer_list);
  488. if (send_buffer == NULL) {
  489. qnetd_log(LOG_ERR, "Can't alloc echo reply msg from list. "
  490. "Disconnecting client connection.");
  491. return (-1);
  492. }
  493. if (msg_create_echo_reply(&send_buffer->buffer, msg_orig) == -1) {
  494. qnetd_log(LOG_ERR, "Can't alloc echo reply msg. Disconnecting client connection.");
  495. return (-1);
  496. }
  497. send_buffer_list_put(&client->send_buffer_list, send_buffer);
  498. return (0);
  499. }
  500. static int
  501. qnetd_client_msg_received_node_list(struct qnetd_instance *instance, struct qnetd_client *client,
  502. const struct msg_decoded *msg)
  503. {
  504. int res;
  505. struct send_buffer_list_entry *send_buffer;
  506. enum tlv_reply_error_code reply_error_code;
  507. enum tlv_vote result_vote;
  508. reply_error_code = TLV_REPLY_ERROR_CODE_NO_ERROR;
  509. if ((res = qnetd_client_check_tls(instance, client, msg)) != 0) {
  510. return (res == -1 ? -1 : 0);
  511. }
  512. if (!client->init_received) {
  513. qnetd_log(LOG_ERR, "Received node list message before init message. "
  514. "Sending error reply.");
  515. if (qnetd_client_send_err(client, msg->seq_number_set, msg->seq_number,
  516. TLV_REPLY_ERROR_CODE_INIT_REQUIRED) != 0) {
  517. return (-1);
  518. }
  519. return (0);
  520. }
  521. if (!msg->node_list_type_set) {
  522. qnetd_log(LOG_ERR, "Received node list message without node list type set. "
  523. "Sending error reply.");
  524. if (qnetd_client_send_err(client, msg->seq_number_set, msg->seq_number,
  525. TLV_REPLY_ERROR_CODE_DOESNT_CONTAIN_REQUIRED_OPTION) != 0) {
  526. return (-1);
  527. }
  528. return (0);
  529. }
  530. if (!msg->seq_number_set) {
  531. qnetd_log(LOG_ERR, "Received node list message without seq number set. "
  532. "Sending error reply.");
  533. if (qnetd_client_send_err(client, msg->seq_number_set, msg->seq_number,
  534. TLV_REPLY_ERROR_CODE_DOESNT_CONTAIN_REQUIRED_OPTION) != 0) {
  535. return (-1);
  536. }
  537. return (0);
  538. }
  539. switch (msg->node_list_type) {
  540. case TLV_NODE_LIST_TYPE_INITIAL_CONFIG:
  541. case TLV_NODE_LIST_TYPE_CHANGED_CONFIG:
  542. reply_error_code = qnetd_algorithm_config_node_list_received(client,
  543. msg->seq_number, msg->config_version_set, msg->config_version,
  544. &msg->nodes,
  545. (msg->node_list_type == TLV_NODE_LIST_TYPE_INITIAL_CONFIG),
  546. &result_vote);
  547. break;
  548. case TLV_NODE_LIST_TYPE_MEMBERSHIP:
  549. if (!msg->ring_id_set) {
  550. qnetd_log(LOG_ERR, "Received node list message without ring id number set. "
  551. "Sending error reply.");
  552. if (qnetd_client_send_err(client, msg->seq_number_set, msg->seq_number,
  553. TLV_REPLY_ERROR_CODE_DOESNT_CONTAIN_REQUIRED_OPTION) != 0) {
  554. return (-1);
  555. }
  556. return (0);
  557. }
  558. if (!msg->quorate_set) {
  559. qnetd_log(LOG_ERR, "Received node list message without quorate set. "
  560. "Sending error reply.");
  561. if (qnetd_client_send_err(client, msg->seq_number_set, msg->seq_number,
  562. TLV_REPLY_ERROR_CODE_DOESNT_CONTAIN_REQUIRED_OPTION) != 0) {
  563. return (-1);
  564. }
  565. return (0);
  566. }
  567. reply_error_code = qnetd_algorithm_membership_node_list_received(client,
  568. msg->seq_number, msg->config_version_set, msg->config_version,
  569. &msg->ring_id, msg->quorate, &msg->nodes, &result_vote);
  570. break;
  571. default:
  572. errx(1, "qnetd_client_msg_received_node_list fatal error. "
  573. "Unhandled node_list_type");
  574. break;
  575. }
  576. if (reply_error_code != TLV_REPLY_ERROR_CODE_NO_ERROR) {
  577. qnetd_log(LOG_ERR, "Algorithm returned error code. "
  578. "Sending error reply.");
  579. if (qnetd_client_send_err(client, msg->seq_number_set, msg->seq_number,
  580. reply_error_code) != 0) {
  581. return (-1);
  582. }
  583. return (0);
  584. }
  585. /*
  586. * Store node list for future use
  587. */
  588. switch (msg->node_list_type) {
  589. case TLV_NODE_LIST_TYPE_INITIAL_CONFIG:
  590. case TLV_NODE_LIST_TYPE_CHANGED_CONFIG:
  591. node_list_free(&client->configuration_node_list);
  592. if (node_list_clone(&client->configuration_node_list, &msg->nodes) == -1) {
  593. qnetd_log(LOG_ERR, "Can't alloc config node list clone. "
  594. "Disconnecting client connection.");
  595. return (-1);
  596. }
  597. break;
  598. case TLV_NODE_LIST_TYPE_MEMBERSHIP:
  599. node_list_free(&client->last_membership_node_list);
  600. if (node_list_clone(&client->last_membership_node_list, &msg->nodes) == -1) {
  601. qnetd_log(LOG_ERR, "Can't alloc membership node list clone. "
  602. "Disconnecting client connection.");
  603. return (-1);
  604. }
  605. break;
  606. default:
  607. errx(1, "qnetd_client_msg_received_node_list fatal error. "
  608. "Unhandled node_list_type");
  609. break;
  610. }
  611. send_buffer = send_buffer_list_get_new(&client->send_buffer_list);
  612. if (send_buffer == NULL) {
  613. qnetd_log(LOG_ERR, "Can't alloc node list reply msg from list. "
  614. "Disconnecting client connection.");
  615. return (-1);
  616. }
  617. if (msg_create_node_list_reply(&send_buffer->buffer, msg->seq_number, result_vote) == -1) {
  618. qnetd_log(LOG_ERR, "Can't alloc node list reply msg. "
  619. "Disconnecting client connection.");
  620. return (-1);
  621. }
  622. send_buffer_list_put(&client->send_buffer_list, send_buffer);
  623. return (0);
  624. }
  625. static int
  626. qnetd_client_msg_received_node_list_reply(struct qnetd_instance *instance,
  627. struct qnetd_client *client, const struct msg_decoded *msg)
  628. {
  629. return (qnetd_client_msg_received_unexpected_msg(client, msg, "node list reply"));
  630. }
  631. static int
  632. qnetd_client_msg_received_ask_for_vote(struct qnetd_instance *instance, struct qnetd_client *client,
  633. const struct msg_decoded *msg)
  634. {
  635. int res;
  636. struct send_buffer_list_entry *send_buffer;
  637. enum tlv_reply_error_code reply_error_code;
  638. enum tlv_vote result_vote;
  639. reply_error_code = TLV_REPLY_ERROR_CODE_NO_ERROR;
  640. if ((res = qnetd_client_check_tls(instance, client, msg)) != 0) {
  641. return (res == -1 ? -1 : 0);
  642. }
  643. if (!client->init_received) {
  644. qnetd_log(LOG_ERR, "Received ask for vote message before init message. "
  645. "Sending error reply.");
  646. if (qnetd_client_send_err(client, msg->seq_number_set, msg->seq_number,
  647. TLV_REPLY_ERROR_CODE_INIT_REQUIRED) != 0) {
  648. return (-1);
  649. }
  650. return (0);
  651. }
  652. if (!msg->seq_number_set) {
  653. qnetd_log(LOG_ERR, "Received ask for vote message without seq number set. "
  654. "Sending error reply.");
  655. if (qnetd_client_send_err(client, msg->seq_number_set, msg->seq_number,
  656. TLV_REPLY_ERROR_CODE_DOESNT_CONTAIN_REQUIRED_OPTION) != 0) {
  657. return (-1);
  658. }
  659. return (0);
  660. }
  661. reply_error_code = qnetd_algorithm_ask_for_vote_received(client, msg->seq_number,
  662. &result_vote);
  663. if (reply_error_code != TLV_REPLY_ERROR_CODE_NO_ERROR) {
  664. qnetd_log(LOG_ERR, "Algorithm returned error code. "
  665. "Sending error reply.");
  666. if (qnetd_client_send_err(client, msg->seq_number_set, msg->seq_number,
  667. reply_error_code) != 0) {
  668. return (-1);
  669. }
  670. return (0);
  671. }
  672. send_buffer = send_buffer_list_get_new(&client->send_buffer_list);
  673. if (send_buffer == NULL) {
  674. qnetd_log(LOG_ERR, "Can't alloc ask for vote reply msg from list. "
  675. "Disconnecting client connection.");
  676. return (-1);
  677. }
  678. if (msg_create_ask_for_vote_reply(&send_buffer->buffer, msg->seq_number,
  679. result_vote) == -1) {
  680. qnetd_log(LOG_ERR, "Can't alloc ask for vote reply msg. "
  681. "Disconnecting client connection.");
  682. return (-1);
  683. }
  684. send_buffer_list_put(&client->send_buffer_list, send_buffer);
  685. return (0);
  686. }
  687. static int
  688. qnetd_client_msg_received_ask_for_vote_reply(struct qnetd_instance *instance,
  689. struct qnetd_client *client, const struct msg_decoded *msg)
  690. {
  691. return (qnetd_client_msg_received_unexpected_msg(client, msg, "ask for vote reply"));
  692. }
  693. static int
  694. qnetd_client_msg_received_vote_info(struct qnetd_instance *instance, struct qnetd_client *client,
  695. const struct msg_decoded *msg)
  696. {
  697. return (qnetd_client_msg_received_unexpected_msg(client, msg, "vote info"));
  698. }
  699. static int
  700. qnetd_client_msg_received_vote_info_reply(struct qnetd_instance *instance,
  701. struct qnetd_client *client, const struct msg_decoded *msg)
  702. {
  703. int res;
  704. enum tlv_reply_error_code reply_error_code;
  705. enum tlv_vote result_vote;
  706. reply_error_code = TLV_REPLY_ERROR_CODE_NO_ERROR;
  707. if ((res = qnetd_client_check_tls(instance, client, msg)) != 0) {
  708. return (res == -1 ? -1 : 0);
  709. }
  710. if (!client->init_received) {
  711. qnetd_log(LOG_ERR, "Received vote info reply before init message. "
  712. "Sending error reply.");
  713. if (qnetd_client_send_err(client, msg->seq_number_set, msg->seq_number,
  714. TLV_REPLY_ERROR_CODE_INIT_REQUIRED) != 0) {
  715. return (-1);
  716. }
  717. return (0);
  718. }
  719. if (!msg->seq_number_set) {
  720. qnetd_log(LOG_ERR, "Received vote info reply message without seq number set. "
  721. "Sending error reply.");
  722. if (qnetd_client_send_err(client, msg->seq_number_set, msg->seq_number,
  723. TLV_REPLY_ERROR_CODE_DOESNT_CONTAIN_REQUIRED_OPTION) != 0) {
  724. return (-1);
  725. }
  726. return (0);
  727. }
  728. reply_error_code = qnetd_algorithm_membership_node_list_received(client,
  729. msg->seq_number, msg->config_version_set, msg->config_version,
  730. &msg->ring_id, msg->quorate, &msg->nodes, &result_vote);
  731. if (reply_error_code != TLV_REPLY_ERROR_CODE_NO_ERROR) {
  732. qnetd_log(LOG_ERR, "Algorithm returned error code. "
  733. "Sending error reply.");
  734. if (qnetd_client_send_err(client, msg->seq_number_set, msg->seq_number,
  735. reply_error_code) != 0) {
  736. return (-1);
  737. }
  738. return (0);
  739. }
  740. return (0);
  741. }
  742. static int
  743. qnetd_client_msg_received(struct qnetd_instance *instance, struct qnetd_client *client)
  744. {
  745. struct msg_decoded msg;
  746. int res;
  747. int ret_val;
  748. msg_decoded_init(&msg);
  749. res = msg_decode(&client->receive_buffer, &msg);
  750. if (res != 0) {
  751. /*
  752. * Error occurred. Send server error.
  753. */
  754. qnetd_client_log_msg_decode_error(res);
  755. qnetd_log(LOG_INFO, "Sending back error message");
  756. if (qnetd_client_send_err(client, msg.seq_number_set, msg.seq_number,
  757. TLV_REPLY_ERROR_CODE_ERROR_DECODING_MSG) != 0) {
  758. return (-1);
  759. }
  760. return (0);
  761. }
  762. ret_val = 0;
  763. switch (msg.type) {
  764. case MSG_TYPE_PREINIT:
  765. ret_val = qnetd_client_msg_received_preinit(instance, client, &msg);
  766. break;
  767. case MSG_TYPE_PREINIT_REPLY:
  768. ret_val = qnetd_client_msg_received_preinit_reply(instance, client, &msg);
  769. break;
  770. case MSG_TYPE_STARTTLS:
  771. ret_val = qnetd_client_msg_received_starttls(instance, client, &msg);
  772. break;
  773. case MSG_TYPE_INIT:
  774. ret_val = qnetd_client_msg_received_init(instance, client, &msg);
  775. break;
  776. case MSG_TYPE_INIT_REPLY:
  777. ret_val = qnetd_client_msg_received_init_reply(instance, client, &msg);
  778. break;
  779. case MSG_TYPE_SERVER_ERROR:
  780. ret_val = qnetd_client_msg_received_server_error(instance, client, &msg);
  781. break;
  782. case MSG_TYPE_SET_OPTION:
  783. ret_val = qnetd_client_msg_received_set_option(instance, client, &msg);
  784. break;
  785. case MSG_TYPE_SET_OPTION_REPLY:
  786. ret_val = qnetd_client_msg_received_set_option_reply(instance, client, &msg);
  787. break;
  788. case MSG_TYPE_ECHO_REQUEST:
  789. ret_val = qnetd_client_msg_received_echo_request(instance, client, &msg,
  790. &client->receive_buffer);
  791. break;
  792. case MSG_TYPE_ECHO_REPLY:
  793. ret_val = qnetd_client_msg_received_echo_reply(instance, client, &msg);
  794. break;
  795. case MSG_TYPE_NODE_LIST:
  796. ret_val = qnetd_client_msg_received_node_list(instance, client, &msg);
  797. break;
  798. case MSG_TYPE_NODE_LIST_REPLY:
  799. ret_val = qnetd_client_msg_received_node_list_reply(instance, client, &msg);
  800. break;
  801. case MSG_TYPE_ASK_FOR_VOTE:
  802. ret_val = qnetd_client_msg_received_ask_for_vote(instance, client, &msg);
  803. break;
  804. case MSG_TYPE_ASK_FOR_VOTE_REPLY:
  805. ret_val = qnetd_client_msg_received_ask_for_vote_reply(instance, client, &msg);
  806. break;
  807. case MSG_TYPE_VOTE_INFO:
  808. ret_val = qnetd_client_msg_received_vote_info(instance, client, &msg);
  809. break;
  810. case MSG_TYPE_VOTE_INFO_REPLY:
  811. ret_val = qnetd_client_msg_received_vote_info_reply(instance, client, &msg);
  812. break;
  813. default:
  814. qnetd_log(LOG_ERR, "Unsupported message %u received from client. "
  815. "Sending back error message", msg.type);
  816. if (qnetd_client_send_err(client, msg.seq_number_set, msg.seq_number,
  817. TLV_REPLY_ERROR_CODE_UNSUPPORTED_MESSAGE) != 0) {
  818. ret_val = -1;
  819. }
  820. break;
  821. }
  822. msg_decoded_destroy(&msg);
  823. return (ret_val);
  824. }
  825. static int
  826. qnetd_client_net_write_finished(struct qnetd_instance *instance, struct qnetd_client *client)
  827. {
  828. /*
  829. * Callback is currently unused
  830. */
  831. return (0);
  832. }
  833. static int
  834. qnetd_client_net_write(struct qnetd_instance *instance, struct qnetd_client *client)
  835. {
  836. int res;
  837. struct send_buffer_list_entry *send_buffer;
  838. send_buffer = send_buffer_list_get_active(&client->send_buffer_list);
  839. if (send_buffer == NULL) {
  840. qnetd_log_nss(LOG_CRIT, "send_buffer_list_get_active returned NULL");
  841. return (-1);
  842. }
  843. res = msgio_write(client->socket, &send_buffer->buffer,
  844. &send_buffer->msg_already_sent_bytes);
  845. if (res == 1) {
  846. send_buffer_list_delete(&client->send_buffer_list, send_buffer);
  847. if (qnetd_client_net_write_finished(instance, client) == -1) {
  848. return (-1);
  849. }
  850. }
  851. if (res == -1) {
  852. qnetd_log_nss(LOG_CRIT, "PR_Send returned 0");
  853. return (-1);
  854. }
  855. if (res == -2) {
  856. qnetd_log_nss(LOG_ERR, "Unhandled error when sending message to client");
  857. return (-1);
  858. }
  859. return (0);
  860. }
  861. /*
  862. * -1 means end of connection (EOF) or some other unhandled error. 0 = success
  863. */
  864. static int
  865. qnetd_client_net_read(struct qnetd_instance *instance, struct qnetd_client *client)
  866. {
  867. int res;
  868. int ret_val;
  869. int orig_skipping_msg;
  870. orig_skipping_msg = client->skipping_msg;
  871. res = msgio_read(client->socket, &client->receive_buffer,
  872. &client->msg_already_received_bytes, &client->skipping_msg);
  873. if (!orig_skipping_msg && client->skipping_msg) {
  874. qnetd_log(LOG_DEBUG, "msgio_read set skipping_msg");
  875. }
  876. ret_val = 0;
  877. switch (res) {
  878. case 0:
  879. /*
  880. * Partial read
  881. */
  882. break;
  883. case -1:
  884. qnetd_log(LOG_DEBUG, "Client closed connection");
  885. ret_val = -1;
  886. break;
  887. case -2:
  888. qnetd_log_nss(LOG_ERR, "Unhandled error when reading from client. "
  889. "Disconnecting client");
  890. ret_val = -1;
  891. break;
  892. case -3:
  893. qnetd_log(LOG_ERR, "Can't store message header from client. Disconnecting client");
  894. ret_val = -1;
  895. break;
  896. case -4:
  897. qnetd_log(LOG_ERR, "Can't store message from client. Skipping message");
  898. client->skipping_msg_reason = TLV_REPLY_ERROR_CODE_ERROR_DECODING_MSG;
  899. break;
  900. case -5:
  901. qnetd_log(LOG_WARNING, "Client sent unsupported msg type %u. Skipping message",
  902. msg_get_type(&client->receive_buffer));
  903. client->skipping_msg_reason = TLV_REPLY_ERROR_CODE_UNSUPPORTED_MESSAGE;
  904. break;
  905. case -6:
  906. qnetd_log(LOG_WARNING,
  907. "Client wants to send too long message %u bytes. Skipping message",
  908. msg_get_len(&client->receive_buffer));
  909. client->skipping_msg_reason = TLV_REPLY_ERROR_CODE_MESSAGE_TOO_LONG;
  910. break;
  911. case 1:
  912. /*
  913. * Full message received / skipped
  914. */
  915. if (!client->skipping_msg) {
  916. if (qnetd_client_msg_received(instance, client) == -1) {
  917. ret_val = -1;
  918. }
  919. } else {
  920. if (qnetd_client_send_err(client, 0, 0, client->skipping_msg_reason) != 0) {
  921. ret_val = -1;
  922. }
  923. }
  924. client->skipping_msg = 0;
  925. client->skipping_msg_reason = TLV_REPLY_ERROR_CODE_NO_ERROR;
  926. client->msg_already_received_bytes = 0;
  927. dynar_clean(&client->receive_buffer);
  928. break;
  929. default:
  930. errx(1, "Unhandled msgio_read error %d\n", res);
  931. break;
  932. }
  933. return (ret_val);
  934. }
  935. static int
  936. qnetd_client_accept(struct qnetd_instance *instance)
  937. {
  938. PRNetAddr client_addr;
  939. PRFileDesc *client_socket;
  940. struct qnetd_client *client;
  941. if ((client_socket = PR_Accept(instance->server.socket, &client_addr,
  942. PR_INTERVAL_NO_TIMEOUT)) == NULL) {
  943. qnetd_log_nss(LOG_ERR, "Can't accept connection");
  944. return (-1);
  945. }
  946. if (nss_sock_set_nonblocking(client_socket) != 0) {
  947. qnetd_log_nss(LOG_ERR, "Can't set client socket to non blocking mode");
  948. PR_Close(client_socket);
  949. return (-1);
  950. }
  951. client = qnetd_client_list_add(&instance->clients, client_socket, &client_addr,
  952. instance->max_client_receive_size, instance->max_client_send_buffers,
  953. instance->max_client_send_size);
  954. if (client == NULL) {
  955. qnetd_log(LOG_ERR, "Can't add client to list");
  956. PR_Close(client_socket);
  957. return (-2);
  958. }
  959. return (0);
  960. }
  961. static void
  962. qnetd_client_disconnect(struct qnetd_instance *instance, struct qnetd_client *client,
  963. int server_going_down)
  964. {
  965. qnetd_algorithm_client_disconnect(client, server_going_down);
  966. PR_Close(client->socket);
  967. if (client->cluster != NULL) {
  968. qnetd_cluster_list_del_client(&instance->clusters, client->cluster, client);
  969. }
  970. qnetd_client_list_del(&instance->clients, client);
  971. }
  972. static int
  973. qnetd_poll(struct qnetd_instance *instance)
  974. {
  975. struct qnetd_client *client;
  976. struct qnetd_client *client_next;
  977. PRPollDesc *pfds;
  978. PRInt32 poll_res;
  979. int i;
  980. int client_disconnect;
  981. client = NULL;
  982. client_disconnect = 0;
  983. pfds = qnetd_poll_array_create_from_client_list(&instance->poll_array,
  984. &instance->clients, instance->server.socket, PR_POLL_READ);
  985. if (pfds == NULL) {
  986. return (-1);
  987. }
  988. if ((poll_res = PR_Poll(pfds, qnetd_poll_array_size(&instance->poll_array),
  989. PR_INTERVAL_NO_TIMEOUT)) > 0) {
  990. /*
  991. * Walk thru pfds array and process events
  992. */
  993. for (i = 0; i < qnetd_poll_array_size(&instance->poll_array); i++) {
  994. /*
  995. * Also traverse clients list
  996. */
  997. if (i > 0) {
  998. if (i == 1) {
  999. client = TAILQ_FIRST(&instance->clients);
  1000. client_next = TAILQ_NEXT(client, entries);
  1001. } else {
  1002. client = client_next;
  1003. client_next = TAILQ_NEXT(client, entries);
  1004. }
  1005. }
  1006. client_disconnect = 0;
  1007. if (!client_disconnect && pfds[i].out_flags & PR_POLL_READ) {
  1008. if (i == 0) {
  1009. qnetd_client_accept(instance);
  1010. } else {
  1011. if (qnetd_client_net_read(instance, client) == -1) {
  1012. client_disconnect = 1;
  1013. }
  1014. }
  1015. }
  1016. if (!client_disconnect && pfds[i].out_flags & PR_POLL_WRITE) {
  1017. if (i == 0) {
  1018. /*
  1019. * Poll write on listen socket -> fatal error
  1020. */
  1021. qnetd_log(LOG_CRIT, "POLL_WRITE on listening socket");
  1022. return (-1);
  1023. } else {
  1024. if (qnetd_client_net_write(instance, client) == -1) {
  1025. client_disconnect = 1;
  1026. }
  1027. }
  1028. }
  1029. if (!client_disconnect &&
  1030. pfds[i].out_flags &
  1031. (PR_POLL_ERR|PR_POLL_NVAL|PR_POLL_HUP|PR_POLL_EXCEPT)) {
  1032. if (i == 0) {
  1033. if (pfds[i].out_flags != PR_POLL_NVAL) {
  1034. /*
  1035. * Poll ERR on listening socket is fatal error.
  1036. * POLL_NVAL is used as a signal to quit poll loop.
  1037. */
  1038. qnetd_log(LOG_CRIT, "POLL_ERR (%u) on listening "
  1039. "socket", pfds[i].out_flags);
  1040. } else {
  1041. qnetd_log(LOG_DEBUG, "Listening socket is closed");
  1042. }
  1043. return (-1);
  1044. } else {
  1045. qnetd_log(LOG_DEBUG, "POLL_ERR (%u) on client socket. "
  1046. "Disconnecting.", pfds[i].out_flags);
  1047. client_disconnect = 1;
  1048. }
  1049. }
  1050. /*
  1051. * If client is scheduled for disconnect, disconnect it
  1052. */
  1053. if (client_disconnect) {
  1054. qnetd_client_disconnect(instance, client, 0);
  1055. }
  1056. }
  1057. }
  1058. return (0);
  1059. }
  1060. static int
  1061. qnetd_instance_init_certs(struct qnetd_instance *instance)
  1062. {
  1063. instance->server.cert = PK11_FindCertFromNickname(QNETD_CERT_NICKNAME, NULL);
  1064. if (instance->server.cert == NULL) {
  1065. return (-1);
  1066. }
  1067. instance->server.private_key = PK11_FindKeyByAnyCert(instance->server.cert, NULL);
  1068. if (instance->server.private_key == NULL) {
  1069. return (-1);
  1070. }
  1071. return (0);
  1072. }
  1073. static int
  1074. qnetd_instance_init(struct qnetd_instance *instance, size_t max_client_receive_size,
  1075. size_t max_client_send_buffers, size_t max_client_send_size,
  1076. enum tlv_tls_supported tls_supported, int tls_client_cert_required)
  1077. {
  1078. memset(instance, 0, sizeof(*instance));
  1079. qnetd_poll_array_init(&instance->poll_array);
  1080. qnetd_client_list_init(&instance->clients);
  1081. qnetd_cluster_list_init(&instance->clusters);
  1082. instance->max_client_receive_size = max_client_receive_size;
  1083. instance->max_client_send_buffers = max_client_send_buffers;
  1084. instance->max_client_send_size = max_client_send_size;
  1085. instance->tls_supported = tls_supported;
  1086. instance->tls_client_cert_required = tls_client_cert_required;
  1087. return (0);
  1088. }
  1089. static int
  1090. qnetd_instance_destroy(struct qnetd_instance *instance)
  1091. {
  1092. struct qnetd_client *client;
  1093. struct qnetd_client *client_next;
  1094. client = TAILQ_FIRST(&instance->clients);
  1095. while (client != NULL) {
  1096. client_next = TAILQ_NEXT(client, entries);
  1097. qnetd_client_disconnect(instance, client, 1);
  1098. client = client_next;
  1099. }
  1100. qnetd_poll_array_destroy(&instance->poll_array);
  1101. qnetd_cluster_list_free(&instance->clusters);
  1102. qnetd_client_list_free(&instance->clients);
  1103. return (0);
  1104. }
  1105. static void
  1106. signal_int_handler(int sig)
  1107. {
  1108. qnetd_log(LOG_DEBUG, "SIGINT received - closing server socket");
  1109. PR_Close(global_server_socket);
  1110. }
  1111. static void
  1112. signal_handlers_register(void)
  1113. {
  1114. struct sigaction act;
  1115. act.sa_handler = signal_int_handler;
  1116. sigemptyset(&act.sa_mask);
  1117. act.sa_flags = SA_RESTART;
  1118. sigaction(SIGINT, &act, NULL);
  1119. }
  1120. static void
  1121. usage(void)
  1122. {
  1123. printf("usage: %s [-h listen_addr] [-p listen_port]\n", QNETD_PROGRAM_NAME);
  1124. }
  1125. static void
  1126. cli_parse(int argc, char * const argv[], char **host_addr, uint16_t *host_port)
  1127. {
  1128. int ch;
  1129. char *ep;
  1130. *host_addr = NULL;
  1131. *host_port = QNETD_DEFAULT_HOST_PORT;
  1132. while ((ch = getopt(argc, argv, "h:p:")) != -1) {
  1133. switch (ch) {
  1134. case 'h':
  1135. *host_addr = strdup(optarg);
  1136. break;
  1137. case 'p':
  1138. *host_port = strtol(optarg, &ep, 10);
  1139. if (*host_port <= 0 || *host_port > ((uint16_t)~0) || *ep != '\0') {
  1140. errx(1, "host port must be in range 0-65535");
  1141. }
  1142. break;
  1143. case '?':
  1144. usage();
  1145. exit(1);
  1146. break;
  1147. }
  1148. }
  1149. }
  1150. int
  1151. main(int argc, char *argv[])
  1152. {
  1153. struct qnetd_instance instance;
  1154. char *host_addr;
  1155. uint16_t host_port;
  1156. /*
  1157. * INIT
  1158. */
  1159. qnetd_log_init(QNETD_LOG_TARGET_STDERR);
  1160. qnetd_log_set_debug(1);
  1161. if (nss_sock_init_nss((char *)QNETD_NSS_DB_DIR) != 0) {
  1162. qnetd_err_nss();
  1163. }
  1164. if (SSL_ConfigServerSessionIDCache(0, 0, 0, NULL) != SECSuccess) {
  1165. qnetd_err_nss();
  1166. }
  1167. cli_parse(argc, argv, &host_addr, &host_port);
  1168. if (qnetd_instance_init(&instance, QNETD_MAX_CLIENT_RECEIVE_SIZE,
  1169. QNETD_MAX_CLIENT_SEND_BUFFERS, QNETD_MAX_CLIENT_SEND_SIZE,
  1170. QNETD_TLS_SUPPORTED, QNETD_TLS_CLIENT_CERT_REQUIRED) == -1) {
  1171. errx(1, "Can't initialize qnetd");
  1172. }
  1173. instance.host_addr = host_addr;
  1174. instance.host_port = host_port;
  1175. if (qnetd_instance_init_certs(&instance) == -1) {
  1176. qnetd_err_nss();
  1177. }
  1178. instance.server.socket = nss_sock_create_listen_socket(instance.host_addr,
  1179. instance.host_port, PR_AF_INET6);
  1180. if (instance.server.socket == NULL) {
  1181. qnetd_err_nss();
  1182. }
  1183. if (nss_sock_set_nonblocking(instance.server.socket) != 0) {
  1184. qnetd_err_nss();
  1185. }
  1186. if (PR_Listen(instance.server.socket, QNETD_LISTEN_BACKLOG) != PR_SUCCESS) {
  1187. qnetd_err_nss();
  1188. }
  1189. global_server_socket = instance.server.socket;
  1190. signal_handlers_register();
  1191. algorithms_register();
  1192. /*
  1193. * MAIN LOOP
  1194. */
  1195. while (qnetd_poll(&instance) == 0) {
  1196. }
  1197. /*
  1198. * Cleanup
  1199. */
  1200. CERT_DestroyCertificate(instance.server.cert);
  1201. SECKEY_DestroyPrivateKey(instance.server.private_key);
  1202. SSL_ClearSessionCache();
  1203. SSL_ShutdownServerSessionIDCache();
  1204. qnetd_instance_destroy(&instance);
  1205. if (NSS_Shutdown() != SECSuccess) {
  1206. qnetd_warn_nss();
  1207. }
  1208. if (PR_Cleanup() != PR_SUCCESS) {
  1209. qnetd_warn_nss();
  1210. }
  1211. qnetd_log_close();
  1212. return (0);
  1213. }