blob: 52c751591de6dc0047ba20d68bdacf0268ba975f [file] [log] [blame]
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01001<?php
2function fwdhost($_action, $_data = null) {
3 require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/spf.inc.php';
Matthias Andreas Benkard8f915942021-08-16 10:57:25 +02004 global $redis;
5 global $lang;
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01006 $_data_log = $_data;
7 switch ($_action) {
8 case 'add':
9 global $lang;
10 if ($_SESSION['mailcow_cc_role'] != "admin") {
11 $_SESSION['return'][] = array(
12 'type' => 'danger',
13 'log' => array(__FUNCTION__, $_action, $_data_log),
14 'msg' => 'access_denied'
15 );
16 return false;
17 }
18 $source = $_data['hostname'];
19 $host = trim($_data['hostname']);
20 $filter_spam = (isset($_data['filter_spam']) && $_data['filter_spam'] == 1) ? 1 : 0;
21 if (preg_match('/^[0-9a-fA-F:\/]+$/', $host)) { // IPv6 address
22 $hosts = array($host);
23 }
24 elseif (preg_match('/^[0-9\.\/]+$/', $host)) { // IPv4 address
25 $hosts = array($host);
26 }
27 else {
28 $hosts = get_outgoing_hosts_best_guess($host);
29 }
30 if (empty($hosts)) {
31 $_SESSION['return'][] = array(
32 'type' => 'danger',
33 'log' => array(__FUNCTION__, $_action, $_data_log),
34 'msg' => array('invalid_host', htmlspecialchars($host))
35 );
36 return false;
37 }
38 foreach ($hosts as $host) {
39 try {
40 $redis->hSet('WHITELISTED_FWD_HOST', $host, $source);
41 if ($filter_spam == 0) {
42 $redis->hSet('KEEP_SPAM', $host, 1);
43 }
44 elseif ($redis->hGet('KEEP_SPAM', $host)) {
45 $redis->hDel('KEEP_SPAM', $host);
46 }
47 }
48 catch (RedisException $e) {
49 $_SESSION['return'][] = array(
50 'type' => 'danger',
51 'log' => array(__FUNCTION__, $_action, $_data_log),
52 'msg' => array('redis_error', $e)
53 );
54 return false;
55 }
56 }
57 $_SESSION['return'][] = array(
58 'type' => 'success',
59 'log' => array(__FUNCTION__, $_action, $_data_log),
Matthias Andreas Benkard8f915942021-08-16 10:57:25 +020060 'msg' => array('forwarding_host_added', htmlspecialchars(implode(', ', (array)$hosts)))
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +010061 );
62 break;
63 case 'edit':
64 global $lang;
65 if ($_SESSION['mailcow_cc_role'] != "admin") {
66 $_SESSION['return'][] = array(
67 'type' => 'danger',
68 'log' => array(__FUNCTION__, $_action, $_data_log),
69 'msg' => 'access_denied'
70 );
71 return false;
72 }
73 $fwdhosts = (array)$_data['fwdhost'];
74 foreach ($fwdhosts as $fwdhost) {
75 $is_now = fwdhost('details', $fwdhost);
76 if (!empty($is_now)) {
77 $keep_spam = (isset($_data['keep_spam'])) ? $_data['keep_spam'] : $is_now['keep_spam'];
78 }
79 else {
80 $_SESSION['return'][] = array(
81 'type' => 'danger',
82 'log' => array(__FUNCTION__, $_action, $_data_log),
83 'msg' => 'access_denied'
84 );
85 continue;
86 }
87 try {
88 if ($keep_spam == 1) {
89 $redis->hSet('KEEP_SPAM', $fwdhost, 1);
90 }
91 else {
92 $redis->hDel('KEEP_SPAM', $fwdhost);
93 }
94 }
95 catch (RedisException $e) {
96 $_SESSION['return'][] = array(
97 'type' => 'danger',
98 'log' => array(__FUNCTION__, $_action, $_data_log),
99 'msg' => array('redis_error', $e)
100 );
101 continue;
102 }
103 $_SESSION['return'][] = array(
104 'type' => 'success',
105 'log' => array(__FUNCTION__, $_action, $_data_log),
106 'msg' => array('object_modified', htmlspecialchars($fwdhost))
107 );
108 }
109 break;
110 case 'delete':
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +0200111 if ($_SESSION['mailcow_cc_role'] != "admin") {
112 $_SESSION['return'][] = array(
113 'type' => 'danger',
114 'log' => array(__FUNCTION__, $_action, $_data_log),
115 'msg' => 'access_denied'
116 );
117 return false;
118 }
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +0100119 $hosts = (array)$_data['forwardinghost'];
120 foreach ($hosts as $host) {
121 try {
122 $redis->hDel('WHITELISTED_FWD_HOST', $host);
123 $redis->hDel('KEEP_SPAM', $host);
124 }
125 catch (RedisException $e) {
126 $_SESSION['return'][] = array(
127 'type' => 'danger',
128 'log' => array(__FUNCTION__, $_action, $_data_log),
129 'msg' => array('redis_error', $e)
130 );
131 continue;
132 }
133 $_SESSION['return'][] = array(
134 'type' => 'success',
135 'log' => array(__FUNCTION__, $_action, $_data_log),
136 'msg' => array('forwarding_host_removed', htmlspecialchars($host))
137 );
138 }
139 break;
140 case 'get':
141 if ($_SESSION['mailcow_cc_role'] != "admin") {
142 return false;
143 }
144 $fwdhostsdata = array();
145 try {
146 $fwd_hosts = $redis->hGetAll('WHITELISTED_FWD_HOST');
147 if (!empty($fwd_hosts)) {
148 foreach ($fwd_hosts as $fwd_host => $source) {
149 $keep_spam = ($redis->hGet('KEEP_SPAM', $fwd_host)) ? "yes" : "no";
150 $fwdhostsdata[] = array(
151 'host' => $fwd_host,
152 'source' => $source,
153 'keep_spam' => $keep_spam
154 );
155 }
156 }
157 }
158 catch (RedisException $e) {
159 $_SESSION['return'][] = array(
160 'type' => 'danger',
161 'log' => array(__FUNCTION__, $_action, $_data_log),
162 'msg' => array('redis_error', $e)
163 );
164 return false;
165 }
166 return $fwdhostsdata;
167 break;
168 case 'details':
169 $fwdhostdetails = array();
170 if (!isset($_data) || empty($_data)) {
171 return false;
172 }
173 try {
174 if ($source = $redis->hGet('WHITELISTED_FWD_HOST', $_data)) {
175 $fwdhostdetails['host'] = $_data;
176 $fwdhostdetails['source'] = $source;
177 $fwdhostdetails['keep_spam'] = ($redis->hGet('KEEP_SPAM', $_data)) ? "yes" : "no";
178 }
179 }
180 catch (RedisException $e) {
181 $_SESSION['return'][] = array(
182 'type' => 'danger',
183 'log' => array(__FUNCTION__, $_action, $_data_log),
184 'msg' => array('redis_error', $e)
185 );
186 return false;
187 }
188 return $fwdhostdetails;
189 break;
190 }
Matthias Andreas Benkard8f915942021-08-16 10:57:25 +0200191}