corosync-qnetd.c 33 KB

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