2014-11-22 18:27:23 +00:00
|
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
|
|
let
|
|
|
|
|
|
|
|
|
|
inherit (builtins) toFile;
|
|
|
|
|
inherit (lib) concatMapStringsSep concatStringsSep mapAttrsToList
|
|
|
|
|
mkIf mkEnableOption mkOption types;
|
|
|
|
|
|
|
|
|
|
cfg = config.services.strongswan;
|
|
|
|
|
|
|
|
|
|
ipsecSecrets = secrets: toFile "ipsec.secrets" (
|
|
|
|
|
concatMapStringsSep "\n" (f: "include ${f}") secrets
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
ipsecConf = {setup, connections, ca}:
|
|
|
|
|
let
|
|
|
|
|
# https://wiki.strongswan.org/projects/strongswan/wiki/IpsecConf
|
|
|
|
|
makeSections = type: sections: concatStringsSep "\n\n" (
|
|
|
|
|
mapAttrsToList (sec: attrs:
|
|
|
|
|
"${type} ${sec}\n" +
|
|
|
|
|
(concatStringsSep "\n" ( mapAttrsToList (k: v: " ${k}=${v}") attrs ))
|
|
|
|
|
) sections
|
|
|
|
|
);
|
|
|
|
|
setupConf = makeSections "config" { inherit setup; };
|
|
|
|
|
connectionsConf = makeSections "conn" connections;
|
|
|
|
|
caConf = makeSections "ca" ca;
|
|
|
|
|
|
|
|
|
|
in
|
|
|
|
|
builtins.toFile "ipsec.conf" ''
|
|
|
|
|
${setupConf}
|
|
|
|
|
${connectionsConf}
|
|
|
|
|
${caConf}
|
|
|
|
|
'';
|
|
|
|
|
|
2017-10-31 11:14:00 +00:00
|
|
|
|
strongswanConf = {setup, connections, ca, secretsFile, managePlugins, enabledPlugins}: toFile "strongswan.conf" ''
|
2014-11-22 18:27:23 +00:00
|
|
|
|
charon {
|
2017-09-22 11:39:00 +01:00
|
|
|
|
${if managePlugins then "load_modular = no" else ""}
|
|
|
|
|
${if managePlugins then ("load = " + (concatStringsSep " " enabledPlugins)) else ""}
|
2014-11-22 18:27:23 +00:00
|
|
|
|
plugins {
|
|
|
|
|
stroke {
|
2017-10-31 11:14:00 +00:00
|
|
|
|
secrets_file = ${secretsFile}
|
2014-11-22 18:27:23 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
starter {
|
|
|
|
|
config_file = ${ipsecConf { inherit setup connections ca; }}
|
|
|
|
|
}
|
|
|
|
|
'';
|
|
|
|
|
|
|
|
|
|
in
|
|
|
|
|
{
|
|
|
|
|
options.services.strongswan = {
|
|
|
|
|
enable = mkEnableOption "strongSwan";
|
|
|
|
|
|
|
|
|
|
secrets = mkOption {
|
|
|
|
|
type = types.listOf types.path;
|
|
|
|
|
default = [];
|
|
|
|
|
example = [ "/run/keys/ipsec-foo.secret" ];
|
|
|
|
|
description = ''
|
|
|
|
|
A list of paths to IPSec secret files. These
|
2014-11-25 15:01:27 +00:00
|
|
|
|
files will be included into the main ipsec.secrets file with
|
|
|
|
|
the <literal>include</literal> directive. It is safer if these
|
|
|
|
|
paths are absolute.
|
2014-11-22 18:27:23 +00:00
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
setup = mkOption {
|
|
|
|
|
type = types.attrsOf types.str;
|
|
|
|
|
default = {};
|
|
|
|
|
example = { cachecrls = "yes"; strictcrlpolicy = "yes"; };
|
|
|
|
|
description = ''
|
2014-11-25 15:01:27 +00:00
|
|
|
|
A set of options for the ‘config setup’ section of the
|
|
|
|
|
<filename>ipsec.conf</filename> file. Defines general
|
|
|
|
|
configuration parameters.
|
2014-11-22 18:27:23 +00:00
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
connections = mkOption {
|
|
|
|
|
type = types.attrsOf (types.attrsOf types.str);
|
|
|
|
|
default = {};
|
|
|
|
|
example = {
|
|
|
|
|
"%default" = {
|
|
|
|
|
keyexchange = "ikev2";
|
|
|
|
|
keyingtries = "1";
|
|
|
|
|
};
|
|
|
|
|
roadwarrior = {
|
|
|
|
|
auto = "add";
|
|
|
|
|
leftcert = "/run/keys/moonCert.pem";
|
|
|
|
|
leftid = "@moon.strongswan.org";
|
|
|
|
|
leftsubnet = "10.1.0.0/16";
|
|
|
|
|
right = "%any";
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
description = ''
|
2014-11-25 15:01:27 +00:00
|
|
|
|
A set of connections and their options for the ‘conn xxx’
|
|
|
|
|
sections of the <filename>ipsec.conf</filename> file.
|
2014-11-22 18:27:23 +00:00
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
ca = mkOption {
|
|
|
|
|
type = types.attrsOf (types.attrsOf types.str);
|
|
|
|
|
default = {};
|
|
|
|
|
example = {
|
|
|
|
|
strongswan = {
|
|
|
|
|
auto = "add";
|
|
|
|
|
cacert = "/run/keys/strongswanCert.pem";
|
|
|
|
|
crluri = "http://crl2.strongswan.org/strongswan.crl";
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
description = ''
|
2014-11-25 15:01:27 +00:00
|
|
|
|
A set of CAs (certification authorities) and their options for
|
|
|
|
|
the ‘ca xxx’ sections of the <filename>ipsec.conf</filename>
|
|
|
|
|
file.
|
2014-11-22 18:27:23 +00:00
|
|
|
|
'';
|
|
|
|
|
};
|
2017-09-22 11:39:00 +01:00
|
|
|
|
|
|
|
|
|
managePlugins = mkOption {
|
|
|
|
|
type = types.bool;
|
|
|
|
|
default = false;
|
|
|
|
|
description = ''
|
|
|
|
|
If set to true, this option will disable automatic plugin loading and
|
|
|
|
|
then tell strongSwan to enable the plugins specified in the
|
|
|
|
|
<option>enabledPlugins</option> option.
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
enabledPlugins = mkOption {
|
|
|
|
|
type = types.listOf types.str;
|
|
|
|
|
default = [];
|
|
|
|
|
description = ''
|
|
|
|
|
A list of additional plugins to enable if
|
|
|
|
|
<option>managePlugins</option> is true.
|
|
|
|
|
'';
|
|
|
|
|
};
|
2014-11-22 18:27:23 +00:00
|
|
|
|
};
|
|
|
|
|
|
2017-10-31 11:14:00 +00:00
|
|
|
|
|
|
|
|
|
config = with cfg;
|
|
|
|
|
let
|
|
|
|
|
secretsFile = ipsecSecrets cfg.secrets;
|
|
|
|
|
in
|
|
|
|
|
mkIf enable
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
# here we should use the default strongswan ipsec.secrets and
|
|
|
|
|
# append to it (default one is empty so not a pb for now)
|
|
|
|
|
environment.etc."ipsec.secrets".source = secretsFile;
|
|
|
|
|
|
2014-11-22 18:27:23 +00:00
|
|
|
|
systemd.services.strongswan = {
|
2014-11-25 15:01:27 +00:00
|
|
|
|
description = "strongSwan IPSec Service";
|
2014-11-22 18:27:23 +00:00
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
2016-08-14 11:00:52 +01:00
|
|
|
|
path = with pkgs; [ kmod iproute iptables utillinux ]; # XXX Linux
|
2014-11-22 18:27:23 +00:00
|
|
|
|
wants = [ "keys.target" ];
|
2017-07-17 14:40:33 +01:00
|
|
|
|
after = [ "network-online.target" "keys.target" ];
|
2014-11-22 18:27:23 +00:00
|
|
|
|
environment = {
|
2017-10-31 11:14:00 +00:00
|
|
|
|
STRONGSWAN_CONF = strongswanConf { inherit setup connections ca secretsFile managePlugins enabledPlugins; };
|
2014-11-22 18:27:23 +00:00
|
|
|
|
};
|
|
|
|
|
serviceConfig = {
|
|
|
|
|
ExecStart = "${pkgs.strongswan}/sbin/ipsec start --nofork";
|
|
|
|
|
};
|
2017-10-31 11:14:00 +00:00
|
|
|
|
preStart = ''
|
|
|
|
|
# with 'nopeerdns' setting, ppp writes into this folder
|
|
|
|
|
mkdir -m 700 -p /etc/ppp
|
|
|
|
|
'';
|
2014-11-22 18:27:23 +00:00
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|