1
|
<?php
|
2
|
/****h* pfSense/pfsense-utils
|
3
|
* NAME
|
4
|
* pfsense-utils.inc - Utilities specific to pfSense
|
5
|
* DESCRIPTION
|
6
|
* This include contains various pfSense specific functions.
|
7
|
* HISTORY
|
8
|
* $Id$
|
9
|
******
|
10
|
*
|
11
|
* Copyright (C) 2005 Scott Ullrich (sullrich@gmail.com)
|
12
|
* All rights reserved.
|
13
|
* Redistribution and use in source and binary forms, with or without
|
14
|
* modification, are permitted provided that the following conditions are met:
|
15
|
*
|
16
|
* 1. Redistributions of source code must retain the above copyright notice,
|
17
|
* this list of conditions and the following disclaimer.
|
18
|
*
|
19
|
* 2. Redistributions in binary form must reproduce the above copyright
|
20
|
* notice, this list of conditions and the following disclaimer in the
|
21
|
* documentation and/or other materials provided with the distribution.
|
22
|
*
|
23
|
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
24
|
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
|
25
|
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
26
|
* AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
|
27
|
* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
28
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
29
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
30
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
31
|
* RISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
32
|
* POSSIBILITY OF SUCH DAMAGE.
|
33
|
*
|
34
|
*/
|
35
|
|
36
|
/****f* pfsense-utils/log_error
|
37
|
* NAME
|
38
|
* log_error - Sends a string to syslog.
|
39
|
* INPUTS
|
40
|
* $error - string containing the syslog message.
|
41
|
* RESULT
|
42
|
* null
|
43
|
******/
|
44
|
function log_error($error) {
|
45
|
syslog(LOG_WARNING, $error);
|
46
|
return;
|
47
|
}
|
48
|
|
49
|
/****f* pfsense-utils/return_dir_as_array
|
50
|
* NAME
|
51
|
* return_dir_as_array - Return a directory's contents as an array.
|
52
|
* INPUTS
|
53
|
* $dir - string containing the path to the desired directory.
|
54
|
* RESULT
|
55
|
* $dir_array - array containing the directory's contents. This array will be empty if the path specified is invalid.
|
56
|
******/
|
57
|
function return_dir_as_array($dir) {
|
58
|
$dir_array = array();
|
59
|
if (is_dir($dir)) {
|
60
|
if ($dh = opendir($dir)) {
|
61
|
while (($file = readdir($dh)) !== false) {
|
62
|
$canadd = 0;
|
63
|
if($file == ".") $canadd = 1;
|
64
|
if($file == "..") $canadd = 1;
|
65
|
if($canadd == 0)
|
66
|
array_push($dir_array, $file);
|
67
|
}
|
68
|
closedir($dh);
|
69
|
}
|
70
|
}
|
71
|
return $dir_array;
|
72
|
}
|
73
|
|
74
|
/****f* pfsense-utils/enable_hardware_offloading
|
75
|
* NAME
|
76
|
* enable_hardware_offloading - Enable a NIC's supported hardware features.
|
77
|
* INPUTS
|
78
|
* $interface - string containing the physical interface to work on.
|
79
|
* RESULT
|
80
|
* null
|
81
|
* NOTES
|
82
|
* This function only supports the fxp driver's loadable microcode.
|
83
|
******/
|
84
|
function enable_hardware_offloading($interface) {
|
85
|
global $config;
|
86
|
global $g;
|
87
|
if($g['booting']) {
|
88
|
$supported_ints = array('fxp');
|
89
|
foreach($supported_ints as $int) {
|
90
|
if(stristr($interface,$int) != false) {
|
91
|
mwexec("/sbin/ifconfig $interface link0");
|
92
|
}
|
93
|
}
|
94
|
}
|
95
|
return;
|
96
|
}
|
97
|
|
98
|
/****f* pfsense-utils/setup_microcode
|
99
|
* NAME
|
100
|
* enumerates all interfaces and calls enable_hardware_offloading which
|
101
|
* enables a NIC's supported hardware features.
|
102
|
* INPUTS
|
103
|
*
|
104
|
* RESULT
|
105
|
* null
|
106
|
* NOTES
|
107
|
* This function only supports the fxp driver's loadable microcode.
|
108
|
******/
|
109
|
function setup_microcode() {
|
110
|
global $config;
|
111
|
if($ip == "") return;
|
112
|
$i = 0;
|
113
|
$ifdescrs = array('wan', 'lan');
|
114
|
for ($j = 1; isset($config['interfaces']['opt' . $j]); $j++) {
|
115
|
$ifdescrs['opt' . $j] = "opt" . $j;
|
116
|
}
|
117
|
foreach($ifdescrs as $if)
|
118
|
enable_hardware_offloading($if);
|
119
|
}
|
120
|
|
121
|
/****f* pfsense-utils/return_filename_as_array
|
122
|
* NAME
|
123
|
* return_filename_as_array - Return a file's contents as an array.
|
124
|
* INPUTS
|
125
|
* $filename - string containing the path to the desired file.
|
126
|
* $strip - array of characters to strip - default is '#'.
|
127
|
* RESULT
|
128
|
* $file - array containing the file's contents.
|
129
|
* NOTES
|
130
|
* This function strips lines starting with '#' and leading/trailing whitespace by default.
|
131
|
******/
|
132
|
function return_filename_as_array($filename, $strip = array('#')) {
|
133
|
if(file_exists($filename)) $file = file($filename);
|
134
|
if(is_array($file)) {
|
135
|
foreach($file as $line) $line = trim($line);
|
136
|
foreach($strip as $tostrip) $file = preg_grep("/^{$tostrip}/", $file, PREG_GREP_INVERT);
|
137
|
}
|
138
|
return $file;
|
139
|
}
|
140
|
|
141
|
/****f* pfsense-utils/get_carp_status
|
142
|
* NAME
|
143
|
* get_carp_status - Return whether CARP is enabled or disabled.
|
144
|
* RESULT
|
145
|
* boolean - true if CARP is enabled, false if otherwise.
|
146
|
******/
|
147
|
function get_carp_status() {
|
148
|
/* grab the current status of carp */
|
149
|
$status = `/sbin/sysctl net.inet.carp.allow | cut -d" " -f2`;
|
150
|
if(intval($status) == "0") return false;
|
151
|
return true;
|
152
|
}
|
153
|
|
154
|
/****f* pfsense-utils/return_filename_as_string
|
155
|
* NAME
|
156
|
* return_filename_as_string - Return a file's contents as a string.
|
157
|
* INPUTS
|
158
|
* $filename - string containing the path to the desired file.
|
159
|
* RESULT
|
160
|
* $tmp - string containing the file's contents.
|
161
|
******/
|
162
|
function return_filename_as_string($filename) {
|
163
|
if(file_exists($filename)) {
|
164
|
return file_get_contents($filename);
|
165
|
} else {
|
166
|
return false;
|
167
|
}
|
168
|
}
|
169
|
|
170
|
/****f* pfsense-utils/is_carp_defined
|
171
|
* NAME
|
172
|
* is_carp_defined - Return whether CARP is detected in the kernel.
|
173
|
* RESULT
|
174
|
* boolean - true if CARP is detected, false otherwise.
|
175
|
******/
|
176
|
function is_carp_defined() {
|
177
|
/* is carp compiled into the kernel and userland? */
|
178
|
$command = "/sbin/sysctl -a | grep carp";
|
179
|
$fd = popen($command . " 2>&1 ", "r");
|
180
|
if(!$fd) {
|
181
|
log_error("Warning, could not execute command {$command}");
|
182
|
return 0;
|
183
|
}
|
184
|
while(!feof($fd)) {
|
185
|
$tmp .= fread($fd,49);
|
186
|
}
|
187
|
fclose($fd);
|
188
|
|
189
|
if($tmp == "")
|
190
|
return false;
|
191
|
else
|
192
|
return true;
|
193
|
}
|
194
|
|
195
|
/****f* pfsense-utils/find_number_of_created_carp_interfaces
|
196
|
* NAME
|
197
|
* find_number_of_created_carp_interfaces - Return the number of CARP interfaces.
|
198
|
* RESULT
|
199
|
* $tmp - Number of currently created CARP interfaces.
|
200
|
******/
|
201
|
function find_number_of_created_carp_interfaces() {
|
202
|
$command = "/sbin/ifconfig | /usr/bin/grep \"carp*:\" | /usr/bin/wc -l";
|
203
|
$fd = popen($command . " 2>&1 ", "r");
|
204
|
if(!$fd) {
|
205
|
log_error("Warning, could not execute command {$command}");
|
206
|
return 0;
|
207
|
}
|
208
|
while(!feof($fd)) {
|
209
|
$tmp .= fread($fd,49);
|
210
|
}
|
211
|
fclose($fd);
|
212
|
$tmp = intval($tmp);
|
213
|
return $tmp;
|
214
|
}
|
215
|
|
216
|
/****f* pfsense-utils/link_ip_to_carp_interface
|
217
|
* NAME
|
218
|
* link_ip_to_carp_interface - Find where a CARP interface links to.
|
219
|
* INPUTS
|
220
|
* $ip
|
221
|
* RESULT
|
222
|
* $carp_ints
|
223
|
******/
|
224
|
function link_ip_to_carp_interface($ip) {
|
225
|
global $config;
|
226
|
if($ip == "") return;
|
227
|
$i = 0;
|
228
|
|
229
|
$ifdescrs = array('wan', 'lan');
|
230
|
for ($j = 1; isset($config['interfaces']['opt' . $j]); $j++) {
|
231
|
$ifdescrs['opt' . $j] = "opt" . $j;
|
232
|
}
|
233
|
|
234
|
$ft = split("\.", $ip);
|
235
|
$ft_ip = $ft[0] . "." . $ft[1] . "." . $ft[2] . ".";
|
236
|
|
237
|
$carp_ints = "";
|
238
|
$num_carp_ints = find_number_of_created_carp_interfaces();
|
239
|
foreach ($ifdescrs as $ifdescr => $ifname) {
|
240
|
for($x=0; $x<$num_carp_ints; $x++) {
|
241
|
$carp_int = "carp{$x}";
|
242
|
$carp_ip = find_interface_ip($carp_int);
|
243
|
$carp_ft = split("\.", $carp_ip);
|
244
|
$carp_ft_ip = $carp_ft[0] . "." . $carp_ft[1] . "." . $carp_ft[2] . ".";
|
245
|
$result = does_interface_exist($carp_int);
|
246
|
if($result <> true) break;
|
247
|
$interface = filter_opt_interface_to_real($ifname);
|
248
|
if($ft_ip == $carp_ft_ip)
|
249
|
if(stristr($carp_ints,$carp_int) == false)
|
250
|
$carp_ints .= " " . $carp_int;
|
251
|
}
|
252
|
}
|
253
|
return $carp_ints;
|
254
|
}
|
255
|
|
256
|
/****f* pfsense-utils/exec_command
|
257
|
* NAME
|
258
|
* exec_command - Execute a command and return a string of the result.
|
259
|
* INPUTS
|
260
|
* $command - String of the command to be executed.
|
261
|
* RESULT
|
262
|
* String containing the command's result.
|
263
|
* NOTES
|
264
|
* This function returns the command's stdout and stderr.
|
265
|
******/
|
266
|
function exec_command($command) {
|
267
|
$output = array();
|
268
|
exec($command . ' 2>&1 ', $output);
|
269
|
return(implode("\n", $output));
|
270
|
}
|
271
|
|
272
|
/*
|
273
|
* does_interface_exist($interface): return true or false if a interface is detected.
|
274
|
*/
|
275
|
function does_interface_exist($interface) {
|
276
|
$ints = exec_command("/sbin/ifconfig -l");
|
277
|
if(stristr($ints, $interface) !== false)
|
278
|
return true;
|
279
|
else
|
280
|
return false;
|
281
|
}
|
282
|
|
283
|
/*
|
284
|
* convert_ip_to_network_format($ip, $subnet): converts an ip address to network form
|
285
|
*/
|
286
|
function convert_ip_to_network_format($ip, $subnet) {
|
287
|
$ipsplit = split('[.]', $ip);
|
288
|
$string = $ipsplit[0] . "." . $ipsplit[1] . "." . $ipsplit[2] . ".0/" . $subnet;
|
289
|
return $string;
|
290
|
}
|
291
|
|
292
|
/*
|
293
|
* find_interface_ip($interface): return the interface ip (first found)
|
294
|
*/
|
295
|
function find_interface_ip($interface) {
|
296
|
if(does_interface_exist($interface) == false) return;
|
297
|
$ip = exec_command("/sbin/ifconfig {$interface} | /usr/bin/grep -w \"inet\" | /usr/bin/cut -d\" \" -f 2");
|
298
|
$ip = str_replace("\n","",$ip);
|
299
|
return $ip;
|
300
|
}
|
301
|
|
302
|
function guess_interface_from_ip($ipaddress) {
|
303
|
$ints = `/sbin/ifconfig -l`;
|
304
|
$ints_split = split(" ", $ints);
|
305
|
$ip_subnet_split = split("\.", $ipaddress);
|
306
|
$ip_subnet = $ip_subnet_split[0] . "." . $ip_subnet_split[1] . "." . $ip_subnet_split[2] . ".";
|
307
|
foreach($ints_split as $int) {
|
308
|
$ip = find_interface_ip($int);
|
309
|
$ip_split = split("\.", $ip);
|
310
|
$ip_tocheck = $ip_split[0] . "." . $ip_split[1] . "." . $ip_split[2] . ".";
|
311
|
if(stristr($ip_tocheck, $ip_subnet) != false) return $int;
|
312
|
}
|
313
|
}
|
314
|
|
315
|
function filter_opt_interface_to_real($opt) {
|
316
|
global $config;
|
317
|
return $config['interfaces'][$opt]['if'];
|
318
|
}
|
319
|
|
320
|
function filter_get_opt_interface_descr($opt) {
|
321
|
global $config;
|
322
|
return $config['interfaces'][$opt]['descr'];
|
323
|
}
|
324
|
|
325
|
function get_friendly_interface_list_as_array() {
|
326
|
global $config;
|
327
|
$ints = array();
|
328
|
$i = 0;
|
329
|
$ifdescrs = array('wan', 'lan');
|
330
|
for ($j = 1; isset($config['interfaces']['opt' . $j]); $j++) {
|
331
|
$ifdescrs['opt' . $j] = "opt" . $j;
|
332
|
}
|
333
|
$ifdescrs = get_interface_list();
|
334
|
foreach ($ifdescrs as $ifdescr => $ifname) {
|
335
|
array_push($ints,$ifdescr);
|
336
|
}
|
337
|
return $ints;
|
338
|
}
|
339
|
|
340
|
/*
|
341
|
* find_ip_interface($ip): return the interface where an ip is defined
|
342
|
*/
|
343
|
function find_ip_interface($ip) {
|
344
|
global $config;
|
345
|
$i = 0;
|
346
|
$ifdescrs = array('wan', 'lan');
|
347
|
for ($j = 1; isset($config['interfaces']['opt' . $j]); $j++) {
|
348
|
$ifdescrs['opt' . $j] = "opt" . $j;
|
349
|
}
|
350
|
foreach ($ifdescrs as $ifdescr => $ifname) {
|
351
|
$int = filter_translate_type_to_real_interface($ifname);
|
352
|
$ifconfig = exec_command("/sbin/ifconfig {$int}");
|
353
|
if(stristr($ifconfig,$ip) <> false)
|
354
|
return $int;
|
355
|
}
|
356
|
return false;
|
357
|
}
|
358
|
|
359
|
/*
|
360
|
* filter_translate_type_to_real_interface($interface): returns the real interface name
|
361
|
* for a friendly interface. ie: wan
|
362
|
*/
|
363
|
function filter_translate_type_to_real_interface($interface) {
|
364
|
global $config;
|
365
|
return $config['interfaces'][$interface]['if'];
|
366
|
}
|
367
|
|
368
|
/*
|
369
|
* get_carp_interface_status($carpinterface): returns the status of a carp ip
|
370
|
*/
|
371
|
function get_carp_interface_status($carpinterface) {
|
372
|
$result = does_interface_exist($carpinterface);
|
373
|
if($result <> true) return false;
|
374
|
$status = exec_command("/sbin/ifconfig {$carpinterface} | /usr/bin/grep \"carp:\" | /usr/bin/cut -d\" \" -f2");
|
375
|
return $status;
|
376
|
}
|
377
|
|
378
|
/*
|
379
|
* get_pfsync_interface_status($pfsyncinterface): returns the status of a pfsync
|
380
|
*/
|
381
|
function get_pfsync_interface_status($pfsyncinterface) {
|
382
|
$result = does_interface_exist($pfsyncinterface);
|
383
|
if($result <> true) return;
|
384
|
$status = exec_command("/sbin/ifconfig {$pfsyncinterface} | /usr/bin/grep \"pfsync:\" | /usr/bin/cut -d\" \" -f5");
|
385
|
return $status;
|
386
|
}
|
387
|
|
388
|
/*
|
389
|
* find_carp_interface($ip): return the carp interface where an ip is defined
|
390
|
*/
|
391
|
function find_carp_interface($ip) {
|
392
|
$num_carp_ints = find_number_of_created_carp_interfaces();
|
393
|
for($x=0; $x<$num_carp_ints; $x++) {
|
394
|
$result = does_interface_exist("carp{$x}");
|
395
|
if($result <> true) return;
|
396
|
$ifconfig = exec_command("/sbin/ifconfig carp{$x}");
|
397
|
if(stristr($ifconfig,$ip))
|
398
|
return "carp" . $x;
|
399
|
}
|
400
|
}
|
401
|
|
402
|
/*
|
403
|
* add_rule_to_anchor($anchor, $rule): adds the specified rule to an anchor
|
404
|
*/
|
405
|
function add_rule_to_anchor($anchor, $rule, $label) {
|
406
|
mwexec("echo " . $rule . " | /sbin/pfctl -a " . $anchor . ":" . $label . " -f -");
|
407
|
}
|
408
|
|
409
|
/*
|
410
|
* remove_text_from_file
|
411
|
* remove $text from file $file
|
412
|
*/
|
413
|
function remove_text_from_file($file, $text) {
|
414
|
global $fd_log;
|
415
|
fwrite($fd_log, "Adding needed text items:\n");
|
416
|
$filecontents = exec_command_and_return_text("cat " . $file);
|
417
|
$textTMP = str_replace($text, "", $filecontents);
|
418
|
$text .= $textTMP;
|
419
|
fwrite($fd_log, $text . "\n");
|
420
|
$fd = fopen($file, "w");
|
421
|
fwrite($fd, $text);
|
422
|
fclose($fd);
|
423
|
}
|
424
|
|
425
|
/*
|
426
|
* is_package_installed($packagename): returns 1 if a package is installed, 0 otherwise.
|
427
|
*/
|
428
|
function is_package_installed($packagename) {
|
429
|
global $config;
|
430
|
if($config['installedpackages']['package'] <> "")
|
431
|
foreach ($config['installedpackages']['package'] as $pkg) {
|
432
|
if($pkg['name'] == $packagename) return 1;
|
433
|
}
|
434
|
return 0;
|
435
|
}
|
436
|
|
437
|
/*
|
438
|
* lookup pkg array id#
|
439
|
*/
|
440
|
function get_pkg_id($pkg_name) {
|
441
|
global $config;
|
442
|
global $pkg_config;
|
443
|
if(is_array($config['installedpackages']['package'])) {
|
444
|
$i = 0;
|
445
|
foreach ($config['installedpackages']['package'] as $pkg) {
|
446
|
if($pkg['name'] == $pkg_name) return $i;
|
447
|
$i++;
|
448
|
}
|
449
|
return $i;
|
450
|
}
|
451
|
return -1;
|
452
|
}
|
453
|
|
454
|
/*
|
455
|
* get_latest_package_version($pkgname): Get current version of a package. Returns latest package version or false
|
456
|
* if package isn't defined in the currently used pkg_config.xml.
|
457
|
*/
|
458
|
function get_latest_package_version($pkg_name) {
|
459
|
global $g;
|
460
|
fetch_latest_pkg_config();
|
461
|
$pkg_config = parse_xml_config_pkg("{$g['tmp_path']}/pkg_config.xml", "pfsensepkgs");
|
462
|
foreach($pkg_config['packages']['package'] as $pkg) {
|
463
|
if($pkg['name'] == $pkg_name) {
|
464
|
return $pkg['version'];
|
465
|
}
|
466
|
}
|
467
|
return false;
|
468
|
}
|
469
|
|
470
|
/*
|
471
|
* Lookup pkg_id in pkg_config.xml
|
472
|
*/
|
473
|
function get_available_pkg_id($pkg_name) {
|
474
|
global $pkg_config, $g;
|
475
|
if(!is_array($pkg_config)) {
|
476
|
fetch_latest_pkg_config();
|
477
|
}
|
478
|
$pkg_config = parse_xml_config_pkg("{$g['tmp_path']}/pkg_config.xml", "pfsensepkgs");
|
479
|
$id = 0;
|
480
|
foreach($pkg_config['packages']['package'] as $pkg) {
|
481
|
if($pkg['name'] == $pkg_name) {
|
482
|
return $id;
|
483
|
}
|
484
|
$id++;
|
485
|
}
|
486
|
return;
|
487
|
}
|
488
|
|
489
|
/*
|
490
|
* fetch_latest_pkg_config: download the latest pkg_config.xml to /tmp/ directory
|
491
|
*/
|
492
|
function fetch_latest_pkg_config() {
|
493
|
global $g;
|
494
|
global $config;
|
495
|
if(!file_exists("{$g['tmp_path']}/pkg_config.xml")) {
|
496
|
$pkg_config_location = $g['pkg_config_location'];
|
497
|
$pkg_config_base_url = $g['pkg_config_base_url'];
|
498
|
if(isset($config['system']['alt_pkgconfig_url']['enabled'])) {
|
499
|
$pkg_config_location = $config['system']['alt_pkgconfig_url']['pkgconfig_base_url'] . $config['system']['alt_pkgconfig_url']['pkgconfig_filename'];
|
500
|
$pkg_config_base_url = $config['system']['alt_pkgconfig_url']['pkgconfig_base_url'];
|
501
|
}
|
502
|
mwexec("/usr/bin/fetch -o {$g['tmp_path']}/pkg_config.xml {$pkg_config_location}");
|
503
|
if(!file_exists("{$g['tmp_path']}/pkg_config.xml")) {
|
504
|
print_info_box_np("Could not download pkg_config.xml from " . $pkg_config_base_url . ". Check your DNS settings.");
|
505
|
die;
|
506
|
}
|
507
|
}
|
508
|
return;
|
509
|
}
|
510
|
|
511
|
/*
|
512
|
* add_text_to_file($file, $text): adds $text to $file.
|
513
|
* replaces the text if it already exists.
|
514
|
*/
|
515
|
function add_text_to_file($file, $text) {
|
516
|
global $fd_log;
|
517
|
fwrite($fd_log, "Adding needed text items:\n");
|
518
|
$filecontents = exec_command_and_return_text("cat " . $file);
|
519
|
$filecontents = str_replace($text, "", $filecontents);
|
520
|
$text = $filecontents . $text;
|
521
|
fwrite($fd_log, $text . "\n");
|
522
|
$fd = fopen($file, "w");
|
523
|
fwrite($fd, $text . "\n");
|
524
|
fclose($fd);
|
525
|
}
|
526
|
|
527
|
/*
|
528
|
* get_filename_from_url($url): converts a url to its filename.
|
529
|
*/
|
530
|
function get_filename_from_url($url) {
|
531
|
$filenamesplit = split("/", $url);
|
532
|
foreach($filenamesplit as $fn) $filename = $fn;
|
533
|
return $filename;
|
534
|
}
|
535
|
|
536
|
/*
|
537
|
* update_output_window: update bottom textarea dynamically.
|
538
|
*/
|
539
|
function update_output_window($text) {
|
540
|
$log = ereg_replace("\n", "\\n", $text);
|
541
|
echo "\n<script language=\"JavaScript\">this.document.forms[0].output.value = \"" . $log . "\";</script>";
|
542
|
}
|
543
|
|
544
|
/*
|
545
|
* get_dir: return an array of $dir
|
546
|
*/
|
547
|
function get_dir($dir) {
|
548
|
$dir_array = array();
|
549
|
$d = dir($dir);
|
550
|
while (false !== ($entry = $d->read())) {
|
551
|
array_push($dir_array, $entry);
|
552
|
}
|
553
|
$d->close();
|
554
|
return $dir_array;
|
555
|
}
|
556
|
|
557
|
/*
|
558
|
* update_output_window: update top textarea dynamically.
|
559
|
*/
|
560
|
function update_status($status) {
|
561
|
echo "\n<script language=\"JavaScript\">document.forms[0].status.value=\"" . $status . "\";</script>";
|
562
|
}
|
563
|
|
564
|
/*
|
565
|
* exec_command_and_return_text_array: execute command and return output
|
566
|
*/
|
567
|
function exec_command_and_return_text_array($command) {
|
568
|
$counter = 0;
|
569
|
$fd = popen($command . " 2>&1 ", "r");
|
570
|
while(!feof($fd)) {
|
571
|
$tmp .= fread($fd,49);
|
572
|
}
|
573
|
fclose($fd);
|
574
|
$temp_array = split("\n", $tmp);
|
575
|
return $tmp_array;
|
576
|
}
|
577
|
|
578
|
/*
|
579
|
* exec_command_and_return_text: execute command and return output
|
580
|
*/
|
581
|
function exec_command_and_return_text($command) {
|
582
|
return exec_command($command);
|
583
|
}
|
584
|
|
585
|
/*
|
586
|
* exec_command_and_return_text: execute command and update output window dynamically
|
587
|
*/
|
588
|
function execute_command_return_output($command) {
|
589
|
global $fd_log;
|
590
|
$fd = popen($command . " 2>&1 ", "r");
|
591
|
echo "\n<script language=\"JavaScript\">this.document.forms[0].output.value = \"\";</script>";
|
592
|
$counter = 0;
|
593
|
$counter2 = 0;
|
594
|
while(!feof($fd)) {
|
595
|
$tmp = fread($fd, 50);
|
596
|
$tmp1 = ereg_replace("\n","\\n", $tmp);
|
597
|
$text = ereg_replace("\"","'", $tmp1);
|
598
|
if($lasttext == "..") {
|
599
|
$text = "";
|
600
|
$lasttext = "";
|
601
|
$counter=$counter-2;
|
602
|
} else {
|
603
|
$lasttext .= $text;
|
604
|
}
|
605
|
if($counter > 51) {
|
606
|
$counter = 0;
|
607
|
$extrabreak = "\\n";
|
608
|
} else {
|
609
|
$extrabreak = "";
|
610
|
$counter++;
|
611
|
}
|
612
|
if($counter2 > 600) {
|
613
|
echo "\n<script language=\"JavaScript\">this.document.forms[0].output.value = \"\";</script>";
|
614
|
$counter2 = 0;
|
615
|
} else
|
616
|
$counter2++;
|
617
|
echo "\n<script language=\"JavaScript\">this.document.forms[0].output.value = this.document.forms[0].output.value + \"" . $text . $extrabreak . "\"; f('output'); </script>";
|
618
|
}
|
619
|
fclose($fd);
|
620
|
}
|
621
|
|
622
|
/*
|
623
|
* convert_friendly_interface_to_real_interface_name($interface): convert WAN to FXP0
|
624
|
*/
|
625
|
function convert_friendly_interface_to_real_interface_name($interface) {
|
626
|
global $config;
|
627
|
$lc_interface = strtolower($interface);
|
628
|
if($lc_interface == "lan") return $config['interfaces']['lan']['if'];
|
629
|
if($lc_interface == "wan") return $config['interfaces']['wan']['if'];
|
630
|
$i = 0;
|
631
|
$ifdescrs = array();
|
632
|
for ($j = 1; isset($config['interfaces']['opt' . $j]); $j++)
|
633
|
$ifdescrs['opt' . $j] = "opt" . $j;
|
634
|
foreach ($ifdescrs as $ifdescr => $ifname) {
|
635
|
if(strtolower($ifname) == $lc_interface)
|
636
|
return $config['interfaces'][$ifname]['if'];
|
637
|
if(strtolower($config['interfaces'][$ifname]['descr']) == $lc_interface)
|
638
|
return $config['interfaces'][$ifname]['if'];
|
639
|
}
|
640
|
return $interface;
|
641
|
}
|
642
|
|
643
|
/*
|
644
|
* convert_real_interface_to_friendly_interface_name($interface): convert fxp0 -> wan, etc.
|
645
|
*/
|
646
|
function convert_real_interface_to_friendly_interface_name($interface) {
|
647
|
global $config;
|
648
|
$i = 0;
|
649
|
$ifdescrs = array('wan', 'lan');
|
650
|
for ($j = 1; isset($config['interfaces']['opt' . $j]); $j++)
|
651
|
$ifdescrs['opt' . $j] = "opt" . $j;
|
652
|
foreach ($ifdescrs as $ifdescr => $ifname) {
|
653
|
$int = filter_translate_type_to_real_interface($ifname);
|
654
|
if($ifname == $interface) return $ifname;
|
655
|
if($int == $interface) return $ifname;
|
656
|
}
|
657
|
return $interface;
|
658
|
}
|
659
|
|
660
|
/*
|
661
|
* update_progress_bar($percent): updates the javascript driven progress bar.
|
662
|
*/
|
663
|
function update_progress_bar($percent) {
|
664
|
if($percent > 100) $percent = 1;
|
665
|
echo "\n<script type=\"text/javascript\" language=\"javascript\">";
|
666
|
echo "\ndocument.progressbar.style.width='" . $percent . "%';";
|
667
|
echo "\n</script>";
|
668
|
}
|
669
|
|
670
|
/*
|
671
|
* resync_all_package_configs() Force packages to setup their configuration and rc.d files.
|
672
|
* This function may also print output to the terminal indicating progress.
|
673
|
*/
|
674
|
function resync_all_package_configs($show_message = false) {
|
675
|
global $config;
|
676
|
$i = 0;
|
677
|
log_error("Resyncing configuration for all packages.");
|
678
|
if(!$config['installedpackages']['package']) return;
|
679
|
if($show_message == true) print "Syncing packages:";
|
680
|
foreach($config['installedpackages']['package'] as $package) {
|
681
|
if($show_message == true) print " " . $package['name'];
|
682
|
sync_package($i, true, true);
|
683
|
$i++;
|
684
|
}
|
685
|
if($show_message == true) print ".\n";
|
686
|
}
|
687
|
|
688
|
/*
|
689
|
* sweep_package_processes(): Periodically kill a package's unnecessary processes
|
690
|
* that may still be running (a server that does not automatically timeout, for example)
|
691
|
*/
|
692
|
function sweep_package_processes() {
|
693
|
global $config;
|
694
|
if(!$config['installedpackages']['package']) return;
|
695
|
foreach($config['installedpackages']['package'] as $package) {
|
696
|
$pkg_config = parse_xml_config_pkg("/usr/local/pkg/" . $package['configurationfile'], "packagegui");
|
697
|
if($pkg_config['swept_processes'] <> "") {
|
698
|
mwexec("/usr/bin/killall " . $pkg_config['swept_processes']);
|
699
|
log_error("Killed " . $package['name'] . "'s unnecessary processes.");
|
700
|
}
|
701
|
}
|
702
|
}
|
703
|
|
704
|
/*
|
705
|
* gather_altq_queue_stats(): gather alq queue stats and return an array that
|
706
|
* is queuename|qlength|measured_packets
|
707
|
* NOTE: this command takes 5 seconds to run
|
708
|
*/
|
709
|
function gather_altq_queue_stats($dont_return_root_queues) {
|
710
|
mwexec("/usr/bin/killall -9 pfctl");
|
711
|
$stats = `/sbin/pfctl -vvsq & /bin/sleep 5;/usr/bin/killall pfctl 2>/dev/null`;
|
712
|
$stats_array = split("\n", $stats);
|
713
|
$queue_stats = array();
|
714
|
foreach ($stats_array as $stats_line) {
|
715
|
if (preg_match_all("/queue\s+(\w+)\s+/",$stats_line,$match_array))
|
716
|
$queue_name = $match_array[1][0];
|
717
|
if (preg_match_all("/measured:\s+.*packets\/s\,\s(.*)\s+\]/",$stats_line,$match_array))
|
718
|
$speed = $match_array[1][0];
|
719
|
if (preg_match_all("/borrows:\s+(.*)/",$stats_line,$match_array))
|
720
|
$borrows = $match_array[1][0];
|
721
|
if (preg_match_all("/suspends:\s+(.*)/",$stats_line,$match_array))
|
722
|
$suspends = $match_array[1][0];
|
723
|
if (preg_match_all("/dropped pkts:\s+(.*)/",$stats_line,$match_array))
|
724
|
$drops = $match_array[1][0];
|
725
|
if (preg_match_all("/measured:\s+(.*)packets/",$stats_line,$match_array)) {
|
726
|
$measured = $match_array[1][0];
|
727
|
if($dont_return_root_queues == true)
|
728
|
if(stristr($queue_name,"root_") == false)
|
729
|
array_push($queue_stats, "{$queue_name}|{$speed}|{$measured}|{$borrows}|{$suspends}|{$drops}");
|
730
|
}
|
731
|
}
|
732
|
return $queue_stats;
|
733
|
}
|
734
|
|
735
|
/*
|
736
|
* reverse_strrchr($haystack, $needle): Return everything in $haystack up to the *last* instance of $needle.
|
737
|
* Useful for finding paths and stripping file extensions.
|
738
|
*/
|
739
|
function reverse_strrchr($haystack, $needle)
|
740
|
{
|
741
|
return strrpos($haystack, $needle) ? substr($haystack, 0, strrpos($haystack, $needle) +1 ) : false;
|
742
|
}
|
743
|
|
744
|
/*
|
745
|
* get_pkg_depends($pkg_name, $filetype = ".xml", $format = "files", return_nosync = 1): Return a package's dependencies.
|
746
|
*
|
747
|
* $filetype = "all" || ".xml", ".tgz", etc.
|
748
|
* $format = "files" (full filenames) || "names" (stripped / parsed depend names)
|
749
|
* $return_nosync = 1 (return depends that have nosync set) | 0 (ignore packages with nosync)
|
750
|
*
|
751
|
*/
|
752
|
function get_pkg_depends($pkg_name, $filetype = ".xml", $format = "files", $return_nosync = 1) {
|
753
|
global $config;
|
754
|
if(!is_numeric($pkg_name)) {
|
755
|
$pkg_name = get_pkg_id($pkg_name);
|
756
|
if($pkg_id == -1) return -1; // This package doesn't really exist - exit the function.
|
757
|
} else {
|
758
|
if(!isset($config['installedpackages']['package'][$pkg_id])) return; // No package belongs to the pkg_id passed to this function.
|
759
|
}
|
760
|
$package = $config['installedpackages']['package'][$pkg_id];
|
761
|
print '$package done.';
|
762
|
if(!file_exists("/usr/local/pkg/" . $package['configurationfile'])) { // If the package's config file doesn't exist, log an error and fetch it.
|
763
|
log_error("Fetching missing configuration XML for " . $package['name']);
|
764
|
mwexec("/usr/bin/fetch -o /usr/local/pkg/" . $package['configurationfile'] . " http://www.pfsense.com/packages/config/" . $package['configurationfile']);
|
765
|
}
|
766
|
$pkg_xml = parse_xml_config_pkg("/usr/local/pkg/" . $package['configurationfile'], "packagegui");
|
767
|
if($pkg_xml['additional_files_needed'] != "") {
|
768
|
foreach($pkg_xml['additional_files_needed'] as $item) {
|
769
|
if (($return_nosync == 0) && (isset($item['nosync']))) continue; // Do not return depends with nosync set if not required.
|
770
|
$depend_file = substr(strrchr($item['item']['0'], '/'),1); // Strip URLs down to filenames.
|
771
|
$depend_name = substr(substr($depend_file,0,strpos($depend_file,".")+1),0,-1); // Strip filename down to dependency name.
|
772
|
if (($filetype != "all") && (!preg_match("/${filetype}/i", $depend_file))) continue;
|
773
|
if ($item['prefix'] != "") {
|
774
|
$prefix = $item['prefix'];
|
775
|
} else {
|
776
|
$prefix = "/usr/local/pkg/";
|
777
|
}
|
778
|
if(!file_exists($prefix . $pkg_name)) {
|
779
|
log_error("Fetching missing dependency (" . $depend_name . ") for " . $pkg_name);
|
780
|
mwexec("/usr/local/bin/fetch -o " . $prefix . $depend_file . " " . $item['name']['0']);
|
781
|
if($item['chmod'] != "")
|
782
|
chmod($prefix . $depend_file, $item['chmod']); // Handle chmods.
|
783
|
}
|
784
|
switch ($format) {
|
785
|
case "files":
|
786
|
$depends[] = $depend_file;
|
787
|
break;
|
788
|
case "names":
|
789
|
switch ($filetype) {
|
790
|
case "all":
|
791
|
if(preg_match("/\.xml/i", $depend_file)) {
|
792
|
$depend_xml = parse_xml_config_pkg("/usr/local/pkg/" . $depend_file, "packagegui");
|
793
|
$depends[] = $depend_xml['name'];
|
794
|
break;
|
795
|
} else {
|
796
|
$depends[] = $depend_name; // If this dependency isn't package XML, use the stripped filename.
|
797
|
break;
|
798
|
}
|
799
|
case ".xml":
|
800
|
$depend_xml = parse_xml_config_pkg("/usr/local/pkg/" . $depend_file, "packagegui");
|
801
|
$depends[] = $depend_xml['name'];
|
802
|
break;
|
803
|
default:
|
804
|
$depends[] = $depend_name; // If we aren't looking for XML, use the stripped filename (it's all we have).
|
805
|
break;
|
806
|
}
|
807
|
}
|
808
|
}
|
809
|
return $depends;
|
810
|
}
|
811
|
}
|
812
|
|
813
|
/*
|
814
|
* is_service_running($service_name): checks to see if a service is running.
|
815
|
* if the service is running returns 1.
|
816
|
*/
|
817
|
function is_service_running($service_name) {
|
818
|
$status = `/bin/ps ax | grep {$service_name} | grep -v grep`;
|
819
|
$status_split = split("\n", $service_name);
|
820
|
$counter = 0;
|
821
|
foreach ($status_split as $ss) $counter++;
|
822
|
if($counter > 0) return 1;
|
823
|
return 0;
|
824
|
}
|
825
|
|
826
|
/*
|
827
|
* backup_config_section($section): returns as an xml file string of
|
828
|
* the configuration section
|
829
|
*/
|
830
|
function backup_config_section($section) {
|
831
|
global $config;
|
832
|
$new_section = &$config[$section];
|
833
|
/* generate configuration XML */
|
834
|
$xmlconfig = dump_xml_config($new_section, $section);
|
835
|
$xmlconfig = str_replace("<?xml version=\"1.0\"?>", "", $xmlconfig);
|
836
|
return $xmlconfig;
|
837
|
}
|
838
|
|
839
|
/*
|
840
|
* restore_config_section($section, new_contents): restore a configuration section,
|
841
|
* and write the configuration out
|
842
|
* to disk/cf.
|
843
|
*/
|
844
|
function restore_config_section($section, $new_contents) {
|
845
|
global $config;
|
846
|
$fout = fopen("{$g['tmp_path']}/tmpxml","w");
|
847
|
fwrite($fout, $new_contents);
|
848
|
fclose($fout);
|
849
|
$section_xml = parse_xml_config_pkg($g['tmp_path'] . "/tmpxml", $section);
|
850
|
$config[$section] = &$section_xml;
|
851
|
unlink($g['tmp_path'] . "/tmpxml");
|
852
|
write_config("Restored {$section} of config file (maybe from CARP partner)");
|
853
|
return;
|
854
|
}
|
855
|
|
856
|
/*
|
857
|
* http_post($server, $port, $url, $vars): does an http post to a web server
|
858
|
* posting the vars array.
|
859
|
* written by nf@bigpond.net.au
|
860
|
*/
|
861
|
function http_post($server, $port, $url, $vars) {
|
862
|
$user_agent = "Mozilla/4.0 (compatible; MSIE 5.5; Windows 98)";
|
863
|
$urlencoded = "";
|
864
|
while (list($key,$value) = each($vars))
|
865
|
$urlencoded.= urlencode($key) . "=" . urlencode($value) . "&";
|
866
|
$urlencoded = substr($urlencoded,0,-1);
|
867
|
|
868
|
$content_length = strlen($urlencoded);
|
869
|
|
870
|
$headers = "POST $url HTTP/1.1
|
871
|
Accept: */*
|
872
|
Accept-Language: en-au
|
873
|
Content-Type: application/x-www-form-urlencoded
|
874
|
User-Agent: $user_agent
|
875
|
Host: $server
|
876
|
Connection: Keep-Alive
|
877
|
Cache-Control: no-cache
|
878
|
Content-Length: $content_length
|
879
|
|
880
|
";
|
881
|
|
882
|
$fp = fsockopen($server, $port, $errno, $errstr);
|
883
|
if (!$fp) {
|
884
|
return false;
|
885
|
}
|
886
|
|
887
|
fputs($fp, $headers);
|
888
|
fputs($fp, $urlencoded);
|
889
|
|
890
|
$ret = "";
|
891
|
while (!feof($fp))
|
892
|
$ret.= fgets($fp, 1024);
|
893
|
|
894
|
fclose($fp);
|
895
|
|
896
|
return $ret;
|
897
|
|
898
|
}
|
899
|
|
900
|
/*
|
901
|
* php_check_syntax($code_tocheck, $errormessage): checks $code_to_check for errors
|
902
|
*/
|
903
|
if (!function_exists('php_check_syntax')){
|
904
|
function php_check_syntax($code_to_check, &$errormessage){
|
905
|
return false;
|
906
|
$fout = fopen("/tmp/codetocheck.php","w");
|
907
|
$code = $_POST['content'];
|
908
|
$code = str_replace("<?php", "", $code);
|
909
|
$code = str_replace("?>", "", $code);
|
910
|
fwrite($fout, "<?php\n\n");
|
911
|
fwrite($fout, $code);
|
912
|
fwrite($fout, "\n\n?>\n");
|
913
|
fclose($fout);
|
914
|
$command = "/usr/local/bin/php -l /tmp/codetocheck.php";
|
915
|
$output = exec_command($command);
|
916
|
if (stristr($output, "Errors parsing") == false) {
|
917
|
echo "false\n";
|
918
|
$errormessage = '';
|
919
|
return(false);
|
920
|
} else {
|
921
|
$errormessage = $output;
|
922
|
return(true);
|
923
|
}
|
924
|
}
|
925
|
}
|
926
|
|
927
|
/*
|
928
|
* php_check_filename_syntax($filename, $errormessage): checks the file $filename for errors
|
929
|
*/
|
930
|
if (!function_exists('php_check_syntax')){
|
931
|
function php_check_syntax($code_to_check, &$errormessage){
|
932
|
return false;
|
933
|
$command = "/usr/local/bin/php -l " . $code_to_check;
|
934
|
$output = exec_command($command);
|
935
|
if (stristr($output, "Errors parsing") == false) {
|
936
|
echo "false\n";
|
937
|
$errormessage = '';
|
938
|
return(false);
|
939
|
} else {
|
940
|
$errormessage = $output;
|
941
|
return(true);
|
942
|
}
|
943
|
}
|
944
|
}
|
945
|
|
946
|
/*
|
947
|
* sync_package($pkg_name, $sync_depends = true, $show_message = false) Force a package to setup its configuration and rc.d files.
|
948
|
*/
|
949
|
function sync_package($pkg_name, $sync_depends = true, $show_message = false) {
|
950
|
global $config;
|
951
|
|
952
|
if(!file_exists("/usr/local/pkg")) mwexec("/bin/mkdir -p /usr/local/pkg/pf");
|
953
|
if(!$config['installedpackages']['package']) return;
|
954
|
if(!is_numeric($pkg_name)) {
|
955
|
$pkg_id = get_pkg_id($pkg_name);
|
956
|
if($pkg_id == -1) return -1; // This package doesn't really exist - exit the function.
|
957
|
} else {
|
958
|
$pkg_id = $pkg_name;
|
959
|
if(!isset($config['installedpackages']['package'][$pkg_id]))
|
960
|
return; // No package belongs to the pkg_id passed to this function.
|
961
|
}
|
962
|
$package = $config['installedpackages']['package'][$pkg_id];
|
963
|
if(!file_exists("/usr/local/pkg/" . $package['configurationfile'])) {
|
964
|
//if($show_message == true) print "(f)"; Don't mess with this until the package system has settled.
|
965
|
log_error("Fetching missing configuration XML for " . $package['name']);
|
966
|
mwexec("/usr/bin/fetch -o /usr/local/pkg/" . $package['configurationfile'] . " http://www.pfsense.com/packages/config/" . $package['configurationfile']);
|
967
|
}
|
968
|
$pkg_config = parse_xml_config_pkg("/usr/local/pkg/" . $package['configurationfile'], "packagegui");
|
969
|
if(isset($pkg_config['nosync'])) continue;
|
970
|
//if($show_message == true) print "Syncing " . $pkg_name;
|
971
|
if($pkg['custom_php_global_functions'] <> "")
|
972
|
eval($pkg['custom_php_global_functions']);
|
973
|
if($pkg_config['custom_php_command_before_form'] <> "")
|
974
|
eval($pkg_config['custom_php_command_before_form']);
|
975
|
if($pkg_config['custom_php_resync_config_command'] <> "")
|
976
|
eval($pkg_config['custom_php_resync_config_command']);
|
977
|
if($sync_depends == true) {
|
978
|
$depends = get_pkg_depends($pkg_name, ".xml", "files", 1); // Call dependency handler and do a little more error checking.
|
979
|
if(is_array($depends)) {
|
980
|
foreach($depends as $item) {
|
981
|
$item_config = parse_xml_config_pkg("/usr/local/pkg/" . $item, "packagegui");
|
982
|
if(isset($item_config['nosync'])) continue;
|
983
|
if($item_config['custom_php_command_before_form'] <> "") {
|
984
|
eval($item_config['custom_php_command_before_form']);
|
985
|
print "Evaled dependency.";
|
986
|
}
|
987
|
if($item_config['custom_php_resync_config_command'] <> "") {
|
988
|
eval($item_config['custom_php_resync_config_command']);
|
989
|
print "Evaled dependency.";
|
990
|
}
|
991
|
if($show_message == true) print " " . $item_config['name'];
|
992
|
}
|
993
|
}
|
994
|
}
|
995
|
// if($show_message == true) print ".";
|
996
|
}
|
997
|
|
998
|
/*
|
999
|
* rmdir_recursive($path,$follow_links=false)
|
1000
|
* Recursively remove a directory tree (rm -rf path)
|
1001
|
* This is for directories _only_
|
1002
|
*/
|
1003
|
function rmdir_recursive($path,$follow_links=false) {
|
1004
|
$to_do = glob($path);
|
1005
|
if(!is_array($to_do)) {
|
1006
|
if(file_exists($to_do)) {
|
1007
|
$dir = opendir($path);
|
1008
|
while ($entry = readdir($dir)) {
|
1009
|
if (is_file("$path/$entry") || ((!$follow_links) && is_link("$path/$entry")))
|
1010
|
unlink("$path/$entry");
|
1011
|
elseif (is_dir("$path/$entry") && $entry!='.' && $entry!='..')
|
1012
|
rmdir_recursive("$path/$entry");
|
1013
|
}
|
1014
|
closedir($dir);
|
1015
|
rmdir($path);
|
1016
|
return;
|
1017
|
}
|
1018
|
} else {
|
1019
|
foreach($to_do as $workingdir) { // Handle wildcards by foreaching.
|
1020
|
if(file_exists($workingdir)) {
|
1021
|
$dir = opendir($workingdir);
|
1022
|
while ($entry = readdir($dir)) {
|
1023
|
if (is_file("$workingdir/$entry") || ((!$follow_links) && is_link("$workingdir/$entry")))
|
1024
|
unlink("$workingdir/$entry");
|
1025
|
elseif (is_dir("$workingdir/$entry") && $entry!='.' && $entry!='..')
|
1026
|
rmdir_recursive("$workingdir/$entry");
|
1027
|
}
|
1028
|
closedir($dir);
|
1029
|
rmdir($workingdir);
|
1030
|
}
|
1031
|
}
|
1032
|
return;
|
1033
|
}
|
1034
|
return;
|
1035
|
}
|
1036
|
|
1037
|
/*
|
1038
|
* safe_mkdir($path, $mode = 0755)
|
1039
|
* create directory if it doesn't already exist and isn't a file!
|
1040
|
*/
|
1041
|
function safe_mkdir($path, $mode=0755) {
|
1042
|
if (!is_file($path) && !is_dir($path))
|
1043
|
return mkdir($path, $mode);
|
1044
|
else
|
1045
|
return false;
|
1046
|
}
|
1047
|
|
1048
|
/*
|
1049
|
* make_dirs($path, $mode = 0755)
|
1050
|
* create directory tree recursively (mkdir -p)
|
1051
|
*/
|
1052
|
function make_dirs($path, $mode = 0755)
|
1053
|
{
|
1054
|
return is_dir($path) || (make_dirs(dirname($path), $mode) && safe_mkdir($path, $mode));
|
1055
|
}
|
1056
|
|
1057
|
/****f* pfsense-utils/auto_upgrade
|
1058
|
* NAME
|
1059
|
* auto_upgrade - pfSense autoupdate handler.
|
1060
|
* FUNCTION
|
1061
|
* Begin the pfSense autoupdate process. This function calls check_firmware_version to get
|
1062
|
* a list of current versions and then loops through them, applying binary diffs etc.
|
1063
|
* RESULT
|
1064
|
* null
|
1065
|
* BUGS
|
1066
|
* This function needs to have logic in place to automatically switch over to full updates
|
1067
|
* if a certain amount of binary diffs do not apply successfully.
|
1068
|
* SEE ALSO
|
1069
|
* pfsense.utils/check_firmware_version
|
1070
|
******/
|
1071
|
function auto_upgrade() {
|
1072
|
global $config, $g;
|
1073
|
if (isset($config['system']['alt_firmware_url']['enabled'])) {
|
1074
|
$firmwareurl=$config['system']['alt_firmware_url']['firmware_base_url'];
|
1075
|
$firmwarepath=$config['system']['alt_firmware_url']['firmware_filename'];
|
1076
|
} else {
|
1077
|
$firmwareurl=$g['firmwarebaseurl'];
|
1078
|
$firmwarepath=$g['firmwarefilename'];
|
1079
|
}
|
1080
|
if($config['system']['proxy_auth_username'] <> "")
|
1081
|
$http_auth_username = $config['system']['proxy_auth_username'];
|
1082
|
if($config['system']['proxy_auth_password'] <> "")
|
1083
|
$http_auth_password = $config['system']['proxy_auth_password'];
|
1084
|
if (isset($config['system']['alt_firmware_url']['enabled'])) {
|
1085
|
$firmwareurl=$config['system']['alt_firmware_url']['firmware_base_url'];
|
1086
|
$firmwarename=$config['system']['alt_firmware_url']['firmware_filename'];
|
1087
|
} else {
|
1088
|
$firmwareurl=$g['firmwarebaseurl'];
|
1089
|
$firmwarename=$g['firmwarefilename'];
|
1090
|
}
|
1091
|
exec_rc_script_async("/etc/rc.firmware_auto {$firmwareurl} {$firmwarename} {$http_auth_username} {$http_auth_password}");
|
1092
|
return;
|
1093
|
}
|
1094
|
|
1095
|
/*
|
1096
|
* check_firmware_version(): Check whether the current firmware installed is the most recently released.
|
1097
|
*/
|
1098
|
function check_firmware_version($tocheck = "all", $return_php = true) {
|
1099
|
global $g;
|
1100
|
$versioncheck_base_url = $g['versioncheckbaseurl'];
|
1101
|
$versioncheck_path = $g['versioncheckpath'];
|
1102
|
if(isset($config['system']['alt_firmware_url']['enabled']) and isset($config['system']['alt_firmware_url']['versioncheck_base_url'])) {
|
1103
|
$versioncheck_base_url = $config['system']['alt_firmware_url']['versioncheck_base_url'];
|
1104
|
}
|
1105
|
$rawparams = array("firmware" => array("version" => trim(file_get_contents('/etc/version'))),
|
1106
|
"kernel" => array("version" => trim(file_get_contents('/etc/version_kernel'))),
|
1107
|
"base" => array("version" => trim(file_get_contents('/etc/version_base'))),
|
1108
|
"platform" => trim(file_get_contents('/etc/platform'))
|
1109
|
);
|
1110
|
if($tocheck = "all") {
|
1111
|
$params = $rawparams;
|
1112
|
} else {
|
1113
|
foreach($tocheck as $check) {
|
1114
|
$params['check'] = $rawparams['check'];
|
1115
|
$params['platform'] = $rawparams['platform'];
|
1116
|
}
|
1117
|
}
|
1118
|
if(isset($config['system']['firmwarebranch'])) {
|
1119
|
$params['branch'] = $config['system']['firmwarebranch'];
|
1120
|
}
|
1121
|
$xmlparams = php_value_to_xmlrpc($params);
|
1122
|
$msg = new XML_RPC_Message('pfsense.get_firmware_version', array($xmlparams));
|
1123
|
$cli = new XML_RPC_Client($versioncheck_path, $versioncheck_base_url);
|
1124
|
$resp = $cli->send($msg, 10);
|
1125
|
if(!$resp or $resp->faultCode()) {
|
1126
|
$raw_versions = false;
|
1127
|
} else {
|
1128
|
$raw_versions = xmlrpc_value_to_php($resp->value());
|
1129
|
$raw_versions["current"] = $params;
|
1130
|
}
|
1131
|
return $raw_versions;
|
1132
|
}
|
1133
|
|
1134
|
?>
|