Project

General

Profile

Download (22.1 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
 * alias-utils.inc
4
 *
5
 * part of pfSense (https://www.pfsense.org)
6
 * Copyright (c) 2004-2013 BSD Perimeter
7
 * Copyright (c) 2013-2016 Electric Sheep Fencing
8
 * Copyright (c) 2014-2021 Rubicon Communications, LLC (Netgate)
9
 * All rights reserved.
10
 *
11
 * originally based on m0n0wall (http://m0n0.ch/wall)
12
 * Copyright (c) 2003-2004 Manuel Kasper <mk@neon1.net>.
13
 * All rights reserved.
14
 *
15
 * Licensed under the Apache License, Version 2.0 (the "License");
16
 * you may not use this file except in compliance with the License.
17
 * You may obtain a copy of the License at
18
 *
19
 * http://www.apache.org/licenses/LICENSE-2.0
20
 *
21
 * Unless required by applicable law or agreed to in writing, software
22
 * distributed under the License is distributed on an "AS IS" BASIS,
23
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24
 * See the License for the specific language governing permissions and
25
 * limitations under the License.
26
 */
27

    
28
require_once("config.gui.inc");
29
require_once("functions.inc");
30
require_once("filter.inc");
31
require_once("shaper.inc");
32

    
33
init_config_arr(array('aliases', 'alias'));
34
$a_aliases = &$config['aliases']['alias'];
35

    
36
function deleteAlias($id, $apply = false) {
37
	global $config;
38

    
39
	/* make sure rule is not being referenced by any nat or filter rules */
40
	init_config_arr(array('aliases', 'alias'));
41
	$a_aliases = &$config['aliases']['alias'];
42

    
43
	$delete_error = "";
44
	$is_alias_referenced = false;
45
	$referenced_by = false;
46
	$alias_name = $a_aliases[$id]['name'];
47
	// Firewall rules
48
	find_alias_reference(array('filter', 'rule'), array('source', 'address'), $alias_name, $is_alias_referenced, $referenced_by);
49
	find_alias_reference(array('filter', 'rule'), array('destination', 'address'), $alias_name, $is_alias_referenced, $referenced_by);
50
	find_alias_reference(array('filter', 'rule'), array('source', 'port'), $alias_name, $is_alias_referenced, $referenced_by);
51
	find_alias_reference(array('filter', 'rule'), array('destination', 'port'), $alias_name, $is_alias_referenced, $referenced_by);
52
	// NAT Rules
53
	find_alias_reference(array('nat', 'rule'), array('source', 'address'), $alias_name, $is_alias_referenced, $referenced_by);
54
	find_alias_reference(array('nat', 'rule'), array('source', 'port'), $alias_name, $is_alias_referenced, $referenced_by);
55
	find_alias_reference(array('nat', 'rule'), array('destination', 'address'), $alias_name, $is_alias_referenced, $referenced_by);
56
	find_alias_reference(array('nat', 'rule'), array('destination', 'port'), $alias_name, $is_alias_referenced, $referenced_by);
57
	find_alias_reference(array('nat', 'rule'), array('target'), $alias_name, $is_alias_referenced, $referenced_by);
58
	find_alias_reference(array('nat', 'rule'), array('local-port'), $alias_name, $is_alias_referenced, $referenced_by);
59
	// NAT 1:1 Rules
60
	find_alias_reference(array('nat', 'onetoone'), array('destination', 'address'), $alias_name, $is_alias_referenced, $referenced_by);
61
	// NAT Outbound Rules
62
	find_alias_reference(array('nat', 'outbound', 'rule'), array('source', 'network'), $alias_name, $is_alias_referenced, $referenced_by);
63
	find_alias_reference(array('nat', 'outbound', 'rule'), array('sourceport'), $alias_name, $is_alias_referenced, $referenced_by);
64
	find_alias_reference(array('nat', 'outbound', 'rule'), array('destination', 'address'), $alias_name, $is_alias_referenced, $referenced_by);
65
	find_alias_reference(array('nat', 'outbound', 'rule'), array('dstport'), $alias_name, $is_alias_referenced, $referenced_by);
66
	find_alias_reference(array('nat', 'outbound', 'rule'), array('target'), $alias_name, $is_alias_referenced, $referenced_by);
67
	// Alias in an alias
68
	find_alias_reference(array('aliases', 'alias'), array('address'), $alias_name, $is_alias_referenced, $referenced_by);
69
	// Static routes
70
	find_alias_reference(array('staticroutes', 'route'), array('network'), $alias_name, $is_alias_referenced, $referenced_by);
71
	if ($is_alias_referenced == true) {
72
		$delete_error = sprintf(gettext("Cannot delete alias. Currently in use by %s."), htmlspecialchars($referenced_by));
73
	} else {
74
		if (preg_match("/urltable/i", $a_aliases[$id]['type'])) {
75
			// this is a URL table type alias, delete its file as well
76
			unlink_if_exists("/var/db/aliastables/" . $a_aliases[$id]['name'] . ".txt");
77
		}
78
		unset($a_aliases[$id]);
79
		if (write_config(gettext("Deleted firewall alias " . $a_aliases[$id]['name']))) {
80
			filter_configure();
81

    
82
			if (!$apply) {
83
				mark_subsystem_dirty('aliases');
84
			} 
85
		}
86
	}
87

    
88
	return $delete_error;
89
}
90

    
91
function find_alias_reference($section, $field, $origname, &$is_alias_referenced, &$referenced_by) {
92
	global $config;
93
	if (!$origname || $is_alias_referenced) {
94
		return;
95
	}
96

    
97
	$sectionref = &$config;
98
	foreach ($section as $sectionname) {
99
		if (is_array($sectionref) && isset($sectionref[$sectionname])) {
100
			$sectionref = &$sectionref[$sectionname];
101
		} else {
102
			return;
103
		}
104
	}
105

    
106
	if (is_array($sectionref)) {
107
		foreach ($sectionref as $itemkey => $item) {
108
			$fieldfound = true;
109
			$fieldref = &$sectionref[$itemkey];
110
			foreach ($field as $fieldname) {
111
				if (is_array($fieldref) && isset($fieldref[$fieldname])) {
112
					$fieldref = &$fieldref[$fieldname];
113
				} else {
114
					$fieldfound = false;
115
					break;
116
				}
117
			}
118
			/* explode() alias address string, see https://redmine.pfsense.org/issues/11372 */
119
			if ($fieldfound && in_array($origname, explode(' ', $fieldref))) {
120
				$is_alias_referenced = true;
121
				if (is_array($item)) {
122
					$referenced_by = $item['descr'];
123
				}
124
				break;
125
			}
126
		}
127
	}
128
}
129

    
130
function alias_same_type($name, $type) {
131
	global $config;
132

    
133
	foreach ($config['aliases']['alias'] as $alias) {
134
		if ($name == $alias['name']) {
135
			if (in_array($type, array("host", "network")) &&
136
			    in_array($alias['type'], array("host", "network"))) {
137
				return true;
138
			}
139

    
140
			if ($type == $alias['type']) {
141
				return true;
142
			} else {
143
				return false;
144
			}
145
		}
146
	}
147
	return true;
148
}
149

    
150
function saveAlias($post, $id) {
151
	global $config, $pf_reserved_keywords, $reserved_table_names, $pconfig;
152

    
153
	$reserved_ifs = get_configured_interface_list(true);
154
	$pf_reserved_keywords = array_merge($pf_reserved_keywords, $reserved_ifs, $reserved_table_names);
155
	$max_alias_addresses = 5000;
156

    
157
	init_config_arr(array('aliases', 'alias'));
158
	$a_aliases = &$config['aliases']['alias'];
159

    
160
	$data_errors = array();
161

    
162
	$vertical_bar_err_text = gettext("Vertical bars (|) at start or end, or double in the middle of descriptions not allowed. Descriptions have been cleaned. Check and save again.");
163

    
164
	/* input validation */
165

    
166
	$reqdfields = explode(" ", "name");
167
	$reqdfieldsn = array(gettext("Name"));
168

    
169
	do_local_input_validation($post, $reqdfields, $reqdfieldsn, $data_errors);
170

    
171
	if (!is_validaliasname($post['name'])) {
172
		$data_errors[] = invalidaliasnamemsg($post['name']);
173
	}
174

    
175
	/* check for name conflicts */
176
	foreach ($a_aliases as $key => $alias) {
177
		if (($alias['name'] == $post['name']) && (empty($a_aliases[$id]) || ($key != $id))) {
178
			$data_errors[] = gettext("An alias with this name already exists.");
179
			break;
180
		}
181
	}
182

    
183
	/* Check for reserved keyword names */
184
	foreach ($pf_reserved_keywords as $rk) {
185
		if (strcasecmp($rk, $post['name']) == 0) {
186
			$data_errors[] = sprintf(gettext("Cannot use a reserved keyword as an alias name: %s"), $rk);
187
		}
188
	}
189

    
190
	/*
191
	 * Packages (e.g. tinc) create interface groups, reserve this
192
	 * namespace pkg_ for them.
193
	 * One namespace is shared by Interfaces, Interface Groups and Aliases.
194
	 */
195
	if (substr($post['name'], 0, 4) == 'pkg_') {
196
		$data_errors[] = gettext("The alias name cannot start with pkg_");
197
	}
198

    
199
	/* check for name interface description conflicts */
200
	foreach ($config['interfaces'] as $interface) {
201
		if (strcasecmp($interface['descr'], $post['name']) == 0) {
202
			$data_errors[] = gettext("An interface description with this name already exists.");
203
			break;
204
		}
205
	}
206

    
207
	/* Is the description already used as an interface group name? */
208
	if (is_array($config['ifgroups']['ifgroupentry'])) {
209
		foreach ($config['ifgroups']['ifgroupentry'] as $ifgroupentry) {
210
			if ($ifgroupentry['ifname'] == $post['name']) {
211
				$data_errors[] = gettext("Sorry, an interface group with this name already exists.");
212
			}
213
		}
214
	}
215

    
216
	/* To prevent infinite loops make sure the alias name does not equal the value. */
217
	for($i = 0; isset($post['address' . $i]); $i++) {
218
			if($post['address' . $i] == $post['name']){
219
				$data_errors[] = gettext("Alias value cannot be the same as the alias name: `" . $post['name'] . " and " . $post['address' . $i] . "`");
220
			}
221
	}
222

    
223
	$alias = array();
224
	$address = array();
225
	$final_address_details = array();
226
	$alias['name'] = $post['name'];
227
	$alias['type'] = $post['type'];
228

    
229
	if (preg_match("/urltable/i", $post['type'])) {
230
		$address = array();
231

    
232
		/* item is a url table type */
233
		if ($post['address0']) {
234
			/* fetch down and add in */
235
			$address[] = $post['address0'];
236
			$alias['url'] = $post['address0'];
237
			$alias['updatefreq'] = $post['address_subnet0'] ? $post['address_subnet0'] : 7;
238
			if (!is_URL($alias['url']) || empty($alias['url'])) {
239
				$data_errors[] = gettext("A valid URL must be provided.");
240
			} elseif (!process_alias_urltable($alias['name'], $alias['type'], $alias['url'], 0, true, true)) {
241
				$data_errors[] = sprintf(gettext("Unable to fetch usable data from URL %s"), htmlspecialchars($alias['url']));
242
			}
243
			if ($post["detail0"] <> "") {
244
				if ((strpos($post["detail0"], "||") === false) && (substr($post["detail0"], 0, 1) != "|") && (substr($post["detail0"], -1, 1) != "|")) {
245
					$final_address_details[] = $post["detail0"];
246
				} else {
247
					/* Remove leading and trailing vertical bars and replace multiple vertical bars with single, */
248
					/* and put in the output array so the text is at least redisplayed for the user. */
249
					$final_address_details[] = preg_replace('/\|\|+/', '|', trim($post["detail0"], "|"));
250
					$data_errors[] = $vertical_bar_err_text;
251
				}
252
			} else {
253
				$final_address_details[] = sprintf(gettext("Entry added %s"), date('r'));
254
			}
255
		}
256
	} elseif (($post['type'] == "url") || ($post['type'] == "url_ports")) {
257
		$desc_fmt_err_found = false;
258

    
259
		/* item is a url type */
260
		for ($x = 0; $x < $max_alias_addresses - 1; $x++) {
261
			if ($post['address' . $x]) {
262
				/* fetch down and add in */
263
				$temp_filename = tempnam("{$g['tmp_path']}/", "alias_import");
264
				unlink_if_exists($temp_filename);
265
				$verify_ssl = isset($config['system']['checkaliasesurlcert']);
266
				mkdir($temp_filename);
267
				if (!download_file($post['address' . $x], $temp_filename . "/aliases", $verify_ssl)) {
268
					$data_errors[] = sprintf(gettext("Could not fetch the URL '%s'."), $post['address' . $x]);
269
					continue;
270
				}
271

    
272
				/* if the item is tar gzipped then extract */
273
				if (stristr($post['address' . $x], ".tgz")) {
274
					process_alias_tgz($temp_filename);
275
				}
276

    
277
				if (!isset($alias['aliasurl'])) {
278
					$alias['aliasurl'] = array();
279
				}
280

    
281
				$alias['aliasurl'][] = $post['address' . $x];
282
				if ($post["detail{$x}"] <> "") {
283
					if ((strpos($post["detail{$x}"], "||") === false) && (substr($post["detail{$x}"], 0, 1) != "|") && (substr($post["detail{$x}"], -1, 1) != "|")) {
284
						$final_address_details[] = $post["detail{$x}"];
285
					} else {
286
						/* Remove leading and trailing vertical bars and replace multiple vertical bars with single, */
287
						/* and put in the output array so the text is at least redisplayed for the user. */
288
						$final_address_details[] = preg_replace('/\|\|+/', '|', trim($post["detail{$x}"], "|"));
289
						if (!$desc_fmt_err_found) {
290
							$data_errors[] = $vertical_bar_err_text;
291
							$desc_fmt_err_found = true;
292
						}
293
					}
294
				} else {
295
					$final_address_details[] = sprintf(gettext("Entry added %s"), date('r'));
296
				}
297

    
298
				if (file_exists("{$temp_filename}/aliases")) {
299
					$t_address = parse_aliases_file("{$temp_filename}/aliases", $post['type'], 5000);
300
					if ($t_address == null) {
301
						/* nothing was found */
302
						$data_errors[] = sprintf(gettext("A valid URL must be provided. Could not fetch usable data from '%s'."), $post['address' . $x]);
303
					} else {
304
						array_push($address, ...$t_address);
305
					}
306
					unset($t_address);
307
					mwexec("/bin/rm -rf " . escapeshellarg($temp_filename));
308
				} else {
309
					$data_errors[] = sprintf(gettext("URL '%s' is not valid."), $post['address' . $x]);
310
				}
311
			}
312
		}
313
		unset($desc_fmt_err_found);
314
		if ($post['type'] == "url_ports") {
315
			$address = group_ports($address);
316
		}
317
	} else {
318
		/* item is a normal alias type */
319
		$wrongaliases = "";
320
		$desc_fmt_err_found = false;
321
		$alias_address_count = 0;
322
		$input_addresses = array();
323

    
324
		// First trim and expand the input data.
325
		// Users can paste strings like "10.1.2.0/24 10.3.0.0/16 9.10.11.0/24" into an address box.
326
		// They can also put an IP range.
327
		// This loop expands out that stuff so it can easily be validated.
328
		for ($x = 0; $x < ($max_alias_addresses - 1); $x++) {
329
			if ($post["address{$x}"] <> "") {
330
				if ($post["detail{$x}"] <> "") {
331
					if ((strpos($post["detail{$x}"], "||") === false) && (substr($post["detail{$x}"], 0, 1) != "|") && (substr($post["detail{$x}"], -1, 1) != "|")) {
332
						$detail_text = $post["detail{$x}"];
333
					} else {
334
						/* Remove leading and trailing vertical bars and replace multiple vertical bars with single, */
335
						/* and put in the output array so the text is at least redisplayed for the user. */
336
						$detail_text = preg_replace('/\|\|+/', '|', trim($post["detail{$x}"], "|"));
337
						if (!$desc_fmt_err_found) {
338
							$data_errors[] = $vertical_bar_err_text;
339
							$desc_fmt_err_found = true;
340
						}
341
					}
342
				} else {
343
					$detail_text = sprintf(gettext("Entry added %s"), date('r'));
344
				}
345

    
346
				$address_items = explode(" ", trim(alias_idn_to_ascii($post["address{$x}"])));
347
				foreach ($address_items as $address_item) {
348
					$iprange_type = is_iprange($address_item);
349
					if ($iprange_type == 4) {
350
						list($startip, $endip) = explode('-', $address_item);
351
						if ($post['type'] == "network") {
352
							// For network type aliases, expand an IPv4 range into an array of subnets.
353
							$rangesubnets = ip_range_to_subnet_array($startip, $endip);
354
							foreach ($rangesubnets as $rangesubnet) {
355
								if ($alias_address_count > $max_alias_addresses) {
356
									break;
357
								}
358
								list($address_part, $subnet_part) = explode("/", $rangesubnet);
359
								$input_addresses[] = $address_part;
360
								$input_address_subnet[] = $subnet_part;
361
								$final_address_details[] = $detail_text;
362
								$alias_address_count++;
363
							}
364
						} else {
365
							// For host type aliases, expand an IPv4 range into a list of individual IPv4 addresses.
366
							$rangeaddresses = ip_range_to_address_array($startip, $endip, $max_alias_addresses - $alias_address_count);
367
							if (is_array($rangeaddresses)) {
368
								foreach ($rangeaddresses as $rangeaddress) {
369
									$input_addresses[] = $rangeaddress;
370
									$input_address_subnet[] = "";
371
									$final_address_details[] = $detail_text;
372
									$alias_address_count++;
373
								}
374
							} else {
375
								$data_errors[] = sprintf(gettext('Range is too large to expand into individual host IP addresses (%s)'), $address_item);
376
								$data_errors[] = sprintf(gettext('The maximum number of entries in an alias is %s'), $max_alias_addresses);
377
								// Put the user-entered data in the output anyway, so it will be re-displayed for correction.
378
								$input_addresses[] = $address_item;
379
								$input_address_subnet[] = "";
380
								$final_address_details[] = $detail_text;
381
							}
382
						}
383
					} else if ($iprange_type == 6) {
384
						$data_errors[] = sprintf(gettext('IPv6 address ranges are not supported (%s)'), $address_item);
385
						// Put the user-entered data in the output anyway, so it will be re-displayed for correction.
386
						$input_addresses[] = $address_item;
387
						$input_address_subnet[] = "";
388
						$final_address_details[] = $detail_text;
389
					} else {
390
						$subnet_type = is_subnet($address_item);
391
						if (($post['type'] == "host") && $subnet_type) {
392
							if ($subnet_type == 4) {
393
								// For host type aliases, if the user enters an IPv4 subnet, expand it into a list of individual IPv4 addresses.
394
								$subnet_size = subnet_size($address_item);
395
								if ($subnet_size > 0 &&
396
								    $subnet_size <= ($max_alias_addresses - $alias_address_count)) {
397
									$rangeaddresses = subnetv4_expand($address_item);
398
									foreach ($rangeaddresses as $rangeaddress) {
399
										$input_addresses[] = $rangeaddress;
400
										$input_address_subnet[] = "";
401
										$final_address_details[] = $detail_text;
402
										$alias_address_count++;
403
									}
404
								} else {
405
									$data_errors[] = sprintf(gettext('Subnet is too large to expand into individual host IP addresses (%s)'), $address_item);
406
									$data_errors[] = sprintf(gettext('The maximum number of entries in an alias is %s'), $max_alias_addresses);
407
									// Put the user-entered data in the output anyway, so it will be re-displayed for correction.
408
									$input_addresses[] = $address_item;
409
									$input_address_subnet[] = "";
410
									$final_address_details[] = $detail_text;
411
								}
412
							} else {
413
								$data_errors[] = sprintf(gettext('IPv6 subnets are not supported in host aliases (%s)'), $address_item);
414
								// Put the user-entered data in the output anyway, so it will be re-displayed for correction.
415
								$input_addresses[] = $address_item;
416
								$input_address_subnet[] = "";
417
								$final_address_details[] = $detail_text;
418
							}
419
						} else {
420
							list($address_part, $subnet_part) = explode("/", $address_item);
421
							if (!empty($subnet_part)) {
422
								if (is_subnet($address_item)) {
423
									$input_addresses[] = $address_part;
424
									$input_address_subnet[] = $subnet_part;
425
								} else {
426
									// The user typed something like "1.2.3.444/24" or "1.2.3.0/36" or similar rubbish.
427
									// Feed it through without splitting it apart, then it will be caught by the validation loop below.
428
									$input_addresses[] = $address_item;
429
									$input_address_subnet[] = "";
430
								}
431
							} else {
432
								$input_addresses[] = $address_part;
433
								$input_address_subnet[] = $post["address_subnet{$x}"];
434
							}
435
							$final_address_details[] = $detail_text;
436
							$alias_address_count++;
437
						}
438
					}
439
					if ($alias_address_count > $max_alias_addresses) {
440
						$data_errors[] = sprintf(gettext('The maximum number of entries in an alias has been exceeded (%s)'), $max_alias_addresses);
441
						break;
442
					}
443
				}
444
			}
445
		}
446

    
447
		// Validate the input data expanded above.
448
		foreach ($input_addresses as $idx => $input_address) {
449
			if (is_alias($input_address)) {
450
				if (!alias_same_type($input_address, $post['type'])) {
451
					// But alias type network can include alias type urltable. Feature#1603.
452
					if (!($post['type'] == 'network' &&
453
					    preg_match("/urltable/i", alias_get_type($input_address)))) {
454
						$wrongaliases .= " " . $input_address;
455
					}
456
				}
457
			} else if ($post['type'] == "port") {
458
				if (!is_port_or_range($input_address)) {
459
					$data_errors[] = sprintf(gettext("%s is not a valid port or alias."), $input_address);
460
				}
461
			} else if (($post['type'] == "host") || ($post['type'] == "network")) {
462
				if (is_subnet($input_address) ||
463
				    (!is_ipaddr($input_address) && !is_hostname($input_address))) {
464
					$data_errors[] = sprintf(gettext('%1$s is not a valid %2$s address, FQDN or alias.'), $input_address, $singular_types[$post['type']]);
465
				}
466
			}
467
			$tmpaddress = $input_address;
468
			if (($post['type'] != "host") && is_ipaddr($input_address) && ($input_address_subnet[$idx] <> "")) {
469
				if (!is_subnet($input_address . "/" . $input_address_subnet[$idx])) {
470
					$data_errors[] = sprintf(gettext('%1$s/%2$s is not a valid subnet.'), $input_address, $input_address_subnet[$idx]);
471
				} else {
472
					$tmpaddress .= "/" . $input_address_subnet[$idx];
473
				}
474
			}
475
			$address[] = addrtolower($tmpaddress);
476
		}
477
		unset($desc_fmt_err_found);
478
		if ($wrongaliases <> "") {
479
			$data_errors[] = sprintf(gettext('The alias(es): %s cannot be nested because they are not of the same type.'), $wrongaliases);
480
		}
481
	}
482

    
483
	unset($vertical_bar_err_text);
484

    
485
	// Allow extending of the firewall edit page and include custom input validation
486
	pfSense_handle_custom_code("/usr/local/pkg/firewall_aliases_edit/input_validation");
487

    
488
	if (!$data_errors) {
489
		$alias['address'] = is_array($address) ? implode(" ", $address) : $address;
490
		$alias['descr'] = $post['descr'];
491
		$alias['type'] = $post['type'];
492
		$alias['detail'] = implode("||", $final_address_details);
493

    
494
		/*	 Check to see if alias name needs to be
495
		 *	 renamed on referenced rules and such
496
		 */
497
		if ($post['name'] <> $origname) {
498
			update_alias_name($post['name'], $origname);
499
		}
500

    
501
		pfSense_handle_custom_code("/usr/local/pkg/firewall_aliases_edit/pre_write_config");
502

    
503
		if (isset($id) && $a_aliases[$id]) {
504
			if ($a_aliases[$id]['name'] <> $alias['name']) {
505
				foreach ($a_aliases as $aliasid => $aliasd) {
506
					if ($aliasd['address'] <> "") {
507
						$tmpdirty = false;
508
						$tmpaddr = explode(" ", $aliasd['address']);
509
						foreach ($tmpaddr as $tmpidx => $tmpalias) {
510
							if ($tmpalias == $a_aliases[$id]['name']) {
511
								$tmpaddr[$tmpidx] = $alias['name'];
512
								$tmpdirty = true;
513
							}
514
						}
515
						if ($tmpdirty == true) {
516
							$a_aliases[$aliasid]['address'] = implode(" ", $tmpaddr);
517
						}
518
					}
519
				}
520
			}
521
			$a_aliases[$id] = $alias;
522
		} else {
523
			$a_aliases[] = $alias;
524
		}
525

    
526
		// Sort list
527
		$a_aliases = msort($a_aliases, "name");
528

    
529
		write_config(gettext("Edited a firewall alias."));
530
	} else if (isset($pconfig)) {
531
		//we received input errors, copy data to prevent retype
532
		$pconfig['name'] = $post['name'];
533
		$pconfig['descr'] = $post['descr'];
534
		if (isset($alias['aliasurl']) && ($post['type'] == 'url') || ($post['type'] == 'url_ports')) {
535
			$pconfig['address'] = implode(" ", $alias['aliasurl']);
536
		} else {
537
			$pconfig['address'] = implode(" ", $address);
538
		}
539
		$pconfig['type'] = $post['type'];
540
		$pconfig['detail'] = implode("||", $final_address_details);
541
	}
542

    
543
	return $data_errors;
544
}
545

    
546
function do_local_input_validation($postdata, $reqdfields, $reqdfieldsn, &$input_errors) {
547

    
548
	/* check for bad control characters */
549
	foreach ($postdata as $pn => $pd) {
550
		if (is_string($pd) && preg_match("/[\\x00-\\x08\\x0b\\x0c\\x0e-\\x1f]/", $pd)) {
551
			$input_errors[] = sprintf(gettext("The field %s contains invalid characters."), $pn);
552
		}
553
	}
554

    
555
	if (is_array($reqdfields)) {
556
		for ($i = 0; $i < count($reqdfields); $i++) {
557
			if ($postdata[$reqdfields[$i]] == "") {
558
				$input_errors[] = sprintf(gettext("The field %s is required."), $reqdfieldsn[$i]);
559
			}
560
		}
561
	}
562
}
563

    
564
?>
(1-1/7)