/
services.inc
3256 lines (2828 loc) · 102 KB
/
services.inc
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
<?php
/*
* services.inc
*
* part of pfSense (https://www.pfsense.org)
* Copyright (c) 2004-2013 BSD Perimeter
* Copyright (c) 2013-2016 Electric Sheep Fencing
* Copyright (c) 2014-2021 Rubicon Communications, LLC (Netgate)
* All rights reserved.
*
* originally part of m0n0wall (http://m0n0.ch/wall)
* Copyright (c) 2003-2004 Manuel Kasper <mk@neon1.net>.
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
define('DYNDNS_PROVIDER_VALUES', 'all-inkl azure azurev6 citynetwork cloudflare cloudflare-v6 cloudns custom custom-v6 desec desec-v6 digitalocean digitalocean-v6 dnsexit dnsimple dnsmadeeasy dnsomatic domeneshop domeneshop-v6 dreamhost dreamhost-v6 duiadns duiadns-v6 dyfi dyndns dyndns-custom dyndns-static dyns dynv6 dynv6-v6 easydns easydns-v6 eurodns freedns freedns-v6 freedns2 freedns2-v6 glesys gandi-livedns gandi-livedns-v6 godaddy godaddy-v6 googledomains gratisdns he-net he-net-v6 he-net-tunnelbroker hover linode linode-v6 loopia mythicbeasts mythicbeasts-v6 namecheap nicru nicru-v6 noip noip-v6 noip-free noip-free-v6 onecom onecom-v6 ods opendns ovh-dynhost route53 route53-v6 selfhost spdyn spdyn-v6 strato yandex yandex-v6 zoneedit');
define('DYNDNS_PROVIDER_DESCRIPTIONS', 'All-Inkl.com,Azure DNS,Azure DNS (v6),City Network,Cloudflare,Cloudflare (v6),ClouDNS,Custom,Custom (v6),deSEC,deSEC (v6),DigitalOcean,DigitalOcean (v6),DNSexit,DNSimple,DNS Made Easy,DNS-O-Matic,Domeneshop,Domeneshop (v6),DreamHost,Dreamhost (v6),DuiaDns.net,DuiaDns.net (v6),DY.fi,DynDNS (dynamic),DynDNS (custom),DynDNS (static),DyNS,Dynv6,Dynv6 (v6),easyDNS,easyDNS (v6),Euro Dns,freeDNS,freeDNS (v6),freeDNS API Version 2, freeDNS API Version 2 (v6),GleSYS,Gandi Live DNS,Gandi Live DNS (v6),GoDaddy,GoDaddy (v6),Google Domains,GratisDNS,HE.net,HE.net (v6),HE.net Tunnelbroker,Hover,Linode,Linode (v6),Loopia,Mythic Beasts,Mythic Beasts (v6),Namecheap,NIC.RU,NIC.RU (v6),No-IP,No-IP (v6),No-IP (free),No-IP (free-v6),One.com,One.com (v6),ODS.org,OpenDNS,OVH DynHOST,Route 53,Route 53 (v6),SelfHost,SPDYN,SPDYN (v6),Strato,Yandex,Yandex (v6),ZoneEdit');
/* implement ipv6 route advertising daemon */
function services_radvd_configure($blacklist = array()) {
global $config, $g;
if (isset($config['system']['developerspew'])) {
$mt = microtime();
echo "services_radvd_configure() being called $mt\n";
}
if (!is_array($config['dhcpdv6'])) {
$config['dhcpdv6'] = array();
}
$Iflist = get_configured_interface_list();
$Iflist = array_merge($Iflist, get_configured_pppoe_server_interfaces());
$radvdconf = "# Automatically Generated, do not edit\n";
/* Process all links which need the router advertise daemon */
$radvdifs = array();
/* handle manually configured DHCP6 server settings first */
foreach ($config['dhcpdv6'] as $dhcpv6if => $dhcpv6ifconf) {
if (!is_array($config['interfaces'][$dhcpv6if])) {
continue;
}
if (!isset($config['interfaces'][$dhcpv6if]['enable'])) {
continue;
}
/* Do not put in the config an interface which is down */
if (isset($blacklist[$dhcpv6if])) {
continue;
}
if (!isset($dhcpv6ifconf['ramode'])) {
$dhcpv6ifconf['ramode'] = $dhcpv6ifconf['mode'];
}
/* are router advertisements enabled? */
if ($dhcpv6ifconf['ramode'] == "disabled") {
continue;
}
if (!isset($dhcpv6ifconf['rapriority'])) {
$dhcpv6ifconf['rapriority'] = "medium";
}
$racarpif = false;
$rasrcaddr = "";
/* check if binding to CARP IP */
if (!empty($dhcpv6ifconf['rainterface'])) {
if (strstr($dhcpv6ifconf['rainterface'], "_vip")) {
if (is_linklocal(get_configured_vip_ipv6($dhcpv6ifconf['rainterface']))) {
$rasrcaddr = get_configured_vip_ipv6($dhcpv6ifconf['rainterface']);
}
elseif (get_carp_interface_status($dhcpv6ifconf['rainterface']) == "MASTER") {
$dhcpv6if = $dhcpv6ifconf['rainterface'];
$racarpif = true;
} else {
continue;
}
}
}
$realif = get_real_interface($dhcpv6if, "inet6");
if (isset($radvdifs[$realif])) {
continue;
}
$ifcfgipv6 = get_interface_ipv6($dhcpv6if);
if (!is_ipaddrv6($ifcfgipv6)) {
continue;
}
$ifcfgsnv6 = get_interface_subnetv6($dhcpv6if);
$subnetv6 = gen_subnetv6($ifcfgipv6, $ifcfgsnv6);
if (!is_subnetv6($subnetv6 . "/" . $ifcfgsnv6)) {
log_error("radvd: skipping configuration for interface $dhcpv6if because its subnet or prefix length is invalid.");
continue;
}
$radvdifs[$realif] = $realif;
$radvdconf .= "# Generated for DHCPv6 Server $dhcpv6if\n";
$radvdconf .= "interface {$realif} {\n";
if (strstr($realif, "ovpn")) {
$radvdconf .= "\tUnicastOnly on;\n";
}
$radvdconf .= "\tAdvSendAdvert on;\n";
if ($rasrcaddr) {
$radvdconf .= "\tAdvRASrcAddress {\n";
$radvdconf .= "\t\t{$rasrcaddr};\n";
$radvdconf .= "\t};\n";
}
if (is_numericint($dhcpv6ifconf['raminrtradvinterval'])) {
$radvdconf .= "\tMinRtrAdvInterval {$dhcpv6ifconf['raminrtradvinterval']};\n";
} else {
$radvdconf .= "\tMinRtrAdvInterval 5;\n";
}
if (is_numericint($dhcpv6ifconf['ramaxrtradvinterval'])) {
$ramaxrtradvinterval = $dhcpv6ifconf['ramaxrtradvinterval'];
} else {
$ramaxrtradvinterval = 20;
}
$radvdconf .= "\tMaxRtrAdvInterval {$ramaxrtradvinterval};\n";
if (is_numericint($dhcpv6ifconf['raadvdefaultlifetime'])) {
$raadvdefaultlifetime = $dhcpv6ifconf['raadvdefaultlifetime'];
} else {
$raadvdefaultlifetime = $ramaxrtradvinterval * 3;
}
$radvdconf .= "\tAdvDefaultLifetime {$raadvdefaultlifetime};\n";
$mtu = get_interface_mtu($realif);
if (is_numeric($mtu)) {
$radvdconf .= "\tAdvLinkMTU {$mtu};\n";
} else {
$radvdconf .= "\tAdvLinkMTU 1280;\n";
}
switch ($dhcpv6ifconf['rapriority']) {
case "low":
$radvdconf .= "\tAdvDefaultPreference low;\n";
break;
case "high":
$radvdconf .= "\tAdvDefaultPreference high;\n";
break;
default:
$radvdconf .= "\tAdvDefaultPreference medium;\n";
break;
}
switch ($dhcpv6ifconf['ramode']) {
case "managed":
case "assist":
$radvdconf .= "\tAdvManagedFlag on;\n";
$radvdconf .= "\tAdvOtherConfigFlag on;\n";
break;
case "stateless_dhcp":
$radvdconf .= "\tAdvManagedFlag off;\n";
$radvdconf .= "\tAdvOtherConfigFlag on;\n";
break;
}
$radvdconf .= "\tprefix {$subnetv6}/{$ifcfgsnv6} {\n";
if ($racarpif == true || $rasrcaddr) {
$radvdconf .= "\t\tDeprecatePrefix off;\n";
} else {
$radvdconf .= "\t\tDeprecatePrefix on;\n";
}
switch ($dhcpv6ifconf['ramode']) {
case "managed":
$radvdconf .= "\t\tAdvOnLink on;\n";
$radvdconf .= "\t\tAdvAutonomous off;\n";
break;
case "router":
$radvdconf .= "\t\tAdvOnLink off;\n";
$radvdconf .= "\t\tAdvAutonomous off;\n";
break;
case "stateless_dhcp":
case "assist":
$radvdconf .= "\t\tAdvOnLink on;\n";
$radvdconf .= "\t\tAdvAutonomous on;\n";
break;
case "unmanaged":
$radvdconf .= "\t\tAdvOnLink on;\n";
$radvdconf .= "\t\tAdvAutonomous on;\n";
break;
}
if (is_numericint($dhcpv6ifconf['ravalidlifetime'])) {
$radvdconf .= "\t\tAdvValidLifetime {$dhcpv6ifconf['ravalidlifetime']};\n";
} else {
$radvdconf .= "\t\tAdvValidLifetime 86400;\n";
}
if (is_numericint($dhcpv6ifconf['rapreferredlifetime'])) {
$radvdconf .= "\t\tAdvPreferredLifetime {$dhcpv6ifconf['rapreferredlifetime']};\n";
} else {
$radvdconf .= "\t\tAdvPreferredLifetime 14400;\n";
}
$radvdconf .= "\t};\n";
if (is_array($dhcpv6ifconf['subnets']['item'])) {
foreach ($dhcpv6ifconf['subnets']['item'] as $subnet) {
if (is_subnetv6($subnet)) {
$radvdconf .= "\tprefix {$subnet} {\n";
$radvdconf .= "\t\tDeprecatePrefix on;\n";
switch ($dhcpv6ifconf['ramode']) {
case "managed":
$radvdconf .= "\t\tAdvOnLink on;\n";
$radvdconf .= "\t\tAdvAutonomous off;\n";
break;
case "router":
$radvdconf .= "\t\tAdvOnLink off;\n";
$radvdconf .= "\t\tAdvAutonomous off;\n";
break;
case "assist":
$radvdconf .= "\t\tAdvOnLink on;\n";
$radvdconf .= "\t\tAdvAutonomous on;\n";
break;
case "unmanaged":
$radvdconf .= "\t\tAdvOnLink on;\n";
$radvdconf .= "\t\tAdvAutonomous on;\n";
break;
}
$radvdconf .= "\t};\n";
}
}
}
$radvdconf .= "\troute ::/0 {\n";
switch ($dhcpv6ifconf['rapriority']) {
case "low":
$radvdconf .= "\t\tAdvRoutePreference low;\n";
break;
case "high":
$radvdconf .= "\t\tAdvRoutePreference high;\n";
break;
default:
$radvdconf .= "\t\tAdvRoutePreference medium;\n";
break;
}
if ($rasrcaddr) {
$radvdconf .= "\t\tRemoveRoute off;\n";
}
else {
$radvdconf .= "\t\tRemoveRoute on;\n";
}
$radvdconf .= "\t};\n";
/* add DNS servers */
if ($dhcpv6ifconf['radvd-dns'] != 'disabled') {
$dnslist = array();
if (isset($dhcpv6ifconf['rasamednsasdhcp6']) && is_array($dhcpv6ifconf['dnsserver']) && !empty($dhcpv6ifconf['dnsserver'])) {
foreach ($dhcpv6ifconf['dnsserver'] as $server) {
if (is_ipaddrv6($server)) {
$dnslist[] = $server;
}
}
} elseif (!isset($dhcpv6ifconf['rasamednsasdhcp6']) && isset($dhcpv6ifconf['radnsserver']) && is_array($dhcpv6ifconf['radnsserver'])) {
foreach ($dhcpv6ifconf['radnsserver'] as $server) {
if (is_ipaddrv6($server)) {
$dnslist[] = $server;
}
}
} elseif (isset($config['dnsmasq']['enable']) || isset($config['unbound']['enable'])) {
$dnslist[] = get_interface_ipv6($realif);
} elseif (is_array($config['system']['dnsserver']) && !empty($config['system']['dnsserver'])) {
foreach ($config['system']['dnsserver'] as $server) {
if (is_ipaddrv6($server)) {
$dnslist[] = $server;
}
}
}
if (count($dnslist) > 0) {
$dnsstring = implode(" ", $dnslist);
if ($dnsstring <> "") {
/*
* The value of Lifetime SHOULD by default be at least
* 3 * MaxRtrAdvInterval, where MaxRtrAdvInterval is the
* maximum RA interval as defined in [RFC4861].
* see https://redmine.pfsense.org/issues/11105
*/
$raadvrdnsslifetime = $ramaxrtradvinterval * 3;
$radvdconf .= "\tRDNSS {$dnsstring} {\n";
$radvdconf .= "\t\tAdvRDNSSLifetime {$raadvrdnsslifetime};\n";
$radvdconf .= "\t};\n";
}
}
$searchlist = array();
$domainsearchlist = explode(';', $dhcpv6ifconf['radomainsearchlist']);
foreach ($domainsearchlist as $sd) {
$sd = trim($sd);
if (is_hostname($sd)) {
$searchlist[] = $sd;
}
}
if (count($searchlist) > 0) {
$searchliststring = trim(implode(" ", $searchlist));
}
if (!empty($dhcpv6ifconf['domain'])) {
$radvdconf .= "\tDNSSL {$dhcpv6ifconf['domain']} {$searchliststring} { };\n";
} elseif (!empty($config['system']['domain'])) {
$radvdconf .= "\tDNSSL {$config['system']['domain']} {$searchliststring} { };\n";
}
}
$radvdconf .= "};\n";
}
/* handle DHCP-PD prefixes and 6RD dynamic interfaces */
foreach ($Iflist as $if => $ifdescr) {
if (!isset($config['interfaces'][$if]['track6-interface']) ||
!isset($config['interfaces'][$if]['ipaddrv6']) ||
$config['interfaces'][$if]['ipaddrv6'] != 'track6') {
continue;
}
if (!isset($config['interfaces'][$if]['enable'])) {
continue;
}
if ($config['dhcpdv6'][$if]['ramode'] == "disabled") {
continue;
}
/* Do not put in the config an interface which is down */
if (isset($blacklist[$if])) {
continue;
}
$trackif = $config['interfaces'][$if]['track6-interface'];
if (empty($config['interfaces'][$trackif])) {
continue;
}
$realif = get_real_interface($if, "inet6");
/* prevent duplicate entries, manual overrides */
if (isset($radvdifs[$realif])) {
continue;
}
$ifcfgipv6 = get_interface_ipv6($if);
if (!is_ipaddrv6($ifcfgipv6)) {
$subnetv6 = "::";
$ifcfgsnv6 = "64";
} else {
$ifcfgsnv6 = get_interface_subnetv6($if);
$subnetv6 = gen_subnetv6($ifcfgipv6, $ifcfgsnv6);
}
$radvdifs[$realif] = $realif;
$autotype = $config['interfaces'][$trackif]['ipaddrv6'];
if ($g['debug']) {
log_error("configuring RA on {$if} for type {$autotype} radvd subnet {$subnetv6}/{$ifcfgsnv6}");
}
$radvdconf .= "# Generated config for {$autotype} delegation from {$trackif} on {$if}\n";
$radvdconf .= "interface {$realif} {\n";
$radvdconf .= "\tAdvSendAdvert on;\n";
if (is_numericint($dhcpv6ifconf['raminrtradvinterval'])) {
$radvdconf .= "\tMinRtrAdvInterval {$dhcpv6ifconf['raminrtradvinterval']};\n";
} else {
$radvdconf .= "\tMinRtrAdvInterval 5;\n";
}
if (is_numericint($dhcpv6ifconf['ramaxrtradvinterval'])) {
$radvdconf .= "\tMaxRtrAdvInterval {$dhcpv6ifconf['ramaxrtradvinterval']};\n";
} else {
$radvdconf .= "\tMaxRtrAdvInterval 10;\n";
}
$mtu = get_interface_mtu($realif);
if (is_numeric($mtu)) {
$radvdconf .= "\tAdvLinkMTU {$mtu};\n";
} else {
$radvdconf .= "\tAdvLinkMTU 1280;\n";
}
$radvdconf .= "\tAdvOtherConfigFlag on;\n";
$radvdconf .= "\tprefix {$subnetv6}/{$ifcfgsnv6} {\n";
$radvdconf .= "\t\tAdvOnLink on;\n";
$radvdconf .= "\t\tAdvAutonomous on;\n";
$radvdconf .= "\t};\n";
/* add DNS servers */
if ($dhcpv6ifconf['radvd-dns'] != 'disabled') {
$dnslist = array();
if (isset($config['dnsmasq']['enable']) || isset($config['unbound']['enable'])) {
$dnslist[] = $ifcfgipv6;
} elseif (is_array($config['system']['dnsserver']) && !empty($config['system']['dnsserver'])) {
foreach ($config['system']['dnsserver'] as $server) {
if (is_ipaddrv6($server)) {
$dnslist[] = $server;
}
}
}
if (count($dnslist) > 0) {
$dnsstring = implode(" ", $dnslist);
if (!empty($dnsstring)) {
$radvdconf .= "\tRDNSS {$dnsstring} { };\n";
}
}
if (!empty($config['system']['domain'])) {
$radvdconf .= "\tDNSSL {$config['system']['domain']} { };\n";
}
}
$radvdconf .= "};\n";
}
/* write radvd.conf */
if (!@file_put_contents("{$g['varetc_path']}/radvd.conf", $radvdconf)) {
log_error(gettext("Error: cannot open radvd.conf in services_radvd_configure()."));
if (platform_booting()) {
printf("Error: cannot open radvd.conf in services_radvd_configure().\n");
}
}
unset($radvdconf);
if (count($radvdifs) > 0) {
if (isvalidpid("{$g['varrun_path']}/radvd.pid")) {
sigkillbypid("{$g['varrun_path']}/radvd.pid", "HUP");
} else {
mwexec("/usr/local/sbin/radvd -p {$g['varrun_path']}/radvd.pid -C {$g['varetc_path']}/radvd.conf -m syslog");
}
} else {
/* we need to shut down the radvd cleanly, it will send out the prefix
* information with a lifetime of 0 to notify clients of a (possible) new prefix */
if (isvalidpid("{$g['varrun_path']}/radvd.pid")) {
log_error(gettext("Shutting down Router Advertisment daemon cleanly"));
killbypid("{$g['varrun_path']}/radvd.pid");
@unlink("{$g['varrun_path']}/radvd.pid");
}
}
return 0;
}
function services_dhcpd_configure($family = "all", $blacklist = array()) {
global $config, $g;
$dhcpdconfigurelck = lock("dhcpdconfigure", LOCK_EX);
/* configure DHCPD chroot once */
$fd = fopen("{$g['tmp_path']}/dhcpd.sh", "w");
fwrite($fd, "/bin/mkdir -p {$g['dhcpd_chroot_path']}\n");
fwrite($fd, "/bin/mkdir -p {$g['dhcpd_chroot_path']}/dev\n");
fwrite($fd, "/bin/mkdir -p {$g['dhcpd_chroot_path']}/etc\n");
fwrite($fd, "/bin/mkdir -p {$g['dhcpd_chroot_path']}/usr/local/sbin\n");
fwrite($fd, "/bin/mkdir -p {$g['dhcpd_chroot_path']}/var/db\n");
fwrite($fd, "/bin/mkdir -p {$g['dhcpd_chroot_path']}/var/run\n");
fwrite($fd, "/bin/mkdir -p {$g['dhcpd_chroot_path']}/usr\n");
fwrite($fd, "/bin/mkdir -p {$g['dhcpd_chroot_path']}/lib\n");
fwrite($fd, "/bin/mkdir -p {$g['dhcpd_chroot_path']}/run\n");
fwrite($fd, "/usr/sbin/chown -R dhcpd:_dhcp {$g['dhcpd_chroot_path']}/*\n");
fwrite($fd, "/bin/cp -n /lib/libc.so.* {$g['dhcpd_chroot_path']}/lib/\n");
fwrite($fd, "/bin/cp -n /usr/local/sbin/dhcpd {$g['dhcpd_chroot_path']}/usr/local/sbin/\n");
fwrite($fd, "/bin/chmod a+rx {$g['dhcpd_chroot_path']}/usr/local/sbin/dhcpd\n");
$status = `/sbin/mount | /usr/bin/grep -v grep | /usr/bin/grep "{$g['dhcpd_chroot_path']}/dev"`;
if (!trim($status)) {
fwrite($fd, "/sbin/mount -t devfs devfs {$g['dhcpd_chroot_path']}/dev\n");
}
fclose($fd);
mwexec("/bin/sh {$g['tmp_path']}/dhcpd.sh");
if ($family == "all" || $family == "inet") {
services_dhcpdv4_configure();
}
if ($family == "all" || $family == "inet6") {
services_dhcpdv6_configure($blacklist);
services_radvd_configure($blacklist);
}
unlock($dhcpdconfigurelck);
}
function services_dhcpdv4_configure() {
global $config, $g;
$need_ddns_updates = false;
$ddns_zones = array();
if ($g['services_dhcp_server_enable'] == false) {
return;
}
if (isset($config['system']['developerspew'])) {
$mt = microtime();
echo "services_dhcpdv4_configure($if) being called $mt\n";
}
/* kill any running dhcpd */
if (isvalidpid("{$g['dhcpd_chroot_path']}{$g['varrun_path']}/dhcpd.pid")) {
killbypid("{$g['dhcpd_chroot_path']}{$g['varrun_path']}/dhcpd.pid");
}
/* DHCP enabled on any interfaces? */
if (!is_dhcp_server_enabled()) {
return 0;
}
$syscfg = $config['system'];
if (!is_array($config['dhcpd'])) {
$config['dhcpd'] = array();
}
$dhcpdcfg = $config['dhcpd'];
$Iflist = get_configured_interface_list();
/* Only consider DNS servers with IPv4 addresses for the IPv4 DHCP server. */
$dns_arrv4 = array();
if (is_array($syscfg['dnsserver'])) {
foreach ($syscfg['dnsserver'] as $dnsserver) {
if (is_ipaddrv4($dnsserver)) {
$dns_arrv4[] = $dnsserver;
}
}
}
if (platform_booting()) {
echo gettext("Starting DHCP service...");
} else {
sleep(1);
}
$custoptions = "";
foreach ($dhcpdcfg as $dhcpif => $dhcpifconf) {
if (is_array($dhcpifconf['numberoptions']) && is_array($dhcpifconf['numberoptions']['item'])) {
foreach ($dhcpifconf['numberoptions']['item'] as $itemidx => $item) {
if (!empty($item['type'])) {
$itemtype = $item['type'];
} else {
$itemtype = "text";
}
$custoptions .= "option custom-{$dhcpif}-{$itemidx} code {$item['number']} = {$itemtype};\n";
}
}
if (is_array($dhcpifconf['pool'])) {
foreach ($dhcpifconf['pool'] as $poolidx => $poolconf) {
if (is_array($poolconf['numberoptions']) && is_array($poolconf['numberoptions']['item'])) {
foreach ($poolconf['numberoptions']['item'] as $itemidx => $item) {
if (!empty($item['type'])) {
$itemtype = $item['type'];
} else {
$itemtype = "text";
}
$custoptions .= "option custom-{$dhcpif}-{$poolidx}-{$itemidx} code {$item['number']} = {$itemtype};\n";
}
}
}
}
if (is_array($dhcpifconf['staticmap'])) {
$i = 0;
foreach ($dhcpifconf['staticmap'] as $sm) {
if (is_array($sm['numberoptions']) && is_array($sm['numberoptions']['item'])) {
foreach ($sm['numberoptions']['item'] as $itemidx => $item) {
if (!empty($item['type'])) {
$itemtype = $item['type'];
} else {
$itemtype = "text";
}
$custoptions .= "option custom-s_{$dhcpif}_{$i}-{$itemidx} code {$item['number']} = {$itemtype};\n";
}
}
$i++;
}
}
}
$dhcpdconf = <<<EOD
option domain-name "{$syscfg['domain']}";
option ldap-server code 95 = text;
option domain-search-list code 119 = text;
option arch code 93 = unsigned integer 16; # RFC4578
{$custoptions}
default-lease-time 7200;
max-lease-time 86400;
log-facility local7;
one-lease-per-client true;
deny duplicates;
update-conflict-detection false;
EOD;
/* take these settings from the first DHCP configured interface,
* see https://redmine.pfsense.org/issues/10270
* TODO: Global Settings tab, see https://redmine.pfsense.org/issues/5080 */
foreach ($dhcpdcfg as $dhcpif => $dhcpifconf) {
if (!isset($dhcpifconf['disableauthoritative'])) {
$dhcpdconf .= "authoritative;\n";
}
if (isset($dhcpifconf['alwaysbroadcast'])) {
$dhcpdconf .= "always-broadcast on\n";
}
// OMAPI Settings
if (isset($dhcpifconf['omapi_port']) && is_numeric($dhcpifconf['omapi_port'])) {
$dhcpdconf .= <<<EOD
key omapi_key {
algorithm {$dhcpifconf['omapi_key_algorithm']};
secret "{$dhcpifconf['omapi_key']}";
};
omapi-port {$dhcpifconf['omapi_port']};
omapi-key omapi_key;
EOD;
}
break;
}
$dhcpdifs = array();
$enable_add_routers = false;
$gateways_arr = return_gateways_array();
/* only add a routers line if the system has any IPv4 gateway at all */
/* a static route has a gateway, manually overriding this field always works */
foreach ($gateways_arr as $gwitem) {
if ($gwitem['ipprotocol'] == "inet") {
$enable_add_routers = true;
break;
}
}
/* loop through and determine if we need to setup
* failover peer "bleh" entries
*/
foreach ($dhcpdcfg as $dhcpif => $dhcpifconf) {
if (!isset($config['interfaces'][$dhcpif]['enable'])) {
continue;
}
interfaces_staticarp_configure($dhcpif);
if (!isset($dhcpifconf['enable'])) {
continue;
}
if ($dhcpifconf['failover_peerip'] <> "") {
$intip = get_interface_ip($dhcpif);
/*
* yep, failover peer is defined.
* does it match up to a defined vip?
*/
$skew = 110;
if (is_array($config['virtualip']['vip'])) {
foreach ($config['virtualip']['vip'] as $vipent) {
if ($vipent['mode'] != 'carp') {
continue;
}
if ($vipent['interface'] == $dhcpif) {
$carp_nw = gen_subnet($config['interfaces'][$dhcpif]['ipaddr'],
$config['interfaces'][$dhcpif]['subnet']);
$carp_nw .= "/{$config['interfaces'][$dhcpif]['subnet']}";
if (ip_in_subnet($dhcpifconf['failover_peerip'], $carp_nw)) {
/* this is the interface! */
if (is_numeric($vipent['advskew']) && (intval($vipent['advskew']) < 20)) {
$skew = 0;
break;
}
}
}
}
} else {
log_error(gettext("Warning! DHCP Failover setup and no CARP virtual IPs defined!"));
}
if ($skew > 10) {
$type = "secondary";
$my_port = "520";
$peer_port = "519";
$dhcpdconf_pri = '';
} else {
$my_port = "519";
$peer_port = "520";
$type = "primary";
$dhcpdconf_pri = "split 128;\n";
$dhcpdconf_pri .= " mclt 600;\n";
}
if (is_ipaddrv4($intip)) {
$dhcpdconf .= <<<EOPP
failover peer "dhcp_{$dhcpif}" {
{$type};
address {$intip};
port {$my_port};
peer address {$dhcpifconf['failover_peerip']};
peer port {$peer_port};
max-response-delay 10;
max-unacked-updates 10;
{$dhcpdconf_pri}
load balance max seconds 3;
}
\n
EOPP;
}
}
}
foreach ($dhcpdcfg as $dhcpif => $dhcpifconf) {
$newzone = array();
$ifcfg = $config['interfaces'][$dhcpif];
if (!isset($dhcpifconf['enable']) || !isset($Iflist[$dhcpif])) {
continue;
}
$ifcfgip = get_interface_ip($dhcpif);
$ifcfgsn = get_interface_subnet($dhcpif);
$subnet = gen_subnet($ifcfgip, $ifcfgsn);
$subnetmask = gen_subnet_mask($ifcfgsn);
if (!is_ipaddr($subnet)) {
continue;
}
$all_pools = array();
$all_pools[] = $dhcpifconf;
if (is_array($dhcpifconf['pool'])) {
$all_pools = array_merge($all_pools, $dhcpifconf['pool']);
}
$dnscfg = "";
if ($dhcpifconf['domain']) {
$dnscfg .= " option domain-name \"{$dhcpifconf['domain']}\";\n";
}
if ($dhcpifconf['domainsearchlist'] <> "") {
$dnscfg .= " option domain-search \"" . join("\",\"", preg_split("/[ ;]+/", $dhcpifconf['domainsearchlist'])) . "\";\n";
}
if (isset($dhcpifconf['ddnsupdate'])) {
$need_ddns_updates = true;
$newzone = array();
if ($dhcpifconf['ddnsdomain'] <> "") {
$newzone['domain-name'] = $dhcpifconf['ddnsdomain'];
$dnscfg .= " ddns-domainname \"{$dhcpifconf['ddnsdomain']}\";\n";
} else {
$newzone['domain-name'] = $config['system']['domain'];
}
if (empty($dhcpifconf['ddnsclientupdates'])) {
$ddnsclientupdates = 'allow';
} else {
$ddnsclientupdates = $dhcpifconf['ddnsclientupdates'];
}
$dnscfg .= " {$ddnsclientupdates} client-updates;\n";
$revsubnet = array_reverse(explode('.',$subnet));
$subnet_mask_bits = 32 - $ifcfgsn;
$start_octet = $subnet_mask_bits >> 3;
$octet_mask_bits = $subnet_mask_bits & ($subnet_mask_bits % 8);
if ($octet_mask_bits) {
$octet_mask = (1 << $octet_mask_bits) - 1;
$octet_start = $revsubnet[$start_octet] & ~$octet_mask;
$revsubnet[$start_octet] = $octet_start . "-" . ($octet_start + $octet_mask);
}
$ptr_domain = '';
for ($octet = 0; $octet <= 3; $octet++) {
if ($octet < $start_octet) {
continue;
}
$ptr_domain .= ((empty($ptr_domain) && $ptr_domain !== "0") ? '' : '.');
$ptr_domain .= $revsubnet[$octet];
}
$ptr_domain .= ".in-addr.arpa";
$newzone['ptr-domain'] = $ptr_domain;
unset($ptr_domain, $revsubnet, $start_octet);
}
if (is_array($dhcpifconf['dnsserver']) && ($dhcpifconf['dnsserver'][0])) {
$dnscfg .= " option domain-name-servers " . join(",", $dhcpifconf['dnsserver']) . ";";
if ($newzone['domain-name']) {
$newzone['dns-servers'] = $dhcpifconf['dnsserver'];
}
} else if (isset($config['dnsmasq']['enable'])) {
$dnscfg .= " option domain-name-servers {$ifcfgip};";
if ($newzone['domain-name'] && is_array($syscfg['dnsserver']) && ($syscfg['dnsserver'][0])) {
$newzone['dns-servers'] = $syscfg['dnsserver'];
}
} else if (isset($config['unbound']['enable'])) {
$dnscfg .= " option domain-name-servers {$ifcfgip};";
} else if (!empty($dns_arrv4)) {
$dnscfg .= " option domain-name-servers " . join(",", $dns_arrv4) . ";";
if ($newzone['domain-name']) {
$newzone['dns-servers'] = $dns_arrv4;
}
}
/* Create classes - These all contain comma separated lists. Join them into one
big comma separated string then split them all up. */
$all_mac_strings = array();
if (is_array($dhcpifconf['pool'])) {
foreach ($all_pools as $poolconf) {
$all_mac_strings[] = $poolconf['mac_allow'];
$all_mac_strings[] = $poolconf['mac_deny'];
}
}
$all_mac_strings[] = $dhcpifconf['mac_allow'];
$all_mac_strings[] = $dhcpifconf['mac_deny'];
if (!empty($all_mac_strings)) {
$all_mac_list = array_unique(explode(',', implode(',', $all_mac_strings)));
foreach ($all_mac_list as $mac) {
if (empty($mac)) {
continue;
}
$dhcpdconf .= 'class "' . str_replace(':', '', $mac) . '" {' . "\n";
// Skip the first octet of the MAC address - for media type, typically Ethernet ("01") and match the rest.
$dhcpdconf .= ' match if substring (hardware, 1, ' . (substr_count($mac, ':') + 1) . ') = ' . $mac . ';' . "\n";
$dhcpdconf .= '}' . "\n";
}
}
// instantiate class before pool definition
$dhcpdconf .= "class \"s_{$dhcpif}\" {\n match pick-first-value (option dhcp-client-identifier, hardware);\n}\n";
$dhcpdconf .= "subnet {$subnet} netmask {$subnetmask} {\n";
// Setup pool options
foreach ($all_pools as $all_pools_idx => $poolconf) {
if (!(ip_in_subnet($poolconf['range']['from'], "{$subnet}/{$ifcfgsn}") && ip_in_subnet($poolconf['range']['to'], "{$subnet}/{$ifcfgsn}"))) {
// If the user has changed the subnet from the interfaces page and applied,
// but has not updated the DHCP range, then the range to/from of the pool can be outside the subnet.
// This can also happen when implementing the batch of changes when the setup wizard reloads the new settings.
$error_msg = sprintf(gettext('Invalid DHCP pool %1$s - %2$s for %3$s subnet %4$s/%5$s detected. Please correct the settings in Services, DHCP Server'), $poolconf['range']['from'], $poolconf['range']['to'], convert_real_interface_to_friendly_descr($dhcpif), $subnet, $ifcfgsn);
$do_file_notice = true;
$conf_ipv4_address = $ifcfg['ipaddr'];
$conf_ipv4_subnetmask = $ifcfg['subnet'];
if (is_ipaddrv4($conf_ipv4_address) && is_subnet("{$conf_ipv4_address}/{$conf_ipv4_subnetmask}")) {
$conf_subnet_base = gen_subnet($conf_ipv4_address, $conf_ipv4_subnetmask);
if (ip_in_subnet($poolconf['range']['from'], "{$conf_subnet_base}/{$conf_ipv4_subnetmask}") &&
ip_in_subnet($poolconf['range']['to'], "{$conf_subnet_base}/{$conf_ipv4_subnetmask}")) {
// Even though the running interface subnet does not match the pool range,
// the interface subnet in the config file contains the pool range.
// We are somewhere part-way through a settings reload, e.g. after running the setup wizard.
// services_dhcpdv4_configure will be called again later when the new interface settings from
// the config are applied and at that time everything will match up.
// Ignore this pool on this interface for now and just log the error to the system log.
log_error($error_msg);
$do_file_notice = false;
}
}
if ($do_file_notice) {
file_notice("DHCP", $error_msg);
}
continue;
}
$dhcpdconf .= " pool {\n";
/* is failover dns setup? */
if (is_array($poolconf['dnsserver']) && $poolconf['dnsserver'][0] <> "") {
$dhcpdconf .= " option domain-name-servers {$poolconf['dnsserver'][0]}";
if ($poolconf['dnsserver'][1] <> "") {
$dhcpdconf .= ",{$poolconf['dnsserver'][1]}";
}
if ($poolconf['dnsserver'][2] <> "") {
$dhcpdconf .= ",{$poolconf['dnsserver'][2]}";
}
if ($poolconf['dnsserver'][3] <> "") {
$dhcpdconf .= ",{$poolconf['dnsserver'][3]}";
}
$dhcpdconf .= ";\n";
}
/* allow/deny MACs */
$mac_allow_list = array_unique(explode(',', $poolconf['mac_allow']));
foreach ($mac_allow_list as $mac) {
if (empty($mac)) {
continue;
}
$dhcpdconf .= " allow members of \"" . str_replace(':', '', $mac) . "\";\n";
}
$deny_action = "deny";
if (isset($poolconf['nonak']) && empty($poolconf['failover_peerip'])) {
$deny_action = "ignore";
}
$mac_deny_list = array_unique(explode(',', $poolconf['mac_deny']));
foreach ($mac_deny_list as $mac) {
if (empty($mac)) {
continue;
}
$dhcpdconf .= " $deny_action members of \"" . str_replace(':', '', $mac) . "\";\n";
}
if ($poolconf['failover_peerip'] <> "") {
$dhcpdconf .= " $deny_action dynamic bootp clients;\n";
}
// set pool MAC limitations
if (isset($poolconf['denyunknown'])) {
if ($poolconf['denyunknown'] == "class") {
$dhcpdconf .= " allow members of \"s_{$dhcpif}\";\n";
$dhcpdconf .= " $deny_action unknown-clients;\n";
} else if ($poolconf['denyunknown'] == "disabled") {
// add nothing to $dhcpdconf; condition added to prevent next condition applying if ever engine changes such that: isset("disabled") == true
} else { // "catch-all" covering "enabled" value post-PR#4066, and covering non-upgraded boolean option (i.e. literal value "enabled")
$dhcpdconf .= " $deny_action unknown-clients;\n";
}
}
if ($poolconf['gateway'] && $poolconf['gateway'] != "none" && ($poolconf['gateway'] != $dhcpifconf['gateway'])) {
$dhcpdconf .= " option routers {$poolconf['gateway']};\n";
}
if ($dhcpifconf['failover_peerip'] <> "") {
$dhcpdconf .= " failover peer \"dhcp_{$dhcpif}\";\n";
}
$pdnscfg = "";
if ($poolconf['domain'] && ($poolconf['domain'] != $dhcpifconf['domain'])) {
$pdnscfg .= " option domain-name \"{$poolconf['domain']}\";\n";
}
if (!empty($poolconf['domainsearchlist']) && ($poolconf['domainsearchlist'] != $dhcpifconf['domainsearchlist'])) {
$pdnscfg .= " option domain-search \"" . join("\",\"", preg_split("/[ ;]+/", $poolconf['domainsearchlist'])) . "\";\n";
}
if (isset($poolconf['ddnsupdate'])) {
if (($poolconf['ddnsdomain'] <> "") && ($poolconf['ddnsdomain'] != $dhcpifconf['ddnsdomain'])) {
$pdnscfg .= " ddns-domainname \"{$poolconf['ddnsdomain']}\";\n";
}
$pdnscfg .= " ddns-update-style interim;\n";
}
$dhcpdconf .= "{$pdnscfg}";
// default-lease-time
if ($poolconf['defaultleasetime'] && ($poolconf['defaultleasetime'] != $dhcpifconf['defaultleasetime'])) {
$dhcpdconf .= " default-lease-time {$poolconf['defaultleasetime']};\n";
}
// max-lease-time
if ($poolconf['maxleasetime'] && ($poolconf['maxleasetime'] != $dhcpifconf['maxleasetime'])) {
$dhcpdconf .= " max-lease-time {$poolconf['maxleasetime']};\n";
}
// ignore bootp
if (isset($poolconf['ignorebootp'])) {
$dhcpdconf .= " ignore bootp;\n";
}
// ignore-client-uids
if (isset($poolconf['ignoreclientuids'])) {
$dhcpdconf .= " ignore-client-uids true;\n";
}
// netbios-name*
if (is_array($poolconf['winsserver']) && $poolconf['winsserver'][0] && ($poolconf['winsserver'][0] != $dhcpifconf['winsserver'][0])) {
$dhcpdconf .= " option netbios-name-servers " . join(",", $poolconf['winsserver']) . ";\n";
$dhcpdconf .= " option netbios-node-type 8;\n";
}
// ntp-servers
if (is_array($poolconf['ntpserver']) && $poolconf['ntpserver'][0] && ($poolconf['ntpserver'][0] != $dhcpifconf['ntpserver'][0])) {
$dhcpdconf .= " option ntp-servers " . join(",", $poolconf['ntpserver']) . ";\n";
}
// tftp-server-name
if (!empty($poolconf['tftp']) && ($poolconf['tftp'] != $dhcpifconf['tftp'])) {
$dhcpdconf .= " option tftp-server-name \"{$poolconf['tftp']}\";\n";
}
// Handle pool-specific options
$dhcpdconf .= "\n";
// Ignore the first pool, which is the "overall" pool when $all_pools_idx is 0 - those are put outside the pool block later
if (isset($poolconf['numberoptions']['item']) && is_array($poolconf['numberoptions']['item']) && ($all_pools_idx > 0)) {
// Use the "real" pool index from the config, excluding the "overall" pool, and based from 0.
// This matches the way $poolidx was used when generating the $custoptions string earlier.
$poolidx = $all_pools_idx - 1;
foreach ($poolconf['numberoptions']['item'] as $itemidx => $item) {
$item_value = base64_decode($item['value']);
if (empty($item['type']) || $item['type'] == "text") {
$dhcpdconf .= " option custom-{$dhcpif}-{$poolidx}-{$itemidx} \"{$item_value}\";\n";
} else {
$dhcpdconf .= " option custom-{$dhcpif}-{$poolidx}-{$itemidx} {$item_value};\n";
}
}
}
// ldap-server
if (!empty($poolconf['ldap']) && ($poolconf['ldap'] != $dhcpifconf['ldap'])) {
$dhcpdconf .= " option ldap-server \"{$poolconf['ldap']}\";\n";
}
// net boot information
if (isset($poolconf['netboot'])) {
if (!empty($poolconf['nextserver']) && ($poolconf['nextserver'] != $dhcpifconf['nextserver'])) {
$dhcpdconf .= " next-server {$poolconf['nextserver']};\n";
}