Blob


1 .\" Copyright (c) 2021 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: January 30 2021$
15 .Dt GMID 1
16 .Os
17 .Sh NAME
18 .Nm gmid
19 .Nd simple and secure Gemini server
20 .Sh SYNOPSIS
21 .Nm
22 .Bk -words
23 .Op Fl fnv
24 .Op Fl c Ar config
25 .Op Fl D Ar macro Ns = Ns Ar value
26 .Op Fl P Ar pidfile
27 .Ek
28 .Nm
29 .Bk -words
30 .Op Fl 6hVv
31 .Op Fl d Pa certs-dir
32 .Op Fl H Ar hostname
33 .Op Fl p Ar port
34 .Op Fl x Pa cgi
35 .Op Pa dir
36 .Ek
37 .Sh DESCRIPTION
38 .Nm
39 is a simple and minimal gemini server that can serve static files,
40 execute CGI scripts and talk to FastCGI applications.
41 It can run without a configuration file with a limited set of features
42 available.
43 .Pp
44 .Nm
45 rereads the configuration file when it receives
46 .Dv SIGHUP .
47 .Pp
48 The options are as follows:
49 .Bl -tag -width 14m
50 .It Fl c Pa config
51 Specify the configuration file.
52 .It Fl D Ar macro Ns = Ns Ar value
53 Define
54 .Ar macro
55 to be set to
56 .Ar value
57 on the command line.
58 Overrides the definition of
59 .Ar macro
60 in the config file if present.
61 .It Fl f
62 Stays and logs on the foreground.
63 .It Fl n
64 Check that the configuration is valid, but don't start the server.
65 .It Fl P Pa pidfile
66 Write
67 .Nm
68 pid to the given path.
69 .El
70 .Pp
71 If no configuration file is given,
72 .Nm
73 will look for the following options
74 .Bl -tag -width 14m
75 .It Fl 6
76 Enable IPv6.
77 .It Fl d Pa certs-path
78 Directory where certificates for the config-less mode are stored.
79 By default is
80 .Pa $XDG_DATA_HOME/gmid ,
81 i.e.
82 .Pa ~/.local/share/gmid .
83 .It Fl H Ar hostname
84 The hostname, by default
85 .Ar localhost .
86 Certificates for the given
87 .Ar hostname
88 are searched inside the
89 .Pa certs-dir
90 directory given with the
91 .Fl d
92 option.
93 They have the form
94 .Pa hostname.cert.pem
95 and
96 .Pa hostname.key.pem .
97 If a certificate or key don't exists for a given hostname they
98 will be automatically generated.
99 .It Fl h , Fl -help
100 Print the usage and exit.
101 .It Fl p Ar port
102 The port to listen on, by default 1965.
103 .It Fl V , Fl -version
104 Print the version and exit.
105 .It Fl v
106 Verbose mode.
107 Multiple
108 .Fl v
109 options increase the verbosity.
110 .It Fl x Pa path
111 Enable execution of CGI scripts.
112 See the description of the
113 .Ic cgi
114 option in the section
115 .Sq Servers
116 below to learn how
117 .Pa path
118 is processed.
119 Cannot be provided more than once.
120 .It Pa dir
121 The root directory to serve.
122 By default the current working directory is assumed.
123 .El
124 .Sh CONFIGURATION FILE
125 The configuration file is divided into three sections:
126 .Bl -tag -width xxxx
127 .It Sy Macros
128 User-defined variables may be defined and used later, simplifying the
129 configuration file.
130 .It Sy Global Options
131 Global settings for
132 .Nm .
133 .It Sy Servers
134 Virtual hosts definition.
135 .El
136 .Pp
137 Within the sections, empty lines are ignored and comments can be put
138 anywhere in the file using a hash mark
139 .Pq Sq # ,
140 and extend to the end of the current line.
141 A boolean is either the symbol
142 .Sq on
143 or
144 .Sq off .
145 A string is a sequence of characters wrapped in double quotes,
146 .Dq like this .
147 Multiple strings one next to the other are joined into a single
148 string:
149 .Bd -literal -offset indent
150 # equivalent to "temporary-failure"
151 block return 40 "temporary" "-" "failure"
152 .Ed
153 .Pp
154 Strict ordering of the sections is not enforced, so that is possible
155 to mix macros, options and
156 .Ic server
157 blocks.
158 However, defining all the
159 .Ic server
160 blocks after the macros and the global options is recommended.
161 .Ss Macros
162 Macros can be defined that will later be expanded in context.
163 Macro names must start with a letter, digit or underscore and may
164 contain any of those characters.
165 Macros names may not be reserved words.
166 Macros are not expanded inside quotes.
167 .Pp
168 For example:
169 .Bd -literal -offset indent
170 dir = "/var/gemini"
171 cert = "/etc/keys"
173 server "foo" {
174 root $dir "/foo" # -> /var/gemini/foo
175 cert $cert "/foo.crt" # -> /etc/keys/foo.crt
176 key $cert "/foo.pem" # -> /etc/keys/foo.pem
178 .Ed
179 .Ss Global Options
180 .Bl -tag -width 12m
181 .It Ic chroot Pa path
182 .Xr chroot 2
183 the process to the given
184 .Pa path .
185 The daemon has to be run with root privileges and thus the option
186 .Ic user
187 needs to be provided, so privileges can be dropped.
188 Note that
189 .Nm
190 will enter the chroot after loading the TLS keys, but before opening
191 the virtual host root directories.
192 It's recommended to keep the TLS keys outside the chroot.
193 Future version of
194 .Nm
195 may enforce this.
196 .It Ic ipv6 Ar bool
197 Enable or disable IPv6 support.
198 By default is off.
199 .It Ic mime Ar mime-type Ar file-extension
200 Add a mapping for the given
201 .Ar file-extension
202 to the given
203 .Ar mime-type .
204 Both argument are strings.
205 .It Ic port Ar portno
206 The port to listen on.
207 By default is 1965.
208 .It Ic prefork Ar number
209 Run the specified number of server processes.
210 This increases the performance and prevents delays when connecting to
211 a server.
212 .Nm
213 runs 3 server processes by default, when not in config-less mode.
214 The maximum number allowed is 16.
215 .It Ic protocols Ar string
216 Specify the TLS protocols to enable.
217 Refer to
218 .Xr tls_config_parse_protocols 3
219 for the valid protocol string values.
220 By default, both TLSv1.3 and TLSv1.2 are enabled.
221 Use
222 .Dq tlsv1.3
223 to enable only TLSv1.3.
224 .It Ic user Ar string
225 Run the daemon as the given user.
226 .El
227 .Ss Servers
228 Every virtual host is defined by a
229 .Ic server
230 block:
231 .Bl -tag -width Ds
232 .It Ic server Ar hostname Brq ...
233 Match the server name using shell globbing rules.
234 This can be an explicit name,
235 .Ar www.example.com ,
236 or a name including a wildcards,
237 .Ar *.example.com .
238 .El
239 .Pp
240 Followed by a block of options that is enclosed in curly brackets:
241 .Bl -tag -width Ds
242 .It Ic alias Ar name
243 Specify an additional alias
244 .Ar name
245 for this server.
246 .It Ic auto Ic index Ar bool
247 If no index file is found, automatically generate a directory listing.
248 It's disabled by default.
249 .It Ic block Op Ic return Ar code Op Ar meta
250 Send a reply and close the connection;
251 .Ar code
252 is 40
253 and
254 .Ar meta
255 is
256 .Dq temporary failure
257 by default.
258 If
259 .Ar code
260 is in the 3x range, then
261 .Ar meta
262 must be provided.
263 Inside
264 .Ar meta ,
265 the following special sequences are replaced:
266 .Bl -tag -width Ds -compact
267 .It \&%\&%
268 is replaced with a single
269 .Sq \&% .
270 .It \&%p
271 is replaced with the request path.
272 .It \&%q
273 is replaced with the query string of the request.
274 .It \&%P
275 is replaced with the server port.
276 .It \&%N
277 is replaced with the server name.
278 .El
279 .It Ic cert Pa file
280 Path to the certificate to use for this server.
281 The
282 .Pa file
283 should contain a PEM encoded certificate.
284 This option is mandatory.
285 .It Ic cgi Pa path
286 Execute CGI scripts that matches
287 .Pa path
288 using shell globbing rules.
289 .It Ic default type Ar string
290 Set the default media type that is used if the media type for a
291 specified extension is not found.
292 If not specified, the
293 .Ic default type
294 is set to
295 .Dq application/octet-stream .
296 .It Ic entrypoint Pa path
297 Handle all the requests for the current virtual host using the
298 CGI script at
299 .Pa path .
300 .It Ic env Ar name Ar value
301 Set the environment variable
302 .Ar name
303 to
304 .Ar value
305 when executing CGI scripts.
306 Can be provided more than once.
307 .\" don't document the "spawn <prog>" form because it probably won't
308 .\" be kept.
309 .It Ic fastcgi Oo Ic tcp Oc Pa socket Oo Ar port Oc
310 Enable FastCGI instead of serving files.
311 The
312 .Pa socket
313 can either be a UNIX domain socket or a TCP socket.
314 If the FastCGI application is listening on a UNIX domain socket,
315 .Pa socket
316 is a local path name within the
317 .Xr chroot 2
318 root directory of
319 .Nm .
320 Otherwise, the
321 .Ic tcp
322 keyword must be provided and
323 .Pa socket
324 is interpreted as a hostname or an IP address.
325 .Ar port
326 can be either a port number or the name of a service enclosed in
327 double quotes.
328 If it's not specified defaults to 9000.
329 .It Ic index Ar string
330 Set the directory index file.
331 If not specified, it defaults to
332 .Pa index.gmi .
333 .It Ic key Pa file
334 Specify the private key to use for this server.
335 The
336 .Pa file
337 should contain a PEM encoded private key.
338 This option is mandatory.
339 .It Ic lang Ar string
340 Specify the language tag for the text/gemini content served.
341 If not specified, no
342 .Dq lang
343 parameter will be added in the response.
344 .It Ic location Pa path Brq ...
345 Specify server configuration rules for a specific location.
346 The
347 .Pa path
348 argument will be matched against the request path with shell globbing
349 rules.
350 In case of multiple location statements in the same context, the first
351 matching location will be put into effect and the later ones ignored.
352 Therefore is advisable to match for more specific paths first and for
353 generic ones later on.
355 .Ic location
356 section may include most of the server configuration rules
357 except
358 .Ic alias , Ic cert , Ic env , Ic key , Ic location ,
359 .Ic entrypoint No and Ic cgi .
360 .It Ic log Ar bool
361 Enable or disable the logging for the current server or location block.
362 .It Ic param Ar name Ar value
363 Set the param
364 .Ar name
365 to
366 .Ar value
367 for FastCGI.
368 .It Ic root Pa directory
369 Specify the root directory for this server.
370 It's relative to the chroot, if enabled.
371 .It Ic require Ic client Ic ca Pa path
372 Allow requests only from clients that provide a certificate signed by
373 the CA certificate in
374 .Pa path .
375 It needs to be a PEM-encoded certificate and it's not relative to the
376 chroot.
377 .It Ic strip Ar number
378 Strip
379 .Ar number
380 components from the beginning of the path before doing a lookup in the
381 root directory.
382 It's also considered for the
383 .Ar meta
384 parameter in the scope of a
385 .Ic block return .
386 .El
387 .Sh CGI
388 When a request for an executable file matches the
389 .Ic cgi
390 rule, that file will be execute and its output fed to the client.
391 .Pp
392 The CGI scripts are executed in the directory they reside and inherit
393 the environment from
394 .Nm
395 with these additional variables set:
396 .Bl -tag -width 24m
397 .It Ev GATEWAY_INTERFACE
398 .Dq CGI/1.1
399 .It Ev GEMINI_DOCUMENT_ROOT
400 The root directory of the virtual host.
401 .It Ev GEMINI_SCRIPT_FILENAME
402 Full path to the CGI script being executed.
403 .It Ev GEMINI_URL
404 The full IRI of the request.
405 .It Ev GEMINI_URL_PATH
406 The path of the request.
407 .It Ev PATH_INFO
408 The portion of the requested path that is derived from the the IRI
409 path hierarchy following the part that identifies the script itself.
410 Can be unset.
411 .It Ev PATH_TRANSLATED
412 Present if and only if
413 .Ev PATH_INFO
414 is set.
415 It represent the translation of the
416 .Ev PATH_INFO .
417 .Nm
418 builds this by appending the
419 .Ev PATH_INFO
420 to the virtual host directory root.
421 .It Ev QUERY_STRING
422 The decoded query string.
423 .It Ev REMOTE_ADDR , Ev REMOTE_HOST
424 Textual representation of the client IP.
425 .It Ev REQUEST_METHOD
426 This is present only for RFC3875 (CGI) compliance.
427 It's always set to the empty string.
428 .It Ev SCRIPT_NAME
429 The part of the
430 .Ev GEMINI_URL_PATH
431 that identifies the current CGI script.
432 .It Ev SERVER_NAME
433 The name of the server
434 .It Ev SERVER_PORT
435 The port the server is listening on.
436 .It Ev SERVER_PROTOCOL
437 .Dq GEMINI
438 .It Ev SERVER_SOFTWARE
439 The name and version of the server, i.e.
440 .Dq gmid/1.5
441 .It Ev AUTH_TYPE
442 The string "Certificate" if the client used a certificate, otherwise
443 unset.
444 .It Ev REMOTE_USER
445 The subject of the client certificate if provided, otherwise unset.
446 .It Ev TLS_CLIENT_ISSUER
447 The is the issuer of the client certificate if provided, otherwise
448 unset.
449 .It Ev TLS_CLIENT_HASH
450 The hash of the client certificate if provided, otherwise unset.
451 The format is
452 .Dq ALGO:HASH .
453 .It Ev TLS_VERSION
454 The TLS version negotiated with the peer.
455 .It Ev TLS_CIPHER
456 The cipher suite negotiated with the peer.
457 .It Ev TLS_CIPHER_STRENGTH
458 The strength in bits for the symmetric cipher that is being used with
459 the peer.
460 .It Ev TLS_CLIENT_NOT_AFTER
461 The time corresponding to the end of the validity period of the peer
462 certificate in the ISO 8601 format
463 .Pq e.g. Dq 2021-02-07T20:17:41Z .
464 .It Ev TLS_CLIENT_NOT_BEFORE
465 The time corresponding to the start of the validity period of the peer
466 certificate in the ISO 8601 format.
467 .El
468 .Sh FastCGI
469 .Nm
470 optionally supports FastCGI.
472 .Ic fastcgi
473 rule must be present in a server or location block.
474 Then, all requests matching that server or location will be handled
475 via the specified FastCGI backend.
476 .Pp
477 By default the following variables
478 .Pq parameters
479 are sent, and carry the same semantics as with CGI.
480 More parameters can be added with the
481 .Ic param
482 option.
483 .Bl -bullet -compact
484 .It
485 GATEWAY_INTERFACE
486 .It
487 GEMINI_URL_PATH
488 .It
489 QUERY_STRING
490 .It
491 REMOTE_ADDR
492 .It
493 REMOTE_HOST
494 .It
495 REQUEST_METHOD
496 .It
497 SERVER_NAME
498 .It
499 SERVER_PROTOCOL
500 .It
501 SERVER_SOFTWARE
502 .It
503 AUTH_TYPE
504 .It
505 REMOTE_USER
506 .It
507 TLS_CLIENT_ISSUER
508 .It
509 TLS_CLIENT_HASH
510 .It
511 TLS_VERSION
512 .It
513 TLS_CIPHER
514 .It
515 TLS_CIPHER_STRENGTH
516 .It
517 TLS_CLIENT_NOT_BEFORE
518 .It
519 TLS_CLIENT_NOT_AFTER
520 .El
521 .Sh MIME
522 To auto-detect the MIME type of the response
523 .Nm
524 looks at the file extension and consults its internal table.
525 By default the following mappings are loaded, but they can be
526 overridden or extended using the
527 .Ic mime
528 configuration option.
529 If no MIME is found, the value of
530 .Ic default type
531 matching the file
532 .Ic location
533 will be used, which is
534 .Dq application/octet-stream
535 by default.
536 .Pp
537 .Bl -tag -offset indent -width 14m -compact
538 .It diff
539 text/x-patch
540 .It gemini, gmi
541 text/gemini
542 .It gif
543 image/gif
544 .It jpeg
545 image/jpeg
546 .It jpg
547 image/jpeg
548 .It markdown, md
549 text/markdown
550 .It patch
551 text/x-patch
552 .It pdf
553 application/pdf
554 .It png
555 image/png
556 .It svg
557 image/svg+xml
558 .It txt
559 text/plain
560 .It xml
561 text/xml
562 .El
563 .Sh EXAMPLES
564 Serve the current directory
565 .Bd -literal -offset indent
566 $ gmid .
567 .Ed
568 .Pp
569 To serve the directory
570 .Pa docs
571 and enable CGI scripts inside
572 .Pa docs/cgi ,
573 you can
574 .Bd -literal -offset indent
575 $ mkdir docs/cgi
576 $ cat <<EOF > docs/cgi/hello
577 #!/bin/sh
578 printf "20 text/plain\\r\\n"
579 echo "hello world"
580 EOF
581 $ chmod +x docs/cgi/hello
582 $ gmid -x '/cgi/*' docs
583 .Ed
584 .Pp
585 The following is an example of a possible configuration for a site
586 that enables only TLSv1.3, adds a mime type for the file extension
587 "rtf" and defines two virtual host:
588 .Bd -literal -offset indent
589 ipv6 on # enable ipv6
591 protocols "tlsv1.3"
593 mime "application/rtf" "rtf"
595 server "example.com" {
596 cert "/path/to/cert.pem"
597 key "/path/to/key.pem"
598 root "/var/gemini/example.com"
601 server "it.example.com" {
602 cert "/path/to/cert.pem"
603 key "/path/to/key.pem"
604 root "/var/gemini/it.example.com"
605 cgi "/cgi-bin/*"
606 lang "it"
608 .Ed
609 .Pp
610 Yet another example, showing how to enable a
611 .Ic chroot
612 and use
613 .Ic location
614 rule
615 .Bd -literal -offset indent
616 chroot "/var/gemini"
617 user "_gmid"
619 server "example.com" {
620 cert "/path/to/cert.pem"
621 key "/path/to/key.pem"
622 root "/example.com" # in the /var/gemini chroot
624 location "/static/*" {
625 auto index on
626 index "index.gemini"
629 .Ed
630 .Sh ACKNOWLEDGEMENTS
631 .Nm
632 uses the
633 .Dq Flexible and Economical
634 UTF-8 decoder written by
635 .An Bjoern Hoehrmann .
636 .Sh AUTHORS
637 .An -nosplit
638 The
639 .Nm
640 program was written by
641 .An Omar Polo Aq Mt op@omarpolo.com .
642 .Sh CAVEATS
643 .Bl -bullet
644 .It
645 The root directories of all virtual hosts are opened during the daemon
646 startup; this means that if a root directory gets deleted and then
647 re-created,
648 .Nm
649 won't be able to serve files inside that directory until a restart.
650 This restriction applies only to the root directories and not their content.
651 .It
652 a %2F sequence is indistinguishable from a literal slash: this is not
653 RFC3986-compliant.
654 .It
655 a %00 sequence is treated as invalid character and thus rejected.
656 .El