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.

archives.c 51KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088
  1. /*
  2. * Copyright 2024 Vsevolod Stakhov
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "config.h"
  17. #include "message.h"
  18. #include "task.h"
  19. #include "archives.h"
  20. #include "libmime/mime_encoding.h"
  21. #include <unicode/uchar.h>
  22. #include <unicode/utf8.h>
  23. #include <unicode/utf16.h>
  24. #include <unicode/ucnv.h>
  25. #include <archive.h>
  26. #include <archive_entry.h>
  27. #define msg_debug_archive(...) rspamd_conditional_debug_fast(NULL, NULL, \
  28. rspamd_archive_log_id, "archive", task->task_pool->tag.uid, \
  29. G_STRFUNC, \
  30. __VA_ARGS__)
  31. INIT_LOG_MODULE(archive)
  32. static void
  33. rspamd_archive_dtor(gpointer p)
  34. {
  35. struct rspamd_archive *arch = p;
  36. struct rspamd_archive_file *f;
  37. guint i;
  38. for (i = 0; i < arch->files->len; i++) {
  39. f = g_ptr_array_index(arch->files, i);
  40. if (f->fname) {
  41. g_string_free(f->fname, TRUE);
  42. }
  43. g_free(f);
  44. }
  45. g_ptr_array_free(arch->files, TRUE);
  46. }
  47. static bool
  48. rspamd_archive_file_try_utf(struct rspamd_task *task,
  49. struct rspamd_archive *arch,
  50. struct rspamd_archive_file *fentry,
  51. const gchar *in, gsize inlen)
  52. {
  53. const gchar *charset = NULL, *p, *end;
  54. GString *res;
  55. charset = rspamd_mime_charset_find_by_content(in, inlen, TRUE);
  56. if (charset) {
  57. UChar *tmp;
  58. UErrorCode uc_err = U_ZERO_ERROR;
  59. gint32 r, clen, dlen;
  60. struct rspamd_charset_converter *conv;
  61. UConverter *utf8_converter;
  62. conv = rspamd_mime_get_converter_cached(charset, task->task_pool,
  63. TRUE, &uc_err);
  64. utf8_converter = rspamd_get_utf8_converter();
  65. if (conv == NULL) {
  66. msg_info_task("cannot open converter for %s: %s",
  67. charset, u_errorName(uc_err));
  68. fentry->flags |= RSPAMD_ARCHIVE_FILE_OBFUSCATED;
  69. fentry->fname = g_string_new_len(in, inlen);
  70. return false;
  71. }
  72. tmp = g_malloc(sizeof(*tmp) * (inlen + 1));
  73. r = rspamd_converter_to_uchars(conv, tmp, inlen + 1,
  74. in, inlen, &uc_err);
  75. if (!U_SUCCESS(uc_err)) {
  76. msg_info_task("cannot convert data to unicode from %s: %s",
  77. charset, u_errorName(uc_err));
  78. g_free(tmp);
  79. fentry->flags |= RSPAMD_ARCHIVE_FILE_OBFUSCATED;
  80. fentry->fname = g_string_new_len(in, inlen);
  81. return NULL;
  82. }
  83. int i = 0;
  84. while (i < r) {
  85. UChar32 uc;
  86. U16_NEXT(tmp, i, r, uc);
  87. if (IS_ZERO_WIDTH_SPACE(uc) || u_iscntrl(uc)) {
  88. msg_info_task("control character in archive file name found: 0x%02xd "
  89. "(filename=%T)",
  90. uc, arch->archive_name);
  91. fentry->flags |= RSPAMD_ARCHIVE_FILE_OBFUSCATED;
  92. break;
  93. }
  94. }
  95. clen = ucnv_getMaxCharSize(utf8_converter);
  96. dlen = UCNV_GET_MAX_BYTES_FOR_STRING(r, clen);
  97. res = g_string_sized_new(dlen);
  98. r = ucnv_fromUChars(utf8_converter, res->str, dlen, tmp, r, &uc_err);
  99. if (!U_SUCCESS(uc_err)) {
  100. msg_info_task("cannot convert data from unicode from %s: %s",
  101. charset, u_errorName(uc_err));
  102. g_free(tmp);
  103. g_string_free(res, TRUE);
  104. fentry->flags |= RSPAMD_ARCHIVE_FILE_OBFUSCATED;
  105. fentry->fname = g_string_new_len(in, inlen);
  106. return NULL;
  107. }
  108. g_free(tmp);
  109. res->len = r;
  110. msg_debug_archive("converted from %s to UTF-8 inlen: %z, outlen: %d",
  111. charset, inlen, r);
  112. fentry->fname = res;
  113. }
  114. else {
  115. /* Convert unsafe characters to '?' */
  116. res = g_string_sized_new(inlen);
  117. p = in;
  118. end = in + inlen;
  119. while (p < end) {
  120. if (g_ascii_isgraph(*p)) {
  121. g_string_append_c(res, *p);
  122. }
  123. else {
  124. g_string_append_c(res, '?');
  125. if (*p < 0x7f && (g_ascii_iscntrl(*p) || *p == '\0')) {
  126. if (!(fentry->flags & RSPAMD_ARCHIVE_FILE_OBFUSCATED)) {
  127. msg_info_task("suspicious character in archive file name found: 0x%02xd "
  128. "(filename=%T)",
  129. (int) *p, arch->archive_name);
  130. fentry->flags |= RSPAMD_ARCHIVE_FILE_OBFUSCATED;
  131. }
  132. }
  133. }
  134. p++;
  135. }
  136. fentry->fname = res;
  137. }
  138. return true;
  139. }
  140. static void
  141. rspamd_archive_process_zip(struct rspamd_task *task,
  142. struct rspamd_mime_part *part)
  143. {
  144. const guchar *p, *start, *end, *eocd = NULL, *cd;
  145. const guint32 eocd_magic = 0x06054b50, cd_basic_len = 46;
  146. const guchar cd_magic[] = {0x50, 0x4b, 0x01, 0x02};
  147. const guint max_processed = 1024;
  148. guint32 cd_offset, cd_size, comp_size, uncomp_size, processed = 0;
  149. guint16 extra_len, fname_len, comment_len;
  150. struct rspamd_archive *arch;
  151. struct rspamd_archive_file *f = NULL;
  152. /* Zip files have interesting data at the end of archive */
  153. p = part->parsed_data.begin + part->parsed_data.len - 1;
  154. start = part->parsed_data.begin;
  155. end = p;
  156. /* Search for EOCD:
  157. * 22 bytes is a typical size of eocd without a comment and
  158. * end points one byte after the last character
  159. */
  160. p -= 21;
  161. while (p > start + sizeof(guint32)) {
  162. guint32 t;
  163. if (processed > max_processed) {
  164. break;
  165. }
  166. /* XXX: not an efficient approach */
  167. memcpy(&t, p, sizeof(t));
  168. if (GUINT32_FROM_LE(t) == eocd_magic) {
  169. eocd = p;
  170. break;
  171. }
  172. p--;
  173. processed++;
  174. }
  175. if (eocd == NULL) {
  176. /* Not a zip file */
  177. msg_info_task("zip archive is invalid (no EOCD)");
  178. return;
  179. }
  180. if (end - eocd < 21) {
  181. msg_info_task("zip archive is invalid (short EOCD)");
  182. return;
  183. }
  184. memcpy(&cd_size, eocd + 12, sizeof(cd_size));
  185. cd_size = GUINT32_FROM_LE(cd_size);
  186. memcpy(&cd_offset, eocd + 16, sizeof(cd_offset));
  187. cd_offset = GUINT32_FROM_LE(cd_offset);
  188. /* We need to check sanity as well */
  189. if (cd_offset + cd_size > (guint) (eocd - start)) {
  190. msg_info_task("zip archive is invalid (bad size/offset for CD)");
  191. return;
  192. }
  193. cd = start + cd_offset;
  194. arch = rspamd_mempool_alloc0(task->task_pool, sizeof(*arch));
  195. arch->files = g_ptr_array_new();
  196. arch->type = RSPAMD_ARCHIVE_ZIP;
  197. if (part->cd) {
  198. arch->archive_name = &part->cd->filename;
  199. }
  200. rspamd_mempool_add_destructor(task->task_pool, rspamd_archive_dtor,
  201. arch);
  202. while (cd < start + cd_offset + cd_size) {
  203. guint16 flags;
  204. /* Read central directory record */
  205. if (eocd - cd < cd_basic_len ||
  206. memcmp(cd, cd_magic, sizeof(cd_magic)) != 0) {
  207. msg_info_task("zip archive is invalid (bad cd record)");
  208. return;
  209. }
  210. memcpy(&flags, cd + 8, sizeof(guint16));
  211. flags = GUINT16_FROM_LE(flags);
  212. memcpy(&comp_size, cd + 20, sizeof(guint32));
  213. comp_size = GUINT32_FROM_LE(comp_size);
  214. memcpy(&uncomp_size, cd + 24, sizeof(guint32));
  215. uncomp_size = GUINT32_FROM_LE(uncomp_size);
  216. memcpy(&fname_len, cd + 28, sizeof(fname_len));
  217. fname_len = GUINT16_FROM_LE(fname_len);
  218. memcpy(&extra_len, cd + 30, sizeof(extra_len));
  219. extra_len = GUINT16_FROM_LE(extra_len);
  220. memcpy(&comment_len, cd + 32, sizeof(comment_len));
  221. comment_len = GUINT16_FROM_LE(comment_len);
  222. if (cd + fname_len + comment_len + extra_len + cd_basic_len > eocd) {
  223. msg_info_task("zip archive is invalid (too large cd record)");
  224. return;
  225. }
  226. f = g_malloc0(sizeof(*f));
  227. rspamd_archive_file_try_utf(task, arch, f, cd + cd_basic_len, fname_len);
  228. f->compressed_size = comp_size;
  229. f->uncompressed_size = uncomp_size;
  230. if (flags & 0x41u) {
  231. f->flags |= RSPAMD_ARCHIVE_FILE_ENCRYPTED;
  232. }
  233. if (f->fname) {
  234. if (f->flags & RSPAMD_ARCHIVE_FILE_OBFUSCATED) {
  235. arch->flags |= RSPAMD_ARCHIVE_HAS_OBFUSCATED_FILES;
  236. }
  237. g_ptr_array_add(arch->files, f);
  238. msg_debug_archive("found file in zip archive: %v", f->fname);
  239. }
  240. else {
  241. g_free(f);
  242. return;
  243. }
  244. /* Process extra fields */
  245. const guchar *extra = cd + fname_len + cd_basic_len;
  246. p = extra;
  247. while (p + sizeof(guint16) * 2 < extra + extra_len) {
  248. guint16 hid, hlen;
  249. memcpy(&hid, p, sizeof(guint16));
  250. hid = GUINT16_FROM_LE(hid);
  251. memcpy(&hlen, p + sizeof(guint16), sizeof(guint16));
  252. hlen = GUINT16_FROM_LE(hlen);
  253. if (hid == 0x0017) {
  254. f->flags |= RSPAMD_ARCHIVE_FILE_ENCRYPTED;
  255. }
  256. p += hlen + sizeof(guint16) * 2;
  257. }
  258. cd += fname_len + comment_len + extra_len + cd_basic_len;
  259. }
  260. part->part_type = RSPAMD_MIME_PART_ARCHIVE;
  261. part->specific.arch = arch;
  262. arch->size = part->parsed_data.len;
  263. }
  264. static inline gint
  265. rspamd_archive_rar_read_vint(const guchar *start, gsize remain, guint64 *res)
  266. {
  267. /*
  268. * From http://www.rarlab.com/technote.htm:
  269. * Variable length integer. Can include one or more bytes, where
  270. * lower 7 bits of every byte contain integer data and highest bit
  271. * in every byte is the continuation flag.
  272. * If highest bit is 0, this is the last byte in sequence.
  273. * So first byte contains 7 least significant bits of integer and
  274. * continuation flag. Second byte, if present, contains next 7 bits and so on.
  275. */
  276. guint64 t = 0;
  277. guint shift = 0;
  278. const guchar *p = start;
  279. while (remain > 0 && shift <= 57) {
  280. if (*p & 0x80) {
  281. t |= ((guint64) (*p & 0x7f)) << shift;
  282. }
  283. else {
  284. t |= ((guint64) (*p & 0x7f)) << shift;
  285. p++;
  286. break;
  287. }
  288. shift += 7;
  289. p++;
  290. remain--;
  291. }
  292. if (remain == 0 || shift > 64) {
  293. return -1;
  294. }
  295. *res = GUINT64_FROM_LE(t);
  296. return p - start;
  297. }
  298. #define RAR_SKIP_BYTES(n) \
  299. do { \
  300. if ((n) <= 0) { \
  301. msg_debug_archive("rar archive is invalid (bad skip value)"); \
  302. return; \
  303. } \
  304. if ((gsize) (end - p) < (n)) { \
  305. msg_debug_archive("rar archive is invalid (truncated)"); \
  306. return; \
  307. } \
  308. p += (n); \
  309. } while (0)
  310. #define RAR_READ_VINT() \
  311. do { \
  312. r = rspamd_archive_rar_read_vint(p, end - p, &vint); \
  313. if (r == -1) { \
  314. msg_debug_archive("rar archive is invalid (bad vint)"); \
  315. return; \
  316. } \
  317. else if (r == 0) { \
  318. msg_debug_archive("rar archive is invalid (BAD vint offset)"); \
  319. return; \
  320. } \
  321. } while (0)
  322. #define RAR_READ_VINT_SKIP() \
  323. do { \
  324. r = rspamd_archive_rar_read_vint(p, end - p, &vint); \
  325. if (r == -1) { \
  326. msg_debug_archive("rar archive is invalid (bad vint)"); \
  327. return; \
  328. } \
  329. p += r; \
  330. } while (0)
  331. #define RAR_READ_UINT16(n) \
  332. do { \
  333. if (end - p < (glong) sizeof(guint16)) { \
  334. msg_debug_archive("rar archive is invalid (bad int16)"); \
  335. return; \
  336. } \
  337. n = p[0] + (p[1] << 8); \
  338. p += sizeof(guint16); \
  339. } while (0)
  340. #define RAR_READ_UINT32(n) \
  341. do { \
  342. if (end - p < (glong) sizeof(guint32)) { \
  343. msg_debug_archive("rar archive is invalid (bad int32)"); \
  344. return; \
  345. } \
  346. n = (guint) p[0] + ((guint) p[1] << 8) + ((guint) p[2] << 16) + ((guint) p[3] << 24); \
  347. p += sizeof(guint32); \
  348. } while (0)
  349. static void
  350. rspamd_archive_process_rar_v4(struct rspamd_task *task, const guchar *start,
  351. const guchar *end, struct rspamd_mime_part *part)
  352. {
  353. const guchar *p = start, *start_section;
  354. guint8 type;
  355. guint flags;
  356. guint64 sz, comp_sz = 0, uncomp_sz = 0;
  357. struct rspamd_archive *arch;
  358. struct rspamd_archive_file *f;
  359. arch = rspamd_mempool_alloc0(task->task_pool, sizeof(*arch));
  360. arch->files = g_ptr_array_new();
  361. arch->type = RSPAMD_ARCHIVE_RAR;
  362. if (part->cd) {
  363. arch->archive_name = &part->cd->filename;
  364. }
  365. rspamd_mempool_add_destructor(task->task_pool, rspamd_archive_dtor,
  366. arch);
  367. while (p < end) {
  368. /* Crc16 */
  369. start_section = p;
  370. RAR_SKIP_BYTES(sizeof(guint16));
  371. type = *p;
  372. p++;
  373. RAR_READ_UINT16(flags);
  374. if (type == 0x73) {
  375. /* Main header, check for encryption */
  376. if (flags & 0x80) {
  377. arch->flags |= RSPAMD_ARCHIVE_ENCRYPTED;
  378. goto end;
  379. }
  380. }
  381. RAR_READ_UINT16(sz);
  382. if (flags & 0x8000) {
  383. /* We also need to read ADD_SIZE element */
  384. guint32 tmp;
  385. RAR_READ_UINT32(tmp);
  386. sz += tmp;
  387. /* This is also used as PACK_SIZE */
  388. comp_sz = tmp;
  389. }
  390. if (sz == 0) {
  391. /* Zero sized block - error */
  392. msg_debug_archive("rar archive is invalid (zero size block)");
  393. return;
  394. }
  395. if (type == 0x74) {
  396. guint fname_len;
  397. /* File header */
  398. /* Uncompressed size */
  399. RAR_READ_UINT32(uncomp_sz);
  400. /* Skip to NAME_SIZE element */
  401. RAR_SKIP_BYTES(11);
  402. RAR_READ_UINT16(fname_len);
  403. if (fname_len == 0 || fname_len > (gsize) (end - p)) {
  404. msg_debug_archive("rar archive is invalid (bad filename size: %d)",
  405. fname_len);
  406. return;
  407. }
  408. /* Attrs */
  409. RAR_SKIP_BYTES(4);
  410. if (flags & 0x100) {
  411. /* We also need to read HIGH_PACK_SIZE */
  412. guint32 tmp;
  413. RAR_READ_UINT32(tmp);
  414. sz += tmp;
  415. comp_sz += tmp;
  416. /* HIGH_UNP_SIZE */
  417. RAR_READ_UINT32(tmp);
  418. uncomp_sz += tmp;
  419. }
  420. f = g_malloc0(sizeof(*f));
  421. if (flags & 0x200) {
  422. /* We have unicode + normal version */
  423. guchar *tmp;
  424. tmp = memchr(p, '\0', fname_len);
  425. if (tmp != NULL) {
  426. /* Just use ASCII version */
  427. rspamd_archive_file_try_utf(task, arch, f, p, tmp - p);
  428. msg_debug_archive("found ascii filename in rarv4 archive: %v",
  429. f->fname);
  430. }
  431. else {
  432. /* We have UTF8 filename, use it as is */
  433. rspamd_archive_file_try_utf(task, arch, f, p, fname_len);
  434. msg_debug_archive("found utf filename in rarv4 archive: %v",
  435. f->fname);
  436. }
  437. }
  438. else {
  439. rspamd_archive_file_try_utf(task, arch, f, p, fname_len);
  440. msg_debug_archive("found ascii (old) filename in rarv4 archive: %v",
  441. f->fname);
  442. }
  443. f->compressed_size = comp_sz;
  444. f->uncompressed_size = uncomp_sz;
  445. if (flags & 0x4) {
  446. f->flags |= RSPAMD_ARCHIVE_FILE_ENCRYPTED;
  447. }
  448. if (f->fname) {
  449. if (f->flags & RSPAMD_ARCHIVE_FILE_OBFUSCATED) {
  450. arch->flags |= RSPAMD_ARCHIVE_HAS_OBFUSCATED_FILES;
  451. }
  452. g_ptr_array_add(arch->files, f);
  453. }
  454. else {
  455. g_free(f);
  456. }
  457. }
  458. p = start_section;
  459. RAR_SKIP_BYTES(sz);
  460. }
  461. end:
  462. part->part_type = RSPAMD_MIME_PART_ARCHIVE;
  463. part->specific.arch = arch;
  464. arch->size = part->parsed_data.len;
  465. }
  466. static void
  467. rspamd_archive_process_rar(struct rspamd_task *task,
  468. struct rspamd_mime_part *part)
  469. {
  470. const guchar *p, *end, *section_start;
  471. const guchar rar_v5_magic[] = {0x52, 0x61, 0x72, 0x21, 0x1A, 0x07, 0x01, 0x00},
  472. rar_v4_magic[] = {0x52, 0x61, 0x72, 0x21, 0x1A, 0x07, 0x00};
  473. const guint rar_encrypted_header = 4, rar_main_header = 1,
  474. rar_file_header = 2;
  475. guint64 vint, sz, comp_sz = 0, uncomp_sz = 0, flags = 0, type = 0,
  476. extra_sz = 0;
  477. struct rspamd_archive *arch;
  478. struct rspamd_archive_file *f;
  479. gint r;
  480. p = part->parsed_data.begin;
  481. end = p + part->parsed_data.len;
  482. if ((gsize) (end - p) <= sizeof(rar_v5_magic)) {
  483. msg_debug_archive("rar archive is invalid (too small)");
  484. return;
  485. }
  486. if (memcmp(p, rar_v5_magic, sizeof(rar_v5_magic)) == 0) {
  487. p += sizeof(rar_v5_magic);
  488. }
  489. else if (memcmp(p, rar_v4_magic, sizeof(rar_v4_magic)) == 0) {
  490. p += sizeof(rar_v4_magic);
  491. rspamd_archive_process_rar_v4(task, p, end, part);
  492. return;
  493. }
  494. else {
  495. msg_debug_archive("rar archive is invalid (no rar magic)");
  496. return;
  497. }
  498. /* Rar v5 format */
  499. arch = rspamd_mempool_alloc0(task->task_pool, sizeof(*arch));
  500. arch->files = g_ptr_array_new();
  501. arch->type = RSPAMD_ARCHIVE_RAR;
  502. if (part->cd) {
  503. arch->archive_name = &part->cd->filename;
  504. }
  505. rspamd_mempool_add_destructor(task->task_pool, rspamd_archive_dtor,
  506. arch);
  507. /* Now we can have either encryption header or archive header */
  508. /* Crc 32 */
  509. RAR_SKIP_BYTES(sizeof(guint32));
  510. /* Size */
  511. RAR_READ_VINT_SKIP();
  512. sz = vint;
  513. /* Type */
  514. section_start = p;
  515. RAR_READ_VINT_SKIP();
  516. type = vint;
  517. /* Header flags */
  518. RAR_READ_VINT_SKIP();
  519. flags = vint;
  520. if (flags & 0x1) {
  521. /* Have extra zone */
  522. RAR_READ_VINT_SKIP();
  523. }
  524. if (flags & 0x2) {
  525. /* Data zone is presented */
  526. RAR_READ_VINT_SKIP();
  527. sz += vint;
  528. }
  529. if (type == rar_encrypted_header) {
  530. /* We can't read any further information as archive is encrypted */
  531. arch->flags |= RSPAMD_ARCHIVE_ENCRYPTED;
  532. goto end;
  533. }
  534. else if (type != rar_main_header) {
  535. msg_debug_archive("rar archive is invalid (bad main header)");
  536. return;
  537. }
  538. /* Nothing useful in main header */
  539. p = section_start;
  540. RAR_SKIP_BYTES(sz);
  541. while (p < end) {
  542. gboolean has_extra = FALSE;
  543. /* Read the next header */
  544. /* Crc 32 */
  545. RAR_SKIP_BYTES(sizeof(guint32));
  546. /* Size */
  547. RAR_READ_VINT_SKIP();
  548. sz = vint;
  549. if (sz == 0) {
  550. /* Zero sized block - error */
  551. msg_debug_archive("rar archive is invalid (zero size block)");
  552. return;
  553. }
  554. section_start = p;
  555. /* Type */
  556. RAR_READ_VINT_SKIP();
  557. type = vint;
  558. /* Header flags */
  559. RAR_READ_VINT_SKIP();
  560. flags = vint;
  561. if (flags & 0x1) {
  562. /* Have extra zone */
  563. RAR_READ_VINT_SKIP();
  564. extra_sz = vint;
  565. has_extra = TRUE;
  566. }
  567. if (flags & 0x2) {
  568. /* Data zone is presented */
  569. RAR_READ_VINT_SKIP();
  570. sz += vint;
  571. comp_sz = vint;
  572. }
  573. if (type != rar_file_header) {
  574. p = section_start;
  575. RAR_SKIP_BYTES(sz);
  576. }
  577. else {
  578. /* We have a file header, go forward */
  579. guint64 fname_len;
  580. bool is_directory = false;
  581. /* File header specific flags */
  582. RAR_READ_VINT_SKIP();
  583. flags = vint;
  584. /* Unpacked size */
  585. RAR_READ_VINT_SKIP();
  586. uncomp_sz = vint;
  587. /* Attributes */
  588. RAR_READ_VINT_SKIP();
  589. if (flags & 0x2) {
  590. /* Unix mtime */
  591. RAR_SKIP_BYTES(sizeof(guint32));
  592. }
  593. if (flags & 0x4) {
  594. /* Crc32 */
  595. RAR_SKIP_BYTES(sizeof(guint32));
  596. }
  597. if (flags & 0x1) {
  598. /* Ignore directories for sanity purposes */
  599. is_directory = true;
  600. msg_debug_archive("skip directory record in a rar archive");
  601. }
  602. if (!is_directory) {
  603. /* Compression */
  604. RAR_READ_VINT_SKIP();
  605. /* Host OS */
  606. RAR_READ_VINT_SKIP();
  607. /* Filename length (finally!) */
  608. RAR_READ_VINT_SKIP();
  609. fname_len = vint;
  610. if (fname_len == 0 || fname_len > (gsize) (end - p)) {
  611. msg_debug_archive("rar archive is invalid (bad filename size)");
  612. return;
  613. }
  614. f = g_malloc0(sizeof(*f));
  615. f->uncompressed_size = uncomp_sz;
  616. f->compressed_size = comp_sz;
  617. rspamd_archive_file_try_utf(task, arch, f, p, fname_len);
  618. if (f->fname) {
  619. msg_debug_archive("added rarv5 file: %v", f->fname);
  620. g_ptr_array_add(arch->files, f);
  621. if (f->flags & RSPAMD_ARCHIVE_FILE_OBFUSCATED) {
  622. arch->flags |= RSPAMD_ARCHIVE_HAS_OBFUSCATED_FILES;
  623. }
  624. }
  625. else {
  626. g_free(f);
  627. f = NULL;
  628. }
  629. if (f && has_extra && extra_sz > 0 &&
  630. p + fname_len + extra_sz < end) {
  631. /* Try to find encryption record in extra field */
  632. const guchar *ex = p + fname_len;
  633. while (ex < p + extra_sz) {
  634. const guchar *t;
  635. gint64 cur_sz = 0, sec_type = 0;
  636. r = rspamd_archive_rar_read_vint(ex, extra_sz, &cur_sz);
  637. if (r == -1) {
  638. msg_debug_archive("rar archive is invalid (bad vint)");
  639. return;
  640. }
  641. t = ex + r;
  642. r = rspamd_archive_rar_read_vint(t, extra_sz - r, &sec_type);
  643. if (r == -1) {
  644. msg_debug_archive("rar archive is invalid (bad vint)");
  645. return;
  646. }
  647. if (sec_type == 0x01) {
  648. f->flags |= RSPAMD_ARCHIVE_FILE_ENCRYPTED;
  649. arch->flags |= RSPAMD_ARCHIVE_ENCRYPTED;
  650. break;
  651. }
  652. ex += cur_sz;
  653. }
  654. }
  655. }
  656. /* Restore p to the beginning of the header */
  657. p = section_start;
  658. RAR_SKIP_BYTES(sz);
  659. }
  660. }
  661. end:
  662. part->part_type = RSPAMD_MIME_PART_ARCHIVE;
  663. part->specific.arch = arch;
  664. arch->size = part->parsed_data.len;
  665. }
  666. static inline gint
  667. rspamd_archive_7zip_read_vint(const guchar *start, gsize remain, guint64 *res)
  668. {
  669. /*
  670. * REAL_UINT64 means real UINT64.
  671. * UINT64 means real UINT64 encoded with the following scheme:
  672. *
  673. * Size of encoding sequence depends from first byte:
  674. * First_Byte Extra_Bytes Value
  675. * (binary)
  676. * 0xxxxxxx : ( xxxxxxx )
  677. * 10xxxxxx BYTE y[1] : ( xxxxxx << (8 * 1)) + y
  678. * 110xxxxx BYTE y[2] : ( xxxxx << (8 * 2)) + y
  679. * ...
  680. * 1111110x BYTE y[6] : ( x << (8 * 6)) + y
  681. * 11111110 BYTE y[7] : y
  682. * 11111111 BYTE y[8] : y
  683. */
  684. guchar t;
  685. if (remain == 0) {
  686. return -1;
  687. }
  688. t = *start;
  689. if (!isset(&t, 7)) {
  690. /* Trivial case */
  691. *res = t;
  692. return 1;
  693. }
  694. else if (t == 0xFF) {
  695. if (remain >= sizeof(guint64) + 1) {
  696. memcpy(res, start + 1, sizeof(guint64));
  697. *res = GUINT64_FROM_LE(*res);
  698. return sizeof(guint64) + 1;
  699. }
  700. }
  701. else {
  702. gint cur_bit = 6, intlen = 1;
  703. const guchar bmask = 0xFF;
  704. guint64 tgt;
  705. while (cur_bit > 0) {
  706. if (!isset(&t, cur_bit)) {
  707. if (remain >= intlen + 1) {
  708. memcpy(&tgt, start + 1, intlen);
  709. tgt = GUINT64_FROM_LE(tgt);
  710. /* Shift back */
  711. tgt >>= sizeof(tgt) - NBBY * intlen;
  712. /* Add masked value */
  713. tgt += (guint64) (t & (bmask >> (NBBY - cur_bit)))
  714. << (NBBY * intlen);
  715. *res = tgt;
  716. return intlen + 1;
  717. }
  718. }
  719. cur_bit--;
  720. intlen++;
  721. }
  722. }
  723. return -1;
  724. }
  725. #define SZ_READ_VINT_SKIP() \
  726. do { \
  727. r = rspamd_archive_7zip_read_vint(p, end - p, &vint); \
  728. if (r == -1) { \
  729. msg_debug_archive("7z archive is invalid (bad vint)"); \
  730. return; \
  731. } \
  732. p += r; \
  733. } while (0)
  734. #define SZ_READ_VINT(var) \
  735. do { \
  736. int r; \
  737. r = rspamd_archive_7zip_read_vint(p, end - p, &(var)); \
  738. if (r == -1) { \
  739. msg_debug_archive("7z archive is invalid (bad vint): %s", G_STRLOC); \
  740. return NULL; \
  741. } \
  742. p += r; \
  743. } while (0)
  744. #define SZ_READ_UINT64(n) \
  745. do { \
  746. if (end - p < (goffset) sizeof(guint64)) { \
  747. msg_debug_archive("7zip archive is invalid (bad uint64): %s", G_STRLOC); \
  748. return; \
  749. } \
  750. memcpy(&(n), p, sizeof(guint64)); \
  751. n = GUINT64_FROM_LE(n); \
  752. p += sizeof(guint64); \
  753. } while (0)
  754. #define SZ_SKIP_BYTES(n) \
  755. do { \
  756. if (end - p >= (n)) { \
  757. p += (n); \
  758. } \
  759. else { \
  760. msg_debug_archive("7zip archive is invalid (truncated); wanted to read %d bytes, %d avail: %s", (gint) (n), (gint) (end - p), G_STRLOC); \
  761. return NULL; \
  762. } \
  763. } while (0)
  764. enum rspamd_7zip_header_mark {
  765. kEnd = 0x00,
  766. kHeader = 0x01,
  767. kArchiveProperties = 0x02,
  768. kAdditionalStreamsInfo = 0x03,
  769. kMainStreamsInfo = 0x04,
  770. kFilesInfo = 0x05,
  771. kPackInfo = 0x06,
  772. kUnPackInfo = 0x07,
  773. kSubStreamsInfo = 0x08,
  774. kSize = 0x09,
  775. kCRC = 0x0A,
  776. kFolder = 0x0B,
  777. kCodersUnPackSize = 0x0C,
  778. kNumUnPackStream = 0x0D,
  779. kEmptyStream = 0x0E,
  780. kEmptyFile = 0x0F,
  781. kAnti = 0x10,
  782. kName = 0x11,
  783. kCTime = 0x12,
  784. kATime = 0x13,
  785. kMTime = 0x14,
  786. kWinAttributes = 0x15,
  787. kComment = 0x16,
  788. kEncodedHeader = 0x17,
  789. kStartPos = 0x18,
  790. kDummy = 0x19,
  791. };
  792. #define _7Z_CRYPTO_MAIN_ZIP 0x06F10101 /* Main Zip crypto algo */
  793. #define _7Z_CRYPTO_RAR_29 0x06F10303 /* Rar29 AES-128 + (modified SHA-1) */
  794. #define _7Z_CRYPTO_AES_256_SHA_256 0x06F10701 /* AES-256 + SHA-256 */
  795. #define IS_SZ_ENCRYPTED(codec_id) (((codec_id) == _7Z_CRYPTO_MAIN_ZIP) || \
  796. ((codec_id) == _7Z_CRYPTO_RAR_29) || \
  797. ((codec_id) == _7Z_CRYPTO_AES_256_SHA_256))
  798. static const guchar *
  799. rspamd_7zip_read_bits(struct rspamd_task *task,
  800. const guchar *p, const guchar *end,
  801. struct rspamd_archive *arch, guint nbits,
  802. guint *pbits_set)
  803. {
  804. unsigned mask = 0, avail = 0, i;
  805. gboolean bit_set = 0;
  806. for (i = 0; i < nbits; i++) {
  807. if (mask == 0) {
  808. avail = *p;
  809. SZ_SKIP_BYTES(1);
  810. mask = 0x80;
  811. }
  812. bit_set = (avail & mask) ? 1 : 0;
  813. if (bit_set && pbits_set) {
  814. (*pbits_set)++;
  815. }
  816. mask >>= 1;
  817. }
  818. return p;
  819. }
  820. static const guchar *
  821. rspamd_7zip_read_digest(struct rspamd_task *task,
  822. const guchar *p, const guchar *end,
  823. struct rspamd_archive *arch,
  824. guint64 num_streams,
  825. guint *pdigest_read)
  826. {
  827. guchar all_defined = *p;
  828. guint64 i;
  829. guint num_defined = 0;
  830. /*
  831. * BYTE AllAreDefined
  832. * if (AllAreDefined == 0)
  833. * {
  834. * for(NumStreams)
  835. * BIT Defined
  836. * }
  837. * UINT32 CRCs[NumDefined]
  838. */
  839. SZ_SKIP_BYTES(1);
  840. if (all_defined) {
  841. num_defined = num_streams;
  842. }
  843. else {
  844. if (num_streams > 8192) {
  845. /* Gah */
  846. return NULL;
  847. }
  848. p = rspamd_7zip_read_bits(task, p, end, arch, num_streams, &num_defined);
  849. if (p == NULL) {
  850. return NULL;
  851. }
  852. }
  853. for (i = 0; i < num_defined; i++) {
  854. SZ_SKIP_BYTES(sizeof(guint32));
  855. }
  856. if (pdigest_read) {
  857. *pdigest_read = num_defined;
  858. }
  859. return p;
  860. }
  861. static const guchar *
  862. rspamd_7zip_read_pack_info(struct rspamd_task *task,
  863. const guchar *p, const guchar *end,
  864. struct rspamd_archive *arch)
  865. {
  866. guint64 pack_pos = 0, pack_streams = 0, i, cur_sz;
  867. guint num_digests = 0;
  868. guchar t;
  869. /*
  870. * UINT64 PackPos
  871. * UINT64 NumPackStreams
  872. *
  873. * []
  874. * BYTE NID::kSize (0x09)
  875. * UINT64 PackSizes[NumPackStreams]
  876. * []
  877. *
  878. * []
  879. * BYTE NID::kCRC (0x0A)
  880. * PackStreamDigests[NumPackStreams]
  881. * []
  882. * BYTE NID::kEnd
  883. */
  884. SZ_READ_VINT(pack_pos);
  885. SZ_READ_VINT(pack_streams);
  886. while (p != NULL && p < end) {
  887. t = *p;
  888. SZ_SKIP_BYTES(1);
  889. msg_debug_archive("7zip: read pack info %xc", t);
  890. switch (t) {
  891. case kSize:
  892. /* We need to skip pack_streams VINTS */
  893. for (i = 0; i < pack_streams; i++) {
  894. SZ_READ_VINT(cur_sz);
  895. }
  896. break;
  897. case kCRC:
  898. /* CRCs are more complicated */
  899. p = rspamd_7zip_read_digest(task, p, end, arch, pack_streams,
  900. &num_digests);
  901. break;
  902. case kEnd:
  903. goto end;
  904. break;
  905. default:
  906. p = NULL;
  907. msg_debug_archive("bad 7zip type: %xc; %s", t, G_STRLOC);
  908. goto end;
  909. break;
  910. }
  911. }
  912. end:
  913. return p;
  914. }
  915. static const guchar *
  916. rspamd_7zip_read_folder(struct rspamd_task *task,
  917. const guchar *p, const guchar *end,
  918. struct rspamd_archive *arch, guint *pnstreams, guint *ndigests)
  919. {
  920. guint64 ncoders = 0, i, j, noutstreams = 0, ninstreams = 0;
  921. SZ_READ_VINT(ncoders);
  922. for (i = 0; i < ncoders && p != NULL && p < end; i++) {
  923. guint64 sz, tmp;
  924. guchar t;
  925. /*
  926. * BYTE
  927. * {
  928. * 0:3 CodecIdSize
  929. * 4: Is Complex Coder
  930. * 5: There Are Attributes
  931. * 6: Reserved
  932. * 7: There are more alternative methods. (Not used anymore, must be 0).
  933. * }
  934. * BYTE CodecId[CodecIdSize]
  935. * if (Is Complex Coder)
  936. * {
  937. * UINT64 NumInStreams;
  938. * UINT64 NumOutStreams;
  939. * }
  940. * if (There Are Attributes)
  941. * {
  942. * UINT64 PropertiesSize
  943. * BYTE Properties[PropertiesSize]
  944. * }
  945. */
  946. t = *p;
  947. SZ_SKIP_BYTES(1);
  948. sz = t & 0xF;
  949. /* Codec ID */
  950. tmp = 0;
  951. for (j = 0; j < sz; j++) {
  952. tmp <<= 8;
  953. tmp += p[j];
  954. }
  955. msg_debug_archive("7zip: read codec id: %L", tmp);
  956. if (IS_SZ_ENCRYPTED(tmp)) {
  957. arch->flags |= RSPAMD_ARCHIVE_ENCRYPTED;
  958. }
  959. SZ_SKIP_BYTES(sz);
  960. if (t & (1u << 4)) {
  961. /* Complex */
  962. SZ_READ_VINT(tmp); /* InStreams */
  963. ninstreams += tmp;
  964. SZ_READ_VINT(tmp); /* OutStreams */
  965. noutstreams += tmp;
  966. }
  967. else {
  968. /* XXX: is it correct ? */
  969. noutstreams++;
  970. ninstreams++;
  971. }
  972. if (t & (1u << 5)) {
  973. /* Attributes ... */
  974. SZ_READ_VINT(tmp); /* Size of attrs */
  975. SZ_SKIP_BYTES(tmp);
  976. }
  977. }
  978. if (noutstreams > 1) {
  979. /* BindPairs, WTF, huh */
  980. for (i = 0; i < noutstreams - 1; i++) {
  981. guint64 tmp;
  982. SZ_READ_VINT(tmp);
  983. SZ_READ_VINT(tmp);
  984. }
  985. }
  986. gint64 npacked = (gint64) ninstreams - (gint64) noutstreams + 1;
  987. msg_debug_archive("7zip: instreams=%L, outstreams=%L, packed=%L",
  988. ninstreams, noutstreams, npacked);
  989. if (npacked > 1) {
  990. /* Gah... */
  991. for (i = 0; i < npacked; i++) {
  992. guint64 tmp;
  993. SZ_READ_VINT(tmp);
  994. }
  995. }
  996. *pnstreams = noutstreams;
  997. (*ndigests) += npacked;
  998. return p;
  999. }
  1000. static const guchar *
  1001. rspamd_7zip_read_coders_info(struct rspamd_task *task,
  1002. const guchar *p, const guchar *end,
  1003. struct rspamd_archive *arch,
  1004. guint *pnum_folders, guint *pnum_nodigest)
  1005. {
  1006. guint64 num_folders = 0, i, tmp;
  1007. guchar t;
  1008. guint *folder_nstreams = NULL, num_digests = 0, digests_read = 0;
  1009. while (p != NULL && p < end) {
  1010. /*
  1011. * BYTE NID::kFolder (0x0B)
  1012. * UINT64 NumFolders
  1013. * BYTE External
  1014. * switch(External)
  1015. * {
  1016. * case 0:
  1017. * Folders[NumFolders]
  1018. * case 1:
  1019. * UINT64 DataStreamIndex
  1020. * }
  1021. * BYTE ID::kCodersUnPackSize (0x0C)
  1022. * for(Folders)
  1023. * for(Folder.NumOutStreams)
  1024. * UINT64 UnPackSize;
  1025. * []
  1026. * BYTE NID::kCRC (0x0A)
  1027. * UnPackDigests[NumFolders]
  1028. * []
  1029. * BYTE NID::kEnd
  1030. */
  1031. t = *p;
  1032. SZ_SKIP_BYTES(1);
  1033. msg_debug_archive("7zip: read coders info %xc", t);
  1034. switch (t) {
  1035. case kFolder:
  1036. SZ_READ_VINT(num_folders);
  1037. msg_debug_archive("7zip: nfolders=%L", num_folders);
  1038. if (*p != 0) {
  1039. /* External folders */
  1040. SZ_SKIP_BYTES(1);
  1041. SZ_READ_VINT(tmp);
  1042. }
  1043. else {
  1044. SZ_SKIP_BYTES(1);
  1045. if (num_folders > 8192) {
  1046. /* Gah */
  1047. return NULL;
  1048. }
  1049. if (folder_nstreams) {
  1050. g_free(folder_nstreams);
  1051. }
  1052. folder_nstreams = g_malloc(sizeof(int) * num_folders);
  1053. for (i = 0; i < num_folders && p != NULL && p < end; i++) {
  1054. p = rspamd_7zip_read_folder(task, p, end, arch,
  1055. &folder_nstreams[i], &num_digests);
  1056. }
  1057. }
  1058. break;
  1059. case kCodersUnPackSize:
  1060. for (i = 0; i < num_folders && p != NULL && p < end; i++) {
  1061. if (folder_nstreams) {
  1062. for (guint j = 0; j < folder_nstreams[i]; j++) {
  1063. SZ_READ_VINT(tmp); /* Unpacked size */
  1064. msg_debug_archive("7zip: unpacked size "
  1065. "(folder=%d, stream=%d) = %L",
  1066. (gint) i, j, tmp);
  1067. }
  1068. }
  1069. else {
  1070. msg_err_task("internal 7zip error");
  1071. }
  1072. }
  1073. break;
  1074. case kCRC:
  1075. /*
  1076. * Here are dragons. Spec tells that here there could be up
  1077. * to nfolders digests. However, according to the actual source
  1078. * code, in case of multiple out streams there should be digests
  1079. * for all out streams.
  1080. *
  1081. * In the real life (tm) it is even more idiotic: all these digests
  1082. * are in another section! But that section needs number of digests
  1083. * that are absent here. It is the most stupid thing I've ever seen
  1084. * in any file format.
  1085. *
  1086. * I hope there *WAS* some reason to do such shit...
  1087. */
  1088. p = rspamd_7zip_read_digest(task, p, end, arch, num_digests,
  1089. &digests_read);
  1090. break;
  1091. case kEnd:
  1092. goto end;
  1093. break;
  1094. default:
  1095. p = NULL;
  1096. msg_debug_archive("bad 7zip type: %xc; %s", t, G_STRLOC);
  1097. goto end;
  1098. break;
  1099. }
  1100. }
  1101. end:
  1102. if (pnum_nodigest) {
  1103. *pnum_nodigest = num_digests - digests_read;
  1104. }
  1105. if (pnum_folders) {
  1106. *pnum_folders = num_folders;
  1107. }
  1108. if (folder_nstreams) {
  1109. g_free(folder_nstreams);
  1110. }
  1111. return p;
  1112. }
  1113. static const guchar *
  1114. rspamd_7zip_read_substreams_info(struct rspamd_task *task,
  1115. const guchar *p, const guchar *end,
  1116. struct rspamd_archive *arch,
  1117. guint num_folders, guint num_nodigest)
  1118. {
  1119. guchar t;
  1120. guint i;
  1121. guint64 *folder_nstreams;
  1122. if (num_folders > 8192) {
  1123. /* Gah */
  1124. return NULL;
  1125. }
  1126. folder_nstreams = g_alloca(sizeof(guint64) * num_folders);
  1127. memset(folder_nstreams, 0, sizeof(guint64) * num_folders);
  1128. while (p != NULL && p < end) {
  1129. /*
  1130. * []
  1131. * BYTE NID::kNumUnPackStream; (0x0D)
  1132. * UINT64 NumUnPackStreamsInFolders[NumFolders];
  1133. * []
  1134. *
  1135. * []
  1136. * BYTE NID::kSize (0x09)
  1137. * UINT64 UnPackSizes[??]
  1138. * []
  1139. *
  1140. *
  1141. * []
  1142. * BYTE NID::kCRC (0x0A)
  1143. * Digests[Number of streams with unknown CRC]
  1144. * []
  1145. */
  1146. t = *p;
  1147. SZ_SKIP_BYTES(1);
  1148. msg_debug_archive("7zip: read substream info %xc", t);
  1149. switch (t) {
  1150. case kNumUnPackStream:
  1151. for (i = 0; i < num_folders; i++) {
  1152. guint64 tmp;
  1153. SZ_READ_VINT(tmp);
  1154. folder_nstreams[i] = tmp;
  1155. }
  1156. break;
  1157. case kCRC:
  1158. /*
  1159. * Read the comment in the rspamd_7zip_read_coders_info
  1160. */
  1161. p = rspamd_7zip_read_digest(task, p, end, arch, num_nodigest,
  1162. NULL);
  1163. break;
  1164. case kSize:
  1165. /*
  1166. * Another brain damaged logic, but we have to support it
  1167. * as there are no ways to proceed without it.
  1168. * In fact, it is just absent in the real life...
  1169. */
  1170. for (i = 0; i < num_folders; i++) {
  1171. for (guint j = 0; j < folder_nstreams[i]; j++) {
  1172. guint64 tmp;
  1173. SZ_READ_VINT(tmp); /* Who cares indeed */
  1174. }
  1175. }
  1176. break;
  1177. case kEnd:
  1178. goto end;
  1179. break;
  1180. default:
  1181. p = NULL;
  1182. msg_debug_archive("bad 7zip type: %xc; %s", t, G_STRLOC);
  1183. goto end;
  1184. break;
  1185. }
  1186. }
  1187. end:
  1188. return p;
  1189. }
  1190. static const guchar *
  1191. rspamd_7zip_read_main_streams_info(struct rspamd_task *task,
  1192. const guchar *p, const guchar *end,
  1193. struct rspamd_archive *arch)
  1194. {
  1195. guchar t;
  1196. guint num_folders = 0, unknown_digests = 0;
  1197. while (p != NULL && p < end) {
  1198. t = *p;
  1199. SZ_SKIP_BYTES(1);
  1200. msg_debug_archive("7zip: read main streams info %xc", t);
  1201. /*
  1202. *
  1203. * []
  1204. * PackInfo
  1205. * []
  1206. * []
  1207. * CodersInfo
  1208. * []
  1209. *
  1210. * []
  1211. * SubStreamsInfo
  1212. * []
  1213. *
  1214. * BYTE NID::kEnd
  1215. */
  1216. switch (t) {
  1217. case kPackInfo:
  1218. p = rspamd_7zip_read_pack_info(task, p, end, arch);
  1219. break;
  1220. case kUnPackInfo:
  1221. p = rspamd_7zip_read_coders_info(task, p, end, arch, &num_folders,
  1222. &unknown_digests);
  1223. break;
  1224. case kSubStreamsInfo:
  1225. p = rspamd_7zip_read_substreams_info(task, p, end, arch, num_folders,
  1226. unknown_digests);
  1227. break;
  1228. break;
  1229. case kEnd:
  1230. goto end;
  1231. break;
  1232. default:
  1233. p = NULL;
  1234. msg_debug_archive("bad 7zip type: %xc; %s", t, G_STRLOC);
  1235. goto end;
  1236. break;
  1237. }
  1238. }
  1239. end:
  1240. return p;
  1241. }
  1242. static const guchar *
  1243. rspamd_7zip_read_archive_props(struct rspamd_task *task,
  1244. const guchar *p, const guchar *end,
  1245. struct rspamd_archive *arch)
  1246. {
  1247. guchar proptype;
  1248. guint64 proplen;
  1249. /*
  1250. * for (;;)
  1251. * {
  1252. * BYTE PropertyType;
  1253. * if (aType == 0)
  1254. * break;
  1255. * UINT64 PropertySize;
  1256. * BYTE PropertyData[PropertySize];
  1257. * }
  1258. */
  1259. if (p != NULL) {
  1260. proptype = *p;
  1261. SZ_SKIP_BYTES(1);
  1262. while (proptype != 0) {
  1263. SZ_READ_VINT(proplen);
  1264. if (p + proplen < end) {
  1265. p += proplen;
  1266. }
  1267. else {
  1268. return NULL;
  1269. }
  1270. proptype = *p;
  1271. SZ_SKIP_BYTES(1);
  1272. }
  1273. }
  1274. return p;
  1275. }
  1276. static GString *
  1277. rspamd_7zip_ucs2_to_utf8(struct rspamd_task *task, const guchar *p,
  1278. const guchar *end)
  1279. {
  1280. GString *res;
  1281. goffset dest_pos = 0, src_pos = 0;
  1282. const gsize len = (end - p) / sizeof(guint16);
  1283. guint16 *up;
  1284. UChar32 wc;
  1285. UBool is_error = 0;
  1286. res = g_string_sized_new((end - p) * 3 / 2 + sizeof(wc) + 1);
  1287. up = (guint16 *) p;
  1288. while (src_pos < len) {
  1289. U16_NEXT(up, src_pos, len, wc);
  1290. if (wc > 0) {
  1291. U8_APPEND(res->str, dest_pos,
  1292. res->allocated_len - 1,
  1293. wc, is_error);
  1294. }
  1295. if (is_error) {
  1296. g_string_free(res, TRUE);
  1297. return NULL;
  1298. }
  1299. }
  1300. g_assert(dest_pos < res->allocated_len);
  1301. res->len = dest_pos;
  1302. res->str[dest_pos] = '\0';
  1303. return res;
  1304. }
  1305. static const guchar *
  1306. rspamd_7zip_read_files_info(struct rspamd_task *task,
  1307. const guchar *p, const guchar *end,
  1308. struct rspamd_archive *arch)
  1309. {
  1310. guint64 nfiles = 0, sz, i;
  1311. guchar t, b;
  1312. struct rspamd_archive_file *fentry;
  1313. SZ_READ_VINT(nfiles);
  1314. for (; p != NULL && p < end;) {
  1315. t = *p;
  1316. SZ_SKIP_BYTES(1);
  1317. msg_debug_archive("7zip: read file data type %xc", t);
  1318. if (t == kEnd) {
  1319. goto end;
  1320. }
  1321. /* This is SO SPECIAL, gah */
  1322. SZ_READ_VINT(sz);
  1323. switch (t) {
  1324. case kEmptyStream:
  1325. case kEmptyFile:
  1326. case kAnti: /* AntiFile, OMFG */
  1327. /* We don't care about these bits */
  1328. case kCTime:
  1329. case kATime:
  1330. case kMTime:
  1331. /* We don't care of these guys, but we still have to parse them, gah */
  1332. if (sz > 0) {
  1333. SZ_SKIP_BYTES(sz);
  1334. }
  1335. break;
  1336. case kName:
  1337. /* The most useful part in this whole bloody format */
  1338. b = *p; /* External flag */
  1339. SZ_SKIP_BYTES(1);
  1340. if (b) {
  1341. /* TODO: for the god sake, do something about external
  1342. * filenames...
  1343. */
  1344. guint64 tmp;
  1345. SZ_READ_VINT(tmp);
  1346. }
  1347. else {
  1348. for (i = 0; i < nfiles; i++) {
  1349. /* Zero terminated wchar_t: happy converting... */
  1350. /* First, find terminator */
  1351. const guchar *fend = NULL, *tp = p;
  1352. GString *res;
  1353. while (tp < end - 1) {
  1354. if (*tp == 0 && *(tp + 1) == 0) {
  1355. fend = tp;
  1356. break;
  1357. }
  1358. tp += 2;
  1359. }
  1360. if (fend == NULL || fend - p == 0) {
  1361. /* Crap instead of fname */
  1362. msg_debug_archive("bad 7zip name; %s", G_STRLOC);
  1363. goto end;
  1364. }
  1365. res = rspamd_7zip_ucs2_to_utf8(task, p, fend);
  1366. if (res != NULL) {
  1367. fentry = g_malloc0(sizeof(*fentry));
  1368. fentry->fname = res;
  1369. g_ptr_array_add(arch->files, fentry);
  1370. msg_debug_archive("7zip: found file %v", res);
  1371. }
  1372. else {
  1373. msg_debug_archive("bad 7zip name; %s", G_STRLOC);
  1374. }
  1375. /* Skip zero terminating character */
  1376. p = fend + 2;
  1377. }
  1378. }
  1379. break;
  1380. case kDummy:
  1381. case kWinAttributes:
  1382. if (sz > 0) {
  1383. SZ_SKIP_BYTES(sz);
  1384. }
  1385. break;
  1386. default:
  1387. p = NULL;
  1388. msg_debug_archive("bad 7zip type: %xc; %s", t, G_STRLOC);
  1389. goto end;
  1390. break;
  1391. }
  1392. }
  1393. end:
  1394. return p;
  1395. }
  1396. static const guchar *
  1397. rspamd_7zip_read_next_section(struct rspamd_task *task,
  1398. const guchar *p, const guchar *end,
  1399. struct rspamd_archive *arch,
  1400. struct rspamd_mime_part *part)
  1401. {
  1402. guchar t = *p;
  1403. SZ_SKIP_BYTES(1);
  1404. msg_debug_archive("7zip: read section %xc", t);
  1405. switch (t) {
  1406. case kHeader:
  1407. /* We just skip byte and go further */
  1408. break;
  1409. case kEncodedHeader:
  1410. /*
  1411. * In fact, headers are just packed, but we assume it as
  1412. * encrypted to distinguish from the normal archives
  1413. */
  1414. {
  1415. msg_debug_archive("7zip: encoded header, needs to be uncompressed");
  1416. struct archive *a = archive_read_new();
  1417. archive_read_support_format_7zip(a);
  1418. int r = archive_read_open_memory(a, part->parsed_data.begin, part->parsed_data.len);
  1419. if (r != ARCHIVE_OK) {
  1420. msg_debug_archive("7zip: cannot open memory archive: %s", archive_error_string(a));
  1421. archive_read_free(a);
  1422. return NULL;
  1423. }
  1424. /* Clean the existing files if any */
  1425. rspamd_archive_dtor(arch);
  1426. arch->files = g_ptr_array_new();
  1427. struct archive_entry *ae;
  1428. while (archive_read_next_header(a, &ae) == ARCHIVE_OK) {
  1429. const char *name = archive_entry_pathname(ae);
  1430. if (name) {
  1431. msg_debug_archive("7zip: found file %s", name);
  1432. struct rspamd_archive_file *f = g_malloc0(sizeof(*f));
  1433. f->fname = g_string_new(name);
  1434. g_ptr_array_add(arch->files, f);
  1435. }
  1436. archive_read_data_skip(a);
  1437. }
  1438. archive_read_free(a);
  1439. p = NULL; /* Stop internal processor, as we rely on libarchive here */
  1440. break;
  1441. }
  1442. case kArchiveProperties:
  1443. p = rspamd_7zip_read_archive_props(task, p, end, arch);
  1444. break;
  1445. case kMainStreamsInfo:
  1446. p = rspamd_7zip_read_main_streams_info(task, p, end, arch);
  1447. break;
  1448. case kAdditionalStreamsInfo:
  1449. p = rspamd_7zip_read_main_streams_info(task, p, end, arch);
  1450. break;
  1451. case kFilesInfo:
  1452. p = rspamd_7zip_read_files_info(task, p, end, arch);
  1453. break;
  1454. case kEnd:
  1455. p = NULL;
  1456. msg_debug_archive("7zip: read final section");
  1457. break;
  1458. default:
  1459. p = NULL;
  1460. msg_debug_archive("bad 7zip type: %xc; %s", t, G_STRLOC);
  1461. break;
  1462. }
  1463. return p;
  1464. }
  1465. static void
  1466. rspamd_archive_process_7zip(struct rspamd_task *task,
  1467. struct rspamd_mime_part *part)
  1468. {
  1469. struct rspamd_archive *arch;
  1470. const guchar *start, *p, *end;
  1471. const guchar sz_magic[] = {'7', 'z', 0xBC, 0xAF, 0x27, 0x1C};
  1472. guint64 section_offset = 0, section_length = 0;
  1473. start = part->parsed_data.begin;
  1474. p = start;
  1475. end = p + part->parsed_data.len;
  1476. if (end - p <= sizeof(guint64) + sizeof(guint32) ||
  1477. memcmp(p, sz_magic, sizeof(sz_magic)) != 0) {
  1478. msg_debug_archive("7z archive is invalid (no 7z magic)");
  1479. return;
  1480. }
  1481. arch = rspamd_mempool_alloc0(task->task_pool, sizeof(*arch));
  1482. arch->files = g_ptr_array_new();
  1483. arch->type = RSPAMD_ARCHIVE_7ZIP;
  1484. rspamd_mempool_add_destructor(task->task_pool, rspamd_archive_dtor,
  1485. arch);
  1486. /* Magic (6 bytes) + version (2 bytes) + crc32 (4 bytes) */
  1487. p += sizeof(guint64) + sizeof(guint32);
  1488. SZ_READ_UINT64(section_offset);
  1489. SZ_READ_UINT64(section_length);
  1490. if (end - p > sizeof(guint32)) {
  1491. p += sizeof(guint32);
  1492. }
  1493. else {
  1494. msg_debug_archive("7z archive is invalid (truncated crc)");
  1495. return;
  1496. }
  1497. if (end - p > section_offset) {
  1498. p += section_offset;
  1499. }
  1500. else {
  1501. msg_debug_archive("7z archive is invalid (incorrect section offset)");
  1502. return;
  1503. }
  1504. while ((p = rspamd_7zip_read_next_section(task, p, end, arch, part)) != NULL)
  1505. ;
  1506. part->part_type = RSPAMD_MIME_PART_ARCHIVE;
  1507. part->specific.arch = arch;
  1508. if (part->cd != NULL) {
  1509. arch->archive_name = &part->cd->filename;
  1510. }
  1511. arch->size = part->parsed_data.len;
  1512. }
  1513. static void
  1514. rspamd_archive_process_gzip(struct rspamd_task *task,
  1515. struct rspamd_mime_part *part)
  1516. {
  1517. struct rspamd_archive *arch;
  1518. const guchar *start, *p, *end;
  1519. const guchar gz_magic[] = {0x1F, 0x8B};
  1520. guchar flags;
  1521. start = part->parsed_data.begin;
  1522. p = start;
  1523. end = p + part->parsed_data.len;
  1524. if (end - p <= 10 || memcmp(p, gz_magic, sizeof(gz_magic)) != 0) {
  1525. msg_debug_archive("gzip archive is invalid (no gzip magic)");
  1526. return;
  1527. }
  1528. arch = rspamd_mempool_alloc0(task->task_pool, sizeof(*arch));
  1529. arch->files = g_ptr_array_sized_new(1);
  1530. arch->type = RSPAMD_ARCHIVE_GZIP;
  1531. if (part->cd) {
  1532. arch->archive_name = &part->cd->filename;
  1533. }
  1534. rspamd_mempool_add_destructor(task->task_pool, rspamd_archive_dtor,
  1535. arch);
  1536. flags = p[3];
  1537. if (flags & (1u << 5)) {
  1538. arch->flags |= RSPAMD_ARCHIVE_ENCRYPTED;
  1539. }
  1540. if (flags & (1u << 3)) {
  1541. /* We have file name presented in archive, try to use it */
  1542. if (flags & (1u << 1)) {
  1543. /* Multipart */
  1544. p += 12;
  1545. }
  1546. else {
  1547. p += 10;
  1548. }
  1549. if (flags & (1u << 2)) {
  1550. /* Optional section */
  1551. guint16 optlen = 0;
  1552. RAR_READ_UINT16(optlen);
  1553. if (end <= p + optlen) {
  1554. msg_debug_archive("gzip archive is invalid, bad extra length: %d",
  1555. (int) optlen);
  1556. return;
  1557. }
  1558. p += optlen;
  1559. }
  1560. /* Read file name */
  1561. const guchar *fname_start = p;
  1562. while (p < end) {
  1563. if (*p == '\0') {
  1564. if (p > fname_start) {
  1565. struct rspamd_archive_file *f;
  1566. f = g_malloc0(sizeof(*f));
  1567. rspamd_archive_file_try_utf(task, arch, f,
  1568. fname_start, p - fname_start);
  1569. if (f->fname) {
  1570. g_ptr_array_add(arch->files, f);
  1571. if (f->flags & RSPAMD_ARCHIVE_FILE_OBFUSCATED) {
  1572. arch->flags |= RSPAMD_ARCHIVE_HAS_OBFUSCATED_FILES;
  1573. }
  1574. }
  1575. else {
  1576. /* Invalid filename, skip */
  1577. g_free(f);
  1578. }
  1579. goto set;
  1580. }
  1581. }
  1582. p++;
  1583. }
  1584. /* Wrong filename, not zero terminated */
  1585. msg_debug_archive("gzip archive is invalid, bad filename at pos %d",
  1586. (int) (p - start));
  1587. return;
  1588. }
  1589. /* Fallback, we need to extract file name from archive name if possible */
  1590. if (part->cd && part->cd->filename.len > 0) {
  1591. const gchar *dot_pos, *slash_pos;
  1592. dot_pos = rspamd_memrchr(part->cd->filename.begin, '.',
  1593. part->cd->filename.len);
  1594. if (dot_pos) {
  1595. struct rspamd_archive_file *f;
  1596. slash_pos = rspamd_memrchr(part->cd->filename.begin, '/',
  1597. part->cd->filename.len);
  1598. if (slash_pos && slash_pos < dot_pos) {
  1599. f = g_malloc0(sizeof(*f));
  1600. f->fname = g_string_sized_new(dot_pos - slash_pos);
  1601. g_string_append_len(f->fname, slash_pos + 1,
  1602. dot_pos - slash_pos - 1);
  1603. msg_debug_archive("fallback to gzip filename based on cd: %v",
  1604. f->fname);
  1605. g_ptr_array_add(arch->files, f);
  1606. goto set;
  1607. }
  1608. else {
  1609. const gchar *fname_start = part->cd->filename.begin;
  1610. f = g_malloc0(sizeof(*f));
  1611. if (memchr(fname_start, '.', part->cd->filename.len) != dot_pos) {
  1612. /* Double dots, something like foo.exe.gz */
  1613. f->fname = g_string_sized_new(dot_pos - fname_start);
  1614. g_string_append_len(f->fname, fname_start,
  1615. dot_pos - fname_start);
  1616. }
  1617. else {
  1618. /* Single dot, something like foo.gzz */
  1619. f->fname = g_string_sized_new(part->cd->filename.len);
  1620. g_string_append_len(f->fname, fname_start,
  1621. part->cd->filename.len);
  1622. }
  1623. msg_debug_archive("fallback to gzip filename based on cd: %v",
  1624. f->fname);
  1625. g_ptr_array_add(arch->files, f);
  1626. goto set;
  1627. }
  1628. }
  1629. }
  1630. return;
  1631. set:
  1632. /* Set archive data */
  1633. part->part_type = RSPAMD_MIME_PART_ARCHIVE;
  1634. part->specific.arch = arch;
  1635. arch->size = part->parsed_data.len;
  1636. }
  1637. static gboolean
  1638. rspamd_archive_cheat_detect(struct rspamd_mime_part *part, const gchar *str,
  1639. const guchar *magic_start, gsize magic_len)
  1640. {
  1641. struct rspamd_content_type *ct;
  1642. const gchar *p;
  1643. rspamd_ftok_t srch, *fname;
  1644. ct = part->ct;
  1645. RSPAMD_FTOK_ASSIGN(&srch, "application");
  1646. if (ct && ct->type.len && ct->subtype.len > 0 && rspamd_ftok_cmp(&ct->type, &srch) == 0) {
  1647. if (rspamd_substring_search_caseless(ct->subtype.begin, ct->subtype.len,
  1648. str, strlen(str)) != -1) {
  1649. /* We still need to check magic, see #1848 */
  1650. if (magic_start != NULL) {
  1651. if (part->parsed_data.len > magic_len &&
  1652. memcmp(part->parsed_data.begin,
  1653. magic_start, magic_len) == 0) {
  1654. return TRUE;
  1655. }
  1656. /* No magic, refuse this type of archive */
  1657. return FALSE;
  1658. }
  1659. else {
  1660. return TRUE;
  1661. }
  1662. }
  1663. }
  1664. if (part->cd) {
  1665. fname = &part->cd->filename;
  1666. if (fname && fname->len > strlen(str)) {
  1667. p = fname->begin + fname->len - strlen(str);
  1668. if (rspamd_lc_cmp(p, str, strlen(str)) == 0) {
  1669. if (*(p - 1) == '.') {
  1670. if (magic_start != NULL) {
  1671. if (part->parsed_data.len > magic_len &&
  1672. memcmp(part->parsed_data.begin,
  1673. magic_start, magic_len) == 0) {
  1674. return TRUE;
  1675. }
  1676. /* No magic, refuse this type of archive */
  1677. return FALSE;
  1678. }
  1679. return TRUE;
  1680. }
  1681. }
  1682. }
  1683. if (magic_start != NULL) {
  1684. if (part->parsed_data.len > magic_len &&
  1685. memcmp(part->parsed_data.begin, magic_start, magic_len) == 0) {
  1686. return TRUE;
  1687. }
  1688. }
  1689. }
  1690. else {
  1691. if (magic_start != NULL) {
  1692. if (part->parsed_data.len > magic_len &&
  1693. memcmp(part->parsed_data.begin, magic_start, magic_len) == 0) {
  1694. return TRUE;
  1695. }
  1696. }
  1697. }
  1698. return FALSE;
  1699. }
  1700. void rspamd_archives_process(struct rspamd_task *task)
  1701. {
  1702. guint i;
  1703. struct rspamd_mime_part *part;
  1704. const guchar rar_magic[] = {0x52, 0x61, 0x72, 0x21, 0x1A, 0x07};
  1705. const guchar zip_magic[] = {0x50, 0x4b, 0x03, 0x04};
  1706. const guchar sz_magic[] = {'7', 'z', 0xBC, 0xAF, 0x27, 0x1C};
  1707. const guchar gz_magic[] = {0x1F, 0x8B, 0x08};
  1708. PTR_ARRAY_FOREACH(MESSAGE_FIELD(task, parts), i, part)
  1709. {
  1710. if (part->part_type == RSPAMD_MIME_PART_UNDEFINED) {
  1711. if (part->parsed_data.len > 0) {
  1712. if (rspamd_archive_cheat_detect(part, "zip",
  1713. zip_magic, sizeof(zip_magic))) {
  1714. rspamd_archive_process_zip(task, part);
  1715. }
  1716. else if (rspamd_archive_cheat_detect(part, "rar",
  1717. rar_magic, sizeof(rar_magic))) {
  1718. rspamd_archive_process_rar(task, part);
  1719. }
  1720. else if (rspamd_archive_cheat_detect(part, "7z",
  1721. sz_magic, sizeof(sz_magic))) {
  1722. rspamd_archive_process_7zip(task, part);
  1723. }
  1724. else if (rspamd_archive_cheat_detect(part, "gz",
  1725. gz_magic, sizeof(gz_magic))) {
  1726. rspamd_archive_process_gzip(task, part);
  1727. }
  1728. if (part->ct && (part->ct->flags & RSPAMD_CONTENT_TYPE_TEXT) &&
  1729. part->part_type == RSPAMD_MIME_PART_ARCHIVE &&
  1730. part->specific.arch) {
  1731. struct rspamd_archive *arch = part->specific.arch;
  1732. msg_info_task("found %s archive with incorrect content-type: %T/%T",
  1733. rspamd_archive_type_str(arch->type),
  1734. &part->ct->type, &part->ct->subtype);
  1735. if (!(part->ct->flags & RSPAMD_CONTENT_TYPE_MISSING)) {
  1736. part->ct->flags |= RSPAMD_CONTENT_TYPE_BROKEN;
  1737. }
  1738. }
  1739. }
  1740. }
  1741. }
  1742. }
  1743. const gchar *
  1744. rspamd_archive_type_str(enum rspamd_archive_type type)
  1745. {
  1746. const gchar *ret = "unknown";
  1747. switch (type) {
  1748. case RSPAMD_ARCHIVE_ZIP:
  1749. ret = "zip";
  1750. break;
  1751. case RSPAMD_ARCHIVE_RAR:
  1752. ret = "rar";
  1753. break;
  1754. case RSPAMD_ARCHIVE_7ZIP:
  1755. ret = "7z";
  1756. break;
  1757. case RSPAMD_ARCHIVE_GZIP:
  1758. ret = "gz";
  1759. break;
  1760. }
  1761. return ret;
  1762. }