Setting Up Stunnel On Linux
Intro
This article was written while getting SMTP authentication working with AT&T Business Class DSL services. The SMTP service requires authentication via a secure connection on port 465. Other articles will get into further details, this article’s focus is on the stunnel part of the equation, which we use to wrap the standard sendmail/SMTP configuration.
In This Article
- An example stunnel config file for talking to AT&T SMTP servers on port 465 (SMTPS)
- Testing the connection to AT&T SMTPS is working via telnet
- Getting stunnel running on system boot.
Our Environment
- CentOS release 5.2
- stunnel 4.15-2
We assume you have stunnel and telnet installed. If not, research the yum install commands for CentOS. You will also need superuser access to update the running services on your box.
Setting up stunnel
Stunnel will allow you to listen for data connections on a local port and redirect that traffic through an SSH wrapper to another system. In our case we are using stunnel to listen on port 2525 on our local server, wrap the communication in ssh and send it along to our local AT&T SMTP Server at smtp.att.yahoo.com on port 465 (aka SMTPS).
Install
To do this you will need stunnel installed. If yum is configured properly and the remote yum servers are online you can try this:
# yum install stunnel
Configure
You will then need to create or edit the stunnel configuration file and setup the AT&T SMTPS redirect. Your config file should look like this (your remote SMTPS server may have a different URL, check with your ISP):
-
client=yes
-
[rev-smtps]
-
accept=127.0.0.1:2525
-
connect=smtp.att.yahoo.com:smtps
Test
Run stunnel in a detached daemon mode:
# stunnel &
Then telnet in to localhost port 2525, which should SSH wrap the connection to the AT&T SMTP Server
# telnet 127.0.0.1 2525
You should see something like this:
-
[root@dev xinetd.d]# telnet localhost 2525
-
Trying 127.0.0.1…
-
Connected to localhost.localdomain (127.0.0.1).
-
Escape character is ‘^]’.
-
220 smtp104.sbc.mail.re3.yahoo.com ESMTP
-
EHLO
-
250-smtp104.sbc.mail.re3.yahoo.com
-
250-AUTH LOGIN PLAIN XYMCOOKIE
-
250-PIPELINING
-
250 8BITMIME
-
quit
-
-
Connection closed by foreign host.
Stop the test process by killing the detached process. Find the process ID with ps and kill it.
# ps -ef | grep stunnel
You should see something like this:
-
root 6181 1 0 11:37 ? 00:00:00 stunnel
-
root 10698 3626 0 14:11 pts/0 00:00:00 grep stunnel
Kill the process.
# kill <pid>
Starting up stunnel on boot.
stunnel can be started by using the simple # stunnel & command via a shell script that runs at startup. This method allows for session caching and generally improves performance over an xinetd controlled session.
Configure
Create /etc/init.d/stunnel:
-
#!/bin/bash#
-
# /etc/rc.d/init.d/stunnel
-
#
-
# Starts the stunnel daemon
-
#
-
# Source function library.
-
. /etc/init.d/functions
-
test -x /usr/sbin/stunnel || exit 0
-
RETVAL=0
-
#
-
# See how we were called.
-
#
-
prog="stunnel"
-
start() {
-
# Check if stunnel is already running
-
if [ ! -f /var/lock/subsys/stunnel ]; then
-
echo -n $"Starting $prog: "
-
daemon /usr/sbin/stunnel
-
RETVAL=$?
-
[ $RETVAL -eq 0 ] && touch /var/lock/subsys/stunnel
-
echo
-
fi
-
return $RETVAL
-
}
-
stop() {
-
echo -n $"Stopping $prog: "
-
killproc /usr/sbin/stunnel
-
RETVAL=$?
-
[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/stunnel
-
echo
-
return $RETVAL
-
}
-
restart() {
-
stop
-
start
-
}
-
reload() {
-
restart
-
}
-
status_at() {
-
status /usr/sbin/atd
-
}
-
case "$1" in
-
start)
-
start
-
;;
-
stop)
-
stop
-
;;
-
reload|restart)
-
restart
-
;;
-
condrestart)
-
if [ -f /var/lock/subsys/atd ]; then
-
restart
-
fi
-
;;status)
-
status_at
-
;;
-
*)
-
echo $"Usage: $0 {start|stop|restart|condrestart|status}"
-
exit 1
-
esac
-
exit $?
-
exit $RETVAL
Set the stunnel script to run at startup level 3:
# ln -s /etc/init.d/stunnel /etc/rc3.d/S58stunnel
Test
Run the same telnet test to port 2525 on localhost as noted above. Don’t kill the process when you are done.
Running via xinetd
xinetd runs various port listening services through a single program (xinet) that runs as a daemon. Since our box (and most RHEL variants) runs xinetd by default, we simply need to create our configuration file for stunnel and put it in the xinet.d directory & restart the xinetd process. This is NOT the recommended method for running stunnel.
Install
If xinetd is not installed and running on your system (it should be) then grab it with yum
# yum install xinetd
Configure
Create a new stunnel configuration file in the /etc/xinetd.d directory.
-
# description: stunnel listner to map local ports to outside ports
-
service stunnel
-
{
-
disable = no
-
flags = REUSE
-
socket_type = stream
-
wait = no
-
user = root
-
port = 2525
-
server = /usr/sbin/stunnel
-
}
You can learn more about xinetd configuration files here:
http://www.redhat.com/docs/manuals/linux/RHL-9-Manual/ref-guide/s1-tcpwrappers-xinetd-config.html
You will also need to change your stunnel config file as the accept port is now handled by xinetd. You can learn more via the stunnel manual by using # man stunnel at your linux prompt.
The new stunnel.conf file:
-
client=yes
-
connect=smtp.att.yahoo.com:smtps
Test
-
#service xinetd restart
-
#telnet 127.0.0.1 2525
You should see the same results as the stunnel test above.
- Upgrading Logwatch on CentOS 5
- Logon To Your Linux Box Using SSH Keys
- Scheduling Linux Apps
- Creating and Installing SSL Certs via SSH
2 Comments for this entry
-
Mark Hopkins August 30th, 2010 on 10:31 PM
Your startup script, /etc/init.d/stunnel does not work. What is with the “&&”? Also why is that a “#” at the end of line 1?
Also executing via xinetd did not work on RHEL 4.7.
Thanks anyway.
Mark
-
Ronnie Shavlik March 18th, 2011 on 11:20 AM
Thanks for sharing this write-up, saved me time in setting it up. As to Mark’s comment, yeah…to get the service script to work:
Remove the # from the end of the first line. Then replace where it has…
&&
with…
&&
And then down lower for the status and conditional restart options, change it from pointing to atd to stunnel.
Additionally, if you get an error about incorrect\improper permissions on your certificate file, do a ‘chmod 600 ‘.
That should do it.
1 Trackback or Pingback for this entry
-
Firebird: セキュア通信(2) : BfitSoft.Com, March 9th, 2011 on 3:05 AM
[...] Setting Up Stunnel On Linux http://www.cybersprocket.com/2010/system-administration/applications/setting-up-stunnel-on-linux/ Rsyslog, the enhanced syslogd for Linux and Unix http://www.rsyslog.com/ |Top| Tags: CentOS, [...]



