corosync-qnetd.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389
  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. if (!msg->seq_number_set) {
  557. qnetd_log(LOG_ERR, "Received node list message without seq number set. "
  558. "Sending error reply.");
  559. if (qnetd_client_send_err(client, msg->seq_number_set, msg->seq_number,
  560. TLV_REPLY_ERROR_CODE_DOESNT_CONTAIN_REQUIRED_OPTION) != 0) {
  561. return (-1);
  562. }
  563. return (0);
  564. }
  565. switch (msg->node_list_type) {
  566. case TLV_NODE_LIST_TYPE_INITIAL_CONFIG:
  567. case TLV_NODE_LIST_TYPE_CHANGED_CONFIG:
  568. reply_error_code = qnetd_algorithm_config_node_list_received(client,
  569. msg->seq_number, msg->config_version_set, msg->config_version,
  570. &msg->nodes,
  571. (msg->node_list_type == TLV_NODE_LIST_TYPE_INITIAL_CONFIG),
  572. &result_vote);
  573. break;
  574. case TLV_NODE_LIST_TYPE_MEMBERSHIP:
  575. if (!msg->ring_id_set) {
  576. qnetd_log(LOG_ERR, "Received node list message without ring id number set. "
  577. "Sending error reply.");
  578. if (qnetd_client_send_err(client, msg->seq_number_set, msg->seq_number,
  579. TLV_REPLY_ERROR_CODE_DOESNT_CONTAIN_REQUIRED_OPTION) != 0) {
  580. return (-1);
  581. }
  582. return (0);
  583. }
  584. if (!msg->quorate_set) {
  585. qnetd_log(LOG_ERR, "Received node list message without quorate set. "
  586. "Sending error reply.");
  587. if (qnetd_client_send_err(client, msg->seq_number_set, msg->seq_number,
  588. TLV_REPLY_ERROR_CODE_DOESNT_CONTAIN_REQUIRED_OPTION) != 0) {
  589. return (-1);
  590. }
  591. return (0);
  592. }
  593. reply_error_code = qnetd_algorithm_membership_node_list_received(client,
  594. msg->seq_number, msg->config_version_set, msg->config_version,
  595. &msg->ring_id, msg->quorate, &msg->nodes, &result_vote);
  596. break;
  597. default:
  598. errx(1, "qnetd_client_msg_received_node_list fatal error. "
  599. "Unhandled node_list_type");
  600. break;
  601. }
  602. if (reply_error_code != TLV_REPLY_ERROR_CODE_NO_ERROR) {
  603. qnetd_log(LOG_ERR, "Algorithm returned error code. "
  604. "Sending error reply.");
  605. if (qnetd_client_send_err(client, msg->seq_number_set, msg->seq_number,
  606. reply_error_code) != 0) {
  607. return (-1);
  608. }
  609. return (0);
  610. }
  611. /*
  612. * Store node list for future use
  613. */
  614. switch (msg->node_list_type) {
  615. case TLV_NODE_LIST_TYPE_INITIAL_CONFIG:
  616. case TLV_NODE_LIST_TYPE_CHANGED_CONFIG:
  617. node_list_free(&client->configuration_node_list);
  618. if (node_list_clone(&client->configuration_node_list, &msg->nodes) == -1) {
  619. qnetd_log(LOG_ERR, "Can't alloc config node list clone. "
  620. "Disconnecting client connection.");
  621. return (-1);
  622. }
  623. break;
  624. case TLV_NODE_LIST_TYPE_MEMBERSHIP:
  625. node_list_free(&client->last_membership_node_list);
  626. if (node_list_clone(&client->last_membership_node_list, &msg->nodes) == -1) {
  627. qnetd_log(LOG_ERR, "Can't alloc membership node list clone. "
  628. "Disconnecting client connection.");
  629. return (-1);
  630. }
  631. break;
  632. default:
  633. errx(1, "qnetd_client_msg_received_node_list fatal error. "
  634. "Unhandled node_list_type");
  635. break;
  636. }
  637. send_buffer = send_buffer_list_get_new(&client->send_buffer_list);
  638. if (send_buffer == NULL) {
  639. qnetd_log(LOG_ERR, "Can't alloc node list reply msg from list. "
  640. "Disconnecting client connection.");
  641. return (-1);
  642. }
  643. if (msg_create_node_list_reply(&send_buffer->buffer, msg->seq_number, result_vote) == -1) {
  644. qnetd_log(LOG_ERR, "Can't alloc node list reply msg. "
  645. "Disconnecting client connection.");
  646. return (-1);
  647. }
  648. send_buffer_list_put(&client->send_buffer_list, send_buffer);
  649. return (0);
  650. }
  651. static int
  652. qnetd_client_msg_received_node_list_reply(struct qnetd_instance *instance, struct qnetd_client *client,
  653. const struct msg_decoded *msg)
  654. {
  655. qnetd_log(LOG_ERR, "Received node list reply. Sending back error message");
  656. if (qnetd_client_send_err(client, msg->seq_number_set, msg->seq_number,
  657. TLV_REPLY_ERROR_CODE_UNEXPECTED_MESSAGE) != 0) {
  658. return (-1);
  659. }
  660. return (0);
  661. }
  662. static int
  663. qnetd_client_msg_received(struct qnetd_instance *instance, struct qnetd_client *client)
  664. {
  665. struct msg_decoded msg;
  666. int res;
  667. int ret_val;
  668. msg_decoded_init(&msg);
  669. res = msg_decode(&client->receive_buffer, &msg);
  670. if (res != 0) {
  671. /*
  672. * Error occurred. Send server error.
  673. */
  674. qnetd_client_log_msg_decode_error(res);
  675. qnetd_log(LOG_INFO, "Sending back error message");
  676. if (qnetd_client_send_err(client, msg.seq_number_set, msg.seq_number,
  677. TLV_REPLY_ERROR_CODE_ERROR_DECODING_MSG) != 0) {
  678. return (-1);
  679. }
  680. return (0);
  681. }
  682. ret_val = 0;
  683. switch (msg.type) {
  684. case MSG_TYPE_PREINIT:
  685. ret_val = qnetd_client_msg_received_preinit(instance, client, &msg);
  686. break;
  687. case MSG_TYPE_PREINIT_REPLY:
  688. ret_val = qnetd_client_msg_received_preinit_reply(instance, client, &msg);
  689. break;
  690. case MSG_TYPE_STARTTLS:
  691. ret_val = qnetd_client_msg_received_starttls(instance, client, &msg);
  692. break;
  693. case MSG_TYPE_INIT:
  694. ret_val = qnetd_client_msg_received_init(instance, client, &msg);
  695. break;
  696. case MSG_TYPE_INIT_REPLY:
  697. ret_val = qnetd_client_msg_received_init_reply(instance, client, &msg);
  698. break;
  699. case MSG_TYPE_SERVER_ERROR:
  700. ret_val = qnetd_client_msg_received_server_error(instance, client, &msg);
  701. break;
  702. case MSG_TYPE_SET_OPTION:
  703. ret_val = qnetd_client_msg_received_set_option(instance, client, &msg);
  704. break;
  705. case MSG_TYPE_SET_OPTION_REPLY:
  706. ret_val = qnetd_client_msg_received_set_option_reply(instance, client, &msg);
  707. break;
  708. case MSG_TYPE_ECHO_REQUEST:
  709. ret_val = qnetd_client_msg_received_echo_request(instance, client, &msg,
  710. &client->receive_buffer);
  711. break;
  712. case MSG_TYPE_ECHO_REPLY:
  713. ret_val = qnetd_client_msg_received_echo_reply(instance, client, &msg);
  714. break;
  715. case MSG_TYPE_NODE_LIST:
  716. ret_val = qnetd_client_msg_received_node_list(instance, client, &msg);
  717. break;
  718. case MSG_TYPE_NODE_LIST_REPLY:
  719. ret_val = qnetd_client_msg_received_node_list_reply(instance, client, &msg);
  720. break;
  721. default:
  722. qnetd_log(LOG_ERR, "Unsupported message %u received from client. "
  723. "Sending back error message", msg.type);
  724. if (qnetd_client_send_err(client, msg.seq_number_set, msg.seq_number,
  725. TLV_REPLY_ERROR_CODE_UNSUPPORTED_MESSAGE) != 0) {
  726. ret_val = -1;
  727. }
  728. break;
  729. }
  730. msg_decoded_destroy(&msg);
  731. return (ret_val);
  732. }
  733. static int
  734. qnetd_client_net_write_finished(struct qnetd_instance *instance, struct qnetd_client *client)
  735. {
  736. /*
  737. * Callback is currently unused
  738. */
  739. return (0);
  740. }
  741. static int
  742. qnetd_client_net_write(struct qnetd_instance *instance, struct qnetd_client *client)
  743. {
  744. int res;
  745. struct send_buffer_list_entry *send_buffer;
  746. send_buffer = send_buffer_list_get_active(&client->send_buffer_list);
  747. if (send_buffer == NULL) {
  748. qnetd_log_nss(LOG_CRIT, "send_buffer_list_get_active returned NULL");
  749. return (-1);
  750. }
  751. res = msgio_write(client->socket, &send_buffer->buffer,
  752. &send_buffer->msg_already_sent_bytes);
  753. if (res == 1) {
  754. send_buffer_list_delete(&client->send_buffer_list, send_buffer);
  755. if (qnetd_client_net_write_finished(instance, client) == -1) {
  756. return (-1);
  757. }
  758. }
  759. if (res == -1) {
  760. qnetd_log_nss(LOG_CRIT, "PR_Send returned 0");
  761. return (-1);
  762. }
  763. if (res == -2) {
  764. qnetd_log_nss(LOG_ERR, "Unhandled error when sending message to client");
  765. return (-1);
  766. }
  767. return (0);
  768. }
  769. /*
  770. * -1 means end of connection (EOF) or some other unhandled error. 0 = success
  771. */
  772. static int
  773. qnetd_client_net_read(struct qnetd_instance *instance, struct qnetd_client *client)
  774. {
  775. int res;
  776. int ret_val;
  777. int orig_skipping_msg;
  778. orig_skipping_msg = client->skipping_msg;
  779. res = msgio_read(client->socket, &client->receive_buffer,
  780. &client->msg_already_received_bytes, &client->skipping_msg);
  781. if (!orig_skipping_msg && client->skipping_msg) {
  782. qnetd_log(LOG_DEBUG, "msgio_read set skipping_msg");
  783. }
  784. ret_val = 0;
  785. switch (res) {
  786. case 0:
  787. /*
  788. * Partial read
  789. */
  790. break;
  791. case -1:
  792. qnetd_log(LOG_DEBUG, "Client closed connection");
  793. ret_val = -1;
  794. break;
  795. case -2:
  796. qnetd_log_nss(LOG_ERR, "Unhandled error when reading from client. "
  797. "Disconnecting client");
  798. ret_val = -1;
  799. break;
  800. case -3:
  801. qnetd_log(LOG_ERR, "Can't store message header from client. Disconnecting client");
  802. ret_val = -1;
  803. break;
  804. case -4:
  805. qnetd_log(LOG_ERR, "Can't store message from client. Skipping message");
  806. client->skipping_msg_reason = TLV_REPLY_ERROR_CODE_ERROR_DECODING_MSG;
  807. break;
  808. case -5:
  809. qnetd_log(LOG_WARNING, "Client sent unsupported msg type %u. Skipping message",
  810. msg_get_type(&client->receive_buffer));
  811. client->skipping_msg_reason = TLV_REPLY_ERROR_CODE_UNSUPPORTED_MESSAGE;
  812. break;
  813. case -6:
  814. qnetd_log(LOG_WARNING,
  815. "Client wants to send too long message %u bytes. Skipping message",
  816. msg_get_len(&client->receive_buffer));
  817. client->skipping_msg_reason = TLV_REPLY_ERROR_CODE_MESSAGE_TOO_LONG;
  818. break;
  819. case 1:
  820. /*
  821. * Full message received / skipped
  822. */
  823. if (!client->skipping_msg) {
  824. if (qnetd_client_msg_received(instance, client) == -1) {
  825. ret_val = -1;
  826. }
  827. } else {
  828. if (qnetd_client_send_err(client, 0, 0, client->skipping_msg_reason) != 0) {
  829. ret_val = -1;
  830. }
  831. }
  832. client->skipping_msg = 0;
  833. client->skipping_msg_reason = TLV_REPLY_ERROR_CODE_NO_ERROR;
  834. client->msg_already_received_bytes = 0;
  835. dynar_clean(&client->receive_buffer);
  836. break;
  837. default:
  838. errx(1, "Unhandled msgio_read error %d\n", res);
  839. break;
  840. }
  841. return (ret_val);
  842. }
  843. static int
  844. qnetd_client_accept(struct qnetd_instance *instance)
  845. {
  846. PRNetAddr client_addr;
  847. PRFileDesc *client_socket;
  848. struct qnetd_client *client;
  849. if ((client_socket = PR_Accept(instance->server.socket, &client_addr,
  850. PR_INTERVAL_NO_TIMEOUT)) == NULL) {
  851. qnetd_log_nss(LOG_ERR, "Can't accept connection");
  852. return (-1);
  853. }
  854. if (nss_sock_set_nonblocking(client_socket) != 0) {
  855. qnetd_log_nss(LOG_ERR, "Can't set client socket to non blocking mode");
  856. PR_Close(client_socket);
  857. return (-1);
  858. }
  859. client = qnetd_client_list_add(&instance->clients, client_socket, &client_addr,
  860. instance->max_client_receive_size, instance->max_client_send_buffers,
  861. instance->max_client_send_size);
  862. if (client == NULL) {
  863. qnetd_log(LOG_ERR, "Can't add client to list");
  864. PR_Close(client_socket);
  865. return (-2);
  866. }
  867. return (0);
  868. }
  869. static void
  870. qnetd_client_disconnect(struct qnetd_instance *instance, struct qnetd_client *client,
  871. int server_going_down)
  872. {
  873. qnetd_algorithm_client_disconnect(client, server_going_down);
  874. PR_Close(client->socket);
  875. qnetd_client_list_del(&instance->clients, client);
  876. }
  877. static int
  878. qnetd_poll(struct qnetd_instance *instance)
  879. {
  880. struct qnetd_client *client;
  881. struct qnetd_client *client_next;
  882. PRPollDesc *pfds;
  883. PRInt32 poll_res;
  884. int i;
  885. int client_disconnect;
  886. client = NULL;
  887. client_disconnect = 0;
  888. pfds = qnetd_poll_array_create_from_client_list(&instance->poll_array,
  889. &instance->clients, instance->server.socket, PR_POLL_READ);
  890. if (pfds == NULL) {
  891. return (-1);
  892. }
  893. if ((poll_res = PR_Poll(pfds, qnetd_poll_array_size(&instance->poll_array),
  894. PR_INTERVAL_NO_TIMEOUT)) > 0) {
  895. /*
  896. * Walk thru pfds array and process events
  897. */
  898. for (i = 0; i < qnetd_poll_array_size(&instance->poll_array); i++) {
  899. /*
  900. * Also traverse clients list
  901. */
  902. if (i > 0) {
  903. if (i == 1) {
  904. client = TAILQ_FIRST(&instance->clients);
  905. client_next = TAILQ_NEXT(client, entries);
  906. } else {
  907. client = client_next;
  908. client_next = TAILQ_NEXT(client, entries);
  909. }
  910. }
  911. client_disconnect = 0;
  912. if (!client_disconnect && pfds[i].out_flags & PR_POLL_READ) {
  913. if (i == 0) {
  914. qnetd_client_accept(instance);
  915. } else {
  916. if (qnetd_client_net_read(instance, client) == -1) {
  917. client_disconnect = 1;
  918. }
  919. }
  920. }
  921. if (!client_disconnect && pfds[i].out_flags & PR_POLL_WRITE) {
  922. if (i == 0) {
  923. /*
  924. * Poll write on listen socket -> fatal error
  925. */
  926. qnetd_log(LOG_CRIT, "POLL_WRITE on listening socket");
  927. return (-1);
  928. } else {
  929. if (qnetd_client_net_write(instance, client) == -1) {
  930. client_disconnect = 1;
  931. }
  932. }
  933. }
  934. if (!client_disconnect &&
  935. pfds[i].out_flags &
  936. (PR_POLL_ERR|PR_POLL_NVAL|PR_POLL_HUP|PR_POLL_EXCEPT)) {
  937. if (i == 0) {
  938. if (pfds[i].out_flags != PR_POLL_NVAL) {
  939. /*
  940. * Poll ERR on listening socket is fatal error.
  941. * POLL_NVAL is used as a signal to quit poll loop.
  942. */
  943. qnetd_log(LOG_CRIT, "POLL_ERR (%u) on listening "
  944. "socket", pfds[i].out_flags);
  945. } else {
  946. qnetd_log(LOG_DEBUG, "Listening socket is closed");
  947. }
  948. return (-1);
  949. } else {
  950. qnetd_log(LOG_DEBUG, "POLL_ERR (%u) on client socket. "
  951. "Disconnecting.", pfds[i].out_flags);
  952. client_disconnect = 1;
  953. }
  954. }
  955. /*
  956. * If client is scheduled for disconnect, disconnect it
  957. */
  958. if (client_disconnect) {
  959. qnetd_client_disconnect(instance, client, 0);
  960. }
  961. }
  962. }
  963. return (0);
  964. }
  965. static int
  966. qnetd_instance_init_certs(struct qnetd_instance *instance)
  967. {
  968. instance->server.cert = PK11_FindCertFromNickname(QNETD_CERT_NICKNAME, NULL);
  969. if (instance->server.cert == NULL) {
  970. return (-1);
  971. }
  972. instance->server.private_key = PK11_FindKeyByAnyCert(instance->server.cert, NULL);
  973. if (instance->server.private_key == NULL) {
  974. return (-1);
  975. }
  976. return (0);
  977. }
  978. static int
  979. qnetd_instance_init(struct qnetd_instance *instance, size_t max_client_receive_size,
  980. size_t max_client_send_buffers, size_t max_client_send_size,
  981. enum tlv_tls_supported tls_supported, int tls_client_cert_required)
  982. {
  983. memset(instance, 0, sizeof(*instance));
  984. qnetd_poll_array_init(&instance->poll_array);
  985. qnetd_client_list_init(&instance->clients);
  986. instance->max_client_receive_size = max_client_receive_size;
  987. instance->max_client_send_buffers = max_client_send_buffers;
  988. instance->max_client_send_size = max_client_send_size;
  989. instance->tls_supported = tls_supported;
  990. instance->tls_client_cert_required = tls_client_cert_required;
  991. return (0);
  992. }
  993. static int
  994. qnetd_instance_destroy(struct qnetd_instance *instance)
  995. {
  996. struct qnetd_client *client;
  997. struct qnetd_client *client_next;
  998. client = TAILQ_FIRST(&instance->clients);
  999. while (client != NULL) {
  1000. client_next = TAILQ_NEXT(client, entries);
  1001. qnetd_client_disconnect(instance, client, 1);
  1002. client = client_next;
  1003. }
  1004. qnetd_poll_array_destroy(&instance->poll_array);
  1005. qnetd_client_list_free(&instance->clients);
  1006. return (0);
  1007. }
  1008. static void
  1009. signal_int_handler(int sig)
  1010. {
  1011. qnetd_log(LOG_DEBUG, "SIGINT received - closing server socket");
  1012. PR_Close(global_server_socket);
  1013. }
  1014. static void
  1015. signal_handlers_register(void)
  1016. {
  1017. struct sigaction act;
  1018. act.sa_handler = signal_int_handler;
  1019. sigemptyset(&act.sa_mask);
  1020. act.sa_flags = SA_RESTART;
  1021. sigaction(SIGINT, &act, NULL);
  1022. }
  1023. static void
  1024. usage(void)
  1025. {
  1026. printf("usage: %s [-h listen_addr] [-p listen_port]\n", QNETD_PROGRAM_NAME);
  1027. }
  1028. static void
  1029. cli_parse(int argc, char * const argv[], char **host_addr, uint16_t *host_port)
  1030. {
  1031. int ch;
  1032. char *ep;
  1033. *host_addr = NULL;
  1034. *host_port = QNETD_DEFAULT_HOST_PORT;
  1035. while ((ch = getopt(argc, argv, "h:p:")) != -1) {
  1036. switch (ch) {
  1037. case 'h':
  1038. *host_addr = strdup(optarg);
  1039. break;
  1040. case 'p':
  1041. *host_port = strtol(optarg, &ep, 10);
  1042. if (*host_port <= 0 || *host_port > ((uint16_t)~0) || *ep != '\0') {
  1043. errx(1, "host port must be in range 0-65535");
  1044. }
  1045. break;
  1046. case '?':
  1047. usage();
  1048. exit(1);
  1049. break;
  1050. }
  1051. }
  1052. }
  1053. int
  1054. main(int argc, char *argv[])
  1055. {
  1056. struct qnetd_instance instance;
  1057. char *host_addr;
  1058. uint16_t host_port;
  1059. /*
  1060. * INIT
  1061. */
  1062. qnetd_log_init(QNETD_LOG_TARGET_STDERR);
  1063. qnetd_log_set_debug(1);
  1064. if (nss_sock_init_nss((char *)NSS_DB_DIR) != 0) {
  1065. qnetd_err_nss();
  1066. }
  1067. if (SSL_ConfigServerSessionIDCache(0, 0, 0, NULL) != SECSuccess) {
  1068. qnetd_err_nss();
  1069. }
  1070. cli_parse(argc, argv, &host_addr, &host_port);
  1071. if (qnetd_instance_init(&instance, QNETD_MAX_CLIENT_RECEIVE_SIZE,
  1072. QNETD_MAX_CLIENT_SEND_BUFFERS, QNETD_MAX_CLIENT_SEND_SIZE,
  1073. QNETD_TLS_SUPPORTED, QNETD_TLS_CLIENT_CERT_REQUIRED) == -1) {
  1074. errx(1, "Can't initialize qnetd");
  1075. }
  1076. instance.host_addr = host_addr;
  1077. instance.host_port = host_port;
  1078. if (qnetd_instance_init_certs(&instance) == -1) {
  1079. qnetd_err_nss();
  1080. }
  1081. instance.server.socket = nss_sock_create_listen_socket(instance.host_addr,
  1082. instance.host_port, PR_AF_INET6);
  1083. if (instance.server.socket == NULL) {
  1084. qnetd_err_nss();
  1085. }
  1086. if (nss_sock_set_nonblocking(instance.server.socket) != 0) {
  1087. qnetd_err_nss();
  1088. }
  1089. if (PR_Listen(instance.server.socket, QNETD_LISTEN_BACKLOG) != PR_SUCCESS) {
  1090. qnetd_err_nss();
  1091. }
  1092. global_server_socket = instance.server.socket;
  1093. signal_handlers_register();
  1094. /*
  1095. * MAIN LOOP
  1096. */
  1097. while (qnetd_poll(&instance) == 0) {
  1098. }
  1099. /*
  1100. * Cleanup
  1101. */
  1102. CERT_DestroyCertificate(instance.server.cert);
  1103. SECKEY_DestroyPrivateKey(instance.server.private_key);
  1104. SSL_ClearSessionCache();
  1105. SSL_ShutdownServerSessionIDCache();
  1106. qnetd_instance_destroy(&instance);
  1107. if (NSS_Shutdown() != SECSuccess) {
  1108. qnetd_warn_nss();
  1109. }
  1110. if (PR_Cleanup() != PR_SUCCESS) {
  1111. qnetd_warn_nss();
  1112. }
  1113. qnetd_log_close();
  1114. return (0);
  1115. }