functions.quarantine.inc.php 34 KB

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