Project

General

Profile

« Previous | Next » 

Revision d88f26df

Added by Phil Davis over 8 years ago

Redmine #5549 Allow variable number of DNS Servers

(cherry picked from commit a2d23e88596deab6bbed2818385a0b72c913843a)

View differences:

src/usr/local/www/system.php
70 70

  
71 71
$pconfig['hostname'] = $config['system']['hostname'];
72 72
$pconfig['domain'] = $config['system']['domain'];
73
list($pconfig['dns1'], $pconfig['dns2'], $pconfig['dns3'], $pconfig['dns4']) = $config['system']['dnsserver'];
73
$pconfig['dnsserver'] = $config['system']['dnsserver'];
74 74

  
75 75
$arr_gateways = return_gateways_array();
76 76

  
......
79 79
	$config['system']['webgui']['dashboardcolumns'] = 2;
80 80
}
81 81

  
82
$pconfig['dns1gw'] = $config['system']['dns1gw'];
83
$pconfig['dns2gw'] = $config['system']['dns2gw'];
84
$pconfig['dns3gw'] = $config['system']['dns3gw'];
85
$pconfig['dns4gw'] = $config['system']['dns4gw'];
82
$dnsgw_counter = 1;
83

  
84
while (isset($config["system"]["dns{$dnsgw_counter}gw"])) {
85
	$pconfig_dnsgw_counter = $dnsgw_counter - 1;
86
	$pconfig["dnsgw{$pconfig_dnsgw_counter}"] = $config["system"]["dns{$dnsgw_counter}gw"];
87
	$dnsgw_counter++;
88
}
86 89

  
87 90
$pconfig['dnsallowoverride'] = isset($config['system']['dnsallowoverride']);
88 91
$pconfig['timezone'] = $config['system']['timezone'];
......
227 230

  
228 231
	$dnslist = $ignore_posted_dnsgw = array();
229 232

  
230
	for ($dnscounter=1; $dnscounter<5; $dnscounter++) {
231
		$dnsname="dns{$dnscounter}";
232
		$dnsgwname="dns{$dnscounter}gw";
233
	$dnscounter = 0;
234
	$dnsname = "dns{$dnscounter}";
235

  
236
	while (isset($_POST[$dnsname])) {
237
		$dnsgwname = "dnsgw{$dnscounter}";
233 238
		$dnslist[] = $_POST[$dnsname];
234 239

  
235 240
		if (($_POST[$dnsname] && !is_ipaddr($_POST[$dnsname]))) {
236
			$input_errors[] = sprintf(gettext("A valid IP address must be specified for DNS server %s."), $dnscounter);
241
			$input_errors[] = sprintf(gettext("A valid IP address must be specified for DNS server %s."), $dnscounter+1);
237 242
		} else {
238 243
			if (($_POST[$dnsgwname] <> "") && ($_POST[$dnsgwname] <> "none")) {
239 244
				// A real gateway has been selected.
......
250 255
				}
251 256
			}
252 257
		}
258
		$dnscounter++;
259
		$dnsname = "dns{$dnscounter}";
253 260
	}
254 261

  
255 262
	if (count(array_filter($dnslist)) != count(array_unique(array_filter($dnslist)))) {
256 263
		$input_errors[] = gettext('Each configured DNS server must have a unique IP address. Remove the duplicated IP.');
257 264
	}
258 265

  
266
	$dnscounter = 0;
267
	$dnsname = "dns{$dnscounter}";
268

  
259 269
	$direct_networks_list = explode(" ", filter_get_direct_networks_list());
260
	for ($dnscounter=1; $dnscounter<5; $dnscounter++) {
261
		$dnsitem = "dns{$dnscounter}";
262
		$dnsgwitem = "dns{$dnscounter}gw";
263
		if ($_POST[$dnsgwitem] && ($_POST[$dnsgwitem] <> "none")) {
270
	while (isset($_POST[$dnsname])) {
271
		$dnsgwname = "dnsgw{$dnscounter}";
272
		if ($_POST[$dnsgwname] && ($_POST[$dnsgwname] <> "none")) {
264 273
			foreach ($direct_networks_list as $direct_network) {
265
				if (ip_in_subnet($_POST[$dnsitem], $direct_network)) {
266
					$input_errors[] = sprintf(gettext("A gateway can not be assigned to DNS '%s' server which is on a directly connected network."), $_POST[$dnsitem]);
274
				if (ip_in_subnet($_POST[$dnsname], $direct_network)) {
275
					$input_errors[] = sprintf(gettext("A gateway can not be assigned to DNS '%s' server which is on a directly connected network."), $_POST[$dnsname]);
267 276
				}
268 277
			}
269 278
		}
279
		$dnscounter++;
280
		$dnsname = "dns{$dnscounter}";
270 281
	}
271 282

  
272 283
	# it's easy to have a little too much whitespace in the field, clean it up for the user before processing.
......
278 289
		}
279 290
	}
280 291

  
281
	if (!$input_errors) {
292
	if ($input_errors) {
293
		// Put the user-entered list back into place so it will be redisplayed for correction.
294
		$pconfig['dnsserver'] = $dnslist;
295
	} else {
282 296
		update_if_changed("hostname", $config['system']['hostname'], $_POST['hostname']);
283 297
		update_if_changed("domain", $config['system']['domain'], $_POST['domain']);
284 298
		update_if_changed("timezone", $config['system']['timezone'], $_POST['timezone']);
......
307 321
		/* XXX - billm: these still need updating after figuring out how to check if they actually changed */
308 322
		$olddnsservers = $config['system']['dnsserver'];
309 323
		unset($config['system']['dnsserver']);
310
		if ($_POST['dns1']) {
311
			$config['system']['dnsserver'][] = $_POST['dns1'];
312
		}
313
		if ($_POST['dns2']) {
314
			$config['system']['dnsserver'][] = $_POST['dns2'];
315
		}
316
		if ($_POST['dns3']) {
317
			$config['system']['dnsserver'][] = $_POST['dns3'];
318
		}
319
		if ($_POST['dns4']) {
320
			$config['system']['dnsserver'][] = $_POST['dns4'];
324

  
325
		$dnscounter = 0;
326
		$dnsname = "dns{$dnscounter}";
327

  
328
		while (isset($_POST[$dnsname])) {
329
			if ($_POST[$dnsname]) {
330
				$config['system']['dnsserver'][] = $_POST[$dnsname];
331
			}
332
			$dnscounter++;
333
			$dnsname = "dns{$dnscounter}";
321 334
		}
322 335

  
336
		// Remember the new list for display also.
337
		$pconfig['dnsserver'] = $config['system']['dnsserver'];
338

  
323 339
		$olddnsallowoverride = $config['system']['dnsallowoverride'];
324 340

  
325 341
		unset($config['system']['dnsallowoverride']);
......
332 348
		}
333 349

  
334 350
		/* which interface should the dns servers resolve through? */
351
		$dnscounter = 0;
352
		// The $_POST array key of the DNS IP (starts from 0)
353
		$dnsname = "dns{$dnscounter}";
335 354
		$outdnscounter = 0;
336
		for ($dnscounter=1; $dnscounter<5; $dnscounter++) {
337
			$dnsname="dns{$dnscounter}";
338
			$dnsgwname="dns{$dnscounter}gw";
339
			$olddnsgwname = $config['system'][$dnsgwname];
355
		while (isset($_POST[$dnsname])) {
356
			// The $_POST array key of the corresponding gateway (starts from 0)
357
			$dnsgwname = "dnsgw{$dnscounter}";
358
			// The numbering of DNS GW entries in the config starts from 1
359
			$dnsgwconfigcounter = $dnscounter + 1;
360
			// So this is the array key of the DNS GW entry in $config['system']
361
			$dnsgwconfigname = "dns{$dnsgwconfigcounter}gw";
362

  
363
			$olddnsgwname = $config['system'][$dnsgwconfigname];
340 364

  
341 365
			if ($ignore_posted_dnsgw[$dnsgwname]) {
342 366
				$thisdnsgwname = "none";
......
345 369
			}
346 370

  
347 371
			// "Blank" out the settings for this index, then we set them below using the "outdnscounter" index.
348
			$config['system'][$dnsgwname] = "none";
372
			$config['system'][$dnsgwconfigname] = "none";
349 373
			$pconfig[$dnsgwname] = "none";
350 374
			$pconfig[$dnsname] = "";
351 375

  
......
353 377
				// Only the non-blank DNS servers were put into the config above.
354 378
				// So we similarly only add the corresponding gateways sequentially to the config (and to pconfig), as we find non-blank DNS servers.
355 379
				// 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.
356
				$outdnscounter++;
357
				$outdnsname="dns{$outdnscounter}";
358
				$outdnsgwname="dns{$outdnscounter}gw";
380

  
381
				// The $pconfig array key of the DNS IP (starts from 0)
382
				$outdnsname = "dns{$outdnscounter}";
383
				// The $pconfig array key of the corresponding gateway (starts from 0)
384
				$outdnsgwname = "dnsgw{$outdnscounter}";
385
				// The numbering of DNS GW entries in the config starts from 1
386
				$outdnsgwconfigcounter = $outdnscounter + 1;
387
				// So this is the array key of the output DNS GW entry in $config['system']
388
				$outdnsgwconfigname = "dns{$outdnsgwconfigcounter}gw";
389

  
359 390
				$pconfig[$outdnsname] = $_POST[$dnsname];
360 391
				if ($_POST[$dnsgwname]) {
361
					$config['system'][$outdnsgwname] = $thisdnsgwname;
392
					$config['system'][$outdnsgwconfigname] = $thisdnsgwname;
362 393
					$pconfig[$outdnsgwname] = $thisdnsgwname;
363 394
				} else {
364 395
					// Note: when no DNS GW name is chosen, the entry is set to "none", so actually this case never happens.
365
					unset($config['system'][$outdnsgwname]);
396
					unset($config['system'][$outdnsgwconfigname]);
366 397
					$pconfig[$outdnsgwname] = "";
367 398
				}
399
				$outdnscounter++;
368 400
			}
369
			if (($olddnsgwname != "") && ($olddnsgwname != "none") && (($olddnsgwname != $thisdnsgwname) || ($olddnsservers[$dnscounter-1] != $_POST[$dnsname]))) {
401
			if (($olddnsgwname != "") && ($olddnsgwname != "none") && (($olddnsgwname != $thisdnsgwname) || ($olddnsservers[$dnscounter] != $_POST[$dnsname]))) {
370 402
				// A previous DNS GW name was specified. It has now gone or changed, or the DNS server address has changed.
371 403
				// Remove the route. Later calls will add the correct new route if needed.
372
				if (is_ipaddrv4($olddnsservers[$dnscounter-1])) {
404
				if (is_ipaddrv4($olddnsservers[$dnscounter])) {
373 405
					mwexec("/sbin/route delete " . escapeshellarg($olddnsservers[$dnscounter-1]));
374
				} else if (is_ipaddrv6($olddnsservers[$dnscounter-1])) {
406
				} else if (is_ipaddrv6($olddnsservers[$dnscounter])) {
375 407
					mwexec("/sbin/route delete -inet6 " . escapeshellarg($olddnsservers[$dnscounter-1]));
376 408
				}
377 409
			}
410

  
411
			$dnscounter++;
412
			// The $_POST array key of the DNS IP (starts from 0)
413
			$dnsname = "dns{$dnscounter}";
378 414
		}
379 415

  
380 416
		if ($changecount > 0) {
......
444 480

  
445 481
$section = new Form_Section('DNS Server Settings');
446 482

  
447
for ($i=1; $i<5; $i++) {
448
//	if (!isset($pconfig['dns'.$i]))
449
//		continue;
483
if (!is_array($pconfig['dnsserver'])) {
484
	$pconfig['dnsserver'] = array();
485
}
486

  
487
$dnsserver_count = count($pconfig['dnsserver']);
488
$dnsserver_num = 0;
489
$dnsserver_help = gettext("Address") . '<br/>' . gettext("Enter IP addresses to be used by the system for DNS resolution.") . " " .
490
	gettext("These are also used for the DHCP service, DNS Forwarder and DNS Resolver when it has DNS Query Forwarding enabled.");
491
$dnsgw_help = gettext("Gateway") . '<br/>'. gettext("Optionally select the gateway for each DNS server.") . " " .
492
	gettext("When using multiple WAN connections there should be at least one unique DNS server per gateway.");
493

  
494
// If there are no DNS servers, make an empty entry for initial display.
495
if ($dnsserver_count == 0) {
496
	$pconfig['dnsserver'][] = '';
497
}
498

  
499
foreach ($pconfig['dnsserver'] as $dnsserver) {
450 500

  
451
	$group = new Form_Group('DNS Server ' . $i);
501
	$is_last_dnsserver = ($dnsserver_num == $dnsserver_count - 1);
502
	$group = new Form_Group($dnsserver_num == 0 ? 'DNS Servers':'');
503
	$group->addClass('repeatable');
452 504

  
453 505
	$group->add(new Form_Input(
454
		'dns' . $i,
506
		'dns' . $dnsserver_num,
455 507
		'DNS Server',
456 508
		'text',
457
		$pconfig['dns'. $i]
458
	))->setHelp(($i == 4) ? 'Address':null);
459

  
460
	$help = "Enter IP addresses to be used by the system for DNS resolution. " .
461
		"These are also used for the DHCP service, DNS Forwarder and DNS Resolver " .
462
		"when it has DNS Query Forwarding enabled.";
509
		$dnsserver
510
	))->setHelp(($is_last_dnsserver) ? $dnsserver_help:null);
463 511

  
464 512
	if ($multiwan)	{
465 513
		$options = array('none' => 'none');
......
477 525
		}
478 526

  
479 527
		$group->add(new Form_Select(
480
			'dns' . $i . 'gw',
528
			'dnsgw' . $dnsserver_num,
481 529
			'Gateway',
482
			$pconfig['dns' . $i . 'gw'],
530
			$pconfig['dnsgw' . $dnsserver_num],
483 531
			$options
484
		))->setHelp(($i == 4) ? 'Gateway':null);;
485

  
486
		$help .= '<br/>'. "In addition, optionally select the gateway for each DNS server. " .
487
			"When using multiple WAN connections there should be at least one unique DNS server per gateway.";
532
		))->setHelp(($is_last_dnsserver) ? $dnsgw_help:null);;
488 533
	}
489 534

  
490
	if ($i == 4) {
491
		$group->setHelp($help);
492
	}
535
	$group->add(new Form_Button(
536
		'deleterow' . $dnsserver_num,
537
		'Delete',
538
		null,
539
		'fa-trash'
540
	))->addClass('btn-warning');
493 541

  
494 542
	$section->add($group);
543
	$dnsserver_num++;
495 544
}
496 545

  
546
$section->addInput(new Form_Button(
547
	'addrow',
548
	'Add DNS Server',
549
	null,
550
	'fa-plus'
551
))->addClass('btn-success addbtn');
552

  
497 553
$section->addInput(new Form_Checkbox(
498 554
	'dnsallowoverride',
499 555
	'DNS Server Override',
......
601 657
	});
602 658

  
603 659
	setThemeWarning();
660

  
661
	// Suppress "Delete row" button if there are fewer than two rows
662
	checkLastRow();
604 663
});
605 664
//]]>
606 665
</script>

Also available in: Unified diff