2022-04-15 01:41:22 +00:00
|
|
|
{ config, lib, options, pkgs, ... }:
|
|
|
|
with lib;
|
|
|
|
let
|
|
|
|
cfg = config.networking.openconnect;
|
|
|
|
openconnect = cfg.package;
|
|
|
|
pkcs11 = types.strMatching "pkcs11:.+" // {
|
|
|
|
name = "pkcs11";
|
|
|
|
description = "PKCS#11 URI";
|
|
|
|
};
|
|
|
|
interfaceOptions = {
|
|
|
|
options = {
|
2022-06-16 17:23:12 +00:00
|
|
|
autoStart = mkOption {
|
|
|
|
default = true;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "Whether this VPN connection should be started automatically.";
|
2022-06-16 17:23:12 +00:00
|
|
|
type = types.bool;
|
|
|
|
};
|
|
|
|
|
2022-04-15 01:41:22 +00:00
|
|
|
gateway = mkOption {
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "Gateway server to connect to.";
|
2022-04-15 01:41:22 +00:00
|
|
|
example = "gateway.example.com";
|
|
|
|
type = types.str;
|
|
|
|
};
|
|
|
|
|
|
|
|
protocol = mkOption {
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "Protocol to use.";
|
2022-04-15 01:41:22 +00:00
|
|
|
example = "anyconnect";
|
|
|
|
type =
|
|
|
|
types.enum [ "anyconnect" "array" "nc" "pulse" "gp" "f5" "fortinet" ];
|
|
|
|
};
|
|
|
|
|
|
|
|
user = mkOption {
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "Username to authenticate with.";
|
2022-04-15 01:41:22 +00:00
|
|
|
example = "example-user";
|
|
|
|
type = types.nullOr types.str;
|
2023-01-11 07:51:40 +00:00
|
|
|
default = null;
|
2022-04-15 01:41:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
# Note: It does not make sense to provide a way to declaratively
|
|
|
|
# set an authentication cookie, because they have to be requested
|
|
|
|
# for every new connection and would only work once.
|
|
|
|
passwordFile = mkOption {
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2022-04-15 01:41:22 +00:00
|
|
|
File containing the password to authenticate with. This
|
2022-08-12 12:06:08 +00:00
|
|
|
is passed to `openconnect` via the
|
|
|
|
`--passwd-on-stdin` option.
|
2022-04-15 01:41:22 +00:00
|
|
|
'';
|
|
|
|
default = null;
|
|
|
|
example = "/var/lib/secrets/openconnect-passwd";
|
|
|
|
type = types.nullOr types.path;
|
|
|
|
};
|
|
|
|
|
|
|
|
certificate = mkOption {
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "Certificate to authenticate with.";
|
2022-04-15 01:41:22 +00:00
|
|
|
default = null;
|
|
|
|
example = "/var/lib/secrets/openconnect_certificate.pem";
|
|
|
|
type = with types; nullOr (either path pkcs11);
|
|
|
|
};
|
|
|
|
|
|
|
|
privateKey = mkOption {
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "Private key to authenticate with.";
|
2022-04-15 01:41:22 +00:00
|
|
|
example = "/var/lib/secrets/openconnect_private_key.pem";
|
|
|
|
default = null;
|
|
|
|
type = with types; nullOr (either path pkcs11);
|
|
|
|
};
|
|
|
|
|
|
|
|
extraOptions = mkOption {
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2022-04-15 01:41:22 +00:00
|
|
|
Extra config to be appended to the interface config. It should
|
|
|
|
contain long-format options as would be accepted on the command
|
2022-08-12 12:06:08 +00:00
|
|
|
line by `openconnect`
|
2022-04-15 01:41:22 +00:00
|
|
|
(see https://www.infradead.org/openconnect/manual.html).
|
2022-08-12 12:06:08 +00:00
|
|
|
Non-key-value options like `deflate` can be used by
|
|
|
|
declaring them as booleans, i. e. `deflate = true;`.
|
2022-04-15 01:41:22 +00:00
|
|
|
'';
|
|
|
|
default = { };
|
|
|
|
example = {
|
|
|
|
compression = "stateless";
|
|
|
|
|
|
|
|
no-http-keepalive = true;
|
|
|
|
no-dtls = true;
|
|
|
|
};
|
|
|
|
type = with types; attrsOf (either str bool);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
generateExtraConfig = extra_cfg:
|
|
|
|
strings.concatStringsSep "\n" (attrsets.mapAttrsToList
|
|
|
|
(name: value: if (value == true) then name else "${name}=${value}")
|
|
|
|
(attrsets.filterAttrs (_: value: value != false) extra_cfg));
|
|
|
|
generateConfig = name: icfg:
|
|
|
|
pkgs.writeText "config" ''
|
|
|
|
interface=${name}
|
2023-02-02 18:25:31 +00:00
|
|
|
${optionalString (icfg.protocol != null) "protocol=${icfg.protocol}"}
|
2022-04-15 01:41:22 +00:00
|
|
|
${optionalString (icfg.user != null) "user=${icfg.user}"}
|
|
|
|
${optionalString (icfg.passwordFile != null) "passwd-on-stdin"}
|
|
|
|
${optionalString (icfg.certificate != null)
|
|
|
|
"certificate=${icfg.certificate}"}
|
|
|
|
${optionalString (icfg.privateKey != null) "sslkey=${icfg.privateKey}"}
|
|
|
|
|
|
|
|
${generateExtraConfig icfg.extraOptions}
|
|
|
|
'';
|
|
|
|
generateUnit = name: icfg: {
|
|
|
|
description = "OpenConnect Interface - ${name}";
|
|
|
|
requires = [ "network-online.target" ];
|
|
|
|
after = [ "network.target" "network-online.target" ];
|
2022-06-16 17:23:12 +00:00
|
|
|
wantedBy = optional icfg.autoStart "multi-user.target";
|
2022-04-15 01:41:22 +00:00
|
|
|
|
|
|
|
serviceConfig = {
|
|
|
|
Type = "simple";
|
|
|
|
ExecStart = "${openconnect}/bin/openconnect --config=${
|
|
|
|
generateConfig name icfg
|
|
|
|
} ${icfg.gateway}";
|
2023-01-11 07:51:40 +00:00
|
|
|
StandardInput = lib.mkIf (icfg.passwordFile != null) "file:${icfg.passwordFile}";
|
2022-04-15 01:41:22 +00:00
|
|
|
|
|
|
|
ProtectHome = true;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
in {
|
|
|
|
options.networking.openconnect = {
|
2023-01-11 07:51:40 +00:00
|
|
|
package = mkPackageOptionMD pkgs "openconnect" { };
|
2022-04-15 01:41:22 +00:00
|
|
|
|
|
|
|
interfaces = mkOption {
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "OpenConnect interfaces.";
|
2022-04-15 01:41:22 +00:00
|
|
|
default = { };
|
|
|
|
example = {
|
|
|
|
openconnect0 = {
|
|
|
|
gateway = "gateway.example.com";
|
|
|
|
protocol = "anyconnect";
|
|
|
|
user = "example-user";
|
|
|
|
passwordFile = "/var/lib/secrets/openconnect-passwd";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
type = with types; attrsOf (submodule interfaceOptions);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
config = {
|
|
|
|
systemd.services = mapAttrs' (name: value: {
|
|
|
|
name = "openconnect-${name}";
|
|
|
|
value = generateUnit name value;
|
|
|
|
}) cfg.interfaces;
|
|
|
|
};
|
|
|
|
|
|
|
|
meta.maintainers = with maintainers; [ alyaeanyx ];
|
|
|
|
}
|