Project

General

Profile

Download (28.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 Electric Sheep Fencing, LLC
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
		conf_mount_rw();
204
		unlink_if_exists("{$g['tmp_path']}/config.cache");
205
		copy("$file", "/cf/conf/config.xml");
206
		pfSense_fsync("/cf/conf/config.xml");
207
		pfSense_fsync($g['conf_path']);
208
		disable_security_checks();
209
		log_error(sprintf(gettext('%1$s is restoring the configuration %2$s'), $g['product_name'], $file));
210
		file_notice("config.xml", sprintf(gettext('%1$s is restoring the configuration %2$s'), $g['product_name'], $file), "pfSenseConfigurator", "");
211
		conf_mount_ro();
212
	}
213
}
214

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

    
224
	if (platform_booting()) {
225
		echo ".";
226
	}
227

    
228
	$lockkey = lock('config');
229
	if (!file_exists("{$g['conf_path']}/config.xml")) {
230
		if (platform_booting()) {
231
			if (strstr($g['platform'], "cdrom")) {
232
				/* try copying the default config. to the floppy */
233
				echo gettext("Resetting factory defaults...") . "\n";
234
				reset_factory_defaults(true, false);
235
				if (!file_exists("{$g['conf_path']}/config.xml")) {
236
					echo gettext("No XML configuration file found - using factory defaults.\n" .
237
								 "Make sure that the configuration floppy disk with the conf/config.xml\n" .
238
								 "file is inserted. If it isn't, the configuration changes will be lost\n" .
239
								 "on reboot.\n");
240
				}
241
			} else {
242
				$last_backup = discover_last_backup();
243
				if ($last_backup) {
244
					log_error("No config.xml found, attempting last known config restore.");
245
					file_notice("config.xml", gettext("No config.xml found, attempting last known config restore."), "pfSenseConfigurator", "");
246
					restore_backup("/cf/conf/backup/{$last_backup}");
247
				}
248
				if (!file_exists("{$g['conf_path']}/config.xml")) {
249
					echo sprintf(gettext("XML configuration file not found.  %s cannot continue booting."), $g['product_name']) . "\n";
250
					unlock($lockkey);
251
					mwexec("/sbin/halt");
252
					exit;
253
				}
254
				log_error("Last known config found and restored.  Please double check the configuration file for accuracy.");
255
				file_notice("config.xml", gettext("Last known config found and restored.  Please double check the configuration file for accuracy."), "pfSenseConfigurator", "");
256
			}
257
		} else {
258
			unlock($lockkey);
259
			log_error(gettext("Could not find a usable configuration file! Exiting...."));
260
			exit(0);
261
		}
262
	}
263

    
264
	if (filesize("{$g['conf_path']}/config.xml") == 0) {
265
		$last_backup = discover_last_backup();
266
		if ($last_backup) {
267
			log_error(gettext("No config.xml found, attempting last known config restore."));
268
			file_notice("config.xml", gettext("No config.xml found, attempting last known config restore."), "pfSenseConfigurator", "");
269
			restore_backup("{$g['conf_path']}/backup/{$last_backup}");
270
		} else {
271
			unlock($lockkey);
272
			die(gettext("Config.xml is corrupted and is 0 bytes.  Could not restore a previous backup."));
273
		}
274
	}
275
	unlock($lockkey);
276

    
277
	$config = parse_config(true);
278

    
279
	if ((float)$config['version'] > (float)$g['latest_config']) {
280
		echo <<<EOD
281

    
282

    
283
*******************************************************************************
284
* WARNING!                                                                    *
285
* The current configuration has been created with a newer version of {$g['product_name']}  *
286
* than this one! This can lead to serious misbehavior and even security       *
287
* holes! You are urged to either upgrade to a newer version of {$g['product_name']} or     *
288
* revert to the default configuration immediately!                            *
289
*******************************************************************************
290

    
291

    
292
EOD;
293
		}
294

    
295
	/* make alias table (for faster lookups) */
296
	alias_make_table($config);
297
}
298

    
299
/****f* config/conf_mount_rw
300
 * NAME
301
 *   conf_mount_rw - Mount filesystems read/write.
302
 * RESULT
303
 *   null
304
 ******/
305
/* mount flash card read/write */
306
function conf_mount_rw() {
307
	global $g, $config;
308

    
309
	/* do not mount on cdrom platform */
310
	if ($g['platform'] == "cdrom" or $g['platform'] == $g['product_name']) {
311
		return;
312
	}
313

    
314
	if ((refcount_reference(1000) > 1) && is_writable("/")) {
315
		return;
316
	}
317

    
318
	$status = mwexec("/sbin/mount -u -w -o sync,noatime {$g['cf_path']}");
319
	if ($status <> 0) {
320
		if (platform_booting()) {
321
			echo gettext("/cf Filesystem is dirty.") . "\n";
322
		}
323
		$status = mwexec("/sbin/mount -u -w -o sync,noatime {$g['cf_path']}");
324
	}
325

    
326
	/*    if the platform is soekris or wrap or $product, lets mount the
327
	 *    compact flash cards root.
328
	*/
329
	$status = mwexec("/sbin/mount -u -w -o sync,noatime /");
330
	/* we could not mount this correctly. */
331
	if ($status <> 0) {
332
		log_error(gettext("/ File system is dirty."));
333
		$status = mwexec("/sbin/mount -u -w -o sync,noatime /");
334
	}
335

    
336
	mark_subsystem_dirty('mount');
337
}
338

    
339
/****f* config/conf_mount_ro
340
 * NAME
341
 *   conf_mount_ro - Mount filesystems readonly.
342
 * RESULT
343
 *   null
344
 ******/
345
function conf_mount_ro() {
346
	global $g, $config;
347

    
348
	// do nothing here. redmine #6184
349
	return;
350

    
351
	/* Do not trust $g['platform'] since this can be clobbered during factory reset. */
352
	$platform = trim(file_get_contents("/etc/platform"));
353
	/* do not umount on cdrom or pfSense platforms */
354
	if ($platform == "cdrom" or $platform == $g['product_name']) {
355
		return;
356
	}
357

    
358
	if (refcount_unreference(1000) > 0) {
359
		return;
360
	}
361

    
362
	if (isset($config['system']['nanobsd_force_rw'])) {
363
		return;
364
	}
365

    
366
	if (platform_booting()) {
367
		return;
368
	}
369

    
370
	clear_subsystem_dirty('mount');
371
	/* sync data, then force a remount of /cf */
372
	pfSense_fsync($g['cf_path']);
373
	mwexec("/sbin/mount -u -r -f -o sync,noatime {$g['cf_path']}");
374
	mwexec("/sbin/mount -u -r -f -o sync,noatime /");
375
}
376

    
377
/****f* config/convert_config
378
 * NAME
379
 *   convert_config - Attempt to update config.xml.
380
 * DESCRIPTION
381
 *   convert_config() reads the current global configuration
382
 *   and attempts to convert it to conform to the latest
383
 *   config.xml version. This allows major formatting changes
384
 *   to be made with a minimum of breakage.
385
 * RESULT
386
 *   null
387
 ******/
388
/* convert configuration, if necessary */
389
function convert_config() {
390
	global $config, $g;
391
	$now = date("H:i:s");
392
	log_error(sprintf(gettext("Start Configuration upgrade at %s, set execution timeout to 15 minutes"), $now));
393
	//ini_set("max_execution_time", "900");
394

    
395
	/* special case upgrades */
396
	/* fix every minute crontab bogons entry */
397
	if (is_array($config['cron'])) {
398
		$cron_item_count = count($config['cron']['item']);
399
		for ($x = 0; $x < $cron_item_count; $x++) {
400
			if (stristr($config['cron']['item'][$x]['command'], "rc.update_bogons.sh")) {
401
				if ($config['cron']['item'][$x]['hour'] == "*") {
402
					$config['cron']['item'][$x]['hour'] = "3";
403
					write_config(gettext("Updated bogon update frequency to 3am"));
404
					log_error(gettext("Updated bogon update frequency to 3am"));
405
				}
406
			}
407
		}
408
	}
409
	if ($config['version'] == $g['latest_config']) {
410
		return;		/* already at latest version */
411
	}
412

    
413
	// Save off config version
414
	$prev_version = $config['version'];
415

    
416
	include_once('auth.inc');
417
	include_once('upgrade_config.inc');
418
	if (file_exists("/etc/inc/upgrade_config_custom.inc")) {
419
		include_once("upgrade_config_custom.inc");
420
	}
421
	/* Loop and run upgrade_VER_to_VER() until we're at current version */
422
	while ($config['version'] < $g['latest_config']) {
423
		$cur = $config['version'] * 10;
424
		$next = $cur + 1;
425
		$migration_function = sprintf('upgrade_%03d_to_%03d', $cur, $next);
426
		if (function_exists($migration_function)) {
427
			$migration_function();
428
		}
429
		$migration_function = "{$migration_function}_custom";
430
		if (function_exists($migration_function)) {
431
			$migration_function();
432
		}
433
		$config['version'] = sprintf('%.1f', $next / 10);
434
		if (platform_booting()) {
435
			echo ".";
436
		}
437
	}
438

    
439
	$now = date("H:i:s");
440
	log_error(sprintf(gettext("Ended Configuration upgrade at %s"), $now));
441

    
442
	if ($prev_version != $config['version']) {
443
		write_config(sprintf(gettext('Upgraded config version level from %1$s to %2$s'), $prev_version, $config['version']));
444
	}
445
}
446

    
447
/****f* config/safe_write_file
448
 * NAME
449
 *   safe_write_file - Write a file out atomically
450
 * DESCRIPTION
451
 *   safe_write_file() Writes a file out atomically by first writing to a
452
 *   temporary file of the same name but ending with the pid of the current
453
 *   process, them renaming the temporary file over the original.
454
 * INPUTS
455
 *   $filename  - string containing the filename of the file to write
456
 *   $content   - string containing the file content to write to file
457
 *   $force_binary      - boolean denoting whether we should force binary
458
 *   mode writing.
459
 * RESULT
460
 *   boolean - true if successful, false if not
461
 ******/
462
function safe_write_file($file, $content, $force_binary) {
463
	$tmp_file = $file . "." . getmypid();
464
	$write_mode = $force_binary ? "wb" : "w";
465

    
466
	$fd = fopen($tmp_file, $write_mode);
467
	if (!$fd) {
468
		// Unable to open temporary file for writing
469
		return false;
470
	}
471
	if (!fwrite($fd, $content)) {
472
		// Unable to write to temporary file
473
		fclose($fd);
474
		return false;
475
	}
476
	fflush($fd);
477
	fclose($fd);
478

    
479
	if (!pfSense_fsync($tmp_file) || !rename($tmp_file, $file)) {
480
		// Unable to move temporary file to original
481
		@unlink($tmp_file);
482
		return false;
483
	}
484

    
485
	// Sync file before returning
486
	return pfSense_fsync($file);
487
}
488

    
489
/****f* config/write_config
490
 * NAME
491
 *   write_config - Backup and write the firewall configuration.
492
 * DESCRIPTION
493
 *   write_config() handles backing up the current configuration,
494
 *   applying changes, and regenerating the configuration cache.
495
 * INPUTS
496
 *   $desc	- string containing the a description of configuration changes
497
 *   $backup	- boolean: do not back up current configuration if false.
498
 *   $write_config_only	- boolean: do not sync or reload anything; just save the configuration if true.
499
 * RESULT
500
 *   null
501
 ******/
502
/* save the system configuration */
503
function write_config($desc="Unknown", $backup = true, $write_config_only = false) {
504
	global $config, $g;
505

    
506
	if (!empty($_SERVER['REMOTE_ADDR'])) {
507
		if (!session_id()) {
508
			@session_start();
509
		}
510
		if (!empty($_SESSION['Username']) && ($_SESSION['Username'] != "admin")) {
511
			$user = getUserEntry($_SESSION['Username']);
512
			if (is_array($user) && userHasPrivilege($user, "user-config-readonly")) {
513
				session_commit();
514
				return false;
515
			}
516
		}
517
	}
518

    
519
	if (!isset($argc)) {
520
		session_commit();
521
	}
522

    
523
	if ($backup) {
524
		backup_config();
525
	}
526

    
527
	$config['revision'] = make_config_revision_entry($desc);
528

    
529
	conf_mount_rw();
530
	$lockkey = lock('config', LOCK_EX);
531

    
532
	/* generate configuration XML */
533
	$xmlconfig = dump_xml_config($config, $g['xml_rootobj']);
534

    
535
	/* write new configuration */
536
	if (!safe_write_file("{$g['cf_conf_path']}/config.xml", $xmlconfig, false)) {
537
		log_error(gettext("WARNING: Config contents could not be saved. Could not open file!"));
538
		unlock($lockkey);
539
		file_notice("config.xml", sprintf(gettext("Unable to open %s/config.xml for writing in write_config()%s"), $g['cf_conf_path'], "\n"));
540
		return -1;
541
	}
542

    
543
	cleanup_backupcache(true);
544

    
545
	/* re-read configuration */
546
	/* NOTE: We assume that the file can be parsed since we wrote it. */
547
	$config = parse_xml_config("{$g['conf_path']}/config.xml", $g['xml_rootobj']);
548
	if ($config == -1) {
549
		copy("{$g['conf_path']}/config.xml", "{$g['conf_path']}/config.xml.bad");
550
		$last_backup = discover_last_backup();
551
		if ($last_backup) {
552
			restore_backup("/cf/conf/backup/{$last_backup}");
553
			$config = parse_xml_config("{$g['conf_path']}/config.xml", $g['xml_rootobj']);
554
			if (platform_booting()) {
555
				echo "\n\n ************** WARNING **************";
556
				echo "\n\n Configuration could not be validated. A previous configuration was restored. \n";
557
				echo "\n The failed configuration file has been saved as {$g['conf_path']}/config.xml.bad \n\n";
558
			}
559
		} else {
560
			log_error(gettext("Could not restore config.xml."));
561
		}
562
	} else {
563
		generate_config_cache($config);
564
	}
565

    
566
	unlock($lockkey);
567

    
568
	if ($write_config_only) {
569
		/* tell kernel to sync fs data */
570
		conf_mount_ro();
571
		return $config;
572
	}
573

    
574
	unlink_if_exists("/usr/local/pkg/pf/carp_sync_client.php");
575

    
576
	/* tell kernel to sync fs data */
577
	conf_mount_ro();
578

    
579
	/* sync carp entries to other firewalls */
580
	carp_sync_client();
581

    
582
	if (is_dir("/usr/local/pkg/write_config")) {
583
		/* process packager manager custom rules */
584
		run_plugins("/usr/local/pkg/write_config/");
585
	}
586

    
587
	return $config;
588
}
589

    
590
/****f* config/reset_factory_defaults
591
 * NAME
592
 *   reset_factory_defaults - Reset the system to its default configuration.
593
 * RESULT
594
 *   integer	- indicates completion
595
 ******/
596
function reset_factory_defaults($lock = false, $reboot_required = true) {
597
	global $g;
598

    
599
	conf_mount_rw();
600

    
601
	/* Remove all additional packages */
602
	mwexec("/bin/sh /usr/local/sbin/{$g['product_name']}-upgrade " .
603
	    "-r ALL_PACKAGES");
604

    
605
	if (!$lock) {
606
		$lockkey = lock('config', LOCK_EX);
607
	}
608

    
609
	/* create conf directory, if necessary */
610
	safe_mkdir($g['cf_conf_path']);
611

    
612
	/* clear out /conf */
613
	$dh = opendir($g['conf_path']);
614
	while ($filename = readdir($dh)) {
615
		if (($filename != ".") && ($filename != "..") &&
616
		    (!is_dir($g['conf_path'] . "/" . $filename))) {
617
			unlink_if_exists($g['conf_path'] . "/" . $filename);
618
		}
619
	}
620
	closedir($dh);
621
	unlink_if_exists($g['tmp_path'] . "/config.cache");
622

    
623
	/* copy default configuration */
624
	copy("{$g['conf_default_path']}/config.xml",
625
	    "{$g['cf_conf_path']}/config.xml");
626

    
627
	disable_security_checks();
628

    
629
	/* call the wizard */
630
	if ($reboot_required) {
631
		// If we need a reboot first then touch a different trigger file.
632
		touch("/conf/trigger_initial_wizard_after_reboot");
633
	} else {
634
		touch("/conf/trigger_initial_wizard");
635
	}
636
	if (!$lock) {
637
		unlock($lockkey);
638
	}
639
	conf_mount_ro();
640
	setup_serial_port();
641
	return 0;
642
}
643

    
644
function config_restore($conffile) {
645
	global $config, $g;
646

    
647
	if (!file_exists($conffile)) {
648
		return 1;
649
	}
650

    
651
	backup_config();
652

    
653
	conf_mount_rw();
654

    
655
	$lockkey = lock('config', LOCK_EX);
656

    
657
	unlink_if_exists("{$g['tmp_path']}/config.cache");
658
	copy($conffile, "{$g['cf_conf_path']}/config.xml");
659

    
660
	disable_security_checks();
661

    
662
	unlock($lockkey);
663

    
664
	$config = parse_config(true);
665

    
666
	conf_mount_ro();
667

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

    
670
	return 0;
671
}
672

    
673
function config_install($conffile) {
674
	global $config, $g;
675

    
676
	if (!file_exists($conffile)) {
677
		return 1;
678
	}
679

    
680
	if (!config_validate("{$conffile}")) {
681
		return 1;
682
	}
683

    
684
	if (platform_booting()) {
685
		echo gettext("Installing configuration...") . "\n";
686
	} else {
687
		log_error(gettext("Installing configuration ...."));
688
	}
689

    
690
	conf_mount_rw();
691
	$lockkey = lock('config', LOCK_EX);
692

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

    
695
	disable_security_checks();
696

    
697
	/* unlink cache file if it exists */
698
	if (file_exists("{$g['tmp_path']}/config.cache")) {
699
		unlink("{$g['tmp_path']}/config.cache");
700
	}
701

    
702
	unlock($lockkey);
703
	conf_mount_ro();
704

    
705
	return 0;
706
}
707

    
708
/*
709
 * Disable security checks for DNS rebind and HTTP referrer until next time
710
 * they pass (or reboot), to aid in preventing accidental lockout when
711
 * restoring settings like hostname, domain, IP addresses, and settings
712
 * related to the DNS rebind and HTTP referrer checks.
713
 * Intended for use when restoring a configuration or directly
714
 * modifying config.xml without an unconditional reboot.
715
 */
716
function disable_security_checks() {
717
	global $g;
718
	touch("{$g['tmp_path']}/disable_security_checks");
719
}
720

    
721
/* Restores security checks.  Should be called after all succeed. */
722
function restore_security_checks() {
723
	global $g;
724
	unlink_if_exists("{$g['tmp_path']}/disable_security_checks");
725
}
726

    
727
/* Returns status of security check temporary disable. */
728
function security_checks_disabled() {
729
	global $g;
730
	return file_exists("{$g['tmp_path']}/disable_security_checks");
731
}
732

    
733
function config_validate($conffile) {
734

    
735
	global $g, $xmlerr;
736

    
737
	$xml_parser = xml_parser_create();
738

    
739
	if (!($fp = fopen($conffile, "r"))) {
740
		$xmlerr = gettext("XML error: unable to open file");
741
		return false;
742
	}
743

    
744
	while ($data = fread($fp, 4096)) {
745
		if (!xml_parse($xml_parser, $data, feof($fp))) {
746
			$xmlerr = sprintf(gettext('%1$s at line %2$d'),
747
						xml_error_string(xml_get_error_code($xml_parser)),
748
						xml_get_current_line_number($xml_parser));
749
			return false;
750
		}
751
	}
752
	xml_parser_free($xml_parser);
753

    
754
	fclose($fp);
755

    
756
	return true;
757
}
758

    
759
function cleanup_backupcache($lock = false) {
760
	global $g;
761
	$i = false;
762

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

    
765
	if (!$lock) {
766
		$lockkey = lock('config');
767
	}
768

    
769
	conf_mount_rw();
770

    
771
	$backups = get_backups();
772
	if ($backups) {
773
		$baktimes = $backups['versions'];
774
		unset($backups['versions']);
775
	} else {
776
		$backups = array();
777
		$baktimes = array();
778
	}
779
	$newbaks = array();
780
	$bakfiles = glob($g['cf_conf_path'] . "/backup/config-*");
781
	$tocache = array();
782

    
783
	foreach ($bakfiles as $backup) { // Check for backups in the directory not represented in the cache.
784
		$backupsize = filesize($backup);
785
		if ($backupsize == 0) {
786
			unlink($backup);
787
			continue;
788
		}
789
		$backupexp = explode('-', $backup);
790
		$backupexp = explode('.', array_pop($backupexp));
791
		$tocheck = array_shift($backupexp);
792
		unset($backupexp);
793
		if (!in_array($tocheck, $baktimes)) {
794
			$i = true;
795
			if (platform_booting()) {
796
				echo ".";
797
			}
798
			$newxml = parse_xml_config($backup, array($g['xml_rootobj'], 'pfsense'));
799
			if ($newxml == "-1") {
800
				log_error(sprintf(gettext("The backup cache file %s is corrupted.  Unlinking."), $backup));
801
				unlink($backup);
802
				log_error(sprintf(gettext("The backup cache file %s is corrupted.  Unlinking."), $backup));
803
				continue;
804
			}
805
			if ($newxml['revision']['description'] == "") {
806
				$newxml['revision']['description'] = "Unknown";
807
			}
808
			if ($newxml['version'] == "") {
809
				$newxml['version'] = "?";
810
			}
811
			$tocache[$tocheck] = array('description' => $newxml['revision']['description'], 'version' => $newxml['version'], 'filesize' => $backupsize);
812
		}
813
	}
814
	foreach ($backups as $checkbak) {
815
		if (count(preg_grep('/' . $checkbak['time'] . '/i', $bakfiles)) != 0) {
816
			$newbaks[] = $checkbak;
817
		} else {
818
			$i = true;
819
			if (platform_booting()) print " " . $tocheck . "r";
820
		}
821
	}
822
	foreach ($newbaks as $todo) {
823
		$tocache[$todo['time']] = array('description' => $todo['description'], 'version' => $todo['version'], 'filesize' => $todo['filesize']);
824
	}
825
	if (is_int($revisions) and (count($tocache) > $revisions)) {
826
		$toslice = array_slice(array_keys($tocache), 0, $revisions);
827
		foreach ($toslice as $sliced) {
828
			$newcache[$sliced] = $tocache[$sliced];
829
		}
830
		foreach ($tocache as $version => $versioninfo) {
831
			if (!in_array($version, array_keys($newcache))) {
832
				unlink_if_exists($g['conf_path'] . '/backup/config-' . $version . '.xml');
833
			}
834
		}
835
		$tocache = $newcache;
836
	}
837
	$bakout = fopen($g['cf_conf_path'] . '/backup/backup.cache', "w");
838
	fwrite($bakout, serialize($tocache));
839
	fclose($bakout);
840
	pfSense_fsync("{$g['cf_conf_path']}/backup/backup.cache");
841
	conf_mount_ro();
842

    
843
	if (!$lock) {
844
		unlock($lockkey);
845
	}
846
}
847

    
848
function get_backups() {
849
	global $g;
850
	if (file_exists("{$g['cf_conf_path']}/backup/backup.cache")) {
851
		$confvers = unserialize(file_get_contents("{$g['cf_conf_path']}/backup/backup.cache"));
852
		$bakvers = array_keys($confvers);
853
		$toreturn = array();
854
		sort($bakvers);
855
		// 	$bakvers = array_reverse($bakvers);
856
		foreach (array_reverse($bakvers) as $bakver) {
857
			$toreturn[] = array('time' => $bakver, 'description' => $confvers[$bakver]['description'], 'version' => $confvers[$bakver]['version'], 'filesize' => $confvers[$bakver]['filesize']);
858
		}
859
	} else {
860
		return false;
861
	}
862
	$toreturn['versions'] = $bakvers;
863
	return $toreturn;
864
}
865

    
866
function backup_config() {
867
	global $config, $g;
868

    
869
	if ($g['platform'] == "cdrom") {
870
		return;
871
	}
872

    
873
	conf_mount_rw();
874

    
875
	/* Create backup directory if needed */
876
	safe_mkdir("{$g['cf_conf_path']}/backup");
877
	if ($config['revision']['time'] == "") {
878
		$baktime = 0;
879
	} else {
880
		$baktime = $config['revision']['time'];
881
	}
882

    
883
	if ($config['revision']['description'] == "") {
884
		$bakdesc = "Unknown";
885
	} else {
886
		$bakdesc = $config['revision']['description'];
887
	}
888

    
889
	$bakver = ($config['version'] == "") ? "?" : $config['version'];
890
	$bakfilename = $g['cf_conf_path'] . '/backup/config-' . $baktime . '.xml';
891
	copy($g['cf_conf_path'] . '/config.xml', $bakfilename);
892

    
893
	if (file_exists($g['cf_conf_path'] . '/backup/backup.cache')) {
894
		$backupcache = unserialize(file_get_contents($g['cf_conf_path'] . '/backup/backup.cache'));
895
	} else {
896
		$backupcache = array();
897
	}
898
	$backupcache[$baktime] = array('description' => $bakdesc, 'version' => $bakver, 'filesize' => filesize($bakfilename));
899
	$bakout = fopen($g['cf_conf_path'] . '/backup/backup.cache', "w");
900
	fwrite($bakout, serialize($backupcache));
901
	fclose($bakout);
902
	pfSense_fsync("{$g['cf_conf_path']}/backup/backup.cache");
903

    
904
	conf_mount_ro();
905

    
906
	return true;
907
}
908

    
909
function set_device_perms() {
910
	$devices = array(
911
		'pf' => array(
912
			'user' => 'root',
913
			'group' => 'proxy',
914
			'mode' => 0660),
915
		);
916

    
917
	foreach ($devices as $name => $attr) {
918
		$path = "/dev/$name";
919
		if (file_exists($path)) {
920
			chown($path, $attr['user']);
921
			chgrp($path, $attr['group']);
922
			chmod($path, $attr['mode']);
923
		}
924
	}
925
}
926

    
927
function get_config_user() {
928
	if (empty($_SESSION["Username"])) {
929
		$username = getenv("USER");
930
		if (empty($conuser) || $conuser == "root") {
931
			$username = "(system)";
932
		}
933
	} else {
934
		$username = $_SESSION["Username"];
935
	}
936

    
937
	if (!empty($_SERVER['REMOTE_ADDR'])) {
938
		$username .= '@' . $_SERVER['REMOTE_ADDR'];
939
	}
940

    
941
	return $username;
942
}
943

    
944
function make_config_revision_entry($desc = null, $override_user = null) {
945
	if (empty($override_user)) {
946
		$username = get_config_user();
947
	} else {
948
		$username = $override_user;
949
	}
950

    
951
	$revision = array();
952

    
953
	if (time() > mktime(0, 0, 0, 9, 1, 2004)) {     /* make sure the clock settings are plausible */
954
		$revision['time'] = time();
955
	}
956

    
957
	/* Log the running script so it's not entirely unlogged what changed */
958
	if ($desc == "Unknown") {
959
		$desc = sprintf(gettext("%s made unknown change"), $_SERVER['SCRIPT_NAME']);
960
	}
961
	if (!empty($desc)) {
962
		$revision['description'] = "{$username}: " . $desc;
963
	}
964
	$revision['username'] = $username;
965
	return $revision;
966
}
967

    
968
function pfSense_clear_globals() {
969
	global $config, $FilterIfList, $GatewaysList, $filterdns, $aliases, $aliastable;
970

    
971
	$error = error_get_last();
972

    
973
	if ($error !== NULL) {
974
		if ($error['type'] == E_ERROR) {
975
			$errorstr = "PHP ERROR: Type: {$error['type']}, File: {$error['file']}, Line: {$error['line']}, Message: {$error['message']}";
976
			print($errorstr);
977
			log_error($errorstr);
978
		} else if ($error['type'] != E_NOTICE) {
979
			$errorstr = "PHP WARNING: Type: {$error['type']}, File: {$error['file']}, Line: {$error['line']}, Message: {$error['message']}";
980
			// XXX: comment out for now, should re-enable post-2.2
981
			//print($errorstr);
982
			//log_error($errorstr);
983
		}
984
	}
985

    
986
	if (isset($FilterIfList)) {
987
		unset($FilterIfList);
988
	}
989

    
990
	if (isset($GatewaysList)) {
991
		unset($GatewaysList);
992
	}
993

    
994
	/* Used for the hostname dns resolver */
995
	if (isset($filterdns)) {
996
		unset($filterdns);
997
	}
998

    
999
	/* Used for aliases and interface macros */
1000
	if (isset($aliases)) {
1001
		unset($aliases);
1002
	}
1003
	if (isset($aliastable)) {
1004
		unset($aliastable);
1005
	}
1006

    
1007
	unset($config);
1008
}
1009

    
1010
register_shutdown_function('pfSense_clear_globals');
1011

    
1012
?>
(8-8/50)