Skip to content

Commit f7fb111

Browse files
committed
Alias Management: modify - network_address_aliases_update
Signed-off-by: lilinzhe <[email protected]>
1 parent 140aec0 commit f7fb111

File tree

4 files changed

+289
-142
lines changed

4 files changed

+289
-142
lines changed

README.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ tasks feasible.
2626
- [system_info](#user-content-system_info) - Returns various useful system info.
2727
- [network_address_aliases_get](#user-content-network_address_aliases_get) - Returns address aliaes used by rules.
2828
- [network_address_aliases_create](#user-content-network_address_aliases_create) - Creates An network aliaes for rules
29+
- [network_address_aliases_update](#user-content-network_address_aliases_update) - Update a address aliaes. Returns newest result
2930
- [filter_rules_get](#user-content-filter_rules_get) - Returns firewall filters.
3031

3132

@@ -994,6 +995,59 @@ curl \
994995
"https://<host-address>/fauxapi/v1/?action=network_address_aliases_create"
995996
```
996997
998+
*Example Response*
999+
```javascript
1000+
{
1001+
"callid": "5e22393a9aa5a",
1002+
"action": "network_address_aliases_create",
1003+
"message": "ok",
1004+
"data": {
1005+
"aliases": {
1006+
"alias": [
1007+
{
1008+
"name": "EasyRuleBlockHostsWAN",
1009+
"type": "network",
1010+
"address": "1.2.3.4/32 5.6.7.8/32",
1011+
"descr": "Hosts blocked from Firewall Log view",
1012+
"detail": "Entry added Fri, 27 Dec 2019 00:53:01 -0800||\u5df2\u6dfb\u52a0\u6761\u76ee Thu, 16 Jan 2020 03:42:37 -0800"
1013+
},
1014+
{
1015+
"name": "wsdfan",
1016+
"descr": "Test",
1017+
"type": "network",
1018+
"address": "12.23.45.3/32",
1019+
"detail": "a"
1020+
}
1021+
]
1022+
}
1023+
}
1024+
}
1025+
```
1026+
---
1027+
### network_address_aliases_update
1028+
- Update a address aliaes. Returns newest result
1029+
- HTTP: **POST**
1030+
- Params: none
1031+
- Request body: json
1032+
- **name** :<string> name of aliases. identiy which aliases frr modify
1033+
- **type** :<string> type of aliases. **MUST** be `network` for now.
1034+
- **cidr_addresses** : < list of <object> > name alias what
1035+
- **address** an ip address or a network prefix.
1036+
- **details** a description of this address. for human readable documentation.
1037+
- **descr** : <string> the description of current aliases.
1038+
- Response: json <object>: the items after created
1039+
1040+
*Example Request*
1041+
```bash
1042+
curl \
1043+
-X GET \
1044+
--silent \
1045+
--insecure \
1046+
--header "fauxapi-auth: <auth-value>" \
1047+
--data '{"name": "wsdfan", "type": "network", "cidr_addresses": [{"address":"12.23.45.3/32", "details":"a"}], "descr":"Test"}'
1048+
"https://<host-address>/fauxapi/v1/?action=network_address_aliases_create"
1049+
```
1050+
9971051
*Example Response*
9981052
```javascript
9991053
{

pfSense-pkg-FauxAPI/files/etc/inc/fauxapi/fauxapi_actions.inc

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,7 @@ class fauxApiActions {
473473
return TRUE;
474474
}
475475
/**
476-
* address_aliases_get()
476+
* network_address_aliases_create()
477477
*
478478
* @return boolean
479479
*/
@@ -483,7 +483,7 @@ class fauxApiActions {
483483
$name = $this->action_input_data["name"];
484484
$type = $this->action_input_data["type"];
485485
$cidr_addresses =$this->action_input_data['cidr_addresses'];
486-
$descr=$this->action_input_data['descr'] or "Added by fauxapi";
486+
$descr=$this->action_input_data['descr'];
487487

488488
$alias = $this->PfsenseInterface->network_address_aliases_create($name, $type, $cidr_addresses, $descr);
489489

@@ -499,6 +499,34 @@ class fauxApiActions {
499499
);
500500
return TRUE;
501501
}
502+
503+
/**
504+
* network_address_aliases_update()
505+
*
506+
* @return boolean
507+
*/
508+
public function network_address_aliases_update() {
509+
fauxApiLogger::debug(__METHOD__);
510+
511+
$name = $this->action_input_data["name"];
512+
$type = $this->action_input_data["type"];
513+
$cidr_addresses =$this->action_input_data['cidr_addresses'];
514+
$descr=$this->action_input_data['descr'] ;
515+
516+
$alias = $this->PfsenseInterface->network_address_aliases_update($name, $type, $cidr_addresses, $descr);
517+
518+
if (empty($alias)) {
519+
$this->response->http_code = 500;
520+
$this->response->message = 'unable to get address aliases';
521+
return FALSE;
522+
}
523+
$this->response->http_code = 200;
524+
$this->response->message = 'ok';
525+
$this->response->data = array(
526+
'aliases' => $alias,
527+
);
528+
return TRUE;
529+
}
502530

503531
/**
504532
* alias_update_urltables()

pfSense-pkg-FauxAPI/files/etc/inc/fauxapi/fauxapi_pfsense_interface_alias.inc

Lines changed: 57 additions & 140 deletions
Original file line numberDiff line numberDiff line change
@@ -2,108 +2,9 @@
22

33
namespace fauxapi\v1;
44

5-
// write_config requires functions from this
6-
include '/etc/inc/phpsessionmanager.inc';
7-
include '/etc/inc/auth.inc';
5+
include 'fauxapi_pfsense_interface_alias.priv.inc';
86
trait network_address_aliases
97
{
10-
11-
/*
12-
From PFSENSE. copyed for old version.
13-
If $return_message is true then
14-
returns a text message about the reason that the name is invalid.
15-
the text includes the type of "thing" that is being checked, passed in $object. (e.g. "alias", "gateway group", "schedule")
16-
else
17-
returns true if $name is a valid name for an alias
18-
returns false if $name is not a valid name for an alias
19-
20-
Aliases cannot be:
21-
bad chars: anything except a-z 0-9 and underscore
22-
bad names: empty string, pure numeric, pure underscore
23-
reserved words: pre-defined service/protocol/port names which should not be ambiguous, and the words "port" and "pass" */
24-
25-
private function is_valid_network_address_alias_name($name)
26-
{
27-
global $config;
28-
global $pf_reserved_keywords;
29-
$reserved_table_names = array(
30-
"bogons",
31-
"bogonsv6",
32-
"negate_networks",
33-
"snort2c",
34-
"sshguard",
35-
"tonatsubnets",
36-
"virusprot",
37-
"vpn_networks",
38-
);
39-
$reserved_ifs = get_configured_interface_list(true);
40-
$pf_reserved_keywords = array_merge($pf_reserved_keywords, $reserved_ifs, $reserved_table_names);
41-
42-
$object = "alias";
43-
/* Array of reserved words */
44-
$reserved = array("port", "pass");
45-
46-
if (!is_string($name) || strlen($name) >= 32 || preg_match('/(^_*$|^\d*$|[^a-z0-9_])/i', $name)) {
47-
return sprintf(gettext('The %1$s name must be less than 32 characters long, may not consist of only numbers, may not consist of only underscores, and may only contain the following characters: %2$s'), $object, 'a-z, A-Z, 0-9, _');
48-
}
49-
if (in_array($name, $reserved, true)) {
50-
return sprintf(gettext('The %1$s name must not be either of the reserved words %2$s or %3$s.'), $object, "'port'", "'pass'");
51-
}
52-
if (getprotobyname($name)) {
53-
return sprintf(gettext('The %1$s name must not be an IP protocol name such as TCP, UDP, ICMP etc.'), $object);
54-
}
55-
if (getservbyname($name, "tcp") || getservbyname($name, "udp")) {
56-
return sprintf(gettext('The %1$s name must not be a well-known or registered TCP or UDP port name such as ssh, smtp, pop3, tftp, http, openvpn etc.'), $object);
57-
}
58-
59-
/* Check for reserved keyword names */
60-
foreach ($pf_reserved_keywords as $rk) {
61-
if (strcasecmp($rk, $name) == 0) {
62-
return sprintf(gettext("Cannot use a reserved keyword as an alias name: %s"), $rk);
63-
}
64-
}
65-
66-
/*
67-
* Packages (e.g. tinc) create interface groups, reserve this
68-
* namespace pkg_ for them.
69-
* One namespace is shared by Interfaces, Interface Groups and Aliases.
70-
*/
71-
if (substr($name, 0, 4) == 'pkg_') {
72-
return gettext("The alias name cannot start with pkg_");
73-
}
74-
75-
/* check for name interface description conflicts */
76-
foreach ($config['interfaces'] as $interface) {
77-
if (strcasecmp($interface['descr'], $name) == 0) {
78-
return gettext("An interface description with this name already exists.");
79-
}
80-
}
81-
82-
/* Is the description already used as an interface group name? */
83-
if (is_array($config['ifgroups']['ifgroupentry'])) {
84-
foreach ($config['ifgroups']['ifgroupentry'] as $ifgroupentry) {
85-
if ($ifgroupentry['ifname'] == $name) {
86-
return gettext("Sorry, an interface group with this name already exists.");
87-
}
88-
}
89-
}
90-
91-
// SHELL NOT BE A IP address. for prevent infinite loops make sure the alias name does not equal the value
92-
if (is_ipaddr($name)) {
93-
return "name shell not be ip address";
94-
}
95-
// CHECK NAME
96-
foreach ($config["aliases"]["alias"] as $cfgitem) {
97-
if ($cfgitem["name"] == $name) {
98-
return gettext("An alias with this name already exists.");
99-
}
100-
}
101-
102-
return NULL;
103-
}
104-
105-
106-
1078
/**
1089
* network_address_aliases_get()
10910
*
@@ -138,7 +39,7 @@ trait network_address_aliases
13839
fauxApiLogger::error($error_message, $error_data);
13940
throw new \Exception($error_message);
14041
}
141-
$error_message = $this->is_valid_network_address_alias_name($name);
42+
$error_message = fauxApiInterfaceAliasTools::is_valid_network_address_alias_name($name);
14243
if ($error_message !== NULL) {
14344
$error_data = array('name' => $name);
14445
fauxApiLogger::error($error_message, $error_data);
@@ -150,69 +51,85 @@ trait network_address_aliases
15051
fauxApiLogger::error($error_message, $error_data);
15152
throw new \Exception($error_message);
15253
}
153-
$address_cfg = array();
154-
$details_cfg = array();
155-
foreach ($cidr_addresses as $addresscfg) {
156-
$address = $addresscfg["address"];
157-
if (!is_ipaddr($address) && !is_subnet($address)) {
158-
$error_message = "must be a address or subnet";
159-
$error_data = array('address' => $addresscfg);
160-
fauxApiLogger::error($error_message, $error_data);
161-
throw new \Exception($error_message);
162-
}
163-
$details = $addresscfg["details"];
164-
$details = preg_replace('/\|\|+/', '|', trim($details, "|"));
165-
array_push($address_cfg, $address);
166-
array_push($details_cfg, $details);
167-
}
168-
$address_cfg = join(" ", $address_cfg);
169-
$details_cfg = join("||", $details_cfg);
54+
$result = fauxApiInterfaceAliasTools::parse_cidr_addresslist_to_config($cidr_addresses);
17055
# this shell saves to config.
17156
$pconfig['name'] = $name;
17257
$pconfig['descr'] = $descr;
17358
$pconfig['type'] = $type;
174-
$pconfig['address'] = $address_cfg;
175-
$pconfig['detail'] = $details_cfg;
59+
$pconfig['address'] = $result["address"];
60+
$pconfig['detail'] = $result["detail"];
17661
init_config_arr(array('aliases', 'alias'));
17762
$currsize = count($config['aliases']['alias']);
17863
$config["aliases"]['alias'][$currsize] = $pconfig;
179-
$session_item = $_SESSION['Username'];
180-
$_SESSION['Username'] = "admin"; // workaround to make writeconfig work
181-
if (write_config(gettext("Edited a firewall alias."))) {
182-
mark_subsystem_dirty('aliases');
183-
$_SESSION['Username'] = $session_item;
184-
} else {
185-
$_SESSION['Username'] = $session_item;
186-
$error_message = "must be a address or subnet";
187-
$error_data = array('address' => $addresscfg);
188-
fauxApiLogger::error($error_message, $error_data);
189-
throw new \Exception($error_message);
64+
if (!fauxApiInterfaceAliasTools::write_config_aliases()) {
65+
return NULL;
19066
}
191-
19267
return $config["aliases"];
19368
}
19469

19570
/**
196-
* network_address_aliases_get()
71+
* network_address_aliases_update()
19772
*
73+
* @param string $name - which alias to modify
74+
* @param string $type - alias type, supports "network" only
75+
* @param array $cidr_addresses - alias CIDRAddress {"address": "1.2.3.4/32", "details":"message"}
76+
* @param string $descr - alias descr, use for UI item.
19877
* @return array
19978
*/
200-
public function network_address_aliases_update()
79+
public function network_address_aliases_update($name, $type, $cidr_addresses, $descr = "Added by fauxapi")
20180
{
20281
global $config;
203-
fauxApiLogger::debug(__METHOD__);
82+
fauxApiLogger::debug(__METHOD__, array(
83+
'name' => $name, 'type' => $type, 'cidr_addresses' => $cidr_addresses, 'descr' => $descr
84+
));
85+
$pconfig=NULL;
86+
for ($id = 0; $id < count($config["aliases"]["alias"]); $id+=1) {
87+
$cfgitem = $config["aliases"]["alias"][$id];
88+
if ($cfgitem["name"] == $name){
89+
$pconfig=&$config["aliases"]["alias"][$id];
90+
}
91+
}
92+
if ($pconfig== NULL){
93+
//not find
94+
$error_message = "not find name";
95+
$error_data = array('name' => $name);
96+
fauxApiLogger::error($error_message, $error_data);
97+
throw new \Exception($error_message);
98+
}
99+
if ($type != "network") {
100+
$error_message = "can support type network only for now";
101+
$error_data = array('type' => $type);
102+
fauxApiLogger::error($error_message, $error_data);
103+
throw new \Exception($error_message);
104+
}
105+
$result = fauxApiInterfaceAliasTools::parse_cidr_addresslist_to_config($cidr_addresses);
106+
# this shell saves to config.
107+
$pconfig['name'] = $name;
108+
$pconfig['descr'] = $descr;
109+
$pconfig['type'] = $type;
110+
$pconfig['address'] = $result["address"];
111+
$pconfig['detail'] = $result["detail"];
112+
if (!fauxApiInterfaceAliasTools::write_config_aliases()) {
113+
return NULL;
114+
}
115+
204116
return $config["aliases"];
205117
}
206-
207118
/**
208-
* network_address_aliases_get()
119+
* network_address_aliases_delete()
209120
*
121+
* @param string $name - which alias to modify
210122
* @return array
211123
*/
212-
public function network_address_aliases_delete()
124+
public function network_address_aliases_delete($name)
213125
{
214126
global $config;
215-
fauxApiLogger::debug(__METHOD__);
216-
return $config["aliases"];
127+
fauxApiLogger::debug(__METHOD__, array(
128+
'name' => $name
129+
));
130+
$pconfig=NULL;
131+
for ($id = 0; $id < count($config["aliases"]["alias"]); $id+=1) {
132+
}
133+
217134
}
218135
}

0 commit comments

Comments
 (0)