blob: 37ccbd85c8ae100e5c02fc25bc71f0669b678338 [file] [log] [blame]
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01001<?php
2function mailbox($_action, $_type, $_data = null, $_extra = null) {
3 global $pdo;
4 global $redis;
5 global $lang;
6 global $MAILBOX_DEFAULT_ATTRIBUTES;
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02007 global $iam_settings;
8
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01009 $_data_log = $_data;
10 !isset($_data_log['password']) ?: $_data_log['password'] = '*';
11 !isset($_data_log['password2']) ?: $_data_log['password2'] = '*';
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +020012
13 // Track mailboxes affected by alias operations for incremental SOGo updates
14 $update_sogo_mailboxes = array();
15
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +010016 switch ($_action) {
17 case 'add':
18 switch ($_type) {
19 case 'time_limited_alias':
20 if (!isset($_SESSION['acl']['spam_alias']) || $_SESSION['acl']['spam_alias'] != "1" ) {
21 $_SESSION['return'][] = array(
22 'type' => 'danger',
23 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
24 'msg' => 'access_denied'
25 );
26 return false;
27 }
28 if (isset($_data['username']) && filter_var($_data['username'], FILTER_VALIDATE_EMAIL)) {
29 if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data['username'])) {
30 $_SESSION['return'][] = array(
31 'type' => 'danger',
32 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
33 'msg' => 'access_denied'
34 );
35 return false;
36 }
37 else {
38 $username = $_data['username'];
39 }
40 }
41 else {
42 $username = $_SESSION['mailcow_cc_username'];
43 }
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +020044 if (isset($_data["validity"])) {
45 if (!filter_var($_data["validity"], FILTER_VALIDATE_INT, array('options' => array('min_range' => 1, 'max_range' => 87600)))) {
46 $_SESSION['return'][] = array(
47 'type' => 'danger',
48 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
49 'msg' => 'validity_missing'
50 );
51 return false;
52 }
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +010053 }
Matthias Andreas Benkard8f915942021-08-16 10:57:25 +020054 else {
55 // Default to 1 yr
56 $_data["validity"] = 8760;
57 }
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +020058 if (isset($_data["permanent"]) && filter_var($_data["permanent"], FILTER_VALIDATE_BOOL)) {
59 $permanent = 1;
60 }
61 else {
62 $permanent = 0;
63 }
Matthias Andreas Benkard8f915942021-08-16 10:57:25 +020064 $domain = $_data['domain'];
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +020065 // spamalias.description is NOT NULL, an absent description must not become a null insert
66 $description = $_data['description'] ?? '';
Matthias Andreas Benkard8f915942021-08-16 10:57:25 +020067 $valid_domains[] = mailbox('get', 'mailbox_details', $username)['domain'];
68 $valid_alias_domains = user_get_alias_details($username)['alias_domains'];
69 if (!empty($valid_alias_domains)) {
70 $valid_domains = array_merge($valid_domains, $valid_alias_domains);
71 }
72 if (!in_array($domain, $valid_domains)) {
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +010073 $_SESSION['return'][] = array(
74 'type' => 'danger',
75 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
76 'msg' => 'domain_invalid'
77 );
78 return false;
79 }
Matthias Andreas Benkard8f915942021-08-16 10:57:25 +020080 $validity = strtotime("+" . $_data["validity"] . " hour");
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +020081 $stmt = $pdo->prepare("INSERT INTO `spamalias` (`address`, `description`, `goto`, `validity`, `permanent`) VALUES
82 (:address, :description, :goto, :validity, :permanent)");
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +010083 $stmt->execute(array(
Matthias Andreas Benkard8f915942021-08-16 10:57:25 +020084 ':address' => readable_random_string(rand(rand(3, 9), rand(3, 9))) . '.' . readable_random_string(rand(rand(3, 9), rand(3, 9))) . '@' . $domain,
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +020085 ':description' => $description,
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +010086 ':goto' => $username,
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +020087 ':validity' => $validity,
88 ':permanent' => $permanent
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +010089 ));
90 $_SESSION['return'][] = array(
91 'type' => 'success',
92 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
93 'msg' => array('mailbox_modified', $username)
94 );
95 break;
96 case 'global_filter':
97 if ($_SESSION['mailcow_cc_role'] != "admin") {
98 $_SESSION['return'][] = array(
99 'type' => 'danger',
100 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
101 'msg' => 'access_denied'
102 );
103 return false;
104 }
105 $sieve = new Sieve\SieveParser();
106 $script_data = $_data['script_data'];
107 $script_data = str_replace("\r\n", "\n", $script_data); // windows -> unix
108 $script_data = str_replace("\r", "\n", $script_data); // remaining -> unix
109 $filter_type = $_data['filter_type'];
110 try {
111 $sieve->parse($script_data);
112 }
113 catch (Exception $e) {
114 $_SESSION['return'][] = array(
115 'type' => 'danger',
116 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
117 'msg' => array('sieve_error', $e->getMessage())
118 );
119 return false;
120 }
121 if ($filter_type == 'prefilter') {
122 try {
123 if (file_exists('/global_sieve/before')) {
124 $filter_handle = fopen('/global_sieve/before', 'w');
125 if (!$filter_handle) {
126 throw new Exception($lang['danger']['file_open_error']);
127 }
128 fwrite($filter_handle, $script_data);
129 fclose($filter_handle);
130 }
131 $restart_response = json_decode(docker('post', 'dovecot-mailcow', 'restart'), true);
132 if ($restart_response['type'] == "success") {
133 $_SESSION['return'][] = array(
134 'type' => 'success',
135 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
136 'msg' => 'dovecot_restart_success'
137 );
138 }
139 else {
140 $_SESSION['return'][] = array(
141 'type' => 'warning',
142 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
143 'msg' => 'dovecot_restart_failed'
144 );
145 }
146 }
147 catch (Exception $e) {
148 $_SESSION['return'][] = array(
149 'type' => 'danger',
150 'log' => array(__FUNCTION__, $_action, $_data_log),
151 'msg' => array('global_filter_write_error', htmlspecialchars($e->getMessage()))
152 );
153 return false;
154 }
155 }
156 elseif ($filter_type == 'postfilter') {
157 try {
158 if (file_exists('/global_sieve/after')) {
159 $filter_handle = fopen('/global_sieve/after', 'w');
160 if (!$filter_handle) {
161 throw new Exception($lang['danger']['file_open_error']);
162 }
163 fwrite($filter_handle, $script_data);
164 fclose($filter_handle);
165 }
166 $restart_response = json_decode(docker('post', 'dovecot-mailcow', 'restart'), true);
167 if ($restart_response['type'] == "success") {
168 $_SESSION['return'][] = array(
169 'type' => 'success',
170 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
171 'msg' => 'dovecot_restart_success'
172 );
173 }
174 else {
175 $_SESSION['return'][] = array(
176 'type' => 'warning',
177 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
178 'msg' => 'dovecot_restart_failed'
179 );
180 }
181 }
182 catch (Exception $e) {
183 $_SESSION['return'][] = array(
184 'type' => 'danger',
185 'log' => array(__FUNCTION__, $_action, $_data_log),
186 'msg' => array('global_filter_write_error', htmlspecialchars($e->getMessage()))
187 );
188 return false;
189 }
190 }
191 else {
192 $_SESSION['return'][] = array(
193 'type' => 'danger',
194 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
195 'msg' => 'invalid_filter_type'
196 );
197 return false;
198 }
199 $_SESSION['return'][] = array(
200 'type' => 'success',
201 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
202 'msg' => 'global_filter_written'
203 );
204 return true;
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +0200205 break;
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +0100206 case 'filter':
207 $sieve = new Sieve\SieveParser();
208 if (!isset($_SESSION['acl']['filters']) || $_SESSION['acl']['filters'] != "1" ) {
209 $_SESSION['return'][] = array(
210 'type' => 'danger',
211 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
212 'msg' => 'access_denied'
213 );
214 return false;
215 }
216 if (isset($_data['username']) && filter_var($_data['username'], FILTER_VALIDATE_EMAIL)) {
217 if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data['username'])) {
218 $_SESSION['return'][] = array(
219 'type' => 'danger',
220 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
221 'msg' => 'access_denied'
222 );
223 return false;
224 }
225 else {
226 $username = $_data['username'];
227 }
228 }
229 elseif ($_SESSION['mailcow_cc_role'] == "user") {
230 $username = $_SESSION['mailcow_cc_username'];
231 }
232 else {
233 $_SESSION['return'][] = array(
234 'type' => 'danger',
235 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
236 'msg' => 'no_user_defined'
237 );
238 return false;
239 }
240 $active = intval($_data['active']);
241 $script_data = $_data['script_data'];
242 $script_desc = $_data['script_desc'];
243 $filter_type = $_data['filter_type'];
244 if (empty($script_data)) {
245 $_SESSION['return'][] = array(
246 'type' => 'danger',
247 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
248 'msg' => 'script_empty'
249 );
250 return false;
251 }
252 try {
253 $sieve->parse($script_data);
254 }
255 catch (Exception $e) {
256 $_SESSION['return'][] = array(
257 'type' => 'danger',
258 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
259 'msg' => array('sieve_error', $e->getMessage())
260 );
261 return false;
262 }
263 if (empty($script_data) || empty($script_desc)) {
264 $_SESSION['return'][] = array(
265 'type' => 'danger',
266 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
267 'msg' => 'value_missing'
268 );
269 return false;
270 }
271 if ($filter_type != 'postfilter' && $filter_type != 'prefilter') {
272 $_SESSION['return'][] = array(
273 'type' => 'danger',
274 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
275 'msg' => 'filter_type'
276 );
277 return false;
278 }
279 if (!empty($active)) {
280 $script_name = 'active';
281 $stmt = $pdo->prepare("UPDATE `sieve_filters` SET `script_name` = 'inactive' WHERE `username` = :username AND `filter_type` = :filter_type");
282 $stmt->execute(array(
283 ':username' => $username,
284 ':filter_type' => $filter_type
285 ));
286 }
287 else {
288 $script_name = 'inactive';
289 }
290 $stmt = $pdo->prepare("INSERT INTO `sieve_filters` (`username`, `script_data`, `script_desc`, `script_name`, `filter_type`)
291 VALUES (:username, :script_data, :script_desc, :script_name, :filter_type)");
292 $stmt->execute(array(
293 ':username' => $username,
294 ':script_data' => $script_data,
295 ':script_desc' => $script_desc,
296 ':script_name' => $script_name,
297 ':filter_type' => $filter_type
298 ));
299 $_SESSION['return'][] = array(
300 'type' => 'success',
301 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
302 'msg' => array('mailbox_modified', $username)
303 );
304 break;
305 case 'syncjob':
306 if (!isset($_SESSION['acl']['syncjobs']) || $_SESSION['acl']['syncjobs'] != "1" ) {
307 $_SESSION['return'][] = array(
308 'type' => 'danger',
309 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
310 'msg' => 'access_denied'
311 );
312 return false;
313 }
314 if (isset($_data['username']) && filter_var($_data['username'], FILTER_VALIDATE_EMAIL)) {
315 if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data['username'])) {
316 $_SESSION['return'][] = array(
317 'type' => 'danger',
318 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
319 'msg' => 'access_denied'
320 );
321 return false;
322 }
323 else {
324 $username = $_data['username'];
325 }
326 }
327 elseif ($_SESSION['mailcow_cc_role'] == "user") {
328 $username = $_SESSION['mailcow_cc_username'];
329 }
330 else {
331 $_SESSION['return'][] = array(
332 'type' => 'danger',
333 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
334 'msg' => 'no_user_defined'
335 );
336 return false;
337 }
338 $active = intval($_data['active']);
339 $subscribeall = intval($_data['subscribeall']);
340 $delete2duplicates = intval($_data['delete2duplicates']);
341 $delete1 = intval($_data['delete1']);
342 $delete2 = intval($_data['delete2']);
343 $timeout1 = intval($_data['timeout1']);
344 $timeout2 = intval($_data['timeout2']);
345 $skipcrossduplicates = intval($_data['skipcrossduplicates']);
346 $automap = intval($_data['automap']);
Matthias Andreas Benkard9dabdab2024-03-06 13:12:09 +0100347 $dry = intval($_data['dry']);
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +0100348 $port1 = $_data['port1'];
349 $host1 = strtolower($_data['host1']);
350 $password1 = $_data['password1'];
351 $exclude = $_data['exclude'];
352 $maxage = $_data['maxage'];
353 $maxbytespersecond = $_data['maxbytespersecond'];
354 $subfolder2 = $_data['subfolder2'];
355 $user1 = $_data['user1'];
356 $mins_interval = $_data['mins_interval'];
357 $enc1 = $_data['enc1'];
358 $custom_params = (empty(trim($_data['custom_params']))) ? '' : trim($_data['custom_params']);
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +0100359
360 // validate custom params
361 foreach (explode('-', $custom_params) as $param){
362 if(empty($param)) continue;
363
364 // extract option
365 if (str_contains($param, '=')) $param = explode('=', $param)[0];
366 else $param = rtrim($param, ' ');
367 // remove first char if first char is -
368 if ($param[0] == '-') $param = ltrim($param, $param[0]);
369
370 if (str_contains($param, ' ')) {
371 // bad char
372 $_SESSION['return'][] = array(
373 'type' => 'danger',
374 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
375 'msg' => 'bad character SPACE'
376 );
377 return false;
378 }
379
380 // check if param is whitelisted
381 if (!in_array(strtolower($param), $GLOBALS["IMAPSYNC_OPTIONS"]["whitelist"])){
382 // bad option
383 $_SESSION['return'][] = array(
384 'type' => 'danger',
385 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
386 'msg' => 'bad option '. $param
387 );
388 return false;
389 }
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +0100390 }
391 if (empty($subfolder2)) {
392 $subfolder2 = "";
393 }
394 if (!isset($maxage) || !filter_var($maxage, FILTER_VALIDATE_INT, array('options' => array('min_range' => 1, 'max_range' => 32000)))) {
395 $maxage = "0";
396 }
397 if (!isset($timeout1) || !filter_var($timeout1, FILTER_VALIDATE_INT, array('options' => array('min_range' => 1, 'max_range' => 32000)))) {
398 $timeout1 = "600";
399 }
400 if (!isset($timeout2) || !filter_var($timeout2, FILTER_VALIDATE_INT, array('options' => array('min_range' => 1, 'max_range' => 32000)))) {
401 $timeout2 = "600";
402 }
403 if (!isset($maxbytespersecond) || !filter_var($maxbytespersecond, FILTER_VALIDATE_INT, array('options' => array('min_range' => 1, 'max_range' => 125000000)))) {
404 $maxbytespersecond = "0";
405 }
406 if (!filter_var($port1, FILTER_VALIDATE_INT, array('options' => array('min_range' => 1, 'max_range' => 65535)))) {
407 $_SESSION['return'][] = array(
408 'type' => 'danger',
409 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
410 'msg' => 'access_denied'
411 );
412 return false;
413 }
414 if (!filter_var($mins_interval, FILTER_VALIDATE_INT, array('options' => array('min_range' => 1, 'max_range' => 43800)))) {
415 $_SESSION['return'][] = array(
416 'type' => 'danger',
417 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
418 'msg' => 'access_denied'
419 );
420 return false;
421 }
422 // if (!is_valid_domain_name($host1)) {
423 // $_SESSION['return'][] = array(
424 // 'type' => 'danger',
425 // 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
426 // 'msg' => 'access_denied'
427 // );
428 // return false;
429 // }
430 if ($enc1 != "TLS" && $enc1 != "SSL" && $enc1 != "PLAIN") {
431 $_SESSION['return'][] = array(
432 'type' => 'danger',
433 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
434 'msg' => 'access_denied'
435 );
436 return false;
437 }
438 if (@preg_match("/" . $exclude . "/", null) === false) {
439 $_SESSION['return'][] = array(
440 'type' => 'danger',
441 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
442 'msg' => 'access_denied'
443 );
444 return false;
445 }
446 $stmt = $pdo->prepare("SELECT '1' FROM `imapsync`
447 WHERE `user2` = :user2 AND `user1` = :user1 AND `host1` = :host1");
448 $stmt->execute(array(':user1' => $user1, ':user2' => $username, ':host1' => $host1));
449 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
450 if ($num_results != 0) {
451 $_SESSION['return'][] = array(
452 'type' => 'danger',
453 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
454 'msg' => array('object_exists', htmlspecialchars($host1 . ' / ' . $user1))
455 );
456 return false;
457 }
Matthias Andreas Benkard9dabdab2024-03-06 13:12:09 +0100458 $stmt = $pdo->prepare("INSERT INTO `imapsync` (`user2`, `exclude`, `delete1`, `delete2`, `timeout1`, `timeout2`, `automap`, `skipcrossduplicates`, `maxbytespersecond`, `subscribeall`, `dry`, `maxage`, `subfolder2`, `host1`, `authmech1`, `user1`, `password1`, `mins_interval`, `port1`, `enc1`, `delete2duplicates`, `custom_params`, `active`)
459 VALUES (:user2, :exclude, :delete1, :delete2, :timeout1, :timeout2, :automap, :skipcrossduplicates, :maxbytespersecond, :subscribeall, :dry, :maxage, :subfolder2, :host1, :authmech1, :user1, :password1, :mins_interval, :port1, :enc1, :delete2duplicates, :custom_params, :active)");
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +0100460 $stmt->execute(array(
461 ':user2' => $username,
462 ':custom_params' => $custom_params,
463 ':exclude' => $exclude,
464 ':maxage' => $maxage,
465 ':delete1' => $delete1,
466 ':delete2' => $delete2,
467 ':timeout1' => $timeout1,
468 ':timeout2' => $timeout2,
469 ':automap' => $automap,
470 ':skipcrossduplicates' => $skipcrossduplicates,
471 ':maxbytespersecond' => $maxbytespersecond,
472 ':subscribeall' => $subscribeall,
Matthias Andreas Benkard9dabdab2024-03-06 13:12:09 +0100473 ':dry' => $dry,
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +0100474 ':subfolder2' => $subfolder2,
475 ':host1' => $host1,
476 ':authmech1' => 'PLAIN',
477 ':user1' => $user1,
478 ':password1' => $password1,
479 ':mins_interval' => $mins_interval,
480 ':port1' => $port1,
481 ':enc1' => $enc1,
482 ':delete2duplicates' => $delete2duplicates,
483 ':active' => $active,
484 ));
485 $_SESSION['return'][] = array(
486 'type' => 'success',
487 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
488 'msg' => array('mailbox_modified', $username)
489 );
490 break;
491 case 'domain':
492 if ($_SESSION['mailcow_cc_role'] != "admin") {
493 $_SESSION['return'][] = array(
494 'type' => 'danger',
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +0100495 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_extra),
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +0100496 'msg' => 'access_denied'
497 );
498 return false;
499 }
Matthias Andreas Benkard9dabdab2024-03-06 13:12:09 +0100500 $DOMAIN_DEFAULT_ATTRIBUTES = null;
501 if ($_data['template']){
502 $DOMAIN_DEFAULT_ATTRIBUTES = mailbox('get', 'domain_templates', $_data['template'])['attributes'];
503 }
504 if (empty($DOMAIN_DEFAULT_ATTRIBUTES)) {
505 $DOMAIN_DEFAULT_ATTRIBUTES = mailbox('get', 'domain_templates')[0]['attributes'];
506 }
507
Matthias Andreas Benkard8f915942021-08-16 10:57:25 +0200508 $domain = idn_to_ascii(strtolower(trim($_data['domain'])), 0, INTL_IDNA_VARIANT_UTS46);
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +0100509 $description = $_data['description'];
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +0100510 if (empty($description)) $description = $domain;
Matthias Andreas Benkard9dabdab2024-03-06 13:12:09 +0100511 $tags = (isset($_data['tags'])) ? (array)$_data['tags'] : $DOMAIN_DEFAULT_ATTRIBUTES['tags'];
512 $aliases = (isset($_data['aliases'])) ? (int)$_data['aliases'] : $DOMAIN_DEFAULT_ATTRIBUTES['max_num_aliases_for_domain'];
513 $mailboxes = (isset($_data['mailboxes'])) ? (int)$_data['mailboxes'] : $DOMAIN_DEFAULT_ATTRIBUTES['max_num_mboxes_for_domain'];
514 $defquota = (isset($_data['defquota'])) ? (int)$_data['defquota'] : $DOMAIN_DEFAULT_ATTRIBUTES['def_quota_for_mbox'] / 1024 ** 2;
515 $maxquota = (isset($_data['maxquota'])) ? (int)$_data['maxquota'] : $DOMAIN_DEFAULT_ATTRIBUTES['max_quota_for_mbox'] / 1024 ** 2;
Matthias Andreas Benkard8f915942021-08-16 10:57:25 +0200516 $restart_sogo = (int)$_data['restart_sogo'];
Matthias Andreas Benkard9dabdab2024-03-06 13:12:09 +0100517 $quota = (isset($_data['quota'])) ? (int)$_data['quota'] : $DOMAIN_DEFAULT_ATTRIBUTES['max_quota_for_domain'] / 1024 ** 2;
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +0100518 if ($defquota > $maxquota) {
519 $_SESSION['return'][] = array(
520 'type' => 'danger',
521 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
522 'msg' => 'mailbox_defquota_exceeds_mailbox_maxquota'
523 );
524 return false;
525 }
526 if ($maxquota > $quota) {
527 $_SESSION['return'][] = array(
528 'type' => 'danger',
529 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
530 'msg' => 'mailbox_quota_exceeds_domain_quota'
531 );
532 return false;
533 }
534 if ($defquota == "0" || empty($defquota)) {
535 $_SESSION['return'][] = array(
536 'type' => 'danger',
537 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
538 'msg' => 'defquota_empty'
539 );
540 return false;
541 }
542 if ($maxquota == "0" || empty($maxquota)) {
543 $_SESSION['return'][] = array(
544 'type' => 'danger',
545 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
546 'msg' => 'maxquota_empty'
547 );
548 return false;
549 }
Matthias Andreas Benkard9dabdab2024-03-06 13:12:09 +0100550 $active = (isset($_data['active'])) ? intval($_data['active']) : $DOMAIN_DEFAULT_ATTRIBUTES['active'];
551 $relay_all_recipients = (isset($_data['relay_all_recipients'])) ? intval($_data['relay_all_recipients']) : $DOMAIN_DEFAULT_ATTRIBUTES['relay_all_recipients'];
552 $relay_unknown_only = (isset($_data['relay_unknown_only'])) ? intval($_data['relay_unknown_only']) : $DOMAIN_DEFAULT_ATTRIBUTES['relay_unknown_only'];
553 $backupmx = (isset($_data['backupmx'])) ? intval($_data['backupmx']) : $DOMAIN_DEFAULT_ATTRIBUTES['backupmx'];
554 $gal = (isset($_data['gal'])) ? intval($_data['gal']) : $DOMAIN_DEFAULT_ATTRIBUTES['gal'];
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +0100555 if ($relay_all_recipients == 1) {
556 $backupmx = '1';
557 }
558 if ($relay_unknown_only == 1) {
559 $backupmx = 1;
560 $relay_all_recipients = 1;
561 }
562 if (!is_valid_domain_name($domain)) {
563 $_SESSION['return'][] = array(
564 'type' => 'danger',
565 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
566 'msg' => 'domain_invalid'
567 );
568 return false;
569 }
570 foreach (array($quota, $maxquota, $mailboxes, $aliases) as $data) {
571 if (!is_numeric($data)) {
572 $_SESSION['return'][] = array(
573 'type' => 'danger',
574 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
575 'msg' => array('object_is_not_numeric', htmlspecialchars($data))
576 );
577 return false;
578 }
579 }
580 $stmt = $pdo->prepare("SELECT `domain` FROM `domain`
581 WHERE `domain` = :domain");
582 $stmt->execute(array(':domain' => $domain));
583 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
584 $stmt = $pdo->prepare("SELECT `alias_domain` FROM `alias_domain`
585 WHERE `alias_domain` = :domain");
586 $stmt->execute(array(':domain' => $domain));
587 $num_results = $num_results + count($stmt->fetchAll(PDO::FETCH_ASSOC));
588 if ($num_results != 0) {
589 $_SESSION['return'][] = array(
590 'type' => 'danger',
591 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
592 'msg' => array('domain_exists', htmlspecialchars($domain))
593 );
594 return false;
595 }
596 if ($domain == getenv('MAILCOW_HOSTNAME')) {
597 $_SESSION['return'][] = array(
598 'type' => 'danger',
599 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
600 'msg' => 'domain_cannot_match_hostname'
601 );
602 return false;
603 }
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +0100604
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +0100605 $stmt = $pdo->prepare("DELETE FROM `sender_acl` WHERE `external` = 1 AND `send_as` LIKE :domain");
606 $stmt->execute(array(
607 ':domain' => '%@' . $domain
608 ));
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +0100609 // save domain
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +0100610 $stmt = $pdo->prepare("INSERT INTO `domain` (`domain`, `description`, `aliases`, `mailboxes`, `defquota`, `maxquota`, `quota`, `backupmx`, `gal`, `active`, `relay_unknown_only`, `relay_all_recipients`)
611 VALUES (:domain, :description, :aliases, :mailboxes, :defquota, :maxquota, :quota, :backupmx, :gal, :active, :relay_unknown_only, :relay_all_recipients)");
612 $stmt->execute(array(
613 ':domain' => $domain,
614 ':description' => $description,
615 ':aliases' => $aliases,
616 ':mailboxes' => $mailboxes,
617 ':defquota' => $defquota,
618 ':maxquota' => $maxquota,
619 ':quota' => $quota,
620 ':backupmx' => $backupmx,
621 ':gal' => $gal,
622 ':active' => $active,
623 ':relay_unknown_only' => $relay_unknown_only,
624 ':relay_all_recipients' => $relay_all_recipients
625 ));
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +0100626 // save tags
627 foreach($tags as $index => $tag){
628 if (empty($tag)) continue;
629 if ($index > $GLOBALS['TAGGING_LIMIT']) {
630 $_SESSION['return'][] = array(
631 'type' => 'warning',
632 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
633 'msg' => array('tag_limit_exceeded', 'limit '.$GLOBALS['TAGGING_LIMIT'])
634 );
635 break;
636 }
637 $stmt = $pdo->prepare("INSERT INTO `tags_domain` (`domain`, `tag_name`) VALUES (:domain, :tag_name)");
638 $stmt->execute(array(
639 ':domain' => $domain,
640 ':tag_name' => $tag,
641 ));
642 }
643
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +0100644 try {
645 $redis->hSet('DOMAIN_MAP', $domain, 1);
646 }
647 catch (RedisException $e) {
648 $_SESSION['return'][] = array(
649 'type' => 'danger',
650 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
651 'msg' => array('redis_error', $e)
652 );
653 return false;
654 }
Matthias Andreas Benkard9dabdab2024-03-06 13:12:09 +0100655 $_data['rl_value'] = (isset($_data['rl_value'])) ? intval($_data['rl_value']) : $DOMAIN_DEFAULT_ATTRIBUTES['rl_value'];
656 $_data['rl_frame'] = (isset($_data['rl_frame'])) ? $_data['rl_frame'] : $DOMAIN_DEFAULT_ATTRIBUTES['rl_frame'];
657 if (!empty($_data['rl_value']) && !empty($_data['rl_frame'])){
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +0100658 ratelimit('edit', 'domain', array('rl_value' => $_data['rl_value'], 'rl_frame' => $_data['rl_frame'], 'object' => $domain));
659 }
Matthias Andreas Benkard9dabdab2024-03-06 13:12:09 +0100660 $_data['key_size'] = (isset($_data['key_size'])) ? intval($_data['key_size']) : $DOMAIN_DEFAULT_ATTRIBUTES['key_size'];
661 $_data['dkim_selector'] = (isset($_data['dkim_selector'])) ? $_data['dkim_selector'] : $DOMAIN_DEFAULT_ATTRIBUTES['dkim_selector'];
Matthias Andreas Benkardb4b5cc12021-12-28 18:02:04 +0100662 if (!empty($_data['key_size']) && !empty($_data['dkim_selector'])) {
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +0100663 if (!empty($redis->hGet('DKIM_SELECTORS', $domain))) {
664 $_SESSION['return'][] = array(
665 'type' => 'success',
666 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
667 'msg' => 'domain_add_dkim_available'
668 );
669 }
670 else {
671 dkim('add', array('key_size' => $_data['key_size'], 'dkim_selector' => $_data['dkim_selector'], 'domains' => $domain));
672 }
Matthias Andreas Benkardb4b5cc12021-12-28 18:02:04 +0100673 }
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +0100674 if (!empty($restart_sogo)) {
675 $restart_response = json_decode(docker('post', 'sogo-mailcow', 'restart'), true);
676 if ($restart_response['type'] == "success") {
677 $_SESSION['return'][] = array(
678 'type' => 'success',
679 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
680 'msg' => array('domain_added', htmlspecialchars($domain))
681 );
682 return true;
683 }
684 else {
685 $_SESSION['return'][] = array(
686 'type' => 'warning',
687 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
688 'msg' => 'domain_added_sogo_failed'
689 );
690 return false;
691 }
692 }
693 $_SESSION['return'][] = array(
694 'type' => 'success',
695 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
696 'msg' => array('domain_added', htmlspecialchars($domain))
697 );
698 return true;
699 break;
700 case 'alias':
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +0200701 $addresses = array_map('trim', preg_split( "/( |,|;|\n)/", $_data['address']));
702 $gotos = array_map('trim', preg_split( "/( |,|;|\n)/", $_data['goto']));
703 $internal = intval($_data['internal']);
704 $active = intval($_data['active']);
705 $sender_allowed = intval($_data['sender_allowed']);
706 $sogo_visible = intval($_data['sogo_visible']);
707 $goto_null = intval($_data['goto_null']);
708 $goto_spam = intval($_data['goto_spam']);
709 $goto_ham = intval($_data['goto_ham']);
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +0100710 $private_comment = $_data['private_comment'];
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +0200711 $public_comment = $_data['public_comment'];
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +0100712 if (strlen($private_comment) > 160 | strlen($public_comment) > 160){
713 $_SESSION['return'][] = array(
714 'type' => 'danger',
715 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
716 'msg' => 'comment_too_long'
717 );
718 return false;
719 }
720 if (empty($addresses[0])) {
721 $_SESSION['return'][] = array(
722 'type' => 'danger',
723 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
724 'msg' => 'alias_empty'
725 );
726 return false;
727 }
728 if (empty($gotos[0]) && ($goto_null + $goto_spam + $goto_ham == 0)) {
729 $_SESSION['return'][] = array(
730 'type' => 'danger',
731 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
732 'msg' => 'goto_empty'
733 );
734 return false;
735 }
736 if ($goto_null == "1") {
737 $goto = "null@localhost";
738 }
739 elseif ($goto_spam == "1") {
740 $goto = "spam@localhost";
741 }
742 elseif ($goto_ham == "1") {
743 $goto = "ham@localhost";
744 }
745 else {
746 foreach ($gotos as $i => &$goto) {
747 if (empty($goto)) {
748 continue;
749 }
750 $goto_domain = idn_to_ascii(substr(strstr($goto, '@'), 1), 0, INTL_IDNA_VARIANT_UTS46);
751 $goto_local_part = strstr($goto, '@', true);
752 $goto = $goto_local_part.'@'.$goto_domain;
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +0200753 // Deny external goto domains: global switch (all roles) overrides the per-DA ACL
754 if (($GLOBALS['ALIAS_DISABLE_EXTERNAL_DOMAINS'] === true ||
755 (isset($_SESSION['acl']['alias_external_goto']) && $_SESSION['acl']['alias_external_goto'] != "1")) &&
756 !is_local_mailcow_domain($goto_domain)) {
757 $_SESSION['return'][] = array(
758 'type' => 'danger',
759 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
760 'msg' => array('external_goto_denied', htmlspecialchars($goto))
761 );
762 unset($gotos[$i]);
763 continue;
764 }
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +0100765 $stmt = $pdo->prepare("SELECT `username` FROM `mailbox`
766 WHERE `kind` REGEXP 'location|thing|group'
767 AND `username`= :goto");
768 $stmt->execute(array(':goto' => $goto));
769 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
770 if ($num_results != 0) {
771 $_SESSION['return'][] = array(
772 'type' => 'danger',
773 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
774 'msg' => array('goto_invalid', htmlspecialchars($goto))
775 );
776 unset($gotos[$i]);
777 continue;
778 }
779 if (!filter_var($goto, FILTER_VALIDATE_EMAIL) === true) {
780 $_SESSION['return'][] = array(
781 'type' => 'danger',
782 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
783 'msg' => array('goto_invalid', htmlspecialchars($goto))
784 );
785 unset($gotos[$i]);
786 continue;
787 }
788 }
Matthias Andreas Benkard8f915942021-08-16 10:57:25 +0200789 $gotos = array_unique($gotos);
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +0100790 $gotos = array_filter($gotos);
791 if (empty($gotos)) { return false; }
Matthias Andreas Benkard8f915942021-08-16 10:57:25 +0200792 $goto = implode(",", (array)$gotos);
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +0100793 }
794 foreach ($addresses as $address) {
795 if (empty($address)) {
796 continue;
797 }
798 if (in_array($address, $gotos)) {
799 continue;
800 }
801 $domain = idn_to_ascii(substr(strstr($address, '@'), 1), 0, INTL_IDNA_VARIANT_UTS46);
802 $local_part = strstr($address, '@', true);
803 $address = $local_part.'@'.$domain;
804 $domaindata = mailbox('get', 'domain_details', $domain);
805 if (is_array($domaindata) && $domaindata['aliases_left'] == "0") {
806 $_SESSION['return'][] = array(
807 'type' => 'danger',
808 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
809 'msg' => 'max_alias_exceeded'
810 );
811 return false;
812 }
813 $stmt = $pdo->prepare("SELECT `address` FROM `alias`
814 WHERE `address`= :address OR `address` IN (
815 SELECT `username` FROM `mailbox`, `alias_domain`
816 WHERE (
817 `alias_domain`.`alias_domain` = :address_d
818 AND `mailbox`.`username` = CONCAT(:address_l, '@', alias_domain.target_domain)))");
819 $stmt->execute(array(
820 ':address' => $address,
821 ':address_l' => $local_part,
822 ':address_d' => $domain
823 ));
824 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
825 if ($num_results != 0) {
826 $_SESSION['return'][] = array(
827 'type' => 'danger',
828 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
829 'msg' => array('is_alias_or_mailbox', htmlspecialchars($address))
830 );
831 continue;
832 }
833 $stmt = $pdo->prepare("SELECT `domain` FROM `domain`
834 WHERE `domain`= :domain1 OR `domain` = (SELECT `target_domain` FROM `alias_domain` WHERE `alias_domain` = :domain2)");
835 $stmt->execute(array(':domain1' => $domain, ':domain2' => $domain));
836 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
837 if ($num_results == 0) {
838 $_SESSION['return'][] = array(
839 'type' => 'danger',
840 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
841 'msg' => array('domain_not_found', htmlspecialchars($domain))
842 );
843 continue;
844 }
845 $stmt = $pdo->prepare("SELECT `address` FROM `spamalias`
846 WHERE `address`= :address");
847 $stmt->execute(array(':address' => $address));
848 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
849 if ($num_results != 0) {
850 $_SESSION['return'][] = array(
851 'type' => 'danger',
852 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
853 'msg' => array('is_spam_alias', htmlspecialchars($address))
854 );
855 continue;
856 }
857 if ((!filter_var($address, FILTER_VALIDATE_EMAIL) === true) && !empty($local_part)) {
858 $_SESSION['return'][] = array(
859 'type' => 'danger',
860 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
861 'msg' => array('alias_invalid', $address)
862 );
863 continue;
864 }
865 if (!hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $domain)) {
866 $_SESSION['return'][] = array(
867 'type' => 'danger',
868 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
869 'msg' => 'access_denied'
870 );
871 continue;
872 }
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +0200873 $stmt = $pdo->prepare("INSERT INTO `alias` (`address`, `public_comment`, `private_comment`, `goto`, `domain`, `sogo_visible`, `internal`, `sender_allowed`, `active`)
874 VALUES (:address, :public_comment, :private_comment, :goto, :domain, :sogo_visible, :internal, :sender_allowed, :active)");
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +0100875 if (!filter_var($address, FILTER_VALIDATE_EMAIL) === true) {
876 $stmt->execute(array(
877 ':address' => '@'.$domain,
878 ':public_comment' => $public_comment,
879 ':private_comment' => $private_comment,
880 ':address' => '@'.$domain,
881 ':goto' => $goto,
882 ':domain' => $domain,
883 ':sogo_visible' => $sogo_visible,
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +0200884 ':internal' => $internal,
885 ':sender_allowed' => $sender_allowed,
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +0100886 ':active' => $active
887 ));
888 }
889 else {
890 $stmt->execute(array(
891 ':address' => $address,
892 ':public_comment' => $public_comment,
893 ':private_comment' => $private_comment,
894 ':goto' => $goto,
895 ':domain' => $domain,
896 ':sogo_visible' => $sogo_visible,
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +0200897 ':internal' => $internal,
898 ':sender_allowed' => $sender_allowed,
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +0100899 ':active' => $active
900 ));
901 }
902 $id = $pdo->lastInsertId();
903 $_SESSION['return'][] = array(
904 'type' => 'success',
905 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
906 'msg' => array('alias_added', $address, $id)
907 );
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +0200908
909 // Track affected mailboxes for SOGo update
910 if (!empty($goto)) {
911 $gotos = array_map('trim', explode(',', $goto));
912 foreach ($gotos as $g) {
913 if (filter_var($g, FILTER_VALIDATE_EMAIL) &&
914 !in_array($g, array('null@localhost', 'spam@localhost', 'ham@localhost'))) {
915 $update_sogo_mailboxes[] = $g;
916 }
917 }
918 }
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +0100919 }
920 break;
921 case 'alias_domain':
922 $active = intval($_data['active']);
923 $alias_domains = array_map('trim', preg_split( "/( |,|;|\n)/", $_data['alias_domain']));
924 $alias_domains = array_filter($alias_domains);
925 $target_domain = idn_to_ascii(strtolower(trim($_data['target_domain'])), 0, INTL_IDNA_VARIANT_UTS46);
926 if (!isset($_SESSION['acl']['alias_domains']) || $_SESSION['acl']['alias_domains'] != "1" ) {
927 $_SESSION['return'][] = array(
928 'type' => 'danger',
929 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
930 'msg' => 'access_denied'
931 );
932 return false;
933 }
934 if (!is_valid_domain_name($target_domain)) {
935 $_SESSION['return'][] = array(
936 'type' => 'danger',
937 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
938 'msg' => 'target_domain_invalid'
939 );
940 return false;
941 }
942 if (!hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $target_domain)) {
943 $_SESSION['return'][] = array(
944 'type' => 'danger',
945 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
946 'msg' => 'access_denied'
947 );
948 return false;
949 }
950 foreach ($alias_domains as $i => $alias_domain) {
951 $alias_domain = idn_to_ascii(strtolower(trim($alias_domain)), 0, INTL_IDNA_VARIANT_UTS46);
952 if (!is_valid_domain_name($alias_domain)) {
953 $_SESSION['return'][] = array(
954 'type' => 'danger',
955 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
956 'msg' => array('alias_domain_invalid', htmlspecialchars(alias_domain))
957 );
958 continue;
959 }
960 if ($alias_domain == $target_domain) {
961 $_SESSION['return'][] = array(
962 'type' => 'danger',
963 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
964 'msg' => array('aliasd_targetd_identical', htmlspecialchars($target_domain))
965 );
966 continue;
967 }
968 $stmt = $pdo->prepare("SELECT `domain` FROM `domain`
969 WHERE `domain`= :target_domain");
970 $stmt->execute(array(':target_domain' => $target_domain));
971 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
972 if ($num_results == 0) {
973 $_SESSION['return'][] = array(
974 'type' => 'danger',
975 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
976 'msg' => array('targetd_not_found', htmlspecialchars($target_domain))
977 );
978 continue;
979 }
980 $stmt = $pdo->prepare("SELECT `domain` FROM `domain`
981 WHERE `domain`= :target_domain AND `backupmx` = '1'");
982 $stmt->execute(array(':target_domain' => $target_domain));
983 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
984 if ($num_results == 1) {
985 $_SESSION['return'][] = array(
986 'type' => 'danger',
987 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
988 'msg' => array('targetd_relay_domain', htmlspecialchars($target_domain))
989 );
990 continue;
991 }
992 $stmt = $pdo->prepare("SELECT `alias_domain` FROM `alias_domain` WHERE `alias_domain`= :alias_domain
993 UNION
994 SELECT `domain` FROM `domain` WHERE `domain`= :alias_domain_in_domain");
995 $stmt->execute(array(':alias_domain' => $alias_domain, ':alias_domain_in_domain' => $alias_domain));
996 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
997 if ($num_results != 0) {
998 $_SESSION['return'][] = array(
999 'type' => 'danger',
1000 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1001 'msg' => array('alias_domain_invalid', $alias_domain)
1002 );
1003 continue;
1004 }
1005 $stmt = $pdo->prepare("DELETE FROM `sender_acl` WHERE `external` = 1 AND `send_as` LIKE :domain");
1006 $stmt->execute(array(
1007 ':domain' => '%@' . $domain
1008 ));
1009 $stmt = $pdo->prepare("INSERT INTO `alias_domain` (`alias_domain`, `target_domain`, `active`)
1010 VALUES (:alias_domain, :target_domain, :active)");
1011 $stmt->execute(array(
1012 ':alias_domain' => $alias_domain,
1013 ':target_domain' => $target_domain,
1014 ':active' => $active
1015 ));
1016 try {
1017 $redis->hSet('DOMAIN_MAP', $alias_domain, 1);
1018 }
1019 catch (RedisException $e) {
1020 $_SESSION['return'][] = array(
1021 'type' => 'danger',
1022 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1023 'msg' => array('redis_error', $e)
1024 );
1025 return false;
1026 }
1027 if (!empty(intval($_data['rl_value']))) {
1028 ratelimit('edit', 'domain', array('rl_value' => $_data['rl_value'], 'rl_frame' => $_data['rl_frame'], 'object' => $alias_domain));
1029 }
Matthias Andreas Benkardb4b5cc12021-12-28 18:02:04 +01001030 if (!empty($_data['key_size']) && !empty($_data['dkim_selector'])) {
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +01001031 if (!empty($redis->hGet('DKIM_SELECTORS', $alias_domain))) {
1032 $_SESSION['return'][] = array(
1033 'type' => 'success',
1034 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1035 'msg' => 'domain_add_dkim_available'
1036 );
1037 }
1038 else {
1039 dkim('add', array('key_size' => $_data['key_size'], 'dkim_selector' => $_data['dkim_selector'], 'domains' => $alias_domain));
1040 }
Matthias Andreas Benkardb4b5cc12021-12-28 18:02:04 +01001041 }
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01001042 $_SESSION['return'][] = array(
1043 'type' => 'success',
1044 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1045 'msg' => array('aliasd_added', htmlspecialchars($alias_domain))
1046 );
1047 }
1048 break;
1049 case 'mailbox':
1050 $local_part = strtolower(trim($_data['local_part']));
1051 $domain = idn_to_ascii(strtolower(trim($_data['domain'])), 0, INTL_IDNA_VARIANT_UTS46);
1052 $username = $local_part . '@' . $domain;
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02001053 $authsource = 'mailcow';
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01001054 if (!filter_var($username, FILTER_VALIDATE_EMAIL)) {
1055 $_SESSION['return'][] = array(
1056 'type' => 'danger',
1057 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1058 'msg' => 'mailbox_invalid'
1059 );
1060 return false;
1061 }
1062 if (empty($_data['local_part'])) {
1063 $_SESSION['return'][] = array(
1064 'type' => 'danger',
1065 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1066 'msg' => 'mailbox_invalid'
1067 );
1068 return false;
1069 }
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02001070 if ($_data['authsource'] == "mailcow" ||
1071 in_array($_data['authsource'], array('keycloak', 'generic-oidc', 'ldap')) && $iam_settings['authsource'] == $_data['authsource']){
1072 $authsource = $_data['authsource'];
1073 }
Matthias Andreas Benkard9dabdab2024-03-06 13:12:09 +01001074 if (empty($name)) {
1075 $name = $local_part;
1076 }
1077 $template_attr = null;
1078 if ($_data['template']){
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02001079 $template_attr = mailbox('get', 'mailbox_templates', $_data['template'], $_extra)['attributes'];
Matthias Andreas Benkard9dabdab2024-03-06 13:12:09 +01001080 }
1081 if (empty($template_attr)) {
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02001082 $template_attr = mailbox('get', 'mailbox_templates', null, $_extra)[0]['attributes'];
Matthias Andreas Benkard9dabdab2024-03-06 13:12:09 +01001083 }
1084 $MAILBOX_DEFAULT_ATTRIBUTES = array_merge($MAILBOX_DEFAULT_ATTRIBUTES, $template_attr);
1085
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01001086 $password = $_data['password'];
1087 $password2 = $_data['password2'];
1088 $name = ltrim(rtrim($_data['name'], '>'), '<');
Matthias Andreas Benkard9dabdab2024-03-06 13:12:09 +01001089 $tags = (isset($_data['tags'])) ? $_data['tags'] : $MAILBOX_DEFAULT_ATTRIBUTES['tags'];
1090 $quota_m = (isset($_data['quota'])) ? intval($_data['quota']) : intval($MAILBOX_DEFAULT_ATTRIBUTES['quota']) / 1024 ** 2;
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02001091 if ($authsource != 'mailcow'){
1092 $password = '';
1093 $password2 = '';
1094 $password_hashed = '';
1095 }
1096 if (!hasACLAccess("unlimited_quota") && $quota_m === 0) {
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01001097 $_SESSION['return'][] = array(
1098 'type' => 'danger',
1099 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1100 'msg' => 'unlimited_quota_acl'
1101 );
1102 return false;
1103 }
Matthias Andreas Benkard9dabdab2024-03-06 13:12:09 +01001104
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +01001105 if (isset($_data['protocol_access'])) {
1106 $_data['protocol_access'] = (array)$_data['protocol_access'];
1107 $_data['imap_access'] = (in_array('imap', $_data['protocol_access'])) ? 1 : 0;
1108 $_data['pop3_access'] = (in_array('pop3', $_data['protocol_access'])) ? 1 : 0;
1109 $_data['smtp_access'] = (in_array('smtp', $_data['protocol_access'])) ? 1 : 0;
1110 $_data['sieve_access'] = (in_array('sieve', $_data['protocol_access'])) ? 1 : 0;
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02001111 $_data['eas_access'] = (in_array('eas', $_data['protocol_access'])) ? 1 : 0;
1112 $_data['dav_access'] = (in_array('dav', $_data['protocol_access'])) ? 1 : 0;
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +01001113 }
Matthias Andreas Benkard9dabdab2024-03-06 13:12:09 +01001114 $active = (isset($_data['active'])) ? intval($_data['active']) : intval($MAILBOX_DEFAULT_ATTRIBUTES['active']);
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01001115 $force_pw_update = (isset($_data['force_pw_update'])) ? intval($_data['force_pw_update']) : intval($MAILBOX_DEFAULT_ATTRIBUTES['force_pw_update']);
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02001116 $force_tfa = (isset($_data['force_tfa'])) ? intval($_data['force_tfa']) : intval($MAILBOX_DEFAULT_ATTRIBUTES['force_tfa']);
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01001117 $tls_enforce_in = (isset($_data['tls_enforce_in'])) ? intval($_data['tls_enforce_in']) : intval($MAILBOX_DEFAULT_ATTRIBUTES['tls_enforce_in']);
1118 $tls_enforce_out = (isset($_data['tls_enforce_out'])) ? intval($_data['tls_enforce_out']) : intval($MAILBOX_DEFAULT_ATTRIBUTES['tls_enforce_out']);
1119 $sogo_access = (isset($_data['sogo_access'])) ? intval($_data['sogo_access']) : intval($MAILBOX_DEFAULT_ATTRIBUTES['sogo_access']);
1120 $imap_access = (isset($_data['imap_access'])) ? intval($_data['imap_access']) : intval($MAILBOX_DEFAULT_ATTRIBUTES['imap_access']);
1121 $pop3_access = (isset($_data['pop3_access'])) ? intval($_data['pop3_access']) : intval($MAILBOX_DEFAULT_ATTRIBUTES['pop3_access']);
1122 $smtp_access = (isset($_data['smtp_access'])) ? intval($_data['smtp_access']) : intval($MAILBOX_DEFAULT_ATTRIBUTES['smtp_access']);
Matthias Andreas Benkardb4b5cc12021-12-28 18:02:04 +01001123 $sieve_access = (isset($_data['sieve_access'])) ? intval($_data['sieve_access']) : intval($MAILBOX_DEFAULT_ATTRIBUTES['sieve_access']);
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02001124 $eas_access = (isset($_data['eas_access'])) ? intval($_data['eas_access']) : intval($MAILBOX_DEFAULT_ATTRIBUTES['eas_access']);
1125 $dav_access = (isset($_data['dav_access'])) ? intval($_data['dav_access']) : intval($MAILBOX_DEFAULT_ATTRIBUTES['dav_access']);
Matthias Andreas Benkard8f915942021-08-16 10:57:25 +02001126 $relayhost = (isset($_data['relayhost'])) ? intval($_data['relayhost']) : 0;
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01001127 $quarantine_notification = (isset($_data['quarantine_notification'])) ? strval($_data['quarantine_notification']) : strval($MAILBOX_DEFAULT_ATTRIBUTES['quarantine_notification']);
1128 $quarantine_category = (isset($_data['quarantine_category'])) ? strval($_data['quarantine_category']) : strval($MAILBOX_DEFAULT_ATTRIBUTES['quarantine_category']);
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02001129 // Validate quarantine_category
1130 if (!in_array($quarantine_category, array('add_header', 'reject', 'all'))) {
1131 $_SESSION['return'][] = array(
1132 'type' => 'danger',
1133 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1134 'msg' => 'quarantine_category_invalid'
1135 );
1136 return false;
1137 }
Matthias Andreas Benkard8f915942021-08-16 10:57:25 +02001138 $quota_b = ($quota_m * 1048576);
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02001139 $attribute_hash = (!empty($_data['attribute_hash'])) ? $_data['attribute_hash'] : '';
1140 if (in_array($authsource, array('keycloak', 'generic-oidc', 'ldap'))){
1141 $force_pw_update = 0;
1142 }
1143 if ($authsource == 'generic-oidc'){
1144 $force_tfa = 0;
1145 }
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01001146 $mailbox_attrs = json_encode(
1147 array(
1148 'force_pw_update' => strval($force_pw_update),
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02001149 'force_tfa' => strval($force_tfa),
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01001150 'tls_enforce_in' => strval($tls_enforce_in),
1151 'tls_enforce_out' => strval($tls_enforce_out),
1152 'sogo_access' => strval($sogo_access),
1153 'imap_access' => strval($imap_access),
1154 'pop3_access' => strval($pop3_access),
1155 'smtp_access' => strval($smtp_access),
Matthias Andreas Benkardb4b5cc12021-12-28 18:02:04 +01001156 'sieve_access' => strval($sieve_access),
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02001157 'eas_access' => strval($eas_access),
1158 'dav_access' => strval($dav_access),
Matthias Andreas Benkard8f915942021-08-16 10:57:25 +02001159 'relayhost' => strval($relayhost),
1160 'passwd_update' => time(),
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01001161 'mailbox_format' => strval($MAILBOX_DEFAULT_ATTRIBUTES['mailbox_format']),
1162 'quarantine_notification' => strval($quarantine_notification),
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02001163 'quarantine_category' => strval($quarantine_category),
1164 'attribute_hash' => $attribute_hash
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01001165 )
1166 );
1167 if (!is_valid_domain_name($domain)) {
1168 $_SESSION['return'][] = array(
1169 'type' => 'danger',
1170 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1171 'msg' => 'domain_invalid'
1172 );
1173 return false;
1174 }
1175 if (!hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $domain)) {
1176 $_SESSION['return'][] = array(
1177 'type' => 'danger',
1178 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1179 'msg' => 'access_denied'
1180 );
1181 return false;
1182 }
1183 $stmt = $pdo->prepare("SELECT `mailboxes`, `maxquota`, `quota` FROM `domain`
1184 WHERE `domain` = :domain");
1185 $stmt->execute(array(':domain' => $domain));
1186 $DomainData = $stmt->fetch(PDO::FETCH_ASSOC);
1187 $stmt = $pdo->prepare("SELECT
1188 COUNT(*) as count,
1189 COALESCE(ROUND(SUM(`quota`)/1048576), 0) as `quota`
1190 FROM `mailbox`
Matthias Andreas Benkard8f915942021-08-16 10:57:25 +02001191 WHERE (`kind` = '' OR `kind` = NULL)
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01001192 AND `domain` = :domain");
1193 $stmt->execute(array(':domain' => $domain));
1194 $MailboxData = $stmt->fetch(PDO::FETCH_ASSOC);
1195 $stmt = $pdo->prepare("SELECT `local_part` FROM `mailbox` WHERE `local_part` = :local_part and `domain`= :domain");
1196 $stmt->execute(array(':local_part' => $local_part, ':domain' => $domain));
1197 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
1198 if ($num_results != 0) {
1199 $_SESSION['return'][] = array(
1200 'type' => 'danger',
1201 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1202 'msg' => array('object_exists', htmlspecialchars($username))
1203 );
1204 return false;
1205 }
1206 $stmt = $pdo->prepare("SELECT `address` FROM `alias` WHERE address= :username");
1207 $stmt->execute(array(':username' => $username));
1208 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
1209 if ($num_results != 0) {
1210 $_SESSION['return'][] = array(
1211 'type' => 'danger',
1212 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1213 'msg' => array('is_alias', htmlspecialchars($username))
1214 );
1215 return false;
1216 }
1217 $stmt = $pdo->prepare("SELECT `address` FROM `spamalias` WHERE `address`= :username");
1218 $stmt->execute(array(':username' => $username));
1219 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
1220 if ($num_results != 0) {
1221 $_SESSION['return'][] = array(
1222 'type' => 'danger',
1223 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1224 'msg' => array('is_spam_alias', htmlspecialchars($username))
1225 );
1226 return false;
1227 }
1228 $stmt = $pdo->prepare("SELECT `domain` FROM `domain` WHERE `domain`= :domain");
1229 $stmt->execute(array(':domain' => $domain));
1230 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
1231 if ($num_results == 0) {
1232 $_SESSION['return'][] = array(
1233 'type' => 'danger',
1234 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1235 'msg' => array('domain_not_found', htmlspecialchars($domain))
1236 );
1237 return false;
1238 }
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02001239 if ($authsource == 'mailcow'){
1240 if (password_check($password, $password2) !== true) {
1241 return false;
1242 }
1243 $password_hashed = hash_password($password);
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01001244 }
1245 if ($MailboxData['count'] >= $DomainData['mailboxes']) {
1246 $_SESSION['return'][] = array(
1247 'type' => 'danger',
1248 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1249 'msg' => array('max_mailbox_exceeded', $MailboxData['count'], $DomainData['mailboxes'])
1250 );
1251 return false;
1252 }
1253 if ($quota_m > $DomainData['maxquota']) {
1254 $_SESSION['return'][] = array(
1255 'type' => 'danger',
1256 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1257 'msg' => array('mailbox_quota_exceeded', $DomainData['maxquota'])
1258 );
1259 return false;
1260 }
1261 if (($MailboxData['quota'] + $quota_m) > $DomainData['quota']) {
1262 $quota_left_m = ($DomainData['quota'] - $MailboxData['quota']);
1263 $_SESSION['return'][] = array(
1264 'type' => 'danger',
1265 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1266 'msg' => array('mailbox_quota_left_exceeded', $quota_left_m)
1267 );
1268 return false;
1269 }
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02001270 $stmt = $pdo->prepare("INSERT INTO `mailbox` (`username`, `password`, `name`, `quota`, `local_part`, `domain`, `attributes`, `authsource`, `active`)
1271 VALUES (:username, :password_hashed, :name, :quota_b, :local_part, :domain, :mailbox_attrs, :authsource, :active)");
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01001272 $stmt->execute(array(
1273 ':username' => $username,
1274 ':password_hashed' => $password_hashed,
1275 ':name' => $name,
1276 ':quota_b' => $quota_b,
1277 ':local_part' => $local_part,
1278 ':domain' => $domain,
1279 ':mailbox_attrs' => $mailbox_attrs,
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02001280 ':authsource' => $authsource,
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01001281 ':active' => $active
1282 ));
Matthias Andreas Benkard8f915942021-08-16 10:57:25 +02001283 $stmt = $pdo->prepare("UPDATE `mailbox` SET
1284 `attributes` = JSON_SET(`attributes`, '$.passwd_update', NOW())
1285 WHERE `username` = :username");
1286 $stmt->execute(array(
1287 ':username' => $username
1288 ));
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02001289 // save delimiter_action
1290 if (isset($_data['tagged_mail_handler'])) {
1291 mailbox('edit', 'delimiter_action', array(
1292 'username' => $username,
1293 'tagged_mail_handler' => $_data['tagged_mail_handler']
1294 ));
1295 }
1296
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +01001297 // save tags
1298 foreach($tags as $index => $tag){
1299 if (empty($tag)) continue;
1300 if ($index > $GLOBALS['TAGGING_LIMIT']) {
1301 $_SESSION['return'][] = array(
1302 'type' => 'warning',
1303 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1304 'msg' => array('tag_limit_exceeded', 'limit '.$GLOBALS['TAGGING_LIMIT'])
1305 );
1306 break;
1307 }
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02001308 try {
1309 $stmt = $pdo->prepare("INSERT INTO `tags_mailbox` (`username`, `tag_name`) VALUES (:username, :tag_name)");
1310 $stmt->execute(array(
1311 ':username' => $username,
1312 ':tag_name' => $tag,
1313 ));
1314 } catch (Exception $e) {
1315 }
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +01001316 }
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01001317 $stmt = $pdo->prepare("INSERT INTO `quota2` (`username`, `bytes`, `messages`)
1318 VALUES (:username, '0', '0') ON DUPLICATE KEY UPDATE `bytes` = '0', `messages` = '0';");
1319 $stmt->execute(array(':username' => $username));
1320 $stmt = $pdo->prepare("INSERT INTO `quota2replica` (`username`, `bytes`, `messages`)
1321 VALUES (:username, '0', '0') ON DUPLICATE KEY UPDATE `bytes` = '0', `messages` = '0';");
1322 $stmt->execute(array(':username' => $username));
1323 $stmt = $pdo->prepare("INSERT INTO `alias` (`address`, `goto`, `domain`, `active`)
1324 VALUES (:username1, :username2, :domain, :active)");
1325 $stmt->execute(array(
1326 ':username1' => $username,
1327 ':username2' => $username,
1328 ':domain' => $domain,
1329 ':active' => $active
1330 ));
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +01001331
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02001332
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +01001333 if (isset($_data['acl'])) {
1334 $_data['acl'] = (array)$_data['acl'];
1335 $_data['spam_alias'] = (in_array('spam_alias', $_data['acl'])) ? 1 : 0;
1336 $_data['tls_policy'] = (in_array('tls_policy', $_data['acl'])) ? 1 : 0;
1337 $_data['spam_score'] = (in_array('spam_score', $_data['acl'])) ? 1 : 0;
1338 $_data['spam_policy'] = (in_array('spam_policy', $_data['acl'])) ? 1 : 0;
1339 $_data['delimiter_action'] = (in_array('delimiter_action', $_data['acl'])) ? 1 : 0;
1340 $_data['syncjobs'] = (in_array('syncjobs', $_data['acl'])) ? 1 : 0;
1341 $_data['eas_reset'] = (in_array('eas_reset', $_data['acl'])) ? 1 : 0;
1342 $_data['sogo_profile_reset'] = (in_array('sogo_profile_reset', $_data['acl'])) ? 1 : 0;
1343 $_data['pushover'] = (in_array('pushover', $_data['acl'])) ? 1 : 0;
1344 $_data['quarantine'] = (in_array('quarantine', $_data['acl'])) ? 1 : 0;
1345 $_data['quarantine_attachments'] = (in_array('quarantine_attachments', $_data['acl'])) ? 1 : 0;
1346 $_data['quarantine_notification'] = (in_array('quarantine_notification', $_data['acl'])) ? 1 : 0;
1347 $_data['quarantine_category'] = (in_array('quarantine_category', $_data['acl'])) ? 1 : 0;
1348 $_data['app_passwds'] = (in_array('app_passwds', $_data['acl'])) ? 1 : 0;
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02001349 $_data['pw_reset'] = (in_array('pw_reset', $_data['acl'])) ? 1 : 0;
Matthias Andreas Benkard9dabdab2024-03-06 13:12:09 +01001350 } else {
1351 $_data['spam_alias'] = intval($MAILBOX_DEFAULT_ATTRIBUTES['acl_spam_alias']);
1352 $_data['tls_policy'] = intval($MAILBOX_DEFAULT_ATTRIBUTES['acl_tls_policy']);
1353 $_data['spam_score'] = intval($MAILBOX_DEFAULT_ATTRIBUTES['acl_spam_score']);
1354 $_data['spam_policy'] = intval($MAILBOX_DEFAULT_ATTRIBUTES['acl_spam_policy']);
1355 $_data['delimiter_action'] = intval($MAILBOX_DEFAULT_ATTRIBUTES['acl_delimiter_action']);
1356 $_data['syncjobs'] = intval($MAILBOX_DEFAULT_ATTRIBUTES['acl_syncjobs']);
1357 $_data['eas_reset'] = intval($MAILBOX_DEFAULT_ATTRIBUTES['acl_eas_reset']);
1358 $_data['sogo_profile_reset'] = intval($MAILBOX_DEFAULT_ATTRIBUTES['acl_sogo_profile_reset']);
1359 $_data['pushover'] = intval($MAILBOX_DEFAULT_ATTRIBUTES['acl_pushover']);
1360 $_data['quarantine'] = intval($MAILBOX_DEFAULT_ATTRIBUTES['acl_quarantine']);
1361 $_data['quarantine_attachments'] = intval($MAILBOX_DEFAULT_ATTRIBUTES['acl_quarantine_attachments']);
1362 $_data['quarantine_notification'] = intval($MAILBOX_DEFAULT_ATTRIBUTES['acl_quarantine_notification']);
1363 $_data['quarantine_category'] = intval($MAILBOX_DEFAULT_ATTRIBUTES['acl_quarantine_category']);
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02001364 $_data['app_passwds'] = intval($MAILBOX_DEFAULT_ATTRIBUTES['acl_app_passwds']);
1365 $_data['pw_reset'] = intval($MAILBOX_DEFAULT_ATTRIBUTES['acl_pw_reset']);
Matthias Andreas Benkard9dabdab2024-03-06 13:12:09 +01001366 }
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +01001367
Matthias Andreas Benkard9dabdab2024-03-06 13:12:09 +01001368 try {
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02001369 $stmt = $pdo->prepare("INSERT INTO `user_acl`
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +01001370 (`username`, `spam_alias`, `tls_policy`, `spam_score`, `spam_policy`, `delimiter_action`, `syncjobs`, `eas_reset`, `sogo_profile_reset`,
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02001371 `pushover`, `quarantine`, `quarantine_attachments`, `quarantine_notification`, `quarantine_category`, `app_passwds`, `pw_reset`)
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +01001372 VALUES (:username, :spam_alias, :tls_policy, :spam_score, :spam_policy, :delimiter_action, :syncjobs, :eas_reset, :sogo_profile_reset,
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02001373 :pushover, :quarantine, :quarantine_attachments, :quarantine_notification, :quarantine_category, :app_passwds, :pw_reset) ");
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +01001374 $stmt->execute(array(
1375 ':username' => $username,
1376 ':spam_alias' => $_data['spam_alias'],
1377 ':tls_policy' => $_data['tls_policy'],
1378 ':spam_score' => $_data['spam_score'],
1379 ':spam_policy' => $_data['spam_policy'],
1380 ':delimiter_action' => $_data['delimiter_action'],
1381 ':syncjobs' => $_data['syncjobs'],
1382 ':eas_reset' => $_data['eas_reset'],
1383 ':sogo_profile_reset' => $_data['sogo_profile_reset'],
1384 ':pushover' => $_data['pushover'],
1385 ':quarantine' => $_data['quarantine'],
1386 ':quarantine_attachments' => $_data['quarantine_attachments'],
1387 ':quarantine_notification' => $_data['quarantine_notification'],
1388 ':quarantine_category' => $_data['quarantine_category'],
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02001389 ':app_passwds' => $_data['app_passwds'],
1390 ':pw_reset' => $_data['pw_reset']
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +01001391 ));
1392 }
Matthias Andreas Benkard9dabdab2024-03-06 13:12:09 +01001393 catch (PDOException $e) {
1394 $_SESSION['return'][] = array(
1395 'type' => 'danger',
1396 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1397 'msg' => $e->getMessage()
1398 );
1399 return false;
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +01001400 }
1401
Matthias Andreas Benkard9dabdab2024-03-06 13:12:09 +01001402 $_data['rl_frame'] = (isset($_data['rl_frame'])) ? $_data['rl_frame'] : $MAILBOX_DEFAULT_ATTRIBUTES['rl_frame'];
1403 $_data['rl_value'] = (isset($_data['rl_value'])) ? $_data['rl_value'] : $MAILBOX_DEFAULT_ATTRIBUTES['rl_value'];
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +01001404 if (isset($_data['rl_frame']) && isset($_data['rl_value'])){
1405 ratelimit('edit', 'mailbox', array(
1406 'object' => $username,
1407 'rl_frame' => $_data['rl_frame'],
1408 'rl_value' => $_data['rl_value']
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02001409 ), $_extra);
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +01001410 }
1411
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02001412 // Track affected mailboxes for SOGo update
1413 $update_sogo_mailboxes[] = $username;
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01001414 $_SESSION['return'][] = array(
1415 'type' => 'success',
1416 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1417 'msg' => array('mailbox_added', htmlspecialchars($username))
1418 );
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02001419 break;
1420 case 'mailbox_from_template':
1421 $stmt = $pdo->prepare("SELECT * FROM `templates`
1422 WHERE `template` = :template AND type = 'mailbox'");
1423 $stmt->execute(array(
1424 ":template" => $_data['template']
1425 ));
1426 $mbox_template_data = $stmt->fetch(PDO::FETCH_ASSOC);
1427 if (empty($mbox_template_data)){
1428 $_SESSION['return'][] = array(
1429 'type' => 'danger',
1430 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1431 'msg' => 'template_missing'
1432 );
1433 return false;
1434 }
1435
1436 $attribute_hash = sha1(json_encode($mbox_template_data["attributes"]));
1437 $mbox_template_data = json_decode($mbox_template_data["attributes"], true);
1438 $mbox_template_data['domain'] = $_data['domain'];
1439 $mbox_template_data['name'] = $_data['name'];
1440 $mbox_template_data['local_part'] = $_data['local_part'];
1441 $mbox_template_data['authsource'] = $_data['authsource'];
1442 $mbox_template_data['attribute_hash'] = $attribute_hash;
1443 $mbox_template_data['quota'] = intval($mbox_template_data['quota'] / 1048576);
1444
1445 $mailbox_attributes = array('acl' => array());
1446 foreach ($mbox_template_data as $key => $value){
1447 switch (true) {
1448 case (strpos($key, 'acl_') === 0 && $value != 0):
1449 array_push($mailbox_attributes['acl'], str_replace('acl_' , '', $key));
1450 break;
1451 default:
1452 $mailbox_attributes[$key] = $value;
1453 break;
1454 }
1455 }
1456
1457 return mailbox('add', 'mailbox', $mailbox_attributes);
1458 break;
1459 case 'mta_sts':
1460 $domain = idn_to_ascii(strtolower(trim($_data['domain'])), 0, INTL_IDNA_VARIANT_UTS46);
1461 $version = strtolower($_data['version']);
1462 $mode = strtolower($_data['mode']);
1463 $mx = explode(",", preg_replace('/\s+/', '', $_data['mx']));
1464 $max_age = intval($_data['max_age']);
1465 $active = (intval($_data['active']) == 1) ? 1 : 0;
1466 $id = date('YmdHis');
1467
1468 if (!hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $domain)) {
1469 $_SESSION['return'][] = array(
1470 'type' => 'danger',
1471 'log' => array(__FUNCTION__, $_action, $_type, $_data, $_attr),
1472 'msg' => 'access_denied'
1473 );
1474 return false;
1475 }
1476 if (empty($version) || !in_array($version, array('stsv1'))) {
1477 $_SESSION['return'][] = array(
1478 'type' => 'danger',
1479 'log' => array(__FUNCTION__, $_action, $_type, $_data, $_attr),
1480 'msg' => array('version_invalid', htmlspecialchars($domain))
1481 );
1482 return false;
1483 }
1484 if (empty($mode) || !in_array($mode, array('enforce', 'testing', 'none'))) {
1485 $_SESSION['return'][] = array(
1486 'type' => 'danger',
1487 'log' => array(__FUNCTION__, $_action, $_type, $_data, $_attr),
1488 'msg' => array('mode_invalid', htmlspecialchars($domain))
1489 );
1490 return false;
1491 }
1492 if (empty($max_age) || $max_age < 0 || $max_age > 31536000) {
1493 $_SESSION['return'][] = array(
1494 'type' => 'danger',
1495 'log' => array(__FUNCTION__, $_action, $_type, $_data, $_attr),
1496 'msg' => array('max_age_invalid', htmlspecialchars($domain))
1497 );
1498 return false;
1499 }
1500 foreach ($mx as $index => $mx_domain) {
1501 $mx_domain = idn_to_ascii(strtolower(trim($mx_domain)), 0, INTL_IDNA_VARIANT_UTS46);
1502 if (!is_valid_domain_name($mx_domain, array('allow_wildcard' => true))) {
1503 $_SESSION['return'][] = array(
1504 'type' => 'danger',
1505 'log' => array(__FUNCTION__, $_action, $_type, $_data, $_attr),
1506 'msg' => array('mx_invalid', htmlspecialchars($mx_domain))
1507 );
1508 return false;
1509 }
1510 }
1511
1512 try {
1513 $stmt = $pdo->prepare("INSERT INTO `mta_sts` (`id`, `domain`, `version`, `mode`, `mx`, `max_age`, `active`)
1514 VALUES (:id, :domain, :version, :mode, :mx, :max_age, :active)");
1515 $stmt->execute(array(
1516 ':id' => $id,
1517 ':domain' => $domain,
1518 ':version' => $version,
1519 ':mode' => $mode,
1520 ':mx' => implode(",", $mx),
1521 ':max_age' => $max_age,
1522 ':active' => $active
1523 ));
1524 } catch (PDOException $e) {
1525 $_SESSION['return'][] = array(
1526 'type' => 'danger',
1527 'log' => array(__FUNCTION__, $_action, $_type, $_data),
1528 'msg' => $e->getMessage()
1529 );
1530 return false;
1531 }
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01001532 break;
1533 case 'resource':
1534 $domain = idn_to_ascii(strtolower(trim($_data['domain'])), 0, INTL_IDNA_VARIANT_UTS46);
1535 $description = $_data['description'];
1536 $local_part = preg_replace('/[^\da-z]/i', '', preg_quote($description, '/'));
1537 $name = $local_part . '@' . $domain;
1538 $kind = $_data['kind'];
1539 $multiple_bookings = intval($_data['multiple_bookings']);
1540 $active = intval($_data['active']);
1541 if (!filter_var($name, FILTER_VALIDATE_EMAIL)) {
1542 $_SESSION['return'][] = array(
1543 'type' => 'danger',
1544 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1545 'msg' => 'resource_invalid'
1546 );
1547 return false;
1548 }
1549 if (empty($description)) {
1550 $_SESSION['return'][] = array(
1551 'type' => 'danger',
1552 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1553 'msg' => 'description_invalid'
1554 );
1555 return false;
1556 }
1557 if (!isset($multiple_bookings) || $multiple_bookings < -1) {
1558 $multiple_bookings = -1;
1559 }
1560 if ($kind != 'location' && $kind != 'group' && $kind != 'thing') {
1561 $_SESSION['return'][] = array(
1562 'type' => 'danger',
1563 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1564 'msg' => 'resource_invalid'
1565 );
1566 return false;
1567 }
1568 if (!is_valid_domain_name($domain)) {
1569 $_SESSION['return'][] = array(
1570 'type' => 'danger',
1571 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1572 'msg' => 'domain_invalid'
1573 );
1574 return false;
1575 }
1576 if (!hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $domain)) {
1577 $_SESSION['return'][] = array(
1578 'type' => 'danger',
1579 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1580 'msg' => 'access_denied'
1581 );
1582 return false;
1583 }
1584 $stmt = $pdo->prepare("SELECT `username` FROM `mailbox` WHERE `username` = :name");
1585 $stmt->execute(array(':name' => $name));
1586 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
1587 if ($num_results != 0) {
1588 $_SESSION['return'][] = array(
1589 'type' => 'danger',
1590 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1591 'msg' => array('object_exists', htmlspecialchars($name))
1592 );
1593 return false;
1594 }
1595 $stmt = $pdo->prepare("SELECT `address` FROM `alias` WHERE address= :name");
1596 $stmt->execute(array(':name' => $name));
1597 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
1598 if ($num_results != 0) {
1599 $_SESSION['return'][] = array(
1600 'type' => 'danger',
1601 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1602 'msg' => array('is_alias', htmlspecialchars($name))
1603 );
1604 return false;
1605 }
1606 $stmt = $pdo->prepare("SELECT `address` FROM `spamalias` WHERE `address`= :name");
1607 $stmt->execute(array(':name' => $name));
1608 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
1609 if ($num_results != 0) {
1610 $_SESSION['return'][] = array(
1611 'type' => 'danger',
1612 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1613 'msg' => array('is_spam_alias', htmlspecialchars($name))
1614 );
1615 return false;
1616 }
1617 $stmt = $pdo->prepare("SELECT `domain` FROM `domain` WHERE `domain`= :domain");
1618 $stmt->execute(array(':domain' => $domain));
1619 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
1620 if ($num_results == 0) {
1621 $_SESSION['return'][] = array(
1622 'type' => 'danger',
1623 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1624 'msg' => array('domain_not_found', htmlspecialchars($domain))
1625 );
1626 return false;
1627 }
1628 $stmt = $pdo->prepare("INSERT INTO `mailbox` (`username`, `password`, `name`, `quota`, `local_part`, `domain`, `active`, `multiple_bookings`, `kind`)
1629 VALUES (:name, 'RESOURCE', :description, 0, :local_part, :domain, :active, :multiple_bookings, :kind)");
1630 $stmt->execute(array(
1631 ':name' => $name,
1632 ':description' => $description,
1633 ':local_part' => $local_part,
1634 ':domain' => $domain,
1635 ':active' => $active,
1636 ':kind' => $kind,
1637 ':multiple_bookings' => $multiple_bookings
1638 ));
1639 $_SESSION['return'][] = array(
1640 'type' => 'success',
1641 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1642 'msg' => array('resource_added', htmlspecialchars($name))
1643 );
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02001644
1645 // Track affected mailboxes for SOGo update
1646 $update_sogo_mailboxes[] = $name;
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01001647 break;
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +01001648 case 'domain_templates':
1649 if ($_SESSION['mailcow_cc_role'] != "admin") {
1650 $_SESSION['return'][] = array(
1651 'type' => 'danger',
1652 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_extra),
1653 'msg' => 'access_denied'
1654 );
1655 return false;
1656 }
1657 if (empty($_data["template"])){
1658 $_SESSION['return'][] = array(
1659 'type' => 'danger',
1660 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_extra),
1661 'msg' => 'template_name_invalid'
1662 );
1663 return false;
1664 }
1665
1666 // check if template name exists, return false
1667 $stmt = $pdo->prepare("SELECT id FROM `templates` WHERE `type` = :type AND `template` = :template");
1668 $stmt->execute(array(
1669 ":type" => "domain",
1670 ":template" => $_data["template"]
1671 ));
1672 $row = $stmt->fetch(PDO::FETCH_ASSOC);
1673
1674 if (!empty($row)){
1675 $_SESSION['return'][] = array(
1676 'type' => 'danger',
1677 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_extra),
1678 'msg' => array('template_exists', $_data["template"])
1679 );
1680 return false;
1681 }
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02001682
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +01001683 // check attributes
1684 $attr = array();
1685 $attr['tags'] = (isset($_data['tags'])) ? $_data['tags'] : array();
1686 $attr['max_num_aliases_for_domain'] = (!empty($_data['max_num_aliases_for_domain'])) ? intval($_data['max_num_aliases_for_domain']) : 400;
1687 $attr['max_num_mboxes_for_domain'] = (!empty($_data['max_num_mboxes_for_domain'])) ? intval($_data['max_num_mboxes_for_domain']) : 10;
1688 $attr['def_quota_for_mbox'] = (!empty($_data['def_quota_for_mbox'])) ? intval($_data['def_quota_for_mbox']) * 1048576 : 3072 * 1048576;
1689 $attr['max_quota_for_mbox'] = (!empty($_data['max_quota_for_mbox'])) ? intval($_data['max_quota_for_mbox']) * 1048576 : 10240 * 1048576;
1690 $attr['max_quota_for_domain'] = (!empty($_data['max_quota_for_domain'])) ? intval($_data['max_quota_for_domain']) * 1048576 : 10240 * 1048576;
1691 $attr['rl_frame'] = (!empty($_data['rl_frame'])) ? $_data['rl_frame'] : "s";
1692 $attr['rl_value'] = (!empty($_data['rl_value'])) ? $_data['rl_value'] : "";
1693 $attr['active'] = isset($_data['active']) ? intval($_data['active']) : 1;
1694 $attr['gal'] = (isset($_data['gal'])) ? intval($_data['gal']) : 1;
1695 $attr['backupmx'] = (isset($_data['backupmx'])) ? intval($_data['backupmx']) : 0;
1696 $attr['relay_all_recipients'] = (isset($_data['relay_all_recipients'])) ? intval($_data['relay_all_recipients']) : 0;
1697 $attr['relay_unknown_only'] = (isset($_data['relay_unknown_only'])) ? intval($_data['relay_unknown_only']) : 0;
1698 $attr['dkim_selector'] = (isset($_data['dkim_selector'])) ? $_data['dkim_selector'] : "dkim";
1699 $attr['key_size'] = isset($_data['key_size']) ? intval($_data['key_size']) : 2048;
1700
1701 // save template
1702 $stmt = $pdo->prepare("INSERT INTO `templates` (`type`, `template`, `attributes`)
1703 VALUES (:type, :template, :attributes)");
1704 $stmt->execute(array(
1705 ":type" => "domain",
1706 ":template" => $_data["template"],
1707 ":attributes" => json_encode($attr)
1708 ));
1709
1710 // success
1711 $_SESSION['return'][] = array(
1712 'type' => 'success',
1713 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1714 'msg' => array('template_added', $_data["template"])
1715 );
1716 return true;
1717 break;
1718 case 'mailbox_templates':
1719 if ($_SESSION['mailcow_cc_role'] != "admin") {
1720 $_SESSION['return'][] = array(
1721 'type' => 'danger',
1722 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_extra),
1723 'msg' => 'access_denied'
1724 );
1725 return false;
1726 }
1727 if (empty($_data["template"])){
1728 $_SESSION['return'][] = array(
1729 'type' => 'danger',
1730 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_extra),
1731 'msg' => 'template_name_invalid'
1732 );
1733 return false;
1734 }
1735
1736 // check if template name exists, return false
1737 $stmt = $pdo->prepare("SELECT id FROM `templates` WHERE `type` = :type AND `template` = :template");
1738 $stmt->execute(array(
1739 ":type" => "mailbox",
1740 ":template" => $_data["template"]
1741 ));
1742 $row = $stmt->fetch(PDO::FETCH_ASSOC);
1743 if (!empty($row)){
1744 $_SESSION['return'][] = array(
1745 'type' => 'danger',
1746 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_extra),
1747 'msg' => array('template_exists', $_data["template"])
1748 );
1749 return false;
1750 }
1751
1752
1753 // check attributes
1754 $attr = array();
1755 $attr["quota"] = isset($_data['quota']) ? intval($_data['quota']) * 1048576 : 0;
1756 $attr['tags'] = (isset($_data['tags'])) ? $_data['tags'] : array();
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02001757 $attr["tagged_mail_handler"] = (!empty($_data['tagged_mail_handler'])) ? $_data['tagged_mail_handler'] : strval($MAILBOX_DEFAULT_ATTRIBUTES['tagged_mail_handler']);
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +01001758 $attr["quarantine_notification"] = (!empty($_data['quarantine_notification'])) ? $_data['quarantine_notification'] : strval($MAILBOX_DEFAULT_ATTRIBUTES['quarantine_notification']);
1759 $attr["quarantine_category"] = (!empty($_data['quarantine_category'])) ? $_data['quarantine_category'] : strval($MAILBOX_DEFAULT_ATTRIBUTES['quarantine_category']);
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02001760 // Validate quarantine_category
1761 if (!in_array($attr["quarantine_category"], array('add_header', 'reject', 'all'))) {
1762 $_SESSION['return'][] = array(
1763 'type' => 'danger',
1764 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_extra),
1765 'msg' => 'quarantine_category_invalid'
1766 );
1767 return false;
1768 }
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +01001769 $attr["rl_frame"] = (!empty($_data['rl_frame'])) ? $_data['rl_frame'] : "s";
1770 $attr["rl_value"] = (!empty($_data['rl_value'])) ? $_data['rl_value'] : "";
1771 $attr["force_pw_update"] = isset($_data['force_pw_update']) ? intval($_data['force_pw_update']) : intval($MAILBOX_DEFAULT_ATTRIBUTES['force_pw_update']);
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02001772 $attr["force_tfa"] = isset($_data['force_tfa']) ? intval($_data['force_tfa']) : intval($MAILBOX_DEFAULT_ATTRIBUTES['force_tfa']);
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +01001773 $attr["sogo_access"] = isset($_data['sogo_access']) ? intval($_data['sogo_access']) : intval($MAILBOX_DEFAULT_ATTRIBUTES['sogo_access']);
1774 $attr["active"] = isset($_data['active']) ? intval($_data['active']) : 1;
1775 $attr["tls_enforce_in"] = isset($_data['tls_enforce_in']) ? intval($_data['tls_enforce_in']) : intval($MAILBOX_DEFAULT_ATTRIBUTES['tls_enforce_in']);
1776 $attr["tls_enforce_out"] = isset($_data['tls_enforce_out']) ? intval($_data['tls_enforce_out']) : intval($MAILBOX_DEFAULT_ATTRIBUTES['tls_enforce_out']);
1777 if (isset($_data['protocol_access'])) {
1778 $_data['protocol_access'] = (array)$_data['protocol_access'];
Matthias Andreas Benkard9dabdab2024-03-06 13:12:09 +01001779 $attr['imap_access'] = (in_array('imap', $_data['protocol_access'])) ? 1 : 0;
1780 $attr['pop3_access'] = (in_array('pop3', $_data['protocol_access'])) ? 1 : 0;
1781 $attr['smtp_access'] = (in_array('smtp', $_data['protocol_access'])) ? 1 : 0;
1782 $attr['sieve_access'] = (in_array('sieve', $_data['protocol_access'])) ? 1 : 0;
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02001783 $attr['eas_access'] = (in_array('eas', $_data['protocol_access'])) ? 1 : 0;
1784 $attr['dav_access'] = (in_array('dav', $_data['protocol_access'])) ? 1 : 0;
1785 }
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +01001786 else {
1787 $attr['imap_access'] = intval($MAILBOX_DEFAULT_ATTRIBUTES['imap_access']);
1788 $attr['pop3_access'] = intval($MAILBOX_DEFAULT_ATTRIBUTES['pop3_access']);
1789 $attr['smtp_access'] = intval($MAILBOX_DEFAULT_ATTRIBUTES['smtp_access']);
1790 $attr['sieve_access'] = intval($MAILBOX_DEFAULT_ATTRIBUTES['sieve_access']);
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02001791 $attr['eas_access'] = intval($MAILBOX_DEFAULT_ATTRIBUTES['eas_access']);
1792 $attr['dav_access'] = intval($MAILBOX_DEFAULT_ATTRIBUTES['dav_access']);
Matthias Andreas Benkard9dabdab2024-03-06 13:12:09 +01001793 }
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +01001794 if (isset($_data['acl'])) {
1795 $_data['acl'] = (array)$_data['acl'];
1796 $attr['acl_spam_alias'] = (in_array('spam_alias', $_data['acl'])) ? 1 : 0;
1797 $attr['acl_tls_policy'] = (in_array('tls_policy', $_data['acl'])) ? 1 : 0;
1798 $attr['acl_spam_score'] = (in_array('spam_score', $_data['acl'])) ? 1 : 0;
1799 $attr['acl_spam_policy'] = (in_array('spam_policy', $_data['acl'])) ? 1 : 0;
1800 $attr['acl_delimiter_action'] = (in_array('delimiter_action', $_data['acl'])) ? 1 : 0;
1801 $attr['acl_syncjobs'] = (in_array('syncjobs', $_data['acl'])) ? 1 : 0;
1802 $attr['acl_eas_reset'] = (in_array('eas_reset', $_data['acl'])) ? 1 : 0;
1803 $attr['acl_sogo_profile_reset'] = (in_array('sogo_profile_reset', $_data['acl'])) ? 1 : 0;
1804 $attr['acl_pushover'] = (in_array('pushover', $_data['acl'])) ? 1 : 0;
1805 $attr['acl_quarantine'] = (in_array('quarantine', $_data['acl'])) ? 1 : 0;
1806 $attr['acl_quarantine_attachments'] = (in_array('quarantine_attachments', $_data['acl'])) ? 1 : 0;
1807 $attr['acl_quarantine_notification'] = (in_array('quarantine_notification', $_data['acl'])) ? 1 : 0;
1808 $attr['acl_quarantine_category'] = (in_array('quarantine_category', $_data['acl'])) ? 1 : 0;
1809 $attr['acl_app_passwds'] = (in_array('app_passwds', $_data['acl'])) ? 1 : 0;
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02001810 $attr['acl_pw_reset'] = (in_array('pw_reset', $_data['acl'])) ? 1 : 0;
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +01001811 } else {
1812 $_data['acl'] = (array)$_data['acl'];
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +01001813 $attr['acl_spam_alias'] = 0;
1814 $attr['acl_tls_policy'] = 0;
1815 $attr['acl_spam_score'] = 0;
1816 $attr['acl_spam_policy'] = 0;
1817 $attr['acl_delimiter_action'] = 0;
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +01001818 $attr['acl_syncjobs'] = 0;
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +01001819 $attr['acl_eas_reset'] = 0;
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +01001820 $attr['acl_sogo_profile_reset'] = 0;
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +01001821 $attr['acl_pushover'] = 0;
1822 $attr['acl_quarantine'] = 0;
1823 $attr['acl_quarantine_attachments'] = 0;
1824 $attr['acl_quarantine_notification'] = 0;
1825 $attr['acl_quarantine_category'] = 0;
1826 $attr['acl_app_passwds'] = 0;
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +01001827 }
1828
1829
1830
1831 // save template
1832 $stmt = $pdo->prepare("INSERT INTO `templates` (`type`, `template`, `attributes`)
1833 VALUES (:type, :template, :attributes)");
1834 $stmt->execute(array(
1835 ":type" => "mailbox",
1836 ":template" => $_data["template"],
1837 ":attributes" => json_encode($attr)
1838 ));
1839
1840 // success
1841 $_SESSION['return'][] = array(
1842 'type' => 'success',
1843 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1844 'msg' => array('template_added', $_data["template"])
1845 );
1846 return true;
1847 break;
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01001848 }
1849 break;
1850 case 'edit':
1851 switch ($_type) {
1852 case 'alias_domain':
1853 $alias_domains = (array)$_data['alias_domain'];
1854 foreach ($alias_domains as $alias_domain) {
1855 $alias_domain = idn_to_ascii(strtolower(trim($alias_domain)), 0, INTL_IDNA_VARIANT_UTS46);
1856 $is_now = mailbox('get', 'alias_domain_details', $alias_domain);
1857 if (!empty($is_now)) {
1858 $active = (isset($_data['active'])) ? intval($_data['active']) : $is_now['active'];
1859 $target_domain = (!empty($_data['target_domain'])) ? idn_to_ascii(strtolower(trim($_data['target_domain'])), 0, INTL_IDNA_VARIANT_UTS46) : $is_now['target_domain'];
1860 }
1861 else {
1862 $_SESSION['return'][] = array(
1863 'type' => 'danger',
1864 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1865 'msg' => array('alias_domain_invalid', htmlspecialchars($alias_domain))
1866 );
1867 continue;
1868 }
1869 if (!is_valid_domain_name($target_domain)) {
1870 $_SESSION['return'][] = array(
1871 'type' => 'danger',
1872 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1873 'msg' => array('target_domain_invalid', htmlspecialchars($target_domain))
1874 );
1875 continue;
1876 }
1877 if (!hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $target_domain)) {
1878 $_SESSION['return'][] = array(
1879 'type' => 'danger',
1880 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1881 'msg' => 'access_denied'
1882 );
1883 continue;
1884 }
1885 if (empty(mailbox('get', 'domain_details', $target_domain)) || !empty(mailbox('get', 'alias_domain_details', $target_domain))) {
1886 $_SESSION['return'][] = array(
1887 'type' => 'danger',
1888 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1889 'msg' => array('target_domain_invalid', htmlspecialchars($target_domain))
1890 );
1891 continue;
1892 }
1893 $stmt = $pdo->prepare("UPDATE `alias_domain` SET
1894 `target_domain` = :target_domain,
1895 `active` = :active
1896 WHERE `alias_domain` = :alias_domain");
1897 $stmt->execute(array(
1898 ':alias_domain' => $alias_domain,
1899 ':target_domain' => $target_domain,
1900 ':active' => $active
1901 ));
1902 $_SESSION['return'][] = array(
1903 'type' => 'success',
1904 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1905 'msg' => array('aliasd_modified', htmlspecialchars($alias_domain))
1906 );
1907 }
1908 break;
1909 case 'tls_policy':
1910 if (!is_array($_data['username'])) {
1911 $usernames = array();
1912 $usernames[] = $_data['username'];
1913 }
1914 else {
1915 $usernames = $_data['username'];
1916 }
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02001917 if (!hasACLAccess("tls_policy")) {
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01001918 $_SESSION['return'][] = array(
1919 'type' => 'danger',
1920 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1921 'msg' => 'access_denied'
1922 );
1923 return false;
1924 }
1925 foreach ($usernames as $username) {
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02001926 if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $username)) {
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01001927 $_SESSION['return'][] = array(
1928 'type' => 'danger',
1929 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1930 'msg' => 'access_denied'
1931 );
1932 continue;
1933 }
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02001934 $is_now = mailbox('get', 'tls_policy', $username, $_extra);
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01001935 if (!empty($is_now)) {
1936 $tls_enforce_in = (isset($_data['tls_enforce_in'])) ? intval($_data['tls_enforce_in']) : $is_now['tls_enforce_in'];
1937 $tls_enforce_out = (isset($_data['tls_enforce_out'])) ? intval($_data['tls_enforce_out']) : $is_now['tls_enforce_out'];
1938 }
1939 else {
1940 $_SESSION['return'][] = array(
1941 'type' => 'danger',
1942 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1943 'msg' => 'access_denied'
1944 );
1945 continue;
1946 }
1947 $stmt = $pdo->prepare("UPDATE `mailbox`
1948 SET `attributes` = JSON_SET(`attributes`, '$.tls_enforce_out', :tls_out),
1949 `attributes` = JSON_SET(`attributes`, '$.tls_enforce_in', :tls_in)
1950 WHERE `username` = :username");
1951 $stmt->execute(array(
1952 ':tls_out' => intval($tls_enforce_out),
1953 ':tls_in' => intval($tls_enforce_in),
1954 ':username' => $username
1955 ));
1956 $_SESSION['return'][] = array(
1957 'type' => 'success',
1958 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1959 'msg' => array('mailbox_modified', $username)
1960 );
1961 }
1962 break;
1963 case 'quarantine_notification':
1964 if (!is_array($_data['username'])) {
1965 $usernames = array();
1966 $usernames[] = $_data['username'];
1967 }
1968 else {
1969 $usernames = $_data['username'];
1970 }
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02001971 if (!hasACLAccess("quarantine_notification")) {
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01001972 $_SESSION['return'][] = array(
1973 'type' => 'danger',
1974 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1975 'msg' => 'access_denied'
1976 );
1977 return false;
1978 }
1979 foreach ($usernames as $username) {
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02001980 if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $username)) {
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01001981 $_SESSION['return'][] = array(
1982 'type' => 'danger',
1983 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1984 'msg' => 'access_denied'
1985 );
1986 continue;
1987 }
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02001988 $is_now = mailbox('get', 'quarantine_notification', $username, $_extra);
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01001989 if (!empty($is_now)) {
1990 $quarantine_notification = (isset($_data['quarantine_notification'])) ? $_data['quarantine_notification'] : $is_now['quarantine_notification'];
1991 }
1992 else {
1993 $_SESSION['return'][] = array(
1994 'type' => 'danger',
1995 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1996 'msg' => 'access_denied'
1997 );
1998 continue;
1999 }
2000 if (!in_array($quarantine_notification, array('never', 'hourly', 'daily', 'weekly'))) {
2001 $_SESSION['return'][] = array(
2002 'type' => 'danger',
2003 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2004 'msg' => 'access_denied'
2005 );
2006 continue;
2007 }
2008 $stmt = $pdo->prepare("UPDATE `mailbox`
2009 SET `attributes` = JSON_SET(`attributes`, '$.quarantine_notification', :quarantine_notification)
2010 WHERE `username` = :username");
2011 $stmt->execute(array(
2012 ':quarantine_notification' => $quarantine_notification,
2013 ':username' => $username
2014 ));
2015 $_SESSION['return'][] = array(
2016 'type' => 'success',
2017 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2018 'msg' => array('mailbox_modified', $username)
2019 );
2020 }
2021 break;
2022 case 'quarantine_category':
2023 if (!is_array($_data['username'])) {
2024 $usernames = array();
2025 $usernames[] = $_data['username'];
2026 }
2027 else {
2028 $usernames = $_data['username'];
2029 }
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02002030 if (!hasACLAccess("quarantine_category")) {
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01002031 $_SESSION['return'][] = array(
2032 'type' => 'danger',
2033 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2034 'msg' => 'access_denied'
2035 );
2036 return false;
2037 }
2038 foreach ($usernames as $username) {
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02002039 if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $username)) {
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01002040 $_SESSION['return'][] = array(
2041 'type' => 'danger',
2042 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2043 'msg' => 'access_denied'
2044 );
2045 continue;
2046 }
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02002047 $is_now = mailbox('get', 'quarantine_category', $username, $_extra);
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01002048 if (!empty($is_now)) {
2049 $quarantine_category = (isset($_data['quarantine_category'])) ? $_data['quarantine_category'] : $is_now['quarantine_category'];
2050 }
2051 else {
2052 $_SESSION['return'][] = array(
2053 'type' => 'danger',
2054 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2055 'msg' => 'access_denied'
2056 );
2057 continue;
2058 }
2059 if (!in_array($quarantine_category, array('add_header', 'reject', 'all'))) {
2060 $_SESSION['return'][] = array(
2061 'type' => 'danger',
2062 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2063 'msg' => 'access_denied'
2064 );
2065 continue;
2066 }
2067 $stmt = $pdo->prepare("UPDATE `mailbox`
2068 SET `attributes` = JSON_SET(`attributes`, '$.quarantine_category', :quarantine_category)
2069 WHERE `username` = :username");
2070 $stmt->execute(array(
2071 ':quarantine_category' => $quarantine_category,
2072 ':username' => $username
2073 ));
2074 $_SESSION['return'][] = array(
2075 'type' => 'success',
2076 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2077 'msg' => array('mailbox_modified', $username)
2078 );
2079 }
2080 break;
2081 case 'spam_score':
2082 if (!is_array($_data['username'])) {
2083 $usernames = array();
2084 $usernames[] = $_data['username'];
2085 }
2086 else {
2087 $usernames = $_data['username'];
2088 }
2089 if (!isset($_SESSION['acl']['spam_score']) || $_SESSION['acl']['spam_score'] != "1" ) {
2090 $_SESSION['return'][] = array(
2091 'type' => 'danger',
2092 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2093 'msg' => 'access_denied'
2094 );
2095 return false;
2096 }
2097 foreach ($usernames as $username) {
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02002098 if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $username)) {
2099 $_SESSION['return'][] = array(
2100 'type' => 'danger',
2101 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2102 'msg' => 'access_denied'
2103 );
2104 continue;
2105 }
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01002106 if ($_data['spam_score'] == "default") {
2107 $stmt = $pdo->prepare("DELETE FROM `filterconf` WHERE `object` = :username
2108 AND (`option` = 'lowspamlevel' OR `option` = 'highspamlevel')");
2109 $stmt->execute(array(
2110 ':username' => $username
2111 ));
2112 $_SESSION['return'][] = array(
2113 'type' => 'success',
2114 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2115 'msg' => array('mailbox_modified', $username)
2116 );
2117 continue;
2118 }
Matthias Andreas Benkard8f915942021-08-16 10:57:25 +02002119 $lowspamlevel = explode(',', $_data['spam_score'])[0];
2120 $highspamlevel = explode(',', $_data['spam_score'])[1];
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01002121 if (!is_numeric($lowspamlevel) || !is_numeric($highspamlevel)) {
2122 $_SESSION['return'][] = array(
2123 'type' => 'danger',
2124 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2125 'msg' => 'Invalid spam score, format must be "1,2" where first is low and second is high spam value.'
2126 );
2127 continue;
2128 }
Matthias Andreas Benkard8f915942021-08-16 10:57:25 +02002129 if ($lowspamlevel == $highspamlevel) {
2130 $highspamlevel = $highspamlevel + 0.1;
2131 }
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01002132 $stmt = $pdo->prepare("DELETE FROM `filterconf` WHERE `object` = :username
2133 AND (`option` = 'lowspamlevel' OR `option` = 'highspamlevel')");
2134 $stmt->execute(array(
2135 ':username' => $username
2136 ));
2137 $stmt = $pdo->prepare("INSERT INTO `filterconf` (`object`, `option`, `value`)
2138 VALUES (:username, 'highspamlevel', :highspamlevel)");
2139 $stmt->execute(array(
2140 ':username' => $username,
2141 ':highspamlevel' => $highspamlevel
2142 ));
2143 $stmt = $pdo->prepare("INSERT INTO `filterconf` (`object`, `option`, `value`)
2144 VALUES (:username, 'lowspamlevel', :lowspamlevel)");
2145 $stmt->execute(array(
2146 ':username' => $username,
2147 ':lowspamlevel' => $lowspamlevel
2148 ));
2149 $_SESSION['return'][] = array(
2150 'type' => 'success',
2151 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2152 'msg' => array('mailbox_modified', $username)
2153 );
2154 }
2155 break;
2156 case 'time_limited_alias':
2157 if (!isset($_SESSION['acl']['spam_alias']) || $_SESSION['acl']['spam_alias'] != "1" ) {
2158 $_SESSION['return'][] = array(
2159 'type' => 'danger',
2160 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2161 'msg' => 'access_denied'
2162 );
2163 return false;
2164 }
2165 if (!is_array($_data['address'])) {
2166 $addresses = array();
2167 $addresses[] = $_data['address'];
2168 }
2169 else {
2170 $addresses = $_data['address'];
2171 }
2172 foreach ($addresses as $address) {
2173 $stmt = $pdo->prepare("SELECT `goto` FROM `spamalias` WHERE `address` = :address");
2174 $stmt->execute(array(':address' => $address));
2175 $goto = $stmt->fetch(PDO::FETCH_ASSOC)['goto'];
2176 if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $goto)) {
2177 $_SESSION['return'][] = array(
2178 'type' => 'danger',
2179 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2180 'msg' => 'access_denied'
2181 );
2182 continue;
2183 }
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02002184 if (empty($_data['validity']) && empty($_data['permanent'])) {
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01002185 continue;
2186 }
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02002187 if (isset($_data['permanent']) && filter_var($_data['permanent'], FILTER_VALIDATE_BOOL)) {
2188 $permanent = 1;
2189 $validity = 0;
2190 }
2191 else if (isset($_data['validity'])) {
2192 $permanent = 0;
2193 $validity = round((int)time() + ($_data['validity'] * 3600));
2194 }
2195 $stmt = $pdo->prepare("UPDATE `spamalias` SET `validity` = :validity, `permanent` = :permanent WHERE
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01002196 `address` = :address");
2197 $stmt->execute(array(
2198 ':address' => $address,
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02002199 ':validity' => $validity,
2200 ':permanent' => $permanent
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01002201 ));
2202 $_SESSION['return'][] = array(
2203 'type' => 'success',
2204 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
Matthias Andreas Benkard8f915942021-08-16 10:57:25 +02002205 'msg' => array('mailbox_modified', htmlspecialchars(implode(', ', (array)$usernames)))
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01002206 );
2207 }
2208 break;
2209 case 'delimiter_action':
2210 if (!is_array($_data['username'])) {
2211 $usernames = array();
2212 $usernames[] = $_data['username'];
2213 }
2214 else {
2215 $usernames = $_data['username'];
2216 }
2217 if (!isset($_SESSION['acl']['delimiter_action']) || $_SESSION['acl']['delimiter_action'] != "1" ) {
2218 $_SESSION['return'][] = array(
2219 'type' => 'danger',
2220 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2221 'msg' => 'access_denied'
2222 );
2223 return false;
2224 }
2225 foreach ($usernames as $username) {
2226 if (!filter_var($username, FILTER_VALIDATE_EMAIL) || !hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $username)) {
2227 $_SESSION['return'][] = array(
2228 'type' => 'danger',
2229 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2230 'msg' => 'access_denied'
2231 );
2232 continue;
2233 }
2234 if (isset($_data['tagged_mail_handler']) && $_data['tagged_mail_handler'] == "subject") {
2235 try {
2236 $redis->hSet('RCPT_WANTS_SUBJECT_TAG', $username, 1);
2237 $redis->hDel('RCPT_WANTS_SUBFOLDER_TAG', $username);
2238 }
2239 catch (RedisException $e) {
2240 $_SESSION['return'][] = array(
2241 'type' => 'danger',
2242 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2243 'msg' => array('redis_error', $e)
2244 );
2245 continue;
2246 }
2247 }
2248 else if (isset($_data['tagged_mail_handler']) && $_data['tagged_mail_handler'] == "subfolder") {
2249 try {
2250 $redis->hSet('RCPT_WANTS_SUBFOLDER_TAG', $username, 1);
2251 $redis->hDel('RCPT_WANTS_SUBJECT_TAG', $username);
2252 }
2253 catch (RedisException $e) {
2254 $_SESSION['return'][] = array(
2255 'type' => 'danger',
2256 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2257 'msg' => array('redis_error', $e)
2258 );
2259 continue;
2260 }
2261 }
2262 else {
2263 try {
2264 $redis->hDel('RCPT_WANTS_SUBJECT_TAG', $username);
2265 $redis->hDel('RCPT_WANTS_SUBFOLDER_TAG', $username);
2266 }
2267 catch (RedisException $e) {
2268 $_SESSION['return'][] = array(
2269 'type' => 'danger',
2270 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2271 'msg' => array('redis_error', $e)
2272 );
2273 continue;
2274 }
2275 }
2276 $_SESSION['return'][] = array(
2277 'type' => 'success',
2278 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2279 'msg' => array('mailbox_modified', $username)
2280 );
2281 }
2282 break;
2283 case 'syncjob':
2284 if (!is_array($_data['id'])) {
2285 $ids = array();
2286 $ids[] = $_data['id'];
2287 }
2288 else {
2289 $ids = $_data['id'];
2290 }
2291 if (!isset($_SESSION['acl']['syncjobs']) || $_SESSION['acl']['syncjobs'] != "1" ) {
2292 $_SESSION['return'][] = array(
2293 'type' => 'danger',
2294 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2295 'msg' => 'access_denied'
2296 );
2297 return false;
2298 }
2299 foreach ($ids as $id) {
2300 $is_now = mailbox('get', 'syncjob_details', $id, array('with_password'));
2301 if (!empty($is_now)) {
2302 $username = $is_now['user2'];
2303 $user1 = (!empty($_data['user1'])) ? $_data['user1'] : $is_now['user1'];
2304 $active = (isset($_data['active'])) ? intval($_data['active']) : $is_now['active'];
2305 $last_run = (isset($_data['last_run'])) ? NULL : $is_now['last_run'];
Matthias Andreas Benkardb4b5cc12021-12-28 18:02:04 +01002306 $success = (isset($_data['success'])) ? NULL : $is_now['success'];
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01002307 $delete2duplicates = (isset($_data['delete2duplicates'])) ? intval($_data['delete2duplicates']) : $is_now['delete2duplicates'];
2308 $subscribeall = (isset($_data['subscribeall'])) ? intval($_data['subscribeall']) : $is_now['subscribeall'];
Matthias Andreas Benkard9dabdab2024-03-06 13:12:09 +01002309 $dry = (isset($_data['dry'])) ? intval($_data['dry']) : $is_now['dry'];
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01002310 $delete1 = (isset($_data['delete1'])) ? intval($_data['delete1']) : $is_now['delete1'];
2311 $delete2 = (isset($_data['delete2'])) ? intval($_data['delete2']) : $is_now['delete2'];
2312 $automap = (isset($_data['automap'])) ? intval($_data['automap']) : $is_now['automap'];
2313 $skipcrossduplicates = (isset($_data['skipcrossduplicates'])) ? intval($_data['skipcrossduplicates']) : $is_now['skipcrossduplicates'];
2314 $port1 = (!empty($_data['port1'])) ? $_data['port1'] : $is_now['port1'];
2315 $password1 = (!empty($_data['password1'])) ? $_data['password1'] : $is_now['password1'];
2316 $host1 = (!empty($_data['host1'])) ? $_data['host1'] : $is_now['host1'];
2317 $subfolder2 = (isset($_data['subfolder2'])) ? $_data['subfolder2'] : $is_now['subfolder2'];
2318 $enc1 = (!empty($_data['enc1'])) ? $_data['enc1'] : $is_now['enc1'];
2319 $mins_interval = (!empty($_data['mins_interval'])) ? $_data['mins_interval'] : $is_now['mins_interval'];
2320 $exclude = (isset($_data['exclude'])) ? $_data['exclude'] : $is_now['exclude'];
2321 $custom_params = (isset($_data['custom_params'])) ? $_data['custom_params'] : $is_now['custom_params'];
2322 $maxage = (isset($_data['maxage']) && $_data['maxage'] != "") ? intval($_data['maxage']) : $is_now['maxage'];
2323 $maxbytespersecond = (isset($_data['maxbytespersecond']) && $_data['maxbytespersecond'] != "") ? intval($_data['maxbytespersecond']) : $is_now['maxbytespersecond'];
2324 $timeout1 = (isset($_data['timeout1']) && $_data['timeout1'] != "") ? intval($_data['timeout1']) : $is_now['timeout1'];
2325 $timeout2 = (isset($_data['timeout2']) && $_data['timeout2'] != "") ? intval($_data['timeout2']) : $is_now['timeout2'];
2326 }
2327 else {
2328 $_SESSION['return'][] = array(
2329 'type' => 'danger',
2330 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2331 'msg' => 'access_denied'
2332 );
2333 continue;
2334 }
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +01002335
2336 // validate custom params
2337 foreach (explode('-', $custom_params) as $param){
2338 if(empty($param)) continue;
2339
2340 // extract option
2341 if (str_contains($param, '=')) $param = explode('=', $param)[0];
2342 else $param = rtrim($param, ' ');
2343 // remove first char if first char is -
2344 if ($param[0] == '-') $param = ltrim($param, $param[0]);
2345
2346 if (str_contains($param, ' ')) {
2347 // bad char
2348 $_SESSION['return'][] = array(
2349 'type' => 'danger',
2350 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2351 'msg' => 'bad character SPACE'
2352 );
2353 return false;
2354 }
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02002355
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +01002356 // check if param is whitelisted
2357 if (!in_array(strtolower($param), $GLOBALS["IMAPSYNC_OPTIONS"]["whitelist"])){
2358 // bad option
2359 $_SESSION['return'][] = array(
2360 'type' => 'danger',
2361 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2362 'msg' => 'bad option '. $param
2363 );
2364 return false;
2365 }
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01002366 }
2367 if (empty($subfolder2)) {
2368 $subfolder2 = "";
2369 }
2370 if (!isset($maxage) || !filter_var($maxage, FILTER_VALIDATE_INT, array('options' => array('min_range' => 1, 'max_range' => 32000)))) {
2371 $maxage = "0";
2372 }
2373 if (!isset($timeout1) || !filter_var($timeout1, FILTER_VALIDATE_INT, array('options' => array('min_range' => 1, 'max_range' => 32000)))) {
2374 $timeout1 = "600";
2375 }
2376 if (!isset($timeout2) || !filter_var($timeout2, FILTER_VALIDATE_INT, array('options' => array('min_range' => 1, 'max_range' => 32000)))) {
2377 $timeout2 = "600";
2378 }
2379 if (!isset($maxbytespersecond) || !filter_var($maxbytespersecond, FILTER_VALIDATE_INT, array('options' => array('min_range' => 1, 'max_range' => 125000000)))) {
2380 $maxbytespersecond = "0";
2381 }
2382 if (!filter_var($port1, FILTER_VALIDATE_INT, array('options' => array('min_range' => 1, 'max_range' => 65535)))) {
2383 $_SESSION['return'][] = array(
2384 'type' => 'danger',
2385 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2386 'msg' => 'access_denied'
2387 );
2388 continue;
2389 }
2390 if (!filter_var($mins_interval, FILTER_VALIDATE_INT, array('options' => array('min_range' => 1, 'max_range' => 43800)))) {
2391 $_SESSION['return'][] = array(
2392 'type' => 'danger',
2393 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2394 'msg' => 'access_denied'
2395 );
2396 continue;
2397 }
2398 if (!is_valid_domain_name($host1)) {
2399 $_SESSION['return'][] = array(
2400 'type' => 'danger',
2401 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2402 'msg' => 'access_denied'
2403 );
2404 continue;
2405 }
2406 if ($enc1 != "TLS" && $enc1 != "SSL" && $enc1 != "PLAIN") {
2407 $_SESSION['return'][] = array(
2408 'type' => 'danger',
2409 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2410 'msg' => 'access_denied'
2411 );
2412 continue;
2413 }
2414 if (@preg_match("/" . $exclude . "/", null) === false) {
2415 $_SESSION['return'][] = array(
2416 'type' => 'danger',
2417 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2418 'msg' => 'access_denied'
2419 );
2420 continue;
2421 }
2422 $stmt = $pdo->prepare("UPDATE `imapsync` SET `delete1` = :delete1,
2423 `delete2` = :delete2,
2424 `automap` = :automap,
2425 `skipcrossduplicates` = :skipcrossduplicates,
2426 `maxage` = :maxage,
2427 `maxbytespersecond` = :maxbytespersecond,
2428 `subfolder2` = :subfolder2,
2429 `exclude` = :exclude,
2430 `host1` = :host1,
2431 `last_run` = :last_run,
Matthias Andreas Benkardb4b5cc12021-12-28 18:02:04 +01002432 `success` = :success,
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01002433 `user1` = :user1,
2434 `password1` = :password1,
2435 `mins_interval` = :mins_interval,
2436 `port1` = :port1,
2437 `enc1` = :enc1,
2438 `delete2duplicates` = :delete2duplicates,
2439 `custom_params` = :custom_params,
2440 `timeout1` = :timeout1,
2441 `timeout2` = :timeout2,
2442 `subscribeall` = :subscribeall,
Matthias Andreas Benkard9dabdab2024-03-06 13:12:09 +01002443 `dry` = :dry,
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01002444 `active` = :active
2445 WHERE `id` = :id");
2446 $stmt->execute(array(
2447 ':delete1' => $delete1,
2448 ':delete2' => $delete2,
2449 ':automap' => $automap,
2450 ':skipcrossduplicates' => $skipcrossduplicates,
2451 ':id' => $id,
2452 ':exclude' => $exclude,
2453 ':maxage' => $maxage,
2454 ':maxbytespersecond' => $maxbytespersecond,
2455 ':subfolder2' => $subfolder2,
2456 ':host1' => $host1,
2457 ':user1' => $user1,
2458 ':password1' => $password1,
2459 ':last_run' => $last_run,
Matthias Andreas Benkardb4b5cc12021-12-28 18:02:04 +01002460 ':success' => $success,
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01002461 ':mins_interval' => $mins_interval,
2462 ':port1' => $port1,
2463 ':enc1' => $enc1,
2464 ':delete2duplicates' => $delete2duplicates,
2465 ':custom_params' => $custom_params,
2466 ':timeout1' => $timeout1,
2467 ':timeout2' => $timeout2,
2468 ':subscribeall' => $subscribeall,
Matthias Andreas Benkard9dabdab2024-03-06 13:12:09 +01002469 ':dry' => $dry,
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01002470 ':active' => $active,
2471 ));
2472 $_SESSION['return'][] = array(
2473 'type' => 'success',
2474 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2475 'msg' => array('mailbox_modified', $username)
2476 );
2477 }
2478 break;
2479 case 'filter':
2480 if (!isset($_SESSION['acl']['filters']) || $_SESSION['acl']['filters'] != "1" ) {
2481 $_SESSION['return'][] = array(
2482 'type' => 'danger',
2483 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2484 'msg' => 'access_denied'
2485 );
2486 return false;
2487 }
2488 $sieve = new Sieve\SieveParser();
2489 if (!is_array($_data['id'])) {
2490 $ids = array();
2491 $ids[] = $_data['id'];
2492 }
2493 else {
2494 $ids = $_data['id'];
2495 }
2496 foreach ($ids as $id) {
2497 $is_now = mailbox('get', 'filter_details', $id);
2498 if (!empty($is_now)) {
2499 $username = $is_now['username'];
2500 $active = (isset($_data['active'])) ? intval($_data['active']) : $is_now['active'];
2501 $script_desc = (!empty($_data['script_desc'])) ? $_data['script_desc'] : $is_now['script_desc'];
2502 $script_data = (!empty($_data['script_data'])) ? $_data['script_data'] : $is_now['script_data'];
2503 $filter_type = (!empty($_data['filter_type'])) ? $_data['filter_type'] : $is_now['filter_type'];
2504 }
2505 else {
2506 $_SESSION['return'][] = array(
2507 'type' => 'danger',
2508 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2509 'msg' => 'access_denied'
2510 );
2511 continue;
2512 }
2513 try {
2514 $sieve->parse($script_data);
2515 }
2516 catch (Exception $e) {
2517 $_SESSION['return'][] = array(
2518 'type' => 'danger',
2519 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2520 'msg' => array('sieve_error', $e->getMessage())
2521 );
2522 continue;
2523 }
2524 if ($filter_type != 'postfilter' && $filter_type != 'prefilter') {
2525 $_SESSION['return'][] = array(
2526 'type' => 'danger',
2527 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2528 'msg' => 'filter_type'
2529 );
2530 continue;
2531 }
2532 if ($active == '1') {
2533 $script_name = 'active';
2534 $stmt = $pdo->prepare("UPDATE `sieve_filters`
2535 SET `script_name` = 'inactive'
2536 WHERE `username` = :username
2537 AND `filter_type` = :filter_type");
2538 $stmt->execute(array(
2539 ':username' => $username,
2540 ':filter_type' => $filter_type
2541 ));
2542 }
2543 else {
2544 $script_name = 'inactive';
2545 }
2546 $stmt = $pdo->prepare("UPDATE `sieve_filters` SET `script_desc` = :script_desc, `script_data` = :script_data, `script_name` = :script_name, `filter_type` = :filter_type
2547 WHERE `id` = :id");
2548 $stmt->execute(array(
2549 ':script_desc' => $script_desc,
2550 ':script_data' => $script_data,
2551 ':script_name' => $script_name,
2552 ':filter_type' => $filter_type,
2553 ':id' => $id
2554 ));
2555 $_SESSION['return'][] = array(
2556 'type' => 'success',
2557 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2558 'msg' => array('mailbox_modified', $username)
2559 );
2560 }
2561 break;
2562 case 'alias':
2563 if (!is_array($_data['id'])) {
2564 $ids = array();
2565 $ids[] = $_data['id'];
2566 }
2567 else {
2568 $ids = $_data['id'];
2569 }
2570 foreach ($ids as $id) {
2571 $is_now = mailbox('get', 'alias_details', $id);
2572 if (!empty($is_now)) {
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02002573 $internal = (isset($_data['internal'])) ? intval($_data['internal']) : $is_now['internal'];
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01002574 $active = (isset($_data['active'])) ? intval($_data['active']) : $is_now['active'];
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02002575 $sender_allowed = (isset($_data['sender_allowed'])) ? intval($_data['sender_allowed']) : $is_now['sender_allowed'];
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01002576 $sogo_visible = (isset($_data['sogo_visible'])) ? intval($_data['sogo_visible']) : $is_now['sogo_visible'];
2577 $goto_null = (isset($_data['goto_null'])) ? intval($_data['goto_null']) : 0;
2578 $goto_spam = (isset($_data['goto_spam'])) ? intval($_data['goto_spam']) : 0;
2579 $goto_ham = (isset($_data['goto_ham'])) ? intval($_data['goto_ham']) : 0;
2580 $public_comment = (isset($_data['public_comment'])) ? $_data['public_comment'] : $is_now['public_comment'];
2581 $private_comment = (isset($_data['private_comment'])) ? $_data['private_comment'] : $is_now['private_comment'];
2582 $goto = (!empty($_data['goto'])) ? $_data['goto'] : $is_now['goto'];
2583 $address = (!empty($_data['address'])) ? $_data['address'] : $is_now['address'];
2584 }
2585 else {
2586 $_SESSION['return'][] = array(
2587 'type' => 'danger',
2588 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2589 'msg' => array('alias_invalid', $address)
2590 );
2591 continue;
2592 }
Matthias Andreas Benkard8f915942021-08-16 10:57:25 +02002593 if ($_data['expand_alias'] === true || $_data['expand_alias'] == 1) {
2594 $stmt = $pdo->prepare("SELECT `address` FROM `alias`
2595 WHERE `address` = :address
2596 AND `domain` NOT IN (
2597 SELECT `alias_domain` FROM `alias_domain`
2598 )");
2599 $stmt->execute(array(
2600 ':address' => $address,
2601 ));
2602 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
2603 if ($num_results == 0) {
2604 $_SESSION['return'][] = array(
2605 'type' => 'warning',
2606 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2607 'msg' => array('is_not_primary_alias', htmlspecialchars($address))
2608 );
2609 continue;
2610 }
2611 $stmt = $pdo->prepare("SELECT `goto`, GROUP_CONCAT(CONCAT(SUBSTRING(`alias`.`address`, 1, LOCATE('@', `alias`.`address`) - 1), '@', `alias_domain`.`alias_domain`)) AS `missing_alias`
2612 FROM `alias` JOIN `alias_domain` ON `alias_domain`.`target_domain` = `alias`.`domain`
2613 WHERE CONCAT(SUBSTRING(`alias`.`address`, 1, LOCATE('@', `alias`.`address`) - 1), '@', `alias_domain`.`alias_domain`) NOT IN (
2614 SELECT `address` FROM `alias` WHERE `address` != `goto`
2615 )
2616 AND `alias`.`address` NOT IN (
2617 SELECT `address` FROM `alias` WHERE `address` = `goto`
2618 )
2619 AND `address` = :address ;");
2620 $stmt->execute(array(
2621 ':address' => $address
2622 ));
2623 $missing_aliases = $stmt->fetch(PDO::FETCH_ASSOC);
2624 if (!empty($missing_aliases['missing_alias'])) {
2625 mailbox('add', 'alias', array(
2626 'address' => $missing_aliases['missing_alias'],
2627 'goto' => $missing_aliases['goto'],
2628 'sogo_visible' => 1,
2629 'active' => 1
2630 ));
2631 }
2632 $_SESSION['return'][] = array(
2633 'type' => 'success',
2634 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2635 'msg' => array('alias_modified', htmlspecialchars($address))
2636 );
2637 continue;
2638 }
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01002639 $domain = idn_to_ascii(substr(strstr($address, '@'), 1), 0, INTL_IDNA_VARIANT_UTS46);
2640 if ($is_now['address'] != $address) {
2641 $local_part = strstr($address, '@', true);
2642 $address = $local_part.'@'.$domain;
2643 if (!hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $domain)) {
2644 $_SESSION['return'][] = array(
2645 'type' => 'danger',
2646 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2647 'msg' => 'access_denied'
2648 );
2649 continue;
2650 }
2651 if ((!filter_var($address, FILTER_VALIDATE_EMAIL) === true) && !empty($local_part)) {
2652 $_SESSION['return'][] = array(
2653 'type' => 'danger',
2654 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2655 'msg' => array('alias_invalid', $address)
2656 );
2657 continue;
2658 }
2659 if (strtolower($is_now['address']) != strtolower($address)) {
2660 $stmt = $pdo->prepare("SELECT `address` FROM `alias`
2661 WHERE `address`= :address OR `address` IN (
2662 SELECT `username` FROM `mailbox`, `alias_domain`
2663 WHERE (
2664 `alias_domain`.`alias_domain` = :address_d
2665 AND `mailbox`.`username` = CONCAT(:address_l, '@', alias_domain.target_domain)))");
2666 $stmt->execute(array(
2667 ':address' => $address,
2668 ':address_l' => $local_part,
2669 ':address_d' => $domain
2670 ));
2671 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
2672 if ($num_results != 0) {
2673 $_SESSION['return'][] = array(
2674 'type' => 'danger',
2675 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2676 'msg' => array('is_alias_or_mailbox', htmlspecialchars($address))
2677 );
2678 continue;
2679 }
2680 }
2681 $stmt = $pdo->prepare("SELECT `domain` FROM `domain`
2682 WHERE `domain`= :domain1 OR `domain` = (SELECT `target_domain` FROM `alias_domain` WHERE `alias_domain` = :domain2)");
2683 $stmt->execute(array(':domain1' => $domain, ':domain2' => $domain));
2684 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
2685 if ($num_results == 0) {
2686 $_SESSION['return'][] = array(
2687 'type' => 'danger',
2688 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2689 'msg' => array('domain_not_found', htmlspecialchars($domain))
2690 );
2691 continue;
2692 }
2693 $stmt = $pdo->prepare("SELECT `address` FROM `spamalias`
2694 WHERE `address`= :address");
2695 $stmt->execute(array(':address' => $address));
2696 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
2697 if ($num_results != 0) {
2698 $_SESSION['return'][] = array(
2699 'type' => 'danger',
2700 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2701 'msg' => array('is_spam_alias', htmlspecialchars($address))
2702 );
2703 continue;
2704 }
2705 }
2706 if ($goto_null == "1") {
2707 $goto = "null@localhost";
2708 }
2709 elseif ($goto_spam == "1") {
2710 $goto = "spam@localhost";
2711 }
2712 elseif ($goto_ham == "1") {
2713 $goto = "ham@localhost";
2714 }
2715 else {
2716 $gotos = array_map('trim', preg_split( "/( |,|;|\n)/", $goto));
2717 foreach ($gotos as $i => &$goto) {
2718 if (empty($goto)) {
2719 continue;
2720 }
2721 if (!filter_var($goto, FILTER_VALIDATE_EMAIL)) {
2722 $_SESSION['return'][] = array(
2723 'type' => 'danger',
2724 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2725 'msg' => array('goto_invalid', $goto)
2726 );
2727 unset($gotos[$i]);
2728 continue;
2729 }
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02002730 // Deny external goto domains: global switch (all roles) overrides the per-DA ACL
2731 $goto_domain = idn_to_ascii(substr(strstr($goto, '@'), 1), 0, INTL_IDNA_VARIANT_UTS46);
2732 if (($GLOBALS['ALIAS_DISABLE_EXTERNAL_DOMAINS'] === true ||
2733 (isset($_SESSION['acl']['alias_external_goto']) && $_SESSION['acl']['alias_external_goto'] != "1")) &&
2734 !is_local_mailcow_domain($goto_domain)) {
2735 $_SESSION['return'][] = array(
2736 'type' => 'danger',
2737 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2738 'msg' => array('external_goto_denied', htmlspecialchars($goto))
2739 );
2740 unset($gotos[$i]);
2741 continue;
2742 }
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01002743 if ($goto == $address) {
2744 $_SESSION['return'][] = array(
2745 'type' => 'danger',
2746 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2747 'msg' => 'alias_goto_identical'
2748 );
2749 unset($gotos[$i]);
2750 continue;
2751 }
2752 // Delete from sender_acl to prevent duplicates
2753 $stmt = $pdo->prepare("DELETE FROM `sender_acl` WHERE
2754 `logged_in_as` = :goto AND
2755 `send_as` = :address");
2756 $stmt->execute(array(
2757 ':goto' => $goto,
2758 ':address' => $address
2759 ));
2760 }
Matthias Andreas Benkard8f915942021-08-16 10:57:25 +02002761 $gotos = array_unique($gotos);
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01002762 $gotos = array_filter($gotos);
Matthias Andreas Benkard8f915942021-08-16 10:57:25 +02002763 $goto = implode(",", (array)$gotos);
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01002764 }
2765 if (!empty($goto)) {
2766 $stmt = $pdo->prepare("UPDATE `alias` SET
2767 `address` = :address,
2768 `public_comment` = :public_comment,
2769 `private_comment` = :private_comment,
2770 `domain` = :domain,
2771 `goto` = :goto,
2772 `sogo_visible`= :sogo_visible,
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02002773 `internal`= :internal,
2774 `sender_allowed`= :sender_allowed,
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01002775 `active`= :active
2776 WHERE `id` = :id");
2777 $stmt->execute(array(
2778 ':address' => $address,
2779 ':public_comment' => $public_comment,
2780 ':private_comment' => $private_comment,
2781 ':domain' => $domain,
2782 ':goto' => $goto,
2783 ':sogo_visible' => $sogo_visible,
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02002784 ':internal' => $internal,
2785 ':sender_allowed' => $sender_allowed,
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01002786 ':active' => $active,
2787 ':id' => $is_now['id']
2788 ));
2789 }
2790 $_SESSION['return'][] = array(
2791 'type' => 'success',
2792 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2793 'msg' => array('alias_modified', htmlspecialchars($address))
2794 );
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02002795
2796 // Track affected mailboxes for SOGo update (both old and new goto addresses)
2797 // Old goto: to remove alias from their view
2798 if (!empty($is_now['goto'])) {
2799 $old_gotos = array_map('trim', explode(',', $is_now['goto']));
2800 foreach ($old_gotos as $g) {
2801 if (filter_var($g, FILTER_VALIDATE_EMAIL) &&
2802 !in_array($g, array('null@localhost', 'spam@localhost', 'ham@localhost'))) {
2803 $update_sogo_mailboxes[] = $g;
2804 }
2805 }
2806 }
2807 // New goto: to add alias to their view
2808 if (!empty($goto)) {
2809 $new_gotos = array_map('trim', explode(',', $goto));
2810 foreach ($new_gotos as $g) {
2811 if (filter_var($g, FILTER_VALIDATE_EMAIL) &&
2812 !in_array($g, array('null@localhost', 'spam@localhost', 'ham@localhost'))) {
2813 $update_sogo_mailboxes[] = $g;
2814 }
2815 }
2816 }
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01002817 }
2818 break;
2819 case 'domain':
2820 if (!is_array($_data['domain'])) {
2821 $domains = array();
2822 $domains[] = $_data['domain'];
2823 }
2824 else {
2825 $domains = $_data['domain'];
2826 }
2827 foreach ($domains as $domain) {
2828 $domain = idn_to_ascii($domain, 0, INTL_IDNA_VARIANT_UTS46);
2829 if (!is_valid_domain_name($domain)) {
2830 $_SESSION['return'][] = array(
2831 'type' => 'danger',
2832 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2833 'msg' => 'domain_invalid'
2834 );
2835 continue;
2836 }
2837 if ($_SESSION['mailcow_cc_role'] == "domainadmin" &&
2838 hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $domain)) {
2839 $is_now = mailbox('get', 'domain_details', $domain);
2840 if (!empty($is_now)) {
2841 $gal = (isset($_data['gal'])) ? intval($_data['gal']) : $is_now['gal'];
2842 $description = (!empty($_data['description']) && isset($_SESSION['acl']['domain_desc']) && $_SESSION['acl']['domain_desc'] == "1") ? $_data['description'] : $is_now['description'];
Matthias Andreas Benkard8f915942021-08-16 10:57:25 +02002843 (int)$relayhost = (isset($_data['relayhost']) && isset($_SESSION['acl']['domain_relayhost']) && $_SESSION['acl']['domain_relayhost'] == "1") ? intval($_data['relayhost']) : intval($is_now['relayhost']);
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +01002844 $tags = (is_array($_data['tags']) ? $_data['tags'] : array());
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01002845 }
2846 else {
2847 $_SESSION['return'][] = array(
2848 'type' => 'danger',
2849 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2850 'msg' => 'domain_invalid'
2851 );
2852 continue;
2853 }
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +01002854
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01002855 $stmt = $pdo->prepare("UPDATE `domain` SET
2856 `description` = :description,
2857 `gal` = :gal
2858 WHERE `domain` = :domain");
2859 $stmt->execute(array(
2860 ':description' => $description,
2861 ':gal' => $gal,
2862 ':domain' => $domain
2863 ));
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +01002864 // save tags
2865 foreach($tags as $index => $tag){
2866 if (empty($tag)) continue;
2867 if ($index > $GLOBALS['TAGGING_LIMIT']) {
2868 $_SESSION['return'][] = array(
2869 'type' => 'warning',
2870 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2871 'msg' => array('tag_limit_exceeded', 'limit '.$GLOBALS['TAGGING_LIMIT'])
2872 );
2873 break;
2874 }
2875 $stmt = $pdo->prepare("INSERT INTO `tags_domain` (`domain`, `tag_name`) VALUES (:domain, :tag_name)");
2876 $stmt->execute(array(
2877 ':domain' => $domain,
2878 ':tag_name' => $tag,
2879 ));
2880 }
2881
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01002882 $_SESSION['return'][] = array(
2883 'type' => 'success',
2884 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2885 'msg' => array('domain_modified', htmlspecialchars($domain))
2886 );
2887 }
2888 elseif ($_SESSION['mailcow_cc_role'] == "admin") {
2889 $is_now = mailbox('get', 'domain_details', $domain);
2890 if (!empty($is_now)) {
2891 $active = (isset($_data['active'])) ? intval($_data['active']) : $is_now['active'];
2892 $backupmx = (isset($_data['backupmx'])) ? intval($_data['backupmx']) : $is_now['backupmx'];
2893 $gal = (isset($_data['gal'])) ? intval($_data['gal']) : $is_now['gal'];
2894 $relay_all_recipients = (isset($_data['relay_all_recipients'])) ? intval($_data['relay_all_recipients']) : $is_now['relay_all_recipients'];
2895 $relay_unknown_only = (isset($_data['relay_unknown_only'])) ? intval($_data['relay_unknown_only']) : $is_now['relay_unknown_only'];
2896 $relayhost = (isset($_data['relayhost'])) ? intval($_data['relayhost']) : $is_now['relayhost'];
2897 $aliases = (!empty($_data['aliases'])) ? $_data['aliases'] : $is_now['max_num_aliases_for_domain'];
2898 $mailboxes = (isset($_data['mailboxes']) && $_data['mailboxes'] != '') ? intval($_data['mailboxes']) : $is_now['max_num_mboxes_for_domain'];
2899 $defquota = (isset($_data['defquota']) && $_data['defquota'] != '') ? intval($_data['defquota']) : ($is_now['def_quota_for_mbox'] / 1048576);
2900 $maxquota = (!empty($_data['maxquota'])) ? $_data['maxquota'] : ($is_now['max_quota_for_mbox'] / 1048576);
2901 $quota = (!empty($_data['quota'])) ? $_data['quota'] : ($is_now['max_quota_for_domain'] / 1048576);
2902 $description = (!empty($_data['description'])) ? $_data['description'] : $is_now['description'];
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +01002903 $tags = (is_array($_data['tags']) ? $_data['tags'] : array());
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01002904 if ($relay_all_recipients == '1') {
2905 $backupmx = '1';
2906 }
2907 if ($relay_unknown_only == '1') {
2908 $backupmx = '1';
2909 $relay_all_recipients = '1';
2910 }
2911 }
2912 else {
2913 $_SESSION['return'][] = array(
2914 'type' => 'danger',
2915 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2916 'msg' => 'domain_invalid'
2917 );
2918 continue;
2919 }
2920 // todo: should be using api here
2921 $stmt = $pdo->prepare("SELECT
2922 COUNT(*) AS count,
2923 MAX(COALESCE(ROUND(`quota`/1048576), 0)) AS `biggest_mailbox`,
2924 COALESCE(ROUND(SUM(`quota`)/1048576), 0) AS `quota_all`
2925 FROM `mailbox`
Matthias Andreas Benkard8f915942021-08-16 10:57:25 +02002926 WHERE (`kind` = '' OR `kind` = NULL)
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01002927 AND domain = :domain");
2928 $stmt->execute(array(':domain' => $domain));
2929 $MailboxData = $stmt->fetch(PDO::FETCH_ASSOC);
2930 // todo: should be using api here
2931 $stmt = $pdo->prepare("SELECT COUNT(*) AS `count` FROM `alias`
2932 WHERE domain = :domain
2933 AND address NOT IN (
2934 SELECT `username` FROM `mailbox`
2935 )");
2936 $stmt->execute(array(':domain' => $domain));
2937 $AliasData = $stmt->fetch(PDO::FETCH_ASSOC);
2938 if ($defquota > $maxquota) {
2939 $_SESSION['return'][] = array(
2940 'type' => 'danger',
2941 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2942 'msg' => 'mailbox_defquota_exceeds_mailbox_maxquota'
2943 );
2944 continue;
2945 }
2946 if ($defquota == "0" || empty($defquota)) {
2947 $_SESSION['return'][] = array(
2948 'type' => 'danger',
2949 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2950 'msg' => 'defquota_empty'
2951 );
2952 continue;
2953 }
2954 if ($maxquota > $quota) {
2955 $_SESSION['return'][] = array(
2956 'type' => 'danger',
2957 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2958 'msg' => 'mailbox_quota_exceeds_domain_quota'
2959 );
2960 continue;
2961 }
2962 if ($maxquota == "0" || empty($maxquota)) {
2963 $_SESSION['return'][] = array(
2964 'type' => 'danger',
2965 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2966 'msg' => 'maxquota_empty'
2967 );
2968 continue;
2969 }
2970 if ($MailboxData['biggest_mailbox'] > $maxquota) {
2971 $_SESSION['return'][] = array(
2972 'type' => 'danger',
2973 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2974 'msg' => array('max_quota_in_use', $MailboxData['biggest_mailbox'])
2975 );
2976 continue;
2977 }
2978 if ($MailboxData['quota_all'] > $quota) {
2979 $_SESSION['return'][] = array(
2980 'type' => 'danger',
2981 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2982 'msg' => array('domain_quota_m_in_use', $MailboxData['quota_all'])
2983 );
2984 continue;
2985 }
2986 if ($MailboxData['count'] > $mailboxes) {
2987 $_SESSION['return'][] = array(
2988 'type' => 'danger',
2989 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2990 'msg' => array('mailboxes_in_use', $MailboxData['count'])
2991 );
2992 continue;
2993 }
2994 if ($AliasData['count'] > $aliases) {
2995 $_SESSION['return'][] = array(
2996 'type' => 'danger',
2997 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2998 'msg' => array('aliases_in_use', $AliasData['count'])
2999 );
3000 continue;
3001 }
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +01003002
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01003003 $stmt = $pdo->prepare("UPDATE `domain` SET
3004 `relay_all_recipients` = :relay_all_recipients,
3005 `relay_unknown_only` = :relay_unknown_only,
3006 `backupmx` = :backupmx,
3007 `gal` = :gal,
3008 `active` = :active,
3009 `quota` = :quota,
3010 `defquota` = :defquota,
3011 `maxquota` = :maxquota,
3012 `relayhost` = :relayhost,
3013 `mailboxes` = :mailboxes,
3014 `aliases` = :aliases,
3015 `description` = :description
3016 WHERE `domain` = :domain");
3017 $stmt->execute(array(
3018 ':relay_all_recipients' => $relay_all_recipients,
3019 ':relay_unknown_only' => $relay_unknown_only,
3020 ':backupmx' => $backupmx,
3021 ':gal' => $gal,
3022 ':active' => $active,
3023 ':quota' => $quota,
3024 ':defquota' => $defquota,
3025 ':maxquota' => $maxquota,
3026 ':relayhost' => $relayhost,
3027 ':mailboxes' => $mailboxes,
3028 ':aliases' => $aliases,
3029 ':description' => $description,
3030 ':domain' => $domain
3031 ));
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +01003032 // save tags
3033 foreach($tags as $index => $tag){
3034 if (empty($tag)) continue;
3035 if ($index > $GLOBALS['TAGGING_LIMIT']) {
3036 $_SESSION['return'][] = array(
3037 'type' => 'warning',
3038 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
3039 'msg' => array('tag_limit_exceeded', 'limit '.$GLOBALS['TAGGING_LIMIT'])
3040 );
3041 break;
3042 }
3043 $stmt = $pdo->prepare("INSERT INTO `tags_domain` (`domain`, `tag_name`) VALUES (:domain, :tag_name)");
3044 $stmt->execute(array(
3045 ':domain' => $domain,
3046 ':tag_name' => $tag,
3047 ));
3048 }
3049
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01003050 $_SESSION['return'][] = array(
3051 'type' => 'success',
3052 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
3053 'msg' => array('domain_modified', htmlspecialchars($domain))
3054 );
3055 }
3056 }
3057 break;
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +01003058 case 'domain_templates':
3059 if ($_SESSION['mailcow_cc_role'] != "admin") {
3060 $_SESSION['return'][] = array(
3061 'type' => 'danger',
3062 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_extra),
3063 'msg' => 'access_denied'
3064 );
3065 return false;
3066 }
3067 if (!is_array($_data['ids'])) {
3068 $ids = array();
3069 $ids[] = $_data['ids'];
3070 }
3071 else {
3072 $ids = $_data['ids'];
3073 }
3074 foreach ($ids as $id) {
3075 $is_now = mailbox("get", "domain_templates", $id);
3076 if (empty($is_now) ||
3077 $is_now["type"] != "domain"){
3078 $_SESSION['return'][] = array(
3079 'type' => 'danger',
3080 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_extra),
3081 'msg' => 'template_id_invalid'
3082 );
3083 continue;
3084 }
3085
3086 // check name
3087 if ($is_now["template"] == "Default" && $is_now["template"] != $_data["template"]){
3088 // keep template name of Default template
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02003089 $_data["template"] = $is_now["template"];
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +01003090 }
3091 else {
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02003092 $_data["template"] = (isset($_data["template"])) ? $_data["template"] : $is_now["template"];
3093 }
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +01003094 // check attributes
3095 $attr = array();
3096 $attr['tags'] = (isset($_data['tags'])) ? $_data['tags'] : array();
3097 $attr['max_num_aliases_for_domain'] = (isset($_data['max_num_aliases_for_domain'])) ? intval($_data['max_num_aliases_for_domain']) : 0;
3098 $attr['max_num_mboxes_for_domain'] = (isset($_data['max_num_mboxes_for_domain'])) ? intval($_data['max_num_mboxes_for_domain']) : 0;
3099 $attr['def_quota_for_mbox'] = (isset($_data['def_quota_for_mbox'])) ? intval($_data['def_quota_for_mbox']) * 1048576 : 0;
3100 $attr['max_quota_for_mbox'] = (isset($_data['max_quota_for_mbox'])) ? intval($_data['max_quota_for_mbox']) * 1048576 : 0;
3101 $attr['max_quota_for_domain'] = (isset($_data['max_quota_for_domain'])) ? intval($_data['max_quota_for_domain']) * 1048576 : 0;
3102 $attr['rl_frame'] = (!empty($_data['rl_frame'])) ? $_data['rl_frame'] : "s";
3103 $attr['rl_value'] = (!empty($_data['rl_value'])) ? $_data['rl_value'] : "";
3104 $attr['active'] = isset($_data['active']) ? intval($_data['active']) : 1;
3105 $attr['gal'] = (isset($_data['gal'])) ? intval($_data['gal']) : 1;
3106 $attr['backupmx'] = (isset($_data['backupmx'])) ? intval($_data['backupmx']) : 0;
3107 $attr['relay_all_recipients'] = (isset($_data['relay_all_recipients'])) ? intval($_data['relay_all_recipients']) : 0;
3108 $attr['relay_unknown_only'] = (isset($_data['relay_unknown_only'])) ? intval($_data['relay_unknown_only']) : 0;
3109 $attr['dkim_selector'] = (isset($_data['dkim_selector'])) ? $_data['dkim_selector'] : "dkim";
3110 $attr['key_size'] = isset($_data['key_size']) ? intval($_data['key_size']) : 2048;
3111
3112 // update template
3113 $stmt = $pdo->prepare("UPDATE `templates`
3114 SET `template` = :template, `attributes` = :attributes
3115 WHERE id = :id");
3116 $stmt->execute(array(
3117 ":id" => $id ,
3118 ":template" => $_data["template"] ,
3119 ":attributes" => json_encode($attr)
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02003120 ));
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +01003121 }
3122
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02003123
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +01003124 $_SESSION['return'][] = array(
3125 'type' => 'success',
3126 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
3127 'msg' => array('template_modified', $_data["template"])
3128 );
3129 return true;
3130 break;
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01003131 case 'mailbox':
3132 if (!is_array($_data['username'])) {
3133 $usernames = array();
3134 $usernames[] = $_data['username'];
3135 }
3136 else {
3137 $usernames = $_data['username'];
3138 }
3139 foreach ($usernames as $username) {
3140 if (!filter_var($username, FILTER_VALIDATE_EMAIL)) {
3141 $_SESSION['return'][] = array(
3142 'type' => 'danger',
3143 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
3144 'msg' => array('username_invalid', $username)
3145 );
3146 continue;
3147 }
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02003148 $is_now = mailbox('get', 'mailbox_details', $username, $_extra);
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01003149 if (isset($_data['protocol_access'])) {
Matthias Andreas Benkardb4b5cc12021-12-28 18:02:04 +01003150 $_data['protocol_access'] = (array)$_data['protocol_access'];
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01003151 $_data['imap_access'] = (in_array('imap', $_data['protocol_access'])) ? 1 : 0;
3152 $_data['pop3_access'] = (in_array('pop3', $_data['protocol_access'])) ? 1 : 0;
3153 $_data['smtp_access'] = (in_array('smtp', $_data['protocol_access'])) ? 1 : 0;
Matthias Andreas Benkardb4b5cc12021-12-28 18:02:04 +01003154 $_data['sieve_access'] = (in_array('sieve', $_data['protocol_access'])) ? 1 : 0;
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02003155 $_data['eas_access'] = (in_array('eas', $_data['protocol_access'])) ? 1 : 0;
3156 $_data['dav_access'] = (in_array('dav', $_data['protocol_access'])) ? 1 : 0;
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01003157 }
3158 if (!empty($is_now)) {
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02003159 $active = (isset($_data['active'])) ? intval($_data['active']) : $is_now['active'];
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01003160 (int)$force_pw_update = (isset($_data['force_pw_update'])) ? intval($_data['force_pw_update']) : intval($is_now['attributes']['force_pw_update']);
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02003161 (int)$force_tfa = (isset($_data['force_tfa'])) ? intval($_data['force_tfa']) : intval($is_now['attributes']['force_tfa']);
3162 (int)$sogo_access = (isset($_data['sogo_access']) && hasACLAccess("sogo_access")) ? intval($_data['sogo_access']) : intval($is_now['attributes']['sogo_access']);
3163 (int)$imap_access = (isset($_data['imap_access']) && hasACLAccess("protocol_access")) ? intval($_data['imap_access']) : intval($is_now['attributes']['imap_access']);
3164 (int)$pop3_access = (isset($_data['pop3_access']) && hasACLAccess("protocol_access")) ? intval($_data['pop3_access']) : intval($is_now['attributes']['pop3_access']);
3165 (int)$smtp_access = (isset($_data['smtp_access']) && hasACLAccess("protocol_access")) ? intval($_data['smtp_access']) : intval($is_now['attributes']['smtp_access']);
3166 (int)$sieve_access = (isset($_data['sieve_access']) && hasACLAccess("protocol_access")) ? intval($_data['sieve_access']) : intval($is_now['attributes']['sieve_access']);
3167 (int)$eas_access = (isset($_data['eas_access']) && hasACLAccess("protocol_access")) ? intval($_data['eas_access']) : intval($is_now['attributes']['eas_access']);
3168 (int)$dav_access = (isset($_data['dav_access']) && hasACLAccess("protocol_access")) ? intval($_data['dav_access']) : intval($is_now['attributes']['dav_access']);
3169 (int)$relayhost = (isset($_data['relayhost']) && hasACLAccess("mailbox_relayhost")) ? intval($_data['relayhost']) : intval($is_now['attributes']['relayhost']);
3170 (int)$quota_m = (isset_has_content($_data['quota'])) ? intval($_data['quota']) : ($is_now['quota'] / 1048576);
3171 $name = (!empty($_data['name'])) ? ltrim(rtrim($_data['name'], '>'), '<') : $is_now['name'];
3172 $domain = $is_now['domain'];
3173 $quota_b = $quota_m * 1048576;
3174 $password = (!empty($_data['password'])) ? $_data['password'] : null;
3175 $password2 = (!empty($_data['password2'])) ? $_data['password2'] : null;
3176 $tags = (is_array($_data['tags']) ? $_data['tags'] : array());
3177 $attribute_hash = (!empty($_data['attribute_hash'])) ? $_data['attribute_hash'] : '';
3178 $authsource = $is_now['authsource'];
3179 if ($_data['authsource'] == "mailcow" ||
3180 in_array($_data['authsource'], array('keycloak', 'generic-oidc', 'ldap')) && $iam_settings['authsource'] == $_data['authsource']){
3181 $authsource = $_data['authsource'];
3182 }
3183 if (in_array($authsource, array('keycloak', 'generic-oidc', 'ldap'))){
3184 $force_pw_update = 0;
3185 }
3186 if ($authsource == 'generic-oidc'){
3187 $force_tfa = 0;
3188 }
3189 $pw_recovery_email = (isset($_data['pw_recovery_email']) && $authsource == 'mailcow') ? $_data['pw_recovery_email'] : $is_now['attributes']['recovery_email'];
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01003190 }
3191 else {
3192 $_SESSION['return'][] = array(
3193 'type' => 'danger',
3194 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
3195 'msg' => 'access_denied'
3196 );
3197 continue;
3198 }
3199 // if already 0 == ok
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02003200 if (!hasACLAccess("unlimited_quota") && ($quota_m == 0 && $is_now['quota'] != 0)) {
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01003201 $_SESSION['return'][] = array(
3202 'type' => 'danger',
3203 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
3204 'msg' => 'unlimited_quota_acl'
3205 );
3206 return false;
3207 }
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01003208 if (!hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $domain)) {
3209 $_SESSION['return'][] = array(
3210 'type' => 'danger',
3211 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
3212 'msg' => 'access_denied'
3213 );
3214 continue;
3215 }
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02003216 $DomainData = mailbox('get', 'domain_details', $domain, $_extra);
Matthias Andreas Benkard8f915942021-08-16 10:57:25 +02003217 if ($quota_m > ($is_now['max_new_quota'] / 1048576)) {
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01003218 $_SESSION['return'][] = array(
3219 'type' => 'danger',
3220 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
3221 'msg' => array('mailbox_quota_left_exceeded', ($is_now['max_new_quota'] / 1048576))
3222 );
3223 continue;
3224 }
Matthias Andreas Benkard8f915942021-08-16 10:57:25 +02003225 if ($quota_m > $DomainData['max_quota_for_mbox']) {
3226 $_SESSION['return'][] = array(
3227 'type' => 'danger',
3228 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
3229 'msg' => array('mailbox_quota_exceeded', $DomainData['max_quota_for_mbox'])
3230 );
3231 continue;
3232 }
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01003233 $extra_acls = array();
3234 if (isset($_data['extended_sender_acl'])) {
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02003235 if (!hasACLAccess("extend_sender_acl")) {
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01003236 $_SESSION['return'][] = array(
3237 'type' => 'danger',
3238 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +01003239 'msg' => 'extended_sender_acl_denied'
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01003240 );
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01003241 }
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +01003242 else {
3243 $extra_acls = array_map('trim', preg_split( "/( |,|;|\n)/", $_data['extended_sender_acl']));
3244 foreach ($extra_acls as $i => &$extra_acl) {
3245 if (empty($extra_acl)) {
3246 continue;
3247 }
3248 if (substr($extra_acl, 0, 1) === "@") {
3249 $extra_acl = ltrim($extra_acl, '@');
3250 }
3251 if (!filter_var($extra_acl, FILTER_VALIDATE_EMAIL) && !is_valid_domain_name($extra_acl)) {
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01003252 $_SESSION['return'][] = array(
3253 'type' => 'danger',
3254 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +01003255 'msg' => array('extra_acl_invalid', htmlspecialchars($extra_acl))
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01003256 );
3257 unset($extra_acls[$i]);
3258 continue;
3259 }
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +01003260 $domains = array_merge(mailbox('get', 'domains'), mailbox('get', 'alias_domains'));
3261 if (filter_var($extra_acl, FILTER_VALIDATE_EMAIL)) {
3262 $extra_acl_domain = idn_to_ascii(substr(strstr($extra_acl, '@'), 1), 0, INTL_IDNA_VARIANT_UTS46);
3263 if (in_array($extra_acl_domain, $domains)) {
3264 $_SESSION['return'][] = array(
3265 'type' => 'danger',
3266 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
3267 'msg' => array('extra_acl_invalid_domain', $extra_acl_domain)
3268 );
3269 unset($extra_acls[$i]);
3270 continue;
3271 }
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01003272 }
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +01003273 else {
3274 if (in_array($extra_acl, $domains)) {
3275 $_SESSION['return'][] = array(
3276 'type' => 'danger',
3277 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
3278 'msg' => array('extra_acl_invalid_domain', $extra_acl_domain)
3279 );
3280 unset($extra_acls[$i]);
3281 continue;
3282 }
3283 $extra_acl = '@' . $extra_acl;
3284 }
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01003285 }
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +01003286 $extra_acls = array_filter($extra_acls);
3287 $extra_acls = array_values($extra_acls);
3288 $extra_acls = array_unique($extra_acls);
3289 $stmt = $pdo->prepare("DELETE FROM `sender_acl` WHERE `external` = 1 AND `logged_in_as` = :username");
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01003290 $stmt->execute(array(
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01003291 ':username' => $username
3292 ));
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +01003293 foreach ($extra_acls as $sender_acl_external) {
3294 $stmt = $pdo->prepare("INSERT INTO `sender_acl` (`send_as`, `logged_in_as`, `external`)
3295 VALUES (:sender_acl, :username, 1)");
3296 $stmt->execute(array(
3297 ':sender_acl' => $sender_acl_external,
3298 ':username' => $username
3299 ));
3300 }
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01003301 }
3302 }
3303 if (isset($_data['sender_acl'])) {
3304 // Get sender_acl items set by admin
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02003305 $current_sender_acls = mailbox('get', 'sender_acl_handles', $username);
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01003306 $sender_acl_admin = array_merge(
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02003307 $current_sender_acls['sender_acl_domains']['ro'],
3308 $current_sender_acls['sender_acl_addresses']['ro']
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01003309 );
3310 // Get sender_acl items from POST array
3311 // Set sender_acl_domain_admin to empty array if sender_acl contains "default" to trigger a reset
3312 // Delete records from sender_acl if sender_acl contains "*" and set to array("*")
3313 $_data['sender_acl'] = (array)$_data['sender_acl'];
3314 if (in_array("*", $_data['sender_acl'])) {
3315 $sender_acl_domain_admin = array('*');
3316 }
3317 elseif (array("default") === $_data['sender_acl']) {
3318 $sender_acl_domain_admin = array();
3319 }
3320 else {
3321 if (array_search('default', $_data['sender_acl']) !== false){
3322 unset($_data['sender_acl'][array_search('default', $_data['sender_acl'])]);
3323 }
3324 $sender_acl_domain_admin = $_data['sender_acl'];
3325 }
3326 if (!empty($sender_acl_domain_admin) || !empty($sender_acl_admin)) {
3327 // Check items in POST array and skip invalid
3328 foreach ($sender_acl_domain_admin as $key => $val) {
3329 // Check for invalid domain or email format or not *
3330 if (!filter_var($val, FILTER_VALIDATE_EMAIL) && !is_valid_domain_name(ltrim($val, '@')) && $val != '*') {
3331 $_SESSION['return'][] = array(
3332 'type' => 'danger',
3333 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
3334 'msg' => array('sender_acl_invalid', $sender_acl_domain_admin[$key])
3335 );
3336 unset($sender_acl_domain_admin[$key]);
3337 continue;
3338 }
3339 // Check if user has domain access (if object is domain)
3340 $domain = ltrim($sender_acl_domain_admin[$key], '@');
3341 if (is_valid_domain_name($domain)) {
3342 // Check for- and skip non-mailcow domains
3343 $domains = array_merge(mailbox('get', 'domains'), mailbox('get', 'alias_domains'));
3344 if (!empty($domains)) {
3345 if (!in_array($domain, $domains)) {
3346 $_SESSION['return'][] = array(
3347 'type' => 'danger',
3348 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
3349 'msg' => array('sender_acl_invalid', $sender_acl_domain_admin[$key])
3350 );
3351 unset($sender_acl_domain_admin[$key]);
3352 continue;
3353 }
3354 }
3355 if (!hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $domain)) {
3356 $_SESSION['return'][] = array(
3357 'type' => 'danger',
3358 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
3359 'msg' => array('sender_acl_invalid', $sender_acl_domain_admin[$key])
3360 );
3361 unset($sender_acl_domain_admin[$key]);
3362 continue;
3363 }
3364 }
3365 // Wildcard can only be used if role == admin
3366 if ($val == '*' && $_SESSION['mailcow_cc_role'] != 'admin') {
3367 $_SESSION['return'][] = array(
3368 'type' => 'danger',
3369 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
3370 'msg' => array('sender_acl_invalid', $sender_acl_domain_admin[$key])
3371 );
3372 unset($sender_acl_domain_admin[$key]);
3373 continue;
3374 }
3375 // Check if user has alias access (if object is email)
3376 if (filter_var($val, FILTER_VALIDATE_EMAIL)) {
3377 if (!hasAliasObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $val)) {
3378 $_SESSION['return'][] = array(
3379 'type' => 'danger',
3380 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
3381 'msg' => array('sender_acl_invalid', $sender_acl_domain_admin[$key])
3382 );
3383 unset($sender_acl_domain_admin[$key]);
3384 continue;
3385 }
3386 }
3387 }
3388 // Merge both arrays
3389 $sender_acl_merged = array_merge($sender_acl_domain_admin, $sender_acl_admin);
3390 // If merged array still contains "*", set it as only value
3391 !in_array('*', $sender_acl_merged) ?: $sender_acl_merged = array('*');
3392 $stmt = $pdo->prepare("DELETE FROM `sender_acl` WHERE `external` = 0 AND `logged_in_as` = :username");
3393 $stmt->execute(array(
3394 ':username' => $username
3395 ));
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02003396 $sender_acl_handles = mailbox('get', 'sender_acl_handles', $username);
3397 $fixed_sender_aliases_allowed = $sender_acl_handles['fixed_sender_aliases_allowed'];
3398 $fixed_sender_aliases_blocked = $sender_acl_handles['fixed_sender_aliases_blocked'];
3399
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01003400 foreach ($sender_acl_merged as $sender_acl) {
3401 $domain = ltrim($sender_acl, '@');
3402 if (is_valid_domain_name($domain)) {
3403 $sender_acl = '@' . $domain;
3404 }
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02003405
3406 // Always add to sender_acl table to create explicit permission
3407 // Skip only if it's in allowed list (would be redundant)
3408 // But DO add if it's in blocked list (creates override)
3409 if (in_array($sender_acl, $fixed_sender_aliases_allowed)) {
3410 // Skip: already allowed by sender_allowed=1, no need for sender_acl entry
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01003411 continue;
3412 }
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02003413
3414 // Add to sender_acl (either override for blocked aliases, or grant for selectable ones)
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01003415 $stmt = $pdo->prepare("INSERT INTO `sender_acl` (`send_as`, `logged_in_as`)
3416 VALUES (:sender_acl, :username)");
3417 $stmt->execute(array(
3418 ':sender_acl' => $sender_acl,
3419 ':username' => $username
3420 ));
3421 }
3422 }
3423 else {
3424 $stmt = $pdo->prepare("DELETE FROM `sender_acl` WHERE `external` = 0 AND `logged_in_as` = :username");
3425 $stmt->execute(array(
3426 ':username' => $username
3427 ));
3428 }
3429 }
Matthias Andreas Benkard8f915942021-08-16 10:57:25 +02003430 if (!empty($password)) {
3431 if (password_check($password, $password2) !== true) {
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01003432 continue;
3433 }
Matthias Andreas Benkard8f915942021-08-16 10:57:25 +02003434 $password_hashed = hash_password($password);
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01003435 $stmt = $pdo->prepare("UPDATE `mailbox` SET
Matthias Andreas Benkard8f915942021-08-16 10:57:25 +02003436 `password` = :password_hashed,
3437 `attributes` = JSON_SET(`attributes`, '$.passwd_update', NOW())
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02003438 WHERE `username` = :username AND authsource = 'mailcow'");
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01003439 $stmt->execute(array(
3440 ':password_hashed' => $password_hashed,
3441 ':username' => $username
3442 ));
3443 }
Matthias Andreas Benkard8f915942021-08-16 10:57:25 +02003444 // We could either set alias = 1 if alias = 2 or tune the Postfix alias table (that's what we did, TODO: do it the other way)
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01003445 $stmt = $pdo->prepare("UPDATE `alias` SET
3446 `active` = :active
3447 WHERE `address` = :address");
3448 $stmt->execute(array(
3449 ':address' => $username,
3450 ':active' => $active
3451 ));
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02003452 try {
3453 $stmt = $pdo->prepare("UPDATE `mailbox` SET
3454 `active` = :active,
3455 `name`= :name,
3456 `quota` = :quota_b,
3457 `authsource` = :authsource,
3458 `attributes` = JSON_SET(`attributes`, '$.force_pw_update', :force_pw_update),
3459 `attributes` = JSON_SET(`attributes`, '$.force_tfa', :force_tfa),
3460 `attributes` = JSON_SET(`attributes`, '$.sogo_access', :sogo_access),
3461 `attributes` = JSON_SET(`attributes`, '$.imap_access', :imap_access),
3462 `attributes` = JSON_SET(`attributes`, '$.sieve_access', :sieve_access),
3463 `attributes` = JSON_SET(`attributes`, '$.pop3_access', :pop3_access),
3464 `attributes` = JSON_SET(`attributes`, '$.relayhost', :relayhost),
3465 `attributes` = JSON_SET(`attributes`, '$.smtp_access', :smtp_access),
3466 `attributes` = JSON_SET(`attributes`, '$.eas_access', :eas_access),
3467 `attributes` = JSON_SET(`attributes`, '$.dav_access', :dav_access),
3468 `attributes` = JSON_SET(`attributes`, '$.recovery_email', :recovery_email),
3469 `attributes` = JSON_SET(`attributes`, '$.attribute_hash', :attribute_hash)
3470 WHERE `username` = :username");
3471 $stmt->execute(array(
3472 ':active' => $active,
3473 ':name' => $name,
3474 ':quota_b' => $quota_b,
3475 ':attribute_hash' => $attribute_hash,
3476 ':force_pw_update' => $force_pw_update,
3477 ':force_tfa' => $force_tfa,
3478 ':sogo_access' => $sogo_access,
3479 ':imap_access' => $imap_access,
3480 ':pop3_access' => $pop3_access,
3481 ':sieve_access' => $sieve_access,
3482 ':smtp_access' => $smtp_access,
3483 ':eas_access' => $eas_access,
3484 ':dav_access' => $dav_access,
3485 ':recovery_email' => $pw_recovery_email,
3486 ':relayhost' => $relayhost,
3487 ':username' => $username,
3488 ':authsource' => $authsource
3489 ));
3490 }
3491 catch (PDOException $e) {
3492 $_SESSION['return'][] = array(
3493 'type' => 'danger',
3494 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
3495 'msg' => $e->getMessage()
3496 );
3497 return false;
3498 }
3499 // save delimiter_action
3500 if (isset($_data['tagged_mail_handler'])) {
3501 mailbox('edit', 'delimiter_action', array(
3502 'username' => $username,
3503 'tagged_mail_handler' => $_data['tagged_mail_handler']
3504 ));
3505 }
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +01003506 // save tags
3507 foreach($tags as $index => $tag){
3508 if (empty($tag)) continue;
3509 if ($index > $GLOBALS['TAGGING_LIMIT']) {
3510 $_SESSION['return'][] = array(
3511 'type' => 'warning',
3512 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
3513 'msg' => array('tag_limit_exceeded', 'limit '.$GLOBALS['TAGGING_LIMIT'])
3514 );
3515 break;
3516 }
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02003517 try {
3518 $stmt = $pdo->prepare("INSERT INTO `tags_mailbox` (`username`, `tag_name`) VALUES (:username, :tag_name)");
3519 $stmt->execute(array(
3520 ':username' => $username,
3521 ':tag_name' => $tag,
3522 ));
3523 } catch (Exception $e) {
3524 }
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +01003525 }
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02003526
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01003527 $_SESSION['return'][] = array(
3528 'type' => 'success',
3529 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
3530 'msg' => array('mailbox_modified', $username)
3531 );
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +01003532
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02003533 // Track affected mailboxes for SOGo update
3534 $update_sogo_mailboxes[] = $username;
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01003535 }
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02003536 break;
3537 case 'mailbox_rename':
3538 $domain = $_data['domain'];
3539 $old_local_part = $_data['old_local_part'];
3540 $old_username = $old_local_part . "@" . $domain;
3541 $new_local_part = $_data['new_local_part'];
3542 $new_username = $new_local_part . "@" . $domain;
3543 $create_alias = intval($_data['create_alias']);
3544
3545 if (!filter_var($old_username, FILTER_VALIDATE_EMAIL)) {
3546 $_SESSION['return'][] = array(
3547 'type' => 'danger',
3548 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
3549 'msg' => array('username_invalid', $old_username)
3550 );
3551 return false;
3552 }
3553 if (!filter_var($new_username, FILTER_VALIDATE_EMAIL)) {
3554 $_SESSION['return'][] = array(
3555 'type' => 'danger',
3556 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
3557 'msg' => array('username_invalid', $new_username)
3558 );
3559 return false;
3560 }
3561
3562 $is_now = mailbox('get', 'mailbox_details', $old_username);
3563 if (empty($is_now) || ($is_now['active'] != '1' && $is_now['active'] != '2')) {
3564 $_SESSION['return'][] = array(
3565 'type' => 'danger',
3566 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
3567 'msg' => 'access_denied'
3568 );
3569 return false;
3570 }
3571
3572 if (!hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $is_now['domain'])) {
3573 $_SESSION['return'][] = array(
3574 'type' => 'danger',
3575 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
3576 'msg' => 'access_denied'
3577 );
3578 return false;
3579 }
3580
3581 // get imap acls
3582 try {
3583 $exec_fields = array(
3584 'cmd' => 'doveadm',
3585 'task' => 'get_acl',
3586 'id' => $old_username
3587 );
3588 $imap_acls = json_decode(docker('post', 'dovecot-mailcow', 'exec', $exec_fields), true);
3589 // delete imap acls
3590 foreach ($imap_acls as $imap_acl) {
3591 $exec_fields = array(
3592 'cmd' => 'doveadm',
3593 'task' => 'delete_acl',
3594 'user' => $imap_acl['user'],
3595 'mailbox' => $imap_acl['mailbox'],
3596 'id' => $imap_acl['id']
3597 );
3598 docker('post', 'dovecot-mailcow', 'exec', $exec_fields);
3599 }
3600 } catch (Exception $e) {
3601 $_SESSION['return'][] = array(
3602 'type' => 'danger',
3603 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
3604 'msg' => $e->getMessage()
3605 );
3606 return false;
3607 }
3608
3609 // rename username in sql
3610 try {
3611 $pdo->beginTransaction();
3612 $pdo->exec('SET FOREIGN_KEY_CHECKS = 0');
3613
3614 // Update username in mailbox table
3615 $pdo->prepare('UPDATE mailbox SET username = :new_username, local_part = :new_local_part WHERE username = :old_username')
3616 ->execute([
3617 ':new_username' => $new_username,
3618 ':new_local_part' => $new_local_part,
3619 ':old_username' => $old_username
3620 ]);
3621
3622 $pdo->prepare("UPDATE alias SET address = :new_username, goto = :new_username2 WHERE address = :old_username")
3623 ->execute([
3624 ':new_username' => $new_username,
3625 ':new_username2' => $new_username,
3626 ':old_username' => $old_username
3627 ]);
3628
3629 // Update the username in all related tables
3630 $tables = [
3631 'tags_mailbox' => ['username'],
3632 'sieve_filters' => ['username'],
3633 'app_passwd' => ['mailbox'],
3634 'user_acl' => ['username'],
3635 'da_acl' => ['username'],
3636 'quota2' => ['username'],
3637 'quota2replica' => ['username'],
3638 'pushover' => ['username'],
3639 'alias' => ['goto'],
3640 "imapsync" => ['user2'],
3641 'bcc_maps' => ['local_dest', 'bcc_dest'],
3642 'recipient_maps' => ['old_dest', 'new_dest'],
3643 'sender_acl' => ['logged_in_as', 'send_as']
3644 ];
3645 foreach ($tables as $table => $columns) {
3646 foreach ($columns as $column) {
3647 $stmt = $pdo->prepare("UPDATE $table SET $column = :new_username WHERE $column = :old_username")
3648 ->execute([
3649 ':new_username' => $new_username,
3650 ':old_username' => $old_username
3651 ]);
3652 }
3653 }
3654
3655 // Update c_uid, c_name and mail in _sogo_static_view table
3656 $pdo->prepare("UPDATE _sogo_static_view SET c_uid = :new_username, c_name = :new_username2, mail = :new_username3 WHERE c_uid = :old_username")
3657 ->execute([
3658 ':new_username' => $new_username,
3659 ':new_username2' => $new_username,
3660 ':new_username3' => $new_username,
3661 ':old_username' => $old_username
3662 ]);
3663
3664 // Re-enable foreign key checks
3665 $pdo->exec('SET FOREIGN_KEY_CHECKS = 1');
3666 $pdo->commit();
3667 } catch (PDOException $e) {
3668 // Rollback the transaction if something goes wrong
3669 $pdo->rollBack();
3670 $_SESSION['return'][] = array(
3671 'type' => 'danger',
3672 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
3673 'msg' => $e->getMessage()
3674 );
3675 return false;
3676 }
3677
3678 // move maildir
3679 $exec_fields = array(
3680 'cmd' => 'maildir',
3681 'task' => 'move',
3682 'old_maildir' => $domain . '/' . $old_local_part,
3683 'new_maildir' => $domain . '/' . $new_local_part
3684 );
3685 if (getenv("CLUSTERMODE") == "replication") {
3686 // broadcast to each dovecot container
3687 docker('broadcast', 'dovecot-mailcow', 'exec', $exec_fields);
3688 } else {
3689 docker('post', 'dovecot-mailcow', 'exec', $exec_fields);
3690 }
3691
3692 // rename username in sogo
3693 $exec_fields = array(
3694 'cmd' => 'sogo',
3695 'task' => 'rename_user',
3696 'old_username' => $old_username,
3697 'new_username' => $new_username
3698 );
3699 docker('post', 'sogo-mailcow', 'exec', $exec_fields);
3700
3701 // set imap acls
3702 foreach ($imap_acls as $imap_acl) {
3703 $user_id = ($imap_acl['id'] == $old_username) ? $new_username : $imap_acl['id'];
3704 $user = ($imap_acl['user'] == $old_username) ? $new_username : $imap_acl['user'];
3705 $exec_fields = array(
3706 'cmd' => 'doveadm',
3707 'task' => 'set_acl',
3708 'user' => $user,
3709 'mailbox' => $imap_acl['mailbox'],
3710 'id' => $user_id,
3711 'rights' => $imap_acl['rights']
3712 );
3713 docker('post', 'dovecot-mailcow', 'exec', $exec_fields);
3714 }
3715
3716 // create alias
3717 if ($create_alias == 1) {
3718 mailbox("add", "alias", array(
3719 "address" => $old_username,
3720 "goto" => $new_username,
3721 "active" => 1,
3722 "sogo_visible" => 1,
3723 "private_comment" => sprintf($lang['success']['mailbox_renamed'], $old_username, $new_username)
3724 ));
3725 }
3726
3727 $_SESSION['return'][] = array(
3728 'type' => 'success',
3729 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
3730 'msg' => array('mailbox_renamed', $old_username, $new_username)
3731 );
3732 break;
3733 case 'mailbox_from_template':
3734 $stmt = $pdo->prepare("SELECT * FROM `templates`
3735 WHERE `template` = :template AND type = 'mailbox'");
3736 $stmt->execute(array(
3737 ":template" => $_data['template']
3738 ));
3739 $mbox_template_data = $stmt->fetch(PDO::FETCH_ASSOC);
3740 if (empty($mbox_template_data)){
3741 $_SESSION['return'][] = array(
3742 'type' => 'danger',
3743 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
3744 'msg' => 'template_missing'
3745 );
3746 return false;
3747 }
3748
3749 $attribute_hash = sha1(json_encode($mbox_template_data["attributes"]));
3750 $is_now = mailbox('get', 'mailbox_details', $_data['username']);
3751 $name = ltrim(rtrim($_data['name'], '>'), '<');
3752 if ($is_now['attributes']['attribute_hash'] == $attribute_hash && $is_now['name'] == $name)
3753 return true;
3754
3755 $mbox_template_data = json_decode($mbox_template_data["attributes"], true);
3756 $mbox_template_data['attribute_hash'] = $attribute_hash;
3757 $mbox_template_data['name'] = $name;
3758 $quarantine_attributes = array('username' => $_data['username']);
3759 $tls_attributes = array('username' => $_data['username']);
3760 $ratelimit_attributes = array('object' => $_data['username']);
3761 $acl_attributes = array('username' => $_data['username'], 'user_acl' => array());
3762 $mailbox_attributes = array('username' => $_data['username']);
3763 foreach ($mbox_template_data as $key => $value){
3764 switch (true) {
3765 case (strpos($key, 'quarantine_') === 0):
3766 $quarantine_attributes[$key] = $value;
3767 break;
3768 case (strpos($key, 'tls_') === 0):
3769 if ($value == null)
3770 $value = 0;
3771 $tls_attributes[$key] = $value;
3772 break;
3773 case (strpos($key, 'rl_') === 0):
3774 $ratelimit_attributes[$key] = $value;
3775 break;
3776 case (strpos($key, 'acl_') === 0 && $value != 0):
3777 array_push($acl_attributes['user_acl'], str_replace('acl_' , '', $key));
3778 break;
3779 default:
3780 $mailbox_attributes[$key] = $value;
3781 break;
3782 }
3783 }
3784
3785 $mailbox_attributes['quota'] = intval($mailbox_attributes['quota'] / 1048576);
3786 $result = mailbox('edit', 'mailbox', $mailbox_attributes);
3787 if ($result === false) return $result;
3788 $result = mailbox('edit', 'tls_policy', $tls_attributes);
3789 if ($result === false) return $result;
3790 $result = mailbox('edit', 'quarantine_notification', $quarantine_attributes);
3791 if ($result === false) return $result;
3792 $result = mailbox('edit', 'quarantine_category', $quarantine_attributes);
3793 if ($result === false) return $result;
3794 $result = ratelimit('edit', 'mailbox', $ratelimit_attributes);
3795 if ($result === false) return $result;
3796 $result = acl('edit', 'user', $acl_attributes);
3797 if ($result === false) return $result;
3798
3799 $_SESSION['return'] = array();
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +01003800 return true;
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01003801 break;
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +01003802 case 'mailbox_templates':
3803 if ($_SESSION['mailcow_cc_role'] != "admin") {
3804 $_SESSION['return'][] = array(
3805 'type' => 'danger',
3806 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_extra),
3807 'msg' => 'access_denied'
3808 );
3809 return false;
3810 }
3811 if (!is_array($_data['ids'])) {
3812 $ids = array();
3813 $ids[] = $_data['ids'];
3814 }
3815 else {
3816 $ids = $_data['ids'];
3817 }
3818 foreach ($ids as $id) {
3819 $is_now = mailbox("get", "mailbox_templates", $id);
3820 if (empty($is_now) ||
3821 $is_now["type"] != "mailbox"){
3822 $_SESSION['return'][] = array(
3823 'type' => 'danger',
3824 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_extra),
3825 'msg' => 'template_id_invalid'
3826 );
3827 continue;
3828 }
3829
3830
3831 // check name
3832 if ($is_now["template"] == "Default" && $is_now["template"] != $_data["template"]){
3833 // keep template name of Default template
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02003834 $_data["template"] = $is_now["template"];
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +01003835 }
3836 else {
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02003837 $_data["template"] = (isset($_data["template"])) ? $_data["template"] : $is_now["template"];
3838 }
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +01003839 // check attributes
3840 $attr = array();
3841 $attr["quota"] = isset($_data['quota']) ? intval($_data['quota']) * 1048576 : 0;
3842 $attr['tags'] = (isset($_data['tags'])) ? $_data['tags'] : $is_now['tags'];
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02003843 $attr["tagged_mail_handler"] = (!empty($_data['tagged_mail_handler'])) ? $_data['tagged_mail_handler'] : $is_now['tagged_mail_handler'];
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +01003844 $attr["quarantine_notification"] = (!empty($_data['quarantine_notification'])) ? $_data['quarantine_notification'] : $is_now['quarantine_notification'];
3845 $attr["quarantine_category"] = (!empty($_data['quarantine_category'])) ? $_data['quarantine_category'] : $is_now['quarantine_category'];
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02003846 // Validate quarantine_category
3847 if (!in_array($attr["quarantine_category"], array('add_header', 'reject', 'all'))) {
3848 $_SESSION['return'][] = array(
3849 'type' => 'danger',
3850 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_extra),
3851 'msg' => 'quarantine_category_invalid'
3852 );
3853 continue;
3854 }
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +01003855 $attr["rl_frame"] = (!empty($_data['rl_frame'])) ? $_data['rl_frame'] : $is_now['rl_frame'];
3856 $attr["rl_value"] = (!empty($_data['rl_value'])) ? $_data['rl_value'] : $is_now['rl_value'];
3857 $attr["force_pw_update"] = isset($_data['force_pw_update']) ? intval($_data['force_pw_update']) : $is_now['force_pw_update'];
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02003858 $attr["force_tfa"] = isset($_data['force_tfa']) ? intval($_data['force_tfa']) : $is_now['force_tfa'];
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +01003859 $attr["sogo_access"] = isset($_data['sogo_access']) ? intval($_data['sogo_access']) : $is_now['sogo_access'];
3860 $attr["active"] = isset($_data['active']) ? intval($_data['active']) : $is_now['active'];
3861 $attr["tls_enforce_in"] = isset($_data['tls_enforce_in']) ? intval($_data['tls_enforce_in']) : $is_now['tls_enforce_in'];
3862 $attr["tls_enforce_out"] = isset($_data['tls_enforce_out']) ? intval($_data['tls_enforce_out']) : $is_now['tls_enforce_out'];
3863 if (isset($_data['protocol_access'])) {
3864 $_data['protocol_access'] = (array)$_data['protocol_access'];
3865 $attr['imap_access'] = (in_array('imap', $_data['protocol_access'])) ? 1 : 0;
3866 $attr['pop3_access'] = (in_array('pop3', $_data['protocol_access'])) ? 1 : 0;
3867 $attr['smtp_access'] = (in_array('smtp', $_data['protocol_access'])) ? 1 : 0;
3868 $attr['sieve_access'] = (in_array('sieve', $_data['protocol_access'])) ? 1 : 0;
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02003869 $attr['eas_access'] = (in_array('eas', $_data['protocol_access'])) ? 1 : 0;
3870 $attr['dav_access'] = (in_array('dav', $_data['protocol_access'])) ? 1 : 0;
3871 }
3872 else {
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +01003873 foreach ($is_now as $key => $value){
3874 $attr[$key] = $is_now[$key];
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02003875 }
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +01003876 }
3877 if (isset($_data['acl'])) {
3878 $_data['acl'] = (array)$_data['acl'];
3879 $attr['acl_spam_alias'] = (in_array('spam_alias', $_data['acl'])) ? 1 : 0;
3880 $attr['acl_tls_policy'] = (in_array('tls_policy', $_data['acl'])) ? 1 : 0;
3881 $attr['acl_spam_score'] = (in_array('spam_score', $_data['acl'])) ? 1 : 0;
3882 $attr['acl_spam_policy'] = (in_array('spam_policy', $_data['acl'])) ? 1 : 0;
3883 $attr['acl_delimiter_action'] = (in_array('delimiter_action', $_data['acl'])) ? 1 : 0;
3884 $attr['acl_syncjobs'] = (in_array('syncjobs', $_data['acl'])) ? 1 : 0;
3885 $attr['acl_eas_reset'] = (in_array('eas_reset', $_data['acl'])) ? 1 : 0;
3886 $attr['acl_sogo_profile_reset'] = (in_array('sogo_profile_reset', $_data['acl'])) ? 1 : 0;
3887 $attr['acl_pushover'] = (in_array('pushover', $_data['acl'])) ? 1 : 0;
3888 $attr['acl_quarantine'] = (in_array('quarantine', $_data['acl'])) ? 1 : 0;
3889 $attr['acl_quarantine_attachments'] = (in_array('quarantine_attachments', $_data['acl'])) ? 1 : 0;
3890 $attr['acl_quarantine_notification'] = (in_array('quarantine_notification', $_data['acl'])) ? 1 : 0;
3891 $attr['acl_quarantine_category'] = (in_array('quarantine_category', $_data['acl'])) ? 1 : 0;
3892 $attr['acl_app_passwds'] = (in_array('app_passwds', $_data['acl'])) ? 1 : 0;
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02003893 $attr['acl_pw_reset'] = (in_array('pw_reset', $_data['acl'])) ? 1 : 0;
3894 } else {
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +01003895 foreach ($is_now as $key => $value){
3896 $attr[$key] = $is_now[$key];
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02003897 }
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +01003898 }
3899
3900
3901 // update template
3902 $stmt = $pdo->prepare("UPDATE `templates`
3903 SET `template` = :template, `attributes` = :attributes
3904 WHERE id = :id");
3905 $stmt->execute(array(
3906 ":id" => $id ,
3907 ":template" => $_data["template"] ,
3908 ":attributes" => json_encode($attr)
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02003909 ));
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +01003910 }
3911
3912
3913 $_SESSION['return'][] = array(
3914 'type' => 'success',
3915 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
3916 'msg' => array('template_modified', $_data["template"])
3917 );
3918 return true;
3919 break;
Matthias Andreas Benkard9dabdab2024-03-06 13:12:09 +01003920 case 'mailbox_custom_attribute':
3921 $_data['attribute'] = isset($_data['attribute']) ? $_data['attribute'] : array();
3922 $_data['attribute'] = is_array($_data['attribute']) ? $_data['attribute'] : array($_data['attribute']);
3923 $_data['attribute'] = array_map(function($value) { return str_replace(' ', '', $value); }, $_data['attribute']);
3924 $_data['value'] = isset($_data['value']) ? $_data['value'] : array();
3925 $_data['value'] = is_array($_data['value']) ? $_data['value'] : array($_data['value']);
3926 $attributes = (object)array_combine($_data['attribute'], $_data['value']);
3927 $mailboxes = is_array($_data['mailboxes']) ? $_data['mailboxes'] : array($_data['mailboxes']);
3928
3929 foreach ($mailboxes as $mailbox) {
3930 if (!filter_var($mailbox, FILTER_VALIDATE_EMAIL)) {
3931 $_SESSION['return'][] = array(
3932 'type' => 'danger',
3933 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
3934 'msg' => array('username_invalid', $mailbox)
3935 );
3936 continue;
3937 }
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02003938 $is_now = mailbox('get', 'mailbox_details', $mailbox);
Matthias Andreas Benkard9dabdab2024-03-06 13:12:09 +01003939 if(!empty($is_now)){
3940 if (!hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $is_now['domain'])) {
3941 $_SESSION['return'][] = array(
3942 'type' => 'danger',
3943 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
3944 'msg' => 'access_denied'
3945 );
3946 continue;
3947 }
3948 }
3949 else {
3950 $_SESSION['return'][] = array(
3951 'type' => 'danger',
3952 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
3953 'msg' => 'access_denied'
3954 );
3955 continue;
3956 }
3957
3958
3959 $stmt = $pdo->prepare("UPDATE `mailbox`
3960 SET `custom_attributes` = :custom_attributes
3961 WHERE username = :username");
3962 $stmt->execute(array(
3963 ":username" => $mailbox,
3964 ":custom_attributes" => json_encode($attributes)
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02003965 ));
3966
Matthias Andreas Benkard9dabdab2024-03-06 13:12:09 +01003967 $_SESSION['return'][] = array(
3968 'type' => 'success',
3969 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
3970 'msg' => array('mailbox_modified', $mailbox)
3971 );
3972 }
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02003973
3974 return true;
3975 break;
3976 case 'mta_sts':
3977 if (!is_array($_data['domains'])) {
3978 $domains = array();
3979 $domains[] = $_data['domains'];
3980 }
3981 else {
3982 $domains = $_data['domains'];
3983 }
3984
3985 foreach ($domains as $domain) {
3986 $domain = idn_to_ascii(strtolower(trim($domain)), 0, INTL_IDNA_VARIANT_UTS46);
3987
3988 if (!hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $domain)) {
3989 $_SESSION['return'][] = array(
3990 'type' => 'danger',
3991 'log' => array(__FUNCTION__, $_action, $_type, $_data, $_attr),
3992 'msg' => 'access_denied'
3993 );
3994 continue;
3995 }
3996
3997 $is_now = mailbox('get', 'mta_sts', $domain);
3998 if (!empty($is_now)) {
3999 $version = (isset($_data['version'])) ? strtolower($_data['version']) : $is_now['version'];
4000 $active = (isset($_data['active'])) ? intval($_data['active']) : $is_now['active'];
4001 $active = ($active == 1) ? 1 : 0;
4002 $mode = (isset($_data['mode'])) ? strtolower($_data['mode']) : $is_now['mode'];
4003 $mx = (isset($_data['mx'])) ? explode(",", preg_replace('/\s+/', '', $_data['mx'])) : $is_now['mx'];
4004 $max_age = (isset($_data['max_age'])) ? intval($_data['max_age']) : $is_now['max_age'];
4005
4006 // Update ID if neccesary
4007 if ($version != strtolower($is_now['version']) ||
4008 $mode != strtolower($is_now['mode']) ||
4009 $mx != $is_now['mx'] ||
4010 $max_age != $is_now['max_age']) {
4011 $id = date('YmdHis');
4012 } else {
4013 $id = $is_now['id'];
4014 }
4015
4016 } else {
4017 $_SESSION['return'][] = array(
4018 'type' => 'danger',
4019 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
4020 'msg' => 'access_denied'
4021 );
4022 continue;
4023 }
4024
4025 if (empty($version) || !in_array($version, array('stsv1'))) {
4026 $_SESSION['return'][] = array(
4027 'type' => 'danger',
4028 'log' => array(__FUNCTION__, $_action, $_type, $_data, $_attr),
4029 'msg' => array('version_invalid', htmlspecialchars($version))
4030 );
4031 continue;
4032 }
4033 if (empty($mode) || !in_array($mode, array('enforce', 'testing', 'none'))) {
4034 $_SESSION['return'][] = array(
4035 'type' => 'danger',
4036 'log' => array(__FUNCTION__, $_action, $_type, $_data, $_attr),
4037 'msg' => array('mode_invalid', htmlspecialchars($domain))
4038 );
4039 continue;
4040 }
4041 if (empty($max_age) || $max_age < 0 || $max_age > 31557600) {
4042 $_SESSION['return'][] = array(
4043 'type' => 'danger',
4044 'log' => array(__FUNCTION__, $_action, $_type, $_data, $_attr),
4045 'msg' => array('max_age_invalid', htmlspecialchars($domain))
4046 );
4047 continue;
4048 }
4049 foreach ($mx as $index => $mx_domain) {
4050 $mx_domain = idn_to_ascii(strtolower(trim($mx_domain)), 0, INTL_IDNA_VARIANT_UTS46);
4051 $invalid_mx = false;
4052 if (!is_valid_domain_name($mx_domain, array('allow_wildcard' => true))) {
4053 $invalid_mx = $mx_domain;
4054 break;
4055 }
4056 }
4057 if ($invalid_mx) {
4058 $_SESSION['return'][] = array(
4059 'type' => 'danger',
4060 'log' => array(__FUNCTION__, $_action, $_type, $_data, $_attr),
4061 'msg' => array('mx_invalid', htmlspecialchars($invalid_mx))
4062 );
4063 continue;
4064 }
4065
4066 try {
4067 $stmt = $pdo->prepare("UPDATE `mta_sts` SET `id` = :id, `version` = :version, `mode` = :mode, `mx` = :mx, `max_age` = :max_age, `active` = :active WHERE `domain` = :domain");
4068 $stmt->execute(array(
4069 ':id' => $id,
4070 ':domain' => $domain,
4071 ':version' => $version,
4072 ':mode' => $mode,
4073 ':mx' => implode(",", $mx),
4074 ':max_age' => $max_age,
4075 ':active' => $active
4076 ));
4077 } catch (PDOException $e) {
4078 $_SESSION['return'][] = array(
4079 'type' => 'danger',
4080 'log' => array(__FUNCTION__, $_action, $_type, $_data),
4081 'msg' => $e->getMessage()
4082 );
4083 continue;
4084 }
4085
4086 $_SESSION['return'][] = array(
4087 'type' => 'success',
4088 'log' => array(__FUNCTION__, $_action, $_type, $_data, $_attr),
4089 'msg' => array('object_modified', $domain)
4090 );
4091 }
4092
Matthias Andreas Benkard9dabdab2024-03-06 13:12:09 +01004093 return true;
4094 break;
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01004095 case 'resource':
4096 if (!is_array($_data['name'])) {
4097 $names = array();
4098 $names[] = $_data['name'];
4099 }
4100 else {
4101 $names = $_data['name'];
4102 }
4103 foreach ($names as $name) {
4104 $is_now = mailbox('get', 'resource_details', $name);
4105 if (!empty($is_now)) {
4106 $active = (isset($_data['active'])) ? intval($_data['active']) : $is_now['active'];
4107 $multiple_bookings = (isset($_data['multiple_bookings'])) ? intval($_data['multiple_bookings']) : $is_now['multiple_bookings'];
4108 $description = (!empty($_data['description'])) ? $_data['description'] : $is_now['description'];
4109 $kind = (!empty($_data['kind'])) ? $_data['kind'] : $is_now['kind'];
4110 }
4111 else {
4112 $_SESSION['return'][] = array(
4113 'type' => 'danger',
4114 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
4115 'msg' => array('resource_invalid', htmlspecialchars($name))
4116 );
4117 continue;
4118 }
4119 if (!filter_var($name, FILTER_VALIDATE_EMAIL)) {
4120 $_SESSION['return'][] = array(
4121 'type' => 'danger',
4122 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
4123 'msg' => array('resource_invalid', htmlspecialchars($name))
4124 );
4125 continue;
4126 }
4127 if (!isset($multiple_bookings) || $multiple_bookings < -1) {
4128 $multiple_bookings = -1;
4129 }
4130 if (empty($description)) {
4131 $_SESSION['return'][] = array(
4132 'type' => 'danger',
4133 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
4134 'msg' => array('description_invalid', htmlspecialchars($name))
4135 );
4136 continue;
4137 }
4138 if ($kind != 'location' && $kind != 'group' && $kind != 'thing') {
4139 $_SESSION['return'][] = array(
4140 'type' => 'danger',
4141 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
4142 'msg' => array('resource_invalid', htmlspecialchars($name))
4143 );
4144 continue;
4145 }
4146 if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $name)) {
4147 $_SESSION['return'][] = array(
4148 'type' => 'danger',
4149 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
4150 'msg' => 'access_denied'
4151 );
4152 continue;
4153 }
4154 $stmt = $pdo->prepare("UPDATE `mailbox` SET
4155 `active` = :active,
4156 `name`= :description,
4157 `kind`= :kind,
4158 `multiple_bookings`= :multiple_bookings
4159 WHERE `username` = :name");
4160 $stmt->execute(array(
4161 ':active' => $active,
4162 ':description' => $description,
4163 ':multiple_bookings' => $multiple_bookings,
4164 ':kind' => $kind,
4165 ':name' => $name
4166 ));
4167 $_SESSION['return'][] = array(
4168 'type' => 'success',
4169 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
4170 'msg' => array('resource_modified', htmlspecialchars($name))
4171 );
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02004172
4173 // Track affected mailboxes for SOGo update
4174 $update_sogo_mailboxes[] = $name;
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01004175 }
4176 break;
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02004177 case 'domain_wide_footer':
Matthias Andreas Benkard9dabdab2024-03-06 13:12:09 +01004178 if (!is_array($_data['domains'])) {
4179 $domains = array();
4180 $domains[] = $_data['domains'];
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +01004181 }
Matthias Andreas Benkard9dabdab2024-03-06 13:12:09 +01004182 else {
4183 $domains = $_data['domains'];
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +01004184 }
4185
4186 $footers = array();
Matthias Andreas Benkard9dabdab2024-03-06 13:12:09 +01004187 $footers['html'] = isset($_data['html']) ? $_data['html'] : '';
4188 $footers['plain'] = isset($_data['plain']) ? $_data['plain'] : '';
4189 $footers['skip_replies'] = isset($_data['skip_replies']) ? (int)$_data['skip_replies'] : 0;
4190 $footers['mbox_exclude'] = array();
4191 $footers['alias_domain_exclude'] = array();
4192 if (isset($_data["exclude"])){
4193 if (!is_array($_data["exclude"])) {
4194 $_data["exclude"] = array($_data["exclude"]);
4195 }
4196 foreach ($_data["exclude"] as $exclude) {
4197 if (filter_var($exclude, FILTER_VALIDATE_EMAIL)) {
4198 $stmt = $pdo->prepare("SELECT `address` FROM `alias` WHERE `address` = :address
4199 UNION
4200 SELECT `username` FROM `mailbox` WHERE `username` = :username");
4201 $stmt->execute(array(
4202 ':address' => $exclude,
4203 ':username' => $exclude,
4204 ));
4205 $row = $stmt->fetch(PDO::FETCH_ASSOC);
4206 if(!$row){
4207 $_SESSION['return'][] = array(
4208 'type' => 'danger',
4209 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
4210 'msg' => array('username_invalid', $exclude)
4211 );
4212 continue;
4213 }
4214 array_push($footers['mbox_exclude'], $exclude);
4215 }
4216 elseif (is_valid_domain_name($exclude)) {
4217 $stmt = $pdo->prepare("SELECT `alias_domain` FROM `alias_domain` WHERE `alias_domain` = :alias_domain");
4218 $stmt->execute(array(
4219 ':alias_domain' => $exclude,
4220 ));
4221 $row = $stmt->fetch(PDO::FETCH_ASSOC);
4222 if(!$row){
4223 $_SESSION['return'][] = array(
4224 'type' => 'danger',
4225 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
4226 'msg' => array('username_invalid', $exclude)
4227 );
4228 continue;
4229 }
4230 array_push($footers['alias_domain_exclude'], $exclude);
4231 }
4232 else {
4233 $_SESSION['return'][] = array(
4234 'type' => 'danger',
4235 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
4236 'msg' => array('username_invalid', $exclude)
4237 );
4238 }
4239 }
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +01004240 }
Matthias Andreas Benkard9dabdab2024-03-06 13:12:09 +01004241 foreach ($domains as $domain) {
4242 $domain = idn_to_ascii(strtolower(trim($domain)), 0, INTL_IDNA_VARIANT_UTS46);
4243 if (!is_valid_domain_name($domain)) {
4244 $_SESSION['return'][] = array(
4245 'type' => 'danger',
4246 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
4247 'msg' => 'domain_invalid'
4248 );
4249 return false;
4250 }
4251 if (!hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $domain)) {
4252 $_SESSION['return'][] = array(
4253 'type' => 'danger',
4254 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
4255 'msg' => 'access_denied'
4256 );
4257 return false;
4258 }
4259
4260 try {
4261 $stmt = $pdo->prepare("DELETE FROM `domain_wide_footer` WHERE `domain`= :domain");
4262 $stmt->execute(array(':domain' => $domain));
4263 $stmt = $pdo->prepare("INSERT INTO `domain_wide_footer` (`domain`, `html`, `plain`, `mbox_exclude`, `alias_domain_exclude`, `skip_replies`) VALUES (:domain, :html, :plain, :mbox_exclude, :alias_domain_exclude, :skip_replies)");
4264 $stmt->execute(array(
4265 ':domain' => $domain,
4266 ':html' => $footers['html'],
4267 ':plain' => $footers['plain'],
4268 ':mbox_exclude' => json_encode($footers['mbox_exclude']),
4269 ':alias_domain_exclude' => json_encode($footers['alias_domain_exclude']),
4270 ':skip_replies' => $footers['skip_replies'],
4271 ));
4272 }
4273 catch (PDOException $e) {
4274 $_SESSION['return'][] = array(
4275 'type' => 'danger',
4276 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
4277 'msg' => $e->getMessage()
4278 );
4279 return false;
4280 }
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +01004281 $_SESSION['return'][] = array(
Matthias Andreas Benkard9dabdab2024-03-06 13:12:09 +01004282 'type' => 'success',
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +01004283 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
Matthias Andreas Benkard9dabdab2024-03-06 13:12:09 +01004284 'msg' => array('domain_footer_modified', htmlspecialchars($domain))
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +01004285 );
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +01004286 }
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +01004287 break;
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01004288 }
4289 break;
4290 case 'get':
4291 switch ($_type) {
4292 case 'sender_acl_handles':
4293 if ($_SESSION['mailcow_cc_role'] != "admin" && $_SESSION['mailcow_cc_role'] != "domainadmin") {
4294 return false;
4295 }
4296 $data['sender_acl_domains']['ro'] = array();
4297 $data['sender_acl_domains']['rw'] = array();
4298 $data['sender_acl_domains']['selectable'] = array();
4299 $data['sender_acl_addresses']['ro'] = array();
4300 $data['sender_acl_addresses']['rw'] = array();
4301 $data['sender_acl_addresses']['selectable'] = array();
4302 $data['fixed_sender_aliases'] = array();
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02004303 $data['fixed_sender_aliases_allowed'] = array();
4304 $data['fixed_sender_aliases_blocked'] = array();
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01004305 $data['external_sender_aliases'] = array();
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02004306 // Fixed addresses - split by sender_allowed status
4307 $stmt = $pdo->prepare("SELECT `address`, `sender_allowed` FROM `alias` WHERE `goto` REGEXP :goto AND `address` NOT LIKE '@%'");
4308 $stmt->execute(array(':goto' => '(^|,)'.preg_quote($_data, '/').'($|,)'));
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01004309 $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
4310 while ($row = array_shift($rows)) {
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02004311 // Keep old array for backward compatibility
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01004312 $data['fixed_sender_aliases'][] = $row['address'];
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02004313 // Split into allowed/blocked for proper display
4314 if ($row['sender_allowed'] == '1') {
4315 $data['fixed_sender_aliases_allowed'][] = $row['address'];
4316 } else {
4317 $data['fixed_sender_aliases_blocked'][] = $row['address'];
4318 }
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01004319 }
4320 $stmt = $pdo->prepare("SELECT CONCAT(`local_part`, '@', `alias_domain`.`alias_domain`) AS `alias_domain_alias` FROM `mailbox`, `alias_domain`
4321 WHERE `alias_domain`.`target_domain` = `mailbox`.`domain`
4322 AND `mailbox`.`username` = :username");
4323 $stmt->execute(array(':username' => $_data));
4324 $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
4325 while ($row = array_shift($rows)) {
4326 if (!empty($row['alias_domain_alias'])) {
4327 $data['fixed_sender_aliases'][] = $row['alias_domain_alias'];
4328 }
4329 }
4330 // External addresses
4331 $stmt = $pdo->prepare("SELECT `send_as` as `send_as_external` FROM `sender_acl` WHERE `logged_in_as` = :logged_in_as AND `external` = '1'");
4332 $stmt->execute(array(':logged_in_as' => $_data));
4333 $exernal_rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
4334 while ($row = array_shift($exernal_rows)) {
4335 if (!empty($row['send_as_external'])) {
4336 $data['external_sender_aliases'][] = $row['send_as_external'];
4337 }
4338 }
4339 // Return array $data['sender_acl_domains/addresses']['ro'] with read-only objects
4340 // Return array $data['sender_acl_domains/addresses']['rw'] with read-write objects (can be deleted)
4341 $stmt = $pdo->prepare("SELECT REPLACE(`send_as`, '@', '') AS `send_as` FROM `sender_acl` WHERE `logged_in_as` = :logged_in_as AND `external` = '0' AND (`send_as` LIKE '@%' OR `send_as` = '*')");
4342 $stmt->execute(array(':logged_in_as' => $_data));
4343 $domain_rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
4344 while ($domain_row = array_shift($domain_rows)) {
4345 if (is_valid_domain_name($domain_row['send_as']) && !hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $domain_row['send_as'])) {
4346 $data['sender_acl_domains']['ro'][] = $domain_row['send_as'];
4347 continue;
4348 }
4349 if (is_valid_domain_name($domain_row['send_as']) && hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $domain_row['send_as'])) {
4350 $data['sender_acl_domains']['rw'][] = $domain_row['send_as'];
4351 continue;
4352 }
4353 if ($domain_row['send_as'] == '*' && $_SESSION['mailcow_cc_role'] != 'admin') {
4354 $data['sender_acl_domains']['ro'][] = $domain_row['send_as'];
4355 }
4356 if ($domain_row['send_as'] == '*' && $_SESSION['mailcow_cc_role'] == 'admin') {
4357 $data['sender_acl_domains']['rw'][] = $domain_row['send_as'];
4358 }
4359 }
4360 $stmt = $pdo->prepare("SELECT `send_as` FROM `sender_acl` WHERE `logged_in_as` = :logged_in_as AND `external` = '0' AND (`send_as` NOT LIKE '@%' AND `send_as` != '*')");
4361 $stmt->execute(array(':logged_in_as' => $_data));
4362 $address_rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
4363 while ($address_row = array_shift($address_rows)) {
4364 if (filter_var($address_row['send_as'], FILTER_VALIDATE_EMAIL) && !hasAliasObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $address_row['send_as'])) {
4365 $data['sender_acl_addresses']['ro'][] = $address_row['send_as'];
4366 continue;
4367 }
4368 if (filter_var($address_row['send_as'], FILTER_VALIDATE_EMAIL) && hasAliasObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $address_row['send_as'])) {
4369 $data['sender_acl_addresses']['rw'][] = $address_row['send_as'];
4370 continue;
4371 }
4372 }
4373 $stmt = $pdo->prepare("SELECT `domain` FROM `domain`
4374 WHERE `domain` NOT IN (
4375 SELECT REPLACE(`send_as`, '@', '') FROM `sender_acl`
4376 WHERE `logged_in_as` = :logged_in_as1
4377 AND `external` = '0'
4378 AND `send_as` LIKE '@%')
4379 UNION
4380 SELECT '*' FROM `domain`
4381 WHERE '*' NOT IN (
4382 SELECT `send_as` FROM `sender_acl`
4383 WHERE `logged_in_as` = :logged_in_as2
4384 AND `external` = '0'
4385 )");
4386 $stmt->execute(array(
4387 ':logged_in_as1' => $_data,
4388 ':logged_in_as2' => $_data
4389 ));
4390 $rows_domain = $stmt->fetchAll(PDO::FETCH_ASSOC);
4391 while ($row_domain = array_shift($rows_domain)) {
4392 if (is_valid_domain_name($row_domain['domain']) && hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $row_domain['domain'])) {
4393 $data['sender_acl_domains']['selectable'][] = $row_domain['domain'];
4394 continue;
4395 }
4396 if ($row_domain['domain'] == '*' && $_SESSION['mailcow_cc_role'] == 'admin') {
4397 $data['sender_acl_domains']['selectable'][] = $row_domain['domain'];
4398 continue;
4399 }
4400 }
4401 $stmt = $pdo->prepare("SELECT `address` FROM `alias`
4402 WHERE `goto` != :goto
4403 AND `address` NOT IN (
4404 SELECT `send_as` FROM `sender_acl`
4405 WHERE `logged_in_as` = :logged_in_as
4406 AND `external` = '0'
4407 AND `send_as` NOT LIKE '@%')");
4408 $stmt->execute(array(
4409 ':logged_in_as' => $_data,
4410 ':goto' => $_data
4411 ));
4412 $rows_mbox = $stmt->fetchAll(PDO::FETCH_ASSOC);
4413 while ($row = array_shift($rows_mbox)) {
4414 // Aliases are not selectable
4415 if (in_array($row['address'], $data['fixed_sender_aliases'])) {
4416 continue;
4417 }
4418 if (filter_var($row['address'], FILTER_VALIDATE_EMAIL) && hasAliasObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $row['address'])) {
4419 $data['sender_acl_addresses']['selectable'][] = $row['address'];
4420 }
4421 }
4422 return $data;
4423 break;
4424 case 'mailboxes':
4425 $mailboxes = array();
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +01004426 if (isset($_extra) && is_array($_extra) && isset($_data)) {
4427 // get by domain and tags
4428 $tags = is_array($_extra) ? $_extra : array();
4429
4430 $sql = "";
4431 foreach ($tags as $key => $tag) {
4432 $sql = $sql."SELECT DISTINCT `username` FROM `tags_mailbox` WHERE `username` LIKE ? AND `tag_name` LIKE ?"; // distinct, avoid duplicates
4433 if ($key === array_key_last($tags)) break;
4434 $sql = $sql.' UNION DISTINCT '; // combine querys with union - distinct, avoid duplicates
4435 }
4436
4437 // prepend domain to array
4438 $params = array();
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02004439 foreach ($tags as $key => $val){
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +01004440 array_push($params, '%'.$_data.'%');
4441 array_push($params, '%'.$val.'%');
4442 }
4443 $stmt = $pdo->prepare($sql);
4444 $stmt->execute($params);
4445
4446 $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
4447 while($row = array_shift($rows)) {
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02004448 if (hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], explode('@', $row['username'])[1]))
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +01004449 $mailboxes[] = $row['username'];
4450 }
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01004451 }
4452 elseif (isset($_data) && hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +01004453 // get by domain
Matthias Andreas Benkard8f915942021-08-16 10:57:25 +02004454 $stmt = $pdo->prepare("SELECT `username` FROM `mailbox` WHERE (`kind` = '' OR `kind` = NULL) AND `domain` = :domain");
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01004455 $stmt->execute(array(
4456 ':domain' => $_data,
4457 ));
4458 $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
4459 while($row = array_shift($rows)) {
4460 $mailboxes[] = $row['username'];
4461 }
4462 }
4463 else {
Matthias Andreas Benkard8f915942021-08-16 10:57:25 +02004464 $stmt = $pdo->prepare("SELECT `username` FROM `mailbox` WHERE (`kind` = '' OR `kind` = NULL) AND (`domain` IN (SELECT `domain` FROM `domain_admins` WHERE `active` = '1' AND `username` = :username) OR 'admin' = :role)");
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01004465 $stmt->execute(array(
4466 ':username' => $_SESSION['mailcow_cc_username'],
4467 ':role' => $_SESSION['mailcow_cc_role'],
4468 ));
4469 $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
4470 while($row = array_shift($rows)) {
4471 $mailboxes[] = $row['username'];
4472 }
4473 }
4474 return $mailboxes;
4475 break;
4476 case 'tls_policy':
4477 $attrs = array();
4478 if (isset($_data) && filter_var($_data, FILTER_VALIDATE_EMAIL)) {
4479 if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
4480 return false;
4481 }
4482 }
4483 else {
4484 $_data = $_SESSION['mailcow_cc_username'];
4485 }
4486 $stmt = $pdo->prepare("SELECT `attributes` FROM `mailbox` WHERE `username` = :username");
4487 $stmt->execute(array(':username' => $_data));
4488 $attrs = $stmt->fetch(PDO::FETCH_ASSOC);
4489 $attrs = json_decode($attrs['attributes'], true);
4490 return array(
4491 'tls_enforce_in' => $attrs['tls_enforce_in'],
4492 'tls_enforce_out' => $attrs['tls_enforce_out']
4493 );
4494 break;
4495 case 'quarantine_notification':
4496 $attrs = array();
4497 if (isset($_data) && filter_var($_data, FILTER_VALIDATE_EMAIL)) {
4498 if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
4499 return false;
4500 }
4501 }
4502 else {
4503 $_data = $_SESSION['mailcow_cc_username'];
4504 }
4505 $stmt = $pdo->prepare("SELECT `attributes` FROM `mailbox` WHERE `username` = :username");
4506 $stmt->execute(array(':username' => $_data));
4507 $attrs = $stmt->fetch(PDO::FETCH_ASSOC);
4508 $attrs = json_decode($attrs['attributes'], true);
4509 return $attrs['quarantine_notification'];
4510 break;
4511 case 'quarantine_category':
4512 $attrs = array();
4513 if (isset($_data) && filter_var($_data, FILTER_VALIDATE_EMAIL)) {
4514 if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
4515 return false;
4516 }
4517 }
4518 else {
4519 $_data = $_SESSION['mailcow_cc_username'];
4520 }
4521 $stmt = $pdo->prepare("SELECT `attributes` FROM `mailbox` WHERE `username` = :username");
4522 $stmt->execute(array(':username' => $_data));
4523 $attrs = $stmt->fetch(PDO::FETCH_ASSOC);
4524 $attrs = json_decode($attrs['attributes'], true);
4525 return $attrs['quarantine_category'];
4526 break;
4527 case 'filters':
4528 $filters = array();
4529 if (isset($_data) && filter_var($_data, FILTER_VALIDATE_EMAIL)) {
4530 if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
4531 return false;
4532 }
4533 }
4534 else {
4535 $_data = $_SESSION['mailcow_cc_username'];
4536 }
4537 $stmt = $pdo->prepare("SELECT `id` FROM `sieve_filters` WHERE `username` = :username");
4538 $stmt->execute(array(':username' => $_data));
4539 $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
4540 while($row = array_shift($rows)) {
4541 $filters[] = $row['id'];
4542 }
4543 return $filters;
4544 break;
4545 case 'global_filter_details':
4546 $global_filters = array();
4547 if ($_SESSION['mailcow_cc_role'] != "admin") {
4548 return false;
4549 }
4550 $global_filters['prefilter'] = file_get_contents('/global_sieve/before');
4551 $global_filters['postfilter'] = file_get_contents('/global_sieve/after');
4552 return $global_filters;
4553 break;
4554 case 'filter_details':
4555 $filter_details = array();
4556 if (!is_numeric($_data)) {
4557 return false;
4558 }
4559 $stmt = $pdo->prepare("SELECT CASE `script_name` WHEN 'active' THEN 1 ELSE 0 END AS `active`,
4560 id,
4561 username,
4562 filter_type,
4563 script_data,
4564 script_desc
4565 FROM `sieve_filters`
4566 WHERE `id` = :id");
4567 $stmt->execute(array(':id' => $_data));
4568 $filter_details = $stmt->fetch(PDO::FETCH_ASSOC);
4569 if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $filter_details['username'])) {
4570 return false;
4571 }
4572 return $filter_details;
4573 break;
4574 case 'active_user_sieve':
4575 $filter_details = array();
4576 if (isset($_data) && filter_var($_data, FILTER_VALIDATE_EMAIL)) {
4577 if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
4578 return false;
4579 }
4580 }
4581 else {
4582 $_data = $_SESSION['mailcow_cc_username'];
4583 }
4584 $exec_fields = array(
4585 'cmd' => 'sieve',
4586 'task' => 'list',
4587 'username' => $_data
4588 );
4589 $filters = docker('post', 'dovecot-mailcow', 'exec', $exec_fields);
4590 $filters = array_filter(preg_split("/(\r\n|\n|\r)/",$filters));
4591 foreach ($filters as $filter) {
4592 if (preg_match('/.+ ACTIVE/i', $filter)) {
4593 $exec_fields = array(
4594 'cmd' => 'sieve',
4595 'task' => 'print',
4596 'script_name' => substr($filter, 0, -7),
4597 'username' => $_data
4598 );
4599 $script = docker('post', 'dovecot-mailcow', 'exec', $exec_fields);
4600 // Remove first line
4601 return preg_replace('/^.+\n/', '', $script);
4602 }
4603 }
4604 return false;
4605 break;
4606 case 'syncjob_details':
4607 $syncjobdetails = array();
4608 if (!is_numeric($_data)) {
4609 return false;
4610 }
4611 if (isset($_extra) && in_array('no_log', $_extra)) {
4612 $field_query = $pdo->query('SHOW FIELDS FROM `imapsync` WHERE FIELD NOT IN ("returned_text", "password1")');
4613 $fields = $field_query->fetchAll(PDO::FETCH_ASSOC);
4614 while($field = array_shift($fields)) {
4615 $shown_fields[] = $field['Field'];
4616 }
Matthias Andreas Benkard8f915942021-08-16 10:57:25 +02004617 $stmt = $pdo->prepare("SELECT " . implode(',', (array)$shown_fields) . ",
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01004618 `active`
4619 FROM `imapsync` WHERE id = :id");
4620 }
4621 elseif (isset($_extra) && in_array('with_password', $_extra)) {
4622 $stmt = $pdo->prepare("SELECT *,
4623 `active`
4624 FROM `imapsync` WHERE id = :id");
4625 }
4626 else {
4627 $field_query = $pdo->query('SHOW FIELDS FROM `imapsync` WHERE FIELD NOT IN ("password1")');
4628 $fields = $field_query->fetchAll(PDO::FETCH_ASSOC);
4629 while($field = array_shift($fields)) {
4630 $shown_fields[] = $field['Field'];
4631 }
Matthias Andreas Benkard8f915942021-08-16 10:57:25 +02004632 $stmt = $pdo->prepare("SELECT " . implode(',', (array)$shown_fields) . ",
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01004633 `active`
4634 FROM `imapsync` WHERE id = :id");
4635 }
4636 $stmt->execute(array(':id' => $_data));
4637 $syncjobdetails = $stmt->fetch(PDO::FETCH_ASSOC);
4638 if (!empty($syncjobdetails['returned_text'])) {
4639 $syncjobdetails['log'] = $syncjobdetails['returned_text'];
4640 }
4641 else {
4642 $syncjobdetails['log'] = '';
4643 }
4644 unset($syncjobdetails['returned_text']);
4645 if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $syncjobdetails['user2'])) {
4646 return false;
4647 }
4648 return $syncjobdetails;
4649 break;
4650 case 'syncjobs':
4651 $syncjobdata = array();
4652 if (isset($_data) && filter_var($_data, FILTER_VALIDATE_EMAIL)) {
4653 if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
4654 return false;
4655 }
4656 }
4657 else {
4658 $_data = $_SESSION['mailcow_cc_username'];
4659 }
4660 $stmt = $pdo->prepare("SELECT `id` FROM `imapsync` WHERE `user2` = :username");
4661 $stmt->execute(array(':username' => $_data));
4662 $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
4663 while($row = array_shift($rows)) {
4664 $syncjobdata[] = $row['id'];
4665 }
4666 return $syncjobdata;
4667 break;
4668 case 'spam_score':
4669 $curl = curl_init();
4670 curl_setopt($curl, CURLOPT_UNIX_SOCKET_PATH, '/var/lib/rspamd/rspamd.sock');
4671 curl_setopt($curl, CURLOPT_URL,"http://rspamd/actions");
4672 curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
4673 $default_actions = curl_exec($curl);
4674 if (!curl_errno($curl)) {
4675 $data_array = json_decode($default_actions, true);
4676 curl_close($curl);
4677 foreach ($data_array as $data) {
4678 if ($data['action'] == 'reject') {
4679 $reject = $data['value'];
4680 continue;
4681 }
4682 elseif ($data['action'] == 'add header') {
4683 $add_header = $data['value'];
4684 continue;
4685 }
4686 }
4687 if (empty($add_header) || empty($reject)) {
4688 // Assume default, set warning
4689 $default = "5, 15";
4690 $_SESSION['return'][] = array(
4691 'type' => 'warning',
4692 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
4693 'msg' => 'Could not determine servers default spam score, assuming default'
4694 );
4695 }
4696 else {
4697 $default = $add_header . ', ' . $reject;
4698 }
4699 }
4700 else {
4701 // Assume default, set warning
4702 $default = "5, 15";
4703 $_SESSION['return'][] = array(
4704 'type' => 'warning',
4705 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
4706 'msg' => 'Could not determine servers default spam score, assuming default'
4707 );
4708 }
4709 curl_close($curl);
4710 $policydata = array();
4711 if (isset($_data) && filter_var($_data, FILTER_VALIDATE_EMAIL)) {
4712 if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
4713 return false;
4714 }
4715 }
4716 else {
4717 $_data = $_SESSION['mailcow_cc_username'];
4718 }
4719 $stmt = $pdo->prepare("SELECT `value` FROM `filterconf` WHERE `object` = :username AND
4720 (`option` = 'lowspamlevel' OR `option` = 'highspamlevel')");
4721 $stmt->execute(array(':username' => $_data));
4722 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
4723 if (empty($num_results)) {
4724 return $default;
4725 }
4726 else {
4727 $stmt = $pdo->prepare("SELECT `value` FROM `filterconf` WHERE `option` = 'highspamlevel' AND `object` = :username");
4728 $stmt->execute(array(':username' => $_data));
4729 $highspamlevel = $stmt->fetch(PDO::FETCH_ASSOC);
4730 $stmt = $pdo->prepare("SELECT `value` FROM `filterconf` WHERE `option` = 'lowspamlevel' AND `object` = :username");
4731 $stmt->execute(array(':username' => $_data));
4732 $lowspamlevel = $stmt->fetch(PDO::FETCH_ASSOC);
4733 return $lowspamlevel['value'].', '.$highspamlevel['value'];
4734 }
4735 break;
4736 case 'time_limited_aliases':
4737 $tladata = array();
4738 if (isset($_data) && filter_var($_data, FILTER_VALIDATE_EMAIL)) {
4739 if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
4740 return false;
4741 }
4742 }
4743 else {
4744 $_data = $_SESSION['mailcow_cc_username'];
4745 }
4746 $stmt = $pdo->prepare("SELECT `address`,
4747 `goto`,
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02004748 `description`,
Matthias Andreas Benkard8f915942021-08-16 10:57:25 +02004749 `validity`,
4750 `created`,
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02004751 `modified`,
4752 `permanent`
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01004753 FROM `spamalias`
4754 WHERE `goto` = :username
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02004755 AND (`validity` >= :unixnow
4756 OR `permanent` != 0)");
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01004757 $stmt->execute(array(':username' => $_data, ':unixnow' => time()));
4758 $tladata = $stmt->fetchAll(PDO::FETCH_ASSOC);
4759 return $tladata;
4760 break;
4761 case 'delimiter_action':
4762 $policydata = array();
4763 if (isset($_data) && filter_var($_data, FILTER_VALIDATE_EMAIL)) {
4764 if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
4765 return false;
4766 }
4767 }
4768 else {
4769 $_data = $_SESSION['mailcow_cc_username'];
4770 }
4771 try {
4772 if ($redis->hGet('RCPT_WANTS_SUBJECT_TAG', $_data)) {
4773 return "subject";
4774 }
4775 elseif ($redis->hGet('RCPT_WANTS_SUBFOLDER_TAG', $_data)) {
4776 return "subfolder";
4777 }
4778 else {
4779 return "none";
4780 }
4781 }
4782 catch (RedisException $e) {
4783 $_SESSION['return'][] = array(
4784 'type' => 'danger',
4785 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
4786 'msg' => array('redis_error', $e)
4787 );
4788 return false;
4789 }
4790 break;
4791 case 'resources':
4792 $resources = array();
4793 if (isset($_data) && !hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
4794 return false;
4795 }
4796 elseif (isset($_data) && hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
4797 $stmt = $pdo->prepare("SELECT `username` FROM `mailbox` WHERE `kind` REGEXP 'location|thing|group' AND `domain` = :domain");
4798 $stmt->execute(array(
4799 ':domain' => $_data,
4800 ));
4801 $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
4802 while($row = array_shift($rows)) {
4803 $resources[] = $row['username'];
4804 }
4805 }
4806 else {
4807 $stmt = $pdo->prepare("SELECT `username` FROM `mailbox` WHERE `kind` REGEXP 'location|thing|group' AND `domain` IN (SELECT `domain` FROM `domain_admins` WHERE `active` = '1' AND `username` = :username) OR 'admin' = :role");
4808 $stmt->execute(array(
4809 ':username' => $_SESSION['mailcow_cc_username'],
4810 ':role' => $_SESSION['mailcow_cc_role'],
4811 ));
4812 $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
4813 while($row = array_shift($rows)) {
4814 $resources[] = $row['username'];
4815 }
4816 }
4817 return $resources;
4818 break;
4819 case 'alias_domains':
4820 $aliasdomains = array();
4821 if (isset($_data) && !hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
4822 return false;
4823 }
4824 elseif (isset($_data) && hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
4825 $stmt = $pdo->prepare("SELECT `alias_domain` FROM `alias_domain` WHERE `target_domain` = :domain");
4826 $stmt->execute(array(
4827 ':domain' => $_data,
4828 ));
4829 $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
4830 while($row = array_shift($rows)) {
4831 $aliasdomains[] = $row['alias_domain'];
4832 }
4833 }
4834 else {
4835 $stmt = $pdo->prepare("SELECT `alias_domain` FROM `alias_domain` WHERE `target_domain` IN (SELECT `domain` FROM `domain_admins` WHERE `active` = '1' AND `username` = :username) OR 'admin' = :role");
4836 $stmt->execute(array(
4837 ':username' => $_SESSION['mailcow_cc_username'],
4838 ':role' => $_SESSION['mailcow_cc_role'],
4839 ));
4840 $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
4841 while($row = array_shift($rows)) {
4842 $aliasdomains[] = $row['alias_domain'];
4843 }
4844 }
4845 return $aliasdomains;
4846 break;
4847 case 'aliases':
4848 $aliases = array();
4849 if (!hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
4850 return false;
4851 }
Matthias Andreas Benkard9dabdab2024-03-06 13:12:09 +01004852 $stmt = $pdo->prepare("SELECT `id`, `address` FROM `alias` WHERE `address` != `goto` AND `domain` = :domain");
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01004853 $stmt->execute(array(
4854 ':domain' => $_data,
4855 ));
4856 $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
4857 while($row = array_shift($rows)) {
Matthias Andreas Benkard9dabdab2024-03-06 13:12:09 +01004858 if ($_extra == "address"){
4859 $aliases[] = $row['address'];
4860 } else {
4861 $aliases[] = $row['id'];
4862 }
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01004863 }
4864 return $aliases;
4865 break;
4866 case 'alias_details':
4867 $aliasdata = array();
4868 $stmt = $pdo->prepare("SELECT
4869 `id`,
4870 `domain`,
4871 `goto`,
4872 `address`,
4873 `public_comment`,
4874 `private_comment`,
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02004875 `internal`,
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01004876 `active`,
4877 `sogo_visible`,
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02004878 `sender_allowed`,
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01004879 `created`,
4880 `modified`
4881 FROM `alias`
4882 WHERE (`id` = :id OR `address` = :address) AND `address` != `goto`");
4883 $stmt->execute(array(
4884 ':id' => $_data,
4885 ':address' => $_data,
4886 ));
4887 $row = $stmt->fetch(PDO::FETCH_ASSOC);
4888 $stmt = $pdo->prepare("SELECT `target_domain` FROM `alias_domain` WHERE `alias_domain` = :domain");
4889 $stmt->execute(array(
4890 ':domain' => $row['domain'],
4891 ));
4892 $row_alias_domain = $stmt->fetch(PDO::FETCH_ASSOC);
4893 if (isset($row_alias_domain['target_domain']) && !empty($row_alias_domain['target_domain'])) {
4894 $aliasdata['in_primary_domain'] = $row_alias_domain['target_domain'];
4895 }
4896 else {
4897 $aliasdata['in_primary_domain'] = "";
4898 }
4899 $aliasdata['id'] = $row['id'];
4900 $aliasdata['domain'] = $row['domain'];
4901 $aliasdata['public_comment'] = $row['public_comment'];
4902 $aliasdata['private_comment'] = $row['private_comment'];
4903 $aliasdata['domain'] = $row['domain'];
4904 $aliasdata['goto'] = $row['goto'];
4905 $aliasdata['address'] = $row['address'];
4906 (!filter_var($aliasdata['address'], FILTER_VALIDATE_EMAIL)) ? $aliasdata['is_catch_all'] = 1 : $aliasdata['is_catch_all'] = 0;
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02004907 $aliasdata['internal'] = $row['internal'];
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01004908 $aliasdata['active'] = $row['active'];
4909 $aliasdata['active_int'] = $row['active'];
4910 $aliasdata['sogo_visible'] = $row['sogo_visible'];
4911 $aliasdata['sogo_visible_int'] = $row['sogo_visible'];
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02004912 $aliasdata['sender_allowed'] = $row['sender_allowed'];
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01004913 $aliasdata['created'] = $row['created'];
4914 $aliasdata['modified'] = $row['modified'];
4915 if (!hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $aliasdata['domain'])) {
4916 return false;
4917 }
4918 return $aliasdata;
4919 break;
4920 case 'alias_domain_details':
4921 $aliasdomaindata = array();
4922 $rl = ratelimit('get', 'domain', $_data);
4923 $stmt = $pdo->prepare("SELECT
4924 `alias_domain`,
4925 `target_domain`,
4926 `active`,
4927 `created`,
4928 `modified`
4929 FROM `alias_domain`
4930 WHERE `alias_domain` = :aliasdomain");
4931 $stmt->execute(array(
4932 ':aliasdomain' => $_data,
4933 ));
4934 $row = $stmt->fetch(PDO::FETCH_ASSOC);
4935 $stmt = $pdo->prepare("SELECT `backupmx` FROM `domain` WHERE `domain` = :target_domain");
4936 $stmt->execute(array(
4937 ':target_domain' => $row['target_domain']
4938 ));
4939 $row_parent = $stmt->fetch(PDO::FETCH_ASSOC);
4940 $aliasdomaindata['alias_domain'] = $row['alias_domain'];
4941 $aliasdomaindata['parent_is_backupmx'] = $row_parent['backupmx'];
4942 $aliasdomaindata['target_domain'] = $row['target_domain'];
4943 $aliasdomaindata['active'] = $row['active'];
4944 $aliasdomaindata['active_int'] = $row['active'];
4945 $aliasdomaindata['rl'] = $rl;
4946 $aliasdomaindata['created'] = $row['created'];
4947 $aliasdomaindata['modified'] = $row['modified'];
4948 if (!hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $aliasdomaindata['target_domain'])) {
4949 return false;
4950 }
4951 return $aliasdomaindata;
4952 break;
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +01004953 case 'shared_aliases':
4954 $shared_aliases = array();
4955 $stmt = $pdo->query("SELECT `address` FROM `alias`
4956 WHERE `goto` REGEXP ','
4957 AND `address` NOT LIKE '@%'
4958 AND `goto` != `address`");
4959 $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
4960 while($row = array_shift($rows)) {
4961 $domain = explode("@", $row['address'])[1];
4962 if (hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $domain)) {
4963 $shared_aliases[] = $row['address'];
4964 }
4965 }
4966
4967 return $shared_aliases;
4968 break;
4969 case 'direct_aliases':
4970 $direct_aliases = array();
4971 $stmt = $pdo->query("SELECT `address` FROM `alias`
4972 WHERE `goto` NOT LIKE '%,%'
4973 AND `address` NOT LIKE '@%'
4974 AND `goto` != `address`");
4975 $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
4976
4977 while($row = array_shift($rows)) {
4978 $domain = explode("@", $row['address'])[1];
4979 if (hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $domain)) {
4980 $direct_aliases[] = $row['address'];
4981 }
4982 }
4983
4984 return $direct_aliases;
4985 break;
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01004986 case 'domains':
4987 $domains = array();
4988 if ($_SESSION['mailcow_cc_role'] != "admin" && $_SESSION['mailcow_cc_role'] != "domainadmin") {
4989 return false;
4990 }
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +01004991
4992 if (isset($_extra) && is_array($_extra)){
4993 // get by tags
4994 $tags = is_array($_extra) ? $_extra : array();
4995 // add % as prefix and suffix to every element for relative searching
4996 $tags = array_map(function($x){ return '%'.$x.'%'; }, $tags);
4997 $sql = "";
4998 foreach ($tags as $key => $tag) {
4999 $sql = $sql."SELECT DISTINCT `domain` FROM `tags_domain` WHERE `tag_name` LIKE ?"; // distinct, avoid duplicates
5000 if ($key === array_key_last($tags)) break;
5001 $sql = $sql.' UNION DISTINCT '; // combine querys with union - distinct, avoid duplicates
5002 }
5003 $stmt = $pdo->prepare($sql);
5004 $stmt->execute($tags);
5005
5006 $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
5007 while($row = array_shift($rows)) {
5008 if ($_SESSION['mailcow_cc_role'] == "admin")
5009 $domains[] = $row['domain'];
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02005010 elseif (hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $row['domain']))
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +01005011 $domains[] = $row['domain'];
5012 }
5013 } else {
5014 // get all
5015 $stmt = $pdo->prepare("SELECT `domain` FROM `domain`
5016 WHERE (`domain` IN (
5017 SELECT `domain` from `domain_admins`
5018 WHERE (`active`='1' AND `username` = :username))
5019 )
5020 OR 'admin'= :role");
5021 $stmt->execute(array(
5022 ':username' => $_SESSION['mailcow_cc_username'],
5023 ':role' => $_SESSION['mailcow_cc_role'],
5024 ));
5025 $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
5026 while($row = array_shift($rows)) {
5027 $domains[] = $row['domain'];
5028 }
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01005029 }
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +01005030
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01005031 return $domains;
5032 break;
5033 case 'domain_details':
5034 $domaindata = array();
5035 $_data = idn_to_ascii(strtolower(trim($_data)), 0, INTL_IDNA_VARIANT_UTS46);
5036 if (!hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
5037 return false;
5038 }
5039 $stmt = $pdo->prepare("SELECT `target_domain` FROM `alias_domain` WHERE `alias_domain` = :domain");
5040 $stmt->execute(array(
5041 ':domain' => $_data
5042 ));
5043 $row = $stmt->fetch(PDO::FETCH_ASSOC);
5044 if (!empty($row)) {
5045 $_data = $row['target_domain'];
5046 }
5047 $stmt = $pdo->prepare("SELECT
5048 `domain`,
5049 `description`,
5050 `aliases`,
5051 `mailboxes`,
5052 `defquota`,
5053 `maxquota`,
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +01005054 `created`,
5055 `modified`,
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01005056 `quota`,
5057 `relayhost`,
5058 `relay_all_recipients`,
5059 `relay_unknown_only`,
5060 `backupmx`,
5061 `gal`,
5062 `active`
5063 FROM `domain` WHERE `domain`= :domain");
5064 $stmt->execute(array(
5065 ':domain' => $_data
5066 ));
5067 $row = $stmt->fetch(PDO::FETCH_ASSOC);
5068 if (empty($row)) {
5069 return false;
5070 }
Matthias Andreas Benkard8f915942021-08-16 10:57:25 +02005071 $stmt = $pdo->prepare("SELECT COUNT(`username`) AS `count`,
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01005072 COALESCE(SUM(`quota`), 0) AS `in_use`
5073 FROM `mailbox`
Matthias Andreas Benkard8f915942021-08-16 10:57:25 +02005074 WHERE (`kind` = '' OR `kind` = NULL)
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01005075 AND `domain` = :domain");
5076 $stmt->execute(array(':domain' => $row['domain']));
5077 $MailboxDataDomain = $stmt->fetch(PDO::FETCH_ASSOC);
5078 $stmt = $pdo->prepare("SELECT SUM(bytes) AS `bytes_total`, SUM(messages) AS `msgs_total` FROM `quota2`
5079 WHERE `username` IN (
5080 SELECT `username` FROM `mailbox`
5081 WHERE `domain` = :domain
5082 );");
5083 $stmt->execute(array(':domain' => $row['domain']));
5084 $SumQuotaInUse = $stmt->fetch(PDO::FETCH_ASSOC);
5085 $rl = ratelimit('get', 'domain', $_data);
Matthias Andreas Benkard8f915942021-08-16 10:57:25 +02005086 $domaindata['max_new_mailbox_quota'] = ($row['quota'] * 1048576) - $MailboxDataDomain['in_use'];
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01005087 if ($domaindata['max_new_mailbox_quota'] > ($row['maxquota'] * 1048576)) {
5088 $domaindata['max_new_mailbox_quota'] = ($row['maxquota'] * 1048576);
5089 }
5090 $domaindata['def_new_mailbox_quota'] = $domaindata['max_new_mailbox_quota'];
5091 if ($domaindata['def_new_mailbox_quota'] > ($row['defquota'] * 1048576)) {
5092 $domaindata['def_new_mailbox_quota'] = ($row['defquota'] * 1048576);
5093 }
5094 $domaindata['quota_used_in_domain'] = $MailboxDataDomain['in_use'];
5095 if (!empty($SumQuotaInUse['bytes_total'])) {
5096 $domaindata['bytes_total'] = $SumQuotaInUse['bytes_total'];
5097 }
5098 else {
5099 $domaindata['bytes_total'] = 0;
5100 }
5101 if (!empty($SumQuotaInUse['msgs_total'])) {
5102 $domaindata['msgs_total'] = $SumQuotaInUse['msgs_total'];
5103 }
5104 else {
5105 $domaindata['msgs_total'] = 0;
5106 }
5107 $domaindata['mboxes_in_domain'] = $MailboxDataDomain['count'];
Matthias Andreas Benkard8f915942021-08-16 10:57:25 +02005108 $domaindata['mboxes_left'] = $row['mailboxes'] - $MailboxDataDomain['count'];
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01005109 $domaindata['domain_name'] = $row['domain'];
Matthias Andreas Benkard9dabdab2024-03-06 13:12:09 +01005110 $domaindata['domain_h_name'] = idn_to_utf8($row['domain']);
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01005111 $domaindata['description'] = $row['description'];
5112 $domaindata['max_num_aliases_for_domain'] = $row['aliases'];
5113 $domaindata['max_num_mboxes_for_domain'] = $row['mailboxes'];
5114 $domaindata['def_quota_for_mbox'] = $row['defquota'] * 1048576;
5115 $domaindata['max_quota_for_mbox'] = $row['maxquota'] * 1048576;
5116 $domaindata['max_quota_for_domain'] = $row['quota'] * 1048576;
5117 $domaindata['relayhost'] = $row['relayhost'];
5118 $domaindata['backupmx'] = $row['backupmx'];
5119 $domaindata['backupmx_int'] = $row['backupmx'];
5120 $domaindata['gal'] = $row['gal'];
5121 $domaindata['gal_int'] = $row['gal'];
5122 $domaindata['rl'] = $rl;
5123 $domaindata['active'] = $row['active'];
5124 $domaindata['active_int'] = $row['active'];
5125 $domaindata['relay_all_recipients'] = $row['relay_all_recipients'];
5126 $domaindata['relay_all_recipients_int'] = $row['relay_all_recipients'];
5127 $domaindata['relay_unknown_only'] = $row['relay_unknown_only'];
5128 $domaindata['relay_unknown_only_int'] = $row['relay_unknown_only'];
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +01005129 $domaindata['created'] = $row['created'];
5130 $domaindata['modified'] = $row['modified'];
Matthias Andreas Benkard8f915942021-08-16 10:57:25 +02005131 $stmt = $pdo->prepare("SELECT COUNT(`address`) AS `alias_count` FROM `alias`
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01005132 WHERE (`domain`= :domain OR `domain` IN (SELECT `alias_domain` FROM `alias_domain` WHERE `target_domain` = :domain2))
5133 AND `address` NOT IN (
5134 SELECT `username` FROM `mailbox`
5135 )");
5136 $stmt->execute(array(
5137 ':domain' => $_data,
5138 ':domain2' => $_data
5139 ));
5140 $AliasDataDomain = $stmt->fetch(PDO::FETCH_ASSOC);
5141 (isset($AliasDataDomain['alias_count'])) ? $domaindata['aliases_in_domain'] = $AliasDataDomain['alias_count'] : $domaindata['aliases_in_domain'] = "0";
Matthias Andreas Benkard8f915942021-08-16 10:57:25 +02005142 $domaindata['aliases_left'] = $row['aliases'] - $AliasDataDomain['alias_count'];
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01005143 if ($_SESSION['mailcow_cc_role'] == "admin")
5144 {
5145 $stmt = $pdo->prepare("SELECT GROUP_CONCAT(`username` SEPARATOR ', ') AS domain_admins FROM `domain_admins` WHERE `domain` = :domain");
5146 $stmt->execute(array(
5147 ':domain' => $_data
5148 ));
5149 $domain_admins = $stmt->fetch(PDO::FETCH_ASSOC);
5150 (isset($domain_admins['domain_admins'])) ? $domaindata['domain_admins'] = $domain_admins['domain_admins'] : $domaindata['domain_admins'] = "-";
5151 }
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +01005152 $stmt = $pdo->prepare("SELECT `tag_name`
5153 FROM `tags_domain` WHERE `domain`= :domain");
5154 $stmt->execute(array(
5155 ':domain' => $_data
5156 ));
5157 $tags = $stmt->fetchAll(PDO::FETCH_ASSOC);
5158 while ($tag = array_shift($tags)) {
5159 $domaindata['tags'][] = $tag['tag_name'];
5160 }
5161
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01005162 return $domaindata;
5163 break;
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +01005164 case 'domain_templates':
5165 if ($_SESSION['mailcow_cc_role'] != "admin" && $_SESSION['mailcow_cc_role'] != "domainadmin") {
5166 return false;
5167 }
5168 $_data = (isset($_data)) ? intval($_data) : null;
5169
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02005170 if (isset($_data)){
5171 $stmt = $pdo->prepare("SELECT * FROM `templates`
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +01005172 WHERE `id` = :id AND type = :type");
5173 $stmt->execute(array(
5174 ":id" => $_data,
5175 ":type" => "domain"
5176 ));
5177 $row = $stmt->fetch(PDO::FETCH_ASSOC);
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02005178
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +01005179 if (empty($row)){
5180 return false;
5181 }
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02005182
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +01005183 $row["attributes"] = json_decode($row["attributes"], true);
5184 return $row;
5185 }
5186 else {
5187 $stmt = $pdo->prepare("SELECT * FROM `templates` WHERE `type` = 'domain'");
5188 $stmt->execute();
5189 $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02005190
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +01005191 if (empty($rows)){
5192 return false;
5193 }
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02005194
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +01005195 foreach($rows as $key => $row){
5196 $rows[$key]["attributes"] = json_decode($row["attributes"], true);
5197 }
5198 return $rows;
5199 }
5200 break;
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01005201 case 'mailbox_details':
5202 if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
5203 return false;
5204 }
5205 $mailboxdata = array();
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01005206 if (preg_match('/y|yes/i', getenv('MASTER'))) {
5207 $stmt = $pdo->prepare("SELECT
5208 `domain`.`backupmx`,
5209 `mailbox`.`username`,
5210 `mailbox`.`name`,
5211 `mailbox`.`active`,
5212 `mailbox`.`domain`,
5213 `mailbox`.`local_part`,
5214 `mailbox`.`quota`,
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +01005215 `mailbox`.`created`,
5216 `mailbox`.`modified`,
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02005217 `mailbox`.`authsource`,
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01005218 `quota2`.`bytes`,
5219 `attributes`,
Matthias Andreas Benkard9dabdab2024-03-06 13:12:09 +01005220 `custom_attributes`,
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01005221 `quota2`.`messages`
5222 FROM `mailbox`, `quota2`, `domain`
Matthias Andreas Benkard8f915942021-08-16 10:57:25 +02005223 WHERE (`mailbox`.`kind` = '' OR `mailbox`.`kind` = NULL)
5224 AND `mailbox`.`username` = `quota2`.`username`
5225 AND `domain`.`domain` = `mailbox`.`domain`
5226 AND `mailbox`.`username` = :mailbox");
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01005227 }
5228 else {
5229 $stmt = $pdo->prepare("SELECT
5230 `domain`.`backupmx`,
5231 `mailbox`.`username`,
5232 `mailbox`.`name`,
5233 `mailbox`.`active`,
5234 `mailbox`.`domain`,
5235 `mailbox`.`local_part`,
5236 `mailbox`.`quota`,
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +01005237 `mailbox`.`created`,
5238 `mailbox`.`modified`,
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02005239 `mailbox`.`authsource`,
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01005240 `quota2replica`.`bytes`,
5241 `attributes`,
Matthias Andreas Benkard9dabdab2024-03-06 13:12:09 +01005242 `custom_attributes`,
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01005243 `quota2replica`.`messages`
5244 FROM `mailbox`, `quota2replica`, `domain`
Matthias Andreas Benkard8f915942021-08-16 10:57:25 +02005245 WHERE (`mailbox`.`kind` = '' OR `mailbox`.`kind` = NULL)
5246 AND `mailbox`.`username` = `quota2replica`.`username`
5247 AND `domain`.`domain` = `mailbox`.`domain`
5248 AND `mailbox`.`username` = :mailbox");
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01005249 }
5250 $stmt->execute(array(
5251 ':mailbox' => $_data,
5252 ));
5253 $row = $stmt->fetch(PDO::FETCH_ASSOC);
Matthias Andreas Benkard8f915942021-08-16 10:57:25 +02005254
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01005255 $mailboxdata['username'] = $row['username'];
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01005256 $mailboxdata['active'] = $row['active'];
5257 $mailboxdata['active_int'] = $row['active'];
5258 $mailboxdata['domain'] = $row['domain'];
Matthias Andreas Benkard8f915942021-08-16 10:57:25 +02005259 $mailboxdata['name'] = $row['name'];
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01005260 $mailboxdata['local_part'] = $row['local_part'];
5261 $mailboxdata['quota'] = $row['quota'];
Matthias Andreas Benkard8f915942021-08-16 10:57:25 +02005262 $mailboxdata['messages'] = $row['messages'];
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01005263 $mailboxdata['attributes'] = json_decode($row['attributes'], true);
Matthias Andreas Benkard9dabdab2024-03-06 13:12:09 +01005264 $mailboxdata['custom_attributes'] = json_decode($row['custom_attributes'], true);
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01005265 $mailboxdata['quota_used'] = intval($row['bytes']);
5266 $mailboxdata['percent_in_use'] = ($row['quota'] == 0) ? '- ' : round((intval($row['bytes']) / intval($row['quota'])) * 100);
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +01005267 $mailboxdata['created'] = $row['created'];
5268 $mailboxdata['modified'] = $row['modified'];
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02005269 $mailboxdata['authsource'] = ($row['authsource']) ? $row['authsource'] : 'mailcow';
Matthias Andreas Benkard8f915942021-08-16 10:57:25 +02005270
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01005271 if ($mailboxdata['percent_in_use'] === '- ') {
5272 $mailboxdata['percent_class'] = "info";
5273 }
5274 elseif ($mailboxdata['percent_in_use'] >= 90) {
5275 $mailboxdata['percent_class'] = "danger";
5276 }
5277 elseif ($mailboxdata['percent_in_use'] >= 75) {
5278 $mailboxdata['percent_class'] = "warning";
5279 }
5280 else {
5281 $mailboxdata['percent_class'] = "success";
5282 }
Matthias Andreas Benkard8f915942021-08-16 10:57:25 +02005283
5284 // Determine last logins
5285 $stmt = $pdo->prepare("SELECT MAX(`datetime`) AS `datetime`, `service` FROM `sasl_log`
5286 WHERE `username` = :mailbox
5287 GROUP BY `service` DESC");
5288 $stmt->execute(array(':mailbox' => $_data));
5289 $SaslLogsData = $stmt->fetchAll(PDO::FETCH_ASSOC);
5290 foreach ($SaslLogsData as $SaslLogs) {
5291 if ($SaslLogs['service'] == 'imap') {
5292 $last_imap_login = strtotime($SaslLogs['datetime']);
5293 }
5294 else if ($SaslLogs['service'] == 'smtp') {
5295 $last_smtp_login = strtotime($SaslLogs['datetime']);
5296 }
5297 else if ($SaslLogs['service'] == 'pop3') {
5298 $last_pop3_login = strtotime($SaslLogs['datetime']);
5299 }
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02005300 else if ($SaslLogs['service'] == 'SSO') {
5301 $last_sso_login = strtotime($SaslLogs['datetime']);
5302 }
Matthias Andreas Benkard8f915942021-08-16 10:57:25 +02005303 }
5304 if (!isset($last_imap_login) || $GLOBALS['SHOW_LAST_LOGIN'] === false) {
5305 $last_imap_login = 0;
5306 }
5307 if (!isset($last_smtp_login) || $GLOBALS['SHOW_LAST_LOGIN'] === false) {
5308 $last_smtp_login = 0;
5309 }
5310 if (!isset($last_pop3_login) || $GLOBALS['SHOW_LAST_LOGIN'] === false) {
5311 $last_pop3_login = 0;
5312 }
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02005313 if (!isset($last_sso_login) || $GLOBALS['SHOW_LAST_LOGIN'] === false) {
5314 $last_sso_login = 0;
5315 }
Matthias Andreas Benkard8f915942021-08-16 10:57:25 +02005316 $mailboxdata['last_imap_login'] = $last_imap_login;
5317 $mailboxdata['last_smtp_login'] = $last_smtp_login;
5318 $mailboxdata['last_pop3_login'] = $last_pop3_login;
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02005319 $mailboxdata['last_sso_login'] = $last_sso_login;
Matthias Andreas Benkard8f915942021-08-16 10:57:25 +02005320
5321 if (!isset($_extra) || $_extra != 'reduced') {
5322 $rl = ratelimit('get', 'mailbox', $_data);
5323 $stmt = $pdo->prepare("SELECT `maxquota`, `quota` FROM `domain` WHERE `domain` = :domain");
5324 $stmt->execute(array(':domain' => $row['domain']));
5325 $DomainQuota = $stmt->fetch(PDO::FETCH_ASSOC);
5326
5327 $stmt = $pdo->prepare("SELECT IFNULL(COUNT(`active`), 0) AS `pushover_active` FROM `pushover` WHERE `username` = :username AND `active` = 1");
5328 $stmt->execute(array(':username' => $_data));
5329 $PushoverActive = $stmt->fetch(PDO::FETCH_ASSOC);
5330 $stmt = $pdo->prepare("SELECT COALESCE(SUM(`quota`), 0) as `in_use` FROM `mailbox` WHERE (`kind` = '' OR `kind` = NULL) AND `domain` = :domain AND `username` != :username");
5331 $stmt->execute(array(':domain' => $row['domain'], ':username' => $_data));
5332 $MailboxUsage = $stmt->fetch(PDO::FETCH_ASSOC);
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02005333 $stmt = $pdo->prepare("SELECT IFNULL(COUNT(`address`), 0) AS `sa_count` FROM `spamalias` WHERE `goto` = :address AND (`validity` >= :unixnow OR `permanent` != 0)");
Matthias Andreas Benkard8f915942021-08-16 10:57:25 +02005334 $stmt->execute(array(':address' => $_data, ':unixnow' => time()));
5335 $SpamaliasUsage = $stmt->fetch(PDO::FETCH_ASSOC);
5336 $mailboxdata['max_new_quota'] = ($DomainQuota['quota'] * 1048576) - $MailboxUsage['in_use'];
5337 $mailboxdata['spam_aliases'] = $SpamaliasUsage['sa_count'];
5338 $mailboxdata['pushover_active'] = ($PushoverActive['pushover_active'] == 1) ? 1 : 0;
5339 if ($mailboxdata['max_new_quota'] > ($DomainQuota['maxquota'] * 1048576)) {
5340 $mailboxdata['max_new_quota'] = ($DomainQuota['maxquota'] * 1048576);
5341 }
5342 if (!empty($rl)) {
5343 $mailboxdata['rl'] = $rl;
5344 $mailboxdata['rl_scope'] = 'mailbox';
5345 }
5346 else {
5347 $mailboxdata['rl'] = ratelimit('get', 'domain', $row['domain']);
5348 $mailboxdata['rl_scope'] = 'domain';
5349 }
5350 $mailboxdata['is_relayed'] = $row['backupmx'];
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02005351 // Internal send-as ACL for this mailbox, mirrors the sender_acl field accepted by edit/mailbox
5352 $mailboxdata['sender_acl'] = array();
5353 $stmt = $pdo->prepare("SELECT `send_as` FROM `sender_acl` WHERE `logged_in_as` = :username AND `external` = '0'");
5354 $stmt->execute(array(':username' => $_data));
5355 $sender_acl_rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
5356 foreach ($sender_acl_rows as $sender_acl_row) {
5357 $mailboxdata['sender_acl'][] = $sender_acl_row['send_as'];
5358 }
Matthias Andreas Benkard8f915942021-08-16 10:57:25 +02005359 }
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +01005360 $stmt = $pdo->prepare("SELECT `tag_name`
5361 FROM `tags_mailbox` WHERE `username`= :username");
5362 $stmt->execute(array(
5363 ':username' => $_data
5364 ));
5365 $tags = $stmt->fetchAll(PDO::FETCH_ASSOC);
5366 while ($tag = array_shift($tags)) {
5367 $mailboxdata['tags'][] = $tag['tag_name'];
5368 }
Matthias Andreas Benkard8f915942021-08-16 10:57:25 +02005369
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01005370 return $mailboxdata;
5371 break;
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +01005372 case 'mailbox_templates':
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02005373 if ($_SESSION['mailcow_cc_role'] != "admin" && $_SESSION['mailcow_cc_role'] != "domainadmin" && $_SESSION['access_all_exception'] != "1") {
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +01005374 return false;
5375 }
5376 $_data = (isset($_data)) ? intval($_data) : null;
5377
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02005378 if (isset($_data)){
5379 $stmt = $pdo->prepare("SELECT * FROM `templates`
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +01005380 WHERE `id` = :id AND type = :type");
5381 $stmt->execute(array(
5382 ":id" => $_data,
5383 ":type" => "mailbox"
5384 ));
5385 $row = $stmt->fetch(PDO::FETCH_ASSOC);
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02005386
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +01005387 if (empty($row)){
5388 return false;
5389 }
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02005390
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +01005391 $row["attributes"] = json_decode($row["attributes"], true);
5392 return $row;
5393 }
5394 else {
5395 $stmt = $pdo->prepare("SELECT * FROM `templates` WHERE `type` = 'mailbox'");
5396 $stmt->execute();
5397 $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
5398
5399 if (empty($rows)){
5400 return false;
5401 }
5402
5403 foreach($rows as $key => $row){
5404 $rows[$key]["attributes"] = json_decode($row["attributes"], true);
5405 }
5406 return $rows;
5407 }
5408 break;
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02005409 case 'mta_sts':
5410 $stmt = $pdo->prepare("SELECT * FROM `mta_sts` WHERE `domain` = :domain");
5411 $stmt->execute(array(
5412 ':domain' => $_data,
5413 ));
5414 $row = $stmt->fetch(PDO::FETCH_ASSOC);
5415 if (empty($row)){
5416 return [];
5417 }
5418 $row['mx'] = explode(',', $row['mx']);
5419 $row['version'] = strtoupper(substr($row['version'], 0, 3)) . substr($row['version'], 3);
5420
5421 return $row;
5422 break;
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01005423 case 'resource_details':
5424 $resourcedata = array();
5425 if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
5426 return false;
5427 }
5428 $stmt = $pdo->prepare("SELECT
5429 `username`,
5430 `name`,
5431 `kind`,
5432 `multiple_bookings`,
5433 `local_part`,
5434 `active`,
5435 `domain`
5436 FROM `mailbox` WHERE `kind` REGEXP 'location|thing|group' AND `username` = :resource");
5437 $stmt->execute(array(
5438 ':resource' => $_data,
5439 ));
5440 $row = $stmt->fetch(PDO::FETCH_ASSOC);
5441 $resourcedata['name'] = $row['username'];
5442 $resourcedata['kind'] = $row['kind'];
5443 $resourcedata['multiple_bookings'] = $row['multiple_bookings'];
5444 $resourcedata['description'] = $row['name'];
5445 $resourcedata['active'] = $row['active'];
5446 $resourcedata['active_int'] = $row['active'];
5447 $resourcedata['domain'] = $row['domain'];
5448 $resourcedata['local_part'] = $row['local_part'];
5449 if (!isset($resourcedata['domain']) ||
5450 (isset($resourcedata['domain']) && !hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $resourcedata['domain']))) {
5451 return false;
5452 }
5453 return $resourcedata;
5454 break;
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +01005455 case 'domain_wide_footer':
5456 $domain = idn_to_ascii(strtolower(trim($_data)), 0, INTL_IDNA_VARIANT_UTS46);
5457 if (!is_valid_domain_name($domain)) {
5458 $_SESSION['return'][] = array(
5459 'type' => 'danger',
5460 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
5461 'msg' => 'domain_invalid'
5462 );
5463 return false;
5464 }
5465 if (!hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
5466 $_SESSION['return'][] = array(
5467 'type' => 'danger',
5468 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
5469 'msg' => 'access_denied'
5470 );
5471 return false;
5472 }
5473
5474 try {
Matthias Andreas Benkard9dabdab2024-03-06 13:12:09 +01005475 $stmt = $pdo->prepare("SELECT `html`, `plain`, `mbox_exclude`, `alias_domain_exclude`, `skip_replies` FROM `domain_wide_footer`
5476 WHERE `domain` = :domain");
5477 $stmt->execute(array(
5478 ':domain' => $domain
5479 ));
5480 $footer = $stmt->fetch(PDO::FETCH_ASSOC);
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +01005481 }
Matthias Andreas Benkard9dabdab2024-03-06 13:12:09 +01005482 catch (PDOException $e) {
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +01005483 $_SESSION['return'][] = array(
5484 'type' => 'danger',
5485 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
Matthias Andreas Benkard9dabdab2024-03-06 13:12:09 +01005486 'msg' => $e->getMessage()
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +01005487 );
5488 return false;
5489 }
5490
Matthias Andreas Benkard9dabdab2024-03-06 13:12:09 +01005491 return $footer;
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +01005492 break;
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01005493 }
5494 break;
5495 case 'delete':
5496 switch ($_type) {
5497 case 'syncjob':
5498 if (!is_array($_data['id'])) {
5499 $ids = array();
5500 $ids[] = $_data['id'];
5501 }
5502 else {
5503 $ids = $_data['id'];
5504 }
5505 if (!isset($_SESSION['acl']['syncjobs']) || $_SESSION['acl']['syncjobs'] != "1" ) {
5506 $_SESSION['return'][] = array(
5507 'type' => 'danger',
5508 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
5509 'msg' => 'access_denied'
5510 );
5511 return false;
5512 }
5513 foreach ($ids as $id) {
5514 if (!is_numeric($id)) {
5515 return false;
5516 }
5517 $stmt = $pdo->prepare("SELECT `user2` FROM `imapsync` WHERE id = :id");
5518 $stmt->execute(array(':id' => $id));
5519 $user2 = $stmt->fetch(PDO::FETCH_ASSOC)['user2'];
5520 if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $user2)) {
5521 $_SESSION['return'][] = array(
5522 'type' => 'danger',
5523 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
5524 'msg' => 'access_denied'
5525 );
5526 continue;
5527 }
5528 $stmt = $pdo->prepare("DELETE FROM `imapsync` WHERE `id`= :id");
5529 $stmt->execute(array(':id' => $id));
5530 $_SESSION['return'][] = array(
5531 'type' => 'success',
5532 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
5533 'msg' => array('deleted_syncjob', $id)
5534 );
5535 }
5536 break;
5537 case 'filter':
5538 if (!is_array($_data['id'])) {
5539 $ids = array();
5540 $ids[] = $_data['id'];
5541 }
5542 else {
5543 $ids = $_data['id'];
5544 }
5545 if (!isset($_SESSION['acl']['filters']) || $_SESSION['acl']['filters'] != "1" ) {
5546 $_SESSION['return'][] = array(
5547 'type' => 'danger',
5548 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
5549 'msg' => 'access_denied'
5550 );
5551 return false;
5552 }
5553 foreach ($ids as $id) {
5554 if (!is_numeric($id)) {
5555 continue;
5556 }
5557 $stmt = $pdo->prepare("SELECT `username` FROM `sieve_filters` WHERE id = :id");
5558 $stmt->execute(array(':id' => $id));
5559 $usr = $stmt->fetch(PDO::FETCH_ASSOC)['username'];
5560 if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $usr)) {
5561 $_SESSION['return'][] = array(
5562 'type' => 'danger',
5563 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
5564 'msg' => 'access_denied'
5565 );
5566 continue;
5567 }
5568 $stmt = $pdo->prepare("DELETE FROM `sieve_filters` WHERE `id`= :id");
5569 $stmt->execute(array(':id' => $id));
5570 $_SESSION['return'][] = array(
5571 'type' => 'success',
5572 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
5573 'msg' => array('delete_filter', $id)
5574 );
5575 }
5576 break;
5577 case 'time_limited_alias':
5578 if (!is_array($_data['address'])) {
5579 $addresses = array();
5580 $addresses[] = $_data['address'];
5581 }
5582 else {
5583 $addresses = $_data['address'];
5584 }
5585 if (!isset($_SESSION['acl']['spam_alias']) || $_SESSION['acl']['spam_alias'] != "1" ) {
5586 $_SESSION['return'][] = array(
5587 'type' => 'danger',
5588 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
5589 'msg' => 'access_denied'
5590 );
5591 return false;
5592 }
5593 foreach ($addresses as $address) {
5594 $stmt = $pdo->prepare("SELECT `goto` FROM `spamalias` WHERE `address` = :address");
5595 $stmt->execute(array(':address' => $address));
5596 $goto = $stmt->fetch(PDO::FETCH_ASSOC)['goto'];
5597 if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $goto)) {
5598 $_SESSION['return'][] = array(
5599 'type' => 'danger',
5600 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
5601 'msg' => 'access_denied'
5602 );
5603 continue;
5604 }
5605 $stmt = $pdo->prepare("DELETE FROM `spamalias` WHERE `goto` = :username AND `address` = :item");
5606 $stmt->execute(array(
5607 ':username' => $goto,
5608 ':item' => $address
5609 ));
5610 $_SESSION['return'][] = array(
5611 'type' => 'success',
5612 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
5613 'msg' => array('mailbox_modified', htmlspecialchars($goto))
5614 );
5615 }
5616 break;
5617 case 'eas_cache':
5618 if (!is_array($_data['username'])) {
5619 $usernames = array();
5620 $usernames[] = $_data['username'];
5621 }
5622 else {
5623 $usernames = $_data['username'];
5624 }
5625 if (!isset($_SESSION['acl']['eas_reset']) || $_SESSION['acl']['eas_reset'] != "1" ) {
5626 $_SESSION['return'][] = array(
5627 'type' => 'danger',
5628 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
5629 'msg' => 'access_denied'
5630 );
5631 return false;
5632 }
5633 foreach ($usernames as $username) {
5634 if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $username)) {
5635 $_SESSION['return'][] = array(
5636 'type' => 'danger',
5637 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
5638 'msg' => 'access_denied'
5639 );
5640 continue;
5641 }
5642 $stmt = $pdo->prepare("DELETE FROM `sogo_cache_folder` WHERE `c_uid` = :username");
5643 $stmt->execute(array(
5644 ':username' => $username
5645 ));
5646 $_SESSION['return'][] = array(
5647 'type' => 'success',
5648 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
5649 'msg' => array('eas_reset', htmlspecialchars($username))
5650 );
5651 }
5652 break;
5653 case 'sogo_profile':
5654 if (!is_array($_data['username'])) {
5655 $usernames = array();
5656 $usernames[] = $_data['username'];
5657 }
5658 else {
5659 $usernames = $_data['username'];
5660 }
5661 if (!isset($_SESSION['acl']['sogo_profile_reset']) || $_SESSION['acl']['sogo_profile_reset'] != "1" ) {
5662 $_SESSION['return'][] = array(
5663 'type' => 'danger',
5664 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
5665 'msg' => 'access_denied'
5666 );
5667 return false;
5668 }
5669 foreach ($usernames as $username) {
5670 if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $username)) {
5671 $_SESSION['return'][] = array(
5672 'type' => 'danger',
5673 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
5674 'msg' => 'access_denied'
5675 );
5676 continue;
5677 }
5678 $stmt = $pdo->prepare("DELETE FROM `sogo_user_profile` WHERE `c_uid` = :username");
5679 $stmt->execute(array(
5680 ':username' => $username
5681 ));
5682 $stmt = $pdo->prepare("DELETE FROM `sogo_cache_folder` WHERE `c_uid` = :username");
5683 $stmt->execute(array(
5684 ':username' => $username
5685 ));
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02005686 // bind LIKE pattern + escape LIKE wildcards so the value matches literally (no SQLi, no over-match)
5687 $c_object_like = '%/' . addcslashes($username, '\\%_') . '/%';
5688 $stmt = $pdo->prepare("DELETE FROM `sogo_acl` WHERE `c_object` LIKE :c_object_like OR `c_uid` = :username");
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01005689 $stmt->execute(array(
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02005690 ':c_object_like' => $c_object_like,
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01005691 ':username' => $username
5692 ));
5693 $stmt = $pdo->prepare("DELETE FROM `sogo_store` WHERE `c_folder_id` IN (SELECT `c_folder_id` FROM `sogo_folder_info` WHERE `c_path2` = :username)");
5694 $stmt->execute(array(
5695 ':username' => $username
5696 ));
5697 $stmt = $pdo->prepare("DELETE FROM `sogo_quick_contact` WHERE `c_folder_id` IN (SELECT `c_folder_id` FROM `sogo_folder_info` WHERE `c_path2` = :username)");
5698 $stmt->execute(array(
5699 ':username' => $username
5700 ));
5701 $stmt = $pdo->prepare("DELETE FROM `sogo_quick_appointment` WHERE `c_folder_id` IN (SELECT `c_folder_id` FROM `sogo_folder_info` WHERE `c_path2` = :username)");
5702 $stmt->execute(array(
5703 ':username' => $username
5704 ));
5705 $stmt = $pdo->prepare("DELETE FROM `sogo_folder_info` WHERE `c_path2` = :username");
5706 $stmt->execute(array(
5707 ':username' => $username
5708 ));
5709 $_SESSION['return'][] = array(
5710 'type' => 'success',
5711 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
5712 'msg' => array('sogo_profile_reset', htmlspecialchars($username))
5713 );
5714 }
5715 break;
5716 case 'domain':
5717 if (!is_array($_data['domain'])) {
5718 $domains = array();
5719 $domains[] = $_data['domain'];
5720 }
5721 else {
5722 $domains = $_data['domain'];
5723 }
5724 if ($_SESSION['mailcow_cc_role'] != "admin") {
5725 $_SESSION['return'][] = array(
5726 'type' => 'danger',
5727 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
5728 'msg' => 'access_denied'
5729 );
5730 return false;
5731 }
5732 foreach ($domains as $domain) {
5733 if (!is_valid_domain_name($domain)) {
5734 $_SESSION['return'][] = array(
5735 'type' => 'danger',
5736 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
5737 'msg' => 'domain_invalid'
5738 );
5739 continue;
5740 }
Matthias Andreas Benkard8f915942021-08-16 10:57:25 +02005741 $domain = idn_to_ascii(strtolower(trim($domain)), 0, INTL_IDNA_VARIANT_UTS46);
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01005742 $stmt = $pdo->prepare("SELECT `username` FROM `mailbox`
5743 WHERE `domain` = :domain");
5744 $stmt->execute(array(':domain' => $domain));
5745 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
5746 if ($num_results != 0 || !empty($num_results)) {
5747 $_SESSION['return'][] = array(
5748 'type' => 'danger',
5749 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
5750 'msg' => array('domain_not_empty', $domain)
5751 );
5752 continue;
5753 }
5754 $exec_fields = array('cmd' => 'maildir', 'task' => 'cleanup', 'maildir' => $domain);
5755 $maildir_gc = json_decode(docker('post', 'dovecot-mailcow', 'exec', $exec_fields), true);
5756 if ($maildir_gc['type'] != 'success') {
5757 $_SESSION['return'][] = array(
5758 'type' => 'warning',
5759 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
5760 'msg' => 'Could not move mail storage to garbage collector: ' . $maildir_gc['msg']
5761 );
5762 }
5763 $stmt = $pdo->prepare("DELETE FROM `domain` WHERE `domain` = :domain");
5764 $stmt->execute(array(
5765 ':domain' => $domain,
5766 ));
5767 $stmt = $pdo->prepare("DELETE FROM `domain_admins` WHERE `domain` = :domain");
5768 $stmt->execute(array(
5769 ':domain' => $domain,
5770 ));
5771 $stmt = $pdo->prepare("DELETE FROM `alias` WHERE `domain` = :domain");
5772 $stmt->execute(array(
5773 ':domain' => $domain,
5774 ));
5775 $stmt = $pdo->prepare("DELETE FROM `alias_domain` WHERE `target_domain` = :domain");
5776 $stmt->execute(array(
5777 ':domain' => $domain,
5778 ));
5779 $stmt = $pdo->prepare("DELETE FROM `mailbox` WHERE `domain` = :domain");
5780 $stmt->execute(array(
5781 ':domain' => $domain,
5782 ));
5783 $stmt = $pdo->prepare("DELETE FROM `sender_acl` WHERE `logged_in_as` LIKE :domain");
5784 $stmt->execute(array(
5785 ':domain' => '%@'.$domain,
5786 ));
5787 $stmt = $pdo->prepare("DELETE FROM `quota2` WHERE `username` LIKE :domain");
5788 $stmt->execute(array(
5789 ':domain' => '%@'.$domain,
5790 ));
5791 $stmt = $pdo->prepare("DELETE FROM `pushover` WHERE `username` LIKE :domain");
5792 $stmt->execute(array(
5793 ':domain' => '%@'.$domain,
5794 ));
5795 $stmt = $pdo->prepare("DELETE FROM `quota2replica` WHERE `username` LIKE :domain");
5796 $stmt->execute(array(
5797 ':domain' => '%@'.$domain,
5798 ));
5799 $stmt = $pdo->prepare("DELETE FROM `spamalias` WHERE `address` LIKE :domain");
5800 $stmt->execute(array(
5801 ':domain' => '%@'.$domain,
5802 ));
5803 $stmt = $pdo->prepare("DELETE FROM `filterconf` WHERE `object` = :domain");
5804 $stmt->execute(array(
5805 ':domain' => $domain,
5806 ));
5807 $stmt = $pdo->prepare("DELETE FROM `bcc_maps` WHERE `local_dest` = :domain");
5808 $stmt->execute(array(
5809 ':domain' => $domain,
5810 ));
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02005811 $stmt = $pdo->prepare("DELETE FROM `mta_sts` WHERE `domain` = :domain");
5812 $stmt->execute(array(
5813 ':domain' => $domain,
5814 ));
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01005815 $stmt = $pdo->query("DELETE FROM `admin` WHERE `superadmin` = 0 AND `username` NOT IN (SELECT `username`FROM `domain_admins`);");
5816 $stmt = $pdo->query("DELETE FROM `da_acl` WHERE `username` NOT IN (SELECT `username`FROM `domain_admins`);");
5817 try {
5818 $redis->hDel('DOMAIN_MAP', $domain);
5819 $redis->hDel('RL_VALUE', $domain);
5820 }
5821 catch (RedisException $e) {
5822 $_SESSION['return'][] = array(
5823 'type' => 'danger',
5824 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
5825 'msg' => array('redis_error', $e)
5826 );
5827 continue;
5828 }
5829 $_SESSION['return'][] = array(
5830 'type' => 'success',
5831 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
5832 'msg' => array('domain_removed', htmlspecialchars($domain))
5833 );
5834 }
5835 break;
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +01005836 case 'domain_templates':
5837 if ($_SESSION['mailcow_cc_role'] != "admin") {
5838 $_SESSION['return'][] = array(
5839 'type' => 'danger',
5840 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
5841 'msg' => 'access_denied'
5842 );
5843 return false;
5844 }
5845 if (!is_array($_data['ids'])) {
5846 $ids = array();
5847 $ids[] = $_data['ids'];
5848 }
5849 else {
5850 $ids = $_data['ids'];
5851 }
5852
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02005853
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +01005854 foreach ($ids as $id) {
5855 // delete template
5856 $stmt = $pdo->prepare("DELETE FROM `templates`
5857 WHERE id = :id AND type = :type AND NOT template = :template");
5858 $stmt->execute(array(
5859 ":id" => $id,
5860 ":type" => "domain",
5861 ":template" => "Default"
5862 ));
5863
5864 $_SESSION['return'][] = array(
5865 'type' => 'success',
5866 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
5867 'msg' => array('template_removed', htmlspecialchars($id))
5868 );
5869 return true;
5870 }
5871 break;
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01005872 case 'alias':
5873 if (!is_array($_data['id'])) {
5874 $ids = array();
5875 $ids[] = $_data['id'];
5876 }
5877 else {
5878 $ids = $_data['id'];
5879 }
5880 foreach ($ids as $id) {
5881 $alias_data = mailbox('get', 'alias_details', $id);
5882 if (empty($alias_data)) {
5883 $_SESSION['return'][] = array(
5884 'type' => 'danger',
5885 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
5886 'msg' => 'access_denied'
5887 );
5888 continue;
5889 }
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02005890
5891 // Track affected mailboxes for SOGo update (capture before deletion)
5892 if (!empty($alias_data['goto'])) {
5893 $gotos = array_map('trim', explode(',', $alias_data['goto']));
5894 foreach ($gotos as $g) {
5895 if (filter_var($g, FILTER_VALIDATE_EMAIL) &&
5896 !in_array($g, array('null@localhost', 'spam@localhost', 'ham@localhost'))) {
5897 $update_sogo_mailboxes[] = $g;
5898 }
5899 }
5900 }
5901
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01005902 $stmt = $pdo->prepare("DELETE FROM `alias` WHERE `id` = :id");
5903 $stmt->execute(array(
5904 ':id' => $alias_data['id']
5905 ));
5906 $stmt = $pdo->prepare("DELETE FROM `sender_acl` WHERE `send_as` = :alias_address");
5907 $stmt->execute(array(
5908 ':alias_address' => $alias_data['address']
5909 ));
5910 $_SESSION['return'][] = array(
5911 'type' => 'success',
5912 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
5913 'msg' => array('alias_removed', htmlspecialchars($alias_data['address']))
5914 );
5915 }
5916 break;
5917 case 'alias_domain':
5918 if (!is_array($_data['alias_domain'])) {
5919 $alias_domains = array();
5920 $alias_domains[] = $_data['alias_domain'];
5921 }
5922 else {
5923 $alias_domains = $_data['alias_domain'];
5924 }
5925 foreach ($alias_domains as $alias_domain) {
5926 if (!is_valid_domain_name($alias_domain)) {
5927 $_SESSION['return'][] = array(
5928 'type' => 'danger',
5929 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
5930 'msg' => 'domain_invalid'
5931 );
5932 continue;
5933 }
5934 $stmt = $pdo->prepare("SELECT `target_domain` FROM `alias_domain`
5935 WHERE `alias_domain`= :alias_domain");
5936 $stmt->execute(array(':alias_domain' => $alias_domain));
5937 $DomainData = $stmt->fetch(PDO::FETCH_ASSOC);
5938 if (!hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $DomainData['target_domain'])) {
5939 $_SESSION['return'][] = array(
5940 'type' => 'danger',
5941 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
5942 'msg' => 'access_denied'
5943 );
5944 continue;
5945 }
5946 $stmt = $pdo->prepare("DELETE FROM `alias_domain` WHERE `alias_domain` = :alias_domain");
5947 $stmt->execute(array(
5948 ':alias_domain' => $alias_domain,
5949 ));
5950 $stmt = $pdo->prepare("DELETE FROM `alias` WHERE `domain` = :alias_domain");
5951 $stmt->execute(array(
5952 ':alias_domain' => $alias_domain,
5953 ));
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +01005954 $stmt = $pdo->prepare("DELETE FROM `spamalias` WHERE `address` LIKE :domain");
5955 $stmt->execute(array(
5956 ':domain' => '%@'.$alias_domain,
5957 ));
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01005958 $stmt = $pdo->prepare("DELETE FROM `bcc_maps` WHERE `local_dest` = :alias_domain");
5959 $stmt->execute(array(
5960 ':alias_domain' => $alias_domain,
5961 ));
5962 try {
5963 $redis->hDel('DOMAIN_MAP', $alias_domain);
5964 $redis->hDel('RL_VALUE', $domain);
5965 }
5966 catch (RedisException $e) {
5967 $_SESSION['return'][] = array(
5968 'type' => 'danger',
5969 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
5970 'msg' => array('redis_error', $e)
5971 );
5972 continue;
5973 }
5974 $_SESSION['return'][] = array(
5975 'type' => 'success',
5976 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
5977 'msg' => array('alias_domain_removed', htmlspecialchars($alias_domain))
5978 );
5979 }
5980 break;
5981 case 'mailbox':
5982 if (!is_array($_data['username'])) {
5983 $usernames = array();
5984 $usernames[] = $_data['username'];
5985 }
5986 else {
5987 $usernames = $_data['username'];
5988 }
5989 foreach ($usernames as $username) {
5990 if (!filter_var($username, FILTER_VALIDATE_EMAIL)) {
5991 $_SESSION['return'][] = array(
5992 'type' => 'danger',
5993 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
5994 'msg' => 'access_denied'
5995 );
5996 continue;
5997 }
5998 if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $username)) {
5999 $_SESSION['return'][] = array(
6000 'type' => 'danger',
6001 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
6002 'msg' => 'access_denied'
6003 );
6004 continue;
6005 }
6006 $mailbox_details = mailbox('get', 'mailbox_details', $username);
6007 if (!empty($mailbox_details['domain']) && !empty($mailbox_details['local_part'])) {
6008 $maildir = $mailbox_details['domain'] . '/' . $mailbox_details['local_part'];
6009 $exec_fields = array('cmd' => 'maildir', 'task' => 'cleanup', 'maildir' => $maildir);
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +01006010
6011 if (getenv("CLUSTERMODE") == "replication") {
6012 // broadcast to each dovecot container
6013 docker('broadcast', 'dovecot-mailcow', 'exec', $exec_fields);
6014 } else {
6015 $maildir_gc = json_decode(docker('post', 'dovecot-mailcow', 'exec', $exec_fields), true);
6016 if ($maildir_gc['type'] != 'success') {
6017 $_SESSION['return'][] = array(
6018 'type' => 'warning',
6019 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
6020 'msg' => 'Could not move maildir to garbage collector: ' . $maildir_gc['msg']
6021 );
6022 }
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01006023 }
6024 }
6025 else {
6026 $_SESSION['return'][] = array(
6027 'type' => 'warning',
6028 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
6029 'msg' => 'Could not move maildir to garbage collector: variables local_part and/or domain empty'
6030 );
6031 }
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01006032 $stmt = $pdo->prepare("DELETE FROM `alias` WHERE `goto` = :username");
6033 $stmt->execute(array(
6034 ':username' => $username
6035 ));
6036 $stmt = $pdo->prepare("DELETE FROM `pushover` WHERE `username` = :username");
6037 $stmt->execute(array(
6038 ':username' => $username
6039 ));
6040 $stmt = $pdo->prepare("DELETE FROM `quarantine` WHERE `rcpt` = :username");
6041 $stmt->execute(array(
6042 ':username' => $username
6043 ));
6044 $stmt = $pdo->prepare("DELETE FROM `quota2` WHERE `username` = :username");
6045 $stmt->execute(array(
6046 ':username' => $username
6047 ));
6048 $stmt = $pdo->prepare("DELETE FROM `quota2replica` WHERE `username` = :username");
6049 $stmt->execute(array(
6050 ':username' => $username
6051 ));
6052 $stmt = $pdo->prepare("DELETE FROM `mailbox` WHERE `username` = :username");
6053 $stmt->execute(array(
6054 ':username' => $username
6055 ));
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +01006056 $stmt = $pdo->prepare("DELETE FROM `sender_acl` WHERE `logged_in_as` = :logged_in_as OR `send_as` = :send_as");
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01006057 $stmt->execute(array(
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +01006058 ':logged_in_as' => $username,
6059 ':send_as' => $username
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01006060 ));
6061 // fk, better safe than sorry
6062 $stmt = $pdo->prepare("DELETE FROM `user_acl` WHERE `username` = :username");
6063 $stmt->execute(array(
6064 ':username' => $username
6065 ));
6066 $stmt = $pdo->prepare("DELETE FROM `spamalias` WHERE `goto` = :username");
6067 $stmt->execute(array(
6068 ':username' => $username
6069 ));
6070 $stmt = $pdo->prepare("DELETE FROM `imapsync` WHERE `user2` = :username");
6071 $stmt->execute(array(
6072 ':username' => $username
6073 ));
6074 $stmt = $pdo->prepare("DELETE FROM `filterconf` WHERE `object` = :username");
6075 $stmt->execute(array(
6076 ':username' => $username
6077 ));
6078 $stmt = $pdo->prepare("DELETE FROM `sogo_user_profile` WHERE `c_uid` = :username");
6079 $stmt->execute(array(
6080 ':username' => $username
6081 ));
6082 $stmt = $pdo->prepare("DELETE FROM `sogo_cache_folder` WHERE `c_uid` = :username");
6083 $stmt->execute(array(
6084 ':username' => $username
6085 ));
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02006086 // bind LIKE pattern + escape LIKE wildcards so the value matches literally (no SQLi, no over-match)
6087 $c_object_like = '%/' . addcslashes($username, '\\%_') . '/%';
6088 $stmt = $pdo->prepare("DELETE FROM `sogo_acl` WHERE `c_object` LIKE :c_object_like OR `c_uid` = :username");
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01006089 $stmt->execute(array(
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02006090 ':c_object_like' => $c_object_like,
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01006091 ':username' => $username
6092 ));
6093 $stmt = $pdo->prepare("DELETE FROM `sogo_store` WHERE `c_folder_id` IN (SELECT `c_folder_id` FROM `sogo_folder_info` WHERE `c_path2` = :username)");
6094 $stmt->execute(array(
6095 ':username' => $username
6096 ));
6097 $stmt = $pdo->prepare("DELETE FROM `sogo_quick_contact` WHERE `c_folder_id` IN (SELECT `c_folder_id` FROM `sogo_folder_info` WHERE `c_path2` = :username)");
6098 $stmt->execute(array(
6099 ':username' => $username
6100 ));
6101 $stmt = $pdo->prepare("DELETE FROM `sogo_quick_appointment` WHERE `c_folder_id` IN (SELECT `c_folder_id` FROM `sogo_folder_info` WHERE `c_path2` = :username)");
6102 $stmt->execute(array(
6103 ':username' => $username
6104 ));
6105 $stmt = $pdo->prepare("DELETE FROM `sogo_folder_info` WHERE `c_path2` = :username");
6106 $stmt->execute(array(
6107 ':username' => $username
6108 ));
6109 $stmt = $pdo->prepare("DELETE FROM `bcc_maps` WHERE `local_dest` = :username");
6110 $stmt->execute(array(
6111 ':username' => $username
6112 ));
6113 $stmt = $pdo->prepare("DELETE FROM `oauth_access_tokens` WHERE `user_id` = :username");
6114 $stmt->execute(array(
6115 ':username' => $username
6116 ));
6117 $stmt = $pdo->prepare("DELETE FROM `oauth_refresh_tokens` WHERE `user_id` = :username");
6118 $stmt->execute(array(
6119 ':username' => $username
6120 ));
6121 $stmt = $pdo->prepare("DELETE FROM `oauth_authorization_codes` WHERE `user_id` = :username");
6122 $stmt->execute(array(
6123 ':username' => $username
6124 ));
Matthias Andreas Benkard8f915942021-08-16 10:57:25 +02006125 $stmt = $pdo->prepare("DELETE FROM `tfa` WHERE `username` = :username");
6126 $stmt->execute(array(
6127 ':username' => $username,
6128 ));
6129 $stmt = $pdo->prepare("DELETE FROM `fido2` WHERE `username` = :username");
6130 $stmt->execute(array(
6131 ':username' => $username,
6132 ));
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01006133 $stmt = $pdo->prepare("SELECT `address`, `goto` FROM `alias`
6134 WHERE `goto` REGEXP :username");
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02006135 $stmt->execute(array(':username' => '(^|,)'.preg_quote($username, '/').'($|,)'));
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01006136 $GotoData = $stmt->fetchAll(PDO::FETCH_ASSOC);
6137 foreach ($GotoData as $gotos) {
6138 $goto_exploded = explode(',', $gotos['goto']);
6139 if (($key = array_search($username, $goto_exploded)) !== false) {
6140 unset($goto_exploded[$key]);
6141 }
Matthias Andreas Benkard8f915942021-08-16 10:57:25 +02006142 $gotos_rebuild = implode(',', (array)$goto_exploded);
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01006143 $stmt = $pdo->prepare("UPDATE `alias` SET
6144 `goto` = :goto
6145 WHERE `address` = :address");
6146 $stmt->execute(array(
6147 ':goto' => $gotos_rebuild,
6148 ':address' => $gotos['address']
6149 ));
6150 }
6151 try {
6152 $redis->hDel('RL_VALUE', $username);
6153 }
6154 catch (RedisException $e) {
6155 $_SESSION['return'][] = array(
6156 'type' => 'danger',
6157 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
6158 'msg' => array('redis_error', $e)
6159 );
6160 continue;
6161 }
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02006162
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01006163 $_SESSION['return'][] = array(
6164 'type' => 'success',
6165 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
6166 'msg' => array('mailbox_removed', htmlspecialchars($username))
6167 );
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02006168
6169 // Track affected mailboxes for SOGo update
6170 $update_sogo_mailboxes[] = $username;
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01006171 }
6172 break;
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +01006173 case 'mailbox_templates':
6174 if ($_SESSION['mailcow_cc_role'] != "admin") {
6175 $_SESSION['return'][] = array(
6176 'type' => 'danger',
6177 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
6178 'msg' => 'access_denied'
6179 );
6180 return false;
6181 }
6182 if (!is_array($_data['ids'])) {
6183 $ids = array();
6184 $ids[] = $_data['ids'];
6185 }
6186 else {
6187 $ids = $_data['ids'];
6188 }
6189
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02006190
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +01006191 foreach ($ids as $id) {
6192 // delete template
6193 $stmt = $pdo->prepare("DELETE FROM `templates`
6194 WHERE id = :id AND type = :type AND NOT template = :template");
6195 $stmt->execute(array(
6196 ":id" => $id,
6197 ":type" => "mailbox",
6198 ":template" => "Default"
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02006199 ));
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +01006200 }
6201
6202 $_SESSION['return'][] = array(
6203 'type' => 'success',
6204 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
6205 'msg' => 'template_removed'
6206 );
6207 return true;
6208 break;
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01006209 case 'resource':
6210 if (!is_array($_data['name'])) {
6211 $names = array();
6212 $names[] = $_data['name'];
6213 }
6214 else {
6215 $names = $_data['name'];
6216 }
6217 foreach ($names as $name) {
6218 if (!filter_var($name, FILTER_VALIDATE_EMAIL)) {
6219 $_SESSION['return'][] = array(
6220 'type' => 'danger',
6221 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
6222 'msg' => 'access_denied'
6223 );
6224 continue;
6225 }
6226 if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $name)) {
6227 $_SESSION['return'][] = array(
6228 'type' => 'danger',
6229 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
6230 'msg' => 'access_denied'
6231 );
6232 continue;
6233 }
6234 $stmt = $pdo->prepare("DELETE FROM `mailbox` WHERE `username` = :username");
6235 $stmt->execute(array(
6236 ':username' => $name
6237 ));
6238 $stmt = $pdo->prepare("DELETE FROM `sogo_user_profile` WHERE `c_uid` = :username");
6239 $stmt->execute(array(
6240 ':username' => $name
6241 ));
6242 $stmt = $pdo->prepare("DELETE FROM `sogo_cache_folder` WHERE `c_uid` = :username");
6243 $stmt->execute(array(
6244 ':username' => $name
6245 ));
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02006246 // bind LIKE pattern + escape LIKE wildcards so the value matches literally (no SQLi, no over-match)
6247 $c_object_like = '%/' . addcslashes($name, '\\%_') . '/%';
6248 $stmt = $pdo->prepare("DELETE FROM `sogo_acl` WHERE `c_object` LIKE :c_object_like OR `c_uid` = :username");
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01006249 $stmt->execute(array(
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02006250 ':c_object_like' => $c_object_like,
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01006251 ':username' => $name
6252 ));
6253 $stmt = $pdo->prepare("DELETE FROM `sogo_store` WHERE `c_folder_id` IN (SELECT `c_folder_id` FROM `sogo_folder_info` WHERE `c_path2` = :username)");
6254 $stmt->execute(array(
6255 ':username' => $name
6256 ));
6257 $stmt = $pdo->prepare("DELETE FROM `sogo_quick_contact` WHERE `c_folder_id` IN (SELECT `c_folder_id` FROM `sogo_folder_info` WHERE `c_path2` = :username)");
6258 $stmt->execute(array(
6259 ':username' => $name
6260 ));
6261 $stmt = $pdo->prepare("DELETE FROM `sogo_quick_appointment` WHERE `c_folder_id` IN (SELECT `c_folder_id` FROM `sogo_folder_info` WHERE `c_path2` = :username)");
6262 $stmt->execute(array(
6263 ':username' => $name
6264 ));
6265 $stmt = $pdo->prepare("DELETE FROM `sogo_folder_info` WHERE `c_path2` = :username");
6266 $stmt->execute(array(
6267 ':username' => $name
6268 ));
6269 $_SESSION['return'][] = array(
6270 'type' => 'success',
6271 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
6272 'msg' => array('resource_removed', htmlspecialchars($name))
6273 );
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02006274
6275 // Track affected mailboxes for SOGo update
6276 $update_sogo_mailboxes[] = $name;
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01006277 }
6278 break;
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02006279 case 'tags_domain':
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +01006280 if (!is_array($_data['domain'])) {
6281 $domains = array();
6282 $domains[] = $_data['domain'];
6283 }
6284 else {
6285 $domains = $_data['domain'];
6286 }
6287 $tags = $_data['tags'];
6288 if (!is_array($tags)) $tags = array();
6289
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +01006290
6291 $wasModified = false;
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02006292 foreach ($domains as $domain) {
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +01006293 if (!is_valid_domain_name($domain)) {
6294 $_SESSION['return'][] = array(
6295 'type' => 'danger',
6296 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
6297 'msg' => 'domain_invalid'
6298 );
6299 continue;
6300 }
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +01006301 if (!hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $domain)) {
6302 $_SESSION['return'][] = array(
6303 'type' => 'danger',
6304 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
6305 'msg' => 'access_denied'
6306 );
6307 return false;
6308 }
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02006309
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +01006310 foreach($tags as $tag){
6311 // delete tag
6312 $wasModified = true;
6313 $stmt = $pdo->prepare("DELETE FROM `tags_domain` WHERE `domain` = :domain AND `tag_name` = :tag_name");
6314 $stmt->execute(array(
6315 ':domain' => $domain,
6316 ':tag_name' => $tag,
6317 ));
6318 }
6319 }
6320
6321 if (!$wasModified) return false;
6322 $_SESSION['return'][] = array(
6323 'type' => 'success',
6324 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
6325 'msg' => array('domain_modified', $domain)
6326 );
6327 break;
6328 case 'tags_mailbox':
6329 if (!is_array($_data['username'])) {
6330 $usernames = array();
6331 $usernames[] = $_data['username'];
6332 }
6333 else {
6334 $usernames = $_data['username'];
6335 }
6336 $tags = $_data['tags'];
6337 if (!is_array($tags)) $tags = array();
6338
6339 $wasModified = false;
6340 foreach ($usernames as $username) {
6341 if (!filter_var($username, FILTER_VALIDATE_EMAIL)) {
6342 $_SESSION['return'][] = array(
6343 'type' => 'danger',
6344 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
6345 'msg' => 'email invalid'
6346 );
6347 continue;
6348 }
6349
6350 $is_now = mailbox('get', 'mailbox_details', $username);
6351 $domain = $is_now['domain'];
6352 if (!hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $domain)) {
6353 $_SESSION['return'][] = array(
6354 'type' => 'danger',
6355 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
6356 'msg' => 'access_denied'
6357 );
6358 continue;
6359 }
6360
6361 // delete tags
6362 foreach($tags as $tag){
6363 $wasModified = true;
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02006364
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +01006365 $stmt = $pdo->prepare("DELETE FROM `tags_mailbox` WHERE `username` = :username AND `tag_name` = :tag_name");
6366 $stmt->execute(array(
6367 ':username' => $username,
6368 ':tag_name' => $tag,
6369 ));
6370 }
6371 }
6372
6373 if (!$wasModified) return false;
6374 $_SESSION['return'][] = array(
6375 'type' => 'success',
6376 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
6377 'msg' => array('mailbox_modified', $username)
6378 );
6379 break;
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01006380 }
6381 break;
6382 }
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02006383 if ($_action != 'get' && in_array($_type, array('domain', 'alias', 'alias_domain', 'resource', 'mailbox')) && getenv('SKIP_SOGO') != "y") {
6384 try {
6385 if (($_type == 'alias' || $_type == 'resource' || $_type == 'mailbox') && !empty($update_sogo_mailboxes)) {
6386 // INCREMENTAL UPDATE: Update only affected mailboxes/resources
6387 $update_sogo_mailboxes = array_unique($update_sogo_mailboxes);
6388 foreach ($update_sogo_mailboxes as $mailbox) {
6389 update_sogo_static_view($mailbox);
6390 }
6391 }
6392 else {
6393 // FULL REBUILD: For domain and alias_domain operations or if no tracked mailboxes
6394 // Domain operations affect all mailboxes
6395 // Alias_domain operations affect entire target domain
6396 update_sogo_static_view();
6397 }
6398 }catch (PDOException $e) {
6399 $_SESSION['return'][] = array(
6400 'type' => 'success',
6401 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
6402 'msg' => $e->getMessage()
6403 );
6404 }
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01006405 }
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02006406
6407 return true;
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01006408}