Blob


1 .\" Copyright (c) 2020 Omar Polo <op@omarpolo.com>
2 .\"
3 .\" Permission to use, copy, modify, and distribute this software for any
4 .\" purpose with or without fee is hereby granted, provided that the above
5 .\" copyright notice and this permission notice appear in all copies.
6 .\"
7 .\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8 .\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9 .\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
10 .\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11 .\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
12 .\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
13 .\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
14 .Dd $Mdocdate: October 2 2020$
15 .Dt GMIND 1
16 .Os
17 .Sh NAME
18 .Nm gmid
19 .Nd dead simple zero configuration gemini server
20 .Sh SYNOPSIS
21 .Nm
22 .Bk -words
23 .Op Fl n
24 .Op Fl c Ar config
25 |
26 .Op Fl 6fh
27 .Op Fl C Ar cert
28 .Op Fl d Ar root
29 .Op Fl K Ar key
30 .Op Fl p Ar port
31 .Op Fl x Ar directory
32 .Ek
33 .Sh DESCRIPTION
34 .Nm
35 is a simple and minimal gemini server that can serve static files and
36 execute CGI scripts.
37 It can run without a configuration file with a limited set of features
38 available.
39 If a configuration file is given, no other flags shall be given,
40 except for
41 .Fl n .
42 .Pp
43 .Nm
44 fully supports IRIs (Internationalized Resource Identifiers, see
45 RFC3987).
46 .Pp
47 The options are as follows:
48 .Bl -tag -width 12m
49 .It Fl c Pa config
50 Specifies the configuration file.
51 .It Fl n
52 Check that the configuration is valid, but don't start the server.
53 .El
54 .Pp
55 If no configuration file is given,
56 .Nm
57 will look for the following option
58 .Bl -tag -width 12m
59 .It Fl 6
60 Enable IPv6.
61 .It Fl C Pa file
62 The certificate to use.
63 .It Fl d Pa directory
64 The root directory to serve.
65 .It Fl f
66 Stays and log in the foreground, do not daemonize the process.
67 .It Fl h
68 Print the usage and exit.
69 .It Fl K Pa file
70 The key for the certificate.
71 .It Fl p Ar port
72 The port to listen on, by default 1965.
73 .It Fl x Pa directory
74 Enable execution of CGI scripts.
75 See the description of the
76 .Ic cgi
77 .Ic server
78 option in the section
79 .Sq Servers
80 below to learn how
81 .Pa directory
82 is processed.
83 Cannot be provided more than once.
84 .El
85 .Sh CONFIGURATION FILE
86 The configuration file is divided into two sections:
87 .Bl -tag -width xxxx
88 .It Sy Global Options
89 Global settings for
90 .Nm .
91 .It Sy Servers
92 Virtual hosts definition
93 .El
94 .Pp
95 Within the sections, empty lines are ignored and comments can be put
96 anywhere in the file using a hash mark
97 .Pq Sq # ,
98 and extend to the end of the current line.
99 A boolean is either the symbol
100 .Sq on
101 or
102 .Sq off .
103 .Ss Global Options
104 .Bl -tag -width 12m
105 .It Ic daemon Ar bool
106 Enable or disables the daemon mode.
107 In daemon mode
108 .Nm
109 will log to syslog and fork in the background.
110 By default is off.
111 .It Ic ipv6 Ar bool
112 Enable or disable IPv6 support.
113 By default is off.
114 .It Ic port Ar portno
115 The port to listen on.
116 By default is 1965.
117 .It Ic protocols Ar string
118 Specify the TLS protocols to enable.
119 Refer to
120 .Xr tls_config_parse_protocols 3
121 for the valid protocol string values.
122 By default, both TLSv1.3 and TLSv1.2 are used.
123 Use
124 .Dq tlsv1.3
125 to enable only TLSv1.3.
126 .It Ic mime Ar mime-type Ar file-extension
127 Add a mapping for the given
128 .Ar file-extension
129 to the given
130 .Ar mime-type .
131 Both argument are strings.
132 .It Ic chroot Pa path
133 .Xr chroot 2
134 the process to the given
135 .Pa path .
136 The daemon has to be run with root privileges and thus the option
137 .Ic user
138 needs to be provided, so
139 .Nm
140 can drop the privileges.
141 Note that they are dropped after loading the TLS keys, so it's
142 recommended to put those outside the chroot.
143 Future version of
144 .Nm
145 may require this.
146 .It Ic user Ar string
147 Run the daemon as the given user.
148 .El
149 .Ss Servers
150 Every virtual host is defined by a
151 .Ic server
152 block:
153 .Bl -tag -width Ds
154 .It Ic server Ar hostname Brq ...
155 Match the server name using shell globbing rules. This can be an explicit name,
156 .Ar www.example.com ,
157 or a name including a wildcards,
158 .Ar *.example.com .
159 .El
160 .Pp
161 Followed by a block of options that is enclosed in curly brackets:
162 .Bl -tag -width Ds
163 .It Ic cert Pa file
164 Path to the certificate to use for this server.
165 The
166 .Pa file
167 should contain a PEM encoded certificate.
168 This option is mandatory.
169 .It Ic key Pa file
170 Specify the private key to use for this server.
171 The
172 .Pa file
173 should contain a PEM encoded private key.
174 This option is mandatory.
175 .It Ic root Pa directory
176 Specify the root directory for this server.
177 This option is mandatory.
178 .It Ic cgi Pa path
179 Enable the execution of CGI scripts if
180 .Pa path
181 is a prefix of the user request string.
182 An empty path "" will effectively enable the execution of any file
183 with the executable bit set inside the root directory.
184 .It Ic default type Ar string
185 Set the default media type that is used if the media type for a
186 specified extension is not found.
187 If not specified, the
188 .Ic default type
189 is set to
190 .Dq application/octet-stream .
191 .It Ic lang Ar string
192 Specify the language tag for the text/gemini content served.
193 If not specified, no
194 .Dq lang
195 parameter will be added in the response.
196 .It Ic index Ar string
197 Set the directory index file.
198 If not specified, it defaults to
199 .Pa index.gmi
200 .It Ic auto Ic index Ar bool
201 If no index file is found, automatically generate a directory listing.
202 It's disabled by default.
203 .It Ic location Pa path Brq ...
204 Specify server configuration rules for a specific location.
205 The
206 .Pa path
207 argument will be matched against the request path with shell globbing
208 rules.
209 In case of multiple location statements in the same context, the last
210 matching location will be put into effect.
211 Therefore is advisable to match for a generic paths first and for more
212 specific ones later on.
214 .Ic location
215 section may include most of the server configuration rules
216 except
217 .Ic cert , Ic key , Ic root , Ic location No and Ic CGI .
218 .El
219 .Sh CGI
220 When CGI scripts are enabled for a directory, a request for an
221 executable file will execute it and fed its output to the client.
222 .Pp
223 The CGI scripts are executed in the root directory of the virtual
224 host, or in the served directory if run without config, and inherits
225 the environment from
226 .Nm
227 with these additional variables set:
228 .Bl -tag -width 18m
229 .It Ev GATEWAY_INTERFACE
230 "CGI/1.1"
231 .It Ev SERVER_PROTOCOL
232 "GEMINI"
233 .It Ev SERVER_SOFTWARE
234 "gmid"
235 .It Ev SERVER_PORT
236 "1965"
237 .It Ev SERVER_NAME
238 The vhost.
239 This variable is not available when operating without a configuration.
240 .It Ev SCRIPT_NAME
241 The (public) path to the script, e.g.
242 .Pa "/cgi-bin/example.cgi"
243 .It Ev SCRIPT_EXECUTABLE
244 The full path to the executable.
245 .It Ev REQUEST_URI
246 The user request (without the query parameters.)
247 .It Ev REQUEST_RELATIVE
248 The request relative to the script.
249 .It Ev QUERY_STRING
250 The query parameters.
251 .It Ev REMOTE_HOST
252 The remote IP address.
253 .It Ev REMOTE_ADDR
254 The remote IP address.
255 .It Ev DOCUMENT_ROOT
256 The root directory being served, the one provided with the
257 .Ar d
258 parameter to
259 .Nm
260 or the root directory of the virtual host.
261 .It Ev AUTH_TYPE
262 The string "Certificate" if the client used a certificate, otherwise
263 unset.
264 .It Ev REMOTE_USER
265 The subject of the client certificate if provided, otherwise unset.
266 .It Ev TLS_CLIENT_ISSUER
267 The is the issuer of the client certificate if provided, otherwise
268 unset.
269 .It Ev TLS_CLIENT_HASH
270 The hash of the client certificate if provided, otherwise unset.
271 The format is "ALGO:HASH".
272 .El
273 .Pp
274 Let's say you have a script in
275 .Pa /cgi-bin/script
276 and the user request is
277 .Pa /cgi-bin/script/foo/bar?quux .
278 Then
279 .Ev SCRIPT_NAME
280 will be
281 .Pa cgi-bin/script ,
282 .Ev SCRIPT_EXECUTABLE
283 will be
284 .Pa $DOCUMENT_ROOT/cgi-bin/script ,
285 .Ev REQUEST_URI
286 will be
287 .Pa cgi-bin/script/foo/bar ,
288 .Ev REQUEST_RELATIVE
289 will be
290 .Pa foo/bar
291 and
292 .Ev QUERY_STRING
293 will be
294 .Ar quux .
295 .Sh EXAMPLES
296 To quickly getting started
297 .Bd -literal -offset indent
298 $ # generate a cert and a key
299 $ openssl req -x509 -newkey rsa:4096 -keyout key.pem \\
300 -out cert.pem -days 365 -nodes
301 $ mkdir docs
302 $ cat <<EOF > docs/index.gmi
303 # Hello world
304 test paragraph...
305 EOF
306 $ gmid -C cert.pem -K key.pem -d docs
307 .Ed
308 .Pp
309 Now you can visit gemini://localhost/ with your preferred gemini
310 client.
311 .Pp
312 To add some CGI scripts, assuming a setup similar to the previous
313 example, you can
314 .Bd -literal -offset indent
315 $ mkdir docs/cgi-bin
316 $ cat <<EOF > docs/cgi-bin/hello-world
317 #!/bin/sh
318 printf "20 text/plain\\r\\n"
319 echo "hello world!"
320 EOF
321 $ gmid -C cert.pem -K key.pem -d docs -x cgi-bin
322 .Ed
323 .Pp
324 Note that the argument to the
325 .Fl x
326 option is
327 .Pa cgi-bin
328 and not
329 .Pa docs/cgi-bin ,
330 since it's relative to the document root.
331 .Pp
332 The following is an example of a possible configuration for a site
333 that enables only TLSv1.3, adds a mime type for the file extension
334 "rtf" and defines two virtual host:
335 .Bd -literal -offset indent
336 ipv6 on # enable ipv6
337 daemon on # enable daemon mode
339 protocols "tlsv1.3"
341 mime "application/rtf" "rtf"
343 server "example.com" {
344 cert "/path/to/cert.pem"
345 key "/path/to/key.pem"
346 root "/var/gemini/example.com"
349 server "it.example.com" {
350 cert "/path/to/cert.pem"
351 key "/path/to/key.pem"
352 root "/var/gemini/it.example.com"
353 cgi "/cgi-bin"
354 lang "it"
356 .Ed
357 .Sh ACKNOWLEDGEMENTS
358 .Nm
359 uses the
360 .Dq Flexible and Economical
361 UTF-8 decoder written by
362 .An Bjoern Hoehrmann
363 for its IRI parser.
364 .Sh CAVEATS
365 .Bl -bullet
366 .It
367 The root directories of all virtual hosts are opened during the daemon
368 startup; this means that if a root directory gets deleted and then
369 re-created,
370 .Nm
371 won't be able to serve files inside that directory until a restart.
372 This restriction applies only to the root directories and not their content.
373 .It
374 a %2F sequence in the path part is indistinguishable from a literal
375 slash: this is not RFC3986-compliant.
376 .It
377 a %00 sequence either in the path or in the query part is treated as
378 invalid character and thus rejected.
379 .El