-
Notifications
You must be signed in to change notification settings - Fork 759
/
config.inc
339 lines (282 loc) · 10.1 KB
/
config.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
<?php
/*
* Copyright (C) 2015-2017 Franco Fichtner <franco@opnsense.org>
* Copyright (C) 2009 Erik Kristensen <erik@erikkristensen.com>
* Copyright (C) 2004-2010 Scott Ullrich <sullrich@gmail.com>
* Copyright (C) 2003-2004 Manuel Kasper <mk@neon1.net>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/* XXX make this a getter function for traceability */
$g = json_decode(file_get_contents('/usr/local/opnsense/version/opnsense'), true);
openlog($g['product_id'], LOG_ODELAY, LOG_USER);
register_shutdown_function('closelog');
require_once("xmlparse.inc");
require_once("notices.inc");
require_once("legacy_bindings.inc");
require_once("certs.inc");
/*
* Hook up the plugin system which consists of several low-profile
* functions that can be called from within our backend code when
* they exist.
*/
require_once('plugins.inc');
/**
* parse config into array and return
*/
function load_config_from_file($filename)
{
return OPNsense\Core\Config::getInstance()->toArrayFromFile($filename, listtags());
}
/****f* config/parse_config
* NAME
* parse_config - Read in config.xml if needed and return $config array
* RESULT
* $config - array containing all configuration variables
******/
function parse_config()
{
global $aliastable;
$cnf = OPNsense\Core\Config::getInstance();
// return config data as array, use old "listags" construction to mark certain elements as array (even if they're not recurring)
$config = $cnf->toArray(listtags());
$aliastable = array();
foreach ((new \OPNsense\Firewall\Alias())->aliasIterator() as $alias) {
if (strncmp($alias['type'], 'url', 3) !== 0) {
$aliastable[$alias['name']] = implode(" ", explode("\n", $alias['content']));
} else {
$aliastable[$alias['name']] = "";
}
}
return $config;
}
/****f* config/convert_config
* NAME
* convert_config - Attempt to update config.xml.
* DESCRIPTION
* convert_config() reads the current global configuration
* and attempts to convert it to conform to the latest
* config.xml version. This allows major formatting changes
* to be made with a minimum of breakage.
* RESULT
* null
******/
/* convert configuration, if necessary */
function convert_config($verbose = false)
{
global $config;
if (!isset($config['revision'])) {
/* force a revision tag for proper handling in config history */
write_config('Factory configuration', false);
}
/* chain the new migration into this function call */
$mvc_migration = '/usr/local/opnsense/mvc/script/run_migrations.php';
if ($verbose) {
passthru($mvc_migration);
} else {
mwexecf($mvc_migration);
}
/* reload the config as it was rewritten and saved in the script context */
OPNsense\Core\Config::getInstance()->forceReload();
$config = parse_config();
}
/****f* config/write_config
* NAME
* write_config - Backup and write the firewall configuration.
* DESCRIPTION
* write_config() handles backing up the current configuration,
* applying changes, and regenerating the configuration cache.
* INPUTS
* $desc - string containing the a description of configuration changes
* $backup - boolean: do not back up current configuration if false.
* RESULT
* null
******/
/* save the system configuration */
function write_config($desc = '', $backup = true)
{
global $config;
if (!empty($_SERVER['REMOTE_ADDR'])) {
if (!empty($_SESSION['Username']) && ($_SESSION['Username'] != 'root')) {
$user = getUserEntry($_SESSION['Username']);
if (is_array($user) && userHasPrivilege($user, "user-config-readonly")) {
// okay, it's not very nice to check permissions here, but let's make it explicit while we do...
log_error("WARNING: User {$_SESSION['Username']} may not write config (user-config-readonly set)");
return false;
}
}
}
plugins_interfaces();
$cnf = OPNsense\Core\Config::getInstance();
$cnf->fromArray($config);
$revision_info = make_config_revision_entry($desc);
try {
$cnf->save($revision_info, $backup);
} catch (OPNsense\Core\ConfigException $e) {
// write failure
syslog(LOG_ERR, 'WARNING: Config contents could not be saved. Could not open file!');
return -1;
}
/* sync carp entries to other firewalls */
if (isset($config['hasync']['synchronizetoip']) && trim($config['hasync']['synchronizetoip']) != "") {
configd_run('filter sync load');
}
/* cleanup backups */
cleanup_backups();
/* on succesfull save, serialize config back to global */
$config = $cnf->toArray(listtags());
return $config;
}
function config_restore($conffile)
{
global $config;
if (!file_exists($conffile))
return 1;
$cnf = OPNsense\Core\Config::getInstance();
$cnf->backup();
$cnf->restoreBackup($conffile);
disable_security_checks();
$config = parse_config();
write_config(sprintf('Reverted to %s', array_pop(explode('/', $conffile))), false);
return 0;
}
/*
* Disable security checks for DNS rebind and HTTP referrer until next time
* they pass (or reboot), to aid in preventing accidental lockout when
* restoring settings like hostname, domain, IP addresses, and settings
* related to the DNS rebind and HTTP referrer checks.
* Intended for use when restoring a configuration or directly
* modifying config.xml without an unconditional reboot.
*/
function disable_security_checks()
{
touch('/tmp/disable_security_checks');
}
/* Restores security checks. Should be called after all succeed. */
function restore_security_checks()
{
@unlink('/tmp/disable_security_checks');
}
/* Returns status of security check temporary disable. */
function security_checks_disabled()
{
return file_exists('/tmp/disable_security_checks');
}
/**
* remove old backups
*/
function cleanup_backups()
{
global $config;
if (isset($config['system']['backupcount']) && is_numeric($config['system']['backupcount']) && ($config['system']['backupcount'] >= 0)) {
$revisions = intval($config['system']['backupcount']);
} else {
/* XXX this value used to be left out of the config */
$revisions = 60;
}
$cnf = OPNsense\Core\Config::getInstance();
$cnt=1;
foreach ($cnf->getBackups() as $filename) {
if ($cnt > $revisions ) {
@unlink($filename);
}
++$cnt ;
}
}
function &config_read_array()
{
global $config;
$current = &$config;
foreach (func_get_args() as $key) {
if (!isset($current[$key]) || !is_array($current[$key])) {
$current[$key] = array();
}
$current = &$current[$key];
}
return $current;
}
function make_config_revision_entry($desc = '')
{
if (!empty($_SESSION['Username'])) {
$username = $_SESSION['Username'];
} else {
$username = '(' . trim(shell_exec('/usr/bin/whoami')) . ')';
}
if (!empty($_SERVER['REMOTE_ADDR'])) {
$username .= '@' . $_SERVER['REMOTE_ADDR'];
}
if (empty($desc)) {
$desc = sprintf('%s made changes', $_SERVER['SCRIPT_NAME']);
}
$revision = array();
$revision['username'] = $username;
$revision['time'] = microtime(true);
$revision['description'] = $desc;
return $revision;
}
/**
* find list of registered interfaces
* @param array $filters list of filters to apply
* @return array interfaces
*/
function legacy_config_get_interfaces($filters = array())
{
$interfaces = array();
foreach (config_read_array('interfaces') as $ifname => $iface) {
// undo stupid listags() turning our item into a new array, preventing certain names to be used as interface.
// see src/etc/inc/xmlparse.inc
if (isset($iface[0])) {
$iface = $iface[0];
}
// apply filters
$iface_match = true;
foreach ($filters as $filter_key => $filter_value) {
if ($filter_key == 'enable' && isset($iface[$filter_key])) {
$field_value = true;
} else {
$field_value = isset($iface[$filter_key]) ? $iface[$filter_key] : false;
}
if ($field_value != $filter_value) {
$iface_match = false;
break;
}
}
if ($iface_match) {
$iface['descr'] = !empty($iface['descr']) ? $iface['descr'] : strtoupper($ifname);
$iface['ipaddrv6'] = !empty($iface['ipaddrv6']) ? $iface['ipaddrv6'] : null;
$iface['ipaddr'] = !empty($iface['ipaddr']) ? $iface['ipaddr'] : null;
$interfaces[$ifname] = $iface;
}
}
uasort($interfaces, function ($a, $b) {
return strnatcmp($a['descr'], $b['descr']);
});
return $interfaces;
}
$config = parse_config();
/* set timezone */
$timezone = $config['system']['timezone'];
if (!$timezone) {
$timezone = 'Etc/UTC';
}
date_default_timezone_set($timezone);