Running OpenSSH under Daemontools and UCSPI-TCP

OpenSSH is a set of computer programs which provide encrypted communications using the SSH protocol. OpenSSH provides a secure alternative for ftp (with sftp), rlogin and telnet (with ssh) and rcp (with scp). OpenBSD currently develops OpenSSH and is need of funding. If you find this document and/or OpenSSH helpful, I encourage you to make a donation.

Daemontools is a collection of tools for managing Unix services. It provides a means of monitoring a service, starting and stopping it and logging any debug and/or error messages. Daemontools provides easy service installation and removal, easy first time service startup, reliable restarts, easy, reliable signalling, clean process state and OS portability.

Ucspi-tcp is a collection of command line tools for building TCP client server applications. It is commonly used as a replacement for inetd and xinetd.

Daemontools ensures that if OpenSSH crashes, the daemon automatically restarts. Ucspi-tcp provides a wonderful mechanisms for access control to OpenSSH. Ucspi-tcp allows you to limit connections based on number, amount of memory or by IP addresses. Blocking IP addresses could be already accomplished by a program like iptables or tcp-wrappers, however a little defense in depth never hurt. Also the CDB format for the access control rules allows for quicker action with a much larger number of hosts.


Setting up the Service Directories

First verify the daemontools "svscan" process is running. Choose a location where you want the physical service directories. I usually use "/var/service", however any directory may be used as long as it is not "/service".

Create the service directories with the following commands.

# mkdir -m 1755 /var/service/sshd
# mkdir -p -m 755 /var/service/sshd/log

Download the "run" scripts for sshd and its log.

# cd /var/service/sshd
# wget -c http://www.antagonism.org/scripts/sshd-run
# mv sshd-run run
# chmod 755 run
# cd log
# wget -c http://www.antagonism.org/scripts/log-run
# mv log-run run
# chmod 755 run

Warning, before using either of my "run" scripts, make sure you understand what the commands do. In the sshd "run" script, the softlimit options I have configured do the following:

The tcpserver options I have configured do the following:

The sshd options I have configured do the following:

These options and more are covered in the softlimit, tcpserver and sshd man pages. The "log" run script rotates the logs every 1MB, keeps the latest 20 logs and writes them to the "main" directory under the "log" directory.

Create a "ssh" file in the "/etc/tcp" directory which contains your access controls. A default control list which will allow all traffic may look like this:

:allow

After creating the file, download and edit the Makefile so the line reading "all:" contains the file name "ssh.cdb". Below is an example.

all: ssh.cdb

Running the "make" command will create/update the CDB files as needed.


Activating the Service

(The below section is taken almost verbatim from the following page created by John Simpson. I felt that his description on what happens when you activate a service was the most clear and easy to understand, so why change a thing?)

Once the directories are set up, you need to make them start running. This is done by creating a symbolic link from /service/(whatever) to the physical directory where the service lives. The "svscan" program checks /service every five seconds, and when it sees a new directory (or symbolic link) there, it starts a "supervise" process for that directory. In addition, if the directory has the sticky bit set and a child directory called "log", it starts a "supervise" process for the "log" child directory and sets up a pipe between the two processes (so that the main process's logs end up being sent to the log process).

The "supervise" program works by running the "run" script inside of whatever directory it's watching. If that child process (either the "run" script itself, or whatever process it runs using "exec") stops, it starts it back up by running the "run" script again.

The following commands will create the symbolic links needed to start the OpenSSH service.

# ln -s /var/service/sshd /service/

After running this command, wait ten seconds (to give it time to start) and then run the "svstat" command to see what's running:

# svstat /service/sshd /service/sshd/log
/service/sshd: up (pid 2542) 311723 seconds
/service/sshd/log: up (pid 2532) 311723 seconds

As long as the new services show "up" with a timer of more than one second, the services are running correctly. If the timer on a service is 0 or 1 second, then wait about five seconds and run the same command - it should now be higher than 1 second. If it's still 0 or 1, then the service is having a problem and you need to fix it. This page provides some steps to troubleshoot daemontools service installations.


Downloads

File: sshd-run
Size: 195 bytes
Date: 2008-05-02 14:49:56 -0700
MD5: 47155771375af092dbc04128a3ca7be7
SHA-1: 2cf59407998f2c00945029c5c6a3669f4ba4b3bc
RIPEMD-160: bb8622cbdc300af697bd25eab58507bce4440727
PGP Signature: sshd-run.asc
File: log-run
Size: 47 bytes
Date: 2008-05-02 14:49:56 -0700
MD5: 1cc7ef3d56be3ec766a9b382d19d1604
SHA-1: d04a2286a41bddd77577443253ac67654f0b7425
RIPEMD-160: 4df90694ac5cb454b6e360c69278e244c6cdf924
PGP Signature: log-run.asc
File: etc-tcp-makefile
Size: 1,373 bytes
Date: 2008-05-02 14:49:56 -0700
MD5: 4a684354c95a9b28698263f0bb5798d7
SHA-1: 37faff28754d91eaef1d3b74309eb500867244e4
RIPEMD-160: 0fbcf43106d5155b5fbb11302d427c639f906b52
PGP Signature: etc-tcp-makefile.asc