Project

General

Profile

Download (26.5 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
 * config.lib.inc
4
 *
5
 * part of pfSense (https://www.pfsense.org)
6
 * Copyright (c) 2004-2016 Rubicon Communications, LLC (Netgate)
7
 * Copyright (c) 2009 Erik Kristensen
8
 * All rights reserved.
9
 *
10
 * originally part of m0n0wall (http://m0n0.ch/wall)
11
 * Copyright (c) 2003-2004 Manuel Kasper <mk@neon1.net>.
12
 * All rights reserved.
13
 *
14
 * Licensed under the Apache License, Version 2.0 (the "License");
15
 * you may not use this file except in compliance with the License.
16
 * You may obtain a copy of the License at
17
 *
18
 * http://www.apache.org/licenses/LICENSE-2.0
19
 *
20
 * Unless required by applicable law or agreed to in writing, software
21
 * distributed under the License is distributed on an "AS IS" BASIS,
22
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
23
 * See the License for the specific language governing permissions and
24
 * limitations under the License.
25
 */
26

    
27
/****f* config/encrypted_configxml
28
 * NAME
29
 *   encrypted_configxml - Checks to see if config.xml is encrypted and if so, prompts to unlock.
30
 * INPUTS
31
 *   None
32
 * RESULT
33
 *   $config 	- rewrites config.xml without encryption
34
 ******/
35
function encrypted_configxml() {
36
	global $g, $config;
37

    
38
	if (!file_exists($g['conf_path'] . "/config.xml")) {
39
		return;
40
	}
41

    
42
	if (!platform_booting()) {
43
		return;
44
	}
45

    
46
	$configtxt = file_get_contents($g['conf_path'] . "/config.xml");
47
	if (tagfile_deformat($configtxt, $configtxt, "config.xml")) {
48
		$fp = fopen('php://stdin', 'r');
49
		$data = "";
50
		echo "\n\n*** Encrypted config.xml detected ***\n";
51
		while ($data == "") {
52
			echo "\nEnter the password to decrypt config.xml: ";
53
			$decrypt_password = chop(fgets($fp));
54
			$data = decrypt_data($configtxt, $decrypt_password);
55
			if (!strstr($data, "<pfsense>")) {
56
				$data = "";
57
			}
58
			if ($data) {
59
				$fd = fopen($g['conf_path'] . "/config.xml.tmp", "w");
60
				fwrite($fd, $data);
61
				fclose($fd);
62
				exec("/bin/mv {$g['conf_path']}/config.xml.tmp {$g['conf_path']}/config.xml");
63
				echo "\n" . gettext("Config.xml unlocked.") . "\n";
64
				fclose($fp);
65
				pfSense_fsync("{$g['conf_path']}/config.xml");
66
			} else {
67
				echo "\n" . gettext("Invalid password entered.  Please try again.") . "\n";
68
			}
69
		}
70
	}
71
}
72

    
73
/****f* config/parse_config
74
 * NAME
75
 *   parse_config - Read in config.cache or config.xml if needed and return $config array
76
 * INPUTS
77
 *   $parse       - boolean to force parse_config() to read config.xml and generate config.cache
78
 * RESULT
79
 *   $config      - array containing all configuration variables
80
 ******/
81
function parse_config($parse = false) {
82
	global $g, $config_parsed, $config_extra;
83

    
84
	$lockkey = lock('config');
85
	$config_parsed = false;
86

    
87
	if (!file_exists("{$g['conf_path']}/config.xml") || filesize("{$g['conf_path']}/config.xml") == 0) {
88
		$last_backup = discover_last_backup();
89
		if ($last_backup) {
90
			log_error(gettext("No config.xml found, attempting last known config restore."));
91
			file_notice("config.xml", gettext("No config.xml found, attempting last known config restore."), "pfSenseConfigurator", "");
92
			restore_backup("{$g['conf_path']}/backup/{$last_backup}");
93
		} else {
94
			unlock($lockkey);
95
			die(gettext("Config.xml is corrupted and is 0 bytes.  Could not restore a previous backup."));
96
		}
97
	}
98

    
99
	if (platform_booting(true)) {
100
		echo ".";
101
	}
102

    
103
	// Check for encrypted config.xml
104
	encrypted_configxml();
105

    
106
	if (!$parse) {
107
		if (file_exists($g['tmp_path'] . '/config.cache')) {
108
			$config = unserialize(file_get_contents($g['tmp_path'] . '/config.cache'));
109
			if (!is_array($config)) {
110
				$parse = true;
111
			}
112
		} else {
113
			$parse = true;
114
		}
115
	}
116
	if ($parse == true) {
117
		if (!file_exists($g['conf_path'] . "/config.xml")) {
118
			if (platform_booting(true)) {
119
				echo ".";
120
			}
121
			log_error("No config.xml found, attempting last known config restore.");
122
			file_notice("config.xml", "No config.xml found, attempting last known config restore.", "pfSenseConfigurator", "");
123
			$last_backup = discover_last_backup();
124
			if ($last_backup) {
125
				restore_backup("/cf/conf/backup/{$last_backup}");
126
			} else {
127
				log_error(gettext("Could not restore config.xml."));
128
				unlock($lockkey);
129
				die(gettext("Config.xml is corrupted and is 0 bytes.  Could not restore a previous backup."));
130
			}
131
		}
132
		$config = parse_xml_config($g['conf_path'] . '/config.xml', array($g['xml_rootobj'], 'pfsense'));
133
		if ($config == -1) {
134
			$last_backup = discover_last_backup();
135
			if ($last_backup) {
136
				restore_backup("/cf/conf/backup/{$last_backup}");
137
			} else {
138
				log_error(gettext("Could not restore config.xml."));
139
				unlock($lockkey);
140
				die("Config.xml is corrupted and is 0 bytes.  Could not restore a previous backup.");
141
			}
142
		}
143
		generate_config_cache($config);
144
	}
145

    
146
	if (platform_booting(true)) {
147
		echo ".";
148
	}
149

    
150
	$config_parsed = true;
151
	unlock($lockkey);
152

    
153
	alias_make_table($config);
154

    
155
	return $config;
156
}
157

    
158
/****f* config/generate_config_cache
159
 * NAME
160
 *   generate_config_cache - Write serialized configuration to cache.
161
 * INPUTS
162
 *   $config	- array containing current firewall configuration
163
 * RESULT
164
 *   boolean	- true on completion
165
 ******/
166
function generate_config_cache($config) {
167
	global $g, $config_extra;
168

    
169
	$configcache = fopen($g['tmp_path'] . '/config.cache', "w");
170
	fwrite($configcache, serialize($config));
171
	fclose($configcache);
172
	pfSense_fsync("{$g['tmp_path']}/config.cache");
173

    
174
	unset($configcache);
175
	/* Used for config.extra.xml */
176
	if (file_exists($g['tmp_path'] . '/config.extra.cache') && $config_extra) {
177
		$configcacheextra = fopen($g['tmp_path'] . '/config.extra.cache', "w");
178
		fwrite($configcacheextra, serialize($config_extra));
179
		fclose($configcacheextra);
180
		pfSense_fsync("{$g['tmp_path']}/config.extra.cache");
181
		unset($configcacheextra);
182
	}
183
}
184

    
185
function discover_last_backup() {
186
	$backups = glob('/cf/conf/backup/*.xml');
187
	$last_backup = "";
188
	$last_mtime = 0;
189
	foreach ($backups as $backup) {
190
		if (filemtime($backup) > $last_mtime) {
191
			$last_mtime = filemtime($backup);
192
			$last_backup = $backup;
193
		}
194
	}
195

    
196
	return basename($last_backup);
197
}
198

    
199
function restore_backup($file) {
200
	global $g;
201

    
202
	if (file_exists($file)) {
203
		unlink_if_exists("{$g['tmp_path']}/config.cache");
204
		copy("$file", "/cf/conf/config.xml");
205
		pfSense_fsync("/cf/conf/config.xml");
206
		pfSense_fsync($g['conf_path']);
207
		disable_security_checks();
208
		log_error(sprintf(gettext('%1$s is restoring the configuration %2$s'), $g['product_name'], $file));
209
		file_notice("config.xml", sprintf(gettext('%1$s is restoring the configuration %2$s'), $g['product_name'], $file), "pfSenseConfigurator", "");
210
	}
211
}
212

    
213
/****f* config/parse_config_bootup
214
 * NAME
215
 *   parse_config_bootup - Bootup-specific configuration checks.
216
 * RESULT
217
 *   null
218
 ******/
219
function parse_config_bootup() {
220
	global $config, $g;
221

    
222
	if (platform_booting()) {
223
		echo ".";
224
	}
225

    
226
	$lockkey = lock('config');
227
	if (!file_exists("{$g['conf_path']}/config.xml")) {
228
		if (platform_booting()) {
229
			$last_backup = discover_last_backup();
230
			if ($last_backup) {
231
				log_error("No config.xml found, attempting last known config restore.");
232
				file_notice("config.xml", gettext("No config.xml found, attempting last known config restore."), "pfSenseConfigurator", "");
233
				restore_backup("/cf/conf/backup/{$last_backup}");
234
			}
235
			if (!file_exists("{$g['conf_path']}/config.xml")) {
236
				echo sprintf(gettext("XML configuration file not found.  %s cannot continue booting."), $g['product_name']) . "\n";
237
				unlock($lockkey);
238
				mwexec("/sbin/halt");
239
				exit;
240
			}
241
			log_error("Last known config found and restored.  Please double check the configuration file for accuracy.");
242
			file_notice("config.xml", gettext("Last known config found and restored.  Please double check the configuration file for accuracy."), "pfSenseConfigurator", "");
243
		} else {
244
			unlock($lockkey);
245
			log_error(gettext("Could not find a usable configuration file! Exiting...."));
246
			exit(0);
247
		}
248
	}
249

    
250
	if (filesize("{$g['conf_path']}/config.xml") == 0) {
251
		$last_backup = discover_last_backup();
252
		if ($last_backup) {
253
			log_error(gettext("No config.xml found, attempting last known config restore."));
254
			file_notice("config.xml", gettext("No config.xml found, attempting last known config restore."), "pfSenseConfigurator", "");
255
			restore_backup("{$g['conf_path']}/backup/{$last_backup}");
256
		} else {
257
			unlock($lockkey);
258
			die(gettext("Config.xml is corrupted and is 0 bytes.  Could not restore a previous backup."));
259
		}
260
	}
261
	unlock($lockkey);
262

    
263
	$config = parse_config(true);
264

    
265
	if ((float)$config['version'] > (float)$g['latest_config']) {
266
		echo <<<EOD
267

    
268

    
269
*******************************************************************************
270
* WARNING!                                                                    *
271
* The current configuration has been created with a newer version of {$g['product_name']}  *
272
* than this one! This can lead to serious misbehavior and even security       *
273
* holes! You are urged to either upgrade to a newer version of {$g['product_name']} or     *
274
* revert to the default configuration immediately!                            *
275
*******************************************************************************
276

    
277

    
278
EOD;
279
		}
280

    
281
	/* make alias table (for faster lookups) */
282
	alias_make_table($config);
283
}
284

    
285
/****f* config/conf_mount_rw
286
 * NAME
287
 *   conf_mount_rw - Mount filesystems read/write.
288
 * RESULT
289
 *   null
290
 ******/
291
/* mount flash card read/write */
292
function conf_mount_rw() {
293
	/* Obsoleted. Keep it here untill all calls are removed */
294
	return;
295
}
296

    
297
/****f* config/conf_mount_ro
298
 * NAME
299
 *   conf_mount_ro - Mount filesystems readonly.
300
 * RESULT
301
 *   null
302
 ******/
303
function conf_mount_ro() {
304
	/* Obsoleted. Keep it here untill all calls are removed */
305
	return;
306
}
307

    
308
/****f* config/convert_config
309
 * NAME
310
 *   convert_config - Attempt to update config.xml.
311
 * DESCRIPTION
312
 *   convert_config() reads the current global configuration
313
 *   and attempts to convert it to conform to the latest
314
 *   config.xml version. This allows major formatting changes
315
 *   to be made with a minimum of breakage.
316
 * RESULT
317
 *   null
318
 ******/
319
/* convert configuration, if necessary */
320
function convert_config() {
321
	global $config, $g;
322
	$now = date("H:i:s");
323
	log_error(sprintf(gettext("Start Configuration upgrade at %s, set execution timeout to 15 minutes"), $now));
324
	//ini_set("max_execution_time", "900");
325

    
326
	/* special case upgrades */
327
	/* fix every minute crontab bogons entry */
328
	if (is_array($config['cron'])) {
329
		$cron_item_count = count($config['cron']['item']);
330
		for ($x = 0; $x < $cron_item_count; $x++) {
331
			if (stristr($config['cron']['item'][$x]['command'], "rc.update_bogons.sh")) {
332
				if ($config['cron']['item'][$x]['hour'] == "*") {
333
					$config['cron']['item'][$x]['hour'] = "3";
334
					write_config(gettext("Updated bogon update frequency to 3am"));
335
					log_error(gettext("Updated bogon update frequency to 3am"));
336
				}
337
			}
338
		}
339
	}
340
	if ($config['version'] == $g['latest_config']) {
341
		return;		/* already at latest version */
342
	}
343

    
344
	// Save off config version
345
	$prev_version = $config['version'];
346

    
347
	include_once('auth.inc');
348
	include_once('upgrade_config.inc');
349
	if (file_exists("/etc/inc/upgrade_config_custom.inc")) {
350
		include_once("upgrade_config_custom.inc");
351
	}
352
	/* Loop and run upgrade_VER_to_VER() until we're at current version */
353
	while ($config['version'] < $g['latest_config']) {
354
		$cur = $config['version'] * 10;
355
		$next = $cur + 1;
356
		$migration_function = sprintf('upgrade_%03d_to_%03d', $cur, $next);
357
		if (function_exists($migration_function)) {
358
			$migration_function();
359
		}
360
		$migration_function = "{$migration_function}_custom";
361
		if (function_exists($migration_function)) {
362
			$migration_function();
363
		}
364
		$config['version'] = sprintf('%.1f', $next / 10);
365
		if (platform_booting()) {
366
			echo ".";
367
		}
368
	}
369

    
370
	$now = date("H:i:s");
371
	log_error(sprintf(gettext("Ended Configuration upgrade at %s"), $now));
372

    
373
	if ($prev_version != $config['version']) {
374
		write_config(sprintf(gettext('Upgraded config version level from %1$s to %2$s'), $prev_version, $config['version']));
375
	}
376
}
377

    
378
/****f* config/safe_write_file
379
 * NAME
380
 *   safe_write_file - Write a file out atomically
381
 * DESCRIPTION
382
 *   safe_write_file() Writes a file out atomically by first writing to a
383
 *   temporary file of the same name but ending with the pid of the current
384
 *   process, them renaming the temporary file over the original.
385
 * INPUTS
386
 *   $filename  - string containing the filename of the file to write
387
 *   $content   - string or array containing the file content to write to file
388
 *   $force_binary      - boolean denoting whether we should force binary
389
 *   mode writing.
390
 * RESULT
391
 *   boolean - true if successful, false if not
392
 ******/
393
function safe_write_file($file, $content, $force_binary = false) {
394
	$tmp_file = $file . "." . getmypid();
395
	$write_mode = $force_binary ? "wb" : "w";
396

    
397
	$fd = fopen($tmp_file, $write_mode);
398
	if (!$fd) {
399
		// Unable to open temporary file for writing
400
		return false;
401
	}
402
	if (is_array($content)) {
403
		foreach ($content as $line) {
404
			if (!fwrite($fd, $line . "\n")) {
405
				// Unable to write to temporary file
406
				fclose($fd);
407
				return false;
408
			}
409
		}
410
	} elseif (!fwrite($fd, $content)) {
411
		// Unable to write to temporary file
412
		fclose($fd);
413
		return false;
414
	}
415
	fflush($fd);
416
	fclose($fd);
417

    
418
	if (!pfSense_fsync($tmp_file) || !rename($tmp_file, $file)) {
419
		// Unable to move temporary file to original
420
		@unlink($tmp_file);
421
		return false;
422
	}
423

    
424
	// Sync file before returning
425
	return pfSense_fsync($file);
426
}
427

    
428
/****f* config/write_config
429
 * NAME
430
 *   write_config - Backup and write the firewall configuration.
431
 * DESCRIPTION
432
 *   write_config() handles backing up the current configuration,
433
 *   applying changes, and regenerating the configuration cache.
434
 * INPUTS
435
 *   $desc	- string containing the a description of configuration changes
436
 *   $backup	- boolean: do not back up current configuration if false.
437
 *   $write_config_only	- boolean: do not sync or reload anything; just save the configuration if true.
438
 * RESULT
439
 *   null
440
 ******/
441
/* save the system configuration */
442
function write_config($desc="Unknown", $backup = true, $write_config_only = false) {
443
	global $config, $g;
444

    
445
	if (!empty($_SERVER['REMOTE_ADDR'])) {
446
		if (!session_id()) {
447
			@session_start();
448
		}
449
		if (!empty($_SESSION['Username']) && ($_SESSION['Username'] != "admin")) {
450
			$user = getUserEntry($_SESSION['Username']);
451
			if (is_array($user) && userHasPrivilege($user, "user-config-readonly")) {
452
				session_commit();
453
				return false;
454
			}
455
		}
456
	}
457

    
458
	if (!isset($argc)) {
459
		session_commit();
460
	}
461

    
462
	if ($backup) {
463
		backup_config();
464
	}
465

    
466
	$config['revision'] = make_config_revision_entry($desc);
467

    
468
	$lockkey = lock('config', LOCK_EX);
469

    
470
	/* generate configuration XML */
471
	$xmlconfig = dump_xml_config($config, $g['xml_rootobj']);
472

    
473
	/* write new configuration */
474
	if (!safe_write_file("{$g['cf_conf_path']}/config.xml", $xmlconfig)) {
475
		log_error(gettext("WARNING: Config contents could not be saved. Could not open file!"));
476
		unlock($lockkey);
477
		file_notice("config.xml", sprintf(gettext("Unable to open %s/config.xml for writing in write_config()%s"), $g['cf_conf_path'], "\n"));
478
		return -1;
479
	}
480

    
481
	cleanup_backupcache(true);
482

    
483
	/* re-read configuration */
484
	/* NOTE: We assume that the file can be parsed since we wrote it. */
485
	$config = parse_xml_config("{$g['conf_path']}/config.xml", $g['xml_rootobj']);
486
	if ($config == -1) {
487
		copy("{$g['conf_path']}/config.xml", "{$g['conf_path']}/config.xml.bad");
488
		$last_backup = discover_last_backup();
489
		if ($last_backup) {
490
			restore_backup("/cf/conf/backup/{$last_backup}");
491
			$config = parse_xml_config("{$g['conf_path']}/config.xml", $g['xml_rootobj']);
492
			if (platform_booting()) {
493
				echo "\n\n ************** WARNING **************";
494
				echo "\n\n Configuration could not be validated. A previous configuration was restored. \n";
495
				echo "\n The failed configuration file has been saved as {$g['conf_path']}/config.xml.bad \n\n";
496
			}
497
		} else {
498
			log_error(gettext("Could not restore config.xml."));
499
		}
500
	} else {
501
		generate_config_cache($config);
502
	}
503

    
504
	unlock($lockkey);
505

    
506
	if ($write_config_only) {
507
		return $config;
508
	}
509

    
510
	unlink_if_exists("/usr/local/pkg/pf/carp_sync_client.php");
511

    
512
	/* sync carp entries to other firewalls */
513
	carp_sync_client();
514

    
515
	if (is_dir("/usr/local/pkg/write_config")) {
516
		/* process packager manager custom rules */
517
		run_plugins("/usr/local/pkg/write_config/");
518
	}
519

    
520
	return $config;
521
}
522

    
523
/****f* config/reset_factory_defaults
524
 * NAME
525
 *   reset_factory_defaults - Reset the system to its default configuration.
526
 * RESULT
527
 *   integer	- indicates completion
528
 ******/
529
function reset_factory_defaults($lock = false, $reboot_required = true) {
530
	global $g;
531

    
532

    
533
	/* Remove all additional packages */
534
	mwexec("/bin/sh /usr/local/sbin/{$g['product_name']}-upgrade " .
535
	    "-r ALL_PACKAGES");
536

    
537
	if (!$lock) {
538
		$lockkey = lock('config', LOCK_EX);
539
	}
540

    
541
	/* create conf directory, if necessary */
542
	safe_mkdir($g['cf_conf_path']);
543

    
544
	/* clear out /conf */
545
	$dh = opendir($g['conf_path']);
546
	while ($filename = readdir($dh)) {
547
		if (($filename != ".") && ($filename != "..") &&
548
		    (!is_dir($g['conf_path'] . "/" . $filename))) {
549
			unlink_if_exists($g['conf_path'] . "/" . $filename);
550
		}
551
	}
552
	closedir($dh);
553
	unlink_if_exists($g['tmp_path'] . "/config.cache");
554

    
555
	/* copy default configuration */
556
	copy("{$g['conf_default_path']}/config.xml",
557
	    "{$g['cf_conf_path']}/config.xml");
558

    
559
	disable_security_checks();
560

    
561
	/* call the wizard */
562
	if ($reboot_required) {
563
		// If we need a reboot first then touch a different trigger file.
564
		touch("/conf/trigger_initial_wizard_after_reboot");
565
	} else {
566
		touch("/conf/trigger_initial_wizard");
567
	}
568
	if (!$lock) {
569
		unlock($lockkey);
570
	}
571
	setup_serial_port();
572
	return 0;
573
}
574

    
575
function config_restore($conffile) {
576
	global $config, $g;
577

    
578
	if (!file_exists($conffile)) {
579
		return 1;
580
	}
581

    
582
	backup_config();
583

    
584

    
585
	$lockkey = lock('config', LOCK_EX);
586

    
587
	unlink_if_exists("{$g['tmp_path']}/config.cache");
588
	copy($conffile, "{$g['cf_conf_path']}/config.xml");
589

    
590
	disable_security_checks();
591

    
592
	unlock($lockkey);
593

    
594
	$config = parse_config(true);
595

    
596

    
597
	write_config(sprintf(gettext("Reverted to %s."), array_pop(explode("/", $conffile))), false);
598

    
599
	return 0;
600
}
601

    
602
function config_install($conffile) {
603
	global $config, $g;
604

    
605
	if (!file_exists($conffile)) {
606
		return 1;
607
	}
608

    
609
	if (!config_validate("{$conffile}")) {
610
		return 1;
611
	}
612

    
613
	if (platform_booting()) {
614
		echo gettext("Installing configuration...") . "\n";
615
	} else {
616
		log_error(gettext("Installing configuration ...."));
617
	}
618

    
619
	$lockkey = lock('config', LOCK_EX);
620

    
621
	copy($conffile, "{$g['conf_path']}/config.xml");
622

    
623
	disable_security_checks();
624

    
625
	/* unlink cache file if it exists */
626
	if (file_exists("{$g['tmp_path']}/config.cache")) {
627
		unlink("{$g['tmp_path']}/config.cache");
628
	}
629

    
630
	unlock($lockkey);
631

    
632
	return 0;
633
}
634

    
635
/*
636
 * Disable security checks for DNS rebind and HTTP referrer until next time
637
 * they pass (or reboot), to aid in preventing accidental lockout when
638
 * restoring settings like hostname, domain, IP addresses, and settings
639
 * related to the DNS rebind and HTTP referrer checks.
640
 * Intended for use when restoring a configuration or directly
641
 * modifying config.xml without an unconditional reboot.
642
 */
643
function disable_security_checks() {
644
	global $g;
645
	touch("{$g['tmp_path']}/disable_security_checks");
646
}
647

    
648
/* Restores security checks.  Should be called after all succeed. */
649
function restore_security_checks() {
650
	global $g;
651
	unlink_if_exists("{$g['tmp_path']}/disable_security_checks");
652
}
653

    
654
/* Returns status of security check temporary disable. */
655
function security_checks_disabled() {
656
	global $g;
657
	return file_exists("{$g['tmp_path']}/disable_security_checks");
658
}
659

    
660
function config_validate($conffile) {
661

    
662
	global $g, $xmlerr;
663

    
664
	$xml_parser = xml_parser_create();
665

    
666
	if (!($fp = fopen($conffile, "r"))) {
667
		$xmlerr = gettext("XML error: unable to open file");
668
		return false;
669
	}
670

    
671
	while ($data = fread($fp, 4096)) {
672
		if (!xml_parse($xml_parser, $data, feof($fp))) {
673
			$xmlerr = sprintf(gettext('%1$s at line %2$d'),
674
						xml_error_string(xml_get_error_code($xml_parser)),
675
						xml_get_current_line_number($xml_parser));
676
			return false;
677
		}
678
	}
679
	xml_parser_free($xml_parser);
680

    
681
	fclose($fp);
682

    
683
	return true;
684
}
685

    
686
function cleanup_backupcache($lock = false) {
687
	global $config, $g;
688
	$i = false;
689

    
690
	$revisions = intval(is_numericint($config['system']['backupcount']) ? $config['system']['backupcount'] : $g['default_config_backup_count']);
691

    
692
	if (!$lock) {
693
		$lockkey = lock('config');
694
	}
695

    
696

    
697
	$backups = get_backups();
698
	if ($backups) {
699
		$baktimes = $backups['versions'];
700
		unset($backups['versions']);
701
	} else {
702
		$backups = array();
703
		$baktimes = array();
704
	}
705
	$newbaks = array();
706
	$bakfiles = glob($g['cf_conf_path'] . "/backup/config-*");
707
	$tocache = array();
708

    
709
	foreach ($bakfiles as $backup) { // Check for backups in the directory not represented in the cache.
710
		$backupsize = filesize($backup);
711
		if ($backupsize == 0) {
712
			unlink($backup);
713
			continue;
714
		}
715
		$backupexp = explode('-', $backup);
716
		$backupexp = explode('.', array_pop($backupexp));
717
		$tocheck = array_shift($backupexp);
718
		unset($backupexp);
719
		if (!in_array($tocheck, $baktimes)) {
720
			$i = true;
721
			if (platform_booting()) {
722
				echo ".";
723
			}
724
			$newxml = parse_xml_config($backup, array($g['xml_rootobj'], 'pfsense'));
725
			if ($newxml == "-1") {
726
				log_error(sprintf(gettext("The backup cache file %s is corrupted.  Unlinking."), $backup));
727
				unlink($backup);
728
				log_error(sprintf(gettext("The backup cache file %s is corrupted.  Unlinking."), $backup));
729
				continue;
730
			}
731
			if ($newxml['revision']['description'] == "") {
732
				$newxml['revision']['description'] = "Unknown";
733
			}
734
			if ($newxml['version'] == "") {
735
				$newxml['version'] = "?";
736
			}
737
			$tocache[$tocheck] = array('description' => $newxml['revision']['description'], 'version' => $newxml['version'], 'filesize' => $backupsize);
738
		}
739
	}
740
	foreach ($backups as $checkbak) {
741
		if (count(preg_grep('/' . $checkbak['time'] . '/i', $bakfiles)) != 0) {
742
			$newbaks[] = $checkbak;
743
		} else {
744
			$i = true;
745
			if (platform_booting()) print " " . $tocheck . "r";
746
		}
747
	}
748
	foreach ($newbaks as $todo) {
749
		$tocache[$todo['time']] = array('description' => $todo['description'], 'version' => $todo['version'], 'filesize' => $todo['filesize']);
750
	}
751
	if (is_int($revisions) and (count($tocache) > $revisions)) {
752
		$toslice = array_slice(array_keys($tocache), 0, $revisions);
753
		foreach ($toslice as $sliced) {
754
			$newcache[$sliced] = $tocache[$sliced];
755
		}
756
		foreach ($tocache as $version => $versioninfo) {
757
			if (!in_array($version, array_keys($newcache))) {
758
				unlink_if_exists($g['conf_path'] . '/backup/config-' . $version . '.xml');
759
			}
760
		}
761
		$tocache = $newcache;
762
	}
763
	$bakout = fopen($g['cf_conf_path'] . '/backup/backup.cache', "w");
764
	fwrite($bakout, serialize($tocache));
765
	fclose($bakout);
766
	pfSense_fsync("{$g['cf_conf_path']}/backup/backup.cache");
767

    
768
	if (!$lock) {
769
		unlock($lockkey);
770
	}
771
}
772

    
773
function get_backups() {
774
	global $g;
775
	if (file_exists("{$g['cf_conf_path']}/backup/backup.cache")) {
776
		$confvers = unserialize(file_get_contents("{$g['cf_conf_path']}/backup/backup.cache"));
777
		$bakvers = array_keys($confvers);
778
		$toreturn = array();
779
		sort($bakvers);
780
		// 	$bakvers = array_reverse($bakvers);
781
		foreach (array_reverse($bakvers) as $bakver) {
782
			$toreturn[] = array('time' => $bakver, 'description' => $confvers[$bakver]['description'], 'version' => $confvers[$bakver]['version'], 'filesize' => $confvers[$bakver]['filesize']);
783
		}
784
	} else {
785
		return false;
786
	}
787
	$toreturn['versions'] = $bakvers;
788
	return $toreturn;
789
}
790

    
791
function backup_config() {
792
	global $config, $g;
793

    
794

    
795
	/* Create backup directory if needed */
796
	safe_mkdir("{$g['cf_conf_path']}/backup");
797
	if ($config['revision']['time'] == "") {
798
		$baktime = 0;
799
	} else {
800
		$baktime = $config['revision']['time'];
801
	}
802

    
803
	if ($config['revision']['description'] == "") {
804
		$bakdesc = "Unknown";
805
	} else {
806
		$bakdesc = $config['revision']['description'];
807
	}
808

    
809
	$bakver = ($config['version'] == "") ? "?" : $config['version'];
810
	$bakfilename = $g['cf_conf_path'] . '/backup/config-' . $baktime . '.xml';
811
	copy($g['cf_conf_path'] . '/config.xml', $bakfilename);
812

    
813
	if (file_exists($g['cf_conf_path'] . '/backup/backup.cache')) {
814
		$backupcache = unserialize(file_get_contents($g['cf_conf_path'] . '/backup/backup.cache'));
815
	} else {
816
		$backupcache = array();
817
	}
818
	$backupcache[$baktime] = array('description' => $bakdesc, 'version' => $bakver, 'filesize' => filesize($bakfilename));
819
	$bakout = fopen($g['cf_conf_path'] . '/backup/backup.cache', "w");
820
	fwrite($bakout, serialize($backupcache));
821
	fclose($bakout);
822
	pfSense_fsync("{$g['cf_conf_path']}/backup/backup.cache");
823

    
824

    
825
	return true;
826
}
827

    
828
function set_device_perms() {
829
	$devices = array(
830
		'pf' => array(
831
			'user' => 'root',
832
			'group' => 'proxy',
833
			'mode' => 0660),
834
		);
835

    
836
	foreach ($devices as $name => $attr) {
837
		$path = "/dev/$name";
838
		if (file_exists($path)) {
839
			chown($path, $attr['user']);
840
			chgrp($path, $attr['group']);
841
			chmod($path, $attr['mode']);
842
		}
843
	}
844
}
845

    
846
function get_config_user() {
847
	if (empty($_SESSION["Username"])) {
848
		$username = getenv("USER");
849
		if (empty($conuser) || $conuser == "root") {
850
			$username = "(system)";
851
		}
852
	} else {
853
		$username = $_SESSION["Username"];
854
	}
855

    
856
	if (!empty($_SERVER['REMOTE_ADDR'])) {
857
		$username .= '@' . $_SERVER['REMOTE_ADDR'];
858
	}
859

    
860
	return $username;
861
}
862

    
863
function make_config_revision_entry($desc = null, $override_user = null) {
864
	if (empty($override_user)) {
865
		$username = get_config_user();
866
	} else {
867
		$username = $override_user;
868
	}
869

    
870
	$revision = array();
871

    
872
	if (time() > mktime(0, 0, 0, 9, 1, 2004)) {     /* make sure the clock settings are plausible */
873
		$revision['time'] = time();
874
	}
875

    
876
	/* Log the running script so it's not entirely unlogged what changed */
877
	if ($desc == "Unknown") {
878
		$desc = sprintf(gettext("%s made unknown change"), $_SERVER['SCRIPT_NAME']);
879
	}
880
	if (!empty($desc)) {
881
		$revision['description'] = "{$username}: " . $desc;
882
	}
883
	$revision['username'] = $username;
884
	return $revision;
885
}
886

    
887
function pfSense_clear_globals() {
888
	global $config, $FilterIfList, $GatewaysList, $filterdns, $aliases, $aliastable;
889

    
890
	$error = error_get_last();
891

    
892
	if ($error !== NULL) {
893
		if (in_array($error['type'], array(E_ERROR, E_COMPILE_ERROR, E_CORE_ERROR, E_RECOVERABLE_ERROR))) {
894
			$errorstr = "PHP ERROR: Type: {$error['type']}, File: {$error['file']}, Line: {$error['line']}, Message: {$error['message']}";
895
			print($errorstr);
896
			log_error($errorstr);
897
			file_notice("phperror", $errorstr, 'PHP errors');
898
		} else if ($error['type'] != E_NOTICE) {
899
			$errorstr = "PHP WARNING: Type: {$error['type']}, File: {$error['file']}, Line: {$error['line']}, Message: {$error['message']}";
900
			// XXX: comment out for now, should re-enable post-2.2
901
			//print($errorstr);
902
			//log_error($errorstr);
903
			//file_notice("phpwarning", $errorstr, 'PHP warning');
904
		}
905
	}
906

    
907
	if (isset($FilterIfList)) {
908
		unset($FilterIfList);
909
	}
910

    
911
	if (isset($GatewaysList)) {
912
		unset($GatewaysList);
913
	}
914

    
915
	/* Used for the hostname dns resolver */
916
	if (isset($filterdns)) {
917
		unset($filterdns);
918
	}
919

    
920
	/* Used for aliases and interface macros */
921
	if (isset($aliases)) {
922
		unset($aliases);
923
	}
924
	if (isset($aliastable)) {
925
		unset($aliastable);
926
	}
927

    
928
	unset($config);
929
}
930

    
931
register_shutdown_function('pfSense_clear_globals');
932

    
933
?>
(8-8/51)