1 (edited by alex.w 2017-05-11 21:51:47)

Topic: Implementing Dovecot PostLoginScripting

======== Required information ====
- iRedMail version (check /etc/iredmail-release): 0.9.5-1
- Linux/BSD distribution name and version: Ubuntu 14.04 x64
- Store mail accounts in which backend (LDAP/MySQL/PGSQL): MySQL
- Web server (Apache or Nginx): Nginx
- Manage mail accounts with iRedAdmin-Pro? no
- Related log if you're reporting an issue:
====
Hi Zhang,
I need to record lastlogindate in vmail database on mailbox table.
I use reference from here https://wiki.dovecot.org/PostLoginScripting
My actual implementation is:

/usr/local/bin$ ls -l postlogin.sh
-rwxr-x--- 1 vmail vmail 401 May 11 19:50 postlogin.sh
/usr/local/bin$ sudo cat postlogin.sh
#!/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 lastlogindate=now(),lastloginipv4='$IP' WHERE username = '$USER'" | mysql vmail -uvmailadmin -pvmailadminpassword
exec "$@"

My changes on  /etc/dovecot/conf.d/10-master.conf

...
service lmtp {
  unix_listener lmtp {
    #mode = 0666
  }

  # Create inet listener only if you can't use the above UNIX socket
  #inet_listener lmtp {
    # Avoid making LMTP visible for the entire internet
    #address =
    #port =
  #}
  executable = lmtp custom-postlogin
}

service imap {
  # Most of the memory goes to mmap()ing files. You may need to increase this
  # limit if you have huge mailboxes.
  #vsz_limit = $default_vsz_limit

  # Max. number of IMAP processes (connections)
  #process_limit = 1024
  executable = imap custom-postlogin
}

service pop3 {
  # Max. number of POP3 processes (connections)
  #process_limit = 1024
  executable = pop3 custom-postlogin
}

service custom-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 (v2.0.14+):
  user = vmail
  # this UNIX socket listener must use the same name as given to imap executable
  unix_listener custom-postlogin {
  }
}

...

Then I restart dovecot using: sudo doveadm reload
But nothing get updated to mailbox table.
And I don't know which log file I need to check for the script-login execution status.
I knew my postlogin.sh is working when I executed it manually from console.
Can you help me with this problem? Thanks.

----

Spider Email Archiver: On-Premises, lightweight email archiving software developed by iRedMail team. Supports Amazon S3 compatible storage and custom branding.

2

Re: Implementing Dovecot PostLoginScripting

The wiki tutorial you're using is for Dovecot-1.x, please use this one:
https://wiki2.dovecot.org/PostLoginScripting

If you have Dovecot-2.2.14+, try its plugin:
https://wiki2.dovecot.org/Plugins/LastLogin

alex.w wrote:

My changes on  /etc/dovecot/conf.d/10-master.conf

iRedMail uses only /etc/dovecot/dovecot.conf.

3

Re: Implementing Dovecot PostLoginScripting

Zhang,
A cup of coffee served on your table.
I moved my PostLoginScript to /etc/dovecot/dovecot.conf

service pop3 {
  executable = pop3 pop3-postlogin
}
service pop3-postlogin {
  # all post-login scripts are executed via script-login binary
  executable = script-login /usr/local/bin/pop3-postlogin.sh

  # the script process runs as the user specified here (v2.0.14+):
  user = vmail
  # this UNIX socket listener must use the same name as given to imap executable
  unix_listener pop3-postlogin {
  }
}

And changes are start flowing to my mailbox table. But I still have a little problem.
My code below is not recording correct last login IPv4 correctly

#!/bin/sh
echo "UPDATE mailbox SET lastlogindate=now(),lastloginipv4='$IP' WHERE username = '$USER'" | mysql vmail -uvmailadmin -pvmailadminpassword
exec "$@"

Here's my lastloginipv4 example

mysql> select username,lastlogindate,lastloginipv4,lastloginprotocol from mailbox order by lastlogindate desc limit 1;
+-------------------------+---------------------+---------------+-------------------+
| username                | lastlogindate       | lastloginipv4 | lastloginprotocol |
+-------------------------+---------------------+---------------+-------------------+
| somebody@example.com    | 2017-05-13 23:48:26 |        115178 | POP3              |
+-------------------------+---------------------+---------------+-------------------+
1 row in set (0.00 sec)

Can you show me what's wrong with my lastloginipv4?
Thanks.

4

Re: Implementing Dovecot PostLoginScripting

alex.w wrote:

A cup of coffee served on your table.

Thank you. smile

Try this SQL command:

USE vmail;
SELECT INET_NTOA(lastloginipv4) FROM mailbox LIMIT 1;

5

Re: Implementing Dovecot PostLoginScripting

Hi Zhang,
Thank you now it works correctly. I've changed my bash script as below:

#!/bin/sh
echo "UPDATE mailbox SET lastlogindate=now(),lastloginipv4=INET_ATON('$IP') WHERE username = '$USER'" | mysql vmail -uvmailadmin -pvmailadminpassword
exec "$@"