Commit Graph

1440 Commits

Author SHA1 Message Date
Joris Vink 0268a0ae0a Wrap certain syscalls inside of an ifdef.
Makes kore with python and acme work on my pinebook pro.
2020-07-14 15:38:54 +02:00
Joris Vink bb2f0d8b52 Python: improve kore.lock when handling cancelled coroutines.
If a coroutine is killed from another coroutine and the killed coroutine
was waiting on a kore.lock() object, it would have been incorrectly
woken up again once said lock was released.

This would cause a Python exception that a generator was already
running and a crash due to the pool element already being freed.

Track the active locking operation per coroutine so we can remove
the coroutine if it is killed, fixing the problem.
2020-07-09 20:22:18 +02:00
Joris Vink 8235759bca Python: Add kore.app().
This method allows you to set a Python object and obtain it
by calling the method again without any arguments.

eg:

foo = SomeClass()

kore.app(foo)

foo = kore.app()
2020-07-05 21:47:22 +02:00
Joris Vink e38c6e5d30 Python: Several fixes for our async curl support.
- Fix the curl-extract-opt.sh generation script to work on newer
  curl releases as the header changed slightly.
- Use the correct handles when calling curl_easy_setopt() inside
  of our setopt functions exported via Python.
- Add a curl.setbody() method, allowing a body to be sent to be set.
  (eg when sending mail via SMTP).
- Regen of our python_curlopt.h from 7.71.1
2020-07-02 08:41:17 +02:00
Joris Vink 122a86013b Python: respond with 500 in case of a coroutine error.
If a coroutine throws an exception, respond with a 500
after logging the exception itself.
2020-06-30 09:57:48 +02:00
Joris Vink 2316f1016d Always prune disconnected clients at the end of the event loop. 2020-06-26 12:25:07 +02:00
Joris Vink 74432aeff7 Set netwait to 10ms if a signal is pending.
If a signal is delivered after the signal check in the worker
loop we could end up in a scenario where we wait for i/o to
start triggering it.
2020-06-16 17:29:45 +02:00
Frederic Cambus ce8290cefc Add missing test for SYS_stat, fixes the build on arm64. 2020-06-09 21:44:38 +02:00
Joris Vink 8a39d18196 work around different dirname()/basename() implementations. 2020-06-09 12:22:22 +02:00
Joris Vink 30e9b642a0 Skip ACME worker process if ACME isnt built-in. 2020-06-05 14:21:39 +02:00
Joris Vink dcb34033fa trailing whitespace 2020-05-08 22:43:12 +02:00
Joris Vink 40dddbc769 whitespace 2020-04-27 09:07:37 +02:00
Joris Vink c83f34c938 Rework http_pretty_error a tiny bit. 2020-03-04 08:36:10 +01:00
Frederic Cambus fe43ed09ac Add the http_pretty_error configuration option.
When enabled, Kore returns HTML error pages for status codes 4xx and
5xx instead of empty content.
2020-03-04 08:22:51 +01:00
Joris Vink db31f37ab0 Add a "return" configuration option.
This hooks into the existing redirection framework but allows you
to quickly deny certain paths with a 403 or other status code.

The snippet below would for example declare a filemap served from 'www'
directory but denying all access to the files under the 'data' directory:

filemap		/	www
deny		/data	403
2020-03-03 11:28:16 +01:00
Joris Vink 9e5e698e4b only count TLS enabled domains as primary_dom.
Otherwise one can run into trouble with ACME if it was enabled on
domains but the first domain in the configuration was a non TLS domain.
2020-02-21 13:58:03 +01:00
Joris Vink 82d7b58405 Add trailing byte in http_token and http_field_content. 2020-02-19 10:38:41 +01:00
Frederic Cambus b4ebee5913 Add the http_server_version configuration option.
This allows setting a custom server header from the config file, for
example to mask the version number.
2020-02-19 08:07:02 +01:00
Frederic Cambus cc6ab4f05d Allow the stat syscall in keymgr, it is used in keymgr_save_randfile(). 2020-02-13 15:10:58 +01:00
Joris Vink 4115df69f6 adjust for configuration changes 2020-02-11 11:04:10 +01:00
Frederic Cambus b24bc43051 Variable ‘c’ was set but not used so remove it, broke the build with GCC 9.2.0. 2020-02-10 15:43:46 +01:00
Joris Vink 6ba56bb8f6 adjust copyright years 2020-02-10 15:35:41 +01:00
Joris Vink 9d0aef0079 bump copyright 2020-02-10 14:47:33 +01:00
Joris Vink fa2e8ef0b6 Add support for config based redirection.
Inside the domain contexts a 'redirect' rule will allow you to redirect
a request to another URI.

Ex:

Redirect all requests with a 301 to example.com

	redirect ^/.*$ 301 https://example.com

Using capture groups

	redirect ^/account/(.*)$ 301 https://example.com/account/$1

Adding the query string in the mix

	redirect ^/(.*)$ 301 https://example.com/$1?$qs
2020-02-07 06:42:33 +01:00
Joris Vink 6072828d8f Improve BSD platform event code.
Instead of adding all listening sockets into the kqueue at platform init,
do it in the first call to kore_platform_enable_accept().

This way a worker process can still call kore_server_create() in its
kore_worker_configure() hook.
2020-01-24 19:36:32 +01:00
Joris Vink d86a10afa1 allow use udata in kore.timer() via the data kwarg. 2020-01-22 09:42:41 +01:00
Joris Vink 2d380cac3f Expose our async libcurl support to the Python api.
Kore already exposed parts of this via the kore.httpclient() method but
this commit takes it a bit further and exposes the libcurl interface
completely (including the setopt options).

tldr:

handle = kore.curl("ftp://ftp.eu.openbsd.org/pub/OpenBSD/README")
handle.setopt(kore.CURLOPT_TIMEOUT, 5)

data = await handle.run()
print("%s" % data.decode())
2020-01-18 19:43:38 +01:00
Joris Vink 6f31e14e83 sprinkle kore_quiet around 2020-01-17 22:25:52 +01:00
Joris Vink 01cc981632 Improve waiting on workers to exit take 2.
Keep track of what workers are running and account for those when things
exit. Somewhat reverts the entire last commit, that was the wrong approach.
2020-01-17 21:48:55 +01:00
Joris Vink d8ff8e2c18 Improve waiting on children to exit.
If waitpid() returns -1 check if errno is ECHILD, just mark the worker
process as exited.

This could happen if Kore starts without keymgr/acme but those would still
be accounted for.
2020-01-17 21:43:56 +01:00
Joris Vink 445163f7c5 Add support for setting an email for ACME.
Can be configured via the acme_email configuration option.

eg:

	acme_email john@example.com
2020-01-13 11:00:40 +01:00
Frederic Cambus ff40f04693 Remove forgotten debug message. 2020-01-07 21:46:19 +01:00
Joris Vink 14095a7702 Revert chunk that snuck in last commit. 2019-12-13 20:30:13 +01:00
Joris Vink 243cd4e6a0 kore_listener_init() returns KORE_RESULT_OK or KORE_RESULT_ERROR. 2019-12-13 09:14:26 +01:00
Joris Vink 1d30760aa0 use X509_NAME_ENTRY* functions directly. 2019-11-19 13:00:17 +01:00
Joris Vink ca70f9d726 TLS improvements.
These changes improve the constraint kore had with client authentication and
multiple domains.

- Add kore_x509_subject_name() which will return a C string containing
  the x509 subject name in full (in utf8).

- Log TLS errors if client authentication was turned on, will help debug
  issues with client authentication in the future.

- If SNI was present in the TLS handshake, check it against the host specified
  in the HTTP request and send a 421 in case they mismatch.

- Throw a 403 if client authentication was enabled but no client certificate
  was specified.
2019-11-19 11:09:24 +01:00
Joris Vink 283cd6da54 allow NULL pointers to cleanup/free calls. 2019-11-19 09:44:13 +01:00
Joris Vink 56c33f85d4 change client_authority to client_verify. 2019-11-18 20:30:52 +01:00
Frederic Cambus c6dbf16b65 Account for the change from 'static' to 'route' in the CLI tool as well. 2019-11-16 12:34:57 +01:00
Joris Vink f8accef3f7 allow access/accessat at all times in keymgr. 2019-11-15 09:28:33 +01:00
Joris Vink f6cd16c567 Replace static/dynamic with a single option: route
Kore will automatically detect if a route is a dynamic or static one
so there is no need for the configuration options to differ anymore.
2019-11-15 08:11:02 +01:00
Joris Vink 73757a29d5 Make dumb compilers happy. 2019-11-15 07:49:16 +01:00
Joris Vink b3b5aa37b7 Allow acme config via python api 2019-11-13 23:01:24 +01:00
Joris Vink 7c78526e82 pull in ptrace.h 2019-11-13 15:56:36 +01:00
Joris Vink 7cf0006f52 fix potential NULL dereferences.
found by clang --analyze, reminded by fahlgren@
2019-11-13 11:23:02 +01:00
Joris Vink e2930f29c1 wrap pledge stuff into an ifdef __OpenBSD__ block. 2019-11-07 08:29:38 +01:00
Joris Vink bb39643b48 small acme fixes.
- don't create the NID for the acme extension several times
- add missing pledges for openbsd keymgr (it will write+create files)
2019-11-07 07:56:13 +01:00
Joris Vink c78535aa5d Add acmev2 (RFC8555) support to Kore.
A new acme process is created that communicates with the acme servers.

This process does not hold any of your private keys (no account keys,
no domain keys etc).

Whenever the acme process requires a signed payload it will ask the keymgr
process to do the signing with the relevant keys.

This process is also sandboxed with pledge+unveil on OpenBSD and seccomp
syscall filtering on Linux.

The implementation only supports the tls-alpn-01 challenge. This means that
you do not need to open additional ports on your machine.

http-01 and dns-01 are currently not supported (no wildcard support).

A new configuration option "acme_provider" is available and can be set
to the acme server its directory. By default this will point to the
live letsencrypt environment:
    https://acme-v02.api.letsencrypt.org/directory

The acme process can be controlled via the following config options:
  - acme_root (where the acme process will chroot/chdir into).
  - acme_runas (the user the acme process will run as).

  If none are set, the values from 'root' and 'runas' are taken.

If you want to turn on acme for domains you do it as follows:

domain kore.io {
	acme yes
}

You do not need to specify certkey/certfile anymore, if they are present
still
they will be overwritten by the acme system.

The keymgr will store all certificates and keys under its root
(keymgr_root), the account key is stored as "/account-key.pem" and all
obtained certificates go under "certificates/<domain>/fullchain.pem" while
keys go under "certificates/<domain>/key.pem".

Kore will automatically renew certificates if they will expire in 7 days
or less.
2019-11-06 19:43:48 +01:00
Joris Vink b226b6ca89 Allow seccomp to work on arm64 and i386. 2019-11-05 13:12:43 +01:00
Joris Vink 68ce436fa7 expand previous commit further, check proto as well. 2019-11-04 07:23:21 +01:00
Joris Vink 0eab72f4cd Only call http_check_timeout on established clients. 2019-11-03 22:28:48 +01:00
Joris Vink 068b295427 remove error pledge, die hard. 2019-11-03 19:52:47 +01:00
Raymond Pasco 811b2eb3f0 Add dns to pledges when initializing curl
Without the dns pledge, curl can't resolve names on OpenBSD.
2019-11-03 11:47:49 +01:00
Joris Vink 8311c036d9 Add seccomp_tracing configuration option for linux.
If set to "yes" then Kore will trace its child processes and properly
notify you of seccomp violations while still allowing the syscalls.

This can be very useful when running Kore on new platforms that have
not been properly tested with seccomp, allowing me to adjust the default
policies as we move further.
2019-10-31 12:52:10 +01:00
Joris Vink 16afcb66d0 kodev MINIMAL=1 shouldn't include mime types either. 2019-10-31 09:55:14 +01:00
Joris Vink ca17e08ad9 Add MIMINAL=1 build to kodev.
If the kodev tool is built with MINIMAL=1 it will not compile in
support for creating application skeletons, only to build apps, etc.

Building with MINIMAL=1 drops the openssl linkage.
2019-10-31 09:44:47 +01:00
Joris Vink 699ba3c0fe Make sure json offset advanced upon end of object/array. 2019-10-30 10:02:51 +01:00
Joris Vink 7b5046873a Make sure we wakeup the coroutine that called proc.reap().
We actually woke up the coroutine that originally spawned the process
when we reap it, but another coroutine may have taken over the object.

This mimics how we do things for the pysock_op things.
2019-10-29 15:12:20 +01:00
Joris Vink ebee0f3752 nope, changed my mind, drop libressl 2.7.5. 2019-10-28 12:47:51 +01:00
Joris Vink d001b022a7 Cleanup ifdef mess for different OpenSSL based libs.
Now everything that has the "newer" OpenSSL API (1.1.x) is hidden
behind a KORE_OPENSSL_NEWER_API define. Tone down minimum libressl
version again to 2.7.5.
2019-10-28 12:33:39 +01:00
Joris Vink 4808365f8a Make sure things build with libressl 3.0.0 2019-10-27 21:20:47 +01:00
Joris Vink 5f03f991c9 Change kore_json_find() to operate on a kore_json_item.
This way you can call the lookup function on any JSON value that you
previously obtained (or the JSON context root).
2019-10-25 12:27:16 +02:00
Joris Vink f637d617aa Allow SECCOMP_KILL_POLICY to be overwritten at build. 2019-10-23 15:01:31 +02:00
Joris Vink da959e8275 reset curl timer if nothing is running anymore. 2019-10-23 12:38:31 +02:00
Joris Vink 890eb78fd8 Improve our new JSON api a bit.
Allow JSON to be constructed via kore_json_create_item and its
handy macro family:
	- kore_json_create_object()
	- kore_json_create_array()
	- kore_json_create_string()
	- kore_json_create_number()
	- kore_json_create_literal().

Adds kore_json_item_tobuf() to convert a JSON item into a string
representation in a kore_buf data structure.

Renames the kore_json_get* family to kore_json_find* instead.

Allows for quite clean code:

	struct kore_buf		buf;
	struct kore_json_item	*root;

	root = kore_json_create_object(NULL, NULL);
	kore_json_create_string(root, "hello", "world");
	kore_json_create_number(root, "value", 2.241);

	kore_buf_init(&buf, 128);
	kore_json_item_tobuf(root, &buf);

	kore_json_item_free(root);
	kore_buf_cleanup(&buf);
2019-10-22 23:56:47 +02:00
Joris Vink 790d020ce9 Stop a python coro from getting stuck with httpclient.
In cases where a request is immediately completed in libcurl its multi
handle and no additional i/o is happening a coro can get stuck waiting
to be run.

Prevent this by lowering netwait from KORE_WAIT_INFINITE if there
are pending python coroutines.
2019-10-22 17:06:32 +02:00
Joris Vink a119f18a23 Adjust to new kore_curl_init(). 2019-10-21 13:29:26 +02:00
Joris Vink b64ae5d111 Allow kore_curl calls to be synchronous.
Changes kore_curl_init() to take a flag parameter, much like pgsql api
in which you specify KORE_CURL_ASYNC or KORE_CURL_SYNC.

If KORE_CURL_ASYNC is specified, Kore will behave as before.
If Kore_CURL_SYNC is specified, Kore will execute the libcurl immediately
and return once it has been completed.
2019-10-21 13:26:44 +02:00
Joris Vink 22ebfae240 missing kore_free() 2019-10-20 23:55:08 +02:00
Joris Vink 00f505e3f3 simplify strtod() error checking.
we can just check if errno is ERANGE and drop d == 0 comparison.
2019-10-20 23:43:54 +02:00
Joris Vink 88330a5fcd missing kore_free() 2019-10-20 23:42:37 +02:00
Joris Vink e94cc2f3a8 Yikes, add the actual JSON parser code.. 2019-10-20 23:30:10 +02:00
Joris Vink 7da7141c21 remove FILEREF_DEBUG that sneaked in 2019-10-16 12:13:21 +02:00
Joris Vink cdc3347120 Add kore.sendmsg(object, worker=None) to the python api.
This allows you to send Python objects that can be run through pickle
to other worker processes.

If your application implements koreapp.onmsg() you will be able to receive
these objects.
2019-10-16 12:05:27 +02:00
Joris Vink ffb9fd2fc5 Align kore memory functions on 16-byte boundaries. 2019-10-15 21:33:43 +02:00
Joris Vink e90e3749a7 use Py_InitializeEx and do not install python signals. 2019-10-15 21:18:38 +02:00
Joris Vink bdc75556fe call PyErr_Clear() in python_callable(). 2019-10-15 16:03:39 +02:00
Joris Vink a1c1245ef7 don't call Py_DECREF on list in configure 2019-10-15 15:53:08 +02:00
Joris Vink 0f4d5294ad store active pid into apid so it does not overwritten. 2019-10-15 14:25:57 +02:00
Joris Vink bc33a5def4 Add kore.proc.pid, returns the PID of the proc. 2019-10-15 14:23:49 +02:00
Joris Vink ec249390b1 Allow building with python3.8 2019-10-15 10:16:53 +02:00
Joris Vink 3dcf94d1ae Add seccomp.bpf_stmt() method to Python api.
Allows a developer to create their own statements, the bpf_jmp equivalent
may follow later if I need it.
2019-10-07 13:44:31 +02:00
Joris Vink 0eb11794f5 Do not add keymgr its msg fd if not started.
Reshuffles the keymgr_active flag to keymgr.c and let it be figured out
from inside kore_server_start() instead of the worker init code.
2019-10-07 10:31:35 +02:00
Joris Vink 7209a67d47 unbreak DEBUG builds 2019-10-04 19:24:57 +02:00
Joris Vink 97523e2768 only register tls related msg callbacks if needed 2019-10-04 19:20:37 +02:00
Joris Vink 7bc1a92192 remove norwegian debug 2019-10-04 12:44:37 +02:00
Joris Vink 6c8970651c s/hook/method 2019-10-04 11:34:49 +02:00
Joris Vink b0cf42726d Do not start keymgr if no tls enabled servers are present 2019-10-04 11:29:45 +02:00
Joris Vink 11cc5d304f let kore_python_seccomp_hook() take the hook name 2019-10-04 11:28:14 +02:00
Joris Vink 8bbdaedf94 Allow configuring seccomp on Linux via the python api.
A new hook in the koreapp class is called right before seccomp
is enabled. This hook receives a Kore seccomp object which has
the following methods:

	seccomp.allow("syscall")
	seccomp.allow_arg("syscall", arg, value)
	seccomp.allow_flag("syscall", arg, flag)
	seccomp.allow_mask("syscall", arg, mask)

	seccomp.deny("syscall")
	seccomp.deny_arg("syscall", arg, value, errno=EACCES)
	seccomp.deny_flag("syscall", arg, flag, errno=EACCES)
	seccomp.deny_mask("syscall", arg, mask, errno=EACCES)

This allows you to finetune the seccomp filters for your application
from inside your koreapp.
2019-10-04 10:59:48 +02:00
Joris Vink bcf0355704 Add clock_gettime to keymgr process 2019-10-03 15:55:19 +02:00
Frederic Cambus 85981e22cc Allow the nanosleep syscall, it is used in kore_accesslog(). 2019-10-03 15:53:36 +02:00
Joris Vink 5e6a8d42cf include sys/socket.h in net.c 2019-10-03 12:19:36 +02:00
Joris Vink d0e46adfb1 kill norwegian debug 2019-09-27 20:08:16 +02:00
Joris Vink 46375303cb Allow multiple binds on new server directive. 2019-09-27 20:00:35 +02:00
Joris Vink be70118bf2 move l->tls check upwards 2019-09-27 12:33:48 +02:00
Joris Vink 46127459d9 fix BSDs after last commit 2019-09-27 12:31:17 +02:00
Joris Vink 7350131232 Allow listening of tls/notls ports at the same time.
Before kore needed to be built with NOTLS=1 to be able to do non TLS
connections. This has been like this for years.

It is time to allow non TLS listeners without having to rebuild Kore.

This commit changes your configuration format and will break existing
applications their config.

Configurations now get listener {} contexts:

listen default {
	bind 127.0.0.1 8888
}

The above will create a listener on 127.0.0.1, port 8888 that will serve
TLS (still the default).

If you want to turn off TLS on that listener, specify "tls no" in that
context.

Domains now need to be attached to a listener:

Eg:
	domain * {
		attach	default
	}

For the Python API this kills kore.bind(), and kore.bind_unix(). They are
replaced with:

	kore.listen("name", ip=None, port=None, path=None, tls=True).
2019-09-27 12:27:04 +02:00
Joris Vink 0d72f11902 make sure we can still run normal apps even with PYTHON=1 2019-09-26 20:38:02 +02:00
Joris Vink f725ca228c alter python skeleton from kodev create -p.
adds the kore.config.file setting (required a fix for -c) and the
kore.config.deployment option is set to "development".
2019-09-26 19:58:13 +02:00
Joris Vink 555856ab0a fix usage for python builds.
while here, force a module or script as a cli argument and
fix kodev run to pass the config if inside of a module.
2019-09-26 16:41:52 +02:00
Joris Vink b7163f35ce allow readlink syscall 2019-09-26 16:29:45 +02:00
Joris Vink d4ba6f3485 allow more net related syscalls for python. 2019-09-26 16:13:42 +02:00
Joris Vink 9d7ef805f0 hide rcall properly if needed 2019-09-26 16:05:01 +02:00
Joris Vink 937c39f041 Many many Python improvements.
- Kore can now fully be configured via Python code if one wants nothing to
  do with configuration files.

- Kore can now start single python files and no longer requires them to be
  inside a module directory.

- Pass all regex capture groups to the handler methods, allowing you to
  get access to them immediately.

- Change python websocket_handshake to take callable objects directly.

- Added a new deployment configuration option. If set to "dev" or
  "development" Kore will automatically foreground, no chroot / etc.
  If set to "production" Kore *will* chroot, drop privs, etc.

- Many more..

These are all backported from a project that I was working on a while
ago. I decided these should go back into mainline Kore.
2019-09-26 15:49:00 +02:00
Joris Vink 296fe7a6d4 seccomp improvements.
More BPF helper macros, more helper for granular syscall checking.
Use these throughout kore where it makes sense.

The new helpers:

- KORE_SYSCALL_DENY_ARG(name, arg, value, errno):
	Deny the system call with errno if the argument matches value.

- KORE_SYSCALL_DENY_MASK(name, arg, mask, errno):
	Deny the system call with errno if the mask argument does not match
	the exact mask given.

- KORE_SYSCALL_DENY_WITH_FLAG(name, arg, flag, errno):
	Deny the system call with errno if the argument contains the
	given flag.

The reverse also exists:

- KORE_SYSCALL_ALLOW_ARG()
- KORE_SYSCALL_ALLOW_MASK()
- KORE_SYSCALL_ALLOW_WITH_FLAG()
2019-09-26 13:51:53 +02:00
Joris Vink 4ed6136693 add 2 more missing syscalls for musl to keymgr. 2019-09-26 10:20:30 +02:00
Joris Vink 9dc12af1a4 add rt_sigprocmask for keymgr seccomp policy 2019-09-26 10:06:32 +02:00
Joris Vink a64be43ccf add writev to keymgr, needed on some platforms 2019-09-26 10:02:58 +02:00
Joris Vink c6c253305a swap sockets to use send/recv and update seccomp. 2019-09-26 09:53:51 +02:00
Joris Vink 0314521658 shuffle some stuff around 2019-09-26 09:28:12 +02:00
Joris Vink aafecb9485 Make sure filters from the hook are added first.
This allows user seccomp filters to be added before the kore ones which
means developers can override our own settings.
2019-09-26 09:22:33 +02:00
Joris Vink c1affbbd74 simplify bpf rule generation, add deny macro. 2019-09-25 23:41:43 +02:00
Joris Vink 61b8a9e90d fix tpyo 2019-09-25 16:14:58 +02:00
Joris Vink de97318a95 hide seccomp logs behind !quiet 2019-09-25 16:14:17 +02:00
Joris Vink 4bc39f19af add lstat to worker seccomp filter 2019-09-25 15:44:03 +02:00
Joris Vink eab94eda49 move libressl support to minimum 3.3.0. 2019-09-25 15:39:39 +02:00
Joris Vink 54ca865c11 add a few more syscalls for keymgr.
different platforms may do different things
2019-09-25 15:04:12 +02:00
Joris Vink 68e90507f4 properly seccomp keymgr 2019-09-25 14:41:09 +02:00
Joris Vink cd9971247c Add seccomp syscall filtering to kore.
With this commit all Kore processes (minus the parent) are running
under seccomp.

The worker processes get the bare minimum allowed syscalls while each module
like curl, pgsql, etc will add their own filters to allow what they require.

New API functions:
    int kore_seccomp_filter(const char *name, void *filter, size_t len);

    Adds a filter into the seccomp system (must be called before
    seccomp is enabled).

New helpful macro:
    define KORE_SYSCALL_ALLOW(name)

    Allow the syscall with a given name, should be used in
    a sock_filter data structure.

New hooks:
    void kore_seccomp_hook(void);

    Called before seccomp is enabled, allows developers to add their
    own BPF filters into seccomp.
2019-09-25 14:31:20 +02:00
Joris Vink d99d0b2d77 add lock.trylock() and make lock.release() sync. 2019-09-20 10:03:43 +02:00
Joris Vink c3b2a8b2a2 fix NOHTTP builds 2019-09-20 09:37:02 +02:00
Joris Vink 868b0a71a2 make coro tracing available at runtime, always.
call kore.corotrace(True) to enable it and kore.corotrace(False) to disable.
2019-09-18 10:55:13 +02:00
Joris Vink 95506204e8 Add kore.task_kill() to the python api.
Allows killing of coroutines, given their task id.

The kore.task_create() method now returns the task id for a newly
created task to the caller.

While here, change the coroutine task id to a uint32 from uint64.
There is no need for it to be 64bit. (famous last words)
2019-09-17 20:04:35 +02:00
Joris Vink c1653a5254 hide kore.coroname() behind PYTHON_CORO_TRACE. 2019-09-16 20:33:14 +02:00
Joris Vink 019006620d allow coroutines to set friendly names. 2019-09-16 20:31:25 +02:00
Joris Vink 960fabe44c s/PYTHON_CORO_DEBUG/PYTHON_CORO_TRACE 2019-09-16 20:25:34 +02:00
Joris Vink 1ee1021aba trace created/deleted. 2019-09-16 20:21:04 +02:00
Joris Vink 9bcccb9483 Add coroutine debugging.
If built with PYTHON_CORO_DEBUG in CFLAGS Kore will spew out coroutine
traces while running. These traces include the filename, function and line
number where the coroutines are waking up, running and suspended.
2019-09-16 20:11:42 +02:00
Joris Vink dbb99a56d1 move new/wokenup/suspended coros to end of lists. 2019-09-16 19:34:47 +02:00
Joris Vink 58a6b4e331 trailing space fix 2019-09-13 23:22:38 +02:00
Joris Vink 3207ce8526 pgsql improvements round 2
- kill PQsetnonblocking(), it's not what one thinks.
- keep doing the PQconsumeInput()/PQisBusy() dance until
  the former clears all read data.
2019-09-13 23:20:51 +02:00
Joris Vink f2472ba485 allow python modules to set progname. 2019-09-04 20:37:33 +02:00
Joris Vink 8e858983bf python pgsql changes.
- decouple pgsql from the HTTP request allowing it to be used in other
  contexts as well (such as a task, etc).

- change names to dbsetup() and dbquery().

eg:

result = kore.dbquery("db", "select foo from bar")
2019-09-04 19:57:28 +02:00
Joris Vink f3b7cba58c Call PQConsumeInput() again after PQisBusy().
Prevents a stall in case there is still data in the read end of the socket
but PQisBusy() told us to not fetch a result yet. In that case we end up
stalling due to epoll not giving us another EPOLLIN event due to EPOLLET.
2019-09-04 19:19:52 +02:00
Joris Vink b54b93536d Use strdup() when munging environment pointers. 2019-08-27 13:12:44 +02:00
Joris Vink c32880e12b http_argument_decode() can fail. 2019-08-02 11:34:45 +02:00
Joris Vink 0b9e174af4 http_argument_urldecode() could fail. 2019-07-20 20:35:58 +02:00
Joris Vink 0f58de36f4 Be a little less strict on url decoding. 2019-07-16 21:31:32 +02:00
Joris Vink f2e17c1384 return NULL if PyUnicode_FromString fails. 2019-07-16 21:31:19 +02:00
Joris Vink 5055f3c872 add kore.worker() to the python api.
returns the current worker number or None if on parent.
2019-06-16 19:57:48 +02:00
Joris Vink 4a64b4f07b Improve curl timeout handling.
In case libcurl instructs us to call the timeout function as soon
as possible (timeout == 0 in curl_timeout), don't try to be clever
with a timeout value of 10ms.

Instead call the timeout function once we get back in the worker
event loop. This makes things a lot snappier as we don't depend
on epoll/kqueue waiting for io for 10ms (which actually isn't 10ms...).
2019-06-13 12:59:17 +02:00
Joris Vink 574c9a7084 make sure kodev run works in python modules. 2019-06-13 11:18:10 +02:00
Joris Vink 3114f8d8d0 Improve python experience.
- If Kore is built with PYTHON=1 you can now specify the module that
  should be loaded on the command-line.

     eg: $ kore -frn myapp

- Add skeleton generation for python applications to kodev.

     eg: $ kodev create -p myapp

This should make it a whole lot easier to get started with kore python.
2019-06-12 23:35:43 +02:00
Joris Vink a46447b1f9 set hdlr_extra to NULL in pyhttp_iterobj_disconnect. 2019-06-12 21:09:57 +02:00
Joris Vink 6c18856a3e get rid of mtime in modules.
kore should always reload things when it was told to reload things.
there are use cases were a module reload is because the code itself changed.
2019-06-09 23:24:53 +02:00
Frederic Cambus bae117b68c Set req->agent to "-" if it is NULL, as do other major HTTP servers.
This avoids passing a NULL value to a format string when writing a log
entry.
2019-06-09 21:36:12 +02:00
Joris Vink d4f4e007e0 make sure prereqs are in order 2019-06-07 21:33:00 +02:00
Joris Vink 6ed3ac4658 kill debug 2019-06-07 21:25:18 +02:00
Joris Vink 3cc7d6e238 Allow kore.prerequests to be async. 2019-06-07 21:06:54 +02:00
Joris Vink 9cc58d45c1 Python HTTP improvements.
1) Add @kore.prerequest python decorator.

Using this decorator on a function will cause that function
to always be executed *before* any page handler is run.

eg:

@kore.prerequest
def _check(req):
    if req.method == kore.HTTP_METHOD_POST:
        req.populate_post()

2) Allow attributes to be set on the pyhttp object.
2019-06-05 23:45:45 +02:00
Joris Vink 89e58fa474 Improve iterator support for Python req.response().
If the connection on which we are about to send the response was
marked as disconnecting, do not go ahead and hook into the disconnect
callback (it will never be called, it is already disconnecting).

Instead just return, the connection will be removed anyway.
2019-06-05 10:35:47 +02:00
Joris Vink 93b1d621d7 Remove memleak from Python httpclient.
We grab a reference to the pyhttp_client for the client_op data structure
but never removed it. This caused the pyhttp_client object to never
be released when out of scope.
2019-06-05 10:27:20 +02:00
Joris Vink a4d18ca276 Add HTTP runlocks.
A way to serialize access to HTTP page handlers in case you are
using some asynchronous api such as pgsql or libcurl stuff.
2019-06-02 16:29:54 +02:00
Joris Vink 1686ec22e6 Some C pgsql api improvements. 2019-06-01 23:14:50 +02:00
Joris Vink ff7c85460c Wake up HTTP requests if python coros finish immediately.
python_coro_create() puts the HTTP request to sleep, but if they
finish immediately they will be removed from the list and should
properly be woken up or they are removed from the wrong list.
2019-05-30 17:15:08 +02:00
Joris Vink a8aff8b737 Improve curl error string handling.
Introduce kore_curl_strerror(), use this in kore_curl_logerror()
instead of assuming our errbuf has been populated.

Also use it in the python httpclient when throwing an exception rather
then looking at the errbuf member which may or may not be empty.
2019-05-30 14:25:04 +02:00
Joris Vink 88553cd2dd Immediately remove completed HTTP requests.
No need to wait until the next time http_process() is called, which
could result in HTTP requests backing up even though we are processing
them at a fast pace.
2019-05-29 20:30:43 +02:00
Joris Vink 07fc7a9097 Improve HTTP processing.
If netwait is INFINITE but there are requests pending reduce the
netwait back down to 100ms so we keep processing them.
2019-05-29 15:27:44 +02:00
Joris Vink c2d9f1413c Fix two concurrency issues with kore.lock().
1) If a coroutine was waken up by another releasing a lock it was waiting
on we would incorrectly remove the pylock_op when the newly awoken coroutine
hits the iternext for pylock.

2) If a coroutine attempts to grab a lock it was woken up for only to fail
we did not properly rearm the coroutine to be woken up later to try again.
2019-05-29 15:25:31 +02:00
Joris Vink 5f0153ba0e Fix unix binds on BSD families. 2019-05-28 21:44:46 +02:00
Joris Vink b4aba48fad small python improvements.
- Check for an exception when in a timer context and log it if uncaught.
- Make sure tlsverify is settable regardless of client certs being specified.
2019-05-16 21:07:42 +02:00
Joris Vink a10dfe03fe make sure user-defined headers are set for > 500.
a commit done in 2018 prevented http responses with error codes
> 500 to include any user-set headers, preventing a developer
to include things like content-type etc.

reported by Arun Babu via users@
2019-05-12 20:53:27 +02:00
Joris Vink 53f042a5b4 add stdint.h header to keymgr.c 2019-05-09 20:55:49 +02:00
Joris Vink 3299f60df4 tiny curl fixes. 2019-05-05 21:16:42 +02:00
Joris Vink 503f42074c http_timeout must be 0 when upgrading to websockets
otherwise kore will timeout an established websocket connection
after http_timeout seconds.
2019-05-05 14:47:04 +02:00
Joris Vink fe84997ce9 add option to use unix paths via kore.httpclient 2019-05-03 15:31:16 +02:00
Joris Vink 3c9a141cd0 allow an iterator to be passed to req.response().
if an iterator is passed kore will send the response with
transfer-encoding: chunked and call the iterator for every
chunk that was sent.

The iterator must return a utf-8 string.

Works wonderful with TemplateStream from jinja2.
2019-05-03 13:42:34 +02:00
Joris Vink 12fc1396c1 use / to split up name/version. 2019-04-30 20:45:56 +02:00
Joris Vink 502368a27e set user-agent in the curl component. 2019-04-30 20:39:46 +02:00
Joris Vink 403938d8e9 make native proctitle better.
count how much space is available for a mangled process title
only once, and use that as reference later.
2019-04-29 21:08:58 +02:00
Joris Vink d0a6958747 Let http_state_create() take an "onfree" callback.
This function is called when an HTTP request is being free'd,
allowing you to perform any sort of state cleanup attached
to the HTTP request.
2019-04-28 21:48:16 +02:00
Joris Vink 9ac77d0c9a make sure CURL_CSELECT_ERR is OR'd into flags. 2019-04-28 21:44:59 +02:00
Joris Vink e54886960e objs is a pointer, and use Py_XDECREF on it. 2019-04-25 23:25:19 +02:00
Joris Vink d0d0bdeb4f Improve pgsql support.
- Add kore_pgsql_query_param_fields() which allows you to pass in the
  arrays for values, lengths and formats yourself.

- Add kore_pgsql_column_binary() which will return 1 if the given column
  index contains a binary result or 0 if it contains a text result.

- Change the query call in req.pgsql() for Python to always use the
  parameterized queries.

This adds the 'params' and 'binary' keywords to the req.pgsql method.

Eg:
	result = await req.pgsql("db", "INSERT INTO foo (field) VALUES($1"),
	    params=["this is my value"])
2019-04-25 23:13:13 +02:00
Joris Vink 994f428a8d add cabundle keyword to kore.httpclient() 2019-04-24 21:09:24 +02:00
Joris Vink 6bd3d92fe2 unbreak. 2019-04-24 18:30:07 +02:00
Joris Vink a6af458cd2 add tlsverify keyword to kore.httpclient(). 2019-04-24 18:28:23 +02:00
Joris Vink 1b9ba84ab0 missing return value check. 2019-04-24 09:33:56 +02:00
Joris Vink a9683caed5 missing header 2019-04-24 00:16:51 +02:00
Joris Vink 2c88bc6120 Add asynchronous libcurl support.
This commit adds the CURL=1 build option. When enabled allows
you to schedule CURL easy handles onto the Kore event loop.

It also adds an easy to use HTTP client API that abstracts away the
settings required from libcurl to make HTTP requests.

Tied together with HTTP request state machines this means you can
write fully asynchronous HTTP client requests in an easy way.

Additionally this exposes that API to the Python code as well
allowing you do to things like:

	client = kore.httpclient("https://kore.io")
	status, body = await client.get()

Introduces 2 configuration options:
	- curl_recv_max
		Max incoming bytes for a response.

	- curl_timeout
		Timeout in seconds before a transfer is cancelled.

This API also allows you to take the CURL easy handle and send emails
with it, run FTP, etc. All asynchronously.
2019-04-24 00:15:17 +02:00
Joris Vink c89ba3daa3 check http timeouts better 2019-04-12 14:26:47 +02:00
Joris Vink 612fc034fa remove debug 2019-04-11 20:54:29 +02:00
Joris Vink aa49e181b6 Add http_[header|body]_timeout.
If the HTTP request headers or the HTTP body have not arrived before
these timeouts expire, Kore will send a 408 back to the client.
2019-04-11 20:51:49 +02:00
Joris Vink a191445f76 set body length+offset to 0 when populating data.
otherwise this isn't properly picked up by http_body_read() later
if dealing with in-memory HTTP bodies and you get inconsistent behaviour.
2019-04-02 22:26:44 +02:00
Joris Vink 5d16a7a123 make sure stdint.h is always included.
unbreaks NOTLS=1 and NOSENDFILE=1 builds on several platforms.

reported by Sam on irc, thanks.
2019-03-29 19:25:27 +01:00
Joris Vink a66d259271 kill debug line 2019-03-29 19:15:10 +01:00
Joris Vink d2aa64df5c add kore_proctitle().
manipulates the argv+environ pointers to get a sensible process title
under linux / darwin.
2019-03-29 16:24:14 +01:00
Joris Vink 92fb4974b1 only deschedule if not -1. 2019-03-27 22:15:35 +01:00
Joris Vink 39ffa047ca remove dh parameter check at configure time.
this is done when the workers startup if a domain with tls was used.
2019-03-26 21:44:44 +01:00
Joris Vink e4caac9e0c Be more clear when an invalid TLS setup is found 2019-03-26 21:36:45 +01:00
Joris Vink bf42e56c7d kill whitespace 2019-03-26 20:24:46 +01:00
Joris Vink 4ca7f29649 Add a concurrency parameter to kore.gather() 2019-03-25 10:13:52 +01:00
Joris Vink e1766e74ba always capture worker processes exiting.
even if they terminated normally.
2019-03-22 10:29:14 +01:00
Joris Vink 4238431b9e Add worker_death_policy setting.
By default kore will restart worker processes if they terminate
unexpected. However in certain scenarios you may want to bring down
an entire kore instance if a worker process fails.

By setting worker_death_policy to "terminate" the Kore server will
completely stop if a worker exits unexpected.
2019-03-22 09:49:50 +01:00
Joris Vink cd80685d9d get rid of pyko 2019-03-21 21:37:16 +01:00
Joris Vink 58c2739dee If fd is -1 in pysock_op_iternext just return None.
None in pysock signals EOF and if it was closed under our feet we should
relay that properly.
2019-03-21 14:44:41 +01:00
Joris Vink 370041656e Get rid of WORKER_LOCK_TIMEOUT.
Instead let the workers send a message on the msg channel to each
other when they have given up the accept lock and it is now available
to be grabbed.
2019-03-21 14:03:11 +01:00
Joris Vink 8b0279879a rework timers so they fire more predictably.
this change also stops python coroutines from waking up very
late after their timeout has expired.

in filerefs, don't prime the timer until we actually have something
to expire, and kill the timer when the last ref drops.
2019-03-21 10:17:08 +01:00
Joris Vink ce012e7bd5 set fd to -1 on close. 2019-03-21 10:09:24 +01:00
Joris Vink d371454dad do not grab the result if it was in a gatherop 2019-03-19 13:58:00 +01:00
Joris Vink 8afaac9fde log pid number of worker upon start. 2019-03-19 13:42:22 +01:00
Joris Vink 681a88d535 simplify af_unix recvfrom/sendto support. 2019-03-19 13:27:50 +01:00
Erik Karlsson d60994abb5 add python sendto recvfrom abstract socket support 2019-03-19 11:11:44 +01:00
Joris Vink 9caa45a050 Allow python validator methods to be async. 2019-03-18 09:34:31 +01:00
Joris Vink c3ab570f56 Append full module path if it is a directory. 2019-03-16 16:13:52 +01:00
Joris Vink ec7c8b8e1d fix a few minor problems with pylock.
- when pylocks are deallocated we ended up deleting the wrong object.
- do not call Py_DECREF on the op when inside the op its iternext call.
2019-03-14 17:31:57 +01:00
Joris Vink 2217c7a2c8 pysock: call disable read/write only when needed 2019-03-14 09:27:13 +01:00
Joris Vink d41054bd26 remove the socket from the event queue on dealloc. 2019-03-13 16:01:42 +01:00
Joris Vink b06160c768 Make sure coros are woken up on eof. 2019-03-13 15:45:55 +01:00
Joris Vink 3b4574d791 Rework pysocket async/await.
Attach the events directly to the pysocket data structure instead of
one event per pysocket_op.

Makes the code easier, gives us a good performance boost and reduces
the number of system calls required when doing an await on a socket.
2019-03-13 11:07:15 +01:00
Joris Vink 01f9b4fcde Check for EAGAIN when calling connect(2).
Linux does this under certain conditions.
2019-03-12 20:04:08 +01:00
Joris Vink eb9b7f7b14 explicitly include sys/types.h
some smaller libc variants do not include this from sys/param.h.
2019-03-06 09:29:46 +01:00
Joris Vink 61863bfd3a kill recv() timer if we have data.
also reset any exception that is set when we have successfully
read data from a socket.
2019-03-04 16:37:25 +01:00
Joris Vink a0c203f507 remove norwegian debug 2019-03-01 20:57:31 +01:00
Joris Vink 31a9a70d5a cleanup. 2019-03-01 20:56:03 +01:00
Erik Karlsson 01c1a8f8f8 support AF_UNIX in recvfrom()/sendto() python sockets 2019-03-01 20:13:28 +01:00
Joris Vink 2057f4db58 sprinkle more kore_quiet around 2019-02-27 19:59:31 +01:00
Joris Vink 1ebd82969c Add timeout support to proc.recv() 2019-02-26 15:22:55 +01:00
Joris Vink f4cd70956b Add an optional timeout to socketop.recv(). 2019-02-25 10:35:00 +01:00
Joris Vink bf1e8e5ffb bump copyright to 2019 2019-02-22 16:57:28 +01:00
Joris Vink 3dbb80463a allow double quotes in kore.proc argument lists. 2019-02-22 16:56:34 +01:00
Joris Vink 66305e551a sig_recv is extern'd in kore.h 2019-02-11 14:01:25 +01:00
Joris Vink 8a8b08f1e6 use %u when logging worker_rlimit_nofiles 2019-02-11 14:00:28 +01:00
Joris Vink 429159ee3c reprime idle timer if SSL_accept() fails.
reported by Daniel Chavez (@dacechavez)
2019-02-08 15:40:00 +01:00
Joris Vink 8040103458 fix for multiple consumers for the async queues. 2019-01-22 11:23:59 +01:00
Joris Vink 39467847fb remove SSL_get_verify_result() check.
If peer verification is turned on this becomes part of the handshake
process anyway and SSL_accept() will fail when appropriate.
2019-01-21 10:36:50 +01:00
Joris Vink 3f083d6126 deal with crls being expired / not-yet-valid.
if a crl is expired or not-yet-valid SSL_get_verify_result()
will return these errors too so check for them explicitly
instead of depending on X509_V_OK.

found by @dacechavez
2019-01-19 11:49:54 +01:00
Joris Vink d1e87c1a54 deal with PyObject_CallObject() returning NULL. 2019-01-15 10:20:13 +01:00
Joris Vink d6b05bcff7 always force reload cert so we get a new x509 store.
Otherwise older OpenSSL or current LibreSSL will fail to add the new
CRL as they still match on subject name rather then hash of the CRL data.
2019-01-14 20:57:40 +01:00
Joris Vink 73cdbd1a01 Let CRLs be reloadable via keymgr.
With these changes CRLs can be reloaded like certificates
by sending a SIGUSR1 to the parent process.

Track mtime on both certificate files and CRL files as well
and only submit them to the workers if this has changed.
2019-01-14 11:41:50 +01:00
Joris Vink b07cc0237c Support recvfrom()/sendto() on kore python sockets. 2019-01-08 17:49:00 +01:00
Joris Vink a868ff2b25 wrap accesslog calls in !KORE_NO_HTTP. 2018-12-22 09:41:55 +01:00
Joris Vink 9aa0e95643 Rework accesslog handling.
Move away from the parent constantly hitting the disk for every
accesslog the workers are sending.

The workers will now write their own accesslogs to shared
memory before the parent will pick those up. The parent
will flush them to disk once every second or if they grow
larger then 1MB.

This removes the heavy penalty for having access logs
turned on when you are dealing with a large volume
of requests.
2018-12-22 09:25:00 +01:00
Joris Vink 0d6a188b01 close fd if we're a directory in filemap. 2018-12-17 16:22:36 +01:00
Joris Vink 61b385ae11 do not set CONN_CLOSE_EMPTY for 1.0 until we reply. 2018-11-30 22:12:43 +01:00
Joris Vink ce3b60b8c8 keep a reference around for tracer. 2018-11-29 09:53:26 +01:00
Joris Vink d9f543ef5b Allow user-supplied tracer callback. 2018-11-29 09:51:24 +01:00
Joris Vink b400fdcd9f include sys/param.h in python.c 2018-11-29 09:11:31 +01:00
Joris Vink fd1ab5879d KODEV_OUTPUT tells kodev the outdir of the binary.
eg:

$ env KODEV_OUTPUT=/tmp kodev build

will place the resulting binary under /tmp/<binary>
2018-11-28 14:24:42 +01:00
Joris Vink 8dd075e71c style 2018-11-28 14:22:27 +01:00
Joris Vink e3efeb9465 Move assets.h into object_dir. 2018-11-28 14:19:32 +01:00
Joris Vink c431c2bf72 Add support to obtain peer certificate from Python
This will return the DER encoded bytes representing the peer certificate.
2018-11-28 11:28:07 +01:00
Joris Vink 2d8874dd2a change accept threshold default to 16. 2018-11-16 11:37:09 +01:00
Joris Vink 4e70636269 Reduce accept lock accept lockout time to 100ms. 2018-11-16 11:35:53 +01:00
Joris Vink 5c8efde841 allow foo.method symbols in python.
this way you can create page handlers that reside inside
of other objects.

eg:

static / restapi.index
2018-11-16 11:07:21 +01:00
Joris Vink baafa4897e Add -q flag.
If specified Kore will run quietly and only log important messages.
2018-11-15 16:01:37 +01:00
Joris Vink cb482d8e8f Always use http_response_stream() in req.response.
Keep around the python bytes object while we stream the contents
of it out over the wire. This avoids an entire copy of the data.
2018-11-14 10:03:32 +01:00
Joris Vink 966eaf8f7a Add a kore_python_preinit() hook.
This is called before the python initialization is completed
and allows developers to inject their own built-in methods.
2018-11-12 09:01:05 +01:00
Joris Vink 3925ba60ad don't call close() no underlying socket. 2018-11-12 09:00:36 +01:00
Joris Vink 4cc9e216bb Don't call close() on the python socket on dealloc 2018-11-06 16:54:26 +01:00
Joris Vink 2dd66586ff several python improvements.
- add kore.time() as equivalent for kore_time_ms().
- call waitpid() until no more children are available for reaping otherwise
  we risk missing a process if several die at the same time and only one
  SIGCHLD is delivered to us.
- drain a RECV socket operation if eof is set but no exception was given.
2018-10-30 20:28:27 +01:00
Joris Vink 5456f2e1d5 save/restore coro_running in pygather_op_dealloc(). 2018-10-30 12:37:30 +01:00
Joris Vink 062c80c1ac start enforcing a mininum number of elms in pools. 2018-10-30 10:41:49 +01:00
Joris Vink 71c145932c grow kore_pools at a slower rate.
Before we just doubled in size the second we exhausted a pool instead
of doing a more controlled expansion.

Now we will expand at 25% of the initial elm count whenever we need to.

Will help with memory pressure in certain scenarios.
2018-10-30 10:36:18 +01:00
Joris Vink bb00deca2c don't leak op in error path 2018-10-29 21:18:38 +01:00
Joris Vink 1c30da855c Add kore.gather() to the python api.
Allows one to run coroutines concurrently and gather all their
results in a single returned list.

If any of the coroutines throw an exception the exception is
returned as the value of that coroutine in the returned list.
2018-10-29 21:16:08 +01:00
Joris Vink 740acb4760 make sure we can still build vs 1.1.0 2018-10-29 21:11:29 +01:00
Joris Vink 818c7847cd simplify even more 2018-10-29 20:43:40 +01:00
Joris Vink ae72b21d35 make sure older openssl/libressl builds work. 2018-10-29 20:42:28 +01:00
Joris Vink 339df66fd5 Add support for TLS 1.3 via OpenSSL 1.1.1.
This commit removes TLS 1.0 support no matter what OpenSSL
you are linking against.

Changes the value of tls_version from 1.2 to both. Meaning if
you link with OpenSSL 1.1.1 you will get 1.2 + 1.3.
2018-10-29 20:38:58 +01:00
Joris Vink dda2e1fb2c Some things still talk http/1.0. 2018-10-26 21:24:51 +02:00
Joris Vink e2651889e0 Add asynchronous subprocess support.
This adds kore.proc to the python runtime allowing async processing
handling:

The kore.proc method takes the command to run and an optional timeout
parameter in milliseconds. If the process did not exit normally after
that amount of time a TimeoutError exception is raised.

For instance:

async def run(cmd):
	proc = kore.proc(cmd, 1000)

	try:
		await proc.send("hello")
		proc.close_stdin()
	except TimeoutError:
		proc.kill()

	retcode = await proc.reap()

	return retcode
2018-10-26 19:19:47 +02:00
Joris Vink ea7ea48840 actually just query for HW_NCPU on all BSDs. 2018-10-25 19:31:48 +02:00
Joris Vink 9d9e9dcec2 set cpu_count to 1 for unknown BSDs 2018-10-25 19:30:10 +02:00
Joris Vink 892814e353 Add kore_[parent|worker]_teardown().
If exists these functions are called when the worker is exiting
and when right before the parent exists.

Allows for cleanup code for applications if need to do cleanup on exit.
2018-10-23 21:46:34 +02:00
Joris Vink fc5fc4f4ab make sure timers don't go out of scope. 2018-10-23 21:36:19 +02:00
Joris Vink 8ea32983ae Add kore.suspend(delay) to python.
Will suspend the coroutine for a number of milliseconds.

Example:

async def page(req):
	await kore.suspend(1000)
	req.response(200, b'')
2018-10-23 21:32:08 +02:00
Joris Vink 47776a9fbb Hook kore timers into python. 2018-10-23 20:44:43 +02:00
Joris Vink c41c1db303 Add kore_shutdown().
Allows workers to cleanly initiate a shutdown of the
entire server process.
2018-10-23 19:49:42 +02:00
Joris Vink 4c35073232 Use number of cpu cores if no worker count is set. 2018-10-22 21:27:17 +02:00
Joris Vink b70d1ee80f Add a locking mechanism in pykore.
Support the async with syntax:

	lock = kore.lock()

	async with lock:
		# your block

Fix some small issues with other parts of the python system.
2018-10-22 20:09:23 +02:00
Joris Vink 1ac131c48f If we hit the accept threshold, unlock worker. 2018-10-22 09:01:05 +02:00
Joris Vink 8be316ac0c Let kore_worker_make_busy() set next_lock. 2018-10-22 09:00:55 +02:00
Joris Vink fad5c6ea6f Give pyqueues "popnow". 2018-10-22 08:28:03 +02:00
Joris Vink c8795b7d7f pyqueue improvements.
- cleanup queue if it gets deallocated.
- make sure waitables on a queue get removed if their pyqueue_op dies.
2018-10-21 21:58:34 +02:00
Joris Vink 4ae3d23c7e s/INCREF/DECREF 2018-10-18 22:24:58 +02:00
Joris Vink 0cda9ecfb0 Add an asynchronous queue mechanism.
This allows coroutines to submit messages to and pop
messages from a queue in an asynchronous way.
2018-10-18 22:15:21 +02:00
Joris Vink b5958f7d7d Add kore_parent_daemonized().
This is called for single binaries after the parent
process has called daemon().

Also fix kore_parent_configure() for !single binaries.
2018-10-18 17:18:41 +02:00
Joris Vink c172d49567 kodev improvements.
- Take CFLAGS, CXXFLAGS and LDFLAGS from environment
  if present and append them to the build.conf cflags

- Allow overriding of the OBJDIR via KORE_OBJDIR.

- Allow overriding of build flavor via KORE_BUILD_FLAVOR.
2018-10-17 11:30:14 +02:00
Joris Vink 300daeadef fix comment 2018-10-16 13:17:44 +02:00
Joris Vink 29202d7330 Make kore_python_log_error() public.
While here also make kore_module_load() return the
kore_module data structure pointer it just added
to the modules list.
2018-10-16 13:16:36 +02:00
Joris Vink 20a0103f1e Add async/await support for socket i/o in python.
This means you can now do things like:

	resp = await koresock.recv(1024)
	await koresock.send(resp)

directly from page handlers if they are defined as async.

Adds lots more to the python goo such as fatalx(), bind_unix(),
task_create() and socket_wrap().
2018-10-15 20:18:54 +02:00
Joris Vink c12f296743 missing EPOLLRDHUP check since event loop rework 2018-10-10 14:33:26 +02:00
Joris Vink c463ecb3cb Changes to the event loop inside of Kore.
Now anyone can schedule events and get a callback to work as long
as the user data structure that is added for the event begins
with a kore_event data structure.

All event state is now kept in that kore_event structure and renamed
CONN_[READ|WRITE]_POSSIBLE to KORE_EVENT_[READ|WRITE].
2018-10-09 19:34:40 +02:00
Joris Vink 2449a86085 missing addrtype -> family renames 2018-10-08 20:12:25 +02:00
Joris Vink 884d6d722e add listener to the list earlier. 2018-10-07 21:25:00 +02:00
Joris Vink 19044919b2 don't set nodelay on unix listener sockets 2018-10-07 21:21:37 +02:00
Joris Vink 9427ed8a2e rename unix to sun 2018-10-07 21:03:12 +02:00
Joris Vink 442bdef79b allow kore to bind to unix sockets via bind_unix. 2018-10-07 20:49:16 +02:00
Joris Vink f87624a459 Add missing KORE_NO_TLS guards.
Fixes NOTLS=1 builds on openssl 1.1.1
2018-10-01 10:36:33 +02:00
Joris Vink 7db3e4d946 fix compare match on db name.
from Kevin Lam via patches@
2018-09-19 07:18:46 +02:00
Joris Vink ba8d6301b3 style 2018-08-30 09:13:11 +02:00
Joris Vink 566fefd031 do not http_argument_urldecode for multipart data. 2018-08-16 14:11:28 +02:00
Joris Vink 754ba47cc4 shuffle stuff around so we're not hitting bad juju. 2018-08-13 13:07:32 +02:00
Joris Vink 8d3f73bc71 call fatalx() instead of fatal() in certain cases. 2018-08-13 13:01:27 +02:00
Joris Vink b0074ba45e Add fatalx().
If a worker process dies it automatically gets respawned by the
parent process, but sometimes you want the entire server to go down
if a worker encounters an error. This is what fatalx() does.

Calling fatalx() from a worker process will initiate a full shutdown
of the kore server you are running under.
2018-08-13 09:53:49 +02:00
Joris Vink cf92097bc2 log what dir we can't chdir() into. 2018-08-13 09:53:38 +02:00
Joris Vink 6ef8d59f7d more directly include assets.h 2018-08-13 09:16:28 +02:00
Joris Vink d1c8f95590 Show the reason why regcomp() failed if it does. 2018-08-10 08:06:09 +02:00