-
Notifications
You must be signed in to change notification settings - Fork 759
/
AliasController.php
487 lines (456 loc) · 17.9 KB
/
AliasController.php
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
<?php
/**
* Copyright (C) 2018 Deciso B.V.
*
* 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.
*
*/
namespace OPNsense\Firewall\Api;
use OPNsense\Base\ApiMutableModelControllerBase;
use OPNsense\Base\UserException;
use OPNsense\Core\Backend;
use OPNsense\Core\Config;
use OPNsense\Firewall\Category;
/**
* @package OPNsense\Firewall
*/
class AliasController extends ApiMutableModelControllerBase
{
protected static $internalModelName = 'alias';
protected static $internalModelClass = 'OPNsense\Firewall\Alias';
/**
* search aliases
* @return array search results
* @throws \ReflectionException
*/
public function searchItemAction()
{
$type = $this->request->get('type');
$category = $this->request->get('category');
$filter_funct = function ($record) use ($type, $category) {
$match_type = empty($type) || in_array($record->type, $type);
$match_cat = empty($category) || array_intersect(explode(',', $record->categories), $category);
return $match_type && $match_cat;
};
$result = $this->searchBase(
"aliases.alias",
['enabled', 'name', 'description', 'type', 'content', 'current_items', 'last_updated'],
"name",
$filter_funct
);
/**
* remap some source data from the model as searchBase() is not able to distinct this.
* - category uuid's
* - unix group id's to names in content fields
*/
$categories = [];
$types = [];
foreach ($this->getModel()->aliases->alias->iterateItems() as $key => $alias) {
$categories[$key] = !empty((string)$alias->categories) ? explode(',', (string)$alias->categories) : [];
$types[$key] = (string)$alias->type;
}
$group_mapping = null;
foreach ($result['rows'] as &$record) {
$record['categories_uuid'] = $categories[$record['uuid']];
if ($types[$record['uuid']] == 'authgroup') {
if ($group_mapping === null) {
$group_mapping = $this->listUserGroupsAction();
}
$groups = [];
foreach (explode(',', $record['content']) as $grp) {
if (isset($group_mapping[$grp])) {
$groups[] = $group_mapping[$grp]['name'];
}
}
$record['content'] = implode(',', $groups);
}
}
return $result;
}
/**
* list categories and usage
* @return array
*/
public function listCategoriesAction()
{
$response = ['rows' => []];
$catcount = [];
foreach ($this->getModel()->aliases->alias->iterateItems() as $alias) {
if (!empty((string)$alias->categories)) {
foreach (explode(',', (string)$alias->categories) as $cat) {
if (!isset($catcount[$cat])) {
$catcount[$cat] = 0;
}
$catcount[$cat] += 1;
}
}
}
$mdlcat = new Category();
foreach ($mdlcat->categories->category->iterateItems() as $key => $category) {
$response['rows'][] = [
"uuid" => $key,
"name" => (string)$category->name,
"color" => (string)$category->color,
"used" => isset($catcount[$key]) ? $catcount[$key] : 0
];
}
array_multisort(array_column($response['rows'], "name"), SORT_ASC, SORT_NATURAL, $response['rows']);
return $response;
}
/**
* Update alias with given properties
* @param string $uuid internal id
* @return array save result + validation output
* @throws \OPNsense\Base\ValidationException when field validations fail
* @throws \ReflectionException when not bound to model
*/
public function setItemAction($uuid)
{
$node = $this->getModel()->getNodeByReference('aliases.alias.' . $uuid);
$old_name = $node != null ? (string)$node->name : null;
if (
$old_name !== null &&
$this->request->isPost() &&
$this->request->hasPost("alias") &&
!empty($this->request->getPost("alias")['name'])
) {
$new_name = $this->request->getPost("alias")['name'];
if ($new_name != $old_name) {
// replace aliases, setBase() will synchronise the changes to disk
$this->getModel()->refactor($old_name, $new_name);
}
}
return $this->setBase("alias", "aliases.alias", $uuid);
}
/**
* Add new alias and set with attributes from post
* @return array save result + validation output
* @throws \OPNsense\Base\ModelException when not bound to model
* @throws \OPNsense\Base\ValidationException when field validations fail
* @throws \ReflectionException when not bound to model
*/
public function addItemAction()
{
return $this->addBase("alias", "aliases.alias");
}
/**
* Retrieve alias settings or return defaults for new one
* @param $uuid item unique id
* @return array alias content
* @throws \ReflectionException when not bound to model
*/
public function getItemAction($uuid = null)
{
$response = $this->getBase("alias", "aliases.alias", $uuid);
$selected_aliases = array_keys($response['alias']['content']);
foreach ($this->getModel()->aliasIterator() as $alias) {
if (!in_array($alias['name'], $selected_aliases)) {
$response['alias']['content'][$alias['name']] = [
"selected" => 0, "value" => $alias['name']
];
}
// append descriptions
$response['alias']['content'][$alias['name']]['description'] = $alias['description'];
}
return $response;
}
/**
* find the alias uuid by name
* @param $name alias name
* @return array uuid
* @throws \ReflectionException
*/
public function getAliasUUIDAction($name)
{
$node = $this->getModel();
foreach ($node->aliases->alias->iterateItems() as $key => $alias) {
if ((string)$alias->name == $name) {
return array('uuid' => $key);
}
}
return array();
}
/**
* Delete alias by uuid, save contents to tmp for removal on apply
* @param string $uuid internal id
* @return array save status
* @throws \OPNsense\Base\ValidationException when field validations fail
* @throws \ReflectionException when not bound to model
* @throws \OPNsense\Base\UserException when unable to delete
*/
public function delItemAction($uuid)
{
Config::getInstance()->lock();
$node = $this->getModel()->getNodeByReference('aliases.alias.' . $uuid);
if ($node != null) {
$uses = $this->getModel()->whereUsed((string)$node->name);
if (!empty($uses)) {
$message = "";
foreach ($uses as $key => $value) {
$message .= htmlspecialchars(sprintf("\n[%s] %s", $key, $value), ENT_NOQUOTES | ENT_HTML401);
}
$message = sprintf(gettext("Cannot delete alias. Currently in use by %s"), $message);
throw new \OPNsense\Base\UserException($message, gettext("Alias in use"));
}
}
return $this->delBase("aliases.alias", $uuid);
}
/**
* toggle status
* @param string $uuid id to toggled
* @param string|null $enabled set enabled by default
* @return array status
* @throws \OPNsense\Base\ValidationException when field validations fail
* @throws \ReflectionException when not bound to model
*/
public function toggleItemAction($uuid, $enabled = null)
{
return $this->toggleBase("aliases.alias", $uuid, $enabled);
}
/**
* list countries and regions
* @return array indexed by country code
*/
public function listCountriesAction()
{
$result = [
'EU' => [
'name' => gettext('Unclassified'),
'region' => 'Europe'
]
];
foreach (explode("\n", file_get_contents('/usr/local/opnsense/contrib/tzdata/iso3166.tab')) as $line) {
$line = trim($line);
if (strlen($line) > 3 && substr($line, 0, 1) != '#') {
$result[substr($line, 0, 2)] = array(
"name" => trim(substr($line, 2, 9999)),
"region" => null
);
}
}
foreach (explode("\n", file_get_contents('/usr/local/opnsense/contrib/tzdata/zone.tab')) as $line) {
if (strlen($line) > 0 && substr($line, 0, 1) == '#') {
continue;
}
$line = explode("\t", $line);
if (empty($line[0]) || strlen($line[0]) != 2) {
continue;
}
if (empty($line[2]) || strpos($line[2], '/') === false) {
continue;
}
if (!empty($result[$line[0]]) && empty($result[$line[0]]['region'])) {
$result[$line[0]]['region'] = explode('/', $line[2])[0];
}
}
return $result;
}
/**
* list user groups
* @return array user groups
*/
public function listUserGroupsAction()
{
$result = [];
$cnf = Config::getInstance()->object();
if (isset($cnf->system->group)) {
foreach ($cnf->system->group as $group) {
$name = (string)$group->name;
if ($name != 'all') {
$result[(string)$group->gid] = [
"name" => $name,
"gid" => (string)$group->gid
];
}
}
ksort($result);
}
return $result;
}
/**
* list network alias types
* @return array indexed by country alias name
*/
public function listNetworkAliasesAction()
{
$result = [];
foreach ($this->getModel()->aliases->alias->iterateItems() as $alias) {
if (!in_array((string)$alias->type, ['external', 'port'])) {
$result[(string)$alias->name] = [
"name" => (string)$alias->name,
"description" => (string)$alias->description
];
}
}
ksort($result);
return $result;
}
/**
* reconfigure aliases
*/
public function reconfigureAction()
{
if ($this->request->isPost()) {
$backend = new Backend();
$backend->configdRun('template reload OPNsense/Filter');
$backend->configdRun("filter reload skip_alias");
$bckresult = json_decode($backend->configdRun("filter refresh_aliases"), true);
if (!empty($bckresult['messages'])) {
throw new UserException(implode("\n", $bckresult['messages']), gettext("Alias"));
}
return array("status" => "ok");
} else {
return array("status" => "failed");
}
}
/**
* get aliases load stats and table-entries limit
*/
public function getTableSizeAction()
{
return json_decode((new Backend())->configdRun('filter diag table_size'), true);
}
/**
* variant on model.getNodes() returning actual field values
* @param $parent_node BaseField node to reverse
*/
private function getRawNodes($parent_node)
{
$result = array();
foreach ($parent_node->iterateItems() as $key => $node) {
if ($node->isContainer()) {
$result[$key] = $this->getRawNodes($node);
} else {
$result[$key] = (string)$node;
}
}
return $result;
}
/**
* export configured aliases
*/
public function exportAction()
{
if ($this->request->isGet()) {
// return raw, unescaped since this content is intended for direct download
$this->response->setContentType('application/json', 'UTF-8');
$this->response->setContent(json_encode($this->getRawNodes($this->getModel())));
} else {
throw new UserException("Unsupported request type");
}
}
/**
* import delivered aliases in post variable "data", validate all only commit when fully valid.
*/
public function importAction()
{
if ($this->request->isPost()) {
$result = array("existing" => 0, "new" => 0, "status" => "failed");
$data = $this->request->getPost("data");
if (
is_array($data) && !empty($data['aliases'])
&& !empty($data['aliases']['alias']) && is_array($data['aliases']['alias'])
) {
Config::getInstance()->lock();
// save into model
$uuid_mapping = array();
foreach ($data['aliases']['alias'] as $uuid => $content) {
$type = !empty($content['type']) ? $content['type'] : "";
if (is_array($content) && !empty($content['name']) && $type != 'internal') {
$node = $this->getModel()->getByName($content['name'], true);
if ($node == null) {
$node = $this->getModel()->aliases->alias->Add();
$result['new'] += 1;
} else {
$result['existing'] += 1;
}
foreach ($content as $prop => $value) {
$node->$prop = $value;
}
$uuid_mapping[$node->getAttribute('uuid')] = $uuid;
}
}
// attach this alias to util class, to avoid recursion issues (aliases used in aliases).
\OPNsense\Firewall\Util::attachAliasObject($this->getModel());
// perform validation, record details.
foreach ($this->getModel()->performValidation() as $msg) {
if (empty($result['validations'])) {
$result['validations'] = array();
}
$parts = explode('.', $msg->getField());
$uuid = $parts[count($parts) - 2];
$fieldname = $parts[count($parts) - 1];
$result['validations'][$uuid_mapping[$uuid] . "." . $fieldname] = $msg->getMessage();
}
// only persist when valid import
if (empty($result['validations'])) {
$result['status'] = "ok";
$this->save();
} else {
$result['status'] = "failed";
Config::getInstance()->unlock();
}
}
} else {
throw new UserException("Unsupported request type");
}
return $result;
}
/**
* get geoip settings (and stats)
*/
public function getGeoIPAction()
{
$result = array();
if ($this->request->isGet()) {
$cnf = Config::getInstance()->object();
$result[static::$internalModelName] = ['geoip' => array()];
$node = $this->getModel()->getNodeByReference('geoip');
if ($node != null) {
$result[static::$internalModelName]['geoip'] = $node->getNodes();
}
// count aliases that depend on GeoIP data
$result[static::$internalModelName]['geoip']['usages'] = 0;
foreach ($this->getModel()->aliasIterator() as $alias) {
if ($alias['type'] == "geoip") {
$result[static::$internalModelName]['geoip']['usages']++;
}
}
if (isset($cnf->system->firmware) && !empty($cnf->system->firmware->mirror)) {
// XXX: we might add some attribute in firmware to store subscription status, since we now only store uri
$result[static::$internalModelName]['geoip']['subscription'] =
strpos($cnf->system->firmware->mirror, 'opnsense-update.deciso.com') !== false;
}
$result[static::$internalModelName]['geoip']['address_count'] = 0;
if (file_exists('/usr/local/share/GeoIP/alias.stats')) {
$stats = json_decode(file_get_contents('/usr/local/share/GeoIP/alias.stats'), true);
$result[static::$internalModelName]['geoip'] = array_merge(
$result[static::$internalModelName]['geoip'],
$stats
);
}
}
return $result;
}
}