22
33namespace 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 ' ;
86trait 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