Project

General

Profile

Download (35.8 KB) Statistics
| Branch: | Tag: | Revision:
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/return_filename_as_array
99
 * NAME
100
 *   return_filename_as_array - Return a file's contents as an array.
101
 * INPUTS
102
 *   $filename - string containing the path to the desired file.
103
 *   $strip - array of characters to strip - default is '#'.
104
 * RESULT
105
 *   $file - array containing the file's contents.
106
 * NOTES
107
 *   This function strips lines starting with '#' and leading/trailing whitespace by default.
108
 ******/
109
function return_filename_as_array($filename, $strip = array('#')) {
110
    if(file_exists($filename)) $file = file($filename);
111
    if(is_array($file)) {
112
		foreach($file as $line) $line = trim($line);
113
        foreach($strip as $tostrip) $file = preg_grep("/^{$tostrip}/", $file, PREG_GREP_INVERT);
114
    }
115
    return $file;
116
}
117

    
118
/*
119
 *   function get_carp_status(): returns the status of carp being enabled or disabled.
120
 */
121
function get_carp_status() {
122
    /* grab the current status of carp */
123
    $status = `/sbin/sysctl net.inet.carp.allow`;
124
    $status = str_replace("\n","",$status);
125
    $status = str_replace(" ","",$status);
126
    $status = split(":", $status);
127
    if($status == "0") return false;
128
    return true;
129
}
130

    
131
/*
132
 * return_dir_as_array($filename): returns $filename contents as a string
133
 */
134
function return_filename_as_string($filename) {
135
    $tmp = "";
136
    $fd = fopen($filename, "r");
137
    if(!$fd) {
138
	log_error("Could not open {$filename}");
139
	return;
140
    }
141
    while(!feof($fd)) {
142
	$tmp .= fread($fd,49);
143
    }
144
    fclose($fd);
145
    return $tmp;
146
}
147

    
148
/*
149
 * is_carp_defined: returns true if carp is detected in kernel
150
 */
151
function is_carp_defined() {
152
    /* is carp compiled into the kernel and userland? */
153
    $command = "/sbin/sysctl -a | grep carp";
154
    $fd = popen($command . " 2>&1 ", "r");
155
    if(!$fd) {
156
	log_error("Warning, could not execute command {$command}");
157
	return 0;
158
    }
159
    while(!feof($fd)) {
160
	$tmp .= fread($fd,49);
161
    }
162
    fclose($fd);
163

    
164
    if($tmp == "")
165
	return false;
166
    else
167
	return true;
168
}
169

    
170
/*
171
 * find_number_of_created_carp_interfaces() returns the number of currently created carp interfaces
172
 */
173
function find_number_of_created_carp_interfaces() {
174
    $command = "/sbin/ifconfig | /usr/bin/grep \"carp*:\" | /usr/bin/wc -l";
175
    $fd = popen($command . " 2>&1 ", "r");
176
    if(!$fd) {
177
	log_error("Warning, could not execute command {$command}");
178
	return 0;
179
    }
180
    while(!feof($fd)) {
181
	$tmp .= fread($fd,49);
182
    }
183
    fclose($fd);
184
    $tmp = intval($tmp);
185
    return $tmp;
186
}
187

    
188
/*
189
 * link_ip_to_carp_interface($ip): finds where a carp interface links to.
190
*/
191
function link_ip_to_carp_interface($ip) {
192
    global $config;
193
    if($ip == "") return;
194
    $i = 0;
195

    
196
    $ifdescrs = array('wan', 'lan');
197
    for ($j = 1; isset($config['interfaces']['opt' . $j]); $j++) {
198
	$ifdescrs['opt' . $j] = "opt" . $j;
199
    }
200

    
201
    $ft = split("\.", $ip);
202
    $ft_ip = $ft[0] . "." . $ft[1] . "." . $ft[2] . ".";
203

    
204
    $carp_ints = "";
205
    $num_carp_ints = find_number_of_created_carp_interfaces();
206
    foreach ($ifdescrs as $ifdescr => $ifname) {
207
	for($x=0; $x<$num_carp_ints; $x++) {
208
	    $carp_int = "carp{$x}";
209
	    $carp_ip = find_interface_ip($carp_int);
210
	    $carp_ft = split("\.", $carp_ip);
211
	    $carp_ft_ip = $carp_ft[0] . "." . $carp_ft[1] . "." . $carp_ft[2] . ".";
212
	    $result = does_interface_exist($carp_int);
213
	    if($result <> true) break;
214
	    $interface = filter_opt_interface_to_real($ifname);
215
	    if($ft_ip == $carp_ft_ip)
216
		if(stristr($carp_ints,$carp_int) == false)
217
		    $carp_ints .= " " . $carp_int;
218
	}
219
    }
220
    return $carp_ints;
221
}
222

    
223
/*
224
 * exec_command($command): execute command return string of result
225
 */
226
function exec_command($command) {
227
    $counter = 0;
228
    $tmp = "";
229
    $fd = popen($command . " 2>&1 ", "r");
230
    while(!feof($fd)) {
231
	$tmp .= fread($fd,49);
232
    }
233
    fclose($fd);
234
    return $tmp;
235
}
236

    
237
/*
238
 * does_interface_exist($interface): return true or false if a interface is detected.
239
 */
240
function does_interface_exist($interface) {
241
    $ints = exec_command("/sbin/ifconfig -l");
242
    if(stristr($ints, $interface) !== false)
243
	return true;
244
    else
245
	return false;
246
}
247

    
248
/*
249
 * convert_ip_to_network_format($ip, $subnet): converts an ip address to network form
250
 */
251
function convert_ip_to_network_format($ip, $subnet) {
252
    $ipsplit = split('[.]', $ip);
253
    $string = $ipsplit[0] . "." . $ipsplit[1] . "." . $ipsplit[2] . ".0/" . $subnet;
254
    return $string;
255
}
256

    
257
/*
258
 * find_interface_ip($interface): return the interface ip (first found)
259
 */
260
function find_interface_ip($interface) {
261
    if(does_interface_exist($interface) == false) return;
262
    $ip = exec_command("/sbin/ifconfig {$interface} | /usr/bin/grep -w \"inet\" | /usr/bin/cut -d\" \" -f 2");
263
    $ip = str_replace("\n","",$ip);
264
    return $ip;
265
}
266

    
267
function guess_interface_from_ip($ipaddress) {
268
    $ints = `/sbin/ifconfig -l`;
269
    $ints_split = split(" ", $ints);
270
    $ip_subnet_split = split("\.", $ipaddress);
271
    $ip_subnet = $ip_subnet_split[0] . "." . $ip_subnet_split[1] . "." . $ip_subnet_split[2] . ".";
272
    foreach($ints_split as $int) {
273
        $ip = find_interface_ip($int);
274
        $ip_split = split("\.", $ip);
275
        $ip_tocheck = $ip_split[0] . "." . $ip_split[1] . "." . $ip_split[2] . ".";
276
        if(stristr($ip_tocheck, $ip_subnet) != false) return $int;
277
    }
278
}
279

    
280
function filter_opt_interface_to_real($opt) {
281
    global $config;
282
    return $config['interfaces'][$opt]['if'];
283
}
284

    
285
function filter_get_opt_interface_descr($opt) {
286
    global $config;
287
    return $config['interfaces'][$opt]['descr'];
288
}
289

    
290
function get_friendly_interface_list_as_array() {
291
    global $config;
292
    $ints = array();
293
    $i = 0;
294
    $ifdescrs = array('wan', 'lan');
295
    for ($j = 1; isset($config['interfaces']['opt' . $j]); $j++) {
296
	$ifdescrs['opt' . $j] = "opt" . $j;
297
    }
298
    $ifdescrs = get_interface_list();
299
    foreach ($ifdescrs as $ifdescr => $ifname) {
300
	array_push($ints,$ifdescr);
301
    }
302
    return $ints;
303
}
304

    
305
/*
306
 * find_ip_interface($ip): return the interface where an ip is defined
307
 */
308
function find_ip_interface($ip) {
309
    global $config;
310
    $i = 0;
311
    $ifdescrs = array('wan', 'lan');
312
    for ($j = 1; isset($config['interfaces']['opt' . $j]); $j++) {
313
	$ifdescrs['opt' . $j] = "opt" . $j;
314
    }
315
    foreach ($ifdescrs as $ifdescr => $ifname) {
316
	$int = filter_translate_type_to_real_interface($ifname);
317
	$ifconfig = exec_command("/sbin/ifconfig {$int}");
318
	if(stristr($ifconfig,$ip) <> false)
319
	    return $int;
320
    }
321
    return false;
322
}
323

    
324
/*
325
 *  filter_translate_type_to_real_interface($interface): returns the real interface name
326
 *                                                       for a friendly interface.  ie: wan
327
 */
328
function filter_translate_type_to_real_interface($interface) {
329
    global $config;
330
    return $config['interfaces'][$interface]['if'];
331
}
332

    
333
/*
334
 * get_carp_interface_status($carpinterface): returns the status of a carp ip
335
 */
336
function get_carp_interface_status($carpinterface) {
337
    $result = does_interface_exist($carpinterface);
338
    if($result <> true) return false;
339
    $status = exec_command("/sbin/ifconfig {$carpinterface} | /usr/bin/grep \"carp:\" | /usr/bin/cut -d\" \" -f2");
340
    return $status;
341
}
342

    
343
/*
344
 * get_pfsync_interface_status($pfsyncinterface): returns the status of a pfsync
345
 */
346
function get_pfsync_interface_status($pfsyncinterface) {
347
    $result = does_interface_exist($pfsyncinterface);
348
    if($result <> true) return;
349
    $status = exec_command("/sbin/ifconfig {$pfsyncinterface} | /usr/bin/grep \"pfsync:\" | /usr/bin/cut -d\" \" -f5");
350
    return $status;
351
}
352

    
353
/*
354
 * find_carp_interface($ip): return the carp interface where an ip is defined
355
 */
356
function find_carp_interface($ip) {
357
    $num_carp_ints = find_number_of_created_carp_interfaces();
358
    for($x=0; $x<$num_carp_ints; $x++) {
359
        $result = does_interface_exist("carp{$x}");
360
	if($result <> true) return;
361
	$ifconfig = exec_command("/sbin/ifconfig carp{$x}");
362
	if(stristr($ifconfig,$ip))
363
	    return "carp" . $x;
364
    }
365
}
366

    
367
/*
368
 * add_rule_to_anchor($anchor, $rule): adds the specified rule to an anchor
369
 */
370
function add_rule_to_anchor($anchor, $rule, $label) {
371
    mwexec("echo " . $rule . " | /sbin/pfctl -a " . $anchor . ":" . $label . " -f -");
372
}
373

    
374
/*
375
 * remove_text_from_file
376
 * remove $text from file $file
377
 */
378
function remove_text_from_file($file, $text) {
379
    global $fd_log;
380
    fwrite($fd_log, "Adding needed text items:\n");
381
    $filecontents = exec_command_and_return_text("cat " . $file);
382
    $textTMP = str_replace($text, "", $filecontents);
383
    $text .= $textTMP;
384
    fwrite($fd_log, $text . "\n");
385
    $fd = fopen($file, "w");
386
    fwrite($fd, $text);
387
    fclose($fd);
388
}
389

    
390
/*
391
 *  is_package_installed($packagename): returns 1 if a package is installed, 0 otherwise.
392
 */
393
function is_package_installed($packagename) {
394
    global $config;
395
    if($config['installedpackages']['package'] <> "")
396
	foreach ($config['installedpackages']['package'] as $pkg) {
397
	    if($pkg['name'] == $packagename) return 1;
398
	}
399
    return 0;
400
}
401

    
402
/*
403
 * lookup pkg array id#
404
 */
405
function get_pkg_id($pkg_name) {
406
    global $config;
407
    global $pkg_config;
408
    if(is_array($config['installedpackages']['package'])) {
409
	$i = 0;
410
	foreach ($config['installedpackages']['package'] as $pkg) {
411
	    if($pkg['name'] == $pkg_name) return $i;
412
	    $i++;
413
	}
414
	return $i;
415
    }
416
    return -1;
417
}
418

    
419
/*
420
 *  get_latest_package_version($pkgname): Get current version of a package. Returns latest package version or false
421
 *  					  if package isn't defined in the currently used pkg_config.xml.
422
 */
423
function get_latest_package_version($pkg_name) {
424
    global $g;
425
    fetch_latest_pkg_config();
426
    $pkg_config = parse_xml_config_pkg("{$g['tmp_path']}/pkg_config.xml", "pfsensepkgs");
427
    foreach($pkg_config['packages']['package'] as $pkg) {
428
	if($pkg['name'] == $pkg_name) {
429
	    return $pkg['version'];
430
	}
431
    }
432
    return false;
433
}
434

    
435
/*
436
 * Lookup pkg_id in pkg_config.xml
437
 */
438
function get_available_pkg_id($pkg_name) {
439
    global $pkg_config, $g;
440
    if(!is_array($pkg_config)) {
441
	fetch_latest_pkg_config();
442
    }
443
    $pkg_config = parse_xml_config_pkg("{$g['tmp_path']}/pkg_config.xml", "pfsensepkgs");
444
    $id = 0;
445
    foreach($pkg_config['packages']['package'] as $pkg) {
446
	if($pkg['name'] == $pkg_name) {
447
	    return $id;
448
	}
449
	$id++;
450
    }
451
    return;
452
}
453

    
454
/*
455
 * fetch_latest_pkg_config: download the latest pkg_config.xml to /tmp/ directory
456
 */
457
function fetch_latest_pkg_config() {
458
    global $g;
459
    global $config;
460
    if(!file_exists("{$g['tmp_path']}/pkg_config.xml")) {
461
	$pkg_config_location = $g['pkg_config_location'];
462
	$pkg_config_base_url = $g['pkg_config_base_url'];
463
	if(isset($config['system']['alt_pkgconfig_url']['enabled'])) {
464
	    $pkg_config_location = $config['system']['alt_pkgconfig_url']['pkgconfig_base_url'] . $config['system']['alt_pkgconfig_url']['pkgconfig_filename'];
465
	    $pkg_config_base_url = $config['system']['alt_pkgconfig_url']['pkgconfig_base_url'];
466
	}
467
	mwexec("/usr/bin/fetch -o {$g['tmp_path']}/pkg_config.xml {$pkg_config_location}");
468
	if(!file_exists("{$g['tmp_path']}/pkg_config.xml")) {
469
	    print_info_box_np("Could not download pkg_config.xml from " . $pkg_config_base_url . ". Check your DNS settings.");
470
	    die;
471
    	}
472
    }
473
    return;
474
}
475

    
476
/*
477
 * add_text_to_file($file, $text): adds $text to $file.
478
 * replaces the text if it already exists.
479
 */
480
function add_text_to_file($file, $text) {
481
    global $fd_log;
482
    fwrite($fd_log, "Adding needed text items:\n");
483
    $filecontents = exec_command_and_return_text("cat " . $file);
484
    $filecontents = str_replace($text, "", $filecontents);
485
    $text = $filecontents . $text;
486
    fwrite($fd_log, $text . "\n");
487
    $fd = fopen($file, "w");
488
    fwrite($fd, $text . "\n");
489
    fclose($fd);
490
}
491

    
492
/*
493
 * get_filename_from_url($url): converts a url to its filename.
494
 */
495
function get_filename_from_url($url) {
496
    $filenamesplit = split("/", $url);
497
    foreach($filenamesplit as $fn) $filename = $fn;
498
    return $filename;
499
}
500

    
501
/*
502
 *   update_output_window: update bottom textarea dynamically.
503
 */
504
function update_output_window($text) {
505
    $log = ereg_replace("\n", "\\n", $text);
506
    echo "\n<script language=\"JavaScript\">this.document.forms[0].output.value = \"" . $log . "\";</script>";
507
}
508

    
509
/*
510
 *   get_dir: return an array of $dir
511
 */
512
function get_dir($dir) {
513
    $dir_array = array();
514
    $d = dir($dir);
515
    while (false !== ($entry = $d->read())) {
516
	array_push($dir_array, $entry);
517
    }
518
    $d->close();
519
    return $dir_array;
520
}
521

    
522
/*
523
 *   update_output_window: update top textarea dynamically.
524
 */
525
function update_status($status) {
526
    echo "\n<script language=\"JavaScript\">document.forms[0].status.value=\"" . $status . "\";</script>";
527
}
528

    
529
/*
530
 *   exec_command_and_return_text_array: execute command and return output
531
 */
532
function exec_command_and_return_text_array($command) {
533
    $counter = 0;
534
    $fd = popen($command . " 2>&1 ", "r");
535
    while(!feof($fd)) {
536
	$tmp .= fread($fd,49);
537
    }
538
    fclose($fd);
539
    $temp_array = split("\n", $tmp);
540
    return $tmp_array;
541
}
542

    
543
/*
544
 *   exec_command_and_return_text: execute command and return output
545
 */
546
function exec_command_and_return_text($command) {
547
    return exec_command($command);
548
}
549

    
550
/*
551
 *   exec_command_and_return_text: execute command and update output window dynamically
552
 */
553
function execute_command_return_output($command) {
554
    global $fd_log;
555
    $fd = popen($command . " 2>&1 ", "r");
556
    echo "\n<script language=\"JavaScript\">this.document.forms[0].output.value = \"\";</script>";
557
    $counter = 0;
558
    $counter2 = 0;
559
    while(!feof($fd)) {
560
	$tmp = fread($fd, 50);
561
	$tmp1 = ereg_replace("\n","\\n", $tmp);
562
	$text = ereg_replace("\"","'", $tmp1);
563
	if($lasttext == "..") {
564
	    $text = "";
565
	    $lasttext = "";
566
	    $counter=$counter-2;
567
	} else {
568
	    $lasttext .= $text;
569
	}
570
	if($counter > 51) {
571
	    $counter = 0;
572
	    $extrabreak = "\\n";
573
	} else {
574
	    $extrabreak = "";
575
	    $counter++;
576
	}
577
	if($counter2 > 600) {
578
	    echo "\n<script language=\"JavaScript\">this.document.forms[0].output.value = \"\";</script>";
579
	    $counter2 = 0;
580
	} else
581
	    $counter2++;
582
	echo "\n<script language=\"JavaScript\">this.document.forms[0].output.value = this.document.forms[0].output.value + \"" . $text . $extrabreak .  "\"; f('output'); </script>";
583
    }
584
    fclose($fd);
585
}
586

    
587
/*
588
 * convert_friendly_interface_to_real_interface_name($interface): convert WAN to FXP0
589
 */
590
function convert_friendly_interface_to_real_interface_name($interface) {
591
    global $config;
592
    $lc_interface = strtolower($interface);
593
    if($lc_interface == "lan") return $config['interfaces']['lan']['if'];
594
    if($lc_interface == "wan") return $config['interfaces']['wan']['if'];
595
    $i = 0;
596
    $ifdescrs = array();
597
    for ($j = 1; isset($config['interfaces']['opt' . $j]); $j++)
598
	$ifdescrs['opt' . $j] = "opt" . $j;
599
    foreach ($ifdescrs as $ifdescr => $ifname) {
600
	if(strtolower($ifname) == $lc_interface)
601
	    return $config['interfaces'][$ifname]['if'];
602
	if(strtolower($config['interfaces'][$ifname]['descr']) == $lc_interface)
603
	    return $config['interfaces'][$ifname]['if'];
604
    }
605
    return $interface;
606
}
607

    
608
/*
609
 * convert_real_interface_to_friendly_interface_name($interface): convert fxp0 -> wan, etc.
610
 */
611
function convert_real_interface_to_friendly_interface_name($interface) {
612
    global $config;
613
    $i = 0;
614
    $ifdescrs = array('wan', 'lan');
615
    for ($j = 1; isset($config['interfaces']['opt' . $j]); $j++)
616
	$ifdescrs['opt' . $j] = "opt" . $j;
617
    foreach ($ifdescrs as $ifdescr => $ifname) {
618
	$int = filter_translate_type_to_real_interface($ifname);
619
	if($ifname == $interface) return $ifname;
620
	if($int == $interface) return $ifname;
621
    }
622
    return $interface;
623
}
624

    
625
/*
626
 * update_progress_bar($percent): updates the javascript driven progress bar.
627
 */
628
function update_progress_bar($percent) {
629
    if($percent > 100) $percent = 1;
630
    echo "\n<script type=\"text/javascript\" language=\"javascript\">";
631
    echo "\ndocument.progressbar.style.width='" . $percent . "%';";
632
    echo "\n</script>";
633
}
634

    
635
/*
636
 * resync_all_package_configs() Force packages to setup their configuration and rc.d files.
637
 * This function may also print output to the terminal indicating progress.
638
 */
639
function resync_all_package_configs($show_message = false) {
640
    global $config;
641
    $i = 0;
642
    log_error("Resyncing configuration for all packages.");
643
    if(!$config['installedpackages']['package']) return;
644
    if($show_message == true) print "Syncing packages:";
645
    foreach($config['installedpackages']['package'] as $package) {
646
	if($show_message == true) print " " . $package['name'];
647
	sync_package($i, true, true);
648
	$i++;
649
    }
650
    if($show_message == true) print ".\n";
651
}
652

    
653
/*
654
 * sweep_package_processes(): Periodically kill a package's unnecessary processes
655
 *			      that may still be running (a server that does not automatically timeout, for example)
656
 */
657
function sweep_package_processes() {
658
    global $config;
659
    if(!$config['installedpackages']['package']) return;
660
    foreach($config['installedpackages']['package'] as $package) {
661
        $pkg_config = parse_xml_config_pkg("/usr/local/pkg/" . $package['configurationfile'], "packagegui");
662
        if($pkg_config['swept_processes'] <> "") {
663
            mwexec("/usr/bin/killall " . $pkg_config['swept_processes']);
664
            log_error("Killed " . $package['name'] . "'s unnecessary processes.");
665
        }
666
    }
667
}
668

    
669
/*
670
 * gather_altq_queue_stats():  gather alq queue stats and return an array that
671
 *                             is queuename|qlength|measured_packets
672
 *                             NOTE: this command takes 5 seconds to run
673
 */
674
function gather_altq_queue_stats($dont_return_root_queues) {
675
    mwexec("/usr/bin/killall -9 pfctl");
676
    $stats = `/sbin/pfctl -vvsq & /bin/sleep 5;/usr/bin/killall pfctl 2>/dev/null`;
677
    $stats_array = split("\n", $stats);
678
    $queue_stats = array();
679
    foreach ($stats_array as $stats_line) {
680
        if (preg_match_all("/queue\s+(\w+)\s+/",$stats_line,$match_array))
681
            $queue_name = $match_array[1][0];
682
        if (preg_match_all("/measured:\s+.*packets\/s\,\s(.*)\s+\]/",$stats_line,$match_array))
683
            $speed = $match_array[1][0];
684
        if (preg_match_all("/borrows:\s+(.*)/",$stats_line,$match_array))
685
            $borrows = $match_array[1][0];
686
        if (preg_match_all("/suspends:\s+(.*)/",$stats_line,$match_array))
687
            $suspends = $match_array[1][0];
688
        if (preg_match_all("/dropped pkts:\s+(.*)/",$stats_line,$match_array))
689
            $drops = $match_array[1][0];
690
        if (preg_match_all("/measured:\s+(.*)packets/",$stats_line,$match_array)) {
691
            $measured = $match_array[1][0];
692
	    if($dont_return_root_queues == true)
693
		if(stristr($queue_name,"root_") == false)
694
		    array_push($queue_stats, "{$queue_name}|{$speed}|{$measured}|{$borrows}|{$suspends}|{$drops}");
695
        }
696
    }
697
    return $queue_stats;
698
}
699

    
700
/*
701
 * reverse_strrchr($haystack, $needle):  Return everything in $haystack up to the *last* instance of $needle.
702
 *					 Useful for finding paths and stripping file extensions.
703
 */
704
function reverse_strrchr($haystack, $needle)
705
{
706
               return strrpos($haystack, $needle) ? substr($haystack, 0, strrpos($haystack, $needle) +1 ) : false;
707
}
708

    
709
/*
710
 * get_pkg_depends($pkg_name, $filetype = ".xml", $format = "files", return_nosync = 1):  Return a package's dependencies.
711
 *
712
 * $filetype = "all" || ".xml", ".tgz", etc.
713
 * $format = "files" (full filenames) || "names" (stripped / parsed depend names)
714
 * $return_nosync = 1 (return depends that have nosync set) | 0 (ignore packages with nosync)
715
 *
716
 */
717
function get_pkg_depends($pkg_name, $filetype = ".xml", $format = "files", $return_nosync = 1) {
718
    global $config;
719
    if(!is_numeric($pkg_name)) {
720
	$pkg_name = get_pkg_id($pkg_name);
721
	if($pkg_id == -1) return -1; // This package doesn't really exist - exit the function.
722
    } else {
723
	if(!isset($config['installedpackages']['package'][$pkg_id])) return; // No package belongs to the pkg_id passed to this function.
724
    }
725
    $package = $config['installedpackages']['package'][$pkg_id];
726
    print '$package done.';
727
    if(!file_exists("/usr/local/pkg/" . $package['configurationfile'])) { // If the package's config file doesn't exist, log an error and fetch it.
728
	log_error("Fetching missing configuration XML for " . $package['name']);
729
	mwexec("/usr/bin/fetch -o /usr/local/pkg/" . $package['configurationfile'] . " http://www.pfsense.com/packages/config/" . $package['configurationfile']);
730
    }
731
    $pkg_xml = parse_xml_config_pkg("/usr/local/pkg/" . $package['configurationfile'], "packagegui");
732
    if($pkg_xml['additional_files_needed'] != "") {
733
	foreach($pkg_xml['additional_files_needed'] as $item) {
734
	    if (($return_nosync == 0) && (isset($item['nosync']))) continue; // Do not return depends with nosync set if not required.
735
	    $depend_file = substr(strrchr($item['item']['0'], '/'),1); // Strip URLs down to filenames.
736
	    $depend_name = substr(substr($depend_file,0,strpos($depend_file,".")+1),0,-1); // Strip filename down to dependency name.
737
	    if (($filetype != "all") && (!preg_match("/${filetype}/i", $depend_file))) continue;
738
	    if ($item['prefix'] != "") {
739
		$prefix = $item['prefix'];
740
	    } else {
741
		$prefix = "/usr/local/pkg/";
742
	    }
743
	    if(!file_exists($prefix . $pkg_name)) {
744
		log_error("Fetching missing dependency (" . $depend_name . ") for " . $pkg_name);
745
		mwexec("/usr/local/bin/fetch -o " . $prefix . $depend_file . " " . $item['name']['0']);
746
		if($item['chmod'] != "")
747
		    chmod($prefix . $depend_file, $item['chmod']); // Handle chmods.
748
	    }
749
	    switch ($format) {
750
	    case "files":
751
		$depends[] = $depend_file;
752
		break;
753
	    case "names":
754
		switch ($filetype) {
755
		case "all":
756
		    if(preg_match("/\.xml/i", $depend_file)) {
757
			$depend_xml = parse_xml_config_pkg("/usr/local/pkg/" . $depend_file, "packagegui");
758
			$depends[] = $depend_xml['name'];
759
			break;
760
		    } else {
761
			$depends[] = $depend_name; // If this dependency isn't package XML, use the stripped filename.
762
			break;
763
		    }
764
		case ".xml":
765
		    $depend_xml = parse_xml_config_pkg("/usr/local/pkg/" . $depend_file, "packagegui");
766
		    $depends[] = $depend_xml['name'];
767
		    break;
768
		default:
769
		    $depends[] = $depend_name; // If we aren't looking for XML, use the stripped filename (it's all we have).
770
		    break;
771
		}
772
	    }
773
	}
774
	return $depends;
775
    }
776
}
777

    
778
/*
779
 * is_service_running($service_name): checks to see if a service is running.
780
 *                                    if the service is running returns 1.
781
 */
782
function is_service_running($service_name) {
783
    $status = `/bin/ps ax | grep {$service_name} | grep -v grep`;
784
    $status_split = split("\n", $service_name);
785
    $counter = 0;
786
    foreach ($status_split as $ss) $counter++;
787
    if($counter > 0) return 1;
788
    return 0;
789
}
790

    
791
/*
792
 *  backup_config_section($section): returns as an xml file string of
793
 *                                   the configuration section
794
 */
795
function backup_config_section($section) {
796
    global $config;
797
    $new_section = &$config[$section];
798
    /* generate configuration XML */
799
    $xmlconfig = dump_xml_config($new_section, $section);
800
    $xmlconfig = str_replace("<?xml version=\"1.0\"?>", "", $xmlconfig);
801
    return $xmlconfig;
802
}
803

    
804
/*
805
 *  restore_config_section($section, new_contents): restore a configuration section,
806
 *                                                  and write the configuration out
807
 *                                                  to disk/cf.
808
 */
809
function restore_config_section($section, $new_contents) {
810
    global $config;
811
    $fout = fopen("{$g['tmp_path']}/tmpxml","w");
812
    fwrite($fout, $new_contents);
813
    fclose($fout);
814
    $section_xml = parse_xml_config_pkg($g['tmp_path'] . "/tmpxml", $section);
815
    $config[$section] = &$section_xml;
816
    unlink($g['tmp_path'] . "/tmpxml");
817
    write_config("Restored {$section} of config file (maybe from CARP partner)");
818
    return;
819
}
820

    
821
/*
822
 * http_post($server, $port, $url, $vars): does an http post to a web server
823
 *                                         posting the vars array.
824
 * written by nf@bigpond.net.au
825
 */
826
function http_post($server, $port, $url, $vars) {
827
    $user_agent = "Mozilla/4.0 (compatible; MSIE 5.5; Windows 98)";
828
    $urlencoded = "";
829
    while (list($key,$value) = each($vars))
830
	$urlencoded.= urlencode($key) . "=" . urlencode($value) . "&";
831
    $urlencoded = substr($urlencoded,0,-1);
832

    
833
    $content_length = strlen($urlencoded);
834

    
835
    $headers = "POST $url HTTP/1.1
836
Accept: */*
837
Accept-Language: en-au
838
Content-Type: application/x-www-form-urlencoded
839
User-Agent: $user_agent
840
Host: $server
841
Connection: Keep-Alive
842
Cache-Control: no-cache
843
Content-Length: $content_length
844

    
845
";
846

    
847
    $fp = fsockopen($server, $port, $errno, $errstr);
848
    if (!$fp) {
849
	return false;
850
    }
851

    
852
    fputs($fp, $headers);
853
    fputs($fp, $urlencoded);
854

    
855
    $ret = "";
856
    while (!feof($fp))
857
	$ret.= fgets($fp, 1024);
858

    
859
    fclose($fp);
860

    
861
    return $ret;
862

    
863
}
864

    
865
/*
866
 *  php_check_syntax($code_tocheck, $errormessage): checks $code_to_check for errors
867
 */
868
if (!function_exists('php_check_syntax')){
869
   function php_check_syntax($code_to_check, &$errormessage){
870
	return false;
871
        $fout = fopen("/tmp/codetocheck.php","w");
872
        $code = $_POST['content'];
873
        $code = str_replace("<?php", "", $code);
874
        $code = str_replace("?>", "", $code);
875
        fwrite($fout, "<?php\n\n");
876
        fwrite($fout, $code);
877
        fwrite($fout, "\n\n?>\n");
878
        fclose($fout);
879
        $command = "/usr/local/bin/php -l /tmp/codetocheck.php";
880
        $output = exec_command($command);
881
        if (stristr($output, "Errors parsing") == false) {
882
            echo "false\n";
883
            $errormessage = '';
884
            return(false);
885
        } else {
886
            $errormessage = $output;
887
            return(true);
888
        }
889
    }
890
}
891

    
892
/*
893
 *  php_check_filename_syntax($filename, $errormessage): checks the file $filename for errors
894
 */
895
if (!function_exists('php_check_syntax')){
896
   function php_check_syntax($code_to_check, &$errormessage){
897
	return false;
898
        $command = "/usr/local/bin/php -l " . $code_to_check;
899
        $output = exec_command($command);
900
        if (stristr($output, "Errors parsing") == false) {
901
            echo "false\n";
902
            $errormessage = '';
903
            return(false);
904
        } else {
905
            $errormessage = $output;
906
            return(true);
907
        }
908
    }
909
}
910

    
911
/*
912
 * sync_package($pkg_name, $sync_depends = true, $show_message = false) Force a package to setup its configuration and rc.d files.
913
 */
914
function sync_package($pkg_name, $sync_depends = true, $show_message = false) {
915
    global $config;
916

    
917
    if(!file_exists("/usr/local/pkg")) mwexec("/bin/mkdir -p /usr/local/pkg/pf");
918
    if(!$config['installedpackages']['package']) return;
919
    if(!is_numeric($pkg_name)) {
920
	$pkg_id = get_pkg_id($pkg_name);
921
	if($pkg_id == -1) return -1; // This package doesn't really exist - exit the function.
922
    } else {
923
	$pkg_id = $pkg_name;
924
	if(!isset($config['installedpackages']['package'][$pkg_id]))
925
	    return;  // No package belongs to the pkg_id passed to this function.
926
    }
927
    $package = $config['installedpackages']['package'][$pkg_id];
928
    if(!file_exists("/usr/local/pkg/" . $package['configurationfile'])) {
929
	//if($show_message == true) print "(f)"; Don't mess with this until the package system has settled.
930
	log_error("Fetching missing configuration XML for " . $package['name']);
931
	mwexec("/usr/bin/fetch -o /usr/local/pkg/" . $package['configurationfile'] . " http://www.pfsense.com/packages/config/" . $package['configurationfile']);
932
    }
933
    $pkg_config = parse_xml_config_pkg("/usr/local/pkg/" . $package['configurationfile'], "packagegui");
934
    if(isset($pkg_config['nosync'])) continue;
935
    //if($show_message == true) print "Syncing " . $pkg_name;
936
    if($pkg['custom_php_global_functions'] <> "")
937
        eval($pkg['custom_php_global_functions']);
938
    if($pkg_config['custom_php_command_before_form'] <> "")
939
	eval($pkg_config['custom_php_command_before_form']);
940
    if($pkg_config['custom_php_resync_config_command'] <> "")
941
	eval($pkg_config['custom_php_resync_config_command']);
942
    if($sync_depends == true) {
943
	$depends = get_pkg_depends($pkg_name, ".xml", "files", 1); // Call dependency handler and do a little more error checking.
944
	if(is_array($depends)) {
945
	    foreach($depends as $item) {
946
		$item_config = parse_xml_config_pkg("/usr/local/pkg/" . $item, "packagegui");
947
		if(isset($item_config['nosync'])) continue;
948
		if($item_config['custom_php_command_before_form'] <> "") {
949
		    eval($item_config['custom_php_command_before_form']);
950
		    print "Evaled dependency.";
951
		}
952
		if($item_config['custom_php_resync_config_command'] <> "") {
953
		    eval($item_config['custom_php_resync_config_command']);
954
		    print "Evaled dependency.";
955
		}
956
		if($show_message == true) print " " . $item_config['name'];
957
	    }
958
	}
959
    }
960
    // if($show_message == true) print ".";
961
}
962

    
963
/*
964
 * rmdir_recursive($path,$follow_links=false)
965
 * Recursively remove a directory tree (rm -rf path)
966
 * This is for directories _only_
967
 */
968
function rmdir_recursive($path,$follow_links=false) {
969
	$to_do = glob($path);
970
	if(!is_array($to_do)) {
971
		if(file_exists($to_do)) {
972
			$dir = opendir($path);
973
			while ($entry = readdir($dir)) {
974
				if (is_file("$path/$entry") || ((!$follow_links) && is_link("$path/$entry")))
975
					unlink("$path/$entry");
976
      	 			elseif (is_dir("$path/$entry") && $entry!='.' && $entry!='..')
977
					rmdir_recursive("$path/$entry");
978
			}
979
			closedir($dir);
980
			rmdir($path);
981
			return;
982
		}
983
	} else {
984
		foreach($to_do as $workingdir) { // Handle wildcards by foreaching.
985
			if(file_exists($workingdir)) {
986
				$dir = opendir($workingdir);
987
				while ($entry = readdir($dir)) {
988
					if (is_file("$workingdir/$entry") || ((!$follow_links) && is_link("$workingdir/$entry")))
989
					unlink("$workingdir/$entry");
990
					elseif (is_dir("$workingdir/$entry") && $entry!='.' && $entry!='..')
991
					rmdir_recursive("$workingdir/$entry");
992
				}
993
				closedir($dir);
994
				rmdir($workingdir);
995
                	}
996
		}
997
		return;
998
	}
999
	return;
1000
}
1001

    
1002
/*
1003
 * safe_mkdir($path, $mode = 0755)
1004
 * create directory if it doesn't already exist and isn't a file!
1005
 */
1006
function safe_mkdir($path, $mode=0755) {
1007
	if (!is_file($path) && !is_dir($path))
1008
		return mkdir($path, $mode);
1009
	else
1010
		return false;
1011
}
1012

    
1013
/*
1014
 * make_dirs($path, $mode = 0755)
1015
 * create directory tree recursively (mkdir -p)
1016
 */
1017
function make_dirs($path, $mode = 0755)
1018
{
1019
	return is_dir($path) || (make_dirs(dirname($path), $mode) && safe_mkdir($path, $mode));
1020
}
1021

    
1022
/****f* pfsense-utils/auto_upgrade
1023
 * NAME
1024
 *   auto_upgrade - pfSense autoupdate handler.
1025
 * FUNCTION
1026
 *   Begin the pfSense autoupdate process. This function calls check_firmware_version to get
1027
 *   a list of current versions and then loops through them, applying binary diffs etc.
1028
 * RESULT
1029
 *   null
1030
 * BUGS
1031
 *   This function needs to have logic in place to automatically switch over to full updates
1032
 *   if a certain amount of binary diffs do not apply successfully.
1033
 * SEE ALSO
1034
 *   pfsense.utils/check_firmware_version
1035
 ******/
1036
function auto_upgrade() {
1037
        global $config, $g;
1038
	if (isset($config['system']['alt_firmware_url']['enabled'])) {
1039
                $firmwareurl=$config['system']['alt_firmware_url']['firmware_base_url'];
1040
                $firmwarepath=$config['system']['alt_firmware_url']['firmware_filename'];
1041
        } else {
1042
                $firmwareurl=$g['firmwarebaseurl'];
1043
                $firmwarepath=$g['firmwarefilename'];
1044
        }
1045
        if($config['system']['proxy_auth_username'] <> "")
1046
	    $http_auth_username = $config['system']['proxy_auth_username'];
1047
        if($config['system']['proxy_auth_password'] <> "")
1048
	    $http_auth_password = $config['system']['proxy_auth_password'];
1049
        if (isset($config['system']['alt_firmware_url']['enabled'])) {
1050
                $firmwareurl=$config['system']['alt_firmware_url']['firmware_base_url'];
1051
                $firmwarename=$config['system']['alt_firmware_url']['firmware_filename'];
1052
        } else {
1053
                $firmwareurl=$g['firmwarebaseurl'];
1054
                $firmwarename=$g['firmwarefilename'];
1055
        }
1056
        exec_rc_script_async("/etc/rc.firmware_auto {$firmwareurl} {$firmwarename} {$http_auth_username} {$http_auth_password}");
1057
	return;
1058
}
1059

    
1060
/*
1061
 * check_firmware_version(): Check whether the current firmware installed is the most recently released.
1062
 */
1063
function check_firmware_version($return_php = true) {
1064
        global $g;
1065
	$versioncheck_base_url = $g['versioncheckbaseurl'];
1066
        $versioncheck_path = $g['versioncheckpath'];
1067
        if(isset($config['system']['alt_firmware_url']['enabled']) and isset($config['system']['alt_firmware_url']['versioncheck_base_url'])) {
1068
                $versioncheck_base_url = $config['system']['alt_firmware_url']['versioncheck_base_url'];
1069
	}
1070
        $params = array(new XML_RPC_Value(trim(file_get_contents('/etc/platform')), 'string'),
1071
                        new XML_RPC_Value(trim(file_get_contents('/etc/version')), 'string'),
1072
                        new XML_RPC_Value(trim(file_get_contents('/etc/version_kernel')), 'string'),
1073
                        new XML_RPC_Value(trim(file_get_contents('/etc/version_base')), 'string'));
1074
        $msg = new XML_RPC_Message('pfsense.get_firmware_version', $params);
1075
        $cli = new XML_RPC_Client($versioncheck_path, $versioncheck_base_url);
1076
        $resp = $cli->send($msg);
1077
	if(!$resp) return -1;
1078
	if($resp->faultCode()) return -1;
1079
        if($return_php == false) return $resp->serialize();
1080
	$raw_versions = $resp->value();
1081
	$toreturn = array();
1082
	for($i = 0; $i < $raw_versions->arraysize(); $i++) {
1083
		$arrayval = $raw_versions->arraymem($i);
1084
		$toreturn[] = $arrayval->scalarval();
1085
	}
1086
	return $toreturn;
1087
}
1088

    
1089
?>
(10-10/18)