2021-01-14 15:24:10 +03:00
|
|
|
# This module enables unprivileged users to read onion addresses.
|
|
|
|
# By default, onion addresses in /var/lib/tor/onion are only readable by the
|
|
|
|
# tor user.
|
|
|
|
# The included service copies onion addresses to /var/lib/onion-addresses/<user>/
|
|
|
|
# and sets permissions according to option 'access'.
|
2019-03-29 17:46:13 +03:00
|
|
|
|
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
let
|
2021-01-14 15:24:10 +03:00
|
|
|
cfg = config.nix-bitcoin.onionAddresses;
|
2019-11-27 16:04:23 +03:00
|
|
|
inherit (config) nix-bitcoin-services;
|
2021-01-14 15:24:10 +03:00
|
|
|
dataDir = "/var/lib/onion-addresses/";
|
|
|
|
onion-addresses-script = pkgs.writeScript "onion-addresses.sh" ''
|
2019-03-29 17:46:13 +03:00
|
|
|
# wait until tor is up
|
|
|
|
until ls -l /var/lib/tor/state; do sleep 1; done
|
|
|
|
|
|
|
|
cd ${dataDir}
|
|
|
|
|
|
|
|
# Create directory for every user and set permissions
|
|
|
|
${ builtins.foldl'
|
|
|
|
(x: user: x +
|
|
|
|
''
|
|
|
|
mkdir -p -m 0700 ${user}
|
|
|
|
chown ${user} ${user}
|
|
|
|
# Copy onion hostnames into the user's directory
|
|
|
|
${ builtins.foldl'
|
|
|
|
(x: onion: x +
|
|
|
|
''
|
|
|
|
ONION_FILE=/var/lib/tor/onion/${onion}/hostname
|
|
|
|
if [ -e "$ONION_FILE" ]; then
|
|
|
|
cp $ONION_FILE ${user}/${onion}
|
|
|
|
chown ${user} ${user}/${onion}
|
|
|
|
fi
|
|
|
|
'')
|
|
|
|
""
|
|
|
|
(builtins.getAttr user cfg.access)
|
|
|
|
}
|
|
|
|
'')
|
|
|
|
""
|
|
|
|
(builtins.attrNames cfg.access)
|
|
|
|
}
|
|
|
|
'';
|
|
|
|
in {
|
2021-01-14 15:24:10 +03:00
|
|
|
options.nix-bitcoin.onionAddresses = {
|
2019-03-29 17:46:13 +03:00
|
|
|
enable = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
|
|
|
description = ''
|
2021-01-14 15:24:10 +03:00
|
|
|
If enabled, the onion-addresses service will be installed.
|
2019-03-29 17:46:13 +03:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
access = mkOption {
|
|
|
|
type = types.attrs;
|
|
|
|
default = {};
|
|
|
|
description = ''
|
|
|
|
This option controls who is allowed to access onion hostnames. For
|
|
|
|
example the following allows the user operator to access the bitcoind
|
|
|
|
and clightning onion.
|
|
|
|
{
|
|
|
|
"operator" = [ "bitcoind" "clightning" ];
|
|
|
|
};
|
|
|
|
The onion hostnames can then be read from
|
2021-01-14 15:24:10 +03:00
|
|
|
/var/lib/onion-addresses/<user>.
|
2019-03-29 17:46:13 +03:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
config = mkIf cfg.enable {
|
2021-01-14 15:24:10 +03:00
|
|
|
systemd.services.onion-addresses = {
|
|
|
|
description = "Run onion-addresses";
|
2019-11-27 16:04:37 +03:00
|
|
|
wantedBy = [ "tor.service" ];
|
|
|
|
bindsTo = [ "tor.service" ];
|
2019-03-29 17:46:13 +03:00
|
|
|
after = [ "tor.service" ];
|
2020-05-05 16:18:41 +03:00
|
|
|
serviceConfig = nix-bitcoin-services.defaultHardening // {
|
2021-01-14 15:24:10 +03:00
|
|
|
ExecStart = "${pkgs.bash}/bin/bash ${onion-addresses-script}";
|
2019-03-29 17:46:13 +03:00
|
|
|
Type = "oneshot";
|
2019-11-27 16:04:37 +03:00
|
|
|
RemainAfterExit = true;
|
2021-01-14 15:24:11 +03:00
|
|
|
StateDirectory = "onion-addresses";
|
2020-05-05 16:25:00 +03:00
|
|
|
PrivateNetwork = "true"; # This service needs no network access
|
2020-05-06 11:28:00 +03:00
|
|
|
PrivateUsers = "false";
|
2020-05-05 16:27:07 +03:00
|
|
|
CapabilityBoundingSet = "CAP_CHOWN CAP_FSETID CAP_SETFCAP CAP_DAC_OVERRIDE CAP_DAC_READ_SEARCH CAP_FOWNER CAP_IPC_OWNER";
|
2020-05-05 16:18:41 +03:00
|
|
|
};
|
2019-03-29 17:46:13 +03:00
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|