Project

General

Profile

Download (13 KB) Statistics
| Branch: | Tag: | Revision:
1 cf180ccc jim-p
<?php
2
/*
3 c5d81585 Renato Botelho
 * services_ntpd.php
4
 *
5
 * part of pfSense (https://www.pfsense.org)
6
 * Copyright (c) 2004-2016 Electric Sheep Fencing, LLC
7
 * Copyright (c) 2013 Dagorlad
8
 * All rights reserved.
9
 *
10 b12ea3fb Renato Botelho
 * 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 c5d81585 Renato Botelho
 *
14 b12ea3fb Renato Botelho
 * http://www.apache.org/licenses/LICENSE-2.0
15 c5d81585 Renato Botelho
 *
16 b12ea3fb Renato Botelho
 * 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 f4439c00 Stephen Beaver
 */
22 cf180ccc jim-p
23
##|+PRIV
24
##|*IDENT=page-services-ntpd
25 448280c3 k-paulius
##|*NAME=Services: NTP Settings
26
##|*DESCR=Allow access to the 'Services: NTP Settings' page.
27 cf180ccc jim-p
##|*MATCH=services_ntpd.php*
28
##|-PRIV
29
30 820562e8 NewEraCracker
define('NUMTIMESERVERS', 10);		// The maximum number of configurable time servers
31 c81ef6e2 Phil Davis
require_once("guiconfig.inc");
32 c1e68244 nagyrobi
require_once('rrd.inc');
33
require_once("shaper.inc");
34 cf180ccc jim-p
35 7a6f0ebc Phil Davis
if (!is_array($config['ntpd'])) {
36 08005d0a Ermal
	$config['ntpd'] = array();
37 7a6f0ebc Phil Davis
}
38 08005d0a Ermal
39
if (empty($config['ntpd']['interface'])) {
40
	if (is_array($config['installedpackages']['openntpd']) && is_array($config['installedpackages']['openntpd']['config']) &&
41 20db3e1a Phil Davis
	    is_array($config['installedpackages']['openntpd']['config'][0]) && !empty($config['installedpackages']['openntpd']['config'][0]['interface'])) {
42 46ca7f3d jim-p
		$pconfig['interface'] = explode(",", $config['installedpackages']['openntpd']['config'][0]['interface']);
43 cf180ccc jim-p
		unset($config['installedpackages']['openntpd']);
44 4bfc3f7d Phil Davis
		write_config(gettext("Upgraded settings from openttpd"));
45 7a6f0ebc Phil Davis
	} else {
46 cf180ccc jim-p
		$pconfig['interface'] = array();
47 7a6f0ebc Phil Davis
	}
48
} else {
49 cf180ccc jim-p
	$pconfig['interface'] = explode(",", $config['ntpd']['interface']);
50 7a6f0ebc Phil Davis
}
51 cf180ccc jim-p
52 08d56bd1 sbeaver
if ($_POST) {
53 cf180ccc jim-p
	unset($input_errors);
54
	$pconfig = $_POST;
55
56
	if (!$input_errors) {
57 7a6f0ebc Phil Davis
		if (is_array($_POST['interface'])) {
58 58168f4e jim-p
			$config['ntpd']['interface'] = implode(",", $_POST['interface']);
59 7a6f0ebc Phil Davis
		} elseif (isset($config['ntpd']['interface'])) {
60 58168f4e jim-p
			unset($config['ntpd']['interface']);
61 7a6f0ebc Phil Davis
		}
62 cf180ccc jim-p
63 7a6f0ebc Phil Davis
		if (!empty($_POST['gpsport']) && file_exists('/dev/'.$_POST['gpsport'])) {
64 5c8843d5 jim-p
			$config['ntpd']['gpsport'] = $_POST['gpsport'];
65 7a6f0ebc Phil Davis
		} elseif (isset($config['ntpd']['gpsport'])) {
66 5c8843d5 jim-p
			unset($config['ntpd']['gpsport']);
67 7a6f0ebc Phil Davis
		}
68 5c8843d5 jim-p
69 c1e68244 nagyrobi
		unset($config['ntpd']['prefer']);
70
		unset($config['ntpd']['noselect']);
71
		$timeservers = '';
72 08d56bd1 sbeaver
73 83b9d03e Phil Davis
		for ($i = 0; $i < NUMTIMESERVERS; $i++) {
74 08005d0a Ermal
			$tserver = trim($_POST["server{$i}"]);
75 c1e68244 nagyrobi
			if (!empty($tserver)) {
76
				$timeservers .= "{$tserver} ";
77 7a6f0ebc Phil Davis
				if (!empty($_POST["servprefer{$i}"])) {
78
					$config['ntpd']['prefer'] .= "{$tserver} ";
79
				}
80
				if (!empty($_POST["servselect{$i}"])) {
81
					$config['ntpd']['noselect'] .= "{$tserver} ";
82
				}
83 c1e68244 nagyrobi
			}
84
		}
85 7a6f0ebc Phil Davis
		if (trim($timeservers) == "") {
86 4e6b0a0e nagyrobi
			$timeservers = "pool.ntp.org";
87 7a6f0ebc Phil Davis
		}
88 c1e68244 nagyrobi
		$config['system']['timeservers'] = trim($timeservers);
89
90 7a6f0ebc Phil Davis
		if (!empty($_POST['ntporphan']) && ($_POST['ntporphan'] < 17) && ($_POST['ntporphan'] != '12')) {
91 c1e68244 nagyrobi
			$config['ntpd']['orphan'] = $_POST['ntporphan'];
92 7a6f0ebc Phil Davis
		} elseif (isset($config['ntpd']['orphan'])) {
93 c1e68244 nagyrobi
			unset($config['ntpd']['orphan']);
94 7a6f0ebc Phil Davis
		}
95 c1e68244 nagyrobi
96 7a6f0ebc Phil Davis
		if (!empty($_POST['logpeer'])) {
97 c1e68244 nagyrobi
			$config['ntpd']['logpeer'] = $_POST['logpeer'];
98 7a6f0ebc Phil Davis
		} elseif (isset($config['ntpd']['logpeer'])) {
99 c1e68244 nagyrobi
			unset($config['ntpd']['logpeer']);
100 7a6f0ebc Phil Davis
		}
101 c1e68244 nagyrobi
102 7a6f0ebc Phil Davis
		if (!empty($_POST['logsys'])) {
103 c1e68244 nagyrobi
			$config['ntpd']['logsys'] = $_POST['logsys'];
104 7a6f0ebc Phil Davis
		} elseif (isset($config['ntpd']['logsys'])) {
105 c1e68244 nagyrobi
			unset($config['ntpd']['logsys']);
106 7a6f0ebc Phil Davis
		}
107 c1e68244 nagyrobi
108 7a6f0ebc Phil Davis
		if (!empty($_POST['clockstats'])) {
109 c1e68244 nagyrobi
			$config['ntpd']['clockstats'] = $_POST['clockstats'];
110 7a6f0ebc Phil Davis
		} elseif (isset($config['ntpd']['clockstats'])) {
111 c1e68244 nagyrobi
			unset($config['ntpd']['clockstats']);
112 7a6f0ebc Phil Davis
		}
113 c1e68244 nagyrobi
114 7a6f0ebc Phil Davis
		if (!empty($_POST['loopstats'])) {
115 c1e68244 nagyrobi
			$config['ntpd']['loopstats'] = $_POST['loopstats'];
116 7a6f0ebc Phil Davis
		} elseif (isset($config['ntpd']['loopstats'])) {
117 c1e68244 nagyrobi
			unset($config['ntpd']['loopstats']);
118 7a6f0ebc Phil Davis
		}
119 c1e68244 nagyrobi
120 7a6f0ebc Phil Davis
		if (!empty($_POST['peerstats'])) {
121 c1e68244 nagyrobi
			$config['ntpd']['peerstats'] = $_POST['peerstats'];
122 7a6f0ebc Phil Davis
		} elseif (isset($config['ntpd']['peerstats'])) {
123 c1e68244 nagyrobi
			unset($config['ntpd']['peerstats']);
124 7a6f0ebc Phil Davis
		}
125 c1e68244 nagyrobi
126 7a6f0ebc Phil Davis
		if ((empty($_POST['statsgraph'])) == (isset($config['ntpd']['statsgraph']))) {
127 e2caaee8 k-paulius
			$enable_rrd_graphing = true;
128 7a6f0ebc Phil Davis
		}
129
		if (!empty($_POST['statsgraph'])) {
130 c1e68244 nagyrobi
			$config['ntpd']['statsgraph'] = $_POST['statsgraph'];
131 7a6f0ebc Phil Davis
		} elseif (isset($config['ntpd']['statsgraph'])) {
132 c1e68244 nagyrobi
			unset($config['ntpd']['statsgraph']);
133 7a6f0ebc Phil Davis
		}
134
		if (isset($enable_rrd_graphing)) {
135 e2caaee8 k-paulius
			enable_rrd_graphing();
136 7a6f0ebc Phil Davis
		}
137 c1e68244 nagyrobi
138 f99f8a67 Phil Davis
		if (!empty($_POST['leaptext'])) {
139
			$config['ntpd']['leapsec'] = base64_encode($_POST['leaptext']);
140 7a6f0ebc Phil Davis
		} elseif (isset($config['ntpd']['leapsec'])) {
141 c1e68244 nagyrobi
			unset($config['ntpd']['leapsec']);
142 7a6f0ebc Phil Davis
		}
143 c1e68244 nagyrobi
144 7a6f0ebc Phil Davis
		if (is_uploaded_file($_FILES['leapfile']['tmp_name'])) {
145 c1e68244 nagyrobi
			$config['ntpd']['leapsec'] = base64_encode(file_get_contents($_FILES['leapfile']['tmp_name']));
146 7a6f0ebc Phil Davis
		}
147 c1e68244 nagyrobi
148 cf180ccc jim-p
		write_config("Updated NTP Server Settings");
149
150
		$retval = 0;
151
		$retval = system_ntp_configure();
152
		$savemsg = get_std_save_message($retval);
153 08d56bd1 sbeaver
	}
154
}
155
156
function build_interface_list() {
157
	global $pconfig;
158
159
	$iflist = array('options' => array(), 'selected' => array());
160
161
	$interfaces = get_configured_interface_with_descr();
162
	foreach ($interfaces as $iface => $ifacename) {
163 2a5960b0 Luiz Otavio O Souza
		if (!is_ipaddr(get_interface_ip($iface)) &&
164
		    !is_ipaddrv6(get_interface_ipv6($iface))) {
165 08d56bd1 sbeaver
			continue;
166 1fe07ba8 Chris Buechler
		}
167 08d56bd1 sbeaver
168 d05950fb Stephen Beaver
		$iflist['options'][$iface] = $ifacename;
169 08d56bd1 sbeaver
170 1fe07ba8 Chris Buechler
		if (in_array($iface, $pconfig['interface'])) {
171
			array_push($iflist['selected'], $iface);
172
		}
173 cf180ccc jim-p
	}
174 08d56bd1 sbeaver
175
	return($iflist);
176 cf180ccc jim-p
}
177 08d56bd1 sbeaver
178 c1e68244 nagyrobi
$pconfig = &$config['ntpd'];
179 7a6f0ebc Phil Davis
if (empty($pconfig['interface'])) {
180 63d5a5e0 Jean Cyr
	$pconfig['interface'] = array();
181 7a6f0ebc Phil Davis
} else {
182 63d5a5e0 Jean Cyr
	$pconfig['interface'] = explode(",", $pconfig['interface']);
183 7a6f0ebc Phil Davis
}
184 448280c3 k-paulius
$pgtitle = array(gettext("Services"), gettext("NTP"), gettext("Settings"));
185 b32dd0a6 jim-p
$shortcut_section = "ntp";
186 cf180ccc jim-p
include("head.inc");
187
188 20db3e1a Phil Davis
if ($input_errors) {
189 08d56bd1 sbeaver
	print_input_errors($input_errors);
190 20db3e1a Phil Davis
}
191
if ($savemsg) {
192 08d56bd1 sbeaver
	print_info_box($savemsg, 'success');
193 20db3e1a Phil Davis
}
194 08d56bd1 sbeaver
195
$tab_array = array();
196 448280c3 k-paulius
$tab_array[] = array(gettext("Settings"), true, "services_ntpd.php");
197 31b15180 jim-p
$tab_array[] = array(gettext("ACLs"), false, "services_ntpd_acls.php");
198 08d56bd1 sbeaver
$tab_array[] = array(gettext("Serial GPS"), false, "services_ntpd_gps.php");
199
$tab_array[] = array(gettext("PPS"), false, "services_ntpd_pps.php");
200
display_top_tabs($tab_array);
201
202
$form = new Form;
203 76763c4c Phil Davis
$form->setMultipartEncoding();	// Allow file uploads
204 08d56bd1 sbeaver
205 70dc5cd6 Phil Davis
$section = new Form_Section('NTP Server Configuration');
206 08d56bd1 sbeaver
207
$iflist = build_interface_list();
208
209
$section->addInput(new Form_Select(
210
	'interface',
211
	'Interface',
212
	$iflist['selected'],
213
	$iflist['options'],
214
	true
215
))->setHelp('Interfaces without an IP address will not be shown.' . '<br />' .
216
			'Selecting no interfaces will listen on all interfaces with a wildcard.' . '<br />' .
217
			'Selecting all interfaces will explicitly listen on only the interfaces/IPs specified.');
218
219 20db3e1a Phil Davis
$timeservers = explode(' ', $config['system']['timeservers']);
220 83b9d03e Phil Davis
$maxrows = max(count($timeservers), 1);
221 f4439c00 Stephen Beaver
for ($counter=0; $counter < $maxrows; $counter++) {
222 ff8e3635 jim-p
	$group = new Form_Group($counter == 0 ? 'Time Servers':'');
223 f4439c00 Stephen Beaver
	$group->addClass('repeatable');
224 08d56bd1 sbeaver
225
	$group->add(new Form_Input(
226 f4439c00 Stephen Beaver
		'server' . $counter,
227 08d56bd1 sbeaver
		null,
228
		'text',
229 f4439c00 Stephen Beaver
		$timeservers[$counter],
230
		['placeholder' => 'Hostname']
231
	 ))->setWidth(3);
232 08d56bd1 sbeaver
233
	 $group->add(new Form_Checkbox(
234 f4439c00 Stephen Beaver
		'servprefer' . $counter,
235 08d56bd1 sbeaver
		null,
236 f4439c00 Stephen Beaver
		null,
237
		isset($config['ntpd']['prefer']) && isset($timeservers[$counter]) && substr_count($config['ntpd']['prefer'], $timeservers[$counter])
238
	 ))->sethelp('Prefer');
239 08d56bd1 sbeaver
240
	 $group->add(new Form_Checkbox(
241 f4439c00 Stephen Beaver
		'servselect' . $counter,
242
		null,
243 08d56bd1 sbeaver
		null,
244 f4439c00 Stephen Beaver
		isset($config['ntpd']['noselect']) && isset($timeservers[$counter]) && substr_count($config['ntpd']['noselect'], $timeservers[$counter])
245
	 ))->sethelp('No Select');
246
247
	$group->add(new Form_Button(
248
		'deleterow' . $counter,
249 faab522f Renato Botelho
		'Delete',
250 cd7ddae6 jim-p
		null,
251
		'fa-trash'
252
	))->addClass('btn-warning');
253 08d56bd1 sbeaver
254
	 $section->add($group);
255
}
256
257 f4439c00 Stephen Beaver
$section->addInput(new Form_Button(
258
	'addrow',
259 faab522f Renato Botelho
	'Add',
260 cd7ddae6 jim-p
	null,
261
	'fa-plus'
262
))->addClass('btn-success');
263 08d56bd1 sbeaver
264
$section->addInput(new Form_StaticText(
265
	null,
266
	$btnaddrow
267
))->setHelp('For best results three to five servers should be configured here.' . '<br />' .
268
			'The prefer option indicates that NTP should favor the use of this server more than all others.' . '<br />' .
269 bb80af58 NOYB
			'The no select option indicates that NTP should not use this server for time, but stats for this server will be collected and displayed.');
270 08d56bd1 sbeaver
271
$section->addInput(new Form_Input(
272
	'ntporphan',
273 ff8e3635 jim-p
	'Orphan Mode',
274 08d56bd1 sbeaver
	'text',
275
	$pconfig['ntporphan']
276
))->setHelp('Orphan mode allows the system clock to be used when no other clocks are available. ' .
277
			'The number here specifies the stratum reported during orphan mode and should normally be set to a number high enough ' .
278 bb80af58 NOYB
			'to insure that any other servers available to clients are preferred over this server (default: 12).');
279 08d56bd1 sbeaver
280
$section->addInput(new Form_Checkbox(
281
	'statsgraph',
282
	'NTP Graphs',
283
	'Enable RRD graphs of NTP statistics (default: disabled).',
284
	$pconfig['statsgraph']
285
));
286
287
$section->addInput(new Form_Checkbox(
288
	'logpeer',
289 ff8e3635 jim-p
	'Logging',
290
	'Log peer messages (default: disabled).',
291 08d56bd1 sbeaver
	$pconfig['logpeer']
292
));
293
294
$section->addInput(new Form_Checkbox(
295
	'logsys',
296
	null,
297 ff8e3635 jim-p
	'Log system messages (default: disabled).',
298 08d56bd1 sbeaver
	$pconfig['logsys']
299
))->setHelp('These options enable additional messages from NTP to be written to the System Log ' .
300 e78ecb96 NOYB
			'<a href="status_logs.php?logfile=ntpd">' . 'Status > System Logs > NTP' . '</a>.');
301 08d56bd1 sbeaver
302
// Statistics logging section
303 c0d1ceda Phil Davis
$btnadv = new Form_Button(
304 08d56bd1 sbeaver
	'btnadvstats',
305 c0d1ceda Phil Davis
	'Display Advanced',
306 3314e626 jim-p
	null,
307
	'fa-cog'
308 08d56bd1 sbeaver
);
309
310 347c0214 Phil Davis
$btnadv->setAttribute('type','button')->addClass('btn-info btn-sm');
311 08d56bd1 sbeaver
312
$section->addInput(new Form_StaticText(
313 ff8e3635 jim-p
	'Statistics Logging',
314 c0d1ceda Phil Davis
	$btnadv
315 1f1cd32f Chris Buechler
))->setHelp('Warning: These options will create persistent daily log files in /var/log/ntp.');
316 08d56bd1 sbeaver
317
$section->addInput(new Form_Checkbox(
318
	'clockstats',
319
	null,
320 ff8e3635 jim-p
	'Log reference clock statistics (default: disabled).',
321 08d56bd1 sbeaver
	$pconfig['clockstats']
322
));
323
324
$section->addInput(new Form_Checkbox(
325
	'loopstats',
326
	null,
327 ff8e3635 jim-p
	'Log clock discipline statistics (default: disabled).',
328 08d56bd1 sbeaver
	$pconfig['loopstats']
329
));
330
331
$section->addInput(new Form_Checkbox(
332
	'peerstats',
333
	null,
334 ff8e3635 jim-p
	'Log NTP peer statistics (default: disabled).',
335 08d56bd1 sbeaver
	$pconfig['peerstats']
336
));
337
338
// Leap seconds section
339 c0d1ceda Phil Davis
$btnadv = new Form_Button(
340
	'btnadvleap',
341
	'Display Advanced',
342 3314e626 jim-p
	null,
343
	'fa-cog'
344 08d56bd1 sbeaver
);
345
346 347c0214 Phil Davis
$btnadv->setAttribute('type','button')->addClass('btn-info btn-sm');
347 08d56bd1 sbeaver
348
$section->addInput(new Form_StaticText(
349
	'Leap seconds',
350 c0d1ceda Phil Davis
	$btnadv
351 bb80af58 NOYB
))->setHelp('A leap second file allows NTP to advertise an upcoming leap second addition or subtraction. ' .
352 08d56bd1 sbeaver
			'Normally this is only useful if this server is a stratum 1 time server. ');
353
354
$section->addInput(new Form_Textarea(
355
	'leaptext',
356
	null,
357
	base64_decode(chunk_split($pconfig['leapsec']))
358 e78ecb96 NOYB
))->setHelp('Enter Leap second configuration as text OR select a file to upload.');
359 08d56bd1 sbeaver
360
$section->addInput(new Form_Input(
361
	'leapfile',
362
	null,
363
	'file'
364
))->addClass('btn-default');
365
366
$form->add($section);
367 31b15180 jim-p
368 08d56bd1 sbeaver
print($form);
369
370 cf180ccc jim-p
?>
371
372 8fd9052f Colin Fleming
<script type="text/javascript">
373
//<![CDATA[
374 08ec2d99 Stephen Beaver
	// If this variable is declared, any help text will not be deleted when rows are added
375
	// IOW the help text will appear on every row
376
	retainhelp = true;
377
</script>
378
379 8fd9052f Colin Fleming
<script type="text/javascript">
380 d9555fc5 Colin Fleming
//<![CDATA[
381 20db3e1a Phil Davis
events.push(function() {
382 08d56bd1 sbeaver
383 c0d1ceda Phil Davis
	// Show advanced stats options ============================================
384
	var showadvstats = false;
385 08d56bd1 sbeaver
386 c0d1ceda Phil Davis
	function show_advstats(ispageload) {
387
		var text;
388
		// On page load decide the initial state based on the data.
389
		if (ispageload) {
390
<?php
391
			if (!$pconfig['clockstats'] && !$pconfig['loopstats'] && !$pconfig['peerstats']) {
392
				$showadv = false;
393
			} else {
394
				$showadv = true;
395
			}
396
?>
397
			showadvstats = <?php if ($showadv) {echo 'true';} else {echo 'false';} ?>;
398
		} else {
399
			// It was a click, swap the state.
400
			showadvstats = !showadvstats;
401
		}
402
403
		hideCheckbox('clockstats', !showadvstats);
404
		hideCheckbox('loopstats', !showadvstats);
405
		hideCheckbox('peerstats', !showadvstats);
406
407
		if (showadvstats) {
408
			text = "<?=gettext('Hide Advanced');?>";
409
		} else {
410
			text = "<?=gettext('Display Advanced');?>";
411
		}
412
		$('#btnadvstats').html('<i class="fa fa-cog"></i> ' + text);
413
	}
414
415
	$('#btnadvstats').click(function(event) {
416
		show_advstats();
417 08d56bd1 sbeaver
	});
418
419 c0d1ceda Phil Davis
	// Show advanced leap second options ======================================
420
	var showadvleap = false;
421
422
	function show_advleap(ispageload) {
423
		var text;
424
		// On page load decide the initial state based on the data.
425
		if (ispageload) {
426
<?php
427
			// Note: leapfile is not a field saved in the config, so no need to test for it here.
428
			// leapsec is the encoded text in the config, leaptext is not a pconfig[] key.
429
			if (empty($pconfig['leapsec'])) {
430
				$showadv = false;
431
			} else {
432
				$showadv = true;
433
			}
434
?>
435
			showadvleap = <?php if ($showadv) {echo 'true';} else {echo 'false';} ?>;
436
		} else {
437
			// It was a click, swap the state.
438
			showadvleap = !showadvleap;
439
		}
440
441
		hideInput('leaptext', !showadvleap);
442
		hideInput('leapfile', !showadvleap);
443
444
		if (showadvleap) {
445
			text = "<?=gettext('Hide Advanced');?>";
446
		} else {
447
			text = "<?=gettext('Display Advanced');?>";
448
		}
449
		$('#btnadvleap').html('<i class="fa fa-cog"></i> ' + text);
450
	}
451 08d56bd1 sbeaver
452 c0d1ceda Phil Davis
	$('#btnadvleap').click(function(event) {
453
		show_advleap();
454 08d56bd1 sbeaver
	});
455
456 c0d1ceda Phil Davis
	// Set initial states
457
	show_advstats(true);
458
	show_advleap(true);
459 0bc61baa Stephen Beaver
460
	// Suppress "Delete row" button if there are fewer than two rows
461
	checkLastRow();
462 08d56bd1 sbeaver
});
463 d9555fc5 Colin Fleming
//]]>
464 c1e68244 nagyrobi
</script>
465 cf180ccc jim-p
466 c10cb196 Stephen Beaver
<?php include("foot.inc");