summary refs log tree commit diff
path: root/nixos/modules/services/networking/ghostunnel.nix
blob: 58a51df6cca2a48f3e7dc73215e0a01ee7a6c07c (plain) (blame)
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
{ config, lib, pkgs, ... }:
let
  inherit (lib)
    attrValues
    concatMap
    concatStringsSep
    escapeShellArg
    literalExample
    mapAttrs'
    mkDefault
    mkEnableOption
    mkIf
    mkOption
    nameValuePair
    optional
    types
    ;

  mainCfg = config.services.ghostunnel;

  module = { config, name, ... }:
    {
      options = {

        listen = mkOption {
          description = ''
            Address and port to listen on (can be HOST:PORT, unix:PATH).
          '';
          type = types.str;
        };

        target = mkOption {
          description = ''
            Address to forward connections to (can be HOST:PORT or unix:PATH).
          '';
          type = types.str;
        };

        keystore = mkOption {
          description = ''
            Path to keystore (combined PEM with cert/key, or PKCS12 keystore).

            NB: storepass is not supported because it would expose credentials via <code>/proc/*/cmdline</code>.

            Specify this or <code>cert</code> and <code>key</code>.
          '';
          type = types.nullOr types.str;
          default = null;
        };

        cert = mkOption {
          description = ''
            Path to certificate (PEM with certificate chain).

            Not required if <code>keystore</code> is set.
          '';
          type = types.nullOr types.str;
          default = null;
        };

        key = mkOption {
          description = ''
            Path to certificate private key (PEM with private key).

            Not required if <code>keystore</code> is set.
          '';
          type = types.nullOr types.str;
          default = null;
        };

        cacert = mkOption {
          description = ''
            Path to CA bundle file (PEM/X509). Uses system trust store if <code>null</code>.
          '';
          type = types.nullOr types.str;
        };

        disableAuthentication = mkOption {
          description = ''
            Disable client authentication, no client certificate will be required.
          '';
          type = types.bool;
          default = false;
        };

        allowAll = mkOption {
          description = ''
            If true, allow all clients, do not check client cert subject.
          '';
          type = types.bool;
          default = false;
        };

        allowCN = mkOption {
          description = ''
            Allow client if common name appears in the list.
          '';
          type = types.listOf types.str;
          default = [];
        };

        allowOU = mkOption {
          description = ''
            Allow client if organizational unit name appears in the list.
          '';
          type = types.listOf types.str;
          default = [];
        };

        allowDNS = mkOption {
          description = ''
            Allow client if DNS subject alternative name appears in the list.
          '';
          type = types.listOf types.str;
          default = [];
        };

        allowURI = mkOption {
          description = ''
            Allow client if URI subject alternative name appears in the list.
          '';
          type = types.listOf types.str;
          default = [];
        };

        extraArguments = mkOption {
          description = "Extra arguments to pass to <code>ghostunnel server</code>";
          type = types.separatedString " ";
          default = "";
        };

        unsafeTarget = mkOption {
          description = ''
            If set, does not limit target to localhost, 127.0.0.1, [::1], or UNIX sockets.

            This is meant to protect against accidental unencrypted traffic on
            untrusted networks.
          '';
          type = types.bool;
          default = false;
        };

        # Definitions to apply at the root of the NixOS configuration.
        atRoot = mkOption {
          internal = true;
        };
      };

      # Clients should not be authenticated with the public root certificates
      # (afaict, it doesn't make sense), so we only provide that default when
      # client cert auth is disabled.
      config.cacert = mkIf config.disableAuthentication (mkDefault null);

      config.atRoot = {
        assertions = [
          { message = ''
              services.ghostunnel.servers.${name}: At least one access control flag is required.
              Set at least one of:
                - services.ghostunnel.servers.${name}.disableAuthentication
                - services.ghostunnel.servers.${name}.allowAll
                - services.ghostunnel.servers.${name}.allowCN
                - services.ghostunnel.servers.${name}.allowOU
                - services.ghostunnel.servers.${name}.allowDNS
                - services.ghostunnel.servers.${name}.allowURI
            '';
            assertion = config.disableAuthentication
              || config.allowAll
              || config.allowCN != []
              || config.allowOU != []
              || config.allowDNS != []
              || config.allowURI != []
              ;
          }
        ];

        systemd.services."ghostunnel-server-${name}" = {
          after = [ "network.target" ];
          wants = [ "network.target" ];
          wantedBy = [ "multi-user.target" ];
          serviceConfig = {
            Restart = "always";
            AmbientCapabilities = ["CAP_NET_BIND_SERVICE"];
            DynamicUser = true;
            LoadCredential = optional (config.keystore != null) "keystore:${config.keystore}"
              ++ optional (config.cert != null) "cert:${config.cert}"
              ++ optional (config.key != null) "key:${config.key}"
              ++ optional (config.cacert != null) "cacert:${config.cacert}";
           };
          script = concatStringsSep " " (
            [ "${mainCfg.package}/bin/ghostunnel" ]
            ++ optional (config.keystore != null) "--keystore=$CREDENTIALS_DIRECTORY/keystore"
            ++ optional (config.cert != null) "--cert=$CREDENTIALS_DIRECTORY/cert"
            ++ optional (config.key != null) "--key=$CREDENTIALS_DIRECTORY/key"
            ++ optional (config.cacert != null) "--cacert=$CREDENTIALS_DIRECTORY/cacert"
            ++ [
              "server"
              "--listen ${config.listen}"
              "--target ${config.target}"
            ] ++ optional config.allowAll "--allow-all"
              ++ map (v: "--allow-cn=${escapeShellArg v}") config.allowCN
              ++ map (v: "--allow-ou=${escapeShellArg v}") config.allowOU
              ++ map (v: "--allow-dns=${escapeShellArg v}") config.allowDNS
              ++ map (v: "--allow-uri=${escapeShellArg v}") config.allowURI
              ++ optional config.disableAuthentication "--disable-authentication"
              ++ optional config.unsafeTarget "--unsafe-target"
              ++ [ config.extraArguments ]
          );
        };
      };
    };

in
{

  options = {
    services.ghostunnel.enable = mkEnableOption "ghostunnel";

    services.ghostunnel.package = mkOption {
      description = "The ghostunnel package to use.";
      type = types.package;
      default = pkgs.ghostunnel;
      defaultText = literalExample ''pkgs.ghostunnel'';
    };

    services.ghostunnel.servers = mkOption {
      description = ''
        Server mode ghostunnels (TLS listener -> plain TCP/UNIX target)
      '';
      type = types.attrsOf (types.submodule module);
      default = {};
    };
  };

  config = mkIf mainCfg.enable {
    assertions = lib.mkMerge (map (v: v.atRoot.assertions) (attrValues mainCfg.servers));
    systemd = lib.mkMerge (map (v: v.atRoot.systemd) (attrValues mainCfg.servers));
  };

  meta.maintainers = with lib.maintainers; [
    roberth
  ];
}