Project

General

Profile

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