qnetd-client-net.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. /*
  2. * Copyright (c) 2015-2020 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 <sys/types.h>
  35. #include "log.h"
  36. #include "msgio.h"
  37. #include "msg.h"
  38. #include "nss-sock.h"
  39. #include "qnetd-client-dpd-timer.h"
  40. #include "qnetd-client-net.h"
  41. #include "qnetd-client-send.h"
  42. #include "qnetd-client-msg-received.h"
  43. #define CLIENT_ADDR_STR_LEN_COLON_PORT (1 + 5 + 1)
  44. #define CLIENT_ADDR_STR_LEN (INET6_ADDRSTRLEN + CLIENT_ADDR_STR_LEN_COLON_PORT)
  45. static int
  46. qnetd_client_net_write_finished(struct qnetd_instance *instance, struct qnetd_client *client)
  47. {
  48. /*
  49. * Callback is currently unused
  50. */
  51. return (0);
  52. }
  53. static int
  54. qnetd_client_net_socket_poll_loop_set_events_cb(PRFileDesc *prfd, short *events,
  55. void *user_data1, void *user_data2)
  56. {
  57. struct qnetd_client *client = (struct qnetd_client *)user_data2;
  58. if (client->schedule_disconnect) {
  59. /*
  60. * Disconnect logic used to be there but it was moved to
  61. * qnetd-instance.c (see qnetd_instance_poll_loop_pre_poll_cb
  62. * function for reasoning).
  63. *
  64. * This condition (= set_events_cb and client scheduled for disconnect)
  65. * shouldn't really happen, but if it happens just don't add client to
  66. * pr loop and wait for next pre_poll_cb.
  67. */
  68. return (-1);
  69. }
  70. if (!send_buffer_list_empty(&client->send_buffer_list)) {
  71. *events |= POLLOUT;
  72. }
  73. return (0);
  74. }
  75. static int
  76. qnetd_client_net_socket_poll_loop_read_cb(PRFileDesc *prfd, const PRPollDesc *pd,
  77. void *user_data1, void *user_data2)
  78. {
  79. struct qnetd_instance *instance = (struct qnetd_instance *)user_data1;
  80. struct qnetd_client *client = (struct qnetd_client *)user_data2;
  81. if (!client->schedule_disconnect) {
  82. if (qnetd_client_net_read(instance, client) == -1) {
  83. client->schedule_disconnect = 1;
  84. }
  85. }
  86. return (0);
  87. }
  88. static int
  89. qnetd_client_net_socket_poll_loop_write_cb(PRFileDesc *prfd, const PRPollDesc *pd,
  90. void *user_data1, void *user_data2)
  91. {
  92. struct qnetd_instance *instance = (struct qnetd_instance *)user_data1;
  93. struct qnetd_client *client = (struct qnetd_client *)user_data2;
  94. if (!client->schedule_disconnect) {
  95. if (qnetd_client_net_write(instance, client) == -1) {
  96. client->schedule_disconnect = 1;
  97. }
  98. }
  99. return (0);
  100. }
  101. static int
  102. qnetd_client_net_socket_poll_loop_err_cb(PRFileDesc *prfd, short revents,
  103. const PRPollDesc *pd, void *user_data1, void *user_data2)
  104. {
  105. struct qnetd_client *client = (struct qnetd_client *)user_data2;
  106. if (!client->schedule_disconnect) {
  107. log(LOG_DEBUG, "POLL_ERR (%u) on client socket. "
  108. "Disconnecting.", revents);
  109. client->schedule_disconnect = 1;
  110. }
  111. return (0);
  112. }
  113. int
  114. qnetd_client_net_write(struct qnetd_instance *instance, struct qnetd_client *client)
  115. {
  116. int res;
  117. struct send_buffer_list_entry *send_buffer;
  118. send_buffer = send_buffer_list_get_active(&client->send_buffer_list);
  119. if (send_buffer == NULL) {
  120. log_nss(LOG_CRIT, "send_buffer_list_get_active returned NULL");
  121. return (-1);
  122. }
  123. res = msgio_write(client->socket, &send_buffer->buffer,
  124. &send_buffer->msg_already_sent_bytes);
  125. if (res == 1) {
  126. send_buffer_list_delete(&client->send_buffer_list, send_buffer);
  127. if (qnetd_client_net_write_finished(instance, client) == -1) {
  128. return (-1);
  129. }
  130. }
  131. if (res == -1) {
  132. log_nss(LOG_CRIT, "PR_Send returned 0");
  133. return (-1);
  134. }
  135. if (res == -2) {
  136. log_nss(LOG_ERR, "Unhandled error when sending message to client");
  137. return (-1);
  138. }
  139. return (0);
  140. }
  141. /*
  142. * -1 means end of connection (EOF) or some other unhandled error. 0 = success
  143. */
  144. int
  145. qnetd_client_net_read(struct qnetd_instance *instance, struct qnetd_client *client)
  146. {
  147. int res;
  148. int ret_val;
  149. int orig_skipping_msg;
  150. orig_skipping_msg = client->skipping_msg;
  151. res = msgio_read(client->socket, &client->receive_buffer,
  152. &client->msg_already_received_bytes, &client->skipping_msg);
  153. if (!orig_skipping_msg && client->skipping_msg) {
  154. log(LOG_DEBUG, "msgio_read set skipping_msg");
  155. }
  156. ret_val = 0;
  157. switch (res) {
  158. case 0:
  159. /*
  160. * Partial read
  161. */
  162. break;
  163. case -1:
  164. log(LOG_DEBUG, "Client closed connection");
  165. ret_val = -1;
  166. break;
  167. case -2:
  168. log_nss(LOG_ERR, "Unhandled error when reading from client. "
  169. "Disconnecting client");
  170. ret_val = -1;
  171. break;
  172. case -3:
  173. log(LOG_ERR, "Can't store message header from client. Disconnecting client");
  174. ret_val = -1;
  175. break;
  176. case -4:
  177. log(LOG_ERR, "Can't store message from client. Skipping message");
  178. client->skipping_msg_reason = TLV_REPLY_ERROR_CODE_ERROR_DECODING_MSG;
  179. break;
  180. case -5:
  181. log(LOG_WARNING, "Client sent unsupported msg type %u. Skipping message",
  182. msg_get_type(&client->receive_buffer));
  183. client->skipping_msg_reason = TLV_REPLY_ERROR_CODE_UNSUPPORTED_MESSAGE;
  184. break;
  185. case -6:
  186. log(LOG_WARNING,
  187. "Client wants to send too long message %u bytes. Skipping message",
  188. msg_get_len(&client->receive_buffer));
  189. client->skipping_msg_reason = TLV_REPLY_ERROR_CODE_MESSAGE_TOO_LONG;
  190. break;
  191. case 1:
  192. /*
  193. * Full message received / skipped
  194. */
  195. if (!client->skipping_msg) {
  196. if (qnetd_client_msg_received(instance, client) == -1) {
  197. ret_val = -1;
  198. }
  199. } else {
  200. if (qnetd_client_send_err(client, 0, 0, client->skipping_msg_reason) != 0) {
  201. ret_val = -1;
  202. }
  203. }
  204. client->skipping_msg = 0;
  205. client->skipping_msg_reason = TLV_REPLY_ERROR_CODE_NO_ERROR;
  206. client->msg_already_received_bytes = 0;
  207. dynar_clean(&client->receive_buffer);
  208. break;
  209. default:
  210. log(LOG_ERR, "Unhandled msgio_read error %d\n", res);
  211. exit(EXIT_FAILURE);
  212. break;
  213. }
  214. return (ret_val);
  215. }
  216. int
  217. qnetd_client_net_accept(struct qnetd_instance *instance)
  218. {
  219. PRNetAddr client_addr;
  220. PRFileDesc *client_socket;
  221. struct qnetd_client *client;
  222. char *client_addr_str;
  223. int res_err;
  224. client_addr_str = NULL;
  225. res_err = -1;
  226. if ((client_socket = PR_Accept(instance->server.socket, &client_addr,
  227. PR_INTERVAL_NO_TIMEOUT)) == NULL) {
  228. log_nss(LOG_ERR, "Can't accept connection");
  229. return (-1);
  230. }
  231. if (nss_sock_set_non_blocking(client_socket) != 0) {
  232. log_nss(LOG_ERR, "Can't set client socket to non blocking mode");
  233. goto exit_close;
  234. }
  235. if (instance->max_clients != 0 &&
  236. qnetd_client_list_no_clients(&instance->clients) >= instance->max_clients) {
  237. log(LOG_ERR, "Maximum clients reached. Not accepting connection");
  238. goto exit_close;
  239. }
  240. client_addr_str = malloc(CLIENT_ADDR_STR_LEN);
  241. if (client_addr_str == NULL) {
  242. log(LOG_ERR, "Can't alloc client addr str memory. Not accepting connection");
  243. goto exit_close;
  244. }
  245. if (PR_NetAddrToString(&client_addr, client_addr_str, CLIENT_ADDR_STR_LEN) != PR_SUCCESS) {
  246. log_nss(LOG_ERR, "Can't convert client address to string. Not accepting connection");
  247. goto exit_close;
  248. }
  249. if (snprintf(client_addr_str + strlen(client_addr_str),
  250. CLIENT_ADDR_STR_LEN_COLON_PORT, ":%"PRIu16,
  251. ntohs(client_addr.ipv6.port)) >= CLIENT_ADDR_STR_LEN_COLON_PORT) {
  252. log(LOG_ERR, "Can't store port to client addr str. Not accepting connection");
  253. goto exit_close;
  254. }
  255. client = qnetd_client_list_add(&instance->clients, client_socket, &client_addr,
  256. client_addr_str,
  257. instance->advanced_settings->max_client_receive_size,
  258. instance->advanced_settings->max_client_send_buffers,
  259. instance->advanced_settings->max_client_send_size,
  260. pr_poll_loop_get_timer_list(&instance->main_poll_loop));
  261. if (client == NULL) {
  262. log(LOG_ERR, "Can't add client to list");
  263. res_err = -2;
  264. goto exit_close;
  265. }
  266. if (pr_poll_loop_add_prfd(&instance->main_poll_loop, client_socket, POLLIN,
  267. qnetd_client_net_socket_poll_loop_set_events_cb,
  268. qnetd_client_net_socket_poll_loop_read_cb,
  269. qnetd_client_net_socket_poll_loop_write_cb,
  270. qnetd_client_net_socket_poll_loop_err_cb,
  271. instance, client) == -1) {
  272. log(LOG_ERR, "Can't add client to main poll loop");
  273. res_err = -2;
  274. goto exit_client_list_del_close;
  275. }
  276. if (qnetd_client_dpd_timer_init(instance, client) == -1) {
  277. res_err = -2;
  278. goto exit_client_nspr_list_del_close;
  279. }
  280. return (0);
  281. exit_client_nspr_list_del_close:
  282. if (pr_poll_loop_del_prfd(&instance->main_poll_loop, client_socket) == -1) {
  283. log(LOG_ERR, "pr_poll_loop_del_prfd for client socket failed");
  284. }
  285. exit_client_list_del_close:
  286. qnetd_client_list_del(&instance->clients, client);
  287. /*
  288. * client_addr_str is passed to qnetd_client_list_add and becomes part of client struct.
  289. * qnetd_client_list_del calls qnetd_client_destroy which frees this memory
  290. */
  291. client_addr_str = NULL;
  292. exit_close:
  293. free(client_addr_str);
  294. PR_Close(client_socket);
  295. return (res_err);
  296. }