CentOS

2006/2/08 08:38 AM 更新

Courier-IMAP

自宅のSMTPサーバ宛に送られたメールを読むためにPOP3サーバとIMAPサーバを導入する。
普段はクライアントPCにPOP3でメールを取り込だけだがWebメール用としてIMAPサーバも必要。

今のCourier-IMAPのバージョンはサーバ部分と認証部分が別になっておりCourier-IMAPをインストールする前にCourier Authentication Libraryをインストールする必要がある。

Courierを起動する専用ユーザを作成する。
# /usr/sbin/groupadd -g 20012 courier
# /usr/sbin/useradd -u 20012 -g courier -s /sbin/nologin -d /dev/null courier
Courierの公式サイトより最新のソースファイルを入手しインストールする。
$ wget http://www.courier-mta.org/beta/courier-authlib/courier-authlib-0.56.20050702.tar.bz2
$ tar xvjf courier-authlib-0.56.20050702.tar.bz2
$ cd courier-authlib-0.56.20050702
$ ./configure --without-authldap \
                     --without-authvchkpw \
                     --without-authpgsql \
                     --without-authmysql \
                     --without-authcustom \
                     --without-authpipe \
                     --with-mailuser=courier \
                     --with-mailgroup=courier \
                     --with-redhat
$ make
# make install
# make install-configure
PAMの設定を行う。
  /etc/pam.d/pop3
#%PAM-1.0
auth         required     pam_nologin.so
auth         required     pam_stack.so service=system-auth
account    required     pam_stack.so service=system-auth
session    required     pam_stack.so service=system-auth
  /etc/pam.d/imap
#%PAM-1.0
auth         required     pam_nologin.so
auth         required     pam_stack.so service=system-auth
account    required     pam_stack.so service=system-auth
session    required     pam_stack.so service=system-auth
Courier Authentication Libraryの設定ファイルを編集。
  /usr/local/etc/authlib/authdaemonrc
##NAME: authmodulelist:2
#
# The authentication modules that are linked into authdaemond. The
# default list is installed. You may selectively disable modules simply
# by removing them from the following list. The available modules you
# can use are:

authmodulelist="authuserdb authpwd authshadow"


##NAME: authmodulelistorig:3
#
# This setting is used by Courier's webadmin module, and should be left
# alone

authmodulelistorig="authuserdb authpwd authshadow"
起動スクリプトを作成する。
  /etc/rc.d/init.d/authdamond
#!/bin/sh
# authdaemond   This shell script takes care of starting and stopping
#               authdaemond on RedHat or other chkconfig-based system.
#
# chkconfig: 2345 87 78
#
# description: courier-authlib is courier-imap authentication daemon
#
# processname: authdaemond
#

#
# configuration
#

INIT_PROG=authdaemond

#
# Source Redhat function library.
#
. /etc/rc.d/init.d/functions

# Tack on path to authdaemond if not already in PATH
COURIER_PATH=":/usr/local/sbin"

PATH=$PATH$COURIER_PATH
export PATH

RETVAL=0

umask 077
ulimit -c 0

set -a
. /usr/local/etc/authlib/authdaemonrc

# See how we were called.
start() {
        echo -n "Starting $INIT_PROG: "
        daemon /usr/local/sbin/courierlogger -pid=/var/run/authdaemon.pid -start /usr/local/libexec/courier-authlib/authdaemond
        RETVAL=$?
        echo

        [ $RETVAL -eq 0 ] && touch "/var/lock/subsys/${INIT_PROG}"
        return $RETVAL
}

stop() {
        # Same here concerning klogd. Uncomment the following block of
        # code if you are needing to run it

        echo -n "Stopping $INIT_PROG: "
        killproc authdaemond
        RETVAL=$?
        echo

        [ $RETVAL -eq 0 ] && rm -f "/var/lock/subsys/${INIT_PROG}"
        return $RETVAL

}

case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  status)
        status $INIT_PROG
        ;;
  restart)
        stop
        start
        ;;
  *)
        echo $"Usage: $0 {start|stop|status|restart}"
        exit 1
esac

exit $?
起動スクリプトを登録する。
# /sbin/chkconfig --add authdaemond
# /sbin/chkconfig --list |grep authdaemond
authdaemond     0:オフ  1:オフ  2:オン  3:オン  4:オン  5:オン  6:オフ
authdamonを起動する。
# /usr/local/sbin/authdaemond start
# ps -aux |grep auth
root      9356  0.0  0.1  1516  472 ?        S    10:50   0:00 /usr/local/sbin/courierlogger -pid=/usr/local/var/spool/authdaemon/pid -start /usr/local/libexec/courier-authlib/authdaemond
root      9357  1.0  0.2  1892  616 ?        S    10:50   0:00 /usr/local/libexec/courier-authlib/authdaemond
root      9358  0.5  0.2  1892  616 ?        S    10:50   0:00 /usr/local/libexec/courier-authlib/authdaemond
root      9359  0.0  0.2  1892  616 ?        S    10:50   0:00 /usr/local/libexec/courier-authlib/authdaemond
root      9360  0.0  0.2  1892  616 ?        S    10:50   0:00 /usr/local/libexec/courier-authlib/authdaemond
root      9361  0.0  0.2  1892  616 ?        S    10:50   0:00 /usr/local/libexec/courier-authlib/authdaemond
root      9362  0.0  0.2  1892  616 ?        S    10:50   0:00 /usr/local/libexec/courier-authlib/authdaemond
サーバ稼働時のログ。
# tail -f /var/log/messages
Jul 9 12:15:48 boris authdaemond: courierlogger起動 succeeded
Jul 9 12:16:32 boris authdaemond: courierlogger停止 succeeded

# tail /var/log/maillog
Jul 9 12:15:48 boris authdaemond: modules="authuserdb authpwd authshadow", daemons=5
Jul 9 12:15:48 boris authdaemond: Installing libauthuserdb
Jul 9 12:15:48 boris authdaemond: Installation complete: authuserdb
Jul 9 12:15:48 boris authdaemond: Installing libauthpwd
Jul 9 12:15:48 boris authdaemond: Installation complete: authpwd
Jul 9 12:15:48 boris authdaemond: Installing libauthshadow
Jul 9 12:15:48 boris authdaemond: Installation complete: authshadow
Jul 9 12:16:32 boris authdaemond: stopping authdaemond children
次にCourier-IMAPをインストールする。
$ wget http://www.courier-mta.org/beta/imap/courier-imap-4.0.3.20050702.tar.bz2
$ tar xvjf courier-imap-4.0.3.20050702.tar.bz2
$ cd courier-imap-4.0.3.20050702
$ ./configure --without-ipv6 --with-redhat
$ make
# make install
# make install-configure
起動スクリプトを作成する。
  /etc/rc.d/init.d/courier-pop3d
#!/bin/sh
# courier       This shell script takes care of starting and stopping
#               authdaemond on RedHat or other chkconfig-based system.
#
# chkconfig: 2345 89 80
#
# description: courier-pop3d is pop3 daemon
#
# processname: courier-pop3d
#

#
# configuration
#

INIT_PROG=pop3d

#
# Source Redhat function library.
#
. /etc/rc.d/init.d/functions

# Tack on path to authdaemond if not already in PATH
COURIER_PATH=":/usr/lib/courier-imap"

PATH=$PATH$COURIER_PATH
export PATH

RETVAL=0

umask 077
ulimit -c 0

set -a
. /usr/lib/courier-imap/etc/pop3d

# See how we were called.
start() {
        echo -n "Starting $INIT_PROG: "
        daemon /usr/local/sbin/courierlogger -pid=$PIDFILE -start $LOGGEROPTS \
               /usr/lib/courier-imap/libexec/couriertcpd -address=$ADDRESS \
               -maxprocs=$MAXDAEMONS -maxperip=$MAXPERIP $TCPDOPTS \
               $PORT /usr/lib/courier-imap/sbin/pop3login \
               /usr/lib/courier-imap/bin/pop3d ${MAILDIRPATH}
        RETVAL=$?
        echo

        [ $RETVAL -eq 0 ] && touch "/var/lock/subsys/${INIT_PROG}"
        return $RETVAL
}

stop() {
        # Same here concerning klogd. Uncomment the following block of
        # code if you are needing to run it

        echo -n "Stopping $INIT_PROG: "
        /usr/local/sbin/courierlogger -pid=$PIDFILE -stop
        RETVAL=$?
        echo

        [ $RETVAL -eq 0 ] && rm -f "/var/lock/subsys/${INIT_PROG}"
        return $RETVAL

}

case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  restart)
        stop
        start
        ;;
  *)
        echo $"Usage: $0 {start|stop|restart}"
        exit 1
esac

exit $?
  /etc/rc.d/init.d/courier-imapd
#!/bin/sh
# courier       This shell script takes care of starting and stopping
#               authdaemond on RedHat or other chkconfig-based system.
#
# chkconfig: 2345 89 80
#
# description: courier-imapd is imap daemon
#
# processname: courier-imapd
#

#
# configuration
#

INIT_PROG=imapd

#
# Source Redhat function library.
#
. /etc/rc.d/init.d/functions

# Tack on path to authdaemond if not already in PATH
COURIER_PATH=":/usr/lib/courier-imap"

PATH=$PATH$COURIER_PATH
export PATH

RETVAL=0

umask 077
ulimit -c 0

set -a
. /usr/lib/courier-imap/etc/imapd

# See how we were called.
start() {
        echo -n "Starting $INIT_PROG: "
        daemon /usr/local/sbin/courierlogger -pid=$PIDFILE -start $LOGGEROPTS \
               /usr/lib/courier-imap/libexec/couriertcpd -address=$ADDRESS \
               -maxprocs=$MAXDAEMONS -maxperip=$MAXPERIP $TCPDOPTS \
               $PORT /usr/lib/courier-imap/sbin/imaplogin \
               /usr/lib/courier-imap/bin/imapd ${MAILDIRPATH}
        RETVAL=$?
        echo

        [ $RETVAL -eq 0 ] && touch "/var/lock/subsys/${INIT_PROG}"
        return $RETVAL
}

stop() {
        # Same here concerning klogd. Uncomment the following block of
        # code if you are needing to run it

        echo -n "Stopping $INIT_PROG: "
        /usr/local/sbin/courierlogger -pid=$PIDFILE -stop
        RETVAL=$?
        echo

        [ $RETVAL -eq 0 ] && rm -f "/var/lock/subsys/${INIT_PROG}"
        return $RETVAL

}

case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  restart)
        stop
        start
        ;;
  *)
        echo $"Usage: $0 {start|stop|restart}"
        exit 1
esac

exit $?
起動スクリプトを登録する。
# /sbin/chkconfig --add courier-pop3d
# /sbin/chkconfig --add courier-imapd
# /sbin/chkconfig --list |grep courier
courier-imapd   0:オフ  1:オフ  2:オン  3:オン  4:オン  5:オン  6:オフ
courier-pop3d   0:オフ  1:オフ  2:オン  3:オン  4:オン  5:オン  6:オフ
pop3dとimapdを起動する。
○pop3d
# /usr/lib/courier-imap/libexec/pop3d.rc start

# ps -aux |grep pop
root     14752  0.0  0.1  1524  384 ?        S    23:14   0:00 /usr/local/sbin/courierlogger -pid=/var/run/pop3d.pid -start -name=pop3d /usr/lib/courier-imap/libexec/couriertcpd -address=0 -maxprocs=40 -maxperip=4 -nodnslookup -noidentlookup 110 /usr/lib/courier-imap/sbin/pop3login /usr/lib/courier-imap/bin/pop3d Maild
root     14753  0.0  0.1  1620  484 ?        S    23:14   0:00 /usr/lib/courier-imap/libexec/couriertcpd -address=0 -maxprocs=40 -maxperip=4 -nodnslookup -noidentlookup 110 /usr/lib/courier-imap/sbin/pop3login /usr/lib/courier-imap/bin/pop3d Maildir

# netstat -an |grep 110
tcp        0      0 0.0.0.0:110                 0.0.0.0:*                   LISTEN

○imapd
# /usr/lib/courier-imap/libexec/imapd.rc start

# ps -axu |grep imap
root      8659  0.0  0.1  1512  388 ?        S    21:41   0:00 /usr/local/sbin/courierlogger -pid=/var/run/imapd.pid -start -name=imapd /usr/lib/courier-imap/libexec/couriertcpd -address=0 -maxprocs=40 -maxperip=4 -nodnslookup -noidentlookup 143 /usr/lib/courier-imap/sbin/imaplogin /usr/lib/courier-imap/bin/imapd Maild
root      8660  0.0  0.1  1628  476 ?        S    21:41   0:00 /usr/lib/courier-imap/libexec/couriertcpd -address=0 -maxprocs=40 -maxperip=4 -nodnslookup -noidentlookup 143 /usr/lib/courier-imap/sbin/imaplogin /usr/lib/courier-imap/bin/imapd Maildir

# netstat -an |grep 143
tcp        0      0 0.0.0.0:143                 0.0.0.0:*                   LISTEN
動作確認をする。
○POP3
# telnet 192.168.1.xx 110
Trying 192.168.1.xx...
Connected to postman.kajukaju.net (192.168.1.xx).
Escape character is '^]'.
+OK Hello there.
user username
+OK Password required.
pass xxxxxxxx
+OK logged in.
stat
+OK 3 1266
list
+OK POP3 clients that break here, they violate STD53.
1 242
2 281
3 743
.
quit
+OK Bye-bye.
Connection closed by foreign host.

○IMAP
# telnet 192.168.1.xx 143
Trying 192.168.1.xx...
Connected to postman.kajukaju.net (192.168.1.xx).
Escape character is '^]'.
* OK [CAPABILITY IMAP4rev1 UIDPLUS CHILDREN NAMESPACE THREAD=ORDEREDSUBJECT THREAD=REFERENCES SORT QUOTA IDLE ACL ACL2=UNION] Courier-IMAP ready. Copyright 1998-2005 Double Precision, Inc. See COPYING for distribution information.
001 LOGIN username xxxxxxxx
001 OK LOGIN Ok.
002 LOGOUT
* BYE Courier-IMAP server shutting down
002 OK LOGOUT completed
Connection closed by foreign host.
サーバ稼働時のログ
# tail -f /var/log/messages
Jul 9 21:41:32 boris courier-pop3d: courierlogger起動 succeeded
Jul 9 21:41:45 boris courier-imapd: courierlogger起動 succeeded

# tail /var/log/maillog
Jul 9 12:27:02 boris pop3d: Connection, ip=[192.168.1.xx]
Jul 9 12:27:13 boris pop3d: LOGIN, user=username, ip=[192.168.1.xx]
Jul 9 12:27:23 boris pop3d: LOGOUT, user=username, ip=[192.168.1.xx], top=0, retr=0, time=10
Jul 9 21:48:50 boris imapd: Connection, ip=[192.168.1.xx]
Jul 9 21:49:02 boris imapd: LOGIN, user=username, ip=[192.168.1.xx], protocol=IMAP
Jul 9 21:50:43 boris imapd: LOGOUT, user=username, ip=[192.168.1.xx], headers=0, body=0, time=101