User Tools

Site Tools


start:howto:dovecot_plus_database_quota

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
Next revision Both sides next revision
start:howto:dovecot_plus_database_quota [2012/01/09 13:58]
aseques
start:howto:dovecot_plus_database_quota [2012/01/10 08:46]
aseques
Line 1: Line 1:
 **__ALERT: This document is currently being rewritten, only experienced people should use this__** **__ALERT: This document is currently being rewritten, only experienced people should use this__**
  
-====== Dovecot modifications from default imscp install ​======+===== Dovecot modifications from default imscp install =====
 This is an improved version from the original article in ispcp (http://​isp-control.net/​documentation/​howto:​mail:​replace_courier_with_dovecot) This is an improved version from the original article in ispcp (http://​isp-control.net/​documentation/​howto:​mail:​replace_courier_with_dovecot)
 All the parts related to the default install where removed since imscp already takes care of that, and it's focused to add the functionality that is available only on dovecot (instead of courier). All the parts related to the default install where removed since imscp already takes care of that, and it's focused to add the functionality that is available only on dovecot (instead of courier).
 +The required dovecot version is now 1.2 (squeeze'​s default)
  
 ===== Changes from standard dovecot in imscp ===== ===== Changes from standard dovecot in imscp =====
-  * Quotas are stored on database so they can be shown from the control panel +  * Alias and subdomains ​can be used as a full mail domains 
-  * Required dovecot version is now 1.2 (squeeze'​s default) +  * Warning users that are overquota
-  * Filtering is enabled by default (to archive spam)+
  
 ===== Reasons to use dovecot ===== ===== Reasons to use dovecot =====
Line 34: Line 34:
 ==== Sieve filtering ==== ==== Sieve filtering ====
  
-Create ​the sieve script ​to redirect spam to junk folder +There is the global ​script ​located in /​etc/​dovecot/​sieve/​dovecot.sieve,​ it can be edited to suit your needs with more functionality. 
-**/​etc/​dovecot/​sieve/​dovecot.sieve**  +In the default install it moves all the mails tagged as spam to the Junk folder ​of the mail user. 
-If you have a spam filter such as amavis, it will add the header "​X-Spam-Flag = yes", with this little sieve script all this mail will go to Junk folder (can be seen both in webmail or any IMAP client)It's really usefull because ​all the users can check their own spam without ​the need of the server admin.+More info about sieve syntax and usage: http://​wiki.dovecot.org/​LDA/​Sieve
  
-  mkdir /etc/​dovecot/​sieve +==== Use domain alias/subdomain as a mail alias ====
-  chown -R vmail:mail /​etc/​dovecot/​sieve/​ +
-  touch /​etc/​dovecot/​sieve/​dovecot.sieve+
  
-  require "​fileinto";​ +By doing a small change in postfix configuration,​ we can use the domain alias as a mail ailas. 
-    if exists "​X-Spam-Flag"​ { +Being example2.com an alias of example1.com,​ whenever ​we create a mailbox in example1.com the same mailbox in example2.com becomes a redirection. 
-            if header :contains "​X-Spam-Flag"​ "​NO"​ { +So creating [email protected] will also receive the mail from [email protected] automatically. That can be used when a customer don't want the hassle of managing two mail domain (and having to set manually every mail address two times).
-            } else { +
-            fileinto "​INBOX.Junk";​ +
-            stop; +
-            } +
-    } +
- +
- +
-Set correct permissions for dovecot.conf (the deliver command will access this file too) +
- +
-  chmod 0644 /​etc/​dovecot/​dovecot.conf +
- +
-==== Use domain alias as mail alias too ==== +
- +
-Making ​a small change in postfix configuration,​ we can use the domain alias as a mail ailas. +
-Being example2.com an alias of example1.com,​ whenever ​create a mailbox in example1.com the same mailbox in example2.com becomes a redirection. +
-So creating [email protected] will also receive the mail from [email protected] automatically.+
  
 /​etc/​postfix/​mysql_virtual_domains.cf /​etc/​postfix/​mysql_virtual_domains.cf
 <​code>​ <​code>​
-user = ispcp_dovecot +user = DATABASE_USER 
-password = PASS_MAILRW+password = DATABASE_PASSWORD
 hosts = 127.0.0.1 hosts = 127.0.0.1
-dbname = ispcp+dbname = DATABASE_NAME
 query = (SELECT domain_name FROM domain_aliasses query = (SELECT domain_name FROM domain_aliasses
                 INNER JOIN domain ​ ON domain_aliasses.domain_id=domain.domain_id ​                 INNER JOIN domain ​ ON domain_aliasses.domain_id=domain.domain_id ​
Line 83: Line 65:
 /​etc/​postfix/​mysql_virtual_forwards.cf /​etc/​postfix/​mysql_virtual_forwards.cf
 <​code>​ <​code>​
-user = ispcp_dovecot +user = DATABASE_USER 
-password = PASS_MAILRW+password = DATABASE_PASSWORD
 hosts = 127.0.0.1 hosts = 127.0.0.1
-dbname = ispcp+dbname = DATABASE_NAME
 query = query =
         SELECT DISTINCT CONCAT(CONCAT(mail_users.mail_acc,​ "​@"​),​ domain.domain_name) from mail_users         SELECT DISTINCT CONCAT(CONCAT(mail_users.mail_acc,​ "​@"​),​ domain.domain_name) from mail_users
Line 93: Line 75:
         WHERE domain_aliasses.alias_name = '​%d'​ AND mail_users.mail_acc = '​%u'​         WHERE domain_aliasses.alias_name = '​%d'​ AND mail_users.mail_acc = '​%u'​
 </​code>​ </​code>​
 +
 +If you want the subdomains to be able to receive mail create this file:
 +**/​etc/​dovecot/​dovecot-sql-subdomain.conf** ​
 +with the following content:
 +
 +<​code>​
 +driver = mysql
 +connect = host=localhost dbname=ispcp user=ispcp_dovecot password=PASS_MAILRW
 +user_query = SELECT CONCAT('/​var/​mail/​virtual/',​ subdomain.subdomain_name,​ "​.",​ domain.domain_name,​ '/',​mail_acc) AS home, '​1001'​ AS uid, '​8'​ AS gid FROM (mail_users INNER JOIN subdomain ON mail_users.sub_id = subdomain.subdomain_id) INNER JOIN domain ON mail_users.domain_id = domain.domain_id WHERE mail_acc='​%n'​ and concat(subdomain.subdomain_name,"​.",​domain.domain_name)='​%d';​
 +</​code>​
 +
 +If you want the domains alias to be able to receive mail:
 +**/​etc/​dovecot/​dovecot-sql-aliasdomain.conf** ​
 +with the following content:\\
 +(In the sql for user_query there are uids and gids '​hardcoded'​. The uid must be the uid of vmail user and gid the gid of the mail group. Maybe you have to change the defaults (1001/8))
 +
 +<​code>​
 +driver = mysql
 +connect = host=localhost dbname=ispcp user=ispcp_dovecot password=PASS_MAILRW
 +user_query = SELECT concat('/​var/​mail/​virtual/',​ domain_aliasses.alias_name,​ '/',​mail_acc) as home, '​1001'​ as uid, '​8'​ as gid FROM (mail_users INNER JOIN domain_aliasses ON mail_users.sub_id = domain_aliasses.alias_id) INNER JOIN domain ON mail_users.domain_id = domain.domain_id WHERE mail_acc='​%n'​ and domain_aliasses.alias_name ='​%d';​
 +</​code>​
 +
 +You have to add in dovecot.conf (section "auth default"​):​
 +  userdb sql {
 +    args = /​etc/​dovecot/​dovecot-sql-subdomain.conf
 +  }
 +  userdb sql {
 +    args = /​etc/​dovecot/​dovecot-sql-aliasdomain.conf
 +  }
  
 ==== Automatically alert user if his quota exceeds a critical limit ==== ==== Automatically alert user if his quota exceeds a critical limit ====
Line 104: Line 115:
 If the user's quota reaches the limit the external script dovecot-quota-warning.sh is run. If you define quota warnings make sure that you start with the highest limit because Dovecot only runs the command for the first limit that is exceeded. If the user's quota reaches the limit the external script dovecot-quota-warning.sh is run. If you define quota warnings make sure that you start with the highest limit because Dovecot only runs the command for the first limit that is exceeded.
  
-Create ​**/​usr/​local/​bin/​dovecot-quota-warning.sh**+create ​**/​usr/​local/​bin/​dovecot-quota-warning.sh** 
 +Give appropiate permissions to the file 
 +  chmod 755 /​usr/​local/​bin/​dovecot-quota-warning.sh
 <​code>​ <​code>​
 #!/bin/sh #!/bin/sh
Line 148: Line 161:
     flags=DROhu user=vmail:​mail argv=/​usr/​libexec/​dovecot/​deliver -f ${sender} -d ${recipient}     flags=DROhu user=vmail:​mail argv=/​usr/​libexec/​dovecot/​deliver -f ${sender} -d ${recipient}
  
-===== Older documentation =====+===== Older documentation ​(pending to be purged) ​=====
  
- 
-=== 1. Create a new table in ispcp called quota_dovecot === 
- 
-This will create also the database to store quota 
-  mysql -u root -p mysql 
-  mysql> USE ispcp; 
-  mysql> CREATE TABLE quota_dovecot ( 
-    username varchar(100) not null, 
-    bytes bigint not null default 0, 
-    messages integer not null default 0, 
-    primary key (username) 
-    ); 
-  ​ 
-=== 2. Granting permissions to the sql user === 
- 
-  mysql> GRANT SELECT,​INSERT,​UPDATE ON imscp.quota_dovecot to '​ispcp_dovecot'​@'​localhost'​ 
-  mysql> FLUSH PRIVILEGES; 
-  mysql> QUIT; 
- 
-=== 4. Configure Dovecot === 
- 
-Replace /​etc/​dovecot/​dovecot.conf with the following one (you should backup the original because there are a lot of comments in it explaining all the configuration parameters). 
- 
-  cp /​etc/​dovecot/​dovecot.conf /​etc/​dovecot/​dovecot.conf.org 
- 
-Then save this content as your /​etc/​dovecot/​dovecot.conf 
- 
-<​code>​ 
-base_dir = /​var/​run/​dovecot/​ 
- 
-protocols = imap pop3 
-#Uncomment if using ssl 
-#protocols = imap pop3 imaps pop3s 
-disable_plaintext_auth = no 
-listen=* 
-syslog_facility = mail 
- 
-login_greeting = Dovecot ready. 
- 
-mail_location = maildir:%h 
-mail_privileged_group = mail 
- 
- #​Uncomment these lines to enable dovecot ssl support 
- #​Dovecot 1.2+ (i.e *BSD ports, lenny backport or squeeze) 
- ssl = yes 
- # 
- #​Certificate location 
- #​ssl_cert_file = /​etc/​ssl/​certs/​servername.crt 
- #​ssl_key_file = /​etc/​ssl/​certs/​servername.key 
- 
-protocol imap { 
-  mail_plugins = quota imap_quota 
-} 
- 
-#Uncomment to enable debugging 
-#mail_debug = yes 
-#auth_debug = yes 
-#​auth_verbose = yes 
- 
-namespace private { 
- ​prefix = INBOX. 
- inbox = yes 
-} 
- 
-protocol pop3 { 
- ​pop3_uidl_format = %u-%v 
- ​mail_plugins = quota 
-} 
- 
-protocol lda { 
- ​postmaster_address = [email protected] 
- ​auth_socket_path = /​var/​run/​dovecot/​auth-master 
- ​mail_plugins = quota sieve 
-} 
- 
-auth default { 
- ​mechanisms = plain login 
- ​passdb checkpassword { 
-  args = /​var/​www/​ispcp/​engine/​ispcp-dovecot-mngr 
- } 
- ​userdb sql { 
-  args = /​etc/​dovecot/​dovecot-sql-domain.conf 
- } 
- ​userdb sql { 
-   args = /​etc/​dovecot/​dovecot-sql-aliasdomain.conf 
- } 
- 
- ​userdb sql { 
-  args = /​etc/​dovecot/​dovecot-sql-subdomain.conf 
- } 
- 
- ​socket listen { 
-  client { 
-   path = /​var/​spool/​postfix/​private/​auth 
-   mode = 0660 
-   user = postfix 
-   group = postfix 
-  } 
- 
-  master { 
-   path = /​var/​run/​dovecot/​auth-master 
-   mode = 0660 
-   user = vmail 
-   group = mail 
-  } 
- } 
- 
- user = root 
-} 
- 
-#If you don't want to save quota occupation in a database, you should comment this: 
-dict { 
-  quotadict = mysql:/​etc/​dovecot/​dovecot-dict-sql.conf 
-} 
- 
-plugin { 
-  #Enable sieve for everybody (in dovecot >= 1.2 it is configured here) 
-  global_script_path = /​etc/​dovecot/​sieve/​dovecot.sieve 
-  #Uncomment this if you are using managesieve or per-user rules 
-  #sieve = ~/​sieve/​.dovecot.sieve 
-  #sieve_dir = ~/sieve 
-  ​ 
-  #Enable per-user quota 
-  #OPTION A: Using per user maildir files (no database) 
-  #quota = maildir:​User quota 
-  #OPTION B: Using per user database quota (can be read from ispCP) 
-  quota = dict:​user::​proxy::​quotadict 
-  quota_rule = *:bytes=1G 
-} 
-</​code>​ 
- 
-Create a new file  
-**/​etc/​dovecot/​dovecot-sql-domain.conf** ​ 
-with the following content: \\ 
 **NOTE:** In the SQL query for user_query there are uids and gids '​hardcoded'​. The uid must be the uid of your vmail user and gid the gid of the mail group. Maybe you have to change the defaults (1001/8)) **NOTE:** In the SQL query for user_query there are uids and gids '​hardcoded'​. The uid must be the uid of your vmail user and gid the gid of the mail group. Maybe you have to change the defaults (1001/8))
  
-  driver = mysql 
-  connect = host=localhost dbname=ispcp user=ispcp_dovecot password=PASS_MAILRW 
-  user_query = SELECT CONCAT('/​var/​mail/​virtual/',​ domain.domain_name,​ '/',​mail_acc) AS home, '​1001'​ AS uid, '​8'​ AS gid, CONCAT('​maildir:​storage=',​ FLOOR(quota/​1024)) AS quota FROM mail_users INNER JOIN domain ON mail_users.domain_id = domain.domain_id WHERE mail_acc='​%n'​ and domain.domain_name='​%d'​ AND (mail_type='​normal_mail'​ OR mail_type='​normal_mail,​normal_forward'​);​ 
-  ​ 
-  # Use this for Dovecot 1.1+ 
-  # user_query = SELECT CONCAT('/​var/​mail/​virtual/',​ domain.domain_name,​ '/',​mail_acc) AS home, '​1001'​ AS uid, '​8'​ AS gid, CONCAT('​*:​bytes=',​quota,'​B'​) AS quota_rule FROM mail_users INNER JOIN domain ON mail_users.domain_id = domain.domain_id WHERE mail_acc='​%n'​ AND domain.domain_name='​%d'​ AND (mail_type='​normal_mail'​ OR mail_type='​normal_mail,​normal_forward'​);​ 
-  ​ 
-  #Use this if you don't want quota 
-  # user_query = SELECT CONCAT('/​var/​mail/​virtual/',​ domain.domain_name,​ '/',​mail_acc) AS home, '​1001'​ AS uid, '​8'​ AS gid FROM mail_users INNER JOIN domain ON mail_users.domain_id = domain.domain_id WHERE mail_acc='​%n'​ AND domain.domain_name='​%d'​ AND (mail_type='​normal_mail'​ OR mail_type='​normal_mail,​normal_forward'​);​ 
- 
-Create a new file  
-**/​etc/​dovecot/​dovecot-sql-subdomain.conf** ​ 
-with the following content:\\ 
-(In the sql for user_query there are uids and gids '​hardcoded'​. The uid must be the uid of vmail user and gid the gid of the mail group. Maybe you have to change the defaults (1001/8)) 
- 
-<​code>​ 
-driver = mysql 
-connect = host=localhost dbname=ispcp user=ispcp_dovecot password=PASS_MAILRW 
-user_query = SELECT CONCAT('/​var/​mail/​virtual/',​ subdomain.subdomain_name,​ "​.",​ domain.domain_name,​ '/',​mail_acc) AS home, '​1001'​ AS uid, '​8'​ AS gid FROM (mail_users INNER JOIN subdomain ON mail_users.sub_id = subdomain.subdomain_id) INNER JOIN domain ON mail_users.domain_id = domain.domain_id WHERE mail_acc='​%n'​ and concat(subdomain.subdomain_name,"​.",​domain.domain_name)='​%d';​ 
-</​code>​ 
- 
-Create a new file  
-**/​etc/​dovecot/​dovecot-sql-aliasdomain.conf** ​ 
-with the following content:\\ 
-(In the sql for user_query there are uids and gids '​hardcoded'​. The uid must be the uid of vmail user and gid the gid of the mail group. Maybe you have to change the defaults (1001/8)) 
- 
-<​code>​ 
-driver = mysql 
-connect = host=localhost dbname=ispcp user=ispcp_dovecot password=PASS_MAILRW 
-user_query = SELECT concat('/​var/​mail/​virtual/',​ domain_aliasses.alias_name,​ '/',​mail_acc) as home, '​1001'​ as uid, '​8'​ as gid FROM (mail_users INNER JOIN domain_aliasses ON mail_users.sub_id = domain_aliasses.alias_id) INNER JOIN domain ON mail_users.domain_id = domain.domain_id WHERE mail_acc='​%n'​ and domain_aliasses.alias_name ='​%d';​ 
-</​code>​ 
- 
-Create the file used to store the quotas on the db 
-**/​etc/​dovecot/​dovecot-dict-sql.conf** 
-<​code>#​ v1.2+ only: 
-connect = host=localhost dbname=ispcp user=ispcp_dovecot password=PASS_MAILRW 
-map { 
-  pattern = priv/​quota/​storage 
-  table = quota_dovecot 
-  username_field = username 
-  value_field = bytes 
-} 
-map { 
-  pattern = priv/​quota/​messages 
-  table = quota_dovecot 
-  username_field = username 
-  value_field = messages 
-} 
-</​code>​ 
/var/www/virtual/i-mscp.net/wiki/htdocs/data/pages/start/howto/dovecot_plus_database_quota.txt · Last modified: 2013/06/21 07:36 by aseques