summary refs log tree commit diff
path: root/nixos/modules/services/security/haka.nix
blob: 2cfc05f3033bbee9c254aaf087e93bdfc321dded (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
# This module defines global configuration for Haka.

{ config, lib, pkgs, ... }:

with lib;

let

  cfg = config.services.haka;

  haka = cfg.package;

  hakaConf = pkgs.writeText "haka.conf"
  ''
    [general]
    configuration = ${if lib.strings.hasPrefix "/" cfg.configFile
      then "${cfg.configFile}"
      else "${haka}/share/haka/sample/${cfg.configFile}"}
    ${optionalString (builtins.lessThan 0 cfg.threads) "thread = ${cfg.threads}"}

    [packet]
    ${optionalString cfg.pcap ''module = "packet/pcap"''}
    ${optionalString cfg.nfqueue ''module = "packet/nqueue"''}
    ${optionalString cfg.dump.enable ''dump = "yes"''}
    ${optionalString cfg.dump.enable ''dump_input = "${cfg.dump.input}"''}
    ${optionalString cfg.dump.enable ''dump_output = "${cfg.dump.output}"''}

    interfaces = "${lib.strings.concatStringsSep "," cfg.interfaces}"

    [log]
    # Select the log module
    module = "log/syslog"

    # Set the default logging level
    #level = "info,packet=debug"

    [alert]
    # Select the alert module
    module = "alert/syslog"

    # Disable alert on standard output
    #alert_on_stdout = no

    # alert/file module option
    #file = "/dev/null"
  '';

in

{

  ###### interface

  options = {

    services.haka = {

      enable = mkEnableOption "Haka";

      package = mkOption {
        default = pkgs.haka;
        defaultText = literalExpression "pkgs.haka";
        type = types.package;
        description = "
          Which Haka derivation to use.
        ";
      };

      configFile = mkOption {
        default = "empty.lua";
        example = "/srv/haka/myfilter.lua";
        type = types.str;
        description = ''
          Specify which configuration file Haka uses.
          It can be absolute path or a path relative to the sample directory of
          the haka git repo.
        '';
      };

      interfaces = mkOption {
        default = [ "eth0" ];
        example = [ "any" ];
        type = with types; listOf str;
        description = ''
          Specify which interface(s) Haka listens to.
          Use 'any' to listen to all interfaces.
        '';
      };

      threads = mkOption {
        default = 0;
        example = 4;
        type = types.int;
        description = ''
          The number of threads that will be used.
          All system threads are used by default.
        '';
      };

      pcap = mkOption {
        default = true;
        type = types.bool;
        description = "Whether to enable pcap";
      };

      nfqueue = mkEnableOption "nfqueue";

      dump.enable = mkEnableOption "dump";
      dump.input  = mkOption {
        default = "/tmp/input.pcap";
        example = "/path/to/file.pcap";
        type = types.path;
        description = "Path to file where incoming packets are dumped";
      };

      dump.output  = mkOption {
        default = "/tmp/output.pcap";
        example = "/path/to/file.pcap";
        type = types.path;
        description = "Path to file where outgoing packets are dumped";
      };
    };
  };


  ###### implementation

  config = mkIf cfg.enable {

    assertions = [
      { assertion = cfg.pcap != cfg.nfqueue;
        message = "either pcap or nfqueue can be enabled, not both.";
      }
      { assertion = cfg.nfqueue -> !dump.enable;
        message = "dump can only be used with nfqueue.";
      }
      { assertion = cfg.interfaces != [];
        message = "at least one interface must be specified.";
      }];


    environment.systemPackages = [ haka ];

    systemd.services.haka = {
      description = "Haka";
      wantedBy = [ "multi-user.target" ];
      after = [ "network.target" ];
      serviceConfig = {
        ExecStart = "${haka}/bin/haka -c ${hakaConf}";
        ExecStop = "${haka}/bin/hakactl stop";
        User = "root";
        Type = "forking";
      };
    };
  };
}