summaryrefslogtreecommitdiffstats
path: root/nixos/modules/services/network-filesystems/samba.nix
blob: 79a0d42904f85a6bda3256e4f980dc0f7e01caa2 (plain)
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
{
  config,
  lib,
  pkgs,
  ...
}:
let
  cfg = config.services.samba;

  settingsFormat = pkgs.formats.ini {
    listToValue = lib.concatMapStringsSep " " (lib.generators.mkValueStringDefault { });
  };
  # Ensure the global section is always first
  globalConfigFile = settingsFormat.generate "smb-global.conf" { global = cfg.settings.global; };
  sharesConfigFile = settingsFormat.generate "smb-shares.conf" (
    lib.removeAttrs cfg.settings [ "global" ]
  );

  configFile = pkgs.concatText "smb.conf" [
    globalConfigFile
    sharesConfigFile
  ];

in

{
  meta = {
    doc = ./samba.md;
    maintainers = [ lib.maintainers.anthonyroussel ];
  };

  imports = [
    (lib.mkRemovedOptionModule [ "services" "samba" "defaultShare" ] "")
    (lib.mkRemovedOptionModule [ "services" "samba" "syncPasswordsByPam" ]
      "This option has been removed by upstream, see https://bugzilla.samba.org/show_bug.cgi?id=10669#c10"
    )

    (lib.mkRemovedOptionModule [ "services" "samba" "configText" ] ''
      Use services.samba.settings instead.

      This is part of the general move to use structured settings instead of raw
      text for config as introduced by RFC0042:
      https://github.com/NixOS/rfcs/blob/master/rfcs/0042-config-option.md
    '')
    (lib.mkRemovedOptionModule [
      "services"
      "samba"
      "extraConfig"
    ] "Use services.samba.settings instead.")
    (lib.mkRenamedOptionModule
      [ "services" "samba" "invalidUsers" ]
      [ "services" "samba" "settings" "global" "invalid users" ]
    )
    (lib.mkRenamedOptionModule
      [ "services" "samba" "securityType" ]
      [ "services" "samba" "settings" "global" "security" ]
    )
    (lib.mkRenamedOptionModule [ "services" "samba" "shares" ] [ "services" "samba" "settings" ])

    (lib.mkRenamedOptionModule
      [ "services" "samba" "enableWinbindd" ]
      [ "services" "samba" "winbindd" "enable" ]
    )
    (lib.mkRenamedOptionModule
      [ "services" "samba" "enableNmbd" ]
      [ "services" "samba" "nmbd" "enable" ]
    )
  ];

  ###### interface

  options = {
    services.samba = {
      enable = lib.mkEnableOption "Samba, the SMB/CIFS protocol";

      package = lib.mkPackageOption pkgs "samba" {
        example = "samba4Full";
      };

      openFirewall = lib.mkEnableOption "opening the default ports in the firewall for Samba";

      smbd = {
        enable = lib.mkOption {
          type = lib.types.bool;
          default = true;
          description = "Whether to enable Samba's smbd daemon.";
        };

        extraArgs = lib.mkOption {
          type = lib.types.listOf lib.types.str;
          default = [ ];
          description = "Extra arguments to pass to the smbd service.";
        };
      };

      nmbd = {
        enable = lib.mkOption {
          type = lib.types.bool;
          default = true;
          description = ''
            Whether to enable Samba's nmbd, which replies to NetBIOS over IP name
            service requests. It also participates in the browsing protocols
            which make up the Windows "Network Neighborhood" view.
          '';
        };

        extraArgs = lib.mkOption {
          type = lib.types.listOf lib.types.str;
          default = [ ];
          description = "Extra arguments to pass to the nmbd service.";
        };
      };

      winbindd = {
        enable = lib.mkOption {
          type = lib.types.bool;
          default = true;
          description = ''
            Whether to enable Samba's winbindd, which provides a number of services
            to the Name Service Switch capability found in most modern C libraries,
            to arbitrary applications via PAM and ntlm_auth and to Samba itself.
          '';
        };

        extraArgs = lib.mkOption {
          type = lib.types.listOf lib.types.str;
          default = [ ];
          description = "Extra arguments to pass to the winbindd service.";
        };
      };

      usershares = {
        enable = lib.mkEnableOption "user-configurable Samba shares";
        group = lib.mkOption {
          type = lib.types.str;
          default = "samba";
          description = ''
            Name of the group members of which will be allowed to create usershares.

            The group will be created automatically.
          '';
        };
      };

      nsswins = lib.mkEnableOption ''
        WINS NSS (Name Service Switch) plug-in.

        Enabling it allows applications to resolve WINS/NetBIOS names (a.k.a.
        Windows machine names) by transparently querying the winbindd daemon
      '';

      settings = lib.mkOption {
        type = lib.types.submodule {
          freeformType = settingsFormat.type;
          options = {
            global.security = lib.mkOption {
              type = lib.types.enum [
                "auto"
                "user"
                "domain"
                "ads"
              ];
              default = "user";
              description = "Samba security type.";
            };
            global."invalid users" = lib.mkOption {
              type = lib.types.listOf lib.types.str;
              default = [ "root" ];
              description = "List of users who are denied to login via Samba.";
            };
            global."passwd program" = lib.mkOption {
              type = lib.types.str;
              default = "/run/wrappers/bin/passwd %u";
              description = "Path to a program that can be used to set UNIX user passwords.";
            };
          };
        };
        default = {
          "global" = {
            "security" = "user";
            "passwd program" = "/run/wrappers/bin/passwd %u";
            "invalid users" = [ "root" ];
          };
        };
        example = {
          "global" = {
            "security" = "user";
            "passwd program" = "/run/wrappers/bin/passwd %u";
            "invalid users" = [ "root" ];
          };
          "public" = {
            "path" = "/srv/public";
            "read only" = "yes";
            "browseable" = "yes";
            "guest ok" = "yes";
            "comment" = "Public samba share.";
          };
        };
        description = ''
          Configuration file for the Samba suite in ini format.
          This file is located in /etc/samba/smb.conf

          Refer to <https://www.samba.org/samba/docs/current/man-html/smb.conf.5.html>
          for all available options.
        '';
      };
    };
  };

  ###### implementation

  config = lib.mkMerge [
    {
      assertions = [
        {
          assertion = cfg.nsswins -> cfg.winbindd.enable;
          message = "If services.samba.nsswins is enabled, then services.samba.winbindd.enable must also be enabled";
        }
      ];
    }

    (lib.mkIf cfg.enable {
      environment.etc."samba/smb.conf".source = configFile;

      system.nssModules = lib.optional cfg.nsswins cfg.package;
      system.nssDatabases.hosts = lib.optional cfg.nsswins "wins";

      systemd = {
        slices.system-samba = {
          description = "Samba (SMB Networking Protocol) Slice";
        };
        targets.samba = {
          description = "Samba Server";
          after = [ "network.target" ];
          wants = [ "network-online.target" ];
          wantedBy = [ "multi-user.target" ];
        };
        tmpfiles.rules = [
          "d /var/lock/samba - - - - -"
          "d /var/log/samba - - - - -"
          "d /var/cache/samba - - - - -"
          "d /var/lib/samba/private - - - - -"
        ];
      };

      security.pam.services.samba = { };
      environment.systemPackages = [ cfg.package ];
      # Like other mount* related commands that need the setuid bit, this is
      # required too.
      security.wrappers."mount.cifs" = {
        program = "mount.cifs";
        source = "${lib.getBin pkgs.cifs-utils}/bin/mount.cifs";
        owner = "root";
        group = "root";
        setuid = true;
      };

      networking.firewall.allowedTCPPorts = lib.mkIf cfg.openFirewall [
        139
        445
      ];
      networking.firewall.allowedUDPPorts = lib.mkIf cfg.openFirewall [
        137
        138
      ];
    })

    (lib.mkIf (cfg.enable && cfg.nmbd.enable) {
      systemd.services.samba-nmbd = {
        description = "Samba NMB Daemon";
        documentation = [
          "man:nmbd(8)"
          "man:samba(7)"
          "man:smb.conf(5)"
        ];

        after = [
          "network.target"
          "network-online.target"
        ];

        partOf = [ "samba.target" ];
        wantedBy = [ "samba.target" ];
        wants = [ "network-online.target" ];

        environment.LD_LIBRARY_PATH = config.system.nssModules.path;

        serviceConfig = {
          ExecReload = "${pkgs.coreutils}/bin/kill -HUP $MAINPID";
          ExecStart = "${cfg.package}/sbin/nmbd --foreground --no-process-group ${lib.escapeShellArgs cfg.nmbd.extraArgs}";
          LimitCORE = "infinity";
          PIDFile = "/run/samba/nmbd.pid";
          Slice = "system-samba.slice";
          Type = "notify";
        };

        unitConfig.RequiresMountsFor = "/var/lib/samba";

        restartTriggers = [ configFile ];
      };
    })

    (lib.mkIf (cfg.enable && cfg.smbd.enable) {
      systemd.services.samba-smbd = {
        description = "Samba SMB Daemon";
        documentation = [
          "man:smbd(8)"
          "man:samba(7)"
          "man:smb.conf(5)"
        ];

        after = [
          "network.target"
          "network-online.target"
        ]
        ++ lib.optionals (cfg.nmbd.enable) [
          "samba-nmbd.service"
        ]
        ++ lib.optionals (cfg.winbindd.enable) [
          "samba-winbindd.service"
        ];

        partOf = [ "samba.target" ];
        wantedBy = [ "samba.target" ];
        wants = [ "network-online.target" ];

        environment.LD_LIBRARY_PATH = config.system.nssModules.path;

        serviceConfig = {
          ExecReload = "${pkgs.coreutils}/bin/kill -HUP $MAINPID";
          ExecStart = "${cfg.package}/sbin/smbd --foreground --no-process-group ${lib.escapeShellArgs cfg.smbd.extraArgs}";
          LimitCORE = "infinity";
          LimitNOFILE = 16384;
          PIDFile = "/run/samba/smbd.pid";
          Slice = "system-samba.slice";
          Type = "notify";
        };

        unitConfig.RequiresMountsFor = "/var/lib/samba";

        restartTriggers = [ configFile ];
      };
    })

    (lib.mkIf (cfg.enable && cfg.winbindd.enable) {
      systemd.services.samba-winbindd = {
        description = "Samba Winbind Daemon";
        documentation = [
          "man:winbindd(8)"
          "man:samba(7)"
          "man:smb.conf(5)"
        ];

        after = [
          "network.target"
        ]
        ++ lib.optionals (cfg.nmbd.enable) [
          "samba-nmbd.service"
        ];

        partOf = [ "samba.target" ];
        wantedBy = [ "samba.target" ];

        environment.LD_LIBRARY_PATH = config.system.nssModules.path;

        serviceConfig = {
          ExecReload = "${pkgs.coreutils}/bin/kill -HUP $MAINPID";
          ExecStart = "${cfg.package}/sbin/winbindd --foreground --no-process-group ${lib.escapeShellArgs cfg.winbindd.extraArgs}";
          LimitCORE = "infinity";
          PIDFile = "/run/samba/winbindd.pid";
          Slice = "system-samba.slice";
          Type = "notify";
        };

        unitConfig.RequiresMountsFor = "/var/lib/samba";

        restartTriggers = [ configFile ];
      };
    })

    (lib.mkIf (cfg.enable && cfg.usershares.enable) {
      users.groups.${cfg.usershares.group} = { };

      systemd.tmpfiles.settings."50-samba-usershares"."/var/lib/samba/usershares".d = {
        user = "root";
        group = cfg.usershares.group;
        mode = "1775"; # sticky so users can't delete others' shares
      };

      # set some reasonable defaults
      services.samba.settings.global = lib.mkDefault {
        "usershare path" = "/var/lib/samba/usershares";
        "usershare max shares" = 100; # high enough to be considered ~unlimited
        "usershare allow guests" = true;
      };
    })
  ];
}