#!/bin/sh
#
# qvcs-install.sh
# ----------------
# This script modifies some config files right after the installation
# of various qvcs-related RPMs.
#
# Copyright (c) 2001-2002 by Konstantin Riabitsev <icon@duke.edu>
# Licensed under the GNU GPL. For full terms see http://www.gnu.org/
#
# $Id: qvcs-install,v 1.5 2004/07/30 00:44:08 graf25 Exp $
# 
# @author Konstantin Riabitsev <icon@duke.edu> ($Author: graf25 $)
# @version $Date: 2004/07/30 00:44:08 $
#

##
# Asks a question.
#
# @return void
#
inquire(){
    PROCEED="exit";
    echo -n "Should I make the changes? [y]/n/q: "
    read REPLY
    if [ -z $REPLY ]; then REPLY="y"; fi
    if [ $REPLY = "y" ]; then 
	PROCEED=y
    fi
    if [ $REPLY = "n" ]; then
	PROCEED=n
    fi
    if [ $PROCEED = "exit" ]; then
	echo "Exiting."
	exit 0
    fi
}

##
# Backs up the file profived as $1, and copies it into 
# $BAKDIR.
#
# @param  $1 the file to backup.
# @return void
#
bakfile(){
    BAKFILE=`echo $1 | sed "s|/|_|g"`
    cp $1 $BAKDIR/$BAKFILE
    echo "Backup file saved in $BAKDIR/$BAKFILE"
}

##
# Enter to proceed
#
# @return void
#
goon(){
    echo
    echo -n "-- [Enter] to proceed --"
    read
}

##
# Make sure we're root
#
if [ "$UID" != "0" ]; then
    echo "Error: Must be root"
    exit 1
fi

clear
cat <<EOF
Qvcs-install
-------------
This script will change the configurations of Apache, Courier-Imap, and
Squirrelmail to make them ready for the QVCS system. Please run this
script ONLY RIGHT AFTER you have installed the RPM packages. Do not run
this script for any other purposes or any other systems besides Red Hat
Linux 9.

The backup config files will be saved in /var/lib/qvcs in case you want
to restore them at some point in the future.

EOF
inquire
if [ $PROCEED = "n" ]; then
    echo "Exiting."
    exit 0
fi

BAKDIR="/var/lib/qvcs"
mkdir -p $BAKDIR

clear
cat <<EOF
TCP wrappers configs
---------------
I will modify tcp wrappers so you can relay mail on localhost. I will
change /etc/hosts.allow and add these entries:
tcp-env: 127.0.0.1 : setenv RELAYCLIENT
tcp-env: ALL

EOF
inquire
if [ $PROCEED = "y" ]; then
    echo "Making configuration changes to tcp wrappers."
    HALLOW="/etc/hosts.allow"
    bakfile $HALLOW
    egrep -v "^tcp-env" $HALLOW > $HALLOW.new
    echo "tcp-env: 127.0.0.1 : setenv RELAYCLIENT" >> $HALLOW.new
    echo "tcp-env: ALL" >> $HALLOW.new
    mv -f $HALLOW.new $HALLOW
    goon
fi

clear
cat <<EOF
Apache Configs
---------------
I will modify the following entries in your Apache config file:
1) Change document root from /var/www/html to /usr/share/squirrelmail

EOF
inquire
if [ $PROCEED = "y" ]; then
    echo "Making configuration changes in Apache config."
    APACHE_CONF="/etc/httpd/conf/httpd.conf"
    SSL_CONF="/etc/httpd/conf.d/ssl.conf"
    bakfile $APACHE_CONF
    perl -pi -e "s|/var/www/html|/usr/share/squirrelmail|g" $APACHE_CONF
    bakfile $SSL_CONF
    perl -pi -e "s|/var/www/html|/usr/share/squirrelmail|g" $SSL_CONF
    goon
fi

clear
cat <<EOF
Courier-Imap configs
---------------------
I will modify the courier-imap configuration files. I will make the 
following changes:
imapd:
1) Change MAXPERIP setting to 40
2) Change AUTHMODULES to "authvmailmgr authdaemon"
pop3d:
1) Change AUTHMODULES to "authvmailmgr authdaemon"

EOF
inquire
if [ $PROCEED = "y" ]; then
    echo "Making configuration changes in Courier-Imap configs."
    IMAPD="/etc/courier-imap/imapd"
    POP3D="/etc/courier-imap/pop3d"
    bakfile $IMAPD
    bakfile $POP3D
    sed 's|MAXPERIP=[[:digit:]]*|MAXPERIP=40|g;
	 s|AUTHMODULES=.*$|AUTHMODULES="authvmailmgr authdaemon"|g' \
	$IMAPD > $IMAPD.new
    mv -f $IMAPD.new $IMAPD
    sed 's|AUTHMODULES=.*$|AUTHMODULES="authvmailmgr authdaemon"|g' \
	$POP3D > $POP3D.new
    mv -f $POP3D.new $POP3D
    goon
fi

clear
cat <<EOF
Squirrelmail Configs
---------------------
I will do the following in squirrelmail configs:
1) Set it to use smtp for sending mail.
2) Configure it for courier.
3) Add vadmin plugin.

EOF
inquire
if [ $PROCEED = "y" ]; then
    echo "Making Squirrelmail config changes."
    SQCONF="/etc/squirrelmail/config_local.php"
    bakfile $SQCONF
    cat <<EOF > $SQCONF
<?php
##
# Configuration written by qvcs-install
#
\$useSendmail = false;
\$imap_server_type = 'courier';
\$optional_delimiter = '.';
\$default_folder_prefix = 'INBOX.';
\$show_prefix_option = false;
\$show_contain_subfolders_option = false;
\$delete_folder = true;
\$plugins[] = 'vadmin';
?>
EOF
    goon
fi

clear
cat <<EOF
IPchains Config
----------------
I will now configure your firewall. I will allow the following ports:
 22:tcp (ssh)
 25:tcp (smtp)
 80:tcp (http)
110:tcp (pop3)
113:tcp (auth)
143:tcp (imap)
443:tcp (https)
465:tcp (smtps)
993:tcp (imaps)
995:tcp (pop3s)

You can change these settings later using "lokkit".

EOF
inquire
if [ $PROCEED = "y" ]; then
    echo "Making firewall changes."
    IPCHAINS="/etc/sysconfig/ipchains"
    if [ -e $IPCHAINS ]; then
	bakfile $IPCHAINS
    fi
    /usr/sbin/lokkit -q --high --dhcp \
	    --port=ssh:tcp \
	    --port=smtp:tcp \
	    --port=http:tcp \
	    --port=pop3:tcp \
	    --port=auth:tcp \
	    --port=imap:tcp \
	    --port=https:tcp \
	    --port=smtps:tcp \
	    --port=imaps:tcp \
	    --port=pop3s:tcp
    goon
fi

clear
cat <<EOF
Services
---------
I will now do the following:
1) Enable these services:
    courier-imap
    httpd
    vmailmgrd
    portmap
2) Disable these services:
    gpm
    rawdevices
    netfs
    apmd
    isdn
    pcmcia
    autofs
    nfslock

You can configure services later by running "ntsysv."

EOF
inquire
if [ $PROCEED = "y" ]; then
    echo "Disabling services."
    /sbin/chkconfig gpm off
    /sbin/chkconfig rawdevices off
    /sbin/chkconfig netfs off
    /sbin/chkconfig apmd off
    /sbin/chkconfig isdn off
    /sbin/chkconfig pcmcia off
    /sbin/chkconfig autofs off
    /sbin/chkconfig portmap on
    /sbin/chkconfig nfslock off
    echo "Enabling services."
    /sbin/chkconfig courier-imap on
    /sbin/chkconfig httpd on
    /sbin/chkconfig vmailmgrd on
    goon
fi

clear
cat <<EOF
Sendmail
---------
I will now do the following:
1) Remove sendmail
2) Set the links to qmail (alternatives)

EOF
inquire
if [ $PROCEED = "y" ]; then
    echo "Removing sendmail"
    /bin/rpm -e sendmail
    echo "Setting alternatives."
    /usr/sbin/alternatives --auto mta
fi

echo "All finished."
