chiark / gitweb /
tripe
2 years agoserver/peer.c, etc.: Introduce who-goes-there protocol. mdw/wgt
Mark Wooding [Mon, 20 Sep 2021 20:13:27 +0000 (21:13 +0100)]
server/peer.c, etc.: Introduce who-goes-there protocol.

If a laptop (say) has been asleep for a short while, then its peer has
likely forgotten about it, but it will wait for a ping-timeout cycle
before attempting to reconnect.

Introduce a new protocol to speed this up.

  * When a packet which contains a ciphertext (e.g., `MSG_PACKET',
    `MISC_EPING') is received from an unknown sender, then send back a
    `MISC_WGT' (`who-goes-there') message quoting a prefix of the
    offending packet.  These are rate-limited so that TrIPE can't be
    used as an amplifier.  (Under adverse circumstances, we fall back to
    the old timeout mechanisms.)

  * When a `MISC_WGT' packet is received, we check to see whether the
    peer is configured with a `knock' string, and the quoted prefix
    matches a message we sent to that peer recently.  If so, we try to
    restart the key-exchange protocol.  Hence, an adversary who can't
    read the wire has very little chance of proviking a pointless
    key-exchange.  The 20 s cooling-off period still applies, so even an
    adversary who /can/ read the wire can't do too much harm.

2 years agoserver/tests.at: Abstract out the wait-for-knock machinery.
Mark Wooding [Mon, 20 Sep 2021 23:01:02 +0000 (00:01 +0100)]
server/tests.at: Abstract out the wait-for-knock machinery.

We'll want it again soon.

2 years agoserver/peer.c (p_txend): Rework the success path.
Mark Wooding [Mon, 20 Sep 2021 23:21:50 +0000 (00:21 +0100)]
server/peer.c (p_txend): Rework the success path.

Now it's less confusing to add other kinds of success actions here.  Not
that I wiol on-haven try to do this in an alternate future or anything.

2 years agoserver/peer.c: Add a flags argument to `p_txend'.
Mark Wooding [Mon, 20 Sep 2021 20:10:23 +0000 (21:10 +0100)]
server/peer.c: Add a flags argument to `p_txend'.

2 years agosvc/connect.in: Maintain time of last reconnect attempt and add rate limit.
Mark Wooding [Thu, 28 Apr 2022 14:16:46 +0000 (15:16 +0100)]
svc/connect.in: Maintain time of last reconnect attempt and add rate limit.

This isn't used yet, but it's there as a general mechanism.

Don't rate-limit reconnections provoked by the pinger internally because
(a) these are relatively infrequent anyway by the nature of the pinger
mechanism, and (b) the current rate-limiting strategy is simply to
ignore a reconnection request if we've already made an attempt fairly
recently, so there isn't (and it's not worth building) the necessary
machinery to defer reconnection, and determine whether a deferred
reconnection is still needed.

2 years agosvc/connect.in: Pass time into `Pinger._reconnect'.
Mark Wooding [Thu, 28 Apr 2022 14:15:45 +0000 (15:15 +0100)]
svc/connect.in: Pass time into `Pinger._reconnect'.

The callers already know.

2 years agosvc/connect.in: Stash the time in a variable rather than fetching inline.
Mark Wooding [Thu, 28 Apr 2022 14:12:54 +0000 (15:12 +0100)]
svc/connect.in: Stash the time in a variable rather than fetching inline.

2 years agosvc/connect.in: Change the idiom for handling peer nonexistence.
Mark Wooding [Thu, 28 Apr 2022 14:10:17 +0000 (15:10 +0100)]
svc/connect.in: Change the idiom for handling peer nonexistence.

Use `try/except/else' rather than `try/except return'.  It's hardly more
writing for a better code structure.

2 years agosvc/connect.in: Use new `FORCEKX -quiet' when attempting reconnect.
Mark Wooding [Thu, 28 Apr 2022 13:15:31 +0000 (14:15 +0100)]
svc/connect.in: Use new `FORCEKX -quiet' when attempting reconnect.

If the remote peer has forgotten us, and we must run a command to
reinform it of our existence, then we shouldn't immediately send key-
exchange messages because they'll probably be rejected.  Instead, use
the new `-quiet' option to cancel the cooling-off timer and wait for it
to contact us -- similarly to how we used `-cork' when adding the peer
in the first place.

2 years agoserver/admin.c: Add `-quiet' options to `forcekx' and `kill'.
Mark Wooding [Mon, 20 Sep 2021 23:18:30 +0000 (00:18 +0100)]
server/admin.c: Add `-quiet' options to `forcekx' and `kill'.

These will be useful in testing.  Document them anyway, even though
they're probably useless for any other purpose.

2 years agoMerge remote-tracking branch 'origin/mdw/master.found-crybaby'
Mark Wooding [Thu, 28 Apr 2022 15:22:01 +0000 (16:22 +0100)]
Merge remote-tracking branch 'origin/mdw/found-crybaby'

* origin/mdw/master.found-crybaby:
  server/bulkcrypto.c: Document the procedures for producing challenges.
  server/chal.c: Add a missing blank line.
  server/tripe-admin.5.in: Place the blame correctly for a couple of errors.
  svc/connect.8.in: Fix message formatting.
  server/tripe-admin.5.in: Remove incorrect blame on Catacomb.
  server/tripe-admin.5.in: Improve some clumsy wording.
  server/tripe-admin.5.in: Add cross-reference for ECODE and MESSAGE.
  server/tripe-admin.5.in: Add missing origin-command notes to errors.
  server/tripe-admin.5.in: Use gender-neutral pronouns.
  server/bulkcrypto.c: Fix description comment for AEAD schemes.
  py/tripe.py.in: Raise an error if a command token contains a newline.

2 years agosvc/connect.in: Use the service's version, not the TrIPE library version.
Mark Wooding [Thu, 28 Apr 2022 14:01:21 +0000 (15:01 +0100)]
svc/connect.in: Use the service's version, not the TrIPE library version.

2 years agosvc/connect.in: Fix latent type errors in info output.
Mark Wooding [Thu, 28 Apr 2022 13:59:32 +0000 (14:59 +0100)]
svc/connect.in: Fix latent type errors in info output.

Before we've had an answer to the first `ping', the mean and standard
deviation aren't properly defined.  We attempt to avoid this by setting
them to `-', but then try to format them using `%f', which isn't going
to work well.  Fix this and some related problems with the minimum and
maximum times.

2 years agoserver/bulkcrypto.c: Document the procedures for producing challenges. mdw/master.found-crybaby
Mark Wooding [Thu, 28 Apr 2022 01:42:02 +0000 (02:42 +0100)]
server/bulkcrypto.c: Document the procedures for producing challenges.

2 years agoserver/chal.c: Add a missing blank line.
Mark Wooding [Thu, 28 Apr 2022 00:21:54 +0000 (01:21 +0100)]
server/chal.c: Add a missing blank line.

2 years agoserver/tripe-admin.5.in: Place the blame correctly for a couple of errors.
Mark Wooding [Thu, 28 Apr 2022 00:21:30 +0000 (01:21 +0100)]
server/tripe-admin.5.in: Place the blame correctly for a couple of errors.

2 years agosvc/connect.8.in: Fix message formatting.
Mark Wooding [Thu, 28 Apr 2022 00:20:20 +0000 (01:20 +0100)]
svc/connect.8.in: Fix message formatting.

2 years agosvc/connect.in: Qualify reference to symbol in `tripe' module.
Mark Wooding [Thu, 28 Apr 2022 00:01:44 +0000 (01:01 +0100)]
svc/connect.in: Qualify reference to symbol in `tripe' module.

Oops.

2 years agoserver/admin.c: Remove spurious `ping' in usage message. master
Mark Wooding [Mon, 20 Sep 2021 23:04:20 +0000 (00:04 +0100)]
server/admin.c: Remove spurious `ping' in usage message.

Oops.

2 years agoserver/tests.at: Quote messages being echoed.
Mark Wooding [Mon, 20 Sep 2021 22:56:45 +0000 (23:56 +0100)]
server/tests.at: Quote messages being echoed.

For consistency, and to make the literals stand out in my editor.

2 years agoserver/tripe.8.in: Fix the reference for trace options.
Mark Wooding [Mon, 20 Sep 2021 23:34:41 +0000 (00:34 +0100)]
server/tripe.8.in: Fix the reference for trace options.

2 years agoserver/tripe-admin.5.in: Fix out-of-date information about `kill'.
Mark Wooding [Mon, 20 Sep 2021 23:17:31 +0000 (00:17 +0100)]
server/tripe-admin.5.in: Fix out-of-date information about `kill'.

It does sometimes send a `bye' message now.

2 years agosvc/connect.in: Make `failures' info value be a string.
Mark Wooding [Mon, 20 Sep 2021 14:06:39 +0000 (15:06 +0100)]
svc/connect.in: Make `failures' info value be a string.

Otherwise we choke when presenting it for the `info' command.

2 years agopeerdb/tripe-newpeers.in: Reflow an overlong comment.
Mark Wooding [Mon, 20 Sep 2021 14:06:24 +0000 (15:06 +0100)]
peerdb/tripe-newpeers.in: Reflow an overlong comment.

2 years agocommon/defs.man: Set the font for the running headers as well as the body.
Mark Wooding [Mon, 20 Sep 2021 14:05:48 +0000 (15:05 +0100)]
common/defs.man: Set the font for the running headers as well as the body.

This involves switching environments.

3 years agopeerdb/peers.in.5.in: Fix `.' which should have been `,' between list items.
Mark Wooding [Sun, 21 Jun 2020 15:25:33 +0000 (16:25 +0100)]
peerdb/peers.in.5.in: Fix `.' which should have been `,' between list items.

3 years agokeys/tripe-keys.conf.5.in: Delete entirely spurious `rijndael-cbc'.
Mark Wooding [Sun, 21 Jun 2020 15:10:45 +0000 (16:10 +0100)]
keys/tripe-keys.conf.5.in: Delete entirely spurious `rijndael-cbc'.

No idea where that came from.  Looks like copy-and-paste debris from
when that table was originally added.

3 years agokeys/tripe-keys.conf.5.in: Use `\-' (minus) for options, not `-' (hyphen).
Mark Wooding [Sun, 21 Jun 2020 15:10:21 +0000 (16:10 +0100)]
keys/tripe-keys.conf.5.in: Use `\-' (minus) for options, not `-' (hyphen).

3 years agokeys/tripe-keys.conf.5.in: Generally enclose literal strings in quotes.
Mark Wooding [Sun, 21 Jun 2020 15:10:02 +0000 (16:10 +0100)]
keys/tripe-keys.conf.5.in: Generally enclose literal strings in quotes.

3 years agokeys/tripe-keys.conf.5.in: Typeset parameter names in bold.
Mark Wooding [Sun, 21 Jun 2020 15:08:35 +0000 (16:08 +0100)]
keys/tripe-keys.conf.5.in: Typeset parameter names in bold.

Not sure why I decided to set these in italics when I started.  It
doesn't make a great deal of sense.

3 years agoserver/tripe-admin.5.in: Remove incorrect blame on Catacomb.
Mark Wooding [Sat, 13 Jun 2020 15:48:11 +0000 (16:48 +0100)]
server/tripe-admin.5.in: Remove incorrect blame on Catacomb.

The serialization formats are handled entirely within `tripe'.

3 years agoserver/tripe-admin.5.in: Improve some clumsy wording.
Mark Wooding [Sat, 13 Jun 2020 15:47:42 +0000 (16:47 +0100)]
server/tripe-admin.5.in: Improve some clumsy wording.

3 years agoserver/tripe-admin.5.in: Add cross-reference for ECODE and MESSAGE.
Mark Wooding [Sat, 13 Jun 2020 15:47:02 +0000 (16:47 +0100)]
server/tripe-admin.5.in: Add cross-reference for ECODE and MESSAGE.

3 years agoserver/tripe-admin.5.in: Add missing origin-command notes to errors.
Mark Wooding [Sat, 13 Jun 2020 15:45:44 +0000 (16:45 +0100)]
server/tripe-admin.5.in: Add missing origin-command notes to errors.

3 years agoserver/tripe-admin.5.in: Use gender-neutral pronouns.
Mark Wooding [Sat, 13 Jun 2020 15:46:21 +0000 (16:46 +0100)]
server/tripe-admin.5.in: Use gender-neutral pronouns.

3 years agoserver/bulkcrypto.c: Fix description comment for AEAD schemes.
Mark Wooding [Sat, 13 Jun 2020 15:48:50 +0000 (16:48 +0100)]
server/bulkcrypto.c: Fix description comment for AEAD schemes.

While it's true that actual encryption doesn't require AAD processing,
the challenge handling /does/.

3 years agopy/tripe.py.in: Raise an error if a command token contains a newline.
Mark Wooding [Sat, 13 Jun 2020 15:49:47 +0000 (16:49 +0100)]
py/tripe.py.in: Raise an error if a command token contains a newline.

This should never happen, and it confuses the client quite badly if it
does.

3 years agoMerge branch '1.0.0pre19.x'
Mark Wooding [Mon, 25 May 2020 15:33:19 +0000 (16:33 +0100)]
Merge branch '1.0.0pre19.x'

* 1.0.0pre19.x:
  svc/connect.in: Squash newlines to spaces in `info' output.
  server/admin.c: Fix `=' vs `==' error in assertion.
  svc/tripe-ifup.in: Don't set remote IPv6 address until interface is up.

3 years agosvc/connect.in: Squash newlines to spaces in `info' output. 1.0.0pre19.x
Mark Wooding [Mon, 25 May 2020 15:29:45 +0000 (16:29 +0100)]
svc/connect.in: Squash newlines to spaces in `info' output.

Otherwise all sorts of things go horribly wrong.

3 years agoserver/admin.c: Fix `=' vs `==' error in assertion.
Mark Wooding [Mon, 25 May 2020 15:26:08 +0000 (16:26 +0100)]
server/admin.c: Fix `=' vs `==' error in assertion.

Oops.  Well, at least this /shouldn't/ have done anything bad...  I
guess GCC has only just started warning about this because older
versions of <assert.h> provide the necessary parentheses already.

3 years agoserver/peer.c: Always send `BYE' packets on peer death.
Mark Wooding [Mon, 25 May 2020 15:21:14 +0000 (16:21 +0100)]
server/peer.c: Always send `BYE' packets on peer death.

The peer can ignore them if it likes.  The point really is that
different ends of a connection can have different ideas about its
ephemeral-ness without any logical problems.

3 years agoserver/admin.c: Don't automatically mark `knock' peers as ephemeral.
Mark Wooding [Mon, 25 May 2020 15:17:50 +0000 (16:17 +0100)]
server/admin.c: Don't automatically mark `knock' peers as ephemeral.

This was a bad idea on my part.

Consider a server which lives behind NAT, and therefore needs to use
`knock' or some similar dynamic arrangement so as to inform its peers of
its current public address.  This shouldn't be considered ephemeral.  In
particular, if its peer decides, because of flaky networking, say, that
it's no longer available, and sends a `BYE' packet, our server shouldn't
actually stop trying to re-establish the connection.

3 years agoserver/tests.at (knock): Tell `bob' explicitly that `alice' is `ephemeral'.
Mark Wooding [Mon, 25 May 2020 15:15:17 +0000 (16:15 +0100)]
server/tests.at (knock): Tell `bob' explicitly that `alice' is `ephemeral'.

This is currently redundant, but I'm planning to modify the way
ephemeral peers work.

3 years agot/Makefile.am: Run the tests in parallel.
Mark Wooding [Mon, 25 May 2020 15:14:42 +0000 (16:14 +0100)]
t/Makefile.am: Run the tests in parallel.

Now that the ephemeral ports stuff works, this seems like a good plan.

4 years agosvc/tripe-ifup.in: Don't set remote IPv6 address until interface is up.
Mark Wooding [Wed, 8 Apr 2020 12:07:56 +0000 (13:07 +0100)]
svc/tripe-ifup.in: Don't set remote IPv6 address until interface is up.

This is annoying.  Later Linux kernels complain about adding routes to
an interface which is still down.

4 years agodebian/: Bump to Debhelper 10.
Mark Wooding [Sun, 29 Sep 2019 14:55:38 +0000 (15:55 +0100)]
debian/: Bump to Debhelper 10.

4 years agoserver/bulkcrypto.c: Replace dynamic assertions with static ones.
Mark Wooding [Sun, 29 Sep 2019 14:39:49 +0000 (15:39 +0100)]
server/bulkcrypto.c: Replace dynamic assertions with static ones.

This adds a dependency on mLib 2.4.1.

4 years agoproxy/tripe-mitm.c, server/tests.at: Optionally use kernel-assigned ports.
Mark Wooding [Sun, 29 Sep 2019 13:17:54 +0000 (14:17 +0100)]
proxy/tripe-mitm.c, server/tests.at: Optionally use kernel-assigned ports.

Add an option for the `peer' ports in `tripe-mitm' to be allocated by
the kernel, and for the port numbers to be written to named files.  Use
this in the tests so as to avoid conflicts between concurrent instances
of the tests.  It's not perfect: some of the tests require that a new
`tripe-mitm' take over a port from an old one, and there's a chance that
some other process might have grabbed it in between -- but it's much
better than it was before.

I should also consider using `noip' for this testing.

4 years agoRelease 1.5.3. 1.5.3
Mark Wooding [Mon, 23 Sep 2019 10:10:25 +0000 (11:10 +0100)]
Release 1.5.3.

4 years agopeerdb/tripe-newpeers.in: Fix output crash when there are `user' records.
Mark Wooding [Mon, 23 Sep 2019 10:09:03 +0000 (11:09 +0100)]
peerdb/tripe-newpeers.in: Fix output crash when there are `user' records.

Oh, dear.  This really hasn't been a good release.

4 years agoRelease 1.5.2. 1.5.2
Mark Wooding [Sun, 22 Sep 2019 15:22:48 +0000 (16:22 +0100)]
Release 1.5.2.

Fix bungled `tripe-wireshark' package.

4 years agoRelease 1.5.1. 1.5.1
Mark Wooding [Sun, 22 Sep 2019 13:53:29 +0000 (14:53 +0100)]
Release 1.5.1.

4 years agoserver/tests.at: Add roundtrip test for the crypto transforms.
Mark Wooding [Sun, 22 Sep 2019 13:46:53 +0000 (14:46 +0100)]
server/tests.at: Add roundtrip test for the crypto transforms.

This should avoid future débâcles like the one we had this time.

4 years agoserver/test.c: Trace the key-exchange and bulk crypto details.
Mark Wooding [Sun, 22 Sep 2019 13:46:37 +0000 (14:46 +0100)]
server/test.c: Trace the key-exchange and bulk crypto details.

4 years agoserver/test.c: Don't try to trace if it's turned off.
Mark Wooding [Sun, 22 Sep 2019 13:46:20 +0000 (14:46 +0100)]
server/test.c: Don't try to trace if it's turned off.

4 years agoserver/test.c: Don't segfault if the keyring is bogus.
Mark Wooding [Sun, 22 Sep 2019 13:45:41 +0000 (14:45 +0100)]
server/test.c: Don't segfault if the keyring is bogus.

4 years agoserver/tripe-admin.5.in: Document the `unsuitable-aead-cipher' warnings.
Mark Wooding [Sun, 22 Sep 2019 13:44:13 +0000 (14:44 +0100)]
server/tripe-admin.5.in: Document the `unsuitable-aead-cipher' warnings.

This was missed out of the earlier AEAD work.

4 years agoserver/tripe-admin.5.in: Reorder the `KEYMGMT' warnings.
Mark Wooding [Sun, 22 Sep 2019 13:43:41 +0000 (14:43 +0100)]
server/tripe-admin.5.in: Reorder the `KEYMGMT' warnings.

Now they're alphabetical by complaint.

4 years agoserver/bulkcrypto.c: Support smaller nonce spaces, down to 40 bits.
Mark Wooding [Sun, 22 Sep 2019 13:40:13 +0000 (14:40 +0100)]
server/bulkcrypto.c: Support smaller nonce spaces, down to 40 bits.

Pack the type into the available space, however small it is.

4 years agoserver/bulkcrypto.c: Abstract out the AEAD nonce formatting.
Mark Wooding [Sun, 22 Sep 2019 13:39:34 +0000 (14:39 +0100)]
server/bulkcrypto.c: Abstract out the AEAD nonce formatting.

No functional change at this time.

4 years agoserver/bulkcrypto.c: Adjust the scale in the data-format diagrams.
Mark Wooding [Sun, 22 Sep 2019 13:36:21 +0000 (14:36 +0100)]
server/bulkcrypto.c: Adjust the scale in the data-format diagrams.

Two spaces per byte seems to work well.

4 years agoserver/bulkcrypto.c: Fix segfault in `aead' algorithm setup.
Mark Wooding [Sun, 22 Sep 2019 09:24:31 +0000 (10:24 +0100)]
server/bulkcrypto.c: Fix segfault in `aead' algorithm setup.

I think I must have completely failed to test this prior to release.
Brown paper bag time.  Add a new keyring to test this sort-of properly.

4 years agoRelease 1.5.0. 1.5.0
Mark Wooding [Sun, 22 Sep 2019 00:49:14 +0000 (01:49 +0100)]
Release 1.5.0.

4 years agoconfigure.ac, debian/control: Set Catacomb version now that it's released.
Mark Wooding [Sun, 22 Sep 2019 00:18:58 +0000 (01:18 +0100)]
configure.ac, debian/control: Set Catacomb version now that it's released.

4 years agoMerge remote-tracking branch 'origin/1.0.0pre19.x'
Mark Wooding [Sun, 22 Sep 2019 00:18:20 +0000 (01:18 +0100)]
Merge remote-tracking branch 'origin/1.0.0pre19.x'

* origin/1.0.0pre19.x:
  Release 1.0.0pre19.1.
  debian/: Use `dh_python2' for packaging.

4 years agodebian/control: Move the Wireshark gubbins back to general `Build-Depends'.
Mark Wooding [Sat, 21 Sep 2019 23:07:33 +0000 (00:07 +0100)]
debian/control: Move the Wireshark gubbins back to general `Build-Depends'.

The Wireshark plugin directory is architecture-specific, so, while the
Lua dissector is architecture-neutral, the install directory isn't and
we have to build a separate package for each architecture.

This is rather vexing.

4 years agoserver/bulkcrypto.c: Implement a bulk transform based on AEAD schemes.
Mark Wooding [Thu, 5 Sep 2019 12:06:10 +0000 (13:06 +0100)]
server/bulkcrypto.c: Implement a bulk transform based on AEAD schemes.

Reimplement `naclbox' mostly in terms of this.  It doesn't quite fit,
because `naclbox' can't be used directly for challenges.

4 years agoserver/tripe.8.in: Use Rogaway's spelling `blockcipher' consistently.
Mark Wooding [Thu, 5 Sep 2019 12:00:21 +0000 (13:00 +0100)]
server/tripe.8.in: Use Rogaway's spelling `blockcipher' consistently.

4 years agoserver/keymgmt.c: Trace the bulk-transform name.
Mark Wooding [Thu, 5 Sep 2019 12:02:53 +0000 (13:02 +0100)]
server/keymgmt.c: Trace the bulk-transform name.

Most of the bulk-crypto trace is pretty good, but somehow I failed to do
this before.

4 years agopathmtu/pathmtu.c: Disable `raw' strategy if we don't have getifaddrs(3).
Mark Wooding [Sun, 17 Jun 2018 18:00:22 +0000 (19:00 +0100)]
pathmtu/pathmtu.c: Disable `raw' strategy if we don't have getifaddrs(3).

This isn't available on Android earlier than N.

4 years agopathmtu/pathmtu.c: Place `addreq' with the rest of the `raw' strategy.
Mark Wooding [Sun, 17 Jun 2018 17:07:40 +0000 (18:07 +0100)]
pathmtu/pathmtu.c: Place `addreq' with the rest of the `raw' strategy.

It's not needed for the `linux' strategy.

4 years agopathmtu/pathmtu.c: Indent conditional `#define' properly.
Mark Wooding [Sun, 17 Jun 2018 17:06:57 +0000 (18:06 +0100)]
pathmtu/pathmtu.c: Indent conditional `#define' properly.

4 years agoserver/Makefile.am: Build most of the server into a library.
Mark Wooding [Mon, 11 Jun 2018 15:34:10 +0000 (16:34 +0100)]
server/Makefile.am: Build most of the server into a library.

As a bonus, we get to avoid having to mention most of the files twice to
build both the server proper and the test program.

4 years agoserver/: Make initialization errors be non-fatal and restartable.
Mark Wooding [Sat, 19 May 2018 21:03:28 +0000 (22:03 +0100)]
server/: Make initialization errors be non-fatal and restartable.

It should now be possible to trap any error during startup, change any
part of the configuration, shut down the affected components, and
restart everything again, without taking down the whole process.

Mostly, this involves replacing the existing calls to `exit' with
goto-cleanup and return codes.  In some cases, the existing
functionality has been reordered to make cleanup easier.  I didn't leave
this for a separate commit, because including those changes here makes
it clearer what they're for and should make it easier to check that
they're the right fixes.

The details are:

  * admin.c (a_listen): Move resetting the `umask' to the end.

    Specifically, after the last part of the function that can fail.
    This avoids a double reset if the final part, the call to listen(2),
    fails.

  * admin.c (a_init): Move creation of the service table to the end.

    This isn't (currently) a thing that can be cleaned up, so do it only
    after the parts that can fail -- specifically, initializing ADNS.

  * keymgmt.c (kh_init): Use `kh->kf' as a flag to prevent double init.

    As mentioned earlier, this is cleared by the static initializer, so
    we can safely assume that `kh->kf' is null if and only if the
    keyhalf requires initialization.

    Also, reorder slightly, to establish the cache hashtable only after
    the keyring file has been read.

  * keymgmt.c (km_init): Refresh before fetching the master key.

    Suppose we fail to initialize because the master key is missing.
    The right fix is to update the keyring file with the proper key, and
    then retry.  But at this point the private keyhalf has been
    initialized; so we must force a refresh of the keyring data.

  * keymgmt.c (km_init): Be idempotent regarding the master key.

    If there's a master key cached, then don't clobber it if we can't
    find it again.  On the other hand, if we find a different one this
    time then switch.

4 years agoserver/tripe.c, server/standalone.c: Split `main' out into a seprate file.
Mark Wooding [Sat, 19 May 2018 20:51:11 +0000 (21:51 +0100)]
server/tripe.c, server/standalone.c: Split `main' out into a seprate file.

We can also remove the temporary hacks from `test.c'.

4 years agoserver/: Build a proper interface for handling tunnel classes.
Mark Wooding [Sat, 19 May 2018 20:39:53 +0000 (21:39 +0100)]
server/: Build a proper interface for handling tunnel classes.

  * Introduce functions for enumerating the available tunnel types,
    looking one up by name, and returning a default tunnel.  This
    eliminates the direct access previously used by the admin code.

  * Introduce a registration interface.  This isn't necessary for our
    purposes, but, in case it's not been obvious enough, the recent
    changes have been directed towards making the server code suitable
    as a library, and a user of this library might well need a custom
    tunnel class.

5 years agoserver/keymgmt.c: Capture copies of the keyring and tag strings.
Mark Wooding [Sat, 16 Jun 2018 12:16:11 +0000 (13:16 +0100)]
server/keymgmt.c: Capture copies of the keyring and tag strings.

Slightly complicated because the private-key tag can legitimately be
null to indicate that the old-fashioned search by key-exchange group
thing should be done.

5 years agoserver/keymgmt.c, server/tripe.c: Add `km_clear' to shut down keyrings.
Mark Wooding [Sat, 19 May 2018 20:12:10 +0000 (21:12 +0100)]
server/keymgmt.c, server/tripe.c: Add `km_clear' to shut down keyrings.

A bit more fiddly than our previous efforts, but this completes the job
of unwinding all of the configurable components of the server.  The main
thing that doesn't get reset is the set of extant admin connections, and
I intend to leave those running until we have no choice.

5 years agoserver/peer.c, server/tripe.c: Add `p_unbind' to tear down the UDP sockets.
Mark Wooding [Sat, 19 May 2018 19:49:27 +0000 (20:49 +0100)]
server/peer.c, server/tripe.c: Add `p_unbind' to tear down the UDP sockets.

5 years agoserver/keymgmt.c (kh_init): Remove pointless setting of `kh->kf'.
Mark Wooding [Sat, 19 May 2018 19:36:10 +0000 (20:36 +0100)]
server/keymgmt.c (kh_init): Remove pointless setting of `kh->kf'.

It's statically initialized to be null.  The only reason it might not be
is because we already changed it.  Hmm... that gives me an idea.

5 years agoserver/tripe.c, server/admin.c: Add a way to end the main loop.
Mark Wooding [Sat, 19 May 2018 19:05:49 +0000 (20:05 +0100)]
server/tripe.c, server/admin.c: Add a way to end the main loop.

This replaces the rather abrupt `a_quit' function.  Cleanup is handled
in `main', using the facilities added recently.

For now, there's a stub version of `lp_end' in `test.c', joining
`iv_...' from earlier.

5 years agoserver/peer.c, server/admin.c: Introduce `p_destroyall'.
Mark Wooding [Sat, 19 May 2018 19:15:58 +0000 (20:15 +0100)]
server/peer.c, server/admin.c: Introduce `p_destroyall'.

Rather than having the loop in `a_quit' directly.

5 years agoserver/admin.c: Introduce `a_unlisten' to shut down the admin socket.
Mark Wooding [Sat, 19 May 2018 19:13:11 +0000 (20:13 +0100)]
server/admin.c: Introduce `a_unlisten' to shut down the admin socket.

5 years agoserver/peer.c: Engage the interval timer only while there are peers.
Mark Wooding [Sat, 19 May 2018 20:54:22 +0000 (21:54 +0100)]
server/peer.c: Engage the interval timer only while there are peers.

The reason we need the interval timer is (a) to top up the entropy pool,
and (b) to clean up old keysets.  If there are no peers, we do no key
exchange, so we don't need any random bits, and there are no keysets to
clear away.  So we can save wakeups in this case.

For now, there are stub versions of `iv_(add|rm)reason' in `test.c', but
these will vanish once we split `main' out into its own file.  (Spoiler
alert!)

5 years agoserver/tripe.c: Formalize the main loop machinery.
Mark Wooding [Sat, 19 May 2018 19:01:36 +0000 (20:01 +0100)]
server/tripe.c: Formalize the main loop machinery.

The new `lp_init' function has taken on a number of miscellaneous
initialization tasks.  But nothing has really changed much.

5 years agoserver/tripe.c: Formalize the interval-timer arrangements.
Mark Wooding [Sat, 19 May 2018 17:28:15 +0000 (18:28 +0100)]
server/tripe.c: Formalize the interval-timer arrangements.

The hard stuff is now (mostly) detached from the main initialization
code.  There's also some machinery, currently unused, for disabling the
interval timer while it's not doing any good, which might be useful in
energy-constrained devices.

5 years agoserver/keymgmt.c: Replace ad-hoc `die' with an `ABORT'-level warning.
Mark Wooding [Sat, 19 May 2018 17:13:46 +0000 (18:13 +0100)]
server/keymgmt.c: Replace ad-hoc `die' with an `ABORT'-level warning.

It's either this or an `assert', and for some reason I didn't do that
the first time.

5 years agoserver/peer.c (p_bind): Add the selector after fetching the port number.
Mark Wooding [Sun, 13 May 2018 16:21:04 +0000 (17:21 +0100)]
server/peer.c (p_bind): Add the selector after fetching the port number.

This will make more sense later.

5 years agoserver/: Issue `WARN' messages for (most) initialization errors.
Mark Wooding [Sun, 13 May 2018 14:26:59 +0000 (15:26 +0100)]
server/: Issue `WARN' messages for (most) initialization errors.

Now, most initialization problems which aren't to do with duff arguments
are diagnosed as `WARN' messages rather than human-readable messages on
stderr.

Document the new warnings.

5 years agoserver/tripe.c: Establish the stdio admin client early.
Mark Wooding [Sun, 13 May 2018 14:31:15 +0000 (15:31 +0100)]
server/tripe.c: Establish the stdio admin client early.

This way, warnings from peer initialization, for example, are reported
to the stdio admin client rather than stderr.

5 years agoserver/admin.c (a_format): Introduce `?ERR' for explicitly named errors.
Mark Wooding [Sun, 13 May 2018 11:30:06 +0000 (12:30 +0100)]
server/admin.c (a_format): Introduce `?ERR' for explicitly named errors.

Judging by the documentation, `?ERRNO' is supposed to take an `int'
argument and format the error it specifies; but it actually works by
examining `errno', and all the callers know this.  Changing it now seems
pointless, but I do want to be able to report errors in cases where
`errno' is or might be stale, and stuffing an error code back into
`errno' just so that it can be reported seems rather ugly.

Instead, add `?ERR' which /does/ accept an `int' argument, and fix the
documentation so that it describes reality.

5 years agoserver/admin.c (a_listen): If stat(2) says the socket has gone, then retry.
Mark Wooding [Sun, 13 May 2018 00:56:44 +0000 (01:56 +0100)]
server/admin.c (a_listen): If stat(2) says the socket has gone, then retry.

This saves us from making an embarrassing `stat failed because ENOENT'
report.

5 years agoserver/keymgmt.c: Remove redundant error reports.
Mark Wooding [Sat, 12 May 2018 20:58:12 +0000 (21:58 +0100)]
server/keymgmt.c: Remove redundant error reports.

Any I/O errors encountered while reading the key file will have been
reported already.  In the same terms.

Previously, the final `die' message wasn't actually redundant, because
the earlier warnings got swallowed if we were going to run as a daemon.
It's obviously wrong that this happened, and the underlying cause has
been fixed properly now, so this isn't a concern any more.

5 years agoserver/tripe.c: Be more clever about when to switch error output.
Mark Wooding [Sat, 12 May 2018 23:36:36 +0000 (00:36 +0100)]
server/tripe.c: Be more clever about when to switch error output.

If we're going to detach and be a daemon, then we don't adopt
stdin/stdout as an initial administration client -- because we're going
to close these streams and disassociate from the surrounding
environment.  On the other hand, if we /are/ going to adopt
stdin/stdout, it makes sense to report errors to the client listening
there as soon as we can, so that they can keep informed of our
progress.  So take advantage of the new flexibility afforded by the
`a_switcherr' function, and switch the error output early or late
depending on whether we're going to daemonize.

5 years agoserver/keymgmt.c (kh_reopen): Promote the happy path.
Mark Wooding [Sat, 12 May 2018 21:20:36 +0000 (22:20 +0100)]
server/keymgmt.c (kh_reopen): Promote the happy path.

5 years agoserver/: Split peer and admin initialization into smaller pieces.
Mark Wooding [Sat, 12 May 2018 19:12:22 +0000 (20:12 +0100)]
server/: Split peer and admin initialization into smaller pieces.

Split admin initialization into:

  * setting up internal data structures, which is `a_init' as before;

  * establishing the daemonic signal handlers, which is now `a_signals';

  * creating the admin socket and listening for incoming connections,
    which is now `a_listen'; and

  * switching alert reporting over to admin clients rather than stderr,
    which is now `a_switcherr'.

Given that `a_listen' now need not actually be called, only unlink the
socket object if we actually created it.

Split peer initialization into:

  * setting up the data structures, which is `p_init' as before; and

  * creating the UDP sockets for communicating with other peers, which
    is now `p_bind'.

This will make sense in context.

squash! server/: Split peer and admin initialization into smaller pieces.

server/admin.c: Split out signal-handler establishment.

5 years agoserver/admin.c: Note that it's safe to call `a_create' before `a_init'.
Mark Wooding [Sat, 12 May 2018 18:55:26 +0000 (19:55 +0100)]
server/admin.c: Note that it's safe to call `a_create' before `a_init'.

A good thing too, because we do exactly this.

5 years agoserver/tripe.c: Don't start privsep helper unless some tunnel needs it.
Mark Wooding [Sat, 12 May 2018 09:58:53 +0000 (10:58 +0100)]
server/tripe.c: Don't start privsep helper unless some tunnel needs it.

It just saves having a useless process hanging off the side.

5 years agoserver/: Record port number with UDP socket, rather than querying.
Mark Wooding [Sat, 12 May 2018 09:44:14 +0000 (10:44 +0100)]
server/: Record port number with UDP socket, rather than querying.

The old `p_port' function queried getsockname(2) at runtime, and bailed
in the (very unlikely) event that it failed.  Avoid this situation by
recording the port number when the socket is set up and just using the
recorded value when it's needed.

This was the only remaining abnormal exit after the server has started.

5 years agoserver/: Wrap `udpsock' selectors up in a custom structure.
Mark Wooding [Sat, 12 May 2018 09:36:27 +0000 (10:36 +0100)]
server/: Wrap `udpsock' selectors up in a custom structure.

Now we can easily add more per-socket information.  No functional change
at this stage.