Commit Graph

122 Commits

Author SHA1 Message Date
Omar Polo a555e0d67b copyright years 2022-07-04 09:48:39 +00:00
Omar Polo f2f8eb35c8 encode file names in the directory index
Spotted the hard way by cage
2022-07-04 09:31:36 +00:00
Omar Polo 3bd4a6dea0 log when it fails to open a file because of permissions 2022-07-04 08:25:15 +00:00
Omar Polo ea27eaaa83 fix an out-of-bound access in start_cgi
Long time ago, client->req was a static buffer so the memcpy was safe.
However, it's been since moved to a dynamically allocated string, so
it's very often smaller than sizeof(req.buf) (1024), hence the out of
bound access which results in a SIGSEGV very often on OpenBSD thanks to
Otto' malloc.

The situation with the iri parser, client->req and how the request is
forwarded to the other process needs to be improved: this is just a fix
to address the issue quickly, a better one would be to restructure the
iri parser APIs and rethink how the info is forwarded to the ex process.
2022-03-27 12:52:59 +00:00
Omar Polo 3fdc457c8d swap try_client_by_id with client_by_id
i.e. allow client_by_id to fail and return NULL.

Initially I thought it was a good idea to shut down a server process
if we receive an invalid client id as reply from one of our requests
to the executor process.  This turned out not to be correct since a
client can (read: will) disconnect in the delay beteewn we acknowledge
their request and the cgi script execution.

The fastcgi and proxy handler already handled this situation, so
they're unaffected.

This allows an attacker to make gmid unresponsible by just making
enough requests until they hit the right timing.
2022-03-26 11:32:26 +00:00
Omar Polo d98ae929b2 don't log errno, it's always zero after libtls returns
The libevent error value is much more interesting!
see github issue #13
2022-02-19 18:11:05 +00:00
Omar Polo e0f6dc646d improve proxy error path
properly release everything when during client_close if the request
was managed by a proxy.
2022-01-27 09:55:52 +00:00
Omar Polo d28bd963c2 always mark requests as done when their code is != 20 2022-01-27 09:54:48 +00:00
Omar Polo b9b77f5344 fix comment 2022-01-27 09:28:27 +00:00
Omar Polo 901905e0cf bail out of client_read if we've already decide what to do
libevent2 can still somehowe call client_read even in code paths
that never enable reading from the evbuffer.  Can't reproduce on
the libevent in base on OpenBSD.  It's a bit ugly, but it's a small
workaround for something that otherwise *always* make gmid crash
when linked against libevent2.  (client_read works under the
assumption that c->host != NULL, matched_proxy crashes otherwise.)
2022-01-05 18:58:01 +00:00
Omar Polo 876a417023 tweak comment 2022-01-05 18:03:47 +00:00
Omar Polo d474a97922 add missing prototype 2022-01-04 23:15:13 +00:00
Omar Polo ba94a608a8 add `require client ca' for proxy blocks
refactor the code that calls validate_against_ca into an helper
function to reuse it in both apply_require_ca and (optionally) in
apply_reverse_proxy.
2022-01-04 23:14:34 +00:00
Omar Polo b7967bc1f6 proxy: allow multiple proxy blocks, matching options and validations
as a side effect the order of the content of a server block is relaxed:
options, location or proxy blocks can be put in any order.
2022-01-02 16:33:28 +00:00
Omar Polo 593e412b49 allow to disable TLS when proxying requests 2022-01-01 20:16:14 +00:00
Omar Polo 7bdcc91ec7 simplify the proxying code
it doesn't make any sense to keep the proxying info per-location:
proxying only one per-vhost.  It can't work differently, it doesn't make
sense anyway.
2022-01-01 17:08:39 +00:00
Omar Polo d49093c105 support optional client certificate for proxy rule 2022-01-01 16:33:44 +00:00
Omar Polo 6a6b4a2a98 typo 2021-12-29 20:36:54 +00:00
Omar Polo 72b033ef18 add ability to proxy requests
Add to gmid the ability to forwad a request to another gemini server and
thus acting like a reverse proxy.  The current syntax for the config
file is

	server "example.com" {
		...
		proxy relay-to host:port
	}

Further options (like the use of custom certificates) are planned.

cf. github issue #7
2021-12-29 20:36:54 +00:00
Omar Polo 52c92ef680 relax the "wont proxy request" check: don't check the port number
Don't refuse to serve the request if the port number doesn't match the
one we're listening on, as initially suggested by Allen Sobot.

Complex setup may have a gmid instance reachable from multiple ports and
the meaning of the check in the first places was to avoid tricking
clients into thinking that we're serving for those domains: the port
number is way less important than the schema or domain name.

In the long run, the best way would probably to add a `listen on'
keyword for the servers blocks, just like OpenBSD' httpd, but gmid can't
listen on multiple ports/interfaces yet
2021-12-09 20:59:05 +00:00
Omar Polo 4842c72d9f fmt 2021-10-18 10:05:55 +00:00
Omar Polo 8044493865 move bufferevent initialization early in handle_handshake
the error path needs an initialized bufferevent too, otherwise it'll
crash when trying to write the response.

This moves the initialisation early, right after the tls_handshake.
Another option would be to initialise it in do_accept, but that may be
too early.
2021-10-15 07:46:30 +00:00
Omar Polo c62a411f4f don't die on ECONNABORTED
ECONNABORTED is returned if a connections gets aborted after being
queued before the accept(2).  I had some cases of

	accept: Software caused connection abort

on FreeBSD, this should avoid that.
2021-10-13 20:49:58 +00:00
Omar Polo 5eb3fc905f don't work around a missing -Wno-unused-parameter
It's been there for a long time, and it's frankly annoying to pretend
to use parameters.  Most of the time, they're there to satisfy an
interface and nothings more.
2021-10-09 18:54:41 +00:00
Omar Polo 207b3e80d8 Store clients inside a splay tree
From day one we've been using a static array of client struct to hold
the clients data.  This has variuos drawbacks, among which:

 * reuse of the storage  ("shades of heartbleed")
 * maximum fixed amount of clients connected at the same time
 * bugs are harder to debug

The last point in particular is important because if we mess the client
ids, or try to execute some functions (e.g. the various fcgi_*) after a
client has been disconnected, it's harder to "see" this "use after
free"-tier kind of bug.

Now I'm using a splay tree to hold the data about the live connections.
Each client' data is managed by malloc.  If we try to access a client
data after the disconnection we'll probably crash with a SIGSEGV and
find the bug is more easy.  

Performance-wise the connection phase should be faster since we don't
have to loop anymore to find an empty spot in the clients array, but
some operations could be slightly slower (compare the O(1) access in an
array with a SPLAY_FIND operation -- still be faster than O(n) thought.)
2021-10-07 11:20:34 +00:00
Omar Polo 4cd2520965 one FastCGI connection per client
FastCGI is designed to multiplex requests over a single connection, so
ideally the server can open only one connection per worker to the
FastCGI application and that's that.

Doing this kind of multiplexing makes the code harder to follow and
easier to break/leak etc on the gmid side however.  OpenBSD' httpd
seems to open one connection per client, so why can't we too?

One connection per request is still way better (lighter) than using
CGI, and we can avoid all the pitfalls of the multiplexing (keeping
track of "live ids", properly shut down etc...)
2021-10-07 10:47:02 +00:00
Omar Polo e4daebe44a plug a memory leak
c->req is set in client_read but never deallocated
2021-10-06 17:38:37 +00:00
Omar Polo 807a80cb9e fmt 2021-10-06 16:36:31 +00:00
Omar Polo acafce5b7d libevent2 fix: unfreeze the client evbuffer
libevent2 has this concept of "freezeness" of a buffer.  It's a way to
avoid accidentally write/remove data from the wrong "edge" of the
buffer.  The client_tls_{read,write} functions need to add/drain data
from the opposite edge, hence the need for the unfreeze call.

This is the minimum change in order to work on libevent2 too.  Another
way would be to define evbuffer_{un,}freeze as NOP on libevent 1, but
it's ugly IMHO.
2021-10-02 17:20:56 +00:00
Omar Polo efe7d18029 new I/O handling on top of bufferevents
This is a big change in how gmid handles I/O.  Initially we used a
hand-written loop over poll(2), that then was evolved into something
powered by libevent basic API.  This meant that there were a lot of
small "asynchronous" function that did one step, eventually scheduling
the re-execution, that called each others in a chain.

The new implementation revolves completely around libevent'
bufferevents.  It's more clear, as everything is implemented around the
client_read and client_write functions.

There is still space for improvements, like adding timeouts for one, but
it's solid enough to be committed as is and then further improved.
2021-10-02 17:20:56 +00:00
Omar Polo 741b69be96 fastcgi completely asynchronous
This changes the fastcgi implementation from a blocking I/O to an
async implementation on top of libevent' bufferevents.

Should improve the responsiveness of gmid especially when using remote
fastcgi applications.
2021-09-26 17:00:07 +00:00
Omar Polo 83fe545a2b initialize mbufhead 2021-09-26 16:43:19 +00:00
Omar Polo 3571854e94 fix possible out-of-bound access
While computing the parent directory it an out-of-bound access can
occur, which usually means the server process dies.

In particular, it can be triggered by making a request for a
non-existent file in the root of a virtual host if the path matches
the `cgi` pattern.

Thanks cage for helping in debugging!
2021-09-24 10:48:51 +00:00
Omar Polo 353e3c8ebe style 2021-09-24 08:16:28 +00:00
Omar Polo a91ad7f2ff drop unnecessary bzero
the whole struct client is already memset'd to 0 in do_accept.
handle_handshake doesn't touch the request or iri buffer in the code
path that leads to handle_open_conn.  (It does so in the error router
alone.)
2021-09-24 08:08:49 +00:00
Omar Polo 79288c8b60 making more explicit the case of missing SNI
Missing SNI (i.e. servname == NULL) is already handled correctly.
puny_decode refuses to work on NULL servname, c->domain is still the
empty string and everything flows as expected towards the error at the
end.  However, it's better to bail out early and make more explicit
how the case of missing SNI is handled.
2021-09-24 07:40:24 +00:00
Omar Polo efb48052dc relax openat rule: follow symlinks
O_NOFOLLOW acts only on *the last component*, so on
open("/foo/bar/baz") only when baz is a symlink open fails.
Checking every path component is not viable.

gh issue #5 related (sort of)
2021-07-27 09:21:42 +00:00
Omar Polo a8a1f43921 style(9)-ify 2021-07-07 09:46:37 +00:00
Omar Polo 090b8a89fa gracefully shut down fastcgi backends
we need to delete the events associated with the backends, otherwise
the server process won't ever quit.

Here, we add a pending counter to every backend and shut down
immediately if they aren't handling any client; otherwise we try to
close them as soon as possible (i.e. when they close the connection to
the last connected client.)
2021-07-06 10:54:27 +00:00
Omar Polo 1b78bd563a strncpy -> strlcpy
quoting strncpy(3)

     strncpy() only NUL terminates the destination string when the
     length of the source string is less than the length parameter.

strlcpy is more intuitive.

this is another warning gcc 8 found that clang didn't.
2021-06-16 15:06:10 +00:00
Omar Polo 24d362cd67 explicitly use c->fd instead of fd
Yep, fd should be the file descriptor, but for lazyness when manually
calling the function sometimes we supply 0 as fd and event.  Instead of
fixing the usage, do as other of such functions do in this
circumstances: use c->fd.
2021-06-12 13:42:43 +00:00
Omar Polo 89c88caa3c mark backend as FCGI_READY when getting a fd
otherwise clients will remain stuck waiting for a pending request that
doesn't exist (see apply_fastcgi switch.)
2021-06-12 13:41:33 +00:00
Omar Polo 1feaf2a618 use the correct document root
pass the correct loc_off to the executor, so the various variables
that depends on the matched location (like DOCUMENT_ROOT) are computed
correctly.
2021-05-15 10:31:43 +00:00
Omar Polo 91b9f2a8f9 const-ify strip_path 2021-05-15 10:07:21 +00:00
Omar Polo 571d20fbb3 fmt 2021-05-15 10:04:58 +00:00
Omar Polo 8ad1c57024 fastcgi: a first implementation
Not production-ready yet, but it's a start.

This adds a third ``backend'' for gmid: until now there it served
local files or CGI scripts, now FastCGI applications too.

FastCGI is meant to be an improvement over CGI: instead of exec'ing a
script for every request, it allows to open a single connection to an
``application'' and send the requests/receive the responses over that
socket using a simple binary protocol.

At the moment gmid supports three different methods of opening a
fastcgi connection:

 - local unix sockets, with: fastcgi "/path/to/sock"
 - network sockets, with: fastcgi tcp "host" [port]
   port defaults to 9000 and can be either a string or a number
 - subprocess, with: fastcgi spawn "/path/to/program"
   the fastcgi protocol is done over the executed program stdin

of these, the last is only for testing and may be removed in the
future.

P.S.: the fastcgi rule is per-location of course :)
2021-05-09 18:23:36 +00:00
Omar Polo 737a6b50c5 ensure %p (path) is always absolute
with the recent changes, sometimes the path may not start with a '/'.
This ensures that %s is ALWAYS an absolute path.
2021-04-30 19:07:37 +00:00
Omar Polo fdea6aa0bc allow ``root'' rule to be specified per-location block 2021-04-30 17:16:34 +00:00
Omar Polo cc8c2901ad added ``alias'' option to define hostname aliases for a server 2021-04-29 18:23:35 +00:00
Omar Polo e76f2c74b8 don't save the directory fd in c->pfd
scandir_fd already calls closedir, which in turns closes the fd
2021-04-25 12:19:06 +00:00