Linux – Dovecot Virtual Users and Users Domain Mapping

dovecotemaillinux

I have successfully compiled, configured and ran Dovecot with virtual users feature.

Here's part of my /etc/dovecot.conf configuration file:

mail_location = maildir:/home/%d/%n/Maildir

auth default {
    mechanisms = plain login

    userdb passwd-file {
        args = /home/%d/etc/passwd
    }
    passdb passwd-file {
        args = /home/%d/etc/shadow
    }

    socket listen {
        master {
            path = /var/run/dovecot/auth-worker
            mode = 0600
        }
    }
}

I faced one issue I can't resolve myself. Is there anyway to create users' domains mapping and provide username in mail_location?

Examples:
1. currently I have /home/domain.com/user/Maildir
2. I'd like to have /home/USER/domain.com/user/Maildir

Can I achieve this somehow?

Greets,
Stojko

Best Answer

Consider what you are asking for a moment. You want to include the user's home directory in the mail_location. On the surface it seems like a reasonable request - but you must ask - how does Dovecot know the user's home directory?

System users are stored in /etc/passwd - that file contains

username:password:uid:gid:comment:home:shell

(Note: instead of a password, you may have x with the password stored in /etc/shadow; or * to deactivate the account; comment is actually a 'gecos field')

If Dovecot reads this file - it is provided with the user's home directory - once it has that information, it can use it elsewhere (i.e. it will be able to use '~/to signify the home directory, and will set%h` to the same).

If, however, you are using virtual users - instead of system users - one of the fundamental ideas is that not every virtual user has to be a system user. In this case, Dovecot has no way of knowing if a virtual user that shares the same name as a system user is really the same user or not (it also cannot use the UID, since it is possible to have multiple virtual users map to the same UID). It will not assume anything beyond what is provided in your userdb.

A Userdb can provide the following information to Dovecot:

  • uid: User's UID (UNIX user ID), overrides the global mail_uid setting.
  • gid: User's GID (UNIX group ID), overrides the global mail_gid setting.
  • home: User's home directory, overrides the global mail_home setting. Although not required, it's highly recommended even for virtual users.
  • mail: Mail location, overrides the global mail_location setting.
  • Optional extra fields which are passed to the IMAP/POP3 process in environment variables.

In your configuration, you are using a passwd-file - that is, a file with the same format as the system passwd file, but which you have setup for this purpose (i.e. it is not the system passwd file). Dovecot accepts the following passwd-file format (and ignores the gecos and shell fields):

user:password:uid:gid:(gecos):home:(shell):extra_fields

You can override certain defaults, on a per user basis, by specifying (space delimited) key=value extra fields (with keys prefixed with userdb_ in most cases). For instance, userdb_mail=Maildir:~/mail will override mail_location.

Just to reiterate, by using virtual users (and not using the system passwd file), you have told Dovecot that these users might not have any relation to system users, and so it is only going to use the information you provide it, without assuming anything else.

Finally, to try and actually answer your question:

  • You can use several variables in mail_location - these are defined on Dovecot's Variables page.
  • The variable %h or, preferably ~/ can be used to return the home directory that Dovecot retrieved from its UserDB query (and sets mail_home)
  • You should specify a home directory for all users in your UserDB
  • You can override mail_location by returning userdb_mail from your userdb query.

So, basically, set home in your userdb if you want to use the home directory in Dovecot, because it can't know what you don't tell it.

Related Topic