# cd /usr/share/ssl/certs
# make stunnel.pem
umask 77 ; \
PEM1=`/bin/mktemp /tmp/openssl.XXXXXX` ; \
PEM2=`/bin/mktemp /tmp/openssl.XXXXXX` ; \
/usr/bin/openssl req -newkey rsa:1024 -keyout $PEM1 -nodes -x509 -days 365 -out $PEM2 ; \
cat $PEM1 > stunnel.pem ; \
echo "" >> stunnel.pem ; \
cat $PEM2 >> stunnel.pem ; \
rm -f $PEM1 $PEM2
Using configuration from /usr/share/ssl/openssl.cnf
Generating a 1024 bit RSA private key
....++++++
........++++++
writing new private key to '/tmp/openssl.3VVjex'
-----
You are about to be asked to enter information that will be incorporated
into your certificate request. What you are about to enter is what is
called a Distinguished Name or a DN. There are quite a few fields but you
can leave some blank. If you enter '.', the field will be left blank. For
some fields there will be a default value.
-----
Country Name (2 letter code) [AU]:US
State or Province Name (full name) [Some-State]:Maryland
Locality Name (eg, city) []:Gaithersburg
Organization Name (eg, company) [Internet Widgits Ltd]:WroteTheBook.com
Organizational Unit Name (eg, section) []:Books
Common Name (eg, your name or your server's hostname) []:Craig Hunt
Email Address []:craig.hunt@wrotethebook.com
Once the certificate is created, stunnel is ready
for use. POP and IMAP are excellent examples of services that can be
run inside a secure connection using stunnel. The
primary reason that POP and IMAP are run through
stunnel is to ensure that the user's
password cannot be stolen from a POP or IMAP session and then used by
the thief to log into the server. stunnel encrypts
everything: the login and the download of mail. This also guarantees
that the contents of the mail cannot be surreptitiously read by a
snooper during the download, although from the point of view of the
system administrator, the password is really the piece of information
you want to protect.
For secure POP and IMAP communication to work, both ends of the
connection must be able to tunnel the data through SSL. This is not
always the case. Some clients do not have stunnel;
some do not even have SSL. For this reason, servers usually provide
traditional POP and IMAP connections on the appropriate well-known
ports, and SSL-secured POP and IMAP on other ports. When run over
stunnel, POP is called pops
and assigned TCP port 995, and IMAP is called
imaps and assigned TCP port 993.
pops and imaps are not
special protocols. They are simply service names from the
/etc/services file that map to port numbers 995
and 993. The following command added to the system startup runs POP
inside an SSL tunnel on port 995:
stunnel -d 995 -l /usr/sbin/ipop3d -- ipop3d
Alternatively, stunnel can be run by
inetd using an entry in the
inetd.conf file. For example, the following
entry runs POP inside an SSL tunnel on a demand basis:
pops stream tcp nowait root /usr/sbin/stunnel -l /usr/sbin/ipop3d -- ipop3d
Systems that use xinetd can run
stunnel from the xinetd.conf
file. The following xinetd entry runs
imaps:
service imaps
{
socket_type = stream
wait = no
user = root
server = /usr/sbin/stunnel
server_args = -l /usr/sbin/imapd -- imapd
log_on_failure += USERID
}
stunnel has nothing specific to do with POP or
IMAP. It can be used to secure a wide variety of daemons. When used
to secure a daemon that is normally run by inetd
or xinetd, the stunnel command
is placed in the inetd.conf or
xinetd.conf file, as appropriate. When used to
secure a daemon that runs from a startup file, the
stunnel command is placed in that startup file.