Project

General

Profile

Download (16.9 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

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

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

    
58
if ($_POST) {
59
	unset($input_errors);
60
	$pconfig = $_POST;
61

    
62
	if ((strlen($pconfig['ntporphan']) > 0) && (!is_numericint($pconfig['ntporphan']) || ($pconfig['ntporphan'] < 1) || ($pconfig['ntporphan'] > 15))) {
63
		$input_errors[] = gettext("The supplied value for NTP Orphan Mode is invalid.");
64
	}
65

    
66
	if (!array_key_exists($pconfig['ntpminpoll'], $ntp_poll_values)) {
67
		$input_errors[] = gettext("The supplied value for Minimum Poll Interval is invalid.");
68
	}
69

    
70
	if (!array_key_exists($pconfig['ntpmaxpoll'], $ntp_poll_values)) {
71
		$input_errors[] = gettext("The supplied value for Maximum Poll Interval is invalid.");
72
	}
73

    
74
	for ($i = 0; $i < NUMTIMESERVERS; $i++) {
75
		if (isset($pconfig["servselect{$i}"]) && (isset($pconfig["servispool{$i}"]) || 
76
		    (substr_compare($pconfig["server{$i}"], $auto_pool_suffix, strlen($pconfig["server{$i}"]) - strlen($auto_pool_suffix), strlen($auto_pool_suffix)) === 0))) {
77
			$input_errors[] = gettext("It is not possible to use 'No Select' for pools.");
78
		}
79
	}
80

    
81
	if (is_numericint($pconfig['ntpminpoll']) &&
82
	    is_numericint($pconfig['ntpmaxpoll']) &&
83
	    ($pconfig['ntpmaxpoll'] < $pconfig['ntpminpoll'])) {
84
		$input_errors[] = gettext("The supplied value for Minimum Poll Interval is higher than NTP Maximum Poll Interval.");
85
	}
86

    
87
	if (!$input_errors) {
88
		if (is_array($_POST['interface'])) {
89
			$config['ntpd']['interface'] = implode(",", $_POST['interface']);
90
		} elseif (isset($config['ntpd']['interface'])) {
91
			unset($config['ntpd']['interface']);
92
		}
93

    
94
		if (!empty($_POST['gpsport']) && file_exists('/dev/'.$_POST['gpsport'])) {
95
			$config['ntpd']['gpsport'] = $_POST['gpsport'];
96
		} elseif (isset($config['ntpd']['gpsport'])) {
97
			unset($config['ntpd']['gpsport']);
98
		}
99

    
100
		unset($config['ntpd']['prefer']);
101
		unset($config['ntpd']['noselect']);
102
		unset($config['ntpd']['ispool']);
103
		$timeservers = '';
104

    
105
		for ($i = 0; $i < NUMTIMESERVERS; $i++) {
106
			$tserver = trim($_POST["server{$i}"]);
107
			if (!empty($tserver)) {
108
				$timeservers .= "{$tserver} ";
109
				if (isset($_POST["servprefer{$i}"])) {
110
					$config['ntpd']['prefer'] .= "{$tserver} ";
111
				}
112
				if (isset($_POST["servselect{$i}"])) {
113
					$config['ntpd']['noselect'] .= "{$tserver} ";
114
				}
115
				if (isset($_POST["servispool{$i}"])) {
116
					$config['ntpd']['ispool'] .= "{$tserver} ";
117
				}
118
			}
119
		}
120
		if (trim($timeservers) == "") {
121
			$timeservers = "pool.ntp.org";
122
		}
123
		$config['system']['timeservers'] = trim($timeservers);
124

    
125
		$config['ntpd']['orphan'] = trim($pconfig['ntporphan']);
126
		$config['ntpd']['ntpminpoll'] = $pconfig['ntpminpoll'];
127
		$config['ntpd']['ntpmaxpoll'] = $pconfig['ntpmaxpoll'];
128

    
129
		if (!empty($_POST['logpeer'])) {
130
			$config['ntpd']['logpeer'] = $_POST['logpeer'];
131
		} elseif (isset($config['ntpd']['logpeer'])) {
132
			unset($config['ntpd']['logpeer']);
133
		}
134

    
135
		if (!empty($_POST['logsys'])) {
136
			$config['ntpd']['logsys'] = $_POST['logsys'];
137
		} elseif (isset($config['ntpd']['logsys'])) {
138
			unset($config['ntpd']['logsys']);
139
		}
140

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

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

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

    
159
		if ((empty($_POST['statsgraph'])) == (isset($config['ntpd']['statsgraph']))) {
160
			$enable_rrd_graphing = true;
161
		}
162
		if (!empty($_POST['statsgraph'])) {
163
			$config['ntpd']['statsgraph'] = $_POST['statsgraph'];
164
		} elseif (isset($config['ntpd']['statsgraph'])) {
165
			unset($config['ntpd']['statsgraph']);
166
		}
167
		if (isset($enable_rrd_graphing)) {
168
			enable_rrd_graphing();
169
		}
170

    
171
		if (!empty($_POST['leaptext'])) {
172
			$config['ntpd']['leapsec'] = base64_encode($_POST['leaptext']);
173
		} elseif (isset($config['ntpd']['leapsec'])) {
174
			unset($config['ntpd']['leapsec']);
175
		}
176

    
177
		if (is_uploaded_file($_FILES['leapfile']['tmp_name'])) {
178
			$config['ntpd']['leapsec'] = base64_encode(file_get_contents($_FILES['leapfile']['tmp_name']));
179
		}
180

    
181
		write_config("Updated NTP Server Settings");
182

    
183
		$changes_applied = true;
184
		$retval = 0;
185
		$retval |= system_ntp_configure();
186
	}
187
}
188

    
189
function build_interface_list() {
190
	global $pconfig;
191

    
192
	$iflist = array('options' => array(), 'selected' => array());
193

    
194
	$interfaces = get_configured_interface_with_descr();
195
	foreach ($interfaces as $iface => $ifacename) {
196
		if (!is_ipaddr(get_interface_ip($iface)) &&
197
		    !is_ipaddrv6(get_interface_ipv6($iface))) {
198
			continue;
199
		}
200

    
201
		$iflist['options'][$iface] = $ifacename;
202

    
203
		if (in_array($iface, $pconfig['interface'])) {
204
			array_push($iflist['selected'], $iface);
205
		}
206
	}
207

    
208
	return($iflist);
209
}
210

    
211
init_config_arr(array('ntpd'));
212
$pconfig = &$config['ntpd'];
213
if (empty($pconfig['interface'])) {
214
	$pconfig['interface'] = array();
215
} else {
216
	$pconfig['interface'] = explode(",", $pconfig['interface']);
217
}
218
$pgtitle = array(gettext("Services"), gettext("NTP"), gettext("Settings"));
219
$pglinks = array("", "@self", "@self");
220
$shortcut_section = "ntp";
221
include("head.inc");
222

    
223
if ($input_errors) {
224
	print_input_errors($input_errors);
225
}
226

    
227
if ($changes_applied) {
228
	print_apply_result_box($retval);
229
}
230

    
231
$tab_array = array();
232
$tab_array[] = array(gettext("Settings"), true, "services_ntpd.php");
233
$tab_array[] = array(gettext("ACLs"), false, "services_ntpd_acls.php");
234
$tab_array[] = array(gettext("Serial GPS"), false, "services_ntpd_gps.php");
235
$tab_array[] = array(gettext("PPS"), false, "services_ntpd_pps.php");
236
display_top_tabs($tab_array);
237

    
238
$form = new Form;
239
$form->setMultipartEncoding();	// Allow file uploads
240

    
241
$section = new Form_Section('NTP Server Configuration');
242

    
243
$iflist = build_interface_list();
244

    
245
$section->addInput(new Form_Select(
246
	'interface',
247
	'Interface',
248
	$iflist['selected'],
249
	$iflist['options'],
250
	true
251
))->setHelp('Interfaces without an IP address will not be shown.%1$s' .
252
			'Selecting no interfaces will listen on all interfaces with a wildcard.%1$s' .
253
			'Selecting all interfaces will explicitly listen on only the interfaces/IPs specified.', '<br />');
254

    
255
$timeservers = explode(' ', $config['system']['timeservers']);
256
$maxrows = max(count($timeservers), 1);
257
for ($counter=0; $counter < $maxrows; $counter++) {
258
	$group = new Form_Group($counter == 0 ? 'Time Servers':'');
259
	$group->addClass('repeatable');
260
	$group->setAttribute('max_repeats', NUMTIMESERVERS);
261
	$group->setAttribute('max_repeats_alert', sprintf(gettext('%d is the maximum number of configured servers.'), NUMTIMESERVERS));
262

    
263
	$group->add(new Form_Input(
264
		'server' . $counter,
265
		null,
266
		'text',
267
		$timeservers[$counter],
268
		['placeholder' => 'Hostname']
269
	 ))->setWidth(3);
270

    
271
	 $group->add(new Form_Checkbox(
272
		'servprefer' . $counter,
273
		null,
274
		null,
275
		isset($config['ntpd']['prefer']) && isset($timeservers[$counter]) && substr_count($config['ntpd']['prefer'], $timeservers[$counter])
276
	 ))->sethelp('Prefer');
277

    
278
	 $group->add(new Form_Checkbox(
279
		'servselect' . $counter,
280
		null,
281
		null,
282
		isset($config['ntpd']['noselect']) && isset($timeservers[$counter]) && substr_count($config['ntpd']['noselect'], $timeservers[$counter])
283
	 ))->sethelp('No Select');
284

    
285
	$group->add(new Form_Checkbox(
286
		'servispool' . $counter,
287
		null,
288
		null,
289
		(substr_compare($timeservers[$counter], $auto_pool_suffix, strlen($timeservers[$counter]) - strlen($auto_pool_suffix), strlen($auto_pool_suffix)) === 0)
290
		 || (isset($config['ntpd']['ispool']) && isset($timeservers[$counter]) && substr_count($config['ntpd']['ispool'], $timeservers[$counter]))
291
	 ))->sethelp('Is a Pool');
292

    
293
	$group->add(new Form_Button(
294
		'deleterow' . $counter,
295
		'Delete',
296
		null,
297
		'fa-trash'
298
	))->addClass('btn-warning');
299

    
300
	 $section->add($group);
301
}
302

    
303
$section->addInput(new Form_Button(
304
	'addrow',
305
	'Add',
306
	null,
307
	'fa-plus'
308
))->addClass('btn-success');
309

    
310
$section->addInput(new Form_StaticText(
311
	null,
312
	$btnaddrow
313
))->setHelp(
314
	'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 ' .
315
	'(%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 ' .
316
	'are configured and they disagree, %2$sneither%3$s will be believed. Options:%1$s' .
317
	'%2$sPrefer%3$s - NTP should favor the use of this server more than all others.%1$s' .
318
	'%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' .
319
	'%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.',
320
	'<br />',
321
	'<b>',
322
	'</b>',
323
	'<a target="_blank" href="https://support.ntp.org/bin/view/Support/ConfiguringNTP">',
324
	'</a>'
325
	);
326

    
327
$section->addInput(new Form_Input(
328
	'ntporphan',
329
	'Orphan Mode',
330
	'text',
331
	$pconfig['orphan'],
332
	['placeholder' => "12"]
333
))->setHelp('Orphan mode allows the system clock to be used when no other clocks are available. ' .
334
			'The number here specifies the stratum reported during orphan mode and should normally be set to a number high enough ' .
335
			'to insure that any other servers available to clients are preferred over this server (default: 12).');
336

    
337
$section->addInput(new Form_Select(
338
	'ntpminpoll',
339
	'Minimum Poll Interval',
340
	$pconfig['ntpminpoll'],
341
	$ntp_poll_values
342
))->setHelp('Minimum poll interval for NTP messages. If set, must be less than or equal to Maximum Poll Interval.');
343

    
344
$section->addInput(new Form_Select(
345
	'ntpmaxpoll',
346
	'Maximum Poll Interval',
347
	$pconfig['ntpmaxpoll'],
348
	$ntp_poll_values
349
))->setHelp('Maximum poll interval for NTP messages. If set, must be greater than or equal to Minimum Poll Interval.');
350

    
351
$section->addInput(new Form_Checkbox(
352
	'statsgraph',
353
	'NTP Graphs',
354
	'Enable RRD graphs of NTP statistics (default: disabled).',
355
	$pconfig['statsgraph']
356
));
357

    
358
$section->addInput(new Form_Checkbox(
359
	'logpeer',
360
	'Logging',
361
	'Log peer messages (default: disabled).',
362
	$pconfig['logpeer']
363
));
364

    
365
$section->addInput(new Form_Checkbox(
366
	'logsys',
367
	null,
368
	'Log system messages (default: disabled).',
369
	$pconfig['logsys']
370
))->setHelp('These options enable additional messages from NTP to be written to the System Log %1$sStatus > System Logs > NTP%2$s',
371
			'<a href="status_logs.php?logfile=ntpd">', '</a>.');
372

    
373
// Statistics logging section
374
$btnadv = new Form_Button(
375
	'btnadvstats',
376
	'Display Advanced',
377
	null,
378
	'fa-cog'
379
);
380

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

    
383
$section->addInput(new Form_StaticText(
384
	'Statistics Logging',
385
	$btnadv
386
))->setHelp('Warning: These options will create persistent daily log files in /var/log/ntp.');
387

    
388
$section->addInput(new Form_Checkbox(
389
	'clockstats',
390
	null,
391
	'Log reference clock statistics (default: disabled).',
392
	$pconfig['clockstats']
393
));
394

    
395
$section->addInput(new Form_Checkbox(
396
	'loopstats',
397
	null,
398
	'Log clock discipline statistics (default: disabled).',
399
	$pconfig['loopstats']
400
));
401

    
402
$section->addInput(new Form_Checkbox(
403
	'peerstats',
404
	null,
405
	'Log NTP peer statistics (default: disabled).',
406
	$pconfig['peerstats']
407
));
408

    
409
// Leap seconds section
410
$btnadv = new Form_Button(
411
	'btnadvleap',
412
	'Display Advanced',
413
	null,
414
	'fa-cog'
415
);
416

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

    
419
$section->addInput(new Form_StaticText(
420
	'Leap seconds',
421
	$btnadv
422
))->setHelp(
423
	'Leap seconds may be added or subtracted at the end of June or December. Leap seconds are administered by the ' .
424
	'%1$sIERS%2$s, who publish them in their Bulletin C approximately 6 - 12 months in advance.  Normally this correction ' .
425
	'should only be needed if the server is a stratum 1 NTP server, but many NTP servers do not advertise an upcoming leap ' .
426
	'second when other NTP servers synchronise to them.%3$s%4$sIf the leap second is important to your network services, ' .
427
	'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 ' .
428
	'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.',
429
	'<a target="_blank" href="https://www.iers.org">',
430
	'</a>',
431
	'<br />',
432
	'<b>',
433
	'</b>',
434
	'<a target="_blank" href="https://support.ntp.org/bin/view/Support/ConfiguringNTP">',
435
	'<a target="_blank" href="https://www.nist.gov">',
436
	'<a target="_blank" href="https://www.ntp.org">'
437
);
438

    
439
$section->addInput(new Form_Textarea(
440
	'leaptext',
441
	null,
442
	base64_decode(chunk_split($pconfig['leapsec']))
443
))->setHelp('Enter Leap second configuration as text OR select a file to upload.');
444

    
445
$section->addInput(new Form_Input(
446
	'leapfile',
447
	null,
448
	'file'
449
))->addClass('btn-default');
450

    
451
$form->add($section);
452

    
453
print($form);
454

    
455
?>
456

    
457
<script type="text/javascript">
458
//<![CDATA[
459
	// If this variable is declared, any help text will not be deleted when rows are added
460
	// IOW the help text will appear on every row
461
	retainhelp = true;
462
</script>
463

    
464
<script type="text/javascript">
465
//<![CDATA[
466
events.push(function() {
467

    
468
	// Show advanced stats options ============================================
469
	var showadvstats = false;
470

    
471
	function show_advstats(ispageload) {
472
		var text;
473
		// On page load decide the initial state based on the data.
474
		if (ispageload) {
475
<?php
476
			if (!$pconfig['clockstats'] && !$pconfig['loopstats'] && !$pconfig['peerstats']) {
477
				$showadv = false;
478
			} else {
479
				$showadv = true;
480
			}
481
?>
482
			showadvstats = <?php if ($showadv) {echo 'true';} else {echo 'false';} ?>;
483
		} else {
484
			// It was a click, swap the state.
485
			showadvstats = !showadvstats;
486
		}
487

    
488
		hideCheckbox('clockstats', !showadvstats);
489
		hideCheckbox('loopstats', !showadvstats);
490
		hideCheckbox('peerstats', !showadvstats);
491

    
492
		if (showadvstats) {
493
			text = "<?=gettext('Hide Advanced');?>";
494
		} else {
495
			text = "<?=gettext('Display Advanced');?>";
496
		}
497
		$('#btnadvstats').html('<i class="fa fa-cog"></i> ' + text);
498
	}
499

    
500
	$('#btnadvstats').click(function(event) {
501
		show_advstats();
502
	});
503

    
504
	// Show advanced leap second options ======================================
505
	var showadvleap = false;
506

    
507
	function show_advleap(ispageload) {
508
		var text;
509
		// On page load decide the initial state based on the data.
510
		if (ispageload) {
511
<?php
512
			// Note: leapfile is not a field saved in the config, so no need to test for it here.
513
			// leapsec is the encoded text in the config, leaptext is not a pconfig[] key.
514
			if (empty($pconfig['leapsec'])) {
515
				$showadv = false;
516
			} else {
517
				$showadv = true;
518
			}
519
?>
520
			showadvleap = <?php if ($showadv) {echo 'true';} else {echo 'false';} ?>;
521
		} else {
522
			// It was a click, swap the state.
523
			showadvleap = !showadvleap;
524
		}
525

    
526
		hideInput('leaptext', !showadvleap);
527
		hideInput('leapfile', !showadvleap);
528

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

    
537
	$('#btnadvleap').click(function(event) {
538
		show_advleap();
539
	});
540

    
541
	// Set initial states
542
	show_advstats(true);
543
	show_advleap(true);
544

    
545
	// Suppress "Delete row" button if there are fewer than two rows
546
	checkLastRow();
547
});
548
//]]>
549
</script>
550

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