[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: exim help needed



>   Add a transport in the Transport section:
> 
>     tunneled_smtp:
>        driver = smtp
>        port = 6025
> 
>   Start the Router section with:
> 
>     smart_tunnel:
>        driver = domainlist
>        transport = tunneled_smtp
>        self = send
>        route_list = "* localhost byname"
> 
> And of you go! That is, if the port is forwarded in time.
> It works here, but I give no garantees:)

Thanks for the response, I will try it out. 

> Just curious, what tricks do you use to create this port forwarding?
> And is it created on the fly? 

I create the pipes in /etc/network/interfaces:

     up sleep 3 && /etc/init.d/tcp-pipes start && /usr/local/sbin/sync-date
     down /etc/init.d/tcp-pipes stop

(don't remember what the 'sleep 3' is for..) 

/etc/init.d/tcp-pipes  is the  script below.  The command  "msleep" is
just a C  wrapper around usleep(3c). You can  use "sleep" instead (but
maybe make the numbers smaller..) 
-chris

#! /bin/sh
#
# Port forwarding to servers which would otherwise refuse connections from us
#
set -x
echo $*
NAME=`basename $0`
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
SSH=/usr/bin/ssh2 
SSHBASENAME=`basename ${SSH}`
SSHFLAGS="-f -x"
HOST=cascade.cs.ubc.ca
USER=majewski

start()
{
    LOCALPORT=$1
    DEST=$2
    REMOTEPORT=$3
    PIPE=$4
    SSHARGS="${SSHFLAGS} -l ${USER} -L ${LOCALPORT}:${DEST}:${REMOTEPORT} ${HOST} ${PIPE}l"
    PIDFILE=/var/run/${PIPE}-pipe.pid
	if [ -e ${PIDFILE} ]; then
		rm ${PIDFILE}
	fi
    if start-stop-daemon --start --verbose  --background --make-pidfile --pidfile ${PIDFILE} --exec ${SSH} -- ${SSHARGS} 2>&1 | logger; then
    	fixpid ${PIPE} ${PIDFILE}
    fi
}

# Wait for the PIDFILE to get the initial PID of the ssh process
# Wait for the ssh process to exec() to a different PID
# Put the new PID in the PIDFILE
fixpid()
{
    PIPE=$1
    PIDFILE=$2
    PID=
    NEWPID=
    COUNT=1
    MAXCOUNT=60
    GOTCHA=false
    while [ "${PID}" = "" ]; do
	if [ ${COUNT} -gt ${MAXCOUNT} ]; then
	    logger "${NAME}: initial PID not found for ${PIPE}"
	    logger "${NAME}: continuing anyway..."
	    break
	fi
	if [ -e ${PIDFILE} ]; then
		PID=`cat ${PIDFILE}`
	fi
	COUNT=`expr ${COUNT} + 1`
	msleep 1
    done
    # sleep until the process with the old PID goes away
    # can't use 'wait' because the ssh process is not our child
    logger "Waiting for ${PIPE} pipe to fork"
    COUNT=1
    while ps h -o pid -p ${PID} >> /dev/null; do
	if [ ${COUNT} -gt ${MAXCOUNT} ]; then
	    logger "${NAME}: new PID not found for ${PIPE}"
	    logger "${NAME}: continuing anyway..."
	    break
	fi
	COUNT=`expr ${COUNT} + 1`
	GOTCHA=true
	msleep 1
    done
    NEWPID=`ps h -C ${SSHBASENAME} | grep ${PIPE} | awk '{print $1}'`
    if [ "${NEWPID}" = "" ]; then
	logger "${NAME}: Empty PID, you may have to stop ${PIPE} manually later on"
    else
	echo ${NEWPID} > ${PIDFILE}
    fi
}

stop()
{
    PIPE=$1
    PID=
    logger "Stopping ${PIPE}"
    PIDFILE=/var/run/${PIPE}-pipe.pid
    start-stop-daemon --stop --verbose --pidfile ${PIDFILE}
    if [ $? -ne 0 ]; then
	PID=`ps h -C ${SSHBASENAME} | grep ${PIPE} | awk '{print $1}'`
	if [ "${PID}" != "" ]; then
	    echo "Killing ${PIPE} pipe by brute force"
	    logger "${NAME}: Killing ${PIPE} pipe by brute force"
	    # ask process to die honorably
	    kill -TERM ${PID}
	    # coup de grace if necessary
	    PID=`ps h -C ${SSHBASENAME} | grep ${PIPE} | awk '{print $1}'`
	    if [ "${PID}" != "" ]; then
		kill -KILL ${PID}
	    fi
	else
	    logger "${NAME}: Couldn't find PID for ${PIPE}, not killing"
	fi
    fi
    rm -f ${PIDFILE}
}

startImap()
{
    logger "Starting IMAP pipe"
    start 6143 imap.cs.ubc.ca 143 imap
    
}

startNntp()
{
    logger "Starting NNTP pipe"
    start 6119 news.cs.ubc.ca 119 nntp 
}

startSmtp()
{
    logger "Starting SMTP pipe"
    start 6025 mailhost.cs.ubc.ca 25 smtp 
}

startAll()
{
	    echo "Starting ${NAME}: "
	    startImap
	    startNntp
	    startSmtp
	    echo "${NAME}."
}


stopAll()
{
	    echo "Stopping ${NAME}: "
	    stop imap
  	    stop nntp
  	    stop smtp
	    echo "${NAME}."
}

case "$1" in
	imap)
	startImap
	;;
	nntp)
	startNntp
	;;
	smtp)
	startSmtp
	;;	
  start)
    startAll
    ;;
  stop)
    stopAll
    ;;
  restart)
    stopAll
    startAll
    ;;
  *)
	N=/etc/init.d/${NAME}
	# echo "Usage: $N {start|stop|restart|reload|force-reload}" >&2
	# echo "Usage: $N {start|stop|restart|force-reload}" >&2
	echo "Usage: $N {start|stop}" >&2
	exit 1
	;;
esac

exit 0




Reply to: