Project

General

Profile

Download (13.8 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-2016 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.' . '<br />' .
222
			'Selecting no interfaces will listen on all interfaces with a wildcard.' . '<br />' .
223
			'Selecting all interfaces will explicitly listen on only the interfaces/IPs specified.');
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

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

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

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

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

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

    
269
	 $section->add($group);
270
}
271

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

    
279
$section->addInput(new Form_StaticText(
280
	null,
281
	$btnaddrow
282
))->setHelp('For best results three to five servers should be configured here, or at least one pool.' . '<br />' .
283
			'The <b>Prefer</b> option indicates that NTP should favor the use of this server more than all others.' . '<br />' .
284
			'The <b>No Select</b> option indicates that NTP should not use this server for time, but stats for this server will be collected and displayed.' . '<br />' .
285
			'The <b>Is a Pool</b> option indicates this entry is a pool of NTP servers and not a single address. This is assumed for *.pool.ntp.org.');
286

    
287
$section->addInput(new Form_Input(
288
	'ntporphan',
289
	'Orphan Mode',
290
	'text',
291
	$pconfig['orphan'],
292
	['placeholder' => "12"]
293
))->setHelp('Orphan mode allows the system clock to be used when no other clocks are available. ' .
294
			'The number here specifies the stratum reported during orphan mode and should normally be set to a number high enough ' .
295
			'to insure that any other servers available to clients are preferred over this server (default: 12).');
296

    
297
$section->addInput(new Form_Checkbox(
298
	'statsgraph',
299
	'NTP Graphs',
300
	'Enable RRD graphs of NTP statistics (default: disabled).',
301
	$pconfig['statsgraph']
302
));
303

    
304
$section->addInput(new Form_Checkbox(
305
	'logpeer',
306
	'Logging',
307
	'Log peer messages (default: disabled).',
308
	$pconfig['logpeer']
309
));
310

    
311
$section->addInput(new Form_Checkbox(
312
	'logsys',
313
	null,
314
	'Log system messages (default: disabled).',
315
	$pconfig['logsys']
316
))->setHelp('These options enable additional messages from NTP to be written to the System Log ' .
317
			'<a href="status_logs.php?logfile=ntpd">' . 'Status > System Logs > NTP' . '</a>.');
318

    
319
// Statistics logging section
320
$btnadv = new Form_Button(
321
	'btnadvstats',
322
	'Display Advanced',
323
	null,
324
	'fa-cog'
325
);
326

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

    
329
$section->addInput(new Form_StaticText(
330
	'Statistics Logging',
331
	$btnadv
332
))->setHelp('Warning: These options will create persistent daily log files in /var/log/ntp.');
333

    
334
$section->addInput(new Form_Checkbox(
335
	'clockstats',
336
	null,
337
	'Log reference clock statistics (default: disabled).',
338
	$pconfig['clockstats']
339
));
340

    
341
$section->addInput(new Form_Checkbox(
342
	'loopstats',
343
	null,
344
	'Log clock discipline statistics (default: disabled).',
345
	$pconfig['loopstats']
346
));
347

    
348
$section->addInput(new Form_Checkbox(
349
	'peerstats',
350
	null,
351
	'Log NTP peer statistics (default: disabled).',
352
	$pconfig['peerstats']
353
));
354

    
355
// Leap seconds section
356
$btnadv = new Form_Button(
357
	'btnadvleap',
358
	'Display Advanced',
359
	null,
360
	'fa-cog'
361
);
362

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

    
365
$section->addInput(new Form_StaticText(
366
	'Leap seconds',
367
	$btnadv
368
))->setHelp('A leap second file allows NTP to advertise an upcoming leap second addition or subtraction. ' .
369
			'Normally this is only useful if this server is a stratum 1 time server. ');
370

    
371
$section->addInput(new Form_Textarea(
372
	'leaptext',
373
	null,
374
	base64_decode(chunk_split($pconfig['leapsec']))
375
))->setHelp('Enter Leap second configuration as text OR select a file to upload.');
376

    
377
$section->addInput(new Form_Input(
378
	'leapfile',
379
	null,
380
	'file'
381
))->addClass('btn-default');
382

    
383
$form->add($section);
384

    
385
print($form);
386

    
387
?>
388

    
389
<script type="text/javascript">
390
//<![CDATA[
391
	// If this variable is declared, any help text will not be deleted when rows are added
392
	// IOW the help text will appear on every row
393
	retainhelp = true;
394
</script>
395

    
396
<script type="text/javascript">
397
//<![CDATA[
398
events.push(function() {
399

    
400
	// Show advanced stats options ============================================
401
	var showadvstats = false;
402

    
403
	function show_advstats(ispageload) {
404
		var text;
405
		// On page load decide the initial state based on the data.
406
		if (ispageload) {
407
<?php
408
			if (!$pconfig['clockstats'] && !$pconfig['loopstats'] && !$pconfig['peerstats']) {
409
				$showadv = false;
410
			} else {
411
				$showadv = true;
412
			}
413
?>
414
			showadvstats = <?php if ($showadv) {echo 'true';} else {echo 'false';} ?>;
415
		} else {
416
			// It was a click, swap the state.
417
			showadvstats = !showadvstats;
418
		}
419

    
420
		hideCheckbox('clockstats', !showadvstats);
421
		hideCheckbox('loopstats', !showadvstats);
422
		hideCheckbox('peerstats', !showadvstats);
423

    
424
		if (showadvstats) {
425
			text = "<?=gettext('Hide Advanced');?>";
426
		} else {
427
			text = "<?=gettext('Display Advanced');?>";
428
		}
429
		$('#btnadvstats').html('<i class="fa fa-cog"></i> ' + text);
430
	}
431

    
432
	$('#btnadvstats').click(function(event) {
433
		show_advstats();
434
	});
435

    
436
	// Show advanced leap second options ======================================
437
	var showadvleap = false;
438

    
439
	function show_advleap(ispageload) {
440
		var text;
441
		// On page load decide the initial state based on the data.
442
		if (ispageload) {
443
<?php
444
			// Note: leapfile is not a field saved in the config, so no need to test for it here.
445
			// leapsec is the encoded text in the config, leaptext is not a pconfig[] key.
446
			if (empty($pconfig['leapsec'])) {
447
				$showadv = false;
448
			} else {
449
				$showadv = true;
450
			}
451
?>
452
			showadvleap = <?php if ($showadv) {echo 'true';} else {echo 'false';} ?>;
453
		} else {
454
			// It was a click, swap the state.
455
			showadvleap = !showadvleap;
456
		}
457

    
458
		hideInput('leaptext', !showadvleap);
459
		hideInput('leapfile', !showadvleap);
460

    
461
		if (showadvleap) {
462
			text = "<?=gettext('Hide Advanced');?>";
463
		} else {
464
			text = "<?=gettext('Display Advanced');?>";
465
		}
466
		$('#btnadvleap').html('<i class="fa fa-cog"></i> ' + text);
467
	}
468

    
469
	$('#btnadvleap').click(function(event) {
470
		show_advleap();
471
	});
472

    
473
	// Set initial states
474
	show_advstats(true);
475
	show_advleap(true);
476

    
477
	// Suppress "Delete row" button if there are fewer than two rows
478
	checkLastRow();
479
});
480
//]]>
481
</script>
482

    
483
<?php include("foot.inc");
(130-130/225)