corosync-qnetd.c 39 KB

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