-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_smb4_conf.py
executable file
·1653 lines (1301 loc) · 47.6 KB
/
generate_smb4_conf.py
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/local/bin/python
from middlewared.client import Client
from middlewared.client.utils import Struct
import os
import pwd
import re
import sys
import socket
import subprocess
import tempfile
import time
import logging
import logging.config
from dns import resolver
sys.path.extend([
'/usr/local/www',
'/usr/local/www/freenasUI'
])
logging.config.dictConfig({
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'simple': {
'format': '[%(name)s:%(lineno)s] %(message)s'
},
},
'handlers': {
'syslog': {
'level': 'DEBUG',
'class': 'logging.handlers.SysLogHandler',
'formatter': 'simple',
}
},
'loggers': {
'': {
'handlers': ['syslog'],
'level': 'DEBUG',
'propagate': True,
},
}
})
from freenasUI.common.pipesubr import pipeopen
from freenasUI.common.log import log_traceback
from freenasUI.common.freenassysctl import freenas_sysctl as fs
log = logging.getLogger('generate_smb4_conf')
def qw(w):
return '"%s"' % w.replace('"', '\\"')
def get_server_branding(client)
server_branding = "FREENAS"
if not client.call('notifier.is_freenas'):
failover_status = client.call('notifier.failover_status')
server_branding = "TRUENAS_" + failover_status
return server_branding
def debug_SID(str):
if str:
print("XXX: %s" % str, file=sys.stderr)
p = pipeopen("/usr/local/bin/net -d 0 getlocalsid")
out, _ = p.communicate()
if out:
print("XXX: %s" % out, file=sys.stderr)
def smb4_get_system_SID():
SID = None
p = pipeopen("/usr/local/bin/net -d 0 getlocalsid")
net_out = p.communicate()
if p.returncode != 0:
return None
if not net_out:
return None
net_out = net_out[0]
parts = net_out.split()
try:
SID = parts[5]
except Exception as e:
log.debug(
'The following exception occured while trying to obtain system SID: {0}'.format(e)
)
log_traceback(log=log)
SID = None
return SID
def smb4_get_domain_SID():
SID = None
p = pipeopen("/usr/local/bin/net -d 0 getdomainsid")
net_out = p.communicate()
if p.returncode != 0:
return None
if not net_out:
return None
net_out = net_out[0]
parts = net_out.split()
try:
SID = parts[5]
except Exception as e:
log.debug(
'The following exception occured while trying to obtain system SID: {0}'.format(e)
)
log_traceback(log=log)
SID = None
return SID
def smb4_get_database_SID(client):
SID = None
try:
cifs = Struct(client.call('datastore.query', 'services.cifs', None, {'get': True}))
if cifs:
SID = cifs.cifs_SID
except Exception as e:
log.debug(
'The following exception occured while trying to obtain database SID: {0}'.format(e)
)
log_traceback(log=log)
SID = None
return SID
def smb4_set_system_SID(SID):
if not SID:
return False
p = pipeopen("/usr/local/bin/net -d 0 setlocalsid %s" % SID)
net_out = p.communicate()
if p.returncode != 0:
log.error('Failed to setlocalsid with the following error: {0}'.format(net_out[1]))
return False
if not net_out:
return False
return True
def smb4_set_domain_SID(SID):
if not SID:
return False
p = pipeopen("/usr/local/bin/net -d 0 setdomainsid %s" % SID)
net_out = p.communicate()
if p.returncode != 0:
log.error('Failed to setlocalsid with the following error: {0}'.format(net_out[1]))
return False
if not net_out:
return False
return True
def smb4_set_database_SID(client, SID):
ret = False
if not SID:
return ret
try:
cifs = Struct(client.call('datastore.query', 'services.cifs', None, {'get': True}))
cifs.cifs_SID = SID
cifs.save()
ret = True
except Exception as e:
log.debug(
'The following exception occured while trying to set database SID: {0}'.format(e)
)
log_traceback(log=log)
ret = False
return ret
def smb4_set_SID(client, role):
get_sid_func = smb4_get_system_SID
set_sid_func = smb4_set_system_SID
if role == 'dc':
get_sid_func = smb4_get_domain_SID
set_sid_func = smb4_set_domain_SID
database_SID = smb4_get_database_SID(client)
system_SID = get_sid_func()
if database_SID:
if not system_SID:
if not set_sid_func(database_SID):
print("Unable to set SID to %s" % database_SID, file=sys.stderr)
else:
if database_SID != system_SID:
if not set_sid_func(database_SID):
print(("Unable to set SID to "
"%s" % database_SID), file=sys.stderr)
else:
if not system_SID:
print(("Unable to figure out SID, things are "
"seriously jacked!"), file=sys.stderr)
if not set_sid_func(system_SID):
print("Unable to set SID to %s" % system_SID, file=sys.stderr)
else:
smb4_set_database_SID(client, system_SID)
def smb4_ldap_enabled(client):
ret = False
if client.call('notifier.common', 'system', 'ldap_enabled') and client.call('notifier.common', 'system', 'ldap_has_samba_schema'):
ret = True
return ret
def smb4_activedirectory_enabled(client):
ret = False
if client.call('notifier.common', 'system', 'activedirectory_enabled'):
ret = True
return ret
def smb4_autorid_enabled(client):
ret = False
try:
ad = Struct(client.call('datastore.query', 'directoryservice.ActiveDirectory', None, {'get': True}))
except:
return ret
if ad.ad_idmap_backend.lower() == "autorid":
ret = True
return ret
def config_share_for_nfs4(share):
confset1(share, "nfs4:mode = special")
confset1(share, "nfs4:acedup = merge")
confset1(share, "nfs4:chown = true")
def config_share_for_zfs(share):
confset1(share, "zfsacl:acesort = dontcare")
#
# ticket: # 16325
# aio_pthread needs to be last
# fruit needs to be before streams_xattr, streams_xattr is required
# for fruit, and if catia and fruit are used, catia comes before fruit
#
def order_vfs_objects(vfs_objects):
vfs_objects_special = ('catia', 'fruit', 'streams_xattr', 'recycle', 'aio_pthread')
vfs_objects_ordered = []
if 'fruit' in vfs_objects:
if 'streams_xattr' not in vfs_objects:
vfs_objects.append('streams_xattr')
for obj in vfs_objects:
if obj not in vfs_objects_special:
vfs_objects_ordered.append(obj)
for obj in vfs_objects_special:
if obj in vfs_objects:
vfs_objects_ordered.append(obj)
return vfs_objects_ordered
def config_share_for_vfs_objects(share, vfs_objects):
if vfs_objects:
vfs_objects = order_vfs_objects(vfs_objects)
confset2(share, "vfs objects = %s", ' '.join(vfs_objects))
def extend_vfs_objects_for_zfs(path, vfs_objects):
return
if is_within_zfs(path):
vfs_objects.extend([
'zfs_space',
'zfsacl',
])
def is_within_zfs(mountpoint):
try:
st = os.stat(mountpoint)
except:
return False
share_dev = st.st_dev
p = pipeopen("mount")
mount_out = p.communicate()
if p.returncode != 0:
return False
if mount_out:
mount_out = mount_out[0]
zfs_regex = re.compile("^(.*) on (/.*) \(zfs, .*\)$")
# The reversed is important as we would like the code to use
# the most specific (and therefore relevant) mount point.
for line in reversed(mount_out.split('\n')):
match = zfs_regex.match(line.strip())
if not match:
continue
mp = match.group(2)
try:
st = os.stat(mp)
except:
continue
if st.st_dev == share_dev:
return True
return False
def get_sysctl(name):
p = pipeopen("/sbin/sysctl -n '%s'" % name)
out = p.communicate()
if p.returncode != 0:
return None
try:
out = out[0].strip()
except:
pass
return out
def get_server_services():
server_services = [
'rpc', 'nbt', 'wrepl', 'ldap', 'cldap', 'kdc', 'drepl', 'winbind',
'ntp_signd', 'kcc', 'dnsupdate', 'dns', 'smb'
]
return server_services
def get_dcerpc_endpoint_servers():
dcerpc_endpoint_servers = [
'epmapper', 'wkssvc', 'rpcecho', 'samr', 'netlogon', 'lsarpc',
'spoolss', 'drsuapi', 'dssetup', 'unixinfo', 'browser', 'eventlog6',
'backupkey', 'dnsserver', 'winreg', 'srvsvc'
]
return dcerpc_endpoint_servers
def get_server_role(client):
role = "standalone"
if client.call('notifier.common', 'system', 'activedirectory_enabled') or smb4_ldap_enabled(client):
role = "member"
if client.call('notifier.common', 'system', 'domaincontroller_enabled'):
try:
role = client.call('datastore.query', 'services.DomainController', None, {'get': True})['dc_role']
except:
pass
return role
def get_cifs_homedir(client):
cifs_homedir = "/home"
shares = client.call('datastore.query', 'sharing.CIFS_Share')
if len(shares) == 0:
return
for share in shares:
share = Struct(share)
if share.cifs_home and share.cifs_path:
cifs_homedir = share.cifs_path
break
return cifs_homedir
def confset1(conf, line, space=4):
if line:
conf.append(' ' * space + line)
def confset2(conf, line, var, space=4):
if line and var:
line = ' ' * space + line
conf.append(line % var)
def configure_idmap_ad(smb4_conf, idmap, domain):
confset1(smb4_conf, "idmap config %s: backend = %s" % (
domain,
idmap.idmap_backend_name
))
confset1(smb4_conf, "idmap config %s: range = %d-%d" % (
domain,
idmap.idmap_ad_range_low,
idmap.idmap_ad_range_high
))
confset1(smb4_conf, "idmap config %s: schema mode = %s" % (
domain,
idmap.idmap_ad_schema_mode
))
def configure_idmap_adex(smb4_conf, idmap, domain):
confset1(smb4_conf, "idmap backend = adex")
confset1(smb4_conf, "idmap uid = %d-%d" % (
domain,
idmap.idmap_adex_range_low,
idmap.idmap_adex_range_high
))
confset1(smb4_conf, "idmap gid = %d-%d" % (
domain,
idmap.idmap_adex_range_low,
idmap.idmap_adex_range_high
))
def configure_idmap_autorid(smb4_conf, idmap, domain):
confset1(smb4_conf, "idmap config %s: backend = %s" % (
"*",
idmap.idmap_backend_name
))
confset1(smb4_conf, "idmap config %s: range = %d-%d" % (
"*",
idmap.idmap_autorid_range_low,
idmap.idmap_autorid_range_high
))
confset1(smb4_conf, "idmap config %s: rangesize = %d" % (
"*",
idmap.idmap_autorid_rangesize
))
confset1(smb4_conf, "idmap config %s: read only = %s" % (
"*",
"yes" if idmap.idmap_autorid_readonly else "no"
))
confset1(smb4_conf, "idmap config %s: ignore builtin = %s" % (
"*",
"yes" if idmap.idmap_autorid_ignore_builtin else "no"
))
def configure_idmap_fruit(smb4_conf, idmap, domain):
confset1(smb4_conf, "idmap config %s: backend = %s" % (
domain,
idmap.idmap_backend_name
))
confset1(smb4_conf, "idmap config %s: range = %d-%d" % (
domain,
idmap.idmap_fruit_range_low,
idmap.idmap_fruit_range_high
))
def configure_idmap_hash(smb4_conf, idmap, domain):
confset1(smb4_conf, "idmap config %s: backend = %s" % (
domain,
idmap.idmap_backend_name
))
confset1(smb4_conf, "idmap config %s: range = %d-%d" % (
domain,
idmap.idmap_hash_range_low,
idmap.idmap_hash_range_high
))
confset1(smb4_conf, "idmap_hash: name_map = %s" %
idmap.idmap_hash_range_name_map)
def configure_idmap_ldap(smb4_conf, idmap, domain):
confset1(smb4_conf, "idmap config %s: backend = %s" % (
domain,
idmap.idmap_backend_name
))
confset1(smb4_conf, "idmap config %s: range = %d-%d" % (
domain,
idmap.idmap_ldap_range_low,
idmap.idmap_ldap_range_high
))
if idmap.idmap_ldap_ldap_base_dn:
confset1(smb4_conf, "idmap config %s: ldap base dn = %s" % (
domain,
idmap.idmap_ldap_ldap_base_dn
))
if idmap.idmap_ldap_ldap_user_dn:
confset1(smb4_conf, "idmap config %s: ldap user dn = %s" % (
domain,
idmap.idmap_ldap_ldap_user_dn
))
if idmap.idmap_ldap_ldap_url:
confset1(smb4_conf, "idmap config %s: ldap url = %s" % (
domain,
idmap.idmap_ldap_ldap_url
))
def configure_idmap_nss(smb4_conf, idmap, domain):
confset1(smb4_conf, "idmap config %s: backend = %s" % (
domain,
idmap.idmap_backend_name
))
confset1(smb4_conf, "idmap config %s: range = %d-%d" % (
domain,
idmap.idmap_nss_range_low,
idmap.idmap_nss_range_high
))
def configure_idmap_rfc2307(smb4_conf, idmap, domain):
confset1(smb4_conf, "idmap config %s: backend = %s" % (
domain,
idmap.idmap_backend_name
))
confset1(smb4_conf, "idmap config %s: range = %d-%d" % (
domain,
idmap.idmap_rfc2307_range_low,
idmap.idmap_rfc2307_range_high
))
confset1(smb4_conf, "idmap config %s: ldap_server = %s" % (
domain,
idmap.idmap_rfc2307_ldap_server
))
confset1(smb4_conf, "idmap config %s: bind_path_user = %s" % (
domain,
idmap.idmap_rfc2307_bind_path_user
))
confset1(smb4_conf, "idmap config %s: bind_path_group = %s" % (
domain,
idmap.idmap_rfc2307_bind_path_group
))
confset1(smb4_conf, "idmap config %s: user_cn = %s" % (
domain,
"yes" if idmap.idmap_rfc2307_user_cn else "no"
))
confset1(smb4_conf, "idmap config %s: cn_realm = %s" % (
domain,
"yes" if idmap.idmap_rfc2307_cn_realm else "no"
))
if idmap.idmap_rfc2307_ldap_domain:
confset1(smb4_conf, "idmap config %s: ldap_domain = %s" % (
domain,
idmap.idmap_rfc2307_ldap_domain
))
if idmap.idmap_rfc2307_ldap_url:
confset1(smb4_conf, "idmap config %s: ldap_url = %s" % (
domain,
idmap.idmap_rfc2307_ldap_url
))
if idmap.idmap_rfc2307_ldap_user_dn:
confset1(smb4_conf, "idmap config %s: ldap_user_dn = %s" % (
domain,
idmap.idmap_rfc2307_ldap_user_dn
))
if idmap.idmap_rfc2307_ldap_realm:
confset1(smb4_conf, "idmap config %s: ldap_realm = %s" % (
domain,
idmap.idmap_rfc2307_ldap_realm
))
def idmap_backend_rfc2307(client):
try:
ad = Struct(client.call('datastore.query', 'directoryservice.ActiveDirectory', None, {'get': True}))
except:
return False
return ad.ad_idmap_backend == 'rfc2307'
def set_idmap_rfc2307_secret(client):
try:
ad = Struct(client.call('datastore.query', 'directoryservice.ActiveDirectory', None, {'get': True}))
ad.ds_type = 1 # FIXME: DS_TYPE_ACTIVEDIRECTORY = 1
except:
return False
domain = None
# FIXME: ad ds_type, extend model
idmap = Struct(client.call('notifier.ds_get_idmap_object', ad.ds_type, ad.id, ad.ad_idmap_backend))
try:
fad = Struct(client.call('notifier.directoryservice', 'AD'))
domain = fad.netbiosname.upper()
except:
return False
args = [
"/usr/local/bin/net",
"-d 0",
"idmap",
"secret"
]
net_cmd = "%s '%s' '%s'" % (
' '.join(args),
domain,
idmap.idmap_rfc2307_ldap_user_dn_password
)
p = pipeopen(net_cmd, quiet=True)
net_out = p.communicate()
if net_out and net_out[0]:
for line in net_out[0].split('\n'):
if not line:
continue
print(line)
ret = True
if p.returncode != 0:
print("Failed to set idmap secret!", file=sys.stderr)
ret = False
return ret
def configure_idmap_rid(smb4_conf, idmap, domain):
confset1(smb4_conf, "idmap config %s: backend = %s" % (
domain,
idmap.idmap_backend_name
))
confset1(smb4_conf, "idmap config %s: range = %d-%d" % (
domain,
idmap.idmap_rid_range_low,
idmap.idmap_rid_range_high
))
def configure_idmap_tdb(smb4_conf, idmap, domain):
confset1(smb4_conf, "idmap config %s: backend = %s" % (
domain,
idmap.idmap_backend_name
))
confset1(smb4_conf, "idmap config %s: range = %d-%d" % (
domain,
idmap.idmap_tdb_range_low,
idmap.idmap_tdb_range_high
))
def configure_idmap_tdb2(smb4_conf, idmap, domain):
confset1(smb4_conf, "idmap config %s: backend = %s" % (
domain,
idmap.idmap_backend_name
))
confset1(smb4_conf, "idmap config %s: range = %d-%d" % (
domain,
idmap.idmap_tdb2_range_low,
idmap.idmap_tdb2_range_high
))
confset1(smb4_conf, "idmap config %s: script = %s" % (
domain,
idmap.idmap_tdb2_script
))
def configure_idmap_script(smb4_conf, idmap, domain):
confset1(smb4_conf, "idmap config %s: backend = %s" % (
domain,
idmap.idmap_backend_name
))
confset1(smb4_conf, "idmap config %s: range = %d-%d" % (
domain,
idmap.idmap_script_range_low,
idmap.idmap_script_range_high
))
confset1(smb4_conf, "idmap config %s: script = %s" % (
domain,
idmap.idmap_script_script
))
IDMAP_FUNCTIONS = {
'IDMAP_TYPE_AD': configure_idmap_ad,
'IDMAP_TYPE_ADEX': configure_idmap_ad,
'IDMAP_TYPE_AUTORID': configure_idmap_autorid,
'IDMAP_TYPE_FRUIT': configure_idmap_fruit,
'IDMAP_TYPE_HASH': configure_idmap_hash,
'IDMAP_TYPE_LDAP': configure_idmap_ldap,
'IDMAP_TYPE_NSS': configure_idmap_nss,
'IDMAP_TYPE_RFC2307': configure_idmap_rfc2307,
'IDMAP_TYPE_RID': configure_idmap_rid,
'IDMAP_TYPE_TDB': configure_idmap_tdb,
'IDMAP_TYPE_TDB2': configure_idmap_tdb2,
'IDMAP_TYPE_SCRIPT': configure_idmap_script
}
def configure_idmap_backend(client, smb4_conf, idmap, domain):
if not domain:
domain = "*"
try:
idmap_str = client.call('notifier.ds_idmap_type_code_to_string', idmap.idmap_backend_type)
IDMAP_FUNCTIONS[idmap_str](smb4_conf, idmap, domain)
except:
log.warn('Failed to configure idmap', exc_info=True)
pass
def set_ldap_password(client):
try:
ldap = Struct(client.call('datastore.query', 'directoryservice.LDAP', None, {'get': True}))
except:
return
if ldap.ldap_bindpw:
p = pipeopen("/usr/local/bin/smbpasswd -w '%s'" % (
ldap.ldap_bindpw,
), quiet=True)
out = p.communicate()
if out and out[1]:
for line in out[1].split('\n'):
if not line:
continue
print(line)
def add_ldap_conf(client, smb4_conf):
try:
ldap = Struct(client.call('datastore.query', 'directoryservice.LDAP', None, {'get': True}))
ldap.ds_type = 2 # FIXME: DS_TYPE_LDAP = 2
cifs = Struct(client.call('smb.config'))
except:
return
confset1(smb4_conf, "security = user")
confset1(
smb4_conf,
"passdb backend = ldapsam:%s://%s" % (
"ldaps" if ldap.ldap_ssl == 'on' else "ldap",
ldap.ldap_hostname
)
)
ldap_workgroup = cifs.workgroup.upper()
confset2(smb4_conf, "ldap admin dn = %s", ldap.ldap_binddn)
confset2(smb4_conf, "ldap suffix = %s", ldap.ldap_basedn)
confset2(smb4_conf, "ldap user suffix = %s", ldap.ldap_usersuffix)
confset2(smb4_conf, "ldap group suffix = %s", ldap.ldap_groupsuffix)
confset2(smb4_conf, "ldap machine suffix = %s", ldap.ldap_machinesuffix)
confset2(
smb4_conf,
"ldap ssl = %s",
"start tls" if (ldap.ldap_ssl == 'start_tls') else 'off'
)
confset1(smb4_conf, "ldap replication sleep = 1000")
confset1(smb4_conf, "ldap passwd sync = yes")
confset1(smb4_conf, "ldapsam:trusted = yes")
confset2(smb4_conf, "workgroup = %s", ldap_workgroup)
confset1(smb4_conf, "domain logons = yes")
idmap = Struct(client.call('notifier.ds_get_idmap_object', ldap.ds_type, ldap.id, ldap.ldap_idmap_backend))
configure_idmap_backend(client, smb4_conf, idmap, ldap_workgroup)
def add_activedirectory_conf(client, smb4_conf):
try:
ad = Struct(client.call('datastore.query', 'directoryservice.ActiveDirectory', None, {'get': True}))
ad.ds_type = 1 # FIXME: DS_TYPE_ACTIVEDIRECTORY = 1
except:
return
try:
os.makedirs(cachedir)
os.chmod(cachedir, 0o755)
except:
pass
ad_workgroup = None
try:
fad = Struct(client.call('notifier.directoryservice', 'AD'))
ad_workgroup = fad.netbiosname.upper()
except:
return
confset2(smb4_conf, "workgroup = %s", ad_workgroup)
confset2(smb4_conf, "realm = %s", ad.ad_domainname.upper())
confset1(smb4_conf, "security = ADS")
confset1(smb4_conf, "client use spnego = yes")
confset1(smb4_conf, "local master = no")
confset1(smb4_conf, "domain master = no")
confset1(smb4_conf, "preferred master = no")
confset2(smb4_conf, "ads dns update = %s",
"yes" if ad.ad_allow_dns_updates else "no")
confset1(smb4_conf, "winbind cache time = 7200")
confset1(smb4_conf, "winbind offline logon = yes")
confset1(smb4_conf, "winbind enum users = yes")
confset1(smb4_conf, "winbind enum groups = yes")
confset1(smb4_conf, "winbind nested groups = yes")
confset2(smb4_conf, "winbind use default domain = %s",
"yes" if ad.ad_use_default_domain else "no")
confset1(smb4_conf, "winbind refresh tickets = yes")
if ad.ad_nss_info:
confset2(smb4_conf, "winbind nss info = %s", ad.ad_nss_info)
idmap = Struct(client.call('notifier.ds_get_idmap_object', ad.ds_type, ad.id, ad.ad_idmap_backend))
configure_idmap_backend(client, smb4_conf, idmap, ad_workgroup)
confset2(smb4_conf, "allow trusted domains = %s",
"yes" if ad.ad_allow_trusted_doms else "no")
confset2(smb4_conf, "client ldap sasl wrapping = %s",
ad.ad_ldap_sasl_wrapping)
confset1(smb4_conf, "template shell = /bin/sh")
cifs_homedir = "%s/%%D/%%U" % get_cifs_homedir(client)
confset2(smb4_conf, "template homedir = %s", cifs_homedir)
def add_domaincontroller_conf(client, smb4_conf):
try:
dc = Struct(client.call('datastore.query', 'services.DomainController', None, {'get': True}))
cifs = Struct(client.call('smb.config'))
except:
return
# server_services = get_server_services()
# dcerpc_endpoint_servers = get_dcerpc_endpoint_servers()
confset2(smb4_conf, "netbios name = %s", cifs.netbiosname.upper())
if cifs.netbiosalias:
confset2(smb4_conf, "netbios aliases = %s", cifs.netbiosalias.upper())
confset2(smb4_conf, "workgroup = %s", dc.dc_domain.upper())
confset2(smb4_conf, "realm = %s", dc.dc_realm)
confset2(smb4_conf, "dns forwarder = %s", dc.dc_dns_forwarder)
confset1(smb4_conf, "idmap_ldb:use rfc2307 = yes")
# confset2(smb4_conf, "server services = %s",
# string.join(server_services, ',').rstrip(','))
# confset2(smb4_conf, "dcerpc endpoint servers = %s",
# string.join(dcerpc_endpoint_servers, ',').rstrip(','))
ipv4_addrs = []
if cifs.bindip:
for i in cifs.bindip:
try:
socket.inet_aton(i)
ipv4_addrs.append(i)
except:
pass
else:
interfaces = client.call('notifier.choices', 'IPChoices', [True, False])
for i in interfaces:
try:
socket.inet_aton(i[0])
ipv4_addrs.append(i[0])
except:
pass
with open("/usr/local/etc/lmhosts", "w") as f:
for ipv4 in ipv4_addrs:
f.write("%s\t%s\n" % (ipv4, dc.dc_domain.upper()))
def get_smb4_users(client):
return client.call('datastore.query', 'account.bsdusers', [
['OR', [
('bsdusr_smbhash', '~', r'^.+:.+:[X]{32}:.+$'),
('bsdusr_smbhash', '~', r'^.+:.+:[A-F0-9]{32}:.+$'),
]],
])
def get_disabled_users(client):
# XXX: WTF moment, this method is not used
disabled_users = []
try:
# FIXME: test query and support for OR
users = client.call('datastore.query', 'account.bsdusers', (
('bsdusr_smbhash', '~', r'^.+:.+:XXXX.+$'),
(
'OR',
('bsdusr_locked', '=', True),
('bsdusr_password_disabled', '=', True),
),
))
for u in users:
disabled_users.append(u)
except:
disabled_users = []
return disabled_users
def generate_smb4_tdb(client, smb4_tdb):
try:
users = get_smb4_users(client)
for u in users:
smb4_tdb.append(u['bsdusr_smbhash'])
except:
return
def generate_smb4_conf(client, smb4_conf, role):
cifs = Struct(client.call('smb.config'))
if not cifs.guest:
cifs.guest = 'ftp'
if not cifs.filemask:
cifs.filemask = "0666"
if not cifs.dirmask:
cifs.dirmask = "0777"
# standard stuff... should probably do this differently
confset1(smb4_conf, "[global]", space=0)
if os.path.exists("/usr/local/etc/smbusers"):
confset1(smb4_conf, "username map = /usr/local/etc/smbusers")
server_min_protocol = fs().services.smb.config.server_min_protocol
if server_min_protocol != 'NONE':
confset2(smb4_conf, "server min protocol = %s", server_min_protocol)
server_max_protocol = fs().services.smb.config.server_max_protocol
if server_max_protocol != 'NONE':
confset2(smb4_conf, "server max protocol = %s", server_max_protocol)
if cifs.bindip:
interfaces = []
bindips = ' '.join(cifs.bindip)
if role != 'dc':
bindips = "127.0.0.1 %s" % bindips
bindips = bindips.split()
for bindip in bindips:
if not bindip:
continue
bindip = bindip.strip()
iface = client.call('notifier.get_interface', bindip)
is_carp_interface = False
if iface:
try:
is_carp_interface = client.call('notifier.is_carp_interface', iface)
except:
pass
if iface and is_carp_interface:
parent_iface = client.call('notifier.get_parent_interface', iface)
if not parent_iface:
continue
parent_iinfo = client.call('notifier.get_interface_info', parent_iface[0])
if not parent_iinfo:
continue
interfaces.append("%s/%s" % (bindip, parent_iface[2]))
else:
interfaces.append(bindip)
if interfaces:
confset2(smb4_conf, "interfaces = %s", ' '.join(interfaces))
confset1(smb4_conf, "bind interfaces only = yes")
confset1(smb4_conf, "encrypt passwords = yes")
confset1(smb4_conf, "dns proxy = no")
confset1(smb4_conf, "strict locking = no")
confset1(smb4_conf, "oplocks = yes")
confset1(smb4_conf, "deadtime = 15")
confset1(smb4_conf, "max log size = 51200")
confset2(smb4_conf, "max open files = %d",
int(get_sysctl('kern.maxfilesperproc')) - 25)