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.c 60KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608
  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 "config.h"
  17. #include "http.h"
  18. #include "utlist.h"
  19. #include "util.h"
  20. #include "printf.h"
  21. #include "logger.h"
  22. #include "ref.h"
  23. #include "ottery.h"
  24. #include "keypair_private.h"
  25. #include "cryptobox.h"
  26. #include "unix-std.h"
  27. #define ENCRYPTED_VERSION " HTTP/1.0"
  28. struct _rspamd_http_privbuf {
  29. rspamd_fstring_t *data;
  30. ref_entry_t ref;
  31. };
  32. struct rspamd_http_connection_private {
  33. struct _rspamd_http_privbuf *buf;
  34. gboolean new_header;
  35. gboolean encrypted;
  36. gpointer peer_key;
  37. struct rspamd_http_keypair *local_key;
  38. struct rspamd_http_header *header;
  39. struct http_parser parser;
  40. struct http_parser_settings parser_cb;
  41. struct event ev;
  42. struct timeval tv;
  43. struct timeval *ptv;
  44. struct rspamd_http_message *msg;
  45. struct iovec *out;
  46. guint outlen;
  47. gsize wr_pos;
  48. gsize wr_total;
  49. };
  50. enum http_magic_type {
  51. HTTP_MAGIC_PLAIN = 0,
  52. HTTP_MAGIC_HTML,
  53. HTTP_MAGIC_CSS,
  54. HTTP_MAGIC_JS,
  55. HTTP_MAGIC_PNG,
  56. HTTP_MAGIC_JPG
  57. };
  58. static const struct _rspamd_http_magic {
  59. const gchar *ext;
  60. const gchar *ct;
  61. } http_file_types[] = {
  62. [HTTP_MAGIC_PLAIN] = { "txt", "text/plain" },
  63. [HTTP_MAGIC_HTML] = { "html", "text/html" },
  64. [HTTP_MAGIC_CSS] = { "css", "text/css" },
  65. [HTTP_MAGIC_JS] = { "js", "application/javascript" },
  66. [HTTP_MAGIC_PNG] = { "png", "image/png" },
  67. [HTTP_MAGIC_JPG] = { "jpg", "image/jpeg" },
  68. };
  69. static const gchar *http_week[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
  70. static const gchar *http_month[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  71. "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
  72. static const rspamd_ftok_t key_header = {
  73. .begin = "Key",
  74. .len = 3
  75. };
  76. static const rspamd_ftok_t date_header = {
  77. .begin = "Date",
  78. .len = 4
  79. };
  80. #define RSPAMD_HTTP_KEY_ID_LEN 5
  81. #define HTTP_ERROR http_error_quark ()
  82. GQuark
  83. http_error_quark (void)
  84. {
  85. return g_quark_from_static_string ("http-error-quark");
  86. }
  87. static void
  88. rspamd_http_keypair_dtor (struct rspamd_http_keypair *kp)
  89. {
  90. rspamd_explicit_memzero (kp->sk, sizeof (kp->sk));
  91. rspamd_explicit_memzero (kp->nm, sizeof (kp->nm));
  92. g_slice_free1 (sizeof (*kp), kp);
  93. }
  94. static void
  95. rspamd_http_privbuf_dtor (gpointer ud)
  96. {
  97. struct _rspamd_http_privbuf *p = (struct _rspamd_http_privbuf *)ud;
  98. if (p->data) {
  99. rspamd_fstring_free (p->data);
  100. }
  101. g_slice_free1 (sizeof (struct _rspamd_http_privbuf), p);
  102. }
  103. static const gchar *
  104. rspamd_http_code_to_str (gint code)
  105. {
  106. if (code == 200) {
  107. return "OK";
  108. }
  109. else if (code == 404) {
  110. return "Not found";
  111. }
  112. else if (code == 403 || code == 401) {
  113. return "Not authorized";
  114. }
  115. else if (code >= 400 && code < 500) {
  116. return "Bad request";
  117. }
  118. else if (code >= 300 && code < 400) {
  119. return "See Other";
  120. }
  121. else if (code >= 500 && code < 600) {
  122. return "Internal server error";
  123. }
  124. return "Unknown error";
  125. }
  126. /*
  127. * Obtained from nginx
  128. * Copyright (C) Igor Sysoev
  129. */
  130. static guint mday[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  131. time_t
  132. rspamd_http_parse_date (const gchar *header, gsize len)
  133. {
  134. const gchar *p, *end;
  135. gint month;
  136. guint day, year, hour, min, sec;
  137. guint64 time;
  138. enum {
  139. no = 0, rfc822, /* Tue, 10 Nov 2002 23:50:13 */
  140. rfc850, /* Tuesday, 10-Dec-02 23:50:13 */
  141. isoc /* Tue Dec 10 23:50:13 2002 */
  142. } fmt;
  143. fmt = 0;
  144. if (len > 0) {
  145. end = header + len;
  146. }
  147. else {
  148. end = header + strlen (header);
  149. }
  150. day = 32;
  151. year = 2038;
  152. for (p = header; p < end; p++) {
  153. if (*p == ',') {
  154. break;
  155. }
  156. if (*p == ' ') {
  157. fmt = isoc;
  158. break;
  159. }
  160. }
  161. for (p++; p < end; p++)
  162. if (*p != ' ') {
  163. break;
  164. }
  165. if (end - p < 18) {
  166. return (time_t)-1;
  167. }
  168. if (fmt != isoc) {
  169. if (*p < '0' || *p > '9' || *(p + 1) < '0' || *(p + 1) > '9') {
  170. return (time_t)-1;
  171. }
  172. day = (*p - '0') * 10 + *(p + 1) - '0';
  173. p += 2;
  174. if (*p == ' ') {
  175. if (end - p < 18) {
  176. return (time_t)-1;
  177. }
  178. fmt = rfc822;
  179. }
  180. else if (*p == '-') {
  181. fmt = rfc850;
  182. }
  183. else {
  184. return (time_t)-1;
  185. }
  186. p++;
  187. }
  188. switch (*p) {
  189. case 'J':
  190. month = *(p + 1) == 'a' ? 0 : *(p + 2) == 'n' ? 5 : 6;
  191. break;
  192. case 'F':
  193. month = 1;
  194. break;
  195. case 'M':
  196. month = *(p + 2) == 'r' ? 2 : 4;
  197. break;
  198. case 'A':
  199. month = *(p + 1) == 'p' ? 3 : 7;
  200. break;
  201. case 'S':
  202. month = 8;
  203. break;
  204. case 'O':
  205. month = 9;
  206. break;
  207. case 'N':
  208. month = 10;
  209. break;
  210. case 'D':
  211. month = 11;
  212. break;
  213. default:
  214. return (time_t)-1;
  215. }
  216. p += 3;
  217. if ((fmt == rfc822 && *p != ' ') || (fmt == rfc850 && *p != '-')) {
  218. return (time_t)-1;
  219. }
  220. p++;
  221. if (fmt == rfc822) {
  222. if (*p < '0' || *p > '9' || *(p + 1) < '0' || *(p + 1) > '9'
  223. || *(p + 2) < '0' || *(p + 2) > '9' || *(p + 3) < '0'
  224. || *(p + 3) > '9') {
  225. return (time_t)-1;
  226. }
  227. year = (*p - '0') * 1000 + (*(p + 1) - '0') * 100
  228. + (*(p + 2) - '0') * 10 + *(p + 3) - '0';
  229. p += 4;
  230. }
  231. else if (fmt == rfc850) {
  232. if (*p < '0' || *p > '9' || *(p + 1) < '0' || *(p + 1) > '9') {
  233. return (time_t)-1;
  234. }
  235. year = (*p - '0') * 10 + *(p + 1) - '0';
  236. year += (year < 70) ? 2000 : 1900;
  237. p += 2;
  238. }
  239. if (fmt == isoc) {
  240. if (*p == ' ') {
  241. p++;
  242. }
  243. if (*p < '0' || *p > '9') {
  244. return (time_t)-1;
  245. }
  246. day = *p++ - '0';
  247. if (*p != ' ') {
  248. if (*p < '0' || *p > '9') {
  249. return (time_t)-1;
  250. }
  251. day = day * 10 + *p++ - '0';
  252. }
  253. if (end - p < 14) {
  254. return (time_t)-1;
  255. }
  256. }
  257. if (*p++ != ' ') {
  258. return (time_t)-1;
  259. }
  260. if (*p < '0' || *p > '9' || *(p + 1) < '0' || *(p + 1) > '9') {
  261. return (time_t)-1;
  262. }
  263. hour = (*p - '0') * 10 + *(p + 1) - '0';
  264. p += 2;
  265. if (*p++ != ':') {
  266. return (time_t)-1;
  267. }
  268. if (*p < '0' || *p > '9' || *(p + 1) < '0' || *(p + 1) > '9') {
  269. return (time_t)-1;
  270. }
  271. min = (*p - '0') * 10 + *(p + 1) - '0';
  272. p += 2;
  273. if (*p++ != ':') {
  274. return (time_t)-1;
  275. }
  276. if (*p < '0' || *p > '9' || *(p + 1) < '0' || *(p + 1) > '9') {
  277. return (time_t)-1;
  278. }
  279. sec = (*p - '0') * 10 + *(p + 1) - '0';
  280. if (fmt == isoc) {
  281. p += 2;
  282. if (*p++ != ' ') {
  283. return (time_t)-1;
  284. }
  285. if (*p < '0' || *p > '9' || *(p + 1) < '0' || *(p + 1) > '9'
  286. || *(p + 2) < '0' || *(p + 2) > '9' || *(p + 3) < '0'
  287. || *(p + 3) > '9') {
  288. return (time_t)-1;
  289. }
  290. year = (*p - '0') * 1000 + (*(p + 1) - '0') * 100
  291. + (*(p + 2) - '0') * 10 + *(p + 3) - '0';
  292. }
  293. if (hour > 23 || min > 59 || sec > 59) {
  294. return (time_t)-1;
  295. }
  296. if (day == 29 && month == 1) {
  297. if ((year & 3) || ((year % 100 == 0) && (year % 400) != 0)) {
  298. return (time_t)-1;
  299. }
  300. }
  301. else if (day > mday[month]) {
  302. return (time_t)-1;
  303. }
  304. /*
  305. * shift new year to March 1 and start months from 1 (not 0),
  306. * it is needed for Gauss' formula
  307. */
  308. if (--month <= 0) {
  309. month += 12;
  310. year -= 1;
  311. }
  312. /* Gauss' formula for Gregorian days since March 1, 1 BC */
  313. time = (guint64) (
  314. /* days in years including leap years since March 1, 1 BC */
  315. 365 * year + year / 4 - year / 100 + year / 400
  316. /* days before the month */
  317. + 367 * month / 12 - 30
  318. /* days before the day */
  319. + day - 1
  320. /*
  321. * 719527 days were between March 1, 1 BC and March 1, 1970,
  322. * 31 and 28 days were in January and February 1970
  323. */
  324. - 719527 + 31 + 28) * 86400 + hour * 3600 + min * 60 + sec;
  325. return (time_t) time;
  326. }
  327. static void
  328. rspamd_http_parse_key (rspamd_ftok_t *data, struct rspamd_http_connection *conn,
  329. struct rspamd_http_connection_private *priv)
  330. {
  331. guchar *decoded_id, *decoded_key;
  332. const gchar *eq_pos;
  333. gsize id_len, key_len;
  334. struct rspamd_http_keypair *kp;
  335. if (priv->local_key == NULL) {
  336. /* In this case we cannot do anything, e.g. we cannot decrypt payload */
  337. priv->encrypted = TRUE;
  338. }
  339. else {
  340. /* Check sanity of what we have */
  341. eq_pos = memchr (data->begin, '=', data->len);
  342. if (eq_pos != NULL) {
  343. decoded_id = rspamd_decode_base32 (data->begin, eq_pos - data->begin,
  344. &id_len);
  345. decoded_key = rspamd_decode_base32 (eq_pos + 1, data->begin + data->len -
  346. eq_pos - 1, &key_len);
  347. if (decoded_id != NULL && decoded_key != NULL) {
  348. if (id_len >= RSPAMD_HTTP_KEY_ID_LEN &&
  349. key_len >= rspamd_cryptobox_pk_bytes ()) {
  350. if (memcmp (priv->local_key->id, decoded_id,
  351. RSPAMD_HTTP_KEY_ID_LEN) == 0) {
  352. kp = g_slice_alloc0 (sizeof (*kp));
  353. REF_INIT_RETAIN (kp, rspamd_http_keypair_dtor);
  354. memcpy (kp->pk, decoded_key, rspamd_cryptobox_pk_bytes ());
  355. priv->msg->peer_key = kp;
  356. if (conn->cache && priv->msg->peer_key) {
  357. rspamd_keypair_cache_process (conn->cache,
  358. priv->local_key, priv->msg->peer_key);
  359. }
  360. }
  361. }
  362. }
  363. priv->encrypted = TRUE;
  364. g_free (decoded_key);
  365. g_free (decoded_id);
  366. }
  367. }
  368. }
  369. static inline void
  370. rspamd_http_check_special_header (struct rspamd_http_connection *conn,
  371. struct rspamd_http_connection_private *priv)
  372. {
  373. if (rspamd_ftok_casecmp (priv->header->name, &date_header) == 0) {
  374. priv->msg->date = rspamd_http_parse_date (priv->header->value->begin,
  375. priv->header->value->len);
  376. }
  377. else if (rspamd_ftok_casecmp (priv->header->name, &key_header) == 0) {
  378. rspamd_http_parse_key (priv->header->value, conn, priv);
  379. }
  380. }
  381. static gint
  382. rspamd_http_on_url (http_parser * parser, const gchar *at, size_t length)
  383. {
  384. struct rspamd_http_connection *conn =
  385. (struct rspamd_http_connection *)parser->data;
  386. struct rspamd_http_connection_private *priv;
  387. priv = conn->priv;
  388. priv->msg->url = rspamd_fstring_append (priv->msg->url, at, length);
  389. return 0;
  390. }
  391. static gint
  392. rspamd_http_on_status (http_parser * parser, const gchar *at, size_t length)
  393. {
  394. struct rspamd_http_connection *conn =
  395. (struct rspamd_http_connection *)parser->data;
  396. struct rspamd_http_connection_private *priv;
  397. priv = conn->priv;
  398. if (parser->status_code != 200) {
  399. if (priv->msg->status == NULL) {
  400. priv->msg->status = rspamd_fstring_new ();
  401. }
  402. priv->msg->status = rspamd_fstring_append (priv->msg->status, at, length);
  403. }
  404. return 0;
  405. }
  406. static void
  407. rspamd_http_finish_header (struct rspamd_http_connection *conn,
  408. struct rspamd_http_connection_private *priv)
  409. {
  410. priv->header->combined = rspamd_fstring_append (priv->header->combined,
  411. "\r\n", 2);
  412. priv->header->value->len = priv->header->combined->len -
  413. priv->header->name->len - 4;
  414. priv->header->value->begin = priv->header->combined->str +
  415. priv->header->name->len + 2;
  416. priv->header->name->begin = priv->header->combined->str;
  417. DL_APPEND (priv->msg->headers, priv->header);
  418. rspamd_http_check_special_header (conn, priv);
  419. }
  420. static void
  421. rspamd_http_init_header (struct rspamd_http_connection_private *priv)
  422. {
  423. priv->header = g_slice_alloc (sizeof (struct rspamd_http_header));
  424. priv->header->name = g_slice_alloc0 (sizeof (*priv->header->name));
  425. priv->header->value = g_slice_alloc0 (sizeof (*priv->header->value));
  426. priv->header->combined = rspamd_fstring_new ();
  427. }
  428. static gint
  429. rspamd_http_on_header_field (http_parser * parser,
  430. const gchar *at,
  431. size_t length)
  432. {
  433. struct rspamd_http_connection *conn =
  434. (struct rspamd_http_connection *)parser->data;
  435. struct rspamd_http_connection_private *priv;
  436. priv = conn->priv;
  437. if (priv->header == NULL) {
  438. rspamd_http_init_header (priv);
  439. }
  440. else if (priv->new_header) {
  441. rspamd_http_finish_header (conn, priv);
  442. rspamd_http_init_header (priv);
  443. }
  444. priv->new_header = FALSE;
  445. priv->header->combined = rspamd_fstring_append (priv->header->combined,
  446. at, length);
  447. return 0;
  448. }
  449. static gint
  450. rspamd_http_on_header_value (http_parser * parser,
  451. const gchar *at,
  452. size_t length)
  453. {
  454. struct rspamd_http_connection *conn =
  455. (struct rspamd_http_connection *)parser->data;
  456. struct rspamd_http_connection_private *priv;
  457. priv = conn->priv;
  458. if (priv->header == NULL) {
  459. /* Should not happen */
  460. return -1;
  461. }
  462. if (!priv->new_header) {
  463. priv->new_header = TRUE;
  464. priv->header->combined = rspamd_fstring_append (priv->header->combined,
  465. ": ", 2);
  466. priv->header->name->len = priv->header->combined->len - 2;
  467. }
  468. priv->header->combined = rspamd_fstring_append (priv->header->combined,
  469. at, length);
  470. return 0;
  471. }
  472. static int
  473. rspamd_http_on_headers_complete (http_parser * parser)
  474. {
  475. struct rspamd_http_connection *conn =
  476. (struct rspamd_http_connection *)parser->data;
  477. struct rspamd_http_connection_private *priv;
  478. priv = conn->priv;
  479. if (priv->header != NULL) {
  480. rspamd_http_finish_header (conn, priv);
  481. priv->header = NULL;
  482. priv->new_header = FALSE;
  483. }
  484. if (parser->content_length != 0 && parser->content_length != ULLONG_MAX) {
  485. priv->msg->body = rspamd_fstring_sized_new (parser->content_length);
  486. }
  487. else {
  488. priv->msg->body = rspamd_fstring_new ();
  489. }
  490. if (parser->flags & F_SPAMC) {
  491. priv->msg->flags |= RSPAMD_HTTP_FLAG_SPAMC;
  492. }
  493. priv->msg->body_buf.begin = priv->msg->body->str;
  494. priv->msg->method = parser->method;
  495. priv->msg->code = parser->status_code;
  496. return 0;
  497. }
  498. static int
  499. rspamd_http_on_body (http_parser * parser, const gchar *at, size_t length)
  500. {
  501. struct rspamd_http_connection *conn =
  502. (struct rspamd_http_connection *)parser->data;
  503. struct rspamd_http_connection_private *priv;
  504. priv = conn->priv;
  505. priv->msg->body = rspamd_fstring_append (priv->msg->body, at, length);
  506. /* Append might cause realloc */
  507. priv->msg->body_buf.begin = priv->msg->body->str;
  508. priv->msg->body_buf.len = priv->msg->body->len;
  509. if ((conn->opts & RSPAMD_HTTP_BODY_PARTIAL) && !priv->encrypted) {
  510. /* Incremental update is impossible for encrypted requests so far */
  511. return (conn->body_handler (conn, priv->msg, at, length));
  512. }
  513. return 0;
  514. }
  515. static int
  516. rspamd_http_on_body_decrypted (http_parser * parser, const gchar *at, size_t length)
  517. {
  518. struct rspamd_http_connection *conn =
  519. (struct rspamd_http_connection *)parser->data;
  520. struct rspamd_http_connection_private *priv;
  521. priv = conn->priv;
  522. if (priv->header != NULL) {
  523. rspamd_http_finish_header (conn, priv);
  524. priv->header = NULL;
  525. }
  526. if (priv->msg->body_buf.len == 0) {
  527. priv->msg->body_buf.begin = at;
  528. priv->msg->method = parser->method;
  529. priv->msg->code = parser->status_code;
  530. }
  531. priv->msg->body_buf.len += length;
  532. return 0;
  533. }
  534. static int
  535. rspamd_http_on_headers_complete_decrypted (http_parser *parser)
  536. {
  537. struct rspamd_http_connection *conn =
  538. (struct rspamd_http_connection *) parser->data;
  539. struct rspamd_http_connection_private *priv;
  540. priv = conn->priv;
  541. if (priv->header != NULL) {
  542. rspamd_http_finish_header (conn, priv);
  543. priv->header = NULL;
  544. priv->new_header = FALSE;
  545. }
  546. if (parser->flags & F_SPAMC) {
  547. priv->msg->flags |= RSPAMD_HTTP_FLAG_SPAMC;
  548. }
  549. priv->msg->method = parser->method;
  550. priv->msg->code = parser->status_code;
  551. return 0;
  552. }
  553. static int
  554. rspamd_http_decrypt_message (struct rspamd_http_connection *conn,
  555. struct rspamd_http_connection_private *priv,
  556. struct rspamd_http_keypair *peer_key)
  557. {
  558. guchar *nonce, *m;
  559. gsize dec_len;
  560. struct rspamd_http_message *msg = priv->msg;
  561. struct rspamd_http_header *hdr, *hdrtmp;
  562. struct http_parser decrypted_parser;
  563. struct http_parser_settings decrypted_cb;
  564. nonce = msg->body->str;
  565. m = msg->body->str + rspamd_cryptobox_nonce_bytes () +
  566. rspamd_cryptobox_mac_bytes ();
  567. dec_len = msg->body->len - rspamd_cryptobox_nonce_bytes () -
  568. rspamd_cryptobox_mac_bytes ();
  569. if (!peer_key->has_nm) {
  570. /* We still save NM for the following encryption */
  571. rspamd_cryptobox_nm (peer_key->nm, peer_key->pk, priv->local_key->sk);
  572. peer_key->has_nm = TRUE;
  573. }
  574. if (!rspamd_cryptobox_decrypt_nm_inplace (m, dec_len, nonce,
  575. peer_key->nm, m - rspamd_cryptobox_mac_bytes ())) {
  576. msg_err ("cannot verify encrypted message");
  577. return -1;
  578. }
  579. /* Cleanup message */
  580. DL_FOREACH_SAFE (msg->headers, hdr, hdrtmp) {
  581. rspamd_fstring_free (hdr->combined);
  582. g_slice_free1 (sizeof (*hdr->name), hdr->name);
  583. g_slice_free1 (sizeof (*hdr->value), hdr->value);
  584. g_slice_free1 (sizeof (struct rspamd_http_header), hdr);
  585. }
  586. msg->headers = NULL;
  587. if (msg->url != NULL) {
  588. msg->url = rspamd_fstring_assign (msg->url, "", 0);
  589. }
  590. msg->body_buf.len = 0;
  591. http_parser_init (&decrypted_parser,
  592. conn->type == RSPAMD_HTTP_SERVER ? HTTP_REQUEST : HTTP_RESPONSE);
  593. memset (&decrypted_cb, 0, sizeof (decrypted_cb));
  594. decrypted_cb.on_url = rspamd_http_on_url;
  595. decrypted_cb.on_status = rspamd_http_on_status;
  596. decrypted_cb.on_header_field = rspamd_http_on_header_field;
  597. decrypted_cb.on_header_value = rspamd_http_on_header_value;
  598. decrypted_cb.on_headers_complete = rspamd_http_on_headers_complete_decrypted;
  599. decrypted_cb.on_body = rspamd_http_on_body_decrypted;
  600. decrypted_parser.data = conn;
  601. decrypted_parser.content_length = dec_len;
  602. if (http_parser_execute (&decrypted_parser, &decrypted_cb, m,
  603. dec_len) != (size_t)dec_len) {
  604. msg_err ("HTTP parser error: %s when parsing encrypted request",
  605. http_errno_description (decrypted_parser.http_errno));
  606. return -1;
  607. }
  608. return 0;
  609. }
  610. static int
  611. rspamd_http_on_message_complete (http_parser * parser)
  612. {
  613. struct rspamd_http_connection *conn =
  614. (struct rspamd_http_connection *)parser->data;
  615. struct rspamd_http_connection_private *priv;
  616. int ret = 0;
  617. struct rspamd_http_keypair *peer_key = NULL;
  618. priv = conn->priv;
  619. if ((conn->opts & RSPAMD_HTTP_BODY_PARTIAL) == 0 && priv->encrypted) {
  620. if (priv->local_key == NULL || priv->msg->peer_key == NULL ||
  621. priv->msg->body->len < rspamd_cryptobox_nonce_bytes () +
  622. rspamd_cryptobox_mac_bytes ()) {
  623. msg_err ("cannot decrypt message");
  624. return -1;
  625. }
  626. /* We have keys, so we can decrypt message */
  627. peer_key = (struct rspamd_http_keypair *)priv->msg->peer_key;
  628. ret = rspamd_http_decrypt_message (conn, priv, peer_key);
  629. if (ret != 0) {
  630. return ret;
  631. }
  632. if (conn->body_handler != NULL) {
  633. rspamd_http_connection_ref (conn);
  634. ret = conn->body_handler (conn,
  635. priv->msg,
  636. priv->msg->body_buf.begin,
  637. priv->msg->body_buf.len);
  638. rspamd_http_connection_unref (conn);
  639. }
  640. }
  641. else if ((conn->opts & RSPAMD_HTTP_BODY_PARTIAL) == 0 && conn->body_handler) {
  642. g_assert (conn->body_handler != NULL);
  643. rspamd_http_connection_ref (conn);
  644. ret = conn->body_handler (conn,
  645. priv->msg,
  646. priv->msg->body_buf.begin,
  647. priv->msg->body_buf.len);
  648. rspamd_http_connection_unref (conn);
  649. }
  650. if (ret == 0) {
  651. if (event_pending (&priv->ev, EV_READ, NULL)) {
  652. event_del (&priv->ev);
  653. }
  654. rspamd_http_connection_ref (conn);
  655. ret = conn->finish_handler (conn, priv->msg);
  656. conn->finished = TRUE;
  657. rspamd_http_connection_unref (conn);
  658. }
  659. return ret;
  660. }
  661. static void
  662. rspamd_http_simple_client_helper (struct rspamd_http_connection *conn)
  663. {
  664. struct event_base *base;
  665. base = conn->priv->ev.ev_base;
  666. rspamd_http_connection_reset (conn);
  667. /* Plan read message */
  668. rspamd_http_connection_read_message (conn, conn->ud, conn->fd,
  669. conn->priv->ptv, base);
  670. }
  671. static void
  672. rspamd_http_write_helper (struct rspamd_http_connection *conn)
  673. {
  674. struct rspamd_http_connection_private *priv;
  675. struct iovec *start;
  676. guint niov, i;
  677. gint flags = 0;
  678. gsize remain;
  679. gssize r;
  680. GError *err;
  681. struct iovec *cur_iov;
  682. struct msghdr msg;
  683. priv = conn->priv;
  684. if (priv->wr_pos == priv->wr_total) {
  685. goto call_finish_handler;
  686. }
  687. start = &priv->out[0];
  688. niov = priv->outlen;
  689. remain = priv->wr_pos;
  690. /* We know that niov is small enough for that */
  691. cur_iov = alloca (niov * sizeof (struct iovec));
  692. memcpy (cur_iov, priv->out, niov * sizeof (struct iovec));
  693. for (i = 0; i < priv->outlen && remain > 0; i++) {
  694. /* Find out the first iov required */
  695. start = &cur_iov[i];
  696. if (start->iov_len <= remain) {
  697. remain -= start->iov_len;
  698. start = &cur_iov[i + 1];
  699. niov--;
  700. }
  701. else {
  702. start->iov_base = (void *)((char *)start->iov_base + remain);
  703. start->iov_len -= remain;
  704. remain = 0;
  705. }
  706. }
  707. memset (&msg, 0, sizeof (msg));
  708. msg.msg_iov = start;
  709. msg.msg_iovlen = MIN (IOV_MAX, niov);
  710. g_assert (niov > 0);
  711. #ifdef MSG_NOSIGNAL
  712. flags = MSG_NOSIGNAL;
  713. #endif
  714. r = sendmsg (conn->fd, &msg, flags);
  715. if (r == -1) {
  716. err =
  717. g_error_new (HTTP_ERROR, errno, "IO write error: %s", strerror (
  718. errno));
  719. rspamd_http_connection_ref (conn);
  720. conn->error_handler (conn, err);
  721. rspamd_http_connection_unref (conn);
  722. g_error_free (err);
  723. return;
  724. }
  725. else {
  726. priv->wr_pos += r;
  727. }
  728. if (priv->wr_pos >= priv->wr_total) {
  729. goto call_finish_handler;
  730. }
  731. else {
  732. /* Want to write more */
  733. event_add (&priv->ev, priv->ptv);
  734. }
  735. return;
  736. call_finish_handler:
  737. if ((conn->opts & RSPAMD_HTTP_CLIENT_SIMPLE) == 0) {
  738. rspamd_http_connection_ref (conn);
  739. conn->finished = TRUE;
  740. conn->finish_handler (conn, priv->msg);
  741. rspamd_http_connection_unref (conn);
  742. }
  743. else {
  744. /* Plan read message */
  745. rspamd_http_simple_client_helper (conn);
  746. }
  747. }
  748. static gssize
  749. rspamd_http_try_read (gint fd,
  750. struct rspamd_http_connection *conn,
  751. struct rspamd_http_connection_private *priv,
  752. struct _rspamd_http_privbuf *pbuf)
  753. {
  754. gssize r;
  755. rspamd_fstring_t *buf;
  756. buf = priv->buf->data;
  757. r = read (fd, buf->str, buf->allocated);
  758. if (r <= 0) {
  759. return r;
  760. }
  761. else {
  762. buf->len = r;
  763. }
  764. return r;
  765. }
  766. static void
  767. rspamd_http_event_handler (int fd, short what, gpointer ud)
  768. {
  769. struct rspamd_http_connection *conn = (struct rspamd_http_connection *)ud;
  770. struct rspamd_http_connection_private *priv;
  771. struct _rspamd_http_privbuf *pbuf;
  772. rspamd_fstring_t *buf;
  773. gssize r;
  774. GError *err;
  775. priv = conn->priv;
  776. pbuf = priv->buf;
  777. REF_RETAIN (pbuf);
  778. rspamd_http_connection_ref (conn);
  779. buf = priv->buf->data;
  780. if (what == EV_READ) {
  781. r = rspamd_http_try_read (fd, conn, priv, pbuf);
  782. if (r > 0) {
  783. if (http_parser_execute (&priv->parser, &priv->parser_cb,
  784. buf->str, r) != (size_t)r || priv->parser.http_errno != 0) {
  785. err = g_error_new (HTTP_ERROR, priv->parser.http_errno,
  786. "HTTP parser error: %s",
  787. http_errno_description (priv->parser.http_errno));
  788. conn->error_handler (conn, err);
  789. g_error_free (err);
  790. REF_RELEASE (pbuf);
  791. rspamd_http_connection_unref (conn);
  792. return;
  793. }
  794. }
  795. else if (r == 0) {
  796. if (!conn->finished) {
  797. err = g_error_new (HTTP_ERROR,
  798. errno,
  799. "IO read error: unexpected EOF");
  800. conn->error_handler (conn, err);
  801. g_error_free (err);
  802. }
  803. REF_RELEASE (pbuf);
  804. rspamd_http_connection_unref (conn);
  805. return;
  806. }
  807. else {
  808. err = g_error_new (HTTP_ERROR,
  809. errno,
  810. "IO read error: %s",
  811. strerror (errno));
  812. conn->error_handler (conn, err);
  813. g_error_free (err);
  814. REF_RELEASE (pbuf);
  815. rspamd_http_connection_unref (conn);
  816. return;
  817. }
  818. }
  819. else if (what == EV_TIMEOUT) {
  820. /* Let's try to read from the socket first */
  821. r = rspamd_http_try_read (fd, conn, priv, pbuf);
  822. if (r > 0) {
  823. if (http_parser_execute (&priv->parser, &priv->parser_cb,
  824. buf->str, r) != (size_t)r || priv->parser.http_errno != 0) {
  825. err = g_error_new (HTTP_ERROR, priv->parser.http_errno,
  826. "HTTP parser error: %s",
  827. http_errno_description (priv->parser.http_errno));
  828. conn->error_handler (conn, err);
  829. g_error_free (err);
  830. REF_RELEASE (pbuf);
  831. rspamd_http_connection_unref (conn);
  832. return;
  833. }
  834. }
  835. else if (r == 0) {
  836. if (!conn->finished) {
  837. err = g_error_new (HTTP_ERROR, ETIMEDOUT,
  838. "IO timeout");
  839. conn->error_handler (conn, err);
  840. g_error_free (err);
  841. }
  842. REF_RELEASE (pbuf);
  843. rspamd_http_connection_unref (conn);
  844. return;
  845. }
  846. else {
  847. err = g_error_new (HTTP_ERROR, ETIMEDOUT,
  848. "IO timeout");
  849. conn->error_handler (conn, err);
  850. g_error_free (err);
  851. REF_RELEASE (pbuf);
  852. rspamd_http_connection_unref (conn);
  853. return;
  854. }
  855. }
  856. else if (what == EV_WRITE) {
  857. rspamd_http_write_helper (conn);
  858. }
  859. REF_RELEASE (pbuf);
  860. rspamd_http_connection_unref (conn);
  861. }
  862. static void
  863. rspamd_http_parser_reset (struct rspamd_http_connection *conn)
  864. {
  865. struct rspamd_http_connection_private *priv = conn->priv;
  866. http_parser_init (&priv->parser,
  867. conn->type == RSPAMD_HTTP_SERVER ? HTTP_REQUEST : HTTP_RESPONSE);
  868. priv->parser_cb.on_url = rspamd_http_on_url;
  869. priv->parser_cb.on_status = rspamd_http_on_status;
  870. priv->parser_cb.on_header_field = rspamd_http_on_header_field;
  871. priv->parser_cb.on_header_value = rspamd_http_on_header_value;
  872. priv->parser_cb.on_headers_complete = rspamd_http_on_headers_complete;
  873. priv->parser_cb.on_body = rspamd_http_on_body;
  874. priv->parser_cb.on_message_complete = rspamd_http_on_message_complete;
  875. }
  876. struct rspamd_http_connection *
  877. rspamd_http_connection_new (rspamd_http_body_handler_t body_handler,
  878. rspamd_http_error_handler_t error_handler,
  879. rspamd_http_finish_handler_t finish_handler,
  880. unsigned opts,
  881. enum rspamd_http_connection_type type,
  882. struct rspamd_keypair_cache *cache)
  883. {
  884. struct rspamd_http_connection *new;
  885. struct rspamd_http_connection_private *priv;
  886. if (error_handler == NULL || finish_handler == NULL) {
  887. return NULL;
  888. }
  889. new = g_slice_alloc0 (sizeof (struct rspamd_http_connection));
  890. new->opts = opts;
  891. new->type = type;
  892. new->body_handler = body_handler;
  893. new->error_handler = error_handler;
  894. new->finish_handler = finish_handler;
  895. new->fd = -1;
  896. new->ref = 1;
  897. new->finished = FALSE;
  898. new->cache = cache;
  899. /* Init priv */
  900. priv = g_slice_alloc0 (sizeof (struct rspamd_http_connection_private));
  901. new->priv = priv;
  902. rspamd_http_parser_reset (new);
  903. priv->parser.data = new;
  904. return new;
  905. }
  906. void
  907. rspamd_http_connection_reset (struct rspamd_http_connection *conn)
  908. {
  909. struct rspamd_http_connection_private *priv;
  910. struct rspamd_http_message *msg;
  911. priv = conn->priv;
  912. msg = priv->msg;
  913. /* Clear request */
  914. if (msg != NULL) {
  915. if (msg->peer_key) {
  916. priv->peer_key = msg->peer_key;
  917. msg->peer_key = NULL;
  918. }
  919. rspamd_http_message_free (msg);
  920. priv->msg = NULL;
  921. }
  922. conn->finished = FALSE;
  923. /* Clear priv */
  924. event_del (&priv->ev);
  925. if (priv->buf != NULL) {
  926. REF_RELEASE (priv->buf);
  927. priv->buf = NULL;
  928. }
  929. rspamd_http_parser_reset (conn);
  930. if (priv->out != NULL) {
  931. g_slice_free1 (sizeof (struct iovec) * priv->outlen, priv->out);
  932. priv->out = NULL;
  933. }
  934. }
  935. struct rspamd_http_message *
  936. rspamd_http_connection_steal_msg (struct rspamd_http_connection *conn)
  937. {
  938. struct rspamd_http_connection_private *priv;
  939. struct rspamd_http_message *msg;
  940. priv = conn->priv;
  941. msg = priv->msg;
  942. /* Clear request */
  943. if (msg != NULL) {
  944. if (msg->peer_key) {
  945. priv->peer_key = msg->peer_key;
  946. msg->peer_key = NULL;
  947. }
  948. priv->msg = NULL;
  949. }
  950. return msg;
  951. }
  952. void
  953. rspamd_http_connection_free (struct rspamd_http_connection *conn)
  954. {
  955. struct rspamd_http_connection_private *priv;
  956. struct rspamd_http_keypair *peer_key;
  957. priv = conn->priv;
  958. if (priv != NULL) {
  959. rspamd_http_connection_reset (conn);
  960. if (priv->local_key) {
  961. REF_RELEASE (priv->local_key);
  962. }
  963. if (priv->peer_key) {
  964. peer_key = (struct rspamd_http_keypair *)priv->peer_key;
  965. REF_RELEASE (peer_key);
  966. }
  967. g_slice_free1 (sizeof (struct rspamd_http_connection_private), priv);
  968. }
  969. g_slice_free1 (sizeof (struct rspamd_http_connection), conn);
  970. }
  971. void
  972. rspamd_http_connection_read_message (struct rspamd_http_connection *conn,
  973. gpointer ud, gint fd, struct timeval *timeout, struct event_base *base)
  974. {
  975. struct rspamd_http_connection_private *priv = conn->priv;
  976. struct rspamd_http_message *req;
  977. conn->fd = fd;
  978. conn->ud = ud;
  979. req = rspamd_http_new_message (
  980. conn->type == RSPAMD_HTTP_SERVER ? HTTP_REQUEST : HTTP_RESPONSE);
  981. priv->msg = req;
  982. if (priv->peer_key) {
  983. priv->msg->peer_key = priv->peer_key;
  984. priv->peer_key = NULL;
  985. priv->encrypted = TRUE;
  986. }
  987. if (timeout == NULL) {
  988. priv->ptv = NULL;
  989. }
  990. else if (&priv->tv != timeout) {
  991. memcpy (&priv->tv, timeout, sizeof (struct timeval));
  992. priv->ptv = &priv->tv;
  993. }
  994. priv->header = NULL;
  995. priv->buf = g_slice_alloc0 (sizeof (*priv->buf));
  996. REF_INIT_RETAIN (priv->buf, rspamd_http_privbuf_dtor);
  997. priv->buf->data = rspamd_fstring_sized_new (8192);
  998. priv->new_header = TRUE;
  999. event_set (&priv->ev,
  1000. fd,
  1001. EV_READ | EV_PERSIST,
  1002. rspamd_http_event_handler,
  1003. conn);
  1004. if (base != NULL) {
  1005. event_base_set (base, &priv->ev);
  1006. }
  1007. event_add (&priv->ev, priv->ptv);
  1008. }
  1009. static void
  1010. rspamd_http_connection_encrypt_message (
  1011. struct rspamd_http_connection *conn,
  1012. struct rspamd_http_message *msg,
  1013. struct rspamd_http_connection_private *priv,
  1014. guchar *pbody,
  1015. guint bodylen,
  1016. guchar *pmethod,
  1017. guint methodlen,
  1018. guint preludelen,
  1019. gint hdrcount,
  1020. guchar *np,
  1021. guchar *mp,
  1022. struct rspamd_http_keypair *peer_key)
  1023. {
  1024. struct rspamd_cryptobox_segment *segments;
  1025. guchar *crlfp = mp + rspamd_cryptobox_mac_bytes ();
  1026. gint i, cnt;
  1027. guint outlen;
  1028. struct rspamd_http_header *hdr;
  1029. outlen = priv->out[0].iov_len + priv->out[1].iov_len;
  1030. /*
  1031. * Create segments from the following:
  1032. * Method, [URL], CRLF, nheaders, CRLF, body
  1033. */
  1034. segments = g_new (struct rspamd_cryptobox_segment, hdrcount + 5);
  1035. segments[0].data = pmethod;
  1036. segments[0].len = methodlen;
  1037. if (conn->type != RSPAMD_HTTP_SERVER) {
  1038. segments[1].data = msg->url->str;
  1039. segments[1].len = msg->url->len;
  1040. /* space + HTTP version + crlf */
  1041. segments[2].data = crlfp;
  1042. segments[2].len = preludelen - 2;
  1043. crlfp += segments[2].len;
  1044. i = 3;
  1045. }
  1046. else {
  1047. /* Here we send just CRLF */
  1048. segments[1].data = crlfp;
  1049. segments[1].len = 2;
  1050. crlfp += segments[1].len;
  1051. i = 2;
  1052. }
  1053. LL_FOREACH (msg->headers, hdr) {
  1054. segments[i].data = hdr->combined->str;
  1055. segments[i++].len = hdr->combined->len;
  1056. }
  1057. /* crlfp should point now at the second crlf */
  1058. segments[i].data = crlfp;
  1059. segments[i++].len = 2;
  1060. if (pbody) {
  1061. segments[i].data = pbody;
  1062. segments[i++].len = bodylen;
  1063. }
  1064. cnt = i;
  1065. if (!peer_key->has_nm) {
  1066. rspamd_cryptobox_nm (peer_key->nm, peer_key->pk, priv->local_key->sk);
  1067. peer_key->has_nm = TRUE;
  1068. }
  1069. rspamd_cryptobox_encryptv_nm_inplace (segments,
  1070. cnt,
  1071. np,
  1072. peer_key->nm, mp);
  1073. /*
  1074. * iov[0] = base HTTP request
  1075. * iov[1] = CRLF
  1076. * iov[2] = nonce
  1077. * iov[3] = mac
  1078. * iov[4..i] = encrypted HTTP request/reply
  1079. */
  1080. priv->out[2].iov_base = np;
  1081. priv->out[2].iov_len = rspamd_cryptobox_nonce_bytes ();
  1082. priv->out[3].iov_base = mp;
  1083. priv->out[3].iov_len = rspamd_cryptobox_mac_bytes ();
  1084. outlen += rspamd_cryptobox_nonce_bytes () + rspamd_cryptobox_mac_bytes ();
  1085. for (i = 0; i < cnt; i ++) {
  1086. priv->out[i + 4].iov_base = segments[i].data;
  1087. priv->out[i + 4].iov_len = segments[i].len;
  1088. outlen += segments[i].len;
  1089. }
  1090. priv->wr_total = outlen;
  1091. g_free (segments);
  1092. }
  1093. void
  1094. rspamd_http_connection_write_message (struct rspamd_http_connection *conn,
  1095. struct rspamd_http_message *msg, const gchar *host, const gchar *mime_type,
  1096. gpointer ud, gint fd, struct timeval *timeout, struct event_base *base)
  1097. {
  1098. struct rspamd_http_connection_private *priv = conn->priv;
  1099. struct rspamd_http_header *hdr;
  1100. struct tm t, *ptm;
  1101. gchar datebuf[64], repbuf[512], *pbody;
  1102. gint i, hdrcount, meth_len = 0, preludelen = 0;
  1103. gsize bodylen, enclen = 0;
  1104. rspamd_fstring_t *buf;
  1105. gboolean encrypted = FALSE;
  1106. gchar *b32_key, *b32_id;
  1107. guchar nonce[rspamd_cryptobox_MAX_NONCEBYTES], mac[rspamd_cryptobox_MAX_MACBYTES],
  1108. id[rspamd_cryptobox_HASHBYTES];
  1109. guchar *np = NULL, *mp = NULL, *meth_pos = NULL;
  1110. struct rspamd_http_keypair *peer_key = NULL;
  1111. conn->fd = fd;
  1112. conn->ud = ud;
  1113. priv->msg = msg;
  1114. if (timeout == NULL) {
  1115. priv->ptv = NULL;
  1116. }
  1117. else if (timeout != &priv->tv) {
  1118. memcpy (&priv->tv, timeout, sizeof (struct timeval));
  1119. priv->ptv = &priv->tv;
  1120. }
  1121. priv->header = NULL;
  1122. priv->buf = g_slice_alloc0 (sizeof (*priv->buf));
  1123. REF_INIT_RETAIN (priv->buf, rspamd_http_privbuf_dtor);
  1124. priv->buf->data = rspamd_fstring_sized_new (512);
  1125. buf = priv->buf->data;
  1126. if (priv->peer_key && priv->local_key) {
  1127. priv->msg->peer_key = priv->peer_key;
  1128. priv->peer_key = NULL;
  1129. priv->encrypted = TRUE;
  1130. }
  1131. if (priv->local_key != NULL && msg->peer_key != NULL) {
  1132. encrypted = TRUE;
  1133. if (conn->cache) {
  1134. rspamd_keypair_cache_process (conn->cache,
  1135. priv->local_key, priv->msg->peer_key);
  1136. }
  1137. }
  1138. if (encrypted) {
  1139. if (msg->body == NULL || msg->body->len == 0) {
  1140. pbody = NULL;
  1141. bodylen = 0;
  1142. msg->method = HTTP_GET;
  1143. }
  1144. else {
  1145. pbody = msg->body->str;
  1146. bodylen = msg->body->len;
  1147. msg->method = HTTP_POST;
  1148. }
  1149. if (conn->type == RSPAMD_HTTP_SERVER) {
  1150. /*
  1151. * iov[0] = base reply
  1152. * iov[1] = CRLF
  1153. * iov[2] = nonce
  1154. * iov[3] = mac
  1155. * iov[4] = encrypted reply
  1156. * iov[6] = encrypted crlf
  1157. * iov[7..n] = encrypted headers
  1158. * iov[n + 1] = encrypted crlf
  1159. * [iov[n + 2] = encrypted body]
  1160. */
  1161. priv->outlen = 7;
  1162. enclen = rspamd_cryptobox_nonce_bytes () +
  1163. rspamd_cryptobox_mac_bytes () +
  1164. 4 + /* 2 * CRLF */
  1165. bodylen;
  1166. }
  1167. else {
  1168. /*
  1169. * iov[0] = base request
  1170. * iov[1] = CRLF
  1171. * iov[2] = nonce
  1172. * iov[3] = mac
  1173. * iov[4] = encrypted method + space
  1174. * iov[5] = encrypted url
  1175. * iov[7] = encrypted prelude
  1176. * iov[8..n] = encrypted headers
  1177. * iov[n + 1] = encrypted crlf
  1178. * [iov[n + 2] = encrypted body]
  1179. */
  1180. priv->outlen = 8;
  1181. if (bodylen > 0) {
  1182. preludelen = rspamd_snprintf (repbuf, sizeof (repbuf), "%s\r\n"
  1183. "Content-Length: %z\r\n\r\n", ENCRYPTED_VERSION, bodylen);
  1184. }
  1185. else {
  1186. preludelen = rspamd_snprintf (repbuf, sizeof (repbuf), "%s\r\n\r\n",
  1187. ENCRYPTED_VERSION);
  1188. }
  1189. enclen = rspamd_cryptobox_nonce_bytes () +
  1190. rspamd_cryptobox_mac_bytes () +
  1191. preludelen + /* version [content-length] + 2 * CRLF */
  1192. bodylen;
  1193. }
  1194. if (bodylen > 0) {
  1195. priv->outlen ++;
  1196. }
  1197. }
  1198. else {
  1199. if (msg->method < HTTP_SYMBOLS) {
  1200. if (msg->body == NULL || msg->body->len == 0) {
  1201. pbody = NULL;
  1202. bodylen = 0;
  1203. priv->outlen = 2;
  1204. msg->method = HTTP_GET;
  1205. }
  1206. else {
  1207. pbody = msg->body->str;
  1208. bodylen = msg->body->len;
  1209. priv->outlen = 3;
  1210. msg->method = HTTP_POST;
  1211. }
  1212. }
  1213. else if (msg->body != NULL) {
  1214. pbody = msg->body->str;
  1215. bodylen = msg->body->len;
  1216. priv->outlen = 2;
  1217. }
  1218. else {
  1219. /* Invalid body for spamc method */
  1220. return;
  1221. }
  1222. }
  1223. peer_key = (struct rspamd_http_keypair *)msg->peer_key;
  1224. priv->wr_total = bodylen + buf->len + 2;
  1225. hdrcount = 0;
  1226. DL_FOREACH (msg->headers, hdr) {
  1227. /* <name: value\r\n> */
  1228. priv->wr_total += hdr->combined->len;
  1229. enclen += hdr->combined->len;
  1230. priv->outlen ++;
  1231. hdrcount ++;
  1232. }
  1233. /* Allocate iov */
  1234. priv->out = g_slice_alloc (sizeof (struct iovec) * priv->outlen);
  1235. priv->wr_pos = 0;
  1236. if (conn->type == RSPAMD_HTTP_SERVER) {
  1237. /* Format reply */
  1238. if (msg->method < HTTP_SYMBOLS) {
  1239. ptm = gmtime (&msg->date);
  1240. t = *ptm;
  1241. rspamd_snprintf (datebuf,
  1242. sizeof (datebuf),
  1243. "%s, %02d %s %4d %02d:%02d:%02d GMT",
  1244. http_week[t.tm_wday],
  1245. t.tm_mday,
  1246. http_month[t.tm_mon],
  1247. t.tm_year + 1900,
  1248. t.tm_hour,
  1249. t.tm_min,
  1250. t.tm_sec);
  1251. if (mime_type == NULL) {
  1252. mime_type = encrypted ? "application/octet-stream" : "text/plain";
  1253. }
  1254. if (encrypted) {
  1255. /* Internal reply (encrypted) */
  1256. meth_len = rspamd_snprintf (repbuf, sizeof (repbuf),
  1257. "HTTP/1.1 %d %V\r\n"
  1258. "Connection: close\r\n"
  1259. "Server: %s\r\n"
  1260. "Date: %s\r\n"
  1261. "Content-Length: %z\r\n"
  1262. "Content-Type: %s", /* NO \r\n at the end ! */
  1263. msg->code,
  1264. msg->status,
  1265. "rspamd/" RVERSION,
  1266. datebuf,
  1267. bodylen,
  1268. mime_type);
  1269. enclen += meth_len;
  1270. /* External reply */
  1271. rspamd_printf_fstring (&buf, "HTTP/1.1 200 OK\r\n"
  1272. "Connection: close\r\n"
  1273. "Server: rspamd\r\n"
  1274. "Date: %s\r\n"
  1275. "Content-Length: %z\r\n"
  1276. "Content-Type: application/octet-stream\r\n",
  1277. datebuf,
  1278. enclen);
  1279. }
  1280. else {
  1281. meth_len = rspamd_printf_fstring (&buf, "HTTP/1.1 %d %V\r\n"
  1282. "Connection: close\r\n"
  1283. "Server: %s\r\n"
  1284. "Date: %s\r\n"
  1285. "Content-Length: %z\r\n"
  1286. "Content-Type: %s\r\n",
  1287. msg->code,
  1288. msg->status,
  1289. "rspamd/" RVERSION,
  1290. datebuf,
  1291. bodylen,
  1292. mime_type);
  1293. }
  1294. }
  1295. else {
  1296. /* Legacy spamd reply */
  1297. if (msg->flags & RSPAMD_HTTP_FLAG_SPAMC) {
  1298. rspamd_printf_fstring (&buf, "SPAMD/1.1 0 EX_OK\r\n");
  1299. }
  1300. else {
  1301. rspamd_printf_fstring (&buf, "RSPAMD/1.3 0 EX_OK\r\n");
  1302. }
  1303. }
  1304. }
  1305. else {
  1306. /* Format request */
  1307. enclen += msg->url->len +
  1308. strlen (http_method_str (msg->method)) + 1 /* method + space */;
  1309. if (host == NULL && msg->host == NULL) {
  1310. /* Fallback to HTTP/1.0 */
  1311. if (encrypted) {
  1312. rspamd_printf_fstring (&buf, "%s %s HTTP/1.0\r\n"
  1313. "Content-Length: %z\r\n",
  1314. "POST",
  1315. "/post",
  1316. enclen);
  1317. }
  1318. else {
  1319. rspamd_printf_fstring (&buf, "%s %V HTTP/1.0\r\n"
  1320. "Content-Length: %z\r\n",
  1321. http_method_str (msg->method),
  1322. msg->url,
  1323. bodylen);
  1324. }
  1325. }
  1326. else {
  1327. if (encrypted) {
  1328. if (host != NULL) {
  1329. rspamd_printf_fstring (&buf, "%s %s HTTP/1.1\r\n"
  1330. "Connection: close\r\n"
  1331. "Host: %s\r\n"
  1332. "Content-Length: %z\r\n",
  1333. "POST",
  1334. "/post",
  1335. host,
  1336. enclen);
  1337. }
  1338. else {
  1339. rspamd_printf_fstring (&buf, "%s %s HTTP/1.1\r\n"
  1340. "Connection: close\r\n"
  1341. "Host: %V\r\n"
  1342. "Content-Length: %z\r\n",
  1343. "POST",
  1344. "/post",
  1345. msg->host,
  1346. enclen);
  1347. }
  1348. }
  1349. else {
  1350. if (host != NULL) {
  1351. rspamd_printf_fstring (&buf, "%s %V HTTP/1.1\r\n"
  1352. "Connection: close\r\n"
  1353. "Host: %s\r\n"
  1354. "Content-Length: %z\r\n",
  1355. http_method_str (msg->method),
  1356. msg->url,
  1357. host,
  1358. bodylen);
  1359. }
  1360. else {
  1361. rspamd_printf_fstring (&buf, "%s %V HTTP/1.1\r\n"
  1362. "Connection: close\r\n"
  1363. "Host: %V\r\n"
  1364. "Content-Length: %z\r\n",
  1365. http_method_str (msg->method),
  1366. msg->url,
  1367. msg->host,
  1368. bodylen);
  1369. }
  1370. }
  1371. }
  1372. if (encrypted) {
  1373. memcpy (id, peer_key->id, sizeof (id));
  1374. b32_key = rspamd_encode_base32 (priv->local_key->pk,
  1375. rspamd_cryptobox_pk_bytes ());
  1376. b32_id = rspamd_encode_base32 (id, RSPAMD_HTTP_KEY_ID_LEN);
  1377. /* XXX: add some fuzz here */
  1378. rspamd_printf_fstring (&buf, "Key: %s=%s\r\n", b32_id, b32_key);
  1379. g_free (b32_key);
  1380. g_free (b32_id);
  1381. }
  1382. }
  1383. /* Setup external request body */
  1384. priv->out[0].iov_base = buf->str;
  1385. priv->out[0].iov_len = buf->len;
  1386. /* Buf will be used eventually for encryption */
  1387. if (encrypted) {
  1388. gint meth_offset, nonce_offset, mac_offset;
  1389. ottery_rand_bytes (nonce, rspamd_cryptobox_nonce_bytes ());
  1390. memset (mac, 0, rspamd_cryptobox_mac_bytes ());
  1391. meth_offset = buf->len;
  1392. if (conn->type == RSPAMD_HTTP_SERVER) {
  1393. buf = rspamd_fstring_append (buf, repbuf, meth_len);
  1394. }
  1395. else {
  1396. meth_len = strlen (http_method_str (msg->method)) + 1; /* + space */
  1397. buf = rspamd_fstring_append (buf, http_method_str (msg->method),
  1398. meth_len - 1);
  1399. buf = rspamd_fstring_append (buf, " ", 1);
  1400. }
  1401. nonce_offset = buf->len;
  1402. buf = rspamd_fstring_append (buf, nonce, rspamd_cryptobox_nonce_bytes ());
  1403. mac_offset = buf->len;
  1404. buf = rspamd_fstring_append (buf, mac, rspamd_cryptobox_mac_bytes ());
  1405. /* Need to be encrypted */
  1406. if (conn->type == RSPAMD_HTTP_SERVER) {
  1407. buf = rspamd_fstring_append (buf, "\r\n\r\n", 4);
  1408. }
  1409. else {
  1410. buf = rspamd_fstring_append (buf, repbuf, preludelen);
  1411. }
  1412. meth_pos = buf->str + meth_offset;
  1413. np = buf->str + nonce_offset;
  1414. mp = buf->str + mac_offset;
  1415. }
  1416. /* During previous writes, buf might be reallocated and changed */
  1417. priv->buf->data = buf;
  1418. if (encrypted) {
  1419. /* Finish external HTTP request */
  1420. priv->out[1].iov_base = "\r\n";
  1421. priv->out[1].iov_len = 2;
  1422. /* Encrypt the real request */
  1423. rspamd_http_connection_encrypt_message (conn, msg, priv, pbody, bodylen,
  1424. meth_pos, meth_len, preludelen, hdrcount, np, mp, peer_key);
  1425. }
  1426. else {
  1427. i = 1;
  1428. LL_FOREACH (msg->headers, hdr) {
  1429. priv->out[i].iov_base = hdr->combined->str;
  1430. priv->out[i++].iov_len = hdr->combined->len;
  1431. }
  1432. if (msg->method < HTTP_SYMBOLS) {
  1433. priv->out[i].iov_base = "\r\n";
  1434. priv->out[i++].iov_len = 2;
  1435. }
  1436. else {
  1437. /* No CRLF for compatibility reply */
  1438. priv->wr_total -= 2;
  1439. }
  1440. if (pbody != NULL) {
  1441. if (msg->body_buf.begin == NULL && msg->body_buf.len == 0) {
  1442. msg->body_buf.begin = msg->body->str;
  1443. }
  1444. priv->out[i].iov_base = pbody;
  1445. priv->out[i++].iov_len = bodylen;
  1446. }
  1447. }
  1448. if (base != NULL && event_get_base (&priv->ev) == base) {
  1449. event_del (&priv->ev);
  1450. }
  1451. event_set (&priv->ev, fd, EV_WRITE, rspamd_http_event_handler, conn);
  1452. if (base != NULL) {
  1453. event_base_set (base, &priv->ev);
  1454. }
  1455. event_add (&priv->ev, priv->ptv);
  1456. }
  1457. struct rspamd_http_message *
  1458. rspamd_http_new_message (enum http_parser_type type)
  1459. {
  1460. struct rspamd_http_message *new;
  1461. new = g_slice_alloc (sizeof (struct rspamd_http_message));
  1462. if (type == HTTP_REQUEST) {
  1463. new->url = rspamd_fstring_new ();
  1464. }
  1465. else {
  1466. new->url = NULL;
  1467. new->code = 200;
  1468. }
  1469. new->headers = NULL;
  1470. new->date = 0;
  1471. new->body = NULL;
  1472. memset (&new->body_buf, 0, sizeof (new->body_buf));
  1473. new->status = NULL;
  1474. new->host = NULL;
  1475. new->port = 80;
  1476. new->type = type;
  1477. new->method = HTTP_GET;
  1478. new->peer_key = NULL;
  1479. new->flags = 0;
  1480. return new;
  1481. }
  1482. struct rspamd_http_message*
  1483. rspamd_http_message_from_url (const gchar *url)
  1484. {
  1485. struct http_parser_url pu;
  1486. struct rspamd_http_message *msg;
  1487. const gchar *host, *path;
  1488. size_t pathlen, urllen;
  1489. if (url == NULL) {
  1490. return NULL;
  1491. }
  1492. urllen = strlen (url);
  1493. memset (&pu, 0, sizeof (pu));
  1494. if (http_parser_parse_url (url, urllen, FALSE, &pu) != 0) {
  1495. msg_warn ("cannot parse URL: %s", url);
  1496. return NULL;
  1497. }
  1498. if ((pu.field_set & (1 << UF_HOST)) == 0) {
  1499. msg_warn ("no host argument in URL: %s", url);
  1500. return NULL;
  1501. }
  1502. if ((pu.field_set & (1 << UF_PATH)) == 0) {
  1503. path = "/";
  1504. pathlen = 1;
  1505. }
  1506. else {
  1507. path = url + pu.field_data[UF_PATH].off;
  1508. pathlen = pu.field_data[UF_PATH].len;
  1509. }
  1510. msg = rspamd_http_new_message (HTTP_REQUEST);
  1511. host = url + pu.field_data[UF_HOST].off;
  1512. if ((pu.field_set & (1 << UF_PORT)) != 0) {
  1513. msg->port = pu.port;
  1514. }
  1515. else {
  1516. /* XXX: magic constant */
  1517. msg->port = 80;
  1518. }
  1519. msg->host = rspamd_fstring_new_init (host, pu.field_data[UF_HOST].len);
  1520. msg->url = rspamd_fstring_new_init (path, pathlen);
  1521. return msg;
  1522. }
  1523. void
  1524. rspamd_http_message_free (struct rspamd_http_message *msg)
  1525. {
  1526. struct rspamd_http_header *hdr, *tmp_hdr;
  1527. LL_FOREACH_SAFE (msg->headers, hdr, tmp_hdr)
  1528. {
  1529. rspamd_fstring_free (hdr->combined);
  1530. g_slice_free1 (sizeof (*hdr->name), hdr->name);
  1531. g_slice_free1 (sizeof (*hdr->value), hdr->value);
  1532. g_slice_free1 (sizeof (struct rspamd_http_header), hdr);
  1533. }
  1534. if (msg->body != NULL) {
  1535. rspamd_fstring_free (msg->body);
  1536. }
  1537. if (msg->url != NULL) {
  1538. rspamd_fstring_free (msg->url);
  1539. }
  1540. if (msg->status != NULL) {
  1541. rspamd_fstring_free (msg->status);
  1542. }
  1543. if (msg->host != NULL) {
  1544. rspamd_fstring_free (msg->host);
  1545. }
  1546. if (msg->peer_key != NULL) {
  1547. rspamd_http_connection_key_unref (msg->peer_key);
  1548. }
  1549. g_slice_free1 (sizeof (struct rspamd_http_message), msg);
  1550. }
  1551. void
  1552. rspamd_http_message_add_header (struct rspamd_http_message *msg,
  1553. const gchar *name,
  1554. const gchar *value)
  1555. {
  1556. struct rspamd_http_header *hdr;
  1557. guint nlen, vlen;
  1558. if (msg != NULL && name != NULL && value != NULL) {
  1559. hdr = g_slice_alloc (sizeof (struct rspamd_http_header));
  1560. nlen = strlen (name);
  1561. vlen = strlen (value);
  1562. hdr->combined = rspamd_fstring_sized_new (nlen + vlen + 4);
  1563. rspamd_printf_fstring (&hdr->combined, "%s: %s\r\n", name, value);
  1564. hdr->value = g_slice_alloc (sizeof (*hdr->value));
  1565. hdr->name = g_slice_alloc (sizeof (*hdr->name));
  1566. hdr->name->begin = hdr->combined->str;
  1567. hdr->name->len = nlen;
  1568. hdr->value->begin = hdr->combined->str + nlen + 2;
  1569. hdr->value->len = vlen;
  1570. DL_APPEND (msg->headers, hdr);
  1571. }
  1572. }
  1573. const rspamd_ftok_t *
  1574. rspamd_http_message_find_header (struct rspamd_http_message *msg,
  1575. const gchar *name)
  1576. {
  1577. struct rspamd_http_header *hdr;
  1578. const rspamd_ftok_t *res = NULL;
  1579. rspamd_ftok_t cmp;
  1580. guint slen = strlen (name);
  1581. if (msg != NULL) {
  1582. cmp.begin = name;
  1583. cmp.len = slen;
  1584. LL_FOREACH (msg->headers, hdr) {
  1585. if (rspamd_ftok_casecmp (hdr->name, &cmp) == 0) {
  1586. res = hdr->value;
  1587. break;
  1588. }
  1589. }
  1590. }
  1591. return res;
  1592. }
  1593. gboolean rspamd_http_message_remove_header (struct rspamd_http_message *msg,
  1594. const gchar *name)
  1595. {
  1596. struct rspamd_http_header *hdr, *tmp;
  1597. gboolean res = FALSE;
  1598. guint slen = strlen (name);
  1599. rspamd_ftok_t cmp;
  1600. if (msg != NULL) {
  1601. cmp.begin = name;
  1602. cmp.len = slen;
  1603. DL_FOREACH_SAFE (msg->headers, hdr, tmp) {
  1604. if (rspamd_ftok_casecmp (hdr->name, &cmp) == 0) {
  1605. res = TRUE;
  1606. DL_DELETE (msg->headers, hdr);
  1607. rspamd_fstring_free (hdr->combined);
  1608. g_slice_free1 (sizeof (*hdr->value), hdr->value);
  1609. g_slice_free1 (sizeof (*hdr->name), hdr->name);
  1610. g_slice_free1 (sizeof (*hdr), hdr);
  1611. }
  1612. }
  1613. }
  1614. return res;
  1615. }
  1616. /*
  1617. * HTTP router functions
  1618. */
  1619. static void
  1620. rspamd_http_entry_free (struct rspamd_http_connection_entry *entry)
  1621. {
  1622. if (entry != NULL) {
  1623. close (entry->conn->fd);
  1624. rspamd_http_connection_unref (entry->conn);
  1625. if (entry->rt->finish_handler) {
  1626. entry->rt->finish_handler (entry);
  1627. }
  1628. DL_DELETE (entry->rt->conns, entry);
  1629. g_slice_free1 (sizeof (struct rspamd_http_connection_entry), entry);
  1630. }
  1631. }
  1632. static void
  1633. rspamd_http_router_error_handler (struct rspamd_http_connection *conn,
  1634. GError *err)
  1635. {
  1636. struct rspamd_http_connection_entry *entry = conn->ud;
  1637. struct rspamd_http_message *msg;
  1638. if (entry->is_reply) {
  1639. /* At this point we need to finish this session and close owned socket */
  1640. if (entry->rt->error_handler != NULL) {
  1641. entry->rt->error_handler (entry, err);
  1642. }
  1643. rspamd_http_entry_free (entry);
  1644. }
  1645. else {
  1646. /* Here we can write a reply to a client */
  1647. if (entry->rt->error_handler != NULL) {
  1648. entry->rt->error_handler (entry, err);
  1649. }
  1650. msg = rspamd_http_new_message (HTTP_RESPONSE);
  1651. msg->date = time (NULL);
  1652. msg->code = err->code;
  1653. msg->body = rspamd_fstring_new_init (err->message, strlen (err->message));
  1654. rspamd_http_connection_reset (entry->conn);
  1655. rspamd_http_connection_write_message (entry->conn,
  1656. msg,
  1657. NULL,
  1658. "text/plain",
  1659. entry,
  1660. entry->conn->fd,
  1661. entry->rt->ptv,
  1662. entry->rt->ev_base);
  1663. entry->is_reply = TRUE;
  1664. }
  1665. }
  1666. static const gchar *
  1667. rspamd_http_router_detect_ct (const gchar *path)
  1668. {
  1669. const gchar *dot;
  1670. guint i;
  1671. dot = strrchr (path, '.');
  1672. if (dot == NULL) {
  1673. return http_file_types[HTTP_MAGIC_PLAIN].ct;
  1674. }
  1675. dot++;
  1676. for (i = 0; i < G_N_ELEMENTS (http_file_types); i++) {
  1677. if (strcmp (http_file_types[i].ext, dot) == 0) {
  1678. return http_file_types[i].ct;
  1679. }
  1680. }
  1681. return http_file_types[HTTP_MAGIC_PLAIN].ct;
  1682. }
  1683. static gboolean
  1684. rspamd_http_router_is_subdir (const gchar *parent, const gchar *sub)
  1685. {
  1686. if (parent == NULL || sub == NULL || *parent == '\0') {
  1687. return FALSE;
  1688. }
  1689. while (*parent != '\0') {
  1690. if (*sub != *parent) {
  1691. return FALSE;
  1692. }
  1693. parent++;
  1694. sub++;
  1695. }
  1696. parent--;
  1697. if (*parent == G_DIR_SEPARATOR) {
  1698. return TRUE;
  1699. }
  1700. return (*sub == G_DIR_SEPARATOR || *sub == '\0');
  1701. }
  1702. static gboolean
  1703. rspamd_http_router_try_file (struct rspamd_http_connection_entry *entry,
  1704. rspamd_ftok_t *lookup, gboolean expand_path)
  1705. {
  1706. struct stat st;
  1707. gint fd;
  1708. gchar filebuf[PATH_MAX], realbuf[PATH_MAX], *dir;
  1709. struct rspamd_http_message *reply_msg;
  1710. rspamd_snprintf (filebuf, sizeof (filebuf), "%s%c%T",
  1711. entry->rt->default_fs_path, G_DIR_SEPARATOR, lookup);
  1712. if (realpath (filebuf, realbuf) == NULL ||
  1713. lstat (realbuf, &st) == -1) {
  1714. return FALSE;
  1715. }
  1716. if (S_ISDIR (st.st_mode) && expand_path) {
  1717. /* Try to append 'index.html' to the url */
  1718. rspamd_fstring_t *nlookup;
  1719. rspamd_ftok_t tok;
  1720. gboolean ret;
  1721. nlookup = rspamd_fstring_sized_new (lookup->len + sizeof ("index.html"));
  1722. rspamd_printf_fstring (&nlookup, "%T%c%s", lookup, G_DIR_SEPARATOR,
  1723. "index.html");
  1724. tok.begin = nlookup->str;
  1725. tok.len = nlookup->len;
  1726. ret = rspamd_http_router_try_file (entry, &tok, FALSE);
  1727. rspamd_fstring_free (nlookup);
  1728. return ret;
  1729. }
  1730. else if (!S_ISREG (st.st_mode)) {
  1731. return FALSE;
  1732. }
  1733. /* We also need to ensure that file is inside the defined dir */
  1734. rspamd_strlcpy (filebuf, realbuf, sizeof (filebuf));
  1735. dir = dirname (filebuf);
  1736. if (dir == NULL ||
  1737. !rspamd_http_router_is_subdir (entry->rt->default_fs_path,
  1738. dir)) {
  1739. return FALSE;
  1740. }
  1741. fd = open (realbuf, O_RDONLY);
  1742. if (fd == -1) {
  1743. return FALSE;
  1744. }
  1745. reply_msg = rspamd_http_new_message (HTTP_RESPONSE);
  1746. reply_msg->date = time (NULL);
  1747. reply_msg->code = 200;
  1748. reply_msg->body = rspamd_fstring_sized_new (st.st_size);
  1749. reply_msg->body->len = st.st_size;
  1750. reply_msg->body_buf.len = st.st_size;
  1751. reply_msg->body_buf.begin = reply_msg->body->str;
  1752. if (read (fd, reply_msg->body->str, st.st_size) != st.st_size) {
  1753. close (fd);
  1754. rspamd_http_message_free (reply_msg);
  1755. return FALSE;
  1756. }
  1757. close (fd);
  1758. rspamd_http_connection_reset (entry->conn);
  1759. msg_debug ("requested file %s", realbuf);
  1760. rspamd_http_connection_write_message (entry->conn, reply_msg, NULL,
  1761. rspamd_http_router_detect_ct (realbuf), entry, entry->conn->fd,
  1762. entry->rt->ptv, entry->rt->ev_base);
  1763. return TRUE;
  1764. }
  1765. static int
  1766. rspamd_http_router_finish_handler (struct rspamd_http_connection *conn,
  1767. struct rspamd_http_message *msg)
  1768. {
  1769. struct rspamd_http_connection_entry *entry = conn->ud;
  1770. rspamd_http_router_handler_t handler = NULL;
  1771. gpointer found;
  1772. struct rspamd_http_message *err_msg;
  1773. GError *err;
  1774. rspamd_ftok_t lookup;
  1775. struct http_parser_url u;
  1776. G_STATIC_ASSERT (sizeof (rspamd_http_router_handler_t) ==
  1777. sizeof (gpointer));
  1778. memset (&lookup, 0, sizeof (lookup));
  1779. if (entry->is_reply) {
  1780. /* Request is finished, it is safe to free a connection */
  1781. rspamd_http_entry_free (entry);
  1782. }
  1783. else {
  1784. /* Search for path */
  1785. if (msg->url != NULL && msg->url->len != 0) {
  1786. http_parser_parse_url (msg->url->str, msg->url->len, TRUE, &u);
  1787. if (u.field_set & (1 << UF_PATH)) {
  1788. lookup.begin = msg->url->str + u.field_data[UF_PATH].off;
  1789. lookup.len = u.field_data[UF_PATH].len;
  1790. }
  1791. else {
  1792. lookup.begin = msg->url->str;
  1793. lookup.len = msg->url->len;
  1794. }
  1795. found = g_hash_table_lookup (entry->rt->paths, &lookup);
  1796. memcpy (&handler, &found, sizeof (found));
  1797. msg_debug ("requested known path: %T", &lookup);
  1798. }
  1799. entry->is_reply = TRUE;
  1800. if (handler != NULL) {
  1801. return handler (entry, msg);
  1802. }
  1803. else {
  1804. if (entry->rt->default_fs_path == NULL || lookup.len == 0 ||
  1805. !rspamd_http_router_try_file (entry, &lookup, TRUE)) {
  1806. err = g_error_new (HTTP_ERROR, 404,
  1807. "Not found");
  1808. if (entry->rt->error_handler != NULL) {
  1809. entry->rt->error_handler (entry, err);
  1810. }
  1811. msg_info ("path: %T not found", &lookup);
  1812. err_msg = rspamd_http_new_message (HTTP_RESPONSE);
  1813. err_msg->date = time (NULL);
  1814. err_msg->code = err->code;
  1815. err_msg->body = rspamd_fstring_new_init (err->message,
  1816. strlen (err->message));
  1817. rspamd_http_connection_reset (entry->conn);
  1818. rspamd_http_connection_write_message (entry->conn,
  1819. err_msg,
  1820. NULL,
  1821. "text/plain",
  1822. entry,
  1823. entry->conn->fd,
  1824. entry->rt->ptv,
  1825. entry->rt->ev_base);
  1826. g_error_free (err);
  1827. }
  1828. }
  1829. }
  1830. return 0;
  1831. }
  1832. struct rspamd_http_connection_router *
  1833. rspamd_http_router_new (rspamd_http_router_error_handler_t eh,
  1834. rspamd_http_router_finish_handler_t fh,
  1835. struct timeval *timeout, struct event_base *base,
  1836. const char *default_fs_path,
  1837. struct rspamd_keypair_cache *cache)
  1838. {
  1839. struct rspamd_http_connection_router * new;
  1840. struct stat st;
  1841. new = g_slice_alloc0 (sizeof (struct rspamd_http_connection_router));
  1842. new->paths = g_hash_table_new_full (rspamd_ftok_icase_hash,
  1843. rspamd_ftok_icase_equal, rspamd_fstring_mapped_ftok_free, NULL);
  1844. new->conns = NULL;
  1845. new->error_handler = eh;
  1846. new->finish_handler = fh;
  1847. new->ev_base = base;
  1848. if (timeout) {
  1849. new->tv = *timeout;
  1850. new->ptv = &new->tv;
  1851. }
  1852. else {
  1853. new->ptv = NULL;
  1854. }
  1855. new->default_fs_path = NULL;
  1856. if (default_fs_path != NULL) {
  1857. if (stat (default_fs_path, &st) == -1) {
  1858. msg_err ("cannot stat %s", default_fs_path);
  1859. }
  1860. else {
  1861. if (!S_ISDIR (st.st_mode)) {
  1862. msg_err ("path %s is not a directory", default_fs_path);
  1863. }
  1864. else {
  1865. new->default_fs_path = realpath (default_fs_path, NULL);
  1866. }
  1867. }
  1868. }
  1869. new->cache = cache;
  1870. return new;
  1871. }
  1872. void
  1873. rspamd_http_router_set_key (struct rspamd_http_connection_router *router,
  1874. gpointer key)
  1875. {
  1876. struct rspamd_http_keypair *kp = (struct rspamd_http_keypair *)key;
  1877. g_assert (key != NULL);
  1878. REF_RETAIN (kp);
  1879. router->key = key;
  1880. }
  1881. void
  1882. rspamd_http_router_add_path (struct rspamd_http_connection_router *router,
  1883. const gchar *path, rspamd_http_router_handler_t handler)
  1884. {
  1885. gpointer ptr;
  1886. rspamd_ftok_t *key;
  1887. rspamd_fstring_t *storage;
  1888. G_STATIC_ASSERT (sizeof (rspamd_http_router_handler_t) ==
  1889. sizeof (gpointer));
  1890. if (path != NULL && handler != NULL && router != NULL) {
  1891. memcpy (&ptr, &handler, sizeof (ptr));
  1892. storage = rspamd_fstring_new_init (path, strlen (path));
  1893. key = g_slice_alloc0 (sizeof (*key));
  1894. key->begin = storage->str;
  1895. key->len = storage->len;
  1896. g_hash_table_insert (router->paths, key, ptr);
  1897. }
  1898. }
  1899. void
  1900. rspamd_http_router_handle_socket (struct rspamd_http_connection_router *router,
  1901. gint fd, gpointer ud)
  1902. {
  1903. struct rspamd_http_connection_entry *conn;
  1904. conn = g_slice_alloc (sizeof (struct rspamd_http_connection_entry));
  1905. conn->rt = router;
  1906. conn->ud = ud;
  1907. conn->is_reply = FALSE;
  1908. conn->conn = rspamd_http_connection_new (NULL,
  1909. rspamd_http_router_error_handler,
  1910. rspamd_http_router_finish_handler,
  1911. 0,
  1912. RSPAMD_HTTP_SERVER, router->cache);
  1913. if (router->key) {
  1914. rspamd_http_connection_set_key (conn->conn, router->key);
  1915. }
  1916. rspamd_http_connection_read_message (conn->conn, conn, fd, router->ptv,
  1917. router->ev_base);
  1918. DL_PREPEND (router->conns, conn);
  1919. }
  1920. void
  1921. rspamd_http_router_free (struct rspamd_http_connection_router *router)
  1922. {
  1923. struct rspamd_http_connection_entry *conn, *tmp;
  1924. struct rspamd_http_keypair *kp;
  1925. if (router) {
  1926. DL_FOREACH_SAFE (router->conns, conn, tmp)
  1927. {
  1928. rspamd_http_entry_free (conn);
  1929. }
  1930. if (router->key) {
  1931. kp = (struct rspamd_http_keypair *)router->key;
  1932. REF_RELEASE (kp);
  1933. }
  1934. if (router->cache) {
  1935. rspamd_keypair_cache_destroy (router->cache);
  1936. }
  1937. if (router->default_fs_path != NULL) {
  1938. g_free (router->default_fs_path);
  1939. }
  1940. g_hash_table_unref (router->paths);
  1941. g_slice_free1 (sizeof (struct rspamd_http_connection_router), router);
  1942. }
  1943. }
  1944. gpointer
  1945. rspamd_http_connection_make_key (gchar *key, gsize keylen)
  1946. {
  1947. guchar *decoded_sk, *decoded_pk;
  1948. gchar *semicolon;
  1949. gsize decoded_len;
  1950. struct rspamd_http_keypair *kp;
  1951. guchar kh[rspamd_cryptobox_HASHBYTES];
  1952. semicolon = memchr (key, ':', keylen);
  1953. if (semicolon) {
  1954. decoded_sk = rspamd_decode_base32 (key, semicolon - key, &decoded_len);
  1955. decoded_pk = rspamd_decode_base32 (semicolon + 1,
  1956. keylen - (semicolon - key + 1),
  1957. &decoded_len);
  1958. }
  1959. else {
  1960. decoded_sk = rspamd_decode_base32 (key, keylen / 2, &decoded_len);
  1961. decoded_pk = rspamd_decode_base32 (key + keylen / 2,
  1962. keylen / 2,
  1963. &decoded_len);
  1964. }
  1965. if (decoded_pk != NULL && decoded_sk != NULL) {
  1966. if (decoded_len == rspamd_cryptobox_pk_bytes ()) {
  1967. kp = g_slice_alloc (sizeof (*kp));
  1968. REF_INIT_RETAIN (kp, rspamd_http_keypair_dtor);
  1969. memcpy (kp->sk, decoded_sk, rspamd_cryptobox_sk_bytes ());
  1970. memcpy (kp->pk, decoded_pk, rspamd_cryptobox_pk_bytes ());
  1971. rspamd_cryptobox_hash (kh, kp->pk, rspamd_cryptobox_pk_bytes (),
  1972. NULL, 0);
  1973. memcpy (kp->id, kh, sizeof (kp->id));
  1974. return (gpointer) kp;
  1975. }
  1976. g_free (decoded_pk);
  1977. g_free (decoded_sk);
  1978. }
  1979. return NULL;
  1980. }
  1981. gpointer
  1982. rspamd_http_connection_gen_key (void)
  1983. {
  1984. struct rspamd_http_keypair *kp;
  1985. guchar kh[rspamd_cryptobox_HASHBYTES];
  1986. kp = g_slice_alloc (sizeof (*kp));
  1987. REF_INIT_RETAIN (kp, rspamd_http_keypair_dtor);
  1988. rspamd_cryptobox_keypair (kp->pk, kp->sk);
  1989. rspamd_cryptobox_hash (kh, kp->pk, rspamd_cryptobox_pk_bytes (),
  1990. NULL, 0);
  1991. memcpy (kp->id, kh, sizeof (kp->id));
  1992. return (gpointer)kp;
  1993. }
  1994. static void
  1995. rspamd_http_print_key_component (guchar *data, gsize datalen,
  1996. GString *res, guint how, const gchar *description)
  1997. {
  1998. gchar *b32;
  1999. if (how & RSPAMD_KEYPAIR_HUMAN) {
  2000. g_string_append_printf (res, "%s: ", description);
  2001. }
  2002. if (how & RSPAMD_KEYPAIR_BASE32) {
  2003. b32 = rspamd_encode_base32 (data, datalen);
  2004. g_string_append_printf (res, "%s", b32);
  2005. g_free (b32);
  2006. }
  2007. else if (how & RSPAMD_KEYPAIR_HEX) {
  2008. rspamd_printf_gstring (res, "%*xs", (gint)datalen, data);
  2009. }
  2010. else {
  2011. g_string_append_len (res, data, datalen);
  2012. }
  2013. if (how & RSPAMD_KEYPAIR_HUMAN) {
  2014. g_string_append_c (res, '\n');
  2015. }
  2016. }
  2017. GString *
  2018. rspamd_http_connection_print_key (gpointer key, guint how)
  2019. {
  2020. struct rspamd_http_keypair *kp = (struct rspamd_http_keypair *)key;
  2021. GString *res;
  2022. g_assert (key != NULL);
  2023. res = g_string_new (NULL);
  2024. if ((how & RSPAMD_KEYPAIR_PUBKEY)) {
  2025. rspamd_http_print_key_component (kp->pk,
  2026. rspamd_cryptobox_pk_bytes (), res, how,
  2027. "Public key");
  2028. }
  2029. if ((how & RSPAMD_KEYPAIR_PRIVKEY)) {
  2030. rspamd_http_print_key_component (kp->sk, rspamd_cryptobox_sk_bytes (),
  2031. res, how,
  2032. "Private key");
  2033. }
  2034. if ((how & RSPAMD_KEYPAIR_ID)) {
  2035. rspamd_http_print_key_component (kp->id, RSPAMD_HTTP_KEY_ID_LEN, res, how,
  2036. "Key ID");
  2037. }
  2038. return res;
  2039. }
  2040. void
  2041. rspamd_http_connection_set_key (struct rspamd_http_connection *conn,
  2042. gpointer key)
  2043. {
  2044. struct rspamd_http_connection_private *priv = conn->priv;
  2045. struct rspamd_http_keypair *kp = (struct rspamd_http_keypair *)key;
  2046. g_assert (key != NULL);
  2047. REF_RETAIN (kp);
  2048. priv->local_key = kp;
  2049. }
  2050. gboolean
  2051. rspamd_http_connection_is_encrypted (struct rspamd_http_connection *conn)
  2052. {
  2053. struct rspamd_http_connection_private *priv = conn->priv;
  2054. if (priv->peer_key != NULL) {
  2055. return TRUE;
  2056. }
  2057. else if (priv->msg) {
  2058. return priv->msg->peer_key != NULL;
  2059. }
  2060. return FALSE;
  2061. }
  2062. void
  2063. rspamd_http_connection_key_unref (gpointer key)
  2064. {
  2065. struct rspamd_http_keypair *kp = (struct rspamd_http_keypair *)key;
  2066. g_assert (key != NULL);
  2067. REF_RELEASE (kp);
  2068. }
  2069. gpointer
  2070. rspamd_http_connection_key_ref (gpointer key)
  2071. {
  2072. struct rspamd_http_keypair *kp = (struct rspamd_http_keypair *)key;
  2073. g_assert (key != NULL);
  2074. REF_RETAIN (kp);
  2075. return kp;
  2076. }
  2077. gpointer
  2078. rspamd_http_connection_make_peer_key (const gchar *key)
  2079. {
  2080. guchar *pk_decoded;
  2081. gsize dec_len;
  2082. struct rspamd_http_keypair *kp = NULL;
  2083. guchar kh[rspamd_cryptobox_HASHBYTES];
  2084. pk_decoded = rspamd_decode_base32 (key, strlen (key), &dec_len);
  2085. if (pk_decoded != NULL && dec_len == rspamd_cryptobox_pk_bytes ()) {
  2086. kp = g_slice_alloc0 (sizeof (*kp));
  2087. REF_INIT_RETAIN (kp, rspamd_http_keypair_dtor);
  2088. memcpy (kp->pk, pk_decoded, rspamd_cryptobox_pk_bytes ());
  2089. rspamd_cryptobox_hash (kh, kp->pk, rspamd_cryptobox_pk_bytes (),
  2090. NULL, 0);
  2091. memcpy (kp->id, kh, sizeof (kp->id));
  2092. }
  2093. g_free (pk_decoded);
  2094. return kp;
  2095. }
  2096. GHashTable *
  2097. rspamd_http_message_parse_query (struct rspamd_http_message *msg)
  2098. {
  2099. GHashTable *res;
  2100. rspamd_fstring_t *key = NULL, *value = NULL;
  2101. rspamd_ftok_t *key_tok = NULL, *value_tok = NULL;
  2102. const gchar *p, *c, *end;
  2103. struct http_parser_url u;
  2104. enum {
  2105. parse_key,
  2106. parse_eqsign,
  2107. parse_value,
  2108. parse_ampersand
  2109. } state = parse_key;
  2110. res = g_hash_table_new_full (rspamd_ftok_icase_hash,
  2111. rspamd_ftok_icase_equal,
  2112. rspamd_fstring_mapped_ftok_free,
  2113. rspamd_fstring_mapped_ftok_free);
  2114. if (msg->url && msg->url->len > 0) {
  2115. http_parser_parse_url (msg->url->str, msg->url->len, TRUE, &u);
  2116. if (u.field_set & (1 << UF_QUERY)) {
  2117. p = msg->url->str + u.field_data[UF_QUERY].off;
  2118. c = p;
  2119. end = p + u.field_data[UF_QUERY].len;
  2120. while (p <= end) {
  2121. switch (state) {
  2122. case parse_key:
  2123. if ((p == end || *p == '&') && p > c) {
  2124. /* We have a single parameter without a value */
  2125. key = rspamd_fstring_new_init (c, p - c);
  2126. key_tok = rspamd_ftok_map (key);
  2127. key_tok->len = rspamd_decode_url (key->str, key->str,
  2128. key->len);
  2129. value = rspamd_fstring_new_init ("", 0);
  2130. value_tok = rspamd_ftok_map (value);
  2131. g_hash_table_replace (res, key_tok, value_tok);
  2132. state = parse_ampersand;
  2133. }
  2134. else if (*p == '=' && p > c) {
  2135. /* We have something like key=value */
  2136. key = rspamd_fstring_new_init (c, p - c);
  2137. key_tok = rspamd_ftok_map (key);
  2138. key_tok->len = rspamd_decode_url (key->str, key->str,
  2139. key->len);
  2140. state = parse_eqsign;
  2141. }
  2142. else {
  2143. p ++;
  2144. }
  2145. break;
  2146. case parse_eqsign:
  2147. if (*p != '=') {
  2148. c = p;
  2149. state = parse_value;
  2150. }
  2151. else {
  2152. p ++;
  2153. }
  2154. break;
  2155. case parse_value:
  2156. if ((p == end || *p == '&') && p >= c) {
  2157. g_assert (key != NULL);
  2158. if (p > c) {
  2159. value = rspamd_fstring_new_init (c, p - c);
  2160. value_tok = rspamd_ftok_map (value);
  2161. value_tok->len = rspamd_decode_url (value->str,
  2162. value->str,
  2163. value->len);
  2164. /* Detect quotes for value */
  2165. if (value_tok->begin[0] == '"') {
  2166. memmove (value->str, value->str + 1,
  2167. value_tok->len - 1);
  2168. value_tok->len --;
  2169. }
  2170. if (value_tok->begin[value_tok->len - 1] == '"') {
  2171. value_tok->len --;
  2172. }
  2173. }
  2174. else {
  2175. value = rspamd_fstring_new_init ("", 0);
  2176. value_tok = rspamd_ftok_map (value);
  2177. }
  2178. g_hash_table_replace (res, key_tok, value_tok);
  2179. key = value = NULL;
  2180. key_tok = value_tok = NULL;
  2181. state = parse_ampersand;
  2182. }
  2183. else {
  2184. p ++;
  2185. }
  2186. break;
  2187. case parse_ampersand:
  2188. if (p != end && *p != '&') {
  2189. c = p;
  2190. state = parse_key;
  2191. }
  2192. else {
  2193. p ++;
  2194. }
  2195. break;
  2196. }
  2197. }
  2198. }
  2199. if (state != parse_ampersand && key != NULL) {
  2200. rspamd_fstring_free (key);
  2201. }
  2202. }
  2203. return res;
  2204. }