corosync-qnetd.c 42 KB

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