Commit Graph

178 Commits

Author SHA1 Message Date
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
Omar Polo 11c986679a sort the auto index alphabetically 2021-04-25 12:06:54 +00:00
Omar Polo 74c0c7e4ce rename reschedule_* to yield_* 2021-04-20 09:40:09 +00:00
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 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 4604dc9671
move vhost_should_log call to server.c
log.o is linked to some regress/ stuff.  Calling from there a vhost_*
function means that we should link the regress/stuff to server.o too
(and that would pull in other stuff...).  Moving the call is easier,
and also probably better.
2021-02-23 13:43:33 +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 6b191ed52a
tests and compat for imsg 2021-02-23 13:43:14 +01:00
Omar Polo c39b26d308 mark reschedule_write inline & static 2021-02-12 20:25:48 +00:00
Omar Polo eecad7a3ca other s/fnmatch/matches 2021-02-12 19:51:54 +00: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 3cb3dd4d42 accept4 -> accept
accept4(2) isn't part of any standard (even though it'll be part in
the future) and raises warnings on some linux distro.  Moreover, we
don't have thread that may fork at any time, so doing a mark_nonblock
after isn't a big deal.
2021-02-12 11:59:03 +00:00
Omar Polo 5e3285d52e typo 2021-02-12 11:34:17 +00:00
Omar Polo 98ee8406aa fix occurrence of (killed) load_file 2021-02-12 11:32:49 +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 a6e689d745 fix config reload
the old server processes would stick around waiting on the signals
events.  While there, also drop the `struct server_events' and define
events as globals.
2021-02-12 08:50:25 +00:00
Omar Polo 49b73ba1ab fix "first location" bug
reported by devel at datenbrei dot de.  The first location would
overwrite the default value for a server, triggering the "`foo' rule
specified more than once" error.  This also needed a small tweak on
how we match locations to avoid breaking other tests.
2021-02-10 16:37:08 +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 57ec3e776e refactor apply_block_return
move the strip and fmt logic to their own function
2021-02-08 20:50:30 +00:00
Omar Polo df58efff26 fix seccomp for the new event loop
add/remove syscalls from the BPF filter and move sandbox() after
libevent initialisation
2021-02-08 12:46:46 +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 3077ce5bee don't fprintf 2021-02-07 16:10:09 +00:00
Omar Polo 3abf91b0b4 improve logs management 2021-02-07 15:30:28 +00:00
Omar Polo cfb8a77fd4 handle also EAGAIN together with EWOULDBLOCK 2021-02-07 12:04:11 +00:00
Omar Polo e3ddf39095 add the ``entrypoint'' option 2021-02-06 18:28:43 +00:00
Omar Polo cd76162494 swap check in vhost_* fns
it's faster (statistically speaking) to first compute if the option is
set and then fnmatch than the inverse.  This way we can avoid
unnecessary fnmatch.
2021-02-06 17:31:03 +00:00
Omar Polo 6abda252e9 added ``block return'' and ``strip'' options 2021-02-06 17:22:37 +00:00
Omar Polo daac4a9452 fix auto index precedence 2021-02-06 14:36:26 +00:00
Omar Polo ca21e10043 reload configuration on SIGHUP 2021-02-04 13:23:15 +00:00
Omar Polo 1e3ef7ab4f use upper bound given by poll
it's a waste to loop through all fds.  We know the *exact* number of
clients that needs attention, so use that information to limit the
looping.
2021-02-03 21:14:48 +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 87f2b68b58 cgi now follows globbing rules 2021-02-02 22:38:35 +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 b06f80cdf4 switch to handle_open_conn right after handshake
So we don't re-enter the handle_handsahke and re-do the loop on
fnmatch etc.  This way, once we're successfully past the handshake,
we'll re-enter no handle_open_conn.
2021-02-01 20:27:08 +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 b59f3cdd27 typo 2021-01-30 12:12:37 +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 a8d4a89770 don't ignore punycode errors when decoding SNI-provided servname 2021-01-29 17:29:14 +00:00
Omar Polo a2fd801327 puny_decode: set an error string 2021-01-29 17:11:03 +00:00
Omar Polo 90cb9eea8a don't log the SNI & matching
I'll re-enable this when i'll improve the logging
2021-01-28 16:28:44 +00:00
Omar Polo 22c6d6334d log info about SNI, punycode and matched vhost 2021-01-27 15:06:15 +00:00
Omar Polo caad03081b some null checks 2021-01-27 15:05:50 +00:00
Omar Polo c4f682f855 trim_req_iri: set error string 2021-01-27 15:05:16 +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 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 3309ef975c accumulate the whole response line for CGI scripts 2021-01-23 15:32:38 +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 d1ca3911d2 fix redirects
make sure redirect starts with /.  This also requires a tweak in
check_path, in the case we go open_file -> send_dir -> open_file ->
check-path and the path starts with a slash.
2021-01-21 22:44:41 +00:00
Omar Polo 06f233ad8a ensure we're in the correct state 2021-01-21 19:46:36 +00:00
Omar Polo b4d409cf29 use fnmatch on the domain name 2021-01-21 09:07:26 +00:00
Omar Polo ce79c944bc simplify flow when SNI is missing
this also gives a chance for users to use the default host even if SNI
is missing.
2021-01-21 08:46:07 +00:00
Omar Polo 0ab65593e2 don't crash on wrong vhost or missing SNI
the new logging code was crashing if the client didn't support SNI or
if required an unknown vhost: this because we short-circuit in
handle_handshake to an error, so c->iri isn't populated yet (we don't
even read the request).

fixes #1
2021-01-21 08:26:21 +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 df79b4c1d5 rename reason to meta 2021-01-19 11:00:06 +00:00
Omar Polo 05c23a54ea add "lang" server option 2021-01-19 10:58:29 +00:00
Omar Polo 132cae8c6f removing err/warn functions with our fatal
err/warn is not available on some systems (unfortunately!) and in any
case don't play well with our daemon mode (that closes std{in,out,err}).
Use our fatal that is daemon-aware.
2021-01-18 18:15:58 +00:00
Omar Polo f87bf597ea removing O_CLOEXEC
we don't fork anymore in that process, so the flag is extra.
2021-01-17 23:25:59 +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