qnetd-client-net.c 9.1 KB

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