Skip to main content

HOFFA: httpd, OpenBSD, flat-files, and awk

Upon learning that OpenBSD was discontinuing sqlite in the base system, I joked that the BCHS web stack would need to drop sqlite and investigate new options for a web-stack available purely in the OpenBSD base system. So continuing that joke, I proposed the HOFFA web stack: httpd, OpenBSD, flat-files, and awk.

Server configuration

This walk-through assumes you've managed to follow one of the many fine OpenBSD installation guides and now have a fresh OpenBSD system with no packages installed.

Begin by copying the template httpd.conf into the /etc directory:

$ doas cp /etc/examples/httpd.conf /etc/

Next, edit /etc/httpd.conf and in your server block, add a location directive to point to your cgi-bin/ directory (this is relative to your $CHROOT so the absolute path ends up being /var/www/cgi-bin/ by default)

server "example.com" {
    ⋮
    location "/cgi-bin/*" {
        fastcgi
        root "/"
    }
    ⋮
}

Now with the web server configured, it, and the slowcgi proxy need to be enabled.

$ doas rcctl enable slowcgi
$ doas rcctl enable httpd
$ doas rcctl start slowcgi
$ doas rcctl check slowcgi
$ doas rcctl start httpd
(the rcctl check slowcgi may be optional but it doesn't hurt to confirm that it's running)

Populating binaries & libraries in the $CHROOT

With the server and CGI proxy running, binaries need to be put where they'll be found. As this is HOFFA this uses awk

$ doas cp `which awk` /var/www/bin/
then create the library directories and copy in the needed libraries. First, find out which libraries awk needs:
$ ldd `which awk`
/usr/bin/awk:
 Start    End      Type  Open Ref GrpRef Name
 17b0e000 37b20000 exe   1    0   0      /usr/bin/awk
 05321000 2532c000 rlib  0    1   0      /usr/lib/libm.so.10.1
 07f46000 27f76000 rlib  0    1   0      /usr/lib/libc.so.92.3
 02875000 02875000 ld.so 0    1   0      /usr/libexec/ld.so
so we need to create those library directories in the $CHROOT
$ doas mkdir -p /var/www/usr/lib /var/www/usr/libexec
and copy the libraries in
$ doas cp /usr/lib/lib[mc].so* /var/www/usr/lib/
$ doas cp /usr/libexec/ld.so /var/www/usr/libexec/

Hello world

Now with the web server & slowcgi in place, along with the required binaries & libraries, it's time to write some code.

$ cd /var/www/cgi-bin
$ doas ed example.awk
a
#!/bin/awk -f
BEGIN {
 printf("Status: 200 OK\n");
 printf("Content-type: text/plain\n\n");
 print "Hello", ENVIRON["REMOTE_ADDR"], "form awk!";
}
.
wq
$ doas chown www:www example.awk
$ doas chmod 0700 example.awk

With this in place, it should now be possible to point your browser at your newly configured script. If your browser is on the same machine you can visit http://localhost/cgi-bin/example.awk or if it's on a remote machine, use its domain-name or IP address http://example.com/cgi-bin/example.awk