Commit Graph

139 Commits

Author SHA1 Message Date
Omar Polo 89541eeec0 define TLS_VERSION, TLS_CIPHER and TLS_CIPHER_STRENGTH for CGI scripts 2021-04-13 06:59:54 +00:00
Omar Polo b8e64ccd44 list instead of fixed-size array for vhosts and locations
saves some bytes of memory and removes the limit on the maximum number
of vhosts and location blocks.
2021-03-31 16:32:18 +00:00
Omar Polo 7e1df73d7d fix mkdirs: create directories recursively 2021-03-31 13:56:58 +00:00
Omar Polo c836cdfadb handle CGI scripts that replies with the maximum header length allowed
the 1024 bytes limits is for the META only, not for the whole
response.  That means that the maximum size for the header line is
1029!
2021-03-29 09:42:06 +00:00
Omar Polo 62e001b067 move all sandbox-related code to sandbox.c
while there, add capsicum for the logger process
2021-03-20 08:42:08 +00:00
Omar Polo bc99d868bc refactoring: imsg everywhere
use imsg to handle ALL kinds of IPC in gmid.  This simplifies and shorten the
code, and  makes everything more uniform too.
2021-03-19 19:21:29 +00:00
Omar Polo 2c3e53dac6 give each server process its own socket for the executor
this fixes a bug introduced with the prefork mechanics: every server
process shared the same socket, and this would cause a race condition
when multiple server processes asked for a script cgi being executed.

This gives each server process its own socket to talk to the executor,
so the race cannot happen.
2021-03-03 17:22:01 +00:00
Omar Polo 376a540764
move log_init & vars to gmid.c, retain logger_main in log.c
this is to let the regression suite compile
2021-02-23 13:44:20 +01:00
Omar Polo 793835cb26
add `log on/off' to enable/disable logs per-location 2021-02-23 13:43:24 +01:00
Omar Polo d278a0c3c5
moving logging to its own process 2021-02-23 13:40:59 +01:00
Omar Polo 52418c8d82 fix various compilation errors
Include gmid.h as first header in every file, as it then includes
config.h (that defines _GNU_SOURCE for instance).

Fix also a warning about unsigned vs signed const char pointers in
openssl.
2021-02-12 12:47:20 +00:00
Omar Polo 9356f61a63 BUFSIZ is variable, we need *exactly* 1024
Using BUFSIZ in sbuf is not OK.  It's variable, and in various places
we assume that sbuf is 1024 (like handle_cgi_reply).  We could patch
those, but we aren't sure BUFSIZ is >= 1024!  Let's keep the hardcoded
number.

(found by debugging on arch on amd64, where BUFSIZ is bigger)
2021-02-12 11:56:13 +00:00
Omar Polo 27b2fa9ae5 don't mmap
Before we mmap(2) file for reading, and use a buffer to handle CGI
scripts.  Turns out, for sequential access over the whole mmap isn't
better than our loop on read.  This has also the additional advantage
that we can use handle_cgi (now handle_copy) for both files and CGI,
which is pretty cool.

This also fixes a nasty bug where we could hang a connection forever,
because we scheduled the wrong type of event (read on POLLOUT and
write on POLLIN, it's the other way around!)
2021-02-12 11:27:33 +00:00
Omar Polo 02be96c6dd add `require client ca' rule to require certs signed by a CA 2021-02-09 22:30:04 +00:00
Omar Polo d090dc8491 define config_path as global variable 2021-02-08 12:44:34 +00:00
Omar Polo abc007d2b3 rewrite main loop using libevent 2021-02-08 10:01:45 +00:00
Omar Polo b63e30ff44 define TLS_CLIENT_NOT_BEFORE/NOT_AFTER in CGI scripts 2021-02-07 21:47:01 +00:00
Omar Polo 9f006a2127 [cgi] split the query in words if needed and add them to the argv 2021-02-07 18:55:04 +00:00
Omar Polo 3abf91b0b4 improve logs management 2021-02-07 15:30:28 +00:00
Omar Polo a709ddf5eb added prefork option 2021-02-07 12:05:32 +00:00
Omar Polo e3ddf39095 add the ``entrypoint'' option 2021-02-06 18:28:43 +00:00
Omar Polo 6abda252e9 added ``block return'' and ``strip'' options 2021-02-06 17:22:37 +00:00
Omar Polo ca21e10043 reload configuration on SIGHUP 2021-02-04 13:23:15 +00:00
Omar Polo 4e2e2ab1d3 refactor executor_main
now it's symmetrical to listener_main().
2021-02-03 16:37:53 +00:00
Omar Polo d672b8fba1 refactoring startup logic 2021-02-03 16:28:00 +00:00
Omar Polo 9b8f5ed2c0 revert commit 346f28eeaa
keep mark_nonblock in utils.c, as otherwise the build for the regress
suite will fail (mark_nonblock needs fatal which is in gmid.c, and
we can't link gmid.o with the regress suite...)
2021-02-03 14:16:39 +00:00
Omar Polo 346f28eeaa move mark_nonblock to utils.c 2021-02-02 23:03:33 +00:00
Omar Polo fe40638928 mark various functions as static
By marking all those function as static, the compiler is free to do
more optimizations.  In addition, those functions are not used outside
server.c
2021-02-02 23:01:09 +00:00
Omar Polo 5f715ce43f print the header in the directory listing 2021-02-02 09:48:32 +00:00
Omar Polo 35744950aa simplify handle_cgi
Now that I got rid of the enum+switch, adding more state is easier.
Before, we used an hack to remember if we had read the CGI reply or
not (c->code = -1).

This introduces a new state, handle_cgi_reply that reads the CGI
script reply, logs it, and only then switches to handle_cgi.
handle_cgi itself is cleaner, now it only reads into c->sbuf and send
what it had red.

We even get, almost for free, the 42 error.  If read exists with -1 or
0 from in handle_cgi_reply, we return a proper error to the client.
We can extend this further in the future and also try to validate the
CGI reply (for now we're only looking for a \n).
2021-02-01 22:04:51 +00:00
Omar Polo 92da82858b document the DFA 2021-02-01 20:24:14 +00:00
Omar Polo 112802ea31 client state machine: function pointers instead of enum+switch 2021-02-01 20:00:33 +00:00
Omar Polo 2fafa2d23e bring the CGI implementation in par with GLV-1.12556 2021-02-01 11:11:43 +00:00
Omar Polo 6016a593a3 invert the location precedence: first match wins
It's how httpd(8) does it, and it allows us to call fnmatch less time
2021-01-30 12:04:20 +00:00
Omar Polo a2fd801327 puny_decode: set an error string 2021-01-29 17:11:03 +00:00
Omar Polo 35cf19e3f3 punycode: refactoring
use the correct term "label" instead of "component" when speaking
about the parts of a hostname.  Also, fix the sizes for max hostname
and label.
2021-01-28 16:25:59 +00:00
Omar Polo 473e4531d5 delete unused constant 2021-01-28 16:24:48 +00:00
Omar Polo ef0f2ae88c remove unused function 2021-01-28 14:21:57 +00:00
Omar Polo 13ed2fb63f improve configuration parsing error 2021-01-27 16:45:45 +00:00
Omar Polo fe5967cd02 const-ify strings in struct location 2021-01-27 15:53:30 +00:00
Omar Polo 44ee1bac8b use starts_with in puny.c 2021-01-27 15:35:09 +00:00
Omar Polo c4f682f855 trim_req_iri: set error string 2021-01-27 15:05:16 +00:00
Omar Polo 46af8c6cfe drop the daemon config in favour of the -f flag
Now it daemonize by default when running with a config, unless the -f
flag is given.
2021-01-27 12:04:37 +00:00
Omar Polo 7957cbd9aa const-ify puny_decode (and add puny.c) 2021-01-27 11:21:23 +00:00
Omar Polo 3300cbe06a initial punycode support 2021-01-27 10:47:49 +00:00
Omar Polo 8443bff77a rework the configless mode: change flags and generate certs 2021-01-25 14:08:31 +00:00
Omar Polo ae08ec7da5 chroot & drop privileges 2021-01-25 10:30:07 +00:00
Omar Polo 252908e6bb added support for location blocks 2021-01-24 18:53:26 +00:00
Omar Polo c8b7433918 added support for location blocks 2021-01-24 14:11:40 +00:00
Omar Polo 07b0a14218 void-ify some functions
their return value is no longer used, it's only confusing at this point.
2021-01-24 09:54:44 +00:00
Omar Polo a87f662565 refactoring state management
instead of having a flag to discern between two different behaviours
in S_SENDING, split that state into S_SENDING_FILE and S_SENDING_CGI
(this will also make it easier in the future to add other sending
states).  While there, also get rid of `goodbye' and make start_reply
advance the state machine by itself.
2021-01-24 09:49:09 +00:00
Omar Polo e7a2a99b5a added index option 2021-01-24 09:14:01 +00:00
Omar Polo f890c8c54d use a helper to handle no-body replies 2021-01-22 13:58:54 +00:00
Omar Polo 36162ed86d rename goodbye -> close_conn 2021-01-22 13:45:27 +00:00
Omar Polo f77a8c867e add ends_with 2021-01-21 22:45:49 +00:00
Omar Polo b2a6b61371 rename mimes to mime ; pass config esplicitly to mime* functions 2021-01-21 15:01:37 +00:00
Omar Polo 33ac26a0e5 fix prototypes 2021-01-21 14:49:11 +00:00
Omar Polo 12042ad700 add a configure script and some compat
tested on openbsd, alpine and void
2021-01-21 11:57:46 +00:00
Omar Polo 0be51733ef improve logs
now we log the full IRI requested (before was only the path) and the
response line (even for CGI).
2021-01-20 18:54:44 +00:00
Omar Polo 2debfe2b1f drop useless field
was probably added to distinguish between AF_INET and AF_INET6, to use
different functions for logging.  But it wasn't really used, and now
we use getnameinfo anyway.
2021-01-19 18:49:36 +00:00
Omar Polo 6119e13e8a moving "default type" from global options to server options 2021-01-19 11:28:41 +00:00
Omar Polo 05c23a54ea add "lang" server option 2021-01-19 10:58:29 +00:00
Omar Polo a010b0ddc5 move mimes into the struct config 2021-01-18 18:48:37 +00:00
Omar Polo 982069a120 add "mime" and "default type" option for the configuration 2021-01-18 18:43:47 +00:00
Omar Polo 0fbe79b33c improve mime handling
we still have an hardcoded list, but this implements the API needed to
modify the mappings.
2021-01-18 18:18:57 +00:00
Omar Polo d3a08f4d17 reorganize: move bunch of functions to server.c
cgi.c wasn't really needed; it better to group all the server related
functions together, cgi or not.  Now gmid.c contains only startup and
utility code.
2021-01-17 23:23:58 +00:00
Omar Polo 881a9dd9c2 split into two processes: listener and executor
this way, we can sandbox the listener with seccomp (todo) or capsicum
(already done) and still have CGI scripts.  When we want to exec, we
tell the executor what to do, the executor executes the scripts and
send the fd backt to the listener.
2021-01-16 19:41:34 +00:00
Omar Polo 7edc455ab4 move cgi stuff to its own file 2021-01-15 19:08:28 +00:00
Omar Polo 5bc3c98ed4 add protocols to the config 2021-01-15 18:55:05 +00:00
Omar Polo f7b816dc39 style 2021-01-15 15:21:51 +00:00
Omar Polo dafb57b8af sandbox also on FreeBSD with capsicum 2021-01-15 14:03:45 +00:00
Omar Polo 1590277007 conf & vhosts
* gmid.c (main): changed behaviour: daemon off by default
(main): changed -c in -C (cert option)
(main): changed -k in -K (key option, for consistency with -C)
(main): added -c to load a configuration
(main): certs, key and doc (-C -K and -d) doesn't have a default value anymore
(handle_handshake): add vhosts support
2021-01-15 09:17:43 +00:00
Omar Polo 9862b637c2 track handshakes
This alter the current state machine by adding S_HANDSHAKE as the
initial state.  There, we ensure we did the handshake and we check
SNI.  ATM we simply continue in S_OPEN, but later we can add virtual
host checks there, and skip to S_INITIALIZING with an error state if
the client is accessing a wrong host.
2021-01-13 18:40:18 +00:00
Omar Polo 0d7a38c4ce avoid __dead and return at the end of main 2021-01-11 15:28:25 +00:00
Omar Polo 796c6e75d7 fix build on non-OpenBSD systems
not every system has a __dead attribute for functions.  This fixes the
build on FreeBSD.
2021-01-11 15:16:50 +00:00
Omar Polo 7b1d979032 reject non-gemini protocols with 53 2021-01-11 14:45:57 +00:00
Omar Polo 3c1cf9d07c s/uri/iri since we accept IRIs 2021-01-11 13:08:00 +00:00
Omar Polo 28778244d6 fix build 2021-01-11 12:53:46 +00:00
Omar Polo 33756bd235 accept both ipv4 and ipv6 2021-01-10 22:56:33 +00:00
Omar Polo 709d6e5ead log also the port of the client 2021-01-10 22:29:22 +00:00
Omar Polo 9b374f41ee macro reordering
and while there replace SAFE_SETENV with an inline function.  LOG is
more difficult to transform into an inline function, given the string
concatenations it does.  The other LOG* and FATAL macros are fine as
they already are.
2021-01-10 14:40:14 +00:00
Omar Polo ef04b55160 switch to Bjoern Hoehrmann UTF-8 decoder
It's correct, while my hacked valid_multibyte_utf8 would allow things
that aren't technically UTF8.
2021-01-09 20:32:23 +00:00
Omar Polo 6c6c7a0ed7
rename cgi_setpoll_on_* to cgi_poll_on_* 2020-12-28 18:01:06 +01:00
Omar Polo 3d9a1c73de
fixing prototype order 2020-12-28 09:57:58 +01:00
Omar Polo 4a28dd014a
ignore also SIGHUP
SIGHUP is sent when the tty is detached and by default kills the
process.  When we run in the background we don't care anymore about
the tty, so it should be safe for us to ignore SIGHUP.  (frankly,
I expected daemon(3) to do stuff like this for us).
2020-12-28 09:55:56 +01:00
Omar Polo 6d91731552
fmt 2020-12-27 13:27:15 +01:00
Omar Polo 33d32d1fd6
implement a valid RFC3986 (URI) parser
Up until now I used a "poor man" approach: the uri parser is barely a
parser, it tries to extract the path from the request, with some minor
checking, and that's all.  This obviously is not RFC3986-compliant.

The new RFC3986 (URI) parser should be fully compliant.  It may accept
some invalid URI, but shouldn't reject or mis-parse valid URI.  (in
particular, the rule for the path is way more relaxed in this parser
than it is in the RFC text).

A difference with RFC3986 is that we don't even try to parse the
(optional) userinfo part of a URI: following the Gemini spec we treat
it as an error.

A further caveats is that %2F in the path part of the URI is
indistinguishable from a literal '/': this is NOT conforming, but due
to the scope and use of gmid, I don't see how treat a %2F sequence in
the path (reject the URI?).
2020-12-25 13:13:12 +01:00
Omar Polo d5aba4c791
added missing copyright stuff 2020-12-24 16:49:55 +01:00
Omar Polo 488f059ac4
moving declarations to header file 2020-12-24 16:48:39 +01:00