blob: d67fa3e37c3b789a0e42d06e8c719614ee186306 [file] [log] [blame]
Matthias Andreas Benkardb382b102021-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;
7 $_data_log = $_data;
8 !isset($_data_log['password']) ?: $_data_log['password'] = '*';
9 !isset($_data_log['password2']) ?: $_data_log['password2'] = '*';
10 switch ($_action) {
11 case 'add':
12 switch ($_type) {
13 case 'time_limited_alias':
14 if (!isset($_SESSION['acl']['spam_alias']) || $_SESSION['acl']['spam_alias'] != "1" ) {
15 $_SESSION['return'][] = array(
16 'type' => 'danger',
17 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
18 'msg' => 'access_denied'
19 );
20 return false;
21 }
22 if (isset($_data['username']) && filter_var($_data['username'], FILTER_VALIDATE_EMAIL)) {
23 if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data['username'])) {
24 $_SESSION['return'][] = array(
25 'type' => 'danger',
26 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
27 'msg' => 'access_denied'
28 );
29 return false;
30 }
31 else {
32 $username = $_data['username'];
33 }
34 }
35 else {
36 $username = $_SESSION['mailcow_cc_username'];
37 }
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +020038 if (isset($_data["validity"]) && !filter_var($_data["validity"], FILTER_VALIDATE_INT, array('options' => array('min_range' => 1, 'max_range' => 87600)))) {
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +010039 $_SESSION['return'][] = array(
40 'type' => 'danger',
41 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
42 'msg' => 'validity_missing'
43 );
44 return false;
45 }
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +020046 else {
47 // Default to 1 yr
48 $_data["validity"] = 8760;
49 }
50 $domain = $_data['domain'];
51 $valid_domains[] = mailbox('get', 'mailbox_details', $username)['domain'];
52 $valid_alias_domains = user_get_alias_details($username)['alias_domains'];
53 if (!empty($valid_alias_domains)) {
54 $valid_domains = array_merge($valid_domains, $valid_alias_domains);
55 }
56 if (!in_array($domain, $valid_domains)) {
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +010057 $_SESSION['return'][] = array(
58 'type' => 'danger',
59 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
60 'msg' => 'domain_invalid'
61 );
62 return false;
63 }
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +020064 $validity = strtotime("+" . $_data["validity"] . " hour");
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +010065 $stmt = $pdo->prepare("INSERT INTO `spamalias` (`address`, `goto`, `validity`) VALUES
66 (:address, :goto, :validity)");
67 $stmt->execute(array(
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +020068 ':address' => readable_random_string(rand(rand(3, 9), rand(3, 9))) . '.' . readable_random_string(rand(rand(3, 9), rand(3, 9))) . '@' . $domain,
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +010069 ':goto' => $username,
70 ':validity' => $validity
71 ));
72 $_SESSION['return'][] = array(
73 'type' => 'success',
74 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
75 'msg' => array('mailbox_modified', $username)
76 );
77 break;
78 case 'global_filter':
79 if ($_SESSION['mailcow_cc_role'] != "admin") {
80 $_SESSION['return'][] = array(
81 'type' => 'danger',
82 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
83 'msg' => 'access_denied'
84 );
85 return false;
86 }
87 $sieve = new Sieve\SieveParser();
88 $script_data = $_data['script_data'];
89 $script_data = str_replace("\r\n", "\n", $script_data); // windows -> unix
90 $script_data = str_replace("\r", "\n", $script_data); // remaining -> unix
91 $filter_type = $_data['filter_type'];
92 try {
93 $sieve->parse($script_data);
94 }
95 catch (Exception $e) {
96 $_SESSION['return'][] = array(
97 'type' => 'danger',
98 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
99 'msg' => array('sieve_error', $e->getMessage())
100 );
101 return false;
102 }
103 if ($filter_type == 'prefilter') {
104 try {
105 if (file_exists('/global_sieve/before')) {
106 $filter_handle = fopen('/global_sieve/before', 'w');
107 if (!$filter_handle) {
108 throw new Exception($lang['danger']['file_open_error']);
109 }
110 fwrite($filter_handle, $script_data);
111 fclose($filter_handle);
112 }
113 $restart_response = json_decode(docker('post', 'dovecot-mailcow', 'restart'), true);
114 if ($restart_response['type'] == "success") {
115 $_SESSION['return'][] = array(
116 'type' => 'success',
117 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
118 'msg' => 'dovecot_restart_success'
119 );
120 }
121 else {
122 $_SESSION['return'][] = array(
123 'type' => 'warning',
124 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
125 'msg' => 'dovecot_restart_failed'
126 );
127 }
128 }
129 catch (Exception $e) {
130 $_SESSION['return'][] = array(
131 'type' => 'danger',
132 'log' => array(__FUNCTION__, $_action, $_data_log),
133 'msg' => array('global_filter_write_error', htmlspecialchars($e->getMessage()))
134 );
135 return false;
136 }
137 }
138 elseif ($filter_type == 'postfilter') {
139 try {
140 if (file_exists('/global_sieve/after')) {
141 $filter_handle = fopen('/global_sieve/after', 'w');
142 if (!$filter_handle) {
143 throw new Exception($lang['danger']['file_open_error']);
144 }
145 fwrite($filter_handle, $script_data);
146 fclose($filter_handle);
147 }
148 $restart_response = json_decode(docker('post', 'dovecot-mailcow', 'restart'), true);
149 if ($restart_response['type'] == "success") {
150 $_SESSION['return'][] = array(
151 'type' => 'success',
152 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
153 'msg' => 'dovecot_restart_success'
154 );
155 }
156 else {
157 $_SESSION['return'][] = array(
158 'type' => 'warning',
159 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
160 'msg' => 'dovecot_restart_failed'
161 );
162 }
163 }
164 catch (Exception $e) {
165 $_SESSION['return'][] = array(
166 'type' => 'danger',
167 'log' => array(__FUNCTION__, $_action, $_data_log),
168 'msg' => array('global_filter_write_error', htmlspecialchars($e->getMessage()))
169 );
170 return false;
171 }
172 }
173 else {
174 $_SESSION['return'][] = array(
175 'type' => 'danger',
176 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
177 'msg' => 'invalid_filter_type'
178 );
179 return false;
180 }
181 $_SESSION['return'][] = array(
182 'type' => 'success',
183 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
184 'msg' => 'global_filter_written'
185 );
186 return true;
187 case 'filter':
188 $sieve = new Sieve\SieveParser();
189 if (!isset($_SESSION['acl']['filters']) || $_SESSION['acl']['filters'] != "1" ) {
190 $_SESSION['return'][] = array(
191 'type' => 'danger',
192 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
193 'msg' => 'access_denied'
194 );
195 return false;
196 }
197 if (isset($_data['username']) && filter_var($_data['username'], FILTER_VALIDATE_EMAIL)) {
198 if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data['username'])) {
199 $_SESSION['return'][] = array(
200 'type' => 'danger',
201 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
202 'msg' => 'access_denied'
203 );
204 return false;
205 }
206 else {
207 $username = $_data['username'];
208 }
209 }
210 elseif ($_SESSION['mailcow_cc_role'] == "user") {
211 $username = $_SESSION['mailcow_cc_username'];
212 }
213 else {
214 $_SESSION['return'][] = array(
215 'type' => 'danger',
216 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
217 'msg' => 'no_user_defined'
218 );
219 return false;
220 }
221 $active = intval($_data['active']);
222 $script_data = $_data['script_data'];
223 $script_desc = $_data['script_desc'];
224 $filter_type = $_data['filter_type'];
225 if (empty($script_data)) {
226 $_SESSION['return'][] = array(
227 'type' => 'danger',
228 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
229 'msg' => 'script_empty'
230 );
231 return false;
232 }
233 try {
234 $sieve->parse($script_data);
235 }
236 catch (Exception $e) {
237 $_SESSION['return'][] = array(
238 'type' => 'danger',
239 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
240 'msg' => array('sieve_error', $e->getMessage())
241 );
242 return false;
243 }
244 if (empty($script_data) || empty($script_desc)) {
245 $_SESSION['return'][] = array(
246 'type' => 'danger',
247 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
248 'msg' => 'value_missing'
249 );
250 return false;
251 }
252 if ($filter_type != 'postfilter' && $filter_type != 'prefilter') {
253 $_SESSION['return'][] = array(
254 'type' => 'danger',
255 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
256 'msg' => 'filter_type'
257 );
258 return false;
259 }
260 if (!empty($active)) {
261 $script_name = 'active';
262 $stmt = $pdo->prepare("UPDATE `sieve_filters` SET `script_name` = 'inactive' WHERE `username` = :username AND `filter_type` = :filter_type");
263 $stmt->execute(array(
264 ':username' => $username,
265 ':filter_type' => $filter_type
266 ));
267 }
268 else {
269 $script_name = 'inactive';
270 }
271 $stmt = $pdo->prepare("INSERT INTO `sieve_filters` (`username`, `script_data`, `script_desc`, `script_name`, `filter_type`)
272 VALUES (:username, :script_data, :script_desc, :script_name, :filter_type)");
273 $stmt->execute(array(
274 ':username' => $username,
275 ':script_data' => $script_data,
276 ':script_desc' => $script_desc,
277 ':script_name' => $script_name,
278 ':filter_type' => $filter_type
279 ));
280 $_SESSION['return'][] = array(
281 'type' => 'success',
282 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
283 'msg' => array('mailbox_modified', $username)
284 );
285 break;
286 case 'syncjob':
287 if (!isset($_SESSION['acl']['syncjobs']) || $_SESSION['acl']['syncjobs'] != "1" ) {
288 $_SESSION['return'][] = array(
289 'type' => 'danger',
290 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
291 'msg' => 'access_denied'
292 );
293 return false;
294 }
295 if (isset($_data['username']) && filter_var($_data['username'], FILTER_VALIDATE_EMAIL)) {
296 if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data['username'])) {
297 $_SESSION['return'][] = array(
298 'type' => 'danger',
299 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
300 'msg' => 'access_denied'
301 );
302 return false;
303 }
304 else {
305 $username = $_data['username'];
306 }
307 }
308 elseif ($_SESSION['mailcow_cc_role'] == "user") {
309 $username = $_SESSION['mailcow_cc_username'];
310 }
311 else {
312 $_SESSION['return'][] = array(
313 'type' => 'danger',
314 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
315 'msg' => 'no_user_defined'
316 );
317 return false;
318 }
319 $active = intval($_data['active']);
320 $subscribeall = intval($_data['subscribeall']);
321 $delete2duplicates = intval($_data['delete2duplicates']);
322 $delete1 = intval($_data['delete1']);
323 $delete2 = intval($_data['delete2']);
324 $timeout1 = intval($_data['timeout1']);
325 $timeout2 = intval($_data['timeout2']);
326 $skipcrossduplicates = intval($_data['skipcrossduplicates']);
327 $automap = intval($_data['automap']);
328 $port1 = $_data['port1'];
329 $host1 = strtolower($_data['host1']);
330 $password1 = $_data['password1'];
331 $exclude = $_data['exclude'];
332 $maxage = $_data['maxage'];
333 $maxbytespersecond = $_data['maxbytespersecond'];
334 $subfolder2 = $_data['subfolder2'];
335 $user1 = $_data['user1'];
336 $mins_interval = $_data['mins_interval'];
337 $enc1 = $_data['enc1'];
338 $custom_params = (empty(trim($_data['custom_params']))) ? '' : trim($_data['custom_params']);
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +0100339
340 // validate custom params
341 foreach (explode('-', $custom_params) as $param){
342 if(empty($param)) continue;
343
344 // extract option
345 if (str_contains($param, '=')) $param = explode('=', $param)[0];
346 else $param = rtrim($param, ' ');
347 // remove first char if first char is -
348 if ($param[0] == '-') $param = ltrim($param, $param[0]);
349
350 if (str_contains($param, ' ')) {
351 // bad char
352 $_SESSION['return'][] = array(
353 'type' => 'danger',
354 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
355 'msg' => 'bad character SPACE'
356 );
357 return false;
358 }
359
360 // check if param is whitelisted
361 if (!in_array(strtolower($param), $GLOBALS["IMAPSYNC_OPTIONS"]["whitelist"])){
362 // bad option
363 $_SESSION['return'][] = array(
364 'type' => 'danger',
365 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
366 'msg' => 'bad option '. $param
367 );
368 return false;
369 }
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +0100370 }
371 if (empty($subfolder2)) {
372 $subfolder2 = "";
373 }
374 if (!isset($maxage) || !filter_var($maxage, FILTER_VALIDATE_INT, array('options' => array('min_range' => 1, 'max_range' => 32000)))) {
375 $maxage = "0";
376 }
377 if (!isset($timeout1) || !filter_var($timeout1, FILTER_VALIDATE_INT, array('options' => array('min_range' => 1, 'max_range' => 32000)))) {
378 $timeout1 = "600";
379 }
380 if (!isset($timeout2) || !filter_var($timeout2, FILTER_VALIDATE_INT, array('options' => array('min_range' => 1, 'max_range' => 32000)))) {
381 $timeout2 = "600";
382 }
383 if (!isset($maxbytespersecond) || !filter_var($maxbytespersecond, FILTER_VALIDATE_INT, array('options' => array('min_range' => 1, 'max_range' => 125000000)))) {
384 $maxbytespersecond = "0";
385 }
386 if (!filter_var($port1, FILTER_VALIDATE_INT, array('options' => array('min_range' => 1, 'max_range' => 65535)))) {
387 $_SESSION['return'][] = array(
388 'type' => 'danger',
389 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
390 'msg' => 'access_denied'
391 );
392 return false;
393 }
394 if (!filter_var($mins_interval, FILTER_VALIDATE_INT, array('options' => array('min_range' => 1, 'max_range' => 43800)))) {
395 $_SESSION['return'][] = array(
396 'type' => 'danger',
397 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
398 'msg' => 'access_denied'
399 );
400 return false;
401 }
402 // if (!is_valid_domain_name($host1)) {
403 // $_SESSION['return'][] = array(
404 // 'type' => 'danger',
405 // 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
406 // 'msg' => 'access_denied'
407 // );
408 // return false;
409 // }
410 if ($enc1 != "TLS" && $enc1 != "SSL" && $enc1 != "PLAIN") {
411 $_SESSION['return'][] = array(
412 'type' => 'danger',
413 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
414 'msg' => 'access_denied'
415 );
416 return false;
417 }
418 if (@preg_match("/" . $exclude . "/", null) === false) {
419 $_SESSION['return'][] = array(
420 'type' => 'danger',
421 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
422 'msg' => 'access_denied'
423 );
424 return false;
425 }
426 $stmt = $pdo->prepare("SELECT '1' FROM `imapsync`
427 WHERE `user2` = :user2 AND `user1` = :user1 AND `host1` = :host1");
428 $stmt->execute(array(':user1' => $user1, ':user2' => $username, ':host1' => $host1));
429 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
430 if ($num_results != 0) {
431 $_SESSION['return'][] = array(
432 'type' => 'danger',
433 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
434 'msg' => array('object_exists', htmlspecialchars($host1 . ' / ' . $user1))
435 );
436 return false;
437 }
438 $stmt = $pdo->prepare("INSERT INTO `imapsync` (`user2`, `exclude`, `delete1`, `delete2`, `timeout1`, `timeout2`, `automap`, `skipcrossduplicates`, `maxbytespersecond`, `subscribeall`, `maxage`, `subfolder2`, `host1`, `authmech1`, `user1`, `password1`, `mins_interval`, `port1`, `enc1`, `delete2duplicates`, `custom_params`, `active`)
439 VALUES (:user2, :exclude, :delete1, :delete2, :timeout1, :timeout2, :automap, :skipcrossduplicates, :maxbytespersecond, :subscribeall, :maxage, :subfolder2, :host1, :authmech1, :user1, :password1, :mins_interval, :port1, :enc1, :delete2duplicates, :custom_params, :active)");
440 $stmt->execute(array(
441 ':user2' => $username,
442 ':custom_params' => $custom_params,
443 ':exclude' => $exclude,
444 ':maxage' => $maxage,
445 ':delete1' => $delete1,
446 ':delete2' => $delete2,
447 ':timeout1' => $timeout1,
448 ':timeout2' => $timeout2,
449 ':automap' => $automap,
450 ':skipcrossduplicates' => $skipcrossduplicates,
451 ':maxbytespersecond' => $maxbytespersecond,
452 ':subscribeall' => $subscribeall,
453 ':subfolder2' => $subfolder2,
454 ':host1' => $host1,
455 ':authmech1' => 'PLAIN',
456 ':user1' => $user1,
457 ':password1' => $password1,
458 ':mins_interval' => $mins_interval,
459 ':port1' => $port1,
460 ':enc1' => $enc1,
461 ':delete2duplicates' => $delete2duplicates,
462 ':active' => $active,
463 ));
464 $_SESSION['return'][] = array(
465 'type' => 'success',
466 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
467 'msg' => array('mailbox_modified', $username)
468 );
469 break;
470 case 'domain':
471 if ($_SESSION['mailcow_cc_role'] != "admin") {
472 $_SESSION['return'][] = array(
473 'type' => 'danger',
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +0100474 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_extra),
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +0100475 'msg' => 'access_denied'
476 );
477 return false;
478 }
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +0200479 $domain = idn_to_ascii(strtolower(trim($_data['domain'])), 0, INTL_IDNA_VARIANT_UTS46);
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +0100480 $description = $_data['description'];
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +0100481 if (empty($description)) $description = $domain;
482 $tags = (array)$_data['tags'];
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +0200483 $aliases = (int)$_data['aliases'];
484 $mailboxes = (int)$_data['mailboxes'];
485 $defquota = (int)$_data['defquota'];
486 $maxquota = (int)$_data['maxquota'];
487 $restart_sogo = (int)$_data['restart_sogo'];
488 $quota = (int)$_data['quota'];
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +0100489 if ($defquota > $maxquota) {
490 $_SESSION['return'][] = array(
491 'type' => 'danger',
492 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
493 'msg' => 'mailbox_defquota_exceeds_mailbox_maxquota'
494 );
495 return false;
496 }
497 if ($maxquota > $quota) {
498 $_SESSION['return'][] = array(
499 'type' => 'danger',
500 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
501 'msg' => 'mailbox_quota_exceeds_domain_quota'
502 );
503 return false;
504 }
505 if ($defquota == "0" || empty($defquota)) {
506 $_SESSION['return'][] = array(
507 'type' => 'danger',
508 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
509 'msg' => 'defquota_empty'
510 );
511 return false;
512 }
513 if ($maxquota == "0" || empty($maxquota)) {
514 $_SESSION['return'][] = array(
515 'type' => 'danger',
516 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
517 'msg' => 'maxquota_empty'
518 );
519 return false;
520 }
521 $active = intval($_data['active']);
522 $relay_all_recipients = intval($_data['relay_all_recipients']);
523 $relay_unknown_only = intval($_data['relay_unknown_only']);
524 $backupmx = intval($_data['backupmx']);
525 $gal = intval($_data['gal']);
526 if ($relay_all_recipients == 1) {
527 $backupmx = '1';
528 }
529 if ($relay_unknown_only == 1) {
530 $backupmx = 1;
531 $relay_all_recipients = 1;
532 }
533 if (!is_valid_domain_name($domain)) {
534 $_SESSION['return'][] = array(
535 'type' => 'danger',
536 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
537 'msg' => 'domain_invalid'
538 );
539 return false;
540 }
541 foreach (array($quota, $maxquota, $mailboxes, $aliases) as $data) {
542 if (!is_numeric($data)) {
543 $_SESSION['return'][] = array(
544 'type' => 'danger',
545 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
546 'msg' => array('object_is_not_numeric', htmlspecialchars($data))
547 );
548 return false;
549 }
550 }
551 $stmt = $pdo->prepare("SELECT `domain` FROM `domain`
552 WHERE `domain` = :domain");
553 $stmt->execute(array(':domain' => $domain));
554 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
555 $stmt = $pdo->prepare("SELECT `alias_domain` FROM `alias_domain`
556 WHERE `alias_domain` = :domain");
557 $stmt->execute(array(':domain' => $domain));
558 $num_results = $num_results + count($stmt->fetchAll(PDO::FETCH_ASSOC));
559 if ($num_results != 0) {
560 $_SESSION['return'][] = array(
561 'type' => 'danger',
562 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
563 'msg' => array('domain_exists', htmlspecialchars($domain))
564 );
565 return false;
566 }
567 if ($domain == getenv('MAILCOW_HOSTNAME')) {
568 $_SESSION['return'][] = array(
569 'type' => 'danger',
570 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
571 'msg' => 'domain_cannot_match_hostname'
572 );
573 return false;
574 }
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +0100575
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +0100576 $stmt = $pdo->prepare("DELETE FROM `sender_acl` WHERE `external` = 1 AND `send_as` LIKE :domain");
577 $stmt->execute(array(
578 ':domain' => '%@' . $domain
579 ));
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +0100580 // save domain
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +0100581 $stmt = $pdo->prepare("INSERT INTO `domain` (`domain`, `description`, `aliases`, `mailboxes`, `defquota`, `maxquota`, `quota`, `backupmx`, `gal`, `active`, `relay_unknown_only`, `relay_all_recipients`)
582 VALUES (:domain, :description, :aliases, :mailboxes, :defquota, :maxquota, :quota, :backupmx, :gal, :active, :relay_unknown_only, :relay_all_recipients)");
583 $stmt->execute(array(
584 ':domain' => $domain,
585 ':description' => $description,
586 ':aliases' => $aliases,
587 ':mailboxes' => $mailboxes,
588 ':defquota' => $defquota,
589 ':maxquota' => $maxquota,
590 ':quota' => $quota,
591 ':backupmx' => $backupmx,
592 ':gal' => $gal,
593 ':active' => $active,
594 ':relay_unknown_only' => $relay_unknown_only,
595 ':relay_all_recipients' => $relay_all_recipients
596 ));
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +0100597 // save tags
598 foreach($tags as $index => $tag){
599 if (empty($tag)) continue;
600 if ($index > $GLOBALS['TAGGING_LIMIT']) {
601 $_SESSION['return'][] = array(
602 'type' => 'warning',
603 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
604 'msg' => array('tag_limit_exceeded', 'limit '.$GLOBALS['TAGGING_LIMIT'])
605 );
606 break;
607 }
608 $stmt = $pdo->prepare("INSERT INTO `tags_domain` (`domain`, `tag_name`) VALUES (:domain, :tag_name)");
609 $stmt->execute(array(
610 ':domain' => $domain,
611 ':tag_name' => $tag,
612 ));
613 }
614
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +0100615 try {
616 $redis->hSet('DOMAIN_MAP', $domain, 1);
617 }
618 catch (RedisException $e) {
619 $_SESSION['return'][] = array(
620 'type' => 'danger',
621 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
622 'msg' => array('redis_error', $e)
623 );
624 return false;
625 }
626 if (!empty(intval($_data['rl_value']))) {
627 ratelimit('edit', 'domain', array('rl_value' => $_data['rl_value'], 'rl_frame' => $_data['rl_frame'], 'object' => $domain));
628 }
Matthias Andreas Benkard12a57352021-12-28 18:02:04 +0100629 if (!empty($_data['key_size']) && !empty($_data['dkim_selector'])) {
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +0100630 if (!empty($redis->hGet('DKIM_SELECTORS', $domain))) {
631 $_SESSION['return'][] = array(
632 'type' => 'success',
633 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
634 'msg' => 'domain_add_dkim_available'
635 );
636 }
637 else {
638 dkim('add', array('key_size' => $_data['key_size'], 'dkim_selector' => $_data['dkim_selector'], 'domains' => $domain));
639 }
Matthias Andreas Benkard12a57352021-12-28 18:02:04 +0100640 }
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +0100641 if (!empty($restart_sogo)) {
642 $restart_response = json_decode(docker('post', 'sogo-mailcow', 'restart'), true);
643 if ($restart_response['type'] == "success") {
644 $_SESSION['return'][] = array(
645 'type' => 'success',
646 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
647 'msg' => array('domain_added', htmlspecialchars($domain))
648 );
649 return true;
650 }
651 else {
652 $_SESSION['return'][] = array(
653 'type' => 'warning',
654 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
655 'msg' => 'domain_added_sogo_failed'
656 );
657 return false;
658 }
659 }
660 $_SESSION['return'][] = array(
661 'type' => 'success',
662 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
663 'msg' => array('domain_added', htmlspecialchars($domain))
664 );
665 return true;
666 break;
667 case 'alias':
668 $addresses = array_map('trim', preg_split( "/( |,|;|\n)/", $_data['address']));
669 $gotos = array_map('trim', preg_split( "/( |,|;|\n)/", $_data['goto']));
670 $active = intval($_data['active']);
671 $sogo_visible = intval($_data['sogo_visible']);
672 $goto_null = intval($_data['goto_null']);
673 $goto_spam = intval($_data['goto_spam']);
674 $goto_ham = intval($_data['goto_ham']);
675 $private_comment = $_data['private_comment'];
676 $public_comment = $_data['public_comment'];
677 if (strlen($private_comment) > 160 | strlen($public_comment) > 160){
678 $_SESSION['return'][] = array(
679 'type' => 'danger',
680 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
681 'msg' => 'comment_too_long'
682 );
683 return false;
684 }
685 if (empty($addresses[0])) {
686 $_SESSION['return'][] = array(
687 'type' => 'danger',
688 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
689 'msg' => 'alias_empty'
690 );
691 return false;
692 }
693 if (empty($gotos[0]) && ($goto_null + $goto_spam + $goto_ham == 0)) {
694 $_SESSION['return'][] = array(
695 'type' => 'danger',
696 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
697 'msg' => 'goto_empty'
698 );
699 return false;
700 }
701 if ($goto_null == "1") {
702 $goto = "null@localhost";
703 }
704 elseif ($goto_spam == "1") {
705 $goto = "spam@localhost";
706 }
707 elseif ($goto_ham == "1") {
708 $goto = "ham@localhost";
709 }
710 else {
711 foreach ($gotos as $i => &$goto) {
712 if (empty($goto)) {
713 continue;
714 }
715 $goto_domain = idn_to_ascii(substr(strstr($goto, '@'), 1), 0, INTL_IDNA_VARIANT_UTS46);
716 $goto_local_part = strstr($goto, '@', true);
717 $goto = $goto_local_part.'@'.$goto_domain;
718 $stmt = $pdo->prepare("SELECT `username` FROM `mailbox`
719 WHERE `kind` REGEXP 'location|thing|group'
720 AND `username`= :goto");
721 $stmt->execute(array(':goto' => $goto));
722 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
723 if ($num_results != 0) {
724 $_SESSION['return'][] = array(
725 'type' => 'danger',
726 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
727 'msg' => array('goto_invalid', htmlspecialchars($goto))
728 );
729 unset($gotos[$i]);
730 continue;
731 }
732 if (!filter_var($goto, FILTER_VALIDATE_EMAIL) === true) {
733 $_SESSION['return'][] = array(
734 'type' => 'danger',
735 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
736 'msg' => array('goto_invalid', htmlspecialchars($goto))
737 );
738 unset($gotos[$i]);
739 continue;
740 }
741 }
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +0200742 $gotos = array_unique($gotos);
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +0100743 $gotos = array_filter($gotos);
744 if (empty($gotos)) { return false; }
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +0200745 $goto = implode(",", (array)$gotos);
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +0100746 }
747 foreach ($addresses as $address) {
748 if (empty($address)) {
749 continue;
750 }
751 if (in_array($address, $gotos)) {
752 continue;
753 }
754 $domain = idn_to_ascii(substr(strstr($address, '@'), 1), 0, INTL_IDNA_VARIANT_UTS46);
755 $local_part = strstr($address, '@', true);
756 $address = $local_part.'@'.$domain;
757 $domaindata = mailbox('get', 'domain_details', $domain);
758 if (is_array($domaindata) && $domaindata['aliases_left'] == "0") {
759 $_SESSION['return'][] = array(
760 'type' => 'danger',
761 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
762 'msg' => 'max_alias_exceeded'
763 );
764 return false;
765 }
766 $stmt = $pdo->prepare("SELECT `address` FROM `alias`
767 WHERE `address`= :address OR `address` IN (
768 SELECT `username` FROM `mailbox`, `alias_domain`
769 WHERE (
770 `alias_domain`.`alias_domain` = :address_d
771 AND `mailbox`.`username` = CONCAT(:address_l, '@', alias_domain.target_domain)))");
772 $stmt->execute(array(
773 ':address' => $address,
774 ':address_l' => $local_part,
775 ':address_d' => $domain
776 ));
777 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
778 if ($num_results != 0) {
779 $_SESSION['return'][] = array(
780 'type' => 'danger',
781 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
782 'msg' => array('is_alias_or_mailbox', htmlspecialchars($address))
783 );
784 continue;
785 }
786 $stmt = $pdo->prepare("SELECT `domain` FROM `domain`
787 WHERE `domain`= :domain1 OR `domain` = (SELECT `target_domain` FROM `alias_domain` WHERE `alias_domain` = :domain2)");
788 $stmt->execute(array(':domain1' => $domain, ':domain2' => $domain));
789 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
790 if ($num_results == 0) {
791 $_SESSION['return'][] = array(
792 'type' => 'danger',
793 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
794 'msg' => array('domain_not_found', htmlspecialchars($domain))
795 );
796 continue;
797 }
798 $stmt = $pdo->prepare("SELECT `address` FROM `spamalias`
799 WHERE `address`= :address");
800 $stmt->execute(array(':address' => $address));
801 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
802 if ($num_results != 0) {
803 $_SESSION['return'][] = array(
804 'type' => 'danger',
805 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
806 'msg' => array('is_spam_alias', htmlspecialchars($address))
807 );
808 continue;
809 }
810 if ((!filter_var($address, FILTER_VALIDATE_EMAIL) === true) && !empty($local_part)) {
811 $_SESSION['return'][] = array(
812 'type' => 'danger',
813 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
814 'msg' => array('alias_invalid', $address)
815 );
816 continue;
817 }
818 if (!hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $domain)) {
819 $_SESSION['return'][] = array(
820 'type' => 'danger',
821 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
822 'msg' => 'access_denied'
823 );
824 continue;
825 }
826 $stmt = $pdo->prepare("INSERT INTO `alias` (`address`, `public_comment`, `private_comment`, `goto`, `domain`, `sogo_visible`, `active`)
827 VALUES (:address, :public_comment, :private_comment, :goto, :domain, :sogo_visible, :active)");
828 if (!filter_var($address, FILTER_VALIDATE_EMAIL) === true) {
829 $stmt->execute(array(
830 ':address' => '@'.$domain,
831 ':public_comment' => $public_comment,
832 ':private_comment' => $private_comment,
833 ':address' => '@'.$domain,
834 ':goto' => $goto,
835 ':domain' => $domain,
836 ':sogo_visible' => $sogo_visible,
837 ':active' => $active
838 ));
839 }
840 else {
841 $stmt->execute(array(
842 ':address' => $address,
843 ':public_comment' => $public_comment,
844 ':private_comment' => $private_comment,
845 ':goto' => $goto,
846 ':domain' => $domain,
847 ':sogo_visible' => $sogo_visible,
848 ':active' => $active
849 ));
850 }
851 $id = $pdo->lastInsertId();
852 $_SESSION['return'][] = array(
853 'type' => 'success',
854 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
855 'msg' => array('alias_added', $address, $id)
856 );
857 }
858 break;
859 case 'alias_domain':
860 $active = intval($_data['active']);
861 $alias_domains = array_map('trim', preg_split( "/( |,|;|\n)/", $_data['alias_domain']));
862 $alias_domains = array_filter($alias_domains);
863 $target_domain = idn_to_ascii(strtolower(trim($_data['target_domain'])), 0, INTL_IDNA_VARIANT_UTS46);
864 if (!isset($_SESSION['acl']['alias_domains']) || $_SESSION['acl']['alias_domains'] != "1" ) {
865 $_SESSION['return'][] = array(
866 'type' => 'danger',
867 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
868 'msg' => 'access_denied'
869 );
870 return false;
871 }
872 if (!is_valid_domain_name($target_domain)) {
873 $_SESSION['return'][] = array(
874 'type' => 'danger',
875 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
876 'msg' => 'target_domain_invalid'
877 );
878 return false;
879 }
880 if (!hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $target_domain)) {
881 $_SESSION['return'][] = array(
882 'type' => 'danger',
883 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
884 'msg' => 'access_denied'
885 );
886 return false;
887 }
888 foreach ($alias_domains as $i => $alias_domain) {
889 $alias_domain = idn_to_ascii(strtolower(trim($alias_domain)), 0, INTL_IDNA_VARIANT_UTS46);
890 if (!is_valid_domain_name($alias_domain)) {
891 $_SESSION['return'][] = array(
892 'type' => 'danger',
893 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
894 'msg' => array('alias_domain_invalid', htmlspecialchars(alias_domain))
895 );
896 continue;
897 }
898 if ($alias_domain == $target_domain) {
899 $_SESSION['return'][] = array(
900 'type' => 'danger',
901 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
902 'msg' => array('aliasd_targetd_identical', htmlspecialchars($target_domain))
903 );
904 continue;
905 }
906 $stmt = $pdo->prepare("SELECT `domain` FROM `domain`
907 WHERE `domain`= :target_domain");
908 $stmt->execute(array(':target_domain' => $target_domain));
909 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
910 if ($num_results == 0) {
911 $_SESSION['return'][] = array(
912 'type' => 'danger',
913 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
914 'msg' => array('targetd_not_found', htmlspecialchars($target_domain))
915 );
916 continue;
917 }
918 $stmt = $pdo->prepare("SELECT `domain` FROM `domain`
919 WHERE `domain`= :target_domain AND `backupmx` = '1'");
920 $stmt->execute(array(':target_domain' => $target_domain));
921 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
922 if ($num_results == 1) {
923 $_SESSION['return'][] = array(
924 'type' => 'danger',
925 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
926 'msg' => array('targetd_relay_domain', htmlspecialchars($target_domain))
927 );
928 continue;
929 }
930 $stmt = $pdo->prepare("SELECT `alias_domain` FROM `alias_domain` WHERE `alias_domain`= :alias_domain
931 UNION
932 SELECT `domain` FROM `domain` WHERE `domain`= :alias_domain_in_domain");
933 $stmt->execute(array(':alias_domain' => $alias_domain, ':alias_domain_in_domain' => $alias_domain));
934 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
935 if ($num_results != 0) {
936 $_SESSION['return'][] = array(
937 'type' => 'danger',
938 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
939 'msg' => array('alias_domain_invalid', $alias_domain)
940 );
941 continue;
942 }
943 $stmt = $pdo->prepare("DELETE FROM `sender_acl` WHERE `external` = 1 AND `send_as` LIKE :domain");
944 $stmt->execute(array(
945 ':domain' => '%@' . $domain
946 ));
947 $stmt = $pdo->prepare("INSERT INTO `alias_domain` (`alias_domain`, `target_domain`, `active`)
948 VALUES (:alias_domain, :target_domain, :active)");
949 $stmt->execute(array(
950 ':alias_domain' => $alias_domain,
951 ':target_domain' => $target_domain,
952 ':active' => $active
953 ));
954 try {
955 $redis->hSet('DOMAIN_MAP', $alias_domain, 1);
956 }
957 catch (RedisException $e) {
958 $_SESSION['return'][] = array(
959 'type' => 'danger',
960 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
961 'msg' => array('redis_error', $e)
962 );
963 return false;
964 }
965 if (!empty(intval($_data['rl_value']))) {
966 ratelimit('edit', 'domain', array('rl_value' => $_data['rl_value'], 'rl_frame' => $_data['rl_frame'], 'object' => $alias_domain));
967 }
Matthias Andreas Benkard12a57352021-12-28 18:02:04 +0100968 if (!empty($_data['key_size']) && !empty($_data['dkim_selector'])) {
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +0100969 if (!empty($redis->hGet('DKIM_SELECTORS', $alias_domain))) {
970 $_SESSION['return'][] = array(
971 'type' => 'success',
972 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
973 'msg' => 'domain_add_dkim_available'
974 );
975 }
976 else {
977 dkim('add', array('key_size' => $_data['key_size'], 'dkim_selector' => $_data['dkim_selector'], 'domains' => $alias_domain));
978 }
Matthias Andreas Benkard12a57352021-12-28 18:02:04 +0100979 }
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +0100980 $_SESSION['return'][] = array(
981 'type' => 'success',
982 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
983 'msg' => array('aliasd_added', htmlspecialchars($alias_domain))
984 );
985 }
986 break;
987 case 'mailbox':
988 $local_part = strtolower(trim($_data['local_part']));
989 $domain = idn_to_ascii(strtolower(trim($_data['domain'])), 0, INTL_IDNA_VARIANT_UTS46);
990 $username = $local_part . '@' . $domain;
991 if (!filter_var($username, FILTER_VALIDATE_EMAIL)) {
992 $_SESSION['return'][] = array(
993 'type' => 'danger',
994 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
995 'msg' => 'mailbox_invalid'
996 );
997 return false;
998 }
999 if (empty($_data['local_part'])) {
1000 $_SESSION['return'][] = array(
1001 'type' => 'danger',
1002 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1003 'msg' => 'mailbox_invalid'
1004 );
1005 return false;
1006 }
1007 $password = $_data['password'];
1008 $password2 = $_data['password2'];
1009 $name = ltrim(rtrim($_data['name'], '>'), '<');
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +01001010 $tags = $_data['tags'];
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02001011 $quota_m = intval($_data['quota']);
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01001012 if ((!isset($_SESSION['acl']['unlimited_quota']) || $_SESSION['acl']['unlimited_quota'] != "1") && $quota_m === 0) {
1013 $_SESSION['return'][] = array(
1014 'type' => 'danger',
1015 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1016 'msg' => 'unlimited_quota_acl'
1017 );
1018 return false;
1019 }
1020 if (empty($name)) {
1021 $name = $local_part;
1022 }
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +01001023 if (isset($_data['protocol_access'])) {
1024 $_data['protocol_access'] = (array)$_data['protocol_access'];
1025 $_data['imap_access'] = (in_array('imap', $_data['protocol_access'])) ? 1 : 0;
1026 $_data['pop3_access'] = (in_array('pop3', $_data['protocol_access'])) ? 1 : 0;
1027 $_data['smtp_access'] = (in_array('smtp', $_data['protocol_access'])) ? 1 : 0;
1028 $_data['sieve_access'] = (in_array('sieve', $_data['protocol_access'])) ? 1 : 0;
1029 }
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01001030 $active = intval($_data['active']);
1031 $force_pw_update = (isset($_data['force_pw_update'])) ? intval($_data['force_pw_update']) : intval($MAILBOX_DEFAULT_ATTRIBUTES['force_pw_update']);
1032 $tls_enforce_in = (isset($_data['tls_enforce_in'])) ? intval($_data['tls_enforce_in']) : intval($MAILBOX_DEFAULT_ATTRIBUTES['tls_enforce_in']);
1033 $tls_enforce_out = (isset($_data['tls_enforce_out'])) ? intval($_data['tls_enforce_out']) : intval($MAILBOX_DEFAULT_ATTRIBUTES['tls_enforce_out']);
1034 $sogo_access = (isset($_data['sogo_access'])) ? intval($_data['sogo_access']) : intval($MAILBOX_DEFAULT_ATTRIBUTES['sogo_access']);
1035 $imap_access = (isset($_data['imap_access'])) ? intval($_data['imap_access']) : intval($MAILBOX_DEFAULT_ATTRIBUTES['imap_access']);
1036 $pop3_access = (isset($_data['pop3_access'])) ? intval($_data['pop3_access']) : intval($MAILBOX_DEFAULT_ATTRIBUTES['pop3_access']);
1037 $smtp_access = (isset($_data['smtp_access'])) ? intval($_data['smtp_access']) : intval($MAILBOX_DEFAULT_ATTRIBUTES['smtp_access']);
Matthias Andreas Benkard12a57352021-12-28 18:02:04 +01001038 $sieve_access = (isset($_data['sieve_access'])) ? intval($_data['sieve_access']) : intval($MAILBOX_DEFAULT_ATTRIBUTES['sieve_access']);
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02001039 $relayhost = (isset($_data['relayhost'])) ? intval($_data['relayhost']) : 0;
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01001040 $quarantine_notification = (isset($_data['quarantine_notification'])) ? strval($_data['quarantine_notification']) : strval($MAILBOX_DEFAULT_ATTRIBUTES['quarantine_notification']);
1041 $quarantine_category = (isset($_data['quarantine_category'])) ? strval($_data['quarantine_category']) : strval($MAILBOX_DEFAULT_ATTRIBUTES['quarantine_category']);
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02001042 $quota_b = ($quota_m * 1048576);
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01001043 $mailbox_attrs = json_encode(
1044 array(
1045 'force_pw_update' => strval($force_pw_update),
1046 'tls_enforce_in' => strval($tls_enforce_in),
1047 'tls_enforce_out' => strval($tls_enforce_out),
1048 'sogo_access' => strval($sogo_access),
1049 'imap_access' => strval($imap_access),
1050 'pop3_access' => strval($pop3_access),
1051 'smtp_access' => strval($smtp_access),
Matthias Andreas Benkard12a57352021-12-28 18:02:04 +01001052 'sieve_access' => strval($sieve_access),
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02001053 'relayhost' => strval($relayhost),
1054 'passwd_update' => time(),
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01001055 'mailbox_format' => strval($MAILBOX_DEFAULT_ATTRIBUTES['mailbox_format']),
1056 'quarantine_notification' => strval($quarantine_notification),
1057 'quarantine_category' => strval($quarantine_category)
1058 )
1059 );
1060 if (!is_valid_domain_name($domain)) {
1061 $_SESSION['return'][] = array(
1062 'type' => 'danger',
1063 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1064 'msg' => 'domain_invalid'
1065 );
1066 return false;
1067 }
1068 if (!hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $domain)) {
1069 $_SESSION['return'][] = array(
1070 'type' => 'danger',
1071 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1072 'msg' => 'access_denied'
1073 );
1074 return false;
1075 }
1076 $stmt = $pdo->prepare("SELECT `mailboxes`, `maxquota`, `quota` FROM `domain`
1077 WHERE `domain` = :domain");
1078 $stmt->execute(array(':domain' => $domain));
1079 $DomainData = $stmt->fetch(PDO::FETCH_ASSOC);
1080 $stmt = $pdo->prepare("SELECT
1081 COUNT(*) as count,
1082 COALESCE(ROUND(SUM(`quota`)/1048576), 0) as `quota`
1083 FROM `mailbox`
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02001084 WHERE (`kind` = '' OR `kind` = NULL)
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01001085 AND `domain` = :domain");
1086 $stmt->execute(array(':domain' => $domain));
1087 $MailboxData = $stmt->fetch(PDO::FETCH_ASSOC);
1088 $stmt = $pdo->prepare("SELECT `local_part` FROM `mailbox` WHERE `local_part` = :local_part and `domain`= :domain");
1089 $stmt->execute(array(':local_part' => $local_part, ':domain' => $domain));
1090 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
1091 if ($num_results != 0) {
1092 $_SESSION['return'][] = array(
1093 'type' => 'danger',
1094 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1095 'msg' => array('object_exists', htmlspecialchars($username))
1096 );
1097 return false;
1098 }
1099 $stmt = $pdo->prepare("SELECT `address` FROM `alias` WHERE address= :username");
1100 $stmt->execute(array(':username' => $username));
1101 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
1102 if ($num_results != 0) {
1103 $_SESSION['return'][] = array(
1104 'type' => 'danger',
1105 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1106 'msg' => array('is_alias', htmlspecialchars($username))
1107 );
1108 return false;
1109 }
1110 $stmt = $pdo->prepare("SELECT `address` FROM `spamalias` WHERE `address`= :username");
1111 $stmt->execute(array(':username' => $username));
1112 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
1113 if ($num_results != 0) {
1114 $_SESSION['return'][] = array(
1115 'type' => 'danger',
1116 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1117 'msg' => array('is_spam_alias', htmlspecialchars($username))
1118 );
1119 return false;
1120 }
1121 $stmt = $pdo->prepare("SELECT `domain` FROM `domain` WHERE `domain`= :domain");
1122 $stmt->execute(array(':domain' => $domain));
1123 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
1124 if ($num_results == 0) {
1125 $_SESSION['return'][] = array(
1126 'type' => 'danger',
1127 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1128 'msg' => array('domain_not_found', htmlspecialchars($domain))
1129 );
1130 return false;
1131 }
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02001132 if (password_check($password, $password2) !== true) {
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01001133 return false;
1134 }
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02001135 $password_hashed = hash_password($password);
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01001136 if ($MailboxData['count'] >= $DomainData['mailboxes']) {
1137 $_SESSION['return'][] = array(
1138 'type' => 'danger',
1139 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1140 'msg' => array('max_mailbox_exceeded', $MailboxData['count'], $DomainData['mailboxes'])
1141 );
1142 return false;
1143 }
1144 if ($quota_m > $DomainData['maxquota']) {
1145 $_SESSION['return'][] = array(
1146 'type' => 'danger',
1147 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1148 'msg' => array('mailbox_quota_exceeded', $DomainData['maxquota'])
1149 );
1150 return false;
1151 }
1152 if (($MailboxData['quota'] + $quota_m) > $DomainData['quota']) {
1153 $quota_left_m = ($DomainData['quota'] - $MailboxData['quota']);
1154 $_SESSION['return'][] = array(
1155 'type' => 'danger',
1156 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1157 'msg' => array('mailbox_quota_left_exceeded', $quota_left_m)
1158 );
1159 return false;
1160 }
1161 $stmt = $pdo->prepare("INSERT INTO `mailbox` (`username`, `password`, `name`, `quota`, `local_part`, `domain`, `attributes`, `active`)
1162 VALUES (:username, :password_hashed, :name, :quota_b, :local_part, :domain, :mailbox_attrs, :active)");
1163 $stmt->execute(array(
1164 ':username' => $username,
1165 ':password_hashed' => $password_hashed,
1166 ':name' => $name,
1167 ':quota_b' => $quota_b,
1168 ':local_part' => $local_part,
1169 ':domain' => $domain,
1170 ':mailbox_attrs' => $mailbox_attrs,
1171 ':active' => $active
1172 ));
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02001173 $stmt = $pdo->prepare("UPDATE `mailbox` SET
1174 `attributes` = JSON_SET(`attributes`, '$.passwd_update', NOW())
1175 WHERE `username` = :username");
1176 $stmt->execute(array(
1177 ':username' => $username
1178 ));
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +01001179 // save tags
1180 foreach($tags as $index => $tag){
1181 if (empty($tag)) continue;
1182 if ($index > $GLOBALS['TAGGING_LIMIT']) {
1183 $_SESSION['return'][] = array(
1184 'type' => 'warning',
1185 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1186 'msg' => array('tag_limit_exceeded', 'limit '.$GLOBALS['TAGGING_LIMIT'])
1187 );
1188 break;
1189 }
1190 $stmt = $pdo->prepare("INSERT INTO `tags_mailbox` (`username`, `tag_name`) VALUES (:username, :tag_name)");
1191 $stmt->execute(array(
1192 ':username' => $username,
1193 ':tag_name' => $tag,
1194 ));
1195 }
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01001196 $stmt = $pdo->prepare("INSERT INTO `quota2` (`username`, `bytes`, `messages`)
1197 VALUES (:username, '0', '0') ON DUPLICATE KEY UPDATE `bytes` = '0', `messages` = '0';");
1198 $stmt->execute(array(':username' => $username));
1199 $stmt = $pdo->prepare("INSERT INTO `quota2replica` (`username`, `bytes`, `messages`)
1200 VALUES (:username, '0', '0') ON DUPLICATE KEY UPDATE `bytes` = '0', `messages` = '0';");
1201 $stmt->execute(array(':username' => $username));
1202 $stmt = $pdo->prepare("INSERT INTO `alias` (`address`, `goto`, `domain`, `active`)
1203 VALUES (:username1, :username2, :domain, :active)");
1204 $stmt->execute(array(
1205 ':username1' => $username,
1206 ':username2' => $username,
1207 ':domain' => $domain,
1208 ':active' => $active
1209 ));
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +01001210
1211
1212 if (isset($_data['acl'])) {
1213 $_data['acl'] = (array)$_data['acl'];
1214 $_data['spam_alias'] = (in_array('spam_alias', $_data['acl'])) ? 1 : 0;
1215 $_data['tls_policy'] = (in_array('tls_policy', $_data['acl'])) ? 1 : 0;
1216 $_data['spam_score'] = (in_array('spam_score', $_data['acl'])) ? 1 : 0;
1217 $_data['spam_policy'] = (in_array('spam_policy', $_data['acl'])) ? 1 : 0;
1218 $_data['delimiter_action'] = (in_array('delimiter_action', $_data['acl'])) ? 1 : 0;
1219 $_data['syncjobs'] = (in_array('syncjobs', $_data['acl'])) ? 1 : 0;
1220 $_data['eas_reset'] = (in_array('eas_reset', $_data['acl'])) ? 1 : 0;
1221 $_data['sogo_profile_reset'] = (in_array('sogo_profile_reset', $_data['acl'])) ? 1 : 0;
1222 $_data['pushover'] = (in_array('pushover', $_data['acl'])) ? 1 : 0;
1223 $_data['quarantine'] = (in_array('quarantine', $_data['acl'])) ? 1 : 0;
1224 $_data['quarantine_attachments'] = (in_array('quarantine_attachments', $_data['acl'])) ? 1 : 0;
1225 $_data['quarantine_notification'] = (in_array('quarantine_notification', $_data['acl'])) ? 1 : 0;
1226 $_data['quarantine_category'] = (in_array('quarantine_category', $_data['acl'])) ? 1 : 0;
1227 $_data['app_passwds'] = (in_array('app_passwds', $_data['acl'])) ? 1 : 0;
1228
1229 $stmt = $pdo->prepare("INSERT INTO `user_acl`
1230 (`username`, `spam_alias`, `tls_policy`, `spam_score`, `spam_policy`, `delimiter_action`, `syncjobs`, `eas_reset`, `sogo_profile_reset`,
1231 `pushover`, `quarantine`, `quarantine_attachments`, `quarantine_notification`, `quarantine_category`, `app_passwds`)
1232 VALUES (:username, :spam_alias, :tls_policy, :spam_score, :spam_policy, :delimiter_action, :syncjobs, :eas_reset, :sogo_profile_reset,
1233 :pushover, :quarantine, :quarantine_attachments, :quarantine_notification, :quarantine_category, :app_passwds) ");
1234 $stmt->execute(array(
1235 ':username' => $username,
1236 ':spam_alias' => $_data['spam_alias'],
1237 ':tls_policy' => $_data['tls_policy'],
1238 ':spam_score' => $_data['spam_score'],
1239 ':spam_policy' => $_data['spam_policy'],
1240 ':delimiter_action' => $_data['delimiter_action'],
1241 ':syncjobs' => $_data['syncjobs'],
1242 ':eas_reset' => $_data['eas_reset'],
1243 ':sogo_profile_reset' => $_data['sogo_profile_reset'],
1244 ':pushover' => $_data['pushover'],
1245 ':quarantine' => $_data['quarantine'],
1246 ':quarantine_attachments' => $_data['quarantine_attachments'],
1247 ':quarantine_notification' => $_data['quarantine_notification'],
1248 ':quarantine_category' => $_data['quarantine_category'],
1249 ':app_passwds' => $_data['app_passwds']
1250 ));
1251 }
1252 else {
1253 $stmt = $pdo->prepare("INSERT INTO `user_acl` (`username`) VALUES (:username)");
1254 $stmt->execute(array(
1255 ':username' => $username
1256 ));
1257 }
1258
1259 if (isset($_data['rl_frame']) && isset($_data['rl_value'])){
1260 ratelimit('edit', 'mailbox', array(
1261 'object' => $username,
1262 'rl_frame' => $_data['rl_frame'],
1263 'rl_value' => $_data['rl_value']
1264 ));
1265 }
1266
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01001267 $_SESSION['return'][] = array(
1268 'type' => 'success',
1269 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1270 'msg' => array('mailbox_added', htmlspecialchars($username))
1271 );
1272 break;
1273 case 'resource':
1274 $domain = idn_to_ascii(strtolower(trim($_data['domain'])), 0, INTL_IDNA_VARIANT_UTS46);
1275 $description = $_data['description'];
1276 $local_part = preg_replace('/[^\da-z]/i', '', preg_quote($description, '/'));
1277 $name = $local_part . '@' . $domain;
1278 $kind = $_data['kind'];
1279 $multiple_bookings = intval($_data['multiple_bookings']);
1280 $active = intval($_data['active']);
1281 if (!filter_var($name, FILTER_VALIDATE_EMAIL)) {
1282 $_SESSION['return'][] = array(
1283 'type' => 'danger',
1284 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1285 'msg' => 'resource_invalid'
1286 );
1287 return false;
1288 }
1289 if (empty($description)) {
1290 $_SESSION['return'][] = array(
1291 'type' => 'danger',
1292 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1293 'msg' => 'description_invalid'
1294 );
1295 return false;
1296 }
1297 if (!isset($multiple_bookings) || $multiple_bookings < -1) {
1298 $multiple_bookings = -1;
1299 }
1300 if ($kind != 'location' && $kind != 'group' && $kind != 'thing') {
1301 $_SESSION['return'][] = array(
1302 'type' => 'danger',
1303 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1304 'msg' => 'resource_invalid'
1305 );
1306 return false;
1307 }
1308 if (!is_valid_domain_name($domain)) {
1309 $_SESSION['return'][] = array(
1310 'type' => 'danger',
1311 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1312 'msg' => 'domain_invalid'
1313 );
1314 return false;
1315 }
1316 if (!hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $domain)) {
1317 $_SESSION['return'][] = array(
1318 'type' => 'danger',
1319 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1320 'msg' => 'access_denied'
1321 );
1322 return false;
1323 }
1324 $stmt = $pdo->prepare("SELECT `username` FROM `mailbox` WHERE `username` = :name");
1325 $stmt->execute(array(':name' => $name));
1326 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
1327 if ($num_results != 0) {
1328 $_SESSION['return'][] = array(
1329 'type' => 'danger',
1330 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1331 'msg' => array('object_exists', htmlspecialchars($name))
1332 );
1333 return false;
1334 }
1335 $stmt = $pdo->prepare("SELECT `address` FROM `alias` WHERE address= :name");
1336 $stmt->execute(array(':name' => $name));
1337 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
1338 if ($num_results != 0) {
1339 $_SESSION['return'][] = array(
1340 'type' => 'danger',
1341 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1342 'msg' => array('is_alias', htmlspecialchars($name))
1343 );
1344 return false;
1345 }
1346 $stmt = $pdo->prepare("SELECT `address` FROM `spamalias` WHERE `address`= :name");
1347 $stmt->execute(array(':name' => $name));
1348 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
1349 if ($num_results != 0) {
1350 $_SESSION['return'][] = array(
1351 'type' => 'danger',
1352 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1353 'msg' => array('is_spam_alias', htmlspecialchars($name))
1354 );
1355 return false;
1356 }
1357 $stmt = $pdo->prepare("SELECT `domain` FROM `domain` WHERE `domain`= :domain");
1358 $stmt->execute(array(':domain' => $domain));
1359 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
1360 if ($num_results == 0) {
1361 $_SESSION['return'][] = array(
1362 'type' => 'danger',
1363 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1364 'msg' => array('domain_not_found', htmlspecialchars($domain))
1365 );
1366 return false;
1367 }
1368 $stmt = $pdo->prepare("INSERT INTO `mailbox` (`username`, `password`, `name`, `quota`, `local_part`, `domain`, `active`, `multiple_bookings`, `kind`)
1369 VALUES (:name, 'RESOURCE', :description, 0, :local_part, :domain, :active, :multiple_bookings, :kind)");
1370 $stmt->execute(array(
1371 ':name' => $name,
1372 ':description' => $description,
1373 ':local_part' => $local_part,
1374 ':domain' => $domain,
1375 ':active' => $active,
1376 ':kind' => $kind,
1377 ':multiple_bookings' => $multiple_bookings
1378 ));
1379 $_SESSION['return'][] = array(
1380 'type' => 'success',
1381 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1382 'msg' => array('resource_added', htmlspecialchars($name))
1383 );
1384 break;
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +01001385 case 'domain_templates':
1386 if ($_SESSION['mailcow_cc_role'] != "admin") {
1387 $_SESSION['return'][] = array(
1388 'type' => 'danger',
1389 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_extra),
1390 'msg' => 'access_denied'
1391 );
1392 return false;
1393 }
1394 if (empty($_data["template"])){
1395 $_SESSION['return'][] = array(
1396 'type' => 'danger',
1397 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_extra),
1398 'msg' => 'template_name_invalid'
1399 );
1400 return false;
1401 }
1402
1403 // check if template name exists, return false
1404 $stmt = $pdo->prepare("SELECT id FROM `templates` WHERE `type` = :type AND `template` = :template");
1405 $stmt->execute(array(
1406 ":type" => "domain",
1407 ":template" => $_data["template"]
1408 ));
1409 $row = $stmt->fetch(PDO::FETCH_ASSOC);
1410
1411 if (!empty($row)){
1412 $_SESSION['return'][] = array(
1413 'type' => 'danger',
1414 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_extra),
1415 'msg' => array('template_exists', $_data["template"])
1416 );
1417 return false;
1418 }
1419
1420 // check attributes
1421 $attr = array();
1422 $attr['tags'] = (isset($_data['tags'])) ? $_data['tags'] : array();
1423 $attr['max_num_aliases_for_domain'] = (!empty($_data['max_num_aliases_for_domain'])) ? intval($_data['max_num_aliases_for_domain']) : 400;
1424 $attr['max_num_mboxes_for_domain'] = (!empty($_data['max_num_mboxes_for_domain'])) ? intval($_data['max_num_mboxes_for_domain']) : 10;
1425 $attr['def_quota_for_mbox'] = (!empty($_data['def_quota_for_mbox'])) ? intval($_data['def_quota_for_mbox']) * 1048576 : 3072 * 1048576;
1426 $attr['max_quota_for_mbox'] = (!empty($_data['max_quota_for_mbox'])) ? intval($_data['max_quota_for_mbox']) * 1048576 : 10240 * 1048576;
1427 $attr['max_quota_for_domain'] = (!empty($_data['max_quota_for_domain'])) ? intval($_data['max_quota_for_domain']) * 1048576 : 10240 * 1048576;
1428 $attr['rl_frame'] = (!empty($_data['rl_frame'])) ? $_data['rl_frame'] : "s";
1429 $attr['rl_value'] = (!empty($_data['rl_value'])) ? $_data['rl_value'] : "";
1430 $attr['active'] = isset($_data['active']) ? intval($_data['active']) : 1;
1431 $attr['gal'] = (isset($_data['gal'])) ? intval($_data['gal']) : 1;
1432 $attr['backupmx'] = (isset($_data['backupmx'])) ? intval($_data['backupmx']) : 0;
1433 $attr['relay_all_recipients'] = (isset($_data['relay_all_recipients'])) ? intval($_data['relay_all_recipients']) : 0;
1434 $attr['relay_unknown_only'] = (isset($_data['relay_unknown_only'])) ? intval($_data['relay_unknown_only']) : 0;
1435 $attr['dkim_selector'] = (isset($_data['dkim_selector'])) ? $_data['dkim_selector'] : "dkim";
1436 $attr['key_size'] = isset($_data['key_size']) ? intval($_data['key_size']) : 2048;
1437
1438 // save template
1439 $stmt = $pdo->prepare("INSERT INTO `templates` (`type`, `template`, `attributes`)
1440 VALUES (:type, :template, :attributes)");
1441 $stmt->execute(array(
1442 ":type" => "domain",
1443 ":template" => $_data["template"],
1444 ":attributes" => json_encode($attr)
1445 ));
1446
1447 // success
1448 $_SESSION['return'][] = array(
1449 'type' => 'success',
1450 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1451 'msg' => array('template_added', $_data["template"])
1452 );
1453 return true;
1454 break;
1455 case 'mailbox_templates':
1456 if ($_SESSION['mailcow_cc_role'] != "admin") {
1457 $_SESSION['return'][] = array(
1458 'type' => 'danger',
1459 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_extra),
1460 'msg' => 'access_denied'
1461 );
1462 return false;
1463 }
1464 if (empty($_data["template"])){
1465 $_SESSION['return'][] = array(
1466 'type' => 'danger',
1467 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_extra),
1468 'msg' => 'template_name_invalid'
1469 );
1470 return false;
1471 }
1472
1473 // check if template name exists, return false
1474 $stmt = $pdo->prepare("SELECT id FROM `templates` WHERE `type` = :type AND `template` = :template");
1475 $stmt->execute(array(
1476 ":type" => "mailbox",
1477 ":template" => $_data["template"]
1478 ));
1479 $row = $stmt->fetch(PDO::FETCH_ASSOC);
1480 if (!empty($row)){
1481 $_SESSION['return'][] = array(
1482 'type' => 'danger',
1483 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_extra),
1484 'msg' => array('template_exists', $_data["template"])
1485 );
1486 return false;
1487 }
1488
1489
1490 // check attributes
1491 $attr = array();
1492 $attr["quota"] = isset($_data['quota']) ? intval($_data['quota']) * 1048576 : 0;
1493 $attr['tags'] = (isset($_data['tags'])) ? $_data['tags'] : array();
1494 $attr["quarantine_notification"] = (!empty($_data['quarantine_notification'])) ? $_data['quarantine_notification'] : strval($MAILBOX_DEFAULT_ATTRIBUTES['quarantine_notification']);
1495 $attr["quarantine_category"] = (!empty($_data['quarantine_category'])) ? $_data['quarantine_category'] : strval($MAILBOX_DEFAULT_ATTRIBUTES['quarantine_category']);
1496 $attr["rl_frame"] = (!empty($_data['rl_frame'])) ? $_data['rl_frame'] : "s";
1497 $attr["rl_value"] = (!empty($_data['rl_value'])) ? $_data['rl_value'] : "";
1498 $attr["force_pw_update"] = isset($_data['force_pw_update']) ? intval($_data['force_pw_update']) : intval($MAILBOX_DEFAULT_ATTRIBUTES['force_pw_update']);
1499 $attr["sogo_access"] = isset($_data['sogo_access']) ? intval($_data['sogo_access']) : intval($MAILBOX_DEFAULT_ATTRIBUTES['sogo_access']);
1500 $attr["active"] = isset($_data['active']) ? intval($_data['active']) : 1;
1501 $attr["tls_enforce_in"] = isset($_data['tls_enforce_in']) ? intval($_data['tls_enforce_in']) : intval($MAILBOX_DEFAULT_ATTRIBUTES['tls_enforce_in']);
1502 $attr["tls_enforce_out"] = isset($_data['tls_enforce_out']) ? intval($_data['tls_enforce_out']) : intval($MAILBOX_DEFAULT_ATTRIBUTES['tls_enforce_out']);
1503 if (isset($_data['protocol_access'])) {
1504 $_data['protocol_access'] = (array)$_data['protocol_access'];
1505 $attr['imap_access'] = (in_array('imap', $_data['protocol_access'])) ? 1 : intval($MAILBOX_DEFAULT_ATTRIBUTES['imap_access']);
1506 $attr['pop3_access'] = (in_array('pop3', $_data['protocol_access'])) ? 1 : intval($MAILBOX_DEFAULT_ATTRIBUTES['pop3_access']);
1507 $attr['smtp_access'] = (in_array('smtp', $_data['protocol_access'])) ? 1 : intval($MAILBOX_DEFAULT_ATTRIBUTES['smtp_access']);
1508 $attr['sieve_access'] = (in_array('sieve', $_data['protocol_access'])) ? 1 : intval($MAILBOX_DEFAULT_ATTRIBUTES['sieve_access']);
1509 }
1510 else {
1511 $attr['imap_access'] = intval($MAILBOX_DEFAULT_ATTRIBUTES['imap_access']);
1512 $attr['pop3_access'] = intval($MAILBOX_DEFAULT_ATTRIBUTES['pop3_access']);
1513 $attr['smtp_access'] = intval($MAILBOX_DEFAULT_ATTRIBUTES['smtp_access']);
1514 $attr['sieve_access'] = intval($MAILBOX_DEFAULT_ATTRIBUTES['sieve_access']);
1515 }
1516 if (isset($_data['acl'])) {
1517 $_data['acl'] = (array)$_data['acl'];
1518 $attr['acl_spam_alias'] = (in_array('spam_alias', $_data['acl'])) ? 1 : 0;
1519 $attr['acl_tls_policy'] = (in_array('tls_policy', $_data['acl'])) ? 1 : 0;
1520 $attr['acl_spam_score'] = (in_array('spam_score', $_data['acl'])) ? 1 : 0;
1521 $attr['acl_spam_policy'] = (in_array('spam_policy', $_data['acl'])) ? 1 : 0;
1522 $attr['acl_delimiter_action'] = (in_array('delimiter_action', $_data['acl'])) ? 1 : 0;
1523 $attr['acl_syncjobs'] = (in_array('syncjobs', $_data['acl'])) ? 1 : 0;
1524 $attr['acl_eas_reset'] = (in_array('eas_reset', $_data['acl'])) ? 1 : 0;
1525 $attr['acl_sogo_profile_reset'] = (in_array('sogo_profile_reset', $_data['acl'])) ? 1 : 0;
1526 $attr['acl_pushover'] = (in_array('pushover', $_data['acl'])) ? 1 : 0;
1527 $attr['acl_quarantine'] = (in_array('quarantine', $_data['acl'])) ? 1 : 0;
1528 $attr['acl_quarantine_attachments'] = (in_array('quarantine_attachments', $_data['acl'])) ? 1 : 0;
1529 $attr['acl_quarantine_notification'] = (in_array('quarantine_notification', $_data['acl'])) ? 1 : 0;
1530 $attr['acl_quarantine_category'] = (in_array('quarantine_category', $_data['acl'])) ? 1 : 0;
1531 $attr['acl_app_passwds'] = (in_array('app_passwds', $_data['acl'])) ? 1 : 0;
1532 } else {
1533 $_data['acl'] = (array)$_data['acl'];
1534 $attr['acl_spam_alias'] = 1;
1535 $attr['acl_tls_policy'] = 1;
1536 $attr['acl_spam_score'] = 1;
1537 $attr['acl_spam_policy'] = 1;
1538 $attr['acl_delimiter_action'] = 1;
1539 $attr['acl_syncjobs'] = 0;
1540 $attr['acl_eas_reset'] = 1;
1541 $attr['acl_sogo_profile_reset'] = 0;
1542 $attr['acl_pushover'] = 1;
1543 $attr['acl_quarantine'] = 1;
1544 $attr['acl_quarantine_attachments'] = 1;
1545 $attr['acl_quarantine_notification'] = 1;
1546 $attr['acl_quarantine_category'] = 1;
1547 $attr['acl_app_passwds'] = 1;
1548 }
1549
1550
1551
1552 // save template
1553 $stmt = $pdo->prepare("INSERT INTO `templates` (`type`, `template`, `attributes`)
1554 VALUES (:type, :template, :attributes)");
1555 $stmt->execute(array(
1556 ":type" => "mailbox",
1557 ":template" => $_data["template"],
1558 ":attributes" => json_encode($attr)
1559 ));
1560
1561 // success
1562 $_SESSION['return'][] = array(
1563 'type' => 'success',
1564 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1565 'msg' => array('template_added', $_data["template"])
1566 );
1567 return true;
1568 break;
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01001569 }
1570 break;
1571 case 'edit':
1572 switch ($_type) {
1573 case 'alias_domain':
1574 $alias_domains = (array)$_data['alias_domain'];
1575 foreach ($alias_domains as $alias_domain) {
1576 $alias_domain = idn_to_ascii(strtolower(trim($alias_domain)), 0, INTL_IDNA_VARIANT_UTS46);
1577 $is_now = mailbox('get', 'alias_domain_details', $alias_domain);
1578 if (!empty($is_now)) {
1579 $active = (isset($_data['active'])) ? intval($_data['active']) : $is_now['active'];
1580 $target_domain = (!empty($_data['target_domain'])) ? idn_to_ascii(strtolower(trim($_data['target_domain'])), 0, INTL_IDNA_VARIANT_UTS46) : $is_now['target_domain'];
1581 }
1582 else {
1583 $_SESSION['return'][] = array(
1584 'type' => 'danger',
1585 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1586 'msg' => array('alias_domain_invalid', htmlspecialchars($alias_domain))
1587 );
1588 continue;
1589 }
1590 if (!is_valid_domain_name($target_domain)) {
1591 $_SESSION['return'][] = array(
1592 'type' => 'danger',
1593 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1594 'msg' => array('target_domain_invalid', htmlspecialchars($target_domain))
1595 );
1596 continue;
1597 }
1598 if (!hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $target_domain)) {
1599 $_SESSION['return'][] = array(
1600 'type' => 'danger',
1601 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1602 'msg' => 'access_denied'
1603 );
1604 continue;
1605 }
1606 if (empty(mailbox('get', 'domain_details', $target_domain)) || !empty(mailbox('get', 'alias_domain_details', $target_domain))) {
1607 $_SESSION['return'][] = array(
1608 'type' => 'danger',
1609 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1610 'msg' => array('target_domain_invalid', htmlspecialchars($target_domain))
1611 );
1612 continue;
1613 }
1614 $stmt = $pdo->prepare("UPDATE `alias_domain` SET
1615 `target_domain` = :target_domain,
1616 `active` = :active
1617 WHERE `alias_domain` = :alias_domain");
1618 $stmt->execute(array(
1619 ':alias_domain' => $alias_domain,
1620 ':target_domain' => $target_domain,
1621 ':active' => $active
1622 ));
1623 $_SESSION['return'][] = array(
1624 'type' => 'success',
1625 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1626 'msg' => array('aliasd_modified', htmlspecialchars($alias_domain))
1627 );
1628 }
1629 break;
1630 case 'tls_policy':
1631 if (!is_array($_data['username'])) {
1632 $usernames = array();
1633 $usernames[] = $_data['username'];
1634 }
1635 else {
1636 $usernames = $_data['username'];
1637 }
1638 if (!isset($_SESSION['acl']['tls_policy']) || $_SESSION['acl']['tls_policy'] != "1" ) {
1639 $_SESSION['return'][] = array(
1640 'type' => 'danger',
1641 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1642 'msg' => 'access_denied'
1643 );
1644 return false;
1645 }
1646 foreach ($usernames as $username) {
1647 if (!filter_var($username, FILTER_VALIDATE_EMAIL) || !hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $username)) {
1648 $_SESSION['return'][] = array(
1649 'type' => 'danger',
1650 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1651 'msg' => 'access_denied'
1652 );
1653 continue;
1654 }
1655 $is_now = mailbox('get', 'tls_policy', $username);
1656 if (!empty($is_now)) {
1657 $tls_enforce_in = (isset($_data['tls_enforce_in'])) ? intval($_data['tls_enforce_in']) : $is_now['tls_enforce_in'];
1658 $tls_enforce_out = (isset($_data['tls_enforce_out'])) ? intval($_data['tls_enforce_out']) : $is_now['tls_enforce_out'];
1659 }
1660 else {
1661 $_SESSION['return'][] = array(
1662 'type' => 'danger',
1663 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1664 'msg' => 'access_denied'
1665 );
1666 continue;
1667 }
1668 $stmt = $pdo->prepare("UPDATE `mailbox`
1669 SET `attributes` = JSON_SET(`attributes`, '$.tls_enforce_out', :tls_out),
1670 `attributes` = JSON_SET(`attributes`, '$.tls_enforce_in', :tls_in)
1671 WHERE `username` = :username");
1672 $stmt->execute(array(
1673 ':tls_out' => intval($tls_enforce_out),
1674 ':tls_in' => intval($tls_enforce_in),
1675 ':username' => $username
1676 ));
1677 $_SESSION['return'][] = array(
1678 'type' => 'success',
1679 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1680 'msg' => array('mailbox_modified', $username)
1681 );
1682 }
1683 break;
1684 case 'quarantine_notification':
1685 if (!is_array($_data['username'])) {
1686 $usernames = array();
1687 $usernames[] = $_data['username'];
1688 }
1689 else {
1690 $usernames = $_data['username'];
1691 }
1692 if (!isset($_SESSION['acl']['quarantine_notification']) || $_SESSION['acl']['quarantine_notification'] != "1" ) {
1693 $_SESSION['return'][] = array(
1694 'type' => 'danger',
1695 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1696 'msg' => 'access_denied'
1697 );
1698 return false;
1699 }
1700 foreach ($usernames as $username) {
1701 if (!filter_var($username, FILTER_VALIDATE_EMAIL) || !hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $username)) {
1702 $_SESSION['return'][] = array(
1703 'type' => 'danger',
1704 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1705 'msg' => 'access_denied'
1706 );
1707 continue;
1708 }
1709 $is_now = mailbox('get', 'quarantine_notification', $username);
1710 if (!empty($is_now)) {
1711 $quarantine_notification = (isset($_data['quarantine_notification'])) ? $_data['quarantine_notification'] : $is_now['quarantine_notification'];
1712 }
1713 else {
1714 $_SESSION['return'][] = array(
1715 'type' => 'danger',
1716 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1717 'msg' => 'access_denied'
1718 );
1719 continue;
1720 }
1721 if (!in_array($quarantine_notification, array('never', 'hourly', 'daily', 'weekly'))) {
1722 $_SESSION['return'][] = array(
1723 'type' => 'danger',
1724 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1725 'msg' => 'access_denied'
1726 );
1727 continue;
1728 }
1729 $stmt = $pdo->prepare("UPDATE `mailbox`
1730 SET `attributes` = JSON_SET(`attributes`, '$.quarantine_notification', :quarantine_notification)
1731 WHERE `username` = :username");
1732 $stmt->execute(array(
1733 ':quarantine_notification' => $quarantine_notification,
1734 ':username' => $username
1735 ));
1736 $_SESSION['return'][] = array(
1737 'type' => 'success',
1738 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1739 'msg' => array('mailbox_modified', $username)
1740 );
1741 }
1742 break;
1743 case 'quarantine_category':
1744 if (!is_array($_data['username'])) {
1745 $usernames = array();
1746 $usernames[] = $_data['username'];
1747 }
1748 else {
1749 $usernames = $_data['username'];
1750 }
1751 if (!isset($_SESSION['acl']['quarantine_category']) || $_SESSION['acl']['quarantine_category'] != "1" ) {
1752 $_SESSION['return'][] = array(
1753 'type' => 'danger',
1754 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1755 'msg' => 'access_denied'
1756 );
1757 return false;
1758 }
1759 foreach ($usernames as $username) {
1760 if (!filter_var($username, FILTER_VALIDATE_EMAIL) || !hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $username)) {
1761 $_SESSION['return'][] = array(
1762 'type' => 'danger',
1763 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1764 'msg' => 'access_denied'
1765 );
1766 continue;
1767 }
1768 $is_now = mailbox('get', 'quarantine_category', $username);
1769 if (!empty($is_now)) {
1770 $quarantine_category = (isset($_data['quarantine_category'])) ? $_data['quarantine_category'] : $is_now['quarantine_category'];
1771 }
1772 else {
1773 $_SESSION['return'][] = array(
1774 'type' => 'danger',
1775 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1776 'msg' => 'access_denied'
1777 );
1778 continue;
1779 }
1780 if (!in_array($quarantine_category, array('add_header', 'reject', 'all'))) {
1781 $_SESSION['return'][] = array(
1782 'type' => 'danger',
1783 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1784 'msg' => 'access_denied'
1785 );
1786 continue;
1787 }
1788 $stmt = $pdo->prepare("UPDATE `mailbox`
1789 SET `attributes` = JSON_SET(`attributes`, '$.quarantine_category', :quarantine_category)
1790 WHERE `username` = :username");
1791 $stmt->execute(array(
1792 ':quarantine_category' => $quarantine_category,
1793 ':username' => $username
1794 ));
1795 $_SESSION['return'][] = array(
1796 'type' => 'success',
1797 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1798 'msg' => array('mailbox_modified', $username)
1799 );
1800 }
1801 break;
1802 case 'spam_score':
1803 if (!is_array($_data['username'])) {
1804 $usernames = array();
1805 $usernames[] = $_data['username'];
1806 }
1807 else {
1808 $usernames = $_data['username'];
1809 }
1810 if (!isset($_SESSION['acl']['spam_score']) || $_SESSION['acl']['spam_score'] != "1" ) {
1811 $_SESSION['return'][] = array(
1812 'type' => 'danger',
1813 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1814 'msg' => 'access_denied'
1815 );
1816 return false;
1817 }
1818 foreach ($usernames as $username) {
1819 if ($_data['spam_score'] == "default") {
1820 $stmt = $pdo->prepare("DELETE FROM `filterconf` WHERE `object` = :username
1821 AND (`option` = 'lowspamlevel' OR `option` = 'highspamlevel')");
1822 $stmt->execute(array(
1823 ':username' => $username
1824 ));
1825 $_SESSION['return'][] = array(
1826 'type' => 'success',
1827 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1828 'msg' => array('mailbox_modified', $username)
1829 );
1830 continue;
1831 }
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02001832 $lowspamlevel = explode(',', $_data['spam_score'])[0];
1833 $highspamlevel = explode(',', $_data['spam_score'])[1];
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01001834 if (!is_numeric($lowspamlevel) || !is_numeric($highspamlevel)) {
1835 $_SESSION['return'][] = array(
1836 'type' => 'danger',
1837 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1838 'msg' => 'Invalid spam score, format must be "1,2" where first is low and second is high spam value.'
1839 );
1840 continue;
1841 }
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02001842 if ($lowspamlevel == $highspamlevel) {
1843 $highspamlevel = $highspamlevel + 0.1;
1844 }
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01001845 $stmt = $pdo->prepare("DELETE FROM `filterconf` WHERE `object` = :username
1846 AND (`option` = 'lowspamlevel' OR `option` = 'highspamlevel')");
1847 $stmt->execute(array(
1848 ':username' => $username
1849 ));
1850 $stmt = $pdo->prepare("INSERT INTO `filterconf` (`object`, `option`, `value`)
1851 VALUES (:username, 'highspamlevel', :highspamlevel)");
1852 $stmt->execute(array(
1853 ':username' => $username,
1854 ':highspamlevel' => $highspamlevel
1855 ));
1856 $stmt = $pdo->prepare("INSERT INTO `filterconf` (`object`, `option`, `value`)
1857 VALUES (:username, 'lowspamlevel', :lowspamlevel)");
1858 $stmt->execute(array(
1859 ':username' => $username,
1860 ':lowspamlevel' => $lowspamlevel
1861 ));
1862 $_SESSION['return'][] = array(
1863 'type' => 'success',
1864 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1865 'msg' => array('mailbox_modified', $username)
1866 );
1867 }
1868 break;
1869 case 'time_limited_alias':
1870 if (!isset($_SESSION['acl']['spam_alias']) || $_SESSION['acl']['spam_alias'] != "1" ) {
1871 $_SESSION['return'][] = array(
1872 'type' => 'danger',
1873 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1874 'msg' => 'access_denied'
1875 );
1876 return false;
1877 }
1878 if (!is_array($_data['address'])) {
1879 $addresses = array();
1880 $addresses[] = $_data['address'];
1881 }
1882 else {
1883 $addresses = $_data['address'];
1884 }
1885 foreach ($addresses as $address) {
1886 $stmt = $pdo->prepare("SELECT `goto` FROM `spamalias` WHERE `address` = :address");
1887 $stmt->execute(array(':address' => $address));
1888 $goto = $stmt->fetch(PDO::FETCH_ASSOC)['goto'];
1889 if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $goto)) {
1890 $_SESSION['return'][] = array(
1891 'type' => 'danger',
1892 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1893 'msg' => 'access_denied'
1894 );
1895 continue;
1896 }
1897 if (empty($_data['validity'])) {
1898 continue;
1899 }
1900 $validity = round((int)time() + ($_data['validity'] * 3600));
1901 $stmt = $pdo->prepare("UPDATE `spamalias` SET `validity` = :validity WHERE
1902 `address` = :address");
1903 $stmt->execute(array(
1904 ':address' => $address,
1905 ':validity' => $validity
1906 ));
1907 $_SESSION['return'][] = array(
1908 'type' => 'success',
1909 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02001910 'msg' => array('mailbox_modified', htmlspecialchars(implode(', ', (array)$usernames)))
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01001911 );
1912 }
1913 break;
1914 case 'delimiter_action':
1915 if (!is_array($_data['username'])) {
1916 $usernames = array();
1917 $usernames[] = $_data['username'];
1918 }
1919 else {
1920 $usernames = $_data['username'];
1921 }
1922 if (!isset($_SESSION['acl']['delimiter_action']) || $_SESSION['acl']['delimiter_action'] != "1" ) {
1923 $_SESSION['return'][] = array(
1924 'type' => 'danger',
1925 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1926 'msg' => 'access_denied'
1927 );
1928 return false;
1929 }
1930 foreach ($usernames as $username) {
1931 if (!filter_var($username, FILTER_VALIDATE_EMAIL) || !hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $username)) {
1932 $_SESSION['return'][] = array(
1933 'type' => 'danger',
1934 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1935 'msg' => 'access_denied'
1936 );
1937 continue;
1938 }
1939 if (isset($_data['tagged_mail_handler']) && $_data['tagged_mail_handler'] == "subject") {
1940 try {
1941 $redis->hSet('RCPT_WANTS_SUBJECT_TAG', $username, 1);
1942 $redis->hDel('RCPT_WANTS_SUBFOLDER_TAG', $username);
1943 }
1944 catch (RedisException $e) {
1945 $_SESSION['return'][] = array(
1946 'type' => 'danger',
1947 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1948 'msg' => array('redis_error', $e)
1949 );
1950 continue;
1951 }
1952 }
1953 else if (isset($_data['tagged_mail_handler']) && $_data['tagged_mail_handler'] == "subfolder") {
1954 try {
1955 $redis->hSet('RCPT_WANTS_SUBFOLDER_TAG', $username, 1);
1956 $redis->hDel('RCPT_WANTS_SUBJECT_TAG', $username);
1957 }
1958 catch (RedisException $e) {
1959 $_SESSION['return'][] = array(
1960 'type' => 'danger',
1961 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1962 'msg' => array('redis_error', $e)
1963 );
1964 continue;
1965 }
1966 }
1967 else {
1968 try {
1969 $redis->hDel('RCPT_WANTS_SUBJECT_TAG', $username);
1970 $redis->hDel('RCPT_WANTS_SUBFOLDER_TAG', $username);
1971 }
1972 catch (RedisException $e) {
1973 $_SESSION['return'][] = array(
1974 'type' => 'danger',
1975 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1976 'msg' => array('redis_error', $e)
1977 );
1978 continue;
1979 }
1980 }
1981 $_SESSION['return'][] = array(
1982 'type' => 'success',
1983 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1984 'msg' => array('mailbox_modified', $username)
1985 );
1986 }
1987 break;
1988 case 'syncjob':
1989 if (!is_array($_data['id'])) {
1990 $ids = array();
1991 $ids[] = $_data['id'];
1992 }
1993 else {
1994 $ids = $_data['id'];
1995 }
1996 if (!isset($_SESSION['acl']['syncjobs']) || $_SESSION['acl']['syncjobs'] != "1" ) {
1997 $_SESSION['return'][] = array(
1998 'type' => 'danger',
1999 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2000 'msg' => 'access_denied'
2001 );
2002 return false;
2003 }
2004 foreach ($ids as $id) {
2005 $is_now = mailbox('get', 'syncjob_details', $id, array('with_password'));
2006 if (!empty($is_now)) {
2007 $username = $is_now['user2'];
2008 $user1 = (!empty($_data['user1'])) ? $_data['user1'] : $is_now['user1'];
2009 $active = (isset($_data['active'])) ? intval($_data['active']) : $is_now['active'];
2010 $last_run = (isset($_data['last_run'])) ? NULL : $is_now['last_run'];
Matthias Andreas Benkard12a57352021-12-28 18:02:04 +01002011 $success = (isset($_data['success'])) ? NULL : $is_now['success'];
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01002012 $delete2duplicates = (isset($_data['delete2duplicates'])) ? intval($_data['delete2duplicates']) : $is_now['delete2duplicates'];
2013 $subscribeall = (isset($_data['subscribeall'])) ? intval($_data['subscribeall']) : $is_now['subscribeall'];
2014 $delete1 = (isset($_data['delete1'])) ? intval($_data['delete1']) : $is_now['delete1'];
2015 $delete2 = (isset($_data['delete2'])) ? intval($_data['delete2']) : $is_now['delete2'];
2016 $automap = (isset($_data['automap'])) ? intval($_data['automap']) : $is_now['automap'];
2017 $skipcrossduplicates = (isset($_data['skipcrossduplicates'])) ? intval($_data['skipcrossduplicates']) : $is_now['skipcrossduplicates'];
2018 $port1 = (!empty($_data['port1'])) ? $_data['port1'] : $is_now['port1'];
2019 $password1 = (!empty($_data['password1'])) ? $_data['password1'] : $is_now['password1'];
2020 $host1 = (!empty($_data['host1'])) ? $_data['host1'] : $is_now['host1'];
2021 $subfolder2 = (isset($_data['subfolder2'])) ? $_data['subfolder2'] : $is_now['subfolder2'];
2022 $enc1 = (!empty($_data['enc1'])) ? $_data['enc1'] : $is_now['enc1'];
2023 $mins_interval = (!empty($_data['mins_interval'])) ? $_data['mins_interval'] : $is_now['mins_interval'];
2024 $exclude = (isset($_data['exclude'])) ? $_data['exclude'] : $is_now['exclude'];
2025 $custom_params = (isset($_data['custom_params'])) ? $_data['custom_params'] : $is_now['custom_params'];
2026 $maxage = (isset($_data['maxage']) && $_data['maxage'] != "") ? intval($_data['maxage']) : $is_now['maxage'];
2027 $maxbytespersecond = (isset($_data['maxbytespersecond']) && $_data['maxbytespersecond'] != "") ? intval($_data['maxbytespersecond']) : $is_now['maxbytespersecond'];
2028 $timeout1 = (isset($_data['timeout1']) && $_data['timeout1'] != "") ? intval($_data['timeout1']) : $is_now['timeout1'];
2029 $timeout2 = (isset($_data['timeout2']) && $_data['timeout2'] != "") ? intval($_data['timeout2']) : $is_now['timeout2'];
2030 }
2031 else {
2032 $_SESSION['return'][] = array(
2033 'type' => 'danger',
2034 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2035 'msg' => 'access_denied'
2036 );
2037 continue;
2038 }
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +01002039
2040 // validate custom params
2041 foreach (explode('-', $custom_params) as $param){
2042 if(empty($param)) continue;
2043
2044 // extract option
2045 if (str_contains($param, '=')) $param = explode('=', $param)[0];
2046 else $param = rtrim($param, ' ');
2047 // remove first char if first char is -
2048 if ($param[0] == '-') $param = ltrim($param, $param[0]);
2049
2050 if (str_contains($param, ' ')) {
2051 // bad char
2052 $_SESSION['return'][] = array(
2053 'type' => 'danger',
2054 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2055 'msg' => 'bad character SPACE'
2056 );
2057 return false;
2058 }
2059
2060 // check if param is whitelisted
2061 if (!in_array(strtolower($param), $GLOBALS["IMAPSYNC_OPTIONS"]["whitelist"])){
2062 // bad option
2063 $_SESSION['return'][] = array(
2064 'type' => 'danger',
2065 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2066 'msg' => 'bad option '. $param
2067 );
2068 return false;
2069 }
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01002070 }
2071 if (empty($subfolder2)) {
2072 $subfolder2 = "";
2073 }
2074 if (!isset($maxage) || !filter_var($maxage, FILTER_VALIDATE_INT, array('options' => array('min_range' => 1, 'max_range' => 32000)))) {
2075 $maxage = "0";
2076 }
2077 if (!isset($timeout1) || !filter_var($timeout1, FILTER_VALIDATE_INT, array('options' => array('min_range' => 1, 'max_range' => 32000)))) {
2078 $timeout1 = "600";
2079 }
2080 if (!isset($timeout2) || !filter_var($timeout2, FILTER_VALIDATE_INT, array('options' => array('min_range' => 1, 'max_range' => 32000)))) {
2081 $timeout2 = "600";
2082 }
2083 if (!isset($maxbytespersecond) || !filter_var($maxbytespersecond, FILTER_VALIDATE_INT, array('options' => array('min_range' => 1, 'max_range' => 125000000)))) {
2084 $maxbytespersecond = "0";
2085 }
2086 if (!filter_var($port1, FILTER_VALIDATE_INT, array('options' => array('min_range' => 1, 'max_range' => 65535)))) {
2087 $_SESSION['return'][] = array(
2088 'type' => 'danger',
2089 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2090 'msg' => 'access_denied'
2091 );
2092 continue;
2093 }
2094 if (!filter_var($mins_interval, FILTER_VALIDATE_INT, array('options' => array('min_range' => 1, 'max_range' => 43800)))) {
2095 $_SESSION['return'][] = array(
2096 'type' => 'danger',
2097 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2098 'msg' => 'access_denied'
2099 );
2100 continue;
2101 }
2102 if (!is_valid_domain_name($host1)) {
2103 $_SESSION['return'][] = array(
2104 'type' => 'danger',
2105 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2106 'msg' => 'access_denied'
2107 );
2108 continue;
2109 }
2110 if ($enc1 != "TLS" && $enc1 != "SSL" && $enc1 != "PLAIN") {
2111 $_SESSION['return'][] = array(
2112 'type' => 'danger',
2113 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2114 'msg' => 'access_denied'
2115 );
2116 continue;
2117 }
2118 if (@preg_match("/" . $exclude . "/", null) === false) {
2119 $_SESSION['return'][] = array(
2120 'type' => 'danger',
2121 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2122 'msg' => 'access_denied'
2123 );
2124 continue;
2125 }
2126 $stmt = $pdo->prepare("UPDATE `imapsync` SET `delete1` = :delete1,
2127 `delete2` = :delete2,
2128 `automap` = :automap,
2129 `skipcrossduplicates` = :skipcrossduplicates,
2130 `maxage` = :maxage,
2131 `maxbytespersecond` = :maxbytespersecond,
2132 `subfolder2` = :subfolder2,
2133 `exclude` = :exclude,
2134 `host1` = :host1,
2135 `last_run` = :last_run,
Matthias Andreas Benkard12a57352021-12-28 18:02:04 +01002136 `success` = :success,
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01002137 `user1` = :user1,
2138 `password1` = :password1,
2139 `mins_interval` = :mins_interval,
2140 `port1` = :port1,
2141 `enc1` = :enc1,
2142 `delete2duplicates` = :delete2duplicates,
2143 `custom_params` = :custom_params,
2144 `timeout1` = :timeout1,
2145 `timeout2` = :timeout2,
2146 `subscribeall` = :subscribeall,
2147 `active` = :active
2148 WHERE `id` = :id");
2149 $stmt->execute(array(
2150 ':delete1' => $delete1,
2151 ':delete2' => $delete2,
2152 ':automap' => $automap,
2153 ':skipcrossduplicates' => $skipcrossduplicates,
2154 ':id' => $id,
2155 ':exclude' => $exclude,
2156 ':maxage' => $maxage,
2157 ':maxbytespersecond' => $maxbytespersecond,
2158 ':subfolder2' => $subfolder2,
2159 ':host1' => $host1,
2160 ':user1' => $user1,
2161 ':password1' => $password1,
2162 ':last_run' => $last_run,
Matthias Andreas Benkard12a57352021-12-28 18:02:04 +01002163 ':success' => $success,
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01002164 ':mins_interval' => $mins_interval,
2165 ':port1' => $port1,
2166 ':enc1' => $enc1,
2167 ':delete2duplicates' => $delete2duplicates,
2168 ':custom_params' => $custom_params,
2169 ':timeout1' => $timeout1,
2170 ':timeout2' => $timeout2,
2171 ':subscribeall' => $subscribeall,
2172 ':active' => $active,
2173 ));
2174 $_SESSION['return'][] = array(
2175 'type' => 'success',
2176 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2177 'msg' => array('mailbox_modified', $username)
2178 );
2179 }
2180 break;
2181 case 'filter':
2182 if (!isset($_SESSION['acl']['filters']) || $_SESSION['acl']['filters'] != "1" ) {
2183 $_SESSION['return'][] = array(
2184 'type' => 'danger',
2185 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2186 'msg' => 'access_denied'
2187 );
2188 return false;
2189 }
2190 $sieve = new Sieve\SieveParser();
2191 if (!is_array($_data['id'])) {
2192 $ids = array();
2193 $ids[] = $_data['id'];
2194 }
2195 else {
2196 $ids = $_data['id'];
2197 }
2198 foreach ($ids as $id) {
2199 $is_now = mailbox('get', 'filter_details', $id);
2200 if (!empty($is_now)) {
2201 $username = $is_now['username'];
2202 $active = (isset($_data['active'])) ? intval($_data['active']) : $is_now['active'];
2203 $script_desc = (!empty($_data['script_desc'])) ? $_data['script_desc'] : $is_now['script_desc'];
2204 $script_data = (!empty($_data['script_data'])) ? $_data['script_data'] : $is_now['script_data'];
2205 $filter_type = (!empty($_data['filter_type'])) ? $_data['filter_type'] : $is_now['filter_type'];
2206 }
2207 else {
2208 $_SESSION['return'][] = array(
2209 'type' => 'danger',
2210 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2211 'msg' => 'access_denied'
2212 );
2213 continue;
2214 }
2215 try {
2216 $sieve->parse($script_data);
2217 }
2218 catch (Exception $e) {
2219 $_SESSION['return'][] = array(
2220 'type' => 'danger',
2221 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2222 'msg' => array('sieve_error', $e->getMessage())
2223 );
2224 continue;
2225 }
2226 if ($filter_type != 'postfilter' && $filter_type != 'prefilter') {
2227 $_SESSION['return'][] = array(
2228 'type' => 'danger',
2229 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2230 'msg' => 'filter_type'
2231 );
2232 continue;
2233 }
2234 if ($active == '1') {
2235 $script_name = 'active';
2236 $stmt = $pdo->prepare("UPDATE `sieve_filters`
2237 SET `script_name` = 'inactive'
2238 WHERE `username` = :username
2239 AND `filter_type` = :filter_type");
2240 $stmt->execute(array(
2241 ':username' => $username,
2242 ':filter_type' => $filter_type
2243 ));
2244 }
2245 else {
2246 $script_name = 'inactive';
2247 }
2248 $stmt = $pdo->prepare("UPDATE `sieve_filters` SET `script_desc` = :script_desc, `script_data` = :script_data, `script_name` = :script_name, `filter_type` = :filter_type
2249 WHERE `id` = :id");
2250 $stmt->execute(array(
2251 ':script_desc' => $script_desc,
2252 ':script_data' => $script_data,
2253 ':script_name' => $script_name,
2254 ':filter_type' => $filter_type,
2255 ':id' => $id
2256 ));
2257 $_SESSION['return'][] = array(
2258 'type' => 'success',
2259 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2260 'msg' => array('mailbox_modified', $username)
2261 );
2262 }
2263 break;
2264 case 'alias':
2265 if (!is_array($_data['id'])) {
2266 $ids = array();
2267 $ids[] = $_data['id'];
2268 }
2269 else {
2270 $ids = $_data['id'];
2271 }
2272 foreach ($ids as $id) {
2273 $is_now = mailbox('get', 'alias_details', $id);
2274 if (!empty($is_now)) {
2275 $active = (isset($_data['active'])) ? intval($_data['active']) : $is_now['active'];
2276 $sogo_visible = (isset($_data['sogo_visible'])) ? intval($_data['sogo_visible']) : $is_now['sogo_visible'];
2277 $goto_null = (isset($_data['goto_null'])) ? intval($_data['goto_null']) : 0;
2278 $goto_spam = (isset($_data['goto_spam'])) ? intval($_data['goto_spam']) : 0;
2279 $goto_ham = (isset($_data['goto_ham'])) ? intval($_data['goto_ham']) : 0;
2280 $public_comment = (isset($_data['public_comment'])) ? $_data['public_comment'] : $is_now['public_comment'];
2281 $private_comment = (isset($_data['private_comment'])) ? $_data['private_comment'] : $is_now['private_comment'];
2282 $goto = (!empty($_data['goto'])) ? $_data['goto'] : $is_now['goto'];
2283 $address = (!empty($_data['address'])) ? $_data['address'] : $is_now['address'];
2284 }
2285 else {
2286 $_SESSION['return'][] = array(
2287 'type' => 'danger',
2288 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2289 'msg' => array('alias_invalid', $address)
2290 );
2291 continue;
2292 }
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02002293 if ($_data['expand_alias'] === true || $_data['expand_alias'] == 1) {
2294 $stmt = $pdo->prepare("SELECT `address` FROM `alias`
2295 WHERE `address` = :address
2296 AND `domain` NOT IN (
2297 SELECT `alias_domain` FROM `alias_domain`
2298 )");
2299 $stmt->execute(array(
2300 ':address' => $address,
2301 ));
2302 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
2303 if ($num_results == 0) {
2304 $_SESSION['return'][] = array(
2305 'type' => 'warning',
2306 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2307 'msg' => array('is_not_primary_alias', htmlspecialchars($address))
2308 );
2309 continue;
2310 }
2311 $stmt = $pdo->prepare("SELECT `goto`, GROUP_CONCAT(CONCAT(SUBSTRING(`alias`.`address`, 1, LOCATE('@', `alias`.`address`) - 1), '@', `alias_domain`.`alias_domain`)) AS `missing_alias`
2312 FROM `alias` JOIN `alias_domain` ON `alias_domain`.`target_domain` = `alias`.`domain`
2313 WHERE CONCAT(SUBSTRING(`alias`.`address`, 1, LOCATE('@', `alias`.`address`) - 1), '@', `alias_domain`.`alias_domain`) NOT IN (
2314 SELECT `address` FROM `alias` WHERE `address` != `goto`
2315 )
2316 AND `alias`.`address` NOT IN (
2317 SELECT `address` FROM `alias` WHERE `address` = `goto`
2318 )
2319 AND `address` = :address ;");
2320 $stmt->execute(array(
2321 ':address' => $address
2322 ));
2323 $missing_aliases = $stmt->fetch(PDO::FETCH_ASSOC);
2324 if (!empty($missing_aliases['missing_alias'])) {
2325 mailbox('add', 'alias', array(
2326 'address' => $missing_aliases['missing_alias'],
2327 'goto' => $missing_aliases['goto'],
2328 'sogo_visible' => 1,
2329 'active' => 1
2330 ));
2331 }
2332 $_SESSION['return'][] = array(
2333 'type' => 'success',
2334 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2335 'msg' => array('alias_modified', htmlspecialchars($address))
2336 );
2337 continue;
2338 }
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01002339 $domain = idn_to_ascii(substr(strstr($address, '@'), 1), 0, INTL_IDNA_VARIANT_UTS46);
2340 if ($is_now['address'] != $address) {
2341 $local_part = strstr($address, '@', true);
2342 $address = $local_part.'@'.$domain;
2343 if (!hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $domain)) {
2344 $_SESSION['return'][] = array(
2345 'type' => 'danger',
2346 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2347 'msg' => 'access_denied'
2348 );
2349 continue;
2350 }
2351 if ((!filter_var($address, FILTER_VALIDATE_EMAIL) === true) && !empty($local_part)) {
2352 $_SESSION['return'][] = array(
2353 'type' => 'danger',
2354 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2355 'msg' => array('alias_invalid', $address)
2356 );
2357 continue;
2358 }
2359 if (strtolower($is_now['address']) != strtolower($address)) {
2360 $stmt = $pdo->prepare("SELECT `address` FROM `alias`
2361 WHERE `address`= :address OR `address` IN (
2362 SELECT `username` FROM `mailbox`, `alias_domain`
2363 WHERE (
2364 `alias_domain`.`alias_domain` = :address_d
2365 AND `mailbox`.`username` = CONCAT(:address_l, '@', alias_domain.target_domain)))");
2366 $stmt->execute(array(
2367 ':address' => $address,
2368 ':address_l' => $local_part,
2369 ':address_d' => $domain
2370 ));
2371 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
2372 if ($num_results != 0) {
2373 $_SESSION['return'][] = array(
2374 'type' => 'danger',
2375 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2376 'msg' => array('is_alias_or_mailbox', htmlspecialchars($address))
2377 );
2378 continue;
2379 }
2380 }
2381 $stmt = $pdo->prepare("SELECT `domain` FROM `domain`
2382 WHERE `domain`= :domain1 OR `domain` = (SELECT `target_domain` FROM `alias_domain` WHERE `alias_domain` = :domain2)");
2383 $stmt->execute(array(':domain1' => $domain, ':domain2' => $domain));
2384 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
2385 if ($num_results == 0) {
2386 $_SESSION['return'][] = array(
2387 'type' => 'danger',
2388 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2389 'msg' => array('domain_not_found', htmlspecialchars($domain))
2390 );
2391 continue;
2392 }
2393 $stmt = $pdo->prepare("SELECT `address` FROM `spamalias`
2394 WHERE `address`= :address");
2395 $stmt->execute(array(':address' => $address));
2396 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
2397 if ($num_results != 0) {
2398 $_SESSION['return'][] = array(
2399 'type' => 'danger',
2400 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2401 'msg' => array('is_spam_alias', htmlspecialchars($address))
2402 );
2403 continue;
2404 }
2405 }
2406 if ($goto_null == "1") {
2407 $goto = "null@localhost";
2408 }
2409 elseif ($goto_spam == "1") {
2410 $goto = "spam@localhost";
2411 }
2412 elseif ($goto_ham == "1") {
2413 $goto = "ham@localhost";
2414 }
2415 else {
2416 $gotos = array_map('trim', preg_split( "/( |,|;|\n)/", $goto));
2417 foreach ($gotos as $i => &$goto) {
2418 if (empty($goto)) {
2419 continue;
2420 }
2421 if (!filter_var($goto, FILTER_VALIDATE_EMAIL)) {
2422 $_SESSION['return'][] = array(
2423 'type' => 'danger',
2424 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2425 'msg' => array('goto_invalid', $goto)
2426 );
2427 unset($gotos[$i]);
2428 continue;
2429 }
2430 if ($goto == $address) {
2431 $_SESSION['return'][] = array(
2432 'type' => 'danger',
2433 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2434 'msg' => 'alias_goto_identical'
2435 );
2436 unset($gotos[$i]);
2437 continue;
2438 }
2439 // Delete from sender_acl to prevent duplicates
2440 $stmt = $pdo->prepare("DELETE FROM `sender_acl` WHERE
2441 `logged_in_as` = :goto AND
2442 `send_as` = :address");
2443 $stmt->execute(array(
2444 ':goto' => $goto,
2445 ':address' => $address
2446 ));
2447 }
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02002448 $gotos = array_unique($gotos);
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01002449 $gotos = array_filter($gotos);
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02002450 $goto = implode(",", (array)$gotos);
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01002451 }
2452 if (!empty($goto)) {
2453 $stmt = $pdo->prepare("UPDATE `alias` SET
2454 `address` = :address,
2455 `public_comment` = :public_comment,
2456 `private_comment` = :private_comment,
2457 `domain` = :domain,
2458 `goto` = :goto,
2459 `sogo_visible`= :sogo_visible,
2460 `active`= :active
2461 WHERE `id` = :id");
2462 $stmt->execute(array(
2463 ':address' => $address,
2464 ':public_comment' => $public_comment,
2465 ':private_comment' => $private_comment,
2466 ':domain' => $domain,
2467 ':goto' => $goto,
2468 ':sogo_visible' => $sogo_visible,
2469 ':active' => $active,
2470 ':id' => $is_now['id']
2471 ));
2472 }
2473 $_SESSION['return'][] = array(
2474 'type' => 'success',
2475 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2476 'msg' => array('alias_modified', htmlspecialchars($address))
2477 );
2478 }
2479 break;
2480 case 'domain':
2481 if (!is_array($_data['domain'])) {
2482 $domains = array();
2483 $domains[] = $_data['domain'];
2484 }
2485 else {
2486 $domains = $_data['domain'];
2487 }
2488 foreach ($domains as $domain) {
2489 $domain = idn_to_ascii($domain, 0, INTL_IDNA_VARIANT_UTS46);
2490 if (!is_valid_domain_name($domain)) {
2491 $_SESSION['return'][] = array(
2492 'type' => 'danger',
2493 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2494 'msg' => 'domain_invalid'
2495 );
2496 continue;
2497 }
2498 if ($_SESSION['mailcow_cc_role'] == "domainadmin" &&
2499 hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $domain)) {
2500 $is_now = mailbox('get', 'domain_details', $domain);
2501 if (!empty($is_now)) {
2502 $gal = (isset($_data['gal'])) ? intval($_data['gal']) : $is_now['gal'];
2503 $description = (!empty($_data['description']) && isset($_SESSION['acl']['domain_desc']) && $_SESSION['acl']['domain_desc'] == "1") ? $_data['description'] : $is_now['description'];
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02002504 (int)$relayhost = (isset($_data['relayhost']) && isset($_SESSION['acl']['domain_relayhost']) && $_SESSION['acl']['domain_relayhost'] == "1") ? intval($_data['relayhost']) : intval($is_now['relayhost']);
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +01002505 $tags = (is_array($_data['tags']) ? $_data['tags'] : array());
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01002506 }
2507 else {
2508 $_SESSION['return'][] = array(
2509 'type' => 'danger',
2510 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2511 'msg' => 'domain_invalid'
2512 );
2513 continue;
2514 }
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +01002515
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01002516 $stmt = $pdo->prepare("UPDATE `domain` SET
2517 `description` = :description,
2518 `gal` = :gal
2519 WHERE `domain` = :domain");
2520 $stmt->execute(array(
2521 ':description' => $description,
2522 ':gal' => $gal,
2523 ':domain' => $domain
2524 ));
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +01002525 // save tags
2526 foreach($tags as $index => $tag){
2527 if (empty($tag)) continue;
2528 if ($index > $GLOBALS['TAGGING_LIMIT']) {
2529 $_SESSION['return'][] = array(
2530 'type' => 'warning',
2531 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2532 'msg' => array('tag_limit_exceeded', 'limit '.$GLOBALS['TAGGING_LIMIT'])
2533 );
2534 break;
2535 }
2536 $stmt = $pdo->prepare("INSERT INTO `tags_domain` (`domain`, `tag_name`) VALUES (:domain, :tag_name)");
2537 $stmt->execute(array(
2538 ':domain' => $domain,
2539 ':tag_name' => $tag,
2540 ));
2541 }
2542
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01002543 $_SESSION['return'][] = array(
2544 'type' => 'success',
2545 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2546 'msg' => array('domain_modified', htmlspecialchars($domain))
2547 );
2548 }
2549 elseif ($_SESSION['mailcow_cc_role'] == "admin") {
2550 $is_now = mailbox('get', 'domain_details', $domain);
2551 if (!empty($is_now)) {
2552 $active = (isset($_data['active'])) ? intval($_data['active']) : $is_now['active'];
2553 $backupmx = (isset($_data['backupmx'])) ? intval($_data['backupmx']) : $is_now['backupmx'];
2554 $gal = (isset($_data['gal'])) ? intval($_data['gal']) : $is_now['gal'];
2555 $relay_all_recipients = (isset($_data['relay_all_recipients'])) ? intval($_data['relay_all_recipients']) : $is_now['relay_all_recipients'];
2556 $relay_unknown_only = (isset($_data['relay_unknown_only'])) ? intval($_data['relay_unknown_only']) : $is_now['relay_unknown_only'];
2557 $relayhost = (isset($_data['relayhost'])) ? intval($_data['relayhost']) : $is_now['relayhost'];
2558 $aliases = (!empty($_data['aliases'])) ? $_data['aliases'] : $is_now['max_num_aliases_for_domain'];
2559 $mailboxes = (isset($_data['mailboxes']) && $_data['mailboxes'] != '') ? intval($_data['mailboxes']) : $is_now['max_num_mboxes_for_domain'];
2560 $defquota = (isset($_data['defquota']) && $_data['defquota'] != '') ? intval($_data['defquota']) : ($is_now['def_quota_for_mbox'] / 1048576);
2561 $maxquota = (!empty($_data['maxquota'])) ? $_data['maxquota'] : ($is_now['max_quota_for_mbox'] / 1048576);
2562 $quota = (!empty($_data['quota'])) ? $_data['quota'] : ($is_now['max_quota_for_domain'] / 1048576);
2563 $description = (!empty($_data['description'])) ? $_data['description'] : $is_now['description'];
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +01002564 $tags = (is_array($_data['tags']) ? $_data['tags'] : array());
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01002565 if ($relay_all_recipients == '1') {
2566 $backupmx = '1';
2567 }
2568 if ($relay_unknown_only == '1') {
2569 $backupmx = '1';
2570 $relay_all_recipients = '1';
2571 }
2572 }
2573 else {
2574 $_SESSION['return'][] = array(
2575 'type' => 'danger',
2576 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2577 'msg' => 'domain_invalid'
2578 );
2579 continue;
2580 }
2581 // todo: should be using api here
2582 $stmt = $pdo->prepare("SELECT
2583 COUNT(*) AS count,
2584 MAX(COALESCE(ROUND(`quota`/1048576), 0)) AS `biggest_mailbox`,
2585 COALESCE(ROUND(SUM(`quota`)/1048576), 0) AS `quota_all`
2586 FROM `mailbox`
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02002587 WHERE (`kind` = '' OR `kind` = NULL)
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01002588 AND domain = :domain");
2589 $stmt->execute(array(':domain' => $domain));
2590 $MailboxData = $stmt->fetch(PDO::FETCH_ASSOC);
2591 // todo: should be using api here
2592 $stmt = $pdo->prepare("SELECT COUNT(*) AS `count` FROM `alias`
2593 WHERE domain = :domain
2594 AND address NOT IN (
2595 SELECT `username` FROM `mailbox`
2596 )");
2597 $stmt->execute(array(':domain' => $domain));
2598 $AliasData = $stmt->fetch(PDO::FETCH_ASSOC);
2599 if ($defquota > $maxquota) {
2600 $_SESSION['return'][] = array(
2601 'type' => 'danger',
2602 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2603 'msg' => 'mailbox_defquota_exceeds_mailbox_maxquota'
2604 );
2605 continue;
2606 }
2607 if ($defquota == "0" || empty($defquota)) {
2608 $_SESSION['return'][] = array(
2609 'type' => 'danger',
2610 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2611 'msg' => 'defquota_empty'
2612 );
2613 continue;
2614 }
2615 if ($maxquota > $quota) {
2616 $_SESSION['return'][] = array(
2617 'type' => 'danger',
2618 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2619 'msg' => 'mailbox_quota_exceeds_domain_quota'
2620 );
2621 continue;
2622 }
2623 if ($maxquota == "0" || empty($maxquota)) {
2624 $_SESSION['return'][] = array(
2625 'type' => 'danger',
2626 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2627 'msg' => 'maxquota_empty'
2628 );
2629 continue;
2630 }
2631 if ($MailboxData['biggest_mailbox'] > $maxquota) {
2632 $_SESSION['return'][] = array(
2633 'type' => 'danger',
2634 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2635 'msg' => array('max_quota_in_use', $MailboxData['biggest_mailbox'])
2636 );
2637 continue;
2638 }
2639 if ($MailboxData['quota_all'] > $quota) {
2640 $_SESSION['return'][] = array(
2641 'type' => 'danger',
2642 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2643 'msg' => array('domain_quota_m_in_use', $MailboxData['quota_all'])
2644 );
2645 continue;
2646 }
2647 if ($MailboxData['count'] > $mailboxes) {
2648 $_SESSION['return'][] = array(
2649 'type' => 'danger',
2650 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2651 'msg' => array('mailboxes_in_use', $MailboxData['count'])
2652 );
2653 continue;
2654 }
2655 if ($AliasData['count'] > $aliases) {
2656 $_SESSION['return'][] = array(
2657 'type' => 'danger',
2658 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2659 'msg' => array('aliases_in_use', $AliasData['count'])
2660 );
2661 continue;
2662 }
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +01002663
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01002664 $stmt = $pdo->prepare("UPDATE `domain` SET
2665 `relay_all_recipients` = :relay_all_recipients,
2666 `relay_unknown_only` = :relay_unknown_only,
2667 `backupmx` = :backupmx,
2668 `gal` = :gal,
2669 `active` = :active,
2670 `quota` = :quota,
2671 `defquota` = :defquota,
2672 `maxquota` = :maxquota,
2673 `relayhost` = :relayhost,
2674 `mailboxes` = :mailboxes,
2675 `aliases` = :aliases,
2676 `description` = :description
2677 WHERE `domain` = :domain");
2678 $stmt->execute(array(
2679 ':relay_all_recipients' => $relay_all_recipients,
2680 ':relay_unknown_only' => $relay_unknown_only,
2681 ':backupmx' => $backupmx,
2682 ':gal' => $gal,
2683 ':active' => $active,
2684 ':quota' => $quota,
2685 ':defquota' => $defquota,
2686 ':maxquota' => $maxquota,
2687 ':relayhost' => $relayhost,
2688 ':mailboxes' => $mailboxes,
2689 ':aliases' => $aliases,
2690 ':description' => $description,
2691 ':domain' => $domain
2692 ));
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +01002693 // save tags
2694 foreach($tags as $index => $tag){
2695 if (empty($tag)) continue;
2696 if ($index > $GLOBALS['TAGGING_LIMIT']) {
2697 $_SESSION['return'][] = array(
2698 'type' => 'warning',
2699 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2700 'msg' => array('tag_limit_exceeded', 'limit '.$GLOBALS['TAGGING_LIMIT'])
2701 );
2702 break;
2703 }
2704 $stmt = $pdo->prepare("INSERT INTO `tags_domain` (`domain`, `tag_name`) VALUES (:domain, :tag_name)");
2705 $stmt->execute(array(
2706 ':domain' => $domain,
2707 ':tag_name' => $tag,
2708 ));
2709 }
2710
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01002711 $_SESSION['return'][] = array(
2712 'type' => 'success',
2713 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2714 'msg' => array('domain_modified', htmlspecialchars($domain))
2715 );
2716 }
2717 }
2718 break;
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +01002719 case 'domain_templates':
2720 if ($_SESSION['mailcow_cc_role'] != "admin") {
2721 $_SESSION['return'][] = array(
2722 'type' => 'danger',
2723 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_extra),
2724 'msg' => 'access_denied'
2725 );
2726 return false;
2727 }
2728 if (!is_array($_data['ids'])) {
2729 $ids = array();
2730 $ids[] = $_data['ids'];
2731 }
2732 else {
2733 $ids = $_data['ids'];
2734 }
2735 foreach ($ids as $id) {
2736 $is_now = mailbox("get", "domain_templates", $id);
2737 if (empty($is_now) ||
2738 $is_now["type"] != "domain"){
2739 $_SESSION['return'][] = array(
2740 'type' => 'danger',
2741 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_extra),
2742 'msg' => 'template_id_invalid'
2743 );
2744 continue;
2745 }
2746
2747 // check name
2748 if ($is_now["template"] == "Default" && $is_now["template"] != $_data["template"]){
2749 // keep template name of Default template
2750 $_data["template"] = $is_now["template"];
2751 }
2752 else {
2753 $_data["template"] = (isset($_data["template"])) ? $_data["template"] : $is_now["template"];
2754 }
2755 // check attributes
2756 $attr = array();
2757 $attr['tags'] = (isset($_data['tags'])) ? $_data['tags'] : array();
2758 $attr['max_num_aliases_for_domain'] = (isset($_data['max_num_aliases_for_domain'])) ? intval($_data['max_num_aliases_for_domain']) : 0;
2759 $attr['max_num_mboxes_for_domain'] = (isset($_data['max_num_mboxes_for_domain'])) ? intval($_data['max_num_mboxes_for_domain']) : 0;
2760 $attr['def_quota_for_mbox'] = (isset($_data['def_quota_for_mbox'])) ? intval($_data['def_quota_for_mbox']) * 1048576 : 0;
2761 $attr['max_quota_for_mbox'] = (isset($_data['max_quota_for_mbox'])) ? intval($_data['max_quota_for_mbox']) * 1048576 : 0;
2762 $attr['max_quota_for_domain'] = (isset($_data['max_quota_for_domain'])) ? intval($_data['max_quota_for_domain']) * 1048576 : 0;
2763 $attr['rl_frame'] = (!empty($_data['rl_frame'])) ? $_data['rl_frame'] : "s";
2764 $attr['rl_value'] = (!empty($_data['rl_value'])) ? $_data['rl_value'] : "";
2765 $attr['active'] = isset($_data['active']) ? intval($_data['active']) : 1;
2766 $attr['gal'] = (isset($_data['gal'])) ? intval($_data['gal']) : 1;
2767 $attr['backupmx'] = (isset($_data['backupmx'])) ? intval($_data['backupmx']) : 0;
2768 $attr['relay_all_recipients'] = (isset($_data['relay_all_recipients'])) ? intval($_data['relay_all_recipients']) : 0;
2769 $attr['relay_unknown_only'] = (isset($_data['relay_unknown_only'])) ? intval($_data['relay_unknown_only']) : 0;
2770 $attr['dkim_selector'] = (isset($_data['dkim_selector'])) ? $_data['dkim_selector'] : "dkim";
2771 $attr['key_size'] = isset($_data['key_size']) ? intval($_data['key_size']) : 2048;
2772
2773 // update template
2774 $stmt = $pdo->prepare("UPDATE `templates`
2775 SET `template` = :template, `attributes` = :attributes
2776 WHERE id = :id");
2777 $stmt->execute(array(
2778 ":id" => $id ,
2779 ":template" => $_data["template"] ,
2780 ":attributes" => json_encode($attr)
2781 ));
2782 }
2783
2784
2785 $_SESSION['return'][] = array(
2786 'type' => 'success',
2787 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2788 'msg' => array('template_modified', $_data["template"])
2789 );
2790 return true;
2791 break;
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01002792 case 'mailbox':
2793 if (!is_array($_data['username'])) {
2794 $usernames = array();
2795 $usernames[] = $_data['username'];
2796 }
2797 else {
2798 $usernames = $_data['username'];
2799 }
2800 foreach ($usernames as $username) {
2801 if (!filter_var($username, FILTER_VALIDATE_EMAIL)) {
2802 $_SESSION['return'][] = array(
2803 'type' => 'danger',
2804 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2805 'msg' => array('username_invalid', $username)
2806 );
2807 continue;
2808 }
2809 $is_now = mailbox('get', 'mailbox_details', $username);
2810 if (isset($_data['protocol_access'])) {
Matthias Andreas Benkard12a57352021-12-28 18:02:04 +01002811 $_data['protocol_access'] = (array)$_data['protocol_access'];
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01002812 $_data['imap_access'] = (in_array('imap', $_data['protocol_access'])) ? 1 : 0;
2813 $_data['pop3_access'] = (in_array('pop3', $_data['protocol_access'])) ? 1 : 0;
2814 $_data['smtp_access'] = (in_array('smtp', $_data['protocol_access'])) ? 1 : 0;
Matthias Andreas Benkard12a57352021-12-28 18:02:04 +01002815 $_data['sieve_access'] = (in_array('sieve', $_data['protocol_access'])) ? 1 : 0;
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01002816 }
2817 if (!empty($is_now)) {
2818 $active = (isset($_data['active'])) ? intval($_data['active']) : $is_now['active'];
2819 (int)$force_pw_update = (isset($_data['force_pw_update'])) ? intval($_data['force_pw_update']) : intval($is_now['attributes']['force_pw_update']);
Matthias Andreas Benkard12a57352021-12-28 18:02:04 +01002820 (int)$sogo_access = (isset($_data['sogo_access']) && isset($_SESSION['acl']['sogo_access']) && $_SESSION['acl']['sogo_access'] == "1") ? intval($_data['sogo_access']) : intval($is_now['attributes']['sogo_access']);
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01002821 (int)$imap_access = (isset($_data['imap_access']) && isset($_SESSION['acl']['protocol_access']) && $_SESSION['acl']['protocol_access'] == "1") ? intval($_data['imap_access']) : intval($is_now['attributes']['imap_access']);
2822 (int)$pop3_access = (isset($_data['pop3_access']) && isset($_SESSION['acl']['protocol_access']) && $_SESSION['acl']['protocol_access'] == "1") ? intval($_data['pop3_access']) : intval($is_now['attributes']['pop3_access']);
2823 (int)$smtp_access = (isset($_data['smtp_access']) && isset($_SESSION['acl']['protocol_access']) && $_SESSION['acl']['protocol_access'] == "1") ? intval($_data['smtp_access']) : intval($is_now['attributes']['smtp_access']);
Matthias Andreas Benkard12a57352021-12-28 18:02:04 +01002824 (int)$sieve_access = (isset($_data['sieve_access']) && isset($_SESSION['acl']['protocol_access']) && $_SESSION['acl']['protocol_access'] == "1") ? intval($_data['sieve_access']) : intval($is_now['attributes']['sieve_access']);
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02002825 (int)$relayhost = (isset($_data['relayhost']) && isset($_SESSION['acl']['mailbox_relayhost']) && $_SESSION['acl']['mailbox_relayhost'] == "1") ? intval($_data['relayhost']) : intval($is_now['attributes']['relayhost']);
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01002826 (int)$quota_m = (isset_has_content($_data['quota'])) ? intval($_data['quota']) : ($is_now['quota'] / 1048576);
2827 $name = (!empty($_data['name'])) ? ltrim(rtrim($_data['name'], '>'), '<') : $is_now['name'];
2828 $domain = $is_now['domain'];
2829 $quota_b = $quota_m * 1048576;
2830 $password = (!empty($_data['password'])) ? $_data['password'] : null;
2831 $password2 = (!empty($_data['password2'])) ? $_data['password2'] : null;
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +01002832 $tags = (is_array($_data['tags']) ? $_data['tags'] : array());
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01002833 }
2834 else {
2835 $_SESSION['return'][] = array(
2836 'type' => 'danger',
2837 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2838 'msg' => 'access_denied'
2839 );
2840 continue;
2841 }
2842 // if already 0 == ok
2843 if ((!isset($_SESSION['acl']['unlimited_quota']) || $_SESSION['acl']['unlimited_quota'] != "1") && ($quota_m == 0 && $is_now['quota'] != 0)) {
2844 $_SESSION['return'][] = array(
2845 'type' => 'danger',
2846 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2847 'msg' => 'unlimited_quota_acl'
2848 );
2849 return false;
2850 }
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01002851 if (!hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $domain)) {
2852 $_SESSION['return'][] = array(
2853 'type' => 'danger',
2854 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2855 'msg' => 'access_denied'
2856 );
2857 continue;
2858 }
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02002859 $DomainData = mailbox('get', 'domain_details', $domain);
2860 if ($quota_m > ($is_now['max_new_quota'] / 1048576)) {
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01002861 $_SESSION['return'][] = array(
2862 'type' => 'danger',
2863 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2864 'msg' => array('mailbox_quota_left_exceeded', ($is_now['max_new_quota'] / 1048576))
2865 );
2866 continue;
2867 }
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02002868 if ($quota_m > $DomainData['max_quota_for_mbox']) {
2869 $_SESSION['return'][] = array(
2870 'type' => 'danger',
2871 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2872 'msg' => array('mailbox_quota_exceeded', $DomainData['max_quota_for_mbox'])
2873 );
2874 continue;
2875 }
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01002876 $extra_acls = array();
2877 if (isset($_data['extended_sender_acl'])) {
2878 if (!isset($_SESSION['acl']['extend_sender_acl']) || $_SESSION['acl']['extend_sender_acl'] != "1" ) {
2879 $_SESSION['return'][] = array(
2880 'type' => 'danger',
2881 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2882 'msg' => 'access_denied'
2883 );
2884 return false;
2885 }
2886 $extra_acls = array_map('trim', preg_split( "/( |,|;|\n)/", $_data['extended_sender_acl']));
2887 foreach ($extra_acls as $i => &$extra_acl) {
2888 if (empty($extra_acl)) {
2889 continue;
2890 }
2891 if (substr($extra_acl, 0, 1) === "@") {
2892 $extra_acl = ltrim($extra_acl, '@');
2893 }
2894 if (!filter_var($extra_acl, FILTER_VALIDATE_EMAIL) && !is_valid_domain_name($extra_acl)) {
2895 $_SESSION['return'][] = array(
2896 'type' => 'danger',
2897 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2898 'msg' => array('extra_acl_invalid', htmlspecialchars($extra_acl))
2899 );
2900 unset($extra_acls[$i]);
2901 continue;
2902 }
2903 $domains = array_merge(mailbox('get', 'domains'), mailbox('get', 'alias_domains'));
2904 if (filter_var($extra_acl, FILTER_VALIDATE_EMAIL)) {
2905 $extra_acl_domain = idn_to_ascii(substr(strstr($extra_acl, '@'), 1), 0, INTL_IDNA_VARIANT_UTS46);
2906 if (in_array($extra_acl_domain, $domains)) {
2907 $_SESSION['return'][] = array(
2908 'type' => 'danger',
2909 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2910 'msg' => array('extra_acl_invalid_domain', $extra_acl_domain)
2911 );
2912 unset($extra_acls[$i]);
2913 continue;
2914 }
2915 }
2916 else {
2917 if (in_array($extra_acl, $domains)) {
2918 $_SESSION['return'][] = array(
2919 'type' => 'danger',
2920 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2921 'msg' => array('extra_acl_invalid_domain', $extra_acl_domain)
2922 );
2923 unset($extra_acls[$i]);
2924 continue;
2925 }
2926 $extra_acl = '@' . $extra_acl;
2927 }
2928 }
2929 $extra_acls = array_filter($extra_acls);
2930 $extra_acls = array_values($extra_acls);
2931 $extra_acls = array_unique($extra_acls);
2932 $stmt = $pdo->prepare("DELETE FROM `sender_acl` WHERE `external` = 1 AND `logged_in_as` = :username");
2933 $stmt->execute(array(
2934 ':username' => $username
2935 ));
2936 foreach ($extra_acls as $sender_acl_external) {
2937 $stmt = $pdo->prepare("INSERT INTO `sender_acl` (`send_as`, `logged_in_as`, `external`)
2938 VALUES (:sender_acl, :username, 1)");
2939 $stmt->execute(array(
2940 ':sender_acl' => $sender_acl_external,
2941 ':username' => $username
2942 ));
2943 }
2944 }
2945 if (isset($_data['sender_acl'])) {
2946 // Get sender_acl items set by admin
2947 $sender_acl_admin = array_merge(
2948 mailbox('get', 'sender_acl_handles', $username)['sender_acl_domains']['ro'],
2949 mailbox('get', 'sender_acl_handles', $username)['sender_acl_addresses']['ro']
2950 );
2951 // Get sender_acl items from POST array
2952 // Set sender_acl_domain_admin to empty array if sender_acl contains "default" to trigger a reset
2953 // Delete records from sender_acl if sender_acl contains "*" and set to array("*")
2954 $_data['sender_acl'] = (array)$_data['sender_acl'];
2955 if (in_array("*", $_data['sender_acl'])) {
2956 $sender_acl_domain_admin = array('*');
2957 }
2958 elseif (array("default") === $_data['sender_acl']) {
2959 $sender_acl_domain_admin = array();
2960 }
2961 else {
2962 if (array_search('default', $_data['sender_acl']) !== false){
2963 unset($_data['sender_acl'][array_search('default', $_data['sender_acl'])]);
2964 }
2965 $sender_acl_domain_admin = $_data['sender_acl'];
2966 }
2967 if (!empty($sender_acl_domain_admin) || !empty($sender_acl_admin)) {
2968 // Check items in POST array and skip invalid
2969 foreach ($sender_acl_domain_admin as $key => $val) {
2970 // Check for invalid domain or email format or not *
2971 if (!filter_var($val, FILTER_VALIDATE_EMAIL) && !is_valid_domain_name(ltrim($val, '@')) && $val != '*') {
2972 $_SESSION['return'][] = array(
2973 'type' => 'danger',
2974 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2975 'msg' => array('sender_acl_invalid', $sender_acl_domain_admin[$key])
2976 );
2977 unset($sender_acl_domain_admin[$key]);
2978 continue;
2979 }
2980 // Check if user has domain access (if object is domain)
2981 $domain = ltrim($sender_acl_domain_admin[$key], '@');
2982 if (is_valid_domain_name($domain)) {
2983 // Check for- and skip non-mailcow domains
2984 $domains = array_merge(mailbox('get', 'domains'), mailbox('get', 'alias_domains'));
2985 if (!empty($domains)) {
2986 if (!in_array($domain, $domains)) {
2987 $_SESSION['return'][] = array(
2988 'type' => 'danger',
2989 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2990 'msg' => array('sender_acl_invalid', $sender_acl_domain_admin[$key])
2991 );
2992 unset($sender_acl_domain_admin[$key]);
2993 continue;
2994 }
2995 }
2996 if (!hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $domain)) {
2997 $_SESSION['return'][] = array(
2998 'type' => 'danger',
2999 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
3000 'msg' => array('sender_acl_invalid', $sender_acl_domain_admin[$key])
3001 );
3002 unset($sender_acl_domain_admin[$key]);
3003 continue;
3004 }
3005 }
3006 // Wildcard can only be used if role == admin
3007 if ($val == '*' && $_SESSION['mailcow_cc_role'] != 'admin') {
3008 $_SESSION['return'][] = array(
3009 'type' => 'danger',
3010 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
3011 'msg' => array('sender_acl_invalid', $sender_acl_domain_admin[$key])
3012 );
3013 unset($sender_acl_domain_admin[$key]);
3014 continue;
3015 }
3016 // Check if user has alias access (if object is email)
3017 if (filter_var($val, FILTER_VALIDATE_EMAIL)) {
3018 if (!hasAliasObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $val)) {
3019 $_SESSION['return'][] = array(
3020 'type' => 'danger',
3021 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
3022 'msg' => array('sender_acl_invalid', $sender_acl_domain_admin[$key])
3023 );
3024 unset($sender_acl_domain_admin[$key]);
3025 continue;
3026 }
3027 }
3028 }
3029 // Merge both arrays
3030 $sender_acl_merged = array_merge($sender_acl_domain_admin, $sender_acl_admin);
3031 // If merged array still contains "*", set it as only value
3032 !in_array('*', $sender_acl_merged) ?: $sender_acl_merged = array('*');
3033 $stmt = $pdo->prepare("DELETE FROM `sender_acl` WHERE `external` = 0 AND `logged_in_as` = :username");
3034 $stmt->execute(array(
3035 ':username' => $username
3036 ));
3037 $fixed_sender_aliases = mailbox('get', 'sender_acl_handles', $username)['fixed_sender_aliases'];
3038 foreach ($sender_acl_merged as $sender_acl) {
3039 $domain = ltrim($sender_acl, '@');
3040 if (is_valid_domain_name($domain)) {
3041 $sender_acl = '@' . $domain;
3042 }
3043 // Don't add if allowed by alias
3044 if (in_array($sender_acl, $fixed_sender_aliases)) {
3045 continue;
3046 }
3047 $stmt = $pdo->prepare("INSERT INTO `sender_acl` (`send_as`, `logged_in_as`)
3048 VALUES (:sender_acl, :username)");
3049 $stmt->execute(array(
3050 ':sender_acl' => $sender_acl,
3051 ':username' => $username
3052 ));
3053 }
3054 }
3055 else {
3056 $stmt = $pdo->prepare("DELETE FROM `sender_acl` WHERE `external` = 0 AND `logged_in_as` = :username");
3057 $stmt->execute(array(
3058 ':username' => $username
3059 ));
3060 }
3061 }
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02003062 if (!empty($password)) {
3063 if (password_check($password, $password2) !== true) {
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01003064 continue;
3065 }
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02003066 $password_hashed = hash_password($password);
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01003067 $stmt = $pdo->prepare("UPDATE `mailbox` SET
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02003068 `password` = :password_hashed,
3069 `attributes` = JSON_SET(`attributes`, '$.passwd_update', NOW())
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01003070 WHERE `username` = :username");
3071 $stmt->execute(array(
3072 ':password_hashed' => $password_hashed,
3073 ':username' => $username
3074 ));
3075 }
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02003076 // 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 Benkardb382b102021-01-02 15:32:21 +01003077 $stmt = $pdo->prepare("UPDATE `alias` SET
3078 `active` = :active
3079 WHERE `address` = :address");
3080 $stmt->execute(array(
3081 ':address' => $username,
3082 ':active' => $active
3083 ));
3084 $stmt = $pdo->prepare("UPDATE `mailbox` SET
3085 `active` = :active,
3086 `name`= :name,
3087 `quota` = :quota_b,
3088 `attributes` = JSON_SET(`attributes`, '$.force_pw_update', :force_pw_update),
3089 `attributes` = JSON_SET(`attributes`, '$.sogo_access', :sogo_access),
3090 `attributes` = JSON_SET(`attributes`, '$.imap_access', :imap_access),
Matthias Andreas Benkard12a57352021-12-28 18:02:04 +01003091 `attributes` = JSON_SET(`attributes`, '$.sieve_access', :sieve_access),
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01003092 `attributes` = JSON_SET(`attributes`, '$.pop3_access', :pop3_access),
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02003093 `attributes` = JSON_SET(`attributes`, '$.relayhost', :relayhost),
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01003094 `attributes` = JSON_SET(`attributes`, '$.smtp_access', :smtp_access)
3095 WHERE `username` = :username");
3096 $stmt->execute(array(
3097 ':active' => $active,
3098 ':name' => $name,
3099 ':quota_b' => $quota_b,
3100 ':force_pw_update' => $force_pw_update,
3101 ':sogo_access' => $sogo_access,
3102 ':imap_access' => $imap_access,
3103 ':pop3_access' => $pop3_access,
Matthias Andreas Benkard12a57352021-12-28 18:02:04 +01003104 ':sieve_access' => $sieve_access,
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01003105 ':smtp_access' => $smtp_access,
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02003106 ':relayhost' => $relayhost,
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01003107 ':username' => $username
3108 ));
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +01003109 // save tags
3110 foreach($tags as $index => $tag){
3111 if (empty($tag)) continue;
3112 if ($index > $GLOBALS['TAGGING_LIMIT']) {
3113 $_SESSION['return'][] = array(
3114 'type' => 'warning',
3115 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
3116 'msg' => array('tag_limit_exceeded', 'limit '.$GLOBALS['TAGGING_LIMIT'])
3117 );
3118 break;
3119 }
3120 $stmt = $pdo->prepare("INSERT INTO `tags_mailbox` (`username`, `tag_name`) VALUES (:username, :tag_name)");
3121 $stmt->execute(array(
3122 ':username' => $username,
3123 ':tag_name' => $tag,
3124 ));
3125 }
3126
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01003127 $_SESSION['return'][] = array(
3128 'type' => 'success',
3129 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
3130 'msg' => array('mailbox_modified', $username)
3131 );
3132 }
3133 break;
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +01003134 case 'mailbox_templates':
3135 if ($_SESSION['mailcow_cc_role'] != "admin") {
3136 $_SESSION['return'][] = array(
3137 'type' => 'danger',
3138 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_extra),
3139 'msg' => 'access_denied'
3140 );
3141 return false;
3142 }
3143 if (!is_array($_data['ids'])) {
3144 $ids = array();
3145 $ids[] = $_data['ids'];
3146 }
3147 else {
3148 $ids = $_data['ids'];
3149 }
3150 foreach ($ids as $id) {
3151 $is_now = mailbox("get", "mailbox_templates", $id);
3152 if (empty($is_now) ||
3153 $is_now["type"] != "mailbox"){
3154 $_SESSION['return'][] = array(
3155 'type' => 'danger',
3156 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_extra),
3157 'msg' => 'template_id_invalid'
3158 );
3159 continue;
3160 }
3161
3162
3163 // check name
3164 if ($is_now["template"] == "Default" && $is_now["template"] != $_data["template"]){
3165 // keep template name of Default template
3166 $_data["template"] = $is_now["template"];
3167 }
3168 else {
3169 $_data["template"] = (isset($_data["template"])) ? $_data["template"] : $is_now["template"];
3170 }
3171 // check attributes
3172 $attr = array();
3173 $attr["quota"] = isset($_data['quota']) ? intval($_data['quota']) * 1048576 : 0;
3174 $attr['tags'] = (isset($_data['tags'])) ? $_data['tags'] : $is_now['tags'];
3175 $attr["quarantine_notification"] = (!empty($_data['quarantine_notification'])) ? $_data['quarantine_notification'] : $is_now['quarantine_notification'];
3176 $attr["quarantine_category"] = (!empty($_data['quarantine_category'])) ? $_data['quarantine_category'] : $is_now['quarantine_category'];
3177 $attr["rl_frame"] = (!empty($_data['rl_frame'])) ? $_data['rl_frame'] : $is_now['rl_frame'];
3178 $attr["rl_value"] = (!empty($_data['rl_value'])) ? $_data['rl_value'] : $is_now['rl_value'];
3179 $attr["force_pw_update"] = isset($_data['force_pw_update']) ? intval($_data['force_pw_update']) : $is_now['force_pw_update'];
3180 $attr["sogo_access"] = isset($_data['sogo_access']) ? intval($_data['sogo_access']) : $is_now['sogo_access'];
3181 $attr["active"] = isset($_data['active']) ? intval($_data['active']) : $is_now['active'];
3182 $attr["tls_enforce_in"] = isset($_data['tls_enforce_in']) ? intval($_data['tls_enforce_in']) : $is_now['tls_enforce_in'];
3183 $attr["tls_enforce_out"] = isset($_data['tls_enforce_out']) ? intval($_data['tls_enforce_out']) : $is_now['tls_enforce_out'];
3184 if (isset($_data['protocol_access'])) {
3185 $_data['protocol_access'] = (array)$_data['protocol_access'];
3186 $attr['imap_access'] = (in_array('imap', $_data['protocol_access'])) ? 1 : 0;
3187 $attr['pop3_access'] = (in_array('pop3', $_data['protocol_access'])) ? 1 : 0;
3188 $attr['smtp_access'] = (in_array('smtp', $_data['protocol_access'])) ? 1 : 0;
3189 $attr['sieve_access'] = (in_array('sieve', $_data['protocol_access'])) ? 1 : 0;
3190 }
3191 else {
3192 foreach ($is_now as $key => $value){
3193 $attr[$key] = $is_now[$key];
3194 }
3195 }
3196 if (isset($_data['acl'])) {
3197 $_data['acl'] = (array)$_data['acl'];
3198 $attr['acl_spam_alias'] = (in_array('spam_alias', $_data['acl'])) ? 1 : 0;
3199 $attr['acl_tls_policy'] = (in_array('tls_policy', $_data['acl'])) ? 1 : 0;
3200 $attr['acl_spam_score'] = (in_array('spam_score', $_data['acl'])) ? 1 : 0;
3201 $attr['acl_spam_policy'] = (in_array('spam_policy', $_data['acl'])) ? 1 : 0;
3202 $attr['acl_delimiter_action'] = (in_array('delimiter_action', $_data['acl'])) ? 1 : 0;
3203 $attr['acl_syncjobs'] = (in_array('syncjobs', $_data['acl'])) ? 1 : 0;
3204 $attr['acl_eas_reset'] = (in_array('eas_reset', $_data['acl'])) ? 1 : 0;
3205 $attr['acl_sogo_profile_reset'] = (in_array('sogo_profile_reset', $_data['acl'])) ? 1 : 0;
3206 $attr['acl_pushover'] = (in_array('pushover', $_data['acl'])) ? 1 : 0;
3207 $attr['acl_quarantine'] = (in_array('quarantine', $_data['acl'])) ? 1 : 0;
3208 $attr['acl_quarantine_attachments'] = (in_array('quarantine_attachments', $_data['acl'])) ? 1 : 0;
3209 $attr['acl_quarantine_notification'] = (in_array('quarantine_notification', $_data['acl'])) ? 1 : 0;
3210 $attr['acl_quarantine_category'] = (in_array('quarantine_category', $_data['acl'])) ? 1 : 0;
3211 $attr['acl_app_passwds'] = (in_array('app_passwds', $_data['acl'])) ? 1 : 0;
3212 } else {
3213 foreach ($is_now as $key => $value){
3214 $attr[$key] = $is_now[$key];
3215 }
3216 }
3217
3218
3219 // update template
3220 $stmt = $pdo->prepare("UPDATE `templates`
3221 SET `template` = :template, `attributes` = :attributes
3222 WHERE id = :id");
3223 $stmt->execute(array(
3224 ":id" => $id ,
3225 ":template" => $_data["template"] ,
3226 ":attributes" => json_encode($attr)
3227 ));
3228 }
3229
3230
3231 $_SESSION['return'][] = array(
3232 'type' => 'success',
3233 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
3234 'msg' => array('template_modified', $_data["template"])
3235 );
3236 return true;
3237 break;
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01003238 case 'resource':
3239 if (!is_array($_data['name'])) {
3240 $names = array();
3241 $names[] = $_data['name'];
3242 }
3243 else {
3244 $names = $_data['name'];
3245 }
3246 foreach ($names as $name) {
3247 $is_now = mailbox('get', 'resource_details', $name);
3248 if (!empty($is_now)) {
3249 $active = (isset($_data['active'])) ? intval($_data['active']) : $is_now['active'];
3250 $multiple_bookings = (isset($_data['multiple_bookings'])) ? intval($_data['multiple_bookings']) : $is_now['multiple_bookings'];
3251 $description = (!empty($_data['description'])) ? $_data['description'] : $is_now['description'];
3252 $kind = (!empty($_data['kind'])) ? $_data['kind'] : $is_now['kind'];
3253 }
3254 else {
3255 $_SESSION['return'][] = array(
3256 'type' => 'danger',
3257 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
3258 'msg' => array('resource_invalid', htmlspecialchars($name))
3259 );
3260 continue;
3261 }
3262 if (!filter_var($name, FILTER_VALIDATE_EMAIL)) {
3263 $_SESSION['return'][] = array(
3264 'type' => 'danger',
3265 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
3266 'msg' => array('resource_invalid', htmlspecialchars($name))
3267 );
3268 continue;
3269 }
3270 if (!isset($multiple_bookings) || $multiple_bookings < -1) {
3271 $multiple_bookings = -1;
3272 }
3273 if (empty($description)) {
3274 $_SESSION['return'][] = array(
3275 'type' => 'danger',
3276 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
3277 'msg' => array('description_invalid', htmlspecialchars($name))
3278 );
3279 continue;
3280 }
3281 if ($kind != 'location' && $kind != 'group' && $kind != 'thing') {
3282 $_SESSION['return'][] = array(
3283 'type' => 'danger',
3284 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
3285 'msg' => array('resource_invalid', htmlspecialchars($name))
3286 );
3287 continue;
3288 }
3289 if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $name)) {
3290 $_SESSION['return'][] = array(
3291 'type' => 'danger',
3292 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
3293 'msg' => 'access_denied'
3294 );
3295 continue;
3296 }
3297 $stmt = $pdo->prepare("UPDATE `mailbox` SET
3298 `active` = :active,
3299 `name`= :description,
3300 `kind`= :kind,
3301 `multiple_bookings`= :multiple_bookings
3302 WHERE `username` = :name");
3303 $stmt->execute(array(
3304 ':active' => $active,
3305 ':description' => $description,
3306 ':multiple_bookings' => $multiple_bookings,
3307 ':kind' => $kind,
3308 ':name' => $name
3309 ));
3310 $_SESSION['return'][] = array(
3311 'type' => 'success',
3312 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
3313 'msg' => array('resource_modified', htmlspecialchars($name))
3314 );
3315 }
3316 break;
3317 }
3318 break;
3319 case 'get':
3320 switch ($_type) {
3321 case 'sender_acl_handles':
3322 if ($_SESSION['mailcow_cc_role'] != "admin" && $_SESSION['mailcow_cc_role'] != "domainadmin") {
3323 return false;
3324 }
3325 $data['sender_acl_domains']['ro'] = array();
3326 $data['sender_acl_domains']['rw'] = array();
3327 $data['sender_acl_domains']['selectable'] = array();
3328 $data['sender_acl_addresses']['ro'] = array();
3329 $data['sender_acl_addresses']['rw'] = array();
3330 $data['sender_acl_addresses']['selectable'] = array();
3331 $data['fixed_sender_aliases'] = array();
3332 $data['external_sender_aliases'] = array();
3333 // Fixed addresses
3334 $stmt = $pdo->prepare("SELECT `address` FROM `alias` WHERE `goto` REGEXP :goto AND `address` NOT LIKE '@%'");
3335 $stmt->execute(array(':goto' => '(^|,)'.$_data.'($|,)'));
3336 $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
3337 while ($row = array_shift($rows)) {
3338 $data['fixed_sender_aliases'][] = $row['address'];
3339 }
3340 $stmt = $pdo->prepare("SELECT CONCAT(`local_part`, '@', `alias_domain`.`alias_domain`) AS `alias_domain_alias` FROM `mailbox`, `alias_domain`
3341 WHERE `alias_domain`.`target_domain` = `mailbox`.`domain`
3342 AND `mailbox`.`username` = :username");
3343 $stmt->execute(array(':username' => $_data));
3344 $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
3345 while ($row = array_shift($rows)) {
3346 if (!empty($row['alias_domain_alias'])) {
3347 $data['fixed_sender_aliases'][] = $row['alias_domain_alias'];
3348 }
3349 }
3350 // External addresses
3351 $stmt = $pdo->prepare("SELECT `send_as` as `send_as_external` FROM `sender_acl` WHERE `logged_in_as` = :logged_in_as AND `external` = '1'");
3352 $stmt->execute(array(':logged_in_as' => $_data));
3353 $exernal_rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
3354 while ($row = array_shift($exernal_rows)) {
3355 if (!empty($row['send_as_external'])) {
3356 $data['external_sender_aliases'][] = $row['send_as_external'];
3357 }
3358 }
3359 // Return array $data['sender_acl_domains/addresses']['ro'] with read-only objects
3360 // Return array $data['sender_acl_domains/addresses']['rw'] with read-write objects (can be deleted)
3361 $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` = '*')");
3362 $stmt->execute(array(':logged_in_as' => $_data));
3363 $domain_rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
3364 while ($domain_row = array_shift($domain_rows)) {
3365 if (is_valid_domain_name($domain_row['send_as']) && !hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $domain_row['send_as'])) {
3366 $data['sender_acl_domains']['ro'][] = $domain_row['send_as'];
3367 continue;
3368 }
3369 if (is_valid_domain_name($domain_row['send_as']) && hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $domain_row['send_as'])) {
3370 $data['sender_acl_domains']['rw'][] = $domain_row['send_as'];
3371 continue;
3372 }
3373 if ($domain_row['send_as'] == '*' && $_SESSION['mailcow_cc_role'] != 'admin') {
3374 $data['sender_acl_domains']['ro'][] = $domain_row['send_as'];
3375 }
3376 if ($domain_row['send_as'] == '*' && $_SESSION['mailcow_cc_role'] == 'admin') {
3377 $data['sender_acl_domains']['rw'][] = $domain_row['send_as'];
3378 }
3379 }
3380 $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` != '*')");
3381 $stmt->execute(array(':logged_in_as' => $_data));
3382 $address_rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
3383 while ($address_row = array_shift($address_rows)) {
3384 if (filter_var($address_row['send_as'], FILTER_VALIDATE_EMAIL) && !hasAliasObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $address_row['send_as'])) {
3385 $data['sender_acl_addresses']['ro'][] = $address_row['send_as'];
3386 continue;
3387 }
3388 if (filter_var($address_row['send_as'], FILTER_VALIDATE_EMAIL) && hasAliasObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $address_row['send_as'])) {
3389 $data['sender_acl_addresses']['rw'][] = $address_row['send_as'];
3390 continue;
3391 }
3392 }
3393 $stmt = $pdo->prepare("SELECT `domain` FROM `domain`
3394 WHERE `domain` NOT IN (
3395 SELECT REPLACE(`send_as`, '@', '') FROM `sender_acl`
3396 WHERE `logged_in_as` = :logged_in_as1
3397 AND `external` = '0'
3398 AND `send_as` LIKE '@%')
3399 UNION
3400 SELECT '*' FROM `domain`
3401 WHERE '*' NOT IN (
3402 SELECT `send_as` FROM `sender_acl`
3403 WHERE `logged_in_as` = :logged_in_as2
3404 AND `external` = '0'
3405 )");
3406 $stmt->execute(array(
3407 ':logged_in_as1' => $_data,
3408 ':logged_in_as2' => $_data
3409 ));
3410 $rows_domain = $stmt->fetchAll(PDO::FETCH_ASSOC);
3411 while ($row_domain = array_shift($rows_domain)) {
3412 if (is_valid_domain_name($row_domain['domain']) && hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $row_domain['domain'])) {
3413 $data['sender_acl_domains']['selectable'][] = $row_domain['domain'];
3414 continue;
3415 }
3416 if ($row_domain['domain'] == '*' && $_SESSION['mailcow_cc_role'] == 'admin') {
3417 $data['sender_acl_domains']['selectable'][] = $row_domain['domain'];
3418 continue;
3419 }
3420 }
3421 $stmt = $pdo->prepare("SELECT `address` FROM `alias`
3422 WHERE `goto` != :goto
3423 AND `address` NOT IN (
3424 SELECT `send_as` FROM `sender_acl`
3425 WHERE `logged_in_as` = :logged_in_as
3426 AND `external` = '0'
3427 AND `send_as` NOT LIKE '@%')");
3428 $stmt->execute(array(
3429 ':logged_in_as' => $_data,
3430 ':goto' => $_data
3431 ));
3432 $rows_mbox = $stmt->fetchAll(PDO::FETCH_ASSOC);
3433 while ($row = array_shift($rows_mbox)) {
3434 // Aliases are not selectable
3435 if (in_array($row['address'], $data['fixed_sender_aliases'])) {
3436 continue;
3437 }
3438 if (filter_var($row['address'], FILTER_VALIDATE_EMAIL) && hasAliasObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $row['address'])) {
3439 $data['sender_acl_addresses']['selectable'][] = $row['address'];
3440 }
3441 }
3442 return $data;
3443 break;
3444 case 'mailboxes':
3445 $mailboxes = array();
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +01003446 if (isset($_extra) && is_array($_extra) && isset($_data)) {
3447 // get by domain and tags
3448 $tags = is_array($_extra) ? $_extra : array();
3449
3450 $sql = "";
3451 foreach ($tags as $key => $tag) {
3452 $sql = $sql."SELECT DISTINCT `username` FROM `tags_mailbox` WHERE `username` LIKE ? AND `tag_name` LIKE ?"; // distinct, avoid duplicates
3453 if ($key === array_key_last($tags)) break;
3454 $sql = $sql.' UNION DISTINCT '; // combine querys with union - distinct, avoid duplicates
3455 }
3456
3457 // prepend domain to array
3458 $params = array();
3459 foreach ($tags as $key => $val){
3460 array_push($params, '%'.$_data.'%');
3461 array_push($params, '%'.$val.'%');
3462 }
3463 $stmt = $pdo->prepare($sql);
3464 $stmt->execute($params);
3465
3466 $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
3467 while($row = array_shift($rows)) {
3468 if (hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], explode('@', $row['username'])[1]))
3469 $mailboxes[] = $row['username'];
3470 }
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01003471 }
3472 elseif (isset($_data) && hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +01003473 // get by domain
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02003474 $stmt = $pdo->prepare("SELECT `username` FROM `mailbox` WHERE (`kind` = '' OR `kind` = NULL) AND `domain` = :domain");
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01003475 $stmt->execute(array(
3476 ':domain' => $_data,
3477 ));
3478 $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
3479 while($row = array_shift($rows)) {
3480 $mailboxes[] = $row['username'];
3481 }
3482 }
3483 else {
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02003484 $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 Benkardb382b102021-01-02 15:32:21 +01003485 $stmt->execute(array(
3486 ':username' => $_SESSION['mailcow_cc_username'],
3487 ':role' => $_SESSION['mailcow_cc_role'],
3488 ));
3489 $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
3490 while($row = array_shift($rows)) {
3491 $mailboxes[] = $row['username'];
3492 }
3493 }
3494 return $mailboxes;
3495 break;
3496 case 'tls_policy':
3497 $attrs = array();
3498 if (isset($_data) && filter_var($_data, FILTER_VALIDATE_EMAIL)) {
3499 if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
3500 return false;
3501 }
3502 }
3503 else {
3504 $_data = $_SESSION['mailcow_cc_username'];
3505 }
3506 $stmt = $pdo->prepare("SELECT `attributes` FROM `mailbox` WHERE `username` = :username");
3507 $stmt->execute(array(':username' => $_data));
3508 $attrs = $stmt->fetch(PDO::FETCH_ASSOC);
3509 $attrs = json_decode($attrs['attributes'], true);
3510 return array(
3511 'tls_enforce_in' => $attrs['tls_enforce_in'],
3512 'tls_enforce_out' => $attrs['tls_enforce_out']
3513 );
3514 break;
3515 case 'quarantine_notification':
3516 $attrs = array();
3517 if (isset($_data) && filter_var($_data, FILTER_VALIDATE_EMAIL)) {
3518 if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
3519 return false;
3520 }
3521 }
3522 else {
3523 $_data = $_SESSION['mailcow_cc_username'];
3524 }
3525 $stmt = $pdo->prepare("SELECT `attributes` FROM `mailbox` WHERE `username` = :username");
3526 $stmt->execute(array(':username' => $_data));
3527 $attrs = $stmt->fetch(PDO::FETCH_ASSOC);
3528 $attrs = json_decode($attrs['attributes'], true);
3529 return $attrs['quarantine_notification'];
3530 break;
3531 case 'quarantine_category':
3532 $attrs = array();
3533 if (isset($_data) && filter_var($_data, FILTER_VALIDATE_EMAIL)) {
3534 if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
3535 return false;
3536 }
3537 }
3538 else {
3539 $_data = $_SESSION['mailcow_cc_username'];
3540 }
3541 $stmt = $pdo->prepare("SELECT `attributes` FROM `mailbox` WHERE `username` = :username");
3542 $stmt->execute(array(':username' => $_data));
3543 $attrs = $stmt->fetch(PDO::FETCH_ASSOC);
3544 $attrs = json_decode($attrs['attributes'], true);
3545 return $attrs['quarantine_category'];
3546 break;
3547 case 'filters':
3548 $filters = array();
3549 if (isset($_data) && filter_var($_data, FILTER_VALIDATE_EMAIL)) {
3550 if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
3551 return false;
3552 }
3553 }
3554 else {
3555 $_data = $_SESSION['mailcow_cc_username'];
3556 }
3557 $stmt = $pdo->prepare("SELECT `id` FROM `sieve_filters` WHERE `username` = :username");
3558 $stmt->execute(array(':username' => $_data));
3559 $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
3560 while($row = array_shift($rows)) {
3561 $filters[] = $row['id'];
3562 }
3563 return $filters;
3564 break;
3565 case 'global_filter_details':
3566 $global_filters = array();
3567 if ($_SESSION['mailcow_cc_role'] != "admin") {
3568 return false;
3569 }
3570 $global_filters['prefilter'] = file_get_contents('/global_sieve/before');
3571 $global_filters['postfilter'] = file_get_contents('/global_sieve/after');
3572 return $global_filters;
3573 break;
3574 case 'filter_details':
3575 $filter_details = array();
3576 if (!is_numeric($_data)) {
3577 return false;
3578 }
3579 $stmt = $pdo->prepare("SELECT CASE `script_name` WHEN 'active' THEN 1 ELSE 0 END AS `active`,
3580 id,
3581 username,
3582 filter_type,
3583 script_data,
3584 script_desc
3585 FROM `sieve_filters`
3586 WHERE `id` = :id");
3587 $stmt->execute(array(':id' => $_data));
3588 $filter_details = $stmt->fetch(PDO::FETCH_ASSOC);
3589 if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $filter_details['username'])) {
3590 return false;
3591 }
3592 return $filter_details;
3593 break;
3594 case 'active_user_sieve':
3595 $filter_details = array();
3596 if (isset($_data) && filter_var($_data, FILTER_VALIDATE_EMAIL)) {
3597 if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
3598 return false;
3599 }
3600 }
3601 else {
3602 $_data = $_SESSION['mailcow_cc_username'];
3603 }
3604 $exec_fields = array(
3605 'cmd' => 'sieve',
3606 'task' => 'list',
3607 'username' => $_data
3608 );
3609 $filters = docker('post', 'dovecot-mailcow', 'exec', $exec_fields);
3610 $filters = array_filter(preg_split("/(\r\n|\n|\r)/",$filters));
3611 foreach ($filters as $filter) {
3612 if (preg_match('/.+ ACTIVE/i', $filter)) {
3613 $exec_fields = array(
3614 'cmd' => 'sieve',
3615 'task' => 'print',
3616 'script_name' => substr($filter, 0, -7),
3617 'username' => $_data
3618 );
3619 $script = docker('post', 'dovecot-mailcow', 'exec', $exec_fields);
3620 // Remove first line
3621 return preg_replace('/^.+\n/', '', $script);
3622 }
3623 }
3624 return false;
3625 break;
3626 case 'syncjob_details':
3627 $syncjobdetails = array();
3628 if (!is_numeric($_data)) {
3629 return false;
3630 }
3631 if (isset($_extra) && in_array('no_log', $_extra)) {
3632 $field_query = $pdo->query('SHOW FIELDS FROM `imapsync` WHERE FIELD NOT IN ("returned_text", "password1")');
3633 $fields = $field_query->fetchAll(PDO::FETCH_ASSOC);
3634 while($field = array_shift($fields)) {
3635 $shown_fields[] = $field['Field'];
3636 }
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02003637 $stmt = $pdo->prepare("SELECT " . implode(',', (array)$shown_fields) . ",
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01003638 `active`
3639 FROM `imapsync` WHERE id = :id");
3640 }
3641 elseif (isset($_extra) && in_array('with_password', $_extra)) {
3642 $stmt = $pdo->prepare("SELECT *,
3643 `active`
3644 FROM `imapsync` WHERE id = :id");
3645 }
3646 else {
3647 $field_query = $pdo->query('SHOW FIELDS FROM `imapsync` WHERE FIELD NOT IN ("password1")');
3648 $fields = $field_query->fetchAll(PDO::FETCH_ASSOC);
3649 while($field = array_shift($fields)) {
3650 $shown_fields[] = $field['Field'];
3651 }
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02003652 $stmt = $pdo->prepare("SELECT " . implode(',', (array)$shown_fields) . ",
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01003653 `active`
3654 FROM `imapsync` WHERE id = :id");
3655 }
3656 $stmt->execute(array(':id' => $_data));
3657 $syncjobdetails = $stmt->fetch(PDO::FETCH_ASSOC);
3658 if (!empty($syncjobdetails['returned_text'])) {
3659 $syncjobdetails['log'] = $syncjobdetails['returned_text'];
3660 }
3661 else {
3662 $syncjobdetails['log'] = '';
3663 }
3664 unset($syncjobdetails['returned_text']);
3665 if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $syncjobdetails['user2'])) {
3666 return false;
3667 }
3668 return $syncjobdetails;
3669 break;
3670 case 'syncjobs':
3671 $syncjobdata = array();
3672 if (isset($_data) && filter_var($_data, FILTER_VALIDATE_EMAIL)) {
3673 if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
3674 return false;
3675 }
3676 }
3677 else {
3678 $_data = $_SESSION['mailcow_cc_username'];
3679 }
3680 $stmt = $pdo->prepare("SELECT `id` FROM `imapsync` WHERE `user2` = :username");
3681 $stmt->execute(array(':username' => $_data));
3682 $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
3683 while($row = array_shift($rows)) {
3684 $syncjobdata[] = $row['id'];
3685 }
3686 return $syncjobdata;
3687 break;
3688 case 'spam_score':
3689 $curl = curl_init();
3690 curl_setopt($curl, CURLOPT_UNIX_SOCKET_PATH, '/var/lib/rspamd/rspamd.sock');
3691 curl_setopt($curl, CURLOPT_URL,"http://rspamd/actions");
3692 curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
3693 $default_actions = curl_exec($curl);
3694 if (!curl_errno($curl)) {
3695 $data_array = json_decode($default_actions, true);
3696 curl_close($curl);
3697 foreach ($data_array as $data) {
3698 if ($data['action'] == 'reject') {
3699 $reject = $data['value'];
3700 continue;
3701 }
3702 elseif ($data['action'] == 'add header') {
3703 $add_header = $data['value'];
3704 continue;
3705 }
3706 }
3707 if (empty($add_header) || empty($reject)) {
3708 // Assume default, set warning
3709 $default = "5, 15";
3710 $_SESSION['return'][] = array(
3711 'type' => 'warning',
3712 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
3713 'msg' => 'Could not determine servers default spam score, assuming default'
3714 );
3715 }
3716 else {
3717 $default = $add_header . ', ' . $reject;
3718 }
3719 }
3720 else {
3721 // Assume default, set warning
3722 $default = "5, 15";
3723 $_SESSION['return'][] = array(
3724 'type' => 'warning',
3725 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
3726 'msg' => 'Could not determine servers default spam score, assuming default'
3727 );
3728 }
3729 curl_close($curl);
3730 $policydata = array();
3731 if (isset($_data) && filter_var($_data, FILTER_VALIDATE_EMAIL)) {
3732 if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
3733 return false;
3734 }
3735 }
3736 else {
3737 $_data = $_SESSION['mailcow_cc_username'];
3738 }
3739 $stmt = $pdo->prepare("SELECT `value` FROM `filterconf` WHERE `object` = :username AND
3740 (`option` = 'lowspamlevel' OR `option` = 'highspamlevel')");
3741 $stmt->execute(array(':username' => $_data));
3742 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
3743 if (empty($num_results)) {
3744 return $default;
3745 }
3746 else {
3747 $stmt = $pdo->prepare("SELECT `value` FROM `filterconf` WHERE `option` = 'highspamlevel' AND `object` = :username");
3748 $stmt->execute(array(':username' => $_data));
3749 $highspamlevel = $stmt->fetch(PDO::FETCH_ASSOC);
3750 $stmt = $pdo->prepare("SELECT `value` FROM `filterconf` WHERE `option` = 'lowspamlevel' AND `object` = :username");
3751 $stmt->execute(array(':username' => $_data));
3752 $lowspamlevel = $stmt->fetch(PDO::FETCH_ASSOC);
3753 return $lowspamlevel['value'].', '.$highspamlevel['value'];
3754 }
3755 break;
3756 case 'time_limited_aliases':
3757 $tladata = array();
3758 if (isset($_data) && filter_var($_data, FILTER_VALIDATE_EMAIL)) {
3759 if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
3760 return false;
3761 }
3762 }
3763 else {
3764 $_data = $_SESSION['mailcow_cc_username'];
3765 }
3766 $stmt = $pdo->prepare("SELECT `address`,
3767 `goto`,
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02003768 `validity`,
3769 `created`,
3770 `modified`
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01003771 FROM `spamalias`
3772 WHERE `goto` = :username
3773 AND `validity` >= :unixnow");
3774 $stmt->execute(array(':username' => $_data, ':unixnow' => time()));
3775 $tladata = $stmt->fetchAll(PDO::FETCH_ASSOC);
3776 return $tladata;
3777 break;
3778 case 'delimiter_action':
3779 $policydata = array();
3780 if (isset($_data) && filter_var($_data, FILTER_VALIDATE_EMAIL)) {
3781 if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
3782 return false;
3783 }
3784 }
3785 else {
3786 $_data = $_SESSION['mailcow_cc_username'];
3787 }
3788 try {
3789 if ($redis->hGet('RCPT_WANTS_SUBJECT_TAG', $_data)) {
3790 return "subject";
3791 }
3792 elseif ($redis->hGet('RCPT_WANTS_SUBFOLDER_TAG', $_data)) {
3793 return "subfolder";
3794 }
3795 else {
3796 return "none";
3797 }
3798 }
3799 catch (RedisException $e) {
3800 $_SESSION['return'][] = array(
3801 'type' => 'danger',
3802 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
3803 'msg' => array('redis_error', $e)
3804 );
3805 return false;
3806 }
3807 break;
3808 case 'resources':
3809 $resources = array();
3810 if (isset($_data) && !hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
3811 return false;
3812 }
3813 elseif (isset($_data) && hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
3814 $stmt = $pdo->prepare("SELECT `username` FROM `mailbox` WHERE `kind` REGEXP 'location|thing|group' AND `domain` = :domain");
3815 $stmt->execute(array(
3816 ':domain' => $_data,
3817 ));
3818 $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
3819 while($row = array_shift($rows)) {
3820 $resources[] = $row['username'];
3821 }
3822 }
3823 else {
3824 $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");
3825 $stmt->execute(array(
3826 ':username' => $_SESSION['mailcow_cc_username'],
3827 ':role' => $_SESSION['mailcow_cc_role'],
3828 ));
3829 $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
3830 while($row = array_shift($rows)) {
3831 $resources[] = $row['username'];
3832 }
3833 }
3834 return $resources;
3835 break;
3836 case 'alias_domains':
3837 $aliasdomains = array();
3838 if (isset($_data) && !hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
3839 return false;
3840 }
3841 elseif (isset($_data) && hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
3842 $stmt = $pdo->prepare("SELECT `alias_domain` FROM `alias_domain` WHERE `target_domain` = :domain");
3843 $stmt->execute(array(
3844 ':domain' => $_data,
3845 ));
3846 $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
3847 while($row = array_shift($rows)) {
3848 $aliasdomains[] = $row['alias_domain'];
3849 }
3850 }
3851 else {
3852 $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");
3853 $stmt->execute(array(
3854 ':username' => $_SESSION['mailcow_cc_username'],
3855 ':role' => $_SESSION['mailcow_cc_role'],
3856 ));
3857 $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
3858 while($row = array_shift($rows)) {
3859 $aliasdomains[] = $row['alias_domain'];
3860 }
3861 }
3862 return $aliasdomains;
3863 break;
3864 case 'aliases':
3865 $aliases = array();
3866 if (!hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
3867 return false;
3868 }
3869 $stmt = $pdo->prepare("SELECT `id` FROM `alias` WHERE `address` != `goto` AND `domain` = :domain");
3870 $stmt->execute(array(
3871 ':domain' => $_data,
3872 ));
3873 $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
3874 while($row = array_shift($rows)) {
3875 $aliases[] = $row['id'];
3876 }
3877 return $aliases;
3878 break;
3879 case 'alias_details':
3880 $aliasdata = array();
3881 $stmt = $pdo->prepare("SELECT
3882 `id`,
3883 `domain`,
3884 `goto`,
3885 `address`,
3886 `public_comment`,
3887 `private_comment`,
3888 `active`,
3889 `sogo_visible`,
3890 `created`,
3891 `modified`
3892 FROM `alias`
3893 WHERE (`id` = :id OR `address` = :address) AND `address` != `goto`");
3894 $stmt->execute(array(
3895 ':id' => $_data,
3896 ':address' => $_data,
3897 ));
3898 $row = $stmt->fetch(PDO::FETCH_ASSOC);
3899 $stmt = $pdo->prepare("SELECT `target_domain` FROM `alias_domain` WHERE `alias_domain` = :domain");
3900 $stmt->execute(array(
3901 ':domain' => $row['domain'],
3902 ));
3903 $row_alias_domain = $stmt->fetch(PDO::FETCH_ASSOC);
3904 if (isset($row_alias_domain['target_domain']) && !empty($row_alias_domain['target_domain'])) {
3905 $aliasdata['in_primary_domain'] = $row_alias_domain['target_domain'];
3906 }
3907 else {
3908 $aliasdata['in_primary_domain'] = "";
3909 }
3910 $aliasdata['id'] = $row['id'];
3911 $aliasdata['domain'] = $row['domain'];
3912 $aliasdata['public_comment'] = $row['public_comment'];
3913 $aliasdata['private_comment'] = $row['private_comment'];
3914 $aliasdata['domain'] = $row['domain'];
3915 $aliasdata['goto'] = $row['goto'];
3916 $aliasdata['address'] = $row['address'];
3917 (!filter_var($aliasdata['address'], FILTER_VALIDATE_EMAIL)) ? $aliasdata['is_catch_all'] = 1 : $aliasdata['is_catch_all'] = 0;
3918 $aliasdata['active'] = $row['active'];
3919 $aliasdata['active_int'] = $row['active'];
3920 $aliasdata['sogo_visible'] = $row['sogo_visible'];
3921 $aliasdata['sogo_visible_int'] = $row['sogo_visible'];
3922 $aliasdata['created'] = $row['created'];
3923 $aliasdata['modified'] = $row['modified'];
3924 if (!hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $aliasdata['domain'])) {
3925 return false;
3926 }
3927 return $aliasdata;
3928 break;
3929 case 'alias_domain_details':
3930 $aliasdomaindata = array();
3931 $rl = ratelimit('get', 'domain', $_data);
3932 $stmt = $pdo->prepare("SELECT
3933 `alias_domain`,
3934 `target_domain`,
3935 `active`,
3936 `created`,
3937 `modified`
3938 FROM `alias_domain`
3939 WHERE `alias_domain` = :aliasdomain");
3940 $stmt->execute(array(
3941 ':aliasdomain' => $_data,
3942 ));
3943 $row = $stmt->fetch(PDO::FETCH_ASSOC);
3944 $stmt = $pdo->prepare("SELECT `backupmx` FROM `domain` WHERE `domain` = :target_domain");
3945 $stmt->execute(array(
3946 ':target_domain' => $row['target_domain']
3947 ));
3948 $row_parent = $stmt->fetch(PDO::FETCH_ASSOC);
3949 $aliasdomaindata['alias_domain'] = $row['alias_domain'];
3950 $aliasdomaindata['parent_is_backupmx'] = $row_parent['backupmx'];
3951 $aliasdomaindata['target_domain'] = $row['target_domain'];
3952 $aliasdomaindata['active'] = $row['active'];
3953 $aliasdomaindata['active_int'] = $row['active'];
3954 $aliasdomaindata['rl'] = $rl;
3955 $aliasdomaindata['created'] = $row['created'];
3956 $aliasdomaindata['modified'] = $row['modified'];
3957 if (!hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $aliasdomaindata['target_domain'])) {
3958 return false;
3959 }
3960 return $aliasdomaindata;
3961 break;
3962 case 'domains':
3963 $domains = array();
3964 if ($_SESSION['mailcow_cc_role'] != "admin" && $_SESSION['mailcow_cc_role'] != "domainadmin") {
3965 return false;
3966 }
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +01003967
3968 if (isset($_extra) && is_array($_extra)){
3969 // get by tags
3970 $tags = is_array($_extra) ? $_extra : array();
3971 // add % as prefix and suffix to every element for relative searching
3972 $tags = array_map(function($x){ return '%'.$x.'%'; }, $tags);
3973 $sql = "";
3974 foreach ($tags as $key => $tag) {
3975 $sql = $sql."SELECT DISTINCT `domain` FROM `tags_domain` WHERE `tag_name` LIKE ?"; // distinct, avoid duplicates
3976 if ($key === array_key_last($tags)) break;
3977 $sql = $sql.' UNION DISTINCT '; // combine querys with union - distinct, avoid duplicates
3978 }
3979 $stmt = $pdo->prepare($sql);
3980 $stmt->execute($tags);
3981
3982 $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
3983 while($row = array_shift($rows)) {
3984 if ($_SESSION['mailcow_cc_role'] == "admin")
3985 $domains[] = $row['domain'];
3986 elseif (hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $row['domain']))
3987 $domains[] = $row['domain'];
3988 }
3989 } else {
3990 // get all
3991 $stmt = $pdo->prepare("SELECT `domain` FROM `domain`
3992 WHERE (`domain` IN (
3993 SELECT `domain` from `domain_admins`
3994 WHERE (`active`='1' AND `username` = :username))
3995 )
3996 OR 'admin'= :role");
3997 $stmt->execute(array(
3998 ':username' => $_SESSION['mailcow_cc_username'],
3999 ':role' => $_SESSION['mailcow_cc_role'],
4000 ));
4001 $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
4002 while($row = array_shift($rows)) {
4003 $domains[] = $row['domain'];
4004 }
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01004005 }
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +01004006
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01004007 return $domains;
4008 break;
4009 case 'domain_details':
4010 $domaindata = array();
4011 $_data = idn_to_ascii(strtolower(trim($_data)), 0, INTL_IDNA_VARIANT_UTS46);
4012 if (!hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
4013 return false;
4014 }
4015 $stmt = $pdo->prepare("SELECT `target_domain` FROM `alias_domain` WHERE `alias_domain` = :domain");
4016 $stmt->execute(array(
4017 ':domain' => $_data
4018 ));
4019 $row = $stmt->fetch(PDO::FETCH_ASSOC);
4020 if (!empty($row)) {
4021 $_data = $row['target_domain'];
4022 }
4023 $stmt = $pdo->prepare("SELECT
4024 `domain`,
4025 `description`,
4026 `aliases`,
4027 `mailboxes`,
4028 `defquota`,
4029 `maxquota`,
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +01004030 `created`,
4031 `modified`,
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01004032 `quota`,
4033 `relayhost`,
4034 `relay_all_recipients`,
4035 `relay_unknown_only`,
4036 `backupmx`,
4037 `gal`,
4038 `active`
4039 FROM `domain` WHERE `domain`= :domain");
4040 $stmt->execute(array(
4041 ':domain' => $_data
4042 ));
4043 $row = $stmt->fetch(PDO::FETCH_ASSOC);
4044 if (empty($row)) {
4045 return false;
4046 }
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02004047 $stmt = $pdo->prepare("SELECT COUNT(`username`) AS `count`,
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01004048 COALESCE(SUM(`quota`), 0) AS `in_use`
4049 FROM `mailbox`
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02004050 WHERE (`kind` = '' OR `kind` = NULL)
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01004051 AND `domain` = :domain");
4052 $stmt->execute(array(':domain' => $row['domain']));
4053 $MailboxDataDomain = $stmt->fetch(PDO::FETCH_ASSOC);
4054 $stmt = $pdo->prepare("SELECT SUM(bytes) AS `bytes_total`, SUM(messages) AS `msgs_total` FROM `quota2`
4055 WHERE `username` IN (
4056 SELECT `username` FROM `mailbox`
4057 WHERE `domain` = :domain
4058 );");
4059 $stmt->execute(array(':domain' => $row['domain']));
4060 $SumQuotaInUse = $stmt->fetch(PDO::FETCH_ASSOC);
4061 $rl = ratelimit('get', 'domain', $_data);
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02004062 $domaindata['max_new_mailbox_quota'] = ($row['quota'] * 1048576) - $MailboxDataDomain['in_use'];
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01004063 if ($domaindata['max_new_mailbox_quota'] > ($row['maxquota'] * 1048576)) {
4064 $domaindata['max_new_mailbox_quota'] = ($row['maxquota'] * 1048576);
4065 }
4066 $domaindata['def_new_mailbox_quota'] = $domaindata['max_new_mailbox_quota'];
4067 if ($domaindata['def_new_mailbox_quota'] > ($row['defquota'] * 1048576)) {
4068 $domaindata['def_new_mailbox_quota'] = ($row['defquota'] * 1048576);
4069 }
4070 $domaindata['quota_used_in_domain'] = $MailboxDataDomain['in_use'];
4071 if (!empty($SumQuotaInUse['bytes_total'])) {
4072 $domaindata['bytes_total'] = $SumQuotaInUse['bytes_total'];
4073 }
4074 else {
4075 $domaindata['bytes_total'] = 0;
4076 }
4077 if (!empty($SumQuotaInUse['msgs_total'])) {
4078 $domaindata['msgs_total'] = $SumQuotaInUse['msgs_total'];
4079 }
4080 else {
4081 $domaindata['msgs_total'] = 0;
4082 }
4083 $domaindata['mboxes_in_domain'] = $MailboxDataDomain['count'];
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02004084 $domaindata['mboxes_left'] = $row['mailboxes'] - $MailboxDataDomain['count'];
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01004085 $domaindata['domain_name'] = $row['domain'];
4086 $domaindata['description'] = $row['description'];
4087 $domaindata['max_num_aliases_for_domain'] = $row['aliases'];
4088 $domaindata['max_num_mboxes_for_domain'] = $row['mailboxes'];
4089 $domaindata['def_quota_for_mbox'] = $row['defquota'] * 1048576;
4090 $domaindata['max_quota_for_mbox'] = $row['maxquota'] * 1048576;
4091 $domaindata['max_quota_for_domain'] = $row['quota'] * 1048576;
4092 $domaindata['relayhost'] = $row['relayhost'];
4093 $domaindata['backupmx'] = $row['backupmx'];
4094 $domaindata['backupmx_int'] = $row['backupmx'];
4095 $domaindata['gal'] = $row['gal'];
4096 $domaindata['gal_int'] = $row['gal'];
4097 $domaindata['rl'] = $rl;
4098 $domaindata['active'] = $row['active'];
4099 $domaindata['active_int'] = $row['active'];
4100 $domaindata['relay_all_recipients'] = $row['relay_all_recipients'];
4101 $domaindata['relay_all_recipients_int'] = $row['relay_all_recipients'];
4102 $domaindata['relay_unknown_only'] = $row['relay_unknown_only'];
4103 $domaindata['relay_unknown_only_int'] = $row['relay_unknown_only'];
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +01004104 $domaindata['created'] = $row['created'];
4105 $domaindata['modified'] = $row['modified'];
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02004106 $stmt = $pdo->prepare("SELECT COUNT(`address`) AS `alias_count` FROM `alias`
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01004107 WHERE (`domain`= :domain OR `domain` IN (SELECT `alias_domain` FROM `alias_domain` WHERE `target_domain` = :domain2))
4108 AND `address` NOT IN (
4109 SELECT `username` FROM `mailbox`
4110 )");
4111 $stmt->execute(array(
4112 ':domain' => $_data,
4113 ':domain2' => $_data
4114 ));
4115 $AliasDataDomain = $stmt->fetch(PDO::FETCH_ASSOC);
4116 (isset($AliasDataDomain['alias_count'])) ? $domaindata['aliases_in_domain'] = $AliasDataDomain['alias_count'] : $domaindata['aliases_in_domain'] = "0";
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02004117 $domaindata['aliases_left'] = $row['aliases'] - $AliasDataDomain['alias_count'];
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01004118 if ($_SESSION['mailcow_cc_role'] == "admin")
4119 {
4120 $stmt = $pdo->prepare("SELECT GROUP_CONCAT(`username` SEPARATOR ', ') AS domain_admins FROM `domain_admins` WHERE `domain` = :domain");
4121 $stmt->execute(array(
4122 ':domain' => $_data
4123 ));
4124 $domain_admins = $stmt->fetch(PDO::FETCH_ASSOC);
4125 (isset($domain_admins['domain_admins'])) ? $domaindata['domain_admins'] = $domain_admins['domain_admins'] : $domaindata['domain_admins'] = "-";
4126 }
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +01004127 $stmt = $pdo->prepare("SELECT `tag_name`
4128 FROM `tags_domain` WHERE `domain`= :domain");
4129 $stmt->execute(array(
4130 ':domain' => $_data
4131 ));
4132 $tags = $stmt->fetchAll(PDO::FETCH_ASSOC);
4133 while ($tag = array_shift($tags)) {
4134 $domaindata['tags'][] = $tag['tag_name'];
4135 }
4136
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01004137 return $domaindata;
4138 break;
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +01004139 case 'domain_templates':
4140 if ($_SESSION['mailcow_cc_role'] != "admin" && $_SESSION['mailcow_cc_role'] != "domainadmin") {
4141 return false;
4142 }
4143 $_data = (isset($_data)) ? intval($_data) : null;
4144
4145 if (isset($_data)){
4146 $stmt = $pdo->prepare("SELECT * FROM `templates`
4147 WHERE `id` = :id AND type = :type");
4148 $stmt->execute(array(
4149 ":id" => $_data,
4150 ":type" => "domain"
4151 ));
4152 $row = $stmt->fetch(PDO::FETCH_ASSOC);
4153
4154 if (empty($row)){
4155 return false;
4156 }
4157
4158 $row["attributes"] = json_decode($row["attributes"], true);
4159 return $row;
4160 }
4161 else {
4162 $stmt = $pdo->prepare("SELECT * FROM `templates` WHERE `type` = 'domain'");
4163 $stmt->execute();
4164 $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
4165
4166 if (empty($rows)){
4167 return false;
4168 }
4169
4170 foreach($rows as $key => $row){
4171 $rows[$key]["attributes"] = json_decode($row["attributes"], true);
4172 }
4173 return $rows;
4174 }
4175 break;
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01004176 case 'mailbox_details':
4177 if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
4178 return false;
4179 }
4180 $mailboxdata = array();
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01004181 if (preg_match('/y|yes/i', getenv('MASTER'))) {
4182 $stmt = $pdo->prepare("SELECT
4183 `domain`.`backupmx`,
4184 `mailbox`.`username`,
4185 `mailbox`.`name`,
4186 `mailbox`.`active`,
4187 `mailbox`.`domain`,
4188 `mailbox`.`local_part`,
4189 `mailbox`.`quota`,
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +01004190 `mailbox`.`created`,
4191 `mailbox`.`modified`,
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01004192 `quota2`.`bytes`,
4193 `attributes`,
4194 `quota2`.`messages`
4195 FROM `mailbox`, `quota2`, `domain`
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02004196 WHERE (`mailbox`.`kind` = '' OR `mailbox`.`kind` = NULL)
4197 AND `mailbox`.`username` = `quota2`.`username`
4198 AND `domain`.`domain` = `mailbox`.`domain`
4199 AND `mailbox`.`username` = :mailbox");
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01004200 }
4201 else {
4202 $stmt = $pdo->prepare("SELECT
4203 `domain`.`backupmx`,
4204 `mailbox`.`username`,
4205 `mailbox`.`name`,
4206 `mailbox`.`active`,
4207 `mailbox`.`domain`,
4208 `mailbox`.`local_part`,
4209 `mailbox`.`quota`,
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +01004210 `mailbox`.`created`,
4211 `mailbox`.`modified`,
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01004212 `quota2replica`.`bytes`,
4213 `attributes`,
4214 `quota2replica`.`messages`
4215 FROM `mailbox`, `quota2replica`, `domain`
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02004216 WHERE (`mailbox`.`kind` = '' OR `mailbox`.`kind` = NULL)
4217 AND `mailbox`.`username` = `quota2replica`.`username`
4218 AND `domain`.`domain` = `mailbox`.`domain`
4219 AND `mailbox`.`username` = :mailbox");
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01004220 }
4221 $stmt->execute(array(
4222 ':mailbox' => $_data,
4223 ));
4224 $row = $stmt->fetch(PDO::FETCH_ASSOC);
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02004225
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01004226 $mailboxdata['username'] = $row['username'];
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01004227 $mailboxdata['active'] = $row['active'];
4228 $mailboxdata['active_int'] = $row['active'];
4229 $mailboxdata['domain'] = $row['domain'];
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02004230 $mailboxdata['relayhost'] = $row['relayhost'];
4231 $mailboxdata['name'] = $row['name'];
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01004232 $mailboxdata['local_part'] = $row['local_part'];
4233 $mailboxdata['quota'] = $row['quota'];
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02004234 $mailboxdata['messages'] = $row['messages'];
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01004235 $mailboxdata['attributes'] = json_decode($row['attributes'], true);
4236 $mailboxdata['quota_used'] = intval($row['bytes']);
4237 $mailboxdata['percent_in_use'] = ($row['quota'] == 0) ? '- ' : round((intval($row['bytes']) / intval($row['quota'])) * 100);
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +01004238 $mailboxdata['created'] = $row['created'];
4239 $mailboxdata['modified'] = $row['modified'];
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02004240
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01004241 if ($mailboxdata['percent_in_use'] === '- ') {
4242 $mailboxdata['percent_class'] = "info";
4243 }
4244 elseif ($mailboxdata['percent_in_use'] >= 90) {
4245 $mailboxdata['percent_class'] = "danger";
4246 }
4247 elseif ($mailboxdata['percent_in_use'] >= 75) {
4248 $mailboxdata['percent_class'] = "warning";
4249 }
4250 else {
4251 $mailboxdata['percent_class'] = "success";
4252 }
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02004253
4254 // Determine last logins
4255 $stmt = $pdo->prepare("SELECT MAX(`datetime`) AS `datetime`, `service` FROM `sasl_log`
4256 WHERE `username` = :mailbox
4257 GROUP BY `service` DESC");
4258 $stmt->execute(array(':mailbox' => $_data));
4259 $SaslLogsData = $stmt->fetchAll(PDO::FETCH_ASSOC);
4260 foreach ($SaslLogsData as $SaslLogs) {
4261 if ($SaslLogs['service'] == 'imap') {
4262 $last_imap_login = strtotime($SaslLogs['datetime']);
4263 }
4264 else if ($SaslLogs['service'] == 'smtp') {
4265 $last_smtp_login = strtotime($SaslLogs['datetime']);
4266 }
4267 else if ($SaslLogs['service'] == 'pop3') {
4268 $last_pop3_login = strtotime($SaslLogs['datetime']);
4269 }
4270 }
4271 if (!isset($last_imap_login) || $GLOBALS['SHOW_LAST_LOGIN'] === false) {
4272 $last_imap_login = 0;
4273 }
4274 if (!isset($last_smtp_login) || $GLOBALS['SHOW_LAST_LOGIN'] === false) {
4275 $last_smtp_login = 0;
4276 }
4277 if (!isset($last_pop3_login) || $GLOBALS['SHOW_LAST_LOGIN'] === false) {
4278 $last_pop3_login = 0;
4279 }
4280 $mailboxdata['last_imap_login'] = $last_imap_login;
4281 $mailboxdata['last_smtp_login'] = $last_smtp_login;
4282 $mailboxdata['last_pop3_login'] = $last_pop3_login;
4283
4284 if (!isset($_extra) || $_extra != 'reduced') {
4285 $rl = ratelimit('get', 'mailbox', $_data);
4286 $stmt = $pdo->prepare("SELECT `maxquota`, `quota` FROM `domain` WHERE `domain` = :domain");
4287 $stmt->execute(array(':domain' => $row['domain']));
4288 $DomainQuota = $stmt->fetch(PDO::FETCH_ASSOC);
4289
4290 $stmt = $pdo->prepare("SELECT IFNULL(COUNT(`active`), 0) AS `pushover_active` FROM `pushover` WHERE `username` = :username AND `active` = 1");
4291 $stmt->execute(array(':username' => $_data));
4292 $PushoverActive = $stmt->fetch(PDO::FETCH_ASSOC);
4293 $stmt = $pdo->prepare("SELECT COALESCE(SUM(`quota`), 0) as `in_use` FROM `mailbox` WHERE (`kind` = '' OR `kind` = NULL) AND `domain` = :domain AND `username` != :username");
4294 $stmt->execute(array(':domain' => $row['domain'], ':username' => $_data));
4295 $MailboxUsage = $stmt->fetch(PDO::FETCH_ASSOC);
4296 $stmt = $pdo->prepare("SELECT IFNULL(COUNT(`address`), 0) AS `sa_count` FROM `spamalias` WHERE `goto` = :address AND `validity` >= :unixnow");
4297 $stmt->execute(array(':address' => $_data, ':unixnow' => time()));
4298 $SpamaliasUsage = $stmt->fetch(PDO::FETCH_ASSOC);
4299 $mailboxdata['max_new_quota'] = ($DomainQuota['quota'] * 1048576) - $MailboxUsage['in_use'];
4300 $mailboxdata['spam_aliases'] = $SpamaliasUsage['sa_count'];
4301 $mailboxdata['pushover_active'] = ($PushoverActive['pushover_active'] == 1) ? 1 : 0;
4302 if ($mailboxdata['max_new_quota'] > ($DomainQuota['maxquota'] * 1048576)) {
4303 $mailboxdata['max_new_quota'] = ($DomainQuota['maxquota'] * 1048576);
4304 }
4305 if (!empty($rl)) {
4306 $mailboxdata['rl'] = $rl;
4307 $mailboxdata['rl_scope'] = 'mailbox';
4308 }
4309 else {
4310 $mailboxdata['rl'] = ratelimit('get', 'domain', $row['domain']);
4311 $mailboxdata['rl_scope'] = 'domain';
4312 }
4313 $mailboxdata['is_relayed'] = $row['backupmx'];
4314 }
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +01004315 $stmt = $pdo->prepare("SELECT `tag_name`
4316 FROM `tags_mailbox` WHERE `username`= :username");
4317 $stmt->execute(array(
4318 ':username' => $_data
4319 ));
4320 $tags = $stmt->fetchAll(PDO::FETCH_ASSOC);
4321 while ($tag = array_shift($tags)) {
4322 $mailboxdata['tags'][] = $tag['tag_name'];
4323 }
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02004324
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01004325 return $mailboxdata;
4326 break;
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +01004327 case 'mailbox_templates':
4328 if ($_SESSION['mailcow_cc_role'] != "admin" && $_SESSION['mailcow_cc_role'] != "domainadmin") {
4329 return false;
4330 }
4331 $_data = (isset($_data)) ? intval($_data) : null;
4332
4333 if (isset($_data)){
4334 $stmt = $pdo->prepare("SELECT * FROM `templates`
4335 WHERE `id` = :id AND type = :type");
4336 $stmt->execute(array(
4337 ":id" => $_data,
4338 ":type" => "mailbox"
4339 ));
4340 $row = $stmt->fetch(PDO::FETCH_ASSOC);
4341
4342 if (empty($row)){
4343 return false;
4344 }
4345
4346 $row["attributes"] = json_decode($row["attributes"], true);
4347 return $row;
4348 }
4349 else {
4350 $stmt = $pdo->prepare("SELECT * FROM `templates` WHERE `type` = 'mailbox'");
4351 $stmt->execute();
4352 $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
4353
4354 if (empty($rows)){
4355 return false;
4356 }
4357
4358 foreach($rows as $key => $row){
4359 $rows[$key]["attributes"] = json_decode($row["attributes"], true);
4360 }
4361 return $rows;
4362 }
4363 break;
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01004364 case 'resource_details':
4365 $resourcedata = array();
4366 if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
4367 return false;
4368 }
4369 $stmt = $pdo->prepare("SELECT
4370 `username`,
4371 `name`,
4372 `kind`,
4373 `multiple_bookings`,
4374 `local_part`,
4375 `active`,
4376 `domain`
4377 FROM `mailbox` WHERE `kind` REGEXP 'location|thing|group' AND `username` = :resource");
4378 $stmt->execute(array(
4379 ':resource' => $_data,
4380 ));
4381 $row = $stmt->fetch(PDO::FETCH_ASSOC);
4382 $resourcedata['name'] = $row['username'];
4383 $resourcedata['kind'] = $row['kind'];
4384 $resourcedata['multiple_bookings'] = $row['multiple_bookings'];
4385 $resourcedata['description'] = $row['name'];
4386 $resourcedata['active'] = $row['active'];
4387 $resourcedata['active_int'] = $row['active'];
4388 $resourcedata['domain'] = $row['domain'];
4389 $resourcedata['local_part'] = $row['local_part'];
4390 if (!isset($resourcedata['domain']) ||
4391 (isset($resourcedata['domain']) && !hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $resourcedata['domain']))) {
4392 return false;
4393 }
4394 return $resourcedata;
4395 break;
4396 }
4397 break;
4398 case 'delete':
4399 switch ($_type) {
4400 case 'syncjob':
4401 if (!is_array($_data['id'])) {
4402 $ids = array();
4403 $ids[] = $_data['id'];
4404 }
4405 else {
4406 $ids = $_data['id'];
4407 }
4408 if (!isset($_SESSION['acl']['syncjobs']) || $_SESSION['acl']['syncjobs'] != "1" ) {
4409 $_SESSION['return'][] = array(
4410 'type' => 'danger',
4411 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
4412 'msg' => 'access_denied'
4413 );
4414 return false;
4415 }
4416 foreach ($ids as $id) {
4417 if (!is_numeric($id)) {
4418 return false;
4419 }
4420 $stmt = $pdo->prepare("SELECT `user2` FROM `imapsync` WHERE id = :id");
4421 $stmt->execute(array(':id' => $id));
4422 $user2 = $stmt->fetch(PDO::FETCH_ASSOC)['user2'];
4423 if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $user2)) {
4424 $_SESSION['return'][] = array(
4425 'type' => 'danger',
4426 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
4427 'msg' => 'access_denied'
4428 );
4429 continue;
4430 }
4431 $stmt = $pdo->prepare("DELETE FROM `imapsync` WHERE `id`= :id");
4432 $stmt->execute(array(':id' => $id));
4433 $_SESSION['return'][] = array(
4434 'type' => 'success',
4435 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
4436 'msg' => array('deleted_syncjob', $id)
4437 );
4438 }
4439 break;
4440 case 'filter':
4441 if (!is_array($_data['id'])) {
4442 $ids = array();
4443 $ids[] = $_data['id'];
4444 }
4445 else {
4446 $ids = $_data['id'];
4447 }
4448 if (!isset($_SESSION['acl']['filters']) || $_SESSION['acl']['filters'] != "1" ) {
4449 $_SESSION['return'][] = array(
4450 'type' => 'danger',
4451 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
4452 'msg' => 'access_denied'
4453 );
4454 return false;
4455 }
4456 foreach ($ids as $id) {
4457 if (!is_numeric($id)) {
4458 continue;
4459 }
4460 $stmt = $pdo->prepare("SELECT `username` FROM `sieve_filters` WHERE id = :id");
4461 $stmt->execute(array(':id' => $id));
4462 $usr = $stmt->fetch(PDO::FETCH_ASSOC)['username'];
4463 if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $usr)) {
4464 $_SESSION['return'][] = array(
4465 'type' => 'danger',
4466 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
4467 'msg' => 'access_denied'
4468 );
4469 continue;
4470 }
4471 $stmt = $pdo->prepare("DELETE FROM `sieve_filters` WHERE `id`= :id");
4472 $stmt->execute(array(':id' => $id));
4473 $_SESSION['return'][] = array(
4474 'type' => 'success',
4475 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
4476 'msg' => array('delete_filter', $id)
4477 );
4478 }
4479 break;
4480 case 'time_limited_alias':
4481 if (!is_array($_data['address'])) {
4482 $addresses = array();
4483 $addresses[] = $_data['address'];
4484 }
4485 else {
4486 $addresses = $_data['address'];
4487 }
4488 if (!isset($_SESSION['acl']['spam_alias']) || $_SESSION['acl']['spam_alias'] != "1" ) {
4489 $_SESSION['return'][] = array(
4490 'type' => 'danger',
4491 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
4492 'msg' => 'access_denied'
4493 );
4494 return false;
4495 }
4496 foreach ($addresses as $address) {
4497 $stmt = $pdo->prepare("SELECT `goto` FROM `spamalias` WHERE `address` = :address");
4498 $stmt->execute(array(':address' => $address));
4499 $goto = $stmt->fetch(PDO::FETCH_ASSOC)['goto'];
4500 if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $goto)) {
4501 $_SESSION['return'][] = array(
4502 'type' => 'danger',
4503 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
4504 'msg' => 'access_denied'
4505 );
4506 continue;
4507 }
4508 $stmt = $pdo->prepare("DELETE FROM `spamalias` WHERE `goto` = :username AND `address` = :item");
4509 $stmt->execute(array(
4510 ':username' => $goto,
4511 ':item' => $address
4512 ));
4513 $_SESSION['return'][] = array(
4514 'type' => 'success',
4515 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
4516 'msg' => array('mailbox_modified', htmlspecialchars($goto))
4517 );
4518 }
4519 break;
4520 case 'eas_cache':
4521 if (!is_array($_data['username'])) {
4522 $usernames = array();
4523 $usernames[] = $_data['username'];
4524 }
4525 else {
4526 $usernames = $_data['username'];
4527 }
4528 if (!isset($_SESSION['acl']['eas_reset']) || $_SESSION['acl']['eas_reset'] != "1" ) {
4529 $_SESSION['return'][] = array(
4530 'type' => 'danger',
4531 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
4532 'msg' => 'access_denied'
4533 );
4534 return false;
4535 }
4536 foreach ($usernames as $username) {
4537 if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $username)) {
4538 $_SESSION['return'][] = array(
4539 'type' => 'danger',
4540 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
4541 'msg' => 'access_denied'
4542 );
4543 continue;
4544 }
4545 $stmt = $pdo->prepare("DELETE FROM `sogo_cache_folder` WHERE `c_uid` = :username");
4546 $stmt->execute(array(
4547 ':username' => $username
4548 ));
4549 $_SESSION['return'][] = array(
4550 'type' => 'success',
4551 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
4552 'msg' => array('eas_reset', htmlspecialchars($username))
4553 );
4554 }
4555 break;
4556 case 'sogo_profile':
4557 if (!is_array($_data['username'])) {
4558 $usernames = array();
4559 $usernames[] = $_data['username'];
4560 }
4561 else {
4562 $usernames = $_data['username'];
4563 }
4564 if (!isset($_SESSION['acl']['sogo_profile_reset']) || $_SESSION['acl']['sogo_profile_reset'] != "1" ) {
4565 $_SESSION['return'][] = array(
4566 'type' => 'danger',
4567 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
4568 'msg' => 'access_denied'
4569 );
4570 return false;
4571 }
4572 foreach ($usernames as $username) {
4573 if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $username)) {
4574 $_SESSION['return'][] = array(
4575 'type' => 'danger',
4576 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
4577 'msg' => 'access_denied'
4578 );
4579 continue;
4580 }
4581 $stmt = $pdo->prepare("DELETE FROM `sogo_user_profile` WHERE `c_uid` = :username");
4582 $stmt->execute(array(
4583 ':username' => $username
4584 ));
4585 $stmt = $pdo->prepare("DELETE FROM `sogo_cache_folder` WHERE `c_uid` = :username");
4586 $stmt->execute(array(
4587 ':username' => $username
4588 ));
4589 $stmt = $pdo->prepare("DELETE FROM `sogo_acl` WHERE `c_object` LIKE '%/" . $username . "/%' OR `c_uid` = :username");
4590 $stmt->execute(array(
4591 ':username' => $username
4592 ));
4593 $stmt = $pdo->prepare("DELETE FROM `sogo_store` WHERE `c_folder_id` IN (SELECT `c_folder_id` FROM `sogo_folder_info` WHERE `c_path2` = :username)");
4594 $stmt->execute(array(
4595 ':username' => $username
4596 ));
4597 $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)");
4598 $stmt->execute(array(
4599 ':username' => $username
4600 ));
4601 $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)");
4602 $stmt->execute(array(
4603 ':username' => $username
4604 ));
4605 $stmt = $pdo->prepare("DELETE FROM `sogo_folder_info` WHERE `c_path2` = :username");
4606 $stmt->execute(array(
4607 ':username' => $username
4608 ));
4609 $_SESSION['return'][] = array(
4610 'type' => 'success',
4611 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
4612 'msg' => array('sogo_profile_reset', htmlspecialchars($username))
4613 );
4614 }
4615 break;
4616 case 'domain':
4617 if (!is_array($_data['domain'])) {
4618 $domains = array();
4619 $domains[] = $_data['domain'];
4620 }
4621 else {
4622 $domains = $_data['domain'];
4623 }
4624 if ($_SESSION['mailcow_cc_role'] != "admin") {
4625 $_SESSION['return'][] = array(
4626 'type' => 'danger',
4627 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
4628 'msg' => 'access_denied'
4629 );
4630 return false;
4631 }
4632 foreach ($domains as $domain) {
4633 if (!is_valid_domain_name($domain)) {
4634 $_SESSION['return'][] = array(
4635 'type' => 'danger',
4636 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
4637 'msg' => 'domain_invalid'
4638 );
4639 continue;
4640 }
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02004641 $domain = idn_to_ascii(strtolower(trim($domain)), 0, INTL_IDNA_VARIANT_UTS46);
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01004642 $stmt = $pdo->prepare("SELECT `username` FROM `mailbox`
4643 WHERE `domain` = :domain");
4644 $stmt->execute(array(':domain' => $domain));
4645 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
4646 if ($num_results != 0 || !empty($num_results)) {
4647 $_SESSION['return'][] = array(
4648 'type' => 'danger',
4649 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
4650 'msg' => array('domain_not_empty', $domain)
4651 );
4652 continue;
4653 }
4654 $exec_fields = array('cmd' => 'maildir', 'task' => 'cleanup', 'maildir' => $domain);
4655 $maildir_gc = json_decode(docker('post', 'dovecot-mailcow', 'exec', $exec_fields), true);
4656 if ($maildir_gc['type'] != 'success') {
4657 $_SESSION['return'][] = array(
4658 'type' => 'warning',
4659 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
4660 'msg' => 'Could not move mail storage to garbage collector: ' . $maildir_gc['msg']
4661 );
4662 }
4663 $stmt = $pdo->prepare("DELETE FROM `domain` WHERE `domain` = :domain");
4664 $stmt->execute(array(
4665 ':domain' => $domain,
4666 ));
4667 $stmt = $pdo->prepare("DELETE FROM `domain_admins` WHERE `domain` = :domain");
4668 $stmt->execute(array(
4669 ':domain' => $domain,
4670 ));
4671 $stmt = $pdo->prepare("DELETE FROM `alias` WHERE `domain` = :domain");
4672 $stmt->execute(array(
4673 ':domain' => $domain,
4674 ));
4675 $stmt = $pdo->prepare("DELETE FROM `alias_domain` WHERE `target_domain` = :domain");
4676 $stmt->execute(array(
4677 ':domain' => $domain,
4678 ));
4679 $stmt = $pdo->prepare("DELETE FROM `mailbox` WHERE `domain` = :domain");
4680 $stmt->execute(array(
4681 ':domain' => $domain,
4682 ));
4683 $stmt = $pdo->prepare("DELETE FROM `sender_acl` WHERE `logged_in_as` LIKE :domain");
4684 $stmt->execute(array(
4685 ':domain' => '%@'.$domain,
4686 ));
4687 $stmt = $pdo->prepare("DELETE FROM `quota2` WHERE `username` LIKE :domain");
4688 $stmt->execute(array(
4689 ':domain' => '%@'.$domain,
4690 ));
4691 $stmt = $pdo->prepare("DELETE FROM `pushover` WHERE `username` LIKE :domain");
4692 $stmt->execute(array(
4693 ':domain' => '%@'.$domain,
4694 ));
4695 $stmt = $pdo->prepare("DELETE FROM `quota2replica` WHERE `username` LIKE :domain");
4696 $stmt->execute(array(
4697 ':domain' => '%@'.$domain,
4698 ));
4699 $stmt = $pdo->prepare("DELETE FROM `spamalias` WHERE `address` LIKE :domain");
4700 $stmt->execute(array(
4701 ':domain' => '%@'.$domain,
4702 ));
4703 $stmt = $pdo->prepare("DELETE FROM `filterconf` WHERE `object` = :domain");
4704 $stmt->execute(array(
4705 ':domain' => $domain,
4706 ));
4707 $stmt = $pdo->prepare("DELETE FROM `bcc_maps` WHERE `local_dest` = :domain");
4708 $stmt->execute(array(
4709 ':domain' => $domain,
4710 ));
4711 $stmt = $pdo->query("DELETE FROM `admin` WHERE `superadmin` = 0 AND `username` NOT IN (SELECT `username`FROM `domain_admins`);");
4712 $stmt = $pdo->query("DELETE FROM `da_acl` WHERE `username` NOT IN (SELECT `username`FROM `domain_admins`);");
4713 try {
4714 $redis->hDel('DOMAIN_MAP', $domain);
4715 $redis->hDel('RL_VALUE', $domain);
4716 }
4717 catch (RedisException $e) {
4718 $_SESSION['return'][] = array(
4719 'type' => 'danger',
4720 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
4721 'msg' => array('redis_error', $e)
4722 );
4723 continue;
4724 }
4725 $_SESSION['return'][] = array(
4726 'type' => 'success',
4727 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
4728 'msg' => array('domain_removed', htmlspecialchars($domain))
4729 );
4730 }
4731 break;
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +01004732 case 'domain_templates':
4733 if ($_SESSION['mailcow_cc_role'] != "admin") {
4734 $_SESSION['return'][] = array(
4735 'type' => 'danger',
4736 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
4737 'msg' => 'access_denied'
4738 );
4739 return false;
4740 }
4741 if (!is_array($_data['ids'])) {
4742 $ids = array();
4743 $ids[] = $_data['ids'];
4744 }
4745 else {
4746 $ids = $_data['ids'];
4747 }
4748
4749
4750 foreach ($ids as $id) {
4751 // delete template
4752 $stmt = $pdo->prepare("DELETE FROM `templates`
4753 WHERE id = :id AND type = :type AND NOT template = :template");
4754 $stmt->execute(array(
4755 ":id" => $id,
4756 ":type" => "domain",
4757 ":template" => "Default"
4758 ));
4759
4760 $_SESSION['return'][] = array(
4761 'type' => 'success',
4762 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
4763 'msg' => array('template_removed', htmlspecialchars($id))
4764 );
4765 return true;
4766 }
4767 break;
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01004768 case 'alias':
4769 if (!is_array($_data['id'])) {
4770 $ids = array();
4771 $ids[] = $_data['id'];
4772 }
4773 else {
4774 $ids = $_data['id'];
4775 }
4776 foreach ($ids as $id) {
4777 $alias_data = mailbox('get', 'alias_details', $id);
4778 if (empty($alias_data)) {
4779 $_SESSION['return'][] = array(
4780 'type' => 'danger',
4781 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
4782 'msg' => 'access_denied'
4783 );
4784 continue;
4785 }
4786 $stmt = $pdo->prepare("DELETE FROM `alias` WHERE `id` = :id");
4787 $stmt->execute(array(
4788 ':id' => $alias_data['id']
4789 ));
4790 $stmt = $pdo->prepare("DELETE FROM `sender_acl` WHERE `send_as` = :alias_address");
4791 $stmt->execute(array(
4792 ':alias_address' => $alias_data['address']
4793 ));
4794 $_SESSION['return'][] = array(
4795 'type' => 'success',
4796 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
4797 'msg' => array('alias_removed', htmlspecialchars($alias_data['address']))
4798 );
4799 }
4800 break;
4801 case 'alias_domain':
4802 if (!is_array($_data['alias_domain'])) {
4803 $alias_domains = array();
4804 $alias_domains[] = $_data['alias_domain'];
4805 }
4806 else {
4807 $alias_domains = $_data['alias_domain'];
4808 }
4809 foreach ($alias_domains as $alias_domain) {
4810 if (!is_valid_domain_name($alias_domain)) {
4811 $_SESSION['return'][] = array(
4812 'type' => 'danger',
4813 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
4814 'msg' => 'domain_invalid'
4815 );
4816 continue;
4817 }
4818 $stmt = $pdo->prepare("SELECT `target_domain` FROM `alias_domain`
4819 WHERE `alias_domain`= :alias_domain");
4820 $stmt->execute(array(':alias_domain' => $alias_domain));
4821 $DomainData = $stmt->fetch(PDO::FETCH_ASSOC);
4822 if (!hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $DomainData['target_domain'])) {
4823 $_SESSION['return'][] = array(
4824 'type' => 'danger',
4825 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
4826 'msg' => 'access_denied'
4827 );
4828 continue;
4829 }
4830 $stmt = $pdo->prepare("DELETE FROM `alias_domain` WHERE `alias_domain` = :alias_domain");
4831 $stmt->execute(array(
4832 ':alias_domain' => $alias_domain,
4833 ));
4834 $stmt = $pdo->prepare("DELETE FROM `alias` WHERE `domain` = :alias_domain");
4835 $stmt->execute(array(
4836 ':alias_domain' => $alias_domain,
4837 ));
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +01004838 $stmt = $pdo->prepare("DELETE FROM `spamalias` WHERE `address` LIKE :domain");
4839 $stmt->execute(array(
4840 ':domain' => '%@'.$alias_domain,
4841 ));
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01004842 $stmt = $pdo->prepare("DELETE FROM `bcc_maps` WHERE `local_dest` = :alias_domain");
4843 $stmt->execute(array(
4844 ':alias_domain' => $alias_domain,
4845 ));
4846 try {
4847 $redis->hDel('DOMAIN_MAP', $alias_domain);
4848 $redis->hDel('RL_VALUE', $domain);
4849 }
4850 catch (RedisException $e) {
4851 $_SESSION['return'][] = array(
4852 'type' => 'danger',
4853 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
4854 'msg' => array('redis_error', $e)
4855 );
4856 continue;
4857 }
4858 $_SESSION['return'][] = array(
4859 'type' => 'success',
4860 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
4861 'msg' => array('alias_domain_removed', htmlspecialchars($alias_domain))
4862 );
4863 }
4864 break;
4865 case 'mailbox':
4866 if (!is_array($_data['username'])) {
4867 $usernames = array();
4868 $usernames[] = $_data['username'];
4869 }
4870 else {
4871 $usernames = $_data['username'];
4872 }
4873 foreach ($usernames as $username) {
4874 if (!filter_var($username, FILTER_VALIDATE_EMAIL)) {
4875 $_SESSION['return'][] = array(
4876 'type' => 'danger',
4877 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
4878 'msg' => 'access_denied'
4879 );
4880 continue;
4881 }
4882 if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $username)) {
4883 $_SESSION['return'][] = array(
4884 'type' => 'danger',
4885 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
4886 'msg' => 'access_denied'
4887 );
4888 continue;
4889 }
4890 $mailbox_details = mailbox('get', 'mailbox_details', $username);
4891 if (!empty($mailbox_details['domain']) && !empty($mailbox_details['local_part'])) {
4892 $maildir = $mailbox_details['domain'] . '/' . $mailbox_details['local_part'];
4893 $exec_fields = array('cmd' => 'maildir', 'task' => 'cleanup', 'maildir' => $maildir);
4894 $maildir_gc = json_decode(docker('post', 'dovecot-mailcow', 'exec', $exec_fields), true);
4895 if ($maildir_gc['type'] != 'success') {
4896 $_SESSION['return'][] = array(
4897 'type' => 'warning',
4898 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
4899 'msg' => 'Could not move maildir to garbage collector: ' . $maildir_gc['msg']
4900 );
4901 }
4902 }
4903 else {
4904 $_SESSION['return'][] = array(
4905 'type' => 'warning',
4906 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
4907 'msg' => 'Could not move maildir to garbage collector: variables local_part and/or domain empty'
4908 );
4909 }
4910 if (strtolower(getenv('SKIP_SOLR')) == 'n') {
4911 $curl = curl_init();
4912 curl_setopt($curl, CURLOPT_URL, 'http://solr:8983/solr/dovecot-fts/update?commit=true');
4913 curl_setopt($curl, CURLOPT_HTTPHEADER,array('Content-Type: text/xml'));
4914 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
4915 curl_setopt($curl, CURLOPT_POST, 1);
4916 curl_setopt($curl, CURLOPT_POSTFIELDS, '<delete><query>user:' . $username . '</query></delete>');
4917 curl_setopt($curl, CURLOPT_TIMEOUT, 30);
4918 $response = curl_exec($curl);
4919 if ($response === false) {
4920 $err = curl_error($curl);
4921 $_SESSION['return'][] = array(
4922 'type' => 'warning',
4923 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
4924 'msg' => 'Could not remove Solr index: ' . print_r($err, true)
4925 );
4926 }
4927 curl_close($curl);
4928 }
4929 $stmt = $pdo->prepare("DELETE FROM `alias` WHERE `goto` = :username");
4930 $stmt->execute(array(
4931 ':username' => $username
4932 ));
4933 $stmt = $pdo->prepare("DELETE FROM `pushover` WHERE `username` = :username");
4934 $stmt->execute(array(
4935 ':username' => $username
4936 ));
4937 $stmt = $pdo->prepare("DELETE FROM `quarantine` WHERE `rcpt` = :username");
4938 $stmt->execute(array(
4939 ':username' => $username
4940 ));
4941 $stmt = $pdo->prepare("DELETE FROM `quota2` WHERE `username` = :username");
4942 $stmt->execute(array(
4943 ':username' => $username
4944 ));
4945 $stmt = $pdo->prepare("DELETE FROM `quota2replica` WHERE `username` = :username");
4946 $stmt->execute(array(
4947 ':username' => $username
4948 ));
4949 $stmt = $pdo->prepare("DELETE FROM `mailbox` WHERE `username` = :username");
4950 $stmt->execute(array(
4951 ':username' => $username
4952 ));
4953 $stmt = $pdo->prepare("DELETE FROM `sender_acl` WHERE `logged_in_as` = :username");
4954 $stmt->execute(array(
4955 ':username' => $username
4956 ));
4957 // fk, better safe than sorry
4958 $stmt = $pdo->prepare("DELETE FROM `user_acl` WHERE `username` = :username");
4959 $stmt->execute(array(
4960 ':username' => $username
4961 ));
4962 $stmt = $pdo->prepare("DELETE FROM `spamalias` WHERE `goto` = :username");
4963 $stmt->execute(array(
4964 ':username' => $username
4965 ));
4966 $stmt = $pdo->prepare("DELETE FROM `imapsync` WHERE `user2` = :username");
4967 $stmt->execute(array(
4968 ':username' => $username
4969 ));
4970 $stmt = $pdo->prepare("DELETE FROM `filterconf` WHERE `object` = :username");
4971 $stmt->execute(array(
4972 ':username' => $username
4973 ));
4974 $stmt = $pdo->prepare("DELETE FROM `sogo_user_profile` WHERE `c_uid` = :username");
4975 $stmt->execute(array(
4976 ':username' => $username
4977 ));
4978 $stmt = $pdo->prepare("DELETE FROM `sogo_cache_folder` WHERE `c_uid` = :username");
4979 $stmt->execute(array(
4980 ':username' => $username
4981 ));
4982 $stmt = $pdo->prepare("DELETE FROM `sogo_acl` WHERE `c_object` LIKE '%/" . str_replace('%', '\%', $username) . "/%' OR `c_uid` = :username");
4983 $stmt->execute(array(
4984 ':username' => $username
4985 ));
4986 $stmt = $pdo->prepare("DELETE FROM `sogo_store` WHERE `c_folder_id` IN (SELECT `c_folder_id` FROM `sogo_folder_info` WHERE `c_path2` = :username)");
4987 $stmt->execute(array(
4988 ':username' => $username
4989 ));
4990 $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)");
4991 $stmt->execute(array(
4992 ':username' => $username
4993 ));
4994 $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)");
4995 $stmt->execute(array(
4996 ':username' => $username
4997 ));
4998 $stmt = $pdo->prepare("DELETE FROM `sogo_folder_info` WHERE `c_path2` = :username");
4999 $stmt->execute(array(
5000 ':username' => $username
5001 ));
5002 $stmt = $pdo->prepare("DELETE FROM `bcc_maps` WHERE `local_dest` = :username");
5003 $stmt->execute(array(
5004 ':username' => $username
5005 ));
5006 $stmt = $pdo->prepare("DELETE FROM `oauth_access_tokens` WHERE `user_id` = :username");
5007 $stmt->execute(array(
5008 ':username' => $username
5009 ));
5010 $stmt = $pdo->prepare("DELETE FROM `oauth_refresh_tokens` WHERE `user_id` = :username");
5011 $stmt->execute(array(
5012 ':username' => $username
5013 ));
5014 $stmt = $pdo->prepare("DELETE FROM `oauth_authorization_codes` WHERE `user_id` = :username");
5015 $stmt->execute(array(
5016 ':username' => $username
5017 ));
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02005018 $stmt = $pdo->prepare("DELETE FROM `tfa` WHERE `username` = :username");
5019 $stmt->execute(array(
5020 ':username' => $username,
5021 ));
5022 $stmt = $pdo->prepare("DELETE FROM `fido2` WHERE `username` = :username");
5023 $stmt->execute(array(
5024 ':username' => $username,
5025 ));
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01005026 $stmt = $pdo->prepare("SELECT `address`, `goto` FROM `alias`
5027 WHERE `goto` REGEXP :username");
5028 $stmt->execute(array(':username' => '(^|,)'.$username.'($|,)'));
5029 $GotoData = $stmt->fetchAll(PDO::FETCH_ASSOC);
5030 foreach ($GotoData as $gotos) {
5031 $goto_exploded = explode(',', $gotos['goto']);
5032 if (($key = array_search($username, $goto_exploded)) !== false) {
5033 unset($goto_exploded[$key]);
5034 }
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02005035 $gotos_rebuild = implode(',', (array)$goto_exploded);
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01005036 $stmt = $pdo->prepare("UPDATE `alias` SET
5037 `goto` = :goto
5038 WHERE `address` = :address");
5039 $stmt->execute(array(
5040 ':goto' => $gotos_rebuild,
5041 ':address' => $gotos['address']
5042 ));
5043 }
5044 try {
5045 $redis->hDel('RL_VALUE', $username);
5046 }
5047 catch (RedisException $e) {
5048 $_SESSION['return'][] = array(
5049 'type' => 'danger',
5050 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
5051 'msg' => array('redis_error', $e)
5052 );
5053 continue;
5054 }
5055 $_SESSION['return'][] = array(
5056 'type' => 'success',
5057 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
5058 'msg' => array('mailbox_removed', htmlspecialchars($username))
5059 );
5060 }
5061 break;
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +01005062 case 'mailbox_templates':
5063 if ($_SESSION['mailcow_cc_role'] != "admin") {
5064 $_SESSION['return'][] = array(
5065 'type' => 'danger',
5066 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
5067 'msg' => 'access_denied'
5068 );
5069 return false;
5070 }
5071 if (!is_array($_data['ids'])) {
5072 $ids = array();
5073 $ids[] = $_data['ids'];
5074 }
5075 else {
5076 $ids = $_data['ids'];
5077 }
5078
5079
5080 foreach ($ids as $id) {
5081 // delete template
5082 $stmt = $pdo->prepare("DELETE FROM `templates`
5083 WHERE id = :id AND type = :type AND NOT template = :template");
5084 $stmt->execute(array(
5085 ":id" => $id,
5086 ":type" => "mailbox",
5087 ":template" => "Default"
5088 ));
5089 }
5090
5091 $_SESSION['return'][] = array(
5092 'type' => 'success',
5093 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
5094 'msg' => 'template_removed'
5095 );
5096 return true;
5097 break;
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01005098 case 'resource':
5099 if (!is_array($_data['name'])) {
5100 $names = array();
5101 $names[] = $_data['name'];
5102 }
5103 else {
5104 $names = $_data['name'];
5105 }
5106 foreach ($names as $name) {
5107 if (!filter_var($name, FILTER_VALIDATE_EMAIL)) {
5108 $_SESSION['return'][] = array(
5109 'type' => 'danger',
5110 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
5111 'msg' => 'access_denied'
5112 );
5113 continue;
5114 }
5115 if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $name)) {
5116 $_SESSION['return'][] = array(
5117 'type' => 'danger',
5118 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
5119 'msg' => 'access_denied'
5120 );
5121 continue;
5122 }
5123 $stmt = $pdo->prepare("DELETE FROM `mailbox` WHERE `username` = :username");
5124 $stmt->execute(array(
5125 ':username' => $name
5126 ));
5127 $stmt = $pdo->prepare("DELETE FROM `sogo_user_profile` WHERE `c_uid` = :username");
5128 $stmt->execute(array(
5129 ':username' => $name
5130 ));
5131 $stmt = $pdo->prepare("DELETE FROM `sogo_cache_folder` WHERE `c_uid` = :username");
5132 $stmt->execute(array(
5133 ':username' => $name
5134 ));
5135 $stmt = $pdo->prepare("DELETE FROM `sogo_acl` WHERE `c_object` LIKE '%/" . $name . "/%' OR `c_uid` = :username");
5136 $stmt->execute(array(
5137 ':username' => $name
5138 ));
5139 $stmt = $pdo->prepare("DELETE FROM `sogo_store` WHERE `c_folder_id` IN (SELECT `c_folder_id` FROM `sogo_folder_info` WHERE `c_path2` = :username)");
5140 $stmt->execute(array(
5141 ':username' => $name
5142 ));
5143 $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)");
5144 $stmt->execute(array(
5145 ':username' => $name
5146 ));
5147 $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)");
5148 $stmt->execute(array(
5149 ':username' => $name
5150 ));
5151 $stmt = $pdo->prepare("DELETE FROM `sogo_folder_info` WHERE `c_path2` = :username");
5152 $stmt->execute(array(
5153 ':username' => $name
5154 ));
5155 $_SESSION['return'][] = array(
5156 'type' => 'success',
5157 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
5158 'msg' => array('resource_removed', htmlspecialchars($name))
5159 );
5160 }
5161 break;
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +01005162 case 'tags_domain':
5163 if (!is_array($_data['domain'])) {
5164 $domains = array();
5165 $domains[] = $_data['domain'];
5166 }
5167 else {
5168 $domains = $_data['domain'];
5169 }
5170 $tags = $_data['tags'];
5171 if (!is_array($tags)) $tags = array();
5172
5173
5174 if ($_SESSION['mailcow_cc_role'] != "admin") {
5175 $_SESSION['return'][] = array(
5176 'type' => 'danger',
5177 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
5178 'msg' => 'access_denied'
5179 );
5180 return false;
5181 }
5182
5183 $wasModified = false;
5184 foreach ($domains as $domain) {
5185 if (!is_valid_domain_name($domain)) {
5186 $_SESSION['return'][] = array(
5187 'type' => 'danger',
5188 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
5189 'msg' => 'domain_invalid'
5190 );
5191 continue;
5192 }
5193
5194 foreach($tags as $tag){
5195 // delete tag
5196 $wasModified = true;
5197 $stmt = $pdo->prepare("DELETE FROM `tags_domain` WHERE `domain` = :domain AND `tag_name` = :tag_name");
5198 $stmt->execute(array(
5199 ':domain' => $domain,
5200 ':tag_name' => $tag,
5201 ));
5202 }
5203 }
5204
5205 if (!$wasModified) return false;
5206 $_SESSION['return'][] = array(
5207 'type' => 'success',
5208 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
5209 'msg' => array('domain_modified', $domain)
5210 );
5211 break;
5212 case 'tags_mailbox':
5213 if (!is_array($_data['username'])) {
5214 $usernames = array();
5215 $usernames[] = $_data['username'];
5216 }
5217 else {
5218 $usernames = $_data['username'];
5219 }
5220 $tags = $_data['tags'];
5221 if (!is_array($tags)) $tags = array();
5222
5223 $wasModified = false;
5224 foreach ($usernames as $username) {
5225 if (!filter_var($username, FILTER_VALIDATE_EMAIL)) {
5226 $_SESSION['return'][] = array(
5227 'type' => 'danger',
5228 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
5229 'msg' => 'email invalid'
5230 );
5231 continue;
5232 }
5233
5234 $is_now = mailbox('get', 'mailbox_details', $username);
5235 $domain = $is_now['domain'];
5236 if (!hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $domain)) {
5237 $_SESSION['return'][] = array(
5238 'type' => 'danger',
5239 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
5240 'msg' => 'access_denied'
5241 );
5242 continue;
5243 }
5244
5245 // delete tags
5246 foreach($tags as $tag){
5247 $wasModified = true;
5248
5249 $stmt = $pdo->prepare("DELETE FROM `tags_mailbox` WHERE `username` = :username AND `tag_name` = :tag_name");
5250 $stmt->execute(array(
5251 ':username' => $username,
5252 ':tag_name' => $tag,
5253 ));
5254 }
5255 }
5256
5257 if (!$wasModified) return false;
5258 $_SESSION['return'][] = array(
5259 'type' => 'success',
5260 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
5261 'msg' => array('mailbox_modified', $username)
5262 );
5263 break;
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01005264 }
5265 break;
5266 }
5267 if ($_action != 'get' && in_array($_type, array('domain', 'alias', 'alias_domain', 'mailbox', 'resource'))) {
5268 update_sogo_static_view();
5269 }
5270}