dovecot – out of the box setup

Trying to learn a new programming language is a daunting task. My process is to take programs that I’ve written before and rewrite them in the target language of choice.

I’ve got a PHP program that interacts with my IMAP server (smart-imap), reads all the messages in the Inbox, takes the localpart of the email address, creates a folder and moves that email into that folder. If the localpart is part of a pre-determined list of email addresses that have been sold off for Spam it’ll move it to the SPAM folder. If there are no localparts that match the domain name of the server then it drops it into an UNSORTED folder.

Today’s language of choice is Python, specifically 3.9.21. To do this entire feat without breaking things I need an IMAP server, and dovecot 2.3.16 is going to be the death of me.

Never go in head first…

Out of the box it’s set up for all necessary services, PAM authentication, and should be good to go©.

I create a system user called imaptest with a curiously identical password on a Rocky 5.14.0-503 VM. Install dovecot, and launch the service. All goes well. I telnet into localhost:143, authenticated, and then immediately got dropped out. /var/log/maillog screams the following at me:

Jan 16 11:02:25 rocks dovecot[859]: imap(imaptest)<1368><pVmW69QrMJzAqAHf>: Error: Namespace '': Mail storage autodetection failed with home=/home/imaptest
Jan 16 11:02:25 rocks dovecot[859]: imap(imaptest)<1368><pVmW69QrMJzAqAHf>: Disconnected: Namespace '': Mail storage autodetection failed with home=/home/imaptest in=0 out=416 deleted=0 expunged=0 trashed=0 hdr_count=0 hdr_bytes=0 body_count=0 body_bytes=0

Searching gets me to the first derp: RTFM aka “Configuration”. I have to define a mail_location in the configuration for dovecot to know where to do its business. editing /etc/dovecot/conf/10-mail.conf, setting mail_location to mbox:~/mail:INBOX=/var/mail/%u and restarting dovecot gets me authenticating. I’m happy. Lets see if other commands will work.

Lets take a look around?

After authentication I attempt to select inbox, a basic next step in IMAP folder enumerating. This give me a wonky permission error:

NO [NOPERM] Permission denied (0.001 + 0.000 secs).

and the logs give me a good breadcrumb to follow:

Jan 16 12:48:10 rocks dovecot[1504]: imap(imaptest)<1569><BmqGZdYrhtwAAAAAAAAAAAAAAAAAAAAB>: Error: fchown(/home/imaptest/mail, group=12(mail)) failed: Operation not permitted (egid=1001(imaptest), group based on /var/mail/imaptest - see http://wiki2.dovecot.org/Errors/ChgrpNoPerm)

The wiki for ChgrpNoPerm tells me dovecot needs to copy files around and maintain groups and perms and it’s having problems doing all that. To fix this I chose to do the following as I felt it was the easiest.

  1. Add dovecot to the mail group
  2. Add imapuser to the mail group
  3. Change /home/imapuser to 0770 and to imaptest:mail
  4. Restart dovecot
  5. profit

I chose the mail group since /var/mail/imaptest was set to imaptest:mail, and “in keeping with permissions” made sense to spread the love around. Following all this my select inbox command happily returned data.

1 select inbox
* FLAGS (\Answered \Flagged \Deleted \Seen \Draft)
* OK [PERMANENTFLAGS (\Answered \Flagged \Deleted \Seen \Draft \*)] Flags permitted.
* 0 EXISTS
* 0 RECENT
* OK [UIDVALIDITY 1737050040] UIDs valid
* OK [UIDNEXT 1] Predicted next UID
1 OK [READ-WRITE] Select completed (0.001 + 0.000 + 0.001 secs).

Now to see if I can learn this 33+ year old language

Leave a Reply