You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

rspamdclient.c 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. /*-
  2. * Copyright 2016 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 (parser, start, len)) {
  201. err = g_error_new (RCLIENT_ERROR, msg->code, "Cannot parse UCL: %s",
  202. ucl_parser_get_error (parser));
  203. ucl_parser_free (parser);
  204. req->cb (c, msg, c->server_name->str, NULL,
  205. req->input, req->ud,
  206. c->start_time, c->send_time, body, bodylen, err);
  207. g_error_free (err);
  208. goto end;
  209. }
  210. req->cb (c, msg, c->server_name->str,
  211. ucl_parser_get_object (parser),
  212. req->input, req->ud,
  213. c->start_time, c->send_time, body, bodylen, NULL);
  214. ucl_parser_free (parser);
  215. }
  216. end:
  217. if (out) {
  218. g_free (out);
  219. }
  220. return 0;
  221. }
  222. struct rspamd_client_connection *
  223. rspamd_client_init (struct rspamd_http_context *http_ctx,
  224. struct ev_loop *ev_base, const gchar *name,
  225. guint16 port, gdouble timeout, const gchar *key)
  226. {
  227. struct rspamd_client_connection *conn;
  228. gint fd;
  229. fd = rspamd_socket (name, port, SOCK_STREAM, TRUE, FALSE, TRUE);
  230. if (fd == -1) {
  231. return NULL;
  232. }
  233. conn = g_malloc0 (sizeof (struct rspamd_client_connection));
  234. conn->event_loop = ev_base;
  235. conn->fd = fd;
  236. conn->req_sent = FALSE;
  237. conn->http_conn = rspamd_http_connection_new_client_socket (http_ctx,
  238. rspamd_client_body_handler,
  239. rspamd_client_error_handler,
  240. rspamd_client_finish_handler,
  241. 0,
  242. fd);
  243. if (!conn->http_conn) {
  244. rspamd_client_destroy (conn);
  245. return NULL;
  246. }
  247. /* Pass socket ownership */
  248. rspamd_http_connection_own_socket (conn->http_conn);
  249. conn->server_name = g_string_new (name);
  250. if (port != 0) {
  251. rspamd_printf_gstring (conn->server_name, ":%d", (int)port);
  252. }
  253. conn->timeout = timeout;
  254. if (key) {
  255. conn->key = rspamd_pubkey_from_base32 (key, 0, RSPAMD_KEYPAIR_KEX,
  256. RSPAMD_CRYPTOBOX_MODE_25519);
  257. if (conn->key) {
  258. conn->keypair = rspamd_keypair_new (RSPAMD_KEYPAIR_KEX,
  259. RSPAMD_CRYPTOBOX_MODE_25519);
  260. rspamd_http_connection_set_key (conn->http_conn, conn->keypair);
  261. }
  262. else {
  263. rspamd_client_destroy (conn);
  264. return NULL;
  265. }
  266. }
  267. return conn;
  268. }
  269. gboolean
  270. rspamd_client_command (struct rspamd_client_connection *conn,
  271. const gchar *command, GQueue *attrs,
  272. FILE *in, rspamd_client_callback cb,
  273. gpointer ud, gboolean compressed,
  274. const gchar *comp_dictionary,
  275. const gchar *filename,
  276. GError **err)
  277. {
  278. struct rspamd_client_request *req;
  279. struct rspamd_http_client_header *nh;
  280. gchar *p;
  281. gsize remain, old_len;
  282. GList *cur;
  283. GString *input = NULL;
  284. rspamd_fstring_t *body;
  285. guint dict_id = 0;
  286. gsize dict_len = 0;
  287. void *dict = NULL;
  288. ZSTD_CCtx *zctx;
  289. gboolean ret;
  290. req = g_malloc0 (sizeof (struct rspamd_client_request));
  291. req->conn = conn;
  292. req->cb = cb;
  293. req->ud = ud;
  294. req->msg = rspamd_http_new_message (HTTP_REQUEST);
  295. if (conn->key) {
  296. req->msg->peer_key = rspamd_pubkey_ref (conn->key);
  297. }
  298. if (in != NULL) {
  299. /* Read input stream */
  300. input = g_string_sized_new (BUFSIZ);
  301. while (!feof (in)) {
  302. p = input->str + input->len;
  303. remain = input->allocated_len - input->len - 1;
  304. if (remain == 0) {
  305. old_len = input->len;
  306. g_string_set_size (input, old_len * 2);
  307. input->len = old_len;
  308. continue;
  309. }
  310. remain = fread (p, 1, remain, in);
  311. if (remain > 0) {
  312. input->len += remain;
  313. input->str[input->len] = '\0';
  314. }
  315. }
  316. if (ferror (in) != 0) {
  317. g_set_error (err, RCLIENT_ERROR, ferror (
  318. in), "input IO error: %s", strerror (ferror (in)));
  319. g_free (req);
  320. g_string_free (input, TRUE);
  321. return FALSE;
  322. }
  323. if (!compressed) {
  324. /* Detect zstd input */
  325. if (input->len > 4 && memcmp (input->str, "\x28\xb5\x2f\xfd", 4) == 0) {
  326. compressed = TRUE;
  327. }
  328. body = rspamd_fstring_new_init (input->str, input->len);
  329. }
  330. else {
  331. if (comp_dictionary) {
  332. dict = rspamd_file_xmap (comp_dictionary, PROT_READ, &dict_len,
  333. TRUE);
  334. if (dict == NULL) {
  335. g_set_error (err, RCLIENT_ERROR, errno,
  336. "cannot open dictionary %s: %s",
  337. comp_dictionary,
  338. strerror (errno));
  339. g_free (req);
  340. g_string_free (input, TRUE);
  341. return FALSE;
  342. }
  343. dict_id = -1;
  344. }
  345. body = rspamd_fstring_sized_new (ZSTD_compressBound (input->len));
  346. zctx = ZSTD_createCCtx ();
  347. body->len = ZSTD_compress_usingDict (zctx, body->str, body->allocated,
  348. input->str, input->len,
  349. dict, dict_len,
  350. 1);
  351. munmap (dict, dict_len);
  352. if (ZSTD_isError (body->len)) {
  353. g_set_error (err, RCLIENT_ERROR, ferror (
  354. 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. req->msg->url = rspamd_fstring_append (req->msg->url, "/", 1);
  388. req->msg->url = rspamd_fstring_append (req->msg->url, command, strlen (command));
  389. conn->req = req;
  390. conn->start_time = rspamd_get_ticks (FALSE);
  391. if (compressed) {
  392. ret = rspamd_http_connection_write_message (conn->http_conn, req->msg,
  393. NULL,"application/x-compressed", req,
  394. conn->timeout);
  395. }
  396. else {
  397. ret = rspamd_http_connection_write_message (conn->http_conn, req->msg,
  398. NULL,"text/plain", req, conn->timeout);
  399. }
  400. return ret;
  401. }
  402. void
  403. rspamd_client_destroy (struct rspamd_client_connection *conn)
  404. {
  405. if (conn != NULL) {
  406. if (conn->http_conn) {
  407. rspamd_http_connection_unref (conn->http_conn);
  408. }
  409. if (conn->req != NULL) {
  410. rspamd_client_request_free (conn->req);
  411. }
  412. if (conn->key) {
  413. rspamd_pubkey_unref (conn->key);
  414. }
  415. if (conn->keypair) {
  416. rspamd_keypair_unref (conn->keypair);
  417. }
  418. g_string_free (conn->server_name, TRUE);
  419. g_free (conn);
  420. }
  421. }