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

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

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

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

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

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

    
214
$iflist = build_interface_list();
215

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
367
// Leap seconds section
368
$btnadv = new Form_Button(
369
	'btnadvleap',
370
	'Display Advanced',
371
	null,
372
	'fa-cog'
373
);
374

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

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

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

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

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

    
411
print($form);
412

    
413
?>
414

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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