summaryrefslogtreecommitdiff
path: root/modules/pppd.nix
blob: 27b417068525de51d6bb06644a0b988f10193e8d (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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
{ config, lib, pkgs, ... }:

with lib;

let
  cfg = config.services.pppd;
  shTypes = [ "ip-up" "ip-down" "ipv6-up" "ipv6-down" ];
in
{
  meta = {
    maintainers = with maintainers; [ danderson ];
  };

  options.services.pppd = {
    enable = mkEnableOption (lib.mdDoc "pppd");

    package = mkOption {
      default = pkgs.ppp;
      defaultText = literalExpression "pkgs.ppp";
      type = types.package;
      description = lib.mdDoc "pppd package to use.";
    };

    config = mkOption {
      type = types.lines;
      default = "";
      description = lib.mdDoc "default config for pppd";
    };

    secret = {
      chap = mkOption {
        type = types.nullOr types.path;
        default = null;
        description = lib.mdDoc "path to chap secret for pppd";
      };
      pap = mkOption {
        type = types.nullOr types.path;
        default = null;
        description = lib.mdDoc "path to pap secret for pppd";
      };
      srp = mkOption {
        type = types.nullOr types.path;
        default = null;
        description = lib.mdDoc "path to srp secret for pppd";
      };
    };

    script = mkOption {
      default = {};
      description = lib.mdoc ''
        script which is executed when the link is available for sending and
        receiving IP packets or when the link is no longer available for sending
        and receiving IP packets, see pppd(8) for more details
      '';
      type = types.attrsOf (types.submodule (
        { name, ... }:
        {
          options = {
            name = mkOption {
              type = types.str;
              default = name;
              example = "01-ddns.sh";
              description = lib.mdDoc "Name of the script.";
            };
            type = mkOption {
              default = "ip-up";
              type = types.enum shTypes;
              description = lib.mdDoc "Type of the script.";
            };
            text = mkOption {
              type = types.lines;
              default = "";
              description = lib.mdDoc "Shell commands to be executed.";
            };
            runtimeInputs = mkOption {
              type = types.listOf types.package;
              default = [];
              description = lib.mdDoc "dependencies of the shell script";
            };
          };
        }
      ));
    };

    peers = mkOption {
      default = {};
      description = lib.mdDoc "pppd peers.";
      type = types.attrsOf (types.submodule (
        { name, ... }:
        {
          options = {
            name = mkOption {
              type = types.str;
              default = name;
              example = "dialup";
              description = lib.mdDoc "Name of the PPP peer.";
            };

            enable = mkOption {
              type = types.bool;
              default = true;
              example = false;
              description = lib.mdDoc "Whether to enable this PPP peer.";
            };

            autostart = mkOption {
              type = types.bool;
              default = true;
              example = false;
              description = lib.mdDoc "Whether the PPP session is automatically started at boot time.";
            };

            config = mkOption {
              type = types.lines;
              default = "";
              description = lib.mdDoc "pppd configuration for this peer, see the pppd(8) man page.";
            };

            configFile = mkOption {
              type = types.nullOr types.path;
              default = null;
              example = literalExpression "/run/secrets/ppp/peer/options";
              description = lib.mdDoc "pppd configuration file for this peer, see the pppd(8) man page.";
            };
          };
        }
      ));
    };
  };

  config = let
    enabledConfigs = filter (f: f.enable) (attrValues cfg.peers);

    defaultCfg = if (cfg.config != "") then {
      "ppp/options".text = cfg.config;
    } else {};

    mkPeers = peerCfg: with peerCfg; let
      key = if (configFile == null) then "text" else "source";
      val = if (configFile == null) then peerCfg.config else configFile;
    in
    {
      name = "ppp/peers/${name}";
      value.${key} = val;
    };

    enabledSh = filter (s: s.text != "") (attrValues cfg.script);
    mkMsh = name : {
      name = "ppp/${name}";
      value.mode = "0755";
      value.text = ''
        #!/bin/sh

        # see the pppd(8) man page
        for s in /etc/ppp/${name}.d/*.sh; do
            [ -x "$s" ] && "$s" "$@" &
        done
      '';
    };
    mkUsh = shCfg : {
      name = "ppp/${shCfg.type}.d/${shCfg.name}.sh";
      value.mode = "0755";
      value.text = ''
        #!/bin/sh
        export PATH="${makeBinPath shCfg.runtimeInputs}:$PATH"

        ${shCfg.text}
      '';
    };

    enabledSec = let
      l = attrNames cfg.secret;
      f = (s: cfg.secret.${s} != null);
    in filter f l;
    mkSec = sec : {
      name = "ppp/${sec}-secrets";
      value.source = cfg.secret.${sec};
    };

    mkSystemd = peerCfg: {
      name = "pppd-${peerCfg.name}";
      value = {
        restartTriggers = [ config.environment.etc."ppp/peers/${peerCfg.name}".source ];
        before = [ "network.target" ];
        wants = [ "network.target" ];
        after = [ "network-pre.target" ];
        environment = {
          # pppd likes to write directly into /var/run. This is rude
          # on a modern system, so we use libredirect to transparently
          # move those files into /run/pppd.
          LD_PRELOAD = "${pkgs.libredirect}/lib/libredirect.so";
          NIX_REDIRECTS = "/var/run=/run/pppd";
        };
        serviceConfig = let
          capabilities = [
            "CAP_BPF"
            "CAP_SYS_TTY_CONFIG"
            "CAP_NET_ADMIN"
            "CAP_NET_RAW"
          ];
        in
        {
          ExecStart = "${getBin cfg.package}/sbin/pppd call ${peerCfg.name} nodetach nolog";
          Restart = "always";
          RestartSec = 5;

          AmbientCapabilities = capabilities;
          CapabilityBoundingSet = capabilities;
          KeyringMode = "private";
          LockPersonality = true;
          MemoryDenyWriteExecute = true;
          NoNewPrivileges = true;
          PrivateMounts = true;
          PrivateTmp = true;
          ProtectControlGroups = true;
          ProtectHome = true;
          ProtectHostname = true;
          ProtectKernelModules = true;
          # pppd can be configured to tweak kernel settings.
          ProtectKernelTunables = false;
          ProtectSystem = "strict";
          RemoveIPC = true;
          RestrictAddressFamilies = [
            "AF_ATMPVC"
            "AF_ATMSVC"
            "AF_INET"
            "AF_INET6"
            "AF_IPX"
            "AF_NETLINK"
            "AF_PACKET"
            "AF_PPPOX"
            "AF_UNIX"
          ];
          RestrictNamespaces = true;
          RestrictRealtime = true;
          RestrictSUIDSGID = true;
          SecureBits = "no-setuid-fixup-locked noroot-locked";
          SystemCallFilter = "@system-service";
          SystemCallArchitectures = "native";

          # All pppd instances on a system must share a runtime
          # directory in order for PPP multilink to work correctly. So
          # we give all instances the same /run/pppd directory to store
          # things in.
          #
          # For the same reason, we can't set PrivateUsers=true, because
          # all instances need to run as the same user to access the
          # multilink database.
          RuntimeDirectory = "pppd";
          RuntimeDirectoryPreserve = true;
        };
        wantedBy = mkIf peerCfg.autostart [ "multi-user.target" ];
      };
    };

    etcFiles = listToAttrs (map mkPeers enabledConfigs) //
               listToAttrs (map mkMsh shTypes) //
               listToAttrs (map mkUsh enabledSh) //
               listToAttrs (map mkSec enabledSec) //
               defaultCfg;

    systemdConfigs = listToAttrs (map mkSystemd enabledConfigs);

  in mkIf cfg.enable {
    assertions = map (peerCfg: {
      assertion = (peerCfg.configFile == null || peerCfg.config == "");
      message = ''
        Please specify either
        'services.pppd.${peerCfg.name}.config' or
        'services.pppd.${peerCfg.name}.configFile'.
      '';
    }) enabledConfigs;

    environment.etc = etcFiles;
    systemd.services = systemdConfigs;
  };
}