Commit Graph

141 Commits

Author SHA1 Message Date
Omar Polo 7600099513 optionally disable the sandbox on some systems
The FreeBSD and Linux' sandbox can't deal with `fastcgi' and `proxy'
configuration rules: new sockets needs to be opened and it's either
impossible (the former) or a huge pain in the arse (the latter).

The sandbox is still always used in case only static files are served.
2022-09-06 16:40:38 +00:00
Omar Polo d29a2ee224 get rid of the CGI support
I really want to get rid of the `executor' process hack for CGI scripts
and its escalation to allow fastcgi and proxying to work on non-OpenBSD.

This drops the CGI support and the `executor' process entirely and is
the first step towards gmid 2.0.  It also allows to have more secure
defaults.

On non-OpenBSD systems this means that the sandbox will be deactivated
as soon as fastcgi or proxying are used: you can't open sockets under
FreeBSD' capsicum(4) and I don't want to go thru the pain of making it
work under linux' seccomp/landlock.  Patches are always welcome however.

For folks using CGI scripts (hey, I'm one of you!) not all hope is lost:
fcgiwrap or OpenBSD' slowcgi(8) are ways to run CGI scripts as they were
FastCGI applications.

fixes for the documentation and to the non-OpenBSD sandboxes will
follow.
2022-09-06 16:11:09 +00:00
Omar Polo 54203115cd don't load the built-in list when using `types' 2022-04-08 13:52:35 +00:00
Omar Polo d8d170aa5e allow add_mime to fail
add_mime nows allocate dinamically copies of the passed strings, so
that we can actually free what we parse from the config file.

This matters a lot especially with lengthy `types' block: strings that
reach the internal mapping are never free'd, so every manual addition
is leaked.
2022-04-08 13:44:49 +00:00
Omar Polo 6468868fee print a deprecation message for the map rule 2022-04-07 16:15:55 +00:00
Omar Polo e5d82d9472 const-ify some tables
matches found with

	% grep -R '=[	 ]*{' . | fgrep -v const
2022-03-19 11:02:42 +00:00
Omar Polo ee219d702e add type { ... } block to define mime types mapping
The `map' rule is powerful but quite annoying to use if you have/need
lots of entries (and clutters the configuration file too.)

The `type' block is blatantly stolen from httpd(8) and allows for a way
more nice usage:

	type {
		include "/usr/share/misc/mime.types"
	}

or even

	type {
		text/markdown		md markdown
		text/x-perl		pl pm
		# ...
	}
2022-02-26 14:00:20 +00:00
Omar Polo 88971f9a4e add missing token include to the list of tokens 2022-02-26 13:49:24 +00:00
Anna “CyberTailor” 1f1f381068 include gmid.h before other headers to get all the prototypes
> implicit declaration of function 'asprintf'; did you mean 'vsprintf'?
2022-02-03 11:13:45 +01:00
Omar Polo 1cdea97b6c allow using a custom hostname for SNI during proxying
add a `sni' option for the `proxy' block: the given name is used instead
of the one extracted by the `relay-to' rule.
2022-01-30 10:14:44 +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 e2f167afb3 bump copyright year :) 2022-01-02 15:50:14 +00:00
Omar Polo 593e412b49 allow to disable TLS when proxying requests 2022-01-01 20:16:14 +00:00
Omar Polo 294a57275a move %token USER to its own line 2022-01-01 19:51:53 +00:00
Omar Polo 3c4b712bb2 plug some memory leaks in config parsing
I forgot to free some strings when they're no more used.
2022-01-01 19:10:00 +00:00
Omar Polo c7c8ef448b add `protocols' option to `proxy' rule 2022-01-01 19:04:50 +00:00
Omar Polo 5128c0b0e3 add `verifyname' option for `proxy' rule 2022-01-01 18:50:10 +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 da2185f37f move proxy sub-options to their own rule and allow grouping with { ... } 2022-01-01 16:33:40 +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
Stephen Gregoratto ff05125eb8 Implement OCSP stapling support
Currently dogfooding this patch at gemini.sgregoratto.me. To test,
run the following command and look for the "OCSP response" header:

 openssl s_client -connect "gemini.sgregoratto.me:1965" -status
2021-10-15 09:58:23 +02:00
Omar Polo f0a01fc742 two -n to dump the parsed configuration
This adds a barebone dumping of the parsed configuration.  It is not
complete, but I'm interested in dumping the full path to `cert' and
`key' in order to write some scripts that can inspect the
configuration, extract the certificates and renew them when expired
automatically.

It's not easy to parse gmid configuration otherwise because the syntax
is flexible and users can use macros.  Instead, the idea is to run
gmid and let it dump the configuration once it's been parsed in a
static and predictable format.

Now is possible to parse gmid configuration with, say, awk or perl.
2021-10-09 21:40:55 +00:00
Omar Polo 83272dfee9 don't yyerror if we can't open the file
the global var file could still be NULL and yyerror would crash.
2021-08-23 22:42:59 +00:00
Omar Polo abc8801d60 typo 2021-07-19 07:45:55 +00:00
Omar Polo c74c70304d drop leading T from token names 2021-07-19 07:39:13 +00:00
Omar Polo 9a818eebc1 sort tokens 2021-07-19 07:34:29 +00:00
Anna “CyberTailor” f3966209e5 contrib/vim: add Syntastic integration
Error and warning messages are prefixed with "error: " and "warning: "
correspondingly to ease integration with automated tooling.

`yywarn' function added. Off-by-one line numbers in warnings are fixed.

Two error messages are reworded to avoid repeating like
"error: error in server directive" or "error: syntax error".
2021-07-13 13:38:42 +02:00
Omar Polo 391825e360 move parse_portno to gmid.c
it's used only to parse the -p flag.  While there add check_port_num
to check the range for the port.
2021-07-09 13:06:58 +00:00
Omar Polo e3b2a0f8de missing newline in deprecation notice 2021-07-09 12:57:58 +00:00
Omar Polo 1bd706dc41 drop unsigned
it causes a cascade of warnings on some architectures.  Also, for what
we're doing here, the signed/unsigned property isn't important.
2021-07-09 12:56:59 +00:00
Omar Polo 67f494057a @-macros, rollback changes to strings and optional semicolons
* expand $-macros as string, only the new @-macros get expanded as-is
* rollback changes to characters allowed in bare strings
* optional semicolons in optnl, useful for readable @-macros
2021-07-09 12:49:15 +00:00
Omar Polo c39be742cf parsing: bring lots of goodies from OpenBSD' parse.y
This allows to solve the problem with the \n in the grammar (before
two following macro declaration were treated as invalid.  This also
brings in a nice `include' keyword.
2021-07-09 11:25:25 +00:00
Omar Polo efacb859a7 change (again) the env/param separator: use '='
Given that env/param are new features of this release, no support for
the "old" syntax is needed.
2021-07-09 07:27:15 +00:00
Omar Polo 762b9b991f add => in env/param and `port' between hostname and port for fastcgi
In the same spite of the last commit, add the missing separators
between strings to avoid the auto-concat pitfalls.  `=>' is used to
separate between `env' and `param' arguments, while for `fastcgi' the
keyword `port' is required between the hostname/ip address and the
port (if provided).

Since `env', `param' and `fastcgi' are all new stuff, there's no need
to keep compatibility.
2021-07-09 08:30:55 +02:00
Omar Polo ff954a3e76 typo... 2021-07-09 08:30:55 +02:00
Omar Polo d19951cf03 rename `mime MIME EXT' to `map MIME to-ext EXT'
With the newish automatic string concatenation, options like `mime'
that accepts two strings as parameter start to become ambiguous: which
strings gets concatenated?  Instead of trying to document in the
manpage which argument(s) is subject to string concatenation, do the
concat always and introduce a separator.  In the case of mime,
`to-ext' now acts as a separator to distinguish.  While there, also
use a new keyword because it sounds better.

It's dead-easy to upgrade to the new configuration, possibly with some
sed magic, but for the moment the old `mime' form is preserved: (with
a warning!)  Will be dropped in the next release.
2021-07-09 08:30:55 +02:00
Omar Polo d93c819182 use bsearch to match the keywords
not that it's a bottleneck, but it's fancier this way.
2021-07-09 08:30:55 +02:00
Omar Polo bfb076ed7e don't expand macros inside the quotes
Now that we have this auto concat string thingy, macros can simply
expand to standalone strings in place, as single words.

Forgot to point it out in previous commits, but now we can

	cert = "/etc/keys"

	server "foo" {
		cert $cert "/foo.crt"
		...
	}
2021-06-29 16:35:06 +00:00
Omar Polo 7252049dd7 align 2021-06-29 16:10:40 +00:00
Omar Polo 98f5217822 [config] concat two or more strings next to each others 2021-06-29 16:10:10 +00:00
Omar Polo 6b86655a10 don't require the strict order macro > options > servers 2021-06-29 12:42:44 +00:00
Omar Polo 3b21cca385 allow to define macros in the config file
Macros can be defined at the top of the configuration file:

	dir = "/var/gemini"
	cert = "/etc/keys"

and re-used later, for example

	server "foo" {
		root "$dir/foo"       # -> /var/gemini/foo
		cert "$cert/foo.pem" # -> /etc/keys/foo.pem
	}
2021-06-29 12:17:40 +00:00
Omar Polo fafc684957 trailing whitespaces 2021-06-29 11:21:32 +00:00
Omar Polo 8235a81c8f give a name to the anonymous union 2021-06-29 11:11:43 +00:00
Omar Polo ef129b08ef move lineno/colno into the token struct 2021-06-16 14:51:57 +00:00
Omar Polo 74f0778b9a drop the dependency on lex by implementing yylex by ourselves
The actual implementation is based off doas' parse.y.  This gave us
various benefits, like cleaner code, \ to break long lines, better
handling of quotes etc...
2021-06-16 14:43:16 +00:00
Omar Polo c92b802b6a add `param' keyword 2021-06-11 15:49:46 +00:00
Omar Polo 0d047efcb4 refactor fastcgi block 2021-05-24 09:09:10 +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 c705ecb1ba factorize all "only once" checks
and while there add some more
2021-05-03 16:11:28 +00:00
Omar Polo fdea6aa0bc allow ``root'' rule to be specified per-location block 2021-04-30 17:16:34 +00:00
Omar Polo adbe6a6493 always use ensure_absolute_path and improve its error message 2021-04-30 16:17:07 +00:00
Omar Polo d06d6f4bba sort tokens 2021-04-29 19:13:16 +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 48b69cb2dc fix some logging
- we can't use log_* or fatal() before logger_init
- err -> errx if errno isn't involved
2021-04-28 12:43:17 +00:00
Omar Polo 9cc630aa63 added ``env'' option to define environment vars for CGI scripts 2021-04-28 12:43:17 +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 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 793835cb26
add `log on/off' to enable/disable logs per-location 2021-02-23 13:43:24 +01: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 2d34f732f1 typo 2021-02-10 13:13:17 +00:00
Omar Polo 002a84a123 improve errors during config parsing 2021-02-10 11:53:05 +00:00
Omar Polo eb59f87e67 sort 2021-02-09 22:38:10 +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 a13739138b add trailing \n in yyerror 2021-02-07 18:08:50 +00:00
Omar Polo a709ddf5eb added prefork option 2021-02-07 12:05:32 +00:00
Omar Polo eef0492e35 add missing declaration of TENTRYPOINT 2021-02-07 11:27:15 +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 709f4c9447 alter the string, don't advance pointer
if we advance the pointer, we crash during reload due to an invalid
address given to free.  altering the string is the way.
2021-02-04 20:04:12 +00:00
Omar Polo ca21e10043 reload configuration on SIGHUP 2021-02-04 13:23:15 +00:00
Omar Polo e17642a7bb require absolute paths in config file 2021-02-01 11:08:57 +00:00
Omar Polo 32693ee6db include string.h
openbsd and freebsd yacc include string.h in the generated sources,
bison (tested on alpine at least) don't
2021-01-28 17:50:22 +00:00
Omar Polo 415ac7a229 fix build 2021-01-28 16:33:20 +00:00
Omar Polo cbeee4cad1 warn about punycode in server definition 2021-01-28 16:27:40 +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 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 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 e7a2a99b5a added index option 2021-01-24 09:14:01 +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 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 982069a120 add "mime" and "default type" option for the configuration 2021-01-18 18:43:47 +00:00
Omar Polo 5bc3c98ed4 add protocols to the config 2021-01-15 18:55:05 +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