Project

General

Profile

« Previous | Next » 

Revision cd5d6241

Added by Phil Davis over 11 years ago

Enhanced validation of general DNS servers and gateways

View differences:

usr/local/www/system.php
115 115
		$input_errors[] = gettext("The domain may only contain the characters a-z, 0-9, '-' and '.'.");
116 116
	}
117 117

  
118
	$ignore_posted_dnsgw = array();
119

  
118 120
	for ($dnscounter=1; $dnscounter<5; $dnscounter++){
119 121
		$dnsname="dns{$dnscounter}";
120 122
		$dnsgwname="dns{$dnscounter}gw";
121 123
		if (($_POST[$dnsname] && !is_ipaddr($_POST[$dnsname]))) {
122
			$input_errors[] = gettext("A valid IP address must be specified for the DNS server $dnscounter.");
123
		}
124
		if(($_POST[$dnsgwname] <> "") && (is_ipaddr($_POST[$dnsname]))) {
125
			if (($_POST[$dnsgwname] <> "none") && (is_ipaddrv4($_POST[$dnsname])) && (validate_address_family($_POST[$dnsname], $_POST[$dnsgwname]) === false )) {
126
				$input_errors[] = gettext("You can not specify a IPv6 gateway '{$_POST[$dnsgwname]}'for a IPv4 DNS server '{$_POST[$dnsname]}'");
127
			}
128
			if (($_POST[$dnsgwname] <> "none") && (is_ipaddrv6($_POST[$dnsname])) && (validate_address_family($_POST[$dnsname], $_POST[$dnsgwname]) === false )) {
129
				$input_errors[] = gettext("You can not specify a IPv4 gateway '{$_POST[$dnsgwname]}'for a IPv6 DNS server '{$_POST[$dnsname]}'");
124
			$input_errors[] = gettext("A valid IP address must be specified for DNS server $dnscounter.");
125
		} else {
126
			if(($_POST[$dnsgwname] <> "") && ($_POST[$dnsgwname] <> "none")) {
127
				// A real gateway has been selected.
128
				if (is_ipaddr($_POST[$dnsname])) {
129
					if ((is_ipaddrv4($_POST[$dnsname])) && (validate_address_family($_POST[$dnsname], $_POST[$dnsgwname]) === false )) {
130
						$input_errors[] = gettext("You can not specify IPv6 gateway '{$_POST[$dnsgwname]}' for IPv4 DNS server '{$_POST[$dnsname]}'");
131
					}
132
					if ((is_ipaddrv6($_POST[$dnsname])) && (validate_address_family($_POST[$dnsname], $_POST[$dnsgwname]) === false )) {
133
						$input_errors[] = gettext("You can not specify IPv4 gateway '{$_POST[$dnsgwname]}' for IPv6 DNS server '{$_POST[$dnsname]}'");
134
					}
135
				} else {
136
					// The user selected a gateway but did not provide a DNS address. Be nice and set the gateway back to "none".
137
					$ignore_posted_dnsgw[$dnsgwname] = true;
138
				}
130 139
			}
131 140
		}
132 141
	}
......
155 164
	if (($t < 0) || (($t > 0) && ($t < 6)) || ($t > 1440)) {
156 165
		$input_errors[] = gettext("The time update interval must be either 0 (disabled) or between 6 and 1440.");
157 166
	}
167
	# it's easy to have a little too much whitespace in the field, clean it up for the user before processing.
168
	$_POST['timeservers'] = preg_replace('/[[:blank:]]+/', ' ', $_POST['timeservers']);
169
	$_POST['timeservers'] = trim($_POST['timeservers']);
158 170
	foreach (explode(' ', $_POST['timeservers']) as $ts) {
159 171
		if (!is_domain($ts)) {
160 172
			$input_errors[] = gettext("A NTP Time Server name may only contain the characters a-z, 0-9, '-' and '.'.");
......
180 192
		}
181 193

  
182 194
		/* XXX - billm: these still need updating after figuring out how to check if they actually changed */
195
		$olddnsservers = $config['system']['dnsserver'];
183 196
		unset($config['system']['dnsserver']);
184 197
		if ($_POST['dns1'])
185 198
			$config['system']['dnsserver'][] = $_POST['dns1'];
......
201 214
			unset($config['system']['dnslocalhost']);
202 215

  
203 216
		/* which interface should the dns servers resolve through? */
217
		$outdnscounter = 0;
204 218
		for ($dnscounter=1; $dnscounter<5; $dnscounter++) {
205 219
			$dnsname="dns{$dnscounter}";
206 220
			$dnsgwname="dns{$dnscounter}gw";
207
			if($_POST[$dnsgwname]) {
208
				$config['system'][$dnsgwname] = $pconfig[$dnsgwname];
209
			} else {
210
				unset($config['system'][$dnsgwname]);
221
			$olddnsgwname = $config['system'][$dnsgwname];
222

  
223
			if ($ignore_posted_dnsgw[$dnsgwname])
224
				$thisdnsgwname = "none";
225
			else
226
				$thisdnsgwname = $pconfig[$dnsgwname];
227

  
228
			// "Blank" out the settings for this index, then we set them below using the "outdnscounter" index.
229
			$config['system'][$dnsgwname] = "none";
230
			$pconfig[$dnsgwname] = "none";
231
			$pconfig[$dnsname] = "";
232

  
233
			if ($_POST[$dnsname]) {
234
				// Only the non-blank DNS servers were put into the config above.
235
				// So we similarly only add the corresponding gateways sequentially to the config (and to pconfig), as we find non-blank DNS servers.
236
				// This keeps the DNS server IP and corresponding gateway "lined up" when the user blanks out a DNS server IP in the middle of the list.
237
				$outdnscounter++;
238
				$outdnsname="dns{$outdnscounter}";
239
				$outdnsgwname="dns{$outdnscounter}gw";
240
				$pconfig[$outdnsname] = $_POST[$dnsname];
241
				if($_POST[$dnsgwname]) {
242
					$config['system'][$outdnsgwname] = $thisdnsgwname;
243
					$pconfig[$outdnsgwname] = $thisdnsgwname;
244
				} else {
245
					// Note: when no DNS GW name is chosen, the entry is set to "none", so actually this case never happens.
246
					unset($config['system'][$outdnsgwname]);
247
					$pconfig[$outdnsgwname] = "";
248
				}
249
			}
250
			if (($olddnsgwname != "") && ($olddnsgwname != "none") && (($olddnsgwname != $thisdnsgwname) || ($olddnsservers[$dnscounter-1] != $_POST[$dnsname]))) {
251
				// A previous DNS GW name was specified. It has now gone or changed, or the DNS server address has changed.
252
				// Remove the route. Later calls will add the correct new route if needed.
253
				if (is_ipaddrv4($olddnsservers[$dnscounter-1]))
254
					mwexec("/sbin/route delete " . escapeshellarg($olddnsservers[$dnscounter-1]));
255
				else
256
					if (is_ipaddrv6($olddnsservers[$dnscounter-1]))
257
						mwexec("/sbin/route delete -inet6 " . escapeshellarg($olddnsservers[$dnscounter-1]));
211 258
			}
212 259
		}
213 260

  
......
230 277
		
231 278
		$savemsg = get_std_save_message($retval);
232 279
	}
280

  
281
	unset($ignore_posted_dnsgw);
233 282
}
234 283

  
235 284
$pgtitle = array(gettext("System"),gettext("General Setup"));

Also available in: Unified diff