functions.quarantine.inc.php 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867
  1. <?php
  2. use PHPMailer\PHPMailer\PHPMailer;
  3. use PHPMailer\PHPMailer\SMTP;
  4. use PHPMailer\PHPMailer\Exception;
  5. function quarantine($_action, $_data = null) {
  6. global $pdo;
  7. global $redis;
  8. global $lang;
  9. $_data_log = $_data;
  10. switch ($_action) {
  11. case 'quick_delete':
  12. // Dont return results, just log
  13. $hash = trim($_data);
  14. if (preg_match("/^([a-f0-9]{64})$/", $hash) === false) {
  15. logger(array('return' => array(
  16. array(
  17. 'type' => 'danger',
  18. 'log' => array(__FUNCTION__, $_action, $_data_log),
  19. 'msg' => 'access_denied'
  20. )
  21. )));
  22. return false;
  23. }
  24. $stmt = $pdo->prepare('SELECT `id` FROM `quarantine` LEFT OUTER JOIN `user_acl` ON `user_acl`.`username` = `rcpt`
  25. WHERE SHA2(CONCAT(`id`, `qid`), 256) = :hash
  26. AND user_acl.quarantine = 1
  27. AND rcpt IN (SELECT username FROM mailbox)');
  28. $stmt->execute(array(':hash' => $hash));
  29. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  30. if (empty($row['id']) || !is_numeric($row['id'])) {
  31. logger(array('return' => array(
  32. array(
  33. 'type' => 'danger',
  34. 'log' => array(__FUNCTION__, $_action, $_data_log),
  35. 'msg' => 'access_denied'
  36. )
  37. )));
  38. return false;
  39. }
  40. else {
  41. $stmt = $pdo->prepare("DELETE FROM `quarantine` WHERE id = :id");
  42. $stmt->execute(array(
  43. ':id' => $row['id']
  44. ));
  45. }
  46. logger(array('return' => array(
  47. array(
  48. 'type' => 'success',
  49. 'log' => array(__FUNCTION__, $_action, $_data_log),
  50. 'msg' => array('item_deleted', $row['id'])
  51. )
  52. )));
  53. break;
  54. case 'quick_release':
  55. // Dont return results, just log
  56. $hash = trim($_data);
  57. if (preg_match("/^([a-f0-9]{64})$/", $hash) === false) {
  58. logger(array('return' => array(
  59. array(
  60. 'type' => 'danger',
  61. 'log' => array(__FUNCTION__, $_action, $_data_log),
  62. 'msg' => 'access_denied'
  63. )
  64. )));
  65. return false;
  66. }
  67. $stmt = $pdo->prepare('SELECT `id` FROM `quarantine` LEFT OUTER JOIN `user_acl` ON `user_acl`.`username` = `rcpt`
  68. WHERE SHA2(CONCAT(`id`, `qid`), 256) = :hash
  69. AND `user_acl`.`quarantine` = 1
  70. AND `username` IN (SELECT `username` FROM `mailbox`)');
  71. $stmt->execute(array(':hash' => $hash));
  72. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  73. if (empty($row['id']) || !is_numeric($row['id'])) {
  74. logger(array('return' => array(
  75. array(
  76. 'type' => 'danger',
  77. 'log' => array(__FUNCTION__, $_action, $_data_log),
  78. 'msg' => 'access_denied'
  79. )
  80. )));
  81. return false;
  82. }
  83. else {
  84. $stmt = $pdo->prepare('SELECT `msg`, `qid`, `sender`, `rcpt` FROM `quarantine` WHERE `id` = :id');
  85. $stmt->execute(array(':id' => $row['id']));
  86. $detail_row = $stmt->fetch(PDO::FETCH_ASSOC);
  87. $sender = !empty($detail_row['sender']) ? $detail_row['sender'] : 'sender-unknown@rspamd';
  88. if (!empty(gethostbynamel('postfix-mailcow'))) {
  89. $postfix = 'postfix-mailcow';
  90. }
  91. if (!empty(gethostbynamel('postfix'))) {
  92. $postfix = 'postfix';
  93. }
  94. else {
  95. logger(array('return' => array(
  96. array(
  97. 'type' => 'warning',
  98. 'log' => array(__FUNCTION__, $_action, $_data_log),
  99. 'msg' => array('release_send_failed', 'Cannot determine Postfix host')
  100. )
  101. )));
  102. return false;
  103. }
  104. try {
  105. $release_format = $redis->Get('Q_RELEASE_FORMAT');
  106. }
  107. catch (RedisException $e) {
  108. logger(array('return' => array(
  109. array(
  110. 'type' => 'danger',
  111. 'log' => array(__FUNCTION__, $_action, $_data_log),
  112. 'msg' => array('redis_error', $e)
  113. )
  114. )));
  115. return false;
  116. }
  117. if ($release_format == 'attachment') {
  118. try {
  119. $mail = new PHPMailer(true);
  120. $mail->isSMTP();
  121. $mail->SMTPDebug = 0;
  122. $mail->SMTPOptions = array(
  123. 'ssl' => array(
  124. 'verify_peer' => false,
  125. 'verify_peer_name' => false,
  126. 'allow_self_signed' => true
  127. )
  128. );
  129. if (!empty(gethostbynamel('postfix-mailcow'))) {
  130. $postfix = 'postfix-mailcow';
  131. }
  132. if (!empty(gethostbynamel('postfix'))) {
  133. $postfix = 'postfix';
  134. }
  135. else {
  136. logger(array('return' => array(
  137. array(
  138. 'type' => 'warning',
  139. 'log' => array(__FUNCTION__, $_action, $_data_log),
  140. 'msg' => array('release_send_failed', 'Cannot determine Postfix host')
  141. )
  142. )));
  143. return false;
  144. }
  145. $mail->Host = $postfix;
  146. $mail->Port = 590;
  147. $mail->setFrom($sender);
  148. $mail->CharSet = 'UTF-8';
  149. $mail->Subject = sprintf($lang['quarantine']['release_subject'], $detail_row['qid']);
  150. $mail->addAddress($detail_row['rcpt']);
  151. $mail->IsHTML(false);
  152. $msg_tmpf = tempnam("/tmp", $detail_row['qid']);
  153. file_put_contents($msg_tmpf, $detail_row['msg']);
  154. $mail->addAttachment($msg_tmpf, $detail_row['qid'] . '.eml');
  155. $mail->Body = sprintf($lang['quarantine']['release_body']);
  156. $mail->send();
  157. unlink($msg_tmpf);
  158. }
  159. catch (phpmailerException $e) {
  160. unlink($msg_tmpf);
  161. logger(array('return' => array(
  162. array(
  163. 'type' => 'warning',
  164. 'log' => array(__FUNCTION__, $_action, $_data_log),
  165. 'msg' => array('release_send_failed', $e->errorMessage())
  166. )
  167. )));
  168. return false;
  169. }
  170. }
  171. elseif ($release_format == 'raw') {
  172. $detail_row['msg'] = preg_replace('/^X-Spam-Flag: (.*)/', 'X-Pre-Release-Spam-Flag $1', $detail_row['msg']);
  173. $postfix_talk = array(
  174. array('220', 'HELO quarantine' . chr(10)),
  175. array('250', 'MAIL FROM: ' . $sender . chr(10)),
  176. array('250', 'RCPT TO: ' . $detail_row['rcpt'] . chr(10)),
  177. array('250', 'DATA' . chr(10)),
  178. array('354', $detail_row['msg'] . chr(10) . '.' . chr(10)),
  179. array('250', 'QUIT' . chr(10)),
  180. array('221', '')
  181. );
  182. // Thanks to https://stackoverflow.com/questions/6632399/given-an-email-as-raw-text-how-can-i-send-it-using-php
  183. $smtp_connection = fsockopen($postfix, 590, $errno, $errstr, 1);
  184. if (!$smtp_connection) {
  185. logger(array('return' => array(
  186. array(
  187. 'type' => 'warning',
  188. 'log' => array(__FUNCTION__, $_action, $_data_log),
  189. 'msg' => 'Cannot connect to Postfix'
  190. )
  191. )));
  192. return false;
  193. }
  194. for ($i=0; $i < count($postfix_talk); $i++) {
  195. $smtp_resource = fgets($smtp_connection, 256);
  196. if (substr($smtp_resource, 0, 3) !== $postfix_talk[$i][0]) {
  197. $ret = substr($smtp_resource, 0, 3);
  198. $ret = (empty($ret)) ? '-' : $ret;
  199. logger(array('return' => array(
  200. array(
  201. 'type' => 'warning',
  202. 'log' => array(__FUNCTION__, $_action, $_data_log),
  203. 'msg' => 'Postfix returned SMTP code ' . $smtp_resource . ', expected ' . $postfix_talk[$i][0]
  204. )
  205. )));
  206. return false;
  207. }
  208. if ($postfix_talk[$i][1] !== '') {
  209. fputs($smtp_connection, $postfix_talk[$i][1]);
  210. }
  211. }
  212. fclose($smtp_connection);
  213. }
  214. $stmt = $pdo->prepare("DELETE FROM `quarantine` WHERE id = :id");
  215. $stmt->execute(array(
  216. ':id' => $row['id']
  217. ));
  218. }
  219. logger(array('return' => array(
  220. array(
  221. 'type' => 'success',
  222. 'log' => array(__FUNCTION__, $_action, $_data_log),
  223. 'msg' => array('item_released', $hash)
  224. )
  225. )));
  226. break;
  227. case 'delete':
  228. if (!is_array($_data['id'])) {
  229. $ids = array();
  230. $ids[] = $_data['id'];
  231. }
  232. else {
  233. $ids = $_data['id'];
  234. }
  235. if (!isset($_SESSION['acl']['quarantine']) || $_SESSION['acl']['quarantine'] != "1" ) {
  236. $_SESSION['return'][] = array(
  237. 'type' => 'danger',
  238. 'log' => array(__FUNCTION__, $_action, $_data_log),
  239. 'msg' => 'access_denied'
  240. );
  241. return false;
  242. }
  243. foreach ($ids as $id) {
  244. if (!is_numeric($id)) {
  245. $_SESSION['return'][] = array(
  246. 'type' => 'danger',
  247. 'log' => array(__FUNCTION__, $_action, $_data_log),
  248. 'msg' => 'access_denied'
  249. );
  250. continue;
  251. }
  252. $stmt = $pdo->prepare('SELECT `rcpt` FROM `quarantine` WHERE `id` = :id');
  253. $stmt->execute(array(':id' => $id));
  254. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  255. if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $row['rcpt']) && $_SESSION['mailcow_cc_role'] != 'admin') {
  256. $_SESSION['return'][] = array(
  257. 'type' => 'danger',
  258. 'log' => array(__FUNCTION__, $_action, $_data_log),
  259. 'msg' => 'access_denied'
  260. );
  261. continue;
  262. }
  263. else {
  264. $stmt = $pdo->prepare("DELETE FROM `quarantine` WHERE `id` = :id");
  265. $stmt->execute(array(
  266. ':id' => $id
  267. ));
  268. }
  269. $_SESSION['return'][] = array(
  270. 'type' => 'success',
  271. 'log' => array(__FUNCTION__, $_action, $_data_log),
  272. 'msg' => array('item_deleted', $id)
  273. );
  274. }
  275. break;
  276. case 'edit':
  277. if (!isset($_SESSION['acl']['quarantine']) || $_SESSION['acl']['quarantine'] != "1" ) {
  278. $_SESSION['return'][] = array(
  279. 'type' => 'danger',
  280. 'log' => array(__FUNCTION__, $_action, $_data_log),
  281. 'msg' => 'access_denied'
  282. );
  283. return false;
  284. }
  285. // Edit settings
  286. if ($_data['action'] == 'settings') {
  287. if ($_SESSION['mailcow_cc_role'] != "admin") {
  288. $_SESSION['return'][] = array(
  289. 'type' => 'danger',
  290. 'log' => array(__FUNCTION__, $_action, $_data_log),
  291. 'msg' => 'access_denied'
  292. );
  293. return false;
  294. }
  295. $retention_size = $_data['retention_size'];
  296. if ($_data['release_format'] == 'attachment' || $_data['release_format'] == 'raw') {
  297. $release_format = $_data['release_format'];
  298. }
  299. else {
  300. $release_format = 'raw';
  301. }
  302. $max_size = $_data['max_size'];
  303. if ($_data['max_score'] == "") {
  304. $max_score = '';
  305. }
  306. else {
  307. $max_score = floatval($_data['max_score']);
  308. }
  309. $max_age = intval($_data['max_age']);
  310. $subject = $_data['subject'];
  311. if (!filter_var($_data['bcc'], FILTER_VALIDATE_EMAIL)) {
  312. $bcc = '';
  313. }
  314. else {
  315. $bcc = $_data['bcc'];
  316. }
  317. if (!filter_var($_data['redirect'], FILTER_VALIDATE_EMAIL)) {
  318. $redirect = '';
  319. }
  320. else {
  321. $redirect = $_data['redirect'];
  322. }
  323. if (!filter_var($_data['sender'], FILTER_VALIDATE_EMAIL)) {
  324. $sender = '';
  325. }
  326. else {
  327. $sender = $_data['sender'];
  328. }
  329. $html = $_data['html_tmpl'];
  330. if ($max_age <= 0) {
  331. $max_age = 365;
  332. }
  333. $exclude_domains = (array)$_data['exclude_domains'];
  334. try {
  335. $redis->Set('Q_RETENTION_SIZE', intval($retention_size));
  336. $redis->Set('Q_MAX_SIZE', intval($max_size));
  337. $redis->Set('Q_MAX_SCORE', $max_score);
  338. $redis->Set('Q_MAX_AGE', $max_age);
  339. $redis->Set('Q_EXCLUDE_DOMAINS', json_encode($exclude_domains));
  340. $redis->Set('Q_RELEASE_FORMAT', $release_format);
  341. $redis->Set('Q_SENDER', $sender);
  342. $redis->Set('Q_BCC', $bcc);
  343. $redis->Set('Q_REDIRECT', $redirect);
  344. $redis->Set('Q_SUBJ', $subject);
  345. $redis->Set('Q_HTML', $html);
  346. }
  347. catch (RedisException $e) {
  348. $_SESSION['return'][] = array(
  349. 'type' => 'danger',
  350. 'log' => array(__FUNCTION__, $_action, $_data_log),
  351. 'msg' => array('redis_error', $e)
  352. );
  353. return false;
  354. }
  355. $_SESSION['return'][] = array(
  356. 'type' => 'success',
  357. 'log' => array(__FUNCTION__, $_action, $_data_log),
  358. 'msg' => 'saved_settings'
  359. );
  360. }
  361. // Release item
  362. elseif ($_data['action'] == 'release' || $_data['action'] == 'learnham') {
  363. if (!is_array($_data['id'])) {
  364. $ids = array();
  365. $ids[] = $_data['id'];
  366. }
  367. else {
  368. $ids = $_data['id'];
  369. }
  370. foreach ($ids as $id) {
  371. if (!is_numeric($id)) {
  372. $_SESSION['return'][] = array(
  373. 'type' => 'danger',
  374. 'log' => array(__FUNCTION__, $_action, $_data_log),
  375. 'msg' => 'access_denied'
  376. );
  377. continue;
  378. }
  379. $stmt = $pdo->prepare('SELECT `msg`, `action`, `qid`, `sender`, `rcpt` FROM `quarantine` WHERE `id` = :id');
  380. $stmt->execute(array(':id' => $id));
  381. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  382. if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $row['rcpt']) && $_SESSION['mailcow_cc_role'] != 'admin' || empty($row['rcpt'])) {
  383. $_SESSION['return'][] = array(
  384. 'type' => 'danger',
  385. 'log' => array(__FUNCTION__, $_action, $_data_log),
  386. 'msg' => 'access_denied'
  387. );
  388. continue;
  389. }
  390. $sender = !empty($row['sender']) ? $row['sender'] : 'sender-unknown@rspamd';
  391. if (!empty(gethostbynamel('postfix-mailcow'))) {
  392. $postfix = 'postfix-mailcow';
  393. }
  394. if (!empty(gethostbynamel('postfix'))) {
  395. $postfix = 'postfix';
  396. }
  397. else {
  398. $_SESSION['return'][] = array(
  399. 'type' => 'warning',
  400. 'log' => array(__FUNCTION__, $_action, $_data_log),
  401. 'msg' => array('release_send_failed', 'Cannot determine Postfix host')
  402. );
  403. continue;
  404. }
  405. try {
  406. $release_format = $redis->Get('Q_RELEASE_FORMAT');
  407. }
  408. catch (RedisException $e) {
  409. $_SESSION['return'][] = array(
  410. 'type' => 'danger',
  411. 'log' => array(__FUNCTION__, $_action, $_data_log),
  412. 'msg' => array('redis_error', $e)
  413. );
  414. return false;
  415. }
  416. if ($release_format == 'attachment') {
  417. try {
  418. $mail = new PHPMailer(true);
  419. $mail->isSMTP();
  420. $mail->SMTPDebug = 0;
  421. $mail->SMTPOptions = array(
  422. 'ssl' => array(
  423. 'verify_peer' => false,
  424. 'verify_peer_name' => false,
  425. 'allow_self_signed' => true
  426. )
  427. );
  428. if (!empty(gethostbynamel('postfix-mailcow'))) {
  429. $postfix = 'postfix-mailcow';
  430. }
  431. if (!empty(gethostbynamel('postfix'))) {
  432. $postfix = 'postfix';
  433. }
  434. else {
  435. $_SESSION['return'][] = array(
  436. 'type' => 'warning',
  437. 'log' => array(__FUNCTION__, $_action, $_data_log),
  438. 'msg' => array('release_send_failed', 'Cannot determine Postfix host')
  439. );
  440. continue;
  441. }
  442. $mail->Host = $postfix;
  443. $mail->Port = 590;
  444. $mail->setFrom($sender);
  445. $mail->CharSet = 'UTF-8';
  446. $mail->Subject = sprintf($lang['quarantine']['release_subject'], $row['qid']);
  447. $mail->addAddress($row['rcpt']);
  448. $mail->IsHTML(false);
  449. $msg_tmpf = tempnam("/tmp", $row['qid']);
  450. file_put_contents($msg_tmpf, $row['msg']);
  451. $mail->addAttachment($msg_tmpf, $row['qid'] . '.eml');
  452. $mail->Body = sprintf($lang['quarantine']['release_body']);
  453. $mail->send();
  454. unlink($msg_tmpf);
  455. }
  456. catch (phpmailerException $e) {
  457. unlink($msg_tmpf);
  458. $_SESSION['return'][] = array(
  459. 'type' => 'warning',
  460. 'log' => array(__FUNCTION__, $_action, $_data_log),
  461. 'msg' => array('release_send_failed', $e->errorMessage())
  462. );
  463. continue;
  464. }
  465. }
  466. elseif ($release_format == 'raw') {
  467. $row['msg'] = preg_replace('/^X-Spam-Flag: (.*)/', 'X-Pre-Release-Spam-Flag $1', $row['msg']);
  468. $postfix_talk = array(
  469. array('220', 'HELO quarantine' . chr(10)),
  470. array('250', 'MAIL FROM: ' . $sender . chr(10)),
  471. array('250', 'RCPT TO: ' . $row['rcpt'] . chr(10)),
  472. array('250', 'DATA' . chr(10)),
  473. array('354', str_replace("\n.", '', $row['msg']) . chr(10) . '.' . chr(10)),
  474. array('250', 'QUIT' . chr(10)),
  475. array('221', '')
  476. );
  477. // Thanks to https://stackoverflow.com/questions/6632399/given-an-email-as-raw-text-how-can-i-send-it-using-php
  478. $smtp_connection = fsockopen($postfix, 590, $errno, $errstr, 1);
  479. if (!$smtp_connection) {
  480. $_SESSION['return'][] = array(
  481. 'type' => 'warning',
  482. 'log' => array(__FUNCTION__, $_action, $_data_log),
  483. 'msg' => 'Cannot connect to Postfix'
  484. );
  485. return false;
  486. }
  487. for ($i=0; $i < count($postfix_talk); $i++) {
  488. $smtp_resource = fgets($smtp_connection, 256);
  489. if (substr($smtp_resource, 0, 3) !== $postfix_talk[$i][0]) {
  490. $ret = substr($smtp_resource, 0, 3);
  491. $ret = (empty($ret)) ? '-' : $ret;
  492. $_SESSION['return'][] = array(
  493. 'type' => 'warning',
  494. 'log' => array(__FUNCTION__, $_action, $_data_log),
  495. 'msg' => 'Postfix returned SMTP code ' . $smtp_resource . ', expected ' . $postfix_talk[$i][0]
  496. );
  497. return false;
  498. }
  499. if ($postfix_talk[$i][1] !== '') {
  500. fputs($smtp_connection, $postfix_talk[$i][1]);
  501. }
  502. }
  503. fclose($smtp_connection);
  504. }
  505. $stmt = $pdo->prepare("DELETE FROM `quarantine` WHERE `id` = :id");
  506. $stmt->execute(array(
  507. ':id' => $id
  508. ));
  509. $_SESSION['return'][] = array(
  510. 'type' => 'success',
  511. 'log' => array(__FUNCTION__, $_action, $_data_log),
  512. 'msg' => array('item_released', $id)
  513. );
  514. // Item was released and deleted from quarantine, now learning ham
  515. $curl = curl_init();
  516. curl_setopt($curl, CURLOPT_UNIX_SOCKET_PATH, '/var/lib/rspamd/rspamd.sock');
  517. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  518. curl_setopt($curl, CURLOPT_POST, 1);
  519. curl_setopt($curl, CURLOPT_TIMEOUT, 30);
  520. curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: text/plain'));
  521. curl_setopt($curl, CURLOPT_URL,"http://rspamd/learnham");
  522. curl_setopt($curl, CURLOPT_POSTFIELDS, $row['msg']);
  523. $response = curl_exec($curl);
  524. if (!curl_errno($curl)) {
  525. $response = json_decode($response, true);
  526. if (isset($response['error'])) {
  527. if (stripos($response['error'], 'already learned') === false) {
  528. $_SESSION['return'][] = array(
  529. 'type' => 'danger',
  530. 'log' => array(__FUNCTION__),
  531. 'msg' => array('ham_learn_error', $response['error'])
  532. );
  533. continue;
  534. }
  535. }
  536. curl_close($curl);
  537. $curl = curl_init();
  538. curl_setopt($curl, CURLOPT_UNIX_SOCKET_PATH, '/var/lib/rspamd/rspamd.sock');
  539. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  540. curl_setopt($curl, CURLOPT_POST, 1);
  541. curl_setopt($curl, CURLOPT_TIMEOUT, 30);
  542. curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: text/plain', 'Flag: 13'));
  543. curl_setopt($curl, CURLOPT_URL,"http://rspamd/fuzzyadd");
  544. curl_setopt($curl, CURLOPT_POSTFIELDS, $row['msg']);
  545. $response = curl_exec($curl);
  546. if (!curl_errno($curl)) {
  547. $response = json_decode($response, true);
  548. if (isset($response['error'])) {
  549. if (stripos($response['error'], 'No content to generate fuzzy') === false) {
  550. $_SESSION['return'][] = array(
  551. 'type' => 'warning',
  552. 'log' => array(__FUNCTION__),
  553. 'msg' => array('fuzzy_learn_error', $response['error'])
  554. );
  555. }
  556. }
  557. curl_close($curl);
  558. $curl = curl_init();
  559. curl_setopt($curl, CURLOPT_UNIX_SOCKET_PATH, '/var/lib/rspamd/rspamd.sock');
  560. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  561. curl_setopt($curl, CURLOPT_POST, 1);
  562. curl_setopt($curl, CURLOPT_TIMEOUT, 30);
  563. curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: text/plain', 'Flag: 11'));
  564. curl_setopt($curl, CURLOPT_URL,"http://rspamd/fuzzydel");
  565. curl_setopt($curl, CURLOPT_POSTFIELDS, $row['msg']);
  566. // It is most likely not a spam hash, so we ignore any error/warning response
  567. // $response = curl_exec($curl);
  568. curl_exec($curl);
  569. // if (!curl_errno($curl)) {
  570. // $response = json_decode($response, true);
  571. // if (isset($response['error'])) {
  572. // $_SESSION['return'][] = array(
  573. // 'type' => 'warning',
  574. // 'log' => array(__FUNCTION__),
  575. // 'msg' => array('fuzzy_learn_error', $response['error'])
  576. // );
  577. // }
  578. // }
  579. curl_close($curl);
  580. $_SESSION['return'][] = array(
  581. 'type' => 'success',
  582. 'log' => array(__FUNCTION__),
  583. 'msg' => array('learned_ham', $id)
  584. );
  585. continue;
  586. }
  587. else {
  588. curl_close($curl);
  589. $_SESSION['return'][] = array(
  590. 'type' => 'danger',
  591. 'log' => array(__FUNCTION__),
  592. 'msg' => array('ham_learn_error', 'Curl: ' . curl_strerror(curl_errno($curl)))
  593. );
  594. continue;
  595. }
  596. curl_close($curl);
  597. $_SESSION['return'][] = array(
  598. 'type' => 'danger',
  599. 'log' => array(__FUNCTION__),
  600. 'msg' => array('ham_learn_error', 'unknown')
  601. );
  602. continue;
  603. }
  604. else {
  605. $_SESSION['return'][] = array(
  606. 'type' => 'danger',
  607. 'log' => array(__FUNCTION__),
  608. 'msg' => array('ham_learn_error', 'Curl: ' . curl_strerror(curl_errno($curl)))
  609. );
  610. curl_close($curl);
  611. continue;
  612. }
  613. curl_close($curl);
  614. $_SESSION['return'][] = array(
  615. 'type' => 'danger',
  616. 'log' => array(__FUNCTION__),
  617. 'msg' => array('ham_learn_error', 'unknown')
  618. );
  619. continue;
  620. }
  621. }
  622. elseif ($_data['action'] == 'learnspam') {
  623. if (!is_array($_data['id'])) {
  624. $ids = array();
  625. $ids[] = $_data['id'];
  626. }
  627. else {
  628. $ids = $_data['id'];
  629. }
  630. foreach ($ids as $id) {
  631. if (!is_numeric($id)) {
  632. $_SESSION['return'][] = array(
  633. 'type' => 'danger',
  634. 'log' => array(__FUNCTION__, $_action, $_data_log),
  635. 'msg' => 'access_denied'
  636. );
  637. continue;
  638. }
  639. $stmt = $pdo->prepare('SELECT `msg`, `rcpt`, `action` FROM `quarantine` WHERE `id` = :id');
  640. $stmt->execute(array(':id' => $id));
  641. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  642. if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $row['rcpt']) && $_SESSION['mailcow_cc_role'] != 'admin' || empty($row['rcpt'])) {
  643. $_SESSION['return'][] = array(
  644. 'type' => 'danger',
  645. 'log' => array(__FUNCTION__, $_action, $_data_log),
  646. 'msg' => 'access_denied'
  647. );
  648. continue;
  649. }
  650. $curl = curl_init();
  651. curl_setopt($curl, CURLOPT_UNIX_SOCKET_PATH, '/var/lib/rspamd/rspamd.sock');
  652. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  653. curl_setopt($curl, CURLOPT_POST, 1);
  654. curl_setopt($curl, CURLOPT_TIMEOUT, 30);
  655. curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: text/plain'));
  656. curl_setopt($curl, CURLOPT_URL,"http://rspamd/learnspam");
  657. curl_setopt($curl, CURLOPT_POSTFIELDS, $row['msg']);
  658. $response = curl_exec($curl);
  659. if (!curl_errno($curl)) {
  660. $response = json_decode($response, true);
  661. if (isset($response['error'])) {
  662. if (stripos($response['error'], 'already learned') === false) {
  663. $_SESSION['return'][] = array(
  664. 'type' => 'danger',
  665. 'log' => array(__FUNCTION__, $_action, $_data_log),
  666. 'msg' => array('spam_learn_error', $response['error'])
  667. );
  668. continue;
  669. }
  670. }
  671. curl_close($curl);
  672. $curl = curl_init();
  673. curl_setopt($curl, CURLOPT_UNIX_SOCKET_PATH, '/var/lib/rspamd/rspamd.sock');
  674. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  675. curl_setopt($curl, CURLOPT_POST, 1);
  676. curl_setopt($curl, CURLOPT_TIMEOUT, 30);
  677. curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: text/plain', 'Flag: 11'));
  678. curl_setopt($curl, CURLOPT_URL,"http://rspamd/fuzzyadd");
  679. curl_setopt($curl, CURLOPT_POSTFIELDS, $row['msg']);
  680. $response = curl_exec($curl);
  681. if (!curl_errno($curl)) {
  682. $response = json_decode($response, true);
  683. if (isset($response['error'])) {
  684. if (stripos($response['error'], 'No content to generate fuzzy') === false) {
  685. $_SESSION['return'][] = array(
  686. 'type' => 'warning',
  687. 'log' => array(__FUNCTION__),
  688. 'msg' => array('fuzzy_learn_error', $response['error'])
  689. );
  690. }
  691. }
  692. curl_close($curl);
  693. $curl = curl_init();
  694. curl_setopt($curl, CURLOPT_UNIX_SOCKET_PATH, '/var/lib/rspamd/rspamd.sock');
  695. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  696. curl_setopt($curl, CURLOPT_POST, 1);
  697. curl_setopt($curl, CURLOPT_TIMEOUT, 30);
  698. curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: text/plain', 'Flag: 13'));
  699. curl_setopt($curl, CURLOPT_URL,"http://rspamd/fuzzydel");
  700. curl_setopt($curl, CURLOPT_POSTFIELDS, $row['msg']);
  701. // It is most likely not a ham hash, so we ignore any error/warning response
  702. // $response = curl_exec($curl);
  703. curl_exec($curl);
  704. // if (!curl_errno($curl)) {
  705. // $response = json_decode($response, true);
  706. // if (isset($response['error'])) {
  707. // $_SESSION['return'][] = array(
  708. // 'type' => 'warning',
  709. // 'log' => array(__FUNCTION__),
  710. // 'msg' => array('fuzzy_learn_error', $response['error'])
  711. // );
  712. // }
  713. // }
  714. curl_close($curl);
  715. try {
  716. $stmt = $pdo->prepare("DELETE FROM `quarantine` WHERE `id` = :id");
  717. $stmt->execute(array(
  718. ':id' => $id
  719. ));
  720. }
  721. catch (PDOException $e) {
  722. $_SESSION['return'][] = array(
  723. 'type' => 'danger',
  724. 'log' => array(__FUNCTION__, $_action, $_data_log),
  725. 'msg' => array('mysql_error', $e)
  726. );
  727. continue;
  728. }
  729. $_SESSION['return'][] = array(
  730. 'type' => 'success',
  731. 'log' => array(__FUNCTION__),
  732. 'msg' => array('qlearn_spam', $id)
  733. );
  734. continue;
  735. }
  736. else {
  737. curl_close($curl);
  738. $_SESSION['return'][] = array(
  739. 'type' => 'danger',
  740. 'log' => array(__FUNCTION__),
  741. 'msg' => array('spam_learn_error', 'Curl: ' . curl_strerror(curl_errno($curl)))
  742. );
  743. continue;
  744. }
  745. curl_close($curl);
  746. $_SESSION['return'][] = array(
  747. 'type' => 'danger',
  748. 'log' => array(__FUNCTION__),
  749. 'msg' => array('learn_spam_error', 'unknown')
  750. );
  751. continue;
  752. }
  753. else {
  754. $_SESSION['return'][] = array(
  755. 'type' => 'danger',
  756. 'log' => array(__FUNCTION__),
  757. 'msg' => array('spam_learn_error', 'Curl: ' . curl_strerror(curl_errno($curl)))
  758. );
  759. curl_close($curl);
  760. continue;
  761. }
  762. curl_close($curl);
  763. $_SESSION['return'][] = array(
  764. 'type' => 'danger',
  765. 'log' => array(__FUNCTION__),
  766. 'msg' => array('learn_spam_error', 'unknown')
  767. );
  768. continue;
  769. }
  770. }
  771. return true;
  772. break;
  773. case 'get':
  774. if ($_SESSION['mailcow_cc_role'] == "user") {
  775. $stmt = $pdo->prepare('SELECT `id`, `qid`, `subject`, LOCATE("VIRUS_FOUND", `symbols`) AS `virus_flag`, `score`, `rcpt`, `sender`, `action`, UNIX_TIMESTAMP(`created`) AS `created`, `notified` FROM `quarantine` WHERE `rcpt` = :mbox');
  776. $stmt->execute(array(':mbox' => $_SESSION['mailcow_cc_username']));
  777. $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  778. while($row = array_shift($rows)) {
  779. $q_meta[] = $row;
  780. }
  781. }
  782. elseif ($_SESSION['mailcow_cc_role'] == "admin") {
  783. $stmt = $pdo->query('SELECT `id`, `qid`, `subject`, LOCATE("VIRUS_FOUND", `symbols`) AS `virus_flag`, `score`, `rcpt`, `sender`, `action`, UNIX_TIMESTAMP(`created`) AS `created`, `notified` FROM `quarantine`');
  784. $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  785. while($row = array_shift($rows)) {
  786. $q_meta[] = $row;
  787. }
  788. }
  789. else {
  790. $domains = array_merge(mailbox('get', 'domains'), mailbox('get', 'alias_domains'));
  791. foreach ($domains as $domain) {
  792. $stmt = $pdo->prepare('SELECT `id`, `qid`, `subject`, LOCATE("VIRUS_FOUND", `symbols`) AS `virus_flag`, `score`, `rcpt`, `sender`, `action`, UNIX_TIMESTAMP(`created`) AS `created`, `notified` FROM `quarantine` WHERE `rcpt` REGEXP :domain');
  793. $stmt->execute(array(':domain' => '@' . $domain . '$'));
  794. $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  795. while($row = array_shift($rows)) {
  796. $q_meta[] = $row;
  797. }
  798. }
  799. }
  800. return $q_meta;
  801. break;
  802. case 'settings':
  803. try {
  804. if ($_SESSION['mailcow_cc_role'] == "admin") {
  805. $settings['exclude_domains'] = json_decode($redis->Get('Q_EXCLUDE_DOMAINS'), true);
  806. }
  807. $settings['max_size'] = $redis->Get('Q_MAX_SIZE');
  808. $settings['max_score'] = $redis->Get('Q_MAX_SCORE');
  809. $settings['max_age'] = $redis->Get('Q_MAX_AGE');
  810. $settings['retention_size'] = $redis->Get('Q_RETENTION_SIZE');
  811. $settings['release_format'] = $redis->Get('Q_RELEASE_FORMAT');
  812. $settings['subject'] = $redis->Get('Q_SUBJ');
  813. $settings['sender'] = $redis->Get('Q_SENDER');
  814. $settings['bcc'] = $redis->Get('Q_BCC');
  815. $settings['redirect'] = $redis->Get('Q_REDIRECT');
  816. $settings['html_tmpl'] = htmlspecialchars($redis->Get('Q_HTML'));
  817. if (empty($settings['html_tmpl'])) {
  818. $settings['html_tmpl'] = htmlspecialchars(file_get_contents("/tpls/quarantine.tpl"));
  819. }
  820. }
  821. catch (RedisException $e) {
  822. $_SESSION['return'][] = array(
  823. 'type' => 'danger',
  824. 'log' => array(__FUNCTION__, $_action, $_data_log),
  825. 'msg' => array('redis_error', $e)
  826. );
  827. return false;
  828. }
  829. return $settings;
  830. break;
  831. case 'details':
  832. if (!is_numeric($_data) || empty($_data)) {
  833. return false;
  834. }
  835. $stmt = $pdo->prepare('SELECT * FROM `quarantine` WHERE `id`= :id');
  836. $stmt->execute(array(':id' => $_data));
  837. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  838. if (hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $row['rcpt']) || $_SESSION['mailcow_cc_role'] == 'admin') {
  839. return $row;
  840. }
  841. logger(array('return' => array(
  842. array(
  843. 'type' => 'danger',
  844. 'log' => array(__FUNCTION__, $_action, $_data_log),
  845. 'msg' => 'access_denied'
  846. )
  847. )));
  848. return false;
  849. break;
  850. case 'hash_details':
  851. $hash = trim($_data);
  852. if (preg_match("/^([a-f0-9]{64})$/", $hash) === false) {
  853. logger(array('return' => array(
  854. array(
  855. 'type' => 'danger',
  856. 'log' => array(__FUNCTION__, $_action, $_data_log),
  857. 'msg' => 'access_denied'
  858. )
  859. )));
  860. return false;
  861. }
  862. $stmt = $pdo->prepare('SELECT * FROM `quarantine` WHERE SHA2(CONCAT(`id`, `qid`), 256) = :hash');
  863. $stmt->execute(array(':hash' => $hash));
  864. return $stmt->fetch(PDO::FETCH_ASSOC);
  865. break;
  866. }
  867. }