d0d307a
Cyrus IMAP autocreate Inbox patch
d0d307a
----------------------------------
d0d307a
d0d307a
NOTE : This patch has been created at the University of Athens. For more info, as well 
cf58a5c
as more patches on Cyrus IMAPD server, please visit http://email.uoa.gr/ 
d0d307a
d0d307a
The design of Cyrus IMAP server does not predict the automatic creation of users'
d0d307a
INBOX folders. The creation of a user's INBOX is considered to be an external task,
cf58a5c
that has to be completed as part of the user email account creation procedure. 
cf58a5c
Hence, to create a new email account the site administrator has to:
cf58a5c
cf58a5c
  a) Include the new account in the user database for the authentication procedure
cf58a5c
     (e.g. sasldb, shadow, mysql, ldap).
cf58a5c
  b) Create the corresponding INBOX folder. 
d0d307a
d0d307a
Alternatively, the user, if succesfully authenticated, may create his own INBOX folder,
d0d307a
as long as the configuration of the site allows it (see "autocreatequota" in imapd.conf).
cf58a5c
Unlike what not careful readers may think, enabling the "autocreatequota" option, doesn't 
d0d307a
lead to the automatic INBOX folder creation by Cyrus IMAP server.
d0d307a
In fact, "autocreate" means that the IMAP clients are allowed to automatically create 
d0d307a
the user INBOX. 
d0d307a
d0d307a
This patch adds the functionality of automatic creation of the users' INBOX folders into
d0d307a
the Cyrus IMAP server. It is implemented as two features, namely the  "create on login"
d0d307a
and "create on post".
d0d307a
 
d0d307a
d0d307a
d0d307a
Create on login
d0d307a
===============
d0d307a
This feauture provides automatic creation of a user's INBOX folder when all of the 
d0d307a
following requirements are met:
d0d307a
d0d307a
i)  The user has succesfully passed the authentication procedure.
d0d307a
cf58a5c
ii) The user's authorisation ID (typically the same as the user's
d0d307a
authentication ID)  doesn't belong to the imap_admins or admins
d0d307a
accounts (see imapd.conf).
d0d307a
d0d307a
iii) The "autocreatequota" option in the imap configuration file 
d0d307a
has been set to a non zero value. 
d0d307a
cf58a5c
iv) The corresponding to the user's authorisation ID INBOX folder
d0d307a
does not exist.
d0d307a
d0d307a
The user's first login is the most typical case when all four requirements are met. 
cf58a5c
Note that if the authenticated ID is allowed to proxy to another account for which 
d0d307a
all of the above requirements are met, the corresponding INBOX folder for that account 
d0d307a
will be created.
d0d307a
d0d307a
d0d307a
d0d307a
Create on post
d0d307a
==============
d0d307a
This feauture provides automatic creation of a user's INBOX folder when all of the 
d0d307a
following requirements are met. 
d0d307a
cf58a5c
i) An email message addressed to the user has been received.  
d0d307a
d0d307a
ii) The recipient is not any of the imap_admins or admins accounts. 
cf58a5c
Note that passing emails to admins or imap_admins accounts from 
d0d307a
the MTA to LMTP should be avoided in any case.
d0d307a
d0d307a
iii) The recipient's INBOX does not exist.
d0d307a
d0d307a
iv) The "autocreatequota" option in the imap configuration file 
d0d307a
has been set to a non zero value. 
d0d307a
d0d307a
v) The "createonpost" option in the imap configuration file 
d0d307a
has been switched on. 
d0d307a
d0d307a
d0d307a
Besides the automatic creation of INBOX folder, additional functionalities are
d0d307a
provided:
d0d307a
cf58a5c
  (A) Automatic creation of INBOX subfolders controlled by "autocreateinboxfolders"
d0d307a
configuration option. eg 
d0d307a
d0d307a
autocreateinboxfolders: sent|drafts|spam|templates
d0d307a
cf58a5c
  (B) Automatic subscription of INBOX subfolders controlled by "autosubscribeinboxfolders"
d0d307a
configuration option. eg
d0d307a
d0d307a
autosubscribeinboxfolders: sent|spam
d0d307a
d0d307a
Obviously, only subscription to subfolders included in the "autocreateinboxfolder"
cf58a5c
list is meaningful. 
d0d307a
cf58a5c
  (C) Automatic subscription to shared folders (bulletin boards). The user gets
d0d307a
automatically subscribed to the shared folders declared in the "autosubscribesharedfolders"
d0d307a
configuration option in imapd.conf.
d0d307a
eg autosubscribesharedfolders: public_folder | public_folder.subfolder
d0d307a
d0d307a
In order the above action to succeed, the shared folder has to pre-exist the INBOX creation
cf58a5c
and the user must have the appropriate permissions in order to be able to subscribe to the
d0d307a
shared folder.
d0d307a
d0d307a
* A new config option has been added. 'autosubscribe_all_sharedfolders' is a yes/no
d0d307a
option. When set to yes, the user is automatically subscribed to all shared folders one 
d0d307a
has permission to subscribe to. Please, note that when this option is set to yes, then
d0d307a
'autosubscribesharedfolders' option is overriden.
d0d307a
cf58a5c
  (D) Automatic creation of a predefined default sieve script.
d0d307a
d0d307a
This is very useful when a default sieve script is used for every user. Usually, a
d0d307a
default anti-spam script may me be written in a file and copied to each user
d0d307a
sieve scripts upon the INBOX creation. The imapd.conf options that have been added
d0d307a
are 'autocreate_sieve_script', 'autocreate_sieve_compiledscript' and 
d0d307a
'generate_compiled_sieve_script'. 
d0d307a
d0d307a
autocreate_sieve_script configuration option refers to the full path of the file 
d0d307a
that contains the sieve script. The default value is null and if no file is defined,
d0d307a
then no default script is created upon INBOX creation. (The feature is disabled)
d0d307a
eg autocreate_sieve_script: /etc/default_sieve_script
d0d307a
d0d307a
autocreate_sieve_compiledscript configuration option refers to the full path of the
d0d307a
file that contains the bytecode compiled sieve script. If this filename is defined
d0d307a
in imapd.conf and the file exists, then it is automatically copied in the user's sieve
d0d307a
directory. If it is not defined, then a bytecode sieve script gets on the fly compiled
d0d307a
by the daemon. 
d0d307a
eg autocreate_sieve_compiledscript: /etc/default_sieve_script.bc
d0d307a
d0d307a
generate_compiled_sieve_script is a boolean option that triggers the compilation of the 
d0d307a
source sieve script to bytecode sieve script. The file that the bytecode script will
d0d307a
be saved is pointed by autocreate_sieve_compiledscript.
d0d307a
d0d307a
Ways of compiling a sieve script : 
d0d307a
1. Compile a sieve script using the standard sievec utility, distributed by CMU
d0d307a
2. Compile a sieve script using the compile_sieve utility, released by UoA. This 
d0d307a
   tool is almost identical to the sievec utility, with the difference that it 
d0d307a
   reads the input and output file from autocreate_sieve_script and 
d0d307a
   autocreate_sieve_compiledscript options in imapd.conf
d0d307a
3. Let cyrus create a compiled sieve script using a source script. Cyrus can be
d0d307a
   instructed to save the compiled script any time a compiled script does not exist.
d0d307a
d0d307a
NOTES : 
d0d307a
1. In order this functionality to work, the following requirements must have been met:
d0d307a
   - 'sieveusehomedir' option must be 'no' in the configuration (default).
d0d307a
   - 'sievedir' option must have a valid value.
d0d307a
2. Currently, this patch checks the validity of the source script while generating a 
d0d307a
   bytecode compiled script, but not the validity of the bytecode sieve script file.
d0d307a
   The administrator should make sure that the provided files contain a valid sieve
d0d307a
   script as well as the compiled script is updated every time the source script changes.
d0d307a
d0d307a
cf58a5c
  (E) The administrator may control for which users and/or groups may the INBOXes 
cf58a5c
automatically be created. The autocreate_users option restricts the groups
cf58a5c
for which the patch will create the mailboxes. 
cf58a5c
cf58a5c
The default value of autocreate_users is anyone. So, if not set at all, the patch will 
cf58a5c
work for all users. However, one may set:
cf58a5c
cf58a5c
autocreate_users: user1 user2 group:group1 group:group2
cf58a5c
cf58a5c
In that case, the INBOX will be created only for user1, user2 and the users that belong 
cf58a5c
to group1 and group2. 
cf58a5c
cf58a5c
More refined control per service is provided by the options imap_autocreate_users,
cf58a5c
pop3_autocreate_users and lmtp_autocreate_users. These options override the 
cf58a5c
autocreate_users option and offer per service control.
cf58a5c
cf58a5c
Example: 
cf58a5c
One may want to restrict the create on post functionality only for a specific group
cf58a5c
of users. To achieve this, the following lines must be added in the imapd.conf file:
cf58a5c
cf58a5c
createonpost: yes
cf58a5c
lmtp_autocreate_users: group:groupname
cf58a5c
cf58a5c
d0d307a
d0d307a
Issues to be considered 
d0d307a
=======================
d0d307a
d0d307a
I) In order to use the create on post feauture one should be absolutely sure that: 
cf58a5c
a) The MTA checks the validity of the email recipient before sending the email to
d0d307a
LMTP. This is an RFC821 requirement. This usually expands to "the mta should be 
d0d307a
able to use the account database as user mailbox database". 
cf58a5c
b) Only authorised accounts/services can talk to LMTP.
d0d307a
d0d307a
II) Especially in the case of imap logins, the current patch implementation checks
d0d307a
for the INBOX folder existence upon login, causing an extra mailbox lookup in most 
d0d307a
of the cases. 
d0d307a
A better approach would be to chase the "IMAP_MAILBOX_NONEXISTENT" error code and
d0d307a
check if the error is associated with an INBOX folder. However, this would mess up
d0d307a
Cyrus code. The way it was implemented may not have been the most performance
cf58a5c
optimised, but it produces a much cleaner and simple patch.
d0d307a
d0d307a
d0d307a
d0d307a
Virtual Domains Support
d0d307a
=======================
d0d307a
d0d307a
Virtual domains are supported by all versions of the patch for cyrus-imapd-2.2.1-BETA and 
d0d307a
later. However, it is not possible to declare different INBOX subfolders to be created or 
d0d307a
shared folders to be subscribed to for every domain.
d0d307a
d0d307a
d0d307a
d0d307a
Things to be done
d0d307a
=================
d0d307a
cf58a5c
1. Support MUPDATE
cf58a5c
cf58a5c
It is within the developers' intentions to support the mupdate protocol, but serious 
cf58a5c
design issues on future cyrus releases have to resolved first.
d0d307a
cf58a5c
2. Select different attributes (quota, partition, sieve filter, etc) depending on the group
cf58a5c
a user belongs to. 
d0d307a
cf58a5c
For more information and updates please visit http://email.uoa.gr/projects/cyrus/autocreate
d0d307a