Commit Graph

1440 Commits

Author SHA1 Message Date
Joris Vink 80db912c34 Improve CRL reloading.
When a CRL is reloaded, disconnect any TLS clients that authenticated
with a certificate that is now in the CRL.

Improve log messages while here.
2022-09-05 10:59:28 +02:00
Joris Vink 0de5d5e4c7 Use a more structured log format. 2022-09-05 10:59:06 +02:00
Joris Vink 5dcf3d1550 Fix a problem with http_timeout when a body is sent.
When receiving an HTTP body, Kore never reset http_timeout once
the transfer was done.

This can result in a 408 being thrown by Kore while a request is
activity running.
2022-09-01 12:09:15 +02:00
Joris Vink b3c5ce8375 Demote log message to notice in tls_none backend. 2022-08-26 10:23:13 +02:00
Joris Vink c0d6657c26 Fix a log message regarding worker count.
The worker_count is incremented by 2 earlier to account for keymgr/acme
but aren't actually workers that should count towards CPU pinning.

So adjust the count when comparing to cpu_count when logging that there
are more workers than cpus.
2022-08-26 10:19:26 +02:00
Joris Vink 11cf2075a2 Get rid of PRI_TIME_T defines locally.
Instead do an ugly, use PRId64 and cast the time_t to a signed 64-bit integer.
This'll work on all platforms regardless of how they defined time_t.
2022-08-22 13:17:45 +02:00
Joris Vink 139348c2b7 change PRI_TIME_T to ldd on BSD platforms 2022-08-19 17:00:33 +02:00
Joris Vink a2d312d0a0 kore_debug() has been unused for years.
Kill all useless messages, convert useful ones into kore_log() instead.
2022-08-18 15:20:55 +02:00
Joris Vink 52ff37c5be Support crls for kore.domain() in Python.
The crl keyword is parsed when the client_verify keyword has been set.

eg:

kore.domain("kore.io", cert="cert.pem", key="key.pem",
    client_verify="cacert.pem", verify_depth=1, crl="crl.pem")
2022-08-18 10:43:10 +02:00
Joris Vink 773790c18b fix more fallout from printf format attributes. 2022-08-17 13:19:25 +02:00
Daniel Fahlgren 0dc4d1adaa Remove unused define 2022-08-17 13:16:27 +02:00
Daniel Fahlgren 2211bb8f97 Some platforms doesn't have a /tmp/ directory where temporary files can be
stored. Make it possible to override that location compile time.
2022-08-17 13:16:25 +02:00
Daniel Fahlgren 5ddfc2131c Constify filemap_entry argument to filemap_serve() 2022-08-17 13:16:21 +02:00
Daniel Fahlgren 5e21de3a4c Add printf format attributes and fix fallout 2022-08-17 13:16:18 +02:00
Joris Vink 25ea10fea7 Be better. 2022-08-10 10:20:18 +02:00
Joris Vink 73be741bfd Allow authenticators on filemaps.
This commit introduces the ability to add authenticators to filemaps.
Just like in normal routes, the authenticators will be resolved first
before allowing access to the filemap entries.

Configuration wise, the authenticator is an optional value after the
filemap config directive:

	filemap / webroot myauth

In the Python API you can now pass the authenticator for a filemap entry
but turning the value of the filemap into a tuple with the first entry
being the path and the second being the auth dict:

	AUTH AUTH={
	    "type": "cookie",
	    "value": "cookiename",
	    "redirect": "/auth/",
	    "verify": verify_cookie
	}

	domain.filemaps({
	    "/css/": "webroot/css",
	    "/secret/": ("webroot/secret", AUTH)
	})
2022-08-10 10:13:01 +02:00
Joris Vink e2fcedfaec Differentiate between normal shutdown and fatal.
The parent process never differentiated between a worker process
asking for a shutdown or a worker process calling fatalx() when
it came to its exit code.

I made some changes here so the parent process will exit with
an exit code 1 if anything worker related went wrong (fatalx/death policy).
2022-08-08 11:02:27 +02:00
Joris Vink 7316f372d1 If no result was available, use Py_None. 2022-08-04 13:19:02 +02:00
Joris Vink fd94fba3c7 Python 3.10 improvements.
The coroutines results are now relayed back via PyIter_Send() and
no longer obtainable via _PyGen_FetchStopIterationValue().

This means that our kore.gather() would not be able to return any
values from any of the coroutines it governed.

Fix this by saving the object returned in PyIter_Send() and using it
later in pygather_reap_coro().
2022-08-04 09:56:41 +02:00
Joris Vink 30c8652534 More Python 3.10.x changes.
Handle PYGEN_RETURN in case our prerequest handlers return actual
values instead of None.

Mimic PySendResult for older versions for now.
2022-07-14 11:48:32 +02:00
Joris Vink f6c54042c5 Improve Python 3.10.x support.
Python 3.10.x its PyIter_Send() will return PYGEN_ERROR if the
generator returned (as opposed to yielding) if the result returned
is Py_None and no argument was given to PyIter_Send(). Which is counter
intuitive since it seems it should give us a PYGEN_RETURN as per its
documentation.

It also no longer sets PyExc_StopIteration in this case so we cannot depend
on that anymore to figure out if a coroutine has finished.

This caused Kore to respond with a 500 immediately after coroutines
ran to completion as it was looking for the PyExc_StopIteration exception.

Fix this by simply doing another check if an exception happened before
we enter the code path where Kore would spew the 500.
2022-07-14 10:56:27 +02:00
Joris Vink f7a76f7e96 Improvements for kore.recvmsg() in Python.
The cmsghdr contains a length (cmsg_len) which indicates the length
of the data in combination with the cmsghdr length itself.

Remove the length of the cmsghdr before passing it back up to callers
so they don't need to bother with it.

This also fixes a mistake where we ended up copying extra data
from the ancdata buffer that was unintended.
2022-04-22 17:01:06 +02:00
Joris Vink 3516d50be3 adjust log messages 2022-04-20 11:10:01 +02:00
Joris Vink dcdfce5763 Unbreak x509dict.
I accidentally swapped issuer and subject while constructing it.
2022-04-20 11:06:10 +02:00
Joris Vink 38d7a5f88d Fix handling kore_tls_connection_accept() return codes.
When this code was moved from src/connection.c into src/tls_openssl.c
a return wouldn't break us out from kore_connection_handle() as
previously expected.

This ment that Kore would move the connection into established state
immediately even if SSL_accept() needed to read more.

This broke TLS client authentication as Kore its belts and suspenders
kept throwing a 403 due to the code not properly obtaining the client
certificate when expected.
2022-03-21 12:23:38 +01:00
Joris Vink 5bfd61d136 Hide kore_pymodule behind !KORE_SINGLE_BINARY.
Unbreaks building single binaries with Python support.
2022-03-21 08:52:38 +01:00
Joris Vink 0d305ecf5a fix format specifier, previous commit touched a bit too much. 2022-03-15 13:47:35 +01:00
Joris Vink 5021eeb597 use correct format specifier for size_t 2022-03-15 13:39:01 +01:00
Joris Vink 78d667abc7 Set socklen to 0 by default in pysocket_async_recv(). 2022-02-20 21:30:17 +01:00
Joris Vink a65be853f0 Simplify python_push_integer().
We're not doing anything with ret, so kill it.
2022-02-20 21:27:35 +01:00
Joris Vink a7aa51d8d5 Fix unhappy path cleanup.
The whole while (cnt-- >= 0) idiom is busted since cnt started
at 0 and if the first call to PyUnicode_FromStringAndSize() fails
then we're attempting to access -1.
2022-02-20 21:25:18 +01:00
Joris Vink daca80f1cb execve() its envp shouldn't be NULL. 2022-02-20 21:20:14 +01:00
Joris Vink 96d1396df1 Change logic for http_version a bit. 2022-02-20 21:19:44 +01:00
Joris Vink 045beb8622 add kore_mem_zero().
use it in places explicit_bzero() used to be called.

The kore_mem_zero() is a best effort to try and let the compiler
not optimize the code away. Highly platform dependent.
2022-02-18 11:13:01 +01:00
Joris Vink 722a0eca21 Get rid of MAKE_CLONE macro 2022-02-18 11:08:13 +01:00
Joris Vink 576d7ba5e8 more whitespace fixes 2022-02-18 11:07:40 +01:00
Joris Vink 404ccf622f whitespace fixes 2022-02-18 11:06:35 +01:00
Joris Vink 7f74790da7 Define KORE_PRIVATE_KEY per TLS backend. 2022-02-18 10:50:17 +01:00
Joris Vink 80383024a3 For each TLS backend let us use correct types. 2022-02-18 10:47:05 +01:00
Joris Vink a9f7bd7faf rename ssl prefixed things to tls. 2022-02-18 10:20:28 +01:00
Joris Vink b20d26e839 Throw exception is TLS backend isnt supported. 2022-02-18 09:17:58 +01:00
Joris Vink feb90208ef Add kore_tls_x509_data().
Use it in the Python code, which requires it.
2022-02-18 09:14:30 +01:00
Joris Vink 169a4e7c5d wrap 2 more syscalls in ifdefs. 2022-02-17 14:46:33 +01:00
Joris Vink 99a1581e19 Initial work splitting OpenSSL code away.
This work moves all TLS / crypto related code into a tls_openssl.c
file and adds a tls_none.c which contains just stubs.

Allows compilation of Kore with TLS_BACKEND=none to remove building
against OpenSSL.

Also adds code for SHA1/SHA2 taken from openssh-portable so we don't
depend on those being present anymore in libcrypto.
2022-02-17 13:45:28 +01:00
Joris Vink 6dc162e7ee Handle ECHILD when reaping workers on shutdown.
If the child process is already dead we must handle it accordingly
instead of getting stuck waiting on it.
2022-02-16 12:32:20 +01:00
Joris Vink 23d762d682 Allow parent to send msgs to workers via kore_msg.
It wasn't possible for the parent process to send messages
directly via kore_msg_send() to other worker processes.

This is now rectified to from the parent process one can call
kore_msg_send() with a worker destination and it'll work.
2022-02-01 10:36:07 +01:00
Joris Vink b3f54e290a Change parent behaviour when calling waitpid().
Wait for any process in our process group only instead of WAIT_ANY.

This allows the parent process to start subprocesses that end up
in different process groups which are handled in user code instead
completely (using signalfd for example).
2022-02-01 10:34:12 +01:00
Joris Vink 833ca646e7 i forgot, it's 2022. 2022-01-31 22:02:06 +01:00
Joris Vink a29700f26d Bring back page authentication via config.
Inside of the new route handlers the "authenticate" keyword can
be specified to let the route authenticate via a previously
configured authentication block.

The ability to do this went missing in a previous commit that overhauled
the routing structure of the configuration.
2022-01-31 15:13:34 +01:00
Joris Vink 41a4be384e Increase ACME signop timeout.
Just noticed the 5 seconds wasn't enough when renewing all
certificates in my own setup (i have about 30 ACME domains via Kore).

Bumped it to 30 seconds instead.
2022-01-29 08:55:50 +01:00
Joris Vink 93ec99c23e Only enable accesslog vacuum if needed.
If no accesslogs are enabled, the parent has no need for
the vacuum timer to be activated.

This way the parent blocks in epoll_wait() instead of waking up
for no reason when there are no accesslogs enabled.
2022-01-28 14:29:58 +01:00
Joris Vink 93a4fe2a15 Worker hook rework.
This commit adds improved hooks for Python and a new signal delivery hook.

For the Python API kore_worker_configure() and kore_worker_teardown() had
to be implemented before this commit. Now one can create a workerstart
and workerend method in their koreapp as those will be called when
they exist.

The new signal hook is either kore_worker_signal() or koreapp.signal.

This new hook is called after the worker event code handles the received
signal itself first.

With this commit there is also a new kore_signal_trap() API call allowing
you to more easily trap new signals. This API also also exported to the
Python part of the code under kore.sigtrap()
2021-12-22 09:50:26 +01:00
Joris Vink d8113e4545 Reset dom->acme_cert upon clearing. 2021-12-19 00:14:33 +01:00
Joris Vink ff19ce7652 Python: add a protocol member to kore.httprequest
This returns a string depending on the protocol used (https / http) for
the HTTP request.
2021-12-17 16:52:13 +01:00
Joris Vink a3800fa57e Python: allocate py_req in the http_request_free hook if needed.
Makes it possible to mix on_free with other runtimes.
2021-12-15 12:19:04 +01:00
Joris Vink e545657023 make sure we only call rt->on_free if req has route 2021-12-15 12:16:37 +01:00
Joris Vink e8e01980fc Python: allow route hooks via kore.route().
Adding the hooks keyword with a dictionary attached to specify
the relevant hooks will hook them for the given route.

Eg:

domain.route("/", self.index, methods=["get"],
    hooks={
        "on_free": self.request_free
    }
)

These are the same hooks available via a normal Kore route configuration.
2021-12-14 23:15:21 +01:00
Joris Vink 97ef486d22 Fix indentation 2021-12-13 10:48:29 +01:00
Joris Vink 9845c8bbe1 Python: Add req.body_digest.
Returns the SHA256 digest of the uploaded body as a bytes object.
2021-12-13 10:45:00 +01:00
Joris Vink 774cc56ed2 Python: Add an req.connection.x509dict
This dictionary for now only contains the subject and issuer names
from the client certificate (if one was provided) with their
X509_NAME components.

Eg:

{
  "issuer": {
    "C": "SE",
    "O": "kore autogen: x509name-test",
    "CN": "localhost"
  },
  "subject": {
    "C": "SE",
    "O": "kore autogen: x509name-test",
    "CN": "localhost"
  }
}
2021-12-11 22:37:15 +01:00
Joris Vink ca4ffa457c Add a kore_x509_issuer_name() function.
Rework the underlying X509_NAME juggling into a kore_x509name_foreach()
so that it can be called for multiple locations.
2021-12-11 22:36:31 +01:00
Joris Vink cf6cf5f820 Always align pool element lengths properly.
Enforce 8-byte alignment on the pool element lengths.
2021-12-11 22:35:37 +01:00
Joris Vink 685f504564 Log if no TLS server was configured 2021-12-11 21:59:41 +01:00
Joris Vink a641c29301 Add SYS_newfstatat to keymgr seccomp rules.
From Aurelien Jarno via patches@
2021-12-11 20:34:44 +01:00
Joris Vink a54f806978 Don't let kore build with openssl 3 again.
The whole privilege separation breaks with OpenSSL 3, even if it builds.

I guess it is somewhat time to start on donutls.
2021-12-06 23:58:13 +01:00
Joris Vink 06803e2592 Get kore to at least build with openssl 3 2021-12-06 21:21:21 +01:00
Joris Vink 93bf18be81 Handle DELETE maybe having a request body. 2021-12-06 14:44:07 +01:00
Joris Vink 480e589dd5 The DELETE method may have a request body. 2021-12-06 14:43:52 +01:00
Joris Vink 6b2609c2b8 Allow DELETE for kore.httpclient() to have body.
The DELETE method could have an HTTP body, so allow it in the
kore.httpclient() python call.
2021-12-06 14:16:58 +01:00
Joris Vink a9ee15bff6 Improve closing of a kore.socket() in Python API.
When a kore.socket() is closed from any coroutine, make sure any other
coroutines waiting on events on the socket are awoken so they properly
can return errors.
2021-12-02 22:47:17 +01:00
Joris Vink 5ac62b17bc Python coro under-the-hood improvements.
- Change python coroutine id to a uint64_t.
- Add kore.task_id() to return active coro its id.
2021-12-02 21:58:13 +01:00
Joris Vink 86ecb85f03 use correct privsep name for acme 2021-12-02 19:33:20 +01:00
Joris Vink efc7b3d9a6 Improve how the parent handles workers.
- Make sure we drain the worker log channel if it dies
  so we can flush out any lingering log messages.

- Get rid of the raise() in the parent to signal ourselves
  we should terminate. Instead depend on the new kore_quit.

- Always attempt to reap children one way or the other.
2021-11-03 17:23:05 +01:00
Joris Vink 155c7dfbde prefix fatal messages with FATAL 2021-11-03 17:22:53 +01:00
Joris Vink a7e1d1d22a Remove keymgr cleaning keys logs.
its such a dumb log message, obviously its going to cleanup keys.
2021-11-03 17:16:34 +01:00
Joris Vink 01e85fd717 Two small python improvements.
- Decrement bytes count when python_cmsg_to_list() fails.
- Use correct define for PYSOCKET_TYPE_RECVFROM.
2021-11-03 15:19:43 +01:00
Joris Vink 5962a94504 wrap pipeline in PG_VERSION_NUM >= 140000 2021-10-27 22:39:29 +02:00
Joris Vink 960730a062 On MacOS put the OpenSSL flags under FEATURES_INC.
Use this to pick them up automatically for kodev.
2021-10-27 22:28:08 +02:00
Joris Vink fa97544f01 Handle PGRES_PIPELINE_* for PQResult() 2021-10-27 22:27:42 +02:00
Joris Vink 98d5909b7d bring back python_import and python_path.
These were mistakingly removed a while ago.
2021-10-20 11:20:25 +02:00
Joris Vink 23b95448cc Hide worker logs behind kore_quiet. 2021-10-05 12:29:50 +02:00
Joris Vink 0af7258c30 Don't include kore config in all builds just yet. 2021-10-04 19:18:15 +02:00
Joris Vink d078bdfb95 Add a gen command to kodev.
This will generate an asset file for Kore based on the source file
or directory given.

This allows other build systems to more easily generate asset
files if their compilation steps are different.
2021-09-22 22:39:42 +02:00
Joris Vink 1fcc9345a6 add cflags/ldflags commands to kodev.
These will spew out the required CFLAGS and LDFLAGS respectively
when compiling source code for use in Kore applications.

This should make it easier to integrate this into existing
build systems where using kodev may be a bit annoying.

Eg: gcc -Wall -std=c99 `kodev cflags` koreapp.c `kodev ldflags` -o koreapp.so
2021-09-22 20:50:09 +02:00
Joris Vink 3e85d36532 The *_CheckExact() family sets no exceptions.
So set a runtime exception if the objects passed mismatch.
2021-09-22 16:48:21 +02:00
Joris Vink af45284641 count acme domains when configured with Python 2021-09-21 20:47:23 +02:00
Joris Vink 01370c262d fix builds with DEBUG. 2021-09-21 20:47:16 +02:00
Joris Vink 17ceb32e23 Cleanup single/dso files properly with kodev clean.
Obey the out_dir too, in case its set differently.
2021-09-21 20:17:44 +02:00
Joris Vink 46b7a962bf be verbose about filemap resolving. 2021-09-21 16:17:46 +02:00
Joel Arbring 31aaf128a1 Check for .so file where we create it 2021-09-17 20:04:46 +02:00
Joris Vink 63bbc1fa0f Be sure content_length is 0, just in case. 2021-09-17 19:56:35 +02:00
Joris Vink 6b1f02e6b0 Remove another dead assignment. 2021-09-17 19:53:31 +02:00
Joris Vink 9f6043bbde Remove dead assignment. 2021-09-17 19:52:52 +02:00
Joris Vink bcfb79a389 Remove dead assignment and unused vars. 2021-09-17 19:49:32 +02:00
Joris Vink 351eec7eb4 Add the on_body_chunk handler for routes.
If set, will call a given handler with the prototype of

`void body_chunk(struct http_request *req, const void *data, size_t len);`

for each chunk of the received HTTP body, allowing a developer to handle
it in their own way.

The incoming body is still being handled and retained in the same way
as before (in a kore_buf or temporary file).

While here, allow HTTP_STATUS_CONTINUE to work via http_response() and
make the handling of incoming HTTP header data a bit better.
2021-09-17 19:30:22 +02:00
Joris Vink f5a58368b7 HTTP improvements.
Introduce an on_headers callback for routes, allowing one to inspect
the headers before the request is processed further.

Additionall,

Add a new way of obtaining HTTP headers. Much like http_argument_get_*()
functions, these new APIs allow you to fetch the data of an HTTP header
as a specified C type.

The new APIs are:

* http_request_header_int16()
* http_request_header_uint16()
* http_request_header_int32()
* http_request_header_uint32()
* http_request_header_int64()
* http_request_header_uint64()
* http_request_header_float()
* http_request_header_double()

Should make it easier to operate in HTTP header data in a safe way.
No need to always roll your own string to int conversion functions.
2021-09-15 22:16:22 +02:00
Joris Vink e98a4ddab5 Change how routes are configured in Kore.
Routes are now configured in a context per route:

route /path {
	handler handler_name
	methods get post head
	validate qs:get id v_id
}

All route related configurations are per-route, allowing multiple
routes for the same path (for different methods).

The param context is removed and merged into the route context now
so that you use the validate keyword to specify what needs validating.
2021-09-15 11:09:52 +02:00
Joris Vink 2576427dc0 Simplify if a worker needs to be added to msg.
Just look if the kore_worker data structure its ps member is non NULL.
If it is, it was started by the parent process at some point.
2021-09-14 15:45:34 +02:00
Joris Vink a2d48feeb7 Lets make sure milliseconds are formatted nicely. 2021-09-14 09:36:33 +02:00
Joris Vink 41511c1683 Log timestamps in UTC for, add milliseconds.
This is when using the normal foreground logs or a specified logfile.
2021-09-14 09:30:17 +02:00