Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. /*
  2. * Copyright 2024 Vsevolod Stakhov
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "rspamdclient.h"
  17. #include "libutil/util.h"
  18. #include "libserver/http/http_connection.h"
  19. #include "libserver/http/http_private.h"
  20. #include "libserver/protocol_internal.h"
  21. #include "unix-std.h"
  22. #ifdef SYS_ZSTD
  23. #include "zstd.h"
  24. #else
  25. #include "contrib/zstd/zstd.h"
  26. #endif
  27. #ifdef HAVE_FETCH_H
  28. #include <fetch.h>
  29. #elif defined(CURL_FOUND)
  30. #include <curl/curl.h>
  31. #endif
  32. struct rspamd_client_request;
  33. /*
  34. * Since rspamd uses untagged HTTP we can pass a single message per socket
  35. */
  36. struct rspamd_client_connection {
  37. gint fd;
  38. GString *server_name;
  39. struct rspamd_cryptobox_pubkey *key;
  40. struct rspamd_cryptobox_keypair *keypair;
  41. struct ev_loop *event_loop;
  42. ev_tstamp timeout;
  43. struct rspamd_http_connection *http_conn;
  44. gboolean req_sent;
  45. gdouble start_time;
  46. gdouble send_time;
  47. struct rspamd_client_request *req;
  48. struct rspamd_keypair_cache *keys_cache;
  49. };
  50. struct rspamd_client_request {
  51. struct rspamd_client_connection *conn;
  52. struct rspamd_http_message *msg;
  53. GString *input;
  54. rspamd_client_callback cb;
  55. gpointer ud;
  56. };
  57. #define RCLIENT_ERROR rspamd_client_error_quark()
  58. GQuark
  59. rspamd_client_error_quark(void)
  60. {
  61. return g_quark_from_static_string("rspamd-client-error");
  62. }
  63. static void
  64. rspamd_client_request_free(struct rspamd_client_request *req)
  65. {
  66. if (req != NULL) {
  67. if (req->conn) {
  68. req->conn->req = NULL;
  69. }
  70. if (req->input) {
  71. g_string_free(req->input, TRUE);
  72. }
  73. g_free(req);
  74. }
  75. }
  76. static gint
  77. rspamd_client_body_handler(struct rspamd_http_connection *conn,
  78. struct rspamd_http_message *msg,
  79. const gchar *chunk, gsize len)
  80. {
  81. /* Do nothing here */
  82. return 0;
  83. }
  84. static void
  85. rspamd_client_error_handler(struct rspamd_http_connection *conn, GError *err)
  86. {
  87. struct rspamd_client_request *req =
  88. (struct rspamd_client_request *) conn->ud;
  89. struct rspamd_client_connection *c;
  90. c = req->conn;
  91. req->cb(c, NULL, c->server_name->str, NULL,
  92. req->input, req->ud,
  93. c->start_time, c->send_time, NULL, 0, err);
  94. }
  95. static gint
  96. rspamd_client_finish_handler(struct rspamd_http_connection *conn,
  97. struct rspamd_http_message *msg)
  98. {
  99. struct rspamd_client_request *req =
  100. (struct rspamd_client_request *) conn->ud;
  101. struct rspamd_client_connection *c;
  102. struct ucl_parser *parser;
  103. GError *err;
  104. const rspamd_ftok_t *tok;
  105. const gchar *start, *body = NULL;
  106. guchar *out = NULL;
  107. gsize len, bodylen = 0;
  108. c = req->conn;
  109. if (!c->req_sent) {
  110. c->req_sent = TRUE;
  111. c->send_time = rspamd_get_ticks(FALSE);
  112. rspamd_http_connection_reset(c->http_conn);
  113. rspamd_http_connection_read_message(c->http_conn,
  114. c->req,
  115. c->timeout);
  116. return 0;
  117. }
  118. else {
  119. if (rspamd_http_message_get_body(msg, NULL) == NULL || msg->code / 100 != 2) {
  120. err = g_error_new(RCLIENT_ERROR, msg->code, "HTTP error: %d, %.*s",
  121. msg->code,
  122. (gint) msg->status->len, msg->status->str);
  123. req->cb(c, msg, c->server_name->str, NULL, req->input, req->ud,
  124. c->start_time, c->send_time, body, bodylen, err);
  125. g_error_free(err);
  126. return 0;
  127. }
  128. tok = rspamd_http_message_find_header(msg, COMPRESSION_HEADER);
  129. if (tok) {
  130. /* Need to uncompress */
  131. rspamd_ftok_t t;
  132. t.begin = "zstd";
  133. t.len = 4;
  134. if (rspamd_ftok_casecmp(tok, &t) == 0) {
  135. ZSTD_DStream *zstream;
  136. ZSTD_inBuffer zin;
  137. ZSTD_outBuffer zout;
  138. gsize outlen, r;
  139. zstream = ZSTD_createDStream();
  140. ZSTD_initDStream(zstream);
  141. zin.pos = 0;
  142. zin.src = msg->body_buf.begin;
  143. zin.size = msg->body_buf.len;
  144. if ((outlen = ZSTD_getDecompressedSize(zin.src, zin.size)) == 0) {
  145. outlen = ZSTD_DStreamOutSize();
  146. }
  147. out = g_malloc(outlen);
  148. zout.dst = out;
  149. zout.pos = 0;
  150. zout.size = outlen;
  151. while (zin.pos < zin.size) {
  152. r = ZSTD_decompressStream(zstream, &zout, &zin);
  153. if (ZSTD_isError(r)) {
  154. err = g_error_new(RCLIENT_ERROR, 500,
  155. "Decompression error: %s",
  156. ZSTD_getErrorName(r));
  157. req->cb(c, msg, c->server_name->str, NULL,
  158. req->input, req->ud, c->start_time,
  159. c->send_time, body, bodylen, err);
  160. g_error_free(err);
  161. ZSTD_freeDStream(zstream);
  162. goto end;
  163. }
  164. if (zout.pos == zout.size) {
  165. /* We need to extend output buffer */
  166. zout.size = zout.size * 2;
  167. zout.dst = g_realloc(zout.dst, zout.size);
  168. }
  169. }
  170. ZSTD_freeDStream(zstream);
  171. start = zout.dst;
  172. len = zout.pos;
  173. }
  174. else {
  175. err = g_error_new(RCLIENT_ERROR, 500,
  176. "Invalid compression method");
  177. req->cb(c, msg, c->server_name->str, NULL,
  178. req->input, req->ud, c->start_time, c->send_time,
  179. body, bodylen, err);
  180. g_error_free(err);
  181. return 0;
  182. }
  183. }
  184. else {
  185. start = msg->body_buf.begin;
  186. len = msg->body_buf.len;
  187. }
  188. /* Deal with body */
  189. tok = rspamd_http_message_find_header(msg, MESSAGE_OFFSET_HEADER);
  190. if (tok) {
  191. gulong value = 0;
  192. if (rspamd_strtoul(tok->begin, tok->len, &value) &&
  193. value < len) {
  194. body = start + value;
  195. bodylen = len - value;
  196. len = value;
  197. }
  198. }
  199. parser = ucl_parser_new(0);
  200. if (!ucl_parser_add_chunk_full(parser, start, len,
  201. ucl_parser_get_default_priority(parser),
  202. UCL_DUPLICATE_APPEND, UCL_PARSE_AUTO)) {
  203. err = g_error_new(RCLIENT_ERROR, msg->code, "Cannot parse UCL: %s",
  204. ucl_parser_get_error(parser));
  205. ucl_parser_free(parser);
  206. req->cb(c, msg, c->server_name->str, NULL,
  207. req->input, req->ud,
  208. c->start_time, c->send_time, body, bodylen, err);
  209. g_error_free(err);
  210. goto end;
  211. }
  212. req->cb(c, msg, c->server_name->str,
  213. ucl_parser_get_object(parser),
  214. req->input, req->ud,
  215. c->start_time, c->send_time, body, bodylen, NULL);
  216. ucl_parser_free(parser);
  217. }
  218. end:
  219. if (out) {
  220. g_free(out);
  221. }
  222. return 0;
  223. }
  224. struct rspamd_client_connection *
  225. rspamd_client_init(struct rspamd_http_context *http_ctx,
  226. struct ev_loop *ev_base, const gchar *name,
  227. guint16 port, gdouble timeout, const gchar *key)
  228. {
  229. struct rspamd_client_connection *conn;
  230. gint fd;
  231. fd = rspamd_socket(name, port, SOCK_STREAM, TRUE, FALSE, TRUE);
  232. if (fd == -1) {
  233. return NULL;
  234. }
  235. conn = g_malloc0(sizeof(struct rspamd_client_connection));
  236. conn->event_loop = ev_base;
  237. conn->fd = fd;
  238. conn->req_sent = FALSE;
  239. conn->http_conn = rspamd_http_connection_new_client_socket(http_ctx,
  240. rspamd_client_body_handler,
  241. rspamd_client_error_handler,
  242. rspamd_client_finish_handler,
  243. 0,
  244. fd);
  245. if (!conn->http_conn) {
  246. rspamd_client_destroy(conn);
  247. return NULL;
  248. }
  249. /* Pass socket ownership */
  250. rspamd_http_connection_own_socket(conn->http_conn);
  251. conn->server_name = g_string_new(name);
  252. if (port != 0) {
  253. rspamd_printf_gstring(conn->server_name, ":%d", (int) port);
  254. }
  255. conn->timeout = timeout;
  256. if (key) {
  257. conn->key = rspamd_pubkey_from_base32(key, 0, RSPAMD_KEYPAIR_KEX,
  258. RSPAMD_CRYPTOBOX_MODE_25519);
  259. if (conn->key) {
  260. conn->keypair = rspamd_keypair_new(RSPAMD_KEYPAIR_KEX,
  261. RSPAMD_CRYPTOBOX_MODE_25519);
  262. rspamd_http_connection_set_key(conn->http_conn, conn->keypair);
  263. }
  264. else {
  265. rspamd_client_destroy(conn);
  266. return NULL;
  267. }
  268. }
  269. return conn;
  270. }
  271. gboolean
  272. rspamd_client_command(struct rspamd_client_connection *conn,
  273. const gchar *command, GQueue *attrs,
  274. FILE *in, rspamd_client_callback cb,
  275. gpointer ud, gboolean compressed,
  276. const gchar *comp_dictionary,
  277. const gchar *filename,
  278. GError **err)
  279. {
  280. struct rspamd_client_request *req;
  281. struct rspamd_http_client_header *nh;
  282. gchar *p;
  283. gsize remain, old_len;
  284. GList *cur;
  285. GString *input = NULL;
  286. rspamd_fstring_t *body;
  287. guint dict_id = 0;
  288. gsize dict_len = 0;
  289. void *dict = NULL;
  290. ZSTD_CCtx *zctx;
  291. gboolean ret;
  292. req = g_malloc0(sizeof(struct rspamd_client_request));
  293. req->conn = conn;
  294. req->cb = cb;
  295. req->ud = ud;
  296. req->msg = rspamd_http_new_message(HTTP_REQUEST);
  297. if (conn->key) {
  298. req->msg->peer_key = rspamd_pubkey_ref(conn->key);
  299. }
  300. if (in != NULL) {
  301. /* Read input stream */
  302. input = g_string_sized_new(BUFSIZ);
  303. while (!feof(in)) {
  304. p = input->str + input->len;
  305. remain = input->allocated_len - input->len - 1;
  306. if (remain == 0) {
  307. old_len = input->len;
  308. g_string_set_size(input, old_len * 2);
  309. input->len = old_len;
  310. continue;
  311. }
  312. remain = fread(p, 1, remain, in);
  313. if (remain > 0) {
  314. input->len += remain;
  315. input->str[input->len] = '\0';
  316. }
  317. }
  318. if (ferror(in) != 0) {
  319. g_set_error(err, RCLIENT_ERROR, ferror(in), "input IO error: %s", strerror(ferror(in)));
  320. g_free(req);
  321. g_string_free(input, TRUE);
  322. return FALSE;
  323. }
  324. if (!compressed) {
  325. /* Detect zstd input */
  326. if (input->len > 4 && memcmp(input->str, "\x28\xb5\x2f\xfd", 4) == 0) {
  327. compressed = TRUE;
  328. }
  329. body = rspamd_fstring_new_init(input->str, input->len);
  330. }
  331. else {
  332. if (comp_dictionary) {
  333. dict = rspamd_file_xmap(comp_dictionary, PROT_READ, &dict_len,
  334. TRUE);
  335. if (dict == NULL) {
  336. g_set_error(err, RCLIENT_ERROR, errno,
  337. "cannot open dictionary %s: %s",
  338. comp_dictionary,
  339. strerror(errno));
  340. g_free(req);
  341. g_string_free(input, TRUE);
  342. return FALSE;
  343. }
  344. dict_id = -1;
  345. }
  346. body = rspamd_fstring_sized_new(ZSTD_compressBound(input->len));
  347. zctx = ZSTD_createCCtx();
  348. body->len = ZSTD_compress_usingDict(zctx, body->str, body->allocated,
  349. input->str, input->len,
  350. dict, dict_len,
  351. 1);
  352. munmap(dict, dict_len);
  353. if (ZSTD_isError(body->len)) {
  354. g_set_error(err, RCLIENT_ERROR, ferror(in), "compression error");
  355. g_free(req);
  356. g_string_free(input, TRUE);
  357. rspamd_fstring_free(body);
  358. ZSTD_freeCCtx(zctx);
  359. return FALSE;
  360. }
  361. ZSTD_freeCCtx(zctx);
  362. }
  363. rspamd_http_message_set_body_from_fstring_steal(req->msg, body);
  364. req->input = input;
  365. }
  366. else {
  367. req->input = NULL;
  368. }
  369. /* Convert headers */
  370. cur = attrs->head;
  371. while (cur != NULL) {
  372. nh = cur->data;
  373. rspamd_http_message_add_header(req->msg, nh->name, nh->value);
  374. cur = g_list_next(cur);
  375. }
  376. if (compressed) {
  377. rspamd_http_message_add_header(req->msg, COMPRESSION_HEADER, "zstd");
  378. if (dict_id != 0) {
  379. gchar dict_str[32];
  380. rspamd_snprintf(dict_str, sizeof(dict_str), "%ud", dict_id);
  381. rspamd_http_message_add_header(req->msg, "Dictionary", dict_str);
  382. }
  383. }
  384. if (filename) {
  385. rspamd_http_message_add_header(req->msg, "Filename", filename);
  386. }
  387. /*
  388. * Allow messagepack reply if supported
  389. */
  390. rspamd_http_message_add_header(req->msg, "Accept", "application/msgpack");
  391. req->msg->url = rspamd_fstring_append(req->msg->url, "/", 1);
  392. req->msg->url = rspamd_fstring_append(req->msg->url, command, strlen(command));
  393. conn->req = req;
  394. conn->start_time = rspamd_get_ticks(FALSE);
  395. if (compressed) {
  396. ret = rspamd_http_connection_write_message(conn->http_conn, req->msg,
  397. NULL, "application/x-compressed", req,
  398. conn->timeout);
  399. }
  400. else {
  401. ret = rspamd_http_connection_write_message(conn->http_conn, req->msg,
  402. NULL, "text/plain", req, conn->timeout);
  403. }
  404. return ret;
  405. }
  406. void rspamd_client_destroy(struct rspamd_client_connection *conn)
  407. {
  408. if (conn != NULL) {
  409. if (conn->http_conn) {
  410. rspamd_http_connection_unref(conn->http_conn);
  411. }
  412. if (conn->req != NULL) {
  413. rspamd_client_request_free(conn->req);
  414. }
  415. if (conn->key) {
  416. rspamd_pubkey_unref(conn->key);
  417. }
  418. if (conn->keypair) {
  419. rspamd_keypair_unref(conn->keypair);
  420. }
  421. g_string_free(conn->server_name, TRUE);
  422. g_free(conn);
  423. }
  424. }