corosync-qnetd.c 39 KB

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