functions.quarantine.inc.php 34 KB

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