corosync-qnetd.c 36 KB

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