From 8507b06fe08157012d6e14c4c160e685fabc0b9a Mon Sep 17 00:00:00 2001
From: Marius Cramer <m.cramer@pixcept.de>
Date: Thu, 23 Jan 2014 03:57:17 -0500
Subject: [PATCH] Merge branch 'vmailuid' of /home/git/repositories/renky/ispconfig3

---
 install/dist/lib/fedora.lib.php                                                   |    2 
 interface/web/admin/lib/lang/ar_server_config.lng                                 |    1 
 install/tpl/debian6_dovecot2.conf.master                                          |   13 ++
 interface/web/mail/mail_user_edit.php                                             |    8 
 interface/web/admin/lib/lang/es_server_config.lng                                 |    1 
 interface/web/admin/lib/lang/tr_server_config.lng                                 |    1 
 interface/web/admin/lib/lang/de_server_config.lng                                 |    1 
 install/tpl/fedora_dovecot2.conf.master                                           |   13 ++
 interface/web/admin/lib/lang/nl_server_config.lng                                 |    1 
 install/sql/ispconfig3.sql                                                        |    1 
 interface/web/admin/lib/lang/hu_server_config.lng                                 |    1 
 interface/web/admin/lib/lang/pl_server_config.lng                                 |    1 
 interface/web/admin/lib/lang/el_server_config.lng                                 |    1 
 interface/web/themes/default-304/templates/admin/server_config_mail_edit.htm      |    6 +
 interface/lib/classes/remoting.inc.php                                            |    1 
 interface/web/admin/lib/lang/fi_server_config.lng                                 |    1 
 interface/web/admin/lib/lang/se_server_config.lng                                 |    1 
 install/tpl/mysql-virtual_gids.cf.master                                          |    8 +
 install/sql/incremental/upd_0064.sql                                              |    2 
 interface/web/admin/templates/server_config_mail_edit.htm                         |    6 +
 interface/web/admin/lib/lang/sk_server_config.lng                                 |    1 
 interface/web/themes/default-304/templates/sites/web_vhost_subdomain_advanced.htm |    4 
 interface/web/admin/lib/lang/id_server_config.lng                                 |    1 
 install/tpl/debian_postfix.conf.master                                            |    6 
 install/tpl/opensuse_postfix.conf.master                                          |    6 
 interface/web/themes/default-304/templates/mail/spamfilter_config_mail_edit.htm   |    6 +
 install/tpl/server.ini.master                                                     |    1 
 interface/web/admin/form/server_config.tform.php                                  |    6 +
 interface/web/themes/default-304/templates/sites/web_vhost_subdomain_stats.htm    |    4 
 install/tpl/opensuse_dovecot2.conf.master                                         |   13 ++
 interface/web/admin/lib/lang/ru_server_config.lng                                 |    1 
 interface/web/admin/lib/lang/ro_server_config.lng                                 |    1 
 server/plugins-available/mail_plugin.inc.php                                      |   92 ++++++++++----
 interface/web/admin/lib/lang/br_server_config.lng                                 |    1 
 interface/web/admin/lib/lang/hr_server_config.lng                                 |    1 
 interface/web/admin/lib/lang/fr_server_config.lng                                 |    1 
 server/lib/classes/system.inc.php                                                 |   66 +++++++++-
 interface/web/themes/default-304/templates/sites/web_vhost_subdomain_backup.htm   |    4 
 install/tpl/debian_dovecot2.conf.master                                           |   13 ++
 install/dist/lib/gentoo.lib.php                                                   |    2 
 install/dist/lib/opensuse.lib.php                                                 |    2 
 interface/web/themes/default-304/templates/sites/web_vhost_subdomain_ssl.htm      |    4 
 interface/web/admin/lib/lang/en_server_config.lng                                 |    1 
 install/tpl/gentoo_postfix.conf.master                                            |    6 
 interface/lib/classes/remote.d/mail.inc.php                                       |    4 
 interface/web/themes/default-304/templates/sites/web_vhost_subdomain_redirect.htm |    4 
 interface/web/admin/lib/lang/cz_server_config.lng                                 |    1 
 interface/web/admin/lib/lang/it_server_config.lng                                 |    1 
 install/tpl/fedora_postfix.conf.master                                            |    6 
 install/dist/lib/debian60.lib.php                                                 |    2 
 interface/web/admin/lib/lang/ja_server_config.lng                                 |    1 
 interface/web/admin/lib/lang/pt_server_config.lng                                 |    1 
 install/tpl/mysql-virtual_uids.cf.master                                          |    8 +
 interface/web/admin/lib/lang/bg_server_config.lng                                 |    1 
 54 files changed, 273 insertions(+), 69 deletions(-)

diff --git a/install/dist/lib/debian60.lib.php b/install/dist/lib/debian60.lib.php
index b3887cb..d792c5a 100644
--- a/install/dist/lib/debian60.lib.php
+++ b/install/dist/lib/debian60.lib.php
@@ -57,7 +57,7 @@
 		// Adding the amavisd commands to the postfix configuration
 		$postconf_commands = array (
 			'dovecot_destination_recipient_limit = 1',
-			'virtual_transport = dovecot',
+			'virtual_transport = lmtp:unix:private/dovecot-lmtp',
 			'smtpd_sasl_type = dovecot',
 			'smtpd_sasl_path = private/auth'
 		);
diff --git a/install/dist/lib/fedora.lib.php b/install/dist/lib/fedora.lib.php
index ab25741..628f012 100644
--- a/install/dist/lib/fedora.lib.php
+++ b/install/dist/lib/fedora.lib.php
@@ -402,7 +402,7 @@
 		// Adding the amavisd commands to the postfix configuration
 		$postconf_commands = array (
 			'dovecot_destination_recipient_limit = 1',
-			'virtual_transport = dovecot',
+			'virtual_transport = lmtp:unix:private/dovecot-lmtp',
 			'smtpd_sasl_type = dovecot',
 			'smtpd_sasl_path = private/auth',
 			'receive_override_options = no_address_mappings'
diff --git a/install/dist/lib/gentoo.lib.php b/install/dist/lib/gentoo.lib.php
index 99670ee..7078f83 100644
--- a/install/dist/lib/gentoo.lib.php
+++ b/install/dist/lib/gentoo.lib.php
@@ -245,7 +245,7 @@
 		//* Reconfigure postfix to use dovecot authentication
 		$postconf_commands = array (
 			'dovecot_destination_recipient_limit = 1',
-			'virtual_transport = dovecot',
+			'virtual_transport = lmtp:unix:private/dovecot-lmtp',
 			'smtpd_sasl_type = dovecot',
 			'smtpd_sasl_path = private/auth'
 		);
diff --git a/install/dist/lib/opensuse.lib.php b/install/dist/lib/opensuse.lib.php
index 7663f7e..9ec6bcf 100644
--- a/install/dist/lib/opensuse.lib.php
+++ b/install/dist/lib/opensuse.lib.php
@@ -437,7 +437,7 @@
 		// Adding the amavisd commands to the postfix configuration
 		$postconf_commands = array (
 			'dovecot_destination_recipient_limit = 1',
-			'virtual_transport = dovecot',
+			'virtual_transport = lmtp:unix:private/dovecot-lmtp',
 			'smtpd_sasl_type = dovecot',
 			'smtpd_sasl_path = private/auth',
 			'receive_override_options = no_address_mappings'
diff --git a/install/sql/incremental/upd_0064.sql b/install/sql/incremental/upd_0064.sql
new file mode 100644
index 0000000..23bdd26
--- /dev/null
+++ b/install/sql/incremental/upd_0064.sql
@@ -0,0 +1,2 @@
+ALTER TABLE `mail_user` ADD `disablelmtp` ENUM( 'n', 'y' ) NOT NULL DEFAULT 'n' AFTER `disablelda` ;
+ALTER TABLE `mail_user` CHANGE `uid` `uid` INT( 11 ) NOT NULL DEFAULT '5000', CHANGE `gid` `gid` INT( 11 ) NOT NULL DEFAULT '5000' ;
\ No newline at end of file
diff --git a/install/sql/ispconfig3.sql b/install/sql/ispconfig3.sql
index 5efffbf..920ebf9 100644
--- a/install/sql/ispconfig3.sql
+++ b/install/sql/ispconfig3.sql
@@ -910,6 +910,7 @@
   `disablesmtp` enum('n','y') NOT NULL default 'n',
   `disablesieve` enum('n','y') NOT NULL default 'n',
   `disablelda` enum('n','y') NOT NULL default 'n',
+  `disablelmtp` enum('n','y') NOT NULL default 'n',
   `disabledoveadm` enum('n','y') NOT NULL default 'n',
   `last_quota_notification` date NULL default NULL,
   `backup_interval` VARCHAR( 255 ) NOT NULL,
diff --git a/install/tpl/debian6_dovecot2.conf.master b/install/tpl/debian6_dovecot2.conf.master
index f359260..5b04c61 100644
--- a/install/tpl/debian6_dovecot2.conf.master
+++ b/install/tpl/debian6_dovecot2.conf.master
@@ -1,5 +1,5 @@
 listen = *,[::]
-protocols = imap pop3
+protocols = imap pop3 lmtp
 auth_mechanisms = plain login
 disable_plaintext_auth = no
 log_timestamp = "%Y-%m-%d %H:%M:%S "
@@ -31,6 +31,13 @@
   }
   user = root
 }
+service lmtp {
+  unix_listener /var/spool/postfix/private/dovecot-lmtp {
+   group = postfix
+   mode = 0600
+   user = postfix
+  }
+}
 service imap-login {
   client_limit = 1000
   process_limit = 500
@@ -44,4 +51,8 @@
 }
 protocol lda {
   mail_plugins = sieve quota
+}
+protocol lmtp {
+  postmaster_address = webmaster@localhost
+  mail_plugins = quota sieve
 }
\ No newline at end of file
diff --git a/install/tpl/debian_dovecot2.conf.master b/install/tpl/debian_dovecot2.conf.master
index f359260..5b04c61 100644
--- a/install/tpl/debian_dovecot2.conf.master
+++ b/install/tpl/debian_dovecot2.conf.master
@@ -1,5 +1,5 @@
 listen = *,[::]
-protocols = imap pop3
+protocols = imap pop3 lmtp
 auth_mechanisms = plain login
 disable_plaintext_auth = no
 log_timestamp = "%Y-%m-%d %H:%M:%S "
@@ -31,6 +31,13 @@
   }
   user = root
 }
+service lmtp {
+  unix_listener /var/spool/postfix/private/dovecot-lmtp {
+   group = postfix
+   mode = 0600
+   user = postfix
+  }
+}
 service imap-login {
   client_limit = 1000
   process_limit = 500
@@ -44,4 +51,8 @@
 }
 protocol lda {
   mail_plugins = sieve quota
+}
+protocol lmtp {
+  postmaster_address = webmaster@localhost
+  mail_plugins = quota sieve
 }
\ No newline at end of file
diff --git a/install/tpl/debian_postfix.conf.master b/install/tpl/debian_postfix.conf.master
index e5564c8..e8f3d82 100644
--- a/install/tpl/debian_postfix.conf.master
+++ b/install/tpl/debian_postfix.conf.master
@@ -5,8 +5,8 @@
 virtual_mailbox_domains = proxy:mysql:{config_dir}/mysql-virtual_domains.cf
 virtual_mailbox_maps = proxy:mysql:{config_dir}/mysql-virtual_mailboxes.cf
 virtual_mailbox_base = {vmail_mailbox_base}
-virtual_uid_maps = static:{vmail_userid}
-virtual_gid_maps = static:{vmail_groupid}
+virtual_uid_maps = mysql:/etc/postfix/mysql-virtual_uids.cf
+virtual_gid_maps = mysql:/etc/postfix/mysql-virtual_gids.cf
 inet_protocols=all
 smtpd_sasl_auth_enable = yes
 broken_sasl_auth_clients = yes
@@ -25,7 +25,7 @@
 smtpd_client_message_rate_limit = 100
 maildrop_destination_concurrency_limit = 1
 maildrop_destination_recipient_limit   = 1
-virtual_transport = maildrop
+virtual_transport = lmtp:unix:private/dovecot-lmtp
 header_checks = regexp:{config_dir}/header_checks
 mime_header_checks = regexp:{config_dir}/mime_header_checks
 nested_header_checks = regexp:{config_dir}/nested_header_checks
diff --git a/install/tpl/fedora_dovecot2.conf.master b/install/tpl/fedora_dovecot2.conf.master
index 75fad4f..0d91006 100644
--- a/install/tpl/fedora_dovecot2.conf.master
+++ b/install/tpl/fedora_dovecot2.conf.master
@@ -1,5 +1,5 @@
 listen = *,[::]
-protocols = imap pop3
+protocols = imap pop3 lmtp
 auth_mechanisms = plain login
 disable_plaintext_auth = no
 log_timestamp = "%Y-%m-%d %H:%M:%S "
@@ -31,6 +31,13 @@
   }
   user = root
 }
+service lmtp {
+  unix_listener /var/spool/postfix/private/dovecot-lmtp {
+   group = postfix
+   mode = 0600
+   user = postfix
+  }
+}
 service imap-login {
   client_limit = 1000
   process_limit = 500
@@ -44,4 +51,8 @@
 }
 protocol lda {
   mail_plugins = sieve quota
+}
+protocol lmtp {
+  postmaster_address = webmaster@localhost
+  mail_plugins = quota sieve
 }
\ No newline at end of file
diff --git a/install/tpl/fedora_postfix.conf.master b/install/tpl/fedora_postfix.conf.master
index 35c3cac..4401f70 100644
--- a/install/tpl/fedora_postfix.conf.master
+++ b/install/tpl/fedora_postfix.conf.master
@@ -3,8 +3,8 @@
 virtual_mailbox_domains = proxy:mysql:{config_dir}/mysql-virtual_domains.cf
 virtual_mailbox_maps = proxy:mysql:{config_dir}/mysql-virtual_mailboxes.cf
 virtual_mailbox_base = {vmail_mailbox_base}
-virtual_uid_maps = static:{vmail_userid}
-virtual_gid_maps = static:{vmail_groupid}
+virtual_uid_maps = mysql:/etc/postfix/mysql-virtual_uids.cf
+virtual_gid_maps = mysql:/etc/postfix/mysql-virtual_gids.cf
 smtpd_sasl_auth_enable = yes
 broken_sasl_auth_clients = yes
 smtpd_sasl_authenticated_header = yes
@@ -22,7 +22,7 @@
 smtpd_client_message_rate_limit = 100
 maildrop_destination_concurrency_limit = 1
 maildrop_destination_recipient_limit   = 1
-virtual_transport = maildrop
+virtual_transport = lmtp:unix:private/dovecot-lmtp
 header_checks = regexp:{config_dir}/header_checks
 mime_header_checks = regexp:{config_dir}/mime_header_checks
 nested_header_checks = regexp:{config_dir}/nested_header_checks
diff --git a/install/tpl/gentoo_postfix.conf.master b/install/tpl/gentoo_postfix.conf.master
index 35c3cac..4401f70 100644
--- a/install/tpl/gentoo_postfix.conf.master
+++ b/install/tpl/gentoo_postfix.conf.master
@@ -3,8 +3,8 @@
 virtual_mailbox_domains = proxy:mysql:{config_dir}/mysql-virtual_domains.cf
 virtual_mailbox_maps = proxy:mysql:{config_dir}/mysql-virtual_mailboxes.cf
 virtual_mailbox_base = {vmail_mailbox_base}
-virtual_uid_maps = static:{vmail_userid}
-virtual_gid_maps = static:{vmail_groupid}
+virtual_uid_maps = mysql:/etc/postfix/mysql-virtual_uids.cf
+virtual_gid_maps = mysql:/etc/postfix/mysql-virtual_gids.cf
 smtpd_sasl_auth_enable = yes
 broken_sasl_auth_clients = yes
 smtpd_sasl_authenticated_header = yes
@@ -22,7 +22,7 @@
 smtpd_client_message_rate_limit = 100
 maildrop_destination_concurrency_limit = 1
 maildrop_destination_recipient_limit   = 1
-virtual_transport = maildrop
+virtual_transport = lmtp:unix:private/dovecot-lmtp
 header_checks = regexp:{config_dir}/header_checks
 mime_header_checks = regexp:{config_dir}/mime_header_checks
 nested_header_checks = regexp:{config_dir}/nested_header_checks
diff --git a/install/tpl/mysql-virtual_gids.cf.master b/install/tpl/mysql-virtual_gids.cf.master
new file mode 100644
index 0000000..7c7d995
--- /dev/null
+++ b/install/tpl/mysql-virtual_gids.cf.master
@@ -0,0 +1,8 @@
+user = {mysql_server_ispconfig_user}
+password = {mysql_server_ispconfig_password}
+dbname = {mysql_server_database}
+table = mail_user
+select_field = gid
+where_field = email
+additional_conditions = and postfix = 'y' and server_id = {server_id}
+hosts = {mysql_server_ip}
\ No newline at end of file
diff --git a/install/tpl/mysql-virtual_uids.cf.master b/install/tpl/mysql-virtual_uids.cf.master
new file mode 100644
index 0000000..da3cd7c
--- /dev/null
+++ b/install/tpl/mysql-virtual_uids.cf.master
@@ -0,0 +1,8 @@
+user = {mysql_server_ispconfig_user}
+password = {mysql_server_ispconfig_password}
+dbname = {mysql_server_database}
+table = mail_user
+select_field = uid
+where_field = email
+additional_conditions = and postfix = 'y' and server_id = {server_id}
+hosts = {mysql_server_ip}
diff --git a/install/tpl/opensuse_dovecot2.conf.master b/install/tpl/opensuse_dovecot2.conf.master
index f359260..5b04c61 100644
--- a/install/tpl/opensuse_dovecot2.conf.master
+++ b/install/tpl/opensuse_dovecot2.conf.master
@@ -1,5 +1,5 @@
 listen = *,[::]
-protocols = imap pop3
+protocols = imap pop3 lmtp
 auth_mechanisms = plain login
 disable_plaintext_auth = no
 log_timestamp = "%Y-%m-%d %H:%M:%S "
@@ -31,6 +31,13 @@
   }
   user = root
 }
+service lmtp {
+  unix_listener /var/spool/postfix/private/dovecot-lmtp {
+   group = postfix
+   mode = 0600
+   user = postfix
+  }
+}
 service imap-login {
   client_limit = 1000
   process_limit = 500
@@ -44,4 +51,8 @@
 }
 protocol lda {
   mail_plugins = sieve quota
+}
+protocol lmtp {
+  postmaster_address = webmaster@localhost
+  mail_plugins = quota sieve
 }
\ No newline at end of file
diff --git a/install/tpl/opensuse_postfix.conf.master b/install/tpl/opensuse_postfix.conf.master
index 35c3cac..4401f70 100644
--- a/install/tpl/opensuse_postfix.conf.master
+++ b/install/tpl/opensuse_postfix.conf.master
@@ -3,8 +3,8 @@
 virtual_mailbox_domains = proxy:mysql:{config_dir}/mysql-virtual_domains.cf
 virtual_mailbox_maps = proxy:mysql:{config_dir}/mysql-virtual_mailboxes.cf
 virtual_mailbox_base = {vmail_mailbox_base}
-virtual_uid_maps = static:{vmail_userid}
-virtual_gid_maps = static:{vmail_groupid}
+virtual_uid_maps = mysql:/etc/postfix/mysql-virtual_uids.cf
+virtual_gid_maps = mysql:/etc/postfix/mysql-virtual_gids.cf
 smtpd_sasl_auth_enable = yes
 broken_sasl_auth_clients = yes
 smtpd_sasl_authenticated_header = yes
@@ -22,7 +22,7 @@
 smtpd_client_message_rate_limit = 100
 maildrop_destination_concurrency_limit = 1
 maildrop_destination_recipient_limit   = 1
-virtual_transport = maildrop
+virtual_transport = lmtp:unix:private/dovecot-lmtp
 header_checks = regexp:{config_dir}/header_checks
 mime_header_checks = regexp:{config_dir}/mime_header_checks
 nested_header_checks = regexp:{config_dir}/nested_header_checks
diff --git a/install/tpl/server.ini.master b/install/tpl/server.ini.master
index 9186cc1..0d578e1 100644
--- a/install/tpl/server.ini.master
+++ b/install/tpl/server.ini.master
@@ -38,6 +38,7 @@
 mailuser_gid=5000
 mailuser_name=vmail
 mailuser_group=vmail
+mailbox_virtual_uidgid_maps=n
 relayhost=
 relayhost_user=
 relayhost_password=
diff --git a/interface/lib/classes/remote.d/mail.inc.php b/interface/lib/classes/remote.d/mail.inc.php
index d235d01..c54466f 100644
--- a/interface/lib/classes/remote.d/mail.inc.php
+++ b/interface/lib/classes/remote.d/mail.inc.php
@@ -214,6 +214,10 @@
 			return false;
 		}
 
+		//* Set a few params to non empty values that will be overwritten by mail_plugin
+		if (!isset($params['uid'])) $params['uid'] = -1;
+		if (!isset($params['gid'])) $params['gid'] = -1;
+
 		$affected_rows = $this->insertQuery('../mail/form/mail_user.tform.php', $client_id, $params);
 		return $affected_rows;
 	}
diff --git a/interface/lib/classes/remoting.inc.php b/interface/lib/classes/remoting.inc.php
index d0d2dc8..647ba80 100644
--- a/interface/lib/classes/remoting.inc.php
+++ b/interface/lib/classes/remoting.inc.php
@@ -184,7 +184,6 @@
 		return $app->db->affectedRows() == 1;
 	}
 
-
 	//** protected functions -----------------------------------------------------------------------------------
 
 	protected function klientadd($formdef_file, $reseller_id, $params)
diff --git a/interface/web/admin/form/server_config.tform.php b/interface/web/admin/form/server_config.tform.php
index dde1d74..89165ec 100644
--- a/interface/web/admin/form/server_config.tform.php
+++ b/interface/web/admin/form/server_config.tform.php
@@ -368,6 +368,12 @@
 			'width' => '10',
 			'maxlength' => '255'
 		),
+		'mailbox_virtual_uidgid_maps' => array(
+			'datatype' => 'VARCHAR',
+			'formtype' => 'CHECKBOX',
+			'default' => 'n',
+			'value' => array(0 => 'n', 1 => 'y')
+		),
 		'relayhost' => array(
 			'datatype' => 'VARCHAR',
 			'formtype' => 'TEXT',
diff --git a/interface/web/admin/lib/lang/ar_server_config.lng b/interface/web/admin/lib/lang/ar_server_config.lng
index 1c21551..26e13b4 100644
--- a/interface/web/admin/lib/lang/ar_server_config.lng
+++ b/interface/web/admin/lib/lang/ar_server_config.lng
@@ -24,6 +24,7 @@
 $wb['mailuser_gid_txt'] = 'Mailuser GID';
 $wb['mailuser_name_txt'] = 'Mailuser Name';
 $wb['mailuser_group_txt'] = 'Mailuser Group';
+$wb['mailbox_virtual_uidgid_maps_txt'] = 'Use Websites Linux uid for mailbox (only if both on the same server)';
 $wb['relayhost_txt'] = 'Relayhost';
 $wb['relayhost_user_txt'] = 'Relayhost User';
 $wb['relayhost_password_txt'] = 'Relayhost Password';
diff --git a/interface/web/admin/lib/lang/bg_server_config.lng b/interface/web/admin/lib/lang/bg_server_config.lng
index c050aad..db043ee 100644
--- a/interface/web/admin/lib/lang/bg_server_config.lng
+++ b/interface/web/admin/lib/lang/bg_server_config.lng
@@ -22,6 +22,7 @@
 $wb['mailuser_gid_txt'] = 'Mailuser GID';
 $wb['mailuser_name_txt'] = 'Mailuser Name';
 $wb['mailuser_group_txt'] = 'Mailuser Group';
+$wb['mailbox_virtual_uidgid_maps_txt'] = 'Use Websites Linux uid for mailbox (only if both on the same server)';
 $wb['relayhost_txt'] = 'Relayhost';
 $wb['relayhost_user_txt'] = 'Relayhost User';
 $wb['relayhost_password_txt'] = 'Relayhost Password';
diff --git a/interface/web/admin/lib/lang/br_server_config.lng b/interface/web/admin/lib/lang/br_server_config.lng
index 71288a9..67e8a20 100644
--- a/interface/web/admin/lib/lang/br_server_config.lng
+++ b/interface/web/admin/lib/lang/br_server_config.lng
@@ -23,6 +23,7 @@
 $wb['mailuser_gid_txt'] = 'GID usuário de email';
 $wb['mailuser_name_txt'] = 'Nome usuário de email';
 $wb['mailuser_group_txt'] = 'Grupo usuário de email';
+$wb['mailbox_virtual_uidgid_maps_txt'] = 'Use Websites Linux uid for mailbox (only if both on the same server)';
 $wb['relayhost_txt'] = 'Host Relay';
 $wb['relayhost_user_txt'] = 'Usuário do Host Relay';
 $wb['relayhost_password_txt'] = 'Senha do Host Relay';
diff --git a/interface/web/admin/lib/lang/cz_server_config.lng b/interface/web/admin/lib/lang/cz_server_config.lng
index 18e833e..a660171 100644
--- a/interface/web/admin/lib/lang/cz_server_config.lng
+++ b/interface/web/admin/lib/lang/cz_server_config.lng
@@ -24,6 +24,7 @@
 $wb['mailuser_gid_txt'] = 'Mail uživatel GID';
 $wb['mailuser_name_txt'] = 'Mail uživatel jméno';
 $wb['mailuser_group_txt'] = 'Mail uživatel skupina';
+$wb['mailbox_virtual_uidgid_maps_txt'] = 'Use Websites Linux uid for mailbox (only if both on the same server)';
 $wb['relayhost_txt'] = 'Relayhost';
 $wb['relayhost_user_txt'] = 'Relayhost uživatel';
 $wb['relayhost_password_txt'] = 'Relayhost heslo';
diff --git a/interface/web/admin/lib/lang/de_server_config.lng b/interface/web/admin/lib/lang/de_server_config.lng
index 5f615cc..c9673b2 100644
--- a/interface/web/admin/lib/lang/de_server_config.lng
+++ b/interface/web/admin/lib/lang/de_server_config.lng
@@ -25,6 +25,7 @@
 $wb['mailuser_gid_txt'] = 'Mailbenutzer GID';
 $wb['mailuser_name_txt'] = 'Mailbenutzer Name';
 $wb['mailuser_group_txt'] = 'Mailbenutzer Gruppe';
+$wb['mailbox_virtual_uidgid_maps_txt'] = 'Website Linux Uid für Mailboxen (nur wenn beides auf gleichem Server)';
 $wb['relayhost_txt'] = 'Relayhost';
 $wb['relayhost_user_txt'] = 'Relayhost Benutzer';
 $wb['relayhost_password_txt'] = 'Relayhost Passwort';
diff --git a/interface/web/admin/lib/lang/el_server_config.lng b/interface/web/admin/lib/lang/el_server_config.lng
index 169a560..407bc72 100644
--- a/interface/web/admin/lib/lang/el_server_config.lng
+++ b/interface/web/admin/lib/lang/el_server_config.lng
@@ -24,6 +24,7 @@
 $wb['mailuser_gid_txt'] = 'Mailuser GID';
 $wb['mailuser_name_txt'] = 'Όνομα Mailuser';
 $wb['mailuser_group_txt'] = 'Ομάδα Mailuser';
+$wb['mailbox_virtual_uidgid_maps_txt'] = 'Use Websites Linux uid for mailbox (only if both on the same server)';
 $wb['relayhost_txt'] = 'Relayhost';
 $wb['relayhost_user_txt'] = 'Χρήστης Relayhost';
 $wb['relayhost_password_txt'] = 'Συνθηματικό Relayhost';
diff --git a/interface/web/admin/lib/lang/en_server_config.lng b/interface/web/admin/lib/lang/en_server_config.lng
index 07e94af..e472859 100644
--- a/interface/web/admin/lib/lang/en_server_config.lng
+++ b/interface/web/admin/lib/lang/en_server_config.lng
@@ -36,6 +36,7 @@
 $wb["mailuser_gid_txt"] = 'Mailuser GID';
 $wb["mailuser_name_txt"] = 'Mailuser Name';
 $wb["mailuser_group_txt"] = 'Mailuser Group';
+$wb['mailbox_virtual_uidgid_maps_txt'] = 'Use Websites Linux uid for mailbox (only if both on the same server)';
 $wb["relayhost_txt"] = 'Relayhost';
 $wb["relayhost_user_txt"] = 'Relayhost User';
 $wb["relayhost_password_txt"] = 'Relayhost Password';
diff --git a/interface/web/admin/lib/lang/es_server_config.lng b/interface/web/admin/lib/lang/es_server_config.lng
index 1987c51..2088dba 100644
--- a/interface/web/admin/lib/lang/es_server_config.lng
+++ b/interface/web/admin/lib/lang/es_server_config.lng
@@ -22,6 +22,7 @@
 $wb['mailuser_gid_txt'] = 'GID usuario de correo';
 $wb['mailuser_name_txt'] = 'Nombre del usuario de correo';
 $wb['mailuser_group_txt'] = 'Grupo del usuario de correo';
+$wb['mailbox_virtual_uidgid_maps_txt'] = 'Use Websites Linux uid for mailbox (only if both on the same server)';
 $wb['relayhost_txt'] = 'Servidor de retransmisión';
 $wb['relayhost_user_txt'] = 'Usuario de retransmisión';
 $wb['relayhost_password_txt'] = 'Contraseña de retramisión';
diff --git a/interface/web/admin/lib/lang/fi_server_config.lng b/interface/web/admin/lib/lang/fi_server_config.lng
index efa1220..fe203be 100755
--- a/interface/web/admin/lib/lang/fi_server_config.lng
+++ b/interface/web/admin/lib/lang/fi_server_config.lng
@@ -22,6 +22,7 @@
 $wb['mailuser_gid_txt'] = 'Käyttäjäryhmä';
 $wb['mailuser_name_txt'] = 'Postikäyttäjän nimi';
 $wb['mailuser_group_txt'] = 'Postikäyttäjän ryhmä';
+$wb['mailbox_virtual_uidgid_maps_txt'] = 'Use Websites Linux uid for mailbox (only if both on the same server)';
 $wb['relayhost_txt'] = 'Edelleenlähetyspalvelin';
 $wb['relayhost_user_txt'] = 'Edelleenlähetyspalvelimen käyttäjätunnus';
 $wb['relayhost_password_txt'] = 'Edelleenlähetyspalvelimen salasana';
diff --git a/interface/web/admin/lib/lang/fr_server_config.lng b/interface/web/admin/lib/lang/fr_server_config.lng
index 32779c7..f61a409 100644
--- a/interface/web/admin/lib/lang/fr_server_config.lng
+++ b/interface/web/admin/lib/lang/fr_server_config.lng
@@ -24,6 +24,7 @@
 $wb['mailuser_gid_txt'] = 'GID de l\'utilisateur mail';
 $wb['mailuser_name_txt'] = 'Nom d\'utilisateur mail';
 $wb['mailuser_group_txt'] = 'Groupe de l\'utilisateur mail';
+$wb['mailbox_virtual_uidgid_maps_txt'] = 'Use Websites Linux uid for mailbox (only if both on the same server)';
 $wb['relayhost_txt'] = 'Hôte de relais';
 $wb['relayhost_user_txt'] = 'Utilisateur du relais';
 $wb['relayhost_password_txt'] = 'Mot de passe du relais';
diff --git a/interface/web/admin/lib/lang/hr_server_config.lng b/interface/web/admin/lib/lang/hr_server_config.lng
index a441f96..d7880ee 100644
--- a/interface/web/admin/lib/lang/hr_server_config.lng
+++ b/interface/web/admin/lib/lang/hr_server_config.lng
@@ -23,6 +23,7 @@
 $wb['mailuser_gid_txt'] = 'Mailuser GID';
 $wb['mailuser_name_txt'] = 'Mailuser ime';
 $wb['mailuser_group_txt'] = 'Mailuser grupa';
+$wb['mailbox_virtual_uidgid_maps_txt'] = 'Use Websites Linux uid for mailbox (only if both on the same server)';
 $wb['relayhost_txt'] = 'Relayhost';
 $wb['relayhost_user_txt'] = 'Relayhost korisnik';
 $wb['relayhost_password_txt'] = 'Relayhost šifra';
diff --git a/interface/web/admin/lib/lang/hu_server_config.lng b/interface/web/admin/lib/lang/hu_server_config.lng
index 0be61ef..db88185 100644
--- a/interface/web/admin/lib/lang/hu_server_config.lng
+++ b/interface/web/admin/lib/lang/hu_server_config.lng
@@ -22,6 +22,7 @@
 $wb['mailuser_gid_txt'] = 'Mailuser GID';
 $wb['mailuser_name_txt'] = 'Mailuser Name';
 $wb['mailuser_group_txt'] = 'Mailuser Group';
+$wb['mailbox_virtual_uidgid_maps_txt'] = 'Use Websites Linux uid for mailbox (only if both on the same server)';
 $wb['relayhost_txt'] = 'Relayhost';
 $wb['relayhost_user_txt'] = 'Relayhost User';
 $wb['relayhost_password_txt'] = 'Relayhost Password';
diff --git a/interface/web/admin/lib/lang/id_server_config.lng b/interface/web/admin/lib/lang/id_server_config.lng
index 54a4493..dfa67f9 100644
--- a/interface/web/admin/lib/lang/id_server_config.lng
+++ b/interface/web/admin/lib/lang/id_server_config.lng
@@ -24,6 +24,7 @@
 $wb['mailuser_gid_txt'] = 'GID Pengguna Mail';
 $wb['mailuser_name_txt'] = 'Nama Pengguna Mail';
 $wb['mailuser_group_txt'] = 'Grup Pengguna Mail';
+$wb['mailbox_virtual_uidgid_maps_txt'] = 'Use Websites Linux uid for mailbox (only if both on the same server)';
 $wb['relayhost_txt'] = 'Relayhost';
 $wb['relayhost_user_txt'] = 'Pengguna Relayhost';
 $wb['relayhost_password_txt'] = 'Kata Sandi Relayhost';
diff --git a/interface/web/admin/lib/lang/it_server_config.lng b/interface/web/admin/lib/lang/it_server_config.lng
index d040768..33101e9 100644
--- a/interface/web/admin/lib/lang/it_server_config.lng
+++ b/interface/web/admin/lib/lang/it_server_config.lng
@@ -21,6 +21,7 @@
 $wb['mailuser_gid_txt'] = 'Mailuser GID';
 $wb['mailuser_name_txt'] = 'Mailuser Name';
 $wb['mailuser_group_txt'] = 'Mailuser Group';
+$wb['mailbox_virtual_uidgid_maps_txt'] = 'Use Websites Linux uid for mailbox (only if both on the same server)';
 $wb['relayhost_txt'] = 'Relayhost';
 $wb['relayhost_user_txt'] = 'Relayhost User';
 $wb['relayhost_password_txt'] = 'Relayhost Password';
diff --git a/interface/web/admin/lib/lang/ja_server_config.lng b/interface/web/admin/lib/lang/ja_server_config.lng
index 890bb56..46e8b21 100644
--- a/interface/web/admin/lib/lang/ja_server_config.lng
+++ b/interface/web/admin/lib/lang/ja_server_config.lng
@@ -24,6 +24,7 @@
 $wb['mailuser_gid_txt'] = 'メールユーザーのGID';
 $wb['mailuser_name_txt'] = 'メールユーザー名';
 $wb['mailuser_group_txt'] = 'メールユーザーグループ';
+$wb['mailbox_virtual_uidgid_maps_txt'] = 'Use Websites Linux uid for mailbox (only if both on the same server)';
 $wb['relayhost_txt'] = 'リレーホスト';
 $wb['relayhost_user_txt'] = 'リレーホストユーザー';
 $wb['relayhost_password_txt'] = 'リレーホストパスワード';
diff --git a/interface/web/admin/lib/lang/nl_server_config.lng b/interface/web/admin/lib/lang/nl_server_config.lng
index 5d08e91..6847356 100644
--- a/interface/web/admin/lib/lang/nl_server_config.lng
+++ b/interface/web/admin/lib/lang/nl_server_config.lng
@@ -24,6 +24,7 @@
 $wb['mailuser_gid_txt'] = 'Mailuser GID';
 $wb['mailuser_name_txt'] = 'Mailuser name';
 $wb['mailuser_group_txt'] = 'Mailuser groep';
+$wb['mailbox_virtual_uidgid_maps_txt'] = 'Use Websites Linux uid for mailbox (only if both on the same server)';
 $wb['relayhost_txt'] = 'Relayhost';
 $wb['relayhost_user_txt'] = 'Relayhost gebruiker';
 $wb['relayhost_password_txt'] = 'Relayhost wachtwoord';
diff --git a/interface/web/admin/lib/lang/pl_server_config.lng b/interface/web/admin/lib/lang/pl_server_config.lng
index 32c4c00..ec51f68 100644
--- a/interface/web/admin/lib/lang/pl_server_config.lng
+++ b/interface/web/admin/lib/lang/pl_server_config.lng
@@ -24,6 +24,7 @@
 $wb['mailuser_gid_txt'] = 'GID użytkownika e-mail';
 $wb['mailuser_name_txt'] = 'Nazwa użytkownika e-mail';
 $wb['mailuser_group_txt'] = 'Grupa użytkownika e-mail';
+$wb['mailbox_virtual_uidgid_maps_txt'] = 'Use Websites Linux uid for mailbox (only if both on the same server)';
 $wb['relayhost_txt'] = 'Adres Relayhost';
 $wb['relayhost_user_txt'] = 'Użytkownik Relayhost';
 $wb['relayhost_password_txt'] = 'Hasło Relayhost';
diff --git a/interface/web/admin/lib/lang/pt_server_config.lng b/interface/web/admin/lib/lang/pt_server_config.lng
index e427ed9..0fee45f 100644
--- a/interface/web/admin/lib/lang/pt_server_config.lng
+++ b/interface/web/admin/lib/lang/pt_server_config.lng
@@ -23,6 +23,7 @@
 $wb['mailuser_gid_txt'] = 'GID utilizador de email';
 $wb['mailuser_name_txt'] = 'Nome utilizador de email';
 $wb['mailuser_group_txt'] = 'Grupo utilizador de email';
+$wb['mailbox_virtual_uidgid_maps_txt'] = 'Use Websites Linux uid for mailbox (only if both on the same server)';
 $wb['relayhost_txt'] = 'Host Relay';
 $wb['relayhost_user_txt'] = 'Utilizador do Host Relay';
 $wb['relayhost_password_txt'] = 'Senha do Host Relay';
diff --git a/interface/web/admin/lib/lang/ro_server_config.lng b/interface/web/admin/lib/lang/ro_server_config.lng
index 55b7355..e0a43db 100644
--- a/interface/web/admin/lib/lang/ro_server_config.lng
+++ b/interface/web/admin/lib/lang/ro_server_config.lng
@@ -23,6 +23,7 @@
 $wb['mailuser_gid_txt'] = 'Mailuser GID';
 $wb['mailuser_name_txt'] = 'Mailuser Name';
 $wb['mailuser_group_txt'] = 'Mailuser Group';
+$wb['mailbox_virtual_uidgid_maps_txt'] = 'Use Websites Linux uid for mailbox (only if both on the same server)';
 $wb['relayhost_txt'] = 'Relayhost';
 $wb['relayhost_user_txt'] = 'Relayhost User';
 $wb['relayhost_password_txt'] = 'Relayhost Password';
diff --git a/interface/web/admin/lib/lang/ru_server_config.lng b/interface/web/admin/lib/lang/ru_server_config.lng
index 5ae21a9..79cdfe0 100644
--- a/interface/web/admin/lib/lang/ru_server_config.lng
+++ b/interface/web/admin/lib/lang/ru_server_config.lng
@@ -21,6 +21,7 @@
 $wb['mailuser_gid_txt'] = 'Mailuser GID';
 $wb['mailuser_name_txt'] = 'Имя Mailuser';
 $wb['mailuser_group_txt'] = 'Группа Mailuser';
+$wb['mailbox_virtual_uidgid_maps_txt'] = 'Use Websites Linux uid for mailbox (only if both on the same server)';
 $wb['relayhost_txt'] = 'Relay-хост';
 $wb['relayhost_user_txt'] = 'Логин Relay-хоста';
 $wb['relayhost_password_txt'] = 'Пароль Relay-хоста';
diff --git a/interface/web/admin/lib/lang/se_server_config.lng b/interface/web/admin/lib/lang/se_server_config.lng
index 089e629..bf3fe03 100644
--- a/interface/web/admin/lib/lang/se_server_config.lng
+++ b/interface/web/admin/lib/lang/se_server_config.lng
@@ -22,6 +22,7 @@
 $wb['mailuser_gid_txt'] = 'Mailuser GID';
 $wb['mailuser_name_txt'] = 'Mailuser Name';
 $wb['mailuser_group_txt'] = 'Mailuser Group';
+$wb['mailbox_virtual_uidgid_maps_txt'] = 'Use Websites Linux uid for mailbox (only if both on the same server)';
 $wb['relayhost_txt'] = 'Relayhost';
 $wb['relayhost_user_txt'] = 'Relayhost User';
 $wb['relayhost_password_txt'] = 'Relayhost Password';
diff --git a/interface/web/admin/lib/lang/sk_server_config.lng b/interface/web/admin/lib/lang/sk_server_config.lng
index c01606b..74ff994 100644
--- a/interface/web/admin/lib/lang/sk_server_config.lng
+++ b/interface/web/admin/lib/lang/sk_server_config.lng
@@ -24,6 +24,7 @@
 $wb['mailuser_gid_txt'] = 'Mailuser GID';
 $wb['mailuser_name_txt'] = 'Mailuser Meno';
 $wb['mailuser_group_txt'] = 'Mailuser Skupina';
+$wb['mailbox_virtual_uidgid_maps_txt'] = 'Use Websites Linux uid for mailbox (only if both on the same server)';
 $wb['relayhost_txt'] = 'Relayhost';
 $wb['relayhost_user_txt'] = 'Relayhost uživateľ';
 $wb['relayhost_password_txt'] = 'Relayhost heslo';
diff --git a/interface/web/admin/lib/lang/tr_server_config.lng b/interface/web/admin/lib/lang/tr_server_config.lng
index 81595ff..fe2b286 100644
--- a/interface/web/admin/lib/lang/tr_server_config.lng
+++ b/interface/web/admin/lib/lang/tr_server_config.lng
@@ -24,6 +24,7 @@
 $wb['mailuser_gid_txt'] = 'Mail kullanıcısı GID';
 $wb['mailuser_name_txt'] = 'Mail kullanıcısı Adı';
 $wb['mailuser_group_txt'] = 'Mail kullanıcısı Grubu';
+$wb['mailbox_virtual_uidgid_maps_txt'] = 'Use Websites Linux uid for mailbox (only if both on the same server)';
 $wb['relayhost_txt'] = 'Relayhost';
 $wb['relayhost_user_txt'] = 'Relayhost Kullanıcı';
 $wb['relayhost_password_txt'] = 'Relayhost Şifre';
diff --git a/interface/web/admin/templates/server_config_mail_edit.htm b/interface/web/admin/templates/server_config_mail_edit.htm
index 6ab03eb..991f1b0 100644
--- a/interface/web/admin/templates/server_config_mail_edit.htm
+++ b/interface/web/admin/templates/server_config_mail_edit.htm
@@ -56,6 +56,12 @@
                 <input name="mailuser_group" id="mailuser_group" value="{tmpl_var name='mailuser_group'}" size="10" maxlength="255" type="text" class="textInput formLengthHalf" />
             </div>
             <div class="ctrlHolder">
+                <p class="label">{tmpl_var name='mailbox_virtual_uidgid_maps_txt'}</p>
+                <div class="multiField">
+                    {tmpl_var name='mailbox_virtual_uidgid_maps'}
+                </div>
+            </div>
+            <div class="ctrlHolder">
                 <label for="relayhost">{tmpl_var name='relayhost_txt'}</label>
                 <input name="relayhost" id="relayhost" value="{tmpl_var name='relayhost'}" size="40" maxlength="255" type="text" class="textInput formLengthHalf" />
             </div>
diff --git a/interface/web/mail/mail_user_edit.php b/interface/web/mail/mail_user_edit.php
index 61f27cb..75fb42f 100644
--- a/interface/web/mail/mail_user_edit.php
+++ b/interface/web/mail/mail_user_edit.php
@@ -203,9 +203,11 @@
 			$maildir = str_replace("[localpart]", strtolower($_POST["email_local_part"]), $maildir);
 			$this->dataRecord["maildir"] = $maildir;
 			$this->dataRecord["homedir"] = $mail_config["homedir_path"];
-			$this->dataRecord["uid"] = $mail_config["mailuser_uid"];
-			$this->dataRecord["gid"] = $mail_config["mailuser_gid"];
-
+			
+			// Will be overwritten by mail_plugin
+			$this->dataRecord['uid'] = -1;
+			$this->dataRecord['gid'] = -1;
+				
 			//* Check if there is no alias or forward with this address
 			$tmp = $app->db->queryOneRecord("SELECT count(forwarding_id) as number FROM mail_forwarding WHERE active = 'y' AND source = '".$app->db->quote($this->dataRecord["email"])."'");
 			if($tmp['number'] > 0) $app->tform->errorMessage .= $app->tform->lng("duplicate_alias_or_forward_txt")."<br>";
diff --git a/interface/web/themes/default-304/templates/admin/server_config_mail_edit.htm b/interface/web/themes/default-304/templates/admin/server_config_mail_edit.htm
index 72292b3..7293b2c 100644
--- a/interface/web/themes/default-304/templates/admin/server_config_mail_edit.htm
+++ b/interface/web/themes/default-304/templates/admin/server_config_mail_edit.htm
@@ -52,6 +52,12 @@
                 <input name="mailuser_group" id="mailuser_group" value="{tmpl_var name='mailuser_group'}" size="10" maxlength="255" type="text" class="textInput formLengthHalf" />
             </div>
             <div class="ctrlHolder">
+                <p class="label">{tmpl_var name='mailbox_virtual_uidgid_maps_txt'}</p>
+                <div class="multiField">
+                    {tmpl_var name='mailbox_virtual_uidgid_maps'}
+                </div>
+            </div>
+            <div class="ctrlHolder">
                 <label for="relayhost">{tmpl_var name='relayhost_txt'}</label>
                 <input name="relayhost" id="relayhost" value="{tmpl_var name='relayhost'}" size="40" maxlength="255" type="text" class="textInput formLengthHalf" />
             </div>
diff --git a/interface/web/themes/default-304/templates/mail/spamfilter_config_mail_edit.htm b/interface/web/themes/default-304/templates/mail/spamfilter_config_mail_edit.htm
index 524c980..e51a780 100644
--- a/interface/web/themes/default-304/templates/mail/spamfilter_config_mail_edit.htm
+++ b/interface/web/themes/default-304/templates/mail/spamfilter_config_mail_edit.htm
@@ -33,6 +33,12 @@
                 <label for="mailuser_group" class="wf_preField">{tmpl_var name='mailuser_group_txt'}</label>
                 <input type="text" id="mailuser_group" name="mailuser_group" value="{tmpl_var name='mailuser_group'}" size="10" maxlength="255">
             </span>
+            <div class="ctrlHolder">
+                <p class="label">{tmpl_var name='mailbox_virtual_uidgid_maps_txt'}</p>
+                <div class="multiField">
+                    {tmpl_var name='mailbox_virtual_uidgid_maps'}
+                </div>
+            </div>
             <span class="wf_oneField">
                 <label for="relayhost" class="wf_preField">{tmpl_var name='relayhost_txt'}</label>
                 <input type="text" id="relayhost" name="relayhost" value="{tmpl_var name='relayhost'}" size="40" maxlength="255">
diff --git a/interface/web/themes/default-304/templates/sites/web_vhost_subdomain_advanced.htm b/interface/web/themes/default-304/templates/sites/web_vhost_subdomain_advanced.htm
index 8710740..289339b 100644
--- a/interface/web/themes/default-304/templates/sites/web_vhost_subdomain_advanced.htm
+++ b/interface/web/themes/default-304/templates/sites/web_vhost_subdomain_advanced.htm
@@ -82,8 +82,8 @@
         <input type="hidden" name="id" value="{tmpl_var name='id'}">
 
         <div class="buttonHolder buttons">
-            <button class="positive iconstxt icoPositive" type="button" value="{tmpl_var name='btn_save_txt'}" onClick="submitForm('pageForm','sites/web_domain_edit.php');"><span>{tmpl_var name='btn_save_txt'}</span></button>
-            <button class="negative iconstxt icoNegative" type="button" value="{tmpl_var name='btn_cancel_txt'}" onClick="loadContent('sites/web_domain_list.php');"><span>{tmpl_var name='btn_cancel_txt'}</span></button>
+            <button class="positive iconstxt icoPositive" type="button" value="{tmpl_var name='btn_save_txt'}" onClick="submitForm('pageForm','sites/web_vhost_subdomain_edit.php');"><span>{tmpl_var name='btn_save_txt'}</span></button>
+            <button class="negative iconstxt icoNegative" type="button" value="{tmpl_var name='btn_cancel_txt'}" onClick="loadContent('sites/web_vhost_subdomain_list.php');"><span>{tmpl_var name='btn_cancel_txt'}</span></button>
         </div>
     </div>
 
diff --git a/interface/web/themes/default-304/templates/sites/web_vhost_subdomain_backup.htm b/interface/web/themes/default-304/templates/sites/web_vhost_subdomain_backup.htm
index c46d4e2..1784e24 100644
--- a/interface/web/themes/default-304/templates/sites/web_vhost_subdomain_backup.htm
+++ b/interface/web/themes/default-304/templates/sites/web_vhost_subdomain_backup.htm
@@ -24,8 +24,8 @@
         <input type="hidden" name="id" value="{tmpl_var name='id'}">
 
         <div class="buttonHolder buttons">
-            <button class="positive iconstxt icoPositive" type="button" value="{tmpl_var name='btn_save_txt'}" onClick="submitForm('pageForm','sites/web_domain_edit.php');"><span>{tmpl_var name='btn_save_txt'}</span></button>
-            <button class="negative iconstxt icoNegative" type="button" value="{tmpl_var name='btn_cancel_txt'}" onClick="loadContent('sites/web_domain_list.php');"><span>{tmpl_var name='btn_cancel_txt'}</span></button>
+            <button class="positive iconstxt icoPositive" type="button" value="{tmpl_var name='btn_save_txt'}" onClick="submitForm('pageForm','sites/web_vhost_subdomain_edit.php');"><span>{tmpl_var name='btn_save_txt'}</span></button>
+            <button class="negative iconstxt icoNegative" type="button" value="{tmpl_var name='btn_cancel_txt'}" onClick="loadContent('sites/web_vhost_subdomain_list.php');"><span>{tmpl_var name='btn_cancel_txt'}</span></button>
         </div>
     </div>
 
diff --git a/interface/web/themes/default-304/templates/sites/web_vhost_subdomain_redirect.htm b/interface/web/themes/default-304/templates/sites/web_vhost_subdomain_redirect.htm
index 66a1703..eb059e2 100644
--- a/interface/web/themes/default-304/templates/sites/web_vhost_subdomain_redirect.htm
+++ b/interface/web/themes/default-304/templates/sites/web_vhost_subdomain_redirect.htm
@@ -26,8 +26,8 @@
         <input type="hidden" name="id" value="{tmpl_var name='id'}">
 
         <div class="buttonHolder buttons">
-            <button class="positive iconstxt icoPositive" type="button" value="{tmpl_var name='btn_save_txt'}" onClick="submitForm('pageForm','sites/web_domain_edit.php');"><span>{tmpl_var name='btn_save_txt'}</span></button>
-            <button class="negative iconstxt icoNegative" type="button" value="{tmpl_var name='btn_cancel_txt'}" onClick="loadContent('sites/web_domain_list.php');"><span>{tmpl_var name='btn_cancel_txt'}</span></button>
+            <button class="positive iconstxt icoPositive" type="button" value="{tmpl_var name='btn_save_txt'}" onClick="submitForm('pageForm','sites/web_vhost_subdomain_edit.php');"><span>{tmpl_var name='btn_save_txt'}</span></button>
+            <button class="negative iconstxt icoNegative" type="button" value="{tmpl_var name='btn_cancel_txt'}" onClick="loadContent('sites/web_vhost_subdomain_list.php');"><span>{tmpl_var name='btn_cancel_txt'}</span></button>
         </div>
     </div>
 
diff --git a/interface/web/themes/default-304/templates/sites/web_vhost_subdomain_ssl.htm b/interface/web/themes/default-304/templates/sites/web_vhost_subdomain_ssl.htm
index 4c27bb7..b4c0357 100644
--- a/interface/web/themes/default-304/templates/sites/web_vhost_subdomain_ssl.htm
+++ b/interface/web/themes/default-304/templates/sites/web_vhost_subdomain_ssl.htm
@@ -60,8 +60,8 @@
         <input type="hidden" name="id" value="{tmpl_var name='id'}">
 
         <div class="buttonHolder buttons">
-            <button class="positive iconstxt icoPositive" type="button" value="{tmpl_var name='btn_save_txt'}" onClick="submitForm('pageForm','sites/web_domain_edit.php');"><span>{tmpl_var name='btn_save_txt'}</span></button>
-            <button class="negative iconstxt icoNegative" type="button" value="{tmpl_var name='btn_cancel_txt'}" onClick="loadContent('sites/web_domain_list.php');"><span>{tmpl_var name='btn_cancel_txt'}</span></button>
+            <button class="positive iconstxt icoPositive" type="button" value="{tmpl_var name='btn_save_txt'}" onClick="submitForm('pageForm','sites/web_vhost_subdomain_edit.php');"><span>{tmpl_var name='btn_save_txt'}</span></button>
+            <button class="negative iconstxt icoNegative" type="button" value="{tmpl_var name='btn_cancel_txt'}" onClick="loadContent('sites/web_vhost_subdomain_list.php');"><span>{tmpl_var name='btn_cancel_txt'}</span></button>
         </div>
     </div>
 
diff --git a/interface/web/themes/default-304/templates/sites/web_vhost_subdomain_stats.htm b/interface/web/themes/default-304/templates/sites/web_vhost_subdomain_stats.htm
index 26eee94..8234422 100644
--- a/interface/web/themes/default-304/templates/sites/web_vhost_subdomain_stats.htm
+++ b/interface/web/themes/default-304/templates/sites/web_vhost_subdomain_stats.htm
@@ -29,8 +29,8 @@
         <input type="hidden" name="id" value="{tmpl_var name='id'}">
 
         <div class="buttonHolder buttons">
-            <button class="positive iconstxt icoPositive" type="button" value="{tmpl_var name='btn_save_txt'}" onClick="submitForm('pageForm','sites/web_domain_edit.php');"><span>{tmpl_var name='btn_save_txt'}</span></button>
-            <button class="negative iconstxt icoNegative" type="button" value="{tmpl_var name='btn_cancel_txt'}" onClick="loadContent('sites/web_domain_list.php');"><span>{tmpl_var name='btn_cancel_txt'}</span></button>
+            <button class="positive iconstxt icoPositive" type="button" value="{tmpl_var name='btn_save_txt'}" onClick="submitForm('pageForm','sites/web_vhost_subdomain_edit.php');"><span>{tmpl_var name='btn_save_txt'}</span></button>
+            <button class="negative iconstxt icoNegative" type="button" value="{tmpl_var name='btn_cancel_txt'}" onClick="loadContent('sites/web_vhost_subdomain_list.php');"><span>{tmpl_var name='btn_cancel_txt'}</span></button>
         </div>
     </div>
 
diff --git a/server/lib/classes/system.inc.php b/server/lib/classes/system.inc.php
index afb4db8..6ee9555 100644
--- a/server/lib/classes/system.inc.php
+++ b/server/lib/classes/system.inc.php
@@ -606,6 +606,30 @@
 
 
 	/**
+	 * Get the user from an user id
+	 *
+	 */
+	function getuser($uid){
+		global $app;
+		$user_datei = $this->server_conf['passwd_datei'];
+		$users = $app->file->no_comments($user_datei);
+		$lines = explode("\n", $users);
+		if(is_array($lines)){
+			foreach($lines as $line){
+				if(trim($line) != ''){
+					list($f1, $f2, $f3,) = explode(':', $line);
+					if($f3 == $uid) return $f1;
+				}
+			}
+		}
+		return false;
+	}
+
+
+
+
+
+	/**
 	 * Get the user id from an user
 	 *
 	 */
@@ -626,6 +650,30 @@
 		} else {
 			return false;
 		}
+	}
+
+
+
+
+
+	/**
+	 * Get the group from a group id
+	 *
+	 */
+	function getgroup($gid){
+		global $app;
+		$group_datei = $this->server_conf['group_datei'];
+		$groups = $app->file->no_comments($group_datei);
+		$lines = explode("\n", $groups);
+		if(is_array($lines)){
+			foreach($lines as $line){
+				if(trim($line) != ""){
+					list($f1, $f2, $f3, $f4) = explode(':', $line);
+					if($f3 == $gid) return $f1;
+				}
+			}
+		}
+		return false;
 	}
 
 
@@ -1476,7 +1524,7 @@
 		}
 	}
 
-	function maildirmake($maildir_path, $user = '', $subfolder = '') {
+	function maildirmake($maildir_path, $user = '', $subfolder = '', $group = '') {
 
 		global $app;
 
@@ -1490,22 +1538,24 @@
 
 		if($user != '' && $user != 'root' && $this->is_user($user)) {
 			$user = escapeshellcmd($user);
-			// I assume that the name of the (vmail group) is the same as the name of the mail user in ISPConfig 3
-			$group = $user;
 			if(is_dir($dir)) $this->chown($dir, $user);
-			if(is_dir($dir)) $this->chgrp($dir, $group);
 
 			$chown_mdsub = true;
+		}
+
+		if($group != '' && $group != 'root' && $this->is_group($group)) {
+			$group = escapeshellcmd($group);
+			if(is_dir($dir)) $this->chgrp($dir, $group);
+		
+			$chgrp_mdsub = true;
 		}
 
 		$maildirsubs = array('cur', 'new', 'tmp');
 
 		foreach ($maildirsubs as $mdsub) {
 			if(!is_dir($dir.'/'.$mdsub)) mkdir($dir.'/'.$mdsub, 0700, true);
-			if ($chown_mdsub) {
-				chown($dir.'/'.$mdsub, $user);
-				chgrp($dir.'/'.$mdsub, $group);
-			}
+			if ($chown_mdsub) chown($dir.'/'.$mdsub, $user);
+			if ($chgrp_mdsub) chgrp($dir.'/'.$mdsub, $group);
 		}
 
 		chmod($dir, 0700);
diff --git a/server/plugins-available/mail_plugin.inc.php b/server/plugins-available/mail_plugin.inc.php
index 4a308da..c5f1392 100644
--- a/server/plugins-available/mail_plugin.inc.php
+++ b/server/plugins-available/mail_plugin.inc.php
@@ -92,17 +92,48 @@
 		unset($tmp_basepath_parts[count($tmp_basepath_parts)-1]);
 		$base_path = implode('/', $tmp_basepath_parts);
 
+		//* Set the email-uid and gid if not given
+		if (($data['new']['uid'] == -1) || ($data['new']['gid'] == -1)) {
+			$app->log('Setting uid and gid automatically',LOGLEVEL_DEBUG);
+			if ($mail_config["mailbox_virtual_uidgid_maps"] == 'y') {
+				$app->log('Map uid to linux-user',LOGLEVEL_DEBUG);
+				$email_parts = explode('@',$data['new']['email']);
+				$webdomain = $app->db->queryOneRecord("SELECT domain_id, server_id, system_user, parent_domain_id FROM web_domain WHERE domain = '".$app->db->quote($email_parts[1])."'");
+				if ($webdomain) {
+					while ($webdomain['parent_domain_id'] != 0) {
+						$webdomain = $app->db->queryOneRecord("SELECT domain_id, server_id, system_user, parent_domain_id FROM web_domain WHERE domain_id = '".$webdomain['parent_domain_id']."'");
+					}
+					$app->log($data['new']['server_id'].' == '.$webdomain['server_id'],LOGLEVEL_DEBUG);
+
+					// only if web and mailserver are identical
+					if ($data['new']['server_id'] == $webdomain['server_id']) {
+						$data['new']['uid'] = $app->system->getuid($webdomain['system_user']);
+					}
+				}
+			}
+		}
+		// if nothing set before -> use standard mailuser uid and gid vmail
+		if ($data['new']['uid'] == -1) $data['new']['uid'] = $mail_config["mailuser_uid"];
+		if ($data['new']['gid'] == -1) $data['new']['gid'] = $mail_config["mailuser_gid"];
+		$app->log('Mailuser uid: '.$data['new']['uid'].', gid: '.$data['new']['gid'],LOGLEVEL_DEBUG);
+
+		// update DB if values changed
+		$app->db->query("UPDATE mail_user SET uid = ".$data['new']['uid'].", gid = ".$data['new']['gid']." WHERE mailuser_id = ".$data['new']['mailuser_id']);
+
+		// now get names of uid and gid
+		$user = $app->system->getuser($data['new']['uid']);
+		$group = $app->system->getgroup($data['new']['gid']);
 		//* Create the mail domain directory, if it does not exist
 		if(!empty($base_path) && !is_dir($base_path)) {
 			//exec("su -c 'mkdir -p ".escapeshellcmd($base_path)."' ".$mail_config['mailuser_name']);
-			$app->system->mkdirpath($base_path, 0700, $mail_config['mailuser_name'], $mail_config['mailuser_group']);
+			$app->system->mkdirpath($base_path, 0770, $mail_config['mailuser_name'], $mail_config['mailuser_group']); // needs group-access because users of subfolders may differ from vmail
 			$app->log('Created Directory: '.$base_path, LOGLEVEL_DEBUG);
 		}
 
 		// Dovecot uses a different mail layout with a separate 'Maildir' subdirectory.
 		if($mail_config['pop3_imap_daemon'] == 'dovecot') {
 			//exec("su -c 'mkdir -p ".escapeshellcmd($maildomain_path)."' ".$mail_config['mailuser_name']);
-			$app->system->mkdirpath($maildomain_path, 0700, $mail_config['mailuser_name'], $mail_config['mailuser_group']);
+			$app->system->mkdirpath($maildomain_path, 0700, $user, $group);
 			$app->log('Created Directory: '.$maildomain_path, LOGLEVEL_DEBUG);
 			$maildomain_path .= '/Maildir';
 		}
@@ -117,46 +148,48 @@
 		if(!empty($maildomain_path) && !is_dir($maildomain_path)) {
 
 			//exec("su -c 'maildirmake ".escapeshellcmd($maildomain_path)."' ".$mail_config['mailuser_name']);
-			$app->system->maildirmake($maildomain_path, $mail_config['mailuser_name']);
-
-			exec('chown -R '.$mail_config['mailuser_name'].':'.$mail_config['mailuser_group'].' '.escapeshellcmd($data['new']['maildir']));
-			$app->log('Set ownership on '.escapeshellcmd($data['new']['maildir']), LOGLEVEL_DEBUG);
+			$app->system->maildirmake($maildomain_path, $user, '', $group);
 
 			//* This is to fix the maildrop quota not being rebuilt after the quota is changed.
 			if($mail_config['pop3_imap_daemon'] != 'dovecot') {
-				if(is_dir($maildomain_path)) exec("su -c 'maildirmake -q ".$data['new']['quota']."S ".escapeshellcmd($maildomain_path)."' ".$mail_config['mailuser_name']); // Avoid maildirmake quota bug, see debian bug #214911
-				$app->log('Created Maildir: '."su -c 'maildirmake -q ".$data['new']['quota']."S ".escapeshellcmd($maildomain_path)."' ".$mail_config['mailuser_name'], LOGLEVEL_DEBUG);
+				if(is_dir($maildomain_path)) exec("su -c 'maildirmake -q ".$data['new']['quota']."S ".escapeshellcmd($maildomain_path)."' ".$user); // Avoid maildirmake quota bug, see debian bug #214911
+				$app->log('Created Maildir: '."su -c 'maildirmake -q ".$data['new']['quota']."S ".escapeshellcmd($maildomain_path)."' ".$user, LOGLEVEL_DEBUG);
 			}
 		}
 
 		if(!is_dir($data['new']['maildir'].'/.Sent')) {
 			//exec("su -c 'maildirmake -f Sent ".escapeshellcmd($maildomain_path)."' ".$mail_config['mailuser_name']);
 			//$app->log('Created submaildir Sent: '."su -c 'maildirmake -f Sent ".escapeshellcmd($maildomain_path)."' ".$mail_config['mailuser_name'],LOGLEVEL_DEBUG);
-			$app->system->maildirmake($maildomain_path, $mail_config['mailuser_name'], 'Sent');
+			$app->system->maildirmake($maildomain_path, $user, 'Sent', $group);
 		}
 		if(!is_dir($data['new']['maildir'].'/.Drafts')) {
 			//exec("su -c 'maildirmake -f Drafts ".escapeshellcmd($maildomain_path)."' ".$mail_config['mailuser_name']);
 			//$app->log('Created submaildir Drafts: '."su -c 'maildirmake -f Drafts ".escapeshellcmd($maildomain_path)."' ".$mail_config['mailuser_name'],LOGLEVEL_DEBUG);
-			$app->system->maildirmake($maildomain_path, $mail_config['mailuser_name'], 'Drafts');
+			$app->system->maildirmake($maildomain_path, $user, 'Drafts', $group);
 		}
 		if(!is_dir($data['new']['maildir'].'/.Trash')) {
 			//exec("su -c 'maildirmake -f Trash ".escapeshellcmd($maildomain_path)."' ".$mail_config['mailuser_name']);
 			//$app->log('Created submaildir Trash: '."su -c 'maildirmake -f Trash ".escapeshellcmd($maildomain_path)."' ".$mail_config['mailuser_name'],LOGLEVEL_DEBUG);
-			$app->system->maildirmake($maildomain_path, $mail_config['mailuser_name'], 'Trash');
+			$app->system->maildirmake($maildomain_path, $user, 'Trash', $group);
 		}
 		if(!is_dir($data['new']['maildir'].'/.Junk')) {
 			//exec("su -c 'maildirmake -f Junk ".escapeshellcmd($maildomain_path)."' ".$mail_config['mailuser_name']);
 			//$app->log('Created submaildir Junk: '."su -c 'maildirmake -f Junk ".escapeshellcmd($maildomain_path)."' ".$mail_config['mailuser_name'],LOGLEVEL_DEBUG);
-			$app->system->maildirmake($maildomain_path, $mail_config['mailuser_name'], 'Junk');
+			$app->system->maildirmake($maildomain_path, $user, 'Junk', $group);
 		}
+
+		// Set permissions now recursive
+		exec('chown -R '.$user.':'.$group.' '.escapeshellcmd($data['new']['maildir']));
+		$app->log('Set ownership on '.escapeshellcmd($data['new']['maildir']), LOGLEVEL_DEBUG);
 
 		//* Set the maildir quota
 		if(is_dir($data['new']['maildir'].'/new') && $mail_config['pop3_imap_daemon'] != 'dovecot') {
 			if($data['new']['quota'] > 0) {
-				if(is_dir($data['new']['maildir'])) exec("su -c 'maildirmake -q ".$data['new']['quota']."S ".escapeshellcmd($data['new']['maildir'])."' ".$mail_config['mailuser_name']);
-				$app->log('Set Maildir quota: '."su -c 'maildirmake -q ".$data['new']['quota']."S ".escapeshellcmd($data['new']['maildir'])."' ".$mail_config['mailuser_name'], LOGLEVEL_DEBUG);
+				if(is_dir($data['new']['maildir'])) exec("su -c 'maildirmake -q ".$data['new']['quota']."S ".escapeshellcmd($data['new']['maildir'])."' ".$user);
+				$app->log('Set Maildir quota: '."su -c 'maildirmake -q ".$data['new']['quota']."S ".escapeshellcmd($data['new']['maildir'])."' ".$user, LOGLEVEL_DEBUG);
 			}
 		}
+		
 
 		//* Send the welcome email message
 		if(file_exists($conf['rootpath'].'/conf-custom/mail/welcome_email_'.$conf['language'].'.txt')) {
@@ -224,16 +257,19 @@
 		unset($tmp_basepath_parts[count($tmp_basepath_parts)-1]);
 		$base_path = implode('/', $tmp_basepath_parts);
 
+		$user = $app->system->getuser($data['new']['uid']);
+		$group = $app->system->getgroup($data['new']['gid']);
+
 		//* Create the mail domain directory, if it does not exist
 		if(!empty($base_path) && !is_dir($base_path)) {
 			//exec("su -c 'mkdir -p ".escapeshellcmd($base_path)."' ".$mail_config['mailuser_name']);
-			$app->system->mkdirpath($base_path, 0700, $mail_config['mailuser_name'], $mail_config['mailuser_group']);
+			$app->system->mkdirpath($base_path, 0770, $mail_config['mailuser_name'], $mail_config['mailuser_group']); // needs group-access because users of subfolders may differ from vmail
 			$app->log('Created Directory: '.$base_path, LOGLEVEL_DEBUG);
 		}
 
 		// Dovecot uses a different mail layout with a separate 'Maildir' subdirectory.
 		if($mail_config['pop3_imap_daemon'] == 'dovecot') {
-			$app->system->mkdirpath($maildomain_path, 0700, $mail_config['mailuser_name'], $mail_config['mailuser_group']);
+			$app->system->mkdirpath($maildomain_path, 0700, $user, $group);
 			$app->log('Created Directory: '.$base_path, LOGLEVEL_DEBUG);
 			$maildomain_path .= '/Maildir';
 		}
@@ -248,15 +284,13 @@
 		if(!empty($maildomain_path) && !is_dir($maildomain_path.'/new')) {
 			//exec("su -c 'maildirmake ".escapeshellcmd($maildomain_path)."' ".$mail_config['mailuser_name']);
 			//$app->log("Created Maildir "."su -c 'maildirmake ".escapeshellcmd($maildomain_path)."' ".$mail_config['mailuser_name'],LOGLEVEL_DEBUG);
-			$app->system->maildirmake($maildomain_path, $mail_config['mailuser_name']);
+			$app->system->maildirmake($maildomain_path, $user, '', $group);
 
-			exec('chown -R '.$mail_config['mailuser_name'].':'.$mail_config['mailuser_group'].' '.escapeshellcmd($data['new']['maildir']));
-			$app->log('Set ownership on '.escapeshellcmd($data['new']['maildir']), LOGLEVEL_DEBUG);
 			//* This is to fix the maildrop quota not being rebuilt after the quota is changed.
 			if($mail_config['pop3_imap_daemon'] != 'dovecot') {
 				if($data['new']['quota'] > 0) {
-					if(is_dir($maildomain_path)) exec("su -c 'maildirmake -q ".$data['new']['quota']."S ".escapeshellcmd($maildomain_path)."' ".$mail_config['mailuser_name']); // Avoid maildirmake quota bug, see debian bug #214911
-					$app->log('Updated Maildir quota: '."su -c 'maildirmake -q ".$data['new']['quota']."S ".escapeshellcmd($maildomain_path)."' ".$mail_config['mailuser_name'], LOGLEVEL_DEBUG);
+					if(is_dir($maildomain_path)) exec("su -c 'maildirmake -q ".$data['new']['quota']."S ".escapeshellcmd($maildomain_path)."' ".$user); // Avoid maildirmake quota bug, see debian bug #214911
+					$app->log('Updated Maildir quota: '."su -c 'maildirmake -q ".$data['new']['quota']."S ".escapeshellcmd($maildomain_path)."' ".$user, LOGLEVEL_DEBUG);
 				} else {
 					if(file_exists($data['new']['maildir'].'/maildirsize')) unlink($data['new']['maildir'].'/maildirsize');
 					$app->log('Set Maildir quota to unlimited.', LOGLEVEL_DEBUG);
@@ -267,23 +301,27 @@
 		if(!is_dir($data['new']['maildir'].'/.Sent')) {
 			//exec("su -c 'maildirmake -f Sent ".escapeshellcmd($maildomain_path)."' ".$mail_config['mailuser_name']);
 			//$app->log('Created submaildir Sent: '."su -c 'maildirmake -f Sent ".escapeshellcmd($maildomain_path)."' ".$mail_config['mailuser_name'],LOGLEVEL_DEBUG);
-			$app->system->maildirmake($maildomain_path, $mail_config['mailuser_name'], 'Sent');
+			$app->system->maildirmake($maildomain_path, $user, 'Sent', $group);
 		}
 		if(!is_dir($data['new']['maildir'].'/.Drafts')) {
 			//exec("su -c 'maildirmake -f Drafts ".escapeshellcmd($maildomain_path)."' ".$mail_config['mailuser_name']);
 			//$app->log('Created submaildir Drafts: '."su -c 'maildirmake -f Drafts ".escapeshellcmd($maildomain_path)."' ".$mail_config['mailuser_name'],LOGLEVEL_DEBUG);
-			$app->system->maildirmake($maildomain_path, $mail_config['mailuser_name'], 'Drafts');
+			$app->system->maildirmake($maildomain_path, $user, 'Drafts', $group);
 		}
 		if(!is_dir($data['new']['maildir'].'/.Trash')) {
 			//exec("su -c 'maildirmake -f Trash ".escapeshellcmd($maildomain_path)."' ".$mail_config['mailuser_name']);
 			//$app->log('Created submaildir Trash: '."su -c 'maildirmake -f Trash ".escapeshellcmd($maildomain_path)."' ".$mail_config['mailuser_name'],LOGLEVEL_DEBUG);
-			$app->system->maildirmake($maildomain_path, $mail_config['mailuser_name'], 'Trash');
+			$app->system->maildirmake($maildomain_path, $user, 'Trash', $group);
 		}
 		if(!is_dir($data['new']['maildir'].'/.Junk')) {
 			//exec("su -c 'maildirmake -f Junk ".escapeshellcmd($maildomain_path)."' ".$mail_config['mailuser_name']);
 			//$app->log('Created submaildir Junk: '."su -c 'maildirmake -f Junk ".escapeshellcmd($maildomain_path)."' ".$mail_config['mailuser_name'],LOGLEVEL_DEBUG);
-			$app->system->maildirmake($maildomain_path, $mail_config['mailuser_name'], 'Junk');
+			$app->system->maildirmake($maildomain_path, $user, 'Junk', $group);
 		}
+
+		// Set permissions now recursive
+		exec('chown -R '.$user.':'.$group.' '.escapeshellcmd($data['new']['maildir']));
+		$app->log('Set ownership on '.escapeshellcmd($data['new']['maildir']), LOGLEVEL_DEBUG);
 
 		// Move mailbox, if domain has changed and delete old mailbox
 		if($data['new']['maildir'] != $data['old']['maildir'] && is_dir($data['old']['maildir'])) {
@@ -301,8 +339,8 @@
 		// Courier Layout
 		if(is_dir($data['new']['maildir'].'/new') && $mail_config['pop3_imap_daemon'] != 'dovecot') {
 			if($data['new']['quota'] > 0) {
-				if(is_dir($data['new']['maildir'])) exec("su -c 'maildirmake -q ".$data['new']['quota']."S ".escapeshellcmd($data['new']['maildir'])."' ".$mail_config['mailuser_name']);
-				$app->log('Updated Maildir quota: '."su -c 'maildirmake -q ".$data['new']['quota']."S ".escapeshellcmd($data['new']['maildir'])."' ".$mail_config['mailuser_name'], LOGLEVEL_DEBUG);
+				if(is_dir($data['new']['maildir'])) exec("su -c 'maildirmake -q ".$data['new']['quota']."S ".escapeshellcmd($data['new']['maildir'])."' ".$user);
+				$app->log('Updated Maildir quota: '."su -c 'maildirmake -q ".$data['new']['quota']."S ".escapeshellcmd($data['new']['maildir'])."' ".$user, LOGLEVEL_DEBUG);
 			} else {
 				if(file_exists($data['new']['maildir'].'/maildirsize')) unlink($data['new']['maildir'].'/maildirsize');
 				$app->log('Set Maildir quota to unlimited.', LOGLEVEL_DEBUG);

--
Gitblit v1.9.1