functions.quarantine.inc.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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 'delete':
  9. if (!is_array($_data['id'])) {
  10. $ids = array();
  11. $ids[] = $_data['id'];
  12. }
  13. else {
  14. $ids = $_data['id'];
  15. }
  16. if (!isset($_SESSION['acl']['quarantine']) || $_SESSION['acl']['quarantine'] != "1" ) {
  17. $_SESSION['return'][] = array(
  18. 'type' => 'danger',
  19. 'log' => array(__FUNCTION__, $_action, $_data_log),
  20. 'msg' => 'access_denied'
  21. );
  22. return false;
  23. }
  24. foreach ($ids as $id) {
  25. if (!is_numeric($id)) {
  26. $_SESSION['return'][] = array(
  27. 'type' => 'danger',
  28. 'log' => array(__FUNCTION__, $_action, $_data_log),
  29. 'msg' => 'access_denied'
  30. );
  31. continue;
  32. }
  33. $stmt = $pdo->prepare('SELECT `rcpt` FROM `quarantine` WHERE `id` = :id');
  34. $stmt->execute(array(':id' => $id));
  35. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  36. if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $row['rcpt']) && $_SESSION['mailcow_cc_role'] != 'admin') {
  37. $_SESSION['return'][] = array(
  38. 'type' => 'danger',
  39. 'log' => array(__FUNCTION__, $_action, $_data_log),
  40. 'msg' => 'access_denied'
  41. );
  42. continue;
  43. }
  44. else {
  45. $stmt = $pdo->prepare("DELETE FROM `quarantine` WHERE `id` = :id");
  46. $stmt->execute(array(
  47. ':id' => $id
  48. ));
  49. }
  50. $_SESSION['return'][] = array(
  51. 'type' => 'success',
  52. 'log' => array(__FUNCTION__, $_action, $_data_log),
  53. 'msg' => array('item_deleted', $id)
  54. );
  55. }
  56. break;
  57. case 'edit':
  58. if (!isset($_SESSION['acl']['quarantine']) || $_SESSION['acl']['quarantine'] != "1" ) {
  59. $_SESSION['return'][] = array(
  60. 'type' => 'danger',
  61. 'log' => array(__FUNCTION__, $_action, $_data_log),
  62. 'msg' => 'access_denied'
  63. );
  64. return false;
  65. }
  66. // Edit settings
  67. if ($_data['action'] == 'settings') {
  68. if ($_SESSION['mailcow_cc_role'] != "admin") {
  69. $_SESSION['return'][] = array(
  70. 'type' => 'danger',
  71. 'log' => array(__FUNCTION__, $_action, $_data_log),
  72. 'msg' => 'access_denied'
  73. );
  74. return false;
  75. }
  76. $retention_size = $_data['retention_size'];
  77. $max_size = $_data['max_size'];
  78. $exclude_domains = (array)$_data['exclude_domains'];
  79. try {
  80. $redis->Set('Q_RETENTION_SIZE', intval($retention_size));
  81. $redis->Set('Q_MAX_SIZE', intval($max_size));
  82. $redis->Set('Q_EXCLUDE_DOMAINS', json_encode($exclude_domains));
  83. }
  84. catch (RedisException $e) {
  85. $_SESSION['return'][] = array(
  86. 'type' => 'danger',
  87. 'log' => array(__FUNCTION__, $_action, $_data_log),
  88. 'msg' => array('redis_error', $e)
  89. );
  90. return false;
  91. }
  92. $_SESSION['return'][] = array(
  93. 'type' => 'success',
  94. 'log' => array(__FUNCTION__, $_action, $_data_log),
  95. 'msg' => 'saved_settings'
  96. );
  97. }
  98. // Release item
  99. elseif ($_data['action'] == 'release') {
  100. if (!is_array($_data['id'])) {
  101. $ids = array();
  102. $ids[] = $_data['id'];
  103. }
  104. else {
  105. $ids = $_data['id'];
  106. }
  107. foreach ($ids as $id) {
  108. if (!is_numeric($id)) {
  109. $_SESSION['return'][] = array(
  110. 'type' => 'danger',
  111. 'log' => array(__FUNCTION__, $_action, $_data_log),
  112. 'msg' => 'access_denied'
  113. );
  114. continue;
  115. }
  116. $stmt = $pdo->prepare('SELECT `msg`, `qid`, `sender`, `rcpt` FROM `quarantine` WHERE `id` = :id');
  117. $stmt->execute(array(':id' => $id));
  118. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  119. if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $row['rcpt'])) {
  120. $_SESSION['return'][] = array(
  121. 'type' => 'danger',
  122. 'msg' => 'access_denied'
  123. );
  124. continue;
  125. }
  126. $sender = (isset($row['sender'])) ? $row['sender'] : 'sender-unknown@rspamd';
  127. try {
  128. $mail = new PHPMailer(true);
  129. $mail->isSMTP();
  130. $mail->SMTPDebug = 0;
  131. $mail->SMTPOptions = array(
  132. 'ssl' => array(
  133. 'verify_peer' => false,
  134. 'verify_peer_name' => false,
  135. 'allow_self_signed' => true
  136. )
  137. );
  138. if (!empty(gethostbynamel('postfix-mailcow'))) {
  139. $postfix = 'postfix-mailcow';
  140. }
  141. if (!empty(gethostbynamel('postfix'))) {
  142. $postfix = 'postfix';
  143. }
  144. else {
  145. $_SESSION['return'][] = array(
  146. 'type' => 'warning',
  147. 'log' => array(__FUNCTION__, $_action, $_data_log),
  148. 'msg' => array('release_send_failed', 'Cannot determine Postfix host')
  149. );
  150. continue;
  151. }
  152. $mail->Host = $postfix;
  153. $mail->Port = 590;
  154. $mail->setFrom($sender);
  155. $mail->CharSet = 'UTF-8';
  156. $mail->Subject = sprintf($lang['quarantine']['release_subject'], $row['qid']);
  157. $mail->addAddress($row['rcpt']);
  158. $mail->IsHTML(false);
  159. $msg_tmpf = tempnam("/tmp", $row['qid']);
  160. file_put_contents($msg_tmpf, $row['msg']);
  161. $mail->addAttachment($msg_tmpf, $row['qid'] . '.eml');
  162. $mail->Body = sprintf($lang['quarantine']['release_body']);
  163. $mail->send();
  164. unlink($msg_tmpf);
  165. }
  166. catch (phpmailerException $e) {
  167. unlink($msg_tmpf);
  168. $_SESSION['return'][] = array(
  169. 'type' => 'warning',
  170. 'log' => array(__FUNCTION__, $_action, $_data_log),
  171. 'msg' => array('release_send_failed', $e->errorMessage())
  172. );
  173. continue;
  174. }
  175. try {
  176. $stmt = $pdo->prepare("DELETE FROM `quarantine` WHERE `id` = :id");
  177. $stmt->execute(array(
  178. ':id' => $id
  179. ));
  180. }
  181. catch (PDOException $e) {
  182. $_SESSION['return'][] = array(
  183. 'type' => 'danger',
  184. 'log' => array(__FUNCTION__, $_action, $_data_log),
  185. 'msg' => array('mysql_error', $e)
  186. );
  187. continue;
  188. }
  189. $_SESSION['return'][] = array(
  190. 'type' => 'success',
  191. 'log' => array(__FUNCTION__, $_action, $_data_log),
  192. 'msg' => array('item_released', $id)
  193. );
  194. }
  195. }
  196. elseif ($_data['action'] == 'learnspam') {
  197. if (!is_array($_data['id'])) {
  198. $ids = array();
  199. $ids[] = $_data['id'];
  200. }
  201. else {
  202. $ids = $_data['id'];
  203. }
  204. foreach ($ids as $id) {
  205. if (!is_numeric($id)) {
  206. $_SESSION['return'][] = array(
  207. 'type' => 'danger',
  208. 'log' => array(__FUNCTION__, $_action, $_data_log),
  209. 'msg' => 'access_denied'
  210. );
  211. continue;
  212. }
  213. $stmt = $pdo->prepare('SELECT `msg`, `rcpt` FROM `quarantine` WHERE `id` = :id');
  214. $stmt->execute(array(':id' => $id));
  215. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  216. if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $row['rcpt']) && $_SESSION['mailcow_cc_role'] != 'admin') {
  217. $_SESSION['return'][] = array(
  218. 'type' => 'danger',
  219. 'msg' => 'access_denied'
  220. );
  221. continue;
  222. }
  223. $curl = curl_init();
  224. curl_setopt($curl, CURLOPT_UNIX_SOCKET_PATH, '/rspamd-sock/rspamd.sock');
  225. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  226. curl_setopt($curl, CURLOPT_POST, 1);
  227. curl_setopt($curl, CURLOPT_TIMEOUT, 30);
  228. curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: text/plain'));
  229. curl_setopt($curl, CURLOPT_URL,"http://rspamd/learnspam");
  230. curl_setopt($curl, CURLOPT_POSTFIELDS, $row['msg']);
  231. $response = curl_exec($curl);
  232. if (!curl_errno($curl)) {
  233. $response = json_decode($response, true);
  234. if (isset($response['error'])) {
  235. if (stripos($response['error'], 'already learned') === false) {
  236. $_SESSION['return'][] = array(
  237. 'type' => 'danger',
  238. 'log' => array(__FUNCTION__),
  239. 'msg' => array('spam_learn_error', $response['error'])
  240. );
  241. continue;
  242. }
  243. }
  244. curl_close($curl);
  245. $curl = curl_init();
  246. curl_setopt($curl, CURLOPT_UNIX_SOCKET_PATH, '/rspamd-sock/rspamd.sock');
  247. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  248. curl_setopt($curl, CURLOPT_POST, 1);
  249. curl_setopt($curl, CURLOPT_TIMEOUT, 30);
  250. curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: text/plain', 'Flag: 11'));
  251. curl_setopt($curl, CURLOPT_URL,"http://rspamd/fuzzyadd");
  252. curl_setopt($curl, CURLOPT_POSTFIELDS, $row['msg']);
  253. $response = curl_exec($curl);
  254. if (!curl_errno($curl)) {
  255. $response = json_decode($response, true);
  256. if (isset($response['error'])) {
  257. $_SESSION['return'][] = array(
  258. 'type' => 'danger',
  259. 'log' => array(__FUNCTION__),
  260. 'msg' => array('fuzzy_learn_error', $response['error'])
  261. );
  262. continue;
  263. }
  264. curl_close($curl);
  265. try {
  266. $stmt = $pdo->prepare("DELETE FROM `quarantine` WHERE `id` = :id");
  267. $stmt->execute(array(
  268. ':id' => $id
  269. ));
  270. }
  271. catch (PDOException $e) {
  272. $_SESSION['return'][] = array(
  273. 'type' => 'danger',
  274. 'log' => array(__FUNCTION__, $_action, $_data_log),
  275. 'msg' => array('mysql_error', $e)
  276. );
  277. continue;
  278. }
  279. $_SESSION['return'][] = array(
  280. 'type' => 'success',
  281. 'log' => array(__FUNCTION__),
  282. 'msg' => 'qlearn_spam'
  283. );
  284. continue;
  285. }
  286. else {
  287. curl_close($curl);
  288. $_SESSION['return'][] = array(
  289. 'type' => 'danger',
  290. 'log' => array(__FUNCTION__),
  291. 'msg' => array('spam_learn_error', 'curl error ' . curl_errno($curl))
  292. );
  293. continue;
  294. }
  295. curl_close($curl);
  296. $_SESSION['return'][] = array(
  297. 'type' => 'danger',
  298. 'log' => array(__FUNCTION__),
  299. 'msg' => array('learn_spam_error', 'unknown')
  300. );
  301. continue;
  302. }
  303. else {
  304. curl_close($curl);
  305. $_SESSION['return'][] = array(
  306. 'type' => 'danger',
  307. 'log' => array(__FUNCTION__),
  308. 'msg' => array('spam_learn_error', 'curl error ' . curl_errno($curl))
  309. );
  310. continue;
  311. }
  312. curl_close($curl);
  313. $_SESSION['return'][] = array(
  314. 'type' => 'danger',
  315. 'log' => array(__FUNCTION__),
  316. 'msg' => array('learn_spam_error', 'unknown')
  317. );
  318. continue;
  319. }
  320. }
  321. return true;
  322. break;
  323. case 'get':
  324. if ($_SESSION['mailcow_cc_role'] == "user") {
  325. $stmt = $pdo->prepare('SELECT `id`, `qid`, `rcpt`, `sender`, UNIX_TIMESTAMP(`created`) AS `created` FROM `quarantine` WHERE `rcpt` = :mbox');
  326. $stmt->execute(array(':mbox' => $_SESSION['mailcow_cc_username']));
  327. $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  328. while($row = array_shift($rows)) {
  329. $q_meta[] = $row;
  330. }
  331. }
  332. elseif ($_SESSION['mailcow_cc_role'] == "admin") {
  333. $stmt = $pdo->query('SELECT `id`, `qid`, `rcpt`, `sender`, UNIX_TIMESTAMP(`created`) AS `created` FROM `quarantine`');
  334. $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  335. while($row = array_shift($rows)) {
  336. $q_meta[] = $row;
  337. }
  338. }
  339. else {
  340. $domains = array_merge(mailbox('get', 'domains'), mailbox('get', 'alias_domains'));
  341. foreach ($domains as $domain) {
  342. $stmt = $pdo->prepare('SELECT `id`, `qid`, `rcpt`, `sender`, UNIX_TIMESTAMP(`created`) AS `created` FROM `quarantine` WHERE `rcpt` REGEXP :domain');
  343. $stmt->execute(array(':domain' => '@' . $domain . '$'));
  344. $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  345. while($row = array_shift($rows)) {
  346. $q_meta[] = $row;
  347. }
  348. }
  349. }
  350. return $q_meta;
  351. break;
  352. case 'settings':
  353. if ($_SESSION['mailcow_cc_role'] != "admin") {
  354. $_SESSION['return'][] = array(
  355. 'type' => 'danger',
  356. 'log' => array(__FUNCTION__, $_action, $_data_log),
  357. 'msg' => 'access_denied'
  358. );
  359. return false;
  360. }
  361. try {
  362. $settings['exclude_domains'] = json_decode($redis->Get('Q_EXCLUDE_DOMAINS'), true);
  363. $settings['max_size'] = $redis->Get('Q_MAX_SIZE');
  364. $settings['retention_size'] = $redis->Get('Q_RETENTION_SIZE');
  365. }
  366. catch (RedisException $e) {
  367. $_SESSION['return'][] = array(
  368. 'type' => 'danger',
  369. 'log' => array(__FUNCTION__, $_action, $_data_log),
  370. 'msg' => array('redis_error', $e)
  371. );
  372. return false;
  373. }
  374. return $settings;
  375. break;
  376. case 'details':
  377. if (!is_numeric($_data) || empty($_data)) {
  378. return false;
  379. }
  380. $stmt = $pdo->prepare('SELECT `rcpt`, `symbols`, `msg`, `domain` FROM `quarantine` WHERE `id`= :id');
  381. $stmt->execute(array(':id' => $_data));
  382. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  383. if (hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $row['rcpt'])) {
  384. return $row;
  385. }
  386. return false;
  387. break;
  388. }
  389. }