-
Notifications
You must be signed in to change notification settings - Fork 3
/
samba.nix
370 lines (346 loc) · 11.4 KB
/
samba.nix
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
{
config,
globals,
lib,
...
}: let
smbUsers = config.repo.secrets.local.samba.users;
smbGroups = config.repo.secrets.local.samba.groups;
mkPersistent = persistRoot: directory: owner: {
${persistRoot}.directories = [
{
inherit directory;
user = owner;
group = owner;
mode = "0750";
}
];
};
mkShare = id: path: cfg: {
${id} =
{
inherit path;
public = "no";
writable = "yes";
"create mask" = "0740";
"directory mask" = "0750";
"acl allow execute always" = "yes";
}
// cfg;
};
mkGroupShares = group: {enableBunker ? false, ...}:
[
(mkShare group "/shares/groups/${group}" {
"valid users" = "@${group}";
"force user" = group;
"force group" = group;
})
]
++ lib.optional enableBunker (
mkShare "${group}-bunker" "/shares/groups/${group}-bunker" {
"valid users" = "@${group}";
"force user" = group;
"force group" = group;
}
);
mkUserShares = user: {
enableBunker ? false,
enablePaperless ? false,
...
}:
[
(mkShare user "/shares/users/${user}" {
"valid users" = user;
})
]
++ lib.optional enableBunker (
mkShare "${user}-bunker" "/shares/users/${user}-bunker" {
"valid users" = user;
}
)
++ lib.optional enablePaperless (
mkShare "${user}-paperless" "/shares/users/${user}-paperless" {
"valid users" = user;
"force user" = "paperless";
"force group" = "paperless";
}
);
in {
# For influxdb communication channel
wireguard.proxy-home.client.via = "ward";
age.secrets."samba-passdb.tdb" = {
rekeyFile = config.node.secretsDir + "/samba-passdb.tdb.age";
mode = "600";
};
services.openssh = {
# You really have to hate them. Thanks Brother ADS-4300N.
settings = {
Macs = ["hmac-sha2-512"];
HostkeyAlgorithms = "+ssh-rsa";
PubkeyAcceptedAlgorithms = "+ssh-rsa";
};
# We need an RSA key for network attached printers and scanners
# that fucking can't be bothered to support sensible stuff
hostKeys = [
{
bits = 4096;
path = "/etc/ssh/ssh_host_rsa_key";
type = "rsa";
}
];
# Allow SFTP for scanner in /shares/groups/scanner
extraConfig = ''
Match User scanner
ForceCommand internal-sftp
AllowTcpForwarding no
PermitTunnel no
'';
};
environment.persistence = lib.mkMerge (
[
{
"/persist".files = [
"/etc/ssh/ssh_host_rsa_key"
"/etc/ssh/ssh_host_rsa_key.pub"
];
}
]
++ lib.flatten (
lib.flip lib.mapAttrsToList smbUsers (
name: {enableBunker ? false, ...}:
[(mkPersistent "/storage" "/shares/users/${name}" name)]
++ lib.optional enableBunker (
mkPersistent "/bunker" "/shares/users/${name}-bunker" name
)
)
++ lib.flip lib.mapAttrsToList smbGroups (
name: {enableBunker ? false, ...}:
[(mkPersistent "/storage" "/shares/groups/${name}" name)]
++ lib.optional enableBunker (
mkPersistent "/bunker" "/shares/groups/${name}-bunker" name
)
)
)
);
services.samba-wsdd = {
enable = true;
openFirewall = true;
};
globals.monitoring.tcp.samba = {
host = globals.net.home-lan.hosts.sire-samba.ipv4;
port = 445;
network = "home-lan";
};
services.samba = {
enable = true;
openFirewall = true;
# Disable Samba's nmbd, because we don't want to reply to NetBIOS over IP
# requests, since all of our clients hardcode the server shares.
nmbd.enable = false;
# Disable 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.
winbindd.enable = false;
settings = lib.mkMerge ([
{
global = {
# Show the server host name in the printer comment box in print manager
# and next to the IPC connection in net view.
"server string" = "SambaOelig";
# Set the NetBIOS name by which the Samba server is known.
"netbios name" = "SambaOelig";
# Disable netbios support. We don't need to support browsing since all
# clients hardcode the host and share names.
"disable netbios" = "yes";
# Deny access to all hosts by default.
"hosts deny" = "0.0.0.0/0";
# Allow access to local network and TODO: wireguard
"hosts allow" = "${globals.net.home-lan.cidrv4} ${globals.net.home-lan.cidrv6}";
# Don't advertise inaccessible shares to users
"access based share enum" = "yes";
# Set sane logging options
"log level" = "0 auth:2 passdb:2";
"log file" = "/dev/null";
"max log size" = "0";
"logging" = "systemd";
# TODO: allow based on wireguard ip without username and password
# Users always have to login with an account and are never mapped
# to a guest account.
"passdb backend" = "tdbsam:${config.age.secrets."samba-passdb.tdb".path}";
"server role" = "standalone";
"guest account" = "nobody";
"map to guest" = "never";
# Clients should only connect using the latest SMB3 protocol (e.g., on
# clients running Windows 8 and later).
"server min protocol" = "SMB3_11";
# Require native SMB transport encryption by default.
"server smb encrypt" = "required";
# Never map anything to the excutable bit.
"map archive" = "no";
"map system" = "no";
"map hidden" = "no";
# Disable printer sharing. By default Samba shares printers configured
# using CUPS.
"load printers" = "no";
"printing" = "bsd";
"printcap name" = "/dev/null";
"disable spoolss" = "yes";
"show add printer wizard" = "no";
# Load in modules (order is critical!) and enable AAPL extensions.
"vfs objects" = "catia fruit streams_xattr";
# Enable Apple's SMB2+ extension.
"fruit:aapl" = "yes";
# Clean up unused or empty files created by the OS or Samba.
"fruit:wipe_intentionally_left_blank_rfork" = "yes";
"fruit:delete_empty_adfiles" = "yes";
};
}
]
++ lib.flatten (
lib.mapAttrsToList mkUserShares smbUsers
++ lib.mapAttrsToList mkGroupShares smbGroups
));
};
systemd.tmpfiles.settings = lib.mkMerge (
# Make sure the main paperless structure exists
[
{
"10-smb-paperless" = {
"/paperless/consume".d = {
user = "paperless";
group = "paperless";
mode = "0750";
};
"/paperless/media".d = {
user = "paperless";
group = "paperless";
mode = "0750";
};
"/paperless/media/documents".d = {
user = "paperless";
group = "paperless";
mode = "0750";
};
"/paperless/media/documents/archive".d = {
user = "paperless";
group = "paperless";
mode = "0750";
};
"/paperless/media/documents/originals".d = {
user = "paperless";
group = "paperless";
mode = "0750";
};
};
}
]
# For each paperless share, make sure the necessary sub-folders for that user are created
# at boot so we can bind-mount them into the shares.
++ lib.flatten (lib.flip lib.mapAttrsToList smbUsers (
user: userCfg:
lib.optional (userCfg.enablePaperless or false) {
"10-smb-paperless" = {
"/shares/users/${user}-paperless".d = {
user = "paperless";
group = "paperless";
mode = "0550";
};
"/paperless/consume/${user}".d = {
user = "paperless";
group = "paperless";
mode = "0750";
};
"/paperless/media/documents/archive/${user}".d = {
user = "paperless";
group = "paperless";
mode = "0750";
};
# A .keep file prevents paperless from removing this folder if no documents are present
"/paperless/media/documents/archive/${user}/.keep".f = {
user = "paperless";
group = "paperless";
mode = "0640";
};
"/paperless/media/documents/originals/${user}".d = {
user = "paperless";
group = "paperless";
mode = "0750";
};
# A .keep file prevents paperless from removing this folder if no documents are present
"/paperless/media/documents/originals/${user}/.keep".f = {
user = "paperless";
group = "paperless";
mode = "0640";
};
};
}
))
);
# For each paperless share, bind-mount create the necessary folders using tmpfiles.
fileSystems = lib.mkMerge (
[
{
"/storage".neededForBoot = true;
"/bunker".neededForBoot = true;
}
]
++ lib.flip lib.mapAttrsToList smbUsers (
user: userCfg:
lib.optionalAttrs (userCfg.enablePaperless or false) {
"/shares/users/${user}-paperless/consume" = {
fsType = "none";
options = ["bind"];
device = "/paperless/consume/${user}";
};
"/shares/users/${user}-paperless/documents" = {
fsType = "none";
options = ["bind" "ro"];
device = "/paperless/media/documents/archive/${user}";
};
"/shares/users/${user}-paperless/originals" = {
fsType = "none";
options = ["bind" "ro"];
device = "/paperless/media/documents/originals/${user}";
};
}
)
);
users.users = let
mkUser = name: id: groups: {
isNormalUser = true;
uid = id;
group = name;
extraGroups = groups;
createHome = false;
home = "/var/empty";
useDefaultShell = false;
autoSubUidGidRange = false;
};
in
lib.mkMerge [
(
{}
// lib.mapAttrs (name: cfg: mkUser name cfg.id cfg.groups) smbUsers
// lib.mapAttrs (name: cfg: mkUser name cfg.id []) smbGroups
)
{
scanner.openssh.authorizedKeys.keys = [
"ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDJcWkqM2gXM9MJoKggCMpXLBJvgPP0fuoIO3UNy4h4uFzyDqMKAADjaJHCqyIQPq/s5vATVmuu4GQyajkc7Y3fBg/2rvAACzFx/2ufK2M4dkdDcYOX6kyNZL7XiJRmLfUR2cqda3P3bQxapkdfIOWfPQQJUAnYlVvUaIShoBxYw5HXRTr2jR5UAklfIRWZOmx07WKC6dZG5MIm1Luun5KgvqQmzQ9ErL5tz/Oi5pPdK30kdkS5WdeWD6KwL78Ff4KfC0DVTO0zb/C7WyKk4ZLu+UKCLHXDTzE4lhBAu6mSUfJ5nQhmdLdKg6Gvh1St/vRcsDJOZqEFBVn35/oK974l root@ADS_4300N_BRN000EC691D285"
];
paperless = {
group = "paperless";
uid = config.ids.uids.paperless;
home = "/var/empty";
};
}
];
users.groups =
{
paperless.gid = config.ids.gids.paperless;
}
// lib.mapAttrs (_: cfg: {gid = cfg.id;}) (smbUsers // smbGroups);
backups.storageBoxes.dusk = {
subuser = "samba";
paths = ["/bunker"];
};
}