Project

General

Profile

Download (16.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-2019 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

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

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

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

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

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

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

    
73
	if (is_numericint($pconfig['ntpminpoll']) &&
74
	    is_numericint($pconfig['ntpmaxpoll']) &&
75
	    ($pconfig['ntpmaxpoll'] < $pconfig['ntpminpoll'])) {
76
		$input_errors[] = gettext("The supplied value for Minimum Poll Interval is higher than NTP Maximum Poll Interval.");
77
	}
78

    
79
	if (!$input_errors) {
80
		if (is_array($_POST['interface'])) {
81
			$config['ntpd']['interface'] = implode(",", $_POST['interface']);
82
		} elseif (isset($config['ntpd']['interface'])) {
83
			unset($config['ntpd']['interface']);
84
		}
85

    
86
		if (!empty($_POST['gpsport']) && file_exists('/dev/'.$_POST['gpsport'])) {
87
			$config['ntpd']['gpsport'] = $_POST['gpsport'];
88
		} elseif (isset($config['ntpd']['gpsport'])) {
89
			unset($config['ntpd']['gpsport']);
90
		}
91

    
92
		unset($config['ntpd']['prefer']);
93
		unset($config['ntpd']['noselect']);
94
		unset($config['ntpd']['ispool']);
95
		$timeservers = '';
96

    
97
		for ($i = 0; $i < NUMTIMESERVERS; $i++) {
98
			$tserver = trim($_POST["server{$i}"]);
99
			if (!empty($tserver)) {
100
				$timeservers .= "{$tserver} ";
101
				if (!empty($_POST["servprefer{$i}"])) {
102
					$config['ntpd']['prefer'] .= "{$tserver} ";
103
				}
104
				if (!empty($_POST["servselect{$i}"])) {
105
					$config['ntpd']['noselect'] .= "{$tserver} ";
106
				}
107
				if (!empty($_POST["servispool{$i}"])) {
108
					$config['ntpd']['ispool'] .= "{$tserver} ";
109
				}
110
			}
111
		}
112
		if (trim($timeservers) == "") {
113
			$timeservers = "pool.ntp.org";
114
		}
115
		$config['system']['timeservers'] = trim($timeservers);
116

    
117
		$config['ntpd']['orphan'] = trim($pconfig['ntporphan']);
118
		$config['ntpd']['ntpminpoll'] = $pconfig['ntpminpoll'];
119
		$config['ntpd']['ntpmaxpoll'] = $pconfig['ntpmaxpoll'];
120

    
121
		if (!empty($_POST['logpeer'])) {
122
			$config['ntpd']['logpeer'] = $_POST['logpeer'];
123
		} elseif (isset($config['ntpd']['logpeer'])) {
124
			unset($config['ntpd']['logpeer']);
125
		}
126

    
127
		if (!empty($_POST['logsys'])) {
128
			$config['ntpd']['logsys'] = $_POST['logsys'];
129
		} elseif (isset($config['ntpd']['logsys'])) {
130
			unset($config['ntpd']['logsys']);
131
		}
132

    
133
		if (!empty($_POST['clockstats'])) {
134
			$config['ntpd']['clockstats'] = $_POST['clockstats'];
135
		} elseif (isset($config['ntpd']['clockstats'])) {
136
			unset($config['ntpd']['clockstats']);
137
		}
138

    
139
		if (!empty($_POST['loopstats'])) {
140
			$config['ntpd']['loopstats'] = $_POST['loopstats'];
141
		} elseif (isset($config['ntpd']['loopstats'])) {
142
			unset($config['ntpd']['loopstats']);
143
		}
144

    
145
		if (!empty($_POST['peerstats'])) {
146
			$config['ntpd']['peerstats'] = $_POST['peerstats'];
147
		} elseif (isset($config['ntpd']['peerstats'])) {
148
			unset($config['ntpd']['peerstats']);
149
		}
150

    
151
		if ((empty($_POST['statsgraph'])) == (isset($config['ntpd']['statsgraph']))) {
152
			$enable_rrd_graphing = true;
153
		}
154
		if (!empty($_POST['statsgraph'])) {
155
			$config['ntpd']['statsgraph'] = $_POST['statsgraph'];
156
		} elseif (isset($config['ntpd']['statsgraph'])) {
157
			unset($config['ntpd']['statsgraph']);
158
		}
159
		if (isset($enable_rrd_graphing)) {
160
			enable_rrd_graphing();
161
		}
162

    
163
		if (!empty($_POST['leaptext'])) {
164
			$config['ntpd']['leapsec'] = base64_encode($_POST['leaptext']);
165
		} elseif (isset($config['ntpd']['leapsec'])) {
166
			unset($config['ntpd']['leapsec']);
167
		}
168

    
169
		if (is_uploaded_file($_FILES['leapfile']['tmp_name'])) {
170
			$config['ntpd']['leapsec'] = base64_encode(file_get_contents($_FILES['leapfile']['tmp_name']));
171
		}
172

    
173
		write_config("Updated NTP Server Settings");
174

    
175
		$changes_applied = true;
176
		$retval = 0;
177
		$retval |= system_ntp_configure();
178
	}
179
}
180

    
181
function build_interface_list() {
182
	global $pconfig;
183

    
184
	$iflist = array('options' => array(), 'selected' => array());
185

    
186
	$interfaces = get_configured_interface_with_descr();
187
	foreach ($interfaces as $iface => $ifacename) {
188
		if (!is_ipaddr(get_interface_ip($iface)) &&
189
		    !is_ipaddrv6(get_interface_ipv6($iface))) {
190
			continue;
191
		}
192

    
193
		$iflist['options'][$iface] = $ifacename;
194

    
195
		if (in_array($iface, $pconfig['interface'])) {
196
			array_push($iflist['selected'], $iface);
197
		}
198
	}
199

    
200
	return($iflist);
201
}
202

    
203
init_config_arr(array('ntpd'));
204
$pconfig = &$config['ntpd'];
205
if (empty($pconfig['interface'])) {
206
	$pconfig['interface'] = array();
207
} else {
208
	$pconfig['interface'] = explode(",", $pconfig['interface']);
209
}
210
$pgtitle = array(gettext("Services"), gettext("NTP"), gettext("Settings"));
211
$pglinks = array("", "@self", "@self");
212
$shortcut_section = "ntp";
213
include("head.inc");
214

    
215
if ($input_errors) {
216
	print_input_errors($input_errors);
217
}
218

    
219
if ($changes_applied) {
220
	print_apply_result_box($retval);
221
}
222

    
223
$tab_array = array();
224
$tab_array[] = array(gettext("Settings"), true, "services_ntpd.php");
225
$tab_array[] = array(gettext("ACLs"), false, "services_ntpd_acls.php");
226
$tab_array[] = array(gettext("Serial GPS"), false, "services_ntpd_gps.php");
227
$tab_array[] = array(gettext("PPS"), false, "services_ntpd_pps.php");
228
display_top_tabs($tab_array);
229

    
230
$form = new Form;
231
$form->setMultipartEncoding();	// Allow file uploads
232

    
233
$section = new Form_Section('NTP Server Configuration');
234

    
235
$iflist = build_interface_list();
236

    
237
$section->addInput(new Form_Select(
238
	'interface',
239
	'Interface',
240
	$iflist['selected'],
241
	$iflist['options'],
242
	true
243
))->setHelp('Interfaces without an IP address will not be shown.%1$s' .
244
			'Selecting no interfaces will listen on all interfaces with a wildcard.%1$s' .
245
			'Selecting all interfaces will explicitly listen on only the interfaces/IPs specified.', '<br />');
246

    
247
$timeservers = explode(' ', $config['system']['timeservers']);
248
$maxrows = max(count($timeservers), 1);
249
$auto_pool_suffix = "pool.ntp.org";
250
for ($counter=0; $counter < $maxrows; $counter++) {
251
	$group = new Form_Group($counter == 0 ? 'Time Servers':'');
252
	$group->addClass('repeatable');
253
	$group->setAttribute('max_repeats', NUMTIMESERVERS);
254
	$group->setAttribute('max_repeats_alert', sprintf(gettext('%d is the maximum number of configured servers.'), NUMTIMESERVERS));
255

    
256
	$group->add(new Form_Input(
257
		'server' . $counter,
258
		null,
259
		'text',
260
		$timeservers[$counter],
261
		['placeholder' => 'Hostname']
262
	 ))->setWidth(3);
263

    
264
	 $group->add(new Form_Checkbox(
265
		'servprefer' . $counter,
266
		null,
267
		null,
268
		isset($config['ntpd']['prefer']) && isset($timeservers[$counter]) && substr_count($config['ntpd']['prefer'], $timeservers[$counter])
269
	 ))->sethelp('Prefer');
270

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

    
278
	$group->add(new Form_Checkbox(
279
		'servispool' . $counter,
280
		null,
281
		null,
282
		(substr_compare($timeservers[$counter], $auto_pool_suffix, strlen($timeservers[$counter]) - strlen($auto_pool_suffix), strlen($auto_pool_suffix)) === 0)
283
		 || (isset($config['ntpd']['ispool']) && isset($timeservers[$counter]) && substr_count($config['ntpd']['ispool'], $timeservers[$counter]))
284
	 ))->sethelp('Is a Pool');
285

    
286
	$group->add(new Form_Button(
287
		'deleterow' . $counter,
288
		'Delete',
289
		null,
290
		'fa-trash'
291
	))->addClass('btn-warning');
292

    
293
	 $section->add($group);
294
}
295

    
296
$section->addInput(new Form_Button(
297
	'addrow',
298
	'Add',
299
	null,
300
	'fa-plus'
301
))->addClass('btn-success');
302

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

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

    
330
$section->addInput(new Form_Select(
331
	'ntpminpoll',
332
	'Minimum Poll Interval',
333
	$pconfig['ntpminpoll'],
334
	$ntp_poll_values,
335
))->setHelp('Minimum poll interval for NTP messages. If set, must be less than or equal to Maximum Poll Interval.');
336

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

    
344
$section->addInput(new Form_Checkbox(
345
	'statsgraph',
346
	'NTP Graphs',
347
	'Enable RRD graphs of NTP statistics (default: disabled).',
348
	$pconfig['statsgraph']
349
));
350

    
351
$section->addInput(new Form_Checkbox(
352
	'logpeer',
353
	'Logging',
354
	'Log peer messages (default: disabled).',
355
	$pconfig['logpeer']
356
));
357

    
358
$section->addInput(new Form_Checkbox(
359
	'logsys',
360
	null,
361
	'Log system messages (default: disabled).',
362
	$pconfig['logsys']
363
))->setHelp('These options enable additional messages from NTP to be written to the System Log %1$sStatus > System Logs > NTP%2$s',
364
			'<a href="status_logs.php?logfile=ntpd">', '</a>.');
365

    
366
// Statistics logging section
367
$btnadv = new Form_Button(
368
	'btnadvstats',
369
	'Display Advanced',
370
	null,
371
	'fa-cog'
372
);
373

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

    
376
$section->addInput(new Form_StaticText(
377
	'Statistics Logging',
378
	$btnadv
379
))->setHelp('Warning: These options will create persistent daily log files in /var/log/ntp.');
380

    
381
$section->addInput(new Form_Checkbox(
382
	'clockstats',
383
	null,
384
	'Log reference clock statistics (default: disabled).',
385
	$pconfig['clockstats']
386
));
387

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

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

    
402
// Leap seconds section
403
$btnadv = new Form_Button(
404
	'btnadvleap',
405
	'Display Advanced',
406
	null,
407
	'fa-cog'
408
);
409

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

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

    
432
$section->addInput(new Form_Textarea(
433
	'leaptext',
434
	null,
435
	base64_decode(chunk_split($pconfig['leapsec']))
436
))->setHelp('Enter Leap second configuration as text OR select a file to upload.');
437

    
438
$section->addInput(new Form_Input(
439
	'leapfile',
440
	null,
441
	'file'
442
))->addClass('btn-default');
443

    
444
$form->add($section);
445

    
446
print($form);
447

    
448
?>
449

    
450
<script type="text/javascript">
451
//<![CDATA[
452
	// If this variable is declared, any help text will not be deleted when rows are added
453
	// IOW the help text will appear on every row
454
	retainhelp = true;
455
</script>
456

    
457
<script type="text/javascript">
458
//<![CDATA[
459
events.push(function() {
460

    
461
	// Show advanced stats options ============================================
462
	var showadvstats = false;
463

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

    
481
		hideCheckbox('clockstats', !showadvstats);
482
		hideCheckbox('loopstats', !showadvstats);
483
		hideCheckbox('peerstats', !showadvstats);
484

    
485
		if (showadvstats) {
486
			text = "<?=gettext('Hide Advanced');?>";
487
		} else {
488
			text = "<?=gettext('Display Advanced');?>";
489
		}
490
		$('#btnadvstats').html('<i class="fa fa-cog"></i> ' + text);
491
	}
492

    
493
	$('#btnadvstats').click(function(event) {
494
		show_advstats();
495
	});
496

    
497
	// Show advanced leap second options ======================================
498
	var showadvleap = false;
499

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

    
519
		hideInput('leaptext', !showadvleap);
520
		hideInput('leapfile', !showadvleap);
521

    
522
		if (showadvleap) {
523
			text = "<?=gettext('Hide Advanced');?>";
524
		} else {
525
			text = "<?=gettext('Display Advanced');?>";
526
		}
527
		$('#btnadvleap').html('<i class="fa fa-cog"></i> ' + text);
528
	}
529

    
530
	$('#btnadvleap').click(function(event) {
531
		show_advleap();
532
	});
533

    
534
	// Set initial states
535
	show_advstats(true);
536
	show_advleap(true);
537

    
538
	// Suppress "Delete row" button if there are fewer than two rows
539
	checkLastRow();
540
});
541
//]]>
542
</script>
543

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