346e223
diff -Naur cyrus-imapd-2.3.12p2/README.autocreate cyrus-imapd-2.3.12p2-autocreate.uncompiled/README.autocreate
346e223
--- cyrus-imapd-2.3.12p2/README.autocreate	1970-01-01 02:00:00.000000000 +0200
346e223
+++ cyrus-imapd-2.3.12p2-autocreate.uncompiled/README.autocreate	2008-05-06 14:48:41.000000000 +0300
cf58a5c
@@ -0,0 +1,211 @@
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
+
346e223
diff -Naur cyrus-imapd-2.3.12p2/imap/Makefile.in cyrus-imapd-2.3.12p2-autocreate.uncompiled/imap/Makefile.in
346e223
--- cyrus-imapd-2.3.12p2/imap/Makefile.in	2008-04-04 00:09:51.000000000 +0300
346e223
+++ cyrus-imapd-2.3.12p2-autocreate.uncompiled/imap/Makefile.in	2008-05-06 14:49:12.000000000 +0300
cf58a5c
@@ -101,7 +101,7 @@
d0d307a
 	convert_code.o duplicate.o saslclient.o saslserver.o signals.o \
d0d307a
 	annotate.o search_engines.o squat.o squat_internal.o mbdump.o \
346e223
 	imapparse.o telemetry.o user.o notify.o idle.o quota_db.o \
346e223
-	sync_log.o $(SEEN) mboxkey.o backend.o tls.o message_guid.o \
346e223
+	sync_log.o autosieve.o $(SEEN) mboxkey.o backend.o tls.o message_guid.o \
346e223
 	statuscache_db.o
d0d307a
 
Tomas Janousek 915c4a4
 IMAPDOBJS=pushstats.o imapd.o proxy.o imap_proxy.o index.o version.o
346e223
@@ -118,7 +118,7 @@
Tomas Janousek 6861497
 	fud smmapd reconstruct quota mbpath ipurge cyr_dbtool cyr_synclog \
d0d307a
 	cyrdump chk_cyrus cvt_cyrusdb deliver ctl_mboxlist \
d0d307a
 	ctl_deliver ctl_cyrusdb squatter mbexamine cyr_expire arbitron \
cf58a5c
-	unexpunge @IMAP_PROGS@
cf58a5c
+	unexpunge compile_sieve @IMAP_PROGS@
d0d307a
 
d0d307a
 BUILTSOURCES = imap_err.c imap_err.h pushstats.c pushstats.h \
d0d307a
 	lmtpstats.c lmtpstats.h xversion.h mupdate_err.c mupdate_err.h \
346e223
@@ -183,9 +183,9 @@
Tomas Janousek 915c4a4
 mupdate_err.h: mupdate_err.c
Tomas Janousek 915c4a4
 
d0d307a
 ### Services
Tomas Janousek 915c4a4
-idled: idled.o mutex_fake.o libimap.a $(DEPLIBS)
Tomas Janousek 915c4a4
+idled: idled.o mutex_fake.o libimap.a $(SIEVE_LIBS) $(DEPLIBS)
d0d307a
 	$(CC) $(LDFLAGS) -o idled \
d0d307a
-	 idled.o mutex_fake.o libimap.a $(DEPLIBS) $(LIBS)
d0d307a
+	 idled.o mutex_fake.o libimap.a $(SIEVE_LIBS) $(DEPLIBS) $(LIBS)
d0d307a
 
cf58a5c
 lmtpd: lmtpd.o proxy.o $(LMTPOBJS) $(SIEVE_OBJS) mutex_fake.o \
cf58a5c
 	 libimap.a $(SIEVE_LIBS) $(DEPLIBS) $(SERVICE)
346e223
@@ -199,158 +199,162 @@
cf58a5c
 	 $(SERVICE) lmtpd.o proxy.o $(LMTPOBJS) $(SIEVE_OBJS) \
cf58a5c
 	 mutex_fake.o libimap.a $(SIEVE_LIBS) $(DEPLIBS) $(LIBS) $(LIB_WRAP)
d0d307a
 
Tomas Janousek 6861497
-imapd: $(IMAPDOBJS) mutex_fake.o libimap.a $(DEPLIBS) $(SERVICE)
Tomas Janousek 6861497
+imapd: $(IMAPDOBJS) mutex_fake.o libimap.a $(SIEVE_LIBS) $(DEPLIBS) $(SERVICE)
d0d307a
 	$(CC) $(LDFLAGS) -o imapd \
d0d307a
 	 $(SERVICE) $(IMAPDOBJS) mutex_fake.o \
d0d307a
-	libimap.a $(DEPLIBS) $(LIBS) $(LIB_WRAP)
d0d307a
+	libimap.a $(SIEVE_LIBS) $(DEPLIBS) $(LIBS) $(LIB_WRAP)
d0d307a
 
Tomas Janousek 915c4a4
-imapd.pure: $(IMAPDOBJS) mutex_fake.o libimap.a $(DEPLIBS) $(SERVICE)
Tomas Janousek 915c4a4
+imapd.pure: $(IMAPDOBJS) mutex_fake.o libimap.a $(SIEVE_LIBS) $(DEPLIBS) $(SERVICE)
d0d307a
 	$(PURIFY) $(PUREOPT) $(CC) $(LDFLAGS) -o imapd.pure \
Tomas Janousek 915c4a4
 	 $(SERVICE) $(IMAPDOBJS) mutex_fake.o libimap.a \
Tomas Janousek 915c4a4
-	$(DEPLIBS) $(LIBS) $(LIB_WRAP)
Tomas Janousek 915c4a4
+	$(SIEVE_LIBS) $(DEPLIBS) $(LIBS) $(LIB_WRAP)
Tomas Janousek 915c4a4
 
Tomas Janousek 915c4a4
-imapd.quant: $(IMAPDOBJS) mutex_fake.o libimap.a $(DEPLIBS) $(SERVICE)
Tomas Janousek 915c4a4
+imapd.quant: $(IMAPDOBJS) mutex_fake.o libimap.a $(SIEVE_LIBS) $(DEPLIBS) $(SERVICE)
Tomas Janousek 915c4a4
 	$(QUANTIFY) $(QUANTOPT) $(CC) $(LDFLAGS) -o imapd.quant \
Tomas Janousek 915c4a4
 	 $(SERVICE) $(IMAPDOBJS) mutex_fake.o libimap.a \
Tomas Janousek 915c4a4
-	$(DEPLIBS) $(LIBS) $(LIB_WRAP)
Tomas Janousek 915c4a4
+	$(SIEVE_LIBS) $(DEPLIBS) $(LIBS) $(LIB_WRAP)
Tomas Janousek 915c4a4
 
Tomas Janousek 915c4a4
 mupdate: mupdate.o mupdate-slave.o mupdate-client.o mutex_pthread.o tls.o \
Tomas Janousek 915c4a4
-	libimap.a $(DEPLIBS)
Tomas Janousek 915c4a4
+	libimap.a $(SIEVE_LIBS) $(DEPLIBS)
d0d307a
 	$(CC) $(LDFLAGS) -o mupdate \
d0d307a
 	 $(SERVICETHREAD) mupdate.o mupdate-slave.o mupdate-client.o \
d0d307a
 	 mutex_pthread.o tls.o libimap.a \
d0d307a
-	 $(DEPLIBS) $(LIBS) $(LIB_WRAP) -lpthread
d0d307a
+	 $(SIEVE_LIBS) $(DEPLIBS) $(LIBS) $(LIB_WRAP) -lpthread
d0d307a
 
d0d307a
 mupdate.pure: mupdate.o mupdate-slave.o mupdate-client.o mutex_pthread.o \
Tomas Janousek 915c4a4
-	libimap.a $(DEPLIBS)
Tomas Janousek 915c4a4
+	libimap.a $(SIEVE_LIBS) $(DEPLIBS)
Tomas Janousek 915c4a4
 	$(PURIFY) $(PUREOPT) $(CC) $(LDFLAGS) -o mupdate.pure \
Tomas Janousek 915c4a4
 	 $(SERVICETHREAD) mupdate.o mupdate-slave.o mupdate-client.o \
Tomas Janousek 915c4a4
-	 mutex_pthread.o libimap.a $(DEPLIBS) $(LIBS) $(LIB_WRAP) -lpthread
Tomas Janousek 915c4a4
+	 mutex_pthread.o libimap.a $(SIEVE_LIBS) $(DEPLIBS) $(LIBS) $(LIB_WRAP) -lpthread
d0d307a
 
cf58a5c
 pop3d: pop3d.o proxy.o backend.o tls.o mutex_fake.o libimap.a \
cf58a5c
-	$(DEPLIBS) $(SERVICE)
Tomas Janousek 6861497
+	$(SIEVE_LIBS) $(DEPLIBS) $(SERVICE)
cf58a5c
 	$(CC) $(LDFLAGS) -o pop3d pop3d.o proxy.o backend.o tls.o $(SERVICE) \
d0d307a
-	 mutex_fake.o libimap.a $(DEPLIBS) $(LIBS) $(LIB_WRAP)
d0d307a
+	 mutex_fake.o libimap.a $(SIEVE_LIBS) $(DEPLIBS) $(LIBS) $(LIB_WRAP)
d0d307a
 
cf58a5c
 nntpd: nntpd.o proxy.o backend.o index.o smtpclient.o spool.o tls.o \
cf58a5c
-	 mutex_fake.o nntp_err.o libimap.a $(DEPLIBS) $(SERVICE)
Tomas Janousek 915c4a4
+	 mutex_fake.o nntp_err.o libimap.a $(SIEVE_LIBS) $(DEPLIBS) $(SERVICE)
cf58a5c
 	$(CC) $(LDFLAGS) -o nntpd nntpd.o proxy.o backend.o index.o spool.o \
d0d307a
 	 smtpclient.o tls.o $(SERVICE) mutex_fake.o nntp_err.o \
d0d307a
-	 libimap.a $(DEPLIBS) $(LIBS) $(LIB_WRAP)
d0d307a
+	 libimap.a $(SIEVE_LIBS) $(DEPLIBS) $(LIBS) $(LIB_WRAP)
d0d307a
 
d0d307a
-fud: fud.o libimap.a mutex_fake.o $(DEPLIBS) $(SERVICE)
Tomas Janousek 915c4a4
+fud: fud.o libimap.a mutex_fake.o $(SIEVE_LIBS) $(DEPLIBS) $(SERVICE)
d0d307a
 	$(CC) $(LDFLAGS) -o fud $(SERVICE) fud.o mutex_fake.o libimap.a \
d0d307a
-	$(DEPLIBS) $(LIBS) $(LIB_WRAP)
d0d307a
+	$(SIEVE_LIBS) $(DEPLIBS) $(LIBS) $(LIB_WRAP)
d0d307a
 
d0d307a
-smmapd: smmapd.o libimap.a mutex_fake.o $(DEPLIBS) $(SERVICE)
Tomas Janousek 915c4a4
+smmapd: smmapd.o libimap.a mutex_fake.o $(SIEVE_LIBS) $(DEPLIBS) $(SERVICE)
d0d307a
 	$(CC) $(LDFLAGS) -o smmapd $(SERVICE) smmapd.o mutex_fake.o libimap.a \
d0d307a
-	$(DEPLIBS) $(LIBS) $(LIB_WRAP)
d0d307a
+	$(SIEVE_LIBS) $(DEPLIBS) $(LIBS) $(LIB_WRAP)
d0d307a
 
cf58a5c
 sync_server: sync_server.o sync_support.o sync_commit.o \
cf58a5c
-	imapparse.o tls.o libimap.a mutex_fake.o $(DEPLIBS) $(SERVICE)
Tomas Janousek 915c4a4
+	imapparse.o tls.o libimap.a mutex_fake.o $(SIEVE_LIBS) $(DEPLIBS) $(SERVICE)
cf58a5c
 	$(CC) $(LDFLAGS) -o \
cf58a5c
 	sync_server sync_server.o sync_support.o sync_commit.o \
cf58a5c
 	imapparse.o tls.o $(SERVICE) libimap.a mutex_fake.o \
cf58a5c
-	$(DEPLIBS) $(LIBS) $(LIB_WRAP)
cf58a5c
+	$(SIEVE_LIBS) $(DEPLIBS) $(LIBS) $(LIB_WRAP)
cf58a5c
 
d0d307a
 ### Command Line Utilities
d0d307a
-arbitron: arbitron.o $(CLIOBJS) libimap.a $(DEPLIBS)
Tomas Janousek 915c4a4
+arbitron: arbitron.o $(CLIOBJS) libimap.a $(SIEVE_LIBS) $(DEPLIBS)
d0d307a
 	$(CC) $(LDFLAGS) -o arbitron arbitron.o $(CLIOBJS) \
d0d307a
-	libimap.a $(DEPLIBS) $(LIBS)
d0d307a
+	libimap.a $(SIEVE_LIBS) $(DEPLIBS) $(LIBS)
d0d307a
 
Tomas Janousek 915c4a4
-cyr_dbtool: cyr_dbtool.o mutex_fake.o libimap.a $(DEPLIBS)
Tomas Janousek 915c4a4
+cyr_dbtool: cyr_dbtool.o mutex_fake.o libimap.a $(SIEVE_LIBS) $(DEPLIBS)
Tomas Janousek 915c4a4
 	$(CC) $(LDFLAGS) -o cyr_dbtool cyr_dbtool.o $(CLIOBJS) \
Tomas Janousek 915c4a4
-	libimap.a $(DEPLIBS) $(LIBS)
Tomas Janousek 915c4a4
+	libimap.a $(SIEVE_LIBS) $(DEPLIBS) $(LIBS)
Tomas Janousek 915c4a4
 
Tomas Janousek 6861497
-cyr_synclog: cyr_synclog.o mutex_fake.o libimap.a $(DEPLIBS)
Tomas Janousek 6861497
+cyr_synclog: cyr_synclog.o mutex_fake.o libimap.a $(SIEVE_LIBS) $(DEPLIBS)
Tomas Janousek 6861497
 	$(CC) $(LDFLAGS) -o cyr_synclog cyr_synclog.o $(CLIOBJS) \
Tomas Janousek 6861497
-	libimap.a $(DEPLIBS) $(LIBS)
Tomas Janousek 6861497
+	libimap.a $(SIEVE_LIBS) $(DEPLIBS) $(LIBS)
Tomas Janousek 6861497
 
d0d307a
-cvt_cyrusdb: cvt_cyrusdb.o mutex_fake.o libimap.a $(DEPLIBS)
Tomas Janousek 915c4a4
+cvt_cyrusdb: cvt_cyrusdb.o mutex_fake.o libimap.a $(SIEVE_LIBS) $(DEPLIBS)
d0d307a
 	$(CC) $(LDFLAGS) -o cvt_cyrusdb cvt_cyrusdb.o $(CLIOBJS) \
d0d307a
-	libimap.a $(DEPLIBS) $(LIBS)
d0d307a
+	libimap.a $(SIEVE_LIBS) $(DEPLIBS) $(LIBS)
d0d307a
 
d0d307a
-chk_cyrus: chk_cyrus.o mutex_fake.o libimap.a $(DEPLIBS)
Tomas Janousek 915c4a4
+chk_cyrus: chk_cyrus.o mutex_fake.o libimap.a $(SIEVE_LIBS) $(DEPLIBS)
d0d307a
 	$(CC) $(LDFLAGS) -o chk_cyrus chk_cyrus.o $(CLIOBJS) \
d0d307a
-	libimap.a $(DEPLIBS) $(LIBS)
d0d307a
+	libimap.a $(SIEVE_LIBS) $(DEPLIBS) $(LIBS)
d0d307a
 
cf58a5c
-deliver: deliver.o $(LMTPOBJS) proxy.o mutex_fake.o libimap.a $(DEPLIBS)
Tomas Janousek 915c4a4
+deliver: deliver.o $(LMTPOBJS) proxy.o mutex_fake.o libimap.a $(SIEVE_LIBS) $(DEPLIBS)
cf58a5c
 	$(CC) $(LDFLAGS) -o deliver deliver.o $(LMTPOBJS) proxy.o \
d0d307a
-	mutex_fake.o libimap.a $(DEPLIBS) $(LIBS)
d0d307a
+	mutex_fake.o libimap.a $(SIEVE_LIBS) $(DEPLIBS) $(LIBS)
d0d307a
 
d0d307a
-ctl_deliver: ctl_deliver.o $(CLIOBJS) libimap.a $(DEPLIBS)
Tomas Janousek 915c4a4
+ctl_deliver: ctl_deliver.o $(CLIOBJS) libimap.a $(SIEVE_LIBS) $(DEPLIBS)
d0d307a
 	$(CC) $(LDFLAGS) -o \
d0d307a
-	 $@ ctl_deliver.o $(CLIOBJS) libimap.a $(DEPLIBS) $(LIBS)
d0d307a
+	 $@ ctl_deliver.o $(CLIOBJS) libimap.a $(SIEVE_LIBS) $(DEPLIBS) $(LIBS)
d0d307a
 
d0d307a
-ctl_mboxlist: ctl_mboxlist.o mupdate-client.o $(CLIOBJS) libimap.a $(DEPLIBS)
Tomas Janousek 915c4a4
+ctl_mboxlist: ctl_mboxlist.o mupdate-client.o $(CLIOBJS) libimap.a $(SIEVE_LIBS) $(DEPLIBS)
d0d307a
 	$(CC) $(LDFLAGS) -o $@ ctl_mboxlist.o mupdate-client.o $(CLIOBJS) \
d0d307a
-	libimap.a $(DEPLIBS) $(LIBS)
d0d307a
+	libimap.a $(SIEVE_LIBS) $(DEPLIBS) $(LIBS)
d0d307a
 
d0d307a
-ctl_cyrusdb: ctl_cyrusdb.o $(CLIOBJS) libimap.a $(DEPLIBS)
Tomas Janousek 915c4a4
+ctl_cyrusdb: ctl_cyrusdb.o $(CLIOBJS) libimap.a $(SIEVE_LIBS) $(DEPLIBS)
d0d307a
 	$(CC) $(LDFLAGS) -o \
d0d307a
-	 $@ ctl_cyrusdb.o $(CLIOBJS) libimap.a $(DEPLIBS) $(LIBS)
d0d307a
+	 $@ ctl_cyrusdb.o $(CLIOBJS) libimap.a $(SIEVE_LIBS) $(DEPLIBS) $(LIBS)
d0d307a
 
d0d307a
-cyr_expire: cyr_expire.o $(CLIOBJS) libimap.a $(DEPLIBS)
Tomas Janousek 915c4a4
+cyr_expire: cyr_expire.o $(CLIOBJS) libimap.a $(SIEVE_LIBS) $(DEPLIBS)
d0d307a
 	$(CC) $(LDFLAGS) -o $@ cyr_expire.o $(CLIOBJS) \
d0d307a
-	libimap.a $(DEPLIBS) $(LIBS)
d0d307a
+	libimap.a $(SIEVE_LIBS) $(DEPLIBS) $(LIBS)
d0d307a
 
d0d307a
-fetchnews: fetchnews.o $(CLIOBJS) libimap.a $(DEPLIBS)
Tomas Janousek 915c4a4
+fetchnews: fetchnews.o $(CLIOBJS) libimap.a $(SIEVE_LIBS) $(DEPLIBS)
d0d307a
 	$(CC) $(LDFLAGS) -o \
d0d307a
-	 $@ fetchnews.o $(CLIOBJS) libimap.a $(DEPLIBS) $(LIBS)
d0d307a
+	 $@ fetchnews.o $(CLIOBJS) libimap.a $(SIEVE_LIBS) $(DEPLIBS) $(LIBS)
d0d307a
 
d0d307a
-squatter: squatter.o index.o squat_build.o $(CLIOBJS) libimap.a $(DEPLIBS)
Tomas Janousek 915c4a4
+squatter: squatter.o index.o squat_build.o $(CLIOBJS) libimap.a $(SIEVE_LIBS) $(DEPLIBS)
d0d307a
 	$(CC) $(LDFLAGS) -o squatter squatter.o index.o squat_build.o \
d0d307a
-	$(CLIOBJS) libimap.a $(DEPLIBS) $(LIBS)
d0d307a
+	$(CLIOBJS) libimap.a $(SIEVE_LIBS) $(DEPLIBS) $(LIBS)
d0d307a
 
d0d307a
-mbpath: mbpath.o $(CLIOBJS) libimap.a $(DEPLIBS)
Tomas Janousek 915c4a4
+mbpath: mbpath.o $(CLIOBJS) libimap.a $(SIEVE_LIBS) $(DEPLIBS)
d0d307a
 	$(CC) $(LDFLAGS) -o mbpath mbpath.o $(CLIOBJS) libimap.a \
d0d307a
-	$(DEPLIBS) $(LIBS)
cf58a5c
+	$(SIEVE_LIBS) $(DEPLIBS) $(LIBS)
Tomas Janousek 915c4a4
 
Tomas Janousek 915c4a4
-ipurge: ipurge.o $(CLIOBJS) libimap.a $(DEPLIBS)
Tomas Janousek 915c4a4
+ipurge: ipurge.o $(CLIOBJS) libimap.a $(SIEVE_LIBS) $(DEPLIBS)
d0d307a
 	$(CC) $(LDFLAGS) -o ipurge ipurge.o $(CLIOBJS) \
d0d307a
-	libimap.a $(DEPLIBS) $(LIBS)
d0d307a
+	libimap.a $(SIEVE_LIBS) $(DEPLIBS) $(LIBS)
d0d307a
 
d0d307a
-cyrdump: cyrdump.o index.o $(CLIOBJS) libimap.a $(DEPLIBS)
Tomas Janousek 915c4a4
+cyrdump: cyrdump.o index.o $(CLIOBJS) libimap.a $(SIEVE_LIBS) $(DEPLIBS)
d0d307a
 	$(CC) $(LDFLAGS) -o cyrdump cyrdump.o index.o $(CLIOBJS) \
d0d307a
-	libimap.a $(DEPLIBS) $(LIBS)
d0d307a
+	libimap.a $(SIEVE_LIBS) $(DEPLIBS) $(LIBS)
d0d307a
 
d0d307a
-mbexamine: mbexamine.o $(CLIOBJS) libimap.a $(DEPLIBS)
Tomas Janousek 915c4a4
+mbexamine: mbexamine.o $(CLIOBJS) libimap.a $(SIEVE_LIBS) $(DEPLIBS)
d0d307a
 	$(CC) $(LDFLAGS) -o \
d0d307a
-	 mbexamine mbexamine.o $(CLIOBJS) libimap.a $(DEPLIBS) $(LIBS)
d0d307a
+	 mbexamine mbexamine.o $(CLIOBJS) libimap.a $(SIEVE_LIBS) $(DEPLIBS) $(LIBS)
d0d307a
 
d0d307a
-reconstruct: reconstruct.o $(CLIOBJS) libimap.a $(DEPLIBS)
Tomas Janousek 915c4a4
+reconstruct: reconstruct.o $(CLIOBJS) libimap.a $(SIEVE_LIBS) $(DEPLIBS)
d0d307a
 	$(CC) $(LDFLAGS) -o \
d0d307a
-	 reconstruct reconstruct.o $(CLIOBJS) libimap.a $(DEPLIBS) $(LIBS)
d0d307a
+	 reconstruct reconstruct.o $(CLIOBJS) libimap.a $(SIEVE_LIBS) $(DEPLIBS) $(LIBS)
d0d307a
 
d0d307a
-quota: quota.o $(CLIOBJS) libimap.a $(DEPLIBS)
Tomas Janousek 915c4a4
+quota: quota.o $(CLIOBJS) libimap.a $(SIEVE_LIBS) $(DEPLIBS)
d0d307a
 	$(CC) $(LDFLAGS) -o quota quota.o $(CLIOBJS) \
d0d307a
-	libimap.a $(DEPLIBS) $(LIBS)
d0d307a
+	libimap.a $(SIEVE_LIBS) $(DEPLIBS) $(LIBS)
d0d307a
 
d0d307a
-tls_prune: tls_prune.o tls.o $(CLIOBJS) libimap.a $(DEPLIBS)
Tomas Janousek 915c4a4
+tls_prune: tls_prune.o tls.o $(CLIOBJS) libimap.a $(SIEVE_LIBS) $(DEPLIBS)
d0d307a
 	$(CC) $(LDFLAGS) -o \
d0d307a
-	 $@ tls_prune.o tls.o $(CLIOBJS) libimap.a $(DEPLIBS) $(LIBS)
d0d307a
+	 $@ tls_prune.o tls.o $(CLIOBJS) libimap.a $(SIEVE_LIBS) $(DEPLIBS) $(LIBS)
d0d307a
 
cf58a5c
-unexpunge: unexpunge.o $(CLIOBJS) libimap.a $(DEPLIBS)
Tomas Janousek 915c4a4
+unexpunge: unexpunge.o $(CLIOBJS) libimap.a $(SIEVE_LIBS) $(DEPLIBS)
cf58a5c
 	$(CC) $(LDFLAGS) -o $@ unexpunge.o $(CLIOBJS) \
cf58a5c
-	libimap.a $(DEPLIBS) $(LIBS)
cf58a5c
+	libimap.a $(SIEVE_LIBS) $(DEPLIBS) $(LIBS)
cf58a5c
 
cf58a5c
-make_md5: make_md5.o libimap.a mutex_fake.o $(DEPLIBS)
cf58a5c
-	$(CC) $(LDFLAGS) -o make_md5 make_md5.o libimap.a mutex_fake.o $(DEPLIBS) $(LIBS)
Tomas Janousek 915c4a4
+make_md5: make_md5.o libimap.a mutex_fake.o $(SIEVE_LIBS) $(DEPLIBS)
cf58a5c
+	$(CC) $(LDFLAGS) -o make_md5 make_md5.o libimap.a mutex_fake.o $(SIEVE_LIBS) $(DEPLIBS) $(LIBS)
cf58a5c
 
Tomas Janousek 6861497
-make_sha1: make_sha1.o libimap.a mutex_fake.o $(DEPLIBS)
Tomas Janousek 6861497
-	$(CC) $(LDFLAGS) -o make_sha1 make_sha1.o libimap.a mutex_fake.o $(DEPLIBS) $(LIBS)
Tomas Janousek 6861497
+make_sha1: make_sha1.o libimap.a mutex_fake.o $(SIEVE_LIBS) $(DEPLIBS)
Tomas Janousek 6861497
+	$(CC) $(LDFLAGS) -o make_sha1 make_sha1.o libimap.a mutex_fake.o $(SIEVE_LIBS) $(DEPLIBS) $(LIBS)
Tomas Janousek 6861497
 
cf58a5c
 sync_client: sync_client.o sync_support.o \
cf58a5c
-	backend.o tls.o imapparse.o libimap.a mutex_fake.o $(DEPLIBS)
Tomas Janousek 915c4a4
+	backend.o tls.o imapparse.o libimap.a mutex_fake.o $(SIEVE_LIBS) $(DEPLIBS)
cf58a5c
 	$(CC) $(LDFLAGS) -o \
cf58a5c
 	sync_client sync_client.o sync_support.o \
cf58a5c
-	backend.o tls.o imapparse.o libimap.a mutex_fake.o $(DEPLIBS) $(LIBS)
cf58a5c
+	backend.o tls.o imapparse.o libimap.a mutex_fake.o $(SIEVE_LIBS) $(DEPLIBS) $(LIBS)
cf58a5c
 
cf58a5c
 sync_reset: sync_reset.o sync_support.o sync_commit.o \
cf58a5c
-	libimap.a mutex_fake.o $(DEPLIBS)
Tomas Janousek 915c4a4
+	libimap.a mutex_fake.o $(SIEVE_LIBS) $(DEPLIBS)
cf58a5c
 	$(CC) $(LDFLAGS) -o \
cf58a5c
 	sync_reset sync_reset.o sync_support.o sync_commit.o \
cf58a5c
-	libimap.a mutex_fake.o $(DEPLIBS) $(LIBS)
cf58a5c
+	libimap.a mutex_fake.o $(SIEVE_LIBS) $(DEPLIBS) $(LIBS)
cf58a5c
+
cf58a5c
+compile_sieve: compile_sieve.o libimap.a $(DEPLIBS) $(SIEVE_LIBS)
cf58a5c
+	$(CC) $(LDFLAGS) -o compile_sieve compile_sieve.o $(CLIOBJS) \
cf58a5c
+	libimap.a $(SIEVE_LIBS) $(DEPLIBS) $(LIBS)
cf58a5c
 
d0d307a
 ### Other Misc Targets
d0d307a
 
346e223
diff -Naur cyrus-imapd-2.3.12p2/imap/autosieve.c cyrus-imapd-2.3.12p2-autocreate.uncompiled/imap/autosieve.c
346e223
--- cyrus-imapd-2.3.12p2/imap/autosieve.c	1970-01-01 02:00:00.000000000 +0200
346e223
+++ cyrus-imapd-2.3.12p2-autocreate.uncompiled/imap/autosieve.c	2008-05-06 14:48:41.000000000 +0300
Tomas Janousek 6861497
@@ -0,0 +1,590 @@
d0d307a
+#include <stdio.h>
d0d307a
+#include <stdlib.h>
d0d307a
+#include <string.h>
d0d307a
+
d0d307a
+#ifdef HAVE_UNISTD_H
d0d307a
+#include <unistd.h>
d0d307a
+#endif
d0d307a
+
d0d307a
+#include <errno.h>
d0d307a
+#include <sys/types.h>
d0d307a
+#include <sys/stat.h>
d0d307a
+#include <sys/uio.h>
d0d307a
+#include <fcntl.h>
d0d307a
+#include <ctype.h>
d0d307a
+#include <time.h>
d0d307a
+#include <syslog.h>
d0d307a
+#include <com_err.h>
d0d307a
+#include <config.h>
d0d307a
+
d0d307a
+#include "global.h"
d0d307a
+#include "util.h"
Tomas Janousek 6861497
+#include "xmalloc.h"
Tomas Janousek 6861497
+#include "xstrlcpy.h"
Tomas Janousek 6861497
+#include "xstrlcat.h"
d0d307a
+#include "mailbox.h"
d0d307a
+#include "imap_err.h"
d0d307a
+#include "sieve_interface.h"
d0d307a
+#include "script.h"
d0d307a
+
d0d307a
+#define TIMSIEVE_FAIL 	-1
d0d307a
+#define TIMSIEVE_OK 	0
d0d307a
+#define MAX_FILENAME	1024
d0d307a
+
d0d307a
+static int get_script_name(char *sievename, size_t buflen, const char *filename);
d0d307a
+static int get_script_dir(char *sieve_script_dir, size_t buflen, char *userid, const char *sieve_dir);
d0d307a
+int autoadd_sieve(char *userid, const char *source_script);
d0d307a
+
Tomas Janousek 6861497
+//static void fatal(const char *s, int code);
d0d307a
+static void foo(void);
d0d307a
+static int sieve_notify(void *ac __attribute__((unused)),
d0d307a
+                        void *interp_context __attribute__((unused)),
d0d307a
+                        void *script_context __attribute__((unused)),
d0d307a
+                        void *message_context __attribute__((unused)),
d0d307a
+                        const char **errmsg __attribute__((unused)));
d0d307a
+static int mysieve_error(int lineno, const char *msg,
d0d307a
+                  void *i __attribute__((unused)), void *s);
d0d307a
+static int is_script_parsable(FILE *stream, char **errstr, sieve_script_t **ret);
d0d307a
+
d0d307a
+
d0d307a
+sieve_vacation_t vacation2 = {
d0d307a
+    0,                          /* min response */
d0d307a
+    0,                          /* max response */
d0d307a
+    (sieve_callback *) &foo,    /* autorespond() */
d0d307a
+    (sieve_callback *) &foo     /* send_response() */
d0d307a
+};
d0d307a
+
d0d307a
+
d0d307a
+/*
d0d307a
+ * Find the name of the sieve script
d0d307a
+ * given the source script and compiled script names
d0d307a
+ */
d0d307a
+static int get_script_name(char *sievename, size_t buflen, const char *filename)
d0d307a
+{
d0d307a
+  char *p;
d0d307a
+  int r;
d0d307a
+
d0d307a
+  p = strrchr(filename, '/');
d0d307a
+  if (p == NULL)
d0d307a
+      p = (char *) filename;
d0d307a
+  else
d0d307a
+      p++;
d0d307a
+
d0d307a
+  r = strlcpy(sievename, p, buflen) - buflen;
d0d307a
+  return (r >= 0 || r == -buflen ? 1 : 0);
d0d307a
+}
d0d307a
+
d0d307a
+
d0d307a
+/*
d0d307a
+ * Find the directory where the sieve scripts of the user
d0d307a
+ * reside
d0d307a
+ */
d0d307a
+static int get_script_dir(char *sieve_script_dir, size_t buflen, char *userid, const char *sieve_dir)
d0d307a
+{
d0d307a
+    char *user = NULL, *domain = NULL;
d0d307a
+
d0d307a
+    /* Setup the user and the domain */
d0d307a
+    if(config_virtdomains && (domain = strchr(userid, '@'))) {
d0d307a
+        user = (char *) xmalloc((domain - userid +1) * sizeof(char));
d0d307a
+        strlcpy(user, userid, domain - userid + 1);
d0d307a
+        domain++;
d0d307a
+    } else
d0d307a
+        user = userid;
d0d307a
+
d0d307a
+    /*  Find the dir path where the sieve scripts of the user will reside */   
d0d307a
+    if (config_virtdomains && domain) {
d0d307a
+         if(snprintf(sieve_script_dir, buflen, "%s%s%c/%s/%c/%s/",
Tomas Janousek 14dc340
+              sieve_dir, FNAME_DOMAINDIR, dir_hash_c(domain, config_fulldirhash), domain, dir_hash_c(user,config_fulldirhash), user) >= buflen) {
d0d307a
+                 free(user);
d0d307a
+                 return 1;
d0d307a
+ 	 }
d0d307a
+    } else {
d0d307a
+         if(snprintf(sieve_script_dir, buflen, "%s/%c/%s/", 
Tomas Janousek 14dc340
+     	      sieve_dir, dir_hash_c(user,config_fulldirhash), user) >= buflen) 
d0d307a
+                 return 1;
d0d307a
+    }
d0d307a
+
d0d307a
+    /* Free the xmalloced user memory, reserved above */
d0d307a
+    if(user != userid)
d0d307a
+        free(user);
d0d307a
+
d0d307a
+    return 0;
d0d307a
+}
d0d307a
+
d0d307a
+int autoadd_sieve(char *userid, const char *source_script)
d0d307a
+{   
d0d307a
+    sieve_script_t *s = NULL;
d0d307a
+    bytecode_info_t *bc = NULL;
d0d307a
+    char *err = NULL;
d0d307a
+    FILE *in_stream, *out_fp;
d0d307a
+    int out_fd, in_fd, r, k;
d0d307a
+    int do_compile = 0;
d0d307a
+    const char *sieve_dir = NULL;
d0d307a
+    const char *compiled_source_script = NULL;
d0d307a
+    char sievename[MAX_FILENAME];
d0d307a
+    char sieve_script_name[MAX_FILENAME];
d0d307a
+    char sieve_script_dir[MAX_FILENAME];
d0d307a
+    char sieve_bcscript_name[MAX_FILENAME];
d0d307a
+    char sieve_default[MAX_FILENAME];
d0d307a
+    char sieve_tmpname[MAX_FILENAME];
d0d307a
+    char sieve_bctmpname[MAX_FILENAME];
d0d307a
+    char sieve_bclink_name[MAX_FILENAME];
d0d307a
+    char buf[4096];
d0d307a
+    mode_t oldmask;
d0d307a
+    struct stat statbuf;
d0d307a
+
d0d307a
+    /* We don't support using the homedirectory, like timsieved */
d0d307a
+    if (config_getswitch(IMAPOPT_SIEVEUSEHOMEDIR)) {
d0d307a
+        syslog(LOG_WARNING,"autocreate_sieve: autocreate_sieve does not work with sieveusehomedir option in imapd.conf");
d0d307a
+        return 1;
d0d307a
+    }
d0d307a
+
d0d307a
+    /* Check if sievedir is defined in imapd.conf */
d0d307a
+    if(!(sieve_dir = config_getstring(IMAPOPT_SIEVEDIR))) { 
d0d307a
+        syslog(LOG_WARNING, "autocreate_sieve: sievedir option is not defined. Check imapd.conf");
d0d307a
+        return 1;
d0d307a
+    }
d0d307a
+
d0d307a
+    /* Check if autocreate_sieve_compiledscript is defined in imapd.conf */
d0d307a
+    if(!(compiled_source_script  = config_getstring(IMAPOPT_AUTOCREATE_SIEVE_COMPILEDSCRIPT))) {
d0d307a
+        syslog(LOG_WARNING, "autocreate_sieve: autocreate_sieve_compiledscript option is not defined. Compiling it");
d0d307a
+        do_compile = 1;
d0d307a
+    }
d0d307a
+
d0d307a
+    if(get_script_dir(sieve_script_dir, sizeof(sieve_script_dir), userid, sieve_dir)) {
d0d307a
+        syslog(LOG_WARNING, "autocreate_sieve: Cannot find sieve scripts directory");
d0d307a
+        return 1;
d0d307a
+    }
d0d307a
+
d0d307a
+    if (get_script_name(sievename, sizeof(sievename), source_script)) {
d0d307a
+        syslog(LOG_WARNING, "autocreate_sieve: Invalid sieve script %s", source_script);
d0d307a
+        return 1;
d0d307a
+    }
d0d307a
+
d0d307a
+    if(snprintf(sieve_tmpname, sizeof(sieve_tmpname), "%s%s.script.NEW",sieve_script_dir, sievename) >= sizeof(sieve_tmpname)) {
d0d307a
+        syslog(LOG_WARNING, "autocreate_sieve: Invalid sieve path %s, %s, %s", sieve_dir, sievename, userid);
d0d307a
+        return 1;
d0d307a
+    }
d0d307a
+    if(snprintf(sieve_bctmpname, sizeof(sieve_bctmpname), "%s%s.bc.NEW",sieve_script_dir, sievename) >= sizeof(sieve_bctmpname)) {
d0d307a
+        syslog(LOG_WARNING, "autocreate_sieve: Invalid sieve path %s, %s, %s", sieve_dir, sievename, userid);
d0d307a
+        return 1;
d0d307a
+    }    
d0d307a
+    if(snprintf(sieve_script_name, sizeof(sieve_script_name), "%s%s.script",sieve_script_dir, sievename) >= sizeof(sieve_script_name)) {
d0d307a
+        syslog(LOG_WARNING, "autocreate_sieve: Invalid sieve path %s, %s, %s", sieve_dir, sievename, userid);
d0d307a
+        return 1;
d0d307a
+    }
d0d307a
+    if(snprintf(sieve_bcscript_name, sizeof(sieve_bcscript_name), "%s%s.bc",sieve_script_dir, sievename) >= sizeof(sieve_bcscript_name)) {
d0d307a
+        syslog(LOG_WARNING, "autocreate_sieve: Invalid sieve path %s, %s, %s", sieve_dir, sievename, userid);
d0d307a
+        return 1;
d0d307a
+    }
d0d307a
+    if(snprintf(sieve_default, sizeof(sieve_default), "%s%s",sieve_script_dir,"defaultbc") >= sizeof(sieve_default)) {
d0d307a
+        syslog(LOG_WARNING, "autocreate_sieve: Invalid sieve path %s, %s, %s", sieve_dir, sievename, userid);
d0d307a
+        return 1;
d0d307a
+    }
d0d307a
+    if(snprintf(sieve_bclink_name, sizeof(sieve_bclink_name), "%s.bc", sievename) >= sizeof(sieve_bclink_name))  {
d0d307a
+        syslog(LOG_WARNING, "autocreate_sieve: Invalid sieve path %s, %s, %s", sieve_dir, sievename, userid);
d0d307a
+        return 1;
d0d307a
+    }
d0d307a
+
d0d307a
+    /* Check if a default sieve filter alrady exists */
d0d307a
+    if(!stat(sieve_default,&statbuf)) {
d0d307a
+        syslog(LOG_WARNING,"autocreate_sieve: Default sieve script already exists");
d0d307a
+        fclose(in_stream);
d0d307a
+        return 1;
d0d307a
+    }
d0d307a
+
d0d307a
+    /* Open the source script. if there is a problem with that exit */
d0d307a
+    in_stream = fopen(source_script, "r");
d0d307a
+    if(!in_stream) {
d0d307a
+        syslog(LOG_WARNING,"autocreate_sieve: Unable to open sieve script %s. Check permissions",source_script);
d0d307a
+        return 1;
d0d307a
+    }
d0d307a
+    
d0d307a
+    
d0d307a
+    /* 
d0d307a
+     * At this point we start the modifications of the filesystem 
d0d307a
+     */
d0d307a
+
d0d307a
+    /* Create the directory where the sieve scripts will reside */
d0d307a
+    r = cyrus_mkdir(sieve_script_dir, 0755);
d0d307a
+    if(r == -1) {
d0d307a
+        /* If this fails we just leave */
d0d307a
+        syslog(LOG_WARNING,"autocreate_sieve: Unable to create directory %s. Check permissions",sieve_script_name);
d0d307a
+        return 1;
d0d307a
+    }
d0d307a
+
d0d307a
+    /*
d0d307a
+     * We open the file that will be used as the bc file. If this file exists, overwrite it 
d0d307a
+     * since something bad has happened. We open the file here so that this error checking is
d0d307a
+     * done before we try to open the rest of the files to start copying etc. 
d0d307a
+     */
d0d307a
+    out_fd = open(sieve_bctmpname, O_CREAT|O_TRUNC|O_WRONLY, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
d0d307a
+    if(out_fd < 0) {
d0d307a
+        if(errno == EEXIST) {
d0d307a
+            syslog(LOG_WARNING,"autocreate_sieve: File %s already exists. Probaly left over. Ignoring",sieve_bctmpname);
d0d307a
+        } else if (errno == EACCES) {
d0d307a
+            syslog(LOG_WARNING,"autocreate_sieve: No access to create file %s. Check permissions",sieve_bctmpname);
d0d307a
+            fclose(in_stream);
d0d307a
+            return 1;
d0d307a
+        } else {
d0d307a
+            syslog(LOG_WARNING,"autocreate_sieve: Unable to create %s. Unknown error",sieve_bctmpname);
d0d307a
+            fclose(in_stream);
d0d307a
+            return 1;
d0d307a
+        }
d0d307a
+    }
d0d307a
+
d0d307a
+    if(!do_compile && compiled_source_script && (in_fd = open(compiled_source_script, O_RDONLY)) != -1) {
d0d307a
+        while((r = read(in_fd, buf, sizeof(buf))) > 0) {
d0d307a
+            if((k=write(out_fd, buf,r)) < 0) {
d0d307a
+                syslog(LOG_WARNING, "autocreate_sieve: Error writing to file: %s, error: %d", sieve_bctmpname, errno);
d0d307a
+                close(out_fd);
d0d307a
+                close(in_fd);
d0d307a
+                fclose(in_stream);
d0d307a
+                unlink(sieve_bctmpname);
d0d307a
+                return 1;
d0d307a
+           }
d0d307a
+        } 
d0d307a
+
d0d307a
+        if(r == 0) { /* EOF */
d0d307a
+            close(out_fd);
d0d307a
+            close(in_fd);
d0d307a
+        } else if (r < 0) {
d0d307a
+            syslog(LOG_WARNING, "autocreate_sieve: Error reading compiled script file: %s. Will try to compile it", 
d0d307a
+                           compiled_source_script);
d0d307a
+            close(in_fd);
d0d307a
+            do_compile = 1;
d0d307a
+            if(lseek(out_fd, 0, SEEK_SET)) {
d0d307a
+                syslog(LOG_WARNING, "autocreate_sieve: Major IO problem. Aborting");
d0d307a
+                return 1;
d0d307a
+            }
d0d307a
+        }
d0d307a
+        close(in_fd);
d0d307a
+    } else {
d0d307a
+        if(compiled_source_script)
d0d307a
+              syslog(LOG_WARNING,"autocreate_sieve: Problem opening compiled script file: %s. Compiling it", compiled_source_script);
d0d307a
+        do_compile = 1;
d0d307a
+    }
d0d307a
+
d0d307a
+
d0d307a
+    /* Because we failed to open a precompiled bc sieve script, we compile one */
d0d307a
+    if(do_compile) {
d0d307a
+       if(is_script_parsable(in_stream,&err, &s) == TIMSIEVE_FAIL) {
d0d307a
+            if(err && *err) {
d0d307a
+               syslog(LOG_WARNING,"autocreate_sieve: Error while parsing script %s.",err);
d0d307a
+               free(err);
d0d307a
+            } else
d0d307a
+                syslog(LOG_WARNING,"autocreate_sieve: Error while parsing script");
d0d307a
+    
d0d307a
+            unlink(sieve_bctmpname);
d0d307a
+            fclose(in_stream);
d0d307a
+            close(out_fd);
d0d307a
+            return 1;
d0d307a
+        }
d0d307a
+
d0d307a
+        /* generate the bytecode */
d0d307a
+        if(sieve_generate_bytecode(&bc, s) == TIMSIEVE_FAIL) {
d0d307a
+            syslog(LOG_WARNING,"autocreate_sieve: problem compiling sieve script");
d0d307a
+            /* removing the copied script and cleaning up memory */
d0d307a
+            unlink(sieve_bctmpname);
d0d307a
+            sieve_script_free(&s);
d0d307a
+            fclose(in_stream);
d0d307a
+            close(out_fd);
d0d307a
+            return 1;
d0d307a
+        }
d0d307a
+
d0d307a
+        if(sieve_emit_bytecode(out_fd, bc) == TIMSIEVE_FAIL) {
d0d307a
+            syslog(LOG_WARNING,"autocreate_sieve: problem emiting sieve script");
d0d307a
+            /* removing the copied script and cleaning up memory */
d0d307a
+            unlink(sieve_bctmpname);
d0d307a
+            sieve_free_bytecode(&bc);
d0d307a
+            sieve_script_free(&s);
d0d307a
+            fclose(in_stream);
d0d307a
+            close(out_fd);
d0d307a
+            return 1;
d0d307a
+        }
d0d307a
+
d0d307a
+        /* clean up the memory */
d0d307a
+        sieve_free_bytecode(&bc);
d0d307a
+        sieve_script_free(&s);
d0d307a
+    }
d0d307a
+
d0d307a
+    close(out_fd);
d0d307a
+    rewind(in_stream);
d0d307a
+
d0d307a
+    /* Copy the initial script */
d0d307a
+    oldmask = umask(077);
d0d307a
+    if((out_fp = fopen(sieve_tmpname, "w")) == NULL) {
d0d307a
+        syslog(LOG_WARNING,"autocreate_sieve: Unable to open %s destination sieve script", sieve_tmpname);
d0d307a
+        unlink(sieve_bctmpname);
d0d307a
+        umask(oldmask);
d0d307a
+        fclose(in_stream);
d0d307a
+        return 1;
d0d307a
+    }
d0d307a
+    umask(oldmask);
d0d307a
+
d0d307a
+    while((r = fread(buf,sizeof(char), sizeof(buf), in_stream))) {
d0d307a
+        if( fwrite(buf,sizeof(char), r, out_fp) != r) {
d0d307a
+            syslog(LOG_WARNING,"autocreate_sieve: Problem writing to sieve script file: %s",sieve_tmpname);
d0d307a
+            fclose(out_fp);
d0d307a
+            unlink(sieve_tmpname);
d0d307a
+            unlink(sieve_bctmpname);
d0d307a
+            fclose(in_stream);
d0d307a
+            return 1;
d0d307a
+        }
d0d307a
+    }
d0d307a
+    
d0d307a
+    if(feof(in_stream)) {
d0d307a
+        fclose(out_fp);
d0d307a
+    } else { /* ferror */
d0d307a
+        fclose(out_fp);
d0d307a
+        unlink(sieve_tmpname);
d0d307a
+        unlink(sieve_bctmpname);
d0d307a
+        fclose(in_stream);
d0d307a
+        return 1;
d0d307a
+    }
d0d307a
+
d0d307a
+    /* Renaming the necessary stuff */
d0d307a
+    if(rename(sieve_tmpname, sieve_script_name)) {
d0d307a
+        unlink(sieve_tmpname);
d0d307a
+        unlink(sieve_bctmpname);
d0d307a
+        return 1;
d0d307a
+    }
d0d307a
+
d0d307a
+    if(rename(sieve_bctmpname, sieve_bcscript_name)) {
d0d307a
+        unlink(sieve_bctmpname);
d0d307a
+        unlink(sieve_bcscript_name);
d0d307a
+        return 1;
d0d307a
+    }
d0d307a
+
d0d307a
+    /* end now with the symlink */
d0d307a
+    if(symlink(sieve_bclink_name, sieve_default)) {
d0d307a
+        if(errno != EEXIST) {
d0d307a
+            syslog(LOG_WARNING, "autocreate_sieve: problem making the default link.");
d0d307a
+            /* Lets delete the files */
d0d307a
+            unlink(sieve_script_name);
d0d307a
+            unlink(sieve_bcscript_name);
d0d307a
+        }
d0d307a
+    }
d0d307a
+
d0d307a
+    /* 
d0d307a
+     * If everything has succeeded AND we have compiled the script AND we have requested
d0d307a
+     * to generate the global script so that it is not compiled each time then we create it.
d0d307a
+     */
d0d307a
+    if(do_compile && 
d0d307a
+          config_getswitch(IMAPOPT_GENERATE_COMPILED_SIEVE_SCRIPT)) {
d0d307a
+
d0d307a
+        if(!compiled_source_script) {
d0d307a
+            syslog(LOG_WARNING, "autocreate_sieve: To save a compiled sieve script, autocreate_sieve_compiledscript must have been defined in imapd.conf");
d0d307a
+            return 0;
d0d307a
+        }
d0d307a
+
d0d307a
+        if(snprintf(sieve_tmpname, sizeof(sieve_tmpname), "%s.NEW", compiled_source_script) >= sizeof(sieve_tmpname))
d0d307a
+            return 0;
d0d307a
+
d0d307a
+        /*
d0d307a
+         * Copy everything from the newly created bc sieve sieve script.
d0d307a
+         */
d0d307a
+        if((in_fd = open(sieve_bcscript_name, O_RDONLY))<0) {
d0d307a
+            return 0;
d0d307a
+        }
d0d307a
+
d0d307a
+        if((out_fd = open(sieve_tmpname, O_CREAT|O_EXCL|O_WRONLY, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)) < 0) {
d0d307a
+            if(errno == EEXIST) {
d0d307a
+               /* Someone is already doing this so just bail out. */
d0d307a
+               syslog(LOG_WARNING, "autocreate_sieve: %s already exists. Some other instance processing it, or it is left over", sieve_tmpname);
d0d307a
+                close(in_fd);
d0d307a
+                return 0; 
d0d307a
+            } else if (errno == EACCES) {
d0d307a
+                syslog(LOG_WARNING,"autocreate_sieve: No access to create file %s. Check permissions",sieve_tmpname);
d0d307a
+                close(in_fd);
d0d307a
+                return 0;
d0d307a
+            } else {
d0d307a
+                syslog(LOG_WARNING,"autocreate_sieve: Unable to create %s",sieve_tmpname);
d0d307a
+                close(in_fd);
d0d307a
+                return 0;
d0d307a
+            }
d0d307a
+        }
d0d307a
+
d0d307a
+        while((r = read(in_fd, buf, sizeof(buf))) > 0) {
d0d307a
+            if((k = write(out_fd,buf,r)) < 0) {
d0d307a
+                syslog(LOG_WARNING, "autocreate_sieve: Error writing to file: %s, error: %d", sieve_tmpname, errno);
d0d307a
+                close(out_fd);
d0d307a
+                close(in_fd);
d0d307a
+                unlink(sieve_tmpname);
d0d307a
+                return 0;
d0d307a
+           }
d0d307a
+        }
d0d307a
+
d0d307a
+        if(r == 0 ) { /*EOF */
d0d307a
+            close(out_fd);
d0d307a
+            close(in_fd);
d0d307a
+        } else if (r < 0) {
d0d307a
+                syslog(LOG_WARNING, "autocreate_sieve: Error writing to file: %s, error: %d", sieve_tmpname, errno);
d0d307a
+                close(out_fd);
d0d307a
+                close(in_fd);
d0d307a
+                unlink(sieve_tmpname);
d0d307a
+                return 0;
d0d307a
+        }
d0d307a
+
d0d307a
+        /* Rename the temporary created sieve script to its final name. */
d0d307a
+        if(rename(sieve_tmpname, compiled_source_script)) {
d0d307a
+            if(errno != EEXIST) {
d0d307a
+               unlink(sieve_tmpname);
d0d307a
+               unlink(compiled_source_script);
d0d307a
+        }
d0d307a
+            return 0;
d0d307a
+        }
d0d307a
+
d0d307a
+        syslog(LOG_NOTICE, "autocreate_sieve: Compiled sieve script was successfully saved in %s", compiled_source_script);
d0d307a
+    }
d0d307a
+
d0d307a
+    return 0;
d0d307a
+}
d0d307a
+
Tomas Janousek 6861497
+/*static void fatal(const char *s, int code)
d0d307a
+{   
d0d307a
+    printf("Fatal error: %s (%d)\r\n", s, code);
d0d307a
+    exit(1);
Tomas Janousek 6861497
+}*/
d0d307a
+
d0d307a
+/* to make larry's stupid functions happy :) */
d0d307a
+static void foo(void)
d0d307a
+{
d0d307a
+    fatal("stub function called", 0);
d0d307a
+}
d0d307a
+
d0d307a
+static int sieve_notify(void *ac __attribute__((unused)),
d0d307a
+                        void *interp_context __attribute__((unused)),
d0d307a
+                        void *script_context __attribute__((unused)),
d0d307a
+                        void *message_context __attribute__((unused)),
d0d307a
+                        const char **errmsg __attribute__((unused)))
d0d307a
+{
d0d307a
+    fatal("stub function called", 0);
d0d307a
+    return SIEVE_FAIL;
d0d307a
+}
d0d307a
+
d0d307a
+static int mysieve_error(int lineno, const char *msg,
d0d307a
+                  void *i __attribute__((unused)), void *s)
d0d307a
+{
d0d307a
+    char buf[1024];
d0d307a
+    char **errstr = (char **) s;
d0d307a
+
d0d307a
+    snprintf(buf, 80, "line %d: %s\r\n", lineno, msg);
d0d307a
+    *errstr = (char *) xrealloc(*errstr, strlen(*errstr) + strlen(buf) + 30);
d0d307a
+    syslog(LOG_DEBUG, "%s", buf);
d0d307a
+    strcat(*errstr, buf);
d0d307a
+
d0d307a
+    return SIEVE_OK;
d0d307a
+}
d0d307a
+
d0d307a
+/* end the boilerplate */
d0d307a
+
d0d307a
+/* returns TRUE or FALSE */
d0d307a
+int is_script_parsable(FILE *stream, char **errstr, sieve_script_t **ret)
d0d307a
+{
d0d307a
+    sieve_interp_t *i;
d0d307a
+    sieve_script_t *s;
d0d307a
+    int res;
d0d307a
+
d0d307a
+    res = sieve_interp_alloc(&i, NULL);
d0d307a
+    if (res != SIEVE_OK) {
d0d307a
+        syslog(LOG_WARNING, "sieve_interp_alloc() returns %d\n", res);
d0d307a
+        return TIMSIEVE_FAIL;
d0d307a
+    }
d0d307a
+
d0d307a
+    res = sieve_register_redirect(i, (sieve_callback *) &foo;;
d0d307a
+    if (res != SIEVE_OK) {
d0d307a
+        syslog(LOG_WARNING, "sieve_register_redirect() returns %d\n", res);
d0d307a
+        return TIMSIEVE_FAIL;
d0d307a
+    }
d0d307a
+    res = sieve_register_discard(i, (sieve_callback *) &foo;;
d0d307a
+    if (res != SIEVE_OK) {
d0d307a
+        syslog(LOG_WARNING, "sieve_register_discard() returns %d\n", res);
d0d307a
+        return TIMSIEVE_FAIL;
d0d307a
+    }
d0d307a
+    res = sieve_register_reject(i, (sieve_callback *) &foo;;
d0d307a
+    if (res != SIEVE_OK) {
d0d307a
+        syslog(LOG_WARNING, "sieve_register_reject() returns %d\n", res);
d0d307a
+        return TIMSIEVE_FAIL;
d0d307a
+    }
d0d307a
+    res = sieve_register_fileinto(i, (sieve_callback *) &foo;;
d0d307a
+    if (res != SIEVE_OK) {
d0d307a
+        syslog(LOG_WARNING, "sieve_register_fileinto() returns %d\n", res);
d0d307a
+        return TIMSIEVE_FAIL;
d0d307a
+    }
d0d307a
+    res = sieve_register_keep(i, (sieve_callback *) &foo;;
d0d307a
+    if (res != SIEVE_OK) {
d0d307a
+        syslog(LOG_WARNING, "sieve_register_keep() returns %d\n", res);
d0d307a
+        return TIMSIEVE_FAIL;
d0d307a
+    }
d0d307a
+
d0d307a
+    res = sieve_register_imapflags(i, NULL);
d0d307a
+    if (res != SIEVE_OK) {
d0d307a
+        syslog(LOG_WARNING, "sieve_register_imapflags() returns %d\n", res);
d0d307a
+        return TIMSIEVE_FAIL;
d0d307a
+    }
d0d307a
+
d0d307a
+    res = sieve_register_size(i, (sieve_get_size *) &foo;;
d0d307a
+    if (res != SIEVE_OK) {
d0d307a
+        syslog(LOG_WARNING, "sieve_register_size() returns %d\n", res);
d0d307a
+        return TIMSIEVE_FAIL;
d0d307a
+    }
d0d307a
+
d0d307a
+    res = sieve_register_header(i, (sieve_get_header *) &foo;;
d0d307a
+    if (res != SIEVE_OK) {
d0d307a
+        syslog(LOG_WARNING, "sieve_register_header() returns %d\n", res);
d0d307a
+        return TIMSIEVE_FAIL;
d0d307a
+    }
d0d307a
+
d0d307a
+    res = sieve_register_envelope(i, (sieve_get_envelope *) &foo;;
d0d307a
+    if (res != SIEVE_OK) {
d0d307a
+        syslog(LOG_WARNING, "sieve_register_envelope() returns %d\n", res);
d0d307a
+        return TIMSIEVE_FAIL;
d0d307a
+    }
d0d307a
+
d0d307a
+    res = sieve_register_vacation(i, &vacation2);
d0d307a
+    if (res != SIEVE_OK) {
d0d307a
+        syslog(LOG_WARNING, "sieve_register_vacation() returns %d\n", res);
d0d307a
+        return TIMSIEVE_FAIL;
d0d307a
+    }
d0d307a
+
d0d307a
+    res = sieve_register_notify(i, &sieve_notify);
d0d307a
+    if (res != SIEVE_OK) {
d0d307a
+        syslog(LOG_WARNING, "sieve_register_notify() returns %d\n", res);
d0d307a
+        return TIMSIEVE_FAIL;
d0d307a
+    }
d0d307a
+
d0d307a
+    res = sieve_register_parse_error(i, &mysieve_error);
d0d307a
+    if (res != SIEVE_OK) {
d0d307a
+        syslog(LOG_WARNING, "sieve_register_parse_error() returns %d\n", res);
d0d307a
+        return TIMSIEVE_FAIL;
d0d307a
+    }
d0d307a
+
d0d307a
+    rewind(stream);
d0d307a
+
d0d307a
+    *errstr = (char *) xmalloc(20 * sizeof(char));
d0d307a
+    strcpy(*errstr, "script errors:\r\n");
d0d307a
+
d0d307a
+    res = sieve_script_parse(i, stream, errstr, &s);
d0d307a
+
d0d307a
+    if (res == SIEVE_OK) {
d0d307a
+        if(ret) {
d0d307a
+            *ret = s;
d0d307a
+        } else {
d0d307a
+            sieve_script_free(&s);
d0d307a
+        }
d0d307a
+        free(*errstr);
d0d307a
+        *errstr = NULL;
d0d307a
+    }
d0d307a
+
d0d307a
+    /* free interpreter */
d0d307a
+    sieve_interp_free(&i);
d0d307a
+
d0d307a
+    return (res == SIEVE_OK) ? TIMSIEVE_OK : TIMSIEVE_FAIL;
d0d307a
+}
d0d307a
+
d0d307a
+/*
d0d307a
+ * Btw the initial date of this patch is Sep, 02 2004 which is the birthday of
d0d307a
+ * Pavlos. Author of cyrusmaster. So consider this patch as his birthday present
d0d307a
+ */
d0d307a
+
346e223
diff -Naur cyrus-imapd-2.3.12p2/imap/compile_sieve.c cyrus-imapd-2.3.12p2-autocreate.uncompiled/imap/compile_sieve.c
346e223
--- cyrus-imapd-2.3.12p2/imap/compile_sieve.c	1970-01-01 02:00:00.000000000 +0200
346e223
+++ cyrus-imapd-2.3.12p2-autocreate.uncompiled/imap/compile_sieve.c	2008-05-06 14:48:41.000000000 +0300
Tomas Janousek 6861497
@@ -0,0 +1,365 @@
d0d307a
+/* This tool compiles the sieve script from a command
d0d307a
+line so that it can be used wby the autoadd patch */
d0d307a
+#include <stdio.h>
d0d307a
+#include <stdlib.h>
d0d307a
+
d0d307a
+#include <config.h>
d0d307a
+#include <string.h>
d0d307a
+#ifdef HAVE_UNISTD_H
d0d307a
+#include <unistd.h>
d0d307a
+#endif
d0d307a
+#include <errno.h>
d0d307a
+#include <sys/types.h>
d0d307a
+#include <sys/stat.h>
d0d307a
+#include <sys/uio.h>
d0d307a
+#include <fcntl.h>
d0d307a
+#include <ctype.h>
d0d307a
+#include <time.h>
d0d307a
+#include <com_err.h>
d0d307a
+
d0d307a
+#include "global.h"
d0d307a
+
d0d307a
+#include "util.h"
Tomas Janousek 6861497
+#include "xmalloc.h"
Tomas Janousek 6861497
+#include "xstrlcpy.h"
Tomas Janousek 6861497
+#include "xstrlcat.h"
d0d307a
+#include "mailbox.h"
d0d307a
+#include "imap_err.h"
d0d307a
+#include "sieve_interface.h"
d0d307a
+#include "script.h"
d0d307a
+
d0d307a
+#include <pwd.h>
d0d307a
+
d0d307a
+#define TIMSIEVE_FAIL 		-1
d0d307a
+#define TIMSIEVE_OK 		0
d0d307a
+#define MAX_FILENAME_SIZE	100
d0d307a
+
d0d307a
+/* Needed by libconfig */
d0d307a
+const int config_need_data = 0;
d0d307a
+
d0d307a
+static int is_script_parsable(FILE *stream, char **errstr, sieve_script_t **ret);
d0d307a
+
Tomas Janousek 6861497
+/*static void fatal(const char *s, int code)
d0d307a
+{   
d0d307a
+    printf("Fatal error: %s (%d)\r\n", s, code);
d0d307a
+
d0d307a
+    exit(1);
Tomas Janousek 6861497
+}*/
d0d307a
+
d0d307a
+void usage(void)
d0d307a
+{
d0d307a
+    fprintf(stderr,
d0d307a
+            "Usage:\n\tcompile_sieve [-C <altconfig>] [-i <infile> -o <outfile>]\n");
d0d307a
+    exit(-1);
d0d307a
+}
d0d307a
+
d0d307a
+
d0d307a
+int main (int argc, char **argv)
d0d307a
+{   
d0d307a
+
d0d307a
+    sieve_script_t *s = NULL;
d0d307a
+    bytecode_info_t *bc = NULL;
d0d307a
+    char *err = NULL;
d0d307a
+    FILE *in_stream;
Tomas Janousek 6861497
+    int  out_fd, opt;
d0d307a
+    char *source_script = NULL;
d0d307a
+    char *compiled_source_script = NULL;
d0d307a
+    char *alt_config = NULL;
d0d307a
+    extern char *optarg;
d0d307a
+    char sieve_tmpname[MAX_MAILBOX_NAME+1];
d0d307a
+
d0d307a
+    if (geteuid() == 0) fatal("must run as the Cyrus user", EC_USAGE);
d0d307a
+
d0d307a
+    while((opt = getopt(argc, argv, "C:i:o:")) != EOF) {
d0d307a
+        switch (opt) {
d0d307a
+            case 'C': /* alt config file */
d0d307a
+	        alt_config =  optarg;
d0d307a
+	        break;
d0d307a
+	    case 'i': /* input script file */
d0d307a
+		source_script = optarg;
d0d307a
+		break;
d0d307a
+	    case 'o': /* output script file */
d0d307a
+		compiled_source_script = optarg;
d0d307a
+		break;
d0d307a
+	    default:
d0d307a
+	        usage();
d0d307a
+		break;
d0d307a
+	}
d0d307a
+    }
d0d307a
+
d0d307a
+    if(source_script && !compiled_source_script) {
d0d307a
+	    fprintf(stderr, "No output file was defined\n");
d0d307a
+	    usage();
d0d307a
+    } else if (!source_script && compiled_source_script) {
d0d307a
+	    fprintf(stderr, "No input file was defined\n");
d0d307a
+	    usage();
d0d307a
+    }	
d0d307a
+
d0d307a
+    /*
d0d307a
+     * If no <infile> has been defined, then read them from
d0d307a
+     * the configuration file.
d0d307a
+     */
d0d307a
+    if (!source_script && !compiled_source_script) { 
d0d307a
+	    cyrus_init(alt_config, "compile_sieve", 0);
d0d307a
+
d0d307a
+	    /* Initially check if we want to have the sieve script created */
d0d307a
+	    if(!(source_script = (char *) config_getstring(IMAPOPT_AUTOCREATE_SIEVE_SCRIPT))) {
d0d307a
+        	fprintf(stderr,"autocreate_sieve_script option not defined. Check imapd.conf\n");
d0d307a
+	        return 1;
d0d307a
+	    }
d0d307a
+
d0d307a
+	    /* Check if we have an already compiled sieve script*/
d0d307a
+	    if(!(compiled_source_script = (char *) config_getstring(IMAPOPT_AUTOCREATE_SIEVE_COMPILEDSCRIPT))) {
d0d307a
+	        fprintf(stderr, "autocreate_sieve_compiledscript option not defined. Check imapd.conf\n");
d0d307a
+		return 1;
d0d307a
+	    }
d0d307a
+
d0d307a
+	    if(!strrchr(source_script,'/') || !strrchr(compiled_source_script,'/')) {
d0d307a
+       		/* 
d0d307a
+		 * At this point the only think that is inconsistent is the directory 
d0d307a
+		 * that was created. But if the user will have any sieve scripts then 
d0d307a
+		 * they will eventually go there, so no big deal 
d0d307a
+		 */
d0d307a
+	        fprintf(stderr, 
d0d307a
+			"In imapd.conf the full path of the filenames must be defined\n");
d0d307a
+	       	return 1;
d0d307a
+	    }
d0d307a
+    }
d0d307a
+
d0d307a
+    printf("input file : %s, output file : %s\n", source_script, compiled_source_script);
d0d307a
+
d0d307a
+
d0d307a
+    if(strlen(compiled_source_script) + sizeof(".NEW") + 1 > sizeof(sieve_tmpname)) {
d0d307a
+	    fprintf(stderr, "Filename %s is too big\n", compiled_source_script);
d0d307a
+	    return 1;
d0d307a
+    }
d0d307a
+    	
d0d307a
+    snprintf(sieve_tmpname, sizeof(sieve_tmpname), "%s.NEW", compiled_source_script);
d0d307a
+
d0d307a
+    in_stream = fopen(source_script,"r");
d0d307a
+
d0d307a
+    if(!in_stream) {
d0d307a
+        fprintf(stderr,"Unable to open %s source sieve script\n",source_script);
Tomas Janousek 6861497
+        return 1; 
d0d307a
+    }
d0d307a
+
d0d307a
+    /* 
d0d307a
+     * We open the file that will be used as the bc file. If this file exists, overwrite it 
d0d307a
+     * since something bad has happened. We open the file here so that this error checking is
d0d307a
+     * done before we try to open the rest of the files to start copying etc. 
d0d307a
+     */
d0d307a
+    out_fd = open(sieve_tmpname, O_CREAT|O_EXCL|O_WRONLY, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
d0d307a
+    if(out_fd < 0) {
d0d307a
+        if(errno == EEXIST) {
d0d307a
+            fprintf(stderr, "File %s already exists\n", sieve_tmpname);
d0d307a
+        } else if (errno == EACCES) {
d0d307a
+            fprintf(stderr,"No access to create file %s. Please check that you have the correct permissions\n",
d0d307a
+			    sieve_tmpname);
d0d307a
+        } else {
d0d307a
+            fprintf(stderr,"Unable to create %s. Please check that you have the correct permissions\n", 
d0d307a
+			    sieve_tmpname);
d0d307a
+        }
d0d307a
+	
d0d307a
+	fclose(in_stream);
d0d307a
+	return 1;
d0d307a
+    }
d0d307a
+
d0d307a
+    if(is_script_parsable(in_stream,&err, &s) == TIMSIEVE_FAIL) {
d0d307a
+        if(err && *err) {
d0d307a
+           fprintf(stderr, "Error while parsing script %s\n",err);
d0d307a
+           free(err);
d0d307a
+        }
d0d307a
+        else
d0d307a
+            fprintf(stderr,"Error while parsing script\n");
d0d307a
+            unlink(sieve_tmpname);
d0d307a
+	    fclose(in_stream);
d0d307a
+	    close(out_fd);
Tomas Janousek 6861497
+        return 1;
d0d307a
+   }
d0d307a
+
d0d307a
+
d0d307a
+    /* generate the bytecode */
d0d307a
+    if(sieve_generate_bytecode(&bc,s) == TIMSIEVE_FAIL) {
d0d307a
+        fprintf(stderr,"Error occured while compiling sieve script\n");
d0d307a
+        /* removing the copied script and cleaning up memory */
d0d307a
+        unlink(sieve_tmpname);
d0d307a
+        sieve_script_free(&s);
d0d307a
+        fclose(in_stream);
d0d307a
+        close(out_fd);
Tomas Janousek 6861497
+        return 1;
d0d307a
+    }
d0d307a
+    if(sieve_emit_bytecode(out_fd,bc) == TIMSIEVE_FAIL) {
d0d307a
+        fprintf(stderr, "Error occured while emitting sieve script\n");
d0d307a
+        unlink(sieve_tmpname);
d0d307a
+        sieve_free_bytecode(&bc);
d0d307a
+        sieve_script_free(&s);
d0d307a
+        fclose(in_stream);
d0d307a
+        close(out_fd);
Tomas Janousek 6861497
+        return 1;
d0d307a
+    }
d0d307a
+
d0d307a
+    /* clean up the memory */
d0d307a
+    sieve_free_bytecode(&bc);
d0d307a
+    sieve_script_free(&s);
d0d307a
+
d0d307a
+    close(out_fd);
d0d307a
+
d0d307a
+    if(rename(sieve_tmpname, compiled_source_script)) {
d0d307a
+        if(errno != EEXIST) {
d0d307a
+            unlink(sieve_tmpname);
d0d307a
+            unlink(compiled_source_script);
d0d307a
+            return 1;
d0d307a
+        }
d0d307a
+    }
d0d307a
+    return 0;
d0d307a
+}
d0d307a
+
d0d307a
+
d0d307a
+/* to make larry's stupid functions happy :) */
d0d307a
+static void foo(void)
d0d307a
+{
d0d307a
+    fatal("stub function called", 0);
d0d307a
+}
d0d307a
+
d0d307a
+extern sieve_vacation_t vacation2;/* = {
d0d307a
+    0,                          / min response /
d0d307a
+    0,                          / max response /
d0d307a
+    (sieve_callback *) &foo,    / autorespond() /
d0d307a
+    (sieve_callback *) &foo     / send_response() /
d0d307a
+}; */
d0d307a
+
d0d307a
+static int sieve_notify(void *ac __attribute__((unused)),
d0d307a
+                        void *interp_context __attribute__((unused)),
d0d307a
+                        void *script_context __attribute__((unused)),
d0d307a
+                        void *message_context __attribute__((unused)),
d0d307a
+                        const char **errmsg __attribute__((unused)))
d0d307a
+{
d0d307a
+    fatal("stub function called", 0);
d0d307a
+    return SIEVE_FAIL;
d0d307a
+}
d0d307a
+
d0d307a
+static int mysieve_error(int lineno, const char *msg,
d0d307a
+                  void *i __attribute__((unused)), void *s)
d0d307a
+{
d0d307a
+    char buf[1024];
d0d307a
+    char **errstr = (char **) s;
d0d307a
+
d0d307a
+    snprintf(buf, 80, "line %d: %s\r\n", lineno, msg);
d0d307a
+    *errstr = (char *) xrealloc(*errstr, strlen(*errstr) + strlen(buf) + 30);
d0d307a
+    fprintf(stderr, "%s\n", buf);
d0d307a
+    strcat(*errstr, buf);
d0d307a
+
d0d307a
+    return SIEVE_OK;
d0d307a
+}
d0d307a
+
d0d307a
+/* end the boilerplate */
d0d307a
+
d0d307a
+/* returns TRUE or FALSE */
d0d307a
+int is_script_parsable(FILE *stream, char **errstr, sieve_script_t **ret)
d0d307a
+{
d0d307a
+    sieve_interp_t *i;
d0d307a
+    sieve_script_t *s;
d0d307a
+    int res;
d0d307a
+
d0d307a
+    res = sieve_interp_alloc(&i, NULL);
d0d307a
+    if (res != SIEVE_OK) {
d0d307a
+        fprintf(stderr, "sieve_interp_alloc() returns %d\n", res);
d0d307a
+        return TIMSIEVE_FAIL;
d0d307a
+    }
d0d307a
+
d0d307a
+    res = sieve_register_redirect(i, (sieve_callback *) &foo;;
d0d307a
+    if (res != SIEVE_OK) {
d0d307a
+        fprintf(stderr, "sieve_register_redirect() returns %d\n", res);
d0d307a
+        return TIMSIEVE_FAIL;
d0d307a
+    }
d0d307a
+    res = sieve_register_discard(i, (sieve_callback *) &foo;;
d0d307a
+    if (res != SIEVE_OK) {
d0d307a
+        fprintf(stderr, "sieve_register_discard() returns %d\n", res);
d0d307a
+        return TIMSIEVE_FAIL;
d0d307a
+    }
d0d307a
+    res = sieve_register_reject(i, (sieve_callback *) &foo;;
d0d307a
+    if (res != SIEVE_OK) {
d0d307a
+        fprintf(stderr, "sieve_register_reject() returns %d\n", res);
d0d307a
+        return TIMSIEVE_FAIL;
d0d307a
+    }
d0d307a
+    res = sieve_register_fileinto(i, (sieve_callback *) &foo;;
d0d307a
+    if (res != SIEVE_OK) {
d0d307a
+        fprintf(stderr, "sieve_register_fileinto() returns %d\n", res);
d0d307a
+        return TIMSIEVE_FAIL;
d0d307a
+    }
d0d307a
+    res = sieve_register_keep(i, (sieve_callback *) &foo;;
d0d307a
+    if (res != SIEVE_OK) {
d0d307a
+        fprintf(stderr, "sieve_register_keep() returns %d\n", res);
d0d307a
+        return TIMSIEVE_FAIL;
d0d307a
+    }
d0d307a
+
d0d307a
+    res = sieve_register_imapflags(i, NULL);
d0d307a
+    if (res != SIEVE_OK) {
d0d307a
+        fprintf(stderr, "sieve_register_imapflags() returns %d\n", res);
d0d307a
+        return TIMSIEVE_FAIL;
d0d307a
+    }
d0d307a
+
d0d307a
+    res = sieve_register_size(i, (sieve_get_size *) &foo;;
d0d307a
+    if (res != SIEVE_OK) {
d0d307a
+        fprintf(stderr, "sieve_register_size() returns %d\n", res);
d0d307a
+        return TIMSIEVE_FAIL;
d0d307a
+    }
d0d307a
+
d0d307a
+    res = sieve_register_header(i, (sieve_get_header *) &foo;;
d0d307a
+    if (res != SIEVE_OK) {
d0d307a
+        fprintf(stderr, "sieve_register_header() returns %d\n", res);
d0d307a
+        return TIMSIEVE_FAIL;
d0d307a
+    }
d0d307a
+
d0d307a
+    res = sieve_register_envelope(i, (sieve_get_envelope *) &foo;;
d0d307a
+    if (res != SIEVE_OK) {
d0d307a
+        fprintf(stderr, "sieve_register_envelope() returns %d\n", res);
d0d307a
+        return TIMSIEVE_FAIL;
d0d307a
+    }
d0d307a
+
d0d307a
+    res = sieve_register_vacation(i, &vacation2);
d0d307a
+    if (res != SIEVE_OK) {
d0d307a
+        fprintf(stderr, "sieve_register_vacation() returns %d\n", res);
d0d307a
+        return TIMSIEVE_FAIL;
d0d307a
+    }
d0d307a
+
d0d307a
+    res = sieve_register_notify(i, &sieve_notify);
d0d307a
+    if (res != SIEVE_OK) {
d0d307a
+        fprintf(stderr, "sieve_register_notify() returns %d\n", res);
d0d307a
+        return TIMSIEVE_FAIL;
d0d307a
+    }
d0d307a
+
d0d307a
+    res = sieve_register_parse_error(i, &mysieve_error);
d0d307a
+    if (res != SIEVE_OK) {
d0d307a
+        fprintf(stderr, "sieve_register_parse_error() returns %d\n", res);
d0d307a
+        return TIMSIEVE_FAIL;
d0d307a
+    }
d0d307a
+
d0d307a
+    rewind(stream);
d0d307a
+
d0d307a
+    *errstr = (char *) xmalloc(20 * sizeof(char));
d0d307a
+    strcpy(*errstr, "script errors:\r\n");
d0d307a
+
d0d307a
+    res = sieve_script_parse(i, stream, errstr, &s);
d0d307a
+
d0d307a
+    if (res == SIEVE_OK) {
d0d307a
+        if(ret) {
d0d307a
+            *ret = s;
d0d307a
+        } else {
d0d307a
+            sieve_script_free(&s);
d0d307a
+        }
d0d307a
+        free(*errstr);
d0d307a
+        *errstr = NULL;
d0d307a
+    }
d0d307a
+
d0d307a
+    /* free interpreter */
d0d307a
+    sieve_interp_free(&i);
d0d307a
+
d0d307a
+    return (res == SIEVE_OK) ? TIMSIEVE_OK : TIMSIEVE_FAIL;
d0d307a
+}
d0d307a
+
d0d307a
+
d0d307a
+
d0d307a
+
d0d307a
+
d0d307a
+
346e223
diff -Naur cyrus-imapd-2.3.12p2/imap/imapd.c cyrus-imapd-2.3.12p2-autocreate.uncompiled/imap/imapd.c
346e223
--- cyrus-imapd-2.3.12p2/imap/imapd.c	2008-04-13 18:40:29.000000000 +0300
346e223
+++ cyrus-imapd-2.3.12p2-autocreate.uncompiled/imap/imapd.c	2008-05-06 14:48:41.000000000 +0300
346e223
@@ -208,6 +208,7 @@
d0d307a
 void motd_file(int fd);
d0d307a
 void shut_down(int code);
d0d307a
 void fatal(const char *s, int code);
d0d307a
+void autocreate_inbox(void);
d0d307a
 
d0d307a
 void cmdloop(void);
d0d307a
 void cmd_login(char *tag, char *user);
346e223
@@ -1970,6 +1971,43 @@
d0d307a
 }
d0d307a
 
d0d307a
 /*
d0d307a
+ * Autocreate Inbox and subfolders upon login
d0d307a
+ */
d0d307a
+void autocreate_inbox()
d0d307a
+{
d0d307a
+    char inboxname[MAX_MAILBOX_NAME+1];
d0d307a
+    int autocreatequota;
d0d307a
+    int r;
d0d307a
+ 
d0d307a
+    /*
d0d307a
+     * Exlude admin's accounts
d0d307a
+     */
d0d307a
+    if (imapd_userisadmin || imapd_userisproxyadmin)
d0d307a
+        return;
d0d307a
+ 
d0d307a
+    /*
d0d307a
+     * Exclude anonymous
d0d307a
+     */
d0d307a
+    if (!strcmp(imapd_userid, "anonymous"))
d0d307a
+        return;
d0d307a
+ 
d0d307a
+    if ((autocreatequota = config_getint(IMAPOPT_AUTOCREATEQUOTA))) {
d0d307a
+        /* This is actyally not required
d0d307a
+           as long as the lenght of userid is ok */
d0d307a
+           r = (*imapd_namespace.mboxname_tointernal) (&imapd_namespace,
d0d307a
+                                      "INBOX", imapd_userid, inboxname);
d0d307a
+           if (!r)
cf58a5c
+               r = mboxlist_lookup(inboxname, NULL, NULL);
d0d307a
+ 
cf58a5c
+           if (r == IMAP_MAILBOX_NONEXISTENT) {
cf58a5c
+                mboxlist_autocreateinbox(&imapd_namespace, imapd_userid,
d0d307a
+                         imapd_authstate, inboxname, autocreatequota);
cf58a5c
+	   }
d0d307a
+     }
d0d307a
+}
d0d307a
+
d0d307a
+
d0d307a
+/*
d0d307a
  * Perform a LOGIN command
d0d307a
  */
d0d307a
 void cmd_login(char *tag, char *user)
346e223
@@ -2137,6 +2175,9 @@
d0d307a
 				strcspn(imapd_userid, "@") : 0);
d0d307a
 
d0d307a
     freebuf(&passwdbuf);
d0d307a
+
d0d307a
+    autocreate_inbox();
d0d307a
+
d0d307a
     return;
d0d307a
 }
d0d307a
 
346e223
@@ -2295,6 +2336,8 @@
d0d307a
 				config_virtdomains ?
d0d307a
 				strcspn(imapd_userid, "@") : 0);
d0d307a
 
d0d307a
+    autocreate_inbox();
d0d307a
+
d0d307a
     return;
d0d307a
 }
d0d307a
 
346e223
diff -Naur cyrus-imapd-2.3.12p2/imap/lmtpd.c cyrus-imapd-2.3.12p2-autocreate.uncompiled/imap/lmtpd.c
346e223
--- cyrus-imapd-2.3.12p2/imap/lmtpd.c	2008-04-11 23:07:00.000000000 +0300
346e223
+++ cyrus-imapd-2.3.12p2-autocreate.uncompiled/imap/lmtpd.c	2008-05-06 14:48:41.000000000 +0300
Tomas Janousek 915c4a4
@@ -117,6 +117,8 @@
d0d307a
 static FILE *spoolfile(message_data_t *msgdata);
d0d307a
 static void removespool(message_data_t *msgdata);
d0d307a
 
cf58a5c
+static int autocreate_inbox(const char *user, const char *domain);
d0d307a
+
d0d307a
 /* current namespace */
d0d307a
 static struct namespace lmtpd_namespace;
d0d307a
 
346e223
@@ -976,6 +978,86 @@
d0d307a
     exit(code);
d0d307a
 }
d0d307a
 
cf58a5c
+
d0d307a
+/*
d0d307a
+ * Autocreate Inbox and subfolders upon login
d0d307a
+ */
cf58a5c
+int autocreate_inbox(const char *user, const char *domain)
d0d307a
+{
cf58a5c
+    struct auth_state *auth_state;
d0d307a
+    char inboxname[MAX_MAILBOX_NAME+1];
cf58a5c
+    char *rcpt_userid = NULL;
d0d307a
+    int autocreatequota;
cf58a5c
+    int r = 0;
cf58a5c
+
cf58a5c
+    if (user == NULL)
cf58a5c
+          return IMAP_MAILBOX_NONEXISTENT;
cf58a5c
+
cf58a5c
+    if (domain != NULL) {
cf58a5c
+	int k;
cf58a5c
+	
cf58a5c
+	rcpt_userid = (char *) xmalloc((strlen(user) + strlen(domain) + 2) * sizeof(char));
cf58a5c
+	k = strlcpy(rcpt_userid, user, strlen(user) + 1);
cf58a5c
+  	*(rcpt_userid + k) = '@';
cf58a5c
+       strlcpy(rcpt_userid + k + 1, domain, strlen(domain) + 1);
cf58a5c
+    } else {
cf58a5c
+	rcpt_userid = (char *) user;
cf58a5c
+    }
cf58a5c
+
d0d307a
+
d0d307a
+    /*
d0d307a
+     * Exclude anonymous
d0d307a
+     */
cf58a5c
+    if (!strcmp(rcpt_userid, "anonymous")) {
cf58a5c
+	if (rcpt_userid != user) {
cf58a5c
+	    free(rcpt_userid);
cf58a5c
+	}
d0d307a
+
cf58a5c
+        return IMAP_MAILBOX_NONEXISTENT;
cf58a5c
+    }
cf58a5c
+    
d0d307a
+    /*
d0d307a
+     * Check for autocreatequota and createonpost
d0d307a
+     */
d0d307a
+    if (!(autocreatequota = config_getint(IMAPOPT_AUTOCREATEQUOTA)) ||
cf58a5c
+        !(config_getswitch(IMAPOPT_CREATEONPOST))) {
cf58a5c
+	    
cf58a5c
+	if (rcpt_userid != user) {
cf58a5c
+	    free(rcpt_userid);
cf58a5c
+	}
cf58a5c
+	
d0d307a
+        return IMAP_MAILBOX_NONEXISTENT;
cf58a5c
+     }
cf58a5c
+
d0d307a
+
d0d307a
+    /*
d0d307a
+     * Exclude admin's accounts
d0d307a
+     */
cf58a5c
+     auth_state = auth_newstate(rcpt_userid);
cf58a5c
+     
cf58a5c
+     if (global_authisa(auth_state, IMAPOPT_ADMINS)) {
cf58a5c
+	if (rcpt_userid != user) {
cf58a5c
+	    free(rcpt_userid);
cf58a5c
+	}
d0d307a
+
cf58a5c
+        return IMAP_MAILBOX_NONEXISTENT;
cf58a5c
+     }
cf58a5c
+     
d0d307a
+     r = (*lmtpd_namespace.mboxname_tointernal) (&lmtpd_namespace,
d0d307a
+                                "INBOX", rcpt_userid, inboxname);
cf58a5c
+     
d0d307a
+     if (!r)
cf58a5c
+ 	r = mboxlist_autocreateinbox(&lmtpd_namespace, rcpt_userid,
cf58a5c
+                         auth_state, inboxname, autocreatequota);
cf58a5c
+     
cf58a5c
+     if (rcpt_userid != user) {
cf58a5c
+	free(rcpt_userid);
cf58a5c
+     }
cf58a5c
+       
d0d307a
+     return r;
d0d307a
+}
d0d307a
+
d0d307a
+
cf58a5c
 static int verify_user(const char *user, const char *domain, char *mailbox,
d0d307a
 		       long quotacheck, struct auth_state *authstate)
d0d307a
 {
346e223
@@ -1019,6 +1101,15 @@
d0d307a
 	 */
cf58a5c
 	r = mlookup(namebuf, &server, &acl, NULL);
cf58a5c
 
cf58a5c
+	/* If user mailbox does not exist, then invoke autocreate inbox function */
cf58a5c
+	if (r == IMAP_MAILBOX_NONEXISTENT) {
cf58a5c
+	    r = autocreate_inbox(user, domain);
cf58a5c
+
cf58a5c
+	    /* Try to locate the mailbox again */
d0d307a
+	    if (!r)
cf58a5c
+		r = mlookup(namebuf, &server, &acl, NULL);
cf58a5c
+	}
cf58a5c
+
cf58a5c
 	if (r == IMAP_MAILBOX_NONEXISTENT && !user &&
cf58a5c
 	    config_getswitch(IMAPOPT_LMTP_FUZZY_MAILBOX_MATCH) &&
cf58a5c
 	    /* see if we have a mailbox whose name is close */
346e223
@@ -1045,6 +1136,7 @@
cf58a5c
 			     aclcheck, (quotacheck < 0)
cf58a5c
 			     || config_getswitch(IMAPOPT_LMTP_STRICT_QUOTA) ?
cf58a5c
 			     quotacheck : 0);
d0d307a
+
cf58a5c
 	}
d0d307a
     }
d0d307a
 
346e223
diff -Naur cyrus-imapd-2.3.12p2/imap/mboxlist.c cyrus-imapd-2.3.12p2-autocreate.uncompiled/imap/mboxlist.c
346e223
--- cyrus-imapd-2.3.12p2/imap/mboxlist.c	2008-04-04 14:09:42.000000000 +0300
346e223
+++ cyrus-imapd-2.3.12p2-autocreate.uncompiled/imap/mboxlist.c	2008-05-06 14:48:41.000000000 +0300
346e223
@@ -83,6 +83,12 @@
d0d307a
 #include "quota.h"
Tomas Janousek 6861497
 #include "sync_log.h"
d0d307a
 
d0d307a
+#ifdef USE_SIEVE
d0d307a
+extern int autoadd_sieve(char *userid, 
d0d307a
+		const char *source_script);
d0d307a
+#endif
d0d307a
+
d0d307a
+
d0d307a
 #define DB config_mboxlist_db
d0d307a
 #define SUBDB config_subscription_db
d0d307a
 
346e223
@@ -100,11 +106,29 @@
d0d307a
 static int mboxlist_changequota(const char *name, int matchlen, int maycreate,
d0d307a
 				void *rock);
d0d307a
 
d0d307a
+static int mboxlist_autochangesub(char *name, int matchlen, int maycreate,
d0d307a
+			      void *rock);
d0d307a
+
d0d307a
+static int mboxlist_autosubscribe_sharedfolders(struct namespace *namespace,
d0d307a
+                        char *userid, char *auth_userid,
d0d307a
+                        struct auth_state *auth_state);
d0d307a
+
d0d307a
 struct change_rock {
d0d307a
     struct quota *quota;
d0d307a
     struct txn **tid;
d0d307a
 };
d0d307a
 
d0d307a
+/*
d0d307a
+ * Struct needed to be passed as void *rock to
d0d307a
+ * mboxlist_autochangesub();
d0d307a
+ */
d0d307a
+struct changesub_rock_st {
d0d307a
+        char *userid;
d0d307a
+        char *auth_userid;
d0d307a
+        struct auth_state *auth_state;
d0d307a
+};
d0d307a
+
d0d307a
+
d0d307a
 #define FNAME_SUBSSUFFIX ".sub"
d0d307a
 
d0d307a
 /*
346e223
@@ -3383,3 +3407,349 @@
d0d307a
 
Tomas Janousek 6861497
     return(config_delete_mode == IMAP_ENUM_DELETE_MODE_DELAYED);
d0d307a
 }
d0d307a
+
d0d307a
+/*
d0d307a
+ * Automatically subscribe user to *ALL* shared folders,
d0d307a
+ * one has permissions to be subscribed to.
d0d307a
+ * INBOX subfolders are excluded.
d0d307a
+ */
d0d307a
+static int mboxlist_autochangesub(char *name, int matchlen, int maycreate,
d0d307a
+                        void *rock) {
d0d307a
+
d0d307a
+  struct changesub_rock_st *changesub_rock = (struct changesub_rock_st *) rock;
d0d307a
+  char *userid = changesub_rock->userid;
d0d307a
+  char *auth_userid = changesub_rock->auth_userid;
d0d307a
+  struct auth_state *auth_state = changesub_rock->auth_state;
d0d307a
+  int r;
d0d307a
+
d0d307a
+
d0d307a
+  if((strlen(name) == 5 && !strncmp(name, "INBOX", 5)) || /* Exclude INBOX */
d0d307a
+     (strlen(name) > 5  && !strncmp(name, "INBOX.",6)) || /* Exclude INBOX subfolders */
d0d307a
+     (strlen(name) > 4  && !strncmp(name, "user.", 5)))   /* Exclude other users' folders */
d0d307a
+	  return 0;
d0d307a
+
d0d307a
+     
d0d307a
+  r = mboxlist_changesub(name, userid, auth_state, 1, 0);
d0d307a
+
d0d307a
+  if (r) {
d0d307a
+      syslog(LOG_WARNING,
d0d307a
+             "autosubscribe: User %s to folder %s, subscription failed: %s",
d0d307a
+             auth_userid, name, error_message(r));
d0d307a
+  } else {
d0d307a
+      syslog(LOG_NOTICE,
d0d307a
+             "autosubscribe: User %s to folder %s, subscription succeeded",
d0d307a
+             auth_userid, name);
d0d307a
+  }
d0d307a
+
d0d307a
+  return 0;
d0d307a
+}
d0d307a
+
d0d307a
+#define SEP '|'
d0d307a
+
d0d307a
+/*
d0d307a
+ * Automatically subscribe user to a shared folder.
d0d307a
+ * Subscription is done successfully, if the shared
d0d307a
+ * folder exists and the user has the necessary 
d0d307a
+ * permissions.
d0d307a
+ */
d0d307a
+static int mboxlist_autosubscribe_sharedfolders(struct namespace *namespace,
d0d307a
+                        char *userid, char *auth_userid,
d0d307a
+                        struct auth_state *auth_state) {
d0d307a
+        
d0d307a
+    const char *sub ;
d0d307a
+    char *p, *q, *next_sub;
d0d307a
+    char folder[MAX_MAILBOX_NAME+1], name[MAX_MAILBOX_NAME+1], mailboxname[MAX_MAILBOX_NAME+1];
d0d307a
+    int len;
d0d307a
+    int r = 0;
d0d307a
+    int subscribe_all_sharedfolders = 0;
d0d307a
+
d0d307a
+    subscribe_all_sharedfolders = config_getswitch(IMAPOPT_AUTOSUBSCRIBE_ALL_SHAREDFOLDERS);
d0d307a
+
d0d307a
+    /*
d0d307a
+     * If subscribeallsharedfolders is set to yes in imapd.conf, then
d0d307a
+     * subscribe user to every shared folder one has the apropriate 
d0d307a
+     * permissions.
d0d307a
+     */
d0d307a
+    if(subscribe_all_sharedfolders) {
d0d307a
+       char pattern[MAX_MAILBOX_PATH+1];
d0d307a
+       struct changesub_rock_st changesub_rock;
d0d307a
+
d0d307a
+       strcpy(pattern, "*");
d0d307a
+       changesub_rock.userid = userid;
d0d307a
+       changesub_rock.auth_userid = auth_userid;
d0d307a
+       changesub_rock.auth_state = auth_state;
d0d307a
+
d0d307a
+       r = mboxlist_findall(namespace, pattern, 0, userid,
d0d307a
+                            auth_state, mboxlist_autochangesub, &changesub_rock);
d0d307a
+
d0d307a
+       return r;
d0d307a
+    }
d0d307a
+
d0d307a
+    if ((sub=config_getstring(IMAPOPT_AUTOSUBSCRIBESHAREDFOLDERS)) == NULL)
d0d307a
+       return r;
d0d307a
+
d0d307a
+    next_sub = (char *) sub;
d0d307a
+    while (*next_sub) {
d0d307a
+        for (p = next_sub ; isspace((int) *p) || *p == SEP ; p++);
d0d307a
+        for (next_sub = p ; *next_sub && *next_sub != SEP ; next_sub++);
d0d307a
+        for (q = next_sub ; q > p && (isspace((int) *q) || *q == SEP || !*q) ; q--);
d0d307a
+        if (!*p ) continue;
d0d307a
+
d0d307a
+        len = q - p + 1;
d0d307a
+        /* Check for folder length */
d0d307a
+        if (len  > sizeof(folder)-1)
d0d307a
+                continue;
d0d307a
+
d0d307a
+        if (!r) {
d0d307a
+                strncpy(folder, p, len);
d0d307a
+                folder[len] = '\0';
d0d307a
+
d0d307a
+               strlcpy(name, namespace->prefix[NAMESPACE_SHARED], sizeof(name));
d0d307a
+               len = strlcat(name, folder, sizeof(name));
d0d307a
+
d0d307a
+               r = (namespace->mboxname_tointernal) (namespace, name, userid,
d0d307a
+                                                                  mailboxname);
d0d307a
+       }
d0d307a
+                                                                  
d0d307a
+        if (!r)
d0d307a
+               r = mboxlist_changesub(mailboxname, userid, auth_state, 1, 0);
d0d307a
+
d0d307a
+        if (!r) {
d0d307a
+                syslog(LOG_NOTICE, "autosubscribe: User %s to %s succeeded", 
d0d307a
+                       userid, folder);
d0d307a
+        } else {
d0d307a
+                syslog(LOG_WARNING, "autosubscribe: User %s to %s failed: %s", 
d0d307a
+                       userid, folder, error_message(r));
d0d307a
+                r = 0;
d0d307a
+        }
d0d307a
+    }
d0d307a
+
d0d307a
+    return r;
d0d307a
+}
d0d307a
+
d0d307a
+
d0d307a
+
d0d307a
+int mboxlist_autocreateinbox(struct namespace *namespace,
d0d307a
+                        char *userid,
d0d307a
+                        struct auth_state *auth_state,
d0d307a
+                        char *mailboxname, int autocreatequota) {
d0d307a
+    char name [MAX_MAILBOX_NAME+1];
d0d307a
+    char folder [MAX_MAILBOX_NAME+1];
d0d307a
+    char *auth_userid = NULL;
d0d307a
+    char *partition = NULL;
d0d307a
+    const char *crt;
d0d307a
+    const char *sub;
d0d307a
+    char *p, *q, *next_crt, *next_sub;
d0d307a
+    int len;
d0d307a
+    int r = 0;
d0d307a
+    int numcrt = 0;
d0d307a
+    int numsub = 0;
d0d307a
+#ifdef USE_SIEVE
d0d307a
+    const char *source_script;
d0d307a
+#endif
d0d307a
+
cf58a5c
+
cf58a5c
+
cf58a5c
+    auth_userid = auth_canonuser(auth_state);
cf58a5c
+    if (auth_userid == NULL) {
cf58a5c
+         /*
cf58a5c
+          * Couldn't get cannon userid
cf58a5c
+          */
cf58a5c
+          syslog(LOG_ERR,
cf58a5c
+                 "autocreateinbox: Could not get canonified userid for user %s", userid);
cf58a5c
+          return IMAP_PARTITION_UNKNOWN;
cf58a5c
+    }
cf58a5c
+
cf58a5c
+    /* Added this for debug information. */
cf58a5c
+    syslog(LOG_DEBUG, "autocreateinbox: autocreate inbox for user %s was called", auth_userid);
cf58a5c
+
d0d307a
+   /*
d0d307a
+    * While this is not needed for admins
d0d307a
+    * and imap_admins accounts, it would be
d0d307a
+    * better to separate *all* admins and
d0d307a
+    * proxyservers from normal accounts
d0d307a
+    * (accounts that have mailboxes).
d0d307a
+    * UOA Specific note(1): Even if we do not
d0d307a
+    * exclude these servers-classes here,
d0d307a
+    * UOA specific code, will neither return
d0d307a
+    * role, nor create INBOX, because none of these
d0d307a
+    * administrative accounts belong to  the
d0d307a
+    * mailRecipient objectclass, or have imapPartition.
d0d307a
+    * UOA Specific note(2): Another good reason for doing
d0d307a
+    * this, is to prevent the code, from getting into
d0d307a
+    * cyrus_ldap.c because of the continues MSA logins to LMTPd.
d0d307a
+    */
d0d307a
+
d0d307a
+   /*
d0d307a
+    * admins and the coresponding imap
d0d307a
+    * service, had already been excluded.
d0d307a
+    */
d0d307a
+
d0d307a
+   /*
d0d307a
+    * Do we really need group membership
d0d307a
+    * for admins or service_admins?
d0d307a
+    */
d0d307a
+    if (global_authisa(auth_state, IMAPOPT_ADMINS)) return 0;
d0d307a
+
d0d307a
+   /*
d0d307a
+    * Do we really need group membership
d0d307a
+    * for proxyservers?
d0d307a
+    */
d0d307a
+    if (global_authisa(auth_state, IMAPOPT_PROXYSERVERS)) return 0;
d0d307a
+
cf58a5c
+    /* 
cf58a5c
+     * Check if user belongs to the autocreate_users group. This option
cf58a5c
+     * controls for whom the mailbox may be automatically created. Default
cf58a5c
+     * value for this option is 'anyone'. So, if not declared, all mailboxes
cf58a5c
+     * will be created.
cf58a5c
+     */
cf58a5c
+    if (!global_authisa(auth_state, IMAPOPT_AUTOCREATE_USERS)) {
cf58a5c
+	    syslog(LOG_DEBUG, "autocreateinbox: User %s does not belong to the autocreate_users. No mailbox is created",
cf58a5c
+			    auth_userid);
cf58a5c
+	    return IMAP_MAILBOX_NONEXISTENT;
d0d307a
+    }
d0d307a
+
d0d307a
+#if 0
d0d307a
+        /*
d0d307a
+         * Get Partition info or return.
d0d307a
+         * (Here you should propably use
d0d307a
+         * you own "get_partition(char *userid)"
d0d307a
+         * function. Otherwise all new INBOXes will be
d0d307a
+         * created into whatever partition has been declared
d0d307a
+         * as default in your imapd.conf)
d0d307a
+         */
d0d307a
+
d0d307a
+        partition = get_partition(userid);
d0d307a
+        if (partition == NULL) {
d0d307a
+            /*
d0d307a
+             * Couldn't get partition info
d0d307a
+             */
d0d307a
+            syslog(LOG_ERR,
d0d307a
+                   "Could not get imapPartition info for user %s", userid);
d0d307a
+            return IMAP_PARTITION_UNKNOWN;
d0d307a
+        }
d0d307a
+#endif
d0d307a
+
d0d307a
+    r = mboxlist_createmailbox(mailboxname, MAILBOX_FORMAT_NORMAL, NULL,
d0d307a
+                                      1, userid, auth_state, 0, 0, 0);
d0d307a
+
d0d307a
+    if (!r && autocreatequota > 0)
d0d307a
+        r = mboxlist_setquota(mailboxname, autocreatequota, 0);
d0d307a
+
d0d307a
+    if (!r)
d0d307a
+        r = mboxlist_changesub(mailboxname, userid,
d0d307a
+                              auth_state, 1, 1);
d0d307a
+
d0d307a
+    if (!r) {
d0d307a
+       syslog(LOG_NOTICE, "autocreateinbox: User %s, INBOX was successfully created in partition %s", 
d0d307a
+               auth_userid, partition == NULL ? "default" : partition);
d0d307a
+    } else {
d0d307a
+       syslog(LOG_ERR, "autocreateinbox: User %s, INBOX failed. %s", 
d0d307a
+               auth_userid, error_message(r));
d0d307a
+    }
d0d307a
+
d0d307a
+#if 0
d0d307a
+    /* Allocated from get_partition, and not needed any more */
d0d307a
+    free_partition(partition);
d0d307a
+#endif
d0d307a
+
d0d307a
+    if (r) return r;
d0d307a
+
d0d307a
+    /* INBOX's subfolders */
d0d307a
+    if ((crt=config_getstring(IMAPOPT_AUTOCREATEINBOXFOLDERS)))
d0d307a
+        sub=config_getstring(IMAPOPT_AUTOSUBSCRIBEINBOXFOLDERS);
d0d307a
+
d0d307a
+    /* Roll through crt */
d0d307a
+    next_crt = (char *) crt;
d0d307a
+    while (next_crt!=NULL && *next_crt) {
d0d307a
+          for (p = next_crt ; isspace((int) *p) || *p == SEP ; p++);
d0d307a
+          for (next_crt = p ; *next_crt && *next_crt != SEP ; next_crt++);
d0d307a
+          for (q = next_crt ; q > p && (isspace((int) *q) || *q == SEP || !*q); q--);
d0d307a
+
d0d307a
+          if (!*p) continue;
d0d307a
+
d0d307a
+          len = q - p + 1;
d0d307a
+
d0d307a
+          /* First time we check for length */
d0d307a
+          if (len > sizeof(folder) - 5)
d0d307a
+              r = IMAP_MAILBOX_BADNAME;
d0d307a
+
d0d307a
+          if (!r) {
d0d307a
+                  strncpy(folder, p, len);
d0d307a
+                  folder[len] = '\0';
d0d307a
+
d0d307a
+                  strlcpy(name, namespace->prefix[NAMESPACE_INBOX], sizeof(name));
d0d307a
+                  len = strlcat(name, folder, sizeof(name));
d0d307a
+          }
d0d307a
+
d0d307a
+          if (!r)
d0d307a
+            r = (namespace->mboxname_tointernal) (namespace, name, userid,
d0d307a
+                                                 mailboxname);
d0d307a
+          if (!r)
d0d307a
+             r = mboxlist_createmailbox(mailboxname, MAILBOX_FORMAT_NORMAL, NULL,
d0d307a
+                                            1, userid, auth_state, 0, 0, 0);
d0d307a
+
d0d307a
+          if (!r) {
d0d307a
+            numcrt++;
d0d307a
+            syslog(LOG_NOTICE, "autocreateinbox: User %s, subfolder %s creation succeeded.", 
d0d307a
+               auth_userid, name);
d0d307a
+         } else {
d0d307a
+             syslog(LOG_WARNING, "autocreateinbox: User %s, subfolder %s creation failed. %s", 
d0d307a
+               auth_userid, name, error_message(r));
d0d307a
+             r=0;
d0d307a
+             continue;
d0d307a
+          }
d0d307a
+
d0d307a
+          /* Roll through sub */
d0d307a
+          next_sub = (char *) sub;
d0d307a
+          while (next_sub!=NULL && *next_sub) {
d0d307a
+                for (p = next_sub ; isspace((int) *p) || *p == SEP ; p++);
d0d307a
+                for (next_sub = p ; *next_sub && *next_sub != SEP ; next_sub++);
d0d307a
+                for (q = next_sub ; q > p && (isspace((int) *q) || *q == SEP || !*q) ; q--);
d0d307a
+                if (!*p ) continue;
d0d307a
+
d0d307a
+                len = q - p + 1;
d0d307a
+
d0d307a
+                if (len != strlen(folder) || strncmp(folder, p, len))
d0d307a
+                    continue;
d0d307a
+
d0d307a
+                r = mboxlist_changesub(mailboxname, userid, auth_state, 1, 1);
d0d307a
+
d0d307a
+               if (!r) {
d0d307a
+                   numsub++;
d0d307a
+                   syslog(LOG_NOTICE,"autocreateinbox: User %s, subscription to %s succeeded",
d0d307a
+                       auth_userid, name);
d0d307a
+               } else
d0d307a
+                    syslog(LOG_WARNING, "autocreateinbox: User %s, subscription to  %s failed. %s",
d0d307a
+                       auth_userid, name, error_message(r));
d0d307a
+
d0d307a
+                break;
d0d307a
+         }
d0d307a
+    }
d0d307a
+
d0d307a
+    if (crt!=NULL && *crt)
d0d307a
+       syslog(LOG_INFO, "User %s, Inbox subfolders, created %d, subscribed %d", 
d0d307a
+               auth_userid, numcrt, numsub);
d0d307a
+
d0d307a
+    /*
d0d307a
+     * Check if shared folders are available for subscription.
d0d307a
+     */
d0d307a
+    mboxlist_autosubscribe_sharedfolders(namespace, userid, auth_userid, auth_state);
d0d307a
+
d0d307a
+#ifdef USE_SIEVE
d0d307a
+    /*
d0d307a
+     * Here the autocreate sieve script feature is iniated from.
d0d307a
+     */
d0d307a
+    source_script = config_getstring(IMAPOPT_AUTOCREATE_SIEVE_SCRIPT);
d0d307a
+ 
d0d307a
+    if (source_script) {
d0d307a
+        if (!autoadd_sieve(userid, source_script))
d0d307a
+            syslog(LOG_NOTICE, "autocreate_sieve: User %s, default sieve script creation succeeded", auth_userid);
d0d307a
+        else
d0d307a
+            syslog(LOG_WARNING, "autocreate_sieve: User %s, default sieve script creation failed", auth_userid);
d0d307a
+    }
d0d307a
+#endif
d0d307a
+
d0d307a
+    return r;
d0d307a
+}
d0d307a
+
346e223
diff -Naur cyrus-imapd-2.3.12p2/imap/mboxlist.h cyrus-imapd-2.3.12p2-autocreate.uncompiled/imap/mboxlist.h
346e223
--- cyrus-imapd-2.3.12p2/imap/mboxlist.h	2008-03-24 19:09:18.000000000 +0200
346e223
+++ cyrus-imapd-2.3.12p2-autocreate.uncompiled/imap/mboxlist.h	2008-05-06 14:48:41.000000000 +0300
346e223
@@ -212,4 +212,8 @@
d0d307a
 int mboxlist_abort(struct txn *tid);
d0d307a
 
Tomas Janousek 6861497
 int mboxlist_delayed_delete_isenabled(void);
Tomas Janousek 6861497
+int mboxlist_autocreateinbox(struct namespace *namespace,char *userid,
Tomas Janousek 6861497
+                       struct auth_state *auth_state, char *mailboxname, 
Tomas Janousek 6861497
+                       int autocreatequota);
d0d307a
+
d0d307a
 #endif
346e223
diff -Naur cyrus-imapd-2.3.12p2/imap/pop3d.c cyrus-imapd-2.3.12p2-autocreate.uncompiled/imap/pop3d.c
346e223
--- cyrus-imapd-2.3.12p2/imap/pop3d.c	2008-04-11 23:07:00.000000000 +0300
346e223
+++ cyrus-imapd-2.3.12p2-autocreate.uncompiled/imap/pop3d.c	2008-05-06 14:48:41.000000000 +0300
346e223
@@ -172,6 +172,8 @@
d0d307a
 static char popd_apop_chal[45 + MAXHOSTNAMELEN + 1]; /* <rand.time@hostname> */
d0d307a
 static void cmd_apop(char *response);
d0d307a
 
d0d307a
+static int autocreate_inbox(char *inboxname, char *userid);
d0d307a
+
d0d307a
 static void cmd_auth(char *arg);
d0d307a
 static void cmd_capa(void);
d0d307a
 static void cmd_pass(char *pass);
346e223
@@ -1245,6 +1247,7 @@
cf58a5c
 	popd_userid = xstrdup(userbuf);
d0d307a
 	prot_printf(popd_out, "+OK Name is a valid mailbox\r\n");
d0d307a
     }
d0d307a
+
d0d307a
 }
d0d307a
 
d0d307a
 void cmd_pass(char *pass)
346e223
@@ -1545,6 +1548,43 @@
d0d307a
 }
d0d307a
 
d0d307a
 /*
d0d307a
+ * Autocreate Inbox and subfolders upon login
d0d307a
+ */
d0d307a
+int autocreate_inbox(char *inboxname, char *auth_userid)
d0d307a
+{
cf58a5c
+    struct auth_state *auth_state;
d0d307a
+    int autocreatequota;
d0d307a
+    int r;
d0d307a
+
cf58a5c
+    if (inboxname == NULL || auth_userid == NULL)
d0d307a
+	    return IMAP_MAILBOX_NONEXISTENT;
d0d307a
+    
d0d307a
+    /*
d0d307a
+     * Exclude anonymous
d0d307a
+     */
d0d307a
+    if (!strcmp(popd_userid, "anonymous"))
d0d307a
+        return IMAP_MAILBOX_NONEXISTENT;
d0d307a
+
d0d307a
+    /*
d0d307a
+     * Check for autocreatequota
d0d307a
+     */
d0d307a
+    if (!(autocreatequota = config_getint(IMAPOPT_AUTOCREATEQUOTA)))
d0d307a
+        return IMAP_MAILBOX_NONEXISTENT;
d0d307a
+
d0d307a
+    /*
d0d307a
+     * Exclude admin's accounts
d0d307a
+     */
cf58a5c
+     auth_state = auth_newstate(popd_userid);
cf58a5c
+     if (global_authisa(auth_state, IMAPOPT_ADMINS))
d0d307a
+          return IMAP_MAILBOX_NONEXISTENT;
d0d307a
+
d0d307a
+     r = mboxlist_autocreateinbox(&popd_namespace, auth_userid,
cf58a5c
+                         auth_state, inboxname, autocreatequota);
d0d307a
+     return r;
d0d307a
+}
d0d307a
+
d0d307a
+
d0d307a
+/*
d0d307a
  * Complete the login process by opening and locking the user's inbox
d0d307a
  */
d0d307a
 int openinbox(void)
346e223
@@ -1573,6 +1613,12 @@
d0d307a
 
cf58a5c
     if (!r) r = mboxlist_detail(inboxname, &type, NULL, NULL,
cf58a5c
 				&server, &acl, NULL);
cf58a5c
+
d0d307a
+    /* Try once again after autocreate_inbox */
d0d307a
+    if (r == IMAP_MAILBOX_NONEXISTENT && !(r = autocreate_inbox(inboxname, userid)))
cf58a5c
+	r = mboxlist_detail(inboxname, &type, NULL, NULL,
cf58a5c
+				&server, &acl, NULL);
d0d307a
+
d0d307a
     if (!r && (config_popuseacl = config_getswitch(IMAPOPT_POPUSEACL)) &&
d0d307a
 	(!acl ||
d0d307a
 	 !((myrights = cyrus_acl_myrights(popd_authstate, acl)) & ACL_READ))) {
346e223
diff -Naur cyrus-imapd-2.3.12p2/lib/auth.c cyrus-imapd-2.3.12p2-autocreate.uncompiled/lib/auth.c
346e223
--- cyrus-imapd-2.3.12p2/lib/auth.c	2008-03-24 19:43:08.000000000 +0200
346e223
+++ cyrus-imapd-2.3.12p2-autocreate.uncompiled/lib/auth.c	2008-05-06 14:48:41.000000000 +0300
346e223
@@ -118,3 +118,11 @@
d0d307a
 
cf58a5c
     auth->freestate(auth_state);
cf58a5c
 }
d0d307a
+
cf58a5c
+char *auth_canonuser(struct auth_state *auth_state)
cf58a5c
+{
cf58a5c
+    struct auth_mech *auth = auth_fromname();
cf58a5c
+
cf58a5c
+    return auth->auth_canonuser(auth_state);
cf58a5c
+}
cf58a5c
+
346e223
diff -Naur cyrus-imapd-2.3.12p2/lib/auth.h cyrus-imapd-2.3.12p2-autocreate.uncompiled/lib/auth.h
346e223
--- cyrus-imapd-2.3.12p2/lib/auth.h	2008-03-24 19:43:08.000000000 +0200
346e223
+++ cyrus-imapd-2.3.12p2-autocreate.uncompiled/lib/auth.h	2008-05-06 14:48:41.000000000 +0300
346e223
@@ -55,6 +55,7 @@
cf58a5c
              const char *identifier);
cf58a5c
     struct auth_state *(*newstate)(const char *identifier);
cf58a5c
     void (*freestate)(struct auth_state *auth_state);
cf58a5c
+    char *(*auth_canonuser)(struct auth_state *auth_state);
cf58a5c
 };
cf58a5c
 
cf58a5c
 extern struct auth_mech *auth_mechs[];
346e223
@@ -77,5 +78,6 @@
cf58a5c
  	 const char *identifier);
cf58a5c
 struct auth_state *auth_newstate(const char *identifier);
cf58a5c
 void auth_freestate(struct auth_state *auth_state);
cf58a5c
+char *auth_canonuser(struct auth_state *auth_state);
cf58a5c
 
d0d307a
 #endif /* INCLUDED_AUTH_H */
346e223
diff -Naur cyrus-imapd-2.3.12p2/lib/auth_krb.c cyrus-imapd-2.3.12p2-autocreate.uncompiled/lib/auth_krb.c
346e223
--- cyrus-imapd-2.3.12p2/lib/auth_krb.c	2008-03-24 19:43:08.000000000 +0200
346e223
+++ cyrus-imapd-2.3.12p2-autocreate.uncompiled/lib/auth_krb.c	2008-05-06 14:48:41.000000000 +0300
346e223
@@ -340,6 +340,15 @@
d0d307a
     free((char *)auth_state);
d0d307a
 }
d0d307a
 
cf58a5c
+static char *mycanonuser(struct auth_state *auth_state)
d0d307a
+{
d0d307a
+    if (auth_state)
d0d307a
+       return auth_state->userid;
cf58a5c
+
d0d307a
+    return NULL;
d0d307a
+}
d0d307a
+
cf58a5c
+
cf58a5c
 #else /* HAVE_KRB */
cf58a5c
 
cf58a5c
 static int mymemberof(
346e223
@@ -371,6 +380,13 @@
cf58a5c
 	fatal("Authentication mechanism (krb) not compiled in", EC_CONFIG);
cf58a5c
 }
cf58a5c
 
cf58a5c
+static char *mycanonuser(
cf58a5c
+    struct auth_state *auth_state __attribute__((unused)))
cf58a5c
+{
cf58a5c
+        fatal("Authentication mechanism (krb) not compiled in", EC_CONFIG);
cf58a5c
+}
cf58a5c
+
cf58a5c
+
cf58a5c
 #endif
cf58a5c
 
cf58a5c
 struct auth_mech auth_krb = 
346e223
@@ -381,4 +397,5 @@
cf58a5c
     &mymemberof,
cf58a5c
     &mynewstate,
cf58a5c
     &myfreestate,
cf58a5c
+    &mycanonuser,
cf58a5c
 };
346e223
diff -Naur cyrus-imapd-2.3.12p2/lib/auth_krb5.c cyrus-imapd-2.3.12p2-autocreate.uncompiled/lib/auth_krb5.c
346e223
--- cyrus-imapd-2.3.12p2/lib/auth_krb5.c	2008-03-24 19:43:08.000000000 +0200
346e223
+++ cyrus-imapd-2.3.12p2-autocreate.uncompiled/lib/auth_krb5.c	2008-05-06 14:48:41.000000000 +0300
346e223
@@ -199,6 +199,14 @@
d0d307a
     free(auth_state);
d0d307a
 }
d0d307a
 
cf58a5c
+static char *mycanonuser(struct auth_state *auth_state)
d0d307a
+{
d0d307a
+    if (auth_state)
d0d307a
+       return auth_state->userid;
cf58a5c
+
d0d307a
+    return NULL;
d0d307a
+}
cf58a5c
+
cf58a5c
 #else /* HAVE_GSSAPI_H */
d0d307a
 
cf58a5c
 static int mymemberof(
346e223
@@ -230,6 +238,12 @@
cf58a5c
 	fatal("Authentication mechanism (krb5) not compiled in", EC_CONFIG);
d0d307a
 }
cf58a5c
 
cf58a5c
+static char *mycanonuser(
cf58a5c
+    struct auth_state *auth_state __attribute__((unused)))
cf58a5c
+{
cf58a5c
+        fatal("Authentication mechanism (krb5) not compiled in", EC_CONFIG);
cf58a5c
+}
d0d307a
+
cf58a5c
 #endif
cf58a5c
 
cf58a5c
 struct auth_mech auth_krb5 = 
346e223
@@ -240,4 +254,5 @@
cf58a5c
     &mymemberof,
cf58a5c
     &mynewstate,
cf58a5c
     &myfreestate,
cf58a5c
+    &mycanonuser,
cf58a5c
 };
346e223
diff -Naur cyrus-imapd-2.3.12p2/lib/auth_pts.c cyrus-imapd-2.3.12p2-autocreate.uncompiled/lib/auth_pts.c
346e223
--- cyrus-imapd-2.3.12p2/lib/auth_pts.c	2008-03-24 19:43:08.000000000 +0200
346e223
+++ cyrus-imapd-2.3.12p2-autocreate.uncompiled/lib/auth_pts.c	2008-05-06 14:48:41.000000000 +0300
346e223
@@ -512,6 +512,14 @@
cf58a5c
     free(auth_state);
cf58a5c
 }
cf58a5c
 
cf58a5c
+static char *mycanonuser(struct auth_state *auth_state)
d0d307a
+{
d0d307a
+    if (auth_state)
d0d307a
+       return auth_state->userid.id;
cf58a5c
+
d0d307a
+    return NULL;
d0d307a
+}
d0d307a
+
cf58a5c
 struct auth_mech auth_pts = 
cf58a5c
 {
cf58a5c
     "pts",		/* name */
346e223
@@ -520,4 +528,5 @@
cf58a5c
     &mymemberof,
cf58a5c
     &mynewstate,
cf58a5c
     &myfreestate,
cf58a5c
+    &mycanonuser,
cf58a5c
 };
346e223
diff -Naur cyrus-imapd-2.3.12p2/lib/auth_unix.c cyrus-imapd-2.3.12p2-autocreate.uncompiled/lib/auth_unix.c
346e223
--- cyrus-imapd-2.3.12p2/lib/auth_unix.c	2008-03-24 19:43:08.000000000 +0200
346e223
+++ cyrus-imapd-2.3.12p2-autocreate.uncompiled/lib/auth_unix.c	2008-05-06 14:48:41.000000000 +0300
346e223
@@ -312,6 +312,16 @@
d0d307a
     free((char *)auth_state);
d0d307a
 }
d0d307a
 
cf58a5c
+static char *mycanonuser(auth_state)
cf58a5c
+    struct auth_state *auth_state;
d0d307a
+{
d0d307a
+    if (auth_state)
d0d307a
+         return auth_state->userid;
cf58a5c
+
d0d307a
+    return NULL;
d0d307a
+}
cf58a5c
+
cf58a5c
+
d0d307a
 
cf58a5c
 struct auth_mech auth_unix = 
cf58a5c
 {
346e223
@@ -321,4 +331,5 @@
cf58a5c
     &mymemberof,
cf58a5c
     &mynewstate,
cf58a5c
     &myfreestate,
cf58a5c
+    &mycanonuser,
cf58a5c
 };
346e223
diff -Naur cyrus-imapd-2.3.12p2/lib/imapoptions cyrus-imapd-2.3.12p2-autocreate.uncompiled/lib/imapoptions
346e223
--- cyrus-imapd-2.3.12p2/lib/imapoptions	2008-04-11 23:07:00.000000000 +0300
346e223
+++ cyrus-imapd-2.3.12p2-autocreate.uncompiled/lib/imapoptions	2008-05-06 14:48:41.000000000 +0300
346e223
@@ -198,6 +198,55 @@
d0d307a
 /* Number of seconds to wait before returning a timeout failure when
Tomas Janousek 915c4a4
    performing a client connection (e.g. in a murder environment) */
d0d307a
 
d0d307a
+{ "createonpost", 0, SWITCH }
d0d307a
+/* If yes, when lmtpd receives an incoming mail for an INBOX that does not exist,
d0d307a
+   then the INBOX is automatically created by lmtpd. */
d0d307a
+
d0d307a
+{ "autocreateinboxfolders", NULL, STRING }
cf58a5c
+/* If a user does not have an INBOX created then the INBOX as well as some INBOX 
cf58a5c
+   subfolders are created under two conditions.
d0d307a
+     1. The user logins via the IMAP or the POP3 protocol. (autocreatequota option must have a nonzero value)
cf58a5c
+     2. A message arrives for the user through the LMTPD protocol.(createonpost option must be yes)
cf58a5c
+   autocreateinboxfolders is a list of INBOX's subfolders separated by a "|", that 
cf58a5c
+   are automatically created by the server under the previous two situations. */
d0d307a
+
d0d307a
+{ "autosubscribeinboxfolders", NULL, STRING }
cf58a5c
+/* A list of folder names, separated by "|", that the users get automatically subscribed to, 
cf58a5c
+   when their INBOX is created. These folder names must have been included in the 
cf58a5c
+   autocreateinboxfolders option of the imapd.conf. */
d0d307a
+
d0d307a
+{ "autosubscribesharedfolders", NULL, STRING }
cf58a5c
+/* A list of shared folders (bulletin boards), separated by "|", that the users get
cf58a5c
+   automatically subscribed to, after their INBOX is created. The shared folder must 
cf58a5c
+   have been created and the user must have the required permissions to get subscribed 
cf58a5c
+   to it. Otherwise, subscribing to the shared folder fails. */
d0d307a
+
d0d307a
+{ "autosubscribe_all_sharedfolders", 0, SWITCH }
cf58a5c
+/* If set to yes, the user is automatically subscribed to all shared folders, one has permission
d0d307a
+   to subscribe to. */
d0d307a
+
d0d307a
+{ "autocreate_sieve_script", NULL, STRING }
d0d307a
+/* The full path of a file that contains a sieve script. This script automatically becomes a
d0d307a
+   user's initial default sieve filter script. When this option is not defined, no default 
d0d307a
+   sieve filter is created. The file must be readable by the cyrus daemon. */
d0d307a
+
d0d307a
+{ "autocreate_sieve_compiledscript", NULL, STRING }
d0d307a
+/* The full path of a file that contains a compiled in bytecode sieve script. This script 
cf58a5c
+   automatically becomes a user's initial default sieve filter script.  If this option is 
cf58a5c
+   not specified, or the filename doesn't exist then the script defined by 
cf58a5c
+   autocreate_sieve_script is compiled on the fly and installed as the user's default
d0d307a
+   sieve script */
d0d307a
+
d0d307a
+{ "generate_compiled_sieve_script", 0, SWITCH }
cf58a5c
+/* If set to yes and no compiled sieve script file exists, the sieve script which is 
d0d307a
+   compiled on the fly will be saved in the file name that autocreate_sieve_compiledscript 
cf58a5c
+   option points to. In order a compiled script to be generated, autocreate_sieve_script and 
d0d307a
+   autocreate_sieve_compiledscript must have valid values */
d0d307a
+
cf58a5c
+{ "autocreate_users", "anyone", STRING }
cf58a5c
+/* A space separated list of users and/or groups that are allowed their INBOX to be
cf58a5c
+   automatically created. */
cf58a5c
+
d0d307a
 { "configdirectory", NULL, STRING }
d0d307a
 /* The pathname of the IMAP configuration directory.  This field is
d0d307a
    required. */
346e223
diff -Naur cyrus-imapd-2.3.12p2/notifyd/Makefile.in cyrus-imapd-2.3.12p2-autocreate.uncompiled/notifyd/Makefile.in
346e223
--- cyrus-imapd-2.3.12p2/notifyd/Makefile.in	2008-03-24 21:59:32.000000000 +0200
346e223
+++ cyrus-imapd-2.3.12p2-autocreate.uncompiled/notifyd/Makefile.in	2008-05-06 14:48:41.000000000 +0300
346e223
@@ -71,10 +71,11 @@
d0d307a
 SERVICE=../master/service.o
d0d307a
 
d0d307a
 IMAP_LIBS = @IMAP_LIBS@ @LIB_RT@
d0d307a
+SIEVE_LIBS = @SIEVE_LIBS@
d0d307a
 IMAP_COM_ERR_LIBS = @IMAP_COM_ERR_LIBS@
d0d307a
 LIB_WRAP = @LIB_WRAP@
d0d307a
 LIBS = @ZEPHYR_LIBS@ @LIBS@ $(IMAP_COM_ERR_LIBS)
d0d307a
-DEPLIBS=../imap/mutex_fake.o ../imap/libimap.a ../lib/libcyrus.a ../lib/libcyrus_min.a @DEPLIBS@
d0d307a
+DEPLIBS=../imap/mutex_fake.o ../imap/libimap.a $(SIEVE_LIBS) ../lib/libcyrus.a ../lib/libcyrus_min.a @DEPLIBS@
d0d307a
 
d0d307a
 PURIFY=/usr/local/bin/purify
d0d307a
 PUREOPT=-best-effort
346e223
diff -Naur cyrus-imapd-2.3.12p2/notifyd/notifyd.c cyrus-imapd-2.3.12p2-autocreate.uncompiled/notifyd/notifyd.c
346e223
--- cyrus-imapd-2.3.12p2/notifyd/notifyd.c	2008-03-24 21:59:32.000000000 +0200
346e223
+++ cyrus-imapd-2.3.12p2-autocreate.uncompiled/notifyd/notifyd.c	2008-05-06 14:48:41.000000000 +0300
346e223
@@ -96,7 +96,7 @@
d0d307a
 
d0d307a
 #define NOTIFY_MAXSIZE 8192
d0d307a
 
d0d307a
-int do_notify()
d0d307a
+static int do_notify()
d0d307a
 {
d0d307a
     struct sockaddr_un sun_data;
d0d307a
     socklen_t sunlen = sizeof(sun_data);
346e223
diff -Naur cyrus-imapd-2.3.12p2/ptclient/Makefile.in cyrus-imapd-2.3.12p2-autocreate.uncompiled/ptclient/Makefile.in
346e223
--- cyrus-imapd-2.3.12p2/ptclient/Makefile.in	2008-03-24 20:34:22.000000000 +0200
346e223
+++ cyrus-imapd-2.3.12p2-autocreate.uncompiled/ptclient/Makefile.in	2008-05-06 14:48:41.000000000 +0300
d0d307a
@@ -57,10 +57,11 @@
d0d307a
 AFS_LDFLAGS = @AFS_LDFLAGS@ @COM_ERR_LDFLAGS@
d0d307a
 AFS_LIBS = @AFS_LIBS@
d0d307a
 IMAP_LIBS = @IMAP_LIBS@ @LIB_RT@
d0d307a
+SIEVE_LIBS = @SIEVE_LIBS@
d0d307a
 LIBS = $(IMAP_LIBS) @COM_ERR_LIBS@
d0d307a
 LIB_SASL = @LIB_SASL@
d0d307a
 LIB_WRAP = @LIB_WRAP@
d0d307a
-DEPLIBS = ../imap/libimap.a ../lib/libcyrus.a ../lib/libcyrus_min.a @DEPLIBS@
d0d307a
+DEPLIBS = ../imap/libimap.a $(SIEVE_LIBS) ../lib/libcyrus.a ../lib/libcyrus_min.a @DEPLIBS@ 
d0d307a
 UTIL_LIBS = ../imap/mutex_fake.o ../imap/cli_fatal.o
d0d307a
 
d0d307a
 LDAP_LIBS=@LDAP_LIBS@