Project

General

Profile

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

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

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

    
35
if (!is_array($config['ntpd'])) {
36
	$config['ntpd'] = array();
37
}
38

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

    
52
if ($_POST) {
53
	unset($input_errors);
54
	$pconfig = $_POST;
55

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

    
60
	if (!$input_errors) {
61
		if (is_array($_POST['interface'])) {
62
			$config['ntpd']['interface'] = implode(",", $_POST['interface']);
63
		} elseif (isset($config['ntpd']['interface'])) {
64
			unset($config['ntpd']['interface']);
65
		}
66

    
67
		if (!empty($_POST['gpsport']) && file_exists('/dev/'.$_POST['gpsport'])) {
68
			$config['ntpd']['gpsport'] = $_POST['gpsport'];
69
		} elseif (isset($config['ntpd']['gpsport'])) {
70
			unset($config['ntpd']['gpsport']);
71
		}
72

    
73
		unset($config['ntpd']['prefer']);
74
		unset($config['ntpd']['noselect']);
75
		unset($config['ntpd']['ispool']);
76
		$timeservers = '';
77

    
78
		for ($i = 0; $i < NUMTIMESERVERS; $i++) {
79
			$tserver = trim($_POST["server{$i}"]);
80
			if (!empty($tserver)) {
81
				$timeservers .= "{$tserver} ";
82
				if (!empty($_POST["servprefer{$i}"])) {
83
					$config['ntpd']['prefer'] .= "{$tserver} ";
84
				}
85
				if (!empty($_POST["servselect{$i}"])) {
86
					$config['ntpd']['noselect'] .= "{$tserver} ";
87
				}
88
				if (!empty($_POST["servispool{$i}"])) {
89
					$config['ntpd']['ispool'] .= "{$tserver} ";
90
				}
91
			}
92
		}
93
		if (trim($timeservers) == "") {
94
			$timeservers = "pool.ntp.org";
95
		}
96
		$config['system']['timeservers'] = trim($timeservers);
97

    
98
		$config['ntpd']['orphan'] = trim($pconfig['ntporphan']);
99

    
100
		if (!empty($_POST['logpeer'])) {
101
			$config['ntpd']['logpeer'] = $_POST['logpeer'];
102
		} elseif (isset($config['ntpd']['logpeer'])) {
103
			unset($config['ntpd']['logpeer']);
104
		}
105

    
106
		if (!empty($_POST['logsys'])) {
107
			$config['ntpd']['logsys'] = $_POST['logsys'];
108
		} elseif (isset($config['ntpd']['logsys'])) {
109
			unset($config['ntpd']['logsys']);
110
		}
111

    
112
		if (!empty($_POST['clockstats'])) {
113
			$config['ntpd']['clockstats'] = $_POST['clockstats'];
114
		} elseif (isset($config['ntpd']['clockstats'])) {
115
			unset($config['ntpd']['clockstats']);
116
		}
117

    
118
		if (!empty($_POST['loopstats'])) {
119
			$config['ntpd']['loopstats'] = $_POST['loopstats'];
120
		} elseif (isset($config['ntpd']['loopstats'])) {
121
			unset($config['ntpd']['loopstats']);
122
		}
123

    
124
		if (!empty($_POST['peerstats'])) {
125
			$config['ntpd']['peerstats'] = $_POST['peerstats'];
126
		} elseif (isset($config['ntpd']['peerstats'])) {
127
			unset($config['ntpd']['peerstats']);
128
		}
129

    
130
		if ((empty($_POST['statsgraph'])) == (isset($config['ntpd']['statsgraph']))) {
131
			$enable_rrd_graphing = true;
132
		}
133
		if (!empty($_POST['statsgraph'])) {
134
			$config['ntpd']['statsgraph'] = $_POST['statsgraph'];
135
		} elseif (isset($config['ntpd']['statsgraph'])) {
136
			unset($config['ntpd']['statsgraph']);
137
		}
138
		if (isset($enable_rrd_graphing)) {
139
			enable_rrd_graphing();
140
		}
141

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

    
148
		if (is_uploaded_file($_FILES['leapfile']['tmp_name'])) {
149
			$config['ntpd']['leapsec'] = base64_encode(file_get_contents($_FILES['leapfile']['tmp_name']));
150
		}
151

    
152
		write_config("Updated NTP Server Settings");
153

    
154
		$changes_applied = true;
155
		$retval = 0;
156
		$retval |= system_ntp_configure();
157
	}
158
}
159

    
160
function build_interface_list() {
161
	global $pconfig;
162

    
163
	$iflist = array('options' => array(), 'selected' => array());
164

    
165
	$interfaces = get_configured_interface_with_descr();
166
	foreach ($interfaces as $iface => $ifacename) {
167
		if (!is_ipaddr(get_interface_ip($iface)) &&
168
		    !is_ipaddrv6(get_interface_ipv6($iface))) {
169
			continue;
170
		}
171

    
172
		$iflist['options'][$iface] = $ifacename;
173

    
174
		if (in_array($iface, $pconfig['interface'])) {
175
			array_push($iflist['selected'], $iface);
176
		}
177
	}
178

    
179
	return($iflist);
180
}
181

    
182
$pconfig = &$config['ntpd'];
183
if (empty($pconfig['interface'])) {
184
	$pconfig['interface'] = array();
185
} else {
186
	$pconfig['interface'] = explode(",", $pconfig['interface']);
187
}
188
$pgtitle = array(gettext("Services"), gettext("NTP"), gettext("Settings"));
189
$pglinks = array("", "@self", "@self");
190
$shortcut_section = "ntp";
191
include("head.inc");
192

    
193
if ($input_errors) {
194
	print_input_errors($input_errors);
195
}
196

    
197
if ($changes_applied) {
198
	print_apply_result_box($retval);
199
}
200

    
201
$tab_array = array();
202
$tab_array[] = array(gettext("Settings"), true, "services_ntpd.php");
203
$tab_array[] = array(gettext("ACLs"), false, "services_ntpd_acls.php");
204
$tab_array[] = array(gettext("Serial GPS"), false, "services_ntpd_gps.php");
205
$tab_array[] = array(gettext("PPS"), false, "services_ntpd_pps.php");
206
display_top_tabs($tab_array);
207

    
208
$form = new Form;
209
$form->setMultipartEncoding();	// Allow file uploads
210

    
211
$section = new Form_Section('NTP Server Configuration');
212

    
213
$iflist = build_interface_list();
214

    
215
$section->addInput(new Form_Select(
216
	'interface',
217
	'Interface',
218
	$iflist['selected'],
219
	$iflist['options'],
220
	true
221
))->setHelp('Interfaces without an IP address will not be shown.%1$s' .
222
			'Selecting no interfaces will listen on all interfaces with a wildcard.%1$s' .
223
			'Selecting all interfaces will explicitly listen on only the interfaces/IPs specified.', '<br />');
224

    
225
$timeservers = explode(' ', $config['system']['timeservers']);
226
$maxrows = max(count($timeservers), 1);
227
$auto_pool_suffix = "pool.ntp.org";
228
for ($counter=0; $counter < $maxrows; $counter++) {
229
	$group = new Form_Group($counter == 0 ? 'Time Servers':'');
230
	$group->addClass('repeatable');
231
	$group->setAttribute('max_repeats', NUMTIMESERVERS);
232
	$group->setAttribute('max_repeats_alert', sprintf(gettext('%d is the maximum number of configured servers.'), NUMTIMESERVERS));
233

    
234
	$group->add(new Form_Input(
235
		'server' . $counter,
236
		null,
237
		'text',
238
		$timeservers[$counter],
239
		['placeholder' => 'Hostname']
240
	 ))->setWidth(3);
241

    
242
	 $group->add(new Form_Checkbox(
243
		'servprefer' . $counter,
244
		null,
245
		null,
246
		isset($config['ntpd']['prefer']) && isset($timeservers[$counter]) && substr_count($config['ntpd']['prefer'], $timeservers[$counter])
247
	 ))->sethelp('Prefer');
248

    
249
	 $group->add(new Form_Checkbox(
250
		'servselect' . $counter,
251
		null,
252
		null,
253
		isset($config['ntpd']['noselect']) && isset($timeservers[$counter]) && substr_count($config['ntpd']['noselect'], $timeservers[$counter])
254
	 ))->sethelp('No Select');
255

    
256
	$group->add(new Form_Checkbox(
257
		'servispool' . $counter,
258
		null,
259
		null,
260
		(substr_compare($timeservers[$counter], $auto_pool_suffix, strlen($timeservers[$counter]) - strlen($auto_pool_suffix), strlen($auto_pool_suffix)) === 0)
261
		 || (isset($config['ntpd']['ispool']) && isset($timeservers[$counter]) && substr_count($config['ntpd']['ispool'], $timeservers[$counter]))
262
	 ))->sethelp('Is a Pool');
263

    
264
	$group->add(new Form_Button(
265
		'deleterow' . $counter,
266
		'Delete',
267
		null,
268
		'fa-trash'
269
	))->addClass('btn-warning');
270

    
271
	 $section->add($group);
272
}
273

    
274
$section->addInput(new Form_Button(
275
	'addrow',
276
	'Add',
277
	null,
278
	'fa-plus'
279
))->addClass('btn-success');
280

    
281
$section->addInput(new Form_StaticText(
282
	null,
283
	$btnaddrow
284
))->setHelp(
285
	'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 ' .
286
	'(%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 ' .
287
	'are configured and they disagree, %2$sneither%3$s will be believed. Options:%1$s' .
288
	'%2$sPrefer%3$s - NTP should favor the use of this server more than all others.%1$s' .
289
	'%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' .
290
	'%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.',
291
	'<br />',
292
	'<b>',
293
	'</b>',
294
	'<a target="_blank" href="https://support.ntp.org/bin/view/Support/ConfiguringNTP">',
295
	'</a>'
296
	);
297

    
298
$section->addInput(new Form_Input(
299
	'ntporphan',
300
	'Orphan Mode',
301
	'text',
302
	$pconfig['orphan'],
303
	['placeholder' => "12"]
304
))->setHelp('Orphan mode allows the system clock to be used when no other clocks are available. ' .
305
			'The number here specifies the stratum reported during orphan mode and should normally be set to a number high enough ' .
306
			'to insure that any other servers available to clients are preferred over this server (default: 12).');
307

    
308
$section->addInput(new Form_Checkbox(
309
	'statsgraph',
310
	'NTP Graphs',
311
	'Enable RRD graphs of NTP statistics (default: disabled).',
312
	$pconfig['statsgraph']
313
));
314

    
315
$section->addInput(new Form_Checkbox(
316
	'logpeer',
317
	'Logging',
318
	'Log peer messages (default: disabled).',
319
	$pconfig['logpeer']
320
));
321

    
322
$section->addInput(new Form_Checkbox(
323
	'logsys',
324
	null,
325
	'Log system messages (default: disabled).',
326
	$pconfig['logsys']
327
))->setHelp('These options enable additional messages from NTP to be written to the System Log %1$sStatus > System Logs > NTP%2$s',
328
			'<a href="status_logs.php?logfile=ntpd">', '</a>.');
329

    
330
// Statistics logging section
331
$btnadv = new Form_Button(
332
	'btnadvstats',
333
	'Display Advanced',
334
	null,
335
	'fa-cog'
336
);
337

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

    
340
$section->addInput(new Form_StaticText(
341
	'Statistics Logging',
342
	$btnadv
343
))->setHelp('Warning: These options will create persistent daily log files in /var/log/ntp.');
344

    
345
$section->addInput(new Form_Checkbox(
346
	'clockstats',
347
	null,
348
	'Log reference clock statistics (default: disabled).',
349
	$pconfig['clockstats']
350
));
351

    
352
$section->addInput(new Form_Checkbox(
353
	'loopstats',
354
	null,
355
	'Log clock discipline statistics (default: disabled).',
356
	$pconfig['loopstats']
357
));
358

    
359
$section->addInput(new Form_Checkbox(
360
	'peerstats',
361
	null,
362
	'Log NTP peer statistics (default: disabled).',
363
	$pconfig['peerstats']
364
));
365

    
366
// Leap seconds section
367
$btnadv = new Form_Button(
368
	'btnadvleap',
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
	'Leap seconds',
378
	$btnadv
379
))->setHelp(
380
	'Leap seconds may be added or subtracted at the end of June or December. Leap seconds are administered by the ' .
381
	'%1$sIERS%2$s, who publish them in their Bulletin C approximately 6 - 12 months in advance.  Normally this correction ' .
382
	'should only be needed if the server is a stratum 1 NTP server, but many NTP servers do not advertise an upcoming leap ' .
383
	'second when other NTP servers synchronise to them.%3$s%4$sIf the leap second is important to your network services, ' .
384
	'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 ' .
385
	'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.',
386
	'<a target="_blank" href="https://www.iers.org">',
387
	'</a>',
388
	'<br />',
389
	'<b>',
390
	'</b>',
391
	'<a target="_blank" href="https://support.ntp.org/bin/view/Support/ConfiguringNTP">',
392
	'<a target="_blank" href="https://www.nist.gov">',
393
	'<a target="_blank" href="https://www.ntp.org">'
394
);
395

    
396
$section->addInput(new Form_Textarea(
397
	'leaptext',
398
	null,
399
	base64_decode(chunk_split($pconfig['leapsec']))
400
))->setHelp('Enter Leap second configuration as text OR select a file to upload.');
401

    
402
$section->addInput(new Form_Input(
403
	'leapfile',
404
	null,
405
	'file'
406
))->addClass('btn-default');
407

    
408
$form->add($section);
409

    
410
print($form);
411

    
412
?>
413

    
414
<script type="text/javascript">
415
//<![CDATA[
416
	// If this variable is declared, any help text will not be deleted when rows are added
417
	// IOW the help text will appear on every row
418
	retainhelp = true;
419
</script>
420

    
421
<script type="text/javascript">
422
//<![CDATA[
423
events.push(function() {
424

    
425
	// Show advanced stats options ============================================
426
	var showadvstats = false;
427

    
428
	function show_advstats(ispageload) {
429
		var text;
430
		// On page load decide the initial state based on the data.
431
		if (ispageload) {
432
<?php
433
			if (!$pconfig['clockstats'] && !$pconfig['loopstats'] && !$pconfig['peerstats']) {
434
				$showadv = false;
435
			} else {
436
				$showadv = true;
437
			}
438
?>
439
			showadvstats = <?php if ($showadv) {echo 'true';} else {echo 'false';} ?>;
440
		} else {
441
			// It was a click, swap the state.
442
			showadvstats = !showadvstats;
443
		}
444

    
445
		hideCheckbox('clockstats', !showadvstats);
446
		hideCheckbox('loopstats', !showadvstats);
447
		hideCheckbox('peerstats', !showadvstats);
448

    
449
		if (showadvstats) {
450
			text = "<?=gettext('Hide Advanced');?>";
451
		} else {
452
			text = "<?=gettext('Display Advanced');?>";
453
		}
454
		$('#btnadvstats').html('<i class="fa fa-cog"></i> ' + text);
455
	}
456

    
457
	$('#btnadvstats').click(function(event) {
458
		show_advstats();
459
	});
460

    
461
	// Show advanced leap second options ======================================
462
	var showadvleap = false;
463

    
464
	function show_advleap(ispageload) {
465
		var text;
466
		// On page load decide the initial state based on the data.
467
		if (ispageload) {
468
<?php
469
			// Note: leapfile is not a field saved in the config, so no need to test for it here.
470
			// leapsec is the encoded text in the config, leaptext is not a pconfig[] key.
471
			if (empty($pconfig['leapsec'])) {
472
				$showadv = false;
473
			} else {
474
				$showadv = true;
475
			}
476
?>
477
			showadvleap = <?php if ($showadv) {echo 'true';} else {echo 'false';} ?>;
478
		} else {
479
			// It was a click, swap the state.
480
			showadvleap = !showadvleap;
481
		}
482

    
483
		hideInput('leaptext', !showadvleap);
484
		hideInput('leapfile', !showadvleap);
485

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

    
494
	$('#btnadvleap').click(function(event) {
495
		show_advleap();
496
	});
497

    
498
	// Set initial states
499
	show_advstats(true);
500
	show_advleap(true);
501

    
502
	// Suppress "Delete row" button if there are fewer than two rows
503
	checkLastRow();
504
});
505
//]]>
506
</script>
507

    
508
<?php include("foot.inc");
(137-137/234)