functions.quarantine.inc.php 34 KB

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