Tested on OpenBSD 6.4

Handle web forms with slowcgi(8) and httpd(8)

form is a shell script to handle web forms. It validates and saves each posted form into a file in /var/www/db/site/posts directory.

form 321 LoC. Enlarge, enhance, zoom!

Configure httpd(8).
Add db user for dispatching forms.

Enable and start slowcgi(8), make a directory for posted forms and tokens, change owner and group, copy binaries to /var/www, download form.

# rcctl enable slowcgi
# rcctl start slowcgi
#
# mkdir -p -m 0770 /var/www/db
# chown www:db /var/www/db
#
# echo '/bin/cat
/bin/chmod
/bin/date
/bin/dd
/bin/mkdir
/bin/rm
/bin/sh
/usr/bin/awk
/usr/bin/b64encode
/usr/bin/grep
/usr/bin/head
/usr/bin/jot
/usr/bin/printf
/usr/bin/sed
/usr/bin/stat
/usr/bin/tail
/usr/bin/tr
/usr/lib/libc.so.92.5
/usr/lib/libm.so.10.1
/usr/lib/libz.so.5.0
/usr/libexec/ld.so' |
while read -r f
do cp "$f" "/var/www$f"
done
#
# ftp -Vo /var/www/bin/form https://www.romanzolotarev.com/bin/form
form       100% |*********************|    6815      00:00
#

Create a form, template, and “success” page. Update httpd.conf and restart httpd:

# cat > /var/www/htdocs/www/feedback << EOF
#!/bin/sh
MAIL_TO='hi@romanzolotarev.com'
MAIL_SUBJECT='feedback'
FIELDS='
name, 1, 255, Name
email, 7, 255, Email
comment, 0, 500, Comment
'
ERR_FORMAT='%s should be from %s to %s chars'
ERR_INVALID='Oops! Try again.'
ERR_EXPIRED='Expired. Try again.'
EXP_TIME='3600'
DB='/db/www'
TEMPLATE='/htdocs/www/feedback.html'
SUCCESS_URL='/thanks.html'
. /bin/form
EOF
#
# cat > /var/www/htdocs/www/feedback.html << EOF
<form action="feedback.html" method="post">
 <input type="hidden" name="token" value="$token">
 <input type="text"   name="name"  value="$name">
 <input type="email"  name="email" value="$email">
 <textarea name="comment">$comment</textarea>
 <p>$err</p>
 <input type="submit" value="Send">
</form>
EOF
#
# cat > /var/www/htdocs/www/thanks.html << EOF
<h1>Thank you</h1>
EOF
#
# cat > /etc/httpd.conf << EOF
server "www" {
 listen on * port 80
 root "/htdocs/www"
 location "/feedback.html" {
     fastcgi
     root "/htdocs/www/feedback"
 }
}
EOF
#
# httpd -n
configuration ok
# rcctl restart httpd
httpd(ok)
httpd(ok)
#