Project

General

Profile

Download (29.5 KB) Statistics
| Branch: | Tag: | Revision:
1 7b2274cb Scott Ullrich
<?php
2
/*
3
	pfSense-utils.inc
4
	Utilities specific to pfSense
5
	part of pfSense (www.pfSense.com)
6
7
	Copyright (C) 2005 Scott Ullrich (sullrich@gmail.com)
8
	All rights reserved.
9
10
	Redistribution and use in source and binary forms, with or without
11
	modification, are permitted provided that the following conditions are met:
12
13
	1. Redistributions of source code must retain the above copyright notice,
14
	   this list of conditions and the following disclaimer.
15
16
	2. Redistributions in binary form must reproduce the above copyright
17
	   notice, this list of conditions and the following disclaimer in the
18
	   documentation and/or other materials provided with the distribution.
19
20
	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
21
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
22
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
24
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29
	POSSIBILITY OF SUCH DAMAGE.
30
*/
31
32 622fa35a Scott Ullrich
/*
33
 * log_error: send string to syslog
34 5ccfea33 Scott Ullrich
 */
35 7b2274cb Scott Ullrich
function log_error($error) {
36 98a8e831 Scott Ullrich
    syslog(LOG_WARNING, $error);
37 7b2274cb Scott Ullrich
    return;
38
}
39
40 622fa35a Scott Ullrich
/*
41
 * return_dir_as_array($dir): returns $dir contents as an array
42
 */
43
function return_dir_as_array($dir) {
44 174861fd Scott Ullrich
    $dir_array = array();
45 622fa35a Scott Ullrich
    if (is_dir($dir)) {
46 5c52313f Colin Smith
	If ($dh = opendir($dir)) {
47 622fa35a Scott Ullrich
	    while (($file = readdir($dh)) !== false) {
48 174861fd Scott Ullrich
		$canadd = 0;
49
		if($file == ".") $canadd = 1;
50
		if($file == "..") $canadd = 1;
51
		if($canadd == 0)
52
		    array_push($dir_array, $file);
53 622fa35a Scott Ullrich
	    }
54
	    closedir($dh);
55
	}
56
    }
57
    return $dir_array;
58
}
59 7b2274cb Scott Ullrich
60 ed059571 Scott Ullrich
/*
61
 * enable_hardware_offloading() enables hardware features of nics if they are supported
62
 */
63
function enable_hardware_offloading($interface) {
64 31a82233 Scott Ullrich
    global $config;
65
    global $g;
66
    if($g['booting']) {
67
	$supported_ints = array('fxp');
68
	foreach($supported_ints as $int) {
69
	    if(stristr($interface,$int) != false) {
70
		mwexec("/sbin/ifconfig $interface link0");
71
	    }
72 ed059571 Scott Ullrich
	}
73 c9b4da10 Scott Ullrich
    }
74 ed059571 Scott Ullrich
}
75
76 023c3cc0 Scott Ullrich
/*
77 3ff9d424 Scott Ullrich
 * return_filename_as_array($filename): returns $filename contents as a string
78
 */
79
function return_filename_as_array($filename) {
80
    $file = array();
81 0dd62c88 Bill Marquette
    if(file_exists($filename)) {
82
        $text = return_filename_as_string($filename);
83
        $text_split = split("\n", $text);
84
85
        /* Strip out comments */
86
        while (($line = array_shift($text_split)) != NULL) {
87
            if(strpos($line, "#") !== 0)
88
                array_push($file, $line);
89
        }
90 98b77940 Scott Ullrich
    }
91 0dd62c88 Bill Marquette
    return $file;
92 3ff9d424 Scott Ullrich
}
93
94
/*
95
 * return_dir_as_array($filename): returns $filename contents as a string
96 023c3cc0 Scott Ullrich
 */
97
function return_filename_as_string($filename) {
98
    $tmp = "";
99 174861fd Scott Ullrich
    $fd = fopen($filename, "r");
100 a33fd568 Scott Ullrich
    if(!$fd) {
101
	log_error("Could not open {$filename}");
102
	return;
103
    }
104 023c3cc0 Scott Ullrich
    while(!feof($fd)) {
105
	$tmp .= fread($fd,49);
106
    }
107
    fclose($fd);
108
    return $tmp;
109
}
110
111 a8ac6c98 Scott Ullrich
/*
112
 * is_carp_defined: returns true if carp is detected in kernel
113
 */
114
function is_carp_defined() {
115
	/* is carp compiled into the kernel and userland? */
116
	$command = "/sbin/sysctl -a | grep carp";
117
	$fd = popen($command . " 2>&1 ", "r");
118
	if(!$fd) {
119 ed059571 Scott Ullrich
		log_error("Warning, could not execute command {$command}");
120 a8ac6c98 Scott Ullrich
		return 0;
121
	}
122
	while(!feof($fd)) {
123
		    $tmp .= fread($fd,49);
124
	}
125
	fclose($fd);
126
127
	if($tmp == "")
128
		return false;
129
	else
130
		return true;
131
}
132
133 ed059571 Scott Ullrich
/*
134
 * find_number_of_created_carp_interfaces() returns the number of currently created carp interfaces
135
 */
136
function find_number_of_created_carp_interfaces() {
137 e3939e20 Scott Ullrich
	$command = "/sbin/ifconfig | /usr/bin/grep \"carp*:\" | /usr/bin/wc -l";
138 ed059571 Scott Ullrich
	$fd = popen($command . " 2>&1 ", "r");
139
	if(!$fd) {
140
		log_error("Warning, could not execute command {$command}");
141
		return 0;
142
	}
143
	while(!feof($fd)) {
144
		    $tmp .= fread($fd,49);
145
	}
146
	fclose($fd);
147 8d8f27a1 Scott Ullrich
	$tmp = intval($tmp);
148 ed059571 Scott Ullrich
	return $tmp;
149
}
150
151 fa65a62b Scott Ullrich
/*
152
 * link_ip_to_carp_interface($ip): finds where a carp interface links to.
153
*/
154
function link_ip_to_carp_interface($ip) {
155 e3939e20 Scott Ullrich
	global $config;
156
	if($ip == "") return;
157 fa65a62b Scott Ullrich
        $i = 0;
158
159
        $ifdescrs = array('wan', 'lan');
160
        for ($j = 1; isset($config['interfaces']['opt' . $j]); $j++) {
161
                $ifdescrs['opt' . $j] = "opt" . $j;
162
        }
163
164
	$ft = split("\.", $ip);
165
	$ft_ip = $ft[0] . "." . $ft[1] . "." . $ft[2] . ".";
166
167 737ae333 Scott Ullrich
	$carp_ints = "";
168 ed059571 Scott Ullrich
	$num_carp_ints = find_number_of_created_carp_interfaces();
169 fa65a62b Scott Ullrich
        foreach ($ifdescrs as $ifdescr => $ifname) {
170 ed059571 Scott Ullrich
                for($x=0; $x<$num_carp_ints; $x++) {
171 fa65a62b Scott Ullrich
                        $carp_int = "carp{$x}";
172
			$carp_ip = find_interface_ip($carp_int);
173
			$carp_ft = split("\.", $carp_ip);
174
			$carp_ft_ip = $carp_ft[0] . "." . $carp_ft[1] . "." . $carp_ft[2] . ".";
175
                        $result = does_interface_exist($carp_int);
176
                        if($result <> true) break;
177
                        $interface = filter_opt_interface_to_real($ifname);
178
			if($ft_ip == $carp_ft_ip)
179 83661f77 Scott Ullrich
			    if(stristr($carp_ints,$carp_int) == false)
180 24452897 Scott Ullrich
				$carp_ints .= " " . $carp_int;
181 fa65a62b Scott Ullrich
                }
182
        }
183 737ae333 Scott Ullrich
	return $carp_ints;
184 fa65a62b Scott Ullrich
}
185
186
187 5ccfea33 Scott Ullrich
/*
188
 * exec_command($command): execute command return string of result
189
 */
190
function exec_command($command) {
191
            $counter = 0;
192
            $tmp = "";
193
            $fd = popen($command . " 2>&1 ", "r");
194
            while(!feof($fd)) {
195
                        $tmp .= fread($fd,49);
196
            }
197
            fclose($fd);
198
            return $tmp;
199
}
200
201 fa65a62b Scott Ullrich
/*
202
 * does_interface_exist($interface): return true or false if a interface is detected.
203
 */
204
function does_interface_exist($interface) {
205
    $ints = exec_command("/sbin/ifconfig -l");
206 83661f77 Scott Ullrich
    if(stristr($ints, $interface) !== false)
207 fa65a62b Scott Ullrich
	return true;
208
    else
209
	return false;
210
}
211
212 5ccfea33 Scott Ullrich
/*
213
 * convert_ip_to_network_format($ip, $subnet): converts an ip address to network form
214
 */
215
function convert_ip_to_network_format($ip, $subnet) {
216
    $ipsplit = split('[.]', $ip);
217
    $string = $ipsplit[0] . "." . $ipsplit[1] . "." . $ipsplit[2] . ".0/" . $subnet;
218
    return $string;
219
}
220
221 b04a6ca4 Scott Ullrich
/*
222
 * find_interface_ip($interface): return the interface ip (first found)
223
 */
224
function find_interface_ip($interface) {
225 4983ed8c Scott Ullrich
    if(does_interface_exist($interface) == false) return;
226 e3939e20 Scott Ullrich
    $ip = exec_command("/sbin/ifconfig {$interface} | /usr/bin/grep -w \"inet\" | /usr/bin/cut -d\" \" -f 2");
227 e67f187a Scott Ullrich
    $ip = str_replace("\n","",$ip);
228 b04a6ca4 Scott Ullrich
    return $ip;
229
}
230
231 fa65a62b Scott Ullrich
function filter_opt_interface_to_real($opt) {
232
	global $config;
233
	return $config['interfaces'][$opt]['if'];
234
}
235
236
function filter_get_opt_interface_descr($opt) {
237
	global $config;
238
	return $config['interfaces'][$opt]['descr'];
239
}
240
241 b73cc056 Scott Ullrich
function get_friendly_interface_list_as_array() {
242
	global $config;
243
	$ints = array();
244
	$i = 0;
245
	$ifdescrs = array('wan', 'lan');
246
	for ($j = 1; isset($config['interfaces']['opt' . $j]); $j++) {
247
		$ifdescrs['opt' . $j] = "opt" . $j;
248
	}
249
	$ifdescrs = get_interface_list();
250
	foreach ($ifdescrs as $ifdescr => $ifname) {
251
	    array_push($ints,$ifdescr);
252
	}
253
	return $ints;
254
}
255
256 5ccfea33 Scott Ullrich
/*
257
 * find_ip_interface($ip): return the interface where an ip is defined
258
 */
259
function find_ip_interface($ip) {
260 e3939e20 Scott Ullrich
	global $config;
261 5ccfea33 Scott Ullrich
	$i = 0;
262
	$ifdescrs = array('wan', 'lan');
263
	for ($j = 1; isset($config['interfaces']['opt' . $j]); $j++) {
264
		$ifdescrs['opt' . $j] = "opt" . $j;
265
	}
266
	foreach ($ifdescrs as $ifdescr => $ifname) {
267
	    $int = filter_translate_type_to_real_interface($ifname);
268
	    $ifconfig = exec_command("/sbin/ifconfig {$int}");
269
	    if(stristr($ifconfig,$ip) <> false)
270
		return $int;
271
	}
272
	return false;
273
}
274
275 bc5d2a26 Scott Ullrich
/*
276
 *  filter_translate_type_to_real_interface($interface): returns the real interface name
277
 *                                                       for a friendly interface.  ie: wan
278
 */
279
function filter_translate_type_to_real_interface($interface) {
280
        global $config;
281
	return $config['interfaces'][$interface]['if'];
282
}
283
284 591afdfd Scott Ullrich
/*
285
 * get_carp_interface_status($carpinterface): returns the status of a carp ip
286
 */
287
function get_carp_interface_status($carpinterface) {
288 962f7d25 Scott Ullrich
    $result = does_interface_exist($carpinterface);
289 83661f77 Scott Ullrich
    if($result <> true) return false;
290 e3939e20 Scott Ullrich
    $status = exec_command("/sbin/ifconfig {$carpinterface} | /usr/bin/grep \"carp:\" | /usr/bin/cut -d\" \" -f2");
291 591afdfd Scott Ullrich
    return $status;
292
}
293
294 167bcf84 Scott Ullrich
/*
295
 * get_pfsync_interface_status($pfsyncinterface): returns the status of a pfsync
296
 */
297
function get_pfsync_interface_status($pfsyncinterface) {
298 962f7d25 Scott Ullrich
    $result = does_interface_exist($pfsyncinterface);
299 4983ed8c Scott Ullrich
    if($result <> true) return;
300 e3939e20 Scott Ullrich
    $status = exec_command("/sbin/ifconfig {$pfsyncinterface} | /usr/bin/grep \"pfsync:\" | /usr/bin/cut -d\" \" -f5");
301 167bcf84 Scott Ullrich
    return $status;
302
}
303
304 5ccfea33 Scott Ullrich
/*
305
 * find_carp_interface($ip): return the carp interface where an ip is defined
306
 */
307
function find_carp_interface($ip) {
308 ed059571 Scott Ullrich
    $num_carp_ints = find_number_of_created_carp_interfaces();
309
    for($x=0; $x<$num_carp_ints; $x++) {
310 962f7d25 Scott Ullrich
        $result = does_interface_exist("carp{$x}");
311
	if($result <> true) return;
312 f8269a6f Scott Ullrich
	$ifconfig = exec_command("/sbin/ifconfig carp{$x}");
313
	if(stristr($ifconfig,$ip))
314 5ccfea33 Scott Ullrich
	    return "carp" . $x;
315
    }
316
}
317
318
/*
319
 * add_rule_to_anchor($anchor, $rule): adds the specified rule to an anchor
320
 */
321 a35f1242 Scott Ullrich
function add_rule_to_anchor($anchor, $rule, $label) {
322 8c8e6792 Scott Ullrich
    mwexec("echo " . $rule . " | /sbin/pfctl -a " . $anchor . ":" . $label . " -f -");
323 5ccfea33 Scott Ullrich
}
324
325 06e2627e Scott Ullrich
/*
326
 * remove_text_from_file
327
 * remove $text from file $file
328
 */
329
function remove_text_from_file($file, $text) {
330
    global $fd_log;
331
    fwrite($fd_log, "Adding needed text items:\n");
332
    $filecontents = exec_command_and_return_text("cat " . $file);
333
    $textTMP = str_replace($text, "", $filecontents);
334
    $text .= $textTMP;
335
    fwrite($fd_log, $text . "\n");
336
    $fd = fopen($file, "w");
337
    fwrite($fd, $text);
338
    fclose($fd);
339
}
340
341
/*
342
 * lookup pkg array id#
343
 */
344
function get_pkg_id($pkg_name) {
345
            global $config;
346
            global $pkg_config;
347
            $i=0;
348
            foreach ($config['installedpackages']['package'] as $pkg) {
349
                        if($pkg['name'] == $pkg_name) return $i;
350
                        $i++;
351
            }
352
            return -1;
353
}
354
355 3a003406 Scott Ullrich
/*
356
 *  get_latest_package_version($pkgname): get current version of a package.
357
 *  returns latest package version
358
 */
359
function get_latest_package_version($pkg_name) {
360 5f48ff22 Bill Marquette
    global $g;
361 3a003406 Scott Ullrich
    fetch_latest_pkg_config();
362
    $pkg_config = parse_xml_config_pkg("{$g['tmp_path']}/pkg_config.xml", "pfsensepkgs");
363 5f48ff22 Bill Marquette
    foreach($pkg_config['packages']['package'] as $pkg) {
364 2b246073 Colin Smith
	if($pkg['name'] == $pkg_name) {
365
	    return $pkg['version'];
366 3a003406 Scott Ullrich
	}
367
    }
368
    return;
369
}
370 0025d774 Scott Ullrich
371
/*
372
 * Lookup pkg_id in pkg_config.xml
373
 */
374
function get_available_pkg_id($pkg_name) {
375
    fetch_latest_pkg_config();
376
    $pkg_config = parse_xml_config_pkg("{$g['tmp_path']}/pkg_config.xml", "pfsensepkgs");
377
    $id = 0;
378
    foreach($pkg_config as $pkg) {
379
	if($pkg_config['name'] == $pkg_name) {
380
	    return $id;
381
	}
382
	$id++;
383
    }
384
    return;
385
}
386
387
/*
388
 * fetch_latest_pkg_config: download the latest pkg_config.xml to /tmp/ directory
389
 */
390
function fetch_latest_pkg_config() {
391 5f9b482f Scott Ullrich
    global $g;
392 5f48ff22 Bill Marquette
    if(!file_exists("{$g['tmp_path']}/pkg_config.xml")) {
393 d66016a3 Colin Smith
	mwexec("/usr/bin/fetch -o {$g['tmp_path']}/pkg_config.xml {$g['pkg_config_location']}");
394 0025d774 Scott Ullrich
	if(!file_exists("{$g['tmp_path']}/pkg_config.xml")) {
395
	    print_info_box_np("Could not download pkg_config.xml from pfSense.com.  Check your DNS settings.");
396
	    die;
397
	}
398
    }
399
    return;
400
}
401
402 06e2627e Scott Ullrich
/*
403
 * add_text_to_file($file, $text): adds $text to $file.
404
 * replaces the text if it already exists.
405
 */
406
function add_text_to_file($file, $text) {
407
    global $fd_log;
408
    fwrite($fd_log, "Adding needed text items:\n");
409
    $filecontents = exec_command_and_return_text("cat " . $file);
410
    $filecontents = str_replace($text, "", $filecontents);
411
    $text = $filecontents . $text;
412
    fwrite($fd_log, $text . "\n");
413
    $fd = fopen($file, "w");
414
    fwrite($fd, $text . "\n");
415
    fclose($fd);
416
}
417
418
/*
419
 * get_filename_from_url($url): converts a url to its filename.
420
 */
421
function get_filename_from_url($url) {
422
            $filenamesplit = split("/", $url);
423
            foreach($filenamesplit as $fn) $filename = $fn;
424
            return $filename;
425
}
426
427
/*
428
 *   update_output_window: update bottom textarea dynamically.
429
 */
430
function update_output_window($text) {
431
            $log = ereg_replace("\n", "\\n", $text);
432
            echo "\n<script language=\"JavaScript\">this.document.forms[0].output.value = \"" . $log . "\";</script>";
433
}
434
435
/*
436
 *   get_dir: return an array of $dir
437
 */
438
function get_dir($dir) {
439
            $dir_array = array();
440
            $d = dir($dir);
441
            while (false !== ($entry = $d->read())) {
442
                        array_push($dir_array, $entry);
443
            }
444
            $d->close();
445
            return $dir_array;
446
}
447
448
/*
449
 *   update_output_window: update top textarea dynamically.
450
 */
451
function update_status($status) {
452
            echo "\n<script language=\"JavaScript\">document.forms[0].status.value=\"" . $status . "\";</script>";
453
}
454
455
/*
456
 *   exec_command_and_return_text_array: execute command and return output
457
 */
458
function exec_command_and_return_text_array($command) {
459
            $counter = 0;
460
            $fd = popen($command . " 2>&1 ", "r");
461
            while(!feof($fd)) {
462
                        $tmp .= fread($fd,49);
463
            }
464
            fclose($fd);
465
            $temp_array = split("\n", $tmp);
466
            return $tmp_array;
467
}
468
469
/*
470
 *   exec_command_and_return_text: execute command and return output
471
 */
472
function exec_command_and_return_text($command) {
473
	    return exec_command($command);
474
}
475
476
/*
477
 *   exec_command_and_return_text: execute command and update output window dynamically
478
 */
479
function execute_command_return_output($command) {
480
    global $fd_log;
481
    $fd = popen($command . " 2>&1 ", "r");
482
    echo "\n<script language=\"JavaScript\">this.document.forms[0].output.value = \"\";</script>";
483
    $counter = 0;
484
    $counter2 = 0;
485
    while(!feof($fd)) {
486
	$tmp = fread($fd, 50);
487
	$tmp1 = ereg_replace("\n","\\n", $tmp);
488
	$text = ereg_replace("\"","'", $tmp1);
489
	if($lasttext == "..") {
490
	    $text = "";
491
	    $lasttext = "";
492
	    $counter=$counter-2;
493
	} else {
494
	    $lasttext .= $text;
495
	}
496
	if($counter > 51) {
497
	    $counter = 0;
498
	    $extrabreak = "\\n";
499
	} else {
500
	    $extrabreak = "";
501
	    $counter++;
502
	}
503
	if($counter2 > 600) {
504
	    echo "\n<script language=\"JavaScript\">this.document.forms[0].output.value = \"\";</script>";
505
	    $counter2 = 0;
506
	} else
507
	    $counter2++;
508
	echo "\n<script language=\"JavaScript\">this.document.forms[0].output.value = this.document.forms[0].output.value + \"" . $text . $extrabreak .  "\"; f('output'); </script>";
509
    }
510
    fclose($fd);
511
}
512
513 55be70e6 Scott Ullrich
/*
514
 * convert_friendly_interface_to_real_interface_name($interface): convert WAN to FXP0
515
 */
516
function convert_friendly_interface_to_real_interface_name($interface) {
517 54f4caed Scott Ullrich
    global $config;
518 a7f5febb Scott Ullrich
    $lc_interface = strtolower($interface);
519 303831c6 Scott Ullrich
    if($lc_interface == "lan") return $config['interfaces']['lan']['if'];
520
    if($lc_interface == "wan") return $config['interfaces']['wan']['if'];
521 401e59a9 Scott Ullrich
    $i = 0;
522 303831c6 Scott Ullrich
    $ifdescrs = array();
523
    for ($j = 1; isset($config['interfaces']['opt' . $j]); $j++)
524
	$ifdescrs['opt' . $j] = "opt" . $j;
525 401e59a9 Scott Ullrich
    foreach ($ifdescrs as $ifdescr => $ifname) {
526 303831c6 Scott Ullrich
	if(strtolower($ifname) == $lc_interface)
527
	    return $config['interfaces'][$ifname]['if'];
528
	if(strtolower($config['interfaces'][$ifname]['descr']) == $lc_interface)
529 401e59a9 Scott Ullrich
	    return $config['interfaces'][$ifname]['if'];
530
    }
531
    return $interface;
532 55be70e6 Scott Ullrich
}
533
534 cc869478 Scott Ullrich
/*
535
 * convert_real_interface_to_friendly_interface_name($interface): convert fxp0 -> wan, etc.
536
 */
537
function convert_real_interface_to_friendly_interface_name($interface) {
538
	global $config;
539
	$i = 0;
540
	$ifdescrs = array('wan', 'lan');
541 303831c6 Scott Ullrich
	for ($j = 1; isset($config['interfaces']['opt' . $j]); $j++)
542
	    $ifdescrs['opt' . $j] = "opt" . $j;
543 cc869478 Scott Ullrich
	foreach ($ifdescrs as $ifdescr => $ifname) {
544
	    $int = filter_translate_type_to_real_interface($ifname);
545 bc5d2a26 Scott Ullrich
	    if($ifname == $interface) return $ifname;
546 435011b4 Scott Ullrich
	    if($int == $interface) return $ifname;
547 cc869478 Scott Ullrich
	}
548
	return $interface;
549
}
550
551 06e2627e Scott Ullrich
/*
552
 * update_progress_bar($percent): updates the javascript driven progress bar.
553
 */
554
function update_progress_bar($percent) {
555
            if($percent > 100) $percent = 1;
556
            echo "\n<script type=\"text/javascript\" language=\"javascript\">";
557
            echo "\ndocument.progressbar.style.width='" . $percent . "%';";
558
            echo "\n</script>";
559
}
560
561 238f6962 Colin Smith
/*
562 b7c502bb Colin Smith
 * resync_all_package_configs_bootup() Force packages to setup their configuration and rc.d files at bootup.
563
 * This function also prints output to the terminal indicating progress.
564 238f6962 Colin Smith
 */
565 7c603497 Scott Ullrich
function resync_all_package_configs_bootup($show_message) {
566 068b8784 Colin Smith
	global $config;
567
	$i = 0;
568
	log_error("Resyncing configuration for all packages.");
569
	if(!$config['installedpackages']['package']) return;
570
	if($show_message == true) print "Syncing packages:";
571
	foreach($config['installedpackages']['package'] as $package) {
572
		if($show_message == true) print " " . $package['name'];
573
		sync_package($i, true, true);
574 e0dcab6a Colin Smith
		$i++;
575 a1f1ebb4 Colin Smith
	}
576 5c52313f Colin Smith
	if($show_message == true) print ".\n";
577 e0f91f5f Scott Ullrich
}
578
579 b7c502bb Colin Smith
/*
580 fbc24b62 Colin Smith
 * sweep_package_processes(): Periodically kill a package's unnecessary processes
581
 *			      that may still be running (a server that does not automatically timeout, for example)
582 b7c502bb Colin Smith
 */
583
function sweep_package_processes() {
584
    global $config;
585
    if(!$config['installedpackages']['package']) return;
586
    foreach($config['installedpackages']['package'] as $package) {
587
        $pkg_config = parse_xml_config_pkg("/usr/local/pkg/" . $package['configurationfile'], "packagegui");
588
        if($pkg_config['swept_processes'] <> "") {
589 fa35df12 Scott Ullrich
                mwexec("/usr/bin/killall " . $pkg_config['swept_processes']);
590 b7c502bb Colin Smith
                log_error("Killed " . $package['name'] . "'s unnecessary processes.");
591 fa35df12 Scott Ullrich
        }
592 b7c502bb Colin Smith
    }
593 fa35df12 Scott Ullrich
}
594 b7c502bb Colin Smith
595 b45ea709 Scott Ullrich
/*
596 76177335 Scott Ullrich
 * gather_altq_queue_stats():  gather alq queue stats and return an array that
597 b45ea709 Scott Ullrich
 *                             is queuename|qlength|measured_packets
598 fbc24b62 Colin Smith
 *                             NOTE: this command takes 5 seconds to run
599 b45ea709 Scott Ullrich
 */
600 6a153733 Scott Ullrich
function gather_altq_queue_stats($dont_return_root_queues) {
601 e90bc39f Scott Ullrich
    mwexec("/usr/bin/killall -9 pfctl");
602 9c0ad35c Scott Ullrich
    $stats = `/sbin/pfctl -vvsq & /bin/sleep 5;/usr/bin/killall pfctl 2>/dev/null`;
603 b45ea709 Scott Ullrich
    $stats_array = split("\n", $stats);
604
    $queue_stats = array();
605
    foreach ($stats_array as $stats_line) {
606
        if (preg_match_all("/queue\s+(\w+)\s+/",$stats_line,$match_array))
607
            $queue_name = $match_array[1][0];
608 fccb044c Scott Ullrich
        if (preg_match_all("/measured:\s+.*packets\/s\,\s(.*)\s+\]/",$stats_line,$match_array))
609
            $speed = $match_array[1][0];
610 e90bc39f Scott Ullrich
        if (preg_match_all("/borrows:\s+(.*)/",$stats_line,$match_array))
611
            $borrows = $match_array[1][0];
612 4bed294c Bill Marquette
        if (preg_match_all("/suspends:\s+(.*)/",$stats_line,$match_array))
613
            $suspends = $match_array[1][0];
614 04e6e7b7 Bill Marquette
        if (preg_match_all("/dropped pkts:\s+(.*)/",$stats_line,$match_array))
615
            $drops = $match_array[1][0];
616 b45ea709 Scott Ullrich
        if (preg_match_all("/measured:\s+(.*)packets/",$stats_line,$match_array)) {
617
            $measured = $match_array[1][0];
618 6a153733 Scott Ullrich
	    if($dont_return_root_queues == true)
619
		if(stristr($queue_name,"root_") == false)
620 122a9c39 Scott Ullrich
		    array_push($queue_stats, "{$queue_name}|{$speed}|{$measured}|{$borrows}|{$suspends}|{$drops}");
621 b45ea709 Scott Ullrich
        }
622
    }
623
    return $queue_stats;
624
}
625 fa35df12 Scott Ullrich
626 fbc24b62 Colin Smith
/*
627 0ad0f98c Scott Ullrich
 * reverse_strrchr($haystack, $needle):  Return everything in $haystack up to the *last* instance of $needle.
628 fbc24b62 Colin Smith
 *					 Useful for finding paths and stripping file extensions.
629
 */
630
function reverse_strrchr($haystack, $needle) {
631
	$pos = strrpos($haystack, $needle);
632
	if($post === false) {
633
		return $haystack;
634
	}
635
	return substr($haystack, 0, $post + 1);
636
}
637
638
/*
639 ade51705 Colin Smith
 * get_pkg_depends($pkg_name, $filetype = ".xml", $format = "files", return_nosync = 1):  Return a package's dependencies.
640 2bccc5fc Colin Smith
 *
641
 * $filetype = "all" || ".xml", ".tgz", etc.
642 0d34044c Colin Smith
 * $format = "files" (full filenames) || "names" (stripped / parsed depend names)
643 5f9be1c6 Colin Smith
 * $return_nosync = 1 (return depends that have nosync set) | 0 (ignore packages with nosync)
644 2bccc5fc Colin Smith
 *
645 fbc24b62 Colin Smith
 */
646 5f9be1c6 Colin Smith
function get_pkg_depends($pkg_name, $filetype = ".xml", $format = "files", $return_nosync = 1) {
647 2696882b Colin Smith
        global $config;
648 5a0c39fe Colin Smith
	if(!is_numeric($pkg_name)) {
649
                $pkg_name = get_pkg_id($pkg_name);
650
                if($pkg_id == -1) return -1; // This package doesn't really exist - exit the function.
651
        } else {
652 e54448e6 Colin Smith
                if(!isset($config['installedpackages']['package'][$pkg_id])) return; // No package belongs to the pkg_id passed to this function.
653 2f948e1c Colin Smith
        }
654 e54448e6 Colin Smith
        $package = $config['installedpackages']['package'][$pkg_id];
655
	print '$package done.';
656 5a0c39fe Colin Smith
        if(!file_exists("/usr/local/pkg/" . $package['configurationfile'])) { // If the package's config file doesn't exist, log an error and fetch it.
657
		log_error("Fetching missing configuration XML for " . $package['name']);
658
		mwexec("/usr/bin/fetch -o /usr/local/pkg/" . $package['configurationfile'] . " http://www.pfsense.com/packages/config/" . $package['configurationfile']);
659
	}
660
        $pkg_xml = parse_xml_config_pkg("/usr/local/pkg/" . $package['configurationfile'], "packagegui");
661 2f948e1c Colin Smith
        if($pkg_xml['additional_files_needed'] != "") {
662 0d34044c Colin Smith
                foreach($pkg_xml['additional_files_needed'] as $item) {
663 cdcfa555 Colin Smith
			if (($return_nosync == 0) && (isset($item['nosync']))) continue; // Do not return depends with nosync set if not required.
664 0d34044c Colin Smith
                        $depend_file = substr(strrchr($item['item']['0'], '/'),1); // Strip URLs down to filenames.
665
                        $depend_name = substr(substr($depend_file,0,strpos($depend_file,".")+1),0,-1); // Strip filename down to dependency name.
666
                        if (($filetype != "all") && (!preg_match("/${filetype}/i", $depend_file))) continue;
667
                        if ($item['prefix'] != "") {
668
                                $prefix = $item['prefix'];
669
                        } else {
670
                                $prefix = "/usr/local/pkg/";
671
                        }
672
                        if(!file_exists($prefix . $pkg_name)) {
673
                                log_error("Fetching missing dependency (" . $depend_name . ") for " . $pkg_name);
674
                                mwexec("/usr/local/bin/fetch -o " . $prefix . $depend_file . " " . $item['name']['0']);
675
                                if($item['chmod'] != "") mwexec("/bin/chmod " . $item['chmod'] . $prefix . $depend_file); // Handle chmods.
676
                        }
677
                        switch ($format) {
678
                        case "files":
679
                                $depends[] = $depend_file;
680
                                break;
681
                        case "names":
682
                                switch ($filetype) {
683
                                case "all":
684
                                        if(preg_match("/\.xml/i", $depend_file)) {
685
                                                $depend_xml = parse_xml_config_pkg("/usr/local/pkg/" . $depend_file, "packagegui");
686
                                                $depends[] = $depend_xml['name'];
687
                                                break;
688
                                        } else {
689
                                                $depends[] = $depend_name; // If this dependency isn't package XML, use the stripped filename.
690
                                                break;
691
                                        }
692
                                case ".xml":
693
                                        $depend_xml = parse_xml_config_pkg("/usr/local/pkg/" . $depend_file, "packagegui");
694
                                        $depends[] = $depend_xml['name'];
695
                                        break;
696
                                default:
697
                                        $depends[] = $depend_name; // If we aren't looking for XML, use the stripped filename (it's all we have).
698
                                        break;
699
                                }
700 2f948e1c Colin Smith
                        }
701
                }
702
        return $depends;
703
        }
704 fbc24b62 Colin Smith
}
705
706 44dc32e4 Scott Ullrich
/*
707
 * is_service_running($service_name): checks to see if a service is running.
708
 *                                    if the service is running returns 1.
709
 */
710
function is_service_running($service_name) {
711 7ebb7114 Colin Smith
    $status = `/bin/ps ax | grep {$service_name} | grep -v grep`;
712 44dc32e4 Scott Ullrich
    $status_split = split("\n", $service_name);
713
    $counter = 0;
714 257ff0ff Scott Ullrich
    foreach ($status_split as $ss) $counter++;
715 7ebb7114 Colin Smith
    if($counter > 0) return 1;
716 44dc32e4 Scott Ullrich
    return 0;
717
}
718 f8891a0f Scott Ullrich
719
/*
720 832f1b83 Scott Ullrich
 *  backup_config_section($section): returns as an xml file string of
721
 *                                   the configuration section
722 f8891a0f Scott Ullrich
 */
723
function backup_config_section($section) {
724
    global $config;
725 6d501961 Scott Ullrich
    $new_section = &$config[$section];
726 832f1b83 Scott Ullrich
    /* generate configuration XML */
727
    $xmlconfig = dump_xml_config($new_section, $section);
728 014beac3 Scott Ullrich
    return $xmlconfig;
729 f8891a0f Scott Ullrich
}
730
731
/*
732
 *  restore_config_section($section, new_contents): restore a configuration section,
733
 *                                                  and write the configuration out
734
 *                                                  to disk/cf.
735
 */
736
function restore_config_section($section, $new_contents) {
737
    global $config;
738
    conf_mount_rw();
739
    config_lock();
740 832f1b83 Scott Ullrich
    $fout = fopen("{$g['tmp_path']}/tmpxml","w");
741 014beac3 Scott Ullrich
    fwrite($fout, $new_contents);
742 832f1b83 Scott Ullrich
    fclose($fout);
743 014beac3 Scott Ullrich
    $section_xml = parse_xml_config_pkg($g['tmp_path'] . "/tmpxml", $section);
744 832f1b83 Scott Ullrich
    $config[$section] = &$section_xml;
745
    unlink($g['tmp_path'] . "/tmpxml");
746 f8891a0f Scott Ullrich
    write_config();
747
    conf_mount_ro();
748
    config_unlock();
749 46624b94 Scott Ullrich
    return;
750 f8891a0f Scott Ullrich
}
751
752 e99d11e3 Scott Ullrich
/*
753
 * http_post($server, po$port, $url, $vars): does an http post to a web server
754
 *                                           posting the vars array.
755
 * written by nf@bigpond.net.au
756
 */
757
function http_post($server, $port, $url, $vars) {
758
	$user_agent = "Mozilla/4.0 (compatible; MSIE 5.5; Windows 98)";
759
	$urlencoded = "";
760
	while (list($key,$value) = each($vars))
761
		$urlencoded.= urlencode($key) . "=" . urlencode($value) . "&";
762
	$urlencoded = substr($urlencoded,0,-1);
763
764
	$content_length = strlen($urlencoded);
765
766
	$headers = "POST $url HTTP/1.1
767
Accept: */*
768
Accept-Language: en-au
769
Content-Type: application/x-www-form-urlencoded
770
User-Agent: $user_agent
771
Host: $server
772
Connection: Keep-Alive
773
Cache-Control: no-cache
774
Content-Length: $content_length
775
776
";
777
778
	$fp = fsockopen($server, $port, $errno, $errstr);
779
	if (!$fp) {
780
		return false;
781
	}
782
783
	fputs($fp, $headers);
784
	fputs($fp, $urlencoded);
785
786
	$ret = "";
787
	while (!feof($fp))
788
		$ret.= fgets($fp, 1024);
789
790
	fclose($fp);
791
792
	return $ret;
793
794
}
795 f8891a0f Scott Ullrich
796 ee8f4a58 Scott Ullrich
/*
797
 *  php_check_syntax($code_tocheck, $errormessage): checks $code_to_check for errors
798
 */
799
if (!function_exists('php_check_syntax')){
800
   function php_check_syntax($code_to_check, &$errormessage){
801 c177d6ad Scott Ullrich
	return false;
802 7197df7d Scott Ullrich
        $fout = fopen("/tmp/codetocheck.php","w");
803
        $code = $_POST['content'];
804
        $code = str_replace("<?php", "", $code);
805
        $code = str_replace("?>", "", $code);
806
        fwrite($fout, "<?php\n\n");
807
        fwrite($fout, $code);
808
        fwrite($fout, "\n\n?>\n");
809
        fclose($fout);
810
        $command = "/usr/local/bin/php -l /tmp/codetocheck.php";
811
        $output = exec_command($command);
812
        if (stristr($output, "Errors parsing") == false) {
813
            echo "false\n";
814
            $errormessage = '';
815
            return(false);
816
        } else {
817
            $errormessage = $output;
818
            return(true);
819
        }
820 ee8f4a58 Scott Ullrich
    }
821
}
822
823
/*
824
 *  php_check_filename_syntax($filename, $errormessage): checks the file $filename for errors
825
 */
826 7197df7d Scott Ullrich
if (!function_exists('php_check_syntax')){
827
   function php_check_syntax($code_to_check, &$errormessage){
828 c177d6ad Scott Ullrich
	return false;
829 7197df7d Scott Ullrich
        $command = "/usr/local/bin/php -l " . $code_to_check;
830
        $output = exec_command($command);
831
        if (stristr($output, "Errors parsing") == false) {
832
            echo "false\n";
833
            $errormessage = '';
834
            return(false);
835
        } else {
836
            $errormessage = $output;
837
            return(true);
838
        }
839
    }
840 ee8f4a58 Scott Ullrich
}
841
842 5c52313f Colin Smith
/*
843 068b8784 Colin Smith
 * sync_package($pkg_name, $sync_depends = true, $show_message = false) Force a package to setup its configuration and rc.d files.
844 5c52313f Colin Smith
 */
845 f01a0942 Colin Smith
function sync_package($pkg_name, $sync_depends = true, $show_message = false) {
846 c177d6ad Scott Ullrich
	global $config;
847 879be199 Colin Smith
	if(!$config['installedpackages']['package']) return;
848 f01a0942 Colin Smith
	if(!is_numeric($pkg_name)) {
849 879be199 Colin Smith
		$pkg_id = get_pkg_id($pkg_name);
850
		if($pkg_id == -1) return -1; // This package doesn't really exist - exit the function.
851
	} else {
852 f01a0942 Colin Smith
		$pkg_id = $pkg_name;
853 e54448e6 Colin Smith
		if(!isset($config['installedpackages']['package'][$pkg_id])) {
854
			return;  // No package belongs to the pkg_id passed to this function.
855
		}
856 879be199 Colin Smith
	}
857 e54448e6 Colin Smith
	$package = $config['installedpackages']['package'][$pkg_id];
858 5c52313f Colin Smith
	if(!file_exists("/usr/local/pkg/" . $package['configurationfile'])) {
859 879be199 Colin Smith
		//if($show_message == true) print "(f)"; Don't mess with this until the package system has settled.
860
		log_error("Fetching missing configuration XML for " . $package['name']);
861
		mwexec("/usr/bin/fetch -o /usr/local/pkg/" . $package['configurationfile'] . " http://www.pfsense.com/packages/config/" . $package['configurationfile']);
862 5c52313f Colin Smith
        }
863
        $pkg_config = parse_xml_config_pkg("/usr/local/pkg/" . $package['configurationfile'], "packagegui");
864 e54448e6 Colin Smith
	if(isset($pkg_config['nosync'])) continue;
865
	//if($show_message == true) print "Syncing " . $pkg_name;
866 5c52313f Colin Smith
        if($pkg_config['custom_php_command_before_form'] <> "") {
867 879be199 Colin Smith
		eval($pkg_config['custom_php_command_before_form']);
868
	}
869 5c52313f Colin Smith
        if($pkg_config['custom_php_resync_config_command'] <> "") {
870 879be199 Colin Smith
	   	eval($pkg_config['custom_php_resync_config_command']);
871 5c52313f Colin Smith
        }
872 879be199 Colin Smith
        if($sync_depends == true) {
873 e54448e6 Colin Smith
		$depends = get_pkg_depends($pkg_name, ".xml", "files", 1); // Call dependency handler and do a little more error checking.
874 5c52313f Colin Smith
        	if(is_array($depends)) {
875
            		foreach($depends as $item) {
876 879be199 Colin Smith
				$item_config = parse_xml_config_pkg("/usr/local/pkg/" . $item, "packagegui");
877
				if(isset($item_config['nosync'])) continue;
878
				if($item_config['custom_php_command_before_form'] <> "")
879
					eval($item_config['custom_php_command_before_form']);
880 e54448e6 Colin Smith
					print "Evaled dependency.";
881 879be199 Colin Smith
				if($item_config['custom_php_resync_config_command'] <> "")
882
					eval($item_config['custom_php_resync_config_command']);
883 e54448e6 Colin Smith
					print "Evaled dependency.";
884 879be199 Colin Smith
				if($show_message == true) print " " . $item_config['name'];
885
			}
886 5c52313f Colin Smith
		}
887
	}
888 e54448e6 Colin Smith
	// if($show_message == true) print ".";
889 5c52313f Colin Smith
}
890 a1f1ebb4 Colin Smith
?>