1 .\" Copyright (c) 2021, 2022 Omar Polo <op@omarpolo.com>
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.
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 02 2022$
19 .Nd simple and secure Gemini server
25 .Op Fl D Ar macro Ns = Ns Ar value
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
45 rereads the configuration file when it receives
48 The options are as follows:
51 Specify the configuration file.
52 .It Fl D Ar macro Ns = Ns Ar value
58 Overrides the definition of
60 in the config file if present.
62 Stays and logs on the foreground.
64 Check that the configuration is valid, but don't start the server.
65 If specified two or more time, dump the configuration in addition to
68 Write daemon's pid to the given location.
70 will also act as lock: if another process is holding a lock on that
76 If no configuration file is given,
80 .Pq i.e. runs in the foreground to serve a directory from the shell
81 and looks for the following options
85 .It Fl d Pa certs-path
86 Directory where certificates for the config-less mode are stored.
88 .Pa $XDG_DATA_HOME/gmid ,
90 .Pa ~/.local/share/gmid .
97 Certificates for the given
99 are searched inside the
101 directory given with the
105 .Pa hostname.cert.pem
107 .Pa hostname.key.pem .
108 If a certificate or a key doesn't exist for a given hostname, they
109 will be generated automatically.
111 Print the usage and exit.
113 The port to listen on, by default 1965.
114 .It Fl V , Fl -version
115 Print the version and exit.
120 options increase the verbosity.
125 See the description of the
129 section below to learn how
132 Cannot be provided more than once.
134 The root directory to serve.
135 By default the current working directory is assumed.
137 .Sh CONFIGURATION FILE
138 The configuration file is divided into three sections:
141 User-defined variables may be defined and used later, simplifying the
143 .It Sy Global Options
147 Virtual hosts definition.
150 Within the sections, empty lines are ignored and comments can be put
151 anywhere in the file using a hash mark
153 and extend to the end of the current line.
154 A boolean is either the symbol
158 A string is a sequence of characters wrapped in double quotes,
160 Multiple strings one next to the other are joined into a single
162 .Bd -literal -offset indent
163 # equivalent to "temporary-failure"
164 block return 40 "temporary" "-" "failure"
167 Furthermore, quoting is necessary only when a string needs to contain
169 .Pq like spaces or punctuation ,
170 something that looks like a number or a reserved keyword.
171 The last example could have been written also as:
172 .Bd -literal -offset indent
173 block return 40 temporary "-" failure
176 Strict ordering of the sections is not enforced, so that is possible
177 to mix macros, options and
180 However, defining all the
182 blocks after the macros and the global options is recommended.
184 Newlines are often optional, except around top-level instructions, and
187 can also be optionally used to separate options.
189 Additional configuration files can be included with the
191 keyword, for example:
192 .Bd -literal -offset indent
193 include "/etc/gmid.conf.local"
196 Macros can be defined that will later be expanded in context.
197 Macro names must start with a letter, digit or underscore and may
198 contain any of those characters.
199 Macros names may not be reserved words.
200 Macros are not expanded inside quotes.
202 Two kinds of macros are supported: variable-like and proper macros.
203 When a macro is invoked with a
205 before its name its expanded as a string, whereas when it's invoked
208 its expanded in-place.
211 .Bd -literal -offset indent
213 certdir = "/etc/keys"
214 common = "lang it; auto index on"
217 root $dir "/foo" # -> /var/gemini/foo
218 cert $certdir "/foo.crt" # -> /etc/keys/foo.crt
219 key $certdir "/foo.pem" # -> /etc/keys/foo.pem
225 .It Ic chroot Pa path
227 the process to the given
229 The daemon has to be run with root privileges and thus the option
231 needs to be provided, so privileges can be dropped.
234 will enter the chroot after loading the TLS keys, but before opening
235 the virtual host root directories.
236 It's recommended to keep the TLS keys outside the chroot.
241 Enable or disable IPv6 support, off by default.
242 .It Ic map Ar mime-type Cm to-ext Ar file-extension
247 Both argument are strings.
248 .It Ic port Ar portno
249 The port to listen on.
251 .It Ic prefork Ar number
252 Run the specified number of server processes.
253 This increases the performance and prevents delays when connecting to
255 When not in config-less mode,
257 runs 3 server processes by default.
258 The maximum number allowed is 16.
259 .It Ic protocols Ar string
260 Specify the TLS protocols to enable.
262 .Xr tls_config_parse_protocols 3
263 for the valid protocol string values.
264 By default, both TLSv1.3 and TLSv1.2 are enabled.
267 to enable only TLSv1.3.
268 .It Ic user Ar string
269 Run the daemon as the given user.
272 Every virtual host is defined by a
276 .It Ic server Ar hostname Brq ...
277 Match the server name using shell globbing rules.
278 It can be an explicit name,
279 .Ar www.example.com ,
280 or a name including a wildcards,
284 Followed by a block of options that is enclosed in curly brackets:
287 Specify an additional alias
290 .It Ic auto Ic index Ar bool
291 If no index file is found, automatically generate a directory listing.
293 .It Ic block Op Ic return Ar code Op Ar meta
294 Send a reply and close the connection;
301 .Dq temporary failure .
304 is in the 3x range, then
309 the following special sequences are supported:
310 .Bl -tag -width Ds -compact
312 is replaced with a single
315 is replaced with the request path.
317 is replaced with the query string of the request.
319 is replaced with the server port.
321 is replaced with the server name.
324 Path to the certificate to use for this server.
327 should contain a PEM encoded certificate.
328 This option is mandatory.
334 using shell globbing rules.
335 .It Ic default type Ar string
336 Set the default media type that is used if the media type for a
337 specified extension is not found.
338 If not specified, the
341 .Dq application/octet-stream .
342 .It Ic entrypoint Pa path
343 Handle all the requests for the current virtual host using the
347 relative to the current document root.
348 .It Ic env Ar name Cm = Ar value
349 Set the environment variable
353 when executing CGI scripts.
354 Can be provided more than once.
355 .\" don't document the "spawn <prog>" form because it probably won't
357 .It Ic fastcgi Oo Ic tcp Oc Pa socket Oo Cm port Ar port Oc
360 instead of serving files.
363 can either be a UNIX-domain socket or a TCP socket.
364 If the FastCGI application is listening on a UNIX domain socket,
366 is a local path name within the
372 keyword must be provided and
374 is interpreted as a hostname or an IP address.
376 can be either a port number or the name of a service enclosed in
378 If not specified defaults to 9000.
379 .It Ic index Ar string
380 Set the directory index file.
381 If not specified, it defaults to
384 Specify the private key to use for this server.
387 should contain a PEM encoded private key.
388 This option is mandatory.
389 .It Ic lang Ar string
390 Specify the language tag for the text/gemini content served.
393 parameter will be added in the response.
394 .It Ic location Pa path Brq ...
395 Specify server configuration rules for a specific location.
398 argument will be matched against the request path with shell globbing
400 In case of multiple location statements in the same context, the first
401 matching location will be put into effect and the later ones ignored.
402 Therefore is advisable to match for more specific paths first and for
403 generic ones later on.
406 section may include most of the server configuration rules
408 .Ic alias , Ic cert , Ic cgi , Ic entrypoint , Ic env , Ic key ,
409 .Ic location , Ic param No and Ic proxy .
411 Enable or disable the logging for the current server or location block.
412 .It Ic param Ar name Cm = Ar value
419 Specify an OCSP response to be stapled during TLS handshakes
423 should contain a DER-format OCSP response retrieved from an
427 If the OCSP response in
429 is empty, OCSP stapling will not be used.
430 The default is to not use OCSP stapling.
431 .It Ic proxy Oo Cm proto Ar name Oc Oo Cm for-host Ar host : Ns Oo Ar port Oc Oc Brq ...
432 Set up a reverse proxy.
433 The optional matching rules
437 can be used to enable proxying only for protocols matching
442 and/or whose request IRI matches
446 .Pq 1965 by default .
447 Matching happens using shell globbing rules.
449 In case of multiple matching proxy blocks in the same context, the
450 first matching proxy will be put into effect and the later ones
456 Specify the client certificate to use when making requests.
458 Specify the client certificate key to use when making requests.
459 .It Ic protocols Ar string
460 Specify the TLS protocols allowed when making remote requests.
462 .Xr tls_config_parse_protocols 3
463 function for the valid protocol string values.
464 By default, both TLSv1.2 and TLSv1.3 are enabled.
465 .It Ic relay-to Ar host : Ns Op Ar port
466 Relay the request to the given
471 This is the only mandatory option in a
474 .It Ic use-tls Ar bool
475 Specify whether to use TLS when connecting to the proxied host.
477 .It Ic verifyname Ar bool
478 Enable or disable the TLS server name verification.
481 .It Ic root Pa directory
482 Specify the root directory for this server
483 .Pq alas the current Dq document root .
484 It's relative to the chroot if enabled.
485 .It Ic require Ic client Ic ca Pa path
486 Allow requests only from clients that provide a certificate signed by
487 the CA certificate in
489 It needs to be a PEM-encoded certificate and it's not relative to the
491 .It Ic strip Ar number
494 components from the beginning of the path before doing a lookup in the
496 It's also considered for the
498 parameter in the scope of a
502 When a request for an executable file matches the
504 rule, that file will be executed and its output fed to the client.
506 The CGI scripts are executed in the directory they reside and inherit
509 with these additional variables set:
511 .It Ev GATEWAY_INTERFACE
513 .It Ev GEMINI_DOCUMENT_ROOT
514 The root directory of the virtual host.
515 .It Ev GEMINI_SCRIPT_FILENAME
516 Full path to the CGI script being executed.
518 The full IRI of the request.
519 .It Ev GEMINI_URL_PATH
520 The path of the request.
522 The portion of the requested path that is derived from the the IRI
523 path hierarchy following the part that identifies the script itself.
525 .It Ev PATH_TRANSLATED
526 Present if and only if
529 It represent the translation of the
532 builds this by appending the
534 to the virtual host directory root.
536 The decoded query string.
537 .It Ev REMOTE_ADDR , Ev REMOTE_HOST
538 Textual representation of the client IP.
539 .It Ev REQUEST_METHOD
540 This is present only for RFC3875 (CGI) compliance.
541 It's always set to the empty string.
545 that identifies the current CGI script.
547 The name of the server
549 The port the server is listening on.
550 .It Ev SERVER_PROTOCOL
552 .It Ev SERVER_SOFTWARE
553 The name and version of the server, i.e.
556 The string "Certificate" if the client used a certificate, otherwise
559 The subject of the client certificate if provided, otherwise unset.
560 .It Ev TLS_CLIENT_ISSUER
561 The is the issuer of the client certificate if provided, otherwise
563 .It Ev TLS_CLIENT_HASH
564 The hash of the client certificate if provided, otherwise unset.
568 The TLS version negotiated with the peer.
570 The cipher suite negotiated with the peer.
571 .It Ev TLS_CIPHER_STRENGTH
572 The strength in bits for the symmetric cipher that is being used with
574 .It Ev TLS_CLIENT_NOT_AFTER
575 The time corresponding to the end of the validity period of the peer
576 certificate in the ISO 8601 format
577 .Pq e.g. Dq 2021-02-07T20:17:41Z .
578 .It Ev TLS_CLIENT_NOT_BEFORE
579 The time corresponding to the start of the validity period of the peer
580 certificate in the ISO 8601 format.
584 optionally supports FastCGI.
587 rule must be present in a server or location block.
588 Then, all requests matching that server or location will be handled
589 via the specified FastCGI backend.
591 By default the following variables
593 are sent, and carry the same semantics as with CGI.
594 More parameters can be added with the
632 TLS_CLIENT_NOT_BEFORE
637 To auto-detect the MIME type of the response
639 looks at the file extension and consults its internal table.
640 By default the following mappings are loaded, but they can be
641 overridden or extended using the
643 configuration option.
644 If no MIME is found, the value of
648 will be used, which is
649 .Dq application/octet-stream
652 .Bl -tag -offset indent -width 14m -compact
679 Messages and requests are logged by
683 facility or printed on
686 Requests are logged with the
689 Each request log entry has the following fields, separated by
694 Client IP address and the source port number, separated by a colon
706 Serve the current directory
707 .Bd -literal -offset indent
711 To serve the directory
713 and enable CGI scripts inside
715 .Bd -literal -offset indent
717 $ cat <<EOF > docs/cgi/hello
719 printf "20 text/plain\er\en"
722 $ chmod +x docs/cgi/hello
723 $ gmid -x '/cgi/*' docs
726 An X.509 certificate must be provided to run
728 using a configuration file.
729 First, the RSA certificate is created using a wildcard common name:
730 .Bd -literal -offset indent
731 # openssl genrsa \-out /etc/ssl/private/example.com.key 4096
732 # openssl req \-new \-x509 \e
733 \-key /etc/ssl/private/example.com.key \e
734 \-out /etc/ssl/example.com.crt \e
735 \-days 36500 \-nodes \e
736 \-subj "/CN=example.com"
737 # chmod 600 /etc/ssl/example.com.crt
738 # chmod 600 /etc/ssl/private/example.com.key
741 In the example above, a certificate is valid for one hundred years from
742 the date it was created, which is normal for TOFU.
744 The following is an example of a possible configuration for a site
745 that enables only TLSv1.3, adds a mime type for the file extension
747 and defines two virtual host:
748 .Bd -literal -offset indent
749 ipv6 on # enable ipv6
753 map "application/rtf" to-ext "rtf"
755 server "example.com" {
756 cert "/etc/ssl/example.com.crt"
757 key "/etc/ssl/private/example.com.key"
758 root "/var/gemini/example.com"
761 server "it.example.com" {
762 cert "/etc/ssl/example.com.crt"
763 key "/etc/ssl/private/example.com.key"
764 root "/var/gemini/it.example.com"
766 # enable cgi scripts inside "cgi-bin"
769 # set the language for text/gemini files
774 Yet another example, showing how to enable a
779 .Bd -literal -offset indent
783 server "example.com" {
784 cert "/path/to/cert.pem" # absolute path
785 key "/path/to/key.pem" # also absolute
786 root "/example.com" # relative to the chroot
788 location "/static/*" {
789 # load the following rules only for
790 # requests that matches "/static/*"
800 .Dq Flexible and Economical
801 UTF-8 decoder written by
802 .An Bjoern Hoehrmann .
807 program was written by
808 .An Omar Polo Aq Mt op@omarpolo.com .
812 All the root directories are opened during the daemon startup; if a
813 root directory is deleted and then re-created,
815 won't be able to serve files inside that directory until a restart.
816 This restriction only applies to the root directories and not their
819 a %2F sequence is indistinguishable from a literal slash: this is not
822 a %00 sequence is treated as invalid character and thus rejected.