Project

General

Profile

Download (91.3 KB) Statistics
| Branch: | Tag: | Revision:
1 14227c51 Scott Ullrich
<?php
2 09221bc3 Renato Botelho
/*
3 ac24dc24 Renato Botelho
 * pfsense-utils.inc
4
 *
5
 * part of pfSense (https://www.pfsense.org)
6 81299b5c Renato Botelho
 * Copyright (c) 2004-2016 Rubicon Communications, LLC (Netgate)
7 ac24dc24 Renato Botelho
 * All rights reserved.
8
 *
9 b12ea3fb Renato Botelho
 * Licensed under the Apache License, Version 2.0 (the "License");
10
 * you may not use this file except in compliance with the License.
11
 * You may obtain a copy of the License at
12 ac24dc24 Renato Botelho
 *
13 b12ea3fb Renato Botelho
 * http://www.apache.org/licenses/LICENSE-2.0
14 ac24dc24 Renato Botelho
 *
15 b12ea3fb Renato Botelho
 * Unless required by applicable law or agreed to in writing, software
16
 * distributed under the License is distributed on an "AS IS" BASIS,
17
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
 * See the License for the specific language governing permissions and
19
 * limitations under the License.
20 ac24dc24 Renato Botelho
 */
21 3076becf Scott Ullrich
22 0397013a Scott Ullrich
/****f* pfsense-utils/have_natpfruleint_access
23
 * NAME
24
 *   have_natpfruleint_access
25
 * INPUTS
26 c96e71d1 Renato Botelho
 *	none
27 0397013a Scott Ullrich
 * RESULT
28
 *   returns true if user has access to edit a specific firewall nat port forward interface
29
 ******/
30
function have_natpfruleint_access($if) {
31
	$security_url = "firewall_nat_edit.php?if=". strtolower($if);
32 23a193da Phil Davis
	if (isAllowedPage($security_url, $allowed)) {
33 0397013a Scott Ullrich
		return true;
34 23a193da Phil Davis
	}
35 0397013a Scott Ullrich
	return false;
36
}
37
38 b6742927 Scott Ullrich
/****f* pfsense-utils/have_ruleint_access
39
 * NAME
40
 *   have_ruleint_access
41
 * INPUTS
42 c96e71d1 Renato Botelho
 *	none
43 b6742927 Scott Ullrich
 * RESULT
44
 *   returns true if user has access to edit a specific firewall interface
45
 ******/
46
function have_ruleint_access($if) {
47
	$security_url = "firewall_rules.php?if=". strtolower($if);
48 23a193da Phil Davis
	if (isAllowedPage($security_url)) {
49 45ee90ed Matthew Grooms
		return true;
50 23a193da Phil Davis
	}
51 b6742927 Scott Ullrich
	return false;
52
}
53
54 10387862 Scott Ullrich
/****f* pfsense-utils/does_url_exist
55
 * NAME
56
 *   does_url_exist
57
 * INPUTS
58 c96e71d1 Renato Botelho
 *	none
59 10387862 Scott Ullrich
 * RESULT
60
 *   returns true if a url is available
61
 ******/
62
function does_url_exist($url) {
63 4de8f7ba Phil Davis
	$fd = fopen("$url", "r");
64 23a193da Phil Davis
	if ($fd) {
65 4cc6345e Scott Ullrich
		fclose($fd);
66 5fa78adc Renato Botelho
		return true;
67 10387862 Scott Ullrich
	} else {
68 5fa78adc Renato Botelho
		return false;
69 10387862 Scott Ullrich
	}
70
}
71
72 5928bd75 Scott Ullrich
/****f* pfsense-utils/is_private_ip
73
 * NAME
74
 *   is_private_ip
75
 * INPUTS
76 c96e71d1 Renato Botelho
 *	none
77 5928bd75 Scott Ullrich
 * RESULT
78
 *   returns true if an ip address is in a private range
79
 ******/
80
function is_private_ip($iptocheck) {
81 5fa78adc Renato Botelho
	$isprivate = false;
82 4de8f7ba Phil Davis
	$ip_private_list = array(
83 5fa78adc Renato Botelho
		"10.0.0.0/8",
84
		"100.64.0.0/10",
85
		"172.16.0.0/12",
86
		"192.168.0.0/16",
87
	);
88 23a193da Phil Davis
	foreach ($ip_private_list as $private) {
89 4de8f7ba Phil Davis
		if (ip_in_subnet($iptocheck, $private) == true) {
90 5fa78adc Renato Botelho
			$isprivate = true;
91 23a193da Phil Davis
		}
92 5fa78adc Renato Botelho
	}
93
	return $isprivate;
94 5928bd75 Scott Ullrich
}
95
96 8cb370b9 Scott Ullrich
/****f* pfsense-utils/get_tmp_file
97
 * NAME
98
 *   get_tmp_file
99
 * INPUTS
100 c96e71d1 Renato Botelho
 *	none
101 8cb370b9 Scott Ullrich
 * RESULT
102
 *   returns a temporary filename
103
 ******/
104 3076becf Scott Ullrich
function get_tmp_file() {
105 da17d77e Ermal Lu?i
	global $g;
106
	return "{$g['tmp_path']}/tmp-" . time();
107 3076becf Scott Ullrich
}
108
109
/****f* pfsense-utils/get_dns_servers
110
 * NAME
111 0057e62d Chris Buechler
 *   get_dns_servers - get system dns servers
112 3076becf Scott Ullrich
 * INPUTS
113 0057e62d Chris Buechler
 *   none
114 3076becf Scott Ullrich
 * RESULT
115 0057e62d Chris Buechler
 *   $dns_servers - an array of the dns servers
116 3076becf Scott Ullrich
 ******/
117
function get_dns_servers() {
118
	$dns_servers = array();
119 0057e62d Chris Buechler
	if (file_exists("/etc/resolv.conf")) {
120 4de8f7ba Phil Davis
		$dns_s = file("/etc/resolv.conf", FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
121 0057e62d Chris Buechler
	}
122
	if (is_array($dns_s)) {
123 4de8f7ba Phil Davis
		foreach ($dns_s as $dns) {
124
			$matches = "";
125
			if (preg_match("/nameserver (.*)/", $dns, $matches)) {
126
				$dns_servers[] = $matches[1];
127
			}
128 23a193da Phil Davis
		}
129 3076becf Scott Ullrich
	}
130 fa112436 Ermal
	return array_unique($dns_servers);
131 3076becf Scott Ullrich
}
132
133 c35d1294 doktornotor
/****f* pfsense-utils/pfSenseHeader
134
 * NAME
135
 *   pfSenseHeader
136
 * INPUTS
137
 *   none
138
 * RESULT
139
 *   Javascript header change or browser Location:
140
 ******/
141
function pfSenseHeader($text) {
142
	global $_SERVER;
143
	if (isAjax()) {
144
		if ($_SERVER['HTTPS'] == "on") {
145
			$protocol = "https";
146
		} else {
147
			$protocol = "http";
148
		}
149
150
		$port = ":{$_SERVER['SERVER_PORT']}";
151
		if ($_SERVER['SERVER_PORT'] == "80" && $protocol == "http") {
152
			$port = "";
153
		}
154
		if ($_SERVER['SERVER_PORT'] == "443" && $protocol == "https") {
155
			$port = "";
156
		}
157
		$complete_url = "{$protocol}://{$_SERVER['HTTP_HOST']}{$port}/{$text}";
158
		echo "\ndocument.location.href = '{$complete_url}';\n";
159
	} else {
160
		header("Location: $text");
161
	}
162
}
163
164 8bab524e Phil Davis
/****f* pfsense-utils/get_css_files
165
 * NAME
166
 *   get_css_files - get a list of the available CSS files (themes)
167
 * INPUTS
168
 *   none
169
 * RESULT
170
 *   $csslist - an array of the CSS files
171
 ******/
172
function get_css_files() {
173
	$csslist = array();
174
175
	// List pfSense files, then any BETA files followed by any user-contributed files
176
	$cssfiles = glob("/usr/local/www/css/*.css");
177
178
	if (is_array($cssfiles)) {
179
		arsort($cssfiles);
180
		$usrcss = $pfscss = $betacss = array();
181
182
		foreach ($cssfiles as $css) {
183 7f4b697f Steve Beaver
			if (strpos($css, "login") == 0) {	// Don't display any login page related CSS files
184
				if (strpos($css, "BETA") != 0) {
185
					array_push($betacss, $css);
186
				} else if (strpos($css, "pfSense") != 0) {
187
					array_push($pfscss, $css);
188
				} else {
189
					array_push($usrcss, $css);
190
				}
191 8bab524e Phil Davis
			}
192
		}
193
194
		$css = array_merge($pfscss, $betacss, $usrcss);
195
196
		foreach ($css as $file) {
197
			$file = basename($file);
198
			$csslist[$file] = pathinfo($file, PATHINFO_FILENAME);
199
		}
200
	}
201
	return $csslist;
202
}
203
204
/****f* pfsense-utils/gen_webguicss_field
205
 * NAME
206
 *   gen_webguicss_field
207
 * INPUTS
208
 *   Pointer to section object
209
 *   Initial value for the field
210
 * RESULT
211
 *   no return value, section object is updated
212
 ******/
213
function gen_webguicss_field(&$section, $value) {
214
215
	$csslist = get_css_files();
216
217
	if (!isset($csslist[$value])) {
218
		$value = "pfSense.css";
219
	}
220
221
	$section->addInput(new Form_Select(
222
		'webguicss',
223
		'Theme',
224
		$value,
225
		$csslist
226 314a088a Phil Davis
	))->setHelp('Choose an alternative css file (if installed) to change the appearance of the webConfigurator. css files are located in /usr/local/www/css/%s', '<span id="csstxt"></span>');
227 8bab524e Phil Davis
}
228
229
/****f* pfsense-utils/gen_webguifixedmenu_field
230
 * NAME
231
 *   gen_webguifixedmenu_field
232
 * INPUTS
233
 *   Pointer to section object
234
 *   Initial value for the field
235
 * RESULT
236
 *   no return value, section object is updated
237
 ******/
238
function gen_webguifixedmenu_field(&$section, $value) {
239
240
	$section->addInput(new Form_Select(
241
		'webguifixedmenu',
242
		'Top Navigation',
243
		$value,
244
		["" => gettext("Scrolls with page"), "fixed" => gettext("Fixed (Remains visible at top of page)")]
245
	))->setHelp("The fixed option is intended for large screens only.");
246
}
247
248
/****f* pfsense-utils/gen_webguihostnamemenu_field
249
 * NAME
250
 *   gen_webguihostnamemenu_field
251
 * INPUTS
252
 *   Pointer to section object
253
 *   Initial value for the field
254
 * RESULT
255
 *   no return value, section object is updated
256
 ******/
257
function gen_webguihostnamemenu_field(&$section, $value) {
258
259
	$section->addInput(new Form_Select(
260
		'webguihostnamemenu',
261
		'Hostname in Menu',
262
		$value,
263
		["" => gettext("Default (No hostname)"), "hostonly" => gettext("Hostname only"), "fqdn" => gettext("Fully Qualified Domain Name")]
264
	))->setHelp("Replaces the Help menu title in the Navbar with the system hostname or FQDN.");
265
}
266
267
/****f* pfsense-utils/gen_dashboardcolumns_field
268
 * NAME
269
 *   gen_dashboardcolumns_field
270
 * INPUTS
271
 *   Pointer to section object
272
 *   Initial value for the field
273
 * RESULT
274
 *   no return value, section object is updated
275
 ******/
276
function gen_dashboardcolumns_field(&$section, $value) {
277
278 e8813e51 Steve Beaver
	if (($value < 1) || ($value > 6)) {
279 8bab524e Phil Davis
		$value = 2;
280
	}
281
282
	$section->addInput(new Form_Input(
283
		'dashboardcolumns',
284
		'Dashboard Columns',
285
		'number',
286
		$value,
287 e8813e51 Steve Beaver
		[min => 1, max => 6]
288 8bab524e Phil Davis
	));
289
}
290
291 1d3510cf Phil Davis
/****f* pfsense-utils/gen_interfacessort_field
292
 * NAME
293
 *   gen_interfacessort_field
294
 * INPUTS
295
 *   Pointer to section object
296
 *   Initial value for the field
297
 * RESULT
298
 *   no return value, section object is updated
299
 ******/
300
function gen_interfacessort_field(&$section, $value) {
301
302
	$section->addInput(new Form_Checkbox(
303
		'interfacessort',
304
		'Interfaces Sort',
305
		'Sort Alphabetically',
306
		$value
307
	))->setHelp('If selected, lists of interfaces will be sorted by description, otherwise they are listed wan,lan,optn...');
308
}
309
310 8bab524e Phil Davis
/****f* pfsense-utils/gen_associatedpanels_fields
311
 * NAME
312
 *   gen_associatedpanels_fields
313
 * INPUTS
314
 *   Pointer to section object
315
 *   Initial value for each of the fields
316
 * RESULT
317
 *   no return value, section object is updated
318
 ******/
319
function gen_associatedpanels_fields(&$section, $value1, $value2, $value3, $value4) {
320
321
	$group = new Form_Group('Associated Panels Show/Hide');
322
323
	$group->add(new Form_Checkbox(
324
		'dashboardavailablewidgetspanel',
325
		null,
326
		'Available Widgets',
327
		$value1
328
		))->setHelp('Show the Available Widgets panel on the Dashboard.');
329
330
	$group->add(new Form_Checkbox(
331
		'systemlogsfilterpanel',
332
		null,
333
		'Log Filter',
334
		$value2
335
	))->setHelp('Show the Log Filter panel in System Logs.');
336
337
	$group->add(new Form_Checkbox(
338
		'systemlogsmanagelogpanel',
339
		null,
340
		'Manage Log',
341
		$value3
342
	))->setHelp('Show the Manage Log panel in System Logs.');
343
344
	$group->add(new Form_Checkbox(
345
		'statusmonitoringsettingspanel',
346
		null,
347
		'Monitoring Settings',
348
		$value4
349
	))->setHelp('Show the Settings panel in Status Monitoring.');
350
351
	$group->setHelp('These options allow certain panels to be automatically hidden on page load. A control is provided in the title bar to un-hide the panel.');
352
353
	$section->add($group);
354
}
355
356
/****f* pfsense-utils/gen_webguileftcolumnhyper_field
357
 * NAME
358
 *   gen_webguileftcolumnhyper_field
359
 * INPUTS
360
 *   Pointer to section object
361
 *   Initial value for the field
362
 * RESULT
363
 *   no return value, section object is updated
364
 ******/
365
function gen_webguileftcolumnhyper_field(&$section, $value) {
366
367
	$section->addInput(new Form_Checkbox(
368
		'webguileftcolumnhyper',
369
		'Left Column Labels',
370
		'Active',
371
		$value
372
	))->setHelp('If selected, clicking a label in the left column will select/toggle the first item of the group.');
373
}
374
375 d9058974 Phil Davis
/****f* pfsense-utils/gen_disablealiaspopupdetail_field
376
 * NAME
377
 *   gen_disablealiaspopupdetail_field
378
 * INPUTS
379
 *   Pointer to section object
380
 *   Initial value for the field
381
 * RESULT
382
 *   no return value, section object is updated
383
 ******/
384
function gen_disablealiaspopupdetail_field(&$section, $value) {
385
386
	$section->addInput(new Form_Checkbox(
387
		'disablealiaspopupdetail',
388
		'Alias Popups',
389
		'Disable details in alias popups',
390
		$value
391
	))->setHelp('If selected, the details in alias popups will not be shown, just the alias description (e.g. in Firewall Rules).');
392
}
393
394 8bab524e Phil Davis
/****f* pfsense-utils/gen_pagenamefirst_field
395
 * NAME
396
 *   gen_pagenamefirst_field
397
 * INPUTS
398
 *   Pointer to section object
399
 *   Initial value for the field
400
 * RESULT
401
 *   no return value, section object is updated
402
 ******/
403
function gen_pagenamefirst_field(&$section, $value) {
404
405
	$section->addInput(new Form_Checkbox(
406
		'pagenamefirst',
407
		'Browser tab text',
408
		'Display page name first in browser tab',
409
		$value
410
	))->setHelp('When this is unchecked, the browser tab shows the host name followed '.
411
		'by the current page. Check this box to display the current page followed by the '.
412
		'host name.');
413
}
414
415
/****f* pfsense-utils/gen_user_settings_fields
416
 * NAME
417
 *   gen_user_settings_fields
418
 * INPUTS
419
 *   Pointer to section object
420
 *   Array of initial values for the fields
421
 * RESULT
422
 *   no return value, section object is updated
423
 ******/
424
function gen_user_settings_fields(&$section, $pconfig) {
425
426
	gen_webguicss_field($section, $pconfig['webguicss']);
427
	gen_webguifixedmenu_field($section, $pconfig['webguifixedmenu']);
428
	gen_webguihostnamemenu_field($section, $pconfig['webguihostnamemenu']);
429
	gen_dashboardcolumns_field($section, $pconfig['dashboardcolumns']);
430 1d3510cf Phil Davis
	gen_interfacessort_field($section, $pconfig['interfacessort']);
431 8bab524e Phil Davis
	gen_associatedpanels_fields(
432
		$section,
433
		$pconfig['dashboardavailablewidgetspanel'],
434
		$pconfig['systemlogsfilterpanel'],
435
		$pconfig['systemlogsmanagelogpanel'],
436
		$pconfig['statusmonitoringsettingspanel']);
437
	gen_webguileftcolumnhyper_field($section, $pconfig['webguileftcolumnhyper']);
438 d9058974 Phil Davis
	gen_disablealiaspopupdetail_field($section, $pconfig['disablealiaspopupdetail']);
439 8bab524e Phil Davis
	gen_pagenamefirst_field($section, $pconfig['pagenamefirst']);
440
}
441
442 88081ea2 derelict-pf
/****f* pfsense-utils/gen_requirestatefilter_field
443
 * NAME
444
 *   gen_requirestatefilter_field
445
 * INPUTS
446
 *   Pointer to section object
447
 *   Initial value for the field
448
 * RESULT
449
 *   no return value, section object is updated
450
 ******/
451
function gen_requirestatefilter_field(&$section, $value) {
452
	$section->addInput(new Form_Checkbox(
453
		'requirestatefilter',
454
		'Require State Filter',
455
		'Do not display state table without a filter',
456
		$value
457
	))->setHelp('By default, the entire state table is displayed when entering '.
458
		'Diagnostics > States. This option requires a filter to be entered '.
459
		'before the states are displayed. Useful for systems with large state tables.');
460
}
461
462 f0dc8b68 Phil Davis
/****f* pfsense-utils/gen_created_updated_fields
463
 * NAME
464
 *   gen_created_updated_fields
465
 * INPUTS
466
 *   Pointer to form object
467
 *   Array of created time and username
468
 *   Array of updated time and username
469
 * RESULT
470
 *   no return value, section object is added to form if needed
471
 ******/
472
function gen_created_updated_fields(&$form, $created, $updated) {
473
	$has_created_time = (isset($created['time']) && isset($created['username']));
474
	$has_updated_time = (isset($updated['time']) && isset($updated['username']));
475
476
	if ($has_created_time || $has_updated_time) {
477
		$section = new Form_Section('Rule Information');
478
479
		if ($has_created_time) {
480
			$section->addInput(new Form_StaticText(
481
				'Created',
482
				sprintf(
483
					gettext('%1$s by %2$s'),
484
					date(gettext("n/j/y H:i:s"), $created['time']),
485
					$created['username'])
486
			));
487
		}
488
489
		if ($has_updated_time) {
490
			$section->addInput(new Form_StaticText(
491
				'Updated',
492
				sprintf(
493
					gettext('%1$s by %2$s'),
494
					date(gettext("n/j/y H:i:s"), $updated['time']),
495
					$updated['username'])
496
			));
497
		}
498
499
		$form->add($section);
500
	}
501
}
502
503 43517fcc Ermal LUÇI
function hardware_offloading_applyflags($iface) {
504
	global $config;
505
506
	$flags_on = 0;
507
	$flags_off = 0;
508
	$options = pfSense_get_interface_addresses($iface);
509
510 23a193da Phil Davis
	if (isset($config['system']['disablechecksumoffloading'])) {
511
		if (isset($options['encaps']['txcsum'])) {
512 43517fcc Ermal LUÇI
			$flags_off |= IFCAP_TXCSUM;
513 23a193da Phil Davis
		}
514
		if (isset($options['encaps']['rxcsum'])) {
515 43517fcc Ermal LUÇI
			$flags_off |= IFCAP_RXCSUM;
516 23a193da Phil Davis
		}
517 411d4e6e Luiz Otavio O Souza
		if (isset($options['encaps']['txcsum6'])) {
518
			$flags_off |= IFCAP_TXCSUM_IPV6;
519
		}
520
		if (isset($options['encaps']['rxcsum6'])) {
521
			$flags_off |= IFCAP_RXCSUM_IPV6;
522
		}
523 43517fcc Ermal LUÇI
	} else {
524 bc4d752b jim-p
		if (isset($options['caps']['txcsum'])) {
525 43517fcc Ermal LUÇI
			$flags_on |= IFCAP_TXCSUM;
526 23a193da Phil Davis
		}
527 bc4d752b jim-p
		if (isset($options['caps']['rxcsum'])) {
528 43517fcc Ermal LUÇI
			$flags_on |= IFCAP_RXCSUM;
529 23a193da Phil Davis
		}
530 411d4e6e Luiz Otavio O Souza
		if (isset($options['caps']['txcsum6'])) {
531
			$flags_on |= IFCAP_TXCSUM_IPV6;
532
		}
533
		if (isset($options['caps']['rxcsum6'])) {
534
			$flags_on |= IFCAP_RXCSUM_IPV6;
535
		}
536 43517fcc Ermal LUÇI
	}
537
538 23a193da Phil Davis
	if (isset($config['system']['disablesegmentationoffloading'])) {
539 43517fcc Ermal LUÇI
		$flags_off |= IFCAP_TSO;
540 23a193da Phil Davis
	} else if (isset($options['caps']['tso']) || isset($options['caps']['tso4']) || isset($options['caps']['tso6'])) {
541 bc4d752b jim-p
		$flags_on |= IFCAP_TSO;
542 23a193da Phil Davis
	}
543 43517fcc Ermal LUÇI
544 bc4d752b jim-p
	if (isset($config['system']['disablelargereceiveoffloading'])) {
545 43517fcc Ermal LUÇI
		$flags_off |= IFCAP_LRO;
546 bc4d752b jim-p
	} else if (isset($options['caps']['lro'])) {
547 43517fcc Ermal LUÇI
		$flags_on |= IFCAP_LRO;
548 23a193da Phil Davis
	}
549 43517fcc Ermal LUÇI
550
	pfSense_interface_capabilities($iface, -$flags_off);
551
	pfSense_interface_capabilities($iface, $flags_on);
552
}
553
554 3076becf Scott Ullrich
/****f* pfsense-utils/enable_hardware_offloading
555
 * NAME
556
 *   enable_hardware_offloading - Enable a NIC's supported hardware features.
557
 * INPUTS
558
 *   $interface	- string containing the physical interface to work on.
559
 * RESULT
560
 *   null
561
 * NOTES
562
 *   This function only supports the fxp driver's loadable microcode.
563
 ******/
564
function enable_hardware_offloading($interface) {
565
	global $g, $config;
566
567 a2934331 Scott Ullrich
	$int = get_real_interface($interface);
568 23a193da Phil Davis
	if (empty($int)) {
569 3d063391 Ermal
		return;
570 23a193da Phil Davis
	}
571 43517fcc Ermal LUÇI
572
	if (!isset($config['system']['do_not_use_nic_microcode'])) {
573
		/* translate wan, lan, opt -> real interface if needed */
574
		$int_family = preg_split("/[0-9]+/", $int);
575
		$supported_ints = array('fxp');
576
		if (in_array($int_family, $supported_ints)) {
577 23a193da Phil Davis
			if (does_interface_exist($int)) {
578 43517fcc Ermal LUÇI
				pfSense_interface_flags($int, IFF_LINK0);
579 23a193da Phil Davis
			}
580 43517fcc Ermal LUÇI
		}
581 a2934331 Scott Ullrich
	}
582 3076becf Scott Ullrich
583 43517fcc Ermal LUÇI
	/* This is mostly for vlans and ppp types */
584
	$realhwif = get_parent_interface($interface);
585 23a193da Phil Davis
	if ($realhwif[0] == $int) {
586 43517fcc Ermal LUÇI
		hardware_offloading_applyflags($int);
587 23a193da Phil Davis
	} else {
588 43517fcc Ermal LUÇI
		hardware_offloading_applyflags($realhwif[0]);
589
		hardware_offloading_applyflags($int);
590
	}
591 3076becf Scott Ullrich
}
592
593
/****f* pfsense-utils/is_alias_inuse
594
 * NAME
595
 *   checks to see if an alias is currently in use by a rule
596
 * INPUTS
597
 *
598
 * RESULT
599
 *   true or false
600
 * NOTES
601
 *
602
 ******/
603
function is_alias_inuse($alias) {
604
	global $g, $config;
605
606 23a193da Phil Davis
	if ($alias == "") {
607
		return false;
608
	}
609 3076becf Scott Ullrich
	/* loop through firewall rules looking for alias in use */
610 23a193da Phil Davis
	if (is_array($config['filter']['rule'])) {
611
		foreach ($config['filter']['rule'] as $rule) {
612
			if ($rule['source']['address']) {
613
				if ($rule['source']['address'] == $alias) {
614 0c8c496e Scott Ullrich
					return true;
615 23a193da Phil Davis
				}
616
			}
617
			if ($rule['destination']['address']) {
618
				if ($rule['destination']['address'] == $alias) {
619 0c8c496e Scott Ullrich
					return true;
620 23a193da Phil Davis
				}
621
			}
622 0c8c496e Scott Ullrich
		}
623 23a193da Phil Davis
	}
624 3076becf Scott Ullrich
	/* loop through nat rules looking for alias in use */
625 23a193da Phil Davis
	if (is_array($config['nat']['rule'])) {
626
		foreach ($config['nat']['rule'] as $rule) {
627
			if ($rule['target'] && $rule['target'] == $alias) {
628 3076becf Scott Ullrich
				return true;
629 23a193da Phil Davis
			}
630
			if ($rule['source']['address'] && $rule['source']['address'] == $alias) {
631 59ecde49 Renato Botelho
				return true;
632 23a193da Phil Davis
			}
633
			if ($rule['destination']['address'] && $rule['destination']['address'] == $alias) {
634 3076becf Scott Ullrich
				return true;
635 23a193da Phil Davis
			}
636 3076becf Scott Ullrich
		}
637 23a193da Phil Davis
	}
638 3076becf Scott Ullrich
	return false;
639
}
640
641 63724b02 Scott Dale
/****f* pfsense-utils/is_schedule_inuse
642
 * NAME
643
 *   checks to see if a schedule is currently in use by a rule
644
 * INPUTS
645
 *
646
 * RESULT
647
 *   true or false
648
 * NOTES
649
 *
650
 ******/
651
function is_schedule_inuse($schedule) {
652
	global $g, $config;
653
654 23a193da Phil Davis
	if ($schedule == "") {
655
		return false;
656
	}
657 63724b02 Scott Dale
	/* loop through firewall rules looking for schedule in use */
658 23a193da Phil Davis
	if (is_array($config['filter']['rule'])) {
659
		foreach ($config['filter']['rule'] as $rule) {
660
			if ($rule['sched'] == $schedule) {
661 591ceb32 Scott Dale
				return true;
662 23a193da Phil Davis
			}
663 63724b02 Scott Dale
		}
664 23a193da Phil Davis
	}
665 63724b02 Scott Dale
	return false;
666
}
667
668 3076becf Scott Ullrich
/****f* pfsense-utils/setup_microcode
669
 * NAME
670
 *   enumerates all interfaces and calls enable_hardware_offloading which
671
 *   enables a NIC's supported hardware features.
672
 * INPUTS
673
 *
674
 * RESULT
675
 *   null
676
 * NOTES
677
 *   This function only supports the fxp driver's loadable microcode.
678
 ******/
679
function setup_microcode() {
680
681 3a4ce87d Ermal Luçi
	/* if list */
682 80fe8369 Phil Davis
	$iflist = get_configured_interface_list(true);
683 23a193da Phil Davis
	foreach ($iflist as $if => $ifdescr) {
684 3076becf Scott Ullrich
		enable_hardware_offloading($if);
685 23a193da Phil Davis
	}
686 dced0dd0 Ermal LUÇI
	unset($iflist);
687 3076becf Scott Ullrich
}
688
689
/****f* pfsense-utils/get_carp_status
690
 * NAME
691
 *   get_carp_status - Return whether CARP is enabled or disabled.
692
 * RESULT
693
 *   boolean	- true if CARP is enabled, false if otherwise.
694
 ******/
695
function get_carp_status() {
696 5fa78adc Renato Botelho
	/* grab the current status of carp */
697 971de1f9 Renato Botelho
	$status = get_single_sysctl('net.inet.carp.allow');
698 5fa78adc Renato Botelho
	return (intval($status) > 0);
699 3076becf Scott Ullrich
}
700
701
/*
702
 * convert_ip_to_network_format($ip, $subnet): converts an ip address to network form
703 52947718 Ermal Lu?i
704 3076becf Scott Ullrich
 */
705
function convert_ip_to_network_format($ip, $subnet) {
706 2ce660ad smos
	$ipsplit = explode('.', $ip);
707 3076becf Scott Ullrich
	$string = $ipsplit[0] . "." . $ipsplit[1] . "." . $ipsplit[2] . ".0/" . $subnet;
708
	return $string;
709
}
710
711
/*
712 2a0aef55 Luiz Otavio O Souza
 * get_carp_interface_status($carpid): returns the status of a carp uniqid
713 3076becf Scott Ullrich
 */
714 2a0aef55 Luiz Otavio O Souza
function get_carp_interface_status($carpid) {
715
716
	$carpiface = get_configured_vip_interface($carpid);
717
	if ($carpiface == NULL)
718
		return "";
719
	$interface = get_real_interface($carpiface);
720
	if ($interface == NULL)
721
		return "";
722 5116a8aa Fredrik Rönnvall
	$vip = get_configured_vip($carpid);
723
	if ($vip == NULL || !isset($vip['vhid']))
724
		return "";
725 2a0aef55 Luiz Otavio O Souza
726 5116a8aa Fredrik Rönnvall
	$vhid = $vip['vhid'];
727 2a0aef55 Luiz Otavio O Souza
	$carp_query = '';
728 fd4e14b8 jim-p
	$_gb = exec("/sbin/ifconfig {$interface} | /usr/bin/grep \"carp:.* vhid {$vhid} \"", $carp_query);
729 2a0aef55 Luiz Otavio O Souza
	foreach ($carp_query as $int) {
730
		if (stripos($int, "MASTER"))
731
			return "MASTER";
732
		elseif (stripos($int, "BACKUP"))
733
			return "BACKUP";
734
		elseif (stripos($int, "INIT"))
735
			return "INIT";
736 3076becf Scott Ullrich
	}
737 e686a73f Luiz Otavio O Souza
738 0f98065b Luiz Otavio O Souza
	return "";
739 3076becf Scott Ullrich
}
740
741
/*
742
 * get_pfsync_interface_status($pfsyncinterface): returns the status of a pfsync
743
 */
744
function get_pfsync_interface_status($pfsyncinterface) {
745 23a193da Phil Davis
	if (!does_interface_exist($pfsyncinterface)) {
746 306f8556 Renato Botelho
		return;
747 23a193da Phil Davis
	}
748 306f8556 Renato Botelho
749
	return exec_command("/sbin/ifconfig {$pfsyncinterface} | /usr/bin/awk '/pfsync:/ {print \$5}'");
750 3076becf Scott Ullrich
}
751
752
/*
753
 * add_rule_to_anchor($anchor, $rule): adds the specified rule to an anchor
754
 */
755
function add_rule_to_anchor($anchor, $rule, $label) {
756 873c1701 Renato Botelho
	mwexec("echo " . escapeshellarg($rule) . " | /sbin/pfctl -a " . escapeshellarg($anchor) . ":" . escapeshellarg($label) . " -f -");
757 3076becf Scott Ullrich
}
758
759
/*
760
 * remove_text_from_file
761
 * remove $text from file $file
762
 */
763
function remove_text_from_file($file, $text) {
764 23a193da Phil Davis
	if (!file_exists($file) && !is_writable($file)) {
765 2addd5b2 Ermal
		return;
766 23a193da Phil Davis
	}
767 3076becf Scott Ullrich
	$filecontents = file_get_contents($file);
768 2addd5b2 Ermal
	$text = str_replace($text, "", $filecontents);
769 5fa78adc Renato Botelho
	@file_put_contents($file, $text);
770 3076becf Scott Ullrich
}
771
772
/*
773
 *   after_sync_bump_adv_skew(): create skew values by 1S
774
 */
775
function after_sync_bump_adv_skew() {
776
	global $config, $g;
777
	$processed_skew = 1;
778
	$a_vip = &$config['virtualip']['vip'];
779
	foreach ($a_vip as $vipent) {
780 23a193da Phil Davis
		if ($vipent['advskew'] <> "") {
781 3076becf Scott Ullrich
			$processed_skew = 1;
782
			$vipent['advskew'] = $vipent['advskew']+1;
783
		}
784
	}
785 23a193da Phil Davis
	if ($processed_skew == 1) {
786 7d1b238c Carlos Eduardo Ramos
		write_config(gettext("After synch increase advertising skew"));
787 23a193da Phil Davis
	}
788 3076becf Scott Ullrich
}
789
790
/*
791
 * get_filename_from_url($url): converts a url to its filename.
792
 */
793
function get_filename_from_url($url) {
794
	return basename($url);
795
}
796
797
/*
798
 *   get_dir: return an array of $dir
799
 */
800
function get_dir($dir) {
801
	$dir_array = array();
802
	$d = dir($dir);
803 9d3e8723 Phil Davis
	if (!is_object($d)) {
804 e4eca5a3 NewEraCracker
		return array();
805
	}
806 3076becf Scott Ullrich
	while (false !== ($entry = $d->read())) {
807
		array_push($dir_array, $entry);
808
	}
809
	$d->close();
810
	return $dir_array;
811
}
812
813
/****f* pfsense-utils/WakeOnLan
814
 * NAME
815
 *   WakeOnLan - Wake a machine up using the wake on lan format/protocol
816
 * RESULT
817
 *   true/false - true if the operation was successful
818
 ******/
819 086cf944 Phil Davis
function WakeOnLan($addr, $mac) {
820 3076becf Scott Ullrich
	$addr_byte = explode(':', $mac);
821
	$hw_addr = '';
822
823 4de8f7ba Phil Davis
	for ($a = 0; $a < 6; $a++) {
824 3076becf Scott Ullrich
		$hw_addr .= chr(hexdec($addr_byte[$a]));
825 23a193da Phil Davis
	}
826 3076becf Scott Ullrich
827
	$msg = chr(255).chr(255).chr(255).chr(255).chr(255).chr(255);
828
829 23a193da Phil Davis
	for ($a = 1; $a <= 16; $a++) {
830 3076becf Scott Ullrich
		$msg .= $hw_addr;
831 23a193da Phil Davis
	}
832 3076becf Scott Ullrich
833
	// send it to the broadcast address using UDP
834
	$s = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
835
	if ($s == false) {
836 7d1b238c Carlos Eduardo Ramos
		log_error(gettext("Error creating socket!"));
837 addc0439 Renato Botelho
		log_error(sprintf(gettext("Error code is '%1\$s' - %2\$s"), socket_last_error($s), socket_strerror(socket_last_error($s))));
838 3076becf Scott Ullrich
	} else {
839
		// setting a broadcast option to socket:
840 4de8f7ba Phil Davis
		$opt_ret = socket_set_option($s, 1, 6, TRUE);
841 23a193da Phil Davis
		if ($opt_ret < 0) {
842 7d1b238c Carlos Eduardo Ramos
			log_error(sprintf(gettext("setsockopt() failed, error: %s"), strerror($opt_ret)));
843 23a193da Phil Davis
		}
844 3076becf Scott Ullrich
		$e = socket_sendto($s, $msg, strlen($msg), 0, $addr, 2050);
845
		socket_close($s);
846 e8c516a0 Phil Davis
		log_error(sprintf(gettext('Magic Packet sent (%1$s) to (%2$s) MAC=%3$s'), $e, $addr, $mac));
847 3076becf Scott Ullrich
		return true;
848 0c8c496e Scott Ullrich
	}
849 3076becf Scott Ullrich
850
	return false;
851
}
852
853
/*
854
 * reverse_strrchr($haystack, $needle):  Return everything in $haystack up to the *last* instance of $needle.
855
 *					 Useful for finding paths and stripping file extensions.
856
 */
857
function reverse_strrchr($haystack, $needle) {
858 23a193da Phil Davis
	if (!is_string($haystack)) {
859 4824d857 Ermal Lu?i
		return;
860 23a193da Phil Davis
	}
861
	return strrpos($haystack, $needle) ? substr($haystack, 0, strrpos($haystack, $needle) +1) : false;
862 3076becf Scott Ullrich
}
863
864
/*
865
 *  backup_config_section($section): returns as an xml file string of
866
 *                                   the configuration section
867
 */
868 8dcca9b5 Darren Embry
function backup_config_section($section_name) {
869 3076becf Scott Ullrich
	global $config;
870 8dcca9b5 Darren Embry
	$new_section = &$config[$section_name];
871 3076becf Scott Ullrich
	/* generate configuration XML */
872 8dcca9b5 Darren Embry
	$xmlconfig = dump_xml_config($new_section, $section_name);
873 3076becf Scott Ullrich
	$xmlconfig = str_replace("<?xml version=\"1.0\"?>", "", $xmlconfig);
874
	return $xmlconfig;
875
}
876
877
/*
878 8dcca9b5 Darren Embry
 *  restore_config_section($section_name, new_contents): restore a configuration section,
879 3076becf Scott Ullrich
 *                                                  and write the configuration out
880
 *                                                  to disk/cf.
881
 */
882 8dcca9b5 Darren Embry
function restore_config_section($section_name, $new_contents) {
883 3076becf Scott Ullrich
	global $config, $g;
884 4de8f7ba Phil Davis
	$fout = fopen("{$g['tmp_path']}/tmpxml", "w");
885 3076becf Scott Ullrich
	fwrite($fout, $new_contents);
886
	fclose($fout);
887 8dcca9b5 Darren Embry
888
	$xml = parse_xml_config($g['tmp_path'] . "/tmpxml", null);
889
	if ($xml['pfsense']) {
890
		$xml = $xml['pfsense'];
891
	}
892
	else if ($xml['m0n0wall']) {
893
		$xml = $xml['m0n0wall'];
894
	}
895
	if ($xml[$section_name]) {
896
		$section_xml = $xml[$section_name];
897
	} else {
898
		$section_xml = -1;
899
	}
900
901 541989d5 Ermal
	@unlink($g['tmp_path'] . "/tmpxml");
902 8dcca9b5 Darren Embry
	if ($section_xml === -1) {
903
		return false;
904
	}
905
	$config[$section_name] = &$section_xml;
906 23a193da Phil Davis
	if (file_exists("{$g['tmp_path']}/config.cache")) {
907 a57d6170 Scott Ullrich
		unlink("{$g['tmp_path']}/config.cache");
908 23a193da Phil Davis
	}
909 8dcca9b5 Darren Embry
	write_config(sprintf(gettext("Restored %s of config file (maybe from CARP partner)"), $section_name));
910 0f806eca Erik Fonnesbeck
	disable_security_checks();
911 8dcca9b5 Darren Embry
	return true;
912 3076becf Scott Ullrich
}
913
914
/*
915 8dcca9b5 Darren Embry
 *  merge_config_section($section_name, new_contents):   restore a configuration section,
916 3076becf Scott Ullrich
 *                                                  and write the configuration out
917
 *                                                  to disk/cf.  But preserve the prior
918
 * 													structure if needed
919
 */
920 8dcca9b5 Darren Embry
function merge_config_section($section_name, $new_contents) {
921 3076becf Scott Ullrich
	global $config;
922
	$fname = get_tmp_filename();
923
	$fout = fopen($fname, "w");
924
	fwrite($fout, $new_contents);
925
	fclose($fout);
926 8dcca9b5 Darren Embry
	$section_xml = parse_xml_config($fname, $section_name);
927
	$config[$section_name] = $section_xml;
928 3076becf Scott Ullrich
	unlink($fname);
929 8dcca9b5 Darren Embry
	write_config(sprintf(gettext("Restored %s of config file (maybe from CARP partner)"), $section_name));
930 0f806eca Erik Fonnesbeck
	disable_security_checks();
931 3076becf Scott Ullrich
	return;
932
}
933
934
/*
935 4de8f7ba Phil Davis
 * rmdir_recursive($path, $follow_links=false)
936 3076becf Scott Ullrich
 * Recursively remove a directory tree (rm -rf path)
937
 * This is for directories _only_
938
 */
939 4de8f7ba Phil Davis
function rmdir_recursive($path, $follow_links=false) {
940 3076becf Scott Ullrich
	$to_do = glob($path);
941 23a193da Phil Davis
	if (!is_array($to_do)) {
942
		$to_do = array($to_do);
943
	}
944
	foreach ($to_do as $workingdir) { // Handle wildcards by foreaching.
945
		if (file_exists($workingdir)) {
946
			if (is_dir($workingdir)) {
947 3076becf Scott Ullrich
				$dir = opendir($workingdir);
948
				while ($entry = readdir($dir)) {
949 23a193da Phil Davis
					if (is_file("$workingdir/$entry") || ((!$follow_links) && is_link("$workingdir/$entry"))) {
950 3076becf Scott Ullrich
						unlink("$workingdir/$entry");
951 4de8f7ba Phil Davis
					} elseif (is_dir("$workingdir/$entry") && $entry != '.' && $entry != '..') {
952 3076becf Scott Ullrich
						rmdir_recursive("$workingdir/$entry");
953 23a193da Phil Davis
					}
954 6613a031 Scott Ullrich
				}
955 3076becf Scott Ullrich
				closedir($dir);
956
				rmdir($workingdir);
957
			} elseif (is_file($workingdir)) {
958
				unlink($workingdir);
959
			}
960 5fa78adc Renato Botelho
		}
961 3076becf Scott Ullrich
	}
962
	return;
963
}
964
965 e501de37 Ermal
/*
966
 * host_firmware_version(): Return the versions used in this install
967
 */
968 18be996d Ermal
function host_firmware_version($tocheck = "") {
969 5fa78adc Renato Botelho
	global $g, $config;
970 e501de37 Ermal
971 02406801 jim-p
	$os_version = trim(substr(php_uname("r"), 0, strpos(php_uname("r"), '-')));
972
973 5fa78adc Renato Botelho
	return array(
974 5779ade6 Renato Botelho
		"firmware" => array("version" => $g['product_version']),
975 02406801 jim-p
		"kernel"   => array("version" => $os_version),
976
		"base"     => array("version" => $os_version),
977 dc61252a Renato Botelho
		"platform" => $g['platform'],
978 5fa78adc Renato Botelho
		"config_version" => $config['version']
979
	);
980 e501de37 Ermal
}
981
982 3076becf Scott Ullrich
function get_disk_info() {
983
	$diskout = "";
984
	exec("/bin/df -h | /usr/bin/grep -w '/' | /usr/bin/awk '{ print $2, $3, $4, $5 }'", $diskout);
985
	return explode(' ', $diskout[0]);
986
}
987
988
/****f* pfsense-utils/strncpy
989
 * NAME
990
 *   strncpy - copy strings
991
 * INPUTS
992
 *   &$dst, $src, $length
993
 * RESULT
994
 *   none
995
 ******/
996
function strncpy(&$dst, $src, $length) {
997
	if (strlen($src) > $length) {
998
		$dst = substr($src, 0, $length);
999
	} else {
1000
		$dst = $src;
1001
	}
1002
}
1003
1004
/****f* pfsense-utils/reload_interfaces_sync
1005
 * NAME
1006
 *   reload_interfaces - reload all interfaces
1007
 * INPUTS
1008
 *   none
1009
 * RESULT
1010
 *   none
1011
 ******/
1012
function reload_interfaces_sync() {
1013 c0836064 Ermal Luçi
	global $config, $g;
1014 3076becf Scott Ullrich
1015 23a193da Phil Davis
	if ($g['debug']) {
1016 7d1b238c Carlos Eduardo Ramos
		log_error(gettext("reload_interfaces_sync() is starting."));
1017 23a193da Phil Davis
	}
1018 3076becf Scott Ullrich
1019
	/* parse config.xml again */
1020
	$config = parse_config(true);
1021
1022 a5d6f60b Ermal Lu?i
	/* enable routing */
1023
	system_routing_enable();
1024 23a193da Phil Davis
	if ($g['debug']) {
1025 7d1b238c Carlos Eduardo Ramos
		log_error(gettext("Enabling system routing"));
1026 23a193da Phil Davis
	}
1027 3076becf Scott Ullrich
1028 23a193da Phil Davis
	if ($g['debug']) {
1029 7d1b238c Carlos Eduardo Ramos
		log_error(gettext("Cleaning up Interfaces"));
1030 23a193da Phil Davis
	}
1031 3076becf Scott Ullrich
1032 67ee1ec5 Ermal Luçi
	/* set up interfaces */
1033
	interfaces_configure();
1034 3076becf Scott Ullrich
}
1035
1036
/****f* pfsense-utils/reload_all
1037
 * NAME
1038
 *   reload_all - triggers a reload of all settings
1039
 *   * INPUTS
1040
 *   none
1041
 * RESULT
1042
 *   none
1043
 ******/
1044
function reload_all() {
1045 0ae6daf8 Ermal
	send_event("service reload all");
1046 3076becf Scott Ullrich
}
1047
1048
/****f* pfsense-utils/reload_interfaces
1049
 * NAME
1050
 *   reload_interfaces - triggers a reload of all interfaces
1051
 * INPUTS
1052
 *   none
1053
 * RESULT
1054
 *   none
1055
 ******/
1056
function reload_interfaces() {
1057 5e3a84e2 Ermal
	send_event("interface all reload");
1058 3076becf Scott Ullrich
}
1059
1060
/****f* pfsense-utils/reload_all_sync
1061
 * NAME
1062
 *   reload_all - reload all settings
1063
 *   * INPUTS
1064
 *   none
1065
 * RESULT
1066
 *   none
1067
 ******/
1068
function reload_all_sync() {
1069
	global $config, $g;
1070
1071
	/* parse config.xml again */
1072
	$config = parse_config(true);
1073
1074
	/* set up our timezone */
1075
	system_timezone_configure();
1076
1077
	/* set up our hostname */
1078
	system_hostname_configure();
1079
1080
	/* make hosts file */
1081
	system_hosts_generate();
1082
1083
	/* generate resolv.conf */
1084
	system_resolvconf_generate();
1085
1086
	/* enable routing */
1087
	system_routing_enable();
1088
1089 a5d6f60b Ermal Lu?i
	/* set up interfaces */
1090
	interfaces_configure();
1091 3076becf Scott Ullrich
1092
	/* start dyndns service */
1093
	services_dyndns_configure();
1094
1095
	/* configure cron service */
1096
	configure_cron();
1097
1098
	/* start the NTP client */
1099
	system_ntp_configure();
1100
1101
	/* sync pw database */
1102 6b0c5879 Scott Ullrich
	unlink_if_exists("/etc/spwd.db.tmp");
1103 3076becf Scott Ullrich
	mwexec("/usr/sbin/pwd_mkdb -d /etc/ /etc/master.passwd");
1104
1105
	/* restart sshd */
1106 0ae6daf8 Ermal
	send_event("service restart sshd");
1107 3076becf Scott Ullrich
1108
	/* restart webConfigurator if needed */
1109 0ae6daf8 Ermal
	send_event("service restart webgui");
1110 3076becf Scott Ullrich
}
1111
1112 4de8f7ba Phil Davis
function setup_serial_port($when = "save", $path = "") {
1113 3076becf Scott Ullrich
	global $g, $config;
1114 02e4ee54 Renato Botelho
	$ttys_file = "{$path}/etc/ttys";
1115 196d0085 jim-p
	$boot_config_file = "{$path}/boot.config";
1116
	$loader_conf_file = "{$path}/boot/loader.conf";
1117 3076becf Scott Ullrich
	/* serial console - write out /boot.config */
1118 23a193da Phil Davis
	if (file_exists($boot_config_file)) {
1119 196d0085 jim-p
		$boot_config = file_get_contents($boot_config_file);
1120 23a193da Phil Davis
	} else {
1121 3076becf Scott Ullrich
		$boot_config = "";
1122 23a193da Phil Davis
	}
1123 3076becf Scott Ullrich
1124 4887afa1 Renato Botelho
	$serialspeed = (is_numeric($config['system']['serialspeed'])) ? $config['system']['serialspeed'] : "115200";
1125 60f164f3 Renato Botelho
	$serial_only = false;
1126 c7300778 Renato Botelho
	$vga_only = false;
1127 986e77a2 Renato Botelho
1128 dc61252a Renato Botelho
	$specific_platform = system_identify_specific_platform();
1129 8b3345dc Renato Botelho
	if ($specific_platform['name'] == 'XG-1540' ||
1130
	    $specific_platform['name'] == 'Turbot Dual-E') {
1131 c7300778 Renato Botelho
		$vga_only = true;
1132
	} elseif ($specific_platform['name'] == 'RCC-VE' ||
1133 dc61252a Renato Botelho
	    $specific_platform['name'] == 'RCC' ||
1134 089c18f3 Brett Keller
	    $specific_platform['name'] == 'RCC-DFF' ||
1135
	    $specific_platform['name'] == 'apu2') {
1136 60f164f3 Renato Botelho
		$serial_only = true;
1137
	}
1138 986e77a2 Renato Botelho
1139 60f164f3 Renato Botelho
	$boot_config_split = explode("\n", $boot_config);
1140 6172f3de Renato Botelho
	$data = array();
1141
	foreach ($boot_config_split as $bcs) {
1142
		/* Ignore -D and -h lines now */
1143
		if (!empty($bcs) && !stristr($bcs, "-D") &&
1144
		    !stristr($bcs, "-h")) {
1145
			$data[] = $bcs;
1146 0c8c496e Scott Ullrich
		}
1147 6172f3de Renato Botelho
	}
1148
	if ($serial_only === true) {
1149
		$data[] = "-S{$serialspeed} -h";
1150
	} elseif (is_serial_enabled()) {
1151
		$data[] = "-S{$serialspeed} -D";
1152 60f164f3 Renato Botelho
	}
1153 5f36c658 jim-p
1154 6172f3de Renato Botelho
	if (empty($data)) {
1155
		@unlink($boot_conf_file);
1156
	} else {
1157
		safe_write_file($boot_config_file, $data);
1158
	}
1159
1160
	unset($boot_config, $boot_config_file, $boot_config_split);
1161
1162 60f164f3 Renato Botelho
	/* serial console - write out /boot/loader.conf */
1163
	if ($when == "upgrade") {
1164
		system("echo \"Reading {$loader_conf_file}...\" >> /conf/upgrade_log.txt");
1165
	}
1166 25c088de Renato Botelho
1167 6172f3de Renato Botelho
	$loader_conf = file_get_contents($loader_conf_file);
1168
	$loader_conf_split = explode("\n", $loader_conf);
1169
1170
	$data = array();
1171
	// Loop through and only add lines that are not empty, and which
1172
	//  do not contain a console directive.
1173
	foreach ($loader_conf_split as $bcs) {
1174
		if (!empty($bcs) &&
1175
		    (stripos($bcs, "console") === false) &&
1176
		    (stripos($bcs, "boot_multicons") === false) &&
1177
		    (stripos($bcs, "boot_serial") === false) &&
1178
		    (stripos($bcs, "hw.usb.no_pf") === false) &&
1179
		    (stripos($bcs, "hint.uart.0.flags") === false) &&
1180
		    (stripos($bcs, "hint.uart.1.flags") === false)) {
1181
			$data[] = $bcs;
1182 60f164f3 Renato Botelho
		}
1183 6172f3de Renato Botelho
	}
1184 60f164f3 Renato Botelho
1185 6172f3de Renato Botelho
	if ($serial_only === true) {
1186
		$data[] = 'boot_serial="YES"';
1187
		$data[] = 'console="comconsole"';
1188 c7300778 Renato Botelho
	} elseif ($vga_only === true) {
1189
		$data[] = 'console="vidconsole"';
1190
	} elseif (is_serial_enabled()) {
1191 6172f3de Renato Botelho
		$data[] = 'boot_multicons="YES"';
1192
		$data[] = 'boot_serial="YES"';
1193
		$primaryconsole = isset($g['primaryconsole_force']) ?
1194
		    $g['primaryconsole_force'] :
1195
		    $config['system']['primaryconsole'];
1196
		switch ($primaryconsole) {
1197
			case "video":
1198
				$data[] = 'console="vidconsole,comconsole"';
1199
				break;
1200
			case "serial":
1201
			default:
1202
				$data[] = 'console="comconsole,vidconsole"';
1203
		}
1204 0c8c496e Scott Ullrich
	}
1205 6172f3de Renato Botelho
	$data[] = 'comconsole_speed="' . $serialspeed . '"';
1206
1207 c7300778 Renato Botelho
	if ($specific_platform['name'] == 'RCC-VE' ||
1208
	    $specific_platform['name'] == 'RCC' ||
1209
	    $specific_platform['name'] == 'RCC-DFF') {
1210 6172f3de Renato Botelho
		$data[] = 'comconsole_port="0x2F8"';
1211
		$data[] = 'hint.uart.0.flags="0x00"';
1212
		$data[] = 'hint.uart.1.flags="0x10"';
1213
	}
1214
	$data[] = 'hw.usb.no_pf="1"';
1215
1216
	safe_write_file($loader_conf_file, $data);
1217
1218
	unset($loader_conf, $loader_conf_split, $loader_config_file);
1219
1220 02e4ee54 Renato Botelho
	$ttys = file_get_contents($ttys_file);
1221 cfbfd941 smos
	$ttys_split = explode("\n", $ttys);
1222 6172f3de Renato Botelho
1223
	$data = array();
1224 c5f9fb72 Renato Botelho
1225 4f009171 Renato Botelho
	$on_off = (is_serial_enabled() ? 'onifconsole' : 'off');
1226 c5f9fb72 Renato Botelho
1227 edb4b657 Renato Botelho
	if (isset($config['system']['disableconsolemenu'])) {
1228
		$console_type = 'Pc';
1229 3f38f937 Luiz Otavio O Souza
		$serial_type = '3wire';
1230 edb4b657 Renato Botelho
	} else {
1231
		$console_type = 'al.Pc';
1232 3f38f937 Luiz Otavio O Souza
		$serial_type = 'al.3wire';
1233 edb4b657 Renato Botelho
	}
1234 237d29c4 Renato Botelho
1235 6172f3de Renato Botelho
	$console_line = "console\tnone\t\t\t\tunknown\toff\tsecure";
1236
	$ttyv0_line =
1237
	    "ttyv0\t\"/usr/libexec/getty {$console_type}\"\txterm\ton\tsecure";
1238
	$ttyu_line =
1239
	    "\"/usr/libexec/getty {$serial_type}\"\tvt100\t{$on_off}\tsecure";
1240 237d29c4 Renato Botelho
1241
	$found = array();
1242
1243 23a193da Phil Davis
	foreach ($ttys_split as $tty) {
1244 6172f3de Renato Botelho
		/* Ignore blank lines */
1245
		if (empty($tty)) {
1246
			continue;
1247
		}
1248
1249 23a193da Phil Davis
		if (stristr($tty, "ttyv0")) {
1250 237d29c4 Renato Botelho
			$found['ttyv0'] = 1;
1251 6172f3de Renato Botelho
			$data[] = $ttyv0_line;
1252 237d29c4 Renato Botelho
		} elseif (stristr($tty, "ttyu")) {
1253 4f009171 Renato Botelho
			$ttyn = substr($tty, 0, 5);
1254 237d29c4 Renato Botelho
			$found[$ttyn] = 1;
1255 6172f3de Renato Botelho
			$data[] = "{$ttyn}\t{$ttyu_line}";
1256 237d29c4 Renato Botelho
		} elseif (substr($tty, 0, 7) == 'console') {
1257
			$found['console'] = 1;
1258 6172f3de Renato Botelho
			$data[] = $tty;
1259 23a193da Phil Davis
		} else {
1260 6172f3de Renato Botelho
			$data[] = $tty;
1261 23a193da Phil Davis
		}
1262 3076becf Scott Ullrich
	}
1263 edb4b657 Renato Botelho
	unset($on_off, $console_type, $serial_type);
1264 237d29c4 Renato Botelho
1265
	/* Detect missing main lines on original file and try to rebuild it */
1266
	$items = array(
1267
		'console',
1268
		'ttyv0',
1269
		'ttyu0',
1270
		'ttyu1',
1271
		'ttyu2',
1272
		'ttyu3'
1273
	);
1274
1275
	foreach ($items as $item) {
1276
		if (isset($found[$item])) {
1277
			continue;
1278
		}
1279
1280
		if ($item == 'console') {
1281 6172f3de Renato Botelho
			$data[] = $console_line;
1282 237d29c4 Renato Botelho
		} elseif ($item == 'ttyv0') {
1283 6172f3de Renato Botelho
			$data[] = $ttyv0_line;
1284 237d29c4 Renato Botelho
		} else {
1285 6172f3de Renato Botelho
			$data[] = "{$item}\t{$ttyu_line}";
1286 237d29c4 Renato Botelho
		}
1287
	}
1288
1289 6172f3de Renato Botelho
	safe_write_file($ttys_file, $data);
1290
1291
	unset($ttys, $ttys_file, $ttys_split, $data);
1292
1293 23a193da Phil Davis
	if ($when != "upgrade") {
1294 02e4ee54 Renato Botelho
		reload_ttys();
1295 23a193da Phil Davis
	}
1296 a46e450c Ermal Lu?i
1297 3076becf Scott Ullrich
	return;
1298
}
1299
1300 38c7d42e Renato Botelho
function is_serial_enabled() {
1301
	global $g, $config;
1302
1303
	if (!isset($g['enableserial_force']) &&
1304 dc61252a Renato Botelho
	    !isset($config['system']['enableserial'])) {
1305 38c7d42e Renato Botelho
		return false;
1306 23a193da Phil Davis
	}
1307 38c7d42e Renato Botelho
1308
	return true;
1309
}
1310
1311 edb4b657 Renato Botelho
function reload_ttys() {
1312
	// Send a HUP signal to init will make it reload /etc/ttys
1313
	posix_kill(1, SIGHUP);
1314
}
1315
1316 3076becf Scott Ullrich
function print_value_list($list, $count = 10, $separator = ",") {
1317
	$list = implode($separator, array_slice($list, 0, $count));
1318 23a193da Phil Davis
	if (count($list) < $count) {
1319 3076becf Scott Ullrich
		$list .= ".";
1320
	} else {
1321
		$list .= "...";
1322
	}
1323
	return $list;
1324
}
1325
1326 bfe776f0 Ermal Luçi
/* DHCP enabled on any interfaces? */
1327 abdd01f5 Ermal
function is_dhcp_server_enabled() {
1328 db9fabf3 Ermal Luçi
	global $config;
1329 bfe776f0 Ermal Luçi
1330 23a193da Phil Davis
	if (!is_array($config['dhcpd'])) {
1331 bfe776f0 Ermal Luçi
		return false;
1332 23a193da Phil Davis
	}
1333 bfe776f0 Ermal Luçi
1334 abdd01f5 Ermal
	foreach ($config['dhcpd'] as $dhcpif => $dhcpifconf) {
1335 23a193da Phil Davis
		if (isset($dhcpifconf['enable']) && !empty($config['interfaces'][$dhcpif])) {
1336 abdd01f5 Ermal
			return true;
1337 23a193da Phil Davis
		}
1338 3076becf Scott Ullrich
	}
1339 bfe776f0 Ermal Luçi
1340 abdd01f5 Ermal
	return false;
1341 a6610d82 smos
}
1342
1343
/* DHCP enabled on any interfaces? */
1344 abdd01f5 Ermal
function is_dhcpv6_server_enabled() {
1345 a6610d82 smos
	global $config;
1346
1347 abdd01f5 Ermal
	if (is_array($config['interfaces'])) {
1348
		foreach ($config['interfaces'] as $ifcfg) {
1349 23a193da Phil Davis
			if (isset($ifcfg['enable']) && !empty($ifcfg['track6-interface'])) {
1350 abdd01f5 Ermal
				return true;
1351 23a193da Phil Davis
			}
1352 a6610d82 smos
		}
1353
	}
1354
1355 23a193da Phil Davis
	if (!is_array($config['dhcpdv6'])) {
1356 a6610d82 smos
		return false;
1357 23a193da Phil Davis
	}
1358 a6610d82 smos
1359 abdd01f5 Ermal
	foreach ($config['dhcpdv6'] as $dhcpv6if => $dhcpv6ifconf) {
1360 23a193da Phil Davis
		if (isset($dhcpv6ifconf['enable']) && !empty($config['interfaces'][$dhcpv6if])) {
1361 abdd01f5 Ermal
			return true;
1362 23a193da Phil Davis
		}
1363 65b1e7d5 Seth Mos
	}
1364
1365 abdd01f5 Ermal
	return false;
1366 3076becf Scott Ullrich
}
1367
1368 0ed8d746 bcyrill
/* radvd enabled on any interfaces? */
1369
function is_radvd_enabled() {
1370
	global $config;
1371
1372 23a193da Phil Davis
	if (!is_array($config['dhcpdv6'])) {
1373 0ed8d746 bcyrill
		$config['dhcpdv6'] = array();
1374 23a193da Phil Davis
	}
1375 0ed8d746 bcyrill
1376
	$dhcpdv6cfg = $config['dhcpdv6'];
1377
	$Iflist = get_configured_interface_list();
1378
1379
	/* handle manually configured DHCP6 server settings first */
1380
	foreach ($dhcpdv6cfg as $dhcpv6if => $dhcpv6ifconf) {
1381 23a193da Phil Davis
		if (!isset($config['interfaces'][$dhcpv6if]['enable'])) {
1382 0ed8d746 bcyrill
			continue;
1383 23a193da Phil Davis
		}
1384 0ed8d746 bcyrill
1385 23a193da Phil Davis
		if (!isset($dhcpv6ifconf['ramode'])) {
1386 0ed8d746 bcyrill
			$dhcpv6ifconf['ramode'] = $dhcpv6ifconf['mode'];
1387 23a193da Phil Davis
		}
1388 0ed8d746 bcyrill
1389 23a193da Phil Davis
		if ($dhcpv6ifconf['ramode'] == "disabled") {
1390 0ed8d746 bcyrill
			continue;
1391 23a193da Phil Davis
		}
1392 0ed8d746 bcyrill
1393
		$ifcfgipv6 = get_interface_ipv6($dhcpv6if);
1394 23a193da Phil Davis
		if (!is_ipaddrv6($ifcfgipv6)) {
1395 0ed8d746 bcyrill
			continue;
1396 23a193da Phil Davis
		}
1397 0ed8d746 bcyrill
1398
		return true;
1399
	}
1400
1401
	/* handle DHCP-PD prefixes and 6RD dynamic interfaces */
1402
	foreach ($Iflist as $if => $ifdescr) {
1403 23a193da Phil Davis
		if (!isset($config['interfaces'][$if]['track6-interface'])) {
1404 0ed8d746 bcyrill
			continue;
1405 23a193da Phil Davis
		}
1406
		if (!isset($config['interfaces'][$if]['enable'])) {
1407 0ed8d746 bcyrill
			continue;
1408 23a193da Phil Davis
		}
1409 0ed8d746 bcyrill
1410
		$ifcfgipv6 = get_interface_ipv6($if);
1411 23a193da Phil Davis
		if (!is_ipaddrv6($ifcfgipv6)) {
1412 0ed8d746 bcyrill
			continue;
1413 23a193da Phil Davis
		}
1414 0ed8d746 bcyrill
1415
		$ifcfgsnv6 = get_interface_subnetv6($if);
1416
		$subnetv6 = gen_subnetv6($ifcfgipv6, $ifcfgsnv6);
1417
1418 23a193da Phil Davis
		if (!is_ipaddrv6($subnetv6)) {
1419 0ed8d746 bcyrill
			continue;
1420 23a193da Phil Davis
		}
1421 0ed8d746 bcyrill
1422
		return true;
1423
	}
1424
1425
	return false;
1426
}
1427
1428 93c2c1e6 jim-p
/* Any PPPoE servers enabled? */
1429
function is_pppoe_server_enabled() {
1430
	global $config;
1431
1432
	$pppoeenable = false;
1433
1434 23a193da Phil Davis
	if (!is_array($config['pppoes']) || !is_array($config['pppoes']['pppoe'])) {
1435 93c2c1e6 jim-p
		return false;
1436 23a193da Phil Davis
	}
1437 93c2c1e6 jim-p
1438 23a193da Phil Davis
	foreach ($config['pppoes']['pppoe'] as $pppoes) {
1439
		if ($pppoes['mode'] == 'server') {
1440 93c2c1e6 jim-p
			$pppoeenable = true;
1441 23a193da Phil Davis
		}
1442
	}
1443 93c2c1e6 jim-p
1444
	return $pppoeenable;
1445
}
1446
1447 cf63f163 stilez
/* Optional arg forces hh:mm:ss without days */
1448
function convert_seconds_to_dhms($sec, $showhoursonly = false) {
1449 0bde6d10 stilez
	if (!is_numericint($sec)) {
1450
		return '-';
1451
	}
1452
	// FIXME: When we move to PHP 7 we can use "intdiv($sec % X, Y)" etc
1453 cf63f163 stilez
	list($d, $h, $m, $s) = array(	(int)($showhoursonly ? 0 : $sec/86400),
1454 70381d48 stilez
					(int)(($showhoursonly ? $sec : $sec % 86400)/3600),
1455 0bde6d10 stilez
					(int)(($sec % 3600)/60),
1456
					$sec % 60
1457
				);
1458
	return ($d > 0 ? $d . 'd ' : '') . sprintf('%02d:%02d:%02d', $h, $m, $s);
1459 9ebe7028 gnhb
}
1460 8eb2f33a Scott Ullrich
1461 63292199 gnhb
/* Compute the total uptime from the ppp uptime log file in the conf directory */
1462
1463 23a193da Phil Davis
function get_ppp_uptime($port) {
1464
	if (file_exists("/conf/{$port}.log")) {
1465 5fa78adc Renato Botelho
		$saved_time = file_get_contents("/conf/{$port}.log");
1466 4de8f7ba Phil Davis
		$uptime_data = explode("\n", $saved_time);
1467
		$sec = 0;
1468 23a193da Phil Davis
		foreach ($uptime_data as $upt) {
1469 63292199 gnhb
			$sec += substr($upt, 1 + strpos($upt, " "));
1470 5fa78adc Renato Botelho
		}
1471 0bde6d10 stilez
		return convert_seconds_to_dhms($sec);
1472 63292199 gnhb
	} else {
1473 7d1b238c Carlos Eduardo Ramos
		$total_time = gettext("No history data found!");
1474 63292199 gnhb
		return $total_time;
1475
	}
1476
}
1477 8eb2f33a Scott Ullrich
1478 6189988d Scott Dale
//returns interface information
1479
function get_interface_info($ifdescr) {
1480 cffe41cb Ermal
	global $config, $g;
1481 6189988d Scott Dale
1482
	$ifinfo = array();
1483 23a193da Phil Davis
	if (empty($config['interfaces'][$ifdescr])) {
1484 67ee1ec5 Ermal Luçi
		return;
1485 23a193da Phil Davis
	}
1486 ebdbdbc2 gnhb
	$ifinfo['hwif'] = $config['interfaces'][$ifdescr]['if'];
1487 cffe41cb Ermal
	$ifinfo['if'] = get_real_interface($ifdescr);
1488 6189988d Scott Dale
1489 cb074893 Ermal Lu?i
	$chkif = $ifinfo['if'];
1490
	$ifinfotmp = pfSense_get_interface_addresses($chkif);
1491
	$ifinfo['status'] = $ifinfotmp['status'];
1492 23a193da Phil Davis
	if (empty($ifinfo['status'])) {
1493 5fa78adc Renato Botelho
		$ifinfo['status'] = "down";
1494 23a193da Phil Davis
	}
1495 cb074893 Ermal Lu?i
	$ifinfo['macaddr'] = $ifinfotmp['macaddr'];
1496 2d2e466c Ermal LUÇI
	$ifinfo['mtu'] = $ifinfotmp['mtu'];
1497 cb074893 Ermal Lu?i
	$ifinfo['ipaddr'] = $ifinfotmp['ipaddr'];
1498
	$ifinfo['subnet'] = $ifinfotmp['subnet'];
1499 58418355 smos
	$ifinfo['linklocal'] = get_interface_linklocal($ifdescr);
1500 15cc0894 Seth Mos
	$ifinfo['ipaddrv6'] = get_interface_ipv6($ifdescr);
1501
	$ifinfo['subnetv6'] = get_interface_subnetv6($ifdescr);
1502 23a193da Phil Davis
	if (isset($ifinfotmp['link0'])) {
1503 cb074893 Ermal Lu?i
		$link0 = "down";
1504 23a193da Phil Davis
	}
1505 cffe41cb Ermal
	$ifinfotmp = pfSense_get_interface_stats($chkif);
1506 5fa78adc Renato Botelho
	// $ifinfo['inpkts'] = $ifinfotmp['inpkts'];
1507
	// $ifinfo['outpkts'] = $ifinfotmp['outpkts'];
1508
	$ifinfo['inerrs'] = $ifinfotmp['inerrs'];
1509
	$ifinfo['outerrs'] = $ifinfotmp['outerrs'];
1510
	$ifinfo['collisions'] = $ifinfotmp['collisions'];
1511 6189988d Scott Dale
1512 01385b0c Scott Ullrich
	/* Use pfctl for non wrapping 64 bit counters */
1513 b5a8483c Seth Mos
	/* Pass */
1514 cb074893 Ermal Lu?i
	exec("/sbin/pfctl -vvsI -i {$chkif}", $pfctlstats);
1515 971eaab5 Seth Mos
	$pf_in4_pass = preg_split("/ +/ ", $pfctlstats[3]);
1516
	$pf_out4_pass = preg_split("/ +/", $pfctlstats[5]);
1517 15cc0894 Seth Mos
	$pf_in6_pass = preg_split("/ +/ ", $pfctlstats[7]);
1518
	$pf_out6_pass = preg_split("/ +/", $pfctlstats[9]);
1519 971eaab5 Seth Mos
	$in4_pass = $pf_in4_pass[5];
1520
	$out4_pass = $pf_out4_pass[5];
1521
	$in4_pass_packets = $pf_in4_pass[3];
1522
	$out4_pass_packets = $pf_out4_pass[3];
1523 15cc0894 Seth Mos
	$in6_pass = $pf_in6_pass[5];
1524
	$out6_pass = $pf_out6_pass[5];
1525
	$in6_pass_packets = $pf_in6_pass[3];
1526
	$out6_pass_packets = $pf_out6_pass[3];
1527
	$ifinfo['inbytespass'] = $in4_pass + $in6_pass;
1528
	$ifinfo['outbytespass'] = $out4_pass + $out6_pass;
1529
	$ifinfo['inpktspass'] = $in4_pass_packets + $in6_pass_packets;
1530 4bdfa5dd Phil Davis
	$ifinfo['outpktspass'] = $out4_pass_packets + $out6_pass_packets;
1531 01385b0c Scott Ullrich
1532 971eaab5 Seth Mos
	/* Block */
1533
	$pf_in4_block = preg_split("/ +/", $pfctlstats[4]);
1534
	$pf_out4_block = preg_split("/ +/", $pfctlstats[6]);
1535 15cc0894 Seth Mos
	$pf_in6_block = preg_split("/ +/", $pfctlstats[8]);
1536
	$pf_out6_block = preg_split("/ +/", $pfctlstats[10]);
1537 971eaab5 Seth Mos
	$in4_block = $pf_in4_block[5];
1538
	$out4_block = $pf_out4_block[5];
1539
	$in4_block_packets = $pf_in4_block[3];
1540
	$out4_block_packets = $pf_out4_block[3];
1541 15cc0894 Seth Mos
	$in6_block = $pf_in6_block[5];
1542
	$out6_block = $pf_out6_block[5];
1543
	$in6_block_packets = $pf_in6_block[3];
1544
	$out6_block_packets = $pf_out6_block[3];
1545
	$ifinfo['inbytesblock'] = $in4_block + $in6_block;
1546
	$ifinfo['outbytesblock'] = $out4_block + $out6_block;
1547
	$ifinfo['inpktsblock'] = $in4_block_packets + $in6_block_packets;
1548
	$ifinfo['outpktsblock'] = $out4_block_packets + $out6_block_packets;
1549
1550
	$ifinfo['inbytes'] = $in4_pass + $in6_pass;
1551
	$ifinfo['outbytes'] = $out4_pass + $out6_pass;
1552
	$ifinfo['inpkts'] = $in4_pass_packets + $in6_pass_packets;
1553 4bdfa5dd Phil Davis
	$ifinfo['outpkts'] = $out4_pass_packets + $out6_pass_packets;
1554 5fa78adc Renato Botelho
1555 63161b3f Ermal Luçi
	$ifconfiginfo = "";
1556 59db783a gnhb
	$link_type = $config['interfaces'][$ifdescr]['ipaddr'];
1557
	switch ($link_type) {
1558 23a193da Phil Davis
		/* DHCP? -> see if dhclient is up */
1559
		case "dhcp":
1560
			/* see if dhclient is up */
1561
			if (find_dhclient_process($ifinfo['if']) != 0) {
1562
				$ifinfo['dhcplink'] = "up";
1563
			} else {
1564
				$ifinfo['dhcplink'] = "down";
1565 badbe349 gnhb
			}
1566 23a193da Phil Davis
1567 611ae852 Ermal
			break;
1568 23a193da Phil Davis
		/* PPPoE/PPTP/L2TP interface? -> get status from virtual interface */
1569
		case "pppoe":
1570
		case "pptp":
1571
		case "l2tp":
1572
			if ($ifinfo['status'] == "up" && !isset($link0)) {
1573
				/* get PPPoE link status for dial on demand */
1574
				$ifinfo["{$link_type}link"] = "up";
1575
			} else {
1576
				$ifinfo["{$link_type}link"] = "down";
1577 4adf752c smos
			}
1578 23a193da Phil Davis
1579
			break;
1580
		/* PPP interface? -> get uptime for this session and cumulative uptime from the persistent log file in conf */
1581
		case "ppp":
1582
			if ($ifinfo['status'] == "up") {
1583
				$ifinfo['ppplink'] = "up";
1584
			} else {
1585
				$ifinfo['ppplink'] = "down" ;
1586 4adf752c smos
			}
1587 23a193da Phil Davis
1588
			if (empty($ifinfo['status'])) {
1589
				$ifinfo['status'] = "down";
1590
			}
1591
1592
			if (is_array($config['ppps']['ppp']) && count($config['ppps']['ppp'])) {
1593
				foreach ($config['ppps']['ppp'] as $pppid => $ppp) {
1594
					if ($config['interfaces'][$ifdescr]['if'] == $ppp['if']) {
1595
						break;
1596
					}
1597
				}
1598
			}
1599
			$dev = $ppp['ports'];
1600
			if ($config['interfaces'][$ifdescr]['if'] != $ppp['if'] || empty($dev)) {
1601
				break;
1602
			}
1603
			if (!file_exists($dev)) {
1604
				$ifinfo['nodevice'] = 1;
1605
				$ifinfo['pppinfo'] = $dev . " " . gettext("device not present! Is the modem attached to the system?");
1606
			}
1607
1608
			$usbmodemoutput = array();
1609 84c82d3d doktornotor
			exec("/usr/sbin/usbconfig", $usbmodemoutput);
1610 23a193da Phil Davis
			$mondev = "{$g['tmp_path']}/3gstats.{$ifdescr}";
1611
			if (file_exists($mondev)) {
1612
				$cellstats = file($mondev);
1613
				/* skip header */
1614
				$a_cellstats = explode(",", $cellstats[1]);
1615
				if (preg_match("/huawei/i", implode("\n", $usbmodemoutput))) {
1616
					$ifinfo['cell_rssi'] = huawei_rssi_to_string($a_cellstats[1]);
1617
					$ifinfo['cell_mode'] = huawei_mode_to_string($a_cellstats[2], $a_cellstats[3]);
1618
					$ifinfo['cell_simstate'] = huawei_simstate_to_string($a_cellstats[10]);
1619
					$ifinfo['cell_service'] = huawei_service_to_string(trim($a_cellstats[11]));
1620
				}
1621
				if (preg_match("/zte/i", implode("\n", $usbmodemoutput))) {
1622
					$ifinfo['cell_rssi'] = zte_rssi_to_string($a_cellstats[1]);
1623
					$ifinfo['cell_mode'] = zte_mode_to_string($a_cellstats[2], $a_cellstats[3]);
1624
					$ifinfo['cell_simstate'] = zte_simstate_to_string($a_cellstats[10]);
1625
					$ifinfo['cell_service'] = zte_service_to_string(trim($a_cellstats[11]));
1626
				}
1627
				$ifinfo['cell_upstream'] = $a_cellstats[4];
1628
				$ifinfo['cell_downstream'] = trim($a_cellstats[5]);
1629
				$ifinfo['cell_sent'] = $a_cellstats[6];
1630
				$ifinfo['cell_received'] = trim($a_cellstats[7]);
1631
				$ifinfo['cell_bwupstream'] = $a_cellstats[8];
1632
				$ifinfo['cell_bwdownstream'] = trim($a_cellstats[9]);
1633
			}
1634
			// Calculate cumulative uptime for PPP link. Useful for connections that have per minute/hour contracts so you don't go over!
1635
			if (isset($ppp['uptime'])) {
1636
				$ifinfo['ppp_uptime_accumulated'] = "(".get_ppp_uptime($ifinfo['if']).")";
1637
			}
1638
			break;
1639
		default:
1640
			break;
1641 6189988d Scott Dale
	}
1642 5fa78adc Renato Botelho
1643 59db783a gnhb
	if (file_exists("{$g['varrun_path']}/{$link_type}_{$ifdescr}.pid")) {
1644
		$sec = trim(`/usr/local/sbin/ppp-uptime.sh {$ifinfo['if']}`);
1645 0bde6d10 stilez
		$ifinfo['ppp_uptime'] = convert_seconds_to_dhms($sec);
1646 59db783a gnhb
	}
1647 5fa78adc Renato Botelho
1648 6189988d Scott Dale
	if ($ifinfo['status'] == "up") {
1649
		/* try to determine media with ifconfig */
1650
		unset($ifconfiginfo);
1651 818a6b7d Seth Mos
		exec("/sbin/ifconfig " . $ifinfo['if'], $ifconfiginfo);
1652
		$wifconfiginfo = array();
1653 23a193da Phil Davis
		if (is_interface_wireless($ifdescr)) {
1654 818a6b7d Seth Mos
			exec("/sbin/ifconfig {$ifinfo['if']} list sta", $wifconfiginfo);
1655
			array_shift($wifconfiginfo);
1656
		}
1657 6189988d Scott Dale
		$matches = "";
1658
		foreach ($ifconfiginfo as $ici) {
1659
1660
			/* don't list media/speed for wireless cards, as it always
1661
			   displays 2 Mbps even though clients can connect at 11 Mbps */
1662
			if (preg_match("/media: .*? \((.*?)\)/", $ici, $matches)) {
1663
				$ifinfo['media'] = $matches[1];
1664
			} else if (preg_match("/media: Ethernet (.*)/", $ici, $matches)) {
1665
				$ifinfo['media'] = $matches[1];
1666
			} else if (preg_match("/media: IEEE 802.11 Wireless Ethernet (.*)/", $ici, $matches)) {
1667
				$ifinfo['media'] = $matches[1];
1668
			}
1669
1670
			if (preg_match("/status: (.*)$/", $ici, $matches)) {
1671 23a193da Phil Davis
				if ($matches[1] != "active") {
1672 6189988d Scott Dale
					$ifinfo['status'] = $matches[1];
1673 23a193da Phil Davis
				}
1674
				if ($ifinfo['status'] == gettext("running")) {
1675 7d1b238c Carlos Eduardo Ramos
					$ifinfo['status'] = gettext("up");
1676 23a193da Phil Davis
				}
1677 6189988d Scott Dale
			}
1678
			if (preg_match("/channel (\S*)/", $ici, $matches)) {
1679
				$ifinfo['channel'] = $matches[1];
1680
			}
1681
			if (preg_match("/ssid (\".*?\"|\S*)/", $ici, $matches)) {
1682 23a193da Phil Davis
				if ($matches[1][0] == '"') {
1683 6189988d Scott Dale
					$ifinfo['ssid'] = substr($matches[1], 1, -1);
1684 23a193da Phil Davis
				}
1685
				else {
1686 6189988d Scott Dale
					$ifinfo['ssid'] = $matches[1];
1687 23a193da Phil Davis
				}
1688 6189988d Scott Dale
			}
1689 0b29093b jim-p
			if (preg_match("/laggproto (.*)$/", $ici, $matches)) {
1690
				$ifinfo['laggproto'] = $matches[1];
1691
			}
1692
			if (preg_match("/laggport: (.*)$/", $ici, $matches)) {
1693
				$ifinfo['laggport'][] = $matches[1];
1694
			}
1695 6189988d Scott Dale
		}
1696 23a193da Phil Davis
		foreach ($wifconfiginfo as $ici) {
1697 818a6b7d Seth Mos
			$elements = preg_split("/[ ]+/i", $ici);
1698
			if ($elements[0] != "") {
1699
				$ifinfo['bssid'] = $elements[0];
1700
			}
1701
			if ($elements[3] != "") {
1702
				$ifinfo['rate'] = $elements[3];
1703
			}
1704
			if ($elements[4] != "") {
1705
				$ifinfo['rssi'] = $elements[4];
1706
			}
1707
		}
1708 67ee1ec5 Ermal Luçi
		/* lookup the gateway */
1709 2bbb79cb Seth Mos
		if (interface_has_gateway($ifdescr)) {
1710 ebdbdbc2 gnhb
			$ifinfo['gateway'] = get_interface_gateway($ifdescr);
1711 2bbb79cb Seth Mos
			$ifinfo['gatewayv6'] = get_interface_gateway_v6($ifdescr);
1712
		}
1713 6189988d Scott Dale
	}
1714
1715
	$bridge = "";
1716 7ec05d27 Ermal Luçi
	$bridge = link_interface_to_bridge($ifdescr);
1717 23a193da Phil Davis
	if ($bridge) {
1718 6189988d Scott Dale
		$bridge_text = `/sbin/ifconfig {$bridge}`;
1719 23a193da Phil Davis
		if (stristr($bridge_text, "blocking") <> false) {
1720 7d1b238c Carlos Eduardo Ramos
			$ifinfo['bridge'] = "<b><font color='red'>" . gettext("blocking") . "</font></b> - " . gettext("check for ethernet loops");
1721 6189988d Scott Dale
			$ifinfo['bridgeint'] = $bridge;
1722 23a193da Phil Davis
		} else if (stristr($bridge_text, "learning") <> false) {
1723 7d1b238c Carlos Eduardo Ramos
			$ifinfo['bridge'] = gettext("learning");
1724 6189988d Scott Dale
			$ifinfo['bridgeint'] = $bridge;
1725 23a193da Phil Davis
		} else if (stristr($bridge_text, "forwarding") <> false) {
1726 7d1b238c Carlos Eduardo Ramos
			$ifinfo['bridge'] = gettext("forwarding");
1727 6189988d Scott Dale
			$ifinfo['bridgeint'] = $bridge;
1728
		}
1729
	}
1730
1731
	return $ifinfo;
1732
}
1733
1734
//returns cpu speed of processor. Good for determining capabilities of machine
1735
function get_cpu_speed() {
1736 971de1f9 Renato Botelho
	return get_single_sysctl("hw.clockrate");
1737 6189988d Scott Dale
}
1738 fab7ff44 Bill Marquette
1739 df0cb10b Phil Davis
function get_uptime_sec() {
1740
	$boottime = "";
1741
	$matches = "";
1742 971de1f9 Renato Botelho
	$boottime = get_single_sysctl("kern.boottime");
1743
	preg_match("/sec = (\d+)/", $boottime, $matches);
1744 df0cb10b Phil Davis
	$boottime = $matches[1];
1745 23a193da Phil Davis
	if (intval($boottime) == 0) {
1746 df0cb10b Phil Davis
		return 0;
1747 23a193da Phil Davis
	}
1748 df0cb10b Phil Davis
1749
	$uptime = time() - $boottime;
1750
	return $uptime;
1751
}
1752
1753 a5f94f14 Scott Ullrich
function add_hostname_to_watch($hostname) {
1754 23a193da Phil Davis
	if (!is_dir("/var/db/dnscache")) {
1755 c941ea1c Seth Mos
		mkdir("/var/db/dnscache");
1756
	}
1757 2d0c5e3e Renato Botelho
	$result = array();
1758 23a193da Phil Davis
	if ((is_fqdn($hostname)) && (!is_ipaddr($hostname))) {
1759 581e772e Seth Mos
		$domrecords = array();
1760
		$domips = array();
1761 84c82d3d doktornotor
		exec("/usr/bin/host -t A " . escapeshellarg($hostname), $domrecords, $rethost);
1762 23a193da Phil Davis
		if ($rethost == 0) {
1763
			foreach ($domrecords as $domr) {
1764 581e772e Seth Mos
				$doml = explode(" ", $domr);
1765
				$domip = $doml[3];
1766
				/* fill array with domain ip addresses */
1767 23a193da Phil Davis
				if (is_ipaddr($domip)) {
1768 581e772e Seth Mos
					$domips[] = $domip;
1769
				}
1770
			}
1771
		}
1772
		sort($domips);
1773
		$contents = "";
1774 23a193da Phil Davis
		if (!empty($domips)) {
1775
			foreach ($domips as $ip) {
1776 162c059e Seth Mos
				$contents .= "$ip\n";
1777
			}
1778 581e772e Seth Mos
		}
1779
		file_put_contents("/var/db/dnscache/$hostname", $contents);
1780 aa57f965 Renato Botelho
		/* Remove empty elements */
1781
		$result = array_filter(explode("\n", $contents), 'strlen');
1782 a5f94f14 Scott Ullrich
	}
1783 2d0c5e3e Renato Botelho
	return $result;
1784 a5f94f14 Scott Ullrich
}
1785
1786 5ed54b93 Seth Mos
function is_fqdn($fqdn) {
1787
	$hostname = false;
1788 23a193da Phil Davis
	if (preg_match("/[-A-Z0-9\.]+\.[-A-Z0-9\.]+/i", $fqdn)) {
1789 5ed54b93 Seth Mos
		$hostname = true;
1790
	}
1791 23a193da Phil Davis
	if (preg_match("/\.\./", $fqdn)) {
1792 5ed54b93 Seth Mos
		$hostname = false;
1793
	}
1794 23a193da Phil Davis
	if (preg_match("/^\./i", $fqdn)) {
1795 5ed54b93 Seth Mos
		$hostname = false;
1796
	}
1797 23a193da Phil Davis
	if (preg_match("/\//i", $fqdn)) {
1798 c941ea1c Seth Mos
		$hostname = false;
1799
	}
1800 5ed54b93 Seth Mos
	return($hostname);
1801
}
1802
1803 639aaa95 Bill Marquette
function pfsense_default_state_size() {
1804 5fa78adc Renato Botelho
	/* get system memory amount */
1805
	$memory = get_memory();
1806 386758bb Phil Davis
	$physmem = $memory[0];
1807 5fa78adc Renato Botelho
	/* Be cautious and only allocate 10% of system memory to the state table */
1808 386758bb Phil Davis
	$max_states = (int) ($physmem/10)*1000;
1809 5fa78adc Renato Botelho
	return $max_states;
1810 639aaa95 Bill Marquette
}
1811
1812 84aea606 jim-p
function pfsense_default_tables_size() {
1813
	$current = `pfctl -sm | grep ^tables | awk '{print $4};'`;
1814
	return $current;
1815
}
1816
1817 fb586a16 jim-p
function pfsense_default_table_entries_size() {
1818
	$current = `pfctl -sm | grep table-entries | awk '{print $4};'`;
1819 742844a5 NOYB
	return (trim($current));
1820 fb586a16 jim-p
}
1821
1822 7723c7e0 Seth Mos
/* Compare the current hostname DNS to the DNS cache we made
1823
 * if it has changed we return the old records
1824 046b8ba6 Renato Botelho
 * if no change we return false */
1825 7723c7e0 Seth Mos
function compare_hostname_to_dnscache($hostname) {
1826 23a193da Phil Davis
	if (!is_dir("/var/db/dnscache")) {
1827 7723c7e0 Seth Mos
		mkdir("/var/db/dnscache");
1828
	}
1829
	$hostname = trim($hostname);
1830 23a193da Phil Davis
	if (is_readable("/var/db/dnscache/{$hostname}")) {
1831 7723c7e0 Seth Mos
		$oldcontents = file_get_contents("/var/db/dnscache/{$hostname}");
1832
	} else {
1833
		$oldcontents = "";
1834
	}
1835 23a193da Phil Davis
	if ((is_fqdn($hostname)) && (!is_ipaddr($hostname))) {
1836 7723c7e0 Seth Mos
		$domrecords = array();
1837
		$domips = array();
1838 84c82d3d doktornotor
		exec("/usr/bin/host -t A " . escapeshellarg($hostname), $domrecords, $rethost);
1839 23a193da Phil Davis
		if ($rethost == 0) {
1840
			foreach ($domrecords as $domr) {
1841 7723c7e0 Seth Mos
				$doml = explode(" ", $domr);
1842
				$domip = $doml[3];
1843
				/* fill array with domain ip addresses */
1844 23a193da Phil Davis
				if (is_ipaddr($domip)) {
1845 7723c7e0 Seth Mos
					$domips[] = $domip;
1846
				}
1847
			}
1848
		}
1849
		sort($domips);
1850
		$contents = "";
1851 23a193da Phil Davis
		if (!empty($domips)) {
1852
			foreach ($domips as $ip) {
1853 7723c7e0 Seth Mos
				$contents .= "$ip\n";
1854
			}
1855
		}
1856
	}
1857
1858 23a193da Phil Davis
	if (trim($oldcontents) != trim($contents)) {
1859
		if ($g['debug']) {
1860 addc0439 Renato Botelho
			log_error(sprintf(gettext('DNSCACHE: Found old IP %1$s and new IP %2$s'), $oldcontents, $contents));
1861 a5f91ef4 Seth Mos
		}
1862 7723c7e0 Seth Mos
		return ($oldcontents);
1863
	} else {
1864
		return false;
1865
	}
1866
}
1867
1868 09f18f59 jim-p
/*
1869 7530177c jim-p
 * load_crypto() - Load crypto modules if enabled in config.
1870 09f18f59 jim-p
 */
1871 7530177c jim-p
function load_crypto() {
1872 09f18f59 jim-p
	global $config, $g;
1873 2c98383f jim-p
	$crypto_modules = array('aesni', 'cryptodev');
1874 7530177c jim-p
1875 23a193da Phil Davis
	if (!in_array($config['system']['crypto_hardware'], $crypto_modules)) {
1876 7530177c jim-p
		return false;
1877 23a193da Phil Davis
	}
1878 7530177c jim-p
1879 3d74b803 jim-p
	if (!empty($config['system']['crypto_hardware']) && !is_module_loaded($config['system']['crypto_hardware'])) {
1880 e8c516a0 Phil Davis
		log_error(sprintf(gettext("Loading %s cryptographic accelerator module."), $config['system']['crypto_hardware']));
1881 7530177c jim-p
		mwexec("/sbin/kldload {$config['system']['crypto_hardware']}");
1882 09f18f59 jim-p
	}
1883
}
1884
1885 f60156f6 jim-p
/*
1886
 * load_thermal_hardware() - Load temperature monitor kernel module
1887
 */
1888
function load_thermal_hardware() {
1889
	global $config, $g;
1890
	$thermal_hardware_modules = array('coretemp', 'amdtemp');
1891
1892 23a193da Phil Davis
	if (!in_array($config['system']['thermal_hardware'], $thermal_hardware_modules)) {
1893 f60156f6 jim-p
		return false;
1894 23a193da Phil Davis
	}
1895 f60156f6 jim-p
1896 3d74b803 jim-p
	if (!empty($config['system']['thermal_hardware']) && !is_module_loaded($config['system']['thermal_hardware'])) {
1897 e8c516a0 Phil Davis
		log_error(sprintf(gettext("Loading %s thermal monitor module."), $config['system']['thermal_hardware']));
1898 f60156f6 jim-p
		mwexec("/sbin/kldload {$config['system']['thermal_hardware']}");
1899
	}
1900
}
1901
1902 cde4f5d3 Scott Ullrich
/****f* pfsense-utils/isvm
1903
 * NAME
1904
 *   isvm
1905
 * INPUTS
1906 c96e71d1 Renato Botelho
 *	none
1907 cde4f5d3 Scott Ullrich
 * RESULT
1908
 *   returns true if machine is running under a virtual environment
1909
 ******/
1910
function isvm() {
1911 7e36f71c Renato Botelho
	$virtualenvs = array("vmware", "parallels", "qemu", "bochs", "plex86", "VirtualBox");
1912 411f439a Renato Botelho
	$_gb = exec('/bin/kenv -q smbios.system.product 2>/dev/null', $output, $rc);
1913 7e36f71c Renato Botelho
1914 23a193da Phil Davis
	if ($rc != 0 || !isset($output[0])) {
1915 7e36f71c Renato Botelho
		return false;
1916 23a193da Phil Davis
	}
1917 7e36f71c Renato Botelho
1918 23a193da Phil Davis
	foreach ($virtualenvs as $virtualenv) {
1919
		if (stripos($output[0], $virtualenv) !== false) {
1920 58897b8c Warren Baker
			return true;
1921 23a193da Phil Davis
		}
1922
	}
1923 58897b8c Warren Baker
1924
	return false;
1925 cde4f5d3 Scott Ullrich
}
1926
1927 e0d0eb71 Scott Ullrich
function get_freebsd_version() {
1928 54597012 Renato Botelho
	$version = explode(".", php_uname("r"));
1929
	return $version[0];
1930 e0d0eb71 Scott Ullrich
}
1931
1932 a320af18 Chris Buechler
function download_file($url, $destination, $verify_ssl = true, $connect_timeout = 5, $timeout = 0) {
1933 ffd7802a Renato Botelho
	global $config, $g;
1934
1935
	$fp = fopen($destination, "wb");
1936
1937 23a193da Phil Davis
	if (!$fp) {
1938 ffd7802a Renato Botelho
		return false;
1939 23a193da Phil Davis
	}
1940 ffd7802a Renato Botelho
1941
	$ch = curl_init();
1942
	curl_setopt($ch, CURLOPT_URL, $url);
1943 57baf45f Luiz Otavio O Souza
	curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, $verify_ssl);
1944 ffd7802a Renato Botelho
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $verify_ssl);
1945
	curl_setopt($ch, CURLOPT_FILE, $fp);
1946
	curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $connect_timeout);
1947
	curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
1948
	curl_setopt($ch, CURLOPT_HEADER, false);
1949
	curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
1950 2bf67a6f Renato Botelho
	if (!isset($config['system']['do_not_send_uniqueid'])) {
1951 6cc74faa Renato Botelho
		curl_setopt($ch, CURLOPT_USERAGENT, $g['product_name'] . '/' . $g['product_version'] . ':' . system_get_uniqueid());
1952 6c07db48 Phil Davis
	} else {
1953 5779ade6 Renato Botelho
		curl_setopt($ch, CURLOPT_USERAGENT, $g['product_name'] . '/' . $g['product_version']);
1954 6c07db48 Phil Davis
	}
1955 ffd7802a Renato Botelho
1956
	if (!empty($config['system']['proxyurl'])) {
1957
		curl_setopt($ch, CURLOPT_PROXY, $config['system']['proxyurl']);
1958 23a193da Phil Davis
		if (!empty($config['system']['proxyport'])) {
1959 ffd7802a Renato Botelho
			curl_setopt($ch, CURLOPT_PROXYPORT, $config['system']['proxyport']);
1960 23a193da Phil Davis
		}
1961 ffd7802a Renato Botelho
		if (!empty($config['system']['proxyuser']) && !empty($config['system']['proxypass'])) {
1962
			@curl_setopt($ch, CURLOPT_PROXYAUTH, CURLAUTH_ANY | CURLAUTH_ANYSAFE);
1963
			curl_setopt($ch, CURLOPT_PROXYUSERPWD, "{$config['system']['proxyuser']}:{$config['system']['proxypass']}");
1964
		}
1965
	}
1966
1967
	@curl_exec($ch);
1968
	$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
1969
	fclose($fp);
1970
	curl_close($ch);
1971 fd4dbabc Chris Buechler
	if ($http_code == 200) {
1972
		return true;
1973
	} else {
1974 e8c516a0 Phil Davis
		log_error(sprintf(gettext('Download file failed with status code %1$s. URL: %2$s'), $http_code, $url));
1975 fd4dbabc Chris Buechler
		unlink_if_exists($destination);
1976
		return false;
1977
	}
1978 ffd7802a Renato Botelho
}
1979
1980 eb38f9a8 Chris Buechler
function download_file_with_progress_bar($url, $destination, $verify_ssl = true, $readbody = 'read_body', $connect_timeout = 5, $timeout = 0) {
1981 bfc15aca Ermal LUÇI
	global $config, $g;
1982
	global $ch, $fout, $file_size, $downloaded, $config, $first_progress_update;
1983 4de8f7ba Phil Davis
	$file_size = 1;
1984 5fa78adc Renato Botelho
	$downloaded = 1;
1985 e961bd67 phildd
	$first_progress_update = TRUE;
1986 5fa78adc Renato Botelho
	/* open destination file */
1987 eb38f9a8 Chris Buechler
	$fout = fopen($destination, "wb");
1988 5fa78adc Renato Botelho
1989 eb38f9a8 Chris Buechler
	if (!$fout) {
1990
		return false;
1991
	}
1992 5fa78adc Renato Botelho
	/*
1993
	 *      Originally by Author: Keyvan Minoukadeh
1994
	 *      Modified by Scott Ullrich to return Content-Length size
1995
	 */
1996
	$ch = curl_init();
1997 eb38f9a8 Chris Buechler
	curl_setopt($ch, CURLOPT_URL, $url);
1998 57baf45f Luiz Otavio O Souza
	curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, $verify_ssl);
1999 eb38f9a8 Chris Buechler
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $verify_ssl);
2000 5fa78adc Renato Botelho
	curl_setopt($ch, CURLOPT_HEADERFUNCTION, 'read_header');
2001
	curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
2002
	curl_setopt($ch, CURLOPT_WRITEFUNCTION, $readbody);
2003
	curl_setopt($ch, CURLOPT_NOPROGRESS, '1');
2004
	curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $connect_timeout);
2005
	curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
2006 2bf67a6f Renato Botelho
	if (!isset($config['system']['do_not_send_uniqueid'])) {
2007 6cc74faa Renato Botelho
		curl_setopt($ch, CURLOPT_USERAGENT, $g['product_name'] . '/' . $g['product_version'] . ':' . system_get_uniqueid());
2008 6c07db48 Phil Davis
	} else {
2009 5779ade6 Renato Botelho
		curl_setopt($ch, CURLOPT_USERAGENT, $g['product_name'] . '/' . $g['product_version']);
2010 6c07db48 Phil Davis
	}
2011 b31da21e Scott Ullrich
2012 42c07003 Ermal
	if (!empty($config['system']['proxyurl'])) {
2013
		curl_setopt($ch, CURLOPT_PROXY, $config['system']['proxyurl']);
2014 23a193da Phil Davis
		if (!empty($config['system']['proxyport'])) {
2015 42c07003 Ermal
			curl_setopt($ch, CURLOPT_PROXYPORT, $config['system']['proxyport']);
2016 23a193da Phil Davis
		}
2017 42c07003 Ermal
		if (!empty($config['system']['proxyuser']) && !empty($config['system']['proxypass'])) {
2018
			@curl_setopt($ch, CURLOPT_PROXYAUTH, CURLAUTH_ANY | CURLAUTH_ANYSAFE);
2019 2a57a4d1 Ermal
			curl_setopt($ch, CURLOPT_PROXYUSERPWD, "{$config['system']['proxyuser']}:{$config['system']['proxypass']}");
2020 42c07003 Ermal
		}
2021
	}
2022
2023 5fa78adc Renato Botelho
	@curl_exec($ch);
2024
	$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
2025 eb38f9a8 Chris Buechler
	fclose($fout);
2026 5fa78adc Renato Botelho
	curl_close($ch);
2027 eb38f9a8 Chris Buechler
	if ($http_code == 200) {
2028
		return true;
2029
	} else {
2030 e8c516a0 Phil Davis
		log_error(sprintf(gettext('Download file failed with status code %1$s. URL: %2$s'), $http_code, $url));
2031 eb38f9a8 Chris Buechler
		unlink_if_exists($destination);
2032
		return false;
2033
	}
2034 b31da21e Scott Ullrich
}
2035
2036
function read_header($ch, $string) {
2037 5fa78adc Renato Botelho
	global $file_size, $fout;
2038
	$length = strlen($string);
2039
	$regs = "";
2040
	preg_match("/(Content-Length:) (.*)/", $string, $regs);
2041 23a193da Phil Davis
	if ($regs[2] <> "") {
2042 5fa78adc Renato Botelho
		$file_size = intval($regs[2]);
2043
	}
2044
	ob_flush();
2045
	return $length;
2046 b31da21e Scott Ullrich
}
2047
2048
function read_body($ch, $string) {
2049 5fa78adc Renato Botelho
	global $fout, $file_size, $downloaded, $sendto, $static_status, $static_output, $lastseen, $first_progress_update;
2050
	global $pkg_interface;
2051
	$length = strlen($string);
2052
	$downloaded += intval($length);
2053 23a193da Phil Davis
	if ($file_size > 0) {
2054 5fa78adc Renato Botelho
		$downloadProgress = round(100 * (1 - $downloaded / $file_size), 0);
2055
		$downloadProgress = 100 - $downloadProgress;
2056 23a193da Phil Davis
	} else {
2057 5fa78adc Renato Botelho
		$downloadProgress = 0;
2058 23a193da Phil Davis
	}
2059
	if ($lastseen <> $downloadProgress and $downloadProgress < 101) {
2060
		if ($sendto == "status") {
2061
			if ($pkg_interface == "console") {
2062
				if (($downloadProgress % 10) == 0 || $downloadProgress < 10) {
2063 03b2cab6 Ermal
					$tostatus = $static_status . $downloadProgress . "%";
2064 2a315bee Phil Davis
					if ($downloadProgress == 100) {
2065 a3da8f50 Ermal
						$tostatus = $tostatus . "\r";
2066 2a315bee Phil Davis
					}
2067 03b2cab6 Ermal
					update_status($tostatus);
2068
				}
2069
			} else {
2070
				$tostatus = $static_status . $downloadProgress . "%";
2071 5fa78adc Renato Botelho
				update_status($tostatus);
2072 03b2cab6 Ermal
			}
2073 5fa78adc Renato Botelho
		} else {
2074 23a193da Phil Davis
			if ($pkg_interface == "console") {
2075
				if (($downloadProgress % 10) == 0 || $downloadProgress < 10) {
2076 03b2cab6 Ermal
					$tooutput = $static_output . $downloadProgress . "%";
2077 2a315bee Phil Davis
					if ($downloadProgress == 100) {
2078 a3da8f50 Ermal
						$tooutput = $tooutput . "\r";
2079 2a315bee Phil Davis
					}
2080 03b2cab6 Ermal
					update_output_window($tooutput);
2081
				}
2082
			} else {
2083
				$tooutput = $static_output . $downloadProgress . "%";
2084
				update_output_window($tooutput);
2085
			}
2086 5fa78adc Renato Botelho
		}
2087 23a193da Phil Davis
		if (($pkg_interface != "console") || (($downloadProgress % 10) == 0) || ($downloadProgress < 10)) {
2088
			update_progress_bar($downloadProgress, $first_progress_update);
2089
			$first_progress_update = FALSE;
2090
		}
2091 5fa78adc Renato Botelho
		$lastseen = $downloadProgress;
2092
	}
2093 23a193da Phil Davis
	if ($fout) {
2094 5fa78adc Renato Botelho
		fwrite($fout, $string);
2095 23a193da Phil Davis
	}
2096 5fa78adc Renato Botelho
	ob_flush();
2097
	return $length;
2098 b31da21e Scott Ullrich
}
2099
2100 84677257 Scott Ullrich
/*
2101
 *   update_output_window: update bottom textarea dynamically.
2102
 */
2103
function update_output_window($text) {
2104 5fa78adc Renato Botelho
	global $pkg_interface;
2105
	$log = preg_replace("/\n/", "\\n", $text);
2106 23a193da Phil Davis
	if ($pkg_interface != "console") {
2107 2d26ee5e Sjon Hortensius
?>
2108 8fd9052f Colin Fleming
<script type="text/javascript">
2109
//<![CDATA[
2110 2d26ee5e Sjon Hortensius
	document.getElementById("output").textContent="<?=htmlspecialchars($log)?>";
2111
	document.getElementById("output").scrollTop = document.getElementById("output").scrollHeight;
2112 8fd9052f Colin Fleming
//]]>
2113 2d26ee5e Sjon Hortensius
</script>
2114
<?php
2115 5fa78adc Renato Botelho
	}
2116
	/* ensure that contents are written out */
2117
	ob_flush();
2118 84677257 Scott Ullrich
}
2119
2120
/*
2121 82acb8b3 Phil Davis
 *   update_status: update top textarea dynamically.
2122 84677257 Scott Ullrich
 */
2123
function update_status($status) {
2124 5fa78adc Renato Botelho
	global $pkg_interface;
2125 1da49511 Renato Botelho
2126 23a193da Phil Davis
	if ($pkg_interface == "console") {
2127 489c102b BBcan177
		print ("{$status}");
2128 5fa78adc Renato Botelho
	}
2129 2d26ee5e Sjon Hortensius
2130 5fa78adc Renato Botelho
	/* ensure that contents are written out */
2131
	ob_flush();
2132 84677257 Scott Ullrich
}
2133
2134
/*
2135 e961bd67 phildd
 * update_progress_bar($percent, $first_time): updates the javascript driven progress bar.
2136 84677257 Scott Ullrich
 */
2137 e961bd67 phildd
function update_progress_bar($percent, $first_time) {
2138 5fa78adc Renato Botelho
	global $pkg_interface;
2139 23a193da Phil Davis
	if ($percent > 100) {
2140
		$percent = 1;
2141
	}
2142
	if ($pkg_interface <> "console") {
2143 8fd9052f Colin Fleming
		echo '<script type="text/javascript">';
2144
		echo "\n//<![CDATA[\n";
2145 66066eda Stephen Beaver
		echo 'document.getElementById("progressbar").style.width="'. $percent.'%"';
2146 8fd9052f Colin Fleming
		echo "\n//]]>\n";
2147
		echo '</script>';
2148 5fa78adc Renato Botelho
	} else {
2149 23a193da Phil Davis
		if (!($first_time)) {
2150 e961bd67 phildd
			echo "\x08\x08\x08\x08\x08";
2151 23a193da Phil Davis
		}
2152 e961bd67 phildd
		echo sprintf("%4d%%", $percent);
2153 5fa78adc Renato Botelho
	}
2154 84677257 Scott Ullrich
}
2155
2156 24807bfe Phil Davis
function update_alias_name($new_alias_name, $orig_alias_name) {
2157
	if (!$orig_alias_name) {
2158
		return;
2159
	}
2160
2161
	// Firewall rules
2162
	update_alias_names_upon_change(array('filter', 'rule'), array('source', 'address'), $new_alias_name, $orig_alias_name);
2163
	update_alias_names_upon_change(array('filter', 'rule'), array('destination', 'address'), $new_alias_name, $orig_alias_name);
2164
	update_alias_names_upon_change(array('filter', 'rule'), array('source', 'port'), $new_alias_name, $orig_alias_name);
2165
	update_alias_names_upon_change(array('filter', 'rule'), array('destination', 'port'), $new_alias_name, $orig_alias_name);
2166
	// NAT Rules
2167
	update_alias_names_upon_change(array('nat', 'rule'), array('source', 'address'), $new_alias_name, $orig_alias_name);
2168
	update_alias_names_upon_change(array('nat', 'rule'), array('source', 'port'), $new_alias_name, $orig_alias_name);
2169
	update_alias_names_upon_change(array('nat', 'rule'), array('destination', 'address'), $new_alias_name, $orig_alias_name);
2170
	update_alias_names_upon_change(array('nat', 'rule'), array('destination', 'port'), $new_alias_name, $orig_alias_name);
2171
	update_alias_names_upon_change(array('nat', 'rule'), array('target'), $new_alias_name, $orig_alias_name);
2172
	update_alias_names_upon_change(array('nat', 'rule'), array('local-port'), $new_alias_name, $orig_alias_name);
2173
	// NAT 1:1 Rules
2174
	//update_alias_names_upon_change(array('nat', 'onetoone'), array('external'), $new_alias_name, $orig_alias_name);
2175
	//update_alias_names_upon_change(array('nat', 'onetoone'), array('source', 'address'), $new_alias_name, $orig_alias_name);
2176
	update_alias_names_upon_change(array('nat', 'onetoone'), array('destination', 'address'), $new_alias_name, $orig_alias_name);
2177
	// NAT Outbound Rules
2178
	update_alias_names_upon_change(array('nat', 'outbound', 'rule'), array('source', 'network'), $new_alias_name, $orig_alias_name);
2179
	update_alias_names_upon_change(array('nat', 'outbound', 'rule'), array('sourceport'), $new_alias_name, $orig_alias_name);
2180
	update_alias_names_upon_change(array('nat', 'outbound', 'rule'), array('destination', 'address'), $new_alias_name, $orig_alias_name);
2181
	update_alias_names_upon_change(array('nat', 'outbound', 'rule'), array('dstport'), $new_alias_name, $orig_alias_name);
2182
	update_alias_names_upon_change(array('nat', 'outbound', 'rule'), array('target'), $new_alias_name, $orig_alias_name);
2183
	// Alias in an alias
2184
	update_alias_names_upon_change(array('aliases', 'alias'), array('address'), $new_alias_name, $orig_alias_name);
2185
}
2186
2187 f1ac1733 Erik Fonnesbeck
function update_alias_names_upon_change($section, $field, $new_alias_name, $origname) {
2188 978fd2e8 Scott Ullrich
	global $g, $config, $pconfig, $debug;
2189 23a193da Phil Davis
	if (!$origname) {
2190 b6db8ea3 sullrich
		return;
2191 23a193da Phil Davis
	}
2192 b6db8ea3 sullrich
2193 f1ac1733 Erik Fonnesbeck
	$sectionref = &$config;
2194 23a193da Phil Davis
	foreach ($section as $sectionname) {
2195
		if (is_array($sectionref) && isset($sectionref[$sectionname])) {
2196 f1ac1733 Erik Fonnesbeck
			$sectionref = &$sectionref[$sectionname];
2197 23a193da Phil Davis
		} else {
2198 f1ac1733 Erik Fonnesbeck
			return;
2199 23a193da Phil Davis
		}
2200 f1ac1733 Erik Fonnesbeck
	}
2201
2202 23a193da Phil Davis
	if ($debug) {
2203
		$fd = fopen("{$g['tmp_path']}/print_r", "a");
2204
		fwrite($fd, print_r($pconfig, true));
2205
	}
2206 b6db8ea3 sullrich
2207 23a193da Phil Davis
	if (is_array($sectionref)) {
2208
		foreach ($sectionref as $itemkey => $item) {
2209
			if ($debug) {
2210
				fwrite($fd, "$itemkey\n");
2211
			}
2212 f1ac1733 Erik Fonnesbeck
2213
			$fieldfound = true;
2214
			$fieldref = &$sectionref[$itemkey];
2215 23a193da Phil Davis
			foreach ($field as $fieldname) {
2216
				if (is_array($fieldref) && isset($fieldref[$fieldname])) {
2217 f1ac1733 Erik Fonnesbeck
					$fieldref = &$fieldref[$fieldname];
2218 23a193da Phil Davis
				} else {
2219 f1ac1733 Erik Fonnesbeck
					$fieldfound = false;
2220
					break;
2221
				}
2222 b6db8ea3 sullrich
			}
2223 23a193da Phil Davis
			if ($fieldfound && $fieldref == $origname) {
2224
				if ($debug) {
2225
					fwrite($fd, "Setting old alias value $origname to $new_alias_name\n");
2226
				}
2227 f1ac1733 Erik Fonnesbeck
				$fieldref = $new_alias_name;
2228 b6db8ea3 sullrich
			}
2229
		}
2230
	}
2231
2232 23a193da Phil Davis
	if ($debug) {
2233
		fclose($fd);
2234
	}
2235 b6db8ea3 sullrich
2236
}
2237 f6ba4bd1 Scott Ullrich
2238 f6622167 NOYB
function parse_aliases_file($filename, $type = "url", $max_items = -1, $kflc = false) {
2239 6d1907a3 Renato Botelho
	/*
2240
	 * $filename = file to process for example blocklist like DROP:  http://www.spamhaus.org/drop/drop.txt
2241
	 * $type = if set to 'url' then subnets and ips will be returned,
2242
	 *         if set to 'url_ports' port-ranges and ports will be returned
2243
	 * $max_items = sets the maximum amount of valid items to load, -1 the default defines there is no limit.
2244
	 *
2245
	 * RETURNS an array of ip subnets and ip's or ports and port-ranges, returns NULL upon a error conditions (file not found)
2246
	 */
2247
2248 14645549 Chris Buechler
	if (!file_exists($filename)) {
2249
		log_error(sprintf(gettext("Could not process non-existent file from alias: %s"), $filename));
2250
		return null;
2251
	}
2252
2253 6f838722 Chris Buechler
	if (filesize($filename) == 0) {
2254
		log_error(sprintf(gettext("Could not process empty file from alias: %s"), $filename));
2255
		return null;
2256
	}
2257 6d1907a3 Renato Botelho
	$fd = @fopen($filename, 'r');
2258
	if (!$fd) {
2259 e8c516a0 Phil Davis
		log_error(sprintf(gettext("Could not process aliases from alias: %s"), $filename));
2260 6d1907a3 Renato Botelho
		return null;
2261
	}
2262
	$items = array();
2263 f6622167 NOYB
	$comments = array();
2264 6d1907a3 Renato Botelho
	/* NOTE: fgetss() is not a typo RTFM before being smart */
2265
	while (($fc = fgetss($fd)) !== FALSE) {
2266
		$tmp = trim($fc, " \t\n\r");
2267 23a193da Phil Davis
		if (empty($tmp)) {
2268 6d1907a3 Renato Botelho
			continue;
2269 23a193da Phil Davis
		}
2270 f6622167 NOYB
		if (($kflc) && (strpos($tmp, '#') === 0)) {	// Keep Full Line Comments (lines beginning with #).
2271
			$comments[] = $tmp;
2272
		} else {
2273
			$tmp_str = strstr($tmp, '#', true);
2274
			if (!empty($tmp_str)) {
2275
				$tmp = $tmp_str;
2276
			}
2277
			$tmp_str = strstr($tmp, ' ', true);
2278
			if (!empty($tmp_str)) {
2279
				$tmp = $tmp_str;
2280
			}
2281 ebe833f6 NOYB
			$valid = (($type == "url" || $type == "urltable") && (is_ipaddr($tmp) || is_subnet($tmp))) ||
2282 593e9fe3 Phil Davis
				(($type == "url_ports" || $type == "urltable_ports") && is_port_or_range($tmp));
2283 f6622167 NOYB
			if ($valid) {
2284
				$items[] = $tmp;
2285
				if (count($items) == $max_items) {
2286
					break;
2287
				}
2288 23a193da Phil Davis
			}
2289 6d1907a3 Renato Botelho
		}
2290
	}
2291
	fclose($fd);
2292 f6622167 NOYB
	return array_merge($comments, $items);
2293 6d1907a3 Renato Botelho
}
2294
2295 f6ba4bd1 Scott Ullrich
function update_alias_url_data() {
2296
	global $config, $g;
2297 e5953c68 Ermal
2298 8422cdd5 Ermal
	$updated = false;
2299
2300 f6ba4bd1 Scott Ullrich
	/* item is a url type */
2301 8422cdd5 Ermal
	$lockkey = lock('aliasurl');
2302 e5953c68 Ermal
	if (is_array($config['aliases']['alias'])) {
2303
		foreach ($config['aliases']['alias'] as $x => $alias) {
2304 23a193da Phil Davis
			if (empty($alias['aliasurl'])) {
2305 e5953c68 Ermal
				continue;
2306 23a193da Phil Davis
			}
2307 e5953c68 Ermal
2308 6d1907a3 Renato Botelho
			$address = null;
2309 2ef16014 bcyrill
			foreach ($alias['aliasurl'] as $alias_url) {
2310
				/* fetch down and add in */
2311
				$temp_filename = tempnam("{$g['tmp_path']}/", "alias_import");
2312
				unlink($temp_filename);
2313 76590ffe Renato Botelho
				$verify_ssl = isset($config['system']['checkaliasesurlcert']);
2314 873c1701 Renato Botelho
				mkdir($temp_filename);
2315 37af5cf5 Chris Buechler
				if (!download_file($alias_url, $temp_filename . "/aliases", $verify_ssl)) {
2316
					log_error(sprintf(gettext("Failed to download alias %s"), $alias_url));
2317
					continue;
2318
				}
2319 76590ffe Renato Botelho
2320 2ef16014 bcyrill
				/* if the item is tar gzipped then extract */
2321 e45bae34 Ermal
				if (stripos($alias_url, '.tgz')) {
2322 23a193da Phil Davis
					if (!process_alias_tgz($temp_filename)) {
2323 e45bae34 Ermal
						continue;
2324 23a193da Phil Davis
					}
2325 e45bae34 Ermal
				}
2326 2ef16014 bcyrill
				if (file_exists("{$temp_filename}/aliases")) {
2327 ceb9cca7 Chris Buechler
					$address = parse_aliases_file("{$temp_filename}/aliases", $alias['type'], 5000);
2328 2ef16014 bcyrill
					mwexec("/bin/rm -rf {$temp_filename}");
2329 f6ba4bd1 Scott Ullrich
				}
2330 2ef16014 bcyrill
			}
2331 6d1907a3 Renato Botelho
			if ($address != null) {
2332
				$config['aliases']['alias'][$x]['address'] = implode(" ", $address);
2333 2ef16014 bcyrill
				$updated = true;
2334 f6ba4bd1 Scott Ullrich
			}
2335
		}
2336
	}
2337 26d060bc Ermal
	unlock($lockkey);
2338 8422cdd5 Ermal
2339
	/* Report status to callers as well */
2340
	return $updated;
2341 f6ba4bd1 Scott Ullrich
}
2342
2343
function process_alias_tgz($temp_filename) {
2344 23a193da Phil Davis
	if (!file_exists('/usr/bin/tar')) {
2345 e45bae34 Ermal
		log_error(gettext("Alias archive is a .tar/tgz file which cannot be decompressed because utility is missing!"));
2346
		return false;
2347
	}
2348 873c1701 Renato Botelho
	rename("{$temp_filename}/aliases", "{$temp_filename}/aliases.tgz");
2349 f6ba4bd1 Scott Ullrich
	mwexec("/usr/bin/tar xzf {$temp_filename}/aliases.tgz -C {$temp_filename}/aliases/");
2350
	unlink("{$temp_filename}/aliases.tgz");
2351
	$files_to_process = return_dir_as_array("{$temp_filename}/");
2352
	/* foreach through all extracted files and build up aliases file */
2353 e45bae34 Ermal
	$fd = @fopen("{$temp_filename}/aliases", "w");
2354
	if (!$fd) {
2355 e8c516a0 Phil Davis
		log_error(sprintf(gettext("Could not open %s/aliases for writing!"), $temp_filename));
2356 e45bae34 Ermal
		return false;
2357
	}
2358 23a193da Phil Davis
	foreach ($files_to_process as $f2p) {
2359 e45bae34 Ermal
		$tmpfd = @fopen($f2p, 'r');
2360
		if (!$tmpfd) {
2361 e8c516a0 Phil Davis
			log_error(sprintf(gettext('The following file could not be read %1$s from %2$s'), $f2p, $temp_filename));
2362 e45bae34 Ermal
			continue;
2363
		}
2364 23a193da Phil Davis
		while (($tmpbuf = fread($tmpfd, 65536)) !== FALSE) {
2365 e45bae34 Ermal
			fwrite($fd, $tmpbuf);
2366 23a193da Phil Davis
		}
2367 e45bae34 Ermal
		fclose($tmpfd);
2368 f6ba4bd1 Scott Ullrich
		unlink($f2p);
2369
	}
2370
	fclose($fd);
2371 e45bae34 Ermal
	unset($tmpbuf);
2372
2373
	return true;
2374 f6ba4bd1 Scott Ullrich
}
2375
2376 a76c1c45 jim-p
function version_compare_dates($a, $b) {
2377
	$a_time = strtotime($a);
2378
	$b_time = strtotime($b);
2379
2380
	if ((!$a_time) || (!$b_time)) {
2381
		return FALSE;
2382
	} else {
2383 23a193da Phil Davis
		if ($a_time < $b_time) {
2384 a76c1c45 jim-p
			return -1;
2385 23a193da Phil Davis
		} elseif ($a_time == $b_time) {
2386 a76c1c45 jim-p
			return 0;
2387 23a193da Phil Davis
		} else {
2388 a76c1c45 jim-p
			return 1;
2389 23a193da Phil Davis
		}
2390 a76c1c45 jim-p
	}
2391
}
2392
function version_get_string_value($a) {
2393
	$strs = array(
2394
		0 => "ALPHA-ALPHA",
2395
		2 => "ALPHA",
2396
		3 => "BETA",
2397
		4 => "B",
2398 5eb03383 jim-p
		5 => "C",
2399
		6 => "D",
2400
		7 => "RC",
2401 f8c8d65c Stilez
		8 => "RELEASE",
2402
		9 => "*"			// Matches all release levels
2403 a76c1c45 jim-p
	);
2404
	$major = 0;
2405
	$minor = 0;
2406
	foreach ($strs as $num => $str) {
2407
		if (substr($a, 0, strlen($str)) == $str) {
2408
			$major = $num;
2409
			$n = substr($a, strlen($str));
2410 23a193da Phil Davis
			if (is_numeric($n)) {
2411 a76c1c45 jim-p
				$minor = $n;
2412 23a193da Phil Davis
			}
2413 a76c1c45 jim-p
			break;
2414
		}
2415
	}
2416
	return "{$major}.{$minor}";
2417
}
2418
function version_compare_string($a, $b) {
2419 f8c8d65c Stilez
	// Only compare string parts if both versions give a specific release
2420
	// (If either version lacks a string part, assume intended to match all release levels)
2421 23a193da Phil Davis
	if (isset($a) && isset($b)) {
2422 c96e71d1 Renato Botelho
		return version_compare_numeric(version_get_string_value($a), version_get_string_value($b));
2423 23a193da Phil Davis
	} else {
2424 c96e71d1 Renato Botelho
		return 0;
2425 23a193da Phil Davis
	}
2426 a76c1c45 jim-p
}
2427
function version_compare_numeric($a, $b) {
2428 48081e6c Phil Davis
	$a_arr = explode('.', rtrim($a, '.'));
2429
	$b_arr = explode('.', rtrim($b, '.'));
2430 a76c1c45 jim-p
2431
	foreach ($a_arr as $n => $val) {
2432
		if (array_key_exists($n, $b_arr)) {
2433
			// So far so good, both have values at this minor version level. Compare.
2434 23a193da Phil Davis
			if ($val > $b_arr[$n]) {
2435 a76c1c45 jim-p
				return 1;
2436 23a193da Phil Davis
			} elseif ($val < $b_arr[$n]) {
2437 a76c1c45 jim-p
				return -1;
2438 23a193da Phil Davis
			}
2439 a76c1c45 jim-p
		} else {
2440
			// a is greater, since b doesn't have any minor version here.
2441
			return 1;
2442
		}
2443
	}
2444
	if (count($b_arr) > count($a_arr)) {
2445
		// b is longer than a, so it must be greater.
2446
		return -1;
2447
	} else {
2448
		// Both a and b are of equal length and value.
2449
		return 0;
2450
	}
2451
}
2452
function pfs_version_compare($cur_time, $cur_text, $remote) {
2453
	// First try date compare
2454 bda131b2 jim-p
	$v = version_compare_dates($cur_time, $remote);
2455 a76c1c45 jim-p
	if ($v === FALSE) {
2456
		// If that fails, try to compare by string
2457
		// Before anything else, simply test if the strings are equal
2458 23a193da Phil Davis
		if (($cur_text == $remote) || ($cur_time == $remote)) {
2459 a76c1c45 jim-p
			return 0;
2460 23a193da Phil Davis
		}
2461 a76c1c45 jim-p
		list($cur_num, $cur_str) = explode('-', $cur_text);
2462
		list($rem_num, $rem_str) = explode('-', $remote);
2463
2464
		// First try to compare the numeric parts of the version string.
2465
		$v = version_compare_numeric($cur_num, $rem_num);
2466
2467
		// If the numeric parts are the same, compare the string parts.
2468 23a193da Phil Davis
		if ($v == 0) {
2469 a76c1c45 jim-p
			return version_compare_string($cur_str, $rem_str);
2470 23a193da Phil Davis
		}
2471 a76c1c45 jim-p
	}
2472
	return $v;
2473
}
2474 3b07f4fe NOYB
function process_alias_urltable($name, $type, $url, $freq, $forceupdate=false, $validateonly=false) {
2475 03afdafa NOYB
	global $g, $config;
2476 dd042c51 Renato Botelho
2477 c7de8be4 jim-p
	$urltable_prefix = "/var/db/aliastables/";
2478
	$urltable_filename = $urltable_prefix . $name . ".txt";
2479 e9fea9dc Chris Buechler
	$tmp_urltable_filename = $urltable_filename . ".tmp";
2480 c7de8be4 jim-p
2481
	// Make the aliases directory if it doesn't exist
2482
	if (!file_exists($urltable_prefix)) {
2483
		mkdir($urltable_prefix);
2484
	} elseif (!is_dir($urltable_prefix)) {
2485
		unlink($urltable_prefix);
2486
		mkdir($urltable_prefix);
2487
	}
2488
2489
	// If the file doesn't exist or is older than update_freq days, fetch a new copy.
2490 cc293ac0 Chris Buechler
	if (!file_exists($urltable_filename) || (filesize($urltable_filename) == "0") ||
2491 23a193da Phil Davis
	    ((time() - filemtime($urltable_filename)) > ($freq * 86400 - 90)) ||
2492
	    $forceupdate) {
2493 c7de8be4 jim-p
2494
		// Try to fetch the URL supplied
2495 e9fea9dc Chris Buechler
		unlink_if_exists($tmp_urltable_filename);
2496 dd042c51 Renato Botelho
		$verify_ssl = isset($config['system']['checkaliasesurlcert']);
2497 e9fea9dc Chris Buechler
		if (download_file($url, $tmp_urltable_filename, $verify_ssl)) {
2498 f6622167 NOYB
			// Convert lines that begin with '$' or ';' to comments '#' instead of deleting them.
2499
			mwexec("/usr/bin/sed -i \"\" -E 's/^[[:space:]]*($|#|;)/#/g; /^#/!s/\;.*//g;' ". escapeshellarg($tmp_urltable_filename));
2500 ebe833f6 NOYB
2501 3b07f4fe NOYB
			$type = ($type) ? $type : alias_get_type($name);	// If empty type passed, try to get it from config.
2502 ebe833f6 NOYB
2503
			$parsed_contents = parse_aliases_file($tmp_urltable_filename, $type, "-1", true);
2504 f42ef69a NOYB
			if ($type == "urltable_ports") {
2505 ebe833f6 NOYB
				$parsed_contents = group_ports($parsed_contents, true);
2506
			}
2507
			if (is_array($parsed_contents)) {
2508
				file_put_contents($urltable_filename, implode("\n", $parsed_contents));
2509 e5581024 Chris Buechler
			} else {
2510
				touch($urltable_filename);
2511 dd042c51 Renato Botelho
			}
2512 ebe833f6 NOYB
2513 08696051 NOYB
			/* Remove existing archive and create an up to date archive if RAM disk is enabled. */
2514
			unlink_if_exists("{$g['cf_conf_path']}/RAM_Disk_Store/{$name}.txt.tgz");
2515
			if (isset($config['system']['use_mfs_tmpvar'])) {
2516 257d2fd6 NOYB
				mwexec("/usr/bin/tar -czf " . escapeshellarg("{$g['cf_conf_path']}/RAM_Disk_Store/{$name}.txt.tgz") . " -C / " . escapeshellarg($urltable_filename));
2517 03afdafa NOYB
			}
2518 08696051 NOYB
2519 e9fea9dc Chris Buechler
			unlink_if_exists($tmp_urltable_filename);
2520 23a193da Phil Davis
		} else {
2521 b913daf8 Chris Buechler
			if (!$validateonly) {
2522
				touch($urltable_filename);
2523
			}
2524 ca46f1de Chris Buechler
			return false;
2525 23a193da Phil Davis
		}
2526 966f359e Ermal
		return true;
2527 c7de8be4 jim-p
	} else {
2528 23a193da Phil Davis
		// File exists, and it doesn't need to be updated.
2529 c7de8be4 jim-p
		return -1;
2530
	}
2531
}
2532 26c8cc72 jim-p
2533 38080cc1 Scott Ullrich
function get_include_contents($filename) {
2534 5fa78adc Renato Botelho
	if (is_file($filename)) {
2535
		ob_start();
2536
		include $filename;
2537
		$contents = ob_get_contents();
2538
		ob_end_clean();
2539
		return $contents;
2540
	}
2541
	return false;
2542 38080cc1 Scott Ullrich
}
2543
2544 3ffa8318 Renato Botelho
/* This xml 2 array function is courtesy of the php.net comment section on xml_parse.
2545
 * it is roughly 4 times faster then our existing pfSense parser but due to the large
2546
 * size of the RRD xml dumps this is required.
2547
 * The reason we do not use it for pfSense is that it does not know about array fields
2548
 * which causes it to fail on array fields with single items. Possible Todo?
2549
 */
2550 086cf944 Phil Davis
function xml2array($contents, $get_attributes = 1, $priority = 'tag') {
2551 23a193da Phil Davis
	if (!function_exists('xml_parser_create')) {
2552 86c707f3 Darren Embry
		return array ();
2553
	}
2554
	$parser = xml_parser_create('');
2555
	xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, "UTF-8");
2556
	xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
2557
	xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
2558
	xml_parse_into_struct($parser, trim($contents), $xml_values);
2559
	xml_parser_free($parser);
2560 23a193da Phil Davis
	if (!$xml_values) {
2561 86c707f3 Darren Embry
		return; //Hmm...
2562 23a193da Phil Davis
	}
2563 86c707f3 Darren Embry
	$xml_array = array ();
2564
	$parents = array ();
2565
	$opened_tags = array ();
2566
	$arr = array ();
2567
	$current = & $xml_array;
2568
	$repeated_tag_index = array ();
2569 23a193da Phil Davis
	foreach ($xml_values as $data) {
2570 86c707f3 Darren Embry
		unset ($attributes, $value);
2571
		extract($data);
2572
		$result = array ();
2573
		$attributes_data = array ();
2574 23a193da Phil Davis
		if (isset ($value)) {
2575
			if ($priority == 'tag') {
2576 86c707f3 Darren Embry
				$result = $value;
2577 23a193da Phil Davis
			} else {
2578 86c707f3 Darren Embry
				$result['value'] = $value;
2579 23a193da Phil Davis
			}
2580 86c707f3 Darren Embry
		}
2581 23a193da Phil Davis
		if (isset ($attributes) and $get_attributes) {
2582
			foreach ($attributes as $attr => $val) {
2583
				if ($priority == 'tag') {
2584 86c707f3 Darren Embry
					$attributes_data[$attr] = $val;
2585 23a193da Phil Davis
				} else {
2586 86c707f3 Darren Embry
					$result['attr'][$attr] = $val; //Set all the attributes in a array called 'attr'
2587 23a193da Phil Davis
				}
2588 86c707f3 Darren Embry
			}
2589
		}
2590 23a193da Phil Davis
		if ($type == "open") {
2591 86c707f3 Darren Embry
			$parent[$level -1] = & $current;
2592 23a193da Phil Davis
			if (!is_array($current) or (!in_array($tag, array_keys($current)))) {
2593 86c707f3 Darren Embry
				$current[$tag] = $result;
2594 23a193da Phil Davis
				if ($attributes_data) {
2595 86c707f3 Darren Embry
					$current[$tag . '_attr'] = $attributes_data;
2596 23a193da Phil Davis
				}
2597 86c707f3 Darren Embry
				$repeated_tag_index[$tag . '_' . $level] = 1;
2598
				$current = & $current[$tag];
2599 23a193da Phil Davis
			} else {
2600
				if (isset ($current[$tag][0])) {
2601 86c707f3 Darren Embry
					$current[$tag][$repeated_tag_index[$tag . '_' . $level]] = $result;
2602
					$repeated_tag_index[$tag . '_' . $level]++;
2603 23a193da Phil Davis
				} else {
2604 86c707f3 Darren Embry
					$current[$tag] = array (
2605
						$current[$tag],
2606
						$result
2607
						);
2608
					$repeated_tag_index[$tag . '_' . $level] = 2;
2609 23a193da Phil Davis
					if (isset ($current[$tag . '_attr'])) {
2610 86c707f3 Darren Embry
						$current[$tag]['0_attr'] = $current[$tag . '_attr'];
2611
						unset ($current[$tag . '_attr']);
2612
					}
2613
				}
2614
				$last_item_index = $repeated_tag_index[$tag . '_' . $level] - 1;
2615
				$current = & $current[$tag][$last_item_index];
2616
			}
2617 23a193da Phil Davis
		} elseif ($type == "complete") {
2618
			if (!isset ($current[$tag])) {
2619 86c707f3 Darren Embry
				$current[$tag] = $result;
2620
				$repeated_tag_index[$tag . '_' . $level] = 1;
2621 23a193da Phil Davis
				if ($priority == 'tag' and $attributes_data) {
2622 86c707f3 Darren Embry
					$current[$tag . '_attr'] = $attributes_data;
2623 23a193da Phil Davis
				}
2624
			} else {
2625
				if (isset ($current[$tag][0]) and is_array($current[$tag])) {
2626 86c707f3 Darren Embry
					$current[$tag][$repeated_tag_index[$tag . '_' . $level]] = $result;
2627 23a193da Phil Davis
					if ($priority == 'tag' and $get_attributes and $attributes_data) {
2628 86c707f3 Darren Embry
						$current[$tag][$repeated_tag_index[$tag . '_' . $level] . '_attr'] = $attributes_data;
2629
					}
2630
					$repeated_tag_index[$tag . '_' . $level]++;
2631 23a193da Phil Davis
				} else {
2632 86c707f3 Darren Embry
					$current[$tag] = array (
2633
						$current[$tag],
2634
						$result
2635
						);
2636
					$repeated_tag_index[$tag . '_' . $level] = 1;
2637 23a193da Phil Davis
					if ($priority == 'tag' and $get_attributes) {
2638
						if (isset ($current[$tag . '_attr'])) {
2639 86c707f3 Darren Embry
							$current[$tag]['0_attr'] = $current[$tag . '_attr'];
2640
							unset ($current[$tag . '_attr']);
2641
						}
2642 23a193da Phil Davis
						if ($attributes_data) {
2643 86c707f3 Darren Embry
							$current[$tag][$repeated_tag_index[$tag . '_' . $level] . '_attr'] = $attributes_data;
2644
						}
2645
					}
2646
					$repeated_tag_index[$tag . '_' . $level]++; //0 and 1 index is already taken
2647
				}
2648
			}
2649 23a193da Phil Davis
		} elseif ($type == 'close') {
2650 86c707f3 Darren Embry
			$current = & $parent[$level -1];
2651
		}
2652
	}
2653
	return ($xml_array);
2654 3ffa8318 Renato Botelho
}
2655
2656
function get_country_name($country_code) {
2657 23a193da Phil Davis
	if ($country_code != "ALL" && strlen($country_code) != 2) {
2658 3ffa8318 Renato Botelho
		return "";
2659 23a193da Phil Davis
	}
2660 3ffa8318 Renato Botelho
2661 cb0f6bf4 Renato Botelho
	$country_names_xml = "/usr/local/share/pfSense/iso_3166-1_list_en.xml";
2662 3ffa8318 Renato Botelho
	$country_names_contents = file_get_contents($country_names_xml);
2663
	$country_names = xml2array($country_names_contents);
2664
2665 23a193da Phil Davis
	if ($country_code == "ALL") {
2666 3ffa8318 Renato Botelho
		$country_list = array();
2667 23a193da Phil Davis
		foreach ($country_names['ISO_3166-1_List_en']['ISO_3166-1_Entry'] as $country) {
2668
			$country_list[] = array(
2669
				"code" => $country['ISO_3166-1_Alpha-2_Code_element'],
2670
				"name" => ucwords(strtolower($country['ISO_3166-1_Country_name'])));
2671 3ffa8318 Renato Botelho
		}
2672
		return $country_list;
2673
	}
2674
2675
	foreach ($country_names['ISO_3166-1_List_en']['ISO_3166-1_Entry'] as $country) {
2676
		if ($country['ISO_3166-1_Alpha-2_Code_element'] == strtoupper($country_code)) {
2677
			return ucwords(strtolower($country['ISO_3166-1_Country_name']));
2678
		}
2679
	}
2680
	return "";
2681
}
2682
2683 baaa8bb1 Erik Fonnesbeck
/* sort by interface only, retain the original order of rules that apply to
2684
   the same interface */
2685
function filter_rules_sort() {
2686
	global $config;
2687
2688
	/* mark each rule with the sequence number (to retain the order while sorting) */
2689 23a193da Phil Davis
	for ($i = 0; isset($config['filter']['rule'][$i]); $i++) {
2690 baaa8bb1 Erik Fonnesbeck
		$config['filter']['rule'][$i]['seq'] = $i;
2691 23a193da Phil Davis
	}
2692 baaa8bb1 Erik Fonnesbeck
2693
	usort($config['filter']['rule'], "filter_rules_compare");
2694
2695
	/* strip the sequence numbers again */
2696 23a193da Phil Davis
	for ($i = 0; isset($config['filter']['rule'][$i]); $i++) {
2697 baaa8bb1 Erik Fonnesbeck
		unset($config['filter']['rule'][$i]['seq']);
2698 23a193da Phil Davis
	}
2699 baaa8bb1 Erik Fonnesbeck
}
2700
function filter_rules_compare($a, $b) {
2701 23a193da Phil Davis
	if (isset($a['floating']) && isset($b['floating'])) {
2702 baaa8bb1 Erik Fonnesbeck
		return $a['seq'] - $b['seq'];
2703 23a193da Phil Davis
	} else if (isset($a['floating'])) {
2704 baaa8bb1 Erik Fonnesbeck
		return -1;
2705 23a193da Phil Davis
	} else if (isset($b['floating'])) {
2706 baaa8bb1 Erik Fonnesbeck
		return 1;
2707 23a193da Phil Davis
	} else if ($a['interface'] == $b['interface']) {
2708 cea355a5 Erik Fonnesbeck
		return $a['seq'] - $b['seq'];
2709 23a193da Phil Davis
	} else {
2710 baaa8bb1 Erik Fonnesbeck
		return compare_interface_friendly_names($a['interface'], $b['interface']);
2711 23a193da Phil Davis
	}
2712 baaa8bb1 Erik Fonnesbeck
}
2713
2714 22dae853 Seth Mos
function generate_ipv6_from_mac($mac) {
2715
	$elements = explode(":", $mac);
2716 23a193da Phil Davis
	if (count($elements) <> 6) {
2717 22dae853 Seth Mos
		return false;
2718 23a193da Phil Davis
	}
2719 22dae853 Seth Mos
2720
	$i = 0;
2721 5aa28c86 Seth Mos
	$ipv6 = "fe80::";
2722 23a193da Phil Davis
	foreach ($elements as $byte) {
2723
		if ($i == 0) {
2724 4de8f7ba Phil Davis
			$hexadecimal = substr($byte, 1, 2);
2725 22dae853 Seth Mos
			$bitmap = base_convert($hexadecimal, 16, 2);
2726
			$bitmap = str_pad($bitmap, 4, "0", STR_PAD_LEFT);
2727 4de8f7ba Phil Davis
			$bitmap = substr($bitmap, 0, 2) ."1". substr($bitmap, 3, 4);
2728 22dae853 Seth Mos
			$byte = substr($byte, 0, 1) . base_convert($bitmap, 2, 16);
2729
		}
2730
		$ipv6 .= $byte;
2731 23a193da Phil Davis
		if ($i == 1) {
2732 22dae853 Seth Mos
			$ipv6 .= ":";
2733
		}
2734 23a193da Phil Davis
		if ($i == 3) {
2735 22dae853 Seth Mos
			$ipv6 .= ":";
2736
		}
2737 23a193da Phil Davis
		if ($i == 2) {
2738 22dae853 Seth Mos
			$ipv6 .= "ff:fe";
2739
		}
2740 5fa78adc Renato Botelho
2741 22dae853 Seth Mos
		$i++;
2742 5fa78adc Renato Botelho
	}
2743 fcdc8943 Seth Mos
	return $ipv6;
2744 22dae853 Seth Mos
}
2745 325e3163 Bill Marquette
2746 57f2840e Evgeny
/****f* pfsense-utils/load_mac_manufacturer_table
2747
 * NAME
2748
 *   load_mac_manufacturer_table
2749
 * INPUTS
2750
 *   none
2751
 * RESULT
2752
 *   returns associative array with MAC-Manufacturer pairs
2753
 ******/
2754
function load_mac_manufacturer_table() {
2755
	/* load MAC-Manufacture data from the file */
2756 62a29fe3 Ermal
	$macs = false;
2757 23a193da Phil Davis
	if (file_exists("/usr/local/share/nmap/nmap-mac-prefixes")) {
2758 62a29fe3 Ermal
		$macs=file("/usr/local/share/nmap/nmap-mac-prefixes");
2759 23a193da Phil Davis
	}
2760
	if ($macs) {
2761
		foreach ($macs as $line) {
2762
			if (preg_match('/([0-9A-Fa-f]{6}) (.*)$/', $line, $matches)) {
2763 4450527f Evgeny
				/* store values like this $mac_man['000C29']='VMware' */
2764 4de8f7ba Phil Davis
				$mac_man["$matches[1]"] = $matches[2];
2765 57f2840e Evgeny
			}
2766
		}
2767 5fa78adc Renato Botelho
		return $mac_man;
2768 23a193da Phil Davis
	} else {
2769 57f2840e Evgeny
		return -1;
2770 23a193da Phil Davis
	}
2771 57f2840e Evgeny
2772
}
2773
2774 474f36d1 Scott Ullrich
/****f* pfsense-utils/is_ipaddr_configured
2775
 * NAME
2776
 *   is_ipaddr_configured
2777
 * INPUTS
2778
 *   IP Address to check.
2779 4665dbdd Renato Botelho
 *   If ignore_if is a VIP (not carp), vip array index is passed after string _virtualip
2780 f680e46c jim-p
 *   check_localip - if true then also check for matches with PPTP and L2TP addresses
2781 3490b8dd Phil Davis
 *   check_subnets - if true then check if the given ipaddr is contained anywhere in the subnet of any other configured IP address
2782
 *   cidrprefix - the CIDR prefix (16, 20, 24, 64...) of ipaddr.
2783 086cf944 Phil Davis
 *     If check_subnets is true and cidrprefix is specified,
2784 3490b8dd Phil Davis
 *     then check if the ipaddr/cidrprefix subnet overlaps the subnet of any other configured IP address
2785 474f36d1 Scott Ullrich
 * RESULT
2786 3490b8dd Phil Davis
 *   returns true if the IP Address is configured and present on this device or overlaps a configured subnet.
2787 474f36d1 Scott Ullrich
*/
2788 3490b8dd Phil Davis
function is_ipaddr_configured($ipaddr, $ignore_if = "", $check_localip = false, $check_subnets = false, $cidrprefix = "") {
2789
	if (count(where_is_ipaddr_configured($ipaddr, $ignore_if, $check_localip, $check_subnets, $cidrprefix))) {
2790
		return true;
2791
	}
2792
	return false;
2793
}
2794
2795
/****f* pfsense-utils/where_is_ipaddr_configured
2796
 * NAME
2797
 *   where_is_ipaddr_configured
2798
 * INPUTS
2799
 *   IP Address to check.
2800
 *   If ignore_if is a VIP (not carp), vip array index is passed after string _virtualip
2801 f680e46c jim-p
 *   check_localip - if true then also check for matches with PPTP and L2TP addresses
2802 3490b8dd Phil Davis
 *   check_subnets - if true then check if the given ipaddr is contained anywhere in the subnet of any other configured IP address
2803
 *   cidrprefix - the CIDR prefix (16, 20, 24, 64...) of ipaddr.
2804 086cf944 Phil Davis
 *     If check_subnets is true and cidrprefix is specified,
2805 3490b8dd Phil Davis
 *     then check if the ipaddr/cidrprefix subnet overlaps the subnet of any other configured IP address
2806
 * RESULT
2807
 *   Returns an array of the interfaces 'if' plus IP address or subnet 'ip_or_subnet' that match or overlap the IP address to check.
2808
 *   If there are no matches then an empty array is returned.
2809
*/
2810
function where_is_ipaddr_configured($ipaddr, $ignore_if = "", $check_localip = false, $check_subnets = false, $cidrprefix = "") {
2811 e6c60013 Renato Botelho
	global $config;
2812
2813 3490b8dd Phil Davis
	$where_configured = array();
2814
2815 4665dbdd Renato Botelho
	$pos = strpos($ignore_if, '_virtualip');
2816
	if ($pos !== false) {
2817
		$ignore_vip_id = substr($ignore_if, $pos+10);
2818
		$ignore_vip_if = substr($ignore_if, 0, $pos);
2819
	} else {
2820
		$ignore_vip_id = -1;
2821
		$ignore_vip_if = $ignore_if;
2822
	}
2823
2824 1e5da31d Ermal
	$isipv6 = is_ipaddrv6($ipaddr);
2825
2826 cde28bfa Phil Davis
	if ($isipv6) {
2827
		$ipaddr = text_to_compressed_ip6($ipaddr);
2828
	}
2829
2830 e6c60013 Renato Botelho
	if ($check_subnets) {
2831 3490b8dd Phil Davis
		$cidrprefix = intval($cidrprefix);
2832
		if ($isipv6) {
2833
			if (($cidrprefix < 1) || ($cidrprefix > 128)) {
2834
				$cidrprefix = 128;
2835
			}
2836
		} else {
2837
			if (($cidrprefix < 1) || ($cidrprefix > 32)) {
2838
				$cidrprefix = 32;
2839
			}
2840
		}
2841 e6c60013 Renato Botelho
		$iflist = get_configured_interface_list();
2842
		foreach ($iflist as $if => $ifname) {
2843 23a193da Phil Davis
			if ($ignore_if == $if) {
2844 e6c60013 Renato Botelho
				continue;
2845 23a193da Phil Davis
			}
2846 2c98a935 Renato Botelho
2847 3490b8dd Phil Davis
			if ($isipv6) {
2848
				$if_ipv6 = get_interface_ipv6($if);
2849
				$if_snbitsv6 = get_interface_subnetv6($if);
2850
				if ($if_ipv6 && $if_snbitsv6 && check_subnetsv6_overlap($ipaddr, $cidrprefix, $if_ipv6, $if_snbitsv6)) {
2851
					$where_entry = array();
2852
					$where_entry['if'] = $if;
2853
					$where_entry['ip_or_subnet'] = get_interface_ipv6($if) . "/" . get_interface_subnetv6($if);
2854
					$where_configured[] = $where_entry;
2855
				}
2856 1e5da31d Ermal
			} else {
2857 3490b8dd Phil Davis
				$if_ipv4 = get_interface_ip($if);
2858
				$if_snbitsv4 = get_interface_subnet($if);
2859
				if ($if_ipv4 && $if_snbitsv4 && check_subnets_overlap($ipaddr, $cidrprefix, $if_ipv4, $if_snbitsv4)) {
2860
					$where_entry = array();
2861
					$where_entry['if'] = $if;
2862
					$where_entry['ip_or_subnet'] = get_interface_ip($if) . "/" . get_interface_subnet($if);
2863
					$where_configured[] = $where_entry;
2864 4de8f7ba Phil Davis
				}
2865 23a193da Phil Davis
			}
2866 e6c60013 Renato Botelho
		}
2867
	} else {
2868 3490b8dd Phil Davis
		if ($isipv6) {
2869 2c98a935 Renato Botelho
			$interface_list_ips = get_configured_ipv6_addresses();
2870 23a193da Phil Davis
		} else {
2871 2c98a935 Renato Botelho
			$interface_list_ips = get_configured_ip_addresses();
2872 23a193da Phil Davis
		}
2873 2c98a935 Renato Botelho
2874 23a193da Phil Davis
		foreach ($interface_list_ips as $if => $ilips) {
2875
			if ($ignore_if == $if) {
2876 e6c60013 Renato Botelho
				continue;
2877 23a193da Phil Davis
			}
2878
			if (strcasecmp($ipaddr, $ilips) == 0) {
2879 3490b8dd Phil Davis
				$where_entry = array();
2880
				$where_entry['if'] = $if;
2881
				$where_entry['ip_or_subnet'] = $ilips;
2882
				$where_configured[] = $where_entry;
2883 23a193da Phil Davis
			}
2884 e6c60013 Renato Botelho
		}
2885 5fa78adc Renato Botelho
	}
2886 a1613b62 Renato Botelho
2887 e6c60013 Renato Botelho
	if ($check_localip) {
2888 f0b1358d Phil Davis
		if (!is_array($config['l2tp']) && !empty($config['l2tp']['localip']) && (strcasecmp($ipaddr, text_to_compressed_ip6($config['l2tp']['localip'])) == 0)) {
2889 3490b8dd Phil Davis
			$where_entry = array();
2890
			$where_entry['if'] = 'l2tp';
2891
			$where_entry['ip_or_subnet'] = $config['l2tp']['localip'];
2892
			$where_configured[] = $where_entry;
2893 23a193da Phil Davis
		}
2894 a1613b62 Renato Botelho
	}
2895
2896 3490b8dd Phil Davis
	return $where_configured;
2897 474f36d1 Scott Ullrich
}
2898
2899 e4a8ed97 Scott Ullrich
/****f* pfsense-utils/pfSense_handle_custom_code
2900
 * NAME
2901
 *   pfSense_handle_custom_code
2902
 * INPUTS
2903
 *   directory name to process
2904
 * RESULT
2905
 *   globs the directory and includes the files
2906
 */
2907 d65962a7 Scott Ullrich
function pfSense_handle_custom_code($src_dir) {
2908 5fa78adc Renato Botelho
	// Allow extending of the nat edit page and include custom input validation
2909 23a193da Phil Davis
	if (is_dir("$src_dir")) {
2910 3dbceb92 Scott Ullrich
		$cf = glob($src_dir . "/*.inc");
2911 23a193da Phil Davis
		foreach ($cf as $nf) {
2912
			if ($nf == "." || $nf == "..") {
2913 d65962a7 Scott Ullrich
				continue;
2914 23a193da Phil Davis
			}
2915 d65962a7 Scott Ullrich
			// Include the extra handler
2916 86573bb9 Phil Davis
			include_once("$nf");
2917 d65962a7 Scott Ullrich
		}
2918
	}
2919
}
2920
2921 ceecd29b Renato Botelho
function set_language() {
2922
	global $config, $g;
2923
2924
	if (!empty($config['system']['language'])) {
2925
		$lang = $config['system']['language'];
2926
	} elseif (!empty($g['language'])) {
2927
		$lang = $g['language'];
2928
	}
2929
	$lang .= ".UTF-8";
2930
2931
	putenv("LANG={$lang}");
2932 53c25dec Renato Botelho
	setlocale(LC_ALL, $lang);
2933
	textdomain("pfSense");
2934
	bindtextdomain("pfSense", "/usr/local/share/locale");
2935
	bind_textdomain_codeset("pfSense", $lang);
2936 3e139f90 Vinicius Coque
}
2937
2938
function get_locale_list() {
2939
	$locales = array(
2940 b0572bad Renato Botelho
		"bs" => gettext("Bosnian"),
2941 3cf5ed75 Renato Botelho
		"zh_Hans_CN" => gettext("Chinese (Simplified, China)"),
2942
		"zh_TW" => gettext("Chinese (Taiwan)"),
2943
		"nl" => gettext("Dutch"),
2944 3e139f90 Vinicius Coque
		"en_US" => gettext("English"),
2945 3cf5ed75 Renato Botelho
		"de_DE" => gettext("German (Germany)"),
2946 42190719 Renato Botelho
		"nb" => gettext("Norwegian Bokmål"),
2947 ea4f7b62 Renato Botelho
		"pl" => gettext("Polish"),
2948 9b18dc1b Renato Botelho
		"pt_BR" => gettext("Portuguese (Brazil)"),
2949 a6f3daf0 Renato Botelho
		"ru" => gettext("Russian"),
2950 3cf5ed75 Renato Botelho
		"es" => gettext("Spanish"),
2951
		"es_AR" => gettext("Spanish (Argentina)"),
2952 3e139f90 Vinicius Coque
	);
2953 e402b079 Steve Beaver
2954
	// If the locales are sorted, the order changes depending on the language selected. If the user accidentally
2955
	// selects the wrong language, this makes it very difficult to guess the intended language. NOT sorting
2956
	// allows the user to remember that English (say) is the second on the list and to get back to it more easily
2957
2958
	//asort($locales);
2959
2960 3e139f90 Vinicius Coque
	return $locales;
2961
}
2962 20a7cb15 smos
2963
function return_hex_ipv4($ipv4) {
2964 23a193da Phil Davis
	if (!is_ipaddrv4($ipv4)) {
2965 20a7cb15 smos
		return(false);
2966 23a193da Phil Davis
	}
2967 5fa78adc Renato Botelho
2968 20a7cb15 smos
	/* we need the hex form of the interface IPv4 address */
2969
	$ip4arr = explode(".", $ipv4);
2970 733c6f89 Ermal
	return (sprintf("%02x%02x%02x%02x", $ip4arr[0], $ip4arr[1], $ip4arr[2], $ip4arr[3]));
2971 20a7cb15 smos
}
2972
2973
function convert_ipv6_to_128bit($ipv6) {
2974 23a193da Phil Davis
	if (!is_ipaddrv6($ipv6)) {
2975 20a7cb15 smos
		return(false);
2976 23a193da Phil Davis
	}
2977 20a7cb15 smos
2978
	$ip6arr = array();
2979
	$ip6prefix = Net_IPv6::uncompress($ipv6);
2980
	$ip6arr = explode(":", $ip6prefix);
2981
	/* binary presentation of the prefix for all 128 bits. */
2982
	$ip6prefixbin = "";
2983 23a193da Phil Davis
	foreach ($ip6arr as $element) {
2984 20a7cb15 smos
		$ip6prefixbin .= sprintf("%016b", hexdec($element));
2985
	}
2986
	return($ip6prefixbin);
2987
}
2988
2989
function convert_128bit_to_ipv6($ip6bin) {
2990 23a193da Phil Davis
	if (strlen($ip6bin) <> 128) {
2991 20a7cb15 smos
		return(false);
2992 23a193da Phil Davis
	}
2993 20a7cb15 smos
2994
	$ip6arr = array();
2995
	$ip6binarr = array();
2996
	$ip6binarr = str_split($ip6bin, 16);
2997 23a193da Phil Davis
	foreach ($ip6binarr as $binpart) {
2998 20a7cb15 smos
		$ip6arr[] = dechex(bindec($binpart));
2999 23a193da Phil Davis
	}
3000 587995fb Phil Davis
	$ip6addr = text_to_compressed_ip6(implode(":", $ip6arr));
3001 20a7cb15 smos
3002
	return($ip6addr);
3003
}
3004
3005 8b198c64 smos
3006
/* Returns the calculated bit length of the prefix delegation from the WAN interface */
3007
/* DHCP-PD is variable, calculate from the prefix-len on the WAN interface */
3008
/* 6rd is variable, calculate from 64 - (v6 prefixlen - (32 - v4 prefixlen)) */
3009
/* 6to4 is 16 bits, e.g. 65535 */
3010
function calculate_ipv6_delegation_length($if) {
3011
	global $config;
3012
3013 23a193da Phil Davis
	if (!is_array($config['interfaces'][$if])) {
3014 8b198c64 smos
		return false;
3015 23a193da Phil Davis
	}
3016 8b198c64 smos
3017 23a193da Phil Davis
	switch ($config['interfaces'][$if]['ipaddrv6']) {
3018 8b198c64 smos
		case "6to4":
3019
			$pdlen = 16;
3020
			break;
3021
		case "6rd":
3022
			$rd6cfg = $config['interfaces'][$if];
3023
			$rd6plen = explode("/", $rd6cfg['prefix-6rd']);
3024
			$pdlen = (64 - ($rd6plen[1] + (32 - $rd6cfg['prefix-6rd-v4plen'])));
3025
			break;
3026
		case "dhcp6":
3027
			$dhcp6cfg = $config['interfaces'][$if];
3028
			$pdlen = $dhcp6cfg['dhcp6-ia-pd-len'];
3029
			break;
3030
		default:
3031
			$pdlen = 0;
3032
			break;
3033
	}
3034
	return($pdlen);
3035
}
3036 d23e157a smos
3037 a3d07046 Renato Botelho
function merge_ipv6_delegated_prefix($prefix, $suffix, $len = 64) {
3038
	$prefix = Net_IPv6::uncompress($prefix, true);
3039
	$suffix = Net_IPv6::uncompress($suffix, true);
3040
3041
	/*
3042
	 * xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx
3043
	 *                ^^^^ ^
3044
	 *                |||| \-> 64
3045
	 *                |||\---> 63, 62, 61, 60
3046
	 *                ||\----> 56
3047
	 *                |\-----> 52
3048
	 *                \------> 48
3049
	 */
3050
3051
	switch ($len) {
3052
	case 48:
3053
		$prefix_len = 15;
3054
		break;
3055
	case 52:
3056
		$prefix_len = 16;
3057
		break;
3058
	case 56:
3059
		$prefix_len = 17;
3060
		break;
3061 231fe954 Phil Davis
	case 59:
3062 a3d07046 Renato Botelho
	case 60:
3063
		$prefix_len = 18;
3064
		break;
3065
	/*
3066
	 * XXX 63, 62 and 61 should use 18 but PD can change and if
3067
	 * we let user chose this bit it can end up out of PD network
3068
	 *
3069
	 * Leave this with 20 for now until we find a way to let user
3070
	 * chose it. The side-effect is users with PD with one of these
3071
	 * lengths will not be able to setup DHCP server range for full
3072
	 * PD size, only for last /64 network
3073
	 */
3074
	case 63:
3075
	case 62:
3076
	case 61:
3077
	default:
3078
		$prefix_len = 20;
3079
		break;
3080
	}
3081
3082 587995fb Phil Davis
	return text_to_compressed_ip6(substr($prefix, 0, $prefix_len) .
3083 a3d07046 Renato Botelho
	    substr($suffix, $prefix_len));
3084 2bf455ca Renato Botelho
}
3085
3086 6c8beed3 Renato Botelho
function dhcpv6_pd_str_help($pdlen) {
3087
	$result = '';
3088
3089
	switch ($pdlen) {
3090
	case 48:
3091
		$result = '::xxxx:xxxx:xxxx:xxxx:xxxx';
3092
		break;
3093
	case 52:
3094
		$result = '::xxx:xxxx:xxxx:xxxx:xxxx';
3095
		break;
3096
	case 56:
3097
		$result = '::xx:xxxx:xxxx:xxxx:xxxx';
3098
		break;
3099 231fe954 Phil Davis
	case 59:
3100 6c8beed3 Renato Botelho
	case 60:
3101
		$result = '::x:xxxx:xxxx:xxxx:xxxx';
3102
		break;
3103
	/*
3104 b7908243 Phil Davis
	 * XXX 63, 62 and 61 should use same mask as 60 but if
3105
	 * we let the user choose this bit it can end up out of PD network
3106 6c8beed3 Renato Botelho
	 *
3107 b7908243 Phil Davis
	 * Leave this with the same as 64 for now until we find a way to
3108
	 * let the user choose it. The side-effect is users with PD with one
3109
	 * of these lengths will not be able to setup DHCP server ranges
3110 6c8beed3 Renato Botelho
	 * for full PD size, only for last /64 network
3111
	 */
3112
	case 61:
3113
	case 62:
3114
	case 63:
3115
	case 64:
3116 b7908243 Phil Davis
	default:
3117 6c8beed3 Renato Botelho
		$result = '::xxxx:xxxx:xxxx:xxxx';
3118
		break;
3119
	}
3120
3121
	return $result;
3122
}
3123
3124 d23e157a smos
function huawei_rssi_to_string($rssi) {
3125
	$dbm = array();
3126
	$i = 0;
3127 145cc518 smos
	$dbstart = -113;
3128 23a193da Phil Davis
	while ($i < 32) {
3129 145cc518 smos
		$dbm[$i] = $dbstart + ($i * 2);
3130 d23e157a smos
		$i++;
3131
	}
3132
	$percent = round(($rssi / 31) * 100);
3133 145cc518 smos
	$string = "rssi:{$rssi} level:{$dbm[$rssi]}dBm percent:{$percent}%";
3134 d23e157a smos
	return $string;
3135
}
3136
3137
function huawei_mode_to_string($mode, $submode) {
3138 e8c516a0 Phil Davis
	$modes[0] = gettext("None");
3139 5fa78adc Renato Botelho
	$modes[1] = "AMPS";
3140 d23e157a smos
	$modes[2] = "CDMA";
3141
	$modes[3] = "GSM/GPRS";
3142
	$modes[4] = "HDR";
3143
	$modes[5] = "WCDMA";
3144 5fa78adc Renato Botelho
	$modes[6] = "GPS";
3145 d23e157a smos
3146 e8c516a0 Phil Davis
	$submodes[0] = gettext("No Service");
3147 d23e157a smos
	$submodes[1] = "GSM";
3148
	$submodes[2] = "GPRS";
3149
	$submodes[3] = "EDGE";
3150
	$submodes[4] = "WCDMA";
3151
	$submodes[5] = "HSDPA";
3152
	$submodes[6] = "HSUPA";
3153 e313da37 smos
	$submodes[7] = "HSDPA+HSUPA";
3154 d23e157a smos
	$submodes[8] = "TD-SCDMA";
3155
	$submodes[9] = "HSPA+";
3156 e8c516a0 Phil Davis
	$string = "{$modes[$mode]}, {$submodes[$submode]} " . gettext("Mode");
3157 d23e157a smos
	return $string;
3158
}
3159
3160
function huawei_service_to_string($state) {
3161 e8c516a0 Phil Davis
	$modes[0] = gettext("No Service");
3162
	$modes[1] = gettext("Restricted Service");
3163
	$modes[2] = gettext("Valid Service");
3164
	$modes[3] = gettext("Restricted Regional Service");
3165
	$modes[4] = gettext("Powersaving Service");
3166 c6c55ee7 doktornotor
	$modes[255] = gettext("Unknown Service");
3167 e8c516a0 Phil Davis
	$string = $modes[$state];
3168 d23e157a smos
	return $string;
3169
}
3170
3171
function huawei_simstate_to_string($state) {
3172 e8c516a0 Phil Davis
	$modes[0] = gettext("Invalid SIM/locked State");
3173
	$modes[1] = gettext("Valid SIM State");
3174
	$modes[2] = gettext("Invalid SIM CS State");
3175
	$modes[3] = gettext("Invalid SIM PS State");
3176
	$modes[4] = gettext("Invalid SIM CS/PS State");
3177
	$modes[255] = gettext("Missing SIM State");
3178
	$string = $modes[$state];
3179 d23e157a smos
	return $string;
3180
}
3181 4adf752c smos
3182
function zte_rssi_to_string($rssi) {
3183
	return huawei_rssi_to_string($rssi);
3184
}
3185
3186
function zte_mode_to_string($mode, $submode) {
3187 e8c516a0 Phil Davis
	$modes[0] = gettext("No Service");
3188
	$modes[1] = gettext("Limited Service");
3189 4adf752c smos
	$modes[2] = "GPRS";
3190
	$modes[3] = "GSM";
3191
	$modes[4] = "UMTS";
3192
	$modes[5] = "EDGE";
3193 5fa78adc Renato Botelho
	$modes[6] = "HSDPA";
3194 4adf752c smos
3195
	$submodes[0] = "CS_ONLY";
3196
	$submodes[1] = "PS_ONLY";
3197
	$submodes[2] = "CS_PS";
3198
	$submodes[3] = "CAMPED";
3199 e8c516a0 Phil Davis
	$string = "{$modes[$mode]}, {$submodes[$submode]} " . gettext("Mode");
3200 4adf752c smos
	return $string;
3201
}
3202
3203 e8c516a0 Phil Davis
function zte_service_to_string($service) {
3204
	$modes[0] = gettext("Initializing Service");
3205
	$modes[1] = gettext("Network Lock error Service");
3206
	$modes[2] = gettext("Network Locked Service");
3207
	$modes[3] = gettext("Unlocked or correct MCC/MNC Service");
3208
	$string = $modes[$service];
3209 4adf752c smos
	return $string;
3210
}
3211
3212
function zte_simstate_to_string($state) {
3213 e8c516a0 Phil Davis
	$modes[0] = gettext("No action State");
3214
	$modes[1] = gettext("Network lock State");
3215
	$modes[2] = gettext("(U)SIM card lock State");
3216
	$modes[3] = gettext("Network Lock and (U)SIM card Lock State");
3217
	$string = $modes[$state];
3218 4adf752c smos
	return $string;
3219
}
3220 e9ab2ddb smos
3221
function get_configured_pppoe_server_interfaces() {
3222
	global $config;
3223
	$iflist = array();
3224
	if (is_array($config['pppoes']['pppoe'])) {
3225 23a193da Phil Davis
		foreach ($config['pppoes']['pppoe'] as $pppoe) {
3226 e9ab2ddb smos
			if ($pppoe['mode'] == "server") {
3227
				$int = "poes". $pppoe['pppoeid'];
3228
				$iflist[$int] = strtoupper($int);
3229
			}
3230
		}
3231
	}
3232
	return $iflist;
3233
}
3234
3235
function get_pppoes_child_interfaces($ifpattern) {
3236
	$if_arr = array();
3237 23a193da Phil Davis
	if ($ifpattern == "") {
3238 e9ab2ddb smos
		return;
3239 23a193da Phil Davis
	}
3240 e9ab2ddb smos
3241 84c82d3d doktornotor
	exec("/sbin/ifconfig", $out, $ret);
3242 23a193da Phil Davis
	foreach ($out as $line) {
3243
		if (preg_match("/^({$ifpattern}[0-9]+):/i", $line, $match)) {
3244 e9ab2ddb smos
			$if_arr[] = $match[1];
3245
		}
3246
	}
3247
	return $if_arr;
3248
3249
}
3250
3251 331166a8 PiBa-NL
/****f* pfsense-utils/pkg_call_plugins
3252
 * NAME
3253
 *   pkg_call_plugins
3254
 * INPUTS
3255
 *   $plugin_type value used to search in package configuration if the plugin is used, also used to create the function name
3256
 *   $plugin_params parameters to pass to the plugin function for passing multiple parameters a array can be used.
3257
 * RESULT
3258
 *   returns associative array results from the plugin calls for each package
3259
 * NOTES
3260
 *   This generic function can be used to notify or retrieve results from functions that are defined in packages.
3261
 ******/
3262
function pkg_call_plugins($plugin_type, $plugin_params) {
3263 eaee3af6 PiBa-NL
	global $g, $config;
3264
	$results = array();
3265 23a193da Phil Davis
	if (!is_array($config['installedpackages']['package'])) {
3266 331166a8 PiBa-NL
		return $results;
3267 23a193da Phil Davis
	}
3268 eaee3af6 PiBa-NL
	foreach ($config['installedpackages']['package'] as $package) {
3269 804fecdd PiBa-NL
		if (is_array($package['plugins']['item'])) {
3270
			foreach ($package['plugins']['item'] as $plugin) {
3271 331166a8 PiBa-NL
				if ($plugin['type'] == $plugin_type) {
3272 804fecdd PiBa-NL
					if (file_exists($package['include_file'])) {
3273
						require_once($package['include_file']);
3274 23a193da Phil Davis
					} else {
3275 eaee3af6 PiBa-NL
						continue;
3276 23a193da Phil Davis
					}
3277 804fecdd PiBa-NL
					$pkgname = substr(reverse_strrchr($package['configurationfile'], "."), 0, -1);
3278 eaee3af6 PiBa-NL
					$plugin_function = $pkgname . '_'. $plugin_type;
3279 c42117c1 PiBa-NL
					$results[$pkgname] = call_user_func($plugin_function, $plugin_params);
3280 eaee3af6 PiBa-NL
				}
3281
			}
3282 23a193da Phil Davis
		}
3283 eaee3af6 PiBa-NL
	}
3284
	return $results;
3285
}
3286
3287 f3997278 Steve Beaver
// Convert IPv6 addresses to lower case
3288
function addrtolower($ip) {
3289
	if (!filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) === false) {
3290
		return(strtolower($ip));
3291
	} else {
3292
		return($ip);
3293
	}
3294
}
3295 2c1a08a8 jim-p
3296
function compare_by_name($a, $b) {
3297
	return strcasecmp($a['name'], $b['name']);
3298
}
3299 58005e52 jim-p
?>