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
|
<?php
/* $Id$ */
/*
system.inc
part of m0n0wall (http://m0n0.ch/wall)
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.
*/
/* include all configuration functions */
require_once("functions.inc");
function system_resolvconf_generate($dynupdate = false) {
global $config, $g;
$syscfg = $config['system'];
$fd = fopen("{$g['varetc_path']}/resolv.conf", "w");
if (!$fd) {
printf("Error: cannot open resolv.conf in system_resolvconf_generate().\n");
return 1;
}
$resolvconf = "domain {$syscfg['domain']}\n";
$havedns = false;
if (isset($syscfg['dnsallowoverride'])) {
/* get dynamically assigned DNS servers (if any) */
$nfd = @fopen("{$g['varetc_path']}/nameservers.conf", "r");
if ($nfd) {
while (!feof($nfd)) {
$dnss = trim(fgets($nfd));
if ($dnss) {
$resolvconf .= "nameserver $dnss\n";
$havedns = true;
}
}
fclose($nfd);
}
$nfd = @fopen("{$g['varetc_path']}/defaultdomain.conf", "r");
if ($nfd and $havedns) {
while (!feof($nfd)) {
$dnss = trim(fgets($nfd));
if ($dnss) {
$resolvconf .= "search $dnss\n";
}
}
fclose($nfd);
}
}
if (!$havedns && is_array($syscfg['dnsserver'])) {
foreach ($syscfg['dnsserver'] as $ns) {
if ($ns) {
$resolvconf .= "nameserver $ns\n";
$havedns = true;
}
}
}
$ns = str_replace("nameserver nameserver ", "nameserver ", $resolvconf);
fwrite($fd, $resolvconf);
fclose($fd);
if (!$g['booting']) {
/* restart dhcpd (nameservers may have changed) */
if (!$dynupdate)
services_dhcpd_configure();
}
return 0;
}
function system_hosts_generate() {
global $config, $g;
$syscfg = $config['system'];
$lancfg = $config['interfaces']['lan'];
$dnsmasqcfg = $config['dnsmasq'];
if (!is_array($dnsmasqcfg['hosts'])) {
$dnsmasqcfg['hosts'] = array();
}
$hostscfg = $dnsmasqcfg['hosts'];
$fd = fopen("{$g['varetc_path']}/hosts", "w");
if (!$fd) {
printf("Error: cannot open hosts file in system_hosts_generate().\n");
return 1;
}
$hosts = <<<EOD
127.0.0.1 localhost localhost.{$syscfg['domain']}
{$lancfg['ipaddr']} {$syscfg['hostname']}.{$syscfg['domain']} {$syscfg['hostname']}
EOD;
foreach ($hostscfg as $host) {
if ($host['host'])
$hosts .= "{$host['ip']} {$host['host']}.{$host['domain']} {$host['host']}\n";
else
$hosts .= "{$host['ip']} {$host['domain']}\n";
}
fwrite($fd, $hosts);
fclose($fd);
return 0;
}
function system_hostname_configure() {
global $config, $g;
$syscfg = $config['system'];
/* set hostname */
return mwexec("/bin/hostname " .
escapeshellarg("{$syscfg['hostname']}.{$syscfg['domain']}"));
}
function system_routing_configure() {
global $config, $g;
/* Enable fast routing, if enabled */
if(isset($config['staticroutes']['enablefastrouting']))
mwexec("/sbin/sysctl net.inet.ip.fastforwarding=1");
/* clear out old routes, if necessary */
if (file_exists("{$g['vardb_path']}/routes.db")) {
$fd = fopen("{$g['vardb_path']}/routes.db", "r");
if (!$fd) {
printf("Error: cannot open routes DB file in system_routing_configure().\n");
return 1;
}
while (!feof($fd)) {
$oldrt = fgets($fd);
if ($oldrt)
mwexec("/sbin/route delete " . escapeshellarg($oldrt));
}
fclose($fd);
unlink("{$g['vardb_path']}/routes.db");
}
if (is_array($config['staticroutes']['route'])) {
$fd = fopen("{$g['vardb_path']}/routes.db", "w");
if (!$fd) {
printf("Error: cannot open routes DB file in system_routing_configure().\n");
return 1;
}
foreach ($config['staticroutes']['route'] as $rtent) {
mwexec("/sbin/route add " . escapeshellarg($rtent['network']) .
" " . escapeshellarg($rtent['gateway']));
/* record route so it can be easily removed later (if necessary) */
fwrite($fd, $rtent['network'] . "\n");
}
fclose($fd);
}
return 0;
}
function system_routing_enable() {
global $config, $g;
return mwexec("/sbin/sysctl net.inet.ip.forwarding=1");
}
function system_syslogd_start() {
global $config, $g;
$syslogcfg = $config['syslog'];
if ($g['booting'])
echo "Starting syslog... ";
else
killbypid("{$g['varrun_path']}/syslog.pid");
if (isset($syslogcfg)) {
if($config['installedpackages']['package']) {
foreach($config['installedpackages']['package'] as $package) {
if($package['logging']) {
$pkgfacilities[] = $package['logging']['facilityname'];
$syslogconf .= '!' . $package['logging']['facilityname'] . "\n*.*\t\t\t\t\t\t%{$g['varlog_path']}/" . $package['logging']['logfilename'] . "\n";
}
}
if(is_array($pkgfacilities)) $syslogconf .= '!-' . implode(',', $pkgfacilities) . "\n";
}
/* write syslog.conf */
$fd = fopen("{$g['varetc_path']}/syslog.conf", "w");
if (!$fd) {
printf("Error: cannot open syslog.conf in system_syslogd_start().\n");
return 1;
}
if (!isset($syslogcfg['disablelocallogging'])) {
$syslogconf .= <<<EOD
local0.* %{$g['varlog_path']}/filter.log
local3.* %{$g['varlog_path']}/vpn.log
local4.* %{$g['varlog_path']}/portalauth.log
local7.* %{$g['varlog_path']}/dhcpd.log
*.notice;kern.debug;lpr.info;mail.crit; %{$g['varlog_path']}/system.log
news.err;local0.none;local3.none;local4.none; %{$g['varlog_path']}/system.log
local7.none %{$g['varlog_path']}/system.log
security.* %{$g['varlog_path']}/system.log
auth.info;authpriv.info;daemon.info %{$g['varlog_path']}/system.log
local1.* %{$g['varlog_path']}/slbd.log
*.emerg *
EOD;
}
if (isset($syslogcfg['filter'])) {
$syslogconf .= <<<EOD
local0.* @{$syslogcfg['remoteserver']}
EOD;
}
if (isset($syslogcfg['vpn'])) {
$syslogconf .= <<<EOD
local3.* @{$syslogcfg['remoteserver']}
EOD;
}
if (isset($syslogcfg['portalauth'])) {
$syslogconf .= <<<EOD
local4.* @{$syslogcfg['remoteserver']}
EOD;
}
if (isset($syslogcfg['dhcp'])) {
$syslogconf .= <<<EOD
local7.* @{$syslogcfg['remoteserver']}
EOD;
}
if (isset($syslogcfg['system'])) {
$syslogconf .= <<<EOD
*.notice;kern.debug;lpr.info;mail.crit; @{$syslogcfg['remoteserver']}
news.err;local0.none;local3.none;local7.none @{$syslogcfg['remoteserver']}
security.* @{$syslogcfg['remoteserver']}
auth.info;authpriv.info;daemon.info @{$syslogcfg['remoteserver']}
*.emerg @{$syslogcfg['remoteserver']}
EOD;
}
fwrite($fd, $syslogconf);
fclose($fd);
$retval = mwexec("/usr/sbin/syslogd -s -f {$g['varetc_path']}/syslog.conf");
} else {
$retval = mwexec("/usr/sbin/syslogd -ss");
}
if ($g['booting'])
echo "done.\n";
return $retval;
}
function system_pccard_start() {
global $config, $g;
if ($g['booting'])
echo "Initializing PCMCIA... ";
/* kill any running pccardd */
killbypid("{$g['varrun_path']}/pccardd.pid");
/* fire up pccardd */
$res = mwexec("/usr/sbin/pccardd -z -f {$g['etc_path']}/pccard.conf");
if ($g['booting']) {
if ($res == 0)
echo "done.\n";
else
echo "failed!\n";
}
return $res;
}
function system_webgui_start() {
global $config, $g;
if ($g['booting'])
echo "Starting webGUI... ";
/* kill any running mini_httpd */
killbypid("{$g['varrun_path']}/mini_httpd.pid");
/* generate password file */
system_password_configure();
chdir($g['www_path']);
/* non-standard port? */
if ($config['system']['webgui']['port'])
$portarg = "-p {$config['system']['webgui']['port']}";
else
$portarg = "";
if ($config['system']['webgui']['protocol'] == "https") {
if ($config['system']['webgui']['certificate'] && $config['system']['webgui']['private-key']) {
$cert = base64_decode($config['system']['webgui']['certificate']);
$key = base64_decode($config['system']['webgui']['private-key']);
} else {
/* default certificate/key */
$cert = <<<EOD
-----BEGIN CERTIFICATE-----
MIIC4zCCAkygAwIBAgIBADANBgkqhkiG9w0BAQQFADBbMQswCQYDVQQGEwJOQTEL
MAkGA1UECBMCTkExCzAJBgNVBAcTAk5BMQswCQYDVQQKEwJOQTELMAkGA1UECxMC
TkExCzAJBgNVBAMTAk5BMQswCQYDVQQGEwJVUzAeFw0wNTAzMDYwMDE1NDJaFw0x
NTAzMDQwMDE1NDJaMFsxCzAJBgNVBAYTAk5BMQswCQYDVQQIEwJOQTELMAkGA1UE
BxMCTkExCzAJBgNVBAoTAk5BMQswCQYDVQQLEwJOQTELMAkGA1UEAxMCTkExCzAJ
BgNVBAYTAlVTMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDF7luuy70OvHrl
xnW9ID6srsfxEFCF4d9LmlZ6XdW1rEUHQ6KTgz4iSD+pxEOxxlY+bCH6HTkAy5Sa
zt3eT7javvF+ILZgarwoY2x+NbDctd0VBJVkH0fEvBf1xqU7wpkOiWkw1RmfEvZI
6XnGi6VSjSmkm0UoQMKg9R7niRtE4QIDAQABo4G2MIGzMB0GA1U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2R4DtKZn
LbSzUvYjA9z1holDWRoLtycTu2mFNuRbuZC9mqR40/ye/CgdCzdmUagt0QJBAKq1
00ySINd10Cive+yTwMPQIj2CGbpbbbq/hYyMntBWapQmZRFHOYZmkrZeFBGGeQ5u
QJdipiIyivNY2+nxKZECQCvumJPfZYxCeCAEC+G2xezrP6bC6FhzUOw6410UARTM
fuFjHpSfOiG62lfRdZgCPAr1L/1pJF+8RqjGlFfAuFA=
-----END RSA PRIVATE KEY-----
EOD;
}
$fd = fopen("{$g['varetc_path']}/cert.pem", "w");
if (!$fd) {
printf("Error: cannot open cert.pem in system_webgui_start().\n");
return 1;
}
chmod("{$g['varetc_path']}/cert.pem", 0600);
fwrite($fd, $cert);
fwrite($fd, "\n");
fwrite($fd, $key);
fclose($fd);
$res = mwexec("/usr/local/sbin/mini_httpd -S -E {$g['varetc_path']}/cert.pem" .
" -c \"**.php|**.cgi\" -u root -maxproc 16 $portarg" .
" -i {$g['varrun_path']}/mini_httpd.pid");
} else {
$res = mwexec("/usr/local/sbin/mini_httpd -c \"**.php|**.cgi\" -u root" .
" -maxproc 16 $portarg -i {$g['varrun_path']}/mini_httpd.pid");
}
if ($g['booting']) {
if ($res == 0)
echo "done.\n";
else
echo "failed!\n";
}
return $res;
}
function system_password_configure() {
global $config, $g;
$fd = fopen("{$g['varrun_path']}/htpasswd", "w");
if (!$fd) {
printf("Error: cannot open htpasswd in system_password_configure().\n");
return 1;
}
if ($config['system']['username'])
$username = $config['system']['username'];
else
$username = "admin";
fwrite($fd, $username . ":" . $config['system']['password'] . "\n");
fclose($fd);
chmod("{$g['varrun_path']}/htpasswd", 0600);
return 0;
}
function system_timezone_configure() {
global $config, $g;
$syscfg = $config['system'];
if ($g['booting'])
echo "Setting timezone... ";
/* extract appropriate timezone file */
$timezone = $syscfg['timezone'];
if (!$timezone)
$timezone = "Etc/UTC";
conf_mount_rw();
exec("/usr/bin/tar xzfO /usr/share/zoneinfo.tgz " .
escapeshellarg($timezone) . " > /etc/localtime");
conf_mount_ro();
if ($g['booting'])
echo "done.\n";
}
function system_ntp_configure() {
global $config, $g;
$syscfg = $config['system'];
if ($g['booting'])
echo "Starting NTP client... ";
else {
killbypid("{$g['varrun_path']}/runmsntp.pid");
killbypid("{$g['varrun_path']}/msntp.pid");
}
/* start ntp client if needed - needs to be forced into background */
$updateinterval = $syscfg['time-update-interval'];
if ($updateinterval > 0) {
if ($updateinterval < 6)
$updateinterval = 6;
$timeservers = "";
foreach (explode(' ', $syscfg['timeservers']) as $ts)
$timeservers .= " " . $ts;
mwexec_bg("/usr/local/bin/runmsntp.sh " .
escapeshellarg("{$g['varrun_path']}/runmsntp.pid") . " " .
escapeshellarg("{$g['varrun_path']}/msntp.pid") . " " .
escapeshellarg($updateinterval) . " " .
escapeshellarg($timeservers));
}
if ($g['booting'])
echo "done.\n";
}
function system_halt() {
global $g;
system_reboot_cleanup();
mwexec("nohup /etc/rc.halt > /dev/null 2>&1 &");
}
function system_reboot() {
global $g;
system_reboot_cleanup();
mwexec("nohup /etc/rc.reboot > /dev/null 2>&1 &");
}
function system_reboot_sync() {
global $g;
system_reboot_cleanup();
mwexec("/etc/rc.reboot > /dev/null 2>&1");
}
function system_reboot_cleanup() {
captiveportal_radius_stop_all();
}
function system_do_shell_commands($early = 0) {
global $config, $g;
if ($early)
$cmdn = "earlyshellcmd";
else
$cmdn = "shellcmd";
if (is_array($config['system'][$cmdn])) {
foreach ($config['system'][$cmdn] as $cmd) {
exec($cmd);
}
}
}
function system_do_extensions($early = false) {
global $config, $g;
if (!is_dir("{$g['etc_path']}/inc/ext"))
return;
$dh = @opendir("{$g['etc_path']}/inc/ext");
if ($dh) {
while (($extd = readdir($dh)) !== false) {
if (($extd === ".") || ($extd === ".."))
continue;
$rcfile = "{$g['etc_path']}/inc/ext/" . $extd . "/" . ($early ? "rc.early" : "rc");
if (file_exists($rcfile))
passthru($rcfile);
}
closedir($dh);
}
}
function system_console_configure() {
global $config, $g;
if (isset($config['system']['disableconsolemenu'])) {
touch("{$g['varetc_path']}/disableconsole");
} else {
unlink_if_exists("{$g['varetc_path']}/disableconsole");
}
}
function system_dmesg_save() {
global $g;
exec("/sbin/dmesg", $dmesg);
/* find last copyright line (output from previous boots may be present) */
$lastcpline = 0;
for ($i = 0; $i < count($dmesg); $i++) {
if (strstr($dmesg[$i], "Copyright (c) 1992-"))
$lastcpline = $i;
}
$fd = fopen("{$g['varlog_path']}/dmesg.boot", "w");
if (!$fd) {
printf("Error: cannot open dmesg.boot in system_dmesg_save().\n");
return 1;
}
for ($i = $lastcpline; $i < count($dmesg); $i++)
fwrite($fd, $dmesg[$i] . "\n");
fclose($fd);
return 0;
}
function system_set_harddisk_standby() {
global $g, $config;
if ($g['platform'] != "generic-pc")
return;
if (isset($config['system']['harddiskstandby'])) {
if ($g['booting']) {
echo 'Setting hard disk standby... ';
}
$standby = $config['system']['harddiskstandby'];
// Check for a numeric value
if (is_numeric($standby)) {
// Sync the disk(s)
mwexec('/bin/sync');
if (!mwexec('/sbin/sysctl hw.ata.standby=' . ((int)$standby))) {
// Reinitialize ATA-drives
mwexec('/usr/local/sbin/atareinit');
if ($g['booting']) {
echo "done.\n";
}
} else if ($g['booting']) {
echo "failed!\n";
}
} else if ($g['booting']) {
echo "failed!\n";
}
}
}
function system_setup_sysctl() {
$sysctl = return_filename_as_array("/etc/sysctl.conf");
foreach($sysctl as $sysc) {
if($sysc <> "")
mwexec("sysctl {$sysc}");
}
}
?>
|