1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
|
+++
title = "OpenBSD webstack: Relayd, Httpd and Acme-client"
author = ["Michał Sapka"]
date = 2023-07-19T19:08:00+02:00
categories = ["bsd"]
draft = false
weight = 3001
primary_menu = "bsd"
abstract = "How to setup the web server stack work?"
[menu]
[menu.bsd]
weight = 3001
parent = "openbsd"
identifier = "obsdweb"
name = "Webstack: Relayd, Httpd and Acme-Client"
+++
OpenBSD comes with three great tools out of the box:
- httpd(8) - an HTTP daemon
- relayd(8) - a relay daemon
- acme-client(1) - a client for Automatic Certificate Management Environment (ACME)
With those free things, we can serve static webpages over TLS. While you most likely already use [NGINX](https://www.nginx.com/) or [Apache](https://httpd.apache.org/)[^fn:1], those solutions are complex.
They work amazingly in enterprise environments where you have people with doctorates in NGINX configuration, but most real-world examples don't need that complexity.
A static blog most likely doesn't.
Let's set it up.
Due to security concerns, OpenBSD comes with doas(1) instead of sudo(1).
Copy \`/etc/examples/doas.conf\` file to \`/etc/doas.conf\`.
For all intends, and purposes, from now on doas(1) will work the same as sudo(1).
When the system boots for the very first time, ports 80 and 443 are closed, and only the SSH port is open.
This alone was a nice surprise for me.
But it gets better: since all utilities are part of the OSes, they work together perfectly.
Assuming your domain is already pointing at the correct IPs, let's start listening for unencrypted HTTP traffic.
I will use "michal.sapka.me" as the domain in all examples.
First, Open `/etc/httpd.conf` in your favorite editor and add
```shell
server "michal.sapka.me" {
listen on * port 80
root "/htdocs/michal-sapka-me"
}
```
Then create a simple HTML file under `/var/www/htdocs/michal-sapka-me/index.html`.
Httpd(8) works chrooted to _var/www_, so it threats this directory as root.
This makes the "root" option shorter to write, but it also means that the process doesn't have access to anything outside of _var/www_.
Even if an attacker can break in via the daemon, he will be locked in the www folder, so there is no risk to the rest of the system.
As I said, OpenBSD is secure by default[^fn:2].
All we need to do now it to enable the daemon via the handy rcctl(8) tool.
```shell
$ doas rcctl enable httpd
```
and to start it
```shell
$ doas rcctl start httpd
```
And boom. Opening <http://michal.sapka.me> shows on our site both on IPv4 and IPv6.
One thing to note here is the limitation of up to HTTP 1.1.
HTTP 2 is not yet supported.
Let's add TLS, so we have this cute lock icon.
For this, we will request a certificate from [Let's Encrypt](https://letsencrypt.org/) using acme-client(1). If you used certbot, this will look familiar - just tidier.
First, let's add config to `/etc/acme-client.conf`
```shell { linenos=true, linenostart=1 }
authority letsencrypt {
api url "https://acme-v02.api.letsencrypt.org/directory"
account key "/etc/acme/letsencrypt-privkey.pem"
}
authority letsencrypt-staging {
api url "https://acme-staging.api.letsencrypt.org/directory"
account key "/etc/acme/letsencrypt-staging-privkey.pem"
}
domain michal.sapka.me {
domain key "/etc/ssl/private/michal.sapka.me.key"
domain full chain certificate "/etc/ssl/michal.sapka.me.crt"
sign with letsencrypt
}
```
Lines 1-9 tell our acme-client(1) how to talk with Let's Encrypt, while lines 11-15 allow us to request a certificate for our domain.
OpenBSD comes preconfigured for Let's Encrypt, so we just enable provided settings.
Nice!
Next, we need to allow Let's Encrypt challenges.
Acme-client(1) will manage all required files, and Let's Encrypt can read them via httpd(8).
Again, like cogs in a well-oiled machine.
By default, acme-client(1) will write to `/var/www/acme`, so we need to redirect `/.well-known/acme-challenge/*` there. Let's change our `httpd.conf`:
```shell
server "michal.sapka.me" {
listen on * port 80
root "/htdocs/michal-sapka-me"
location "/.well-known/acme-challenge/*" {
root "/acme"
request strip 2
}
}
```
We can now either restart httpd(8) or reload it. Let's for the latter.
```shell
$ doas rcctl reload httpd
```
Now we can request the certificates
```shell
$ doas rcctl reload httpd
$ doas acme-client -v michal.sapka.me
```
OpenBSDs supplied tools don't print unnecessary information to the user, so we add the `-v` to see what's happening.
Assuming everything went fine, let's start serving the page with TLS!
For this, we will use relayd(8).
We could use only httpd(8), but moving it one layer up is easier.
Relayd(8) also gives us nice options for changing headers or moving some locations to a different process, like we will do with Plaroxy soon.
This also shows us the big difference between this simple solution and NGINX: while NGINX shovels everything into one process and config, OpenBSD splits it into narrow focus areas.
Let's open `/etc/relayd.conf` and add:
```shell { linenos=true, linenostart=1 }
table <httpd> { 127.0.0.1 }
http protocol "https" {
tls keypair "michal.sapka.me"
match request quick header "Host" value "michal.sapka.me" forward to <httpd>
}
relay "https" {
listen on 0.0.0.0 port 443 tls
protocol https
forward to <httpd> port 8080
}
relay "https6" {
listen on :: port 443 tls
protocol https
forward to <httpd> port 8080
}
```
Now, I won't go into much detail here, but what happens here is:
1. We create two relays, one for ipv4 and one for ipv6. One relay can listen on a single port for given IP.
Each relay uses protocol "https" to modify and steer the request to a given process.
2. Both relays set up forwarding to httpd (IP taken from the table on the head of the file) on port 8080.
3. https protocol adds a TLS key pair for the session. We've got the files from Let's Encrypt in the step above.
4. We then test each request, and if the host matches "michal.sapka.me" it will be forwarded to httpd(8).
You can also see that relayd(8) can listen on a given IP or all IPs (:: in case of IPv6)
But our httpd(8) listens only on port 80! Let's fix that by changing the \`httpd.conf\` file:
```shell
server "michal.sapka.me" {
listen on * port 8080
```
We also need to redirect HTTP to HTTPS. Since we use Relayd(8) only for HTTPS, this will be done in httpd(8). Let's add a second server to our \`httpd.conf\`:
```shell
server "michal.sapka.me" {
listen on * port 80
location * {
block return 301 "https://$HTTP_HOST$REQUEST_URI"
}
}
```
Now, when the user enters the site, the flow will look like:
1. httpd(8) will respond to :80 requests and return a 301 redirect to HTTPS
2. relayd(8) will catch the request to :443 and forward it on port :8080 to httpd(8)
3. httpd(8) will serve our site and pass the response to relayd(8) again
4. relayd(8) can modify headers before returning the response to the client.
Talking about modifying headers, let's apply some extra security!
We can expand our https protocol with the following:
```shell
# Return HTTP/HTML error pages to the client
return error
match request header set "X-Forwarded-For" value "$REMOTE_ADDR"
match request header set "X-Forwarded-By" value "$SERVER_ADDR:$SERVER_PORT"
match response header remove "Server"
match response header append "Strict-Transport-Security" value "max-age=31536000; includeSubDomains"
match response header append "X-Frame-Options" value "SAMEORIGIN"
match response header append "X-XSS-Protection" value "1; mode=block"
match response header append "X-Content-Type-Options" value "nosniff"
match response header append "Referrer-Policy" value "strict-origin"
match response header append "Content-Security-Policy" value "default-src https:; style-src 'self' \
'unsafe-inline'; font-src 'self' data:; script-src 'self' 'unsafe-inline' 'unsafe-eval'"
match response header append "Permissions-Policy" value "accelerometer=(), camera=(), \
geolocation=(), gyroscope=(), magnetometer=(), microphone=(), payment=(), usb=()"
# set recommended tcp options
tcp { nodelay, sack, socket buffer 65536, backlog 100 }
# set up certs
tls { no tlsv1.0, ciphers "HIGH:!aNULL:!SSLv3:!DSS:!ECDSA:!RSA:-ECDH:ECDHE:+SHA384:+SHA256" }
```
I won't discuss details here as each header has a dedicated MDM webdoc.
Most of the headers here are considered a standard.
Besides adding headers, we configure TLS here, disabling weak ciphers and old TLS versions and adding some standard config.
Lastly, we can automate refreshing the certificate via cron(8):
```shell
0~59 0~23 * * 1 acme-client michal.sapka.me && rcctl reload relayd
```
It looks almost like a normal cron.
The "0~59" and "0~29" parts are unique to OpenBSD: Cron(8) will evenly split all tasks between specified time boxes so that no two jobs run simultaneously.
We now have created a fully working web server without any 3rd party packages.
All OpenBSD provided, all secure, all simple, all cool as ice.
To further your knowledge, you can challenge the assumption that BSD has the best doc and read man pages for `httpd.conf(5)`, `relayd.conf(5)`, and `acme-client.conf(5)`.
I also can't recommend enough "Httpd and Relayd Mastery" by Michael W. Lucas[^fn:3]
[^fn:1]: because there is no fourth way. Please repeat after me: there is no webserver in Windows.
[^fn:2]: The ports collection of OpenBSD contains a fork of NGINX with a similar security treatment.
[^fn:3]: yeah, the one from the top of this article. He's a household name and a staple of the BSD community. I'm primarily a software engineer, and all this sysadmin thing I am doing is a side quest for me. His books make it so much easier. I've already read four of his books, and I will read more as they are amazing. Even a dense person like yours truly comes out smarter after the lecture. While I'm not a [Full Michael](<https://www.tiltedwindmillpress.com/product/full-michael-2023-06/>) kind of person, it seems my library will soon have a very strong representation of his.
|