blob: 24e5dabff5d5ef62a41c5f8081f08f346ecc7052 [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']);
339 // Workaround, fixme
340 if (strpos($custom_params, 'pipemess')) {
341 $custom_params = '';
342 }
343 if (empty($subfolder2)) {
344 $subfolder2 = "";
345 }
346 if (!isset($maxage) || !filter_var($maxage, FILTER_VALIDATE_INT, array('options' => array('min_range' => 1, 'max_range' => 32000)))) {
347 $maxage = "0";
348 }
349 if (!isset($timeout1) || !filter_var($timeout1, FILTER_VALIDATE_INT, array('options' => array('min_range' => 1, 'max_range' => 32000)))) {
350 $timeout1 = "600";
351 }
352 if (!isset($timeout2) || !filter_var($timeout2, FILTER_VALIDATE_INT, array('options' => array('min_range' => 1, 'max_range' => 32000)))) {
353 $timeout2 = "600";
354 }
355 if (!isset($maxbytespersecond) || !filter_var($maxbytespersecond, FILTER_VALIDATE_INT, array('options' => array('min_range' => 1, 'max_range' => 125000000)))) {
356 $maxbytespersecond = "0";
357 }
358 if (!filter_var($port1, FILTER_VALIDATE_INT, array('options' => array('min_range' => 1, 'max_range' => 65535)))) {
359 $_SESSION['return'][] = array(
360 'type' => 'danger',
361 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
362 'msg' => 'access_denied'
363 );
364 return false;
365 }
366 if (!filter_var($mins_interval, FILTER_VALIDATE_INT, array('options' => array('min_range' => 1, 'max_range' => 43800)))) {
367 $_SESSION['return'][] = array(
368 'type' => 'danger',
369 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
370 'msg' => 'access_denied'
371 );
372 return false;
373 }
374 // if (!is_valid_domain_name($host1)) {
375 // $_SESSION['return'][] = array(
376 // 'type' => 'danger',
377 // 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
378 // 'msg' => 'access_denied'
379 // );
380 // return false;
381 // }
382 if ($enc1 != "TLS" && $enc1 != "SSL" && $enc1 != "PLAIN") {
383 $_SESSION['return'][] = array(
384 'type' => 'danger',
385 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
386 'msg' => 'access_denied'
387 );
388 return false;
389 }
390 if (@preg_match("/" . $exclude . "/", null) === false) {
391 $_SESSION['return'][] = array(
392 'type' => 'danger',
393 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
394 'msg' => 'access_denied'
395 );
396 return false;
397 }
398 $stmt = $pdo->prepare("SELECT '1' FROM `imapsync`
399 WHERE `user2` = :user2 AND `user1` = :user1 AND `host1` = :host1");
400 $stmt->execute(array(':user1' => $user1, ':user2' => $username, ':host1' => $host1));
401 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
402 if ($num_results != 0) {
403 $_SESSION['return'][] = array(
404 'type' => 'danger',
405 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
406 'msg' => array('object_exists', htmlspecialchars($host1 . ' / ' . $user1))
407 );
408 return false;
409 }
410 $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`)
411 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)");
412 $stmt->execute(array(
413 ':user2' => $username,
414 ':custom_params' => $custom_params,
415 ':exclude' => $exclude,
416 ':maxage' => $maxage,
417 ':delete1' => $delete1,
418 ':delete2' => $delete2,
419 ':timeout1' => $timeout1,
420 ':timeout2' => $timeout2,
421 ':automap' => $automap,
422 ':skipcrossduplicates' => $skipcrossduplicates,
423 ':maxbytespersecond' => $maxbytespersecond,
424 ':subscribeall' => $subscribeall,
425 ':subfolder2' => $subfolder2,
426 ':host1' => $host1,
427 ':authmech1' => 'PLAIN',
428 ':user1' => $user1,
429 ':password1' => $password1,
430 ':mins_interval' => $mins_interval,
431 ':port1' => $port1,
432 ':enc1' => $enc1,
433 ':delete2duplicates' => $delete2duplicates,
434 ':active' => $active,
435 ));
436 $_SESSION['return'][] = array(
437 'type' => 'success',
438 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
439 'msg' => array('mailbox_modified', $username)
440 );
441 break;
442 case 'domain':
443 if ($_SESSION['mailcow_cc_role'] != "admin") {
444 $_SESSION['return'][] = array(
445 'type' => 'danger',
446 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
447 'msg' => 'access_denied'
448 );
449 return false;
450 }
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +0200451 $domain = idn_to_ascii(strtolower(trim($_data['domain'])), 0, INTL_IDNA_VARIANT_UTS46);
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +0100452 $description = $_data['description'];
453 if (empty($description)) {
454 $description = $domain;
455 }
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +0200456 $aliases = (int)$_data['aliases'];
457 $mailboxes = (int)$_data['mailboxes'];
458 $defquota = (int)$_data['defquota'];
459 $maxquota = (int)$_data['maxquota'];
460 $restart_sogo = (int)$_data['restart_sogo'];
461 $quota = (int)$_data['quota'];
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +0100462 if ($defquota > $maxquota) {
463 $_SESSION['return'][] = array(
464 'type' => 'danger',
465 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
466 'msg' => 'mailbox_defquota_exceeds_mailbox_maxquota'
467 );
468 return false;
469 }
470 if ($maxquota > $quota) {
471 $_SESSION['return'][] = array(
472 'type' => 'danger',
473 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
474 'msg' => 'mailbox_quota_exceeds_domain_quota'
475 );
476 return false;
477 }
478 if ($defquota == "0" || empty($defquota)) {
479 $_SESSION['return'][] = array(
480 'type' => 'danger',
481 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
482 'msg' => 'defquota_empty'
483 );
484 return false;
485 }
486 if ($maxquota == "0" || empty($maxquota)) {
487 $_SESSION['return'][] = array(
488 'type' => 'danger',
489 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
490 'msg' => 'maxquota_empty'
491 );
492 return false;
493 }
494 $active = intval($_data['active']);
495 $relay_all_recipients = intval($_data['relay_all_recipients']);
496 $relay_unknown_only = intval($_data['relay_unknown_only']);
497 $backupmx = intval($_data['backupmx']);
498 $gal = intval($_data['gal']);
499 if ($relay_all_recipients == 1) {
500 $backupmx = '1';
501 }
502 if ($relay_unknown_only == 1) {
503 $backupmx = 1;
504 $relay_all_recipients = 1;
505 }
506 if (!is_valid_domain_name($domain)) {
507 $_SESSION['return'][] = array(
508 'type' => 'danger',
509 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
510 'msg' => 'domain_invalid'
511 );
512 return false;
513 }
514 foreach (array($quota, $maxquota, $mailboxes, $aliases) as $data) {
515 if (!is_numeric($data)) {
516 $_SESSION['return'][] = array(
517 'type' => 'danger',
518 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
519 'msg' => array('object_is_not_numeric', htmlspecialchars($data))
520 );
521 return false;
522 }
523 }
524 $stmt = $pdo->prepare("SELECT `domain` FROM `domain`
525 WHERE `domain` = :domain");
526 $stmt->execute(array(':domain' => $domain));
527 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
528 $stmt = $pdo->prepare("SELECT `alias_domain` FROM `alias_domain`
529 WHERE `alias_domain` = :domain");
530 $stmt->execute(array(':domain' => $domain));
531 $num_results = $num_results + count($stmt->fetchAll(PDO::FETCH_ASSOC));
532 if ($num_results != 0) {
533 $_SESSION['return'][] = array(
534 'type' => 'danger',
535 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
536 'msg' => array('domain_exists', htmlspecialchars($domain))
537 );
538 return false;
539 }
540 if ($domain == getenv('MAILCOW_HOSTNAME')) {
541 $_SESSION['return'][] = array(
542 'type' => 'danger',
543 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
544 'msg' => 'domain_cannot_match_hostname'
545 );
546 return false;
547 }
548 $stmt = $pdo->prepare("DELETE FROM `sender_acl` WHERE `external` = 1 AND `send_as` LIKE :domain");
549 $stmt->execute(array(
550 ':domain' => '%@' . $domain
551 ));
552 $stmt = $pdo->prepare("INSERT INTO `domain` (`domain`, `description`, `aliases`, `mailboxes`, `defquota`, `maxquota`, `quota`, `backupmx`, `gal`, `active`, `relay_unknown_only`, `relay_all_recipients`)
553 VALUES (:domain, :description, :aliases, :mailboxes, :defquota, :maxquota, :quota, :backupmx, :gal, :active, :relay_unknown_only, :relay_all_recipients)");
554 $stmt->execute(array(
555 ':domain' => $domain,
556 ':description' => $description,
557 ':aliases' => $aliases,
558 ':mailboxes' => $mailboxes,
559 ':defquota' => $defquota,
560 ':maxquota' => $maxquota,
561 ':quota' => $quota,
562 ':backupmx' => $backupmx,
563 ':gal' => $gal,
564 ':active' => $active,
565 ':relay_unknown_only' => $relay_unknown_only,
566 ':relay_all_recipients' => $relay_all_recipients
567 ));
568 try {
569 $redis->hSet('DOMAIN_MAP', $domain, 1);
570 }
571 catch (RedisException $e) {
572 $_SESSION['return'][] = array(
573 'type' => 'danger',
574 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
575 'msg' => array('redis_error', $e)
576 );
577 return false;
578 }
579 if (!empty(intval($_data['rl_value']))) {
580 ratelimit('edit', 'domain', array('rl_value' => $_data['rl_value'], 'rl_frame' => $_data['rl_frame'], 'object' => $domain));
581 }
582 if (!empty($restart_sogo)) {
583 $restart_response = json_decode(docker('post', 'sogo-mailcow', 'restart'), true);
584 if ($restart_response['type'] == "success") {
585 $_SESSION['return'][] = array(
586 'type' => 'success',
587 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
588 'msg' => array('domain_added', htmlspecialchars($domain))
589 );
590 return true;
591 }
592 else {
593 $_SESSION['return'][] = array(
594 'type' => 'warning',
595 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
596 'msg' => 'domain_added_sogo_failed'
597 );
598 return false;
599 }
600 }
601 $_SESSION['return'][] = array(
602 'type' => 'success',
603 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
604 'msg' => array('domain_added', htmlspecialchars($domain))
605 );
606 return true;
607 break;
608 case 'alias':
609 $addresses = array_map('trim', preg_split( "/( |,|;|\n)/", $_data['address']));
610 $gotos = array_map('trim', preg_split( "/( |,|;|\n)/", $_data['goto']));
611 $active = intval($_data['active']);
612 $sogo_visible = intval($_data['sogo_visible']);
613 $goto_null = intval($_data['goto_null']);
614 $goto_spam = intval($_data['goto_spam']);
615 $goto_ham = intval($_data['goto_ham']);
616 $private_comment = $_data['private_comment'];
617 $public_comment = $_data['public_comment'];
618 if (strlen($private_comment) > 160 | strlen($public_comment) > 160){
619 $_SESSION['return'][] = array(
620 'type' => 'danger',
621 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
622 'msg' => 'comment_too_long'
623 );
624 return false;
625 }
626 if (empty($addresses[0])) {
627 $_SESSION['return'][] = array(
628 'type' => 'danger',
629 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
630 'msg' => 'alias_empty'
631 );
632 return false;
633 }
634 if (empty($gotos[0]) && ($goto_null + $goto_spam + $goto_ham == 0)) {
635 $_SESSION['return'][] = array(
636 'type' => 'danger',
637 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
638 'msg' => 'goto_empty'
639 );
640 return false;
641 }
642 if ($goto_null == "1") {
643 $goto = "null@localhost";
644 }
645 elseif ($goto_spam == "1") {
646 $goto = "spam@localhost";
647 }
648 elseif ($goto_ham == "1") {
649 $goto = "ham@localhost";
650 }
651 else {
652 foreach ($gotos as $i => &$goto) {
653 if (empty($goto)) {
654 continue;
655 }
656 $goto_domain = idn_to_ascii(substr(strstr($goto, '@'), 1), 0, INTL_IDNA_VARIANT_UTS46);
657 $goto_local_part = strstr($goto, '@', true);
658 $goto = $goto_local_part.'@'.$goto_domain;
659 $stmt = $pdo->prepare("SELECT `username` FROM `mailbox`
660 WHERE `kind` REGEXP 'location|thing|group'
661 AND `username`= :goto");
662 $stmt->execute(array(':goto' => $goto));
663 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
664 if ($num_results != 0) {
665 $_SESSION['return'][] = array(
666 'type' => 'danger',
667 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
668 'msg' => array('goto_invalid', htmlspecialchars($goto))
669 );
670 unset($gotos[$i]);
671 continue;
672 }
673 if (!filter_var($goto, FILTER_VALIDATE_EMAIL) === true) {
674 $_SESSION['return'][] = array(
675 'type' => 'danger',
676 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
677 'msg' => array('goto_invalid', htmlspecialchars($goto))
678 );
679 unset($gotos[$i]);
680 continue;
681 }
682 }
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +0200683 $gotos = array_unique($gotos);
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +0100684 $gotos = array_filter($gotos);
685 if (empty($gotos)) { return false; }
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +0200686 $goto = implode(",", (array)$gotos);
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +0100687 }
688 foreach ($addresses as $address) {
689 if (empty($address)) {
690 continue;
691 }
692 if (in_array($address, $gotos)) {
693 continue;
694 }
695 $domain = idn_to_ascii(substr(strstr($address, '@'), 1), 0, INTL_IDNA_VARIANT_UTS46);
696 $local_part = strstr($address, '@', true);
697 $address = $local_part.'@'.$domain;
698 $domaindata = mailbox('get', 'domain_details', $domain);
699 if (is_array($domaindata) && $domaindata['aliases_left'] == "0") {
700 $_SESSION['return'][] = array(
701 'type' => 'danger',
702 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
703 'msg' => 'max_alias_exceeded'
704 );
705 return false;
706 }
707 $stmt = $pdo->prepare("SELECT `address` FROM `alias`
708 WHERE `address`= :address OR `address` IN (
709 SELECT `username` FROM `mailbox`, `alias_domain`
710 WHERE (
711 `alias_domain`.`alias_domain` = :address_d
712 AND `mailbox`.`username` = CONCAT(:address_l, '@', alias_domain.target_domain)))");
713 $stmt->execute(array(
714 ':address' => $address,
715 ':address_l' => $local_part,
716 ':address_d' => $domain
717 ));
718 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
719 if ($num_results != 0) {
720 $_SESSION['return'][] = array(
721 'type' => 'danger',
722 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
723 'msg' => array('is_alias_or_mailbox', htmlspecialchars($address))
724 );
725 continue;
726 }
727 $stmt = $pdo->prepare("SELECT `domain` FROM `domain`
728 WHERE `domain`= :domain1 OR `domain` = (SELECT `target_domain` FROM `alias_domain` WHERE `alias_domain` = :domain2)");
729 $stmt->execute(array(':domain1' => $domain, ':domain2' => $domain));
730 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
731 if ($num_results == 0) {
732 $_SESSION['return'][] = array(
733 'type' => 'danger',
734 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
735 'msg' => array('domain_not_found', htmlspecialchars($domain))
736 );
737 continue;
738 }
739 $stmt = $pdo->prepare("SELECT `address` FROM `spamalias`
740 WHERE `address`= :address");
741 $stmt->execute(array(':address' => $address));
742 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
743 if ($num_results != 0) {
744 $_SESSION['return'][] = array(
745 'type' => 'danger',
746 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
747 'msg' => array('is_spam_alias', htmlspecialchars($address))
748 );
749 continue;
750 }
751 if ((!filter_var($address, FILTER_VALIDATE_EMAIL) === true) && !empty($local_part)) {
752 $_SESSION['return'][] = array(
753 'type' => 'danger',
754 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
755 'msg' => array('alias_invalid', $address)
756 );
757 continue;
758 }
759 if (!hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $domain)) {
760 $_SESSION['return'][] = array(
761 'type' => 'danger',
762 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
763 'msg' => 'access_denied'
764 );
765 continue;
766 }
767 $stmt = $pdo->prepare("INSERT INTO `alias` (`address`, `public_comment`, `private_comment`, `goto`, `domain`, `sogo_visible`, `active`)
768 VALUES (:address, :public_comment, :private_comment, :goto, :domain, :sogo_visible, :active)");
769 if (!filter_var($address, FILTER_VALIDATE_EMAIL) === true) {
770 $stmt->execute(array(
771 ':address' => '@'.$domain,
772 ':public_comment' => $public_comment,
773 ':private_comment' => $private_comment,
774 ':address' => '@'.$domain,
775 ':goto' => $goto,
776 ':domain' => $domain,
777 ':sogo_visible' => $sogo_visible,
778 ':active' => $active
779 ));
780 }
781 else {
782 $stmt->execute(array(
783 ':address' => $address,
784 ':public_comment' => $public_comment,
785 ':private_comment' => $private_comment,
786 ':goto' => $goto,
787 ':domain' => $domain,
788 ':sogo_visible' => $sogo_visible,
789 ':active' => $active
790 ));
791 }
792 $id = $pdo->lastInsertId();
793 $_SESSION['return'][] = array(
794 'type' => 'success',
795 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
796 'msg' => array('alias_added', $address, $id)
797 );
798 }
799 break;
800 case 'alias_domain':
801 $active = intval($_data['active']);
802 $alias_domains = array_map('trim', preg_split( "/( |,|;|\n)/", $_data['alias_domain']));
803 $alias_domains = array_filter($alias_domains);
804 $target_domain = idn_to_ascii(strtolower(trim($_data['target_domain'])), 0, INTL_IDNA_VARIANT_UTS46);
805 if (!isset($_SESSION['acl']['alias_domains']) || $_SESSION['acl']['alias_domains'] != "1" ) {
806 $_SESSION['return'][] = array(
807 'type' => 'danger',
808 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
809 'msg' => 'access_denied'
810 );
811 return false;
812 }
813 if (!is_valid_domain_name($target_domain)) {
814 $_SESSION['return'][] = array(
815 'type' => 'danger',
816 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
817 'msg' => 'target_domain_invalid'
818 );
819 return false;
820 }
821 if (!hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $target_domain)) {
822 $_SESSION['return'][] = array(
823 'type' => 'danger',
824 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
825 'msg' => 'access_denied'
826 );
827 return false;
828 }
829 foreach ($alias_domains as $i => $alias_domain) {
830 $alias_domain = idn_to_ascii(strtolower(trim($alias_domain)), 0, INTL_IDNA_VARIANT_UTS46);
831 if (!is_valid_domain_name($alias_domain)) {
832 $_SESSION['return'][] = array(
833 'type' => 'danger',
834 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
835 'msg' => array('alias_domain_invalid', htmlspecialchars(alias_domain))
836 );
837 continue;
838 }
839 if ($alias_domain == $target_domain) {
840 $_SESSION['return'][] = array(
841 'type' => 'danger',
842 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
843 'msg' => array('aliasd_targetd_identical', htmlspecialchars($target_domain))
844 );
845 continue;
846 }
847 $stmt = $pdo->prepare("SELECT `domain` FROM `domain`
848 WHERE `domain`= :target_domain");
849 $stmt->execute(array(':target_domain' => $target_domain));
850 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
851 if ($num_results == 0) {
852 $_SESSION['return'][] = array(
853 'type' => 'danger',
854 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
855 'msg' => array('targetd_not_found', htmlspecialchars($target_domain))
856 );
857 continue;
858 }
859 $stmt = $pdo->prepare("SELECT `domain` FROM `domain`
860 WHERE `domain`= :target_domain AND `backupmx` = '1'");
861 $stmt->execute(array(':target_domain' => $target_domain));
862 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
863 if ($num_results == 1) {
864 $_SESSION['return'][] = array(
865 'type' => 'danger',
866 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
867 'msg' => array('targetd_relay_domain', htmlspecialchars($target_domain))
868 );
869 continue;
870 }
871 $stmt = $pdo->prepare("SELECT `alias_domain` FROM `alias_domain` WHERE `alias_domain`= :alias_domain
872 UNION
873 SELECT `domain` FROM `domain` WHERE `domain`= :alias_domain_in_domain");
874 $stmt->execute(array(':alias_domain' => $alias_domain, ':alias_domain_in_domain' => $alias_domain));
875 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
876 if ($num_results != 0) {
877 $_SESSION['return'][] = array(
878 'type' => 'danger',
879 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
880 'msg' => array('alias_domain_invalid', $alias_domain)
881 );
882 continue;
883 }
884 $stmt = $pdo->prepare("DELETE FROM `sender_acl` WHERE `external` = 1 AND `send_as` LIKE :domain");
885 $stmt->execute(array(
886 ':domain' => '%@' . $domain
887 ));
888 $stmt = $pdo->prepare("INSERT INTO `alias_domain` (`alias_domain`, `target_domain`, `active`)
889 VALUES (:alias_domain, :target_domain, :active)");
890 $stmt->execute(array(
891 ':alias_domain' => $alias_domain,
892 ':target_domain' => $target_domain,
893 ':active' => $active
894 ));
895 try {
896 $redis->hSet('DOMAIN_MAP', $alias_domain, 1);
897 }
898 catch (RedisException $e) {
899 $_SESSION['return'][] = array(
900 'type' => 'danger',
901 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
902 'msg' => array('redis_error', $e)
903 );
904 return false;
905 }
906 if (!empty(intval($_data['rl_value']))) {
907 ratelimit('edit', 'domain', array('rl_value' => $_data['rl_value'], 'rl_frame' => $_data['rl_frame'], 'object' => $alias_domain));
908 }
909 $_SESSION['return'][] = array(
910 'type' => 'success',
911 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
912 'msg' => array('aliasd_added', htmlspecialchars($alias_domain))
913 );
914 }
915 break;
916 case 'mailbox':
917 $local_part = strtolower(trim($_data['local_part']));
918 $domain = idn_to_ascii(strtolower(trim($_data['domain'])), 0, INTL_IDNA_VARIANT_UTS46);
919 $username = $local_part . '@' . $domain;
920 if (!filter_var($username, FILTER_VALIDATE_EMAIL)) {
921 $_SESSION['return'][] = array(
922 'type' => 'danger',
923 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
924 'msg' => 'mailbox_invalid'
925 );
926 return false;
927 }
928 if (empty($_data['local_part'])) {
929 $_SESSION['return'][] = array(
930 'type' => 'danger',
931 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
932 'msg' => 'mailbox_invalid'
933 );
934 return false;
935 }
936 $password = $_data['password'];
937 $password2 = $_data['password2'];
938 $name = ltrim(rtrim($_data['name'], '>'), '<');
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +0200939 $quota_m = intval($_data['quota']);
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +0100940 if ((!isset($_SESSION['acl']['unlimited_quota']) || $_SESSION['acl']['unlimited_quota'] != "1") && $quota_m === 0) {
941 $_SESSION['return'][] = array(
942 'type' => 'danger',
943 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
944 'msg' => 'unlimited_quota_acl'
945 );
946 return false;
947 }
948 if (empty($name)) {
949 $name = $local_part;
950 }
951 $active = intval($_data['active']);
952 $force_pw_update = (isset($_data['force_pw_update'])) ? intval($_data['force_pw_update']) : intval($MAILBOX_DEFAULT_ATTRIBUTES['force_pw_update']);
953 $tls_enforce_in = (isset($_data['tls_enforce_in'])) ? intval($_data['tls_enforce_in']) : intval($MAILBOX_DEFAULT_ATTRIBUTES['tls_enforce_in']);
954 $tls_enforce_out = (isset($_data['tls_enforce_out'])) ? intval($_data['tls_enforce_out']) : intval($MAILBOX_DEFAULT_ATTRIBUTES['tls_enforce_out']);
955 $sogo_access = (isset($_data['sogo_access'])) ? intval($_data['sogo_access']) : intval($MAILBOX_DEFAULT_ATTRIBUTES['sogo_access']);
956 $imap_access = (isset($_data['imap_access'])) ? intval($_data['imap_access']) : intval($MAILBOX_DEFAULT_ATTRIBUTES['imap_access']);
957 $pop3_access = (isset($_data['pop3_access'])) ? intval($_data['pop3_access']) : intval($MAILBOX_DEFAULT_ATTRIBUTES['pop3_access']);
958 $smtp_access = (isset($_data['smtp_access'])) ? intval($_data['smtp_access']) : intval($MAILBOX_DEFAULT_ATTRIBUTES['smtp_access']);
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +0200959 $relayhost = (isset($_data['relayhost'])) ? intval($_data['relayhost']) : 0;
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +0100960 $quarantine_notification = (isset($_data['quarantine_notification'])) ? strval($_data['quarantine_notification']) : strval($MAILBOX_DEFAULT_ATTRIBUTES['quarantine_notification']);
961 $quarantine_category = (isset($_data['quarantine_category'])) ? strval($_data['quarantine_category']) : strval($MAILBOX_DEFAULT_ATTRIBUTES['quarantine_category']);
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +0200962 $quota_b = ($quota_m * 1048576);
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +0100963 $mailbox_attrs = json_encode(
964 array(
965 'force_pw_update' => strval($force_pw_update),
966 'tls_enforce_in' => strval($tls_enforce_in),
967 'tls_enforce_out' => strval($tls_enforce_out),
968 'sogo_access' => strval($sogo_access),
969 'imap_access' => strval($imap_access),
970 'pop3_access' => strval($pop3_access),
971 'smtp_access' => strval($smtp_access),
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +0200972 'relayhost' => strval($relayhost),
973 'passwd_update' => time(),
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +0100974 'mailbox_format' => strval($MAILBOX_DEFAULT_ATTRIBUTES['mailbox_format']),
975 'quarantine_notification' => strval($quarantine_notification),
976 'quarantine_category' => strval($quarantine_category)
977 )
978 );
979 if (!is_valid_domain_name($domain)) {
980 $_SESSION['return'][] = array(
981 'type' => 'danger',
982 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
983 'msg' => 'domain_invalid'
984 );
985 return false;
986 }
987 if (!hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $domain)) {
988 $_SESSION['return'][] = array(
989 'type' => 'danger',
990 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
991 'msg' => 'access_denied'
992 );
993 return false;
994 }
995 $stmt = $pdo->prepare("SELECT `mailboxes`, `maxquota`, `quota` FROM `domain`
996 WHERE `domain` = :domain");
997 $stmt->execute(array(':domain' => $domain));
998 $DomainData = $stmt->fetch(PDO::FETCH_ASSOC);
999 $stmt = $pdo->prepare("SELECT
1000 COUNT(*) as count,
1001 COALESCE(ROUND(SUM(`quota`)/1048576), 0) as `quota`
1002 FROM `mailbox`
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02001003 WHERE (`kind` = '' OR `kind` = NULL)
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01001004 AND `domain` = :domain");
1005 $stmt->execute(array(':domain' => $domain));
1006 $MailboxData = $stmt->fetch(PDO::FETCH_ASSOC);
1007 $stmt = $pdo->prepare("SELECT `local_part` FROM `mailbox` WHERE `local_part` = :local_part and `domain`= :domain");
1008 $stmt->execute(array(':local_part' => $local_part, ':domain' => $domain));
1009 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
1010 if ($num_results != 0) {
1011 $_SESSION['return'][] = array(
1012 'type' => 'danger',
1013 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1014 'msg' => array('object_exists', htmlspecialchars($username))
1015 );
1016 return false;
1017 }
1018 $stmt = $pdo->prepare("SELECT `address` FROM `alias` WHERE address= :username");
1019 $stmt->execute(array(':username' => $username));
1020 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
1021 if ($num_results != 0) {
1022 $_SESSION['return'][] = array(
1023 'type' => 'danger',
1024 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1025 'msg' => array('is_alias', htmlspecialchars($username))
1026 );
1027 return false;
1028 }
1029 $stmt = $pdo->prepare("SELECT `address` FROM `spamalias` WHERE `address`= :username");
1030 $stmt->execute(array(':username' => $username));
1031 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
1032 if ($num_results != 0) {
1033 $_SESSION['return'][] = array(
1034 'type' => 'danger',
1035 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1036 'msg' => array('is_spam_alias', htmlspecialchars($username))
1037 );
1038 return false;
1039 }
1040 $stmt = $pdo->prepare("SELECT `domain` FROM `domain` WHERE `domain`= :domain");
1041 $stmt->execute(array(':domain' => $domain));
1042 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
1043 if ($num_results == 0) {
1044 $_SESSION['return'][] = array(
1045 'type' => 'danger',
1046 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1047 'msg' => array('domain_not_found', htmlspecialchars($domain))
1048 );
1049 return false;
1050 }
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02001051 if (password_check($password, $password2) !== true) {
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01001052 return false;
1053 }
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02001054 $password_hashed = hash_password($password);
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01001055 if ($MailboxData['count'] >= $DomainData['mailboxes']) {
1056 $_SESSION['return'][] = array(
1057 'type' => 'danger',
1058 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1059 'msg' => array('max_mailbox_exceeded', $MailboxData['count'], $DomainData['mailboxes'])
1060 );
1061 return false;
1062 }
1063 if ($quota_m > $DomainData['maxquota']) {
1064 $_SESSION['return'][] = array(
1065 'type' => 'danger',
1066 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1067 'msg' => array('mailbox_quota_exceeded', $DomainData['maxquota'])
1068 );
1069 return false;
1070 }
1071 if (($MailboxData['quota'] + $quota_m) > $DomainData['quota']) {
1072 $quota_left_m = ($DomainData['quota'] - $MailboxData['quota']);
1073 $_SESSION['return'][] = array(
1074 'type' => 'danger',
1075 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1076 'msg' => array('mailbox_quota_left_exceeded', $quota_left_m)
1077 );
1078 return false;
1079 }
1080 $stmt = $pdo->prepare("INSERT INTO `mailbox` (`username`, `password`, `name`, `quota`, `local_part`, `domain`, `attributes`, `active`)
1081 VALUES (:username, :password_hashed, :name, :quota_b, :local_part, :domain, :mailbox_attrs, :active)");
1082 $stmt->execute(array(
1083 ':username' => $username,
1084 ':password_hashed' => $password_hashed,
1085 ':name' => $name,
1086 ':quota_b' => $quota_b,
1087 ':local_part' => $local_part,
1088 ':domain' => $domain,
1089 ':mailbox_attrs' => $mailbox_attrs,
1090 ':active' => $active
1091 ));
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02001092 $stmt = $pdo->prepare("UPDATE `mailbox` SET
1093 `attributes` = JSON_SET(`attributes`, '$.passwd_update', NOW())
1094 WHERE `username` = :username");
1095 $stmt->execute(array(
1096 ':username' => $username
1097 ));
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01001098 $stmt = $pdo->prepare("INSERT INTO `quota2` (`username`, `bytes`, `messages`)
1099 VALUES (:username, '0', '0') ON DUPLICATE KEY UPDATE `bytes` = '0', `messages` = '0';");
1100 $stmt->execute(array(':username' => $username));
1101 $stmt = $pdo->prepare("INSERT INTO `quota2replica` (`username`, `bytes`, `messages`)
1102 VALUES (:username, '0', '0') ON DUPLICATE KEY UPDATE `bytes` = '0', `messages` = '0';");
1103 $stmt->execute(array(':username' => $username));
1104 $stmt = $pdo->prepare("INSERT INTO `alias` (`address`, `goto`, `domain`, `active`)
1105 VALUES (:username1, :username2, :domain, :active)");
1106 $stmt->execute(array(
1107 ':username1' => $username,
1108 ':username2' => $username,
1109 ':domain' => $domain,
1110 ':active' => $active
1111 ));
1112 $stmt = $pdo->prepare("INSERT INTO `user_acl` (`username`) VALUES (:username)");
1113 $stmt->execute(array(
1114 ':username' => $username
1115 ));
1116 $_SESSION['return'][] = array(
1117 'type' => 'success',
1118 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1119 'msg' => array('mailbox_added', htmlspecialchars($username))
1120 );
1121 break;
1122 case 'resource':
1123 $domain = idn_to_ascii(strtolower(trim($_data['domain'])), 0, INTL_IDNA_VARIANT_UTS46);
1124 $description = $_data['description'];
1125 $local_part = preg_replace('/[^\da-z]/i', '', preg_quote($description, '/'));
1126 $name = $local_part . '@' . $domain;
1127 $kind = $_data['kind'];
1128 $multiple_bookings = intval($_data['multiple_bookings']);
1129 $active = intval($_data['active']);
1130 if (!filter_var($name, FILTER_VALIDATE_EMAIL)) {
1131 $_SESSION['return'][] = array(
1132 'type' => 'danger',
1133 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1134 'msg' => 'resource_invalid'
1135 );
1136 return false;
1137 }
1138 if (empty($description)) {
1139 $_SESSION['return'][] = array(
1140 'type' => 'danger',
1141 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1142 'msg' => 'description_invalid'
1143 );
1144 return false;
1145 }
1146 if (!isset($multiple_bookings) || $multiple_bookings < -1) {
1147 $multiple_bookings = -1;
1148 }
1149 if ($kind != 'location' && $kind != 'group' && $kind != 'thing') {
1150 $_SESSION['return'][] = array(
1151 'type' => 'danger',
1152 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1153 'msg' => 'resource_invalid'
1154 );
1155 return false;
1156 }
1157 if (!is_valid_domain_name($domain)) {
1158 $_SESSION['return'][] = array(
1159 'type' => 'danger',
1160 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1161 'msg' => 'domain_invalid'
1162 );
1163 return false;
1164 }
1165 if (!hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $domain)) {
1166 $_SESSION['return'][] = array(
1167 'type' => 'danger',
1168 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1169 'msg' => 'access_denied'
1170 );
1171 return false;
1172 }
1173 $stmt = $pdo->prepare("SELECT `username` FROM `mailbox` WHERE `username` = :name");
1174 $stmt->execute(array(':name' => $name));
1175 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
1176 if ($num_results != 0) {
1177 $_SESSION['return'][] = array(
1178 'type' => 'danger',
1179 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1180 'msg' => array('object_exists', htmlspecialchars($name))
1181 );
1182 return false;
1183 }
1184 $stmt = $pdo->prepare("SELECT `address` FROM `alias` WHERE address= :name");
1185 $stmt->execute(array(':name' => $name));
1186 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
1187 if ($num_results != 0) {
1188 $_SESSION['return'][] = array(
1189 'type' => 'danger',
1190 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1191 'msg' => array('is_alias', htmlspecialchars($name))
1192 );
1193 return false;
1194 }
1195 $stmt = $pdo->prepare("SELECT `address` FROM `spamalias` WHERE `address`= :name");
1196 $stmt->execute(array(':name' => $name));
1197 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
1198 if ($num_results != 0) {
1199 $_SESSION['return'][] = array(
1200 'type' => 'danger',
1201 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1202 'msg' => array('is_spam_alias', htmlspecialchars($name))
1203 );
1204 return false;
1205 }
1206 $stmt = $pdo->prepare("SELECT `domain` FROM `domain` WHERE `domain`= :domain");
1207 $stmt->execute(array(':domain' => $domain));
1208 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
1209 if ($num_results == 0) {
1210 $_SESSION['return'][] = array(
1211 'type' => 'danger',
1212 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1213 'msg' => array('domain_not_found', htmlspecialchars($domain))
1214 );
1215 return false;
1216 }
1217 $stmt = $pdo->prepare("INSERT INTO `mailbox` (`username`, `password`, `name`, `quota`, `local_part`, `domain`, `active`, `multiple_bookings`, `kind`)
1218 VALUES (:name, 'RESOURCE', :description, 0, :local_part, :domain, :active, :multiple_bookings, :kind)");
1219 $stmt->execute(array(
1220 ':name' => $name,
1221 ':description' => $description,
1222 ':local_part' => $local_part,
1223 ':domain' => $domain,
1224 ':active' => $active,
1225 ':kind' => $kind,
1226 ':multiple_bookings' => $multiple_bookings
1227 ));
1228 $_SESSION['return'][] = array(
1229 'type' => 'success',
1230 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1231 'msg' => array('resource_added', htmlspecialchars($name))
1232 );
1233 break;
1234 }
1235 break;
1236 case 'edit':
1237 switch ($_type) {
1238 case 'alias_domain':
1239 $alias_domains = (array)$_data['alias_domain'];
1240 foreach ($alias_domains as $alias_domain) {
1241 $alias_domain = idn_to_ascii(strtolower(trim($alias_domain)), 0, INTL_IDNA_VARIANT_UTS46);
1242 $is_now = mailbox('get', 'alias_domain_details', $alias_domain);
1243 if (!empty($is_now)) {
1244 $active = (isset($_data['active'])) ? intval($_data['active']) : $is_now['active'];
1245 $target_domain = (!empty($_data['target_domain'])) ? idn_to_ascii(strtolower(trim($_data['target_domain'])), 0, INTL_IDNA_VARIANT_UTS46) : $is_now['target_domain'];
1246 }
1247 else {
1248 $_SESSION['return'][] = array(
1249 'type' => 'danger',
1250 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1251 'msg' => array('alias_domain_invalid', htmlspecialchars($alias_domain))
1252 );
1253 continue;
1254 }
1255 if (!is_valid_domain_name($target_domain)) {
1256 $_SESSION['return'][] = array(
1257 'type' => 'danger',
1258 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1259 'msg' => array('target_domain_invalid', htmlspecialchars($target_domain))
1260 );
1261 continue;
1262 }
1263 if (!hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $target_domain)) {
1264 $_SESSION['return'][] = array(
1265 'type' => 'danger',
1266 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1267 'msg' => 'access_denied'
1268 );
1269 continue;
1270 }
1271 if (empty(mailbox('get', 'domain_details', $target_domain)) || !empty(mailbox('get', 'alias_domain_details', $target_domain))) {
1272 $_SESSION['return'][] = array(
1273 'type' => 'danger',
1274 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1275 'msg' => array('target_domain_invalid', htmlspecialchars($target_domain))
1276 );
1277 continue;
1278 }
1279 $stmt = $pdo->prepare("UPDATE `alias_domain` SET
1280 `target_domain` = :target_domain,
1281 `active` = :active
1282 WHERE `alias_domain` = :alias_domain");
1283 $stmt->execute(array(
1284 ':alias_domain' => $alias_domain,
1285 ':target_domain' => $target_domain,
1286 ':active' => $active
1287 ));
1288 $_SESSION['return'][] = array(
1289 'type' => 'success',
1290 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1291 'msg' => array('aliasd_modified', htmlspecialchars($alias_domain))
1292 );
1293 }
1294 break;
1295 case 'tls_policy':
1296 if (!is_array($_data['username'])) {
1297 $usernames = array();
1298 $usernames[] = $_data['username'];
1299 }
1300 else {
1301 $usernames = $_data['username'];
1302 }
1303 if (!isset($_SESSION['acl']['tls_policy']) || $_SESSION['acl']['tls_policy'] != "1" ) {
1304 $_SESSION['return'][] = array(
1305 'type' => 'danger',
1306 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1307 'msg' => 'access_denied'
1308 );
1309 return false;
1310 }
1311 foreach ($usernames as $username) {
1312 if (!filter_var($username, FILTER_VALIDATE_EMAIL) || !hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $username)) {
1313 $_SESSION['return'][] = array(
1314 'type' => 'danger',
1315 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1316 'msg' => 'access_denied'
1317 );
1318 continue;
1319 }
1320 $is_now = mailbox('get', 'tls_policy', $username);
1321 if (!empty($is_now)) {
1322 $tls_enforce_in = (isset($_data['tls_enforce_in'])) ? intval($_data['tls_enforce_in']) : $is_now['tls_enforce_in'];
1323 $tls_enforce_out = (isset($_data['tls_enforce_out'])) ? intval($_data['tls_enforce_out']) : $is_now['tls_enforce_out'];
1324 }
1325 else {
1326 $_SESSION['return'][] = array(
1327 'type' => 'danger',
1328 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1329 'msg' => 'access_denied'
1330 );
1331 continue;
1332 }
1333 $stmt = $pdo->prepare("UPDATE `mailbox`
1334 SET `attributes` = JSON_SET(`attributes`, '$.tls_enforce_out', :tls_out),
1335 `attributes` = JSON_SET(`attributes`, '$.tls_enforce_in', :tls_in)
1336 WHERE `username` = :username");
1337 $stmt->execute(array(
1338 ':tls_out' => intval($tls_enforce_out),
1339 ':tls_in' => intval($tls_enforce_in),
1340 ':username' => $username
1341 ));
1342 $_SESSION['return'][] = array(
1343 'type' => 'success',
1344 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1345 'msg' => array('mailbox_modified', $username)
1346 );
1347 }
1348 break;
1349 case 'quarantine_notification':
1350 if (!is_array($_data['username'])) {
1351 $usernames = array();
1352 $usernames[] = $_data['username'];
1353 }
1354 else {
1355 $usernames = $_data['username'];
1356 }
1357 if (!isset($_SESSION['acl']['quarantine_notification']) || $_SESSION['acl']['quarantine_notification'] != "1" ) {
1358 $_SESSION['return'][] = array(
1359 'type' => 'danger',
1360 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1361 'msg' => 'access_denied'
1362 );
1363 return false;
1364 }
1365 foreach ($usernames as $username) {
1366 if (!filter_var($username, FILTER_VALIDATE_EMAIL) || !hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $username)) {
1367 $_SESSION['return'][] = array(
1368 'type' => 'danger',
1369 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1370 'msg' => 'access_denied'
1371 );
1372 continue;
1373 }
1374 $is_now = mailbox('get', 'quarantine_notification', $username);
1375 if (!empty($is_now)) {
1376 $quarantine_notification = (isset($_data['quarantine_notification'])) ? $_data['quarantine_notification'] : $is_now['quarantine_notification'];
1377 }
1378 else {
1379 $_SESSION['return'][] = array(
1380 'type' => 'danger',
1381 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1382 'msg' => 'access_denied'
1383 );
1384 continue;
1385 }
1386 if (!in_array($quarantine_notification, array('never', 'hourly', 'daily', 'weekly'))) {
1387 $_SESSION['return'][] = array(
1388 'type' => 'danger',
1389 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1390 'msg' => 'access_denied'
1391 );
1392 continue;
1393 }
1394 $stmt = $pdo->prepare("UPDATE `mailbox`
1395 SET `attributes` = JSON_SET(`attributes`, '$.quarantine_notification', :quarantine_notification)
1396 WHERE `username` = :username");
1397 $stmt->execute(array(
1398 ':quarantine_notification' => $quarantine_notification,
1399 ':username' => $username
1400 ));
1401 $_SESSION['return'][] = array(
1402 'type' => 'success',
1403 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1404 'msg' => array('mailbox_modified', $username)
1405 );
1406 }
1407 break;
1408 case 'quarantine_category':
1409 if (!is_array($_data['username'])) {
1410 $usernames = array();
1411 $usernames[] = $_data['username'];
1412 }
1413 else {
1414 $usernames = $_data['username'];
1415 }
1416 if (!isset($_SESSION['acl']['quarantine_category']) || $_SESSION['acl']['quarantine_category'] != "1" ) {
1417 $_SESSION['return'][] = array(
1418 'type' => 'danger',
1419 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1420 'msg' => 'access_denied'
1421 );
1422 return false;
1423 }
1424 foreach ($usernames as $username) {
1425 if (!filter_var($username, FILTER_VALIDATE_EMAIL) || !hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $username)) {
1426 $_SESSION['return'][] = array(
1427 'type' => 'danger',
1428 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1429 'msg' => 'access_denied'
1430 );
1431 continue;
1432 }
1433 $is_now = mailbox('get', 'quarantine_category', $username);
1434 if (!empty($is_now)) {
1435 $quarantine_category = (isset($_data['quarantine_category'])) ? $_data['quarantine_category'] : $is_now['quarantine_category'];
1436 }
1437 else {
1438 $_SESSION['return'][] = array(
1439 'type' => 'danger',
1440 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1441 'msg' => 'access_denied'
1442 );
1443 continue;
1444 }
1445 if (!in_array($quarantine_category, array('add_header', 'reject', 'all'))) {
1446 $_SESSION['return'][] = array(
1447 'type' => 'danger',
1448 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1449 'msg' => 'access_denied'
1450 );
1451 continue;
1452 }
1453 $stmt = $pdo->prepare("UPDATE `mailbox`
1454 SET `attributes` = JSON_SET(`attributes`, '$.quarantine_category', :quarantine_category)
1455 WHERE `username` = :username");
1456 $stmt->execute(array(
1457 ':quarantine_category' => $quarantine_category,
1458 ':username' => $username
1459 ));
1460 $_SESSION['return'][] = array(
1461 'type' => 'success',
1462 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1463 'msg' => array('mailbox_modified', $username)
1464 );
1465 }
1466 break;
1467 case 'spam_score':
1468 if (!is_array($_data['username'])) {
1469 $usernames = array();
1470 $usernames[] = $_data['username'];
1471 }
1472 else {
1473 $usernames = $_data['username'];
1474 }
1475 if (!isset($_SESSION['acl']['spam_score']) || $_SESSION['acl']['spam_score'] != "1" ) {
1476 $_SESSION['return'][] = array(
1477 'type' => 'danger',
1478 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1479 'msg' => 'access_denied'
1480 );
1481 return false;
1482 }
1483 foreach ($usernames as $username) {
1484 if ($_data['spam_score'] == "default") {
1485 $stmt = $pdo->prepare("DELETE FROM `filterconf` WHERE `object` = :username
1486 AND (`option` = 'lowspamlevel' OR `option` = 'highspamlevel')");
1487 $stmt->execute(array(
1488 ':username' => $username
1489 ));
1490 $_SESSION['return'][] = array(
1491 'type' => 'success',
1492 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1493 'msg' => array('mailbox_modified', $username)
1494 );
1495 continue;
1496 }
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02001497 $lowspamlevel = explode(',', $_data['spam_score'])[0];
1498 $highspamlevel = explode(',', $_data['spam_score'])[1];
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01001499 if (!is_numeric($lowspamlevel) || !is_numeric($highspamlevel)) {
1500 $_SESSION['return'][] = array(
1501 'type' => 'danger',
1502 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1503 'msg' => 'Invalid spam score, format must be "1,2" where first is low and second is high spam value.'
1504 );
1505 continue;
1506 }
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02001507 if ($lowspamlevel == $highspamlevel) {
1508 $highspamlevel = $highspamlevel + 0.1;
1509 }
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01001510 $stmt = $pdo->prepare("DELETE FROM `filterconf` WHERE `object` = :username
1511 AND (`option` = 'lowspamlevel' OR `option` = 'highspamlevel')");
1512 $stmt->execute(array(
1513 ':username' => $username
1514 ));
1515 $stmt = $pdo->prepare("INSERT INTO `filterconf` (`object`, `option`, `value`)
1516 VALUES (:username, 'highspamlevel', :highspamlevel)");
1517 $stmt->execute(array(
1518 ':username' => $username,
1519 ':highspamlevel' => $highspamlevel
1520 ));
1521 $stmt = $pdo->prepare("INSERT INTO `filterconf` (`object`, `option`, `value`)
1522 VALUES (:username, 'lowspamlevel', :lowspamlevel)");
1523 $stmt->execute(array(
1524 ':username' => $username,
1525 ':lowspamlevel' => $lowspamlevel
1526 ));
1527 $_SESSION['return'][] = array(
1528 'type' => 'success',
1529 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1530 'msg' => array('mailbox_modified', $username)
1531 );
1532 }
1533 break;
1534 case 'time_limited_alias':
1535 if (!isset($_SESSION['acl']['spam_alias']) || $_SESSION['acl']['spam_alias'] != "1" ) {
1536 $_SESSION['return'][] = array(
1537 'type' => 'danger',
1538 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1539 'msg' => 'access_denied'
1540 );
1541 return false;
1542 }
1543 if (!is_array($_data['address'])) {
1544 $addresses = array();
1545 $addresses[] = $_data['address'];
1546 }
1547 else {
1548 $addresses = $_data['address'];
1549 }
1550 foreach ($addresses as $address) {
1551 $stmt = $pdo->prepare("SELECT `goto` FROM `spamalias` WHERE `address` = :address");
1552 $stmt->execute(array(':address' => $address));
1553 $goto = $stmt->fetch(PDO::FETCH_ASSOC)['goto'];
1554 if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $goto)) {
1555 $_SESSION['return'][] = array(
1556 'type' => 'danger',
1557 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1558 'msg' => 'access_denied'
1559 );
1560 continue;
1561 }
1562 if (empty($_data['validity'])) {
1563 continue;
1564 }
1565 $validity = round((int)time() + ($_data['validity'] * 3600));
1566 $stmt = $pdo->prepare("UPDATE `spamalias` SET `validity` = :validity WHERE
1567 `address` = :address");
1568 $stmt->execute(array(
1569 ':address' => $address,
1570 ':validity' => $validity
1571 ));
1572 $_SESSION['return'][] = array(
1573 'type' => 'success',
1574 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02001575 'msg' => array('mailbox_modified', htmlspecialchars(implode(', ', (array)$usernames)))
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01001576 );
1577 }
1578 break;
1579 case 'delimiter_action':
1580 if (!is_array($_data['username'])) {
1581 $usernames = array();
1582 $usernames[] = $_data['username'];
1583 }
1584 else {
1585 $usernames = $_data['username'];
1586 }
1587 if (!isset($_SESSION['acl']['delimiter_action']) || $_SESSION['acl']['delimiter_action'] != "1" ) {
1588 $_SESSION['return'][] = array(
1589 'type' => 'danger',
1590 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1591 'msg' => 'access_denied'
1592 );
1593 return false;
1594 }
1595 foreach ($usernames as $username) {
1596 if (!filter_var($username, FILTER_VALIDATE_EMAIL) || !hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $username)) {
1597 $_SESSION['return'][] = array(
1598 'type' => 'danger',
1599 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1600 'msg' => 'access_denied'
1601 );
1602 continue;
1603 }
1604 if (isset($_data['tagged_mail_handler']) && $_data['tagged_mail_handler'] == "subject") {
1605 try {
1606 $redis->hSet('RCPT_WANTS_SUBJECT_TAG', $username, 1);
1607 $redis->hDel('RCPT_WANTS_SUBFOLDER_TAG', $username);
1608 }
1609 catch (RedisException $e) {
1610 $_SESSION['return'][] = array(
1611 'type' => 'danger',
1612 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1613 'msg' => array('redis_error', $e)
1614 );
1615 continue;
1616 }
1617 }
1618 else if (isset($_data['tagged_mail_handler']) && $_data['tagged_mail_handler'] == "subfolder") {
1619 try {
1620 $redis->hSet('RCPT_WANTS_SUBFOLDER_TAG', $username, 1);
1621 $redis->hDel('RCPT_WANTS_SUBJECT_TAG', $username);
1622 }
1623 catch (RedisException $e) {
1624 $_SESSION['return'][] = array(
1625 'type' => 'danger',
1626 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1627 'msg' => array('redis_error', $e)
1628 );
1629 continue;
1630 }
1631 }
1632 else {
1633 try {
1634 $redis->hDel('RCPT_WANTS_SUBJECT_TAG', $username);
1635 $redis->hDel('RCPT_WANTS_SUBFOLDER_TAG', $username);
1636 }
1637 catch (RedisException $e) {
1638 $_SESSION['return'][] = array(
1639 'type' => 'danger',
1640 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1641 'msg' => array('redis_error', $e)
1642 );
1643 continue;
1644 }
1645 }
1646 $_SESSION['return'][] = array(
1647 'type' => 'success',
1648 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1649 'msg' => array('mailbox_modified', $username)
1650 );
1651 }
1652 break;
1653 case 'syncjob':
1654 if (!is_array($_data['id'])) {
1655 $ids = array();
1656 $ids[] = $_data['id'];
1657 }
1658 else {
1659 $ids = $_data['id'];
1660 }
1661 if (!isset($_SESSION['acl']['syncjobs']) || $_SESSION['acl']['syncjobs'] != "1" ) {
1662 $_SESSION['return'][] = array(
1663 'type' => 'danger',
1664 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1665 'msg' => 'access_denied'
1666 );
1667 return false;
1668 }
1669 foreach ($ids as $id) {
1670 $is_now = mailbox('get', 'syncjob_details', $id, array('with_password'));
1671 if (!empty($is_now)) {
1672 $username = $is_now['user2'];
1673 $user1 = (!empty($_data['user1'])) ? $_data['user1'] : $is_now['user1'];
1674 $active = (isset($_data['active'])) ? intval($_data['active']) : $is_now['active'];
1675 $last_run = (isset($_data['last_run'])) ? NULL : $is_now['last_run'];
1676 $delete2duplicates = (isset($_data['delete2duplicates'])) ? intval($_data['delete2duplicates']) : $is_now['delete2duplicates'];
1677 $subscribeall = (isset($_data['subscribeall'])) ? intval($_data['subscribeall']) : $is_now['subscribeall'];
1678 $delete1 = (isset($_data['delete1'])) ? intval($_data['delete1']) : $is_now['delete1'];
1679 $delete2 = (isset($_data['delete2'])) ? intval($_data['delete2']) : $is_now['delete2'];
1680 $automap = (isset($_data['automap'])) ? intval($_data['automap']) : $is_now['automap'];
1681 $skipcrossduplicates = (isset($_data['skipcrossduplicates'])) ? intval($_data['skipcrossduplicates']) : $is_now['skipcrossduplicates'];
1682 $port1 = (!empty($_data['port1'])) ? $_data['port1'] : $is_now['port1'];
1683 $password1 = (!empty($_data['password1'])) ? $_data['password1'] : $is_now['password1'];
1684 $host1 = (!empty($_data['host1'])) ? $_data['host1'] : $is_now['host1'];
1685 $subfolder2 = (isset($_data['subfolder2'])) ? $_data['subfolder2'] : $is_now['subfolder2'];
1686 $enc1 = (!empty($_data['enc1'])) ? $_data['enc1'] : $is_now['enc1'];
1687 $mins_interval = (!empty($_data['mins_interval'])) ? $_data['mins_interval'] : $is_now['mins_interval'];
1688 $exclude = (isset($_data['exclude'])) ? $_data['exclude'] : $is_now['exclude'];
1689 $custom_params = (isset($_data['custom_params'])) ? $_data['custom_params'] : $is_now['custom_params'];
1690 $maxage = (isset($_data['maxage']) && $_data['maxage'] != "") ? intval($_data['maxage']) : $is_now['maxage'];
1691 $maxbytespersecond = (isset($_data['maxbytespersecond']) && $_data['maxbytespersecond'] != "") ? intval($_data['maxbytespersecond']) : $is_now['maxbytespersecond'];
1692 $timeout1 = (isset($_data['timeout1']) && $_data['timeout1'] != "") ? intval($_data['timeout1']) : $is_now['timeout1'];
1693 $timeout2 = (isset($_data['timeout2']) && $_data['timeout2'] != "") ? intval($_data['timeout2']) : $is_now['timeout2'];
1694 }
1695 else {
1696 $_SESSION['return'][] = array(
1697 'type' => 'danger',
1698 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1699 'msg' => 'access_denied'
1700 );
1701 continue;
1702 }
1703 if (strpos($custom_params, 'pipemess')) {
1704 $custom_params = '';
1705 }
1706 if (empty($subfolder2)) {
1707 $subfolder2 = "";
1708 }
1709 if (!isset($maxage) || !filter_var($maxage, FILTER_VALIDATE_INT, array('options' => array('min_range' => 1, 'max_range' => 32000)))) {
1710 $maxage = "0";
1711 }
1712 if (!isset($timeout1) || !filter_var($timeout1, FILTER_VALIDATE_INT, array('options' => array('min_range' => 1, 'max_range' => 32000)))) {
1713 $timeout1 = "600";
1714 }
1715 if (!isset($timeout2) || !filter_var($timeout2, FILTER_VALIDATE_INT, array('options' => array('min_range' => 1, 'max_range' => 32000)))) {
1716 $timeout2 = "600";
1717 }
1718 if (!isset($maxbytespersecond) || !filter_var($maxbytespersecond, FILTER_VALIDATE_INT, array('options' => array('min_range' => 1, 'max_range' => 125000000)))) {
1719 $maxbytespersecond = "0";
1720 }
1721 if (!filter_var($port1, FILTER_VALIDATE_INT, array('options' => array('min_range' => 1, 'max_range' => 65535)))) {
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 if (!filter_var($mins_interval, FILTER_VALIDATE_INT, array('options' => array('min_range' => 1, 'max_range' => 43800)))) {
1730 $_SESSION['return'][] = array(
1731 'type' => 'danger',
1732 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1733 'msg' => 'access_denied'
1734 );
1735 continue;
1736 }
1737 if (!is_valid_domain_name($host1)) {
1738 $_SESSION['return'][] = array(
1739 'type' => 'danger',
1740 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1741 'msg' => 'access_denied'
1742 );
1743 continue;
1744 }
1745 if ($enc1 != "TLS" && $enc1 != "SSL" && $enc1 != "PLAIN") {
1746 $_SESSION['return'][] = array(
1747 'type' => 'danger',
1748 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1749 'msg' => 'access_denied'
1750 );
1751 continue;
1752 }
1753 if (@preg_match("/" . $exclude . "/", null) === false) {
1754 $_SESSION['return'][] = array(
1755 'type' => 'danger',
1756 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1757 'msg' => 'access_denied'
1758 );
1759 continue;
1760 }
1761 $stmt = $pdo->prepare("UPDATE `imapsync` SET `delete1` = :delete1,
1762 `delete2` = :delete2,
1763 `automap` = :automap,
1764 `skipcrossduplicates` = :skipcrossduplicates,
1765 `maxage` = :maxage,
1766 `maxbytespersecond` = :maxbytespersecond,
1767 `subfolder2` = :subfolder2,
1768 `exclude` = :exclude,
1769 `host1` = :host1,
1770 `last_run` = :last_run,
1771 `user1` = :user1,
1772 `password1` = :password1,
1773 `mins_interval` = :mins_interval,
1774 `port1` = :port1,
1775 `enc1` = :enc1,
1776 `delete2duplicates` = :delete2duplicates,
1777 `custom_params` = :custom_params,
1778 `timeout1` = :timeout1,
1779 `timeout2` = :timeout2,
1780 `subscribeall` = :subscribeall,
1781 `active` = :active
1782 WHERE `id` = :id");
1783 $stmt->execute(array(
1784 ':delete1' => $delete1,
1785 ':delete2' => $delete2,
1786 ':automap' => $automap,
1787 ':skipcrossduplicates' => $skipcrossduplicates,
1788 ':id' => $id,
1789 ':exclude' => $exclude,
1790 ':maxage' => $maxage,
1791 ':maxbytespersecond' => $maxbytespersecond,
1792 ':subfolder2' => $subfolder2,
1793 ':host1' => $host1,
1794 ':user1' => $user1,
1795 ':password1' => $password1,
1796 ':last_run' => $last_run,
1797 ':mins_interval' => $mins_interval,
1798 ':port1' => $port1,
1799 ':enc1' => $enc1,
1800 ':delete2duplicates' => $delete2duplicates,
1801 ':custom_params' => $custom_params,
1802 ':timeout1' => $timeout1,
1803 ':timeout2' => $timeout2,
1804 ':subscribeall' => $subscribeall,
1805 ':active' => $active,
1806 ));
1807 $_SESSION['return'][] = array(
1808 'type' => 'success',
1809 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1810 'msg' => array('mailbox_modified', $username)
1811 );
1812 }
1813 break;
1814 case 'filter':
1815 if (!isset($_SESSION['acl']['filters']) || $_SESSION['acl']['filters'] != "1" ) {
1816 $_SESSION['return'][] = array(
1817 'type' => 'danger',
1818 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1819 'msg' => 'access_denied'
1820 );
1821 return false;
1822 }
1823 $sieve = new Sieve\SieveParser();
1824 if (!is_array($_data['id'])) {
1825 $ids = array();
1826 $ids[] = $_data['id'];
1827 }
1828 else {
1829 $ids = $_data['id'];
1830 }
1831 foreach ($ids as $id) {
1832 $is_now = mailbox('get', 'filter_details', $id);
1833 if (!empty($is_now)) {
1834 $username = $is_now['username'];
1835 $active = (isset($_data['active'])) ? intval($_data['active']) : $is_now['active'];
1836 $script_desc = (!empty($_data['script_desc'])) ? $_data['script_desc'] : $is_now['script_desc'];
1837 $script_data = (!empty($_data['script_data'])) ? $_data['script_data'] : $is_now['script_data'];
1838 $filter_type = (!empty($_data['filter_type'])) ? $_data['filter_type'] : $is_now['filter_type'];
1839 }
1840 else {
1841 $_SESSION['return'][] = array(
1842 'type' => 'danger',
1843 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1844 'msg' => 'access_denied'
1845 );
1846 continue;
1847 }
1848 try {
1849 $sieve->parse($script_data);
1850 }
1851 catch (Exception $e) {
1852 $_SESSION['return'][] = array(
1853 'type' => 'danger',
1854 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1855 'msg' => array('sieve_error', $e->getMessage())
1856 );
1857 continue;
1858 }
1859 if ($filter_type != 'postfilter' && $filter_type != 'prefilter') {
1860 $_SESSION['return'][] = array(
1861 'type' => 'danger',
1862 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1863 'msg' => 'filter_type'
1864 );
1865 continue;
1866 }
1867 if ($active == '1') {
1868 $script_name = 'active';
1869 $stmt = $pdo->prepare("UPDATE `sieve_filters`
1870 SET `script_name` = 'inactive'
1871 WHERE `username` = :username
1872 AND `filter_type` = :filter_type");
1873 $stmt->execute(array(
1874 ':username' => $username,
1875 ':filter_type' => $filter_type
1876 ));
1877 }
1878 else {
1879 $script_name = 'inactive';
1880 }
1881 $stmt = $pdo->prepare("UPDATE `sieve_filters` SET `script_desc` = :script_desc, `script_data` = :script_data, `script_name` = :script_name, `filter_type` = :filter_type
1882 WHERE `id` = :id");
1883 $stmt->execute(array(
1884 ':script_desc' => $script_desc,
1885 ':script_data' => $script_data,
1886 ':script_name' => $script_name,
1887 ':filter_type' => $filter_type,
1888 ':id' => $id
1889 ));
1890 $_SESSION['return'][] = array(
1891 'type' => 'success',
1892 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1893 'msg' => array('mailbox_modified', $username)
1894 );
1895 }
1896 break;
1897 case 'alias':
1898 if (!is_array($_data['id'])) {
1899 $ids = array();
1900 $ids[] = $_data['id'];
1901 }
1902 else {
1903 $ids = $_data['id'];
1904 }
1905 foreach ($ids as $id) {
1906 $is_now = mailbox('get', 'alias_details', $id);
1907 if (!empty($is_now)) {
1908 $active = (isset($_data['active'])) ? intval($_data['active']) : $is_now['active'];
1909 $sogo_visible = (isset($_data['sogo_visible'])) ? intval($_data['sogo_visible']) : $is_now['sogo_visible'];
1910 $goto_null = (isset($_data['goto_null'])) ? intval($_data['goto_null']) : 0;
1911 $goto_spam = (isset($_data['goto_spam'])) ? intval($_data['goto_spam']) : 0;
1912 $goto_ham = (isset($_data['goto_ham'])) ? intval($_data['goto_ham']) : 0;
1913 $public_comment = (isset($_data['public_comment'])) ? $_data['public_comment'] : $is_now['public_comment'];
1914 $private_comment = (isset($_data['private_comment'])) ? $_data['private_comment'] : $is_now['private_comment'];
1915 $goto = (!empty($_data['goto'])) ? $_data['goto'] : $is_now['goto'];
1916 $address = (!empty($_data['address'])) ? $_data['address'] : $is_now['address'];
1917 }
1918 else {
1919 $_SESSION['return'][] = array(
1920 'type' => 'danger',
1921 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1922 'msg' => array('alias_invalid', $address)
1923 );
1924 continue;
1925 }
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02001926 if ($_data['expand_alias'] === true || $_data['expand_alias'] == 1) {
1927 $stmt = $pdo->prepare("SELECT `address` FROM `alias`
1928 WHERE `address` = :address
1929 AND `domain` NOT IN (
1930 SELECT `alias_domain` FROM `alias_domain`
1931 )");
1932 $stmt->execute(array(
1933 ':address' => $address,
1934 ));
1935 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
1936 if ($num_results == 0) {
1937 $_SESSION['return'][] = array(
1938 'type' => 'warning',
1939 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1940 'msg' => array('is_not_primary_alias', htmlspecialchars($address))
1941 );
1942 continue;
1943 }
1944 $stmt = $pdo->prepare("SELECT `goto`, GROUP_CONCAT(CONCAT(SUBSTRING(`alias`.`address`, 1, LOCATE('@', `alias`.`address`) - 1), '@', `alias_domain`.`alias_domain`)) AS `missing_alias`
1945 FROM `alias` JOIN `alias_domain` ON `alias_domain`.`target_domain` = `alias`.`domain`
1946 WHERE CONCAT(SUBSTRING(`alias`.`address`, 1, LOCATE('@', `alias`.`address`) - 1), '@', `alias_domain`.`alias_domain`) NOT IN (
1947 SELECT `address` FROM `alias` WHERE `address` != `goto`
1948 )
1949 AND `alias`.`address` NOT IN (
1950 SELECT `address` FROM `alias` WHERE `address` = `goto`
1951 )
1952 AND `address` = :address ;");
1953 $stmt->execute(array(
1954 ':address' => $address
1955 ));
1956 $missing_aliases = $stmt->fetch(PDO::FETCH_ASSOC);
1957 if (!empty($missing_aliases['missing_alias'])) {
1958 mailbox('add', 'alias', array(
1959 'address' => $missing_aliases['missing_alias'],
1960 'goto' => $missing_aliases['goto'],
1961 'sogo_visible' => 1,
1962 'active' => 1
1963 ));
1964 }
1965 $_SESSION['return'][] = array(
1966 'type' => 'success',
1967 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1968 'msg' => array('alias_modified', htmlspecialchars($address))
1969 );
1970 continue;
1971 }
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01001972 $domain = idn_to_ascii(substr(strstr($address, '@'), 1), 0, INTL_IDNA_VARIANT_UTS46);
1973 if ($is_now['address'] != $address) {
1974 $local_part = strstr($address, '@', true);
1975 $address = $local_part.'@'.$domain;
1976 if (!hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $domain)) {
1977 $_SESSION['return'][] = array(
1978 'type' => 'danger',
1979 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1980 'msg' => 'access_denied'
1981 );
1982 continue;
1983 }
1984 if ((!filter_var($address, FILTER_VALIDATE_EMAIL) === true) && !empty($local_part)) {
1985 $_SESSION['return'][] = array(
1986 'type' => 'danger',
1987 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
1988 'msg' => array('alias_invalid', $address)
1989 );
1990 continue;
1991 }
1992 if (strtolower($is_now['address']) != strtolower($address)) {
1993 $stmt = $pdo->prepare("SELECT `address` FROM `alias`
1994 WHERE `address`= :address OR `address` IN (
1995 SELECT `username` FROM `mailbox`, `alias_domain`
1996 WHERE (
1997 `alias_domain`.`alias_domain` = :address_d
1998 AND `mailbox`.`username` = CONCAT(:address_l, '@', alias_domain.target_domain)))");
1999 $stmt->execute(array(
2000 ':address' => $address,
2001 ':address_l' => $local_part,
2002 ':address_d' => $domain
2003 ));
2004 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
2005 if ($num_results != 0) {
2006 $_SESSION['return'][] = array(
2007 'type' => 'danger',
2008 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2009 'msg' => array('is_alias_or_mailbox', htmlspecialchars($address))
2010 );
2011 continue;
2012 }
2013 }
2014 $stmt = $pdo->prepare("SELECT `domain` FROM `domain`
2015 WHERE `domain`= :domain1 OR `domain` = (SELECT `target_domain` FROM `alias_domain` WHERE `alias_domain` = :domain2)");
2016 $stmt->execute(array(':domain1' => $domain, ':domain2' => $domain));
2017 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
2018 if ($num_results == 0) {
2019 $_SESSION['return'][] = array(
2020 'type' => 'danger',
2021 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2022 'msg' => array('domain_not_found', htmlspecialchars($domain))
2023 );
2024 continue;
2025 }
2026 $stmt = $pdo->prepare("SELECT `address` FROM `spamalias`
2027 WHERE `address`= :address");
2028 $stmt->execute(array(':address' => $address));
2029 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
2030 if ($num_results != 0) {
2031 $_SESSION['return'][] = array(
2032 'type' => 'danger',
2033 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2034 'msg' => array('is_spam_alias', htmlspecialchars($address))
2035 );
2036 continue;
2037 }
2038 }
2039 if ($goto_null == "1") {
2040 $goto = "null@localhost";
2041 }
2042 elseif ($goto_spam == "1") {
2043 $goto = "spam@localhost";
2044 }
2045 elseif ($goto_ham == "1") {
2046 $goto = "ham@localhost";
2047 }
2048 else {
2049 $gotos = array_map('trim', preg_split( "/( |,|;|\n)/", $goto));
2050 foreach ($gotos as $i => &$goto) {
2051 if (empty($goto)) {
2052 continue;
2053 }
2054 if (!filter_var($goto, FILTER_VALIDATE_EMAIL)) {
2055 $_SESSION['return'][] = array(
2056 'type' => 'danger',
2057 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2058 'msg' => array('goto_invalid', $goto)
2059 );
2060 unset($gotos[$i]);
2061 continue;
2062 }
2063 if ($goto == $address) {
2064 $_SESSION['return'][] = array(
2065 'type' => 'danger',
2066 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2067 'msg' => 'alias_goto_identical'
2068 );
2069 unset($gotos[$i]);
2070 continue;
2071 }
2072 // Delete from sender_acl to prevent duplicates
2073 $stmt = $pdo->prepare("DELETE FROM `sender_acl` WHERE
2074 `logged_in_as` = :goto AND
2075 `send_as` = :address");
2076 $stmt->execute(array(
2077 ':goto' => $goto,
2078 ':address' => $address
2079 ));
2080 }
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02002081 $gotos = array_unique($gotos);
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01002082 $gotos = array_filter($gotos);
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02002083 $goto = implode(",", (array)$gotos);
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01002084 }
2085 if (!empty($goto)) {
2086 $stmt = $pdo->prepare("UPDATE `alias` SET
2087 `address` = :address,
2088 `public_comment` = :public_comment,
2089 `private_comment` = :private_comment,
2090 `domain` = :domain,
2091 `goto` = :goto,
2092 `sogo_visible`= :sogo_visible,
2093 `active`= :active
2094 WHERE `id` = :id");
2095 $stmt->execute(array(
2096 ':address' => $address,
2097 ':public_comment' => $public_comment,
2098 ':private_comment' => $private_comment,
2099 ':domain' => $domain,
2100 ':goto' => $goto,
2101 ':sogo_visible' => $sogo_visible,
2102 ':active' => $active,
2103 ':id' => $is_now['id']
2104 ));
2105 }
2106 $_SESSION['return'][] = array(
2107 'type' => 'success',
2108 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2109 'msg' => array('alias_modified', htmlspecialchars($address))
2110 );
2111 }
2112 break;
2113 case 'domain':
2114 if (!is_array($_data['domain'])) {
2115 $domains = array();
2116 $domains[] = $_data['domain'];
2117 }
2118 else {
2119 $domains = $_data['domain'];
2120 }
2121 foreach ($domains as $domain) {
2122 $domain = idn_to_ascii($domain, 0, INTL_IDNA_VARIANT_UTS46);
2123 if (!is_valid_domain_name($domain)) {
2124 $_SESSION['return'][] = array(
2125 'type' => 'danger',
2126 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2127 'msg' => 'domain_invalid'
2128 );
2129 continue;
2130 }
2131 if ($_SESSION['mailcow_cc_role'] == "domainadmin" &&
2132 hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $domain)) {
2133 $is_now = mailbox('get', 'domain_details', $domain);
2134 if (!empty($is_now)) {
2135 $gal = (isset($_data['gal'])) ? intval($_data['gal']) : $is_now['gal'];
2136 $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 +02002137 (int)$relayhost = (isset($_data['relayhost']) && isset($_SESSION['acl']['domain_relayhost']) && $_SESSION['acl']['domain_relayhost'] == "1") ? intval($_data['relayhost']) : intval($is_now['relayhost']);
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01002138 }
2139 else {
2140 $_SESSION['return'][] = array(
2141 'type' => 'danger',
2142 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2143 'msg' => 'domain_invalid'
2144 );
2145 continue;
2146 }
2147 $stmt = $pdo->prepare("UPDATE `domain` SET
2148 `description` = :description,
2149 `gal` = :gal
2150 WHERE `domain` = :domain");
2151 $stmt->execute(array(
2152 ':description' => $description,
2153 ':gal' => $gal,
2154 ':domain' => $domain
2155 ));
2156 $_SESSION['return'][] = array(
2157 'type' => 'success',
2158 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2159 'msg' => array('domain_modified', htmlspecialchars($domain))
2160 );
2161 }
2162 elseif ($_SESSION['mailcow_cc_role'] == "admin") {
2163 $is_now = mailbox('get', 'domain_details', $domain);
2164 if (!empty($is_now)) {
2165 $active = (isset($_data['active'])) ? intval($_data['active']) : $is_now['active'];
2166 $backupmx = (isset($_data['backupmx'])) ? intval($_data['backupmx']) : $is_now['backupmx'];
2167 $gal = (isset($_data['gal'])) ? intval($_data['gal']) : $is_now['gal'];
2168 $relay_all_recipients = (isset($_data['relay_all_recipients'])) ? intval($_data['relay_all_recipients']) : $is_now['relay_all_recipients'];
2169 $relay_unknown_only = (isset($_data['relay_unknown_only'])) ? intval($_data['relay_unknown_only']) : $is_now['relay_unknown_only'];
2170 $relayhost = (isset($_data['relayhost'])) ? intval($_data['relayhost']) : $is_now['relayhost'];
2171 $aliases = (!empty($_data['aliases'])) ? $_data['aliases'] : $is_now['max_num_aliases_for_domain'];
2172 $mailboxes = (isset($_data['mailboxes']) && $_data['mailboxes'] != '') ? intval($_data['mailboxes']) : $is_now['max_num_mboxes_for_domain'];
2173 $defquota = (isset($_data['defquota']) && $_data['defquota'] != '') ? intval($_data['defquota']) : ($is_now['def_quota_for_mbox'] / 1048576);
2174 $maxquota = (!empty($_data['maxquota'])) ? $_data['maxquota'] : ($is_now['max_quota_for_mbox'] / 1048576);
2175 $quota = (!empty($_data['quota'])) ? $_data['quota'] : ($is_now['max_quota_for_domain'] / 1048576);
2176 $description = (!empty($_data['description'])) ? $_data['description'] : $is_now['description'];
2177 if ($relay_all_recipients == '1') {
2178 $backupmx = '1';
2179 }
2180 if ($relay_unknown_only == '1') {
2181 $backupmx = '1';
2182 $relay_all_recipients = '1';
2183 }
2184 }
2185 else {
2186 $_SESSION['return'][] = array(
2187 'type' => 'danger',
2188 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2189 'msg' => 'domain_invalid'
2190 );
2191 continue;
2192 }
2193 // todo: should be using api here
2194 $stmt = $pdo->prepare("SELECT
2195 COUNT(*) AS count,
2196 MAX(COALESCE(ROUND(`quota`/1048576), 0)) AS `biggest_mailbox`,
2197 COALESCE(ROUND(SUM(`quota`)/1048576), 0) AS `quota_all`
2198 FROM `mailbox`
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02002199 WHERE (`kind` = '' OR `kind` = NULL)
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01002200 AND domain = :domain");
2201 $stmt->execute(array(':domain' => $domain));
2202 $MailboxData = $stmt->fetch(PDO::FETCH_ASSOC);
2203 // todo: should be using api here
2204 $stmt = $pdo->prepare("SELECT COUNT(*) AS `count` FROM `alias`
2205 WHERE domain = :domain
2206 AND address NOT IN (
2207 SELECT `username` FROM `mailbox`
2208 )");
2209 $stmt->execute(array(':domain' => $domain));
2210 $AliasData = $stmt->fetch(PDO::FETCH_ASSOC);
2211 if ($defquota > $maxquota) {
2212 $_SESSION['return'][] = array(
2213 'type' => 'danger',
2214 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2215 'msg' => 'mailbox_defquota_exceeds_mailbox_maxquota'
2216 );
2217 continue;
2218 }
2219 if ($defquota == "0" || empty($defquota)) {
2220 $_SESSION['return'][] = array(
2221 'type' => 'danger',
2222 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2223 'msg' => 'defquota_empty'
2224 );
2225 continue;
2226 }
2227 if ($maxquota > $quota) {
2228 $_SESSION['return'][] = array(
2229 'type' => 'danger',
2230 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2231 'msg' => 'mailbox_quota_exceeds_domain_quota'
2232 );
2233 continue;
2234 }
2235 if ($maxquota == "0" || empty($maxquota)) {
2236 $_SESSION['return'][] = array(
2237 'type' => 'danger',
2238 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2239 'msg' => 'maxquota_empty'
2240 );
2241 continue;
2242 }
2243 if ($MailboxData['biggest_mailbox'] > $maxquota) {
2244 $_SESSION['return'][] = array(
2245 'type' => 'danger',
2246 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2247 'msg' => array('max_quota_in_use', $MailboxData['biggest_mailbox'])
2248 );
2249 continue;
2250 }
2251 if ($MailboxData['quota_all'] > $quota) {
2252 $_SESSION['return'][] = array(
2253 'type' => 'danger',
2254 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2255 'msg' => array('domain_quota_m_in_use', $MailboxData['quota_all'])
2256 );
2257 continue;
2258 }
2259 if ($MailboxData['count'] > $mailboxes) {
2260 $_SESSION['return'][] = array(
2261 'type' => 'danger',
2262 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2263 'msg' => array('mailboxes_in_use', $MailboxData['count'])
2264 );
2265 continue;
2266 }
2267 if ($AliasData['count'] > $aliases) {
2268 $_SESSION['return'][] = array(
2269 'type' => 'danger',
2270 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2271 'msg' => array('aliases_in_use', $AliasData['count'])
2272 );
2273 continue;
2274 }
2275 $stmt = $pdo->prepare("UPDATE `domain` SET
2276 `relay_all_recipients` = :relay_all_recipients,
2277 `relay_unknown_only` = :relay_unknown_only,
2278 `backupmx` = :backupmx,
2279 `gal` = :gal,
2280 `active` = :active,
2281 `quota` = :quota,
2282 `defquota` = :defquota,
2283 `maxquota` = :maxquota,
2284 `relayhost` = :relayhost,
2285 `mailboxes` = :mailboxes,
2286 `aliases` = :aliases,
2287 `description` = :description
2288 WHERE `domain` = :domain");
2289 $stmt->execute(array(
2290 ':relay_all_recipients' => $relay_all_recipients,
2291 ':relay_unknown_only' => $relay_unknown_only,
2292 ':backupmx' => $backupmx,
2293 ':gal' => $gal,
2294 ':active' => $active,
2295 ':quota' => $quota,
2296 ':defquota' => $defquota,
2297 ':maxquota' => $maxquota,
2298 ':relayhost' => $relayhost,
2299 ':mailboxes' => $mailboxes,
2300 ':aliases' => $aliases,
2301 ':description' => $description,
2302 ':domain' => $domain
2303 ));
2304 $_SESSION['return'][] = array(
2305 'type' => 'success',
2306 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2307 'msg' => array('domain_modified', htmlspecialchars($domain))
2308 );
2309 }
2310 }
2311 break;
2312 case 'mailbox':
2313 if (!is_array($_data['username'])) {
2314 $usernames = array();
2315 $usernames[] = $_data['username'];
2316 }
2317 else {
2318 $usernames = $_data['username'];
2319 }
2320 foreach ($usernames as $username) {
2321 if (!filter_var($username, FILTER_VALIDATE_EMAIL)) {
2322 $_SESSION['return'][] = array(
2323 'type' => 'danger',
2324 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2325 'msg' => array('username_invalid', $username)
2326 );
2327 continue;
2328 }
2329 $is_now = mailbox('get', 'mailbox_details', $username);
2330 if (isset($_data['protocol_access'])) {
2331 $_data['imap_access'] = (in_array('imap', $_data['protocol_access'])) ? 1 : 0;
2332 $_data['pop3_access'] = (in_array('pop3', $_data['protocol_access'])) ? 1 : 0;
2333 $_data['smtp_access'] = (in_array('smtp', $_data['protocol_access'])) ? 1 : 0;
2334 }
2335 if (!empty($is_now)) {
2336 $active = (isset($_data['active'])) ? intval($_data['active']) : $is_now['active'];
2337 (int)$force_pw_update = (isset($_data['force_pw_update'])) ? intval($_data['force_pw_update']) : intval($is_now['attributes']['force_pw_update']);
2338 (int)$sogo_access = (isset($_data['sogo_access']) && isset($_SESSION['acl']['protocol_access']) && $_SESSION['acl']['protocol_access'] == "1") ? intval($_data['sogo_access']) : intval($is_now['attributes']['sogo_access']);
2339 (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']);
2340 (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']);
2341 (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 Benkard7b2a3a12021-08-16 10:57:25 +02002342 (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 +01002343 (int)$quota_m = (isset_has_content($_data['quota'])) ? intval($_data['quota']) : ($is_now['quota'] / 1048576);
2344 $name = (!empty($_data['name'])) ? ltrim(rtrim($_data['name'], '>'), '<') : $is_now['name'];
2345 $domain = $is_now['domain'];
2346 $quota_b = $quota_m * 1048576;
2347 $password = (!empty($_data['password'])) ? $_data['password'] : null;
2348 $password2 = (!empty($_data['password2'])) ? $_data['password2'] : null;
2349 }
2350 else {
2351 $_SESSION['return'][] = array(
2352 'type' => 'danger',
2353 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2354 'msg' => 'access_denied'
2355 );
2356 continue;
2357 }
2358 // if already 0 == ok
2359 if ((!isset($_SESSION['acl']['unlimited_quota']) || $_SESSION['acl']['unlimited_quota'] != "1") && ($quota_m == 0 && $is_now['quota'] != 0)) {
2360 $_SESSION['return'][] = array(
2361 'type' => 'danger',
2362 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2363 'msg' => 'unlimited_quota_acl'
2364 );
2365 return false;
2366 }
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01002367 if (!hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $domain)) {
2368 $_SESSION['return'][] = array(
2369 'type' => 'danger',
2370 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2371 'msg' => 'access_denied'
2372 );
2373 continue;
2374 }
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02002375 $DomainData = mailbox('get', 'domain_details', $domain);
2376 if ($quota_m > ($is_now['max_new_quota'] / 1048576)) {
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01002377 $_SESSION['return'][] = array(
2378 'type' => 'danger',
2379 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2380 'msg' => array('mailbox_quota_left_exceeded', ($is_now['max_new_quota'] / 1048576))
2381 );
2382 continue;
2383 }
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02002384 if ($quota_m > $DomainData['max_quota_for_mbox']) {
2385 $_SESSION['return'][] = array(
2386 'type' => 'danger',
2387 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2388 'msg' => array('mailbox_quota_exceeded', $DomainData['max_quota_for_mbox'])
2389 );
2390 continue;
2391 }
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01002392 $extra_acls = array();
2393 if (isset($_data['extended_sender_acl'])) {
2394 if (!isset($_SESSION['acl']['extend_sender_acl']) || $_SESSION['acl']['extend_sender_acl'] != "1" ) {
2395 $_SESSION['return'][] = array(
2396 'type' => 'danger',
2397 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2398 'msg' => 'access_denied'
2399 );
2400 return false;
2401 }
2402 $extra_acls = array_map('trim', preg_split( "/( |,|;|\n)/", $_data['extended_sender_acl']));
2403 foreach ($extra_acls as $i => &$extra_acl) {
2404 if (empty($extra_acl)) {
2405 continue;
2406 }
2407 if (substr($extra_acl, 0, 1) === "@") {
2408 $extra_acl = ltrim($extra_acl, '@');
2409 }
2410 if (!filter_var($extra_acl, FILTER_VALIDATE_EMAIL) && !is_valid_domain_name($extra_acl)) {
2411 $_SESSION['return'][] = array(
2412 'type' => 'danger',
2413 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2414 'msg' => array('extra_acl_invalid', htmlspecialchars($extra_acl))
2415 );
2416 unset($extra_acls[$i]);
2417 continue;
2418 }
2419 $domains = array_merge(mailbox('get', 'domains'), mailbox('get', 'alias_domains'));
2420 if (filter_var($extra_acl, FILTER_VALIDATE_EMAIL)) {
2421 $extra_acl_domain = idn_to_ascii(substr(strstr($extra_acl, '@'), 1), 0, INTL_IDNA_VARIANT_UTS46);
2422 if (in_array($extra_acl_domain, $domains)) {
2423 $_SESSION['return'][] = array(
2424 'type' => 'danger',
2425 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2426 'msg' => array('extra_acl_invalid_domain', $extra_acl_domain)
2427 );
2428 unset($extra_acls[$i]);
2429 continue;
2430 }
2431 }
2432 else {
2433 if (in_array($extra_acl, $domains)) {
2434 $_SESSION['return'][] = array(
2435 'type' => 'danger',
2436 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2437 'msg' => array('extra_acl_invalid_domain', $extra_acl_domain)
2438 );
2439 unset($extra_acls[$i]);
2440 continue;
2441 }
2442 $extra_acl = '@' . $extra_acl;
2443 }
2444 }
2445 $extra_acls = array_filter($extra_acls);
2446 $extra_acls = array_values($extra_acls);
2447 $extra_acls = array_unique($extra_acls);
2448 $stmt = $pdo->prepare("DELETE FROM `sender_acl` WHERE `external` = 1 AND `logged_in_as` = :username");
2449 $stmt->execute(array(
2450 ':username' => $username
2451 ));
2452 foreach ($extra_acls as $sender_acl_external) {
2453 $stmt = $pdo->prepare("INSERT INTO `sender_acl` (`send_as`, `logged_in_as`, `external`)
2454 VALUES (:sender_acl, :username, 1)");
2455 $stmt->execute(array(
2456 ':sender_acl' => $sender_acl_external,
2457 ':username' => $username
2458 ));
2459 }
2460 }
2461 if (isset($_data['sender_acl'])) {
2462 // Get sender_acl items set by admin
2463 $sender_acl_admin = array_merge(
2464 mailbox('get', 'sender_acl_handles', $username)['sender_acl_domains']['ro'],
2465 mailbox('get', 'sender_acl_handles', $username)['sender_acl_addresses']['ro']
2466 );
2467 // Get sender_acl items from POST array
2468 // Set sender_acl_domain_admin to empty array if sender_acl contains "default" to trigger a reset
2469 // Delete records from sender_acl if sender_acl contains "*" and set to array("*")
2470 $_data['sender_acl'] = (array)$_data['sender_acl'];
2471 if (in_array("*", $_data['sender_acl'])) {
2472 $sender_acl_domain_admin = array('*');
2473 }
2474 elseif (array("default") === $_data['sender_acl']) {
2475 $sender_acl_domain_admin = array();
2476 }
2477 else {
2478 if (array_search('default', $_data['sender_acl']) !== false){
2479 unset($_data['sender_acl'][array_search('default', $_data['sender_acl'])]);
2480 }
2481 $sender_acl_domain_admin = $_data['sender_acl'];
2482 }
2483 if (!empty($sender_acl_domain_admin) || !empty($sender_acl_admin)) {
2484 // Check items in POST array and skip invalid
2485 foreach ($sender_acl_domain_admin as $key => $val) {
2486 // Check for invalid domain or email format or not *
2487 if (!filter_var($val, FILTER_VALIDATE_EMAIL) && !is_valid_domain_name(ltrim($val, '@')) && $val != '*') {
2488 $_SESSION['return'][] = array(
2489 'type' => 'danger',
2490 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2491 'msg' => array('sender_acl_invalid', $sender_acl_domain_admin[$key])
2492 );
2493 unset($sender_acl_domain_admin[$key]);
2494 continue;
2495 }
2496 // Check if user has domain access (if object is domain)
2497 $domain = ltrim($sender_acl_domain_admin[$key], '@');
2498 if (is_valid_domain_name($domain)) {
2499 // Check for- and skip non-mailcow domains
2500 $domains = array_merge(mailbox('get', 'domains'), mailbox('get', 'alias_domains'));
2501 if (!empty($domains)) {
2502 if (!in_array($domain, $domains)) {
2503 $_SESSION['return'][] = array(
2504 'type' => 'danger',
2505 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2506 'msg' => array('sender_acl_invalid', $sender_acl_domain_admin[$key])
2507 );
2508 unset($sender_acl_domain_admin[$key]);
2509 continue;
2510 }
2511 }
2512 if (!hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $domain)) {
2513 $_SESSION['return'][] = array(
2514 'type' => 'danger',
2515 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2516 'msg' => array('sender_acl_invalid', $sender_acl_domain_admin[$key])
2517 );
2518 unset($sender_acl_domain_admin[$key]);
2519 continue;
2520 }
2521 }
2522 // Wildcard can only be used if role == admin
2523 if ($val == '*' && $_SESSION['mailcow_cc_role'] != 'admin') {
2524 $_SESSION['return'][] = array(
2525 'type' => 'danger',
2526 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2527 'msg' => array('sender_acl_invalid', $sender_acl_domain_admin[$key])
2528 );
2529 unset($sender_acl_domain_admin[$key]);
2530 continue;
2531 }
2532 // Check if user has alias access (if object is email)
2533 if (filter_var($val, FILTER_VALIDATE_EMAIL)) {
2534 if (!hasAliasObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $val)) {
2535 $_SESSION['return'][] = array(
2536 'type' => 'danger',
2537 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2538 'msg' => array('sender_acl_invalid', $sender_acl_domain_admin[$key])
2539 );
2540 unset($sender_acl_domain_admin[$key]);
2541 continue;
2542 }
2543 }
2544 }
2545 // Merge both arrays
2546 $sender_acl_merged = array_merge($sender_acl_domain_admin, $sender_acl_admin);
2547 // If merged array still contains "*", set it as only value
2548 !in_array('*', $sender_acl_merged) ?: $sender_acl_merged = array('*');
2549 $stmt = $pdo->prepare("DELETE FROM `sender_acl` WHERE `external` = 0 AND `logged_in_as` = :username");
2550 $stmt->execute(array(
2551 ':username' => $username
2552 ));
2553 $fixed_sender_aliases = mailbox('get', 'sender_acl_handles', $username)['fixed_sender_aliases'];
2554 foreach ($sender_acl_merged as $sender_acl) {
2555 $domain = ltrim($sender_acl, '@');
2556 if (is_valid_domain_name($domain)) {
2557 $sender_acl = '@' . $domain;
2558 }
2559 // Don't add if allowed by alias
2560 if (in_array($sender_acl, $fixed_sender_aliases)) {
2561 continue;
2562 }
2563 $stmt = $pdo->prepare("INSERT INTO `sender_acl` (`send_as`, `logged_in_as`)
2564 VALUES (:sender_acl, :username)");
2565 $stmt->execute(array(
2566 ':sender_acl' => $sender_acl,
2567 ':username' => $username
2568 ));
2569 }
2570 }
2571 else {
2572 $stmt = $pdo->prepare("DELETE FROM `sender_acl` WHERE `external` = 0 AND `logged_in_as` = :username");
2573 $stmt->execute(array(
2574 ':username' => $username
2575 ));
2576 }
2577 }
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02002578 if (!empty($password)) {
2579 if (password_check($password, $password2) !== true) {
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01002580 continue;
2581 }
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02002582 $password_hashed = hash_password($password);
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01002583 $stmt = $pdo->prepare("UPDATE `mailbox` SET
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02002584 `password` = :password_hashed,
2585 `attributes` = JSON_SET(`attributes`, '$.passwd_update', NOW())
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01002586 WHERE `username` = :username");
2587 $stmt->execute(array(
2588 ':password_hashed' => $password_hashed,
2589 ':username' => $username
2590 ));
2591 }
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02002592 // 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 +01002593 $stmt = $pdo->prepare("UPDATE `alias` SET
2594 `active` = :active
2595 WHERE `address` = :address");
2596 $stmt->execute(array(
2597 ':address' => $username,
2598 ':active' => $active
2599 ));
2600 $stmt = $pdo->prepare("UPDATE `mailbox` SET
2601 `active` = :active,
2602 `name`= :name,
2603 `quota` = :quota_b,
2604 `attributes` = JSON_SET(`attributes`, '$.force_pw_update', :force_pw_update),
2605 `attributes` = JSON_SET(`attributes`, '$.sogo_access', :sogo_access),
2606 `attributes` = JSON_SET(`attributes`, '$.imap_access', :imap_access),
2607 `attributes` = JSON_SET(`attributes`, '$.pop3_access', :pop3_access),
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02002608 `attributes` = JSON_SET(`attributes`, '$.relayhost', :relayhost),
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01002609 `attributes` = JSON_SET(`attributes`, '$.smtp_access', :smtp_access)
2610 WHERE `username` = :username");
2611 $stmt->execute(array(
2612 ':active' => $active,
2613 ':name' => $name,
2614 ':quota_b' => $quota_b,
2615 ':force_pw_update' => $force_pw_update,
2616 ':sogo_access' => $sogo_access,
2617 ':imap_access' => $imap_access,
2618 ':pop3_access' => $pop3_access,
2619 ':smtp_access' => $smtp_access,
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02002620 ':relayhost' => $relayhost,
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01002621 ':username' => $username
2622 ));
2623 $_SESSION['return'][] = array(
2624 'type' => 'success',
2625 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2626 'msg' => array('mailbox_modified', $username)
2627 );
2628 }
2629 break;
2630 case 'resource':
2631 if (!is_array($_data['name'])) {
2632 $names = array();
2633 $names[] = $_data['name'];
2634 }
2635 else {
2636 $names = $_data['name'];
2637 }
2638 foreach ($names as $name) {
2639 $is_now = mailbox('get', 'resource_details', $name);
2640 if (!empty($is_now)) {
2641 $active = (isset($_data['active'])) ? intval($_data['active']) : $is_now['active'];
2642 $multiple_bookings = (isset($_data['multiple_bookings'])) ? intval($_data['multiple_bookings']) : $is_now['multiple_bookings'];
2643 $description = (!empty($_data['description'])) ? $_data['description'] : $is_now['description'];
2644 $kind = (!empty($_data['kind'])) ? $_data['kind'] : $is_now['kind'];
2645 }
2646 else {
2647 $_SESSION['return'][] = array(
2648 'type' => 'danger',
2649 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2650 'msg' => array('resource_invalid', htmlspecialchars($name))
2651 );
2652 continue;
2653 }
2654 if (!filter_var($name, FILTER_VALIDATE_EMAIL)) {
2655 $_SESSION['return'][] = array(
2656 'type' => 'danger',
2657 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2658 'msg' => array('resource_invalid', htmlspecialchars($name))
2659 );
2660 continue;
2661 }
2662 if (!isset($multiple_bookings) || $multiple_bookings < -1) {
2663 $multiple_bookings = -1;
2664 }
2665 if (empty($description)) {
2666 $_SESSION['return'][] = array(
2667 'type' => 'danger',
2668 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2669 'msg' => array('description_invalid', htmlspecialchars($name))
2670 );
2671 continue;
2672 }
2673 if ($kind != 'location' && $kind != 'group' && $kind != 'thing') {
2674 $_SESSION['return'][] = array(
2675 'type' => 'danger',
2676 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2677 'msg' => array('resource_invalid', htmlspecialchars($name))
2678 );
2679 continue;
2680 }
2681 if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $name)) {
2682 $_SESSION['return'][] = array(
2683 'type' => 'danger',
2684 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2685 'msg' => 'access_denied'
2686 );
2687 continue;
2688 }
2689 $stmt = $pdo->prepare("UPDATE `mailbox` SET
2690 `active` = :active,
2691 `name`= :description,
2692 `kind`= :kind,
2693 `multiple_bookings`= :multiple_bookings
2694 WHERE `username` = :name");
2695 $stmt->execute(array(
2696 ':active' => $active,
2697 ':description' => $description,
2698 ':multiple_bookings' => $multiple_bookings,
2699 ':kind' => $kind,
2700 ':name' => $name
2701 ));
2702 $_SESSION['return'][] = array(
2703 'type' => 'success',
2704 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
2705 'msg' => array('resource_modified', htmlspecialchars($name))
2706 );
2707 }
2708 break;
2709 }
2710 break;
2711 case 'get':
2712 switch ($_type) {
2713 case 'sender_acl_handles':
2714 if ($_SESSION['mailcow_cc_role'] != "admin" && $_SESSION['mailcow_cc_role'] != "domainadmin") {
2715 return false;
2716 }
2717 $data['sender_acl_domains']['ro'] = array();
2718 $data['sender_acl_domains']['rw'] = array();
2719 $data['sender_acl_domains']['selectable'] = array();
2720 $data['sender_acl_addresses']['ro'] = array();
2721 $data['sender_acl_addresses']['rw'] = array();
2722 $data['sender_acl_addresses']['selectable'] = array();
2723 $data['fixed_sender_aliases'] = array();
2724 $data['external_sender_aliases'] = array();
2725 // Fixed addresses
2726 $stmt = $pdo->prepare("SELECT `address` FROM `alias` WHERE `goto` REGEXP :goto AND `address` NOT LIKE '@%'");
2727 $stmt->execute(array(':goto' => '(^|,)'.$_data.'($|,)'));
2728 $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
2729 while ($row = array_shift($rows)) {
2730 $data['fixed_sender_aliases'][] = $row['address'];
2731 }
2732 $stmt = $pdo->prepare("SELECT CONCAT(`local_part`, '@', `alias_domain`.`alias_domain`) AS `alias_domain_alias` FROM `mailbox`, `alias_domain`
2733 WHERE `alias_domain`.`target_domain` = `mailbox`.`domain`
2734 AND `mailbox`.`username` = :username");
2735 $stmt->execute(array(':username' => $_data));
2736 $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
2737 while ($row = array_shift($rows)) {
2738 if (!empty($row['alias_domain_alias'])) {
2739 $data['fixed_sender_aliases'][] = $row['alias_domain_alias'];
2740 }
2741 }
2742 // External addresses
2743 $stmt = $pdo->prepare("SELECT `send_as` as `send_as_external` FROM `sender_acl` WHERE `logged_in_as` = :logged_in_as AND `external` = '1'");
2744 $stmt->execute(array(':logged_in_as' => $_data));
2745 $exernal_rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
2746 while ($row = array_shift($exernal_rows)) {
2747 if (!empty($row['send_as_external'])) {
2748 $data['external_sender_aliases'][] = $row['send_as_external'];
2749 }
2750 }
2751 // Return array $data['sender_acl_domains/addresses']['ro'] with read-only objects
2752 // Return array $data['sender_acl_domains/addresses']['rw'] with read-write objects (can be deleted)
2753 $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` = '*')");
2754 $stmt->execute(array(':logged_in_as' => $_data));
2755 $domain_rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
2756 while ($domain_row = array_shift($domain_rows)) {
2757 if (is_valid_domain_name($domain_row['send_as']) && !hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $domain_row['send_as'])) {
2758 $data['sender_acl_domains']['ro'][] = $domain_row['send_as'];
2759 continue;
2760 }
2761 if (is_valid_domain_name($domain_row['send_as']) && hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $domain_row['send_as'])) {
2762 $data['sender_acl_domains']['rw'][] = $domain_row['send_as'];
2763 continue;
2764 }
2765 if ($domain_row['send_as'] == '*' && $_SESSION['mailcow_cc_role'] != 'admin') {
2766 $data['sender_acl_domains']['ro'][] = $domain_row['send_as'];
2767 }
2768 if ($domain_row['send_as'] == '*' && $_SESSION['mailcow_cc_role'] == 'admin') {
2769 $data['sender_acl_domains']['rw'][] = $domain_row['send_as'];
2770 }
2771 }
2772 $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` != '*')");
2773 $stmt->execute(array(':logged_in_as' => $_data));
2774 $address_rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
2775 while ($address_row = array_shift($address_rows)) {
2776 if (filter_var($address_row['send_as'], FILTER_VALIDATE_EMAIL) && !hasAliasObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $address_row['send_as'])) {
2777 $data['sender_acl_addresses']['ro'][] = $address_row['send_as'];
2778 continue;
2779 }
2780 if (filter_var($address_row['send_as'], FILTER_VALIDATE_EMAIL) && hasAliasObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $address_row['send_as'])) {
2781 $data['sender_acl_addresses']['rw'][] = $address_row['send_as'];
2782 continue;
2783 }
2784 }
2785 $stmt = $pdo->prepare("SELECT `domain` FROM `domain`
2786 WHERE `domain` NOT IN (
2787 SELECT REPLACE(`send_as`, '@', '') FROM `sender_acl`
2788 WHERE `logged_in_as` = :logged_in_as1
2789 AND `external` = '0'
2790 AND `send_as` LIKE '@%')
2791 UNION
2792 SELECT '*' FROM `domain`
2793 WHERE '*' NOT IN (
2794 SELECT `send_as` FROM `sender_acl`
2795 WHERE `logged_in_as` = :logged_in_as2
2796 AND `external` = '0'
2797 )");
2798 $stmt->execute(array(
2799 ':logged_in_as1' => $_data,
2800 ':logged_in_as2' => $_data
2801 ));
2802 $rows_domain = $stmt->fetchAll(PDO::FETCH_ASSOC);
2803 while ($row_domain = array_shift($rows_domain)) {
2804 if (is_valid_domain_name($row_domain['domain']) && hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $row_domain['domain'])) {
2805 $data['sender_acl_domains']['selectable'][] = $row_domain['domain'];
2806 continue;
2807 }
2808 if ($row_domain['domain'] == '*' && $_SESSION['mailcow_cc_role'] == 'admin') {
2809 $data['sender_acl_domains']['selectable'][] = $row_domain['domain'];
2810 continue;
2811 }
2812 }
2813 $stmt = $pdo->prepare("SELECT `address` FROM `alias`
2814 WHERE `goto` != :goto
2815 AND `address` NOT IN (
2816 SELECT `send_as` FROM `sender_acl`
2817 WHERE `logged_in_as` = :logged_in_as
2818 AND `external` = '0'
2819 AND `send_as` NOT LIKE '@%')");
2820 $stmt->execute(array(
2821 ':logged_in_as' => $_data,
2822 ':goto' => $_data
2823 ));
2824 $rows_mbox = $stmt->fetchAll(PDO::FETCH_ASSOC);
2825 while ($row = array_shift($rows_mbox)) {
2826 // Aliases are not selectable
2827 if (in_array($row['address'], $data['fixed_sender_aliases'])) {
2828 continue;
2829 }
2830 if (filter_var($row['address'], FILTER_VALIDATE_EMAIL) && hasAliasObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $row['address'])) {
2831 $data['sender_acl_addresses']['selectable'][] = $row['address'];
2832 }
2833 }
2834 return $data;
2835 break;
2836 case 'mailboxes':
2837 $mailboxes = array();
2838 if (isset($_data) && !hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
2839 return false;
2840 }
2841 elseif (isset($_data) && hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02002842 $stmt = $pdo->prepare("SELECT `username` FROM `mailbox` WHERE (`kind` = '' OR `kind` = NULL) AND `domain` = :domain");
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01002843 $stmt->execute(array(
2844 ':domain' => $_data,
2845 ));
2846 $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
2847 while($row = array_shift($rows)) {
2848 $mailboxes[] = $row['username'];
2849 }
2850 }
2851 else {
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02002852 $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 +01002853 $stmt->execute(array(
2854 ':username' => $_SESSION['mailcow_cc_username'],
2855 ':role' => $_SESSION['mailcow_cc_role'],
2856 ));
2857 $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
2858 while($row = array_shift($rows)) {
2859 $mailboxes[] = $row['username'];
2860 }
2861 }
2862 return $mailboxes;
2863 break;
2864 case 'tls_policy':
2865 $attrs = array();
2866 if (isset($_data) && filter_var($_data, FILTER_VALIDATE_EMAIL)) {
2867 if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
2868 return false;
2869 }
2870 }
2871 else {
2872 $_data = $_SESSION['mailcow_cc_username'];
2873 }
2874 $stmt = $pdo->prepare("SELECT `attributes` FROM `mailbox` WHERE `username` = :username");
2875 $stmt->execute(array(':username' => $_data));
2876 $attrs = $stmt->fetch(PDO::FETCH_ASSOC);
2877 $attrs = json_decode($attrs['attributes'], true);
2878 return array(
2879 'tls_enforce_in' => $attrs['tls_enforce_in'],
2880 'tls_enforce_out' => $attrs['tls_enforce_out']
2881 );
2882 break;
2883 case 'quarantine_notification':
2884 $attrs = array();
2885 if (isset($_data) && filter_var($_data, FILTER_VALIDATE_EMAIL)) {
2886 if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
2887 return false;
2888 }
2889 }
2890 else {
2891 $_data = $_SESSION['mailcow_cc_username'];
2892 }
2893 $stmt = $pdo->prepare("SELECT `attributes` FROM `mailbox` WHERE `username` = :username");
2894 $stmt->execute(array(':username' => $_data));
2895 $attrs = $stmt->fetch(PDO::FETCH_ASSOC);
2896 $attrs = json_decode($attrs['attributes'], true);
2897 return $attrs['quarantine_notification'];
2898 break;
2899 case 'quarantine_category':
2900 $attrs = array();
2901 if (isset($_data) && filter_var($_data, FILTER_VALIDATE_EMAIL)) {
2902 if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
2903 return false;
2904 }
2905 }
2906 else {
2907 $_data = $_SESSION['mailcow_cc_username'];
2908 }
2909 $stmt = $pdo->prepare("SELECT `attributes` FROM `mailbox` WHERE `username` = :username");
2910 $stmt->execute(array(':username' => $_data));
2911 $attrs = $stmt->fetch(PDO::FETCH_ASSOC);
2912 $attrs = json_decode($attrs['attributes'], true);
2913 return $attrs['quarantine_category'];
2914 break;
2915 case 'filters':
2916 $filters = array();
2917 if (isset($_data) && filter_var($_data, FILTER_VALIDATE_EMAIL)) {
2918 if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
2919 return false;
2920 }
2921 }
2922 else {
2923 $_data = $_SESSION['mailcow_cc_username'];
2924 }
2925 $stmt = $pdo->prepare("SELECT `id` FROM `sieve_filters` WHERE `username` = :username");
2926 $stmt->execute(array(':username' => $_data));
2927 $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
2928 while($row = array_shift($rows)) {
2929 $filters[] = $row['id'];
2930 }
2931 return $filters;
2932 break;
2933 case 'global_filter_details':
2934 $global_filters = array();
2935 if ($_SESSION['mailcow_cc_role'] != "admin") {
2936 return false;
2937 }
2938 $global_filters['prefilter'] = file_get_contents('/global_sieve/before');
2939 $global_filters['postfilter'] = file_get_contents('/global_sieve/after');
2940 return $global_filters;
2941 break;
2942 case 'filter_details':
2943 $filter_details = array();
2944 if (!is_numeric($_data)) {
2945 return false;
2946 }
2947 $stmt = $pdo->prepare("SELECT CASE `script_name` WHEN 'active' THEN 1 ELSE 0 END AS `active`,
2948 id,
2949 username,
2950 filter_type,
2951 script_data,
2952 script_desc
2953 FROM `sieve_filters`
2954 WHERE `id` = :id");
2955 $stmt->execute(array(':id' => $_data));
2956 $filter_details = $stmt->fetch(PDO::FETCH_ASSOC);
2957 if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $filter_details['username'])) {
2958 return false;
2959 }
2960 return $filter_details;
2961 break;
2962 case 'active_user_sieve':
2963 $filter_details = array();
2964 if (isset($_data) && filter_var($_data, FILTER_VALIDATE_EMAIL)) {
2965 if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
2966 return false;
2967 }
2968 }
2969 else {
2970 $_data = $_SESSION['mailcow_cc_username'];
2971 }
2972 $exec_fields = array(
2973 'cmd' => 'sieve',
2974 'task' => 'list',
2975 'username' => $_data
2976 );
2977 $filters = docker('post', 'dovecot-mailcow', 'exec', $exec_fields);
2978 $filters = array_filter(preg_split("/(\r\n|\n|\r)/",$filters));
2979 foreach ($filters as $filter) {
2980 if (preg_match('/.+ ACTIVE/i', $filter)) {
2981 $exec_fields = array(
2982 'cmd' => 'sieve',
2983 'task' => 'print',
2984 'script_name' => substr($filter, 0, -7),
2985 'username' => $_data
2986 );
2987 $script = docker('post', 'dovecot-mailcow', 'exec', $exec_fields);
2988 // Remove first line
2989 return preg_replace('/^.+\n/', '', $script);
2990 }
2991 }
2992 return false;
2993 break;
2994 case 'syncjob_details':
2995 $syncjobdetails = array();
2996 if (!is_numeric($_data)) {
2997 return false;
2998 }
2999 if (isset($_extra) && in_array('no_log', $_extra)) {
3000 $field_query = $pdo->query('SHOW FIELDS FROM `imapsync` WHERE FIELD NOT IN ("returned_text", "password1")');
3001 $fields = $field_query->fetchAll(PDO::FETCH_ASSOC);
3002 while($field = array_shift($fields)) {
3003 $shown_fields[] = $field['Field'];
3004 }
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02003005 $stmt = $pdo->prepare("SELECT " . implode(',', (array)$shown_fields) . ",
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01003006 `active`
3007 FROM `imapsync` WHERE id = :id");
3008 }
3009 elseif (isset($_extra) && in_array('with_password', $_extra)) {
3010 $stmt = $pdo->prepare("SELECT *,
3011 `active`
3012 FROM `imapsync` WHERE id = :id");
3013 }
3014 else {
3015 $field_query = $pdo->query('SHOW FIELDS FROM `imapsync` WHERE FIELD NOT IN ("password1")');
3016 $fields = $field_query->fetchAll(PDO::FETCH_ASSOC);
3017 while($field = array_shift($fields)) {
3018 $shown_fields[] = $field['Field'];
3019 }
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02003020 $stmt = $pdo->prepare("SELECT " . implode(',', (array)$shown_fields) . ",
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01003021 `active`
3022 FROM `imapsync` WHERE id = :id");
3023 }
3024 $stmt->execute(array(':id' => $_data));
3025 $syncjobdetails = $stmt->fetch(PDO::FETCH_ASSOC);
3026 if (!empty($syncjobdetails['returned_text'])) {
3027 $syncjobdetails['log'] = $syncjobdetails['returned_text'];
3028 }
3029 else {
3030 $syncjobdetails['log'] = '';
3031 }
3032 unset($syncjobdetails['returned_text']);
3033 if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $syncjobdetails['user2'])) {
3034 return false;
3035 }
3036 return $syncjobdetails;
3037 break;
3038 case 'syncjobs':
3039 $syncjobdata = array();
3040 if (isset($_data) && filter_var($_data, FILTER_VALIDATE_EMAIL)) {
3041 if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
3042 return false;
3043 }
3044 }
3045 else {
3046 $_data = $_SESSION['mailcow_cc_username'];
3047 }
3048 $stmt = $pdo->prepare("SELECT `id` FROM `imapsync` WHERE `user2` = :username");
3049 $stmt->execute(array(':username' => $_data));
3050 $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
3051 while($row = array_shift($rows)) {
3052 $syncjobdata[] = $row['id'];
3053 }
3054 return $syncjobdata;
3055 break;
3056 case 'spam_score':
3057 $curl = curl_init();
3058 curl_setopt($curl, CURLOPT_UNIX_SOCKET_PATH, '/var/lib/rspamd/rspamd.sock');
3059 curl_setopt($curl, CURLOPT_URL,"http://rspamd/actions");
3060 curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
3061 $default_actions = curl_exec($curl);
3062 if (!curl_errno($curl)) {
3063 $data_array = json_decode($default_actions, true);
3064 curl_close($curl);
3065 foreach ($data_array as $data) {
3066 if ($data['action'] == 'reject') {
3067 $reject = $data['value'];
3068 continue;
3069 }
3070 elseif ($data['action'] == 'add header') {
3071 $add_header = $data['value'];
3072 continue;
3073 }
3074 }
3075 if (empty($add_header) || empty($reject)) {
3076 // Assume default, set warning
3077 $default = "5, 15";
3078 $_SESSION['return'][] = array(
3079 'type' => 'warning',
3080 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
3081 'msg' => 'Could not determine servers default spam score, assuming default'
3082 );
3083 }
3084 else {
3085 $default = $add_header . ', ' . $reject;
3086 }
3087 }
3088 else {
3089 // Assume default, set warning
3090 $default = "5, 15";
3091 $_SESSION['return'][] = array(
3092 'type' => 'warning',
3093 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
3094 'msg' => 'Could not determine servers default spam score, assuming default'
3095 );
3096 }
3097 curl_close($curl);
3098 $policydata = array();
3099 if (isset($_data) && filter_var($_data, FILTER_VALIDATE_EMAIL)) {
3100 if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
3101 return false;
3102 }
3103 }
3104 else {
3105 $_data = $_SESSION['mailcow_cc_username'];
3106 }
3107 $stmt = $pdo->prepare("SELECT `value` FROM `filterconf` WHERE `object` = :username AND
3108 (`option` = 'lowspamlevel' OR `option` = 'highspamlevel')");
3109 $stmt->execute(array(':username' => $_data));
3110 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
3111 if (empty($num_results)) {
3112 return $default;
3113 }
3114 else {
3115 $stmt = $pdo->prepare("SELECT `value` FROM `filterconf` WHERE `option` = 'highspamlevel' AND `object` = :username");
3116 $stmt->execute(array(':username' => $_data));
3117 $highspamlevel = $stmt->fetch(PDO::FETCH_ASSOC);
3118 $stmt = $pdo->prepare("SELECT `value` FROM `filterconf` WHERE `option` = 'lowspamlevel' AND `object` = :username");
3119 $stmt->execute(array(':username' => $_data));
3120 $lowspamlevel = $stmt->fetch(PDO::FETCH_ASSOC);
3121 return $lowspamlevel['value'].', '.$highspamlevel['value'];
3122 }
3123 break;
3124 case 'time_limited_aliases':
3125 $tladata = array();
3126 if (isset($_data) && filter_var($_data, FILTER_VALIDATE_EMAIL)) {
3127 if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
3128 return false;
3129 }
3130 }
3131 else {
3132 $_data = $_SESSION['mailcow_cc_username'];
3133 }
3134 $stmt = $pdo->prepare("SELECT `address`,
3135 `goto`,
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02003136 `validity`,
3137 `created`,
3138 `modified`
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01003139 FROM `spamalias`
3140 WHERE `goto` = :username
3141 AND `validity` >= :unixnow");
3142 $stmt->execute(array(':username' => $_data, ':unixnow' => time()));
3143 $tladata = $stmt->fetchAll(PDO::FETCH_ASSOC);
3144 return $tladata;
3145 break;
3146 case 'delimiter_action':
3147 $policydata = array();
3148 if (isset($_data) && filter_var($_data, FILTER_VALIDATE_EMAIL)) {
3149 if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
3150 return false;
3151 }
3152 }
3153 else {
3154 $_data = $_SESSION['mailcow_cc_username'];
3155 }
3156 try {
3157 if ($redis->hGet('RCPT_WANTS_SUBJECT_TAG', $_data)) {
3158 return "subject";
3159 }
3160 elseif ($redis->hGet('RCPT_WANTS_SUBFOLDER_TAG', $_data)) {
3161 return "subfolder";
3162 }
3163 else {
3164 return "none";
3165 }
3166 }
3167 catch (RedisException $e) {
3168 $_SESSION['return'][] = array(
3169 'type' => 'danger',
3170 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
3171 'msg' => array('redis_error', $e)
3172 );
3173 return false;
3174 }
3175 break;
3176 case 'resources':
3177 $resources = array();
3178 if (isset($_data) && !hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
3179 return false;
3180 }
3181 elseif (isset($_data) && hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
3182 $stmt = $pdo->prepare("SELECT `username` FROM `mailbox` WHERE `kind` REGEXP 'location|thing|group' AND `domain` = :domain");
3183 $stmt->execute(array(
3184 ':domain' => $_data,
3185 ));
3186 $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
3187 while($row = array_shift($rows)) {
3188 $resources[] = $row['username'];
3189 }
3190 }
3191 else {
3192 $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");
3193 $stmt->execute(array(
3194 ':username' => $_SESSION['mailcow_cc_username'],
3195 ':role' => $_SESSION['mailcow_cc_role'],
3196 ));
3197 $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
3198 while($row = array_shift($rows)) {
3199 $resources[] = $row['username'];
3200 }
3201 }
3202 return $resources;
3203 break;
3204 case 'alias_domains':
3205 $aliasdomains = array();
3206 if (isset($_data) && !hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
3207 return false;
3208 }
3209 elseif (isset($_data) && hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
3210 $stmt = $pdo->prepare("SELECT `alias_domain` FROM `alias_domain` WHERE `target_domain` = :domain");
3211 $stmt->execute(array(
3212 ':domain' => $_data,
3213 ));
3214 $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
3215 while($row = array_shift($rows)) {
3216 $aliasdomains[] = $row['alias_domain'];
3217 }
3218 }
3219 else {
3220 $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");
3221 $stmt->execute(array(
3222 ':username' => $_SESSION['mailcow_cc_username'],
3223 ':role' => $_SESSION['mailcow_cc_role'],
3224 ));
3225 $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
3226 while($row = array_shift($rows)) {
3227 $aliasdomains[] = $row['alias_domain'];
3228 }
3229 }
3230 return $aliasdomains;
3231 break;
3232 case 'aliases':
3233 $aliases = array();
3234 if (!hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
3235 return false;
3236 }
3237 $stmt = $pdo->prepare("SELECT `id` FROM `alias` WHERE `address` != `goto` AND `domain` = :domain");
3238 $stmt->execute(array(
3239 ':domain' => $_data,
3240 ));
3241 $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
3242 while($row = array_shift($rows)) {
3243 $aliases[] = $row['id'];
3244 }
3245 return $aliases;
3246 break;
3247 case 'alias_details':
3248 $aliasdata = array();
3249 $stmt = $pdo->prepare("SELECT
3250 `id`,
3251 `domain`,
3252 `goto`,
3253 `address`,
3254 `public_comment`,
3255 `private_comment`,
3256 `active`,
3257 `sogo_visible`,
3258 `created`,
3259 `modified`
3260 FROM `alias`
3261 WHERE (`id` = :id OR `address` = :address) AND `address` != `goto`");
3262 $stmt->execute(array(
3263 ':id' => $_data,
3264 ':address' => $_data,
3265 ));
3266 $row = $stmt->fetch(PDO::FETCH_ASSOC);
3267 $stmt = $pdo->prepare("SELECT `target_domain` FROM `alias_domain` WHERE `alias_domain` = :domain");
3268 $stmt->execute(array(
3269 ':domain' => $row['domain'],
3270 ));
3271 $row_alias_domain = $stmt->fetch(PDO::FETCH_ASSOC);
3272 if (isset($row_alias_domain['target_domain']) && !empty($row_alias_domain['target_domain'])) {
3273 $aliasdata['in_primary_domain'] = $row_alias_domain['target_domain'];
3274 }
3275 else {
3276 $aliasdata['in_primary_domain'] = "";
3277 }
3278 $aliasdata['id'] = $row['id'];
3279 $aliasdata['domain'] = $row['domain'];
3280 $aliasdata['public_comment'] = $row['public_comment'];
3281 $aliasdata['private_comment'] = $row['private_comment'];
3282 $aliasdata['domain'] = $row['domain'];
3283 $aliasdata['goto'] = $row['goto'];
3284 $aliasdata['address'] = $row['address'];
3285 (!filter_var($aliasdata['address'], FILTER_VALIDATE_EMAIL)) ? $aliasdata['is_catch_all'] = 1 : $aliasdata['is_catch_all'] = 0;
3286 $aliasdata['active'] = $row['active'];
3287 $aliasdata['active_int'] = $row['active'];
3288 $aliasdata['sogo_visible'] = $row['sogo_visible'];
3289 $aliasdata['sogo_visible_int'] = $row['sogo_visible'];
3290 $aliasdata['created'] = $row['created'];
3291 $aliasdata['modified'] = $row['modified'];
3292 if (!hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $aliasdata['domain'])) {
3293 return false;
3294 }
3295 return $aliasdata;
3296 break;
3297 case 'alias_domain_details':
3298 $aliasdomaindata = array();
3299 $rl = ratelimit('get', 'domain', $_data);
3300 $stmt = $pdo->prepare("SELECT
3301 `alias_domain`,
3302 `target_domain`,
3303 `active`,
3304 `created`,
3305 `modified`
3306 FROM `alias_domain`
3307 WHERE `alias_domain` = :aliasdomain");
3308 $stmt->execute(array(
3309 ':aliasdomain' => $_data,
3310 ));
3311 $row = $stmt->fetch(PDO::FETCH_ASSOC);
3312 $stmt = $pdo->prepare("SELECT `backupmx` FROM `domain` WHERE `domain` = :target_domain");
3313 $stmt->execute(array(
3314 ':target_domain' => $row['target_domain']
3315 ));
3316 $row_parent = $stmt->fetch(PDO::FETCH_ASSOC);
3317 $aliasdomaindata['alias_domain'] = $row['alias_domain'];
3318 $aliasdomaindata['parent_is_backupmx'] = $row_parent['backupmx'];
3319 $aliasdomaindata['target_domain'] = $row['target_domain'];
3320 $aliasdomaindata['active'] = $row['active'];
3321 $aliasdomaindata['active_int'] = $row['active'];
3322 $aliasdomaindata['rl'] = $rl;
3323 $aliasdomaindata['created'] = $row['created'];
3324 $aliasdomaindata['modified'] = $row['modified'];
3325 if (!hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $aliasdomaindata['target_domain'])) {
3326 return false;
3327 }
3328 return $aliasdomaindata;
3329 break;
3330 case 'domains':
3331 $domains = array();
3332 if ($_SESSION['mailcow_cc_role'] != "admin" && $_SESSION['mailcow_cc_role'] != "domainadmin") {
3333 return false;
3334 }
3335 $stmt = $pdo->prepare("SELECT `domain` FROM `domain`
3336 WHERE (`domain` IN (
3337 SELECT `domain` from `domain_admins`
3338 WHERE (`active`='1' AND `username` = :username))
3339 )
3340 OR 'admin'= :role");
3341 $stmt->execute(array(
3342 ':username' => $_SESSION['mailcow_cc_username'],
3343 ':role' => $_SESSION['mailcow_cc_role'],
3344 ));
3345 $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
3346 while($row = array_shift($rows)) {
3347 $domains[] = $row['domain'];
3348 }
3349 return $domains;
3350 break;
3351 case 'domain_details':
3352 $domaindata = array();
3353 $_data = idn_to_ascii(strtolower(trim($_data)), 0, INTL_IDNA_VARIANT_UTS46);
3354 if (!hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
3355 return false;
3356 }
3357 $stmt = $pdo->prepare("SELECT `target_domain` FROM `alias_domain` WHERE `alias_domain` = :domain");
3358 $stmt->execute(array(
3359 ':domain' => $_data
3360 ));
3361 $row = $stmt->fetch(PDO::FETCH_ASSOC);
3362 if (!empty($row)) {
3363 $_data = $row['target_domain'];
3364 }
3365 $stmt = $pdo->prepare("SELECT
3366 `domain`,
3367 `description`,
3368 `aliases`,
3369 `mailboxes`,
3370 `defquota`,
3371 `maxquota`,
3372 `quota`,
3373 `relayhost`,
3374 `relay_all_recipients`,
3375 `relay_unknown_only`,
3376 `backupmx`,
3377 `gal`,
3378 `active`
3379 FROM `domain` WHERE `domain`= :domain");
3380 $stmt->execute(array(
3381 ':domain' => $_data
3382 ));
3383 $row = $stmt->fetch(PDO::FETCH_ASSOC);
3384 if (empty($row)) {
3385 return false;
3386 }
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02003387 $stmt = $pdo->prepare("SELECT COUNT(`username`) AS `count`,
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01003388 COALESCE(SUM(`quota`), 0) AS `in_use`
3389 FROM `mailbox`
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02003390 WHERE (`kind` = '' OR `kind` = NULL)
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01003391 AND `domain` = :domain");
3392 $stmt->execute(array(':domain' => $row['domain']));
3393 $MailboxDataDomain = $stmt->fetch(PDO::FETCH_ASSOC);
3394 $stmt = $pdo->prepare("SELECT SUM(bytes) AS `bytes_total`, SUM(messages) AS `msgs_total` FROM `quota2`
3395 WHERE `username` IN (
3396 SELECT `username` FROM `mailbox`
3397 WHERE `domain` = :domain
3398 );");
3399 $stmt->execute(array(':domain' => $row['domain']));
3400 $SumQuotaInUse = $stmt->fetch(PDO::FETCH_ASSOC);
3401 $rl = ratelimit('get', 'domain', $_data);
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02003402 $domaindata['max_new_mailbox_quota'] = ($row['quota'] * 1048576) - $MailboxDataDomain['in_use'];
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01003403 if ($domaindata['max_new_mailbox_quota'] > ($row['maxquota'] * 1048576)) {
3404 $domaindata['max_new_mailbox_quota'] = ($row['maxquota'] * 1048576);
3405 }
3406 $domaindata['def_new_mailbox_quota'] = $domaindata['max_new_mailbox_quota'];
3407 if ($domaindata['def_new_mailbox_quota'] > ($row['defquota'] * 1048576)) {
3408 $domaindata['def_new_mailbox_quota'] = ($row['defquota'] * 1048576);
3409 }
3410 $domaindata['quota_used_in_domain'] = $MailboxDataDomain['in_use'];
3411 if (!empty($SumQuotaInUse['bytes_total'])) {
3412 $domaindata['bytes_total'] = $SumQuotaInUse['bytes_total'];
3413 }
3414 else {
3415 $domaindata['bytes_total'] = 0;
3416 }
3417 if (!empty($SumQuotaInUse['msgs_total'])) {
3418 $domaindata['msgs_total'] = $SumQuotaInUse['msgs_total'];
3419 }
3420 else {
3421 $domaindata['msgs_total'] = 0;
3422 }
3423 $domaindata['mboxes_in_domain'] = $MailboxDataDomain['count'];
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02003424 $domaindata['mboxes_left'] = $row['mailboxes'] - $MailboxDataDomain['count'];
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01003425 $domaindata['domain_name'] = $row['domain'];
3426 $domaindata['description'] = $row['description'];
3427 $domaindata['max_num_aliases_for_domain'] = $row['aliases'];
3428 $domaindata['max_num_mboxes_for_domain'] = $row['mailboxes'];
3429 $domaindata['def_quota_for_mbox'] = $row['defquota'] * 1048576;
3430 $domaindata['max_quota_for_mbox'] = $row['maxquota'] * 1048576;
3431 $domaindata['max_quota_for_domain'] = $row['quota'] * 1048576;
3432 $domaindata['relayhost'] = $row['relayhost'];
3433 $domaindata['backupmx'] = $row['backupmx'];
3434 $domaindata['backupmx_int'] = $row['backupmx'];
3435 $domaindata['gal'] = $row['gal'];
3436 $domaindata['gal_int'] = $row['gal'];
3437 $domaindata['rl'] = $rl;
3438 $domaindata['active'] = $row['active'];
3439 $domaindata['active_int'] = $row['active'];
3440 $domaindata['relay_all_recipients'] = $row['relay_all_recipients'];
3441 $domaindata['relay_all_recipients_int'] = $row['relay_all_recipients'];
3442 $domaindata['relay_unknown_only'] = $row['relay_unknown_only'];
3443 $domaindata['relay_unknown_only_int'] = $row['relay_unknown_only'];
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02003444 $stmt = $pdo->prepare("SELECT COUNT(`address`) AS `alias_count` FROM `alias`
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01003445 WHERE (`domain`= :domain OR `domain` IN (SELECT `alias_domain` FROM `alias_domain` WHERE `target_domain` = :domain2))
3446 AND `address` NOT IN (
3447 SELECT `username` FROM `mailbox`
3448 )");
3449 $stmt->execute(array(
3450 ':domain' => $_data,
3451 ':domain2' => $_data
3452 ));
3453 $AliasDataDomain = $stmt->fetch(PDO::FETCH_ASSOC);
3454 (isset($AliasDataDomain['alias_count'])) ? $domaindata['aliases_in_domain'] = $AliasDataDomain['alias_count'] : $domaindata['aliases_in_domain'] = "0";
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02003455 $domaindata['aliases_left'] = $row['aliases'] - $AliasDataDomain['alias_count'];
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01003456 if ($_SESSION['mailcow_cc_role'] == "admin")
3457 {
3458 $stmt = $pdo->prepare("SELECT GROUP_CONCAT(`username` SEPARATOR ', ') AS domain_admins FROM `domain_admins` WHERE `domain` = :domain");
3459 $stmt->execute(array(
3460 ':domain' => $_data
3461 ));
3462 $domain_admins = $stmt->fetch(PDO::FETCH_ASSOC);
3463 (isset($domain_admins['domain_admins'])) ? $domaindata['domain_admins'] = $domain_admins['domain_admins'] : $domaindata['domain_admins'] = "-";
3464 }
3465 return $domaindata;
3466 break;
3467 case 'mailbox_details':
3468 if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
3469 return false;
3470 }
3471 $mailboxdata = array();
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01003472 if (preg_match('/y|yes/i', getenv('MASTER'))) {
3473 $stmt = $pdo->prepare("SELECT
3474 `domain`.`backupmx`,
3475 `mailbox`.`username`,
3476 `mailbox`.`name`,
3477 `mailbox`.`active`,
3478 `mailbox`.`domain`,
3479 `mailbox`.`local_part`,
3480 `mailbox`.`quota`,
3481 `quota2`.`bytes`,
3482 `attributes`,
3483 `quota2`.`messages`
3484 FROM `mailbox`, `quota2`, `domain`
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02003485 WHERE (`mailbox`.`kind` = '' OR `mailbox`.`kind` = NULL)
3486 AND `mailbox`.`username` = `quota2`.`username`
3487 AND `domain`.`domain` = `mailbox`.`domain`
3488 AND `mailbox`.`username` = :mailbox");
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01003489 }
3490 else {
3491 $stmt = $pdo->prepare("SELECT
3492 `domain`.`backupmx`,
3493 `mailbox`.`username`,
3494 `mailbox`.`name`,
3495 `mailbox`.`active`,
3496 `mailbox`.`domain`,
3497 `mailbox`.`local_part`,
3498 `mailbox`.`quota`,
3499 `quota2replica`.`bytes`,
3500 `attributes`,
3501 `quota2replica`.`messages`
3502 FROM `mailbox`, `quota2replica`, `domain`
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02003503 WHERE (`mailbox`.`kind` = '' OR `mailbox`.`kind` = NULL)
3504 AND `mailbox`.`username` = `quota2replica`.`username`
3505 AND `domain`.`domain` = `mailbox`.`domain`
3506 AND `mailbox`.`username` = :mailbox");
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01003507 }
3508 $stmt->execute(array(
3509 ':mailbox' => $_data,
3510 ));
3511 $row = $stmt->fetch(PDO::FETCH_ASSOC);
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02003512
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01003513 $mailboxdata['username'] = $row['username'];
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01003514 $mailboxdata['active'] = $row['active'];
3515 $mailboxdata['active_int'] = $row['active'];
3516 $mailboxdata['domain'] = $row['domain'];
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02003517 $mailboxdata['relayhost'] = $row['relayhost'];
3518 $mailboxdata['name'] = $row['name'];
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01003519 $mailboxdata['local_part'] = $row['local_part'];
3520 $mailboxdata['quota'] = $row['quota'];
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02003521 $mailboxdata['messages'] = $row['messages'];
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01003522 $mailboxdata['attributes'] = json_decode($row['attributes'], true);
3523 $mailboxdata['quota_used'] = intval($row['bytes']);
3524 $mailboxdata['percent_in_use'] = ($row['quota'] == 0) ? '- ' : round((intval($row['bytes']) / intval($row['quota'])) * 100);
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02003525
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01003526 if ($mailboxdata['percent_in_use'] === '- ') {
3527 $mailboxdata['percent_class'] = "info";
3528 }
3529 elseif ($mailboxdata['percent_in_use'] >= 90) {
3530 $mailboxdata['percent_class'] = "danger";
3531 }
3532 elseif ($mailboxdata['percent_in_use'] >= 75) {
3533 $mailboxdata['percent_class'] = "warning";
3534 }
3535 else {
3536 $mailboxdata['percent_class'] = "success";
3537 }
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02003538
3539 // Determine last logins
3540 $stmt = $pdo->prepare("SELECT MAX(`datetime`) AS `datetime`, `service` FROM `sasl_log`
3541 WHERE `username` = :mailbox
3542 GROUP BY `service` DESC");
3543 $stmt->execute(array(':mailbox' => $_data));
3544 $SaslLogsData = $stmt->fetchAll(PDO::FETCH_ASSOC);
3545 foreach ($SaslLogsData as $SaslLogs) {
3546 if ($SaslLogs['service'] == 'imap') {
3547 $last_imap_login = strtotime($SaslLogs['datetime']);
3548 }
3549 else if ($SaslLogs['service'] == 'smtp') {
3550 $last_smtp_login = strtotime($SaslLogs['datetime']);
3551 }
3552 else if ($SaslLogs['service'] == 'pop3') {
3553 $last_pop3_login = strtotime($SaslLogs['datetime']);
3554 }
3555 }
3556 if (!isset($last_imap_login) || $GLOBALS['SHOW_LAST_LOGIN'] === false) {
3557 $last_imap_login = 0;
3558 }
3559 if (!isset($last_smtp_login) || $GLOBALS['SHOW_LAST_LOGIN'] === false) {
3560 $last_smtp_login = 0;
3561 }
3562 if (!isset($last_pop3_login) || $GLOBALS['SHOW_LAST_LOGIN'] === false) {
3563 $last_pop3_login = 0;
3564 }
3565 $mailboxdata['last_imap_login'] = $last_imap_login;
3566 $mailboxdata['last_smtp_login'] = $last_smtp_login;
3567 $mailboxdata['last_pop3_login'] = $last_pop3_login;
3568
3569 if (!isset($_extra) || $_extra != 'reduced') {
3570 $rl = ratelimit('get', 'mailbox', $_data);
3571 $stmt = $pdo->prepare("SELECT `maxquota`, `quota` FROM `domain` WHERE `domain` = :domain");
3572 $stmt->execute(array(':domain' => $row['domain']));
3573 $DomainQuota = $stmt->fetch(PDO::FETCH_ASSOC);
3574
3575 $stmt = $pdo->prepare("SELECT IFNULL(COUNT(`active`), 0) AS `pushover_active` FROM `pushover` WHERE `username` = :username AND `active` = 1");
3576 $stmt->execute(array(':username' => $_data));
3577 $PushoverActive = $stmt->fetch(PDO::FETCH_ASSOC);
3578 $stmt = $pdo->prepare("SELECT COALESCE(SUM(`quota`), 0) as `in_use` FROM `mailbox` WHERE (`kind` = '' OR `kind` = NULL) AND `domain` = :domain AND `username` != :username");
3579 $stmt->execute(array(':domain' => $row['domain'], ':username' => $_data));
3580 $MailboxUsage = $stmt->fetch(PDO::FETCH_ASSOC);
3581 $stmt = $pdo->prepare("SELECT IFNULL(COUNT(`address`), 0) AS `sa_count` FROM `spamalias` WHERE `goto` = :address AND `validity` >= :unixnow");
3582 $stmt->execute(array(':address' => $_data, ':unixnow' => time()));
3583 $SpamaliasUsage = $stmt->fetch(PDO::FETCH_ASSOC);
3584 $mailboxdata['max_new_quota'] = ($DomainQuota['quota'] * 1048576) - $MailboxUsage['in_use'];
3585 $mailboxdata['spam_aliases'] = $SpamaliasUsage['sa_count'];
3586 $mailboxdata['pushover_active'] = ($PushoverActive['pushover_active'] == 1) ? 1 : 0;
3587 if ($mailboxdata['max_new_quota'] > ($DomainQuota['maxquota'] * 1048576)) {
3588 $mailboxdata['max_new_quota'] = ($DomainQuota['maxquota'] * 1048576);
3589 }
3590 if (!empty($rl)) {
3591 $mailboxdata['rl'] = $rl;
3592 $mailboxdata['rl_scope'] = 'mailbox';
3593 }
3594 else {
3595 $mailboxdata['rl'] = ratelimit('get', 'domain', $row['domain']);
3596 $mailboxdata['rl_scope'] = 'domain';
3597 }
3598 $mailboxdata['is_relayed'] = $row['backupmx'];
3599 }
3600
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01003601 return $mailboxdata;
3602 break;
3603 case 'resource_details':
3604 $resourcedata = array();
3605 if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
3606 return false;
3607 }
3608 $stmt = $pdo->prepare("SELECT
3609 `username`,
3610 `name`,
3611 `kind`,
3612 `multiple_bookings`,
3613 `local_part`,
3614 `active`,
3615 `domain`
3616 FROM `mailbox` WHERE `kind` REGEXP 'location|thing|group' AND `username` = :resource");
3617 $stmt->execute(array(
3618 ':resource' => $_data,
3619 ));
3620 $row = $stmt->fetch(PDO::FETCH_ASSOC);
3621 $resourcedata['name'] = $row['username'];
3622 $resourcedata['kind'] = $row['kind'];
3623 $resourcedata['multiple_bookings'] = $row['multiple_bookings'];
3624 $resourcedata['description'] = $row['name'];
3625 $resourcedata['active'] = $row['active'];
3626 $resourcedata['active_int'] = $row['active'];
3627 $resourcedata['domain'] = $row['domain'];
3628 $resourcedata['local_part'] = $row['local_part'];
3629 if (!isset($resourcedata['domain']) ||
3630 (isset($resourcedata['domain']) && !hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $resourcedata['domain']))) {
3631 return false;
3632 }
3633 return $resourcedata;
3634 break;
3635 }
3636 break;
3637 case 'delete':
3638 switch ($_type) {
3639 case 'syncjob':
3640 if (!is_array($_data['id'])) {
3641 $ids = array();
3642 $ids[] = $_data['id'];
3643 }
3644 else {
3645 $ids = $_data['id'];
3646 }
3647 if (!isset($_SESSION['acl']['syncjobs']) || $_SESSION['acl']['syncjobs'] != "1" ) {
3648 $_SESSION['return'][] = array(
3649 'type' => 'danger',
3650 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
3651 'msg' => 'access_denied'
3652 );
3653 return false;
3654 }
3655 foreach ($ids as $id) {
3656 if (!is_numeric($id)) {
3657 return false;
3658 }
3659 $stmt = $pdo->prepare("SELECT `user2` FROM `imapsync` WHERE id = :id");
3660 $stmt->execute(array(':id' => $id));
3661 $user2 = $stmt->fetch(PDO::FETCH_ASSOC)['user2'];
3662 if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $user2)) {
3663 $_SESSION['return'][] = array(
3664 'type' => 'danger',
3665 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
3666 'msg' => 'access_denied'
3667 );
3668 continue;
3669 }
3670 $stmt = $pdo->prepare("DELETE FROM `imapsync` WHERE `id`= :id");
3671 $stmt->execute(array(':id' => $id));
3672 $_SESSION['return'][] = array(
3673 'type' => 'success',
3674 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
3675 'msg' => array('deleted_syncjob', $id)
3676 );
3677 }
3678 break;
3679 case 'filter':
3680 if (!is_array($_data['id'])) {
3681 $ids = array();
3682 $ids[] = $_data['id'];
3683 }
3684 else {
3685 $ids = $_data['id'];
3686 }
3687 if (!isset($_SESSION['acl']['filters']) || $_SESSION['acl']['filters'] != "1" ) {
3688 $_SESSION['return'][] = array(
3689 'type' => 'danger',
3690 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
3691 'msg' => 'access_denied'
3692 );
3693 return false;
3694 }
3695 foreach ($ids as $id) {
3696 if (!is_numeric($id)) {
3697 continue;
3698 }
3699 $stmt = $pdo->prepare("SELECT `username` FROM `sieve_filters` WHERE id = :id");
3700 $stmt->execute(array(':id' => $id));
3701 $usr = $stmt->fetch(PDO::FETCH_ASSOC)['username'];
3702 if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $usr)) {
3703 $_SESSION['return'][] = array(
3704 'type' => 'danger',
3705 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
3706 'msg' => 'access_denied'
3707 );
3708 continue;
3709 }
3710 $stmt = $pdo->prepare("DELETE FROM `sieve_filters` WHERE `id`= :id");
3711 $stmt->execute(array(':id' => $id));
3712 $_SESSION['return'][] = array(
3713 'type' => 'success',
3714 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
3715 'msg' => array('delete_filter', $id)
3716 );
3717 }
3718 break;
3719 case 'time_limited_alias':
3720 if (!is_array($_data['address'])) {
3721 $addresses = array();
3722 $addresses[] = $_data['address'];
3723 }
3724 else {
3725 $addresses = $_data['address'];
3726 }
3727 if (!isset($_SESSION['acl']['spam_alias']) || $_SESSION['acl']['spam_alias'] != "1" ) {
3728 $_SESSION['return'][] = array(
3729 'type' => 'danger',
3730 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
3731 'msg' => 'access_denied'
3732 );
3733 return false;
3734 }
3735 foreach ($addresses as $address) {
3736 $stmt = $pdo->prepare("SELECT `goto` FROM `spamalias` WHERE `address` = :address");
3737 $stmt->execute(array(':address' => $address));
3738 $goto = $stmt->fetch(PDO::FETCH_ASSOC)['goto'];
3739 if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $goto)) {
3740 $_SESSION['return'][] = array(
3741 'type' => 'danger',
3742 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
3743 'msg' => 'access_denied'
3744 );
3745 continue;
3746 }
3747 $stmt = $pdo->prepare("DELETE FROM `spamalias` WHERE `goto` = :username AND `address` = :item");
3748 $stmt->execute(array(
3749 ':username' => $goto,
3750 ':item' => $address
3751 ));
3752 $_SESSION['return'][] = array(
3753 'type' => 'success',
3754 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
3755 'msg' => array('mailbox_modified', htmlspecialchars($goto))
3756 );
3757 }
3758 break;
3759 case 'eas_cache':
3760 if (!is_array($_data['username'])) {
3761 $usernames = array();
3762 $usernames[] = $_data['username'];
3763 }
3764 else {
3765 $usernames = $_data['username'];
3766 }
3767 if (!isset($_SESSION['acl']['eas_reset']) || $_SESSION['acl']['eas_reset'] != "1" ) {
3768 $_SESSION['return'][] = array(
3769 'type' => 'danger',
3770 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
3771 'msg' => 'access_denied'
3772 );
3773 return false;
3774 }
3775 foreach ($usernames as $username) {
3776 if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $username)) {
3777 $_SESSION['return'][] = array(
3778 'type' => 'danger',
3779 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
3780 'msg' => 'access_denied'
3781 );
3782 continue;
3783 }
3784 $stmt = $pdo->prepare("DELETE FROM `sogo_cache_folder` WHERE `c_uid` = :username");
3785 $stmt->execute(array(
3786 ':username' => $username
3787 ));
3788 $_SESSION['return'][] = array(
3789 'type' => 'success',
3790 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
3791 'msg' => array('eas_reset', htmlspecialchars($username))
3792 );
3793 }
3794 break;
3795 case 'sogo_profile':
3796 if (!is_array($_data['username'])) {
3797 $usernames = array();
3798 $usernames[] = $_data['username'];
3799 }
3800 else {
3801 $usernames = $_data['username'];
3802 }
3803 if (!isset($_SESSION['acl']['sogo_profile_reset']) || $_SESSION['acl']['sogo_profile_reset'] != "1" ) {
3804 $_SESSION['return'][] = array(
3805 'type' => 'danger',
3806 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
3807 'msg' => 'access_denied'
3808 );
3809 return false;
3810 }
3811 foreach ($usernames as $username) {
3812 if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $username)) {
3813 $_SESSION['return'][] = array(
3814 'type' => 'danger',
3815 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
3816 'msg' => 'access_denied'
3817 );
3818 continue;
3819 }
3820 $stmt = $pdo->prepare("DELETE FROM `sogo_user_profile` WHERE `c_uid` = :username");
3821 $stmt->execute(array(
3822 ':username' => $username
3823 ));
3824 $stmt = $pdo->prepare("DELETE FROM `sogo_cache_folder` WHERE `c_uid` = :username");
3825 $stmt->execute(array(
3826 ':username' => $username
3827 ));
3828 $stmt = $pdo->prepare("DELETE FROM `sogo_acl` WHERE `c_object` LIKE '%/" . $username . "/%' OR `c_uid` = :username");
3829 $stmt->execute(array(
3830 ':username' => $username
3831 ));
3832 $stmt = $pdo->prepare("DELETE FROM `sogo_store` WHERE `c_folder_id` IN (SELECT `c_folder_id` FROM `sogo_folder_info` WHERE `c_path2` = :username)");
3833 $stmt->execute(array(
3834 ':username' => $username
3835 ));
3836 $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)");
3837 $stmt->execute(array(
3838 ':username' => $username
3839 ));
3840 $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)");
3841 $stmt->execute(array(
3842 ':username' => $username
3843 ));
3844 $stmt = $pdo->prepare("DELETE FROM `sogo_folder_info` WHERE `c_path2` = :username");
3845 $stmt->execute(array(
3846 ':username' => $username
3847 ));
3848 $_SESSION['return'][] = array(
3849 'type' => 'success',
3850 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
3851 'msg' => array('sogo_profile_reset', htmlspecialchars($username))
3852 );
3853 }
3854 break;
3855 case 'domain':
3856 if (!is_array($_data['domain'])) {
3857 $domains = array();
3858 $domains[] = $_data['domain'];
3859 }
3860 else {
3861 $domains = $_data['domain'];
3862 }
3863 if ($_SESSION['mailcow_cc_role'] != "admin") {
3864 $_SESSION['return'][] = array(
3865 'type' => 'danger',
3866 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
3867 'msg' => 'access_denied'
3868 );
3869 return false;
3870 }
3871 foreach ($domains as $domain) {
3872 if (!is_valid_domain_name($domain)) {
3873 $_SESSION['return'][] = array(
3874 'type' => 'danger',
3875 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
3876 'msg' => 'domain_invalid'
3877 );
3878 continue;
3879 }
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02003880 $domain = idn_to_ascii(strtolower(trim($domain)), 0, INTL_IDNA_VARIANT_UTS46);
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01003881 $stmt = $pdo->prepare("SELECT `username` FROM `mailbox`
3882 WHERE `domain` = :domain");
3883 $stmt->execute(array(':domain' => $domain));
3884 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
3885 if ($num_results != 0 || !empty($num_results)) {
3886 $_SESSION['return'][] = array(
3887 'type' => 'danger',
3888 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
3889 'msg' => array('domain_not_empty', $domain)
3890 );
3891 continue;
3892 }
3893 $exec_fields = array('cmd' => 'maildir', 'task' => 'cleanup', 'maildir' => $domain);
3894 $maildir_gc = json_decode(docker('post', 'dovecot-mailcow', 'exec', $exec_fields), true);
3895 if ($maildir_gc['type'] != 'success') {
3896 $_SESSION['return'][] = array(
3897 'type' => 'warning',
3898 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
3899 'msg' => 'Could not move mail storage to garbage collector: ' . $maildir_gc['msg']
3900 );
3901 }
3902 $stmt = $pdo->prepare("DELETE FROM `domain` WHERE `domain` = :domain");
3903 $stmt->execute(array(
3904 ':domain' => $domain,
3905 ));
3906 $stmt = $pdo->prepare("DELETE FROM `domain_admins` WHERE `domain` = :domain");
3907 $stmt->execute(array(
3908 ':domain' => $domain,
3909 ));
3910 $stmt = $pdo->prepare("DELETE FROM `alias` WHERE `domain` = :domain");
3911 $stmt->execute(array(
3912 ':domain' => $domain,
3913 ));
3914 $stmt = $pdo->prepare("DELETE FROM `alias_domain` WHERE `target_domain` = :domain");
3915 $stmt->execute(array(
3916 ':domain' => $domain,
3917 ));
3918 $stmt = $pdo->prepare("DELETE FROM `mailbox` WHERE `domain` = :domain");
3919 $stmt->execute(array(
3920 ':domain' => $domain,
3921 ));
3922 $stmt = $pdo->prepare("DELETE FROM `sender_acl` WHERE `logged_in_as` LIKE :domain");
3923 $stmt->execute(array(
3924 ':domain' => '%@'.$domain,
3925 ));
3926 $stmt = $pdo->prepare("DELETE FROM `quota2` WHERE `username` LIKE :domain");
3927 $stmt->execute(array(
3928 ':domain' => '%@'.$domain,
3929 ));
3930 $stmt = $pdo->prepare("DELETE FROM `pushover` WHERE `username` LIKE :domain");
3931 $stmt->execute(array(
3932 ':domain' => '%@'.$domain,
3933 ));
3934 $stmt = $pdo->prepare("DELETE FROM `quota2replica` WHERE `username` LIKE :domain");
3935 $stmt->execute(array(
3936 ':domain' => '%@'.$domain,
3937 ));
3938 $stmt = $pdo->prepare("DELETE FROM `spamalias` WHERE `address` LIKE :domain");
3939 $stmt->execute(array(
3940 ':domain' => '%@'.$domain,
3941 ));
3942 $stmt = $pdo->prepare("DELETE FROM `filterconf` WHERE `object` = :domain");
3943 $stmt->execute(array(
3944 ':domain' => $domain,
3945 ));
3946 $stmt = $pdo->prepare("DELETE FROM `bcc_maps` WHERE `local_dest` = :domain");
3947 $stmt->execute(array(
3948 ':domain' => $domain,
3949 ));
3950 $stmt = $pdo->query("DELETE FROM `admin` WHERE `superadmin` = 0 AND `username` NOT IN (SELECT `username`FROM `domain_admins`);");
3951 $stmt = $pdo->query("DELETE FROM `da_acl` WHERE `username` NOT IN (SELECT `username`FROM `domain_admins`);");
3952 try {
3953 $redis->hDel('DOMAIN_MAP', $domain);
3954 $redis->hDel('RL_VALUE', $domain);
3955 }
3956 catch (RedisException $e) {
3957 $_SESSION['return'][] = array(
3958 'type' => 'danger',
3959 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
3960 'msg' => array('redis_error', $e)
3961 );
3962 continue;
3963 }
3964 $_SESSION['return'][] = array(
3965 'type' => 'success',
3966 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
3967 'msg' => array('domain_removed', htmlspecialchars($domain))
3968 );
3969 }
3970 break;
3971 case 'alias':
3972 if (!is_array($_data['id'])) {
3973 $ids = array();
3974 $ids[] = $_data['id'];
3975 }
3976 else {
3977 $ids = $_data['id'];
3978 }
3979 foreach ($ids as $id) {
3980 $alias_data = mailbox('get', 'alias_details', $id);
3981 if (empty($alias_data)) {
3982 $_SESSION['return'][] = array(
3983 'type' => 'danger',
3984 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
3985 'msg' => 'access_denied'
3986 );
3987 continue;
3988 }
3989 $stmt = $pdo->prepare("DELETE FROM `alias` WHERE `id` = :id");
3990 $stmt->execute(array(
3991 ':id' => $alias_data['id']
3992 ));
3993 $stmt = $pdo->prepare("DELETE FROM `sender_acl` WHERE `send_as` = :alias_address");
3994 $stmt->execute(array(
3995 ':alias_address' => $alias_data['address']
3996 ));
3997 $_SESSION['return'][] = array(
3998 'type' => 'success',
3999 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
4000 'msg' => array('alias_removed', htmlspecialchars($alias_data['address']))
4001 );
4002 }
4003 break;
4004 case 'alias_domain':
4005 if (!is_array($_data['alias_domain'])) {
4006 $alias_domains = array();
4007 $alias_domains[] = $_data['alias_domain'];
4008 }
4009 else {
4010 $alias_domains = $_data['alias_domain'];
4011 }
4012 foreach ($alias_domains as $alias_domain) {
4013 if (!is_valid_domain_name($alias_domain)) {
4014 $_SESSION['return'][] = array(
4015 'type' => 'danger',
4016 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
4017 'msg' => 'domain_invalid'
4018 );
4019 continue;
4020 }
4021 $stmt = $pdo->prepare("SELECT `target_domain` FROM `alias_domain`
4022 WHERE `alias_domain`= :alias_domain");
4023 $stmt->execute(array(':alias_domain' => $alias_domain));
4024 $DomainData = $stmt->fetch(PDO::FETCH_ASSOC);
4025 if (!hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $DomainData['target_domain'])) {
4026 $_SESSION['return'][] = array(
4027 'type' => 'danger',
4028 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
4029 'msg' => 'access_denied'
4030 );
4031 continue;
4032 }
4033 $stmt = $pdo->prepare("DELETE FROM `alias_domain` WHERE `alias_domain` = :alias_domain");
4034 $stmt->execute(array(
4035 ':alias_domain' => $alias_domain,
4036 ));
4037 $stmt = $pdo->prepare("DELETE FROM `alias` WHERE `domain` = :alias_domain");
4038 $stmt->execute(array(
4039 ':alias_domain' => $alias_domain,
4040 ));
4041 $stmt = $pdo->prepare("DELETE FROM `bcc_maps` WHERE `local_dest` = :alias_domain");
4042 $stmt->execute(array(
4043 ':alias_domain' => $alias_domain,
4044 ));
4045 try {
4046 $redis->hDel('DOMAIN_MAP', $alias_domain);
4047 $redis->hDel('RL_VALUE', $domain);
4048 }
4049 catch (RedisException $e) {
4050 $_SESSION['return'][] = array(
4051 'type' => 'danger',
4052 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
4053 'msg' => array('redis_error', $e)
4054 );
4055 continue;
4056 }
4057 $_SESSION['return'][] = array(
4058 'type' => 'success',
4059 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
4060 'msg' => array('alias_domain_removed', htmlspecialchars($alias_domain))
4061 );
4062 }
4063 break;
4064 case 'mailbox':
4065 if (!is_array($_data['username'])) {
4066 $usernames = array();
4067 $usernames[] = $_data['username'];
4068 }
4069 else {
4070 $usernames = $_data['username'];
4071 }
4072 foreach ($usernames as $username) {
4073 if (!filter_var($username, FILTER_VALIDATE_EMAIL)) {
4074 $_SESSION['return'][] = array(
4075 'type' => 'danger',
4076 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
4077 'msg' => 'access_denied'
4078 );
4079 continue;
4080 }
4081 if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $username)) {
4082 $_SESSION['return'][] = array(
4083 'type' => 'danger',
4084 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
4085 'msg' => 'access_denied'
4086 );
4087 continue;
4088 }
4089 $mailbox_details = mailbox('get', 'mailbox_details', $username);
4090 if (!empty($mailbox_details['domain']) && !empty($mailbox_details['local_part'])) {
4091 $maildir = $mailbox_details['domain'] . '/' . $mailbox_details['local_part'];
4092 $exec_fields = array('cmd' => 'maildir', 'task' => 'cleanup', 'maildir' => $maildir);
4093 $maildir_gc = json_decode(docker('post', 'dovecot-mailcow', 'exec', $exec_fields), true);
4094 if ($maildir_gc['type'] != 'success') {
4095 $_SESSION['return'][] = array(
4096 'type' => 'warning',
4097 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
4098 'msg' => 'Could not move maildir to garbage collector: ' . $maildir_gc['msg']
4099 );
4100 }
4101 }
4102 else {
4103 $_SESSION['return'][] = array(
4104 'type' => 'warning',
4105 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
4106 'msg' => 'Could not move maildir to garbage collector: variables local_part and/or domain empty'
4107 );
4108 }
4109 if (strtolower(getenv('SKIP_SOLR')) == 'n') {
4110 $curl = curl_init();
4111 curl_setopt($curl, CURLOPT_URL, 'http://solr:8983/solr/dovecot-fts/update?commit=true');
4112 curl_setopt($curl, CURLOPT_HTTPHEADER,array('Content-Type: text/xml'));
4113 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
4114 curl_setopt($curl, CURLOPT_POST, 1);
4115 curl_setopt($curl, CURLOPT_POSTFIELDS, '<delete><query>user:' . $username . '</query></delete>');
4116 curl_setopt($curl, CURLOPT_TIMEOUT, 30);
4117 $response = curl_exec($curl);
4118 if ($response === false) {
4119 $err = curl_error($curl);
4120 $_SESSION['return'][] = array(
4121 'type' => 'warning',
4122 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
4123 'msg' => 'Could not remove Solr index: ' . print_r($err, true)
4124 );
4125 }
4126 curl_close($curl);
4127 }
4128 $stmt = $pdo->prepare("DELETE FROM `alias` WHERE `goto` = :username");
4129 $stmt->execute(array(
4130 ':username' => $username
4131 ));
4132 $stmt = $pdo->prepare("DELETE FROM `pushover` WHERE `username` = :username");
4133 $stmt->execute(array(
4134 ':username' => $username
4135 ));
4136 $stmt = $pdo->prepare("DELETE FROM `quarantine` WHERE `rcpt` = :username");
4137 $stmt->execute(array(
4138 ':username' => $username
4139 ));
4140 $stmt = $pdo->prepare("DELETE FROM `quota2` WHERE `username` = :username");
4141 $stmt->execute(array(
4142 ':username' => $username
4143 ));
4144 $stmt = $pdo->prepare("DELETE FROM `quota2replica` WHERE `username` = :username");
4145 $stmt->execute(array(
4146 ':username' => $username
4147 ));
4148 $stmt = $pdo->prepare("DELETE FROM `mailbox` WHERE `username` = :username");
4149 $stmt->execute(array(
4150 ':username' => $username
4151 ));
4152 $stmt = $pdo->prepare("DELETE FROM `sender_acl` WHERE `logged_in_as` = :username");
4153 $stmt->execute(array(
4154 ':username' => $username
4155 ));
4156 // fk, better safe than sorry
4157 $stmt = $pdo->prepare("DELETE FROM `user_acl` WHERE `username` = :username");
4158 $stmt->execute(array(
4159 ':username' => $username
4160 ));
4161 $stmt = $pdo->prepare("DELETE FROM `spamalias` WHERE `goto` = :username");
4162 $stmt->execute(array(
4163 ':username' => $username
4164 ));
4165 $stmt = $pdo->prepare("DELETE FROM `imapsync` WHERE `user2` = :username");
4166 $stmt->execute(array(
4167 ':username' => $username
4168 ));
4169 $stmt = $pdo->prepare("DELETE FROM `filterconf` WHERE `object` = :username");
4170 $stmt->execute(array(
4171 ':username' => $username
4172 ));
4173 $stmt = $pdo->prepare("DELETE FROM `sogo_user_profile` WHERE `c_uid` = :username");
4174 $stmt->execute(array(
4175 ':username' => $username
4176 ));
4177 $stmt = $pdo->prepare("DELETE FROM `sogo_cache_folder` WHERE `c_uid` = :username");
4178 $stmt->execute(array(
4179 ':username' => $username
4180 ));
4181 $stmt = $pdo->prepare("DELETE FROM `sogo_acl` WHERE `c_object` LIKE '%/" . str_replace('%', '\%', $username) . "/%' OR `c_uid` = :username");
4182 $stmt->execute(array(
4183 ':username' => $username
4184 ));
4185 $stmt = $pdo->prepare("DELETE FROM `sogo_store` WHERE `c_folder_id` IN (SELECT `c_folder_id` FROM `sogo_folder_info` WHERE `c_path2` = :username)");
4186 $stmt->execute(array(
4187 ':username' => $username
4188 ));
4189 $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)");
4190 $stmt->execute(array(
4191 ':username' => $username
4192 ));
4193 $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)");
4194 $stmt->execute(array(
4195 ':username' => $username
4196 ));
4197 $stmt = $pdo->prepare("DELETE FROM `sogo_folder_info` WHERE `c_path2` = :username");
4198 $stmt->execute(array(
4199 ':username' => $username
4200 ));
4201 $stmt = $pdo->prepare("DELETE FROM `bcc_maps` WHERE `local_dest` = :username");
4202 $stmt->execute(array(
4203 ':username' => $username
4204 ));
4205 $stmt = $pdo->prepare("DELETE FROM `oauth_access_tokens` WHERE `user_id` = :username");
4206 $stmt->execute(array(
4207 ':username' => $username
4208 ));
4209 $stmt = $pdo->prepare("DELETE FROM `oauth_refresh_tokens` WHERE `user_id` = :username");
4210 $stmt->execute(array(
4211 ':username' => $username
4212 ));
4213 $stmt = $pdo->prepare("DELETE FROM `oauth_authorization_codes` WHERE `user_id` = :username");
4214 $stmt->execute(array(
4215 ':username' => $username
4216 ));
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02004217 $stmt = $pdo->prepare("DELETE FROM `tfa` WHERE `username` = :username");
4218 $stmt->execute(array(
4219 ':username' => $username,
4220 ));
4221 $stmt = $pdo->prepare("DELETE FROM `fido2` WHERE `username` = :username");
4222 $stmt->execute(array(
4223 ':username' => $username,
4224 ));
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01004225 $stmt = $pdo->prepare("SELECT `address`, `goto` FROM `alias`
4226 WHERE `goto` REGEXP :username");
4227 $stmt->execute(array(':username' => '(^|,)'.$username.'($|,)'));
4228 $GotoData = $stmt->fetchAll(PDO::FETCH_ASSOC);
4229 foreach ($GotoData as $gotos) {
4230 $goto_exploded = explode(',', $gotos['goto']);
4231 if (($key = array_search($username, $goto_exploded)) !== false) {
4232 unset($goto_exploded[$key]);
4233 }
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02004234 $gotos_rebuild = implode(',', (array)$goto_exploded);
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01004235 $stmt = $pdo->prepare("UPDATE `alias` SET
4236 `goto` = :goto
4237 WHERE `address` = :address");
4238 $stmt->execute(array(
4239 ':goto' => $gotos_rebuild,
4240 ':address' => $gotos['address']
4241 ));
4242 }
4243 try {
4244 $redis->hDel('RL_VALUE', $username);
4245 }
4246 catch (RedisException $e) {
4247 $_SESSION['return'][] = array(
4248 'type' => 'danger',
4249 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
4250 'msg' => array('redis_error', $e)
4251 );
4252 continue;
4253 }
4254 $_SESSION['return'][] = array(
4255 'type' => 'success',
4256 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
4257 'msg' => array('mailbox_removed', htmlspecialchars($username))
4258 );
4259 }
4260 break;
4261 case 'resource':
4262 if (!is_array($_data['name'])) {
4263 $names = array();
4264 $names[] = $_data['name'];
4265 }
4266 else {
4267 $names = $_data['name'];
4268 }
4269 foreach ($names as $name) {
4270 if (!filter_var($name, FILTER_VALIDATE_EMAIL)) {
4271 $_SESSION['return'][] = array(
4272 'type' => 'danger',
4273 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
4274 'msg' => 'access_denied'
4275 );
4276 continue;
4277 }
4278 if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $name)) {
4279 $_SESSION['return'][] = array(
4280 'type' => 'danger',
4281 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
4282 'msg' => 'access_denied'
4283 );
4284 continue;
4285 }
4286 $stmt = $pdo->prepare("DELETE FROM `mailbox` WHERE `username` = :username");
4287 $stmt->execute(array(
4288 ':username' => $name
4289 ));
4290 $stmt = $pdo->prepare("DELETE FROM `sogo_user_profile` WHERE `c_uid` = :username");
4291 $stmt->execute(array(
4292 ':username' => $name
4293 ));
4294 $stmt = $pdo->prepare("DELETE FROM `sogo_cache_folder` WHERE `c_uid` = :username");
4295 $stmt->execute(array(
4296 ':username' => $name
4297 ));
4298 $stmt = $pdo->prepare("DELETE FROM `sogo_acl` WHERE `c_object` LIKE '%/" . $name . "/%' OR `c_uid` = :username");
4299 $stmt->execute(array(
4300 ':username' => $name
4301 ));
4302 $stmt = $pdo->prepare("DELETE FROM `sogo_store` WHERE `c_folder_id` IN (SELECT `c_folder_id` FROM `sogo_folder_info` WHERE `c_path2` = :username)");
4303 $stmt->execute(array(
4304 ':username' => $name
4305 ));
4306 $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)");
4307 $stmt->execute(array(
4308 ':username' => $name
4309 ));
4310 $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)");
4311 $stmt->execute(array(
4312 ':username' => $name
4313 ));
4314 $stmt = $pdo->prepare("DELETE FROM `sogo_folder_info` WHERE `c_path2` = :username");
4315 $stmt->execute(array(
4316 ':username' => $name
4317 ));
4318 $_SESSION['return'][] = array(
4319 'type' => 'success',
4320 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
4321 'msg' => array('resource_removed', htmlspecialchars($name))
4322 );
4323 }
4324 break;
4325 }
4326 break;
4327 }
4328 if ($_action != 'get' && in_array($_type, array('domain', 'alias', 'alias_domain', 'mailbox', 'resource'))) {
4329 update_sogo_static_view();
4330 }
4331}