[Dovecot] Auto create folder(s)

Bill Landry bill at inetmsg.com
Mon Oct 1 07:26:12 EEST 2007


mouss wrote the following on 9/30/2007 3:45 PM -0800:
> Fábio M. Catunda wrote:
>   
>> Charles Marcus escreveu:
>>     
>>> On 9/26/2007, Bill Landry (bill at inetmsg.com) wrote:
>>>       
>>>>> But with maildrop you still run the process of creating/checking on
>>>>> every delivery, same thing as dovecot, right?
>>>>>           
>>>> Correct. If the folder does not exist, maildrop will create it on
>>>> first delivery. If the end user deletes the folder, maildrop will
>>>> recreate the folder on the next message delivery to that account.
>>>>         
>>> Could it could be written to simply create the folder if the target
>>> folder doesn't exist (ie, if the initial save fails due to
>>> non-existent folder)? Then there would be no performance hit...
>>>
>>>       
>> There will be a performance hit couse you have to check if the folder
>> exists or not.
>>     
>
> Charles did not say you need to check if the folder exists or not.
>
>   
>> With maildrop its pretty easy to do that, but you will have an extra
>> access to your HD on every message delivery.
>> Maildrop can run shell commands it the user that runs it have a valid
>> shell (Debian-exim do NOT have a valid shell, I created another user
>> just to make the delivery), so, you can run something like
>>
>> /^Envelope-to:.*/
>> getaddr($MATCH) =~ /^.*/;
>> DEST = $MATCH
>> USER = `/bin/echo $DEST | /usr/bin/cut -f1 -d'@'`
>> DOMAIN = `/bin/echo $DEST | /usr/bin/cut -f2 -d'@'`
>>
>> DOEXIST=`[ -d /var/mail/$DOMAIN/$USER/Maildir ]; echo $?`
>>
>> if ($DOEXIST == 1){
>>    <RUN SOMETHING HERE TO CREATE MAILDIRS>
>> }
>>
>> Thats it!
>>
>>     
>
> There's a pattern here. instead of
>
> 	if (file|dir) (exists|is writable) ... write to ...
>
> use
>
> 	write to ...
> 	if (error)  handle it
>
> This has multiple benefits:
> - optimized for the common case
> - less vulnerable to race conditions (things happen between the time you
> check and the time you do).
> - if the file/dir exists/writable, no check to do.
>
> with maildrop, this is done with exceptions (otherwise trying to deliver
> would result in an error reported to the MTA).
>
> # try writing
> exception {
> 	TO $folder
> }
> # failed. maybe folder doesn't exist. try creating it
> # add whatever checks or actins inside this...
> `$maildirmake $folder`
> # try writing again
> TO $folder
>
>
> you can adapat this to delivering spam to the Inbox if the user has
> deleted his Junk folder (a method to opt-out).
>   
Mouss, your post got me thinking, and after a bit of struggle and
testing, I was finally able to come up with a working maildroprc
configuration that would first attempt message delivery and then
fall-back to creating the maildir as an exception if the maildir did not
exist.  The issue I was having was that maildrop was creating mbox files
if the maildir directory structure was not already existing before
sending a message to a new account.  I finally found a hint in an e-mail
exchange on the Courier maildrop list:

http://osdir.com/ml/mail.maildrop/2004-10/msg00031.html

The trick was to terminate the path with a forward slash "/".  I know
this trailing "/" is a maildir standard, but even a follow up review of
the maildrop "maildropfilter" documentation didn't reveal this little
tidbit.  There was just one reference to maildir format vs. "file"
(mbox) format in the official maildrop documentation:

http://www.courier-mta.org/maildrop/maildropfilter.html
"If /|expression|/ is a directory, *maildrop* assumes that the directory
is a maildir directory. Otherwise, *maildrop* will deliver the message
to a file, formatted in traditional mailbox format. *maildrop* will use
either dot-locking, or flock()-locking when delivering the message to
the file."

However, is the maildir directory structure does not exist, then
maildrop creates an mbox file.  With the trailing forward slash,
maildrop will then assume maildir, and if the directory does not exist,
the error condition will trigger and the exception to create the maildir
will be followed.

Hope this help someone else who has been trying to figure this out.  And
thanks for the hint, Mouss!

Bill


More information about the dovecot mailing list