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 h
24 .Op Fl c Ar cert.pem
25 .Op Fl d Ar docs
26 .Op Fl k Ar key.pem
27 .Op Fl l Ar logfile
28 .Op Fl p Ar port
29 .Op Fl x Ar cgi-bin
30 .Ek
31 .Sh DESCRIPTION
32 .Nm
33 is a very simple and minimal gemini server that can serve static files
34 and execute CGI scripts.
35 .Pp
36 .Nm
37 will strip any sequence of
38 .Pa ../
39 or trailing
40 .Pa ..
41 in the requests made by clients and will refuse to follow symlinks.
42 Furthermore, on
43 .Ox ,
44 .Xr pledge 2
45 and
46 .Xr unveil 2
47 are used to ensure that
48 .Nm
49 dosen't do anything else than read files from the given directory,
50 accept network connections and, optionally, execute CGI scripts.
51 .Pp
52 It should be noted that
53 .Nm
54 is very simple in its implementation, and so it may not be appropriate
55 for serving sites with lots of users.
56 After all, the code is single threaded and use a single process,
57 although it can handle multiple requests concurrently.
58 .Pp
59 If a user request path is a directory,
60 .Nm
61 will try to serve a
62 .Pa index.gmi
63 file inside that directory.
64 .Pp
65 The options are as follows:
66 .Bl -tag -width 12m
67 .It Fl c Ar cert.pem
68 The certificate to use, by default is
69 .Pa cert.pem .
70 .It Fl d Ar docs
71 The root directory to serve.
72 .Nm
73 won't serve any file that is outside that directory.
74 By default is
75 .Pa docs .
76 .It Fl h
77 Print the usage and exit.
78 .It Fl k Ar key.pem
79 The key for the certificate, by default is
80 .Pa key.pem .
81 .It Fl l Ar logfile
82 log to the given file instead of the standard error.
83 .It Fl p Ar port
84 The port to bind to, by default 1965.
85 .It Fl x Ar dir
86 Enable execution of CGI scripts inside the given directory (relative
87 to the document root.) Cannot be provided more than once.
88 .El
89 .Sh CGI
90 When CGI scripts are enabled for a directory, a request for an
91 executable file will execute it and fed its output to the client.
92 .Pp
93 The CGI scripts will inherit the environment from
94 .Nm
95 with these additional variables set:
96 .Bl -tag -width 18m
97 .It Ev SERVER_SOFTWARE
98 "gmid"
99 .It Ev SERVER_PORT
100 "1965"
101 .It Ev SCRIPT_NAME
102 The (public) path to the script.
103 .It Ev SCRIPT_EXECUTABLE
104 The full path to the executable.
105 .It Ev REQUEST_URI
106 The user request (without the query parameters.)
107 .It Ev REQUEST_RELATIVE
108 The request relative to the script.
109 .It Ev QUERY_STRING
110 The query parameters.
111 .It Ev REMOTE_HOST
112 The remote IP address.
113 .It Ev DOCUMENT_ROOT
114 The root directory being served, the one provided with the
115 .Ar d
116 parameter to
117 .Nm
118 .El
119 .Pp
120 Let's say you have a script in
121 .Pa /cgi-bin/script
122 and the user request is
123 .Pa /cgi-bin/script/foo/bar?quux .
124 Then
125 .Ev SCRIPT_NAME
126 will be
127 .Pa /cgi-bin/script ,
128 .Ev SCRIPT_EXECUTABLE
129 will be
130 .Pa $DOCUMENT_ROOT/cgi-bin/script ,
131 .Ev REQUEST_URI
132 will be
133 .Pa /cgi-bin/script/foo/bar ,
134 .Ev REQUEST_RELATIVE
135 will be
136 .Pa foo/bar and
137 .Ev QUERY_STRING
138 will be
139 .Ar quux .
140 .Sh EXAMPLES
141 To quickly getting started
142 .Bd -literal -offset indent
143 $ # generate a cert and a key
144 $ openssl req -x509 -newkey rsa:4096 -keyout key.pem \\
145 -out cert.pem -days 365 -nodes
146 $ mkdir docs
147 $ cat <<EOF > docs/index.gmi
148 # Hello world
149 test paragraph...
150 EOF
151 $ gmid -c cert.pem -k key.pem -d docs
152 .Ed
153 .Pp
154 Now you can visit gemini://localhost/ with your preferred gemini
155 client.
156 .Pp
157 To add some CGI scripts, assuming a setup similar to the previous
158 example, you can
159 .Bd -literal -offset indent
160 $ mkdir docs/cgi-bin
161 $ cat <<EOF > docs/cgi-bin/hello-world
162 #!/bin/sh
163 printf "20 text/plain\\r\\n"
164 echo "hello world!"
165 EOF
166 $ gmid -x cgi-bin
167 .Ed
168 .Pp
169 Note that the argument to the
170 .Fl x
171 option is
172 .Pa cgi-bin
173 and not
174 .Pa docs/cgi-bin ,
175 since it's relative to the document root.
176 .Sh CAVEATS
177 .Bl -bullet
178 .It
179 it doesn't support virtual hosts: the host part of the request URL is
180 completely ignored.
181 .It
182 it doesn't fork in the background or anything like that.
183 .El