corosync-qnetd.c 28 KB

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