1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
|
{ config, lib, pkgs, ... }:
with lib;
let
cfg = config.services.spiped;
in
{
options = {
services.spiped = {
enable = mkOption {
type = types.bool;
default = false;
description = "Enable the spiped service module.";
};
config = mkOption {
type = types.attrsOf (types.submodule (
{
options = {
encrypt = mkOption {
type = types.bool;
default = false;
description = ''
Take unencrypted connections from the
`source` socket and send encrypted
connections to the `target` socket.
'';
};
decrypt = mkOption {
type = types.bool;
default = false;
description = ''
Take encrypted connections from the
`source` socket and send unencrypted
connections to the `target` socket.
'';
};
source = mkOption {
type = types.str;
description = ''
Address on which spiped should listen for incoming
connections. Must be in one of the following formats:
`/absolute/path/to/unix/socket`,
`host.name:port`,
`[ip.v4.ad.dr]:port` or
`[ipv6::addr]:port` - note that
hostnames are resolved when spiped is launched and are
not re-resolved later; thus if DNS entries change
spiped will continue to connect to the expired
address.
'';
};
target = mkOption {
type = types.str;
description = "Address to which spiped should connect.";
};
keyfile = mkOption {
type = types.path;
description = ''
Name of a file containing the spiped key.
As the daemon runs as the `spiped` user,
the key file must be readable by that user.
To securely manage the file within your configuration
consider a tool such as agenix or sops-nix.
'';
};
timeout = mkOption {
type = types.int;
default = 5;
description = ''
Timeout, in seconds, after which an attempt to connect to
the target or a protocol handshake will be aborted (and the
connection dropped) if not completed
'';
};
maxConns = mkOption {
type = types.int;
default = 100;
description = ''
Limit on the number of simultaneous connections allowed.
'';
};
waitForDNS = mkOption {
type = types.bool;
default = false;
description = ''
Wait for DNS. Normally when `spiped` is
launched it resolves addresses and binds to its source
socket before the parent process returns; with this option
it will daemonize first and retry failed DNS lookups until
they succeed. This allows `spiped` to
launch even if DNS isn't set up yet, but at the expense of
losing the guarantee that once `spiped` has
finished launching it will be ready to create pipes.
'';
};
disableKeepalives = mkOption {
type = types.bool;
default = false;
description = "Disable transport layer keep-alives.";
};
weakHandshake = mkOption {
type = types.bool;
default = false;
description = ''
Use fast/weak handshaking: This reduces the CPU time spent
in the initial connection setup, at the expense of losing
perfect forward secrecy.
'';
};
resolveRefresh = mkOption {
type = types.int;
default = 60;
description = ''
Resolution refresh time for the target socket, in seconds.
'';
};
disableReresolution = mkOption {
type = types.bool;
default = false;
description = "Disable target address re-resolution.";
};
};
}
));
default = {};
example = literalExpression ''
{
pipe1 =
{ keyfile = "/var/lib/spiped/pipe1.key";
encrypt = true;
source = "localhost:6000";
target = "endpoint.example.com:7000";
};
pipe2 =
{ keyfile = "/var/lib/spiped/pipe2.key";
decrypt = true;
source = "0.0.0.0:7000";
target = "localhost:3000";
};
}
'';
description = ''
Configuration for a secure pipe daemon. The daemon can be
started, stopped, or examined using
`systemctl`, under the name
`spiped@foo`.
'';
};
};
};
config = mkIf cfg.enable {
assertions = mapAttrsToList (name: c: {
assertion = (c.encrypt -> !c.decrypt) || (c.decrypt -> c.encrypt);
message = "A pipe must either encrypt or decrypt";
}) cfg.config;
users.groups.spiped.gid = config.ids.gids.spiped;
users.users.spiped = {
description = "Secure Pipe Service user";
group = "spiped";
uid = config.ids.uids.spiped;
};
systemd.services."spiped@" = {
description = "Secure pipe '%i'";
after = [ "network.target" ];
serviceConfig = {
Restart = "always";
User = "spiped";
};
scriptArgs = "%i";
script = "exec ${pkgs.spiped}/bin/spiped -F `cat /etc/spiped/$1.spec`";
};
# Setup spiped config files
environment.etc = mapAttrs' (name: cfg: nameValuePair "spiped/${name}.spec"
{ text = concatStringsSep " "
[ (if cfg.encrypt then "-e" else "-d") # Mode
"-s ${cfg.source}" # Source
"-t ${cfg.target}" # Target
"-k ${cfg.keyfile}" # Keyfile
"-n ${toString cfg.maxConns}" # Max number of conns
"-o ${toString cfg.timeout}" # Timeout
(optionalString cfg.waitForDNS "-D") # Wait for DNS
(optionalString cfg.weakHandshake "-f") # No PFS
(optionalString cfg.disableKeepalives "-j") # Keepalives
(if cfg.disableReresolution then "-R"
else "-r ${toString cfg.resolveRefresh}")
];
}) cfg.config;
};
}
|