functions.quarantine.inc.php 33 KB

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