-
Notifications
You must be signed in to change notification settings - Fork 759
/
system.inc
1860 lines (1627 loc) · 64.4 KB
/
system.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
/*
Copyright (C) 2016 Franco Fichtner <franco@opnsense.org>
Copyright (C) 2003-2004 Manuel Kasper <mk@neon1.net>
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
function activate_powerd()
{
global $config;
if (is_process_running('powerd')) {
exec('/usr/bin/killall powerd');
}
if(isset($config['system']['powerd_enable'])) {
$ac_mode = "hadp";
if (!empty($config['system']['powerd_ac_mode'])) {
$ac_mode = $config['system']['powerd_ac_mode'];
}
$battery_mode = "hadp";
if (!empty($config['system']['powerd_battery_mode'])) {
$battery_mode = $config['system']['powerd_battery_mode'];
}
$normal_mode = "hadp";
if (!empty($config['system']['powerd_normal_mode'])) {
$normal_mode = $config['system']['powerd_normal_mode'];
}
mwexec("/usr/sbin/powerd -b $battery_mode -a $ac_mode -n $normal_mode");
}
}
function get_default_sysctl_value($id)
{
$sysctls = array(
"debug.pfftpproxy" => "0",
"hw.syscons.kbd_reboot" => "0",
"kern.ipc.maxsockbuf" => "4262144",
"kern.randompid" => "347",
"kern.random.sys.harvest.interrupt" => 0,
"kern.random.sys.harvest.point_to_point" => 0,
"kern.random.sys.harvest.ethernet" => 0,
"kern.filedelay" => "5",
"kern.dirdelay" => "4",
"kern.metadelay" => "3",
"net.bpf.zerocopy_enable" => 1,
"net.inet.ip.portrange.first" => "1024",
"net.inet.tcp.blackhole" => "2",
"net.inet.udp.blackhole" => "1",
"net.inet.ip.random_id" => "1",
"net.inet.tcp.drop_synfin" => "1",
"net.inet.ip.redirect" => "1",
"net.inet6.ip6.redirect" => "1",
"net.inet6.ip6.use_tempaddr" => "0",
"net.inet6.ip6.prefer_tempaddr" => "0",
"net.inet.tcp.syncookies" => "1",
"net.inet.tcp.recvspace" => "65228",
"net.inet.tcp.sendspace" => "65228",
"net.inet.ip.fastforwarding" => "0",
'net.inet.ip.sourceroute' => '0',
'net.inet.ip.accept_sourceroute' => '0',
'net.inet.icmp.drop_redirect' => '0',
'net.inet.icmp.log_redirect' => '0',
"net.inet.tcp.delayed_ack" => "0",
"net.inet.udp.maxdgram" => "57344",
"net.inet.ip.intr_queue_maxlen" => "1000",
"net.inet.tcp.log_debug" => "0",
"net.inet.tcp.tso" => "1",
"net.inet.icmp.icmplim" => "0",
"net.inet.ip.process_options" => 0,
"net.inet.udp.checksum" => 1,
"net.link.bridge.pfil_onlyip" => "0",
"net.link.bridge.pfil_member" => "1",
"net.link.bridge.pfil_bridge" => "0",
"net.link.tap.user_open" => "1",
"net.route.netisr_maxqlen" => 1024,
"net.inet.icmp.reply_from_interface" => 1,
"vfs.read_max" => "32",
);
if (isset($sysctls[$id])) {
return $sysctls[$id];
}
return null;
}
function activate_sysctls()
{
global $config;
$sysctls = array(
"net.enc.in.ipsec_bpf_mask" => "0x0002",
"net.enc.in.ipsec_filter_mask" => "0x0002",
"net.enc.out.ipsec_bpf_mask" => "0x0001",
"net.enc.out.ipsec_filter_mask" => "0x0001",
);
if (isset($config['sysctl']['item'])) {
foreach($config['sysctl']['item'] as $tunable) {
if ($tunable['value'] == 'default') {
$value = get_default_sysctl_value($tunable['tunable']);
} else {
$value = $tunable['value'];
}
$sysctls[$tunable['tunable']] = $value;
}
}
set_sysctl($sysctls);
}
function system_resolvconf_generate($dynupdate = false)
{
global $config;
$syscfg = $config['system'];
// Do not create blank domain lines, it breaks tools like dig.
if($syscfg['domain']) {
$resolvconf = "domain {$syscfg['domain']}\n";
}
if (((isset($config['dnsmasq']['enable']) && (empty($config['dnsmasq']['interface']) || in_array("lo0", explode(",", $config['dnsmasq']['interface']))))
|| (isset($config['unbound']['enable'])) && (empty($config['unbound']['active_interface']) || in_array("lo0", explode(",", $config['unbound']['active_interface']))))
&& !isset($config['system']['dnslocalhost'])) {
$resolvconf .= "nameserver 127.0.0.1\n";
}
if (isset($syscfg['dnsallowoverride'])) {
/* get dynamically assigned DNS servers (if any) */
$ns = array_unique(get_searchdomains());
foreach($ns as $searchserver) {
if($searchserver) {
$resolvconf .= "search {$searchserver}\n";
}
}
$ns = array_unique(get_nameservers());
foreach($ns as $nameserver) {
if($nameserver) {
$resolvconf .= "nameserver $nameserver\n";
}
}
}
if (isset($syscfg['dnsserver']) && is_array($syscfg['dnsserver'])) {
foreach ($syscfg['dnsserver'] as $ns) {
if ($ns) {
$resolvconf .= "nameserver $ns\n";
}
}
}
// Add EDNS support
if (isset($config['unbound']['enable']) && isset($config['unbound']['edns'])) {
$resolvconf .= "options edns0\n";
}
$dnslock = lock('resolvconf', LOCK_EX);
$fd = fopen('/etc/resolv.conf', 'w');
if (!$fd) {
printf("Error: cannot open resolv.conf in system_resolvconf_generate().\n");
unlock($dnslock);
return 1;
}
fwrite($fd, $resolvconf);
fclose($fd);
chmod('/etc/resolv.conf', 0644);
if (!file_exists("/var/run/booting")) {
/* restart dhcpd (nameservers may have changed) */
if (!$dynupdate) {
services_dhcpd_configure();
}
}
/* setup static routes for DNS servers. */
for ($dnscounter=1; $dnscounter<5; $dnscounter++) {
/* setup static routes for dns servers */
$dnsgw = "dns{$dnscounter}gw";
if (isset($config['system'][$dnsgw])) {
$gwname = $config['system'][$dnsgw];
if (($gwname <> "") && ($gwname <> "none")) {
$gatewayip = lookup_gateway_ip_by_name($gwname);
if (is_ipaddrv4($gatewayip)) {
/* dns server array starts at 0 */
$dnscountermo = $dnscounter - 1;
mwexec("/sbin/route delete -host " . $syscfg['dnsserver'][$dnscountermo]);
mwexec("/sbin/route add -host " . $syscfg['dnsserver'][$dnscountermo] . " {$gatewayip}");
}
if (is_ipaddrv6($gatewayip)) {
/* dns server array starts at 0 */
$dnscountermo = $dnscounter - 1;
mwexec("/sbin/route delete -host -inet6 " . $syscfg['dnsserver'][$dnscountermo]);
mwexec("/sbin/route add -host -inet6 " . $syscfg['dnsserver'][$dnscountermo] . " {$gatewayip}");
}
}
}
}
unlock($dnslock);
return 0;
}
function get_country_codes()
{
$dn_cc = array();
$iso3166_tab = '/usr/local/opnsense/contrib/tzdata/iso3166.tab';
if (file_exists($iso3166_tab)) {
$dn_cc_file = file($iso3166_tab);
foreach ($dn_cc_file as $line) {
if (preg_match('/^([A-Z][A-Z])\t(.*)$/', $line, $matches)) {
$dn_cc[$matches[1]] = trim($matches[2]);
}
}
}
return $dn_cc;
}
function get_firmware_mirrors()
{
$mirrors = array();
$mirrors['default'] = '(default)';
$mirrors['https://opnsense.aivian.org'] = 'Aivian (Shaoxing, CN)';
$mirrors['https://opnsense.c0urier.net'] = 'c0urier.net (Lund, SE)';
$mirrors['https://fleximus.org/mirror/opnsense'] = 'Fleximus (Roubaix, FR)';
$mirrors['http://mirror.ams1.nl.leaseweb.net/opnsense'] = 'LeaseWeb (Amsterdam, NL)';
$mirrors['http://mirror.fra10.de.leaseweb.net/opnsense'] = 'LeaseWeb (Frankfurt, DE)';
$mirrors['http://mirror.sfo12.us.leaseweb.net/opnsense'] = 'LeaseWeb (San Francisco, US)';
$mirrors['http://mirror.wdc1.us.leaseweb.net/opnsense'] = 'LeaseWeb (Washington, D.C., US)';
$mirrors['http://mirrors.nycbug.org/pub/opnsense'] = 'NYC*BUG (New York, US)';
$mirrors['http://pkg.opnsense.org'] = 'OPNsense (Amsterdam, NL)';
$mirrors['http://mirror.ragenetwork.de/opnsense'] = 'RageNetwork (Munich, DE)';
$mirrors['http://mirrors.supranet.net/pub/opnsense'] = 'Supranet Communications (Middleton, US)';
$mirrors['http://mirror.wjcomms.co.uk/opnsense'] = 'WJComms (London, GB)';
return $mirrors;
}
function get_firmware_flavours()
{
$flavours = array();
$flavours['default'] = '(default)';
$flavours['libressl'] = 'LibreSSL';
$flavours['latest'] = 'OpenSSL';
return $flavours;
}
function get_zoneinfo()
{
return timezone_identifiers_list(DateTimeZone::ALL ^ DateTimeZone::UTC);
}
function get_searchdomains()
{
global $config;
$master_list = array();
// Read in dhclient nameservers
$search_list = glob("/var/etc/searchdomain_*");
if (is_array($search_list)) {
foreach($search_list as $fdns) {
$contents = file($fdns, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
if (!is_array($contents)) {
continue;
}
foreach ($contents as $dns) {
if(is_hostname($dns)) {
$master_list[] = $dns;
}
}
}
}
return $master_list;
}
function get_nameservers()
{
global $config;
$master_list = array();
// Read in dhclient nameservers
$dns_lists = glob("/var/etc/nameserver_*");
if (is_array($dns_lists)) {
foreach($dns_lists as $fdns) {
$contents = file($fdns, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
if (!is_array($contents)) {
continue;
}
foreach ($contents as $dns) {
if(is_ipaddr($dns)) {
$master_list[] = $dns;
}
}
}
}
// Read in any extra nameservers
if(file_exists("/var/etc/nameservers.conf")) {
$dns_s = file("/var/etc/nameservers.conf", FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
if(is_array($dns_s)) {
foreach($dns_s as $dns) {
if (is_ipaddr($dns)) {
$master_list[] = $dns;
}
}
}
}
return $master_list;
}
function system_hosts_generate()
{
global $config;
$syscfg = $config['system'];
$dnsmasqcfg = $config['dnsmasq'];
$hosts = "127.0.0.1 localhost localhost.{$syscfg['domain']}\n";
$lhosts = "";
$dhosts = "";
if (isset($config['interfaces']['lan'])) {
$cfgip = get_interface_ip("lan");
if (is_ipaddr($cfgip)) {
$hosts .= "{$cfgip} {$syscfg['hostname']}.{$syscfg['domain']} {$syscfg['hostname']}\n";
}
} else {
$sysiflist = get_configured_interface_list();
foreach ($sysiflist as $sysif) {
if (!interface_has_gateway($sysif)) {
$cfgip = get_interface_ip($sysif);
if (is_ipaddr($cfgip)) {
$hosts .= "{$cfgip} {$syscfg['hostname']}.{$syscfg['domain']} {$syscfg['hostname']}\n";
break;
}
}
}
}
if (isset($dnsmasqcfg['enable'])) {
if (!isset($dnsmasqcfg['hosts']) || !is_array($dnsmasqcfg['hosts'])) {
$dnsmasqcfg['hosts'] = array();
}
foreach ($dnsmasqcfg['hosts'] as $host) {
if ($host['host']) {
$lhosts .= "{$host['ip']} {$host['host']}.{$host['domain']} {$host['host']}\n";
} else {
$lhosts .= "{$host['ip']} {$host['domain']}\n";
}
if (!isset($host['aliases']) || !is_array($host['aliases']) || !is_array($host['aliases']['item'])) {
continue;
}
foreach ($host['aliases']['item'] as $alias) {
if ($alias['host']) {
$lhosts .= "{$host['ip']} {$alias['host']}.{$alias['domain']} {$alias['host']}\n";
} else {
$lhosts .= "{$host['ip']} {$alias['domain']}\n";
}
}
}
if (isset($dnsmasqcfg['regdhcpstatic']) && is_array($config['dhcpd'])) {
foreach ($config['dhcpd'] as $dhcpif => $dhcpifconf) {
if (isset($dhcpifconf['staticmap']) && isset($dhcpifconf['enable'])) {
foreach ($dhcpifconf['staticmap'] as $host) {
if ($host['ipaddr'] && $host['hostname'] && $host['domain']) {
$dhosts .= "{$host['ipaddr']} {$host['hostname']}.{$host['domain']} {$host['hostname']}\n";
} elseif ($host['ipaddr'] && $host['hostname'] && $dhcpifconf['domain']) {
$dhosts .= "{$host['ipaddr']} {$host['hostname']}.{$dhcpifconf['domain']} {$host['hostname']}\n";
} elseif ($host['ipaddr'] && $host['hostname']) {
$dhosts .= "{$host['ipaddr']} {$host['hostname']}.{$syscfg['domain']} {$host['hostname']}\n";
}
}
}
}
}
if (isset($dnsmasqcfg['regdhcpstatic']) && is_array($config['dhcpdv6'])) {
foreach ($config['dhcpdv6'] as $dhcpif => $dhcpifconf) {
if (isset($dhcpifconf['staticmap']) && isset($dhcpifconf['enable'])) {
foreach ($dhcpifconf['staticmap'] as $host) {
if ($host['ipaddrv6'] && $host['hostname'] && $host['domain']) {
$dhosts .= "{$host['ipaddrv6']} {$host['hostname']}.{$host['domain']} {$host['hostname']}\n";
} elseif ($host['ipaddrv6'] && $host['hostname'] && $dhcpifconf['domain']) {
$dhosts .= "{$host['ipaddrv6']} {$host['hostname']}.{$dhcpifconf['domain']} {$host['hostname']}\n";
} elseif ($host['ipaddrv6'] && $host['hostname']) {
$dhosts .= "{$host['ipaddrv6']} {$host['hostname']}.{$syscfg['domain']} {$host['hostname']}\n";
}
}
}
}
}
if (isset($dnsmasqcfg['dhcpfirst'])) {
$hosts .= $dhosts . $lhosts;
} else {
$hosts .= $lhosts . $dhosts;
}
}
/*
* Do not remove this because dhcpleases monitors with kqueue
* it needs to be * killed before writing to hosts files.
*/
killbypid('/var/run/dhcpleases.pid');
$fd = fopen('/etc/hosts', 'w');
if (!$fd) {
log_error("Error: cannot open hosts file in system_hosts_generate().\n");
return 1;
}
fwrite($fd, $hosts);
fclose($fd);
if (isset($config['unbound']['enable'])) {
unbound_hosts_generate();
}
system_dhcpleases_configure();
return 0;
}
function system_dhcpleases_configure()
{
global $config, $g;
/* Start the monitoring process for dynamic dhcpclients. */
if (isset($config['dnsmasq']['enable']) && isset($config['dnsmasq']['regdhcp'])) {
/* Make sure we do not error out */
mwexec("/bin/mkdir -p {$g['dhcpd_chroot_path']}/var/db");
if (!file_exists("{$g['dhcpd_chroot_path']}/var/db/dhcpd.leases")) {
@touch("{$g['dhcpd_chroot_path']}/var/db/dhcpd.leases");
}
if (isvalidpid('/var/run/dhcpleases.pid')) {
killbypid('/var/run/dhcpleases.pid', 'HUP');
} else {
/* To ensure we do not start multiple instances of dhcpleases, perform some clean-up first. */
killbyname('dhcpleases');
@unlink('/var/run/dhcpleases.pid');
if (isset($config['unbound']['enable'])) {
$dns_pid = 'unbound.pid';
} else {
$dns_pid = 'dnsmasq.pid';
}
mwexecf(
'/usr/local/sbin/dhcpleases -l %s -d %s -p %s -h %s',
array(
"{$g['dhcpd_chroot_path']}/var/db/dhcpd.leases",
$config['system']['domain'],
"/var/run/{$dns_pid}",
'/etc/hosts'
)
);
}
} else {
killbypid('/var/run/dhcpleases.pid');
}
}
function system_hostname_configure()
{
global $config;
$syscfg = $config['system'];
/* set hostname */
$status = mwexec("/bin/hostname " .
escapeshellarg("{$syscfg['hostname']}.{$syscfg['domain']}"));
/* Setup host GUID ID. This is used by ZFS. */
mwexec("/etc/rc.d/hostid start");
return $status;
}
function system_routing_configure($interface = '')
{
global $config;
$gatewayip = "";
$interfacegw = "";
$foundgw = false;
$gatewayipv6 = "";
$interfacegwv6 = "";
$foundgwv6 = false;
/* tack on all the hard defined gateways as well */
if (isset($config['gateways']['gateway_item'])) {
array_map('unlink', glob('/tmp/*_defaultgw{,v6}', GLOB_BRACE));
foreach ($config['gateways']['gateway_item'] as $gateway) {
if (isset($gateway['defaultgw'])) {
if ($gateway['ipprotocol'] != "inet6" && (is_ipaddrv4($gateway['gateway']) || $gateway['gateway'] == "dynamic")) {
if(strstr($gateway['gateway'], ":")) {
continue;
}
if ($gateway['gateway'] == "dynamic") {
$gateway['gateway'] = get_interface_gateway($gateway['interface']);
}
$gatewayip = $gateway['gateway'];
$interfacegw = $gateway['interface'];
if (!empty($gateway['interface'])) {
$defaultif = get_real_interface($gateway['interface']);
if ($defaultif) {
@file_put_contents("/tmp/{$defaultif}_defaultgw", $gateway['gateway']);
}
}
$foundgw = true;
} else if ($gateway['ipprotocol'] == "inet6" && (is_ipaddrv6($gateway['gateway']) || $gateway['gateway'] == "dynamic")) {
if ($gateway['gateway'] == "dynamic") {
$gateway['gateway'] = get_interface_gateway_v6($gateway['interface']);
}
$gatewayipv6 = $gateway['gateway'];
$interfacegwv6 = $gateway['interface'];
if (!empty($gateway['interface'])) {
$defaultifv6 = get_real_interface($gateway['interface']);
if ($defaultifv6) {
@file_put_contents("/tmp/{$defaultifv6}_defaultgwv6", $gateway['gateway']);
}
}
$foundgwv6 = true;
}
}
if ($foundgw === true && $foundgwv6 === true) {
break;
}
}
}
if (!$foundgw) {
$defaultif = get_real_interface("wan");
$interfacegw = "wan";
$gatewayip = get_interface_gateway("wan");
@touch("/tmp/{$defaultif}_defaultgw");
}
if (!$foundgwv6) {
$defaultifv6 = get_real_interface("wan");
$interfacegwv6 = "wan";
$gatewayipv6 = get_interface_gateway_v6("wan");
@touch("/tmp/{$defaultif}_defaultgwv6");
}
if (!empty($interface) && $interface != $interfacegw)
;
elseif (is_ipaddrv4($gatewayip)) {
log_error("ROUTING: remove current default route to $gatewayip");
mwexec("/sbin/route delete default");
log_error("ROUTING: setting default route to $gatewayip");
mwexec("/sbin/route add -inet default " . escapeshellarg($gatewayip));
}
if (!empty($interface) && $interface != $interfacegwv6)
;
elseif (is_ipaddrv6($gatewayipv6)) {
$ifscope = "";
if (is_linklocal($gatewayipv6)) {
$ifscope = "%{$defaultifv6}";
}
log_error("ROUTING: setting IPv6 default route to {$gatewayipv6}{$ifscope}");
mwexec("/sbin/route delete -inet6 default " . escapeshellarg("{$gatewayipv6}{$ifscope}"));
mwexec("/sbin/route add -inet6 default " . escapeshellarg("{$gatewayipv6}{$ifscope}"));
}
system_staticroutes_configure($interface, false);
return 0;
}
/* Compare the current hostname DNS to the DNS cache we made
* if it has changed we return the old records
* if no change we return false */
function compare_hostname_to_dnscache($hostname) {
if(!is_dir("/var/db/dnscache")) {
mkdir("/var/db/dnscache");
}
$hostname = trim($hostname);
if(is_readable("/var/db/dnscache/{$hostname}")) {
$oldcontents = file_get_contents("/var/db/dnscache/{$hostname}");
} else {
$oldcontents = "";
}
if((is_fqdn($hostname)) && (!is_ipaddr($hostname))) {
$domrecords = array();
$domips = array();
exec("host -t A " . escapeshellarg($hostname), $domrecords, $rethost);
if($rethost == 0) {
foreach($domrecords as $domr) {
$doml = explode(" ", $domr);
$domip = $doml[3];
/* fill array with domain ip addresses */
if(is_ipaddr($domip)) {
$domips[] = $domip;
}
}
}
sort($domips);
$contents = "";
if(! empty($domips)) {
foreach($domips as $ip) {
$contents .= "$ip\n";
}
}
}
if(trim($oldcontents) != trim($contents)) {
log_error(sprintf(gettext('DNSCACHE: Found old IP %s and new IP %s'), $oldcontents, $contents));
return ($oldcontents);
} else {
return false;
}
}
function system_staticroutes_configure($interface = '', $update_dns = false)
{
global $config, $aliastable;
$filterdns_list = array();
$static_routes = get_staticroutes(false, true);
if (count($static_routes)) {
$gateways_arr = return_gateways_array(false, true);
foreach ($static_routes as $rtent) {
if (empty($gateways_arr[$rtent['gateway']])) {
log_error(sprintf(gettext("Static Routes: Gateway IP could not be found for %s"), $rtent['network']));
continue;
}
$gateway = $gateways_arr[$rtent['gateway']];
if (!empty($interface) && $interface != $gateway['friendlyiface']) {
continue;
}
$gatewayip = $gateway['gateway'];
$interfacegw = $gateway['interface'];
$blackhole = "";
if (!strcasecmp("Null", substr($rtent['gateway'], 0, 3))) {
$blackhole = "-blackhole";
}
if (!is_fqdn($rtent['network']) && !is_subnet($rtent['network'])) {
continue;
}
$dnscache = array();
if ($update_dns === true) {
if (is_subnet($rtent['network'])) {
continue;
}
$dnscache = explode("\n", trim(compare_hostname_to_dnscache($rtent['network'])));
if (empty($dnscache)) {
continue;
}
}
if (is_subnet($rtent['network'])) {
$ips = array($rtent['network']);
} else {
if (!isset($rtent['disabled'])) {
$filterdns_list[] = $rtent['network'];
}
$ips = add_hostname_to_watch($rtent['network']);
}
foreach ($dnscache as $ip) {
if (in_array($ip, $ips)) {
continue;
}
mwexec("/sbin/route delete " . escapeshellarg($ip), true);
}
if (isset($rtent['disabled'])) {
/* XXX: This is a bit dangerous in case of routing daemons!? */
foreach ($ips as $ip) {
mwexec("/sbin/route delete " . escapeshellarg($ip), true);
}
continue;
}
foreach ($ips as $ip) {
if (is_ipaddrv4($ip)) {
$ip .= "/32";
} elseif (is_ipaddrv6($ip)) {
$ip .= "/128";
}
$inet = (is_subnetv6($ip) ? "-inet6" : "-inet");
$cmd = " {$inet} {$blackhole} " . escapeshellarg($ip) . " ";
if (is_subnet($ip)) {
if (is_ipaddr($gatewayip)) {
mwexec("/sbin/route delete".$cmd . escapeshellarg($gatewayip));
mwexec("/sbin/route add".$cmd . escapeshellarg($gatewayip));
} elseif (!empty($interfacegw)) {
mwexec("/sbin/route delete".$cmd . "-iface " . escapeshellarg($interfacegw));
mwexec("/sbin/route add".$cmd . "-iface " . escapeshellarg($interfacegw));
}
}
}
}
unset($gateways_arr);
}
unset($static_routes);
if ($update_dns === false) {
if (count($filterdns_list)) {
$interval = 60;
$hostnames = "";
array_unique($filterdns_list);
foreach ($filterdns_list as $hostname) {
$hostnames .= "cmd {$hostname} '/usr/local/opnsense/service/configd_ctl.py routedns reload'\n";
}
file_put_contents("/var/etc/filterdns-route.hosts", $hostnames);
unset($hostnames);
if (isvalidpid('/var/run/filterdns-route.pid')) {
killbypid('/var/run/filterdns-route.pid', 'HUP');
} else {
mwexec("/usr/local/sbin/filterdns -p /var/run/filterdns-route.pid -i {$interval} -c /var/etc/filterdns-route.hosts -d 1");
}
} else {
killbypid('/var/run/filterdns-route.pid');
}
}
unset($filterdns_list);
return 0;
}
function system_routing_enable()
{
global $config;
set_sysctl(array(
"net.inet.ip.forwarding" => "1",
"net.inet6.ip6.forwarding" => "1"
));
}
function system_syslogd_fixup_server($server) {
/* If it's an IPv6 IP alone, encase it in brackets */
if (is_ipaddrv6($server)) {
return "[$server]";
} else {
return $server;
}
}
function system_syslogd_get_remote_servers($syslogcfg, $facility = "*.*") {
// Rather than repeatedly use the same code, use this function to build a list of remote servers.
$facility .= " ".
$remote_servers = "";
$pad_to = 56;
$padding = ceil(($pad_to - strlen($facility))/8)+1;
if(!empty($syslogcfg['remoteserver'])) {
$remote_servers .= "{$facility}" . str_repeat("\t", $padding) . "@" . system_syslogd_fixup_server($syslogcfg['remoteserver']) . "\n";
}
if(!empty($syslogcfg['remoteserver2'])) {
$remote_servers .= "{$facility}" . str_repeat("\t", $padding) . "@" . system_syslogd_fixup_server($syslogcfg['remoteserver2']) . "\n";
}
if(!empty($syslogcfg['remoteserver3'])) {
$remote_servers .= "{$facility}" . str_repeat("\t", $padding) . "@" . system_syslogd_fixup_server($syslogcfg['remoteserver3']) . "\n";
}
return $remote_servers;
}
function system_syslogd_start()
{
global $config, $g;
$retval = null;
/* XXX temporary hook for newsyslog.conf regeneration */
configd_run('template reload OPNsense.Syslog');
mwexec('/etc/rc.d/hostid start');
$syslogcfg = $config['syslog'];
if (file_exists('/var/run/booting')) {
echo gettext('Starting syslog...');
}
$log_directive = '%';
$syslogd_extra = '';
if (isset($syslogcfg)) {
$syslogconf = '';
// create structure with log section definitions and config tags for remote usage
$syslogconfs = array();
$syslogconfs['routing'] = array("facility" => array('radvd', 'routed', 'olsrd', 'zebra', 'ospfd', 'bgpd', 'miniupnpd') , "remote" => null);
$syslogconfs['ntpd'] = array("facility" => array('ntp', 'ntpd', 'ntpdate'), "remote" => null);
$syslogconfs['ppps'] = array("facility" => array('ppp'), "remote" => null);
$syslogconfs['ipsec'] = array("facility" => array('charon'), "remote" => null);
$syslogconfs['openvpn'] = array("facility" => array('openvpn'), "remote" => "vpn");
$syslogconfs['gateways'] = array("facility" => array('apinger'), "remote" => "apinger");
$syslogconfs['resolver'] = array("facility" => array('dnsmasq','filterdns','unbound'), "remote" => null);
$syslogconfs['dhcpd'] = array("facility" => array('dhcpd','dhcrelay','dhclient','dhcp6c'), "remote" => "dhcp");
$syslogconfs['relayd'] = array("facility" => array('relayd'), "remote" => "relayd");
$syslogconfs['wireless'] = array("facility" => array('hostapd'), "remote" => "hostapd");
$syslogconfs['filter'] = array("facility" => array('filterlog'), "remote" => "filter");
$syslogconfs['portalauth'] = array("facility" => array('captiveportal'), "remote" => "portalauth");
// probe syslog facilities in plugins
foreach (plugin_scan() as $name => $path) {
require_once $path;
$func = sprintf('%s_syslog', $name);
if (function_exists($func)) {
$workers = $func();
foreach ($workers as $plugin_syslog => $plugin_details) {
if (!array_key_exists($plugin_syslog, $syslogconfs)) {
$syslogconfs[$plugin_syslog] = array();
$syslogconfs[$plugin_syslog]['facility'] = array();
$syslogconfs[$plugin_syslog]['remote'] = $plugin_details['remote'];
}
$tmp = array_merge($syslogconfs[$plugin_syslog]['facility'], $plugin_details['facility']);
$syslogconfs[$plugin_syslog]['facility'] = $tmp;
}
}
}
$separatelogfacilities = array();
foreach ($syslogconfs as $logTopic => $logConfig) {
$syslogconf .= "!".implode(',', $logConfig['facility'])."\n";
$separatelogfacilities = array_merge($logConfig['facility'], $separatelogfacilities);
if (!isset($syslogcfg['disablelocallogging'])) {
$syslogconf .= "*.* {$log_directive}/var/log/{$logTopic}.log\n";
}
if ($logConfig['remote'] != null && !empty($syslogcfg[$logConfig['remote']]) && !empty($syslogcfg['enable'])) {
$syslogconf .= system_syslogd_get_remote_servers($syslogcfg, "*.*");
}
}
asort($separatelogfacilities);
$facilitylist = implode(',', array_unique($separatelogfacilities));
$syslogconf .= "!-{$facilitylist}\n";
if (!isset($syslogcfg['disablelocallogging'])) {
$syslogconf .= <<<EOD
local3.* {$log_directive}/var/log/vpn.log
local7.* {$log_directive}/var/log/dhcpd.log
*.notice;kern.debug;lpr.info;mail.crit;daemon.none; {$log_directive}/var/log/system.log
news.err;local0.none;local3.none;local4.none; {$log_directive}/var/log/system.log
local7.none {$log_directive}/var/log/system.log
security.* {$log_directive}/var/log/system.log
auth.info;authpriv.info;daemon.info {$log_directive}/var/log/system.log
auth.info;authpriv.info |exec /usr/local/sbin/sshlockout_pf 15
*.emerg *
EOD;
}
if (!empty($syslogcfg['enable'])) {
if (isset($syslogcfg['vpn'])) {
$syslogconf .= system_syslogd_get_remote_servers($syslogcfg, "local3.*");
}
if (isset($syslogcfg['portalauth'])) {
$syslogconf .= system_syslogd_get_remote_servers($syslogcfg, "local4.*");
}
if (isset($syslogcfg['dhcp'])) {
$syslogconf .= system_syslogd_get_remote_servers($syslogcfg, "local7.*");
}
if (isset($syslogcfg['system'])) {
$syslogconf .= system_syslogd_get_remote_servers($syslogcfg, "*.notice;kern.debug;lpr.info;mail.crit;");
$syslogconf .= system_syslogd_get_remote_servers($syslogcfg, "news.err;local0.none;local3.none;local7.none");
$syslogconf .= system_syslogd_get_remote_servers($syslogcfg, "security.*");
$syslogconf .= system_syslogd_get_remote_servers($syslogcfg, "auth.info;authpriv.info;daemon.info");
$syslogconf .= system_syslogd_get_remote_servers($syslogcfg, "*.emerg");
}
if (isset($syslogcfg['logall'])) {
// Make everything mean everything, including facilities excluded above.
$syslogconf .= "!*\n";
$syslogconf .= system_syslogd_get_remote_servers($syslogcfg, "*.*");
}
}
/* write syslog.conf */
if (!@file_put_contents("/var/etc/syslog.conf", $syslogconf)) {
printf(gettext("Error: cannot open syslog.conf in system_syslogd_start().%s"), "\n");
unset($syslogconf);
return 1;
}
unset($syslogconf);
// Ensure that the log directory exists
if (!is_dir("{$g['dhcpd_chroot_path']}/var/run")) {
exec("/bin/mkdir -p {$g['dhcpd_chroot_path']}/var/run");
}
$sourceip = "";
if (!empty($syslogcfg['sourceip'])) {
if ($syslogcfg['ipproto'] == "ipv6") {
$ifaddr = is_ipaddr($syslogcfg['sourceip']) ? $syslogcfg['sourceip'] : get_interface_ipv6($syslogcfg['sourceip']);
if (!is_ipaddr($ifaddr)) {
$ifaddr = get_interface_ip($syslogcfg['sourceip']);
}
} else {
$ifaddr = is_ipaddr($syslogcfg['sourceip']) ? $syslogcfg['sourceip'] : get_interface_ip($syslogcfg['sourceip']);
if (!is_ipaddr($ifaddr)) {
$ifaddr = get_interface_ipv6($syslogcfg['sourceip']);
}
}
if (is_ipaddr($ifaddr)) {
$sourceip = "-b {$ifaddr}";
}
}
$syslogd_extra = "-f /var/etc/syslog.conf {$sourceip}";
// setup log files for all facilities including default
$default_logfile_size = !empty($syslogcfg['logfilesize']) ? $syslogcfg['logfilesize'] : '511488';
$syslog_files = array_keys($syslogconfs);
$syslog_files = array_merge($syslog_files, array('system', 'vpn', 'lighttpd'));
foreach ($syslog_files as $syslog_fn) {
$filename = "/var/log/".basename($syslog_fn).".log";
if (!file_exists($filename)) {
mwexecf('/usr/local/sbin/clog -i -s %s %s', array($default_logfile_size, $filename));
}
mwexecf('chmod 0600 %s', array($filename));
}
}
if (isvalidpid('/var/run/syslog.pid')) {
killbypid('/var/run/syslog.pid', 'HUP');
} else {
$retval = mwexec_bg("/usr/local/sbin/syslogd -s -c -c -l {$g['dhcpd_chroot_path']}/var/run/log -P /var/run/syslog.pid {$syslogd_extra}");
}
if (file_exists("/var/run/booting")) {
echo gettext("done.") . "\n";
}
return $retval;
}
function system_webgui_start()
{
global $config;
chdir('/usr/local/www');
/* defaults */
$portarg = "80";
$crt = "";
$key = "";
$ca = "";
/* non-standard port? */
if (isset($config['system']['webgui']['port']) && $config['system']['webgui']['port'] <> "") {
$portarg = "{$config['system']['webgui']['port']}";
}
if ($config['system']['webgui']['protocol'] == "https") {
// Ensure that we have a webConfigurator CERT
$cert =& lookup_cert($config['system']['webgui']['ssl-certref']);
if(!is_array($cert) && !$cert['crt'] && !$cert['prv']) {
if (!is_array($config['ca'])) {
$config['ca'] = array();
}
$a_ca =& $config['ca'];
if (!is_array($config['cert'])) {
$config['cert'] = array();
}
$a_cert =& $config['cert'];
log_error("Creating SSL Certificate for this host");
$cert = array();
$cert['refid'] = uniqid();
$cert['descr'] = gettext("webConfigurator default");
mwexec(
/* XXX ought to be replaced by PHP calls */
'/usr/local/bin/openssl req -new ' .
'-newkey rsa:4096 -sha256 -days 365 -nodes -x509 ' .
'-subj "/C=NL/ST=Zuid-Holland/L=Middelharnis/O=OPNsense" ' .
'-keyout /tmp/ssl.key -out /tmp/ssl.crt'
);
$crt = file_get_contents('/tmp/ssl.crt');