Project

General

Profile

Download (14 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.%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('For best results three to five servers should be configured here, or at least one pool.%1$s' .
285
			'The %2$sPrefer%3$s option indicates that NTP should favor the use of this server more than all others.%1$s' .
286
			'The %2$sNo Select%3$s option indicates that NTP should not use this server for time, but stats for this server will be collected and displayed.%1$s' .
287
			'The %2$sIs a Pool%3$s option indicates this entry is a pool of NTP servers and not a single address. This is assumed for *.pool.ntp.org.',
288
			'<br />', '<b>', '</b>');
289

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
380
$section->addInput(new Form_Input(
381
	'leapfile',
382
	null,
383
	'file'
384
))->addClass('btn-default');
385

    
386
$form->add($section);
387

    
388
print($form);
389

    
390
?>
391

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

    
399
<script type="text/javascript">
400
//<![CDATA[
401
events.push(function() {
402

    
403
	// Show advanced stats options ============================================
404
	var showadvstats = false;
405

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

    
423
		hideCheckbox('clockstats', !showadvstats);
424
		hideCheckbox('loopstats', !showadvstats);
425
		hideCheckbox('peerstats', !showadvstats);
426

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

    
435
	$('#btnadvstats').click(function(event) {
436
		show_advstats();
437
	});
438

    
439
	// Show advanced leap second options ======================================
440
	var showadvleap = false;
441

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

    
461
		hideInput('leaptext', !showadvleap);
462
		hideInput('leapfile', !showadvleap);
463

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

    
472
	$('#btnadvleap').click(function(event) {
473
		show_advleap();
474
	});
475

    
476
	// Set initial states
477
	show_advstats(true);
478
	show_advleap(true);
479

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

    
486
<?php include("foot.inc");
(128-128/223)