This documentation is for Dovecot v2.x, see wiki1 for v1.x documentation.
Differences between revisions 26 and 27
Revision 26 as of 2011-05-09 17:15:12
Size: 4309
Editor: TimoSirainen
Comment:
Revision 27 as of 2011-05-31 08:08:55
Size: 4406
Editor: bgs
Comment: note about imap-postlogin user behavior change
Deletions are marked like this. Additions are marked like this.
Line 16: Line 16:
  # for 2.0.13 take care of behavior change http://www.mail-archive.com/dovecot@dovecot.org/msg38350.html
Line 22: Line 23:
Line 28: Line 28:
Line 35: Line 34:
Line 42: Line 42:
{{{
#!/bin/sh
{{{#!/bin/sh
Line 45: Line 44:
export MAIL=maildir:/tmp/test
export USERDB_KEYS="$USERDB_KEYS mail"
exec "$@"
}}}

You can change any Dovecot settings using the above method.
export MAIL=maildir:/tmp/test export USERDB_KEYS="$USERDB_KEYS mail" exec "$@" }}} You can change any Dovecot settings using the above method.
Line 71: Line 65:
Line 81: Line 76:
Line 90: Line 84:
Line 93: Line 86:
Line 105: Line 99:
You can also use
Line 106: Line 101:
You can also use

Post-login scripting

If you want to do something special after authentication, but before beginning the IMAP or POP3 session, you can do this by telling imap/pop3 executable to use post-login service:

service imap {
  # tell imap to do post-login lookup using a socket called "imap-postlogin"
  executable = imap imap-postlogin
}

# The service name below doesn't actually matter.
service imap-postlogin {
  # all post-login scripts are executed via script-login binary
  executable = script-login /usr/local/bin/postlogin.sh

  # the script process runs as the user specified here:
  # for 2.0.13 take care of behavior change http://www.mail-archive.com/dovecot@dovecot.org/msg38350.html
  user = $default_internal_user
  # this UNIX socket listener must use the same name as given to imap executable
  unix_listener imap-postlogin {
  }
}

You can run multiple post-login scripts by just giving multiple scripts as parameters to script-login, for example:

  executable = script-login rawlog /usr/local/bin/postlogin.sh /usr/local/bin/postlogin2.sh

Remember that the post-login script runs with the privileges of the "user" setting given to the service (root by default). If you need the script to access user's mail files, change it to whatever user owns the mails (e.g. vmail). If you're using multiple UNIX UIDs (e.g. system users), use script-login -d to drop to the UID/GID specified by the userdb lookup (ignoring user/group/chroot service settings).

Running environment

Standard input and output file descriptors are redirected to the client's network socket, so you can send data to client by simply writing to stdout. Standard error fd is redirected to Dovecot's error log, you can write errors there as well.

The script can use environment variables:

  • USER: Username
  • IP: Remote IP address
  • LOCAL_IP: Local IP address
  • Fields returned by userdb lookup with their keys uppercased (e.g. if userdb returned home, it's stored in HOME).

It's possible to add/modify userdb fields by adding them to environment and adding the field to USERDB_KEYS. For example to change user's mail location:

{{{#!/bin/sh

export MAIL=maildir:/tmp/test export USERDB_KEYS="$USERDB_KEYS mail" exec "$@" }}} You can change any Dovecot settings using the above method.

Last-login tracking

If you want to know when the user last logged in, you can do it like this:

#!/bin/sh
# a) Filesystem based timestamp in user's home directory
touch ~/.last_login
# b) SQL based tracking. Beware of potential SQL injection holes if you allow
# users to have ' characters in usernames. Following is just an example:
#echo "UPDATE mailbox SET modified = now() WHERE username = '$USER'" | mysql postfixadmin
exec "$@"

Note: if creating a timestamp inside the Maildir itself, it's better to avoid filenames which begin with a dot. The IMAP "list" command will show such files as IMAP folders, unless you also set maildir_stat_dirs = yes which generates more I/O ops.

Custom mailbox location autodetection

See MailLocation for an example.

Alerts

If you want to give the user's client some warning notification, you can do it just by writing it to stdout. But note:

  • Not all clients show the alerts, even though IMAP RFC requires it.
  • IMAP protocol requires CRLF (\r\n) line feeds. Some clients will break if you only send LF.

#!/bin/sh
if [ -f ~/.out-of-office ]; then
  printf "* OK [ALERT] You're still marked as being out of office.\r\n"
fi
exec "$@"

Use UNIX groups for ACL authorization

#!/bin/sh
ACL_GROUPS=`groups $USER | tr ' '  ','`
export ACL_GROUPS
export USERDB_KEYS="$USERDB_KEYS acl_groups"
exec "$@"

Denying connection from some IP/User

You can use the IP and USER shell variables that are setup by dovecot in a bash script in order to deny connection (after a successful login), like this:

if [ "$USER" = "myuser" ] ; then
  printf "* NO [ALERT] The user '$USER' can not login\r\n"
  exit 0
fi

if [ ! "$IP" = "192.168.1.1" ] ; then
  printf "* NO [ALERT] Access not allowed from the Internet\r\n"
  exit 0
fi
exec "$@"

You can also use

None: PostLoginScripting (last edited 2022-02-04 22:55:54 by TimoSirainen)