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.

http_message.c 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  1. /*-
  2. * Copyright 2019 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 "http_message.h"
  17. #include "http_connection.h"
  18. #include "http_private.h"
  19. #include "libutil/printf.h"
  20. #include "libserver/logger.h"
  21. #include "utlist.h"
  22. #include "unix-std.h"
  23. struct rspamd_http_message *
  24. rspamd_http_new_message (enum rspamd_http_message_type type)
  25. {
  26. struct rspamd_http_message *new;
  27. new = g_malloc0 (sizeof (struct rspamd_http_message));
  28. if (type == HTTP_REQUEST) {
  29. new->url = rspamd_fstring_new ();
  30. }
  31. else {
  32. new->url = NULL;
  33. new->code = 200;
  34. }
  35. new->port = 80;
  36. new->type = type;
  37. new->method = HTTP_INVALID;
  38. new->headers = kh_init (rspamd_http_headers_hash);
  39. REF_INIT_RETAIN (new, rspamd_http_message_free);
  40. return new;
  41. }
  42. struct rspamd_http_message*
  43. rspamd_http_message_from_url (const gchar *url)
  44. {
  45. struct http_parser_url pu;
  46. struct rspamd_http_message *msg;
  47. const gchar *host, *path;
  48. size_t pathlen, urllen;
  49. guint flags = 0;
  50. if (url == NULL) {
  51. return NULL;
  52. }
  53. urllen = strlen (url);
  54. memset (&pu, 0, sizeof (pu));
  55. if (http_parser_parse_url (url, urllen, FALSE, &pu) != 0) {
  56. msg_warn ("cannot parse URL: %s", url);
  57. return NULL;
  58. }
  59. if ((pu.field_set & (1 << UF_HOST)) == 0) {
  60. msg_warn ("no host argument in URL: %s", url);
  61. return NULL;
  62. }
  63. if ((pu.field_set & (1 << UF_SCHEMA))) {
  64. if (pu.field_data[UF_SCHEMA].len == sizeof ("https") - 1 &&
  65. memcmp (url + pu.field_data[UF_SCHEMA].off, "https", 5) == 0) {
  66. flags |= RSPAMD_HTTP_FLAG_SSL;
  67. }
  68. }
  69. if ((pu.field_set & (1 << UF_PATH)) == 0) {
  70. path = "/";
  71. pathlen = 1;
  72. }
  73. else {
  74. path = url + pu.field_data[UF_PATH].off;
  75. pathlen = urllen - pu.field_data[UF_PATH].off;
  76. }
  77. msg = rspamd_http_new_message (HTTP_REQUEST);
  78. host = url + pu.field_data[UF_HOST].off;
  79. msg->flags = flags;
  80. if ((pu.field_set & (1 << UF_PORT)) != 0) {
  81. msg->port = pu.port;
  82. }
  83. else {
  84. /* XXX: magic constant */
  85. if (flags & RSPAMD_HTTP_FLAG_SSL) {
  86. msg->port = 443;
  87. }
  88. else {
  89. msg->port = 80;
  90. }
  91. }
  92. msg->host = g_string_new_len (host, pu.field_data[UF_HOST].len);
  93. msg->url = rspamd_fstring_append (msg->url, path, pathlen);
  94. REF_INIT_RETAIN (msg, rspamd_http_message_free);
  95. return msg;
  96. }
  97. const gchar *
  98. rspamd_http_message_get_body (struct rspamd_http_message *msg,
  99. gsize *blen)
  100. {
  101. const gchar *ret = NULL;
  102. if (msg->body_buf.len > 0) {
  103. ret = msg->body_buf.begin;
  104. }
  105. if (blen) {
  106. *blen = msg->body_buf.len;
  107. }
  108. return ret;
  109. }
  110. static void
  111. rspamd_http_shname_dtor (void *p)
  112. {
  113. struct rspamd_storage_shmem *n = p;
  114. #ifdef HAVE_SANE_SHMEM
  115. shm_unlink (n->shm_name);
  116. #else
  117. unlink (n->shm_name);
  118. #endif
  119. g_free (n->shm_name);
  120. g_free (n);
  121. }
  122. struct rspamd_storage_shmem *
  123. rspamd_http_message_shmem_ref (struct rspamd_http_message *msg)
  124. {
  125. if ((msg->flags & RSPAMD_HTTP_FLAG_SHMEM) && msg->body_buf.c.shared.name) {
  126. REF_RETAIN (msg->body_buf.c.shared.name);
  127. return msg->body_buf.c.shared.name;
  128. }
  129. return NULL;
  130. }
  131. guint
  132. rspamd_http_message_get_flags (struct rspamd_http_message *msg)
  133. {
  134. return msg->flags;
  135. }
  136. void
  137. rspamd_http_message_shmem_unref (struct rspamd_storage_shmem *p)
  138. {
  139. REF_RELEASE (p);
  140. }
  141. gboolean
  142. rspamd_http_message_set_body (struct rspamd_http_message *msg,
  143. const gchar *data, gsize len)
  144. {
  145. union _rspamd_storage_u *storage;
  146. storage = &msg->body_buf.c;
  147. rspamd_http_message_storage_cleanup (msg);
  148. if (msg->flags & RSPAMD_HTTP_FLAG_SHMEM) {
  149. storage->shared.name = g_malloc (sizeof (*storage->shared.name));
  150. REF_INIT_RETAIN (storage->shared.name, rspamd_http_shname_dtor);
  151. #ifdef HAVE_SANE_SHMEM
  152. #if defined(__DragonFly__)
  153. // DragonFly uses regular files for shm. User rspamd is not allowed to create
  154. // files in the root.
  155. storage->shared.name->shm_name = g_strdup ("/tmp/rhm.XXXXXXXXXXXXXXXXXXXX");
  156. #else
  157. storage->shared.name->shm_name = g_strdup ("/rhm.XXXXXXXXXXXXXXXXXXXX");
  158. #endif
  159. storage->shared.shm_fd = rspamd_shmem_mkstemp (storage->shared.name->shm_name);
  160. #else
  161. /* XXX: assume that tempdir is /tmp */
  162. storage->shared.name->shm_name = g_strdup ("/tmp/rhm.XXXXXXXXXXXXXXXXXXXX");
  163. storage->shared.shm_fd = mkstemp (storage->shared.name->shm_name);
  164. #endif
  165. if (storage->shared.shm_fd == -1) {
  166. return FALSE;
  167. }
  168. if (len != 0 && len != ULLONG_MAX) {
  169. if (ftruncate (storage->shared.shm_fd, len) == -1) {
  170. return FALSE;
  171. }
  172. msg->body_buf.str = mmap (NULL, len,
  173. PROT_WRITE|PROT_READ, MAP_SHARED,
  174. storage->shared.shm_fd, 0);
  175. if (msg->body_buf.str == MAP_FAILED) {
  176. return FALSE;
  177. }
  178. msg->body_buf.begin = msg->body_buf.str;
  179. msg->body_buf.allocated_len = len;
  180. if (data != NULL) {
  181. memcpy (msg->body_buf.str, data, len);
  182. msg->body_buf.len = len;
  183. }
  184. }
  185. else {
  186. msg->body_buf.len = 0;
  187. msg->body_buf.begin = NULL;
  188. msg->body_buf.str = NULL;
  189. msg->body_buf.allocated_len = 0;
  190. }
  191. }
  192. else {
  193. if (len != 0 && len != ULLONG_MAX) {
  194. if (data == NULL) {
  195. storage->normal = rspamd_fstring_sized_new (len);
  196. msg->body_buf.len = 0;
  197. }
  198. else {
  199. storage->normal = rspamd_fstring_new_init (data, len);
  200. msg->body_buf.len = len;
  201. }
  202. }
  203. else {
  204. storage->normal = rspamd_fstring_new ();
  205. }
  206. msg->body_buf.begin = storage->normal->str;
  207. msg->body_buf.str = storage->normal->str;
  208. msg->body_buf.allocated_len = storage->normal->allocated;
  209. }
  210. msg->flags |= RSPAMD_HTTP_FLAG_HAS_BODY;
  211. return TRUE;
  212. }
  213. void
  214. rspamd_http_message_set_method (struct rspamd_http_message *msg,
  215. const gchar *method)
  216. {
  217. gint i;
  218. /* Linear search: not very efficient method */
  219. for (i = 0; i < HTTP_METHOD_MAX; i ++) {
  220. if (g_ascii_strcasecmp (method, http_method_str (i)) == 0) {
  221. msg->method = i;
  222. }
  223. }
  224. }
  225. gboolean
  226. rspamd_http_message_set_body_from_fd (struct rspamd_http_message *msg,
  227. gint fd)
  228. {
  229. union _rspamd_storage_u *storage;
  230. struct stat st;
  231. rspamd_http_message_storage_cleanup (msg);
  232. storage = &msg->body_buf.c;
  233. msg->flags |= RSPAMD_HTTP_FLAG_SHMEM|RSPAMD_HTTP_FLAG_SHMEM_IMMUTABLE;
  234. storage->shared.shm_fd = dup (fd);
  235. msg->body_buf.str = MAP_FAILED;
  236. if (storage->shared.shm_fd == -1) {
  237. return FALSE;
  238. }
  239. if (fstat (storage->shared.shm_fd, &st) == -1) {
  240. return FALSE;
  241. }
  242. msg->body_buf.str = mmap (NULL, st.st_size,
  243. PROT_READ, MAP_SHARED,
  244. storage->shared.shm_fd, 0);
  245. if (msg->body_buf.str == MAP_FAILED) {
  246. return FALSE;
  247. }
  248. msg->body_buf.begin = msg->body_buf.str;
  249. msg->body_buf.len = st.st_size;
  250. msg->body_buf.allocated_len = st.st_size;
  251. return TRUE;
  252. }
  253. gboolean
  254. rspamd_http_message_set_body_from_fstring_steal (struct rspamd_http_message *msg,
  255. rspamd_fstring_t *fstr)
  256. {
  257. union _rspamd_storage_u *storage;
  258. rspamd_http_message_storage_cleanup (msg);
  259. storage = &msg->body_buf.c;
  260. msg->flags &= ~(RSPAMD_HTTP_FLAG_SHMEM|RSPAMD_HTTP_FLAG_SHMEM_IMMUTABLE);
  261. storage->normal = fstr;
  262. msg->body_buf.str = fstr->str;
  263. msg->body_buf.begin = msg->body_buf.str;
  264. msg->body_buf.len = fstr->len;
  265. msg->body_buf.allocated_len = fstr->allocated;
  266. return TRUE;
  267. }
  268. gboolean
  269. rspamd_http_message_set_body_from_fstring_copy (struct rspamd_http_message *msg,
  270. const rspamd_fstring_t *fstr)
  271. {
  272. union _rspamd_storage_u *storage;
  273. rspamd_http_message_storage_cleanup (msg);
  274. storage = &msg->body_buf.c;
  275. msg->flags &= ~(RSPAMD_HTTP_FLAG_SHMEM|RSPAMD_HTTP_FLAG_SHMEM_IMMUTABLE);
  276. storage->normal = rspamd_fstring_new_init (fstr->str, fstr->len);
  277. msg->body_buf.str = storage->normal->str;
  278. msg->body_buf.begin = msg->body_buf.str;
  279. msg->body_buf.len = storage->normal->len;
  280. msg->body_buf.allocated_len = storage->normal->allocated;
  281. return TRUE;
  282. }
  283. gboolean
  284. rspamd_http_message_grow_body (struct rspamd_http_message *msg, gsize len)
  285. {
  286. struct stat st;
  287. union _rspamd_storage_u *storage;
  288. gsize newlen;
  289. storage = &msg->body_buf.c;
  290. if (msg->flags & RSPAMD_HTTP_FLAG_SHMEM) {
  291. if (storage->shared.shm_fd == -1) {
  292. return FALSE;
  293. }
  294. if (fstat (storage->shared.shm_fd, &st) == -1) {
  295. return FALSE;
  296. }
  297. /* Check if we need to grow */
  298. if ((gsize)st.st_size < msg->body_buf.len + len) {
  299. /* Need to grow */
  300. newlen = rspamd_fstring_suggest_size (msg->body_buf.len, st.st_size,
  301. len);
  302. /* Unmap as we need another size of segment */
  303. if (msg->body_buf.str != MAP_FAILED) {
  304. munmap (msg->body_buf.str, st.st_size);
  305. }
  306. if (ftruncate (storage->shared.shm_fd, newlen) == -1) {
  307. return FALSE;
  308. }
  309. msg->body_buf.str = mmap (NULL, newlen,
  310. PROT_WRITE|PROT_READ, MAP_SHARED,
  311. storage->shared.shm_fd, 0);
  312. if (msg->body_buf.str == MAP_FAILED) {
  313. return FALSE;
  314. }
  315. msg->body_buf.begin = msg->body_buf.str;
  316. msg->body_buf.allocated_len = newlen;
  317. }
  318. }
  319. else {
  320. storage->normal = rspamd_fstring_grow (storage->normal, len);
  321. /* Append might cause realloc */
  322. msg->body_buf.begin = storage->normal->str;
  323. msg->body_buf.len = storage->normal->len;
  324. msg->body_buf.str = storage->normal->str;
  325. msg->body_buf.allocated_len = storage->normal->allocated;
  326. }
  327. return TRUE;
  328. }
  329. gboolean
  330. rspamd_http_message_append_body (struct rspamd_http_message *msg,
  331. const gchar *data, gsize len)
  332. {
  333. union _rspamd_storage_u *storage;
  334. storage = &msg->body_buf.c;
  335. if (msg->flags & RSPAMD_HTTP_FLAG_SHMEM) {
  336. if (!rspamd_http_message_grow_body (msg, len)) {
  337. return FALSE;
  338. }
  339. memcpy (msg->body_buf.str + msg->body_buf.len, data, len);
  340. msg->body_buf.len += len;
  341. }
  342. else {
  343. storage->normal = rspamd_fstring_append (storage->normal, data, len);
  344. /* Append might cause realloc */
  345. msg->body_buf.begin = storage->normal->str;
  346. msg->body_buf.len = storage->normal->len;
  347. msg->body_buf.str = storage->normal->str;
  348. msg->body_buf.allocated_len = storage->normal->allocated;
  349. }
  350. return TRUE;
  351. }
  352. void
  353. rspamd_http_message_storage_cleanup (struct rspamd_http_message *msg)
  354. {
  355. union _rspamd_storage_u *storage;
  356. struct stat st;
  357. if (msg->flags & RSPAMD_HTTP_FLAG_SHMEM) {
  358. storage = &msg->body_buf.c;
  359. if (storage->shared.shm_fd > 0) {
  360. g_assert (fstat (storage->shared.shm_fd, &st) != -1);
  361. if (msg->body_buf.str != MAP_FAILED) {
  362. munmap (msg->body_buf.str, st.st_size);
  363. }
  364. close (storage->shared.shm_fd);
  365. }
  366. if (storage->shared.name != NULL) {
  367. REF_RELEASE (storage->shared.name);
  368. }
  369. storage->shared.shm_fd = -1;
  370. msg->body_buf.str = MAP_FAILED;
  371. }
  372. else {
  373. if (msg->body_buf.c.normal) {
  374. rspamd_fstring_free (msg->body_buf.c.normal);
  375. }
  376. msg->body_buf.c.normal = NULL;
  377. }
  378. msg->body_buf.len = 0;
  379. }
  380. void
  381. rspamd_http_message_free (struct rspamd_http_message *msg)
  382. {
  383. struct rspamd_http_header *hdr, *hcur, *hcurtmp;
  384. kh_foreach_value (msg->headers, hdr, {
  385. DL_FOREACH_SAFE (hdr, hcur, hcurtmp) {
  386. rspamd_fstring_free (hcur->combined);
  387. g_free (hcur);
  388. }
  389. });
  390. kh_destroy (rspamd_http_headers_hash, msg->headers);
  391. rspamd_http_message_storage_cleanup (msg);
  392. if (msg->url != NULL) {
  393. rspamd_fstring_free (msg->url);
  394. }
  395. if (msg->status != NULL) {
  396. rspamd_fstring_free (msg->status);
  397. }
  398. if (msg->host != NULL) {
  399. g_string_free (msg->host, TRUE);
  400. }
  401. if (msg->peer_key != NULL) {
  402. rspamd_pubkey_unref (msg->peer_key);
  403. }
  404. g_free (msg);
  405. }
  406. void
  407. rspamd_http_message_set_peer_key (struct rspamd_http_message *msg,
  408. struct rspamd_cryptobox_pubkey *pk)
  409. {
  410. if (msg->peer_key != NULL) {
  411. rspamd_pubkey_unref (msg->peer_key);
  412. }
  413. if (pk) {
  414. msg->peer_key = rspamd_pubkey_ref (pk);
  415. }
  416. else {
  417. msg->peer_key = NULL;
  418. }
  419. }
  420. void
  421. rspamd_http_message_add_header_len (struct rspamd_http_message *msg,
  422. const gchar *name,
  423. const gchar *value,
  424. gsize len)
  425. {
  426. struct rspamd_http_header *hdr, *found;
  427. guint nlen, vlen;
  428. khiter_t k;
  429. gint r;
  430. if (msg != NULL && name != NULL && value != NULL) {
  431. hdr = g_malloc0 (sizeof (struct rspamd_http_header));
  432. nlen = strlen (name);
  433. vlen = len;
  434. hdr->combined = rspamd_fstring_sized_new (nlen + vlen + 4);
  435. rspamd_printf_fstring (&hdr->combined, "%s: %*s\r\n", name, (gint)vlen,
  436. value);
  437. hdr->name.begin = hdr->combined->str;
  438. hdr->name.len = nlen;
  439. hdr->value.begin = hdr->combined->str + nlen + 2;
  440. hdr->value.len = vlen;
  441. k = kh_put (rspamd_http_headers_hash, msg->headers, &hdr->name,
  442. &r);
  443. if (r != 0) {
  444. kh_value (msg->headers, k) = hdr;
  445. found = NULL;
  446. }
  447. else {
  448. found = kh_value (msg->headers, k);
  449. }
  450. DL_APPEND (found, hdr);
  451. }
  452. }
  453. void
  454. rspamd_http_message_add_header (struct rspamd_http_message *msg,
  455. const gchar *name,
  456. const gchar *value)
  457. {
  458. if (value) {
  459. rspamd_http_message_add_header_len (msg, name, value, strlen (value));
  460. }
  461. }
  462. void
  463. rspamd_http_message_add_header_fstr (struct rspamd_http_message *msg,
  464. const gchar *name,
  465. rspamd_fstring_t *value)
  466. {
  467. struct rspamd_http_header *hdr, *found = NULL;
  468. guint nlen, vlen;
  469. khiter_t k;
  470. gint r;
  471. if (msg != NULL && name != NULL && value != NULL) {
  472. hdr = g_malloc0 (sizeof (struct rspamd_http_header));
  473. nlen = strlen (name);
  474. vlen = value->len;
  475. hdr->combined = rspamd_fstring_sized_new (nlen + vlen + 4);
  476. rspamd_printf_fstring (&hdr->combined, "%s: %V\r\n", name, value);
  477. hdr->name.begin = hdr->combined->str;
  478. hdr->name.len = nlen;
  479. hdr->value.begin = hdr->combined->str + nlen + 2;
  480. hdr->value.len = vlen;
  481. k = kh_put (rspamd_http_headers_hash, msg->headers, &hdr->name,
  482. &r);
  483. if (r != 0) {
  484. kh_value (msg->headers, k) = hdr;
  485. found = NULL;
  486. }
  487. else {
  488. found = kh_value (msg->headers, k);
  489. }
  490. DL_APPEND (found, hdr);
  491. }
  492. }
  493. const rspamd_ftok_t *
  494. rspamd_http_message_find_header (struct rspamd_http_message *msg,
  495. const gchar *name)
  496. {
  497. const rspamd_ftok_t *res = NULL;
  498. rspamd_ftok_t srch;
  499. guint slen = strlen (name);
  500. khiter_t k;
  501. if (msg != NULL) {
  502. srch.begin = name;
  503. srch.len = slen;
  504. k = kh_get (rspamd_http_headers_hash, msg->headers, &srch);
  505. if (k != kh_end (msg->headers)) {
  506. res = &(kh_value (msg->headers, k)->value);
  507. }
  508. }
  509. return res;
  510. }
  511. GPtrArray*
  512. rspamd_http_message_find_header_multiple (
  513. struct rspamd_http_message *msg,
  514. const gchar *name)
  515. {
  516. GPtrArray *res = NULL;
  517. struct rspamd_http_header *hdr, *cur;
  518. rspamd_ftok_t srch;
  519. khiter_t k;
  520. guint cnt = 0;
  521. guint slen = strlen (name);
  522. if (msg != NULL) {
  523. srch.begin = name;
  524. srch.len = slen;
  525. k = kh_get (rspamd_http_headers_hash, msg->headers, &srch);
  526. if (k != kh_end (msg->headers)) {
  527. hdr = kh_value (msg->headers, k);
  528. LL_COUNT (hdr, cur, cnt);
  529. res = g_ptr_array_sized_new (cnt);
  530. LL_FOREACH (hdr, cur) {
  531. g_ptr_array_add (res, &cur->value);
  532. }
  533. }
  534. }
  535. return res;
  536. }
  537. gboolean
  538. rspamd_http_message_remove_header (struct rspamd_http_message *msg,
  539. const gchar *name)
  540. {
  541. struct rspamd_http_header *hdr, *hcur, *hcurtmp;
  542. gboolean res = FALSE;
  543. guint slen = strlen (name);
  544. rspamd_ftok_t srch;
  545. khiter_t k;
  546. if (msg != NULL) {
  547. srch.begin = name;
  548. srch.len = slen;
  549. k = kh_get (rspamd_http_headers_hash, msg->headers, &srch);
  550. if (k != kh_end (msg->headers)) {
  551. hdr = kh_value (msg->headers, k);
  552. kh_del (rspamd_http_headers_hash, msg->headers, k);
  553. res = TRUE;
  554. DL_FOREACH_SAFE (hdr, hcur, hcurtmp) {
  555. rspamd_fstring_free (hcur->combined);
  556. g_free (hcur);
  557. }
  558. }
  559. }
  560. return res;
  561. }