summary refs log tree commit diff
path: root/nixos/modules/services/networking/mosquitto.nix
blob: 8e814ffd0b9b022b2579d1ff151061cdbca036fc (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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
{ config, lib, pkgs, ...}:

with lib;

let
  cfg = config.services.mosquitto;

  listenerConf = optionalString cfg.ssl.enable ''
    listener ${toString cfg.ssl.port} ${cfg.ssl.host}
    cafile ${cfg.ssl.cafile}
    certfile ${cfg.ssl.certfile}
    keyfile ${cfg.ssl.keyfile}
  '';

  passwordConf = optionalString cfg.checkPasswords ''
    password_file ${cfg.dataDir}/passwd
  '';

  mosquittoConf = pkgs.writeText "mosquitto.conf" ''
    acl_file ${aclFile}
    persistence true
    allow_anonymous ${boolToString cfg.allowAnonymous}
    listener ${toString cfg.port} ${cfg.host}
    ${passwordConf}
    ${listenerConf}
    ${cfg.extraConf}
  '';

  userAcl = (concatStringsSep "\n\n" (mapAttrsToList (n: c:
    "user ${n}\n" + (concatStringsSep "\n" c.acl)) cfg.users
  ));

  aclFile = pkgs.writeText "mosquitto.acl" ''
    ${cfg.aclExtraConf}
    ${userAcl}
  '';

in

{

  ###### Interface

  options = {
    services.mosquitto = {
      enable = mkEnableOption "the MQTT Mosquitto broker";

      host = mkOption {
        default = "127.0.0.1";
        example = "0.0.0.0";
        type = types.str;
        description = ''
          Host to listen on without SSL.
        '';
      };

      port = mkOption {
        default = 1883;
        example = 1883;
        type = types.int;
        description = ''
          Port on which to listen without SSL.
        '';
      };

      ssl = {
        enable = mkEnableOption "SSL listener";

        cafile = mkOption {
          type = types.nullOr types.path;
          default = null;
          description = "Path to PEM encoded CA certificates.";
        };

        certfile = mkOption {
          type = types.nullOr types.path;
          default = null;
          description = "Path to PEM encoded server certificate.";
        };

        keyfile = mkOption {
          type = types.nullOr types.path;
          default = null;
          description = "Path to PEM encoded server key.";
        };

        host = mkOption {
          default = "0.0.0.0";
          example = "localhost";
          type = types.str;
          description = ''
            Host to listen on with SSL.
          '';
        };

        port = mkOption {
          default = 8883;
          example = 8883;
          type = types.int;
          description = ''
            Port on which to listen with SSL.
          '';
        };
      };

      dataDir = mkOption {
        default = "/var/lib/mosquitto";
        type = types.path;
        description = ''
          The data directory.
        '';
      };

      users = mkOption {
        type = types.attrsOf (types.submodule {
          options = {
            password = mkOption {
              type = with types; uniq (nullOr str);
              default = null;
              description = ''
                Specifies the (clear text) password for the MQTT User.
              '';
            };

            passwordFile = mkOption {
              type = with types; uniq (nullOr str);
              example = "/path/to/file";
              default = null;
              description = ''
                Specifies the path to a file containing the
                clear text password for the MQTT user.
              '';
            };

            hashedPassword = mkOption {
              type = with types; uniq (nullOr str);
              default = null;
              description = ''
                Specifies the hashed password for the MQTT User.
                To generate hashed password install <literal>mosquitto</literal>
                package and use <literal>mosquitto_passwd</literal>.
              '';
            };

            hashedPasswordFile = mkOption {
              type = with types; uniq (nullOr str);
              example = "/path/to/file";
              default = null;
              description = ''
                Specifies the path to a file containing the
                hashed password for the MQTT user.
                To generate hashed password install <literal>mosquitto</literal>
                package and use <literal>mosquitto_passwd</literal>.
              '';
            };

            acl = mkOption {
              type = types.listOf types.str;
              example = [ "topic read A/B" "topic A/#" ];
              description = ''
                Control client access to topics on the broker.
              '';
            };
          };
        });
        example = { john = { password = "123456"; acl = [ "topic readwrite john/#" ]; }; };
        description = ''
          A set of users and their passwords and ACLs.
        '';
      };

      allowAnonymous = mkOption {
        default = false;
        type = types.bool;
        description = ''
          Allow clients to connect without authentication.
        '';
      };

      checkPasswords = mkOption {
        default = false;
        example = true;
        type = types.bool;
        description = ''
          Refuse connection when clients provide incorrect passwords.
        '';
      };

      extraConf = mkOption {
        default = "";
        type = types.lines;
        description = ''
          Extra config to append to `mosquitto.conf` file.
        '';
      };

      aclExtraConf = mkOption {
        default = "";
        type = types.lines;
        description = ''
          Extra config to prepend to the ACL file.
        '';
      };

    };
  };


  ###### Implementation

  config = mkIf cfg.enable {

    assertions = mapAttrsToList (name: cfg: {
      assertion = length (filter (s: s != null) (with cfg; [
        password passwordFile hashedPassword hashedPasswordFile
      ])) <= 1;
      message = "Cannot set more than one password option";
    }) cfg.users;

    systemd.services.mosquitto = {
      description = "Mosquitto MQTT Broker Daemon";
      wantedBy = [ "multi-user.target" ];
      after = [ "network.target" ];
      serviceConfig = {
        Type = "notify";
        NotifyAccess = "main";
        User = "mosquitto";
        Group = "mosquitto";
        RuntimeDirectory = "mosquitto";
        WorkingDirectory = cfg.dataDir;
        Restart = "on-failure";
        ExecStart = "${pkgs.mosquitto}/bin/mosquitto -c ${mosquittoConf}";
        ExecReload = "${pkgs.coreutils}/bin/kill -HUP $MAINPID";

        # Hardening
        CapabilityBoundingSet = "";
        DevicePolicy = "closed";
        LockPersonality = true;
        MemoryDenyWriteExecute = true;
        NoNewPrivileges = true;
        PrivateDevices = true;
        PrivateTmp = true;
        PrivateUsers = true;
        ProtectClock = true;
        ProtectControlGroups = true;
        ProtectHome = true;
        ProtectHostname = true;
        ProtectKernelLogs = true;
        ProtectKernelModules = true;
        ProtectKernelTunables = true;
        ProtectProc = "invisible";
        ProcSubset = "pid";
        ProtectSystem = "strict";
        ReadWritePaths = [
          cfg.dataDir
          "/tmp"  # mosquitto_passwd creates files in /tmp before moving them
        ];
        ReadOnlyPaths = with cfg.ssl; lib.optionals (enable) [
          certfile
          keyfile
          cafile
        ];
        RemoveIPC = true;
        RestrictAddressFamilies = [
          "AF_UNIX"  # for sd_notify() call
          "AF_INET"
          "AF_INET6"
        ];
        RestrictNamespaces = true;
        RestrictRealtime = true;
        RestrictSUIDSGID = true;
        SystemCallArchitectures = "native";
        SystemCallFilter = [
          "@system-service"
          "~@privileged"
          "~@resources"
        ];
        UMask = "0077";
      };
      preStart = ''
        rm -f ${cfg.dataDir}/passwd
        touch ${cfg.dataDir}/passwd
      '' + concatStringsSep "\n" (
        mapAttrsToList (n: c:
          if c.hashedPasswordFile != null then
            "echo '${n}:'$(cat '${c.hashedPasswordFile}') >> ${cfg.dataDir}/passwd"
          else if c.passwordFile != null then
            "${pkgs.mosquitto}/bin/mosquitto_passwd -b ${cfg.dataDir}/passwd ${n} $(cat '${c.passwordFile}')"
          else if c.hashedPassword != null then
            "echo '${n}:${c.hashedPassword}' >> ${cfg.dataDir}/passwd"
          else optionalString (c.password != null)
            "${pkgs.mosquitto}/bin/mosquitto_passwd -b ${cfg.dataDir}/passwd ${n} '${c.password}'"
        ) cfg.users);
    };

    users.users.mosquitto = {
      description = "Mosquitto MQTT Broker Daemon owner";
      group = "mosquitto";
      uid = config.ids.uids.mosquitto;
      home = cfg.dataDir;
      createHome = true;
    };

    users.groups.mosquitto.gid = config.ids.gids.mosquitto;

  };
}