Project

General

Profile

Download (18.5 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
 * services_ntpd.php
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-2020 Rubicon Communications, LLC (Netgate)
9
 * Copyright (c) 2013 Dagorlad
10
 * All rights reserved.
11
 *
12
 * Licensed under the Apache License, Version 2.0 (the "License");
13
 * you may not use this file except in compliance with the License.
14
 * You may obtain a copy of the License at
15
 *
16
 * http://www.apache.org/licenses/LICENSE-2.0
17
 *
18
 * Unless required by applicable law or agreed to in writing, software
19
 * distributed under the License is distributed on an "AS IS" BASIS,
20
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21
 * See the License for the specific language governing permissions and
22
 * limitations under the License.
23
 */
24

    
25
##|+PRIV
26
##|*IDENT=page-services-ntpd
27
##|*NAME=Services: NTP Settings
28
##|*DESCR=Allow access to the 'Services: NTP Settings' page.
29
##|*MATCH=services_ntpd.php*
30
##|-PRIV
31

    
32
define('NUMTIMESERVERS', 10);		// The maximum number of configurable time servers
33
require_once("guiconfig.inc");
34
require_once('rrd.inc');
35
require_once("shaper.inc");
36

    
37
global $ntp_poll_min_default, $ntp_poll_max_default;
38
$ntp_poll_values = system_ntp_poll_values();
39
$auto_pool_suffix = "pool.ntp.org";
40
$max_candidate_peers = 25;
41
$min_candidate_peers = 4;
42

    
43
if (!is_array($config['ntpd'])) {
44
	$config['ntpd'] = array();
45
}
46

    
47
if (empty($config['ntpd']['interface'])) {
48
	if (is_array($config['installedpackages']['openntpd']) && is_array($config['installedpackages']['openntpd']['config']) &&
49
	    is_array($config['installedpackages']['openntpd']['config'][0]) && !empty($config['installedpackages']['openntpd']['config'][0]['interface'])) {
50
		$pconfig['interface'] = explode(",", $config['installedpackages']['openntpd']['config'][0]['interface']);
51
		unset($config['installedpackages']['openntpd']);
52
		write_config(gettext("Upgraded settings from openntpd"));
53
	} else {
54
		$pconfig['interface'] = array();
55
	}
56
} else {
57
	$pconfig['interface'] = explode(",", $config['ntpd']['interface']);
58
}
59

    
60
if ($_POST) {
61
	unset($input_errors);
62
	$pconfig = $_POST;
63

    
64
	if (!empty($_POST['ntpmaxpeers']) && (!is_numericint($_POST['ntpmaxpeers']) ||
65
	    ($_POST['ntpmaxpeers'] < $min_candidate_peers) || ($_POST['ntpmaxpeers'] > $max_candidate_peers))) {
66
		$input_errors[] = sprintf(gettext("Max candidate pool peers must be a number between %d and %d"), $min_candidate_peers, $max_candidate_peers);
67
	}
68
	
69
	if ((strlen($pconfig['ntporphan']) > 0) && (!is_numericint($pconfig['ntporphan']) || ($pconfig['ntporphan'] < 1) || ($pconfig['ntporphan'] > 15))) {
70
		$input_errors[] = gettext("The supplied value for NTP Orphan Mode is invalid.");
71
	}
72

    
73
	if (!array_key_exists($pconfig['ntpminpoll'], $ntp_poll_values)) {
74
		$input_errors[] = gettext("The supplied value for Minimum Poll Interval is invalid.");
75
	}
76

    
77
	if (!array_key_exists($pconfig['ntpmaxpoll'], $ntp_poll_values)) {
78
		$input_errors[] = gettext("The supplied value for Maximum Poll Interval is invalid.");
79
	}
80

    
81
	for ($i = 0; $i < NUMTIMESERVERS; $i++) {
82
		if (isset($pconfig["servselect{$i}"]) && (isset($pconfig["servispool{$i}"]) || 
83
		    (substr_compare($pconfig["server{$i}"], $auto_pool_suffix, strlen($pconfig["server{$i}"]) - strlen($auto_pool_suffix), strlen($auto_pool_suffix)) === 0))) {
84
			$input_errors[] = gettext("It is not possible to use 'No Select' for pools.");
85
		}
86
	}
87

    
88
	if (is_numericint($pconfig['ntpminpoll']) &&
89
	    is_numericint($pconfig['ntpmaxpoll']) &&
90
	    ($pconfig['ntpmaxpoll'] < $pconfig['ntpminpoll'])) {
91
		$input_errors[] = gettext("The supplied value for Minimum Poll Interval is higher than NTP Maximum Poll Interval.");
92
	}
93

    
94
	if (!$input_errors) {
95
		$config['ntpd']['enable'] = isset($pconfig['enable']) ? 'enabled' : 'disabled';
96
		if (is_array($_POST['interface'])) {
97
			$config['ntpd']['interface'] = implode(",", $_POST['interface']);
98
		} elseif (isset($config['ntpd']['interface'])) {
99
			unset($config['ntpd']['interface']);
100
		}
101

    
102
		if (!empty($_POST['gpsport']) && file_exists('/dev/'.$_POST['gpsport'])) {
103
			$config['ntpd']['gpsport'] = $_POST['gpsport'];
104
		} elseif (isset($config['ntpd']['gpsport'])) {
105
			unset($config['ntpd']['gpsport']);
106
		}
107

    
108
		unset($config['ntpd']['prefer']);
109
		unset($config['ntpd']['noselect']);
110
		unset($config['ntpd']['ispool']);
111
		$timeservers = '';
112

    
113
		for ($i = 0; $i < NUMTIMESERVERS; $i++) {
114
			$tserver = trim($_POST["server{$i}"]);
115
			if (!empty($tserver)) {
116
				$timeservers .= "{$tserver} ";
117
				if (isset($_POST["servprefer{$i}"])) {
118
					$config['ntpd']['prefer'] .= "{$tserver} ";
119
				}
120
				if (isset($_POST["servselect{$i}"])) {
121
					$config['ntpd']['noselect'] .= "{$tserver} ";
122
				}
123
				if (isset($_POST["servispool{$i}"])) {
124
					$config['ntpd']['ispool'] .= "{$tserver} ";
125
				}
126
			}
127
		}
128
		if (trim($timeservers) == "") {
129
			$timeservers = "pool.ntp.org";
130
		}
131
		$config['system']['timeservers'] = trim($timeservers);
132

    
133
		if (!empty($pconfig['ntpmaxpeers'])) {
134
			$config['ntpd']['ntpmaxpeers'] = $pconfig['ntpmaxpeers'];
135
		} else {
136
			unset($config['ntpd']['ntpmaxpeers']);
137
		}
138
		$config['ntpd']['orphan'] = trim($pconfig['ntporphan']);
139
		$config['ntpd']['ntpminpoll'] = $pconfig['ntpminpoll'];
140
		$config['ntpd']['ntpmaxpoll'] = $pconfig['ntpmaxpoll'];
141

    
142
		if (!empty($_POST['logpeer'])) {
143
			$config['ntpd']['logpeer'] = $_POST['logpeer'];
144
		} elseif (isset($config['ntpd']['logpeer'])) {
145
			unset($config['ntpd']['logpeer']);
146
		}
147

    
148
		if (!empty($_POST['logsys'])) {
149
			$config['ntpd']['logsys'] = $_POST['logsys'];
150
		} elseif (isset($config['ntpd']['logsys'])) {
151
			unset($config['ntpd']['logsys']);
152
		}
153

    
154
		if (!empty($_POST['clockstats'])) {
155
			$config['ntpd']['clockstats'] = $_POST['clockstats'];
156
		} elseif (isset($config['ntpd']['clockstats'])) {
157
			unset($config['ntpd']['clockstats']);
158
		}
159

    
160
		if (!empty($_POST['loopstats'])) {
161
			$config['ntpd']['loopstats'] = $_POST['loopstats'];
162
		} elseif (isset($config['ntpd']['loopstats'])) {
163
			unset($config['ntpd']['loopstats']);
164
		}
165

    
166
		if (!empty($_POST['peerstats'])) {
167
			$config['ntpd']['peerstats'] = $_POST['peerstats'];
168
		} elseif (isset($config['ntpd']['peerstats'])) {
169
			unset($config['ntpd']['peerstats']);
170
		}
171

    
172
		if ((empty($_POST['statsgraph'])) == (isset($config['ntpd']['statsgraph']))) {
173
			$enable_rrd_graphing = true;
174
		}
175
		if (!empty($_POST['statsgraph'])) {
176
			$config['ntpd']['statsgraph'] = $_POST['statsgraph'];
177
		} elseif (isset($config['ntpd']['statsgraph'])) {
178
			unset($config['ntpd']['statsgraph']);
179
		}
180
		if (isset($enable_rrd_graphing)) {
181
			enable_rrd_graphing();
182
		}
183

    
184
		if (!empty($_POST['leaptext'])) {
185
			$config['ntpd']['leapsec'] = base64_encode($_POST['leaptext']);
186
		} elseif (isset($config['ntpd']['leapsec'])) {
187
			unset($config['ntpd']['leapsec']);
188
		}
189

    
190
		if (is_uploaded_file($_FILES['leapfile']['tmp_name'])) {
191
			$config['ntpd']['leapsec'] = base64_encode(file_get_contents($_FILES['leapfile']['tmp_name']));
192
		}
193

    
194
		write_config("Updated NTP Server Settings");
195

    
196
		$changes_applied = true;
197
		$retval = 0;
198
		$retval |= system_ntp_configure();
199
	}
200
}
201

    
202
function build_interface_list() {
203
	global $pconfig;
204

    
205
	$iflist = array('options' => array(), 'selected' => array());
206

    
207
	$interfaces = get_configured_interface_with_descr();
208
	$interfaces['lo0'] = "Localhost";
209

    
210
	foreach ($interfaces as $iface => $ifacename) {
211
		if (!is_ipaddr(get_interface_ip($iface)) &&
212
		    !is_ipaddrv6(get_interface_ipv6($iface))) {
213
			continue;
214
		}
215

    
216
		$iflist['options'][$iface] = $ifacename;
217

    
218
		if (in_array($iface, $pconfig['interface'])) {
219
			array_push($iflist['selected'], $iface);
220
		}
221
	}
222

    
223
	return($iflist);
224
}
225

    
226
init_config_arr(array('ntpd'));
227
$pconfig = &$config['ntpd'];
228
$config['ntpd']['enable'] = isset($pconfig['enable']) ? 'enabled' : 'disabled';
229
if (empty($pconfig['interface'])) {
230
	$pconfig['interface'] = array();
231
} else {
232
	$pconfig['interface'] = explode(",", $pconfig['interface']);
233
}
234
$pgtitle = array(gettext("Services"), gettext("NTP"), gettext("Settings"));
235
$pglinks = array("", "@self", "@self");
236
$shortcut_section = "ntp";
237
include("head.inc");
238

    
239
if ($input_errors) {
240
	print_input_errors($input_errors);
241
}
242

    
243
if ($changes_applied) {
244
	print_apply_result_box($retval);
245
}
246

    
247
$tab_array = array();
248
$tab_array[] = array(gettext("Settings"), true, "services_ntpd.php");
249
$tab_array[] = array(gettext("ACLs"), false, "services_ntpd_acls.php");
250
$tab_array[] = array(gettext("Serial GPS"), false, "services_ntpd_gps.php");
251
$tab_array[] = array(gettext("PPS"), false, "services_ntpd_pps.php");
252
display_top_tabs($tab_array);
253

    
254
$form = new Form;
255
$form->setMultipartEncoding();	// Allow file uploads
256

    
257
$section = new Form_Section('NTP Server Configuration');
258

    
259
$section->addInput(new Form_Checkbox(
260
	'enable',
261
	'Enable',
262
	'Enable NTP Server',
263
	$pconfig['enable']
264
))->setHelp('You may need to disable NTP if pfSense is running in a virtual machine and the host is responsible for the clock.');
265

    
266
$iflist = build_interface_list();
267

    
268
$section->addInput(new Form_Select(
269
	'interface',
270
	'Interface',
271
	$iflist['selected'],
272
	$iflist['options'],
273
	true
274
))->setHelp('Interfaces without an IP address will not be shown.%1$s' .
275
			'Selecting no interfaces will listen on all interfaces with a wildcard.%1$s' .
276
			'Selecting all interfaces will explicitly listen on only the interfaces/IPs specified.', '<br />');
277

    
278
$timeservers = explode(' ', $config['system']['timeservers']);
279
$maxrows = max(count($timeservers), 1);
280
for ($counter=0; $counter < $maxrows; $counter++) {
281
	$group = new Form_Group($counter == 0 ? 'Time Servers':'');
282
	$group->addClass('repeatable');
283
	$group->setAttribute('max_repeats', NUMTIMESERVERS);
284
	$group->setAttribute('max_repeats_alert', sprintf(gettext('%d is the maximum number of configured servers.'), NUMTIMESERVERS));
285

    
286
	$group->add(new Form_Input(
287
		'server' . $counter,
288
		null,
289
		'text',
290
		$timeservers[$counter],
291
		['placeholder' => 'Hostname']
292
	 ))->setWidth(3);
293

    
294
	 $group->add(new Form_Checkbox(
295
		'servprefer' . $counter,
296
		null,
297
		null,
298
		isset($config['ntpd']['prefer']) && isset($timeservers[$counter]) && substr_count($config['ntpd']['prefer'], $timeservers[$counter])
299
	 ))->sethelp('Prefer');
300

    
301
	 $group->add(new Form_Checkbox(
302
		'servselect' . $counter,
303
		null,
304
		null,
305
		isset($config['ntpd']['noselect']) && isset($timeservers[$counter]) && substr_count($config['ntpd']['noselect'], $timeservers[$counter])
306
	 ))->sethelp('No Select');
307

    
308
	$group->add(new Form_Checkbox(
309
		'servispool' . $counter,
310
		null,
311
		null,
312
		(substr_compare($timeservers[$counter], $auto_pool_suffix, strlen($timeservers[$counter]) - strlen($auto_pool_suffix), strlen($auto_pool_suffix)) === 0)
313
		 || (isset($config['ntpd']['ispool']) && isset($timeservers[$counter]) && substr_count($config['ntpd']['ispool'], $timeservers[$counter]))
314
	 ))->sethelp('Is a Pool');
315

    
316
	$group->add(new Form_Button(
317
		'deleterow' . $counter,
318
		'Delete',
319
		null,
320
		'fa-trash'
321
	))->addClass('btn-warning');
322

    
323
	 $section->add($group);
324
}
325

    
326
$section->addInput(new Form_Button(
327
	'addrow',
328
	'Add',
329
	null,
330
	'fa-plus'
331
))->addClass('btn-success');
332

    
333
$section->addInput(new Form_StaticText(
334
	null,
335
	$btnaddrow
336
))->setHelp(
337
	'NTP will only sync if a majority of the servers agree on the time.  For best results you should configure between 3 and 5 servers ' .
338
	'(%4$sNTP support pages recommend at least 4 or 5%5$s), or a pool. If only one server is configured, it %2$swill%3$s be believed, and if 2 servers ' .
339
	'are configured and they disagree, %2$sneither%3$s will be believed. Options:%1$s' .
340
	'%2$sPrefer%3$s - NTP should favor the use of this server more than all others.%1$s' .
341
	'%2$sNo Select%3$s - NTP should not use this server for time, but stats for this server will be collected and displayed.%1$s' .
342
	'%2$sIs a Pool%3$s - this entry is a pool of NTP servers and not a single address. This is assumed for *.pool.ntp.org.',
343
	'<br />',
344
	'<b>',
345
	'</b>',
346
	'<a target="_blank" href="https://support.ntp.org/bin/view/Support/ConfiguringNTP">',
347
	'</a>'
348
	);
349

    
350
$section->addInput(new Form_Input(
351
	'ntpmaxpeers',
352
	'Max candidate pool peers',
353
	'number',
354
	$pconfig['ntpmaxpeers'],
355
	['min' => $min_candidate_peers, 'max' => $max_candidate_peers]
356
))->setHelp('Maximum number of candidate peers in the NTP pool. This value should be set low enough to provide sufficient alternate sources ' .
357
	    'while not contacting an excessively large number of peers. ' .
358
	    'Many servers inside public pools are provided by volunteers, ' .
359
	    'and a large candidate pool places unnecessary extra load ' .
360
	    'on the volunteer time servers for little to no added benefit. (Default: 5).');
361

    
362
$section->addInput(new Form_Input(
363
	'ntporphan',
364
	'Orphan Mode',
365
	'text',
366
	$pconfig['orphan'],
367
	['placeholder' => "12"]
368
))->setHelp('Orphan mode allows the system clock to be used when no other clocks are available. ' .
369
			'The number here specifies the stratum reported during orphan mode and should normally be set to a number high enough ' .
370
			'to insure that any other servers available to clients are preferred over this server (default: 12).');
371

    
372
$section->addInput(new Form_Select(
373
	'ntpminpoll',
374
	'Minimum Poll Interval',
375
	$pconfig['ntpminpoll'],
376
	$ntp_poll_values
377
))->setHelp('Minimum poll interval for NTP messages. If set, must be less than or equal to Maximum Poll Interval.');
378

    
379
$section->addInput(new Form_Select(
380
	'ntpmaxpoll',
381
	'Maximum Poll Interval',
382
	$pconfig['ntpmaxpoll'],
383
	$ntp_poll_values
384
))->setHelp('Maximum poll interval for NTP messages. If set, must be greater than or equal to Minimum Poll Interval.');
385

    
386
$section->addInput(new Form_Checkbox(
387
	'statsgraph',
388
	'NTP Graphs',
389
	'Enable RRD graphs of NTP statistics (default: disabled).',
390
	$pconfig['statsgraph']
391
));
392

    
393
$section->addInput(new Form_Checkbox(
394
	'logpeer',
395
	'Logging',
396
	'Log peer messages (default: disabled).',
397
	$pconfig['logpeer']
398
));
399

    
400
$section->addInput(new Form_Checkbox(
401
	'logsys',
402
	null,
403
	'Log system messages (default: disabled).',
404
	$pconfig['logsys']
405
))->setHelp('These options enable additional messages from NTP to be written to the System Log %1$sStatus > System Logs > NTP%2$s',
406
			'<a href="status_logs.php?logfile=ntpd">', '</a>.');
407

    
408
// Statistics logging section
409
$btnadv = new Form_Button(
410
	'btnadvstats',
411
	'Display Advanced',
412
	null,
413
	'fa-cog'
414
);
415

    
416
$btnadv->setAttribute('type','button')->addClass('btn-info btn-sm');
417

    
418
$section->addInput(new Form_StaticText(
419
	'Statistics Logging',
420
	$btnadv
421
))->setHelp('Warning: These options will create persistent daily log files in /var/log/ntp.');
422

    
423
$section->addInput(new Form_Checkbox(
424
	'clockstats',
425
	null,
426
	'Log reference clock statistics (default: disabled).',
427
	$pconfig['clockstats']
428
));
429

    
430
$section->addInput(new Form_Checkbox(
431
	'loopstats',
432
	null,
433
	'Log clock discipline statistics (default: disabled).',
434
	$pconfig['loopstats']
435
));
436

    
437
$section->addInput(new Form_Checkbox(
438
	'peerstats',
439
	null,
440
	'Log NTP peer statistics (default: disabled).',
441
	$pconfig['peerstats']
442
));
443

    
444
// Leap seconds section
445
$btnadv = new Form_Button(
446
	'btnadvleap',
447
	'Display Advanced',
448
	null,
449
	'fa-cog'
450
);
451

    
452
$btnadv->setAttribute('type','button')->addClass('btn-info btn-sm');
453

    
454
$section->addInput(new Form_StaticText(
455
	'Leap seconds',
456
	$btnadv
457
))->setHelp(
458
	'Leap seconds may be added or subtracted at the end of June or December. Leap seconds are administered by the ' .
459
	'%1$sIERS%2$s, who publish them in their Bulletin C approximately 6 - 12 months in advance.  Normally this correction ' .
460
	'should only be needed if the server is a stratum 1 NTP server, but many NTP servers do not advertise an upcoming leap ' .
461
	'second when other NTP servers synchronise to them.%3$s%4$sIf the leap second is important to your network services, ' .
462
	'it is %6$sgood practice%2$s to download and add the leap second file at least a day in advance of any time correction%5$s.%3$s ' .
463
	'More information and files for downloading can be found on their %1$swebsite%2$s, and also on the %7$NIST%2$s and %8$sNTP%2$s websites.',
464
	'<a target="_blank" href="https://www.iers.org">',
465
	'</a>',
466
	'<br />',
467
	'<b>',
468
	'</b>',
469
	'<a target="_blank" href="https://support.ntp.org/bin/view/Support/ConfiguringNTP">',
470
	'<a target="_blank" href="https://www.nist.gov">',
471
	'<a target="_blank" href="https://www.ntp.org">'
472
);
473

    
474
$section->addInput(new Form_Textarea(
475
	'leaptext',
476
	null,
477
	base64_decode(chunk_split($pconfig['leapsec']))
478
))->setHelp('Enter Leap second configuration as text OR select a file to upload.');
479

    
480
$section->addInput(new Form_Input(
481
	'leapfile',
482
	null,
483
	'file'
484
))->addClass('btn-default');
485

    
486
$form->add($section);
487

    
488
print($form);
489

    
490
?>
491

    
492
<script type="text/javascript">
493
//<![CDATA[
494
	// If this variable is declared, any help text will not be deleted when rows are added
495
	// IOW the help text will appear on every row
496
	retainhelp = true;
497
</script>
498

    
499
<script type="text/javascript">
500
//<![CDATA[
501
events.push(function() {
502

    
503
	// Show advanced stats options ============================================
504
	var showadvstats = false;
505

    
506
	function show_advstats(ispageload) {
507
		var text;
508
		// On page load decide the initial state based on the data.
509
		if (ispageload) {
510
<?php
511
			if (!$pconfig['clockstats'] && !$pconfig['loopstats'] && !$pconfig['peerstats']) {
512
				$showadv = false;
513
			} else {
514
				$showadv = true;
515
			}
516
?>
517
			showadvstats = <?php if ($showadv) {echo 'true';} else {echo 'false';} ?>;
518
		} else {
519
			// It was a click, swap the state.
520
			showadvstats = !showadvstats;
521
		}
522

    
523
		hideCheckbox('clockstats', !showadvstats);
524
		hideCheckbox('loopstats', !showadvstats);
525
		hideCheckbox('peerstats', !showadvstats);
526

    
527
		if (showadvstats) {
528
			text = "<?=gettext('Hide Advanced');?>";
529
		} else {
530
			text = "<?=gettext('Display Advanced');?>";
531
		}
532
		$('#btnadvstats').html('<i class="fa fa-cog"></i> ' + text);
533
	}
534

    
535
	$('#btnadvstats').click(function(event) {
536
		show_advstats();
537
	});
538

    
539
	// Show advanced leap second options ======================================
540
	var showadvleap = false;
541

    
542
	function show_advleap(ispageload) {
543
		var text;
544
		// On page load decide the initial state based on the data.
545
		if (ispageload) {
546
<?php
547
			// Note: leapfile is not a field saved in the config, so no need to test for it here.
548
			// leapsec is the encoded text in the config, leaptext is not a pconfig[] key.
549
			if (empty($pconfig['leapsec'])) {
550
				$showadv = false;
551
			} else {
552
				$showadv = true;
553
			}
554
?>
555
			showadvleap = <?php if ($showadv) {echo 'true';} else {echo 'false';} ?>;
556
		} else {
557
			// It was a click, swap the state.
558
			showadvleap = !showadvleap;
559
		}
560

    
561
		hideInput('leaptext', !showadvleap);
562
		hideInput('leapfile', !showadvleap);
563

    
564
		if (showadvleap) {
565
			text = "<?=gettext('Hide Advanced');?>";
566
		} else {
567
			text = "<?=gettext('Display Advanced');?>";
568
		}
569
		$('#btnadvleap').html('<i class="fa fa-cog"></i> ' + text);
570
	}
571

    
572
	$('#btnadvleap').click(function(event) {
573
		show_advleap();
574
	});
575

    
576
	// Set initial states
577
	show_advstats(true);
578
	show_advleap(true);
579

    
580
	// Suppress "Delete row" button if there are fewer than two rows
581
	checkLastRow();
582
});
583
//]]>
584
</script>
585

    
586
<?php include("foot.inc");
(131-131/227)