Project

General

Profile

Download (29 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/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
    /* basically cache the contents of ifconfig statement
373
       to speed up this routine */
374
    global $carp_query;
375
    if($carp_query == "")
376
	$carp_query = split("\n", `/sbin/ifconfig | /usr/bin/grep carp`);
377
    $found_interface = 0;
378
    foreach($carp_query as $int) {
379
	if($found_interface == 1) {
380
	    if(stristr($int, "MASTER") == true) return "MASTER";
381
	    if(stristr($int, "BACKUP") == true) return "BACKUP";
382
	    if(stristr($int, "INIT") == true) return "INIT";
383
	    return false;
384
	}
385
	if(stristr($int, $carpinterface) == true) $found_interface=1;
386
    }
387
    return $status;
388
}
389

    
390
/*
391
 * get_pfsync_interface_status($pfsyncinterface): returns the status of a pfsync
392
 */
393
function get_pfsync_interface_status($pfsyncinterface) {
394
    $result = does_interface_exist($pfsyncinterface);
395
    if($result <> true) return;
396
    $status = exec_command("/sbin/ifconfig {$pfsyncinterface} | /usr/bin/grep \"pfsync:\" | /usr/bin/cut -d\" \" -f5");
397
    return $status;
398
}
399

    
400
/*
401
 * find_carp_interface($ip): return the carp interface where an ip is defined
402
 */
403
function find_carp_interface($ip) {
404
    global $find_carp_ifconfig;
405
    if($find_carp_ifconfig == "") {
406
	$find_carp_ifconfig = array();
407
	$num_carp_ints = find_number_of_created_carp_interfaces();
408
	for($x=0; $x<$num_carp_ints; $x++) {
409
	    $find_carp_ifconfig[$x] = exec_command("/sbin/ifconfig carp{$x}");
410
	}
411
    }
412
    $carps = 0;
413
    foreach($find_carp_ifconfig as $fci) {
414
	if(stristr($fci, $ip) == true)
415
	    return "carp{$carps}";
416
	$carps++;
417
    }
418
}
419

    
420
/*
421
 * find_number_of_created_bridges(): returns the number of currently created bridges
422
 */
423
function find_number_of_created_bridges() {
424
    return `/sbin/ifconfig | grep \"bridge[0-999]\:" | wc -l`;
425
}
426

    
427
/*
428
 * add_rule_to_anchor($anchor, $rule): adds the specified rule to an anchor
429
 */
430
function add_rule_to_anchor($anchor, $rule, $label) {
431
    mwexec("echo " . $rule . " | /sbin/pfctl -a " . $anchor . ":" . $label . " -f -");
432
}
433

    
434
/*
435
 * remove_text_from_file
436
 * remove $text from file $file
437
 */
438
function remove_text_from_file($file, $text) {
439
    global $fd_log;
440
    fwrite($fd_log, "Adding needed text items:\n");
441
    $filecontents = exec_command_and_return_text("cat " . $file);
442
    $textTMP = str_replace($text, "", $filecontents);
443
    $text .= $textTMP;
444
    fwrite($fd_log, $text . "\n");
445
    $fd = fopen($file, "w");
446
    fwrite($fd, $text);
447
    fclose($fd);
448
}
449

    
450
/*
451
 * add_text_to_file($file, $text): adds $text to $file.
452
 * replaces the text if it already exists.
453
 */
454
function add_text_to_file($file, $text) {
455
    global $fd_log;
456
    fwrite($fd_log, "Adding needed text items:\n");
457
    $filecontents = exec_command_and_return_text("cat " . $file);
458
    $filecontents = str_replace($text, "", $filecontents);
459
    $text = $filecontents . $text;
460
    fwrite($fd_log, $text . "\n");
461
    $fd = fopen($file, "w");
462
    fwrite($fd, $text . "\n");
463
    fclose($fd);
464
}
465

    
466
/*
467
 * get_filename_from_url($url): converts a url to its filename.
468
 */
469
function get_filename_from_url($url) {
470
    $filenamesplit = split("/", $url);
471
    foreach($filenamesplit as $fn) $filename = $fn;
472
    return $filename;
473
}
474

    
475
/*
476
 *   update_output_window: update bottom textarea dynamically.
477
 */
478
function update_output_window($text) {
479
    $log = ereg_replace("\n", "\\n", $text);
480
    echo "\n<script language=\"JavaScript\">this.document.forms[0].output.value = \"" . $log . "\";</script>";
481
}
482

    
483
/*
484
 *   get_dir: return an array of $dir
485
 */
486
function get_dir($dir) {
487
    $dir_array = array();
488
    $d = dir($dir);
489
    while (false !== ($entry = $d->read())) {
490
	array_push($dir_array, $entry);
491
    }
492
    $d->close();
493
    return $dir_array;
494
}
495

    
496
/*
497
 *   update_output_window: update top textarea dynamically.
498
 */
499
function update_status($status) {
500
    echo "\n<script language=\"JavaScript\">document.forms[0].status.value=\"" . $status . "\";</script>";
501
}
502

    
503
/*
504
 *   exec_command_and_return_text_array: execute command and return output
505
 */
506
function exec_command_and_return_text_array($command) {
507
    $counter = 0;
508
    $fd = popen($command . " 2>&1 ", "r");
509
    while(!feof($fd)) {
510
	$tmp .= fread($fd,49);
511
    }
512
    fclose($fd);
513
    $temp_array = split("\n", $tmp);
514
    return $tmp_array;
515
}
516

    
517
/*
518
 *   exec_command_and_return_text: execute command and return output
519
 */
520
function exec_command_and_return_text($command) {
521
    return exec_command($command);
522
}
523

    
524
/*
525
 *   exec_command_and_return_text: execute command and update output window dynamically
526
 */
527
function execute_command_return_output($command) {
528
    global $fd_log;
529
    $fd = popen($command . " 2>&1 ", "r");
530
    echo "\n<script language=\"JavaScript\">this.document.forms[0].output.value = \"\";</script>";
531
    $counter = 0;
532
    $counter2 = 0;
533
    while(!feof($fd)) {
534
	$tmp = fread($fd, 50);
535
	$tmp1 = ereg_replace("\n","\\n", $tmp);
536
	$text = ereg_replace("\"","'", $tmp1);
537
	if($lasttext == "..") {
538
	    $text = "";
539
	    $lasttext = "";
540
	    $counter=$counter-2;
541
	} else {
542
	    $lasttext .= $text;
543
	}
544
	if($counter > 51) {
545
	    $counter = 0;
546
	    $extrabreak = "\\n";
547
	} else {
548
	    $extrabreak = "";
549
	    $counter++;
550
	}
551
	if($counter2 > 600) {
552
	    echo "\n<script language=\"JavaScript\">this.document.forms[0].output.value = \"\";</script>";
553
	    $counter2 = 0;
554
	} else
555
	    $counter2++;
556
	echo "\n<script language=\"JavaScript\">this.document.forms[0].output.value = this.document.forms[0].output.value + \"" . $text . $extrabreak .  "\"; f('output'); </script>";
557
    }
558
    fclose($fd);
559
}
560

    
561
/*
562
 * convert_friendly_interface_to_real_interface_name($interface): convert WAN to FXP0
563
 */
564
function convert_friendly_interface_to_real_interface_name($interface) {
565
    global $config;
566
    $lc_interface = strtolower($interface);
567
    if($lc_interface == "lan") return $config['interfaces']['lan']['if'];
568
    if($lc_interface == "wan") return $config['interfaces']['wan']['if'];
569
    $i = 0;
570
    $ifdescrs = array();
571
    for ($j = 1; isset($config['interfaces']['opt' . $j]); $j++)
572
	$ifdescrs['opt' . $j] = "opt" . $j;
573
    foreach ($ifdescrs as $ifdescr => $ifname) {
574
	if(strtolower($ifname) == $lc_interface)
575
	    return $config['interfaces'][$ifname]['if'];
576
	if(strtolower($config['interfaces'][$ifname]['descr']) == $lc_interface)
577
	    return $config['interfaces'][$ifname]['if'];
578
    }
579
    return $interface;
580
}
581

    
582
/*
583
 * convert_real_interface_to_friendly_interface_name($interface): convert fxp0 -> wan, etc.
584
 */
585
function convert_real_interface_to_friendly_interface_name($interface) {
586
    global $config;
587
    $i = 0;
588
    $ifdescrs = array('wan', 'lan');
589
    for ($j = 1; isset($config['interfaces']['opt' . $j]); $j++)
590
	$ifdescrs['opt' . $j] = "opt" . $j;
591
    foreach ($ifdescrs as $ifdescr => $ifname) {
592
	$int = filter_translate_type_to_real_interface($ifname);
593
	if($ifname == $interface) return $ifname;
594
	if($int == $interface) return $ifname;
595
    }
596
    return $interface;
597
}
598

    
599
/*
600
 * update_progress_bar($percent): updates the javascript driven progress bar.
601
 */
602
function update_progress_bar($percent) {
603
    if($percent > 100) $percent = 1;
604
    echo "\n<script type=\"text/javascript\" language=\"javascript\">";
605
    echo "\ndocument.progressbar.style.width='" . $percent . "%';";
606
    echo "\n</script>";
607
}
608

    
609
/*
610
 * gather_altq_queue_stats():  gather alq queue stats and return an array that
611
 *                             is queuename|qlength|measured_packets
612
 *                             NOTE: this command takes 5 seconds to run
613
 */
614
function gather_altq_queue_stats($dont_return_root_queues) {
615
    mwexec("/usr/bin/killall -9 pfctl");
616
    $stats = `/sbin/pfctl -vvsq & /bin/sleep 5;/usr/bin/killall pfctl 2>/dev/null`;
617
    $stats_array = split("\n", $stats);
618
    $queue_stats = array();
619
    foreach ($stats_array as $stats_line) {
620
        if (preg_match_all("/queue\s+(\w+)\s+/",$stats_line,$match_array))
621
            $queue_name = $match_array[1][0];
622
        if (preg_match_all("/measured:\s+.*packets\/s\,\s(.*)\s+\]/",$stats_line,$match_array))
623
            $speed = $match_array[1][0];
624
        if (preg_match_all("/borrows:\s+(.*)/",$stats_line,$match_array))
625
            $borrows = $match_array[1][0];
626
        if (preg_match_all("/suspends:\s+(.*)/",$stats_line,$match_array))
627
            $suspends = $match_array[1][0];
628
        if (preg_match_all("/dropped pkts:\s+(.*)/",$stats_line,$match_array))
629
            $drops = $match_array[1][0];
630
        if (preg_match_all("/measured:\s+(.*)packets/",$stats_line,$match_array)) {
631
            $measured = $match_array[1][0];
632
	    if($dont_return_root_queues == true)
633
		if(stristr($queue_name,"root_") == false)
634
		    array_push($queue_stats, "{$queue_name}|{$speed}|{$measured}|{$borrows}|{$suspends}|{$drops}");
635
        }
636
    }
637
    return $queue_stats;
638
}
639

    
640
/*
641
 * reverse_strrchr($haystack, $needle):  Return everything in $haystack up to the *last* instance of $needle.
642
 *					 Useful for finding paths and stripping file extensions.
643
 */
644
function reverse_strrchr($haystack, $needle)
645
{
646
               return strrpos($haystack, $needle) ? substr($haystack, 0, strrpos($haystack, $needle) +1 ) : false;
647
}
648

    
649
/*
650
 * is_service_running($service_name): checks to see if a service is running.
651
 *                                    if the service is running returns 1.
652
 */
653
function is_service_running($service_name) {
654
    $status = `/bin/ps ax | /usr/bin/grep {$service_name} | wc -l`;
655
    if($status > 2) return 1;
656
    return 0;
657
}
658

    
659
/*
660
 *  backup_config_section($section): returns as an xml file string of
661
 *                                   the configuration section
662
 */
663
function backup_config_section($section) {
664
    global $config;
665
    $new_section = &$config[$section];
666
    /* generate configuration XML */
667
    $xmlconfig = dump_xml_config($new_section, $section);
668
    $xmlconfig = str_replace("<?xml version=\"1.0\"?>", "", $xmlconfig);
669
    return $xmlconfig;
670
}
671

    
672
/*
673
 *  restore_config_section($section, new_contents): restore a configuration section,
674
 *                                                  and write the configuration out
675
 *                                                  to disk/cf.
676
 */
677
function restore_config_section($section, $new_contents) {
678
    global $config;
679
    $fout = fopen("{$g['tmp_path']}/tmpxml","w");
680
    fwrite($fout, $new_contents);
681
    fclose($fout);
682
    $section_xml = parse_xml_config_pkg($g['tmp_path'] . "/tmpxml", $section);
683
    $config[$section] = &$section_xml;
684
    unlink($g['tmp_path'] . "/tmpxml");
685
    write_config("Restored {$section} of config file (maybe from CARP partner)");
686
    return;
687
}
688

    
689
/*
690
 * http_post($server, $port, $url, $vars): does an http post to a web server
691
 *                                         posting the vars array.
692
 * written by nf@bigpond.net.au
693
 */
694
function http_post($server, $port, $url, $vars) {
695
    $user_agent = "Mozilla/4.0 (compatible; MSIE 5.5; Windows 98)";
696
    $urlencoded = "";
697
    while (list($key,$value) = each($vars))
698
	$urlencoded.= urlencode($key) . "=" . urlencode($value) . "&";
699
    $urlencoded = substr($urlencoded,0,-1);
700

    
701
    $content_length = strlen($urlencoded);
702

    
703
    $headers = "POST $url HTTP/1.1
704
Accept: */*
705
Accept-Language: en-au
706
Content-Type: application/x-www-form-urlencoded
707
User-Agent: $user_agent
708
Host: $server
709
Connection: Keep-Alive
710
Cache-Control: no-cache
711
Content-Length: $content_length
712

    
713
";
714

    
715
    $fp = fsockopen($server, $port, $errno, $errstr);
716
    if (!$fp) {
717
	return false;
718
    }
719

    
720
    fputs($fp, $headers);
721
    fputs($fp, $urlencoded);
722

    
723
    $ret = "";
724
    while (!feof($fp))
725
	$ret.= fgets($fp, 1024);
726

    
727
    fclose($fp);
728

    
729
    return $ret;
730

    
731
}
732

    
733
/*
734
 *  php_check_syntax($code_tocheck, $errormessage): checks $code_to_check for errors
735
 */
736
if (!function_exists('php_check_syntax')){
737
   function php_check_syntax($code_to_check, &$errormessage){
738
	return false;
739
        $fout = fopen("/tmp/codetocheck.php","w");
740
        $code = $_POST['content'];
741
        $code = str_replace("<?php", "", $code);
742
        $code = str_replace("?>", "", $code);
743
        fwrite($fout, "<?php\n\n");
744
        fwrite($fout, $code);
745
        fwrite($fout, "\n\n?>\n");
746
        fclose($fout);
747
        $command = "/usr/local/bin/php -l /tmp/codetocheck.php";
748
        $output = exec_command($command);
749
        if (stristr($output, "Errors parsing") == false) {
750
            echo "false\n";
751
            $errormessage = '';
752
            return(false);
753
        } else {
754
            $errormessage = $output;
755
            return(true);
756
        }
757
    }
758
}
759

    
760
/*
761
 *  php_check_filename_syntax($filename, $errormessage): checks the file $filename for errors
762
 */
763
if (!function_exists('php_check_syntax')){
764
   function php_check_syntax($code_to_check, &$errormessage){
765
	return false;
766
        $command = "/usr/local/bin/php -l " . $code_to_check;
767
        $output = exec_command($command);
768
        if (stristr($output, "Errors parsing") == false) {
769
            echo "false\n";
770
            $errormessage = '';
771
            return(false);
772
        } else {
773
            $errormessage = $output;
774
            return(true);
775
        }
776
    }
777
}
778

    
779
/*
780
 * rmdir_recursive($path,$follow_links=false)
781
 * Recursively remove a directory tree (rm -rf path)
782
 * This is for directories _only_
783
 */
784
function rmdir_recursive($path,$follow_links=false) {
785
	$to_do = glob($path);
786
	if(!is_array($to_do)) $to_do = array($to_do);
787
	foreach($to_do as $workingdir) { // Handle wildcards by foreaching.
788
		if(file_exists($workingdir)) {
789
			if(is_dir($workingdir)) {
790
				$dir = opendir($workingdir);
791
				while ($entry = readdir($dir)) {
792
					if (is_file("$workingdir/$entry") || ((!$follow_links) && is_link("$workingdir/$entry")))
793
						unlink("$workingdir/$entry");
794
					elseif (is_dir("$workingdir/$entry") && $entry!='.' && $entry!='..')
795
						rmdir_recursive("$workingdir/$entry");
796
				}
797
				closedir($dir);
798
				rmdir($workingdir);
799
			} elseif (is_file($workingdir)) {
800
				unlink($workingdir);
801
			}
802
               	}
803
	}
804
	return;
805
}
806

    
807
/*
808
 * safe_mkdir($path, $mode = 0755)
809
 * create directory if it doesn't already exist and isn't a file!
810
 */
811
function safe_mkdir($path, $mode=0755) {
812
	global $g;
813
	
814
	if($g['platform'] == "cdrom")
815
		return;	
816
	
817
	if (!is_file($path) && !is_dir($path))
818
		return mkdir($path, $mode);
819
	else
820
		return false;
821
}
822

    
823
/*
824
 * make_dirs($path, $mode = 0755)
825
 * create directory tree recursively (mkdir -p)
826
 */
827
function make_dirs($path, $mode = 0755)
828
{
829
	return is_dir($path) || (make_dirs(dirname($path), $mode) && safe_mkdir($path, $mode));
830
}
831

    
832
/****f* pfsense-utils/auto_upgrade
833
 * NAME
834
 *   auto_upgrade - pfSense autoupdate handler.
835
 * FUNCTION
836
 *   Begin the pfSense autoupdate process. This function calls check_firmware_version to get
837
 *   a list of current versions and then loops through them, applying binary diffs etc.
838
 * RESULT
839
 *   null
840
 * BUGS
841
 *   This function needs to have logic in place to automatically switch over to full updates
842
 *   if a certain amount of binary diffs do not apply successfully.
843
 * SEE ALSO
844
 *   pfsense.utils/check_firmware_version
845
 ******/
846
function auto_upgrade() {
847
        global $config, $g;
848
	if (isset($config['system']['alt_firmware_url']['enabled'])) {
849
                $firmwareurl=$config['system']['alt_firmware_url']['firmware_base_url'];
850
                $firmwarepath=$config['system']['alt_firmware_url']['firmware_filename'];
851
        } else {
852
                $firmwareurl=$g['firmwarebaseurl'];
853
                $firmwarepath=$g['firmwarefilename'];
854
        }
855
        if($config['system']['proxy_auth_username'] <> "")
856
	    $http_auth_username = $config['system']['proxy_auth_username'];
857
        if($config['system']['proxy_auth_password'] <> "")
858
	    $http_auth_password = $config['system']['proxy_auth_password'];
859
        if (isset($config['system']['alt_firmware_url']['enabled'])) {
860
                $firmwareurl=$config['system']['alt_firmware_url']['firmware_base_url'];
861
                $firmwarename=$config['system']['alt_firmware_url']['firmware_filename'];
862
        } else {
863
                $firmwareurl=$g['firmwarebaseurl'];
864
                $firmwarename=$g['firmwarefilename'];
865
        }
866
        exec_rc_script_async("/etc/rc.firmware_auto {$firmwareurl} {$firmwarename} {$http_auth_username} {$http_auth_password}");
867
	return;
868
}
869

    
870
/*
871
 * check_firmware_version(): Check whether the current firmware installed is the most recently released.
872
 */
873
function check_firmware_version($tocheck = "all", $return_php = true) {
874
        global $g;
875
	$versioncheck_base_url = $g['versioncheckbaseurl'];
876
        $versioncheck_path = $g['versioncheckpath'];
877
        if(isset($config['system']['alt_firmware_url']['enabled']) and isset($config['system']['alt_firmware_url']['versioncheck_base_url'])) {
878
                $versioncheck_base_url = $config['system']['alt_firmware_url']['versioncheck_base_url'];
879
	}
880
	$rawparams = array("firmware" => array("version" => trim(file_get_contents('/etc/version'))),
881
			"kernel"   => array("version" => trim(file_get_contents('/etc/version_kernel'))),
882
			"base"     => array("version" => trim(file_get_contents('/etc/version_base'))),
883
			"platform" => trim(file_get_contents('/etc/platform'))
884
		);
885
	if($tocheck = "all") {
886
		$params = $rawparams;
887
	} else {
888
		foreach($tocheck as $check) {
889
			$params['check'] = $rawparams['check'];
890
			$params['platform'] = $rawparams['platform'];
891
		}
892
	}
893
	if(isset($config['system']['firmwarebranch'])) {
894
		$params['branch'] = $config['system']['firmwarebranch'];
895
	}
896
	$xmlparams = php_value_to_xmlrpc($params);
897
        $msg = new XML_RPC_Message('pfsense.get_firmware_version', array($xmlparams));
898
        $cli = new XML_RPC_Client($versioncheck_path, $versioncheck_base_url);
899
	$resp = $cli->send($msg, 10);
900
	if(!$resp or $resp->faultCode()) {
901
		$raw_versions = false;
902
	} else {
903
		$raw_versions = xmlrpc_value_to_php($resp->value());
904
		$raw_versions["current"] = $params;
905
	}
906
	return $raw_versions;
907
}
908

    
909
/*
910
 * fetch_latest_pkg_config: download the latest pkg_config.xml to /tmp/ directory
911
 */
912
function fetch_latest_pkg_config() {
913
    global $g;
914
    global $config;
915
    if(!file_exists("{$g['tmp_path']}/pkg_config.xml")) {
916
	$pkg_config_location = $g['pkg_config_location'];
917
	$pkg_config_base_url = $g['pkg_config_base_url'];
918
	if(isset($config['system']['alt_pkgconfig_url']['enabled'])) {
919
	    $pkg_config_location = $config['system']['alt_pkgconfig_url']['pkgconfig_base_url'] . $config['system']['alt_pkgconfig_url']['pkgconfig_filename'];
920
	    $pkg_config_base_url = $config['system']['alt_pkgconfig_url']['pkgconfig_base_url'];
921
	}
922
	mwexec("/usr/bin/fetch -o {$g['tmp_path']}/pkg_config.xml {$pkg_config_location}");
923
	if(!file_exists("{$g['tmp_path']}/pkg_config.xml")) {
924
	    print_info_box_np("Could not download pkg_config.xml from " . $pkg_config_base_url . ". Check your DNS settings.");
925
	    die;
926
    	}
927
    }
928
    return;
929
}
930

    
931
?>
(11-11/20)